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 static struct option longopts[] =
41 { "foreground", 0, 0, 'F' },
42 { "descriptors", 1, 0, 'o' },
43 { "debug", 1, 0, 'd' },
44 { "help", 0, 0, 'h' },
45 { "exports-file", 1, 0, 'f' },
46 { "nfs-version", 1, 0, 'V' },
47 { "no-nfs-version", 1, 0, 'N' },
48 { "version", 0, 0, 'v' },
49 { "port", 1, 0, 'p' },
50 { "no-tcp", 0, 0, 'n' },
54 static int nfs_version = -1;
62 if (nfs_version & 0x1)
63 pmap_unset (MOUNTPROG, MOUNTVERS);
64 if (nfs_version & (0x1 << 1))
65 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
66 if (nfs_version & (0x1 << 2))
67 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
68 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
72 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
78 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
80 struct nfs_fh_len *fh;
82 xlog(D_CALL, "MNT1(%s) called", *path);
83 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
84 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
89 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
91 struct sockaddr_in *addr =
92 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
93 xlog(L_NOTICE, "dump request from %s",
94 inet_ntoa(addr->sin_addr));
96 *res = mountlist_list();
101 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
103 struct sockaddr_in *sin
104 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
107 char rpath[MAXPATHLEN+1];
112 if (realpath(p, rpath) != NULL) {
113 rpath[sizeof (rpath) - 1] = '\0';
117 if (!(exp = auth_authenticate("unmount", sin, p))) {
121 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
122 mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
123 mountlist_del(exp->m_client->m_hostname, p);
125 mountlist_del(exp->m_client->m_hostname, p);
132 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
134 /* Reload /etc/xtab if necessary */
137 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
142 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
144 struct sockaddr_in *addr =
145 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
146 xlog(L_NOTICE, "export request from %s",
147 inet_ntoa(addr->sin_addr));
148 *resp = get_exportlist();
153 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
155 struct sockaddr_in *addr =
156 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
157 xlog(L_NOTICE, "exportall request from %s",
158 inet_ntoa(addr->sin_addr));
159 *resp = get_exportlist();
164 * MNTv2 pathconf procedure
166 * The protocol doesn't include a status field, so Sun apparently considers
167 * it good practice to let anyone snoop on your system, even if it's
168 * pretty harmless data such as pathconf. We don't.
170 * Besides, many of the pathconf values don't make much sense on NFS volumes.
171 * FIFOs and tty device files represent devices on the *client*, so there's
172 * no point in getting the server's buffer sizes etc.
175 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
177 struct sockaddr_in *sin
178 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
181 char rpath[MAXPATHLEN+1];
184 memset(res, 0, sizeof(*res));
189 /* Reload /etc/xtab if necessary */
192 /* Resolve symlinks */
193 if (realpath(p, rpath) != NULL) {
194 rpath[sizeof (rpath) - 1] = '\0';
198 /* Now authenticate the intruder... */
199 if (!(exp = auth_authenticate("pathconf", sin, p))) {
201 } else if (stat(p, &stb) < 0) {
202 xlog(L_WARNING, "can't stat exported dir %s: %s",
208 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
209 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
210 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
211 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
212 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
213 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
214 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
216 /* Can't figure out what to do with pc_mask */
226 * NFSv3 MOUNT procedure
229 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
231 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
232 struct nfs_fh_len *fh;
234 xlog(D_CALL, "MNT3(%s) called", *path);
235 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
236 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
238 ok->fhandle.fhandle3_len = fh->fh_size;
239 ok->fhandle.fhandle3_val = fh->fh_handle;
240 ok->auth_flavors.auth_flavors_len = 2;
241 ok->auth_flavors.auth_flavors_val = flavors;
246 static struct nfs_fh_len *
247 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
249 struct sockaddr_in *sin =
250 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
251 struct stat stb, estb;
253 char rpath[MAXPATHLEN+1];
259 /* Reload /var/lib/nfs/etab if necessary */
262 /* Resolve symlinks */
263 if (realpath(p, rpath) != NULL) {
264 rpath[sizeof (rpath) - 1] = '\0';
268 /* Now authenticate the intruder... */
269 if (!(exp = auth_authenticate("mount", sin, p))) {
270 *error = NFSERR_ACCES;
271 } else if (stat(p, &stb) < 0) {
272 xlog(L_WARNING, "can't stat exported dir %s: %s",
275 *error = NFSERR_NOENT;
277 *error = NFSERR_ACCES;
278 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
279 xlog(L_WARNING, "%s is not a directory or regular file", p);
280 *error = NFSERR_NOTDIR;
281 } else if (stat(exp->m_export.e_path, &estb) < 0) {
282 xlog(L_WARNING, "can't stat export point %s: %s",
284 *error = NFSERR_NOENT;
285 } else if (estb.st_dev != stb.st_dev
286 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
288 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
289 p, exp->m_export.e_path);
290 *error = NFSERR_ACCES;
291 } else if (exp->m_export.e_mountpoint &&
292 !is_mountpoint(exp->m_export.e_mountpoint[0]?
293 exp->m_export.e_mountpoint:
294 exp->m_export.e_path)) {
295 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
297 *error = NFSERR_NOENT;
298 } else if (new_cache) {
299 /* This will be a static private nfs_export with just one
300 * address. We feed it to kernel then extract the filehandle,
303 struct nfs_fh_len *fh;
306 fh = cache_get_filehandle(exp, v3?64:32, p);
308 *error = NFSERR_ACCES;
313 struct nfs_fh_len *fh;
315 if (exp->m_exported<1)
321 fh = getfh_size ((struct sockaddr *) sin, p, 64);
322 if (!v3 || (fh == NULL && errno == EINVAL)) {
323 /* We first try the new nfs syscall. */
324 fh = getfh ((struct sockaddr *) sin, p);
325 if (fh == NULL && errno == EINVAL)
326 /* Let's try the old one. */
327 fh = getfh_old ((struct sockaddr *) sin,
328 stb.st_dev, stb.st_ino);
331 mountlist_add(exp->m_client->m_hostname, p);
336 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
337 *error = NFSERR_ACCES;
346 static exports elist = NULL;
347 struct exportnode *e, *ne;
348 struct groupnode *g, *ng, *c, **cp;
352 if (!auth_reload() && elist)
355 for (e = elist; e != NULL; e = ne) {
357 for (g = e->ex_groups; g != NULL; g = ng) {
367 for (i = 0; i < MCL_MAXTYPES; i++) {
368 for (exp = exportlist[i]; exp; exp = exp->m_next) {
369 for (e = elist; e != NULL; e = e->ex_next) {
370 if (!strcmp(exp->m_export.m_path, e->ex_dir))
374 e = (struct exportnode *) xmalloc(sizeof(*e));
377 e->ex_dir = xstrdup(exp->m_export.m_path);
381 /* We need to check if we should remove
383 if (i == MCL_ANONYMOUS && e->ex_groups) {
384 for (g = e->ex_groups; g; g = ng) {
393 if (i != MCL_FQDN && e->ex_groups) {
397 while ((c = *cp) != NULL) {
398 if (client_gettype (c->gr_name) == MCL_FQDN
399 && (hp = gethostbyname(c->gr_name))) {
400 hp = hostent_dup (hp);
401 if (client_check(exp->m_client, hp)) {
406 if ((c = *cp) == NULL)
416 if (exp->m_export.e_hostname [0] != '\0') {
417 for (g = e->ex_groups; g; g = g->gr_next)
418 if (strcmp (exp->m_export.e_hostname,
423 g = (struct groupnode *) xmalloc(sizeof(*g));
424 g->gr_name = xstrdup(exp->m_export.e_hostname);
425 g->gr_next = e->ex_groups;
435 main(int argc, char **argv)
437 char *export_file = _PATH_EXPORTS;
445 /* Parse the command line options and arguments. */
447 while ((c = getopt_long(argc, argv, "o:n:Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
450 descriptors = atoi(optarg);
451 if (descriptors <= 0) {
452 fprintf(stderr, "%s: bad descriptors: %s\n",
461 xlog_sconfig(optarg, 1);
464 export_file = optarg;
469 case 'P': /* XXX for nfs-server compatibility */
472 if (port <= 0 || port > 65535) {
473 fprintf(stderr, "%s: bad port number: %s\n",
479 nfs_version &= ~(1 << (atoi (optarg) - 1));
482 _rpcfdtype = SOCK_DGRAM;
485 nfs_version |= 1 << (atoi (optarg) - 1);
488 printf("kmountd %s\n", VERSION);
497 /* No more arguments allowed. */
498 if (optind != argc || !(nfs_version & 0x7))
501 if (chdir(NFS_STATEDIR)) {
502 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
503 argv [0], NFS_STATEDIR, strerror(errno));
507 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
508 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
509 argv [0], strerror(errno));
511 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
512 if (descriptors == 0 && rlim.rlim_cur > FD_SETSIZE)
513 descriptors = FD_SETSIZE;
516 rlim.rlim_cur = descriptors;
517 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
518 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
519 argv [0], strerror(errno));
524 /* Initialize logging. */
525 /* xlog_open("mountd"); */
527 sa.sa_handler = SIG_IGN;
529 sigemptyset(&sa.sa_mask);
530 sigaction(SIGHUP, &sa, NULL);
531 sigaction(SIGINT, &sa, NULL);
532 sigaction(SIGTERM, &sa, NULL);
533 sigaction(SIGPIPE, &sa, NULL);
534 /* WARNING: the following works on Linux and SysV, but not BSD! */
535 sigaction(SIGCHLD, &sa, NULL);
537 /* Daemons should close all extra filehandles ... *before* RPC init. */
539 int fd = sysconf (_SC_OPEN_MAX);
544 new_cache = check_new_cache();
548 if (nfs_version & 0x1)
549 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
550 mount_dispatch, port);
551 if (nfs_version & (0x1 << 1))
552 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
553 mount_dispatch, port);
554 if (nfs_version & (0x1 << 2))
555 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
556 mount_dispatch, port);
558 sa.sa_handler = killer;
559 sigaction(SIGHUP, &sa, NULL);
560 sigaction(SIGINT, &sa, NULL);
561 sigaction(SIGTERM, &sa, NULL);
563 auth_init(export_file);
566 /* We first fork off a child. */
567 if ((c = fork()) > 0)
570 xlog(L_FATAL, "mountd: cannot fork: %s\n",
573 /* Now we remove ourselves from the foreground.
574 Redirect stdin/stdout/stderr first. */
576 int fd = open("/dev/null", O_RDWR);
580 if (fd > 2) (void) close(fd);
588 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
593 usage(const char *prog, int n)
596 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
597 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
598 " [-p|--port port] [-V version|--nfs-version version]\n"
599 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n", prog);