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