1 /* $NetBSD: rshd.c,v 1.49 2011/10/30 16:54:58 christos Exp $ */ 2 3 /* 4 * Copyright (C) 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by WIDE Project and 18 * its contributors. 19 * 4. Neither the name of the project nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 /*- 37 * Copyright (c) 1988, 1989, 1992, 1993, 1994 38 * The Regents of the University of California. All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 */ 64 65 #include <sys/cdefs.h> 66 #ifndef lint 67 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\ 68 The Regents of the University of California. All rights reserved."); 69 #if 0 70 static char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94"; 71 #else 72 __RCSID("$NetBSD: rshd.c,v 1.49 2011/10/30 16:54:58 christos Exp $"); 73 #endif 74 #endif /* not lint */ 75 76 /* 77 * remote shell server: 78 * [port]\0 79 * remuser\0 80 * locuser\0 81 * command\0 82 * data 83 */ 84 #include <sys/param.h> 85 #include <sys/ioctl.h> 86 #include <sys/time.h> 87 #include <sys/socket.h> 88 89 #include <netinet/in_systm.h> 90 #include <netinet/in.h> 91 #include <netinet/ip.h> 92 #include <netinet/tcp.h> 93 #include <arpa/inet.h> 94 #include <netdb.h> 95 96 #include <errno.h> 97 #include <fcntl.h> 98 #include <paths.h> 99 #include <pwd.h> 100 #include <signal.h> 101 #include <stdio.h> 102 #include <stdlib.h> 103 #include <string.h> 104 #include <syslog.h> 105 #include <unistd.h> 106 #include <poll.h> 107 #ifdef LOGIN_CAP 108 #include <login_cap.h> 109 #endif 110 111 #ifdef USE_PAM 112 #include <security/pam_appl.h> 113 #include <security/openpam.h> 114 #include <sys/wait.h> 115 116 static struct pam_conv pamc = { openpam_nullconv, NULL }; 117 static pam_handle_t *pamh; 118 static int pam_err; 119 120 #define PAM_END do { \ 121 if ((pam_err = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \ 122 syslog(LOG_ERR|LOG_AUTH, "pam_setcred(): %s", \ 123 pam_strerror(pamh, pam_err)); \ 124 if ((pam_err = pam_close_session(pamh,0)) != PAM_SUCCESS) \ 125 syslog(LOG_ERR|LOG_AUTH, "pam_close_session(): %s", \ 126 pam_strerror(pamh, pam_err)); \ 127 if ((pam_err = pam_end(pamh, pam_err)) != PAM_SUCCESS) \ 128 syslog(LOG_ERR|LOG_AUTH, "pam_end(): %s", \ 129 pam_strerror(pamh, pam_err)); \ 130 } while (/*CONSTCOND*/0) 131 #else 132 #define PAM_END 133 #endif 134 135 static int keepalive = 1; 136 static int check_all; 137 static int log_success; /* If TRUE, log all successful accesses */ 138 static int sent_null; 139 140 __dead static void doit(struct sockaddr *); 141 __dead static void rshd_errx(int, const char *, ...) __printflike(2, 3); 142 static void getstr(char *, int, const char *); 143 static int local_domain(char *); 144 static char *topdomain(char *); 145 __dead static void usage(void); 146 147 #define OPTIONS "aLln" 148 extern int __check_rhosts_file; 149 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */ 150 static const char incorrect[] = "Login incorrect."; 151 152 int 153 main(int argc, char *argv[]) 154 { 155 struct linger linger; 156 int ch, on = 1; 157 socklen_t fromlen; 158 struct sockaddr_storage from; 159 struct protoent *proto; 160 161 openlog("rshd", LOG_PID, LOG_DAEMON); 162 163 opterr = 0; 164 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 165 switch (ch) { 166 case 'a': 167 check_all = 1; 168 break; 169 case 'l': 170 __check_rhosts_file = 0; 171 break; 172 case 'n': 173 keepalive = 0; 174 break; 175 case 'L': 176 log_success = 1; 177 break; 178 case '?': 179 default: 180 usage(); 181 break; 182 } 183 184 argc -= optind; 185 argv += optind; 186 187 fromlen = sizeof(from); /* xxx */ 188 if (getpeername(STDIN_FILENO, (struct sockaddr *)&from, &fromlen) < 0) { 189 syslog(LOG_ERR, "getpeername: %m"); 190 return EXIT_FAILURE; 191 } 192 #if 0 193 if (((struct sockaddr *)&from)->sa_family == AF_INET6 && 194 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr) && 195 sizeof(struct sockaddr_in) <= sizeof(from)) { 196 struct sockaddr_in sin; 197 struct sockaddr_in6 *sin6; 198 const int off = sizeof(struct sockaddr_in6) - 199 sizeof(struct sockaddr_in); 200 201 sin6 = (struct sockaddr_in6 *)&from; 202 (void)memset(&sin, 0, sizeof(sin)); 203 sin.sin_family = AF_INET; 204 sin.sin_len = sizeof(struct sockaddr_in); 205 (void)memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[off], 206 sizeof(sin.sin_addr)); 207 (void)memcpy(&from, &sin, sizeof(sin)); 208 fromlen = sin.sin_len; 209 } 210 #else 211 if (((struct sockaddr *)&from)->sa_family == AF_INET6 && 212 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) { 213 char hbuf[NI_MAXHOST]; 214 if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf, 215 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) { 216 strlcpy(hbuf, "invalid", sizeof(hbuf)); 217 } 218 syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)", 219 hbuf); 220 return EXIT_FAILURE; 221 } 222 #endif 223 if (keepalive && 224 setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, 225 sizeof(on)) < 0) 226 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 227 linger.l_onoff = 1; 228 linger.l_linger = 60; /* XXX */ 229 if (setsockopt(STDIN_FILENO, SOL_SOCKET, SO_LINGER, (char *)&linger, 230 sizeof (linger)) < 0) 231 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m"); 232 proto = getprotobyname("tcp"); 233 (void)setsockopt(STDIN_FILENO, proto->p_proto, TCP_NODELAY, &on, 234 sizeof(on)); 235 doit((struct sockaddr *)&from); 236 } 237 238 extern char **environ; 239 240 static void 241 doit(struct sockaddr *fromp) 242 { 243 struct passwd *pwd, pwres; 244 in_port_t port; 245 struct pollfd set[2]; 246 int cc, pv[2], pid, s = -1; /* XXX gcc */ 247 int one = 1; 248 char *hostname, *errorhost = NULL; /* XXX gcc */ 249 const char *cp; 250 char sig, buf[BUFSIZ]; 251 char cmdbuf[NCARGS+1], locuser[16], remuser[16]; 252 char remotehost[2 * MAXHOSTNAMELEN + 1]; 253 char hostnamebuf[2 * MAXHOSTNAMELEN + 1]; 254 #ifdef LOGIN_CAP 255 login_cap_t *lc; 256 #endif 257 char naddr[NI_MAXHOST]; 258 char saddr[NI_MAXHOST]; 259 char raddr[NI_MAXHOST]; 260 char pbuf[NI_MAXSERV]; 261 int af = fromp->sa_family; 262 u_int16_t *portp; 263 struct addrinfo hints, *res, *res0; 264 int gaierror; 265 const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; 266 const char *errormsg = NULL, *errorstr = NULL; 267 char pwbuf[1024]; 268 269 (void)signal(SIGINT, SIG_DFL); 270 (void)signal(SIGQUIT, SIG_DFL); 271 (void)signal(SIGTERM, SIG_DFL); 272 #ifdef DEBUG 273 { 274 int t = open(_PATH_TTY, O_RDWR); 275 if (t >= 0) { 276 ioctl(t, TIOCNOTTY, NULL); 277 (void)close(t); 278 } 279 } 280 #endif 281 switch (af) { 282 case AF_INET: 283 portp = &((struct sockaddr_in *)fromp)->sin_port; 284 break; 285 #ifdef INET6 286 case AF_INET6: 287 portp = &((struct sockaddr_in6 *)fromp)->sin6_port; 288 break; 289 #endif 290 default: 291 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af); 292 exit(EXIT_FAILURE); 293 } 294 if (getnameinfo(fromp, fromp->sa_len, naddr, sizeof(naddr), 295 pbuf, sizeof(pbuf), niflags) != 0) { 296 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af); 297 exit(EXIT_FAILURE); 298 } 299 #ifdef IP_OPTIONS 300 if (af == AF_INET) { 301 302 u_char optbuf[BUFSIZ/3]; 303 socklen_t optsize = sizeof(optbuf); 304 int ipproto; 305 unsigned int i; 306 struct protoent *ip; 307 308 if ((ip = getprotobyname("ip")) != NULL) 309 ipproto = ip->p_proto; 310 else 311 ipproto = IPPROTO_IP; 312 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) && 313 optsize != 0) { 314 for (i = 0; i < optsize;) { 315 u_char c = optbuf[i]; 316 if (c == IPOPT_LSRR || c == IPOPT_SSRR) { 317 syslog(LOG_NOTICE, 318 "Connection refused from %s " 319 "with IP option %s", 320 inet_ntoa(( 321 (struct sockaddr_in *)fromp)->sin_addr), 322 c == IPOPT_LSRR ? "LSRR" : "SSRR"); 323 exit(EXIT_FAILURE); 324 } 325 if (c == IPOPT_EOL) 326 break; 327 i += (c == IPOPT_NOP) ? 1 : optbuf[i + 1]; 328 } 329 } 330 } 331 #endif 332 if (ntohs(*portp) >= IPPORT_RESERVED 333 || ntohs(*portp) < IPPORT_RESERVED / 2) { 334 syslog(LOG_NOTICE|LOG_AUTH, 335 "Connection from %s on illegal port %u", 336 naddr, ntohs(*portp)); 337 exit(EXIT_FAILURE); 338 } 339 340 (void) alarm(60); 341 port = 0; 342 for (;;) { 343 char c; 344 345 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) { 346 if (cc < 0) 347 syslog(LOG_ERR, "read: %m"); 348 (void)shutdown(0, SHUT_RDWR); 349 exit(EXIT_FAILURE); 350 } 351 if (c == 0) 352 break; 353 port = port * 10 + c - '0'; 354 } 355 356 (void) alarm(0); 357 if (port != 0) { 358 int lport = IPPORT_RESERVED - 1; 359 s = rresvport_af(&lport, af); 360 if (s < 0) { 361 syslog(LOG_ERR, "can't get stderr port: %m"); 362 exit(EXIT_FAILURE); 363 } 364 if (port >= IPPORT_RESERVED) { 365 syslog(LOG_ERR, "2nd port not reserved"); 366 exit(EXIT_FAILURE); 367 } 368 *portp = htons(port); 369 if (connect(s, fromp, fromp->sa_len) < 0) { 370 syslog(LOG_ERR, "connect second port %d: %m", port); 371 exit(EXIT_FAILURE); 372 } 373 } 374 375 376 #ifdef notdef 377 /* from inetd, socket is already on 0, 1, 2 */ 378 (void)dup2(f, STDIN_FILENO); 379 (void)dup2(f, STDOUT_FILENO); 380 (void)dup2(f, STDERR_FILENO); 381 #endif 382 if (getnameinfo(fromp, fromp->sa_len, saddr, sizeof(saddr), 383 NULL, 0, NI_NAMEREQD) == 0) { 384 /* 385 * If name returned by getnameinfo is in our domain, 386 * attempt to verify that we haven't been fooled by someone 387 * in a remote net; look up the name and check that this 388 * address corresponds to the name. 389 */ 390 hostname = saddr; 391 res0 = NULL; 392 if (check_all || local_domain(saddr)) { 393 (void)strlcpy(remotehost, saddr, sizeof(remotehost)); 394 errorhost = remotehost; 395 (void)memset(&hints, 0, sizeof(hints)); 396 hints.ai_family = fromp->sa_family; 397 hints.ai_socktype = SOCK_STREAM; 398 hints.ai_flags = AI_CANONNAME; 399 gaierror = getaddrinfo(remotehost, pbuf, &hints, &res0); 400 if (gaierror) { 401 syslog(LOG_NOTICE, 402 "Couldn't look up address for %s: %s", 403 remotehost, gai_strerror(gaierror)); 404 errorstr = 405 "Couldn't look up address for your host (%s)\n"; 406 hostname = naddr; 407 } else { 408 for (res = res0; res; res = res->ai_next) { 409 if (res->ai_family != fromp->sa_family) 410 continue; 411 if (res->ai_addrlen != fromp->sa_len) 412 continue; 413 if (getnameinfo(res->ai_addr, 414 res->ai_addrlen, 415 raddr, sizeof(raddr), NULL, 0, 416 niflags) == 0 417 && strcmp(naddr, raddr) == 0) { 418 hostname = res->ai_canonname 419 ? res->ai_canonname 420 : saddr; 421 break; 422 } 423 } 424 if (res == NULL) { 425 syslog(LOG_NOTICE, 426 "Host addr %s not listed for host %s", 427 naddr, res0->ai_canonname 428 ? res0->ai_canonname 429 : saddr); 430 errorstr = 431 "Host address mismatch for %s\n"; 432 hostname = naddr; 433 } 434 } 435 } 436 (void)strlcpy(hostnamebuf, hostname, sizeof(hostnamebuf)); 437 hostname = hostnamebuf; 438 if (res0) 439 freeaddrinfo(res0); 440 } else { 441 (void)strlcpy(hostnamebuf, naddr, sizeof(hostnamebuf)); 442 errorhost = hostname = hostnamebuf; 443 } 444 445 (void)alarm(60); 446 getstr(remuser, sizeof(remuser), "remuser"); 447 getstr(locuser, sizeof(locuser), "locuser"); 448 getstr(cmdbuf, sizeof(cmdbuf), "command"); 449 (void)alarm(0); 450 451 #ifdef USE_PAM 452 pam_err = pam_start("rsh", locuser, &pamc, &pamh); 453 if (pam_err != PAM_SUCCESS) { 454 syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s", 455 pam_strerror(pamh, pam_err)); 456 rshd_errx(EXIT_FAILURE, incorrect); 457 } 458 459 if ((pam_err = pam_set_item(pamh, PAM_RUSER, remuser)) != PAM_SUCCESS || 460 (pam_err = pam_set_item(pamh, PAM_RHOST, hostname)) != PAM_SUCCESS){ 461 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s", 462 pam_strerror(pamh, pam_err)); 463 rshd_errx(EXIT_FAILURE, incorrect); 464 } 465 466 pam_err = pam_authenticate(pamh, 0); 467 if (pam_err == PAM_SUCCESS) { 468 if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) { 469 (void)strlcpy(locuser, cp, sizeof(locuser)); 470 /* XXX truncation! */ 471 } 472 pam_err = pam_acct_mgmt(pamh, 0); 473 } 474 if (pam_err != PAM_SUCCESS) { 475 errorstr = incorrect; 476 errormsg = pam_strerror(pamh, pam_err); 477 goto badlogin; 478 } 479 #endif /* USE_PAM */ 480 setpwent(); 481 if (getpwnam_r(locuser, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 || 482 pwd == NULL) { 483 syslog(LOG_INFO|LOG_AUTH, 484 "%s@%s as %s: unknown login. cmd='%.80s'", 485 remuser, hostname, locuser, cmdbuf); 486 if (errorstr == NULL) 487 errorstr = "Permission denied."; 488 rshd_errx(EXIT_FAILURE, errorstr, errorhost); 489 } 490 #ifdef LOGIN_CAP 491 lc = login_getclass(pwd ? pwd->pw_class : NULL); 492 #endif 493 494 if (chdir(pwd->pw_dir) < 0) { 495 if (chdir("/") < 0 496 #ifdef LOGIN_CAP 497 || login_getcapbool(lc, "requirehome", pwd->pw_uid ? 1 : 0) 498 #endif 499 ) { 500 syslog(LOG_INFO|LOG_AUTH, 501 "%s@%s as %s: no home directory. cmd='%.80s'", 502 remuser, hostname, locuser, cmdbuf); 503 rshd_errx(EXIT_SUCCESS, "No remote home directory."); 504 } 505 } 506 507 #ifndef USE_PAM 508 if (errorstr || 509 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' && 510 iruserok_sa(fromp, fromp->sa_len, pwd->pw_uid == 0, remuser, 511 locuser) < 0)) { 512 errormsg = __rcmd_errstr ? __rcmd_errstr : "unknown error"; 513 if (errorstr == NULL) 514 errorstr = "Permission denied."; 515 goto badlogin; 516 } 517 518 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) 519 rshd_errx(EXIT_FAILURE, "Logins currently disabled."); 520 #endif 521 522 #ifdef LOGIN_CAP 523 /* 524 * PAM modules might add supplementary groups in 525 * pam_setcred(), so initialize them first. 526 * But we need to open the session as root. 527 */ 528 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) { 529 syslog(LOG_ERR, "setusercontext: %m"); 530 exit(EXIT_FAILURE); 531 } 532 #else 533 initgroups(pwd->pw_name, pwd->pw_gid); 534 #endif 535 536 #ifdef USE_PAM 537 if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 538 syslog(LOG_ERR, "pam_open_session: %s", 539 pam_strerror(pamh, pam_err)); 540 } else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) 541 != PAM_SUCCESS) { 542 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err)); 543 } 544 #endif 545 546 (void)write(STDERR_FILENO, "\0", 1); 547 sent_null = 1; 548 549 if (port) { 550 if (pipe(pv) < 0) 551 rshd_errx(EXIT_FAILURE, "Can't make pipe. (%s)", 552 strerror(errno)); 553 pid = fork(); 554 if (pid == -1) 555 rshd_errx(EXIT_FAILURE, "Can't fork. (%s)", 556 strerror(errno)); 557 if (pid) { 558 (void)close(STDIN_FILENO); 559 (void)close(STDOUT_FILENO); 560 (void)close(STDERR_FILENO); 561 (void)close(pv[1]); 562 563 set[0].fd = s; 564 set[0].events = POLLIN; 565 set[1].fd = pv[0]; 566 set[1].events = POLLIN; 567 ioctl(pv[0], FIONBIO, (char *)&one); 568 569 /* should set s nbio! */ 570 do { 571 if (poll(set, 2, INFTIM) < 0) 572 break; 573 if (set[0].revents & POLLIN) { 574 int ret; 575 576 ret = read(s, &sig, 1); 577 if (ret <= 0) 578 set[0].events = 0; 579 else 580 killpg(pid, sig); 581 } 582 if (set[1].revents & POLLIN) { 583 errno = 0; 584 cc = read(pv[0], buf, sizeof(buf)); 585 if (cc <= 0) { 586 shutdown(s, SHUT_RDWR); 587 set[1].events = 0; 588 } else { 589 (void)write(s, buf, cc); 590 } 591 } 592 593 } while ((set[0].revents | set[1].revents) & POLLIN); 594 PAM_END; 595 exit(EXIT_SUCCESS); 596 } 597 (void)close(s); 598 (void)close(pv[0]); 599 (void)dup2(pv[1], STDERR_FILENO); 600 close(pv[1]); 601 } 602 #ifdef USE_PAM 603 else { 604 pid = fork(); 605 if (pid == -1) 606 rshd_errx(EXIT_FAILURE, "Can't fork. (%s)", 607 strerror(errno)); 608 if (pid) { 609 pid_t xpid; 610 int status; 611 if ((xpid = waitpid(pid, &status, 0)) != pid) { 612 pam_err = pam_close_session(pamh, 0); 613 if (pam_err != PAM_SUCCESS) { 614 syslog(LOG_ERR, 615 "pam_close_session: %s", 616 pam_strerror(pamh, pam_err)); 617 } 618 PAM_END; 619 if (xpid != -1) 620 syslog(LOG_WARNING, 621 "wrong PID: %d != %d", pid, xpid); 622 else 623 syslog(LOG_WARNING, 624 "wait pid=%d failed %m", pid); 625 exit(EXIT_FAILURE); 626 } 627 exit(EXIT_SUCCESS); 628 } 629 } 630 #endif 631 632 #ifdef F_CLOSEM 633 (void)fcntl(STDERR_FILENO + 1, F_CLOSEM, 0); 634 #else 635 for (fd = getdtablesize(); fd > STDERR_FILENO; fd--) 636 (void)close(fd); 637 #endif 638 if (setsid() == -1) 639 syslog(LOG_ERR, "setsid() failed: %m"); 640 #ifdef USE_PAM 641 if (setlogin(pwd->pw_name) < 0) 642 syslog(LOG_ERR, "setlogin() failed: %m"); 643 644 if (*pwd->pw_shell == '\0') 645 pwd->pw_shell = __UNCONST(_PATH_BSHELL); 646 647 (void)pam_setenv(pamh, "HOME", pwd->pw_dir, 1); 648 (void)pam_setenv(pamh, "SHELL", pwd->pw_shell, 1); 649 (void)pam_setenv(pamh, "USER", pwd->pw_name, 1); 650 (void)pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1); 651 environ = pam_getenvlist(pamh); 652 (void)pam_end(pamh, pam_err); 653 #else 654 #ifdef LOGIN_CAP 655 { 656 char *sh; 657 if ((sh = login_getcapstr(lc, "shell", NULL, NULL))) { 658 if(!(sh = strdup(sh))) { 659 syslog(LOG_ERR, "Cannot alloc mem"); 660 exit(EXIT_FAILURE); 661 } 662 pwd->pw_shell = sh; 663 } 664 } 665 #endif 666 { 667 static char *envinit[] = { NULL }; 668 environ = envinit; 669 } 670 setenv("PATH", _PATH_DEFPATH, 1); 671 setenv("HOME", pwd->pw_dir, 1); 672 setenv("SHELL", pwd->pw_shell, 1); 673 setenv("USER", pwd->pw_name, 1); 674 #endif 675 676 cp = strrchr(pwd->pw_shell, '/'); 677 if (cp) 678 cp++; 679 else 680 cp = pwd->pw_shell; 681 682 #ifdef LOGIN_CAP 683 if (setusercontext(lc, pwd, pwd->pw_uid, 684 LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) { 685 syslog(LOG_ERR, "setusercontext(): %m"); 686 exit(EXIT_FAILURE); 687 } 688 login_close(lc); 689 #else 690 (void)setgid((gid_t)pwd->pw_gid); 691 (void)setuid((uid_t)pwd->pw_uid); 692 #endif 693 endpwent(); 694 if (log_success || pwd->pw_uid == 0) { 695 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'", 696 remuser, hostname, locuser, cmdbuf); 697 } 698 (void)execl(pwd->pw_shell, cp, "-c", cmdbuf, NULL); 699 rshd_errx(EXIT_FAILURE, "%s: %s", pwd->pw_shell, strerror(errno)); 700 badlogin: 701 syslog(LOG_INFO|LOG_AUTH, 702 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 703 remuser, hostname, locuser, errormsg, cmdbuf); 704 rshd_errx(EXIT_FAILURE, errorstr, errorhost); 705 } 706 707 /* 708 * Report error to client. Note: can't be used until second socket has 709 * connected to client, or older clients will hang waiting for that 710 * connection first. 711 */ 712 713 #include <stdarg.h> 714 715 static void 716 rshd_errx(int error, const char *fmt, ...) 717 { 718 va_list ap; 719 int len, rv; 720 char *bp, buf[BUFSIZ]; 721 va_start(ap, fmt); 722 bp = buf; 723 if (sent_null == 0) { 724 *bp++ = 1; 725 len = 1; 726 } else 727 len = 0; 728 rv = vsnprintf(bp, sizeof(buf) - 2, fmt, ap); 729 bp[rv++] = '\n'; 730 (void)write(STDERR_FILENO, buf, len + rv); 731 va_end(ap); 732 exit(error); 733 } 734 735 static void 736 getstr(char *buf, int cnt, const char *err) 737 { 738 char c; 739 740 do { 741 if (read(STDIN_FILENO, &c, 1) != 1) 742 exit(EXIT_FAILURE); 743 *buf++ = c; 744 if (--cnt == 0) 745 rshd_errx(EXIT_FAILURE, "%s too long", err); 746 } while (c != 0); 747 } 748 749 /* 750 * Check whether host h is in our local domain, 751 * defined as sharing the last two components of the domain part, 752 * or the entire domain part if the local domain has only one component. 753 * If either name is unqualified (contains no '.'), 754 * assume that the host is local, as it will be 755 * interpreted as such. 756 */ 757 static int 758 local_domain(char *h) 759 { 760 char localhost[MAXHOSTNAMELEN + 1]; 761 char *p1, *p2; 762 763 localhost[0] = 0; 764 (void)gethostname(localhost, sizeof(localhost)); 765 localhost[sizeof(localhost) - 1] = '\0'; 766 p1 = topdomain(localhost); 767 p2 = topdomain(h); 768 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 769 return (1); 770 return (0); 771 } 772 773 static char * 774 topdomain(char *h) 775 { 776 char *p, *maybe = NULL; 777 int dots = 0; 778 779 for (p = h + strlen(h); p >= h; p--) { 780 if (*p == '.') { 781 if (++dots == 2) 782 return (p); 783 maybe = p; 784 } 785 } 786 return (maybe); 787 } 788 789 static void 790 usage(void) 791 { 792 793 syslog(LOG_ERR, "Usage: %s [-%s]", getprogname(), OPTIONS); 794 exit(EXIT_FAILURE); 795 } 796