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