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>
34 #define EXPORT_DEFAULT_FLAGS \
35 (NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES)
39 static char *efname = NULL;
40 static XFILE *efp = NULL;
42 static int has_default_opts, has_default_subtree_opts;
43 static int *squids = NULL, nsquids = 0,
44 *sqgids = NULL, nsqgids = 0;
46 static int getexport(char *exp, int len);
47 static int getpath(char *path, int len);
48 static int parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr);
49 static int parsesquash(char *list, int **idp, int *lenp, char **ep);
50 static int parsenum(char **cpp);
51 static int parsemaptype(char *type);
52 static void freesquash(void);
53 static void syntaxerr(char *msg);
56 setexportent(char *fname, char *type)
61 fname = _PATH_EXPORTS;
62 if (!(efp = xfopen(fname, type)))
63 xlog(L_ERROR, "can't open %s for %sing",
64 fname, strcmp(type, "r")? "writ" : "read");
65 efname = strdup(fname);
70 getexportent(int fromkernel, int fromexports)
72 static struct exportent ee, def_ee;
73 char exp[512], *hostname;
74 char rpath[MAXPATHLEN+1];
83 if (first || (ok = getexport(exp, sizeof(exp))) == 0) {
85 has_default_subtree_opts = 0;
87 def_ee.e_flags = EXPORT_DEFAULT_FLAGS;
88 /* some kernels assume the default is sync rather than
89 * async. More recent kernels always report one or other,
90 * but this test makes sure we assume same as kernel
94 def_ee.e_flags &= ~NFSEXP_ASYNC;
95 def_ee.e_flags &= ~NFSEXP_GATHERED_WRITES;
97 def_ee.e_maptype = CLE_MAP_IDENT;
98 def_ee.e_anonuid = 65534;
99 def_ee.e_anongid = 65534;
100 def_ee.e_squids = NULL;
101 def_ee.e_sqgids = NULL;
102 def_ee.e_mountpoint = NULL;
103 def_ee.e_nsquids = 0;
104 def_ee.e_nsqgids = 0;
106 ok = getpath(def_ee.e_path, sizeof(def_ee.e_path));
110 strncpy (def_ee.m_path, def_ee.e_path, sizeof (def_ee.m_path) - 1);
111 def_ee.m_path [sizeof (def_ee.m_path) - 1] = '\0';
112 ok = getexport(exp, sizeof(exp));
115 xlog(L_ERROR, "expected client(options...)");
116 export_errno = EINVAL;
121 /* Check for default options */
123 if (parseopts(exp + 1, &def_ee, 0, &has_default_subtree_opts) < 0)
126 has_default_opts = 1;
128 ok = getexport(exp, sizeof(exp));
130 xlog(L_ERROR, "expected client(options...)");
131 export_errno = EINVAL;
138 /* Check for default client */
143 if ((opt = strchr(exp, '(')) != NULL) {
145 xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
149 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
150 syntaxerr("bad option list");
151 export_errno = EINVAL;
156 if (!has_default_opts)
157 xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
159 if (strlen(hostname) >= sizeof(ee.e_hostname)) {
160 syntaxerr("client name too long");
161 export_errno = EINVAL;
164 strncpy(ee.e_hostname, hostname, sizeof (ee.e_hostname) - 1);
165 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
167 if (parseopts(opt, &ee, fromexports && !has_default_subtree_opts, NULL) < 0)
170 /* resolve symlinks */
171 if (realpath(ee.e_path, rpath) != NULL) {
172 rpath[sizeof (rpath) - 1] = '\0';
173 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
174 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
175 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
176 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
183 putexportent(struct exportent *ep)
187 char *esc=ep->e_path;
193 for (i=0; esc[i]; i++)
194 if (iscntrl(esc[i]) || esc[i] == '"' || esc[i] == '\\' || esc[i] == '#' || isspace(esc[i]))
195 fprintf(fp, "\\%03o", esc[i]);
197 fprintf(fp, "%c", esc[i]);
199 fprintf(fp, "\t%s(", ep->e_hostname);
200 fprintf(fp, "%s,", (ep->e_flags & NFSEXP_READONLY)? "ro" : "rw");
201 fprintf(fp, "%ssync,", (ep->e_flags & NFSEXP_ASYNC)? "a" : "");
202 fprintf(fp, "%swdelay,", (ep->e_flags & NFSEXP_GATHERED_WRITES)?
204 fprintf(fp, "%shide,", (ep->e_flags & NFSEXP_NOHIDE)?
206 fprintf(fp, "%scrossmnt,", (ep->e_flags & NFSEXP_CROSSMOUNT)?
208 fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
210 fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
212 fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
214 fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
216 fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
218 fprintf(fp, "%sacl,", (ep->e_flags & NFSEXP_NOACL)?
220 if (ep->e_flags & NFSEXP_FSID) {
221 fprintf(fp, "fsid=%d,", ep->e_fsid);
224 fprintf(fp, "fsid=%s,", ep->e_uuid);
225 if (ep->e_mountpoint)
226 fprintf(fp, "mountpoint%s%s,",
227 ep->e_mountpoint[0]?"=":"", ep->e_mountpoint);
229 fprintf(fp, "mapping=");
230 switch (ep->e_maptype) {
232 fprintf(fp, "identity,");
235 fprintf(fp, "ugidd,");
238 fprintf(fp, "file,");
241 xlog(L_ERROR, "unknown mapping type for %s:%s",
242 ep->e_hostname, ep->e_path);
244 if ((id = ep->e_squids) != NULL) {
245 fprintf(fp, "squash_uids=");
246 for (i = 0; i < ep->e_nsquids; i += 2)
247 if (id[i] != id[i+1])
248 fprintf(fp, "%d-%d,", id[i], id[i+1]);
250 fprintf(fp, "%d,", id[i]);
252 if ((id = ep->e_sqgids) != NULL) {
253 fprintf(fp, "squash_gids=");
254 for (i = 0; i < ep->e_nsquids; i += 2)
255 if (id[i] != id[i+1])
256 fprintf(fp, "%d-%d,", id[i], id[i+1]);
258 fprintf(fp, "%d,", id[i]);
260 fprintf(fp, "anonuid=%d,anongid=%d)\n", ep->e_anonuid, ep->e_anongid);
276 dupexportent(struct exportent *dst, struct exportent *src)
281 if ((n = src->e_nsquids) != 0) {
282 dst->e_squids = (int *) xmalloc(n * sizeof(int));
283 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
285 if ((n = src->e_nsqgids) != 0) {
286 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
287 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
289 if (src->e_mountpoint)
290 dst->e_mountpoint = strdup(src->e_mountpoint);
294 mkexportent(char *hname, char *path, char *options)
296 static struct exportent ee;
298 ee.e_flags = EXPORT_DEFAULT_FLAGS;
299 ee.e_maptype = CLE_MAP_IDENT;
300 ee.e_anonuid = 65534;
301 ee.e_anongid = 65534;
304 ee.e_mountpoint = NULL;
309 if (strlen(hname) >= sizeof(ee.e_hostname)) {
310 xlog(L_WARNING, "client name %s too long", hname);
313 strncpy(ee.e_hostname, hname, sizeof (ee.e_hostname) - 1);
314 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
315 if (strlen(path) >= sizeof(ee.e_path)) {
316 xlog(L_WARNING, "path name %s too long", path);
319 strncpy(ee.e_path, path, sizeof (ee.e_path));
320 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
321 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
322 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
323 if (parseopts(options, &ee, 0, NULL) < 0)
329 updateexportent(struct exportent *eep, char *options)
331 if (parseopts(options, eep, 0, NULL) < 0)
337 static int valid_uuid(char *uuid)
339 /* must have 32 hex digits */
341 for (cnt = 0 ; *uuid; uuid++)
348 * Parse option string pointed to by cp and set mount options accordingly.
351 parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr)
353 int had_subtree_opt = 0;
354 char *flname = efname?efname:"command line";
355 int flline = efp?efp->x_line:0;
357 squids = ep->e_squids; nsquids = ep->e_nsquids;
358 sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
367 char *opt = strdup(cp);
369 while (*cp && *cp != ',')
372 opt[cp-optstart] = '\0';
376 /* process keyword */
377 if (strcmp(opt, "ro") == 0)
378 ep->e_flags |= NFSEXP_READONLY;
379 else if (strcmp(opt, "rw") == 0)
380 ep->e_flags &= ~NFSEXP_READONLY;
381 else if (!strcmp(opt, "secure"))
382 ep->e_flags &= ~NFSEXP_INSECURE_PORT;
383 else if (!strcmp(opt, "insecure"))
384 ep->e_flags |= NFSEXP_INSECURE_PORT;
385 else if (!strcmp(opt, "sync"))
386 ep->e_flags &= ~NFSEXP_ASYNC;
387 else if (!strcmp(opt, "async"))
388 ep->e_flags |= NFSEXP_ASYNC;
389 else if (!strcmp(opt, "nohide"))
390 ep->e_flags |= NFSEXP_NOHIDE;
391 else if (!strcmp(opt, "hide"))
392 ep->e_flags &= ~NFSEXP_NOHIDE;
393 else if (!strcmp(opt, "crossmnt"))
394 ep->e_flags |= NFSEXP_CROSSMOUNT;
395 else if (!strcmp(opt, "nocrossmnt"))
396 ep->e_flags &= ~NFSEXP_CROSSMOUNT;
397 else if (!strcmp(opt, "wdelay"))
398 ep->e_flags |= NFSEXP_GATHERED_WRITES;
399 else if (!strcmp(opt, "no_wdelay"))
400 ep->e_flags &= ~NFSEXP_GATHERED_WRITES;
401 else if (strcmp(opt, "root_squash") == 0)
402 ep->e_flags |= NFSEXP_ROOTSQUASH;
403 else if (!strcmp(opt, "no_root_squash"))
404 ep->e_flags &= ~NFSEXP_ROOTSQUASH;
405 else if (strcmp(opt, "all_squash") == 0)
406 ep->e_flags |= NFSEXP_ALLSQUASH;
407 else if (strcmp(opt, "no_all_squash") == 0)
408 ep->e_flags &= ~NFSEXP_ALLSQUASH;
409 else if (strcmp(opt, "subtree_check") == 0) {
411 ep->e_flags &= ~NFSEXP_NOSUBTREECHECK;
412 } else if (strcmp(opt, "no_subtree_check") == 0) {
414 ep->e_flags |= NFSEXP_NOSUBTREECHECK;
415 } else if (strcmp(opt, "auth_nlm") == 0)
416 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
417 else if (strcmp(opt, "no_auth_nlm") == 0)
418 ep->e_flags |= NFSEXP_NOAUTHNLM;
419 else if (strcmp(opt, "secure_locks") == 0)
420 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
421 else if (strcmp(opt, "insecure_locks") == 0)
422 ep->e_flags |= NFSEXP_NOAUTHNLM;
423 else if (strcmp(opt, "acl") == 0)
424 ep->e_flags &= ~NFSEXP_NOACL;
425 else if (strcmp(opt, "no_acl") == 0)
426 ep->e_flags |= NFSEXP_NOACL;
427 else if (strncmp(opt, "mapping=", 8) == 0)
428 ep->e_maptype = parsemaptype(opt+8);
429 else if (strcmp(opt, "map_identity") == 0) /* old style */
430 ep->e_maptype = CLE_MAP_IDENT;
431 else if (strcmp(opt, "map_daemon") == 0) /* old style */
432 ep->e_maptype = CLE_MAP_UGIDD;
433 else if (strncmp(opt, "anonuid=", 8) == 0) {
435 ep->e_anonuid = strtol(opt+8, &oe, 10);
436 if (opt[8]=='\0' || *oe != '\0') {
437 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
438 flname, flline, opt);
441 export_errno = EINVAL;
444 } else if (strncmp(opt, "anongid=", 8) == 0) {
446 ep->e_anongid = strtol(opt+8, &oe, 10);
447 if (opt[8]=='\0' || *oe != '\0') {
448 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
449 flname, flline, opt);
452 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
453 if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
456 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
457 if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
460 } else if (strncmp(opt, "fsid=", 5) == 0) {
462 if (strcmp(opt+5, "root") == 0) {
464 ep->e_flags |= NFSEXP_FSID;
466 ep->e_fsid = strtoul(opt+5, &oe, 0);
467 if (opt[5]!='\0' && *oe == '\0')
468 ep->e_flags |= NFSEXP_FSID;
469 else if (valid_uuid(opt+5))
470 ep->e_uuid = strdup(opt+7);
472 xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
473 flname, flline, opt);
477 } else if (strcmp(opt, "mountpoint")==0 ||
478 strcmp(opt, "mp") == 0 ||
479 strncmp(opt, "mountpoint=", 11)==0 ||
480 strncmp(opt, "mp=", 3) == 0) {
481 char * mp = strchr(opt, '=');
483 ep->e_mountpoint = strdup(mp+1);
485 ep->e_mountpoint = strdup("");
487 xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
488 flname, flline, opt);
489 ep->e_flags |= NFSEXP_ALLSQUASH | NFSEXP_READONLY;
497 ep->e_squids = squids;
498 ep->e_sqgids = sqgids;
499 ep->e_nsquids = nsquids;
500 ep->e_nsqgids = nsqgids;
503 if (warn && !had_subtree_opt)
504 xlog(L_WARNING, "%s [%d]: Neither 'subtree_check' or 'no_subtree_check' specified for export \"%s:%s\".\n"
505 " Assuming default behaviour ('subtree_check').\n"
506 " NOTE: this default will change with nfs-utils version 1.1.0\n",
509 ep->e_hostname, ep->e_path);
510 if (had_subtree_opt_ptr)
511 *had_subtree_opt_ptr = had_subtree_opt;
517 parsesquash(char *list, int **idp, int *lenp, char **ep)
535 if (id0 == -1 || id1 == -1) {
536 syntaxerr("uid/gid -1 not permitted");
540 id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
543 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
546 syntaxerr("bad uid/gid list");
552 if (**ep == ',') (*ep)++;
582 while (isdigit(**cpp))
584 c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
589 parsemaptype(char *type)
591 if (!strcmp(type, "identity"))
592 return CLE_MAP_IDENT;
593 if (!strcmp(type, "ugidd"))
594 return CLE_MAP_UGIDD;
595 if (!strcmp(type, "file"))
597 syntaxerr("invalid map type");
598 return CLE_MAP_IDENT; /* default */
602 getpath(char *path, int len)
605 return xgettok(efp, 0, path, len);
609 getexport(char *exp, int len)
614 if ((ok = xgettok(efp, 0, exp, len)) < 0)
615 xlog(L_ERROR, "%s:%d: syntax error",
616 efname?"command line":efname, efp->x_line);
623 xlog(L_ERROR, "%s:%d: syntax error: %s",
624 efname, efp?efp->x_line:0, msg);