]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
2000-05-31 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 <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 nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, int *, int v3);
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 nfs_fh_len *fh;
69
70         xlog(D_CALL, "MNT1(%s) called", *path);
71         if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
72                 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 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 nfs_fh_len *fh;
207
208         xlog(D_CALL, "MNT3(%s) called", *path);
209         if ((fh = get_rootfh(rqstp, path, (int *) &res->fhs_status, 1)) != NULL) {
210                 struct mountres3_ok     *ok = &res->mountres3_u.mountinfo;
211
212                 ok->fhandle.fhandle3_len = fh->fh_size;
213                 ok->fhandle.fhandle3_val = fh->fh_handle;
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 nfs_fh_len *
221 get_rootfh(struct svc_req *rqstp, dirpath *path, int *error, int v3)
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 nfs_fh_len  *fh;
256
257                 if (!exp->m_exported)
258                         export_export(exp);
259                 if (!exp->m_xtabent)
260                         xtab_append(exp);
261
262                 if (v3)
263                         fh = getfh_size ((struct sockaddr *) sin, p, 64);
264                 if (!v3 || (fh == NULL && errno == EINVAL)) {
265                         /* We first try the new nfs syscall. */
266                         fh = getfh ((struct sockaddr *) sin, p);
267                         if (fh == NULL && errno == EINVAL)
268                                 /* Let's try the old one. */
269                                 fh = getfh_old ((struct sockaddr *) sin,
270                                                 stb.st_dev, stb.st_ino);
271                 }
272                 if (fh != NULL) {
273                         mountlist_add(exp, p);
274                         *error = NFS_OK;
275                         export_reset (exp);
276                         return fh;
277                 }
278                 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
279                 *error = NFSERR_ACCES;
280         }
281         export_reset (exp);
282         return NULL;
283 }
284
285 static exports
286 get_exportlist(void)
287 {
288         static exports          elist = NULL;
289         struct exportnode       *e, *ne;
290         struct groupnode        *g, *ng, *c, **cp;
291         nfs_export              *exp;
292         int                     i;
293
294         if (!auth_reload() && elist)
295                 return elist;
296
297         for (e = elist; e != NULL; e = ne) {
298                 ne = e->ex_next;
299                 for (g = e->ex_groups; g != NULL; g = ng) {
300                         ng = g->gr_next;
301                         xfree(g->gr_name);
302                         xfree(g);
303                 }
304                 xfree(e->ex_dir);
305                 xfree(e);
306         }
307         elist = NULL;
308
309         for (i = 0; i < MCL_MAXTYPES; i++) {
310                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
311                         for (e = elist; e != NULL; e = e->ex_next) {
312                                 if (!strcmp(exp->m_export.m_path, e->ex_dir))
313                                         break;
314                         }
315                         if (!e) {
316                                 e = (struct exportnode *) xmalloc(sizeof(*e));
317                                 e->ex_next = elist;
318                                 e->ex_groups = NULL;
319                                 e->ex_dir = xstrdup(exp->m_export.m_path);
320                                 elist = e;
321                         }
322
323                         /* We need to check if we should remove
324                            previous ones. */
325                         if (i == MCL_ANONYMOUS && e->ex_groups) {
326                                 for (g = e->ex_groups; g; g = ng) {
327                                         ng = g->gr_next;
328                                         xfree(g->gr_name);
329                                         xfree(g);
330                                 }
331                                 e->ex_groups = NULL;
332                                 continue;
333                         }
334
335                         if (i != MCL_FQDN && e->ex_groups) {
336                           struct hostent        *hp;
337
338                           cp = &e->ex_groups;
339                           while ((c = *cp) != NULL) {
340                             if (client_gettype (c->gr_name) == MCL_FQDN
341                                 && (hp = gethostbyname(c->gr_name))) {
342                               hp = hostent_dup (hp);
343                               if (client_check(exp->m_client, hp)) {
344                                 *cp = c->gr_next;
345                                 xfree(c->gr_name);
346                                 xfree(c);
347                                 xfree (hp);
348                                 if ((c = *cp) == NULL)
349                                   break;
350                               }
351                               else
352                                 xfree (hp);
353                             }
354                             cp = &(c->gr_next);
355                           }
356                         }
357
358                         if (exp->m_export.e_hostname [0] != '\0') {
359                                 for (g = e->ex_groups; g; g = g->gr_next)
360                                         if (strcmp (exp->m_export.e_hostname,
361                                                     g->gr_name) == 0)
362                                                 break;
363                                 if (g)
364                                         continue;
365                                 g = (struct groupnode *) xmalloc(sizeof(*g));
366                                 g->gr_name = xstrdup(exp->m_export.e_hostname);
367                                 g->gr_next = e->ex_groups;
368                                 e->ex_groups = g;
369                         }
370                 }
371         }
372
373         return elist;
374 }
375
376 int
377 main(int argc, char **argv)
378 {
379         char    *export_file = _PATH_EXPORTS;
380         int     foreground = 0;
381         int     port = 0;
382         int     c;
383         struct sigaction sa;
384
385         /* Parse the command line options and arguments. */
386         opterr = 0;
387         while ((c = getopt_long(argc, argv, "Fd:f:p:P:hN:V:v", longopts, NULL)) != EOF)
388                 switch (c) {
389                 case 'F':
390                         foreground = 1;
391                         break;
392                 case 'd':
393                         xlog_sconfig(optarg, 1);
394                         break;
395                 case 'f':
396                         export_file = optarg;
397                         break;
398                 case 'h':
399                         usage(argv [0], 0);
400                         break;
401                 case 'P':       /* XXX for nfs-server compatibility */
402                 case 'p':
403                         port = atoi(optarg);
404                         if (port <= 0 || port > 65535) {
405                                 fprintf(stderr, "%s: bad port number: %s\n",
406                                         argv [0], optarg);
407                                 usage(argv [0], 1);
408                         }
409                         break;
410                 case 'N':
411                         nfs_version &= ~(1 << (atoi (optarg) - 1));
412                         break;
413                 case 'V':
414                         nfs_version |= 1 << (atoi (optarg) - 1);
415                         break;
416                 case 'v':
417                         printf("kmountd %s\n", VERSION);
418                         exit(0);
419                 case 0:
420                         break;
421                 case '?':
422                 default:
423                         usage(argv [0], 1);
424                 }
425
426         /* No more arguments allowed. */
427         if (optind != argc || !(nfs_version & 0x7))
428                 usage(argv [0], 1);
429
430         /* Initialize logging. */
431 /*      xlog_open("mountd"); */
432
433         sa.sa_handler = SIG_IGN;
434         sa.sa_flags = 0;
435         sigemptyset(&sa.sa_mask);
436         sigaction(SIGHUP, &sa, NULL);
437         sigaction(SIGINT, &sa, NULL);
438         sigaction(SIGTERM, &sa, NULL);
439
440         if (nfs_version & 0x1)
441                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
442                          mount_dispatch, port, 0);
443         if (nfs_version & (0x1 << 1))
444                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
445                          mount_dispatch, port, 0);
446         if (nfs_version & (0x1 << 2))
447                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
448                          mount_dispatch, port, 0);
449
450         sa.sa_handler = killer;
451         sigaction(SIGHUP, &sa, NULL);
452         sigaction(SIGINT, &sa, NULL);
453         sigaction(SIGTERM, &sa, NULL);
454
455         auth_init(export_file);
456
457         if (!foreground) {
458                 /* We first fork off a child. */
459                 if ((c = fork()) > 0)
460                         exit(0);
461                 if (c < 0) {
462                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
463                                                 strerror(errno));
464                 }
465                 /* Now we remove ourselves from the foreground.
466                    Redirect stdin/stdout/stderr first. */
467                 {
468                         int fd = open("/dev/null", O_RDWR);
469                         (void) dup2(fd, 0);
470                         (void) dup2(fd, 1);
471                         (void) dup2(fd, 2);
472                         if (fd > 2) (void) close(fd);
473                 }
474                 setsid();
475                 xlog_background();
476         }
477
478         svc_run();
479
480         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
481         exit(1);
482 }
483
484 static void
485 usage(const char *prog, int n)
486 {
487         fprintf(stderr,
488 "Usage: %s [-Fhnv] [-d kind] [-f exports-file] [-V version]\n"
489 "       [-N version] [--debug kind] [-p|--port port] [--help] [--version]\n"
490 "       [--exports-file=file] [--nfs-version version]\n"
491 "       [--no-nfs-version version]\n", prog);
492         exit(n);
493 }