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, char *path);
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;
45 /* PRC: a high-availability callout program can be specified with -H
46 * When this is done, the program will receive callouts whenever clients
47 * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
48 char *ha_callout_prog = NULL;
50 /* Number of mountd threads to start. Default is 1 and
51 * that's probably enough unless you need hundreds of
52 * clients to be able to mount at once. */
53 static int num_threads = 1;
54 /* Arbitrary limit on number of threads */
55 #define MAX_THREADS 64
57 static struct option longopts[] =
59 { "foreground", 0, 0, 'F' },
60 { "descriptors", 1, 0, 'o' },
61 { "debug", 1, 0, 'd' },
62 { "help", 0, 0, 'h' },
63 { "exports-file", 1, 0, 'f' },
64 { "nfs-version", 1, 0, 'V' },
65 { "no-nfs-version", 1, 0, 'N' },
66 { "version", 0, 0, 'v' },
67 { "port", 1, 0, 'p' },
68 { "no-tcp", 0, 0, 'n' },
69 { "ha-callout", 1, 0, 'H' },
70 { "state-directory-path", 1, 0, 's' },
71 { "num-threads", 1, 0, 't' },
72 { "reverse-lookup", 0, 0, 'r' },
73 { "manage-gids", 0, 0, 'g' },
77 static int nfs_version = -1;
80 unregister_services (void)
82 if (nfs_version & 0x1)
83 pmap_unset (MOUNTPROG, MOUNTVERS);
84 if (nfs_version & (0x1 << 1))
85 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
86 if (nfs_version & (0x1 << 2))
87 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
90 /* Wait for all worker child processes to exit and reap them */
92 wait_for_workers (void)
99 pid = waitpid(0, &status, 0);
103 return; /* no more children */
104 xlog(L_FATAL, "mountd: can't wait: %s\n",
108 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
109 * does not happen on 2.6 kernels, and waitpid() blocks
110 * until all the children are dead then returns with
111 * -ECHILD. But, we don't need to do anything on the
112 * death of individual workers, so we don't care. */
113 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
118 /* Fork num_threads worker children and wait for them */
125 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
127 for (i = 0 ; i < num_threads ; i++) {
130 xlog(L_FATAL, "mountd: cannot fork: %s\n",
136 /* Re-enable the default action on SIGTERM et al
137 * so that workers die naturally when sent them.
138 * Only the parent unregisters with pmap and
139 * hence needs to do special SIGTERM handling. */
141 sa.sa_handler = SIG_DFL;
143 sigemptyset(&sa.sa_mask);
144 sigaction(SIGHUP, &sa, NULL);
145 sigaction(SIGINT, &sa, NULL);
146 sigaction(SIGTERM, &sa, NULL);
148 /* fall into my_svc_run in caller */
155 unregister_services();
156 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
166 unregister_services();
167 if (num_threads > 1) {
168 /* play Kronos and eat our children */
172 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
178 /* don't exit on SIGHUP */
179 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
184 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
190 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
192 struct nfs_fh_len *fh;
194 xlog(D_CALL, "MNT1(%s) called", *path);
195 fh = get_rootfh(rqstp, path, &res->fhs_status, 0);
197 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
202 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
204 struct sockaddr_in *addr =
205 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
207 xlog(D_CALL, "dump request from %s.", inet_ntoa(addr->sin_addr));
208 *res = mountlist_list();
214 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
216 struct sockaddr_in *sin
217 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
220 char rpath[MAXPATHLEN+1];
225 if (realpath(p, rpath) != NULL) {
226 rpath[sizeof (rpath) - 1] = '\0';
230 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 xlog(D_CALL, "export request from %s.", inet_ntoa(addr->sin_addr));
255 *resp = get_exportlist();
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 xlog(D_CALL, "exportall request from %s.", inet_ntoa(addr->sin_addr));
267 *resp = get_exportlist();
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 exp = auth_authenticate("pathconf", sin, p);
311 } else if (stat(p, &stb) < 0) {
312 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 */
333 * NFSv3 MOUNT procedure
336 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
338 #define AUTH_GSS_KRB5 390003
339 #define AUTH_GSS_KRB5I 390004
340 #define AUTH_GSS_KRB5P 390005
341 static int flavors[] = { AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
343 * We should advertise the preferred flavours first. (See RFC 2623
344 * section 2.7.) AUTH_UNIX is arbitrarily ranked over the GSS's.
345 * AUTH_NULL is dropped from the list to avoid backward compatibility
346 * issue with older Linux clients, who inspect the list in reversed
349 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
350 struct nfs_fh_len *fh;
352 xlog(D_CALL, "MNT3(%s) called", *path);
353 fh = get_rootfh(rqstp, path, &res->fhs_status, 1);
357 ok->fhandle.fhandle3_len = fh->fh_size;
358 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
359 ok->auth_flavors.auth_flavors_len = sizeof(flavors)/sizeof(flavors[0]);
360 ok->auth_flavors.auth_flavors_val = flavors;
364 static struct nfs_fh_len *
365 get_rootfh(struct svc_req *rqstp, dirpath *path, mountstat3 *error, int v3)
367 struct sockaddr_in *sin =
368 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
369 struct stat stb, estb;
371 struct nfs_fh_len *fh;
372 char rpath[MAXPATHLEN+1];
378 /* Reload /var/lib/nfs/etab if necessary */
381 /* Resolve symlinks */
382 if (realpath(p, rpath) != NULL) {
383 rpath[sizeof (rpath) - 1] = '\0';
387 /* Now authenticate the intruder... */
388 exp = auth_authenticate("mount", sin, p);
390 *error = NFSERR_ACCES;
393 if (stat(p, &stb) < 0) {
394 xlog(L_WARNING, "can't stat exported dir %s: %s",
397 *error = NFSERR_NOENT;
399 *error = NFSERR_ACCES;
402 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
403 xlog(L_WARNING, "%s is not a directory or regular file", p);
404 *error = NFSERR_NOTDIR;
407 if (stat(exp->m_export.e_path, &estb) < 0) {
408 xlog(L_WARNING, "can't stat export point %s: %s",
410 *error = NFSERR_NOENT;
413 if (estb.st_dev != stb.st_dev
415 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
416 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
417 p, exp->m_export.e_path);
418 *error = NFSERR_ACCES;
421 if (exp->m_export.e_mountpoint &&
422 !is_mountpoint(exp->m_export.e_mountpoint[0]?
423 exp->m_export.e_mountpoint:
424 exp->m_export.e_path)) {
425 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
427 *error = NFSERR_NOENT;
432 /* This will be a static private nfs_export with just one
433 * address. We feed it to kernel then extract the filehandle,
437 if (cache_export(exp, p)) {
438 *error = NFSERR_ACCES;
441 fh = cache_get_filehandle(exp, v3?64:32, p);
443 *error = NFSERR_ACCES;
447 if (exp->m_exported<1)
453 fh = getfh_size ((struct sockaddr *) sin, p, 64);
454 if (!v3 || (fh == NULL && errno == EINVAL)) {
455 /* We first try the new nfs syscall. */
456 fh = getfh ((struct sockaddr *) sin, p);
457 if (fh == NULL && errno == EINVAL)
458 /* Let's try the old one. */
459 fh = getfh_old ((struct sockaddr *) sin,
460 stb.st_dev, stb.st_ino);
463 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
464 *error = NFSERR_ACCES;
469 mountlist_add(inet_ntoa(sin->sin_addr), p);
476 static exports elist = NULL;
477 struct exportnode *e, *ne;
478 struct groupnode *g, *ng, *c, **cp;
481 static unsigned int ecounter;
482 unsigned int acounter;
484 acounter = auth_reload();
485 if (elist && acounter == ecounter)
490 for (e = elist; e != NULL; e = ne) {
492 for (g = e->ex_groups; g != NULL; g = ng) {
502 for (i = 0; i < MCL_MAXTYPES; i++) {
503 for (exp = exportlist[i]; exp; exp = exp->m_next) {
504 for (e = elist; e != NULL; e = e->ex_next) {
505 if (!strcmp(exp->m_export.e_path, e->ex_dir))
509 e = (struct exportnode *) xmalloc(sizeof(*e));
512 e->ex_dir = xstrdup(exp->m_export.e_path);
516 /* We need to check if we should remove
518 if (i == MCL_ANONYMOUS && e->ex_groups) {
519 for (g = e->ex_groups; g; g = ng) {
528 if (i != MCL_FQDN && e->ex_groups) {
532 while ((c = *cp) != NULL) {
533 if (client_gettype (c->gr_name) == MCL_FQDN
534 && (hp = gethostbyname(c->gr_name))) {
535 hp = hostent_dup (hp);
536 if (client_check(exp->m_client, hp)) {
549 if (exp->m_export.e_hostname [0] != '\0') {
550 for (g = e->ex_groups; g; g = g->gr_next)
551 if (strcmp (exp->m_export.e_hostname,
556 g = (struct groupnode *) xmalloc(sizeof(*g));
557 g->gr_name = xstrdup(exp->m_export.e_hostname);
558 g->gr_next = e->ex_groups;
568 main(int argc, char **argv)
570 char *export_file = _PATH_EXPORTS;
571 char *state_dir = NFS_STATEDIR;
579 /* Parse the command line options and arguments. */
581 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
587 descriptors = atoi(optarg);
588 if (descriptors <= 0) {
589 fprintf(stderr, "%s: bad descriptors: %s\n",
598 xlog_sconfig(optarg, 1);
601 export_file = optarg;
603 case 'H': /* PRC: specify a high-availability callout program */
604 ha_callout_prog = optarg;
609 case 'P': /* XXX for nfs-server compatibility */
612 if (port <= 0 || port > 65535) {
613 fprintf(stderr, "%s: bad port number: %s\n",
619 nfs_version &= ~(1 << (atoi (optarg) - 1));
622 _rpcfdtype = SOCK_DGRAM;
628 if ((state_dir = xstrdup(optarg)) == NULL) {
629 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
635 num_threads = atoi (optarg);
638 nfs_version |= 1 << (atoi (optarg) - 1);
641 printf("kmountd %s\n", VERSION);
650 /* No more arguments allowed. */
651 if (optind != argc || !(nfs_version & 0x7))
654 if (chdir(state_dir)) {
655 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
656 argv [0], state_dir, strerror(errno));
660 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
661 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
662 argv [0], strerror(errno));
664 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
665 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
666 descriptors > FD_SETSIZE)
667 descriptors = FD_SETSIZE;
669 rlim.rlim_cur = descriptors;
670 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
671 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
672 argv [0], strerror(errno));
677 /* Initialize logging. */
678 if (!foreground) xlog_stderr(0);
681 sa.sa_handler = SIG_IGN;
683 sigemptyset(&sa.sa_mask);
684 sigaction(SIGHUP, &sa, NULL);
685 sigaction(SIGINT, &sa, NULL);
686 sigaction(SIGTERM, &sa, NULL);
687 sigaction(SIGPIPE, &sa, NULL);
688 /* WARNING: the following works on Linux and SysV, but not BSD! */
689 sigaction(SIGCHLD, &sa, NULL);
691 /* Daemons should close all extra filehandles ... *before* RPC init. */
695 new_cache = check_new_cache();
699 if (nfs_version & 0x1)
700 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
701 mount_dispatch, port);
702 if (nfs_version & (0x1 << 1))
703 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
704 mount_dispatch, port);
705 if (nfs_version & (0x1 << 2))
706 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
707 mount_dispatch, port);
709 sa.sa_handler = killer;
710 sigaction(SIGINT, &sa, NULL);
711 sigaction(SIGTERM, &sa, NULL);
712 sa.sa_handler = sig_hup;
713 sigaction(SIGHUP, &sa, NULL);
715 auth_init(export_file);
718 /* We first fork off a child. */
719 if ((c = fork()) > 0)
722 xlog(L_FATAL, "mountd: cannot fork: %s\n",
725 /* Now we remove ourselves from the foreground.
726 Redirect stdin/stdout/stderr first. */
728 int fd = open("/dev/null", O_RDWR);
732 if (fd > 2) (void) close(fd);
737 /* silently bounds check num_threads */
740 else if (num_threads < 1)
742 else if (num_threads > MAX_THREADS)
743 num_threads = MAX_THREADS;
750 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
755 usage(const char *prog, int n)
758 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
759 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
760 " [-p|--port port] [-V version|--nfs-version version]\n"
761 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
762 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
763 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);