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