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