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_NOHIDE)?
175 fprintf(fp, "%scrossmnt,", (ep->e_flags & NFSEXP_CROSSMNT)?
177 fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
179 fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
181 fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
183 fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
185 fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
187 if (ep->e_flags & NFSEXP_FSID) {
188 fprintf(fp, "fsid=%d,", ep->e_fsid);
190 fprintf(fp, "mapping=");
191 switch (ep->e_maptype) {
193 fprintf(fp, "identity,");
196 fprintf(fp, "ugidd,");
199 fprintf(fp, "file,");
202 xlog(L_ERROR, "unknown mapping type for %s:%s",
203 ep->e_hostname, ep->e_path);
205 if ((id = ep->e_squids) != NULL) {
206 fprintf(fp, "squash_uids=");
207 for (i = 0; i < ep->e_nsquids; i += 2)
208 if (id[i] != id[i+1])
209 fprintf(fp, "%d-%d,", id[i], id[i+1]);
211 fprintf(fp, "%d,", id[i]);
213 if ((id = ep->e_sqgids) != NULL) {
214 fprintf(fp, "squash_gids=");
215 for (i = 0; i < ep->e_nsquids; i += 2)
216 if (id[i] != id[i+1])
217 fprintf(fp, "%d-%d,", id[i], id[i+1]);
219 fprintf(fp, "%d,", id[i]);
221 fprintf(fp, "anonuid=%d,anongid=%d)\n", ep->e_anonuid, ep->e_anongid);
237 dupexportent(struct exportent *dst, struct exportent *src)
242 if ((n = src->e_nsquids) != 0) {
243 dst->e_squids = (int *) xmalloc(n * sizeof(int));
244 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
246 if ((n = src->e_nsqgids) != 0) {
247 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
248 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
253 mkexportent(char *hname, char *path, char *options)
255 static struct exportent ee;
257 ee.e_flags = EXPORT_DEFAULT_FLAGS;
258 ee.e_maptype = CLE_MAP_IDENT;
266 if (strlen(hname) >= sizeof(ee.e_hostname)) {
267 xlog(L_WARNING, "client name %s too long", hname);
270 strncpy(ee.e_hostname, hname, sizeof (ee.e_hostname) - 1);
271 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
272 if (strlen(path) >= sizeof(ee.e_path)) {
273 xlog(L_WARNING, "path name %s too long", path);
276 strncpy(ee.e_path, path, sizeof (ee.e_path));
277 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
278 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
279 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
280 if (parseopts(options, &ee, 0) < 0)
286 updateexportent(struct exportent *eep, char *options)
288 if (parseopts(options, eep, 0) < 0)
294 * Parse option string pointed to by cp and set mount options accordingly.
297 parseopts(char *cp, struct exportent *ep, int warn)
299 int had_sync_opt = 0;
300 char *flname = efname?efname:"command line";
301 int flline = efp?efp->x_line:0;
303 squids = ep->e_squids; nsquids = ep->e_nsquids;
304 sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
313 char *opt = strdup(cp);
315 while (*cp && *cp != ',')
318 opt[cp-optstart] = '\0';
322 /* process keyword */
323 if (strcmp(opt, "ro") == 0)
324 ep->e_flags |= NFSEXP_READONLY;
325 else if (strcmp(opt, "rw") == 0)
326 ep->e_flags &= ~NFSEXP_READONLY;
327 else if (!strcmp(opt, "secure"))
328 ep->e_flags &= ~NFSEXP_INSECURE_PORT;
329 else if (!strcmp(opt, "insecure"))
330 ep->e_flags |= NFSEXP_INSECURE_PORT;
331 else if (!strcmp(opt, "sync")) {
333 ep->e_flags &= ~NFSEXP_ASYNC;
334 } else if (!strcmp(opt, "async")) {
336 ep->e_flags |= NFSEXP_ASYNC;
337 } else if (!strcmp(opt, "nohide"))
338 ep->e_flags |= NFSEXP_NOHIDE;
339 else if (!strcmp(opt, "hide"))
340 ep->e_flags &= ~NFSEXP_NOHIDE;
341 else if (!strcmp(opt, "crossmnt")) /* old style */
342 ep->e_flags |= NFSEXP_CROSSMNT;
343 else if (!strcmp(opt, "nocrossmnt")) /* old style */
344 ep->e_flags &= ~NFSEXP_CROSSMNT;
345 else if (!strcmp(opt, "wdelay"))
346 ep->e_flags |= NFSEXP_GATHERED_WRITES;
347 else if (!strcmp(opt, "no_wdelay"))
348 ep->e_flags &= ~NFSEXP_GATHERED_WRITES;
349 else if (strcmp(opt, "root_squash") == 0)
350 ep->e_flags |= NFSEXP_ROOTSQUASH;
351 else if (!strcmp(opt, "no_root_squash"))
352 ep->e_flags &= ~NFSEXP_ROOTSQUASH;
353 else if (strcmp(opt, "all_squash") == 0)
354 ep->e_flags |= NFSEXP_ALLSQUASH;
355 else if (strcmp(opt, "no_all_squash") == 0)
356 ep->e_flags &= ~NFSEXP_ALLSQUASH;
357 else if (strcmp(opt, "subtree_check") == 0)
358 ep->e_flags &= ~NFSEXP_NOSUBTREECHECK;
359 else if (strcmp(opt, "no_subtree_check") == 0)
360 ep->e_flags |= NFSEXP_NOSUBTREECHECK;
361 else if (strcmp(opt, "auth_nlm") == 0)
362 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
363 else if (strcmp(opt, "no_auth_nlm") == 0)
364 ep->e_flags |= NFSEXP_NOAUTHNLM;
365 else if (strcmp(opt, "secure_locks") == 0)
366 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
367 else if (strcmp(opt, "insecure_locks") == 0)
368 ep->e_flags |= NFSEXP_NOAUTHNLM;
369 else if (strncmp(opt, "mapping=", 8) == 0)
370 ep->e_maptype = parsemaptype(opt+8);
371 else if (strcmp(opt, "map_identity") == 0) /* old style */
372 ep->e_maptype = CLE_MAP_IDENT;
373 else if (strcmp(opt, "map_daemon") == 0) /* old style */
374 ep->e_maptype = CLE_MAP_UGIDD;
375 else if (strncmp(opt, "anonuid=", 8) == 0) {
377 ep->e_anonuid = strtol(opt+8, &oe, 10);
378 if (opt[8]=='\0' || *oe != '\0') {
379 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
380 flname, flline, opt);
383 export_errno = EINVAL;
386 } else if (strncmp(opt, "anongid=", 8) == 0) {
388 ep->e_anongid = strtol(opt+8, &oe, 10);
389 if (opt[8]=='\0' || *oe != '\0') {
390 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
391 flname, flline, opt);
394 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
395 if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
398 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
399 if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
402 } else if (strncmp(opt, "fsid=", 5) == 0) {
404 ep->e_fsid = strtoul(opt+5, &oe, 0);
405 if (opt[5]=='\0' || *oe != '\0') {
406 xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
407 flname, flline, opt);
410 ep->e_flags |= NFSEXP_FSID;
412 xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
413 flname, flline, opt);
414 ep->e_flags |= NFSEXP_ALLSQUASH | NFSEXP_READONLY;
422 ep->e_squids = squids;
423 ep->e_sqgids = sqgids;
424 ep->e_nsquids = nsquids;
425 ep->e_nsqgids = nsqgids;
428 if (warn && !had_sync_opt)
429 xlog(L_WARNING, "%s [%d]: No 'sync' or 'async' option specified for export \"%s:%s\".\n"
430 " Assuming default behaviour ('sync').\n"
431 " NOTE: this default has changed from previous versions\n",
434 ep->e_hostname, ep->e_path);
440 parsesquash(char *list, int **idp, int *lenp, char **ep)
458 if (id0 == -1 || id1 == -1) {
459 syntaxerr("uid/gid -1 not permitted");
463 id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
466 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
469 syntaxerr("bad uid/gid list");
475 if (**ep == ',') (*ep)++;
505 while (isdigit(**cpp))
507 c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
512 parsemaptype(char *type)
514 if (!strcmp(type, "identity"))
515 return CLE_MAP_IDENT;
516 if (!strcmp(type, "ugidd"))
517 return CLE_MAP_UGIDD;
518 if (!strcmp(type, "file"))
520 syntaxerr("invalid map type");
521 return CLE_MAP_IDENT; /* default */
525 getpath(char *path, int len)
528 return xgettok(efp, 0, path, len);
532 getexport(char *exp, int len)
537 if ((ok = xgettok(efp, 0, exp, len)) < 0)
538 xlog(L_ERROR, "%s:%d: syntax error",
539 efname?"command line":efname, efp->x_line);
546 xlog(L_ERROR, "%s:%d: syntax error: %s",
547 efname, efp?efp->x_line:0, msg);