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 */
29 extern int reverse_resolve;
31 /* If new path is a link do not destroy it but place the
32 * file where the link points.
36 slink_safe_rename(const char * oldpath, const char * newpath)
40 char slink_path[PATH_MAX];
41 const char *real_newpath = newpath;
43 if ((lstat(newpath, &s) == 0) && S_ISLNK(s.st_mode)) {
44 /* New path is a symbolic link, do not destroy but follow */
45 if ((r = readlink(newpath, slink_path, PATH_MAX - 1)) == -1)
48 real_newpath = slink_path;
51 return rename(oldpath, real_newpath);
55 mountlist_add(char *host, const char *path)
62 if ((lockid = xflock(_PATH_RMTABLCK, "a")) < 0)
65 while ((rep = getrmtabent(1, &pos)) != NULL) {
66 if (strcmp (rep->r_client,
68 && strcmp(rep->r_path, path) == 0) {
70 /* PRC: do the HA callout: */
71 ha_callout("mount", rep->r_client, rep->r_path, rep->r_count);
72 putrmtabent(rep, &pos);
79 strncpy(xe.r_client, host,
80 sizeof (xe.r_client) - 1);
81 xe.r_client [sizeof (xe.r_client) - 1] = '\0';
82 strncpy(xe.r_path, path, sizeof (xe.r_path) - 1);
83 xe.r_path [sizeof (xe.r_path) - 1] = '\0';
85 if (setrmtabent("a")) {
86 /* PRC: do the HA callout: */
87 ha_callout("mount", xe.r_client, xe.r_path, xe.r_count);
88 putrmtabent(&xe, NULL);
95 mountlist_del(char *hname, const char *path)
102 if ((lockid = xflock(_PATH_RMTABLCK, "w")) < 0)
104 if (!setrmtabent("r")) {
108 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
113 while ((rep = getrmtabent(1, NULL)) != NULL) {
114 match = !strcmp (rep->r_client, hname)
115 && !strcmp(rep->r_path, path);
118 /* PRC: do the HA callout: */
119 ha_callout("unmount", rep->r_client, rep->r_path, rep->r_count);
121 if (!match || rep->r_count)
122 fputrmtabent(fp, rep, NULL);
124 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
125 xlog(L_ERROR, "couldn't rename %s to %s",
126 _PATH_RMTABTMP, _PATH_RMTAB);
128 endrmtabent(); /* close & unlink */
134 mountlist_del_all(const struct sockaddr *sap)
137 struct rmtabent *rep;
141 if ((lockid = xflock(_PATH_RMTABLCK, "w")) < 0)
143 hostname = host_canonname(sap);
144 if (hostname == NULL) {
145 char buf[INET6_ADDRSTRLEN];
146 xlog(L_ERROR, "can't get hostname of %s",
147 host_ntop(sap, buf, sizeof(buf)));
151 if (!setrmtabent("r"))
154 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w")))
157 while ((rep = getrmtabent(1, NULL)) != NULL) {
158 if (strcmp(rep->r_client, hostname) == 0 &&
159 auth_authenticate("umountall", sap, rep->r_path) != NULL)
161 fputrmtabent(fp, rep, NULL);
163 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
164 xlog(L_ERROR, "couldn't rename %s to %s",
165 _PATH_RMTABTMP, _PATH_RMTAB);
169 endrmtabent(); /* close & unlink */
177 mountlist_freeall(mountlist list)
179 while (list != NULL) {
182 free(m->ml_hostname);
183 free(m->ml_directory);
191 static mountlist mlist = NULL;
192 static time_t last_mtime = 0;
194 struct rmtabent *rep;
198 if ((lockid = xflock(_PATH_RMTABLCK, "r")) < 0)
200 if (stat(_PATH_RMTAB, &stb) < 0) {
201 xlog(L_ERROR, "can't stat %s: %s",
202 _PATH_RMTAB, strerror(errno));
206 if (stb.st_mtime != last_mtime) {
207 mountlist_freeall(mlist);
209 last_mtime = stb.st_mtime;
212 while ((rep = getrmtabent(1, NULL)) != NULL) {
213 m = calloc(1, sizeof(*m));
215 mountlist_freeall(mlist);
217 xlog(L_ERROR, "%s: memory allocation failed",
222 if (reverse_resolve) {
224 ai = host_pton(rep->r_client);
226 m->ml_hostname = host_canonname(ai->ai_addr);
230 if (m->ml_hostname == NULL)
231 m->ml_hostname = strdup(rep->r_client);
233 m->ml_directory = strdup(rep->r_path);
235 if (m->ml_hostname == NULL || m->ml_directory == NULL) {
236 mountlist_freeall(mlist);
238 xlog(L_ERROR, "%s: memory allocation failed",