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)
84 pmap_unset (MOUNTPROG, MOUNTVERS);
85 if (nfs_version & (0x1 << 1))
86 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
87 if (nfs_version & (0x1 << 2))
88 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
91 /* Wait for all worker child processes to exit and reap them */
93 wait_for_workers (void)
100 pid = waitpid(0, &status, 0);
104 return; /* no more children */
105 xlog(L_FATAL, "mountd: can't wait: %s\n",
109 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
110 * does not happen on 2.6 kernels, and waitpid() blocks
111 * until all the children are dead then returns with
112 * -ECHILD. But, we don't need to do anything on the
113 * death of individual workers, so we don't care. */
114 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
119 /* Fork num_threads worker children and wait for them */
126 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
128 for (i = 0 ; i < num_threads ; i++) {
131 xlog(L_FATAL, "mountd: cannot fork: %s\n",
137 /* Re-enable the default action on SIGTERM et al
138 * so that workers die naturally when sent them.
139 * Only the parent unregisters with pmap and
140 * hence needs to do special SIGTERM handling. */
142 sa.sa_handler = SIG_DFL;
144 sigemptyset(&sa.sa_mask);
145 sigaction(SIGHUP, &sa, NULL);
146 sigaction(SIGINT, &sa, NULL);
147 sigaction(SIGTERM, &sa, NULL);
149 /* fall into my_svc_run in caller */
156 unregister_services();
157 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
167 unregister_services();
168 if (num_threads > 1) {
169 /* play Kronos and eat our children */
173 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
179 /* don't exit on SIGHUP */
180 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
185 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
191 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
193 struct nfs_fh_len *fh;
195 xlog(D_CALL, "MNT1(%s) called", *path);
196 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
198 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
203 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
205 struct sockaddr_in *addr =
206 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
208 xlog(D_CALL, "dump request from %s.", inet_ntoa(addr->sin_addr));
209 *res = mountlist_list();
215 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
217 struct sockaddr_in *sin
218 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
221 char rpath[MAXPATHLEN+1];
226 if (realpath(p, rpath) != NULL) {
227 rpath[sizeof (rpath) - 1] = '\0';
231 if (!(exp = auth_authenticate("unmount", sin, p))) {
235 mountlist_del(inet_ntoa(sin->sin_addr), p);
240 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
242 /* Reload /etc/xtab if necessary */
245 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
250 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
252 struct sockaddr_in *addr =
253 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
255 xlog(D_CALL, "export request from %s.", inet_ntoa(addr->sin_addr));
256 *resp = get_exportlist();
262 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
264 struct sockaddr_in *addr =
265 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
267 xlog(D_CALL, "exportall request from %s.", inet_ntoa(addr->sin_addr));
268 *resp = get_exportlist();
274 * MNTv2 pathconf procedure
276 * The protocol doesn't include a status field, so Sun apparently considers
277 * it good practice to let anyone snoop on your system, even if it's
278 * pretty harmless data such as pathconf. We don't.
280 * Besides, many of the pathconf values don't make much sense on NFS volumes.
281 * FIFOs and tty device files represent devices on the *client*, so there's
282 * no point in getting the server's buffer sizes etc.
285 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
287 struct sockaddr_in *sin
288 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
291 char rpath[MAXPATHLEN+1];
294 memset(res, 0, sizeof(*res));
299 /* Reload /etc/xtab if necessary */
302 /* Resolve symlinks */
303 if (realpath(p, rpath) != NULL) {
304 rpath[sizeof (rpath) - 1] = '\0';
308 /* Now authenticate the intruder... */
309 exp = auth_authenticate("pathconf", sin, p);
312 } else if (stat(p, &stb) < 0) {
313 xlog(L_WARNING, "can't stat exported dir %s: %s",
318 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
319 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
320 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
321 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
322 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
323 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
324 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
326 /* Can't figure out what to do with pc_mask */
334 * We should advertise the preferred flavours first. (See RFC 2623
335 * section 2.7.) We leave that to the administrator, by advertising
336 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
337 * dropped from the list to avoid backward compatibility issue with
338 * older Linux clients, who inspect the list in reversed order.
340 * XXX: It might be more helpful to rearrange these so that flavors
341 * giving more access (as determined from readonly and id-squashing
342 * options) come first. (If we decide to do that we should probably do
343 * that when reading the exports rather than here.)
345 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
348 static int flavors[SECFLAVOR_COUNT];
351 for (s = exp->m_export.e_secinfo; s->flav; s++) {
352 if (s->flav->fnum == AUTH_NULL)
354 flavors[i] = s->flav->fnum;
357 ok->auth_flavors.auth_flavors_val = flavors;
358 ok->auth_flavors.auth_flavors_len = i;
362 * NFSv3 MOUNT procedure
365 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
367 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
369 struct nfs_fh_len *fh;
371 xlog(D_CALL, "MNT3(%s) called", *path);
372 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
376 ok->fhandle.fhandle3_len = fh->fh_size;
377 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
378 set_authflavors(ok, exp);
382 static struct nfs_fh_len *
383 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
384 mountstat3 *error, int v3)
386 struct sockaddr_in *sin =
387 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
388 struct stat stb, estb;
390 struct nfs_fh_len *fh;
391 char rpath[MAXPATHLEN+1];
397 /* Reload /var/lib/nfs/etab if necessary */
400 /* Resolve symlinks */
401 if (realpath(p, rpath) != NULL) {
402 rpath[sizeof (rpath) - 1] = '\0';
406 /* Now authenticate the intruder... */
407 exp = auth_authenticate("mount", sin, p);
409 *error = NFSERR_ACCES;
412 if (stat(p, &stb) < 0) {
413 xlog(L_WARNING, "can't stat exported dir %s: %s",
416 *error = NFSERR_NOENT;
418 *error = NFSERR_ACCES;
421 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
422 xlog(L_WARNING, "%s is not a directory or regular file", p);
423 *error = NFSERR_NOTDIR;
426 if (stat(exp->m_export.e_path, &estb) < 0) {
427 xlog(L_WARNING, "can't stat export point %s: %s",
429 *error = NFSERR_NOENT;
432 if (estb.st_dev != stb.st_dev
434 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
435 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
436 p, exp->m_export.e_path);
437 *error = NFSERR_ACCES;
440 if (exp->m_export.e_mountpoint &&
441 !is_mountpoint(exp->m_export.e_mountpoint[0]?
442 exp->m_export.e_mountpoint:
443 exp->m_export.e_path)) {
444 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
446 *error = NFSERR_NOENT;
451 /* This will be a static private nfs_export with just one
452 * address. We feed it to kernel then extract the filehandle,
456 if (cache_export(exp, p)) {
457 *error = NFSERR_ACCES;
460 fh = cache_get_filehandle(exp, v3?64:32, p);
462 *error = NFSERR_ACCES;
466 if (exp->m_exported<1)
472 fh = getfh_size ((struct sockaddr *) sin, p, 64);
473 if (!v3 || (fh == NULL && errno == EINVAL)) {
474 /* We first try the new nfs syscall. */
475 fh = getfh ((struct sockaddr *) sin, p);
476 if (fh == NULL && errno == EINVAL)
477 /* Let's try the old one. */
478 fh = getfh_old ((struct sockaddr *) sin,
479 stb.st_dev, stb.st_ino);
482 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
483 *error = NFSERR_ACCES;
488 mountlist_add(inet_ntoa(sin->sin_addr), p);
497 static exports elist = NULL;
498 struct exportnode *e, *ne;
499 struct groupnode *g, *ng, *c, **cp;
502 static unsigned int ecounter;
503 unsigned int acounter;
505 acounter = auth_reload();
506 if (elist && acounter == ecounter)
511 for (e = elist; e != NULL; e = ne) {
513 for (g = e->ex_groups; g != NULL; g = ng) {
523 for (i = 0; i < MCL_MAXTYPES; i++) {
524 for (exp = exportlist[i]; exp; exp = exp->m_next) {
525 for (e = elist; e != NULL; e = e->ex_next) {
526 if (!strcmp(exp->m_export.e_path, e->ex_dir))
530 e = (struct exportnode *) xmalloc(sizeof(*e));
533 e->ex_dir = xstrdup(exp->m_export.e_path);
537 /* We need to check if we should remove
539 if (i == MCL_ANONYMOUS && e->ex_groups) {
540 for (g = e->ex_groups; g; g = ng) {
549 if (i != MCL_FQDN && e->ex_groups) {
553 while ((c = *cp) != NULL) {
554 if (client_gettype (c->gr_name) == MCL_FQDN
555 && (hp = gethostbyname(c->gr_name))) {
556 hp = hostent_dup (hp);
557 if (client_check(exp->m_client, hp)) {
570 if (exp->m_export.e_hostname [0] != '\0') {
571 for (g = e->ex_groups; g; g = g->gr_next)
572 if (strcmp (exp->m_export.e_hostname,
577 g = (struct groupnode *) xmalloc(sizeof(*g));
578 g->gr_name = xstrdup(exp->m_export.e_hostname);
579 g->gr_next = e->ex_groups;
589 main(int argc, char **argv)
591 char *export_file = _PATH_EXPORTS;
592 char *state_dir = NFS_STATEDIR;
600 /* Parse the command line options and arguments. */
602 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
608 descriptors = atoi(optarg);
609 if (descriptors <= 0) {
610 fprintf(stderr, "%s: bad descriptors: %s\n",
619 xlog_sconfig(optarg, 1);
622 export_file = optarg;
624 case 'H': /* PRC: specify a high-availability callout program */
625 ha_callout_prog = optarg;
630 case 'P': /* XXX for nfs-server compatibility */
633 if (port <= 0 || port > 65535) {
634 fprintf(stderr, "%s: bad port number: %s\n",
640 nfs_version &= ~(1 << (atoi (optarg) - 1));
643 _rpcfdtype = SOCK_DGRAM;
649 if ((state_dir = xstrdup(optarg)) == NULL) {
650 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
656 num_threads = atoi (optarg);
659 nfs_version |= 1 << (atoi (optarg) - 1);
662 printf("kmountd %s\n", VERSION);
671 /* No more arguments allowed. */
672 if (optind != argc || !(nfs_version & 0x7))
675 if (chdir(state_dir)) {
676 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
677 argv [0], state_dir, strerror(errno));
681 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
682 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
683 argv [0], strerror(errno));
685 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
686 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
687 descriptors > FD_SETSIZE)
688 descriptors = FD_SETSIZE;
690 rlim.rlim_cur = descriptors;
691 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
692 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
693 argv [0], strerror(errno));
698 /* Initialize logging. */
699 if (!foreground) xlog_stderr(0);
702 sa.sa_handler = SIG_IGN;
704 sigemptyset(&sa.sa_mask);
705 sigaction(SIGHUP, &sa, NULL);
706 sigaction(SIGINT, &sa, NULL);
707 sigaction(SIGTERM, &sa, NULL);
708 sigaction(SIGPIPE, &sa, NULL);
709 /* WARNING: the following works on Linux and SysV, but not BSD! */
710 sigaction(SIGCHLD, &sa, NULL);
712 /* Daemons should close all extra filehandles ... *before* RPC init. */
716 new_cache = check_new_cache();
720 if (nfs_version & 0x1)
721 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
722 mount_dispatch, port);
723 if (nfs_version & (0x1 << 1))
724 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
725 mount_dispatch, port);
726 if (nfs_version & (0x1 << 2))
727 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
728 mount_dispatch, port);
730 sa.sa_handler = killer;
731 sigaction(SIGINT, &sa, NULL);
732 sigaction(SIGTERM, &sa, NULL);
733 sa.sa_handler = sig_hup;
734 sigaction(SIGHUP, &sa, NULL);
736 auth_init(export_file);
739 /* We first fork off a child. */
740 if ((c = fork()) > 0)
743 xlog(L_FATAL, "mountd: cannot fork: %s\n",
746 /* Now we remove ourselves from the foreground.
747 Redirect stdin/stdout/stderr first. */
749 int fd = open("/dev/null", O_RDWR);
753 if (fd > 2) (void) close(fd);
758 /* silently bounds check num_threads */
761 else if (num_threads < 1)
763 else if (num_threads > MAX_THREADS)
764 num_threads = MAX_THREADS;
771 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
776 usage(const char *prog, int n)
779 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
780 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
781 " [-p|--port port] [-V version|--nfs-version version]\n"
782 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
783 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
784 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);