]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
mountd: Support IPv6 in mountd's svc routines
[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 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <signal.h>
14 #include <sys/stat.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <getopt.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/resource.h>
24 #include <sys/wait.h>
25 #include "xmalloc.h"
26 #include "misc.h"
27 #include "mountd.h"
28 #include "rpcmisc.h"
29 #include "pseudoflavors.h"
30
31 extern void     cache_open(void);
32 extern struct nfs_fh_len *cache_get_filehandle(nfs_export *exp, int len, char *p);
33 extern int cache_export(nfs_export *exp, char *path);
34
35 extern void my_svc_run(void);
36
37 static void             usage(const char *, int exitcode);
38 static exports          get_exportlist(void);
39 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, nfs_export **, mountstat3 *, int v3);
40
41 int reverse_resolve = 0;
42 int new_cache = 0;
43 int manage_gids;
44 int use_ipaddr = -1;
45
46 /* PRC: a high-availability callout program can be specified with -H
47  * When this is done, the program will receive callouts whenever clients
48  * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
49 char *ha_callout_prog = NULL;
50
51 /* Number of mountd threads to start.   Default is 1 and
52  * that's probably enough unless you need hundreds of
53  * clients to be able to mount at once.  */
54 static int num_threads = 1;
55 /* Arbitrary limit on number of threads */
56 #define MAX_THREADS 64
57
58 static struct option longopts[] =
59 {
60         { "foreground", 0, 0, 'F' },
61         { "descriptors", 1, 0, 'o' },
62         { "debug", 1, 0, 'd' },
63         { "help", 0, 0, 'h' },
64         { "exports-file", 1, 0, 'f' },
65         { "nfs-version", 1, 0, 'V' },
66         { "no-nfs-version", 1, 0, 'N' },
67         { "version", 0, 0, 'v' },
68         { "port", 1, 0, 'p' },
69         { "no-tcp", 0, 0, 'n' },
70         { "ha-callout", 1, 0, 'H' },
71         { "state-directory-path", 1, 0, 's' },
72         { "num-threads", 1, 0, 't' },
73         { "reverse-lookup", 0, 0, 'r' },
74         { "manage-gids", 0, 0, 'g' },
75         { NULL, 0, 0, 0 }
76 };
77
78 static int nfs_version = -1;
79
80 static void
81 unregister_services (void)
82 {
83         if (nfs_version & (0x1 << 1)) {
84                 pmap_unset (MOUNTPROG, MOUNTVERS);
85                 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
86         }
87         if (nfs_version & (0x1 << 2))
88                 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
89 }
90
91 static void
92 cleanup_lockfiles (void)
93 {
94         unlink(_PATH_XTABLCK);
95         unlink(_PATH_ETABLCK);
96         unlink(_PATH_RMTABLCK);
97 }
98
99 /* Wait for all worker child processes to exit and reap them */
100 static void
101 wait_for_workers (void)
102 {
103         int status;
104         pid_t pid;
105
106         for (;;) {
107
108                 pid = waitpid(0, &status, 0);
109
110                 if (pid < 0) {
111                         if (errno == ECHILD)
112                                 return; /* no more children */
113                         xlog(L_FATAL, "mountd: can't wait: %s\n",
114                                         strerror(errno));
115                 }
116
117                 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
118                  * does not happen on 2.6 kernels, and waitpid() blocks
119                  * until all the children are dead then returns with
120                  * -ECHILD.  But, we don't need to do anything on the
121                  * death of individual workers, so we don't care. */
122                 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
123                                 (int)pid, status);
124         }
125 }
126
127 /* Fork num_threads worker children and wait for them */
128 static void
129 fork_workers(void)
130 {
131         int i;
132         pid_t pid;
133
134         xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
135
136         for (i = 0 ; i < num_threads ; i++) {
137                 pid = fork();
138                 if (pid < 0) {
139                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
140                                         strerror(errno));
141                 }
142                 if (pid == 0) {
143                         /* worker child */
144
145                         /* Re-enable the default action on SIGTERM et al
146                          * so that workers die naturally when sent them.
147                          * Only the parent unregisters with pmap and
148                          * hence needs to do special SIGTERM handling. */
149                         struct sigaction sa;
150                         sa.sa_handler = SIG_DFL;
151                         sa.sa_flags = 0;
152                         sigemptyset(&sa.sa_mask);
153                         sigaction(SIGHUP, &sa, NULL);
154                         sigaction(SIGINT, &sa, NULL);
155                         sigaction(SIGTERM, &sa, NULL);
156
157                         /* fall into my_svc_run in caller */
158                         return;
159                 }
160         }
161
162         /* in parent */
163         wait_for_workers();
164         unregister_services();
165         cleanup_lockfiles();
166         xlog(L_NOTICE, "mountd: no more workers, exiting\n");
167         exit(0);
168 }
169
170 /*
171  * Signal handler.
172  */
173 static void 
174 killer (int sig)
175 {
176         unregister_services();
177         if (num_threads > 1) {
178                 /* play Kronos and eat our children */
179                 kill(0, SIGTERM);
180                 wait_for_workers();
181         }
182         cleanup_lockfiles();
183         xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
184 }
185
186 static void
187 sig_hup (int sig)
188 {
189         /* don't exit on SIGHUP */
190         xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
191         return;
192 }
193
194 bool_t
195 mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp), 
196         void *UNUSED(resp))
197 {
198         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
199         char buf[INET6_ADDRSTRLEN];
200
201         xlog(D_CALL, "Received NULL request from %s",
202                 host_ntop(sap, buf, sizeof(buf)));
203
204         return 1;
205 }
206
207 bool_t
208 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
209 {
210         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
211         char buf[INET6_ADDRSTRLEN];
212         struct nfs_fh_len *fh;
213
214         xlog(D_CALL, "Received MNT1(%s) request from %s", *path,
215                 host_ntop(sap, buf, sizeof(buf)));
216
217         fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
218         if (fh)
219                 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
220         return 1;
221 }
222
223 bool_t
224 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
225 {
226         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
227         char buf[INET6_ADDRSTRLEN];
228
229         xlog(D_CALL, "Received DUMP request from %s",
230                 host_ntop(sap, buf, sizeof(buf)));
231
232         *res = mountlist_list();
233
234         return 1;
235 }
236
237 bool_t
238 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
239 {
240         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
241         nfs_export      *exp;
242         char            *p = *argp;
243         char            rpath[MAXPATHLEN+1];
244         char            buf[INET6_ADDRSTRLEN];
245
246         if (*p == '\0')
247                 p = "/";
248
249         if (realpath(p, rpath) != NULL) {
250                 rpath[sizeof (rpath) - 1] = '\0';
251                 p = rpath;
252         }
253
254         xlog(D_CALL, "Received UMNT(%s) request from %s", p,
255                 host_ntop(sap, buf, sizeof(buf)));
256
257         exp = auth_authenticate("unmount", sap, p);
258         if (exp == NULL)
259                 return 1;
260
261         mountlist_del(host_ntop(sap, buf, sizeof(buf)), p);
262         return 1;
263 }
264
265 bool_t
266 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), 
267         void *UNUSED(resp))
268 {
269         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
270         char            buf[INET6_ADDRSTRLEN];
271
272         xlog(D_CALL, "Received UMNTALL request from %s",
273                 host_ntop(sap, buf, sizeof(buf)));
274
275         /* Reload /etc/xtab if necessary */
276         auth_reload();
277
278         mountlist_del_all(nfs_getrpccaller_in(rqstp->rq_xprt));
279         return 1;
280 }
281
282 bool_t
283 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
284 {
285         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
286         char buf[INET6_ADDRSTRLEN];
287
288         xlog(D_CALL, "Received EXPORT request from %s.",
289                 host_ntop(sap, buf, sizeof(buf)));
290
291         *resp = get_exportlist();
292                 
293         return 1;
294 }
295
296 bool_t
297 mount_exportall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
298 {
299         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
300         char buf[INET6_ADDRSTRLEN];
301
302         xlog(D_CALL, "Received EXPORTALL request from %s.",
303                 host_ntop(sap, buf, sizeof(buf)));
304
305         *resp = get_exportlist();
306
307         return 1;
308 }
309
310 /*
311  * MNTv2 pathconf procedure
312  *
313  * The protocol doesn't include a status field, so Sun apparently considers
314  * it good practice to let anyone snoop on your system, even if it's
315  * pretty harmless data such as pathconf. We don't.
316  *
317  * Besides, many of the pathconf values don't make much sense on NFS volumes.
318  * FIFOs and tty device files represent devices on the *client*, so there's
319  * no point in getting the server's buffer sizes etc.
320  */
321 bool_t
322 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
323 {
324         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
325         struct stat     stb;
326         nfs_export      *exp;
327         char            rpath[MAXPATHLEN+1];
328         char            *p = *path;
329         char buf[INET6_ADDRSTRLEN];
330
331         memset(res, 0, sizeof(*res));
332
333         if (*p == '\0')
334                 p = "/";
335
336         /* Reload /etc/xtab if necessary */
337         auth_reload();
338
339         /* Resolve symlinks */
340         if (realpath(p, rpath) != NULL) {
341                 rpath[sizeof (rpath) - 1] = '\0';
342                 p = rpath;
343         }
344
345         xlog(D_CALL, "Received PATHCONF(%s) request from %s", p,
346                 host_ntop(sap, buf, sizeof(buf)));
347
348         /* Now authenticate the intruder... */
349         exp = auth_authenticate("pathconf", sap, p);
350         if (exp == NULL)
351                 return 1;
352         else if (stat(p, &stb) < 0) {
353                 xlog(L_WARNING, "can't stat exported dir %s: %s",
354                                 p, strerror(errno));
355                 return 1;
356         }
357
358         res->pc_link_max  = pathconf(p, _PC_LINK_MAX);
359         res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
360         res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
361         res->pc_name_max  = pathconf(p, _PC_NAME_MAX);
362         res->pc_path_max  = pathconf(p, _PC_PATH_MAX);
363         res->pc_pipe_buf  = pathconf(p, _PC_PIPE_BUF);
364         res->pc_vdisable  = pathconf(p, _PC_VDISABLE);
365
366         /* Can't figure out what to do with pc_mask */
367         res->pc_mask[0]   = 0;
368         res->pc_mask[1]   = 0;
369
370         return 1;
371 }
372
373 /*
374  * We should advertise the preferred flavours first. (See RFC 2623
375  * section 2.7.)  We leave that to the administrator, by advertising
376  * flavours in the order they were listed in /etc/exports.  AUTH_NULL is
377  * dropped from the list to avoid backward compatibility issue with
378  * older Linux clients, who inspect the list in reversed order.
379  *
380  * XXX: It might be more helpful to rearrange these so that flavors
381  * giving more access (as determined from readonly and id-squashing
382  * options) come first.  (If we decide to do that we should probably do
383  * that when reading the exports rather than here.)
384  */
385 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
386 {
387         struct sec_entry *s;
388         static int flavors[SECFLAVOR_COUNT];
389         int i = 0;
390
391         for (s = exp->m_export.e_secinfo; s->flav; s++) {
392                 if (s->flav->fnum == AUTH_NULL)
393                         continue;
394                 flavors[i] = s->flav->fnum;
395                 i++;
396         }
397         if (i == 0) {
398                 /* default when there is no sec= option: */
399                 i = 1;
400                 flavors[0] = AUTH_UNIX;
401         }
402         ok->auth_flavors.auth_flavors_val = flavors;
403         ok->auth_flavors.auth_flavors_len = i;
404 }
405
406 /*
407  * NFSv3 MOUNT procedure
408  */
409 bool_t
410 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
411 {
412         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
413         struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
414         char buf[INET6_ADDRSTRLEN];
415         nfs_export *exp;
416         struct nfs_fh_len *fh;
417
418         xlog(D_CALL, "Received MNT3(%s) request from %s", *path,
419                 host_ntop(sap, buf, sizeof(buf)));
420
421         fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
422         if (!fh)
423                 return 1;
424
425         ok->fhandle.fhandle3_len = fh->fh_size;
426         ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
427         set_authflavors(ok, exp);
428         return 1;
429 }
430
431 static struct nfs_fh_len *
432 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
433                 mountstat3 *error, int v3)
434 {
435         struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt);
436         struct stat     stb, estb;
437         nfs_export      *exp;
438         struct nfs_fh_len *fh;
439         char            rpath[MAXPATHLEN+1];
440         char            *p = *path;
441         char            buf[INET6_ADDRSTRLEN];
442
443         if (*p == '\0')
444                 p = "/";
445
446         /* Reload /var/lib/nfs/etab if necessary */
447         auth_reload();
448
449         /* Resolve symlinks */
450         if (realpath(p, rpath) != NULL) {
451                 rpath[sizeof (rpath) - 1] = '\0';
452                 p = rpath;
453         }
454
455         /* Now authenticate the intruder... */
456         exp = auth_authenticate("mount", sap, p);
457         if (exp == NULL) {
458                 *error = NFSERR_ACCES;
459                 return NULL;
460         }
461         if (stat(p, &stb) < 0) {
462                 xlog(L_WARNING, "can't stat exported dir %s: %s",
463                                 p, strerror(errno));
464                 if (errno == ENOENT)
465                         *error = NFSERR_NOENT;
466                 else
467                         *error = NFSERR_ACCES;
468                 return NULL;
469         }
470         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
471                 xlog(L_WARNING, "%s is not a directory or regular file", p);
472                 *error = NFSERR_NOTDIR;
473                 return NULL;
474         }
475         if (stat(exp->m_export.e_path, &estb) < 0) {
476                 xlog(L_WARNING, "can't stat export point %s: %s",
477                      p, strerror(errno));
478                 *error = NFSERR_NOENT;
479                 return NULL;
480         }
481         if (estb.st_dev != stb.st_dev
482                    && (!new_cache
483                            || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
484                 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
485                      p, exp->m_export.e_path);
486                 *error = NFSERR_ACCES;
487                 return NULL;
488         }
489         if (exp->m_export.e_mountpoint &&
490                    !is_mountpoint(exp->m_export.e_mountpoint[0]?
491                                   exp->m_export.e_mountpoint:
492                                   exp->m_export.e_path)) {
493                 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
494                      p);
495                 *error = NFSERR_NOENT;
496                 return NULL;
497         }
498
499         if (new_cache) {
500                 /* This will be a static private nfs_export with just one
501                  * address.  We feed it to kernel then extract the filehandle,
502                  * 
503                  */
504
505                 if (cache_export(exp, p)) {
506                         *error = NFSERR_ACCES;
507                         return NULL;
508                 }
509                 fh = cache_get_filehandle(exp, v3?64:32, p);
510                 if (fh == NULL) {
511                         *error = NFSERR_ACCES;
512                         return NULL;
513                 }
514         } else {
515                 int did_export = 0;
516         retry:
517                 if (exp->m_exported<1) {
518                         export_export(exp);
519                         did_export = 1;
520                 }
521                 if (!exp->m_xtabent)
522                         xtab_append(exp);
523
524                 if (v3)
525                         fh = getfh_size((struct sockaddr_in *)sap, p, 64);
526                 if (!v3 || (fh == NULL && errno == EINVAL)) {
527                         /* We first try the new nfs syscall. */
528                         fh = getfh((struct sockaddr_in *)sap, p);
529                         if (fh == NULL && errno == EINVAL)
530                                 /* Let's try the old one. */
531                                 fh = getfh_old((struct sockaddr_in *)sap,
532                                                 stb.st_dev, stb.st_ino);
533                 }
534                 if (fh == NULL && !did_export) {
535                         exp->m_exported = 0;
536                         goto retry;
537                 }
538
539                 if (fh == NULL) {
540                         xlog(L_WARNING, "getfh failed: %s", strerror(errno));
541                         *error = NFSERR_ACCES;
542                         return NULL;
543                 }
544         }
545         *error = NFS_OK;
546         mountlist_add(host_ntop(sap, buf, sizeof(buf)), p);
547         if (expret)
548                 *expret = exp;
549         return fh;
550 }
551
552 static void remove_all_clients(exportnode *e)
553 {
554         struct groupnode *g, *ng;
555
556         for (g = e->ex_groups; g; g = ng) {
557                 ng = g->gr_next;
558                 xfree(g->gr_name);
559                 xfree(g);
560         }
561         e->ex_groups = NULL;
562 }
563
564 static void free_exportlist(exports *elist)
565 {
566         struct exportnode *e, *ne;
567
568         for (e = *elist; e != NULL; e = ne) {
569                 ne = e->ex_next;
570                 remove_all_clients(e);
571                 xfree(e->ex_dir);
572                 xfree(e);
573         }
574         *elist = NULL;
575 }
576
577 static void prune_clients(nfs_export *exp, struct exportnode *e)
578 {
579         struct addrinfo *ai = NULL;
580         struct groupnode *c, **cp;
581
582         cp = &e->ex_groups;
583         while ((c = *cp) != NULL) {
584                 if (client_gettype(c->gr_name) == MCL_FQDN
585                     && (ai = host_addrinfo(c->gr_name))) {
586                         if (client_check(exp->m_client, ai)) {
587                                 *cp = c->gr_next;
588                                 xfree(c->gr_name);
589                                 xfree(c);
590                                 freeaddrinfo(ai);
591                                 continue;
592                         }
593                         freeaddrinfo(ai);
594                 }
595                 cp = &(c->gr_next);
596         }
597 }
598
599 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
600 {
601         exportnode *e;
602
603         for (e = *elist; e != NULL; e = e->ex_next) {
604                 if (!strcmp(exp->m_export.e_path, e->ex_dir))
605                         return e;
606         }
607         e = xmalloc(sizeof(*e));
608         e->ex_next = *elist;
609         e->ex_groups = NULL;
610         e->ex_dir = xstrdup(exp->m_export.e_path);
611         *elist = e;
612         return e;
613 }
614
615 static void insert_group(struct exportnode *e, char *newname)
616 {
617         struct groupnode *g;
618
619         for (g = e->ex_groups; g; g = g->gr_next)
620                 if (strcmp(g->gr_name, newname))
621                         return;
622
623         g = xmalloc(sizeof(*g));
624         g->gr_name = xstrdup(newname);
625         g->gr_next = e->ex_groups;
626         e->ex_groups = g;
627 }
628
629 static exports
630 get_exportlist(void)
631 {
632         static exports          elist = NULL;
633         struct exportnode       *e;
634         nfs_export              *exp;
635         int                     i;
636         static unsigned int     ecounter;
637         unsigned int            acounter;
638
639         acounter = auth_reload();
640         if (elist && acounter == ecounter)
641                 return elist;
642
643         ecounter = acounter;
644
645         free_exportlist(&elist);
646
647         for (i = 0; i < MCL_MAXTYPES; i++) {
648                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
649                          /* Don't show pseudo exports */
650                         if (exp->m_export.e_flags & NFSEXP_V4ROOT)
651                                 continue;
652                         e = lookup_or_create_elist_entry(&elist, exp);
653
654                         /* exports to "*" absorb any others */
655                         if (i == MCL_ANONYMOUS && e->ex_groups) {
656                                 remove_all_clients(e);
657                                 continue;
658                         }
659                         /* non-FQDN's absorb FQDN's they contain: */
660                         if (i != MCL_FQDN && e->ex_groups)
661                                 prune_clients(exp, e);
662
663                         if (exp->m_export.e_hostname[0] != '\0')
664                                 insert_group(e, exp->m_export.e_hostname);
665                 }
666         }
667
668         return elist;
669 }
670
671 int
672 main(int argc, char **argv)
673 {
674         char    *export_file = _PATH_EXPORTS;
675         char    *state_dir = NFS_STATEDIR;
676         int     foreground = 0;
677         int     port = 0;
678         int     descriptors = 0;
679         int     c;
680         struct sigaction sa;
681         struct rlimit rlim;
682
683         /* Parse the command line options and arguments. */
684         opterr = 0;
685         while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
686                 switch (c) {
687                 case 'g':
688                         manage_gids = 1;
689                         break;
690                 case 'o':
691                         descriptors = atoi(optarg);
692                         if (descriptors <= 0) {
693                                 fprintf(stderr, "%s: bad descriptors: %s\n",
694                                         argv [0], optarg);
695                                 usage(argv [0], 1);
696                         }
697                         break;
698                 case 'F':
699                         foreground = 1;
700                         break;
701                 case 'd':
702                         xlog_sconfig(optarg, 1);
703                         break;
704                 case 'f':
705                         export_file = optarg;
706                         break;
707                 case 'H': /* PRC: specify a high-availability callout program */
708                         ha_callout_prog = optarg;
709                         break;
710                 case 'h':
711                         usage(argv [0], 0);
712                         break;
713                 case 'P':       /* XXX for nfs-server compatibility */
714                 case 'p':
715                         port = atoi(optarg);
716                         if (port <= 0 || port > 65535) {
717                                 fprintf(stderr, "%s: bad port number: %s\n",
718                                         argv [0], optarg);
719                                 usage(argv [0], 1);
720                         }
721                         break;
722                 case 'N':
723                         nfs_version &= ~(1 << (atoi (optarg) - 1));
724                         break;
725                 case 'n':
726                         _rpcfdtype = SOCK_DGRAM;
727                         break;
728                 case 'r':
729                         reverse_resolve = 1;
730                         break;
731                 case 's':
732                         if ((state_dir = xstrdup(optarg)) == NULL) {
733                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
734                                         argv[0], optarg);
735                                 exit(1);
736                         }
737                         break;
738                 case 't':
739                         num_threads = atoi (optarg);
740                         break;
741                 case 'V':
742                         nfs_version |= 1 << (atoi (optarg) - 1);
743                         break;
744                 case 'v':
745                         printf("kmountd %s\n", VERSION);
746                         exit(0);
747                 case 0:
748                         break;
749                 case '?':
750                 default:
751                         usage(argv [0], 1);
752                 }
753
754         /* No more arguments allowed.
755          * Require at least one valid version (2, 3, or 4)
756          */
757         if (optind != argc || !(nfs_version & 0xE))
758                 usage(argv [0], 1);
759
760         if (chdir(state_dir)) {
761                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
762                         argv [0], state_dir, strerror(errno));
763                 exit(1);
764         }
765
766         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
767                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
768                                 argv [0], strerror(errno));
769         else {
770                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
771                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
772                     descriptors > FD_SETSIZE)
773                         descriptors = FD_SETSIZE;
774                 if (descriptors) {
775                         rlim.rlim_cur = descriptors;
776                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
777                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
778                                         argv [0], strerror(errno));
779                                 exit(1);
780                         }
781                 }
782         }
783         /* Initialize logging. */
784         if (!foreground) xlog_stderr(0);
785         xlog_open("mountd");
786
787         sa.sa_handler = SIG_IGN;
788         sa.sa_flags = 0;
789         sigemptyset(&sa.sa_mask);
790         sigaction(SIGHUP, &sa, NULL);
791         sigaction(SIGINT, &sa, NULL);
792         sigaction(SIGTERM, &sa, NULL);
793         sigaction(SIGPIPE, &sa, NULL);
794         /* WARNING: the following works on Linux and SysV, but not BSD! */
795         sigaction(SIGCHLD, &sa, NULL);
796
797         /* Daemons should close all extra filehandles ... *before* RPC init. */
798         if (!foreground)
799                 closeall(3);
800
801         new_cache = check_new_cache();
802         if (new_cache)
803                 cache_open();
804
805         if (nfs_version & (0x1 << 1)) {
806                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
807                          mount_dispatch, port);
808                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
809                          mount_dispatch, port);
810         }
811         if (nfs_version & (0x1 << 2))
812                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
813                          mount_dispatch, port);
814
815         sa.sa_handler = killer;
816         sigaction(SIGINT, &sa, NULL);
817         sigaction(SIGTERM, &sa, NULL);
818         sa.sa_handler = sig_hup;
819         sigaction(SIGHUP, &sa, NULL);
820
821         auth_init(export_file);
822
823         if (!foreground) {
824                 /* We first fork off a child. */
825                 if ((c = fork()) > 0)
826                         exit(0);
827                 if (c < 0) {
828                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
829                                                 strerror(errno));
830                 }
831                 /* Now we remove ourselves from the foreground.
832                    Redirect stdin/stdout/stderr first. */
833                 {
834                         int fd = open("/dev/null", O_RDWR);
835                         (void) dup2(fd, 0);
836                         (void) dup2(fd, 1);
837                         (void) dup2(fd, 2);
838                         if (fd > 2) (void) close(fd);
839                 }
840                 setsid();
841         }
842
843         /* silently bounds check num_threads */
844         if (foreground)
845                 num_threads = 1;
846         else if (num_threads < 1)
847                 num_threads = 1;
848         else if (num_threads > MAX_THREADS)
849                 num_threads = MAX_THREADS;
850
851         if (num_threads > 1)
852                 fork_workers();
853
854         my_svc_run();
855
856         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
857         exit(1);
858 }
859
860 static void
861 usage(const char *prog, int n)
862 {
863         fprintf(stderr,
864 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
865 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
866 "       [-p|--port port] [-V version|--nfs-version version]\n"
867 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
868 "       [-H ha-callout-prog] [-s|--state-directory-path path]\n"
869 "       [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);
870         exit(n);
871 }