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