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>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
33 #include "pseudoflavors.h"
36 #include "blkid/blkid.h"
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.
59 int cache_export_ent(char *domain, struct exportent *exp, char *p);
64 extern int use_ipaddr;
66 void auth_unix_ip(FILE *f)
70 * Ignore if class != "nfsd"
71 * Otherwise find domainname and write back:
73 * "nfsd" IP-ADDR expiry domainname
80 struct hostent *he = NULL;
81 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
84 xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
88 if (qword_get(&cp, class, 20) <= 0 ||
89 strcmp(class, "nfsd") != 0)
92 if (qword_get(&cp, ipaddr, 20) <= 0)
95 if (inet_aton(ipaddr, &addr)==0)
100 /* addr is a valid, interesting address, find the domain name... */
102 he = client_resolve(addr);
103 client = client_compose(he);
106 qword_print(f, "nfsd");
107 qword_print(f, ipaddr);
108 qword_printint(f, time(0)+30*60);
110 qword_print(f, ipaddr);
112 qword_print(f, *client?client:"DEFAULT");
114 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
116 if (client) free(client);
120 void auth_unix_gid(FILE *f)
125 * uid expiry count list of group ids
129 gid_t glist[100], *groups = glist;
134 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
138 if (qword_get_int(&cp, &uid) != 0)
145 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
146 if (rv == -1 && ngroups >= 100) {
147 groups = malloc(sizeof(gid_t)*ngroups);
151 rv = getgrouplist(pw->pw_name, pw->pw_gid,
155 qword_printint(f, uid);
156 qword_printint(f, time(0)+30*60);
158 qword_printint(f, ngroups);
159 for (i=0; i<ngroups; i++)
160 qword_printint(f, groups[i]);
168 int get_uuid(char *path, char *uuid, int uuidlen, char *u)
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
178 static blkid_cache cache = NULL;
181 blkid_tag_iterate iter;
185 blkid_get_cache(&cache, NULL);
187 blkid_probe_all_new(cache);
189 if (stat(path, &stb) != 0)
191 devname = blkid_devno_to_devname(stb.st_dev);
194 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
198 iter = blkid_tag_iterate_begin(dev);
201 while (blkid_tag_next(iter, &type, &val) == 0)
202 if (strcmp(type, "UUID") == 0)
204 blkid_tag_iterate_end(iter);
211 memset(u, 0, uuidlen);
212 for ( ; *val ; val++) {
234 /* Iterate through /etc/mtab, finding mountpoints
235 * at or below a given path
237 static char *next_mnt(void **v, char *p)
243 f = setmntent("/etc/mtab", "r");
247 while ((me = getmntent(f)) != NULL &&
248 (strncmp(me->mnt_dir, p, l) != 0 ||
249 me->mnt_dir[l] != '/'))
259 void nfsd_fh(FILE *f)
262 * domain fsidtype fsid
263 * interpret fsid, find export point and options, and write:
264 * domain fsidtype fsid expiry path
270 unsigned int dev, major=0, minor=0;
271 unsigned int inode=0;
272 unsigned long long inode64;
273 unsigned int fsidnum=0;
275 struct exportent *found = NULL;
276 struct hostent *he = NULL;
278 char *found_path = NULL;
285 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
288 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
292 dom = malloc(strlen(cp));
295 if (qword_get(&cp, dom, strlen(cp)) <= 0)
297 if (qword_get_int(&cp, &fsidtype) != 0)
299 if (fsidtype < 0 || fsidtype > 7)
300 goto out; /* unknown type */
301 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
304 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
307 memcpy(&dev, fsid, 4);
308 memcpy(&inode, fsid+4, 4);
309 major = ntohl(dev)>>16;
310 minor = ntohl(dev) & 0xFFFF;
313 case FSID_NUM: /* 4 bytes - fsid */
316 memcpy(&fsidnum, fsid, 4);
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
325 memcpy(&dev, fsid, 4); major = ntohl(dev);
326 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
327 memcpy(&inode, fsid+8, 4);
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
336 memcpy(&dev, fsid, 4);
337 memcpy(&inode, fsid+4, 4);
338 major = (dev & 0xfff00) >> 8;
339 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
342 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
345 memcpy(&inode, fsid, 4);
349 case FSID_UUID8: /* 8 byte uuid */
355 case FSID_UUID16: /* 16 byte uuid */
361 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
364 memcpy(&inode64, fsid, 8);
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) {
381 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
382 static nfs_export *prev = NULL;
383 static void *mnt = NULL;
387 path = next_mnt(&mnt, exp->m_export.e_path);
389 next_exp = exp->m_next;
397 path = exp->m_export.e_path;
401 path = exp->m_export.e_path;
402 next_exp = exp->m_next;
405 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
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))
412 if (stat(path, &stb) != 0)
414 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
419 case FSID_MAJOR_MINOR:
420 case FSID_ENCODE_DEV:
421 if (stb.st_ino != inode)
423 if (major != major(stb.st_dev) ||
424 minor != minor(stb.st_dev))
428 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
429 exp->m_export.e_fsid != fsidnum))
432 case FSID_UUID4_INUM:
433 case FSID_UUID16_INUM:
434 if (stb.st_ino != inode)
439 if (!is_mountpoint(path))
443 if (exp->m_export.e_uuid)
444 get_uuid(NULL, exp->m_export.e_uuid,
446 else if (get_uuid(path, NULL,
450 if (memcmp(u, fhuuid, uuidlen) != 0)
459 if (!inet_aton(dom, &addr))
461 he = client_resolve(addr);
463 if (!client_check(exp->m_client, he))
466 /* It's a match !! */
468 found = &exp->m_export;
469 found_path = strdup(path);
470 if (found_path == NULL)
472 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
474 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
475 found_path, path, dom);
480 found->e_mountpoint &&
481 !is_mountpoint(found->e_mountpoint[0]?
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);
489 /* FIXME we need to make sure we re-visit this later */
492 if (!found && dev_missing) {
493 /* The missing dev could be what we want, so just be
494 * quite rather than returning stale yet
500 if (cache_export_ent(dom, found, found_path) < 0)
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
514 qword_printint(f, 0x7fffffff);
516 qword_print(f, found_path);
524 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
528 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
530 struct servers *servers;
532 if (ep->e_fslocmethod == FSLOC_NONE)
535 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
538 qword_print(f, "fsloc");
539 qword_printint(f, servers->h_num);
540 if (servers->h_num >= 0) {
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);
547 qword_printint(f, servers->h_referral);
548 release_replicas(servers);
551 static void write_secinfo(FILE *f, struct exportent *ep)
555 for (p = ep->e_secinfo; p->flav; p++)
557 if (p == ep->e_secinfo) {
558 /* There was no sec= option */
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);
570 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
572 qword_print(f, domain);
573 qword_print(f, path);
574 qword_printint(f, time(0)+30*60);
576 int different_fs = strcmp(path, exp->e_path) != 0;
579 qword_printint(f, exp->e_flags & ~NFSEXP_FSID);
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);
588 if (exp->e_uuid == NULL || different_fs) {
590 if (get_uuid(path, NULL, 16, u)) {
591 qword_print(f, "uuid");
592 qword_printhex(f, u, 16);
595 qword_print(f, "uuid");
596 qword_printhex(f, exp->e_uuid, 16);
603 void nfsd_export(FILE *f)
607 * determine export options and return:
608 * domain path expiry flags anonuid anongid fsid
614 nfs_export *exp, *found = NULL;
617 struct hostent *he = NULL;
620 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
623 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
626 dom = malloc(strlen(cp));
627 path = malloc(strlen(cp));
632 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
634 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
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))
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 &&
650 is_mountpoint(path)))
654 } else if (strcmp(path, exp->m_export.e_path) != 0)
658 if (!inet_aton(dom, &addr))
660 he = client_resolve(addr);
662 if (!client_check(exp->m_client, he))
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)) {
676 if (strlen(exp->m_export.e_path) >
677 strlen(found->m_export.e_path)) {
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,
694 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
696 "Cannot export %s, possibly unsupported filesystem"
697 " or fsid= required", path);
698 dump_to_cache(f, dom, path, NULL);
701 dump_to_cache(f, dom, path, NULL);
704 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
706 if (path) free(path);
713 void (*cache_handle)(FILE *f);
716 { "auth.unix.ip", auth_unix_ip},
717 { "auth.unix.gid", auth_unix_gid},
718 { "nfsd.export", nfsd_export},
719 { "nfsd.fh", nfsd_fh},
723 extern int manage_gids;
724 void cache_open(void)
727 for (i=0; cachelist[i].cache_name; i++ ) {
729 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
731 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
732 cachelist[i].f = fopen(path, "r+");
736 void cache_set_fds(fd_set *fdset)
739 for (i=0; cachelist[i].cache_name; i++) {
741 FD_SET(fileno(cachelist[i].f), fdset);
745 int cache_process_req(fd_set *readfds)
749 for (i=0; cachelist[i].cache_name; i++) {
750 if (cachelist[i].f != NULL &&
751 FD_ISSET(fileno(cachelist[i].f), readfds)) {
753 cachelist[i].cache_handle(cachelist[i].f);
754 FD_CLR(fileno(cachelist[i].f), readfds);
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
767 int cache_export_ent(char *domain, struct exportent *exp, char *path)
770 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
774 err = dump_to_cache(f, domain, exp->e_path, exp);
777 "Cannot export %s, possibly unsupported filesystem or"
778 " fsid= required", exp->e_path);
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
788 int l = strlen(exp->e_path);
791 if (strlen(path) <= l || path[l] != '/' ||
792 strncmp(exp->e_path, path, l) != 0)
794 if (stat(exp->e_path, &stb) != 0)
797 while(path[l] == '/') {
799 /* errors for submount should fail whole filesystem */
803 while (path[l] != '/' && path[l])
807 err2 = lstat(path, &stb);
811 if (stb.st_dev == dev)
815 dump_to_cache(f, domain, path, exp);
825 int cache_export(nfs_export *exp, char *path)
830 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
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);
842 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
849 * echo $domain $path $length
850 * read filehandle <&0
851 * } <> /proc/fs/nfsd/filehandle
854 cache_get_filehandle(nfs_export *exp, int len, char *p)
856 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
860 static struct nfs_fh_len fh;
863 f = fopen("/proc/fs/nfs/filehandle", "r+");
867 qword_print(f, exp->m_client->m_hostname);
869 qword_printint(f, len);
870 failed = qword_eol(f);
873 failed = (fgets(buf, sizeof(buf), f) == NULL);
877 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
878 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);