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