]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/cache.c
nfsstat: reorder nfs4 stats for 2.6.39
[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 static void nfsd_fh(FILE *f)
345 {
346         /* request are:
347          *  domain fsidtype fsid
348          * interpret fsid, find export point and options, and write:
349          *  domain fsidtype fsid expiry path
350          */
351         char *cp;
352         char *dom;
353         int fsidtype;
354         int fsidlen;
355         unsigned int dev, major=0, minor=0;
356         unsigned int inode=0;
357         unsigned long long inode64;
358         unsigned int fsidnum=0;
359         char fsid[32];
360         struct exportent *found = NULL;
361         struct addrinfo *ai = NULL;
362         char *found_path = NULL;
363         nfs_export *exp;
364         int i;
365         int dev_missing = 0;
366         int uuidlen = 0;
367         char *fhuuid = NULL;
368
369         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
370                 return;
371
372         xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
373
374         cp = lbuf;
375         
376         dom = malloc(strlen(cp));
377         if (dom == NULL)
378                 return;
379         if (qword_get(&cp, dom, strlen(cp)) <= 0)
380                 goto out;
381         if (qword_get_int(&cp, &fsidtype) != 0)
382                 goto out;
383         if (fsidtype < 0 || fsidtype > 7)
384                 goto out; /* unknown type */
385         if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
386                 goto out;
387         switch(fsidtype) {
388         case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
389                 if (fsidlen != 8)
390                         goto out;
391                 memcpy(&dev, fsid, 4);
392                 memcpy(&inode, fsid+4, 4);
393                 major = ntohl(dev)>>16;
394                 minor = ntohl(dev) & 0xFFFF;
395                 break;
396
397         case FSID_NUM: /* 4 bytes - fsid */
398                 if (fsidlen != 4)
399                         goto out;
400                 memcpy(&fsidnum, fsid, 4);
401                 break;
402
403         case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode 
404                  * This format is never actually used but was
405                  * an historical accident
406                  */
407                 if (fsidlen != 12)
408                         goto out;
409                 memcpy(&dev, fsid, 4); major = ntohl(dev);
410                 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
411                 memcpy(&inode, fsid+8, 4);
412                 break;
413
414         case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
415                 /* This is *host* endian, not net-byte-order, because
416                  * no-one outside this host has any business interpreting it
417                  */
418                 if (fsidlen != 8)
419                         goto out;
420                 memcpy(&dev, fsid, 4);
421                 memcpy(&inode, fsid+4, 4);
422                 major = (dev & 0xfff00) >> 8;
423                 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
424                 break;
425
426         case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
427                 if (fsidlen != 8)
428                         goto out;
429                 memcpy(&inode, fsid, 4);
430                 uuidlen = 4;
431                 fhuuid = fsid+4;
432                 break;
433         case FSID_UUID8: /* 8 byte uuid */
434                 if (fsidlen != 8)
435                         goto out;
436                 uuidlen = 8;
437                 fhuuid = fsid;
438                 break;
439         case FSID_UUID16: /* 16 byte uuid */
440                 if (fsidlen != 16)
441                         goto out;
442                 uuidlen = 16;
443                 fhuuid = fsid;
444                 break;
445         case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
446                 if (fsidlen != 24)
447                         goto out;
448                 memcpy(&inode64, fsid, 8);
449                 inode = inode64;
450                 uuidlen = 16;
451                 fhuuid = fsid+8;
452                 break;
453         }
454
455         auth_reload();
456
457         /* Now determine export point for this fsid/domain */
458         for (i=0 ; i < MCL_MAXTYPES; i++) {
459                 nfs_export *next_exp;
460                 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
461                         struct stat stb;
462                         char u[16];
463                         char *path;
464                         int type;
465
466                         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
467                                 static nfs_export *prev = NULL;
468                                 static void *mnt = NULL;
469                                 
470                                 if (prev == exp) {
471                                         /* try a submount */
472                                         path = next_mnt(&mnt, exp->m_export.e_path);
473                                         if (!path) {
474                                                 next_exp = exp->m_next;
475                                                 prev = NULL;
476                                                 continue;
477                                         }
478                                         next_exp = exp;
479                                 } else {
480                                         prev = exp;
481                                         mnt = NULL;
482                                         path = exp->m_export.e_path;
483                                         next_exp = exp;
484                                 }
485                         } else {
486                                 path = exp->m_export.e_path;
487                                 next_exp = exp->m_next;
488                         }
489
490                         if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
491                                 continue;
492                         if (exp->m_export.e_mountpoint &&
493                             !is_mountpoint(exp->m_export.e_mountpoint[0]?
494                                            exp->m_export.e_mountpoint:
495                                            exp->m_export.e_path))
496                                 dev_missing ++;
497                         if (stat(path, &stb) != 0)
498                                 continue;
499                         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
500                                 continue;
501                         }
502                         switch(fsidtype){
503                         case FSID_DEV:
504                         case FSID_MAJOR_MINOR:
505                         case FSID_ENCODE_DEV:
506                                 if (stb.st_ino != inode)
507                                         continue;
508                                 if (major != major(stb.st_dev) ||
509                                     minor != minor(stb.st_dev))
510                                         continue;
511                                 break;
512                         case FSID_NUM:
513                                 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
514                                      exp->m_export.e_fsid != fsidnum))
515                                         continue;
516                                 break;
517                         case FSID_UUID4_INUM:
518                         case FSID_UUID16_INUM:
519                                 if (stb.st_ino != inode)
520                                         continue;
521                                 goto check_uuid;
522                         case FSID_UUID8:
523                         case FSID_UUID16:
524                                 if (!is_mountpoint(path))
525                                         continue;
526                         check_uuid:
527                                 if (exp->m_export.e_uuid)
528                                         get_uuid(exp->m_export.e_uuid,
529                                                  uuidlen, u);
530                                 else
531                                         for (type = 0;
532                                              uuid_by_path(path, type, uuidlen, u);
533                                              type++)
534                                                 if (memcmp(u, fhuuid, uuidlen) == 0)
535                                                         break;
536
537                                 if (memcmp(u, fhuuid, uuidlen) != 0)
538                                         continue;
539                                 break;
540                         }
541                         if (use_ipaddr) {
542                                 if (ai == NULL) {
543                                         struct addrinfo *tmp;
544                                         tmp = host_pton(dom);
545                                         if (tmp == NULL)
546                                                 goto out;
547                                         ai = client_resolve(tmp->ai_addr);
548                                         freeaddrinfo(tmp);
549                                 }
550                                 if (!client_check(exp->m_client, ai))
551                                         continue;
552                         }
553                         /* It's a match !! */
554                         if (!found) {
555                                 found = &exp->m_export;
556                                 found_path = strdup(path);
557                                 if (found_path == NULL)
558                                         goto out;
559                         } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
560                         {
561                                 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
562                                      found_path, path, dom);
563                         }
564                 }
565         }
566         if (found && 
567             found->e_mountpoint &&
568             !is_mountpoint(found->e_mountpoint[0]?
569                            found->e_mountpoint:
570                            found->e_path)) {
571                 /* Cannot export this yet 
572                  * should log a warning, but need to rate limit
573                    xlog(L_WARNING, "%s not exported as %d not a mountpoint",
574                    found->e_path, found->e_mountpoint);
575                  */
576                 /* FIXME we need to make sure we re-visit this later */
577                 goto out;
578         }
579         if (!found && dev_missing) {
580                 /* The missing dev could be what we want, so just be
581                  * quite rather than returning stale yet
582                  */
583                 goto out;
584         }
585
586         if (found)
587                 if (cache_export_ent(dom, found, found_path) < 0)
588                         found = 0;
589
590         qword_print(f, dom);
591         qword_printint(f, fsidtype);
592         qword_printhex(f, fsid, fsidlen);
593         /* The fsid -> path lookup can be quite expensive as it
594          * potentially stats and reads lots of devices, and some of those
595          * might have spun-down.  The Answer is not likely to
596          * change underneath us, and an 'exportfs -f' can always
597          * remove this from the kernel, so use a really log
598          * timeout.  Maybe this should be configurable on the command
599          * line.
600          */
601         qword_printint(f, 0x7fffffff);
602         if (found)
603                 qword_print(f, found_path);
604         qword_eol(f);
605  out:
606         if (found_path)
607                 free(found_path);
608         freeaddrinfo(ai);
609         free(dom);
610         xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
611         return;         
612 }
613
614 static void write_fsloc(FILE *f, struct exportent *ep)
615 {
616         struct servers *servers;
617
618         if (ep->e_fslocmethod == FSLOC_NONE)
619                 return;
620
621         servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
622         if (!servers)
623                 return;
624         qword_print(f, "fsloc");
625         qword_printint(f, servers->h_num);
626         if (servers->h_num >= 0) {
627                 int i;
628                 for (i=0; i<servers->h_num; i++) {
629                         qword_print(f, servers->h_mp[i]->h_host);
630                         qword_print(f, servers->h_mp[i]->h_path);
631                 }
632         }
633         qword_printint(f, servers->h_referral);
634         release_replicas(servers);
635 }
636
637 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
638 {
639         struct sec_entry *p;
640
641         for (p = ep->e_secinfo; p->flav; p++)
642                 ; /* Do nothing */
643         if (p == ep->e_secinfo) {
644                 /* There was no sec= option */
645                 return;
646         }
647         qword_print(f, "secinfo");
648         qword_printint(f, p - ep->e_secinfo);
649         for (p = ep->e_secinfo; p->flav; p++) {
650                 qword_printint(f, p->flav->fnum);
651                 qword_printint(f, p->flags & flag_mask);
652         }
653
654 }
655
656 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
657 {
658         qword_print(f, domain);
659         qword_print(f, path);
660         if (exp) {
661                 int different_fs = strcmp(path, exp->e_path) != 0;
662                 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
663
664                 qword_printuint(f, time(0) + exp->e_ttl);
665                 qword_printint(f, exp->e_flags & flag_mask);
666                 qword_printint(f, exp->e_anonuid);
667                 qword_printint(f, exp->e_anongid);
668                 qword_printint(f, exp->e_fsid);
669                 write_fsloc(f, exp);
670                 write_secinfo(f, exp, flag_mask);
671                 if (exp->e_uuid == NULL || different_fs) {
672                         char u[16];
673                         if (uuid_by_path(path, 0, 16, u)) {
674                                 qword_print(f, "uuid");
675                                 qword_printhex(f, u, 16);
676                         }
677                 } else {
678                         char u[16];
679                         get_uuid(exp->e_uuid, 16, u);
680                         qword_print(f, "uuid");
681                         qword_printhex(f, u, 16);
682                 }
683         } else
684                 qword_printuint(f, time(0) + DEFAULT_TTL);
685         return qword_eol(f);
686 }
687
688 static int is_subdirectory(char *child, char *parent)
689 {
690         int l = strlen(parent);
691
692         return strcmp(child, parent) == 0
693                 || (strncmp(child, parent, l) == 0 && child[l] == '/');
694 }
695
696 static int path_matches(nfs_export *exp, char *path)
697 {
698         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
699                 return is_subdirectory(path, exp->m_export.e_path);
700         return strcmp(path, exp->m_export.e_path) == 0;
701 }
702
703 static int
704 client_matches(nfs_export *exp, char *dom, struct addrinfo *ai)
705 {
706         if (use_ipaddr)
707                 return client_check(exp->m_client, ai);
708         return client_member(dom, exp->m_client->m_hostname);
709 }
710
711 static int
712 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
713 {
714         return path_matches(exp, path) && client_matches(exp, dom, ai);
715 }
716
717 static nfs_export *
718 lookup_export(char *dom, char *path, struct addrinfo *ai)
719 {
720         nfs_export *exp;
721         nfs_export *found = NULL;
722         int found_type = 0;
723         int i;
724
725         for (i=0 ; i < MCL_MAXTYPES; i++) {
726                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
727                         if (!export_matches(exp, dom, path, ai))
728                                 continue;
729                         if (!found) {
730                                 found = exp;
731                                 found_type = i;
732                                 continue;
733                         }
734
735                         /* Always prefer non-V4ROOT mounts */
736                         if (found->m_export.e_flags & NFSEXP_V4ROOT)
737                                 continue;
738
739                         /* If one is a CROSSMOUNT, then prefer the longest path */
740                         if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
741                              (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
742                             strlen(found->m_export.e_path) !=
743                             strlen(exp->m_export.e_path)) {
744
745                                 if (strlen(exp->m_export.e_path) >
746                                     strlen(found->m_export.e_path)) {
747                                         found = exp;
748                                         found_type = i;
749                                 }
750                                 continue;
751
752                         } else if (found_type == i && found->m_warned == 0) {
753                                 xlog(L_WARNING, "%s exported to both %s and %s, "
754                                      "arbitrarily choosing options from first",
755                                      path, found->m_client->m_hostname, exp->m_client->m_hostname,
756                                      dom);
757                                 found->m_warned = 1;
758                         }
759                 }
760         }
761         return found;
762 }
763
764 static void nfsd_export(FILE *f)
765 {
766         /* requests are:
767          *  domain path
768          * determine export options and return:
769          *  domain path expiry flags anonuid anongid fsid
770          */
771
772         char *cp;
773         char *dom, *path;
774         nfs_export *found = NULL;
775         struct addrinfo *ai = NULL;
776
777         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
778                 return;
779
780         xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
781
782         cp = lbuf;
783         dom = malloc(strlen(cp));
784         path = malloc(strlen(cp));
785
786         if (!dom || !path)
787                 goto out;
788
789         if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
790                 goto out;
791         if (qword_get(&cp, path, strlen(lbuf)) <= 0)
792                 goto out;
793
794         auth_reload();
795
796         if (use_ipaddr) {
797                 struct addrinfo *tmp;
798                 tmp = host_pton(dom);
799                 if (tmp == NULL)
800                         goto out;
801                 ai = client_resolve(tmp->ai_addr);
802                 freeaddrinfo(tmp);
803                         goto out;
804         }
805
806         found = lookup_export(dom, path, ai);
807
808         if (found) {
809                 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
810                         xlog(L_WARNING,
811                              "Cannot export %s, possibly unsupported filesystem"
812                              " or fsid= required", path);
813                         dump_to_cache(f, dom, path, NULL);
814                 }
815         } else {
816                 dump_to_cache(f, dom, path, NULL);
817         }
818  out:
819         xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
820         if (dom) free(dom);
821         if (path) free(path);
822         freeaddrinfo(ai);
823 }
824
825
826 struct {
827         char *cache_name;
828         void (*cache_handle)(FILE *f);
829         FILE *f;
830         char vbuf[RPC_CHAN_BUF_SIZE];
831 } cachelist[] = {
832         { "auth.unix.ip", auth_unix_ip, NULL, ""},
833         { "auth.unix.gid", auth_unix_gid, NULL, ""},
834         { "nfsd.export", nfsd_export, NULL, ""},
835         { "nfsd.fh", nfsd_fh, NULL, ""},
836         { NULL, NULL, NULL, ""}
837 };
838
839 extern int manage_gids;
840
841 /**
842  * cache_open - prepare communications channels with kernel RPC caches
843  *
844  */
845 void cache_open(void) 
846 {
847         int i;
848         for (i=0; cachelist[i].cache_name; i++ ) {
849                 char path[100];
850                 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
851                         continue;
852                 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
853                 cachelist[i].f = fopen(path, "r+");
854                 if (cachelist[i].f != NULL) {
855                         setvbuf(cachelist[i].f, cachelist[i].vbuf, _IOLBF, 
856                                 RPC_CHAN_BUF_SIZE);
857                 }
858         }
859 }
860
861 /**
862  * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
863  * @fdset: pointer to fd_set to prepare
864  */
865 void cache_set_fds(fd_set *fdset)
866 {
867         int i;
868         for (i=0; cachelist[i].cache_name; i++) {
869                 if (cachelist[i].f)
870                         FD_SET(fileno(cachelist[i].f), fdset);
871         }
872 }
873
874 /**
875  * cache_process_req - process any active cache file descriptors during service loop iteration
876  * @fdset: pointer to fd_set to examine for activity
877  */
878 int cache_process_req(fd_set *readfds) 
879 {
880         int i;
881         int cnt = 0;
882         for (i=0; cachelist[i].cache_name; i++) {
883                 if (cachelist[i].f != NULL &&
884                     FD_ISSET(fileno(cachelist[i].f), readfds)) {
885                         cnt++;
886                         cachelist[i].cache_handle(cachelist[i].f);
887                         FD_CLR(fileno(cachelist[i].f), readfds);
888                 }
889         }
890         return cnt;
891 }
892
893
894 /*
895  * Give IP->domain and domain+path->options to kernel
896  * % echo nfsd $IP  $[now+DEFAULT_TTL] $domain > /proc/net/rpc/auth.unix.ip/channel
897  * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
898  */
899
900 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
901 {
902         int err;
903         FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
904         if (!f)
905                 return -1;
906
907         err = dump_to_cache(f, domain, exp->e_path, exp);
908         if (err) {
909                 xlog(L_WARNING,
910                      "Cannot export %s, possibly unsupported filesystem or"
911                      " fsid= required", exp->e_path);
912         }
913
914         while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
915                 /* really an 'if', but we can break out of
916                  * a 'while' more easily */
917                 /* Look along 'path' for other filesystems
918                  * and export them with the same options
919                  */
920                 struct stat stb;
921                 size_t l = strlen(exp->e_path);
922                 __dev_t dev;
923
924                 if (strlen(path) <= l || path[l] != '/' ||
925                     strncmp(exp->e_path, path, l) != 0)
926                         break;
927                 if (stat(exp->e_path, &stb) != 0)
928                         break;
929                 dev = stb.st_dev;
930                 while(path[l] == '/') {
931                         char c;
932                         /* errors for submount should fail whole filesystem */
933                         int err2;
934
935                         l++;
936                         while (path[l] != '/' && path[l])
937                                 l++;
938                         c = path[l];
939                         path[l] = 0;
940                         err2 = lstat(path, &stb);
941                         path[l] = c;
942                         if (err2 < 0)
943                                 break;
944                         if (stb.st_dev == dev)
945                                 continue;
946                         dev = stb.st_dev;
947                         path[l] = 0;
948                         dump_to_cache(f, domain, path, exp);
949                         path[l] = c;
950                 }
951                 break;
952         }
953
954         fclose(f);
955         return err;
956 }
957
958 /**
959  * cache_export - Inform kernel of a new nfs_export
960  * @exp: target nfs_export
961  * @path: NUL-terminated C string containing export path
962  */
963 int cache_export(nfs_export *exp, char *path)
964 {
965         char buf[INET6_ADDRSTRLEN];
966         int err;
967         FILE *f;
968
969         f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
970         if (!f)
971                 return -1;
972
973
974         qword_print(f, "nfsd");
975         qword_print(f,
976                 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
977         qword_printuint(f, time(0) + exp->m_export.e_ttl);
978         qword_print(f, exp->m_client->m_hostname);
979         err = qword_eol(f);
980         
981         fclose(f);
982
983         err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
984                 || err;
985         return err;
986 }
987
988 /**
989  * cache_get_filehandle - given an nfs_export, get its root filehandle
990  * @exp: target nfs_export
991  * @len: length of requested file handle
992  * @p: NUL-terminated C string containing export path
993  *
994  * Returns pointer to NFS file handle of root directory of export
995  *
996  * { 
997  *   echo $domain $path $length 
998  *   read filehandle <&0
999  * } <> /proc/fs/nfsd/filehandle
1000  */
1001 struct nfs_fh_len *
1002 cache_get_filehandle(nfs_export *exp, int len, char *p)
1003 {
1004         FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
1005         char buf[200];
1006         char *bp = buf;
1007         int failed;
1008         static struct nfs_fh_len fh;
1009
1010         if (!f)
1011                 f = fopen("/proc/fs/nfs/filehandle", "r+");
1012         if (!f)
1013                 return NULL;
1014
1015         qword_print(f, exp->m_client->m_hostname);
1016         qword_print(f, p);
1017         qword_printint(f, len); 
1018         failed = qword_eol(f);
1019         
1020         if (!failed)
1021                 failed = (fgets(buf, sizeof(buf), f) == NULL);
1022         fclose(f);
1023         if (failed)
1024                 return NULL;
1025         memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1026         fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);
1027         return &fh;
1028 }