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>
29 #include "pseudoflavors.h"
31 extern void cache_open(void);
32 extern struct nfs_fh_len *cache_get_filehandle(nfs_export *exp, int len, char *p);
33 extern int cache_export(nfs_export *exp, char *path);
35 extern void my_svc_run(void);
37 static void usage(const char *, int exitcode);
38 static exports get_exportlist(void);
39 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, nfs_export **, mountstat3 *, int v3);
41 int reverse_resolve = 0;
46 /* PRC: a high-availability callout program can be specified with -H
47 * When this is done, the program will receive callouts whenever clients
48 * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
49 char *ha_callout_prog = NULL;
51 /* Number of mountd threads to start. Default is 1 and
52 * that's probably enough unless you need hundreds of
53 * clients to be able to mount at once. */
54 static int num_threads = 1;
55 /* Arbitrary limit on number of threads */
56 #define MAX_THREADS 64
58 static struct option longopts[] =
60 { "foreground", 0, 0, 'F' },
61 { "descriptors", 1, 0, 'o' },
62 { "debug", 1, 0, 'd' },
63 { "help", 0, 0, 'h' },
64 { "exports-file", 1, 0, 'f' },
65 { "nfs-version", 1, 0, 'V' },
66 { "no-nfs-version", 1, 0, 'N' },
67 { "version", 0, 0, 'v' },
68 { "port", 1, 0, 'p' },
69 { "no-tcp", 0, 0, 'n' },
70 { "ha-callout", 1, 0, 'H' },
71 { "state-directory-path", 1, 0, 's' },
72 { "num-threads", 1, 0, 't' },
73 { "reverse-lookup", 0, 0, 'r' },
74 { "manage-gids", 0, 0, 'g' },
78 static int nfs_version = -1;
81 unregister_services (void)
83 if (nfs_version & 0x1)
84 pmap_unset (MOUNTPROG, MOUNTVERS);
85 if (nfs_version & (0x1 << 1))
86 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
87 if (nfs_version & (0x1 << 2))
88 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
92 cleanup_lockfiles (void)
94 unlink(_PATH_XTABLCK);
95 unlink(_PATH_ETABLCK);
96 unlink(_PATH_RMTABLCK);
99 /* Wait for all worker child processes to exit and reap them */
101 wait_for_workers (void)
108 pid = waitpid(0, &status, 0);
112 return; /* no more children */
113 xlog(L_FATAL, "mountd: can't wait: %s\n",
117 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
118 * does not happen on 2.6 kernels, and waitpid() blocks
119 * until all the children are dead then returns with
120 * -ECHILD. But, we don't need to do anything on the
121 * death of individual workers, so we don't care. */
122 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
127 /* Fork num_threads worker children and wait for them */
134 xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
136 for (i = 0 ; i < num_threads ; i++) {
139 xlog(L_FATAL, "mountd: cannot fork: %s\n",
145 /* Re-enable the default action on SIGTERM et al
146 * so that workers die naturally when sent them.
147 * Only the parent unregisters with pmap and
148 * hence needs to do special SIGTERM handling. */
150 sa.sa_handler = SIG_DFL;
152 sigemptyset(&sa.sa_mask);
153 sigaction(SIGHUP, &sa, NULL);
154 sigaction(SIGINT, &sa, NULL);
155 sigaction(SIGTERM, &sa, NULL);
157 /* fall into my_svc_run in caller */
164 unregister_services();
166 xlog(L_NOTICE, "mountd: no more workers, exiting\n");
176 unregister_services();
177 if (num_threads > 1) {
178 /* play Kronos and eat our children */
183 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
189 /* don't exit on SIGHUP */
190 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
195 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
201 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
203 struct nfs_fh_len *fh;
205 xlog(D_CALL, "MNT1(%s) called", *path);
206 fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
208 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
213 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
215 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
217 xlog(D_CALL, "dump request from %s.", inet_ntoa(addr->sin_addr));
218 *res = mountlist_list();
224 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
226 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
229 char rpath[MAXPATHLEN+1];
234 if (realpath(p, rpath) != NULL) {
235 rpath[sizeof (rpath) - 1] = '\0';
239 if (!(exp = auth_authenticate("unmount", sin, p))) {
243 mountlist_del(inet_ntoa(sin->sin_addr), p);
248 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
250 /* Reload /etc/xtab if necessary */
253 mountlist_del_all(nfs_getrpccaller_in(rqstp->rq_xprt));
258 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
260 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
262 xlog(D_CALL, "export request from %s.", inet_ntoa(addr->sin_addr));
263 *resp = get_exportlist();
269 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
271 struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
273 xlog(D_CALL, "exportall request from %s.", inet_ntoa(addr->sin_addr));
274 *resp = get_exportlist();
280 * MNTv2 pathconf procedure
282 * The protocol doesn't include a status field, so Sun apparently considers
283 * it good practice to let anyone snoop on your system, even if it's
284 * pretty harmless data such as pathconf. We don't.
286 * Besides, many of the pathconf values don't make much sense on NFS volumes.
287 * FIFOs and tty device files represent devices on the *client*, so there's
288 * no point in getting the server's buffer sizes etc.
291 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
293 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
296 char rpath[MAXPATHLEN+1];
299 memset(res, 0, sizeof(*res));
304 /* Reload /etc/xtab if necessary */
307 /* Resolve symlinks */
308 if (realpath(p, rpath) != NULL) {
309 rpath[sizeof (rpath) - 1] = '\0';
313 /* Now authenticate the intruder... */
314 exp = auth_authenticate("pathconf", sin, p);
317 } else if (stat(p, &stb) < 0) {
318 xlog(L_WARNING, "can't stat exported dir %s: %s",
323 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
324 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
325 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
326 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
327 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
328 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
329 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
331 /* Can't figure out what to do with pc_mask */
339 * We should advertise the preferred flavours first. (See RFC 2623
340 * section 2.7.) We leave that to the administrator, by advertising
341 * flavours in the order they were listed in /etc/exports. AUTH_NULL is
342 * dropped from the list to avoid backward compatibility issue with
343 * older Linux clients, who inspect the list in reversed order.
345 * XXX: It might be more helpful to rearrange these so that flavors
346 * giving more access (as determined from readonly and id-squashing
347 * options) come first. (If we decide to do that we should probably do
348 * that when reading the exports rather than here.)
350 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
353 static int flavors[SECFLAVOR_COUNT];
356 for (s = exp->m_export.e_secinfo; s->flav; s++) {
357 if (s->flav->fnum == AUTH_NULL)
359 flavors[i] = s->flav->fnum;
363 /* default when there is no sec= option: */
365 flavors[0] = AUTH_UNIX;
367 ok->auth_flavors.auth_flavors_val = flavors;
368 ok->auth_flavors.auth_flavors_len = i;
372 * NFSv3 MOUNT procedure
375 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
377 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
379 struct nfs_fh_len *fh;
381 xlog(D_CALL, "MNT3(%s) called", *path);
382 fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
386 ok->fhandle.fhandle3_len = fh->fh_size;
387 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
388 set_authflavors(ok, exp);
392 static struct nfs_fh_len *
393 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
394 mountstat3 *error, int v3)
396 struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
397 struct stat stb, estb;
399 struct nfs_fh_len *fh;
400 char rpath[MAXPATHLEN+1];
406 /* Reload /var/lib/nfs/etab if necessary */
409 /* Resolve symlinks */
410 if (realpath(p, rpath) != NULL) {
411 rpath[sizeof (rpath) - 1] = '\0';
415 /* Now authenticate the intruder... */
416 exp = auth_authenticate("mount", sin, p);
418 *error = NFSERR_ACCES;
421 if (stat(p, &stb) < 0) {
422 xlog(L_WARNING, "can't stat exported dir %s: %s",
425 *error = NFSERR_NOENT;
427 *error = NFSERR_ACCES;
430 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
431 xlog(L_WARNING, "%s is not a directory or regular file", p);
432 *error = NFSERR_NOTDIR;
435 if (stat(exp->m_export.e_path, &estb) < 0) {
436 xlog(L_WARNING, "can't stat export point %s: %s",
438 *error = NFSERR_NOENT;
441 if (estb.st_dev != stb.st_dev
443 || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
444 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
445 p, exp->m_export.e_path);
446 *error = NFSERR_ACCES;
449 if (exp->m_export.e_mountpoint &&
450 !is_mountpoint(exp->m_export.e_mountpoint[0]?
451 exp->m_export.e_mountpoint:
452 exp->m_export.e_path)) {
453 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
455 *error = NFSERR_NOENT;
460 /* This will be a static private nfs_export with just one
461 * address. We feed it to kernel then extract the filehandle,
465 if (cache_export(exp, p)) {
466 *error = NFSERR_ACCES;
469 fh = cache_get_filehandle(exp, v3?64:32, p);
471 *error = NFSERR_ACCES;
477 if (exp->m_exported<1) {
485 fh = getfh_size ((struct sockaddr *) sin, p, 64);
486 if (!v3 || (fh == NULL && errno == EINVAL)) {
487 /* We first try the new nfs syscall. */
488 fh = getfh ((struct sockaddr *) sin, p);
489 if (fh == NULL && errno == EINVAL)
490 /* Let's try the old one. */
491 fh = getfh_old ((struct sockaddr *) sin,
492 stb.st_dev, stb.st_ino);
494 if (fh == NULL && !did_export) {
500 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
501 *error = NFSERR_ACCES;
506 mountlist_add(inet_ntoa(sin->sin_addr), p);
512 static void remove_all_clients(exportnode *e)
514 struct groupnode *g, *ng;
516 for (g = e->ex_groups; g; g = ng) {
524 static void free_exportlist(exports *elist)
526 struct exportnode *e, *ne;
528 for (e = *elist; e != NULL; e = ne) {
530 remove_all_clients(e);
537 static void prune_clients(nfs_export *exp, struct exportnode *e)
540 struct groupnode *c, **cp;
543 while ((c = *cp) != NULL) {
544 if (client_gettype(c->gr_name) == MCL_FQDN
545 && (hp = gethostbyname(c->gr_name))) {
546 hp = hostent_dup(hp);
547 if (client_check(exp->m_client, hp)) {
560 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
564 for (e = *elist; e != NULL; e = e->ex_next) {
565 if (!strcmp(exp->m_export.e_path, e->ex_dir))
568 e = xmalloc(sizeof(*e));
571 e->ex_dir = xstrdup(exp->m_export.e_path);
576 static void insert_group(struct exportnode *e, char *newname)
580 for (g = e->ex_groups; g; g = g->gr_next)
581 if (strcmp(g->gr_name, newname))
584 g = xmalloc(sizeof(*g));
585 g->gr_name = xstrdup(newname);
586 g->gr_next = e->ex_groups;
593 static exports elist = NULL;
594 struct exportnode *e;
597 static unsigned int ecounter;
598 unsigned int acounter;
600 acounter = auth_reload();
601 if (elist && acounter == ecounter)
606 free_exportlist(&elist);
608 for (i = 0; i < MCL_MAXTYPES; i++) {
609 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
610 /* Don't show pseudo exports */
611 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
613 e = lookup_or_create_elist_entry(&elist, exp);
615 /* exports to "*" absorb any others */
616 if (i == MCL_ANONYMOUS && e->ex_groups) {
617 remove_all_clients(e);
620 /* non-FQDN's absorb FQDN's they contain: */
621 if (i != MCL_FQDN && e->ex_groups)
622 prune_clients(exp, e);
624 if (exp->m_export.e_hostname[0] != '\0')
625 insert_group(e, exp->m_export.e_hostname);
633 main(int argc, char **argv)
635 char *export_file = _PATH_EXPORTS;
636 char *state_dir = NFS_STATEDIR;
644 /* Parse the command line options and arguments. */
646 while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
652 descriptors = atoi(optarg);
653 if (descriptors <= 0) {
654 fprintf(stderr, "%s: bad descriptors: %s\n",
663 xlog_sconfig(optarg, 1);
666 export_file = optarg;
668 case 'H': /* PRC: specify a high-availability callout program */
669 ha_callout_prog = optarg;
674 case 'P': /* XXX for nfs-server compatibility */
677 if (port <= 0 || port > 65535) {
678 fprintf(stderr, "%s: bad port number: %s\n",
684 nfs_version &= ~(1 << (atoi (optarg) - 1));
687 _rpcfdtype = SOCK_DGRAM;
693 if ((state_dir = xstrdup(optarg)) == NULL) {
694 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
700 num_threads = atoi (optarg);
703 nfs_version |= 1 << (atoi (optarg) - 1);
706 printf("kmountd %s\n", VERSION);
715 /* No more arguments allowed. */
716 if (optind != argc || !(nfs_version & 0x7))
719 if (chdir(state_dir)) {
720 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
721 argv [0], state_dir, strerror(errno));
725 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
726 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
727 argv [0], strerror(errno));
729 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
730 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
731 descriptors > FD_SETSIZE)
732 descriptors = FD_SETSIZE;
734 rlim.rlim_cur = descriptors;
735 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
736 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
737 argv [0], strerror(errno));
742 /* Initialize logging. */
743 if (!foreground) xlog_stderr(0);
746 sa.sa_handler = SIG_IGN;
748 sigemptyset(&sa.sa_mask);
749 sigaction(SIGHUP, &sa, NULL);
750 sigaction(SIGINT, &sa, NULL);
751 sigaction(SIGTERM, &sa, NULL);
752 sigaction(SIGPIPE, &sa, NULL);
753 /* WARNING: the following works on Linux and SysV, but not BSD! */
754 sigaction(SIGCHLD, &sa, NULL);
756 /* Daemons should close all extra filehandles ... *before* RPC init. */
760 new_cache = check_new_cache();
764 if (nfs_version & 0x1)
765 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
766 mount_dispatch, port);
767 if (nfs_version & (0x1 << 1))
768 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
769 mount_dispatch, port);
770 if (nfs_version & (0x1 << 2))
771 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
772 mount_dispatch, port);
774 sa.sa_handler = killer;
775 sigaction(SIGINT, &sa, NULL);
776 sigaction(SIGTERM, &sa, NULL);
777 sa.sa_handler = sig_hup;
778 sigaction(SIGHUP, &sa, NULL);
780 auth_init(export_file);
783 /* We first fork off a child. */
784 if ((c = fork()) > 0)
787 xlog(L_FATAL, "mountd: cannot fork: %s\n",
790 /* Now we remove ourselves from the foreground.
791 Redirect stdin/stdout/stderr first. */
793 int fd = open("/dev/null", O_RDWR);
797 if (fd > 2) (void) close(fd);
802 /* silently bounds check num_threads */
805 else if (num_threads < 1)
807 else if (num_threads > MAX_THREADS)
808 num_threads = MAX_THREADS;
815 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
820 usage(const char *prog, int n)
823 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
824 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
825 " [-p|--port port] [-V version|--nfs-version version]\n"
826 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
827 " [-H ha-callout-prog] [-s|--state-directory-path path]\n"
828 " [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);