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 cache_open(void);
32 extern struct nfs_fh_len *cache_get_filehandle(nfs_export *exp, int len, char *p);
33 extern int cache_export(nfs_export *exp, char *path);
35 extern void my_svc_run(void);
37 static void usage(const char *, int exitcode);
38 static exports get_exportlist(void);
39 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, nfs_export **, mountstat3 *, int v3);
41 int reverse_resolve = 0;
46 /* PRC: a high-availability callout program can be specified with -H
47 * When this is done, the program will receive callouts whenever clients
48 * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
49 char *ha_callout_prog = NULL;
51 /* Number of mountd threads to start. Default is 1 and
52 * that's probably enough unless you need hundreds of
53 * clients to be able to mount at once. */
54 static int num_threads = 1;
55 /* Arbitrary limit on number of threads */
56 #define MAX_THREADS 64
58 static struct option longopts[] =
60 { "foreground", 0, 0, 'F' },
61 { "descriptors", 1, 0, 'o' },
62 { "debug", 1, 0, 'd' },
63 { "help", 0, 0, 'h' },
64 { "exports-file", 1, 0, 'f' },
65 { "nfs-version", 1, 0, 'V' },
66 { "no-nfs-version", 1, 0, 'N' },
67 { "version", 0, 0, 'v' },
68 { "port", 1, 0, 'p' },
69 { "no-tcp", 0, 0, 'n' },
70 { "ha-callout", 1, 0, 'H' },
71 { "state-directory-path", 1, 0, 's' },
72 { "num-threads", 1, 0, 't' },
73 { "reverse-lookup", 0, 0, 'r' },
74 { "manage-gids", 0, 0, 'g' },
78 static int nfs_version = -1;
81 unregister_services (void)
83 if (nfs_version & (0x1 << 1)) {
84 pmap_unset (MOUNTPROG, MOUNTVERS);
85 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
87 if (nfs_version & (0x1 << 2))
88 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
92 cleanup_lockfiles (void)
94 unlink(_PATH_XTABLCK);
95 unlink(_PATH_ETABLCK);
96 unlink(_PATH_RMTABLCK);
99 /* Wait for all worker child processes to exit and reap them */
101 wait_for_workers (void)
108 pid = waitpid(0, &status, 0);
112 return; /* no more children */
113 xlog(L_FATAL, "mountd: can't wait: %s\n",
117 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
118 * does not happen on 2.6 kernels, and waitpid() blocks
119 * until all the children are dead then returns with
120 * -ECHILD. But, we don't need to do anything on the
121 * death of individual workers, so we don't care. */
122 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
127 /* Fork num_threads worker children and wait for them */
134 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
136 for (i = 0 ; i < num_threads ; i++) {
139 xlog(L_FATAL, "mountd: cannot fork: %s\n",
145 /* Re-enable the default action on SIGTERM et al
146 * so that workers die naturally when sent them.
147 * Only the parent unregisters with pmap and
148 * hence needs to do special SIGTERM handling. */
150 sa.sa_handler = SIG_DFL;
152 sigemptyset(&sa.sa_mask);
153 sigaction(SIGHUP, &sa, NULL);
154 sigaction(SIGINT, &sa, NULL);
155 sigaction(SIGTERM, &sa, NULL);
157 /* fall into my_svc_run in caller */
164 unregister_services();
166 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
176 unregister_services();
177 if (num_threads > 1) {
178 /* play Kronos and eat our children */
183 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
189 /* don't exit on SIGHUP */
190 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
195 mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
198 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
199 char buf[INET6_ADDRSTRLEN];
201 xlog(D_CALL, "Received NULL request from %s",
202 host_ntop(sap, buf, sizeof(buf)));
208 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
210 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
211 char buf[INET6_ADDRSTRLEN];
212 struct nfs_fh_len *fh;
214 xlog(D_CALL, "Received MNT1(%s) request from %s", *path,
215 host_ntop(sap, buf, sizeof(buf)));
217 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
219 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
224 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
226 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
227 char buf[INET6_ADDRSTRLEN];
229 xlog(D_CALL, "Received DUMP request from %s",
230 host_ntop(sap, buf, sizeof(buf)));
232 *res = mountlist_list();
238 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
240 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
243 char rpath[MAXPATHLEN+1];
244 char buf[INET6_ADDRSTRLEN];
249 if (realpath(p, rpath) != NULL) {
250 rpath[sizeof (rpath) - 1] = '\0';
254 xlog(D_CALL, "Received UMNT(%s) request from %s", p,
255 host_ntop(sap, buf, sizeof(buf)));
257 exp = auth_authenticate("unmount", sap, p);
261 mountlist_del(host_ntop(sap, buf, sizeof(buf)), p);
266 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
269 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
270 char buf[INET6_ADDRSTRLEN];
272 xlog(D_CALL, "Received UMNTALL request from %s",
273 host_ntop(sap, buf, sizeof(buf)));
275 /* Reload /etc/xtab if necessary */
278 mountlist_del_all(nfs_getrpccaller(rqstp->rq_xprt));
283 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
285 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
286 char buf[INET6_ADDRSTRLEN];
288 xlog(D_CALL, "Received EXPORT request from %s.",
289 host_ntop(sap, buf, sizeof(buf)));
291 *resp = get_exportlist();
297 mount_exportall_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 EXPORTALL request from %s.",
303 host_ntop(sap, buf, sizeof(buf)));
305 *resp = get_exportlist();
311 * MNTv2 pathconf procedure
313 * The protocol doesn't include a status field, so Sun apparently considers
314 * it good practice to let anyone snoop on your system, even if it's
315 * pretty harmless data such as pathconf. We don't.
317 * Besides, many of the pathconf values don't make much sense on NFS volumes.
318 * FIFOs and tty device files represent devices on the *client*, so there's
319 * no point in getting the server's buffer sizes etc.
322 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
324 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
327 char rpath[MAXPATHLEN+1];
329 char buf[INET6_ADDRSTRLEN];
331 memset(res, 0, sizeof(*res));
336 /* Reload /etc/xtab if necessary */
339 /* Resolve symlinks */
340 if (realpath(p, rpath) != NULL) {
341 rpath[sizeof (rpath) - 1] = '\0';
345 xlog(D_CALL, "Received PATHCONF(%s) request from %s", p,
346 host_ntop(sap, buf, sizeof(buf)));
348 /* Now authenticate the intruder... */
349 exp = auth_authenticate("pathconf", sap, p);
352 else if (stat(p, &stb) < 0) {
353 xlog(L_WARNING, "can't stat exported dir %s: %s",
358 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
359 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
360 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
361 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
362 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
363 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
364 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
366 /* Can't figure out what to do with pc_mask */
374 * We should advertise the preferred flavours first. (See RFC 2623
375 * section 2.7.) We leave that to the administrator, by advertising
376 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
377 * dropped from the list to avoid backward compatibility issue with
378 * older Linux clients, who inspect the list in reversed order.
380 * XXX: It might be more helpful to rearrange these so that flavors
381 * giving more access (as determined from readonly and id-squashing
382 * options) come first. (If we decide to do that we should probably do
383 * that when reading the exports rather than here.)
385 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
388 static int flavors[SECFLAVOR_COUNT];
391 for (s = exp->m_export.e_secinfo; s->flav; s++) {
392 if (s->flav->fnum == AUTH_NULL)
394 flavors[i] = s->flav->fnum;
398 /* default when there is no sec= option: */
400 flavors[0] = AUTH_UNIX;
402 ok->auth_flavors.auth_flavors_val = flavors;
403 ok->auth_flavors.auth_flavors_len = i;
407 * NFSv3 MOUNT procedure
410 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
412 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
413 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
414 char buf[INET6_ADDRSTRLEN];
416 struct nfs_fh_len *fh;
418 xlog(D_CALL, "Received MNT3(%s) request from %s", *path,
419 host_ntop(sap, buf, sizeof(buf)));
421 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
425 ok->fhandle.fhandle3_len = fh->fh_size;
426 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
427 set_authflavors(ok, exp);
431 static struct nfs_fh_len *
432 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
433 mountstat3 *error, int v3)
435 struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
436 struct stat stb, estb;
438 struct nfs_fh_len *fh;
439 char rpath[MAXPATHLEN+1];
441 char buf[INET6_ADDRSTRLEN];
446 /* Reload /var/lib/nfs/etab if necessary */
449 /* Resolve symlinks */
450 if (realpath(p, rpath) != NULL) {
451 rpath[sizeof (rpath) - 1] = '\0';
455 /* Now authenticate the intruder... */
456 exp = auth_authenticate("mount", sap, p);
458 *error = NFSERR_ACCES;
461 if (stat(p, &stb) < 0) {
462 xlog(L_WARNING, "can't stat exported dir %s: %s",
465 *error = NFSERR_NOENT;
467 *error = NFSERR_ACCES;
470 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
471 xlog(L_WARNING, "%s is not a directory or regular file", p);
472 *error = NFSERR_NOTDIR;
475 if (stat(exp->m_export.e_path, &estb) < 0) {
476 xlog(L_WARNING, "can't stat export point %s: %s",
478 *error = NFSERR_NOENT;
481 if (estb.st_dev != stb.st_dev
483 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
484 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
485 p, exp->m_export.e_path);
486 *error = NFSERR_ACCES;
489 if (exp->m_export.e_mountpoint &&
490 !is_mountpoint(exp->m_export.e_mountpoint[0]?
491 exp->m_export.e_mountpoint:
492 exp->m_export.e_path)) {
493 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
495 *error = NFSERR_NOENT;
500 /* This will be a static private nfs_export with just one
501 * address. We feed it to kernel then extract the filehandle,
505 if (cache_export(exp, p)) {
506 *error = NFSERR_ACCES;
509 fh = cache_get_filehandle(exp, v3?64:32, p);
511 *error = NFSERR_ACCES;
517 if (exp->m_exported<1) {
525 fh = getfh_size((struct sockaddr_in *)sap, p, 64);
526 if (!v3 || (fh == NULL && errno == EINVAL)) {
527 /* We first try the new nfs syscall. */
528 fh = getfh((struct sockaddr_in *)sap, p);
529 if (fh == NULL && errno == EINVAL)
530 /* Let's try the old one. */
531 fh = getfh_old((struct sockaddr_in *)sap,
532 stb.st_dev, stb.st_ino);
534 if (fh == NULL && !did_export) {
540 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
541 *error = NFSERR_ACCES;
546 mountlist_add(host_ntop(sap, buf, sizeof(buf)), p);
552 static void remove_all_clients(exportnode *e)
554 struct groupnode *g, *ng;
556 for (g = e->ex_groups; g; g = ng) {
564 static void free_exportlist(exports *elist)
566 struct exportnode *e, *ne;
568 for (e = *elist; e != NULL; e = ne) {
570 remove_all_clients(e);
577 static void prune_clients(nfs_export *exp, struct exportnode *e)
579 struct addrinfo *ai = NULL;
580 struct groupnode *c, **cp;
583 while ((c = *cp) != NULL) {
584 if (client_gettype(c->gr_name) == MCL_FQDN
585 && (ai = host_addrinfo(c->gr_name))) {
586 if (client_check(exp->m_client, ai)) {
599 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
603 for (e = *elist; e != NULL; e = e->ex_next) {
604 if (!strcmp(exp->m_export.e_path, e->ex_dir))
607 e = xmalloc(sizeof(*e));
610 e->ex_dir = xstrdup(exp->m_export.e_path);
615 static void insert_group(struct exportnode *e, char *newname)
619 for (g = e->ex_groups; g; g = g->gr_next)
620 if (strcmp(g->gr_name, newname))
623 g = xmalloc(sizeof(*g));
624 g->gr_name = xstrdup(newname);
625 g->gr_next = e->ex_groups;
632 static exports elist = NULL;
633 struct exportnode *e;
636 static unsigned int ecounter;
637 unsigned int acounter;
639 acounter = auth_reload();
640 if (elist && acounter == ecounter)
645 free_exportlist(&elist);
647 for (i = 0; i < MCL_MAXTYPES; i++) {
648 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
649 /* Don't show pseudo exports */
650 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
652 e = lookup_or_create_elist_entry(&elist, exp);
654 /* exports to "*" absorb any others */
655 if (i == MCL_ANONYMOUS && e->ex_groups) {
656 remove_all_clients(e);
659 /* non-FQDN's absorb FQDN's they contain: */
660 if (i != MCL_FQDN && e->ex_groups)
661 prune_clients(exp, e);
663 if (exp->m_export.e_hostname[0] != '\0')
664 insert_group(e, exp->m_export.e_hostname);
672 main(int argc, char **argv)
674 char *export_file = _PATH_EXPORTS;
675 char *state_dir = NFS_STATEDIR;
683 /* Parse the command line options and arguments. */
685 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
691 descriptors = atoi(optarg);
692 if (descriptors <= 0) {
693 fprintf(stderr, "%s: bad descriptors: %s\n",
702 xlog_sconfig(optarg, 1);
705 export_file = optarg;
707 case 'H': /* PRC: specify a high-availability callout program */
708 ha_callout_prog = optarg;
713 case 'P': /* XXX for nfs-server compatibility */
716 if (port <= 0 || port > 65535) {
717 fprintf(stderr, "%s: bad port number: %s\n",
723 nfs_version &= ~(1 << (atoi (optarg) - 1));
726 _rpcfdtype = SOCK_DGRAM;
732 if ((state_dir = xstrdup(optarg)) == NULL) {
733 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
739 num_threads = atoi (optarg);
742 nfs_version |= 1 << (atoi (optarg) - 1);
745 printf("kmountd %s\n", VERSION);
754 /* No more arguments allowed.
755 * Require at least one valid version (2, 3, or 4)
757 if (optind != argc || !(nfs_version & 0xE))
760 if (chdir(state_dir)) {
761 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
762 argv [0], state_dir, strerror(errno));
766 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
767 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
768 argv [0], strerror(errno));
770 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
771 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
772 descriptors > FD_SETSIZE)
773 descriptors = FD_SETSIZE;
775 rlim.rlim_cur = descriptors;
776 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
777 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
778 argv [0], strerror(errno));
783 /* Initialize logging. */
784 if (!foreground) xlog_stderr(0);
787 sa.sa_handler = SIG_IGN;
789 sigemptyset(&sa.sa_mask);
790 sigaction(SIGHUP, &sa, NULL);
791 sigaction(SIGINT, &sa, NULL);
792 sigaction(SIGTERM, &sa, NULL);
793 sigaction(SIGPIPE, &sa, NULL);
794 /* WARNING: the following works on Linux and SysV, but not BSD! */
795 sigaction(SIGCHLD, &sa, NULL);
797 /* Daemons should close all extra filehandles ... *before* RPC init. */
801 new_cache = check_new_cache();
805 if (nfs_version & (0x1 << 1)) {
806 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
807 mount_dispatch, port);
808 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
809 mount_dispatch, port);
811 if (nfs_version & (0x1 << 2))
812 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
813 mount_dispatch, port);
815 sa.sa_handler = killer;
816 sigaction(SIGINT, &sa, NULL);
817 sigaction(SIGTERM, &sa, NULL);
818 sa.sa_handler = sig_hup;
819 sigaction(SIGHUP, &sa, NULL);
821 auth_init(export_file);
824 /* We first fork off a child. */
825 if ((c = fork()) > 0)
828 xlog(L_FATAL, "mountd: cannot fork: %s\n",
831 /* Now we remove ourselves from the foreground.
832 Redirect stdin/stdout/stderr first. */
834 int fd = open("/dev/null", O_RDWR);
838 if (fd > 2) (void) close(fd);
843 /* silently bounds check num_threads */
846 else if (num_threads < 1)
848 else if (num_threads > MAX_THREADS)
849 num_threads = MAX_THREADS;
856 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
861 usage(const char *prog, int n)
864 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
865 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
866 " [-p|--port port] [-V version|--nfs-version version]\n"
867 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
868 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
869 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);