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 static int nfs_version = -1;
77 unregister_services (void)
79 if (nfs_version & (0x1 << 1)) {
80 pmap_unset (MOUNTPROG, MOUNTVERS);
81 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
83 if (nfs_version & (0x1 << 2))
84 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
88 cleanup_lockfiles (void)
90 unlink(_PATH_XTABLCK);
91 unlink(_PATH_ETABLCK);
92 unlink(_PATH_RMTABLCK);
95 /* Wait for all worker child processes to exit and reap them */
97 wait_for_workers (void)
104 pid = waitpid(0, &status, 0);
108 return; /* no more children */
109 xlog(L_FATAL, "mountd: can't wait: %s\n",
113 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
114 * does not happen on 2.6 kernels, and waitpid() blocks
115 * until all the children are dead then returns with
116 * -ECHILD. But, we don't need to do anything on the
117 * death of individual workers, so we don't care. */
118 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
123 /* Fork num_threads worker children and wait for them */
130 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
132 for (i = 0 ; i < num_threads ; i++) {
135 xlog(L_FATAL, "mountd: cannot fork: %s\n",
141 /* Re-enable the default action on SIGTERM et al
142 * so that workers die naturally when sent them.
143 * Only the parent unregisters with pmap and
144 * hence needs to do special SIGTERM handling. */
146 sa.sa_handler = SIG_DFL;
148 sigemptyset(&sa.sa_mask);
149 sigaction(SIGHUP, &sa, NULL);
150 sigaction(SIGINT, &sa, NULL);
151 sigaction(SIGTERM, &sa, NULL);
153 /* fall into my_svc_run in caller */
160 unregister_services();
162 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
172 unregister_services();
173 if (num_threads > 1) {
174 /* play Kronos and eat our children */
179 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
185 /* don't exit on SIGHUP */
186 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
191 mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
194 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
195 char buf[INET6_ADDRSTRLEN];
197 xlog(D_CALL, "Received NULL request from %s",
198 host_ntop(sap, buf, sizeof(buf)));
204 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
206 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
207 char buf[INET6_ADDRSTRLEN];
208 struct nfs_fh_len *fh;
210 xlog(D_CALL, "Received MNT1(%s) request from %s", *path,
211 host_ntop(sap, buf, sizeof(buf)));
213 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
215 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
220 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
222 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
223 char buf[INET6_ADDRSTRLEN];
225 xlog(D_CALL, "Received DUMP request from %s",
226 host_ntop(sap, buf, sizeof(buf)));
228 *res = mountlist_list();
234 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
236 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
239 char rpath[MAXPATHLEN+1];
240 char buf[INET6_ADDRSTRLEN];
245 if (realpath(p, rpath) != NULL) {
246 rpath[sizeof (rpath) - 1] = '\0';
250 xlog(D_CALL, "Received UMNT(%s) request from %s", p,
251 host_ntop(sap, buf, sizeof(buf)));
253 exp = auth_authenticate("unmount", sap, p);
257 mountlist_del(host_ntop(sap, buf, sizeof(buf)), p);
262 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
265 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
266 char buf[INET6_ADDRSTRLEN];
268 xlog(D_CALL, "Received UMNTALL request from %s",
269 host_ntop(sap, buf, sizeof(buf)));
271 /* Reload /etc/xtab if necessary */
274 mountlist_del_all(nfs_getrpccaller(rqstp->rq_xprt));
279 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
281 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
282 char buf[INET6_ADDRSTRLEN];
284 xlog(D_CALL, "Received EXPORT request from %s.",
285 host_ntop(sap, buf, sizeof(buf)));
287 *resp = get_exportlist();
293 mount_exportall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
295 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
296 char buf[INET6_ADDRSTRLEN];
298 xlog(D_CALL, "Received EXPORTALL request from %s.",
299 host_ntop(sap, buf, sizeof(buf)));
301 *resp = get_exportlist();
307 * MNTv2 pathconf procedure
309 * The protocol doesn't include a status field, so Sun apparently considers
310 * it good practice to let anyone snoop on your system, even if it's
311 * pretty harmless data such as pathconf. We don't.
313 * Besides, many of the pathconf values don't make much sense on NFS volumes.
314 * FIFOs and tty device files represent devices on the *client*, so there's
315 * no point in getting the server's buffer sizes etc.
318 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
320 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
323 char rpath[MAXPATHLEN+1];
325 char buf[INET6_ADDRSTRLEN];
327 memset(res, 0, sizeof(*res));
332 /* Reload /etc/xtab if necessary */
335 /* Resolve symlinks */
336 if (realpath(p, rpath) != NULL) {
337 rpath[sizeof (rpath) - 1] = '\0';
341 xlog(D_CALL, "Received PATHCONF(%s) request from %s", p,
342 host_ntop(sap, buf, sizeof(buf)));
344 /* Now authenticate the intruder... */
345 exp = auth_authenticate("pathconf", sap, p);
348 else if (stat(p, &stb) < 0) {
349 xlog(L_WARNING, "can't stat exported dir %s: %s",
354 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
355 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
356 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
357 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
358 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
359 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
360 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
362 /* Can't figure out what to do with pc_mask */
370 * We should advertise the preferred flavours first. (See RFC 2623
371 * section 2.7.) We leave that to the administrator, by advertising
372 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
373 * dropped from the list to avoid backward compatibility issue with
374 * older Linux clients, who inspect the list in reversed order.
376 * XXX: It might be more helpful to rearrange these so that flavors
377 * giving more access (as determined from readonly and id-squashing
378 * options) come first. (If we decide to do that we should probably do
379 * that when reading the exports rather than here.)
381 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
384 static int flavors[SECFLAVOR_COUNT];
387 for (s = exp->m_export.e_secinfo; s->flav; s++) {
388 if (s->flav->fnum == AUTH_NULL)
390 flavors[i] = s->flav->fnum;
394 /* default when there is no sec= option: */
396 flavors[0] = AUTH_UNIX;
398 ok->auth_flavors.auth_flavors_val = flavors;
399 ok->auth_flavors.auth_flavors_len = i;
403 * NFSv3 MOUNT procedure
406 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
408 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
409 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
410 char buf[INET6_ADDRSTRLEN];
412 struct nfs_fh_len *fh;
414 xlog(D_CALL, "Received MNT3(%s) request from %s", *path,
415 host_ntop(sap, buf, sizeof(buf)));
417 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
421 ok->fhandle.fhandle3_len = fh->fh_size;
422 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
423 set_authflavors(ok, exp);
427 static struct nfs_fh_len *
428 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
429 mountstat3 *error, int v3)
431 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
432 struct stat stb, estb;
434 struct nfs_fh_len *fh;
435 char rpath[MAXPATHLEN+1];
437 char buf[INET6_ADDRSTRLEN];
442 /* Reload /var/lib/nfs/etab if necessary */
445 /* Resolve symlinks */
446 if (realpath(p, rpath) != NULL) {
447 rpath[sizeof (rpath) - 1] = '\0';
451 /* Now authenticate the intruder... */
452 exp = auth_authenticate("mount", sap, p);
454 *error = NFSERR_ACCES;
457 if (stat(p, &stb) < 0) {
458 xlog(L_WARNING, "can't stat exported dir %s: %s",
461 *error = NFSERR_NOENT;
463 *error = NFSERR_ACCES;
466 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
467 xlog(L_WARNING, "%s is not a directory or regular file", p);
468 *error = NFSERR_NOTDIR;
471 if (stat(exp->m_export.e_path, &estb) < 0) {
472 xlog(L_WARNING, "can't stat export point %s: %s",
474 *error = NFSERR_NOENT;
477 if (estb.st_dev != stb.st_dev
479 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
480 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
481 p, exp->m_export.e_path);
482 *error = NFSERR_ACCES;
485 if (exp->m_export.e_mountpoint &&
486 !is_mountpoint(exp->m_export.e_mountpoint[0]?
487 exp->m_export.e_mountpoint:
488 exp->m_export.e_path)) {
489 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
491 *error = NFSERR_NOENT;
496 /* This will be a static private nfs_export with just one
497 * address. We feed it to kernel then extract the filehandle,
501 if (cache_export(exp, p)) {
502 *error = NFSERR_ACCES;
505 fh = cache_get_filehandle(exp, v3?64:32, p);
507 *error = NFSERR_ACCES;
513 if (exp->m_exported<1) {
521 fh = getfh_size((struct sockaddr_in *)sap, p, 64);
522 if (!v3 || (fh == NULL && errno == EINVAL)) {
523 /* We first try the new nfs syscall. */
524 fh = getfh((struct sockaddr_in *)sap, p);
525 if (fh == NULL && errno == EINVAL)
526 /* Let's try the old one. */
527 fh = getfh_old((struct sockaddr_in *)sap,
528 stb.st_dev, stb.st_ino);
530 if (fh == NULL && !did_export) {
536 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
537 *error = NFSERR_ACCES;
542 mountlist_add(host_ntop(sap, buf, sizeof(buf)), p);
548 static void remove_all_clients(exportnode *e)
550 struct groupnode *g, *ng;
552 for (g = e->ex_groups; g; g = ng) {
560 static void free_exportlist(exports *elist)
562 struct exportnode *e, *ne;
564 for (e = *elist; e != NULL; e = ne) {
566 remove_all_clients(e);
573 static void prune_clients(nfs_export *exp, struct exportnode *e)
575 struct addrinfo *ai = NULL;
576 struct groupnode *c, **cp;
579 while ((c = *cp) != NULL) {
580 if (client_gettype(c->gr_name) == MCL_FQDN
581 && (ai = host_addrinfo(c->gr_name))) {
582 if (client_check(exp->m_client, ai)) {
595 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
599 for (e = *elist; e != NULL; e = e->ex_next) {
600 if (!strcmp(exp->m_export.e_path, e->ex_dir))
603 e = xmalloc(sizeof(*e));
606 e->ex_dir = xstrdup(exp->m_export.e_path);
611 static void insert_group(struct exportnode *e, char *newname)
615 for (g = e->ex_groups; g; g = g->gr_next)
616 if (strcmp(g->gr_name, newname))
619 g = xmalloc(sizeof(*g));
620 g->gr_name = xstrdup(newname);
621 g->gr_next = e->ex_groups;
628 static exports elist = NULL;
629 struct exportnode *e;
632 static unsigned int ecounter;
633 unsigned int acounter;
635 acounter = auth_reload();
636 if (elist && acounter == ecounter)
641 free_exportlist(&elist);
643 for (i = 0; i < MCL_MAXTYPES; i++) {
644 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
645 /* Don't show pseudo exports */
646 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
648 e = lookup_or_create_elist_entry(&elist, exp);
650 /* exports to "*" absorb any others */
651 if (i == MCL_ANONYMOUS && e->ex_groups) {
652 remove_all_clients(e);
655 /* non-FQDN's absorb FQDN's they contain: */
656 if (i != MCL_FQDN && e->ex_groups)
657 prune_clients(exp, e);
659 if (exp->m_export.e_hostname[0] != '\0')
660 insert_group(e, exp->m_export.e_hostname);
668 main(int argc, char **argv)
670 char *export_file = _PATH_EXPORTS;
671 char *state_dir = NFS_STATEDIR;
679 /* Parse the command line options and arguments. */
681 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
687 descriptors = atoi(optarg);
688 if (descriptors <= 0) {
689 fprintf(stderr, "%s: bad descriptors: %s\n",
698 xlog_sconfig(optarg, 1);
701 export_file = optarg;
703 case 'H': /* PRC: specify a high-availability callout program */
704 ha_callout_prog = optarg;
709 case 'P': /* XXX for nfs-server compatibility */
712 if (port <= 0 || port > 65535) {
713 fprintf(stderr, "%s: bad port number: %s\n",
719 nfs_version &= ~(1 << (atoi (optarg) - 1));
722 _rpcfdtype = SOCK_DGRAM;
728 if ((state_dir = xstrdup(optarg)) == NULL) {
729 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
735 num_threads = atoi (optarg);
738 nfs_version |= 1 << (atoi (optarg) - 1);
741 printf("kmountd %s\n", VERSION);
750 /* No more arguments allowed.
751 * Require at least one valid version (2, 3, or 4)
753 if (optind != argc || !(nfs_version & 0xE))
756 if (chdir(state_dir)) {
757 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
758 argv [0], state_dir, strerror(errno));
762 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
763 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
764 argv [0], strerror(errno));
766 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
767 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
768 descriptors > FD_SETSIZE)
769 descriptors = FD_SETSIZE;
771 rlim.rlim_cur = descriptors;
772 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
773 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
774 argv [0], strerror(errno));
779 /* Initialize logging. */
780 if (!foreground) xlog_stderr(0);
783 sa.sa_handler = SIG_IGN;
785 sigemptyset(&sa.sa_mask);
786 sigaction(SIGHUP, &sa, NULL);
787 sigaction(SIGINT, &sa, NULL);
788 sigaction(SIGTERM, &sa, NULL);
789 sigaction(SIGPIPE, &sa, NULL);
790 /* WARNING: the following works on Linux and SysV, but not BSD! */
791 sigaction(SIGCHLD, &sa, NULL);
793 /* Daemons should close all extra filehandles ... *before* RPC init. */
797 new_cache = check_new_cache();
801 if (nfs_version & (0x1 << 1)) {
802 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
803 mount_dispatch, port);
804 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
805 mount_dispatch, port);
807 if (nfs_version & (0x1 << 2))
808 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
809 mount_dispatch, port);
811 sa.sa_handler = killer;
812 sigaction(SIGINT, &sa, NULL);
813 sigaction(SIGTERM, &sa, NULL);
814 sa.sa_handler = sig_hup;
815 sigaction(SIGHUP, &sa, NULL);
817 auth_init(export_file);
820 /* We first fork off a child. */
821 if ((c = fork()) > 0)
824 xlog(L_FATAL, "mountd: cannot fork: %s\n",
827 /* Now we remove ourselves from the foreground.
828 Redirect stdin/stdout/stderr first. */
830 int fd = open("/dev/null", O_RDWR);
834 if (fd > 2) (void) close(fd);
839 /* silently bounds check num_threads */
842 else if (num_threads < 1)
844 else if (num_threads > MAX_THREADS)
845 num_threads = MAX_THREADS;
852 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
857 usage(const char *prog, int n)
860 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
861 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
862 " [-p|--port port] [-V version|--nfs-version version]\n"
863 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
864 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
865 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);