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