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);
40 int reverse_resolve = 0;
43 /* PRC: a high-availability callout program can be specified with -H
44 * When this is done, the program will receive callouts whenever clients
45 * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
46 char *ha_callout_prog = NULL;
48 /* Number of mountd threads to start. Default is 1 and
49 * that's probably enough unless you need hundreds of
50 * clients to be able to mount at once. */
51 static int num_threads = 1;
52 /* Arbitrary limit on number of threads */
53 #define MAX_THREADS 64
55 static struct option longopts[] =
57 { "foreground", 0, 0, 'F' },
58 { "descriptors", 1, 0, 'o' },
59 { "debug", 1, 0, 'd' },
60 { "help", 0, 0, 'h' },
61 { "exports-file", 1, 0, 'f' },
62 { "nfs-version", 1, 0, 'V' },
63 { "no-nfs-version", 1, 0, 'N' },
64 { "version", 0, 0, 'v' },
65 { "port", 1, 0, 'p' },
66 { "no-tcp", 0, 0, 'n' },
67 { "ha-callout", 1, 0, 'H' },
68 { "state-directory-path", 1, 0, 's' },
69 { "num-threads", 1, 0, 't' },
73 static int nfs_version = -1;
76 unregister_services (void)
78 if (nfs_version & 0x1)
79 pmap_unset (MOUNTPROG, MOUNTVERS);
80 if (nfs_version & (0x1 << 1))
81 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
82 if (nfs_version & (0x1 << 2))
83 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
86 /* Wait for all worker child processes to exit and reap them */
88 wait_for_workers (void)
95 pid = waitpid(0, &status, 0);
99 return; /* no more children */
100 xlog(L_FATAL, "mountd: can't wait: %s\n",
104 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
105 * does not happen on 2.6 kernels, and waitpid() blocks
106 * until all the children are dead then returns with
107 * -ECHILD. But, we don't need to do anything on the
108 * death of individual workers, so we don't care. */
109 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
114 /* Fork num_threads worker children and wait for them */
121 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
123 for (i = 0 ; i < num_threads ; i++) {
126 xlog(L_FATAL, "mountd: cannot fork: %s\n",
132 /* Re-enable the default action on SIGTERM et al
133 * so that workers die naturally when sent them.
134 * Only the parent unregisters with pmap and
135 * hence needs to do special SIGTERM handling. */
137 sa.sa_handler = SIG_DFL;
139 sigemptyset(&sa.sa_mask);
140 sigaction(SIGHUP, &sa, NULL);
141 sigaction(SIGINT, &sa, NULL);
142 sigaction(SIGTERM, &sa, NULL);
144 /* fall into my_svc_run in caller */
151 unregister_services();
152 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
162 unregister_services();
163 if (num_threads > 1) {
164 /* play Kronos and eat our children */
168 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
174 /* don't exit on SIGHUP */
175 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
180 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
186 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
188 struct nfs_fh_len *fh;
190 xlog(D_CALL, "MNT1(%s) called", *path);
191 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
192 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
197 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
199 struct sockaddr_in *addr =
200 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
202 if ((*res = mountlist_list()) == NULL)
203 xlog(L_WARNING, "dump request from %s failed.",
204 inet_ntoa(addr->sin_addr));
210 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
212 struct sockaddr_in *sin
213 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
216 char rpath[MAXPATHLEN+1];
221 if (realpath(p, rpath) != NULL) {
222 rpath[sizeof (rpath) - 1] = '\0';
226 if (!(exp = auth_authenticate("unmount", sin, p))) {
233 mountlist_del(inet_ntoa(sin->sin_addr), p);
238 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
240 /* Reload /etc/xtab if necessary */
243 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
248 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
250 struct sockaddr_in *addr =
251 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
253 if ((*resp = get_exportlist()) == NULL)
254 xlog(L_WARNING, "export request from %s failed.",
255 inet_ntoa(addr->sin_addr));
261 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
263 struct sockaddr_in *addr =
264 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
266 if ((*resp = get_exportlist()) == NULL)
267 xlog(L_WARNING, "exportall request from %s failed.",
268 inet_ntoa(addr->sin_addr));
273 * MNTv2 pathconf procedure
275 * The protocol doesn't include a status field, so Sun apparently considers
276 * it good practice to let anyone snoop on your system, even if it's
277 * pretty harmless data such as pathconf. We don't.
279 * Besides, many of the pathconf values don't make much sense on NFS volumes.
280 * FIFOs and tty device files represent devices on the *client*, so there's
281 * no point in getting the server's buffer sizes etc.
284 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
286 struct sockaddr_in *sin
287 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
290 char rpath[MAXPATHLEN+1];
293 memset(res, 0, sizeof(*res));
298 /* Reload /etc/xtab if necessary */
301 /* Resolve symlinks */
302 if (realpath(p, rpath) != NULL) {
303 rpath[sizeof (rpath) - 1] = '\0';
307 /* Now authenticate the intruder... */
308 if (!(exp = auth_authenticate("pathconf", sin, p))) {
310 } else if (stat(p, &stb) < 0) {
311 xlog(L_WARNING, "can't stat exported dir %s: %s",
317 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
318 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
319 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
320 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
321 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
322 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
323 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
325 /* Can't figure out what to do with pc_mask */
335 * NFSv3 MOUNT procedure
338 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
340 #define AUTH_GSS_KRB5 390003
341 #define AUTH_GSS_KRB5I 390004
342 #define AUTH_GSS_KRB5P 390005
343 static int flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
344 struct nfs_fh_len *fh;
346 xlog(D_CALL, "MNT3(%s) called", *path);
347 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 1)) != NULL) {
348 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
350 ok->fhandle.fhandle3_len = fh->fh_size;
351 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
352 ok->auth_flavors.auth_flavors_len
353 = sizeof(flavors)/sizeof(flavors[0]);
354 ok->auth_flavors.auth_flavors_val = flavors;
359 static struct nfs_fh_len *
360 get_rootfh(struct svc_req *rqstp, dirpath *path, mountstat3 *error, int v3)
362 struct sockaddr_in *sin =
363 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
364 struct stat stb, estb;
366 char rpath[MAXPATHLEN+1];
372 /* Reload /var/lib/nfs/etab if necessary */
375 /* Resolve symlinks */
376 if (realpath(p, rpath) != NULL) {
377 rpath[sizeof (rpath) - 1] = '\0';
381 /* Now authenticate the intruder... */
382 if (!(exp = auth_authenticate("mount", sin, p))) {
383 *error = NFSERR_ACCES;
384 } else if (stat(p, &stb) < 0) {
385 xlog(L_WARNING, "can't stat exported dir %s: %s",
388 *error = NFSERR_NOENT;
390 *error = NFSERR_ACCES;
391 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
392 xlog(L_WARNING, "%s is not a directory or regular file", p);
393 *error = NFSERR_NOTDIR;
394 } else if (stat(exp->m_export.e_path, &estb) < 0) {
395 xlog(L_WARNING, "can't stat export point %s: %s",
397 *error = NFSERR_NOENT;
398 } else if (estb.st_dev != stb.st_dev
399 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
401 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
402 p, exp->m_export.e_path);
403 *error = NFSERR_ACCES;
404 } else if (exp->m_export.e_mountpoint &&
405 !is_mountpoint(exp->m_export.e_mountpoint[0]?
406 exp->m_export.e_mountpoint:
407 exp->m_export.e_path)) {
408 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
410 *error = NFSERR_NOENT;
411 } else if (new_cache) {
412 /* This will be a static private nfs_export with just one
413 * address. We feed it to kernel then extract the filehandle,
416 struct nfs_fh_len *fh;
419 fh = cache_get_filehandle(exp, v3?64:32, p);
421 *error = NFSERR_ACCES;
424 mountlist_add(inet_ntoa(sin->sin_addr), p);
428 struct nfs_fh_len *fh;
430 if (exp->m_exported<1)
436 fh = getfh_size ((struct sockaddr *) sin, p, 64);
437 if (!v3 || (fh == NULL && errno == EINVAL)) {
438 /* We first try the new nfs syscall. */
439 fh = getfh ((struct sockaddr *) sin, p);
440 if (fh == NULL && errno == EINVAL)
441 /* Let's try the old one. */
442 fh = getfh_old ((struct sockaddr *) sin,
443 stb.st_dev, stb.st_ino);
446 mountlist_add(inet_ntoa(sin->sin_addr), p);
451 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
452 *error = NFSERR_ACCES;
461 static exports elist = NULL;
462 struct exportnode *e, *ne;
463 struct groupnode *g, *ng, *c, **cp;
467 if (!auth_reload() && elist)
470 for (e = elist; e != NULL; e = ne) {
472 for (g = e->ex_groups; g != NULL; g = ng) {
482 for (i = 0; i < MCL_MAXTYPES; i++) {
483 for (exp = exportlist[i]; exp; exp = exp->m_next) {
484 for (e = elist; e != NULL; e = e->ex_next) {
485 if (!strcmp(exp->m_export.m_path, e->ex_dir))
489 e = (struct exportnode *) xmalloc(sizeof(*e));
492 e->ex_dir = xstrdup(exp->m_export.m_path);
496 /* We need to check if we should remove
498 if (i == MCL_ANONYMOUS && e->ex_groups) {
499 for (g = e->ex_groups; g; g = ng) {
508 if (i != MCL_FQDN && e->ex_groups) {
512 while ((c = *cp) != NULL) {
513 if (client_gettype (c->gr_name) == MCL_FQDN
514 && (hp = gethostbyname(c->gr_name))) {
515 hp = hostent_dup (hp);
516 if (client_check(exp->m_client, hp)) {
529 if (exp->m_export.e_hostname [0] != '\0') {
530 for (g = e->ex_groups; g; g = g->gr_next)
531 if (strcmp (exp->m_export.e_hostname,
536 g = (struct groupnode *) xmalloc(sizeof(*g));
537 g->gr_name = xstrdup(exp->m_export.e_hostname);
538 g->gr_next = e->ex_groups;
548 main(int argc, char **argv)
550 char *export_file = _PATH_EXPORTS;
551 char *state_dir = NFS_STATEDIR;
559 /* Parse the command line options and arguments. */
561 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:", longopts, NULL)) != EOF)
564 descriptors = atoi(optarg);
565 if (descriptors <= 0) {
566 fprintf(stderr, "%s: bad descriptors: %s\n",
575 xlog_sconfig(optarg, 1);
578 export_file = optarg;
580 case 'H': /* PRC: specify a high-availability callout program */
581 ha_callout_prog = optarg;
586 case 'P': /* XXX for nfs-server compatibility */
589 if (port <= 0 || port > 65535) {
590 fprintf(stderr, "%s: bad port number: %s\n",
596 nfs_version &= ~(1 << (atoi (optarg) - 1));
599 _rpcfdtype = SOCK_DGRAM;
605 if ((state_dir = xstrdup(optarg)) == NULL) {
606 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
612 num_threads = atoi (optarg);
615 nfs_version |= 1 << (atoi (optarg) - 1);
618 printf("kmountd %s\n", VERSION);
627 /* No more arguments allowed. */
628 if (optind != argc || !(nfs_version & 0x7))
631 if (chdir(state_dir)) {
632 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
633 argv [0], state_dir, strerror(errno));
637 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
638 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
639 argv [0], strerror(errno));
641 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
642 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
643 descriptors > FD_SETSIZE)
644 descriptors = FD_SETSIZE;
646 rlim.rlim_cur = descriptors;
647 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
648 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
649 argv [0], strerror(errno));
654 /* Initialize logging. */
655 if (!foreground) xlog_stderr(0);
658 sa.sa_handler = SIG_IGN;
660 sigemptyset(&sa.sa_mask);
661 sigaction(SIGHUP, &sa, NULL);
662 sigaction(SIGINT, &sa, NULL);
663 sigaction(SIGTERM, &sa, NULL);
664 sigaction(SIGPIPE, &sa, NULL);
665 /* WARNING: the following works on Linux and SysV, but not BSD! */
666 sigaction(SIGCHLD, &sa, NULL);
668 /* Daemons should close all extra filehandles ... *before* RPC init. */
672 new_cache = check_new_cache();
676 if (nfs_version & 0x1)
677 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
678 mount_dispatch, port);
679 if (nfs_version & (0x1 << 1))
680 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
681 mount_dispatch, port);
682 if (nfs_version & (0x1 << 2))
683 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
684 mount_dispatch, port);
686 sa.sa_handler = killer;
687 sigaction(SIGINT, &sa, NULL);
688 sigaction(SIGTERM, &sa, NULL);
689 sa.sa_handler = sig_hup;
690 sigaction(SIGHUP, &sa, NULL);
692 auth_init(export_file);
695 /* We first fork off a child. */
696 if ((c = fork()) > 0)
699 xlog(L_FATAL, "mountd: cannot fork: %s\n",
702 /* Now we remove ourselves from the foreground.
703 Redirect stdin/stdout/stderr first. */
705 int fd = open("/dev/null", O_RDWR);
709 if (fd > 2) (void) close(fd);
714 /* silently bounds check num_threads */
717 else if (num_threads < 1)
719 else if (num_threads > MAX_THREADS)
720 num_threads = MAX_THREADS;
727 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
732 usage(const char *prog, int n)
735 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
736 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
737 " [-p|--port port] [-V version|--nfs-version version]\n"
738 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
739 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
740 " [-t num|--num-threads=num]\n", prog);