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