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