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>
31 #define EXPORT_DEFAULT_FLAGS \
32 (NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES)
34 static char *efname = NULL;
35 static XFILE *efp = NULL;
37 static int *squids = NULL, nsquids = 0,
38 *sqgids = NULL, nsqgids = 0;
40 static int getexport(char *exp, int len);
41 static int getpath(char *path, int len);
42 static int parseopts(char *cp, struct exportent *ep, int warn);
43 static int parsesquash(char *list, int **idp, int *lenp, char **ep);
44 static int parsenum(char **cpp);
45 static int parsemaptype(char *type);
46 static void freesquash(void);
47 static void syntaxerr(char *msg);
50 setexportent(char *fname, char *type)
55 fname = _PATH_EXPORTS;
56 if (!(efp = xfopen(fname, type)))
57 xlog(L_ERROR, "can't open %s for %sing",
58 fname, strcmp(type, "r")? "writ" : "read");
59 efname = strdup(fname);
64 getexportent(int fromkernel, int fromexports)
66 static struct exportent ee;
68 char rpath[MAXPATHLEN+1];
76 ee.e_flags = EXPORT_DEFAULT_FLAGS;
77 /* some kernels assume the default is sync rather than
78 * async. More recent kernels always report one or other,
79 * but this test makes sure we assume same as kernel
83 ee.e_flags &= ~NFSEXP_ASYNC;
84 ee.e_flags &= ~NFSEXP_GATHERED_WRITES;
86 ee.e_maptype = CLE_MAP_IDENT;
94 if (first || (ok = getexport(exp, sizeof(exp))) == 0) {
95 ok = getpath(ee.e_path, sizeof(ee.e_path));
98 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
99 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
100 ok = getexport(exp, sizeof(exp));
103 xlog(L_ERROR, "expected client(options...)");
108 /* Check for default client */
111 if ((opt = strchr(exp, '(')) != NULL) {
113 xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
115 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
116 syntaxerr("bad option list");
121 xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
123 if (strlen(exp) >= sizeof(ee.e_hostname)) {
124 syntaxerr("client name too long");
127 strncpy(ee.e_hostname, exp, sizeof (ee.e_hostname) - 1);
128 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
130 if (parseopts(opt, &ee, fromexports) < 0)
133 /* resolve symlinks */
134 if (realpath(ee.e_path, rpath) != NULL) {
135 rpath[sizeof (rpath) - 1] = '\0';
136 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
137 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
138 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
139 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
146 putexportent(struct exportent *ep)
150 char *esc=ep->e_path;
156 for (i=0; esc[i]; i++)
157 if (iscntrl(esc[i]) || esc[i] == '"' || esc[i] == '\\'|| isspace(esc[i]))
158 fprintf(fp, "\\%03o", esc[i]);
160 fprintf(fp, "%c", esc[i]);
162 fprintf(fp, "\t%s(", ep->e_hostname);
163 fprintf(fp, "%s,", (ep->e_flags & NFSEXP_READONLY)? "ro" : "rw");
164 fprintf(fp, "%ssync,", (ep->e_flags & NFSEXP_ASYNC)? "a" : "");
165 fprintf(fp, "%swdelay,", (ep->e_flags & NFSEXP_GATHERED_WRITES)?
167 fprintf(fp, "%shide,", (ep->e_flags & NFSEXP_CROSSMNT)?
169 fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
171 fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
173 fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
175 fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
177 fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
179 if (ep->e_flags & NFSEXP_FSID) {
180 fprintf(fp, "fsid=%d,", ep->e_fsid);
182 fprintf(fp, "mapping=");
183 switch (ep->e_maptype) {
185 fprintf(fp, "identity,");
188 fprintf(fp, "ugidd,");
191 fprintf(fp, "file,");
194 xlog(L_ERROR, "unknown mapping type for %s:%s",
195 ep->e_hostname, ep->e_path);
197 if ((id = ep->e_squids) != NULL) {
198 fprintf(fp, "squash_uids=");
199 for (i = 0; i < ep->e_nsquids; i += 2)
200 if (id[i] != id[i+1])
201 fprintf(fp, "%d-%d,", id[i], id[i+1]);
203 fprintf(fp, "%d,", id[i]);
205 if ((id = ep->e_sqgids) != NULL) {
206 fprintf(fp, "squash_gids=");
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 fprintf(fp, "anonuid=%d,anongid=%d)\n", ep->e_anonuid, ep->e_anongid);
229 dupexportent(struct exportent *dst, struct exportent *src)
234 if ((n = src->e_nsquids) != 0) {
235 dst->e_squids = (int *) xmalloc(n * sizeof(int));
236 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
238 if ((n = src->e_nsqgids) != 0) {
239 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
240 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
245 mkexportent(char *hname, char *path, char *options)
247 static struct exportent ee;
249 ee.e_flags = EXPORT_DEFAULT_FLAGS;
250 ee.e_maptype = CLE_MAP_IDENT;
258 if (strlen(hname) >= sizeof(ee.e_hostname)) {
259 xlog(L_WARNING, "client name %s too long", hname);
262 strncpy(ee.e_hostname, hname, sizeof (ee.e_hostname) - 1);
263 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
264 if (strlen(path) >= sizeof(ee.e_path)) {
265 xlog(L_WARNING, "path name %s too long", path);
268 strncpy(ee.e_path, path, sizeof (ee.e_path));
269 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
270 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
271 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
272 if (parseopts(options, &ee, 0) < 0)
278 updateexportent(struct exportent *eep, char *options)
280 if (parseopts(options, eep, 0) < 0)
286 * Parse option string pointed to by cp and set mount options accordingly.
289 parseopts(char *cp, struct exportent *ep, int warn)
291 int had_sync_opt = 0;
292 char *flname = efname?efname:"command line";
293 int flline = efp?efp->x_line:0;
295 squids = ep->e_squids; nsquids = ep->e_nsquids;
296 sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
305 char *opt = strdup(cp);
307 while (*cp && *cp != ',')
310 opt[cp-optstart] = '\0';
314 /* process keyword */
315 if (strcmp(opt, "ro") == 0)
316 ep->e_flags |= NFSEXP_READONLY;
317 else if (strcmp(opt, "rw") == 0)
318 ep->e_flags &= ~NFSEXP_READONLY;
319 else if (!strcmp(opt, "secure"))
320 ep->e_flags &= ~NFSEXP_INSECURE_PORT;
321 else if (!strcmp(opt, "insecure"))
322 ep->e_flags |= NFSEXP_INSECURE_PORT;
323 else if (!strcmp(opt, "sync")) {
325 ep->e_flags &= ~NFSEXP_ASYNC;
326 } else if (!strcmp(opt, "async")) {
328 ep->e_flags |= NFSEXP_ASYNC;
329 } else if (!strcmp(opt, "nohide"))
330 ep->e_flags |= NFSEXP_CROSSMNT;
331 else if (!strcmp(opt, "hide"))
332 ep->e_flags &= ~NFSEXP_CROSSMNT;
333 else if (!strcmp(opt, "crossmnt")) /* old style */
334 ep->e_flags |= NFSEXP_CROSSMNT;
335 else if (!strcmp(opt, "nocrossmnt")) /* old style */
336 ep->e_flags &= ~NFSEXP_CROSSMNT;
337 else if (!strcmp(opt, "wdelay"))
338 ep->e_flags |= NFSEXP_GATHERED_WRITES;
339 else if (!strcmp(opt, "no_wdelay"))
340 ep->e_flags &= ~NFSEXP_GATHERED_WRITES;
341 else if (strcmp(opt, "root_squash") == 0)
342 ep->e_flags |= NFSEXP_ROOTSQUASH;
343 else if (!strcmp(opt, "no_root_squash"))
344 ep->e_flags &= ~NFSEXP_ROOTSQUASH;
345 else if (strcmp(opt, "all_squash") == 0)
346 ep->e_flags |= NFSEXP_ALLSQUASH;
347 else if (strcmp(opt, "no_all_squash") == 0)
348 ep->e_flags &= ~NFSEXP_ALLSQUASH;
349 else if (strcmp(opt, "subtree_check") == 0)
350 ep->e_flags &= ~NFSEXP_NOSUBTREECHECK;
351 else if (strcmp(opt, "no_subtree_check") == 0)
352 ep->e_flags |= NFSEXP_NOSUBTREECHECK;
353 else if (strcmp(opt, "auth_nlm") == 0)
354 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
355 else if (strcmp(opt, "no_auth_nlm") == 0)
356 ep->e_flags |= NFSEXP_NOAUTHNLM;
357 else if (strcmp(opt, "secure_locks") == 0)
358 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
359 else if (strcmp(opt, "insecure_locks") == 0)
360 ep->e_flags |= NFSEXP_NOAUTHNLM;
361 else if (strncmp(opt, "mapping=", 8) == 0)
362 ep->e_maptype = parsemaptype(opt+8);
363 else if (strcmp(opt, "map_identity") == 0) /* old style */
364 ep->e_maptype = CLE_MAP_IDENT;
365 else if (strcmp(opt, "map_daemon") == 0) /* old style */
366 ep->e_maptype = CLE_MAP_UGIDD;
367 else if (strncmp(opt, "anonuid=", 8) == 0) {
369 ep->e_anonuid = strtol(opt+8, &oe, 10);
370 if (opt[8]=='\0' || *oe != '\0') {
371 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
372 flname, flline, opt);
376 } else if (strncmp(opt, "anongid=", 8) == 0) {
378 ep->e_anongid = strtol(opt+8, &oe, 10);
379 if (opt[8]=='\0' || *oe != '\0') {
380 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
381 flname, flline, opt);
385 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
386 if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
390 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
391 if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
395 } else if (strncmp(opt, "fsid=", 5) == 0) {
397 ep->e_fsid = strtoul(opt+5, &oe, 0);
398 if (opt[5]=='\0' || *oe != '\0') {
399 xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
400 flname, flline, opt);
404 ep->e_flags |= NFSEXP_FSID;
406 xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
407 flname, flline, opt);
408 ep->e_flags |= NFSEXP_ALLSQUASH | NFSEXP_READONLY;
417 ep->e_squids = squids;
418 ep->e_sqgids = sqgids;
419 ep->e_nsquids = nsquids;
420 ep->e_nsqgids = nsqgids;
423 if (warn && !had_sync_opt)
424 xlog(L_WARNING, "No 'sync' or 'async' option specified for export \"%s:%s\".\n"
425 " Assuming default behaviour ('sync').\n"
426 " NOTE: this default has changed from previous versions\n",
427 ep->e_hostname, ep->e_path);
433 parsesquash(char *list, int **idp, int *lenp, char **ep)
451 if (id0 == -1 || id1 == -1) {
452 syntaxerr("uid/gid -1 not permitted");
456 id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
459 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
462 syntaxerr("bad uid/gid list");
468 if (**ep == ',') (*ep)++;
498 while (isdigit(**cpp))
500 c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
505 parsemaptype(char *type)
507 if (!strcmp(type, "identity"))
508 return CLE_MAP_IDENT;
509 if (!strcmp(type, "ugidd"))
510 return CLE_MAP_UGIDD;
511 if (!strcmp(type, "file"))
513 syntaxerr("invalid map type");
514 return CLE_MAP_IDENT; /* default */
518 getpath(char *path, int len)
521 return xgettok(efp, 0, path, len);
525 getexport(char *exp, int len)
530 if ((ok = xgettok(efp, 0, exp, len)) < 0)
531 xlog(L_ERROR, "%s:%d: syntax error",
532 efname?"command line":efname, efp->x_line);
539 xlog(L_ERROR, "%s:%d: syntax error: %s",
540 efname, efp?efp->x_line:0, msg);