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