Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nfs-utils / utils / mountd / rmtab.c
blob5787ed6a0bebacdc9f4bb270d933833e28f425bf
1 /*
2 * utils/mountd/rmtab.c
4 * Manage the rmtab file for mountd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <netdb.h>
19 #include "xmalloc.h"
20 #include "misc.h"
21 #include "exportfs.h"
22 #include "xio.h"
23 #include "mountd.h"
24 #include "ha-callout.h"
26 #include <limits.h> /* PATH_MAX */
28 extern int reverse_resolve;
30 /* If new path is a link do not destroy it but place the
31 * file where the link points.
34 static int
35 slink_safe_rename(const char * oldpath, const char * newpath)
37 int r;
38 struct stat s;
39 char slink_path[PATH_MAX];
40 const char *real_newpath = newpath;
42 if ((lstat(newpath, &s) == 0) && S_ISLNK(s.st_mode)) {
43 /* New path is a symbolic link, do not destroy but follow */
44 if ((r = readlink(newpath, slink_path, PATH_MAX - 1)) == -1)
45 return -1;
46 slink_path[r] = '\0';
47 real_newpath = slink_path;
50 return rename(oldpath, real_newpath);
53 void
54 mountlist_add(char *host, const char *path)
56 struct rmtabent xe;
57 struct rmtabent *rep;
58 int lockid;
59 long pos;
61 if ((lockid = xflock(_PATH_RMTAB, "a")) < 0)
62 return;
63 setrmtabent("r+");
64 while ((rep = getrmtabent(1, &pos)) != NULL) {
65 if (strcmp (rep->r_client,
66 host) == 0
67 && strcmp(rep->r_path, path) == 0) {
68 rep->r_count++;
69 /* PRC: do the HA callout: */
70 ha_callout("mount", rep->r_client, rep->r_path, rep->r_count);
71 putrmtabent(rep, &pos);
72 endrmtabent();
73 xfunlock(lockid);
74 return;
77 endrmtabent();
78 strncpy(xe.r_client, host,
79 sizeof (xe.r_client) - 1);
80 xe.r_client [sizeof (xe.r_client) - 1] = '\0';
81 strncpy(xe.r_path, path, sizeof (xe.r_path) - 1);
82 xe.r_path [sizeof (xe.r_path) - 1] = '\0';
83 xe.r_count = 1;
84 if (setrmtabent("a")) {
85 /* PRC: do the HA callout: */
86 ha_callout("mount", xe.r_client, xe.r_path, xe.r_count);
87 putrmtabent(&xe, NULL);
88 endrmtabent();
90 xfunlock(lockid);
93 void
94 mountlist_del(char *hname, const char *path)
96 struct rmtabent *rep;
97 FILE *fp;
98 int lockid;
99 int match;
101 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
102 return;
103 if (!setrmtabent("r")) {
104 xfunlock(lockid);
105 return;
107 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
108 endrmtabent();
109 xfunlock(lockid);
110 return;
112 while ((rep = getrmtabent(1, NULL)) != NULL) {
113 match = !strcmp (rep->r_client, hname)
114 && !strcmp(rep->r_path, path);
115 if (match) {
116 rep->r_count--;
117 /* PRC: do the HA callout: */
118 ha_callout("unmount", rep->r_client, rep->r_path, rep->r_count);
120 if (!match || rep->r_count)
121 fputrmtabent(fp, rep, NULL);
123 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
124 xlog(L_ERROR, "couldn't rename %s to %s",
125 _PATH_RMTABTMP, _PATH_RMTAB);
127 endrmtabent(); /* close & unlink */
128 fendrmtabent(fp);
129 xfunlock(lockid);
132 void
133 mountlist_del_all(struct sockaddr_in *sin)
135 struct in_addr addr = sin->sin_addr;
136 struct hostent *hp;
137 struct rmtabent *rep;
138 nfs_export *exp;
139 FILE *fp;
140 int lockid;
142 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
143 return;
144 if (!(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
145 xlog(L_ERROR, "can't get hostname of %s", inet_ntoa(addr));
146 xfunlock(lockid);
147 return;
149 else
150 hp = hostent_dup (hp);
152 if (!setrmtabent("r")) {
153 xfunlock(lockid);
154 free (hp);
155 return;
157 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
158 endrmtabent();
159 xfunlock(lockid);
160 free (hp);
161 return;
163 while ((rep = getrmtabent(1, NULL)) != NULL) {
164 if (strcmp(rep->r_client, hp->h_name) == 0 &&
165 (exp = auth_authenticate("umountall", sin, rep->r_path)))
166 continue;
167 fputrmtabent(fp, rep, NULL);
169 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
170 xlog(L_ERROR, "couldn't rename %s to %s",
171 _PATH_RMTABTMP, _PATH_RMTAB);
173 endrmtabent(); /* close & unlink */
174 fendrmtabent(fp);
175 xfunlock(lockid);
176 free (hp);
179 mountlist
180 mountlist_list(void)
182 static mountlist mlist = NULL;
183 static time_t last_mtime = 0;
184 mountlist m;
185 struct rmtabent *rep;
186 struct stat stb;
187 int lockid;
188 struct in_addr addr;
189 struct hostent *he;
191 if ((lockid = xflock(_PATH_RMTAB, "r")) < 0)
192 return NULL;
193 if (stat(_PATH_RMTAB, &stb) < 0) {
194 xlog(L_ERROR, "can't stat %s", _PATH_RMTAB);
195 return NULL;
197 if (stb.st_mtime != last_mtime) {
198 while (mlist) {
199 mlist = (m = mlist)->ml_next;
200 xfree(m->ml_hostname);
201 xfree(m->ml_directory);
202 xfree(m);
204 last_mtime = stb.st_mtime;
206 setrmtabent("r");
207 while ((rep = getrmtabent(1, NULL)) != NULL) {
208 m = (mountlist) xmalloc(sizeof(*m));
210 if (reverse_resolve &&
211 inet_aton((const char *) rep->r_client, &addr) &&
212 (he = gethostbyaddr(&addr, sizeof(addr), AF_INET)))
213 m->ml_hostname = xstrdup(he->h_name);
214 else
215 m->ml_hostname = xstrdup(rep->r_client);
217 m->ml_directory = xstrdup(rep->r_path);
218 m->ml_next = mlist;
219 mlist = m;
221 endrmtabent();
223 xfunlock(lockid);
225 return mlist;