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