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