1 /* 2 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. 3 * Copyright (c) 1983, 1993, 1994 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #if defined(LIBC_SCCS) && !defined(lint) 32 static char *rcsid = "$OpenBSD: rcmd.c,v 1.49 2004/11/17 01:42:26 itojun Exp $"; 33 #endif /* LIBC_SCCS and not lint */ 34 35 #include <sys/param.h> 36 #include <sys/socket.h> 37 #include <sys/stat.h> 38 39 #include <netinet/in.h> 40 #include <arpa/inet.h> 41 42 #include <signal.h> 43 #include <fcntl.h> 44 #include <netdb.h> 45 #include <unistd.h> 46 #include <pwd.h> 47 #include <errno.h> 48 #include <stdio.h> 49 #include <ctype.h> 50 #include <string.h> 51 #include <syslog.h> 52 #include <stdlib.h> 53 #include <netgroup.h> 54 55 int __ivaliduser(FILE *, in_addr_t, const char *, const char *); 56 int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t, 57 const char *, const char *); 58 static int __icheckhost(struct sockaddr *, socklen_t, const char *); 59 static char *__gethostloop(struct sockaddr *, socklen_t); 60 61 int 62 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 63 char **ahost; 64 in_port_t rport; 65 const char *locuser, *remuser, *cmd; 66 int *fd2p; 67 { 68 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 69 } 70 71 int 72 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 73 char **ahost; 74 in_port_t rport; 75 const char *locuser, *remuser, *cmd; 76 int *fd2p; 77 int af; 78 { 79 static char hbuf[MAXHOSTNAMELEN]; 80 char pbuf[NI_MAXSERV]; 81 struct addrinfo hints, *res, *r; 82 int error; 83 struct sockaddr_storage from; 84 fd_set *readsp = NULL; 85 sigset_t oldmask, mask; 86 pid_t pid; 87 int s, lport, timo; 88 char c, *p; 89 int refused; 90 91 /* call rcmdsh() with specified remote shell if appropriate. */ 92 if (!issetugid() && (p = getenv("RSH")) && *p) { 93 struct servent *sp = getservbyname("shell", "tcp"); 94 95 if (sp && sp->s_port == rport) 96 return (rcmdsh(ahost, rport, locuser, remuser, 97 cmd, p)); 98 } 99 100 /* use rsh(1) if non-root and remote port is shell. */ 101 if (geteuid()) { 102 struct servent *sp = getservbyname("shell", "tcp"); 103 104 if (sp && sp->s_port == rport) 105 return (rcmdsh(ahost, rport, locuser, remuser, 106 cmd, NULL)); 107 } 108 109 pid = getpid(); 110 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); 111 memset(&hints, 0, sizeof(hints)); 112 hints.ai_family = af; 113 hints.ai_socktype = SOCK_STREAM; 114 hints.ai_flags = AI_CANONNAME; 115 error = getaddrinfo(*ahost, pbuf, &hints, &res); 116 if (error) { 117 #if 0 118 warnx("%s: %s", *ahost, gai_strerror(error)); 119 #endif 120 return (-1); 121 } 122 if (res->ai_canonname) { 123 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); 124 *ahost = hbuf; 125 } else 126 ; /*XXX*/ 127 128 r = res; 129 refused = 0; 130 sigemptyset(&mask); 131 sigaddset(&mask, SIGURG); 132 oldmask = sigprocmask(SIG_BLOCK, &mask, &oldmask); 133 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 134 s = rresvport_af(&lport, r->ai_family); 135 if (s < 0) { 136 if (errno == EAGAIN) 137 (void)fprintf(stderr, 138 "rcmd: socket: All ports in use\n"); 139 else 140 (void)fprintf(stderr, "rcmd: socket: %s\n", 141 strerror(errno)); 142 if (r->ai_next) { 143 r = r->ai_next; 144 continue; 145 } else { 146 sigprocmask(SIG_SETMASK, &oldmask, NULL); 147 freeaddrinfo(res); 148 return (-1); 149 } 150 } 151 fcntl(s, F_SETOWN, pid); 152 if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) 153 break; 154 (void)close(s); 155 if (errno == EADDRINUSE) { 156 lport--; 157 continue; 158 } 159 if (errno == ECONNREFUSED) 160 refused++; 161 if (r->ai_next) { 162 int oerrno = errno; 163 char hbuf[NI_MAXHOST]; 164 const int niflags = NI_NUMERICHOST; 165 166 hbuf[0] = '\0'; 167 if (getnameinfo(r->ai_addr, r->ai_addrlen, 168 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 169 strlcpy(hbuf, "(invalid)", sizeof hbuf); 170 (void)fprintf(stderr, "connect to address %s: ", hbuf); 171 errno = oerrno; 172 perror(0); 173 r = r->ai_next; 174 hbuf[0] = '\0'; 175 if (getnameinfo(r->ai_addr, r->ai_addrlen, 176 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 177 strlcpy(hbuf, "(invalid)", sizeof hbuf); 178 (void)fprintf(stderr, "Trying %s...\n", hbuf); 179 continue; 180 } 181 if (refused && timo <= 16) { 182 (void)sleep(timo); 183 timo *= 2; 184 r = res; 185 refused = 0; 186 continue; 187 } 188 (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, 189 strerror(errno)); 190 sigprocmask(SIG_SETMASK, &oldmask, NULL); 191 freeaddrinfo(res); 192 return (-1); 193 } 194 /* given "af" can be PF_UNSPEC, we need the real af for "s" */ 195 af = r->ai_family; 196 freeaddrinfo(res); 197 #if 0 198 /* 199 * try to rresvport() to the same port. This will make rresvport() 200 * fail it's first bind, resulting in it choosing a random port. 201 */ 202 lport--; 203 #endif 204 if (fd2p == 0) { 205 write(s, "", 1); 206 lport = 0; 207 } else { 208 char num[8]; 209 int s2 = rresvport_af(&lport, af), s3; 210 socklen_t len = sizeof(from); 211 int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask); 212 213 if (s2 < 0) 214 goto bad; 215 readsp = (fd_set *)malloc(fdssize); 216 if (readsp == NULL) 217 goto bad; 218 listen(s2, 1); 219 (void)snprintf(num, sizeof(num), "%d", lport); 220 if (write(s, num, strlen(num)+1) != strlen(num)+1) { 221 (void)fprintf(stderr, 222 "rcmd: write (setting up stderr): %s\n", 223 strerror(errno)); 224 (void)close(s2); 225 goto bad; 226 } 227 again: 228 bzero(readsp, fdssize); 229 FD_SET(s, readsp); 230 FD_SET(s2, readsp); 231 errno = 0; 232 if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 || 233 !FD_ISSET(s2, readsp)) { 234 if (errno != 0) 235 (void)fprintf(stderr, 236 "rcmd: select (setting up stderr): %s\n", 237 strerror(errno)); 238 else 239 (void)fprintf(stderr, 240 "select: protocol failure in circuit setup\n"); 241 (void)close(s2); 242 goto bad; 243 } 244 s3 = accept(s2, (struct sockaddr *)&from, &len); 245 /* 246 * XXX careful for ftp bounce attacks. If discovered, shut them 247 * down and check for the real auxiliary channel to connect. 248 */ 249 switch (from.ss_family) { 250 case AF_INET: 251 case AF_INET6: 252 if (getnameinfo((struct sockaddr *)&from, len, 253 NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 && 254 atoi(num) != 20) { 255 break; 256 } 257 close(s3); 258 goto again; 259 default: 260 break; 261 } 262 (void)close(s2); 263 if (s3 < 0) { 264 (void)fprintf(stderr, 265 "rcmd: accept: %s\n", strerror(errno)); 266 lport = 0; 267 goto bad; 268 } 269 *fd2p = s3; 270 switch (from.ss_family) { 271 case AF_INET: 272 case AF_INET6: 273 if (getnameinfo((struct sockaddr *)&from, len, 274 NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 || 275 (atoi(num) >= IPPORT_RESERVED || 276 atoi(num) < IPPORT_RESERVED / 2)) { 277 (void)fprintf(stderr, 278 "socket: protocol failure in circuit setup.\n"); 279 goto bad2; 280 } 281 break; 282 default: 283 break; 284 } 285 } 286 (void)write(s, locuser, strlen(locuser)+1); 287 (void)write(s, remuser, strlen(remuser)+1); 288 (void)write(s, cmd, strlen(cmd)+1); 289 if (read(s, &c, 1) != 1) { 290 (void)fprintf(stderr, 291 "rcmd: %s: %s\n", *ahost, strerror(errno)); 292 goto bad2; 293 } 294 if (c != 0) { 295 while (read(s, &c, 1) == 1) { 296 (void)write(STDERR_FILENO, &c, 1); 297 if (c == '\n') 298 break; 299 } 300 goto bad2; 301 } 302 sigprocmask(SIG_SETMASK, &oldmask, NULL); 303 free(readsp); 304 return (s); 305 bad2: 306 if (lport) 307 (void)close(*fd2p); 308 bad: 309 if (readsp) 310 free(readsp); 311 (void)close(s); 312 sigprocmask(SIG_SETMASK, &oldmask, NULL); 313 return (-1); 314 } 315 316 int __check_rhosts_file = 1; 317 char *__rcmd_errstr; 318 319 int 320 ruserok(rhost, superuser, ruser, luser) 321 const char *rhost, *ruser, *luser; 322 int superuser; 323 { 324 struct addrinfo hints, *res, *r; 325 int error; 326 327 memset(&hints, 0, sizeof(hints)); 328 hints.ai_family = PF_UNSPEC; 329 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 330 error = getaddrinfo(rhost, "0", &hints, &res); 331 if (error) 332 return (-1); 333 334 for (r = res; r; r = r->ai_next) { 335 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 336 luser) == 0) { 337 freeaddrinfo(res); 338 return (0); 339 } 340 } 341 freeaddrinfo(res); 342 return (-1); 343 } 344 345 /* 346 * New .rhosts strategy: We are passed an ip address. We spin through 347 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 348 * has ip addresses, we don't have to trust a nameserver. When it 349 * contains hostnames, we spin through the list of addresses the nameserver 350 * gives us and look for a match. 351 * 352 * Returns 0 if ok, -1 if not ok. 353 */ 354 int 355 iruserok(raddr, superuser, ruser, luser) 356 u_int32_t raddr; 357 int superuser; 358 const char *ruser, *luser; 359 { 360 struct sockaddr_in sin; 361 362 memset(&sin, 0, sizeof(sin)); 363 sin.sin_family = AF_INET; 364 sin.sin_len = sizeof(struct sockaddr_in); 365 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 366 return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser, 367 luser); 368 } 369 370 int 371 iruserok_sa(raddr, rlen, superuser, ruser, luser) 372 const void *raddr; 373 int rlen; 374 int superuser; 375 const char *ruser, *luser; 376 { 377 struct sockaddr *sa; 378 register char *cp; 379 struct stat sbuf; 380 struct passwd *pwd; 381 FILE *hostf; 382 uid_t uid; 383 int first; 384 char pbuf[MAXPATHLEN]; 385 386 sa = (struct sockaddr *)raddr; 387 first = 1; 388 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 389 again: 390 if (hostf) { 391 if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) { 392 (void)fclose(hostf); 393 return (0); 394 } 395 (void)fclose(hostf); 396 } 397 if (first == 1 && (__check_rhosts_file || superuser)) { 398 first = 0; 399 if ((pwd = getpwnam(luser)) == NULL) 400 return (-1); 401 snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir); 402 403 /* 404 * Change effective uid while opening .rhosts. If root and 405 * reading an NFS mounted file system, can't read files that 406 * are protected read/write owner only. 407 */ 408 uid = geteuid(); 409 (void)seteuid(pwd->pw_uid); 410 hostf = fopen(pbuf, "r"); 411 (void)seteuid(uid); 412 413 if (hostf == NULL) 414 return (-1); 415 /* 416 * If not a regular file, or is owned by someone other than 417 * user or root or if writeable by anyone but the owner, quit. 418 */ 419 cp = NULL; 420 if (lstat(pbuf, &sbuf) < 0) 421 cp = ".rhosts lstat failed"; 422 else if (!S_ISREG(sbuf.st_mode)) 423 cp = ".rhosts not regular file"; 424 else if (fstat(fileno(hostf), &sbuf) < 0) 425 cp = ".rhosts fstat failed"; 426 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 427 cp = "bad .rhosts owner"; 428 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 429 cp = ".rhosts writable by other than owner"; 430 /* If there were any problems, quit. */ 431 if (cp) { 432 __rcmd_errstr = cp; 433 (void)fclose(hostf); 434 return (-1); 435 } 436 goto again; 437 } 438 return (-1); 439 } 440 441 /* 442 * XXX 443 * Don't make static, used by lpd(8). 444 * 445 * Returns 0 if ok, -1 if not ok. 446 */ 447 int 448 __ivaliduser(hostf, raddrl, luser, ruser) 449 FILE *hostf; 450 in_addr_t raddrl; 451 const char *luser, *ruser; 452 { 453 struct sockaddr_in sin; 454 455 memset(&sin, 0, sizeof(sin)); 456 sin.sin_family = AF_INET; 457 sin.sin_len = sizeof(struct sockaddr_in); 458 memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr)); 459 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 460 luser, ruser); 461 } 462 463 int 464 __ivaliduser_sa(hostf, raddr, salen, luser, ruser) 465 FILE *hostf; 466 struct sockaddr *raddr; 467 socklen_t salen; 468 const char *luser, *ruser; 469 { 470 register char *user, *p; 471 char *buf; 472 const char *auser, *ahost; 473 int hostok, userok; 474 char *rhost = (char *)-1; 475 char domain[MAXHOSTNAMELEN]; 476 size_t buflen; 477 478 getdomainname(domain, sizeof(domain)); 479 480 while ((buf = fgetln(hostf, &buflen))) { 481 p = buf; 482 if (*p == '#') 483 continue; 484 while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') { 485 if (!isprint(*p)) 486 goto bail; 487 *p = isupper(*p) ? tolower(*p) : *p; 488 p++; 489 } 490 if (p >= buf + buflen) 491 continue; 492 if (*p == ' ' || *p == '\t') { 493 *p++ = '\0'; 494 while (p < buf + buflen && (*p == ' ' || *p == '\t')) 495 p++; 496 if (p >= buf + buflen) 497 continue; 498 user = p; 499 while (p < buf + buflen && *p != '\n' && *p != ' ' && 500 *p != '\t') { 501 if (!isprint(*p)) 502 goto bail; 503 p++; 504 } 505 } else 506 user = p; 507 *p = '\0'; 508 509 if (p == buf) 510 continue; 511 512 auser = *user ? user : luser; 513 ahost = buf; 514 515 if (strlen(ahost) >= MAXHOSTNAMELEN) 516 continue; 517 518 /* 519 * innetgr() must lookup a hostname (we do not attempt 520 * to change the semantics so that netgroups may have 521 * #.#.#.# addresses in the list.) 522 */ 523 if (ahost[0] == '+') 524 switch (ahost[1]) { 525 case '\0': 526 hostok = 1; 527 break; 528 case '@': 529 if (rhost == (char *)-1) 530 rhost = __gethostloop(raddr, salen); 531 hostok = 0; 532 if (rhost) 533 hostok = innetgr(&ahost[2], rhost, 534 NULL, domain); 535 break; 536 default: 537 hostok = __icheckhost(raddr, salen, &ahost[1]); 538 break; 539 } 540 else if (ahost[0] == '-') 541 switch (ahost[1]) { 542 case '\0': 543 hostok = -1; 544 break; 545 case '@': 546 if (rhost == (char *)-1) 547 rhost = __gethostloop(raddr, salen); 548 hostok = 0; 549 if (rhost) 550 hostok = -innetgr(&ahost[2], rhost, 551 NULL, domain); 552 break; 553 default: 554 hostok = -__icheckhost(raddr, salen, &ahost[1]); 555 break; 556 } 557 else 558 hostok = __icheckhost(raddr, salen, ahost); 559 560 561 if (auser[0] == '+') 562 switch (auser[1]) { 563 case '\0': 564 userok = 1; 565 break; 566 case '@': 567 userok = innetgr(&auser[2], NULL, ruser, 568 domain); 569 break; 570 default: 571 userok = strcmp(ruser, &auser[1]) ? 0 : 1; 572 break; 573 } 574 else if (auser[0] == '-') 575 switch (auser[1]) { 576 case '\0': 577 userok = -1; 578 break; 579 case '@': 580 userok = -innetgr(&auser[2], NULL, ruser, 581 domain); 582 break; 583 default: 584 userok = strcmp(ruser, &auser[1]) ? 0 : -1; 585 break; 586 } 587 else 588 userok = strcmp(ruser, auser) ? 0 : 1; 589 590 /* Check if one component did not match */ 591 if (hostok == 0 || userok == 0) 592 continue; 593 594 /* Check if we got a forbidden pair */ 595 if (userok <= -1 || hostok <= -1) 596 return (-1); 597 598 /* Check if we got a valid pair */ 599 if (hostok >= 1 && userok >= 1) 600 return (0); 601 } 602 bail: 603 return (-1); 604 } 605 606 /* 607 * Returns "true" if match, 0 if no match. If we do not find any 608 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. 609 */ 610 static int 611 __icheckhost(raddr, salen, lhost) 612 struct sockaddr *raddr; 613 socklen_t salen; 614 const char *lhost; 615 { 616 struct addrinfo hints, *res, *r; 617 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 618 int error; 619 const int niflags = NI_NUMERICHOST; 620 621 h1[0] = '\0'; 622 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 623 niflags) != 0) 624 return (0); 625 626 /* Resolve laddr into sockaddr */ 627 memset(&hints, 0, sizeof(hints)); 628 hints.ai_family = raddr->sa_family; 629 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 630 res = NULL; 631 error = getaddrinfo(lhost, "0", &hints, &res); 632 if (error) 633 return (0); 634 635 /* 636 * Try string comparisons between raddr and laddr. 637 */ 638 for (r = res; r; r = r->ai_next) { 639 h2[0] = '\0'; 640 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 641 NULL, 0, niflags) != 0) 642 continue; 643 if (strcmp(h1, h2) == 0) { 644 freeaddrinfo(res); 645 return (1); 646 } 647 } 648 649 /* No match. */ 650 freeaddrinfo(res); 651 return (0); 652 } 653 654 /* 655 * Return the hostname associated with the supplied address. 656 * Do a reverse lookup as well for security. If a loop cannot 657 * be found, pack the result of inet_ntoa() into the string. 658 */ 659 static char * 660 __gethostloop(raddr, salen) 661 struct sockaddr *raddr; 662 socklen_t salen; 663 { 664 static char remotehost[NI_MAXHOST]; 665 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 666 struct addrinfo hints, *res, *r; 667 int error; 668 const int niflags = NI_NUMERICHOST; 669 670 h1[0] = remotehost[0] = '\0'; 671 if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), 672 NULL, 0, NI_NAMEREQD) != 0) 673 return (NULL); 674 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 675 niflags) != 0) 676 return (NULL); 677 678 /* 679 * Look up the name and check that the supplied 680 * address is in the list 681 */ 682 memset(&hints, 0, sizeof(hints)); 683 hints.ai_family = raddr->sa_family; 684 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 685 hints.ai_flags = AI_CANONNAME; 686 res = NULL; 687 error = getaddrinfo(remotehost, "0", &hints, &res); 688 if (error) 689 return (NULL); 690 691 for (r = res; r; r = r->ai_next) { 692 h2[0] = '\0'; 693 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 694 NULL, 0, niflags) != 0) 695 continue; 696 if (strcmp(h1, h2) == 0) { 697 freeaddrinfo(res); 698 return (remotehost); 699 } 700 } 701 702 /* 703 * either the DNS adminstrator has made a configuration 704 * mistake, or someone has attempted to spoof us 705 */ 706 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 707 h1, res->ai_canonname ? res->ai_canonname : remotehost); 708 freeaddrinfo(res); 709 return (NULL); 710 } 711