4 * Manage the rmtab file for mountd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <sys/types.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.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.
35 slink_safe_rename(const char * oldpath, const char * newpath)
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)
47 real_newpath = slink_path;
50 return rename(oldpath, real_newpath);
54 mountlist_add(char *host, const char *path)
61 if ((lockid = xflock(_PATH_RMTAB, "a")) < 0)
64 while ((rep = getrmtabent(1, &pos)) != NULL) {
65 if (strcmp (rep->r_client,
67 && strcmp(rep->r_path, path) == 0) {
69 /* PRC: do the HA callout: */
70 ha_callout("mount", rep->r_client, rep->r_path, rep->r_count);
71 putrmtabent(rep, &pos);
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';
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);
94 mountlist_del(char *hname, const char *path)
101 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
103 if (!setrmtabent("r")) {
107 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
112 while ((rep = getrmtabent(1, NULL)) != NULL) {
113 match = !strcmp (rep->r_client, hname)
114 && !strcmp(rep->r_path, path);
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 */
133 mountlist_del_all(struct sockaddr_in *sin)
135 struct in_addr addr = sin->sin_addr;
137 struct rmtabent *rep;
142 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
144 if (!(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
145 xlog(L_ERROR, "can't get hostname of %s", inet_ntoa(addr));
150 hp = hostent_dup (hp);
152 if (!setrmtabent("r")) {
157 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
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))) {
169 fputrmtabent(fp, rep, NULL);
171 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
172 xlog(L_ERROR, "couldn't rename %s to %s",
173 _PATH_RMTABTMP, _PATH_RMTAB);
175 endrmtabent(); /* close & unlink */
184 static mountlist mlist = NULL;
185 static time_t last_mtime = 0;
187 struct rmtabent *rep;
193 if ((lockid = xflock(_PATH_RMTAB, "r")) < 0)
195 if (stat(_PATH_RMTAB, &stb) < 0) {
196 xlog(L_ERROR, "can't stat %s", _PATH_RMTAB);
199 if (stb.st_mtime != last_mtime) {
201 mlist = (m = mlist)->ml_next;
202 xfree(m->ml_hostname);
203 xfree(m->ml_directory);
206 last_mtime = stb.st_mtime;
209 while ((rep = getrmtabent(1, NULL)) != NULL) {
210 m = (mountlist) xmalloc(sizeof(*m));
212 if (reverse_resolve &&
213 inet_aton((const char *) rep->r_client, &addr) &&
214 (he = gethostbyaddr(&addr, sizeof(addr), AF_INET)))
215 m->ml_hostname = xstrdup(he->h_name);
217 m->ml_hostname = xstrdup(rep->r_client);
219 m->ml_directory = xstrdup(rep->r_path);