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