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 *UNUSED(rqstp), void *UNUSED(argp),
202 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
204 struct nfs_fh_len *fh;
206 xlog(D_CALL, "MNT1(%s) called", *path);
207 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
209 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
214 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
216 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
218 xlog(D_CALL, "dump request from %s.", inet_ntoa(addr->sin_addr));
219 *res = mountlist_list();
225 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
227 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
230 char rpath[MAXPATHLEN+1];
235 if (realpath(p, rpath) != NULL) {
236 rpath[sizeof (rpath) - 1] = '\0';
240 if (!(exp = auth_authenticate("unmount", sin, p))) {
244 mountlist_del(inet_ntoa(sin->sin_addr), p);
249 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp),
252 /* Reload /etc/xtab if necessary */
255 mountlist_del_all(nfs_getrpccaller_in(rqstp->rq_xprt));
260 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
262 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
264 xlog(D_CALL, "export request from %s.", inet_ntoa(addr->sin_addr));
265 *resp = get_exportlist();
271 mount_exportall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
273 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
275 xlog(D_CALL, "exportall request from %s.", inet_ntoa(addr->sin_addr));
276 *resp = get_exportlist();
282 * MNTv2 pathconf procedure
284 * The protocol doesn't include a status field, so Sun apparently considers
285 * it good practice to let anyone snoop on your system, even if it's
286 * pretty harmless data such as pathconf. We don't.
288 * Besides, many of the pathconf values don't make much sense on NFS volumes.
289 * FIFOs and tty device files represent devices on the *client*, so there's
290 * no point in getting the server's buffer sizes etc.
293 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
295 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
298 char rpath[MAXPATHLEN+1];
301 memset(res, 0, sizeof(*res));
306 /* Reload /etc/xtab if necessary */
309 /* Resolve symlinks */
310 if (realpath(p, rpath) != NULL) {
311 rpath[sizeof (rpath) - 1] = '\0';
315 /* Now authenticate the intruder... */
316 exp = auth_authenticate("pathconf", sin, p);
319 } else if (stat(p, &stb) < 0) {
320 xlog(L_WARNING, "can't stat exported dir %s: %s",
325 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
326 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
327 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
328 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
329 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
330 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
331 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
333 /* Can't figure out what to do with pc_mask */
341 * We should advertise the preferred flavours first. (See RFC 2623
342 * section 2.7.) We leave that to the administrator, by advertising
343 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
344 * dropped from the list to avoid backward compatibility issue with
345 * older Linux clients, who inspect the list in reversed order.
347 * XXX: It might be more helpful to rearrange these so that flavors
348 * giving more access (as determined from readonly and id-squashing
349 * options) come first. (If we decide to do that we should probably do
350 * that when reading the exports rather than here.)
352 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
355 static int flavors[SECFLAVOR_COUNT];
358 for (s = exp->m_export.e_secinfo; s->flav; s++) {
359 if (s->flav->fnum == AUTH_NULL)
361 flavors[i] = s->flav->fnum;
365 /* default when there is no sec= option: */
367 flavors[0] = AUTH_UNIX;
369 ok->auth_flavors.auth_flavors_val = flavors;
370 ok->auth_flavors.auth_flavors_len = i;
374 * NFSv3 MOUNT procedure
377 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
379 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
381 struct nfs_fh_len *fh;
383 xlog(D_CALL, "MNT3(%s) called", *path);
384 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
388 ok->fhandle.fhandle3_len = fh->fh_size;
389 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
390 set_authflavors(ok, exp);
394 static struct nfs_fh_len *
395 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
396 mountstat3 *error, int v3)
398 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
399 struct stat stb, estb;
401 struct nfs_fh_len *fh;
402 char rpath[MAXPATHLEN+1];
408 /* Reload /var/lib/nfs/etab if necessary */
411 /* Resolve symlinks */
412 if (realpath(p, rpath) != NULL) {
413 rpath[sizeof (rpath) - 1] = '\0';
417 /* Now authenticate the intruder... */
418 exp = auth_authenticate("mount", sin, p);
420 *error = NFSERR_ACCES;
423 if (stat(p, &stb) < 0) {
424 xlog(L_WARNING, "can't stat exported dir %s: %s",
427 *error = NFSERR_NOENT;
429 *error = NFSERR_ACCES;
432 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
433 xlog(L_WARNING, "%s is not a directory or regular file", p);
434 *error = NFSERR_NOTDIR;
437 if (stat(exp->m_export.e_path, &estb) < 0) {
438 xlog(L_WARNING, "can't stat export point %s: %s",
440 *error = NFSERR_NOENT;
443 if (estb.st_dev != stb.st_dev
445 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
446 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
447 p, exp->m_export.e_path);
448 *error = NFSERR_ACCES;
451 if (exp->m_export.e_mountpoint &&
452 !is_mountpoint(exp->m_export.e_mountpoint[0]?
453 exp->m_export.e_mountpoint:
454 exp->m_export.e_path)) {
455 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
457 *error = NFSERR_NOENT;
462 /* This will be a static private nfs_export with just one
463 * address. We feed it to kernel then extract the filehandle,
467 if (cache_export(exp, p)) {
468 *error = NFSERR_ACCES;
471 fh = cache_get_filehandle(exp, v3?64:32, p);
473 *error = NFSERR_ACCES;
479 if (exp->m_exported<1) {
487 fh = getfh_size ((struct sockaddr *) sin, p, 64);
488 if (!v3 || (fh == NULL && errno == EINVAL)) {
489 /* We first try the new nfs syscall. */
490 fh = getfh ((struct sockaddr *) sin, p);
491 if (fh == NULL && errno == EINVAL)
492 /* Let's try the old one. */
493 fh = getfh_old ((struct sockaddr *) sin,
494 stb.st_dev, stb.st_ino);
496 if (fh == NULL && !did_export) {
502 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
503 *error = NFSERR_ACCES;
508 mountlist_add(inet_ntoa(sin->sin_addr), p);
514 static void remove_all_clients(exportnode *e)
516 struct groupnode *g, *ng;
518 for (g = e->ex_groups; g; g = ng) {
526 static void free_exportlist(exports *elist)
528 struct exportnode *e, *ne;
530 for (e = *elist; e != NULL; e = ne) {
532 remove_all_clients(e);
539 static void prune_clients(nfs_export *exp, struct exportnode *e)
541 struct addrinfo *ai = NULL;
542 struct groupnode *c, **cp;
545 while ((c = *cp) != NULL) {
546 if (client_gettype(c->gr_name) == MCL_FQDN
547 && (ai = host_addrinfo(c->gr_name))) {
548 if (client_check(exp->m_client, ai)) {
561 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
565 for (e = *elist; e != NULL; e = e->ex_next) {
566 if (!strcmp(exp->m_export.e_path, e->ex_dir))
569 e = xmalloc(sizeof(*e));
572 e->ex_dir = xstrdup(exp->m_export.e_path);
577 static void insert_group(struct exportnode *e, char *newname)
581 for (g = e->ex_groups; g; g = g->gr_next)
582 if (strcmp(g->gr_name, newname))
585 g = xmalloc(sizeof(*g));
586 g->gr_name = xstrdup(newname);
587 g->gr_next = e->ex_groups;
594 static exports elist = NULL;
595 struct exportnode *e;
598 static unsigned int ecounter;
599 unsigned int acounter;
601 acounter = auth_reload();
602 if (elist && acounter == ecounter)
607 free_exportlist(&elist);
609 for (i = 0; i < MCL_MAXTYPES; i++) {
610 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
611 /* Don't show pseudo exports */
612 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
614 e = lookup_or_create_elist_entry(&elist, exp);
616 /* exports to "*" absorb any others */
617 if (i == MCL_ANONYMOUS && e->ex_groups) {
618 remove_all_clients(e);
621 /* non-FQDN's absorb FQDN's they contain: */
622 if (i != MCL_FQDN && e->ex_groups)
623 prune_clients(exp, e);
625 if (exp->m_export.e_hostname[0] != '\0')
626 insert_group(e, exp->m_export.e_hostname);
634 main(int argc, char **argv)
636 char *export_file = _PATH_EXPORTS;
637 char *state_dir = NFS_STATEDIR;
645 /* Parse the command line options and arguments. */
647 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
653 descriptors = atoi(optarg);
654 if (descriptors <= 0) {
655 fprintf(stderr, "%s: bad descriptors: %s\n",
664 xlog_sconfig(optarg, 1);
667 export_file = optarg;
669 case 'H': /* PRC: specify a high-availability callout program */
670 ha_callout_prog = optarg;
675 case 'P': /* XXX for nfs-server compatibility */
678 if (port <= 0 || port > 65535) {
679 fprintf(stderr, "%s: bad port number: %s\n",
685 nfs_version &= ~(1 << (atoi (optarg) - 1));
688 _rpcfdtype = SOCK_DGRAM;
694 if ((state_dir = xstrdup(optarg)) == NULL) {
695 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
701 num_threads = atoi (optarg);
704 nfs_version |= 1 << (atoi (optarg) - 1);
707 printf("kmountd %s\n", VERSION);
716 /* No more arguments allowed.
717 * Require at least one valid version (2, 3, or 4)
719 if (optind != argc || !(nfs_version & 0xE))
722 if (chdir(state_dir)) {
723 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
724 argv [0], state_dir, strerror(errno));
728 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
729 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
730 argv [0], strerror(errno));
732 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
733 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
734 descriptors > FD_SETSIZE)
735 descriptors = FD_SETSIZE;
737 rlim.rlim_cur = descriptors;
738 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
739 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
740 argv [0], strerror(errno));
745 /* Initialize logging. */
746 if (!foreground) xlog_stderr(0);
749 sa.sa_handler = SIG_IGN;
751 sigemptyset(&sa.sa_mask);
752 sigaction(SIGHUP, &sa, NULL);
753 sigaction(SIGINT, &sa, NULL);
754 sigaction(SIGTERM, &sa, NULL);
755 sigaction(SIGPIPE, &sa, NULL);
756 /* WARNING: the following works on Linux and SysV, but not BSD! */
757 sigaction(SIGCHLD, &sa, NULL);
759 /* Daemons should close all extra filehandles ... *before* RPC init. */
763 new_cache = check_new_cache();
767 if (nfs_version & (0x1 << 1)) {
768 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
769 mount_dispatch, port);
770 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
771 mount_dispatch, port);
773 if (nfs_version & (0x1 << 2))
774 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
775 mount_dispatch, port);
777 sa.sa_handler = killer;
778 sigaction(SIGINT, &sa, NULL);
779 sigaction(SIGTERM, &sa, NULL);
780 sa.sa_handler = sig_hup;
781 sigaction(SIGHUP, &sa, NULL);
783 auth_init(export_file);
786 /* We first fork off a child. */
787 if ((c = fork()) > 0)
790 xlog(L_FATAL, "mountd: cannot fork: %s\n",
793 /* Now we remove ourselves from the foreground.
794 Redirect stdin/stdout/stderr first. */
796 int fd = open("/dev/null", O_RDWR);
800 if (fd > 2) (void) close(fd);
805 /* silently bounds check num_threads */
808 else if (num_threads < 1)
810 else if (num_threads > MAX_THREADS)
811 num_threads = MAX_THREADS;
818 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
823 usage(const char *prog, int n)
826 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
827 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
828 " [-p|--port port] [-V version|--nfs-version version]\n"
829 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
830 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
831 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);