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>
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);
45 main(int argc, char **argv)
48 char *progname = NULL;
58 if ((progname = strrchr(argv[0], '/')) != NULL)
69 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
99 if (optind != argc && f_all) {
100 xlog(L_ERROR, "extra arguments are not permitted with -a or -r");
103 if (f_ignore && (f_all || ! f_export)) {
104 xlog(L_ERROR, "-i not meaningful with -a, -r or -u");
107 if (f_reexport && ! f_export) {
108 xlog(L_ERROR, "-r and -u are incompatible");
111 new_cache = check_new_cache();
112 if (optind == argc && ! f_all) {
117 xlog(L_ERROR, "-f is available only "
118 "with new cache controls. "
119 "Mount /proc/fs/nfsd first");
129 if (f_export && ! f_ignore)
130 export_read(_PATH_EXPORTS);
133 export_all(f_verbose);
135 for (i = optind; i < argc ; i++)
136 exportfs(argv[i], options, f_verbose);
138 /* If we are unexporting everything, then
139 * don't care about what should be exported, as that
140 * may require DNS lookups..
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
149 for (i = optind ; i < argc ; i++)
150 unexportfs(argv[i], f_verbose);
156 exports_update(f_verbose);
160 cache_flush(force_flush);
168 exports_update_one(nfs_export *exp, int verbose)
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;
180 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
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))
189 if (exp->m_exported && ! exp->m_mayexport) {
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))
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
206 exports_update(int verbose)
210 for (exp = exportlist[MCL_FQDN].p_head; exp; exp=exp->m_next) {
211 exports_update_one(exp, verbose);
213 for (exp = exportlist[MCL_GSS].p_head; exp; exp=exp->m_next) {
214 exports_update_one(exp, verbose);
219 * export_all finds all entries and
220 * marks them xtabent and mayexport so that they get exported
223 export_all(int verbose)
228 for (i = 0; i < MCL_MAXTYPES; i++) {
229 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
231 printf("exporting %s:%s\n",
232 exp->m_client->m_hostname,
233 exp->m_export.e_path);
235 exp->m_mayexport = 1;
238 validate_export(exp);
245 exportfs(char *arg, char *options, int verbose)
247 struct exportent *eep;
249 struct addrinfo *ai = NULL;
254 if ((path = strchr(arg, ':')) != NULL)
257 if (!path || *path != '/') {
258 xlog(L_ERROR, "Invalid exporting option: %s", arg);
262 if ((htype = client_gettype(hname)) == MCL_FQDN) {
263 ai = host_addrinfo(hname);
265 exp = export_find(ai, path);
266 hname = ai->ai_canonname;
269 exp = export_lookup(hname, path, 0);
272 if (!(eep = mkexportent(hname, path, options)) ||
273 !(exp = export_create(eep, 0)))
275 } else if (!updateexportent(&exp->m_export, options))
279 printf("exporting %s:%s\n", exp->m_client->m_hostname,
280 exp->m_export.e_path);
282 exp->m_mayexport = 1;
285 validate_export(exp);
292 unexportfs(char *arg, int verbose)
295 struct addrinfo *ai = NULL;
300 if ((path = strchr(arg, ':')) != NULL)
303 if (!path || *path != '/') {
304 xlog(L_ERROR, "Invalid unexporting option: %s", arg);
308 if ((htype = client_gettype(hname)) == MCL_FQDN) {
309 ai = host_addrinfo(hname);
311 hname = ai->ai_canonname;
314 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
315 if (path && strcmp(path, exp->m_export.e_path))
317 if (htype != exp->m_client->m_type)
319 if (htype == MCL_FQDN
320 && !matchhostname(exp->m_export.e_hostname,
323 if (htype != MCL_FQDN
324 && strcasecmp(exp->m_export.e_hostname, hname))
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);
335 printf("unexporting %s:%s\n",
336 exp->m_client->m_hostname,
337 exp->m_export.e_path);
340 if (exp->m_exported && !export_unexport(exp))
344 exp->m_mayexport = 0;
350 static int can_test(void)
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));
361 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
362 if ( fd < 0) return 0;
367 static int test_export(char *path, int with_fsid)
372 sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
374 with_fsid ? NFSEXP_FSID : 0);
375 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
378 n = write(fd, buf, strlen(buf));
386 validate_export(nfs_export *exp)
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.
394 char *path = exp->m_export.e_path;
398 if (stat(path, &stb) < 0) {
399 xlog(L_ERROR, "Failed to stat %s: %m \n", path);
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);
410 if (!statfs64(path, &stf) &&
411 (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
414 if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
416 if ( !test_export(path, 1)) {
417 xlog(L_ERROR, "%s does not support NFS export", path);
420 } else if ( ! test_export(path, 0)) {
421 if (test_export(path, 1))
422 xlog(L_ERROR, "%s requires fsid= for NFS export", path);
424 xlog(L_ERROR, "%s does not support NFS export", path);
431 is_hostname(const char *sp)
433 if (*sp == '\0' || *sp == '@')
436 for (; *sp != '\0'; sp++) {
437 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
439 if (*sp == '\\' && sp[1] != '\0')
447 compare_sockaddrs4(const struct sockaddr *sa1, const struct sockaddr *sa2)
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;
455 compare_sockaddrs(const struct sockaddr *sa1, const struct sockaddr *sa2)
457 if (sa1->sa_family == sa2->sa_family)
458 switch (sa1->sa_family) {
460 return compare_sockaddrs4(sa1, sa2);
467 matchhostname(const char *hostname1, const char *hostname2)
469 struct addrinfo *results1 = NULL, *results2 = NULL;
470 struct addrinfo *ai1, *ai2;
473 if (strcasecmp(hostname1, hostname2) == 0)
477 * Don't pass export wildcards or netgroup names to DNS
479 if (!is_hostname(hostname1) || !is_hostname(hostname2))
482 results1 = host_addrinfo(hostname1);
483 if (results1 == NULL)
485 results2 = host_addrinfo(hostname2);
486 if (results2 == NULL)
489 if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
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)) {
502 freeaddrinfo(results1);
503 freeaddrinfo(results2);
508 dumpopt(char c, char *fmt, ...)
523 struct exportent *ep;
527 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
528 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
531 continue; /* neilb */
532 if (htype == MCL_ANONYMOUS)
535 hname = ep->e_hostname;
536 if (strlen(ep->e_path) > 14)
537 printf("%-14s\n\t\t%s", ep->e_path, hname);
539 printf("%-14s\t%s", ep->e_path, hname);
545 if (ep->e_flags & NFSEXP_READONLY)
546 c = dumpopt(c, "ro");
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");
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);
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]?"=":"",
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) {
587 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
590 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
594 c = dumpopt(c, "fsloc=stub");
598 secinfo_show(stdout, ep);
599 printf("%c\n", (c != '(')? ')' : ' ');
605 error(nfs_export *exp, int err)
607 xlog(L_ERROR, "%s:%s: %s\n", exp->m_client->m_hostname,
608 exp->m_export.e_path, strerror(err));
612 usage(const char *progname)
614 fprintf(stderr, "usage: %s [-aruv] [host:/path]\n", progname);