1 /* $OpenBSD: mountd.c,v 1.41 2001/12/02 02:05:59 deraadt Exp $ */ 2 /* $NetBSD: mountd.c,v 1.31 1996/02/18 11:57:53 fvdl Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Herb Hasler and Rick Macklem at The University of Guelph. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #ifndef lint 41 static char copyright[] = 42 "@(#) Copyright (c) 1989, 1993\n\ 43 The Regents of the University of California. All rights reserved.\n"; 44 #endif /* not lint */ 45 46 #ifndef lint 47 #if 0 48 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95"; 49 #else 50 static char rcsid[] = "$NetBSD: mountd.c,v 1.31 1996/02/18 11:57:53 fvdl Exp $"; 51 #endif 52 #endif /* not lint */ 53 54 #include <sys/param.h> 55 #include <sys/file.h> 56 #include <sys/ioctl.h> 57 #include <sys/mount.h> 58 #include <sys/socket.h> 59 #include <sys/stat.h> 60 #include <syslog.h> 61 #include <sys/ucred.h> 62 63 #include <rpc/rpc.h> 64 #include <rpc/pmap_clnt.h> 65 #include <rpc/pmap_prot.h> 66 #include <nfs/rpcv2.h> 67 #include <nfs/nfsproto.h> 68 69 #include <arpa/inet.h> 70 71 #include <ctype.h> 72 #include <errno.h> 73 #include <grp.h> 74 #include <netdb.h> 75 #include <netgroup.h> 76 #include <pwd.h> 77 #include <signal.h> 78 #include <stdio.h> 79 #include <stdlib.h> 80 #include <string.h> 81 #include <unistd.h> 82 #include "pathnames.h" 83 84 #include <stdarg.h> 85 86 /* 87 * Structures for keeping the mount list and export list 88 */ 89 struct mountlist { 90 struct mountlist *ml_next; 91 char ml_host[RPCMNT_NAMELEN+1]; 92 char ml_dirp[RPCMNT_PATHLEN+1]; 93 }; 94 95 struct dirlist { 96 struct dirlist *dp_left; 97 struct dirlist *dp_right; 98 int dp_flag; 99 struct hostlist *dp_hosts; /* List of hosts this dir exported to */ 100 char dp_dirp[1]; /* Actually malloc'd to size of dir */ 101 }; 102 /* dp_flag bits */ 103 #define DP_DEFSET 0x1 104 #define DP_HOSTSET 0x2 105 106 struct exportlist { 107 struct exportlist *ex_next; 108 struct dirlist *ex_dirl; 109 struct dirlist *ex_defdir; 110 int ex_flag; 111 fsid_t ex_fs; 112 char *ex_fsdir; 113 }; 114 /* ex_flag bits */ 115 #define EX_LINKED 0x1 116 117 struct netmsk { 118 in_addr_t nt_net; 119 in_addr_t nt_mask; 120 char *nt_name; 121 }; 122 123 union grouptypes { 124 struct hostent *gt_hostent; 125 struct netmsk gt_net; 126 }; 127 128 struct grouplist { 129 int gr_type; 130 union grouptypes gr_ptr; 131 struct grouplist *gr_next; 132 }; 133 /* Group types */ 134 #define GT_NULL 0x0 135 #define GT_HOST 0x1 136 #define GT_NET 0x2 137 #define GT_IGNORE 0x5 138 139 struct hostlist { 140 int ht_flag; /* Uses DP_xx bits */ 141 struct grouplist *ht_grp; 142 struct hostlist *ht_next; 143 }; 144 145 struct fhreturn { 146 int fhr_flag; 147 int fhr_vers; 148 nfsfh_t fhr_fh; 149 }; 150 151 /* Global defs */ 152 char *add_expdir __P((struct dirlist **, char *, int)); 153 void add_dlist __P((struct dirlist **, struct dirlist *, 154 struct grouplist *, int)); 155 void add_mlist __P((char *, char *)); 156 int check_dirpath __P((char *)); 157 int check_options __P((struct dirlist *)); 158 int chk_host __P((struct dirlist *, in_addr_t, int *, int *)); 159 void del_mlist __P((char *, char *)); 160 struct dirlist *dirp_search __P((struct dirlist *, char *)); 161 int do_mount __P((struct exportlist *, struct grouplist *, int, 162 struct ucred *, char *, int, struct statfs *)); 163 int do_opt __P((char **, char **, struct exportlist *, struct grouplist *, 164 int *, int *, struct ucred *)); 165 struct exportlist *ex_search __P((fsid_t *)); 166 struct exportlist *get_exp __P((void)); 167 void free_dir __P((struct dirlist *)); 168 void free_exp __P((struct exportlist *)); 169 void free_grp __P((struct grouplist *)); 170 void free_host __P((struct hostlist *)); 171 void new_exportlist __P((void)); 172 void get_exportlist __P((void)); 173 int get_host __P((char *, struct grouplist *, struct grouplist *)); 174 int get_num __P((char *)); 175 struct hostlist *get_ht __P((void)); 176 int get_line __P((void)); 177 void get_mountlist __P((void)); 178 int get_net __P((char *, struct netmsk *, int)); 179 void getexp_err __P((struct exportlist *, struct grouplist *)); 180 struct grouplist *get_grp __P((void)); 181 void hang_dirp __P((struct dirlist *, struct grouplist *, 182 struct exportlist *, int)); 183 void mntsrv __P((struct svc_req *, SVCXPRT *)); 184 void nextfield __P((char **, char **)); 185 void out_of_mem __P((void)); 186 void parsecred __P((char *, struct ucred *)); 187 int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *)); 188 int scan_tree __P((struct dirlist *, in_addr_t)); 189 void send_umntall __P((void)); 190 int umntall_each __P((caddr_t, struct sockaddr_in *)); 191 int xdr_dir __P((XDR *, char *)); 192 int xdr_explist __P((XDR *, caddr_t)); 193 int xdr_fhs __P((XDR *, caddr_t)); 194 int xdr_mlist __P((XDR *, caddr_t)); 195 void mountd_svc_run __P((void)); 196 197 struct exportlist *exphead; 198 struct mountlist *mlhead; 199 struct grouplist *grphead; 200 char exname[MAXPATHLEN]; 201 struct ucred def_anon = { 202 1, 203 (uid_t) -2, 204 (gid_t) -2, 205 0, 206 { } 207 }; 208 int resvport_only = 1; 209 int opt_flags; 210 /* Bits for above */ 211 #define OP_MAPROOT 0x01 212 #define OP_MAPALL 0x02 213 #define OP_MASK 0x08 214 #define OP_NET 0x10 215 #define OP_ALLDIRS 0x40 216 217 int debug = 0; 218 219 volatile sig_atomic_t gothup; 220 volatile sig_atomic_t gotterm; 221 222 /* 223 * Mountd server for NFS mount protocol as described in: 224 * NFS: Network File System Protocol Specification, RFC1094, Appendix A 225 * The optional arguments are the exports file name 226 * default: _PATH_EXPORTS 227 * "-d" to enable debugging 228 * and "-n" to allow nonroot mount. 229 */ 230 int 231 main(argc, argv) 232 int argc; 233 char **argv; 234 { 235 SVCXPRT *udptransp, *tcptransp; 236 FILE *pidfile; 237 int c; 238 239 while ((c = getopt(argc, argv, "dnr")) != -1) 240 switch (c) { 241 case 'd': 242 debug = 1; 243 break; 244 case 'n': 245 resvport_only = 0; 246 break; 247 case 'r': 248 /* Compatibility */ 249 break; 250 default: 251 fprintf(stderr, "Usage: mountd [-dn] [export_file]\n"); 252 exit(1); 253 } 254 argc -= optind; 255 argv += optind; 256 grphead = NULL; 257 exphead = NULL; 258 mlhead = NULL; 259 260 strlcpy(exname, argc == 1? *argv : _PATH_EXPORTS, sizeof(exname)); 261 262 openlog("mountd", LOG_PID, LOG_DAEMON); 263 if (debug) 264 fprintf(stderr, "Getting export list.\n"); 265 get_exportlist(); 266 if (debug) 267 fprintf(stderr, "Getting mount list.\n"); 268 get_mountlist(); 269 if (debug) 270 fprintf(stderr, "Here we go.\n"); 271 if (debug == 0) { 272 daemon(0, 0); 273 signal(SIGINT, SIG_IGN); 274 signal(SIGQUIT, SIG_IGN); 275 } 276 /* Store pid in file unless mountd is already running */ 277 pidfile = fopen(_PATH_MOUNTDPID, "r"); 278 if (pidfile != NULL) { 279 if (fscanf(pidfile, "%d\n", &c) > 0 && c > 0) { 280 if (kill(c, 0) == 0) { 281 syslog(LOG_ERR, "Already running (pid %d)", c); 282 exit(1); 283 } 284 } 285 pidfile = freopen(_PATH_MOUNTDPID, "w", pidfile); 286 } else { 287 pidfile = fopen(_PATH_MOUNTDPID, "w"); 288 } 289 fprintf(pidfile, "%d\n", getpid()); 290 fclose(pidfile); 291 292 signal(SIGHUP, (void (*) __P((int))) new_exportlist); 293 signal(SIGTERM, (void (*) __P((int))) send_umntall); 294 signal(SIGSYS, SIG_IGN); 295 if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL || 296 (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) { 297 syslog(LOG_ERR, "Can't create socket"); 298 exit(1); 299 } 300 pmap_unset(RPCPROG_MNT, RPCMNT_VER1); 301 pmap_unset(RPCPROG_MNT, RPCMNT_VER3); 302 if (!svc_register(udptransp, RPCPROG_MNT, RPCMNT_VER1, mntsrv, IPPROTO_UDP) || 303 !svc_register(udptransp, RPCPROG_MNT, RPCMNT_VER3, mntsrv, IPPROTO_UDP) || 304 !svc_register(tcptransp, RPCPROG_MNT, RPCMNT_VER1, mntsrv, IPPROTO_TCP) || 305 !svc_register(tcptransp, RPCPROG_MNT, RPCMNT_VER3, mntsrv, IPPROTO_TCP)) { 306 syslog(LOG_ERR, "Can't register mount"); 307 exit(1); 308 } 309 mountd_svc_run(); 310 syslog(LOG_ERR, "Mountd died"); 311 exit(1); 312 } 313 314 void 315 mountd_svc_run() 316 { 317 fd_set *fds = NULL; 318 int fds_size = 0; 319 extern fd_set *__svc_fdset; 320 extern int __svc_fdsetsize; 321 322 for (;;) { 323 if (__svc_fdset) { 324 int bytes = howmany(__svc_fdsetsize, NFDBITS) * 325 sizeof(fd_mask); 326 if (fds_size != __svc_fdsetsize) { 327 if (fds) 328 free(fds); 329 fds = (fd_set *)malloc(bytes); /* XXX */ 330 fds_size = __svc_fdsetsize; 331 } 332 memcpy(fds, __svc_fdset, bytes); 333 } else { 334 if (fds) 335 free(fds); 336 fds = NULL; 337 } 338 switch (select(svc_maxfd+1, fds, 0, 0, (struct timeval *)0)) { 339 case -1: 340 if (errno == EINTR) 341 break; 342 perror("mountd_svc_run: - select failed"); 343 if (fds) 344 free(fds); 345 return; 346 case 0: 347 break; 348 default: 349 svc_getreqset2(fds, svc_maxfd+1); 350 break; 351 } 352 if (gothup) { 353 get_exportlist(); 354 gothup = 0; 355 } 356 if (gotterm) { 357 (void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, 358 RPCMNT_UMNTALL, xdr_void, (caddr_t)0, xdr_void, 359 (caddr_t)0, umntall_each); 360 exit(0); 361 } 362 } 363 } 364 365 /* 366 * The mount rpc service 367 */ 368 void 369 mntsrv(rqstp, transp) 370 struct svc_req *rqstp; 371 SVCXPRT *transp; 372 { 373 struct exportlist *ep; 374 struct dirlist *dp; 375 struct fhreturn fhr; 376 struct stat stb; 377 struct statfs fsb; 378 struct hostent *hp; 379 in_addr_t saddr; 380 u_short sport; 381 char rpcpath[RPCMNT_PATHLEN+1], dirpath[MAXPATHLEN]; 382 long bad = 0; 383 int defset, hostset; 384 sigset_t sighup_mask; 385 386 sigemptyset(&sighup_mask); 387 sigaddset(&sighup_mask, SIGHUP); 388 saddr = transp->xp_raddr.sin_addr.s_addr; 389 sport = ntohs(transp->xp_raddr.sin_port); 390 hp = NULL; 391 switch (rqstp->rq_proc) { 392 case NULLPROC: 393 if (!svc_sendreply(transp, xdr_void, NULL)) 394 syslog(LOG_ERR, "Can't send reply"); 395 return; 396 case RPCMNT_MOUNT: 397 if (sport >= IPPORT_RESERVED && resvport_only) { 398 svcerr_weakauth(transp); 399 return; 400 } 401 if (!svc_getargs(transp, xdr_dir, rpcpath)) { 402 svcerr_decode(transp); 403 return; 404 } 405 406 /* 407 * Get the real pathname and make sure it is a file or 408 * directory that exists. 409 */ 410 if (realpath(rpcpath, dirpath) == 0 || 411 stat(dirpath, &stb) < 0 || 412 (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) || 413 statfs(dirpath, &fsb) < 0) { 414 chdir("/"); /* Just in case realpath doesn't */ 415 if (debug) 416 fprintf(stderr, "stat failed on %s\n", dirpath); 417 bad = ENOENT; /* We will send error reply later */ 418 } 419 420 /* Check in the exports list */ 421 sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 422 ep = ex_search(&fsb.f_fsid); 423 hostset = defset = 0; 424 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) || 425 ((dp = dirp_search(ep->ex_dirl, dirpath)) && 426 chk_host(dp, saddr, &defset, &hostset)) || 427 (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 428 scan_tree(ep->ex_dirl, saddr) == 0))) { 429 if (bad) { 430 if (!svc_sendreply(transp, xdr_long, 431 (caddr_t)&bad)) 432 syslog(LOG_ERR, "Can't send reply"); 433 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 434 return; 435 } 436 if (hostset & DP_HOSTSET) 437 fhr.fhr_flag = hostset; 438 else 439 fhr.fhr_flag = defset; 440 fhr.fhr_vers = rqstp->rq_vers; 441 /* Get the file handle */ 442 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 443 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 444 if (errno == ENOSYS) { 445 syslog(LOG_ERR, 446 "Kernel does not support NFS exporting, " 447 "mountd aborting.."); 448 _exit(1); 449 } 450 bad = errno; 451 syslog(LOG_ERR, "Can't get fh for %s", dirpath); 452 if (!svc_sendreply(transp, xdr_long, 453 (caddr_t)&bad)) 454 syslog(LOG_ERR, "Can't send reply"); 455 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 456 return; 457 } 458 if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr)) 459 syslog(LOG_ERR, "Can't send reply"); 460 if (hp == NULL) 461 hp = gethostbyaddr((caddr_t)&saddr, 462 sizeof(saddr), AF_INET); 463 if (hp) 464 add_mlist(hp->h_name, dirpath); 465 else 466 add_mlist(inet_ntoa(transp->xp_raddr.sin_addr), 467 dirpath); 468 if (debug) 469 fprintf(stderr, "Mount successful.\n"); 470 } else 471 bad = EACCES; 472 473 if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad)) 474 syslog(LOG_ERR, "Can't send reply"); 475 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 476 return; 477 case RPCMNT_DUMP: 478 if (!svc_sendreply(transp, xdr_mlist, NULL)) 479 syslog(LOG_ERR, "Can't send reply"); 480 return; 481 case RPCMNT_UMOUNT: 482 if (sport >= IPPORT_RESERVED && resvport_only) { 483 svcerr_weakauth(transp); 484 return; 485 } 486 if (!svc_getargs(transp, xdr_dir, dirpath)) { 487 svcerr_decode(transp); 488 return; 489 } 490 if (!svc_sendreply(transp, xdr_void, NULL)) 491 syslog(LOG_ERR, "Can't send reply"); 492 hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET); 493 if (hp) 494 del_mlist(hp->h_name, dirpath); 495 del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), dirpath); 496 return; 497 case RPCMNT_UMNTALL: 498 if (sport >= IPPORT_RESERVED && resvport_only) { 499 svcerr_weakauth(transp); 500 return; 501 } 502 if (!svc_sendreply(transp, xdr_void, NULL)) 503 syslog(LOG_ERR, "Can't send reply"); 504 hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET); 505 if (hp) 506 del_mlist(hp->h_name, NULL); 507 del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), NULL); 508 return; 509 case RPCMNT_EXPORT: 510 if (!svc_sendreply(transp, xdr_explist, NULL)) 511 syslog(LOG_ERR, "Can't send reply"); 512 return; 513 default: 514 svcerr_noproc(transp); 515 return; 516 } 517 } 518 519 /* 520 * Xdr conversion for a dirpath string 521 */ 522 int 523 xdr_dir(xdrsp, dirp) 524 XDR *xdrsp; 525 char *dirp; 526 { 527 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 528 } 529 530 /* 531 * Xdr routine to generate file handle reply 532 */ 533 int 534 xdr_fhs(xdrsp, cp) 535 XDR *xdrsp; 536 caddr_t cp; 537 { 538 struct fhreturn *fhrp = (struct fhreturn *)cp; 539 long ok = 0, len, auth; 540 541 if (!xdr_long(xdrsp, &ok)) 542 return (0); 543 switch (fhrp->fhr_vers) { 544 case 1: 545 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 546 case 3: 547 len = NFSX_V3FH; 548 if (!xdr_long(xdrsp, &len)) 549 return (0); 550 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 551 return (0); 552 auth = RPCAUTH_UNIX; 553 len = 1; 554 if (!xdr_long(xdrsp, &len)) 555 return (0); 556 return (xdr_long(xdrsp, &auth)); 557 } 558 return (0); 559 } 560 561 int 562 xdr_mlist(xdrsp, cp) 563 XDR *xdrsp; 564 caddr_t cp; 565 { 566 struct mountlist *mlp; 567 int true = 1; 568 int false = 0; 569 char *strp; 570 571 mlp = mlhead; 572 while (mlp) { 573 if (!xdr_bool(xdrsp, &true)) 574 return (0); 575 strp = &mlp->ml_host[0]; 576 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 577 return (0); 578 strp = &mlp->ml_dirp[0]; 579 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 580 return (0); 581 mlp = mlp->ml_next; 582 } 583 if (!xdr_bool(xdrsp, &false)) 584 return (0); 585 return (1); 586 } 587 588 /* 589 * Xdr conversion for export list 590 */ 591 int 592 xdr_explist(xdrsp, cp) 593 XDR *xdrsp; 594 caddr_t cp; 595 { 596 struct exportlist *ep; 597 int false = 0; 598 int putdef; 599 sigset_t sighup_mask; 600 601 sigemptyset(&sighup_mask); 602 sigaddset(&sighup_mask, SIGHUP); 603 sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 604 ep = exphead; 605 while (ep) { 606 putdef = 0; 607 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef)) 608 goto errout; 609 if (ep->ex_defdir && putdef == 0 && put_exlist(ep->ex_defdir, 610 xdrsp, NULL, &putdef)) 611 goto errout; 612 ep = ep->ex_next; 613 } 614 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 615 if (!xdr_bool(xdrsp, &false)) 616 return (0); 617 return (1); 618 errout: 619 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 620 return (0); 621 } 622 623 /* 624 * Called from xdr_explist() to traverse the tree and export the 625 * directory paths. 626 */ 627 int 628 put_exlist(dp, xdrsp, adp, putdefp) 629 struct dirlist *dp; 630 XDR *xdrsp; 631 struct dirlist *adp; 632 int *putdefp; 633 { 634 struct grouplist *grp; 635 struct hostlist *hp; 636 int true = 1; 637 int false = 0; 638 int gotalldir = 0; 639 char *strp; 640 641 if (dp) { 642 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp)) 643 return (1); 644 if (!xdr_bool(xdrsp, &true)) 645 return (1); 646 strp = dp->dp_dirp; 647 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 648 return (1); 649 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 650 gotalldir = 1; 651 *putdefp = 1; 652 } 653 if ((dp->dp_flag & DP_DEFSET) == 0 && 654 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 655 hp = dp->dp_hosts; 656 while (hp) { 657 grp = hp->ht_grp; 658 if (grp->gr_type == GT_HOST) { 659 if (!xdr_bool(xdrsp, &true)) 660 return (1); 661 strp = grp->gr_ptr.gt_hostent->h_name; 662 if (!xdr_string(xdrsp, &strp, 663 RPCMNT_NAMELEN)) 664 return (1); 665 } else if (grp->gr_type == GT_NET) { 666 if (!xdr_bool(xdrsp, &true)) 667 return (1); 668 strp = grp->gr_ptr.gt_net.nt_name; 669 if (!xdr_string(xdrsp, &strp, 670 RPCMNT_NAMELEN)) 671 return (1); 672 } 673 hp = hp->ht_next; 674 if (gotalldir && hp == NULL) { 675 hp = adp->dp_hosts; 676 gotalldir = 0; 677 } 678 } 679 } 680 if (!xdr_bool(xdrsp, &false)) 681 return (1); 682 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp)) 683 return (1); 684 } 685 return (0); 686 } 687 688 #define LINESIZ 10240 689 char line[LINESIZ]; 690 FILE *exp_file; 691 692 void 693 new_exportlist() 694 { 695 gothup = 1; 696 697 } 698 699 /* 700 * Get the export list 701 */ 702 void 703 get_exportlist() 704 { 705 struct exportlist *ep, *ep2; 706 struct grouplist *grp, *tgrp; 707 struct exportlist **epp; 708 struct dirlist *dirhead; 709 struct statfs fsb, *fsp; 710 struct hostent *hpe; 711 struct ucred anon; 712 char *cp, *endcp, *dirp = NULL, *hst, *usr, *dom, savedc; 713 int len, has_host, exflags, got_nondir, dirplen = 0, num, i, netgrp; 714 int lookup_failed, num_hosts; 715 716 /* 717 * First, get rid of the old list 718 */ 719 ep = exphead; 720 while (ep) { 721 ep2 = ep; 722 ep = ep->ex_next; 723 free_exp(ep2); 724 } 725 exphead = NULL; 726 727 grp = grphead; 728 while (grp) { 729 tgrp = grp; 730 grp = grp->gr_next; 731 free_grp(tgrp); 732 } 733 grphead = NULL; 734 735 /* 736 * And delete exports that are in the kernel for all local 737 * file systems. 738 * XXX: Should know how to handle all local exportable file systems 739 * instead of just MOUNT_FFS. 740 */ 741 num = getmntinfo(&fsp, MNT_NOWAIT); 742 for (i = 0; i < num; i++) { 743 union { 744 struct ufs_args ua; 745 struct iso_args ia; 746 struct mfs_args ma; 747 struct msdosfs_args da; 748 struct adosfs_args aa; 749 } targs; 750 751 if (!strncmp(fsp->f_fstypename, MOUNT_MFS, MFSNAMELEN) || 752 !strncmp(fsp->f_fstypename, MOUNT_FFS, MFSNAMELEN) || 753 !strncmp(fsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN) || 754 !strncmp(fsp->f_fstypename, MOUNT_MSDOS, MFSNAMELEN) || 755 !strncmp(fsp->f_fstypename, MOUNT_ADOSFS, MFSNAMELEN) || 756 !strncmp(fsp->f_fstypename, MOUNT_CD9660, MFSNAMELEN)) { 757 bzero((char *)&targs, sizeof(targs)); 758 targs.ua.fspec = NULL; 759 targs.ua.export.ex_flags = MNT_DELEXPORT; 760 if (mount(fsp->f_fstypename, fsp->f_mntonname, 761 fsp->f_flags | MNT_UPDATE, &targs) < 0) 762 syslog(LOG_ERR, "Can't delete exports for %s: %m", 763 fsp->f_mntonname); 764 } 765 fsp++; 766 } 767 768 /* 769 * Read in the exports file and build the list, calling 770 * mount() as we go along to push the export rules into the kernel. 771 */ 772 if ((exp_file = fopen(exname, "r")) == NULL) { 773 syslog(LOG_ERR, "Can't open %s", exname); 774 exit(2); 775 } 776 dirhead = NULL; 777 while (get_line()) { 778 if (debug) 779 fprintf(stderr, "Got line %s\n",line); 780 cp = line; 781 nextfield(&cp, &endcp); 782 if (*cp == '#') 783 goto nextline; 784 785 /* 786 * Set defaults. 787 */ 788 has_host = FALSE; 789 num_hosts = 0; 790 lookup_failed = FALSE; 791 anon = def_anon; 792 exflags = MNT_EXPORTED; 793 got_nondir = 0; 794 opt_flags = 0; 795 ep = NULL; 796 797 /* 798 * Create new exports list entry 799 */ 800 len = endcp-cp; 801 tgrp = grp = get_grp(); 802 while (len > 0) { 803 if (len > RPCMNT_NAMELEN) { 804 getexp_err(ep, tgrp); 805 goto nextline; 806 } 807 if (*cp == '-') { 808 if (ep == NULL) { 809 getexp_err(ep, tgrp); 810 goto nextline; 811 } 812 if (debug) 813 fprintf(stderr, "doing opt %s\n", cp); 814 got_nondir = 1; 815 if (do_opt(&cp, &endcp, ep, grp, &has_host, 816 &exflags, &anon)) { 817 getexp_err(ep, tgrp); 818 goto nextline; 819 } 820 } else if (*cp == '/') { 821 savedc = *endcp; 822 *endcp = '\0'; 823 if (check_dirpath(cp) && 824 statfs(cp, &fsb) >= 0) { 825 if (got_nondir) { 826 syslog(LOG_ERR, "Dirs must be first"); 827 getexp_err(ep, tgrp); 828 goto nextline; 829 } 830 if (ep) { 831 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] || 832 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) { 833 getexp_err(ep, tgrp); 834 goto nextline; 835 } 836 } else { 837 /* 838 * See if this directory is already 839 * in the list. 840 */ 841 ep = ex_search(&fsb.f_fsid); 842 if (ep == NULL) { 843 ep = get_exp(); 844 ep->ex_fs = fsb.f_fsid; 845 ep->ex_fsdir = (char *) 846 malloc(strlen(fsb.f_mntonname) + 1); 847 if (ep->ex_fsdir) 848 strcpy(ep->ex_fsdir, 849 fsb.f_mntonname); 850 else 851 out_of_mem(); 852 if (debug) 853 fprintf(stderr, 854 "Making new ep fs=0x%x,0x%x\n", 855 fsb.f_fsid.val[0], 856 fsb.f_fsid.val[1]); 857 } else if (debug) 858 fprintf(stderr, 859 "Found ep fs=0x%x,0x%x\n", 860 fsb.f_fsid.val[0], 861 fsb.f_fsid.val[1]); 862 } 863 864 /* 865 * Add dirpath to export mount point. 866 */ 867 dirp = add_expdir(&dirhead, cp, len); 868 dirplen = len; 869 } else { 870 getexp_err(ep, tgrp); 871 goto nextline; 872 } 873 *endcp = savedc; 874 } else { 875 savedc = *endcp; 876 *endcp = '\0'; 877 got_nondir = 1; 878 if (ep == NULL) { 879 getexp_err(ep, tgrp); 880 goto nextline; 881 } 882 883 /* 884 * Get the host or netgroup. 885 */ 886 setnetgrent(cp); 887 netgrp = getnetgrent((const char **)&hst, 888 (const char **)&usr, (const char **)&dom); 889 do { 890 if (has_host) { 891 grp->gr_next = get_grp(); 892 grp = grp->gr_next; 893 } 894 if (netgrp) { 895 if (hst == NULL) { 896 syslog(LOG_ERR, 897 "NULL hostname in netgroup %s, skipping", 898 cp); 899 grp->gr_type = GT_IGNORE; 900 lookup_failed = TRUE; 901 continue; 902 } else if (get_host(hst, grp, tgrp)) { 903 syslog(LOG_ERR, 904 "Unknown host (%s) in netgroup %s", 905 hst, cp); 906 grp->gr_type = GT_IGNORE; 907 lookup_failed = TRUE; 908 continue; 909 } 910 } else if (get_host(cp, grp, tgrp)) { 911 syslog(LOG_ERR, 912 "Unknown host (%s) in line %s", 913 cp, line); 914 grp->gr_type = GT_IGNORE; 915 lookup_failed = TRUE; 916 continue; 917 } 918 has_host = TRUE; 919 num_hosts++; 920 } while (netgrp && getnetgrent((const char **)&hst, 921 (const char **)&usr, (const char **)&dom)); 922 endnetgrent(); 923 *endcp = savedc; 924 } 925 cp = endcp; 926 nextfield(&cp, &endcp); 927 len = endcp - cp; 928 } 929 /* 930 * If the exports list is empty due to unresolvable hostnames 931 * we throw away the line. 932 */ 933 if (lookup_failed == TRUE && num_hosts == 0 && 934 tgrp->gr_type == GT_IGNORE) { 935 getexp_err(ep, tgrp); 936 goto nextline; 937 } 938 if (check_options(dirhead)) { 939 getexp_err(ep, tgrp); 940 goto nextline; 941 } 942 if (!has_host) { 943 grp->gr_type = GT_HOST; 944 if (debug) 945 fprintf(stderr, "Adding a default entry\n"); 946 /* add a default group and make the grp list NULL */ 947 hpe = (struct hostent *)malloc(sizeof(struct hostent)); 948 if (hpe == NULL) 949 out_of_mem(); 950 hpe->h_name = strdup("Default"); 951 hpe->h_addrtype = AF_INET; 952 hpe->h_length = sizeof (u_int32_t); 953 hpe->h_addr_list = NULL; 954 grp->gr_ptr.gt_hostent = hpe; 955 956 /* 957 * Don't allow a network export coincide with a list of 958 * host(s) on the same line. 959 */ 960 } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 961 getexp_err(ep, tgrp); 962 goto nextline; 963 } 964 965 /* 966 * Loop through hosts, pushing the exports into the kernel. 967 * After loop, tgrp points to the start of the list and 968 * grp points to the last entry in the list. 969 */ 970 grp = tgrp; 971 do { 972 /* 973 * Non-zero return indicates an error. Return 974 * val of 1 means line is invalid (not just entry). 975 */ 976 i = do_mount(ep, grp, exflags, &anon, dirp, dirplen, &fsb); 977 if (i == 1) { 978 getexp_err(ep, tgrp); 979 goto nextline; 980 } else if (i == 2) { 981 syslog(LOG_ERR, 982 "Bad exports list entry (%s) in line %s", 983 (grp->gr_type == GT_HOST) 984 ? grp->gr_ptr.gt_hostent->h_name 985 : (grp->gr_type == GT_NET) 986 ? grp->gr_ptr.gt_net.nt_name 987 : "Unknown", line); 988 } 989 } while (grp->gr_next && (grp = grp->gr_next)); 990 991 /* 992 * Success. Update the data structures. 993 */ 994 if (has_host) { 995 hang_dirp(dirhead, tgrp, ep, opt_flags); 996 grp->gr_next = grphead; 997 grphead = tgrp; 998 } else { 999 hang_dirp(dirhead, NULL, ep, 1000 opt_flags); 1001 free_grp(grp); 1002 } 1003 dirhead = NULL; 1004 if ((ep->ex_flag & EX_LINKED) == 0) { 1005 ep2 = exphead; 1006 epp = &exphead; 1007 1008 /* 1009 * Insert in the list in alphabetical order. 1010 */ 1011 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 1012 epp = &ep2->ex_next; 1013 ep2 = ep2->ex_next; 1014 } 1015 if (ep2) 1016 ep->ex_next = ep2; 1017 *epp = ep; 1018 ep->ex_flag |= EX_LINKED; 1019 } 1020 nextline: 1021 if (dirhead) { 1022 free_dir(dirhead); 1023 dirhead = NULL; 1024 } 1025 } 1026 fclose(exp_file); 1027 } 1028 1029 /* 1030 * Allocate an export list element 1031 */ 1032 struct exportlist * 1033 get_exp() 1034 { 1035 struct exportlist *ep; 1036 1037 ep = (struct exportlist *)malloc(sizeof (struct exportlist)); 1038 if (ep == NULL) 1039 out_of_mem(); 1040 memset(ep, 0, sizeof(struct exportlist)); 1041 return (ep); 1042 } 1043 1044 /* 1045 * Allocate a group list element 1046 */ 1047 struct grouplist * 1048 get_grp() 1049 { 1050 struct grouplist *gp; 1051 1052 gp = (struct grouplist *)malloc(sizeof (struct grouplist)); 1053 if (gp == NULL) 1054 out_of_mem(); 1055 memset(gp, 0, sizeof(struct grouplist)); 1056 return (gp); 1057 } 1058 1059 /* 1060 * Clean up upon an error in get_exportlist(). 1061 */ 1062 void 1063 getexp_err(ep, grp) 1064 struct exportlist *ep; 1065 struct grouplist *grp; 1066 { 1067 struct grouplist *tgrp; 1068 1069 syslog(LOG_ERR, "Bad exports list line %s", line); 1070 if (ep && (ep->ex_flag & EX_LINKED) == 0) 1071 free_exp(ep); 1072 while (grp) { 1073 tgrp = grp; 1074 grp = grp->gr_next; 1075 free_grp(tgrp); 1076 } 1077 } 1078 1079 /* 1080 * Search the export list for a matching fs. 1081 */ 1082 struct exportlist * 1083 ex_search(fsid) 1084 fsid_t *fsid; 1085 { 1086 struct exportlist *ep; 1087 1088 ep = exphead; 1089 while (ep) { 1090 if (ep->ex_fs.val[0] == fsid->val[0] && 1091 ep->ex_fs.val[1] == fsid->val[1]) 1092 return (ep); 1093 ep = ep->ex_next; 1094 } 1095 return (ep); 1096 } 1097 1098 /* 1099 * Add a directory path to the list. 1100 */ 1101 char * 1102 add_expdir(dpp, cp, len) 1103 struct dirlist **dpp; 1104 char *cp; 1105 int len; 1106 { 1107 struct dirlist *dp; 1108 1109 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 1110 dp->dp_left = *dpp; 1111 dp->dp_right = NULL; 1112 dp->dp_flag = 0; 1113 dp->dp_hosts = NULL; 1114 strcpy(dp->dp_dirp, cp); 1115 *dpp = dp; 1116 return (dp->dp_dirp); 1117 } 1118 1119 /* 1120 * Hang the dir list element off the dirpath binary tree as required 1121 * and update the entry for host. 1122 */ 1123 void 1124 hang_dirp(dp, grp, ep, flags) 1125 struct dirlist *dp; 1126 struct grouplist *grp; 1127 struct exportlist *ep; 1128 int flags; 1129 { 1130 struct hostlist *hp; 1131 struct dirlist *dp2; 1132 1133 if (flags & OP_ALLDIRS) { 1134 if (ep->ex_defdir) 1135 free((caddr_t)dp); 1136 else 1137 ep->ex_defdir = dp; 1138 if (grp == NULL) { 1139 ep->ex_defdir->dp_flag |= DP_DEFSET; 1140 } else while (grp) { 1141 hp = get_ht(); 1142 hp->ht_grp = grp; 1143 hp->ht_next = ep->ex_defdir->dp_hosts; 1144 ep->ex_defdir->dp_hosts = hp; 1145 grp = grp->gr_next; 1146 } 1147 } else { 1148 1149 /* 1150 * Loop throught the directories adding them to the tree. 1151 */ 1152 while (dp) { 1153 dp2 = dp->dp_left; 1154 add_dlist(&ep->ex_dirl, dp, grp, flags); 1155 dp = dp2; 1156 } 1157 } 1158 } 1159 1160 /* 1161 * Traverse the binary tree either updating a node that is already there 1162 * for the new directory or adding the new node. 1163 */ 1164 void 1165 add_dlist(dpp, newdp, grp, flags) 1166 struct dirlist **dpp; 1167 struct dirlist *newdp; 1168 struct grouplist *grp; 1169 int flags; 1170 { 1171 struct dirlist *dp; 1172 struct hostlist *hp; 1173 int cmp; 1174 1175 dp = *dpp; 1176 if (dp) { 1177 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 1178 if (cmp > 0) { 1179 add_dlist(&dp->dp_left, newdp, grp, flags); 1180 return; 1181 } else if (cmp < 0) { 1182 add_dlist(&dp->dp_right, newdp, grp, flags); 1183 return; 1184 } else 1185 free((caddr_t)newdp); 1186 } else { 1187 dp = newdp; 1188 dp->dp_left = NULL; 1189 *dpp = dp; 1190 } 1191 if (grp) { 1192 1193 /* 1194 * Hang all of the host(s) off of the directory point. 1195 */ 1196 do { 1197 hp = get_ht(); 1198 hp->ht_grp = grp; 1199 hp->ht_next = dp->dp_hosts; 1200 dp->dp_hosts = hp; 1201 grp = grp->gr_next; 1202 } while (grp); 1203 } else { 1204 dp->dp_flag |= DP_DEFSET; 1205 } 1206 } 1207 1208 /* 1209 * Search for a dirpath on the export point. 1210 */ 1211 struct dirlist * 1212 dirp_search(dp, dirpath) 1213 struct dirlist *dp; 1214 char *dirpath; 1215 { 1216 int cmp; 1217 1218 if (dp) { 1219 cmp = strcmp(dp->dp_dirp, dirpath); 1220 if (cmp > 0) 1221 return (dirp_search(dp->dp_left, dirpath)); 1222 else if (cmp < 0) 1223 return (dirp_search(dp->dp_right, dirpath)); 1224 else 1225 return (dp); 1226 } 1227 return (dp); 1228 } 1229 1230 /* 1231 * Scan for a host match in a directory tree. 1232 */ 1233 int 1234 chk_host(dp, saddr, defsetp, hostsetp) 1235 struct dirlist *dp; 1236 in_addr_t saddr; 1237 int *defsetp; 1238 int *hostsetp; 1239 { 1240 struct hostlist *hp; 1241 struct grouplist *grp; 1242 u_int32_t **addrp; 1243 1244 if (dp) { 1245 if (dp->dp_flag & DP_DEFSET) 1246 *defsetp = dp->dp_flag; 1247 hp = dp->dp_hosts; 1248 while (hp) { 1249 grp = hp->ht_grp; 1250 switch (grp->gr_type) { 1251 case GT_HOST: 1252 addrp = (u_int32_t **) 1253 grp->gr_ptr.gt_hostent->h_addr_list; 1254 while (*addrp) { 1255 if (**addrp == saddr) { 1256 *hostsetp = (hp->ht_flag | DP_HOSTSET); 1257 return (1); 1258 } 1259 addrp++; 1260 } 1261 break; 1262 case GT_NET: 1263 if ((saddr & grp->gr_ptr.gt_net.nt_mask) == 1264 grp->gr_ptr.gt_net.nt_net) { 1265 *hostsetp = (hp->ht_flag | DP_HOSTSET); 1266 return (1); 1267 } 1268 break; 1269 } 1270 hp = hp->ht_next; 1271 } 1272 } 1273 return (0); 1274 } 1275 1276 /* 1277 * Scan tree for a host that matches the address. 1278 */ 1279 int 1280 scan_tree(dp, saddr) 1281 struct dirlist *dp; 1282 in_addr_t saddr; 1283 { 1284 int defset, hostset; 1285 1286 if (dp) { 1287 if (scan_tree(dp->dp_left, saddr)) 1288 return (1); 1289 if (chk_host(dp, saddr, &defset, &hostset)) 1290 return (1); 1291 if (scan_tree(dp->dp_right, saddr)) 1292 return (1); 1293 } 1294 return (0); 1295 } 1296 1297 /* 1298 * Traverse the dirlist tree and free it up. 1299 */ 1300 void 1301 free_dir(dp) 1302 struct dirlist *dp; 1303 { 1304 1305 if (dp) { 1306 free_dir(dp->dp_left); 1307 free_dir(dp->dp_right); 1308 free_host(dp->dp_hosts); 1309 free((caddr_t)dp); 1310 } 1311 } 1312 1313 /* 1314 * Parse the option string and update fields. 1315 * Option arguments may either be -<option>=<value> or 1316 * -<option> <value> 1317 */ 1318 int 1319 do_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr) 1320 char **cpp, **endcpp; 1321 struct exportlist *ep; 1322 struct grouplist *grp; 1323 int *has_hostp; 1324 int *exflagsp; 1325 struct ucred *cr; 1326 { 1327 char *cpoptarg, *cpoptend; 1328 char *cp, *endcp, *cpopt, savedc, savedc2 = 0; 1329 int allflag, usedarg; 1330 1331 cpopt = *cpp; 1332 cpopt++; 1333 cp = *endcpp; 1334 savedc = *cp; 1335 *cp = '\0'; 1336 while (cpopt && *cpopt) { 1337 allflag = 1; 1338 usedarg = -2; 1339 if ((cpoptend = strchr(cpopt, ','))) { 1340 *cpoptend++ = '\0'; 1341 if ((cpoptarg = strchr(cpopt, '='))) 1342 *cpoptarg++ = '\0'; 1343 } else { 1344 if ((cpoptarg = strchr(cpopt, '='))) 1345 *cpoptarg++ = '\0'; 1346 else { 1347 *cp = savedc; 1348 nextfield(&cp, &endcp); 1349 **endcpp = '\0'; 1350 if (endcp > cp && *cp != '-') { 1351 cpoptarg = cp; 1352 savedc2 = *endcp; 1353 *endcp = '\0'; 1354 usedarg = 0; 1355 } 1356 } 1357 } 1358 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 1359 *exflagsp |= MNT_EXRDONLY; 1360 } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 1361 !(allflag = strcmp(cpopt, "mapall")) || 1362 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 1363 usedarg++; 1364 parsecred(cpoptarg, cr); 1365 if (allflag == 0) { 1366 *exflagsp |= MNT_EXPORTANON; 1367 opt_flags |= OP_MAPALL; 1368 } else 1369 opt_flags |= OP_MAPROOT; 1370 } else 1371 if (cpoptarg && (!strcmp(cpopt, "mask") || 1372 !strcmp(cpopt, "m"))) { 1373 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 1374 syslog(LOG_ERR, "Bad mask: %s", cpoptarg); 1375 return (1); 1376 } 1377 usedarg++; 1378 opt_flags |= OP_MASK; 1379 } else if (cpoptarg && (!strcmp(cpopt, "network") || 1380 !strcmp(cpopt, "n"))) { 1381 if (grp->gr_type != GT_NULL) { 1382 syslog(LOG_ERR, "Network/host conflict"); 1383 return (1); 1384 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 1385 syslog(LOG_ERR, "Bad net: %s", cpoptarg); 1386 return (1); 1387 } 1388 grp->gr_type = GT_NET; 1389 *has_hostp = 1; 1390 usedarg++; 1391 opt_flags |= OP_NET; 1392 } else if (!strcmp(cpopt, "alldirs")) { 1393 opt_flags |= OP_ALLDIRS; 1394 } else { 1395 syslog(LOG_ERR, "Bad opt %s", cpopt); 1396 return (1); 1397 } 1398 if (usedarg >= 0) { 1399 *endcp = savedc2; 1400 **endcpp = savedc; 1401 if (usedarg > 0) { 1402 *cpp = cp; 1403 *endcpp = endcp; 1404 } 1405 return (0); 1406 } 1407 cpopt = cpoptend; 1408 } 1409 **endcpp = savedc; 1410 return (0); 1411 } 1412 1413 /* 1414 * Translate a character string to the corresponding list of network 1415 * addresses for a hostname. 1416 */ 1417 int 1418 get_host(cp, grp, tgrp) 1419 char *cp; 1420 struct grouplist *grp; 1421 struct grouplist *tgrp; 1422 { 1423 struct grouplist *checkgrp; 1424 struct hostent *hp, *nhp; 1425 char **addrp, **naddrp; 1426 struct hostent t_host; 1427 int i; 1428 in_addr_t saddr; 1429 char *aptr[2]; 1430 1431 if (grp->gr_type != GT_NULL) 1432 return (1); 1433 if ((hp = gethostbyname(cp)) == NULL) { 1434 if (isdigit(*cp)) { 1435 saddr = inet_addr(cp); 1436 if (saddr == -1) { 1437 syslog(LOG_ERR, "inet_addr failed for %s", cp); 1438 return (1); 1439 } 1440 if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr), 1441 AF_INET)) == NULL) { 1442 hp = &t_host; 1443 hp->h_name = cp; 1444 hp->h_addrtype = AF_INET; 1445 hp->h_length = sizeof (u_int32_t); 1446 hp->h_addr_list = aptr; 1447 aptr[0] = (char *)&saddr; 1448 aptr[1] = NULL; 1449 } 1450 } else { 1451 syslog(LOG_ERR, "gethostbyname; failed for %s: %s", cp, 1452 hstrerror(h_errno)); 1453 return (1); 1454 } 1455 } 1456 1457 /* only insert each host onto the list once */ 1458 for (checkgrp = tgrp; checkgrp; checkgrp = checkgrp->gr_next) { 1459 if (checkgrp->gr_type == GT_HOST && 1460 checkgrp->gr_ptr.gt_hostent != NULL && 1461 !strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)) { 1462 grp->gr_type = GT_IGNORE; 1463 return (0); 1464 } 1465 } 1466 1467 grp->gr_type = GT_HOST; 1468 nhp = grp->gr_ptr.gt_hostent = (struct hostent *) 1469 malloc(sizeof(struct hostent)); 1470 if (nhp == NULL) 1471 out_of_mem(); 1472 memcpy(nhp, hp, sizeof(struct hostent)); 1473 i = strlen(hp->h_name)+1; 1474 nhp->h_name = (char *)malloc(i); 1475 if (nhp->h_name == NULL) 1476 out_of_mem(); 1477 memcpy(nhp->h_name, hp->h_name, i); 1478 addrp = hp->h_addr_list; 1479 i = 1; 1480 while (*addrp++) 1481 i++; 1482 naddrp = nhp->h_addr_list = (char **) 1483 malloc(i*sizeof(char *)); 1484 if (naddrp == NULL) 1485 out_of_mem(); 1486 addrp = hp->h_addr_list; 1487 while (*addrp) { 1488 *naddrp = (char *) 1489 malloc(hp->h_length); 1490 if (*naddrp == NULL) 1491 out_of_mem(); 1492 memcpy(*naddrp, *addrp, hp->h_length); 1493 addrp++; 1494 naddrp++; 1495 } 1496 *naddrp = NULL; 1497 if (debug) 1498 fprintf(stderr, "got host %s\n", hp->h_name); 1499 return (0); 1500 } 1501 1502 /* 1503 * Free up an exports list component 1504 */ 1505 void 1506 free_exp(ep) 1507 struct exportlist *ep; 1508 { 1509 1510 if (ep->ex_defdir) { 1511 free_host(ep->ex_defdir->dp_hosts); 1512 free((caddr_t)ep->ex_defdir); 1513 } 1514 if (ep->ex_fsdir) 1515 free(ep->ex_fsdir); 1516 free_dir(ep->ex_dirl); 1517 free((caddr_t)ep); 1518 } 1519 1520 /* 1521 * Free hosts. 1522 */ 1523 void 1524 free_host(hp) 1525 struct hostlist *hp; 1526 { 1527 struct hostlist *hp2; 1528 1529 while (hp) { 1530 hp2 = hp; 1531 hp = hp->ht_next; 1532 free((caddr_t)hp2); 1533 } 1534 } 1535 1536 struct hostlist * 1537 get_ht() 1538 { 1539 struct hostlist *hp; 1540 1541 hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 1542 if (hp == NULL) 1543 out_of_mem(); 1544 hp->ht_next = NULL; 1545 hp->ht_flag = 0; 1546 return (hp); 1547 } 1548 1549 /* 1550 * Out of memory, fatal 1551 */ 1552 void 1553 out_of_mem() 1554 { 1555 1556 syslog(LOG_ERR, "Out of memory"); 1557 exit(2); 1558 } 1559 1560 /* 1561 * Do the mount syscall with the update flag to push the export info into 1562 * the kernel. Returns 0 on success, 1 for fatal error, and 2 for error 1563 * that only invalidates the specific entry/host. 1564 */ 1565 int 1566 do_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb) 1567 struct exportlist *ep; 1568 struct grouplist *grp; 1569 int exflags; 1570 struct ucred *anoncrp; 1571 char *dirp; 1572 int dirplen; 1573 struct statfs *fsb; 1574 { 1575 char *cp = NULL; 1576 u_int32_t **addrp; 1577 int done; 1578 char savedc = '\0'; 1579 struct sockaddr_in sin, imask; 1580 union { 1581 struct ufs_args ua; 1582 struct iso_args ia; 1583 struct mfs_args ma; 1584 struct msdosfs_args da; 1585 struct adosfs_args aa; 1586 } args; 1587 in_addr_t net; 1588 1589 args.ua.fspec = 0; 1590 args.ua.export.ex_flags = exflags; 1591 args.ua.export.ex_anon = *anoncrp; 1592 memset(&sin, 0, sizeof(sin)); 1593 memset(&imask, 0, sizeof(imask)); 1594 sin.sin_family = AF_INET; 1595 sin.sin_len = sizeof(sin); 1596 imask.sin_family = AF_INET; 1597 imask.sin_len = sizeof(sin); 1598 if (grp->gr_type == GT_HOST) 1599 addrp = (u_int32_t **)grp->gr_ptr.gt_hostent->h_addr_list; 1600 else 1601 addrp = NULL; 1602 1603 done = FALSE; 1604 while (!done) { 1605 switch (grp->gr_type) { 1606 case GT_HOST: 1607 args.ua.export.ex_addr = (struct sockaddr *)&sin; 1608 args.ua.export.ex_masklen = 0; 1609 if (!addrp) { 1610 args.ua.export.ex_addrlen = 0; 1611 break; 1612 } 1613 sin.sin_addr.s_addr = **addrp; 1614 args.ua.export.ex_addrlen = sizeof(sin); 1615 break; 1616 case GT_NET: 1617 sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net; 1618 args.ua.export.ex_addr = (struct sockaddr *)&sin; 1619 args.ua.export.ex_addrlen = sizeof (sin); 1620 args.ua.export.ex_mask = (struct sockaddr *)&imask; 1621 args.ua.export.ex_masklen = sizeof (imask); 1622 if (grp->gr_ptr.gt_net.nt_mask) { 1623 imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask; 1624 break; 1625 } 1626 net = ntohl(grp->gr_ptr.gt_net.nt_net); 1627 if (IN_CLASSA(net)) 1628 imask.sin_addr.s_addr = inet_addr("255.0.0.0"); 1629 else if (IN_CLASSB(net)) 1630 imask.sin_addr.s_addr = inet_addr("255.255.0.0"); 1631 else 1632 imask.sin_addr.s_addr = inet_addr("255.255.255.0"); 1633 grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr; 1634 break; 1635 case GT_IGNORE: 1636 return (0); 1637 default: 1638 syslog(LOG_ERR, "Bad grouptype"); 1639 if (cp) 1640 *cp = savedc; 1641 return (1); 1642 } 1643 1644 /* 1645 * XXX: 1646 * Maybe I should just use the fsb->f_mntonname path instead 1647 * of looping back up the dirp to the mount point?? 1648 * Also, needs to know how to export all types of local 1649 * exportable file systems and not just MOUNT_FFS. 1650 */ 1651 while (mount(fsb->f_fstypename, dirp, 1652 fsb->f_flags | MNT_UPDATE, &args) < 0) { 1653 if (cp) 1654 *cp-- = savedc; 1655 else 1656 cp = dirp + dirplen - 1; 1657 if (errno == EPERM) { 1658 syslog(LOG_ERR, 1659 "Can't change attributes for %s (%s).\n", 1660 dirp, 1661 (grp->gr_type == GT_HOST) 1662 ?grp->gr_ptr.gt_hostent->h_name 1663 :(grp->gr_type == GT_NET) 1664 ?grp->gr_ptr.gt_net.nt_name 1665 :"Unknown"); 1666 return (2); 1667 } 1668 if (opt_flags & OP_ALLDIRS) { 1669 #if 0 1670 syslog(LOG_ERR, "Could not remount %s: %m", 1671 dirp); 1672 return (2); 1673 #endif 1674 } 1675 /* back up over the last component */ 1676 while (*cp == '/' && cp > dirp) 1677 cp--; 1678 while (*(cp - 1) != '/' && cp > dirp) 1679 cp--; 1680 if (cp == dirp) { 1681 if (debug) 1682 fprintf(stderr, "mnt unsucc\n"); 1683 syslog(LOG_ERR, "Can't export %s: %m", dirp); 1684 return (2); 1685 } 1686 savedc = *cp; 1687 *cp = '\0'; 1688 } 1689 if (addrp) { 1690 ++addrp; 1691 if (*addrp == NULL) 1692 done = TRUE; 1693 } else 1694 done = TRUE; 1695 } 1696 if (cp) 1697 *cp = savedc; 1698 return (0); 1699 } 1700 1701 /* 1702 * Translate a net address. 1703 */ 1704 int 1705 get_net(cp, net, maskflg) 1706 char *cp; 1707 struct netmsk *net; 1708 int maskflg; 1709 { 1710 struct netent *np; 1711 in_addr_t netaddr; 1712 struct in_addr inetaddr, inetaddr2; 1713 char *name; 1714 1715 if ((netaddr = inet_network(cp)) != INADDR_NONE) { 1716 inetaddr = inet_makeaddr(netaddr, 0); 1717 /* 1718 * Due to arbitrary subnet masks, you don't know how many 1719 * bits to shift the address to make it into a network, 1720 * however you do know how to make a network address into 1721 * a host with host == 0 and then compare them. 1722 * (What a pest) 1723 */ 1724 if (!maskflg) { 1725 setnetent(0); 1726 while ((np = getnetent())) { 1727 inetaddr2 = inet_makeaddr(np->n_net, 0); 1728 if (inetaddr2.s_addr == inetaddr.s_addr) 1729 break; 1730 } 1731 endnetent(); 1732 } 1733 } else { 1734 if ((np = getnetbyname(cp))) 1735 inetaddr = inet_makeaddr(np->n_net, 0); 1736 else 1737 return (1); 1738 } 1739 if (maskflg) 1740 net->nt_mask = inetaddr.s_addr; 1741 else { 1742 if (np) 1743 name = np->n_name; 1744 else 1745 name = inet_ntoa(inetaddr); 1746 net->nt_name = (char *)malloc(strlen(name) + 1); 1747 if (net->nt_name == NULL) 1748 out_of_mem(); 1749 strcpy(net->nt_name, name); 1750 net->nt_net = inetaddr.s_addr; 1751 } 1752 return (0); 1753 } 1754 1755 /* 1756 * Parse out the next white space separated field 1757 */ 1758 void 1759 nextfield(cp, endcp) 1760 char **cp; 1761 char **endcp; 1762 { 1763 char *p; 1764 1765 p = *cp; 1766 while (*p == ' ' || *p == '\t') 1767 p++; 1768 if (*p == '\n' || *p == '\0') 1769 *cp = *endcp = p; 1770 else { 1771 *cp = p++; 1772 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 1773 p++; 1774 *endcp = p; 1775 } 1776 } 1777 1778 /* 1779 * Get an exports file line. Skip over blank lines and handle line 1780 * continuations. 1781 */ 1782 int 1783 get_line() 1784 { 1785 char *p, *cp; 1786 int len; 1787 int totlen, cont_line; 1788 1789 /* 1790 * Loop around ignoring blank lines and getting all continuation lines. 1791 */ 1792 p = line; 1793 totlen = 0; 1794 do { 1795 if (fgets(p, LINESIZ - totlen, exp_file) == NULL) 1796 return (0); 1797 len = strlen(p); 1798 cp = p + len - 1; 1799 cont_line = 0; 1800 while (cp >= p && (*cp == ' ' || *cp == '\t' || *cp == '\n' || 1801 *cp == '\\')) { 1802 if (*cp == '\\') 1803 cont_line = 1; 1804 cp--; 1805 len--; 1806 } 1807 *++cp = '\0'; 1808 if (len > 0) { 1809 totlen += len; 1810 if (totlen >= LINESIZ) { 1811 syslog(LOG_ERR, "Exports line too long"); 1812 exit(2); 1813 } 1814 p = cp; 1815 } 1816 } while (totlen == 0 || cont_line); 1817 return (1); 1818 } 1819 1820 /* 1821 * Parse a description of a credential. 1822 */ 1823 void 1824 parsecred(namelist, cr) 1825 char *namelist; 1826 struct ucred *cr; 1827 { 1828 char *name; 1829 int cnt; 1830 char *names; 1831 struct passwd *pw; 1832 struct group *gr; 1833 int ngroups, groups[NGROUPS + 1]; 1834 1835 /* 1836 * Set up the unpriviledged user. 1837 */ 1838 cr->cr_ref = 1; 1839 cr->cr_uid = -2; 1840 cr->cr_gid = -2; 1841 cr->cr_ngroups = 0; 1842 /* 1843 * Get the user's password table entry. 1844 */ 1845 names = strsep(&namelist, " \t\n"); 1846 name = strsep(&names, ":"); 1847 if (isdigit(*name) || *name == '-') 1848 pw = getpwuid(atoi(name)); 1849 else 1850 pw = getpwnam(name); 1851 /* 1852 * Credentials specified as those of a user. 1853 */ 1854 if (names == NULL) { 1855 if (pw == NULL) { 1856 syslog(LOG_ERR, "Unknown user: %s", name); 1857 return; 1858 } 1859 cr->cr_uid = pw->pw_uid; 1860 ngroups = NGROUPS + 1; 1861 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 1862 syslog(LOG_ERR, "Too many groups"); 1863 /* 1864 * Convert from int's to gid_t's and compress out duplicate 1865 */ 1866 cr->cr_ngroups = ngroups - 1; 1867 cr->cr_gid = groups[0]; 1868 for (cnt = 1; cnt < ngroups; cnt++) 1869 cr->cr_groups[cnt - 1] = groups[cnt]; 1870 return; 1871 } 1872 /* 1873 * Explicit credential specified as a colon separated list: 1874 * uid:gid:gid:... 1875 */ 1876 if (pw != NULL) 1877 cr->cr_uid = pw->pw_uid; 1878 else if (isdigit(*name) || *name == '-') 1879 cr->cr_uid = atoi(name); 1880 else { 1881 syslog(LOG_ERR, "Unknown user: %s", name); 1882 return; 1883 } 1884 cr->cr_ngroups = 0; 1885 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) { 1886 name = strsep(&names, ":"); 1887 if (isdigit(*name) || *name == '-') { 1888 cr->cr_groups[cr->cr_ngroups++] = atoi(name); 1889 } else { 1890 if ((gr = getgrnam(name)) == NULL) { 1891 syslog(LOG_ERR, "Unknown group: %s", name); 1892 continue; 1893 } 1894 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 1895 } 1896 } 1897 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS) 1898 syslog(LOG_ERR, "Too many groups"); 1899 } 1900 1901 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50) 1902 /* 1903 * Routines that maintain the remote mounttab 1904 */ 1905 void 1906 get_mountlist() 1907 { 1908 struct mountlist *mlp, **mlpp; 1909 char *host, *dirp, *cp; 1910 char str[STRSIZ]; 1911 FILE *mlfile; 1912 1913 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 1914 syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST); 1915 return; 1916 } 1917 mlpp = &mlhead; 1918 while (fgets(str, STRSIZ, mlfile) != NULL) { 1919 cp = str; 1920 host = strsep(&cp, " \t\n"); 1921 dirp = strsep(&cp, " \t\n"); 1922 if (host == NULL || dirp == NULL) 1923 continue; 1924 mlp = (struct mountlist *)malloc(sizeof (*mlp)); 1925 strlcpy(mlp->ml_host, host, sizeof(mlp->ml_host)); 1926 strlcpy(mlp->ml_dirp, dirp, sizeof(mlp->ml_dirp)); 1927 mlp->ml_next = NULL; 1928 *mlpp = mlp; 1929 mlpp = &mlp->ml_next; 1930 } 1931 fclose(mlfile); 1932 } 1933 1934 void 1935 del_mlist(hostp, dirp) 1936 char *hostp, *dirp; 1937 { 1938 struct mountlist *mlp, **mlpp; 1939 struct mountlist *mlp2; 1940 FILE *mlfile; 1941 int fnd = 0; 1942 1943 mlpp = &mlhead; 1944 mlp = mlhead; 1945 while (mlp) { 1946 if (!strcmp(mlp->ml_host, hostp) && 1947 (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 1948 fnd = 1; 1949 mlp2 = mlp; 1950 *mlpp = mlp = mlp->ml_next; 1951 free((caddr_t)mlp2); 1952 } else { 1953 mlpp = &mlp->ml_next; 1954 mlp = mlp->ml_next; 1955 } 1956 } 1957 if (fnd) { 1958 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 1959 syslog(LOG_ERR, "Can't update %s: %m", 1960 _PATH_RMOUNTLIST); 1961 return; 1962 } 1963 mlp = mlhead; 1964 while (mlp) { 1965 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 1966 mlp = mlp->ml_next; 1967 } 1968 fclose(mlfile); 1969 } 1970 } 1971 1972 void 1973 add_mlist(hostp, dirp) 1974 char *hostp, *dirp; 1975 { 1976 struct mountlist *mlp, **mlpp; 1977 FILE *mlfile; 1978 1979 mlpp = &mlhead; 1980 mlp = mlhead; 1981 while (mlp) { 1982 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 1983 return; 1984 mlpp = &mlp->ml_next; 1985 mlp = mlp->ml_next; 1986 } 1987 mlp = (struct mountlist *)malloc(sizeof (*mlp)); 1988 strlcpy(mlp->ml_host, hostp, sizeof(mlp->ml_host)); 1989 strlcpy(mlp->ml_dirp, dirp, sizeof(mlp->ml_dirp)); 1990 mlp->ml_next = NULL; 1991 *mlpp = mlp; 1992 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 1993 syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST); 1994 return; 1995 } 1996 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 1997 fclose(mlfile); 1998 } 1999 2000 /* 2001 * This function is called via. SIGTERM when the system is going down. 2002 * It sends a broadcast RPCMNT_UMNTALL. 2003 */ 2004 void 2005 send_umntall() 2006 { 2007 gotterm = 1; 2008 } 2009 2010 int 2011 umntall_each(resultsp, raddr) 2012 caddr_t resultsp; 2013 struct sockaddr_in *raddr; 2014 { 2015 return (1); 2016 } 2017 2018 /* 2019 * Free up a group list. 2020 */ 2021 void 2022 free_grp(grp) 2023 struct grouplist *grp; 2024 { 2025 char **addrp; 2026 2027 if (grp->gr_type == GT_HOST) { 2028 if (grp->gr_ptr.gt_hostent->h_name) { 2029 addrp = grp->gr_ptr.gt_hostent->h_addr_list; 2030 while (addrp && *addrp) 2031 free(*addrp++); 2032 free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list); 2033 free(grp->gr_ptr.gt_hostent->h_name); 2034 } 2035 free((caddr_t)grp->gr_ptr.gt_hostent); 2036 } else if (grp->gr_type == GT_NET) { 2037 if (grp->gr_ptr.gt_net.nt_name) 2038 free(grp->gr_ptr.gt_net.nt_name); 2039 } 2040 free((caddr_t)grp); 2041 } 2042 2043 /* 2044 * Check options for consistency. 2045 */ 2046 int 2047 check_options(dp) 2048 struct dirlist *dp; 2049 { 2050 2051 if (dp == NULL) 2052 return (1); 2053 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) { 2054 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive"); 2055 return (1); 2056 } 2057 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 2058 syslog(LOG_ERR, "-mask requires -network"); 2059 return (1); 2060 } 2061 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 2062 syslog(LOG_ERR, "-alldirs has multiple directories"); 2063 return (1); 2064 } 2065 return (0); 2066 } 2067 2068 /* 2069 * Check an absolute directory path for any symbolic links. Return true 2070 * if no symbolic links are found. 2071 */ 2072 int 2073 check_dirpath(dirp) 2074 char *dirp; 2075 { 2076 char *cp; 2077 int ret = 1; 2078 struct stat sb; 2079 2080 /* Remove trailing '/' */ 2081 cp = dirp + strlen(dirp) - 1; 2082 while (cp > dirp && *cp == '/') 2083 *cp-- = '\0'; 2084 2085 cp = dirp + 1; 2086 while (*cp && ret) { 2087 if (*cp == '/') { 2088 *cp = '\0'; 2089 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 2090 ret = 0; 2091 *cp = '/'; 2092 } 2093 cp++; 2094 } 2095 if (lstat(dirp, &sb) < 0 || 2096 (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))) 2097 ret = 0; 2098 return (ret); 2099 } 2100