]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
a9ec1c7b0ae7f91c24a2d53331a6373d3123b924
[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 static void
78 sig_hup (int sig)
79 {
80   /* don't exit on SIGHUP */
81   xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
82   return;
83 }
84
85 bool_t
86 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
87 {
88         return 1;
89 }
90
91 bool_t
92 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
93 {
94         struct nfs_fh_len *fh;
95
96         xlog(D_CALL, "MNT1(%s) called", *path);
97         if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
98                 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
99         return 1;
100 }
101
102 bool_t
103 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
104 {
105         struct sockaddr_in *addr =
106                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
107
108         if ((*res = mountlist_list()) == NULL)
109                 xlog(L_WARNING, "dump request from %s failed.",
110                         inet_ntoa(addr->sin_addr));
111
112         return 1;
113 }
114
115 bool_t
116 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
117 {
118         struct sockaddr_in *sin
119                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
120         nfs_export      *exp;
121         char            *p = *argp;
122         char            rpath[MAXPATHLEN+1];
123
124         if (*p == '\0')
125                 p = "/";
126
127         if (realpath(p, rpath) != NULL) {
128                 rpath[sizeof (rpath) - 1] = '\0';
129                 p = rpath;
130         }
131
132         if (!(exp = auth_authenticate("unmount", sin, p))) {
133                 return 1;
134         }
135         if (new_cache) {
136                 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
137                         mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
138                 mountlist_del(exp->m_client->m_hostname, p);
139         } else {
140                 mountlist_del(exp->m_client->m_hostname, p);
141                 export_reset (exp);
142         }
143         return 1;
144 }
145
146 bool_t
147 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
148 {
149         /* Reload /etc/xtab if necessary */
150         auth_reload();
151
152         mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
153         return 1;
154 }
155
156 bool_t
157 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
158 {
159         struct sockaddr_in *addr =
160                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
161
162         if ((*resp = get_exportlist()) == NULL)
163                 xlog(L_WARNING, "export request from %s failed.",
164                         inet_ntoa(addr->sin_addr));
165                 
166         return 1;
167 }
168
169 bool_t
170 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
171 {
172         struct sockaddr_in *addr =
173                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
174
175         if ((*resp = get_exportlist()) == NULL)
176                 xlog(L_WARNING, "exportall request from %s failed.",
177                         inet_ntoa(addr->sin_addr));
178         return 1;
179 }
180
181 /*
182  * MNTv2 pathconf procedure
183  *
184  * The protocol doesn't include a status field, so Sun apparently considers
185  * it good practice to let anyone snoop on your system, even if it's
186  * pretty harmless data such as pathconf. We don't.
187  *
188  * Besides, many of the pathconf values don't make much sense on NFS volumes.
189  * FIFOs and tty device files represent devices on the *client*, so there's
190  * no point in getting the server's buffer sizes etc.
191  */
192 bool_t
193 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
194 {
195         struct sockaddr_in *sin
196                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
197         struct stat     stb;
198         nfs_export      *exp;
199         char            rpath[MAXPATHLEN+1];
200         char            *p = *path;
201
202         memset(res, 0, sizeof(*res));
203
204         if (*p == '\0')
205                 p = "/";
206
207         /* Reload /etc/xtab if necessary */
208         auth_reload();
209
210         /* Resolve symlinks */
211         if (realpath(p, rpath) != NULL) {
212                 rpath[sizeof (rpath) - 1] = '\0';
213                 p = rpath;
214         }
215
216         /* Now authenticate the intruder... */
217         if (!(exp = auth_authenticate("pathconf", sin, p))) {
218                 return 1;
219         } else if (stat(p, &stb) < 0) {
220                 xlog(L_WARNING, "can't stat exported dir %s: %s",
221                                 p, strerror(errno));
222                 export_reset (exp);
223                 return 1;
224         }
225
226         res->pc_link_max  = pathconf(p, _PC_LINK_MAX);
227         res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
228         res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
229         res->pc_name_max  = pathconf(p, _PC_NAME_MAX);
230         res->pc_path_max  = pathconf(p, _PC_PATH_MAX);
231         res->pc_pipe_buf  = pathconf(p, _PC_PIPE_BUF);
232         res->pc_vdisable  = pathconf(p, _PC_VDISABLE);
233
234         /* Can't figure out what to do with pc_mask */
235         res->pc_mask[0]   = 0;
236         res->pc_mask[1]   = 0;
237
238         export_reset (exp);
239
240         return 1;
241 }
242
243 /*
244  * NFSv3 MOUNT procedure
245  */
246 bool_t
247 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
248 {
249 #define AUTH_GSS_KRB5 390003
250 #define AUTH_GSS_KRB5I 390004
251 #define AUTH_GSS_KRB5P 390005
252         static int      flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
253         struct nfs_fh_len *fh;
254
255         xlog(D_CALL, "MNT3(%s) called", *path);
256         if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
257                 struct mountres3_ok     *ok = &res->mountres3_u.mountinfo;
258
259                 ok->fhandle.fhandle3_len = fh->fh_size;
260                 ok->fhandle.fhandle3_val = fh->fh_handle;
261                 ok->auth_flavors.auth_flavors_len
262                         = sizeof(flavors)/sizeof(flavors[0]);
263                 ok->auth_flavors.auth_flavors_val = flavors;
264         }
265         return 1;
266 }
267
268 static struct nfs_fh_len *
269 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
270 {
271         struct sockaddr_in *sin =
272                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
273         struct stat     stb, estb;
274         nfs_export      *exp;
275         char            rpath[MAXPATHLEN+1];
276         char            *p = *path;
277
278         if (*p == '\0')
279                 p = "/";
280
281         /* Reload /var/lib/nfs/etab if necessary */
282         auth_reload();
283
284         /* Resolve symlinks */
285         if (realpath(p, rpath) != NULL) {
286                 rpath[sizeof (rpath) - 1] = '\0';
287                 p = rpath;
288         }
289
290         /* Now authenticate the intruder... */
291         if (!(exp = auth_authenticate("mount", sin, p))) {
292                 *error = NFSERR_ACCES;
293         } else if (stat(p, &stb) < 0) {
294                 xlog(L_WARNING, "can't stat exported dir %s: %s",
295                                 p, strerror(errno));
296                 if (errno == ENOENT)
297                         *error = NFSERR_NOENT;
298                 else
299                         *error = NFSERR_ACCES;
300         } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
301                 xlog(L_WARNING, "%s is not a directory or regular file", p);
302                 *error = NFSERR_NOTDIR;
303         } else if (stat(exp->m_export.e_path, &estb) < 0) {
304                 xlog(L_WARNING, "can't stat export point %s: %s",
305                      p, strerror(errno));
306                 *error = NFSERR_NOENT;
307         } else if (estb.st_dev != stb.st_dev
308                    /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
309                 ) {
310                 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
311                      p, exp->m_export.e_path);
312                 *error = NFSERR_ACCES;
313         } else if (exp->m_export.e_mountpoint &&
314                    !is_mountpoint(exp->m_export.e_mountpoint[0]?
315                                   exp->m_export.e_mountpoint:
316                                   exp->m_export.e_path)) {
317                 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
318                      p);
319                 *error = NFSERR_NOENT;
320         } else if (new_cache) {
321                 /* This will be a static private nfs_export with just one
322                  * address.  We feed it to kernel then extract the filehandle,
323                  * 
324                  */
325                 struct nfs_fh_len  *fh;
326
327                 cache_export(exp);
328                 fh = cache_get_filehandle(exp, v3?64:32, p);
329                 if (fh == NULL) 
330                         *error = NFSERR_ACCES;
331                 else
332                         *error = NFS_OK;
333                 return fh;
334         } else {
335                 struct nfs_fh_len  *fh;
336
337                 if (exp->m_exported<1)
338                         export_export(exp);
339                 if (!exp->m_xtabent)
340                         xtab_append(exp);
341
342                 if (v3)
343                         fh = getfh_size ((struct sockaddr *) sin, p, 64);
344                 if (!v3 || (fh == NULL && errno == EINVAL)) {
345                         /* We first try the new nfs syscall. */
346                         fh = getfh ((struct sockaddr *) sin, p);
347                         if (fh == NULL && errno == EINVAL)
348                                 /* Let's try the old one. */
349                                 fh = getfh_old ((struct sockaddr *) sin,
350                                                 stb.st_dev, stb.st_ino);
351                 }
352                 if (fh != NULL) {
353                         mountlist_add(exp->m_client->m_hostname, p);
354                         *error = NFS_OK;
355                         export_reset (exp);
356                         return fh;
357                 }
358                 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
359                 *error = NFSERR_ACCES;
360         }
361         export_reset (exp);
362         return NULL;
363 }
364
365 static exports
366 get_exportlist(void)
367 {
368         static exports          elist = NULL;
369         struct exportnode       *e, *ne;
370         struct groupnode        *g, *ng, *c, **cp;
371         nfs_export              *exp;
372         int                     i;
373
374         if (!auth_reload() && elist)
375                 return elist;
376
377         for (e = elist; e != NULL; e = ne) {
378                 ne = e->ex_next;
379                 for (g = e->ex_groups; g != NULL; g = ng) {
380                         ng = g->gr_next;
381                         xfree(g->gr_name);
382                         xfree(g);
383                 }
384                 xfree(e->ex_dir);
385                 xfree(e);
386         }
387         elist = NULL;
388
389         for (i = 0; i < MCL_MAXTYPES; i++) {
390                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
391                         for (e = elist; e != NULL; e = e->ex_next) {
392                                 if (!strcmp(exp->m_export.m_path, e->ex_dir))
393                                         break;
394                         }
395                         if (!e) {
396                                 e = (struct exportnode *) xmalloc(sizeof(*e));
397                                 e->ex_next = elist;
398                                 e->ex_groups = NULL;
399                                 e->ex_dir = xstrdup(exp->m_export.m_path);
400                                 elist = e;
401                         }
402
403                         /* We need to check if we should remove
404                            previous ones. */
405                         if (i == MCL_ANONYMOUS && e->ex_groups) {
406                                 for (g = e->ex_groups; g; g = ng) {
407                                         ng = g->gr_next;
408                                         xfree(g->gr_name);
409                                         xfree(g);
410                                 }
411                                 e->ex_groups = NULL;
412                                 continue;
413                         }
414
415                         if (i != MCL_FQDN && e->ex_groups) {
416                           struct hostent        *hp;
417
418                           cp = &e->ex_groups;
419                           while ((c = *cp) != NULL) {
420                             if (client_gettype (c->gr_name) == MCL_FQDN
421                                 && (hp = gethostbyname(c->gr_name))) {
422                               hp = hostent_dup (hp);
423                               if (client_check(exp->m_client, hp)) {
424                                 *cp = c->gr_next;
425                                 xfree(c->gr_name);
426                                 xfree(c);
427                                 xfree (hp);
428                                 if ((c = *cp) == NULL)
429                                   break;
430                               }
431                               else
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 }