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