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