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 knfs_fh * get_rootfh(struct svc_req *, dirpath *, int *);
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' },
42 static int nfs_version = -1;
50 if (nfs_version & 0x1)
51 pmap_unset (MOUNTPROG, MOUNTVERS);
52 if (nfs_version & (0x1 << 1))
53 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
54 if (nfs_version & (0x1 << 2))
55 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
56 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
60 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
66 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
70 xlog(D_CALL, "MNT1(%s) called", *path);
71 if ((fh = get_rootfh(rqstp, path, &res->fhs_status)) != NULL)
72 memcpy(&res->fhstatus_u.fhs_fhandle, fh, 32);
77 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
79 xlog(L_NOTICE, "dump request from %s",
80 inet_ntoa(svc_getcaller(rqstp->rq_xprt)->sin_addr));
82 *res = mountlist_list();
87 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
89 struct sockaddr_in *sin = svc_getcaller(rqstp->rq_xprt);
92 char rpath[MAXPATHLEN+1];
97 if (realpath(p, rpath) != NULL) {
98 rpath[sizeof (rpath) - 1] = '\0';
102 if (!(exp = auth_authenticate("unmount", sin, p))) {
105 mountlist_del(exp, p);
111 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
113 /* Reload /etc/xtab if necessary */
116 mountlist_del_all(svc_getcaller(rqstp->rq_xprt));
121 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
123 xlog(L_NOTICE, "export request from %s",
124 inet_ntoa(svc_getcaller(rqstp->rq_xprt)->sin_addr));
125 *resp = get_exportlist();
130 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
132 xlog(L_NOTICE, "exportall request from %s",
133 inet_ntoa(svc_getcaller(rqstp->rq_xprt)->sin_addr));
134 *resp = get_exportlist();
139 * MNTv2 pathconf procedure
141 * The protocol doesn't include a status field, so Sun apparently considers
142 * it good practice to let anyone snoop on your system, even if it's
143 * pretty harmless data such as pathconf. We don't.
145 * Besides, many of the pathconf values don't make much sense on NFS volumes.
146 * FIFOs and tty device files represent devices on the *client*, so there's
147 * no point in getting the server's buffer sizes etc.
150 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
152 struct sockaddr_in *sin = svc_getcaller(rqstp->rq_xprt);
155 char rpath[MAXPATHLEN+1];
158 memset(res, 0, sizeof(*res));
163 /* Reload /etc/xtab if necessary */
166 /* Resolve symlinks */
167 if (realpath(p, rpath) != NULL) {
168 rpath[sizeof (rpath) - 1] = '\0';
172 /* Now authenticate the intruder... */
173 if (!(exp = auth_authenticate("mount", sin, p))) {
175 } else if (stat(p, &stb) < 0) {
176 xlog(L_WARNING, "can't stat exported dir %s: %s",
182 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
183 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
184 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
185 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
186 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
187 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
188 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
190 /* Can't figure out what to do with pc_mask */
200 * NFSv3 MOUNT procedure
203 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
205 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
208 xlog(D_CALL, "MNT3(%s) called", *path);
209 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status)) != NULL) {
210 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
212 ok->fhandle.fhandle3_len = 32;
213 ok->fhandle.fhandle3_val = (char *) fh;
214 ok->auth_flavors.auth_flavors_len = 2;
215 ok->auth_flavors.auth_flavors_val = flavors;
220 static struct knfs_fh *
221 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error)
223 struct sockaddr_in *sin = svc_getcaller(rqstp->rq_xprt);
226 char rpath[MAXPATHLEN+1];
232 /* Reload /var/lib/nfs/etab if necessary */
235 /* Resolve symlinks */
236 if (realpath(p, rpath) != NULL) {
237 rpath[sizeof (rpath) - 1] = '\0';
241 /* Now authenticate the intruder... */
242 if (!(exp = auth_authenticate("mount", sin, p))) {
243 *error = NFSERR_ACCES;
244 } else if (stat(p, &stb) < 0) {
245 xlog(L_WARNING, "can't stat exported dir %s: %s",
248 *error = NFSERR_NOENT;
250 *error = NFSERR_ACCES;
251 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
252 xlog(L_WARNING, "%s is not a directory or regular file", p);
253 *error = NFSERR_NOTDIR;
257 if (!exp->m_exported)
262 /* We first try the new nfs syscall. */
263 fh = getfh ((struct sockaddr *) sin, p);
264 if (fh == NULL && errno == EINVAL)
265 /* Let's try the old one. */
266 fh = getfh_old ((struct sockaddr *) sin,
267 stb.st_dev, stb.st_ino);
269 mountlist_add(exp, p);
274 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
275 *error = NFSERR_ACCES;
284 static exports elist = NULL;
285 struct exportnode *e, *ne;
286 struct groupnode *g, *ng, *c, **cp;
290 if (!auth_reload() && elist)
293 for (e = elist; e != NULL; e = ne) {
295 for (g = e->ex_groups; g != NULL; g = ng) {
305 for (i = 0; i < MCL_MAXTYPES; i++) {
306 for (exp = exportlist[i]; exp; exp = exp->m_next) {
307 for (e = elist; e != NULL; e = e->ex_next) {
308 if (!strcmp(exp->m_export.m_path, e->ex_dir))
312 e = (struct exportnode *) xmalloc(sizeof(*e));
315 e->ex_dir = xstrdup(exp->m_export.m_path);
319 /* We need to check if we should remove
321 if (i == MCL_ANONYMOUS && e->ex_groups) {
322 for (g = e->ex_groups; g; g = ng) {
331 if (i != MCL_FQDN && e->ex_groups) {
335 while ((c = *cp) != NULL) {
336 if (client_gettype (c->gr_name) == MCL_FQDN
337 && (hp = gethostbyname(c->gr_name))) {
338 hp = hostent_dup (hp);
339 if (client_check(exp->m_client, hp)) {
344 if ((c = *cp) == NULL)
354 if (exp->m_export.e_hostname [0] != '\0') {
355 for (g = e->ex_groups; g; g = g->gr_next)
356 if (strcmp (exp->m_export.e_hostname,
361 g = (struct groupnode *) xmalloc(sizeof(*g));
362 g->gr_name = xstrdup(exp->m_export.e_hostname);
363 g->gr_next = e->ex_groups;
373 main(int argc, char **argv)
375 char *export_file = _PATH_EXPORTS;
381 /* Parse the command line options and arguments. */
383 while ((c = getopt_long(argc, argv, "Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
389 xlog_sconfig(optarg, 1);
392 export_file = optarg;
397 case 'P': /* XXX for nfs-server compatibility */
400 if (port <= 0 || port > 65535) {
401 fprintf(stderr, "%s: bad port number: %s\n",
407 nfs_version &= ~(1 << (atoi (optarg) - 1));
410 nfs_version |= 1 << (atoi (optarg) - 1);
413 printf("kmountd %s\n", VERSION);
422 /* No more arguments allowed. */
423 if (optind != argc || !(nfs_version & 0x7))
426 /* Initialize logging. */
429 sa.sa_handler = SIG_IGN;
431 sigemptyset(&sa.sa_mask);
432 sigaction(SIGHUP, &sa, NULL);
433 sigaction(SIGINT, &sa, NULL);
434 sigaction(SIGTERM, &sa, NULL);
436 if (nfs_version & 0x1)
437 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
438 mount_dispatch, port, 0);
439 if (nfs_version & (0x1 << 1))
440 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
441 mount_dispatch, port, 0);
442 if (nfs_version & (0x1 << 2))
443 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
444 mount_dispatch, port, 0);
446 sa.sa_handler = killer;
447 sigaction(SIGHUP, &sa, NULL);
448 sigaction(SIGINT, &sa, NULL);
449 sigaction(SIGTERM, &sa, NULL);
451 auth_init(export_file);
454 /* We first fork off a child. */
455 if ((c = fork()) > 0)
458 xlog(L_FATAL, "mountd: cannot fork: %s\n",
461 /* Now we remove ourselves from the foreground.
462 Redirect stdin/stdout/stderr first. */
464 int fd = open("/dev/null", O_RDWR);
468 if (fd > 2) (void) close(fd);
476 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
481 usage(const char *prog, int n)
484 "Usage: %s [-Fhnv] [-d kind] [-f exports-file] [-V version]\n"
485 " [-N version] [--debug kind] [-p|--port port] [--help] [--version]\n"
486 " [--exports-file=file] [--nfs-version version]\n"
487 " [--no-nfs-version version]\n", prog);