1 /* $OpenBSD: session.c,v 1.294 2018/03/03 03:15:51 djm Exp $ */ 2 /* 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * 6 * As far as I am concerned, the code I have written for this software 7 * can be used freely for any purpose. Any derived versions of this 8 * software must be clearly marked as such, and if the derived work is 9 * incompatible with the protocol description in the RFC file, it must be 10 * called by a name other than "ssh" or "Secure Shell". 11 * 12 * SSH2 support by Markus Friedl. 13 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include <sys/types.h> 37 #include <sys/wait.h> 38 #include <sys/un.h> 39 #include <sys/stat.h> 40 #include <sys/socket.h> 41 #include <sys/queue.h> 42 43 #include <ctype.h> 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <grp.h> 47 #include <login_cap.h> 48 #include <netdb.h> 49 #include <paths.h> 50 #include <pwd.h> 51 #include <signal.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 #include <limits.h> 57 58 #include "xmalloc.h" 59 #include "ssh.h" 60 #include "ssh2.h" 61 #include "sshpty.h" 62 #include "packet.h" 63 #include "buffer.h" 64 #include "match.h" 65 #include "uidswap.h" 66 #include "compat.h" 67 #include "channels.h" 68 #include "key.h" 69 #include "cipher.h" 70 #include "kex.h" 71 #include "hostfile.h" 72 #include "auth.h" 73 #include "auth-options.h" 74 #include "authfd.h" 75 #include "pathnames.h" 76 #include "log.h" 77 #include "misc.h" 78 #include "servconf.h" 79 #include "sshlogin.h" 80 #include "serverloop.h" 81 #include "canohost.h" 82 #include "session.h" 83 #ifdef GSSAPI 84 #include "ssh-gss.h" 85 #endif 86 #include "monitor_wrap.h" 87 #include "sftp.h" 88 #include "atomicio.h" 89 90 #ifdef KRB5 91 #include <kafs.h> 92 #endif 93 94 #define IS_INTERNAL_SFTP(c) \ 95 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \ 96 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \ 97 c[sizeof(INTERNAL_SFTP_NAME) - 1] == ' ' || \ 98 c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\t')) 99 100 /* func */ 101 102 Session *session_new(void); 103 void session_set_fds(struct ssh *, Session *, int, int, int, int, int); 104 void session_pty_cleanup(Session *); 105 void session_proctitle(Session *); 106 int session_setup_x11fwd(struct ssh *, Session *); 107 int do_exec_pty(struct ssh *, Session *, const char *); 108 int do_exec_no_pty(struct ssh *, Session *, const char *); 109 int do_exec(struct ssh *, Session *, const char *); 110 void do_login(struct ssh *, Session *, const char *); 111 void do_child(struct ssh *, Session *, const char *); 112 void do_motd(void); 113 int check_quietlogin(Session *, const char *); 114 115 static void do_authenticated2(struct ssh *, Authctxt *); 116 117 static int session_pty_req(struct ssh *, Session *); 118 119 /* import */ 120 extern ServerOptions options; 121 extern char *__progname; 122 extern int debug_flag; 123 extern u_int utmp_len; 124 extern int startup_pipe; 125 extern void destroy_sensitive_data(void); 126 extern Buffer loginmsg; 127 extern struct sshauthopt *auth_opts; 128 char *tun_fwd_ifnames; /* serverloop.c */ 129 130 /* original command from peer. */ 131 const char *original_command = NULL; 132 133 /* data */ 134 static int sessions_first_unused = -1; 135 static int sessions_nalloc = 0; 136 static Session *sessions = NULL; 137 138 #define SUBSYSTEM_NONE 0 139 #define SUBSYSTEM_EXT 1 140 #define SUBSYSTEM_INT_SFTP 2 141 #define SUBSYSTEM_INT_SFTP_ERROR 3 142 143 login_cap_t *lc; 144 145 static int is_child = 0; 146 static int in_chroot = 0; 147 148 /* File containing userauth info, if ExposeAuthInfo set */ 149 static char *auth_info_file = NULL; 150 151 /* Name and directory of socket for authentication agent forwarding. */ 152 static char *auth_sock_name = NULL; 153 static char *auth_sock_dir = NULL; 154 155 /* removes the agent forwarding socket */ 156 157 static void 158 auth_sock_cleanup_proc(struct passwd *pw) 159 { 160 if (auth_sock_name != NULL) { 161 temporarily_use_uid(pw); 162 unlink(auth_sock_name); 163 rmdir(auth_sock_dir); 164 auth_sock_name = NULL; 165 restore_uid(); 166 } 167 } 168 169 static int 170 auth_input_request_forwarding(struct ssh *ssh, struct passwd * pw) 171 { 172 Channel *nc; 173 int sock = -1; 174 175 if (auth_sock_name != NULL) { 176 error("authentication forwarding requested twice."); 177 return 0; 178 } 179 180 /* Temporarily drop privileged uid for mkdir/bind. */ 181 temporarily_use_uid(pw); 182 183 /* Allocate a buffer for the socket name, and format the name. */ 184 auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX"); 185 186 /* Create private directory for socket */ 187 if (mkdtemp(auth_sock_dir) == NULL) { 188 packet_send_debug("Agent forwarding disabled: " 189 "mkdtemp() failed: %.100s", strerror(errno)); 190 restore_uid(); 191 free(auth_sock_dir); 192 auth_sock_dir = NULL; 193 goto authsock_err; 194 } 195 196 xasprintf(&auth_sock_name, "%s/agent.%ld", 197 auth_sock_dir, (long) getpid()); 198 199 /* Start a Unix listener on auth_sock_name. */ 200 sock = unix_listener(auth_sock_name, SSH_LISTEN_BACKLOG, 0); 201 202 /* Restore the privileged uid. */ 203 restore_uid(); 204 205 /* Check for socket/bind/listen failure. */ 206 if (sock < 0) 207 goto authsock_err; 208 209 /* Allocate a channel for the authentication agent socket. */ 210 nc = channel_new(ssh, "auth socket", 211 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1, 212 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 213 0, "auth socket", 1); 214 nc->path = xstrdup(auth_sock_name); 215 return 1; 216 217 authsock_err: 218 free(auth_sock_name); 219 if (auth_sock_dir != NULL) { 220 rmdir(auth_sock_dir); 221 free(auth_sock_dir); 222 } 223 if (sock != -1) 224 close(sock); 225 auth_sock_name = NULL; 226 auth_sock_dir = NULL; 227 return 0; 228 } 229 230 static void 231 display_loginmsg(void) 232 { 233 if (buffer_len(&loginmsg) > 0) { 234 buffer_append(&loginmsg, "\0", 1); 235 printf("%s", (char *)buffer_ptr(&loginmsg)); 236 buffer_clear(&loginmsg); 237 } 238 } 239 240 static void 241 prepare_auth_info_file(struct passwd *pw, struct sshbuf *info) 242 { 243 int fd = -1, success = 0; 244 245 if (!options.expose_userauth_info || info == NULL) 246 return; 247 248 temporarily_use_uid(pw); 249 auth_info_file = xstrdup("/tmp/sshauth.XXXXXXXXXXXXXXX"); 250 if ((fd = mkstemp(auth_info_file)) == -1) { 251 error("%s: mkstemp: %s", __func__, strerror(errno)); 252 goto out; 253 } 254 if (atomicio(vwrite, fd, sshbuf_mutable_ptr(info), 255 sshbuf_len(info)) != sshbuf_len(info)) { 256 error("%s: write: %s", __func__, strerror(errno)); 257 goto out; 258 } 259 if (close(fd) != 0) { 260 error("%s: close: %s", __func__, strerror(errno)); 261 goto out; 262 } 263 success = 1; 264 out: 265 if (!success) { 266 if (fd != -1) 267 close(fd); 268 free(auth_info_file); 269 auth_info_file = NULL; 270 } 271 restore_uid(); 272 } 273 274 static void 275 set_permitopen_from_authopts(struct ssh *ssh, const struct sshauthopt *opts) 276 { 277 char *tmp, *cp, *host; 278 int port; 279 size_t i; 280 281 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) == 0) 282 return; 283 channel_clear_permitted_opens(ssh); 284 for (i = 0; i < auth_opts->npermitopen; i++) { 285 tmp = cp = xstrdup(auth_opts->permitopen[i]); 286 /* This shouldn't fail as it has already been checked */ 287 if ((host = hpdelim(&cp)) == NULL) 288 fatal("%s: internal error: hpdelim", __func__); 289 host = cleanhostname(host); 290 if (cp == NULL || (port = permitopen_port(cp)) < 0) 291 fatal("%s: internal error: permitopen port", 292 __func__); 293 channel_add_permitted_opens(ssh, host, port); 294 free(tmp); 295 } 296 } 297 298 void 299 do_authenticated(struct ssh *ssh, Authctxt *authctxt) 300 { 301 setproctitle("%s", authctxt->pw->pw_name); 302 303 auth_log_authopts("active", auth_opts, 0); 304 305 /* setup the channel layer */ 306 /* XXX - streamlocal? */ 307 set_permitopen_from_authopts(ssh, auth_opts); 308 if (!auth_opts->permit_port_forwarding_flag || 309 options.disable_forwarding || 310 (options.allow_tcp_forwarding & FORWARD_LOCAL) == 0) 311 channel_disable_adm_local_opens(ssh); 312 else 313 channel_permit_all_opens(ssh); 314 315 auth_debug_send(); 316 317 prepare_auth_info_file(authctxt->pw, authctxt->session_info); 318 319 do_authenticated2(ssh, authctxt); 320 321 do_cleanup(ssh, authctxt); 322 } 323 324 /* Check untrusted xauth strings for metacharacters */ 325 static int 326 xauth_valid_string(const char *s) 327 { 328 size_t i; 329 330 for (i = 0; s[i] != '\0'; i++) { 331 if (!isalnum((u_char)s[i]) && 332 s[i] != '.' && s[i] != ':' && s[i] != '/' && 333 s[i] != '-' && s[i] != '_') 334 return 0; 335 } 336 return 1; 337 } 338 339 #define USE_PIPES 1 340 /* 341 * This is called to fork and execute a command when we have no tty. This 342 * will call do_child from the child, and server_loop from the parent after 343 * setting up file descriptors and such. 344 */ 345 int 346 do_exec_no_pty(struct ssh *ssh, Session *s, const char *command) 347 { 348 pid_t pid; 349 #ifdef USE_PIPES 350 int pin[2], pout[2], perr[2]; 351 352 if (s == NULL) 353 fatal("do_exec_no_pty: no session"); 354 355 /* Allocate pipes for communicating with the program. */ 356 if (pipe(pin) < 0) { 357 error("%s: pipe in: %.100s", __func__, strerror(errno)); 358 return -1; 359 } 360 if (pipe(pout) < 0) { 361 error("%s: pipe out: %.100s", __func__, strerror(errno)); 362 close(pin[0]); 363 close(pin[1]); 364 return -1; 365 } 366 if (pipe(perr) < 0) { 367 error("%s: pipe err: %.100s", __func__, 368 strerror(errno)); 369 close(pin[0]); 370 close(pin[1]); 371 close(pout[0]); 372 close(pout[1]); 373 return -1; 374 } 375 #else 376 int inout[2], err[2]; 377 378 if (s == NULL) 379 fatal("do_exec_no_pty: no session"); 380 381 /* Uses socket pairs to communicate with the program. */ 382 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) { 383 error("%s: socketpair #1: %.100s", __func__, strerror(errno)); 384 return -1; 385 } 386 if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) { 387 error("%s: socketpair #2: %.100s", __func__, 388 strerror(errno)); 389 close(inout[0]); 390 close(inout[1]); 391 return -1; 392 } 393 #endif 394 395 session_proctitle(s); 396 397 /* Fork the child. */ 398 switch ((pid = fork())) { 399 case -1: 400 error("%s: fork: %.100s", __func__, strerror(errno)); 401 #ifdef USE_PIPES 402 close(pin[0]); 403 close(pin[1]); 404 close(pout[0]); 405 close(pout[1]); 406 close(perr[0]); 407 close(perr[1]); 408 #else 409 close(inout[0]); 410 close(inout[1]); 411 close(err[0]); 412 close(err[1]); 413 #endif 414 return -1; 415 case 0: 416 is_child = 1; 417 418 /* 419 * Create a new session and process group since the 4.4BSD 420 * setlogin() affects the entire process group. 421 */ 422 if (setsid() < 0) 423 error("setsid failed: %.100s", strerror(errno)); 424 425 #ifdef USE_PIPES 426 /* 427 * Redirect stdin. We close the parent side of the socket 428 * pair, and make the child side the standard input. 429 */ 430 close(pin[1]); 431 if (dup2(pin[0], 0) < 0) 432 perror("dup2 stdin"); 433 close(pin[0]); 434 435 /* Redirect stdout. */ 436 close(pout[0]); 437 if (dup2(pout[1], 1) < 0) 438 perror("dup2 stdout"); 439 close(pout[1]); 440 441 /* Redirect stderr. */ 442 close(perr[0]); 443 if (dup2(perr[1], 2) < 0) 444 perror("dup2 stderr"); 445 close(perr[1]); 446 #else 447 /* 448 * Redirect stdin, stdout, and stderr. Stdin and stdout will 449 * use the same socket, as some programs (particularly rdist) 450 * seem to depend on it. 451 */ 452 close(inout[1]); 453 close(err[1]); 454 if (dup2(inout[0], 0) < 0) /* stdin */ 455 perror("dup2 stdin"); 456 if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */ 457 perror("dup2 stdout"); 458 close(inout[0]); 459 if (dup2(err[0], 2) < 0) /* stderr */ 460 perror("dup2 stderr"); 461 close(err[0]); 462 #endif 463 464 /* Do processing for the child (exec command etc). */ 465 do_child(ssh, s, command); 466 /* NOTREACHED */ 467 default: 468 break; 469 } 470 471 s->pid = pid; 472 /* Set interactive/non-interactive mode. */ 473 packet_set_interactive(s->display != NULL, 474 options.ip_qos_interactive, options.ip_qos_bulk); 475 476 #ifdef USE_PIPES 477 /* We are the parent. Close the child sides of the pipes. */ 478 close(pin[0]); 479 close(pout[1]); 480 close(perr[1]); 481 482 session_set_fds(ssh, s, pin[1], pout[0], perr[0], 483 s->is_subsystem, 0); 484 #else 485 /* We are the parent. Close the child sides of the socket pairs. */ 486 close(inout[0]); 487 close(err[0]); 488 489 /* 490 * Enter the interactive session. Note: server_loop must be able to 491 * handle the case that fdin and fdout are the same. 492 */ 493 session_set_fds(s, inout[1], inout[1], err[1], 494 s->is_subsystem, 0); 495 #endif 496 return 0; 497 } 498 499 /* 500 * This is called to fork and execute a command when we have a tty. This 501 * will call do_child from the child, and server_loop from the parent after 502 * setting up file descriptors, controlling tty, updating wtmp, utmp, 503 * lastlog, and other such operations. 504 */ 505 int 506 do_exec_pty(struct ssh *ssh, Session *s, const char *command) 507 { 508 int fdout, ptyfd, ttyfd, ptymaster; 509 pid_t pid; 510 511 if (s == NULL) 512 fatal("do_exec_pty: no session"); 513 ptyfd = s->ptyfd; 514 ttyfd = s->ttyfd; 515 516 /* 517 * Create another descriptor of the pty master side for use as the 518 * standard input. We could use the original descriptor, but this 519 * simplifies code in server_loop. The descriptor is bidirectional. 520 * Do this before forking (and cleanup in the child) so as to 521 * detect and gracefully fail out-of-fd conditions. 522 */ 523 if ((fdout = dup(ptyfd)) < 0) { 524 error("%s: dup #1: %s", __func__, strerror(errno)); 525 close(ttyfd); 526 close(ptyfd); 527 return -1; 528 } 529 /* we keep a reference to the pty master */ 530 if ((ptymaster = dup(ptyfd)) < 0) { 531 error("%s: dup #2: %s", __func__, strerror(errno)); 532 close(ttyfd); 533 close(ptyfd); 534 close(fdout); 535 return -1; 536 } 537 538 /* Fork the child. */ 539 switch ((pid = fork())) { 540 case -1: 541 error("%s: fork: %.100s", __func__, strerror(errno)); 542 close(fdout); 543 close(ptymaster); 544 close(ttyfd); 545 close(ptyfd); 546 return -1; 547 case 0: 548 is_child = 1; 549 550 close(fdout); 551 close(ptymaster); 552 553 /* Close the master side of the pseudo tty. */ 554 close(ptyfd); 555 556 /* Make the pseudo tty our controlling tty. */ 557 pty_make_controlling_tty(&ttyfd, s->tty); 558 559 /* Redirect stdin/stdout/stderr from the pseudo tty. */ 560 if (dup2(ttyfd, 0) < 0) 561 error("dup2 stdin: %s", strerror(errno)); 562 if (dup2(ttyfd, 1) < 0) 563 error("dup2 stdout: %s", strerror(errno)); 564 if (dup2(ttyfd, 2) < 0) 565 error("dup2 stderr: %s", strerror(errno)); 566 567 /* Close the extra descriptor for the pseudo tty. */ 568 close(ttyfd); 569 570 /* record login, etc. similar to login(1) */ 571 do_login(ssh, s, command); 572 573 /* 574 * Do common processing for the child, such as execing 575 * the command. 576 */ 577 do_child(ssh, s, command); 578 /* NOTREACHED */ 579 default: 580 break; 581 } 582 s->pid = pid; 583 584 /* Parent. Close the slave side of the pseudo tty. */ 585 close(ttyfd); 586 587 /* Enter interactive session. */ 588 s->ptymaster = ptymaster; 589 packet_set_interactive(1, 590 options.ip_qos_interactive, options.ip_qos_bulk); 591 session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1); 592 return 0; 593 } 594 595 /* 596 * This is called to fork and execute a command. If another command is 597 * to be forced, execute that instead. 598 */ 599 int 600 do_exec(struct ssh *ssh, Session *s, const char *command) 601 { 602 int ret; 603 const char *forced = NULL, *tty = NULL; 604 char session_type[1024]; 605 606 if (options.adm_forced_command) { 607 original_command = command; 608 command = options.adm_forced_command; 609 forced = "(config)"; 610 } else if (auth_opts->force_command != NULL) { 611 original_command = command; 612 command = auth_opts->force_command; 613 forced = "(key-option)"; 614 } 615 if (forced != NULL) { 616 if (IS_INTERNAL_SFTP(command)) { 617 s->is_subsystem = s->is_subsystem ? 618 SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR; 619 } else if (s->is_subsystem) 620 s->is_subsystem = SUBSYSTEM_EXT; 621 snprintf(session_type, sizeof(session_type), 622 "forced-command %s '%.900s'", forced, command); 623 } else if (s->is_subsystem) { 624 snprintf(session_type, sizeof(session_type), 625 "subsystem '%.900s'", s->subsys); 626 } else if (command == NULL) { 627 snprintf(session_type, sizeof(session_type), "shell"); 628 } else { 629 /* NB. we don't log unforced commands to preserve privacy */ 630 snprintf(session_type, sizeof(session_type), "command"); 631 } 632 633 if (s->ttyfd != -1) { 634 tty = s->tty; 635 if (strncmp(tty, "/dev/", 5) == 0) 636 tty += 5; 637 } 638 639 verbose("Starting session: %s%s%s for %s from %.200s port %d id %d", 640 session_type, 641 tty == NULL ? "" : " on ", 642 tty == NULL ? "" : tty, 643 s->pw->pw_name, 644 ssh_remote_ipaddr(ssh), 645 ssh_remote_port(ssh), 646 s->self); 647 648 #ifdef GSSAPI 649 if (options.gss_authentication) { 650 temporarily_use_uid(s->pw); 651 ssh_gssapi_storecreds(); 652 restore_uid(); 653 } 654 #endif 655 if (s->ttyfd != -1) 656 ret = do_exec_pty(ssh, s, command); 657 else 658 ret = do_exec_no_pty(ssh, s, command); 659 660 original_command = NULL; 661 662 /* 663 * Clear loginmsg: it's the child's responsibility to display 664 * it to the user, otherwise multiple sessions may accumulate 665 * multiple copies of the login messages. 666 */ 667 buffer_clear(&loginmsg); 668 669 return ret; 670 } 671 672 673 /* administrative, login(1)-like work */ 674 void 675 do_login(struct ssh *ssh, Session *s, const char *command) 676 { 677 socklen_t fromlen; 678 struct sockaddr_storage from; 679 struct passwd * pw = s->pw; 680 pid_t pid = getpid(); 681 682 /* 683 * Get IP address of client. If the connection is not a socket, let 684 * the address be 0.0.0.0. 685 */ 686 memset(&from, 0, sizeof(from)); 687 fromlen = sizeof(from); 688 if (packet_connection_is_on_socket()) { 689 if (getpeername(packet_get_connection_in(), 690 (struct sockaddr *)&from, &fromlen) < 0) { 691 debug("getpeername: %.100s", strerror(errno)); 692 cleanup_exit(255); 693 } 694 } 695 696 /* Record that there was a login on that tty from the remote host. */ 697 if (!use_privsep) 698 record_login(pid, s->tty, pw->pw_name, pw->pw_uid, 699 session_get_remote_name_or_ip(ssh, utmp_len, 700 options.use_dns), 701 (struct sockaddr *)&from, fromlen); 702 703 if (check_quietlogin(s, command)) 704 return; 705 706 display_loginmsg(); 707 708 do_motd(); 709 } 710 711 /* 712 * Display the message of the day. 713 */ 714 void 715 do_motd(void) 716 { 717 FILE *f; 718 char buf[256]; 719 720 if (options.print_motd) { 721 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd", 722 "/etc/motd"), "r"); 723 if (f) { 724 while (fgets(buf, sizeof(buf), f)) 725 fputs(buf, stdout); 726 fclose(f); 727 } 728 } 729 } 730 731 732 /* 733 * Check for quiet login, either .hushlogin or command given. 734 */ 735 int 736 check_quietlogin(Session *s, const char *command) 737 { 738 char buf[256]; 739 struct passwd *pw = s->pw; 740 struct stat st; 741 742 /* Return 1 if .hushlogin exists or a command given. */ 743 if (command != NULL) 744 return 1; 745 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir); 746 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0) 747 return 1; 748 return 0; 749 } 750 751 /* 752 * Reads environment variables from the given file and adds/overrides them 753 * into the environment. If the file does not exist, this does nothing. 754 * Otherwise, it must consist of empty lines, comments (line starts with '#') 755 * and assignments of the form name=value. No other forms are allowed. 756 */ 757 static void 758 read_environment_file(char ***env, u_int *envsize, 759 const char *filename) 760 { 761 FILE *f; 762 char buf[4096]; 763 char *cp, *value; 764 u_int lineno = 0; 765 766 f = fopen(filename, "r"); 767 if (!f) 768 return; 769 770 while (fgets(buf, sizeof(buf), f)) { 771 if (++lineno > 1000) 772 fatal("Too many lines in environment file %s", filename); 773 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) 774 ; 775 if (!*cp || *cp == '#' || *cp == '\n') 776 continue; 777 778 cp[strcspn(cp, "\n")] = '\0'; 779 780 value = strchr(cp, '='); 781 if (value == NULL) { 782 fprintf(stderr, "Bad line %u in %.100s\n", lineno, 783 filename); 784 continue; 785 } 786 /* 787 * Replace the equals sign by nul, and advance value to 788 * the value string. 789 */ 790 *value = '\0'; 791 value++; 792 child_set_env(env, envsize, cp, value); 793 } 794 fclose(f); 795 } 796 797 static char ** 798 do_setup_env(struct ssh *ssh, Session *s, const char *shell) 799 { 800 char buf[256]; 801 size_t n; 802 u_int i, envsize; 803 char *ocp, *cp, **env, *laddr; 804 struct passwd *pw = s->pw; 805 806 /* Initialize the environment. */ 807 envsize = 100; 808 env = xcalloc(envsize, sizeof(char *)); 809 env[0] = NULL; 810 811 #ifdef GSSAPI 812 /* Allow any GSSAPI methods that we've used to alter 813 * the childs environment as they see fit 814 */ 815 ssh_gssapi_do_child(&env, &envsize); 816 #endif 817 818 /* Set basic environment. */ 819 for (i = 0; i < s->num_env; i++) 820 child_set_env(&env, &envsize, s->env[i].name, s->env[i].val); 821 822 child_set_env(&env, &envsize, "USER", pw->pw_name); 823 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); 824 child_set_env(&env, &envsize, "HOME", pw->pw_dir); 825 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0) 826 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); 827 else 828 child_set_env(&env, &envsize, "PATH", getenv("PATH")); 829 830 snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name); 831 child_set_env(&env, &envsize, "MAIL", buf); 832 833 /* Normal systems set SHELL by default. */ 834 child_set_env(&env, &envsize, "SHELL", shell); 835 836 if (getenv("TZ")) 837 child_set_env(&env, &envsize, "TZ", getenv("TZ")); 838 839 /* Set custom environment options from pubkey authentication. */ 840 if (options.permit_user_env) { 841 for (n = 0 ; n < auth_opts->nenv; n++) { 842 ocp = xstrdup(auth_opts->env[n]); 843 cp = strchr(ocp, '='); 844 if (*cp == '=') { 845 *cp = '\0'; 846 child_set_env(&env, &envsize, ocp, cp + 1); 847 } 848 free(ocp); 849 } 850 } 851 852 /* SSH_CLIENT deprecated */ 853 snprintf(buf, sizeof buf, "%.50s %d %d", 854 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), 855 ssh_local_port(ssh)); 856 child_set_env(&env, &envsize, "SSH_CLIENT", buf); 857 858 laddr = get_local_ipaddr(packet_get_connection_in()); 859 snprintf(buf, sizeof buf, "%.50s %d %.50s %d", 860 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), 861 laddr, ssh_local_port(ssh)); 862 free(laddr); 863 child_set_env(&env, &envsize, "SSH_CONNECTION", buf); 864 865 if (tun_fwd_ifnames != NULL) 866 child_set_env(&env, &envsize, "SSH_TUNNEL", tun_fwd_ifnames); 867 if (auth_info_file != NULL) 868 child_set_env(&env, &envsize, "SSH_USER_AUTH", auth_info_file); 869 if (s->ttyfd != -1) 870 child_set_env(&env, &envsize, "SSH_TTY", s->tty); 871 if (s->term) 872 child_set_env(&env, &envsize, "TERM", s->term); 873 if (s->display) 874 child_set_env(&env, &envsize, "DISPLAY", s->display); 875 if (original_command) 876 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND", 877 original_command); 878 #ifdef KRB5 879 if (s->authctxt->krb5_ticket_file) 880 child_set_env(&env, &envsize, "KRB5CCNAME", 881 s->authctxt->krb5_ticket_file); 882 #endif 883 if (auth_sock_name != NULL) 884 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, 885 auth_sock_name); 886 887 /* read $HOME/.ssh/environment. */ 888 if (options.permit_user_env) { 889 snprintf(buf, sizeof buf, "%.200s/.ssh/environment", 890 pw->pw_dir); 891 read_environment_file(&env, &envsize, buf); 892 } 893 if (debug_flag) { 894 /* dump the environment */ 895 fprintf(stderr, "Environment:\n"); 896 for (i = 0; env[i]; i++) 897 fprintf(stderr, " %.200s\n", env[i]); 898 } 899 return env; 900 } 901 902 /* 903 * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found 904 * first in this order). 905 */ 906 static void 907 do_rc_files(struct ssh *ssh, Session *s, const char *shell) 908 { 909 FILE *f = NULL; 910 char cmd[1024]; 911 int do_xauth; 912 struct stat st; 913 914 do_xauth = 915 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL; 916 917 /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */ 918 if (!s->is_subsystem && options.adm_forced_command == NULL && 919 auth_opts->permit_user_rc && options.permit_user_rc && 920 stat(_PATH_SSH_USER_RC, &st) >= 0) { 921 snprintf(cmd, sizeof cmd, "%s -c '%s %s'", 922 shell, _PATH_BSHELL, _PATH_SSH_USER_RC); 923 if (debug_flag) 924 fprintf(stderr, "Running %s\n", cmd); 925 f = popen(cmd, "w"); 926 if (f) { 927 if (do_xauth) 928 fprintf(f, "%s %s\n", s->auth_proto, 929 s->auth_data); 930 pclose(f); 931 } else 932 fprintf(stderr, "Could not run %s\n", 933 _PATH_SSH_USER_RC); 934 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) { 935 if (debug_flag) 936 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, 937 _PATH_SSH_SYSTEM_RC); 938 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w"); 939 if (f) { 940 if (do_xauth) 941 fprintf(f, "%s %s\n", s->auth_proto, 942 s->auth_data); 943 pclose(f); 944 } else 945 fprintf(stderr, "Could not run %s\n", 946 _PATH_SSH_SYSTEM_RC); 947 } else if (do_xauth && options.xauth_location != NULL) { 948 /* Add authority data to .Xauthority if appropriate. */ 949 if (debug_flag) { 950 fprintf(stderr, 951 "Running %.500s remove %.100s\n", 952 options.xauth_location, s->auth_display); 953 fprintf(stderr, 954 "%.500s add %.100s %.100s %.100s\n", 955 options.xauth_location, s->auth_display, 956 s->auth_proto, s->auth_data); 957 } 958 snprintf(cmd, sizeof cmd, "%s -q -", 959 options.xauth_location); 960 f = popen(cmd, "w"); 961 if (f) { 962 fprintf(f, "remove %s\n", 963 s->auth_display); 964 fprintf(f, "add %s %s %s\n", 965 s->auth_display, s->auth_proto, 966 s->auth_data); 967 pclose(f); 968 } else { 969 fprintf(stderr, "Could not run %s\n", 970 cmd); 971 } 972 } 973 } 974 975 static void 976 do_nologin(struct passwd *pw) 977 { 978 FILE *f = NULL; 979 char buf[1024], *nl, *def_nl = _PATH_NOLOGIN; 980 struct stat sb; 981 982 if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0) 983 return; 984 nl = login_getcapstr(lc, "nologin", def_nl, def_nl); 985 986 if (stat(nl, &sb) == -1) { 987 if (nl != def_nl) 988 free(nl); 989 return; 990 } 991 992 /* /etc/nologin exists. Print its contents if we can and exit. */ 993 logit("User %.100s not allowed because %s exists", pw->pw_name, nl); 994 if ((f = fopen(nl, "r")) != NULL) { 995 while (fgets(buf, sizeof(buf), f)) 996 fputs(buf, stderr); 997 fclose(f); 998 } 999 exit(254); 1000 } 1001 1002 /* 1003 * Chroot into a directory after checking it for safety: all path components 1004 * must be root-owned directories with strict permissions. 1005 */ 1006 static void 1007 safely_chroot(const char *path, uid_t uid) 1008 { 1009 const char *cp; 1010 char component[PATH_MAX]; 1011 struct stat st; 1012 1013 if (*path != '/') 1014 fatal("chroot path does not begin at root"); 1015 if (strlen(path) >= sizeof(component)) 1016 fatal("chroot path too long"); 1017 1018 /* 1019 * Descend the path, checking that each component is a 1020 * root-owned directory with strict permissions. 1021 */ 1022 for (cp = path; cp != NULL;) { 1023 if ((cp = strchr(cp, '/')) == NULL) 1024 strlcpy(component, path, sizeof(component)); 1025 else { 1026 cp++; 1027 memcpy(component, path, cp - path); 1028 component[cp - path] = '\0'; 1029 } 1030 1031 debug3("%s: checking '%s'", __func__, component); 1032 1033 if (stat(component, &st) != 0) 1034 fatal("%s: stat(\"%s\"): %s", __func__, 1035 component, strerror(errno)); 1036 if (st.st_uid != 0 || (st.st_mode & 022) != 0) 1037 fatal("bad ownership or modes for chroot " 1038 "directory %s\"%s\"", 1039 cp == NULL ? "" : "component ", component); 1040 if (!S_ISDIR(st.st_mode)) 1041 fatal("chroot path %s\"%s\" is not a directory", 1042 cp == NULL ? "" : "component ", component); 1043 1044 } 1045 1046 if (chdir(path) == -1) 1047 fatal("Unable to chdir to chroot path \"%s\": " 1048 "%s", path, strerror(errno)); 1049 if (chroot(path) == -1) 1050 fatal("chroot(\"%s\"): %s", path, strerror(errno)); 1051 if (chdir("/") == -1) 1052 fatal("%s: chdir(/) after chroot: %s", 1053 __func__, strerror(errno)); 1054 verbose("Changed root directory to \"%s\"", path); 1055 } 1056 1057 /* Set login name, uid, gid, and groups. */ 1058 void 1059 do_setusercontext(struct passwd *pw) 1060 { 1061 char *chroot_path, *tmp; 1062 1063 if (getuid() == 0 || geteuid() == 0) { 1064 /* Prepare groups */ 1065 if (setusercontext(lc, pw, pw->pw_uid, 1066 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) { 1067 perror("unable to set user context"); 1068 exit(1); 1069 } 1070 1071 if (!in_chroot && options.chroot_directory != NULL && 1072 strcasecmp(options.chroot_directory, "none") != 0) { 1073 tmp = tilde_expand_filename(options.chroot_directory, 1074 pw->pw_uid); 1075 chroot_path = percent_expand(tmp, "h", pw->pw_dir, 1076 "u", pw->pw_name, (char *)NULL); 1077 safely_chroot(chroot_path, pw->pw_uid); 1078 free(tmp); 1079 free(chroot_path); 1080 /* Make sure we don't attempt to chroot again */ 1081 free(options.chroot_directory); 1082 options.chroot_directory = NULL; 1083 in_chroot = 1; 1084 } 1085 1086 /* Set UID */ 1087 if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) { 1088 perror("unable to set user context (setuser)"); 1089 exit(1); 1090 } 1091 } else if (options.chroot_directory != NULL && 1092 strcasecmp(options.chroot_directory, "none") != 0) { 1093 fatal("server lacks privileges to chroot to ChrootDirectory"); 1094 } 1095 1096 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) 1097 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid); 1098 } 1099 1100 static void 1101 do_pwchange(Session *s) 1102 { 1103 fflush(NULL); 1104 fprintf(stderr, "WARNING: Your password has expired.\n"); 1105 if (s->ttyfd != -1) { 1106 fprintf(stderr, 1107 "You must change your password now and login again!\n"); 1108 execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL); 1109 perror("passwd"); 1110 } else { 1111 fprintf(stderr, 1112 "Password change required but no TTY available.\n"); 1113 } 1114 exit(1); 1115 } 1116 1117 static void 1118 child_close_fds(struct ssh *ssh) 1119 { 1120 extern int auth_sock; 1121 1122 if (auth_sock != -1) { 1123 close(auth_sock); 1124 auth_sock = -1; 1125 } 1126 1127 if (packet_get_connection_in() == packet_get_connection_out()) 1128 close(packet_get_connection_in()); 1129 else { 1130 close(packet_get_connection_in()); 1131 close(packet_get_connection_out()); 1132 } 1133 /* 1134 * Close all descriptors related to channels. They will still remain 1135 * open in the parent. 1136 */ 1137 /* XXX better use close-on-exec? -markus */ 1138 channel_close_all(ssh); 1139 1140 /* 1141 * Close any extra file descriptors. Note that there may still be 1142 * descriptors left by system functions. They will be closed later. 1143 */ 1144 endpwent(); 1145 1146 /* 1147 * Close any extra open file descriptors so that we don't have them 1148 * hanging around in clients. Note that we want to do this after 1149 * initgroups, because at least on Solaris 2.3 it leaves file 1150 * descriptors open. 1151 */ 1152 closefrom(STDERR_FILENO + 1); 1153 } 1154 1155 /* 1156 * Performs common processing for the child, such as setting up the 1157 * environment, closing extra file descriptors, setting the user and group 1158 * ids, and executing the command or shell. 1159 */ 1160 #define ARGV_MAX 10 1161 void 1162 do_child(struct ssh *ssh, Session *s, const char *command) 1163 { 1164 extern char **environ; 1165 char **env; 1166 char *argv[ARGV_MAX]; 1167 const char *shell, *shell0; 1168 struct passwd *pw = s->pw; 1169 int r = 0; 1170 1171 /* remove hostkey from the child's memory */ 1172 destroy_sensitive_data(); 1173 packet_clear_keys(); 1174 1175 /* Force a password change */ 1176 if (s->authctxt->force_pwchange) { 1177 do_setusercontext(pw); 1178 child_close_fds(ssh); 1179 do_pwchange(s); 1180 exit(1); 1181 } 1182 1183 /* 1184 * Login(1) does this as well, and it needs uid 0 for the "-h" 1185 * switch, so we let login(1) to this for us. 1186 */ 1187 do_nologin(pw); 1188 do_setusercontext(pw); 1189 1190 /* 1191 * Get the shell from the password data. An empty shell field is 1192 * legal, and means /bin/sh. 1193 */ 1194 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; 1195 1196 /* 1197 * Make sure $SHELL points to the shell from the password file, 1198 * even if shell is overridden from login.conf 1199 */ 1200 env = do_setup_env(ssh, s, shell); 1201 1202 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell); 1203 1204 /* 1205 * Close the connection descriptors; note that this is the child, and 1206 * the server will still have the socket open, and it is important 1207 * that we do not shutdown it. Note that the descriptors cannot be 1208 * closed before building the environment, as we call 1209 * ssh_remote_ipaddr there. 1210 */ 1211 child_close_fds(ssh); 1212 1213 /* 1214 * Must take new environment into use so that .ssh/rc, 1215 * /etc/ssh/sshrc and xauth are run in the proper environment. 1216 */ 1217 environ = env; 1218 1219 #ifdef KRB5 1220 /* 1221 * At this point, we check to see if AFS is active and if we have 1222 * a valid Kerberos 5 TGT. If so, it seems like a good idea to see 1223 * if we can (and need to) extend the ticket into an AFS token. If 1224 * we don't do this, we run into potential problems if the user's 1225 * home directory is in AFS and it's not world-readable. 1226 */ 1227 1228 if (options.kerberos_get_afs_token && k_hasafs() && 1229 (s->authctxt->krb5_ctx != NULL)) { 1230 char cell[64]; 1231 1232 debug("Getting AFS token"); 1233 1234 k_setpag(); 1235 1236 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) 1237 krb5_afslog(s->authctxt->krb5_ctx, 1238 s->authctxt->krb5_fwd_ccache, cell, NULL); 1239 1240 krb5_afslog_home(s->authctxt->krb5_ctx, 1241 s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir); 1242 } 1243 #endif 1244 1245 /* Change current directory to the user's home directory. */ 1246 if (chdir(pw->pw_dir) < 0) { 1247 /* Suppress missing homedir warning for chroot case */ 1248 r = login_getcapbool(lc, "requirehome", 0); 1249 if (r || !in_chroot) { 1250 fprintf(stderr, "Could not chdir to home " 1251 "directory %s: %s\n", pw->pw_dir, 1252 strerror(errno)); 1253 } 1254 if (r) 1255 exit(1); 1256 } 1257 1258 closefrom(STDERR_FILENO + 1); 1259 1260 do_rc_files(ssh, s, shell); 1261 1262 /* restore SIGPIPE for child */ 1263 signal(SIGPIPE, SIG_DFL); 1264 1265 if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) { 1266 printf("This service allows sftp connections only.\n"); 1267 fflush(NULL); 1268 exit(1); 1269 } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) { 1270 extern int optind, optreset; 1271 int i; 1272 char *p, *args; 1273 1274 setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME); 1275 args = xstrdup(command ? command : "sftp-server"); 1276 for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " "))) 1277 if (i < ARGV_MAX - 1) 1278 argv[i++] = p; 1279 argv[i] = NULL; 1280 optind = optreset = 1; 1281 __progname = argv[0]; 1282 exit(sftp_server_main(i, argv, s->pw)); 1283 } 1284 1285 fflush(NULL); 1286 1287 /* Get the last component of the shell name. */ 1288 if ((shell0 = strrchr(shell, '/')) != NULL) 1289 shell0++; 1290 else 1291 shell0 = shell; 1292 1293 /* 1294 * If we have no command, execute the shell. In this case, the shell 1295 * name to be passed in argv[0] is preceded by '-' to indicate that 1296 * this is a login shell. 1297 */ 1298 if (!command) { 1299 char argv0[256]; 1300 1301 /* Start the shell. Set initial character to '-'. */ 1302 argv0[0] = '-'; 1303 1304 if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1) 1305 >= sizeof(argv0) - 1) { 1306 errno = EINVAL; 1307 perror(shell); 1308 exit(1); 1309 } 1310 1311 /* Execute the shell. */ 1312 argv[0] = argv0; 1313 argv[1] = NULL; 1314 execve(shell, argv, env); 1315 1316 /* Executing the shell failed. */ 1317 perror(shell); 1318 exit(1); 1319 } 1320 /* 1321 * Execute the command using the user's shell. This uses the -c 1322 * option to execute the command. 1323 */ 1324 argv[0] = (char *) shell0; 1325 argv[1] = "-c"; 1326 argv[2] = (char *) command; 1327 argv[3] = NULL; 1328 execve(shell, argv, env); 1329 perror(shell); 1330 exit(1); 1331 } 1332 1333 void 1334 session_unused(int id) 1335 { 1336 debug3("%s: session id %d unused", __func__, id); 1337 if (id >= options.max_sessions || 1338 id >= sessions_nalloc) { 1339 fatal("%s: insane session id %d (max %d nalloc %d)", 1340 __func__, id, options.max_sessions, sessions_nalloc); 1341 } 1342 memset(&sessions[id], 0, sizeof(*sessions)); 1343 sessions[id].self = id; 1344 sessions[id].used = 0; 1345 sessions[id].chanid = -1; 1346 sessions[id].ptyfd = -1; 1347 sessions[id].ttyfd = -1; 1348 sessions[id].ptymaster = -1; 1349 sessions[id].x11_chanids = NULL; 1350 sessions[id].next_unused = sessions_first_unused; 1351 sessions_first_unused = id; 1352 } 1353 1354 Session * 1355 session_new(void) 1356 { 1357 Session *s, *tmp; 1358 1359 if (sessions_first_unused == -1) { 1360 if (sessions_nalloc >= options.max_sessions) 1361 return NULL; 1362 debug2("%s: allocate (allocated %d max %d)", 1363 __func__, sessions_nalloc, options.max_sessions); 1364 tmp = xrecallocarray(sessions, sessions_nalloc, 1365 sessions_nalloc + 1, sizeof(*sessions)); 1366 if (tmp == NULL) { 1367 error("%s: cannot allocate %d sessions", 1368 __func__, sessions_nalloc + 1); 1369 return NULL; 1370 } 1371 sessions = tmp; 1372 session_unused(sessions_nalloc++); 1373 } 1374 1375 if (sessions_first_unused >= sessions_nalloc || 1376 sessions_first_unused < 0) { 1377 fatal("%s: insane first_unused %d max %d nalloc %d", 1378 __func__, sessions_first_unused, options.max_sessions, 1379 sessions_nalloc); 1380 } 1381 1382 s = &sessions[sessions_first_unused]; 1383 if (s->used) { 1384 fatal("%s: session %d already used", 1385 __func__, sessions_first_unused); 1386 } 1387 sessions_first_unused = s->next_unused; 1388 s->used = 1; 1389 s->next_unused = -1; 1390 debug("session_new: session %d", s->self); 1391 1392 return s; 1393 } 1394 1395 static void 1396 session_dump(void) 1397 { 1398 int i; 1399 for (i = 0; i < sessions_nalloc; i++) { 1400 Session *s = &sessions[i]; 1401 1402 debug("dump: used %d next_unused %d session %d %p " 1403 "channel %d pid %ld", 1404 s->used, 1405 s->next_unused, 1406 s->self, 1407 s, 1408 s->chanid, 1409 (long)s->pid); 1410 } 1411 } 1412 1413 int 1414 session_open(Authctxt *authctxt, int chanid) 1415 { 1416 Session *s = session_new(); 1417 debug("session_open: channel %d", chanid); 1418 if (s == NULL) { 1419 error("no more sessions"); 1420 return 0; 1421 } 1422 s->authctxt = authctxt; 1423 s->pw = authctxt->pw; 1424 if (s->pw == NULL || !authctxt->valid) 1425 fatal("no user for session %d", s->self); 1426 debug("session_open: session %d: link with channel %d", s->self, chanid); 1427 s->chanid = chanid; 1428 return 1; 1429 } 1430 1431 Session * 1432 session_by_tty(char *tty) 1433 { 1434 int i; 1435 for (i = 0; i < sessions_nalloc; i++) { 1436 Session *s = &sessions[i]; 1437 if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) { 1438 debug("session_by_tty: session %d tty %s", i, tty); 1439 return s; 1440 } 1441 } 1442 debug("session_by_tty: unknown tty %.100s", tty); 1443 session_dump(); 1444 return NULL; 1445 } 1446 1447 static Session * 1448 session_by_channel(int id) 1449 { 1450 int i; 1451 for (i = 0; i < sessions_nalloc; i++) { 1452 Session *s = &sessions[i]; 1453 if (s->used && s->chanid == id) { 1454 debug("session_by_channel: session %d channel %d", 1455 i, id); 1456 return s; 1457 } 1458 } 1459 debug("session_by_channel: unknown channel %d", id); 1460 session_dump(); 1461 return NULL; 1462 } 1463 1464 static Session * 1465 session_by_x11_channel(int id) 1466 { 1467 int i, j; 1468 1469 for (i = 0; i < sessions_nalloc; i++) { 1470 Session *s = &sessions[i]; 1471 1472 if (s->x11_chanids == NULL || !s->used) 1473 continue; 1474 for (j = 0; s->x11_chanids[j] != -1; j++) { 1475 if (s->x11_chanids[j] == id) { 1476 debug("session_by_x11_channel: session %d " 1477 "channel %d", s->self, id); 1478 return s; 1479 } 1480 } 1481 } 1482 debug("session_by_x11_channel: unknown channel %d", id); 1483 session_dump(); 1484 return NULL; 1485 } 1486 1487 static Session * 1488 session_by_pid(pid_t pid) 1489 { 1490 int i; 1491 debug("session_by_pid: pid %ld", (long)pid); 1492 for (i = 0; i < sessions_nalloc; i++) { 1493 Session *s = &sessions[i]; 1494 if (s->used && s->pid == pid) 1495 return s; 1496 } 1497 error("session_by_pid: unknown pid %ld", (long)pid); 1498 session_dump(); 1499 return NULL; 1500 } 1501 1502 static int 1503 session_window_change_req(struct ssh *ssh, Session *s) 1504 { 1505 s->col = packet_get_int(); 1506 s->row = packet_get_int(); 1507 s->xpixel = packet_get_int(); 1508 s->ypixel = packet_get_int(); 1509 packet_check_eom(); 1510 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); 1511 return 1; 1512 } 1513 1514 static int 1515 session_pty_req(struct ssh *ssh, Session *s) 1516 { 1517 u_int len; 1518 int n_bytes; 1519 1520 if (!auth_opts->permit_pty_flag || !options.permit_tty) { 1521 debug("Allocating a pty not permitted for this connection."); 1522 return 0; 1523 } 1524 if (s->ttyfd != -1) { 1525 packet_disconnect("Protocol error: you already have a pty."); 1526 return 0; 1527 } 1528 1529 s->term = packet_get_string(&len); 1530 s->col = packet_get_int(); 1531 s->row = packet_get_int(); 1532 s->xpixel = packet_get_int(); 1533 s->ypixel = packet_get_int(); 1534 1535 if (strcmp(s->term, "") == 0) { 1536 free(s->term); 1537 s->term = NULL; 1538 } 1539 1540 /* Allocate a pty and open it. */ 1541 debug("Allocating pty."); 1542 if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, 1543 sizeof(s->tty)))) { 1544 free(s->term); 1545 s->term = NULL; 1546 s->ptyfd = -1; 1547 s->ttyfd = -1; 1548 error("session_pty_req: session %d alloc failed", s->self); 1549 return 0; 1550 } 1551 debug("session_pty_req: session %d alloc %s", s->self, s->tty); 1552 1553 n_bytes = packet_remaining(); 1554 tty_parse_modes(s->ttyfd, &n_bytes); 1555 1556 if (!use_privsep) 1557 pty_setowner(s->pw, s->tty); 1558 1559 /* Set window size from the packet. */ 1560 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); 1561 1562 packet_check_eom(); 1563 session_proctitle(s); 1564 return 1; 1565 } 1566 1567 static int 1568 session_subsystem_req(struct ssh *ssh, Session *s) 1569 { 1570 struct stat st; 1571 u_int len; 1572 int success = 0; 1573 char *prog, *cmd; 1574 u_int i; 1575 1576 s->subsys = packet_get_string(&len); 1577 packet_check_eom(); 1578 debug2("subsystem request for %.100s by user %s", s->subsys, 1579 s->pw->pw_name); 1580 1581 for (i = 0; i < options.num_subsystems; i++) { 1582 if (strcmp(s->subsys, options.subsystem_name[i]) == 0) { 1583 prog = options.subsystem_command[i]; 1584 cmd = options.subsystem_args[i]; 1585 if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) { 1586 s->is_subsystem = SUBSYSTEM_INT_SFTP; 1587 debug("subsystem: %s", prog); 1588 } else { 1589 if (stat(prog, &st) < 0) 1590 debug("subsystem: cannot stat %s: %s", 1591 prog, strerror(errno)); 1592 s->is_subsystem = SUBSYSTEM_EXT; 1593 debug("subsystem: exec() %s", cmd); 1594 } 1595 success = do_exec(ssh, s, cmd) == 0; 1596 break; 1597 } 1598 } 1599 1600 if (!success) 1601 logit("subsystem request for %.100s by user %s failed, " 1602 "subsystem not found", s->subsys, s->pw->pw_name); 1603 1604 return success; 1605 } 1606 1607 static int 1608 session_x11_req(struct ssh *ssh, Session *s) 1609 { 1610 int success; 1611 1612 if (s->auth_proto != NULL || s->auth_data != NULL) { 1613 error("session_x11_req: session %d: " 1614 "x11 forwarding already active", s->self); 1615 return 0; 1616 } 1617 s->single_connection = packet_get_char(); 1618 s->auth_proto = packet_get_string(NULL); 1619 s->auth_data = packet_get_string(NULL); 1620 s->screen = packet_get_int(); 1621 packet_check_eom(); 1622 1623 if (xauth_valid_string(s->auth_proto) && 1624 xauth_valid_string(s->auth_data)) 1625 success = session_setup_x11fwd(ssh, s); 1626 else { 1627 success = 0; 1628 error("Invalid X11 forwarding data"); 1629 } 1630 if (!success) { 1631 free(s->auth_proto); 1632 free(s->auth_data); 1633 s->auth_proto = NULL; 1634 s->auth_data = NULL; 1635 } 1636 return success; 1637 } 1638 1639 static int 1640 session_shell_req(struct ssh *ssh, Session *s) 1641 { 1642 packet_check_eom(); 1643 return do_exec(ssh, s, NULL) == 0; 1644 } 1645 1646 static int 1647 session_exec_req(struct ssh *ssh, Session *s) 1648 { 1649 u_int len, success; 1650 1651 char *command = packet_get_string(&len); 1652 packet_check_eom(); 1653 success = do_exec(ssh, s, command) == 0; 1654 free(command); 1655 return success; 1656 } 1657 1658 static int 1659 session_break_req(struct ssh *ssh, Session *s) 1660 { 1661 1662 packet_get_int(); /* ignored */ 1663 packet_check_eom(); 1664 1665 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0) 1666 return 0; 1667 return 1; 1668 } 1669 1670 static int 1671 session_env_req(struct ssh *ssh, Session *s) 1672 { 1673 char *name, *val; 1674 u_int name_len, val_len, i; 1675 1676 name = packet_get_cstring(&name_len); 1677 val = packet_get_cstring(&val_len); 1678 packet_check_eom(); 1679 1680 /* Don't set too many environment variables */ 1681 if (s->num_env > 128) { 1682 debug2("Ignoring env request %s: too many env vars", name); 1683 goto fail; 1684 } 1685 1686 for (i = 0; i < options.num_accept_env; i++) { 1687 if (match_pattern(name, options.accept_env[i])) { 1688 debug2("Setting env %d: %s=%s", s->num_env, name, val); 1689 s->env = xrecallocarray(s->env, s->num_env, 1690 s->num_env + 1, sizeof(*s->env)); 1691 s->env[s->num_env].name = name; 1692 s->env[s->num_env].val = val; 1693 s->num_env++; 1694 return (1); 1695 } 1696 } 1697 debug2("Ignoring env request %s: disallowed name", name); 1698 1699 fail: 1700 free(name); 1701 free(val); 1702 return (0); 1703 } 1704 1705 static int 1706 session_auth_agent_req(struct ssh *ssh, Session *s) 1707 { 1708 static int called = 0; 1709 1710 packet_check_eom(); 1711 if (!auth_opts->permit_agent_forwarding_flag || 1712 !options.allow_agent_forwarding) { 1713 debug("%s: agent forwarding disabled", __func__); 1714 return 0; 1715 } 1716 if (called) { 1717 return 0; 1718 } else { 1719 called = 1; 1720 return auth_input_request_forwarding(ssh, s->pw); 1721 } 1722 } 1723 1724 int 1725 session_input_channel_req(struct ssh *ssh, Channel *c, const char *rtype) 1726 { 1727 int success = 0; 1728 Session *s; 1729 1730 if ((s = session_by_channel(c->self)) == NULL) { 1731 logit("%s: no session %d req %.100s", __func__, c->self, rtype); 1732 return 0; 1733 } 1734 debug("%s: session %d req %s", __func__, s->self, rtype); 1735 1736 /* 1737 * a session is in LARVAL state until a shell, a command 1738 * or a subsystem is executed 1739 */ 1740 if (c->type == SSH_CHANNEL_LARVAL) { 1741 if (strcmp(rtype, "shell") == 0) { 1742 success = session_shell_req(ssh, s); 1743 } else if (strcmp(rtype, "exec") == 0) { 1744 success = session_exec_req(ssh, s); 1745 } else if (strcmp(rtype, "pty-req") == 0) { 1746 success = session_pty_req(ssh, s); 1747 } else if (strcmp(rtype, "x11-req") == 0) { 1748 success = session_x11_req(ssh, s); 1749 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) { 1750 success = session_auth_agent_req(ssh, s); 1751 } else if (strcmp(rtype, "subsystem") == 0) { 1752 success = session_subsystem_req(ssh, s); 1753 } else if (strcmp(rtype, "env") == 0) { 1754 success = session_env_req(ssh, s); 1755 } 1756 } 1757 if (strcmp(rtype, "window-change") == 0) { 1758 success = session_window_change_req(ssh, s); 1759 } else if (strcmp(rtype, "break") == 0) { 1760 success = session_break_req(ssh, s); 1761 } 1762 1763 return success; 1764 } 1765 1766 void 1767 session_set_fds(struct ssh *ssh, Session *s, 1768 int fdin, int fdout, int fderr, int ignore_fderr, int is_tty) 1769 { 1770 /* 1771 * now that have a child and a pipe to the child, 1772 * we can activate our channel and register the fd's 1773 */ 1774 if (s->chanid == -1) 1775 fatal("no channel for session %d", s->self); 1776 channel_set_fds(ssh, s->chanid, 1777 fdout, fdin, fderr, 1778 ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, 1779 1, is_tty, CHAN_SES_WINDOW_DEFAULT); 1780 } 1781 1782 /* 1783 * Function to perform pty cleanup. Also called if we get aborted abnormally 1784 * (e.g., due to a dropped connection). 1785 */ 1786 void 1787 session_pty_cleanup2(Session *s) 1788 { 1789 if (s == NULL) { 1790 error("session_pty_cleanup: no session"); 1791 return; 1792 } 1793 if (s->ttyfd == -1) 1794 return; 1795 1796 debug("session_pty_cleanup: session %d release %s", s->self, s->tty); 1797 1798 /* Record that the user has logged out. */ 1799 if (s->pid != 0) 1800 record_logout(s->pid, s->tty); 1801 1802 /* Release the pseudo-tty. */ 1803 if (getuid() == 0) 1804 pty_release(s->tty); 1805 1806 /* 1807 * Close the server side of the socket pairs. We must do this after 1808 * the pty cleanup, so that another process doesn't get this pty 1809 * while we're still cleaning up. 1810 */ 1811 if (s->ptymaster != -1 && close(s->ptymaster) < 0) 1812 error("close(s->ptymaster/%d): %s", 1813 s->ptymaster, strerror(errno)); 1814 1815 /* unlink pty from session */ 1816 s->ttyfd = -1; 1817 } 1818 1819 void 1820 session_pty_cleanup(Session *s) 1821 { 1822 PRIVSEP(session_pty_cleanup2(s)); 1823 } 1824 1825 static char * 1826 sig2name(int sig) 1827 { 1828 #define SSH_SIG(x) if (sig == SIG ## x) return #x 1829 SSH_SIG(ABRT); 1830 SSH_SIG(ALRM); 1831 SSH_SIG(FPE); 1832 SSH_SIG(HUP); 1833 SSH_SIG(ILL); 1834 SSH_SIG(INT); 1835 SSH_SIG(KILL); 1836 SSH_SIG(PIPE); 1837 SSH_SIG(QUIT); 1838 SSH_SIG(SEGV); 1839 SSH_SIG(TERM); 1840 SSH_SIG(USR1); 1841 SSH_SIG(USR2); 1842 #undef SSH_SIG 1843 return "SIG@openssh.com"; 1844 } 1845 1846 static void 1847 session_close_x11(struct ssh *ssh, int id) 1848 { 1849 Channel *c; 1850 1851 if ((c = channel_by_id(ssh, id)) == NULL) { 1852 debug("%s: x11 channel %d missing", __func__, id); 1853 } else { 1854 /* Detach X11 listener */ 1855 debug("%s: detach x11 channel %d", __func__, id); 1856 channel_cancel_cleanup(ssh, id); 1857 if (c->ostate != CHAN_OUTPUT_CLOSED) 1858 chan_mark_dead(ssh, c); 1859 } 1860 } 1861 1862 static void 1863 session_close_single_x11(struct ssh *ssh, int id, void *arg) 1864 { 1865 Session *s; 1866 u_int i; 1867 1868 debug3("%s: channel %d", __func__, id); 1869 channel_cancel_cleanup(ssh, id); 1870 if ((s = session_by_x11_channel(id)) == NULL) 1871 fatal("%s: no x11 channel %d", __func__, id); 1872 for (i = 0; s->x11_chanids[i] != -1; i++) { 1873 debug("%s: session %d: closing channel %d", 1874 __func__, s->self, s->x11_chanids[i]); 1875 /* 1876 * The channel "id" is already closing, but make sure we 1877 * close all of its siblings. 1878 */ 1879 if (s->x11_chanids[i] != id) 1880 session_close_x11(ssh, s->x11_chanids[i]); 1881 } 1882 free(s->x11_chanids); 1883 s->x11_chanids = NULL; 1884 free(s->display); 1885 s->display = NULL; 1886 free(s->auth_proto); 1887 s->auth_proto = NULL; 1888 free(s->auth_data); 1889 s->auth_data = NULL; 1890 free(s->auth_display); 1891 s->auth_display = NULL; 1892 } 1893 1894 static void 1895 session_exit_message(struct ssh *ssh, Session *s, int status) 1896 { 1897 Channel *c; 1898 1899 if ((c = channel_lookup(ssh, s->chanid)) == NULL) 1900 fatal("%s: session %d: no channel %d", 1901 __func__, s->self, s->chanid); 1902 debug("%s: session %d channel %d pid %ld", 1903 __func__, s->self, s->chanid, (long)s->pid); 1904 1905 if (WIFEXITED(status)) { 1906 channel_request_start(ssh, s->chanid, "exit-status", 0); 1907 packet_put_int(WEXITSTATUS(status)); 1908 packet_send(); 1909 } else if (WIFSIGNALED(status)) { 1910 channel_request_start(ssh, s->chanid, "exit-signal", 0); 1911 packet_put_cstring(sig2name(WTERMSIG(status))); 1912 packet_put_char(WCOREDUMP(status)? 1 : 0); 1913 packet_put_cstring(""); 1914 packet_put_cstring(""); 1915 packet_send(); 1916 } else { 1917 /* Some weird exit cause. Just exit. */ 1918 packet_disconnect("wait returned status %04x.", status); 1919 } 1920 1921 /* disconnect channel */ 1922 debug("%s: release channel %d", __func__, s->chanid); 1923 1924 /* 1925 * Adjust cleanup callback attachment to send close messages when 1926 * the channel gets EOF. The session will be then be closed 1927 * by session_close_by_channel when the childs close their fds. 1928 */ 1929 channel_register_cleanup(ssh, c->self, session_close_by_channel, 1); 1930 1931 /* 1932 * emulate a write failure with 'chan_write_failed', nobody will be 1933 * interested in data we write. 1934 * Note that we must not call 'chan_read_failed', since there could 1935 * be some more data waiting in the pipe. 1936 */ 1937 if (c->ostate != CHAN_OUTPUT_CLOSED) 1938 chan_write_failed(ssh, c); 1939 } 1940 1941 void 1942 session_close(struct ssh *ssh, Session *s) 1943 { 1944 u_int i; 1945 1946 verbose("Close session: user %s from %.200s port %d id %d", 1947 s->pw->pw_name, 1948 ssh_remote_ipaddr(ssh), 1949 ssh_remote_port(ssh), 1950 s->self); 1951 1952 if (s->ttyfd != -1) 1953 session_pty_cleanup(s); 1954 free(s->term); 1955 free(s->display); 1956 free(s->x11_chanids); 1957 free(s->auth_display); 1958 free(s->auth_data); 1959 free(s->auth_proto); 1960 free(s->subsys); 1961 if (s->env != NULL) { 1962 for (i = 0; i < s->num_env; i++) { 1963 free(s->env[i].name); 1964 free(s->env[i].val); 1965 } 1966 free(s->env); 1967 } 1968 session_proctitle(s); 1969 session_unused(s->self); 1970 } 1971 1972 void 1973 session_close_by_pid(struct ssh *ssh, pid_t pid, int status) 1974 { 1975 Session *s = session_by_pid(pid); 1976 if (s == NULL) { 1977 debug("%s: no session for pid %ld", __func__, (long)pid); 1978 return; 1979 } 1980 if (s->chanid != -1) 1981 session_exit_message(ssh, s, status); 1982 if (s->ttyfd != -1) 1983 session_pty_cleanup(s); 1984 s->pid = 0; 1985 } 1986 1987 /* 1988 * this is called when a channel dies before 1989 * the session 'child' itself dies 1990 */ 1991 void 1992 session_close_by_channel(struct ssh *ssh, int id, void *arg) 1993 { 1994 Session *s = session_by_channel(id); 1995 u_int i; 1996 1997 if (s == NULL) { 1998 debug("%s: no session for id %d", __func__, id); 1999 return; 2000 } 2001 debug("%s: channel %d child %ld", __func__, id, (long)s->pid); 2002 if (s->pid != 0) { 2003 debug("%s: channel %d: has child", __func__, id); 2004 /* 2005 * delay detach of session, but release pty, since 2006 * the fd's to the child are already closed 2007 */ 2008 if (s->ttyfd != -1) 2009 session_pty_cleanup(s); 2010 return; 2011 } 2012 /* detach by removing callback */ 2013 channel_cancel_cleanup(ssh, s->chanid); 2014 2015 /* Close any X11 listeners associated with this session */ 2016 if (s->x11_chanids != NULL) { 2017 for (i = 0; s->x11_chanids[i] != -1; i++) { 2018 session_close_x11(ssh, s->x11_chanids[i]); 2019 s->x11_chanids[i] = -1; 2020 } 2021 } 2022 2023 s->chanid = -1; 2024 session_close(ssh, s); 2025 } 2026 2027 void 2028 session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *)) 2029 { 2030 int i; 2031 for (i = 0; i < sessions_nalloc; i++) { 2032 Session *s = &sessions[i]; 2033 if (s->used) { 2034 if (closefunc != NULL) 2035 closefunc(s); 2036 else 2037 session_close(ssh, s); 2038 } 2039 } 2040 } 2041 2042 static char * 2043 session_tty_list(void) 2044 { 2045 static char buf[1024]; 2046 int i; 2047 buf[0] = '\0'; 2048 for (i = 0; i < sessions_nalloc; i++) { 2049 Session *s = &sessions[i]; 2050 if (s->used && s->ttyfd != -1) { 2051 if (buf[0] != '\0') 2052 strlcat(buf, ",", sizeof buf); 2053 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf); 2054 } 2055 } 2056 if (buf[0] == '\0') 2057 strlcpy(buf, "notty", sizeof buf); 2058 return buf; 2059 } 2060 2061 void 2062 session_proctitle(Session *s) 2063 { 2064 if (s->pw == NULL) 2065 error("no user for session %d", s->self); 2066 else 2067 setproctitle("%s@%s", s->pw->pw_name, session_tty_list()); 2068 } 2069 2070 int 2071 session_setup_x11fwd(struct ssh *ssh, Session *s) 2072 { 2073 struct stat st; 2074 char display[512], auth_display[512]; 2075 char hostname[NI_MAXHOST]; 2076 u_int i; 2077 2078 if (!auth_opts->permit_x11_forwarding_flag) { 2079 packet_send_debug("X11 forwarding disabled by key options."); 2080 return 0; 2081 } 2082 if (!options.x11_forwarding) { 2083 debug("X11 forwarding disabled in server configuration file."); 2084 return 0; 2085 } 2086 if (options.xauth_location == NULL || 2087 (stat(options.xauth_location, &st) == -1)) { 2088 packet_send_debug("No xauth program; cannot forward X11."); 2089 return 0; 2090 } 2091 if (s->display != NULL) { 2092 debug("X11 display already set."); 2093 return 0; 2094 } 2095 if (x11_create_display_inet(ssh, options.x11_display_offset, 2096 options.x11_use_localhost, s->single_connection, 2097 &s->display_number, &s->x11_chanids) == -1) { 2098 debug("x11_create_display_inet failed."); 2099 return 0; 2100 } 2101 for (i = 0; s->x11_chanids[i] != -1; i++) { 2102 channel_register_cleanup(ssh, s->x11_chanids[i], 2103 session_close_single_x11, 0); 2104 } 2105 2106 /* Set up a suitable value for the DISPLAY variable. */ 2107 if (gethostname(hostname, sizeof(hostname)) < 0) 2108 fatal("gethostname: %.100s", strerror(errno)); 2109 /* 2110 * auth_display must be used as the displayname when the 2111 * authorization entry is added with xauth(1). This will be 2112 * different than the DISPLAY string for localhost displays. 2113 */ 2114 if (options.x11_use_localhost) { 2115 snprintf(display, sizeof display, "localhost:%u.%u", 2116 s->display_number, s->screen); 2117 snprintf(auth_display, sizeof auth_display, "unix:%u.%u", 2118 s->display_number, s->screen); 2119 s->display = xstrdup(display); 2120 s->auth_display = xstrdup(auth_display); 2121 } else { 2122 snprintf(display, sizeof display, "%.400s:%u.%u", hostname, 2123 s->display_number, s->screen); 2124 s->display = xstrdup(display); 2125 s->auth_display = xstrdup(display); 2126 } 2127 2128 return 1; 2129 } 2130 2131 static void 2132 do_authenticated2(struct ssh *ssh, Authctxt *authctxt) 2133 { 2134 server_loop2(ssh, authctxt); 2135 } 2136 2137 void 2138 do_cleanup(struct ssh *ssh, Authctxt *authctxt) 2139 { 2140 static int called = 0; 2141 2142 debug("do_cleanup"); 2143 2144 /* no cleanup if we're in the child for login shell */ 2145 if (is_child) 2146 return; 2147 2148 /* avoid double cleanup */ 2149 if (called) 2150 return; 2151 called = 1; 2152 2153 if (authctxt == NULL || !authctxt->authenticated) 2154 return; 2155 #ifdef KRB5 2156 if (options.kerberos_ticket_cleanup && 2157 authctxt->krb5_ctx) 2158 krb5_cleanup_proc(authctxt); 2159 #endif 2160 2161 #ifdef GSSAPI 2162 if (options.gss_cleanup_creds) 2163 ssh_gssapi_cleanup_creds(); 2164 #endif 2165 2166 /* remove agent socket */ 2167 auth_sock_cleanup_proc(authctxt->pw); 2168 2169 /* remove userauth info */ 2170 if (auth_info_file != NULL) { 2171 temporarily_use_uid(authctxt->pw); 2172 unlink(auth_info_file); 2173 restore_uid(); 2174 free(auth_info_file); 2175 auth_info_file = NULL; 2176 } 2177 2178 /* 2179 * Cleanup ptys/utmp only if privsep is disabled, 2180 * or if running in monitor. 2181 */ 2182 if (!use_privsep || mm_is_monitor()) 2183 session_destroy_all(ssh, session_pty_cleanup2); 2184 } 2185 2186 /* Return a name for the remote host that fits inside utmp_size */ 2187 2188 const char * 2189 session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns) 2190 { 2191 const char *remote = ""; 2192 2193 if (utmp_size > 0) 2194 remote = auth_get_canonical_hostname(ssh, use_dns); 2195 if (utmp_size == 0 || strlen(remote) > utmp_size) 2196 remote = ssh_remote_ipaddr(ssh); 2197 return remote; 2198 } 2199 2200