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