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