1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Herb Hasler and Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
37 #endif /*not lint*/
38
39 #if 0
40 #ifndef lint
41 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
42 #endif /*not lint*/
43 #endif
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD: stable/9/usr.sbin/mountd/mountd.c 308454 2016-11-08 21:52:55Z rmacklem $");
47
48 #include <sys/param.h>
49 #include <sys/fcntl.h>
50 #include <sys/linker.h>
51 #include <sys/module.h>
52 #include <sys/mount.h>
53 #include <sys/stat.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56
57 #include <rpc/rpc.h>
58 #include <rpc/rpc_com.h>
59 #include <rpc/pmap_clnt.h>
60 #include <rpc/pmap_prot.h>
61 #include <rpcsvc/mount.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfssvc.h>
64 #include <nfsserver/nfs.h>
65
66 #include <fs/nfs/nfsport.h>
67
68 #include <arpa/inet.h>
69
70 #include <ctype.h>
71 #include <err.h>
72 #include <errno.h>
73 #include <grp.h>
74 #include <libutil.h>
75 #include <limits.h>
76 #include <netdb.h>
77 #include <pwd.h>
78 #include <signal.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include "pathnames.h"
84 #include "mntopts.h"
85
86 #ifdef DEBUG
87 #include <stdarg.h>
88 #endif
89
90 /*
91 * Structures for keeping the mount list and export list
92 */
93 struct mountlist {
94 struct mountlist *ml_next;
95 char ml_host[MNTNAMLEN+1];
96 char ml_dirp[MNTPATHLEN+1];
97 };
98
99 struct dirlist {
100 struct dirlist *dp_left;
101 struct dirlist *dp_right;
102 int dp_flag;
103 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
104 char dp_dirp[1]; /* Actually malloc'd to size of dir */
105 };
106 /* dp_flag bits */
107 #define DP_DEFSET 0x1
108 #define DP_HOSTSET 0x2
109
110 struct exportlist {
111 struct exportlist *ex_next;
112 struct dirlist *ex_dirl;
113 struct dirlist *ex_defdir;
114 int ex_flag;
115 fsid_t ex_fs;
116 char *ex_fsdir;
117 char *ex_indexfile;
118 int ex_numsecflavors;
119 int ex_secflavors[MAXSECFLAVORS];
120 int ex_defnumsecflavors;
121 int ex_defsecflavors[MAXSECFLAVORS];
122 };
123 /* ex_flag bits */
124 #define EX_LINKED 0x1
125
126 struct netmsk {
127 struct sockaddr_storage nt_net;
128 struct sockaddr_storage nt_mask;
129 char *nt_name;
130 };
131
132 union grouptypes {
133 struct addrinfo *gt_addrinfo;
134 struct netmsk gt_net;
135 };
136
137 struct grouplist {
138 int gr_type;
139 union grouptypes gr_ptr;
140 struct grouplist *gr_next;
141 int gr_numsecflavors;
142 int gr_secflavors[MAXSECFLAVORS];
143 };
144 /* Group types */
145 #define GT_NULL 0x0
146 #define GT_HOST 0x1
147 #define GT_NET 0x2
148 #define GT_DEFAULT 0x3
149 #define GT_IGNORE 0x5
150
151 struct hostlist {
152 int ht_flag; /* Uses DP_xx bits */
153 struct grouplist *ht_grp;
154 struct hostlist *ht_next;
155 };
156
157 struct fhreturn {
158 int fhr_flag;
159 int fhr_vers;
160 nfsfh_t fhr_fh;
161 int fhr_numsecflavors;
162 int *fhr_secflavors;
163 };
164
165 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
166
167 /* Global defs */
168 char *add_expdir(struct dirlist **, char *, int);
169 void add_dlist(struct dirlist **, struct dirlist *,
170 struct grouplist *, int, struct exportlist *);
171 void add_mlist(char *, char *);
172 int check_dirpath(char *);
173 int check_options(struct dirlist *);
174 int checkmask(struct sockaddr *sa);
175 int chk_host(struct dirlist *, struct sockaddr *, int *, int *, int *,
176 int **);
177 static int create_service(struct netconfig *nconf);
178 static void complete_service(struct netconfig *nconf, char *port_str);
179 static void clearout_service(void);
180 void del_mlist(char *hostp, char *dirp);
181 struct dirlist *dirp_search(struct dirlist *, char *);
182 int do_mount(struct exportlist *, struct grouplist *, int,
183 struct xucred *, char *, int, struct statfs *);
184 int do_opt(char **, char **, struct exportlist *, struct grouplist *,
185 int *, int *, struct xucred *);
186 struct exportlist *ex_search(fsid_t *);
187 struct exportlist *get_exp(void);
188 void free_dir(struct dirlist *);
189 void free_exp(struct exportlist *);
190 void free_grp(struct grouplist *);
191 void free_host(struct hostlist *);
192 void get_exportlist(void);
193 int get_host(char *, struct grouplist *, struct grouplist *);
194 struct hostlist *get_ht(void);
195 int get_line(void);
196 void get_mountlist(void);
197 int get_net(char *, struct netmsk *, int);
198 void getexp_err(struct exportlist *, struct grouplist *);
199 struct grouplist *get_grp(void);
200 void hang_dirp(struct dirlist *, struct grouplist *,
201 struct exportlist *, int);
202 void huphandler(int sig);
203 int makemask(struct sockaddr_storage *ssp, int bitlen);
204 void mntsrv(struct svc_req *, SVCXPRT *);
205 void nextfield(char **, char **);
206 void out_of_mem(void);
207 void parsecred(char *, struct xucred *);
208 int parsesec(char *, struct exportlist *);
209 int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
210 void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
211 int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
212 struct sockaddr *samask);
213 int scan_tree(struct dirlist *, struct sockaddr *);
214 static void usage(void);
215 int xdr_dir(XDR *, char *);
216 int xdr_explist(XDR *, caddr_t);
217 int xdr_explist_brief(XDR *, caddr_t);
218 int xdr_explist_common(XDR *, caddr_t, int);
219 int xdr_fhs(XDR *, caddr_t);
220 int xdr_mlist(XDR *, caddr_t);
221 void terminate(int);
222
223 struct exportlist *exphead;
224 struct mountlist *mlhead;
225 struct grouplist *grphead;
226 char *exnames_default[2] = { _PATH_EXPORTS, NULL };
227 char **exnames;
228 char **hosts = NULL;
229 struct xucred def_anon = {
230 XUCRED_VERSION,
231 (uid_t)-2,
232 1,
233 { (gid_t)-2 },
234 NULL
235 };
236 int force_v2 = 0;
237 int resvport_only = 1;
238 int nhosts = 0;
239 int dir_only = 1;
240 int dolog = 0;
241 int got_sighup = 0;
242 int xcreated = 0;
243
244 char *svcport_str = NULL;
245 static int mallocd_svcport = 0;
246 static int *sock_fd;
247 static int sock_fdcnt;
248 static int sock_fdpos;
249 static int suspend_nfsd = 0;
250
251 int opt_flags;
252 static int have_v6 = 1;
253
254 int v4root_phase = 0;
255 char v4root_dirpath[PATH_MAX + 1];
256 int run_v4server = 1;
257 int has_publicfh = 0;
258
259 struct pidfh *pfh = NULL;
260 /* Bits for opt_flags above */
261 #define OP_MAPROOT 0x01
262 #define OP_MAPALL 0x02
263 /* 0x4 free */
264 #define OP_MASK 0x08
265 #define OP_NET 0x10
266 #define OP_ALLDIRS 0x40
267 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
268 #define OP_QUIET 0x100
269 #define OP_MASKLEN 0x200
270 #define OP_SEC 0x400
271
272 #ifdef DEBUG
273 int debug = 1;
274 void SYSLOG(int, const char *, ...) __printflike(2, 3);
275 #define syslog SYSLOG
276 #else
277 int debug = 0;
278 #endif
279
280 /*
281 * Mountd server for NFS mount protocol as described in:
282 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
283 * The optional arguments are the exports file name
284 * default: _PATH_EXPORTS
285 * and "-n" to allow nonroot mount.
286 */
287 int
main(int argc,char ** argv)288 main(int argc, char **argv)
289 {
290 fd_set readfds;
291 struct netconfig *nconf;
292 char *endptr, **hosts_bak;
293 void *nc_handle;
294 pid_t otherpid;
295 in_port_t svcport;
296 int c, k, s;
297 int maxrec = RPC_MAXDATASIZE;
298 int attempt_cnt, port_len, port_pos, ret;
299 char **port_list;
300
301 /* Check that another mountd isn't already running. */
302 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
303 if (pfh == NULL) {
304 if (errno == EEXIST)
305 errx(1, "mountd already running, pid: %d.", otherpid);
306 warn("cannot open or create pidfile");
307 }
308
309 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
310 if (s < 0)
311 have_v6 = 0;
312 else
313 close(s);
314
315 while ((c = getopt(argc, argv, "2deh:lnop:rS")) != -1)
316 switch (c) {
317 case '2':
318 force_v2 = 1;
319 break;
320 case 'e':
321 /* now a no-op, since this is the default */
322 break;
323 case 'n':
324 resvport_only = 0;
325 break;
326 case 'r':
327 dir_only = 0;
328 break;
329 case 'd':
330 debug = debug ? 0 : 1;
331 break;
332 case 'l':
333 dolog = 1;
334 break;
335 case 'o':
336 run_v4server = 0;
337 break;
338 case 'p':
339 endptr = NULL;
340 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
341 if (endptr == NULL || *endptr != '\0' ||
342 svcport == 0 || svcport >= IPPORT_MAX)
343 usage();
344 svcport_str = strdup(optarg);
345 break;
346 case 'h':
347 ++nhosts;
348 hosts_bak = hosts;
349 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
350 if (hosts_bak == NULL) {
351 if (hosts != NULL) {
352 for (k = 0; k < nhosts; k++)
353 free(hosts[k]);
354 free(hosts);
355 out_of_mem();
356 }
357 }
358 hosts = hosts_bak;
359 hosts[nhosts - 1] = strdup(optarg);
360 if (hosts[nhosts - 1] == NULL) {
361 for (k = 0; k < (nhosts - 1); k++)
362 free(hosts[k]);
363 free(hosts);
364 out_of_mem();
365 }
366 break;
367 case 'S':
368 suspend_nfsd = 1;
369 break;
370 default:
371 usage();
372 };
373
374 /*
375 * Unless the "-o" option was specified, try and run "nfsd".
376 * If "-o" was specified, try and run "nfsserver".
377 */
378 if (run_v4server > 0) {
379 if (modfind("nfsd") < 0) {
380 /* Not present in kernel, try loading it */
381 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
382 errx(1, "NFS server is not available");
383 }
384 } else if (modfind("nfsserver") < 0) {
385 /* Not present in kernel, try loading it */
386 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
387 errx(1, "NFS server is not available");
388 }
389
390 argc -= optind;
391 argv += optind;
392 grphead = (struct grouplist *)NULL;
393 exphead = (struct exportlist *)NULL;
394 mlhead = (struct mountlist *)NULL;
395 if (argc > 0)
396 exnames = argv;
397 else
398 exnames = exnames_default;
399 openlog("mountd", LOG_PID, LOG_DAEMON);
400 if (debug)
401 warnx("getting export list");
402 get_exportlist();
403 if (debug)
404 warnx("getting mount list");
405 get_mountlist();
406 if (debug)
407 warnx("here we go");
408 if (debug == 0) {
409 daemon(0, 0);
410 signal(SIGINT, SIG_IGN);
411 signal(SIGQUIT, SIG_IGN);
412 }
413 signal(SIGHUP, huphandler);
414 signal(SIGTERM, terminate);
415 signal(SIGPIPE, SIG_IGN);
416
417 pidfile_write(pfh);
418
419 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
420 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
421 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
422
423 if (!resvport_only) {
424 if (run_v4server != 0) {
425 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
426 &resvport_only, sizeof(resvport_only)) != 0 &&
427 errno != ENOENT) {
428 syslog(LOG_ERR, "sysctl: %m");
429 exit(1);
430 }
431 } else {
432 if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
433 &resvport_only, sizeof(resvport_only)) != 0 &&
434 errno != ENOENT) {
435 syslog(LOG_ERR, "sysctl: %m");
436 exit(1);
437 }
438 }
439 }
440
441 /*
442 * If no hosts were specified, add a wildcard entry to bind to
443 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
444 * list.
445 */
446 if (nhosts == 0) {
447 hosts = malloc(sizeof(char**));
448 if (hosts == NULL)
449 out_of_mem();
450 hosts[0] = "*";
451 nhosts = 1;
452 } else {
453 hosts_bak = hosts;
454 if (have_v6) {
455 hosts_bak = realloc(hosts, (nhosts + 2) *
456 sizeof(char *));
457 if (hosts_bak == NULL) {
458 for (k = 0; k < nhosts; k++)
459 free(hosts[k]);
460 free(hosts);
461 out_of_mem();
462 } else
463 hosts = hosts_bak;
464 nhosts += 2;
465 hosts[nhosts - 2] = "::1";
466 } else {
467 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
468 if (hosts_bak == NULL) {
469 for (k = 0; k < nhosts; k++)
470 free(hosts[k]);
471 free(hosts);
472 out_of_mem();
473 } else {
474 nhosts += 1;
475 hosts = hosts_bak;
476 }
477 }
478
479 hosts[nhosts - 1] = "127.0.0.1";
480 }
481
482 attempt_cnt = 1;
483 sock_fdcnt = 0;
484 sock_fd = NULL;
485 port_list = NULL;
486 port_len = 0;
487 nc_handle = setnetconfig();
488 while ((nconf = getnetconfig(nc_handle))) {
489 if (nconf->nc_flag & NC_VISIBLE) {
490 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
491 "inet6") == 0) {
492 /* DO NOTHING */
493 } else {
494 ret = create_service(nconf);
495 if (ret == 1)
496 /* Ignore this call */
497 continue;
498 if (ret < 0) {
499 /*
500 * Failed to bind port, so close off
501 * all sockets created and try again
502 * if the port# was dynamically
503 * assigned via bind(2).
504 */
505 clearout_service();
506 if (mallocd_svcport != 0 &&
507 attempt_cnt < GETPORT_MAXTRY) {
508 free(svcport_str);
509 svcport_str = NULL;
510 mallocd_svcport = 0;
511 } else {
512 errno = EADDRINUSE;
513 syslog(LOG_ERR,
514 "bindresvport_sa: %m");
515 exit(1);
516 }
517
518 /* Start over at the first service. */
519 free(sock_fd);
520 sock_fdcnt = 0;
521 sock_fd = NULL;
522 nc_handle = setnetconfig();
523 attempt_cnt++;
524 } else if (mallocd_svcport != 0 &&
525 attempt_cnt == GETPORT_MAXTRY) {
526 /*
527 * For the last attempt, allow
528 * different port #s for each nconf
529 * by saving the svcport_str and
530 * setting it back to NULL.
531 */
532 port_list = realloc(port_list,
533 (port_len + 1) * sizeof(char *));
534 if (port_list == NULL)
535 out_of_mem();
536 port_list[port_len++] = svcport_str;
537 svcport_str = NULL;
538 mallocd_svcport = 0;
539 }
540 }
541 }
542 }
543
544 /*
545 * Successfully bound the ports, so call complete_service() to
546 * do the rest of the setup on the service(s).
547 */
548 sock_fdpos = 0;
549 port_pos = 0;
550 nc_handle = setnetconfig();
551 while ((nconf = getnetconfig(nc_handle))) {
552 if (nconf->nc_flag & NC_VISIBLE) {
553 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
554 "inet6") == 0) {
555 /* DO NOTHING */
556 } else if (port_list != NULL) {
557 if (port_pos >= port_len) {
558 syslog(LOG_ERR, "too many port#s");
559 exit(1);
560 }
561 complete_service(nconf, port_list[port_pos++]);
562 } else
563 complete_service(nconf, svcport_str);
564 }
565 }
566 endnetconfig(nc_handle);
567 free(sock_fd);
568 if (port_list != NULL) {
569 for (port_pos = 0; port_pos < port_len; port_pos++)
570 free(port_list[port_pos]);
571 free(port_list);
572 }
573
574 if (xcreated == 0) {
575 syslog(LOG_ERR, "could not create any services");
576 exit(1);
577 }
578
579 /* Expand svc_run() here so that we can call get_exportlist(). */
580 for (;;) {
581 if (got_sighup) {
582 get_exportlist();
583 got_sighup = 0;
584 }
585 readfds = svc_fdset;
586 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
587 case -1:
588 if (errno == EINTR)
589 continue;
590 syslog(LOG_ERR, "mountd died: select: %m");
591 exit(1);
592 case 0:
593 continue;
594 default:
595 svc_getreqset(&readfds);
596 }
597 }
598 }
599
600 /*
601 * This routine creates and binds sockets on the appropriate
602 * addresses. It gets called one time for each transport.
603 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
604 * bind failed with EADDRINUSE.
605 * Any file descriptors that have been created are stored in sock_fd and
606 * the total count of them is maintained in sock_fdcnt.
607 */
608 static int
create_service(struct netconfig * nconf)609 create_service(struct netconfig *nconf)
610 {
611 struct addrinfo hints, *res = NULL;
612 struct sockaddr_in *sin;
613 struct sockaddr_in6 *sin6;
614 struct __rpc_sockinfo si;
615 int aicode;
616 int fd;
617 int nhostsbak;
618 int one = 1;
619 int r;
620 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
621 int mallocd_res;
622
623 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
624 (nconf->nc_semantics != NC_TPI_COTS) &&
625 (nconf->nc_semantics != NC_TPI_COTS_ORD))
626 return (1); /* not my type */
627
628 /*
629 * XXX - using RPC library internal functions.
630 */
631 if (!__rpc_nconf2sockinfo(nconf, &si)) {
632 syslog(LOG_ERR, "cannot get information for %s",
633 nconf->nc_netid);
634 return (1);
635 }
636
637 /* Get mountd's address on this transport */
638 memset(&hints, 0, sizeof hints);
639 hints.ai_flags = AI_PASSIVE;
640 hints.ai_family = si.si_af;
641 hints.ai_socktype = si.si_socktype;
642 hints.ai_protocol = si.si_proto;
643
644 /*
645 * Bind to specific IPs if asked to
646 */
647 nhostsbak = nhosts;
648 while (nhostsbak > 0) {
649 --nhostsbak;
650 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
651 if (sock_fd == NULL)
652 out_of_mem();
653 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
654 mallocd_res = 0;
655
656 /*
657 * XXX - using RPC library internal functions.
658 */
659 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
660 int non_fatal = 0;
661 if (errno == EPROTONOSUPPORT &&
662 nconf->nc_semantics != NC_TPI_CLTS)
663 non_fatal = 1;
664
665 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
666 "cannot create socket for %s", nconf->nc_netid);
667 if (non_fatal != 0)
668 continue;
669 exit(1);
670 }
671
672 switch (hints.ai_family) {
673 case AF_INET:
674 if (inet_pton(AF_INET, hosts[nhostsbak],
675 host_addr) == 1) {
676 hints.ai_flags |= AI_NUMERICHOST;
677 } else {
678 /*
679 * Skip if we have an AF_INET6 address.
680 */
681 if (inet_pton(AF_INET6, hosts[nhostsbak],
682 host_addr) == 1) {
683 close(fd);
684 continue;
685 }
686 }
687 break;
688 case AF_INET6:
689 if (inet_pton(AF_INET6, hosts[nhostsbak],
690 host_addr) == 1) {
691 hints.ai_flags |= AI_NUMERICHOST;
692 } else {
693 /*
694 * Skip if we have an AF_INET address.
695 */
696 if (inet_pton(AF_INET, hosts[nhostsbak],
697 host_addr) == 1) {
698 close(fd);
699 continue;
700 }
701 }
702
703 /*
704 * We're doing host-based access checks here, so don't
705 * allow v4-in-v6 to confuse things. The kernel will
706 * disable it by default on NFS sockets too.
707 */
708 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
709 sizeof one) < 0) {
710 syslog(LOG_ERR,
711 "can't disable v4-in-v6 on IPv6 socket");
712 exit(1);
713 }
714 break;
715 default:
716 break;
717 }
718
719 /*
720 * If no hosts were specified, just bind to INADDR_ANY
721 */
722 if (strcmp("*", hosts[nhostsbak]) == 0) {
723 if (svcport_str == NULL) {
724 res = malloc(sizeof(struct addrinfo));
725 if (res == NULL)
726 out_of_mem();
727 mallocd_res = 1;
728 res->ai_flags = hints.ai_flags;
729 res->ai_family = hints.ai_family;
730 res->ai_protocol = hints.ai_protocol;
731 switch (res->ai_family) {
732 case AF_INET:
733 sin = malloc(sizeof(struct sockaddr_in));
734 if (sin == NULL)
735 out_of_mem();
736 sin->sin_family = AF_INET;
737 sin->sin_port = htons(0);
738 sin->sin_addr.s_addr = htonl(INADDR_ANY);
739 res->ai_addr = (struct sockaddr*) sin;
740 res->ai_addrlen = (socklen_t)
741 sizeof(struct sockaddr_in);
742 break;
743 case AF_INET6:
744 sin6 = malloc(sizeof(struct sockaddr_in6));
745 if (sin6 == NULL)
746 out_of_mem();
747 sin6->sin6_family = AF_INET6;
748 sin6->sin6_port = htons(0);
749 sin6->sin6_addr = in6addr_any;
750 res->ai_addr = (struct sockaddr*) sin6;
751 res->ai_addrlen = (socklen_t)
752 sizeof(struct sockaddr_in6);
753 break;
754 default:
755 syslog(LOG_ERR, "bad addr fam %d",
756 res->ai_family);
757 exit(1);
758 }
759 } else {
760 if ((aicode = getaddrinfo(NULL, svcport_str,
761 &hints, &res)) != 0) {
762 syslog(LOG_ERR,
763 "cannot get local address for %s: %s",
764 nconf->nc_netid,
765 gai_strerror(aicode));
766 close(fd);
767 continue;
768 }
769 }
770 } else {
771 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
772 &hints, &res)) != 0) {
773 syslog(LOG_ERR,
774 "cannot get local address for %s: %s",
775 nconf->nc_netid, gai_strerror(aicode));
776 close(fd);
777 continue;
778 }
779 }
780
781 /* Store the fd. */
782 sock_fd[sock_fdcnt - 1] = fd;
783
784 /* Now, attempt the bind. */
785 r = bindresvport_sa(fd, res->ai_addr);
786 if (r != 0) {
787 if (errno == EADDRINUSE && mallocd_svcport != 0) {
788 if (mallocd_res != 0) {
789 free(res->ai_addr);
790 free(res);
791 } else
792 freeaddrinfo(res);
793 return (-1);
794 }
795 syslog(LOG_ERR, "bindresvport_sa: %m");
796 exit(1);
797 }
798
799 if (svcport_str == NULL) {
800 svcport_str = malloc(NI_MAXSERV * sizeof(char));
801 if (svcport_str == NULL)
802 out_of_mem();
803 mallocd_svcport = 1;
804
805 if (getnameinfo(res->ai_addr,
806 res->ai_addr->sa_len, NULL, NI_MAXHOST,
807 svcport_str, NI_MAXSERV * sizeof(char),
808 NI_NUMERICHOST | NI_NUMERICSERV))
809 errx(1, "Cannot get port number");
810 }
811 if (mallocd_res != 0) {
812 free(res->ai_addr);
813 free(res);
814 } else
815 freeaddrinfo(res);
816 res = NULL;
817 }
818 return (0);
819 }
820
821 /*
822 * Called after all the create_service() calls have succeeded, to complete
823 * the setup and registration.
824 */
825 static void
complete_service(struct netconfig * nconf,char * port_str)826 complete_service(struct netconfig *nconf, char *port_str)
827 {
828 struct addrinfo hints, *res = NULL;
829 struct __rpc_sockinfo si;
830 struct netbuf servaddr;
831 SVCXPRT *transp = NULL;
832 int aicode, fd, nhostsbak;
833 int registered = 0;
834
835 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
836 (nconf->nc_semantics != NC_TPI_COTS) &&
837 (nconf->nc_semantics != NC_TPI_COTS_ORD))
838 return; /* not my type */
839
840 /*
841 * XXX - using RPC library internal functions.
842 */
843 if (!__rpc_nconf2sockinfo(nconf, &si)) {
844 syslog(LOG_ERR, "cannot get information for %s",
845 nconf->nc_netid);
846 return;
847 }
848
849 nhostsbak = nhosts;
850 while (nhostsbak > 0) {
851 --nhostsbak;
852 if (sock_fdpos >= sock_fdcnt) {
853 /* Should never happen. */
854 syslog(LOG_ERR, "Ran out of socket fd's");
855 return;
856 }
857 fd = sock_fd[sock_fdpos++];
858 if (fd < 0)
859 continue;
860
861 if (nconf->nc_semantics != NC_TPI_CLTS)
862 listen(fd, SOMAXCONN);
863
864 if (nconf->nc_semantics == NC_TPI_CLTS )
865 transp = svc_dg_create(fd, 0, 0);
866 else
867 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
868 RPC_MAXDATASIZE);
869
870 if (transp != (SVCXPRT *) NULL) {
871 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
872 NULL))
873 syslog(LOG_ERR,
874 "can't register %s MOUNTVERS service",
875 nconf->nc_netid);
876 if (!force_v2) {
877 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
878 mntsrv, NULL))
879 syslog(LOG_ERR,
880 "can't register %s MOUNTVERS3 service",
881 nconf->nc_netid);
882 }
883 } else
884 syslog(LOG_WARNING, "can't create %s services",
885 nconf->nc_netid);
886
887 if (registered == 0) {
888 registered = 1;
889 memset(&hints, 0, sizeof hints);
890 hints.ai_flags = AI_PASSIVE;
891 hints.ai_family = si.si_af;
892 hints.ai_socktype = si.si_socktype;
893 hints.ai_protocol = si.si_proto;
894
895 if ((aicode = getaddrinfo(NULL, port_str, &hints,
896 &res)) != 0) {
897 syslog(LOG_ERR, "cannot get local address: %s",
898 gai_strerror(aicode));
899 exit(1);
900 }
901
902 servaddr.buf = malloc(res->ai_addrlen);
903 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
904 servaddr.len = res->ai_addrlen;
905
906 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
907 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
908
909 xcreated++;
910 freeaddrinfo(res);
911 }
912 } /* end while */
913 }
914
915 /*
916 * Clear out sockets after a failure to bind one of them, so that the
917 * cycle of socket creation/binding can start anew.
918 */
919 static void
clearout_service(void)920 clearout_service(void)
921 {
922 int i;
923
924 for (i = 0; i < sock_fdcnt; i++) {
925 if (sock_fd[i] >= 0) {
926 shutdown(sock_fd[i], SHUT_RDWR);
927 close(sock_fd[i]);
928 }
929 }
930 }
931
932 static void
usage(void)933 usage(void)
934 {
935 fprintf(stderr,
936 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
937 "[-S] [-h <bindip>] [export_file ...]\n");
938 exit(1);
939 }
940
941 /*
942 * The mount rpc service
943 */
944 void
mntsrv(struct svc_req * rqstp,SVCXPRT * transp)945 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
946 {
947 struct exportlist *ep;
948 struct dirlist *dp;
949 struct fhreturn fhr;
950 struct stat stb;
951 struct statfs fsb;
952 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
953 int lookup_failed = 1;
954 struct sockaddr *saddr;
955 u_short sport;
956 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
957 int bad = 0, defset, hostset;
958 sigset_t sighup_mask;
959 int numsecflavors, *secflavorsp;
960
961 sigemptyset(&sighup_mask);
962 sigaddset(&sighup_mask, SIGHUP);
963 saddr = svc_getrpccaller(transp)->buf;
964 switch (saddr->sa_family) {
965 case AF_INET6:
966 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
967 break;
968 case AF_INET:
969 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
970 break;
971 default:
972 syslog(LOG_ERR, "request from unknown address family");
973 return;
974 }
975 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
976 NULL, 0, 0);
977 getnameinfo(saddr, saddr->sa_len, numerichost,
978 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
979 switch (rqstp->rq_proc) {
980 case NULLPROC:
981 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
982 syslog(LOG_ERR, "can't send reply");
983 return;
984 case MOUNTPROC_MNT:
985 if (sport >= IPPORT_RESERVED && resvport_only) {
986 syslog(LOG_NOTICE,
987 "mount request from %s from unprivileged port",
988 numerichost);
989 svcerr_weakauth(transp);
990 return;
991 }
992 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
993 syslog(LOG_NOTICE, "undecodable mount request from %s",
994 numerichost);
995 svcerr_decode(transp);
996 return;
997 }
998
999 /*
1000 * Get the real pathname and make sure it is a directory
1001 * or a regular file if the -r option was specified
1002 * and it exists.
1003 */
1004 if (realpath(rpcpath, dirpath) == NULL ||
1005 stat(dirpath, &stb) < 0 ||
1006 (!S_ISDIR(stb.st_mode) &&
1007 (dir_only || !S_ISREG(stb.st_mode))) ||
1008 statfs(dirpath, &fsb) < 0) {
1009 chdir("/"); /* Just in case realpath doesn't */
1010 syslog(LOG_NOTICE,
1011 "mount request from %s for non existent path %s",
1012 numerichost, dirpath);
1013 if (debug)
1014 warnx("stat failed on %s", dirpath);
1015 bad = ENOENT; /* We will send error reply later */
1016 }
1017
1018 /* Check in the exports list */
1019 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1020 ep = ex_search(&fsb.f_fsid);
1021 hostset = defset = 0;
1022 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1023 &numsecflavors, &secflavorsp) ||
1024 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1025 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1026 &secflavorsp)) ||
1027 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1028 scan_tree(ep->ex_dirl, saddr) == 0))) {
1029 if (bad) {
1030 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1031 (caddr_t)&bad))
1032 syslog(LOG_ERR, "can't send reply");
1033 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1034 return;
1035 }
1036 if (hostset & DP_HOSTSET) {
1037 fhr.fhr_flag = hostset;
1038 fhr.fhr_numsecflavors = numsecflavors;
1039 fhr.fhr_secflavors = secflavorsp;
1040 } else {
1041 fhr.fhr_flag = defset;
1042 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1043 fhr.fhr_secflavors = ep->ex_defsecflavors;
1044 }
1045 fhr.fhr_vers = rqstp->rq_vers;
1046 /* Get the file handle */
1047 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1048 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1049 bad = errno;
1050 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1051 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1052 (caddr_t)&bad))
1053 syslog(LOG_ERR, "can't send reply");
1054 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1055 return;
1056 }
1057 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1058 (caddr_t)&fhr))
1059 syslog(LOG_ERR, "can't send reply");
1060 if (!lookup_failed)
1061 add_mlist(host, dirpath);
1062 else
1063 add_mlist(numerichost, dirpath);
1064 if (debug)
1065 warnx("mount successful");
1066 if (dolog)
1067 syslog(LOG_NOTICE,
1068 "mount request succeeded from %s for %s",
1069 numerichost, dirpath);
1070 } else {
1071 bad = EACCES;
1072 syslog(LOG_NOTICE,
1073 "mount request denied from %s for %s",
1074 numerichost, dirpath);
1075 }
1076
1077 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1078 (caddr_t)&bad))
1079 syslog(LOG_ERR, "can't send reply");
1080 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1081 return;
1082 case MOUNTPROC_DUMP:
1083 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1084 syslog(LOG_ERR, "can't send reply");
1085 else if (dolog)
1086 syslog(LOG_NOTICE,
1087 "dump request succeeded from %s",
1088 numerichost);
1089 return;
1090 case MOUNTPROC_UMNT:
1091 if (sport >= IPPORT_RESERVED && resvport_only) {
1092 syslog(LOG_NOTICE,
1093 "umount request from %s from unprivileged port",
1094 numerichost);
1095 svcerr_weakauth(transp);
1096 return;
1097 }
1098 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1099 syslog(LOG_NOTICE, "undecodable umount request from %s",
1100 numerichost);
1101 svcerr_decode(transp);
1102 return;
1103 }
1104 if (realpath(rpcpath, dirpath) == NULL) {
1105 syslog(LOG_NOTICE, "umount request from %s "
1106 "for non existent path %s",
1107 numerichost, dirpath);
1108 }
1109 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1110 syslog(LOG_ERR, "can't send reply");
1111 if (!lookup_failed)
1112 del_mlist(host, dirpath);
1113 del_mlist(numerichost, dirpath);
1114 if (dolog)
1115 syslog(LOG_NOTICE,
1116 "umount request succeeded from %s for %s",
1117 numerichost, dirpath);
1118 return;
1119 case MOUNTPROC_UMNTALL:
1120 if (sport >= IPPORT_RESERVED && resvport_only) {
1121 syslog(LOG_NOTICE,
1122 "umountall request from %s from unprivileged port",
1123 numerichost);
1124 svcerr_weakauth(transp);
1125 return;
1126 }
1127 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1128 syslog(LOG_ERR, "can't send reply");
1129 if (!lookup_failed)
1130 del_mlist(host, NULL);
1131 del_mlist(numerichost, NULL);
1132 if (dolog)
1133 syslog(LOG_NOTICE,
1134 "umountall request succeeded from %s",
1135 numerichost);
1136 return;
1137 case MOUNTPROC_EXPORT:
1138 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1139 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1140 (caddr_t)NULL))
1141 syslog(LOG_ERR, "can't send reply");
1142 if (dolog)
1143 syslog(LOG_NOTICE,
1144 "export request succeeded from %s",
1145 numerichost);
1146 return;
1147 default:
1148 svcerr_noproc(transp);
1149 return;
1150 }
1151 }
1152
1153 /*
1154 * Xdr conversion for a dirpath string
1155 */
1156 int
xdr_dir(XDR * xdrsp,char * dirp)1157 xdr_dir(XDR *xdrsp, char *dirp)
1158 {
1159 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1160 }
1161
1162 /*
1163 * Xdr routine to generate file handle reply
1164 */
1165 int
xdr_fhs(XDR * xdrsp,caddr_t cp)1166 xdr_fhs(XDR *xdrsp, caddr_t cp)
1167 {
1168 struct fhreturn *fhrp = (struct fhreturn *)cp;
1169 u_long ok = 0, len, auth;
1170 int i;
1171
1172 if (!xdr_long(xdrsp, &ok))
1173 return (0);
1174 switch (fhrp->fhr_vers) {
1175 case 1:
1176 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1177 case 3:
1178 len = NFSX_V3FH;
1179 if (!xdr_long(xdrsp, &len))
1180 return (0);
1181 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1182 return (0);
1183 if (fhrp->fhr_numsecflavors) {
1184 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1185 return (0);
1186 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1187 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1188 return (0);
1189 return (1);
1190 } else {
1191 auth = AUTH_SYS;
1192 len = 1;
1193 if (!xdr_long(xdrsp, &len))
1194 return (0);
1195 return (xdr_long(xdrsp, &auth));
1196 }
1197 };
1198 return (0);
1199 }
1200
1201 int
xdr_mlist(XDR * xdrsp,caddr_t cp __unused)1202 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1203 {
1204 struct mountlist *mlp;
1205 int true = 1;
1206 int false = 0;
1207 char *strp;
1208
1209 mlp = mlhead;
1210 while (mlp) {
1211 if (!xdr_bool(xdrsp, &true))
1212 return (0);
1213 strp = &mlp->ml_host[0];
1214 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1215 return (0);
1216 strp = &mlp->ml_dirp[0];
1217 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1218 return (0);
1219 mlp = mlp->ml_next;
1220 }
1221 if (!xdr_bool(xdrsp, &false))
1222 return (0);
1223 return (1);
1224 }
1225
1226 /*
1227 * Xdr conversion for export list
1228 */
1229 int
xdr_explist_common(XDR * xdrsp,caddr_t cp __unused,int brief)1230 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1231 {
1232 struct exportlist *ep;
1233 int false = 0;
1234 int putdef;
1235 sigset_t sighup_mask;
1236
1237 sigemptyset(&sighup_mask);
1238 sigaddset(&sighup_mask, SIGHUP);
1239 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1240 ep = exphead;
1241 while (ep) {
1242 putdef = 0;
1243 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1244 &putdef, brief))
1245 goto errout;
1246 if (ep->ex_defdir && putdef == 0 &&
1247 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1248 &putdef, brief))
1249 goto errout;
1250 ep = ep->ex_next;
1251 }
1252 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1253 if (!xdr_bool(xdrsp, &false))
1254 return (0);
1255 return (1);
1256 errout:
1257 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1258 return (0);
1259 }
1260
1261 /*
1262 * Called from xdr_explist() to traverse the tree and export the
1263 * directory paths.
1264 */
1265 int
put_exlist(struct dirlist * dp,XDR * xdrsp,struct dirlist * adp,int * putdefp,int brief)1266 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1267 int brief)
1268 {
1269 struct grouplist *grp;
1270 struct hostlist *hp;
1271 int true = 1;
1272 int false = 0;
1273 int gotalldir = 0;
1274 char *strp;
1275
1276 if (dp) {
1277 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1278 return (1);
1279 if (!xdr_bool(xdrsp, &true))
1280 return (1);
1281 strp = dp->dp_dirp;
1282 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1283 return (1);
1284 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1285 gotalldir = 1;
1286 *putdefp = 1;
1287 }
1288 if (brief) {
1289 if (!xdr_bool(xdrsp, &true))
1290 return (1);
1291 strp = "(...)";
1292 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1293 return (1);
1294 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1295 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1296 hp = dp->dp_hosts;
1297 while (hp) {
1298 grp = hp->ht_grp;
1299 if (grp->gr_type == GT_HOST) {
1300 if (!xdr_bool(xdrsp, &true))
1301 return (1);
1302 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1303 if (!xdr_string(xdrsp, &strp,
1304 MNTNAMLEN))
1305 return (1);
1306 } else if (grp->gr_type == GT_NET) {
1307 if (!xdr_bool(xdrsp, &true))
1308 return (1);
1309 strp = grp->gr_ptr.gt_net.nt_name;
1310 if (!xdr_string(xdrsp, &strp,
1311 MNTNAMLEN))
1312 return (1);
1313 }
1314 hp = hp->ht_next;
1315 if (gotalldir && hp == (struct hostlist *)NULL) {
1316 hp = adp->dp_hosts;
1317 gotalldir = 0;
1318 }
1319 }
1320 }
1321 if (!xdr_bool(xdrsp, &false))
1322 return (1);
1323 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1324 return (1);
1325 }
1326 return (0);
1327 }
1328
1329 int
xdr_explist(XDR * xdrsp,caddr_t cp)1330 xdr_explist(XDR *xdrsp, caddr_t cp)
1331 {
1332
1333 return xdr_explist_common(xdrsp, cp, 0);
1334 }
1335
1336 int
xdr_explist_brief(XDR * xdrsp,caddr_t cp)1337 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1338 {
1339
1340 return xdr_explist_common(xdrsp, cp, 1);
1341 }
1342
1343 char *line;
1344 int linesize;
1345 FILE *exp_file;
1346
1347 /*
1348 * Get the export list from one, currently open file
1349 */
1350 static void
get_exportlist_one(void)1351 get_exportlist_one(void)
1352 {
1353 struct exportlist *ep, *ep2;
1354 struct grouplist *grp, *tgrp;
1355 struct exportlist **epp;
1356 struct dirlist *dirhead;
1357 struct statfs fsb;
1358 struct xucred anon;
1359 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1360 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1361
1362 v4root_phase = 0;
1363 dirhead = (struct dirlist *)NULL;
1364 while (get_line()) {
1365 if (debug)
1366 warnx("got line %s", line);
1367 cp = line;
1368 nextfield(&cp, &endcp);
1369 if (*cp == '#')
1370 goto nextline;
1371
1372 /*
1373 * Set defaults.
1374 */
1375 has_host = FALSE;
1376 anon = def_anon;
1377 exflags = MNT_EXPORTED;
1378 got_nondir = 0;
1379 opt_flags = 0;
1380 ep = (struct exportlist *)NULL;
1381 dirp = NULL;
1382
1383 /*
1384 * Handle the V4 root dir.
1385 */
1386 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1387 /*
1388 * V4: just indicates that it is the v4 root point,
1389 * so skip over that and set v4root_phase.
1390 */
1391 if (v4root_phase > 0) {
1392 syslog(LOG_ERR, "V4:duplicate line, ignored");
1393 goto nextline;
1394 }
1395 v4root_phase = 1;
1396 cp += 3;
1397 nextfield(&cp, &endcp);
1398 }
1399
1400 /*
1401 * Create new exports list entry
1402 */
1403 len = endcp-cp;
1404 tgrp = grp = get_grp();
1405 while (len > 0) {
1406 if (len > MNTNAMLEN) {
1407 getexp_err(ep, tgrp);
1408 goto nextline;
1409 }
1410 if (*cp == '-') {
1411 if (ep == (struct exportlist *)NULL) {
1412 getexp_err(ep, tgrp);
1413 goto nextline;
1414 }
1415 if (debug)
1416 warnx("doing opt %s", cp);
1417 got_nondir = 1;
1418 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1419 &exflags, &anon)) {
1420 getexp_err(ep, tgrp);
1421 goto nextline;
1422 }
1423 } else if (*cp == '/') {
1424 savedc = *endcp;
1425 *endcp = '\0';
1426 if (v4root_phase > 1) {
1427 if (dirp != NULL) {
1428 syslog(LOG_ERR, "Multiple V4 dirs");
1429 getexp_err(ep, tgrp);
1430 goto nextline;
1431 }
1432 }
1433 if (check_dirpath(cp) &&
1434 statfs(cp, &fsb) >= 0) {
1435 if (got_nondir) {
1436 syslog(LOG_ERR, "dirs must be first");
1437 getexp_err(ep, tgrp);
1438 goto nextline;
1439 }
1440 if (v4root_phase == 1) {
1441 if (dirp != NULL) {
1442 syslog(LOG_ERR, "Multiple V4 dirs");
1443 getexp_err(ep, tgrp);
1444 goto nextline;
1445 }
1446 if (strlen(v4root_dirpath) == 0) {
1447 strlcpy(v4root_dirpath, cp,
1448 sizeof (v4root_dirpath));
1449 } else if (strcmp(v4root_dirpath, cp)
1450 != 0) {
1451 syslog(LOG_ERR,
1452 "different V4 dirpath %s", cp);
1453 getexp_err(ep, tgrp);
1454 goto nextline;
1455 }
1456 dirp = cp;
1457 v4root_phase = 2;
1458 got_nondir = 1;
1459 ep = get_exp();
1460 } else {
1461 if (ep) {
1462 if (ep->ex_fs.val[0] !=
1463 fsb.f_fsid.val[0] ||
1464 ep->ex_fs.val[1] !=
1465 fsb.f_fsid.val[1]) {
1466 getexp_err(ep, tgrp);
1467 goto nextline;
1468 }
1469 } else {
1470 /*
1471 * See if this directory is already
1472 * in the list.
1473 */
1474 ep = ex_search(&fsb.f_fsid);
1475 if (ep == (struct exportlist *)NULL) {
1476 ep = get_exp();
1477 ep->ex_fs = fsb.f_fsid;
1478 ep->ex_fsdir = (char *)malloc
1479 (strlen(fsb.f_mntonname) + 1);
1480 if (ep->ex_fsdir)
1481 strcpy(ep->ex_fsdir,
1482 fsb.f_mntonname);
1483 else
1484 out_of_mem();
1485 if (debug)
1486 warnx(
1487 "making new ep fs=0x%x,0x%x",
1488 fsb.f_fsid.val[0],
1489 fsb.f_fsid.val[1]);
1490 } else if (debug)
1491 warnx("found ep fs=0x%x,0x%x",
1492 fsb.f_fsid.val[0],
1493 fsb.f_fsid.val[1]);
1494 }
1495
1496 /*
1497 * Add dirpath to export mount point.
1498 */
1499 dirp = add_expdir(&dirhead, cp, len);
1500 dirplen = len;
1501 }
1502 } else {
1503 getexp_err(ep, tgrp);
1504 goto nextline;
1505 }
1506 *endcp = savedc;
1507 } else {
1508 savedc = *endcp;
1509 *endcp = '\0';
1510 got_nondir = 1;
1511 if (ep == (struct exportlist *)NULL) {
1512 getexp_err(ep, tgrp);
1513 goto nextline;
1514 }
1515
1516 /*
1517 * Get the host or netgroup.
1518 */
1519 setnetgrent(cp);
1520 netgrp = getnetgrent(&hst, &usr, &dom);
1521 do {
1522 if (has_host) {
1523 grp->gr_next = get_grp();
1524 grp = grp->gr_next;
1525 }
1526 if (netgrp) {
1527 if (hst == 0) {
1528 syslog(LOG_ERR,
1529 "null hostname in netgroup %s, skipping", cp);
1530 grp->gr_type = GT_IGNORE;
1531 } else if (get_host(hst, grp, tgrp)) {
1532 syslog(LOG_ERR,
1533 "bad host %s in netgroup %s, skipping", hst, cp);
1534 grp->gr_type = GT_IGNORE;
1535 }
1536 } else if (get_host(cp, grp, tgrp)) {
1537 syslog(LOG_ERR, "bad host %s, skipping", cp);
1538 grp->gr_type = GT_IGNORE;
1539 }
1540 has_host = TRUE;
1541 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1542 endnetgrent();
1543 *endcp = savedc;
1544 }
1545 cp = endcp;
1546 nextfield(&cp, &endcp);
1547 len = endcp - cp;
1548 }
1549 if (check_options(dirhead)) {
1550 getexp_err(ep, tgrp);
1551 goto nextline;
1552 }
1553 if (!has_host) {
1554 grp->gr_type = GT_DEFAULT;
1555 if (debug)
1556 warnx("adding a default entry");
1557
1558 /*
1559 * Don't allow a network export coincide with a list of
1560 * host(s) on the same line.
1561 */
1562 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1563 syslog(LOG_ERR, "network/host conflict");
1564 getexp_err(ep, tgrp);
1565 goto nextline;
1566
1567 /*
1568 * If an export list was specified on this line, make sure
1569 * that we have at least one valid entry, otherwise skip it.
1570 */
1571 } else {
1572 grp = tgrp;
1573 while (grp && grp->gr_type == GT_IGNORE)
1574 grp = grp->gr_next;
1575 if (! grp) {
1576 getexp_err(ep, tgrp);
1577 goto nextline;
1578 }
1579 }
1580
1581 if (v4root_phase == 1) {
1582 syslog(LOG_ERR, "V4:root, no dirp, ignored");
1583 getexp_err(ep, tgrp);
1584 goto nextline;
1585 }
1586
1587 /*
1588 * Loop through hosts, pushing the exports into the kernel.
1589 * After loop, tgrp points to the start of the list and
1590 * grp points to the last entry in the list.
1591 */
1592 grp = tgrp;
1593 do {
1594 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1595 &fsb)) {
1596 getexp_err(ep, tgrp);
1597 goto nextline;
1598 }
1599 } while (grp->gr_next && (grp = grp->gr_next));
1600
1601 /*
1602 * For V4: don't enter in mount lists.
1603 */
1604 if (v4root_phase > 0 && v4root_phase <= 2) {
1605 /*
1606 * Since these structures aren't used by mountd,
1607 * free them up now.
1608 */
1609 if (ep != NULL)
1610 free_exp(ep);
1611 while (tgrp != NULL) {
1612 grp = tgrp;
1613 tgrp = tgrp->gr_next;
1614 free_grp(grp);
1615 }
1616 goto nextline;
1617 }
1618
1619 /*
1620 * Success. Update the data structures.
1621 */
1622 if (has_host) {
1623 hang_dirp(dirhead, tgrp, ep, opt_flags);
1624 grp->gr_next = grphead;
1625 grphead = tgrp;
1626 } else {
1627 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1628 opt_flags);
1629 free_grp(grp);
1630 }
1631 dirhead = (struct dirlist *)NULL;
1632 if ((ep->ex_flag & EX_LINKED) == 0) {
1633 ep2 = exphead;
1634 epp = &exphead;
1635
1636 /*
1637 * Insert in the list in alphabetical order.
1638 */
1639 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1640 epp = &ep2->ex_next;
1641 ep2 = ep2->ex_next;
1642 }
1643 if (ep2)
1644 ep->ex_next = ep2;
1645 *epp = ep;
1646 ep->ex_flag |= EX_LINKED;
1647 }
1648 nextline:
1649 v4root_phase = 0;
1650 if (dirhead) {
1651 free_dir(dirhead);
1652 dirhead = (struct dirlist *)NULL;
1653 }
1654 }
1655 }
1656
1657 /*
1658 * Get the export list from all specified files
1659 */
1660 void
get_exportlist(void)1661 get_exportlist(void)
1662 {
1663 struct exportlist *ep, *ep2;
1664 struct grouplist *grp, *tgrp;
1665 struct export_args export;
1666 struct iovec *iov;
1667 struct statfs *fsp, *mntbufp;
1668 struct xvfsconf vfc;
1669 char *dirp;
1670 char errmsg[255];
1671 int dirplen, num, i;
1672 int iovlen;
1673 int done;
1674 struct nfsex_args eargs;
1675
1676 if (suspend_nfsd != 0)
1677 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1678 v4root_dirpath[0] = '\0';
1679 bzero(&export, sizeof(export));
1680 export.ex_flags = MNT_DELEXPORT;
1681 dirp = NULL;
1682 dirplen = 0;
1683 iov = NULL;
1684 iovlen = 0;
1685 bzero(errmsg, sizeof(errmsg));
1686
1687 /*
1688 * First, get rid of the old list
1689 */
1690 ep = exphead;
1691 while (ep) {
1692 ep2 = ep;
1693 ep = ep->ex_next;
1694 free_exp(ep2);
1695 }
1696 exphead = (struct exportlist *)NULL;
1697
1698 grp = grphead;
1699 while (grp) {
1700 tgrp = grp;
1701 grp = grp->gr_next;
1702 free_grp(tgrp);
1703 }
1704 grphead = (struct grouplist *)NULL;
1705
1706 /*
1707 * and the old V4 root dir.
1708 */
1709 bzero(&eargs, sizeof (eargs));
1710 eargs.export.ex_flags = MNT_DELEXPORT;
1711 if (run_v4server > 0 &&
1712 nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1713 errno != ENOENT)
1714 syslog(LOG_ERR, "Can't delete exports for V4:");
1715
1716 /*
1717 * and clear flag that notes if a public fh has been exported.
1718 */
1719 has_publicfh = 0;
1720
1721 /*
1722 * And delete exports that are in the kernel for all local
1723 * filesystems.
1724 * XXX: Should know how to handle all local exportable filesystems.
1725 */
1726 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1727
1728 if (num > 0) {
1729 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1730 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1731 build_iovec(&iov, &iovlen, "from", NULL, 0);
1732 build_iovec(&iov, &iovlen, "update", NULL, 0);
1733 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1734 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1735 }
1736
1737 for (i = 0; i < num; i++) {
1738 fsp = &mntbufp[i];
1739 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1740 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1741 fsp->f_fstypename);
1742 continue;
1743 }
1744
1745 /*
1746 * Do not delete export for network filesystem by
1747 * passing "export" arg to nmount().
1748 * It only makes sense to do this for local filesystems.
1749 */
1750 if (vfc.vfc_flags & VFCF_NETWORK)
1751 continue;
1752
1753 iov[1].iov_base = fsp->f_fstypename;
1754 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1755 iov[3].iov_base = fsp->f_mntonname;
1756 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1757 iov[5].iov_base = fsp->f_mntfromname;
1758 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1759 errmsg[0] = '\0';
1760
1761 if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1762 errno != ENOENT && errno != ENOTSUP) {
1763 syslog(LOG_ERR,
1764 "can't delete exports for %s: %m %s",
1765 fsp->f_mntonname, errmsg);
1766 }
1767 }
1768
1769 if (iov != NULL) {
1770 /* Free strings allocated by strdup() in getmntopts.c */
1771 free(iov[0].iov_base); /* fstype */
1772 free(iov[2].iov_base); /* fspath */
1773 free(iov[4].iov_base); /* from */
1774 free(iov[6].iov_base); /* update */
1775 free(iov[8].iov_base); /* export */
1776 free(iov[10].iov_base); /* errmsg */
1777
1778 /* free iov, allocated by realloc() */
1779 free(iov);
1780 iovlen = 0;
1781 }
1782
1783 /*
1784 * Read in the exports file and build the list, calling
1785 * nmount() as we go along to push the export rules into the kernel.
1786 */
1787 done = 0;
1788 for (i = 0; exnames[i] != NULL; i++) {
1789 if (debug)
1790 warnx("reading exports from %s", exnames[i]);
1791 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1792 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1793 continue;
1794 }
1795 get_exportlist_one();
1796 fclose(exp_file);
1797 done++;
1798 }
1799 if (done == 0) {
1800 syslog(LOG_ERR, "can't open any exports file");
1801 exit(2);
1802 }
1803
1804 /*
1805 * If there was no public fh, clear any previous one set.
1806 */
1807 if (run_v4server > 0 && has_publicfh == 0)
1808 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1809
1810 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1811 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1812 }
1813
1814 /*
1815 * Allocate an export list element
1816 */
1817 struct exportlist *
get_exp(void)1818 get_exp(void)
1819 {
1820 struct exportlist *ep;
1821
1822 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1823 if (ep == (struct exportlist *)NULL)
1824 out_of_mem();
1825 return (ep);
1826 }
1827
1828 /*
1829 * Allocate a group list element
1830 */
1831 struct grouplist *
get_grp(void)1832 get_grp(void)
1833 {
1834 struct grouplist *gp;
1835
1836 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1837 if (gp == (struct grouplist *)NULL)
1838 out_of_mem();
1839 return (gp);
1840 }
1841
1842 /*
1843 * Clean up upon an error in get_exportlist().
1844 */
1845 void
getexp_err(struct exportlist * ep,struct grouplist * grp)1846 getexp_err(struct exportlist *ep, struct grouplist *grp)
1847 {
1848 struct grouplist *tgrp;
1849
1850 if (!(opt_flags & OP_QUIET))
1851 syslog(LOG_ERR, "bad exports list line %s", line);
1852 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1853 free_exp(ep);
1854 while (grp) {
1855 tgrp = grp;
1856 grp = grp->gr_next;
1857 free_grp(tgrp);
1858 }
1859 }
1860
1861 /*
1862 * Search the export list for a matching fs.
1863 */
1864 struct exportlist *
ex_search(fsid_t * fsid)1865 ex_search(fsid_t *fsid)
1866 {
1867 struct exportlist *ep;
1868
1869 ep = exphead;
1870 while (ep) {
1871 if (ep->ex_fs.val[0] == fsid->val[0] &&
1872 ep->ex_fs.val[1] == fsid->val[1])
1873 return (ep);
1874 ep = ep->ex_next;
1875 }
1876 return (ep);
1877 }
1878
1879 /*
1880 * Add a directory path to the list.
1881 */
1882 char *
add_expdir(struct dirlist ** dpp,char * cp,int len)1883 add_expdir(struct dirlist **dpp, char *cp, int len)
1884 {
1885 struct dirlist *dp;
1886
1887 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1888 if (dp == (struct dirlist *)NULL)
1889 out_of_mem();
1890 dp->dp_left = *dpp;
1891 dp->dp_right = (struct dirlist *)NULL;
1892 dp->dp_flag = 0;
1893 dp->dp_hosts = (struct hostlist *)NULL;
1894 strcpy(dp->dp_dirp, cp);
1895 *dpp = dp;
1896 return (dp->dp_dirp);
1897 }
1898
1899 /*
1900 * Hang the dir list element off the dirpath binary tree as required
1901 * and update the entry for host.
1902 */
1903 void
hang_dirp(struct dirlist * dp,struct grouplist * grp,struct exportlist * ep,int flags)1904 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1905 int flags)
1906 {
1907 struct hostlist *hp;
1908 struct dirlist *dp2;
1909
1910 if (flags & OP_ALLDIRS) {
1911 if (ep->ex_defdir)
1912 free((caddr_t)dp);
1913 else
1914 ep->ex_defdir = dp;
1915 if (grp == (struct grouplist *)NULL) {
1916 ep->ex_defdir->dp_flag |= DP_DEFSET;
1917 /* Save the default security flavors list. */
1918 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1919 if (ep->ex_numsecflavors > 0)
1920 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1921 sizeof(ep->ex_secflavors));
1922 } else while (grp) {
1923 hp = get_ht();
1924 hp->ht_grp = grp;
1925 hp->ht_next = ep->ex_defdir->dp_hosts;
1926 ep->ex_defdir->dp_hosts = hp;
1927 /* Save the security flavors list for this host set. */
1928 grp->gr_numsecflavors = ep->ex_numsecflavors;
1929 if (ep->ex_numsecflavors > 0)
1930 memcpy(grp->gr_secflavors, ep->ex_secflavors,
1931 sizeof(ep->ex_secflavors));
1932 grp = grp->gr_next;
1933 }
1934 } else {
1935
1936 /*
1937 * Loop through the directories adding them to the tree.
1938 */
1939 while (dp) {
1940 dp2 = dp->dp_left;
1941 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
1942 dp = dp2;
1943 }
1944 }
1945 }
1946
1947 /*
1948 * Traverse the binary tree either updating a node that is already there
1949 * for the new directory or adding the new node.
1950 */
1951 void
add_dlist(struct dirlist ** dpp,struct dirlist * newdp,struct grouplist * grp,int flags,struct exportlist * ep)1952 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1953 int flags, struct exportlist *ep)
1954 {
1955 struct dirlist *dp;
1956 struct hostlist *hp;
1957 int cmp;
1958
1959 dp = *dpp;
1960 if (dp) {
1961 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1962 if (cmp > 0) {
1963 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
1964 return;
1965 } else if (cmp < 0) {
1966 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
1967 return;
1968 } else
1969 free((caddr_t)newdp);
1970 } else {
1971 dp = newdp;
1972 dp->dp_left = (struct dirlist *)NULL;
1973 *dpp = dp;
1974 }
1975 if (grp) {
1976
1977 /*
1978 * Hang all of the host(s) off of the directory point.
1979 */
1980 do {
1981 hp = get_ht();
1982 hp->ht_grp = grp;
1983 hp->ht_next = dp->dp_hosts;
1984 dp->dp_hosts = hp;
1985 /* Save the security flavors list for this host set. */
1986 grp->gr_numsecflavors = ep->ex_numsecflavors;
1987 if (ep->ex_numsecflavors > 0)
1988 memcpy(grp->gr_secflavors, ep->ex_secflavors,
1989 sizeof(ep->ex_secflavors));
1990 grp = grp->gr_next;
1991 } while (grp);
1992 } else {
1993 dp->dp_flag |= DP_DEFSET;
1994 /* Save the default security flavors list. */
1995 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1996 if (ep->ex_numsecflavors > 0)
1997 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1998 sizeof(ep->ex_secflavors));
1999 }
2000 }
2001
2002 /*
2003 * Search for a dirpath on the export point.
2004 */
2005 struct dirlist *
dirp_search(struct dirlist * dp,char * dirp)2006 dirp_search(struct dirlist *dp, char *dirp)
2007 {
2008 int cmp;
2009
2010 if (dp) {
2011 cmp = strcmp(dp->dp_dirp, dirp);
2012 if (cmp > 0)
2013 return (dirp_search(dp->dp_left, dirp));
2014 else if (cmp < 0)
2015 return (dirp_search(dp->dp_right, dirp));
2016 else
2017 return (dp);
2018 }
2019 return (dp);
2020 }
2021
2022 /*
2023 * Scan for a host match in a directory tree.
2024 */
2025 int
chk_host(struct dirlist * dp,struct sockaddr * saddr,int * defsetp,int * hostsetp,int * numsecflavors,int ** secflavorsp)2026 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2027 int *hostsetp, int *numsecflavors, int **secflavorsp)
2028 {
2029 struct hostlist *hp;
2030 struct grouplist *grp;
2031 struct addrinfo *ai;
2032
2033 if (dp) {
2034 if (dp->dp_flag & DP_DEFSET)
2035 *defsetp = dp->dp_flag;
2036 hp = dp->dp_hosts;
2037 while (hp) {
2038 grp = hp->ht_grp;
2039 switch (grp->gr_type) {
2040 case GT_HOST:
2041 ai = grp->gr_ptr.gt_addrinfo;
2042 for (; ai; ai = ai->ai_next) {
2043 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2044 *hostsetp =
2045 (hp->ht_flag | DP_HOSTSET);
2046 if (numsecflavors != NULL) {
2047 *numsecflavors =
2048 grp->gr_numsecflavors;
2049 *secflavorsp =
2050 grp->gr_secflavors;
2051 }
2052 return (1);
2053 }
2054 }
2055 break;
2056 case GT_NET:
2057 if (!sacmp(saddr, (struct sockaddr *)
2058 &grp->gr_ptr.gt_net.nt_net,
2059 (struct sockaddr *)
2060 &grp->gr_ptr.gt_net.nt_mask)) {
2061 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2062 if (numsecflavors != NULL) {
2063 *numsecflavors =
2064 grp->gr_numsecflavors;
2065 *secflavorsp =
2066 grp->gr_secflavors;
2067 }
2068 return (1);
2069 }
2070 break;
2071 }
2072 hp = hp->ht_next;
2073 }
2074 }
2075 return (0);
2076 }
2077
2078 /*
2079 * Scan tree for a host that matches the address.
2080 */
2081 int
scan_tree(struct dirlist * dp,struct sockaddr * saddr)2082 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2083 {
2084 int defset, hostset;
2085
2086 if (dp) {
2087 if (scan_tree(dp->dp_left, saddr))
2088 return (1);
2089 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2090 return (1);
2091 if (scan_tree(dp->dp_right, saddr))
2092 return (1);
2093 }
2094 return (0);
2095 }
2096
2097 /*
2098 * Traverse the dirlist tree and free it up.
2099 */
2100 void
free_dir(struct dirlist * dp)2101 free_dir(struct dirlist *dp)
2102 {
2103
2104 if (dp) {
2105 free_dir(dp->dp_left);
2106 free_dir(dp->dp_right);
2107 free_host(dp->dp_hosts);
2108 free((caddr_t)dp);
2109 }
2110 }
2111
2112 /*
2113 * Parse a colon separated list of security flavors
2114 */
2115 int
parsesec(char * seclist,struct exportlist * ep)2116 parsesec(char *seclist, struct exportlist *ep)
2117 {
2118 char *cp, savedc;
2119 int flavor;
2120
2121 ep->ex_numsecflavors = 0;
2122 for (;;) {
2123 cp = strchr(seclist, ':');
2124 if (cp) {
2125 savedc = *cp;
2126 *cp = '\0';
2127 }
2128
2129 if (!strcmp(seclist, "sys"))
2130 flavor = AUTH_SYS;
2131 else if (!strcmp(seclist, "krb5"))
2132 flavor = RPCSEC_GSS_KRB5;
2133 else if (!strcmp(seclist, "krb5i"))
2134 flavor = RPCSEC_GSS_KRB5I;
2135 else if (!strcmp(seclist, "krb5p"))
2136 flavor = RPCSEC_GSS_KRB5P;
2137 else {
2138 if (cp)
2139 *cp = savedc;
2140 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2141 return (1);
2142 }
2143 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2144 if (cp)
2145 *cp = savedc;
2146 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2147 return (1);
2148 }
2149 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2150 ep->ex_numsecflavors++;
2151 if (cp) {
2152 *cp = savedc;
2153 seclist = cp + 1;
2154 } else {
2155 break;
2156 }
2157 }
2158 return (0);
2159 }
2160
2161 /*
2162 * Parse the option string and update fields.
2163 * Option arguments may either be -<option>=<value> or
2164 * -<option> <value>
2165 */
2166 int
do_opt(char ** cpp,char ** endcpp,struct exportlist * ep,struct grouplist * grp,int * has_hostp,int * exflagsp,struct xucred * cr)2167 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2168 int *has_hostp, int *exflagsp, struct xucred *cr)
2169 {
2170 char *cpoptarg, *cpoptend;
2171 char *cp, *endcp, *cpopt, savedc, savedc2;
2172 int allflag, usedarg;
2173
2174 savedc2 = '\0';
2175 cpopt = *cpp;
2176 cpopt++;
2177 cp = *endcpp;
2178 savedc = *cp;
2179 *cp = '\0';
2180 while (cpopt && *cpopt) {
2181 allflag = 1;
2182 usedarg = -2;
2183 if ((cpoptend = strchr(cpopt, ','))) {
2184 *cpoptend++ = '\0';
2185 if ((cpoptarg = strchr(cpopt, '=')))
2186 *cpoptarg++ = '\0';
2187 } else {
2188 if ((cpoptarg = strchr(cpopt, '=')))
2189 *cpoptarg++ = '\0';
2190 else {
2191 *cp = savedc;
2192 nextfield(&cp, &endcp);
2193 **endcpp = '\0';
2194 if (endcp > cp && *cp != '-') {
2195 cpoptarg = cp;
2196 savedc2 = *endcp;
2197 *endcp = '\0';
2198 usedarg = 0;
2199 }
2200 }
2201 }
2202 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2203 *exflagsp |= MNT_EXRDONLY;
2204 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2205 !(allflag = strcmp(cpopt, "mapall")) ||
2206 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2207 usedarg++;
2208 parsecred(cpoptarg, cr);
2209 if (allflag == 0) {
2210 *exflagsp |= MNT_EXPORTANON;
2211 opt_flags |= OP_MAPALL;
2212 } else
2213 opt_flags |= OP_MAPROOT;
2214 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2215 !strcmp(cpopt, "m"))) {
2216 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2217 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2218 return (1);
2219 }
2220 usedarg++;
2221 opt_flags |= OP_MASK;
2222 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2223 !strcmp(cpopt, "n"))) {
2224 if (strchr(cpoptarg, '/') != NULL) {
2225 if (debug)
2226 fprintf(stderr, "setting OP_MASKLEN\n");
2227 opt_flags |= OP_MASKLEN;
2228 }
2229 if (grp->gr_type != GT_NULL) {
2230 syslog(LOG_ERR, "network/host conflict");
2231 return (1);
2232 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2233 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2234 return (1);
2235 }
2236 grp->gr_type = GT_NET;
2237 *has_hostp = 1;
2238 usedarg++;
2239 opt_flags |= OP_NET;
2240 } else if (!strcmp(cpopt, "alldirs")) {
2241 opt_flags |= OP_ALLDIRS;
2242 } else if (!strcmp(cpopt, "public")) {
2243 *exflagsp |= MNT_EXPUBLIC;
2244 } else if (!strcmp(cpopt, "webnfs")) {
2245 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2246 opt_flags |= OP_MAPALL;
2247 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2248 ep->ex_indexfile = strdup(cpoptarg);
2249 } else if (!strcmp(cpopt, "quiet")) {
2250 opt_flags |= OP_QUIET;
2251 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2252 if (parsesec(cpoptarg, ep))
2253 return (1);
2254 opt_flags |= OP_SEC;
2255 usedarg++;
2256 } else {
2257 syslog(LOG_ERR, "bad opt %s", cpopt);
2258 return (1);
2259 }
2260 if (usedarg >= 0) {
2261 *endcp = savedc2;
2262 **endcpp = savedc;
2263 if (usedarg > 0) {
2264 *cpp = cp;
2265 *endcpp = endcp;
2266 }
2267 return (0);
2268 }
2269 cpopt = cpoptend;
2270 }
2271 **endcpp = savedc;
2272 return (0);
2273 }
2274
2275 /*
2276 * Translate a character string to the corresponding list of network
2277 * addresses for a hostname.
2278 */
2279 int
get_host(char * cp,struct grouplist * grp,struct grouplist * tgrp)2280 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2281 {
2282 struct grouplist *checkgrp;
2283 struct addrinfo *ai, *tai, hints;
2284 int ecode;
2285 char host[NI_MAXHOST];
2286
2287 if (grp->gr_type != GT_NULL) {
2288 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2289 return (1);
2290 }
2291 memset(&hints, 0, sizeof hints);
2292 hints.ai_flags = AI_CANONNAME;
2293 hints.ai_protocol = IPPROTO_UDP;
2294 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2295 if (ecode != 0) {
2296 syslog(LOG_ERR,"can't get address info for host %s", cp);
2297 return 1;
2298 }
2299 grp->gr_ptr.gt_addrinfo = ai;
2300 while (ai != NULL) {
2301 if (ai->ai_canonname == NULL) {
2302 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2303 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2304 strlcpy(host, "?", sizeof(host));
2305 ai->ai_canonname = strdup(host);
2306 ai->ai_flags |= AI_CANONNAME;
2307 }
2308 if (debug)
2309 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2310 /*
2311 * Sanity check: make sure we don't already have an entry
2312 * for this host in the grouplist.
2313 */
2314 for (checkgrp = tgrp; checkgrp != NULL;
2315 checkgrp = checkgrp->gr_next) {
2316 if (checkgrp->gr_type != GT_HOST)
2317 continue;
2318 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2319 tai = tai->ai_next) {
2320 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2321 continue;
2322 if (debug)
2323 fprintf(stderr,
2324 "ignoring duplicate host %s\n",
2325 ai->ai_canonname);
2326 grp->gr_type = GT_IGNORE;
2327 return (0);
2328 }
2329 }
2330 ai = ai->ai_next;
2331 }
2332 grp->gr_type = GT_HOST;
2333 return (0);
2334 }
2335
2336 /*
2337 * Free up an exports list component
2338 */
2339 void
free_exp(struct exportlist * ep)2340 free_exp(struct exportlist *ep)
2341 {
2342
2343 if (ep->ex_defdir) {
2344 free_host(ep->ex_defdir->dp_hosts);
2345 free((caddr_t)ep->ex_defdir);
2346 }
2347 if (ep->ex_fsdir)
2348 free(ep->ex_fsdir);
2349 if (ep->ex_indexfile)
2350 free(ep->ex_indexfile);
2351 free_dir(ep->ex_dirl);
2352 free((caddr_t)ep);
2353 }
2354
2355 /*
2356 * Free hosts.
2357 */
2358 void
free_host(struct hostlist * hp)2359 free_host(struct hostlist *hp)
2360 {
2361 struct hostlist *hp2;
2362
2363 while (hp) {
2364 hp2 = hp;
2365 hp = hp->ht_next;
2366 free((caddr_t)hp2);
2367 }
2368 }
2369
2370 struct hostlist *
get_ht(void)2371 get_ht(void)
2372 {
2373 struct hostlist *hp;
2374
2375 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2376 if (hp == (struct hostlist *)NULL)
2377 out_of_mem();
2378 hp->ht_next = (struct hostlist *)NULL;
2379 hp->ht_flag = 0;
2380 return (hp);
2381 }
2382
2383 /*
2384 * Out of memory, fatal
2385 */
2386 void
out_of_mem(void)2387 out_of_mem(void)
2388 {
2389
2390 syslog(LOG_ERR, "out of memory");
2391 exit(2);
2392 }
2393
2394 /*
2395 * Do the nmount() syscall with the update flag to push the export info into
2396 * the kernel.
2397 */
2398 int
do_mount(struct exportlist * ep,struct grouplist * grp,int exflags,struct xucred * anoncrp,char * dirp,int dirplen,struct statfs * fsb)2399 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2400 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2401 {
2402 struct statfs fsb1;
2403 struct addrinfo *ai;
2404 struct export_args ea, *eap;
2405 char errmsg[255];
2406 char *cp;
2407 int done;
2408 char savedc;
2409 struct iovec *iov;
2410 int i, iovlen;
2411 int ret;
2412 struct nfsex_args nfsea;
2413
2414 if (run_v4server > 0)
2415 eap = &nfsea.export;
2416 else
2417 eap = &ea;
2418
2419 cp = NULL;
2420 savedc = '\0';
2421 iov = NULL;
2422 iovlen = 0;
2423 ret = 0;
2424
2425 bzero(eap, sizeof (struct export_args));
2426 bzero(errmsg, sizeof(errmsg));
2427 eap->ex_flags = exflags;
2428 eap->ex_anon = *anoncrp;
2429 eap->ex_indexfile = ep->ex_indexfile;
2430 if (grp->gr_type == GT_HOST)
2431 ai = grp->gr_ptr.gt_addrinfo;
2432 else
2433 ai = NULL;
2434 eap->ex_numsecflavors = ep->ex_numsecflavors;
2435 for (i = 0; i < eap->ex_numsecflavors; i++)
2436 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2437 if (eap->ex_numsecflavors == 0) {
2438 eap->ex_numsecflavors = 1;
2439 eap->ex_secflavors[0] = AUTH_SYS;
2440 }
2441 done = FALSE;
2442
2443 if (v4root_phase == 0) {
2444 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2445 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2446 build_iovec(&iov, &iovlen, "from", NULL, 0);
2447 build_iovec(&iov, &iovlen, "update", NULL, 0);
2448 build_iovec(&iov, &iovlen, "export", eap,
2449 sizeof (struct export_args));
2450 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2451 }
2452
2453 while (!done) {
2454 switch (grp->gr_type) {
2455 case GT_HOST:
2456 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2457 goto skip;
2458 eap->ex_addr = ai->ai_addr;
2459 eap->ex_addrlen = ai->ai_addrlen;
2460 eap->ex_masklen = 0;
2461 break;
2462 case GT_NET:
2463 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2464 have_v6 == 0)
2465 goto skip;
2466 eap->ex_addr =
2467 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2468 eap->ex_addrlen =
2469 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2470 eap->ex_mask =
2471 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2472 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2473 break;
2474 case GT_DEFAULT:
2475 eap->ex_addr = NULL;
2476 eap->ex_addrlen = 0;
2477 eap->ex_mask = NULL;
2478 eap->ex_masklen = 0;
2479 break;
2480 case GT_IGNORE:
2481 ret = 0;
2482 goto error_exit;
2483 break;
2484 default:
2485 syslog(LOG_ERR, "bad grouptype");
2486 if (cp)
2487 *cp = savedc;
2488 ret = 1;
2489 goto error_exit;
2490 };
2491
2492 /*
2493 * For V4:, use the nfssvc() syscall, instead of mount().
2494 */
2495 if (v4root_phase == 2) {
2496 nfsea.fspec = v4root_dirpath;
2497 if (run_v4server > 0 &&
2498 nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2499 syslog(LOG_ERR, "Exporting V4: failed");
2500 return (2);
2501 }
2502 } else {
2503 /*
2504 * XXX:
2505 * Maybe I should just use the fsb->f_mntonname path
2506 * instead of looping back up the dirp to the mount
2507 * point??
2508 * Also, needs to know how to export all types of local
2509 * exportable filesystems and not just "ufs".
2510 */
2511 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2512 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2513 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2514 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2515 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2516 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2517 errmsg[0] = '\0';
2518
2519 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2520 if (cp)
2521 *cp-- = savedc;
2522 else
2523 cp = dirp + dirplen - 1;
2524 if (opt_flags & OP_QUIET) {
2525 ret = 1;
2526 goto error_exit;
2527 }
2528 if (errno == EPERM) {
2529 if (debug)
2530 warnx("can't change attributes for %s: %s",
2531 dirp, errmsg);
2532 syslog(LOG_ERR,
2533 "can't change attributes for %s: %s",
2534 dirp, errmsg);
2535 ret = 1;
2536 goto error_exit;
2537 }
2538 if (opt_flags & OP_ALLDIRS) {
2539 if (errno == EINVAL)
2540 syslog(LOG_ERR,
2541 "-alldirs requested but %s is not a filesystem mountpoint",
2542 dirp);
2543 else
2544 syslog(LOG_ERR,
2545 "could not remount %s: %m",
2546 dirp);
2547 ret = 1;
2548 goto error_exit;
2549 }
2550 /* back up over the last component */
2551 while (*cp == '/' && cp > dirp)
2552 cp--;
2553 while (*(cp - 1) != '/' && cp > dirp)
2554 cp--;
2555 if (cp == dirp) {
2556 if (debug)
2557 warnx("mnt unsucc");
2558 syslog(LOG_ERR, "can't export %s %s",
2559 dirp, errmsg);
2560 ret = 1;
2561 goto error_exit;
2562 }
2563 savedc = *cp;
2564 *cp = '\0';
2565 /*
2566 * Check that we're still on the same
2567 * filesystem.
2568 */
2569 if (statfs(dirp, &fsb1) != 0 ||
2570 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2571 sizeof (fsb1.f_fsid)) != 0) {
2572 *cp = savedc;
2573 syslog(LOG_ERR,
2574 "can't export %s %s", dirp,
2575 errmsg);
2576 ret = 1;
2577 goto error_exit;
2578 }
2579 }
2580 }
2581
2582 /*
2583 * For the experimental server:
2584 * If this is the public directory, get the file handle
2585 * and load it into the kernel via the nfssvc() syscall.
2586 */
2587 if (run_v4server > 0 && (exflags & MNT_EXPUBLIC) != 0) {
2588 fhandle_t fh;
2589 char *public_name;
2590
2591 if (eap->ex_indexfile != NULL)
2592 public_name = eap->ex_indexfile;
2593 else
2594 public_name = dirp;
2595 if (getfh(public_name, &fh) < 0)
2596 syslog(LOG_ERR,
2597 "Can't get public fh for %s", public_name);
2598 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2599 syslog(LOG_ERR,
2600 "Can't set public fh for %s", public_name);
2601 else
2602 has_publicfh = 1;
2603 }
2604 skip:
2605 if (ai != NULL)
2606 ai = ai->ai_next;
2607 if (ai == NULL)
2608 done = TRUE;
2609 }
2610 if (cp)
2611 *cp = savedc;
2612 error_exit:
2613 /* free strings allocated by strdup() in getmntopts.c */
2614 if (iov != NULL) {
2615 free(iov[0].iov_base); /* fstype */
2616 free(iov[2].iov_base); /* fspath */
2617 free(iov[4].iov_base); /* from */
2618 free(iov[6].iov_base); /* update */
2619 free(iov[8].iov_base); /* export */
2620 free(iov[10].iov_base); /* errmsg */
2621
2622 /* free iov, allocated by realloc() */
2623 free(iov);
2624 }
2625 return (ret);
2626 }
2627
2628 /*
2629 * Translate a net address.
2630 *
2631 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2632 */
2633 int
get_net(char * cp,struct netmsk * net,int maskflg)2634 get_net(char *cp, struct netmsk *net, int maskflg)
2635 {
2636 struct netent *np = NULL;
2637 char *name, *p, *prefp;
2638 struct sockaddr_in sin;
2639 struct sockaddr *sa = NULL;
2640 struct addrinfo hints, *ai = NULL;
2641 char netname[NI_MAXHOST];
2642 long preflen;
2643
2644 p = prefp = NULL;
2645 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2646 p = strchr(cp, '/');
2647 *p = '\0';
2648 prefp = p + 1;
2649 }
2650
2651 /*
2652 * Check for a numeric address first. We wish to avoid
2653 * possible DNS lookups in getnetbyname().
2654 */
2655 if (isxdigit(*cp) || *cp == ':') {
2656 memset(&hints, 0, sizeof hints);
2657 /* Ensure the mask and the network have the same family. */
2658 if (maskflg && (opt_flags & OP_NET))
2659 hints.ai_family = net->nt_net.ss_family;
2660 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2661 hints.ai_family = net->nt_mask.ss_family;
2662 else
2663 hints.ai_family = AF_UNSPEC;
2664 hints.ai_flags = AI_NUMERICHOST;
2665 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2666 sa = ai->ai_addr;
2667 if (sa != NULL && ai->ai_family == AF_INET) {
2668 /*
2669 * The address in `cp' is really a network address, so
2670 * use inet_network() to re-interpret this correctly.
2671 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2672 */
2673 bzero(&sin, sizeof sin);
2674 sin.sin_family = AF_INET;
2675 sin.sin_len = sizeof sin;
2676 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2677 if (debug)
2678 fprintf(stderr, "get_net: v4 addr %s\n",
2679 inet_ntoa(sin.sin_addr));
2680 sa = (struct sockaddr *)&sin;
2681 }
2682 }
2683 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2684 bzero(&sin, sizeof sin);
2685 sin.sin_family = AF_INET;
2686 sin.sin_len = sizeof sin;
2687 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2688 sa = (struct sockaddr *)&sin;
2689 }
2690 if (sa == NULL)
2691 goto fail;
2692
2693 if (maskflg) {
2694 /* The specified sockaddr is a mask. */
2695 if (checkmask(sa) != 0)
2696 goto fail;
2697 bcopy(sa, &net->nt_mask, sa->sa_len);
2698 opt_flags |= OP_HAVEMASK;
2699 } else {
2700 /* The specified sockaddr is a network address. */
2701 bcopy(sa, &net->nt_net, sa->sa_len);
2702
2703 /* Get a network name for the export list. */
2704 if (np) {
2705 name = np->n_name;
2706 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2707 NULL, 0, NI_NUMERICHOST) == 0) {
2708 name = netname;
2709 } else {
2710 goto fail;
2711 }
2712 if ((net->nt_name = strdup(name)) == NULL)
2713 out_of_mem();
2714
2715 /*
2716 * Extract a mask from either a "/<masklen>" suffix, or
2717 * from the class of an IPv4 address.
2718 */
2719 if (opt_flags & OP_MASKLEN) {
2720 preflen = strtol(prefp, NULL, 10);
2721 if (preflen < 0L || preflen == LONG_MAX)
2722 goto fail;
2723 bcopy(sa, &net->nt_mask, sa->sa_len);
2724 if (makemask(&net->nt_mask, (int)preflen) != 0)
2725 goto fail;
2726 opt_flags |= OP_HAVEMASK;
2727 *p = '/';
2728 } else if (sa->sa_family == AF_INET &&
2729 (opt_flags & OP_MASK) == 0) {
2730 in_addr_t addr;
2731
2732 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2733 if (IN_CLASSA(addr))
2734 preflen = 8;
2735 else if (IN_CLASSB(addr))
2736 preflen = 16;
2737 else if (IN_CLASSC(addr))
2738 preflen = 24;
2739 else if (IN_CLASSD(addr))
2740 preflen = 28;
2741 else
2742 preflen = 32; /* XXX */
2743
2744 bcopy(sa, &net->nt_mask, sa->sa_len);
2745 makemask(&net->nt_mask, (int)preflen);
2746 opt_flags |= OP_HAVEMASK;
2747 }
2748 }
2749
2750 if (ai)
2751 freeaddrinfo(ai);
2752 return 0;
2753
2754 fail:
2755 if (ai)
2756 freeaddrinfo(ai);
2757 return 1;
2758 }
2759
2760 /*
2761 * Parse out the next white space separated field
2762 */
2763 void
nextfield(char ** cp,char ** endcp)2764 nextfield(char **cp, char **endcp)
2765 {
2766 char *p;
2767
2768 p = *cp;
2769 while (*p == ' ' || *p == '\t')
2770 p++;
2771 if (*p == '\n' || *p == '\0')
2772 *cp = *endcp = p;
2773 else {
2774 *cp = p++;
2775 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2776 p++;
2777 *endcp = p;
2778 }
2779 }
2780
2781 /*
2782 * Get an exports file line. Skip over blank lines and handle line
2783 * continuations.
2784 */
2785 int
get_line(void)2786 get_line(void)
2787 {
2788 char *p, *cp;
2789 size_t len;
2790 int totlen, cont_line;
2791
2792 /*
2793 * Loop around ignoring blank lines and getting all continuation lines.
2794 */
2795 p = line;
2796 totlen = 0;
2797 do {
2798 if ((p = fgetln(exp_file, &len)) == NULL)
2799 return (0);
2800 cp = p + len - 1;
2801 cont_line = 0;
2802 while (cp >= p &&
2803 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2804 if (*cp == '\\')
2805 cont_line = 1;
2806 cp--;
2807 len--;
2808 }
2809 if (cont_line) {
2810 *++cp = ' ';
2811 len++;
2812 }
2813 if (linesize < len + totlen + 1) {
2814 linesize = len + totlen + 1;
2815 line = realloc(line, linesize);
2816 if (line == NULL)
2817 out_of_mem();
2818 }
2819 memcpy(line + totlen, p, len);
2820 totlen += len;
2821 line[totlen] = '\0';
2822 } while (totlen == 0 || cont_line);
2823 return (1);
2824 }
2825
2826 /*
2827 * Parse a description of a credential.
2828 */
2829 void
parsecred(char * namelist,struct xucred * cr)2830 parsecred(char *namelist, struct xucred *cr)
2831 {
2832 char *name;
2833 int cnt;
2834 char *names;
2835 struct passwd *pw;
2836 struct group *gr;
2837 gid_t groups[XU_NGROUPS + 1];
2838 int ngroups;
2839
2840 cr->cr_version = XUCRED_VERSION;
2841 /*
2842 * Set up the unprivileged user.
2843 */
2844 cr->cr_uid = -2;
2845 cr->cr_groups[0] = -2;
2846 cr->cr_ngroups = 1;
2847 /*
2848 * Get the user's password table entry.
2849 */
2850 names = strsep(&namelist, " \t\n");
2851 name = strsep(&names, ":");
2852 if (isdigit(*name) || *name == '-')
2853 pw = getpwuid(atoi(name));
2854 else
2855 pw = getpwnam(name);
2856 /*
2857 * Credentials specified as those of a user.
2858 */
2859 if (names == NULL) {
2860 if (pw == NULL) {
2861 syslog(LOG_ERR, "unknown user: %s", name);
2862 return;
2863 }
2864 cr->cr_uid = pw->pw_uid;
2865 ngroups = XU_NGROUPS + 1;
2866 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2867 syslog(LOG_ERR, "too many groups");
2868 /*
2869 * Compress out duplicate.
2870 */
2871 cr->cr_ngroups = ngroups - 1;
2872 cr->cr_groups[0] = groups[0];
2873 for (cnt = 2; cnt < ngroups; cnt++)
2874 cr->cr_groups[cnt - 1] = groups[cnt];
2875 return;
2876 }
2877 /*
2878 * Explicit credential specified as a colon separated list:
2879 * uid:gid:gid:...
2880 */
2881 if (pw != NULL)
2882 cr->cr_uid = pw->pw_uid;
2883 else if (isdigit(*name) || *name == '-')
2884 cr->cr_uid = atoi(name);
2885 else {
2886 syslog(LOG_ERR, "unknown user: %s", name);
2887 return;
2888 }
2889 cr->cr_ngroups = 0;
2890 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
2891 name = strsep(&names, ":");
2892 if (isdigit(*name) || *name == '-') {
2893 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2894 } else {
2895 if ((gr = getgrnam(name)) == NULL) {
2896 syslog(LOG_ERR, "unknown group: %s", name);
2897 continue;
2898 }
2899 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2900 }
2901 }
2902 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
2903 syslog(LOG_ERR, "too many groups");
2904 }
2905
2906 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
2907 /*
2908 * Routines that maintain the remote mounttab
2909 */
2910 void
get_mountlist(void)2911 get_mountlist(void)
2912 {
2913 struct mountlist *mlp, **mlpp;
2914 char *host, *dirp, *cp;
2915 char str[STRSIZ];
2916 FILE *mlfile;
2917
2918 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2919 if (errno == ENOENT)
2920 return;
2921 else {
2922 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2923 return;
2924 }
2925 }
2926 mlpp = &mlhead;
2927 while (fgets(str, STRSIZ, mlfile) != NULL) {
2928 cp = str;
2929 host = strsep(&cp, " \t\n");
2930 dirp = strsep(&cp, " \t\n");
2931 if (host == NULL || dirp == NULL)
2932 continue;
2933 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2934 if (mlp == (struct mountlist *)NULL)
2935 out_of_mem();
2936 strncpy(mlp->ml_host, host, MNTNAMLEN);
2937 mlp->ml_host[MNTNAMLEN] = '\0';
2938 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2939 mlp->ml_dirp[MNTPATHLEN] = '\0';
2940 mlp->ml_next = (struct mountlist *)NULL;
2941 *mlpp = mlp;
2942 mlpp = &mlp->ml_next;
2943 }
2944 fclose(mlfile);
2945 }
2946
2947 void
del_mlist(char * hostp,char * dirp)2948 del_mlist(char *hostp, char *dirp)
2949 {
2950 struct mountlist *mlp, **mlpp;
2951 struct mountlist *mlp2;
2952 FILE *mlfile;
2953 int fnd = 0;
2954
2955 mlpp = &mlhead;
2956 mlp = mlhead;
2957 while (mlp) {
2958 if (!strcmp(mlp->ml_host, hostp) &&
2959 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2960 fnd = 1;
2961 mlp2 = mlp;
2962 *mlpp = mlp = mlp->ml_next;
2963 free((caddr_t)mlp2);
2964 } else {
2965 mlpp = &mlp->ml_next;
2966 mlp = mlp->ml_next;
2967 }
2968 }
2969 if (fnd) {
2970 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2971 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2972 return;
2973 }
2974 mlp = mlhead;
2975 while (mlp) {
2976 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2977 mlp = mlp->ml_next;
2978 }
2979 fclose(mlfile);
2980 }
2981 }
2982
2983 void
add_mlist(char * hostp,char * dirp)2984 add_mlist(char *hostp, char *dirp)
2985 {
2986 struct mountlist *mlp, **mlpp;
2987 FILE *mlfile;
2988
2989 mlpp = &mlhead;
2990 mlp = mlhead;
2991 while (mlp) {
2992 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2993 return;
2994 mlpp = &mlp->ml_next;
2995 mlp = mlp->ml_next;
2996 }
2997 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2998 if (mlp == (struct mountlist *)NULL)
2999 out_of_mem();
3000 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3001 mlp->ml_host[MNTNAMLEN] = '\0';
3002 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3003 mlp->ml_dirp[MNTPATHLEN] = '\0';
3004 mlp->ml_next = (struct mountlist *)NULL;
3005 *mlpp = mlp;
3006 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3007 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3008 return;
3009 }
3010 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3011 fclose(mlfile);
3012 }
3013
3014 /*
3015 * Free up a group list.
3016 */
3017 void
free_grp(struct grouplist * grp)3018 free_grp(struct grouplist *grp)
3019 {
3020 if (grp->gr_type == GT_HOST) {
3021 if (grp->gr_ptr.gt_addrinfo != NULL)
3022 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3023 } else if (grp->gr_type == GT_NET) {
3024 if (grp->gr_ptr.gt_net.nt_name)
3025 free(grp->gr_ptr.gt_net.nt_name);
3026 }
3027 free((caddr_t)grp);
3028 }
3029
3030 #ifdef DEBUG
3031 void
SYSLOG(int pri,const char * fmt,...)3032 SYSLOG(int pri, const char *fmt, ...)
3033 {
3034 va_list ap;
3035
3036 va_start(ap, fmt);
3037 vfprintf(stderr, fmt, ap);
3038 va_end(ap);
3039 }
3040 #endif /* DEBUG */
3041
3042 /*
3043 * Check options for consistency.
3044 */
3045 int
check_options(struct dirlist * dp)3046 check_options(struct dirlist *dp)
3047 {
3048
3049 if (v4root_phase == 0 && dp == NULL)
3050 return (1);
3051 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3052 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3053 return (1);
3054 }
3055 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3056 syslog(LOG_ERR, "-mask requires -network");
3057 return (1);
3058 }
3059 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3060 syslog(LOG_ERR, "-network requires mask specification");
3061 return (1);
3062 }
3063 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3064 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3065 return (1);
3066 }
3067 if (v4root_phase > 0 &&
3068 (opt_flags &
3069 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3070 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3071 return (1);
3072 }
3073 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3074 syslog(LOG_ERR, "-alldirs has multiple directories");
3075 return (1);
3076 }
3077 return (0);
3078 }
3079
3080 /*
3081 * Check an absolute directory path for any symbolic links. Return true
3082 */
3083 int
check_dirpath(char * dirp)3084 check_dirpath(char *dirp)
3085 {
3086 char *cp;
3087 int ret = 1;
3088 struct stat sb;
3089
3090 cp = dirp + 1;
3091 while (*cp && ret) {
3092 if (*cp == '/') {
3093 *cp = '\0';
3094 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3095 ret = 0;
3096 *cp = '/';
3097 }
3098 cp++;
3099 }
3100 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3101 ret = 0;
3102 return (ret);
3103 }
3104
3105 /*
3106 * Make a netmask according to the specified prefix length. The ss_family
3107 * and other non-address fields must be initialised before calling this.
3108 */
3109 int
makemask(struct sockaddr_storage * ssp,int bitlen)3110 makemask(struct sockaddr_storage *ssp, int bitlen)
3111 {
3112 u_char *p;
3113 int bits, i, len;
3114
3115 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3116 return (-1);
3117 if (bitlen > len * CHAR_BIT)
3118 return (-1);
3119
3120 for (i = 0; i < len; i++) {
3121 bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
3122 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3123 bitlen -= bits;
3124 }
3125 return 0;
3126 }
3127
3128 /*
3129 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3130 * is acceptable (i.e. of the form 1...10....0).
3131 */
3132 int
checkmask(struct sockaddr * sa)3133 checkmask(struct sockaddr *sa)
3134 {
3135 u_char *mask;
3136 int i, len;
3137
3138 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3139 return (-1);
3140
3141 for (i = 0; i < len; i++)
3142 if (mask[i] != 0xff)
3143 break;
3144 if (i < len) {
3145 if (~mask[i] & (u_char)(~mask[i] + 1))
3146 return (-1);
3147 i++;
3148 }
3149 for (; i < len; i++)
3150 if (mask[i] != 0)
3151 return (-1);
3152 return (0);
3153 }
3154
3155 /*
3156 * Compare two sockaddrs according to a specified mask. Return zero if
3157 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3158 * If samask is NULL, perform a full comparision.
3159 */
3160 int
sacmp(struct sockaddr * sa1,struct sockaddr * sa2,struct sockaddr * samask)3161 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3162 {
3163 unsigned char *p1, *p2, *mask;
3164 int len, i;
3165
3166 if (sa1->sa_family != sa2->sa_family ||
3167 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3168 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3169 return (1);
3170
3171 switch (sa1->sa_family) {
3172 case AF_INET6:
3173 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3174 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3175 return (1);
3176 break;
3177 }
3178
3179 /* Simple binary comparison if no mask specified. */
3180 if (samask == NULL)
3181 return (memcmp(p1, p2, len));
3182
3183 /* Set up the mask, and do a mask-based comparison. */
3184 if (sa1->sa_family != samask->sa_family ||
3185 (mask = sa_rawaddr(samask, NULL)) == NULL)
3186 return (1);
3187
3188 for (i = 0; i < len; i++)
3189 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3190 return (1);
3191 return (0);
3192 }
3193
3194 /*
3195 * Return a pointer to the part of the sockaddr that contains the
3196 * raw address, and set *nbytes to its length in bytes. Returns
3197 * NULL if the address family is unknown.
3198 */
3199 void *
sa_rawaddr(struct sockaddr * sa,int * nbytes)3200 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3201 void *p;
3202 int len;
3203
3204 switch (sa->sa_family) {
3205 case AF_INET:
3206 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3207 p = &((struct sockaddr_in *)sa)->sin_addr;
3208 break;
3209 case AF_INET6:
3210 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3211 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3212 break;
3213 default:
3214 p = NULL;
3215 len = 0;
3216 }
3217
3218 if (nbytes != NULL)
3219 *nbytes = len;
3220 return (p);
3221 }
3222
3223 void
huphandler(int sig __unused)3224 huphandler(int sig __unused)
3225 {
3226 got_sighup = 1;
3227 }
3228
terminate(int sig __unused)3229 void terminate(int sig __unused)
3230 {
3231 pidfile_remove(pfh);
3232 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3233 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
3234 exit (0);
3235 }
3236
3237