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>
32 static void export_all(int verbose);
33 static void exportfs(char *arg, char *options, int verbose);
34 static void unexportfs(char *arg, int verbose);
35 static void exports_update(int verbose);
36 static void dump(int verbose);
37 static void error(nfs_export *exp, int err);
38 static void usage(void);
39 static void validate_export(nfs_export *exp);
40 static int matchhostname(const char *hostname1, const char *hostname2);
43 main(int argc, char **argv)
55 xlog_open("exportfs");
59 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
89 if (optind != argc && f_all) {
90 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
93 if (f_ignore && (f_all || ! f_export)) {
94 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
97 if (f_reexport && ! f_export) {
98 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
101 new_cache = check_new_cache();
102 if (optind == argc && ! f_all) {
107 fprintf(stderr, "exportfs: -f: only available with new cache controls: mount /proc/fs/nfsd first\n");
117 if (f_export && ! f_ignore)
118 export_read(_PATH_EXPORTS);
121 export_all(f_verbose);
123 for (i = optind; i < argc ; i++)
124 exportfs(argv[i], options, f_verbose);
126 /* If we are unexporting everything, then
127 * don't care about what should be exported, as that
128 * may require DNS lookups..
130 if (! ( !f_export && f_all)) {
131 /* note: xtab_*_read does not update entries if they already exist,
132 * so this will not lose new options
137 for (i = optind ; i < argc ; i++)
138 unexportfs(argv[i], f_verbose);
144 exports_update(f_verbose);
148 cache_flush(force_flush);
156 exports_update_one(nfs_export *exp, int verbose)
158 /* check mountpoint option */
159 if (exp->m_mayexport &&
160 exp->m_export.e_mountpoint &&
161 !is_mountpoint(exp->m_export.e_mountpoint[0]?
162 exp->m_export.e_mountpoint:
163 exp->m_export.e_path)) {
164 printf("%s not exported as %s not a mountpoint.\n",
165 exp->m_export.e_path, exp->m_export.e_mountpoint);
166 exp->m_mayexport = 0;
168 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
170 printf("%sexporting %s:%s to kernel\n",
171 exp->m_exported ?"re":"",
172 exp->m_client->m_hostname,
173 exp->m_export.e_path);
174 if (!export_export(exp))
177 if (exp->m_exported && ! exp->m_mayexport) {
179 printf("unexporting %s:%s from kernel\n",
180 exp->m_client->m_hostname,
181 exp->m_export.e_path);
182 if (!export_unexport(exp))
188 /* we synchronise intention with reality.
189 * entries with m_mayexport get exported
190 * entries with m_exported but not m_mayexport get unexported
191 * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
194 exports_update(int verbose)
198 for (exp = exportlist[MCL_FQDN].p_head; exp; exp=exp->m_next) {
199 exports_update_one(exp, verbose);
201 for (exp = exportlist[MCL_GSS].p_head; exp; exp=exp->m_next) {
202 exports_update_one(exp, verbose);
207 * export_all finds all entries and
208 * marks them xtabent and mayexport so that they get exported
211 export_all(int verbose)
216 for (i = 0; i < MCL_MAXTYPES; i++) {
217 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
219 printf("exporting %s:%s\n",
220 exp->m_client->m_hostname,
221 exp->m_export.e_path);
223 exp->m_mayexport = 1;
226 validate_export(exp);
233 exportfs(char *arg, char *options, int verbose)
235 struct exportent *eep;
237 struct addrinfo *ai = NULL;
242 if ((path = strchr(arg, ':')) != NULL)
245 if (!path || *path != '/') {
246 fprintf(stderr, "Invalid exporting option: %s\n", arg);
250 if ((htype = client_gettype(hname)) == MCL_FQDN) {
251 ai = host_addrinfo(hname);
253 exp = export_find(ai, path);
254 hname = ai->ai_canonname;
257 exp = export_lookup(hname, path, 0);
260 if (!(eep = mkexportent(hname, path, options)) ||
261 !(exp = export_create(eep, 0)))
263 } else if (!updateexportent(&exp->m_export, options))
267 printf("exporting %s:%s\n", exp->m_client->m_hostname,
268 exp->m_export.e_path);
270 exp->m_mayexport = 1;
273 validate_export(exp);
280 unexportfs(char *arg, int verbose)
283 struct addrinfo *ai = NULL;
288 if ((path = strchr(arg, ':')) != NULL)
291 if (!path || *path != '/') {
292 fprintf(stderr, "Invalid unexporting option: %s\n",
297 if ((htype = client_gettype(hname)) == MCL_FQDN) {
298 ai = host_addrinfo(hname);
300 hname = ai->ai_canonname;
303 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
304 if (path && strcmp(path, exp->m_export.e_path))
306 if (htype != exp->m_client->m_type)
308 if (htype == MCL_FQDN
309 && !matchhostname(exp->m_export.e_hostname,
312 if (htype != MCL_FQDN
313 && strcasecmp(exp->m_export.e_hostname, hname))
317 if (exp->m_exported) {
318 printf("unexporting %s:%s from kernel\n",
319 exp->m_client->m_hostname,
320 exp->m_export.e_path);
324 printf("unexporting %s:%s\n",
325 exp->m_client->m_hostname,
326 exp->m_export.e_path);
329 if (exp->m_exported && !export_unexport(exp))
333 exp->m_mayexport = 0;
339 static int can_test(void)
343 char *setup = "nfsd 0.0.0.0 2147483647 -test-client-\n";
344 fd = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
345 if ( fd < 0) return 0;
346 n = write(fd, setup, strlen(setup));
350 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
351 if ( fd < 0) return 0;
356 static int test_export(char *path, int with_fsid)
361 sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
363 with_fsid ? NFSEXP_FSID : 0);
364 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
367 n = write(fd, buf, strlen(buf));
375 validate_export(nfs_export *exp)
377 /* Check that the given export point is potentially exportable.
378 * We just give warnings here, don't cause anything to fail.
379 * If a path doesn't exist, or is not a dir or file, give an warning
380 * otherwise trial-export to '-test-client-' and check for failure.
383 char *path = exp->m_export.e_path;
387 if (stat(path, &stb) < 0) {
388 fprintf(stderr, "exportfs: Warning: %s does not exist\n",
392 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
393 fprintf(stderr, "exportfs: Warning: %s is neither "
394 "a directory nor a file.\n"
395 " remote access will fail\n", path);
401 if (!statfs64(path, &stf) &&
402 (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
405 if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
407 if ( !test_export(path, 1)) {
408 fprintf(stderr, "exportfs: Warning: %s does not "
409 "support NFS export.\n",
413 } else if ( ! test_export(path, 0)) {
414 if (test_export(path, 1))
415 fprintf(stderr, "exportfs: Warning: %s requires fsid= "
416 "for NFS export\n", path);
418 fprintf(stderr, "exportfs: Warning: %s does not "
419 "support NFS export.\n",
427 is_hostname(const char *sp)
429 if (*sp == '\0' || *sp == '@')
432 for (; *sp != '\0'; sp++) {
433 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
435 if (*sp == '\\' && sp[1] != '\0')
443 compare_sockaddrs4(const struct sockaddr *sa1, const struct sockaddr *sa2)
445 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
446 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
447 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
451 compare_sockaddrs(const struct sockaddr *sa1, const struct sockaddr *sa2)
453 if (sa1->sa_family == sa2->sa_family)
454 switch (sa1->sa_family) {
456 return compare_sockaddrs4(sa1, sa2);
463 matchhostname(const char *hostname1, const char *hostname2)
465 struct addrinfo *results1 = NULL, *results2 = NULL;
466 struct addrinfo *ai1, *ai2;
469 if (strcasecmp(hostname1, hostname2) == 0)
473 * Don't pass export wildcards or netgroup names to DNS
475 if (!is_hostname(hostname1) || !is_hostname(hostname2))
478 results1 = host_addrinfo(hostname1);
479 if (results1 == NULL)
481 results2 = host_addrinfo(hostname2);
482 if (results2 == NULL)
485 if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
490 for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
491 for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
492 if (compare_sockaddrs(ai1->ai_addr, ai2->ai_addr)) {
498 freeaddrinfo(results1);
499 freeaddrinfo(results2);
504 dumpopt(char c, char *fmt, ...)
519 struct exportent *ep;
523 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
524 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
527 continue; /* neilb */
528 if (htype == MCL_ANONYMOUS)
531 hname = ep->e_hostname;
532 if (strlen(ep->e_path) > 14)
533 printf("%-14s\n\t\t%s", ep->e_path, hname);
535 printf("%-14s\t%s", ep->e_path, hname);
541 if (ep->e_flags & NFSEXP_READONLY)
542 c = dumpopt(c, "ro");
544 c = dumpopt(c, "rw");
545 if (ep->e_flags & NFSEXP_ASYNC)
546 c = dumpopt(c, "async");
547 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
548 c = dumpopt(c, "wdelay");
549 if (ep->e_flags & NFSEXP_NOHIDE)
550 c = dumpopt(c, "nohide");
551 if (ep->e_flags & NFSEXP_CROSSMOUNT)
552 c = dumpopt(c, "crossmnt");
553 if (ep->e_flags & NFSEXP_INSECURE_PORT)
554 c = dumpopt(c, "insecure");
555 if (ep->e_flags & NFSEXP_ROOTSQUASH)
556 c = dumpopt(c, "root_squash");
558 c = dumpopt(c, "no_root_squash");
559 if (ep->e_flags & NFSEXP_ALLSQUASH)
560 c = dumpopt(c, "all_squash");
561 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
562 c = dumpopt(c, "no_subtree_check");
563 if (ep->e_flags & NFSEXP_NOAUTHNLM)
564 c = dumpopt(c, "insecure_locks");
565 if (ep->e_flags & NFSEXP_NOACL)
566 c = dumpopt(c, "no_acl");
567 if (ep->e_flags & NFSEXP_FSID)
568 c = dumpopt(c, "fsid=%d", ep->e_fsid);
570 c = dumpopt(c, "fsid=%s", ep->e_uuid);
571 if (ep->e_mountpoint)
572 c = dumpopt(c, "mountpoint%s%s",
573 ep->e_mountpoint[0]?"=":"",
575 if (ep->e_anonuid != 65534)
576 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
577 if (ep->e_anongid != 65534)
578 c = dumpopt(c, "anongid=%d", ep->e_anongid);
579 switch(ep->e_fslocmethod) {
583 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
586 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
590 c = dumpopt(c, "fsloc=stub");
594 secinfo_show(stdout, ep);
595 printf("%c\n", (c != '(')? ')' : ' ');
601 error(nfs_export *exp, int err)
603 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
604 exp->m_export.e_path, strerror(err));
610 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");