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