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