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