3 * Handle communication with knfsd internal cache
5 * We open /proc/net/rpc/{auth.unix.ip,nfsd.export,nfsd.fh}/channel
6 * and listen for requests (using my_svc_run)
14 #include <sys/types.h>
15 #include <sys/select.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
34 #include "pseudoflavors.h"
37 #include "blkid/blkid.h"
53 * Support routines for text-based upcalls.
54 * Fields are separated by spaces.
55 * Fields are either mangled to quote space tab newline slosh with slosh
56 * or a hexified with a leading \x
57 * Record is terminated with newline.
60 int cache_export_ent(char *domain, struct exportent *exp, char *p);
65 extern int use_ipaddr;
67 void auth_unix_ip(FILE *f)
71 * Ignore if class != "nfsd"
72 * Otherwise find domainname and write back:
74 * "nfsd" IP-ADDR expiry domainname
81 struct hostent *he = NULL;
82 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
85 xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
89 if (qword_get(&cp, class, 20) <= 0 ||
90 strcmp(class, "nfsd") != 0)
93 if (qword_get(&cp, ipaddr, 20) <= 0)
96 if (inet_aton(ipaddr, &addr)==0)
101 /* addr is a valid, interesting address, find the domain name... */
103 he = client_resolve(addr);
104 client = client_compose(he);
107 qword_print(f, "nfsd");
108 qword_print(f, ipaddr);
109 qword_printint(f, time(0)+30*60);
111 qword_print(f, ipaddr);
113 qword_print(f, *client?client:"DEFAULT");
115 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
117 if (client) free(client);
121 void auth_unix_gid(FILE *f)
126 * uid expiry count list of group ids
130 gid_t glist[100], *groups = glist;
135 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
139 if (qword_get_int(&cp, &uid) != 0)
146 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
147 if (rv == -1 && ngroups >= 100) {
148 groups = malloc(sizeof(gid_t)*ngroups);
152 rv = getgrouplist(pw->pw_name, pw->pw_gid,
156 qword_printint(f, uid);
157 qword_printint(f, time(0)+30*60);
159 qword_printint(f, ngroups);
160 for (i=0; i<ngroups; i++)
161 qword_printint(f, groups[i]);
163 qword_printint(f, 0);
171 static const char *get_uuid_blkdev(char *path)
173 static blkid_cache cache = NULL;
176 blkid_tag_iterate iter;
179 const char *val = NULL;
182 blkid_get_cache(&cache, NULL);
184 if (stat(path, &stb) != 0)
186 devname = blkid_devno_to_devname(stb.st_dev);
189 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
193 iter = blkid_tag_iterate_begin(dev);
196 while (blkid_tag_next(iter, &type, &val) == 0)
197 if (strcmp(type, "UUID") == 0)
199 blkid_tag_iterate_end(iter);
203 #define get_uuid_blkdev(path) (NULL)
206 int get_uuid(char *path, char *uuid, int uuidlen, char *u)
208 /* extract hex digits from uuidstr and compose a uuid
209 * of the given length (max 16), xoring bytes to make
210 * a smaller uuid. Then compare with uuid
213 const char *val = NULL;
217 val = get_uuid_blkdev(path);
224 memset(u, 0, uuidlen);
225 for ( ; *val ; val++) {
246 /* Iterate through /etc/mtab, finding mountpoints
247 * at or below a given path
249 static char *next_mnt(void **v, char *p)
255 f = setmntent("/etc/mtab", "r");
259 while ((me = getmntent(f)) != NULL &&
260 (strncmp(me->mnt_dir, p, l) != 0 ||
261 me->mnt_dir[l] != '/'))
271 void nfsd_fh(FILE *f)
274 * domain fsidtype fsid
275 * interpret fsid, find export point and options, and write:
276 * domain fsidtype fsid expiry path
282 unsigned int dev, major=0, minor=0;
283 unsigned int inode=0;
284 unsigned long long inode64;
285 unsigned int fsidnum=0;
287 struct exportent *found = NULL;
288 struct hostent *he = NULL;
290 char *found_path = NULL;
297 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
300 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
304 dom = malloc(strlen(cp));
307 if (qword_get(&cp, dom, strlen(cp)) <= 0)
309 if (qword_get_int(&cp, &fsidtype) != 0)
311 if (fsidtype < 0 || fsidtype > 7)
312 goto out; /* unknown type */
313 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
316 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
319 memcpy(&dev, fsid, 4);
320 memcpy(&inode, fsid+4, 4);
321 major = ntohl(dev)>>16;
322 minor = ntohl(dev) & 0xFFFF;
325 case FSID_NUM: /* 4 bytes - fsid */
328 memcpy(&fsidnum, fsid, 4);
331 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
332 * This format is never actually used but was
333 * an historical accident
337 memcpy(&dev, fsid, 4); major = ntohl(dev);
338 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
339 memcpy(&inode, fsid+8, 4);
342 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
343 /* This is *host* endian, not net-byte-order, because
344 * no-one outside this host has any business interpreting it
348 memcpy(&dev, fsid, 4);
349 memcpy(&inode, fsid+4, 4);
350 major = (dev & 0xfff00) >> 8;
351 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
354 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
357 memcpy(&inode, fsid, 4);
361 case FSID_UUID8: /* 8 byte uuid */
367 case FSID_UUID16: /* 16 byte uuid */
373 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
376 memcpy(&inode64, fsid, 8);
385 /* Now determine export point for this fsid/domain */
386 for (i=0 ; i < MCL_MAXTYPES; i++) {
387 nfs_export *next_exp;
388 for (exp = exportlist[i]; exp; exp = next_exp) {
393 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
394 static nfs_export *prev = NULL;
395 static void *mnt = NULL;
399 path = next_mnt(&mnt, exp->m_export.e_path);
401 next_exp = exp->m_next;
409 path = exp->m_export.e_path;
413 path = exp->m_export.e_path;
414 next_exp = exp->m_next;
417 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
419 if (exp->m_export.e_mountpoint &&
420 !is_mountpoint(exp->m_export.e_mountpoint[0]?
421 exp->m_export.e_mountpoint:
422 exp->m_export.e_path))
424 if (stat(path, &stb) != 0)
426 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
431 case FSID_MAJOR_MINOR:
432 case FSID_ENCODE_DEV:
433 if (stb.st_ino != inode)
435 if (major != major(stb.st_dev) ||
436 minor != minor(stb.st_dev))
440 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
441 exp->m_export.e_fsid != fsidnum))
444 case FSID_UUID4_INUM:
445 case FSID_UUID16_INUM:
446 if (stb.st_ino != inode)
451 if (!is_mountpoint(path))
454 if (exp->m_export.e_uuid)
455 get_uuid(NULL, exp->m_export.e_uuid,
457 else if (get_uuid(path, NULL, uuidlen, u) == 0)
460 if (memcmp(u, fhuuid, uuidlen) != 0)
466 if (!inet_aton(dom, &addr))
468 he = client_resolve(addr);
470 if (!client_check(exp->m_client, he))
473 /* It's a match !! */
475 found = &exp->m_export;
476 found_path = strdup(path);
477 if (found_path == NULL)
479 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
481 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
482 found_path, path, dom);
487 found->e_mountpoint &&
488 !is_mountpoint(found->e_mountpoint[0]?
491 /* Cannot export this yet
492 * should log a warning, but need to rate limit
493 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
494 found->e_path, found->e_mountpoint);
496 /* FIXME we need to make sure we re-visit this later */
499 if (!found && dev_missing) {
500 /* The missing dev could be what we want, so just be
501 * quite rather than returning stale yet
507 if (cache_export_ent(dom, found, found_path) < 0)
511 qword_printint(f, fsidtype);
512 qword_printhex(f, fsid, fsidlen);
513 /* The fsid -> path lookup can be quite expensive as it
514 * potentially stats and reads lots of devices, and some of those
515 * might have spun-down. The Answer is not likely to
516 * change underneath us, and an 'exportfs -f' can always
517 * remove this from the kernel, so use a really log
518 * timeout. Maybe this should be configurable on the command
521 qword_printint(f, 0x7fffffff);
523 qword_print(f, found_path);
531 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
535 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
537 struct servers *servers;
539 if (ep->e_fslocmethod == FSLOC_NONE)
542 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
545 qword_print(f, "fsloc");
546 qword_printint(f, servers->h_num);
547 if (servers->h_num >= 0) {
549 for (i=0; i<servers->h_num; i++) {
550 qword_print(f, servers->h_mp[i]->h_host);
551 qword_print(f, servers->h_mp[i]->h_path);
554 qword_printint(f, servers->h_referral);
555 release_replicas(servers);
558 static void write_secinfo(FILE *f, struct exportent *ep)
562 for (p = ep->e_secinfo; p->flav; p++)
564 if (p == ep->e_secinfo) {
565 /* There was no sec= option */
568 qword_print(f, "secinfo");
569 qword_printint(f, p - ep->e_secinfo);
570 for (p = ep->e_secinfo; p->flav; p++) {
571 qword_printint(f, p->flav->fnum);
572 qword_printint(f, p->flags);
577 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
579 qword_print(f, domain);
580 qword_print(f, path);
581 qword_printint(f, time(0)+30*60);
583 int different_fs = strcmp(path, exp->e_path) != 0;
586 qword_printint(f, exp->e_flags & ~NFSEXP_FSID);
588 qword_printint(f, exp->e_flags);
589 qword_printint(f, exp->e_anonuid);
590 qword_printint(f, exp->e_anongid);
591 qword_printint(f, exp->e_fsid);
592 write_fsloc(f, exp, path);
593 write_secinfo(f, exp);
594 if (exp->e_uuid == NULL || different_fs) {
596 if (get_uuid(path, NULL, 16, u)) {
597 qword_print(f, "uuid");
598 qword_printhex(f, u, 16);
602 get_uuid(NULL, exp->e_uuid, 16, u);
603 qword_print(f, "uuid");
604 qword_printhex(f, u, 16);
610 void nfsd_export(FILE *f)
614 * determine export options and return:
615 * domain path expiry flags anonuid anongid fsid
621 nfs_export *exp, *found = NULL;
624 struct hostent *he = NULL;
627 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
630 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
633 dom = malloc(strlen(cp));
634 path = malloc(strlen(cp));
639 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
641 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
646 /* now find flags for this export point in this domain */
647 for (i=0 ; i < MCL_MAXTYPES; i++) {
648 for (exp = exportlist[i]; exp; exp = exp->m_next) {
649 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
651 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
652 /* if path is a mountpoint below e_path, then OK */
653 int l = strlen(exp->m_export.e_path);
654 if (strcmp(path, exp->m_export.e_path) == 0 ||
655 (strncmp(path, exp->m_export.e_path, l) == 0 &&
657 is_mountpoint(path)))
661 } else if (strcmp(path, exp->m_export.e_path) != 0)
665 if (!inet_aton(dom, &addr))
667 he = client_resolve(addr);
669 if (!client_check(exp->m_client, he))
677 /* If one is a CROSSMOUNT, then prefer the longest path */
678 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
679 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
680 strlen(found->m_export.e_path) !=
681 strlen(exp->m_export.e_path)) {
683 if (strlen(exp->m_export.e_path) >
684 strlen(found->m_export.e_path)) {
690 } else if (found_type == i && found->m_warned == 0) {
691 xlog(L_WARNING, "%s exported to both %s and %s, "
692 "arbitrarily choosing options from first",
693 path, found->m_client->m_hostname, exp->m_client->m_hostname,
701 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
703 "Cannot export %s, possibly unsupported filesystem"
704 " or fsid= required", path);
705 dump_to_cache(f, dom, path, NULL);
708 dump_to_cache(f, dom, path, NULL);
711 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
713 if (path) free(path);
720 void (*cache_handle)(FILE *f);
723 { "auth.unix.ip", auth_unix_ip},
724 { "auth.unix.gid", auth_unix_gid},
725 { "nfsd.export", nfsd_export},
726 { "nfsd.fh", nfsd_fh},
730 extern int manage_gids;
731 void cache_open(void)
734 for (i=0; cachelist[i].cache_name; i++ ) {
736 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
738 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
739 cachelist[i].f = fopen(path, "r+");
743 void cache_set_fds(fd_set *fdset)
746 for (i=0; cachelist[i].cache_name; i++) {
748 FD_SET(fileno(cachelist[i].f), fdset);
752 int cache_process_req(fd_set *readfds)
756 for (i=0; cachelist[i].cache_name; i++) {
757 if (cachelist[i].f != NULL &&
758 FD_ISSET(fileno(cachelist[i].f), readfds)) {
760 cachelist[i].cache_handle(cachelist[i].f);
761 FD_CLR(fileno(cachelist[i].f), readfds);
769 * Give IP->domain and domain+path->options to kernel
770 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
771 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
774 int cache_export_ent(char *domain, struct exportent *exp, char *path)
777 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
781 err = dump_to_cache(f, domain, exp->e_path, exp);
784 "Cannot export %s, possibly unsupported filesystem or"
785 " fsid= required", exp->e_path);
788 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
789 /* really an 'if', but we can break out of
790 * a 'while' more easily */
791 /* Look along 'path' for other filesystems
792 * and export them with the same options
795 int l = strlen(exp->e_path);
798 if (strlen(path) <= l || path[l] != '/' ||
799 strncmp(exp->e_path, path, l) != 0)
801 if (stat(exp->e_path, &stb) != 0)
804 while(path[l] == '/') {
806 /* errors for submount should fail whole filesystem */
810 while (path[l] != '/' && path[l])
814 err2 = lstat(path, &stb);
818 if (stb.st_dev == dev)
822 dump_to_cache(f, domain, path, exp);
832 int cache_export(nfs_export *exp, char *path)
837 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
841 qword_print(f, "nfsd");
842 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
843 qword_printint(f, time(0)+30*60);
844 qword_print(f, exp->m_client->m_hostname);
849 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
856 * echo $domain $path $length
857 * read filehandle <&0
858 * } <> /proc/fs/nfsd/filehandle
861 cache_get_filehandle(nfs_export *exp, int len, char *p)
863 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
867 static struct nfs_fh_len fh;
870 f = fopen("/proc/fs/nfs/filehandle", "r+");
874 qword_print(f, exp->m_client->m_hostname);
876 qword_printint(f, len);
877 failed = qword_eol(f);
880 failed = (fgets(buf, sizeof(buf), f) == NULL);
884 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
885 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);