1 /* $NetBSD: rcmd.c,v 1.42 2000/05/30 01:07:44 itojun Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Matthew R. Green. 5 * Copyright (c) 1983, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 #if defined(LIBC_SCCS) && !defined(lint) 39 #if 0 40 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 41 #else 42 __RCSID("$NetBSD: rcmd.c,v 1.42 2000/05/30 01:07:44 itojun Exp $"); 43 #endif 44 #endif /* LIBC_SCCS and not lint */ 45 46 #ifdef _LIBC 47 #include "namespace.h" 48 #endif 49 #include <sys/param.h> 50 #include <sys/socket.h> 51 #include <sys/stat.h> 52 #include <sys/poll.h> 53 #include <sys/wait.h> 54 55 #include <netinet/in.h> 56 #include <rpc/rpc.h> 57 #include <arpa/inet.h> 58 #include <netgroup.h> 59 60 #include <assert.h> 61 #include <ctype.h> 62 #include <err.h> 63 #include <errno.h> 64 #include <fcntl.h> 65 #include <grp.h> 66 #include <netdb.h> 67 #include <paths.h> 68 #include <pwd.h> 69 #include <signal.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <syslog.h> 74 #include <unistd.h> 75 76 #include "pathnames.h" 77 78 int orcmd __P((char **, u_int, const char *, const char *, const char *, 79 int *)); 80 int orcmd_af __P((char **, u_int, const char *, const char *, const char *, 81 int *, int)); 82 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 83 int __ivaliduser_sa __P((FILE *, struct sockaddr *, socklen_t, const char *, 84 const char *)); 85 static int rshrcmd __P((char **, u_int32_t, const char *, const char *, 86 const char *, int *, const char *)); 87 static int resrcmd __P((struct addrinfo *, char **, u_int32_t, const char *, 88 const char *, const char *, int *)); 89 static int __icheckhost __P((struct sockaddr *, socklen_t, const char *)); 90 static char *__gethostloop __P((struct sockaddr *, socklen_t)); 91 92 int 93 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 94 char **ahost; 95 u_short rport; 96 const char *locuser, *remuser, *cmd; 97 int *fd2p; 98 { 99 100 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 101 } 102 103 int 104 rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 105 char **ahost; 106 u_short rport; 107 const char *locuser, *remuser, *cmd; 108 int *fd2p; 109 int af; 110 { 111 static char hbuf[MAXHOSTNAMELEN]; 112 char pbuf[NI_MAXSERV]; 113 struct addrinfo hints, *res; 114 int error; 115 struct servent *sp; 116 117 _DIAGASSERT(ahost != NULL); 118 _DIAGASSERT(locuser != NULL); 119 _DIAGASSERT(remuser != NULL); 120 _DIAGASSERT(cmd != NULL); 121 /* fd2p may be NULL */ 122 123 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); 124 memset(&hints, 0, sizeof(hints)); 125 hints.ai_family = af; 126 hints.ai_socktype = SOCK_STREAM; 127 hints.ai_flags = AI_CANONNAME; 128 error = getaddrinfo(*ahost, pbuf, &hints, &res); 129 if (error) { 130 warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ 131 return (-1); 132 } 133 if (res->ai_canonname) { 134 /* 135 * Canonicalise hostname. 136 * XXX: Should we really do this? 137 */ 138 strncpy(hbuf, res->ai_canonname, sizeof(hbuf) - 1); 139 hbuf[sizeof(hbuf) - 1] = '\0'; 140 *ahost = hbuf; 141 } 142 143 /* 144 * Check if rport is the same as the shell port, and that the fd2p. If 145 * it is not, the program isn't expecting 'rsh' and so we can't use the 146 * RCMD_CMD environment. 147 */ 148 sp = getservbyname("shell", "tcp"); 149 if (sp != NULL && sp->s_port == rport) 150 error = rshrcmd(ahost, (u_int32_t)rport, 151 locuser, remuser, cmd, fd2p, getenv("RCMD_CMD")); 152 else 153 error = resrcmd(res, ahost, (u_int32_t)rport, 154 locuser, remuser, cmd, fd2p); 155 freeaddrinfo(res); 156 return (error); 157 } 158 159 /* this is simply a wrapper around hprcmd() that handles ahost first */ 160 int 161 orcmd(ahost, rport, locuser, remuser, cmd, fd2p) 162 char **ahost; 163 u_int rport; 164 const char *locuser, *remuser, *cmd; 165 int *fd2p; 166 { 167 return orcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 168 } 169 170 int 171 orcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 172 char **ahost; 173 u_int rport; 174 const char *locuser, *remuser, *cmd; 175 int *fd2p; 176 int af; 177 { 178 static char hbuf[MAXHOSTNAMELEN]; 179 char pbuf[NI_MAXSERV]; 180 struct addrinfo hints, *res; 181 int error; 182 183 _DIAGASSERT(ahost != NULL); 184 _DIAGASSERT(locuser != NULL); 185 _DIAGASSERT(remuser != NULL); 186 _DIAGASSERT(cmd != NULL); 187 /* fd2p may be NULL */ 188 189 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); 190 memset(&hints, 0, sizeof(hints)); 191 hints.ai_family = af; 192 hints.ai_socktype = SOCK_STREAM; 193 hints.ai_flags = AI_CANONNAME; 194 error = getaddrinfo(*ahost, pbuf, &hints, &res); 195 if (error) { 196 warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ 197 return (-1); 198 } 199 if (res->ai_canonname) { 200 strncpy(hbuf, res->ai_canonname, sizeof(hbuf) - 1); 201 hbuf[sizeof(hbuf) - 1] = '\0'; 202 *ahost = hbuf; 203 } 204 205 error = resrcmd(res, ahost, rport, locuser, remuser, cmd, fd2p); 206 freeaddrinfo(res); 207 return (error); 208 } 209 210 static int 211 resrcmd(res, ahost, rport, locuser, remuser, cmd, fd2p) 212 struct addrinfo *res; 213 char **ahost; 214 u_int32_t rport; 215 const char *locuser, *remuser, *cmd; 216 int *fd2p; 217 { 218 struct addrinfo *r; 219 struct sockaddr_storage from; 220 struct pollfd reads[2]; 221 sigset_t nmask, omask; 222 pid_t pid; 223 int s, lport, timo; 224 int pollr; 225 char c; 226 int refused; 227 228 _DIAGASSERT(res != NULL); 229 _DIAGASSERT(ahost != NULL); 230 _DIAGASSERT(locuser != NULL); 231 _DIAGASSERT(remuser != NULL); 232 _DIAGASSERT(cmd != NULL); 233 /* fd2p may be NULL */ 234 235 r = res; 236 refused = 0; 237 pid = getpid(); 238 sigemptyset(&nmask); 239 sigaddset(&nmask, SIGURG); 240 if (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1) 241 return -1; 242 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 243 s = rresvport_af(&lport, r->ai_family); 244 if (s < 0) { 245 if (errno == EAGAIN) 246 warnx("rcmd: socket: All ports in use"); 247 else 248 warn("rcmd: socket"); 249 if (r->ai_next) { 250 r = r->ai_next; 251 continue; 252 } else { 253 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 254 return (-1); 255 } 256 } 257 fcntl(s, F_SETOWN, pid); 258 if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) 259 break; 260 (void)close(s); 261 if (errno == EADDRINUSE) { 262 lport--; 263 continue; 264 } else if (errno == ECONNREFUSED) 265 refused++; 266 if (r->ai_next) { 267 int oerrno = errno; 268 char hbuf[NI_MAXHOST]; 269 #ifdef NI_WITHSCOPEID 270 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 271 #else 272 const int niflags = NI_NUMERICHOST; 273 #endif 274 275 hbuf[0] = '\0'; 276 if (getnameinfo(r->ai_addr, r->ai_addrlen, 277 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 278 strcpy(hbuf, "(invalid)"); 279 warnx("rcmd: connect to address %s", hbuf); 280 errno = oerrno; 281 perror(0); 282 r = r->ai_next; 283 hbuf[0] = '\0'; 284 if (getnameinfo(r->ai_addr, r->ai_addrlen, 285 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 286 strcpy(hbuf, "(invalid)"); 287 (void)fprintf(stderr, "Trying %s...\n", hbuf); 288 continue; 289 } 290 if (refused && timo <= 16) { 291 (void)sleep((unsigned int)timo); 292 timo *= 2; 293 r = res; 294 refused = 0; 295 continue; 296 } 297 (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, 298 strerror(errno)); 299 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 300 return (-1); 301 } 302 lport--; 303 if (fd2p == 0) { 304 write(s, "", 1); 305 lport = 0; 306 } else { 307 char num[8]; 308 int s2 = rresvport_af(&lport, r->ai_family), s3; 309 socklen_t len = sizeof(from); 310 311 if (s2 < 0) 312 goto bad; 313 listen(s2, 1); 314 (void)snprintf(num, sizeof(num), "%d", lport); 315 if (write(s, num, strlen(num) + 1) != strlen(num) + 1) { 316 warn("rcmd: write (setting up stderr)"); 317 (void)close(s2); 318 goto bad; 319 } 320 reads[0].fd = s; 321 reads[0].events = POLLIN; 322 reads[1].fd = s2; 323 reads[1].events = POLLIN; 324 errno = 0; 325 pollr = poll(reads, 2, INFTIM); 326 if (pollr < 1 || (reads[1].revents & POLLIN) == 0) { 327 if (errno != 0) 328 warn("poll: setting up stderr"); 329 else 330 warnx("poll: protocol failure in circuit setup"); 331 (void)close(s2); 332 goto bad; 333 } 334 s3 = accept(s2, (struct sockaddr *)(void *)&from, &len); 335 (void)close(s2); 336 if (s3 < 0) { 337 warn("rcmd: accept"); 338 lport = 0; 339 goto bad; 340 } 341 *fd2p = s3; 342 switch (((struct sockaddr *)&from)->sa_family) { 343 case AF_INET: 344 #ifdef INET6 345 case AF_INET6: 346 #endif 347 if (getnameinfo((struct sockaddr *)&from, len, 348 NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 || 349 (atoi(num) >= IPPORT_RESERVED || 350 atoi(num) < IPPORT_RESERVED / 2)) { 351 warnx("rcmd: protocol failure in circuit setup."); 352 goto bad2; 353 } 354 break; 355 default: 356 break; 357 } 358 } 359 360 (void)write(s, locuser, strlen(locuser)+1); 361 (void)write(s, remuser, strlen(remuser)+1); 362 (void)write(s, cmd, strlen(cmd)+1); 363 if (read(s, &c, 1) != 1) { 364 warn("%s", *ahost); 365 goto bad2; 366 } 367 if (c != 0) { 368 while (read(s, &c, 1) == 1) { 369 (void)write(STDERR_FILENO, &c, 1); 370 if (c == '\n') 371 break; 372 } 373 goto bad2; 374 } 375 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 376 return (s); 377 bad2: 378 if (lport) 379 (void)close(*fd2p); 380 bad: 381 (void)close(s); 382 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 383 return (-1); 384 } 385 386 /* 387 * based on code written by Chris Siebenmann <cks@utcc.utoronto.ca> 388 */ 389 /* ARGSUSED */ 390 static int 391 rshrcmd(ahost, rport, locuser, remuser, cmd, fd2p, rshcmd) 392 char **ahost; 393 u_int32_t rport; 394 const char *locuser, *remuser, *cmd; 395 int *fd2p; 396 const char *rshcmd; 397 { 398 pid_t pid; 399 int sp[2], ep[2]; 400 char *p; 401 struct passwd *pw; 402 403 _DIAGASSERT(ahost != NULL); 404 _DIAGASSERT(locuser != NULL); 405 _DIAGASSERT(remuser != NULL); 406 _DIAGASSERT(cmd != NULL); 407 /* fd2p may be NULL */ 408 409 /* What rsh/shell to use. */ 410 if (rshcmd == NULL) 411 rshcmd = _PATH_BIN_RCMD; 412 413 /* locuser must exist on this host. */ 414 if ((pw = getpwnam(locuser)) == NULL) { 415 warnx("rshrcmd: unknown user: %s", locuser); 416 return(-1); 417 } 418 419 /* get a socketpair we'll use for stdin and stdout. */ 420 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sp) < 0) { 421 warn("rshrcmd: socketpair"); 422 return (-1); 423 } 424 /* we will use this for the fd2 pointer */ 425 if (fd2p) { 426 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, ep) < 0) { 427 warn("rshrcmd: socketpair"); 428 return (-1); 429 } 430 *fd2p = ep[0]; 431 } 432 433 pid = fork(); 434 if (pid < 0) { 435 warn("rshrcmd: fork"); 436 return (-1); 437 } 438 if (pid == 0) { 439 /* 440 * child 441 * - we use sp[1] to be stdin/stdout, and close sp[0] 442 * - with fd2p, we use ep[1] for stderr, and close ep[0] 443 */ 444 (void)close(sp[0]); 445 if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) { 446 warn("rshrcmd: dup2"); 447 _exit(1); 448 } 449 if (fd2p) { 450 if (dup2(ep[1], 2) < 0) { 451 warn("rshrcmd: dup2"); 452 _exit(1); 453 } 454 (void)close(ep[0]); 455 (void)close(ep[1]); 456 } else if (dup2(0, 2) < 0) { 457 warn("rshrcmd: dup2"); 458 _exit(1); 459 } 460 /* fork again to lose parent. */ 461 pid = fork(); 462 if (pid < 0) { 463 warn("rshrcmd: second fork"); 464 _exit(1); 465 } 466 if (pid > 0) 467 _exit(0); 468 469 /* Orphan. Become local user for rshprog. */ 470 if (setuid(pw->pw_uid)) { 471 warn("rshrcmd: setuid(%lu)", (u_long)pw->pw_uid); 472 _exit(1); 473 } 474 475 /* 476 * If we are rcmd'ing to "localhost" as the same user as we are, 477 * then avoid running remote shell for efficiency. 478 */ 479 if (strcmp(*ahost, "localhost") == 0 && 480 strcmp(locuser, remuser) == 0) { 481 if (pw->pw_shell[0] == '\0') 482 rshcmd = _PATH_BSHELL; 483 else 484 rshcmd = pw->pw_shell; 485 p = strrchr(rshcmd, '/'); 486 execlp(rshcmd, p ? p + 1 : rshcmd, "-c", cmd, NULL); 487 } else { 488 p = strrchr(rshcmd, '/'); 489 execlp(rshcmd, p ? p + 1 : rshcmd, *ahost, "-l", 490 remuser, cmd, NULL); 491 } 492 warn("rshrcmd: exec %s", rshcmd); 493 _exit(1); 494 } 495 /* Parent */ 496 (void)close(sp[1]); 497 if (fd2p) 498 (void)close(ep[1]); 499 500 (void)waitpid(pid, NULL, 0); 501 return (sp[0]); 502 } 503 504 int 505 rresvport(alport) 506 int *alport; 507 { 508 509 _DIAGASSERT(alport != NULL); 510 511 return rresvport_af(alport, AF_INET); 512 } 513 514 int 515 rresvport_af(alport, family) 516 int *alport; 517 int family; 518 { 519 struct sockaddr_storage ss; 520 struct sockaddr *sa; 521 int salen; 522 int s; 523 u_int16_t *portp; 524 525 _DIAGASSERT(alport != NULL); 526 527 memset(&ss, 0, sizeof(ss)); 528 sa = (struct sockaddr *)&ss; 529 switch (family) { 530 case AF_INET: 531 #ifdef BSD4_4 532 sa->sa_len = 533 #endif 534 salen = sizeof(struct sockaddr_in); 535 portp = &((struct sockaddr_in *)sa)->sin_port; 536 break; 537 #ifdef INET6 538 case AF_INET6: 539 #ifdef BSD4_4 540 sa->sa_len = 541 #endif 542 salen = sizeof(struct sockaddr_in6); 543 portp = &((struct sockaddr_in6 *)sa)->sin6_port; 544 break; 545 #endif 546 default: 547 portp = NULL; 548 return EAFNOSUPPORT; 549 } 550 sa->sa_family = family; 551 s = socket(family, SOCK_STREAM, 0); 552 if (s < 0) 553 return (-1); 554 #ifdef BSD4_4 555 switch (family) { 556 case AF_INET: 557 case AF_INET6: 558 *portp = 0; 559 if (bindresvport(s, (struct sockaddr_in *)sa) < 0) { 560 int sverr = errno; 561 562 (void)close(s); 563 errno = sverr; 564 return (-1); 565 } 566 *alport = (int)ntohs(*portp); 567 return (s); 568 default: 569 /* is it necessary to try keep code for other AFs? */ 570 break; 571 } 572 #endif 573 for (;;) { 574 *portp = htons((u_short)*alport); 575 if (bind(s, sa, (socklen_t)salen) >= 0) 576 return (s); 577 if (errno != EADDRINUSE) { 578 (void)close(s); 579 return (-1); 580 } 581 (*alport)--; 582 if (*alport == IPPORT_RESERVED/2) { 583 (void)close(s); 584 errno = EAGAIN; /* close */ 585 return (-1); 586 } 587 } 588 } 589 590 int __check_rhosts_file = 1; 591 char *__rcmd_errstr; 592 593 int 594 ruserok(rhost, superuser, ruser, luser) 595 const char *rhost, *ruser, *luser; 596 int superuser; 597 { 598 struct addrinfo hints, *res, *r; 599 int error; 600 601 _DIAGASSERT(rhost != NULL); 602 _DIAGASSERT(ruser != NULL); 603 _DIAGASSERT(luser != NULL); 604 605 memset(&hints, 0, sizeof(hints)); 606 hints.ai_family = PF_UNSPEC; 607 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 608 error = getaddrinfo(rhost, "0", &hints, &res); 609 if (error) 610 return (-1); 611 612 for (r = res; r; r = r->ai_next) { 613 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 614 luser) == 0) { 615 freeaddrinfo(res); 616 return (0); 617 } 618 } 619 freeaddrinfo(res); 620 return (-1); 621 } 622 623 /* 624 * New .rhosts strategy: We are passed an ip address. We spin through 625 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 626 * has ip addresses, we don't have to trust a nameserver. When it 627 * contains hostnames, we spin through the list of addresses the nameserver 628 * gives us and look for a match. 629 * 630 * Returns 0 if ok, -1 if not ok. 631 */ 632 int 633 iruserok(raddr, superuser, ruser, luser) 634 u_int32_t raddr; 635 int superuser; 636 const char *ruser, *luser; 637 { 638 struct sockaddr_in sin; 639 640 memset(&sin, 0, sizeof(sin)); 641 sin.sin_family = AF_INET; 642 #ifdef BSD4_4 643 sin.sin_len = sizeof(struct sockaddr_in); 644 #endif 645 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 646 return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser, 647 luser); 648 } 649 650 /* 651 * 2nd and 3rd arguments are typed like this, to avoid dependency between 652 * unistd.h and sys/socket.h. There's no better way. 653 */ 654 int 655 iruserok_sa(raddr, rlen, superuser, ruser, luser) 656 const void *raddr; 657 int rlen; 658 int superuser; 659 const char *ruser, *luser; 660 { 661 struct sockaddr *sa; 662 register char *cp; 663 struct stat sbuf; 664 struct passwd *pwd; 665 FILE *hostf; 666 uid_t uid; 667 gid_t gid; 668 int first; 669 char pbuf[MAXPATHLEN]; 670 671 _DIAGASSERT(raddr != NULL); 672 _DIAGASSERT(ruser != NULL); 673 _DIAGASSERT(luser != NULL); 674 675 sa = (struct sockaddr *)raddr; 676 677 first = 1; 678 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 679 again: 680 if (hostf) { 681 if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) { 682 (void)fclose(hostf); 683 return (0); 684 } 685 (void)fclose(hostf); 686 } 687 if (first == 1 && (__check_rhosts_file || superuser)) { 688 first = 0; 689 if ((pwd = getpwnam(luser)) == NULL) 690 return (-1); 691 (void)strncpy(pbuf, pwd->pw_dir, sizeof(pbuf) - 1); 692 (void)strncat(pbuf, "/.rhosts", sizeof(pbuf) - strlen(pbuf) - 1); 693 694 /* 695 * Change effective uid while opening .rhosts. If root and 696 * reading an NFS mounted file system, can't read files that 697 * are protected read/write owner only. 698 */ 699 uid = geteuid(); 700 gid = getegid(); 701 (void)setegid(pwd->pw_gid); 702 initgroups(pwd->pw_name, pwd->pw_gid); 703 (void)seteuid(pwd->pw_uid); 704 hostf = fopen(pbuf, "r"); 705 (void)seteuid(uid); 706 (void)setegid(gid); 707 708 if (hostf == NULL) 709 return (-1); 710 /* 711 * If not a regular file, or is owned by someone other than 712 * user or root or if writeable by anyone but the owner, quit. 713 */ 714 cp = NULL; 715 if (lstat(pbuf, &sbuf) < 0) 716 cp = ".rhosts lstat failed"; 717 else if (!S_ISREG(sbuf.st_mode)) 718 cp = ".rhosts not regular file"; 719 else if (fstat(fileno(hostf), &sbuf) < 0) 720 cp = ".rhosts fstat failed"; 721 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 722 cp = "bad .rhosts owner"; 723 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 724 cp = ".rhosts writeable by other than owner"; 725 /* If there were any problems, quit. */ 726 if (cp) { 727 __rcmd_errstr = cp; 728 (void)fclose(hostf); 729 return (-1); 730 } 731 goto again; 732 } 733 return (-1); 734 } 735 736 /* 737 * XXX 738 * Don't make static, used by lpd(8). We will be able to change the function 739 * into static function, when we bump libc major #. 740 * 741 * Returns 0 if ok, -1 if not ok. 742 */ 743 #ifdef notdef /*_LIBC*/ 744 static 745 #endif 746 int 747 __ivaliduser(hostf, raddr, luser, ruser) 748 FILE *hostf; 749 u_int32_t raddr; 750 const char *luser, *ruser; 751 { 752 struct sockaddr_in sin; 753 754 memset(&sin, 0, sizeof(sin)); 755 sin.sin_family = AF_INET; 756 #ifdef BSD4_4 757 sin.sin_len = sizeof(struct sockaddr_in); 758 #endif 759 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 760 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, 761 sizeof(struct sockaddr_in), luser, ruser); 762 } 763 764 #ifdef notdef /*_LIBC*/ 765 static 766 #endif 767 int 768 __ivaliduser_sa(hostf, raddr, salen, luser, ruser) 769 FILE *hostf; 770 struct sockaddr *raddr; 771 socklen_t salen; 772 const char *luser, *ruser; 773 { 774 register char *user, *p; 775 int ch; 776 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 777 const char *auser, *ahost; 778 int hostok, userok; 779 char *rhost = NULL; 780 int firsttime = 1; 781 char domain[MAXHOSTNAMELEN]; 782 783 getdomainname(domain, sizeof(domain)); 784 785 _DIAGASSERT(hostf != NULL); 786 _DIAGASSERT(luser != NULL); 787 _DIAGASSERT(ruser != NULL); 788 789 while (fgets(buf, sizeof(buf), hostf)) { 790 p = buf; 791 /* Skip lines that are too long. */ 792 if (strchr(p, '\n') == NULL) { 793 while ((ch = getc(hostf)) != '\n' && ch != EOF) 794 ; 795 continue; 796 } 797 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 798 *p = isupper((unsigned char)*p) ? tolower(*p) : *p; 799 p++; 800 } 801 if (*p == ' ' || *p == '\t') { 802 *p++ = '\0'; 803 while (*p == ' ' || *p == '\t') 804 p++; 805 user = p; 806 while (*p != '\n' && *p != ' ' && 807 *p != '\t' && *p != '\0') 808 p++; 809 } else 810 user = p; 811 *p = '\0'; 812 813 if (p == buf) 814 continue; 815 816 auser = *user ? user : luser; 817 ahost = buf; 818 819 if (ahost[0] == '+') 820 switch (ahost[1]) { 821 case '\0': 822 hostok = 1; 823 break; 824 825 case '@': 826 if (firsttime) { 827 rhost = __gethostloop(raddr, salen); 828 firsttime = 0; 829 } 830 if (rhost) 831 hostok = innetgr(&ahost[2], rhost, 832 NULL, domain); 833 else 834 hostok = 0; 835 break; 836 837 default: 838 hostok = __icheckhost(raddr, salen, &ahost[1]); 839 break; 840 } 841 else if (ahost[0] == '-') 842 switch (ahost[1]) { 843 case '\0': 844 hostok = -1; 845 break; 846 847 case '@': 848 if (firsttime) { 849 rhost = __gethostloop(raddr, salen); 850 firsttime = 0; 851 } 852 if (rhost) 853 hostok = -innetgr(&ahost[2], rhost, 854 NULL, domain); 855 else 856 hostok = 0; 857 break; 858 859 default: 860 hostok = -__icheckhost(raddr, salen, &ahost[1]); 861 break; 862 } 863 else 864 hostok = __icheckhost(raddr, salen, ahost); 865 866 867 if (auser[0] == '+') 868 switch (auser[1]) { 869 case '\0': 870 userok = 1; 871 break; 872 873 case '@': 874 userok = innetgr(&auser[2], NULL, ruser, 875 domain); 876 break; 877 878 default: 879 userok = strcmp(ruser, &auser[1]) == 0; 880 break; 881 } 882 else if (auser[0] == '-') 883 switch (auser[1]) { 884 case '\0': 885 userok = -1; 886 break; 887 888 case '@': 889 userok = -innetgr(&auser[2], NULL, ruser, 890 domain); 891 break; 892 893 default: 894 userok = 895 -(strcmp(ruser, &auser[1]) == 0 ? 1 : 0); 896 break; 897 } 898 else 899 userok = strcmp(ruser, auser) == 0; 900 901 /* Check if one component did not match */ 902 if (hostok == 0 || userok == 0) 903 continue; 904 905 /* Check if we got a forbidden pair */ 906 if (userok == -1 || hostok == -1) 907 return -1; 908 909 /* Check if we got a valid pair */ 910 if (hostok == 1 && userok == 1) 911 return 0; 912 } 913 return -1; 914 } 915 916 /* 917 * Returns "true" if match, 0 if no match. 918 * 919 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 920 * if af == AF_INET6. 921 */ 922 static int 923 __icheckhost(raddr, salen, lhost) 924 struct sockaddr *raddr; 925 socklen_t salen; 926 const char *lhost; 927 { 928 struct addrinfo hints, *res, *r; 929 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 930 int error; 931 #ifdef NI_WITHSCOPEID 932 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 933 #else 934 const int niflags = NI_NUMERICHOST; 935 #endif 936 937 _DIAGASSERT(raddr != NULL); 938 _DIAGASSERT(lhost != NULL); 939 940 h1[0] = '\0'; 941 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 942 niflags) != 0) 943 return (0); 944 945 /* Resolve laddr into sockaddr */ 946 memset(&hints, 0, sizeof(hints)); 947 hints.ai_family = raddr->sa_family; 948 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 949 res = NULL; 950 error = getaddrinfo(lhost, "0", &hints, &res); 951 if (error) 952 return (0); 953 954 /* 955 * Try string comparisons between raddr and laddr. 956 */ 957 for (r = res; r; r = r->ai_next) { 958 h2[0] = '\0'; 959 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 960 NULL, 0, niflags) != 0) 961 continue; 962 if (strcmp(h1, h2) == 0) { 963 freeaddrinfo(res); 964 return (1); 965 } 966 } 967 968 /* No match. */ 969 freeaddrinfo(res); 970 return (0); 971 } 972 973 /* 974 * Return the hostname associated with the supplied address. 975 * Do a reverse lookup as well for security. If a loop cannot 976 * be found, pack the numeric IP address into the string. 977 * 978 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 979 * if af == AF_INET6. 980 */ 981 static char * 982 __gethostloop(raddr, salen) 983 struct sockaddr *raddr; 984 socklen_t salen; 985 { 986 static char remotehost[NI_MAXHOST]; 987 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 988 struct addrinfo hints, *res, *r; 989 int error; 990 #ifdef NI_WITHSCOPEID 991 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 992 #else 993 const int niflags = NI_NUMERICHOST; 994 #endif 995 996 _DIAGASSERT(raddr != NULL); 997 998 h1[0] = remotehost[0] = '\0'; 999 if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), 1000 NULL, 0, NI_NAMEREQD) != 0) 1001 return (NULL); 1002 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 1003 niflags) != 0) 1004 return (NULL); 1005 1006 /* 1007 * Look up the name and check that the supplied 1008 * address is in the list 1009 */ 1010 memset(&hints, 0, sizeof(hints)); 1011 hints.ai_family = raddr->sa_family; 1012 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 1013 hints.ai_flags = AI_CANONNAME; 1014 res = NULL; 1015 error = getaddrinfo(remotehost, "0", &hints, &res); 1016 if (error) 1017 return (NULL); 1018 1019 for (r = res; r; r = r->ai_next) { 1020 h2[0] = '\0'; 1021 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 1022 NULL, 0, niflags) != 0) 1023 continue; 1024 if (strcmp(h1, h2) == 0) { 1025 freeaddrinfo(res); 1026 return (remotehost); 1027 } 1028 } 1029 1030 /* 1031 * either the DNS adminstrator has made a configuration 1032 * mistake, or someone has attempted to spoof us 1033 */ 1034 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 1035 h1, res->ai_canonname ? res->ai_canonname : remotehost); 1036 freeaddrinfo(res); 1037 return (NULL); 1038 } 1039