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);
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);
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 (new_cache) {
276 /* This will be a static private nfs_export with just one
277 * address. We feed it to kernel then extract the filehandle,
280 struct nfs_fh_len *fh;
283 fh = cache_get_filehandle(exp, v3?64:32);
285 *error = NFSERR_ACCES;
290 struct nfs_fh_len *fh;
292 if (exp->m_exported<1)
298 fh = getfh_size ((struct sockaddr *) sin, p, 64);
299 if (!v3 || (fh == NULL && errno == EINVAL)) {
300 /* We first try the new nfs syscall. */
301 fh = getfh ((struct sockaddr *) sin, p);
302 if (fh == NULL && errno == EINVAL)
303 /* Let's try the old one. */
304 fh = getfh_old ((struct sockaddr *) sin,
305 stb.st_dev, stb.st_ino);
308 mountlist_add(exp, p);
313 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
314 *error = NFSERR_ACCES;
323 static exports elist = NULL;
324 struct exportnode *e, *ne;
325 struct groupnode *g, *ng, *c, **cp;
329 if (!auth_reload() && elist)
332 for (e = elist; e != NULL; e = ne) {
334 for (g = e->ex_groups; g != NULL; g = ng) {
344 for (i = 0; i < MCL_MAXTYPES; i++) {
345 for (exp = exportlist[i]; exp; exp = exp->m_next) {
346 for (e = elist; e != NULL; e = e->ex_next) {
347 if (!strcmp(exp->m_export.m_path, e->ex_dir))
351 e = (struct exportnode *) xmalloc(sizeof(*e));
354 e->ex_dir = xstrdup(exp->m_export.m_path);
358 /* We need to check if we should remove
360 if (i == MCL_ANONYMOUS && e->ex_groups) {
361 for (g = e->ex_groups; g; g = ng) {
370 if (i != MCL_FQDN && e->ex_groups) {
374 while ((c = *cp) != NULL) {
375 if (client_gettype (c->gr_name) == MCL_FQDN
376 && (hp = gethostbyname(c->gr_name))) {
377 hp = hostent_dup (hp);
378 if (client_check(exp->m_client, hp)) {
383 if ((c = *cp) == NULL)
393 if (exp->m_export.e_hostname [0] != '\0') {
394 for (g = e->ex_groups; g; g = g->gr_next)
395 if (strcmp (exp->m_export.e_hostname,
400 g = (struct groupnode *) xmalloc(sizeof(*g));
401 g->gr_name = xstrdup(exp->m_export.e_hostname);
402 g->gr_next = e->ex_groups;
412 main(int argc, char **argv)
414 char *export_file = _PATH_EXPORTS;
417 int descriptors = 256;
422 /* Parse the command line options and arguments. */
424 while ((c = getopt_long(argc, argv, "on:Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
427 descriptors = atoi(optarg);
428 if (descriptors <= 0) {
429 fprintf(stderr, "%s: bad descriptors: %s\n",
438 xlog_sconfig(optarg, 1);
441 export_file = optarg;
446 case 'P': /* XXX for nfs-server compatibility */
449 if (port <= 0 || port > 65535) {
450 fprintf(stderr, "%s: bad port number: %s\n",
456 nfs_version &= ~(1 << (atoi (optarg) - 1));
459 _rpcfdtype = SOCK_DGRAM;
462 nfs_version |= 1 << (atoi (optarg) - 1);
465 printf("kmountd %s\n", VERSION);
474 /* No more arguments allowed. */
475 if (optind != argc || !(nfs_version & 0x7))
478 if (chdir(NFS_STATEDIR)) {
479 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
480 argv [0], NFS_STATEDIR, strerror(errno));
484 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0) {
485 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
486 argv [0], strerror(errno));
490 rlim.rlim_cur = descriptors;
491 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
492 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
493 argv [0], strerror(errno));
497 /* Initialize logging. */
498 /* xlog_open("mountd"); */
500 sa.sa_handler = SIG_IGN;
502 sigemptyset(&sa.sa_mask);
503 sigaction(SIGHUP, &sa, NULL);
504 sigaction(SIGINT, &sa, NULL);
505 sigaction(SIGTERM, &sa, NULL);
506 /* WARNING: the following works on Linux and SysV, but not BSD! */
507 sigaction(SIGCHLD, &sa, NULL);
509 /* Daemons should close all extra filehandles ... *before* RPC init. */
511 int fd = sysconf (_SC_OPEN_MAX);
516 new_cache = check_new_cache();
520 if (nfs_version & 0x1)
521 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
522 mount_dispatch, port);
523 if (nfs_version & (0x1 << 1))
524 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
525 mount_dispatch, port);
526 if (nfs_version & (0x1 << 2))
527 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
528 mount_dispatch, port);
530 sa.sa_handler = killer;
531 sigaction(SIGHUP, &sa, NULL);
532 sigaction(SIGINT, &sa, NULL);
533 sigaction(SIGTERM, &sa, NULL);
535 auth_init(export_file);
538 /* We first fork off a child. */
539 if ((c = fork()) > 0)
542 xlog(L_FATAL, "mountd: cannot fork: %s\n",
545 /* Now we remove ourselves from the foreground.
546 Redirect stdin/stdout/stderr first. */
548 int fd = open("/dev/null", O_RDWR);
552 if (fd > 2) (void) close(fd);
560 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
565 usage(const char *prog, int n)
568 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
569 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
570 " [-p|--port port] [-V version|--nfs-version version]\n"
571 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n", prog);