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