2 * utils/mountd/mountd.c
4 * Authenticate mount requests and retrieve file handle.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
23 #include <sys/resource.h>
29 #include "pseudoflavors.h"
31 extern void my_svc_run(void);
33 static void usage(const char *, int exitcode);
34 static exports get_exportlist(void);
35 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, nfs_export **, mountstat3 *, int v3);
37 int reverse_resolve = 0;
42 /* PRC: a high-availability callout program can be specified with -H
43 * When this is done, the program will receive callouts whenever clients
44 * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
45 char *ha_callout_prog = NULL;
47 /* Number of mountd threads to start. Default is 1 and
48 * that's probably enough unless you need hundreds of
49 * clients to be able to mount at once. */
50 static int num_threads = 1;
51 /* Arbitrary limit on number of threads */
52 #define MAX_THREADS 64
54 static struct option longopts[] =
56 { "foreground", 0, 0, 'F' },
57 { "descriptors", 1, 0, 'o' },
58 { "debug", 1, 0, 'd' },
59 { "help", 0, 0, 'h' },
60 { "exports-file", 1, 0, 'f' },
61 { "nfs-version", 1, 0, 'V' },
62 { "no-nfs-version", 1, 0, 'N' },
63 { "version", 0, 0, 'v' },
64 { "port", 1, 0, 'p' },
65 { "no-tcp", 0, 0, 'n' },
66 { "ha-callout", 1, 0, 'H' },
67 { "state-directory-path", 1, 0, 's' },
68 { "num-threads", 1, 0, 't' },
69 { "reverse-lookup", 0, 0, 'r' },
70 { "manage-gids", 0, 0, 'g' },
74 #define NFSVERSBIT(vers) (0x1 << (vers - 1))
75 #define NFSVERSBIT_ALL (NFSVERSBIT(2) | NFSVERSBIT(3) | NFSVERSBIT(4))
77 static int nfs_version = NFSVERSBIT_ALL;
79 static int version2(void)
81 return nfs_version & NFSVERSBIT(2);
84 static int version3(void)
86 return nfs_version & NFSVERSBIT(3);
89 static int version23(void)
91 return nfs_version & (NFSVERSBIT(2) | NFSVERSBIT(3));
94 static int version_any(void)
96 return nfs_version & NFSVERSBIT_ALL;
100 unregister_services (void)
102 nfs_svc_unregister(MOUNTPROG, MOUNTVERS);
103 nfs_svc_unregister(MOUNTPROG, MOUNTVERS_POSIX);
104 nfs_svc_unregister(MOUNTPROG, MOUNTVERS_NFSV3);
108 cleanup_lockfiles (void)
110 unlink(_PATH_XTABLCK);
111 unlink(_PATH_ETABLCK);
112 unlink(_PATH_RMTABLCK);
115 /* Wait for all worker child processes to exit and reap them */
117 wait_for_workers (void)
124 pid = waitpid(0, &status, 0);
128 return; /* no more children */
129 xlog(L_FATAL, "mountd: can't wait: %s\n",
133 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
134 * does not happen on 2.6 kernels, and waitpid() blocks
135 * until all the children are dead then returns with
136 * -ECHILD. But, we don't need to do anything on the
137 * death of individual workers, so we don't care. */
138 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
143 /* Fork num_threads worker children and wait for them */
150 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
152 for (i = 0 ; i < num_threads ; i++) {
155 xlog(L_FATAL, "mountd: cannot fork: %s\n",
161 /* Re-enable the default action on SIGTERM et al
162 * so that workers die naturally when sent them.
163 * Only the parent unregisters with pmap and
164 * hence needs to do special SIGTERM handling. */
166 sa.sa_handler = SIG_DFL;
168 sigemptyset(&sa.sa_mask);
169 sigaction(SIGHUP, &sa, NULL);
170 sigaction(SIGINT, &sa, NULL);
171 sigaction(SIGTERM, &sa, NULL);
173 /* fall into my_svc_run in caller */
180 unregister_services();
182 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
192 unregister_services();
193 if (num_threads > 1) {
194 /* play Kronos and eat our children */
199 xlog (L_NOTICE, "Caught signal %d, un-registering and exiting.", sig);
206 /* don't exit on SIGHUP */
207 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
212 mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
215 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
216 char buf[INET6_ADDRSTRLEN];
218 xlog(D_CALL, "Received NULL request from %s",
219 host_ntop(sap, buf, sizeof(buf)));
225 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
227 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
228 char buf[INET6_ADDRSTRLEN];
229 struct nfs_fh_len *fh;
231 xlog(D_CALL, "Received MNT1(%s) request from %s", *path,
232 host_ntop(sap, buf, sizeof(buf)));
234 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
236 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
241 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
243 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
244 char buf[INET6_ADDRSTRLEN];
246 xlog(D_CALL, "Received DUMP request from %s",
247 host_ntop(sap, buf, sizeof(buf)));
249 *res = mountlist_list();
255 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
257 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
260 char rpath[MAXPATHLEN+1];
261 char buf[INET6_ADDRSTRLEN];
266 if (realpath(p, rpath) != NULL) {
267 rpath[sizeof (rpath) - 1] = '\0';
271 xlog(D_CALL, "Received UMNT(%s) request from %s", p,
272 host_ntop(sap, buf, sizeof(buf)));
274 exp = auth_authenticate("unmount", sap, p);
278 mountlist_del(host_ntop(sap, buf, sizeof(buf)), p);
283 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
286 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
287 char buf[INET6_ADDRSTRLEN];
289 xlog(D_CALL, "Received UMNTALL request from %s",
290 host_ntop(sap, buf, sizeof(buf)));
292 /* Reload /etc/xtab if necessary */
295 mountlist_del_all(nfs_getrpccaller(rqstp->rq_xprt));
300 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
302 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
303 char buf[INET6_ADDRSTRLEN];
305 xlog(D_CALL, "Received EXPORT request from %s.",
306 host_ntop(sap, buf, sizeof(buf)));
308 *resp = get_exportlist();
314 mount_exportall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
316 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
317 char buf[INET6_ADDRSTRLEN];
319 xlog(D_CALL, "Received EXPORTALL request from %s.",
320 host_ntop(sap, buf, sizeof(buf)));
322 *resp = get_exportlist();
328 * MNTv2 pathconf procedure
330 * The protocol doesn't include a status field, so Sun apparently considers
331 * it good practice to let anyone snoop on your system, even if it's
332 * pretty harmless data such as pathconf. We don't.
334 * Besides, many of the pathconf values don't make much sense on NFS volumes.
335 * FIFOs and tty device files represent devices on the *client*, so there's
336 * no point in getting the server's buffer sizes etc.
339 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
341 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
344 char rpath[MAXPATHLEN+1];
346 char buf[INET6_ADDRSTRLEN];
348 memset(res, 0, sizeof(*res));
353 /* Reload /etc/xtab if necessary */
356 /* Resolve symlinks */
357 if (realpath(p, rpath) != NULL) {
358 rpath[sizeof (rpath) - 1] = '\0';
362 xlog(D_CALL, "Received PATHCONF(%s) request from %s", p,
363 host_ntop(sap, buf, sizeof(buf)));
365 /* Now authenticate the intruder... */
366 exp = auth_authenticate("pathconf", sap, p);
369 else if (stat(p, &stb) < 0) {
370 xlog(L_WARNING, "can't stat exported dir %s: %s",
375 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
376 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
377 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
378 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
379 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
380 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
381 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
383 /* Can't figure out what to do with pc_mask */
391 * We should advertise the preferred flavours first. (See RFC 2623
392 * section 2.7.) We leave that to the administrator, by advertising
393 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
394 * dropped from the list to avoid backward compatibility issue with
395 * older Linux clients, who inspect the list in reversed order.
397 * XXX: It might be more helpful to rearrange these so that flavors
398 * giving more access (as determined from readonly and id-squashing
399 * options) come first. (If we decide to do that we should probably do
400 * that when reading the exports rather than here.)
402 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
405 static int flavors[SECFLAVOR_COUNT];
408 for (s = exp->m_export.e_secinfo; s->flav; s++) {
409 if (s->flav->fnum == AUTH_NULL)
411 flavors[i] = s->flav->fnum;
415 /* default when there is no sec= option: */
417 flavors[0] = AUTH_UNIX;
419 ok->auth_flavors.auth_flavors_val = flavors;
420 ok->auth_flavors.auth_flavors_len = i;
424 * NFSv3 MOUNT procedure
427 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
429 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
430 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
431 char buf[INET6_ADDRSTRLEN];
433 struct nfs_fh_len *fh;
435 xlog(D_CALL, "Received MNT3(%s) request from %s", *path,
436 host_ntop(sap, buf, sizeof(buf)));
438 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
442 ok->fhandle.fhandle3_len = fh->fh_size;
443 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
444 set_authflavors(ok, exp);
448 static struct nfs_fh_len *
449 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
450 mountstat3 *error, int v3)
452 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
453 struct stat stb, estb;
455 struct nfs_fh_len *fh;
456 char rpath[MAXPATHLEN+1];
458 char buf[INET6_ADDRSTRLEN];
463 /* Reload /var/lib/nfs/etab if necessary */
466 /* Resolve symlinks */
467 if (realpath(p, rpath) != NULL) {
468 rpath[sizeof (rpath) - 1] = '\0';
472 /* Now authenticate the intruder... */
473 exp = auth_authenticate("mount", sap, p);
475 *error = MNT3ERR_ACCES;
478 if (stat(p, &stb) < 0) {
479 xlog(L_WARNING, "can't stat exported dir %s: %s",
482 *error = MNT3ERR_NOENT;
484 *error = MNT3ERR_ACCES;
487 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
488 xlog(L_WARNING, "%s is not a directory or regular file", p);
489 *error = MNT3ERR_NOTDIR;
492 if (stat(exp->m_export.e_path, &estb) < 0) {
493 xlog(L_WARNING, "can't stat export point %s: %s",
495 *error = MNT3ERR_NOENT;
498 if (estb.st_dev != stb.st_dev
500 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
501 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
502 p, exp->m_export.e_path);
503 *error = MNT3ERR_ACCES;
506 if (exp->m_export.e_mountpoint &&
507 !is_mountpoint(exp->m_export.e_mountpoint[0]?
508 exp->m_export.e_mountpoint:
509 exp->m_export.e_path)) {
510 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
512 *error = MNT3ERR_NOENT;
517 /* This will be a static private nfs_export with just one
518 * address. We feed it to kernel then extract the filehandle,
522 if (cache_export(exp, p)) {
523 *error = MNT3ERR_ACCES;
526 fh = cache_get_filehandle(exp, v3?64:32, p);
528 *error = MNT3ERR_ACCES;
534 if (exp->m_exported<1) {
542 fh = getfh_size((struct sockaddr_in *)sap, p, 64);
543 if (!v3 || (fh == NULL && errno == EINVAL)) {
544 /* We first try the new nfs syscall. */
545 fh = getfh((struct sockaddr_in *)sap, p);
546 if (fh == NULL && errno == EINVAL)
547 /* Let's try the old one. */
548 fh = getfh_old((struct sockaddr_in *)sap,
549 stb.st_dev, stb.st_ino);
551 if (fh == NULL && !did_export) {
557 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
558 *error = MNT3ERR_ACCES;
563 mountlist_add(host_ntop(sap, buf, sizeof(buf)), p);
569 static void remove_all_clients(exportnode *e)
571 struct groupnode *g, *ng;
573 for (g = e->ex_groups; g; g = ng) {
581 static void free_exportlist(exports *elist)
583 struct exportnode *e, *ne;
585 for (e = *elist; e != NULL; e = ne) {
587 remove_all_clients(e);
594 static void prune_clients(nfs_export *exp, struct exportnode *e)
596 struct addrinfo *ai = NULL;
597 struct groupnode *c, **cp;
600 while ((c = *cp) != NULL) {
601 if (client_gettype(c->gr_name) == MCL_FQDN
602 && (ai = host_addrinfo(c->gr_name))) {
603 if (client_check(exp->m_client, ai)) {
616 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
620 for (e = *elist; e != NULL; e = e->ex_next) {
621 if (!strcmp(exp->m_export.e_path, e->ex_dir))
624 e = xmalloc(sizeof(*e));
627 e->ex_dir = xstrdup(exp->m_export.e_path);
632 static void insert_group(struct exportnode *e, char *newname)
636 for (g = e->ex_groups; g; g = g->gr_next)
637 if (!strcmp(g->gr_name, newname))
640 g = xmalloc(sizeof(*g));
641 g->gr_name = xstrdup(newname);
642 g->gr_next = e->ex_groups;
649 static exports elist = NULL;
650 struct exportnode *e;
653 static unsigned int ecounter;
654 unsigned int acounter;
656 acounter = auth_reload();
657 if (elist && acounter == ecounter)
662 free_exportlist(&elist);
664 for (i = 0; i < MCL_MAXTYPES; i++) {
665 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
666 /* Don't show pseudo exports */
667 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
669 e = lookup_or_create_elist_entry(&elist, exp);
671 /* exports to "*" absorb any others */
672 if (i == MCL_ANONYMOUS && e->ex_groups) {
673 remove_all_clients(e);
676 /* non-FQDN's absorb FQDN's they contain: */
677 if (i != MCL_FQDN && e->ex_groups)
678 prune_clients(exp, e);
680 if (exp->m_export.e_hostname[0] != '\0')
681 insert_group(e, exp->m_export.e_hostname);
689 main(int argc, char **argv)
691 char *export_file = _PATH_EXPORTS;
692 char *state_dir = NFS_STATEDIR;
694 unsigned int listeners = 0;
703 /* Set the basename */
704 if ((progname = strrchr(argv[0], '/')) != NULL)
709 /* Parse the command line options and arguments. */
711 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
717 descriptors = atoi(optarg);
718 if (descriptors <= 0) {
719 fprintf(stderr, "%s: bad descriptors: %s\n",
728 xlog_sconfig(optarg, 1);
731 export_file = optarg;
733 case 'H': /* PRC: specify a high-availability callout program */
734 ha_callout_prog = optarg;
739 case 'P': /* XXX for nfs-server compatibility */
742 if (port <= 0 || port > 65535) {
743 fprintf(stderr, "%s: bad port number: %s\n",
750 if (vers < 2 || vers > 4) {
751 fprintf(stderr, "%s: bad version number: %s\n",
755 nfs_version &= ~NFSVERSBIT(vers);
758 _rpcfdtype = SOCK_DGRAM;
764 if ((state_dir = xstrdup(optarg)) == NULL) {
765 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
771 num_threads = atoi (optarg);
775 if (vers < 2 || vers > 4) {
776 fprintf(stderr, "%s: bad version number: %s\n",
780 nfs_version |= NFSVERSBIT(vers);
783 printf("%s version " VERSION "\n", progname);
792 /* No more arguments allowed. */
793 if (optind != argc || !version_any())
796 if (chdir(state_dir)) {
797 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
798 progname, state_dir, strerror(errno));
802 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
803 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
804 progname, strerror(errno));
806 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
807 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
808 descriptors > FD_SETSIZE)
809 descriptors = FD_SETSIZE;
811 rlim.rlim_cur = descriptors;
812 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
813 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
814 progname, strerror(errno));
819 /* Initialize logging. */
820 if (!foreground) xlog_stderr(0);
823 sa.sa_handler = SIG_IGN;
825 sigemptyset(&sa.sa_mask);
826 sigaction(SIGHUP, &sa, NULL);
827 sigaction(SIGINT, &sa, NULL);
828 sigaction(SIGTERM, &sa, NULL);
829 sigaction(SIGPIPE, &sa, NULL);
830 /* WARNING: the following works on Linux and SysV, but not BSD! */
831 sigaction(SIGCHLD, &sa, NULL);
833 /* Daemons should close all extra filehandles ... *before* RPC init. */
837 new_cache = check_new_cache();
841 unregister_services();
843 listeners += nfs_svc_create("mountd", MOUNTPROG,
844 MOUNTVERS, mount_dispatch, port);
845 listeners += nfs_svc_create("mountd", MOUNTPROG,
846 MOUNTVERS_POSIX, mount_dispatch, port);
849 listeners += nfs_svc_create("mountd", MOUNTPROG,
850 MOUNTVERS_NFSV3, mount_dispatch, port);
851 if (version23() && listeners == 0)
852 xlog(L_FATAL, "mountd: could not create listeners\n");
854 sa.sa_handler = killer;
855 sigaction(SIGINT, &sa, NULL);
856 sigaction(SIGTERM, &sa, NULL);
857 sa.sa_handler = sig_hup;
858 sigaction(SIGHUP, &sa, NULL);
860 auth_init(export_file);
863 /* We first fork off a child. */
864 if ((c = fork()) > 0)
867 xlog(L_FATAL, "mountd: cannot fork: %s\n",
870 /* Now we remove ourselves from the foreground.
871 Redirect stdin/stdout/stderr first. */
873 int fd = open("/dev/null", O_RDWR);
877 if (fd > 2) (void) close(fd);
882 /* silently bounds check num_threads */
885 else if (num_threads < 1)
887 else if (num_threads > MAX_THREADS)
888 num_threads = MAX_THREADS;
893 xlog(L_NOTICE, "Version " VERSION " starting");
896 xlog(L_ERROR, "RPC service loop terminated unexpectedly. Exiting...\n");
897 unregister_services();
902 usage(const char *prog, int n)
905 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
906 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
907 " [-p|--port port] [-V version|--nfs-version version]\n"
908 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
909 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
910 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);