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.
19 #include <sys/param.h>
32 #define EXPORT_DEFAULT_FLAGS \
33 (NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES)
37 static char *efname = NULL;
38 static XFILE *efp = NULL;
40 static int *squids = NULL, nsquids = 0,
41 *sqgids = NULL, nsqgids = 0;
43 static int getexport(char *exp, int len);
44 static int getpath(char *path, int len);
45 static int parseopts(char *cp, struct exportent *ep, int warn);
46 static int parsesquash(char *list, int **idp, int *lenp, char **ep);
47 static int parsenum(char **cpp);
48 static int parsemaptype(char *type);
49 static void freesquash(void);
50 static void syntaxerr(char *msg);
53 setexportent(char *fname, char *type)
58 fname = _PATH_EXPORTS;
59 if (!(efp = xfopen(fname, type)))
60 xlog(L_ERROR, "can't open %s for %sing",
61 fname, strcmp(type, "r")? "writ" : "read");
62 efname = strdup(fname);
67 getexportent(int fromkernel, int fromexports)
69 static struct exportent ee;
71 char rpath[MAXPATHLEN+1];
79 ee.e_flags = EXPORT_DEFAULT_FLAGS;
80 /* some kernels assume the default is sync rather than
81 * async. More recent kernels always report one or other,
82 * but this test makes sure we assume same as kernel
86 ee.e_flags &= ~NFSEXP_ASYNC;
87 ee.e_flags &= ~NFSEXP_GATHERED_WRITES;
89 ee.e_maptype = CLE_MAP_IDENT;
97 if (first || (ok = getexport(exp, sizeof(exp))) == 0) {
98 ok = getpath(ee.e_path, sizeof(ee.e_path));
101 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
102 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
103 ok = getexport(exp, sizeof(exp));
106 xlog(L_ERROR, "expected client(options...)");
107 export_errno = EINVAL;
112 /* Check for default client */
115 if ((opt = strchr(exp, '(')) != NULL) {
117 xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
119 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
120 syntaxerr("bad option list");
121 export_errno = EINVAL;
126 xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
128 if (strlen(exp) >= sizeof(ee.e_hostname)) {
129 syntaxerr("client name too long");
130 export_errno = EINVAL;
133 strncpy(ee.e_hostname, exp, sizeof (ee.e_hostname) - 1);
134 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
136 if (parseopts(opt, &ee, fromexports) < 0)
139 /* resolve symlinks */
140 if (realpath(ee.e_path, rpath) != NULL) {
141 rpath[sizeof (rpath) - 1] = '\0';
142 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
143 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
144 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
145 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
152 putexportent(struct exportent *ep)
156 char *esc=ep->e_path;
162 for (i=0; esc[i]; i++)
163 if (iscntrl(esc[i]) || esc[i] == '"' || esc[i] == '\\'|| isspace(esc[i]))
164 fprintf(fp, "\\%03o", esc[i]);
166 fprintf(fp, "%c", esc[i]);
168 fprintf(fp, "\t%s(", ep->e_hostname);
169 fprintf(fp, "%s,", (ep->e_flags & NFSEXP_READONLY)? "ro" : "rw");
170 fprintf(fp, "%ssync,", (ep->e_flags & NFSEXP_ASYNC)? "a" : "");
171 fprintf(fp, "%swdelay,", (ep->e_flags & NFSEXP_GATHERED_WRITES)?
173 fprintf(fp, "%shide,", (ep->e_flags & NFSEXP_CROSSMNT)?
175 fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
177 fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
179 fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
181 fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
183 fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
185 if (ep->e_flags & NFSEXP_FSID) {
186 fprintf(fp, "fsid=%d,", ep->e_fsid);
188 fprintf(fp, "mapping=");
189 switch (ep->e_maptype) {
191 fprintf(fp, "identity,");
194 fprintf(fp, "ugidd,");
197 fprintf(fp, "file,");
200 xlog(L_ERROR, "unknown mapping type for %s:%s",
201 ep->e_hostname, ep->e_path);
203 if ((id = ep->e_squids) != NULL) {
204 fprintf(fp, "squash_uids=");
205 for (i = 0; i < ep->e_nsquids; i += 2)
206 if (id[i] != id[i+1])
207 fprintf(fp, "%d-%d,", id[i], id[i+1]);
209 fprintf(fp, "%d,", id[i]);
211 if ((id = ep->e_sqgids) != NULL) {
212 fprintf(fp, "squash_gids=");
213 for (i = 0; i < ep->e_nsquids; i += 2)
214 if (id[i] != id[i+1])
215 fprintf(fp, "%d-%d,", id[i], id[i+1]);
217 fprintf(fp, "%d,", id[i]);
219 fprintf(fp, "anonuid=%d,anongid=%d)\n", ep->e_anonuid, ep->e_anongid);
235 dupexportent(struct exportent *dst, struct exportent *src)
240 if ((n = src->e_nsquids) != 0) {
241 dst->e_squids = (int *) xmalloc(n * sizeof(int));
242 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
244 if ((n = src->e_nsqgids) != 0) {
245 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
246 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
251 mkexportent(char *hname, char *path, char *options)
253 static struct exportent ee;
255 ee.e_flags = EXPORT_DEFAULT_FLAGS;
256 ee.e_maptype = CLE_MAP_IDENT;
264 if (strlen(hname) >= sizeof(ee.e_hostname)) {
265 xlog(L_WARNING, "client name %s too long", hname);
268 strncpy(ee.e_hostname, hname, sizeof (ee.e_hostname) - 1);
269 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
270 if (strlen(path) >= sizeof(ee.e_path)) {
271 xlog(L_WARNING, "path name %s too long", path);
274 strncpy(ee.e_path, path, sizeof (ee.e_path));
275 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
276 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
277 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
278 if (parseopts(options, &ee, 0) < 0)
284 updateexportent(struct exportent *eep, char *options)
286 if (parseopts(options, eep, 0) < 0)
292 * Parse option string pointed to by cp and set mount options accordingly.
295 parseopts(char *cp, struct exportent *ep, int warn)
297 int had_sync_opt = 0;
298 char *flname = efname?efname:"command line";
299 int flline = efp?efp->x_line:0;
301 squids = ep->e_squids; nsquids = ep->e_nsquids;
302 sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
311 char *opt = strdup(cp);
313 while (*cp && *cp != ',')
316 opt[cp-optstart] = '\0';
320 /* process keyword */
321 if (strcmp(opt, "ro") == 0)
322 ep->e_flags |= NFSEXP_READONLY;
323 else if (strcmp(opt, "rw") == 0)
324 ep->e_flags &= ~NFSEXP_READONLY;
325 else if (!strcmp(opt, "secure"))
326 ep->e_flags &= ~NFSEXP_INSECURE_PORT;
327 else if (!strcmp(opt, "insecure"))
328 ep->e_flags |= NFSEXP_INSECURE_PORT;
329 else if (!strcmp(opt, "sync")) {
331 ep->e_flags &= ~NFSEXP_ASYNC;
332 } else if (!strcmp(opt, "async")) {
334 ep->e_flags |= NFSEXP_ASYNC;
335 } else if (!strcmp(opt, "nohide"))
336 ep->e_flags |= NFSEXP_CROSSMNT;
337 else if (!strcmp(opt, "hide"))
338 ep->e_flags &= ~NFSEXP_CROSSMNT;
339 else if (!strcmp(opt, "crossmnt")) /* old style */
340 ep->e_flags |= NFSEXP_CROSSMNT;
341 else if (!strcmp(opt, "nocrossmnt")) /* old style */
342 ep->e_flags &= ~NFSEXP_CROSSMNT;
343 else if (!strcmp(opt, "wdelay"))
344 ep->e_flags |= NFSEXP_GATHERED_WRITES;
345 else if (!strcmp(opt, "no_wdelay"))
346 ep->e_flags &= ~NFSEXP_GATHERED_WRITES;
347 else if (strcmp(opt, "root_squash") == 0)
348 ep->e_flags |= NFSEXP_ROOTSQUASH;
349 else if (!strcmp(opt, "no_root_squash"))
350 ep->e_flags &= ~NFSEXP_ROOTSQUASH;
351 else if (strcmp(opt, "all_squash") == 0)
352 ep->e_flags |= NFSEXP_ALLSQUASH;
353 else if (strcmp(opt, "no_all_squash") == 0)
354 ep->e_flags &= ~NFSEXP_ALLSQUASH;
355 else if (strcmp(opt, "subtree_check") == 0)
356 ep->e_flags &= ~NFSEXP_NOSUBTREECHECK;
357 else if (strcmp(opt, "no_subtree_check") == 0)
358 ep->e_flags |= NFSEXP_NOSUBTREECHECK;
359 else if (strcmp(opt, "auth_nlm") == 0)
360 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
361 else if (strcmp(opt, "no_auth_nlm") == 0)
362 ep->e_flags |= NFSEXP_NOAUTHNLM;
363 else if (strcmp(opt, "secure_locks") == 0)
364 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
365 else if (strcmp(opt, "insecure_locks") == 0)
366 ep->e_flags |= NFSEXP_NOAUTHNLM;
367 else if (strncmp(opt, "mapping=", 8) == 0)
368 ep->e_maptype = parsemaptype(opt+8);
369 else if (strcmp(opt, "map_identity") == 0) /* old style */
370 ep->e_maptype = CLE_MAP_IDENT;
371 else if (strcmp(opt, "map_daemon") == 0) /* old style */
372 ep->e_maptype = CLE_MAP_UGIDD;
373 else if (strncmp(opt, "anonuid=", 8) == 0) {
375 ep->e_anonuid = strtol(opt+8, &oe, 10);
376 if (opt[8]=='\0' || *oe != '\0') {
377 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
378 flname, flline, opt);
381 export_errno = EINVAL;
384 } else if (strncmp(opt, "anongid=", 8) == 0) {
386 ep->e_anongid = strtol(opt+8, &oe, 10);
387 if (opt[8]=='\0' || *oe != '\0') {
388 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
389 flname, flline, opt);
392 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
393 if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
396 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
397 if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
400 } else if (strncmp(opt, "fsid=", 5) == 0) {
402 ep->e_fsid = strtoul(opt+5, &oe, 0);
403 if (opt[5]=='\0' || *oe != '\0') {
404 xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
405 flname, flline, opt);
408 ep->e_flags |= NFSEXP_FSID;
410 xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
411 flname, flline, opt);
412 ep->e_flags |= NFSEXP_ALLSQUASH | NFSEXP_READONLY;
420 ep->e_squids = squids;
421 ep->e_sqgids = sqgids;
422 ep->e_nsquids = nsquids;
423 ep->e_nsqgids = nsqgids;
426 if (warn && !had_sync_opt)
427 xlog(L_WARNING, "%s [%d]: No 'sync' or 'async' option specified for export \"%s:%s\".\n"
428 " Assuming default behaviour ('sync').\n"
429 " NOTE: this default has changed from previous versions\n",
432 ep->e_hostname, ep->e_path);
438 parsesquash(char *list, int **idp, int *lenp, char **ep)
456 if (id0 == -1 || id1 == -1) {
457 syntaxerr("uid/gid -1 not permitted");
461 id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
464 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
467 syntaxerr("bad uid/gid list");
473 if (**ep == ',') (*ep)++;
503 while (isdigit(**cpp))
505 c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
510 parsemaptype(char *type)
512 if (!strcmp(type, "identity"))
513 return CLE_MAP_IDENT;
514 if (!strcmp(type, "ugidd"))
515 return CLE_MAP_UGIDD;
516 if (!strcmp(type, "file"))
518 syntaxerr("invalid map type");
519 return CLE_MAP_IDENT; /* default */
523 getpath(char *path, int len)
526 return xgettok(efp, 0, path, len);
530 getexport(char *exp, int len)
535 if ((ok = xgettok(efp, 0, exp, len)) < 0)
536 xlog(L_ERROR, "%s:%d: syntax error",
537 efname?"command line":efname, efp->x_line);
544 xlog(L_ERROR, "%s:%d: syntax error: %s",
545 efname, efp?efp->x_line:0, msg);