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