]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/cache.c
Understand type 2 and type 3 filesystem identifiers.
[nfs-utils.git] / utils / mountd / cache.c
1
2 /*
3  * Handle communication with knfsd internal cache
4  *
5  * We open /proc/net/rpc/{auth.unix.ip,nfsd.export,nfsd.fh}/channel
6  * and listen for requests (using my_svc_run)
7  * 
8  */
9 #include "config.h"
10
11 #include <sys/types.h>
12 #include <sys/select.h>
13 #include <sys/stat.h>
14 #include <time.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include "misc.h"
22 #include "nfslib.h"
23 #include "exportfs.h"
24 #include "mountd.h"
25 #include "xmalloc.h"
26
27 /*
28  * Support routines for text-based upcalls.
29  * Fields are separated by spaces.
30  * Fields are either mangled to quote space tab newline slosh with slosh
31  * or a hexified with a leading \x
32  * Record is terminated with newline.
33  *
34  */
35 void cache_export_ent(char *domain, struct exportent *exp);
36
37
38 char *lbuf  = NULL;
39 int lbuflen = 0;
40
41 void auth_unix_ip(FILE *f)
42 {
43         /* requests are
44          *  class IP-ADDR
45          * Ignore if class != "nfsd"
46          * Otherwise find domainname and write back:
47          *
48          *  "nfsd" IP-ADDR expiry domainname
49          */
50         char *cp;
51         char class[20];
52         char ipaddr[20];
53         char *client;
54         struct in_addr addr;
55         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
56                 return;
57
58         cp = lbuf;
59
60         if (qword_get(&cp, class, 20) <= 0 ||
61             strcmp(class, "nfsd") != 0)
62                 return;
63
64         if (qword_get(&cp, ipaddr, 20) <= 0)
65                 return;
66
67         if (inet_aton(ipaddr, &addr)==0)
68                 return;
69
70         auth_reload();
71
72         /* addr is a valid, interesting address, find the domain name... */
73         client = client_compose(addr);
74
75         
76         qword_print(f, "nfsd");
77         qword_print(f, ipaddr);
78         qword_printint(f, time(0)+30*60);
79         if (client)
80                 qword_print(f, *client?client:"DEFAULT");
81         qword_eol(f);
82
83         if (client && strcmp(ipaddr, client))
84                 mountlist_add(ipaddr, *client?client:"DEFAULT");
85
86         if (client) free(client);
87         
88 }
89
90 void nfsd_fh(FILE *f)
91 {
92         /* request are:
93          *  domain fsidtype fsid
94          * interpret fsid, find export point and options, and write:
95          *  domain fsidtype fsid expiry path
96          */
97         char *cp;
98         char *dom;
99         int fsidtype;
100         int fsidlen;
101         unsigned int dev, major=0, minor=0;
102         unsigned int inode=0;
103         unsigned int fsidnum=0;
104         char fsid[32];
105         struct exportent *found = NULL;
106         nfs_export *exp;
107         int i;
108         int dev_missing = 0;
109
110         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
111                 return;
112
113         cp = lbuf;
114
115         dom = malloc(strlen(cp));
116         if (dom == NULL)
117                 return;
118         if (qword_get(&cp, dom, strlen(cp)) <= 0)
119                 goto out;
120         if (qword_get_int(&cp, &fsidtype) != 0)
121                 goto out;
122         if (fsidtype < 0 || fsidtype > 3)
123                 goto out; /* unknown type */
124         if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
125                 goto out;
126         switch(fsidtype) {
127         case 0: /* 4 bytes: 2 major, 2 minor, 4 inode */
128                 if (fsidlen != 8)
129                         goto out;
130                 memcpy(&dev, fsid, 4);
131                 memcpy(&inode, fsid+4, 4);
132                 major = ntohl(dev)>>16;
133                 minor = ntohl(dev) & 0xFFFF;
134                 break;
135
136         case 1: /* 4 bytes - fsid */
137                 if (fsidlen != 4)
138                         goto out;
139                 memcpy(&fsidnum, fsid, 4);
140                 break;
141
142         case 2: /* 12 bytes: 4 major, 4 minor, 4 inode 
143                  * This format is never actually used but was
144                  * an historical accident
145                  */
146                 if (fsidlen != 12)
147                         goto out;
148                 memcpy(&dev, fsid, 4); major = ntohl(dev);
149                 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
150                 memcpy(&inode, fsid+8, 4);
151                 break;
152
153         case 3: /* 8 bytes: 4 byte packed device number, 4 inode */
154                 /* This is *host* endian, not net-byte-order, because
155                  * no-one outside this host has any business interpreting it
156                  */
157                 if (fsidlen != 8)
158                         goto out;
159                 memcpy(&dev, fsid, 4);
160                 memcpy(&inode, fsid+4, 4);
161                 major = (dev & 0xfff00) >> 8;
162                 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
163                 break;
164
165         }
166
167         auth_reload();
168
169         /* Now determine export point for this fsid/domain */
170         for (i=0 ; i < MCL_MAXTYPES; i++) {
171                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
172                         struct stat stb;
173
174                         if (!client_member(dom, exp->m_client->m_hostname))
175                                 continue;
176                         if (exp->m_export.e_mountpoint &&
177                             !is_mountpoint(exp->m_export.e_mountpoint[0]?
178                                            exp->m_export.e_mountpoint:
179                                            exp->m_export.e_path))
180                                 dev_missing ++;
181                         if (stat(exp->m_export.e_path, &stb) != 0)
182                                 continue;
183                         if (fsidtype == 1 &&
184                             ((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
185                              exp->m_export.e_fsid != fsidnum))
186                                 continue;
187                         if (fsidtype != 1) {
188                                 if (stb.st_ino != inode)
189                                         continue;
190                                 if (major != major(stb.st_dev) ||
191                                     minor != minor(stb.st_dev))
192                                         continue;
193                         }
194                         /* It's a match !! */
195                         if (!found)
196                                 found = &exp->m_export;
197                         else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
198                         {
199                                 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
200                                      found->e_path, exp->m_export.e_path, dom);
201                         }
202                 }
203         }
204         if (found && 
205             found->e_mountpoint &&
206             !is_mountpoint(found->e_mountpoint[0]?
207                            found->e_mountpoint:
208                            found->e_path)) {
209                 /* Cannot export this yet 
210                  * should log a warning, but need to rate limit
211                    xlog(L_WARNING, "%s not exported as %d not a mountpoint",
212                    found->e_path, found->e_mountpoint);
213                  */
214                 /* FIXME we need to make sure we re-visit this later */
215                 goto out;
216         }
217         if (!found && dev_missing) {
218                 /* The missing dev could be what we want, so just be
219                  * quite rather than returning stale yet
220                  */
221                 goto out;
222         }
223
224         if (found)
225                 cache_export_ent(dom, found);
226
227         qword_print(f, dom);
228         qword_printint(f, fsidtype);
229         qword_printhex(f, fsid, fsidlen);
230         qword_printint(f, time(0)+30*60);
231         if (found)
232                 qword_print(f, found->e_path);
233         qword_eol(f);
234  out:
235         free(dom);
236         return;         
237 }
238
239 void nfsd_export(FILE *f)
240 {
241         /* requests are:
242          *  domain path
243          * determine export options and return:
244          *  domain path expiry flags anonuid anongid fsid
245          */
246
247         char *cp;
248         int i;
249         char *dom, *path;
250         nfs_export *exp, *found = NULL;
251
252
253         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
254                 return;
255
256         cp = lbuf;
257         dom = malloc(strlen(cp));
258         path = malloc(strlen(cp));
259
260         if (!dom || !path)
261                 goto out;
262
263         if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
264                 goto out;
265         if (qword_get(&cp, path, strlen(lbuf)) <= 0)
266                 goto out;
267
268         auth_reload();
269
270         /* now find flags for this export point in this domain */
271         for (i=0 ; i < MCL_MAXTYPES; i++) {
272                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
273                         if (!client_member(dom, exp->m_client->m_hostname))
274                                 continue;
275                         if (strcmp(path, exp->m_export.e_path))
276                                 continue;
277                         if (!found)
278                                 found = exp;
279                         else {
280                                 xlog(L_WARNING, "%s exported to both %s and %s in %s",
281                                      path, exp->m_client->m_hostname, found->m_client->m_hostname,
282                                      dom);
283                         }
284                 }
285         }
286
287         qword_print(f, dom);
288         qword_print(f, path);
289         qword_printint(f, time(0)+30*60);
290         if (found) {
291                 qword_printint(f, found->m_export.e_flags);
292                 qword_printint(f, found->m_export.e_anonuid);
293                 qword_printint(f, found->m_export.e_anongid);
294                 qword_printint(f, found->m_export.e_fsid);
295                 mountlist_add(dom, path);
296         }
297         qword_eol(f);
298  out:
299         if (dom) free(dom);
300         if (path) free(path);
301 }
302
303
304 struct {
305         char *cache_name;
306         void (*cache_handle)(FILE *f);
307         FILE *f;
308 } cachelist[] = {
309         { "auth.unix.ip", auth_unix_ip},
310         { "nfsd.export", nfsd_export},
311         { "nfsd.fh", nfsd_fh},
312         { NULL, NULL }
313 };
314
315 void cache_open(void) 
316 {
317         int i;
318         for (i=0; cachelist[i].cache_name; i++ ){
319                 char path[100];
320                 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
321                 cachelist[i].f = fopen(path, "r+");
322         }
323 }
324
325 void cache_set_fds(fd_set *fdset)
326 {
327         int i;
328         for (i=0; cachelist[i].cache_name; i++) {
329                 if (cachelist[i].f)
330                         FD_SET(fileno(cachelist[i].f), fdset);
331         }
332 }
333
334 int cache_process_req(fd_set *readfds) 
335 {
336         int i;
337         int cnt = 0;
338         for (i=0; cachelist[i].cache_name; i++) {
339                 if (cachelist[i].f != NULL &&
340                     FD_ISSET(fileno(cachelist[i].f), readfds)) {
341                         cnt++;
342                         cachelist[i].cache_handle(cachelist[i].f);
343                         FD_CLR(fileno(cachelist[i].f), readfds);
344                 }
345         }
346         return cnt;
347 }
348
349
350 /*
351  * Give IP->domain and domain+path->options to kernel
352  * % echo nfsd $IP  $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
353  * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
354  */
355
356 void cache_export_ent(char *domain, struct exportent *exp)
357 {
358
359         FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
360         if (!f)
361                 return;
362
363         qword_print(f, domain);
364         qword_print(f, exp->e_path);
365         qword_printint(f, time(0)+30*60);
366         qword_printint(f, exp->e_flags);
367         qword_printint(f, exp->e_anonuid);
368         qword_printint(f, exp->e_anongid);
369         qword_printint(f, exp->e_fsid);
370         qword_eol(f);
371
372         fclose(f);
373
374         mountlist_add(domain, exp->e_path);
375 }
376
377 void cache_export(nfs_export *exp)
378 {
379         FILE *f;
380
381         f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
382         if (!f)
383                 return;
384
385         qword_print(f, "nfsd");
386         qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
387         qword_printint(f, time(0)+30*60);
388         qword_print(f, exp->m_client->m_hostname);
389         qword_eol(f);
390         
391         fclose(f);
392
393         if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
394                 mountlist_add(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
395
396         cache_export_ent(exp->m_client->m_hostname, &exp->m_export);
397 }
398
399 /* Get a filehandle.
400  * { 
401  *   echo $domain $path $length 
402  *   read filehandle <&0
403  * } <> /proc/fs/nfsd/filehandle
404  */
405 struct nfs_fh_len *
406 cache_get_filehandle(nfs_export *exp, int len, char *p)
407 {
408         FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
409         char buf[200];
410         char *bp = buf;
411         int failed;
412         static struct nfs_fh_len fh;
413
414         if (!f)
415                 f = fopen("/proc/fs/nfs/filehandle", "r+");
416         if (!f)
417                 return NULL;
418
419         qword_print(f, exp->m_client->m_hostname);
420         qword_print(f, p);
421         qword_printint(f, len); 
422         qword_eol(f);
423         
424         failed = (fgets(buf, sizeof(buf), f) == NULL);
425         fclose(f);
426         if (failed)
427                 return NULL;
428         memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
429         fh.fh_size = qword_get(&bp, fh.fh_handle, NFS3_FHSIZE);
430         return &fh;
431 }
432