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