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' },
70 { "reverse-lookup", 0, 0, 'r' },
74 static int nfs_version = -1;
77 unregister_services (void)
79 if (nfs_version & 0x1)
80 pmap_unset (MOUNTPROG, MOUNTVERS);
81 if (nfs_version & (0x1 << 1))
82 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
83 if (nfs_version & (0x1 << 2))
84 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
87 /* Wait for all worker child processes to exit and reap them */
89 wait_for_workers (void)
96 pid = waitpid(0, &status, 0);
100 return; /* no more children */
101 xlog(L_FATAL, "mountd: can't wait: %s\n",
105 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
106 * does not happen on 2.6 kernels, and waitpid() blocks
107 * until all the children are dead then returns with
108 * -ECHILD. But, we don't need to do anything on the
109 * death of individual workers, so we don't care. */
110 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
115 /* Fork num_threads worker children and wait for them */
122 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
124 for (i = 0 ; i < num_threads ; i++) {
127 xlog(L_FATAL, "mountd: cannot fork: %s\n",
133 /* Re-enable the default action on SIGTERM et al
134 * so that workers die naturally when sent them.
135 * Only the parent unregisters with pmap and
136 * hence needs to do special SIGTERM handling. */
138 sa.sa_handler = SIG_DFL;
140 sigemptyset(&sa.sa_mask);
141 sigaction(SIGHUP, &sa, NULL);
142 sigaction(SIGINT, &sa, NULL);
143 sigaction(SIGTERM, &sa, NULL);
145 /* fall into my_svc_run in caller */
152 unregister_services();
153 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
163 unregister_services();
164 if (num_threads > 1) {
165 /* play Kronos and eat our children */
169 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
175 /* don't exit on SIGHUP */
176 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
181 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
187 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
189 struct nfs_fh_len *fh;
191 xlog(D_CALL, "MNT1(%s) called", *path);
192 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
193 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
198 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
200 struct sockaddr_in *addr =
201 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
203 if ((*res = mountlist_list()) == NULL)
204 xlog(L_WARNING, "dump request from %s failed.",
205 inet_ntoa(addr->sin_addr));
211 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
213 struct sockaddr_in *sin
214 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
217 char rpath[MAXPATHLEN+1];
222 if (realpath(p, rpath) != NULL) {
223 rpath[sizeof (rpath) - 1] = '\0';
227 if (!(exp = auth_authenticate("unmount", sin, p))) {
234 mountlist_del(inet_ntoa(sin->sin_addr), p);
239 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
241 /* Reload /etc/xtab if necessary */
244 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
249 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
251 struct sockaddr_in *addr =
252 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
254 if ((*resp = get_exportlist()) == NULL)
255 xlog(L_WARNING, "export request from %s failed.",
256 inet_ntoa(addr->sin_addr));
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 if ((*resp = get_exportlist()) == NULL)
268 xlog(L_WARNING, "exportall request from %s failed.",
269 inet_ntoa(addr->sin_addr));
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 if (!(exp = auth_authenticate("pathconf", sin, p))) {
311 } else if (stat(p, &stb) < 0) {
312 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 */
336 * NFSv3 MOUNT procedure
339 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
341 #define AUTH_GSS_KRB5 390003
342 #define AUTH_GSS_KRB5I 390004
343 #define AUTH_GSS_KRB5P 390005
344 static int flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
345 struct nfs_fh_len *fh;
347 xlog(D_CALL, "MNT3(%s) called", *path);
348 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 1)) != NULL) {
349 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
351 ok->fhandle.fhandle3_len = fh->fh_size;
352 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
353 ok->auth_flavors.auth_flavors_len
354 = sizeof(flavors)/sizeof(flavors[0]);
355 ok->auth_flavors.auth_flavors_val = flavors;
360 static struct nfs_fh_len *
361 get_rootfh(struct svc_req *rqstp, dirpath *path, mountstat3 *error, int v3)
363 struct sockaddr_in *sin =
364 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
365 struct stat stb, estb;
367 char rpath[MAXPATHLEN+1];
373 /* Reload /var/lib/nfs/etab if necessary */
376 /* Resolve symlinks */
377 if (realpath(p, rpath) != NULL) {
378 rpath[sizeof (rpath) - 1] = '\0';
382 /* Now authenticate the intruder... */
383 if (!(exp = auth_authenticate("mount", sin, p))) {
384 *error = NFSERR_ACCES;
385 } else if (stat(p, &stb) < 0) {
386 xlog(L_WARNING, "can't stat exported dir %s: %s",
389 *error = NFSERR_NOENT;
391 *error = NFSERR_ACCES;
392 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
393 xlog(L_WARNING, "%s is not a directory or regular file", p);
394 *error = NFSERR_NOTDIR;
395 } else if (stat(exp->m_export.e_path, &estb) < 0) {
396 xlog(L_WARNING, "can't stat export point %s: %s",
398 *error = NFSERR_NOENT;
399 } else if (estb.st_dev != stb.st_dev
400 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
402 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
403 p, exp->m_export.e_path);
404 *error = NFSERR_ACCES;
405 } else if (exp->m_export.e_mountpoint &&
406 !is_mountpoint(exp->m_export.e_mountpoint[0]?
407 exp->m_export.e_mountpoint:
408 exp->m_export.e_path)) {
409 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
411 *error = NFSERR_NOENT;
412 } else if (new_cache) {
413 /* This will be a static private nfs_export with just one
414 * address. We feed it to kernel then extract the filehandle,
417 struct nfs_fh_len *fh;
420 fh = cache_get_filehandle(exp, v3?64:32, p);
422 *error = NFSERR_ACCES;
425 mountlist_add(inet_ntoa(sin->sin_addr), p);
429 struct nfs_fh_len *fh;
431 if (exp->m_exported<1)
437 fh = getfh_size ((struct sockaddr *) sin, p, 64);
438 if (!v3 || (fh == NULL && errno == EINVAL)) {
439 /* We first try the new nfs syscall. */
440 fh = getfh ((struct sockaddr *) sin, p);
441 if (fh == NULL && errno == EINVAL)
442 /* Let's try the old one. */
443 fh = getfh_old ((struct sockaddr *) sin,
444 stb.st_dev, stb.st_ino);
447 mountlist_add(inet_ntoa(sin->sin_addr), p);
452 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
453 *error = NFSERR_ACCES;
462 static exports elist = NULL;
463 struct exportnode *e, *ne;
464 struct groupnode *g, *ng, *c, **cp;
468 if (!auth_reload() && elist)
471 for (e = elist; e != NULL; e = ne) {
473 for (g = e->ex_groups; g != NULL; g = ng) {
483 for (i = 0; i < MCL_MAXTYPES; i++) {
484 for (exp = exportlist[i]; exp; exp = exp->m_next) {
485 for (e = elist; e != NULL; e = e->ex_next) {
486 if (!strcmp(exp->m_export.m_path, e->ex_dir))
490 e = (struct exportnode *) xmalloc(sizeof(*e));
493 e->ex_dir = xstrdup(exp->m_export.m_path);
497 /* We need to check if we should remove
499 if (i == MCL_ANONYMOUS && e->ex_groups) {
500 for (g = e->ex_groups; g; g = ng) {
509 if (i != MCL_FQDN && e->ex_groups) {
513 while ((c = *cp) != NULL) {
514 if (client_gettype (c->gr_name) == MCL_FQDN
515 && (hp = gethostbyname(c->gr_name))) {
516 hp = hostent_dup (hp);
517 if (client_check(exp->m_client, hp)) {
530 if (exp->m_export.e_hostname [0] != '\0') {
531 for (g = e->ex_groups; g; g = g->gr_next)
532 if (strcmp (exp->m_export.e_hostname,
537 g = (struct groupnode *) xmalloc(sizeof(*g));
538 g->gr_name = xstrdup(exp->m_export.e_hostname);
539 g->gr_next = e->ex_groups;
549 main(int argc, char **argv)
551 char *export_file = _PATH_EXPORTS;
552 char *state_dir = NFS_STATEDIR;
560 /* Parse the command line options and arguments. */
562 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:", longopts, NULL)) != EOF)
565 descriptors = atoi(optarg);
566 if (descriptors <= 0) {
567 fprintf(stderr, "%s: bad descriptors: %s\n",
576 xlog_sconfig(optarg, 1);
579 export_file = optarg;
581 case 'H': /* PRC: specify a high-availability callout program */
582 ha_callout_prog = optarg;
587 case 'P': /* XXX for nfs-server compatibility */
590 if (port <= 0 || port > 65535) {
591 fprintf(stderr, "%s: bad port number: %s\n",
597 nfs_version &= ~(1 << (atoi (optarg) - 1));
600 _rpcfdtype = SOCK_DGRAM;
606 if ((state_dir = xstrdup(optarg)) == NULL) {
607 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
613 num_threads = atoi (optarg);
616 nfs_version |= 1 << (atoi (optarg) - 1);
619 printf("kmountd %s\n", VERSION);
628 /* No more arguments allowed. */
629 if (optind != argc || !(nfs_version & 0x7))
632 if (chdir(state_dir)) {
633 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
634 argv [0], state_dir, strerror(errno));
638 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
639 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
640 argv [0], strerror(errno));
642 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
643 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
644 descriptors > FD_SETSIZE)
645 descriptors = FD_SETSIZE;
647 rlim.rlim_cur = descriptors;
648 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
649 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
650 argv [0], strerror(errno));
655 /* Initialize logging. */
656 if (!foreground) xlog_stderr(0);
659 sa.sa_handler = SIG_IGN;
661 sigemptyset(&sa.sa_mask);
662 sigaction(SIGHUP, &sa, NULL);
663 sigaction(SIGINT, &sa, NULL);
664 sigaction(SIGTERM, &sa, NULL);
665 sigaction(SIGPIPE, &sa, NULL);
666 /* WARNING: the following works on Linux and SysV, but not BSD! */
667 sigaction(SIGCHLD, &sa, NULL);
669 /* Daemons should close all extra filehandles ... *before* RPC init. */
673 new_cache = check_new_cache();
677 if (nfs_version & 0x1)
678 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
679 mount_dispatch, port);
680 if (nfs_version & (0x1 << 1))
681 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
682 mount_dispatch, port);
683 if (nfs_version & (0x1 << 2))
684 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
685 mount_dispatch, port);
687 sa.sa_handler = killer;
688 sigaction(SIGINT, &sa, NULL);
689 sigaction(SIGTERM, &sa, NULL);
690 sa.sa_handler = sig_hup;
691 sigaction(SIGHUP, &sa, NULL);
693 auth_init(export_file);
696 /* We first fork off a child. */
697 if ((c = fork()) > 0)
700 xlog(L_FATAL, "mountd: cannot fork: %s\n",
703 /* Now we remove ourselves from the foreground.
704 Redirect stdin/stdout/stderr first. */
706 int fd = open("/dev/null", O_RDWR);
710 if (fd > 2) (void) close(fd);
715 /* silently bounds check num_threads */
718 else if (num_threads < 1)
720 else if (num_threads > MAX_THREADS)
721 num_threads = MAX_THREADS;
728 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
733 usage(const char *prog, int n)
736 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
737 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
738 " [-p|--port port] [-V version|--nfs-version version]\n"
739 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
740 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
741 " [-t num|--num-threads=num]\n", prog);