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_uint(&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_printuint(f, uid);
157 qword_printuint(f, time(0)+30*60);
159 qword_printuint(f, ngroups);
160 for (i=0; i<ngroups; i++)
161 qword_printuint(f, groups[i]);
163 qword_printuint(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, int flag_mask)
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 & flag_mask);
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;
593 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
595 qword_printint(f, exp->e_flags & flag_mask);
596 qword_printint(f, exp->e_anonuid);
597 qword_printint(f, exp->e_anongid);
598 qword_printint(f, exp->e_fsid);
599 write_fsloc(f, exp, path);
600 write_secinfo(f, exp, flag_mask);
601 if (exp->e_uuid == NULL || different_fs) {
603 if (get_uuid(path, NULL, 16, u)) {
604 qword_print(f, "uuid");
605 qword_printhex(f, u, 16);
609 get_uuid(NULL, exp->e_uuid, 16, u);
610 qword_print(f, "uuid");
611 qword_printhex(f, u, 16);
617 static int is_subdirectory(char *subpath, char *path)
619 int l = strlen(path);
621 return strcmp(subpath, path) == 0
622 || (strncmp(subpath, path, l) == 0 && path[l] == '/');
625 static int path_matches(nfs_export *exp, char *path)
627 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
628 return is_subdirectory(path, exp->m_export.e_path);
629 return strcmp(path, exp->m_export.e_path) == 0;
632 static int client_matches(nfs_export *exp, char *dom, struct hostent *he)
635 return client_check(exp->m_client, he);
636 return client_member(dom, exp->m_client->m_hostname);
639 static int export_matches(nfs_export *exp, char *dom, char *path, struct hostent *he)
641 return path_matches(exp, path) && client_matches(exp, dom, he);
644 static nfs_export *lookup_export(char *dom, char *path, struct hostent *he)
647 nfs_export *found = NULL;
651 for (i=0 ; i < MCL_MAXTYPES; i++) {
652 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
653 if (!export_matches(exp, dom, path, he))
661 /* Always prefer non-V4ROOT mounts */
662 if (found->m_export.e_flags & NFSEXP_V4ROOT)
665 /* If one is a CROSSMOUNT, then prefer the longest path */
666 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
667 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
668 strlen(found->m_export.e_path) !=
669 strlen(exp->m_export.e_path)) {
671 if (strlen(exp->m_export.e_path) >
672 strlen(found->m_export.e_path)) {
678 } else if (found_type == i && found->m_warned == 0) {
679 xlog(L_WARNING, "%s exported to both %s and %s, "
680 "arbitrarily choosing options from first",
681 path, found->m_client->m_hostname, exp->m_client->m_hostname,
690 void nfsd_export(FILE *f)
694 * determine export options and return:
695 * domain path expiry flags anonuid anongid fsid
700 nfs_export *found = NULL;
702 struct hostent *he = NULL;
705 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
708 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
711 dom = malloc(strlen(cp));
712 path = malloc(strlen(cp));
717 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
719 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
725 if (!inet_aton(dom, &addr))
727 he = client_resolve(addr);
730 found = lookup_export(dom, path, he);
733 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
735 "Cannot export %s, possibly unsupported filesystem"
736 " or fsid= required", path);
737 dump_to_cache(f, dom, path, NULL);
740 dump_to_cache(f, dom, path, NULL);
743 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
745 if (path) free(path);
752 void (*cache_handle)(FILE *f);
755 { "auth.unix.ip", auth_unix_ip},
756 { "auth.unix.gid", auth_unix_gid},
757 { "nfsd.export", nfsd_export},
758 { "nfsd.fh", nfsd_fh},
762 extern int manage_gids;
763 void cache_open(void)
766 for (i=0; cachelist[i].cache_name; i++ ) {
768 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
770 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
771 cachelist[i].f = fopen(path, "r+");
775 void cache_set_fds(fd_set *fdset)
778 for (i=0; cachelist[i].cache_name; i++) {
780 FD_SET(fileno(cachelist[i].f), fdset);
784 int cache_process_req(fd_set *readfds)
788 for (i=0; cachelist[i].cache_name; i++) {
789 if (cachelist[i].f != NULL &&
790 FD_ISSET(fileno(cachelist[i].f), readfds)) {
792 cachelist[i].cache_handle(cachelist[i].f);
793 FD_CLR(fileno(cachelist[i].f), readfds);
801 * Give IP->domain and domain+path->options to kernel
802 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
803 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
806 int cache_export_ent(char *domain, struct exportent *exp, char *path)
809 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
813 err = dump_to_cache(f, domain, exp->e_path, exp);
816 "Cannot export %s, possibly unsupported filesystem or"
817 " fsid= required", exp->e_path);
820 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
821 /* really an 'if', but we can break out of
822 * a 'while' more easily */
823 /* Look along 'path' for other filesystems
824 * and export them with the same options
827 int l = strlen(exp->e_path);
830 if (strlen(path) <= l || path[l] != '/' ||
831 strncmp(exp->e_path, path, l) != 0)
833 if (stat(exp->e_path, &stb) != 0)
836 while(path[l] == '/') {
838 /* errors for submount should fail whole filesystem */
842 while (path[l] != '/' && path[l])
846 err2 = lstat(path, &stb);
850 if (stb.st_dev == dev)
854 dump_to_cache(f, domain, path, exp);
864 int cache_export(nfs_export *exp, char *path)
869 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
873 qword_print(f, "nfsd");
874 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
875 qword_printint(f, time(0)+30*60);
876 qword_print(f, exp->m_client->m_hostname);
881 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
888 * echo $domain $path $length
889 * read filehandle <&0
890 * } <> /proc/fs/nfsd/filehandle
893 cache_get_filehandle(nfs_export *exp, int len, char *p)
895 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
899 static struct nfs_fh_len fh;
902 f = fopen("/proc/fs/nfs/filehandle", "r+");
906 qword_print(f, exp->m_client->m_hostname);
908 qword_printint(f, len);
909 failed = qword_eol(f);
912 failed = (fgets(buf, sizeof(buf), f) == NULL);
916 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
917 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);