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