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