2 * utils/mountd/mountd.c
4 * Authenticate mount requests and retrieve file handle.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
21 #include <sys/resource.h>
27 extern void cache_open(void);
28 extern struct nfs_fh_len *cache_get_filehandle(nfs_export *exp, int len, char *p);
29 extern void cache_export(nfs_export *exp);
31 extern void my_svc_run(void);
33 static void usage(const char *, int exitcode);
34 static exports get_exportlist(void);
35 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, int *, int v3);
39 /* PRC: a high-availability callout program can be specified with -H
40 * When this is done, the program will receive callouts whenever clients
41 * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
42 char *ha_callout_prog = NULL;
44 static struct option longopts[] =
46 { "foreground", 0, 0, 'F' },
47 { "descriptors", 1, 0, 'o' },
48 { "debug", 1, 0, 'd' },
49 { "help", 0, 0, 'h' },
50 { "exports-file", 1, 0, 'f' },
51 { "nfs-version", 1, 0, 'V' },
52 { "no-nfs-version", 1, 0, 'N' },
53 { "version", 0, 0, 'v' },
54 { "port", 1, 0, 'p' },
55 { "no-tcp", 0, 0, 'n' },
56 { "ha-callout", 1, 0, 'H' },
57 { "state-directory-path", 1, 0, 's' },
61 static int nfs_version = -1;
69 if (nfs_version & 0x1)
70 pmap_unset (MOUNTPROG, MOUNTVERS);
71 if (nfs_version & (0x1 << 1))
72 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
73 if (nfs_version & (0x1 << 2))
74 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
75 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
79 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
85 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
87 struct nfs_fh_len *fh;
89 xlog(D_CALL, "MNT1(%s) called", *path);
90 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
91 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
96 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
98 struct sockaddr_in *addr =
99 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
100 xlog(L_NOTICE, "dump request from %s",
101 inet_ntoa(addr->sin_addr));
103 *res = mountlist_list();
108 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
110 struct sockaddr_in *sin
111 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
114 char rpath[MAXPATHLEN+1];
119 if (realpath(p, rpath) != NULL) {
120 rpath[sizeof (rpath) - 1] = '\0';
124 if (!(exp = auth_authenticate("unmount", sin, p))) {
128 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
129 mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
130 mountlist_del(exp->m_client->m_hostname, p);
132 mountlist_del(exp->m_client->m_hostname, p);
139 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
141 /* Reload /etc/xtab if necessary */
144 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
149 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
151 struct sockaddr_in *addr =
152 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
153 xlog(L_NOTICE, "export request from %s",
154 inet_ntoa(addr->sin_addr));
155 *resp = get_exportlist();
160 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
162 struct sockaddr_in *addr =
163 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
164 xlog(L_NOTICE, "exportall request from %s",
165 inet_ntoa(addr->sin_addr));
166 *resp = get_exportlist();
171 * MNTv2 pathconf procedure
173 * The protocol doesn't include a status field, so Sun apparently considers
174 * it good practice to let anyone snoop on your system, even if it's
175 * pretty harmless data such as pathconf. We don't.
177 * Besides, many of the pathconf values don't make much sense on NFS volumes.
178 * FIFOs and tty device files represent devices on the *client*, so there's
179 * no point in getting the server's buffer sizes etc.
182 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
184 struct sockaddr_in *sin
185 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
188 char rpath[MAXPATHLEN+1];
191 memset(res, 0, sizeof(*res));
196 /* Reload /etc/xtab if necessary */
199 /* Resolve symlinks */
200 if (realpath(p, rpath) != NULL) {
201 rpath[sizeof (rpath) - 1] = '\0';
205 /* Now authenticate the intruder... */
206 if (!(exp = auth_authenticate("pathconf", sin, p))) {
208 } else if (stat(p, &stb) < 0) {
209 xlog(L_WARNING, "can't stat exported dir %s: %s",
215 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
216 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
217 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
218 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
219 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
220 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
221 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
223 /* Can't figure out what to do with pc_mask */
233 * NFSv3 MOUNT procedure
236 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
238 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
239 struct nfs_fh_len *fh;
241 xlog(D_CALL, "MNT3(%s) called", *path);
242 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
243 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
245 ok->fhandle.fhandle3_len = fh->fh_size;
246 ok->fhandle.fhandle3_val = fh->fh_handle;
247 ok->auth_flavors.auth_flavors_len = 2;
248 ok->auth_flavors.auth_flavors_val = flavors;
253 static struct nfs_fh_len *
254 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
256 struct sockaddr_in *sin =
257 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
258 struct stat stb, estb;
260 char rpath[MAXPATHLEN+1];
266 /* Reload /var/lib/nfs/etab if necessary */
269 /* Resolve symlinks */
270 if (realpath(p, rpath) != NULL) {
271 rpath[sizeof (rpath) - 1] = '\0';
275 /* Now authenticate the intruder... */
276 if (!(exp = auth_authenticate("mount", sin, p))) {
277 *error = NFSERR_ACCES;
278 } else if (stat(p, &stb) < 0) {
279 xlog(L_WARNING, "can't stat exported dir %s: %s",
282 *error = NFSERR_NOENT;
284 *error = NFSERR_ACCES;
285 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
286 xlog(L_WARNING, "%s is not a directory or regular file", p);
287 *error = NFSERR_NOTDIR;
288 } else if (stat(exp->m_export.e_path, &estb) < 0) {
289 xlog(L_WARNING, "can't stat export point %s: %s",
291 *error = NFSERR_NOENT;
292 } else if (estb.st_dev != stb.st_dev
293 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
295 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
296 p, exp->m_export.e_path);
297 *error = NFSERR_ACCES;
298 } else if (exp->m_export.e_mountpoint &&
299 !is_mountpoint(exp->m_export.e_mountpoint[0]?
300 exp->m_export.e_mountpoint:
301 exp->m_export.e_path)) {
302 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
304 *error = NFSERR_NOENT;
305 } else if (new_cache) {
306 /* This will be a static private nfs_export with just one
307 * address. We feed it to kernel then extract the filehandle,
310 struct nfs_fh_len *fh;
313 fh = cache_get_filehandle(exp, v3?64:32, p);
315 *error = NFSERR_ACCES;
320 struct nfs_fh_len *fh;
322 if (exp->m_exported<1)
328 fh = getfh_size ((struct sockaddr *) sin, p, 64);
329 if (!v3 || (fh == NULL && errno == EINVAL)) {
330 /* We first try the new nfs syscall. */
331 fh = getfh ((struct sockaddr *) sin, p);
332 if (fh == NULL && errno == EINVAL)
333 /* Let's try the old one. */
334 fh = getfh_old ((struct sockaddr *) sin,
335 stb.st_dev, stb.st_ino);
338 mountlist_add(exp->m_client->m_hostname, p);
343 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
344 *error = NFSERR_ACCES;
353 static exports elist = NULL;
354 struct exportnode *e, *ne;
355 struct groupnode *g, *ng, *c, **cp;
359 if (!auth_reload() && elist)
362 for (e = elist; e != NULL; e = ne) {
364 for (g = e->ex_groups; g != NULL; g = ng) {
374 for (i = 0; i < MCL_MAXTYPES; i++) {
375 for (exp = exportlist[i]; exp; exp = exp->m_next) {
376 for (e = elist; e != NULL; e = e->ex_next) {
377 if (!strcmp(exp->m_export.m_path, e->ex_dir))
381 e = (struct exportnode *) xmalloc(sizeof(*e));
384 e->ex_dir = xstrdup(exp->m_export.m_path);
388 /* We need to check if we should remove
390 if (i == MCL_ANONYMOUS && e->ex_groups) {
391 for (g = e->ex_groups; g; g = ng) {
400 if (i != MCL_FQDN && e->ex_groups) {
404 while ((c = *cp) != NULL) {
405 if (client_gettype (c->gr_name) == MCL_FQDN
406 && (hp = gethostbyname(c->gr_name))) {
407 hp = hostent_dup (hp);
408 if (client_check(exp->m_client, hp)) {
413 if ((c = *cp) == NULL)
423 if (exp->m_export.e_hostname [0] != '\0') {
424 for (g = e->ex_groups; g; g = g->gr_next)
425 if (strcmp (exp->m_export.e_hostname,
430 g = (struct groupnode *) xmalloc(sizeof(*g));
431 g->gr_name = xstrdup(exp->m_export.e_hostname);
432 g->gr_next = e->ex_groups;
442 main(int argc, char **argv)
444 char *export_file = _PATH_EXPORTS;
445 char *state_dir = NFS_STATEDIR;
453 /* Parse the command line options and arguments. */
455 while ((c = getopt_long(argc, argv, "o:n:Fd:f:p:P:hH:N:V:v", longopts, NULL)) != EOF)
458 descriptors = atoi(optarg);
459 if (descriptors <= 0) {
460 fprintf(stderr, "%s: bad descriptors: %s\n",
469 xlog_sconfig(optarg, 1);
472 export_file = optarg;
474 case 'H': /* PRC: specify a high-availability callout program */
475 ha_callout_prog = optarg;
480 case 'P': /* XXX for nfs-server compatibility */
483 if (port <= 0 || port > 65535) {
484 fprintf(stderr, "%s: bad port number: %s\n",
490 nfs_version &= ~(1 << (atoi (optarg) - 1));
493 _rpcfdtype = SOCK_DGRAM;
496 if ((state_dir = xstrdup(optarg)) == NULL) {
497 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
503 nfs_version |= 1 << (atoi (optarg) - 1);
506 printf("kmountd %s\n", VERSION);
515 /* No more arguments allowed. */
516 if (optind != argc || !(nfs_version & 0x7))
519 if (chdir(state_dir)) {
520 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
521 argv [0], state_dir, strerror(errno));
525 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
526 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
527 argv [0], strerror(errno));
529 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
530 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
531 descriptors > FD_SETSIZE)
532 descriptors = FD_SETSIZE;
534 rlim.rlim_cur = descriptors;
535 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
536 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
537 argv [0], strerror(errno));
542 /* Initialize logging. */
543 if (!foreground) xlog_stderr(0);
546 sa.sa_handler = SIG_IGN;
548 sigemptyset(&sa.sa_mask);
549 sigaction(SIGHUP, &sa, NULL);
550 sigaction(SIGINT, &sa, NULL);
551 sigaction(SIGTERM, &sa, NULL);
552 sigaction(SIGPIPE, &sa, NULL);
553 /* WARNING: the following works on Linux and SysV, but not BSD! */
554 sigaction(SIGCHLD, &sa, NULL);
556 /* Daemons should close all extra filehandles ... *before* RPC init. */
558 int fd = sysconf (_SC_OPEN_MAX);
563 new_cache = check_new_cache();
567 if (nfs_version & 0x1)
568 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
569 mount_dispatch, port);
570 if (nfs_version & (0x1 << 1))
571 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
572 mount_dispatch, port);
573 if (nfs_version & (0x1 << 2))
574 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
575 mount_dispatch, port);
577 sa.sa_handler = killer;
578 sigaction(SIGHUP, &sa, NULL);
579 sigaction(SIGINT, &sa, NULL);
580 sigaction(SIGTERM, &sa, NULL);
582 auth_init(export_file);
585 /* We first fork off a child. */
586 if ((c = fork()) > 0)
589 xlog(L_FATAL, "mountd: cannot fork: %s\n",
592 /* Now we remove ourselves from the foreground.
593 Redirect stdin/stdout/stderr first. */
595 int fd = open("/dev/null", O_RDWR);
599 if (fd > 2) (void) close(fd);
606 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
611 usage(const char *prog, int n)
614 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
615 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
616 " [-p|--port port] [-V version|--nfs-version version]\n"
617 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
618 " [-H ha-callout-prog] [-s|--state-directory-path path]\n", prog);