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