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>
25 static void usage(const char *, int exitcode);
26 static exports get_exportlist(void);
27 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, int *, int v3);
29 static struct option longopts[] =
31 { "foreground", 0, 0, 'F' },
32 { "debug", 1, 0, 'd' },
33 { "help", 0, 0, 'h' },
34 { "exports-file", 1, 0, 'f' },
35 { "nfs-version", 1, 0, 'V' },
36 { "no-nfs-version", 1, 0, 'N' },
37 { "version", 0, 0, 'v' },
38 { "port", 1, 0, 'p' },
39 { "no-tcp", 0, 0, 'n' },
43 static int nfs_version = -1;
51 if (nfs_version & 0x1)
52 pmap_unset (MOUNTPROG, MOUNTVERS);
53 if (nfs_version & (0x1 << 1))
54 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
55 if (nfs_version & (0x1 << 2))
56 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
57 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
61 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
67 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
69 struct nfs_fh_len *fh;
71 xlog(D_CALL, "MNT1(%s) called", *path);
72 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
73 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
78 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
80 struct sockaddr_in *addr =
81 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
82 xlog(L_NOTICE, "dump request from %s",
83 inet_ntoa(addr->sin_addr));
85 *res = mountlist_list();
90 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
92 struct sockaddr_in *sin
93 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
96 char rpath[MAXPATHLEN+1];
101 if (realpath(p, rpath) != NULL) {
102 rpath[sizeof (rpath) - 1] = '\0';
106 if (!(exp = auth_authenticate("unmount", sin, p))) {
109 mountlist_del(exp, p);
115 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
117 /* Reload /etc/xtab if necessary */
120 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
125 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
127 struct sockaddr_in *addr =
128 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
129 xlog(L_NOTICE, "export request from %s",
130 inet_ntoa(addr->sin_addr));
131 *resp = get_exportlist();
136 mount_exportall_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, "exportall request from %s",
141 inet_ntoa(addr->sin_addr));
142 *resp = get_exportlist();
147 * MNTv2 pathconf procedure
149 * The protocol doesn't include a status field, so Sun apparently considers
150 * it good practice to let anyone snoop on your system, even if it's
151 * pretty harmless data such as pathconf. We don't.
153 * Besides, many of the pathconf values don't make much sense on NFS volumes.
154 * FIFOs and tty device files represent devices on the *client*, so there's
155 * no point in getting the server's buffer sizes etc.
158 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
160 struct sockaddr_in *sin
161 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
164 char rpath[MAXPATHLEN+1];
167 memset(res, 0, sizeof(*res));
172 /* Reload /etc/xtab if necessary */
175 /* Resolve symlinks */
176 if (realpath(p, rpath) != NULL) {
177 rpath[sizeof (rpath) - 1] = '\0';
181 /* Now authenticate the intruder... */
182 if (!(exp = auth_authenticate("mount", sin, p))) {
184 } else if (stat(p, &stb) < 0) {
185 xlog(L_WARNING, "can't stat exported dir %s: %s",
191 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
192 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
193 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
194 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
195 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
196 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
197 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
199 /* Can't figure out what to do with pc_mask */
209 * NFSv3 MOUNT procedure
212 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
214 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
215 struct nfs_fh_len *fh;
217 xlog(D_CALL, "MNT3(%s) called", *path);
218 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
219 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
221 ok->fhandle.fhandle3_len = fh->fh_size;
222 ok->fhandle.fhandle3_val = fh->fh_handle;
223 ok->auth_flavors.auth_flavors_len = 2;
224 ok->auth_flavors.auth_flavors_val = flavors;
229 static struct nfs_fh_len *
230 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
232 struct sockaddr_in *sin =
233 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
236 char rpath[MAXPATHLEN+1];
242 /* Reload /var/lib/nfs/etab if necessary */
245 /* Resolve symlinks */
246 if (realpath(p, rpath) != NULL) {
247 rpath[sizeof (rpath) - 1] = '\0';
251 /* Now authenticate the intruder... */
252 if (!(exp = auth_authenticate("mount", sin, p))) {
253 *error = NFSERR_ACCES;
254 } else if (stat(p, &stb) < 0) {
255 xlog(L_WARNING, "can't stat exported dir %s: %s",
258 *error = NFSERR_NOENT;
260 *error = NFSERR_ACCES;
261 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
262 xlog(L_WARNING, "%s is not a directory or regular file", p);
263 *error = NFSERR_NOTDIR;
265 struct nfs_fh_len *fh;
267 if (exp->m_exported<1)
273 fh = getfh_size ((struct sockaddr *) sin, p, 64);
274 if (!v3 || (fh == NULL && errno == EINVAL)) {
275 /* We first try the new nfs syscall. */
276 fh = getfh ((struct sockaddr *) sin, p);
277 if (fh == NULL && errno == EINVAL)
278 /* Let's try the old one. */
279 fh = getfh_old ((struct sockaddr *) sin,
280 stb.st_dev, stb.st_ino);
283 mountlist_add(exp, p);
288 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
289 *error = NFSERR_ACCES;
298 static exports elist = NULL;
299 struct exportnode *e, *ne;
300 struct groupnode *g, *ng, *c, **cp;
304 if (!auth_reload() && elist)
307 for (e = elist; e != NULL; e = ne) {
309 for (g = e->ex_groups; g != NULL; g = ng) {
319 for (i = 0; i < MCL_MAXTYPES; i++) {
320 for (exp = exportlist[i]; exp; exp = exp->m_next) {
321 for (e = elist; e != NULL; e = e->ex_next) {
322 if (!strcmp(exp->m_export.m_path, e->ex_dir))
326 e = (struct exportnode *) xmalloc(sizeof(*e));
329 e->ex_dir = xstrdup(exp->m_export.m_path);
333 /* We need to check if we should remove
335 if (i == MCL_ANONYMOUS && e->ex_groups) {
336 for (g = e->ex_groups; g; g = ng) {
345 if (i != MCL_FQDN && e->ex_groups) {
349 while ((c = *cp) != NULL) {
350 if (client_gettype (c->gr_name) == MCL_FQDN
351 && (hp = gethostbyname(c->gr_name))) {
352 hp = hostent_dup (hp);
353 if (client_check(exp->m_client, hp)) {
358 if ((c = *cp) == NULL)
368 if (exp->m_export.e_hostname [0] != '\0') {
369 for (g = e->ex_groups; g; g = g->gr_next)
370 if (strcmp (exp->m_export.e_hostname,
375 g = (struct groupnode *) xmalloc(sizeof(*g));
376 g->gr_name = xstrdup(exp->m_export.e_hostname);
377 g->gr_next = e->ex_groups;
387 main(int argc, char **argv)
389 char *export_file = _PATH_EXPORTS;
395 /* Parse the command line options and arguments. */
397 while ((c = getopt_long(argc, argv, "Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
403 xlog_sconfig(optarg, 1);
406 export_file = optarg;
411 case 'P': /* XXX for nfs-server compatibility */
414 if (port <= 0 || port > 65535) {
415 fprintf(stderr, "%s: bad port number: %s\n",
421 nfs_version &= ~(1 << (atoi (optarg) - 1));
424 _rpcfdtype = SOCK_DGRAM;
427 nfs_version |= 1 << (atoi (optarg) - 1);
430 printf("kmountd %s\n", VERSION);
439 /* No more arguments allowed. */
440 if (optind != argc || !(nfs_version & 0x7))
443 /* Initialize logging. */
444 /* xlog_open("mountd"); */
446 sa.sa_handler = SIG_IGN;
448 sigemptyset(&sa.sa_mask);
449 sigaction(SIGHUP, &sa, NULL);
450 sigaction(SIGINT, &sa, NULL);
451 sigaction(SIGTERM, &sa, NULL);
452 /* WARNING: the following works on Linux and SysV, but not BSD! */
453 sigaction(SIGCHLD, &sa, NULL);
455 if (nfs_version & 0x1)
456 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
457 mount_dispatch, port);
458 if (nfs_version & (0x1 << 1))
459 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
460 mount_dispatch, port);
461 if (nfs_version & (0x1 << 2))
462 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
463 mount_dispatch, port);
465 sa.sa_handler = killer;
466 sigaction(SIGHUP, &sa, NULL);
467 sigaction(SIGINT, &sa, NULL);
468 sigaction(SIGTERM, &sa, NULL);
470 auth_init(export_file);
473 /* We first fork off a child. */
474 if ((c = fork()) > 0)
477 xlog(L_FATAL, "mountd: cannot fork: %s\n",
480 /* Now we remove ourselves from the foreground.
481 Redirect stdin/stdout/stderr first. */
483 int fd = open("/dev/null", O_RDWR);
487 if (fd > 2) (void) close(fd);
495 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
500 usage(const char *prog, int n)
503 "Usage: %s [-Fhnv] [-d kind] [-f exports-file] [-V version]\n"
504 " [-N version] [--debug kind] [-p|--port port] [--help] [--version]\n"
505 " [--exports-file=file] [--nfs-version version]\n"
506 " [--no-nfs-version version] [--no-tcp]\n", prog);