050470987c8e8ec883715a9fd44033ed92816940
[nfs-utils.git] / utils / exportfs / exportfs.c
1 /*
2  * utils/exportfs/exportfs.c
3  *
4  * Export file systems to knfsd
5  *
6  * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
7  *
8  * Extensive changes, 1999, Neil Brown <neilb@cse.unsw.edu.au>
9  */
10
11 #include "config.h"
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include <getopt.h>
17 #include <netdb.h>
18 #include <errno.h>
19 #include "xmalloc.h"
20 #include "misc.h"
21 #include "nfslib.h"
22 #include "exportfs.h"
23 #include "xmalloc.h"
24 #include "xlog.h"
25
26 static void     export_all(int verbose);
27 static void     unexport_all(int verbose);
28 static void     exportfs(char *arg, char *options, int verbose);
29 static void     unexportfs(char *arg, int verbose);
30 static void     exports_update(int verbose);
31 static void     dump(int verbose);
32 static void     error(nfs_export *exp, int err);
33 static void     usage(void);
34
35
36 int
37 main(int argc, char **argv)
38 {
39         char    *options = NULL;
40         int     f_export = 1;
41         int     f_all = 0;
42         int     f_verbose = 0;
43         int     f_reexport = 0;
44         int     f_ignore = 0;
45         int     i, c;
46
47         xlog_open("exportfs");
48
49         while ((c = getopt(argc, argv, "aio:ruv")) != EOF) {
50                 switch(c) {
51                 case 'a':
52                         f_all = 1;
53                         break;
54                 case 'i':
55                         f_ignore = 1;
56                         break;
57                 case 'o':
58                         options = optarg;
59                         break;
60                 case 'r':
61                         f_reexport = 1;
62                         f_all = 1;
63                         break;
64                 case 'u':
65                         f_export = 0;
66                         break;
67                 case 'v':
68                         f_verbose = 1;
69                         break;
70                 default:
71                         usage();
72                         break;
73                 }
74         }
75
76         if (optind != argc && f_all) {
77                 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
78                 return 1;
79         }
80         if (f_ignore && (f_all || ! f_export)) {
81                 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
82                 return 1;
83         }
84         if (f_reexport && ! f_export) {
85                 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
86                 return 1;
87         }
88         if (optind == argc && ! f_all) {
89                 xtab_export_read();
90                 dump(f_verbose);
91                 return 0;
92         }
93
94         if (f_export && ! f_ignore)
95                 export_read(_PATH_EXPORTS);
96         if (f_export) {
97                 if (f_all)
98                         export_all(f_verbose);
99                 else
100                         for (i = optind; i < argc ; i++)
101                                 exportfs(argv[i], options, f_verbose);
102         }
103         /* note: xtab_*_read does not update entries if they already exist,
104          * so this will not lose new options
105          */
106         if (!f_reexport)
107                 xtab_export_read();
108         if (!f_export) {
109                 if (f_all)
110                         unexport_all(f_verbose);
111                 else
112                         for (i = optind ; i < argc ; i++)
113                                 unexportfs(argv[i], f_verbose);
114         }
115         rmtab_read();
116         xtab_mount_read();
117         exports_update(f_verbose);
118         xtab_export_write();
119         xtab_mount_write();
120
121         return 0;
122 }
123
124 /* we synchronise intention with reality.
125  * entries with m_mayexport get exported
126  * entries with m_exported but not m_mayexport get unexported
127  * looking at m_client->m_type == MCL_FQDN only
128  */
129 static void
130 exports_update(int verbose)
131 {
132         nfs_export      *exp;
133
134         for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
135                 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
136                         if (verbose)
137                                 printf("%sexporting %s:%s to kernel\n",
138                                        exp->m_exported ?"re":"",
139                                        exp->m_client->m_hostname,
140                                        exp->m_export.e_path);
141                         if (!export_export(exp))
142                                 error(exp, errno);
143                 }
144                 if (exp->m_exported && ! exp->m_mayexport) {
145                         if (verbose)
146                                 printf("unexporting %s:%s from kernel\n",
147                                        exp->m_client->m_hostname,
148                                        exp->m_export.e_path);
149                         if (!export_unexport(exp))
150                                 error(exp, errno);
151                 }
152         }
153 }
154                         
155 /*
156  * export_all finds all entries and
157  *    marks them xtabent and mayexport so that they get exported
158  */
159 static void
160 export_all(int verbose)
161 {
162         nfs_export      *exp;
163         int             i;
164
165         for (i = 0; i < MCL_MAXTYPES; i++) {
166                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
167                         if (verbose)
168                                 printf("exporting %s:%s\n",
169                                        exp->m_client->m_hostname, 
170                                        exp->m_export.e_path);
171                         exp->m_xtabent = 1;
172                         exp->m_mayexport = 1;
173                         exp->m_changed = 1;
174                 }
175         }
176 }
177 /*
178  * unexport_all finds all entries that are mayexport, and
179  *    marks them not xtabent and not mayexport
180  */
181 static void
182 unexport_all(int verbose)
183 {
184         nfs_export      *exp;
185         int             i;
186
187         for (i = 0; i < MCL_MAXTYPES; i++) {
188                 for (exp = exportlist[i]; exp; exp = exp->m_next)
189                         if (exp->m_mayexport) {
190                                 if (verbose) {
191                                         if (exp->m_exported) {
192                                                 printf("unexporting %s:%s from kernel\n",
193                                                        exp->m_client->m_hostname,
194                                                        exp->m_export.e_path);
195                                         }
196                                         else {
197                                                 printf("unexporting %s:%s\n",
198                                                         exp->m_client->m_hostname, 
199                                                         exp->m_export.e_path);
200                                         }
201                                 }
202                                 if (exp->m_exported && !export_unexport(exp))
203                                         error(exp, errno);
204                                 exp->m_xtabent = 0;
205                                 exp->m_mayexport = 0;
206                         }
207         }
208 }
209
210
211 static void
212 exportfs(char *arg, char *options, int verbose)
213 {
214         struct exportent *eep;
215         nfs_export      *exp;
216         struct hostent  *hp = NULL;
217         char            *path;
218         char            *hname = arg;
219         int             htype;
220
221         if ((path = strchr(arg, ':')) != NULL)
222                 *path++ = '\0';
223
224         if (!path || *path != '/') {
225                 fprintf(stderr, "Invalid exporting option: %s\n", arg);
226                 return;
227         }
228
229         if ((htype = client_gettype(hname)) == MCL_FQDN &&
230             (hp = gethostbyname(hname)) != NULL) {
231                 hp = hostent_dup (hp);
232                 exp = export_find(hp, path);
233         } else {
234                 exp = export_lookup(hname, path);
235         }
236
237         if (!exp) {
238                 if (!(eep = mkexportent(hname, path, options)) ||
239                     !(exp = export_create(eep))) {
240                         if (hp) free (hp);
241                         return;
242                 }
243         } else if (!updateexportent(&exp->m_export, options)) {
244                 if (hp) free (hp);
245                 return;
246         }
247
248         if (verbose)
249                 printf("exporting %s:%s\n", exp->m_client->m_hostname, 
250                         exp->m_export.e_path);
251         exp->m_xtabent = 1;
252         exp->m_mayexport = 1;
253         exp->m_changed = 1;
254         if (hp) free (hp);
255 }
256
257 static void
258 unexportfs(char *arg, int verbose)
259 {
260         nfs_export      *exp;
261         struct hostent  *hp = NULL;
262         char            *path;
263         char            *hname = arg;
264         int             htype;
265
266         if ((path = strchr(arg, ':')) != NULL)
267                 *path++ = '\0';
268
269         if (!path || *path != '/') {
270                 fprintf(stderr, "Invalid unexporting option: %s\n",
271                         arg);
272                 return;
273         }
274
275         if ((htype = client_gettype(hname)) == MCL_FQDN) {
276                 if ((hp = gethostbyname(hname)) != 0) {
277                         hp = hostent_dup (hp);
278                         hname = (char *) hp->h_name;
279                 }
280         }
281
282         for (exp = exportlist[htype]; exp; exp = exp->m_next) {
283                 if (path && strcmp(path, exp->m_export.e_path))
284                         continue;
285                 if (htype != exp->m_client->m_type
286                     || (htype == MCL_FQDN
287                         && !matchhostname(exp->m_export.e_hostname,
288                                           hname)))
289                         continue;
290                 if (verbose) {
291                         if (exp->m_exported) {
292                                 printf("unexporting %s:%s from kernel\n",
293                                        exp->m_client->m_hostname,
294                                        exp->m_export.e_path);
295                         }
296                         else {
297                                 printf("unexporting %s:%s\n",
298                                         exp->m_client->m_hostname, 
299                                         exp->m_export.e_path);
300                         }
301                 }
302                 if (exp->m_exported && !export_unexport(exp))
303                         error(exp, errno);
304                 exp->m_xtabent = 0;
305                 exp->m_mayexport = 0;
306         }
307
308         if (hp) free (hp);
309 }
310
311 static char
312 dumpopt(char c, char *fmt, ...)
313 {
314         va_list ap;
315
316         va_start(ap, fmt);
317         printf("%c", c);
318         vprintf(fmt, ap);
319         va_end(ap);
320         return ',';
321 }
322
323 static void
324 dump(int verbose)
325 {
326         nfs_export      *exp;
327         struct exportent *ep;
328         int             htype;
329         char            *hname, c;
330
331         for (htype = 0; htype < MCL_MAXTYPES; htype++) {
332                 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
333                         ep = &exp->m_export;
334                         if (!exp->m_xtabent)
335                             continue; /* neilb */
336                         if (htype == MCL_ANONYMOUS)
337                                 hname = "<world>";
338                         else
339                                 hname = ep->e_hostname;
340                         if (strlen(ep->e_path) > 14)
341                                 printf("%-14s\n\t\t%s", ep->e_path, hname);
342                         else
343                                 printf("%-14s\t%s", ep->e_path, hname);
344                         if (!verbose) {
345                                 printf("\n");
346                                 continue;
347                         }
348                         c = '(';
349                         if (ep->e_flags & NFSEXP_READONLY)
350                                 c = dumpopt(c, "ro");
351                         else
352                                 c = dumpopt(c, "rw");
353                         if (ep->e_flags & NFSEXP_ASYNC)
354                                 c = dumpopt(c, "async");
355                         if (ep->e_flags & NFSEXP_GATHERED_WRITES)
356                                 c = dumpopt(c, "wdelay");
357                         if (ep->e_flags & NFSEXP_CROSSMNT)
358                                 c = dumpopt(c, "nohide");
359                         if (ep->e_flags & NFSEXP_INSECURE_PORT)
360                                 c = dumpopt(c, "insecure");
361                         if (ep->e_flags & NFSEXP_ROOTSQUASH)
362                                 c = dumpopt(c, "root_squash");
363                         else
364                                 c = dumpopt(c, "no_root_squash");
365                         if (ep->e_flags & NFSEXP_ALLSQUASH)
366                                 c = dumpopt(c, "all_squash");
367                         if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
368                                 c = dumpopt(c, "no_subtree_check");
369                         if (ep->e_flags & NFSEXP_NOAUTHNLM)
370                                 c = dumpopt(c, "insecure_locks");
371                         if (ep->e_maptype == CLE_MAP_UGIDD)
372                                 c = dumpopt(c, "mapping=ugidd");
373                         else if (ep->e_maptype == CLE_MAP_FILE)
374                                 c = dumpopt(c, "mapping=file");
375                         if (ep->e_anonuid != -2)
376                                 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
377                         if (ep->e_anongid != -2)
378                                 c = dumpopt(c, "anongid=%d", ep->e_anongid);
379
380                         printf("%c\n", (c != '(')? ')' : ' ');
381                 }
382         }
383 }
384
385 static void
386 error(nfs_export *exp, int err)
387 {
388         fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname, 
389                 exp->m_export.e_path, strerror(err));
390 }
391
392 static void
393 usage(void)
394 {
395         fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");
396         exit(1);
397 }