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);
80 /* don't exit on SIGHUP */
81 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
86 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
92 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
94 struct nfs_fh_len *fh;
96 xlog(D_CALL, "MNT1(%s) called", *path);
97 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
98 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
103 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
105 struct sockaddr_in *addr =
106 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
108 if ((*res = mountlist_list()) == NULL)
109 xlog(L_WARNING, "dump request from %s failed.",
110 inet_ntoa(addr->sin_addr));
116 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
118 struct sockaddr_in *sin
119 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
122 char rpath[MAXPATHLEN+1];
127 if (realpath(p, rpath) != NULL) {
128 rpath[sizeof (rpath) - 1] = '\0';
132 if (!(exp = auth_authenticate("unmount", sin, p))) {
136 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
137 mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
138 mountlist_del(exp->m_client->m_hostname, p);
140 mountlist_del(exp->m_client->m_hostname, p);
147 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
149 /* Reload /etc/xtab if necessary */
152 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
157 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
159 struct sockaddr_in *addr =
160 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
162 if ((*resp = get_exportlist()) == NULL)
163 xlog(L_WARNING, "export request from %s failed.",
164 inet_ntoa(addr->sin_addr));
170 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
172 struct sockaddr_in *addr =
173 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
175 if ((*resp = get_exportlist()) == NULL)
176 xlog(L_WARNING, "exportall request from %s failed.",
177 inet_ntoa(addr->sin_addr));
182 * MNTv2 pathconf procedure
184 * The protocol doesn't include a status field, so Sun apparently considers
185 * it good practice to let anyone snoop on your system, even if it's
186 * pretty harmless data such as pathconf. We don't.
188 * Besides, many of the pathconf values don't make much sense on NFS volumes.
189 * FIFOs and tty device files represent devices on the *client*, so there's
190 * no point in getting the server's buffer sizes etc.
193 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
195 struct sockaddr_in *sin
196 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
199 char rpath[MAXPATHLEN+1];
202 memset(res, 0, sizeof(*res));
207 /* Reload /etc/xtab if necessary */
210 /* Resolve symlinks */
211 if (realpath(p, rpath) != NULL) {
212 rpath[sizeof (rpath) - 1] = '\0';
216 /* Now authenticate the intruder... */
217 if (!(exp = auth_authenticate("pathconf", sin, p))) {
219 } else if (stat(p, &stb) < 0) {
220 xlog(L_WARNING, "can't stat exported dir %s: %s",
226 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
227 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
228 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
229 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
230 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
231 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
232 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
234 /* Can't figure out what to do with pc_mask */
244 * NFSv3 MOUNT procedure
247 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
249 #define AUTH_GSS_KRB5 390003
250 #define AUTH_GSS_KRB5I 390004
251 #define AUTH_GSS_KRB5P 390005
252 static int flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
253 struct nfs_fh_len *fh;
255 xlog(D_CALL, "MNT3(%s) called", *path);
256 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
257 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
259 ok->fhandle.fhandle3_len = fh->fh_size;
260 ok->fhandle.fhandle3_val = fh->fh_handle;
261 ok->auth_flavors.auth_flavors_len
262 = sizeof(flavors)/sizeof(flavors[0]);
263 ok->auth_flavors.auth_flavors_val = flavors;
268 static struct nfs_fh_len *
269 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
271 struct sockaddr_in *sin =
272 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
273 struct stat stb, estb;
275 char rpath[MAXPATHLEN+1];
281 /* Reload /var/lib/nfs/etab if necessary */
284 /* Resolve symlinks */
285 if (realpath(p, rpath) != NULL) {
286 rpath[sizeof (rpath) - 1] = '\0';
290 /* Now authenticate the intruder... */
291 if (!(exp = auth_authenticate("mount", sin, p))) {
292 *error = NFSERR_ACCES;
293 } else if (stat(p, &stb) < 0) {
294 xlog(L_WARNING, "can't stat exported dir %s: %s",
297 *error = NFSERR_NOENT;
299 *error = NFSERR_ACCES;
300 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
301 xlog(L_WARNING, "%s is not a directory or regular file", p);
302 *error = NFSERR_NOTDIR;
303 } else if (stat(exp->m_export.e_path, &estb) < 0) {
304 xlog(L_WARNING, "can't stat export point %s: %s",
306 *error = NFSERR_NOENT;
307 } else if (estb.st_dev != stb.st_dev
308 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
310 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
311 p, exp->m_export.e_path);
312 *error = NFSERR_ACCES;
313 } else if (exp->m_export.e_mountpoint &&
314 !is_mountpoint(exp->m_export.e_mountpoint[0]?
315 exp->m_export.e_mountpoint:
316 exp->m_export.e_path)) {
317 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
319 *error = NFSERR_NOENT;
320 } else if (new_cache) {
321 /* This will be a static private nfs_export with just one
322 * address. We feed it to kernel then extract the filehandle,
325 struct nfs_fh_len *fh;
328 fh = cache_get_filehandle(exp, v3?64:32, p);
330 *error = NFSERR_ACCES;
335 struct nfs_fh_len *fh;
337 if (exp->m_exported<1)
343 fh = getfh_size ((struct sockaddr *) sin, p, 64);
344 if (!v3 || (fh == NULL && errno == EINVAL)) {
345 /* We first try the new nfs syscall. */
346 fh = getfh ((struct sockaddr *) sin, p);
347 if (fh == NULL && errno == EINVAL)
348 /* Let's try the old one. */
349 fh = getfh_old ((struct sockaddr *) sin,
350 stb.st_dev, stb.st_ino);
353 mountlist_add(exp->m_client->m_hostname, p);
358 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
359 *error = NFSERR_ACCES;
368 static exports elist = NULL;
369 struct exportnode *e, *ne;
370 struct groupnode *g, *ng, *c, **cp;
374 if (!auth_reload() && elist)
377 for (e = elist; e != NULL; e = ne) {
379 for (g = e->ex_groups; g != NULL; g = ng) {
389 for (i = 0; i < MCL_MAXTYPES; i++) {
390 for (exp = exportlist[i]; exp; exp = exp->m_next) {
391 for (e = elist; e != NULL; e = e->ex_next) {
392 if (!strcmp(exp->m_export.m_path, e->ex_dir))
396 e = (struct exportnode *) xmalloc(sizeof(*e));
399 e->ex_dir = xstrdup(exp->m_export.m_path);
403 /* We need to check if we should remove
405 if (i == MCL_ANONYMOUS && e->ex_groups) {
406 for (g = e->ex_groups; g; g = ng) {
415 if (i != MCL_FQDN && e->ex_groups) {
419 while ((c = *cp) != NULL) {
420 if (client_gettype (c->gr_name) == MCL_FQDN
421 && (hp = gethostbyname(c->gr_name))) {
422 hp = hostent_dup (hp);
423 if (client_check(exp->m_client, hp)) {
436 if (exp->m_export.e_hostname [0] != '\0') {
437 for (g = e->ex_groups; g; g = g->gr_next)
438 if (strcmp (exp->m_export.e_hostname,
443 g = (struct groupnode *) xmalloc(sizeof(*g));
444 g->gr_name = xstrdup(exp->m_export.e_hostname);
445 g->gr_next = e->ex_groups;
455 main(int argc, char **argv)
457 char *export_file = _PATH_EXPORTS;
465 /* Parse the command line options and arguments. */
467 while ((c = getopt_long(argc, argv, "o:n:Fd:f:p:P:hH:N:V:v", longopts, NULL)) != EOF)
470 descriptors = atoi(optarg);
471 if (descriptors <= 0) {
472 fprintf(stderr, "%s: bad descriptors: %s\n",
481 xlog_sconfig(optarg, 1);
484 export_file = optarg;
486 case 'H': /* PRC: specify a high-availability callout program */
487 ha_callout_prog = optarg;
492 case 'P': /* XXX for nfs-server compatibility */
495 if (port <= 0 || port > 65535) {
496 fprintf(stderr, "%s: bad port number: %s\n",
502 nfs_version &= ~(1 << (atoi (optarg) - 1));
505 _rpcfdtype = SOCK_DGRAM;
508 nfs_version |= 1 << (atoi (optarg) - 1);
511 printf("kmountd %s\n", VERSION);
520 /* No more arguments allowed. */
521 if (optind != argc || !(nfs_version & 0x7))
524 if (chdir(NFS_STATEDIR)) {
525 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
526 argv [0], NFS_STATEDIR, strerror(errno));
530 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
531 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
532 argv [0], strerror(errno));
534 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
535 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
536 descriptors > FD_SETSIZE)
537 descriptors = FD_SETSIZE;
539 rlim.rlim_cur = descriptors;
540 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
541 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
542 argv [0], strerror(errno));
547 /* Initialize logging. */
548 if (!foreground) xlog_stderr(0);
551 sa.sa_handler = SIG_IGN;
553 sigemptyset(&sa.sa_mask);
554 sigaction(SIGHUP, &sa, NULL);
555 sigaction(SIGINT, &sa, NULL);
556 sigaction(SIGTERM, &sa, NULL);
557 sigaction(SIGPIPE, &sa, NULL);
558 /* WARNING: the following works on Linux and SysV, but not BSD! */
559 sigaction(SIGCHLD, &sa, NULL);
561 /* Daemons should close all extra filehandles ... *before* RPC init. */
565 new_cache = check_new_cache();
569 if (nfs_version & 0x1)
570 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
571 mount_dispatch, port);
572 if (nfs_version & (0x1 << 1))
573 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
574 mount_dispatch, port);
575 if (nfs_version & (0x1 << 2))
576 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
577 mount_dispatch, port);
579 sa.sa_handler = killer;
580 sigaction(SIGINT, &sa, NULL);
581 sigaction(SIGTERM, &sa, NULL);
582 sa.sa_handler = sig_hup;
583 sigaction(SIGHUP, &sa, NULL);
585 auth_init(export_file);
588 /* We first fork off a child. */
589 if ((c = fork()) > 0)
592 xlog(L_FATAL, "mountd: cannot fork: %s\n",
595 /* Now we remove ourselves from the foreground.
596 Redirect stdin/stdout/stderr first. */
598 int fd = open("/dev/null", O_RDWR);
602 if (fd > 2) (void) close(fd);
609 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
614 usage(const char *prog, int n)
617 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
618 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
619 " [-p|--port port] [-V version|--nfs-version version]\n"
620 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
621 " [-H ha-callout-prog]\n", prog);