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