rshd.c revision 76094
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 76094 2001-04-28 07:43:01Z 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 <err.h> 69#include <errno.h> 70#include <fcntl.h> 71#include <libutil.h> 72#include <paths.h> 73#include <pwd.h> 74#include <signal.h> 75#include <stdio.h> 76#include <stdlib.h> 77#include <string.h> 78#include <syslog.h> 79#include <unistd.h> 80#include <login_cap.h> 81 82#ifdef USE_PAM 83#include <security/pam_appl.h> 84#include <sys/wait.h> 85 86static int export_pam_environment __P((void)); 87static int ok_to_export __P((const char *)); 88 89static pam_handle_t *pamh; 90static char **environ_pam; 91 92#define PAM_END { \ 93 if ((retcode = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \ 94 syslog(LOG_ERR|LOG_AUTH, "pam_setcred: %s", pam_strerror(pamh, retcode)); \ 95 if ((retcode = pam_close_session(pamh,0)) != PAM_SUCCESS) \ 96 syslog(LOG_ERR|LOG_AUTH, "pam_close_session: %s", pam_strerror(pamh, retcode)); \ 97 if ((retcode = pam_end(pamh, retcode)) != PAM_SUCCESS) \ 98 syslog(LOG_ERR|LOG_AUTH, "pam_end: %s", pam_strerror(pamh, retcode)); \ 99} 100#endif /* USE_PAM */ 101 102/* wrapper for KAME-special getnameinfo() */ 103#ifndef NI_WITHSCOPEID 104#define NI_WITHSCOPEID 0 105#endif 106 107int keepalive = 1; 108int log_success; /* If TRUE, log all successful accesses */ 109int sent_null; 110int no_delay; 111#ifdef CRYPT 112int doencrypt = 0; 113#endif 114 115union sockunion { 116 struct sockinet { 117 u_char si_len; 118 u_char si_family; 119 u_short si_port; 120 } su_si; 121 struct sockaddr_in su_sin; 122 struct sockaddr_in6 su_sin6; 123}; 124#define su_len su_si.si_len 125#define su_family su_si.si_family 126#define su_port su_si.si_port 127 128void doit __P((union sockunion *)); 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 errx(1, "rlogind: getsockname: %m"); /* XXX */ 364 } 365 authopts = KOPT_DO_MUTUAL; 366 rc = krb_recvauth(authopts, 0, ticket, 367 "rcmd", instance, &fromaddr, 368 &local_addr, kdata, "", schedule, 369 version); 370 des_set_key(&kdata->session, schedule); 371 } 372#endif 373 getstr(remuser, sizeof(remuser), "remuser"); 374 375 getstr(locuser, sizeof(locuser), "locuser"); 376 getstr(cmdbuf, sizeof(cmdbuf), "command"); 377 378#ifdef USE_PAM 379 retcode = pam_start("rsh", locuser, &conv, &pamh); 380 if (retcode != PAM_SUCCESS) { 381 syslog(LOG_ERR|LOG_AUTH, "pam_start: %s", pam_strerror(pamh, retcode)); 382 errx(1, "Login incorrect."); 383 } 384 385 retcode = pam_set_item (pamh, PAM_RUSER, remuser); 386 if (retcode != PAM_SUCCESS) { 387 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(PAM_RUSER): %s", pam_strerror(pamh, retcode)); 388 errx(1, "Login incorrect."); 389 } 390 retcode = pam_set_item (pamh, PAM_RHOST, fromhost); 391 if (retcode != PAM_SUCCESS) { 392 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(PAM_RHOST): %s", pam_strerror(pamh, retcode)); 393 errx(1, "Login incorrect."); 394 } 395 retcode = pam_set_item (pamh, PAM_TTY, "tty"); 396 if (retcode != PAM_SUCCESS) { 397 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(PAM_TTY): %s", pam_strerror(pamh, retcode)); 398 errx(1, "Login incorrect."); 399 } 400 401 retcode = pam_authenticate(pamh, 0); 402 if (retcode == PAM_SUCCESS) { 403 if ((retcode = pam_get_item(pamh, PAM_USER, (const void **) &cp)) == PAM_SUCCESS) { 404 strncpy(locuser, cp, sizeof(locuser)); 405 locuser[sizeof(locuser) - 1] = '\0'; 406 } else 407 syslog(LOG_ERR|LOG_AUTH, "pam_get_item(PAM_USER): %s", 408 pam_strerror(pamh, retcode)); 409 retcode = pam_acct_mgmt(pamh, 0); 410 } 411 if (retcode != PAM_SUCCESS) { 412 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: permission denied (%s). cmd='%.80s'", 413 remuser, fromhost, locuser, pam_strerror(pamh, retcode), cmdbuf); 414 errx(1, "Login incorrect."); 415 } 416#endif /* USE_PAM */ 417 418 setpwent(); 419 pwd = getpwnam(locuser); 420 if (pwd == NULL) { 421 syslog(LOG_INFO|LOG_AUTH, 422 "%s@%s as %s: unknown login. cmd='%.80s'", 423 remuser, fromhost, locuser, cmdbuf); 424 if (errorstr == NULL) 425 errorstr = "Login incorrect."; 426 errx(1, errorstr, fromhost); 427 } 428 429#ifndef USE_PAM 430 if (errorstr || 431 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) || 432 iruserok_sa(fromp, fromp->su_len, pwd->pw_uid == 0, 433 remuser, locuser) < 0) { 434 if (__rcmd_errstr) 435 syslog(LOG_INFO|LOG_AUTH, 436 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 437 remuser, fromhost, locuser, __rcmd_errstr, 438 cmdbuf); 439 else 440 syslog(LOG_INFO|LOG_AUTH, 441 "%s@%s as %s: permission denied. cmd='%.80s'", 442 remuser, fromhost, locuser, cmdbuf); 443 if (errorstr == NULL) 444 errorstr = "Login incorrect."; 445 errx(1, errorstr, fromhost); 446 } 447#endif /* USE_PAM */ 448 449 lc = login_getpwclass(pwd); 450 if (pwd->pw_uid) 451 auth_checknologin(lc); 452 453 if (chdir(pwd->pw_dir) < 0) { 454 if (chdir("/") < 0 || 455 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) { 456 syslog(LOG_INFO|LOG_AUTH, 457 "%s@%s as %s: no home directory. cmd='%.80s'", 458 remuser, fromhost, locuser, cmdbuf); 459 errx(0, "No remote home directory."); 460 } 461 pwd->pw_dir = "/"; 462 } 463 464 if (lc != NULL && fromp->su_family == AF_INET) { /*XXX*/ 465 char remote_ip[MAXHOSTNAMELEN]; 466 467 strncpy(remote_ip, numericname, 468 sizeof(remote_ip) - 1); 469 remote_ip[sizeof(remote_ip) - 1] = 0; 470 if (!auth_hostok(lc, fromhost, remote_ip)) { 471 syslog(LOG_INFO|LOG_AUTH, 472 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 473 remuser, fromhost, locuser, __rcmd_errstr, 474 cmdbuf); 475 errx(1, "Login incorrect."); 476 } 477 if (!auth_timeok(lc, time(NULL))) 478 errx(1, "Logins not available right now"); 479 } 480#if BSD > 43 481 /* before fork, while we're session leader */ 482 if (setlogin(pwd->pw_name) < 0) 483 syslog(LOG_ERR, "setlogin() failed: %m"); 484#endif 485 486 /* 487 * PAM modules might add supplementary groups in 488 * pam_setcred(), so initialize them first. 489 * But we need to open the session as root. 490 */ 491 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) { 492 syslog(LOG_ERR, "setusercontext: %m"); 493 exit(1); 494 } 495 496#ifdef USE_PAM 497 if ((retcode = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 498 syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, retcode)); 499 } else if ((retcode = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 500 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, retcode)); 501 } 502#endif /* USE_PAM */ 503 504 (void) write(STDERR_FILENO, "\0", 1); 505 sent_null = 1; 506 507 if (port) { 508 if (pipe(pv) < 0) 509 errx(1, "Can't make pipe."); 510#ifdef CRYPT 511 if (doencrypt) { 512 if (pipe(pv1) < 0) 513 errx(1, "Can't make 2nd pipe."); 514 if (pipe(pv2) < 0) 515 errx(1, "Can't make 3rd pipe."); 516 } 517#endif 518 pid = fork(); 519 if (pid == -1) 520 errx(1, "Can't fork; try again."); 521 if (pid) { 522#ifdef CRYPT 523 if (doencrypt) { 524 static char msg[] = SECURE_MESSAGE; 525 (void) close(pv1[1]); 526 (void) close(pv2[1]); 527 des_enc_write(s, msg, sizeof(msg) - 1, 528 schedule, &kdata->session); 529 530 } else 531#endif 532 { 533 (void) close(0); 534 (void) close(1); 535 } 536 (void) close(2); 537 (void) close(pv[1]); 538 539 FD_ZERO(&readfrom); 540 FD_SET(s, &readfrom); 541 FD_SET(pv[0], &readfrom); 542 if (pv[0] > s) 543 nfd = pv[0]; 544 else 545 nfd = s; 546#ifdef CRYPT 547 if (doencrypt) { 548 FD_ZERO(&writeto); 549 FD_SET(pv2[0], &writeto); 550 FD_SET(pv1[0], &readfrom); 551 552 nfd = MAX(nfd, pv2[0]); 553 nfd = MAX(nfd, pv1[0]); 554 } else 555#endif 556 ioctl(pv[0], FIONBIO, (char *)&one); 557 558 /* should set s nbio! */ 559 nfd++; 560 do { 561 ready = readfrom; 562#ifdef CRYPT 563 if (doencrypt) { 564 wready = writeto; 565 if (select(nfd, &ready, 566 &wready, (fd_set *) 0, 567 (struct timeval *) 0) < 0) 568 break; 569 } else 570#endif 571 if (select(nfd, &ready, (fd_set *)0, 572 (fd_set *)0, (struct timeval *)0) < 0) 573 break; 574 if (FD_ISSET(s, &ready)) { 575 int ret; 576#ifdef CRYPT 577 if (doencrypt) 578 ret = des_enc_read(s, &sig, 1, 579 schedule, &kdata->session); 580 else 581#endif 582 ret = read(s, &sig, 1); 583 if (ret <= 0) 584 FD_CLR(s, &readfrom); 585 else 586 killpg(pid, sig); 587 } 588 if (FD_ISSET(pv[0], &ready)) { 589 errno = 0; 590 cc = read(pv[0], buf, sizeof(buf)); 591 if (cc <= 0) { 592 shutdown(s, 1+1); 593 FD_CLR(pv[0], &readfrom); 594 } else { 595#ifdef CRYPT 596 if (doencrypt) 597 (void) 598 des_enc_write(s, buf, cc, 599 schedule, &kdata->session); 600 else 601#endif 602 (void) 603 write(s, buf, cc); 604 } 605 } 606#ifdef CRYPT 607 if (doencrypt && FD_ISSET(pv1[0], &ready)) { 608 errno = 0; 609 cc = read(pv1[0], buf, sizeof(buf)); 610 if (cc <= 0) { 611 shutdown(pv1[0], 1+1); 612 FD_CLR(pv1[0], &readfrom); 613 } else 614 (void) des_enc_write(STDOUT_FILENO, 615 buf, cc, 616 schedule, &kdata->session); 617 } 618 619 if (doencrypt && FD_ISSET(pv2[0], &wready)) { 620 errno = 0; 621 cc = des_enc_read(STDIN_FILENO, 622 buf, sizeof(buf), 623 schedule, &kdata->session); 624 if (cc <= 0) { 625 shutdown(pv2[0], 1+1); 626 FD_CLR(pv2[0], &writeto); 627 } else 628 (void) write(pv2[0], buf, cc); 629 } 630#endif 631 632 } while (FD_ISSET(s, &readfrom) || 633#ifdef CRYPT 634 (doencrypt && FD_ISSET(pv1[0], &readfrom)) || 635#endif 636 FD_ISSET(pv[0], &readfrom)); 637#ifdef USE_PAM 638 PAM_END; 639#endif /* USE_PAM */ 640 exit(0); 641 } 642 setpgrp(0, getpid()); 643 (void) close(s); 644 (void) close(pv[0]); 645#ifdef CRYPT 646 if (doencrypt) { 647 close(pv1[0]); close(pv2[0]); 648 dup2(pv1[1], 1); 649 dup2(pv2[1], 0); 650 close(pv1[1]); 651 close(pv2[1]); 652 } 653#endif 654 dup2(pv[1], 2); 655 close(pv[1]); 656 } 657#ifdef USE_PAM 658 else { 659 pid = fork(); 660 if (pid == -1) 661 errx(1, "Can't fork; try again."); 662 if (pid) { 663 /* Parent. */ 664 wait(NULL); 665 PAM_END; 666 exit(0); 667 } 668 } 669#endif /* USE_PAM */ 670 671 if (*pwd->pw_shell == '\0') 672 pwd->pw_shell = _PATH_BSHELL; 673 environ = envinit; 674 675#ifdef USE_PAM 676 /* 677 * Add any environmental variables that the 678 * PAM modules may have set. 679 */ 680 environ_pam = pam_getenvlist(pamh); 681 if (environ_pam) 682 export_pam_environment(); 683 if ((retcode = pam_end(pamh, PAM_DATA_SILENT)) != PAM_SUCCESS) 684 syslog(LOG_ERR|LOG_AUTH, "pam_end: %s", pam_strerror(pamh, retcode)); 685#endif /* USE_PAM */ 686 687 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 688 strcat(path, _PATH_DEFPATH); 689 strncat(shell, pwd->pw_shell, sizeof(shell)-7); 690 strncat(username, pwd->pw_name, sizeof(username)-6); 691 cp = strrchr(pwd->pw_shell, '/'); 692 if (cp) 693 cp++; 694 else 695 cp = pwd->pw_shell; 696 697 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL & ~LOGIN_SETGROUP) != 0) { 698 syslog(LOG_ERR, "setusercontext: %m"); 699 exit(1); 700 } 701 login_close(lc); 702 703 endpwent(); 704 if (log_success || pwd->pw_uid == 0) { 705 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'", 706 remuser, fromhost, locuser, cmdbuf); 707 } 708 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 709 perror(pwd->pw_shell); 710 exit(1); 711} 712 713void 714getstr(buf, cnt, err) 715 char *buf, *err; 716 int cnt; 717{ 718 char c; 719 720 do { 721 if (read(STDIN_FILENO, &c, 1) != 1) 722 exit(1); 723 *buf++ = c; 724 errx(1, "%s too long", err); 725 } while (c != 0); 726} 727 728/* 729 * Check whether host h is in our local domain, 730 * defined as sharing the last two components of the domain part, 731 * or the entire domain part if the local domain has only one component. 732 * If either name is unqualified (contains no '.'), 733 * assume that the host is local, as it will be 734 * interpreted as such. 735 */ 736int 737local_domain(h) 738 char *h; 739{ 740 char localhost[MAXHOSTNAMELEN]; 741 char *p1, *p2; 742 743 localhost[0] = 0; 744 (void) gethostname(localhost, sizeof(localhost) - 1); 745 localhost[sizeof(localhost) - 1] = '\0'; 746 p1 = topdomain(localhost); 747 p2 = topdomain(h); 748 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 749 return (1); 750 return (0); 751} 752 753char * 754topdomain(h) 755 char *h; 756{ 757 char *p, *maybe = NULL; 758 int dots = 0; 759 760 for (p = h + strlen(h); p >= h; p--) { 761 if (*p == '.') { 762 if (++dots == 2) 763 return (p); 764 maybe = p; 765 } 766 } 767 return (maybe); 768} 769 770#ifdef USE_PAM 771static int 772export_pam_environment() 773{ 774 char **pp; 775 776 for (pp = environ_pam; *pp != NULL; pp++) { 777 if (ok_to_export(*pp)) 778 (void) putenv(*pp); 779 free(*pp); 780 } 781 return PAM_SUCCESS; 782} 783 784/* 785 * Sanity checks on PAM environmental variables: 786 * - Make sure there is an '=' in the string. 787 * - Make sure the string doesn't run on too long. 788 * - Do not export certain variables. This list was taken from the 789 * Solaris pam_putenv(3) man page. 790 */ 791static int 792ok_to_export(s) 793 const char *s; 794{ 795 static const char *noexport[] = { 796 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH", 797 "IFS", "PATH", NULL 798 }; 799 const char **pp; 800 size_t n; 801 802 if (strlen(s) > 1024 || strchr(s, '=') == NULL) 803 return 0; 804 if (strncmp(s, "LD_", 3) == 0) 805 return 0; 806 for (pp = noexport; *pp != NULL; pp++) { 807 n = strlen(*pp); 808 if (s[n] == '=' && strncmp(s, *pp, n) == 0) 809 return 0; 810 } 811 return 1; 812} 813#endif /* USE_PAM */ 814 815void 816usage() 817{ 818 819 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS); 820 exit(2); 821} 822