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