Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * clientloop.c |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
| 10 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 11 | * Created: Sat Sep 23 12:23:57 1995 ylo |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 12 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 13 | * The main loop for the interactive session (client side). |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 14 | * |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 15 | * SSH2 support added by Markus Friedl. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 16 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 17 | |
| 18 | #include "includes.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 19 | RCSID("$OpenBSD: clientloop.c,v 1.29 2000/07/16 08:27:21 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | |
| 21 | #include "xmalloc.h" |
| 22 | #include "ssh.h" |
| 23 | #include "packet.h" |
| 24 | #include "buffer.h" |
Damien Miller | 5ce662a | 1999-11-11 17:57:39 +1100 | [diff] [blame] | 25 | #include "readconf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 26 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 27 | #include "ssh2.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 28 | #include "compat.h" |
| 29 | #include "channels.h" |
| 30 | #include "dispatch.h" |
| 31 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | /* Flag indicating that stdin should be redirected from /dev/null. */ |
| 33 | extern int stdin_null_flag; |
| 34 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 35 | /* |
| 36 | * Name of the host we are connecting to. This is the name given on the |
| 37 | * command line, or the HostName specified for the user-supplied name in a |
| 38 | * configuration file. |
| 39 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | extern char *host; |
| 41 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 42 | /* |
| 43 | * Flag to indicate that we have received a window change signal which has |
| 44 | * not yet been processed. This will cause a message indicating the new |
| 45 | * window size to be sent to the server a little later. This is volatile |
| 46 | * because this is updated in a signal handler. |
| 47 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 48 | static volatile int received_window_change_signal = 0; |
| 49 | |
| 50 | /* Terminal modes, as saved by enter_raw_mode. */ |
| 51 | static struct termios saved_tio; |
| 52 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 53 | /* |
| 54 | * Flag indicating whether we are in raw mode. This is used by |
| 55 | * enter_raw_mode and leave_raw_mode. |
| 56 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | static int in_raw_mode = 0; |
| 58 | |
| 59 | /* Flag indicating whether the user\'s terminal is in non-blocking mode. */ |
| 60 | static int in_non_blocking_mode = 0; |
| 61 | |
| 62 | /* Common data for the client loop code. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 63 | static int escape_pending; /* Last character was the escape character */ |
| 64 | static int last_was_cr; /* Last character was a newline. */ |
| 65 | static int exit_status; /* Used to store the exit status of the command. */ |
| 66 | static int stdin_eof; /* EOF has been encountered on standard error. */ |
| 67 | static Buffer stdin_buffer; /* Buffer for stdin data. */ |
| 68 | static Buffer stdout_buffer; /* Buffer for stdout data. */ |
| 69 | static Buffer stderr_buffer; /* Buffer for stderr data. */ |
| 70 | static unsigned int buffer_high;/* Soft max buffer size. */ |
| 71 | static int max_fd; /* Maximum file descriptor number in select(). */ |
| 72 | static int connection_in; /* Connection to server (input). */ |
| 73 | static int connection_out; /* Connection to server (output). */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | static unsigned long stdin_bytes, stdout_bytes, stderr_bytes; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 75 | static int quit_pending; /* Set to non-zero to quit the client loop. */ |
| 76 | static int escape_char; /* Escape character. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 77 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 78 | |
| 79 | void client_init_dispatch(void); |
| 80 | int session_ident = -1; |
| 81 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 82 | /* Returns the user\'s terminal to normal mode if it had been put in raw mode. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 84 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | leave_raw_mode() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 87 | if (!in_raw_mode) |
| 88 | return; |
| 89 | in_raw_mode = 0; |
| 90 | if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0) |
| 91 | perror("tcsetattr"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 93 | fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Puts the user\'s terminal in raw mode. */ |
| 97 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 98 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 99 | enter_raw_mode() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 101 | struct termios tio; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 102 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 103 | if (tcgetattr(fileno(stdin), &tio) < 0) |
| 104 | perror("tcgetattr"); |
| 105 | saved_tio = tio; |
| 106 | tio.c_iflag |= IGNPAR; |
| 107 | tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF); |
| 108 | tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 109 | #ifdef IEXTEN |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | tio.c_lflag &= ~IEXTEN; |
| 111 | #endif /* IEXTEN */ |
| 112 | tio.c_oflag &= ~OPOST; |
| 113 | tio.c_cc[VMIN] = 1; |
| 114 | tio.c_cc[VTIME] = 0; |
| 115 | if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0) |
| 116 | perror("tcsetattr"); |
| 117 | in_raw_mode = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 119 | fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL); |
| 120 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 121 | |
| 122 | /* Restores stdin to blocking mode. */ |
| 123 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 124 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 125 | leave_non_blocking() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 126 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | if (in_non_blocking_mode) { |
| 128 | (void) fcntl(fileno(stdin), F_SETFL, 0); |
| 129 | in_non_blocking_mode = 0; |
| 130 | fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL); |
| 131 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | } |
| 133 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 134 | /* Puts stdin terminal in non-blocking mode. */ |
| 135 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 136 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | enter_non_blocking() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 138 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 139 | in_non_blocking_mode = 1; |
| 140 | (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK); |
| 141 | fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 142 | } |
| 143 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 144 | /* |
| 145 | * Signal handler for the window change signal (SIGWINCH). This just sets a |
| 146 | * flag indicating that the window has changed. |
| 147 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 148 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 149 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | window_change_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | received_window_change_signal = 1; |
| 153 | signal(SIGWINCH, window_change_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 154 | } |
| 155 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 156 | /* |
| 157 | * Signal handler for signals that cause the program to terminate. These |
| 158 | * signals must be trapped to restore terminal modes. |
| 159 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 160 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 161 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | signal_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 163 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 164 | if (in_raw_mode) |
| 165 | leave_raw_mode(); |
| 166 | if (in_non_blocking_mode) |
| 167 | leave_non_blocking(); |
| 168 | channel_stop_listening(); |
| 169 | packet_close(); |
| 170 | fatal("Killed by signal %d.", sig); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | } |
| 172 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 173 | /* |
| 174 | * Returns current time in seconds from Jan 1, 1970 with the maximum |
| 175 | * available resolution. |
| 176 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 177 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 178 | double |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | get_current_time() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 180 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 181 | struct timeval tv; |
| 182 | gettimeofday(&tv, NULL); |
| 183 | return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | } |
| 185 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 186 | /* |
| 187 | * This is called when the interactive is entered. This checks if there is |
| 188 | * an EOF coming on stdin. We must check this explicitly, as select() does |
| 189 | * not appear to wake up when redirecting from /dev/null. |
| 190 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 191 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 192 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 193 | client_check_initial_eof_on_stdin() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 195 | int len; |
| 196 | char buf[1]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 197 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 198 | /* |
| 199 | * If standard input is to be "redirected from /dev/null", we simply |
| 200 | * mark that we have seen an EOF and send an EOF message to the |
| 201 | * server. Otherwise, we try to read a single character; it appears |
| 202 | * that for some files, such /dev/null, select() never wakes up for |
| 203 | * read for this descriptor, which means that we never get EOF. This |
| 204 | * way we will get the EOF if stdin comes from /dev/null or similar. |
| 205 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 206 | if (stdin_null_flag) { |
| 207 | /* Fake EOF on stdin. */ |
| 208 | debug("Sending eof."); |
| 209 | stdin_eof = 1; |
| 210 | packet_start(SSH_CMSG_EOF); |
| 211 | packet_send(); |
| 212 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 213 | enter_non_blocking(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 214 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 215 | /* Check for immediate EOF on stdin. */ |
| 216 | len = read(fileno(stdin), buf, 1); |
| 217 | if (len == 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 218 | /* EOF. Record that we have seen it and send EOF to server. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 219 | debug("Sending eof."); |
| 220 | stdin_eof = 1; |
| 221 | packet_start(SSH_CMSG_EOF); |
| 222 | packet_send(); |
| 223 | } else if (len > 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 224 | /* |
| 225 | * Got data. We must store the data in the buffer, |
| 226 | * and also process it as an escape character if |
| 227 | * appropriate. |
| 228 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | if ((unsigned char) buf[0] == escape_char) |
| 230 | escape_pending = 1; |
| 231 | else { |
| 232 | buffer_append(&stdin_buffer, buf, 1); |
| 233 | stdin_bytes += 1; |
| 234 | } |
| 235 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 236 | leave_non_blocking(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 237 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 238 | } |
| 239 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 240 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 241 | /* |
| 242 | * Make packets from buffered stdin data, and buffer them for sending to the |
| 243 | * connection. |
| 244 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 245 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 246 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 247 | client_make_packets_from_stdin_data() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 248 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 249 | unsigned int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 250 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 251 | /* Send buffered stdin data to the server. */ |
| 252 | while (buffer_len(&stdin_buffer) > 0 && |
| 253 | packet_not_very_much_data_to_write()) { |
| 254 | len = buffer_len(&stdin_buffer); |
| 255 | /* Keep the packets at reasonable size. */ |
| 256 | if (len > packet_get_maxsize()) |
| 257 | len = packet_get_maxsize(); |
| 258 | packet_start(SSH_CMSG_STDIN_DATA); |
| 259 | packet_put_string(buffer_ptr(&stdin_buffer), len); |
| 260 | packet_send(); |
| 261 | buffer_consume(&stdin_buffer, len); |
| 262 | /* If we have a pending EOF, send it now. */ |
| 263 | if (stdin_eof && buffer_len(&stdin_buffer) == 0) { |
| 264 | packet_start(SSH_CMSG_EOF); |
| 265 | packet_send(); |
| 266 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 267 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 268 | } |
| 269 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 270 | /* |
| 271 | * Checks if the client window has changed, and sends a packet about it to |
| 272 | * the server if so. The actual change is detected elsewhere (by a software |
| 273 | * interrupt on Unix); this just checks the flag and sends a message if |
| 274 | * appropriate. |
| 275 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 276 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 277 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 278 | client_check_window_change() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 279 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 280 | struct winsize ws; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 281 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 282 | if (! received_window_change_signal) |
| 283 | return; |
| 284 | /** XXX race */ |
| 285 | received_window_change_signal = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 286 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 287 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) |
| 288 | return; |
| 289 | |
| 290 | debug("client_check_window_change: changed"); |
| 291 | |
| 292 | if (compat20) { |
| 293 | channel_request_start(session_ident, "window-change", 0); |
| 294 | packet_put_int(ws.ws_col); |
| 295 | packet_put_int(ws.ws_row); |
| 296 | packet_put_int(ws.ws_xpixel); |
| 297 | packet_put_int(ws.ws_ypixel); |
| 298 | packet_send(); |
| 299 | } else { |
| 300 | packet_start(SSH_CMSG_WINDOW_SIZE); |
| 301 | packet_put_int(ws.ws_row); |
| 302 | packet_put_int(ws.ws_col); |
| 303 | packet_put_int(ws.ws_xpixel); |
| 304 | packet_put_int(ws.ws_ypixel); |
| 305 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 306 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 307 | } |
| 308 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 309 | /* |
| 310 | * Waits until the client can do something (some data becomes available on |
| 311 | * one of the file descriptors). |
| 312 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 313 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 314 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 315 | client_wait_until_can_do_something(fd_set * readset, fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 316 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 317 | /*debug("client_wait_until_can_do_something"); */ |
| 318 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 319 | /* Initialize select masks. */ |
| 320 | FD_ZERO(readset); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 321 | FD_ZERO(writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 322 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 323 | if (!compat20) { |
| 324 | /* Read from the connection, unless our buffers are full. */ |
| 325 | if (buffer_len(&stdout_buffer) < buffer_high && |
| 326 | buffer_len(&stderr_buffer) < buffer_high && |
| 327 | channel_not_very_much_buffered_data()) |
| 328 | FD_SET(connection_in, readset); |
| 329 | /* |
| 330 | * Read from stdin, unless we have seen EOF or have very much |
| 331 | * buffered data to send to the server. |
| 332 | */ |
| 333 | if (!stdin_eof && packet_not_very_much_data_to_write()) |
| 334 | FD_SET(fileno(stdin), readset); |
| 335 | |
| 336 | /* Select stdout/stderr if have data in buffer. */ |
| 337 | if (buffer_len(&stdout_buffer) > 0) |
| 338 | FD_SET(fileno(stdout), writeset); |
| 339 | if (buffer_len(&stderr_buffer) > 0) |
| 340 | FD_SET(fileno(stderr), writeset); |
| 341 | } else { |
| 342 | FD_SET(connection_in, readset); |
| 343 | } |
| 344 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 345 | /* Add any selections by the channel mechanism. */ |
| 346 | channel_prepare_select(readset, writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 347 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 348 | /* Select server connection if have data to write to the server. */ |
| 349 | if (packet_have_data_to_write()) |
| 350 | FD_SET(connection_out, writeset); |
| 351 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 352 | /* move UP XXX */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 353 | /* Update maximum file descriptor number, if appropriate. */ |
| 354 | if (channel_max_fd() > max_fd) |
| 355 | max_fd = channel_max_fd(); |
| 356 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 357 | /* |
| 358 | * Wait for something to happen. This will suspend the process until |
| 359 | * some selected descriptor can be read, written, or has some other |
| 360 | * event pending. Note: if you want to implement SSH_MSG_IGNORE |
| 361 | * messages to fool traffic analysis, this might be the place to do |
| 362 | * it: just have a random timeout for the select, and send a random |
| 363 | * SSH_MSG_IGNORE packet when the timeout expires. |
| 364 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 365 | |
| 366 | if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) { |
| 367 | char buf[100]; |
| 368 | /* Some systems fail to clear these automatically. */ |
| 369 | FD_ZERO(readset); |
| 370 | FD_ZERO(writeset); |
| 371 | if (errno == EINTR) |
| 372 | return; |
| 373 | /* Note: we might still have data in the buffers. */ |
| 374 | snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno)); |
| 375 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 376 | stderr_bytes += strlen(buf); |
| 377 | quit_pending = 1; |
| 378 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 379 | } |
| 380 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 381 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 382 | client_suspend_self() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 383 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 384 | struct winsize oldws, newws; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 385 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 386 | /* Flush stdout and stderr buffers. */ |
| 387 | if (buffer_len(&stdout_buffer) > 0) |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 388 | atomicio(write, fileno(stdout), buffer_ptr(&stdout_buffer), |
| 389 | buffer_len(&stdout_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 390 | if (buffer_len(&stderr_buffer) > 0) |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 391 | atomicio(write, fileno(stderr), buffer_ptr(&stderr_buffer), |
| 392 | buffer_len(&stderr_buffer)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 393 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 394 | leave_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 395 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 396 | /* |
| 397 | * Free (and clear) the buffer to reduce the amount of data that gets |
| 398 | * written to swap. |
| 399 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 400 | buffer_free(&stdin_buffer); |
| 401 | buffer_free(&stdout_buffer); |
| 402 | buffer_free(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 403 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 404 | /* Save old window size. */ |
| 405 | ioctl(fileno(stdin), TIOCGWINSZ, &oldws); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 406 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 407 | /* Send the suspend signal to the program itself. */ |
| 408 | kill(getpid(), SIGTSTP); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 409 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 410 | /* Check if the window size has changed. */ |
| 411 | if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 && |
| 412 | (oldws.ws_row != newws.ws_row || |
| 413 | oldws.ws_col != newws.ws_col || |
| 414 | oldws.ws_xpixel != newws.ws_xpixel || |
| 415 | oldws.ws_ypixel != newws.ws_ypixel)) |
| 416 | received_window_change_signal = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 417 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 418 | /* OK, we have been continued by the user. Reinitialize buffers. */ |
| 419 | buffer_init(&stdin_buffer); |
| 420 | buffer_init(&stdout_buffer); |
| 421 | buffer_init(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 422 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 423 | enter_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 424 | } |
| 425 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 426 | void |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 427 | client_process_net_input(fd_set * readset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 428 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 429 | int len; |
| 430 | char buf[8192]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 431 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 432 | /* |
| 433 | * Read input from the server, and add any such data to the buffer of |
| 434 | * the packet subsystem. |
| 435 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 436 | if (FD_ISSET(connection_in, readset)) { |
| 437 | /* Read as much as possible. */ |
| 438 | len = read(connection_in, buf, sizeof(buf)); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 439 | /*debug("read connection_in len %d", len); XXX */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 440 | if (len == 0) { |
| 441 | /* Received EOF. The remote host has closed the connection. */ |
| 442 | snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n", |
| 443 | host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 444 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 445 | stderr_bytes += strlen(buf); |
| 446 | quit_pending = 1; |
| 447 | return; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 448 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 449 | /* |
| 450 | * There is a kernel bug on Solaris that causes select to |
| 451 | * sometimes wake up even though there is no data available. |
| 452 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 453 | if (len < 0 && errno == EAGAIN) |
| 454 | len = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 455 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 456 | if (len < 0) { |
| 457 | /* An error has encountered. Perhaps there is a network problem. */ |
| 458 | snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n", |
| 459 | host, strerror(errno)); |
| 460 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 461 | stderr_bytes += strlen(buf); |
| 462 | quit_pending = 1; |
| 463 | return; |
| 464 | } |
| 465 | packet_process_incoming(buf, len); |
| 466 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 467 | } |
| 468 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 469 | void |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 470 | client_process_input(fd_set * readset) |
| 471 | { |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 472 | int len; |
| 473 | pid_t pid; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 474 | char buf[8192], *s; |
| 475 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 476 | /* Read input from stdin. */ |
| 477 | if (FD_ISSET(fileno(stdin), readset)) { |
| 478 | /* Read as much as possible. */ |
| 479 | len = read(fileno(stdin), buf, sizeof(buf)); |
| 480 | if (len <= 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 481 | /* |
| 482 | * Received EOF or error. They are treated |
| 483 | * similarly, except that an error message is printed |
| 484 | * if it was an error condition. |
| 485 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 486 | if (len < 0) { |
| 487 | snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno)); |
| 488 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 489 | stderr_bytes += strlen(buf); |
| 490 | } |
| 491 | /* Mark that we have seen EOF. */ |
| 492 | stdin_eof = 1; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 493 | /* |
| 494 | * Send an EOF message to the server unless there is |
| 495 | * data in the buffer. If there is data in the |
| 496 | * buffer, no message will be sent now. Code |
| 497 | * elsewhere will send the EOF when the buffer |
| 498 | * becomes empty if stdin_eof is set. |
| 499 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 500 | if (buffer_len(&stdin_buffer) == 0) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 501 | packet_start(SSH_CMSG_EOF); |
| 502 | packet_send(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 503 | } |
| 504 | } else if (escape_char == -1) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 505 | /* |
| 506 | * Normal successful read, and no escape character. |
| 507 | * Just append the data to buffer. |
| 508 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 509 | buffer_append(&stdin_buffer, buf, len); |
| 510 | stdin_bytes += len; |
| 511 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 512 | /* |
| 513 | * Normal, successful read. But we have an escape character |
| 514 | * and have to process the characters one by one. |
| 515 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 516 | unsigned int i; |
| 517 | for (i = 0; i < len; i++) { |
| 518 | unsigned char ch; |
| 519 | /* Get one character at a time. */ |
| 520 | ch = buf[i]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 521 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 522 | if (escape_pending) { |
| 523 | /* We have previously seen an escape character. */ |
| 524 | /* Clear the flag now. */ |
| 525 | escape_pending = 0; |
| 526 | /* Process the escaped character. */ |
| 527 | switch (ch) { |
| 528 | case '.': |
| 529 | /* Terminate the connection. */ |
| 530 | snprintf(buf, sizeof buf, "%c.\r\n", escape_char); |
| 531 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 532 | stderr_bytes += strlen(buf); |
| 533 | quit_pending = 1; |
| 534 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 535 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 536 | case 'Z' - 64: |
| 537 | /* Suspend the program. */ |
| 538 | /* Print a message to that effect to the user. */ |
| 539 | snprintf(buf, sizeof buf, "%c^Z\r\n", escape_char); |
| 540 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 541 | stderr_bytes += strlen(buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 542 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 543 | /* Restore terminal modes and suspend. */ |
| 544 | client_suspend_self(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 545 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 546 | /* We have been continued. */ |
| 547 | continue; |
| 548 | |
| 549 | case '&': |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 550 | /* |
| 551 | * Detach the program (continue to serve connections, |
| 552 | * but put in background and no more new connections). |
| 553 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 554 | if (!stdin_eof) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 555 | /* |
| 556 | * Sending SSH_CMSG_EOF alone does not always appear |
| 557 | * to be enough. So we try to send an EOF character |
| 558 | * first. |
| 559 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 560 | packet_start(SSH_CMSG_STDIN_DATA); |
| 561 | packet_put_string("\004", 1); |
| 562 | packet_send(); |
| 563 | /* Close stdin. */ |
| 564 | stdin_eof = 1; |
| 565 | if (buffer_len(&stdin_buffer) == 0) { |
| 566 | packet_start(SSH_CMSG_EOF); |
| 567 | packet_send(); |
| 568 | } |
| 569 | } |
| 570 | /* Restore tty modes. */ |
| 571 | leave_raw_mode(); |
| 572 | |
| 573 | /* Stop listening for new connections. */ |
| 574 | channel_stop_listening(); |
| 575 | |
| 576 | printf("%c& [backgrounded]\n", escape_char); |
| 577 | |
| 578 | /* Fork into background. */ |
| 579 | pid = fork(); |
| 580 | if (pid < 0) { |
| 581 | error("fork: %.100s", strerror(errno)); |
| 582 | continue; |
| 583 | } |
| 584 | if (pid != 0) { /* This is the parent. */ |
| 585 | /* The parent just exits. */ |
| 586 | exit(0); |
| 587 | } |
| 588 | /* The child continues serving connections. */ |
| 589 | continue; |
| 590 | |
| 591 | case '?': |
| 592 | snprintf(buf, sizeof buf, |
| 593 | "%c?\r\n\ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 594 | Supported escape sequences:\r\n\ |
| 595 | ~. - terminate connection\r\n\ |
| 596 | ~^Z - suspend ssh\r\n\ |
| 597 | ~# - list forwarded connections\r\n\ |
| 598 | ~& - background ssh (when waiting for connections to terminate)\r\n\ |
| 599 | ~? - this message\r\n\ |
| 600 | ~~ - send the escape character by typing it twice\r\n\ |
| 601 | (Note that escapes are only recognized immediately after newline.)\r\n", |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 602 | escape_char); |
| 603 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 604 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 605 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 606 | case '#': |
| 607 | snprintf(buf, sizeof buf, "%c#\r\n", escape_char); |
| 608 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 609 | s = channel_open_message(); |
| 610 | buffer_append(&stderr_buffer, s, strlen(s)); |
| 611 | xfree(s); |
| 612 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 613 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 614 | default: |
| 615 | if (ch != escape_char) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 616 | /* |
| 617 | * Escape character followed by non-special character. |
| 618 | * Append both to the input buffer. |
| 619 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 620 | buf[0] = escape_char; |
| 621 | buf[1] = ch; |
| 622 | buffer_append(&stdin_buffer, buf, 2); |
| 623 | stdin_bytes += 2; |
| 624 | continue; |
| 625 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 626 | /* |
| 627 | * Note that escape character typed twice |
| 628 | * falls through here; the latter gets processed |
| 629 | * as a normal character below. |
| 630 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 631 | break; |
| 632 | } |
| 633 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 634 | /* |
| 635 | * The previous character was not an escape char. Check if this |
| 636 | * is an escape. |
| 637 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 638 | if (last_was_cr && ch == escape_char) { |
| 639 | /* It is. Set the flag and continue to next character. */ |
| 640 | escape_pending = 1; |
| 641 | continue; |
| 642 | } |
| 643 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 644 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 645 | /* |
| 646 | * Normal character. Record whether it was a newline, |
| 647 | * and append it to the buffer. |
| 648 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 649 | last_was_cr = (ch == '\r' || ch == '\n'); |
| 650 | buf[0] = ch; |
| 651 | buffer_append(&stdin_buffer, buf, 1); |
| 652 | stdin_bytes += 1; |
| 653 | continue; |
| 654 | } |
| 655 | } |
| 656 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 657 | } |
| 658 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 659 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 660 | client_process_output(fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 661 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 662 | int len; |
| 663 | char buf[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 664 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 665 | /* Write buffered output to stdout. */ |
| 666 | if (FD_ISSET(fileno(stdout), writeset)) { |
| 667 | /* Write as much data as possible. */ |
| 668 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 669 | buffer_len(&stdout_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 670 | if (len <= 0) { |
| 671 | if (errno == EAGAIN) |
| 672 | len = 0; |
| 673 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 674 | /* |
| 675 | * An error or EOF was encountered. Put an |
| 676 | * error message to stderr buffer. |
| 677 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 678 | snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno)); |
| 679 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 680 | stderr_bytes += strlen(buf); |
| 681 | quit_pending = 1; |
| 682 | return; |
| 683 | } |
| 684 | } |
| 685 | /* Consume printed data from the buffer. */ |
| 686 | buffer_consume(&stdout_buffer, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 687 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 688 | /* Write buffered output to stderr. */ |
| 689 | if (FD_ISSET(fileno(stderr), writeset)) { |
| 690 | /* Write as much data as possible. */ |
| 691 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 692 | buffer_len(&stderr_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 693 | if (len <= 0) { |
| 694 | if (errno == EAGAIN) |
| 695 | len = 0; |
| 696 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 697 | /* EOF or error, but can't even print error message. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 698 | quit_pending = 1; |
| 699 | return; |
| 700 | } |
| 701 | } |
| 702 | /* Consume printed characters from the buffer. */ |
| 703 | buffer_consume(&stderr_buffer, len); |
| 704 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 705 | } |
| 706 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 707 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 708 | * Get packets from the connection input buffer, and process them as long as |
| 709 | * there are packets available. |
| 710 | * |
| 711 | * Any unknown packets received during the actual |
| 712 | * session cause the session to terminate. This is |
| 713 | * intended to make debugging easier since no |
| 714 | * confirmations are sent. Any compatible protocol |
| 715 | * extensions must be negotiated during the |
| 716 | * preparatory phase. |
| 717 | */ |
| 718 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 719 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 720 | client_process_buffered_input_packets() |
| 721 | { |
| 722 | dispatch_run(DISPATCH_NONBLOCK, &quit_pending); |
| 723 | } |
| 724 | |
| 725 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 726 | * Implements the interactive session with the server. This is called after |
| 727 | * the user has been authenticated, and a command has been started on the |
| 728 | * remote host. If escape_char != -1, it is the character used as an escape |
| 729 | * character for terminating or suspending the session. |
| 730 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 731 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 732 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 733 | client_loop(int have_pty, int escape_char_arg) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 734 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 735 | extern Options options; |
| 736 | double start_time, total_time; |
| 737 | int len; |
| 738 | char buf[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 739 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 740 | debug("Entering interactive session."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 741 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 742 | start_time = get_current_time(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 743 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 744 | /* Initialize variables. */ |
| 745 | escape_pending = 0; |
| 746 | last_was_cr = 1; |
| 747 | exit_status = -1; |
| 748 | stdin_eof = 0; |
| 749 | buffer_high = 64 * 1024; |
| 750 | connection_in = packet_get_connection_in(); |
| 751 | connection_out = packet_get_connection_out(); |
| 752 | max_fd = connection_in; |
| 753 | if (connection_out > max_fd) |
| 754 | max_fd = connection_out; |
| 755 | stdin_bytes = 0; |
| 756 | stdout_bytes = 0; |
| 757 | stderr_bytes = 0; |
| 758 | quit_pending = 0; |
| 759 | escape_char = escape_char_arg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 760 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 761 | /* Initialize buffers. */ |
| 762 | buffer_init(&stdin_buffer); |
| 763 | buffer_init(&stdout_buffer); |
| 764 | buffer_init(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 765 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 766 | client_init_dispatch(); |
| 767 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 768 | /* Set signal handlers to restore non-blocking mode. */ |
| 769 | signal(SIGINT, signal_handler); |
| 770 | signal(SIGQUIT, signal_handler); |
| 771 | signal(SIGTERM, signal_handler); |
| 772 | signal(SIGPIPE, SIG_IGN); |
| 773 | if (have_pty) |
| 774 | signal(SIGWINCH, window_change_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 775 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 776 | if (have_pty) |
| 777 | enter_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 778 | |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 779 | /* Check if we should immediately send eof on stdin. */ |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 780 | if (!compat20) |
| 781 | client_check_initial_eof_on_stdin(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 782 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 783 | /* Main loop of the client for the interactive session mode. */ |
| 784 | while (!quit_pending) { |
| 785 | fd_set readset, writeset; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 786 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 787 | /* Process buffered packets sent by the server. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 788 | client_process_buffered_input_packets(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 789 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 790 | if (compat20 && !channel_still_open()) { |
| 791 | debug("!channel_still_open."); |
| 792 | break; |
| 793 | } |
| 794 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 795 | /* |
| 796 | * Make packets of buffered stdin data, and buffer them for |
| 797 | * sending to the server. |
| 798 | */ |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 799 | if (!compat20) |
| 800 | client_make_packets_from_stdin_data(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 801 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 802 | /* |
| 803 | * Make packets from buffered channel data, and buffer them |
| 804 | * for sending to the server. |
| 805 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 806 | if (packet_not_very_much_data_to_write()) |
| 807 | channel_output_poll(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 808 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 809 | /* |
| 810 | * Check if the window size has changed, and buffer a message |
| 811 | * about it to the server if so. |
| 812 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 813 | client_check_window_change(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 814 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 815 | if (quit_pending) |
| 816 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 817 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 818 | /* |
| 819 | * Wait until we have something to do (something becomes |
| 820 | * available on one of the descriptors). |
| 821 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 822 | client_wait_until_can_do_something(&readset, &writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 823 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 824 | if (quit_pending) |
| 825 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 826 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 827 | /* Do channel operations. */ |
| 828 | channel_after_select(&readset, &writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 829 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 830 | /* Buffer input from the connection. */ |
| 831 | client_process_net_input(&readset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 832 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 833 | if (quit_pending) |
| 834 | break; |
| 835 | |
| 836 | if (!compat20) { |
| 837 | /* Buffer data from stdin */ |
| 838 | client_process_input(&readset); |
| 839 | /* |
| 840 | * Process output to stdout and stderr. Output to |
| 841 | * the connection is processed elsewhere (above). |
| 842 | */ |
| 843 | client_process_output(&writeset); |
| 844 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 845 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 846 | /* Send as much buffered packet data as possible to the sender. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 847 | if (FD_ISSET(connection_out, &writeset)) |
| 848 | packet_write_poll(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 849 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 850 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 851 | /* Terminate the session. */ |
| 852 | |
| 853 | /* Stop watching for window change. */ |
| 854 | if (have_pty) |
| 855 | signal(SIGWINCH, SIG_DFL); |
| 856 | |
| 857 | /* Stop listening for connections. */ |
| 858 | channel_stop_listening(); |
| 859 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 860 | /* |
| 861 | * In interactive mode (with pseudo tty) display a message indicating |
| 862 | * that the connection has been closed. |
| 863 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 864 | if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { |
| 865 | snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host); |
| 866 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
| 867 | stderr_bytes += strlen(buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 868 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 869 | /* Output any buffered data for stdout. */ |
| 870 | while (buffer_len(&stdout_buffer) > 0) { |
| 871 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 872 | buffer_len(&stdout_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 873 | if (len <= 0) { |
| 874 | error("Write failed flushing stdout buffer."); |
| 875 | break; |
| 876 | } |
| 877 | buffer_consume(&stdout_buffer, len); |
| 878 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 879 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 880 | /* Output any buffered data for stderr. */ |
| 881 | while (buffer_len(&stderr_buffer) > 0) { |
| 882 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 883 | buffer_len(&stderr_buffer)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 884 | if (len <= 0) { |
| 885 | error("Write failed flushing stderr buffer."); |
| 886 | break; |
| 887 | } |
| 888 | buffer_consume(&stderr_buffer, len); |
| 889 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 890 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 891 | if (have_pty) |
| 892 | leave_raw_mode(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 893 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 894 | /* Clear and free any buffers. */ |
| 895 | memset(buf, 0, sizeof(buf)); |
| 896 | buffer_free(&stdin_buffer); |
| 897 | buffer_free(&stdout_buffer); |
| 898 | buffer_free(&stderr_buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 899 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 900 | /* Report bytes transferred, and transfer rates. */ |
| 901 | total_time = get_current_time() - start_time; |
| 902 | debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds", |
| 903 | stdin_bytes, stdout_bytes, stderr_bytes, total_time); |
| 904 | if (total_time > 0) |
| 905 | debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f", |
| 906 | stdin_bytes / total_time, stdout_bytes / total_time, |
| 907 | stderr_bytes / total_time); |
| 908 | |
| 909 | /* Return the exit status of the program. */ |
| 910 | debug("Exit status %d", exit_status); |
| 911 | return exit_status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 912 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 913 | |
| 914 | /*********/ |
| 915 | |
| 916 | void |
| 917 | client_input_stdout_data(int type, int plen) |
| 918 | { |
| 919 | unsigned int data_len; |
| 920 | char *data = packet_get_string(&data_len); |
| 921 | packet_integrity_check(plen, 4 + data_len, type); |
| 922 | buffer_append(&stdout_buffer, data, data_len); |
| 923 | stdout_bytes += data_len; |
| 924 | memset(data, 0, data_len); |
| 925 | xfree(data); |
| 926 | } |
| 927 | void |
| 928 | client_input_stderr_data(int type, int plen) |
| 929 | { |
| 930 | unsigned int data_len; |
| 931 | char *data = packet_get_string(&data_len); |
| 932 | packet_integrity_check(plen, 4 + data_len, type); |
| 933 | buffer_append(&stderr_buffer, data, data_len); |
| 934 | stdout_bytes += data_len; |
| 935 | memset(data, 0, data_len); |
| 936 | xfree(data); |
| 937 | } |
| 938 | void |
| 939 | client_input_exit_status(int type, int plen) |
| 940 | { |
| 941 | packet_integrity_check(plen, 4, type); |
| 942 | exit_status = packet_get_int(); |
| 943 | /* Acknowledge the exit. */ |
| 944 | packet_start(SSH_CMSG_EXIT_CONFIRMATION); |
| 945 | packet_send(); |
| 946 | /* |
| 947 | * Must wait for packet to be sent since we are |
| 948 | * exiting the loop. |
| 949 | */ |
| 950 | packet_write_wait(); |
| 951 | /* Flag that we want to exit. */ |
| 952 | quit_pending = 1; |
| 953 | } |
| 954 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 955 | /* XXXX move to generic input handler */ |
| 956 | void |
| 957 | client_input_channel_open(int type, int plen) |
| 958 | { |
| 959 | Channel *c = NULL; |
| 960 | char *ctype; |
| 961 | int id; |
| 962 | unsigned int len; |
| 963 | int rchan; |
| 964 | int rmaxpack; |
| 965 | int rwindow; |
| 966 | |
| 967 | ctype = packet_get_string(&len); |
| 968 | rchan = packet_get_int(); |
| 969 | rwindow = packet_get_int(); |
| 970 | rmaxpack = packet_get_int(); |
| 971 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 972 | debug("client_input_channel_open: ctype %s rchan %d win %d max %d", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 973 | ctype, rchan, rwindow, rmaxpack); |
| 974 | |
| 975 | if (strcmp(ctype, "x11") == 0) { |
| 976 | int sock; |
| 977 | char *originator; |
| 978 | int originator_port; |
| 979 | originator = packet_get_string(NULL); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 980 | if (datafellows & SSH_BUG_X11FWD) { |
Damien Miller | 6d48871 | 2000-05-08 13:44:52 +1000 | [diff] [blame] | 981 | debug("buggy server: x11 request w/o originator_port"); |
| 982 | originator_port = 0; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 983 | } else { |
| 984 | originator_port = packet_get_int(); |
Damien Miller | 6d48871 | 2000-05-08 13:44:52 +1000 | [diff] [blame] | 985 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 986 | packet_done(); |
| 987 | /* XXX check permission */ |
| 988 | xfree(originator); |
| 989 | /* XXX move to channels.c */ |
| 990 | sock = x11_connect_display(); |
| 991 | if (sock >= 0) { |
| 992 | id = channel_new("x11", SSH_CHANNEL_X11_OPEN, |
| 993 | sock, sock, -1, 4*1024, 32*1024, 0, |
| 994 | xstrdup("x11")); |
| 995 | c = channel_lookup(id); |
| 996 | } |
| 997 | } |
| 998 | /* XXX duplicate : */ |
| 999 | if (c != NULL) { |
| 1000 | debug("confirm %s", ctype); |
| 1001 | c->remote_id = rchan; |
| 1002 | c->remote_window = rwindow; |
| 1003 | c->remote_maxpacket = rmaxpack; |
| 1004 | |
| 1005 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1006 | packet_put_int(c->remote_id); |
| 1007 | packet_put_int(c->self); |
| 1008 | packet_put_int(c->local_window); |
| 1009 | packet_put_int(c->local_maxpacket); |
| 1010 | packet_send(); |
| 1011 | } else { |
| 1012 | debug("failure %s", ctype); |
| 1013 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 1014 | packet_put_int(rchan); |
| 1015 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); |
| 1016 | packet_put_cstring("bla bla"); |
| 1017 | packet_put_cstring(""); |
| 1018 | packet_send(); |
| 1019 | } |
| 1020 | xfree(ctype); |
| 1021 | } |
| 1022 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1023 | void |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1024 | client_init_dispatch_20() |
| 1025 | { |
| 1026 | dispatch_init(&dispatch_protocol_error); |
| 1027 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
| 1028 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
| 1029 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
| 1030 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1031 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1032 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 1033 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 1034 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request); |
| 1035 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
| 1036 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1037 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1038 | client_init_dispatch_13() |
| 1039 | { |
| 1040 | dispatch_init(NULL); |
| 1041 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); |
| 1042 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); |
| 1043 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1044 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 1045 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 1046 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); |
| 1047 | dispatch_set(SSH_SMSG_AGENT_OPEN, &auth_input_open_request); |
| 1048 | dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status); |
| 1049 | dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data); |
| 1050 | dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data); |
| 1051 | dispatch_set(SSH_SMSG_X11_OPEN, &x11_input_open); |
| 1052 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1053 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1054 | client_init_dispatch_15() |
| 1055 | { |
| 1056 | client_init_dispatch_13(); |
| 1057 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); |
| 1058 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose); |
| 1059 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1060 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1061 | client_init_dispatch() |
| 1062 | { |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1063 | if (compat20) |
| 1064 | client_init_dispatch_20(); |
| 1065 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1066 | client_init_dispatch_13(); |
| 1067 | else |
| 1068 | client_init_dispatch_15(); |
| 1069 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1070 | |
| 1071 | void |
| 1072 | client_input_channel_req(int id, void *arg) |
| 1073 | { |
| 1074 | Channel *c = NULL; |
| 1075 | unsigned int len; |
| 1076 | int success = 0; |
| 1077 | int reply; |
| 1078 | char *rtype; |
| 1079 | |
| 1080 | rtype = packet_get_string(&len); |
| 1081 | reply = packet_get_char(); |
| 1082 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1083 | debug("client_input_channel_req: rtype %s reply %d", rtype, reply); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1084 | |
| 1085 | c = channel_lookup(id); |
| 1086 | if (c == NULL) |
| 1087 | fatal("session_input_channel_req: channel %d: bad channel", id); |
| 1088 | |
| 1089 | if (session_ident == -1) { |
| 1090 | error("client_input_channel_req: no channel %d", id); |
| 1091 | } else if (id != session_ident) { |
| 1092 | error("client_input_channel_req: bad channel %d != %d", |
| 1093 | id, session_ident); |
| 1094 | } else if (strcmp(rtype, "exit-status") == 0) { |
| 1095 | success = 1; |
| 1096 | exit_status = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1097 | packet_done(); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1098 | } |
| 1099 | if (reply) { |
| 1100 | packet_start(success ? |
| 1101 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); |
| 1102 | packet_put_int(c->remote_id); |
| 1103 | packet_send(); |
| 1104 | } |
| 1105 | xfree(rtype); |
| 1106 | } |
| 1107 | |
| 1108 | void |
| 1109 | client_set_session_ident(int id) |
| 1110 | { |
| 1111 | debug("client_set_session_ident: id %d", id); |
| 1112 | session_ident = id; |
| 1113 | channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST, |
| 1114 | client_input_channel_req, (void *)0); |
| 1115 | } |