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 extern void cache_open(void);
30 extern struct nfs_fh_len *cache_get_filehandle(nfs_export *exp, int len, char *p);
31 extern void cache_export(nfs_export *exp);
33 extern void my_svc_run(void);
35 static void usage(const char *, int exitcode);
36 static exports get_exportlist(void);
37 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, int *, int v3);
41 /* PRC: a high-availability callout program can be specified with -H
42 * When this is done, the program will receive callouts whenever clients
43 * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
44 char *ha_callout_prog = NULL;
46 static struct option longopts[] =
48 { "foreground", 0, 0, 'F' },
49 { "descriptors", 1, 0, 'o' },
50 { "debug", 1, 0, 'd' },
51 { "help", 0, 0, 'h' },
52 { "exports-file", 1, 0, 'f' },
53 { "nfs-version", 1, 0, 'V' },
54 { "no-nfs-version", 1, 0, 'N' },
55 { "version", 0, 0, 'v' },
56 { "port", 1, 0, 'p' },
57 { "no-tcp", 0, 0, 'n' },
58 { "ha-callout", 1, 0, 'H' },
59 { "state-directory-path", 1, 0, 's' },
63 static int nfs_version = -1;
71 if (nfs_version & 0x1)
72 pmap_unset (MOUNTPROG, MOUNTVERS);
73 if (nfs_version & (0x1 << 1))
74 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
75 if (nfs_version & (0x1 << 2))
76 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
77 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
83 /* don't exit on SIGHUP */
84 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
89 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
95 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
97 struct nfs_fh_len *fh;
99 xlog(D_CALL, "MNT1(%s) called", *path);
100 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
101 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
106 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
108 struct sockaddr_in *addr =
109 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
111 if ((*res = mountlist_list()) == NULL)
112 xlog(L_WARNING, "dump request from %s failed.",
113 inet_ntoa(addr->sin_addr));
119 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
121 struct sockaddr_in *sin
122 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
125 char rpath[MAXPATHLEN+1];
130 if (realpath(p, rpath) != NULL) {
131 rpath[sizeof (rpath) - 1] = '\0';
135 if (!(exp = auth_authenticate("unmount", sin, p))) {
139 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
140 mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
141 mountlist_del(exp->m_client->m_hostname, p);
143 mountlist_del(exp->m_client->m_hostname, p);
150 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
152 /* Reload /etc/xtab if necessary */
155 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
160 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
162 struct sockaddr_in *addr =
163 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
165 if ((*resp = get_exportlist()) == NULL)
166 xlog(L_WARNING, "export request from %s failed.",
167 inet_ntoa(addr->sin_addr));
173 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
175 struct sockaddr_in *addr =
176 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
178 if ((*resp = get_exportlist()) == NULL)
179 xlog(L_WARNING, "exportall request from %s failed.",
180 inet_ntoa(addr->sin_addr));
185 * MNTv2 pathconf procedure
187 * The protocol doesn't include a status field, so Sun apparently considers
188 * it good practice to let anyone snoop on your system, even if it's
189 * pretty harmless data such as pathconf. We don't.
191 * Besides, many of the pathconf values don't make much sense on NFS volumes.
192 * FIFOs and tty device files represent devices on the *client*, so there's
193 * no point in getting the server's buffer sizes etc.
196 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
198 struct sockaddr_in *sin
199 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
202 char rpath[MAXPATHLEN+1];
205 memset(res, 0, sizeof(*res));
210 /* Reload /etc/xtab if necessary */
213 /* Resolve symlinks */
214 if (realpath(p, rpath) != NULL) {
215 rpath[sizeof (rpath) - 1] = '\0';
219 /* Now authenticate the intruder... */
220 if (!(exp = auth_authenticate("pathconf", sin, p))) {
222 } else if (stat(p, &stb) < 0) {
223 xlog(L_WARNING, "can't stat exported dir %s: %s",
229 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
230 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
231 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
232 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
233 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
234 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
235 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
237 /* Can't figure out what to do with pc_mask */
247 * NFSv3 MOUNT procedure
250 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
252 #define AUTH_GSS_KRB5 390003
253 #define AUTH_GSS_KRB5I 390004
254 #define AUTH_GSS_KRB5P 390005
255 static int flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
256 struct nfs_fh_len *fh;
258 xlog(D_CALL, "MNT3(%s) called", *path);
259 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
260 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
262 ok->fhandle.fhandle3_len = fh->fh_size;
263 ok->fhandle.fhandle3_val = fh->fh_handle;
264 ok->auth_flavors.auth_flavors_len
265 = sizeof(flavors)/sizeof(flavors[0]);
266 ok->auth_flavors.auth_flavors_val = flavors;
271 static struct nfs_fh_len *
272 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
274 struct sockaddr_in *sin =
275 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
276 struct stat stb, estb;
278 char rpath[MAXPATHLEN+1];
284 /* Reload /var/lib/nfs/etab if necessary */
287 /* Resolve symlinks */
288 if (realpath(p, rpath) != NULL) {
289 rpath[sizeof (rpath) - 1] = '\0';
293 /* Now authenticate the intruder... */
294 if (!(exp = auth_authenticate("mount", sin, p))) {
295 *error = NFSERR_ACCES;
296 } else if (stat(p, &stb) < 0) {
297 xlog(L_WARNING, "can't stat exported dir %s: %s",
300 *error = NFSERR_NOENT;
302 *error = NFSERR_ACCES;
303 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
304 xlog(L_WARNING, "%s is not a directory or regular file", p);
305 *error = NFSERR_NOTDIR;
306 } else if (stat(exp->m_export.e_path, &estb) < 0) {
307 xlog(L_WARNING, "can't stat export point %s: %s",
309 *error = NFSERR_NOENT;
310 } else if (estb.st_dev != stb.st_dev
311 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
313 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
314 p, exp->m_export.e_path);
315 *error = NFSERR_ACCES;
316 } else if (exp->m_export.e_mountpoint &&
317 !is_mountpoint(exp->m_export.e_mountpoint[0]?
318 exp->m_export.e_mountpoint:
319 exp->m_export.e_path)) {
320 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
322 *error = NFSERR_NOENT;
323 } else if (new_cache) {
324 /* This will be a static private nfs_export with just one
325 * address. We feed it to kernel then extract the filehandle,
328 struct nfs_fh_len *fh;
331 fh = cache_get_filehandle(exp, v3?64:32, p);
333 *error = NFSERR_ACCES;
338 struct nfs_fh_len *fh;
340 if (exp->m_exported<1)
346 fh = getfh_size ((struct sockaddr *) sin, p, 64);
347 if (!v3 || (fh == NULL && errno == EINVAL)) {
348 /* We first try the new nfs syscall. */
349 fh = getfh ((struct sockaddr *) sin, p);
350 if (fh == NULL && errno == EINVAL)
351 /* Let's try the old one. */
352 fh = getfh_old ((struct sockaddr *) sin,
353 stb.st_dev, stb.st_ino);
356 mountlist_add(exp->m_client->m_hostname, p);
361 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
362 *error = NFSERR_ACCES;
371 static exports elist = NULL;
372 struct exportnode *e, *ne;
373 struct groupnode *g, *ng, *c, **cp;
377 if (!auth_reload() && elist)
380 for (e = elist; e != NULL; e = ne) {
382 for (g = e->ex_groups; g != NULL; g = ng) {
392 for (i = 0; i < MCL_MAXTYPES; i++) {
393 for (exp = exportlist[i]; exp; exp = exp->m_next) {
394 for (e = elist; e != NULL; e = e->ex_next) {
395 if (!strcmp(exp->m_export.m_path, e->ex_dir))
399 e = (struct exportnode *) xmalloc(sizeof(*e));
402 e->ex_dir = xstrdup(exp->m_export.m_path);
406 /* We need to check if we should remove
408 if (i == MCL_ANONYMOUS && e->ex_groups) {
409 for (g = e->ex_groups; g; g = ng) {
418 if (i != MCL_FQDN && e->ex_groups) {
422 while ((c = *cp) != NULL) {
423 if (client_gettype (c->gr_name) == MCL_FQDN
424 && (hp = gethostbyname(c->gr_name))) {
425 hp = hostent_dup (hp);
426 if (client_check(exp->m_client, hp)) {
439 if (exp->m_export.e_hostname [0] != '\0') {
440 for (g = e->ex_groups; g; g = g->gr_next)
441 if (strcmp (exp->m_export.e_hostname,
446 g = (struct groupnode *) xmalloc(sizeof(*g));
447 g->gr_name = xstrdup(exp->m_export.e_hostname);
448 g->gr_next = e->ex_groups;
458 main(int argc, char **argv)
460 char *export_file = _PATH_EXPORTS;
461 char *state_dir = NFS_STATEDIR;
469 /* Parse the command line options and arguments. */
471 while ((c = getopt_long(argc, argv, "o:n:Fd:f:p:P:hH:N:V:vs:", longopts, NULL)) != EOF)
474 descriptors = atoi(optarg);
475 if (descriptors <= 0) {
476 fprintf(stderr, "%s: bad descriptors: %s\n",
485 xlog_sconfig(optarg, 1);
488 export_file = optarg;
490 case 'H': /* PRC: specify a high-availability callout program */
491 ha_callout_prog = optarg;
496 case 'P': /* XXX for nfs-server compatibility */
499 if (port <= 0 || port > 65535) {
500 fprintf(stderr, "%s: bad port number: %s\n",
506 nfs_version &= ~(1 << (atoi (optarg) - 1));
509 _rpcfdtype = SOCK_DGRAM;
512 if ((state_dir = xstrdup(optarg)) == NULL) {
513 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
519 nfs_version |= 1 << (atoi (optarg) - 1);
522 printf("kmountd %s\n", VERSION);
531 /* No more arguments allowed. */
532 if (optind != argc || !(nfs_version & 0x7))
535 if (chdir(state_dir)) {
536 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
537 argv [0], state_dir, strerror(errno));
541 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
542 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
543 argv [0], strerror(errno));
545 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
546 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
547 descriptors > FD_SETSIZE)
548 descriptors = FD_SETSIZE;
550 rlim.rlim_cur = descriptors;
551 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
552 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
553 argv [0], strerror(errno));
558 /* Initialize logging. */
559 if (!foreground) xlog_stderr(0);
562 sa.sa_handler = SIG_IGN;
564 sigemptyset(&sa.sa_mask);
565 sigaction(SIGHUP, &sa, NULL);
566 sigaction(SIGINT, &sa, NULL);
567 sigaction(SIGTERM, &sa, NULL);
568 sigaction(SIGPIPE, &sa, NULL);
569 /* WARNING: the following works on Linux and SysV, but not BSD! */
570 sigaction(SIGCHLD, &sa, NULL);
572 /* Daemons should close all extra filehandles ... *before* RPC init. */
576 new_cache = check_new_cache();
580 if (nfs_version & 0x1)
581 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
582 mount_dispatch, port);
583 if (nfs_version & (0x1 << 1))
584 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
585 mount_dispatch, port);
586 if (nfs_version & (0x1 << 2))
587 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
588 mount_dispatch, port);
590 sa.sa_handler = killer;
591 sigaction(SIGINT, &sa, NULL);
592 sigaction(SIGTERM, &sa, NULL);
593 sa.sa_handler = sig_hup;
594 sigaction(SIGHUP, &sa, NULL);
596 auth_init(export_file);
599 /* We first fork off a child. */
600 if ((c = fork()) > 0)
603 xlog(L_FATAL, "mountd: cannot fork: %s\n",
606 /* Now we remove ourselves from the foreground.
607 Redirect stdin/stdout/stderr first. */
609 int fd = open("/dev/null", O_RDWR);
613 if (fd > 2) (void) close(fd);
620 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
625 usage(const char *prog, int n)
628 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
629 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
630 " [-p|--port port] [-V version|--nfs-version version]\n"
631 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
632 " [-H ha-callout-prog] [-s|--state-directory-path path]\n", prog);