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