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(nfs_export *exp, const char *path)
56 if ((lockid = xflock(_PATH_RMTAB, "a")) < 0)
59 while ((rep = getrmtabent(1, &pos)) != NULL) {
60 if (strcmp (rep->r_client,
61 exp->m_client->m_hostname) == 0
62 && strcmp(rep->r_path, path) == 0) {
64 putrmtabent(rep, &pos);
71 strncpy(xe.r_client, exp->m_client->m_hostname,
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(nfs_export *exp, const char *path)
89 char *hname = exp->m_client->m_hostname;
93 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
95 if (!setrmtabent("r")) {
99 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
104 while ((rep = getrmtabent(1, NULL)) != NULL) {
105 match = !strcmp (rep->r_client, hname)
106 && !strcmp(rep->r_path, path);
109 if (!match || rep->r_count)
110 fputrmtabent(fp, rep, NULL);
112 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
113 xlog(L_ERROR, "couldn't rename %s to %s",
114 _PATH_RMTABTMP, _PATH_RMTAB);
116 endrmtabent(); /* close & unlink */
122 mountlist_del_all(struct sockaddr_in *sin)
124 struct in_addr addr = sin->sin_addr;
126 struct rmtabent *rep;
131 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
133 if (!(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
134 xlog(L_ERROR, "can't get hostname of %s", inet_ntoa(addr));
139 hp = hostent_dup (hp);
141 if (!setrmtabent("r")) {
146 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
152 while ((rep = getrmtabent(1, NULL)) != NULL) {
153 if (strcmp(rep->r_client, hp->h_name) == 0 &&
154 (exp = auth_authenticate("umountall", sin, rep->r_path))) {
158 fputrmtabent(fp, rep, NULL);
160 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
161 xlog(L_ERROR, "couldn't rename %s to %s",
162 _PATH_RMTABTMP, _PATH_RMTAB);
164 endrmtabent(); /* close & unlink */
173 static mountlist mlist = NULL;
174 static time_t last_mtime = 0;
176 struct rmtabent *rep;
180 if ((lockid = xflock(_PATH_RMTAB, "r")) < 0)
182 if (stat(_PATH_RMTAB, &stb) < 0) {
183 xlog(L_ERROR, "can't stat %s", _PATH_RMTAB);
186 if (stb.st_mtime != last_mtime) {
188 mlist = (m = mlist)->ml_next;
189 xfree(m->ml_hostname);
190 xfree(m->ml_directory);
193 last_mtime = stb.st_mtime;
196 while ((rep = getrmtabent(1, NULL)) != NULL) {
197 m = (mountlist) xmalloc(sizeof(*m));
198 m->ml_hostname = xstrdup(rep->r_client);
199 m->ml_directory = xstrdup(rep->r_path);