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);
65 void auth_unix_ip(FILE *f)
69 * Ignore if class != "nfsd"
70 * Otherwise find domainname and write back:
72 * "nfsd" IP-ADDR expiry domainname
79 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
84 if (qword_get(&cp, class, 20) <= 0 ||
85 strcmp(class, "nfsd") != 0)
88 if (qword_get(&cp, ipaddr, 20) <= 0)
91 if (inet_aton(ipaddr, &addr)==0)
96 /* addr is a valid, interesting address, find the domain name... */
97 client = client_compose(addr);
100 qword_print(f, "nfsd");
101 qword_print(f, ipaddr);
102 qword_printint(f, time(0)+30*60);
104 qword_print(f, *client?client:"DEFAULT");
107 if (client) free(client);
111 void auth_unix_gid(FILE *f)
116 * uid expiry count list of group ids
120 gid_t glist[100], *groups = glist;
125 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
129 if (qword_get_int(&cp, &uid) != 0)
136 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
137 if (rv == -1 && ngroups >= 100) {
138 groups = malloc(sizeof(gid_t)*ngroups);
142 rv = getgrouplist(pw->pw_name, pw->pw_gid,
146 qword_printint(f, uid);
147 qword_printint(f, time(0)+30*60);
149 qword_printint(f, ngroups);
150 for (i=0; i<ngroups; i++)
151 qword_printint(f, groups[i]);
159 int get_uuid(char *path, char *uuid, int uuidlen, char *u)
161 /* extract hex digits from uuidstr and compose a uuid
162 * of the given length (max 16), xoring bytes to make
163 * a smaller uuid. Then compare with uuid
169 static blkid_cache cache = NULL;
172 blkid_tag_iterate iter;
176 blkid_get_cache(&cache, NULL);
178 blkid_probe_all_new(cache);
180 if (stat(path, &stb) != 0)
182 devname = blkid_devno_to_devname(stb.st_dev);
185 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
189 iter = blkid_tag_iterate_begin(dev);
192 while (blkid_tag_next(iter, &type, &val) == 0)
193 if (strcmp(type, "UUID") == 0)
195 blkid_tag_iterate_end(iter);
202 memset(u, 0, uuidlen);
203 for ( ; *val ; val++) {
225 /* Iterate through /etc/mtab, finding mountpoints
226 * at or below a given path
228 static char *next_mnt(void **v, char *p)
234 f = setmntent("/etc/mtab", "r");
238 while ((me = getmntent(f)) != NULL &&
239 (strncmp(me->mnt_dir, p, l) != 0 ||
240 me->mnt_dir[l] != '/'))
250 void nfsd_fh(FILE *f)
253 * domain fsidtype fsid
254 * interpret fsid, find export point and options, and write:
255 * domain fsidtype fsid expiry path
261 unsigned int dev, major=0, minor=0;
262 unsigned int inode=0;
263 unsigned long long inode64;
264 unsigned int fsidnum=0;
266 struct exportent *found = NULL;
267 char *found_path = NULL;
274 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
279 dom = malloc(strlen(cp));
282 if (qword_get(&cp, dom, strlen(cp)) <= 0)
284 if (qword_get_int(&cp, &fsidtype) != 0)
286 if (fsidtype < 0 || fsidtype > 7)
287 goto out; /* unknown type */
288 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
291 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
294 memcpy(&dev, fsid, 4);
295 memcpy(&inode, fsid+4, 4);
296 major = ntohl(dev)>>16;
297 minor = ntohl(dev) & 0xFFFF;
300 case FSID_NUM: /* 4 bytes - fsid */
303 memcpy(&fsidnum, fsid, 4);
306 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
307 * This format is never actually used but was
308 * an historical accident
312 memcpy(&dev, fsid, 4); major = ntohl(dev);
313 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
314 memcpy(&inode, fsid+8, 4);
317 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
318 /* This is *host* endian, not net-byte-order, because
319 * no-one outside this host has any business interpreting it
323 memcpy(&dev, fsid, 4);
324 memcpy(&inode, fsid+4, 4);
325 major = (dev & 0xfff00) >> 8;
326 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
329 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
332 memcpy(&inode, fsid, 4);
336 case FSID_UUID8: /* 8 byte uuid */
342 case FSID_UUID16: /* 16 byte uuid */
348 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
351 memcpy(&inode64, fsid, 8);
360 /* Now determine export point for this fsid/domain */
361 for (i=0 ; i < MCL_MAXTYPES; i++) {
362 nfs_export *next_exp;
363 for (exp = exportlist[i]; exp; exp = next_exp) {
368 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
369 static nfs_export *prev = NULL;
370 static void *mnt = NULL;
374 path = next_mnt(&mnt, exp->m_export.e_path);
376 next_exp = exp->m_next;
384 path = exp->m_export.e_path;
388 path = exp->m_export.e_path;
389 next_exp = exp->m_next;
392 if (!client_member(dom, exp->m_client->m_hostname))
394 if (exp->m_export.e_mountpoint &&
395 !is_mountpoint(exp->m_export.e_mountpoint[0]?
396 exp->m_export.e_mountpoint:
397 exp->m_export.e_path))
399 if (stat(path, &stb) != 0)
401 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
406 case FSID_MAJOR_MINOR:
407 case FSID_ENCODE_DEV:
408 if (stb.st_ino != inode)
410 if (major != major(stb.st_dev) ||
411 minor != minor(stb.st_dev))
415 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
416 exp->m_export.e_fsid != fsidnum))
419 case FSID_UUID4_INUM:
420 case FSID_UUID16_INUM:
421 if (stb.st_ino != inode)
426 if (!is_mountpoint(path))
430 if (exp->m_export.e_uuid)
431 get_uuid(NULL, exp->m_export.e_uuid,
433 else if (get_uuid(path, NULL,
437 if (memcmp(u, fhuuid, uuidlen) != 0)
444 /* It's a match !! */
446 found = &exp->m_export;
447 found_path = strdup(path);
448 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
450 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
451 found_path, path, dom);
456 found->e_mountpoint &&
457 !is_mountpoint(found->e_mountpoint[0]?
460 /* Cannot export this yet
461 * should log a warning, but need to rate limit
462 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
463 found->e_path, found->e_mountpoint);
465 /* FIXME we need to make sure we re-visit this later */
468 if (!found && dev_missing) {
469 /* The missing dev could be what we want, so just be
470 * quite rather than returning stale yet
476 if (cache_export_ent(dom, found, found_path) < 0)
480 qword_printint(f, fsidtype);
481 qword_printhex(f, fsid, fsidlen);
482 /* The fsid -> path lookup can be quite expensive as it
483 * potentially stats and reads lots of devices, and some of those
484 * might have spun-down. The Answer is not likely to
485 * change underneath us, and an 'exportfs -f' can always
486 * remove this from the kernel, so use a really log
487 * timeout. Maybe this should be configurable on the command
490 qword_printint(f, 0x7fffffff);
492 qword_print(f, found->e_path);
499 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
501 struct servers *servers;
503 if (ep->e_fslocmethod == FSLOC_NONE)
506 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
509 qword_print(f, "fsloc");
510 qword_printint(f, servers->h_num);
511 if (servers->h_num >= 0) {
513 for (i=0; i<servers->h_num; i++) {
514 qword_print(f, servers->h_mp[i]->h_host);
515 qword_print(f, servers->h_mp[i]->h_path);
518 qword_printint(f, servers->h_referral);
519 release_replicas(servers);
522 static void write_secinfo(FILE *f, struct exportent *ep)
526 for (p = ep->e_secinfo; p->flav; p++)
528 if (p == ep->e_secinfo) {
529 /* There was no sec= option */
532 qword_print(f, "secinfo");
533 qword_printint(f, p - ep->e_secinfo);
534 for (p = ep->e_secinfo; p->flav; p++) {
535 qword_printint(f, p->flav->fnum);
536 qword_printint(f, p->flags);
541 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
543 qword_print(f, domain);
544 qword_print(f, path);
545 qword_printint(f, time(0)+30*60);
547 qword_printint(f, exp->e_flags);
548 qword_printint(f, exp->e_anonuid);
549 qword_printint(f, exp->e_anongid);
550 qword_printint(f, exp->e_fsid);
551 write_fsloc(f, exp, path);
552 write_secinfo(f, exp);
554 if (exp->e_uuid == NULL) {
556 if (get_uuid(path, NULL, 16, u)) {
557 qword_print(f, "uuid");
558 qword_printhex(f, u, 16);
560 } else if (exp->e_uuid) {
561 qword_print(f, "uuid");
562 qword_printhex(f, exp->e_uuid, 16);
569 void nfsd_export(FILE *f)
573 * determine export options and return:
574 * domain path expiry flags anonuid anongid fsid
580 nfs_export *exp, *found = NULL;
584 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
588 dom = malloc(strlen(cp));
589 path = malloc(strlen(cp));
594 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
596 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
601 /* now find flags for this export point in this domain */
602 for (i=0 ; i < MCL_MAXTYPES; i++) {
603 for (exp = exportlist[i]; exp; exp = exp->m_next) {
604 if (!client_member(dom, exp->m_client->m_hostname))
606 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
607 /* if path is a mountpoint below e_path, then OK */
608 int l = strlen(exp->m_export.e_path);
609 if (strcmp(path, exp->m_export.e_path) == 0 ||
610 (strncmp(path, exp->m_export.e_path, l) == 0 &&
612 is_mountpoint(path)))
616 } else if (strcmp(path, exp->m_export.e_path) != 0)
623 /* If one is a CROSSMOUNT, then prefer the longest path */
624 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
625 (found->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
626 strlen(found->m_export.e_path) !=
627 strlen(found->m_export.e_path)) {
629 if (strlen(exp->m_export.e_path) >
630 strlen(found->m_export.e_path)) {
636 } else if (found_type == i && found->m_warned == 0) {
637 xlog(L_WARNING, "%s exported to both %s and %s, "
638 "arbitrarily choosing options from first",
639 path, found->m_client->m_hostname, exp->m_client->m_hostname,
647 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
649 "Cannot export %s, possibly unsupported filesystem"
650 " or fsid= required", path);
651 dump_to_cache(f, dom, path, NULL);
653 mountlist_add(dom, path);
655 dump_to_cache(f, dom, path, NULL);
659 if (path) free(path);
665 void (*cache_handle)(FILE *f);
668 { "auth.unix.ip", auth_unix_ip},
669 { "auth.unix.gid", auth_unix_gid},
670 { "nfsd.export", nfsd_export},
671 { "nfsd.fh", nfsd_fh},
675 extern int manage_gids;
676 void cache_open(void)
679 for (i=0; cachelist[i].cache_name; i++ ) {
681 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
683 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
684 cachelist[i].f = fopen(path, "r+");
688 void cache_set_fds(fd_set *fdset)
691 for (i=0; cachelist[i].cache_name; i++) {
693 FD_SET(fileno(cachelist[i].f), fdset);
697 int cache_process_req(fd_set *readfds)
701 for (i=0; cachelist[i].cache_name; i++) {
702 if (cachelist[i].f != NULL &&
703 FD_ISSET(fileno(cachelist[i].f), readfds)) {
705 cachelist[i].cache_handle(cachelist[i].f);
706 FD_CLR(fileno(cachelist[i].f), readfds);
714 * Give IP->domain and domain+path->options to kernel
715 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
716 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
719 int cache_export_ent(char *domain, struct exportent *exp, char *path)
722 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
726 err = dump_to_cache(f, domain, exp->e_path, exp);
729 "Cannot export %s, possibly unsupported filesystem or"
730 " fsid= required", exp->e_path);
732 mountlist_add(domain, exp->e_path);
734 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
735 /* really an 'if', but we can break out of
736 * a 'while' more easily */
737 /* Look along 'path' for other filesystems
738 * and export them with the same options
741 int l = strlen(exp->e_path);
744 if (strlen(path) <= l || path[l] != '/' ||
745 strncmp(exp->e_path, path, l) != 0)
747 if (stat(exp->e_path, &stb) != 0)
750 while(path[l] == '/') {
752 /* errors for submount should fail whole filesystem */
756 while (path[l] != '/' && path[l])
760 err2 = lstat(path, &stb);
764 if (stb.st_dev == dev)
768 dump_to_cache(f, domain, path, exp);
778 int cache_export(nfs_export *exp, char *path)
783 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
787 qword_print(f, "nfsd");
788 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
789 qword_printint(f, time(0)+30*60);
790 qword_print(f, exp->m_client->m_hostname);
795 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
802 * echo $domain $path $length
803 * read filehandle <&0
804 * } <> /proc/fs/nfsd/filehandle
807 cache_get_filehandle(nfs_export *exp, int len, char *p)
809 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
813 static struct nfs_fh_len fh;
816 f = fopen("/proc/fs/nfs/filehandle", "r+");
820 qword_print(f, exp->m_client->m_hostname);
822 qword_printint(f, len);
823 failed = qword_eol(f);
826 failed = (fgets(buf, sizeof(buf), f) == NULL);
830 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
831 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);