]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/exportfs/exportfs.c
nfsdcld: demote pipe opening error to D_GENERAL
[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 <sys/file.h>
20 #include <unistd.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <getopt.h>
26 #include <fcntl.h>
27 #include <netdb.h>
28 #include <errno.h>
29 #include <dirent.h>
30
31 #include "sockaddr.h"
32 #include "misc.h"
33 #include "nfslib.h"
34 #include "exportfs.h"
35 #include "xlog.h"
36
37 static void     export_all(int verbose);
38 static void     exportfs(char *arg, char *options, int verbose);
39 static void     unexportfs(char *arg, int verbose);
40 static void     exports_update(int verbose);
41 static void     dump(int verbose);
42 static void     error(nfs_export *exp, int err);
43 static void     usage(const char *progname);
44 static void     validate_export(nfs_export *exp);
45 static int      matchhostname(const char *hostname1, const char *hostname2);
46 static void     export_d_read(const char *dname);
47 static void grab_lockfile(void);
48 static void release_lockfile(void);
49
50 static const char *lockfile = EXP_LOCKFILE;
51 static int _lockfd = -1;
52
53 /*
54  * If we aren't careful, changes made by exportfs can be lost
55  * when multiple exports process run at once:
56  *
57  *      exportfs process 1      exportfs process 2
58  *      ------------------------------------------
59  *      reads etab version A    reads etab version A
60  *      adds new export B       adds new export C
61  *      writes A+B              writes A+C
62  *
63  * The locking in support/export/xtab.c will prevent mountd from
64  * seeing a partially written version of etab, and will prevent 
65  * the two writers above from writing simultaneously and
66  * corrupting etab, but to prevent problems like the above we
67  * need these additional lockfile() routines.
68  */
69 static void 
70 grab_lockfile()
71 {
72         _lockfd = open(lockfile, O_CREAT|O_RDWR, 0666);
73         if (_lockfd != -1) 
74                 lockf(_lockfd, F_LOCK, 0);
75 }
76 static void 
77 release_lockfile()
78 {
79         if (_lockfd != -1)
80                 lockf(_lockfd, F_ULOCK, 0);
81 }
82
83 int
84 main(int argc, char **argv)
85 {
86         char    *options = NULL;
87         char    *progname = NULL;
88         int     f_export = 1;
89         int     f_all = 0;
90         int     f_verbose = 0;
91         int     f_reexport = 0;
92         int     f_ignore = 0;
93         int     i, c;
94         int     new_cache = 0;
95         int     force_flush = 0;
96
97         if ((progname = strrchr(argv[0], '/')) != NULL)
98                 progname++;
99         else
100                 progname = argv[0];
101
102         xlog_open(progname);
103         xlog_stderr(1);
104         xlog_syslog(0);
105
106         export_errno = 0;
107
108         while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
109                 switch(c) {
110                 case 'a':
111                         f_all = 1;
112                         break;
113                 case 'i':
114                         f_ignore = 1;
115                         break;
116                 case 'o':
117                         options = optarg;
118                         break;
119                 case 'r':
120                         f_reexport = 1;
121                         f_all = 1;
122                         break;
123                 case 'u':
124                         f_export = 0;
125                         break;
126                 case 'v':
127                         f_verbose = 1;
128                         break;
129                 case 'f':
130                         force_flush = 1;
131                         break;
132                 default:
133                         usage(progname);
134                         break;
135                 }
136         }
137
138         if (optind != argc && f_all) {
139                 xlog(L_ERROR, "extra arguments are not permitted with -a or -r");
140                 return 1;
141         }
142         if (f_ignore && (f_all || ! f_export)) {
143                 xlog(L_ERROR, "-i not meaningful with -a, -r or -u");
144                 return 1;
145         }
146         if (f_reexport && ! f_export) {
147                 xlog(L_ERROR, "-r and -u are incompatible");
148                 return 1;
149         }
150         new_cache = check_new_cache();
151         if (optind == argc && ! f_all) {
152                 if (force_flush) {
153                         if (new_cache)
154                                 cache_flush(1);
155                         else {
156                                 xlog(L_ERROR, "-f is available only "
157                                         "with new cache controls. "
158                                         "Mount /proc/fs/nfsd first");
159                                 return 1;
160                         }
161                         return 0;
162                 } else {
163                         xtab_export_read();
164                         dump(f_verbose);
165                         return 0;
166                 }
167         }
168
169         /*
170          * Serialize things as best we can
171          */
172         grab_lockfile();
173         atexit(release_lockfile);
174
175         if (f_export && ! f_ignore) {
176                 export_read(_PATH_EXPORTS);
177                 export_d_read(_PATH_EXPORTS_D);
178         }
179         if (f_export) {
180                 if (f_all)
181                         export_all(f_verbose);
182                 else
183                         for (i = optind; i < argc ; i++)
184                                 exportfs(argv[i], options, f_verbose);
185         }
186         /* If we are unexporting everything, then
187          * don't care about what should be exported, as that
188          * may require DNS lookups..
189          */
190         if (! ( !f_export && f_all)) {
191                 /* note: xtab_*_read does not update entries if they already exist,
192                  * so this will not lose new options
193                  */
194                 if (!f_reexport)
195                         xtab_export_read();
196                 if (!f_export)
197                         for (i = optind ; i < argc ; i++)
198                                 unexportfs(argv[i], f_verbose);
199                 if (!new_cache)
200                         rmtab_read();
201         }
202         if (!new_cache) {
203                 xtab_mount_read();
204                 exports_update(f_verbose);
205         }
206         xtab_export_write();
207         if (new_cache)
208                 cache_flush(force_flush);
209         if (!new_cache)
210                 xtab_mount_write();
211
212         return export_errno;
213 }
214
215 static void
216 exports_update_one(nfs_export *exp, int verbose)
217 {
218                 /* check mountpoint option */
219         if (exp->m_mayexport &&
220             exp->m_export.e_mountpoint &&
221             !is_mountpoint(exp->m_export.e_mountpoint[0]?
222                            exp->m_export.e_mountpoint:
223                            exp->m_export.e_path)) {
224                 printf("%s not exported as %s not a mountpoint.\n",
225                        exp->m_export.e_path, exp->m_export.e_mountpoint);
226                 exp->m_mayexport = 0;
227         }
228         if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
229                 if (verbose)
230                         printf("%sexporting %s:%s to kernel\n",
231                                exp->m_exported ?"re":"",
232                                exp->m_client->m_hostname,
233                                exp->m_export.e_path);
234                 if (!export_export(exp))
235                         error(exp, errno);
236         }
237         if (exp->m_exported && ! exp->m_mayexport) {
238                 if (verbose)
239                         printf("unexporting %s:%s from kernel\n",
240                                exp->m_client->m_hostname,
241                                exp->m_export.e_path);
242                 if (!export_unexport(exp))
243                         error(exp, errno);
244         }
245 }
246
247
248 /* we synchronise intention with reality.
249  * entries with m_mayexport get exported
250  * entries with m_exported but not m_mayexport get unexported
251  * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
252  */
253 static void
254 exports_update(int verbose)
255 {
256         nfs_export      *exp;
257
258         for (exp = exportlist[MCL_FQDN].p_head; exp; exp=exp->m_next) {
259                 exports_update_one(exp, verbose);
260         }
261         for (exp = exportlist[MCL_GSS].p_head; exp; exp=exp->m_next) {
262                 exports_update_one(exp, verbose);
263         }
264 }
265                         
266 /*
267  * export_all finds all entries and
268  *    marks them xtabent and mayexport so that they get exported
269  */
270 static void
271 export_all(int verbose)
272 {
273         nfs_export      *exp;
274         int             i;
275
276         for (i = 0; i < MCL_MAXTYPES; i++) {
277                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
278                         if (verbose)
279                                 printf("exporting %s:%s\n",
280                                        exp->m_client->m_hostname, 
281                                        exp->m_export.e_path);
282                         exp->m_xtabent = 1;
283                         exp->m_mayexport = 1;
284                         exp->m_changed = 1;
285                         exp->m_warned = 0;
286                         validate_export(exp);
287                 }
288         }
289 }
290
291
292 static void
293 exportfs(char *arg, char *options, int verbose)
294 {
295         struct exportent *eep;
296         nfs_export      *exp = NULL;
297         struct addrinfo *ai = NULL;
298         char            *path;
299         char            *hname = arg;
300         int             htype;
301
302         if ((path = strchr(arg, ':')) != NULL)
303                 *path++ = '\0';
304
305         if (!path || *path != '/') {
306                 xlog(L_ERROR, "Invalid exporting option: %s", arg);
307                 return;
308         }
309
310         if ((htype = client_gettype(hname)) == MCL_FQDN) {
311                 ai = host_addrinfo(hname);
312                 if (ai != NULL) {
313                         exp = export_find(ai, path);
314                         hname = ai->ai_canonname;
315                 }
316         } else
317                 exp = export_lookup(hname, path, 0);
318
319         if (!exp) {
320                 if (!(eep = mkexportent(hname, path, options)) ||
321                     !(exp = export_create(eep, 0)))
322                         goto out;
323         } else if (!updateexportent(&exp->m_export, options))
324                 goto out;
325
326         if (verbose)
327                 printf("exporting %s:%s\n", exp->m_client->m_hostname, 
328                         exp->m_export.e_path);
329         exp->m_xtabent = 1;
330         exp->m_mayexport = 1;
331         exp->m_changed = 1;
332         exp->m_warned = 0;
333         validate_export(exp);
334
335 out:
336         freeaddrinfo(ai);
337 }
338
339 static void
340 unexportfs(char *arg, int verbose)
341 {
342         nfs_export      *exp;
343         struct addrinfo *ai = NULL;
344         char            *path;
345         char            *hname = arg;
346         int             htype;
347
348         if ((path = strchr(arg, ':')) != NULL)
349                 *path++ = '\0';
350
351         if (!path || *path != '/') {
352                 xlog(L_ERROR, "Invalid unexporting option: %s", arg);
353                 return;
354         }
355
356         if ((htype = client_gettype(hname)) == MCL_FQDN) {
357                 ai = host_addrinfo(hname);
358                 if (ai)
359                         hname = ai->ai_canonname;
360         }
361
362         for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
363                 if (path && strcmp(path, exp->m_export.e_path))
364                         continue;
365                 if (htype != exp->m_client->m_type)
366                         continue;
367                 if (htype == MCL_FQDN
368                     && !matchhostname(exp->m_export.e_hostname,
369                                           hname))
370                         continue;
371                 if (htype != MCL_FQDN
372                     && strcasecmp(exp->m_export.e_hostname, hname))
373                         continue;
374                 if (verbose) {
375 #if 0
376                         if (exp->m_exported) {
377                                 printf("unexporting %s:%s from kernel\n",
378                                        exp->m_client->m_hostname,
379                                        exp->m_export.e_path);
380                         }
381                         else
382 #endif
383                                 printf("unexporting %s:%s\n",
384                                         exp->m_client->m_hostname, 
385                                         exp->m_export.e_path);
386                 }
387 #if 0
388                 if (exp->m_exported && !export_unexport(exp))
389                         error(exp, errno);
390 #endif
391                 exp->m_xtabent = 0;
392                 exp->m_mayexport = 0;
393         }
394
395         freeaddrinfo(ai);
396 }
397
398 static int can_test(void)
399 {
400         int fd;
401         int n;
402         char *setup = "nfsd 0.0.0.0 2147483647 -test-client-\n";
403         fd = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
404         if ( fd < 0) return 0;
405         n = write(fd, setup, strlen(setup));
406         close(fd);
407         if (n < 0)
408                 return 0;
409         fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
410         if ( fd < 0) return 0;
411         close(fd);
412         return 1;
413 }
414
415 static int test_export(char *path, int with_fsid)
416 {
417         char buf[1024];
418         int fd, n;
419
420         sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
421                 path,
422                 with_fsid ? NFSEXP_FSID : 0);
423         fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
424         if (fd < 0)
425                 return 0;
426         n = write(fd, buf, strlen(buf));
427         close(fd);
428         if (n < 0)
429                 return 0;
430         return 1;
431 }
432
433 static void
434 validate_export(nfs_export *exp)
435 {
436         /* Check that the given export point is potentially exportable.
437          * We just give warnings here, don't cause anything to fail.
438          * If a path doesn't exist, or is not a dir or file, give an warning
439          * otherwise trial-export to '-test-client-' and check for failure.
440          */
441         struct stat stb;
442         char *path = exp->m_export.e_path;
443         struct statfs64 stf;
444         int fs_has_fsid = 0;
445
446         if (stat(path, &stb) < 0) {
447                 xlog(L_ERROR, "Failed to stat %s: %m", path);
448                 return;
449         }
450         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
451                 xlog(L_ERROR, "%s is neither a directory nor a file. "
452                         "Remote access will fail", path);
453                 return;
454         }
455         if (!can_test())
456                 return;
457
458         if (!statfs64(path, &stf) &&
459             (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
460                 fs_has_fsid = 1;
461
462         if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
463             fs_has_fsid) {
464                 if ( !test_export(path, 1)) {
465                         xlog(L_ERROR, "%s does not support NFS export", path);
466                         return;
467                 }
468         } else if ( ! test_export(path, 0)) {
469                 if (test_export(path, 1))
470                         xlog(L_ERROR, "%s requires fsid= for NFS export", path);
471                 else
472                         xlog(L_ERROR, "%s does not support NFS export", path);
473                 return;
474
475         }
476 }
477
478 static _Bool
479 is_hostname(const char *sp)
480 {
481         if (*sp == '\0' || *sp == '@')
482                 return false;
483
484         for (; *sp != '\0'; sp++) {
485                 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
486                         return false;
487                 if (*sp == '\\' && sp[1] != '\0')
488                         sp++;
489         }
490
491         return true;
492 }
493
494 /*
495  * Take care to perform an explicit reverse lookup on presentation
496  * addresses.  Otherwise we don't get a real canonical name or a
497  * complete list of addresses.
498  */
499 static struct addrinfo *
500 address_list(const char *hostname)
501 {
502         struct addrinfo *ai;
503         char *cname;
504
505         ai = host_pton(hostname);
506         if (ai != NULL) {
507                 /* @hostname was a presentation address */
508                 cname = host_canonname(ai->ai_addr);
509                 freeaddrinfo(ai);
510                 if (cname != NULL)
511                         goto out;
512         }
513         /* @hostname was a hostname or had no reverse mapping */
514         cname = strdup(hostname);
515         if (cname == NULL)
516                 return NULL;
517
518 out:
519         ai = host_addrinfo(cname);
520         free(cname);
521         return ai;
522 }
523
524 static int
525 matchhostname(const char *hostname1, const char *hostname2)
526 {
527         struct addrinfo *results1 = NULL, *results2 = NULL;
528         struct addrinfo *ai1, *ai2;
529         int result = 0;
530
531         if (strcasecmp(hostname1, hostname2) == 0)
532                 return 1;
533
534         /*
535          * Don't pass export wildcards or netgroup names to DNS
536          */
537         if (!is_hostname(hostname1) || !is_hostname(hostname2))
538                 return 0;
539
540         results1 = address_list(hostname1);
541         if (results1 == NULL)
542                 goto out;
543         results2 = address_list(hostname2);
544         if (results2 == NULL)
545                 goto out;
546
547         if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
548                 result = 1;
549                 goto out;
550         }
551
552         for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
553                 for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
554                         if (nfs_compare_sockaddr(ai1->ai_addr, ai2->ai_addr)) {
555                                 result = 1;
556                                 break;
557                         }
558
559 out:
560         freeaddrinfo(results1);
561         freeaddrinfo(results2);
562         return result;
563 }
564
565 /* Based on mnt_table_parse_dir() in
566    util-linux-ng/shlibs/mount/src/tab_parse.c */
567 static void
568 export_d_read(const char *dname)
569 {
570         int n = 0, i;
571         struct dirent **namelist = NULL;
572
573
574         n = scandir(dname, &namelist, NULL, versionsort);
575         if (n < 0) {
576                 if (errno == ENOENT)
577                         /* Silently return */
578                         return;
579                 xlog(L_NOTICE, "scandir %s: %s", dname, strerror(errno));
580         } else if (n == 0)
581                 return;
582
583         for (i = 0; i < n; i++) {
584                 struct dirent *d = namelist[i];
585                 size_t namesz;
586                 char fname[PATH_MAX + 1];
587                 int fname_len;
588
589
590                 if (d->d_type != DT_UNKNOWN 
591                     && d->d_type != DT_REG
592                     && d->d_type != DT_LNK)
593                         continue;
594                 if (*d->d_name == '.')
595                         continue;
596
597 #define _EXT_EXPORT_SIZ   (sizeof(_EXT_EXPORT) - 1)
598                 namesz = strlen(d->d_name);
599                 if (!namesz 
600                     || namesz < _EXT_EXPORT_SIZ + 1
601                     || strcmp(d->d_name + (namesz - _EXT_EXPORT_SIZ),
602                               _EXT_EXPORT))
603                         continue;
604
605                 fname_len = snprintf(fname, PATH_MAX +1, "%s/%s", dname, d->d_name);
606                 if (fname_len > PATH_MAX) {
607                         xlog(L_WARNING, "Too long file name: %s in %s", d->d_name, dname);
608                         continue;
609                 }
610
611                 export_read(fname);
612         }
613                 
614         for (i = 0; i < n; i++)
615                 free(namelist[i]);
616         free(namelist);
617
618         return;
619 }
620
621 static char
622 dumpopt(char c, char *fmt, ...)
623 {
624         va_list ap;
625
626         va_start(ap, fmt);
627         printf("%c", c);
628         vprintf(fmt, ap);
629         va_end(ap);
630         return ',';
631 }
632
633 static void
634 dump(int verbose)
635 {
636         nfs_export      *exp;
637         struct exportent *ep;
638         int             htype;
639         char            *hname, c;
640
641         for (htype = 0; htype < MCL_MAXTYPES; htype++) {
642                 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
643                         ep = &exp->m_export;
644                         if (!exp->m_xtabent)
645                             continue; /* neilb */
646                         if (htype == MCL_ANONYMOUS)
647                                 hname = "<world>";
648                         else
649                                 hname = ep->e_hostname;
650                         if (strlen(ep->e_path) > 14)
651                                 printf("%-14s\n\t\t%s", ep->e_path, hname);
652                         else
653                                 printf("%-14s\t%s", ep->e_path, hname);
654                         if (!verbose) {
655                                 printf("\n");
656                                 continue;
657                         }
658                         c = '(';
659                         if (ep->e_flags & NFSEXP_READONLY)
660                                 c = dumpopt(c, "ro");
661                         else
662                                 c = dumpopt(c, "rw");
663                         if (ep->e_flags & NFSEXP_ASYNC)
664                                 c = dumpopt(c, "async");
665                         if (ep->e_flags & NFSEXP_GATHERED_WRITES)
666                                 c = dumpopt(c, "wdelay");
667                         if (ep->e_flags & NFSEXP_NOHIDE)
668                                 c = dumpopt(c, "nohide");
669                         if (ep->e_flags & NFSEXP_CROSSMOUNT)
670                                 c = dumpopt(c, "crossmnt");
671                         if (ep->e_flags & NFSEXP_INSECURE_PORT)
672                                 c = dumpopt(c, "insecure");
673                         if (ep->e_flags & NFSEXP_ROOTSQUASH)
674                                 c = dumpopt(c, "root_squash");
675                         else
676                                 c = dumpopt(c, "no_root_squash");
677                         if (ep->e_flags & NFSEXP_ALLSQUASH)
678                                 c = dumpopt(c, "all_squash");
679                         if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
680                                 c = dumpopt(c, "no_subtree_check");
681                         if (ep->e_flags & NFSEXP_NOAUTHNLM)
682                                 c = dumpopt(c, "insecure_locks");
683                         if (ep->e_flags & NFSEXP_NOACL)
684                                 c = dumpopt(c, "no_acl");
685                         if (ep->e_flags & NFSEXP_FSID)
686                                 c = dumpopt(c, "fsid=%d", ep->e_fsid);
687                         if (ep->e_uuid)
688                                 c = dumpopt(c, "fsid=%s", ep->e_uuid);
689                         if (ep->e_mountpoint)
690                                 c = dumpopt(c, "mountpoint%s%s", 
691                                             ep->e_mountpoint[0]?"=":"", 
692                                             ep->e_mountpoint);
693                         if (ep->e_anonuid != 65534)
694                                 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
695                         if (ep->e_anongid != 65534)
696                                 c = dumpopt(c, "anongid=%d", ep->e_anongid);
697                         switch(ep->e_fslocmethod) {
698                         case FSLOC_NONE:
699                                 break;
700                         case FSLOC_REFER:
701                                 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
702                                 break;
703                         case FSLOC_REPLICA:
704                                 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
705                                 break;
706 #ifdef DEBUG
707                         case FSLOC_STUB:
708                                 c = dumpopt(c, "fsloc=stub");
709                                 break;
710 #endif
711                         }
712                         secinfo_show(stdout, ep);
713                         printf("%c\n", (c != '(')? ')' : ' ');
714                 }
715         }
716 }
717
718 static void
719 error(nfs_export *exp, int err)
720 {
721         xlog(L_ERROR, "%s:%s: %s", exp->m_client->m_hostname,
722                 exp->m_export.e_path, strerror(err));
723 }
724
725 static void
726 usage(const char *progname)
727 {
728         fprintf(stderr, "usage: %s [-aruv] [host:/path]\n", progname);
729         exit(1);
730 }