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