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