]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/exportfs/exportfs.c
exportfs: Use xlog() for error reporting
[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 <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/vfs.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <stdbool.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <getopt.h>
25 #include <fcntl.h>
26 #include <netdb.h>
27 #include <errno.h>
28
29 #include "misc.h"
30 #include "nfslib.h"
31 #include "exportfs.h"
32 #include "xlog.h"
33
34 static void     export_all(int verbose);
35 static void     exportfs(char *arg, char *options, int verbose);
36 static void     unexportfs(char *arg, int verbose);
37 static void     exports_update(int verbose);
38 static void     dump(int verbose);
39 static void     error(nfs_export *exp, int err);
40 static void     usage(const char *progname);
41 static void     validate_export(nfs_export *exp);
42 static int      matchhostname(const char *hostname1, const char *hostname2);
43
44 int
45 main(int argc, char **argv)
46 {
47         char    *options = NULL;
48         char    *progname = NULL;
49         int     f_export = 1;
50         int     f_all = 0;
51         int     f_verbose = 0;
52         int     f_reexport = 0;
53         int     f_ignore = 0;
54         int     i, c;
55         int     new_cache = 0;
56         int     force_flush = 0;
57
58         if ((progname = strrchr(argv[0], '/')) != NULL)
59                 progname++;
60         else
61                 progname = argv[0];
62
63         xlog_open(progname);
64         xlog_stderr(1);
65         xlog_syslog(0);
66
67         export_errno = 0;
68
69         while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
70                 switch(c) {
71                 case 'a':
72                         f_all = 1;
73                         break;
74                 case 'i':
75                         f_ignore = 1;
76                         break;
77                 case 'o':
78                         options = optarg;
79                         break;
80                 case 'r':
81                         f_reexport = 1;
82                         f_all = 1;
83                         break;
84                 case 'u':
85                         f_export = 0;
86                         break;
87                 case 'v':
88                         f_verbose = 1;
89                         break;
90                 case 'f':
91                         force_flush = 1;
92                         break;
93                 default:
94                         usage(progname);
95                         break;
96                 }
97         }
98
99         if (optind != argc && f_all) {
100                 xlog(L_ERROR, "extra arguments are not permitted with -a or -r");
101                 return 1;
102         }
103         if (f_ignore && (f_all || ! f_export)) {
104                 xlog(L_ERROR, "-i not meaningful with -a, -r or -u");
105                 return 1;
106         }
107         if (f_reexport && ! f_export) {
108                 xlog(L_ERROR, "-r and -u are incompatible");
109                 return 1;
110         }
111         new_cache = check_new_cache();
112         if (optind == argc && ! f_all) {
113                 if (force_flush) {
114                         if (new_cache)
115                                 cache_flush(1);
116                         else {
117                                 xlog(L_ERROR, "-f is available only "
118                                         "with new cache controls. "
119                                         "Mount /proc/fs/nfsd first");
120                                 return 1;
121                         }
122                         return 0;
123                 } else {
124                         xtab_export_read();
125                         dump(f_verbose);
126                         return 0;
127                 }
128         }
129         if (f_export && ! f_ignore)
130                 export_read(_PATH_EXPORTS);
131         if (f_export) {
132                 if (f_all)
133                         export_all(f_verbose);
134                 else
135                         for (i = optind; i < argc ; i++)
136                                 exportfs(argv[i], options, f_verbose);
137         }
138         /* If we are unexporting everything, then
139          * don't care about what should be exported, as that
140          * may require DNS lookups..
141          */
142         if (! ( !f_export && f_all)) {
143                 /* note: xtab_*_read does not update entries if they already exist,
144                  * so this will not lose new options
145                  */
146                 if (!f_reexport)
147                         xtab_export_read();
148                 if (!f_export)
149                         for (i = optind ; i < argc ; i++)
150                                 unexportfs(argv[i], f_verbose);
151                 if (!new_cache)
152                         rmtab_read();
153         }
154         if (!new_cache) {
155                 xtab_mount_read();
156                 exports_update(f_verbose);
157         }
158         xtab_export_write();
159         if (new_cache)
160                 cache_flush(force_flush);
161         if (!new_cache)
162                 xtab_mount_write();
163
164         return export_errno;
165 }
166
167 static void
168 exports_update_one(nfs_export *exp, int verbose)
169 {
170                 /* check mountpoint option */
171         if (exp->m_mayexport &&
172             exp->m_export.e_mountpoint &&
173             !is_mountpoint(exp->m_export.e_mountpoint[0]?
174                            exp->m_export.e_mountpoint:
175                            exp->m_export.e_path)) {
176                 printf("%s not exported as %s not a mountpoint.\n",
177                        exp->m_export.e_path, exp->m_export.e_mountpoint);
178                 exp->m_mayexport = 0;
179         }
180         if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
181                 if (verbose)
182                         printf("%sexporting %s:%s to kernel\n",
183                                exp->m_exported ?"re":"",
184                                exp->m_client->m_hostname,
185                                exp->m_export.e_path);
186                 if (!export_export(exp))
187                         error(exp, errno);
188         }
189         if (exp->m_exported && ! exp->m_mayexport) {
190                 if (verbose)
191                         printf("unexporting %s:%s from kernel\n",
192                                exp->m_client->m_hostname,
193                                exp->m_export.e_path);
194                 if (!export_unexport(exp))
195                         error(exp, errno);
196         }
197 }
198
199
200 /* we synchronise intention with reality.
201  * entries with m_mayexport get exported
202  * entries with m_exported but not m_mayexport get unexported
203  * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
204  */
205 static void
206 exports_update(int verbose)
207 {
208         nfs_export      *exp;
209
210         for (exp = exportlist[MCL_FQDN].p_head; exp; exp=exp->m_next) {
211                 exports_update_one(exp, verbose);
212         }
213         for (exp = exportlist[MCL_GSS].p_head; exp; exp=exp->m_next) {
214                 exports_update_one(exp, verbose);
215         }
216 }
217                         
218 /*
219  * export_all finds all entries and
220  *    marks them xtabent and mayexport so that they get exported
221  */
222 static void
223 export_all(int verbose)
224 {
225         nfs_export      *exp;
226         int             i;
227
228         for (i = 0; i < MCL_MAXTYPES; i++) {
229                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
230                         if (verbose)
231                                 printf("exporting %s:%s\n",
232                                        exp->m_client->m_hostname, 
233                                        exp->m_export.e_path);
234                         exp->m_xtabent = 1;
235                         exp->m_mayexport = 1;
236                         exp->m_changed = 1;
237                         exp->m_warned = 0;
238                         validate_export(exp);
239                 }
240         }
241 }
242
243
244 static void
245 exportfs(char *arg, char *options, int verbose)
246 {
247         struct exportent *eep;
248         nfs_export      *exp;
249         struct addrinfo *ai = NULL;
250         char            *path;
251         char            *hname = arg;
252         int             htype;
253
254         if ((path = strchr(arg, ':')) != NULL)
255                 *path++ = '\0';
256
257         if (!path || *path != '/') {
258                 xlog(L_ERROR, "Invalid exporting option: %s", arg);
259                 return;
260         }
261
262         if ((htype = client_gettype(hname)) == MCL_FQDN) {
263                 ai = host_addrinfo(hname);
264                 if (ai != NULL) {
265                         exp = export_find(ai, path);
266                         hname = ai->ai_canonname;
267                 }
268         } else
269                 exp = export_lookup(hname, path, 0);
270
271         if (!exp) {
272                 if (!(eep = mkexportent(hname, path, options)) ||
273                     !(exp = export_create(eep, 0)))
274                         goto out;
275         } else if (!updateexportent(&exp->m_export, options))
276                 goto out;
277
278         if (verbose)
279                 printf("exporting %s:%s\n", exp->m_client->m_hostname, 
280                         exp->m_export.e_path);
281         exp->m_xtabent = 1;
282         exp->m_mayexport = 1;
283         exp->m_changed = 1;
284         exp->m_warned = 0;
285         validate_export(exp);
286
287 out:
288         freeaddrinfo(ai);
289 }
290
291 static void
292 unexportfs(char *arg, int verbose)
293 {
294         nfs_export      *exp;
295         struct addrinfo *ai = NULL;
296         char            *path;
297         char            *hname = arg;
298         int             htype;
299
300         if ((path = strchr(arg, ':')) != NULL)
301                 *path++ = '\0';
302
303         if (!path || *path != '/') {
304                 xlog(L_ERROR, "Invalid unexporting option: %s", arg);
305                 return;
306         }
307
308         if ((htype = client_gettype(hname)) == MCL_FQDN) {
309                 ai = host_addrinfo(hname);
310                 if (ai)
311                         hname = ai->ai_canonname;
312         }
313
314         for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
315                 if (path && strcmp(path, exp->m_export.e_path))
316                         continue;
317                 if (htype != exp->m_client->m_type)
318                         continue;
319                 if (htype == MCL_FQDN
320                     && !matchhostname(exp->m_export.e_hostname,
321                                           hname))
322                         continue;
323                 if (htype != MCL_FQDN
324                     && strcasecmp(exp->m_export.e_hostname, hname))
325                         continue;
326                 if (verbose) {
327 #if 0
328                         if (exp->m_exported) {
329                                 printf("unexporting %s:%s from kernel\n",
330                                        exp->m_client->m_hostname,
331                                        exp->m_export.e_path);
332                         }
333                         else
334 #endif
335                                 printf("unexporting %s:%s\n",
336                                         exp->m_client->m_hostname, 
337                                         exp->m_export.e_path);
338                 }
339 #if 0
340                 if (exp->m_exported && !export_unexport(exp))
341                         error(exp, errno);
342 #endif
343                 exp->m_xtabent = 0;
344                 exp->m_mayexport = 0;
345         }
346
347         freeaddrinfo(ai);
348 }
349
350 static int can_test(void)
351 {
352         int fd;
353         int n;
354         char *setup = "nfsd 0.0.0.0 2147483647 -test-client-\n";
355         fd = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
356         if ( fd < 0) return 0;
357         n = write(fd, setup, strlen(setup));
358         close(fd);
359         if (n < 0)
360                 return 0;
361         fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
362         if ( fd < 0) return 0;
363         close(fd);
364         return 1;
365 }
366
367 static int test_export(char *path, int with_fsid)
368 {
369         char buf[1024];
370         int fd, n;
371
372         sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
373                 path,
374                 with_fsid ? NFSEXP_FSID : 0);
375         fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
376         if (fd < 0)
377                 return 0;
378         n = write(fd, buf, strlen(buf));
379         close(fd);
380         if (n < 0)
381                 return 0;
382         return 1;
383 }
384
385 static void
386 validate_export(nfs_export *exp)
387 {
388         /* Check that the given export point is potentially exportable.
389          * We just give warnings here, don't cause anything to fail.
390          * If a path doesn't exist, or is not a dir or file, give an warning
391          * otherwise trial-export to '-test-client-' and check for failure.
392          */
393         struct stat stb;
394         char *path = exp->m_export.e_path;
395         struct statfs64 stf;
396         int fs_has_fsid = 0;
397
398         if (stat(path, &stb) < 0) {
399                 xlog(L_ERROR, "Failed to stat %s: %m \n", path);
400                 return;
401         }
402         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
403                 xlog(L_ERROR, "%s is neither a directory nor a file. "
404                         "Remote access will fail", path);
405                 return;
406         }
407         if (!can_test())
408                 return;
409
410         if (!statfs64(path, &stf) &&
411             (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
412                 fs_has_fsid = 1;
413
414         if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
415             fs_has_fsid) {
416                 if ( !test_export(path, 1)) {
417                         xlog(L_ERROR, "%s does not support NFS export", path);
418                         return;
419                 }
420         } else if ( ! test_export(path, 0)) {
421                 if (test_export(path, 1))
422                         xlog(L_ERROR, "%s requires fsid= for NFS export", path);
423                 else
424                         xlog(L_ERROR, "%s does not support NFS export", path);
425                 return;
426
427         }
428 }
429
430 static _Bool
431 is_hostname(const char *sp)
432 {
433         if (*sp == '\0' || *sp == '@')
434                 return false;
435
436         for (; *sp != '\0'; sp++) {
437                 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
438                         return false;
439                 if (*sp == '\\' && sp[1] != '\0')
440                         sp++;
441         }
442
443         return true;
444 }
445
446 static _Bool
447 compare_sockaddrs4(const struct sockaddr *sa1, const struct sockaddr *sa2)
448 {
449         const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
450         const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
451         return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
452 }
453
454 static _Bool
455 compare_sockaddrs(const struct sockaddr *sa1, const struct sockaddr *sa2)
456 {
457         if (sa1->sa_family == sa2->sa_family)
458                 switch (sa1->sa_family) {
459                 case AF_INET:
460                         return compare_sockaddrs4(sa1, sa2);
461                 }
462
463         return false;
464 }
465
466 static int
467 matchhostname(const char *hostname1, const char *hostname2)
468 {
469         struct addrinfo *results1 = NULL, *results2 = NULL;
470         struct addrinfo *ai1, *ai2;
471         int result = 0;
472
473         if (strcasecmp(hostname1, hostname2) == 0)
474                 return 1;
475
476         /*
477          * Don't pass export wildcards or netgroup names to DNS
478          */
479         if (!is_hostname(hostname1) || !is_hostname(hostname2))
480                 return 0;
481
482         results1 = host_addrinfo(hostname1);
483         if (results1 == NULL)
484                 goto out;
485         results2 = host_addrinfo(hostname2);
486         if (results2 == NULL)
487                 goto out;
488
489         if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
490                 result = 1;
491                 goto out;
492         }
493
494         for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
495                 for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
496                         if (compare_sockaddrs(ai1->ai_addr, ai2->ai_addr)) {
497                                 result = 1;
498                                 break;
499                         }
500
501 out:
502         freeaddrinfo(results1);
503         freeaddrinfo(results2);
504         return result;
505 }
506
507 static char
508 dumpopt(char c, char *fmt, ...)
509 {
510         va_list ap;
511
512         va_start(ap, fmt);
513         printf("%c", c);
514         vprintf(fmt, ap);
515         va_end(ap);
516         return ',';
517 }
518
519 static void
520 dump(int verbose)
521 {
522         nfs_export      *exp;
523         struct exportent *ep;
524         int             htype;
525         char            *hname, c;
526
527         for (htype = 0; htype < MCL_MAXTYPES; htype++) {
528                 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
529                         ep = &exp->m_export;
530                         if (!exp->m_xtabent)
531                             continue; /* neilb */
532                         if (htype == MCL_ANONYMOUS)
533                                 hname = "<world>";
534                         else
535                                 hname = ep->e_hostname;
536                         if (strlen(ep->e_path) > 14)
537                                 printf("%-14s\n\t\t%s", ep->e_path, hname);
538                         else
539                                 printf("%-14s\t%s", ep->e_path, hname);
540                         if (!verbose) {
541                                 printf("\n");
542                                 continue;
543                         }
544                         c = '(';
545                         if (ep->e_flags & NFSEXP_READONLY)
546                                 c = dumpopt(c, "ro");
547                         else
548                                 c = dumpopt(c, "rw");
549                         if (ep->e_flags & NFSEXP_ASYNC)
550                                 c = dumpopt(c, "async");
551                         if (ep->e_flags & NFSEXP_GATHERED_WRITES)
552                                 c = dumpopt(c, "wdelay");
553                         if (ep->e_flags & NFSEXP_NOHIDE)
554                                 c = dumpopt(c, "nohide");
555                         if (ep->e_flags & NFSEXP_CROSSMOUNT)
556                                 c = dumpopt(c, "crossmnt");
557                         if (ep->e_flags & NFSEXP_INSECURE_PORT)
558                                 c = dumpopt(c, "insecure");
559                         if (ep->e_flags & NFSEXP_ROOTSQUASH)
560                                 c = dumpopt(c, "root_squash");
561                         else
562                                 c = dumpopt(c, "no_root_squash");
563                         if (ep->e_flags & NFSEXP_ALLSQUASH)
564                                 c = dumpopt(c, "all_squash");
565                         if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
566                                 c = dumpopt(c, "no_subtree_check");
567                         if (ep->e_flags & NFSEXP_NOAUTHNLM)
568                                 c = dumpopt(c, "insecure_locks");
569                         if (ep->e_flags & NFSEXP_NOACL)
570                                 c = dumpopt(c, "no_acl");
571                         if (ep->e_flags & NFSEXP_FSID)
572                                 c = dumpopt(c, "fsid=%d", ep->e_fsid);
573                         if (ep->e_uuid)
574                                 c = dumpopt(c, "fsid=%s", ep->e_uuid);
575                         if (ep->e_mountpoint)
576                                 c = dumpopt(c, "mountpoint%s%s", 
577                                             ep->e_mountpoint[0]?"=":"", 
578                                             ep->e_mountpoint);
579                         if (ep->e_anonuid != 65534)
580                                 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
581                         if (ep->e_anongid != 65534)
582                                 c = dumpopt(c, "anongid=%d", ep->e_anongid);
583                         switch(ep->e_fslocmethod) {
584                         case FSLOC_NONE:
585                                 break;
586                         case FSLOC_REFER:
587                                 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
588                                 break;
589                         case FSLOC_REPLICA:
590                                 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
591                                 break;
592 #ifdef DEBUG
593                         case FSLOC_STUB:
594                                 c = dumpopt(c, "fsloc=stub");
595                                 break;
596 #endif
597                         }
598                         secinfo_show(stdout, ep);
599                         printf("%c\n", (c != '(')? ')' : ' ');
600                 }
601         }
602 }
603
604 static void
605 error(nfs_export *exp, int err)
606 {
607         xlog(L_ERROR, "%s:%s: %s\n", exp->m_client->m_hostname,
608                 exp->m_export.e_path, strerror(err));
609 }
610
611 static void
612 usage(const char *progname)
613 {
614         fprintf(stderr, "usage: %s [-aruv] [host:/path]\n", progname);
615         exit(1);
616 }