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