1 /* $NetBSD: rcmd.c,v 1.56 2004/03/21 05:46:42 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 36 #else 37 __RCSID("$NetBSD: rcmd.c,v 1.56 2004/03/21 05:46:42 mrg Exp $"); 38 #endif 39 #endif /* LIBC_SCCS and not lint */ 40 41 #ifdef _LIBC 42 #include "namespace.h" 43 #endif 44 #include <sys/param.h> 45 #include <sys/socket.h> 46 #include <sys/stat.h> 47 #include <sys/poll.h> 48 #include <sys/wait.h> 49 50 #include <netinet/in.h> 51 #include <rpc/rpc.h> 52 #include <arpa/inet.h> 53 #include <netgroup.h> 54 55 #include <assert.h> 56 #include <ctype.h> 57 #include <err.h> 58 #include <errno.h> 59 #include <fcntl.h> 60 #include <grp.h> 61 #include <netdb.h> 62 #include <paths.h> 63 #include <pwd.h> 64 #include <signal.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <syslog.h> 69 #include <unistd.h> 70 71 #include "pathnames.h" 72 73 int orcmd __P((char **, u_int, const char *, const char *, const char *, 74 int *)); 75 int orcmd_af __P((char **, u_int, const char *, const char *, const char *, 76 int *, int)); 77 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 78 int __ivaliduser_sa __P((FILE *, struct sockaddr *, socklen_t, const char *, 79 const char *)); 80 static int rshrcmd __P((char **, u_int32_t, const char *, const char *, 81 const char *, int *, const char *)); 82 static int resrcmd __P((struct addrinfo *, char **, u_int32_t, const char *, 83 const char *, const char *, int *)); 84 static int __icheckhost __P((struct sockaddr *, socklen_t, const char *)); 85 static char *__gethostloop __P((struct sockaddr *, socklen_t)); 86 87 int 88 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 89 char **ahost; 90 u_short rport; 91 const char *locuser, *remuser, *cmd; 92 int *fd2p; 93 { 94 95 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 96 } 97 98 int 99 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 100 char **ahost; 101 u_short rport; 102 const char *locuser, *remuser, *cmd; 103 int *fd2p; 104 int af; 105 { 106 static char hbuf[MAXHOSTNAMELEN]; 107 char pbuf[NI_MAXSERV]; 108 struct addrinfo hints, *res; 109 int error; 110 struct servent *sp; 111 112 _DIAGASSERT(ahost != NULL); 113 _DIAGASSERT(locuser != NULL); 114 _DIAGASSERT(remuser != NULL); 115 _DIAGASSERT(cmd != NULL); 116 /* fd2p may be NULL */ 117 118 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); 119 memset(&hints, 0, sizeof(hints)); 120 hints.ai_family = af; 121 hints.ai_socktype = SOCK_STREAM; 122 hints.ai_flags = AI_CANONNAME; 123 error = getaddrinfo(*ahost, pbuf, &hints, &res); 124 if (error) { 125 warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ 126 return (-1); 127 } 128 if (res->ai_canonname) { 129 /* 130 * Canonicalise hostname. 131 * XXX: Should we really do this? 132 */ 133 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); 134 *ahost = hbuf; 135 } 136 137 /* 138 * Check if rport is the same as the shell port, and that the fd2p. If 139 * it is not, the program isn't expecting 'rsh' and so we can't use the 140 * RCMD_CMD environment. 141 */ 142 sp = getservbyname("shell", "tcp"); 143 if (sp != NULL && sp->s_port == rport) 144 error = rshrcmd(ahost, (u_int32_t)rport, 145 locuser, remuser, cmd, fd2p, getenv("RCMD_CMD")); 146 else 147 error = resrcmd(res, ahost, (u_int32_t)rport, 148 locuser, remuser, cmd, fd2p); 149 freeaddrinfo(res); 150 return (error); 151 } 152 153 /* this is simply a wrapper around hprcmd() that handles ahost first */ 154 int 155 orcmd(ahost, rport, locuser, remuser, cmd, fd2p) 156 char **ahost; 157 u_int rport; 158 const char *locuser, *remuser, *cmd; 159 int *fd2p; 160 { 161 return orcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 162 } 163 164 int 165 orcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 166 char **ahost; 167 u_int rport; 168 const char *locuser, *remuser, *cmd; 169 int *fd2p; 170 int af; 171 { 172 static char hbuf[MAXHOSTNAMELEN]; 173 char pbuf[NI_MAXSERV]; 174 struct addrinfo hints, *res; 175 int error; 176 177 _DIAGASSERT(ahost != NULL); 178 _DIAGASSERT(locuser != NULL); 179 _DIAGASSERT(remuser != NULL); 180 _DIAGASSERT(cmd != NULL); 181 /* fd2p may be NULL */ 182 183 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); 184 memset(&hints, 0, sizeof(hints)); 185 hints.ai_family = af; 186 hints.ai_socktype = SOCK_STREAM; 187 hints.ai_flags = AI_CANONNAME; 188 error = getaddrinfo(*ahost, pbuf, &hints, &res); 189 if (error) { 190 warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ 191 return (-1); 192 } 193 if (res->ai_canonname) { 194 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); 195 *ahost = hbuf; 196 } 197 198 error = resrcmd(res, ahost, rport, locuser, remuser, cmd, fd2p); 199 freeaddrinfo(res); 200 return (error); 201 } 202 203 /*ARGSUSED*/ 204 static int 205 resrcmd(res, ahost, rport, locuser, remuser, cmd, fd2p) 206 struct addrinfo *res; 207 char **ahost; 208 u_int32_t rport; 209 const char *locuser, *remuser, *cmd; 210 int *fd2p; 211 { 212 struct addrinfo *r; 213 struct sockaddr_storage from; 214 struct pollfd reads[2]; 215 sigset_t nmask, omask; 216 pid_t pid; 217 int s, lport, timo; 218 int pollr; 219 char c; 220 int refused; 221 222 _DIAGASSERT(res != NULL); 223 _DIAGASSERT(ahost != NULL); 224 _DIAGASSERT(locuser != NULL); 225 _DIAGASSERT(remuser != NULL); 226 _DIAGASSERT(cmd != NULL); 227 /* fd2p may be NULL */ 228 229 r = res; 230 refused = 0; 231 pid = getpid(); 232 sigemptyset(&nmask); 233 sigaddset(&nmask, SIGURG); 234 if (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1) 235 return -1; 236 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 237 s = rresvport_af(&lport, r->ai_family); 238 if (s < 0) { 239 if (errno == EAGAIN) 240 warnx("rcmd: socket: All ports in use"); 241 else 242 warn("rcmd: socket"); 243 if (r->ai_next) { 244 r = r->ai_next; 245 continue; 246 } else { 247 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 248 return (-1); 249 } 250 } 251 fcntl(s, F_SETOWN, pid); 252 if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) 253 break; 254 (void)close(s); 255 if (errno == EADDRINUSE) { 256 lport--; 257 continue; 258 } else if (errno == ECONNREFUSED) 259 refused++; 260 if (r->ai_next) { 261 int oerrno = errno; 262 char hbuf[NI_MAXHOST]; 263 #ifdef NI_WITHSCOPEID 264 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 265 #else 266 const int niflags = NI_NUMERICHOST; 267 #endif 268 269 hbuf[0] = '\0'; 270 if (getnameinfo(r->ai_addr, r->ai_addrlen, 271 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 272 strlcpy(hbuf, "(invalid)", sizeof(hbuf)); 273 warnx("rcmd: connect to address %s", hbuf); 274 errno = oerrno; 275 perror(0); 276 r = r->ai_next; 277 hbuf[0] = '\0'; 278 if (getnameinfo(r->ai_addr, r->ai_addrlen, 279 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 280 strlcpy(hbuf, "(invalid)", sizeof(hbuf)); 281 (void)fprintf(stderr, "Trying %s...\n", hbuf); 282 continue; 283 } 284 if (refused && timo <= 16) { 285 (void)sleep((unsigned int)timo); 286 timo *= 2; 287 r = res; 288 refused = 0; 289 continue; 290 } 291 (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, 292 strerror(errno)); 293 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 294 return (-1); 295 } 296 lport--; 297 if (fd2p == 0) { 298 write(s, "", 1); 299 lport = 0; 300 } else { 301 char num[8]; 302 int s2 = rresvport_af(&lport, r->ai_family), s3; 303 socklen_t len = sizeof(from); 304 305 if (s2 < 0) 306 goto bad; 307 listen(s2, 1); 308 (void)snprintf(num, sizeof(num), "%d", lport); 309 if (write(s, num, strlen(num) + 1) != 310 (ssize_t) (strlen(num) + 1)) { 311 warn("rcmd: write (setting up stderr)"); 312 (void)close(s2); 313 goto bad; 314 } 315 reads[0].fd = s; 316 reads[0].events = POLLIN; 317 reads[1].fd = s2; 318 reads[1].events = POLLIN; 319 errno = 0; 320 pollr = poll(reads, 2, INFTIM); 321 if (pollr < 1 || (reads[1].revents & POLLIN) == 0) { 322 if (errno != 0) 323 warn("poll: setting up stderr"); 324 else 325 warnx("poll: protocol failure in circuit setup"); 326 (void)close(s2); 327 goto bad; 328 } 329 s3 = accept(s2, (struct sockaddr *)(void *)&from, &len); 330 (void)close(s2); 331 if (s3 < 0) { 332 warn("rcmd: accept"); 333 lport = 0; 334 goto bad; 335 } 336 *fd2p = s3; 337 switch (((struct sockaddr *)(void *)&from)->sa_family) { 338 case AF_INET: 339 #ifdef INET6 340 case AF_INET6: 341 #endif 342 if (getnameinfo((struct sockaddr *)(void *)&from, len, 343 NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 || 344 (atoi(num) >= IPPORT_RESERVED || 345 atoi(num) < IPPORT_RESERVED / 2)) { 346 warnx("rcmd: protocol failure in circuit setup."); 347 goto bad2; 348 } 349 break; 350 default: 351 break; 352 } 353 } 354 355 (void)write(s, locuser, strlen(locuser)+1); 356 (void)write(s, remuser, strlen(remuser)+1); 357 (void)write(s, cmd, strlen(cmd)+1); 358 if (read(s, &c, 1) != 1) { 359 warn("%s", *ahost); 360 goto bad2; 361 } 362 if (c != 0) { 363 while (read(s, &c, 1) == 1) { 364 (void)write(STDERR_FILENO, &c, 1); 365 if (c == '\n') 366 break; 367 } 368 goto bad2; 369 } 370 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 371 return (s); 372 bad2: 373 if (lport) 374 (void)close(*fd2p); 375 bad: 376 (void)close(s); 377 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 378 return (-1); 379 } 380 381 /* 382 * based on code written by Chris Siebenmann <cks@utcc.utoronto.ca> 383 */ 384 /* ARGSUSED */ 385 static int 386 rshrcmd(ahost, rport, locuser, remuser, cmd, fd2p, rshcmd) 387 char **ahost; 388 u_int32_t rport; 389 const char *locuser, *remuser, *cmd; 390 int *fd2p; 391 const char *rshcmd; 392 { 393 pid_t pid; 394 int sp[2], ep[2]; 395 char *p; 396 struct passwd *pw; 397 398 _DIAGASSERT(ahost != NULL); 399 _DIAGASSERT(locuser != NULL); 400 _DIAGASSERT(remuser != NULL); 401 _DIAGASSERT(cmd != NULL); 402 /* fd2p may be NULL */ 403 404 /* What rsh/shell to use. */ 405 if (rshcmd == NULL) 406 rshcmd = _PATH_BIN_RCMD; 407 408 /* locuser must exist on this host. */ 409 if ((pw = getpwnam(locuser)) == NULL) { 410 warnx("rshrcmd: unknown user: %s", locuser); 411 return(-1); 412 } 413 414 /* get a socketpair we'll use for stdin and stdout. */ 415 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sp) < 0) { 416 warn("rshrcmd: socketpair"); 417 return (-1); 418 } 419 /* we will use this for the fd2 pointer */ 420 if (fd2p) { 421 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, ep) < 0) { 422 warn("rshrcmd: socketpair"); 423 return (-1); 424 } 425 *fd2p = ep[0]; 426 } 427 428 pid = fork(); 429 if (pid < 0) { 430 warn("rshrcmd: fork"); 431 return (-1); 432 } 433 if (pid == 0) { 434 /* 435 * child 436 * - we use sp[1] to be stdin/stdout, and close sp[0] 437 * - with fd2p, we use ep[1] for stderr, and close ep[0] 438 */ 439 (void)close(sp[0]); 440 if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) { 441 warn("rshrcmd: dup2"); 442 _exit(1); 443 } 444 if (fd2p) { 445 if (dup2(ep[1], 2) < 0) { 446 warn("rshrcmd: dup2"); 447 _exit(1); 448 } 449 (void)close(ep[0]); 450 (void)close(ep[1]); 451 } else if (dup2(0, 2) < 0) { 452 warn("rshrcmd: dup2"); 453 _exit(1); 454 } 455 /* fork again to lose parent. */ 456 pid = fork(); 457 if (pid < 0) { 458 warn("rshrcmd: second fork"); 459 _exit(1); 460 } 461 if (pid > 0) 462 _exit(0); 463 464 /* Orphan. Become local user for rshprog. */ 465 if (setuid(pw->pw_uid)) { 466 warn("rshrcmd: setuid(%lu)", (u_long)pw->pw_uid); 467 _exit(1); 468 } 469 470 /* 471 * If we are rcmd'ing to "localhost" as the same user as we are, 472 * then avoid running remote shell for efficiency. 473 */ 474 if (strcmp(*ahost, "localhost") == 0 && 475 strcmp(locuser, remuser) == 0) { 476 if (pw->pw_shell[0] == '\0') 477 rshcmd = _PATH_BSHELL; 478 else 479 rshcmd = pw->pw_shell; 480 p = strrchr(rshcmd, '/'); 481 execlp(rshcmd, p ? p + 1 : rshcmd, "-c", cmd, NULL); 482 } else { 483 p = strrchr(rshcmd, '/'); 484 execlp(rshcmd, p ? p + 1 : rshcmd, *ahost, "-l", 485 remuser, cmd, NULL); 486 } 487 warn("rshrcmd: exec %s", rshcmd); 488 _exit(1); 489 } 490 /* Parent */ 491 (void)close(sp[1]); 492 if (fd2p) 493 (void)close(ep[1]); 494 495 (void)waitpid(pid, NULL, 0); 496 return (sp[0]); 497 } 498 499 int 500 rresvport(alport) 501 int *alport; 502 { 503 504 _DIAGASSERT(alport != NULL); 505 506 return rresvport_af(alport, AF_INET); 507 } 508 509 int 510 rresvport_af(alport, family) 511 int *alport; 512 int family; 513 { 514 struct sockaddr_storage ss; 515 struct sockaddr *sa; 516 int salen; 517 int s; 518 u_int16_t *portp; 519 520 _DIAGASSERT(alport != NULL); 521 522 memset(&ss, 0, sizeof(ss)); 523 sa = (struct sockaddr *)(void *)&ss; 524 switch (family) { 525 case AF_INET: 526 #ifdef BSD4_4 527 sa->sa_len = 528 #endif 529 salen = sizeof(struct sockaddr_in); 530 portp = &((struct sockaddr_in *)(void *)sa)->sin_port; 531 break; 532 #ifdef INET6 533 case AF_INET6: 534 #ifdef BSD4_4 535 sa->sa_len = 536 #endif 537 salen = sizeof(struct sockaddr_in6); 538 portp = &((struct sockaddr_in6 *)(void *)sa)->sin6_port; 539 break; 540 #endif 541 default: 542 portp = NULL; 543 return EAFNOSUPPORT; 544 } 545 sa->sa_family = family; 546 s = socket(family, SOCK_STREAM, 0); 547 if (s < 0) 548 return (-1); 549 #ifdef BSD4_4 550 switch (family) { 551 case AF_INET: 552 case AF_INET6: 553 *portp = 0; 554 if (bindresvport(s, (struct sockaddr_in *)(void *)sa) < 0) { 555 int sverr = errno; 556 557 (void)close(s); 558 errno = sverr; 559 return (-1); 560 } 561 *alport = (int)ntohs(*portp); 562 return (s); 563 default: 564 /* is it necessary to try keep code for other AFs? */ 565 break; 566 } 567 #endif 568 for (;;) { 569 *portp = htons((u_short)*alport); 570 if (bind(s, sa, (socklen_t)salen) >= 0) 571 return (s); 572 if (errno != EADDRINUSE) { 573 (void)close(s); 574 return (-1); 575 } 576 (*alport)--; 577 if (*alport == IPPORT_RESERVED/2) { 578 (void)close(s); 579 errno = EAGAIN; /* close */ 580 return (-1); 581 } 582 } 583 } 584 585 int __check_rhosts_file = 1; 586 char *__rcmd_errstr; 587 588 int 589 ruserok(rhost, superuser, ruser, luser) 590 const char *rhost, *ruser, *luser; 591 int superuser; 592 { 593 struct addrinfo hints, *res, *r; 594 int error; 595 596 _DIAGASSERT(rhost != NULL); 597 _DIAGASSERT(ruser != NULL); 598 _DIAGASSERT(luser != NULL); 599 600 memset(&hints, 0, sizeof(hints)); 601 hints.ai_family = PF_UNSPEC; 602 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 603 error = getaddrinfo(rhost, "0", &hints, &res); 604 if (error) 605 return (-1); 606 607 for (r = res; r; r = r->ai_next) { 608 if (iruserok_sa(r->ai_addr, (int)r->ai_addrlen, superuser, 609 ruser, luser) == 0) { 610 freeaddrinfo(res); 611 return (0); 612 } 613 } 614 freeaddrinfo(res); 615 return (-1); 616 } 617 618 /* 619 * New .rhosts strategy: We are passed an ip address. We spin through 620 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 621 * has ip addresses, we don't have to trust a nameserver. When it 622 * contains hostnames, we spin through the list of addresses the nameserver 623 * gives us and look for a match. 624 * 625 * Returns 0 if ok, -1 if not ok. 626 */ 627 int 628 iruserok(raddr, superuser, ruser, luser) 629 u_int32_t raddr; 630 int superuser; 631 const char *ruser, *luser; 632 { 633 struct sockaddr_in irsin; 634 635 memset(&irsin, 0, sizeof(irsin)); 636 irsin.sin_family = AF_INET; 637 #ifdef BSD4_4 638 irsin.sin_len = sizeof(struct sockaddr_in); 639 #endif 640 memcpy(&irsin.sin_addr, &raddr, sizeof(irsin.sin_addr)); 641 return iruserok_sa(&irsin, sizeof(struct sockaddr_in), superuser, ruser, 642 luser); 643 } 644 645 /* 646 * 2nd and 3rd arguments are typed like this, to avoid dependency between 647 * unistd.h and sys/socket.h. There's no better way. 648 */ 649 int 650 iruserok_sa(raddr, rlen, superuser, ruser, luser) 651 const void *raddr; 652 int rlen; 653 int superuser; 654 const char *ruser, *luser; 655 { 656 struct sockaddr *sa; 657 struct stat sbuf; 658 struct passwd *pwd; 659 FILE *hostf; 660 uid_t uid; 661 gid_t gid; 662 int isvaliduser; 663 char pbuf[MAXPATHLEN]; 664 665 _DIAGASSERT(raddr != NULL); 666 _DIAGASSERT(ruser != NULL); 667 _DIAGASSERT(luser != NULL); 668 669 /*LINTED const castaway*/ 670 sa = (struct sockaddr *)(void *)raddr; 671 672 __rcmd_errstr = NULL; 673 674 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 675 676 if (hostf) { 677 if (__ivaliduser_sa(hostf, sa, (socklen_t)rlen, luser, 678 ruser) == 0) { 679 (void)fclose(hostf); 680 return (0); 681 } 682 (void)fclose(hostf); 683 } 684 685 isvaliduser = -1; 686 if (__check_rhosts_file || superuser) { 687 688 if ((pwd = getpwnam(luser)) == NULL) 689 return (-1); 690 (void)strlcpy(pbuf, pwd->pw_dir, sizeof(pbuf)); 691 (void)strlcat(pbuf, "/.rhosts", sizeof(pbuf)); 692 693 /* 694 * Change effective uid while opening and reading .rhosts. 695 * If root and reading an NFS mounted file system, can't 696 * read files that are protected read/write owner only. 697 */ 698 uid = geteuid(); 699 gid = getegid(); 700 (void)setegid(pwd->pw_gid); 701 initgroups(pwd->pw_name, pwd->pw_gid); 702 (void)seteuid(pwd->pw_uid); 703 hostf = fopen(pbuf, "r"); 704 705 if (hostf != NULL) { 706 /* 707 * If not a regular file, or is owned by someone other 708 * than user or root or if writable by anyone but the 709 * owner, quit. 710 */ 711 if (lstat(pbuf, &sbuf) < 0) 712 __rcmd_errstr = ".rhosts lstat failed"; 713 else if (!S_ISREG(sbuf.st_mode)) 714 __rcmd_errstr = ".rhosts not regular file"; 715 else if (fstat(fileno(hostf), &sbuf) < 0) 716 __rcmd_errstr = ".rhosts fstat failed"; 717 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 718 __rcmd_errstr = "bad .rhosts owner"; 719 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 720 __rcmd_errstr = 721 ".rhosts writable by other than owner"; 722 else 723 isvaliduser = 724 __ivaliduser_sa(hostf, sa, (socklen_t)rlen, 725 luser, ruser); 726 727 (void)fclose(hostf); 728 } 729 (void)seteuid(uid); 730 (void)setegid(gid); 731 732 } 733 return (isvaliduser); 734 } 735 736 /* 737 * XXX 738 * Don't make static, used by lpd(8). We will be able to change the function 739 * into static function, when we bump libc major #. 740 * 741 * Returns 0 if ok, -1 if not ok. 742 */ 743 #ifdef notdef /*_LIBC*/ 744 static 745 #endif 746 int 747 __ivaliduser(hostf, raddr, luser, ruser) 748 FILE *hostf; 749 u_int32_t raddr; 750 const char *luser, *ruser; 751 { 752 struct sockaddr_in ivusin; 753 754 memset(&ivusin, 0, sizeof(ivusin)); 755 ivusin.sin_family = AF_INET; 756 #ifdef BSD4_4 757 ivusin.sin_len = sizeof(struct sockaddr_in); 758 #endif 759 memcpy(&ivusin.sin_addr, &raddr, sizeof(ivusin.sin_addr)); 760 return __ivaliduser_sa(hostf, (struct sockaddr *)(void *)&ivusin, 761 sizeof(struct sockaddr_in), luser, ruser); 762 } 763 764 #ifdef notdef /*_LIBC*/ 765 static 766 #endif 767 int 768 __ivaliduser_sa(hostf, raddr, salen, luser, ruser) 769 FILE *hostf; 770 struct sockaddr *raddr; 771 socklen_t salen; 772 const char *luser, *ruser; 773 { 774 register char *user, *p; 775 int ch; 776 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 777 const char *auser, *ahost; 778 int hostok, userok; 779 char *rhost = NULL; 780 int firsttime = 1; 781 char domain[MAXHOSTNAMELEN]; 782 783 getdomainname(domain, sizeof(domain)); 784 785 _DIAGASSERT(hostf != NULL); 786 _DIAGASSERT(luser != NULL); 787 _DIAGASSERT(ruser != NULL); 788 789 while (fgets(buf, sizeof(buf), hostf)) { 790 p = buf; 791 /* Skip lines that are too long. */ 792 if (strchr(p, '\n') == NULL) { 793 while ((ch = getc(hostf)) != '\n' && ch != EOF) 794 ; 795 continue; 796 } 797 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 798 *p = isupper((unsigned char)*p) ? 799 tolower((unsigned char)*p) : *p; 800 p++; 801 } 802 if (*p == ' ' || *p == '\t') { 803 *p++ = '\0'; 804 while (*p == ' ' || *p == '\t') 805 p++; 806 user = p; 807 while (*p != '\n' && *p != ' ' && 808 *p != '\t' && *p != '\0') 809 p++; 810 } else 811 user = p; 812 *p = '\0'; 813 814 if (p == buf) 815 continue; 816 817 auser = *user ? user : luser; 818 ahost = buf; 819 820 if (ahost[0] == '+') 821 switch (ahost[1]) { 822 case '\0': 823 hostok = 1; 824 break; 825 826 case '@': 827 if (firsttime) { 828 rhost = __gethostloop(raddr, salen); 829 firsttime = 0; 830 } 831 if (rhost) 832 hostok = innetgr(&ahost[2], rhost, 833 NULL, domain); 834 else 835 hostok = 0; 836 break; 837 838 default: 839 hostok = __icheckhost(raddr, salen, &ahost[1]); 840 break; 841 } 842 else if (ahost[0] == '-') 843 switch (ahost[1]) { 844 case '\0': 845 hostok = -1; 846 break; 847 848 case '@': 849 if (firsttime) { 850 rhost = __gethostloop(raddr, salen); 851 firsttime = 0; 852 } 853 if (rhost) 854 hostok = -innetgr(&ahost[2], rhost, 855 NULL, domain); 856 else 857 hostok = 0; 858 break; 859 860 default: 861 hostok = -__icheckhost(raddr, salen, &ahost[1]); 862 break; 863 } 864 else 865 hostok = __icheckhost(raddr, salen, ahost); 866 867 868 if (auser[0] == '+') 869 switch (auser[1]) { 870 case '\0': 871 userok = 1; 872 break; 873 874 case '@': 875 userok = innetgr(&auser[2], NULL, ruser, 876 domain); 877 break; 878 879 default: 880 userok = strcmp(ruser, &auser[1]) == 0; 881 break; 882 } 883 else if (auser[0] == '-') 884 switch (auser[1]) { 885 case '\0': 886 userok = -1; 887 break; 888 889 case '@': 890 userok = -innetgr(&auser[2], NULL, ruser, 891 domain); 892 break; 893 894 default: 895 userok = 896 -(strcmp(ruser, &auser[1]) == 0 ? 1 : 0); 897 break; 898 } 899 else 900 userok = strcmp(ruser, auser) == 0; 901 902 /* Check if one component did not match */ 903 if (hostok == 0 || userok == 0) 904 continue; 905 906 /* Check if we got a forbidden pair */ 907 if (userok == -1 || hostok == -1) 908 return -1; 909 910 /* Check if we got a valid pair */ 911 if (hostok == 1 && userok == 1) 912 return 0; 913 } 914 return -1; 915 } 916 917 /* 918 * Returns "true" if match, 0 if no match. 919 * 920 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 921 * if af == AF_INET6. 922 */ 923 static int 924 __icheckhost(raddr, salen, lhost) 925 struct sockaddr *raddr; 926 socklen_t salen; 927 const char *lhost; 928 { 929 struct addrinfo hints, *res, *r; 930 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 931 int error; 932 #ifdef NI_WITHSCOPEID 933 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 934 #else 935 const int niflags = NI_NUMERICHOST; 936 #endif 937 938 _DIAGASSERT(raddr != NULL); 939 _DIAGASSERT(lhost != NULL); 940 941 h1[0] = '\0'; 942 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 943 niflags) != 0) 944 return (0); 945 946 /* Resolve laddr into sockaddr */ 947 memset(&hints, 0, sizeof(hints)); 948 hints.ai_family = raddr->sa_family; 949 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 950 res = NULL; 951 error = getaddrinfo(lhost, "0", &hints, &res); 952 if (error) 953 return (0); 954 955 /* 956 * Try string comparisons between raddr and laddr. 957 */ 958 for (r = res; r; r = r->ai_next) { 959 h2[0] = '\0'; 960 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 961 NULL, 0, niflags) != 0) 962 continue; 963 if (strcmp(h1, h2) == 0) { 964 freeaddrinfo(res); 965 return (1); 966 } 967 } 968 969 /* No match. */ 970 freeaddrinfo(res); 971 return (0); 972 } 973 974 /* 975 * Return the hostname associated with the supplied address. 976 * Do a reverse lookup as well for security. If a loop cannot 977 * be found, pack the numeric IP address into the string. 978 * 979 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 980 * if af == AF_INET6. 981 */ 982 static char * 983 __gethostloop(raddr, salen) 984 struct sockaddr *raddr; 985 socklen_t salen; 986 { 987 static char remotehost[NI_MAXHOST]; 988 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 989 struct addrinfo hints, *res, *r; 990 int error; 991 #ifdef NI_WITHSCOPEID 992 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 993 #else 994 const int niflags = NI_NUMERICHOST; 995 #endif 996 997 _DIAGASSERT(raddr != NULL); 998 999 h1[0] = remotehost[0] = '\0'; 1000 if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), 1001 NULL, 0, NI_NAMEREQD) != 0) 1002 return (NULL); 1003 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 1004 niflags) != 0) 1005 return (NULL); 1006 1007 /* 1008 * Look up the name and check that the supplied 1009 * address is in the list 1010 */ 1011 memset(&hints, 0, sizeof(hints)); 1012 hints.ai_family = raddr->sa_family; 1013 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 1014 hints.ai_flags = AI_CANONNAME; 1015 res = NULL; 1016 error = getaddrinfo(remotehost, "0", &hints, &res); 1017 if (error) 1018 return (NULL); 1019 1020 for (r = res; r; r = r->ai_next) { 1021 h2[0] = '\0'; 1022 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 1023 NULL, 0, niflags) != 0) 1024 continue; 1025 if (strcmp(h1, h2) == 0) { 1026 freeaddrinfo(res); 1027 return (remotehost); 1028 } 1029 } 1030 1031 /* 1032 * either the DNS adminstrator has made a configuration 1033 * mistake, or someone has attempted to spoof us 1034 */ 1035 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 1036 h1, res->ai_canonname ? res->ai_canonname : remotehost); 1037 freeaddrinfo(res); 1038 return (NULL); 1039 } 1040