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 * $FreeBSD$ 34 */ 35 36 #if defined(LIBC_SCCS) && !defined(lint) 37 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 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 #ifdef YP 57 #include <rpc/rpc.h> 58 #include <rpcsvc/yp_prot.h> 59 #include <rpcsvc/ypclnt.h> 60 #endif 61 62 /* wrapper for KAME-special getnameinfo() */ 63 #ifndef NI_WITHSCOPEID 64 #define NI_WITHSCOPEID 0 65 #endif 66 67 extern int innetgr __P(( const char *, const char *, const char *, const char * )); 68 69 #define max(a, b) ((a > b) ? a : b) 70 71 static int __iruserok_af __P((void *, int, const char *, const char *, int)); 72 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 73 static int __icheckhost __P((void *, char *, int, int)); 74 75 char paddr[INET6_ADDRSTRLEN]; 76 77 int 78 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 79 char **ahost; 80 u_short rport; 81 const char *locuser, *remuser, *cmd; 82 int *fd2p; 83 { 84 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 85 } 86 87 int 88 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 89 char **ahost; 90 u_short rport; 91 const char *locuser, *remuser, *cmd; 92 int *fd2p; 93 int af; 94 { 95 struct addrinfo hints, *res, *ai; 96 struct sockaddr_storage from; 97 fd_set reads; 98 long oldmask; 99 pid_t pid; 100 int s, aport, lport, timo, error; 101 char c; 102 int refused; 103 char num[8]; 104 105 pid = getpid(); 106 107 memset(&hints, 0, sizeof(hints)); 108 hints.ai_flags = AI_CANONNAME; 109 hints.ai_family = af; 110 hints.ai_socktype = SOCK_STREAM; 111 hints.ai_protocol = 0; 112 (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 113 error = getaddrinfo(*ahost, num, &hints, &res); 114 if (error) { 115 fprintf(stderr, "rcmd: getaddrinfo: %s\n", 116 gai_strerror(error)); 117 if (error == EAI_SYSTEM) 118 fprintf(stderr, "rcmd: getaddrinfo: %s\n", 119 strerror(errno)); 120 return (-1); 121 } 122 if (res->ai_canonname) 123 *ahost = res->ai_canonname; 124 ai = res; 125 refused = 0; 126 oldmask = sigblock(sigmask(SIGURG)); 127 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 128 s = rresvport_af(&lport, ai->ai_family); 129 if (s < 0) { 130 if (errno != EAGAIN && ai->ai_next) { 131 ai = ai->ai_next; 132 continue; 133 } 134 if (errno == EAGAIN) 135 (void)fprintf(stderr, 136 "rcmd: socket: All ports in use\n"); 137 else 138 (void)fprintf(stderr, "rcmd: socket: %s\n", 139 strerror(errno)); 140 freeaddrinfo(res); 141 sigsetmask(oldmask); 142 return (-1); 143 } 144 _fcntl(s, F_SETOWN, pid); 145 if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 146 break; 147 (void)_close(s); 148 if (errno == EADDRINUSE) { 149 lport--; 150 continue; 151 } 152 if (errno == ECONNREFUSED) 153 refused = 1; 154 if (ai->ai_next != NULL) { 155 int oerrno = errno; 156 157 getnameinfo(ai->ai_addr, ai->ai_addrlen, 158 paddr, sizeof(paddr), 159 NULL, 0, 160 NI_NUMERICHOST|NI_WITHSCOPEID); 161 (void)fprintf(stderr, "connect to address %s: ", 162 paddr); 163 errno = oerrno; 164 perror(0); 165 ai = ai->ai_next; 166 getnameinfo(ai->ai_addr, ai->ai_addrlen, 167 paddr, sizeof(paddr), 168 NULL, 0, 169 NI_NUMERICHOST|NI_WITHSCOPEID); 170 fprintf(stderr, "Trying %s...\n", paddr); 171 continue; 172 } 173 if (refused && timo <= 16) { 174 struct timespec time_to_sleep, time_remaining; 175 176 time_to_sleep.tv_sec = timo; 177 time_to_sleep.tv_nsec = 0; 178 (void)_nanosleep(&time_to_sleep, &time_remaining); 179 180 timo *= 2; 181 ai = res; 182 refused = 0; 183 continue; 184 } 185 freeaddrinfo(res); 186 (void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno)); 187 sigsetmask(oldmask); 188 return (-1); 189 } 190 lport--; 191 if (fd2p == 0) { 192 _write(s, "", 1); 193 lport = 0; 194 } else { 195 char num[8]; 196 int s2 = rresvport_af(&lport, ai->ai_family), s3; 197 int len = ai->ai_addrlen; 198 int nfds; 199 200 if (s2 < 0) 201 goto bad; 202 listen(s2, 1); 203 (void)snprintf(num, sizeof(num), "%d", lport); 204 if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 205 (void)fprintf(stderr, 206 "rcmd: write (setting up stderr): %s\n", 207 strerror(errno)); 208 (void)_close(s2); 209 goto bad; 210 } 211 nfds = max(s, s2)+1; 212 if(nfds > FD_SETSIZE) { 213 fprintf(stderr, "rcmd: too many files\n"); 214 (void)_close(s2); 215 goto bad; 216 } 217 again: 218 FD_ZERO(&reads); 219 FD_SET(s, &reads); 220 FD_SET(s2, &reads); 221 errno = 0; 222 if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 223 if (errno != 0) 224 (void)fprintf(stderr, 225 "rcmd: select (setting up stderr): %s\n", 226 strerror(errno)); 227 else 228 (void)fprintf(stderr, 229 "select: protocol failure in circuit setup\n"); 230 (void)_close(s2); 231 goto bad; 232 } 233 s3 = accept(s2, (struct sockaddr *)&from, &len); 234 switch (from.ss_family) { 235 case AF_INET: 236 aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 237 break; 238 #ifdef INET6 239 case AF_INET6: 240 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 241 break; 242 #endif 243 default: 244 aport = 0; /* error */ 245 break; 246 } 247 /* 248 * XXX careful for ftp bounce attacks. If discovered, shut them 249 * down and check for the real auxiliary channel to connect. 250 */ 251 if (aport == 20) { 252 _close(s3); 253 goto again; 254 } 255 (void)_close(s2); 256 if (s3 < 0) { 257 (void)fprintf(stderr, 258 "rcmd: accept: %s\n", strerror(errno)); 259 lport = 0; 260 goto bad; 261 } 262 *fd2p = s3; 263 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 264 (void)fprintf(stderr, 265 "socket: protocol failure in circuit setup.\n"); 266 goto bad2; 267 } 268 } 269 (void)_write(s, locuser, strlen(locuser)+1); 270 (void)_write(s, remuser, strlen(remuser)+1); 271 (void)_write(s, cmd, strlen(cmd)+1); 272 if (_read(s, &c, 1) != 1) { 273 (void)fprintf(stderr, 274 "rcmd: %s: %s\n", *ahost, strerror(errno)); 275 goto bad2; 276 } 277 if (c != 0) { 278 while (_read(s, &c, 1) == 1) { 279 (void)_write(STDERR_FILENO, &c, 1); 280 if (c == '\n') 281 break; 282 } 283 goto bad2; 284 } 285 sigsetmask(oldmask); 286 freeaddrinfo(res); 287 return (s); 288 bad2: 289 if (lport) 290 (void)_close(*fd2p); 291 bad: 292 (void)_close(s); 293 sigsetmask(oldmask); 294 freeaddrinfo(res); 295 return (-1); 296 } 297 298 int 299 rresvport(port) 300 int *port; 301 { 302 return rresvport_af(port, AF_INET); 303 } 304 305 int 306 rresvport_af(alport, family) 307 int *alport, family; 308 { 309 int i, s, len, err; 310 struct sockaddr_storage ss; 311 u_short *sport; 312 313 memset(&ss, 0, sizeof(ss)); 314 ss.ss_family = family; 315 switch (family) { 316 case AF_INET: 317 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 318 sport = &((struct sockaddr_in *)&ss)->sin_port; 319 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 320 break; 321 #ifdef INET6 322 case AF_INET6: 323 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 324 sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 325 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 326 break; 327 #endif 328 default: 329 errno = EAFNOSUPPORT; 330 return -1; 331 } 332 333 s = socket(ss.ss_family, SOCK_STREAM, 0); 334 if (s < 0) 335 return (-1); 336 #if 0 /* compat_exact_traditional_rresvport_semantics */ 337 sin.sin_port = htons((u_short)*alport); 338 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 339 return (s); 340 if (errno != EADDRINUSE) { 341 (void)_close(s); 342 return (-1); 343 } 344 #endif 345 *sport = 0; 346 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 347 (void)_close(s); 348 return (-1); 349 } 350 *alport = (int)ntohs(*sport); 351 return (s); 352 } 353 354 int __check_rhosts_file = 1; 355 char *__rcmd_errstr; 356 357 int 358 ruserok(rhost, superuser, ruser, luser) 359 const char *rhost, *ruser, *luser; 360 int superuser; 361 { 362 struct addrinfo hints, *res, *r; 363 int error; 364 365 memset(&hints, 0, sizeof(hints)); 366 hints.ai_family = PF_UNSPEC; 367 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 368 error = getaddrinfo(rhost, "0", &hints, &res); 369 if (error) 370 return (-1); 371 372 for (r = res; r; r = r->ai_next) { 373 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 374 luser) == 0) { 375 freeaddrinfo(res); 376 return (0); 377 } 378 } 379 freeaddrinfo(res); 380 return (-1); 381 } 382 383 /* 384 * New .rhosts strategy: We are passed an ip address. We spin through 385 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 386 * has ip addresses, we don't have to trust a nameserver. When it 387 * contains hostnames, we spin through the list of addresses the nameserver 388 * gives us and look for a match. 389 * 390 * Returns 0 if ok, -1 if not ok. 391 */ 392 int 393 iruserok(raddr, superuser, ruser, luser) 394 unsigned long raddr; 395 int superuser; 396 const char *ruser, *luser; 397 { 398 return __iruserok_af(&raddr, superuser, ruser, luser, AF_INET); 399 } 400 401 /* Other AF support extension of iruserok. */ 402 static int 403 __iruserok_af(raddr, superuser, ruser, luser, af) 404 void *raddr; 405 int superuser; 406 const char *ruser, *luser; 407 int af; 408 { 409 register char *cp; 410 struct stat sbuf; 411 struct passwd *pwd; 412 FILE *hostf; 413 uid_t uid; 414 int first; 415 char pbuf[MAXPATHLEN]; 416 int len = 0; 417 418 switch (af) { 419 case AF_INET: 420 len = sizeof(struct in_addr); 421 break; 422 #ifdef INET6 423 case AF_INET6: 424 len = sizeof(struct in6_addr); 425 break; 426 #endif 427 } 428 429 first = 1; 430 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 431 again: 432 if (hostf) { 433 if (__ivaliduser_af(hostf, raddr, luser, ruser, af, len) 434 == 0) { 435 (void)fclose(hostf); 436 return (0); 437 } 438 (void)fclose(hostf); 439 } 440 if (first == 1 && (__check_rhosts_file || superuser)) { 441 first = 0; 442 if ((pwd = getpwnam(luser)) == NULL) 443 return (-1); 444 (void)strcpy(pbuf, pwd->pw_dir); 445 (void)strcat(pbuf, "/.rhosts"); 446 447 /* 448 * Change effective uid while opening .rhosts. If root and 449 * reading an NFS mounted file system, can't read files that 450 * are protected read/write owner only. 451 */ 452 uid = geteuid(); 453 (void)seteuid(pwd->pw_uid); 454 hostf = fopen(pbuf, "r"); 455 (void)seteuid(uid); 456 457 if (hostf == NULL) 458 return (-1); 459 /* 460 * If not a regular file, or is owned by someone other than 461 * user or root or if writeable by anyone but the owner, quit. 462 */ 463 cp = NULL; 464 if (lstat(pbuf, &sbuf) < 0) 465 cp = ".rhosts lstat failed"; 466 else if (!S_ISREG(sbuf.st_mode)) 467 cp = ".rhosts not regular file"; 468 else if (fstat(fileno(hostf), &sbuf) < 0) 469 cp = ".rhosts fstat failed"; 470 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 471 cp = "bad .rhosts owner"; 472 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 473 cp = ".rhosts writeable by other than owner"; 474 /* If there were any problems, quit. */ 475 if (cp) { 476 __rcmd_errstr = cp; 477 (void)fclose(hostf); 478 return (-1); 479 } 480 goto again; 481 } 482 return (-1); 483 } 484 485 /* 486 * AF independent extension of iruserok. We are passed an sockaddr, and 487 * then call iruserok_af() as the type of sockaddr. 488 * 489 * Returns 0 if ok, -1 if not ok. 490 */ 491 int 492 iruserok_sa(addr, addrlen, superuser, ruser, luser) 493 const void *addr; 494 int addrlen; 495 int superuser; 496 const char *ruser, *luser; 497 { 498 struct sockaddr *sa; 499 void *raddr = NULL; 500 501 sa = (struct sockaddr *)addr; 502 switch (sa->sa_family) { 503 case AF_INET: 504 raddr = &((struct sockaddr_in *)sa)->sin_addr; 505 break; 506 #ifdef INET6 507 case AF_INET6: 508 raddr = &((struct sockaddr_in6 *)sa)->sin6_addr; 509 break; 510 #endif 511 } 512 513 __iruserok_af(raddr, superuser, ruser, luser, sa->sa_family); 514 } 515 516 /* 517 * XXX 518 * Don't make static, used by lpd(8). 519 * 520 * Returns 0 if ok, -1 if not ok. 521 */ 522 int 523 __ivaliduser(hostf, raddr, luser, ruser) 524 FILE *hostf; 525 u_int32_t raddr; 526 const char *luser, *ruser; 527 { 528 return __ivaliduser_af(hostf, &raddr, luser, ruser, AF_INET, 529 sizeof(raddr)); 530 } 531 532 int 533 __ivaliduser_af(hostf, raddr, luser, ruser, af, len) 534 FILE *hostf; 535 void *raddr; 536 const char *luser, *ruser; 537 int af, len; 538 { 539 register char *user, *p; 540 int ch; 541 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 542 char hname[MAXHOSTNAMELEN]; 543 struct hostent *hp; 544 /* Presumed guilty until proven innocent. */ 545 int userok = 0, hostok = 0; 546 int h_error; 547 #ifdef YP 548 char *ypdomain; 549 550 if (yp_get_default_domain(&ypdomain)) 551 ypdomain = NULL; 552 #else 553 #define ypdomain NULL 554 #endif 555 /* We need to get the damn hostname back for netgroup matching. */ 556 if ((hp = getipnodebyaddr((char *)raddr, len, af, &h_error)) == NULL) 557 return (-1); 558 strncpy(hname, hp->h_name, sizeof(hname)); 559 hname[sizeof(hname) - 1] = '\0'; 560 freehostent(hp); 561 562 while (fgets(buf, sizeof(buf), hostf)) { 563 p = buf; 564 /* Skip lines that are too long. */ 565 if (strchr(p, '\n') == NULL) { 566 while ((ch = getc(hostf)) != '\n' && ch != EOF); 567 continue; 568 } 569 if (*p == '\n' || *p == '#') { 570 /* comment... */ 571 continue; 572 } 573 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 574 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 575 p++; 576 } 577 if (*p == ' ' || *p == '\t') { 578 *p++ = '\0'; 579 while (*p == ' ' || *p == '\t') 580 p++; 581 user = p; 582 while (*p != '\n' && *p != ' ' && 583 *p != '\t' && *p != '\0') 584 p++; 585 } else 586 user = p; 587 *p = '\0'; 588 /* 589 * Do +/- and +@/-@ checking. This looks really nasty, 590 * but it matches SunOS's behavior so far as I can tell. 591 */ 592 switch(buf[0]) { 593 case '+': 594 if (!buf[1]) { /* '+' matches all hosts */ 595 hostok = 1; 596 break; 597 } 598 if (buf[1] == '@') /* match a host by netgroup */ 599 hostok = innetgr((char *)&buf[2], 600 (char *)&hname, NULL, ypdomain); 601 else /* match a host by addr */ 602 hostok = __icheckhost(raddr,(char *)&buf[1], 603 af, len); 604 break; 605 case '-': /* reject '-' hosts and all their users */ 606 if (buf[1] == '@') { 607 if (innetgr((char *)&buf[2], 608 (char *)&hname, NULL, ypdomain)) 609 return(-1); 610 } else { 611 if (__icheckhost(raddr,(char *)&buf[1],af,len)) 612 return(-1); 613 } 614 break; 615 default: /* if no '+' or '-', do a simple match */ 616 hostok = __icheckhost(raddr, buf, af, len); 617 break; 618 } 619 switch(*user) { 620 case '+': 621 if (!*(user+1)) { /* '+' matches all users */ 622 userok = 1; 623 break; 624 } 625 if (*(user+1) == '@') /* match a user by netgroup */ 626 userok = innetgr(user+2, NULL, ruser, ypdomain); 627 else /* match a user by direct specification */ 628 userok = !(strcmp(ruser, user+1)); 629 break; 630 case '-': /* if we matched a hostname, */ 631 if (hostok) { /* check for user field rejections */ 632 if (!*(user+1)) 633 return(-1); 634 if (*(user+1) == '@') { 635 if (innetgr(user+2, NULL, 636 ruser, ypdomain)) 637 return(-1); 638 } else { 639 if (!strcmp(ruser, user+1)) 640 return(-1); 641 } 642 } 643 break; 644 default: /* no rejections: try to match the user */ 645 if (hostok) 646 userok = !(strcmp(ruser,*user ? user : luser)); 647 break; 648 } 649 if (hostok && userok) 650 return(0); 651 } 652 return (-1); 653 } 654 655 /* 656 * Returns "true" if match, 0 if no match. 657 */ 658 static int 659 __icheckhost(raddr, lhost, af, len) 660 void *raddr; 661 register char *lhost; 662 int af, len; 663 { 664 register struct hostent *hp; 665 char laddr[BUFSIZ]; /* xxx */ 666 register char **pp; 667 int h_error; 668 int match; 669 670 /* Try for raw ip address first. */ 671 if (inet_pton(af, lhost, laddr) == 1) { 672 if (memcmp(raddr, laddr, len) == 0) 673 return (1); 674 else 675 return (0); 676 } 677 678 /* Better be a hostname. */ 679 if ((hp = getipnodebyname(lhost, af, AI_ALL|AI_DEFAULT, &h_error)) 680 == NULL) 681 return (0); 682 683 /* Spin through ip addresses. */ 684 match = 0; 685 for (pp = hp->h_addr_list; *pp; ++pp) 686 if (!bcmp(raddr, *pp, len)) { 687 match = 1; 688 break; 689 } 690 691 freehostent(hp); 692 return (match); 693 } 694