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' },
60 static int nfs_version = -1;
68 if (nfs_version & 0x1)
69 pmap_unset (MOUNTPROG, MOUNTVERS);
70 if (nfs_version & (0x1 << 1))
71 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
72 if (nfs_version & (0x1 << 2))
73 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
74 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
78 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
84 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
86 struct nfs_fh_len *fh;
88 xlog(D_CALL, "MNT1(%s) called", *path);
89 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
90 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
95 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
97 struct sockaddr_in *addr =
98 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
99 xlog(L_NOTICE, "dump request from %s",
100 inet_ntoa(addr->sin_addr));
102 *res = mountlist_list();
107 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
109 struct sockaddr_in *sin
110 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
113 char rpath[MAXPATHLEN+1];
118 if (realpath(p, rpath) != NULL) {
119 rpath[sizeof (rpath) - 1] = '\0';
123 if (!(exp = auth_authenticate("unmount", sin, p))) {
127 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
128 mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
129 mountlist_del(exp->m_client->m_hostname, p);
131 mountlist_del(exp->m_client->m_hostname, p);
138 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
140 /* Reload /etc/xtab if necessary */
143 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
148 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
150 struct sockaddr_in *addr =
151 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
152 xlog(L_NOTICE, "export request from %s",
153 inet_ntoa(addr->sin_addr));
154 *resp = get_exportlist();
159 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
161 struct sockaddr_in *addr =
162 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
163 xlog(L_NOTICE, "exportall request from %s",
164 inet_ntoa(addr->sin_addr));
165 *resp = get_exportlist();
170 * MNTv2 pathconf procedure
172 * The protocol doesn't include a status field, so Sun apparently considers
173 * it good practice to let anyone snoop on your system, even if it's
174 * pretty harmless data such as pathconf. We don't.
176 * Besides, many of the pathconf values don't make much sense on NFS volumes.
177 * FIFOs and tty device files represent devices on the *client*, so there's
178 * no point in getting the server's buffer sizes etc.
181 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
183 struct sockaddr_in *sin
184 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
187 char rpath[MAXPATHLEN+1];
190 memset(res, 0, sizeof(*res));
195 /* Reload /etc/xtab if necessary */
198 /* Resolve symlinks */
199 if (realpath(p, rpath) != NULL) {
200 rpath[sizeof (rpath) - 1] = '\0';
204 /* Now authenticate the intruder... */
205 if (!(exp = auth_authenticate("pathconf", sin, p))) {
207 } else if (stat(p, &stb) < 0) {
208 xlog(L_WARNING, "can't stat exported dir %s: %s",
214 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
215 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
216 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
217 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
218 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
219 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
220 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
222 /* Can't figure out what to do with pc_mask */
232 * NFSv3 MOUNT procedure
235 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
237 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
238 struct nfs_fh_len *fh;
240 xlog(D_CALL, "MNT3(%s) called", *path);
241 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
242 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
244 ok->fhandle.fhandle3_len = fh->fh_size;
245 ok->fhandle.fhandle3_val = fh->fh_handle;
246 ok->auth_flavors.auth_flavors_len = 2;
247 ok->auth_flavors.auth_flavors_val = flavors;
252 static struct nfs_fh_len *
253 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
255 struct sockaddr_in *sin =
256 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
257 struct stat stb, estb;
259 char rpath[MAXPATHLEN+1];
265 /* Reload /var/lib/nfs/etab if necessary */
268 /* Resolve symlinks */
269 if (realpath(p, rpath) != NULL) {
270 rpath[sizeof (rpath) - 1] = '\0';
274 /* Now authenticate the intruder... */
275 if (!(exp = auth_authenticate("mount", sin, p))) {
276 *error = NFSERR_ACCES;
277 } else if (stat(p, &stb) < 0) {
278 xlog(L_WARNING, "can't stat exported dir %s: %s",
281 *error = NFSERR_NOENT;
283 *error = NFSERR_ACCES;
284 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
285 xlog(L_WARNING, "%s is not a directory or regular file", p);
286 *error = NFSERR_NOTDIR;
287 } else if (stat(exp->m_export.e_path, &estb) < 0) {
288 xlog(L_WARNING, "can't stat export point %s: %s",
290 *error = NFSERR_NOENT;
291 } else if (estb.st_dev != stb.st_dev
292 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
294 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
295 p, exp->m_export.e_path);
296 *error = NFSERR_ACCES;
297 } else if (exp->m_export.e_mountpoint &&
298 !is_mountpoint(exp->m_export.e_mountpoint[0]?
299 exp->m_export.e_mountpoint:
300 exp->m_export.e_path)) {
301 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
303 *error = NFSERR_NOENT;
304 } else if (new_cache) {
305 /* This will be a static private nfs_export with just one
306 * address. We feed it to kernel then extract the filehandle,
309 struct nfs_fh_len *fh;
312 fh = cache_get_filehandle(exp, v3?64:32, p);
314 *error = NFSERR_ACCES;
319 struct nfs_fh_len *fh;
321 if (exp->m_exported<1)
327 fh = getfh_size ((struct sockaddr *) sin, p, 64);
328 if (!v3 || (fh == NULL && errno == EINVAL)) {
329 /* We first try the new nfs syscall. */
330 fh = getfh ((struct sockaddr *) sin, p);
331 if (fh == NULL && errno == EINVAL)
332 /* Let's try the old one. */
333 fh = getfh_old ((struct sockaddr *) sin,
334 stb.st_dev, stb.st_ino);
337 mountlist_add(exp->m_client->m_hostname, p);
342 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
343 *error = NFSERR_ACCES;
352 static exports elist = NULL;
353 struct exportnode *e, *ne;
354 struct groupnode *g, *ng, *c, **cp;
358 if (!auth_reload() && elist)
361 for (e = elist; e != NULL; e = ne) {
363 for (g = e->ex_groups; g != NULL; g = ng) {
373 for (i = 0; i < MCL_MAXTYPES; i++) {
374 for (exp = exportlist[i]; exp; exp = exp->m_next) {
375 for (e = elist; e != NULL; e = e->ex_next) {
376 if (!strcmp(exp->m_export.m_path, e->ex_dir))
380 e = (struct exportnode *) xmalloc(sizeof(*e));
383 e->ex_dir = xstrdup(exp->m_export.m_path);
387 /* We need to check if we should remove
389 if (i == MCL_ANONYMOUS && e->ex_groups) {
390 for (g = e->ex_groups; g; g = ng) {
399 if (i != MCL_FQDN && e->ex_groups) {
403 while ((c = *cp) != NULL) {
404 if (client_gettype (c->gr_name) == MCL_FQDN
405 && (hp = gethostbyname(c->gr_name))) {
406 hp = hostent_dup (hp);
407 if (client_check(exp->m_client, hp)) {
412 if ((c = *cp) == NULL)
422 if (exp->m_export.e_hostname [0] != '\0') {
423 for (g = e->ex_groups; g; g = g->gr_next)
424 if (strcmp (exp->m_export.e_hostname,
429 g = (struct groupnode *) xmalloc(sizeof(*g));
430 g->gr_name = xstrdup(exp->m_export.e_hostname);
431 g->gr_next = e->ex_groups;
441 main(int argc, char **argv)
443 char *export_file = _PATH_EXPORTS;
451 /* Parse the command line options and arguments. */
453 while ((c = getopt_long(argc, argv, "o:n:Fd:f:p:P:hH:N:V:v", longopts, NULL)) != EOF)
456 descriptors = atoi(optarg);
457 if (descriptors <= 0) {
458 fprintf(stderr, "%s: bad descriptors: %s\n",
467 xlog_sconfig(optarg, 1);
470 export_file = optarg;
472 case 'H': /* PRC: specify a high-availability callout program */
473 ha_callout_prog = optarg;
478 case 'P': /* XXX for nfs-server compatibility */
481 if (port <= 0 || port > 65535) {
482 fprintf(stderr, "%s: bad port number: %s\n",
488 nfs_version &= ~(1 << (atoi (optarg) - 1));
491 _rpcfdtype = SOCK_DGRAM;
494 nfs_version |= 1 << (atoi (optarg) - 1);
497 printf("kmountd %s\n", VERSION);
506 /* No more arguments allowed. */
507 if (optind != argc || !(nfs_version & 0x7))
510 if (chdir(NFS_STATEDIR)) {
511 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
512 argv [0], NFS_STATEDIR, strerror(errno));
516 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
517 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
518 argv [0], strerror(errno));
520 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
521 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
522 descriptors > FD_SETSIZE)
523 descriptors = FD_SETSIZE;
525 rlim.rlim_cur = descriptors;
526 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
527 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
528 argv [0], strerror(errno));
533 /* Initialize logging. */
534 if (!foreground) xlog_stderr(0);
537 sa.sa_handler = SIG_IGN;
539 sigemptyset(&sa.sa_mask);
540 sigaction(SIGHUP, &sa, NULL);
541 sigaction(SIGINT, &sa, NULL);
542 sigaction(SIGTERM, &sa, NULL);
543 sigaction(SIGPIPE, &sa, NULL);
544 /* WARNING: the following works on Linux and SysV, but not BSD! */
545 sigaction(SIGCHLD, &sa, NULL);
547 /* Daemons should close all extra filehandles ... *before* RPC init. */
549 int fd = sysconf (_SC_OPEN_MAX);
554 new_cache = check_new_cache();
558 if (nfs_version & 0x1)
559 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
560 mount_dispatch, port);
561 if (nfs_version & (0x1 << 1))
562 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
563 mount_dispatch, port);
564 if (nfs_version & (0x1 << 2))
565 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
566 mount_dispatch, port);
568 sa.sa_handler = killer;
569 sigaction(SIGHUP, &sa, NULL);
570 sigaction(SIGINT, &sa, NULL);
571 sigaction(SIGTERM, &sa, NULL);
573 auth_init(export_file);
576 /* We first fork off a child. */
577 if ((c = fork()) > 0)
580 xlog(L_FATAL, "mountd: cannot fork: %s\n",
583 /* Now we remove ourselves from the foreground.
584 Redirect stdin/stdout/stderr first. */
586 int fd = open("/dev/null", O_RDWR);
590 if (fd > 2) (void) close(fd);
597 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
602 usage(const char *prog, int n)
605 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
606 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
607 " [-p|--port port] [-V version|--nfs-version version]\n"
608 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
609 " [-H ha-callout-prog]\n", prog);