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_FATAL, "Caught signal %d, un-registering and exiting.", sig);
205 /* don't exit on SIGHUP */
206 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
211 mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
214 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
215 char buf[INET6_ADDRSTRLEN];
217 xlog(D_CALL, "Received NULL request from %s",
218 host_ntop(sap, buf, sizeof(buf)));
224 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
226 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
227 char buf[INET6_ADDRSTRLEN];
228 struct nfs_fh_len *fh;
230 xlog(D_CALL, "Received MNT1(%s) request from %s", *path,
231 host_ntop(sap, buf, sizeof(buf)));
233 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
235 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
240 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
242 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
243 char buf[INET6_ADDRSTRLEN];
245 xlog(D_CALL, "Received DUMP request from %s",
246 host_ntop(sap, buf, sizeof(buf)));
248 *res = mountlist_list();
254 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
256 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
259 char rpath[MAXPATHLEN+1];
260 char buf[INET6_ADDRSTRLEN];
265 if (realpath(p, rpath) != NULL) {
266 rpath[sizeof (rpath) - 1] = '\0';
270 xlog(D_CALL, "Received UMNT(%s) request from %s", p,
271 host_ntop(sap, buf, sizeof(buf)));
273 exp = auth_authenticate("unmount", sap, p);
277 mountlist_del(host_ntop(sap, buf, sizeof(buf)), p);
282 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
285 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
286 char buf[INET6_ADDRSTRLEN];
288 xlog(D_CALL, "Received UMNTALL request from %s",
289 host_ntop(sap, buf, sizeof(buf)));
291 /* Reload /etc/xtab if necessary */
294 mountlist_del_all(nfs_getrpccaller(rqstp->rq_xprt));
299 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
301 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
302 char buf[INET6_ADDRSTRLEN];
304 xlog(D_CALL, "Received EXPORT request from %s.",
305 host_ntop(sap, buf, sizeof(buf)));
307 *resp = get_exportlist();
313 mount_exportall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
315 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
316 char buf[INET6_ADDRSTRLEN];
318 xlog(D_CALL, "Received EXPORTALL request from %s.",
319 host_ntop(sap, buf, sizeof(buf)));
321 *resp = get_exportlist();
327 * MNTv2 pathconf procedure
329 * The protocol doesn't include a status field, so Sun apparently considers
330 * it good practice to let anyone snoop on your system, even if it's
331 * pretty harmless data such as pathconf. We don't.
333 * Besides, many of the pathconf values don't make much sense on NFS volumes.
334 * FIFOs and tty device files represent devices on the *client*, so there's
335 * no point in getting the server's buffer sizes etc.
338 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
340 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
343 char rpath[MAXPATHLEN+1];
345 char buf[INET6_ADDRSTRLEN];
347 memset(res, 0, sizeof(*res));
352 /* Reload /etc/xtab if necessary */
355 /* Resolve symlinks */
356 if (realpath(p, rpath) != NULL) {
357 rpath[sizeof (rpath) - 1] = '\0';
361 xlog(D_CALL, "Received PATHCONF(%s) request from %s", p,
362 host_ntop(sap, buf, sizeof(buf)));
364 /* Now authenticate the intruder... */
365 exp = auth_authenticate("pathconf", sap, p);
368 else if (stat(p, &stb) < 0) {
369 xlog(L_WARNING, "can't stat exported dir %s: %s",
374 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
375 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
376 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
377 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
378 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
379 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
380 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
382 /* Can't figure out what to do with pc_mask */
390 * We should advertise the preferred flavours first. (See RFC 2623
391 * section 2.7.) We leave that to the administrator, by advertising
392 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
393 * dropped from the list to avoid backward compatibility issue with
394 * older Linux clients, who inspect the list in reversed order.
396 * XXX: It might be more helpful to rearrange these so that flavors
397 * giving more access (as determined from readonly and id-squashing
398 * options) come first. (If we decide to do that we should probably do
399 * that when reading the exports rather than here.)
401 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
404 static int flavors[SECFLAVOR_COUNT];
407 for (s = exp->m_export.e_secinfo; s->flav; s++) {
408 if (s->flav->fnum == AUTH_NULL)
410 flavors[i] = s->flav->fnum;
414 /* default when there is no sec= option: */
416 flavors[0] = AUTH_UNIX;
418 ok->auth_flavors.auth_flavors_val = flavors;
419 ok->auth_flavors.auth_flavors_len = i;
423 * NFSv3 MOUNT procedure
426 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
428 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
429 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
430 char buf[INET6_ADDRSTRLEN];
432 struct nfs_fh_len *fh;
434 xlog(D_CALL, "Received MNT3(%s) request from %s", *path,
435 host_ntop(sap, buf, sizeof(buf)));
437 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
441 ok->fhandle.fhandle3_len = fh->fh_size;
442 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
443 set_authflavors(ok, exp);
447 static struct nfs_fh_len *
448 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
449 mountstat3 *error, int v3)
451 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
452 struct stat stb, estb;
454 struct nfs_fh_len *fh;
455 char rpath[MAXPATHLEN+1];
457 char buf[INET6_ADDRSTRLEN];
462 /* Reload /var/lib/nfs/etab if necessary */
465 /* Resolve symlinks */
466 if (realpath(p, rpath) != NULL) {
467 rpath[sizeof (rpath) - 1] = '\0';
471 /* Now authenticate the intruder... */
472 exp = auth_authenticate("mount", sap, p);
474 *error = MNT3ERR_ACCES;
477 if (stat(p, &stb) < 0) {
478 xlog(L_WARNING, "can't stat exported dir %s: %s",
481 *error = MNT3ERR_NOENT;
483 *error = MNT3ERR_ACCES;
486 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
487 xlog(L_WARNING, "%s is not a directory or regular file", p);
488 *error = MNT3ERR_NOTDIR;
491 if (stat(exp->m_export.e_path, &estb) < 0) {
492 xlog(L_WARNING, "can't stat export point %s: %s",
494 *error = MNT3ERR_NOENT;
497 if (estb.st_dev != stb.st_dev
499 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
500 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
501 p, exp->m_export.e_path);
502 *error = MNT3ERR_ACCES;
505 if (exp->m_export.e_mountpoint &&
506 !is_mountpoint(exp->m_export.e_mountpoint[0]?
507 exp->m_export.e_mountpoint:
508 exp->m_export.e_path)) {
509 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
511 *error = MNT3ERR_NOENT;
516 /* This will be a static private nfs_export with just one
517 * address. We feed it to kernel then extract the filehandle,
521 if (cache_export(exp, p)) {
522 *error = MNT3ERR_ACCES;
525 fh = cache_get_filehandle(exp, v3?64:32, p);
527 *error = MNT3ERR_ACCES;
533 if (exp->m_exported<1) {
541 fh = getfh_size((struct sockaddr_in *)sap, p, 64);
542 if (!v3 || (fh == NULL && errno == EINVAL)) {
543 /* We first try the new nfs syscall. */
544 fh = getfh((struct sockaddr_in *)sap, p);
545 if (fh == NULL && errno == EINVAL)
546 /* Let's try the old one. */
547 fh = getfh_old((struct sockaddr_in *)sap,
548 stb.st_dev, stb.st_ino);
550 if (fh == NULL && !did_export) {
556 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
557 *error = MNT3ERR_ACCES;
562 mountlist_add(host_ntop(sap, buf, sizeof(buf)), p);
568 static void remove_all_clients(exportnode *e)
570 struct groupnode *g, *ng;
572 for (g = e->ex_groups; g; g = ng) {
580 static void free_exportlist(exports *elist)
582 struct exportnode *e, *ne;
584 for (e = *elist; e != NULL; e = ne) {
586 remove_all_clients(e);
593 static void prune_clients(nfs_export *exp, struct exportnode *e)
595 struct addrinfo *ai = NULL;
596 struct groupnode *c, **cp;
599 while ((c = *cp) != NULL) {
600 if (client_gettype(c->gr_name) == MCL_FQDN
601 && (ai = host_addrinfo(c->gr_name))) {
602 if (client_check(exp->m_client, ai)) {
615 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
619 for (e = *elist; e != NULL; e = e->ex_next) {
620 if (!strcmp(exp->m_export.e_path, e->ex_dir))
623 e = xmalloc(sizeof(*e));
626 e->ex_dir = xstrdup(exp->m_export.e_path);
631 static void insert_group(struct exportnode *e, char *newname)
635 for (g = e->ex_groups; g; g = g->gr_next)
636 if (!strcmp(g->gr_name, newname))
639 g = xmalloc(sizeof(*g));
640 g->gr_name = xstrdup(newname);
641 g->gr_next = e->ex_groups;
648 static exports elist = NULL;
649 struct exportnode *e;
652 static unsigned int ecounter;
653 unsigned int acounter;
655 acounter = auth_reload();
656 if (elist && acounter == ecounter)
661 free_exportlist(&elist);
663 for (i = 0; i < MCL_MAXTYPES; i++) {
664 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
665 /* Don't show pseudo exports */
666 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
668 e = lookup_or_create_elist_entry(&elist, exp);
670 /* exports to "*" absorb any others */
671 if (i == MCL_ANONYMOUS && e->ex_groups) {
672 remove_all_clients(e);
675 /* non-FQDN's absorb FQDN's they contain: */
676 if (i != MCL_FQDN && e->ex_groups)
677 prune_clients(exp, e);
679 if (exp->m_export.e_hostname[0] != '\0')
680 insert_group(e, exp->m_export.e_hostname);
688 main(int argc, char **argv)
690 char *export_file = _PATH_EXPORTS;
691 char *state_dir = NFS_STATEDIR;
693 unsigned int listeners = 0;
702 /* Set the basename */
703 if ((progname = strrchr(argv[0], '/')) != NULL)
708 /* Parse the command line options and arguments. */
710 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
716 descriptors = atoi(optarg);
717 if (descriptors <= 0) {
718 fprintf(stderr, "%s: bad descriptors: %s\n",
727 xlog_sconfig(optarg, 1);
730 export_file = optarg;
732 case 'H': /* PRC: specify a high-availability callout program */
733 ha_callout_prog = optarg;
738 case 'P': /* XXX for nfs-server compatibility */
741 if (port <= 0 || port > 65535) {
742 fprintf(stderr, "%s: bad port number: %s\n",
749 if (vers < 2 || vers > 4) {
750 fprintf(stderr, "%s: bad version number: %s\n",
754 nfs_version &= ~NFSVERSBIT(vers);
757 _rpcfdtype = SOCK_DGRAM;
763 if ((state_dir = xstrdup(optarg)) == NULL) {
764 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
770 num_threads = atoi (optarg);
774 if (vers < 2 || vers > 4) {
775 fprintf(stderr, "%s: bad version number: %s\n",
779 nfs_version |= NFSVERSBIT(vers);
782 printf("%s version " VERSION "\n", progname);
791 /* No more arguments allowed. */
792 if (optind != argc || !version_any())
795 if (chdir(state_dir)) {
796 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
797 progname, state_dir, strerror(errno));
801 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
802 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
803 progname, strerror(errno));
805 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
806 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
807 descriptors > FD_SETSIZE)
808 descriptors = FD_SETSIZE;
810 rlim.rlim_cur = descriptors;
811 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
812 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
813 progname, strerror(errno));
818 /* Initialize logging. */
819 if (!foreground) xlog_stderr(0);
822 sa.sa_handler = SIG_IGN;
824 sigemptyset(&sa.sa_mask);
825 sigaction(SIGHUP, &sa, NULL);
826 sigaction(SIGINT, &sa, NULL);
827 sigaction(SIGTERM, &sa, NULL);
828 sigaction(SIGPIPE, &sa, NULL);
829 /* WARNING: the following works on Linux and SysV, but not BSD! */
830 sigaction(SIGCHLD, &sa, NULL);
832 /* Daemons should close all extra filehandles ... *before* RPC init. */
836 new_cache = check_new_cache();
840 unregister_services();
842 listeners += nfs_svc_create("mountd", MOUNTPROG,
843 MOUNTVERS, mount_dispatch, port);
844 listeners += nfs_svc_create("mountd", MOUNTPROG,
845 MOUNTVERS_POSIX, mount_dispatch, port);
848 listeners += nfs_svc_create("mountd", MOUNTPROG,
849 MOUNTVERS_NFSV3, mount_dispatch, port);
850 if (version23() && listeners == 0)
851 xlog(L_FATAL, "mountd: could not create listeners\n");
853 sa.sa_handler = killer;
854 sigaction(SIGINT, &sa, NULL);
855 sigaction(SIGTERM, &sa, NULL);
856 sa.sa_handler = sig_hup;
857 sigaction(SIGHUP, &sa, NULL);
859 auth_init(export_file);
862 /* We first fork off a child. */
863 if ((c = fork()) > 0)
866 xlog(L_FATAL, "mountd: cannot fork: %s\n",
869 /* Now we remove ourselves from the foreground.
870 Redirect stdin/stdout/stderr first. */
872 int fd = open("/dev/null", O_RDWR);
876 if (fd > 2) (void) close(fd);
881 /* silently bounds check num_threads */
884 else if (num_threads < 1)
886 else if (num_threads > MAX_THREADS)
887 num_threads = MAX_THREADS;
892 xlog(L_NOTICE, "Version " VERSION " starting");
895 xlog(L_ERROR, "RPC service loop terminated unexpectedly. Exiting...\n");
896 unregister_services();
901 usage(const char *prog, int n)
904 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
905 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
906 " [-p|--port port] [-V version|--nfs-version version]\n"
907 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
908 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
909 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);