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);
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 *argp, void *resp)
201 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
203 struct nfs_fh_len *fh;
205 xlog(D_CALL, "MNT1(%s) called", *path);
206 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
208 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
213 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
215 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
217 xlog(D_CALL, "dump request from %s.", inet_ntoa(addr->sin_addr));
218 *res = mountlist_list();
224 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
226 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
229 char rpath[MAXPATHLEN+1];
234 if (realpath(p, rpath) != NULL) {
235 rpath[sizeof (rpath) - 1] = '\0';
239 if (!(exp = auth_authenticate("unmount", sin, p))) {
243 mountlist_del(inet_ntoa(sin->sin_addr), p);
248 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
250 /* Reload /etc/xtab if necessary */
253 mountlist_del_all(nfs_getrpccaller_in(rqstp->rq_xprt));
258 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
260 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
262 xlog(D_CALL, "export request from %s.", inet_ntoa(addr->sin_addr));
263 *resp = get_exportlist();
269 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
271 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
273 xlog(D_CALL, "exportall request from %s.", inet_ntoa(addr->sin_addr));
274 *resp = get_exportlist();
280 * MNTv2 pathconf procedure
282 * The protocol doesn't include a status field, so Sun apparently considers
283 * it good practice to let anyone snoop on your system, even if it's
284 * pretty harmless data such as pathconf. We don't.
286 * Besides, many of the pathconf values don't make much sense on NFS volumes.
287 * FIFOs and tty device files represent devices on the *client*, so there's
288 * no point in getting the server's buffer sizes etc.
291 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
293 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
296 char rpath[MAXPATHLEN+1];
299 memset(res, 0, sizeof(*res));
304 /* Reload /etc/xtab if necessary */
307 /* Resolve symlinks */
308 if (realpath(p, rpath) != NULL) {
309 rpath[sizeof (rpath) - 1] = '\0';
313 /* Now authenticate the intruder... */
314 exp = auth_authenticate("pathconf", sin, p);
317 } else if (stat(p, &stb) < 0) {
318 xlog(L_WARNING, "can't stat exported dir %s: %s",
323 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
324 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
325 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
326 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
327 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
328 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
329 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
331 /* Can't figure out what to do with pc_mask */
339 * We should advertise the preferred flavours first. (See RFC 2623
340 * section 2.7.) We leave that to the administrator, by advertising
341 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
342 * dropped from the list to avoid backward compatibility issue with
343 * older Linux clients, who inspect the list in reversed order.
345 * XXX: It might be more helpful to rearrange these so that flavors
346 * giving more access (as determined from readonly and id-squashing
347 * options) come first. (If we decide to do that we should probably do
348 * that when reading the exports rather than here.)
350 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
353 static int flavors[SECFLAVOR_COUNT];
356 for (s = exp->m_export.e_secinfo; s->flav; s++) {
357 if (s->flav->fnum == AUTH_NULL)
359 flavors[i] = s->flav->fnum;
362 ok->auth_flavors.auth_flavors_val = flavors;
363 ok->auth_flavors.auth_flavors_len = i;
367 * NFSv3 MOUNT procedure
370 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
372 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
374 struct nfs_fh_len *fh;
376 xlog(D_CALL, "MNT3(%s) called", *path);
377 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
381 ok->fhandle.fhandle3_len = fh->fh_size;
382 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
383 set_authflavors(ok, exp);
387 static struct nfs_fh_len *
388 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
389 mountstat3 *error, int v3)
391 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
392 struct stat stb, estb;
394 struct nfs_fh_len *fh;
395 char rpath[MAXPATHLEN+1];
401 /* Reload /var/lib/nfs/etab if necessary */
404 /* Resolve symlinks */
405 if (realpath(p, rpath) != NULL) {
406 rpath[sizeof (rpath) - 1] = '\0';
410 /* Now authenticate the intruder... */
411 exp = auth_authenticate("mount", sin, p);
413 *error = NFSERR_ACCES;
416 if (stat(p, &stb) < 0) {
417 xlog(L_WARNING, "can't stat exported dir %s: %s",
420 *error = NFSERR_NOENT;
422 *error = NFSERR_ACCES;
425 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
426 xlog(L_WARNING, "%s is not a directory or regular file", p);
427 *error = NFSERR_NOTDIR;
430 if (stat(exp->m_export.e_path, &estb) < 0) {
431 xlog(L_WARNING, "can't stat export point %s: %s",
433 *error = NFSERR_NOENT;
436 if (estb.st_dev != stb.st_dev
438 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
439 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
440 p, exp->m_export.e_path);
441 *error = NFSERR_ACCES;
444 if (exp->m_export.e_mountpoint &&
445 !is_mountpoint(exp->m_export.e_mountpoint[0]?
446 exp->m_export.e_mountpoint:
447 exp->m_export.e_path)) {
448 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
450 *error = NFSERR_NOENT;
455 /* This will be a static private nfs_export with just one
456 * address. We feed it to kernel then extract the filehandle,
460 if (cache_export(exp, p)) {
461 *error = NFSERR_ACCES;
464 fh = cache_get_filehandle(exp, v3?64:32, p);
466 *error = NFSERR_ACCES;
470 if (exp->m_exported<1)
476 fh = getfh_size ((struct sockaddr *) sin, p, 64);
477 if (!v3 || (fh == NULL && errno == EINVAL)) {
478 /* We first try the new nfs syscall. */
479 fh = getfh ((struct sockaddr *) sin, p);
480 if (fh == NULL && errno == EINVAL)
481 /* Let's try the old one. */
482 fh = getfh_old ((struct sockaddr *) sin,
483 stb.st_dev, stb.st_ino);
486 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
487 *error = NFSERR_ACCES;
492 mountlist_add(inet_ntoa(sin->sin_addr), p);
501 static exports elist = NULL;
502 struct exportnode *e, *ne;
503 struct groupnode *g, *ng, *c, **cp;
506 static unsigned int ecounter;
507 unsigned int acounter;
509 acounter = auth_reload();
510 if (elist && acounter == ecounter)
515 for (e = elist; e != NULL; e = ne) {
517 for (g = e->ex_groups; g != NULL; g = ng) {
527 for (i = 0; i < MCL_MAXTYPES; i++) {
528 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
529 for (e = elist; e != NULL; e = e->ex_next) {
530 if (!strcmp(exp->m_export.e_path, e->ex_dir))
534 e = (struct exportnode *) xmalloc(sizeof(*e));
537 e->ex_dir = xstrdup(exp->m_export.e_path);
541 /* We need to check if we should remove
543 if (i == MCL_ANONYMOUS && e->ex_groups) {
544 for (g = e->ex_groups; g; g = ng) {
553 if (i != MCL_FQDN && e->ex_groups) {
557 while ((c = *cp) != NULL) {
558 if (client_gettype (c->gr_name) == MCL_FQDN
559 && (hp = gethostbyname(c->gr_name))) {
560 hp = hostent_dup (hp);
561 if (client_check(exp->m_client, hp)) {
574 if (exp->m_export.e_hostname [0] != '\0') {
575 for (g = e->ex_groups; g; g = g->gr_next)
576 if (strcmp (exp->m_export.e_hostname,
581 g = (struct groupnode *) xmalloc(sizeof(*g));
582 g->gr_name = xstrdup(exp->m_export.e_hostname);
583 g->gr_next = e->ex_groups;
593 main(int argc, char **argv)
595 char *export_file = _PATH_EXPORTS;
596 char *state_dir = NFS_STATEDIR;
604 /* Parse the command line options and arguments. */
606 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
612 descriptors = atoi(optarg);
613 if (descriptors <= 0) {
614 fprintf(stderr, "%s: bad descriptors: %s\n",
623 xlog_sconfig(optarg, 1);
626 export_file = optarg;
628 case 'H': /* PRC: specify a high-availability callout program */
629 ha_callout_prog = optarg;
634 case 'P': /* XXX for nfs-server compatibility */
637 if (port <= 0 || port > 65535) {
638 fprintf(stderr, "%s: bad port number: %s\n",
644 nfs_version &= ~(1 << (atoi (optarg) - 1));
647 _rpcfdtype = SOCK_DGRAM;
653 if ((state_dir = xstrdup(optarg)) == NULL) {
654 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
660 num_threads = atoi (optarg);
663 nfs_version |= 1 << (atoi (optarg) - 1);
666 printf("kmountd %s\n", VERSION);
675 /* No more arguments allowed. */
676 if (optind != argc || !(nfs_version & 0x7))
679 if (chdir(state_dir)) {
680 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
681 argv [0], state_dir, strerror(errno));
685 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
686 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
687 argv [0], strerror(errno));
689 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
690 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
691 descriptors > FD_SETSIZE)
692 descriptors = FD_SETSIZE;
694 rlim.rlim_cur = descriptors;
695 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
696 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
697 argv [0], strerror(errno));
702 /* Initialize logging. */
703 if (!foreground) xlog_stderr(0);
706 sa.sa_handler = SIG_IGN;
708 sigemptyset(&sa.sa_mask);
709 sigaction(SIGHUP, &sa, NULL);
710 sigaction(SIGINT, &sa, NULL);
711 sigaction(SIGTERM, &sa, NULL);
712 sigaction(SIGPIPE, &sa, NULL);
713 /* WARNING: the following works on Linux and SysV, but not BSD! */
714 sigaction(SIGCHLD, &sa, NULL);
716 /* Daemons should close all extra filehandles ... *before* RPC init. */
720 new_cache = check_new_cache();
724 if (nfs_version & 0x1)
725 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
726 mount_dispatch, port);
727 if (nfs_version & (0x1 << 1))
728 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
729 mount_dispatch, port);
730 if (nfs_version & (0x1 << 2))
731 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
732 mount_dispatch, port);
734 sa.sa_handler = killer;
735 sigaction(SIGINT, &sa, NULL);
736 sigaction(SIGTERM, &sa, NULL);
737 sa.sa_handler = sig_hup;
738 sigaction(SIGHUP, &sa, NULL);
740 auth_init(export_file);
743 /* We first fork off a child. */
744 if ((c = fork()) > 0)
747 xlog(L_FATAL, "mountd: cannot fork: %s\n",
750 /* Now we remove ourselves from the foreground.
751 Redirect stdin/stdout/stderr first. */
753 int fd = open("/dev/null", O_RDWR);
757 if (fd > 2) (void) close(fd);
762 /* silently bounds check num_threads */
765 else if (num_threads < 1)
767 else if (num_threads > MAX_THREADS)
768 num_threads = MAX_THREADS;
775 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
780 usage(const char *prog, int n)
783 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
784 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
785 " [-p|--port port] [-V version|--nfs-version version]\n"
786 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
787 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
788 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);