4 * Parse the exports file. Derived from the unfsd implementation.
6 * Authors: Donald J. Becker, <becker@super.org>
7 * Rick Sladkey, <jrs@world.std.com>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 * Olaf Kirch, <okir@monad.swb.de>
10 * Alexander O. Yuriev, <alex@bach.cis.temple.edu>
12 * This software maybe be used for any purpose provided
13 * the above copyright notice is retained. It is supplied
14 * as is, with no warranty expressed or implied.
21 #include <sys/param.h>
33 #include "pseudoflavors.h"
35 #define EXPORT_DEFAULT_FLAGS \
36 (NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES|NFSEXP_NOSUBTREECHECK)
38 struct flav_info flav_map[] = {
39 { "krb5", RPC_AUTH_GSS_KRB5 },
40 { "krb5i", RPC_AUTH_GSS_KRB5I },
41 { "krb5p", RPC_AUTH_GSS_KRB5P },
42 { "unix", AUTH_UNIX },
44 { "null", AUTH_NULL },
45 { "none", AUTH_NONE },
48 const int flav_map_size = sizeof(flav_map)/sizeof(flav_map[0]);
52 static char *efname = NULL;
53 static XFILE *efp = NULL;
55 static int has_default_opts, has_default_subtree_opts;
56 static int *squids = NULL, nsquids = 0,
57 *sqgids = NULL, nsqgids = 0;
59 static int getexport(char *exp, int len);
60 static int getpath(char *path, int len);
61 static int parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr);
62 static int parsesquash(char *list, int **idp, int *lenp, char **ep);
63 static int parsenum(char **cpp);
64 static void freesquash(void);
65 static void syntaxerr(char *msg);
68 setexportent(char *fname, char *type)
73 fname = _PATH_EXPORTS;
74 if (!(efp = xfopen(fname, type)))
75 xlog(L_ERROR, "can't open %s for %sing",
76 fname, strcmp(type, "r")? "writ" : "read");
77 efname = strdup(fname);
81 static void init_exportent (struct exportent *ee, int fromkernel)
83 ee->e_flags = EXPORT_DEFAULT_FLAGS;
84 /* some kernels assume the default is sync rather than
85 * async. More recent kernels always report one or other,
86 * but this test makes sure we assume same as kernel
90 ee->e_flags &= ~NFSEXP_ASYNC;
91 ee->e_flags &= ~NFSEXP_GATHERED_WRITES;
93 ee->e_anonuid = 65534;
94 ee->e_anongid = 65534;
97 ee->e_mountpoint = NULL;
98 ee->e_fslocmethod = FSLOC_NONE;
99 ee->e_fslocdata = NULL;
100 ee->e_secinfo[0].flav = NULL;
104 ee->e_ttl = DEFAULT_TTL;
108 getexportent(int fromkernel, int fromexports)
110 static struct exportent ee, def_ee;
111 char exp[512], *hostname;
112 char rpath[MAXPATHLEN+1];
121 if (first || (ok = getexport(exp, sizeof(exp))) == 0) {
122 has_default_opts = 0;
123 has_default_subtree_opts = 0;
125 init_exportent(&def_ee, fromkernel);
127 ok = getpath(def_ee.e_path, sizeof(def_ee.e_path));
131 ok = getexport(exp, sizeof(exp));
134 xlog(L_ERROR, "expected client(options...)");
135 export_errno = EINVAL;
141 * Check for default options. The kernel will never have default
142 * options in /proc/fs/nfs/exports, however due to the initial '-' in
143 * the -test-client- string from the test export we have to check that
144 * we're not reading from the kernel.
146 if (exp[0] == '-' && !fromkernel) {
147 if (parseopts(exp + 1, &def_ee, 0, &has_default_subtree_opts) < 0)
150 has_default_opts = 1;
152 ok = getexport(exp, sizeof(exp));
154 xlog(L_ERROR, "expected client(options...)");
155 export_errno = EINVAL;
162 /* Check for default client */
167 if ((opt = strchr(exp, '(')) != NULL) {
169 xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
173 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
174 syntaxerr("bad option list");
175 export_errno = EINVAL;
180 if (!has_default_opts)
181 xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
183 xfree(ee.e_hostname);
184 ee.e_hostname = xstrdup(hostname);
186 if (parseopts(opt, &ee, fromexports && !has_default_subtree_opts, NULL) < 0)
189 /* resolve symlinks */
190 if (realpath(ee.e_path, rpath) != NULL) {
191 rpath[sizeof (rpath) - 1] = '\0';
192 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
193 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
199 void secinfo_show(FILE *fp, struct exportent *ep)
201 struct sec_entry *p1, *p2;
204 for (p1=ep->e_secinfo; p1->flav; p1=p2) {
206 fprintf(fp, ",sec=%s", p1->flav->flavour);
207 for (p2=p1+1; (p2->flav != NULL) && (p1->flags == p2->flags);
209 fprintf(fp, ":%s", p2->flav->flavour);
212 fprintf(fp, ",%s", (flags & NFSEXP_READONLY) ? "ro" : "rw");
213 fprintf(fp, ",%sroot_squash", (flags & NFSEXP_ROOTSQUASH)?
215 fprintf(fp, ",%sall_squash", (flags & NFSEXP_ALLSQUASH)?
221 putexportent(struct exportent *ep)
225 char *esc=ep->e_path;
231 for (i=0; esc[i]; i++)
232 if (iscntrl(esc[i]) || esc[i] == '"' || esc[i] == '\\' || esc[i] == '#' || isspace(esc[i]))
233 fprintf(fp, "\\%03o", esc[i]);
235 fprintf(fp, "%c", esc[i]);
237 fprintf(fp, "\t%s(", ep->e_hostname);
238 fprintf(fp, "%s,", (ep->e_flags & NFSEXP_READONLY)? "ro" : "rw");
239 fprintf(fp, "%ssync,", (ep->e_flags & NFSEXP_ASYNC)? "a" : "");
240 fprintf(fp, "%swdelay,", (ep->e_flags & NFSEXP_GATHERED_WRITES)?
242 fprintf(fp, "%shide,", (ep->e_flags & NFSEXP_NOHIDE)?
244 fprintf(fp, "%scrossmnt,", (ep->e_flags & NFSEXP_CROSSMOUNT)?
246 fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
248 fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
250 fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
252 fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
254 fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
256 fprintf(fp, "%sacl,", (ep->e_flags & NFSEXP_NOACL)?
258 if (ep->e_flags & NFSEXP_FSID) {
259 fprintf(fp, "fsid=%d,", ep->e_fsid);
262 fprintf(fp, "fsid=%s,", ep->e_uuid);
263 if (ep->e_mountpoint)
264 fprintf(fp, "mountpoint%s%s,",
265 ep->e_mountpoint[0]?"=":"", ep->e_mountpoint);
266 switch (ep->e_fslocmethod) {
270 fprintf(fp, "refer=%s,", ep->e_fslocdata);
273 fprintf(fp, "replicas=%s,", ep->e_fslocdata);
277 fprintf(fp, "fsloc=stub,");
281 xlog(L_ERROR, "unknown fsloc method for %s:%s",
282 ep->e_hostname, ep->e_path);
284 if ((id = ep->e_squids) != NULL) {
285 fprintf(fp, "squash_uids=");
286 for (i = 0; i < ep->e_nsquids; i += 2)
287 if (id[i] != id[i+1])
288 fprintf(fp, "%d-%d,", id[i], id[i+1]);
290 fprintf(fp, "%d,", id[i]);
292 if ((id = ep->e_sqgids) != NULL) {
293 fprintf(fp, "squash_gids=");
294 for (i = 0; i < ep->e_nsquids; i += 2)
295 if (id[i] != id[i+1])
296 fprintf(fp, "%d-%d,", id[i], id[i+1]);
298 fprintf(fp, "%d,", id[i]);
300 fprintf(fp, "anonuid=%d,anongid=%d", ep->e_anonuid, ep->e_anongid);
301 secinfo_show(fp, ep);
318 dupexportent(struct exportent *dst, struct exportent *src)
323 if ((n = src->e_nsquids) != 0) {
324 dst->e_squids = (int *) xmalloc(n * sizeof(int));
325 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
327 if ((n = src->e_nsqgids) != 0) {
328 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
329 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
331 if (src->e_mountpoint)
332 dst->e_mountpoint = strdup(src->e_mountpoint);
333 if (src->e_fslocdata)
334 dst->e_fslocdata = strdup(src->e_fslocdata);
336 dst->e_uuid = strdup(src->e_uuid);
337 dst->e_hostname = NULL;
341 mkexportent(char *hname, char *path, char *options)
343 static struct exportent ee;
345 init_exportent(&ee, 0);
347 xfree(ee.e_hostname);
348 ee.e_hostname = xstrdup(hname);
350 if (strlen(path) >= sizeof(ee.e_path)) {
351 xlog(L_WARNING, "path name %s too long", path);
354 strncpy(ee.e_path, path, sizeof (ee.e_path));
355 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
356 if (parseopts(options, &ee, 0, NULL) < 0)
362 updateexportent(struct exportent *eep, char *options)
364 if (parseopts(options, eep, 0, NULL) < 0)
370 static int valid_uuid(char *uuid)
372 /* must have 32 hex digits */
374 for (cnt = 0 ; *uuid; uuid++)
381 * Append the given flavor to the exportent's e_secinfo array, or
382 * do nothing if it's already there. Returns the index of flavor
383 * in the resulting array in any case.
385 int secinfo_addflavor(struct flav_info *flav, struct exportent *ep)
389 for (p=ep->e_secinfo; p->flav; p++) {
391 return p - ep->e_secinfo;
393 if (p - ep->e_secinfo >= SECFLAVOR_COUNT) {
394 xlog(L_ERROR, "more than %d security flavors on an export\n",
399 p->flags = ep->e_flags;
401 return p - ep->e_secinfo;
404 static struct flav_info *find_flavor(char *name)
406 struct flav_info *flav;
407 for (flav = flav_map; flav < flav_map + flav_map_size; flav++)
408 if (strcmp(flav->flavour, name) == 0)
413 /* @str is a colon seperated list of security flavors. Their order
414 * is recorded in @ep, and a bitmap corresponding to the list is returned.
415 * A zero return indicates an error.
417 static unsigned int parse_flavors(char *str, struct exportent *ep)
423 while ( (flavor=strsep(&str, ":")) ) {
424 struct flav_info *flav = find_flavor(flavor);
426 xlog(L_ERROR, "unknown flavor %s\n", flavor);
429 bit = secinfo_addflavor(flav, ep);
437 /* Sets the bits in @mask for the appropriate security flavor flags. */
438 static void setflags(int mask, unsigned int active, struct exportent *ep)
446 ep->e_secinfo[bit].flags |= mask;
452 /* Clears the bits in @mask for the appropriate security flavor flags. */
453 static void clearflags(int mask, unsigned int active, struct exportent *ep)
457 ep->e_flags &= ~mask;
461 ep->e_secinfo[bit].flags &= ~mask;
468 * For those flags which are not allowed to vary by pseudoflavor,
469 * ensure that the export flags agree with the flags on each
472 static void fix_pseudoflavor_flags(struct exportent *ep)
474 struct export_features *ef;
477 ef = get_export_features();
478 for (p = ep->e_secinfo; p->flav; p++)
479 p->flags |= ep->e_flags & ~ef->secinfo_flags;
483 * Parse option string pointed to by cp and set mount options accordingly.
486 parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr)
488 int had_subtree_opt = 0;
489 char *flname = efname?efname:"command line";
490 int flline = efp?efp->x_line:0;
491 unsigned int active = 0;
493 squids = ep->e_squids; nsquids = ep->e_nsquids;
494 sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
502 char *opt = strdup(cp);
504 while (*cp && *cp != ',')
507 opt[cp-optstart] = '\0';
511 /* process keyword */
512 if (strcmp(opt, "ro") == 0)
513 setflags(NFSEXP_READONLY, active, ep);
514 else if (strcmp(opt, "rw") == 0)
515 clearflags(NFSEXP_READONLY, active, ep);
516 else if (!strcmp(opt, "secure"))
517 clearflags(NFSEXP_INSECURE_PORT, active, ep);
518 else if (!strcmp(opt, "insecure"))
519 setflags(NFSEXP_INSECURE_PORT, active, ep);
520 else if (!strcmp(opt, "sync"))
521 clearflags(NFSEXP_ASYNC, active, ep);
522 else if (!strcmp(opt, "async"))
523 setflags(NFSEXP_ASYNC, active, ep);
524 else if (!strcmp(opt, "nohide"))
525 setflags(NFSEXP_NOHIDE, active, ep);
526 else if (!strcmp(opt, "hide"))
527 clearflags(NFSEXP_NOHIDE, active, ep);
528 else if (!strcmp(opt, "crossmnt"))
529 setflags(NFSEXP_CROSSMOUNT, active, ep);
530 else if (!strcmp(opt, "nocrossmnt"))
531 clearflags(NFSEXP_CROSSMOUNT, active, ep);
532 else if (!strcmp(opt, "wdelay"))
533 setflags(NFSEXP_GATHERED_WRITES, active, ep);
534 else if (!strcmp(opt, "no_wdelay"))
535 clearflags(NFSEXP_GATHERED_WRITES, active, ep);
536 else if (strcmp(opt, "root_squash") == 0)
537 setflags(NFSEXP_ROOTSQUASH, active, ep);
538 else if (!strcmp(opt, "no_root_squash"))
539 clearflags(NFSEXP_ROOTSQUASH, active, ep);
540 else if (strcmp(opt, "all_squash") == 0)
541 setflags(NFSEXP_ALLSQUASH, active, ep);
542 else if (strcmp(opt, "no_all_squash") == 0)
543 clearflags(NFSEXP_ALLSQUASH, active, ep);
544 else if (strcmp(opt, "subtree_check") == 0) {
546 clearflags(NFSEXP_NOSUBTREECHECK, active, ep);
547 } else if (strcmp(opt, "no_subtree_check") == 0) {
549 setflags(NFSEXP_NOSUBTREECHECK, active, ep);
550 } else if (strcmp(opt, "auth_nlm") == 0)
551 clearflags(NFSEXP_NOAUTHNLM, active, ep);
552 else if (strcmp(opt, "no_auth_nlm") == 0)
553 setflags(NFSEXP_NOAUTHNLM, active, ep);
554 else if (strcmp(opt, "secure_locks") == 0)
555 clearflags(NFSEXP_NOAUTHNLM, active, ep);
556 else if (strcmp(opt, "insecure_locks") == 0)
557 setflags(NFSEXP_NOAUTHNLM, active, ep);
558 else if (strcmp(opt, "acl") == 0)
559 clearflags(NFSEXP_NOACL, active, ep);
560 else if (strcmp(opt, "no_acl") == 0)
561 setflags(NFSEXP_NOACL, active, ep);
562 else if (strncmp(opt, "anonuid=", 8) == 0) {
564 ep->e_anonuid = strtol(opt+8, &oe, 10);
565 if (opt[8]=='\0' || *oe != '\0') {
566 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
567 flname, flline, opt);
570 export_errno = EINVAL;
573 } else if (strncmp(opt, "anongid=", 8) == 0) {
575 ep->e_anongid = strtol(opt+8, &oe, 10);
576 if (opt[8]=='\0' || *oe != '\0') {
577 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
578 flname, flline, opt);
581 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
582 if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
585 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
586 if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
589 } else if (strncmp(opt, "fsid=", 5) == 0) {
591 if (strcmp(opt+5, "root") == 0) {
593 setflags(NFSEXP_FSID, active, ep);
595 ep->e_fsid = strtoul(opt+5, &oe, 0);
596 if (opt[5]!='\0' && *oe == '\0')
597 setflags(NFSEXP_FSID, active, ep);
598 else if (valid_uuid(opt+5))
599 ep->e_uuid = strdup(opt+5);
601 xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
602 flname, flline, opt);
606 } else if (strcmp(opt, "mountpoint")==0 ||
607 strcmp(opt, "mp") == 0 ||
608 strncmp(opt, "mountpoint=", 11)==0 ||
609 strncmp(opt, "mp=", 3) == 0) {
610 char * mp = strchr(opt, '=');
612 ep->e_mountpoint = strdup(mp+1);
614 ep->e_mountpoint = strdup("");
616 } else if (strncmp(opt, "fsloc=", 6) == 0) {
617 if (strcmp(opt+6, "stub") == 0)
618 ep->e_fslocmethod = FSLOC_STUB;
620 xlog(L_ERROR, "%s:%d: bad option %s\n",
621 flname, flline, opt);
625 } else if (strncmp(opt, "refer=", 6) == 0) {
626 ep->e_fslocmethod = FSLOC_REFER;
627 ep->e_fslocdata = strdup(opt+6);
628 } else if (strncmp(opt, "replicas=", 9) == 0) {
629 ep->e_fslocmethod = FSLOC_REPLICA;
630 ep->e_fslocdata = strdup(opt+9);
631 } else if (strncmp(opt, "sec=", 4) == 0) {
632 active = parse_flavors(opt+4, ep);
636 xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
637 flname, flline, opt);
638 setflags(NFSEXP_ALLSQUASH | NFSEXP_READONLY, active, ep);
646 fix_pseudoflavor_flags(ep);
647 ep->e_squids = squids;
648 ep->e_sqgids = sqgids;
649 ep->e_nsquids = nsquids;
650 ep->e_nsqgids = nsqgids;
653 if (warn && !had_subtree_opt)
654 xlog(L_WARNING, "%s [%d]: Neither 'subtree_check' or 'no_subtree_check' specified for export \"%s:%s\".\n"
655 " Assuming default behaviour ('no_subtree_check').\n"
656 " NOTE: this default has changed since nfs-utils version 1.0.x\n",
659 ep->e_hostname, ep->e_path);
660 if (had_subtree_opt_ptr)
661 *had_subtree_opt_ptr = had_subtree_opt;
667 parsesquash(char *list, int **idp, int *lenp, char **ep)
685 if (id0 == -1 || id1 == -1) {
686 syntaxerr("uid/gid -1 not permitted");
690 id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
693 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
696 syntaxerr("bad uid/gid list");
702 if (**ep == ',') (*ep)++;
732 while (isdigit(**cpp))
734 c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
739 getpath(char *path, int len)
742 return xgettok(efp, 0, path, len);
746 getexport(char *exp, int len)
751 if ((ok = xgettok(efp, 0, exp, len)) < 0)
752 xlog(L_ERROR, "%s:%d: syntax error",
753 efname?"command line":efname, efp->x_line);
760 xlog(L_ERROR, "%s:%d: syntax error: %s",
761 efname, efp?efp->x_line:0, msg);
763 struct export_features *get_export_features(void)
765 static char *path = "/proc/fs/nfsd/export_features";
766 static struct export_features ef;
767 static int cached = 0;
775 ef.flags = NFSEXP_OLDFLAGS;
776 ef.secinfo_flags = NFSEXP_OLD_SECINFO_FLAGS;
778 fd = open(path, O_RDONLY);
781 c = read(fd, buf, 50);
785 c = sscanf(buf, "%x %x", &ef.flags, &ef.secinfo_flags);
792 xlog(L_WARNING, "unexpected error reading %s", path);