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);
34 static void usage(const char *, int exitcode);
35 static exports get_exportlist(void);
36 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, int *, int v3);
40 static struct option longopts[] =
42 { "foreground", 0, 0, 'F' },
43 { "descriptors", 1, 0, 'o' },
44 { "debug", 1, 0, 'd' },
45 { "help", 0, 0, 'h' },
46 { "exports-file", 1, 0, 'f' },
47 { "nfs-version", 1, 0, 'V' },
48 { "no-nfs-version", 1, 0, 'N' },
49 { "version", 0, 0, 'v' },
50 { "port", 1, 0, 'p' },
51 { "no-tcp", 0, 0, 'n' },
55 static int nfs_version = -1;
63 if (nfs_version & 0x1)
64 pmap_unset (MOUNTPROG, MOUNTVERS);
65 if (nfs_version & (0x1 << 1))
66 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
67 if (nfs_version & (0x1 << 2))
68 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
69 xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
73 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
79 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
81 struct nfs_fh_len *fh;
83 xlog(D_CALL, "MNT1(%s) called", *path);
84 if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
85 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
90 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
92 struct sockaddr_in *addr =
93 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
94 xlog(L_NOTICE, "dump request from %s",
95 inet_ntoa(addr->sin_addr));
97 *res = mountlist_list();
102 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
104 struct sockaddr_in *sin
105 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
108 char rpath[MAXPATHLEN+1];
113 if (realpath(p, rpath) != NULL) {
114 rpath[sizeof (rpath) - 1] = '\0';
118 if (!(exp = auth_authenticate("unmount", sin, p))) {
121 mountlist_del(exp, p);
127 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
129 /* Reload /etc/xtab if necessary */
132 mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
137 mount_export_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, "export request from %s",
142 inet_ntoa(addr->sin_addr));
143 *resp = get_exportlist();
148 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
150 struct sockaddr_in *addr =
151 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
152 xlog(L_NOTICE, "exportall request from %s",
153 inet_ntoa(addr->sin_addr));
154 *resp = get_exportlist();
159 * MNTv2 pathconf procedure
161 * The protocol doesn't include a status field, so Sun apparently considers
162 * it good practice to let anyone snoop on your system, even if it's
163 * pretty harmless data such as pathconf. We don't.
165 * Besides, many of the pathconf values don't make much sense on NFS volumes.
166 * FIFOs and tty device files represent devices on the *client*, so there's
167 * no point in getting the server's buffer sizes etc.
170 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
172 struct sockaddr_in *sin
173 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
176 char rpath[MAXPATHLEN+1];
179 memset(res, 0, sizeof(*res));
184 /* Reload /etc/xtab if necessary */
187 /* Resolve symlinks */
188 if (realpath(p, rpath) != NULL) {
189 rpath[sizeof (rpath) - 1] = '\0';
193 /* Now authenticate the intruder... */
194 if (!(exp = auth_authenticate("pathconf", sin, p))) {
196 } else if (stat(p, &stb) < 0) {
197 xlog(L_WARNING, "can't stat exported dir %s: %s",
203 res->pc_link_max = pathconf(p, _PC_LINK_MAX);
204 res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
205 res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
206 res->pc_name_max = pathconf(p, _PC_NAME_MAX);
207 res->pc_path_max = pathconf(p, _PC_PATH_MAX);
208 res->pc_pipe_buf = pathconf(p, _PC_PIPE_BUF);
209 res->pc_vdisable = pathconf(p, _PC_VDISABLE);
211 /* Can't figure out what to do with pc_mask */
221 * NFSv3 MOUNT procedure
224 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
226 static int flavors[] = { AUTH_NULL, AUTH_UNIX };
227 struct nfs_fh_len *fh;
229 xlog(D_CALL, "MNT3(%s) called", *path);
230 if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
231 struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
233 ok->fhandle.fhandle3_len = fh->fh_size;
234 ok->fhandle.fhandle3_val = fh->fh_handle;
235 ok->auth_flavors.auth_flavors_len = 2;
236 ok->auth_flavors.auth_flavors_val = flavors;
241 static struct nfs_fh_len *
242 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
244 struct sockaddr_in *sin =
245 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
248 char rpath[MAXPATHLEN+1];
254 /* Reload /var/lib/nfs/etab if necessary */
257 /* Resolve symlinks */
258 if (realpath(p, rpath) != NULL) {
259 rpath[sizeof (rpath) - 1] = '\0';
263 /* Now authenticate the intruder... */
264 if (!(exp = auth_authenticate("mount", sin, p))) {
265 *error = NFSERR_ACCES;
266 } else if (stat(p, &stb) < 0) {
267 xlog(L_WARNING, "can't stat exported dir %s: %s",
270 *error = NFSERR_NOENT;
272 *error = NFSERR_ACCES;
273 } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
274 xlog(L_WARNING, "%s is not a directory or regular file", p);
275 *error = NFSERR_NOTDIR;
276 } else if (new_cache) {
277 /* This will be a static private nfs_export with just one
278 * address. We feed it to kernel then extract the filehandle,
281 struct nfs_fh_len *fh;
284 fh = cache_get_filehandle(exp, v3?64:32);
286 *error = NFSERR_ACCES;
291 struct nfs_fh_len *fh;
293 if (exp->m_exported<1)
299 fh = getfh_size ((struct sockaddr *) sin, p, 64);
300 if (!v3 || (fh == NULL && errno == EINVAL)) {
301 /* We first try the new nfs syscall. */
302 fh = getfh ((struct sockaddr *) sin, p);
303 if (fh == NULL && errno == EINVAL)
304 /* Let's try the old one. */
305 fh = getfh_old ((struct sockaddr *) sin,
306 stb.st_dev, stb.st_ino);
309 mountlist_add(exp, p);
314 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
315 *error = NFSERR_ACCES;
324 static exports elist = NULL;
325 struct exportnode *e, *ne;
326 struct groupnode *g, *ng, *c, **cp;
330 if (!auth_reload() && elist)
333 for (e = elist; e != NULL; e = ne) {
335 for (g = e->ex_groups; g != NULL; g = ng) {
345 for (i = 0; i < MCL_MAXTYPES; i++) {
346 for (exp = exportlist[i]; exp; exp = exp->m_next) {
347 for (e = elist; e != NULL; e = e->ex_next) {
348 if (!strcmp(exp->m_export.m_path, e->ex_dir))
352 e = (struct exportnode *) xmalloc(sizeof(*e));
355 e->ex_dir = xstrdup(exp->m_export.m_path);
359 /* We need to check if we should remove
361 if (i == MCL_ANONYMOUS && e->ex_groups) {
362 for (g = e->ex_groups; g; g = ng) {
371 if (i != MCL_FQDN && e->ex_groups) {
375 while ((c = *cp) != NULL) {
376 if (client_gettype (c->gr_name) == MCL_FQDN
377 && (hp = gethostbyname(c->gr_name))) {
378 hp = hostent_dup (hp);
379 if (client_check(exp->m_client, hp)) {
384 if ((c = *cp) == NULL)
394 if (exp->m_export.e_hostname [0] != '\0') {
395 for (g = e->ex_groups; g; g = g->gr_next)
396 if (strcmp (exp->m_export.e_hostname,
401 g = (struct groupnode *) xmalloc(sizeof(*g));
402 g->gr_name = xstrdup(exp->m_export.e_hostname);
403 g->gr_next = e->ex_groups;
413 main(int argc, char **argv)
415 char *export_file = _PATH_EXPORTS;
418 int descriptors = 256;
423 /* Parse the command line options and arguments. */
425 while ((c = getopt_long(argc, argv, "on:Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
428 descriptors = atoi(optarg);
429 if (descriptors <= 0) {
430 fprintf(stderr, "%s: bad descriptors: %s\n",
439 xlog_sconfig(optarg, 1);
442 export_file = optarg;
447 case 'P': /* XXX for nfs-server compatibility */
450 if (port <= 0 || port > 65535) {
451 fprintf(stderr, "%s: bad port number: %s\n",
457 nfs_version &= ~(1 << (atoi (optarg) - 1));
460 _rpcfdtype = SOCK_DGRAM;
463 nfs_version |= 1 << (atoi (optarg) - 1);
466 printf("kmountd %s\n", VERSION);
475 /* No more arguments allowed. */
476 if (optind != argc || !(nfs_version & 0x7))
479 if (chdir(NFS_STATEDIR)) {
480 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
481 argv [0], NFS_STATEDIR, strerror(errno));
485 if (getrlimit (RLIMIT_NOFILE, &rlim) != 0) {
486 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
487 argv [0], strerror(errno));
491 rlim.rlim_cur = descriptors;
492 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
493 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
494 argv [0], strerror(errno));
498 /* Initialize logging. */
499 /* xlog_open("mountd"); */
501 sa.sa_handler = SIG_IGN;
503 sigemptyset(&sa.sa_mask);
504 sigaction(SIGHUP, &sa, NULL);
505 sigaction(SIGINT, &sa, NULL);
506 sigaction(SIGTERM, &sa, NULL);
507 /* WARNING: the following works on Linux and SysV, but not BSD! */
508 sigaction(SIGCHLD, &sa, NULL);
510 /* Daemons should close all extra filehandles ... *before* RPC init. */
512 int fd = sysconf (_SC_OPEN_MAX);
517 new_cache = check_new_cache();
521 if (nfs_version & 0x1)
522 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
523 mount_dispatch, port);
524 if (nfs_version & (0x1 << 1))
525 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
526 mount_dispatch, port);
527 if (nfs_version & (0x1 << 2))
528 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
529 mount_dispatch, port);
531 sa.sa_handler = killer;
532 sigaction(SIGHUP, &sa, NULL);
533 sigaction(SIGINT, &sa, NULL);
534 sigaction(SIGTERM, &sa, NULL);
536 auth_init(export_file);
539 /* We first fork off a child. */
540 if ((c = fork()) > 0)
543 xlog(L_FATAL, "mountd: cannot fork: %s\n",
546 /* Now we remove ourselves from the foreground.
547 Redirect stdin/stdout/stderr first. */
549 int fd = open("/dev/null", O_RDWR);
553 if (fd > 2) (void) close(fd);
561 xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
566 usage(const char *prog, int n)
569 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
570 " [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
571 " [-p|--port port] [-V version|--nfs-version version]\n"
572 " [-N version|--no-nfs-version version] [-n|--no-tcp]\n", prog);