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