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