]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/exports.c
c250383a1b2fe2ada93bc5c8bf172fc02e3ad49b
[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         /*
147          * Check for default options.  The kernel will never have default
148          * options in /proc/fs/nfs/exports, however due to the initial '-' in
149          * the -test-client- string from the test export we have to check that
150          * we're not reading from the kernel.
151          */
152         if (exp[0] == '-' && !fromkernel) {
153                 if (parseopts(exp + 1, &def_ee, 0, &has_default_subtree_opts) < 0)
154                         return NULL;
155                 
156                 has_default_opts = 1;
157
158                 ok = getexport(exp, sizeof(exp));
159                 if (ok < 0) {
160                         xlog(L_ERROR, "expected client(options...)");
161                         export_errno = EINVAL;
162                         return NULL;
163                 }
164         }
165
166         ee = def_ee;
167
168         /* Check for default client */
169         if (ok == 0)
170                 exp[0] = '\0';
171
172         hostname = exp;
173         if ((opt = strchr(exp, '(')) != NULL) {
174                 if (opt == exp) {
175                         xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
176                         hostname = "*";
177                 }
178                 *opt++ = '\0';
179                 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
180                         syntaxerr("bad option list");
181                         export_errno = EINVAL;
182                         return NULL;
183                 }
184                 *sp = '\0';
185         } else {
186                 if (!has_default_opts)
187                         xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
188         }
189         xfree(ee.e_hostname);
190         ee.e_hostname = xstrdup(hostname);
191
192         if (parseopts(opt, &ee, fromexports && !has_default_subtree_opts, NULL) < 0)
193                 return NULL;
194
195         /* resolve symlinks */
196         if (realpath(ee.e_path, rpath) != NULL) {
197                 rpath[sizeof (rpath) - 1] = '\0';
198                 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
199                 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
200         }
201
202         return &ee;
203 }
204
205 void secinfo_show(FILE *fp, struct exportent *ep)
206 {
207         struct sec_entry *p1, *p2;
208         int flags;
209
210         for (p1=ep->e_secinfo; p1->flav; p1=p2) {
211
212                 fprintf(fp, ",sec=%s", p1->flav->flavour);
213                 for (p2=p1+1; (p2->flav != NULL) && (p1->flags == p2->flags);
214                                                                 p2++) {
215                         fprintf(fp, ":%s", p2->flav->flavour);
216                 }
217                 flags = p1->flags;
218                 fprintf(fp, ",%s", (flags & NFSEXP_READONLY) ? "ro" : "rw");
219                 fprintf(fp, ",%sroot_squash", (flags & NFSEXP_ROOTSQUASH)?
220                                 "" : "no_");
221                 fprintf(fp, ",%sall_squash", (flags & NFSEXP_ALLSQUASH)?
222                                 "" : "no_");
223         }
224 }
225
226 void
227 putexportent(struct exportent *ep)
228 {
229         FILE    *fp;
230         int     *id, i;
231         char    *esc=ep->e_path;
232
233         if (!efp)
234                 return;
235
236         fp = efp->x_fp;
237         for (i=0; esc[i]; i++)
238                 if (iscntrl(esc[i]) || esc[i] == '"' || esc[i] == '\\' || esc[i] == '#' || isspace(esc[i]))
239                         fprintf(fp, "\\%03o", esc[i]);
240                 else
241                         fprintf(fp, "%c", esc[i]);
242
243         fprintf(fp, "\t%s(", ep->e_hostname);
244         fprintf(fp, "%s,", (ep->e_flags & NFSEXP_READONLY)? "ro" : "rw");
245         fprintf(fp, "%ssync,", (ep->e_flags & NFSEXP_ASYNC)? "a" : "");
246         fprintf(fp, "%swdelay,", (ep->e_flags & NFSEXP_GATHERED_WRITES)?
247                                 "" : "no_");
248         fprintf(fp, "%shide,", (ep->e_flags & NFSEXP_NOHIDE)?
249                                 "no" : "");
250         fprintf(fp, "%scrossmnt,", (ep->e_flags & NFSEXP_CROSSMOUNT)?
251                                 "" : "no");
252         fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
253                                 "in" : "");
254         fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
255                                 "" : "no_");
256         fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
257                                 "" : "no_");
258         fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
259                 "no_" : "");
260         fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
261                 "in" : "");
262         fprintf(fp, "%sacl,", (ep->e_flags & NFSEXP_NOACL)?
263                 "no_" : "");
264         if (ep->e_flags & NFSEXP_FSID) {
265                 fprintf(fp, "fsid=%d,", ep->e_fsid);
266         }
267         if (ep->e_uuid)
268                 fprintf(fp, "fsid=%s,", ep->e_uuid);
269         if (ep->e_mountpoint)
270                 fprintf(fp, "mountpoint%s%s,",
271                         ep->e_mountpoint[0]?"=":"", ep->e_mountpoint);
272         switch (ep->e_fslocmethod) {
273         case FSLOC_NONE:
274                 break;
275         case FSLOC_REFER:
276                 fprintf(fp, "refer=%s,", ep->e_fslocdata);
277                 break;
278         case FSLOC_REPLICA:
279                 fprintf(fp, "replicas=%s,", ep->e_fslocdata);
280                 break;
281 #ifdef DEBUG
282         case FSLOC_STUB:
283                 fprintf(fp, "fsloc=stub,");
284                 break;
285 #endif
286         default:
287                 xlog(L_ERROR, "unknown fsloc method for %s:%s",
288                      ep->e_hostname, ep->e_path);
289         }
290         if ((id = ep->e_squids) != NULL) {
291                 fprintf(fp, "squash_uids=");
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         if ((id = ep->e_sqgids) != NULL) {
299                 fprintf(fp, "squash_gids=");
300                 for (i = 0; i < ep->e_nsquids; i += 2)
301                         if (id[i] != id[i+1])
302                                 fprintf(fp, "%d-%d,", id[i], id[i+1]);
303                         else
304                                 fprintf(fp, "%d,", id[i]);
305         }
306         fprintf(fp, "anonuid=%d,anongid=%d", ep->e_anonuid, ep->e_anongid);
307         secinfo_show(fp, ep);
308         fprintf(fp, ")\n");
309 }
310
311 void
312 endexportent(void)
313 {
314         if (efp)
315                 xfclose(efp);
316         efp = NULL;
317         if (efname)
318                 free(efname);
319         efname = NULL;
320         freesquash();
321 }
322
323 void
324 dupexportent(struct exportent *dst, struct exportent *src)
325 {
326         int     n;
327
328         *dst = *src;
329         if ((n = src->e_nsquids) != 0) {
330                 dst->e_squids = (int *) xmalloc(n * sizeof(int));
331                 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
332         }
333         if ((n = src->e_nsqgids) != 0) {
334                 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
335                 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
336         }
337         if (src->e_mountpoint)
338                 dst->e_mountpoint = strdup(src->e_mountpoint);
339         if (src->e_fslocdata)
340                 dst->e_fslocdata = strdup(src->e_fslocdata);
341         if (src->e_uuid)
342                 dst->e_uuid = strdup(src->e_uuid);
343         dst->e_hostname = NULL;
344 }
345
346 struct exportent *
347 mkexportent(char *hname, char *path, char *options)
348 {
349         static struct exportent ee;
350
351         init_exportent(&ee, 0);
352
353         xfree(ee.e_hostname);
354         ee.e_hostname = xstrdup(hname);
355
356         if (strlen(path) >= sizeof(ee.e_path)) {
357                 xlog(L_WARNING, "path name %s too long", path);
358                 return NULL;
359         }
360         strncpy(ee.e_path, path, sizeof (ee.e_path));
361         ee.e_path[sizeof (ee.e_path) - 1] = '\0';
362         if (parseopts(options, &ee, 0, NULL) < 0)
363                 return NULL;
364         return &ee;
365 }
366
367 int
368 updateexportent(struct exportent *eep, char *options)
369 {
370         if (parseopts(options, eep, 0, NULL) < 0)
371                 return 0;
372         return 1;
373 }
374
375
376 static int valid_uuid(char *uuid)
377 {
378         /* must have 32 hex digits */
379         int cnt;
380         for (cnt = 0 ; *uuid; uuid++)
381                 if (isxdigit(*uuid))
382                         cnt++;
383         return cnt == 32;
384 }
385
386 /*
387  * Append the given flavor to the exportent's e_secinfo array, or
388  * do nothing if it's already there.  Returns the index of flavor
389  * in the resulting array in any case.
390  */
391 int secinfo_addflavor(struct flav_info *flav, struct exportent *ep)
392 {
393         struct sec_entry *p;
394
395         for (p=ep->e_secinfo; p->flav; p++) {
396                 if (p->flav == flav)
397                         return p - ep->e_secinfo;
398         }
399         if (p - ep->e_secinfo >= SECFLAVOR_COUNT) {
400                 xlog(L_ERROR, "more than %d security flavors on an export\n",
401                         SECFLAVOR_COUNT);
402                 return -1;
403         }
404         p->flav = flav;
405         p->flags = ep->e_flags;
406         (p+1)->flav = NULL;
407         return p - ep->e_secinfo;
408 }
409
410 static struct flav_info *find_flavor(char *name)
411 {
412         struct flav_info *flav;
413         for (flav = flav_map; flav < flav_map + flav_map_size; flav++)
414                 if (strcmp(flav->flavour, name) == 0)
415                         return flav;
416         return NULL;
417 }
418
419 /* @str is a colon seperated list of security flavors.  Their order
420  * is recorded in @ep, and a bitmap corresponding to the list is returned.
421  * A zero return indicates an error.
422  */
423 static unsigned int parse_flavors(char *str, struct exportent *ep)
424 {
425         unsigned int out=0;
426         char *flavor;
427         int bit;
428
429         while ( (flavor=strsep(&str, ":")) ) {
430                 struct flav_info *flav = find_flavor(flavor);
431                 if (flav == NULL) {
432                         xlog(L_ERROR, "unknown flavor %s\n", flavor);
433                         return 0;
434                 }
435                 bit = secinfo_addflavor(flav, ep);
436                 if (bit < 0)
437                         return 0;
438                 out |= 1<<bit;
439         }
440         return out;
441 }
442
443 /* Sets the bits in @mask for the appropriate security flavor flags. */
444 static void setflags(int mask, unsigned int active, struct exportent *ep)
445 {
446         int bit=0;
447
448         ep->e_flags |= mask;
449
450         while (active) {
451                 if (active & 1)
452                         ep->e_secinfo[bit].flags |= mask;
453                 bit++;
454                 active >>= 1;
455         }
456 }
457
458 /* Clears the bits in @mask for the appropriate security flavor flags. */
459 static void clearflags(int mask, unsigned int active, struct exportent *ep)
460 {
461         int bit=0;
462
463         ep->e_flags &= ~mask;
464
465         while (active) {
466                 if (active & 1)
467                         ep->e_secinfo[bit].flags &= ~mask;
468                 bit++;
469                 active >>= 1;
470         }
471 }
472
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 export_features *ef;
481         struct sec_entry *p;
482
483         ef = get_export_features();
484         for (p = ep->e_secinfo; p->flav; p++)
485                 p->flags |= ep->e_flags & ~ef->secinfo_flags;
486 }
487
488 /*
489  * Parse option string pointed to by cp and set mount options accordingly.
490  */
491 static int
492 parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr)
493 {
494         int     had_subtree_opt = 0;
495         char    *flname = efname?efname:"command line";
496         int     flline = efp?efp->x_line:0;
497         unsigned int active = 0;
498
499         squids = ep->e_squids; nsquids = ep->e_nsquids;
500         sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
501         if (!cp)
502                 goto out;
503
504         while (isblank(*cp))
505                 cp++;
506
507         while (*cp) {
508                 char *opt = strdup(cp);
509                 char *optstart = cp;
510                 while (*cp && *cp != ',')
511                         cp++;
512                 if (*cp) {
513                         opt[cp-optstart] = '\0';
514                         cp++;
515                 }
516
517                 /* process keyword */
518                 if (strcmp(opt, "ro") == 0)
519                         setflags(NFSEXP_READONLY, active, ep);
520                 else if (strcmp(opt, "rw") == 0)
521                         clearflags(NFSEXP_READONLY, active, ep);
522                 else if (!strcmp(opt, "secure"))
523                         clearflags(NFSEXP_INSECURE_PORT, active, ep);
524                 else if (!strcmp(opt, "insecure"))
525                         setflags(NFSEXP_INSECURE_PORT, active, ep);
526                 else if (!strcmp(opt, "sync"))
527                         clearflags(NFSEXP_ASYNC, active, ep);
528                 else if (!strcmp(opt, "async"))
529                         setflags(NFSEXP_ASYNC, active, ep);
530                 else if (!strcmp(opt, "nohide"))
531                         setflags(NFSEXP_NOHIDE, active, ep);
532                 else if (!strcmp(opt, "hide"))
533                         clearflags(NFSEXP_NOHIDE, active, ep);
534                 else if (!strcmp(opt, "crossmnt"))
535                         setflags(NFSEXP_CROSSMOUNT, active, ep);
536                 else if (!strcmp(opt, "nocrossmnt"))
537                         clearflags(NFSEXP_CROSSMOUNT, active, ep);
538                 else if (!strcmp(opt, "wdelay"))
539                         setflags(NFSEXP_GATHERED_WRITES, active, ep);
540                 else if (!strcmp(opt, "no_wdelay"))
541                         clearflags(NFSEXP_GATHERED_WRITES, active, ep);
542                 else if (strcmp(opt, "root_squash") == 0)
543                         setflags(NFSEXP_ROOTSQUASH, active, ep);
544                 else if (!strcmp(opt, "no_root_squash"))
545                         clearflags(NFSEXP_ROOTSQUASH, active, ep);
546                 else if (strcmp(opt, "all_squash") == 0)
547                         setflags(NFSEXP_ALLSQUASH, active, ep);
548                 else if (strcmp(opt, "no_all_squash") == 0)
549                         clearflags(NFSEXP_ALLSQUASH, active, ep);
550                 else if (strcmp(opt, "subtree_check") == 0) {
551                         had_subtree_opt = 1;
552                         clearflags(NFSEXP_NOSUBTREECHECK, active, ep);
553                 } else if (strcmp(opt, "no_subtree_check") == 0) {
554                         had_subtree_opt = 1;
555                         setflags(NFSEXP_NOSUBTREECHECK, active, ep);
556                 } else if (strcmp(opt, "auth_nlm") == 0)
557                         clearflags(NFSEXP_NOAUTHNLM, active, ep);
558                 else if (strcmp(opt, "no_auth_nlm") == 0)
559                         setflags(NFSEXP_NOAUTHNLM, active, ep);
560                 else if (strcmp(opt, "secure_locks") == 0)
561                         clearflags(NFSEXP_NOAUTHNLM, active, ep);
562                 else if (strcmp(opt, "insecure_locks") == 0)
563                         setflags(NFSEXP_NOAUTHNLM, active, ep);
564                 else if (strcmp(opt, "acl") == 0)
565                         clearflags(NFSEXP_NOACL, active, ep);
566                 else if (strcmp(opt, "no_acl") == 0)
567                         setflags(NFSEXP_NOACL, active, ep);
568                 else if (strncmp(opt, "anonuid=", 8) == 0) {
569                         char *oe;
570                         ep->e_anonuid = strtol(opt+8, &oe, 10);
571                         if (opt[8]=='\0' || *oe != '\0') {
572                                 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
573                                      flname, flline, opt);      
574 bad_option:
575                                 free(opt);
576                                 export_errno = EINVAL;
577                                 return -1;
578                         }
579                 } else if (strncmp(opt, "anongid=", 8) == 0) {
580                         char *oe;
581                         ep->e_anongid = strtol(opt+8, &oe, 10);
582                         if (opt[8]=='\0' || *oe != '\0') {
583                                 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
584                                      flname, flline, opt);      
585                                 goto bad_option;
586                         }
587                 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
588                         if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
589                                 goto bad_option;
590                         }
591                 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
592                         if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
593                                 goto bad_option;
594                         }
595                 } else if (strncmp(opt, "fsid=", 5) == 0) {
596                         char *oe;
597                         if (strcmp(opt+5, "root") == 0) {
598                                 ep->e_fsid = 0;
599                                 setflags(NFSEXP_FSID, active, ep);
600                         } else {
601                                 ep->e_fsid = strtoul(opt+5, &oe, 0);
602                                 if (opt[5]!='\0' && *oe == '\0') 
603                                         setflags(NFSEXP_FSID, active, ep);
604                                 else if (valid_uuid(opt+5))
605                                         ep->e_uuid = strdup(opt+5);
606                                 else {
607                                         xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
608                                              flname, flline, opt);      
609                                         goto bad_option;
610                                 }
611                         }
612                 } else if (strcmp(opt, "mountpoint")==0 ||
613                            strcmp(opt, "mp") == 0 ||
614                            strncmp(opt, "mountpoint=", 11)==0 ||
615                            strncmp(opt, "mp=", 3) == 0) {
616                         char * mp = strchr(opt, '=');
617                         if (mp)
618                                 ep->e_mountpoint = strdup(mp+1);
619                         else
620                                 ep->e_mountpoint = strdup("");
621 #ifdef DEBUG
622                 } else if (strncmp(opt, "fsloc=", 6) == 0) {
623                         if (strcmp(opt+6, "stub") == 0)
624                                 ep->e_fslocmethod = FSLOC_STUB;
625                         else {
626                                 xlog(L_ERROR, "%s:%d: bad option %s\n",
627                                      flname, flline, opt);
628                                 goto bad_option;
629                         }
630 #endif
631                 } else if (strncmp(opt, "refer=", 6) == 0) {
632                         ep->e_fslocmethod = FSLOC_REFER;
633                         ep->e_fslocdata = strdup(opt+6);
634                 } else if (strncmp(opt, "replicas=", 9) == 0) {
635                         ep->e_fslocmethod = FSLOC_REPLICA;
636                         ep->e_fslocdata = strdup(opt+9);
637                 } else if (strncmp(opt, "sec=", 4) == 0) {
638                         active = parse_flavors(opt+4, ep);
639                         if (!active)
640                                 goto bad_option;
641                 } else {
642                         xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
643                                         flname, flline, opt);
644                         setflags(NFSEXP_ALLSQUASH | NFSEXP_READONLY, active, ep);
645                         goto bad_option;
646                 }
647                 free(opt);
648                 while (isblank(*cp))
649                         cp++;
650         }
651
652         fix_pseudoflavor_flags(ep);
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 struct export_features *get_export_features(void)
770 {
771         static char *path = "/proc/fs/nfsd/export_features";
772         static struct export_features ef;
773         static int cached = 0;
774         char buf[50];
775         int c;
776         int fd;
777
778         if (cached)
779                 return &ef;
780
781         ef.flags = NFSEXP_OLDFLAGS;
782         ef.secinfo_flags = NFSEXP_OLD_SECINFO_FLAGS;
783
784         fd = open(path, O_RDONLY);
785         if (fd == -1)
786                 goto good;
787         fd = read(fd, buf, 50);
788         if (fd == -1)
789                 goto err;
790         c = sscanf(buf, "%x %x", &ef.flags, &ef.secinfo_flags);
791         if (c != 2)
792                 goto err;
793 good:
794         cached = 1;
795         return &ef;
796 err:
797         xlog(L_WARNING, "unexpected error reading %s", path);
798         return &ef;
799 }