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