rshd.c revision 24349
1253923Smarcel/*- 2263409Smarcel * Copyright (c) 1988, 1989, 1992, 1993, 1994 3253923Smarcel * The Regents of the University of California. All rights reserved. 4253923Smarcel * 5253923Smarcel * Redistribution and use in source and binary forms, with or without 6253923Smarcel * modification, are permitted provided that the following conditions 7253923Smarcel * are met: 8253923Smarcel * 1. Redistributions of source code must retain the above copyright 9253923Smarcel * notice, this list of conditions and the following disclaimer. 10253923Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11253923Smarcel * notice, this list of conditions and the following disclaimer in the 12253923Smarcel * documentation and/or other materials provided with the distribution. 13253923Smarcel * 3. All advertising materials mentioning features or use of this software 14253923Smarcel * must display the following acknowledgement: 15253923Smarcel * This product includes software developed by the University of 16253923Smarcel * California, Berkeley and its contributors. 17253923Smarcel * 4. Neither the name of the University nor the names of its contributors 18253923Smarcel * may be used to endorse or promote products derived from this software 19253923Smarcel * without specific prior written permission. 20253923Smarcel * 21253923Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22253923Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23253923Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24253923Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25253923Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26253923Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27253923Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28253923Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29253923Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30253923Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31263409Smarcel * SUCH DAMAGE. 32263382Smarcel * 33263537Smarcel * $Id: rshd.c,v 1.13 1997/02/22 14:22:23 peter Exp $ 34253923Smarcel */ 35253923Smarcel 36253923Smarcel#ifndef lint 37263537Smarcelstatic char copyright[] = 38263466Smarcel"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\ 39253923Smarcel The Regents of the University of California. All rights reserved.\n"; 40253923Smarcel#endif /* not lint */ 41268161Smarcel 42263382Smarcel#ifndef lint 43253923Smarcelstatic char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94"; 44253923Smarcel#endif /* not lint */ 45263487Smarcel 46263487Smarcel/* 47263487Smarcel * remote shell server: 48263487Smarcel * [port]\0 49263672Smarcel * remuser\0 50263487Smarcel * locuser\0 51263672Smarcel * command\0 52263487Smarcel * data 53263487Smarcel */ 54263487Smarcel#include <sys/param.h> 55263487Smarcel#include <sys/ioctl.h> 56263487Smarcel#include <sys/time.h> 57263487Smarcel#include <sys/socket.h> 58263487Smarcel 59263487Smarcel#include <netinet/in_systm.h> 60263487Smarcel#include <netinet/in.h> 61263487Smarcel#include <netinet/ip.h> 62263487Smarcel#include <arpa/inet.h> 63263409Smarcel#include <netdb.h> 64263537Smarcel 65253923Smarcel#include <errno.h> 66263487Smarcel#include <fcntl.h> 67263487Smarcel#include <paths.h> 68263487Smarcel#include <pwd.h> 69263487Smarcel#include <signal.h> 70263487Smarcel#include <stdio.h> 71263487Smarcel#include <stdlib.h> 72263487Smarcel#include <string.h> 73263487Smarcel#include <syslog.h> 74263487Smarcel#include <unistd.h> 75263487Smarcel 76263487Smarcelint keepalive = 1; 77263487Smarcelint check_all; 78263487Smarcelint log_success; /* If TRUE, log all successful accesses */ 79263487Smarcelint sent_null; 80253923Smarcel 81253923Smarcelvoid doit __P((struct sockaddr_in *)); 82253923Smarcelvoid error __P((const char *, ...)); 83263409Smarcelvoid getstr __P((char *, int, char *)); 84253923Smarcelint local_domain __P((char *)); 85263409Smarcelchar *topdomain __P((char *)); 86263409Smarcelvoid usage __P((void)); 87263409Smarcel 88263409Smarcel#ifdef KERBEROS 89253923Smarcel#include <des.h> 90253923Smarcel#include <kerberosIV/krb.h> 91253923Smarcel#define VERSION_SIZE 9 92253923Smarcel#define SECURE_MESSAGE "This rsh session is using DES encryption for all transmissions.\r\n" 93253923Smarcel#define OPTIONS "alnkvxL" 94253923Smarcelchar authbuf[sizeof(AUTH_DAT)]; 95263409Smarcelchar tickbuf[sizeof(KTEXT_ST)]; 96253923Smarcelint doencrypt, use_kerberos, vacuous; 97253923SmarcelKey_schedule schedule; 98253923Smarcel#else 99253923Smarcel#define OPTIONS "alnL" 100253923Smarcel#endif 101253923Smarcel 102253923Smarcelint 103263537Smarcelmain(argc, argv) 104263537Smarcel int argc; 105263537Smarcel char *argv[]; 106263537Smarcel{ 107263537Smarcel extern int __check_rhosts_file; 108263537Smarcel struct linger linger; 109263537Smarcel int ch, on = 1, fromlen; 110268161Smarcel struct sockaddr_in from; 111268161Smarcel 112263537Smarcel openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON); 113263537Smarcel 114263537Smarcel opterr = 0; 115263537Smarcel while ((ch = getopt(argc, argv, OPTIONS)) != -1) 116263537Smarcel switch (ch) { 117263537Smarcel case 'a': 118263537Smarcel check_all = 1; 119263537Smarcel break; 120263537Smarcel case 'l': 121263537Smarcel __check_rhosts_file = 0; 122263537Smarcel break; 123263537Smarcel case 'n': 124263537Smarcel keepalive = 0; 125263537Smarcel break; 126263537Smarcel#ifdef KERBEROS 127263537Smarcel case 'k': 128263409Smarcel use_kerberos = 1; 129253923Smarcel break; 130263487Smarcel 131263487Smarcel case 'v': 132253923Smarcel vacuous = 1; 133263414Smarcel break; 134263487Smarcel 135263487Smarcel#ifdef CRYPT 136263487Smarcel case 'x': 137263487Smarcel doencrypt = 1; 138263414Smarcel break; 139263487Smarcel#endif 140263487Smarcel#endif 141263414Smarcel case 'L': 142263414Smarcel log_success = 1; 143263414Smarcel break; 144263487Smarcel case '?': 145263414Smarcel default: 146263461Smarcel usage(); 147263466Smarcel break; 148263466Smarcel } 149263466Smarcel 150263466Smarcel argc -= optind; 151263487Smarcel argv += optind; 152263466Smarcel 153263466Smarcel#ifdef KERBEROS 154253923Smarcel if (use_kerberos && vacuous) { 155253923Smarcel syslog(LOG_ERR, "only one of -k and -v allowed"); 156253923Smarcel exit(2); 157253923Smarcel } 158253923Smarcel#ifdef CRYPT 159253923Smarcel if (doencrypt && !use_kerberos) { 160253923Smarcel syslog(LOG_ERR, "-k is required for -x"); 161263409Smarcel exit(2); 162253923Smarcel } 163253923Smarcel#endif 164263829Smarcel#endif 165263829Smarcel 166263829Smarcel fromlen = sizeof (from); 167263829Smarcel if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 168263829Smarcel syslog(LOG_ERR, "getpeername: %m"); 169263829Smarcel _exit(1); 170263829Smarcel } 171263653Smarcel if (keepalive && 172263844Smarcel setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, 173263442Smarcel sizeof(on)) < 0) 174263442Smarcel syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 175272030Smarcel linger.l_onoff = 1; 176263442Smarcel linger.l_linger = 60; /* XXX */ 177263442Smarcel if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger, 178263442Smarcel sizeof (linger)) < 0) 179268161Smarcel syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m"); 180253923Smarcel doit(&from); 181263442Smarcel /* NOTREACHED */ 182253923Smarcel} 183268161Smarcel 184268161Smarcelchar username[20] = "USER="; 185263442Smarcelchar homedir[64] = "HOME="; 186253923Smarcelchar shell[64] = "SHELL="; 187char path[100] = "PATH="; 188char *envinit[] = 189 {homedir, shell, path, username, 0}; 190char **environ; 191 192void 193doit(fromp) 194 struct sockaddr_in *fromp; 195{ 196 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */ 197 struct hostent *hp; 198 struct passwd *pwd; 199 u_short port; 200 fd_set ready, readfrom; 201 int cc, nfd, pv[2], pid, s; 202 int one = 1; 203 char *hostname, *errorstr, *errorhost; 204 char *cp, sig, buf[BUFSIZ]; 205 char cmdbuf[NCARGS+1], locuser[16], remuser[16]; 206 char remotehost[2 * MAXHOSTNAMELEN + 1]; 207 char fromhost[2 * MAXHOSTNAMELEN + 1]; 208 209#ifdef KERBEROS 210 AUTH_DAT *kdata = (AUTH_DAT *) NULL; 211 KTEXT ticket = (KTEXT) NULL; 212 char instance[INST_SZ], version[VERSION_SIZE]; 213 struct sockaddr_in fromaddr; 214 int rc; 215 long authopts; 216 int pv1[2], pv2[2]; 217 fd_set wready, writeto; 218 219 fromaddr = *fromp; 220#endif 221 222 (void) signal(SIGINT, SIG_DFL); 223 (void) signal(SIGQUIT, SIG_DFL); 224 (void) signal(SIGTERM, SIG_DFL); 225#ifdef DEBUG 226 { int t = open(_PATH_TTY, 2); 227 if (t >= 0) { 228 ioctl(t, TIOCNOTTY, (char *)0); 229 (void) close(t); 230 } 231 } 232#endif 233 fromp->sin_port = ntohs((u_short)fromp->sin_port); 234 if (fromp->sin_family != AF_INET) { 235 syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", 236 fromp->sin_family); 237 exit(1); 238 } 239#ifdef IP_OPTIONS 240 { 241 u_char optbuf[BUFSIZ/3]; 242 int optsize = sizeof(optbuf), ipproto, i; 243 struct protoent *ip; 244 245 if ((ip = getprotobyname("ip")) != NULL) 246 ipproto = ip->p_proto; 247 else 248 ipproto = IPPROTO_IP; 249 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) && 250 optsize != 0) { 251 for (i = 0; i < optsize; ) { 252 u_char c = optbuf[i]; 253 if (c == IPOPT_LSRR || c == IPOPT_SSRR) { 254 syslog(LOG_NOTICE, 255 "Connection refused from %s with IP option %s", 256 inet_ntoa(fromp->sin_addr), 257 c == IPOPT_LSRR ? "LSRR" : "SSRR"); 258 exit(1); 259 } 260 if (c == IPOPT_EOL) 261 break; 262 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1]; 263 } 264 } 265 } 266#endif 267 268#ifdef KERBEROS 269 if (!use_kerberos) 270#endif 271 if (fromp->sin_port >= IPPORT_RESERVED || 272 fromp->sin_port < IPPORT_RESERVED/2) { 273 syslog(LOG_NOTICE|LOG_AUTH, 274 "Connection from %s on illegal port %u", 275 inet_ntoa(fromp->sin_addr), 276 fromp->sin_port); 277 exit(1); 278 } 279 280 (void) alarm(60); 281 port = 0; 282 for (;;) { 283 char c; 284 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) { 285 if (cc < 0) 286 syslog(LOG_NOTICE, "read: %m"); 287 shutdown(0, 1+1); 288 exit(1); 289 } 290 if (c== 0) 291 break; 292 port = port * 10 + c - '0'; 293 } 294 295 (void) alarm(0); 296 if (port != 0) { 297 int lport = IPPORT_RESERVED - 1; 298 s = rresvport(&lport); 299 if (s < 0) { 300 syslog(LOG_ERR, "can't get stderr port: %m"); 301 exit(1); 302 } 303#ifdef KERBEROS 304 if (!use_kerberos) 305#endif 306 if (port >= IPPORT_RESERVED) { 307 syslog(LOG_ERR, "2nd port not reserved\n"); 308 exit(1); 309 } 310 fromp->sin_port = htons(port); 311 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) { 312 syslog(LOG_INFO, "connect second port %d: %m", port); 313 exit(1); 314 } 315 } 316 317#ifdef KERBEROS 318 if (vacuous) { 319 error("rshd: remote host requires Kerberos authentication\n"); 320 exit(1); 321 } 322#endif 323 324#ifdef notdef 325 /* from inetd, socket is already on 0, 1, 2 */ 326 dup2(f, 0); 327 dup2(f, 1); 328 dup2(f, 2); 329#endif 330 errorstr = NULL; 331 hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr), 332 fromp->sin_family); 333 if (hp) { 334 /* 335 * If name returned by gethostbyaddr is in our domain, 336 * attempt to verify that we haven't been fooled by someone 337 * in a remote net; look up the name and check that this 338 * address corresponds to the name. 339 */ 340 strncpy(fromhost, hp->h_name, sizeof(fromhost) - 1); 341 fromhost[sizeof(fromhost) - 1] = 0; 342 hostname = fromhost; 343#ifdef KERBEROS 344 if (!use_kerberos) 345#endif 346 if (check_all || local_domain(hp->h_name)) { 347 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 348 remotehost[sizeof(remotehost) - 1] = 0; 349 errorhost = remotehost; 350 hp = gethostbyname(remotehost); 351 if (hp == NULL) { 352 syslog(LOG_INFO, 353 "Couldn't look up address for %s", 354 remotehost); 355 errorstr = 356 "Couldn't look up address for your host (%s)\n"; 357 strncpy(fromhost, inet_ntoa(fromp->sin_addr), 358 sizeof(fromhost) - 1); 359 fromhost[sizeof(fromhost) - 1] = 0; 360 hostname = fromhost; 361 } else for (; ; hp->h_addr_list++) { 362 if (hp->h_addr_list[0] == NULL) { 363 syslog(LOG_NOTICE, 364 "Host addr %s not listed for host %s", 365 inet_ntoa(fromp->sin_addr), 366 hp->h_name); 367 errorstr = 368 "Host address mismatch for %s\n"; 369 strncpy(fromhost, inet_ntoa(fromp->sin_addr), 370 sizeof(fromhost) - 1); 371 fromhost[sizeof(fromhost) - 1] = 0; 372 hostname = fromhost; 373 break; 374 } 375 if (!bcmp(hp->h_addr_list[0], 376 (caddr_t)&fromp->sin_addr, 377 sizeof(fromp->sin_addr))) { 378 hostname = remotehost; 379 break; 380 } 381 } 382 } 383 } else { 384 strncpy(fromhost, inet_ntoa(fromp->sin_addr), 385 sizeof(fromhost) - 1); 386 fromhost[sizeof(fromhost) - 1] = 0; 387 errorhost = hostname = fromhost; 388 } 389 390#ifdef KERBEROS 391 if (use_kerberos) { 392 kdata = (AUTH_DAT *) authbuf; 393 ticket = (KTEXT) tickbuf; 394 authopts = 0L; 395 strcpy(instance, "*"); 396 version[VERSION_SIZE - 1] = '\0'; 397#ifdef CRYPT 398 if (doencrypt) { 399 struct sockaddr_in local_addr; 400 rc = sizeof(local_addr); 401 if (getsockname(0, (struct sockaddr *)&local_addr, 402 &rc) < 0) { 403 syslog(LOG_ERR, "getsockname: %m"); 404 error("rlogind: getsockname: %m"); 405 exit(1); 406 } 407 authopts = KOPT_DO_MUTUAL; 408 rc = krb_recvauth(authopts, 0, ticket, 409 "rcmd", instance, &fromaddr, 410 &local_addr, kdata, "", schedule, 411 version); 412 des_set_key_krb(&kdata->session, schedule); 413 } else 414#endif 415 rc = krb_recvauth(authopts, 0, ticket, "rcmd", 416 instance, &fromaddr, 417 (struct sockaddr_in *) 0, 418 kdata, "", NULL, version); 419 if (rc != KSUCCESS) { 420 error("Kerberos authentication failure: %s\n", 421 krb_err_txt[rc]); 422 exit(1); 423 } 424 } else 425#endif 426 getstr(remuser, sizeof(remuser), "remuser"); 427 428 getstr(locuser, sizeof(locuser), "locuser"); 429 getstr(cmdbuf, sizeof(cmdbuf), "command"); 430 setpwent(); 431 pwd = getpwnam(locuser); 432 if (pwd == NULL) { 433 syslog(LOG_INFO|LOG_AUTH, 434 "%s@%s as %s: unknown login. cmd='%.80s'", 435 remuser, hostname, locuser, cmdbuf); 436 if (errorstr == NULL) 437 errorstr = "Login incorrect.\n"; 438 goto fail; 439 } 440 if (chdir(pwd->pw_dir) < 0) { 441 (void) chdir("/"); 442#ifdef notdef 443 syslog(LOG_INFO|LOG_AUTH, 444 "%s@%s as %s: no home directory. cmd='%.80s'", 445 remuser, hostname, locuser, cmdbuf); 446 error("No remote directory.\n"); 447 exit(1); 448#endif 449 } 450 451#ifdef KERBEROS 452 if (use_kerberos) { 453 if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') { 454 if (kuserok(kdata, locuser) != 0) { 455 syslog(LOG_INFO|LOG_AUTH, 456 "Kerberos rsh denied to %s.%s@%s", 457 kdata->pname, kdata->pinst, kdata->prealm); 458 error("Permission denied.\n"); 459 exit(1); 460 } 461 } 462 } else 463#endif 464 465 if (errorstr || 466 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) || 467 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' && 468 iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0, 469 remuser, locuser) < 0)) { 470 if (__rcmd_errstr) 471 syslog(LOG_INFO|LOG_AUTH, 472 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 473 remuser, hostname, locuser, __rcmd_errstr, 474 cmdbuf); 475 else 476 syslog(LOG_INFO|LOG_AUTH, 477 "%s@%s as %s: permission denied. cmd='%.80s'", 478 remuser, hostname, locuser, cmdbuf); 479fail: 480 if (errorstr == NULL) 481 errorstr = "Permission denied.\n"; 482 error(errorstr, errorhost); 483 exit(1); 484 } 485 486 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) { 487 error("Logins currently disabled.\n"); 488 exit(1); 489 } 490#if BSD > 43 491 /* before fork, while we're session leader */ 492 if (setlogin(pwd->pw_name) < 0) 493 syslog(LOG_ERR, "setlogin() failed: %m"); 494#endif 495 496 (void) write(STDERR_FILENO, "\0", 1); 497 sent_null = 1; 498 499 if (port) { 500 if (pipe(pv) < 0) { 501 error("Can't make pipe.\n"); 502 exit(1); 503 } 504#ifdef CRYPT 505#ifdef KERBEROS 506 if (doencrypt) { 507 if (pipe(pv1) < 0) { 508 error("Can't make 2nd pipe.\n"); 509 exit(1); 510 } 511 if (pipe(pv2) < 0) { 512 error("Can't make 3rd pipe.\n"); 513 exit(1); 514 } 515 } 516#endif 517#endif 518 pid = fork(); 519 if (pid == -1) { 520 error("Can't fork; try again.\n"); 521 exit(1); 522 } 523 if (pid) { 524#ifdef CRYPT 525#ifdef KERBEROS 526 if (doencrypt) { 527 static char msg[] = SECURE_MESSAGE; 528 (void) close(pv1[1]); 529 (void) close(pv2[1]); 530 des_write(s, msg, sizeof(msg) - 1); 531 532 } else 533#endif 534#endif 535 { 536 (void) close(0); 537 (void) close(1); 538 } 539 (void) close(2); 540 (void) close(pv[1]); 541 542 FD_ZERO(&readfrom); 543 FD_SET(s, &readfrom); 544 FD_SET(pv[0], &readfrom); 545 if (pv[0] > s) 546 nfd = pv[0]; 547 else 548 nfd = s; 549#ifdef CRYPT 550#ifdef KERBEROS 551 if (doencrypt) { 552 FD_ZERO(&writeto); 553 FD_SET(pv2[0], &writeto); 554 FD_SET(pv1[0], &readfrom); 555 556 nfd = MAX(nfd, pv2[0]); 557 nfd = MAX(nfd, pv1[0]); 558 } else 559#endif 560#endif 561 ioctl(pv[0], FIONBIO, (char *)&one); 562 563 /* should set s nbio! */ 564 nfd++; 565 do { 566 ready = readfrom; 567#ifdef CRYPT 568#ifdef KERBEROS 569 if (doencrypt) { 570 wready = writeto; 571 if (select(nfd, &ready, 572 &wready, (fd_set *) 0, 573 (struct timeval *) 0) < 0) 574 break; 575 } else 576#endif 577#endif 578 if (select(nfd, &ready, (fd_set *)0, 579 (fd_set *)0, (struct timeval *)0) < 0) 580 break; 581 if (FD_ISSET(s, &ready)) { 582 int ret; 583#ifdef CRYPT 584#ifdef KERBEROS 585 if (doencrypt) 586 ret = des_read(s, &sig, 1); 587 else 588#endif 589#endif 590 ret = read(s, &sig, 1); 591 if (ret <= 0) 592 FD_CLR(s, &readfrom); 593 else 594 killpg(pid, sig); 595 } 596 if (FD_ISSET(pv[0], &ready)) { 597 errno = 0; 598 cc = read(pv[0], buf, sizeof(buf)); 599 if (cc <= 0) { 600 shutdown(s, 1+1); 601 FD_CLR(pv[0], &readfrom); 602 } else { 603#ifdef CRYPT 604#ifdef KERBEROS 605 if (doencrypt) 606 (void) 607 des_write(s, buf, cc); 608 else 609#endif 610#endif 611 (void) 612 write(s, buf, cc); 613 } 614 } 615#ifdef CRYPT 616#ifdef KERBEROS 617 if (doencrypt && FD_ISSET(pv1[0], &ready)) { 618 errno = 0; 619 cc = read(pv1[0], buf, sizeof(buf)); 620 if (cc <= 0) { 621 shutdown(pv1[0], 1+1); 622 FD_CLR(pv1[0], &readfrom); 623 } else 624 (void) des_write(STDOUT_FILENO, 625 buf, cc); 626 } 627 628 if (doencrypt && FD_ISSET(pv2[0], &wready)) { 629 errno = 0; 630 cc = des_read(STDIN_FILENO, 631 buf, sizeof(buf)); 632 if (cc <= 0) { 633 shutdown(pv2[0], 1+1); 634 FD_CLR(pv2[0], &writeto); 635 } else 636 (void) write(pv2[0], buf, cc); 637 } 638#endif 639#endif 640 641 } while (FD_ISSET(s, &readfrom) || 642#ifdef CRYPT 643#ifdef KERBEROS 644 (doencrypt && FD_ISSET(pv1[0], &readfrom)) || 645#endif 646#endif 647 FD_ISSET(pv[0], &readfrom)); 648 exit(0); 649 } 650 setpgrp(0, getpid()); 651 (void) close(s); 652 (void) close(pv[0]); 653#ifdef CRYPT 654#ifdef KERBEROS 655 if (doencrypt) { 656 close(pv1[0]); close(pv2[0]); 657 dup2(pv1[1], 1); 658 dup2(pv2[1], 0); 659 close(pv1[1]); 660 close(pv2[1]); 661 } 662#endif 663#endif 664 dup2(pv[1], 2); 665 close(pv[1]); 666 } 667 if (*pwd->pw_shell == '\0') 668 pwd->pw_shell = _PATH_BSHELL; 669 (void) setgid((gid_t)pwd->pw_gid); 670 initgroups(pwd->pw_name, pwd->pw_gid); 671 (void) setuid((uid_t)pwd->pw_uid); 672 environ = envinit; 673 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 674 strcat(path, _PATH_DEFPATH); 675 strncat(shell, pwd->pw_shell, sizeof(shell)-7); 676 strncat(username, pwd->pw_name, sizeof(username)-6); 677 cp = strrchr(pwd->pw_shell, '/'); 678 if (cp) 679 cp++; 680 else 681 cp = pwd->pw_shell; 682 endpwent(); 683 if (log_success || pwd->pw_uid == 0) { 684#ifdef KERBEROS 685 if (use_kerberos) 686 syslog(LOG_INFO|LOG_AUTH, 687 "Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'", 688 kdata->pname, kdata->pinst, kdata->prealm, 689 hostname, locuser, cmdbuf); 690 else 691#endif 692 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'", 693 remuser, hostname, locuser, cmdbuf); 694 } 695 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 696 perror(pwd->pw_shell); 697 exit(1); 698} 699 700/* 701 * Report error to client. Note: can't be used until second socket has 702 * connected to client, or older clients will hang waiting for that 703 * connection first. 704 */ 705#if __STDC__ 706#include <stdarg.h> 707#else 708#include <varargs.h> 709#endif 710 711void 712#if __STDC__ 713error(const char *fmt, ...) 714#else 715error(fmt, va_alist) 716 char *fmt; 717 va_dcl 718#endif 719{ 720 va_list ap; 721 int len; 722 char *bp, buf[BUFSIZ]; 723#if __STDC__ 724 va_start(ap, fmt); 725#else 726 va_start(ap); 727#endif 728 bp = buf; 729 if (sent_null == 0) { 730 *bp++ = 1; 731 len = 1; 732 } else 733 len = 0; 734 (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap); 735 (void)write(STDERR_FILENO, buf, len + strlen(bp)); 736} 737 738void 739getstr(buf, cnt, err) 740 char *buf, *err; 741 int cnt; 742{ 743 char c; 744 745 do { 746 if (read(STDIN_FILENO, &c, 1) != 1) 747 exit(1); 748 *buf++ = c; 749 if (--cnt == 0) { 750 error("%s too long\n", err); 751 exit(1); 752 } 753 } while (c != 0); 754} 755 756/* 757 * Check whether host h is in our local domain, 758 * defined as sharing the last two components of the domain part, 759 * or the entire domain part if the local domain has only one component. 760 * If either name is unqualified (contains no '.'), 761 * assume that the host is local, as it will be 762 * interpreted as such. 763 */ 764int 765local_domain(h) 766 char *h; 767{ 768 char localhost[MAXHOSTNAMELEN]; 769 char *p1, *p2; 770 771 localhost[0] = 0; 772 (void) gethostname(localhost, sizeof(localhost)); 773 p1 = topdomain(localhost); 774 p2 = topdomain(h); 775 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 776 return (1); 777 return (0); 778} 779 780char * 781topdomain(h) 782 char *h; 783{ 784 char *p, *maybe = NULL; 785 int dots = 0; 786 787 for (p = h + strlen(h); p >= h; p--) { 788 if (*p == '.') { 789 if (++dots == 2) 790 return (p); 791 maybe = p; 792 } 793 } 794 return (maybe); 795} 796 797void 798usage() 799{ 800 801 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS); 802 exit(2); 803} 804