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 version_any(void)
91 return nfs_version & NFSVERSBIT_ALL;
95 unregister_services (void)
98 pmap_unset (MOUNTPROG, MOUNTVERS);
99 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
102 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
106 cleanup_lockfiles (void)
108 unlink(_PATH_XTABLCK);
109 unlink(_PATH_ETABLCK);
110 unlink(_PATH_RMTABLCK);
113 /* Wait for all worker child processes to exit and reap them */
115 wait_for_workers (void)
122 pid = waitpid(0, &status, 0);
126 return; /* no more children */
127 xlog(L_FATAL, "mountd: can't wait: %s\n",
131 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
132 * does not happen on 2.6 kernels, and waitpid() blocks
133 * until all the children are dead then returns with
134 * -ECHILD. But, we don't need to do anything on the
135 * death of individual workers, so we don't care. */
136 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
141 /* Fork num_threads worker children and wait for them */
148 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
150 for (i = 0 ; i < num_threads ; i++) {
153 xlog(L_FATAL, "mountd: cannot fork: %s\n",
159 /* Re-enable the default action on SIGTERM et al
160 * so that workers die naturally when sent them.
161 * Only the parent unregisters with pmap and
162 * hence needs to do special SIGTERM handling. */
164 sa.sa_handler = SIG_DFL;
166 sigemptyset(&sa.sa_mask);
167 sigaction(SIGHUP, &sa, NULL);
168 sigaction(SIGINT, &sa, NULL);
169 sigaction(SIGTERM, &sa, NULL);
171 /* fall into my_svc_run in caller */
178 unregister_services();
180 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
190 unregister_services();
191 if (num_threads > 1) {
192 /* play Kronos and eat our children */
197 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
203 /* don't exit on SIGHUP */
204 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
209 mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
212 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
213 char buf[INET6_ADDRSTRLEN];
215 xlog(D_CALL, "Received NULL request from %s",
216 host_ntop(sap, buf, sizeof(buf)));
222 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
224 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
225 char buf[INET6_ADDRSTRLEN];
226 struct nfs_fh_len *fh;
228 xlog(D_CALL, "Received MNT1(%s) request from %s", *path,
229 host_ntop(sap, buf, sizeof(buf)));
231 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
233 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
238 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
240 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
241 char buf[INET6_ADDRSTRLEN];
243 xlog(D_CALL, "Received DUMP request from %s",
244 host_ntop(sap, buf, sizeof(buf)));
246 *res = mountlist_list();
252 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
254 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
257 char rpath[MAXPATHLEN+1];
258 char buf[INET6_ADDRSTRLEN];
263 if (realpath(p, rpath) != NULL) {
264 rpath[sizeof (rpath) - 1] = '\0';
268 xlog(D_CALL, "Received UMNT(%s) request from %s", p,
269 host_ntop(sap, buf, sizeof(buf)));
271 exp = auth_authenticate("unmount", sap, p);
275 mountlist_del(host_ntop(sap, buf, sizeof(buf)), p);
280 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
283 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
284 char buf[INET6_ADDRSTRLEN];
286 xlog(D_CALL, "Received UMNTALL request from %s",
287 host_ntop(sap, buf, sizeof(buf)));
289 /* Reload /etc/xtab if necessary */
292 mountlist_del_all(nfs_getrpccaller(rqstp->rq_xprt));
297 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
299 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
300 char buf[INET6_ADDRSTRLEN];
302 xlog(D_CALL, "Received EXPORT request from %s.",
303 host_ntop(sap, buf, sizeof(buf)));
305 *resp = get_exportlist();
311 mount_exportall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
313 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
314 char buf[INET6_ADDRSTRLEN];
316 xlog(D_CALL, "Received EXPORTALL request from %s.",
317 host_ntop(sap, buf, sizeof(buf)));
319 *resp = get_exportlist();
325 * MNTv2 pathconf procedure
327 * The protocol doesn't include a status field, so Sun apparently considers
328 * it good practice to let anyone snoop on your system, even if it's
329 * pretty harmless data such as pathconf. We don't.
331 * Besides, many of the pathconf values don't make much sense on NFS volumes.
332 * FIFOs and tty device files represent devices on the *client*, so there's
333 * no point in getting the server's buffer sizes etc.
336 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
338 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
341 char rpath[MAXPATHLEN+1];
343 char buf[INET6_ADDRSTRLEN];
345 memset(res, 0, sizeof(*res));
350 /* Reload /etc/xtab if necessary */
353 /* Resolve symlinks */
354 if (realpath(p, rpath) != NULL) {
355 rpath[sizeof (rpath) - 1] = '\0';
359 xlog(D_CALL, "Received PATHCONF(%s) request from %s", p,
360 host_ntop(sap, buf, sizeof(buf)));
362 /* Now authenticate the intruder... */
363 exp = auth_authenticate("pathconf", sap, p);
366 else if (stat(p, &stb) < 0) {
367 xlog(L_WARNING, "can't stat exported dir %s: %s",
372 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
373 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
374 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
375 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
376 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
377 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
378 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
380 /* Can't figure out what to do with pc_mask */
388 * We should advertise the preferred flavours first. (See RFC 2623
389 * section 2.7.) We leave that to the administrator, by advertising
390 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
391 * dropped from the list to avoid backward compatibility issue with
392 * older Linux clients, who inspect the list in reversed order.
394 * XXX: It might be more helpful to rearrange these so that flavors
395 * giving more access (as determined from readonly and id-squashing
396 * options) come first. (If we decide to do that we should probably do
397 * that when reading the exports rather than here.)
399 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
402 static int flavors[SECFLAVOR_COUNT];
405 for (s = exp->m_export.e_secinfo; s->flav; s++) {
406 if (s->flav->fnum == AUTH_NULL)
408 flavors[i] = s->flav->fnum;
412 /* default when there is no sec= option: */
414 flavors[0] = AUTH_UNIX;
416 ok->auth_flavors.auth_flavors_val = flavors;
417 ok->auth_flavors.auth_flavors_len = i;
421 * NFSv3 MOUNT procedure
424 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
426 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
427 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
428 char buf[INET6_ADDRSTRLEN];
430 struct nfs_fh_len *fh;
432 xlog(D_CALL, "Received MNT3(%s) request from %s", *path,
433 host_ntop(sap, buf, sizeof(buf)));
435 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
439 ok->fhandle.fhandle3_len = fh->fh_size;
440 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
441 set_authflavors(ok, exp);
445 static struct nfs_fh_len *
446 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
447 mountstat3 *error, int v3)
449 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
450 struct stat stb, estb;
452 struct nfs_fh_len *fh;
453 char rpath[MAXPATHLEN+1];
455 char buf[INET6_ADDRSTRLEN];
460 /* Reload /var/lib/nfs/etab if necessary */
463 /* Resolve symlinks */
464 if (realpath(p, rpath) != NULL) {
465 rpath[sizeof (rpath) - 1] = '\0';
469 /* Now authenticate the intruder... */
470 exp = auth_authenticate("mount", sap, p);
472 *error = NFSERR_ACCES;
475 if (stat(p, &stb) < 0) {
476 xlog(L_WARNING, "can't stat exported dir %s: %s",
479 *error = NFSERR_NOENT;
481 *error = NFSERR_ACCES;
484 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
485 xlog(L_WARNING, "%s is not a directory or regular file", p);
486 *error = NFSERR_NOTDIR;
489 if (stat(exp->m_export.e_path, &estb) < 0) {
490 xlog(L_WARNING, "can't stat export point %s: %s",
492 *error = NFSERR_NOENT;
495 if (estb.st_dev != stb.st_dev
497 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
498 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
499 p, exp->m_export.e_path);
500 *error = NFSERR_ACCES;
503 if (exp->m_export.e_mountpoint &&
504 !is_mountpoint(exp->m_export.e_mountpoint[0]?
505 exp->m_export.e_mountpoint:
506 exp->m_export.e_path)) {
507 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
509 *error = NFSERR_NOENT;
514 /* This will be a static private nfs_export with just one
515 * address. We feed it to kernel then extract the filehandle,
519 if (cache_export(exp, p)) {
520 *error = NFSERR_ACCES;
523 fh = cache_get_filehandle(exp, v3?64:32, p);
525 *error = NFSERR_ACCES;
531 if (exp->m_exported<1) {
539 fh = getfh_size((struct sockaddr_in *)sap, p, 64);
540 if (!v3 || (fh == NULL && errno == EINVAL)) {
541 /* We first try the new nfs syscall. */
542 fh = getfh((struct sockaddr_in *)sap, p);
543 if (fh == NULL && errno == EINVAL)
544 /* Let's try the old one. */
545 fh = getfh_old((struct sockaddr_in *)sap,
546 stb.st_dev, stb.st_ino);
548 if (fh == NULL && !did_export) {
554 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
555 *error = NFSERR_ACCES;
560 mountlist_add(host_ntop(sap, buf, sizeof(buf)), p);
566 static void remove_all_clients(exportnode *e)
568 struct groupnode *g, *ng;
570 for (g = e->ex_groups; g; g = ng) {
578 static void free_exportlist(exports *elist)
580 struct exportnode *e, *ne;
582 for (e = *elist; e != NULL; e = ne) {
584 remove_all_clients(e);
591 static void prune_clients(nfs_export *exp, struct exportnode *e)
593 struct addrinfo *ai = NULL;
594 struct groupnode *c, **cp;
597 while ((c = *cp) != NULL) {
598 if (client_gettype(c->gr_name) == MCL_FQDN
599 && (ai = host_addrinfo(c->gr_name))) {
600 if (client_check(exp->m_client, ai)) {
613 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
617 for (e = *elist; e != NULL; e = e->ex_next) {
618 if (!strcmp(exp->m_export.e_path, e->ex_dir))
621 e = xmalloc(sizeof(*e));
624 e->ex_dir = xstrdup(exp->m_export.e_path);
629 static void insert_group(struct exportnode *e, char *newname)
633 for (g = e->ex_groups; g; g = g->gr_next)
634 if (strcmp(g->gr_name, newname))
637 g = xmalloc(sizeof(*g));
638 g->gr_name = xstrdup(newname);
639 g->gr_next = e->ex_groups;
646 static exports elist = NULL;
647 struct exportnode *e;
650 static unsigned int ecounter;
651 unsigned int acounter;
653 acounter = auth_reload();
654 if (elist && acounter == ecounter)
659 free_exportlist(&elist);
661 for (i = 0; i < MCL_MAXTYPES; i++) {
662 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
663 /* Don't show pseudo exports */
664 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
666 e = lookup_or_create_elist_entry(&elist, exp);
668 /* exports to "*" absorb any others */
669 if (i == MCL_ANONYMOUS && e->ex_groups) {
670 remove_all_clients(e);
673 /* non-FQDN's absorb FQDN's they contain: */
674 if (i != MCL_FQDN && e->ex_groups)
675 prune_clients(exp, e);
677 if (exp->m_export.e_hostname[0] != '\0')
678 insert_group(e, exp->m_export.e_hostname);
686 main(int argc, char **argv)
688 char *export_file = _PATH_EXPORTS;
689 char *state_dir = NFS_STATEDIR;
698 /* Parse the command line options and arguments. */
700 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
706 descriptors = atoi(optarg);
707 if (descriptors <= 0) {
708 fprintf(stderr, "%s: bad descriptors: %s\n",
717 xlog_sconfig(optarg, 1);
720 export_file = optarg;
722 case 'H': /* PRC: specify a high-availability callout program */
723 ha_callout_prog = optarg;
728 case 'P': /* XXX for nfs-server compatibility */
731 if (port <= 0 || port > 65535) {
732 fprintf(stderr, "%s: bad port number: %s\n",
739 if (vers < 2 || vers > 4) {
740 fprintf(stderr, "%s: bad version number: %s\n",
744 nfs_version &= ~NFSVERSBIT(vers);
747 _rpcfdtype = SOCK_DGRAM;
753 if ((state_dir = xstrdup(optarg)) == NULL) {
754 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
760 num_threads = atoi (optarg);
764 if (vers < 2 || vers > 4) {
765 fprintf(stderr, "%s: bad version number: %s\n",
769 nfs_version |= NFSVERSBIT(vers);
772 printf("kmountd %s\n", VERSION);
781 /* No more arguments allowed.
782 * Require at least one valid version (2, 3, or 4)
784 if (optind != argc || !version_any())
787 if (chdir(state_dir)) {
788 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
789 argv [0], state_dir, strerror(errno));
793 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
794 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
795 argv [0], strerror(errno));
797 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
798 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
799 descriptors > FD_SETSIZE)
800 descriptors = FD_SETSIZE;
802 rlim.rlim_cur = descriptors;
803 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
804 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
805 argv [0], strerror(errno));
810 /* Initialize logging. */
811 if (!foreground) xlog_stderr(0);
814 sa.sa_handler = SIG_IGN;
816 sigemptyset(&sa.sa_mask);
817 sigaction(SIGHUP, &sa, NULL);
818 sigaction(SIGINT, &sa, NULL);
819 sigaction(SIGTERM, &sa, NULL);
820 sigaction(SIGPIPE, &sa, NULL);
821 /* WARNING: the following works on Linux and SysV, but not BSD! */
822 sigaction(SIGCHLD, &sa, NULL);
824 /* Daemons should close all extra filehandles ... *before* RPC init. */
828 new_cache = check_new_cache();
833 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
834 mount_dispatch, port);
835 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
836 mount_dispatch, port);
839 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
840 mount_dispatch, port);
842 sa.sa_handler = killer;
843 sigaction(SIGINT, &sa, NULL);
844 sigaction(SIGTERM, &sa, NULL);
845 sa.sa_handler = sig_hup;
846 sigaction(SIGHUP, &sa, NULL);
848 auth_init(export_file);
851 /* We first fork off a child. */
852 if ((c = fork()) > 0)
855 xlog(L_FATAL, "mountd: cannot fork: %s\n",
858 /* Now we remove ourselves from the foreground.
859 Redirect stdin/stdout/stderr first. */
861 int fd = open("/dev/null", O_RDWR);
865 if (fd > 2) (void) close(fd);
870 /* silently bounds check num_threads */
873 else if (num_threads < 1)
875 else if (num_threads > MAX_THREADS)
876 num_threads = MAX_THREADS;
883 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
888 usage(const char *prog, int n)
891 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
892 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
893 " [-p|--port port] [-V version|--nfs-version version]\n"
894 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
895 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
896 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);