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_ASYNC|NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES)
34 static XFILE *efp = NULL;
36 static int *squids = NULL, nsquids = 0,
37 *sqgids = NULL, nsqgids = 0;
39 static int getexport(char *exp, int len);
40 static int getpath(char *path, int len);
41 static int parseopts(char *cp, struct exportent *ep);
42 static int parsesquash(char *list, int **idp, int *lenp, char **ep);
43 static int parsenum(char **cpp);
44 static int parsemaptype(char *type);
45 static void freesquash(void);
46 static void syntaxerr(char *msg);
49 setexportent(char *fname, char *type)
54 fname = _PATH_EXPORTS;
55 if (!(efp = xfopen(fname, type)))
56 xlog(L_ERROR, "can't open %s for %sing",
57 fname, strcmp(type, "r")? "writ" : "read");
64 static struct exportent ee;
66 char rpath[MAXPATHLEN+1];
74 ee.e_flags = EXPORT_DEFAULT_FLAGS;
75 ee.e_maptype = CLE_MAP_IDENT;
83 if (first || (ok = getexport(exp, sizeof(exp))) == 0) {
84 ok = getpath(ee.e_path, sizeof(ee.e_path));
87 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
88 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
89 ok = getexport(exp, sizeof(exp));
92 xlog(L_ERROR, "expected client(options...)");
97 /* Check for default client */
100 if ((opt = strchr(exp, '(')) != NULL) {
102 xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
104 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
105 syntaxerr("bad option list");
109 if (parseopts(opt, &ee) < 0)
112 xlog(L_WARNING, "No options for %s %s: suggest %s() to avoid warning", ee.e_path, exp, exp);
114 if (strlen(exp) >= sizeof(ee.e_hostname)) {
115 syntaxerr("client name too long");
118 strncpy(ee.e_hostname, exp, sizeof (ee.e_hostname) - 1);
119 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
121 /* resolve symlinks */
122 if (realpath(ee.e_path, rpath) != NULL) {
123 rpath[sizeof (rpath) - 1] = '\0';
124 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
125 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
126 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
127 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
134 putexportent(struct exportent *ep)
138 char *esc=ep->e_path;
144 for (i=0; esc[i]; i++)
145 if (iscntrl(esc[i]) || esc[i] == '"' || esc[i] == '\\'|| isspace(esc[i]))
146 fprintf(fp, "\\%03o", esc[i]);
148 fprintf(fp, "%c", esc[i]);
150 fprintf(fp, "\t%s(", ep->e_hostname);
151 fprintf(fp, "%s,", (ep->e_flags & NFSEXP_READONLY)? "ro" : "rw");
152 fprintf(fp, "%ssync,", (ep->e_flags & NFSEXP_ASYNC)? "a" : "");
153 fprintf(fp, "%swdelay,", (ep->e_flags & NFSEXP_GATHERED_WRITES)?
155 fprintf(fp, "%shide,", (ep->e_flags & NFSEXP_CROSSMNT)?
157 fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
159 fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
161 fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
163 fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
165 fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
168 fprintf(fp, "mapping=");
169 switch (ep->e_maptype) {
171 fprintf(fp, "identity,");
174 fprintf(fp, "ugidd,");
177 fprintf(fp, "file,");
180 xlog(L_ERROR, "unknown mapping type for %s:%s",
181 ep->e_hostname, ep->e_path);
183 if ((id = ep->e_squids) != NULL) {
184 fprintf(fp, "squash_uids=");
185 for (i = 0; i < ep->e_nsquids; i += 2)
186 if (id[i] != id[i+1])
187 fprintf(fp, "%d-%d,", id[i], id[i+1]);
189 fprintf(fp, "%d,", id[i]);
191 if ((id = ep->e_sqgids) != NULL) {
192 fprintf(fp, "squash_gids=");
193 for (i = 0; i < ep->e_nsquids; i += 2)
194 if (id[i] != id[i+1])
195 fprintf(fp, "%d-%d,", id[i], id[i+1]);
197 fprintf(fp, "%d,", id[i]);
199 fprintf(fp, "anonuid=%d,anongid=%d)\n", ep->e_anonuid, ep->e_anongid);
212 dupexportent(struct exportent *dst, struct exportent *src)
217 if ((n = src->e_nsquids) != 0) {
218 dst->e_squids = (int *) xmalloc(n * sizeof(int));
219 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
221 if ((n = src->e_nsqgids) != 0) {
222 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
223 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
228 mkexportent(char *hname, char *path, char *options)
230 static struct exportent ee;
232 ee.e_flags = EXPORT_DEFAULT_FLAGS;
233 ee.e_maptype = CLE_MAP_IDENT;
241 if (strlen(hname) >= sizeof(ee.e_hostname)) {
242 xlog(L_WARNING, "client name %s too long", hname);
245 strncpy(ee.e_hostname, hname, sizeof (ee.e_hostname) - 1);
246 ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
247 if (strlen(path) >= sizeof(ee.e_path)) {
248 xlog(L_WARNING, "path name %s too long", path);
251 strncpy(ee.e_path, path, sizeof (ee.e_path));
252 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
253 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
254 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
255 if (options && parseopts(options, &ee) < 0)
261 updateexportent(struct exportent *eep, char *options)
263 if (options && parseopts(options, eep) < 0)
269 * Parse option string pointed to by cp and set mount options accordingly.
272 parseopts(char *cp, struct exportent *ep)
275 squids = ep->e_squids; nsquids = ep->e_nsquids;
276 sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
281 char *opt = strdup(cp);
283 while (*cp && *cp != ',')
286 opt[cp-optstart] = '\0';
290 /* process keyword */
291 if (strcmp(opt, "ro") == 0)
292 ep->e_flags |= NFSEXP_READONLY;
293 else if (strcmp(opt, "rw") == 0)
294 ep->e_flags &= ~NFSEXP_READONLY;
295 else if (!strcmp(opt, "secure"))
296 ep->e_flags &= ~NFSEXP_INSECURE_PORT;
297 else if (!strcmp(opt, "insecure"))
298 ep->e_flags |= NFSEXP_INSECURE_PORT;
299 else if (!strcmp(opt, "sync"))
300 ep->e_flags &= ~NFSEXP_ASYNC;
301 else if (!strcmp(opt, "async"))
302 ep->e_flags |= NFSEXP_ASYNC;
303 else if (!strcmp(opt, "nohide"))
304 ep->e_flags |= NFSEXP_CROSSMNT;
305 else if (!strcmp(opt, "hide"))
306 ep->e_flags &= ~NFSEXP_CROSSMNT;
307 else if (!strcmp(opt, "wdelay"))
308 ep->e_flags |= NFSEXP_GATHERED_WRITES;
309 else if (!strcmp(opt, "no_wdelay"))
310 ep->e_flags &= ~NFSEXP_GATHERED_WRITES;
311 else if (strcmp(opt, "root_squash") == 0)
312 ep->e_flags |= NFSEXP_ROOTSQUASH;
313 else if (!strcmp(opt, "no_root_squash"))
314 ep->e_flags &= ~NFSEXP_ROOTSQUASH;
315 else if (strcmp(opt, "all_squash") == 0)
316 ep->e_flags |= NFSEXP_ALLSQUASH;
317 else if (strcmp(opt, "no_all_squash") == 0)
318 ep->e_flags &= ~NFSEXP_ALLSQUASH;
319 else if (strcmp(opt, "subtree_check") == 0)
320 ep->e_flags &= ~NFSEXP_NOSUBTREECHECK;
321 else if (strcmp(opt, "no_subtree_check") == 0)
322 ep->e_flags |= NFSEXP_NOSUBTREECHECK;
323 else if (strcmp(opt, "auth_nlm") == 0)
324 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
325 else if (strcmp(opt, "no_auth_nlm") == 0)
326 ep->e_flags |= NFSEXP_NOAUTHNLM;
327 else if (strcmp(opt, "secure_locks") == 0)
328 ep->e_flags &= ~NFSEXP_NOAUTHNLM;
329 else if (strcmp(opt, "insecure_locks") == 0)
330 ep->e_flags |= NFSEXP_NOAUTHNLM;
331 else if (strncmp(opt, "mapping=", 8) == 0)
332 ep->e_maptype = parsemaptype(opt+8);
333 else if (strcmp(opt, "map_identity") == 0) /* old style */
334 ep->e_maptype = CLE_MAP_IDENT;
335 else if (strcmp(opt, "map_daemon") == 0) /* old style */
336 ep->e_maptype = CLE_MAP_UGIDD;
337 else if (strncmp(opt, "anonuid=", 8) == 0)
338 ep->e_anonuid = atoi(opt+8);
339 else if (strncmp(opt, "anongid=", 8) == 0)
340 ep->e_anongid = atoi(opt+8);
341 else if (strncmp(opt, "squash_uids=", 12) == 0) {
342 if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
346 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
347 if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
353 "Unknown keyword \"%s\" in export file\n",
355 ep->e_flags |= NFSEXP_ALLSQUASH | NFSEXP_READONLY;
364 ep->e_squids = squids;
365 ep->e_sqgids = sqgids;
366 ep->e_nsquids = nsquids;
367 ep->e_nsqgids = nsqgids;
373 parsesquash(char *list, int **idp, int *lenp, char **ep)
391 if (id0 == -1 || id1 == -1) {
392 syntaxerr("uid/gid -1 not permitted");
396 id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
399 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
402 syntaxerr("bad uid/gid list");
408 if (**ep == ',') (*ep)++;
438 while (isdigit(**cpp))
440 c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
445 parsemaptype(char *type)
447 if (!strcmp(type, "identity"))
448 return CLE_MAP_IDENT;
449 if (!strcmp(type, "ugidd"))
450 return CLE_MAP_UGIDD;
451 if (!strcmp(type, "file"))
453 syntaxerr("invalid map type");
454 return CLE_MAP_IDENT; /* default */
458 getpath(char *path, int len)
461 return xgettok(efp, 0, path, len);
465 getexport(char *exp, int len)
470 if ((ok = xgettok(efp, 0, exp, len)) < 0)
471 xlog(L_ERROR, "error parsing export entry");
478 xlog(L_ERROR, "syntax error in exports file (line %d): %s",