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