]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
145a4daa555691ec4748287205eb2bb1a98b6175
[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);
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;
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 (new_cache) {
276                 /* This will be a static private nfs_export with just one
277                  * address.  We feed it to kernel then extract the filehandle,
278                  * 
279                  */
280                 struct nfs_fh_len  *fh;
281
282                 cache_export(exp);
283                 fh = cache_get_filehandle(exp, v3?64:32);
284                 if (fh == NULL) 
285                         *error = NFSERR_ACCES;
286                 else
287                         *error = NFS_OK;
288                 return fh;
289         } else {
290                 struct nfs_fh_len  *fh;
291
292                 if (exp->m_exported<1)
293                         export_export(exp);
294                 if (!exp->m_xtabent)
295                         xtab_append(exp);
296
297                 if (v3)
298                         fh = getfh_size ((struct sockaddr *) sin, p, 64);
299                 if (!v3 || (fh == NULL && errno == EINVAL)) {
300                         /* We first try the new nfs syscall. */
301                         fh = getfh ((struct sockaddr *) sin, p);
302                         if (fh == NULL && errno == EINVAL)
303                                 /* Let's try the old one. */
304                                 fh = getfh_old ((struct sockaddr *) sin,
305                                                 stb.st_dev, stb.st_ino);
306                 }
307                 if (fh != NULL) {
308                         mountlist_add(exp, p);
309                         *error = NFS_OK;
310                         export_reset (exp);
311                         return fh;
312                 }
313                 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
314                 *error = NFSERR_ACCES;
315         }
316         export_reset (exp);
317         return NULL;
318 }
319
320 static exports
321 get_exportlist(void)
322 {
323         static exports          elist = NULL;
324         struct exportnode       *e, *ne;
325         struct groupnode        *g, *ng, *c, **cp;
326         nfs_export              *exp;
327         int                     i;
328
329         if (!auth_reload() && elist)
330                 return elist;
331
332         for (e = elist; e != NULL; e = ne) {
333                 ne = e->ex_next;
334                 for (g = e->ex_groups; g != NULL; g = ng) {
335                         ng = g->gr_next;
336                         xfree(g->gr_name);
337                         xfree(g);
338                 }
339                 xfree(e->ex_dir);
340                 xfree(e);
341         }
342         elist = NULL;
343
344         for (i = 0; i < MCL_MAXTYPES; i++) {
345                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
346                         for (e = elist; e != NULL; e = e->ex_next) {
347                                 if (!strcmp(exp->m_export.m_path, e->ex_dir))
348                                         break;
349                         }
350                         if (!e) {
351                                 e = (struct exportnode *) xmalloc(sizeof(*e));
352                                 e->ex_next = elist;
353                                 e->ex_groups = NULL;
354                                 e->ex_dir = xstrdup(exp->m_export.m_path);
355                                 elist = e;
356                         }
357
358                         /* We need to check if we should remove
359                            previous ones. */
360                         if (i == MCL_ANONYMOUS && e->ex_groups) {
361                                 for (g = e->ex_groups; g; g = ng) {
362                                         ng = g->gr_next;
363                                         xfree(g->gr_name);
364                                         xfree(g);
365                                 }
366                                 e->ex_groups = NULL;
367                                 continue;
368                         }
369
370                         if (i != MCL_FQDN && e->ex_groups) {
371                           struct hostent        *hp;
372
373                           cp = &e->ex_groups;
374                           while ((c = *cp) != NULL) {
375                             if (client_gettype (c->gr_name) == MCL_FQDN
376                                 && (hp = gethostbyname(c->gr_name))) {
377                               hp = hostent_dup (hp);
378                               if (client_check(exp->m_client, hp)) {
379                                 *cp = c->gr_next;
380                                 xfree(c->gr_name);
381                                 xfree(c);
382                                 xfree (hp);
383                                 if ((c = *cp) == NULL)
384                                   break;
385                               }
386                               else
387                                 xfree (hp);
388                             }
389                             cp = &(c->gr_next);
390                           }
391                         }
392
393                         if (exp->m_export.e_hostname [0] != '\0') {
394                                 for (g = e->ex_groups; g; g = g->gr_next)
395                                         if (strcmp (exp->m_export.e_hostname,
396                                                     g->gr_name) == 0)
397                                                 break;
398                                 if (g)
399                                         continue;
400                                 g = (struct groupnode *) xmalloc(sizeof(*g));
401                                 g->gr_name = xstrdup(exp->m_export.e_hostname);
402                                 g->gr_next = e->ex_groups;
403                                 e->ex_groups = g;
404                         }
405                 }
406         }
407
408         return elist;
409 }
410
411 int
412 main(int argc, char **argv)
413 {
414         char    *export_file = _PATH_EXPORTS;
415         int     foreground = 0;
416         int     port = 0;
417         int     descriptors = 256;
418         int     c;
419         struct sigaction sa;
420         struct rlimit rlim;
421
422         /* Parse the command line options and arguments. */
423         opterr = 0;
424         while ((c = getopt_long(argc, argv, "on:Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
425                 switch (c) {
426                 case 'o':
427                         descriptors = atoi(optarg);
428                         if (descriptors <= 0) {
429                                 fprintf(stderr, "%s: bad descriptors: %s\n",
430                                         argv [0], optarg);
431                                 usage(argv [0], 1);
432                         }
433                         break;
434                 case 'F':
435                         foreground = 1;
436                         break;
437                 case 'd':
438                         xlog_sconfig(optarg, 1);
439                         break;
440                 case 'f':
441                         export_file = optarg;
442                         break;
443                 case 'h':
444                         usage(argv [0], 0);
445                         break;
446                 case 'P':       /* XXX for nfs-server compatibility */
447                 case 'p':
448                         port = atoi(optarg);
449                         if (port <= 0 || port > 65535) {
450                                 fprintf(stderr, "%s: bad port number: %s\n",
451                                         argv [0], optarg);
452                                 usage(argv [0], 1);
453                         }
454                         break;
455                 case 'N':
456                         nfs_version &= ~(1 << (atoi (optarg) - 1));
457                         break;
458                 case 'n':
459                         _rpcfdtype = SOCK_DGRAM;
460                         break;
461                 case 'V':
462                         nfs_version |= 1 << (atoi (optarg) - 1);
463                         break;
464                 case 'v':
465                         printf("kmountd %s\n", VERSION);
466                         exit(0);
467                 case 0:
468                         break;
469                 case '?':
470                 default:
471                         usage(argv [0], 1);
472                 }
473
474         /* No more arguments allowed. */
475         if (optind != argc || !(nfs_version & 0x7))
476                 usage(argv [0], 1);
477
478         if (chdir(NFS_STATEDIR)) {
479                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
480                         argv [0], NFS_STATEDIR, strerror(errno));
481                 exit(1);
482         }
483
484         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0) {
485                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
486                         argv [0], strerror(errno));
487                 exit(1);
488         }
489
490         rlim.rlim_cur = descriptors;
491         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
492                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
493                         argv [0], strerror(errno));
494                 exit(1);
495         }
496
497         /* Initialize logging. */
498 /*      xlog_open("mountd"); */
499
500         sa.sa_handler = SIG_IGN;
501         sa.sa_flags = 0;
502         sigemptyset(&sa.sa_mask);
503         sigaction(SIGHUP, &sa, NULL);
504         sigaction(SIGINT, &sa, NULL);
505         sigaction(SIGTERM, &sa, NULL);
506         /* WARNING: the following works on Linux and SysV, but not BSD! */
507         sigaction(SIGCHLD, &sa, NULL);
508
509         /* Daemons should close all extra filehandles ... *before* RPC init. */
510         if (!foreground) {
511                 int fd = sysconf (_SC_OPEN_MAX);
512                 while (--fd > 2)
513                         (void) close(fd);
514         }
515
516         new_cache = check_new_cache();
517         if (new_cache)
518                 cache_open();
519
520         if (nfs_version & 0x1)
521                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
522                          mount_dispatch, port);
523         if (nfs_version & (0x1 << 1))
524                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
525                          mount_dispatch, port);
526         if (nfs_version & (0x1 << 2))
527                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
528                          mount_dispatch, port);
529
530         sa.sa_handler = killer;
531         sigaction(SIGHUP, &sa, NULL);
532         sigaction(SIGINT, &sa, NULL);
533         sigaction(SIGTERM, &sa, NULL);
534
535         auth_init(export_file);
536
537         if (!foreground) {
538                 /* We first fork off a child. */
539                 if ((c = fork()) > 0)
540                         exit(0);
541                 if (c < 0) {
542                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
543                                                 strerror(errno));
544                 }
545                 /* Now we remove ourselves from the foreground.
546                    Redirect stdin/stdout/stderr first. */
547                 {
548                         int fd = open("/dev/null", O_RDWR);
549                         (void) dup2(fd, 0);
550                         (void) dup2(fd, 1);
551                         (void) dup2(fd, 2);
552                         if (fd > 2) (void) close(fd);
553                 }
554                 setsid();
555                 xlog_background();
556         }
557
558         my_svc_run();
559
560         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
561         exit(1);
562 }
563
564 static void
565 usage(const char *prog, int n)
566 {
567         fprintf(stderr,
568 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
569 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
570 "       [-p|--port port] [-V version|--nfs-version version]\n"
571 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n", prog);
572         exit(n);
573 }