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>
35 static void export_all(int verbose);
36 static void exportfs(char *arg, char *options, int verbose);
37 static void unexportfs(char *arg, int verbose);
38 static void exports_update(int verbose);
39 static void dump(int verbose);
40 static void error(nfs_export *exp, int err);
41 static void usage(const char *progname);
42 static void validate_export(nfs_export *exp);
43 static int matchhostname(const char *hostname1, const char *hostname2);
46 main(int argc, char **argv)
49 char *progname = NULL;
59 if ((progname = strrchr(argv[0], '/')) != NULL)
70 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
100 if (optind != argc && f_all) {
101 xlog(L_ERROR, "extra arguments are not permitted with -a or -r");
104 if (f_ignore && (f_all || ! f_export)) {
105 xlog(L_ERROR, "-i not meaningful with -a, -r or -u");
108 if (f_reexport && ! f_export) {
109 xlog(L_ERROR, "-r and -u are incompatible");
112 new_cache = check_new_cache();
113 if (optind == argc && ! f_all) {
118 xlog(L_ERROR, "-f is available only "
119 "with new cache controls. "
120 "Mount /proc/fs/nfsd first");
130 if (f_export && ! f_ignore)
131 export_read(_PATH_EXPORTS);
134 export_all(f_verbose);
136 for (i = optind; i < argc ; i++)
137 exportfs(argv[i], options, f_verbose);
139 /* If we are unexporting everything, then
140 * don't care about what should be exported, as that
141 * may require DNS lookups..
143 if (! ( !f_export && f_all)) {
144 /* note: xtab_*_read does not update entries if they already exist,
145 * so this will not lose new options
150 for (i = optind ; i < argc ; i++)
151 unexportfs(argv[i], f_verbose);
157 exports_update(f_verbose);
161 cache_flush(force_flush);
169 exports_update_one(nfs_export *exp, int verbose)
171 /* check mountpoint option */
172 if (exp->m_mayexport &&
173 exp->m_export.e_mountpoint &&
174 !is_mountpoint(exp->m_export.e_mountpoint[0]?
175 exp->m_export.e_mountpoint:
176 exp->m_export.e_path)) {
177 printf("%s not exported as %s not a mountpoint.\n",
178 exp->m_export.e_path, exp->m_export.e_mountpoint);
179 exp->m_mayexport = 0;
181 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
183 printf("%sexporting %s:%s to kernel\n",
184 exp->m_exported ?"re":"",
185 exp->m_client->m_hostname,
186 exp->m_export.e_path);
187 if (!export_export(exp))
190 if (exp->m_exported && ! exp->m_mayexport) {
192 printf("unexporting %s:%s from kernel\n",
193 exp->m_client->m_hostname,
194 exp->m_export.e_path);
195 if (!export_unexport(exp))
201 /* we synchronise intention with reality.
202 * entries with m_mayexport get exported
203 * entries with m_exported but not m_mayexport get unexported
204 * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
207 exports_update(int verbose)
211 for (exp = exportlist[MCL_FQDN].p_head; exp; exp=exp->m_next) {
212 exports_update_one(exp, verbose);
214 for (exp = exportlist[MCL_GSS].p_head; exp; exp=exp->m_next) {
215 exports_update_one(exp, verbose);
220 * export_all finds all entries and
221 * marks them xtabent and mayexport so that they get exported
224 export_all(int verbose)
229 for (i = 0; i < MCL_MAXTYPES; i++) {
230 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
232 printf("exporting %s:%s\n",
233 exp->m_client->m_hostname,
234 exp->m_export.e_path);
236 exp->m_mayexport = 1;
239 validate_export(exp);
246 exportfs(char *arg, char *options, int verbose)
248 struct exportent *eep;
250 struct addrinfo *ai = NULL;
255 if ((path = strchr(arg, ':')) != NULL)
258 if (!path || *path != '/') {
259 xlog(L_ERROR, "Invalid exporting option: %s", arg);
263 if ((htype = client_gettype(hname)) == MCL_FQDN) {
264 ai = host_addrinfo(hname);
266 exp = export_find(ai, path);
267 hname = ai->ai_canonname;
270 exp = export_lookup(hname, path, 0);
273 if (!(eep = mkexportent(hname, path, options)) ||
274 !(exp = export_create(eep, 0)))
276 } else if (!updateexportent(&exp->m_export, options))
280 printf("exporting %s:%s\n", exp->m_client->m_hostname,
281 exp->m_export.e_path);
283 exp->m_mayexport = 1;
286 validate_export(exp);
293 unexportfs(char *arg, int verbose)
296 struct addrinfo *ai = NULL;
301 if ((path = strchr(arg, ':')) != NULL)
304 if (!path || *path != '/') {
305 xlog(L_ERROR, "Invalid unexporting option: %s", arg);
309 if ((htype = client_gettype(hname)) == MCL_FQDN) {
310 ai = host_addrinfo(hname);
312 hname = ai->ai_canonname;
315 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
316 if (path && strcmp(path, exp->m_export.e_path))
318 if (htype != exp->m_client->m_type)
320 if (htype == MCL_FQDN
321 && !matchhostname(exp->m_export.e_hostname,
324 if (htype != MCL_FQDN
325 && strcasecmp(exp->m_export.e_hostname, hname))
329 if (exp->m_exported) {
330 printf("unexporting %s:%s from kernel\n",
331 exp->m_client->m_hostname,
332 exp->m_export.e_path);
336 printf("unexporting %s:%s\n",
337 exp->m_client->m_hostname,
338 exp->m_export.e_path);
341 if (exp->m_exported && !export_unexport(exp))
345 exp->m_mayexport = 0;
351 static int can_test(void)
355 char *setup = "nfsd 0.0.0.0 2147483647 -test-client-\n";
356 fd = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
357 if ( fd < 0) return 0;
358 n = write(fd, setup, strlen(setup));
362 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
363 if ( fd < 0) return 0;
368 static int test_export(char *path, int with_fsid)
373 sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
375 with_fsid ? NFSEXP_FSID : 0);
376 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
379 n = write(fd, buf, strlen(buf));
387 validate_export(nfs_export *exp)
389 /* Check that the given export point is potentially exportable.
390 * We just give warnings here, don't cause anything to fail.
391 * If a path doesn't exist, or is not a dir or file, give an warning
392 * otherwise trial-export to '-test-client-' and check for failure.
395 char *path = exp->m_export.e_path;
399 if (stat(path, &stb) < 0) {
400 xlog(L_ERROR, "Failed to stat %s: %m \n", path);
403 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
404 xlog(L_ERROR, "%s is neither a directory nor a file. "
405 "Remote access will fail", path);
411 if (!statfs64(path, &stf) &&
412 (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
415 if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
417 if ( !test_export(path, 1)) {
418 xlog(L_ERROR, "%s does not support NFS export", path);
421 } else if ( ! test_export(path, 0)) {
422 if (test_export(path, 1))
423 xlog(L_ERROR, "%s requires fsid= for NFS export", path);
425 xlog(L_ERROR, "%s does not support NFS export", path);
432 is_hostname(const char *sp)
434 if (*sp == '\0' || *sp == '@')
437 for (; *sp != '\0'; sp++) {
438 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
440 if (*sp == '\\' && sp[1] != '\0')
448 matchhostname(const char *hostname1, const char *hostname2)
450 struct addrinfo *results1 = NULL, *results2 = NULL;
451 struct addrinfo *ai1, *ai2;
454 if (strcasecmp(hostname1, hostname2) == 0)
458 * Don't pass export wildcards or netgroup names to DNS
460 if (!is_hostname(hostname1) || !is_hostname(hostname2))
463 results1 = host_addrinfo(hostname1);
464 if (results1 == NULL)
466 results2 = host_addrinfo(hostname2);
467 if (results2 == NULL)
470 if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
475 for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
476 for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
477 if (nfs_compare_sockaddr(ai1->ai_addr, ai2->ai_addr)) {
483 freeaddrinfo(results1);
484 freeaddrinfo(results2);
489 dumpopt(char c, char *fmt, ...)
504 struct exportent *ep;
508 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
509 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
512 continue; /* neilb */
513 if (htype == MCL_ANONYMOUS)
516 hname = ep->e_hostname;
517 if (strlen(ep->e_path) > 14)
518 printf("%-14s\n\t\t%s", ep->e_path, hname);
520 printf("%-14s\t%s", ep->e_path, hname);
526 if (ep->e_flags & NFSEXP_READONLY)
527 c = dumpopt(c, "ro");
529 c = dumpopt(c, "rw");
530 if (ep->e_flags & NFSEXP_ASYNC)
531 c = dumpopt(c, "async");
532 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
533 c = dumpopt(c, "wdelay");
534 if (ep->e_flags & NFSEXP_NOHIDE)
535 c = dumpopt(c, "nohide");
536 if (ep->e_flags & NFSEXP_CROSSMOUNT)
537 c = dumpopt(c, "crossmnt");
538 if (ep->e_flags & NFSEXP_INSECURE_PORT)
539 c = dumpopt(c, "insecure");
540 if (ep->e_flags & NFSEXP_ROOTSQUASH)
541 c = dumpopt(c, "root_squash");
543 c = dumpopt(c, "no_root_squash");
544 if (ep->e_flags & NFSEXP_ALLSQUASH)
545 c = dumpopt(c, "all_squash");
546 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
547 c = dumpopt(c, "no_subtree_check");
548 if (ep->e_flags & NFSEXP_NOAUTHNLM)
549 c = dumpopt(c, "insecure_locks");
550 if (ep->e_flags & NFSEXP_NOACL)
551 c = dumpopt(c, "no_acl");
552 if (ep->e_flags & NFSEXP_FSID)
553 c = dumpopt(c, "fsid=%d", ep->e_fsid);
555 c = dumpopt(c, "fsid=%s", ep->e_uuid);
556 if (ep->e_mountpoint)
557 c = dumpopt(c, "mountpoint%s%s",
558 ep->e_mountpoint[0]?"=":"",
560 if (ep->e_anonuid != 65534)
561 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
562 if (ep->e_anongid != 65534)
563 c = dumpopt(c, "anongid=%d", ep->e_anongid);
564 switch(ep->e_fslocmethod) {
568 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
571 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
575 c = dumpopt(c, "fsloc=stub");
579 secinfo_show(stdout, ep);
580 printf("%c\n", (c != '(')? ')' : ' ');
586 error(nfs_export *exp, int err)
588 xlog(L_ERROR, "%s:%s: %s\n", exp->m_client->m_hostname,
589 exp->m_export.e_path, strerror(err));
593 usage(const char *progname)
595 fprintf(stderr, "usage: %s [-aruv] [host:/path]\n", progname);