Cleaned up some typos that were found in the various
[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         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                         qword_print(f, "uuid");
596                         qword_printhex(f, exp->e_uuid, 16);
597                 }
598 #endif
599         }
600         return qword_eol(f);
601 }
602
603 void nfsd_export(FILE *f)
604 {
605         /* requests are:
606          *  domain path
607          * determine export options and return:
608          *  domain path expiry flags anonuid anongid fsid
609          */
610
611         char *cp;
612         int i;
613         char *dom, *path;
614         nfs_export *exp, *found = NULL;
615         int found_type = 0;
616         struct in_addr addr;
617         struct hostent *he = NULL;
618
619
620         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
621                 return;
622
623         xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
624
625         cp = lbuf;
626         dom = malloc(strlen(cp));
627         path = malloc(strlen(cp));
628
629         if (!dom || !path)
630                 goto out;
631
632         if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
633                 goto out;
634         if (qword_get(&cp, path, strlen(lbuf)) <= 0)
635                 goto out;
636
637         auth_reload();
638
639         /* now find flags for this export point in this domain */
640         for (i=0 ; i < MCL_MAXTYPES; i++) {
641                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
642                         if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
643                                 continue;
644                         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
645                                 /* if path is a mountpoint below e_path, then OK */
646                                 int l = strlen(exp->m_export.e_path);
647                                 if (strcmp(path, exp->m_export.e_path) == 0 ||
648                                     (strncmp(path, exp->m_export.e_path, l) == 0 &&
649                                      path[l] == '/' &&
650                                      is_mountpoint(path)))
651                                         /* ok */;
652                                 else
653                                         continue;
654                         } else if (strcmp(path, exp->m_export.e_path) != 0)
655                                 continue;
656                         if (use_ipaddr) {
657                                 if (he == NULL) {
658                                         if (!inet_aton(dom, &addr))
659                                                 goto out;
660                                         he = client_resolve(addr);
661                                 }
662                                 if (!client_check(exp->m_client, he))
663                                         continue;
664                         }
665                         if (!found) {
666                                 found = exp;
667                                 found_type = i;
668                                 continue;
669                         }
670                         /* If one is a CROSSMOUNT, then prefer the longest path */
671                         if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
672                              (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
673                             strlen(found->m_export.e_path) !=
674                             strlen(exp->m_export.e_path)) {
675
676                                 if (strlen(exp->m_export.e_path) >
677                                     strlen(found->m_export.e_path)) {
678                                         found = exp;
679                                         found_type = i;
680                                 }
681                                 continue;
682
683                         } else if (found_type == i && found->m_warned == 0) {
684                                 xlog(L_WARNING, "%s exported to both %s and %s, "
685                                      "arbitrarily choosing options from first",
686                                      path, found->m_client->m_hostname, exp->m_client->m_hostname,
687                                      dom);
688                                 found->m_warned = 1;
689                         }
690                 }
691         }
692
693         if (found) {
694                 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
695                         xlog(L_WARNING,
696                              "Cannot export %s, possibly unsupported filesystem"
697                              " or fsid= required", path);
698                         dump_to_cache(f, dom, path, NULL);
699                 }
700         } else {
701                 dump_to_cache(f, dom, path, NULL);
702         }
703  out:
704         xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
705         if (dom) free(dom);
706         if (path) free(path);
707         if (he) free(he);
708 }
709
710
711 struct {
712         char *cache_name;
713         void (*cache_handle)(FILE *f);
714         FILE *f;
715 } cachelist[] = {
716         { "auth.unix.ip", auth_unix_ip},
717         { "auth.unix.gid", auth_unix_gid},
718         { "nfsd.export", nfsd_export},
719         { "nfsd.fh", nfsd_fh},
720         { NULL, NULL }
721 };
722
723 extern int manage_gids;
724 void cache_open(void) 
725 {
726         int i;
727         for (i=0; cachelist[i].cache_name; i++ ) {
728                 char path[100];
729                 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
730                         continue;
731                 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
732                 cachelist[i].f = fopen(path, "r+");
733         }
734 }
735
736 void cache_set_fds(fd_set *fdset)
737 {
738         int i;
739         for (i=0; cachelist[i].cache_name; i++) {
740                 if (cachelist[i].f)
741                         FD_SET(fileno(cachelist[i].f), fdset);
742         }
743 }
744
745 int cache_process_req(fd_set *readfds) 
746 {
747         int i;
748         int cnt = 0;
749         for (i=0; cachelist[i].cache_name; i++) {
750                 if (cachelist[i].f != NULL &&
751                     FD_ISSET(fileno(cachelist[i].f), readfds)) {
752                         cnt++;
753                         cachelist[i].cache_handle(cachelist[i].f);
754                         FD_CLR(fileno(cachelist[i].f), readfds);
755                 }
756         }
757         return cnt;
758 }
759
760
761 /*
762  * Give IP->domain and domain+path->options to kernel
763  * % echo nfsd $IP  $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
764  * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
765  */
766
767 int cache_export_ent(char *domain, struct exportent *exp, char *path)
768 {
769         int err;
770         FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
771         if (!f)
772                 return -1;
773
774         err = dump_to_cache(f, domain, exp->e_path, exp);
775         if (err) {
776                 xlog(L_WARNING,
777                      "Cannot export %s, possibly unsupported filesystem or"
778                      " fsid= required", exp->e_path);
779         }
780
781         while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
782                 /* really an 'if', but we can break out of
783                  * a 'while' more easily */
784                 /* Look along 'path' for other filesystems
785                  * and export them with the same options
786                  */
787                 struct stat stb;
788                 int l = strlen(exp->e_path);
789                 int dev;
790
791                 if (strlen(path) <= l || path[l] != '/' ||
792                     strncmp(exp->e_path, path, l) != 0)
793                         break;
794                 if (stat(exp->e_path, &stb) != 0)
795                         break;
796                 dev = stb.st_dev;
797                 while(path[l] == '/') {
798                         char c;
799                         /* errors for submount should fail whole filesystem */
800                         int err2;
801
802                         l++;
803                         while (path[l] != '/' && path[l])
804                                 l++;
805                         c = path[l];
806                         path[l] = 0;
807                         err2 = lstat(path, &stb);
808                         path[l] = c;
809                         if (err2 < 0)
810                                 break;
811                         if (stb.st_dev == dev)
812                                 continue;
813                         dev = stb.st_dev;
814                         path[l] = 0;
815                         dump_to_cache(f, domain, path, exp);
816                         path[l] = c;
817                 }
818                 break;
819         }
820
821         fclose(f);
822         return err;
823 }
824
825 int cache_export(nfs_export *exp, char *path)
826 {
827         int err;
828         FILE *f;
829
830         f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
831         if (!f)
832                 return -1;
833
834         qword_print(f, "nfsd");
835         qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
836         qword_printint(f, time(0)+30*60);
837         qword_print(f, exp->m_client->m_hostname);
838         err = qword_eol(f);
839         
840         fclose(f);
841
842         err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
843                 || err;
844         return err;
845 }
846
847 /* Get a filehandle.
848  * { 
849  *   echo $domain $path $length 
850  *   read filehandle <&0
851  * } <> /proc/fs/nfsd/filehandle
852  */
853 struct nfs_fh_len *
854 cache_get_filehandle(nfs_export *exp, int len, char *p)
855 {
856         FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
857         char buf[200];
858         char *bp = buf;
859         int failed;
860         static struct nfs_fh_len fh;
861
862         if (!f)
863                 f = fopen("/proc/fs/nfs/filehandle", "r+");
864         if (!f)
865                 return NULL;
866
867         qword_print(f, exp->m_client->m_hostname);
868         qword_print(f, p);
869         qword_printint(f, len); 
870         failed = qword_eol(f);
871         
872         if (!failed)
873                 failed = (fgets(buf, sizeof(buf), f) == NULL);
874         fclose(f);
875         if (failed)
876                 return NULL;
877         memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
878         fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);
879         return &fh;
880 }
881