rshd.c revision 25674
1/*- 2 * Copyright (c) 1988, 1989, 1992, 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 * $Id: rshd.c,v 1.16 1997/04/23 03:06:47 davidn Exp $ 34 */ 35 36#ifndef lint 37static char copyright[] = 38"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43static char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94"; 44#endif /* not lint */ 45 46/* 47 * remote shell server: 48 * [port]\0 49 * remuser\0 50 * locuser\0 51 * command\0 52 * data 53 */ 54#include <sys/param.h> 55#include <sys/ioctl.h> 56#include <sys/time.h> 57#include <sys/socket.h> 58 59#include <netinet/in_systm.h> 60#include <netinet/in.h> 61#include <netinet/ip.h> 62#include <arpa/inet.h> 63#include <netdb.h> 64 65#include <errno.h> 66#include <fcntl.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#ifdef LOGIN_CAP 76#include <login_cap.h> 77#endif 78 79int keepalive = 1; 80int check_all; 81int log_success; /* If TRUE, log all successful accesses */ 82int sent_null; 83 84void doit __P((struct sockaddr_in *)); 85void error __P((const char *, ...)); 86void getstr __P((char *, int, char *)); 87int local_domain __P((char *)); 88char *topdomain __P((char *)); 89void usage __P((void)); 90 91#ifdef KERBEROS 92#include <des.h> 93#include <kerberosIV/krb.h> 94#define VERSION_SIZE 9 95#define SECURE_MESSAGE "This rsh session is using DES encryption for all transmissions.\r\n" 96#define OPTIONS "alnkvxL" 97char authbuf[sizeof(AUTH_DAT)]; 98char tickbuf[sizeof(KTEXT_ST)]; 99int doencrypt, use_kerberos, vacuous; 100Key_schedule schedule; 101#else 102#define OPTIONS "alnL" 103#endif 104 105int 106main(argc, argv) 107 int argc; 108 char *argv[]; 109{ 110 extern int __check_rhosts_file; 111 struct linger linger; 112 int ch, on = 1, fromlen; 113 struct sockaddr_in from; 114 115 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON); 116 117 opterr = 0; 118 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 119 switch (ch) { 120 case 'a': 121 check_all = 1; 122 break; 123 case 'l': 124 __check_rhosts_file = 0; 125 break; 126 case 'n': 127 keepalive = 0; 128 break; 129#ifdef KERBEROS 130 case 'k': 131 use_kerberos = 1; 132 break; 133 134 case 'v': 135 vacuous = 1; 136 break; 137 138#ifdef CRYPT 139 case 'x': 140 doencrypt = 1; 141 break; 142#endif 143#endif 144 case 'L': 145 log_success = 1; 146 break; 147 case '?': 148 default: 149 usage(); 150 break; 151 } 152 153 argc -= optind; 154 argv += optind; 155 156#ifdef KERBEROS 157 if (use_kerberos && vacuous) { 158 syslog(LOG_ERR, "only one of -k and -v allowed"); 159 exit(2); 160 } 161#ifdef CRYPT 162 if (doencrypt && !use_kerberos) { 163 syslog(LOG_ERR, "-k is required for -x"); 164 exit(2); 165 } 166#endif 167#endif 168 169 fromlen = sizeof (from); 170 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 171 syslog(LOG_ERR, "getpeername: %m"); 172 _exit(1); 173 } 174 if (keepalive && 175 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, 176 sizeof(on)) < 0) 177 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 178 linger.l_onoff = 1; 179 linger.l_linger = 60; /* XXX */ 180 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger, 181 sizeof (linger)) < 0) 182 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m"); 183 doit(&from); 184 /* NOTREACHED */ 185} 186 187char username[20] = "USER="; 188char homedir[64] = "HOME="; 189char shell[64] = "SHELL="; 190char path[100] = "PATH="; 191char *envinit[] = 192 {homedir, shell, path, username, 0}; 193char **environ; 194 195void 196doit(fromp) 197 struct sockaddr_in *fromp; 198{ 199 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */ 200 struct hostent *hp; 201 struct passwd *pwd; 202 u_short port; 203 fd_set ready, readfrom; 204 int cc, nfd, pv[2], pid, s; 205 int one = 1; 206 char *hostname, *errorstr, *errorhost; 207 char *cp, sig, buf[BUFSIZ]; 208 char cmdbuf[NCARGS+1], locuser[16], remuser[16]; 209 char remotehost[2 * MAXHOSTNAMELEN + 1]; 210 char fromhost[2 * MAXHOSTNAMELEN + 1]; 211#ifdef LOGIN_CAP 212 login_cap_t *lc; 213#endif 214 215#ifdef KERBEROS 216 AUTH_DAT *kdata = (AUTH_DAT *) NULL; 217 KTEXT ticket = (KTEXT) NULL; 218 char instance[INST_SZ], version[VERSION_SIZE]; 219 struct sockaddr_in fromaddr; 220 int rc; 221 long authopts; 222 int pv1[2], pv2[2]; 223 fd_set wready, writeto; 224 225 fromaddr = *fromp; 226#endif 227 228 (void) signal(SIGINT, SIG_DFL); 229 (void) signal(SIGQUIT, SIG_DFL); 230 (void) signal(SIGTERM, SIG_DFL); 231#ifdef DEBUG 232 { int t = open(_PATH_TTY, 2); 233 if (t >= 0) { 234 ioctl(t, TIOCNOTTY, (char *)0); 235 (void) close(t); 236 } 237 } 238#endif 239 fromp->sin_port = ntohs((u_short)fromp->sin_port); 240 if (fromp->sin_family != AF_INET) { 241 syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", 242 fromp->sin_family); 243 exit(1); 244 } 245#ifdef IP_OPTIONS 246 { 247 u_char optbuf[BUFSIZ/3]; 248 int optsize = sizeof(optbuf), ipproto, i; 249 struct protoent *ip; 250 251 if ((ip = getprotobyname("ip")) != NULL) 252 ipproto = ip->p_proto; 253 else 254 ipproto = IPPROTO_IP; 255 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) && 256 optsize != 0) { 257 for (i = 0; i < optsize; ) { 258 u_char c = optbuf[i]; 259 if (c == IPOPT_LSRR || c == IPOPT_SSRR) { 260 syslog(LOG_NOTICE, 261 "Connection refused from %s with IP option %s", 262 inet_ntoa(fromp->sin_addr), 263 c == IPOPT_LSRR ? "LSRR" : "SSRR"); 264 exit(1); 265 } 266 if (c == IPOPT_EOL) 267 break; 268 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1]; 269 } 270 } 271 } 272#endif 273 274#ifdef KERBEROS 275 if (!use_kerberos) 276#endif 277 if (fromp->sin_port >= IPPORT_RESERVED || 278 fromp->sin_port < IPPORT_RESERVED/2) { 279 syslog(LOG_NOTICE|LOG_AUTH, 280 "Connection from %s on illegal port %u", 281 inet_ntoa(fromp->sin_addr), 282 fromp->sin_port); 283 exit(1); 284 } 285 286 (void) alarm(60); 287 port = 0; 288 for (;;) { 289 char c; 290 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) { 291 if (cc < 0) 292 syslog(LOG_NOTICE, "read: %m"); 293 shutdown(0, 1+1); 294 exit(1); 295 } 296 if (c== 0) 297 break; 298 port = port * 10 + c - '0'; 299 } 300 301 (void) alarm(0); 302 if (port != 0) { 303 int lport = IPPORT_RESERVED - 1; 304 s = rresvport(&lport); 305 if (s < 0) { 306 syslog(LOG_ERR, "can't get stderr port: %m"); 307 exit(1); 308 } 309#ifdef KERBEROS 310 if (!use_kerberos) 311#endif 312 if (port >= IPPORT_RESERVED || 313 port < IPPORT_RESERVED/2) { 314 syslog(LOG_NOTICE|LOG_AUTH, 315 "2nd socket from %s on unreserved port %u", 316 inet_ntoa(fromp->sin_addr), 317 port); 318 exit(1); 319 } 320 fromp->sin_port = htons(port); 321 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) { 322 syslog(LOG_INFO, "connect second port %d: %m", port); 323 exit(1); 324 } 325 } 326 327#ifdef KERBEROS 328 if (vacuous) { 329 error("rshd: remote host requires Kerberos authentication\n"); 330 exit(1); 331 } 332#endif 333 334#ifdef notdef 335 /* from inetd, socket is already on 0, 1, 2 */ 336 dup2(f, 0); 337 dup2(f, 1); 338 dup2(f, 2); 339#endif 340 errorstr = NULL; 341 hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr), 342 fromp->sin_family); 343 if (hp) { 344 /* 345 * If name returned by gethostbyaddr is in our domain, 346 * attempt to verify that we haven't been fooled by someone 347 * in a remote net; look up the name and check that this 348 * address corresponds to the name. 349 */ 350 strncpy(fromhost, hp->h_name, sizeof(fromhost) - 1); 351 fromhost[sizeof(fromhost) - 1] = 0; 352 hostname = fromhost; 353#ifdef KERBEROS 354 if (!use_kerberos) 355#endif 356 if (check_all || local_domain(hp->h_name)) { 357 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 358 remotehost[sizeof(remotehost) - 1] = 0; 359 errorhost = remotehost; 360 hp = gethostbyname(remotehost); 361 if (hp == NULL) { 362 syslog(LOG_INFO, 363 "Couldn't look up address for %s", 364 remotehost); 365 errorstr = 366 "Couldn't look up address for your host (%s)\n"; 367 strncpy(fromhost, inet_ntoa(fromp->sin_addr), 368 sizeof(fromhost) - 1); 369 fromhost[sizeof(fromhost) - 1] = 0; 370 hostname = fromhost; 371 } else for (; ; hp->h_addr_list++) { 372 if (hp->h_addr_list[0] == NULL) { 373 syslog(LOG_NOTICE, 374 "Host addr %s not listed for host %s", 375 inet_ntoa(fromp->sin_addr), 376 hp->h_name); 377 errorstr = 378 "Host address mismatch for %s\n"; 379 strncpy(fromhost, inet_ntoa(fromp->sin_addr), 380 sizeof(fromhost) - 1); 381 fromhost[sizeof(fromhost) - 1] = 0; 382 hostname = fromhost; 383 break; 384 } 385 if (!bcmp(hp->h_addr_list[0], 386 (caddr_t)&fromp->sin_addr, 387 sizeof(fromp->sin_addr))) { 388 hostname = remotehost; 389 break; 390 } 391 } 392 } 393 } else { 394 strncpy(fromhost, inet_ntoa(fromp->sin_addr), 395 sizeof(fromhost) - 1); 396 fromhost[sizeof(fromhost) - 1] = 0; 397 errorhost = hostname = fromhost; 398 } 399 400#ifdef KERBEROS 401 if (use_kerberos) { 402 kdata = (AUTH_DAT *) authbuf; 403 ticket = (KTEXT) tickbuf; 404 authopts = 0L; 405 strcpy(instance, "*"); 406 version[VERSION_SIZE - 1] = '\0'; 407#ifdef CRYPT 408 if (doencrypt) { 409 struct sockaddr_in local_addr; 410 rc = sizeof(local_addr); 411 if (getsockname(0, (struct sockaddr *)&local_addr, 412 &rc) < 0) { 413 syslog(LOG_ERR, "getsockname: %m"); 414 error("rlogind: getsockname: %m"); 415 exit(1); 416 } 417 authopts = KOPT_DO_MUTUAL; 418 rc = krb_recvauth(authopts, 0, ticket, 419 "rcmd", instance, &fromaddr, 420 &local_addr, kdata, "", schedule, 421 version); 422 des_set_key_krb(&kdata->session, schedule); 423 } else 424#endif 425 rc = krb_recvauth(authopts, 0, ticket, "rcmd", 426 instance, &fromaddr, 427 (struct sockaddr_in *) 0, 428 kdata, "", NULL, version); 429 if (rc != KSUCCESS) { 430 error("Kerberos authentication failure: %s\n", 431 krb_err_txt[rc]); 432 exit(1); 433 } 434 } else 435#endif 436 getstr(remuser, sizeof(remuser), "remuser"); 437 438 getstr(locuser, sizeof(locuser), "locuser"); 439 getstr(cmdbuf, sizeof(cmdbuf), "command"); 440 setpwent(); 441 pwd = getpwnam(locuser); 442 if (pwd == NULL) { 443 syslog(LOG_INFO|LOG_AUTH, 444 "%s@%s as %s: unknown login. cmd='%.80s'", 445 remuser, hostname, locuser, cmdbuf); 446 if (errorstr == NULL) 447 errorstr = "Login incorrect.\n"; 448 goto fail; 449 } 450#ifdef LOGIN_CAP 451 lc = login_getpwclass(pwd); 452#endif 453 if (chdir(pwd->pw_dir) < 0) { 454#ifdef LOGIN_CAP 455 if (chdir("/") < 0 || 456 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) { 457 syslog(LOG_INFO|LOG_AUTH, 458 "%s@%s as %s: no home directory. cmd='%.80s'", 459 remuser, hostname, locuser, cmdbuf); 460 error("No remote home directory.\n"); 461 exit(0); 462 } 463#else 464 (void) chdir("/"); 465#ifdef notdef 466 syslog(LOG_INFO|LOG_AUTH, 467 "%s@%s as %s: no home directory. cmd='%.80s'", 468 remuser, hostname, locuser, cmdbuf); 469 error("No remote directory.\n"); 470 exit(1); 471#endif 472#endif 473 pwd->pw_dir = "/"; 474 } 475 476#ifdef KERBEROS 477 if (use_kerberos) { 478 if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') { 479 if (kuserok(kdata, locuser) != 0) { 480 syslog(LOG_INFO|LOG_AUTH, 481 "Kerberos rsh denied to %s.%s@%s", 482 kdata->pname, kdata->pinst, kdata->prealm); 483 error("Permission denied.\n"); 484 exit(1); 485 } 486 } 487 } else 488#endif 489 490 if (errorstr || 491 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) || 492 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' && 493 iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0, 494 remuser, locuser) < 0)) { 495 if (__rcmd_errstr) 496 syslog(LOG_INFO|LOG_AUTH, 497 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 498 remuser, hostname, locuser, __rcmd_errstr, 499 cmdbuf); 500 else 501 syslog(LOG_INFO|LOG_AUTH, 502 "%s@%s as %s: permission denied. cmd='%.80s'", 503 remuser, hostname, locuser, cmdbuf); 504fail: 505 if (errorstr == NULL) 506 errorstr = "Permission denied.\n"; 507 error(errorstr, errorhost); 508 exit(1); 509 } 510 511 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) { 512 error("Logins currently disabled.\n"); 513 exit(1); 514 } 515#ifdef LOGIN_CAP 516 if (lc != NULL) { 517 char remote_ip[MAXHOSTNAMELEN]; 518 519 strncpy(remote_ip, inet_ntoa(fromp->sin_addr), 520 sizeof(remote_ip) - 1); 521 remote_ip[sizeof(remote_ip) - 1] = 0; 522 if (!auth_hostok(lc, fromhost, remote_ip)) { 523 syslog(LOG_INFO|LOG_AUTH, 524 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 525 remuser, hostname, locuser, __rcmd_errstr, 526 cmdbuf); 527 error("Permission denied.\n"); 528 exit(1); 529 } 530 if (!auth_timeok(lc, time(NULL))) { 531 error("Logins not available right now\n"); 532 exit(1); 533 } 534 } 535#endif /* !LOGIN_CAP */ 536#if BSD > 43 537 /* before fork, while we're session leader */ 538 if (setlogin(pwd->pw_name) < 0) 539 syslog(LOG_ERR, "setlogin() failed: %m"); 540#endif 541 542 (void) write(STDERR_FILENO, "\0", 1); 543 sent_null = 1; 544 545 if (port) { 546 if (pipe(pv) < 0) { 547 error("Can't make pipe.\n"); 548 exit(1); 549 } 550#ifdef CRYPT 551#ifdef KERBEROS 552 if (doencrypt) { 553 if (pipe(pv1) < 0) { 554 error("Can't make 2nd pipe.\n"); 555 exit(1); 556 } 557 if (pipe(pv2) < 0) { 558 error("Can't make 3rd pipe.\n"); 559 exit(1); 560 } 561 } 562#endif 563#endif 564 pid = fork(); 565 if (pid == -1) { 566 error("Can't fork; try again.\n"); 567 exit(1); 568 } 569 if (pid) { 570#ifdef CRYPT 571#ifdef KERBEROS 572 if (doencrypt) { 573 static char msg[] = SECURE_MESSAGE; 574 (void) close(pv1[1]); 575 (void) close(pv2[1]); 576 des_write(s, msg, sizeof(msg) - 1); 577 578 } else 579#endif 580#endif 581 { 582 (void) close(0); 583 (void) close(1); 584 } 585 (void) close(2); 586 (void) close(pv[1]); 587 588 FD_ZERO(&readfrom); 589 FD_SET(s, &readfrom); 590 FD_SET(pv[0], &readfrom); 591 if (pv[0] > s) 592 nfd = pv[0]; 593 else 594 nfd = s; 595#ifdef CRYPT 596#ifdef KERBEROS 597 if (doencrypt) { 598 FD_ZERO(&writeto); 599 FD_SET(pv2[0], &writeto); 600 FD_SET(pv1[0], &readfrom); 601 602 nfd = MAX(nfd, pv2[0]); 603 nfd = MAX(nfd, pv1[0]); 604 } else 605#endif 606#endif 607 ioctl(pv[0], FIONBIO, (char *)&one); 608 609 /* should set s nbio! */ 610 nfd++; 611 do { 612 ready = readfrom; 613#ifdef CRYPT 614#ifdef KERBEROS 615 if (doencrypt) { 616 wready = writeto; 617 if (select(nfd, &ready, 618 &wready, (fd_set *) 0, 619 (struct timeval *) 0) < 0) 620 break; 621 } else 622#endif 623#endif 624 if (select(nfd, &ready, (fd_set *)0, 625 (fd_set *)0, (struct timeval *)0) < 0) 626 break; 627 if (FD_ISSET(s, &ready)) { 628 int ret; 629#ifdef CRYPT 630#ifdef KERBEROS 631 if (doencrypt) 632 ret = des_read(s, &sig, 1); 633 else 634#endif 635#endif 636 ret = read(s, &sig, 1); 637 if (ret <= 0) 638 FD_CLR(s, &readfrom); 639 else 640 killpg(pid, sig); 641 } 642 if (FD_ISSET(pv[0], &ready)) { 643 errno = 0; 644 cc = read(pv[0], buf, sizeof(buf)); 645 if (cc <= 0) { 646 shutdown(s, 1+1); 647 FD_CLR(pv[0], &readfrom); 648 } else { 649#ifdef CRYPT 650#ifdef KERBEROS 651 if (doencrypt) 652 (void) 653 des_write(s, buf, cc); 654 else 655#endif 656#endif 657 (void) 658 write(s, buf, cc); 659 } 660 } 661#ifdef CRYPT 662#ifdef KERBEROS 663 if (doencrypt && FD_ISSET(pv1[0], &ready)) { 664 errno = 0; 665 cc = read(pv1[0], buf, sizeof(buf)); 666 if (cc <= 0) { 667 shutdown(pv1[0], 1+1); 668 FD_CLR(pv1[0], &readfrom); 669 } else 670 (void) des_write(STDOUT_FILENO, 671 buf, cc); 672 } 673 674 if (doencrypt && FD_ISSET(pv2[0], &wready)) { 675 errno = 0; 676 cc = des_read(STDIN_FILENO, 677 buf, sizeof(buf)); 678 if (cc <= 0) { 679 shutdown(pv2[0], 1+1); 680 FD_CLR(pv2[0], &writeto); 681 } else 682 (void) write(pv2[0], buf, cc); 683 } 684#endif 685#endif 686 687 } while (FD_ISSET(s, &readfrom) || 688#ifdef CRYPT 689#ifdef KERBEROS 690 (doencrypt && FD_ISSET(pv1[0], &readfrom)) || 691#endif 692#endif 693 FD_ISSET(pv[0], &readfrom)); 694 exit(0); 695 } 696 setpgrp(0, getpid()); 697 (void) close(s); 698 (void) close(pv[0]); 699#ifdef CRYPT 700#ifdef KERBEROS 701 if (doencrypt) { 702 close(pv1[0]); close(pv2[0]); 703 dup2(pv1[1], 1); 704 dup2(pv2[1], 0); 705 close(pv1[1]); 706 close(pv2[1]); 707 } 708#endif 709#endif 710 dup2(pv[1], 2); 711 close(pv[1]); 712 } 713 if (*pwd->pw_shell == '\0') 714 pwd->pw_shell = _PATH_BSHELL; 715 environ = envinit; 716 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 717 strcat(path, _PATH_DEFPATH); 718 strncat(shell, pwd->pw_shell, sizeof(shell)-7); 719 strncat(username, pwd->pw_name, sizeof(username)-6); 720 cp = strrchr(pwd->pw_shell, '/'); 721 if (cp) 722 cp++; 723 else 724 cp = pwd->pw_shell; 725#ifdef LOGIN_CAP 726 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) { 727 syslog(LOG_ERR, "setusercontext: %m"); 728 exit(1); 729 } 730 login_close(lc); 731#else 732 (void) setgid((gid_t)pwd->pw_gid); 733 initgroups(pwd->pw_name, pwd->pw_gid); 734 (void) setuid((uid_t)pwd->pw_uid); 735#endif 736 endpwent(); 737 if (log_success || pwd->pw_uid == 0) { 738#ifdef KERBEROS 739 if (use_kerberos) 740 syslog(LOG_INFO|LOG_AUTH, 741 "Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'", 742 kdata->pname, kdata->pinst, kdata->prealm, 743 hostname, locuser, cmdbuf); 744 else 745#endif 746 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'", 747 remuser, hostname, locuser, cmdbuf); 748 } 749 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 750 perror(pwd->pw_shell); 751 exit(1); 752} 753 754/* 755 * Report error to client. Note: can't be used until second socket has 756 * connected to client, or older clients will hang waiting for that 757 * connection first. 758 */ 759#if __STDC__ 760#include <stdarg.h> 761#else 762#include <varargs.h> 763#endif 764 765void 766#if __STDC__ 767error(const char *fmt, ...) 768#else 769error(fmt, va_alist) 770 char *fmt; 771 va_dcl 772#endif 773{ 774 va_list ap; 775 int len; 776 char *bp, buf[BUFSIZ]; 777#if __STDC__ 778 va_start(ap, fmt); 779#else 780 va_start(ap); 781#endif 782 bp = buf; 783 if (sent_null == 0) { 784 *bp++ = 1; 785 len = 1; 786 } else 787 len = 0; 788 (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap); 789 (void)write(STDERR_FILENO, buf, len + strlen(bp)); 790} 791 792void 793getstr(buf, cnt, err) 794 char *buf, *err; 795 int cnt; 796{ 797 char c; 798 799 do { 800 if (read(STDIN_FILENO, &c, 1) != 1) 801 exit(1); 802 *buf++ = c; 803 if (--cnt == 0) { 804 error("%s too long\n", err); 805 exit(1); 806 } 807 } while (c != 0); 808} 809 810/* 811 * Check whether host h is in our local domain, 812 * defined as sharing the last two components of the domain part, 813 * or the entire domain part if the local domain has only one component. 814 * If either name is unqualified (contains no '.'), 815 * assume that the host is local, as it will be 816 * interpreted as such. 817 */ 818int 819local_domain(h) 820 char *h; 821{ 822 char localhost[MAXHOSTNAMELEN]; 823 char *p1, *p2; 824 825 localhost[0] = 0; 826 (void) gethostname(localhost, sizeof(localhost)); 827 p1 = topdomain(localhost); 828 p2 = topdomain(h); 829 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 830 return (1); 831 return (0); 832} 833 834char * 835topdomain(h) 836 char *h; 837{ 838 char *p, *maybe = NULL; 839 int dots = 0; 840 841 for (p = h + strlen(h); p >= h; p--) { 842 if (*p == '.') { 843 if (++dots == 2) 844 return (p); 845 maybe = p; 846 } 847 } 848 return (maybe); 849} 850 851void 852usage() 853{ 854 855 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS); 856 exit(2); 857} 858