4aead6c3861864d1d42b25404d1df210483457ba
[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                 blkid_probe_all_new(cache);
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         free(found_path);
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                              (found->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
671                             strlen(found->m_export.e_path) !=
672                             strlen(found->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