]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/exports.c
6e7ed699dfda6863e1bb1c0ef7894d473232f834
[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         { "unix",       AUTH_UNIX               },
46         { "sys",        AUTH_SYS                },
47         { "null",       AUTH_NULL               },
48         { "none",       AUTH_NONE               },
49 };
50
51 const int flav_map_size = sizeof(flav_map)/sizeof(flav_map[0]);
52
53 int export_errno;
54
55 static char     *efname = NULL;
56 static XFILE    *efp = NULL;
57 static int      first;
58 static int      has_default_opts, has_default_subtree_opts;
59 static int      *squids = NULL, nsquids = 0,
60                 *sqgids = NULL, nsqgids = 0;
61
62 static int      getexport(char *exp, int len);
63 static int      getpath(char *path, int len);
64 static int      parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr);
65 static int      parsesquash(char *list, int **idp, int *lenp, char **ep);
66 static int      parsenum(char **cpp);
67 static void     freesquash(void);
68 static void     syntaxerr(char *msg);
69
70 void
71 setexportent(char *fname, char *type)
72 {
73         if (efp)
74                 endexportent();
75         if (!fname)
76                 fname = _PATH_EXPORTS;
77         if (!(efp = xfopen(fname, type)))
78                 xlog(L_ERROR, "can't open %s for %sing",
79                                 fname, strcmp(type, "r")? "writ" : "read");
80         efname = strdup(fname);
81         first = 1;
82 }
83
84 static void init_exportent (struct exportent *ee, int fromkernel)
85 {
86         ee->e_flags = EXPORT_DEFAULT_FLAGS;
87         /* some kernels assume the default is sync rather than
88          * async.  More recent kernels always report one or other,
89          * but this test makes sure we assume same as kernel
90          * Ditto for wgather
91          */
92         if (fromkernel) {
93                 ee->e_flags &= ~NFSEXP_ASYNC;
94                 ee->e_flags &= ~NFSEXP_GATHERED_WRITES;
95         }
96         ee->e_anonuid = 65534;
97         ee->e_anongid = 65534;
98         ee->e_squids = NULL;
99         ee->e_sqgids = NULL;
100         ee->e_mountpoint = NULL;
101         ee->e_fslocmethod = FSLOC_NONE;
102         ee->e_fslocdata = NULL;
103         ee->e_secinfo[0].flav = NULL;
104         ee->e_nsquids = 0;
105         ee->e_nsqgids = 0;
106         ee->e_uuid = NULL;
107         ee->e_ttl = DEFAULT_TTL;
108 }
109
110 struct exportent *
111 getexportent(int fromkernel, int fromexports)
112 {
113         static struct exportent ee, def_ee;
114         char            exp[512], *hostname;
115         char            rpath[MAXPATHLEN+1];
116         char            *opt, *sp;
117         int             ok;
118
119         if (!efp)
120                 return NULL;
121
122         freesquash();
123
124         if (first || (ok = getexport(exp, sizeof(exp))) == 0) {
125                 has_default_opts = 0;
126                 has_default_subtree_opts = 0;
127         
128                 init_exportent(&def_ee, fromkernel);
129
130                 ok = getpath(def_ee.e_path, sizeof(def_ee.e_path));
131                 if (ok <= 0)
132                         return NULL;
133
134                 ok = getexport(exp, sizeof(exp));
135         }
136         if (ok < 0) {
137                 xlog(L_ERROR, "expected client(options...)");
138                 export_errno = EINVAL;
139                 return NULL;
140         }
141         first = 0;
142
143         /*
144          * Check for default options.  The kernel will never have default
145          * options in /proc/fs/nfs/exports, however due to the initial '-' in
146          * the -test-client- string from the test export we have to check that
147          * we're not reading from the kernel.
148          */
149         if (exp[0] == '-' && !fromkernel) {
150                 if (parseopts(exp + 1, &def_ee, 0, &has_default_subtree_opts) < 0)
151                         return NULL;
152                 
153                 has_default_opts = 1;
154
155                 ok = getexport(exp, sizeof(exp));
156                 if (ok < 0) {
157                         xlog(L_ERROR, "expected client(options...)");
158                         export_errno = EINVAL;
159                         return NULL;
160                 }
161         }
162
163         ee = def_ee;
164
165         /* Check for default client */
166         if (ok == 0)
167                 exp[0] = '\0';
168
169         hostname = exp;
170         if ((opt = strchr(exp, '(')) != NULL) {
171                 if (opt == exp) {
172                         xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
173                         hostname = "*";
174                 }
175                 *opt++ = '\0';
176                 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
177                         syntaxerr("bad option list");
178                         export_errno = EINVAL;
179                         return NULL;
180                 }
181                 *sp = '\0';
182         } else {
183                 if (!has_default_opts)
184                         xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
185         }
186         xfree(ee.e_hostname);
187         ee.e_hostname = xstrdup(hostname);
188
189         if (parseopts(opt, &ee, fromexports && !has_default_subtree_opts, NULL) < 0)
190                 return NULL;
191
192         /* resolve symlinks */
193         if (realpath(ee.e_path, rpath) != NULL) {
194                 rpath[sizeof (rpath) - 1] = '\0';
195                 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
196                 ee.e_path[sizeof (ee.e_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         if (src->e_uuid)
339                 dst->e_uuid = strdup(src->e_uuid);
340         dst->e_hostname = NULL;
341 }
342
343 struct exportent *
344 mkexportent(char *hname, char *path, char *options)
345 {
346         static struct exportent ee;
347
348         init_exportent(&ee, 0);
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 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 /*
471  * For those flags which are not allowed to vary by pseudoflavor,
472  * ensure that the export flags agree with the flags on each
473  * pseudoflavor:
474  */
475 static void fix_pseudoflavor_flags(struct exportent *ep)
476 {
477         struct export_features *ef;
478         struct sec_entry *p;
479
480         ef = get_export_features();
481         for (p = ep->e_secinfo; p->flav; p++)
482                 p->flags |= ep->e_flags & ~ef->secinfo_flags;
483 }
484
485 /*
486  * Parse option string pointed to by cp and set mount options accordingly.
487  */
488 static int
489 parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr)
490 {
491         int     had_subtree_opt = 0;
492         char    *flname = efname?efname:"command line";
493         int     flline = efp?efp->x_line:0;
494         unsigned int active = 0;
495
496         squids = ep->e_squids; nsquids = ep->e_nsquids;
497         sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
498         if (!cp)
499                 goto out;
500
501         while (isblank(*cp))
502                 cp++;
503
504         while (*cp) {
505                 char *opt = strdup(cp);
506                 char *optstart = cp;
507                 while (*cp && *cp != ',')
508                         cp++;
509                 if (*cp) {
510                         opt[cp-optstart] = '\0';
511                         cp++;
512                 }
513
514                 /* process keyword */
515                 if (strcmp(opt, "ro") == 0)
516                         setflags(NFSEXP_READONLY, active, ep);
517                 else if (strcmp(opt, "rw") == 0)
518                         clearflags(NFSEXP_READONLY, active, ep);
519                 else if (!strcmp(opt, "secure"))
520                         clearflags(NFSEXP_INSECURE_PORT, active, ep);
521                 else if (!strcmp(opt, "insecure"))
522                         setflags(NFSEXP_INSECURE_PORT, active, ep);
523                 else if (!strcmp(opt, "sync"))
524                         clearflags(NFSEXP_ASYNC, active, ep);
525                 else if (!strcmp(opt, "async"))
526                         setflags(NFSEXP_ASYNC, active, ep);
527                 else if (!strcmp(opt, "nohide"))
528                         setflags(NFSEXP_NOHIDE, active, ep);
529                 else if (!strcmp(opt, "hide"))
530                         clearflags(NFSEXP_NOHIDE, active, ep);
531                 else if (!strcmp(opt, "crossmnt"))
532                         setflags(NFSEXP_CROSSMOUNT, active, ep);
533                 else if (!strcmp(opt, "nocrossmnt"))
534                         clearflags(NFSEXP_CROSSMOUNT, active, ep);
535                 else if (!strcmp(opt, "wdelay"))
536                         setflags(NFSEXP_GATHERED_WRITES, active, ep);
537                 else if (!strcmp(opt, "no_wdelay"))
538                         clearflags(NFSEXP_GATHERED_WRITES, active, ep);
539                 else if (strcmp(opt, "root_squash") == 0)
540                         setflags(NFSEXP_ROOTSQUASH, active, ep);
541                 else if (!strcmp(opt, "no_root_squash"))
542                         clearflags(NFSEXP_ROOTSQUASH, active, ep);
543                 else if (strcmp(opt, "all_squash") == 0)
544                         setflags(NFSEXP_ALLSQUASH, active, ep);
545                 else if (strcmp(opt, "no_all_squash") == 0)
546                         clearflags(NFSEXP_ALLSQUASH, active, ep);
547                 else if (strcmp(opt, "subtree_check") == 0) {
548                         had_subtree_opt = 1;
549                         clearflags(NFSEXP_NOSUBTREECHECK, active, ep);
550                 } else if (strcmp(opt, "no_subtree_check") == 0) {
551                         had_subtree_opt = 1;
552                         setflags(NFSEXP_NOSUBTREECHECK, active, ep);
553                 } else if (strcmp(opt, "auth_nlm") == 0)
554                         clearflags(NFSEXP_NOAUTHNLM, active, ep);
555                 else if (strcmp(opt, "no_auth_nlm") == 0)
556                         setflags(NFSEXP_NOAUTHNLM, active, ep);
557                 else if (strcmp(opt, "secure_locks") == 0)
558                         clearflags(NFSEXP_NOAUTHNLM, active, ep);
559                 else if (strcmp(opt, "insecure_locks") == 0)
560                         setflags(NFSEXP_NOAUTHNLM, active, ep);
561                 else if (strcmp(opt, "acl") == 0)
562                         clearflags(NFSEXP_NOACL, active, ep);
563                 else if (strcmp(opt, "no_acl") == 0)
564                         setflags(NFSEXP_NOACL, active, ep);
565                 else if (strncmp(opt, "anonuid=", 8) == 0) {
566                         char *oe;
567                         ep->e_anonuid = strtol(opt+8, &oe, 10);
568                         if (opt[8]=='\0' || *oe != '\0') {
569                                 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
570                                      flname, flline, opt);      
571 bad_option:
572                                 free(opt);
573                                 export_errno = EINVAL;
574                                 return -1;
575                         }
576                 } else if (strncmp(opt, "anongid=", 8) == 0) {
577                         char *oe;
578                         ep->e_anongid = strtol(opt+8, &oe, 10);
579                         if (opt[8]=='\0' || *oe != '\0') {
580                                 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
581                                      flname, flline, opt);      
582                                 goto bad_option;
583                         }
584                 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
585                         if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
586                                 goto bad_option;
587                         }
588                 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
589                         if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
590                                 goto bad_option;
591                         }
592                 } else if (strncmp(opt, "fsid=", 5) == 0) {
593                         char *oe;
594                         if (strcmp(opt+5, "root") == 0) {
595                                 ep->e_fsid = 0;
596                                 setflags(NFSEXP_FSID, active, ep);
597                         } else {
598                                 ep->e_fsid = strtoul(opt+5, &oe, 0);
599                                 if (opt[5]!='\0' && *oe == '\0') 
600                                         setflags(NFSEXP_FSID, active, ep);
601                                 else if (valid_uuid(opt+5))
602                                         ep->e_uuid = strdup(opt+5);
603                                 else {
604                                         xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
605                                              flname, flline, opt);      
606                                         goto bad_option;
607                                 }
608                         }
609                 } else if (strcmp(opt, "mountpoint")==0 ||
610                            strcmp(opt, "mp") == 0 ||
611                            strncmp(opt, "mountpoint=", 11)==0 ||
612                            strncmp(opt, "mp=", 3) == 0) {
613                         char * mp = strchr(opt, '=');
614                         if (mp)
615                                 ep->e_mountpoint = strdup(mp+1);
616                         else
617                                 ep->e_mountpoint = strdup("");
618 #ifdef DEBUG
619                 } else if (strncmp(opt, "fsloc=", 6) == 0) {
620                         if (strcmp(opt+6, "stub") == 0)
621                                 ep->e_fslocmethod = FSLOC_STUB;
622                         else {
623                                 xlog(L_ERROR, "%s:%d: bad option %s\n",
624                                      flname, flline, opt);
625                                 goto bad_option;
626                         }
627 #endif
628                 } else if (strncmp(opt, "refer=", 6) == 0) {
629                         ep->e_fslocmethod = FSLOC_REFER;
630                         ep->e_fslocdata = strdup(opt+6);
631                 } else if (strncmp(opt, "replicas=", 9) == 0) {
632                         ep->e_fslocmethod = FSLOC_REPLICA;
633                         ep->e_fslocdata = strdup(opt+9);
634                 } else if (strncmp(opt, "sec=", 4) == 0) {
635                         active = parse_flavors(opt+4, ep);
636                         if (!active)
637                                 goto bad_option;
638                 } else {
639                         xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
640                                         flname, flline, opt);
641                         setflags(NFSEXP_ALLSQUASH | NFSEXP_READONLY, active, ep);
642                         goto bad_option;
643                 }
644                 free(opt);
645                 while (isblank(*cp))
646                         cp++;
647         }
648
649         fix_pseudoflavor_flags(ep);
650         ep->e_squids = squids;
651         ep->e_sqgids = sqgids;
652         ep->e_nsquids = nsquids;
653         ep->e_nsqgids = nsqgids;
654
655 out:
656         if (warn && !had_subtree_opt)
657                 xlog(L_WARNING, "%s [%d]: Neither 'subtree_check' or 'no_subtree_check' specified for export \"%s:%s\".\n"
658                                 "  Assuming default behaviour ('no_subtree_check').\n"
659                                 "  NOTE: this default has changed since nfs-utils version 1.0.x\n",
660
661                                 flname, flline,
662                                 ep->e_hostname, ep->e_path);
663         if (had_subtree_opt_ptr)
664                 *had_subtree_opt_ptr = had_subtree_opt;
665
666         return 1;
667 }
668
669 static int
670 parsesquash(char *list, int **idp, int *lenp, char **ep)
671 {
672         char    *cp = list;
673         int     id0, id1;
674         int     len = *lenp;
675         int     *id = *idp;
676
677         if (**ep)
678             *--(*ep) = ',';
679
680         do {
681                 id0 = parsenum(&cp);
682                 if (*cp == '-') {
683                         cp++;
684                         id1 = parsenum(&cp);
685                 } else {
686                         id1 = id0;
687                 }
688                 if (id0 == -1 || id1 == -1) {
689                         syntaxerr("uid/gid -1 not permitted");
690                         return -1;
691                 }
692                 if ((len % 8) == 0)
693                         id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
694                 id[len++] = id0;
695                 id[len++] = id1;
696                 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
697                         break;
698                 if (*cp != ',') {
699                         syntaxerr("bad uid/gid list");
700                         return -1;
701                 }
702                 cp++;
703         } while(1);
704
705         if (**ep == ',') (*ep)++;
706
707         *lenp = len;
708         *idp = id;
709         return 1;
710 }
711
712 static void
713 freesquash(void)
714 {
715         if (squids) {
716                 xfree (squids);
717                 squids = NULL;
718                 nsquids = 0;
719         }
720         if (sqgids) {
721                 xfree (sqgids);
722                 sqgids = NULL;
723                 nsqgids = 0;
724         }
725 }
726
727 static int
728 parsenum(char **cpp)
729 {
730         char    *cp = *cpp, c;
731         int     num = 0;
732
733         if (**cpp == '-')
734                 (*cpp)++;
735         while (isdigit(**cpp))
736                 (*cpp)++;
737         c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
738         return num;
739 }
740
741 static int
742 getpath(char *path, int len)
743 {
744         xskip(efp, " \t\n");
745         return xgettok(efp, 0, path, len);
746 }
747
748 static int
749 getexport(char *exp, int len)
750 {
751         int     ok;
752
753         xskip(efp, " \t");
754         if ((ok = xgettok(efp, 0, exp, len)) < 0)
755                 xlog(L_ERROR, "%s:%d: syntax error",
756                         efname?"command line":efname, efp->x_line);
757         return ok;
758 }
759
760 static void
761 syntaxerr(char *msg)
762 {
763         xlog(L_ERROR, "%s:%d: syntax error: %s",
764                         efname, efp?efp->x_line:0, msg);
765 }
766 struct export_features *get_export_features(void)
767 {
768         static char *path = "/proc/fs/nfsd/export_features";
769         static struct export_features ef;
770         static int cached = 0;
771         char buf[50];
772         int c;
773         int fd;
774
775         if (cached)
776                 return &ef;
777
778         ef.flags = NFSEXP_OLDFLAGS;
779         ef.secinfo_flags = NFSEXP_OLD_SECINFO_FLAGS;
780
781         fd = open(path, O_RDONLY);
782         if (fd == -1)
783                 goto good;
784         c = read(fd, buf, 50);
785         close(fd);
786         if (c == -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 }