]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/cache.c
f70f4d6e81a7e1ee7b57061f34b7d1c6d68ba162
[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
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <sys/types.h>
15 #include <sys/select.h>
16 #include <sys/stat.h>
17 #include <sys/vfs.h>
18 #include <time.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <pwd.h>
26 #include <grp.h>
27 #include <mntent.h>
28 #include "misc.h"
29 #include "nfslib.h"
30 #include "exportfs.h"
31 #include "mountd.h"
32 #include "xmalloc.h"
33 #include "fsloc.h"
34 #include "pseudoflavors.h"
35
36 #ifdef USE_BLKID
37 #include "blkid/blkid.h"
38 #endif
39
40 /*
41  * Invoked by RPC service loop
42  */
43 void    cache_set_fds(fd_set *fdset);
44 int     cache_process_req(fd_set *readfds);
45
46 enum nfsd_fsid {
47         FSID_DEV = 0,
48         FSID_NUM,
49         FSID_MAJOR_MINOR,
50         FSID_ENCODE_DEV,
51         FSID_UUID4_INUM,
52         FSID_UUID8,
53         FSID_UUID16,
54         FSID_UUID16_INUM,
55 };
56
57 /*
58  * Support routines for text-based upcalls.
59  * Fields are separated by spaces.
60  * Fields are either mangled to quote space tab newline slosh with slosh
61  * or a hexified with a leading \x
62  * Record is terminated with newline.
63  *
64  */
65 static int cache_export_ent(char *domain, struct exportent *exp, char *p);
66
67
68 char *lbuf  = NULL;
69 int lbuflen = 0;
70 extern int use_ipaddr;
71
72 static void auth_unix_ip(FILE *f)
73 {
74         /* requests are
75          *  class IP-ADDR
76          * Ignore if class != "nfsd"
77          * Otherwise find domainname and write back:
78          *
79          *  "nfsd" IP-ADDR expiry domainname
80          */
81         char *cp;
82         char class[20];
83         char ipaddr[INET6_ADDRSTRLEN];
84         char *client = NULL;
85         struct addrinfo *tmp = NULL;
86         struct addrinfo *ai = NULL;
87         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
88                 return;
89
90         xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
91
92         cp = lbuf;
93
94         if (qword_get(&cp, class, 20) <= 0 ||
95             strcmp(class, "nfsd") != 0)
96                 return;
97
98         if (qword_get(&cp, ipaddr, sizeof(ipaddr)) <= 0)
99                 return;
100
101         tmp = host_pton(ipaddr);
102         if (tmp == NULL)
103                 return;
104
105         auth_reload();
106
107         /* addr is a valid, interesting address, find the domain name... */
108         if (!use_ipaddr) {
109                 ai = client_resolve(tmp->ai_addr);
110                 client = client_compose(ai);
111                 freeaddrinfo(ai);
112         }
113         freeaddrinfo(tmp);
114
115         qword_print(f, "nfsd");
116         qword_print(f, ipaddr);
117         qword_printint(f, time(0)+30*60);
118         if (use_ipaddr)
119                 qword_print(f, ipaddr);
120         else if (client)
121                 qword_print(f, *client?client:"DEFAULT");
122         qword_eol(f);
123         xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
124
125         free(client);
126 }
127
128 static void auth_unix_gid(FILE *f)
129 {
130         /* Request are
131          *  uid
132          * reply is
133          *  uid expiry count list of group ids
134          */
135         uid_t uid;
136         struct passwd *pw;
137         gid_t glist[100], *groups = glist;
138         int ngroups = 100;
139         int rv, i;
140         char *cp;
141
142         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
143                 return;
144
145         cp = lbuf;
146         if (qword_get_uint(&cp, &uid) != 0)
147                 return;
148
149         pw = getpwuid(uid);
150         if (!pw)
151                 rv = -1;
152         else {
153                 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
154                 if (rv == -1 && ngroups >= 100) {
155                         groups = malloc(sizeof(gid_t)*ngroups);
156                         if (!groups)
157                                 rv = -1;
158                         else
159                                 rv = getgrouplist(pw->pw_name, pw->pw_gid,
160                                                   groups, &ngroups);
161                 }
162         }
163         qword_printuint(f, uid);
164         qword_printuint(f, time(0)+30*60);
165         if (rv >= 0) {
166                 qword_printuint(f, ngroups);
167                 for (i=0; i<ngroups; i++)
168                         qword_printuint(f, groups[i]);
169         } else
170                 qword_printuint(f, 0);
171         qword_eol(f);
172
173         if (groups != glist)
174                 free(groups);
175 }
176
177 #if USE_BLKID
178 static const char *get_uuid_blkdev(char *path)
179 {
180         /* We set *safe if we know that we need the
181          * fsid from statfs too.
182          */
183         static blkid_cache cache = NULL;
184         struct stat stb;
185         char *devname;
186         blkid_tag_iterate iter;
187         blkid_dev dev;
188         const char *type;
189         const char *val, *uuid = NULL;
190
191         if (cache == NULL)
192                 blkid_get_cache(&cache, NULL);
193
194         if (stat(path, &stb) != 0)
195                 return NULL;
196         devname = blkid_devno_to_devname(stb.st_dev);
197         if (!devname)
198                 return NULL;
199         dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
200         free(devname);
201         if (!dev)
202                 return NULL;
203         iter = blkid_tag_iterate_begin(dev);
204         if (!iter)
205                 return NULL;
206         while (blkid_tag_next(iter, &type, &val) == 0) {
207                 if (strcmp(type, "UUID") == 0)
208                         uuid = val;
209                 if (strcmp(type, "TYPE") == 0 &&
210                     strcmp(val, "btrfs") == 0) {
211                         uuid = NULL;
212                         break;
213                 }
214         }
215         blkid_tag_iterate_end(iter);
216         return uuid;
217 }
218 #else
219 #define get_uuid_blkdev(path) (NULL)
220 #endif
221
222 static int get_uuid(const char *val, int uuidlen, char *u)
223 {
224         /* extract hex digits from uuidstr and compose a uuid
225          * of the given length (max 16), xoring bytes to make
226          * a smaller uuid.
227          */
228         int i = 0;
229         
230         memset(u, 0, uuidlen);
231         for ( ; *val ; val++) {
232                 char c = *val;
233                 if (!isxdigit(c))
234                         continue;
235                 if (isalpha(c)) {
236                         if (isupper(c))
237                                 c = c - 'A' + 10;
238                         else
239                                 c = c - 'a' + 10;
240                 } else
241                         c = c - '0' + 0;
242                 if ((i&1) == 0)
243                         c <<= 4;
244                 u[i/2] ^= c;
245                 i++;
246                 if (i == uuidlen*2)
247                         i = 0;
248         }
249         return 1;
250 }
251
252 static int uuid_by_path(char *path, int type, int uuidlen, char *uuid)
253 {
254         /* get a uuid for the filesystem found at 'path'.
255          * There are several possible ways of generating the
256          * uuids (types).
257          * Type 0 is used for new filehandles, while other types
258          * may be used to interpret old filehandle - to ensure smooth
259          * forward migration.
260          * We return 1 if a uuid was found (and it might be worth 
261          * trying the next type) or 0 if no more uuid types can be
262          * extracted.
263          */
264
265         /* Possible sources of uuid are
266          * - blkid uuid
267          * - statfs64 uuid
268          *
269          * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
270          * encoding of the device that the filesystem is mounted from, so
271          * it we be very bad to use that (as device numbers change).  blkid
272          * must be preferred.
273          * On other filesystems (e.g. btrfs) the statfs64 uuid contains
274          * important info that the blkid uuid cannot contain:  This happens
275          * when multiple subvolumes are exported (they have the same
276          * blkid uuid but different statfs64 uuids).
277          * We rely on get_uuid_blkdev *knowing* which is which and not returning
278          * a uuid for filesystems where the statfs64 uuid is better.
279          *
280          */
281         struct statfs64 st;
282         char fsid_val[17];
283         const char *blkid_val;
284         const char *val;
285
286         blkid_val = get_uuid_blkdev(path);
287
288         if (statfs64(path, &st) == 0 &&
289             (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
290                 snprintf(fsid_val, 17, "%08x%08x",
291                          st.f_fsid.__val[0], st.f_fsid.__val[1]);
292         else
293                 fsid_val[0] = 0;
294
295         if (blkid_val && (type--) == 0)
296                 val = blkid_val;
297         else if (fsid_val[0] && (type--) == 0)
298                 val = fsid_val;
299         else
300                 return 0;
301
302         get_uuid(val, uuidlen, uuid);
303         return 1;
304 }
305
306 /* Iterate through /etc/mtab, finding mountpoints
307  * at or below a given path
308  */
309 static char *next_mnt(void **v, char *p)
310 {
311         FILE *f;
312         struct mntent *me;
313         int l = strlen(p);
314         if (*v == NULL) {
315                 f = setmntent("/etc/mtab", "r");
316                 *v = f;
317         } else
318                 f = *v;
319         while ((me = getmntent(f)) != NULL &&
320                (strncmp(me->mnt_dir, p, l) != 0 ||
321                 me->mnt_dir[l] != '/'))
322                 ;
323         if (me == NULL) {
324                 endmntent(f);
325                 *v = NULL;
326                 return NULL;
327         }
328         return me->mnt_dir;
329 }
330
331 static void nfsd_fh(FILE *f)
332 {
333         /* request are:
334          *  domain fsidtype fsid
335          * interpret fsid, find export point and options, and write:
336          *  domain fsidtype fsid expiry path
337          */
338         char *cp;
339         char *dom;
340         int fsidtype;
341         int fsidlen;
342         unsigned int dev, major=0, minor=0;
343         unsigned int inode=0;
344         unsigned long long inode64;
345         unsigned int fsidnum=0;
346         char fsid[32];
347         struct exportent *found = NULL;
348         struct addrinfo *ai = NULL;
349         char *found_path = NULL;
350         nfs_export *exp;
351         int i;
352         int dev_missing = 0;
353         int uuidlen = 0;
354         char *fhuuid = NULL;
355
356         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
357                 return;
358
359         xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
360
361         cp = lbuf;
362         
363         dom = malloc(strlen(cp));
364         if (dom == NULL)
365                 return;
366         if (qword_get(&cp, dom, strlen(cp)) <= 0)
367                 goto out;
368         if (qword_get_int(&cp, &fsidtype) != 0)
369                 goto out;
370         if (fsidtype < 0 || fsidtype > 7)
371                 goto out; /* unknown type */
372         if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
373                 goto out;
374         switch(fsidtype) {
375         case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
376                 if (fsidlen != 8)
377                         goto out;
378                 memcpy(&dev, fsid, 4);
379                 memcpy(&inode, fsid+4, 4);
380                 major = ntohl(dev)>>16;
381                 minor = ntohl(dev) & 0xFFFF;
382                 break;
383
384         case FSID_NUM: /* 4 bytes - fsid */
385                 if (fsidlen != 4)
386                         goto out;
387                 memcpy(&fsidnum, fsid, 4);
388                 break;
389
390         case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode 
391                  * This format is never actually used but was
392                  * an historical accident
393                  */
394                 if (fsidlen != 12)
395                         goto out;
396                 memcpy(&dev, fsid, 4); major = ntohl(dev);
397                 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
398                 memcpy(&inode, fsid+8, 4);
399                 break;
400
401         case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
402                 /* This is *host* endian, not net-byte-order, because
403                  * no-one outside this host has any business interpreting it
404                  */
405                 if (fsidlen != 8)
406                         goto out;
407                 memcpy(&dev, fsid, 4);
408                 memcpy(&inode, fsid+4, 4);
409                 major = (dev & 0xfff00) >> 8;
410                 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
411                 break;
412
413         case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
414                 if (fsidlen != 8)
415                         goto out;
416                 memcpy(&inode, fsid, 4);
417                 uuidlen = 4;
418                 fhuuid = fsid+4;
419                 break;
420         case FSID_UUID8: /* 8 byte uuid */
421                 if (fsidlen != 8)
422                         goto out;
423                 uuidlen = 8;
424                 fhuuid = fsid;
425                 break;
426         case FSID_UUID16: /* 16 byte uuid */
427                 if (fsidlen != 16)
428                         goto out;
429                 uuidlen = 16;
430                 fhuuid = fsid;
431                 break;
432         case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
433                 if (fsidlen != 24)
434                         goto out;
435                 memcpy(&inode64, fsid, 8);
436                 inode = inode64;
437                 uuidlen = 16;
438                 fhuuid = fsid+8;
439                 break;
440         }
441
442         auth_reload();
443
444         /* Now determine export point for this fsid/domain */
445         for (i=0 ; i < MCL_MAXTYPES; i++) {
446                 nfs_export *next_exp;
447                 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
448                         struct stat stb;
449                         char u[16];
450                         char *path;
451                         int type;
452
453                         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
454                                 static nfs_export *prev = NULL;
455                                 static void *mnt = NULL;
456                                 
457                                 if (prev == exp) {
458                                         /* try a submount */
459                                         path = next_mnt(&mnt, exp->m_export.e_path);
460                                         if (!path) {
461                                                 next_exp = exp->m_next;
462                                                 prev = NULL;
463                                                 continue;
464                                         }
465                                         next_exp = exp;
466                                 } else {
467                                         prev = exp;
468                                         mnt = NULL;
469                                         path = exp->m_export.e_path;
470                                         next_exp = exp;
471                                 }
472                         } else {
473                                 path = exp->m_export.e_path;
474                                 next_exp = exp->m_next;
475                         }
476
477                         if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
478                                 continue;
479                         if (exp->m_export.e_mountpoint &&
480                             !is_mountpoint(exp->m_export.e_mountpoint[0]?
481                                            exp->m_export.e_mountpoint:
482                                            exp->m_export.e_path))
483                                 dev_missing ++;
484                         if (stat(path, &stb) != 0)
485                                 continue;
486                         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
487                                 continue;
488                         }
489                         switch(fsidtype){
490                         case FSID_DEV:
491                         case FSID_MAJOR_MINOR:
492                         case FSID_ENCODE_DEV:
493                                 if (stb.st_ino != inode)
494                                         continue;
495                                 if (major != major(stb.st_dev) ||
496                                     minor != minor(stb.st_dev))
497                                         continue;
498                                 break;
499                         case FSID_NUM:
500                                 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
501                                      exp->m_export.e_fsid != fsidnum))
502                                         continue;
503                                 break;
504                         case FSID_UUID4_INUM:
505                         case FSID_UUID16_INUM:
506                                 if (stb.st_ino != inode)
507                                         continue;
508                                 goto check_uuid;
509                         case FSID_UUID8:
510                         case FSID_UUID16:
511                                 if (!is_mountpoint(path))
512                                         continue;
513                         check_uuid:
514                                 if (exp->m_export.e_uuid)
515                                         get_uuid(exp->m_export.e_uuid,
516                                                  uuidlen, u);
517                                 else
518                                         for (type = 0;
519                                              uuid_by_path(path, type, uuidlen, u);
520                                              type++)
521                                                 if (memcmp(u, fhuuid, uuidlen) == 0)
522                                                         break;
523
524                                 if (memcmp(u, fhuuid, uuidlen) != 0)
525                                         continue;
526                                 break;
527                         }
528                         if (use_ipaddr) {
529                                 if (ai == NULL) {
530                                         struct addrinfo *tmp;
531                                         tmp = host_pton(dom);
532                                         if (tmp == NULL)
533                                                 goto out;
534                                         ai = client_resolve(tmp->ai_addr);
535                                         freeaddrinfo(tmp);
536                                 }
537                                 if (!client_check(exp->m_client, ai))
538                                         continue;
539                         }
540                         /* It's a match !! */
541                         if (!found) {
542                                 found = &exp->m_export;
543                                 found_path = strdup(path);
544                                 if (found_path == NULL)
545                                         goto out;
546                         } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
547                         {
548                                 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
549                                      found_path, path, dom);
550                         }
551                 }
552         }
553         if (found && 
554             found->e_mountpoint &&
555             !is_mountpoint(found->e_mountpoint[0]?
556                            found->e_mountpoint:
557                            found->e_path)) {
558                 /* Cannot export this yet 
559                  * should log a warning, but need to rate limit
560                    xlog(L_WARNING, "%s not exported as %d not a mountpoint",
561                    found->e_path, found->e_mountpoint);
562                  */
563                 /* FIXME we need to make sure we re-visit this later */
564                 goto out;
565         }
566         if (!found && dev_missing) {
567                 /* The missing dev could be what we want, so just be
568                  * quite rather than returning stale yet
569                  */
570                 goto out;
571         }
572
573         if (found)
574                 if (cache_export_ent(dom, found, found_path) < 0)
575                         found = 0;
576
577         qword_print(f, dom);
578         qword_printint(f, fsidtype);
579         qword_printhex(f, fsid, fsidlen);
580         /* The fsid -> path lookup can be quite expensive as it
581          * potentially stats and reads lots of devices, and some of those
582          * might have spun-down.  The Answer is not likely to
583          * change underneath us, and an 'exportfs -f' can always
584          * remove this from the kernel, so use a really log
585          * timeout.  Maybe this should be configurable on the command
586          * line.
587          */
588         qword_printint(f, 0x7fffffff);
589         if (found)
590                 qword_print(f, found_path);
591         qword_eol(f);
592  out:
593         if (found_path)
594                 free(found_path);
595         freeaddrinfo(ai);
596         free(dom);
597         xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
598         return;         
599 }
600
601 static void write_fsloc(FILE *f, struct exportent *ep)
602 {
603         struct servers *servers;
604
605         if (ep->e_fslocmethod == FSLOC_NONE)
606                 return;
607
608         servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
609         if (!servers)
610                 return;
611         qword_print(f, "fsloc");
612         qword_printint(f, servers->h_num);
613         if (servers->h_num >= 0) {
614                 int i;
615                 for (i=0; i<servers->h_num; i++) {
616                         qword_print(f, servers->h_mp[i]->h_host);
617                         qword_print(f, servers->h_mp[i]->h_path);
618                 }
619         }
620         qword_printint(f, servers->h_referral);
621         release_replicas(servers);
622 }
623
624 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
625 {
626         struct sec_entry *p;
627
628         for (p = ep->e_secinfo; p->flav; p++)
629                 ; /* Do nothing */
630         if (p == ep->e_secinfo) {
631                 /* There was no sec= option */
632                 return;
633         }
634         qword_print(f, "secinfo");
635         qword_printint(f, p - ep->e_secinfo);
636         for (p = ep->e_secinfo; p->flav; p++) {
637                 qword_printint(f, p->flav->fnum);
638                 qword_printint(f, p->flags & flag_mask);
639         }
640
641 }
642
643 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
644 {
645         qword_print(f, domain);
646         qword_print(f, path);
647         qword_printint(f, time(0)+30*60);
648         if (exp) {
649                 int different_fs = strcmp(path, exp->e_path) != 0;
650                 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
651
652                 qword_printint(f, exp->e_flags & flag_mask);
653                 qword_printint(f, exp->e_anonuid);
654                 qword_printint(f, exp->e_anongid);
655                 qword_printint(f, exp->e_fsid);
656                 write_fsloc(f, exp);
657                 write_secinfo(f, exp, flag_mask);
658                 if (exp->e_uuid == NULL || different_fs) {
659                         char u[16];
660                         if (uuid_by_path(path, 0, 16, u)) {
661                                 qword_print(f, "uuid");
662                                 qword_printhex(f, u, 16);
663                         }
664                 } else {
665                         char u[16];
666                         get_uuid(exp->e_uuid, 16, u);
667                         qword_print(f, "uuid");
668                         qword_printhex(f, u, 16);
669                 }
670         }
671         return qword_eol(f);
672 }
673
674 static int is_subdirectory(char *child, char *parent)
675 {
676         int l = strlen(parent);
677
678         return strcmp(child, parent) == 0
679                 || (strncmp(child, parent, l) == 0 && child[l] == '/');
680 }
681
682 static int path_matches(nfs_export *exp, char *path)
683 {
684         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
685                 return is_subdirectory(path, exp->m_export.e_path);
686         return strcmp(path, exp->m_export.e_path) == 0;
687 }
688
689 static int
690 client_matches(nfs_export *exp, char *dom, struct addrinfo *ai)
691 {
692         if (use_ipaddr)
693                 return client_check(exp->m_client, ai);
694         return client_member(dom, exp->m_client->m_hostname);
695 }
696
697 static int
698 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
699 {
700         return path_matches(exp, path) && client_matches(exp, dom, ai);
701 }
702
703 static nfs_export *
704 lookup_export(char *dom, char *path, struct addrinfo *ai)
705 {
706         nfs_export *exp;
707         nfs_export *found = NULL;
708         int found_type = 0;
709         int i;
710
711         for (i=0 ; i < MCL_MAXTYPES; i++) {
712                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
713                         if (!export_matches(exp, dom, path, ai))
714                                 continue;
715                         if (!found) {
716                                 found = exp;
717                                 found_type = i;
718                                 continue;
719                         }
720
721                         /* Always prefer non-V4ROOT mounts */
722                         if (found->m_export.e_flags & NFSEXP_V4ROOT)
723                                 continue;
724
725                         /* If one is a CROSSMOUNT, then prefer the longest path */
726                         if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
727                              (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
728                             strlen(found->m_export.e_path) !=
729                             strlen(exp->m_export.e_path)) {
730
731                                 if (strlen(exp->m_export.e_path) >
732                                     strlen(found->m_export.e_path)) {
733                                         found = exp;
734                                         found_type = i;
735                                 }
736                                 continue;
737
738                         } else if (found_type == i && found->m_warned == 0) {
739                                 xlog(L_WARNING, "%s exported to both %s and %s, "
740                                      "arbitrarily choosing options from first",
741                                      path, found->m_client->m_hostname, exp->m_client->m_hostname,
742                                      dom);
743                                 found->m_warned = 1;
744                         }
745                 }
746         }
747         return found;
748 }
749
750 static void nfsd_export(FILE *f)
751 {
752         /* requests are:
753          *  domain path
754          * determine export options and return:
755          *  domain path expiry flags anonuid anongid fsid
756          */
757
758         char *cp;
759         char *dom, *path;
760         nfs_export *found = NULL;
761         struct addrinfo *ai = NULL;
762
763         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
764                 return;
765
766         xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
767
768         cp = lbuf;
769         dom = malloc(strlen(cp));
770         path = malloc(strlen(cp));
771
772         if (!dom || !path)
773                 goto out;
774
775         if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
776                 goto out;
777         if (qword_get(&cp, path, strlen(lbuf)) <= 0)
778                 goto out;
779
780         auth_reload();
781
782         if (use_ipaddr) {
783                 struct addrinfo *tmp;
784                 tmp = host_pton(dom);
785                 if (tmp == NULL)
786                         goto out;
787                 ai = client_resolve(tmp->ai_addr);
788                 freeaddrinfo(tmp);
789                         goto out;
790         }
791
792         found = lookup_export(dom, path, ai);
793
794         if (found) {
795                 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
796                         xlog(L_WARNING,
797                              "Cannot export %s, possibly unsupported filesystem"
798                              " or fsid= required", path);
799                         dump_to_cache(f, dom, path, NULL);
800                 }
801         } else {
802                 dump_to_cache(f, dom, path, NULL);
803         }
804  out:
805         xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
806         if (dom) free(dom);
807         if (path) free(path);
808         freeaddrinfo(ai);
809 }
810
811
812 struct {
813         char *cache_name;
814         void (*cache_handle)(FILE *f);
815         FILE *f;
816 } cachelist[] = {
817         { "auth.unix.ip", auth_unix_ip, NULL},
818         { "auth.unix.gid", auth_unix_gid, NULL},
819         { "nfsd.export", nfsd_export, NULL},
820         { "nfsd.fh", nfsd_fh, NULL},
821         { NULL, NULL, NULL }
822 };
823
824 extern int manage_gids;
825
826 /**
827  * cache_open - prepare communications channels with kernel RPC caches
828  *
829  */
830 void cache_open(void) 
831 {
832         int i;
833         for (i=0; cachelist[i].cache_name; i++ ) {
834                 char path[100];
835                 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
836                         continue;
837                 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
838                 cachelist[i].f = fopen(path, "r+");
839         }
840 }
841
842 /**
843  * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
844  * @fdset: pointer to fd_set to prepare
845  */
846 void cache_set_fds(fd_set *fdset)
847 {
848         int i;
849         for (i=0; cachelist[i].cache_name; i++) {
850                 if (cachelist[i].f)
851                         FD_SET(fileno(cachelist[i].f), fdset);
852         }
853 }
854
855 /**
856  * cache_process_req - process any active cache file descriptors during service loop iteration
857  * @fdset: pointer to fd_set to examine for activity
858  */
859 int cache_process_req(fd_set *readfds) 
860 {
861         int i;
862         int cnt = 0;
863         for (i=0; cachelist[i].cache_name; i++) {
864                 if (cachelist[i].f != NULL &&
865                     FD_ISSET(fileno(cachelist[i].f), readfds)) {
866                         cnt++;
867                         cachelist[i].cache_handle(cachelist[i].f);
868                         FD_CLR(fileno(cachelist[i].f), readfds);
869                 }
870         }
871         return cnt;
872 }
873
874
875 /*
876  * Give IP->domain and domain+path->options to kernel
877  * % echo nfsd $IP  $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
878  * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
879  */
880
881 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
882 {
883         int err;
884         FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
885         if (!f)
886                 return -1;
887
888         err = dump_to_cache(f, domain, exp->e_path, exp);
889         if (err) {
890                 xlog(L_WARNING,
891                      "Cannot export %s, possibly unsupported filesystem or"
892                      " fsid= required", exp->e_path);
893         }
894
895         while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
896                 /* really an 'if', but we can break out of
897                  * a 'while' more easily */
898                 /* Look along 'path' for other filesystems
899                  * and export them with the same options
900                  */
901                 struct stat stb;
902                 size_t l = strlen(exp->e_path);
903                 __dev_t dev;
904
905                 if (strlen(path) <= l || path[l] != '/' ||
906                     strncmp(exp->e_path, path, l) != 0)
907                         break;
908                 if (stat(exp->e_path, &stb) != 0)
909                         break;
910                 dev = stb.st_dev;
911                 while(path[l] == '/') {
912                         char c;
913                         /* errors for submount should fail whole filesystem */
914                         int err2;
915
916                         l++;
917                         while (path[l] != '/' && path[l])
918                                 l++;
919                         c = path[l];
920                         path[l] = 0;
921                         err2 = lstat(path, &stb);
922                         path[l] = c;
923                         if (err2 < 0)
924                                 break;
925                         if (stb.st_dev == dev)
926                                 continue;
927                         dev = stb.st_dev;
928                         path[l] = 0;
929                         dump_to_cache(f, domain, path, exp);
930                         path[l] = c;
931                 }
932                 break;
933         }
934
935         fclose(f);
936         return err;
937 }
938
939 /**
940  * cache_export - Inform kernel of a new nfs_export
941  * @exp: target nfs_export
942  * @path: NUL-terminated C string containing export path
943  */
944 int cache_export(nfs_export *exp, char *path)
945 {
946         char buf[INET6_ADDRSTRLEN];
947         int err;
948         FILE *f;
949
950         f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
951         if (!f)
952                 return -1;
953
954
955         qword_print(f, "nfsd");
956         qword_print(f,
957                 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
958         qword_printint(f, time(0)+30*60);
959         qword_print(f, exp->m_client->m_hostname);
960         err = qword_eol(f);
961         
962         fclose(f);
963
964         err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
965                 || err;
966         return err;
967 }
968
969 /**
970  * cache_get_filehandle - given an nfs_export, get its root filehandle
971  * @exp: target nfs_export
972  * @len: length of requested file handle
973  * @p: NUL-terminated C string containing export path
974  *
975  * Returns pointer to NFS file handle of root directory of export
976  *
977  * { 
978  *   echo $domain $path $length 
979  *   read filehandle <&0
980  * } <> /proc/fs/nfsd/filehandle
981  */
982 struct nfs_fh_len *
983 cache_get_filehandle(nfs_export *exp, int len, char *p)
984 {
985         FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
986         char buf[200];
987         char *bp = buf;
988         int failed;
989         static struct nfs_fh_len fh;
990
991         if (!f)
992                 f = fopen("/proc/fs/nfs/filehandle", "r+");
993         if (!f)
994                 return NULL;
995
996         qword_print(f, exp->m_client->m_hostname);
997         qword_print(f, p);
998         qword_printint(f, len); 
999         failed = qword_eol(f);
1000         
1001         if (!failed)
1002                 failed = (fgets(buf, sizeof(buf), f) == NULL);
1003         fclose(f);
1004         if (failed)
1005                 return NULL;
1006         memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1007         fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);
1008         return &fh;
1009 }