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