4 * Manage the rmtab file for mountd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
11 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
23 #include <limits.h> /* PATH_MAX */
25 /* If new path is a link do not destroy it but place the
26 * file where the link points.
30 slink_safe_rename(const char * oldpath, const char * newpath)
34 char slink_path[PATH_MAX];
35 const char *real_newpath = newpath;
37 if ((lstat(newpath, &s) == 0) && S_ISLNK(s.st_mode)) {
38 /* New path is a symbolic link, do not destroy but follow */
39 if ((r = readlink(newpath, slink_path, PATH_MAX - 1)) == -1)
42 real_newpath = slink_path;
45 return rename(oldpath, real_newpath);
49 mountlist_add(char *host, const char *path)
56 if ((lockid = xflock(_PATH_RMTAB, "a")) < 0)
59 while ((rep = getrmtabent(1, &pos)) != NULL) {
60 if (strcmp (rep->r_client,
62 && strcmp(rep->r_path, path) == 0) {
64 putrmtabent(rep, &pos);
71 strncpy(xe.r_client, host,
72 sizeof (xe.r_client) - 1);
73 xe.r_client [sizeof (xe.r_client) - 1] = '\0';
74 strncpy(xe.r_path, path, sizeof (xe.r_path) - 1);
75 xe.r_path [sizeof (xe.r_path) - 1] = '\0';
77 if (setrmtabent("a")) {
78 putrmtabent(&xe, NULL);
85 mountlist_del(char *hname, const char *path)
92 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
94 if (!setrmtabent("r")) {
98 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
103 while ((rep = getrmtabent(1, NULL)) != NULL) {
104 match = !strcmp (rep->r_client, hname)
105 && !strcmp(rep->r_path, path);
108 if (!match || rep->r_count)
109 fputrmtabent(fp, rep, NULL);
111 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
112 xlog(L_ERROR, "couldn't rename %s to %s",
113 _PATH_RMTABTMP, _PATH_RMTAB);
115 endrmtabent(); /* close & unlink */
121 mountlist_del_all(struct sockaddr_in *sin)
123 struct in_addr addr = sin->sin_addr;
125 struct rmtabent *rep;
130 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
132 if (!(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
133 xlog(L_ERROR, "can't get hostname of %s", inet_ntoa(addr));
138 hp = hostent_dup (hp);
140 if (!setrmtabent("r")) {
145 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
151 while ((rep = getrmtabent(1, NULL)) != NULL) {
152 if (strcmp(rep->r_client, hp->h_name) == 0 &&
153 (exp = auth_authenticate("umountall", sin, rep->r_path))) {
157 fputrmtabent(fp, rep, NULL);
159 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
160 xlog(L_ERROR, "couldn't rename %s to %s",
161 _PATH_RMTABTMP, _PATH_RMTAB);
163 endrmtabent(); /* close & unlink */
172 static mountlist mlist = NULL;
173 static time_t last_mtime = 0;
175 struct rmtabent *rep;
179 if ((lockid = xflock(_PATH_RMTAB, "r")) < 0)
181 if (stat(_PATH_RMTAB, &stb) < 0) {
182 xlog(L_ERROR, "can't stat %s", _PATH_RMTAB);
185 if (stb.st_mtime != last_mtime) {
187 mlist = (m = mlist)->ml_next;
188 xfree(m->ml_hostname);
189 xfree(m->ml_directory);
192 last_mtime = stb.st_mtime;
195 while ((rep = getrmtabent(1, NULL)) != NULL) {
196 m = (mountlist) xmalloc(sizeof(*m));
197 m->ml_hostname = xstrdup(rep->r_client);
198 m->ml_directory = xstrdup(rep->r_path);