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>
33 #include "pseudoflavors.h"
36 #include "blkid/blkid.h"
40 * Invoked by RPC service loop
42 void cache_set_fds(fd_set *fdset);
43 int cache_process_req(fd_set *readfds);
57 * Support routines for text-based upcalls.
58 * Fields are separated by spaces.
59 * Fields are either mangled to quote space tab newline slosh with slosh
60 * or a hexified with a leading \x
61 * Record is terminated with newline.
64 static int cache_export_ent(char *domain, struct exportent *exp, char *p);
66 #define INITIAL_MANAGED_GROUPS 100
70 extern int use_ipaddr;
72 static void auth_unix_ip(FILE *f)
76 * Ignore if class != "nfsd"
77 * Otherwise find domainname and write back:
79 * "nfsd" IP-ADDR expiry domainname
83 char ipaddr[INET6_ADDRSTRLEN];
85 struct addrinfo *tmp = NULL;
86 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
89 xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
93 if (qword_get(&cp, class, 20) <= 0 ||
94 strcmp(class, "nfsd") != 0)
97 if (qword_get(&cp, ipaddr, sizeof(ipaddr)) <= 0)
100 tmp = host_pton(ipaddr);
106 /* addr is a valid, interesting address, find the domain name... */
108 struct addrinfo *ai = NULL;
110 ai = client_resolve(tmp->ai_addr);
112 client = client_compose(ai);
116 qword_print(f, "nfsd");
117 qword_print(f, ipaddr);
118 qword_printtimefrom(f, DEFAULT_TTL);
120 qword_print(f, ipaddr);
122 qword_print(f, *client?client:"DEFAULT");
124 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
131 static void auth_unix_gid(FILE *f)
136 * uid expiry count list of group ids
140 static gid_t *groups = NULL;
141 static int groups_len = 0;
147 if (groups_len == 0) {
148 groups = malloc(sizeof(gid_t) * INITIAL_MANAGED_GROUPS);
152 groups_len = INITIAL_MANAGED_GROUPS;
155 ngroups = groups_len;
157 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
161 if (qword_get_uint(&cp, &uid) != 0)
168 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
169 if (rv == -1 && ngroups >= groups_len) {
170 more_groups = realloc(groups, sizeof(gid_t)*ngroups);
174 groups = more_groups;
175 groups_len = ngroups;
176 rv = getgrouplist(pw->pw_name, pw->pw_gid,
181 qword_printuint(f, uid);
182 qword_printtimefrom(f, DEFAULT_TTL);
184 qword_printuint(f, ngroups);
185 for (i=0; i<ngroups; i++)
186 qword_printuint(f, groups[i]);
188 qword_printuint(f, 0);
193 static const char *get_uuid_blkdev(char *path)
195 /* We set *safe if we know that we need the
196 * fsid from statfs too.
198 static blkid_cache cache = NULL;
201 blkid_tag_iterate iter;
204 const char *val, *uuid = NULL;
207 blkid_get_cache(&cache, NULL);
209 if (stat(path, &stb) != 0)
211 devname = blkid_devno_to_devname(stb.st_dev);
214 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
218 iter = blkid_tag_iterate_begin(dev);
221 while (blkid_tag_next(iter, &type, &val) == 0) {
222 if (strcmp(type, "UUID") == 0)
224 if (strcmp(type, "TYPE") == 0 &&
225 strcmp(val, "btrfs") == 0) {
230 blkid_tag_iterate_end(iter);
234 #define get_uuid_blkdev(path) (NULL)
237 static int get_uuid(const char *val, size_t uuidlen, char *u)
239 /* extract hex digits from uuidstr and compose a uuid
240 * of the given length (max 16), xoring bytes to make
245 memset(u, 0, uuidlen);
246 for ( ; *val ; val++) {
267 static int uuid_by_path(char *path, int type, size_t uuidlen, char *uuid)
269 /* get a uuid for the filesystem found at 'path'.
270 * There are several possible ways of generating the
272 * Type 0 is used for new filehandles, while other types
273 * may be used to interpret old filehandle - to ensure smooth
275 * We return 1 if a uuid was found (and it might be worth
276 * trying the next type) or 0 if no more uuid types can be
280 /* Possible sources of uuid are
284 * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
285 * encoding of the device that the filesystem is mounted from, so
286 * it we be very bad to use that (as device numbers change). blkid
288 * On other filesystems (e.g. btrfs) the statfs64 uuid contains
289 * important info that the blkid uuid cannot contain: This happens
290 * when multiple subvolumes are exported (they have the same
291 * blkid uuid but different statfs64 uuids).
292 * We rely on get_uuid_blkdev *knowing* which is which and not returning
293 * a uuid for filesystems where the statfs64 uuid is better.
298 const char *blkid_val;
301 blkid_val = get_uuid_blkdev(path);
303 if (statfs64(path, &st) == 0 &&
304 (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
305 snprintf(fsid_val, 17, "%08x%08x",
306 st.f_fsid.__val[0], st.f_fsid.__val[1]);
310 if (blkid_val && (type--) == 0)
312 else if (fsid_val[0] && (type--) == 0)
317 get_uuid(val, uuidlen, uuid);
321 /* Iterate through /etc/mtab, finding mountpoints
322 * at or below a given path
324 static char *next_mnt(void **v, char *p)
328 size_t l = strlen(p);
330 f = setmntent("/etc/mtab", "r");
334 while ((me = getmntent(f)) != NULL &&
335 (strncmp(me->mnt_dir, p, l) != 0 ||
336 me->mnt_dir[l] != '/'))
346 static int is_subdirectory(char *child, char *parent)
348 size_t l = strlen(parent);
350 if (strcmp(parent, "/") == 0)
353 return strcmp(child, parent) == 0
354 || (strncmp(child, parent, l) == 0 && child[l] == '/');
357 static int path_matches(nfs_export *exp, char *path)
359 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
360 return is_subdirectory(path, exp->m_export.e_path);
361 return strcmp(path, exp->m_export.e_path) == 0;
365 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
367 return path_matches(exp, path) && client_matches(exp, dom, ai);
370 /* True iff e1 is a child of e2 and e2 has crossmnt set: */
371 static bool subexport(struct exportent *e1, struct exportent *e2)
373 char *p1 = e1->e_path, *p2 = e2->e_path;
374 return e2->e_flags & NFSEXP_CROSSMOUNT
375 && is_subdirectory(p1, p2);
380 /* We could use a union for this, but it would be more
381 * complicated; why bother? */
385 unsigned int fsidnum;
390 static int parse_fsid(int fsidtype, int fsidlen, char *fsid,
391 struct parsed_fsid *parsed)
394 unsigned long long inode64;
396 memset(parsed, 0, sizeof(*parsed));
397 parsed->fsidtype = fsidtype;
399 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
402 memcpy(&dev, fsid, 4);
403 memcpy(&parsed->inode, fsid+4, 4);
404 parsed->major = ntohl(dev)>>16;
405 parsed->minor = ntohl(dev) & 0xFFFF;
408 case FSID_NUM: /* 4 bytes - fsid */
411 memcpy(&parsed->fsidnum, fsid, 4);
414 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
415 * This format is never actually used but was
416 * an historical accident
420 memcpy(&dev, fsid, 4);
421 parsed->major = ntohl(dev);
422 memcpy(&dev, fsid+4, 4);
423 parsed->minor = ntohl(dev);
424 memcpy(&parsed->inode, fsid+8, 4);
427 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
428 /* This is *host* endian, not net-byte-order, because
429 * no-one outside this host has any business interpreting it
433 memcpy(&dev, fsid, 4);
434 memcpy(&parsed->inode, fsid+4, 4);
435 parsed->major = (dev & 0xfff00) >> 8;
436 parsed->minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
439 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
442 memcpy(&parsed->inode, fsid, 4);
444 parsed->fhuuid = fsid+4;
446 case FSID_UUID8: /* 8 byte uuid */
450 parsed->fhuuid = fsid;
452 case FSID_UUID16: /* 16 byte uuid */
455 parsed->uuidlen = 16;
456 parsed->fhuuid = fsid;
458 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
461 memcpy(&inode64, fsid, 8);
462 parsed->inode = inode64;
463 parsed->uuidlen = 16;
464 parsed->fhuuid = fsid+8;
470 static bool match_fsid(struct parsed_fsid *parsed, nfs_export *exp, char *path)
476 if (stat(path, &stb) != 0)
478 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode))
481 switch (parsed->fsidtype) {
483 case FSID_MAJOR_MINOR:
484 case FSID_ENCODE_DEV:
485 if (stb.st_ino != parsed->inode)
487 if (parsed->major != major(stb.st_dev) ||
488 parsed->minor != minor(stb.st_dev))
492 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
493 exp->m_export.e_fsid != parsed->fsidnum))
496 case FSID_UUID4_INUM:
497 case FSID_UUID16_INUM:
498 if (stb.st_ino != parsed->inode)
503 if (!is_mountpoint(path))
506 if (exp->m_export.e_uuid)
507 get_uuid(exp->m_export.e_uuid, parsed->uuidlen, u);
510 uuid_by_path(path, type, parsed->uuidlen, u);
512 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) == 0)
515 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) != 0)
519 /* Well, unreachable, actually: */
523 static struct addrinfo *lookup_client_addr(char *dom)
525 struct addrinfo *ret;
526 struct addrinfo *tmp;
528 dom++; /* skip initial "$" */
530 tmp = host_pton(dom);
533 ret = client_resolve(tmp->ai_addr);
538 static void nfsd_fh(FILE *f)
541 * domain fsidtype fsid
542 * interpret fsid, find export point and options, and write:
543 * domain fsidtype fsid expiry path
550 struct parsed_fsid parsed;
551 struct exportent *found = NULL;
552 struct addrinfo *ai = NULL;
553 char *found_path = NULL;
558 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
561 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
565 dom = malloc(strlen(cp));
568 if (qword_get(&cp, dom, strlen(cp)) <= 0)
570 if (qword_get_int(&cp, &fsidtype) != 0)
572 if (fsidtype < 0 || fsidtype > 7)
573 goto out; /* unknown type */
574 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
576 if (parse_fsid(fsidtype, fsidlen, fsid, &parsed))
581 if (is_ipaddr_client(dom)) {
582 ai = lookup_client_addr(dom);
587 /* Now determine export point for this fsid/domain */
588 for (i=0 ; i < MCL_MAXTYPES; i++) {
589 nfs_export *next_exp;
590 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
593 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
594 static nfs_export *prev = NULL;
595 static void *mnt = NULL;
599 path = next_mnt(&mnt, exp->m_export.e_path);
601 next_exp = exp->m_next;
609 path = exp->m_export.e_path;
613 path = exp->m_export.e_path;
614 next_exp = exp->m_next;
617 if (!is_ipaddr_client(dom)
618 && !namelist_client_matches(exp, dom))
620 if (exp->m_export.e_mountpoint &&
621 !is_mountpoint(exp->m_export.e_mountpoint[0]?
622 exp->m_export.e_mountpoint:
623 exp->m_export.e_path))
626 if (!match_fsid(&parsed, exp, path))
628 if (is_ipaddr_client(dom)
629 && !ipaddr_client_matches(exp, ai))
631 if (!found || subexport(&exp->m_export, found)) {
632 found = &exp->m_export;
634 found_path = strdup(path);
635 if (found_path == NULL)
637 } else if (strcmp(found->e_path, exp->m_export.e_path) != 0
638 && !subexport(found, &exp->m_export))
640 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
641 found_path, path, dom);
643 /* same path, if one is V4ROOT, choose the other */
644 if (found->e_flags & NFSEXP_V4ROOT) {
645 found = &exp->m_export;
647 found_path = strdup(path);
648 if (found_path == NULL)
655 found->e_mountpoint &&
656 !is_mountpoint(found->e_mountpoint[0]?
659 /* Cannot export this yet
660 * should log a warning, but need to rate limit
661 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
662 found->e_path, found->e_mountpoint);
664 /* FIXME we need to make sure we re-visit this later */
667 if (!found && dev_missing) {
668 /* The missing dev could be what we want, so just be
669 * quite rather than returning stale yet
675 if (cache_export_ent(dom, found, found_path) < 0)
679 qword_printint(f, fsidtype);
680 qword_printhex(f, fsid, fsidlen);
681 /* The fsid -> path lookup can be quite expensive as it
682 * potentially stats and reads lots of devices, and some of those
683 * might have spun-down. The Answer is not likely to
684 * change underneath us, and an 'exportfs -f' can always
685 * remove this from the kernel, so use a really log
686 * timeout. Maybe this should be configurable on the command
689 qword_printint(f, 0x7fffffff);
691 qword_print(f, found_path);
698 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
702 static void write_fsloc(FILE *f, struct exportent *ep)
704 struct servers *servers;
706 if (ep->e_fslocmethod == FSLOC_NONE)
709 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
712 qword_print(f, "fsloc");
713 qword_printint(f, servers->h_num);
714 if (servers->h_num >= 0) {
716 for (i=0; i<servers->h_num; i++) {
717 qword_print(f, servers->h_mp[i]->h_host);
718 qword_print(f, servers->h_mp[i]->h_path);
721 qword_printint(f, servers->h_referral);
722 release_replicas(servers);
725 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
729 for (p = ep->e_secinfo; p->flav; p++)
731 if (p == ep->e_secinfo) {
732 /* There was no sec= option */
735 qword_print(f, "secinfo");
736 qword_printint(f, p - ep->e_secinfo);
737 for (p = ep->e_secinfo; p->flav; p++) {
738 qword_printint(f, p->flav->fnum);
739 qword_printint(f, p->flags & flag_mask);
744 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
746 qword_print(f, domain);
747 qword_print(f, path);
749 int different_fs = strcmp(path, exp->e_path) != 0;
750 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
752 qword_printtimefrom(f, exp->e_ttl);
753 qword_printint(f, exp->e_flags & flag_mask);
754 qword_printint(f, exp->e_anonuid);
755 qword_printint(f, exp->e_anongid);
756 qword_printint(f, exp->e_fsid);
758 write_secinfo(f, exp, flag_mask);
759 if (exp->e_uuid == NULL || different_fs) {
761 if (uuid_by_path(path, 0, 16, u)) {
762 qword_print(f, "uuid");
763 qword_printhex(f, u, 16);
767 get_uuid(exp->e_uuid, 16, u);
768 qword_print(f, "uuid");
769 qword_printhex(f, u, 16);
772 qword_printtimefrom(f, DEFAULT_TTL);
777 lookup_export(char *dom, char *path, struct addrinfo *ai)
780 nfs_export *found = NULL;
784 for (i=0 ; i < MCL_MAXTYPES; i++) {
785 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
786 if (!export_matches(exp, dom, path, ai))
793 /* Always prefer non-V4ROOT exports */
794 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
796 if (found->m_export.e_flags & NFSEXP_V4ROOT) {
802 /* If one is a CROSSMOUNT, then prefer the longest path */
803 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
804 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
805 strlen(found->m_export.e_path) !=
806 strlen(exp->m_export.e_path)) {
808 if (strlen(exp->m_export.e_path) >
809 strlen(found->m_export.e_path)) {
815 } else if (found_type == i && found->m_warned == 0) {
816 xlog(L_WARNING, "%s exported to both %s and %s, "
817 "arbitrarily choosing options from first",
818 path, found->m_client->m_hostname, exp->m_client->m_hostname,
827 #ifdef HAVE_NFS_PLUGIN_H
830 #include <nfs-plugin.h>
833 * Find the export entry for the parent of "pathname".
834 * Caller must not free returned exportent.
836 static struct exportent *lookup_parent_export(char *dom,
837 const char *pathname, struct addrinfo *ai)
839 char *parent, *slash;
842 parent = strdup(pathname);
843 if (parent == NULL) {
844 xlog(D_GENERAL, "%s: failed to allocate parent path buffer",
848 xlog(D_CALL, "%s: pathname = '%s'", __func__, pathname);
851 /* shorten pathname by one component */
852 slash = strrchr(parent, '/');
854 xlog(D_GENERAL, "%s: no slash found in pathname",
860 if (strlen(parent) == 0) {
861 result = lookup_export(dom, "/", ai);
862 if (result == NULL) {
863 xlog(L_ERROR, "%s: no root export found.", __func__);
869 result = lookup_export(dom, parent, ai);
870 if (result == NULL) {
871 xlog(D_GENERAL, "%s: lookup_export(%s) found nothing",
877 xlog(D_CALL, "%s: found export for %s", __func__, parent);
879 return &result->m_export;
883 return mkexportent("*", "/", "insecure");
887 * Walk through a set of FS locations and build an e_fslocdata string.
888 * Returns true if all went to plan; otherwise, false.
890 static bool locations_to_fslocdata(struct jp_ops *ops,
891 nfs_fsloc_set_t locations, char *fslocdata,
892 size_t remaining, int *ttl)
894 char *server, *last_path, *rootpath, *ptr;
904 enum jp_status status;
907 status = ops->jp_get_next_location(locations, &server,
909 if (status == JP_EMPTY)
911 if (status != JP_OK) {
912 xlog(D_GENERAL, "%s: failed to parse location: %s",
913 __func__, ops->jp_error(status));
916 xlog(D_GENERAL, "%s: Location: %s:%s",
917 __func__, server, rootpath);
919 if (last_path && strcmp(rootpath, last_path) == 0) {
920 len = snprintf(ptr, remaining, "+%s", server);
922 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
925 if ((size_t)len >= remaining) {
926 xlog(D_GENERAL, "%s: fslocdata buffer overflow", __func__);
929 remaining -= (size_t)len;
932 if (last_path == NULL)
933 len = snprintf(ptr, remaining, "%s@%s",
936 len = snprintf(ptr, remaining, ":%s@%s",
939 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
942 if ((size_t)len >= remaining) {
943 xlog(D_GENERAL, "%s: fslocdata buffer overflow",
947 remaining -= (size_t)len;
949 last_path = rootpath;
957 xlog(D_CALL, "%s: fslocdata='%s', ttl=%d",
958 __func__, fslocdata, *ttl);
968 * Duplicate the junction's parent's export options and graft in
969 * the fslocdata we constructed from the locations list.
971 static struct exportent *create_junction_exportent(struct exportent *parent,
972 const char *junction, const char *fslocdata, int ttl)
974 static struct exportent *eep;
976 eep = (struct exportent *)malloc(sizeof(*eep));
980 dupexportent(eep, parent);
981 strcpy(eep->e_path, junction);
982 eep->e_hostname = strdup(parent->e_hostname);
983 if (eep->e_hostname == NULL) {
989 eep->e_ttl = (unsigned int)ttl;
991 free(eep->e_fslocdata);
992 eep->e_fslocdata = strdup(fslocdata);
993 if (eep->e_fslocdata == NULL) {
994 free(eep->e_hostname);
998 eep->e_fslocmethod = FSLOC_REFER;
1002 xlog(L_ERROR, "%s: No memory", __func__);
1007 * Walk through the set of FS locations and build an exportent.
1008 * Returns pointer to an exportent if "junction" refers to a junction.
1010 static struct exportent *locations_to_export(struct jp_ops *ops,
1011 nfs_fsloc_set_t locations, const char *junction,
1012 struct exportent *parent)
1014 static char fslocdata[BUFSIZ];
1017 fslocdata[0] = '\0';
1018 if (!locations_to_fslocdata(ops, locations,
1019 fslocdata, sizeof(fslocdata), &ttl))
1021 return create_junction_exportent(parent, junction, fslocdata, ttl);
1025 * Retrieve locations information in "junction" and dump it to the
1026 * kernel. Returns pointer to an exportent if "junction" refers
1029 static struct exportent *invoke_junction_ops(void *handle, char *dom,
1030 const char *junction, struct addrinfo *ai)
1032 struct exportent *parent, *exp = NULL;
1033 nfs_fsloc_set_t locations;
1034 enum jp_status status;
1038 ops = (struct jp_ops *)dlsym(handle, "nfs_junction_ops");
1040 if (error != NULL) {
1041 xlog(D_GENERAL, "%s: dlsym(jp_junction_ops): %s",
1045 if (ops->jp_api_version != JP_API_VERSION) {
1046 xlog(D_GENERAL, "%s: unrecognized junction API version: %u",
1047 __func__, ops->jp_api_version);
1051 status = ops->jp_init(false);
1052 if (status != JP_OK) {
1053 xlog(D_GENERAL, "%s: failed to resolve %s: %s",
1054 __func__, junction, ops->jp_error(status));
1058 status = ops->jp_get_locations(junction, &locations);
1062 case JP_NOTJUNCTION:
1063 xlog(D_GENERAL, "%s: %s is not a junction",
1064 __func__, junction);
1067 xlog(L_WARNING, "Dangling junction %s: %s",
1068 junction, ops->jp_error(status));
1072 parent = lookup_parent_export(dom, junction, ai);
1076 exp = locations_to_export(ops, locations, junction, parent);
1078 ops->jp_put_locations(locations);
1086 * Load the junction plug-in, then try to resolve "pathname".
1087 * Returns pointer to an initialized exportent if "junction"
1088 * refers to a junction, or NULL if not.
1090 static struct exportent *lookup_junction(char *dom, const char *pathname,
1091 struct addrinfo *ai)
1093 struct exportent *exp;
1094 struct link_map *map;
1097 handle = dlopen("libnfsjunct.so", RTLD_NOW);
1098 if (handle == NULL) {
1099 xlog(D_GENERAL, "%s: dlopen: %s", __func__, dlerror());
1103 if (dlinfo(handle, RTLD_DI_LINKMAP, &map) == 0)
1104 xlog(D_GENERAL, "%s: loaded plug-in %s",
1105 __func__, map->l_name);
1107 (void)dlerror(); /* Clear any error */
1109 exp = invoke_junction_ops(handle, dom, pathname, ai);
1111 /* We could leave it loaded to make junction resolution
1112 * faster next time. However, if we want to replace the
1113 * library, that would require restarting mountd. */
1114 (void)dlclose(handle);
1118 static void lookup_nonexport(FILE *f, char *dom, char *path,
1119 struct addrinfo *ai)
1121 struct exportent *eep;
1123 eep = lookup_junction(dom, path, ai);
1124 dump_to_cache(f, dom, path, eep);
1127 exportent_release(eep);
1130 #else /* !HAVE_NFS_PLUGIN_H */
1131 static void lookup_nonexport(FILE *f, char *dom, char *path,
1132 struct addrinfo *UNUSED(ai))
1134 dump_to_cache(f, dom, path, NULL);
1136 #endif /* !HAVE_NFS_PLUGIN_H */
1138 static void nfsd_export(FILE *f)
1142 * determine export options and return:
1143 * domain path expiry flags anonuid anongid fsid
1148 nfs_export *found = NULL;
1149 struct addrinfo *ai = NULL;
1151 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
1154 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
1157 dom = malloc(strlen(cp));
1158 path = malloc(strlen(cp));
1163 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
1165 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
1170 if (is_ipaddr_client(dom)) {
1171 ai = lookup_client_addr(dom);
1176 found = lookup_export(dom, path, ai);
1179 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
1181 "Cannot export %s, possibly unsupported filesystem"
1182 " or fsid= required", path);
1183 dump_to_cache(f, dom, path, NULL);
1186 lookup_nonexport(f, dom, path, ai);
1189 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
1191 if (path) free(path);
1198 void (*cache_handle)(FILE *f);
1200 char vbuf[RPC_CHAN_BUF_SIZE];
1202 { "auth.unix.ip", auth_unix_ip, NULL, ""},
1203 { "auth.unix.gid", auth_unix_gid, NULL, ""},
1204 { "nfsd.export", nfsd_export, NULL, ""},
1205 { "nfsd.fh", nfsd_fh, NULL, ""},
1206 { NULL, NULL, NULL, ""}
1209 extern int manage_gids;
1212 * cache_open - prepare communications channels with kernel RPC caches
1215 void cache_open(void)
1218 for (i=0; cachelist[i].cache_name; i++ ) {
1220 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
1222 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
1223 cachelist[i].f = fopen(path, "r+");
1224 if (cachelist[i].f != NULL) {
1225 setvbuf(cachelist[i].f, cachelist[i].vbuf, _IOLBF,
1232 * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
1233 * @fdset: pointer to fd_set to prepare
1235 void cache_set_fds(fd_set *fdset)
1238 for (i=0; cachelist[i].cache_name; i++) {
1240 FD_SET(fileno(cachelist[i].f), fdset);
1245 * cache_process_req - process any active cache file descriptors during service loop iteration
1246 * @fdset: pointer to fd_set to examine for activity
1248 int cache_process_req(fd_set *readfds)
1252 for (i=0; cachelist[i].cache_name; i++) {
1253 if (cachelist[i].f != NULL &&
1254 FD_ISSET(fileno(cachelist[i].f), readfds)) {
1256 cachelist[i].cache_handle(cachelist[i].f);
1257 FD_CLR(fileno(cachelist[i].f), readfds);
1265 * Give IP->domain and domain+path->options to kernel
1266 * % echo nfsd $IP $[now+DEFAULT_TTL] $domain > /proc/net/rpc/auth.unix.ip/channel
1267 * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
1270 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
1273 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
1277 err = dump_to_cache(f, domain, exp->e_path, exp);
1280 "Cannot export %s, possibly unsupported filesystem or"
1281 " fsid= required", exp->e_path);
1284 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
1285 /* really an 'if', but we can break out of
1286 * a 'while' more easily */
1287 /* Look along 'path' for other filesystems
1288 * and export them with the same options
1291 size_t l = strlen(exp->e_path);
1294 if (strlen(path) <= l || path[l] != '/' ||
1295 strncmp(exp->e_path, path, l) != 0)
1297 if (stat(exp->e_path, &stb) != 0)
1300 while(path[l] == '/') {
1302 /* errors for submount should fail whole filesystem */
1306 while (path[l] != '/' && path[l])
1310 err2 = lstat(path, &stb);
1314 if (stb.st_dev == dev)
1318 dump_to_cache(f, domain, path, exp);
1329 * cache_export - Inform kernel of a new nfs_export
1330 * @exp: target nfs_export
1331 * @path: NUL-terminated C string containing export path
1333 int cache_export(nfs_export *exp, char *path)
1335 char buf[INET6_ADDRSTRLEN];
1339 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
1344 qword_print(f, "nfsd");
1346 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
1347 qword_printtimefrom(f, exp->m_export.e_ttl);
1348 qword_print(f, exp->m_client->m_hostname);
1353 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
1359 * cache_get_filehandle - given an nfs_export, get its root filehandle
1360 * @exp: target nfs_export
1361 * @len: length of requested file handle
1362 * @p: NUL-terminated C string containing export path
1364 * Returns pointer to NFS file handle of root directory of export
1367 * echo $domain $path $length
1368 * read filehandle <&0
1369 * } <> /proc/fs/nfsd/filehandle
1372 cache_get_filehandle(nfs_export *exp, int len, char *p)
1374 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
1378 static struct nfs_fh_len fh;
1381 f = fopen("/proc/fs/nfs/filehandle", "r+");
1385 qword_print(f, exp->m_client->m_hostname);
1387 qword_printint(f, len);
1388 failed = qword_eol(f);
1391 failed = (fgets(buf, sizeof(buf), f) == NULL);
1395 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1396 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);