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