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))) {
229 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
230 mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
231 mountlist_del(exp->m_client->m_hostname, p);
233 mountlist_del(exp->m_client->m_hostname, 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 if ((*resp = get_exportlist()) == NULL)
256 xlog(L_WARNING, "export request from %s failed.",
257 inet_ntoa(addr->sin_addr));
263 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
265 struct sockaddr_in *addr =
266 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
268 if ((*resp = get_exportlist()) == NULL)
269 xlog(L_WARNING, "exportall request from %s failed.",
270 inet_ntoa(addr->sin_addr));
275 * MNTv2 pathconf procedure
277 * The protocol doesn't include a status field, so Sun apparently considers
278 * it good practice to let anyone snoop on your system, even if it's
279 * pretty harmless data such as pathconf. We don't.
281 * Besides, many of the pathconf values don't make much sense on NFS volumes.
282 * FIFOs and tty device files represent devices on the *client*, so there's
283 * no point in getting the server's buffer sizes etc.
286 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
288 struct sockaddr_in *sin
289 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
292 char rpath[MAXPATHLEN+1];
295 memset(res, 0, sizeof(*res));
300 /* Reload /etc/xtab if necessary */
303 /* Resolve symlinks */
304 if (realpath(p, rpath) != NULL) {
305 rpath[sizeof (rpath) - 1] = '\0';
309 /* Now authenticate the intruder... */
310 if (!(exp = auth_authenticate("pathconf", sin, p))) {
312 } else if (stat(p, &stb) < 0) {
313 xlog(L_WARNING, "can't stat exported dir %s: %s",
319 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
320 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
321 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
322 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
323 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
324 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
325 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
327 /* Can't figure out what to do with pc_mask */
337 * NFSv3 MOUNT procedure
340 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
342 #define AUTH_GSS_KRB5 390003
343 #define AUTH_GSS_KRB5I 390004
344 #define AUTH_GSS_KRB5P 390005
345 static int flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
346 struct nfs_fh_len *fh;
348 xlog(D_CALL, "MNT3(%s) called", *path);
349 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 1)) != NULL) {
350 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
352 ok->fhandle.fhandle3_len = fh->fh_size;
353 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
354 ok->auth_flavors.auth_flavors_len
355 = sizeof(flavors)/sizeof(flavors[0]);
356 ok->auth_flavors.auth_flavors_val = flavors;
361 static struct nfs_fh_len *
362 get_rootfh(struct svc_req *rqstp, dirpath *path, mountstat3 *error, int v3)
364 struct sockaddr_in *sin =
365 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
366 struct stat stb, estb;
368 char rpath[MAXPATHLEN+1];
374 /* Reload /var/lib/nfs/etab if necessary */
377 /* Resolve symlinks */
378 if (realpath(p, rpath) != NULL) {
379 rpath[sizeof (rpath) - 1] = '\0';
383 /* Now authenticate the intruder... */
384 if (!(exp = auth_authenticate("mount", sin, p))) {
385 *error = NFSERR_ACCES;
386 } else if (stat(p, &stb) < 0) {
387 xlog(L_WARNING, "can't stat exported dir %s: %s",
390 *error = NFSERR_NOENT;
392 *error = NFSERR_ACCES;
393 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
394 xlog(L_WARNING, "%s is not a directory or regular file", p);
395 *error = NFSERR_NOTDIR;
396 } else if (stat(exp->m_export.e_path, &estb) < 0) {
397 xlog(L_WARNING, "can't stat export point %s: %s",
399 *error = NFSERR_NOENT;
400 } else if (estb.st_dev != stb.st_dev
401 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
403 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
404 p, exp->m_export.e_path);
405 *error = NFSERR_ACCES;
406 } else if (exp->m_export.e_mountpoint &&
407 !is_mountpoint(exp->m_export.e_mountpoint[0]?
408 exp->m_export.e_mountpoint:
409 exp->m_export.e_path)) {
410 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
412 *error = NFSERR_NOENT;
413 } else if (new_cache) {
414 /* This will be a static private nfs_export with just one
415 * address. We feed it to kernel then extract the filehandle,
418 struct nfs_fh_len *fh;
421 fh = cache_get_filehandle(exp, v3?64:32, p);
423 *error = NFSERR_ACCES;
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(exp->m_client->m_hostname, 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:n:Fd:f:p:P:hH:N:V:vs: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;
602 if ((state_dir = xstrdup(optarg)) == NULL) {
603 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
609 num_threads = atoi (optarg);
612 nfs_version |= 1 << (atoi (optarg) - 1);
615 printf("kmountd %s\n", VERSION);
624 /* No more arguments allowed. */
625 if (optind != argc || !(nfs_version & 0x7))
628 if (chdir(state_dir)) {
629 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
630 argv [0], state_dir, strerror(errno));
634 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
635 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
636 argv [0], strerror(errno));
638 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
639 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
640 descriptors > FD_SETSIZE)
641 descriptors = FD_SETSIZE;
643 rlim.rlim_cur = descriptors;
644 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
645 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
646 argv [0], strerror(errno));
651 /* Initialize logging. */
652 if (!foreground) xlog_stderr(0);
655 sa.sa_handler = SIG_IGN;
657 sigemptyset(&sa.sa_mask);
658 sigaction(SIGHUP, &sa, NULL);
659 sigaction(SIGINT, &sa, NULL);
660 sigaction(SIGTERM, &sa, NULL);
661 sigaction(SIGPIPE, &sa, NULL);
662 /* WARNING: the following works on Linux and SysV, but not BSD! */
663 sigaction(SIGCHLD, &sa, NULL);
665 /* Daemons should close all extra filehandles ... *before* RPC init. */
669 new_cache = check_new_cache();
673 if (nfs_version & 0x1)
674 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
675 mount_dispatch, port);
676 if (nfs_version & (0x1 << 1))
677 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
678 mount_dispatch, port);
679 if (nfs_version & (0x1 << 2))
680 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
681 mount_dispatch, port);
683 sa.sa_handler = killer;
684 sigaction(SIGINT, &sa, NULL);
685 sigaction(SIGTERM, &sa, NULL);
686 sa.sa_handler = sig_hup;
687 sigaction(SIGHUP, &sa, NULL);
689 auth_init(export_file);
692 /* We first fork off a child. */
693 if ((c = fork()) > 0)
696 xlog(L_FATAL, "mountd: cannot fork: %s\n",
699 /* Now we remove ourselves from the foreground.
700 Redirect stdin/stdout/stderr first. */
702 int fd = open("/dev/null", O_RDWR);
706 if (fd > 2) (void) close(fd);
711 /* silently bounds check num_threads */
714 else if (num_threads < 1)
716 else if (num_threads > MAX_THREADS)
717 num_threads = MAX_THREADS;
724 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
729 usage(const char *prog, int n)
732 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
733 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
734 " [-p|--port port] [-V version|--nfs-version version]\n"
735 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
736 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
737 " [-t num|--num-threads=num]\n", prog);