]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
14dac4185c32e9483f108a7faccdc9811ff0f3c1
[nfs-utils.git] / utils / mountd / mountd.c
1 /*
2  * utils/mountd/mountd.c
3  *
4  * Authenticate mount requests and retrieve file handle.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include "config.h"
10
11 #include <signal.h>
12 #include <sys/stat.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <getopt.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <sys/resource.h>
22 #include "xmalloc.h"
23 #include "misc.h"
24 #include "mountd.h"
25 #include "rpcmisc.h"
26
27 extern void     cache_open(void);
28 extern struct nfs_fh_len *cache_get_filehandle(nfs_export *exp, int len, char *p);
29 extern void cache_export(nfs_export *exp);
30
31 extern void my_svc_run(void);
32
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);
36
37 int new_cache = 0;
38
39 /* PRC: a high-availability callout program can be specified with -H
40  * When this is done, the program will receive callouts whenever clients
41  * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
42 char *ha_callout_prog = NULL;
43
44 static struct option longopts[] =
45 {
46         { "foreground", 0, 0, 'F' },
47         { "descriptors", 1, 0, 'o' },
48         { "debug", 1, 0, 'd' },
49         { "help", 0, 0, 'h' },
50         { "exports-file", 1, 0, 'f' },
51         { "nfs-version", 1, 0, 'V' },
52         { "no-nfs-version", 1, 0, 'N' },
53         { "version", 0, 0, 'v' },
54         { "port", 1, 0, 'p' },
55         { "no-tcp", 0, 0, 'n' },
56         { "ha-callout", 1, 0, 'H' },
57         { NULL, 0, 0, 0 }
58 };
59
60 static int nfs_version = -1;
61
62 /*
63  * Signal handler.
64  */
65 static void 
66 killer (int sig)
67 {
68   if (nfs_version & 0x1)
69     pmap_unset (MOUNTPROG, MOUNTVERS);
70   if (nfs_version & (0x1 << 1))
71     pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
72   if (nfs_version & (0x1 << 2))
73     pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
74   xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
75 }
76
77 bool_t
78 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
79 {
80         return 1;
81 }
82
83 bool_t
84 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
85 {
86         struct nfs_fh_len *fh;
87
88         xlog(D_CALL, "MNT1(%s) called", *path);
89         if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
90                 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
91         return 1;
92 }
93
94 bool_t
95 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
96 {
97         struct sockaddr_in *addr =
98                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
99         xlog(L_NOTICE, "dump request from %s",
100                 inet_ntoa(addr->sin_addr));
101
102         *res = mountlist_list();
103         return 1;
104 }
105
106 bool_t
107 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
108 {
109         struct sockaddr_in *sin
110                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
111         nfs_export      *exp;
112         char            *p = *argp;
113         char            rpath[MAXPATHLEN+1];
114
115         if (*p == '\0')
116                 p = "/";
117
118         if (realpath(p, rpath) != NULL) {
119                 rpath[sizeof (rpath) - 1] = '\0';
120                 p = rpath;
121         }
122
123         if (!(exp = auth_authenticate("unmount", sin, p))) {
124                 return 1;
125         }
126         if (new_cache) {
127                 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
128                         mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
129                 mountlist_del(exp->m_client->m_hostname, p);
130         } else {
131                 mountlist_del(exp->m_client->m_hostname, p);
132                 export_reset (exp);
133         }
134         return 1;
135 }
136
137 bool_t
138 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
139 {
140         /* Reload /etc/xtab if necessary */
141         auth_reload();
142
143         mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
144         return 1;
145 }
146
147 bool_t
148 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
149 {
150         struct sockaddr_in *addr =
151                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
152         xlog(L_NOTICE, "export request from %s",
153                 inet_ntoa(addr->sin_addr));
154         *resp = get_exportlist();
155         return 1;
156 }
157
158 bool_t
159 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
160 {
161         struct sockaddr_in *addr =
162                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
163         xlog(L_NOTICE, "exportall request from %s",
164                 inet_ntoa(addr->sin_addr));
165         *resp = get_exportlist();
166         return 1;
167 }
168
169 /*
170  * MNTv2 pathconf procedure
171  *
172  * The protocol doesn't include a status field, so Sun apparently considers
173  * it good practice to let anyone snoop on your system, even if it's
174  * pretty harmless data such as pathconf. We don't.
175  *
176  * Besides, many of the pathconf values don't make much sense on NFS volumes.
177  * FIFOs and tty device files represent devices on the *client*, so there's
178  * no point in getting the server's buffer sizes etc.
179  */
180 bool_t
181 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
182 {
183         struct sockaddr_in *sin
184                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
185         struct stat     stb;
186         nfs_export      *exp;
187         char            rpath[MAXPATHLEN+1];
188         char            *p = *path;
189
190         memset(res, 0, sizeof(*res));
191
192         if (*p == '\0')
193                 p = "/";
194
195         /* Reload /etc/xtab if necessary */
196         auth_reload();
197
198         /* Resolve symlinks */
199         if (realpath(p, rpath) != NULL) {
200                 rpath[sizeof (rpath) - 1] = '\0';
201                 p = rpath;
202         }
203
204         /* Now authenticate the intruder... */
205         if (!(exp = auth_authenticate("pathconf", sin, p))) {
206                 return 1;
207         } else if (stat(p, &stb) < 0) {
208                 xlog(L_WARNING, "can't stat exported dir %s: %s",
209                                 p, strerror(errno));
210                 export_reset (exp);
211                 return 1;
212         }
213
214         res->pc_link_max  = pathconf(p, _PC_LINK_MAX);
215         res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
216         res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
217         res->pc_name_max  = pathconf(p, _PC_NAME_MAX);
218         res->pc_path_max  = pathconf(p, _PC_PATH_MAX);
219         res->pc_pipe_buf  = pathconf(p, _PC_PIPE_BUF);
220         res->pc_vdisable  = pathconf(p, _PC_VDISABLE);
221
222         /* Can't figure out what to do with pc_mask */
223         res->pc_mask[0]   = 0;
224         res->pc_mask[1]   = 0;
225
226         export_reset (exp);
227
228         return 1;
229 }
230
231 /*
232  * NFSv3 MOUNT procedure
233  */
234 bool_t
235 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
236 {
237         static int      flavors[] = { AUTH_NULL, AUTH_UNIX };
238         struct nfs_fh_len *fh;
239
240         xlog(D_CALL, "MNT3(%s) called", *path);
241         if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
242                 struct mountres3_ok     *ok = &res->mountres3_u.mountinfo;
243
244                 ok->fhandle.fhandle3_len = fh->fh_size;
245                 ok->fhandle.fhandle3_val = fh->fh_handle;
246                 ok->auth_flavors.auth_flavors_len = 2;
247                 ok->auth_flavors.auth_flavors_val = flavors;
248         }
249         return 1;
250 }
251
252 static struct nfs_fh_len *
253 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
254 {
255         struct sockaddr_in *sin =
256                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
257         struct stat     stb, estb;
258         nfs_export      *exp;
259         char            rpath[MAXPATHLEN+1];
260         char            *p = *path;
261
262         if (*p == '\0')
263                 p = "/";
264
265         /* Reload /var/lib/nfs/etab if necessary */
266         auth_reload();
267
268         /* Resolve symlinks */
269         if (realpath(p, rpath) != NULL) {
270                 rpath[sizeof (rpath) - 1] = '\0';
271                 p = rpath;
272         }
273
274         /* Now authenticate the intruder... */
275         if (!(exp = auth_authenticate("mount", sin, p))) {
276                 *error = NFSERR_ACCES;
277         } else if (stat(p, &stb) < 0) {
278                 xlog(L_WARNING, "can't stat exported dir %s: %s",
279                                 p, strerror(errno));
280                 if (errno == ENOENT)
281                         *error = NFSERR_NOENT;
282                 else
283                         *error = NFSERR_ACCES;
284         } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
285                 xlog(L_WARNING, "%s is not a directory or regular file", p);
286                 *error = NFSERR_NOTDIR;
287         } else if (stat(exp->m_export.e_path, &estb) < 0) {
288                 xlog(L_WARNING, "can't stat export point %s: %s",
289                      p, strerror(errno));
290                 *error = NFSERR_NOENT;
291         } else if (estb.st_dev != stb.st_dev
292                    /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
293                 ) {
294                 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
295                      p, exp->m_export.e_path);
296                 *error = NFSERR_ACCES;
297         } else if (exp->m_export.e_mountpoint &&
298                    !is_mountpoint(exp->m_export.e_mountpoint[0]?
299                                   exp->m_export.e_mountpoint:
300                                   exp->m_export.e_path)) {
301                 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
302                      p);
303                 *error = NFSERR_NOENT;
304         } else if (new_cache) {
305                 /* This will be a static private nfs_export with just one
306                  * address.  We feed it to kernel then extract the filehandle,
307                  * 
308                  */
309                 struct nfs_fh_len  *fh;
310
311                 cache_export(exp);
312                 fh = cache_get_filehandle(exp, v3?64:32, p);
313                 if (fh == NULL) 
314                         *error = NFSERR_ACCES;
315                 else
316                         *error = NFS_OK;
317                 return fh;
318         } else {
319                 struct nfs_fh_len  *fh;
320
321                 if (exp->m_exported<1)
322                         export_export(exp);
323                 if (!exp->m_xtabent)
324                         xtab_append(exp);
325
326                 if (v3)
327                         fh = getfh_size ((struct sockaddr *) sin, p, 64);
328                 if (!v3 || (fh == NULL && errno == EINVAL)) {
329                         /* We first try the new nfs syscall. */
330                         fh = getfh ((struct sockaddr *) sin, p);
331                         if (fh == NULL && errno == EINVAL)
332                                 /* Let's try the old one. */
333                                 fh = getfh_old ((struct sockaddr *) sin,
334                                                 stb.st_dev, stb.st_ino);
335                 }
336                 if (fh != NULL) {
337                         mountlist_add(exp->m_client->m_hostname, p);
338                         *error = NFS_OK;
339                         export_reset (exp);
340                         return fh;
341                 }
342                 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
343                 *error = NFSERR_ACCES;
344         }
345         export_reset (exp);
346         return NULL;
347 }
348
349 static exports
350 get_exportlist(void)
351 {
352         static exports          elist = NULL;
353         struct exportnode       *e, *ne;
354         struct groupnode        *g, *ng, *c, **cp;
355         nfs_export              *exp;
356         int                     i;
357
358         if (!auth_reload() && elist)
359                 return elist;
360
361         for (e = elist; e != NULL; e = ne) {
362                 ne = e->ex_next;
363                 for (g = e->ex_groups; g != NULL; g = ng) {
364                         ng = g->gr_next;
365                         xfree(g->gr_name);
366                         xfree(g);
367                 }
368                 xfree(e->ex_dir);
369                 xfree(e);
370         }
371         elist = NULL;
372
373         for (i = 0; i < MCL_MAXTYPES; i++) {
374                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
375                         for (e = elist; e != NULL; e = e->ex_next) {
376                                 if (!strcmp(exp->m_export.m_path, e->ex_dir))
377                                         break;
378                         }
379                         if (!e) {
380                                 e = (struct exportnode *) xmalloc(sizeof(*e));
381                                 e->ex_next = elist;
382                                 e->ex_groups = NULL;
383                                 e->ex_dir = xstrdup(exp->m_export.m_path);
384                                 elist = e;
385                         }
386
387                         /* We need to check if we should remove
388                            previous ones. */
389                         if (i == MCL_ANONYMOUS && e->ex_groups) {
390                                 for (g = e->ex_groups; g; g = ng) {
391                                         ng = g->gr_next;
392                                         xfree(g->gr_name);
393                                         xfree(g);
394                                 }
395                                 e->ex_groups = NULL;
396                                 continue;
397                         }
398
399                         if (i != MCL_FQDN && e->ex_groups) {
400                           struct hostent        *hp;
401
402                           cp = &e->ex_groups;
403                           while ((c = *cp) != NULL) {
404                             if (client_gettype (c->gr_name) == MCL_FQDN
405                                 && (hp = gethostbyname(c->gr_name))) {
406                               hp = hostent_dup (hp);
407                               if (client_check(exp->m_client, hp)) {
408                                 *cp = c->gr_next;
409                                 xfree(c->gr_name);
410                                 xfree(c);
411                                 xfree (hp);
412                                 if ((c = *cp) == NULL)
413                                   break;
414                               }
415                               else
416                                 xfree (hp);
417                             }
418                             cp = &(c->gr_next);
419                           }
420                         }
421
422                         if (exp->m_export.e_hostname [0] != '\0') {
423                                 for (g = e->ex_groups; g; g = g->gr_next)
424                                         if (strcmp (exp->m_export.e_hostname,
425                                                     g->gr_name) == 0)
426                                                 break;
427                                 if (g)
428                                         continue;
429                                 g = (struct groupnode *) xmalloc(sizeof(*g));
430                                 g->gr_name = xstrdup(exp->m_export.e_hostname);
431                                 g->gr_next = e->ex_groups;
432                                 e->ex_groups = g;
433                         }
434                 }
435         }
436
437         return elist;
438 }
439
440 int
441 main(int argc, char **argv)
442 {
443         char    *export_file = _PATH_EXPORTS;
444         int     foreground = 0;
445         int     port = 0;
446         int     descriptors = 0;
447         int     c;
448         struct sigaction sa;
449         struct rlimit rlim;
450
451         /* Parse the command line options and arguments. */
452         opterr = 0;
453         while ((c = getopt_long(argc, argv, "o:n:Fd:f:p:P:hH:N:V:v", longopts, NULL)) != EOF)
454                 switch (c) {
455                 case 'o':
456                         descriptors = atoi(optarg);
457                         if (descriptors <= 0) {
458                                 fprintf(stderr, "%s: bad descriptors: %s\n",
459                                         argv [0], optarg);
460                                 usage(argv [0], 1);
461                         }
462                         break;
463                 case 'F':
464                         foreground = 1;
465                         break;
466                 case 'd':
467                         xlog_sconfig(optarg, 1);
468                         break;
469                 case 'f':
470                         export_file = optarg;
471                         break;
472                 case 'H': /* PRC: specify a high-availability callout program */
473                         ha_callout_prog = optarg;
474                         break;
475                 case 'h':
476                         usage(argv [0], 0);
477                         break;
478                 case 'P':       /* XXX for nfs-server compatibility */
479                 case 'p':
480                         port = atoi(optarg);
481                         if (port <= 0 || port > 65535) {
482                                 fprintf(stderr, "%s: bad port number: %s\n",
483                                         argv [0], optarg);
484                                 usage(argv [0], 1);
485                         }
486                         break;
487                 case 'N':
488                         nfs_version &= ~(1 << (atoi (optarg) - 1));
489                         break;
490                 case 'n':
491                         _rpcfdtype = SOCK_DGRAM;
492                         break;
493                 case 'V':
494                         nfs_version |= 1 << (atoi (optarg) - 1);
495                         break;
496                 case 'v':
497                         printf("kmountd %s\n", VERSION);
498                         exit(0);
499                 case 0:
500                         break;
501                 case '?':
502                 default:
503                         usage(argv [0], 1);
504                 }
505
506         /* No more arguments allowed. */
507         if (optind != argc || !(nfs_version & 0x7))
508                 usage(argv [0], 1);
509
510         if (chdir(NFS_STATEDIR)) {
511                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
512                         argv [0], NFS_STATEDIR, strerror(errno));
513                 exit(1);
514         }
515
516         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
517                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
518                                 argv [0], strerror(errno));
519         else {
520                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
521                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
522                     descriptors > FD_SETSIZE)
523                         descriptors = FD_SETSIZE;
524                 if (descriptors) {
525                         rlim.rlim_cur = descriptors;
526                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
527                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
528                                         argv [0], strerror(errno));
529                                 exit(1);
530                         }
531                 }
532         }
533         /* Initialize logging. */
534         if (!foreground) xlog_stderr(0);
535         xlog_open("mountd");
536
537         sa.sa_handler = SIG_IGN;
538         sa.sa_flags = 0;
539         sigemptyset(&sa.sa_mask);
540         sigaction(SIGHUP, &sa, NULL);
541         sigaction(SIGINT, &sa, NULL);
542         sigaction(SIGTERM, &sa, NULL);
543         sigaction(SIGPIPE, &sa, NULL);
544         /* WARNING: the following works on Linux and SysV, but not BSD! */
545         sigaction(SIGCHLD, &sa, NULL);
546
547         /* Daemons should close all extra filehandles ... *before* RPC init. */
548         if (!foreground) {
549                 int fd = sysconf (_SC_OPEN_MAX);
550                 while (--fd > 2)
551                         (void) close(fd);
552         }
553
554         new_cache = check_new_cache();
555         if (new_cache)
556                 cache_open();
557
558         if (nfs_version & 0x1)
559                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
560                          mount_dispatch, port);
561         if (nfs_version & (0x1 << 1))
562                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
563                          mount_dispatch, port);
564         if (nfs_version & (0x1 << 2))
565                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
566                          mount_dispatch, port);
567
568         sa.sa_handler = killer;
569         sigaction(SIGHUP, &sa, NULL);
570         sigaction(SIGINT, &sa, NULL);
571         sigaction(SIGTERM, &sa, NULL);
572
573         auth_init(export_file);
574
575         if (!foreground) {
576                 /* We first fork off a child. */
577                 if ((c = fork()) > 0)
578                         exit(0);
579                 if (c < 0) {
580                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
581                                                 strerror(errno));
582                 }
583                 /* Now we remove ourselves from the foreground.
584                    Redirect stdin/stdout/stderr first. */
585                 {
586                         int fd = open("/dev/null", O_RDWR);
587                         (void) dup2(fd, 0);
588                         (void) dup2(fd, 1);
589                         (void) dup2(fd, 2);
590                         if (fd > 2) (void) close(fd);
591                 }
592                 setsid();
593         }
594
595         my_svc_run();
596
597         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
598         exit(1);
599 }
600
601 static void
602 usage(const char *prog, int n)
603 {
604         fprintf(stderr,
605 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
606 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
607 "       [-p|--port port] [-V version|--nfs-version version]\n"
608 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
609 "       [-H ha-callout-prog]\n", prog);
610         exit(n);
611 }