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