mountd: fix is_subdirectory to understand '/'
[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 "fsloc.h"
33 #include "pseudoflavors.h"
34
35 #ifdef USE_BLKID
36 #include "blkid/blkid.h"
37 #endif
38
39 /*
40  * Invoked by RPC service loop
41  */
42 void    cache_set_fds(fd_set *fdset);
43 int     cache_process_req(fd_set *readfds);
44
45 enum nfsd_fsid {
46         FSID_DEV = 0,
47         FSID_NUM,
48         FSID_MAJOR_MINOR,
49         FSID_ENCODE_DEV,
50         FSID_UUID4_INUM,
51         FSID_UUID8,
52         FSID_UUID16,
53         FSID_UUID16_INUM,
54 };
55
56 /*
57  * Support routines for text-based upcalls.
58  * Fields are separated by spaces.
59  * Fields are either mangled to quote space tab newline slosh with slosh
60  * or a hexified with a leading \x
61  * Record is terminated with newline.
62  *
63  */
64 static int cache_export_ent(char *domain, struct exportent *exp, char *p);
65
66 #define INITIAL_MANAGED_GROUPS 100
67
68 char *lbuf  = NULL;
69 int lbuflen = 0;
70 extern int use_ipaddr;
71
72 static void auth_unix_ip(FILE *f)
73 {
74         /* requests are
75          *  class IP-ADDR
76          * Ignore if class != "nfsd"
77          * Otherwise find domainname and write back:
78          *
79          *  "nfsd" IP-ADDR expiry domainname
80          */
81         char *cp;
82         char class[20];
83         char ipaddr[INET6_ADDRSTRLEN];
84         char *client = NULL;
85         struct addrinfo *tmp = NULL;
86         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
87                 return;
88
89         xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
90
91         cp = lbuf;
92
93         if (qword_get(&cp, class, 20) <= 0 ||
94             strcmp(class, "nfsd") != 0)
95                 return;
96
97         if (qword_get(&cp, ipaddr, sizeof(ipaddr)) <= 0)
98                 return;
99
100         tmp = host_pton(ipaddr);
101         if (tmp == NULL)
102                 return;
103
104         auth_reload();
105
106         /* addr is a valid, interesting address, find the domain name... */
107         if (!use_ipaddr) {
108                 struct addrinfo *ai = NULL;
109
110                 ai = client_resolve(tmp->ai_addr);
111                 if (ai) {
112                         client = client_compose(ai);
113                         freeaddrinfo(ai);
114                 }
115         }
116         qword_print(f, "nfsd");
117         qword_print(f, ipaddr);
118         qword_printtimefrom(f, 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         freeaddrinfo(tmp);
128
129 }
130
131 static void auth_unix_gid(FILE *f)
132 {
133         /* Request are
134          *  uid
135          * reply is
136          *  uid expiry count list of group ids
137          */
138         uid_t uid;
139         struct passwd *pw;
140         static gid_t *groups = NULL;
141         static int groups_len = 0;
142         gid_t *more_groups;
143         int ngroups;
144         int rv, i;
145         char *cp;
146
147         if (groups_len == 0) {
148                 groups = malloc(sizeof(gid_t) * INITIAL_MANAGED_GROUPS);
149                 if (!groups)
150                         return;
151
152                 groups_len = INITIAL_MANAGED_GROUPS;
153         }
154
155         ngroups = groups_len;
156
157         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
158                 return;
159
160         cp = lbuf;
161         if (qword_get_uint(&cp, &uid) != 0)
162                 return;
163
164         pw = getpwuid(uid);
165         if (!pw)
166                 rv = -1;
167         else {
168                 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
169                 if (rv == -1 && ngroups >= groups_len) {
170                         more_groups = realloc(groups, sizeof(gid_t)*ngroups);
171                         if (!more_groups)
172                                 rv = -1;
173                         else {
174                                 groups = more_groups;
175                                 groups_len = ngroups;
176                                 rv = getgrouplist(pw->pw_name, pw->pw_gid,
177                                                   groups, &ngroups);
178                         }
179                 }
180         }
181         qword_printuint(f, uid);
182         qword_printtimefrom(f, DEFAULT_TTL);
183         if (rv >= 0) {
184                 qword_printuint(f, ngroups);
185                 for (i=0; i<ngroups; i++)
186                         qword_printuint(f, groups[i]);
187         } else
188                 qword_printuint(f, 0);
189         qword_eol(f);
190 }
191
192 #if USE_BLKID
193 static const char *get_uuid_blkdev(char *path)
194 {
195         /* We set *safe if we know that we need the
196          * fsid from statfs too.
197          */
198         static blkid_cache cache = NULL;
199         struct stat stb;
200         char *devname;
201         blkid_tag_iterate iter;
202         blkid_dev dev;
203         const char *type;
204         const char *val, *uuid = NULL;
205
206         if (cache == NULL)
207                 blkid_get_cache(&cache, NULL);
208
209         if (stat(path, &stb) != 0)
210                 return NULL;
211         devname = blkid_devno_to_devname(stb.st_dev);
212         if (!devname)
213                 return NULL;
214         dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
215         free(devname);
216         if (!dev)
217                 return NULL;
218         iter = blkid_tag_iterate_begin(dev);
219         if (!iter)
220                 return NULL;
221         while (blkid_tag_next(iter, &type, &val) == 0) {
222                 if (strcmp(type, "UUID") == 0)
223                         uuid = val;
224                 if (strcmp(type, "TYPE") == 0 &&
225                     strcmp(val, "btrfs") == 0) {
226                         uuid = NULL;
227                         break;
228                 }
229         }
230         blkid_tag_iterate_end(iter);
231         return uuid;
232 }
233 #else
234 #define get_uuid_blkdev(path) (NULL)
235 #endif
236
237 static int get_uuid(const char *val, size_t uuidlen, char *u)
238 {
239         /* extract hex digits from uuidstr and compose a uuid
240          * of the given length (max 16), xoring bytes to make
241          * a smaller uuid.
242          */
243         size_t i = 0;
244         
245         memset(u, 0, uuidlen);
246         for ( ; *val ; val++) {
247                 int c = *val;
248                 if (!isxdigit(c))
249                         continue;
250                 if (isalpha(c)) {
251                         if (isupper(c))
252                                 c = c - 'A' + 10;
253                         else
254                                 c = c - 'a' + 10;
255                 } else
256                         c = c - '0' + 0;
257                 if ((i&1) == 0)
258                         c <<= 4;
259                 u[i/2] ^= (char)c;
260                 i++;
261                 if (i == uuidlen*2)
262                         i = 0;
263         }
264         return 1;
265 }
266
267 static int uuid_by_path(char *path, int type, size_t uuidlen, char *uuid)
268 {
269         /* get a uuid for the filesystem found at 'path'.
270          * There are several possible ways of generating the
271          * uuids (types).
272          * Type 0 is used for new filehandles, while other types
273          * may be used to interpret old filehandle - to ensure smooth
274          * forward migration.
275          * We return 1 if a uuid was found (and it might be worth 
276          * trying the next type) or 0 if no more uuid types can be
277          * extracted.
278          */
279
280         /* Possible sources of uuid are
281          * - blkid uuid
282          * - statfs64 uuid
283          *
284          * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
285          * encoding of the device that the filesystem is mounted from, so
286          * it we be very bad to use that (as device numbers change).  blkid
287          * must be preferred.
288          * On other filesystems (e.g. btrfs) the statfs64 uuid contains
289          * important info that the blkid uuid cannot contain:  This happens
290          * when multiple subvolumes are exported (they have the same
291          * blkid uuid but different statfs64 uuids).
292          * We rely on get_uuid_blkdev *knowing* which is which and not returning
293          * a uuid for filesystems where the statfs64 uuid is better.
294          *
295          */
296         struct statfs64 st;
297         char fsid_val[17];
298         const char *blkid_val;
299         const char *val;
300
301         blkid_val = get_uuid_blkdev(path);
302
303         if (statfs64(path, &st) == 0 &&
304             (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
305                 snprintf(fsid_val, 17, "%08x%08x",
306                          st.f_fsid.__val[0], st.f_fsid.__val[1]);
307         else
308                 fsid_val[0] = 0;
309
310         if (blkid_val && (type--) == 0)
311                 val = blkid_val;
312         else if (fsid_val[0] && (type--) == 0)
313                 val = fsid_val;
314         else
315                 return 0;
316
317         get_uuid(val, uuidlen, uuid);
318         return 1;
319 }
320
321 /* Iterate through /etc/mtab, finding mountpoints
322  * at or below a given path
323  */
324 static char *next_mnt(void **v, char *p)
325 {
326         FILE *f;
327         struct mntent *me;
328         size_t l = strlen(p);
329         if (*v == NULL) {
330                 f = setmntent("/etc/mtab", "r");
331                 *v = f;
332         } else
333                 f = *v;
334         while ((me = getmntent(f)) != NULL &&
335                (strncmp(me->mnt_dir, p, l) != 0 ||
336                 me->mnt_dir[l] != '/'))
337                 ;
338         if (me == NULL) {
339                 endmntent(f);
340                 *v = NULL;
341                 return NULL;
342         }
343         return me->mnt_dir;
344 }
345
346 static int is_subdirectory(char *child, char *parent)
347 {
348         size_t l = strlen(parent);
349
350         if (strcmp(parent, "/") == 0)
351                 return 1;
352
353         return strcmp(child, parent) == 0
354                 || (strncmp(child, parent, l) == 0 && child[l] == '/');
355 }
356
357 static int path_matches(nfs_export *exp, char *path)
358 {
359         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
360                 return is_subdirectory(path, exp->m_export.e_path);
361         return strcmp(path, exp->m_export.e_path) == 0;
362 }
363
364 static int
365 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
366 {
367         return path_matches(exp, path) && client_matches(exp, dom, ai);
368 }
369
370 /* True iff e1 is a child of e2 and e2 has crossmnt set: */
371 static bool subexport(struct exportent *e1, struct exportent *e2)
372 {
373         char *p1 = e1->e_path, *p2 = e2->e_path;
374         size_t l2 = strlen(p2);
375
376         return e2->e_flags & NFSEXP_CROSSMOUNT
377                 && is_subdirectory(p1, p2);
378 }
379
380 struct parsed_fsid {
381         int fsidtype;
382         /* We could use a union for this, but it would be more
383          * complicated; why bother? */
384         unsigned int inode;
385         unsigned int minor;
386         unsigned int major;
387         unsigned int fsidnum;
388         size_t uuidlen;
389         char *fhuuid;
390 };
391
392 static int parse_fsid(int fsidtype, int fsidlen, char *fsid,
393                 struct parsed_fsid *parsed)
394 {
395         unsigned int dev;
396         unsigned long long inode64;
397
398         memset(parsed, 0, sizeof(*parsed));
399         parsed->fsidtype = fsidtype;
400         switch(fsidtype) {
401         case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
402                 if (fsidlen != 8)
403                         return -1;
404                 memcpy(&dev, fsid, 4);
405                 memcpy(&parsed->inode, fsid+4, 4);
406                 parsed->major = ntohl(dev)>>16;
407                 parsed->minor = ntohl(dev) & 0xFFFF;
408                 break;
409
410         case FSID_NUM: /* 4 bytes - fsid */
411                 if (fsidlen != 4)
412                         return -1;
413                 memcpy(&parsed->fsidnum, fsid, 4);
414                 break;
415
416         case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode 
417                  * This format is never actually used but was
418                  * an historical accident
419                  */
420                 if (fsidlen != 12)
421                         return -1;
422                 memcpy(&dev, fsid, 4);
423                 parsed->major = ntohl(dev);
424                 memcpy(&dev, fsid+4, 4);
425                 parsed->minor = ntohl(dev);
426                 memcpy(&parsed->inode, fsid+8, 4);
427                 break;
428
429         case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
430                 /* This is *host* endian, not net-byte-order, because
431                  * no-one outside this host has any business interpreting it
432                  */
433                 if (fsidlen != 8)
434                         return -1;
435                 memcpy(&dev, fsid, 4);
436                 memcpy(&parsed->inode, fsid+4, 4);
437                 parsed->major = (dev & 0xfff00) >> 8;
438                 parsed->minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
439                 break;
440
441         case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
442                 if (fsidlen != 8)
443                         return -1;
444                 memcpy(&parsed->inode, fsid, 4);
445                 parsed->uuidlen = 4;
446                 parsed->fhuuid = fsid+4;
447                 break;
448         case FSID_UUID8: /* 8 byte uuid */
449                 if (fsidlen != 8)
450                         return -1;
451                 parsed->uuidlen = 8;
452                 parsed->fhuuid = fsid;
453                 break;
454         case FSID_UUID16: /* 16 byte uuid */
455                 if (fsidlen != 16)
456                         return -1;
457                 parsed->uuidlen = 16;
458                 parsed->fhuuid = fsid;
459                 break;
460         case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
461                 if (fsidlen != 24)
462                         return -1;
463                 memcpy(&inode64, fsid, 8);
464                 parsed->inode = inode64;
465                 parsed->uuidlen = 16;
466                 parsed->fhuuid = fsid+8;
467                 break;
468         }
469         return 0;
470 }
471
472 static bool match_fsid(struct parsed_fsid *parsed, nfs_export *exp, char *path)
473 {
474         struct stat stb;
475         int type;
476         char u[16];
477
478         if (stat(path, &stb) != 0)
479                 return false;
480         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode))
481                 return false;
482
483         switch (parsed->fsidtype) {
484         case FSID_DEV:
485         case FSID_MAJOR_MINOR:
486         case FSID_ENCODE_DEV:
487                 if (stb.st_ino != parsed->inode)
488                         return false;
489                 if (parsed->major != major(stb.st_dev) ||
490                     parsed->minor != minor(stb.st_dev))
491                         return false;
492                 return true;
493         case FSID_NUM:
494                 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
495                      exp->m_export.e_fsid != parsed->fsidnum))
496                         return false;
497                 return true;
498         case FSID_UUID4_INUM:
499         case FSID_UUID16_INUM:
500                 if (stb.st_ino != parsed->inode)
501                         return false;
502                 goto check_uuid;
503         case FSID_UUID8:
504         case FSID_UUID16:
505                 if (!is_mountpoint(path))
506                         return false;
507         check_uuid:
508                 if (exp->m_export.e_uuid)
509                         get_uuid(exp->m_export.e_uuid, parsed->uuidlen, u);
510                 else
511                         for (type = 0;
512                              uuid_by_path(path, type, parsed->uuidlen, u);
513                              type++)
514                                 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) == 0)
515                                         return true;
516
517                 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) != 0)
518                         return false;
519                 return true;
520         }
521         /* Well, unreachable, actually: */
522         return false;
523 }
524
525 static struct addrinfo *lookup_client_addr(char *dom)
526 {
527         struct addrinfo *ret;
528         struct addrinfo *tmp;
529
530         dom++; /* skip initial "$" */
531
532         tmp = host_pton(dom);
533         if (tmp == NULL)
534                 return NULL;
535         ret = client_resolve(tmp->ai_addr);
536         freeaddrinfo(tmp);
537         return ret;
538 }
539
540 static void nfsd_fh(FILE *f)
541 {
542         /* request are:
543          *  domain fsidtype fsid
544          * interpret fsid, find export point and options, and write:
545          *  domain fsidtype fsid expiry path
546          */
547         char *cp;
548         char *dom;
549         int fsidtype;
550         int fsidlen;
551         char fsid[32];
552         struct parsed_fsid parsed;
553         struct exportent *found = NULL;
554         struct addrinfo *ai = NULL;
555         char *found_path = NULL;
556         nfs_export *exp;
557         int i;
558         int dev_missing = 0;
559
560         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
561                 return;
562
563         xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
564
565         cp = lbuf;
566
567         dom = malloc(strlen(cp));
568         if (dom == NULL)
569                 return;
570         if (qword_get(&cp, dom, strlen(cp)) <= 0)
571                 goto out;
572         if (qword_get_int(&cp, &fsidtype) != 0)
573                 goto out;
574         if (fsidtype < 0 || fsidtype > 7)
575                 goto out; /* unknown type */
576         if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
577                 goto out;
578         if (parse_fsid(fsidtype, fsidlen, fsid, &parsed))
579                 goto out;
580
581         auth_reload();
582
583         if (is_ipaddr_client(dom)) {
584                 ai = lookup_client_addr(dom);
585                 if (!ai)
586                         goto out;
587         }
588
589         /* Now determine export point for this fsid/domain */
590         for (i=0 ; i < MCL_MAXTYPES; i++) {
591                 nfs_export *next_exp;
592                 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
593                         char *path;
594
595                         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
596                                 static nfs_export *prev = NULL;
597                                 static void *mnt = NULL;
598                                 
599                                 if (prev == exp) {
600                                         /* try a submount */
601                                         path = next_mnt(&mnt, exp->m_export.e_path);
602                                         if (!path) {
603                                                 next_exp = exp->m_next;
604                                                 prev = NULL;
605                                                 continue;
606                                         }
607                                         next_exp = exp;
608                                 } else {
609                                         prev = exp;
610                                         mnt = NULL;
611                                         path = exp->m_export.e_path;
612                                         next_exp = exp;
613                                 }
614                         } else {
615                                 path = exp->m_export.e_path;
616                                 next_exp = exp->m_next;
617                         }
618
619                         if (!is_ipaddr_client(dom)
620                                         && !namelist_client_matches(exp, dom))
621                                 continue;
622                         if (exp->m_export.e_mountpoint &&
623                             !is_mountpoint(exp->m_export.e_mountpoint[0]?
624                                            exp->m_export.e_mountpoint:
625                                            exp->m_export.e_path))
626                                 dev_missing ++;
627
628                         if (!match_fsid(&parsed, exp, path))
629                                 continue;
630                         if (is_ipaddr_client(dom)
631                                         && !ipaddr_client_matches(exp, ai))
632                                 continue;
633                         if (!found || subexport(&exp->m_export, found)) {
634                                 found = &exp->m_export;
635                                 free(found_path);
636                                 found_path = strdup(path);
637                                 if (found_path == NULL)
638                                         goto out;
639                         } else if (strcmp(found->e_path, exp->m_export.e_path) != 0
640                                    && !subexport(found, &exp->m_export))
641                         {
642                                 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
643                                      found_path, path, dom);
644                         } else {
645                                 /* same path, if one is V4ROOT, choose the other */
646                                 if (found->e_flags & NFSEXP_V4ROOT) {
647                                         found = &exp->m_export;
648                                         free(found_path);
649                                         found_path = strdup(path);
650                                         if (found_path == NULL)
651                                                 goto out;
652                                 }
653                         }
654                 }
655         }
656         if (found && 
657             found->e_mountpoint &&
658             !is_mountpoint(found->e_mountpoint[0]?
659                            found->e_mountpoint:
660                            found->e_path)) {
661                 /* Cannot export this yet 
662                  * should log a warning, but need to rate limit
663                    xlog(L_WARNING, "%s not exported as %d not a mountpoint",
664                    found->e_path, found->e_mountpoint);
665                  */
666                 /* FIXME we need to make sure we re-visit this later */
667                 goto out;
668         }
669         if (!found && dev_missing) {
670                 /* The missing dev could be what we want, so just be
671                  * quite rather than returning stale yet
672                  */
673                 goto out;
674         }
675
676         if (found)
677                 if (cache_export_ent(dom, found, found_path) < 0)
678                         found = 0;
679
680         qword_print(f, dom);
681         qword_printint(f, fsidtype);
682         qword_printhex(f, fsid, fsidlen);
683         /* The fsid -> path lookup can be quite expensive as it
684          * potentially stats and reads lots of devices, and some of those
685          * might have spun-down.  The Answer is not likely to
686          * change underneath us, and an 'exportfs -f' can always
687          * remove this from the kernel, so use a really log
688          * timeout.  Maybe this should be configurable on the command
689          * line.
690          */
691         qword_printint(f, 0x7fffffff);
692         if (found)
693                 qword_print(f, found_path);
694         qword_eol(f);
695  out:
696         if (found_path)
697                 free(found_path);
698         freeaddrinfo(ai);
699         free(dom);
700         xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
701         return;         
702 }
703
704 static void write_fsloc(FILE *f, struct exportent *ep)
705 {
706         struct servers *servers;
707
708         if (ep->e_fslocmethod == FSLOC_NONE)
709                 return;
710
711         servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
712         if (!servers)
713                 return;
714         qword_print(f, "fsloc");
715         qword_printint(f, servers->h_num);
716         if (servers->h_num >= 0) {
717                 int i;
718                 for (i=0; i<servers->h_num; i++) {
719                         qword_print(f, servers->h_mp[i]->h_host);
720                         qword_print(f, servers->h_mp[i]->h_path);
721                 }
722         }
723         qword_printint(f, servers->h_referral);
724         release_replicas(servers);
725 }
726
727 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
728 {
729         struct sec_entry *p;
730
731         for (p = ep->e_secinfo; p->flav; p++)
732                 ; /* Do nothing */
733         if (p == ep->e_secinfo) {
734                 /* There was no sec= option */
735                 return;
736         }
737         qword_print(f, "secinfo");
738         qword_printint(f, p - ep->e_secinfo);
739         for (p = ep->e_secinfo; p->flav; p++) {
740                 qword_printint(f, p->flav->fnum);
741                 qword_printint(f, p->flags & flag_mask);
742         }
743
744 }
745
746 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
747 {
748         qword_print(f, domain);
749         qword_print(f, path);
750         if (exp) {
751                 int different_fs = strcmp(path, exp->e_path) != 0;
752                 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
753
754                 qword_printtimefrom(f, exp->e_ttl);
755                 qword_printint(f, exp->e_flags & flag_mask);
756                 qword_printint(f, exp->e_anonuid);
757                 qword_printint(f, exp->e_anongid);
758                 qword_printint(f, exp->e_fsid);
759                 write_fsloc(f, exp);
760                 write_secinfo(f, exp, flag_mask);
761                 if (exp->e_uuid == NULL || different_fs) {
762                         char u[16];
763                         if (uuid_by_path(path, 0, 16, u)) {
764                                 qword_print(f, "uuid");
765                                 qword_printhex(f, u, 16);
766                         }
767                 } else {
768                         char u[16];
769                         get_uuid(exp->e_uuid, 16, u);
770                         qword_print(f, "uuid");
771                         qword_printhex(f, u, 16);
772                 }
773         } else
774                 qword_printtimefrom(f, DEFAULT_TTL);
775         return qword_eol(f);
776 }
777
778 static nfs_export *
779 lookup_export(char *dom, char *path, struct addrinfo *ai)
780 {
781         nfs_export *exp;
782         nfs_export *found = NULL;
783         int found_type = 0;
784         int i;
785
786         for (i=0 ; i < MCL_MAXTYPES; i++) {
787                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
788                         if (!export_matches(exp, dom, path, ai))
789                                 continue;
790                         if (!found) {
791                                 found = exp;
792                                 found_type = i;
793                                 continue;
794                         }
795                         /* Always prefer non-V4ROOT exports */
796                         if (exp->m_export.e_flags & NFSEXP_V4ROOT)
797                                 continue;
798                         if (found->m_export.e_flags & NFSEXP_V4ROOT) {
799                                 found = exp;
800                                 found_type = i;
801                                 continue;
802                         }
803
804                         /* If one is a CROSSMOUNT, then prefer the longest path */
805                         if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
806                              (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
807                             strlen(found->m_export.e_path) !=
808                             strlen(exp->m_export.e_path)) {
809
810                                 if (strlen(exp->m_export.e_path) >
811                                     strlen(found->m_export.e_path)) {
812                                         found = exp;
813                                         found_type = i;
814                                 }
815                                 continue;
816
817                         } else if (found_type == i && found->m_warned == 0) {
818                                 xlog(L_WARNING, "%s exported to both %s and %s, "
819                                      "arbitrarily choosing options from first",
820                                      path, found->m_client->m_hostname, exp->m_client->m_hostname,
821                                      dom);
822                                 found->m_warned = 1;
823                         }
824                 }
825         }
826         return found;
827 }
828
829 #ifdef HAVE_NFS_PLUGIN_H
830 #include <dlfcn.h>
831 #include <link.h>
832 #include <nfs-plugin.h>
833
834 /*
835  * Find the export entry for the parent of "pathname".
836  * Caller must not free returned exportent.
837  */
838 static struct exportent *lookup_parent_export(char *dom,
839                 const char *pathname, struct addrinfo *ai)
840 {
841         char *parent, *slash;
842         nfs_export *result;
843
844         parent = strdup(pathname);
845         if (parent == NULL) {
846                 xlog(D_GENERAL, "%s: failed to allocate parent path buffer",
847                         __func__);
848                 goto out_default;
849         }
850         xlog(D_CALL, "%s: pathname = '%s'", __func__, pathname);
851
852 again:
853         /* shorten pathname by one component */
854         slash = strrchr(parent, '/');
855         if (slash == NULL) {
856                 xlog(D_GENERAL, "%s: no slash found in pathname",
857                         __func__);
858                 goto out_default;
859         }
860         *slash = '\0';
861
862         if (strlen(parent) == 0) {
863                 result = lookup_export(dom, "/", ai);
864                 if (result == NULL) {
865                         xlog(L_ERROR, "%s: no root export found.", __func__);
866                         goto out_default;
867                 }
868                 goto out;
869         }
870
871         result = lookup_export(dom, parent, ai);
872         if (result == NULL) {
873                 xlog(D_GENERAL, "%s: lookup_export(%s) found nothing",
874                         __func__, parent);
875                 goto again;
876         }
877
878 out:
879         xlog(D_CALL, "%s: found export for %s", __func__, parent);
880         free(parent);
881         return &result->m_export;
882
883 out_default:
884         free(parent);
885         return mkexportent("*", "/", "insecure");
886 }
887
888 /*
889  * Walk through a set of FS locations and build an e_fslocdata string.
890  * Returns true if all went to plan; otherwise, false.
891  */
892 static bool locations_to_fslocdata(struct jp_ops *ops,
893                 nfs_fsloc_set_t locations, char *fslocdata,
894                 size_t remaining, int *ttl)
895 {
896         char *server, *last_path, *rootpath, *ptr;
897         _Bool seen = false;
898
899         last_path = NULL;
900         rootpath = NULL;
901         server = NULL;
902         ptr = fslocdata;
903         *ttl = 0;
904
905         for (;;) {
906                 enum jp_status status;
907                 int len;
908
909                 status = ops->jp_get_next_location(locations, &server,
910                                                         &rootpath, ttl);
911                 if (status == JP_EMPTY)
912                         break;
913                 if (status != JP_OK) {
914                         xlog(D_GENERAL, "%s: failed to parse location: %s",
915                                 __func__, ops->jp_error(status));
916                         goto out_false;
917                 }
918                 xlog(D_GENERAL, "%s: Location: %s:%s",
919                         __func__, server, rootpath);
920
921                 if (last_path && strcmp(rootpath, last_path) == 0) {
922                         len = snprintf(ptr, remaining, "+%s", server);
923                         if (len < 0) {
924                                 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
925                                 goto out_false;
926                         }
927                         if ((size_t)len >= remaining) {
928                                 xlog(D_GENERAL, "%s: fslocdata buffer overflow", __func__);
929                                 goto out_false;
930                         }
931                         remaining -= (size_t)len;
932                         ptr += len;
933                 } else {
934                         if (last_path == NULL)
935                                 len = snprintf(ptr, remaining, "%s@%s",
936                                                         rootpath, server);
937                         else
938                                 len = snprintf(ptr, remaining, ":%s@%s",
939                                                         rootpath, server);
940                         if (len < 0) {
941                                 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
942                                 goto out_false;
943                         }
944                         if ((size_t)len >= remaining) {
945                                 xlog(D_GENERAL, "%s: fslocdata buffer overflow",
946                                         __func__);
947                                 goto out_false;
948                         }
949                         remaining -= (size_t)len;
950                         ptr += len;
951                         last_path = rootpath;
952                 }
953
954                 seen = true;
955                 free(rootpath);
956                 free(server);
957         }
958
959         xlog(D_CALL, "%s: fslocdata='%s', ttl=%d",
960                 __func__, fslocdata, *ttl);
961         return seen;
962
963 out_false:
964         free(rootpath);
965         free(server);
966         return false;
967 }
968
969 /*
970  * Duplicate the junction's parent's export options and graft in
971  * the fslocdata we constructed from the locations list.
972  */
973 static struct exportent *create_junction_exportent(struct exportent *parent,
974                 const char *junction, const char *fslocdata, int ttl)
975 {
976         static struct exportent *eep;
977
978         eep = (struct exportent *)malloc(sizeof(*eep));
979         if (eep == NULL)
980                 goto out_nomem;
981
982         dupexportent(eep, parent);
983         strcpy(eep->e_path, junction);
984         eep->e_hostname = strdup(parent->e_hostname);
985         if (eep->e_hostname == NULL) {
986                 free(eep);
987                 goto out_nomem;
988         }
989         free(eep->e_uuid);
990         eep->e_uuid = NULL;
991         eep->e_ttl = (unsigned int)ttl;
992
993         free(eep->e_fslocdata);
994         eep->e_fslocdata = strdup(fslocdata);
995         if (eep->e_fslocdata == NULL) {
996                 free(eep->e_hostname);
997                 free(eep);
998                 goto out_nomem;
999         }
1000         eep->e_fslocmethod = FSLOC_REFER;
1001         return eep;
1002
1003 out_nomem:
1004         xlog(L_ERROR, "%s: No memory", __func__);
1005         return NULL;
1006 }
1007
1008 /*
1009  * Walk through the set of FS locations and build an exportent.
1010  * Returns pointer to an exportent if "junction" refers to a junction.
1011  */
1012 static struct exportent *locations_to_export(struct jp_ops *ops,
1013                 nfs_fsloc_set_t locations, const char *junction,
1014                 struct exportent *parent)
1015 {
1016         static char fslocdata[BUFSIZ];
1017         int ttl;
1018
1019         fslocdata[0] = '\0';
1020         if (!locations_to_fslocdata(ops, locations,
1021                                         fslocdata, sizeof(fslocdata), &ttl))
1022                 return NULL;
1023         return create_junction_exportent(parent, junction, fslocdata, ttl);
1024 }
1025
1026 /*
1027  * Retrieve locations information in "junction" and dump it to the
1028  * kernel.  Returns pointer to an exportent if "junction" refers
1029  * to a junction.
1030  */
1031 static struct exportent *invoke_junction_ops(void *handle, char *dom,
1032                 const char *junction, struct addrinfo *ai)
1033 {
1034         struct exportent *parent, *exp = NULL;
1035         nfs_fsloc_set_t locations;
1036         enum jp_status status;
1037         struct jp_ops *ops;
1038         char *error;
1039
1040         ops = (struct jp_ops *)dlsym(handle, "nfs_junction_ops");
1041         error = dlerror();
1042         if (error != NULL) {
1043                 xlog(D_GENERAL, "%s: dlsym(jp_junction_ops): %s",
1044                         __func__, error);
1045                 return NULL;
1046         }
1047         if (ops->jp_api_version != JP_API_VERSION) {
1048                 xlog(D_GENERAL, "%s: unrecognized junction API version: %u",
1049                         __func__, ops->jp_api_version);
1050                 return NULL;
1051         }
1052
1053         status = ops->jp_init(false);
1054         if (status != JP_OK) {
1055                 xlog(D_GENERAL, "%s: failed to resolve %s: %s",
1056                         __func__, junction, ops->jp_error(status));
1057                 return NULL;
1058         }
1059
1060         status = ops->jp_get_locations(junction, &locations);
1061         switch (status) {
1062         case JP_OK:
1063                 break;
1064         case JP_NOTJUNCTION:
1065                 xlog(D_GENERAL, "%s: %s is not a junction",
1066                         __func__, junction);
1067                 goto out;
1068         default:
1069                 xlog(L_WARNING, "Dangling junction %s: %s",
1070                         junction, ops->jp_error(status));
1071                 goto out;
1072         }
1073
1074         parent = lookup_parent_export(dom, junction, ai);
1075         if (parent == NULL)
1076                 goto out;
1077
1078         exp = locations_to_export(ops, locations, junction, parent);
1079
1080         ops->jp_put_locations(locations);
1081
1082 out:
1083         ops->jp_done();
1084         return exp;
1085 }
1086
1087 /*
1088  * Load the junction plug-in, then try to resolve "pathname".
1089  * Returns pointer to an initialized exportent if "junction"
1090  * refers to a junction, or NULL if not.
1091  */
1092 static struct exportent *lookup_junction(char *dom, const char *pathname,
1093                 struct addrinfo *ai)
1094 {
1095         struct exportent *exp;
1096         struct link_map *map;
1097         void *handle;
1098
1099         handle = dlopen("libnfsjunct.so", RTLD_NOW);
1100         if (handle == NULL) {
1101                 xlog(D_GENERAL, "%s: dlopen: %s", __func__, dlerror());
1102                 return NULL;
1103         }
1104
1105         if (dlinfo(handle, RTLD_DI_LINKMAP, &map) == 0)
1106                 xlog(D_GENERAL, "%s: loaded plug-in %s",
1107                         __func__, map->l_name);
1108
1109         (void)dlerror();        /* Clear any error */
1110
1111         exp = invoke_junction_ops(handle, dom, pathname, ai);
1112
1113         /* We could leave it loaded to make junction resolution
1114          * faster next time.  However, if we want to replace the
1115          * library, that would require restarting mountd. */
1116         (void)dlclose(handle);
1117         return exp;
1118 }
1119
1120 static void lookup_nonexport(FILE *f, char *dom, char *path,
1121                 struct addrinfo *ai)
1122 {
1123         struct exportent *eep;
1124
1125         eep = lookup_junction(dom, path, ai);
1126         dump_to_cache(f, dom, path, eep);
1127         if (eep == NULL)
1128                 return;
1129         exportent_release(eep);
1130         free(eep);
1131 }
1132 #else   /* !HAVE_NFS_PLUGIN_H */
1133 static void lookup_nonexport(FILE *f, char *dom, char *path,
1134                 struct addrinfo *UNUSED(ai))
1135 {
1136         dump_to_cache(f, dom, path, NULL);
1137 }
1138 #endif  /* !HAVE_NFS_PLUGIN_H */
1139
1140 static void nfsd_export(FILE *f)
1141 {
1142         /* requests are:
1143          *  domain path
1144          * determine export options and return:
1145          *  domain path expiry flags anonuid anongid fsid
1146          */
1147
1148         char *cp;
1149         char *dom, *path;
1150         nfs_export *found = NULL;
1151         struct addrinfo *ai = NULL;
1152
1153         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
1154                 return;
1155
1156         xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
1157
1158         cp = lbuf;
1159         dom = malloc(strlen(cp));
1160         path = malloc(strlen(cp));
1161
1162         if (!dom || !path)
1163                 goto out;
1164
1165         if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
1166                 goto out;
1167         if (qword_get(&cp, path, strlen(lbuf)) <= 0)
1168                 goto out;
1169
1170         auth_reload();
1171
1172         if (is_ipaddr_client(dom)) {
1173                 ai = lookup_client_addr(dom);
1174                 if (!ai)
1175                         goto out;
1176         }
1177
1178         found = lookup_export(dom, path, ai);
1179
1180         if (found) {
1181                 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
1182                         xlog(L_WARNING,
1183                              "Cannot export %s, possibly unsupported filesystem"
1184                              " or fsid= required", path);
1185                         dump_to_cache(f, dom, path, NULL);
1186                 }
1187         } else
1188                 lookup_nonexport(f, dom, path, ai);
1189
1190  out:
1191         xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
1192         if (dom) free(dom);
1193         if (path) free(path);
1194         freeaddrinfo(ai);
1195 }
1196
1197
1198 struct {
1199         char *cache_name;
1200         void (*cache_handle)(FILE *f);
1201         FILE *f;
1202         char vbuf[RPC_CHAN_BUF_SIZE];
1203 } cachelist[] = {
1204         { "auth.unix.ip", auth_unix_ip, NULL, ""},
1205         { "auth.unix.gid", auth_unix_gid, NULL, ""},
1206         { "nfsd.export", nfsd_export, NULL, ""},
1207         { "nfsd.fh", nfsd_fh, NULL, ""},
1208         { NULL, NULL, NULL, ""}
1209 };
1210
1211 extern int manage_gids;
1212
1213 /**
1214  * cache_open - prepare communications channels with kernel RPC caches
1215  *
1216  */
1217 void cache_open(void) 
1218 {
1219         int i;
1220         for (i=0; cachelist[i].cache_name; i++ ) {
1221                 char path[100];
1222                 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
1223                         continue;
1224                 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
1225                 cachelist[i].f = fopen(path, "r+");
1226                 if (cachelist[i].f != NULL) {
1227                         setvbuf(cachelist[i].f, cachelist[i].vbuf, _IOLBF, 
1228                                 RPC_CHAN_BUF_SIZE);
1229                 }
1230         }
1231 }
1232
1233 /**
1234  * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
1235  * @fdset: pointer to fd_set to prepare
1236  */
1237 void cache_set_fds(fd_set *fdset)
1238 {
1239         int i;
1240         for (i=0; cachelist[i].cache_name; i++) {
1241                 if (cachelist[i].f)
1242                         FD_SET(fileno(cachelist[i].f), fdset);
1243         }
1244 }
1245
1246 /**
1247  * cache_process_req - process any active cache file descriptors during service loop iteration
1248  * @fdset: pointer to fd_set to examine for activity
1249  */
1250 int cache_process_req(fd_set *readfds) 
1251 {
1252         int i;
1253         int cnt = 0;
1254         for (i=0; cachelist[i].cache_name; i++) {
1255                 if (cachelist[i].f != NULL &&
1256                     FD_ISSET(fileno(cachelist[i].f), readfds)) {
1257                         cnt++;
1258                         cachelist[i].cache_handle(cachelist[i].f);
1259                         FD_CLR(fileno(cachelist[i].f), readfds);
1260                 }
1261         }
1262         return cnt;
1263 }
1264
1265
1266 /*
1267  * Give IP->domain and domain+path->options to kernel
1268  * % echo nfsd $IP  $[now+DEFAULT_TTL] $domain > /proc/net/rpc/auth.unix.ip/channel
1269  * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
1270  */
1271
1272 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
1273 {
1274         int err;
1275         FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
1276         if (!f)
1277                 return -1;
1278
1279         err = dump_to_cache(f, domain, exp->e_path, exp);
1280         if (err) {
1281                 xlog(L_WARNING,
1282                      "Cannot export %s, possibly unsupported filesystem or"
1283                      " fsid= required", exp->e_path);
1284         }
1285
1286         while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
1287                 /* really an 'if', but we can break out of
1288                  * a 'while' more easily */
1289                 /* Look along 'path' for other filesystems
1290                  * and export them with the same options
1291                  */
1292                 struct stat stb;
1293                 size_t l = strlen(exp->e_path);
1294                 __dev_t dev;
1295
1296                 if (strlen(path) <= l || path[l] != '/' ||
1297                     strncmp(exp->e_path, path, l) != 0)
1298                         break;
1299                 if (stat(exp->e_path, &stb) != 0)
1300                         break;
1301                 dev = stb.st_dev;
1302                 while(path[l] == '/') {
1303                         char c;
1304                         /* errors for submount should fail whole filesystem */
1305                         int err2;
1306
1307                         l++;
1308                         while (path[l] != '/' && path[l])
1309                                 l++;
1310                         c = path[l];
1311                         path[l] = 0;
1312                         err2 = lstat(path, &stb);
1313                         path[l] = c;
1314                         if (err2 < 0)
1315                                 break;
1316                         if (stb.st_dev == dev)
1317                                 continue;
1318                         dev = stb.st_dev;
1319                         path[l] = 0;
1320                         dump_to_cache(f, domain, path, exp);
1321                         path[l] = c;
1322                 }
1323                 break;
1324         }
1325
1326         fclose(f);
1327         return err;
1328 }
1329
1330 /**
1331  * cache_export - Inform kernel of a new nfs_export
1332  * @exp: target nfs_export
1333  * @path: NUL-terminated C string containing export path
1334  */
1335 int cache_export(nfs_export *exp, char *path)
1336 {
1337         char buf[INET6_ADDRSTRLEN];
1338         int err;
1339         FILE *f;
1340
1341         f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
1342         if (!f)
1343                 return -1;
1344
1345
1346         qword_print(f, "nfsd");
1347         qword_print(f,
1348                 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
1349         qword_printtimefrom(f, exp->m_export.e_ttl);
1350         qword_print(f, exp->m_client->m_hostname);
1351         err = qword_eol(f);
1352         
1353         fclose(f);
1354
1355         err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
1356                 || err;
1357         return err;
1358 }
1359
1360 /**
1361  * cache_get_filehandle - given an nfs_export, get its root filehandle
1362  * @exp: target nfs_export
1363  * @len: length of requested file handle
1364  * @p: NUL-terminated C string containing export path
1365  *
1366  * Returns pointer to NFS file handle of root directory of export
1367  *
1368  * { 
1369  *   echo $domain $path $length 
1370  *   read filehandle <&0
1371  * } <> /proc/fs/nfsd/filehandle
1372  */
1373 struct nfs_fh_len *
1374 cache_get_filehandle(nfs_export *exp, int len, char *p)
1375 {
1376         FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
1377         char buf[200];
1378         char *bp = buf;
1379         int failed;
1380         static struct nfs_fh_len fh;
1381
1382         if (!f)
1383                 f = fopen("/proc/fs/nfs/filehandle", "r+");
1384         if (!f)
1385                 return NULL;
1386
1387         qword_print(f, exp->m_client->m_hostname);
1388         qword_print(f, p);
1389         qword_printint(f, len); 
1390         failed = qword_eol(f);
1391         
1392         if (!failed)
1393                 failed = (fgets(buf, sizeof(buf), f) == NULL);
1394         fclose(f);
1395         if (failed)
1396                 return NULL;
1397         memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1398         fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);
1399         return &fh;
1400 }