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