1 /* $NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 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 #if 0 38 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 39 #else 40 static char *rcsid = "$NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $"; 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 #include <sys/param.h> 45 #include <sys/socket.h> 46 #include <sys/stat.h> 47 48 #include <netinet/in.h> 49 #include <arpa/inet.h> 50 51 #include <signal.h> 52 #include <fcntl.h> 53 #include <netdb.h> 54 #include <unistd.h> 55 #include <pwd.h> 56 #include <errno.h> 57 #include <stdio.h> 58 #include <ctype.h> 59 #include <string.h> 60 #include <syslog.h> 61 62 int __ivaliduser __P((FILE *, u_long, const char *, const char *)); 63 static int __icheckhost __P((u_int32_t, const char *)); 64 static char *__gethostloop __P((u_int32_t)); 65 66 int 67 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 68 char **ahost; 69 u_short rport; 70 const char *locuser, *remuser, *cmd; 71 int *fd2p; 72 { 73 struct hostent *hp; 74 struct sockaddr_in sin, from; 75 fd_set reads; 76 int oldmask; 77 pid_t pid; 78 int s, lport, timo; 79 char c; 80 81 pid = getpid(); 82 hp = gethostbyname(*ahost); 83 if (hp == NULL) { 84 herror(*ahost); 85 return (-1); 86 } 87 *ahost = hp->h_name; 88 oldmask = sigblock(sigmask(SIGURG)); 89 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 90 s = rresvport(&lport); 91 if (s < 0) { 92 if (errno == EAGAIN) 93 (void)fprintf(stderr, 94 "rcmd: socket: All ports in use\n"); 95 else 96 (void)fprintf(stderr, "rcmd: socket: %s\n", 97 strerror(errno)); 98 sigsetmask(oldmask); 99 return (-1); 100 } 101 fcntl(s, F_SETOWN, pid); 102 bzero(&sin, sizeof sin); 103 sin.sin_len = sizeof(struct sockaddr_in); 104 sin.sin_family = hp->h_addrtype; 105 sin.sin_port = rport; 106 bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); 107 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 108 break; 109 (void)close(s); 110 if (errno == EADDRINUSE) { 111 lport--; 112 continue; 113 } 114 if (errno == ECONNREFUSED && timo <= 16) { 115 (void)sleep(timo); 116 timo *= 2; 117 continue; 118 } 119 if (hp->h_addr_list[1] != NULL) { 120 int oerrno = errno; 121 122 (void)fprintf(stderr, "connect to address %s: ", 123 inet_ntoa(sin.sin_addr)); 124 errno = oerrno; 125 perror(0); 126 hp->h_addr_list++; 127 bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); 128 (void)fprintf(stderr, "Trying %s...\n", 129 inet_ntoa(sin.sin_addr)); 130 continue; 131 } 132 (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); 133 sigsetmask(oldmask); 134 return (-1); 135 } 136 #if 0 137 /* 138 * try to rresvport() to the same port. This will make rresvport() 139 * fail it's first bind, resulting in it choosing a random port. 140 */ 141 lport--; 142 #endif 143 if (fd2p == 0) { 144 write(s, "", 1); 145 lport = 0; 146 } else { 147 char num[8]; 148 int s2 = rresvport(&lport), s3; 149 int len = sizeof(from); 150 151 if (s2 < 0) 152 goto bad; 153 listen(s2, 1); 154 (void)snprintf(num, sizeof(num), "%d", lport); 155 if (write(s, num, strlen(num)+1) != strlen(num)+1) { 156 (void)fprintf(stderr, 157 "rcmd: write (setting up stderr): %s\n", 158 strerror(errno)); 159 (void)close(s2); 160 goto bad; 161 } 162 again: 163 FD_ZERO(&reads); 164 FD_SET(s, &reads); 165 FD_SET(s2, &reads); 166 errno = 0; 167 if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 || 168 !FD_ISSET(s2, &reads)) { 169 if (errno != 0) 170 (void)fprintf(stderr, 171 "rcmd: select (setting up stderr): %s\n", 172 strerror(errno)); 173 else 174 (void)fprintf(stderr, 175 "select: protocol failure in circuit setup\n"); 176 (void)close(s2); 177 goto bad; 178 } 179 s3 = accept(s2, (struct sockaddr *)&from, &len); 180 /* 181 * XXX careful for ftp bounce attacks. If discovered, shut them 182 * down and check for the real auxiliary channel to connect. 183 */ 184 if (from.sin_family == AF_INET && from.sin_port == htons(20)) { 185 close(s3); 186 goto again; 187 } 188 (void)close(s2); 189 if (s3 < 0) { 190 (void)fprintf(stderr, 191 "rcmd: accept: %s\n", strerror(errno)); 192 lport = 0; 193 goto bad; 194 } 195 *fd2p = s3; 196 from.sin_port = ntohs(from.sin_port); 197 if (from.sin_family != AF_INET || 198 from.sin_port >= IPPORT_RESERVED || 199 from.sin_port < IPPORT_RESERVED / 2) { 200 (void)fprintf(stderr, 201 "socket: protocol failure in circuit setup.\n"); 202 goto bad2; 203 } 204 } 205 (void)write(s, locuser, strlen(locuser)+1); 206 (void)write(s, remuser, strlen(remuser)+1); 207 (void)write(s, cmd, strlen(cmd)+1); 208 if (read(s, &c, 1) != 1) { 209 (void)fprintf(stderr, 210 "rcmd: %s: %s\n", *ahost, strerror(errno)); 211 goto bad2; 212 } 213 if (c != 0) { 214 while (read(s, &c, 1) == 1) { 215 (void)write(STDERR_FILENO, &c, 1); 216 if (c == '\n') 217 break; 218 } 219 goto bad2; 220 } 221 sigsetmask(oldmask); 222 return (s); 223 bad2: 224 if (lport) 225 (void)close(*fd2p); 226 bad: 227 (void)close(s); 228 sigsetmask(oldmask); 229 return (-1); 230 } 231 232 int 233 rresvport(alport) 234 int *alport; 235 { 236 struct sockaddr_in sin; 237 int s; 238 239 bzero(&sin, sizeof sin); 240 sin.sin_len = sizeof(struct sockaddr_in); 241 sin.sin_family = AF_INET; 242 sin.sin_addr.s_addr = INADDR_ANY; 243 s = socket(AF_INET, SOCK_STREAM, 0); 244 if (s < 0) 245 return (-1); 246 sin.sin_port = htons((u_short)*alport); 247 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 248 return (s); 249 if (errno != EADDRINUSE) { 250 (void)close(s); 251 return (-1); 252 } 253 sin.sin_port = 0; 254 if (bindresvport(s, &sin) == -1) { 255 (void)close(s); 256 return (-1); 257 } 258 *alport = (int)ntohs(sin.sin_port); 259 return (s); 260 } 261 262 int __check_rhosts_file = 1; 263 char *__rcmd_errstr; 264 265 int 266 ruserok(rhost, superuser, ruser, luser) 267 const char *rhost, *ruser, *luser; 268 int superuser; 269 { 270 struct hostent *hp; 271 char **ap; 272 int i; 273 #define MAXADDRS 35 274 u_int32_t addrs[MAXADDRS + 1]; 275 276 if ((hp = gethostbyname(rhost)) == NULL) 277 return (-1); 278 for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i) 279 bcopy(*ap, &addrs[i], sizeof(addrs[i])); 280 addrs[i] = 0; 281 282 for (i = 0; i < MAXADDRS && addrs[i]; i++) 283 if (iruserok((u_long)addrs[i], superuser, ruser, luser) == 0) 284 return (0); 285 return (-1); 286 } 287 288 /* 289 * New .rhosts strategy: We are passed an ip address. We spin through 290 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 291 * has ip addresses, we don't have to trust a nameserver. When it 292 * contains hostnames, we spin through the list of addresses the nameserver 293 * gives us and look for a match. 294 * 295 * Returns 0 if ok, -1 if not ok. 296 */ 297 int 298 iruserok(raddr, superuser, ruser, luser) 299 u_int32_t raddr; 300 int superuser; 301 const char *ruser, *luser; 302 { 303 register char *cp; 304 struct stat sbuf; 305 struct passwd *pwd; 306 FILE *hostf; 307 uid_t uid; 308 int first; 309 char pbuf[MAXPATHLEN]; 310 311 first = 1; 312 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 313 again: 314 if (hostf) { 315 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) { 316 (void)fclose(hostf); 317 return (0); 318 } 319 (void)fclose(hostf); 320 } 321 if (first == 1 && (__check_rhosts_file || superuser)) { 322 first = 0; 323 if ((pwd = getpwnam(luser)) == NULL) 324 return (-1); 325 (void)strcpy(pbuf, pwd->pw_dir); 326 (void)strcat(pbuf, "/.rhosts"); 327 328 /* 329 * Change effective uid while opening .rhosts. If root and 330 * reading an NFS mounted file system, can't read files that 331 * are protected read/write owner only. 332 */ 333 uid = geteuid(); 334 (void)seteuid(pwd->pw_uid); 335 hostf = fopen(pbuf, "r"); 336 (void)seteuid(uid); 337 338 if (hostf == NULL) 339 return (-1); 340 /* 341 * If not a regular file, or is owned by someone other than 342 * user or root or if writeable by anyone but the owner, quit. 343 */ 344 cp = NULL; 345 if (lstat(pbuf, &sbuf) < 0) 346 cp = ".rhosts lstat failed"; 347 else if (!S_ISREG(sbuf.st_mode)) 348 cp = ".rhosts not regular file"; 349 else if (fstat(fileno(hostf), &sbuf) < 0) 350 cp = ".rhosts fstat failed"; 351 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 352 cp = "bad .rhosts owner"; 353 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 354 cp = ".rhosts writeable by other than owner"; 355 /* If there were any problems, quit. */ 356 if (cp) { 357 __rcmd_errstr = cp; 358 (void)fclose(hostf); 359 return (-1); 360 } 361 goto again; 362 } 363 return (-1); 364 } 365 366 /* 367 * XXX 368 * Don't make static, used by lpd(8). 369 * 370 * Returns 0 if ok, -1 if not ok. 371 */ 372 int 373 __ivaliduser(hostf, raddrl, luser, ruser) 374 FILE *hostf; 375 u_long raddrl; 376 const char *luser, *ruser; 377 { 378 register char *user, *p; 379 int ch; 380 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 381 const char *auser, *ahost; 382 int hostok, userok; 383 char *rhost = (char *)-1; 384 char domain[MAXHOSTNAMELEN]; 385 u_int32_t raddr = (u_int32_t)raddrl; 386 387 getdomainname(domain, sizeof(domain)); 388 389 while (fgets(buf, sizeof(buf), hostf)) { 390 p = buf; 391 /* Skip lines that are too long. */ 392 if (strchr(p, '\n') == NULL) { 393 while ((ch = getc(hostf)) != '\n' && ch != EOF) 394 ; 395 continue; 396 } 397 if (*p == '#') 398 continue; 399 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 400 *p = isupper(*p) ? tolower(*p) : *p; 401 p++; 402 } 403 if (*p == ' ' || *p == '\t') { 404 *p++ = '\0'; 405 while (*p == ' ' || *p == '\t') 406 p++; 407 user = p; 408 while (*p != '\n' && *p != ' ' && 409 *p != '\t' && *p != '\0') 410 p++; 411 } else 412 user = p; 413 *p = '\0'; 414 415 if (p == buf) 416 continue; 417 418 auser = *user ? user : luser; 419 ahost = buf; 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 return (-1); 506 } 507 508 /* 509 * Returns "true" if match, 0 if no match. If we do not find any 510 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. 511 */ 512 static int 513 __icheckhost(raddr, lhost) 514 u_int32_t raddr; 515 const char *lhost; 516 { 517 register struct hostent *hp; 518 register char **pp; 519 struct in_addr in; 520 521 hp = gethostbyname(lhost); 522 if (hp != NULL) { 523 /* Spin through ip addresses. */ 524 for (pp = hp->h_addr_list; *pp; ++pp) 525 if (!bcmp(&raddr, *pp, sizeof(raddr))) 526 return (1); 527 } 528 529 in.s_addr = raddr; 530 if (strcmp(lhost, inet_ntoa(in)) == 0) 531 return (1); 532 return (0); 533 } 534 535 /* 536 * Return the hostname associated with the supplied address. 537 * Do a reverse lookup as well for security. If a loop cannot 538 * be found, pack the result of inet_ntoa() into the string. 539 */ 540 static char * 541 __gethostloop(raddr) 542 u_int32_t raddr; 543 { 544 static char remotehost[MAXHOSTNAMELEN]; 545 struct hostent *hp; 546 struct in_addr in; 547 548 hp = gethostbyaddr((char *) &raddr, sizeof(raddr), AF_INET); 549 if (hp == NULL) 550 return (NULL); 551 552 /* 553 * Look up the name and check that the supplied 554 * address is in the list 555 */ 556 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 557 remotehost[sizeof(remotehost) - 1] = '\0'; 558 hp = gethostbyname(remotehost); 559 if (hp == NULL) 560 return (NULL); 561 562 for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++) 563 if (!bcmp(hp->h_addr_list[0], (caddr_t)&raddr, sizeof(raddr))) 564 return (remotehost); 565 566 /* 567 * either the DNS adminstrator has made a configuration 568 * mistake, or someone has attempted to spoof us 569 */ 570 in.s_addr = raddr; 571 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 572 inet_ntoa(in), hp->h_name); 573 return (NULL); 574 } 575