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