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))) {
120 mountlist_del(exp, p);
126 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
128 /* Reload /etc/xtab if necessary */
131 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
136 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
138 struct sockaddr_in *addr =
139 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
140 xlog(L_NOTICE, "export request from %s",
141 inet_ntoa(addr->sin_addr));
142 *resp = get_exportlist();
147 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
149 struct sockaddr_in *addr =
150 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
151 xlog(L_NOTICE, "exportall request from %s",
152 inet_ntoa(addr->sin_addr));
153 *resp = get_exportlist();
158 * MNTv2 pathconf procedure
160 * The protocol doesn't include a status field, so Sun apparently considers
161 * it good practice to let anyone snoop on your system, even if it's
162 * pretty harmless data such as pathconf. We don't.
164 * Besides, many of the pathconf values don't make much sense on NFS volumes.
165 * FIFOs and tty device files represent devices on the *client*, so there's
166 * no point in getting the server's buffer sizes etc.
169 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
171 struct sockaddr_in *sin
172 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
175 char rpath[MAXPATHLEN+1];
178 memset(res, 0, sizeof(*res));
183 /* Reload /etc/xtab if necessary */
186 /* Resolve symlinks */
187 if (realpath(p, rpath) != NULL) {
188 rpath[sizeof (rpath) - 1] = '\0';
192 /* Now authenticate the intruder... */
193 if (!(exp = auth_authenticate("pathconf", sin, p))) {
195 } else if (stat(p, &stb) < 0) {
196 xlog(L_WARNING, "can't stat exported dir %s: %s",
202 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
203 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
204 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
205 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
206 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
207 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
208 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
210 /* Can't figure out what to do with pc_mask */
220 * NFSv3 MOUNT procedure
223 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
225 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
226 struct nfs_fh_len *fh;
228 xlog(D_CALL, "MNT3(%s) called", *path);
229 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
230 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
232 ok->fhandle.fhandle3_len = fh->fh_size;
233 ok->fhandle.fhandle3_val = fh->fh_handle;
234 ok->auth_flavors.auth_flavors_len = 2;
235 ok->auth_flavors.auth_flavors_val = flavors;
240 static struct nfs_fh_len *
241 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
243 struct sockaddr_in *sin =
244 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
245 struct stat stb, estb;
247 char rpath[MAXPATHLEN+1];
253 /* Reload /var/lib/nfs/etab if necessary */
256 /* Resolve symlinks */
257 if (realpath(p, rpath) != NULL) {
258 rpath[sizeof (rpath) - 1] = '\0';
262 /* Now authenticate the intruder... */
263 if (!(exp = auth_authenticate("mount", sin, p))) {
264 *error = NFSERR_ACCES;
265 } else if (stat(p, &stb) < 0) {
266 xlog(L_WARNING, "can't stat exported dir %s: %s",
269 *error = NFSERR_NOENT;
271 *error = NFSERR_ACCES;
272 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
273 xlog(L_WARNING, "%s is not a directory or regular file", p);
274 *error = NFSERR_NOTDIR;
275 } else if (stat(exp->m_export.e_path, &estb) < 0) {
276 xlog(L_WARNING, "can't stat export point %s: %s",
278 *error = NFSERR_NOENT;
279 } else if (estb.st_dev != stb.st_dev
280 /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMNT)) */
282 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
283 p, exp->m_export.e_path);
284 *error = NFSERR_ACCES;
285 } else if (exp->m_export.e_mountpoint &&
286 !is_mountpoint(exp->m_export.e_mountpoint[0]?
287 exp->m_export.e_mountpoint:
288 exp->m_export.e_path)) {
289 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
291 *error = NFSERR_NOENT;
292 } else if (new_cache) {
293 /* This will be a static private nfs_export with just one
294 * address. We feed it to kernel then extract the filehandle,
297 struct nfs_fh_len *fh;
300 fh = cache_get_filehandle(exp, v3?64:32, p);
302 *error = NFSERR_ACCES;
307 struct nfs_fh_len *fh;
309 if (exp->m_exported<1)
315 fh = getfh_size ((struct sockaddr *) sin, p, 64);
316 if (!v3 || (fh == NULL && errno == EINVAL)) {
317 /* We first try the new nfs syscall. */
318 fh = getfh ((struct sockaddr *) sin, p);
319 if (fh == NULL && errno == EINVAL)
320 /* Let's try the old one. */
321 fh = getfh_old ((struct sockaddr *) sin,
322 stb.st_dev, stb.st_ino);
325 mountlist_add(exp, p);
330 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
331 *error = NFSERR_ACCES;
340 static exports elist = NULL;
341 struct exportnode *e, *ne;
342 struct groupnode *g, *ng, *c, **cp;
346 if (!auth_reload() && elist)
349 for (e = elist; e != NULL; e = ne) {
351 for (g = e->ex_groups; g != NULL; g = ng) {
361 for (i = 0; i < MCL_MAXTYPES; i++) {
362 for (exp = exportlist[i]; exp; exp = exp->m_next) {
363 for (e = elist; e != NULL; e = e->ex_next) {
364 if (!strcmp(exp->m_export.m_path, e->ex_dir))
368 e = (struct exportnode *) xmalloc(sizeof(*e));
371 e->ex_dir = xstrdup(exp->m_export.m_path);
375 /* We need to check if we should remove
377 if (i == MCL_ANONYMOUS && e->ex_groups) {
378 for (g = e->ex_groups; g; g = ng) {
387 if (i != MCL_FQDN && e->ex_groups) {
391 while ((c = *cp) != NULL) {
392 if (client_gettype (c->gr_name) == MCL_FQDN
393 && (hp = gethostbyname(c->gr_name))) {
394 hp = hostent_dup (hp);
395 if (client_check(exp->m_client, hp)) {
400 if ((c = *cp) == NULL)
410 if (exp->m_export.e_hostname [0] != '\0') {
411 for (g = e->ex_groups; g; g = g->gr_next)
412 if (strcmp (exp->m_export.e_hostname,
417 g = (struct groupnode *) xmalloc(sizeof(*g));
418 g->gr_name = xstrdup(exp->m_export.e_hostname);
419 g->gr_next = e->ex_groups;
429 main(int argc, char **argv)
431 char *export_file = _PATH_EXPORTS;
434 int descriptors = 256;
439 /* Parse the command line options and arguments. */
441 while ((c = getopt_long(argc, argv, "on:Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
444 descriptors = atoi(optarg);
445 if (descriptors <= 0) {
446 fprintf(stderr, "%s: bad descriptors: %s\n",
455 xlog_sconfig(optarg, 1);
458 export_file = optarg;
463 case 'P': /* XXX for nfs-server compatibility */
466 if (port <= 0 || port > 65535) {
467 fprintf(stderr, "%s: bad port number: %s\n",
473 nfs_version &= ~(1 << (atoi (optarg) - 1));
476 _rpcfdtype = SOCK_DGRAM;
479 nfs_version |= 1 << (atoi (optarg) - 1);
482 printf("kmountd %s\n", VERSION);
491 /* No more arguments allowed. */
492 if (optind != argc || !(nfs_version & 0x7))
495 if (chdir(NFS_STATEDIR)) {
496 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
497 argv [0], NFS_STATEDIR, strerror(errno));
501 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0) {
502 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
503 argv [0], strerror(errno));
507 rlim.rlim_cur = descriptors;
508 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
509 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
510 argv [0], strerror(errno));
514 /* Initialize logging. */
515 /* xlog_open("mountd"); */
517 sa.sa_handler = SIG_IGN;
519 sigemptyset(&sa.sa_mask);
520 sigaction(SIGHUP, &sa, NULL);
521 sigaction(SIGINT, &sa, NULL);
522 sigaction(SIGTERM, &sa, NULL);
523 sigaction(SIGPIPE, &sa, NULL);
524 /* WARNING: the following works on Linux and SysV, but not BSD! */
525 sigaction(SIGCHLD, &sa, NULL);
527 /* Daemons should close all extra filehandles ... *before* RPC init. */
529 int fd = sysconf (_SC_OPEN_MAX);
534 new_cache = check_new_cache();
538 if (nfs_version & 0x1)
539 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
540 mount_dispatch, port);
541 if (nfs_version & (0x1 << 1))
542 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
543 mount_dispatch, port);
544 if (nfs_version & (0x1 << 2))
545 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
546 mount_dispatch, port);
548 sa.sa_handler = killer;
549 sigaction(SIGHUP, &sa, NULL);
550 sigaction(SIGINT, &sa, NULL);
551 sigaction(SIGTERM, &sa, NULL);
553 auth_init(export_file);
556 /* We first fork off a child. */
557 if ((c = fork()) > 0)
560 xlog(L_FATAL, "mountd: cannot fork: %s\n",
563 /* Now we remove ourselves from the foreground.
564 Redirect stdin/stdout/stderr first. */
566 int fd = open("/dev/null", O_RDWR);
570 if (fd > 2) (void) close(fd);
578 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
583 usage(const char *prog, int n)
586 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
587 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
588 " [-p|--port port] [-V version|--nfs-version version]\n"
589 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n", prog);