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 char real_newpath = newpath;
37 if((lstat(newpath, &s) == 0) && (S_ISLNK(s.st_mode))) {
39 /* New path is a symbolic link, do not destroy but follow */
41 if((r = readlink(newpath, slink_path, PATH_MAX))==-1) {
47 slink_path[ (r < PATH_MAX) ? (r + 1) : (PATH_MAX - 1)] = '\0';
49 real_newpath = slink_path;
53 return rename(oldpath, real_newpath);
55 }/* static int slink_safe_rename() */
58 mountlist_add(nfs_export *exp, const char *path)
65 if ((lockid = xflock(_PATH_RMTAB, "a")) < 0)
68 while ((rep = getrmtabent(1, &pos)) != NULL) {
69 if (strcmp (rep->r_client,
70 exp->m_client->m_hostname) == 0
71 && strcmp(rep->r_path, path) == 0) {
73 putrmtabent(rep, &pos);
80 strncpy(xe.r_client, exp->m_client->m_hostname,
81 sizeof (xe.r_client) - 1);
82 xe.r_client [sizeof (xe.r_client) - 1] = '\0';
83 strncpy(xe.r_path, path, sizeof (xe.r_path) - 1);
84 xe.r_path [sizeof (xe.r_path) - 1] = '\0';
86 if (setrmtabent("a")) {
87 putrmtabent(&xe, NULL);
94 mountlist_del(nfs_export *exp, const char *path)
98 char *hname = exp->m_client->m_hostname;
102 if ((lockid = xflock(_PATH_RMTAB, "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 if (!match || rep->r_count)
119 fputrmtabent(fp, rep, NULL);
121 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
122 xlog(L_ERROR, "couldn't rename %s to %s",
123 _PATH_RMTABTMP, _PATH_RMTAB);
125 endrmtabent(); /* close & unlink */
131 mountlist_del_all(struct sockaddr_in *sin)
133 struct in_addr addr = sin->sin_addr;
135 struct rmtabent *rep;
140 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
142 if (!(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
143 xlog(L_ERROR, "can't get hostname of %s", inet_ntoa(addr));
148 hp = hostent_dup (hp);
150 if (!setrmtabent("r")) {
155 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
161 while ((rep = getrmtabent(1, NULL)) != NULL) {
162 if (strcmp(rep->r_client, hp->h_name) == 0 &&
163 (exp = auth_authenticate("umountall", sin, rep->r_path))) {
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 */
182 static mountlist mlist = NULL;
183 static time_t last_mtime = 0;
185 struct rmtabent *rep;
189 if ((lockid = xflock(_PATH_RMTAB, "r")) < 0)
191 if (stat(_PATH_RMTAB, &stb) < 0) {
192 xlog(L_ERROR, "can't stat %s", _PATH_RMTAB);
195 if (stb.st_mtime != last_mtime) {
197 mlist = (m = mlist)->ml_next;
198 xfree(m->ml_hostname);
199 xfree(m->ml_directory);
202 last_mtime = stb.st_mtime;
205 while ((rep = getrmtabent(1, NULL)) != NULL) {
206 m = (mountlist) xmalloc(sizeof(*m));
207 m->ml_hostname = xstrdup(rep->r_client);
208 m->ml_directory = xstrdup(rep->r_path);