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(void);
41 static void validate_export(nfs_export *exp);
42 static int matchhostname(const char *hostname1, const char *hostname2);
45 main(int argc, char **argv)
57 xlog_open("exportfs");
61 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
91 if (optind != argc && f_all) {
92 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
95 if (f_ignore && (f_all || ! f_export)) {
96 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
99 if (f_reexport && ! f_export) {
100 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
103 new_cache = check_new_cache();
104 if (optind == argc && ! f_all) {
109 fprintf(stderr, "exportfs: -f: only available with new cache controls: mount /proc/fs/nfsd first\n");
119 if (f_export && ! f_ignore)
120 export_read(_PATH_EXPORTS);
123 export_all(f_verbose);
125 for (i = optind; i < argc ; i++)
126 exportfs(argv[i], options, f_verbose);
128 /* If we are unexporting everything, then
129 * don't care about what should be exported, as that
130 * may require DNS lookups..
132 if (! ( !f_export && f_all)) {
133 /* note: xtab_*_read does not update entries if they already exist,
134 * so this will not lose new options
139 for (i = optind ; i < argc ; i++)
140 unexportfs(argv[i], f_verbose);
146 exports_update(f_verbose);
150 cache_flush(force_flush);
158 exports_update_one(nfs_export *exp, int verbose)
160 /* check mountpoint option */
161 if (exp->m_mayexport &&
162 exp->m_export.e_mountpoint &&
163 !is_mountpoint(exp->m_export.e_mountpoint[0]?
164 exp->m_export.e_mountpoint:
165 exp->m_export.e_path)) {
166 printf("%s not exported as %s not a mountpoint.\n",
167 exp->m_export.e_path, exp->m_export.e_mountpoint);
168 exp->m_mayexport = 0;
170 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
172 printf("%sexporting %s:%s to kernel\n",
173 exp->m_exported ?"re":"",
174 exp->m_client->m_hostname,
175 exp->m_export.e_path);
176 if (!export_export(exp))
179 if (exp->m_exported && ! exp->m_mayexport) {
181 printf("unexporting %s:%s from kernel\n",
182 exp->m_client->m_hostname,
183 exp->m_export.e_path);
184 if (!export_unexport(exp))
190 /* we synchronise intention with reality.
191 * entries with m_mayexport get exported
192 * entries with m_exported but not m_mayexport get unexported
193 * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
196 exports_update(int verbose)
200 for (exp = exportlist[MCL_FQDN].p_head; exp; exp=exp->m_next) {
201 exports_update_one(exp, verbose);
203 for (exp = exportlist[MCL_GSS].p_head; exp; exp=exp->m_next) {
204 exports_update_one(exp, verbose);
209 * export_all finds all entries and
210 * marks them xtabent and mayexport so that they get exported
213 export_all(int verbose)
218 for (i = 0; i < MCL_MAXTYPES; i++) {
219 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
221 printf("exporting %s:%s\n",
222 exp->m_client->m_hostname,
223 exp->m_export.e_path);
225 exp->m_mayexport = 1;
228 validate_export(exp);
235 exportfs(char *arg, char *options, int verbose)
237 struct exportent *eep;
239 struct addrinfo *ai = NULL;
244 if ((path = strchr(arg, ':')) != NULL)
247 if (!path || *path != '/') {
248 fprintf(stderr, "Invalid exporting option: %s\n", arg);
252 if ((htype = client_gettype(hname)) == MCL_FQDN) {
253 ai = host_addrinfo(hname);
255 exp = export_find(ai, path);
256 hname = ai->ai_canonname;
259 exp = export_lookup(hname, path, 0);
262 if (!(eep = mkexportent(hname, path, options)) ||
263 !(exp = export_create(eep, 0)))
265 } else if (!updateexportent(&exp->m_export, options))
269 printf("exporting %s:%s\n", exp->m_client->m_hostname,
270 exp->m_export.e_path);
272 exp->m_mayexport = 1;
275 validate_export(exp);
282 unexportfs(char *arg, int verbose)
285 struct addrinfo *ai = NULL;
290 if ((path = strchr(arg, ':')) != NULL)
293 if (!path || *path != '/') {
294 fprintf(stderr, "Invalid unexporting option: %s\n",
299 if ((htype = client_gettype(hname)) == MCL_FQDN) {
300 ai = host_addrinfo(hname);
302 hname = ai->ai_canonname;
305 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
306 if (path && strcmp(path, exp->m_export.e_path))
308 if (htype != exp->m_client->m_type)
310 if (htype == MCL_FQDN
311 && !matchhostname(exp->m_export.e_hostname,
314 if (htype != MCL_FQDN
315 && strcasecmp(exp->m_export.e_hostname, hname))
319 if (exp->m_exported) {
320 printf("unexporting %s:%s from kernel\n",
321 exp->m_client->m_hostname,
322 exp->m_export.e_path);
326 printf("unexporting %s:%s\n",
327 exp->m_client->m_hostname,
328 exp->m_export.e_path);
331 if (exp->m_exported && !export_unexport(exp))
335 exp->m_mayexport = 0;
341 static int can_test(void)
345 char *setup = "nfsd 0.0.0.0 2147483647 -test-client-\n";
346 fd = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
347 if ( fd < 0) return 0;
348 n = write(fd, setup, strlen(setup));
352 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
353 if ( fd < 0) return 0;
358 static int test_export(char *path, int with_fsid)
363 sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
365 with_fsid ? NFSEXP_FSID : 0);
366 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
369 n = write(fd, buf, strlen(buf));
377 validate_export(nfs_export *exp)
379 /* Check that the given export point is potentially exportable.
380 * We just give warnings here, don't cause anything to fail.
381 * If a path doesn't exist, or is not a dir or file, give an warning
382 * otherwise trial-export to '-test-client-' and check for failure.
385 char *path = exp->m_export.e_path;
389 if (stat(path, &stb) < 0) {
390 fprintf(stderr, "exportfs: Warning: %s does not exist\n",
394 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
395 fprintf(stderr, "exportfs: Warning: %s is neither "
396 "a directory nor a file.\n"
397 " remote access will fail\n", path);
403 if (!statfs64(path, &stf) &&
404 (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
407 if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
409 if ( !test_export(path, 1)) {
410 fprintf(stderr, "exportfs: Warning: %s does not "
411 "support NFS export.\n",
415 } else if ( ! test_export(path, 0)) {
416 if (test_export(path, 1))
417 fprintf(stderr, "exportfs: Warning: %s requires fsid= "
418 "for NFS export\n", path);
420 fprintf(stderr, "exportfs: Warning: %s does not "
421 "support NFS export.\n",
429 is_hostname(const char *sp)
431 if (*sp == '\0' || *sp == '@')
434 for (; *sp != '\0'; sp++) {
435 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
437 if (*sp == '\\' && sp[1] != '\0')
445 compare_sockaddrs4(const struct sockaddr *sa1, const struct sockaddr *sa2)
447 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
448 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
449 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
453 compare_sockaddrs(const struct sockaddr *sa1, const struct sockaddr *sa2)
455 if (sa1->sa_family == sa2->sa_family)
456 switch (sa1->sa_family) {
458 return compare_sockaddrs4(sa1, sa2);
465 matchhostname(const char *hostname1, const char *hostname2)
467 struct addrinfo *results1 = NULL, *results2 = NULL;
468 struct addrinfo *ai1, *ai2;
471 if (strcasecmp(hostname1, hostname2) == 0)
475 * Don't pass export wildcards or netgroup names to DNS
477 if (!is_hostname(hostname1) || !is_hostname(hostname2))
480 results1 = host_addrinfo(hostname1);
481 if (results1 == NULL)
483 results2 = host_addrinfo(hostname2);
484 if (results2 == NULL)
487 if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
492 for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
493 for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
494 if (compare_sockaddrs(ai1->ai_addr, ai2->ai_addr)) {
500 freeaddrinfo(results1);
501 freeaddrinfo(results2);
506 dumpopt(char c, char *fmt, ...)
521 struct exportent *ep;
525 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
526 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
529 continue; /* neilb */
530 if (htype == MCL_ANONYMOUS)
533 hname = ep->e_hostname;
534 if (strlen(ep->e_path) > 14)
535 printf("%-14s\n\t\t%s", ep->e_path, hname);
537 printf("%-14s\t%s", ep->e_path, hname);
543 if (ep->e_flags & NFSEXP_READONLY)
544 c = dumpopt(c, "ro");
546 c = dumpopt(c, "rw");
547 if (ep->e_flags & NFSEXP_ASYNC)
548 c = dumpopt(c, "async");
549 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
550 c = dumpopt(c, "wdelay");
551 if (ep->e_flags & NFSEXP_NOHIDE)
552 c = dumpopt(c, "nohide");
553 if (ep->e_flags & NFSEXP_CROSSMOUNT)
554 c = dumpopt(c, "crossmnt");
555 if (ep->e_flags & NFSEXP_INSECURE_PORT)
556 c = dumpopt(c, "insecure");
557 if (ep->e_flags & NFSEXP_ROOTSQUASH)
558 c = dumpopt(c, "root_squash");
560 c = dumpopt(c, "no_root_squash");
561 if (ep->e_flags & NFSEXP_ALLSQUASH)
562 c = dumpopt(c, "all_squash");
563 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
564 c = dumpopt(c, "no_subtree_check");
565 if (ep->e_flags & NFSEXP_NOAUTHNLM)
566 c = dumpopt(c, "insecure_locks");
567 if (ep->e_flags & NFSEXP_NOACL)
568 c = dumpopt(c, "no_acl");
569 if (ep->e_flags & NFSEXP_FSID)
570 c = dumpopt(c, "fsid=%d", ep->e_fsid);
572 c = dumpopt(c, "fsid=%s", ep->e_uuid);
573 if (ep->e_mountpoint)
574 c = dumpopt(c, "mountpoint%s%s",
575 ep->e_mountpoint[0]?"=":"",
577 if (ep->e_anonuid != 65534)
578 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
579 if (ep->e_anongid != 65534)
580 c = dumpopt(c, "anongid=%d", ep->e_anongid);
581 switch(ep->e_fslocmethod) {
585 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
588 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
592 c = dumpopt(c, "fsloc=stub");
596 secinfo_show(stdout, ep);
597 printf("%c\n", (c != '(')? ')' : ' ');
603 error(nfs_export *exp, int err)
605 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
606 exp->m_export.e_path, strerror(err));
612 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");