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