2 * utils/exportfs/exportfs.c
4 * Export file systems to knfsd
6 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
8 * Extensive changes, 1999, Neil Brown <neilb@cse.unsw.edu.au>
15 #include <sys/types.h>
36 static void export_all(int verbose);
37 static void exportfs(char *arg, char *options, int verbose);
38 static void unexportfs(char *arg, int verbose);
39 static void exports_update(int verbose);
40 static void dump(int verbose);
41 static void error(nfs_export *exp, int err);
42 static void usage(const char *progname);
43 static void validate_export(nfs_export *exp);
44 static int matchhostname(const char *hostname1, const char *hostname2);
45 static void export_d_read(const char *dname);
48 main(int argc, char **argv)
51 char *progname = NULL;
61 if ((progname = strrchr(argv[0], '/')) != NULL)
72 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
102 if (optind != argc && f_all) {
103 xlog(L_ERROR, "extra arguments are not permitted with -a or -r");
106 if (f_ignore && (f_all || ! f_export)) {
107 xlog(L_ERROR, "-i not meaningful with -a, -r or -u");
110 if (f_reexport && ! f_export) {
111 xlog(L_ERROR, "-r and -u are incompatible");
114 new_cache = check_new_cache();
115 if (optind == argc && ! f_all) {
120 xlog(L_ERROR, "-f is available only "
121 "with new cache controls. "
122 "Mount /proc/fs/nfsd first");
132 if (f_export && ! f_ignore) {
133 export_read(_PATH_EXPORTS);
134 export_d_read(_PATH_EXPORTS_D);
138 export_all(f_verbose);
140 for (i = optind; i < argc ; i++)
141 exportfs(argv[i], options, f_verbose);
143 /* If we are unexporting everything, then
144 * don't care about what should be exported, as that
145 * may require DNS lookups..
147 if (! ( !f_export && f_all)) {
148 /* note: xtab_*_read does not update entries if they already exist,
149 * so this will not lose new options
154 for (i = optind ; i < argc ; i++)
155 unexportfs(argv[i], f_verbose);
161 exports_update(f_verbose);
165 cache_flush(force_flush);
173 exports_update_one(nfs_export *exp, int verbose)
175 /* check mountpoint option */
176 if (exp->m_mayexport &&
177 exp->m_export.e_mountpoint &&
178 !is_mountpoint(exp->m_export.e_mountpoint[0]?
179 exp->m_export.e_mountpoint:
180 exp->m_export.e_path)) {
181 printf("%s not exported as %s not a mountpoint.\n",
182 exp->m_export.e_path, exp->m_export.e_mountpoint);
183 exp->m_mayexport = 0;
185 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
187 printf("%sexporting %s:%s to kernel\n",
188 exp->m_exported ?"re":"",
189 exp->m_client->m_hostname,
190 exp->m_export.e_path);
191 if (!export_export(exp))
194 if (exp->m_exported && ! exp->m_mayexport) {
196 printf("unexporting %s:%s from kernel\n",
197 exp->m_client->m_hostname,
198 exp->m_export.e_path);
199 if (!export_unexport(exp))
205 /* we synchronise intention with reality.
206 * entries with m_mayexport get exported
207 * entries with m_exported but not m_mayexport get unexported
208 * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
211 exports_update(int verbose)
215 for (exp = exportlist[MCL_FQDN].p_head; exp; exp=exp->m_next) {
216 exports_update_one(exp, verbose);
218 for (exp = exportlist[MCL_GSS].p_head; exp; exp=exp->m_next) {
219 exports_update_one(exp, verbose);
224 * export_all finds all entries and
225 * marks them xtabent and mayexport so that they get exported
228 export_all(int verbose)
233 for (i = 0; i < MCL_MAXTYPES; i++) {
234 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
236 printf("exporting %s:%s\n",
237 exp->m_client->m_hostname,
238 exp->m_export.e_path);
240 exp->m_mayexport = 1;
243 validate_export(exp);
250 exportfs(char *arg, char *options, int verbose)
252 struct exportent *eep;
253 nfs_export *exp = NULL;
254 struct addrinfo *ai = NULL;
259 if ((path = strchr(arg, ':')) != NULL)
262 if (!path || *path != '/') {
263 xlog(L_ERROR, "Invalid exporting option: %s", arg);
267 if ((htype = client_gettype(hname)) == MCL_FQDN) {
268 ai = host_addrinfo(hname);
270 exp = export_find(ai, path);
271 hname = ai->ai_canonname;
274 exp = export_lookup(hname, path, 0);
277 if (!(eep = mkexportent(hname, path, options)) ||
278 !(exp = export_create(eep, 0)))
280 } else if (!updateexportent(&exp->m_export, options))
284 printf("exporting %s:%s\n", exp->m_client->m_hostname,
285 exp->m_export.e_path);
287 exp->m_mayexport = 1;
290 validate_export(exp);
297 unexportfs(char *arg, int verbose)
300 struct addrinfo *ai = NULL;
305 if ((path = strchr(arg, ':')) != NULL)
308 if (!path || *path != '/') {
309 xlog(L_ERROR, "Invalid unexporting option: %s", arg);
313 if ((htype = client_gettype(hname)) == MCL_FQDN) {
314 ai = host_addrinfo(hname);
316 hname = ai->ai_canonname;
319 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
320 if (path && strcmp(path, exp->m_export.e_path))
322 if (htype != exp->m_client->m_type)
324 if (htype == MCL_FQDN
325 && !matchhostname(exp->m_export.e_hostname,
328 if (htype != MCL_FQDN
329 && strcasecmp(exp->m_export.e_hostname, hname))
333 if (exp->m_exported) {
334 printf("unexporting %s:%s from kernel\n",
335 exp->m_client->m_hostname,
336 exp->m_export.e_path);
340 printf("unexporting %s:%s\n",
341 exp->m_client->m_hostname,
342 exp->m_export.e_path);
345 if (exp->m_exported && !export_unexport(exp))
349 exp->m_mayexport = 0;
355 static int can_test(void)
359 char *setup = "nfsd 0.0.0.0 2147483647 -test-client-\n";
360 fd = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
361 if ( fd < 0) return 0;
362 n = write(fd, setup, strlen(setup));
366 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
367 if ( fd < 0) return 0;
372 static int test_export(char *path, int with_fsid)
377 sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
379 with_fsid ? NFSEXP_FSID : 0);
380 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
383 n = write(fd, buf, strlen(buf));
391 validate_export(nfs_export *exp)
393 /* Check that the given export point is potentially exportable.
394 * We just give warnings here, don't cause anything to fail.
395 * If a path doesn't exist, or is not a dir or file, give an warning
396 * otherwise trial-export to '-test-client-' and check for failure.
399 char *path = exp->m_export.e_path;
403 if (stat(path, &stb) < 0) {
404 xlog(L_ERROR, "Failed to stat %s: %m \n", path);
407 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
408 xlog(L_ERROR, "%s is neither a directory nor a file. "
409 "Remote access will fail", path);
415 if (!statfs64(path, &stf) &&
416 (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
419 if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
421 if ( !test_export(path, 1)) {
422 xlog(L_ERROR, "%s does not support NFS export", path);
425 } else if ( ! test_export(path, 0)) {
426 if (test_export(path, 1))
427 xlog(L_ERROR, "%s requires fsid= for NFS export", path);
429 xlog(L_ERROR, "%s does not support NFS export", path);
436 is_hostname(const char *sp)
438 if (*sp == '\0' || *sp == '@')
441 for (; *sp != '\0'; sp++) {
442 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
444 if (*sp == '\\' && sp[1] != '\0')
452 matchhostname(const char *hostname1, const char *hostname2)
454 struct addrinfo *results1 = NULL, *results2 = NULL;
455 struct addrinfo *ai1, *ai2;
458 if (strcasecmp(hostname1, hostname2) == 0)
462 * Don't pass export wildcards or netgroup names to DNS
464 if (!is_hostname(hostname1) || !is_hostname(hostname2))
467 results1 = host_addrinfo(hostname1);
468 if (results1 == NULL)
470 results2 = host_addrinfo(hostname2);
471 if (results2 == NULL)
474 if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
479 for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
480 for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
481 if (nfs_compare_sockaddr(ai1->ai_addr, ai2->ai_addr)) {
487 freeaddrinfo(results1);
488 freeaddrinfo(results2);
492 /* Based on mnt_table_parse_dir() in
493 util-linux-ng/shlibs/mount/src/tab_parse.c */
495 export_d_read(const char *dname)
498 struct dirent **namelist = NULL;
501 n = scandir(dname, &namelist, NULL, versionsort);
503 xlog(L_NOTICE, "scandir %s: %s\n", dname, strerror(errno));
507 for (i = 0; i < n; i++) {
508 struct dirent *d = namelist[i];
510 char fname[PATH_MAX + 1];
514 if (d->d_type != DT_UNKNOWN
515 && d->d_type != DT_REG
516 && d->d_type != DT_LNK)
518 if (*d->d_name == '.')
521 #define _EXT_EXPORT_SIZ (sizeof(_EXT_EXPORT) - 1)
522 namesz = strlen(d->d_name);
524 || namesz < _EXT_EXPORT_SIZ + 1
525 || strcmp(d->d_name + (namesz - _EXT_EXPORT_SIZ),
529 fname_len = snprintf(fname, PATH_MAX +1, "%s/%s", dname, d->d_name);
530 if (fname_len > PATH_MAX) {
531 xlog(L_WARNING, "Too long file name: %s in %s\n", d->d_name, dname);
538 for (i = 0; i < n; i++)
546 dumpopt(char c, char *fmt, ...)
561 struct exportent *ep;
565 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
566 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
569 continue; /* neilb */
570 if (htype == MCL_ANONYMOUS)
573 hname = ep->e_hostname;
574 if (strlen(ep->e_path) > 14)
575 printf("%-14s\n\t\t%s", ep->e_path, hname);
577 printf("%-14s\t%s", ep->e_path, hname);
583 if (ep->e_flags & NFSEXP_READONLY)
584 c = dumpopt(c, "ro");
586 c = dumpopt(c, "rw");
587 if (ep->e_flags & NFSEXP_ASYNC)
588 c = dumpopt(c, "async");
589 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
590 c = dumpopt(c, "wdelay");
591 if (ep->e_flags & NFSEXP_NOHIDE)
592 c = dumpopt(c, "nohide");
593 if (ep->e_flags & NFSEXP_CROSSMOUNT)
594 c = dumpopt(c, "crossmnt");
595 if (ep->e_flags & NFSEXP_INSECURE_PORT)
596 c = dumpopt(c, "insecure");
597 if (ep->e_flags & NFSEXP_ROOTSQUASH)
598 c = dumpopt(c, "root_squash");
600 c = dumpopt(c, "no_root_squash");
601 if (ep->e_flags & NFSEXP_ALLSQUASH)
602 c = dumpopt(c, "all_squash");
603 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
604 c = dumpopt(c, "no_subtree_check");
605 if (ep->e_flags & NFSEXP_NOAUTHNLM)
606 c = dumpopt(c, "insecure_locks");
607 if (ep->e_flags & NFSEXP_NOACL)
608 c = dumpopt(c, "no_acl");
609 if (ep->e_flags & NFSEXP_FSID)
610 c = dumpopt(c, "fsid=%d", ep->e_fsid);
612 c = dumpopt(c, "fsid=%s", ep->e_uuid);
613 if (ep->e_mountpoint)
614 c = dumpopt(c, "mountpoint%s%s",
615 ep->e_mountpoint[0]?"=":"",
617 if (ep->e_anonuid != 65534)
618 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
619 if (ep->e_anongid != 65534)
620 c = dumpopt(c, "anongid=%d", ep->e_anongid);
621 switch(ep->e_fslocmethod) {
625 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
628 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
632 c = dumpopt(c, "fsloc=stub");
636 secinfo_show(stdout, ep);
637 printf("%c\n", (c != '(')? ')' : ' ');
643 error(nfs_export *exp, int err)
645 xlog(L_ERROR, "%s:%s: %s\n", exp->m_client->m_hostname,
646 exp->m_export.e_path, strerror(err));
650 usage(const char *progname)
652 fprintf(stderr, "usage: %s [-aruv] [host:/path]\n", progname);