1 /* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * The main loop for the interactive session (client side). 6 * 7 * As far as I am concerned, the code I have written for this software 8 * can be used freely for any purpose. Any derived versions of this 9 * software must be clearly marked as such, and if the derived work is 10 * incompatible with the protocol description in the RFC file, it must be 11 * called by a name other than "ssh" or "Secure Shell". 12 * 13 * 14 * Copyright (c) 1999 Theo de Raadt. All rights reserved. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 * 36 * 37 * SSH2 support added by Markus Friedl. 38 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61 #include "includes.h" 62 RCSID("$OpenBSD: clientloop.c,v 1.115 2003/09/23 20:41:11 markus Exp $"); 63 64 #include "ssh.h" 65 #include "ssh1.h" 66 #include "ssh2.h" 67 #include "xmalloc.h" 68 #include "packet.h" 69 #include "buffer.h" 70 #include "compat.h" 71 #include "channels.h" 72 #include "dispatch.h" 73 #include "buffer.h" 74 #include "bufaux.h" 75 #include "key.h" 76 #include "kex.h" 77 #include "log.h" 78 #include "readconf.h" 79 #include "clientloop.h" 80 #include "authfd.h" 81 #include "atomicio.h" 82 #include "sshtty.h" 83 #include "misc.h" 84 #include "readpass.h" 85 86 /* import options */ 87 extern Options options; 88 89 /* Flag indicating that stdin should be redirected from /dev/null. */ 90 extern int stdin_null_flag; 91 92 /* 93 * Name of the host we are connecting to. This is the name given on the 94 * command line, or the HostName specified for the user-supplied name in a 95 * configuration file. 96 */ 97 extern char *host; 98 99 /* 100 * Flag to indicate that we have received a window change signal which has 101 * not yet been processed. This will cause a message indicating the new 102 * window size to be sent to the server a little later. This is volatile 103 * because this is updated in a signal handler. 104 */ 105 static volatile sig_atomic_t received_window_change_signal = 0; 106 static volatile sig_atomic_t received_signal = 0; 107 108 /* Flag indicating whether the user\'s terminal is in non-blocking mode. */ 109 static int in_non_blocking_mode = 0; 110 111 /* Common data for the client loop code. */ 112 static int quit_pending; /* Set to non-zero to quit the client loop. */ 113 static int escape_char; /* Escape character. */ 114 static int escape_pending; /* Last character was the escape character */ 115 static int last_was_cr; /* Last character was a newline. */ 116 static int exit_status; /* Used to store the exit status of the command. */ 117 static int stdin_eof; /* EOF has been encountered on standard error. */ 118 static Buffer stdin_buffer; /* Buffer for stdin data. */ 119 static Buffer stdout_buffer; /* Buffer for stdout data. */ 120 static Buffer stderr_buffer; /* Buffer for stderr data. */ 121 static u_long stdin_bytes, stdout_bytes, stderr_bytes; 122 static u_int buffer_high;/* Soft max buffer size. */ 123 static int connection_in; /* Connection to server (input). */ 124 static int connection_out; /* Connection to server (output). */ 125 static int need_rekeying; /* Set to non-zero if rekeying is requested. */ 126 static int session_closed = 0; /* In SSH2: login session closed. */ 127 128 static void client_init_dispatch(void); 129 int session_ident = -1; 130 131 /*XXX*/ 132 extern Kex *xxx_kex; 133 134 /* Restores stdin to blocking mode. */ 135 136 static void 137 leave_non_blocking(void) 138 { 139 if (in_non_blocking_mode) { 140 (void) fcntl(fileno(stdin), F_SETFL, 0); 141 in_non_blocking_mode = 0; 142 } 143 } 144 145 /* Puts stdin terminal in non-blocking mode. */ 146 147 static void 148 enter_non_blocking(void) 149 { 150 in_non_blocking_mode = 1; 151 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK); 152 } 153 154 /* 155 * Signal handler for the window change signal (SIGWINCH). This just sets a 156 * flag indicating that the window has changed. 157 */ 158 159 static void 160 window_change_handler(int sig) 161 { 162 received_window_change_signal = 1; 163 signal(SIGWINCH, window_change_handler); 164 } 165 166 /* 167 * Signal handler for signals that cause the program to terminate. These 168 * signals must be trapped to restore terminal modes. 169 */ 170 171 static void 172 signal_handler(int sig) 173 { 174 received_signal = sig; 175 quit_pending = 1; 176 } 177 178 /* 179 * Returns current time in seconds from Jan 1, 1970 with the maximum 180 * available resolution. 181 */ 182 183 static double 184 get_current_time(void) 185 { 186 struct timeval tv; 187 gettimeofday(&tv, NULL); 188 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0; 189 } 190 191 /* 192 * This is called when the interactive is entered. This checks if there is 193 * an EOF coming on stdin. We must check this explicitly, as select() does 194 * not appear to wake up when redirecting from /dev/null. 195 */ 196 197 static void 198 client_check_initial_eof_on_stdin(void) 199 { 200 int len; 201 char buf[1]; 202 203 /* 204 * If standard input is to be "redirected from /dev/null", we simply 205 * mark that we have seen an EOF and send an EOF message to the 206 * server. Otherwise, we try to read a single character; it appears 207 * that for some files, such /dev/null, select() never wakes up for 208 * read for this descriptor, which means that we never get EOF. This 209 * way we will get the EOF if stdin comes from /dev/null or similar. 210 */ 211 if (stdin_null_flag) { 212 /* Fake EOF on stdin. */ 213 debug("Sending eof."); 214 stdin_eof = 1; 215 packet_start(SSH_CMSG_EOF); 216 packet_send(); 217 } else { 218 enter_non_blocking(); 219 220 /* Check for immediate EOF on stdin. */ 221 len = read(fileno(stdin), buf, 1); 222 if (len == 0) { 223 /* EOF. Record that we have seen it and send EOF to server. */ 224 debug("Sending eof."); 225 stdin_eof = 1; 226 packet_start(SSH_CMSG_EOF); 227 packet_send(); 228 } else if (len > 0) { 229 /* 230 * Got data. We must store the data in the buffer, 231 * and also process it as an escape character if 232 * appropriate. 233 */ 234 if ((u_char) buf[0] == escape_char) 235 escape_pending = 1; 236 else 237 buffer_append(&stdin_buffer, buf, 1); 238 } 239 leave_non_blocking(); 240 } 241 } 242 243 244 /* 245 * Make packets from buffered stdin data, and buffer them for sending to the 246 * connection. 247 */ 248 249 static void 250 client_make_packets_from_stdin_data(void) 251 { 252 u_int len; 253 254 /* Send buffered stdin data to the server. */ 255 while (buffer_len(&stdin_buffer) > 0 && 256 packet_not_very_much_data_to_write()) { 257 len = buffer_len(&stdin_buffer); 258 /* Keep the packets at reasonable size. */ 259 if (len > packet_get_maxsize()) 260 len = packet_get_maxsize(); 261 packet_start(SSH_CMSG_STDIN_DATA); 262 packet_put_string(buffer_ptr(&stdin_buffer), len); 263 packet_send(); 264 buffer_consume(&stdin_buffer, len); 265 stdin_bytes += len; 266 /* If we have a pending EOF, send it now. */ 267 if (stdin_eof && buffer_len(&stdin_buffer) == 0) { 268 packet_start(SSH_CMSG_EOF); 269 packet_send(); 270 } 271 } 272 } 273 274 /* 275 * Checks if the client window has changed, and sends a packet about it to 276 * the server if so. The actual change is detected elsewhere (by a software 277 * interrupt on Unix); this just checks the flag and sends a message if 278 * appropriate. 279 */ 280 281 static void 282 client_check_window_change(void) 283 { 284 struct winsize ws; 285 286 if (! received_window_change_signal) 287 return; 288 /** XXX race */ 289 received_window_change_signal = 0; 290 291 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) 292 return; 293 294 debug2("client_check_window_change: changed"); 295 296 if (compat20) { 297 channel_request_start(session_ident, "window-change", 0); 298 packet_put_int(ws.ws_col); 299 packet_put_int(ws.ws_row); 300 packet_put_int(ws.ws_xpixel); 301 packet_put_int(ws.ws_ypixel); 302 packet_send(); 303 } else { 304 packet_start(SSH_CMSG_WINDOW_SIZE); 305 packet_put_int(ws.ws_row); 306 packet_put_int(ws.ws_col); 307 packet_put_int(ws.ws_xpixel); 308 packet_put_int(ws.ws_ypixel); 309 packet_send(); 310 } 311 } 312 313 /* 314 * Waits until the client can do something (some data becomes available on 315 * one of the file descriptors). 316 */ 317 318 static void 319 client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, 320 int *maxfdp, int *nallocp, int rekeying) 321 { 322 /* Add any selections by the channel mechanism. */ 323 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying); 324 325 if (!compat20) { 326 /* Read from the connection, unless our buffers are full. */ 327 if (buffer_len(&stdout_buffer) < buffer_high && 328 buffer_len(&stderr_buffer) < buffer_high && 329 channel_not_very_much_buffered_data()) 330 FD_SET(connection_in, *readsetp); 331 /* 332 * Read from stdin, unless we have seen EOF or have very much 333 * buffered data to send to the server. 334 */ 335 if (!stdin_eof && packet_not_very_much_data_to_write()) 336 FD_SET(fileno(stdin), *readsetp); 337 338 /* Select stdout/stderr if have data in buffer. */ 339 if (buffer_len(&stdout_buffer) > 0) 340 FD_SET(fileno(stdout), *writesetp); 341 if (buffer_len(&stderr_buffer) > 0) 342 FD_SET(fileno(stderr), *writesetp); 343 } else { 344 /* channel_prepare_select could have closed the last channel */ 345 if (session_closed && !channel_still_open() && 346 !packet_have_data_to_write()) { 347 /* clear mask since we did not call select() */ 348 memset(*readsetp, 0, *nallocp); 349 memset(*writesetp, 0, *nallocp); 350 return; 351 } else { 352 FD_SET(connection_in, *readsetp); 353 } 354 } 355 356 /* Select server connection if have data to write to the server. */ 357 if (packet_have_data_to_write()) 358 FD_SET(connection_out, *writesetp); 359 360 /* 361 * Wait for something to happen. This will suspend the process until 362 * some selected descriptor can be read, written, or has some other 363 * event pending. Note: if you want to implement SSH_MSG_IGNORE 364 * messages to fool traffic analysis, this might be the place to do 365 * it: just have a random timeout for the select, and send a random 366 * SSH_MSG_IGNORE packet when the timeout expires. 367 */ 368 369 if (select((*maxfdp)+1, *readsetp, *writesetp, NULL, NULL) < 0) { 370 char buf[100]; 371 372 /* 373 * We have to clear the select masks, because we return. 374 * We have to return, because the mainloop checks for the flags 375 * set by the signal handlers. 376 */ 377 memset(*readsetp, 0, *nallocp); 378 memset(*writesetp, 0, *nallocp); 379 380 if (errno == EINTR) 381 return; 382 /* Note: we might still have data in the buffers. */ 383 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno)); 384 buffer_append(&stderr_buffer, buf, strlen(buf)); 385 quit_pending = 1; 386 } 387 } 388 389 static void 390 client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr) 391 { 392 struct winsize oldws, newws; 393 394 /* Flush stdout and stderr buffers. */ 395 if (buffer_len(bout) > 0) 396 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout)); 397 if (buffer_len(berr) > 0) 398 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr)); 399 400 leave_raw_mode(); 401 402 /* 403 * Free (and clear) the buffer to reduce the amount of data that gets 404 * written to swap. 405 */ 406 buffer_free(bin); 407 buffer_free(bout); 408 buffer_free(berr); 409 410 /* Save old window size. */ 411 ioctl(fileno(stdin), TIOCGWINSZ, &oldws); 412 413 /* Send the suspend signal to the program itself. */ 414 kill(getpid(), SIGTSTP); 415 416 /* Check if the window size has changed. */ 417 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 && 418 (oldws.ws_row != newws.ws_row || 419 oldws.ws_col != newws.ws_col || 420 oldws.ws_xpixel != newws.ws_xpixel || 421 oldws.ws_ypixel != newws.ws_ypixel)) 422 received_window_change_signal = 1; 423 424 /* OK, we have been continued by the user. Reinitialize buffers. */ 425 buffer_init(bin); 426 buffer_init(bout); 427 buffer_init(berr); 428 429 enter_raw_mode(); 430 } 431 432 static void 433 client_process_net_input(fd_set * readset) 434 { 435 int len; 436 char buf[8192]; 437 438 /* 439 * Read input from the server, and add any such data to the buffer of 440 * the packet subsystem. 441 */ 442 if (FD_ISSET(connection_in, readset)) { 443 /* Read as much as possible. */ 444 len = read(connection_in, buf, sizeof(buf)); 445 if (len == 0) { 446 /* Received EOF. The remote host has closed the connection. */ 447 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n", 448 host); 449 buffer_append(&stderr_buffer, buf, strlen(buf)); 450 quit_pending = 1; 451 return; 452 } 453 /* 454 * There is a kernel bug on Solaris that causes select to 455 * sometimes wake up even though there is no data available. 456 */ 457 if (len < 0 && (errno == EAGAIN || errno == EINTR)) 458 len = 0; 459 460 if (len < 0) { 461 /* An error has encountered. Perhaps there is a network problem. */ 462 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n", 463 host, strerror(errno)); 464 buffer_append(&stderr_buffer, buf, strlen(buf)); 465 quit_pending = 1; 466 return; 467 } 468 packet_process_incoming(buf, len); 469 } 470 } 471 472 static void 473 process_cmdline(void) 474 { 475 void (*handler)(int); 476 char *s, *cmd; 477 u_short fwd_port, fwd_host_port; 478 char buf[1024], sfwd_port[6], sfwd_host_port[6]; 479 int local = 0; 480 481 leave_raw_mode(); 482 handler = signal(SIGINT, SIG_IGN); 483 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO); 484 if (s == NULL) 485 goto out; 486 while (*s && isspace(*s)) 487 s++; 488 if (*s == 0) 489 goto out; 490 if (strlen(s) < 2 || s[0] != '-' || !(s[1] == 'L' || s[1] == 'R')) { 491 logit("Invalid command."); 492 goto out; 493 } 494 if (s[1] == 'L') 495 local = 1; 496 if (!local && !compat20) { 497 logit("Not supported for SSH protocol version 1."); 498 goto out; 499 } 500 s += 2; 501 while (*s && isspace(*s)) 502 s++; 503 504 if (sscanf(s, "%5[0-9]:%255[^:]:%5[0-9]", 505 sfwd_port, buf, sfwd_host_port) != 3 && 506 sscanf(s, "%5[0-9]/%255[^/]/%5[0-9]", 507 sfwd_port, buf, sfwd_host_port) != 3) { 508 logit("Bad forwarding specification."); 509 goto out; 510 } 511 if ((fwd_port = a2port(sfwd_port)) == 0 || 512 (fwd_host_port = a2port(sfwd_host_port)) == 0) { 513 logit("Bad forwarding port(s)."); 514 goto out; 515 } 516 if (local) { 517 if (channel_setup_local_fwd_listener(fwd_port, buf, 518 fwd_host_port, options.gateway_ports) < 0) { 519 logit("Port forwarding failed."); 520 goto out; 521 } 522 } else 523 channel_request_remote_forwarding(fwd_port, buf, 524 fwd_host_port); 525 logit("Forwarding port."); 526 out: 527 signal(SIGINT, handler); 528 enter_raw_mode(); 529 if (cmd) 530 xfree(cmd); 531 } 532 533 /* process the characters one by one */ 534 static int 535 process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len) 536 { 537 char string[1024]; 538 pid_t pid; 539 int bytes = 0; 540 u_int i; 541 u_char ch; 542 char *s; 543 544 for (i = 0; i < len; i++) { 545 /* Get one character at a time. */ 546 ch = buf[i]; 547 548 if (escape_pending) { 549 /* We have previously seen an escape character. */ 550 /* Clear the flag now. */ 551 escape_pending = 0; 552 553 /* Process the escaped character. */ 554 switch (ch) { 555 case '.': 556 /* Terminate the connection. */ 557 snprintf(string, sizeof string, "%c.\r\n", escape_char); 558 buffer_append(berr, string, strlen(string)); 559 560 quit_pending = 1; 561 return -1; 562 563 case 'Z' - 64: 564 /* Suspend the program. */ 565 /* Print a message to that effect to the user. */ 566 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char); 567 buffer_append(berr, string, strlen(string)); 568 569 /* Restore terminal modes and suspend. */ 570 client_suspend_self(bin, bout, berr); 571 572 /* We have been continued. */ 573 continue; 574 575 case 'B': 576 if (compat20) { 577 snprintf(string, sizeof string, 578 "%cB\r\n", escape_char); 579 buffer_append(berr, string, 580 strlen(string)); 581 channel_request_start(session_ident, 582 "break", 0); 583 packet_put_int(1000); 584 packet_send(); 585 } 586 continue; 587 588 case 'R': 589 if (compat20) { 590 if (datafellows & SSH_BUG_NOREKEY) 591 logit("Server does not support re-keying"); 592 else 593 need_rekeying = 1; 594 } 595 continue; 596 597 case '&': 598 /* 599 * Detach the program (continue to serve connections, 600 * but put in background and no more new connections). 601 */ 602 /* Restore tty modes. */ 603 leave_raw_mode(); 604 605 /* Stop listening for new connections. */ 606 channel_stop_listening(); 607 608 snprintf(string, sizeof string, 609 "%c& [backgrounded]\n", escape_char); 610 buffer_append(berr, string, strlen(string)); 611 612 /* Fork into background. */ 613 pid = fork(); 614 if (pid < 0) { 615 error("fork: %.100s", strerror(errno)); 616 continue; 617 } 618 if (pid != 0) { /* This is the parent. */ 619 /* The parent just exits. */ 620 exit(0); 621 } 622 /* The child continues serving connections. */ 623 if (compat20) { 624 buffer_append(bin, "\004", 1); 625 /* fake EOF on stdin */ 626 return -1; 627 } else if (!stdin_eof) { 628 /* 629 * Sending SSH_CMSG_EOF alone does not always appear 630 * to be enough. So we try to send an EOF character 631 * first. 632 */ 633 packet_start(SSH_CMSG_STDIN_DATA); 634 packet_put_string("\004", 1); 635 packet_send(); 636 /* Close stdin. */ 637 stdin_eof = 1; 638 if (buffer_len(bin) == 0) { 639 packet_start(SSH_CMSG_EOF); 640 packet_send(); 641 } 642 } 643 continue; 644 645 case '?': 646 snprintf(string, sizeof string, 647 "%c?\r\n\ 648 Supported escape sequences:\r\n\ 649 %c. - terminate connection\r\n\ 650 %cB - send a BREAK to the remote system\r\n\ 651 %cC - open a command line\r\n\ 652 %cR - Request rekey (SSH protocol 2 only)\r\n\ 653 %c^Z - suspend ssh\r\n\ 654 %c# - list forwarded connections\r\n\ 655 %c& - background ssh (when waiting for connections to terminate)\r\n\ 656 %c? - this message\r\n\ 657 %c%c - send the escape character by typing it twice\r\n\ 658 (Note that escapes are only recognized immediately after newline.)\r\n", 659 escape_char, escape_char, escape_char, escape_char, 660 escape_char, escape_char, escape_char, escape_char, 661 escape_char, escape_char, escape_char); 662 buffer_append(berr, string, strlen(string)); 663 continue; 664 665 case '#': 666 snprintf(string, sizeof string, "%c#\r\n", escape_char); 667 buffer_append(berr, string, strlen(string)); 668 s = channel_open_message(); 669 buffer_append(berr, s, strlen(s)); 670 xfree(s); 671 continue; 672 673 case 'C': 674 process_cmdline(); 675 continue; 676 677 default: 678 if (ch != escape_char) { 679 buffer_put_char(bin, escape_char); 680 bytes++; 681 } 682 /* Escaped characters fall through here */ 683 break; 684 } 685 } else { 686 /* 687 * The previous character was not an escape char. Check if this 688 * is an escape. 689 */ 690 if (last_was_cr && ch == escape_char) { 691 /* It is. Set the flag and continue to next character. */ 692 escape_pending = 1; 693 continue; 694 } 695 } 696 697 /* 698 * Normal character. Record whether it was a newline, 699 * and append it to the buffer. 700 */ 701 last_was_cr = (ch == '\r' || ch == '\n'); 702 buffer_put_char(bin, ch); 703 bytes++; 704 } 705 return bytes; 706 } 707 708 static void 709 client_process_input(fd_set * readset) 710 { 711 int len; 712 char buf[8192]; 713 714 /* Read input from stdin. */ 715 if (FD_ISSET(fileno(stdin), readset)) { 716 /* Read as much as possible. */ 717 len = read(fileno(stdin), buf, sizeof(buf)); 718 if (len < 0 && (errno == EAGAIN || errno == EINTR)) 719 return; /* we'll try again later */ 720 if (len <= 0) { 721 /* 722 * Received EOF or error. They are treated 723 * similarly, except that an error message is printed 724 * if it was an error condition. 725 */ 726 if (len < 0) { 727 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno)); 728 buffer_append(&stderr_buffer, buf, strlen(buf)); 729 } 730 /* Mark that we have seen EOF. */ 731 stdin_eof = 1; 732 /* 733 * Send an EOF message to the server unless there is 734 * data in the buffer. If there is data in the 735 * buffer, no message will be sent now. Code 736 * elsewhere will send the EOF when the buffer 737 * becomes empty if stdin_eof is set. 738 */ 739 if (buffer_len(&stdin_buffer) == 0) { 740 packet_start(SSH_CMSG_EOF); 741 packet_send(); 742 } 743 } else if (escape_char == SSH_ESCAPECHAR_NONE) { 744 /* 745 * Normal successful read, and no escape character. 746 * Just append the data to buffer. 747 */ 748 buffer_append(&stdin_buffer, buf, len); 749 } else { 750 /* 751 * Normal, successful read. But we have an escape character 752 * and have to process the characters one by one. 753 */ 754 if (process_escapes(&stdin_buffer, &stdout_buffer, 755 &stderr_buffer, buf, len) == -1) 756 return; 757 } 758 } 759 } 760 761 static void 762 client_process_output(fd_set * writeset) 763 { 764 int len; 765 char buf[100]; 766 767 /* Write buffered output to stdout. */ 768 if (FD_ISSET(fileno(stdout), writeset)) { 769 /* Write as much data as possible. */ 770 len = write(fileno(stdout), buffer_ptr(&stdout_buffer), 771 buffer_len(&stdout_buffer)); 772 if (len <= 0) { 773 if (errno == EINTR || errno == EAGAIN) 774 len = 0; 775 else { 776 /* 777 * An error or EOF was encountered. Put an 778 * error message to stderr buffer. 779 */ 780 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno)); 781 buffer_append(&stderr_buffer, buf, strlen(buf)); 782 quit_pending = 1; 783 return; 784 } 785 } 786 /* Consume printed data from the buffer. */ 787 buffer_consume(&stdout_buffer, len); 788 stdout_bytes += len; 789 } 790 /* Write buffered output to stderr. */ 791 if (FD_ISSET(fileno(stderr), writeset)) { 792 /* Write as much data as possible. */ 793 len = write(fileno(stderr), buffer_ptr(&stderr_buffer), 794 buffer_len(&stderr_buffer)); 795 if (len <= 0) { 796 if (errno == EINTR || errno == EAGAIN) 797 len = 0; 798 else { 799 /* EOF or error, but can't even print error message. */ 800 quit_pending = 1; 801 return; 802 } 803 } 804 /* Consume printed characters from the buffer. */ 805 buffer_consume(&stderr_buffer, len); 806 stderr_bytes += len; 807 } 808 } 809 810 /* 811 * Get packets from the connection input buffer, and process them as long as 812 * there are packets available. 813 * 814 * Any unknown packets received during the actual 815 * session cause the session to terminate. This is 816 * intended to make debugging easier since no 817 * confirmations are sent. Any compatible protocol 818 * extensions must be negotiated during the 819 * preparatory phase. 820 */ 821 822 static void 823 client_process_buffered_input_packets(void) 824 { 825 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL); 826 } 827 828 /* scan buf[] for '~' before sending data to the peer */ 829 830 static int 831 simple_escape_filter(Channel *c, char *buf, int len) 832 { 833 /* XXX we assume c->extended is writeable */ 834 return process_escapes(&c->input, &c->output, &c->extended, buf, len); 835 } 836 837 static void 838 client_channel_closed(int id, void *arg) 839 { 840 if (id != session_ident) 841 error("client_channel_closed: id %d != session_ident %d", 842 id, session_ident); 843 channel_cancel_cleanup(id); 844 session_closed = 1; 845 leave_raw_mode(); 846 } 847 848 /* 849 * Implements the interactive session with the server. This is called after 850 * the user has been authenticated, and a command has been started on the 851 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character 852 * used as an escape character for terminating or suspending the session. 853 */ 854 855 int 856 client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id) 857 { 858 fd_set *readset = NULL, *writeset = NULL; 859 double start_time, total_time; 860 int max_fd = 0, max_fd2 = 0, len, rekeying = 0, nalloc = 0; 861 char buf[100]; 862 863 debug("Entering interactive session."); 864 865 start_time = get_current_time(); 866 867 /* Initialize variables. */ 868 escape_pending = 0; 869 last_was_cr = 1; 870 exit_status = -1; 871 stdin_eof = 0; 872 buffer_high = 64 * 1024; 873 connection_in = packet_get_connection_in(); 874 connection_out = packet_get_connection_out(); 875 max_fd = MAX(connection_in, connection_out); 876 877 if (!compat20) { 878 /* enable nonblocking unless tty */ 879 if (!isatty(fileno(stdin))) 880 set_nonblock(fileno(stdin)); 881 if (!isatty(fileno(stdout))) 882 set_nonblock(fileno(stdout)); 883 if (!isatty(fileno(stderr))) 884 set_nonblock(fileno(stderr)); 885 max_fd = MAX(max_fd, fileno(stdin)); 886 max_fd = MAX(max_fd, fileno(stdout)); 887 max_fd = MAX(max_fd, fileno(stderr)); 888 } 889 stdin_bytes = 0; 890 stdout_bytes = 0; 891 stderr_bytes = 0; 892 quit_pending = 0; 893 escape_char = escape_char_arg; 894 895 /* Initialize buffers. */ 896 buffer_init(&stdin_buffer); 897 buffer_init(&stdout_buffer); 898 buffer_init(&stderr_buffer); 899 900 client_init_dispatch(); 901 902 /* 903 * Set signal handlers, (e.g. to restore non-blocking mode) 904 * but don't overwrite SIG_IGN, matches behaviour from rsh(1) 905 */ 906 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 907 signal(SIGINT, signal_handler); 908 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) 909 signal(SIGQUIT, signal_handler); 910 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 911 signal(SIGTERM, signal_handler); 912 if (have_pty) 913 signal(SIGWINCH, window_change_handler); 914 915 if (have_pty) 916 enter_raw_mode(); 917 918 if (compat20) { 919 session_ident = ssh2_chan_id; 920 if (escape_char != SSH_ESCAPECHAR_NONE) 921 channel_register_filter(session_ident, 922 simple_escape_filter); 923 if (session_ident != -1) 924 channel_register_cleanup(session_ident, 925 client_channel_closed); 926 } else { 927 /* Check if we should immediately send eof on stdin. */ 928 client_check_initial_eof_on_stdin(); 929 } 930 931 /* Main loop of the client for the interactive session mode. */ 932 while (!quit_pending) { 933 934 /* Process buffered packets sent by the server. */ 935 client_process_buffered_input_packets(); 936 937 if (compat20 && session_closed && !channel_still_open()) 938 break; 939 940 rekeying = (xxx_kex != NULL && !xxx_kex->done); 941 942 if (rekeying) { 943 debug("rekeying in progress"); 944 } else { 945 /* 946 * Make packets of buffered stdin data, and buffer 947 * them for sending to the server. 948 */ 949 if (!compat20) 950 client_make_packets_from_stdin_data(); 951 952 /* 953 * Make packets from buffered channel data, and 954 * enqueue them for sending to the server. 955 */ 956 if (packet_not_very_much_data_to_write()) 957 channel_output_poll(); 958 959 /* 960 * Check if the window size has changed, and buffer a 961 * message about it to the server if so. 962 */ 963 client_check_window_change(); 964 965 if (quit_pending) 966 break; 967 } 968 /* 969 * Wait until we have something to do (something becomes 970 * available on one of the descriptors). 971 */ 972 max_fd2 = max_fd; 973 client_wait_until_can_do_something(&readset, &writeset, 974 &max_fd2, &nalloc, rekeying); 975 976 if (quit_pending) 977 break; 978 979 /* Do channel operations unless rekeying in progress. */ 980 if (!rekeying) { 981 channel_after_select(readset, writeset); 982 if (need_rekeying || packet_need_rekeying()) { 983 debug("need rekeying"); 984 xxx_kex->done = 0; 985 kex_send_kexinit(xxx_kex); 986 need_rekeying = 0; 987 } 988 } 989 990 /* Buffer input from the connection. */ 991 client_process_net_input(readset); 992 993 if (quit_pending) 994 break; 995 996 if (!compat20) { 997 /* Buffer data from stdin */ 998 client_process_input(readset); 999 /* 1000 * Process output to stdout and stderr. Output to 1001 * the connection is processed elsewhere (above). 1002 */ 1003 client_process_output(writeset); 1004 } 1005 1006 /* Send as much buffered packet data as possible to the sender. */ 1007 if (FD_ISSET(connection_out, writeset)) 1008 packet_write_poll(); 1009 } 1010 if (readset) 1011 xfree(readset); 1012 if (writeset) 1013 xfree(writeset); 1014 1015 /* Terminate the session. */ 1016 1017 /* Stop watching for window change. */ 1018 if (have_pty) 1019 signal(SIGWINCH, SIG_DFL); 1020 1021 channel_free_all(); 1022 1023 if (have_pty) 1024 leave_raw_mode(); 1025 1026 /* restore blocking io */ 1027 if (!isatty(fileno(stdin))) 1028 unset_nonblock(fileno(stdin)); 1029 if (!isatty(fileno(stdout))) 1030 unset_nonblock(fileno(stdout)); 1031 if (!isatty(fileno(stderr))) 1032 unset_nonblock(fileno(stderr)); 1033 1034 if (received_signal) 1035 fatal("Killed by signal %d.", (int) received_signal); 1036 1037 /* 1038 * In interactive mode (with pseudo tty) display a message indicating 1039 * that the connection has been closed. 1040 */ 1041 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { 1042 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host); 1043 buffer_append(&stderr_buffer, buf, strlen(buf)); 1044 } 1045 1046 /* Output any buffered data for stdout. */ 1047 while (buffer_len(&stdout_buffer) > 0) { 1048 len = write(fileno(stdout), buffer_ptr(&stdout_buffer), 1049 buffer_len(&stdout_buffer)); 1050 if (len <= 0) { 1051 error("Write failed flushing stdout buffer."); 1052 break; 1053 } 1054 buffer_consume(&stdout_buffer, len); 1055 stdout_bytes += len; 1056 } 1057 1058 /* Output any buffered data for stderr. */ 1059 while (buffer_len(&stderr_buffer) > 0) { 1060 len = write(fileno(stderr), buffer_ptr(&stderr_buffer), 1061 buffer_len(&stderr_buffer)); 1062 if (len <= 0) { 1063 error("Write failed flushing stderr buffer."); 1064 break; 1065 } 1066 buffer_consume(&stderr_buffer, len); 1067 stderr_bytes += len; 1068 } 1069 1070 /* Clear and free any buffers. */ 1071 memset(buf, 0, sizeof(buf)); 1072 buffer_free(&stdin_buffer); 1073 buffer_free(&stdout_buffer); 1074 buffer_free(&stderr_buffer); 1075 1076 /* Report bytes transferred, and transfer rates. */ 1077 total_time = get_current_time() - start_time; 1078 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds", 1079 stdin_bytes, stdout_bytes, stderr_bytes, total_time); 1080 if (total_time > 0) 1081 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f", 1082 stdin_bytes / total_time, stdout_bytes / total_time, 1083 stderr_bytes / total_time); 1084 1085 /* Return the exit status of the program. */ 1086 debug("Exit status %d", exit_status); 1087 return exit_status; 1088 } 1089 1090 /*********/ 1091 1092 static void 1093 client_input_stdout_data(int type, u_int32_t seq, void *ctxt) 1094 { 1095 u_int data_len; 1096 char *data = packet_get_string(&data_len); 1097 packet_check_eom(); 1098 buffer_append(&stdout_buffer, data, data_len); 1099 memset(data, 0, data_len); 1100 xfree(data); 1101 } 1102 static void 1103 client_input_stderr_data(int type, u_int32_t seq, void *ctxt) 1104 { 1105 u_int data_len; 1106 char *data = packet_get_string(&data_len); 1107 packet_check_eom(); 1108 buffer_append(&stderr_buffer, data, data_len); 1109 memset(data, 0, data_len); 1110 xfree(data); 1111 } 1112 static void 1113 client_input_exit_status(int type, u_int32_t seq, void *ctxt) 1114 { 1115 exit_status = packet_get_int(); 1116 packet_check_eom(); 1117 /* Acknowledge the exit. */ 1118 packet_start(SSH_CMSG_EXIT_CONFIRMATION); 1119 packet_send(); 1120 /* 1121 * Must wait for packet to be sent since we are 1122 * exiting the loop. 1123 */ 1124 packet_write_wait(); 1125 /* Flag that we want to exit. */ 1126 quit_pending = 1; 1127 } 1128 static void 1129 client_input_agent_open(int type, u_int32_t seq, void *ctxt) 1130 { 1131 Channel *c = NULL; 1132 int remote_id, sock; 1133 1134 /* Read the remote channel number from the message. */ 1135 remote_id = packet_get_int(); 1136 packet_check_eom(); 1137 1138 /* 1139 * Get a connection to the local authentication agent (this may again 1140 * get forwarded). 1141 */ 1142 sock = ssh_get_authentication_socket(); 1143 1144 /* 1145 * If we could not connect the agent, send an error message back to 1146 * the server. This should never happen unless the agent dies, 1147 * because authentication forwarding is only enabled if we have an 1148 * agent. 1149 */ 1150 if (sock >= 0) { 1151 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, 1152 -1, 0, 0, 0, "authentication agent connection", 1); 1153 c->remote_id = remote_id; 1154 c->force_drain = 1; 1155 } 1156 if (c == NULL) { 1157 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); 1158 packet_put_int(remote_id); 1159 } else { 1160 /* Send a confirmation to the remote host. */ 1161 debug("Forwarding authentication connection."); 1162 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1163 packet_put_int(remote_id); 1164 packet_put_int(c->self); 1165 } 1166 packet_send(); 1167 } 1168 1169 static Channel * 1170 client_request_forwarded_tcpip(const char *request_type, int rchan) 1171 { 1172 Channel *c = NULL; 1173 char *listen_address, *originator_address; 1174 int listen_port, originator_port; 1175 int sock; 1176 1177 /* Get rest of the packet */ 1178 listen_address = packet_get_string(NULL); 1179 listen_port = packet_get_int(); 1180 originator_address = packet_get_string(NULL); 1181 originator_port = packet_get_int(); 1182 packet_check_eom(); 1183 1184 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d", 1185 listen_address, listen_port, originator_address, originator_port); 1186 1187 sock = channel_connect_by_listen_address(listen_port); 1188 if (sock < 0) { 1189 xfree(originator_address); 1190 xfree(listen_address); 1191 return NULL; 1192 } 1193 c = channel_new("forwarded-tcpip", 1194 SSH_CHANNEL_CONNECTING, sock, sock, -1, 1195 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, 1196 originator_address, 1); 1197 xfree(originator_address); 1198 xfree(listen_address); 1199 return c; 1200 } 1201 1202 static Channel * 1203 client_request_x11(const char *request_type, int rchan) 1204 { 1205 Channel *c = NULL; 1206 char *originator; 1207 int originator_port; 1208 int sock; 1209 1210 if (!options.forward_x11) { 1211 error("Warning: ssh server tried X11 forwarding."); 1212 error("Warning: this is probably a break in attempt by a malicious server."); 1213 return NULL; 1214 } 1215 originator = packet_get_string(NULL); 1216 if (datafellows & SSH_BUG_X11FWD) { 1217 debug2("buggy server: x11 request w/o originator_port"); 1218 originator_port = 0; 1219 } else { 1220 originator_port = packet_get_int(); 1221 } 1222 packet_check_eom(); 1223 /* XXX check permission */ 1224 debug("client_request_x11: request from %s %d", originator, 1225 originator_port); 1226 xfree(originator); 1227 sock = x11_connect_display(); 1228 if (sock < 0) 1229 return NULL; 1230 c = channel_new("x11", 1231 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 1232 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1); 1233 c->force_drain = 1; 1234 return c; 1235 } 1236 1237 static Channel * 1238 client_request_agent(const char *request_type, int rchan) 1239 { 1240 Channel *c = NULL; 1241 int sock; 1242 1243 if (!options.forward_agent) { 1244 error("Warning: ssh server tried agent forwarding."); 1245 error("Warning: this is probably a break in attempt by a malicious server."); 1246 return NULL; 1247 } 1248 sock = ssh_get_authentication_socket(); 1249 if (sock < 0) 1250 return NULL; 1251 c = channel_new("authentication agent connection", 1252 SSH_CHANNEL_OPEN, sock, sock, -1, 1253 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, 1254 "authentication agent connection", 1); 1255 c->force_drain = 1; 1256 return c; 1257 } 1258 1259 /* XXXX move to generic input handler */ 1260 static void 1261 client_input_channel_open(int type, u_int32_t seq, void *ctxt) 1262 { 1263 Channel *c = NULL; 1264 char *ctype; 1265 int rchan; 1266 u_int rmaxpack, rwindow, len; 1267 1268 ctype = packet_get_string(&len); 1269 rchan = packet_get_int(); 1270 rwindow = packet_get_int(); 1271 rmaxpack = packet_get_int(); 1272 1273 debug("client_input_channel_open: ctype %s rchan %d win %d max %d", 1274 ctype, rchan, rwindow, rmaxpack); 1275 1276 if (strcmp(ctype, "forwarded-tcpip") == 0) { 1277 c = client_request_forwarded_tcpip(ctype, rchan); 1278 } else if (strcmp(ctype, "x11") == 0) { 1279 c = client_request_x11(ctype, rchan); 1280 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { 1281 c = client_request_agent(ctype, rchan); 1282 } 1283 /* XXX duplicate : */ 1284 if (c != NULL) { 1285 debug("confirm %s", ctype); 1286 c->remote_id = rchan; 1287 c->remote_window = rwindow; 1288 c->remote_maxpacket = rmaxpack; 1289 if (c->type != SSH_CHANNEL_CONNECTING) { 1290 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); 1291 packet_put_int(c->remote_id); 1292 packet_put_int(c->self); 1293 packet_put_int(c->local_window); 1294 packet_put_int(c->local_maxpacket); 1295 packet_send(); 1296 } 1297 } else { 1298 debug("failure %s", ctype); 1299 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); 1300 packet_put_int(rchan); 1301 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); 1302 if (!(datafellows & SSH_BUG_OPENFAILURE)) { 1303 packet_put_cstring("open failed"); 1304 packet_put_cstring(""); 1305 } 1306 packet_send(); 1307 } 1308 xfree(ctype); 1309 } 1310 static void 1311 client_input_channel_req(int type, u_int32_t seq, void *ctxt) 1312 { 1313 Channel *c = NULL; 1314 int id, reply, success = 0; 1315 char *rtype; 1316 1317 id = packet_get_int(); 1318 rtype = packet_get_string(NULL); 1319 reply = packet_get_char(); 1320 1321 debug("client_input_channel_req: channel %d rtype %s reply %d", 1322 id, rtype, reply); 1323 1324 if (session_ident == -1) { 1325 error("client_input_channel_req: no channel %d", session_ident); 1326 } else if (id != session_ident) { 1327 error("client_input_channel_req: channel %d: wrong channel: %d", 1328 session_ident, id); 1329 } 1330 c = channel_lookup(id); 1331 if (c == NULL) { 1332 error("client_input_channel_req: channel %d: unknown channel", id); 1333 } else if (strcmp(rtype, "exit-status") == 0) { 1334 success = 1; 1335 exit_status = packet_get_int(); 1336 packet_check_eom(); 1337 } 1338 if (reply) { 1339 packet_start(success ? 1340 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); 1341 packet_put_int(c->remote_id); 1342 packet_send(); 1343 } 1344 xfree(rtype); 1345 } 1346 static void 1347 client_input_global_request(int type, u_int32_t seq, void *ctxt) 1348 { 1349 char *rtype; 1350 int want_reply; 1351 int success = 0; 1352 1353 rtype = packet_get_string(NULL); 1354 want_reply = packet_get_char(); 1355 debug("client_input_global_request: rtype %s want_reply %d", rtype, want_reply); 1356 if (want_reply) { 1357 packet_start(success ? 1358 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); 1359 packet_send(); 1360 packet_write_wait(); 1361 } 1362 xfree(rtype); 1363 } 1364 1365 static void 1366 client_init_dispatch_20(void) 1367 { 1368 dispatch_init(&dispatch_protocol_error); 1369 1370 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 1371 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); 1372 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 1373 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 1374 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); 1375 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 1376 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 1377 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req); 1378 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 1379 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request); 1380 1381 /* rekeying */ 1382 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 1383 1384 /* global request reply messages */ 1385 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply); 1386 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply); 1387 } 1388 static void 1389 client_init_dispatch_13(void) 1390 { 1391 dispatch_init(NULL); 1392 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); 1393 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); 1394 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); 1395 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 1396 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 1397 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); 1398 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status); 1399 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data); 1400 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data); 1401 1402 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ? 1403 &client_input_agent_open : &deny_input_open); 1404 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ? 1405 &x11_input_open : &deny_input_open); 1406 } 1407 static void 1408 client_init_dispatch_15(void) 1409 { 1410 client_init_dispatch_13(); 1411 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); 1412 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose); 1413 } 1414 static void 1415 client_init_dispatch(void) 1416 { 1417 if (compat20) 1418 client_init_dispatch_20(); 1419 else if (compat13) 1420 client_init_dispatch_13(); 1421 else 1422 client_init_dispatch_15(); 1423 } 1424 1425 /* client specific fatal cleanup */ 1426 void 1427 cleanup_exit(int i) 1428 { 1429 leave_raw_mode(); 1430 leave_non_blocking(); 1431 _exit(i); 1432 } 1433