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