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