]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/exports.c
exports: common exportent initializer
[nfs-utils.git] / support / nfs / exports.c
1 /*
2  * support/nfs/export.c
3  *
4  * Parse the exports file. Derived from the unfsd implementation.
5  *
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>
11  *
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.
15  */
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <sys/param.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include "nfslib.h"
29 #include "exportfs.h"
30 #include "xmalloc.h"
31 #include "xlog.h"
32 #include "xio.h"
33 #include "pseudoflavors.h"
34
35 #define EXPORT_DEFAULT_FLAGS    \
36   (NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES|NFSEXP_NOSUBTREECHECK)
37
38 struct flav_info flav_map[] = {
39         { "krb5",       RPC_AUTH_GSS_KRB5       },
40         { "krb5i",      RPC_AUTH_GSS_KRB5I      },
41         { "krb5p",      RPC_AUTH_GSS_KRB5P      },
42         { "lipkey",     RPC_AUTH_GSS_LKEY       },
43         { "lipkey-i",   RPC_AUTH_GSS_LKEYI      },
44         { "lipkey-p",   RPC_AUTH_GSS_LKEYP      },
45         { "spkm3",      RPC_AUTH_GSS_SPKM       },
46         { "spkm3i",     RPC_AUTH_GSS_SPKMI      },
47         { "spkm3p",     RPC_AUTH_GSS_SPKMP      },
48         { "unix",       AUTH_UNIX               },
49         { "sys",        AUTH_SYS                },
50         { "null",       AUTH_NULL               },
51         { "none",       AUTH_NONE               },
52 };
53
54 const int flav_map_size = sizeof(flav_map)/sizeof(flav_map[0]);
55
56 int export_errno;
57
58 static char     *efname = NULL;
59 static XFILE    *efp = NULL;
60 static int      first;
61 static int      has_default_opts, has_default_subtree_opts;
62 static int      *squids = NULL, nsquids = 0,
63                 *sqgids = NULL, nsqgids = 0;
64
65 static int      getexport(char *exp, int len);
66 static int      getpath(char *path, int len);
67 static int      parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr);
68 static int      parsesquash(char *list, int **idp, int *lenp, char **ep);
69 static int      parsenum(char **cpp);
70 static void     freesquash(void);
71 static void     syntaxerr(char *msg);
72
73 void
74 setexportent(char *fname, char *type)
75 {
76         if (efp)
77                 endexportent();
78         if (!fname)
79                 fname = _PATH_EXPORTS;
80         if (!(efp = xfopen(fname, type)))
81                 xlog(L_ERROR, "can't open %s for %sing",
82                                 fname, strcmp(type, "r")? "writ" : "read");
83         efname = strdup(fname);
84         first = 1;
85 }
86
87 static void init_exportent (struct exportent *ee, int fromkernel)
88 {
89         ee->e_flags = EXPORT_DEFAULT_FLAGS;
90         /* some kernels assume the default is sync rather than
91          * async.  More recent kernels always report one or other,
92          * but this test makes sure we assume same as kernel
93          * Ditto for wgather
94          */
95         if (fromkernel) {
96                 ee->e_flags &= ~NFSEXP_ASYNC;
97                 ee->e_flags &= ~NFSEXP_GATHERED_WRITES;
98         }
99         ee->e_anonuid = 65534;
100         ee->e_anongid = 65534;
101         ee->e_squids = NULL;
102         ee->e_sqgids = NULL;
103         ee->e_mountpoint = NULL;
104         ee->e_fslocmethod = FSLOC_NONE;
105         ee->e_fslocdata = NULL;
106         ee->e_secinfo[0].flav = NULL;
107         ee->e_nsquids = 0;
108         ee->e_nsqgids = 0;
109         ee->e_uuid = NULL;
110 }
111
112 struct exportent *
113 getexportent(int fromkernel, int fromexports)
114 {
115         static struct exportent ee, def_ee;
116         char            exp[512], *hostname;
117         char            rpath[MAXPATHLEN+1];
118         char            *opt, *sp;
119         int             ok;
120
121         if (!efp)
122                 return NULL;
123
124         freesquash();
125
126         if (first || (ok = getexport(exp, sizeof(exp))) == 0) {
127                 has_default_opts = 0;
128                 has_default_subtree_opts = 0;
129         
130                 init_exportent(&def_ee, fromkernel);
131
132                 ok = getpath(def_ee.e_path, sizeof(def_ee.e_path));
133                 if (ok <= 0)
134                         return NULL;
135
136                 ok = getexport(exp, sizeof(exp));
137         }
138         if (ok < 0) {
139                 xlog(L_ERROR, "expected client(options...)");
140                 export_errno = EINVAL;
141                 return NULL;
142         }
143         first = 0;
144                 
145         /* Check for default options */
146         if (exp[0] == '-') {
147                 if (parseopts(exp + 1, &def_ee, 0, &has_default_subtree_opts) < 0)
148                         return NULL;
149                 
150                 has_default_opts = 1;
151
152                 ok = getexport(exp, sizeof(exp));
153                 if (ok < 0) {
154                         xlog(L_ERROR, "expected client(options...)");
155                         export_errno = EINVAL;
156                         return NULL;
157                 }
158         }
159
160         ee = def_ee;
161
162         /* Check for default client */
163         if (ok == 0)
164                 exp[0] = '\0';
165
166         hostname = exp;
167         if ((opt = strchr(exp, '(')) != NULL) {
168                 if (opt == exp) {
169                         xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
170                         hostname = "*";
171                 }
172                 *opt++ = '\0';
173                 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
174                         syntaxerr("bad option list");
175                         export_errno = EINVAL;
176                         return NULL;
177                 }
178                 *sp = '\0';
179         } else {
180                 if (!has_default_opts)
181                         xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
182         }
183         xfree(ee.e_hostname);
184         ee.e_hostname = xstrdup(hostname);
185
186         if (parseopts(opt, &ee, fromexports && !has_default_subtree_opts, NULL) < 0)
187                 return NULL;
188
189         /* resolve symlinks */
190         if (realpath(ee.e_path, rpath) != NULL) {
191                 rpath[sizeof (rpath) - 1] = '\0';
192                 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
193                 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
194         }
195
196         return &ee;
197 }
198
199 void secinfo_show(FILE *fp, struct exportent *ep)
200 {
201         struct sec_entry *p1, *p2;
202         int flags;
203
204         for (p1=ep->e_secinfo; p1->flav; p1=p2) {
205
206                 fprintf(fp, ",sec=%s", p1->flav->flavour);
207                 for (p2=p1+1; (p2->flav != NULL) && (p1->flags == p2->flags);
208                                                                 p2++) {
209                         fprintf(fp, ":%s", p2->flav->flavour);
210                 }
211                 flags = p1->flags;
212                 fprintf(fp, ",%s", (flags & NFSEXP_READONLY) ? "ro" : "rw");
213                 fprintf(fp, ",%sroot_squash", (flags & NFSEXP_ROOTSQUASH)?
214                                 "" : "no_");
215                 fprintf(fp, ",%sall_squash", (flags & NFSEXP_ALLSQUASH)?
216                                 "" : "no_");
217         }
218 }
219
220 void
221 putexportent(struct exportent *ep)
222 {
223         FILE    *fp;
224         int     *id, i;
225         char    *esc=ep->e_path;
226
227         if (!efp)
228                 return;
229
230         fp = efp->x_fp;
231         for (i=0; esc[i]; i++)
232                 if (iscntrl(esc[i]) || esc[i] == '"' || esc[i] == '\\' || esc[i] == '#' || isspace(esc[i]))
233                         fprintf(fp, "\\%03o", esc[i]);
234                 else
235                         fprintf(fp, "%c", esc[i]);
236
237         fprintf(fp, "\t%s(", ep->e_hostname);
238         fprintf(fp, "%s,", (ep->e_flags & NFSEXP_READONLY)? "ro" : "rw");
239         fprintf(fp, "%ssync,", (ep->e_flags & NFSEXP_ASYNC)? "a" : "");
240         fprintf(fp, "%swdelay,", (ep->e_flags & NFSEXP_GATHERED_WRITES)?
241                                 "" : "no_");
242         fprintf(fp, "%shide,", (ep->e_flags & NFSEXP_NOHIDE)?
243                                 "no" : "");
244         fprintf(fp, "%scrossmnt,", (ep->e_flags & NFSEXP_CROSSMOUNT)?
245                                 "" : "no");
246         fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
247                                 "in" : "");
248         fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
249                                 "" : "no_");
250         fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
251                                 "" : "no_");
252         fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
253                 "no_" : "");
254         fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
255                 "in" : "");
256         fprintf(fp, "%sacl,", (ep->e_flags & NFSEXP_NOACL)?
257                 "no_" : "");
258         if (ep->e_flags & NFSEXP_FSID) {
259                 fprintf(fp, "fsid=%d,", ep->e_fsid);
260         }
261         if (ep->e_uuid)
262                 fprintf(fp, "fsid=%s,", ep->e_uuid);
263         if (ep->e_mountpoint)
264                 fprintf(fp, "mountpoint%s%s,",
265                         ep->e_mountpoint[0]?"=":"", ep->e_mountpoint);
266         switch (ep->e_fslocmethod) {
267         case FSLOC_NONE:
268                 break;
269         case FSLOC_REFER:
270                 fprintf(fp, "refer=%s,", ep->e_fslocdata);
271                 break;
272         case FSLOC_REPLICA:
273                 fprintf(fp, "replicas=%s,", ep->e_fslocdata);
274                 break;
275 #ifdef DEBUG
276         case FSLOC_STUB:
277                 fprintf(fp, "fsloc=stub,");
278                 break;
279 #endif
280         default:
281                 xlog(L_ERROR, "unknown fsloc method for %s:%s",
282                      ep->e_hostname, ep->e_path);
283         }
284         if ((id = ep->e_squids) != NULL) {
285                 fprintf(fp, "squash_uids=");
286                 for (i = 0; i < ep->e_nsquids; i += 2)
287                         if (id[i] != id[i+1])
288                                 fprintf(fp, "%d-%d,", id[i], id[i+1]);
289                         else
290                                 fprintf(fp, "%d,", id[i]);
291         }
292         if ((id = ep->e_sqgids) != NULL) {
293                 fprintf(fp, "squash_gids=");
294                 for (i = 0; i < ep->e_nsquids; i += 2)
295                         if (id[i] != id[i+1])
296                                 fprintf(fp, "%d-%d,", id[i], id[i+1]);
297                         else
298                                 fprintf(fp, "%d,", id[i]);
299         }
300         fprintf(fp, "anonuid=%d,anongid=%d", ep->e_anonuid, ep->e_anongid);
301         secinfo_show(fp, ep);
302         fprintf(fp, ")\n");
303 }
304
305 void
306 endexportent(void)
307 {
308         if (efp)
309                 xfclose(efp);
310         efp = NULL;
311         if (efname)
312                 free(efname);
313         efname = NULL;
314         freesquash();
315 }
316
317 void
318 dupexportent(struct exportent *dst, struct exportent *src)
319 {
320         int     n;
321
322         *dst = *src;
323         if ((n = src->e_nsquids) != 0) {
324                 dst->e_squids = (int *) xmalloc(n * sizeof(int));
325                 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
326         }
327         if ((n = src->e_nsqgids) != 0) {
328                 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
329                 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
330         }
331         if (src->e_mountpoint)
332                 dst->e_mountpoint = strdup(src->e_mountpoint);
333         if (src->e_fslocdata)
334                 dst->e_fslocdata = strdup(src->e_fslocdata);
335         dst->e_hostname = NULL;
336 }
337
338 struct exportent *
339 mkexportent(char *hname, char *path, char *options)
340 {
341         static struct exportent ee;
342
343         init_exportent(&ee, 0);
344
345         xfree(ee.e_hostname);
346         ee.e_hostname = xstrdup(hname);
347
348         if (strlen(path) >= sizeof(ee.e_path)) {
349                 xlog(L_WARNING, "path name %s too long", path);
350                 return NULL;
351         }
352         strncpy(ee.e_path, path, sizeof (ee.e_path));
353         ee.e_path[sizeof (ee.e_path) - 1] = '\0';
354         if (parseopts(options, &ee, 0, NULL) < 0)
355                 return NULL;
356         return &ee;
357 }
358
359 int
360 updateexportent(struct exportent *eep, char *options)
361 {
362         if (parseopts(options, eep, 0, NULL) < 0)
363                 return 0;
364         return 1;
365 }
366
367
368 static int valid_uuid(char *uuid)
369 {
370         /* must have 32 hex digits */
371         int cnt;
372         for (cnt = 0 ; *uuid; uuid++)
373                 if (isxdigit(*uuid))
374                         cnt++;
375         return cnt == 32;
376 }
377
378 /*
379  * Append the given flavor to the exportent's e_secinfo array, or
380  * do nothing if it's already there.  Returns the index of flavor
381  * in the resulting array in any case.
382  */
383 int secinfo_addflavor(struct flav_info *flav, struct exportent *ep)
384 {
385         struct sec_entry *p;
386
387         for (p=ep->e_secinfo; p->flav; p++) {
388                 if (p->flav == flav)
389                         return p - ep->e_secinfo;
390         }
391         if (p - ep->e_secinfo >= SECFLAVOR_COUNT) {
392                 xlog(L_ERROR, "more than %d security flavors on an export\n",
393                         SECFLAVOR_COUNT);
394                 return -1;
395         }
396         p->flav = flav;
397         p->flags = ep->e_flags;
398         (p+1)->flav = NULL;
399         return p - ep->e_secinfo;
400 }
401
402 static struct flav_info *find_flavor(char *name)
403 {
404         struct flav_info *flav;
405         for (flav = flav_map; flav < flav_map + flav_map_size; flav++)
406                 if (strcmp(flav->flavour, name) == 0)
407                         return flav;
408         return NULL;
409 }
410
411 /* @str is a colon seperated list of security flavors.  Their order
412  * is recorded in @ep, and a bitmap corresponding to the list is returned.
413  * A zero return indicates an error.
414  */
415 static unsigned int parse_flavors(char *str, struct exportent *ep)
416 {
417         unsigned int out=0;
418         char *flavor;
419         int bit;
420
421         while ( (flavor=strsep(&str, ":")) ) {
422                 struct flav_info *flav = find_flavor(flavor);
423                 if (flav == NULL) {
424                         xlog(L_ERROR, "unknown flavor %s\n", flavor);
425                         return 0;
426                 }
427                 bit = secinfo_addflavor(flav, ep);
428                 if (bit < 0)
429                         return 0;
430                 out |= 1<<bit;
431         }
432         return out;
433 }
434
435 /* Sets the bits in @mask for the appropriate security flavor flags. */
436 static void setflags(int mask, unsigned int active, struct exportent *ep)
437 {
438         int bit=0;
439
440         ep->e_flags |= mask;
441
442         while (active) {
443                 if (active & 1)
444                         ep->e_secinfo[bit].flags |= mask;
445                 bit++;
446                 active >>= 1;
447         }
448 }
449
450 /* Clears the bits in @mask for the appropriate security flavor flags. */
451 static void clearflags(int mask, unsigned int active, struct exportent *ep)
452 {
453         int bit=0;
454
455         ep->e_flags &= ~mask;
456
457         while (active) {
458                 if (active & 1)
459                         ep->e_secinfo[bit].flags &= ~mask;
460                 bit++;
461                 active >>= 1;
462         }
463 }
464
465 /*
466  * For those flags which are not allowed to vary by pseudoflavor,
467  * ensure that the export flags agree with the flags on each
468  * pseudoflavor:
469  */
470 static void fix_pseudoflavor_flags(struct exportent *ep)
471 {
472         struct export_features *ef;
473         struct sec_entry *p;
474
475         ef = get_export_features();
476         for (p = ep->e_secinfo; p->flav; p++)
477                 p->flags |= ep->e_flags & ~ef->secinfo_flags;
478 }
479
480 /*
481  * Parse option string pointed to by cp and set mount options accordingly.
482  */
483 static int
484 parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr)
485 {
486         int     had_subtree_opt = 0;
487         char    *flname = efname?efname:"command line";
488         int     flline = efp?efp->x_line:0;
489         unsigned int active = 0;
490
491         squids = ep->e_squids; nsquids = ep->e_nsquids;
492         sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
493         if (!cp)
494                 goto out;
495
496         while (isblank(*cp))
497                 cp++;
498
499         while (*cp) {
500                 char *opt = strdup(cp);
501                 char *optstart = cp;
502                 while (*cp && *cp != ',')
503                         cp++;
504                 if (*cp) {
505                         opt[cp-optstart] = '\0';
506                         cp++;
507                 }
508
509                 /* process keyword */
510                 if (strcmp(opt, "ro") == 0)
511                         setflags(NFSEXP_READONLY, active, ep);
512                 else if (strcmp(opt, "rw") == 0)
513                         clearflags(NFSEXP_READONLY, active, ep);
514                 else if (!strcmp(opt, "secure"))
515                         clearflags(NFSEXP_INSECURE_PORT, active, ep);
516                 else if (!strcmp(opt, "insecure"))
517                         setflags(NFSEXP_INSECURE_PORT, active, ep);
518                 else if (!strcmp(opt, "sync"))
519                         clearflags(NFSEXP_ASYNC, active, ep);
520                 else if (!strcmp(opt, "async"))
521                         setflags(NFSEXP_ASYNC, active, ep);
522                 else if (!strcmp(opt, "nohide"))
523                         setflags(NFSEXP_NOHIDE, active, ep);
524                 else if (!strcmp(opt, "hide"))
525                         clearflags(NFSEXP_NOHIDE, active, ep);
526                 else if (!strcmp(opt, "crossmnt"))
527                         setflags(NFSEXP_CROSSMOUNT, active, ep);
528                 else if (!strcmp(opt, "nocrossmnt"))
529                         clearflags(NFSEXP_CROSSMOUNT, active, ep);
530                 else if (!strcmp(opt, "wdelay"))
531                         setflags(NFSEXP_GATHERED_WRITES, active, ep);
532                 else if (!strcmp(opt, "no_wdelay"))
533                         clearflags(NFSEXP_GATHERED_WRITES, active, ep);
534                 else if (strcmp(opt, "root_squash") == 0)
535                         setflags(NFSEXP_ROOTSQUASH, active, ep);
536                 else if (!strcmp(opt, "no_root_squash"))
537                         clearflags(NFSEXP_ROOTSQUASH, active, ep);
538                 else if (strcmp(opt, "all_squash") == 0)
539                         setflags(NFSEXP_ALLSQUASH, active, ep);
540                 else if (strcmp(opt, "no_all_squash") == 0)
541                         clearflags(NFSEXP_ALLSQUASH, active, ep);
542                 else if (strcmp(opt, "subtree_check") == 0) {
543                         had_subtree_opt = 1;
544                         clearflags(NFSEXP_NOSUBTREECHECK, active, ep);
545                 } else if (strcmp(opt, "no_subtree_check") == 0) {
546                         had_subtree_opt = 1;
547                         setflags(NFSEXP_NOSUBTREECHECK, active, ep);
548                 } else if (strcmp(opt, "auth_nlm") == 0)
549                         clearflags(NFSEXP_NOAUTHNLM, active, ep);
550                 else if (strcmp(opt, "no_auth_nlm") == 0)
551                         setflags(NFSEXP_NOAUTHNLM, active, ep);
552                 else if (strcmp(opt, "secure_locks") == 0)
553                         clearflags(NFSEXP_NOAUTHNLM, active, ep);
554                 else if (strcmp(opt, "insecure_locks") == 0)
555                         setflags(NFSEXP_NOAUTHNLM, active, ep);
556                 else if (strcmp(opt, "acl") == 0)
557                         clearflags(NFSEXP_NOACL, active, ep);
558                 else if (strcmp(opt, "no_acl") == 0)
559                         setflags(NFSEXP_NOACL, active, ep);
560                 else if (strncmp(opt, "anonuid=", 8) == 0) {
561                         char *oe;
562                         ep->e_anonuid = strtol(opt+8, &oe, 10);
563                         if (opt[8]=='\0' || *oe != '\0') {
564                                 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
565                                      flname, flline, opt);      
566 bad_option:
567                                 free(opt);
568                                 export_errno = EINVAL;
569                                 return -1;
570                         }
571                 } else if (strncmp(opt, "anongid=", 8) == 0) {
572                         char *oe;
573                         ep->e_anongid = strtol(opt+8, &oe, 10);
574                         if (opt[8]=='\0' || *oe != '\0') {
575                                 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
576                                      flname, flline, opt);      
577                                 goto bad_option;
578                         }
579                 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
580                         if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
581                                 goto bad_option;
582                         }
583                 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
584                         if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
585                                 goto bad_option;
586                         }
587                 } else if (strncmp(opt, "fsid=", 5) == 0) {
588                         char *oe;
589                         if (strcmp(opt+5, "root") == 0) {
590                                 ep->e_fsid = 0;
591                                 setflags(NFSEXP_FSID, active, ep);
592                         } else {
593                                 ep->e_fsid = strtoul(opt+5, &oe, 0);
594                                 if (opt[5]!='\0' && *oe == '\0') 
595                                         setflags(NFSEXP_FSID, active, ep);
596                                 else if (valid_uuid(opt+5))
597                                         ep->e_uuid = strdup(opt+5);
598                                 else {
599                                         xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
600                                              flname, flline, opt);      
601                                         goto bad_option;
602                                 }
603                         }
604                 } else if (strcmp(opt, "mountpoint")==0 ||
605                            strcmp(opt, "mp") == 0 ||
606                            strncmp(opt, "mountpoint=", 11)==0 ||
607                            strncmp(opt, "mp=", 3) == 0) {
608                         char * mp = strchr(opt, '=');
609                         if (mp)
610                                 ep->e_mountpoint = strdup(mp+1);
611                         else
612                                 ep->e_mountpoint = strdup("");
613 #ifdef DEBUG
614                 } else if (strncmp(opt, "fsloc=", 6) == 0) {
615                         if (strcmp(opt+6, "stub") == 0)
616                                 ep->e_fslocmethod = FSLOC_STUB;
617                         else {
618                                 xlog(L_ERROR, "%s:%d: bad option %s\n",
619                                      flname, flline, opt);
620                                 goto bad_option;
621                         }
622 #endif
623                 } else if (strncmp(opt, "refer=", 6) == 0) {
624                         ep->e_fslocmethod = FSLOC_REFER;
625                         ep->e_fslocdata = strdup(opt+6);
626                 } else if (strncmp(opt, "replicas=", 9) == 0) {
627                         ep->e_fslocmethod = FSLOC_REPLICA;
628                         ep->e_fslocdata = strdup(opt+9);
629                 } else if (strncmp(opt, "sec=", 4) == 0) {
630                         active = parse_flavors(opt+4, ep);
631                         if (!active)
632                                 goto bad_option;
633                 } else {
634                         xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
635                                         flname, flline, opt);
636                         setflags(NFSEXP_ALLSQUASH | NFSEXP_READONLY, active, ep);
637                         goto bad_option;
638                 }
639                 free(opt);
640                 while (isblank(*cp))
641                         cp++;
642         }
643
644         /*
645          * Turn on nohide which will allow this export to cross over
646          * the 'mount --bind' mount point.
647          */
648         if (ep->e_fslocdata)
649                 setflags(NFSEXP_NOHIDE, active, ep);
650         fix_pseudoflavor_flags(ep);
651         ep->e_squids = squids;
652         ep->e_sqgids = sqgids;
653         ep->e_nsquids = nsquids;
654         ep->e_nsqgids = nsqgids;
655
656 out:
657         if (warn && !had_subtree_opt)
658                 xlog(L_WARNING, "%s [%d]: Neither 'subtree_check' or 'no_subtree_check' specified for export \"%s:%s\".\n"
659                                 "  Assuming default behaviour ('no_subtree_check').\n"
660                                 "  NOTE: this default has changed since nfs-utils version 1.0.x\n",
661
662                                 flname, flline,
663                                 ep->e_hostname, ep->e_path);
664         if (had_subtree_opt_ptr)
665                 *had_subtree_opt_ptr = had_subtree_opt;
666
667         return 1;
668 }
669
670 static int
671 parsesquash(char *list, int **idp, int *lenp, char **ep)
672 {
673         char    *cp = list;
674         int     id0, id1;
675         int     len = *lenp;
676         int     *id = *idp;
677
678         if (**ep)
679             *--(*ep) = ',';
680
681         do {
682                 id0 = parsenum(&cp);
683                 if (*cp == '-') {
684                         cp++;
685                         id1 = parsenum(&cp);
686                 } else {
687                         id1 = id0;
688                 }
689                 if (id0 == -1 || id1 == -1) {
690                         syntaxerr("uid/gid -1 not permitted");
691                         return -1;
692                 }
693                 if ((len % 8) == 0)
694                         id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
695                 id[len++] = id0;
696                 id[len++] = id1;
697                 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
698                         break;
699                 if (*cp != ',') {
700                         syntaxerr("bad uid/gid list");
701                         return -1;
702                 }
703                 cp++;
704         } while(1);
705
706         if (**ep == ',') (*ep)++;
707
708         *lenp = len;
709         *idp = id;
710         return 1;
711 }
712
713 static void
714 freesquash(void)
715 {
716         if (squids) {
717                 xfree (squids);
718                 squids = NULL;
719                 nsquids = 0;
720         }
721         if (sqgids) {
722                 xfree (sqgids);
723                 sqgids = NULL;
724                 nsqgids = 0;
725         }
726 }
727
728 static int
729 parsenum(char **cpp)
730 {
731         char    *cp = *cpp, c;
732         int     num = 0;
733
734         if (**cpp == '-')
735                 (*cpp)++;
736         while (isdigit(**cpp))
737                 (*cpp)++;
738         c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
739         return num;
740 }
741
742 static int
743 getpath(char *path, int len)
744 {
745         xskip(efp, " \t\n");
746         return xgettok(efp, 0, path, len);
747 }
748
749 static int
750 getexport(char *exp, int len)
751 {
752         int     ok;
753
754         xskip(efp, " \t");
755         if ((ok = xgettok(efp, 0, exp, len)) < 0)
756                 xlog(L_ERROR, "%s:%d: syntax error",
757                         efname?"command line":efname, efp->x_line);
758         return ok;
759 }
760
761 static void
762 syntaxerr(char *msg)
763 {
764         xlog(L_ERROR, "%s:%d: syntax error: %s",
765                         efname, efp?efp->x_line:0, msg);
766 }
767 struct export_features *get_export_features(void)
768 {
769         static char *path = "/proc/fs/nfsd/export_features";
770         static struct export_features ef;
771         static int cached = 0;
772         char buf[50];
773         int c;
774         int fd;
775
776         if (cached)
777                 return &ef;
778
779         ef.flags = NFSEXP_OLDFLAGS;
780         ef.secinfo_flags = NFSEXP_OLD_SECINFO_FLAGS;
781
782         fd = open(path, O_RDONLY);
783         if (fd == -1)
784                 goto good;
785         fd = read(fd, buf, 50);
786         if (fd == -1)
787                 goto err;
788         c = sscanf(buf, "%x %x", &ef.flags, &ef.secinfo_flags);
789         if (c != 2)
790                 goto err;
791 good:
792         cached = 1;
793         return &ef;
794 err:
795         xlog(L_WARNING, "unexpected error reading %s", path);
796         return &ef;
797 }