]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/rmtab.c
2002-10-15 Juan Gomez <juang@us.ibm.com>
[nfs-utils.git] / utils / mountd / rmtab.c
1 /*
2  * utils/mountd/rmtab.c
3  *
4  * Manage the rmtab file for mountd.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include "config.h"
10
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <netdb.h>
17 #include "xmalloc.h"
18 #include "misc.h"
19 #include "exportfs.h"
20 #include "xio.h"
21 #include "mountd.h"
22
23 #include <limits.h> /* PATH_MAX */
24
25 /* If new path is a link do not destroy it but place the
26  * file where the link points.
27  */
28
29 static int 
30 slink_safe_rename(const char * oldpath, const char * newpath)
31 {
32   int r;
33   struct stat s;
34   char slink_path[PATH_MAX];
35   char real_newpath = newpath;
36
37   if((lstat(newpath, &s) == 0) && (S_ISLNK(s.st_mode))) {
38
39     /* New path is a symbolic link, do not destroy but follow */
40
41     if((r = readlink(newpath, slink_path, PATH_MAX))==-1) {
42
43       return -1;
44
45     }
46
47     slink_path[ (r < PATH_MAX) ? (r + 1) : (PATH_MAX - 1)] = '\0';
48
49     real_newpath = slink_path;
50
51   }
52
53   return rename(oldpath, real_newpath);
54
55 }/* static int slink_safe_rename() */
56
57 void
58 mountlist_add(nfs_export *exp, const char *path)
59 {
60         struct rmtabent xe;
61         struct rmtabent *rep;
62         int             lockid;
63         long            pos;
64
65         if ((lockid = xflock(_PATH_RMTAB, "a")) < 0)
66                 return;
67         setrmtabent("r+");
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) {
72                         rep->r_count++;
73                         putrmtabent(rep, &pos);
74                         endrmtabent();
75                         xfunlock(lockid);
76                         return;
77                 }
78         }
79         endrmtabent();
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';
85         xe.r_count = 1;
86         if (setrmtabent("a")) {
87                 putrmtabent(&xe, NULL);
88                 endrmtabent();
89         }
90         xfunlock(lockid);
91 }
92
93 void
94 mountlist_del(nfs_export *exp, const char *path)
95 {
96         struct rmtabent *rep;
97         FILE            *fp;
98         char            *hname = exp->m_client->m_hostname;
99         int             lockid;
100         int             match;
101
102         if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
103                 return;
104         if (!setrmtabent("r")) {
105                 xfunlock(lockid);
106                 return;
107         }
108         if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
109                 endrmtabent();
110                 xfunlock(lockid);
111                 return;
112         }
113         while ((rep = getrmtabent(1, NULL)) != NULL) {
114                 match = !strcmp (rep->r_client, hname)
115                         && !strcmp(rep->r_path, path);
116                 if (match)
117                         rep->r_count--;
118                 if (!match || rep->r_count)
119                         fputrmtabent(fp, rep, NULL);
120         }
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);
124         }
125         endrmtabent();  /* close & unlink */
126         fendrmtabent(fp);
127         xfunlock(lockid);
128 }
129
130 void
131 mountlist_del_all(struct sockaddr_in *sin)
132 {
133         struct in_addr  addr = sin->sin_addr;
134         struct hostent  *hp;
135         struct rmtabent *rep;
136         nfs_export      *exp;
137         FILE            *fp;
138         int             lockid;
139
140         if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
141                 return;
142         if (!(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
143                 xlog(L_ERROR, "can't get hostname of %s", inet_ntoa(addr));
144                 xfunlock(lockid);
145                 return;
146         }
147         else
148                 hp = hostent_dup (hp);
149
150         if (!setrmtabent("r")) {
151                 xfunlock(lockid);
152                 free (hp);
153                 return;
154         }
155         if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
156                 endrmtabent();
157                 xfunlock(lockid);
158                 free (hp);
159                 return;
160         }
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))) {
164                         export_reset(exp);
165                         continue;
166                 }
167                 fputrmtabent(fp, rep, NULL);
168         }
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);
172         }
173         endrmtabent();  /* close & unlink */
174         fendrmtabent(fp);
175         xfunlock(lockid);
176         free (hp);
177 }
178
179 mountlist
180 mountlist_list(void)
181 {
182         static mountlist        mlist = NULL;
183         static time_t           last_mtime = 0;
184         mountlist               m;
185         struct rmtabent         *rep;
186         struct stat             stb;
187         int                     lockid;
188
189         if ((lockid = xflock(_PATH_RMTAB, "r")) < 0)
190                 return NULL;
191         if (stat(_PATH_RMTAB, &stb) < 0) {
192                 xlog(L_ERROR, "can't stat %s", _PATH_RMTAB);
193                 return NULL;
194         }
195         if (stb.st_mtime != last_mtime) {
196                 while (mlist) {
197                         mlist = (m = mlist)->ml_next;
198                         xfree(m->ml_hostname);
199                         xfree(m->ml_directory);
200                         xfree(m);
201                 }
202                 last_mtime = stb.st_mtime;
203
204                 setrmtabent("r");
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);
209                         m->ml_next = mlist;
210                         mlist = m;
211                 }
212                 endrmtabent();
213         }
214         xfunlock(lockid);
215
216         return mlist;
217 }