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);
221 if (statfs64(path, &st))
223 if (!st.f_fsid.__val[0] && !st.f_fsid.__val[1])
225 snprintf(fsid_val, 17, "%08x%08x",
226 st.f_fsid.__val[0], st.f_fsid.__val[1]);
233 memset(u, 0, uuidlen);
234 for ( ; *val ; val++) {
255 /* Iterate through /etc/mtab, finding mountpoints
256 * at or below a given path
258 static char *next_mnt(void **v, char *p)
264 f = setmntent("/etc/mtab", "r");
268 while ((me = getmntent(f)) != NULL &&
269 (strncmp(me->mnt_dir, p, l) != 0 ||
270 me->mnt_dir[l] != '/'))
280 void nfsd_fh(FILE *f)
283 * domain fsidtype fsid
284 * interpret fsid, find export point and options, and write:
285 * domain fsidtype fsid expiry path
291 unsigned int dev, major=0, minor=0;
292 unsigned int inode=0;
293 unsigned long long inode64;
294 unsigned int fsidnum=0;
296 struct exportent *found = NULL;
297 struct hostent *he = NULL;
299 char *found_path = NULL;
306 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
309 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
313 dom = malloc(strlen(cp));
316 if (qword_get(&cp, dom, strlen(cp)) <= 0)
318 if (qword_get_int(&cp, &fsidtype) != 0)
320 if (fsidtype < 0 || fsidtype > 7)
321 goto out; /* unknown type */
322 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
325 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
328 memcpy(&dev, fsid, 4);
329 memcpy(&inode, fsid+4, 4);
330 major = ntohl(dev)>>16;
331 minor = ntohl(dev) & 0xFFFF;
334 case FSID_NUM: /* 4 bytes - fsid */
337 memcpy(&fsidnum, fsid, 4);
340 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
341 * This format is never actually used but was
342 * an historical accident
346 memcpy(&dev, fsid, 4); major = ntohl(dev);
347 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
348 memcpy(&inode, fsid+8, 4);
351 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
352 /* This is *host* endian, not net-byte-order, because
353 * no-one outside this host has any business interpreting it
357 memcpy(&dev, fsid, 4);
358 memcpy(&inode, fsid+4, 4);
359 major = (dev & 0xfff00) >> 8;
360 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
363 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
366 memcpy(&inode, fsid, 4);
370 case FSID_UUID8: /* 8 byte uuid */
376 case FSID_UUID16: /* 16 byte uuid */
382 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
385 memcpy(&inode64, fsid, 8);
394 /* Now determine export point for this fsid/domain */
395 for (i=0 ; i < MCL_MAXTYPES; i++) {
396 nfs_export *next_exp;
397 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
402 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
403 static nfs_export *prev = NULL;
404 static void *mnt = NULL;
408 path = next_mnt(&mnt, exp->m_export.e_path);
410 next_exp = exp->m_next;
418 path = exp->m_export.e_path;
422 path = exp->m_export.e_path;
423 next_exp = exp->m_next;
426 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
428 if (exp->m_export.e_mountpoint &&
429 !is_mountpoint(exp->m_export.e_mountpoint[0]?
430 exp->m_export.e_mountpoint:
431 exp->m_export.e_path))
433 if (stat(path, &stb) != 0)
435 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
440 case FSID_MAJOR_MINOR:
441 case FSID_ENCODE_DEV:
442 if (stb.st_ino != inode)
444 if (major != major(stb.st_dev) ||
445 minor != minor(stb.st_dev))
449 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
450 exp->m_export.e_fsid != fsidnum))
453 case FSID_UUID4_INUM:
454 case FSID_UUID16_INUM:
455 if (stb.st_ino != inode)
460 if (!is_mountpoint(path))
463 if (exp->m_export.e_uuid)
464 get_uuid(NULL, exp->m_export.e_uuid,
466 else if (get_uuid(path, NULL, uuidlen, u) == 0)
469 if (memcmp(u, fhuuid, uuidlen) != 0)
475 if (!inet_aton(dom, &addr))
477 he = client_resolve(addr);
479 if (!client_check(exp->m_client, he))
482 /* It's a match !! */
484 found = &exp->m_export;
485 found_path = strdup(path);
486 if (found_path == NULL)
488 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
490 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
491 found_path, path, dom);
496 found->e_mountpoint &&
497 !is_mountpoint(found->e_mountpoint[0]?
500 /* Cannot export this yet
501 * should log a warning, but need to rate limit
502 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
503 found->e_path, found->e_mountpoint);
505 /* FIXME we need to make sure we re-visit this later */
508 if (!found && dev_missing) {
509 /* The missing dev could be what we want, so just be
510 * quite rather than returning stale yet
516 if (cache_export_ent(dom, found, found_path) < 0)
520 qword_printint(f, fsidtype);
521 qword_printhex(f, fsid, fsidlen);
522 /* The fsid -> path lookup can be quite expensive as it
523 * potentially stats and reads lots of devices, and some of those
524 * might have spun-down. The Answer is not likely to
525 * change underneath us, and an 'exportfs -f' can always
526 * remove this from the kernel, so use a really log
527 * timeout. Maybe this should be configurable on the command
530 qword_printint(f, 0x7fffffff);
532 qword_print(f, found_path);
540 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
544 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
546 struct servers *servers;
548 if (ep->e_fslocmethod == FSLOC_NONE)
551 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
554 qword_print(f, "fsloc");
555 qword_printint(f, servers->h_num);
556 if (servers->h_num >= 0) {
558 for (i=0; i<servers->h_num; i++) {
559 qword_print(f, servers->h_mp[i]->h_host);
560 qword_print(f, servers->h_mp[i]->h_path);
563 qword_printint(f, servers->h_referral);
564 release_replicas(servers);
567 static void write_secinfo(FILE *f, struct exportent *ep)
571 for (p = ep->e_secinfo; p->flav; p++)
573 if (p == ep->e_secinfo) {
574 /* There was no sec= option */
577 qword_print(f, "secinfo");
578 qword_printint(f, p - ep->e_secinfo);
579 for (p = ep->e_secinfo; p->flav; p++) {
580 qword_printint(f, p->flav->fnum);
581 qword_printint(f, p->flags);
586 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
588 qword_print(f, domain);
589 qword_print(f, path);
590 qword_printint(f, time(0)+30*60);
592 int different_fs = strcmp(path, exp->e_path) != 0;
595 qword_printint(f, exp->e_flags & ~NFSEXP_FSID);
597 qword_printint(f, exp->e_flags);
598 qword_printint(f, exp->e_anonuid);
599 qword_printint(f, exp->e_anongid);
600 qword_printint(f, exp->e_fsid);
601 write_fsloc(f, exp, path);
602 write_secinfo(f, exp);
603 if (exp->e_uuid == NULL || different_fs) {
605 if (get_uuid(path, NULL, 16, u)) {
606 qword_print(f, "uuid");
607 qword_printhex(f, u, 16);
611 get_uuid(NULL, exp->e_uuid, 16, u);
612 qword_print(f, "uuid");
613 qword_printhex(f, u, 16);
619 void nfsd_export(FILE *f)
623 * determine export options and return:
624 * domain path expiry flags anonuid anongid fsid
630 nfs_export *exp, *found = NULL;
633 struct hostent *he = NULL;
636 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
639 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
642 dom = malloc(strlen(cp));
643 path = malloc(strlen(cp));
648 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
650 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
655 /* now find flags for this export point in this domain */
656 for (i=0 ; i < MCL_MAXTYPES; i++) {
657 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
658 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
660 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
661 /* if path is a mountpoint below e_path, then OK */
662 int l = strlen(exp->m_export.e_path);
663 if (strcmp(path, exp->m_export.e_path) == 0 ||
664 (strncmp(path, exp->m_export.e_path, l) == 0 &&
666 is_mountpoint(path)))
670 } else if (strcmp(path, exp->m_export.e_path) != 0)
674 if (!inet_aton(dom, &addr))
676 he = client_resolve(addr);
678 if (!client_check(exp->m_client, he))
686 /* If one is a CROSSMOUNT, then prefer the longest path */
687 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
688 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
689 strlen(found->m_export.e_path) !=
690 strlen(exp->m_export.e_path)) {
692 if (strlen(exp->m_export.e_path) >
693 strlen(found->m_export.e_path)) {
699 } else if (found_type == i && found->m_warned == 0) {
700 xlog(L_WARNING, "%s exported to both %s and %s, "
701 "arbitrarily choosing options from first",
702 path, found->m_client->m_hostname, exp->m_client->m_hostname,
710 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
712 "Cannot export %s, possibly unsupported filesystem"
713 " or fsid= required", path);
714 dump_to_cache(f, dom, path, NULL);
717 dump_to_cache(f, dom, path, NULL);
720 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
722 if (path) free(path);
729 void (*cache_handle)(FILE *f);
732 { "auth.unix.ip", auth_unix_ip},
733 { "auth.unix.gid", auth_unix_gid},
734 { "nfsd.export", nfsd_export},
735 { "nfsd.fh", nfsd_fh},
739 extern int manage_gids;
740 void cache_open(void)
743 for (i=0; cachelist[i].cache_name; i++ ) {
745 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
747 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
748 cachelist[i].f = fopen(path, "r+");
752 void cache_set_fds(fd_set *fdset)
755 for (i=0; cachelist[i].cache_name; i++) {
757 FD_SET(fileno(cachelist[i].f), fdset);
761 int cache_process_req(fd_set *readfds)
765 for (i=0; cachelist[i].cache_name; i++) {
766 if (cachelist[i].f != NULL &&
767 FD_ISSET(fileno(cachelist[i].f), readfds)) {
769 cachelist[i].cache_handle(cachelist[i].f);
770 FD_CLR(fileno(cachelist[i].f), readfds);
778 * Give IP->domain and domain+path->options to kernel
779 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
780 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
783 int cache_export_ent(char *domain, struct exportent *exp, char *path)
786 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
790 err = dump_to_cache(f, domain, exp->e_path, exp);
793 "Cannot export %s, possibly unsupported filesystem or"
794 " fsid= required", exp->e_path);
797 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
798 /* really an 'if', but we can break out of
799 * a 'while' more easily */
800 /* Look along 'path' for other filesystems
801 * and export them with the same options
804 int l = strlen(exp->e_path);
807 if (strlen(path) <= l || path[l] != '/' ||
808 strncmp(exp->e_path, path, l) != 0)
810 if (stat(exp->e_path, &stb) != 0)
813 while(path[l] == '/') {
815 /* errors for submount should fail whole filesystem */
819 while (path[l] != '/' && path[l])
823 err2 = lstat(path, &stb);
827 if (stb.st_dev == dev)
831 dump_to_cache(f, domain, path, exp);
841 int cache_export(nfs_export *exp, char *path)
846 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
850 qword_print(f, "nfsd");
851 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
852 qword_printint(f, time(0)+30*60);
853 qword_print(f, exp->m_client->m_hostname);
858 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
865 * echo $domain $path $length
866 * read filehandle <&0
867 * } <> /proc/fs/nfsd/filehandle
870 cache_get_filehandle(nfs_export *exp, int len, char *p)
872 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
876 static struct nfs_fh_len fh;
879 f = fopen("/proc/fs/nfs/filehandle", "r+");
883 qword_print(f, exp->m_client->m_hostname);
885 qword_printint(f, len);
886 failed = qword_eol(f);
889 failed = (fgets(buf, sizeof(buf), f) == NULL);
893 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
894 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);