]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/exports.c
2002-10-11 H.J. Lu <hjl@lucon.org>
[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 #include "config.h"
18
19 #include <sys/param.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include "nfslib.h"
27 #include "exportfs.h"
28 #include "xmalloc.h"
29 #include "xlog.h"
30 #include "xio.h"
31
32 #define EXPORT_DEFAULT_FLAGS    \
33   (NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES)
34
35 int export_errno;
36
37 static char     *efname = NULL;
38 static XFILE    *efp = NULL;
39 static int      first;
40 static int      *squids = NULL, nsquids = 0,
41                 *sqgids = NULL, nsqgids = 0;
42
43 static int      getexport(char *exp, int len);
44 static int      getpath(char *path, int len);
45 static int      parseopts(char *cp, struct exportent *ep, int warn);
46 static int      parsesquash(char *list, int **idp, int *lenp, char **ep);
47 static int      parsenum(char **cpp);
48 static int      parsemaptype(char *type);
49 static void     freesquash(void);
50 static void     syntaxerr(char *msg);
51
52 void
53 setexportent(char *fname, char *type)
54 {
55         if (efp)
56                 endexportent();
57         if (!fname)
58                 fname = _PATH_EXPORTS;
59         if (!(efp = xfopen(fname, type)))
60                 xlog(L_ERROR, "can't open %s for %sing",
61                                 fname, strcmp(type, "r")? "writ" : "read");
62         efname = strdup(fname);
63         first = 1;
64 }
65
66 struct exportent *
67 getexportent(int fromkernel, int fromexports)
68 {
69         static struct exportent ee;
70         char            exp[512];
71         char            rpath[MAXPATHLEN+1];
72         char            *opt, *sp;
73         int             ok;
74
75         if (!efp)
76                 return NULL;
77
78         freesquash();
79         ee.e_flags = EXPORT_DEFAULT_FLAGS;
80         /* some kernels assume the default is sync rather than
81          * async.  More recent kernels always report one or other,
82          * but this test makes sure we assume same as kernel
83          * Ditto for wgather
84          */
85         if (fromkernel) {
86                 ee.e_flags &= ~NFSEXP_ASYNC;
87                 ee.e_flags &= ~NFSEXP_GATHERED_WRITES;
88         }
89         ee.e_maptype = CLE_MAP_IDENT;
90         ee.e_anonuid = -2;
91         ee.e_anongid = -2;
92         ee.e_squids = NULL;
93         ee.e_sqgids = NULL;
94         ee.e_nsquids = 0;
95         ee.e_nsqgids = 0;
96
97         if (first || (ok = getexport(exp, sizeof(exp))) == 0) {
98                 ok = getpath(ee.e_path, sizeof(ee.e_path));
99                 if (ok <= 0)
100                         return NULL;
101                 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
102                 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
103                 ok = getexport(exp, sizeof(exp));
104         }
105         if (ok < 0) {
106                 xlog(L_ERROR, "expected client(options...)");
107                 export_errno = EINVAL;
108                 return NULL;
109         }
110         first = 0;
111
112         /* Check for default client */
113         if (ok == 0)
114                 exp[0] = '\0';
115         if ((opt = strchr(exp, '(')) != NULL) {
116                 if (opt == exp) 
117                         xlog(L_WARNING, "No host name given with %s %s, suggest *%s to avoid warning", ee.e_path, exp, exp);
118                 *opt++ = '\0';
119                 if (!(sp = strchr(opt, ')')) || sp[1] != '\0') {
120                         syntaxerr("bad option list");
121                         export_errno = EINVAL;
122                         return NULL;
123                 }
124                 *sp = '\0';
125         } else {
126             xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
127         }
128         if (strlen(exp) >= sizeof(ee.e_hostname)) {
129                 syntaxerr("client name too long");
130                 export_errno = EINVAL;
131                 return NULL;
132         }
133         strncpy(ee.e_hostname, exp, sizeof (ee.e_hostname) - 1);
134         ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
135
136         if (parseopts(opt, &ee, fromexports) < 0)
137                 return NULL;
138
139         /* resolve symlinks */
140         if (realpath(ee.e_path, rpath) != NULL) {
141                 rpath[sizeof (rpath) - 1] = '\0';
142                 strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
143                 ee.e_path[sizeof (ee.e_path) - 1] = '\0';
144                 strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
145                 ee.m_path [sizeof (ee.m_path) - 1] = '\0';
146         }
147
148         return &ee;
149 }
150
151 void
152 putexportent(struct exportent *ep)
153 {
154         FILE    *fp;
155         int     *id, i;
156         char    *esc=ep->e_path;
157
158         if (!efp)
159                 return;
160
161         fp = efp->x_fp;
162         for (i=0; esc[i]; i++)
163                 if (iscntrl(esc[i]) || esc[i] == '"' || esc[i] == '\\'|| isspace(esc[i]))
164                         fprintf(fp, "\\%03o", esc[i]);
165                 else
166                         fprintf(fp, "%c", esc[i]);
167
168         fprintf(fp, "\t%s(", ep->e_hostname);
169         fprintf(fp, "%s,", (ep->e_flags & NFSEXP_READONLY)? "ro" : "rw");
170         fprintf(fp, "%ssync,", (ep->e_flags & NFSEXP_ASYNC)? "a" : "");
171         fprintf(fp, "%swdelay,", (ep->e_flags & NFSEXP_GATHERED_WRITES)?
172                                 "" : "no_");
173         fprintf(fp, "%shide,", (ep->e_flags & NFSEXP_CROSSMNT)?
174                                 "no" : "");
175         fprintf(fp, "%ssecure,", (ep->e_flags & NFSEXP_INSECURE_PORT)?
176                                 "in" : "");
177         fprintf(fp, "%sroot_squash,", (ep->e_flags & NFSEXP_ROOTSQUASH)?
178                                 "" : "no_");
179         fprintf(fp, "%sall_squash,", (ep->e_flags & NFSEXP_ALLSQUASH)?
180                                 "" : "no_");
181         fprintf(fp, "%ssubtree_check,", (ep->e_flags & NFSEXP_NOSUBTREECHECK)?
182                 "no_" : "");
183         fprintf(fp, "%ssecure_locks,", (ep->e_flags & NFSEXP_NOAUTHNLM)?
184                 "in" : "");
185         if (ep->e_flags & NFSEXP_FSID) {
186                 fprintf(fp, "fsid=%d,", ep->e_fsid);
187         }
188         fprintf(fp, "mapping=");
189         switch (ep->e_maptype) {
190         case CLE_MAP_IDENT:
191                 fprintf(fp, "identity,");
192                 break;
193         case CLE_MAP_UGIDD:
194                 fprintf(fp, "ugidd,");
195                 break;
196         case CLE_MAP_FILE:
197                 fprintf(fp, "file,");
198                 break;
199         default:
200                 xlog(L_ERROR, "unknown mapping type for %s:%s",
201                                         ep->e_hostname, ep->e_path);
202         }
203         if ((id = ep->e_squids) != NULL) {
204                 fprintf(fp, "squash_uids=");
205                 for (i = 0; i < ep->e_nsquids; i += 2)
206                         if (id[i] != id[i+1])
207                                 fprintf(fp, "%d-%d,", id[i], id[i+1]);
208                         else
209                                 fprintf(fp, "%d,", id[i]);
210         }
211         if ((id = ep->e_sqgids) != NULL) {
212                 fprintf(fp, "squash_gids=");
213                 for (i = 0; i < ep->e_nsquids; i += 2)
214                         if (id[i] != id[i+1])
215                                 fprintf(fp, "%d-%d,", id[i], id[i+1]);
216                         else
217                                 fprintf(fp, "%d,", id[i]);
218         }
219         fprintf(fp, "anonuid=%d,anongid=%d)\n", ep->e_anonuid, ep->e_anongid);
220 }
221
222 void
223 endexportent(void)
224 {
225         if (efp)
226                 xfclose(efp);
227         efp = NULL;
228         if (efname)
229                 free(efname);
230         efname = NULL;
231         freesquash();
232 }
233
234 void
235 dupexportent(struct exportent *dst, struct exportent *src)
236 {
237         int     n;
238
239         *dst = *src;
240         if ((n = src->e_nsquids) != 0) {
241                 dst->e_squids = (int *) xmalloc(n * sizeof(int));
242                 memcpy(dst->e_squids, src->e_squids, n * sizeof(int));
243         }
244         if ((n = src->e_nsqgids) != 0) {
245                 dst->e_sqgids = (int *) xmalloc(n * sizeof(int));
246                 memcpy(dst->e_sqgids, src->e_sqgids, n * sizeof(int));
247         }
248 }
249
250 struct exportent *
251 mkexportent(char *hname, char *path, char *options)
252 {
253         static struct exportent ee;
254
255         ee.e_flags = EXPORT_DEFAULT_FLAGS;
256         ee.e_maptype = CLE_MAP_IDENT;
257         ee.e_anonuid = -2;
258         ee.e_anongid = -2;
259         ee.e_squids = NULL;
260         ee.e_sqgids = NULL;
261         ee.e_nsquids = 0;
262         ee.e_nsqgids = 0;
263
264         if (strlen(hname) >= sizeof(ee.e_hostname)) {
265                 xlog(L_WARNING, "client name %s too long", hname);
266                 return NULL;
267         }
268         strncpy(ee.e_hostname, hname, sizeof (ee.e_hostname) - 1);
269         ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
270         if (strlen(path) >= sizeof(ee.e_path)) {
271                 xlog(L_WARNING, "path name %s too long", path);
272                 return NULL;
273         }
274         strncpy(ee.e_path, path, sizeof (ee.e_path));
275         ee.e_path[sizeof (ee.e_path) - 1] = '\0';
276         strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
277         ee.m_path [sizeof (ee.m_path) - 1] = '\0';
278         if (parseopts(options, &ee, 0) < 0)
279                 return NULL;
280         return &ee;
281 }
282
283 int
284 updateexportent(struct exportent *eep, char *options)
285 {
286         if (parseopts(options, eep, 0) < 0)
287                 return 0;
288         return 1;
289 }
290
291 /*
292  * Parse option string pointed to by cp and set mount options accordingly.
293  */
294 static int
295 parseopts(char *cp, struct exportent *ep, int warn)
296 {
297         int     had_sync_opt = 0;
298         char    *flname = efname?efname:"command line";
299         int     flline = efp?efp->x_line:0;
300
301         squids = ep->e_squids; nsquids = ep->e_nsquids;
302         sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
303
304         if (!cp)
305                 goto out;
306
307         while (isblank(*cp))
308                 cp++;
309
310         while (*cp) {
311                 char *opt = strdup(cp);
312                 char *optstart = cp;
313                 while (*cp && *cp != ',')
314                         cp++;
315                 if (*cp) {
316                         opt[cp-optstart] = '\0';
317                         cp++;
318                 }
319
320                 /* process keyword */
321                 if (strcmp(opt, "ro") == 0)
322                         ep->e_flags |= NFSEXP_READONLY;
323                 else if (strcmp(opt, "rw") == 0)
324                         ep->e_flags &= ~NFSEXP_READONLY;
325                 else if (!strcmp(opt, "secure"))
326                         ep->e_flags &= ~NFSEXP_INSECURE_PORT;
327                 else if (!strcmp(opt, "insecure"))
328                         ep->e_flags |= NFSEXP_INSECURE_PORT;
329                 else if (!strcmp(opt, "sync")) {
330                         had_sync_opt = 1;
331                         ep->e_flags &= ~NFSEXP_ASYNC;
332                 } else if (!strcmp(opt, "async")) {
333                         had_sync_opt = 1;
334                         ep->e_flags |= NFSEXP_ASYNC;
335                 } else if (!strcmp(opt, "nohide"))
336                         ep->e_flags |= NFSEXP_CROSSMNT;
337                 else if (!strcmp(opt, "hide"))
338                         ep->e_flags &= ~NFSEXP_CROSSMNT;
339                 else if (!strcmp(opt, "crossmnt"))              /* old style */
340                         ep->e_flags |= NFSEXP_CROSSMNT;
341                 else if (!strcmp(opt, "nocrossmnt"))            /* old style */
342                         ep->e_flags &= ~NFSEXP_CROSSMNT;
343                 else if (!strcmp(opt, "wdelay"))
344                         ep->e_flags |= NFSEXP_GATHERED_WRITES;
345                 else if (!strcmp(opt, "no_wdelay"))
346                         ep->e_flags &= ~NFSEXP_GATHERED_WRITES;
347                 else if (strcmp(opt, "root_squash") == 0)
348                         ep->e_flags |= NFSEXP_ROOTSQUASH;
349                 else if (!strcmp(opt, "no_root_squash"))
350                         ep->e_flags &= ~NFSEXP_ROOTSQUASH;
351                 else if (strcmp(opt, "all_squash") == 0)
352                         ep->e_flags |= NFSEXP_ALLSQUASH;
353                 else if (strcmp(opt, "no_all_squash") == 0)
354                         ep->e_flags &= ~NFSEXP_ALLSQUASH;
355                 else if (strcmp(opt, "subtree_check") == 0)
356                         ep->e_flags &= ~NFSEXP_NOSUBTREECHECK;
357                 else if (strcmp(opt, "no_subtree_check") == 0)
358                         ep->e_flags |= NFSEXP_NOSUBTREECHECK;
359                 else if (strcmp(opt, "auth_nlm") == 0)
360                         ep->e_flags &= ~NFSEXP_NOAUTHNLM;
361                 else if (strcmp(opt, "no_auth_nlm") == 0)
362                         ep->e_flags |= NFSEXP_NOAUTHNLM;
363                 else if (strcmp(opt, "secure_locks") == 0)
364                         ep->e_flags &= ~NFSEXP_NOAUTHNLM;
365                 else if (strcmp(opt, "insecure_locks") == 0)
366                         ep->e_flags |= NFSEXP_NOAUTHNLM;
367                 else if (strncmp(opt, "mapping=", 8) == 0)
368                         ep->e_maptype = parsemaptype(opt+8);
369                 else if (strcmp(opt, "map_identity") == 0)      /* old style */
370                         ep->e_maptype = CLE_MAP_IDENT;
371                 else if (strcmp(opt, "map_daemon") == 0)        /* old style */
372                         ep->e_maptype = CLE_MAP_UGIDD;
373                 else if (strncmp(opt, "anonuid=", 8) == 0) {
374                         char *oe;
375                         ep->e_anonuid = strtol(opt+8, &oe, 10);
376                         if (opt[8]=='\0' || *oe != '\0') {
377                                 xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
378                                      flname, flline, opt);      
379 bad_option:
380                                 free(opt);
381                                 export_errno = EINVAL;
382                                 return -1;
383                         }
384                 } else if (strncmp(opt, "anongid=", 8) == 0) {
385                         char *oe;
386                         ep->e_anongid = strtol(opt+8, &oe, 10);
387                         if (opt[8]=='\0' || *oe != '\0') {
388                                 xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
389                                      flname, flline, opt);      
390                                 goto bad_option;
391                         }
392                 } else if (strncmp(opt, "squash_uids=", 12) == 0) {
393                         if (parsesquash(opt+12, &squids, &nsquids, &cp) < 0) {
394                                 goto bad_option;
395                         }
396                 } else if (strncmp(opt, "squash_gids=", 12) == 0) {
397                         if (parsesquash(opt+12, &sqgids, &nsqgids, &cp) < 0) {
398                                 goto bad_option;
399                         }
400                 } else if (strncmp(opt, "fsid=", 5) == 0) {
401                         char *oe;
402                         ep->e_fsid = strtoul(opt+5, &oe, 0);
403                         if (opt[5]=='\0' || *oe != '\0') {
404                                 xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
405                                      flname, flline, opt);      
406                                 goto bad_option;
407                         }
408                         ep->e_flags |= NFSEXP_FSID;
409                 } else {
410                         xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
411                                         flname, flline, opt);
412                         ep->e_flags |= NFSEXP_ALLSQUASH | NFSEXP_READONLY;
413                         goto bad_option;
414                 }
415                 free(opt);
416                 while (isblank(*cp))
417                         cp++;
418         }
419
420         ep->e_squids = squids;
421         ep->e_sqgids = sqgids;
422         ep->e_nsquids = nsquids;
423         ep->e_nsqgids = nsqgids;
424
425 out:
426         if (warn && !had_sync_opt)
427                 xlog(L_WARNING, "%s [%d]: No 'sync' or 'async' option specified for export \"%s:%s\".\n"
428                                 "  Assuming default behaviour ('sync').\n"
429                                 "  NOTE: this default has changed from previous versions\n",
430
431                                 flname, flline,
432                                 ep->e_hostname, ep->e_path);
433
434         return 1;
435 }
436
437 static int
438 parsesquash(char *list, int **idp, int *lenp, char **ep)
439 {
440         char    *cp = list;
441         int     id0, id1;
442         int     len = *lenp;
443         int     *id = *idp;
444
445         if (**ep)
446             *--(*ep) = ',';
447
448         do {
449                 id0 = parsenum(&cp);
450                 if (*cp == '-') {
451                         cp++;
452                         id1 = parsenum(&cp);
453                 } else {
454                         id1 = id0;
455                 }
456                 if (id0 == -1 || id1 == -1) {
457                         syntaxerr("uid/gid -1 not permitted");
458                         return -1;
459                 }
460                 if ((len % 8) == 0)
461                         id = (int *) xrealloc(id, (len + 8) * sizeof(*id));
462                 id[len++] = id0;
463                 id[len++] = id1;
464                 if (!*cp || *cp == ')' || (*cp == ',' && !isdigit(cp[1])))
465                         break;
466                 if (*cp != ',') {
467                         syntaxerr("bad uid/gid list");
468                         return -1;
469                 }
470                 cp++;
471         } while(1);
472
473         if (**ep == ',') (*ep)++;
474
475         *lenp = len;
476         *idp = id;
477         return 1;
478 }
479
480 static void
481 freesquash(void)
482 {
483         if (squids) {
484                 xfree (squids);
485                 squids = NULL;
486                 nsquids = 0;
487         }
488         if (sqgids) {
489                 xfree (sqgids);
490                 sqgids = NULL;
491                 nsqgids = 0;
492         }
493 }
494
495 static int
496 parsenum(char **cpp)
497 {
498         char    *cp = *cpp, c;
499         int     num = 0;
500
501         if (**cpp == '-')
502                 (*cpp)++;
503         while (isdigit(**cpp))
504                 (*cpp)++;
505         c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
506         return num;
507 }
508
509 static int
510 parsemaptype(char *type)
511 {
512         if (!strcmp(type, "identity"))
513                 return CLE_MAP_IDENT;
514         if (!strcmp(type, "ugidd"))
515                 return CLE_MAP_UGIDD;
516         if (!strcmp(type, "file"))
517                 return CLE_MAP_FILE;
518         syntaxerr("invalid map type");
519         return CLE_MAP_IDENT;   /* default */
520 }
521
522 static int
523 getpath(char *path, int len)
524 {
525         xskip(efp, " \t\n");
526         return xgettok(efp, 0, path, len);
527 }
528
529 static int
530 getexport(char *exp, int len)
531 {
532         int     ok;
533
534         xskip(efp, " \t");
535         if ((ok = xgettok(efp, 0, exp, len)) < 0)
536                 xlog(L_ERROR, "%s:%d: syntax error",
537                         efname?"command line":efname, efp->x_line);
538         return ok;
539 }
540
541 static void
542 syntaxerr(char *msg)
543 {
544         xlog(L_ERROR, "%s:%d: syntax error: %s",
545                         efname, efp?efp->x_line:0, msg);
546 }
547