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