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