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