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