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)
103 nfs_svc_unregister(MOUNTPROG, MOUNTVERS);
104 nfs_svc_unregister(MOUNTPROG, MOUNTVERS_POSIX);
107 nfs_svc_unregister(MOUNTPROG, MOUNTVERS_NFSV3);
111 cleanup_lockfiles (void)
113 unlink(_PATH_XTABLCK);
114 unlink(_PATH_ETABLCK);
115 unlink(_PATH_RMTABLCK);
118 /* Wait for all worker child processes to exit and reap them */
120 wait_for_workers (void)
127 pid = waitpid(0, &status, 0);
131 return; /* no more children */
132 xlog(L_FATAL, "mountd: can't wait: %s\n",
136 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
137 * does not happen on 2.6 kernels, and waitpid() blocks
138 * until all the children are dead then returns with
139 * -ECHILD. But, we don't need to do anything on the
140 * death of individual workers, so we don't care. */
141 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
146 /* Fork num_threads worker children and wait for them */
153 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
155 for (i = 0 ; i < num_threads ; i++) {
158 xlog(L_FATAL, "mountd: cannot fork: %s\n",
164 /* Re-enable the default action on SIGTERM et al
165 * so that workers die naturally when sent them.
166 * Only the parent unregisters with pmap and
167 * hence needs to do special SIGTERM handling. */
169 sa.sa_handler = SIG_DFL;
171 sigemptyset(&sa.sa_mask);
172 sigaction(SIGHUP, &sa, NULL);
173 sigaction(SIGINT, &sa, NULL);
174 sigaction(SIGTERM, &sa, NULL);
176 /* fall into my_svc_run in caller */
183 unregister_services();
185 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
195 unregister_services();
196 if (num_threads > 1) {
197 /* play Kronos and eat our children */
202 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
208 /* don't exit on SIGHUP */
209 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
214 mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
217 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
218 char buf[INET6_ADDRSTRLEN];
220 xlog(D_CALL, "Received NULL request from %s",
221 host_ntop(sap, buf, sizeof(buf)));
227 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
229 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
230 char buf[INET6_ADDRSTRLEN];
231 struct nfs_fh_len *fh;
233 xlog(D_CALL, "Received MNT1(%s) request from %s", *path,
234 host_ntop(sap, buf, sizeof(buf)));
236 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
238 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
243 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
245 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
246 char buf[INET6_ADDRSTRLEN];
248 xlog(D_CALL, "Received DUMP request from %s",
249 host_ntop(sap, buf, sizeof(buf)));
251 *res = mountlist_list();
257 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
259 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
262 char rpath[MAXPATHLEN+1];
263 char buf[INET6_ADDRSTRLEN];
268 if (realpath(p, rpath) != NULL) {
269 rpath[sizeof (rpath) - 1] = '\0';
273 xlog(D_CALL, "Received UMNT(%s) request from %s", p,
274 host_ntop(sap, buf, sizeof(buf)));
276 exp = auth_authenticate("unmount", sap, p);
280 mountlist_del(host_ntop(sap, buf, sizeof(buf)), p);
285 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
288 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
289 char buf[INET6_ADDRSTRLEN];
291 xlog(D_CALL, "Received UMNTALL request from %s",
292 host_ntop(sap, buf, sizeof(buf)));
294 /* Reload /etc/xtab if necessary */
297 mountlist_del_all(nfs_getrpccaller(rqstp->rq_xprt));
302 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
304 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
305 char buf[INET6_ADDRSTRLEN];
307 xlog(D_CALL, "Received EXPORT request from %s.",
308 host_ntop(sap, buf, sizeof(buf)));
310 *resp = get_exportlist();
316 mount_exportall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
318 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
319 char buf[INET6_ADDRSTRLEN];
321 xlog(D_CALL, "Received EXPORTALL request from %s.",
322 host_ntop(sap, buf, sizeof(buf)));
324 *resp = get_exportlist();
330 * MNTv2 pathconf procedure
332 * The protocol doesn't include a status field, so Sun apparently considers
333 * it good practice to let anyone snoop on your system, even if it's
334 * pretty harmless data such as pathconf. We don't.
336 * Besides, many of the pathconf values don't make much sense on NFS volumes.
337 * FIFOs and tty device files represent devices on the *client*, so there's
338 * no point in getting the server's buffer sizes etc.
341 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
343 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
346 char rpath[MAXPATHLEN+1];
348 char buf[INET6_ADDRSTRLEN];
350 memset(res, 0, sizeof(*res));
355 /* Reload /etc/xtab if necessary */
358 /* Resolve symlinks */
359 if (realpath(p, rpath) != NULL) {
360 rpath[sizeof (rpath) - 1] = '\0';
364 xlog(D_CALL, "Received PATHCONF(%s) request from %s", p,
365 host_ntop(sap, buf, sizeof(buf)));
367 /* Now authenticate the intruder... */
368 exp = auth_authenticate("pathconf", sap, p);
371 else if (stat(p, &stb) < 0) {
372 xlog(L_WARNING, "can't stat exported dir %s: %s",
377 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
378 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
379 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
380 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
381 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
382 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
383 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
385 /* Can't figure out what to do with pc_mask */
393 * We should advertise the preferred flavours first. (See RFC 2623
394 * section 2.7.) We leave that to the administrator, by advertising
395 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
396 * dropped from the list to avoid backward compatibility issue with
397 * older Linux clients, who inspect the list in reversed order.
399 * XXX: It might be more helpful to rearrange these so that flavors
400 * giving more access (as determined from readonly and id-squashing
401 * options) come first. (If we decide to do that we should probably do
402 * that when reading the exports rather than here.)
404 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
407 static int flavors[SECFLAVOR_COUNT];
410 for (s = exp->m_export.e_secinfo; s->flav; s++) {
411 if (s->flav->fnum == AUTH_NULL)
413 flavors[i] = s->flav->fnum;
417 /* default when there is no sec= option: */
419 flavors[0] = AUTH_UNIX;
421 ok->auth_flavors.auth_flavors_val = flavors;
422 ok->auth_flavors.auth_flavors_len = i;
426 * NFSv3 MOUNT procedure
429 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
431 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
432 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
433 char buf[INET6_ADDRSTRLEN];
435 struct nfs_fh_len *fh;
437 xlog(D_CALL, "Received MNT3(%s) request from %s", *path,
438 host_ntop(sap, buf, sizeof(buf)));
440 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
444 ok->fhandle.fhandle3_len = fh->fh_size;
445 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
446 set_authflavors(ok, exp);
450 static struct nfs_fh_len *
451 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
452 mountstat3 *error, int v3)
454 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
455 struct stat stb, estb;
457 struct nfs_fh_len *fh;
458 char rpath[MAXPATHLEN+1];
460 char buf[INET6_ADDRSTRLEN];
465 /* Reload /var/lib/nfs/etab if necessary */
468 /* Resolve symlinks */
469 if (realpath(p, rpath) != NULL) {
470 rpath[sizeof (rpath) - 1] = '\0';
474 /* Now authenticate the intruder... */
475 exp = auth_authenticate("mount", sap, p);
477 *error = MNT3ERR_ACCES;
480 if (stat(p, &stb) < 0) {
481 xlog(L_WARNING, "can't stat exported dir %s: %s",
484 *error = MNT3ERR_NOENT;
486 *error = MNT3ERR_ACCES;
489 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
490 xlog(L_WARNING, "%s is not a directory or regular file", p);
491 *error = MNT3ERR_NOTDIR;
494 if (stat(exp->m_export.e_path, &estb) < 0) {
495 xlog(L_WARNING, "can't stat export point %s: %s",
497 *error = MNT3ERR_NOENT;
500 if (estb.st_dev != stb.st_dev
502 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
503 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
504 p, exp->m_export.e_path);
505 *error = MNT3ERR_ACCES;
508 if (exp->m_export.e_mountpoint &&
509 !is_mountpoint(exp->m_export.e_mountpoint[0]?
510 exp->m_export.e_mountpoint:
511 exp->m_export.e_path)) {
512 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
514 *error = MNT3ERR_NOENT;
519 /* This will be a static private nfs_export with just one
520 * address. We feed it to kernel then extract the filehandle,
524 if (cache_export(exp, p)) {
525 *error = MNT3ERR_ACCES;
528 fh = cache_get_filehandle(exp, v3?64:32, p);
530 *error = MNT3ERR_ACCES;
536 if (exp->m_exported<1) {
544 fh = getfh_size((struct sockaddr_in *)sap, p, 64);
545 if (!v3 || (fh == NULL && errno == EINVAL)) {
546 /* We first try the new nfs syscall. */
547 fh = getfh((struct sockaddr_in *)sap, p);
548 if (fh == NULL && errno == EINVAL)
549 /* Let's try the old one. */
550 fh = getfh_old((struct sockaddr_in *)sap,
551 stb.st_dev, stb.st_ino);
553 if (fh == NULL && !did_export) {
559 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
560 *error = MNT3ERR_ACCES;
565 mountlist_add(host_ntop(sap, buf, sizeof(buf)), p);
571 static void remove_all_clients(exportnode *e)
573 struct groupnode *g, *ng;
575 for (g = e->ex_groups; g; g = ng) {
583 static void free_exportlist(exports *elist)
585 struct exportnode *e, *ne;
587 for (e = *elist; e != NULL; e = ne) {
589 remove_all_clients(e);
596 static void prune_clients(nfs_export *exp, struct exportnode *e)
598 struct addrinfo *ai = NULL;
599 struct groupnode *c, **cp;
602 while ((c = *cp) != NULL) {
603 if (client_gettype(c->gr_name) == MCL_FQDN
604 && (ai = host_addrinfo(c->gr_name))) {
605 if (client_check(exp->m_client, ai)) {
618 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
622 for (e = *elist; e != NULL; e = e->ex_next) {
623 if (!strcmp(exp->m_export.e_path, e->ex_dir))
626 e = xmalloc(sizeof(*e));
629 e->ex_dir = xstrdup(exp->m_export.e_path);
634 static void insert_group(struct exportnode *e, char *newname)
638 for (g = e->ex_groups; g; g = g->gr_next)
639 if (strcmp(g->gr_name, newname))
642 g = xmalloc(sizeof(*g));
643 g->gr_name = xstrdup(newname);
644 g->gr_next = e->ex_groups;
651 static exports elist = NULL;
652 struct exportnode *e;
655 static unsigned int ecounter;
656 unsigned int acounter;
658 acounter = auth_reload();
659 if (elist && acounter == ecounter)
664 free_exportlist(&elist);
666 for (i = 0; i < MCL_MAXTYPES; i++) {
667 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
668 /* Don't show pseudo exports */
669 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
671 e = lookup_or_create_elist_entry(&elist, exp);
673 /* exports to "*" absorb any others */
674 if (i == MCL_ANONYMOUS && e->ex_groups) {
675 remove_all_clients(e);
678 /* non-FQDN's absorb FQDN's they contain: */
679 if (i != MCL_FQDN && e->ex_groups)
680 prune_clients(exp, e);
682 if (exp->m_export.e_hostname[0] != '\0')
683 insert_group(e, exp->m_export.e_hostname);
691 main(int argc, char **argv)
693 char *export_file = _PATH_EXPORTS;
694 char *state_dir = NFS_STATEDIR;
696 unsigned int listeners = 0;
705 /* Set the basename */
706 if ((progname = strrchr(argv[0], '/')) != NULL)
711 /* Parse the command line options and arguments. */
713 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
719 descriptors = atoi(optarg);
720 if (descriptors <= 0) {
721 fprintf(stderr, "%s: bad descriptors: %s\n",
730 xlog_sconfig(optarg, 1);
733 export_file = optarg;
735 case 'H': /* PRC: specify a high-availability callout program */
736 ha_callout_prog = optarg;
741 case 'P': /* XXX for nfs-server compatibility */
744 if (port <= 0 || port > 65535) {
745 fprintf(stderr, "%s: bad port number: %s\n",
752 if (vers < 2 || vers > 4) {
753 fprintf(stderr, "%s: bad version number: %s\n",
757 nfs_version &= ~NFSVERSBIT(vers);
760 _rpcfdtype = SOCK_DGRAM;
766 if ((state_dir = xstrdup(optarg)) == NULL) {
767 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
773 num_threads = atoi (optarg);
777 if (vers < 2 || vers > 4) {
778 fprintf(stderr, "%s: bad version number: %s\n",
782 nfs_version |= NFSVERSBIT(vers);
785 printf("%s version " VERSION "\n", progname);
794 /* No more arguments allowed. */
795 if (optind != argc || !version_any())
798 if (chdir(state_dir)) {
799 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
800 progname, state_dir, strerror(errno));
804 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
805 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
806 progname, strerror(errno));
808 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
809 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
810 descriptors > FD_SETSIZE)
811 descriptors = FD_SETSIZE;
813 rlim.rlim_cur = descriptors;
814 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
815 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
816 progname, strerror(errno));
821 /* Initialize logging. */
822 if (!foreground) xlog_stderr(0);
825 sa.sa_handler = SIG_IGN;
827 sigemptyset(&sa.sa_mask);
828 sigaction(SIGHUP, &sa, NULL);
829 sigaction(SIGINT, &sa, NULL);
830 sigaction(SIGTERM, &sa, NULL);
831 sigaction(SIGPIPE, &sa, NULL);
832 /* WARNING: the following works on Linux and SysV, but not BSD! */
833 sigaction(SIGCHLD, &sa, NULL);
835 /* Daemons should close all extra filehandles ... *before* RPC init. */
839 new_cache = check_new_cache();
844 listeners += nfs_svc_create("mountd", MOUNTPROG,
845 MOUNTVERS, mount_dispatch, port);
846 listeners += nfs_svc_create("mountd", MOUNTPROG,
847 MOUNTVERS_POSIX, mount_dispatch, port);
850 listeners += nfs_svc_create("mountd", MOUNTPROG,
851 MOUNTVERS_NFSV3, mount_dispatch, port);
852 if (version23() && listeners == 0)
853 xlog(L_FATAL, "mountd: could not create listeners\n");
855 sa.sa_handler = killer;
856 sigaction(SIGINT, &sa, NULL);
857 sigaction(SIGTERM, &sa, NULL);
858 sa.sa_handler = sig_hup;
859 sigaction(SIGHUP, &sa, NULL);
861 auth_init(export_file);
864 /* We first fork off a child. */
865 if ((c = fork()) > 0)
868 xlog(L_FATAL, "mountd: cannot fork: %s\n",
871 /* Now we remove ourselves from the foreground.
872 Redirect stdin/stdout/stderr first. */
874 int fd = open("/dev/null", O_RDWR);
878 if (fd > 2) (void) close(fd);
883 /* silently bounds check num_threads */
886 else if (num_threads < 1)
888 else if (num_threads > MAX_THREADS)
889 num_threads = MAX_THREADS;
894 xlog(L_NOTICE, "Version " VERSION " starting");
897 xlog(L_ERROR, "RPC service loop terminated unexpectedly. Exiting...\n");
898 unregister_services();
903 usage(const char *prog, int n)
906 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
907 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
908 " [-p|--port port] [-V version|--nfs-version version]\n"
909 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
910 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
911 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);