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