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