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