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 if (nconf->nc_semantics != NC_TPI_CLTS) 912 listen(fd, SOMAXCONN); 913 914 if (nconf->nc_semantics == NC_TPI_CLTS ) 915 transp = svc_dg_create(fd, 0, 0); 916 else 917 transp = svc_vc_create(fd, RPC_MAXDATASIZE, 918 RPC_MAXDATASIZE); 919 920 if (transp != (SVCXPRT *) NULL) { 921 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv, 922 NULL)) 923 syslog(LOG_ERR, 924 "can't register %s MOUNTVERS service", 925 nconf->nc_netid); 926 if (!force_v2) { 927 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3, 928 mntsrv, NULL)) 929 syslog(LOG_ERR, 930 "can't register %s MOUNTVERS3 service", 931 nconf->nc_netid); 932 } 933 } else 934 syslog(LOG_WARNING, "can't create %s services", 935 nconf->nc_netid); 936 937 if (registered == 0) { 938 registered = 1; 939 memset(&hints, 0, sizeof hints); 940 hints.ai_flags = AI_PASSIVE; 941 hints.ai_family = si.si_af; 942 hints.ai_socktype = si.si_socktype; 943 hints.ai_protocol = si.si_proto; 944 945 if ((aicode = getaddrinfo(NULL, port_str, &hints, 946 &res)) != 0) { 947 syslog(LOG_ERR, "cannot get local address: %s", 948 gai_strerror(aicode)); 949 exit(1); 950 } 951 952 servaddr.buf = malloc(res->ai_addrlen); 953 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen); 954 servaddr.len = res->ai_addrlen; 955 956 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr); 957 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr); 958 959 xcreated++; 960 freeaddrinfo(res); 961 } 962 } /* end while */ 963 } 964 965 /* 966 * Clear out sockets after a failure to bind one of them, so that the 967 * cycle of socket creation/binding can start anew. 968 */ 969 static void 970 clearout_service(void) 971 { 972 int i; 973 974 for (i = 0; i < sock_fdcnt; i++) { 975 if (sock_fd[i] >= 0) { 976 shutdown(sock_fd[i], SHUT_RDWR); 977 close(sock_fd[i]); 978 } 979 } 980 } 981 982 static void 983 usage(void) 984 { 985 fprintf(stderr, 986 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] " 987 "[-S] [-h <bindip>] [export_file ...]\n"); 988 exit(1); 989 } 990 991 /* 992 * The mount rpc service 993 */ 994 void 995 mntsrv(struct svc_req *rqstp, SVCXPRT *transp) 996 { 997 struct exportlist *ep; 998 struct dirlist *dp; 999 struct fhreturn fhr; 1000 struct stat stb; 1001 struct statfs fsb; 1002 char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 1003 int lookup_failed = 1; 1004 struct sockaddr *saddr; 1005 u_short sport; 1006 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN]; 1007 int bad = 0, defset, hostset; 1008 sigset_t sighup_mask; 1009 int numsecflavors, *secflavorsp; 1010 1011 sigemptyset(&sighup_mask); 1012 sigaddset(&sighup_mask, SIGHUP); 1013 saddr = svc_getrpccaller(transp)->buf; 1014 switch (saddr->sa_family) { 1015 case AF_INET6: 1016 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 1017 break; 1018 case AF_INET: 1019 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 1020 break; 1021 default: 1022 syslog(LOG_ERR, "request from unknown address family"); 1023 return; 1024 } 1025 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 1026 NULL, 0, 0); 1027 getnameinfo(saddr, saddr->sa_len, numerichost, 1028 sizeof numerichost, NULL, 0, NI_NUMERICHOST); 1029 switch (rqstp->rq_proc) { 1030 case NULLPROC: 1031 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) 1032 syslog(LOG_ERR, "can't send reply"); 1033 return; 1034 case MOUNTPROC_MNT: 1035 if (sport >= IPPORT_RESERVED && resvport_only) { 1036 syslog(LOG_NOTICE, 1037 "mount request from %s from unprivileged port", 1038 numerichost); 1039 svcerr_weakauth(transp); 1040 return; 1041 } 1042 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 1043 syslog(LOG_NOTICE, "undecodable mount request from %s", 1044 numerichost); 1045 svcerr_decode(transp); 1046 return; 1047 } 1048 1049 /* 1050 * Get the real pathname and make sure it is a directory 1051 * or a regular file if the -r option was specified 1052 * and it exists. 1053 */ 1054 if (realpath(rpcpath, dirpath) == NULL || 1055 stat(dirpath, &stb) < 0 || 1056 statfs(dirpath, &fsb) < 0) { 1057 chdir("/"); /* Just in case realpath doesn't */ 1058 syslog(LOG_NOTICE, 1059 "mount request from %s for non existent path %s", 1060 numerichost, dirpath); 1061 if (debug) 1062 warnx("stat failed on %s", dirpath); 1063 bad = ENOENT; /* We will send error reply later */ 1064 } 1065 if (!bad && 1066 !S_ISDIR(stb.st_mode) && 1067 (dir_only || !S_ISREG(stb.st_mode))) { 1068 syslog(LOG_NOTICE, 1069 "mount request from %s for non-directory path %s", 1070 numerichost, dirpath); 1071 if (debug) 1072 warnx("mounting non-directory %s", dirpath); 1073 bad = ENOTDIR; /* We will send error reply later */ 1074 } 1075 1076 /* Check in the exports list */ 1077 sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 1078 if (bad) 1079 ep = NULL; 1080 else 1081 ep = ex_search(&fsb.f_fsid); 1082 hostset = defset = 0; 1083 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset, 1084 &numsecflavors, &secflavorsp) || 1085 ((dp = dirp_search(ep->ex_dirl, dirpath)) && 1086 chk_host(dp, saddr, &defset, &hostset, &numsecflavors, 1087 &secflavorsp)) || 1088 (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 1089 scan_tree(ep->ex_dirl, saddr) == 0))) { 1090 if (bad) { 1091 if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 1092 (caddr_t)&bad)) 1093 syslog(LOG_ERR, "can't send reply"); 1094 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 1095 return; 1096 } 1097 if (hostset & DP_HOSTSET) { 1098 fhr.fhr_flag = hostset; 1099 fhr.fhr_numsecflavors = numsecflavors; 1100 fhr.fhr_secflavors = secflavorsp; 1101 } else { 1102 fhr.fhr_flag = defset; 1103 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors; 1104 fhr.fhr_secflavors = ep->ex_defsecflavors; 1105 } 1106 fhr.fhr_vers = rqstp->rq_vers; 1107 /* Get the file handle */ 1108 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 1109 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 1110 bad = errno; 1111 syslog(LOG_ERR, "can't get fh for %s", dirpath); 1112 if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 1113 (caddr_t)&bad)) 1114 syslog(LOG_ERR, "can't send reply"); 1115 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 1116 return; 1117 } 1118 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, 1119 (caddr_t)&fhr)) 1120 syslog(LOG_ERR, "can't send reply"); 1121 if (!lookup_failed) 1122 add_mlist(host, dirpath); 1123 else 1124 add_mlist(numerichost, dirpath); 1125 if (debug) 1126 warnx("mount successful"); 1127 if (dolog) 1128 syslog(LOG_NOTICE, 1129 "mount request succeeded from %s for %s", 1130 numerichost, dirpath); 1131 } else { 1132 if (!bad) 1133 bad = EACCES; 1134 syslog(LOG_NOTICE, 1135 "mount request denied from %s for %s", 1136 numerichost, dirpath); 1137 } 1138 1139 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, 1140 (caddr_t)&bad)) 1141 syslog(LOG_ERR, "can't send reply"); 1142 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 1143 return; 1144 case MOUNTPROC_DUMP: 1145 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL)) 1146 syslog(LOG_ERR, "can't send reply"); 1147 else if (dolog) 1148 syslog(LOG_NOTICE, 1149 "dump request succeeded from %s", 1150 numerichost); 1151 return; 1152 case MOUNTPROC_UMNT: 1153 if (sport >= IPPORT_RESERVED && resvport_only) { 1154 syslog(LOG_NOTICE, 1155 "umount request from %s from unprivileged port", 1156 numerichost); 1157 svcerr_weakauth(transp); 1158 return; 1159 } 1160 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 1161 syslog(LOG_NOTICE, "undecodable umount request from %s", 1162 numerichost); 1163 svcerr_decode(transp); 1164 return; 1165 } 1166 if (realpath(rpcpath, dirpath) == NULL) { 1167 syslog(LOG_NOTICE, "umount request from %s " 1168 "for non existent path %s", 1169 numerichost, dirpath); 1170 } 1171 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 1172 syslog(LOG_ERR, "can't send reply"); 1173 if (!lookup_failed) 1174 del_mlist(host, dirpath); 1175 del_mlist(numerichost, dirpath); 1176 if (dolog) 1177 syslog(LOG_NOTICE, 1178 "umount request succeeded from %s for %s", 1179 numerichost, dirpath); 1180 return; 1181 case MOUNTPROC_UMNTALL: 1182 if (sport >= IPPORT_RESERVED && resvport_only) { 1183 syslog(LOG_NOTICE, 1184 "umountall request from %s from unprivileged port", 1185 numerichost); 1186 svcerr_weakauth(transp); 1187 return; 1188 } 1189 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 1190 syslog(LOG_ERR, "can't send reply"); 1191 if (!lookup_failed) 1192 del_mlist(host, NULL); 1193 del_mlist(numerichost, NULL); 1194 if (dolog) 1195 syslog(LOG_NOTICE, 1196 "umountall request succeeded from %s", 1197 numerichost); 1198 return; 1199 case MOUNTPROC_EXPORT: 1200 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL)) 1201 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, 1202 (caddr_t)NULL)) 1203 syslog(LOG_ERR, "can't send reply"); 1204 if (dolog) 1205 syslog(LOG_NOTICE, 1206 "export request succeeded from %s", 1207 numerichost); 1208 return; 1209 default: 1210 svcerr_noproc(transp); 1211 return; 1212 } 1213 } 1214 1215 /* 1216 * Xdr conversion for a dirpath string 1217 */ 1218 static int 1219 xdr_dir(XDR *xdrsp, char *dirp) 1220 { 1221 return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); 1222 } 1223 1224 /* 1225 * Xdr routine to generate file handle reply 1226 */ 1227 static int 1228 xdr_fhs(XDR *xdrsp, caddr_t cp) 1229 { 1230 struct fhreturn *fhrp = (struct fhreturn *)cp; 1231 u_long ok = 0, len, auth; 1232 int i; 1233 1234 if (!xdr_long(xdrsp, &ok)) 1235 return (0); 1236 switch (fhrp->fhr_vers) { 1237 case 1: 1238 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 1239 case 3: 1240 len = NFSX_V3FH; 1241 if (!xdr_long(xdrsp, &len)) 1242 return (0); 1243 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 1244 return (0); 1245 if (fhrp->fhr_numsecflavors) { 1246 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors)) 1247 return (0); 1248 for (i = 0; i < fhrp->fhr_numsecflavors; i++) 1249 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i])) 1250 return (0); 1251 return (1); 1252 } else { 1253 auth = AUTH_SYS; 1254 len = 1; 1255 if (!xdr_long(xdrsp, &len)) 1256 return (0); 1257 return (xdr_long(xdrsp, &auth)); 1258 } 1259 } 1260 return (0); 1261 } 1262 1263 static int 1264 xdr_mlist(XDR *xdrsp, caddr_t cp __unused) 1265 { 1266 struct mountlist *mlp; 1267 int true = 1; 1268 int false = 0; 1269 char *strp; 1270 1271 SLIST_FOREACH(mlp, &mlhead, next) { 1272 if (!xdr_bool(xdrsp, &true)) 1273 return (0); 1274 strp = &mlp->ml_host[0]; 1275 if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) 1276 return (0); 1277 strp = &mlp->ml_dirp[0]; 1278 if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 1279 return (0); 1280 } 1281 if (!xdr_bool(xdrsp, &false)) 1282 return (0); 1283 return (1); 1284 } 1285 1286 /* 1287 * Xdr conversion for export list 1288 */ 1289 static int 1290 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief) 1291 { 1292 struct exportlist *ep; 1293 int false = 0; 1294 int putdef; 1295 sigset_t sighup_mask; 1296 1297 sigemptyset(&sighup_mask); 1298 sigaddset(&sighup_mask, SIGHUP); 1299 sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 1300 1301 SLIST_FOREACH(ep, &exphead, entries) { 1302 putdef = 0; 1303 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, 1304 &putdef, brief)) 1305 goto errout; 1306 if (ep->ex_defdir && putdef == 0 && 1307 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 1308 &putdef, brief)) 1309 goto errout; 1310 } 1311 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 1312 if (!xdr_bool(xdrsp, &false)) 1313 return (0); 1314 return (1); 1315 errout: 1316 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 1317 return (0); 1318 } 1319 1320 /* 1321 * Called from xdr_explist() to traverse the tree and export the 1322 * directory paths. 1323 */ 1324 static int 1325 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp, 1326 int brief) 1327 { 1328 struct grouplist *grp; 1329 struct hostlist *hp; 1330 int true = 1; 1331 int false = 0; 1332 int gotalldir = 0; 1333 char *strp; 1334 1335 if (dp) { 1336 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief)) 1337 return (1); 1338 if (!xdr_bool(xdrsp, &true)) 1339 return (1); 1340 strp = dp->dp_dirp; 1341 if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 1342 return (1); 1343 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 1344 gotalldir = 1; 1345 *putdefp = 1; 1346 } 1347 if (brief) { 1348 if (!xdr_bool(xdrsp, &true)) 1349 return (1); 1350 strp = "(...)"; 1351 if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 1352 return (1); 1353 } else if ((dp->dp_flag & DP_DEFSET) == 0 && 1354 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 1355 hp = dp->dp_hosts; 1356 while (hp) { 1357 grp = hp->ht_grp; 1358 if (grp->gr_type == GT_HOST) { 1359 if (!xdr_bool(xdrsp, &true)) 1360 return (1); 1361 strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 1362 if (!xdr_string(xdrsp, &strp, 1363 MNTNAMLEN)) 1364 return (1); 1365 } else if (grp->gr_type == GT_NET) { 1366 if (!xdr_bool(xdrsp, &true)) 1367 return (1); 1368 strp = grp->gr_ptr.gt_net.nt_name; 1369 if (!xdr_string(xdrsp, &strp, 1370 MNTNAMLEN)) 1371 return (1); 1372 } 1373 hp = hp->ht_next; 1374 if (gotalldir && hp == (struct hostlist *)NULL) { 1375 hp = adp->dp_hosts; 1376 gotalldir = 0; 1377 } 1378 } 1379 } 1380 if (!xdr_bool(xdrsp, &false)) 1381 return (1); 1382 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief)) 1383 return (1); 1384 } 1385 return (0); 1386 } 1387 1388 static int 1389 xdr_explist(XDR *xdrsp, caddr_t cp) 1390 { 1391 1392 return xdr_explist_common(xdrsp, cp, 0); 1393 } 1394 1395 static int 1396 xdr_explist_brief(XDR *xdrsp, caddr_t cp) 1397 { 1398 1399 return xdr_explist_common(xdrsp, cp, 1); 1400 } 1401 1402 static char *line; 1403 static size_t linesize; 1404 static FILE *exp_file; 1405 1406 /* 1407 * Get the export list from one, currently open file 1408 */ 1409 static void 1410 get_exportlist_one(void) 1411 { 1412 struct exportlist *ep; 1413 struct grouplist *grp, *tgrp; 1414 struct dirlist *dirhead; 1415 struct statfs fsb; 1416 struct xucred anon; 1417 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 1418 int len, has_host, exflags, got_nondir, dirplen, netgrp; 1419 1420 v4root_phase = 0; 1421 dirhead = (struct dirlist *)NULL; 1422 while (get_line()) { 1423 if (debug) 1424 warnx("got line %s", line); 1425 cp = line; 1426 nextfield(&cp, &endcp); 1427 if (*cp == '#') 1428 goto nextline; 1429 1430 /* 1431 * Set defaults. 1432 */ 1433 has_host = FALSE; 1434 anon = def_anon; 1435 exflags = MNT_EXPORTED; 1436 got_nondir = 0; 1437 opt_flags = 0; 1438 ep = (struct exportlist *)NULL; 1439 dirp = NULL; 1440 1441 /* 1442 * Handle the V4 root dir. 1443 */ 1444 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') { 1445 /* 1446 * V4: just indicates that it is the v4 root point, 1447 * so skip over that and set v4root_phase. 1448 */ 1449 if (v4root_phase > 0) { 1450 syslog(LOG_ERR, "V4:duplicate line, ignored"); 1451 goto nextline; 1452 } 1453 v4root_phase = 1; 1454 cp += 3; 1455 nextfield(&cp, &endcp); 1456 } 1457 1458 /* 1459 * Create new exports list entry 1460 */ 1461 len = endcp-cp; 1462 tgrp = grp = get_grp(); 1463 while (len > 0) { 1464 if (len > MNTNAMLEN) { 1465 getexp_err(ep, tgrp, "mountpoint too long"); 1466 goto nextline; 1467 } 1468 if (*cp == '-') { 1469 if (ep == (struct exportlist *)NULL) { 1470 getexp_err(ep, tgrp, 1471 "flag before export path definition"); 1472 goto nextline; 1473 } 1474 if (debug) 1475 warnx("doing opt %s", cp); 1476 got_nondir = 1; 1477 if (do_opt(&cp, &endcp, ep, grp, &has_host, 1478 &exflags, &anon)) { 1479 getexp_err(ep, tgrp, NULL); 1480 goto nextline; 1481 } 1482 } else if (*cp == '/') { 1483 savedc = *endcp; 1484 *endcp = '\0'; 1485 if (v4root_phase > 1) { 1486 if (dirp != NULL) { 1487 getexp_err(ep, tgrp, "Multiple V4 dirs"); 1488 goto nextline; 1489 } 1490 } 1491 if (check_dirpath(cp) && 1492 statfs(cp, &fsb) >= 0) { 1493 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0) 1494 syslog(LOG_ERR, "Warning: exporting of " 1495 "automounted fs %s not supported", cp); 1496 if (got_nondir) { 1497 getexp_err(ep, tgrp, "dirs must be first"); 1498 goto nextline; 1499 } 1500 if (v4root_phase == 1) { 1501 if (dirp != NULL) { 1502 getexp_err(ep, tgrp, "Multiple V4 dirs"); 1503 goto nextline; 1504 } 1505 if (strlen(v4root_dirpath) == 0) { 1506 strlcpy(v4root_dirpath, cp, 1507 sizeof (v4root_dirpath)); 1508 } else if (strcmp(v4root_dirpath, cp) 1509 != 0) { 1510 syslog(LOG_ERR, 1511 "different V4 dirpath %s", cp); 1512 getexp_err(ep, tgrp, NULL); 1513 goto nextline; 1514 } 1515 dirp = cp; 1516 v4root_phase = 2; 1517 got_nondir = 1; 1518 ep = get_exp(); 1519 } else { 1520 if (ep) { 1521 if (ep->ex_fs.val[0] != 1522 fsb.f_fsid.val[0] || 1523 ep->ex_fs.val[1] != 1524 fsb.f_fsid.val[1]) { 1525 getexp_err(ep, tgrp, 1526 "fsid mismatch"); 1527 goto nextline; 1528 } 1529 } else { 1530 /* 1531 * See if this directory is already 1532 * in the list. 1533 */ 1534 ep = ex_search(&fsb.f_fsid); 1535 if (ep == (struct exportlist *)NULL) { 1536 ep = get_exp(); 1537 ep->ex_fs = fsb.f_fsid; 1538 ep->ex_fsdir = strdup(fsb.f_mntonname); 1539 if (ep->ex_fsdir == NULL) 1540 out_of_mem(); 1541 if (debug) 1542 warnx( 1543 "making new ep fs=0x%x,0x%x", 1544 fsb.f_fsid.val[0], 1545 fsb.f_fsid.val[1]); 1546 } else if (debug) 1547 warnx("found ep fs=0x%x,0x%x", 1548 fsb.f_fsid.val[0], 1549 fsb.f_fsid.val[1]); 1550 } 1551 1552 /* 1553 * Add dirpath to export mount point. 1554 */ 1555 dirp = add_expdir(&dirhead, cp, len); 1556 dirplen = len; 1557 } 1558 } else { 1559 getexp_err(ep, tgrp, 1560 "symbolic link in export path or statfs failed"); 1561 goto nextline; 1562 } 1563 *endcp = savedc; 1564 } else { 1565 savedc = *endcp; 1566 *endcp = '\0'; 1567 got_nondir = 1; 1568 if (ep == (struct exportlist *)NULL) { 1569 getexp_err(ep, tgrp, 1570 "host(s) before export path definition"); 1571 goto nextline; 1572 } 1573 1574 /* 1575 * Get the host or netgroup. 1576 */ 1577 setnetgrent(cp); 1578 netgrp = getnetgrent(&hst, &usr, &dom); 1579 do { 1580 if (has_host) { 1581 grp->gr_next = get_grp(); 1582 grp = grp->gr_next; 1583 } 1584 if (netgrp) { 1585 if (hst == 0) { 1586 syslog(LOG_ERR, 1587 "null hostname in netgroup %s, skipping", cp); 1588 grp->gr_type = GT_IGNORE; 1589 } else if (get_host(hst, grp, tgrp)) { 1590 syslog(LOG_ERR, 1591 "bad host %s in netgroup %s, skipping", hst, cp); 1592 grp->gr_type = GT_IGNORE; 1593 } 1594 } else if (get_host(cp, grp, tgrp)) { 1595 syslog(LOG_ERR, "bad host %s, skipping", cp); 1596 grp->gr_type = GT_IGNORE; 1597 } 1598 has_host = TRUE; 1599 } while (netgrp && getnetgrent(&hst, &usr, &dom)); 1600 endnetgrent(); 1601 *endcp = savedc; 1602 } 1603 cp = endcp; 1604 nextfield(&cp, &endcp); 1605 len = endcp - cp; 1606 } 1607 if (check_options(dirhead)) { 1608 getexp_err(ep, tgrp, NULL); 1609 goto nextline; 1610 } 1611 if (!has_host) { 1612 grp->gr_type = GT_DEFAULT; 1613 if (debug) 1614 warnx("adding a default entry"); 1615 1616 /* 1617 * Don't allow a network export coincide with a list of 1618 * host(s) on the same line. 1619 */ 1620 } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 1621 getexp_err(ep, tgrp, "network/host conflict"); 1622 goto nextline; 1623 1624 /* 1625 * If an export list was specified on this line, make sure 1626 * that we have at least one valid entry, otherwise skip it. 1627 */ 1628 } else { 1629 grp = tgrp; 1630 while (grp && grp->gr_type == GT_IGNORE) 1631 grp = grp->gr_next; 1632 if (! grp) { 1633 getexp_err(ep, tgrp, "no valid entries"); 1634 goto nextline; 1635 } 1636 } 1637 1638 if (v4root_phase == 1) { 1639 getexp_err(ep, tgrp, "V4:root, no dirp, ignored"); 1640 goto nextline; 1641 } 1642 1643 /* 1644 * Loop through hosts, pushing the exports into the kernel. 1645 * After loop, tgrp points to the start of the list and 1646 * grp points to the last entry in the list. 1647 */ 1648 grp = tgrp; 1649 do { 1650 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 1651 &fsb)) { 1652 getexp_err(ep, tgrp, NULL); 1653 goto nextline; 1654 } 1655 } while (grp->gr_next && (grp = grp->gr_next)); 1656 1657 /* 1658 * For V4: don't enter in mount lists. 1659 */ 1660 if (v4root_phase > 0 && v4root_phase <= 2) { 1661 /* 1662 * Since these structures aren't used by mountd, 1663 * free them up now. 1664 */ 1665 if (ep != NULL) 1666 free_exp(ep); 1667 while (tgrp != NULL) { 1668 grp = tgrp; 1669 tgrp = tgrp->gr_next; 1670 free_grp(grp); 1671 } 1672 goto nextline; 1673 } 1674 1675 /* 1676 * Success. Update the data structures. 1677 */ 1678 if (has_host) { 1679 hang_dirp(dirhead, tgrp, ep, opt_flags); 1680 grp->gr_next = grphead; 1681 grphead = tgrp; 1682 } else { 1683 hang_dirp(dirhead, (struct grouplist *)NULL, ep, 1684 opt_flags); 1685 free_grp(grp); 1686 } 1687 dirhead = (struct dirlist *)NULL; 1688 if ((ep->ex_flag & EX_LINKED) == 0) { 1689 SLIST_INSERT_HEAD(&exphead, ep, entries); 1690 1691 ep->ex_flag |= EX_LINKED; 1692 } 1693 nextline: 1694 v4root_phase = 0; 1695 if (dirhead) { 1696 free_dir(dirhead); 1697 dirhead = (struct dirlist *)NULL; 1698 } 1699 } 1700 } 1701 1702 /* 1703 * Get the export list from all specified files 1704 */ 1705 static void 1706 get_exportlist(void) 1707 { 1708 struct exportlist *ep, *ep2; 1709 struct grouplist *grp, *tgrp; 1710 struct export_args export; 1711 struct iovec *iov; 1712 struct statfs *fsp, *mntbufp; 1713 struct xvfsconf vfc; 1714 char errmsg[255]; 1715 int num, i; 1716 int iovlen; 1717 int done; 1718 struct nfsex_args eargs; 1719 1720 if (suspend_nfsd != 0) 1721 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL); 1722 v4root_dirpath[0] = '\0'; 1723 bzero(&export, sizeof(export)); 1724 export.ex_flags = MNT_DELEXPORT; 1725 iov = NULL; 1726 iovlen = 0; 1727 bzero(errmsg, sizeof(errmsg)); 1728 1729 /* 1730 * First, get rid of the old list 1731 */ 1732 SLIST_FOREACH_SAFE(ep, &exphead, entries, ep2) { 1733 SLIST_REMOVE(&exphead, ep, exportlist, entries); 1734 free_exp(ep); 1735 } 1736 1737 grp = grphead; 1738 while (grp) { 1739 tgrp = grp; 1740 grp = grp->gr_next; 1741 free_grp(tgrp); 1742 } 1743 grphead = (struct grouplist *)NULL; 1744 1745 /* 1746 * and the old V4 root dir. 1747 */ 1748 bzero(&eargs, sizeof (eargs)); 1749 eargs.export.ex_flags = MNT_DELEXPORT; 1750 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 && 1751 errno != ENOENT) 1752 syslog(LOG_ERR, "Can't delete exports for V4:"); 1753 1754 /* 1755 * and clear flag that notes if a public fh has been exported. 1756 */ 1757 has_publicfh = 0; 1758 1759 /* 1760 * And delete exports that are in the kernel for all local 1761 * filesystems. 1762 * XXX: Should know how to handle all local exportable filesystems. 1763 */ 1764 num = getmntinfo(&mntbufp, MNT_NOWAIT); 1765 1766 if (num > 0) { 1767 build_iovec(&iov, &iovlen, "fstype", NULL, 0); 1768 build_iovec(&iov, &iovlen, "fspath", NULL, 0); 1769 build_iovec(&iov, &iovlen, "from", NULL, 0); 1770 build_iovec(&iov, &iovlen, "update", NULL, 0); 1771 build_iovec(&iov, &iovlen, "export", &export, sizeof(export)); 1772 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 1773 } 1774 1775 for (i = 0; i < num; i++) { 1776 fsp = &mntbufp[i]; 1777 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) { 1778 syslog(LOG_ERR, "getvfsbyname() failed for %s", 1779 fsp->f_fstypename); 1780 continue; 1781 } 1782 1783 /* 1784 * We do not need to delete "export" flag from 1785 * filesystems that do not have it set. 1786 */ 1787 if (!(fsp->f_flags & MNT_EXPORTED)) 1788 continue; 1789 /* 1790 * Do not delete export for network filesystem by 1791 * passing "export" arg to nmount(). 1792 * It only makes sense to do this for local filesystems. 1793 */ 1794 if (vfc.vfc_flags & VFCF_NETWORK) 1795 continue; 1796 1797 iov[1].iov_base = fsp->f_fstypename; 1798 iov[1].iov_len = strlen(fsp->f_fstypename) + 1; 1799 iov[3].iov_base = fsp->f_mntonname; 1800 iov[3].iov_len = strlen(fsp->f_mntonname) + 1; 1801 iov[5].iov_base = fsp->f_mntfromname; 1802 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1; 1803 errmsg[0] = '\0'; 1804 1805 /* 1806 * EXDEV is returned when path exists but is not a 1807 * mount point. May happens if raced with unmount. 1808 */ 1809 if (nmount(iov, iovlen, fsp->f_flags) < 0 && 1810 errno != ENOENT && errno != ENOTSUP && errno != EXDEV) { 1811 syslog(LOG_ERR, 1812 "can't delete exports for %s: %m %s", 1813 fsp->f_mntonname, errmsg); 1814 } 1815 } 1816 1817 if (iov != NULL) { 1818 /* Free strings allocated by strdup() in getmntopts.c */ 1819 free(iov[0].iov_base); /* fstype */ 1820 free(iov[2].iov_base); /* fspath */ 1821 free(iov[4].iov_base); /* from */ 1822 free(iov[6].iov_base); /* update */ 1823 free(iov[8].iov_base); /* export */ 1824 free(iov[10].iov_base); /* errmsg */ 1825 1826 /* free iov, allocated by realloc() */ 1827 free(iov); 1828 iovlen = 0; 1829 } 1830 1831 /* 1832 * Read in the exports file and build the list, calling 1833 * nmount() as we go along to push the export rules into the kernel. 1834 */ 1835 done = 0; 1836 for (i = 0; exnames[i] != NULL; i++) { 1837 if (debug) 1838 warnx("reading exports from %s", exnames[i]); 1839 if ((exp_file = fopen(exnames[i], "r")) == NULL) { 1840 syslog(LOG_WARNING, "can't open %s", exnames[i]); 1841 continue; 1842 } 1843 get_exportlist_one(); 1844 fclose(exp_file); 1845 done++; 1846 } 1847 if (done == 0) { 1848 syslog(LOG_ERR, "can't open any exports file"); 1849 exit(2); 1850 } 1851 1852 /* 1853 * If there was no public fh, clear any previous one set. 1854 */ 1855 if (has_publicfh == 0) 1856 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL); 1857 1858 /* Resume the nfsd. If they weren't suspended, this is harmless. */ 1859 (void)nfssvc(NFSSVC_RESUMENFSD, NULL); 1860 } 1861 1862 /* 1863 * Allocate an export list element 1864 */ 1865 static struct exportlist * 1866 get_exp(void) 1867 { 1868 struct exportlist *ep; 1869 1870 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist)); 1871 if (ep == (struct exportlist *)NULL) 1872 out_of_mem(); 1873 return (ep); 1874 } 1875 1876 /* 1877 * Allocate a group list element 1878 */ 1879 static struct grouplist * 1880 get_grp(void) 1881 { 1882 struct grouplist *gp; 1883 1884 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist)); 1885 if (gp == (struct grouplist *)NULL) 1886 out_of_mem(); 1887 return (gp); 1888 } 1889 1890 /* 1891 * Clean up upon an error in get_exportlist(). 1892 */ 1893 static void 1894 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason) 1895 { 1896 struct grouplist *tgrp; 1897 1898 if (!(opt_flags & OP_QUIET)) { 1899 if (reason != NULL) 1900 syslog(LOG_ERR, "bad exports list line '%s': %s", line, 1901 reason); 1902 else 1903 syslog(LOG_ERR, "bad exports list line '%s'", line); 1904 } 1905 if (ep && (ep->ex_flag & EX_LINKED) == 0) 1906 free_exp(ep); 1907 while (grp) { 1908 tgrp = grp; 1909 grp = grp->gr_next; 1910 free_grp(tgrp); 1911 } 1912 } 1913 1914 /* 1915 * Search the export list for a matching fs. 1916 */ 1917 static struct exportlist * 1918 ex_search(fsid_t *fsid) 1919 { 1920 struct exportlist *ep; 1921 1922 SLIST_FOREACH(ep, &exphead, entries) { 1923 if (ep->ex_fs.val[0] == fsid->val[0] && 1924 ep->ex_fs.val[1] == fsid->val[1]) 1925 return (ep); 1926 } 1927 1928 return (ep); 1929 } 1930 1931 /* 1932 * Add a directory path to the list. 1933 */ 1934 static char * 1935 add_expdir(struct dirlist **dpp, char *cp, int len) 1936 { 1937 struct dirlist *dp; 1938 1939 dp = malloc(sizeof (struct dirlist)); 1940 if (dp == (struct dirlist *)NULL) 1941 out_of_mem(); 1942 dp->dp_left = *dpp; 1943 dp->dp_right = (struct dirlist *)NULL; 1944 dp->dp_flag = 0; 1945 dp->dp_hosts = (struct hostlist *)NULL; 1946 dp->dp_dirp = strndup(cp, len); 1947 if (dp->dp_dirp == NULL) 1948 out_of_mem(); 1949 *dpp = dp; 1950 return (dp->dp_dirp); 1951 } 1952 1953 /* 1954 * Hang the dir list element off the dirpath binary tree as required 1955 * and update the entry for host. 1956 */ 1957 static void 1958 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep, 1959 int flags) 1960 { 1961 struct hostlist *hp; 1962 struct dirlist *dp2; 1963 1964 if (flags & OP_ALLDIRS) { 1965 if (ep->ex_defdir) 1966 free((caddr_t)dp); 1967 else 1968 ep->ex_defdir = dp; 1969 if (grp == (struct grouplist *)NULL) { 1970 ep->ex_defdir->dp_flag |= DP_DEFSET; 1971 /* Save the default security flavors list. */ 1972 ep->ex_defnumsecflavors = ep->ex_numsecflavors; 1973 if (ep->ex_numsecflavors > 0) 1974 memcpy(ep->ex_defsecflavors, ep->ex_secflavors, 1975 sizeof(ep->ex_secflavors)); 1976 } else while (grp) { 1977 hp = get_ht(); 1978 hp->ht_grp = grp; 1979 hp->ht_next = ep->ex_defdir->dp_hosts; 1980 ep->ex_defdir->dp_hosts = hp; 1981 /* Save the security flavors list for this host set. */ 1982 grp->gr_numsecflavors = ep->ex_numsecflavors; 1983 if (ep->ex_numsecflavors > 0) 1984 memcpy(grp->gr_secflavors, ep->ex_secflavors, 1985 sizeof(ep->ex_secflavors)); 1986 grp = grp->gr_next; 1987 } 1988 } else { 1989 1990 /* 1991 * Loop through the directories adding them to the tree. 1992 */ 1993 while (dp) { 1994 dp2 = dp->dp_left; 1995 add_dlist(&ep->ex_dirl, dp, grp, flags, ep); 1996 dp = dp2; 1997 } 1998 } 1999 } 2000 2001 /* 2002 * Traverse the binary tree either updating a node that is already there 2003 * for the new directory or adding the new node. 2004 */ 2005 static void 2006 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp, 2007 int flags, struct exportlist *ep) 2008 { 2009 struct dirlist *dp; 2010 struct hostlist *hp; 2011 int cmp; 2012 2013 dp = *dpp; 2014 if (dp) { 2015 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 2016 if (cmp > 0) { 2017 add_dlist(&dp->dp_left, newdp, grp, flags, ep); 2018 return; 2019 } else if (cmp < 0) { 2020 add_dlist(&dp->dp_right, newdp, grp, flags, ep); 2021 return; 2022 } else 2023 free((caddr_t)newdp); 2024 } else { 2025 dp = newdp; 2026 dp->dp_left = (struct dirlist *)NULL; 2027 *dpp = dp; 2028 } 2029 if (grp) { 2030 2031 /* 2032 * Hang all of the host(s) off of the directory point. 2033 */ 2034 do { 2035 hp = get_ht(); 2036 hp->ht_grp = grp; 2037 hp->ht_next = dp->dp_hosts; 2038 dp->dp_hosts = hp; 2039 /* Save the security flavors list for this host set. */ 2040 grp->gr_numsecflavors = ep->ex_numsecflavors; 2041 if (ep->ex_numsecflavors > 0) 2042 memcpy(grp->gr_secflavors, ep->ex_secflavors, 2043 sizeof(ep->ex_secflavors)); 2044 grp = grp->gr_next; 2045 } while (grp); 2046 } else { 2047 dp->dp_flag |= DP_DEFSET; 2048 /* Save the default security flavors list. */ 2049 ep->ex_defnumsecflavors = ep->ex_numsecflavors; 2050 if (ep->ex_numsecflavors > 0) 2051 memcpy(ep->ex_defsecflavors, ep->ex_secflavors, 2052 sizeof(ep->ex_secflavors)); 2053 } 2054 } 2055 2056 /* 2057 * Search for a dirpath on the export point. 2058 */ 2059 static struct dirlist * 2060 dirp_search(struct dirlist *dp, char *dirp) 2061 { 2062 int cmp; 2063 2064 if (dp) { 2065 cmp = strcmp(dp->dp_dirp, dirp); 2066 if (cmp > 0) 2067 return (dirp_search(dp->dp_left, dirp)); 2068 else if (cmp < 0) 2069 return (dirp_search(dp->dp_right, dirp)); 2070 else 2071 return (dp); 2072 } 2073 return (dp); 2074 } 2075 2076 /* 2077 * Scan for a host match in a directory tree. 2078 */ 2079 static int 2080 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp, 2081 int *hostsetp, int *numsecflavors, int **secflavorsp) 2082 { 2083 struct hostlist *hp; 2084 struct grouplist *grp; 2085 struct addrinfo *ai; 2086 2087 if (dp) { 2088 if (dp->dp_flag & DP_DEFSET) 2089 *defsetp = dp->dp_flag; 2090 hp = dp->dp_hosts; 2091 while (hp) { 2092 grp = hp->ht_grp; 2093 switch (grp->gr_type) { 2094 case GT_HOST: 2095 ai = grp->gr_ptr.gt_addrinfo; 2096 for (; ai; ai = ai->ai_next) { 2097 if (!sacmp(ai->ai_addr, saddr, NULL)) { 2098 *hostsetp = 2099 (hp->ht_flag | DP_HOSTSET); 2100 if (numsecflavors != NULL) { 2101 *numsecflavors = 2102 grp->gr_numsecflavors; 2103 *secflavorsp = 2104 grp->gr_secflavors; 2105 } 2106 return (1); 2107 } 2108 } 2109 break; 2110 case GT_NET: 2111 if (!sacmp(saddr, (struct sockaddr *) 2112 &grp->gr_ptr.gt_net.nt_net, 2113 (struct sockaddr *) 2114 &grp->gr_ptr.gt_net.nt_mask)) { 2115 *hostsetp = (hp->ht_flag | DP_HOSTSET); 2116 if (numsecflavors != NULL) { 2117 *numsecflavors = 2118 grp->gr_numsecflavors; 2119 *secflavorsp = 2120 grp->gr_secflavors; 2121 } 2122 return (1); 2123 } 2124 break; 2125 } 2126 hp = hp->ht_next; 2127 } 2128 } 2129 return (0); 2130 } 2131 2132 /* 2133 * Scan tree for a host that matches the address. 2134 */ 2135 static int 2136 scan_tree(struct dirlist *dp, struct sockaddr *saddr) 2137 { 2138 int defset, hostset; 2139 2140 if (dp) { 2141 if (scan_tree(dp->dp_left, saddr)) 2142 return (1); 2143 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL)) 2144 return (1); 2145 if (scan_tree(dp->dp_right, saddr)) 2146 return (1); 2147 } 2148 return (0); 2149 } 2150 2151 /* 2152 * Traverse the dirlist tree and free it up. 2153 */ 2154 static void 2155 free_dir(struct dirlist *dp) 2156 { 2157 2158 if (dp) { 2159 free_dir(dp->dp_left); 2160 free_dir(dp->dp_right); 2161 free_host(dp->dp_hosts); 2162 free(dp->dp_dirp); 2163 free(dp); 2164 } 2165 } 2166 2167 /* 2168 * Parse a colon separated list of security flavors 2169 */ 2170 static int 2171 parsesec(char *seclist, struct exportlist *ep) 2172 { 2173 char *cp, savedc; 2174 int flavor; 2175 2176 ep->ex_numsecflavors = 0; 2177 for (;;) { 2178 cp = strchr(seclist, ':'); 2179 if (cp) { 2180 savedc = *cp; 2181 *cp = '\0'; 2182 } 2183 2184 if (!strcmp(seclist, "sys")) 2185 flavor = AUTH_SYS; 2186 else if (!strcmp(seclist, "krb5")) 2187 flavor = RPCSEC_GSS_KRB5; 2188 else if (!strcmp(seclist, "krb5i")) 2189 flavor = RPCSEC_GSS_KRB5I; 2190 else if (!strcmp(seclist, "krb5p")) 2191 flavor = RPCSEC_GSS_KRB5P; 2192 else { 2193 if (cp) 2194 *cp = savedc; 2195 syslog(LOG_ERR, "bad sec flavor: %s", seclist); 2196 return (1); 2197 } 2198 if (ep->ex_numsecflavors == MAXSECFLAVORS) { 2199 if (cp) 2200 *cp = savedc; 2201 syslog(LOG_ERR, "too many sec flavors: %s", seclist); 2202 return (1); 2203 } 2204 ep->ex_secflavors[ep->ex_numsecflavors] = flavor; 2205 ep->ex_numsecflavors++; 2206 if (cp) { 2207 *cp = savedc; 2208 seclist = cp + 1; 2209 } else { 2210 break; 2211 } 2212 } 2213 return (0); 2214 } 2215 2216 /* 2217 * Parse the option string and update fields. 2218 * Option arguments may either be -<option>=<value> or 2219 * -<option> <value> 2220 */ 2221 static int 2222 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp, 2223 int *has_hostp, int *exflagsp, struct xucred *cr) 2224 { 2225 char *cpoptarg, *cpoptend; 2226 char *cp, *endcp, *cpopt, savedc, savedc2; 2227 int allflag, usedarg; 2228 2229 savedc2 = '\0'; 2230 cpopt = *cpp; 2231 cpopt++; 2232 cp = *endcpp; 2233 savedc = *cp; 2234 *cp = '\0'; 2235 while (cpopt && *cpopt) { 2236 allflag = 1; 2237 usedarg = -2; 2238 if ((cpoptend = strchr(cpopt, ','))) { 2239 *cpoptend++ = '\0'; 2240 if ((cpoptarg = strchr(cpopt, '='))) 2241 *cpoptarg++ = '\0'; 2242 } else { 2243 if ((cpoptarg = strchr(cpopt, '='))) 2244 *cpoptarg++ = '\0'; 2245 else { 2246 *cp = savedc; 2247 nextfield(&cp, &endcp); 2248 **endcpp = '\0'; 2249 if (endcp > cp && *cp != '-') { 2250 cpoptarg = cp; 2251 savedc2 = *endcp; 2252 *endcp = '\0'; 2253 usedarg = 0; 2254 } 2255 } 2256 } 2257 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 2258 *exflagsp |= MNT_EXRDONLY; 2259 } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 2260 !(allflag = strcmp(cpopt, "mapall")) || 2261 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 2262 usedarg++; 2263 parsecred(cpoptarg, cr); 2264 if (allflag == 0) { 2265 *exflagsp |= MNT_EXPORTANON; 2266 opt_flags |= OP_MAPALL; 2267 } else 2268 opt_flags |= OP_MAPROOT; 2269 } else if (cpoptarg && (!strcmp(cpopt, "mask") || 2270 !strcmp(cpopt, "m"))) { 2271 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 2272 syslog(LOG_ERR, "bad mask: %s", cpoptarg); 2273 return (1); 2274 } 2275 usedarg++; 2276 opt_flags |= OP_MASK; 2277 } else if (cpoptarg && (!strcmp(cpopt, "network") || 2278 !strcmp(cpopt, "n"))) { 2279 if (strchr(cpoptarg, '/') != NULL) { 2280 if (debug) 2281 fprintf(stderr, "setting OP_MASKLEN\n"); 2282 opt_flags |= OP_MASKLEN; 2283 } 2284 if (grp->gr_type != GT_NULL) { 2285 syslog(LOG_ERR, "network/host conflict"); 2286 return (1); 2287 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 2288 syslog(LOG_ERR, "bad net: %s", cpoptarg); 2289 return (1); 2290 } 2291 grp->gr_type = GT_NET; 2292 *has_hostp = 1; 2293 usedarg++; 2294 opt_flags |= OP_NET; 2295 } else if (!strcmp(cpopt, "alldirs")) { 2296 opt_flags |= OP_ALLDIRS; 2297 } else if (!strcmp(cpopt, "public")) { 2298 *exflagsp |= MNT_EXPUBLIC; 2299 } else if (!strcmp(cpopt, "webnfs")) { 2300 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 2301 opt_flags |= OP_MAPALL; 2302 } else if (cpoptarg && !strcmp(cpopt, "index")) { 2303 ep->ex_indexfile = strdup(cpoptarg); 2304 } else if (!strcmp(cpopt, "quiet")) { 2305 opt_flags |= OP_QUIET; 2306 } else if (cpoptarg && !strcmp(cpopt, "sec")) { 2307 if (parsesec(cpoptarg, ep)) 2308 return (1); 2309 opt_flags |= OP_SEC; 2310 usedarg++; 2311 } else { 2312 syslog(LOG_ERR, "bad opt %s", cpopt); 2313 return (1); 2314 } 2315 if (usedarg >= 0) { 2316 *endcp = savedc2; 2317 **endcpp = savedc; 2318 if (usedarg > 0) { 2319 *cpp = cp; 2320 *endcpp = endcp; 2321 } 2322 return (0); 2323 } 2324 cpopt = cpoptend; 2325 } 2326 **endcpp = savedc; 2327 return (0); 2328 } 2329 2330 /* 2331 * Translate a character string to the corresponding list of network 2332 * addresses for a hostname. 2333 */ 2334 static int 2335 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp) 2336 { 2337 struct grouplist *checkgrp; 2338 struct addrinfo *ai, *tai, hints; 2339 int ecode; 2340 char host[NI_MAXHOST]; 2341 2342 if (grp->gr_type != GT_NULL) { 2343 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 2344 return (1); 2345 } 2346 memset(&hints, 0, sizeof hints); 2347 hints.ai_flags = AI_CANONNAME; 2348 hints.ai_protocol = IPPROTO_UDP; 2349 ecode = getaddrinfo(cp, NULL, &hints, &ai); 2350 if (ecode != 0) { 2351 syslog(LOG_ERR,"can't get address info for host %s", cp); 2352 return 1; 2353 } 2354 grp->gr_ptr.gt_addrinfo = ai; 2355 while (ai != NULL) { 2356 if (ai->ai_canonname == NULL) { 2357 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 2358 sizeof host, NULL, 0, NI_NUMERICHOST) != 0) 2359 strlcpy(host, "?", sizeof(host)); 2360 ai->ai_canonname = strdup(host); 2361 ai->ai_flags |= AI_CANONNAME; 2362 } 2363 if (debug) 2364 fprintf(stderr, "got host %s\n", ai->ai_canonname); 2365 /* 2366 * Sanity check: make sure we don't already have an entry 2367 * for this host in the grouplist. 2368 */ 2369 for (checkgrp = tgrp; checkgrp != NULL; 2370 checkgrp = checkgrp->gr_next) { 2371 if (checkgrp->gr_type != GT_HOST) 2372 continue; 2373 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 2374 tai = tai->ai_next) { 2375 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 2376 continue; 2377 if (debug) 2378 fprintf(stderr, 2379 "ignoring duplicate host %s\n", 2380 ai->ai_canonname); 2381 grp->gr_type = GT_IGNORE; 2382 return (0); 2383 } 2384 } 2385 ai = ai->ai_next; 2386 } 2387 grp->gr_type = GT_HOST; 2388 return (0); 2389 } 2390 2391 /* 2392 * Free up an exports list component 2393 */ 2394 static void 2395 free_exp(struct exportlist *ep) 2396 { 2397 2398 if (ep->ex_defdir) { 2399 free_host(ep->ex_defdir->dp_hosts); 2400 free((caddr_t)ep->ex_defdir); 2401 } 2402 if (ep->ex_fsdir) 2403 free(ep->ex_fsdir); 2404 if (ep->ex_indexfile) 2405 free(ep->ex_indexfile); 2406 free_dir(ep->ex_dirl); 2407 free((caddr_t)ep); 2408 } 2409 2410 /* 2411 * Free hosts. 2412 */ 2413 static void 2414 free_host(struct hostlist *hp) 2415 { 2416 struct hostlist *hp2; 2417 2418 while (hp) { 2419 hp2 = hp; 2420 hp = hp->ht_next; 2421 free((caddr_t)hp2); 2422 } 2423 } 2424 2425 static struct hostlist * 2426 get_ht(void) 2427 { 2428 struct hostlist *hp; 2429 2430 hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 2431 if (hp == (struct hostlist *)NULL) 2432 out_of_mem(); 2433 hp->ht_next = (struct hostlist *)NULL; 2434 hp->ht_flag = 0; 2435 return (hp); 2436 } 2437 2438 /* 2439 * Out of memory, fatal 2440 */ 2441 static void 2442 out_of_mem(void) 2443 { 2444 2445 syslog(LOG_ERR, "out of memory"); 2446 exit(2); 2447 } 2448 2449 /* 2450 * Do the nmount() syscall with the update flag to push the export info into 2451 * the kernel. 2452 */ 2453 static int 2454 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags, 2455 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb) 2456 { 2457 struct statfs fsb1; 2458 struct addrinfo *ai; 2459 struct export_args *eap; 2460 char errmsg[255]; 2461 char *cp; 2462 int done; 2463 char savedc; 2464 struct iovec *iov; 2465 int i, iovlen; 2466 int ret; 2467 struct nfsex_args nfsea; 2468 2469 eap = &nfsea.export; 2470 2471 cp = NULL; 2472 savedc = '\0'; 2473 iov = NULL; 2474 iovlen = 0; 2475 ret = 0; 2476 2477 bzero(eap, sizeof (struct export_args)); 2478 bzero(errmsg, sizeof(errmsg)); 2479 eap->ex_flags = exflags; 2480 eap->ex_anon = *anoncrp; 2481 eap->ex_indexfile = ep->ex_indexfile; 2482 if (grp->gr_type == GT_HOST) 2483 ai = grp->gr_ptr.gt_addrinfo; 2484 else 2485 ai = NULL; 2486 eap->ex_numsecflavors = ep->ex_numsecflavors; 2487 for (i = 0; i < eap->ex_numsecflavors; i++) 2488 eap->ex_secflavors[i] = ep->ex_secflavors[i]; 2489 if (eap->ex_numsecflavors == 0) { 2490 eap->ex_numsecflavors = 1; 2491 eap->ex_secflavors[0] = AUTH_SYS; 2492 } 2493 done = FALSE; 2494 2495 if (v4root_phase == 0) { 2496 build_iovec(&iov, &iovlen, "fstype", NULL, 0); 2497 build_iovec(&iov, &iovlen, "fspath", NULL, 0); 2498 build_iovec(&iov, &iovlen, "from", NULL, 0); 2499 build_iovec(&iov, &iovlen, "update", NULL, 0); 2500 build_iovec(&iov, &iovlen, "export", eap, 2501 sizeof (struct export_args)); 2502 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 2503 } 2504 2505 while (!done) { 2506 switch (grp->gr_type) { 2507 case GT_HOST: 2508 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 2509 goto skip; 2510 eap->ex_addr = ai->ai_addr; 2511 eap->ex_addrlen = ai->ai_addrlen; 2512 eap->ex_masklen = 0; 2513 break; 2514 case GT_NET: 2515 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 2516 have_v6 == 0) 2517 goto skip; 2518 eap->ex_addr = 2519 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 2520 eap->ex_addrlen = 2521 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len; 2522 eap->ex_mask = 2523 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 2524 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len; 2525 break; 2526 case GT_DEFAULT: 2527 eap->ex_addr = NULL; 2528 eap->ex_addrlen = 0; 2529 eap->ex_mask = NULL; 2530 eap->ex_masklen = 0; 2531 break; 2532 case GT_IGNORE: 2533 ret = 0; 2534 goto error_exit; 2535 break; 2536 default: 2537 syslog(LOG_ERR, "bad grouptype"); 2538 if (cp) 2539 *cp = savedc; 2540 ret = 1; 2541 goto error_exit; 2542 } 2543 2544 /* 2545 * For V4:, use the nfssvc() syscall, instead of mount(). 2546 */ 2547 if (v4root_phase == 2) { 2548 nfsea.fspec = v4root_dirpath; 2549 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) { 2550 syslog(LOG_ERR, "Exporting V4: failed"); 2551 return (2); 2552 } 2553 } else { 2554 /* 2555 * XXX: 2556 * Maybe I should just use the fsb->f_mntonname path 2557 * instead of looping back up the dirp to the mount 2558 * point?? 2559 * Also, needs to know how to export all types of local 2560 * exportable filesystems and not just "ufs". 2561 */ 2562 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */ 2563 iov[1].iov_len = strlen(fsb->f_fstypename) + 1; 2564 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */ 2565 iov[3].iov_len = strlen(fsb->f_mntonname) + 1; 2566 iov[5].iov_base = fsb->f_mntfromname; /* "from" */ 2567 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1; 2568 errmsg[0] = '\0'; 2569 2570 while (nmount(iov, iovlen, fsb->f_flags) < 0) { 2571 if (cp) 2572 *cp-- = savedc; 2573 else 2574 cp = dirp + dirplen - 1; 2575 if (opt_flags & OP_QUIET) { 2576 ret = 1; 2577 goto error_exit; 2578 } 2579 if (errno == EPERM) { 2580 if (debug) 2581 warnx("can't change attributes for %s: %s", 2582 dirp, errmsg); 2583 syslog(LOG_ERR, 2584 "can't change attributes for %s: %s", 2585 dirp, errmsg); 2586 ret = 1; 2587 goto error_exit; 2588 } 2589 if (opt_flags & OP_ALLDIRS) { 2590 if (errno == EINVAL) 2591 syslog(LOG_ERR, 2592 "-alldirs requested but %s is not a filesystem mountpoint", 2593 dirp); 2594 else 2595 syslog(LOG_ERR, 2596 "could not remount %s: %m", 2597 dirp); 2598 ret = 1; 2599 goto error_exit; 2600 } 2601 /* back up over the last component */ 2602 while (*cp == '/' && cp > dirp) 2603 cp--; 2604 while (*(cp - 1) != '/' && cp > dirp) 2605 cp--; 2606 if (cp == dirp) { 2607 if (debug) 2608 warnx("mnt unsucc"); 2609 syslog(LOG_ERR, "can't export %s %s", 2610 dirp, errmsg); 2611 ret = 1; 2612 goto error_exit; 2613 } 2614 savedc = *cp; 2615 *cp = '\0'; 2616 /* 2617 * Check that we're still on the same 2618 * filesystem. 2619 */ 2620 if (statfs(dirp, &fsb1) != 0 || 2621 bcmp(&fsb1.f_fsid, &fsb->f_fsid, 2622 sizeof (fsb1.f_fsid)) != 0) { 2623 *cp = savedc; 2624 syslog(LOG_ERR, 2625 "can't export %s %s", dirp, 2626 errmsg); 2627 ret = 1; 2628 goto error_exit; 2629 } 2630 } 2631 } 2632 2633 /* 2634 * For the experimental server: 2635 * If this is the public directory, get the file handle 2636 * and load it into the kernel via the nfssvc() syscall. 2637 */ 2638 if ((exflags & MNT_EXPUBLIC) != 0) { 2639 fhandle_t fh; 2640 char *public_name; 2641 2642 if (eap->ex_indexfile != NULL) 2643 public_name = eap->ex_indexfile; 2644 else 2645 public_name = dirp; 2646 if (getfh(public_name, &fh) < 0) 2647 syslog(LOG_ERR, 2648 "Can't get public fh for %s", public_name); 2649 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0) 2650 syslog(LOG_ERR, 2651 "Can't set public fh for %s", public_name); 2652 else 2653 has_publicfh = 1; 2654 } 2655 skip: 2656 if (ai != NULL) 2657 ai = ai->ai_next; 2658 if (ai == NULL) 2659 done = TRUE; 2660 } 2661 if (cp) 2662 *cp = savedc; 2663 error_exit: 2664 /* free strings allocated by strdup() in getmntopts.c */ 2665 if (iov != NULL) { 2666 free(iov[0].iov_base); /* fstype */ 2667 free(iov[2].iov_base); /* fspath */ 2668 free(iov[4].iov_base); /* from */ 2669 free(iov[6].iov_base); /* update */ 2670 free(iov[8].iov_base); /* export */ 2671 free(iov[10].iov_base); /* errmsg */ 2672 2673 /* free iov, allocated by realloc() */ 2674 free(iov); 2675 } 2676 return (ret); 2677 } 2678 2679 /* 2680 * Translate a net address. 2681 * 2682 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 2683 */ 2684 static int 2685 get_net(char *cp, struct netmsk *net, int maskflg) 2686 { 2687 struct netent *np = NULL; 2688 char *name, *p, *prefp; 2689 struct sockaddr_in sin; 2690 struct sockaddr *sa = NULL; 2691 struct addrinfo hints, *ai = NULL; 2692 char netname[NI_MAXHOST]; 2693 long preflen; 2694 2695 p = prefp = NULL; 2696 if ((opt_flags & OP_MASKLEN) && !maskflg) { 2697 p = strchr(cp, '/'); 2698 *p = '\0'; 2699 prefp = p + 1; 2700 } 2701 2702 /* 2703 * Check for a numeric address first. We wish to avoid 2704 * possible DNS lookups in getnetbyname(). 2705 */ 2706 if (isxdigit(*cp) || *cp == ':') { 2707 memset(&hints, 0, sizeof hints); 2708 /* Ensure the mask and the network have the same family. */ 2709 if (maskflg && (opt_flags & OP_NET)) 2710 hints.ai_family = net->nt_net.ss_family; 2711 else if (!maskflg && (opt_flags & OP_HAVEMASK)) 2712 hints.ai_family = net->nt_mask.ss_family; 2713 else 2714 hints.ai_family = AF_UNSPEC; 2715 hints.ai_flags = AI_NUMERICHOST; 2716 if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 2717 sa = ai->ai_addr; 2718 if (sa != NULL && ai->ai_family == AF_INET) { 2719 /* 2720 * The address in `cp' is really a network address, so 2721 * use inet_network() to re-interpret this correctly. 2722 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 2723 */ 2724 bzero(&sin, sizeof sin); 2725 sin.sin_family = AF_INET; 2726 sin.sin_len = sizeof sin; 2727 sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 2728 if (debug) 2729 fprintf(stderr, "get_net: v4 addr %s\n", 2730 inet_ntoa(sin.sin_addr)); 2731 sa = (struct sockaddr *)&sin; 2732 } 2733 } 2734 if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 2735 bzero(&sin, sizeof sin); 2736 sin.sin_family = AF_INET; 2737 sin.sin_len = sizeof sin; 2738 sin.sin_addr = inet_makeaddr(np->n_net, 0); 2739 sa = (struct sockaddr *)&sin; 2740 } 2741 if (sa == NULL) 2742 goto fail; 2743 2744 if (maskflg) { 2745 /* The specified sockaddr is a mask. */ 2746 if (checkmask(sa) != 0) 2747 goto fail; 2748 bcopy(sa, &net->nt_mask, sa->sa_len); 2749 opt_flags |= OP_HAVEMASK; 2750 } else { 2751 /* The specified sockaddr is a network address. */ 2752 bcopy(sa, &net->nt_net, sa->sa_len); 2753 2754 /* Get a network name for the export list. */ 2755 if (np) { 2756 name = np->n_name; 2757 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 2758 NULL, 0, NI_NUMERICHOST) == 0) { 2759 name = netname; 2760 } else { 2761 goto fail; 2762 } 2763 if ((net->nt_name = strdup(name)) == NULL) 2764 out_of_mem(); 2765 2766 /* 2767 * Extract a mask from either a "/<masklen>" suffix, or 2768 * from the class of an IPv4 address. 2769 */ 2770 if (opt_flags & OP_MASKLEN) { 2771 preflen = strtol(prefp, NULL, 10); 2772 if (preflen < 0L || preflen == LONG_MAX) 2773 goto fail; 2774 bcopy(sa, &net->nt_mask, sa->sa_len); 2775 if (makemask(&net->nt_mask, (int)preflen) != 0) 2776 goto fail; 2777 opt_flags |= OP_HAVEMASK; 2778 *p = '/'; 2779 } else if (sa->sa_family == AF_INET && 2780 (opt_flags & OP_MASK) == 0) { 2781 in_addr_t addr; 2782 2783 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 2784 if (IN_CLASSA(addr)) 2785 preflen = 8; 2786 else if (IN_CLASSB(addr)) 2787 preflen = 16; 2788 else if (IN_CLASSC(addr)) 2789 preflen = 24; 2790 else if (IN_CLASSD(addr)) 2791 preflen = 28; 2792 else 2793 preflen = 32; /* XXX */ 2794 2795 bcopy(sa, &net->nt_mask, sa->sa_len); 2796 makemask(&net->nt_mask, (int)preflen); 2797 opt_flags |= OP_HAVEMASK; 2798 } 2799 } 2800 2801 if (ai) 2802 freeaddrinfo(ai); 2803 return 0; 2804 2805 fail: 2806 if (ai) 2807 freeaddrinfo(ai); 2808 return 1; 2809 } 2810 2811 /* 2812 * Parse out the next white space separated field 2813 */ 2814 static void 2815 nextfield(char **cp, char **endcp) 2816 { 2817 char *p; 2818 2819 p = *cp; 2820 while (*p == ' ' || *p == '\t') 2821 p++; 2822 if (*p == '\n' || *p == '\0') 2823 *cp = *endcp = p; 2824 else { 2825 *cp = p++; 2826 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 2827 p++; 2828 *endcp = p; 2829 } 2830 } 2831 2832 /* 2833 * Get an exports file line. Skip over blank lines and handle line 2834 * continuations. 2835 */ 2836 static int 2837 get_line(void) 2838 { 2839 char *p, *cp; 2840 size_t len; 2841 int totlen, cont_line; 2842 2843 /* 2844 * Loop around ignoring blank lines and getting all continuation lines. 2845 */ 2846 p = line; 2847 totlen = 0; 2848 do { 2849 if ((p = fgetln(exp_file, &len)) == NULL) 2850 return (0); 2851 cp = p + len - 1; 2852 cont_line = 0; 2853 while (cp >= p && 2854 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 2855 if (*cp == '\\') 2856 cont_line = 1; 2857 cp--; 2858 len--; 2859 } 2860 if (cont_line) { 2861 *++cp = ' '; 2862 len++; 2863 } 2864 if (linesize < len + totlen + 1) { 2865 linesize = len + totlen + 1; 2866 line = realloc(line, linesize); 2867 if (line == NULL) 2868 out_of_mem(); 2869 } 2870 memcpy(line + totlen, p, len); 2871 totlen += len; 2872 line[totlen] = '\0'; 2873 } while (totlen == 0 || cont_line); 2874 return (1); 2875 } 2876 2877 /* 2878 * Parse a description of a credential. 2879 */ 2880 static void 2881 parsecred(char *namelist, struct xucred *cr) 2882 { 2883 char *name; 2884 int cnt; 2885 char *names; 2886 struct passwd *pw; 2887 struct group *gr; 2888 gid_t groups[XU_NGROUPS + 1]; 2889 int ngroups; 2890 2891 cr->cr_version = XUCRED_VERSION; 2892 /* 2893 * Set up the unprivileged user. 2894 */ 2895 cr->cr_uid = 65534; 2896 cr->cr_groups[0] = 65533; 2897 cr->cr_ngroups = 1; 2898 /* 2899 * Get the user's password table entry. 2900 */ 2901 names = strsep_quote(&namelist, " \t\n"); 2902 name = strsep(&names, ":"); 2903 /* Bug? name could be NULL here */ 2904 if (isdigit(*name) || *name == '-') 2905 pw = getpwuid(atoi(name)); 2906 else 2907 pw = getpwnam(name); 2908 /* 2909 * Credentials specified as those of a user. 2910 */ 2911 if (names == NULL) { 2912 if (pw == NULL) { 2913 syslog(LOG_ERR, "unknown user: %s", name); 2914 return; 2915 } 2916 cr->cr_uid = pw->pw_uid; 2917 ngroups = XU_NGROUPS + 1; 2918 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) { 2919 syslog(LOG_ERR, "too many groups"); 2920 ngroups = XU_NGROUPS + 1; 2921 } 2922 2923 /* 2924 * Compress out duplicate. 2925 */ 2926 cr->cr_ngroups = ngroups - 1; 2927 cr->cr_groups[0] = groups[0]; 2928 for (cnt = 2; cnt < ngroups; cnt++) 2929 cr->cr_groups[cnt - 1] = groups[cnt]; 2930 return; 2931 } 2932 /* 2933 * Explicit credential specified as a colon separated list: 2934 * uid:gid:gid:... 2935 */ 2936 if (pw != NULL) 2937 cr->cr_uid = pw->pw_uid; 2938 else if (isdigit(*name) || *name == '-') 2939 cr->cr_uid = atoi(name); 2940 else { 2941 syslog(LOG_ERR, "unknown user: %s", name); 2942 return; 2943 } 2944 cr->cr_ngroups = 0; 2945 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) { 2946 name = strsep(&names, ":"); 2947 if (isdigit(*name) || *name == '-') { 2948 cr->cr_groups[cr->cr_ngroups++] = atoi(name); 2949 } else { 2950 if ((gr = getgrnam(name)) == NULL) { 2951 syslog(LOG_ERR, "unknown group: %s", name); 2952 continue; 2953 } 2954 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 2955 } 2956 } 2957 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS) 2958 syslog(LOG_ERR, "too many groups"); 2959 } 2960 2961 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50) 2962 /* 2963 * Routines that maintain the remote mounttab 2964 */ 2965 static void 2966 get_mountlist(void) 2967 { 2968 struct mountlist *mlp; 2969 char *host, *dirp, *cp; 2970 char str[STRSIZ]; 2971 FILE *mlfile; 2972 2973 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 2974 if (errno == ENOENT) 2975 return; 2976 else { 2977 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 2978 return; 2979 } 2980 } 2981 while (fgets(str, STRSIZ, mlfile) != NULL) { 2982 cp = str; 2983 host = strsep(&cp, " \t\n"); 2984 dirp = strsep(&cp, " \t\n"); 2985 if (host == NULL || dirp == NULL) 2986 continue; 2987 mlp = (struct mountlist *)malloc(sizeof (*mlp)); 2988 if (mlp == (struct mountlist *)NULL) 2989 out_of_mem(); 2990 strncpy(mlp->ml_host, host, MNTNAMLEN); 2991 mlp->ml_host[MNTNAMLEN] = '\0'; 2992 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN); 2993 mlp->ml_dirp[MNTPATHLEN] = '\0'; 2994 2995 SLIST_INSERT_HEAD(&mlhead, mlp, next); 2996 } 2997 fclose(mlfile); 2998 } 2999 3000 static void 3001 del_mlist(char *hostp, char *dirp) 3002 { 3003 struct mountlist *mlp, *mlp2; 3004 FILE *mlfile; 3005 int fnd = 0; 3006 3007 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) { 3008 if (!strcmp(mlp->ml_host, hostp) && 3009 (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 3010 fnd = 1; 3011 SLIST_REMOVE(&mlhead, mlp, mountlist, next); 3012 free((caddr_t)mlp); 3013 } 3014 } 3015 if (fnd) { 3016 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 3017 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 3018 return; 3019 } 3020 SLIST_FOREACH(mlp, &mlhead, next) { 3021 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 3022 } 3023 fclose(mlfile); 3024 } 3025 } 3026 3027 static void 3028 add_mlist(char *hostp, char *dirp) 3029 { 3030 struct mountlist *mlp; 3031 FILE *mlfile; 3032 3033 SLIST_FOREACH(mlp, &mlhead, next) { 3034 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 3035 return; 3036 } 3037 3038 mlp = (struct mountlist *)malloc(sizeof (*mlp)); 3039 if (mlp == (struct mountlist *)NULL) 3040 out_of_mem(); 3041 strncpy(mlp->ml_host, hostp, MNTNAMLEN); 3042 mlp->ml_host[MNTNAMLEN] = '\0'; 3043 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN); 3044 mlp->ml_dirp[MNTPATHLEN] = '\0'; 3045 SLIST_INSERT_HEAD(&mlhead, mlp, next); 3046 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 3047 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 3048 return; 3049 } 3050 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 3051 fclose(mlfile); 3052 } 3053 3054 /* 3055 * Free up a group list. 3056 */ 3057 static void 3058 free_grp(struct grouplist *grp) 3059 { 3060 if (grp->gr_type == GT_HOST) { 3061 if (grp->gr_ptr.gt_addrinfo != NULL) 3062 freeaddrinfo(grp->gr_ptr.gt_addrinfo); 3063 } else if (grp->gr_type == GT_NET) { 3064 if (grp->gr_ptr.gt_net.nt_name) 3065 free(grp->gr_ptr.gt_net.nt_name); 3066 } 3067 free((caddr_t)grp); 3068 } 3069 3070 #ifdef DEBUG 3071 static void 3072 SYSLOG(int pri, const char *fmt, ...) 3073 { 3074 va_list ap; 3075 3076 va_start(ap, fmt); 3077 vfprintf(stderr, fmt, ap); 3078 va_end(ap); 3079 } 3080 #endif /* DEBUG */ 3081 3082 /* 3083 * Check options for consistency. 3084 */ 3085 static int 3086 check_options(struct dirlist *dp) 3087 { 3088 3089 if (v4root_phase == 0 && dp == NULL) 3090 return (1); 3091 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) { 3092 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive"); 3093 return (1); 3094 } 3095 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 3096 syslog(LOG_ERR, "-mask requires -network"); 3097 return (1); 3098 } 3099 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 3100 syslog(LOG_ERR, "-network requires mask specification"); 3101 return (1); 3102 } 3103 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 3104 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 3105 return (1); 3106 } 3107 if (v4root_phase > 0 && 3108 (opt_flags & 3109 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) { 3110 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:"); 3111 return (1); 3112 } 3113 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 3114 syslog(LOG_ERR, "-alldirs has multiple directories"); 3115 return (1); 3116 } 3117 return (0); 3118 } 3119 3120 /* 3121 * Check an absolute directory path for any symbolic links. Return true 3122 */ 3123 static int 3124 check_dirpath(char *dirp) 3125 { 3126 char *cp; 3127 int ret = 1; 3128 struct stat sb; 3129 3130 cp = dirp + 1; 3131 while (*cp && ret) { 3132 if (*cp == '/') { 3133 *cp = '\0'; 3134 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 3135 ret = 0; 3136 *cp = '/'; 3137 } 3138 cp++; 3139 } 3140 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 3141 ret = 0; 3142 return (ret); 3143 } 3144 3145 /* 3146 * Make a netmask according to the specified prefix length. The ss_family 3147 * and other non-address fields must be initialised before calling this. 3148 */ 3149 static int 3150 makemask(struct sockaddr_storage *ssp, int bitlen) 3151 { 3152 u_char *p; 3153 int bits, i, len; 3154 3155 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 3156 return (-1); 3157 if (bitlen > len * CHAR_BIT) 3158 return (-1); 3159 3160 for (i = 0; i < len; i++) { 3161 bits = MIN(CHAR_BIT, bitlen); 3162 *p++ = (u_char)~0 << (CHAR_BIT - bits); 3163 bitlen -= bits; 3164 } 3165 return 0; 3166 } 3167 3168 /* 3169 * Check that the sockaddr is a valid netmask. Returns 0 if the mask 3170 * is acceptable (i.e. of the form 1...10....0). 3171 */ 3172 static int 3173 checkmask(struct sockaddr *sa) 3174 { 3175 u_char *mask; 3176 int i, len; 3177 3178 if ((mask = sa_rawaddr(sa, &len)) == NULL) 3179 return (-1); 3180 3181 for (i = 0; i < len; i++) 3182 if (mask[i] != 0xff) 3183 break; 3184 if (i < len) { 3185 if (~mask[i] & (u_char)(~mask[i] + 1)) 3186 return (-1); 3187 i++; 3188 } 3189 for (; i < len; i++) 3190 if (mask[i] != 0) 3191 return (-1); 3192 return (0); 3193 } 3194 3195 /* 3196 * Compare two sockaddrs according to a specified mask. Return zero if 3197 * `sa1' matches `sa2' when filtered by the netmask in `samask'. 3198 * If samask is NULL, perform a full comparison. 3199 */ 3200 static int 3201 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 3202 { 3203 unsigned char *p1, *p2, *mask; 3204 int len, i; 3205 3206 if (sa1->sa_family != sa2->sa_family || 3207 (p1 = sa_rawaddr(sa1, &len)) == NULL || 3208 (p2 = sa_rawaddr(sa2, NULL)) == NULL) 3209 return (1); 3210 3211 switch (sa1->sa_family) { 3212 case AF_INET6: 3213 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 3214 ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 3215 return (1); 3216 break; 3217 } 3218 3219 /* Simple binary comparison if no mask specified. */ 3220 if (samask == NULL) 3221 return (memcmp(p1, p2, len)); 3222 3223 /* Set up the mask, and do a mask-based comparison. */ 3224 if (sa1->sa_family != samask->sa_family || 3225 (mask = sa_rawaddr(samask, NULL)) == NULL) 3226 return (1); 3227 3228 for (i = 0; i < len; i++) 3229 if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 3230 return (1); 3231 return (0); 3232 } 3233 3234 /* 3235 * Return a pointer to the part of the sockaddr that contains the 3236 * raw address, and set *nbytes to its length in bytes. Returns 3237 * NULL if the address family is unknown. 3238 */ 3239 static void * 3240 sa_rawaddr(struct sockaddr *sa, int *nbytes) { 3241 void *p; 3242 int len; 3243 3244 switch (sa->sa_family) { 3245 case AF_INET: 3246 len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 3247 p = &((struct sockaddr_in *)sa)->sin_addr; 3248 break; 3249 case AF_INET6: 3250 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 3251 p = &((struct sockaddr_in6 *)sa)->sin6_addr; 3252 break; 3253 default: 3254 p = NULL; 3255 len = 0; 3256 } 3257 3258 if (nbytes != NULL) 3259 *nbytes = len; 3260 return (p); 3261 } 3262 3263 static void 3264 huphandler(int sig __unused) 3265 { 3266 3267 got_sighup = 1; 3268 } 3269 3270 static void 3271 terminate(int sig __unused) 3272 { 3273 pidfile_remove(pfh); 3274 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL); 3275 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL); 3276 exit (0); 3277 } 3278