1 /* $NetBSD: rcmd.c,v 1.70 2014/09/18 13:58:20 christos 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.70 2014/09/18 13:58:20 christos 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(char **, u_int, const char *, const char *, const char *, int *); 74 int orcmd_af(char **, u_int, const char *, const char *, const char *, 75 int *, int); 76 int __ivaliduser(FILE *, u_int32_t, const char *, const char *); 77 int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, 78 const char *, const char *); 79 static int rshrcmd(int, char **, u_int32_t, const char *, 80 const char *, const char *, int *, const char *); 81 static int resrcmd(struct addrinfo *, char **, u_int32_t, const char *, 82 const char *, const char *, int *); 83 static int __icheckhost(const struct sockaddr *, socklen_t, 84 const char *); 85 static char *__gethostloop(const struct sockaddr *, socklen_t); 86 87 int 88 rcmd(char **ahost, int rport, const char *locuser, const char *remuser, 89 const char *cmd, int *fd2p) 90 { 91 92 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 93 } 94 95 int 96 rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser, 97 const char *cmd, int *fd2p, int af) 98 { 99 static char hbuf[MAXHOSTNAMELEN]; 100 char pbuf[NI_MAXSERV]; 101 struct addrinfo hints, *res; 102 int error; 103 struct servent *sp; 104 105 _DIAGASSERT(ahost != NULL); 106 _DIAGASSERT(locuser != NULL); 107 _DIAGASSERT(remuser != NULL); 108 _DIAGASSERT(cmd != NULL); 109 /* fd2p may be NULL */ 110 111 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); 112 memset(&hints, 0, sizeof(hints)); 113 hints.ai_family = af; 114 hints.ai_socktype = SOCK_STREAM; 115 hints.ai_flags = AI_CANONNAME; 116 error = getaddrinfo(*ahost, pbuf, &hints, &res); 117 if (error) { 118 warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ 119 return -1; 120 } 121 if (res->ai_canonname) { 122 /* 123 * Canonicalise hostname. 124 * XXX: Should we really do this? 125 */ 126 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); 127 *ahost = hbuf; 128 } 129 130 /* 131 * Check if rport is the same as the shell port, and that the fd2p. If 132 * it is not, the program isn't expecting 'rsh' and so we can't use the 133 * RCMD_CMD environment. 134 */ 135 sp = getservbyname("shell", "tcp"); 136 if (sp != NULL && sp->s_port == rport) 137 error = rshrcmd(af, ahost, (u_int32_t)rport, 138 locuser, remuser, cmd, fd2p, getenv("RCMD_CMD")); 139 else 140 error = resrcmd(res, ahost, (u_int32_t)rport, 141 locuser, remuser, cmd, fd2p); 142 freeaddrinfo(res); 143 return error; 144 } 145 146 /* this is simply a wrapper around hprcmd() that handles ahost first */ 147 int 148 orcmd(char **ahost, u_int rport, const char *locuser, const char *remuser, 149 const char *cmd, int *fd2p) 150 { 151 return orcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 152 } 153 154 int 155 orcmd_af(char **ahost, u_int rport, const char *locuser, const char *remuser, 156 const char *cmd, int *fd2p, int af) 157 { 158 static char hbuf[MAXHOSTNAMELEN]; 159 char pbuf[NI_MAXSERV]; 160 struct addrinfo hints, *res; 161 int error; 162 163 _DIAGASSERT(ahost != NULL); 164 _DIAGASSERT(locuser != NULL); 165 _DIAGASSERT(remuser != NULL); 166 _DIAGASSERT(cmd != NULL); 167 /* fd2p may be NULL */ 168 169 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); 170 memset(&hints, 0, sizeof(hints)); 171 hints.ai_family = af; 172 hints.ai_socktype = SOCK_STREAM; 173 hints.ai_flags = AI_CANONNAME; 174 error = getaddrinfo(*ahost, pbuf, &hints, &res); 175 if (error) { 176 warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ 177 return -1; 178 } 179 if (res->ai_canonname) { 180 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); 181 *ahost = hbuf; 182 } 183 184 error = resrcmd(res, ahost, rport, locuser, remuser, cmd, fd2p); 185 freeaddrinfo(res); 186 return error; 187 } 188 189 /*ARGSUSED*/ 190 static int 191 resrcmd(struct addrinfo *res, char **ahost, u_int32_t rport, 192 const char *locuser, const char *remuser, const char *cmd, int *fd2p) 193 { 194 struct addrinfo *r; 195 struct sockaddr_storage from; 196 struct pollfd reads[2]; 197 sigset_t nmask, omask; 198 pid_t pid; 199 int s, lport, timo; 200 int pollr; 201 char c; 202 int refused; 203 204 _DIAGASSERT(res != NULL); 205 _DIAGASSERT(ahost != NULL); 206 _DIAGASSERT(locuser != NULL); 207 _DIAGASSERT(remuser != NULL); 208 _DIAGASSERT(cmd != NULL); 209 /* fd2p may be NULL */ 210 211 r = res; 212 refused = 0; 213 pid = getpid(); 214 sigemptyset(&nmask); 215 sigaddset(&nmask, SIGURG); 216 if (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1) 217 return -1; 218 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 219 s = rresvport_af(&lport, r->ai_family); 220 if (s < 0) { 221 if (errno == EAGAIN) 222 warnx("rcmd: socket: All ports in use"); 223 else 224 warn("rcmd: socket"); 225 if (r->ai_next) { 226 r = r->ai_next; 227 continue; 228 } else { 229 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 230 return -1; 231 } 232 } 233 fcntl(s, F_SETOWN, pid); 234 if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) 235 break; 236 (void)close(s); 237 if (errno == EADDRINUSE) { 238 lport--; 239 continue; 240 } else if (errno == ECONNREFUSED) 241 refused++; 242 if (r->ai_next) { 243 int oerrno = errno; 244 char hbuf[NI_MAXHOST]; 245 const int niflags = NI_NUMERICHOST; 246 247 hbuf[0] = '\0'; 248 if (getnameinfo(r->ai_addr, r->ai_addrlen, 249 hbuf, (socklen_t)sizeof(hbuf), NULL, 0, niflags) != 250 0) 251 strlcpy(hbuf, "(invalid)", sizeof(hbuf)); 252 errno = oerrno; 253 warn("rcmd: connect to address %s", hbuf); 254 r = r->ai_next; 255 hbuf[0] = '\0'; 256 if (getnameinfo(r->ai_addr, r->ai_addrlen, 257 hbuf, (socklen_t)sizeof(hbuf), NULL, 0, niflags) != 258 0) 259 strlcpy(hbuf, "(invalid)", sizeof(hbuf)); 260 (void)fprintf(stderr, "Trying %s...\n", hbuf); 261 continue; 262 } 263 if (refused && timo <= 16) { 264 (void)sleep((unsigned int)timo); 265 timo *= 2; 266 r = res; 267 refused = 0; 268 continue; 269 } 270 (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, 271 strerror(errno)); 272 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 273 return -1; 274 } 275 lport--; 276 if (fd2p == 0) { 277 write(s, "", 1); 278 lport = 0; 279 } else { 280 char num[8]; 281 int s2 = rresvport_af(&lport, r->ai_family), s3; 282 socklen_t len = sizeof(from); 283 284 if (s2 < 0) 285 goto bad; 286 listen(s2, 1); 287 (void)snprintf(num, sizeof(num), "%d", lport); 288 if (write(s, num, strlen(num) + 1) != 289 (ssize_t) (strlen(num) + 1)) { 290 warn("rcmd: write (setting up stderr)"); 291 (void)close(s2); 292 goto bad; 293 } 294 reads[0].fd = s; 295 reads[0].events = POLLIN; 296 reads[1].fd = s2; 297 reads[1].events = POLLIN; 298 errno = 0; 299 pollr = poll(reads, 2, INFTIM); 300 if (pollr < 1 || (reads[1].revents & POLLIN) == 0) { 301 if (errno != 0) 302 warn("poll: setting up stderr"); 303 else 304 warnx( 305 "poll: protocol failure in circuit setup"); 306 (void)close(s2); 307 goto bad; 308 } 309 s3 = accept(s2, (struct sockaddr *)(void *)&from, &len); 310 (void)close(s2); 311 if (s3 < 0) { 312 warn("rcmd: accept"); 313 lport = 0; 314 goto bad; 315 } 316 *fd2p = s3; 317 switch (((struct sockaddr *)(void *)&from)->sa_family) { 318 case AF_INET: 319 #ifdef INET6 320 case AF_INET6: 321 #endif 322 if (getnameinfo((struct sockaddr *)(void *)&from, len, 323 NULL, 0, num, (socklen_t)sizeof(num), 324 NI_NUMERICSERV) != 0 || 325 (atoi(num) >= IPPORT_RESERVED || 326 atoi(num) < IPPORT_RESERVED / 2)) { 327 warnx( 328 "rcmd: protocol failure in circuit setup."); 329 goto bad2; 330 } 331 break; 332 default: 333 break; 334 } 335 } 336 337 (void)write(s, locuser, strlen(locuser)+1); 338 (void)write(s, remuser, strlen(remuser)+1); 339 (void)write(s, cmd, strlen(cmd)+1); 340 if (read(s, &c, 1) != 1) { 341 warn("%s", *ahost); 342 goto bad2; 343 } 344 if (c != 0) { 345 while (read(s, &c, 1) == 1) { 346 (void)write(STDERR_FILENO, &c, 1); 347 if (c == '\n') 348 break; 349 } 350 goto bad2; 351 } 352 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 353 return s; 354 bad2: 355 if (lport) 356 (void)close(*fd2p); 357 bad: 358 (void)close(s); 359 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 360 return -1; 361 } 362 363 /* 364 * based on code written by Chris Siebenmann <cks@utcc.utoronto.ca> 365 */ 366 /* ARGSUSED */ 367 static int 368 rshrcmd(int af, char **ahost, u_int32_t rport, const char *locuser, 369 const char *remuser, const char *cmd, int *fd2p, const char *rshcmd) 370 { 371 pid_t pid; 372 int sp[2], ep[2]; 373 char *p; 374 struct passwd *pw, pwres; 375 char pwbuf[1024]; 376 377 _DIAGASSERT(ahost != NULL); 378 _DIAGASSERT(locuser != NULL); 379 _DIAGASSERT(remuser != NULL); 380 _DIAGASSERT(cmd != NULL); 381 /* fd2p may be NULL */ 382 383 /* What rsh/shell to use. */ 384 if (rshcmd == NULL) 385 rshcmd = _PATH_BIN_RCMD; 386 387 /* locuser must exist on this host. */ 388 if (getpwnam_r(locuser, &pwres, pwbuf, sizeof(pwbuf), &pw) != 0 || 389 pw == NULL) { 390 warnx("%s: unknown user: %s", __func__, locuser); 391 return -1; 392 } 393 394 /* get a socketpair we'll use for stdin and stdout. */ 395 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sp) < 0) { 396 warn("%s: socketpair", __func__); 397 return -1; 398 } 399 /* we will use this for the fd2 pointer */ 400 if (fd2p) { 401 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, ep) < 0) { 402 warn("%s: socketpair", __func__); 403 return -1; 404 } 405 *fd2p = ep[0]; 406 } 407 408 pid = fork(); 409 if (pid < 0) { 410 warn("%s: fork", __func__); 411 return -1; 412 } 413 if (pid == 0) { 414 /* 415 * child 416 * - we use sp[1] to be stdin/stdout, and close sp[0] 417 * - with fd2p, we use ep[1] for stderr, and close ep[0] 418 */ 419 (void)close(sp[0]); 420 if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) { 421 warn("%s: dup2", __func__); 422 _exit(1); 423 } 424 (void)close(sp[1]); 425 if (fd2p) { 426 if (dup2(ep[1], 2) < 0) { 427 warn("%s: dup2", __func__); 428 _exit(1); 429 } 430 (void)close(ep[0]); 431 (void)close(ep[1]); 432 } else if (dup2(0, 2) < 0) { 433 warn("%s: dup2", __func__); 434 _exit(1); 435 } 436 /* fork again to lose parent. */ 437 pid = fork(); 438 if (pid < 0) { 439 warn("%s: second fork", __func__); 440 _exit(1); 441 } 442 if (pid > 0) 443 _exit(0); 444 445 /* Orphan. Become local user for rshprog. */ 446 if (setuid(pw->pw_uid)) { 447 warn("%s: setuid(%lu)", __func__, (u_long)pw->pw_uid); 448 _exit(1); 449 } 450 451 /* 452 * If we are rcmd'ing to "localhost" as the same user as we 453 * are, then avoid running remote shell for efficiency. 454 */ 455 if (strcmp(*ahost, "localhost") == 0 && 456 strcmp(locuser, remuser) == 0) { 457 if (pw->pw_shell[0] == '\0') 458 rshcmd = _PATH_BSHELL; 459 else 460 rshcmd = pw->pw_shell; 461 p = strrchr(rshcmd, '/'); 462 execlp(rshcmd, p ? p + 1 : rshcmd, "-c", cmd, NULL); 463 } else { 464 const char *program; 465 program = strrchr(rshcmd, '/'); 466 program = program ? program + 1 : rshcmd; 467 switch (af) { 468 case AF_INET: 469 execlp(rshcmd, program, "-4", "-l", remuser, 470 *ahost, cmd, NULL); 471 break; 472 473 case AF_INET6: 474 execlp(rshcmd, program, "-6", "-l", remuser, 475 *ahost, cmd, NULL); 476 break; 477 478 default: 479 /* typically AF_UNSPEC, plus whatever */ 480 execlp(rshcmd, program, "-l", remuser, 481 *ahost, cmd, NULL); 482 break; 483 } 484 } 485 warn("%s: exec %s", __func__, rshcmd); 486 _exit(1); 487 } 488 /* Parent */ 489 (void)close(sp[1]); 490 if (fd2p) 491 (void)close(ep[1]); 492 493 (void)waitpid(pid, NULL, 0); 494 return sp[0]; 495 } 496 497 int 498 rresvport(int *alport) 499 { 500 501 _DIAGASSERT(alport != NULL); 502 503 return rresvport_af(alport, AF_INET); 504 } 505 506 int 507 rresvport_af(int *alport, int family) 508 { 509 return rresvport_af_addr(alport, family, NULL); 510 } 511 512 int 513 rresvport_af_addr(int *alport, int family, void *addr) 514 { 515 struct sockaddr_storage ss; 516 struct sockaddr *sa; 517 socklen_t salen; 518 int s; 519 u_int16_t *portp; 520 521 _DIAGASSERT(alport != NULL); 522 523 memset(&ss, 0, sizeof(ss)); 524 sa = (struct sockaddr *)(void *)&ss; 525 switch (family) { 526 case AF_INET: 527 #ifdef BSD4_4 528 sa->sa_len = 529 #endif 530 salen = sizeof(struct sockaddr_in); 531 if (addr) 532 ((struct sockaddr_in *)(void *)sa)->sin_addr = 533 ((struct sockaddr_in *)addr)->sin_addr; 534 portp = &((struct sockaddr_in *)(void *)sa)->sin_port; 535 break; 536 #ifdef INET6 537 case AF_INET6: 538 #ifdef BSD4_4 539 sa->sa_len = 540 #endif 541 salen = sizeof(struct sockaddr_in6); 542 if (addr) 543 ((struct sockaddr_in6 *)(void *)sa)->sin6_addr = 544 ((struct sockaddr_in6 *)addr)->sin6_addr; 545 portp = &((struct sockaddr_in6 *)(void *)sa)->sin6_port; 546 break; 547 #endif 548 default: 549 errno = EAFNOSUPPORT; 550 return -1; 551 } 552 sa->sa_family = family; 553 s = socket(family, SOCK_STREAM, 0); 554 if (s < 0) 555 return -1; 556 #ifdef BSD4_4 557 switch (family) { 558 case AF_INET: 559 case AF_INET6: 560 *portp = 0; 561 if (bindresvport(s, (struct sockaddr_in *)(void *)sa) < 0) { 562 int sverr = errno; 563 564 (void)close(s); 565 errno = sverr; 566 return -1; 567 } 568 *alport = (int)ntohs(*portp); 569 return s; 570 default: 571 /* is it necessary to try keep code for other AFs? */ 572 break; 573 } 574 #endif 575 for (;;) { 576 *portp = htons((u_short)*alport); 577 if (bind(s, sa, salen) >= 0) 578 return s; 579 if (errno != EADDRINUSE) { 580 (void)close(s); 581 return -1; 582 } 583 (*alport)--; 584 if (*alport == IPPORT_RESERVED/2) { 585 (void)close(s); 586 errno = EAGAIN; /* close */ 587 return -1; 588 } 589 } 590 } 591 592 int __check_rhosts_file = 1; 593 const char *__rcmd_errstr; 594 595 int 596 ruserok(const char *rhost, int superuser, const char *ruser, const char *luser) 597 { 598 struct addrinfo hints, *res, *r; 599 int error; 600 601 _DIAGASSERT(rhost != NULL); 602 _DIAGASSERT(ruser != NULL); 603 _DIAGASSERT(luser != NULL); 604 605 memset(&hints, 0, sizeof(hints)); 606 hints.ai_family = PF_UNSPEC; 607 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 608 error = getaddrinfo(rhost, "0", &hints, &res); 609 if (error) 610 return -1; 611 612 for (r = res; r; r = r->ai_next) { 613 if (iruserok_sa(r->ai_addr, (int)r->ai_addrlen, superuser, 614 ruser, luser) == 0) { 615 freeaddrinfo(res); 616 return 0; 617 } 618 } 619 freeaddrinfo(res); 620 return -1; 621 } 622 623 /* 624 * New .rhosts strategy: We are passed an ip address. We spin through 625 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 626 * has ip addresses, we don't have to trust a nameserver. When it 627 * contains hostnames, we spin through the list of addresses the nameserver 628 * gives us and look for a match. 629 * 630 * Returns 0 if ok, -1 if not ok. 631 */ 632 int 633 iruserok(u_int32_t raddr, int superuser, const char *ruser, const char *luser) 634 { 635 struct sockaddr_in irsin; 636 637 memset(&irsin, 0, sizeof(irsin)); 638 irsin.sin_family = AF_INET; 639 #ifdef BSD4_4 640 irsin.sin_len = sizeof(irsin); 641 #endif 642 memcpy(&irsin.sin_addr, &raddr, sizeof(irsin.sin_addr)); 643 return iruserok_sa(&irsin, (socklen_t)sizeof(irsin), superuser, ruser, 644 luser); 645 } 646 647 /* 648 * 2nd and 3rd arguments are typed like this, to avoid dependency between 649 * unistd.h and sys/socket.h. There's no better way. 650 */ 651 int 652 iruserok_sa(const void *raddr, int rlen, int superuser, const char *ruser, 653 const char *luser) 654 { 655 const struct sockaddr *sa; 656 struct stat sbuf; 657 struct passwd *pwd, pwres; 658 FILE *hostf; 659 uid_t uid; 660 gid_t gid; 661 int isvaliduser; 662 char pbuf[MAXPATHLEN]; 663 char pwbuf[1024]; 664 665 _DIAGASSERT(raddr != NULL); 666 _DIAGASSERT(ruser != NULL); 667 _DIAGASSERT(luser != NULL); 668 669 sa = raddr; 670 671 __rcmd_errstr = NULL; 672 673 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re"); 674 675 if (hostf) { 676 if (__ivaliduser_sa(hostf, sa, (socklen_t)rlen, luser, 677 ruser) == 0) { 678 (void)fclose(hostf); 679 return 0; 680 } 681 (void)fclose(hostf); 682 } 683 684 isvaliduser = -1; 685 if (__check_rhosts_file || superuser) { 686 687 if (getpwnam_r(luser, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 688 || pwd == 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 (void)initgroups(pwd->pw_name, pwd->pw_gid); 702 (void)seteuid(pwd->pw_uid); 703 hostf = fopen(pbuf, "re"); 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(FILE *hostf, u_int32_t raddr, const char *luser, 748 const char *ruser) 749 { 750 struct sockaddr_in ivusin; 751 752 memset(&ivusin, 0, sizeof(ivusin)); 753 ivusin.sin_family = AF_INET; 754 #ifdef BSD4_4 755 ivusin.sin_len = sizeof(ivusin); 756 #endif 757 memcpy(&ivusin.sin_addr, &raddr, sizeof(ivusin.sin_addr)); 758 return __ivaliduser_sa(hostf, (struct sockaddr *)(void *)&ivusin, 759 (socklen_t)sizeof(ivusin), luser, ruser); 760 } 761 762 #ifdef notdef /*_LIBC*/ 763 static 764 #endif 765 int 766 __ivaliduser_sa(FILE *hostf, const struct sockaddr *raddr, socklen_t salen, 767 const char *luser, const char *ruser) 768 { 769 char *user, *p; 770 int ch; 771 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 772 const char *auser, *ahost; 773 int hostok, userok; 774 char *rhost = NULL; 775 int firsttime = 1; 776 char domain[MAXHOSTNAMELEN]; 777 778 getdomainname(domain, sizeof(domain)); 779 780 _DIAGASSERT(hostf != NULL); 781 _DIAGASSERT(luser != NULL); 782 _DIAGASSERT(ruser != NULL); 783 784 while (fgets(buf, (int)sizeof(buf), hostf)) { 785 p = buf; 786 /* Skip lines that are too long. */ 787 if (strchr(p, '\n') == NULL) { 788 while ((ch = getc(hostf)) != '\n' && ch != EOF) 789 ; 790 continue; 791 } 792 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 793 *p = isupper((unsigned char)*p) ? 794 tolower((unsigned char)*p) : *p; 795 p++; 796 } 797 if (*p == ' ' || *p == '\t') { 798 *p++ = '\0'; 799 while (*p == ' ' || *p == '\t') 800 p++; 801 user = p; 802 while (*p != '\n' && *p != ' ' && 803 *p != '\t' && *p != '\0') 804 p++; 805 } else 806 user = p; 807 *p = '\0'; 808 809 if (p == buf) 810 continue; 811 812 auser = *user ? user : luser; 813 ahost = buf; 814 815 if (ahost[0] == '+') 816 switch (ahost[1]) { 817 case '\0': 818 hostok = 1; 819 break; 820 821 case '@': 822 if (firsttime) { 823 rhost = __gethostloop(raddr, salen); 824 firsttime = 0; 825 } 826 if (rhost) 827 hostok = innetgr(&ahost[2], rhost, 828 NULL, domain); 829 else 830 hostok = 0; 831 break; 832 833 default: 834 hostok = __icheckhost(raddr, salen, &ahost[1]); 835 break; 836 } 837 else if (ahost[0] == '-') 838 switch (ahost[1]) { 839 case '\0': 840 hostok = -1; 841 break; 842 843 case '@': 844 if (firsttime) { 845 rhost = __gethostloop(raddr, salen); 846 firsttime = 0; 847 } 848 if (rhost) 849 hostok = -innetgr(&ahost[2], rhost, 850 NULL, domain); 851 else 852 hostok = 0; 853 break; 854 855 default: 856 hostok = 857 -__icheckhost(raddr, salen, &ahost[1]); 858 break; 859 } 860 else 861 hostok = __icheckhost(raddr, salen, ahost); 862 863 864 if (auser[0] == '+') 865 switch (auser[1]) { 866 case '\0': 867 userok = 1; 868 break; 869 870 case '@': 871 userok = innetgr(&auser[2], NULL, ruser, 872 domain); 873 break; 874 875 default: 876 userok = strcmp(ruser, &auser[1]) == 0; 877 break; 878 } 879 else if (auser[0] == '-') 880 switch (auser[1]) { 881 case '\0': 882 userok = -1; 883 break; 884 885 case '@': 886 userok = -innetgr(&auser[2], NULL, ruser, 887 domain); 888 break; 889 890 default: 891 userok = 892 -(strcmp(ruser, &auser[1]) == 0 ? 1 : 0); 893 break; 894 } 895 else 896 userok = strcmp(ruser, auser) == 0; 897 898 /* Check if one component did not match */ 899 if (hostok == 0 || userok == 0) 900 continue; 901 902 /* Check if we got a forbidden pair */ 903 if (userok == -1 || hostok == -1) 904 return -1; 905 906 /* Check if we got a valid pair */ 907 if (hostok == 1 && userok == 1) 908 return 0; 909 } 910 return -1; 911 } 912 913 /* 914 * Returns "true" if match, 0 if no match. 915 */ 916 static int 917 __icheckhost(const struct sockaddr *raddr, socklen_t salen, const char *lhost) 918 { 919 struct addrinfo hints, *res, *r; 920 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 921 int error; 922 const int niflags = NI_NUMERICHOST; 923 924 _DIAGASSERT(raddr != NULL); 925 _DIAGASSERT(lhost != NULL); 926 927 h1[0] = '\0'; 928 if (getnameinfo(raddr, salen, h1, (socklen_t)sizeof(h1), NULL, 0, 929 niflags) != 0) 930 return 0; 931 932 /* Resolve laddr into sockaddr */ 933 memset(&hints, 0, sizeof(hints)); 934 hints.ai_family = raddr->sa_family; 935 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 936 res = NULL; 937 error = getaddrinfo(lhost, "0", &hints, &res); 938 if (error) 939 return 0; 940 941 /* 942 * Try string comparisons between raddr and laddr. 943 */ 944 for (r = res; r; r = r->ai_next) { 945 h2[0] = '\0'; 946 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, 947 (socklen_t)sizeof(h2), NULL, 0, niflags) != 0) 948 continue; 949 if (strcmp(h1, h2) == 0) { 950 freeaddrinfo(res); 951 return 1; 952 } 953 } 954 955 /* No match. */ 956 freeaddrinfo(res); 957 return 0; 958 } 959 960 /* 961 * Return the hostname associated with the supplied address. 962 * Do a reverse lookup as well for security. If a loop cannot 963 * be found, pack the numeric IP address into the string. 964 */ 965 static char * 966 __gethostloop(const struct sockaddr *raddr, socklen_t salen) 967 { 968 static char remotehost[NI_MAXHOST]; 969 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 970 struct addrinfo hints, *res, *r; 971 int error; 972 const int niflags = NI_NUMERICHOST; 973 974 _DIAGASSERT(raddr != NULL); 975 976 h1[0] = remotehost[0] = '\0'; 977 if (getnameinfo(raddr, salen, remotehost, (socklen_t)sizeof(remotehost), 978 NULL, 0, NI_NAMEREQD) != 0) 979 return NULL; 980 if (getnameinfo(raddr, salen, h1, (socklen_t)sizeof(h1), NULL, 0, 981 niflags) != 0) 982 return NULL; 983 984 /* 985 * Look up the name and check that the supplied 986 * address is in the list 987 */ 988 memset(&hints, 0, sizeof(hints)); 989 hints.ai_family = raddr->sa_family; 990 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 991 hints.ai_flags = AI_CANONNAME; 992 res = NULL; 993 error = getaddrinfo(remotehost, "0", &hints, &res); 994 if (error) 995 return NULL; 996 997 for (r = res; r; r = r->ai_next) { 998 h2[0] = '\0'; 999 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, 1000 (socklen_t)sizeof(h2), NULL, 0, niflags) != 0) 1001 continue; 1002 if (strcmp(h1, h2) == 0) { 1003 freeaddrinfo(res); 1004 return remotehost; 1005 } 1006 } 1007 1008 /* 1009 * either the DNS adminstrator has made a configuration 1010 * mistake, or someone has attempted to spoof us 1011 */ 1012 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 1013 h1, res->ai_canonname ? res->ai_canonname : remotehost); 1014 freeaddrinfo(res); 1015 return NULL; 1016 } 1017