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