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 int 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;
419 if (cache_export(exp)) {
420 *error = NFSERR_ACCES;
423 fh = cache_get_filehandle(exp, v3?64:32, p);
425 *error = NFSERR_ACCES;
428 mountlist_add(inet_ntoa(sin->sin_addr), p);
432 struct nfs_fh_len *fh;
434 if (exp->m_exported<1)
440 fh = getfh_size ((struct sockaddr *) sin, p, 64);
441 if (!v3 || (fh == NULL && errno == EINVAL)) {
442 /* We first try the new nfs syscall. */
443 fh = getfh ((struct sockaddr *) sin, p);
444 if (fh == NULL && errno == EINVAL)
445 /* Let's try the old one. */
446 fh = getfh_old ((struct sockaddr *) sin,
447 stb.st_dev, stb.st_ino);
450 mountlist_add(inet_ntoa(sin->sin_addr), p);
455 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
456 *error = NFSERR_ACCES;
465 static exports elist = NULL;
466 static time_t etime = 0;
468 struct exportnode *e, *ne;
469 struct groupnode *g, *ng, *c, **cp;
473 atime = auth_reload();
474 if (elist && atime == etime)
479 for (e = elist; e != NULL; e = ne) {
481 for (g = e->ex_groups; g != NULL; g = ng) {
491 for (i = 0; i < MCL_MAXTYPES; i++) {
492 for (exp = exportlist[i]; exp; exp = exp->m_next) {
493 for (e = elist; e != NULL; e = e->ex_next) {
494 if (!strcmp(exp->m_export.m_path, e->ex_dir))
498 e = (struct exportnode *) xmalloc(sizeof(*e));
501 e->ex_dir = xstrdup(exp->m_export.m_path);
505 /* We need to check if we should remove
507 if (i == MCL_ANONYMOUS && e->ex_groups) {
508 for (g = e->ex_groups; g; g = ng) {
517 if (i != MCL_FQDN && e->ex_groups) {
521 while ((c = *cp) != NULL) {
522 if (client_gettype (c->gr_name) == MCL_FQDN
523 && (hp = gethostbyname(c->gr_name))) {
524 hp = hostent_dup (hp);
525 if (client_check(exp->m_client, hp)) {
538 if (exp->m_export.e_hostname [0] != '\0') {
539 for (g = e->ex_groups; g; g = g->gr_next)
540 if (strcmp (exp->m_export.e_hostname,
545 g = (struct groupnode *) xmalloc(sizeof(*g));
546 g->gr_name = xstrdup(exp->m_export.e_hostname);
547 g->gr_next = e->ex_groups;
557 main(int argc, char **argv)
559 char *export_file = _PATH_EXPORTS;
560 char *state_dir = NFS_STATEDIR;
568 /* Parse the command line options and arguments. */
570 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:", longopts, NULL)) != EOF)
573 descriptors = atoi(optarg);
574 if (descriptors <= 0) {
575 fprintf(stderr, "%s: bad descriptors: %s\n",
584 xlog_sconfig(optarg, 1);
587 export_file = optarg;
589 case 'H': /* PRC: specify a high-availability callout program */
590 ha_callout_prog = optarg;
595 case 'P': /* XXX for nfs-server compatibility */
598 if (port <= 0 || port > 65535) {
599 fprintf(stderr, "%s: bad port number: %s\n",
605 nfs_version &= ~(1 << (atoi (optarg) - 1));
608 _rpcfdtype = SOCK_DGRAM;
614 if ((state_dir = xstrdup(optarg)) == NULL) {
615 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
621 num_threads = atoi (optarg);
624 nfs_version |= 1 << (atoi (optarg) - 1);
627 printf("kmountd %s\n", VERSION);
636 /* No more arguments allowed. */
637 if (optind != argc || !(nfs_version & 0x7))
640 if (chdir(state_dir)) {
641 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
642 argv [0], state_dir, strerror(errno));
646 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
647 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
648 argv [0], strerror(errno));
650 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
651 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
652 descriptors > FD_SETSIZE)
653 descriptors = FD_SETSIZE;
655 rlim.rlim_cur = descriptors;
656 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
657 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
658 argv [0], strerror(errno));
663 /* Initialize logging. */
664 if (!foreground) xlog_stderr(0);
667 sa.sa_handler = SIG_IGN;
669 sigemptyset(&sa.sa_mask);
670 sigaction(SIGHUP, &sa, NULL);
671 sigaction(SIGINT, &sa, NULL);
672 sigaction(SIGTERM, &sa, NULL);
673 sigaction(SIGPIPE, &sa, NULL);
674 /* WARNING: the following works on Linux and SysV, but not BSD! */
675 sigaction(SIGCHLD, &sa, NULL);
677 /* Daemons should close all extra filehandles ... *before* RPC init. */
681 new_cache = check_new_cache();
685 if (nfs_version & 0x1)
686 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
687 mount_dispatch, port);
688 if (nfs_version & (0x1 << 1))
689 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
690 mount_dispatch, port);
691 if (nfs_version & (0x1 << 2))
692 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
693 mount_dispatch, port);
695 sa.sa_handler = killer;
696 sigaction(SIGINT, &sa, NULL);
697 sigaction(SIGTERM, &sa, NULL);
698 sa.sa_handler = sig_hup;
699 sigaction(SIGHUP, &sa, NULL);
701 auth_init(export_file);
704 /* We first fork off a child. */
705 if ((c = fork()) > 0)
708 xlog(L_FATAL, "mountd: cannot fork: %s\n",
711 /* Now we remove ourselves from the foreground.
712 Redirect stdin/stdout/stderr first. */
714 int fd = open("/dev/null", O_RDWR);
718 if (fd > 2) (void) close(fd);
723 /* silently bounds check num_threads */
726 else if (num_threads < 1)
728 else if (num_threads > MAX_THREADS)
729 num_threads = MAX_THREADS;
736 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
741 usage(const char *prog, int n)
744 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
745 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
746 " [-p|--port port] [-V version|--nfs-version version]\n"
747 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
748 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
749 " [-t num|--num-threads=num]\n", prog);