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>
26 static void usage(const char *, int exitcode);
27 static exports get_exportlist(void);
28 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, int *, int v3);
30 static struct option longopts[] =
32 { "foreground", 0, 0, 'F' },
33 { "debug", 1, 0, 'd' },
34 { "help", 0, 0, 'h' },
35 { "exports-file", 1, 0, 'f' },
36 { "nfs-version", 1, 0, 'V' },
37 { "no-nfs-version", 1, 0, 'N' },
38 { "version", 0, 0, 'v' },
39 { "port", 1, 0, 'p' },
40 { "no-tcp", 0, 0, 'n' },
44 static int nfs_version = -1;
52 if (nfs_version & 0x1)
53 pmap_unset (MOUNTPROG, MOUNTVERS);
54 if (nfs_version & (0x1 << 1))
55 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
56 if (nfs_version & (0x1 << 2))
57 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
58 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
62 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
68 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
70 struct nfs_fh_len *fh;
72 xlog(D_CALL, "MNT1(%s) called", *path);
73 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
74 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
79 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
81 struct sockaddr_in *addr =
82 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
83 xlog(L_NOTICE, "dump request from %s",
84 inet_ntoa(addr->sin_addr));
86 *res = mountlist_list();
91 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
93 struct sockaddr_in *sin
94 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
97 char rpath[MAXPATHLEN+1];
102 if (realpath(p, rpath) != NULL) {
103 rpath[sizeof (rpath) - 1] = '\0';
107 if (!(exp = auth_authenticate("unmount", sin, p))) {
110 mountlist_del(exp, p);
116 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
118 /* Reload /etc/xtab if necessary */
121 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
126 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
128 struct sockaddr_in *addr =
129 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
130 xlog(L_NOTICE, "export request from %s",
131 inet_ntoa(addr->sin_addr));
132 *resp = get_exportlist();
137 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
139 struct sockaddr_in *addr =
140 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
141 xlog(L_NOTICE, "exportall request from %s",
142 inet_ntoa(addr->sin_addr));
143 *resp = get_exportlist();
148 * MNTv2 pathconf procedure
150 * The protocol doesn't include a status field, so Sun apparently considers
151 * it good practice to let anyone snoop on your system, even if it's
152 * pretty harmless data such as pathconf. We don't.
154 * Besides, many of the pathconf values don't make much sense on NFS volumes.
155 * FIFOs and tty device files represent devices on the *client*, so there's
156 * no point in getting the server's buffer sizes etc.
159 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
161 struct sockaddr_in *sin
162 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
165 char rpath[MAXPATHLEN+1];
168 memset(res, 0, sizeof(*res));
173 /* Reload /etc/xtab if necessary */
176 /* Resolve symlinks */
177 if (realpath(p, rpath) != NULL) {
178 rpath[sizeof (rpath) - 1] = '\0';
182 /* Now authenticate the intruder... */
183 if (!(exp = auth_authenticate("mount", sin, p))) {
185 } else if (stat(p, &stb) < 0) {
186 xlog(L_WARNING, "can't stat exported dir %s: %s",
192 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
193 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
194 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
195 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
196 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
197 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
198 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
200 /* Can't figure out what to do with pc_mask */
210 * NFSv3 MOUNT procedure
213 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
215 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
216 struct nfs_fh_len *fh;
218 xlog(D_CALL, "MNT3(%s) called", *path);
219 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
220 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
222 ok->fhandle.fhandle3_len = fh->fh_size;
223 ok->fhandle.fhandle3_val = fh->fh_handle;
224 ok->auth_flavors.auth_flavors_len = 2;
225 ok->auth_flavors.auth_flavors_val = flavors;
230 static struct nfs_fh_len *
231 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
233 struct sockaddr_in *sin =
234 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
237 char rpath[MAXPATHLEN+1];
243 /* Reload /var/lib/nfs/etab if necessary */
246 /* Resolve symlinks */
247 if (realpath(p, rpath) != NULL) {
248 rpath[sizeof (rpath) - 1] = '\0';
252 /* Now authenticate the intruder... */
253 if (!(exp = auth_authenticate("mount", sin, p))) {
254 *error = NFSERR_ACCES;
255 } else if (stat(p, &stb) < 0) {
256 xlog(L_WARNING, "can't stat exported dir %s: %s",
259 *error = NFSERR_NOENT;
261 *error = NFSERR_ACCES;
262 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
263 xlog(L_WARNING, "%s is not a directory or regular file", p);
264 *error = NFSERR_NOTDIR;
266 struct nfs_fh_len *fh;
268 if (exp->m_exported<1)
274 fh = getfh_size ((struct sockaddr *) sin, p, 64);
275 if (!v3 || (fh == NULL && errno == EINVAL)) {
276 /* We first try the new nfs syscall. */
277 fh = getfh ((struct sockaddr *) sin, p);
278 if (fh == NULL && errno == EINVAL)
279 /* Let's try the old one. */
280 fh = getfh_old ((struct sockaddr *) sin,
281 stb.st_dev, stb.st_ino);
284 mountlist_add(exp, p);
289 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
290 *error = NFSERR_ACCES;
299 static exports elist = NULL;
300 struct exportnode *e, *ne;
301 struct groupnode *g, *ng, *c, **cp;
305 if (!auth_reload() && elist)
308 for (e = elist; e != NULL; e = ne) {
310 for (g = e->ex_groups; g != NULL; g = ng) {
320 for (i = 0; i < MCL_MAXTYPES; i++) {
321 for (exp = exportlist[i]; exp; exp = exp->m_next) {
322 for (e = elist; e != NULL; e = e->ex_next) {
323 if (!strcmp(exp->m_export.m_path, e->ex_dir))
327 e = (struct exportnode *) xmalloc(sizeof(*e));
330 e->ex_dir = xstrdup(exp->m_export.m_path);
334 /* We need to check if we should remove
336 if (i == MCL_ANONYMOUS && e->ex_groups) {
337 for (g = e->ex_groups; g; g = ng) {
346 if (i != MCL_FQDN && e->ex_groups) {
350 while ((c = *cp) != NULL) {
351 if (client_gettype (c->gr_name) == MCL_FQDN
352 && (hp = gethostbyname(c->gr_name))) {
353 hp = hostent_dup (hp);
354 if (client_check(exp->m_client, hp)) {
359 if ((c = *cp) == NULL)
369 if (exp->m_export.e_hostname [0] != '\0') {
370 for (g = e->ex_groups; g; g = g->gr_next)
371 if (strcmp (exp->m_export.e_hostname,
376 g = (struct groupnode *) xmalloc(sizeof(*g));
377 g->gr_name = xstrdup(exp->m_export.e_hostname);
378 g->gr_next = e->ex_groups;
388 main(int argc, char **argv)
390 char *export_file = _PATH_EXPORTS;
396 /* Parse the command line options and arguments. */
398 while ((c = getopt_long(argc, argv, "Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
404 xlog_sconfig(optarg, 1);
407 export_file = optarg;
412 case 'P': /* XXX for nfs-server compatibility */
415 if (port <= 0 || port > 65535) {
416 fprintf(stderr, "%s: bad port number: %s\n",
422 nfs_version &= ~(1 << (atoi (optarg) - 1));
425 _rpcfdtype = SOCK_DGRAM;
428 nfs_version |= 1 << (atoi (optarg) - 1);
431 printf("kmountd %s\n", VERSION);
440 /* No more arguments allowed. */
441 if (optind != argc || !(nfs_version & 0x7))
444 if (chdir(NFS_STATEDIR)) {
445 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
446 argv [0], NFS_STATEDIR, strerror(errno));
450 /* Initialize logging. */
451 /* xlog_open("mountd"); */
453 sa.sa_handler = SIG_IGN;
455 sigemptyset(&sa.sa_mask);
456 sigaction(SIGHUP, &sa, NULL);
457 sigaction(SIGINT, &sa, NULL);
458 sigaction(SIGTERM, &sa, NULL);
459 /* WARNING: the following works on Linux and SysV, but not BSD! */
460 sigaction(SIGCHLD, &sa, NULL);
462 if (nfs_version & 0x1)
463 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
464 mount_dispatch, port);
465 if (nfs_version & (0x1 << 1))
466 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
467 mount_dispatch, port);
468 if (nfs_version & (0x1 << 2))
469 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
470 mount_dispatch, port);
472 sa.sa_handler = killer;
473 sigaction(SIGHUP, &sa, NULL);
474 sigaction(SIGINT, &sa, NULL);
475 sigaction(SIGTERM, &sa, NULL);
477 auth_init(export_file);
480 /* We first fork off a child. */
481 if ((c = fork()) > 0)
484 xlog(L_FATAL, "mountd: cannot fork: %s\n",
487 /* Now we remove ourselves from the foreground.
488 Redirect stdin/stdout/stderr first. */
490 int fd = open("/dev/null", O_RDWR);
494 if (fd > 2) (void) close(fd);
502 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
507 usage(const char *prog, int n)
510 "Usage: %s [-Fhnv] [-d kind] [-f exports-file] [-V version]\n"
511 " [-N version] [--debug kind] [-p|--port port] [--help] [--version]\n"
512 " [--exports-file=file] [--nfs-version version]\n"
513 " [--no-nfs-version version] [--no-tcp]\n", prog);