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>
30 extern void cache_open(void);
31 extern struct nfs_fh_len *cache_get_filehandle(nfs_export *exp, int len, char *p);
32 extern void cache_export(nfs_export *exp);
34 extern void my_svc_run(void);
36 static void usage(const char *, int exitcode);
37 static exports get_exportlist(void);
38 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, mountstat3 *, int v3);
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' },
72 static int nfs_version = -1;
75 unregister_services (void)
77 if (nfs_version & 0x1)
78 pmap_unset (MOUNTPROG, MOUNTVERS);
79 if (nfs_version & (0x1 << 1))
80 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
81 if (nfs_version & (0x1 << 2))
82 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
85 /* Wait for all worker child processes to exit and reap them */
87 wait_for_workers (void)
94 pid = waitpid(0, &status, 0);
98 return; /* no more children */
99 xlog(L_FATAL, "mountd: can't wait: %s\n",
103 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
104 * does not happen on 2.6 kernels, and waitpid() blocks
105 * until all the children are dead then returns with
106 * -ECHILD. But, we don't need to do anything on the
107 * death of individual workers, so we don't care. */
108 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
113 /* Fork num_threads worker children and wait for them */
120 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
122 for (i = 0 ; i < num_threads ; i++) {
125 xlog(L_FATAL, "mountd: cannot fork: %s\n",
131 /* Re-enable the default action on SIGTERM et al
132 * so that workers die naturally when sent them.
133 * Only the parent unregisters with pmap and
134 * hence needs to do special SIGTERM handling. */
136 sa.sa_handler = SIG_DFL;
138 sigemptyset(&sa.sa_mask);
139 sigaction(SIGHUP, &sa, NULL);
140 sigaction(SIGINT, &sa, NULL);
141 sigaction(SIGTERM, &sa, NULL);
143 /* fall into my_svc_run in caller */
150 unregister_services();
151 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
161 unregister_services();
162 if (num_threads > 1) {
163 /* play Kronos and eat our children */
167 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
173 /* don't exit on SIGHUP */
174 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
179 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
185 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
187 struct nfs_fh_len *fh;
189 xlog(D_CALL, "MNT1(%s) called", *path);
190 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
191 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
196 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
198 struct sockaddr_in *addr =
199 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
201 if ((*res = mountlist_list()) == NULL)
202 xlog(L_WARNING, "dump request from %s failed.",
203 inet_ntoa(addr->sin_addr));
209 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
211 struct sockaddr_in *sin
212 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
215 char rpath[MAXPATHLEN+1];
220 if (realpath(p, rpath) != NULL) {
221 rpath[sizeof (rpath) - 1] = '\0';
225 if (!(exp = auth_authenticate("unmount", sin, p))) {
232 mountlist_del(inet_ntoa(sin->sin_addr), p);
237 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
239 /* Reload /etc/xtab if necessary */
242 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
247 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
249 struct sockaddr_in *addr =
250 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
252 if ((*resp = get_exportlist()) == NULL)
253 xlog(L_WARNING, "export request from %s failed.",
254 inet_ntoa(addr->sin_addr));
260 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
262 struct sockaddr_in *addr =
263 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
265 if ((*resp = get_exportlist()) == NULL)
266 xlog(L_WARNING, "exportall request from %s failed.",
267 inet_ntoa(addr->sin_addr));
272 * MNTv2 pathconf procedure
274 * The protocol doesn't include a status field, so Sun apparently considers
275 * it good practice to let anyone snoop on your system, even if it's
276 * pretty harmless data such as pathconf. We don't.
278 * Besides, many of the pathconf values don't make much sense on NFS volumes.
279 * FIFOs and tty device files represent devices on the *client*, so there's
280 * no point in getting the server's buffer sizes etc.
283 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
285 struct sockaddr_in *sin
286 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
289 char rpath[MAXPATHLEN+1];
292 memset(res, 0, sizeof(*res));
297 /* Reload /etc/xtab if necessary */
300 /* Resolve symlinks */
301 if (realpath(p, rpath) != NULL) {
302 rpath[sizeof (rpath) - 1] = '\0';
306 /* Now authenticate the intruder... */
307 if (!(exp = auth_authenticate("pathconf", sin, p))) {
309 } else if (stat(p, &stb) < 0) {
310 xlog(L_WARNING, "can't stat exported dir %s: %s",
316 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
317 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
318 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
319 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
320 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
321 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
322 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
324 /* Can't figure out what to do with pc_mask */
334 * NFSv3 MOUNT procedure
337 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
339 #define AUTH_GSS_KRB5 390003
340 #define AUTH_GSS_KRB5I 390004
341 #define AUTH_GSS_KRB5P 390005
342 static int flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
343 struct nfs_fh_len *fh;
345 xlog(D_CALL, "MNT3(%s) called", *path);
346 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 1)) != NULL) {
347 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
349 ok->fhandle.fhandle3_len = fh->fh_size;
350 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
351 ok->auth_flavors.auth_flavors_len
352 = sizeof(flavors)/sizeof(flavors[0]);
353 ok->auth_flavors.auth_flavors_val = flavors;
358 static struct nfs_fh_len *
359 get_rootfh(struct svc_req *rqstp, dirpath *path, mountstat3 *error, int v3)
361 struct sockaddr_in *sin =
362 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
363 struct stat stb, estb;
365 char rpath[MAXPATHLEN+1];
371 /* Reload /var/lib/nfs/etab if necessary */
374 /* Resolve symlinks */
375 if (realpath(p, rpath) != NULL) {
376 rpath[sizeof (rpath) - 1] = '\0';
380 /* Now authenticate the intruder... */
381 if (!(exp = auth_authenticate("mount", sin, p))) {
382 *error = NFSERR_ACCES;
383 } else if (stat(p, &stb) < 0) {
384 xlog(L_WARNING, "can't stat exported dir %s: %s",
387 *error = NFSERR_NOENT;
389 *error = NFSERR_ACCES;
390 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
391 xlog(L_WARNING, "%s is not a directory or regular file", p);
392 *error = NFSERR_NOTDIR;
393 } else if (stat(exp->m_export.e_path, &estb) < 0) {
394 xlog(L_WARNING, "can't stat export point %s: %s",
396 *error = NFSERR_NOENT;
397 } else if (estb.st_dev != stb.st_dev
398 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
400 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
401 p, exp->m_export.e_path);
402 *error = NFSERR_ACCES;
403 } else if (exp->m_export.e_mountpoint &&
404 !is_mountpoint(exp->m_export.e_mountpoint[0]?
405 exp->m_export.e_mountpoint:
406 exp->m_export.e_path)) {
407 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
409 *error = NFSERR_NOENT;
410 } else if (new_cache) {
411 /* This will be a static private nfs_export with just one
412 * address. We feed it to kernel then extract the filehandle,
415 struct nfs_fh_len *fh;
418 fh = cache_get_filehandle(exp, v3?64:32, p);
420 *error = NFSERR_ACCES;
423 mountlist_add(inet_ntoa(sin->sin_addr), p);
427 struct nfs_fh_len *fh;
429 if (exp->m_exported<1)
435 fh = getfh_size ((struct sockaddr *) sin, p, 64);
436 if (!v3 || (fh == NULL && errno == EINVAL)) {
437 /* We first try the new nfs syscall. */
438 fh = getfh ((struct sockaddr *) sin, p);
439 if (fh == NULL && errno == EINVAL)
440 /* Let's try the old one. */
441 fh = getfh_old ((struct sockaddr *) sin,
442 stb.st_dev, stb.st_ino);
445 mountlist_add(inet_ntoa(sin->sin_addr), p);
450 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
451 *error = NFSERR_ACCES;
460 static exports elist = NULL;
461 struct exportnode *e, *ne;
462 struct groupnode *g, *ng, *c, **cp;
466 if (!auth_reload() && elist)
469 for (e = elist; e != NULL; e = ne) {
471 for (g = e->ex_groups; g != NULL; g = ng) {
481 for (i = 0; i < MCL_MAXTYPES; i++) {
482 for (exp = exportlist[i]; exp; exp = exp->m_next) {
483 for (e = elist; e != NULL; e = e->ex_next) {
484 if (!strcmp(exp->m_export.m_path, e->ex_dir))
488 e = (struct exportnode *) xmalloc(sizeof(*e));
491 e->ex_dir = xstrdup(exp->m_export.m_path);
495 /* We need to check if we should remove
497 if (i == MCL_ANONYMOUS && e->ex_groups) {
498 for (g = e->ex_groups; g; g = ng) {
507 if (i != MCL_FQDN && e->ex_groups) {
511 while ((c = *cp) != NULL) {
512 if (client_gettype (c->gr_name) == MCL_FQDN
513 && (hp = gethostbyname(c->gr_name))) {
514 hp = hostent_dup (hp);
515 if (client_check(exp->m_client, hp)) {
528 if (exp->m_export.e_hostname [0] != '\0') {
529 for (g = e->ex_groups; g; g = g->gr_next)
530 if (strcmp (exp->m_export.e_hostname,
535 g = (struct groupnode *) xmalloc(sizeof(*g));
536 g->gr_name = xstrdup(exp->m_export.e_hostname);
537 g->gr_next = e->ex_groups;
547 main(int argc, char **argv)
549 char *export_file = _PATH_EXPORTS;
550 char *state_dir = NFS_STATEDIR;
558 /* Parse the command line options and arguments. */
560 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vs:t:", longopts, NULL)) != EOF)
563 descriptors = atoi(optarg);
564 if (descriptors <= 0) {
565 fprintf(stderr, "%s: bad descriptors: %s\n",
574 xlog_sconfig(optarg, 1);
577 export_file = optarg;
579 case 'H': /* PRC: specify a high-availability callout program */
580 ha_callout_prog = optarg;
585 case 'P': /* XXX for nfs-server compatibility */
588 if (port <= 0 || port > 65535) {
589 fprintf(stderr, "%s: bad port number: %s\n",
595 nfs_version &= ~(1 << (atoi (optarg) - 1));
598 _rpcfdtype = SOCK_DGRAM;
601 if ((state_dir = xstrdup(optarg)) == NULL) {
602 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
608 num_threads = atoi (optarg);
611 nfs_version |= 1 << (atoi (optarg) - 1);
614 printf("kmountd %s\n", VERSION);
623 /* No more arguments allowed. */
624 if (optind != argc || !(nfs_version & 0x7))
627 if (chdir(state_dir)) {
628 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
629 argv [0], state_dir, strerror(errno));
633 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
634 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
635 argv [0], strerror(errno));
637 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
638 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
639 descriptors > FD_SETSIZE)
640 descriptors = FD_SETSIZE;
642 rlim.rlim_cur = descriptors;
643 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
644 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
645 argv [0], strerror(errno));
650 /* Initialize logging. */
651 if (!foreground) xlog_stderr(0);
654 sa.sa_handler = SIG_IGN;
656 sigemptyset(&sa.sa_mask);
657 sigaction(SIGHUP, &sa, NULL);
658 sigaction(SIGINT, &sa, NULL);
659 sigaction(SIGTERM, &sa, NULL);
660 sigaction(SIGPIPE, &sa, NULL);
661 /* WARNING: the following works on Linux and SysV, but not BSD! */
662 sigaction(SIGCHLD, &sa, NULL);
664 /* Daemons should close all extra filehandles ... *before* RPC init. */
668 new_cache = check_new_cache();
672 if (nfs_version & 0x1)
673 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
674 mount_dispatch, port);
675 if (nfs_version & (0x1 << 1))
676 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
677 mount_dispatch, port);
678 if (nfs_version & (0x1 << 2))
679 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
680 mount_dispatch, port);
682 sa.sa_handler = killer;
683 sigaction(SIGINT, &sa, NULL);
684 sigaction(SIGTERM, &sa, NULL);
685 sa.sa_handler = sig_hup;
686 sigaction(SIGHUP, &sa, NULL);
688 auth_init(export_file);
691 /* We first fork off a child. */
692 if ((c = fork()) > 0)
695 xlog(L_FATAL, "mountd: cannot fork: %s\n",
698 /* Now we remove ourselves from the foreground.
699 Redirect stdin/stdout/stderr first. */
701 int fd = open("/dev/null", O_RDWR);
705 if (fd > 2) (void) close(fd);
710 /* silently bounds check num_threads */
713 else if (num_threads < 1)
715 else if (num_threads > MAX_THREADS)
716 num_threads = MAX_THREADS;
723 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
728 usage(const char *prog, int n)
731 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
732 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
733 " [-p|--port port] [-V version|--nfs-version version]\n"
734 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
735 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
736 " [-t num|--num-threads=num]\n", prog);