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