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