1 /* 2 * Copyright (c) 1995, 1996 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. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * This product includes software developed by Theo de Raadt. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #if defined(LIBC_SCCS) && !defined(lint) 37 static char *rcsid = "$OpenBSD: rcmd.c,v 1.16 1996/09/02 21:26:09 millert Exp $"; 38 #endif /* LIBC_SCCS and not lint */ 39 40 #include <sys/param.h> 41 #include <sys/socket.h> 42 #include <sys/stat.h> 43 44 #include <netinet/in.h> 45 #include <arpa/inet.h> 46 47 #include <signal.h> 48 #include <fcntl.h> 49 #include <netdb.h> 50 #include <unistd.h> 51 #include <pwd.h> 52 #include <errno.h> 53 #include <stdio.h> 54 #include <ctype.h> 55 #include <string.h> 56 #include <syslog.h> 57 58 int __ivaliduser __P((FILE *, u_long, const char *, const char *)); 59 static int __icheckhost __P((u_int32_t, const char *)); 60 static char *__gethostloop __P((u_int32_t)); 61 62 int 63 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 64 char **ahost; 65 u_short rport; 66 const char *locuser, *remuser, *cmd; 67 int *fd2p; 68 { 69 struct hostent *hp; 70 struct sockaddr_in sin, from; 71 fd_set reads; 72 int oldmask; 73 pid_t pid; 74 int s, lport, timo; 75 char c, *p; 76 77 /* call rcmdsh() with specified remote shell if appropriate. */ 78 if ((p = getenv("RSH"))) { 79 struct servent *sp = getservbyname("shell", "tcp"); 80 81 if (sp && sp->s_port == rport) 82 return (rcmdsh(ahost, rport, locuser, remuser, 83 cmd, p)); 84 } 85 86 /* use rsh(1) if non-root and remote port is shell. */ 87 if (geteuid()) { 88 struct servent *sp = getservbyname("shell", "tcp"); 89 90 if (sp && sp->s_port == rport) 91 return (rcmdsh(ahost, rport, locuser, remuser, 92 cmd, NULL)); 93 } 94 95 pid = getpid(); 96 hp = gethostbyname(*ahost); 97 if (hp == NULL) { 98 herror(*ahost); 99 return (-1); 100 } 101 *ahost = hp->h_name; 102 103 oldmask = sigblock(sigmask(SIGURG)); 104 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 105 s = rresvport(&lport); 106 if (s < 0) { 107 if (errno == EAGAIN) 108 (void)fprintf(stderr, 109 "rcmd: socket: All ports in use\n"); 110 else 111 (void)fprintf(stderr, "rcmd: socket: %s\n", 112 strerror(errno)); 113 sigsetmask(oldmask); 114 return (-1); 115 } 116 fcntl(s, F_SETOWN, pid); 117 bzero(&sin, sizeof sin); 118 sin.sin_len = sizeof(struct sockaddr_in); 119 sin.sin_family = hp->h_addrtype; 120 sin.sin_port = rport; 121 bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); 122 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 123 break; 124 (void)close(s); 125 if (errno == EADDRINUSE) { 126 lport--; 127 continue; 128 } 129 if (errno == ECONNREFUSED && timo <= 16) { 130 (void)sleep(timo); 131 timo *= 2; 132 continue; 133 } 134 if (hp->h_addr_list[1] != NULL) { 135 int oerrno = errno; 136 137 (void)fprintf(stderr, "connect to address %s: ", 138 inet_ntoa(sin.sin_addr)); 139 errno = oerrno; 140 perror(0); 141 hp->h_addr_list++; 142 bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); 143 (void)fprintf(stderr, "Trying %s...\n", 144 inet_ntoa(sin.sin_addr)); 145 continue; 146 } 147 (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); 148 sigsetmask(oldmask); 149 return (-1); 150 } 151 #if 0 152 /* 153 * try to rresvport() to the same port. This will make rresvport() 154 * fail it's first bind, resulting in it choosing a random port. 155 */ 156 lport--; 157 #endif 158 if (fd2p == 0) { 159 write(s, "", 1); 160 lport = 0; 161 } else { 162 char num[8]; 163 int s2 = rresvport(&lport), s3; 164 int len = sizeof(from); 165 166 if (s2 < 0) 167 goto bad; 168 listen(s2, 1); 169 (void)snprintf(num, sizeof(num), "%d", lport); 170 if (write(s, num, strlen(num)+1) != strlen(num)+1) { 171 (void)fprintf(stderr, 172 "rcmd: write (setting up stderr): %s\n", 173 strerror(errno)); 174 (void)close(s2); 175 goto bad; 176 } 177 again: 178 FD_ZERO(&reads); 179 FD_SET(s, &reads); 180 FD_SET(s2, &reads); 181 errno = 0; 182 if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 || 183 !FD_ISSET(s2, &reads)) { 184 if (errno != 0) 185 (void)fprintf(stderr, 186 "rcmd: select (setting up stderr): %s\n", 187 strerror(errno)); 188 else 189 (void)fprintf(stderr, 190 "select: protocol failure in circuit setup\n"); 191 (void)close(s2); 192 goto bad; 193 } 194 s3 = accept(s2, (struct sockaddr *)&from, &len); 195 /* 196 * XXX careful for ftp bounce attacks. If discovered, shut them 197 * down and check for the real auxiliary channel to connect. 198 */ 199 if (from.sin_family == AF_INET && from.sin_port == htons(20)) { 200 close(s3); 201 goto again; 202 } 203 (void)close(s2); 204 if (s3 < 0) { 205 (void)fprintf(stderr, 206 "rcmd: accept: %s\n", strerror(errno)); 207 lport = 0; 208 goto bad; 209 } 210 *fd2p = s3; 211 from.sin_port = ntohs(from.sin_port); 212 if (from.sin_family != AF_INET || 213 from.sin_port >= IPPORT_RESERVED || 214 from.sin_port < IPPORT_RESERVED / 2) { 215 (void)fprintf(stderr, 216 "socket: protocol failure in circuit setup.\n"); 217 goto bad2; 218 } 219 } 220 (void)write(s, locuser, strlen(locuser)+1); 221 (void)write(s, remuser, strlen(remuser)+1); 222 (void)write(s, cmd, strlen(cmd)+1); 223 if (read(s, &c, 1) != 1) { 224 (void)fprintf(stderr, 225 "rcmd: %s: %s\n", *ahost, strerror(errno)); 226 goto bad2; 227 } 228 if (c != 0) { 229 while (read(s, &c, 1) == 1) { 230 (void)write(STDERR_FILENO, &c, 1); 231 if (c == '\n') 232 break; 233 } 234 goto bad2; 235 } 236 sigsetmask(oldmask); 237 return (s); 238 bad2: 239 if (lport) 240 (void)close(*fd2p); 241 bad: 242 (void)close(s); 243 sigsetmask(oldmask); 244 return (-1); 245 } 246 247 int 248 rresvport(alport) 249 int *alport; 250 { 251 struct sockaddr_in sin; 252 int s; 253 254 bzero(&sin, sizeof sin); 255 sin.sin_len = sizeof(struct sockaddr_in); 256 sin.sin_family = AF_INET; 257 sin.sin_addr.s_addr = INADDR_ANY; 258 s = socket(AF_INET, SOCK_STREAM, 0); 259 if (s < 0) 260 return (-1); 261 sin.sin_port = htons((u_short)*alport); 262 if (*alport != IPPORT_RESERVED - 1) { 263 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 264 return (s); 265 if (errno != EADDRINUSE) { 266 (void)close(s); 267 return (-1); 268 } 269 } 270 sin.sin_port = 0; 271 if (bindresvport(s, &sin) == -1) { 272 (void)close(s); 273 return (-1); 274 } 275 *alport = (int)ntohs(sin.sin_port); 276 return (s); 277 } 278 279 int __check_rhosts_file = 1; 280 char *__rcmd_errstr; 281 282 int 283 ruserok(rhost, superuser, ruser, luser) 284 const char *rhost, *ruser, *luser; 285 int superuser; 286 { 287 struct hostent *hp; 288 char **ap; 289 int i; 290 #define MAXADDRS 35 291 u_int32_t addrs[MAXADDRS + 1]; 292 293 if ((hp = gethostbyname(rhost)) == NULL) 294 return (-1); 295 for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i) 296 bcopy(*ap, &addrs[i], sizeof(addrs[i])); 297 addrs[i] = 0; 298 299 for (i = 0; i < MAXADDRS && addrs[i]; i++) 300 if (iruserok((u_long)addrs[i], superuser, ruser, luser) == 0) 301 return (0); 302 return (-1); 303 } 304 305 /* 306 * New .rhosts strategy: We are passed an ip address. We spin through 307 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 308 * has ip addresses, we don't have to trust a nameserver. When it 309 * contains hostnames, we spin through the list of addresses the nameserver 310 * gives us and look for a match. 311 * 312 * Returns 0 if ok, -1 if not ok. 313 */ 314 int 315 iruserok(raddr, superuser, ruser, luser) 316 u_int32_t raddr; 317 int superuser; 318 const char *ruser, *luser; 319 { 320 register char *cp; 321 struct stat sbuf; 322 struct passwd *pwd; 323 FILE *hostf; 324 uid_t uid; 325 int first; 326 char pbuf[MAXPATHLEN]; 327 328 first = 1; 329 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 330 again: 331 if (hostf) { 332 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) { 333 (void)fclose(hostf); 334 return (0); 335 } 336 (void)fclose(hostf); 337 } 338 if (first == 1 && (__check_rhosts_file || superuser)) { 339 first = 0; 340 if ((pwd = getpwnam(luser)) == NULL) 341 return (-1); 342 (void)strcpy(pbuf, pwd->pw_dir); 343 (void)strcat(pbuf, "/.rhosts"); 344 345 /* 346 * Change effective uid while opening .rhosts. If root and 347 * reading an NFS mounted file system, can't read files that 348 * are protected read/write owner only. 349 */ 350 uid = geteuid(); 351 (void)seteuid(pwd->pw_uid); 352 hostf = fopen(pbuf, "r"); 353 (void)seteuid(uid); 354 355 if (hostf == NULL) 356 return (-1); 357 /* 358 * If not a regular file, or is owned by someone other than 359 * user or root or if writeable by anyone but the owner, quit. 360 */ 361 cp = NULL; 362 if (lstat(pbuf, &sbuf) < 0) 363 cp = ".rhosts lstat failed"; 364 else if (!S_ISREG(sbuf.st_mode)) 365 cp = ".rhosts not regular file"; 366 else if (fstat(fileno(hostf), &sbuf) < 0) 367 cp = ".rhosts fstat failed"; 368 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 369 cp = "bad .rhosts owner"; 370 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 371 cp = ".rhosts writeable by other than owner"; 372 /* If there were any problems, quit. */ 373 if (cp) { 374 __rcmd_errstr = cp; 375 (void)fclose(hostf); 376 return (-1); 377 } 378 goto again; 379 } 380 return (-1); 381 } 382 383 /* 384 * XXX 385 * Don't make static, used by lpd(8). 386 * 387 * Returns 0 if ok, -1 if not ok. 388 */ 389 int 390 __ivaliduser(hostf, raddrl, luser, ruser) 391 FILE *hostf; 392 u_long raddrl; 393 const char *luser, *ruser; 394 { 395 register char *user, *p; 396 int ch; 397 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 398 const char *auser, *ahost; 399 int hostok, userok; 400 char *rhost = (char *)-1; 401 char domain[MAXHOSTNAMELEN]; 402 u_int32_t raddr = (u_int32_t)raddrl; 403 404 getdomainname(domain, sizeof(domain)); 405 406 while (fgets(buf, sizeof(buf), hostf)) { 407 p = buf; 408 /* Skip lines that are too long. */ 409 if (strchr(p, '\n') == NULL) { 410 while ((ch = getc(hostf)) != '\n' && ch != EOF) 411 ; 412 continue; 413 } 414 if (*p == '#') 415 continue; 416 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 417 *p = isupper(*p) ? tolower(*p) : *p; 418 p++; 419 } 420 if (*p == ' ' || *p == '\t') { 421 *p++ = '\0'; 422 while (*p == ' ' || *p == '\t') 423 p++; 424 user = p; 425 while (*p != '\n' && *p != ' ' && 426 *p != '\t' && *p != '\0') 427 p++; 428 } else 429 user = p; 430 *p = '\0'; 431 432 if (p == buf) 433 continue; 434 435 auser = *user ? user : luser; 436 ahost = buf; 437 438 /* 439 * innetgr() must lookup a hostname (we do not attempt 440 * to change the semantics so that netgroups may have 441 * #.#.#.# addresses in the list.) 442 */ 443 if (ahost[0] == '+') 444 switch (ahost[1]) { 445 case '\0': 446 hostok = 1; 447 break; 448 case '@': 449 if (rhost == (char *)-1) 450 rhost = __gethostloop(raddr); 451 hostok = 0; 452 if (rhost) 453 hostok = innetgr(&ahost[2], rhost, 454 NULL, domain); 455 break; 456 default: 457 hostok = __icheckhost(raddr, &ahost[1]); 458 break; 459 } 460 else if (ahost[0] == '-') 461 switch (ahost[1]) { 462 case '\0': 463 hostok = -1; 464 break; 465 case '@': 466 if (rhost == (char *)-1) 467 rhost = __gethostloop(raddr); 468 hostok = 0; 469 if (rhost) 470 hostok = -innetgr(&ahost[2], rhost, 471 NULL, domain); 472 break; 473 default: 474 hostok = -__icheckhost(raddr, &ahost[1]); 475 break; 476 } 477 else 478 hostok = __icheckhost(raddr, ahost); 479 480 481 if (auser[0] == '+') 482 switch (auser[1]) { 483 case '\0': 484 userok = 1; 485 break; 486 case '@': 487 userok = innetgr(&auser[2], NULL, ruser, 488 domain); 489 break; 490 default: 491 userok = strcmp(ruser, &auser[1]) ? 0 : 1; 492 break; 493 } 494 else if (auser[0] == '-') 495 switch (auser[1]) { 496 case '\0': 497 userok = -1; 498 break; 499 case '@': 500 userok = -innetgr(&auser[2], NULL, ruser, 501 domain); 502 break; 503 default: 504 userok = strcmp(ruser, &auser[1]) ? 0 : -1; 505 break; 506 } 507 else 508 userok = strcmp(ruser, auser) ? 0 : 1; 509 510 /* Check if one component did not match */ 511 if (hostok == 0 || userok == 0) 512 continue; 513 514 /* Check if we got a forbidden pair */ 515 if (userok <= -1 || hostok <= -1) 516 return (-1); 517 518 /* Check if we got a valid pair */ 519 if (hostok >= 1 && userok >= 1) 520 return (0); 521 } 522 return (-1); 523 } 524 525 /* 526 * Returns "true" if match, 0 if no match. If we do not find any 527 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. 528 */ 529 static int 530 __icheckhost(raddr, lhost) 531 u_int32_t raddr; 532 const char *lhost; 533 { 534 register struct hostent *hp; 535 register char **pp; 536 struct in_addr in; 537 538 hp = gethostbyname(lhost); 539 if (hp != NULL) { 540 /* Spin through ip addresses. */ 541 for (pp = hp->h_addr_list; *pp; ++pp) 542 if (!bcmp(&raddr, *pp, sizeof(raddr))) 543 return (1); 544 } 545 546 in.s_addr = raddr; 547 if (strcmp(lhost, inet_ntoa(in)) == 0) 548 return (1); 549 return (0); 550 } 551 552 /* 553 * Return the hostname associated with the supplied address. 554 * Do a reverse lookup as well for security. If a loop cannot 555 * be found, pack the result of inet_ntoa() into the string. 556 */ 557 static char * 558 __gethostloop(raddr) 559 u_int32_t raddr; 560 { 561 static char remotehost[MAXHOSTNAMELEN]; 562 struct hostent *hp; 563 struct in_addr in; 564 565 hp = gethostbyaddr((char *) &raddr, sizeof(raddr), AF_INET); 566 if (hp == NULL) 567 return (NULL); 568 569 /* 570 * Look up the name and check that the supplied 571 * address is in the list 572 */ 573 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 574 remotehost[sizeof(remotehost) - 1] = '\0'; 575 hp = gethostbyname(remotehost); 576 if (hp == NULL) 577 return (NULL); 578 579 for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++) 580 if (!bcmp(hp->h_addr_list[0], (caddr_t)&raddr, sizeof(raddr))) 581 return (remotehost); 582 583 /* 584 * either the DNS adminstrator has made a configuration 585 * mistake, or someone has attempted to spoof us 586 */ 587 in.s_addr = raddr; 588 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 589 inet_ntoa(in), hp->h_name); 590 return (NULL); 591 } 592