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' },
62 static int nfs_version = -1;
70 if (nfs_version & 0x1)
71 pmap_unset (MOUNTPROG, MOUNTVERS);
72 if (nfs_version & (0x1 << 1))
73 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
74 if (nfs_version & (0x1 << 2))
75 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
76 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
82 /* don't exit on SIGHUP */
83 xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
88 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
94 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
96 struct nfs_fh_len *fh;
98 xlog(D_CALL, "MNT1(%s) called", *path);
99 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
100 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
105 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
107 struct sockaddr_in *addr =
108 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
110 if ((*res = mountlist_list()) == NULL)
111 xlog(L_WARNING, "dump request from %s failed.",
112 inet_ntoa(addr->sin_addr));
118 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
120 struct sockaddr_in *sin
121 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
124 char rpath[MAXPATHLEN+1];
129 if (realpath(p, rpath) != NULL) {
130 rpath[sizeof (rpath) - 1] = '\0';
134 if (!(exp = auth_authenticate("unmount", sin, p))) {
138 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
139 mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
140 mountlist_del(exp->m_client->m_hostname, p);
142 mountlist_del(exp->m_client->m_hostname, p);
149 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
151 /* Reload /etc/xtab if necessary */
154 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
159 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
161 struct sockaddr_in *addr =
162 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
164 if ((*resp = get_exportlist()) == NULL)
165 xlog(L_WARNING, "export request from %s failed.",
166 inet_ntoa(addr->sin_addr));
172 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
174 struct sockaddr_in *addr =
175 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
177 if ((*resp = get_exportlist()) == NULL)
178 xlog(L_WARNING, "exportall request from %s failed.",
179 inet_ntoa(addr->sin_addr));
184 * MNTv2 pathconf procedure
186 * The protocol doesn't include a status field, so Sun apparently considers
187 * it good practice to let anyone snoop on your system, even if it's
188 * pretty harmless data such as pathconf. We don't.
190 * Besides, many of the pathconf values don't make much sense on NFS volumes.
191 * FIFOs and tty device files represent devices on the *client*, so there's
192 * no point in getting the server's buffer sizes etc.
195 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
197 struct sockaddr_in *sin
198 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
201 char rpath[MAXPATHLEN+1];
204 memset(res, 0, sizeof(*res));
209 /* Reload /etc/xtab if necessary */
212 /* Resolve symlinks */
213 if (realpath(p, rpath) != NULL) {
214 rpath[sizeof (rpath) - 1] = '\0';
218 /* Now authenticate the intruder... */
219 if (!(exp = auth_authenticate("pathconf", sin, p))) {
221 } else if (stat(p, &stb) < 0) {
222 xlog(L_WARNING, "can't stat exported dir %s: %s",
228 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
229 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
230 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
231 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
232 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
233 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
234 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
236 /* Can't figure out what to do with pc_mask */
246 * NFSv3 MOUNT procedure
249 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
251 #define AUTH_GSS_KRB5 390003
252 #define AUTH_GSS_KRB5I 390004
253 #define AUTH_GSS_KRB5P 390005
254 static int flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
255 struct nfs_fh_len *fh;
257 xlog(D_CALL, "MNT3(%s) called", *path);
258 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
259 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
261 ok->fhandle.fhandle3_len = fh->fh_size;
262 ok->fhandle.fhandle3_val = fh->fh_handle;
263 ok->auth_flavors.auth_flavors_len
264 = sizeof(flavors)/sizeof(flavors[0]);
265 ok->auth_flavors.auth_flavors_val = flavors;
270 static struct nfs_fh_len *
271 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
273 struct sockaddr_in *sin =
274 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
275 struct stat stb, estb;
277 char rpath[MAXPATHLEN+1];
283 /* Reload /var/lib/nfs/etab if necessary */
286 /* Resolve symlinks */
287 if (realpath(p, rpath) != NULL) {
288 rpath[sizeof (rpath) - 1] = '\0';
292 /* Now authenticate the intruder... */
293 if (!(exp = auth_authenticate("mount", sin, p))) {
294 *error = NFSERR_ACCES;
295 } else if (stat(p, &stb) < 0) {
296 xlog(L_WARNING, "can't stat exported dir %s: %s",
299 *error = NFSERR_NOENT;
301 *error = NFSERR_ACCES;
302 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
303 xlog(L_WARNING, "%s is not a directory or regular file", p);
304 *error = NFSERR_NOTDIR;
305 } else if (stat(exp->m_export.e_path, &estb) < 0) {
306 xlog(L_WARNING, "can't stat export point %s: %s",
308 *error = NFSERR_NOENT;
309 } else if (estb.st_dev != stb.st_dev
310 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
312 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
313 p, exp->m_export.e_path);
314 *error = NFSERR_ACCES;
315 } else if (exp->m_export.e_mountpoint &&
316 !is_mountpoint(exp->m_export.e_mountpoint[0]?
317 exp->m_export.e_mountpoint:
318 exp->m_export.e_path)) {
319 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
321 *error = NFSERR_NOENT;
322 } else if (new_cache) {
323 /* This will be a static private nfs_export with just one
324 * address. We feed it to kernel then extract the filehandle,
327 struct nfs_fh_len *fh;
330 fh = cache_get_filehandle(exp, v3?64:32, p);
332 *error = NFSERR_ACCES;
337 struct nfs_fh_len *fh;
339 if (exp->m_exported<1)
345 fh = getfh_size ((struct sockaddr *) sin, p, 64);
346 if (!v3 || (fh == NULL && errno == EINVAL)) {
347 /* We first try the new nfs syscall. */
348 fh = getfh ((struct sockaddr *) sin, p);
349 if (fh == NULL && errno == EINVAL)
350 /* Let's try the old one. */
351 fh = getfh_old ((struct sockaddr *) sin,
352 stb.st_dev, stb.st_ino);
355 mountlist_add(exp->m_client->m_hostname, p);
360 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
361 *error = NFSERR_ACCES;
370 static exports elist = NULL;
371 struct exportnode *e, *ne;
372 struct groupnode *g, *ng, *c, **cp;
376 if (!auth_reload() && elist)
379 for (e = elist; e != NULL; e = ne) {
381 for (g = e->ex_groups; g != NULL; g = ng) {
391 for (i = 0; i < MCL_MAXTYPES; i++) {
392 for (exp = exportlist[i]; exp; exp = exp->m_next) {
393 for (e = elist; e != NULL; e = e->ex_next) {
394 if (!strcmp(exp->m_export.m_path, e->ex_dir))
398 e = (struct exportnode *) xmalloc(sizeof(*e));
401 e->ex_dir = xstrdup(exp->m_export.m_path);
405 /* We need to check if we should remove
407 if (i == MCL_ANONYMOUS && e->ex_groups) {
408 for (g = e->ex_groups; g; g = ng) {
417 if (i != MCL_FQDN && e->ex_groups) {
421 while ((c = *cp) != NULL) {
422 if (client_gettype (c->gr_name) == MCL_FQDN
423 && (hp = gethostbyname(c->gr_name))) {
424 hp = hostent_dup (hp);
425 if (client_check(exp->m_client, hp)) {
438 if (exp->m_export.e_hostname [0] != '\0') {
439 for (g = e->ex_groups; g; g = g->gr_next)
440 if (strcmp (exp->m_export.e_hostname,
445 g = (struct groupnode *) xmalloc(sizeof(*g));
446 g->gr_name = xstrdup(exp->m_export.e_hostname);
447 g->gr_next = e->ex_groups;
457 main(int argc, char **argv)
459 char *export_file = _PATH_EXPORTS;
467 /* Parse the command line options and arguments. */
469 while ((c = getopt_long(argc, argv, "o:n:Fd:f:p:P:hH:N:V:v", longopts, NULL)) != EOF)
472 descriptors = atoi(optarg);
473 if (descriptors <= 0) {
474 fprintf(stderr, "%s: bad descriptors: %s\n",
483 xlog_sconfig(optarg, 1);
486 export_file = optarg;
488 case 'H': /* PRC: specify a high-availability callout program */
489 ha_callout_prog = optarg;
494 case 'P': /* XXX for nfs-server compatibility */
497 if (port <= 0 || port > 65535) {
498 fprintf(stderr, "%s: bad port number: %s\n",
504 nfs_version &= ~(1 << (atoi (optarg) - 1));
507 _rpcfdtype = SOCK_DGRAM;
510 nfs_version |= 1 << (atoi (optarg) - 1);
513 printf("kmountd %s\n", VERSION);
522 /* No more arguments allowed. */
523 if (optind != argc || !(nfs_version & 0x7))
526 if (chdir(NFS_STATEDIR)) {
527 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
528 argv [0], NFS_STATEDIR, strerror(errno));
532 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
533 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
534 argv [0], strerror(errno));
536 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
537 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
538 descriptors > FD_SETSIZE)
539 descriptors = FD_SETSIZE;
541 rlim.rlim_cur = descriptors;
542 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
543 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
544 argv [0], strerror(errno));
549 /* Initialize logging. */
550 if (!foreground) xlog_stderr(0);
553 sa.sa_handler = SIG_IGN;
555 sigemptyset(&sa.sa_mask);
556 sigaction(SIGHUP, &sa, NULL);
557 sigaction(SIGINT, &sa, NULL);
558 sigaction(SIGTERM, &sa, NULL);
559 sigaction(SIGPIPE, &sa, NULL);
560 /* WARNING: the following works on Linux and SysV, but not BSD! */
561 sigaction(SIGCHLD, &sa, NULL);
563 /* Daemons should close all extra filehandles ... *before* RPC init. */
567 new_cache = check_new_cache();
571 if (nfs_version & 0x1)
572 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
573 mount_dispatch, port);
574 if (nfs_version & (0x1 << 1))
575 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
576 mount_dispatch, port);
577 if (nfs_version & (0x1 << 2))
578 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
579 mount_dispatch, port);
581 sa.sa_handler = killer;
582 sigaction(SIGINT, &sa, NULL);
583 sigaction(SIGTERM, &sa, NULL);
584 sa.sa_handler = sig_hup;
585 sigaction(SIGHUP, &sa, NULL);
587 auth_init(export_file);
590 /* We first fork off a child. */
591 if ((c = fork()) > 0)
594 xlog(L_FATAL, "mountd: cannot fork: %s\n",
597 /* Now we remove ourselves from the foreground.
598 Redirect stdin/stdout/stderr first. */
600 int fd = open("/dev/null", O_RDWR);
604 if (fd > 2) (void) close(fd);
611 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
616 usage(const char *prog, int n)
619 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
620 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
621 " [-p|--port port] [-V version|--nfs-version version]\n"
622 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
623 " [-H ha-callout-prog]\n", prog);