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