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