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 static void usage(const char *, int exitcode);
28 static exports get_exportlist(void);
29 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, int *, int v3);
31 static struct option longopts[] =
33 { "foreground", 0, 0, 'F' },
34 { "descriptors", 0, 0, 'o' },
35 { "debug", 1, 0, 'd' },
36 { "help", 0, 0, 'h' },
37 { "exports-file", 1, 0, 'f' },
38 { "nfs-version", 1, 0, 'V' },
39 { "no-nfs-version", 1, 0, 'N' },
40 { "version", 0, 0, 'v' },
41 { "port", 1, 0, 'p' },
42 { "no-tcp", 0, 0, 'n' },
46 static int nfs_version = -1;
54 if (nfs_version & 0x1)
55 pmap_unset (MOUNTPROG, MOUNTVERS);
56 if (nfs_version & (0x1 << 1))
57 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
58 if (nfs_version & (0x1 << 2))
59 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
60 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
64 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
70 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
72 struct nfs_fh_len *fh;
74 xlog(D_CALL, "MNT1(%s) called", *path);
75 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
76 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
81 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
83 struct sockaddr_in *addr =
84 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
85 xlog(L_NOTICE, "dump request from %s",
86 inet_ntoa(addr->sin_addr));
88 *res = mountlist_list();
93 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
95 struct sockaddr_in *sin
96 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
99 char rpath[MAXPATHLEN+1];
104 if (realpath(p, rpath) != NULL) {
105 rpath[sizeof (rpath) - 1] = '\0';
109 if (!(exp = auth_authenticate("unmount", sin, p))) {
112 mountlist_del(exp, p);
118 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
120 /* Reload /etc/xtab if necessary */
123 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
128 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
130 struct sockaddr_in *addr =
131 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
132 xlog(L_NOTICE, "export request from %s",
133 inet_ntoa(addr->sin_addr));
134 *resp = get_exportlist();
139 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
141 struct sockaddr_in *addr =
142 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
143 xlog(L_NOTICE, "exportall request from %s",
144 inet_ntoa(addr->sin_addr));
145 *resp = get_exportlist();
150 * MNTv2 pathconf procedure
152 * The protocol doesn't include a status field, so Sun apparently considers
153 * it good practice to let anyone snoop on your system, even if it's
154 * pretty harmless data such as pathconf. We don't.
156 * Besides, many of the pathconf values don't make much sense on NFS volumes.
157 * FIFOs and tty device files represent devices on the *client*, so there's
158 * no point in getting the server's buffer sizes etc.
161 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
163 struct sockaddr_in *sin
164 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
167 char rpath[MAXPATHLEN+1];
170 memset(res, 0, sizeof(*res));
175 /* Reload /etc/xtab if necessary */
178 /* Resolve symlinks */
179 if (realpath(p, rpath) != NULL) {
180 rpath[sizeof (rpath) - 1] = '\0';
184 /* Now authenticate the intruder... */
185 if (!(exp = auth_authenticate("mount", sin, p))) {
187 } else if (stat(p, &stb) < 0) {
188 xlog(L_WARNING, "can't stat exported dir %s: %s",
194 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
195 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
196 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
197 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
198 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
199 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
200 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
202 /* Can't figure out what to do with pc_mask */
212 * NFSv3 MOUNT procedure
215 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
217 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
218 struct nfs_fh_len *fh;
220 xlog(D_CALL, "MNT3(%s) called", *path);
221 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
222 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
224 ok->fhandle.fhandle3_len = fh->fh_size;
225 ok->fhandle.fhandle3_val = fh->fh_handle;
226 ok->auth_flavors.auth_flavors_len = 2;
227 ok->auth_flavors.auth_flavors_val = flavors;
232 static struct nfs_fh_len *
233 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
235 struct sockaddr_in *sin =
236 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
239 char rpath[MAXPATHLEN+1];
245 /* Reload /var/lib/nfs/etab if necessary */
248 /* Resolve symlinks */
249 if (realpath(p, rpath) != NULL) {
250 rpath[sizeof (rpath) - 1] = '\0';
254 /* Now authenticate the intruder... */
255 if (!(exp = auth_authenticate("mount", sin, p))) {
256 *error = NFSERR_ACCES;
257 } else if (stat(p, &stb) < 0) {
258 xlog(L_WARNING, "can't stat exported dir %s: %s",
261 *error = NFSERR_NOENT;
263 *error = NFSERR_ACCES;
264 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
265 xlog(L_WARNING, "%s is not a directory or regular file", p);
266 *error = NFSERR_NOTDIR;
268 struct nfs_fh_len *fh;
270 if (exp->m_exported<1)
276 fh = getfh_size ((struct sockaddr *) sin, p, 64);
277 if (!v3 || (fh == NULL && errno == EINVAL)) {
278 /* We first try the new nfs syscall. */
279 fh = getfh ((struct sockaddr *) sin, p);
280 if (fh == NULL && errno == EINVAL)
281 /* Let's try the old one. */
282 fh = getfh_old ((struct sockaddr *) sin,
283 stb.st_dev, stb.st_ino);
286 mountlist_add(exp, p);
291 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
292 *error = NFSERR_ACCES;
301 static exports elist = NULL;
302 struct exportnode *e, *ne;
303 struct groupnode *g, *ng, *c, **cp;
307 if (!auth_reload() && elist)
310 for (e = elist; e != NULL; e = ne) {
312 for (g = e->ex_groups; g != NULL; g = ng) {
322 for (i = 0; i < MCL_MAXTYPES; i++) {
323 for (exp = exportlist[i]; exp; exp = exp->m_next) {
324 for (e = elist; e != NULL; e = e->ex_next) {
325 if (!strcmp(exp->m_export.m_path, e->ex_dir))
329 e = (struct exportnode *) xmalloc(sizeof(*e));
332 e->ex_dir = xstrdup(exp->m_export.m_path);
336 /* We need to check if we should remove
338 if (i == MCL_ANONYMOUS && e->ex_groups) {
339 for (g = e->ex_groups; g; g = ng) {
348 if (i != MCL_FQDN && e->ex_groups) {
352 while ((c = *cp) != NULL) {
353 if (client_gettype (c->gr_name) == MCL_FQDN
354 && (hp = gethostbyname(c->gr_name))) {
355 hp = hostent_dup (hp);
356 if (client_check(exp->m_client, hp)) {
361 if ((c = *cp) == NULL)
371 if (exp->m_export.e_hostname [0] != '\0') {
372 for (g = e->ex_groups; g; g = g->gr_next)
373 if (strcmp (exp->m_export.e_hostname,
378 g = (struct groupnode *) xmalloc(sizeof(*g));
379 g->gr_name = xstrdup(exp->m_export.e_hostname);
380 g->gr_next = e->ex_groups;
390 main(int argc, char **argv)
392 char *export_file = _PATH_EXPORTS;
395 int descriptors = 256;
400 /* Parse the command line options and arguments. */
402 while ((c = getopt_long(argc, argv, "o:Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
405 descriptors = atoi(optarg);
406 if (descriptors <= 0) {
407 fprintf(stderr, "%s: bad descriptors: %s\n",
416 xlog_sconfig(optarg, 1);
419 export_file = optarg;
424 case 'P': /* XXX for nfs-server compatibility */
427 if (port <= 0 || port > 65535) {
428 fprintf(stderr, "%s: bad port number: %s\n",
434 nfs_version &= ~(1 << (atoi (optarg) - 1));
437 _rpcfdtype = SOCK_DGRAM;
440 nfs_version |= 1 << (atoi (optarg) - 1);
443 printf("kmountd %s\n", VERSION);
452 /* No more arguments allowed. */
453 if (optind != argc || !(nfs_version & 0x7))
456 if (chdir(NFS_STATEDIR)) {
457 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
458 argv [0], NFS_STATEDIR, strerror(errno));
462 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0) {
463 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
464 argv [0], strerror(errno));
468 rlim.rlim_cur = descriptors;
469 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
470 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
471 argv [0], strerror(errno));
475 /* Initialize logging. */
476 /* xlog_open("mountd"); */
478 sa.sa_handler = SIG_IGN;
480 sigemptyset(&sa.sa_mask);
481 sigaction(SIGHUP, &sa, NULL);
482 sigaction(SIGINT, &sa, NULL);
483 sigaction(SIGTERM, &sa, NULL);
484 /* WARNING: the following works on Linux and SysV, but not BSD! */
485 sigaction(SIGCHLD, &sa, NULL);
487 /* Daemons should close all extra filehandles ... *before* RPC init. */
489 int fd = sysconf (_SC_OPEN_MAX);
494 if (nfs_version & 0x1)
495 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
496 mount_dispatch, port);
497 if (nfs_version & (0x1 << 1))
498 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
499 mount_dispatch, port);
500 if (nfs_version & (0x1 << 2))
501 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
502 mount_dispatch, port);
504 sa.sa_handler = killer;
505 sigaction(SIGHUP, &sa, NULL);
506 sigaction(SIGINT, &sa, NULL);
507 sigaction(SIGTERM, &sa, NULL);
509 auth_init(export_file);
512 /* We first fork off a child. */
513 if ((c = fork()) > 0)
516 xlog(L_FATAL, "mountd: cannot fork: %s\n",
519 /* Now we remove ourselves from the foreground.
520 Redirect stdin/stdout/stderr first. */
522 int fd = open("/dev/null", O_RDWR);
526 if (fd > 2) (void) close(fd);
534 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
539 usage(const char *prog, int n)
542 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
543 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
544 " [-p|--port port] [-V version|--nfs-version version]\n"
545 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n", prog);