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