]>
Commit | Line | Data |
---|---|---|
e264ac72 | 1 | /* $OpenBSD: clientloop.c,v 1.171 2006/07/25 02:59:21 stevesk Exp $ */ |
8efc0c15 | 2 | /* |
5260325f | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 | * All rights reserved | |
bcbf86ec | 6 | * The main loop for the interactive session (client side). |
6ae2364d | 7 | * |
bcbf86ec | 8 | * As far as I am concerned, the code I have written for this software |
9 | * can be used freely for any purpose. Any derived versions of this | |
10 | * software must be clearly marked as such, and if the derived work is | |
11 | * incompatible with the protocol description in the RFC file, it must be | |
12 | * called by a name other than "ssh" or "Secure Shell". | |
6ae2364d | 13 | * |
6ae2364d | 14 | * |
bcbf86ec | 15 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
16 | * | |
17 | * Redistribution and use in source and binary forms, with or without | |
18 | * modification, are permitted provided that the following conditions | |
19 | * are met: | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * 2. Redistributions in binary form must reproduce the above copyright | |
23 | * notice, this list of conditions and the following disclaimer in the | |
24 | * documentation and/or other materials provided with the distribution. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
36 | * | |
6ae2364d | 37 | * |
8ce64345 | 38 | * SSH2 support added by Markus Friedl. |
a96070d4 | 39 | * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved. |
bcbf86ec | 40 | * |
41 | * Redistribution and use in source and binary forms, with or without | |
42 | * modification, are permitted provided that the following conditions | |
43 | * are met: | |
44 | * 1. Redistributions of source code must retain the above copyright | |
45 | * notice, this list of conditions and the following disclaimer. | |
46 | * 2. Redistributions in binary form must reproduce the above copyright | |
47 | * notice, this list of conditions and the following disclaimer in the | |
48 | * documentation and/or other materials provided with the distribution. | |
49 | * | |
50 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
51 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
52 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
53 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
54 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
55 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
56 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
57 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
58 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
59 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
5260325f | 60 | */ |
8efc0c15 | 61 | |
62 | #include "includes.h" | |
bd7c69ea | 63 | |
4095f623 | 64 | #include <sys/types.h> |
e264ac72 | 65 | #include <sys/ioctl.h> |
4095f623 | 66 | #ifdef HAVE_SYS_STAT_H |
67 | # include <sys/stat.h> | |
68 | #endif | |
e264ac72 | 69 | #ifdef HAVE_SYS_TIME_H |
70 | # include <sys/time.h> | |
71 | #endif | |
5b04a8bf | 72 | #include <sys/socket.h> |
1d3e026f | 73 | |
b6438382 | 74 | #include <ctype.h> |
028094f4 | 75 | #include <errno.h> |
b5b88c19 | 76 | #ifdef HAVE_PATHS_H |
a75f5360 | 77 | #include <paths.h> |
b5b88c19 | 78 | #endif |
ada68823 | 79 | #include <signal.h> |
00146caa | 80 | #include <string.h> |
1d3e026f | 81 | #include <termios.h> |
5188ba17 | 82 | #include <unistd.h> |
8efc0c15 | 83 | |
8efc0c15 | 84 | #include "ssh.h" |
42f11eb2 | 85 | #include "ssh1.h" |
86 | #include "ssh2.h" | |
87 | #include "xmalloc.h" | |
8efc0c15 | 88 | #include "packet.h" |
89 | #include "buffer.h" | |
7368a6c8 | 90 | #include "compat.h" |
a5efa1bb | 91 | #include "channels.h" |
7368a6c8 | 92 | #include "dispatch.h" |
2e73a022 | 93 | #include "buffer.h" |
94 | #include "bufaux.h" | |
fa08c86b | 95 | #include "key.h" |
e092ce67 | 96 | #include "kex.h" |
42f11eb2 | 97 | #include "log.h" |
98 | #include "readconf.h" | |
b5c334cc | 99 | #include "clientloop.h" |
ceec33f3 | 100 | #include "sshconnect.h" |
42f11eb2 | 101 | #include "authfd.h" |
102 | #include "atomicio.h" | |
07d80252 | 103 | #include "sshpty.h" |
e968270c | 104 | #include "misc.h" |
5e96b616 | 105 | #include "monitor_fdpass.h" |
106 | #include "match.h" | |
107 | #include "msg.h" | |
a22aff1f | 108 | |
109 | /* import options */ | |
110 | extern Options options; | |
111 | ||
8efc0c15 | 112 | /* Flag indicating that stdin should be redirected from /dev/null. */ |
113 | extern int stdin_null_flag; | |
114 | ||
66357af5 | 115 | /* Flag indicating that no shell has been requested */ |
116 | extern int no_shell_flag; | |
117 | ||
5e96b616 | 118 | /* Control socket */ |
119 | extern int control_fd; | |
120 | ||
aa3378df | 121 | /* |
122 | * Name of the host we are connecting to. This is the name given on the | |
123 | * command line, or the HostName specified for the user-supplied name in a | |
124 | * configuration file. | |
125 | */ | |
8efc0c15 | 126 | extern char *host; |
127 | ||
aa3378df | 128 | /* |
129 | * Flag to indicate that we have received a window change signal which has | |
130 | * not yet been processed. This will cause a message indicating the new | |
131 | * window size to be sent to the server a little later. This is volatile | |
132 | * because this is updated in a signal handler. | |
133 | */ | |
7a934d1b | 134 | static volatile sig_atomic_t received_window_change_signal = 0; |
135 | static volatile sig_atomic_t received_signal = 0; | |
8efc0c15 | 136 | |
7b9b0103 | 137 | /* Flag indicating whether the user's terminal is in non-blocking mode. */ |
8efc0c15 | 138 | static int in_non_blocking_mode = 0; |
139 | ||
140 | /* Common data for the client loop code. */ | |
50394f26 | 141 | static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */ |
2e73a022 | 142 | static int escape_char; /* Escape character. */ |
5260325f | 143 | static int escape_pending; /* Last character was the escape character */ |
144 | static int last_was_cr; /* Last character was a newline. */ | |
145 | static int exit_status; /* Used to store the exit status of the command. */ | |
146 | static int stdin_eof; /* EOF has been encountered on standard error. */ | |
147 | static Buffer stdin_buffer; /* Buffer for stdin data. */ | |
148 | static Buffer stdout_buffer; /* Buffer for stdout data. */ | |
149 | static Buffer stderr_buffer; /* Buffer for stderr data. */ | |
1e3b8b07 | 150 | static u_long stdin_bytes, stdout_bytes, stderr_bytes; |
151 | static u_int buffer_high;/* Soft max buffer size. */ | |
5260325f | 152 | static int connection_in; /* Connection to server (input). */ |
153 | static int connection_out; /* Connection to server (output). */ | |
c422989b | 154 | static int need_rekeying; /* Set to non-zero if rekeying is requested. */ |
eb0dd41f | 155 | static int session_closed = 0; /* In SSH2: login session closed. */ |
5d8d32a3 | 156 | static int server_alive_timeouts = 0; |
8efc0c15 | 157 | |
396c147e | 158 | static void client_init_dispatch(void); |
8ce64345 | 159 | int session_ident = -1; |
160 | ||
5e96b616 | 161 | struct confirm_ctx { |
162 | int want_tty; | |
163 | int want_subsys; | |
ef07103c | 164 | int want_x_fwd; |
165 | int want_agent_fwd; | |
5e96b616 | 166 | Buffer cmd; |
167 | char *term; | |
168 | struct termios tio; | |
1786be35 | 169 | char **env; |
5e96b616 | 170 | }; |
171 | ||
e092ce67 | 172 | /*XXX*/ |
173 | extern Kex *xxx_kex; | |
174 | ||
5e96b616 | 175 | void ssh_process_session2_setup(int, int, int, Buffer *); |
176 | ||
8efc0c15 | 177 | /* Restores stdin to blocking mode. */ |
178 | ||
396c147e | 179 | static void |
5ca51e19 | 180 | leave_non_blocking(void) |
8efc0c15 | 181 | { |
5260325f | 182 | if (in_non_blocking_mode) { |
1b273ece | 183 | unset_nonblock(fileno(stdin)); |
5260325f | 184 | in_non_blocking_mode = 0; |
5260325f | 185 | } |
8efc0c15 | 186 | } |
187 | ||
5260325f | 188 | /* Puts stdin terminal in non-blocking mode. */ |
189 | ||
396c147e | 190 | static void |
5ca51e19 | 191 | enter_non_blocking(void) |
8efc0c15 | 192 | { |
5260325f | 193 | in_non_blocking_mode = 1; |
170694d7 | 194 | set_nonblock(fileno(stdin)); |
8efc0c15 | 195 | } |
196 | ||
aa3378df | 197 | /* |
198 | * Signal handler for the window change signal (SIGWINCH). This just sets a | |
199 | * flag indicating that the window has changed. | |
200 | */ | |
3efa8ea9 | 201 | /*ARGSUSED */ |
396c147e | 202 | static void |
5260325f | 203 | window_change_handler(int sig) |
8efc0c15 | 204 | { |
5260325f | 205 | received_window_change_signal = 1; |
206 | signal(SIGWINCH, window_change_handler); | |
8efc0c15 | 207 | } |
208 | ||
aa3378df | 209 | /* |
210 | * Signal handler for signals that cause the program to terminate. These | |
211 | * signals must be trapped to restore terminal modes. | |
212 | */ | |
3efa8ea9 | 213 | /*ARGSUSED */ |
396c147e | 214 | static void |
5260325f | 215 | signal_handler(int sig) |
8efc0c15 | 216 | { |
c9130033 | 217 | received_signal = sig; |
218 | quit_pending = 1; | |
8efc0c15 | 219 | } |
220 | ||
aa3378df | 221 | /* |
222 | * Returns current time in seconds from Jan 1, 1970 with the maximum | |
223 | * available resolution. | |
224 | */ | |
8efc0c15 | 225 | |
396c147e | 226 | static double |
5ca51e19 | 227 | get_current_time(void) |
8efc0c15 | 228 | { |
5260325f | 229 | struct timeval tv; |
230 | gettimeofday(&tv, NULL); | |
231 | return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0; | |
8efc0c15 | 232 | } |
233 | ||
9bf083eb | 234 | #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1" |
235 | void | |
236 | client_x11_get_proto(const char *display, const char *xauth_path, | |
237 | u_int trusted, char **_proto, char **_data) | |
238 | { | |
239 | char cmd[1024]; | |
240 | char line[512]; | |
241 | char xdisplay[512]; | |
242 | static char proto[512], data[512]; | |
243 | FILE *f; | |
244 | int got_data = 0, generated = 0, do_unlink = 0, i; | |
245 | char *xauthdir, *xauthfile; | |
246 | struct stat st; | |
247 | ||
248 | xauthdir = xauthfile = NULL; | |
249 | *_proto = proto; | |
250 | *_data = data; | |
251 | proto[0] = data[0] = '\0'; | |
252 | ||
253 | if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) { | |
254 | debug("No xauth program."); | |
255 | } else { | |
256 | if (display == NULL) { | |
257 | debug("x11_get_proto: DISPLAY not set"); | |
258 | return; | |
259 | } | |
260 | /* | |
261 | * Handle FamilyLocal case where $DISPLAY does | |
262 | * not match an authorization entry. For this we | |
263 | * just try "xauth list unix:displaynum.screennum". | |
264 | * XXX: "localhost" match to determine FamilyLocal | |
265 | * is not perfect. | |
266 | */ | |
267 | if (strncmp(display, "localhost:", 10) == 0) { | |
268 | snprintf(xdisplay, sizeof(xdisplay), "unix:%s", | |
269 | display + 10); | |
270 | display = xdisplay; | |
271 | } | |
272 | if (trusted == 0) { | |
273 | xauthdir = xmalloc(MAXPATHLEN); | |
274 | xauthfile = xmalloc(MAXPATHLEN); | |
275 | strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN); | |
276 | if (mkdtemp(xauthdir) != NULL) { | |
277 | do_unlink = 1; | |
278 | snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile", | |
279 | xauthdir); | |
280 | snprintf(cmd, sizeof(cmd), | |
281 | "%s -f %s generate %s " SSH_X11_PROTO | |
282 | " untrusted timeout 1200 2>" _PATH_DEVNULL, | |
283 | xauth_path, xauthfile, display); | |
284 | debug2("x11_get_proto: %s", cmd); | |
285 | if (system(cmd) == 0) | |
286 | generated = 1; | |
287 | } | |
288 | } | |
289 | snprintf(cmd, sizeof(cmd), | |
b5443199 | 290 | "%s %s%s list %s 2>" _PATH_DEVNULL, |
9bf083eb | 291 | xauth_path, |
292 | generated ? "-f " : "" , | |
293 | generated ? xauthfile : "", | |
294 | display); | |
295 | debug2("x11_get_proto: %s", cmd); | |
296 | f = popen(cmd, "r"); | |
297 | if (f && fgets(line, sizeof(line), f) && | |
298 | sscanf(line, "%*s %511s %511s", proto, data) == 2) | |
299 | got_data = 1; | |
300 | if (f) | |
301 | pclose(f); | |
302 | } | |
303 | ||
304 | if (do_unlink) { | |
305 | unlink(xauthfile); | |
306 | rmdir(xauthdir); | |
307 | } | |
308 | if (xauthdir) | |
309 | xfree(xauthdir); | |
310 | if (xauthfile) | |
311 | xfree(xauthfile); | |
312 | ||
313 | /* | |
314 | * If we didn't get authentication data, just make up some | |
315 | * data. The forwarding code will check the validity of the | |
316 | * response anyway, and substitute this data. The X11 | |
317 | * server, however, will ignore this fake data and use | |
318 | * whatever authentication mechanisms it was using otherwise | |
319 | * for the local connection. | |
320 | */ | |
321 | if (!got_data) { | |
322 | u_int32_t rnd = 0; | |
323 | ||
324 | logit("Warning: No xauth data; " | |
325 | "using fake authentication data for X11 forwarding."); | |
326 | strlcpy(proto, SSH_X11_PROTO, sizeof proto); | |
327 | for (i = 0; i < 16; i++) { | |
328 | if (i % 4 == 0) | |
329 | rnd = arc4random(); | |
330 | snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", | |
331 | rnd & 0xff); | |
332 | rnd >>= 8; | |
333 | } | |
334 | } | |
335 | } | |
336 | ||
aa3378df | 337 | /* |
338 | * This is called when the interactive is entered. This checks if there is | |
339 | * an EOF coming on stdin. We must check this explicitly, as select() does | |
340 | * not appear to wake up when redirecting from /dev/null. | |
341 | */ | |
8efc0c15 | 342 | |
396c147e | 343 | static void |
5ca51e19 | 344 | client_check_initial_eof_on_stdin(void) |
8efc0c15 | 345 | { |
5260325f | 346 | int len; |
347 | char buf[1]; | |
348 | ||
aa3378df | 349 | /* |
350 | * If standard input is to be "redirected from /dev/null", we simply | |
351 | * mark that we have seen an EOF and send an EOF message to the | |
352 | * server. Otherwise, we try to read a single character; it appears | |
353 | * that for some files, such /dev/null, select() never wakes up for | |
354 | * read for this descriptor, which means that we never get EOF. This | |
355 | * way we will get the EOF if stdin comes from /dev/null or similar. | |
356 | */ | |
5260325f | 357 | if (stdin_null_flag) { |
358 | /* Fake EOF on stdin. */ | |
359 | debug("Sending eof."); | |
360 | stdin_eof = 1; | |
361 | packet_start(SSH_CMSG_EOF); | |
362 | packet_send(); | |
363 | } else { | |
5260325f | 364 | enter_non_blocking(); |
365 | ||
366 | /* Check for immediate EOF on stdin. */ | |
367 | len = read(fileno(stdin), buf, 1); | |
368 | if (len == 0) { | |
aa3378df | 369 | /* EOF. Record that we have seen it and send EOF to server. */ |
5260325f | 370 | debug("Sending eof."); |
371 | stdin_eof = 1; | |
372 | packet_start(SSH_CMSG_EOF); | |
373 | packet_send(); | |
374 | } else if (len > 0) { | |
aa3378df | 375 | /* |
376 | * Got data. We must store the data in the buffer, | |
377 | * and also process it as an escape character if | |
378 | * appropriate. | |
379 | */ | |
1e3b8b07 | 380 | if ((u_char) buf[0] == escape_char) |
5260325f | 381 | escape_pending = 1; |
7e8911cd | 382 | else |
5260325f | 383 | buffer_append(&stdin_buffer, buf, 1); |
5260325f | 384 | } |
5260325f | 385 | leave_non_blocking(); |
8efc0c15 | 386 | } |
8efc0c15 | 387 | } |
388 | ||
8efc0c15 | 389 | |
aa3378df | 390 | /* |
391 | * Make packets from buffered stdin data, and buffer them for sending to the | |
392 | * connection. | |
393 | */ | |
8efc0c15 | 394 | |
396c147e | 395 | static void |
5ca51e19 | 396 | client_make_packets_from_stdin_data(void) |
8efc0c15 | 397 | { |
1e3b8b07 | 398 | u_int len; |
5260325f | 399 | |
400 | /* Send buffered stdin data to the server. */ | |
401 | while (buffer_len(&stdin_buffer) > 0 && | |
184eed6a | 402 | packet_not_very_much_data_to_write()) { |
5260325f | 403 | len = buffer_len(&stdin_buffer); |
404 | /* Keep the packets at reasonable size. */ | |
405 | if (len > packet_get_maxsize()) | |
406 | len = packet_get_maxsize(); | |
407 | packet_start(SSH_CMSG_STDIN_DATA); | |
408 | packet_put_string(buffer_ptr(&stdin_buffer), len); | |
409 | packet_send(); | |
410 | buffer_consume(&stdin_buffer, len); | |
7e8911cd | 411 | stdin_bytes += len; |
5260325f | 412 | /* If we have a pending EOF, send it now. */ |
413 | if (stdin_eof && buffer_len(&stdin_buffer) == 0) { | |
414 | packet_start(SSH_CMSG_EOF); | |
415 | packet_send(); | |
416 | } | |
8efc0c15 | 417 | } |
8efc0c15 | 418 | } |
419 | ||
aa3378df | 420 | /* |
421 | * Checks if the client window has changed, and sends a packet about it to | |
422 | * the server if so. The actual change is detected elsewhere (by a software | |
423 | * interrupt on Unix); this just checks the flag and sends a message if | |
424 | * appropriate. | |
425 | */ | |
8efc0c15 | 426 | |
396c147e | 427 | static void |
5ca51e19 | 428 | client_check_window_change(void) |
8efc0c15 | 429 | { |
8ce64345 | 430 | struct winsize ws; |
431 | ||
432 | if (! received_window_change_signal) | |
433 | return; | |
434 | /** XXX race */ | |
435 | received_window_change_signal = 0; | |
436 | ||
de273eef | 437 | debug2("client_check_window_change: changed"); |
8ce64345 | 438 | |
439 | if (compat20) { | |
5e96b616 | 440 | channel_send_window_changes(); |
8ce64345 | 441 | } else { |
5e96b616 | 442 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) |
443 | return; | |
8ce64345 | 444 | packet_start(SSH_CMSG_WINDOW_SIZE); |
148de80c | 445 | packet_put_int((u_int)ws.ws_row); |
446 | packet_put_int((u_int)ws.ws_col); | |
447 | packet_put_int((u_int)ws.ws_xpixel); | |
448 | packet_put_int((u_int)ws.ws_ypixel); | |
8ce64345 | 449 | packet_send(); |
8efc0c15 | 450 | } |
8efc0c15 | 451 | } |
452 | ||
5d8d32a3 | 453 | static void |
454 | client_global_request_reply(int type, u_int32_t seq, void *ctxt) | |
455 | { | |
456 | server_alive_timeouts = 0; | |
457 | client_global_request_reply_fwd(type, seq, ctxt); | |
458 | } | |
459 | ||
460 | static void | |
461 | server_alive_check(void) | |
462 | { | |
463 | if (++server_alive_timeouts > options.server_alive_count_max) | |
464 | packet_disconnect("Timeout, server not responding."); | |
465 | packet_start(SSH2_MSG_GLOBAL_REQUEST); | |
466 | packet_put_cstring("keepalive@openssh.com"); | |
467 | packet_put_char(1); /* boolean: want reply */ | |
468 | packet_send(); | |
469 | } | |
470 | ||
aa3378df | 471 | /* |
472 | * Waits until the client can do something (some data becomes available on | |
473 | * one of the file descriptors). | |
474 | */ | |
396c147e | 475 | static void |
39929cdb | 476 | client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, |
9a995072 | 477 | int *maxfdp, u_int *nallocp, int rekeying) |
8efc0c15 | 478 | { |
5d8d32a3 | 479 | struct timeval tv, *tvp; |
480 | int ret; | |
481 | ||
39929cdb | 482 | /* Add any selections by the channel mechanism. */ |
489aa2e9 | 483 | channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying); |
5260325f | 484 | |
8ce64345 | 485 | if (!compat20) { |
486 | /* Read from the connection, unless our buffers are full. */ | |
487 | if (buffer_len(&stdout_buffer) < buffer_high && | |
488 | buffer_len(&stderr_buffer) < buffer_high && | |
489 | channel_not_very_much_buffered_data()) | |
39929cdb | 490 | FD_SET(connection_in, *readsetp); |
8ce64345 | 491 | /* |
492 | * Read from stdin, unless we have seen EOF or have very much | |
493 | * buffered data to send to the server. | |
494 | */ | |
495 | if (!stdin_eof && packet_not_very_much_data_to_write()) | |
39929cdb | 496 | FD_SET(fileno(stdin), *readsetp); |
8ce64345 | 497 | |
498 | /* Select stdout/stderr if have data in buffer. */ | |
499 | if (buffer_len(&stdout_buffer) > 0) | |
39929cdb | 500 | FD_SET(fileno(stdout), *writesetp); |
8ce64345 | 501 | if (buffer_len(&stderr_buffer) > 0) |
39929cdb | 502 | FD_SET(fileno(stderr), *writesetp); |
8ce64345 | 503 | } else { |
24b6b45f | 504 | /* channel_prepare_select could have closed the last channel */ |
fd6cfbaf | 505 | if (session_closed && !channel_still_open() && |
506 | !packet_have_data_to_write()) { | |
507 | /* clear mask since we did not call select() */ | |
b3ad8fe6 | 508 | memset(*readsetp, 0, *nallocp); |
509 | memset(*writesetp, 0, *nallocp); | |
fd6cfbaf | 510 | return; |
24b6b45f | 511 | } else { |
512 | FD_SET(connection_in, *readsetp); | |
513 | } | |
8ce64345 | 514 | } |
5260325f | 515 | |
5260325f | 516 | /* Select server connection if have data to write to the server. */ |
517 | if (packet_have_data_to_write()) | |
39929cdb | 518 | FD_SET(connection_out, *writesetp); |
5260325f | 519 | |
5e96b616 | 520 | if (control_fd != -1) |
521 | FD_SET(control_fd, *readsetp); | |
522 | ||
aa3378df | 523 | /* |
524 | * Wait for something to happen. This will suspend the process until | |
525 | * some selected descriptor can be read, written, or has some other | |
5d8d32a3 | 526 | * event pending. |
aa3378df | 527 | */ |
5260325f | 528 | |
5d8d32a3 | 529 | if (options.server_alive_interval == 0 || !compat20) |
530 | tvp = NULL; | |
f2107e97 | 531 | else { |
5d8d32a3 | 532 | tv.tv_sec = options.server_alive_interval; |
533 | tv.tv_usec = 0; | |
534 | tvp = &tv; | |
535 | } | |
536 | ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); | |
537 | if (ret < 0) { | |
5260325f | 538 | char buf[100]; |
fd193ca4 | 539 | |
540 | /* | |
541 | * We have to clear the select masks, because we return. | |
542 | * We have to return, because the mainloop checks for the flags | |
543 | * set by the signal handlers. | |
544 | */ | |
b3ad8fe6 | 545 | memset(*readsetp, 0, *nallocp); |
546 | memset(*writesetp, 0, *nallocp); | |
fd193ca4 | 547 | |
5260325f | 548 | if (errno == EINTR) |
549 | return; | |
550 | /* Note: we might still have data in the buffers. */ | |
551 | snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno)); | |
552 | buffer_append(&stderr_buffer, buf, strlen(buf)); | |
5260325f | 553 | quit_pending = 1; |
5d8d32a3 | 554 | } else if (ret == 0) |
555 | server_alive_check(); | |
8efc0c15 | 556 | } |
557 | ||
396c147e | 558 | static void |
2e73a022 | 559 | client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr) |
8efc0c15 | 560 | { |
5260325f | 561 | /* Flush stdout and stderr buffers. */ |
2e73a022 | 562 | if (buffer_len(bout) > 0) |
dc54438a | 563 | atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout)); |
2e73a022 | 564 | if (buffer_len(berr) > 0) |
dc54438a | 565 | atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr)); |
5260325f | 566 | |
5260325f | 567 | leave_raw_mode(); |
568 | ||
aa3378df | 569 | /* |
570 | * Free (and clear) the buffer to reduce the amount of data that gets | |
571 | * written to swap. | |
572 | */ | |
2e73a022 | 573 | buffer_free(bin); |
574 | buffer_free(bout); | |
575 | buffer_free(berr); | |
5260325f | 576 | |
5260325f | 577 | /* Send the suspend signal to the program itself. */ |
578 | kill(getpid(), SIGTSTP); | |
579 | ||
2c9a4d41 | 580 | /* Reset window sizes in case they have changed */ |
581 | received_window_change_signal = 1; | |
5260325f | 582 | |
583 | /* OK, we have been continued by the user. Reinitialize buffers. */ | |
2e73a022 | 584 | buffer_init(bin); |
585 | buffer_init(bout); | |
586 | buffer_init(berr); | |
5260325f | 587 | |
5260325f | 588 | enter_raw_mode(); |
8efc0c15 | 589 | } |
590 | ||
396c147e | 591 | static void |
b476adfa | 592 | client_process_net_input(fd_set *readset) |
8efc0c15 | 593 | { |
8ce64345 | 594 | int len; |
595 | char buf[8192]; | |
5260325f | 596 | |
aa3378df | 597 | /* |
598 | * Read input from the server, and add any such data to the buffer of | |
599 | * the packet subsystem. | |
600 | */ | |
5260325f | 601 | if (FD_ISSET(connection_in, readset)) { |
602 | /* Read as much as possible. */ | |
603 | len = read(connection_in, buf, sizeof(buf)); | |
604 | if (len == 0) { | |
605 | /* Received EOF. The remote host has closed the connection. */ | |
606 | snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n", | |
607 | host); | |
8efc0c15 | 608 | buffer_append(&stderr_buffer, buf, strlen(buf)); |
8efc0c15 | 609 | quit_pending = 1; |
610 | return; | |
5260325f | 611 | } |
aa3378df | 612 | /* |
613 | * There is a kernel bug on Solaris that causes select to | |
614 | * sometimes wake up even though there is no data available. | |
615 | */ | |
09cb311c | 616 | if (len < 0 && (errno == EAGAIN || errno == EINTR)) |
5260325f | 617 | len = 0; |
618 | ||
619 | if (len < 0) { | |
620 | /* An error has encountered. Perhaps there is a network problem. */ | |
621 | snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n", | |
622 | host, strerror(errno)); | |
623 | buffer_append(&stderr_buffer, buf, strlen(buf)); | |
5260325f | 624 | quit_pending = 1; |
625 | return; | |
626 | } | |
627 | packet_process_incoming(buf, len); | |
628 | } | |
8ce64345 | 629 | } |
630 | ||
5e96b616 | 631 | static void |
632 | client_subsystem_reply(int type, u_int32_t seq, void *ctxt) | |
633 | { | |
634 | int id; | |
635 | Channel *c; | |
f2107e97 | 636 | |
5e96b616 | 637 | id = packet_get_int(); |
638 | packet_check_eom(); | |
639 | ||
640 | if ((c = channel_lookup(id)) == NULL) { | |
641 | error("%s: no channel for id %d", __func__, id); | |
642 | return; | |
643 | } | |
644 | ||
645 | if (type == SSH2_MSG_CHANNEL_SUCCESS) | |
646 | debug2("Request suceeded on channel %d", id); | |
647 | else if (type == SSH2_MSG_CHANNEL_FAILURE) { | |
648 | error("Request failed on channel %d", id); | |
649 | channel_free(c); | |
650 | } | |
651 | } | |
652 | ||
653 | static void | |
654 | client_extra_session2_setup(int id, void *arg) | |
655 | { | |
656 | struct confirm_ctx *cctx = arg; | |
ef07103c | 657 | const char *display; |
5e96b616 | 658 | Channel *c; |
1786be35 | 659 | int i; |
f2107e97 | 660 | |
5e96b616 | 661 | if (cctx == NULL) |
662 | fatal("%s: cctx == NULL", __func__); | |
663 | if ((c = channel_lookup(id)) == NULL) | |
664 | fatal("%s: no channel for id %d", __func__, id); | |
665 | ||
56964485 | 666 | display = getenv("DISPLAY"); |
ef07103c | 667 | if (cctx->want_x_fwd && options.forward_x11 && display != NULL) { |
668 | char *proto, *data; | |
669 | /* Get reasonable local authentication information. */ | |
670 | client_x11_get_proto(display, options.xauth_location, | |
671 | options.forward_x11_trusted, &proto, &data); | |
672 | /* Request forwarding with authentication spoofing. */ | |
673 | debug("Requesting X11 forwarding with authentication spoofing."); | |
674 | x11_request_forwarding_with_spoofing(id, display, proto, data); | |
675 | /* XXX wait for reply */ | |
676 | } | |
677 | ||
678 | if (cctx->want_agent_fwd && options.forward_agent) { | |
679 | debug("Requesting authentication agent forwarding."); | |
680 | channel_request_start(id, "auth-agent-req@openssh.com", 0); | |
681 | packet_send(); | |
682 | } | |
683 | ||
f2107e97 | 684 | client_session2_setup(id, cctx->want_tty, cctx->want_subsys, |
1786be35 | 685 | cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env, |
5e96b616 | 686 | client_subsystem_reply); |
0d34d6ce | 687 | |
5e96b616 | 688 | c->confirm_ctx = NULL; |
689 | buffer_free(&cctx->cmd); | |
1786be35 | 690 | xfree(cctx->term); |
691 | if (cctx->env != NULL) { | |
692 | for (i = 0; cctx->env[i] != NULL; i++) | |
693 | xfree(cctx->env[i]); | |
694 | xfree(cctx->env); | |
f2107e97 | 695 | } |
1786be35 | 696 | xfree(cctx); |
5e96b616 | 697 | } |
698 | ||
699 | static void | |
b476adfa | 700 | client_process_control(fd_set *readset) |
5e96b616 | 701 | { |
702 | Buffer m; | |
703 | Channel *c; | |
2ceb8101 | 704 | int client_fd, new_fd[3], ver, allowed; |
5e96b616 | 705 | socklen_t addrlen; |
706 | struct sockaddr_storage addr; | |
707 | struct confirm_ctx *cctx; | |
708 | char *cmd; | |
2ceb8101 | 709 | u_int i, len, env_len, command, flags; |
5e96b616 | 710 | uid_t euid; |
711 | gid_t egid; | |
712 | ||
713 | /* | |
714 | * Accept connection on control socket | |
715 | */ | |
716 | if (control_fd == -1 || !FD_ISSET(control_fd, readset)) | |
717 | return; | |
718 | ||
719 | memset(&addr, 0, sizeof(addr)); | |
720 | addrlen = sizeof(addr); | |
721 | if ((client_fd = accept(control_fd, | |
722 | (struct sockaddr*)&addr, &addrlen)) == -1) { | |
723 | error("%s accept: %s", __func__, strerror(errno)); | |
724 | return; | |
725 | } | |
726 | ||
727 | if (getpeereid(client_fd, &euid, &egid) < 0) { | |
728 | error("%s getpeereid failed: %s", __func__, strerror(errno)); | |
729 | close(client_fd); | |
730 | return; | |
731 | } | |
732 | if ((euid != 0) && (getuid() != euid)) { | |
733 | error("control mode uid mismatch: peer euid %u != uid %u", | |
734 | (u_int) euid, (u_int) getuid()); | |
735 | close(client_fd); | |
736 | return; | |
737 | } | |
0d34d6ce | 738 | |
5e96b616 | 739 | unset_nonblock(client_fd); |
740 | ||
f8c6db83 | 741 | /* Read command */ |
5e96b616 | 742 | buffer_init(&m); |
f8c6db83 | 743 | if (ssh_msg_recv(client_fd, &m) == -1) { |
744 | error("%s: client msg_recv failed", __func__); | |
745 | close(client_fd); | |
746 | buffer_free(&m); | |
747 | return; | |
748 | } | |
ef07103c | 749 | if ((ver = buffer_get_char(&m)) != SSHMUX_VER) { |
f8c6db83 | 750 | error("%s: wrong client version %d", __func__, ver); |
751 | buffer_free(&m); | |
752 | close(client_fd); | |
753 | return; | |
754 | } | |
755 | ||
756 | allowed = 1; | |
757 | command = buffer_get_int(&m); | |
758 | flags = buffer_get_int(&m); | |
759 | ||
760 | buffer_clear(&m); | |
5e96b616 | 761 | |
f8c6db83 | 762 | switch (command) { |
763 | case SSHMUX_COMMAND_OPEN: | |
9dfd96d6 | 764 | if (options.control_master == SSHCTL_MASTER_ASK || |
765 | options.control_master == SSHCTL_MASTER_AUTO_ASK) | |
f8c6db83 | 766 | allowed = ask_permission("Allow shared connection " |
767 | "to %s? ", host); | |
768 | /* continue below */ | |
769 | break; | |
770 | case SSHMUX_COMMAND_TERMINATE: | |
9dfd96d6 | 771 | if (options.control_master == SSHCTL_MASTER_ASK || |
772 | options.control_master == SSHCTL_MASTER_AUTO_ASK) | |
f8c6db83 | 773 | allowed = ask_permission("Terminate shared connection " |
774 | "to %s? ", host); | |
775 | if (allowed) | |
776 | quit_pending = 1; | |
f8cc7664 | 777 | /* FALLTHROUGH */ |
f8c6db83 | 778 | case SSHMUX_COMMAND_ALIVE_CHECK: |
779 | /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */ | |
780 | buffer_clear(&m); | |
781 | buffer_put_int(&m, allowed); | |
782 | buffer_put_int(&m, getpid()); | |
ef07103c | 783 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
f8c6db83 | 784 | error("%s: client msg_send failed", __func__); |
785 | close(client_fd); | |
786 | buffer_free(&m); | |
787 | return; | |
788 | } | |
789 | buffer_free(&m); | |
790 | close(client_fd); | |
791 | return; | |
792 | default: | |
793 | error("Unsupported command %d", command); | |
794 | buffer_free(&m); | |
795 | close(client_fd); | |
796 | return; | |
797 | } | |
798 | ||
799 | /* Reply for SSHMUX_COMMAND_OPEN */ | |
800 | buffer_clear(&m); | |
0d34d6ce | 801 | buffer_put_int(&m, allowed); |
5e96b616 | 802 | buffer_put_int(&m, getpid()); |
ef07103c | 803 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
5e96b616 | 804 | error("%s: client msg_send failed", __func__); |
805 | close(client_fd); | |
0d34d6ce | 806 | buffer_free(&m); |
5e96b616 | 807 | return; |
808 | } | |
5e96b616 | 809 | |
0d34d6ce | 810 | if (!allowed) { |
811 | error("Refused control connection"); | |
812 | close(client_fd); | |
813 | buffer_free(&m); | |
814 | return; | |
815 | } | |
816 | ||
f8c6db83 | 817 | buffer_clear(&m); |
5e96b616 | 818 | if (ssh_msg_recv(client_fd, &m) == -1) { |
819 | error("%s: client msg_recv failed", __func__); | |
820 | close(client_fd); | |
0d34d6ce | 821 | buffer_free(&m); |
5e96b616 | 822 | return; |
823 | } | |
ef07103c | 824 | if ((ver = buffer_get_char(&m)) != SSHMUX_VER) { |
5e96b616 | 825 | error("%s: wrong client version %d", __func__, ver); |
826 | buffer_free(&m); | |
827 | close(client_fd); | |
828 | return; | |
829 | } | |
830 | ||
52e3daed | 831 | cctx = xcalloc(1, sizeof(*cctx)); |
f8c6db83 | 832 | cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0; |
833 | cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0; | |
ef07103c | 834 | cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0; |
835 | cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0; | |
5e96b616 | 836 | cctx->term = buffer_get_string(&m, &len); |
837 | ||
838 | cmd = buffer_get_string(&m, &len); | |
839 | buffer_init(&cctx->cmd); | |
840 | buffer_append(&cctx->cmd, cmd, strlen(cmd)); | |
841 | ||
1786be35 | 842 | env_len = buffer_get_int(&m); |
843 | env_len = MIN(env_len, 4096); | |
844 | debug3("%s: receiving %d env vars", __func__, env_len); | |
845 | if (env_len != 0) { | |
52e3daed | 846 | cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env)); |
1786be35 | 847 | for (i = 0; i < env_len; i++) |
848 | cctx->env[i] = buffer_get_string(&m, &len); | |
849 | cctx->env[i] = NULL; | |
850 | } | |
851 | ||
5e96b616 | 852 | debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__, |
853 | cctx->want_tty, cctx->want_subsys, cmd); | |
3173dd0d | 854 | xfree(cmd); |
5e96b616 | 855 | |
856 | /* Gather fds from client */ | |
857 | new_fd[0] = mm_receive_fd(client_fd); | |
858 | new_fd[1] = mm_receive_fd(client_fd); | |
859 | new_fd[2] = mm_receive_fd(client_fd); | |
860 | ||
861 | debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__, | |
862 | new_fd[0], new_fd[1], new_fd[2]); | |
863 | ||
864 | /* Try to pick up ttymodes from client before it goes raw */ | |
865 | if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1) | |
866 | error("%s: tcgetattr: %s", __func__, strerror(errno)); | |
867 | ||
f8c6db83 | 868 | /* This roundtrip is just for synchronisation of ttymodes */ |
5e96b616 | 869 | buffer_clear(&m); |
ef07103c | 870 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { |
5e96b616 | 871 | error("%s: client msg_send failed", __func__); |
872 | close(client_fd); | |
873 | close(new_fd[0]); | |
874 | close(new_fd[1]); | |
875 | close(new_fd[2]); | |
0d34d6ce | 876 | buffer_free(&m); |
f8c6db83 | 877 | xfree(cctx->term); |
878 | if (env_len != 0) { | |
879 | for (i = 0; i < env_len; i++) | |
880 | xfree(cctx->env[i]); | |
881 | xfree(cctx->env); | |
882 | } | |
5e96b616 | 883 | return; |
884 | } | |
885 | buffer_free(&m); | |
886 | ||
887 | /* enable nonblocking unless tty */ | |
888 | if (!isatty(new_fd[0])) | |
889 | set_nonblock(new_fd[0]); | |
890 | if (!isatty(new_fd[1])) | |
891 | set_nonblock(new_fd[1]); | |
892 | if (!isatty(new_fd[2])) | |
893 | set_nonblock(new_fd[2]); | |
894 | ||
895 | set_nonblock(client_fd); | |
896 | ||
f2107e97 | 897 | c = channel_new("session", SSH_CHANNEL_OPENING, |
5e96b616 | 898 | new_fd[0], new_fd[1], new_fd[2], |
899 | CHAN_SES_WINDOW_DEFAULT, CHAN_SES_PACKET_DEFAULT, | |
900 | CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0); | |
901 | ||
902 | /* XXX */ | |
903 | c->ctl_fd = client_fd; | |
904 | ||
905 | debug3("%s: channel_new: %d", __func__, c->self); | |
906 | ||
907 | channel_send_open(c->self); | |
908 | channel_register_confirm(c->self, client_extra_session2_setup, cctx); | |
909 | } | |
910 | ||
c53c54c2 | 911 | static void |
ec9b7086 | 912 | process_cmdline(void) |
c53c54c2 | 913 | { |
c53c54c2 | 914 | void (*handler)(int); |
3867aa0a | 915 | char *s, *cmd, *cancel_host; |
d740ec16 | 916 | int delete = 0; |
c53c54c2 | 917 | int local = 0; |
3867aa0a | 918 | u_short cancel_port; |
919 | Forward fwd; | |
c53c54c2 | 920 | |
921 | leave_raw_mode(); | |
875ec275 | 922 | handler = signal(SIGINT, SIG_IGN); |
ec9b7086 | 923 | cmd = s = read_passphrase("\r\nssh> ", RP_ECHO); |
c53c54c2 | 924 | if (s == NULL) |
925 | goto out; | |
c53c54c2 | 926 | while (*s && isspace(*s)) |
927 | s++; | |
d740ec16 | 928 | if (*s == '-') |
929 | s++; /* Skip cmdline '-', if any */ | |
27c6fcae | 930 | if (*s == '\0') |
c53c54c2 | 931 | goto out; |
d740ec16 | 932 | |
7069a5e2 | 933 | if (*s == 'h' || *s == 'H' || *s == '?') { |
d740ec16 | 934 | logit("Commands:"); |
9f1090be | 935 | logit(" -L[bind_address:]port:host:hostport " |
936 | "Request local forward"); | |
937 | logit(" -R[bind_address:]port:host:hostport " | |
938 | "Request remote forward"); | |
33f13fa5 | 939 | logit(" -KR[bind_address:]port " |
9f1090be | 940 | "Cancel remote forward"); |
d20f3c9e | 941 | if (!options.permit_local_command) |
942 | goto out; | |
9f1090be | 943 | logit(" !args " |
944 | "Execute local command"); | |
d20f3c9e | 945 | goto out; |
946 | } | |
947 | ||
948 | if (*s == '!' && options.permit_local_command) { | |
949 | s++; | |
950 | ssh_local_cmd(s); | |
d740ec16 | 951 | goto out; |
952 | } | |
953 | ||
954 | if (*s == 'K') { | |
955 | delete = 1; | |
956 | s++; | |
957 | } | |
958 | if (*s != 'L' && *s != 'R') { | |
bbe88b6d | 959 | logit("Invalid command."); |
c53c54c2 | 960 | goto out; |
961 | } | |
d740ec16 | 962 | if (*s == 'L') |
c53c54c2 | 963 | local = 1; |
d740ec16 | 964 | if (local && delete) { |
965 | logit("Not supported."); | |
966 | goto out; | |
967 | } | |
968 | if ((!local || delete) && !compat20) { | |
bbe88b6d | 969 | logit("Not supported for SSH protocol version 1."); |
c53c54c2 | 970 | goto out; |
971 | } | |
d740ec16 | 972 | |
973 | s++; | |
c53c54c2 | 974 | while (*s && isspace(*s)) |
975 | s++; | |
976 | ||
d740ec16 | 977 | if (delete) { |
3867aa0a | 978 | cancel_port = 0; |
979 | cancel_host = hpdelim(&s); /* may be NULL */ | |
980 | if (s != NULL) { | |
981 | cancel_port = a2port(s); | |
982 | cancel_host = cleanhostname(cancel_host); | |
983 | } else { | |
984 | cancel_port = a2port(cancel_host); | |
985 | cancel_host = NULL; | |
d740ec16 | 986 | } |
3867aa0a | 987 | if (cancel_port == 0) { |
988 | logit("Bad forwarding close port"); | |
c53c54c2 | 989 | goto out; |
990 | } | |
3867aa0a | 991 | channel_request_rforward_cancel(cancel_host, cancel_port); |
d740ec16 | 992 | } else { |
3867aa0a | 993 | if (!parse_forward(&fwd, s)) { |
d740ec16 | 994 | logit("Bad forwarding specification."); |
995 | goto out; | |
996 | } | |
d740ec16 | 997 | if (local) { |
3867aa0a | 998 | if (channel_setup_local_fwd_listener(fwd.listen_host, |
999 | fwd.listen_port, fwd.connect_host, | |
1000 | fwd.connect_port, options.gateway_ports) < 0) { | |
d740ec16 | 1001 | logit("Port forwarding failed."); |
1002 | goto out; | |
1003 | } | |
3867aa0a | 1004 | } else { |
42ea6f5e | 1005 | if (channel_request_remote_forwarding(fwd.listen_host, |
3867aa0a | 1006 | fwd.listen_port, fwd.connect_host, |
42ea6f5e | 1007 | fwd.connect_port) < 0) { |
1008 | logit("Port forwarding failed."); | |
1009 | goto out; | |
1010 | } | |
3867aa0a | 1011 | } |
1012 | ||
d740ec16 | 1013 | logit("Forwarding port."); |
1014 | } | |
1015 | ||
c53c54c2 | 1016 | out: |
1017 | signal(SIGINT, handler); | |
1018 | enter_raw_mode(); | |
1019 | if (cmd) | |
1020 | xfree(cmd); | |
1021 | } | |
1022 | ||
2e73a022 | 1023 | /* process the characters one by one */ |
396c147e | 1024 | static int |
2e73a022 | 1025 | process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len) |
1026 | { | |
1027 | char string[1024]; | |
1028 | pid_t pid; | |
1029 | int bytes = 0; | |
1e3b8b07 | 1030 | u_int i; |
1031 | u_char ch; | |
2e73a022 | 1032 | char *s; |
1033 | ||
2ceb8101 | 1034 | if (len <= 0) |
1035 | return (0); | |
1036 | ||
1037 | for (i = 0; i < (u_int)len; i++) { | |
2e73a022 | 1038 | /* Get one character at a time. */ |
1039 | ch = buf[i]; | |
1040 | ||
1041 | if (escape_pending) { | |
1042 | /* We have previously seen an escape character. */ | |
1043 | /* Clear the flag now. */ | |
1044 | escape_pending = 0; | |
1045 | ||
1046 | /* Process the escaped character. */ | |
1047 | switch (ch) { | |
1048 | case '.': | |
1049 | /* Terminate the connection. */ | |
1050 | snprintf(string, sizeof string, "%c.\r\n", escape_char); | |
1051 | buffer_append(berr, string, strlen(string)); | |
2e73a022 | 1052 | |
1053 | quit_pending = 1; | |
1054 | return -1; | |
1055 | ||
1056 | case 'Z' - 64: | |
1057 | /* Suspend the program. */ | |
1058 | /* Print a message to that effect to the user. */ | |
1059 | snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char); | |
1060 | buffer_append(berr, string, strlen(string)); | |
2e73a022 | 1061 | |
1062 | /* Restore terminal modes and suspend. */ | |
1063 | client_suspend_self(bin, bout, berr); | |
1064 | ||
1065 | /* We have been continued. */ | |
1066 | continue; | |
1067 | ||
16a79097 | 1068 | case 'B': |
1069 | if (compat20) { | |
1070 | snprintf(string, sizeof string, | |
1071 | "%cB\r\n", escape_char); | |
1072 | buffer_append(berr, string, | |
1073 | strlen(string)); | |
1074 | channel_request_start(session_ident, | |
1075 | "break", 0); | |
1076 | packet_put_int(1000); | |
1077 | packet_send(); | |
1078 | } | |
1079 | continue; | |
1080 | ||
e092ce67 | 1081 | case 'R': |
54506d2e | 1082 | if (compat20) { |
1083 | if (datafellows & SSH_BUG_NOREKEY) | |
bbe88b6d | 1084 | logit("Server does not support re-keying"); |
54506d2e | 1085 | else |
1086 | need_rekeying = 1; | |
1087 | } | |
e092ce67 | 1088 | continue; |
1089 | ||
2e73a022 | 1090 | case '&': |
2e73a022 | 1091 | /* |
1092 | * Detach the program (continue to serve connections, | |
1093 | * but put in background and no more new connections). | |
1094 | */ | |
2e73a022 | 1095 | /* Restore tty modes. */ |
1096 | leave_raw_mode(); | |
1097 | ||
1098 | /* Stop listening for new connections. */ | |
c6940381 | 1099 | channel_stop_listening(); |
2e73a022 | 1100 | |
c6940381 | 1101 | snprintf(string, sizeof string, |
1102 | "%c& [backgrounded]\n", escape_char); | |
1103 | buffer_append(berr, string, strlen(string)); | |
2e73a022 | 1104 | |
1105 | /* Fork into background. */ | |
1106 | pid = fork(); | |
1107 | if (pid < 0) { | |
1108 | error("fork: %.100s", strerror(errno)); | |
1109 | continue; | |
1110 | } | |
1111 | if (pid != 0) { /* This is the parent. */ | |
1112 | /* The parent just exits. */ | |
1113 | exit(0); | |
1114 | } | |
1115 | /* The child continues serving connections. */ | |
c6940381 | 1116 | if (compat20) { |
1117 | buffer_append(bin, "\004", 1); | |
1118 | /* fake EOF on stdin */ | |
1119 | return -1; | |
1120 | } else if (!stdin_eof) { | |
1121 | /* | |
1122 | * Sending SSH_CMSG_EOF alone does not always appear | |
1123 | * to be enough. So we try to send an EOF character | |
1124 | * first. | |
1125 | */ | |
1126 | packet_start(SSH_CMSG_STDIN_DATA); | |
1127 | packet_put_string("\004", 1); | |
1128 | packet_send(); | |
1129 | /* Close stdin. */ | |
1130 | stdin_eof = 1; | |
1131 | if (buffer_len(bin) == 0) { | |
1132 | packet_start(SSH_CMSG_EOF); | |
1133 | packet_send(); | |
1134 | } | |
1135 | } | |
1136 | continue; | |
2e73a022 | 1137 | |
1138 | case '?': | |
1139 | snprintf(string, sizeof string, | |
1140 | "%c?\r\n\ | |
1141 | Supported escape sequences:\r\n\ | |
1d77f8cb | 1142 | %c. - terminate connection\r\n\ |
16a79097 | 1143 | %cB - send a BREAK to the remote system\r\n\ |
1d77f8cb | 1144 | %cC - open a command line\r\n\ |
1145 | %cR - Request rekey (SSH protocol 2 only)\r\n\ | |
1146 | %c^Z - suspend ssh\r\n\ | |
1147 | %c# - list forwarded connections\r\n\ | |
1148 | %c& - background ssh (when waiting for connections to terminate)\r\n\ | |
1149 | %c? - this message\r\n\ | |
1150 | %c%c - send the escape character by typing it twice\r\n\ | |
2e73a022 | 1151 | (Note that escapes are only recognized immediately after newline.)\r\n", |
1d77f8cb | 1152 | escape_char, escape_char, escape_char, escape_char, |
1153 | escape_char, escape_char, escape_char, escape_char, | |
16a79097 | 1154 | escape_char, escape_char, escape_char); |
2e73a022 | 1155 | buffer_append(berr, string, strlen(string)); |
1156 | continue; | |
1157 | ||
1158 | case '#': | |
1159 | snprintf(string, sizeof string, "%c#\r\n", escape_char); | |
1160 | buffer_append(berr, string, strlen(string)); | |
1161 | s = channel_open_message(); | |
1162 | buffer_append(berr, s, strlen(s)); | |
1163 | xfree(s); | |
1164 | continue; | |
1165 | ||
c53c54c2 | 1166 | case 'C': |
ec9b7086 | 1167 | process_cmdline(); |
c53c54c2 | 1168 | continue; |
1169 | ||
2e73a022 | 1170 | default: |
1171 | if (ch != escape_char) { | |
1172 | buffer_put_char(bin, escape_char); | |
1173 | bytes++; | |
1174 | } | |
1175 | /* Escaped characters fall through here */ | |
1176 | break; | |
1177 | } | |
1178 | } else { | |
1179 | /* | |
1180 | * The previous character was not an escape char. Check if this | |
1181 | * is an escape. | |
1182 | */ | |
1183 | if (last_was_cr && ch == escape_char) { | |
1184 | /* It is. Set the flag and continue to next character. */ | |
1185 | escape_pending = 1; | |
1186 | continue; | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | /* | |
1191 | * Normal character. Record whether it was a newline, | |
1192 | * and append it to the buffer. | |
1193 | */ | |
1194 | last_was_cr = (ch == '\r' || ch == '\n'); | |
1195 | buffer_put_char(bin, ch); | |
1196 | bytes++; | |
1197 | } | |
1198 | return bytes; | |
1199 | } | |
1200 | ||
396c147e | 1201 | static void |
b476adfa | 1202 | client_process_input(fd_set *readset) |
8ce64345 | 1203 | { |
9da5c3c9 | 1204 | int len; |
2e73a022 | 1205 | char buf[8192]; |
8ce64345 | 1206 | |
5260325f | 1207 | /* Read input from stdin. */ |
1208 | if (FD_ISSET(fileno(stdin), readset)) { | |
1209 | /* Read as much as possible. */ | |
1210 | len = read(fileno(stdin), buf, sizeof(buf)); | |
cf6bc93c | 1211 | if (len < 0 && (errno == EAGAIN || errno == EINTR)) |
1212 | return; /* we'll try again later */ | |
5260325f | 1213 | if (len <= 0) { |
aa3378df | 1214 | /* |
1215 | * Received EOF or error. They are treated | |
1216 | * similarly, except that an error message is printed | |
1217 | * if it was an error condition. | |
1218 | */ | |
5260325f | 1219 | if (len < 0) { |
1220 | snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno)); | |
1221 | buffer_append(&stderr_buffer, buf, strlen(buf)); | |
5260325f | 1222 | } |
1223 | /* Mark that we have seen EOF. */ | |
1224 | stdin_eof = 1; | |
aa3378df | 1225 | /* |
1226 | * Send an EOF message to the server unless there is | |
1227 | * data in the buffer. If there is data in the | |
1228 | * buffer, no message will be sent now. Code | |
1229 | * elsewhere will send the EOF when the buffer | |
1230 | * becomes empty if stdin_eof is set. | |
1231 | */ | |
5260325f | 1232 | if (buffer_len(&stdin_buffer) == 0) { |
8efc0c15 | 1233 | packet_start(SSH_CMSG_EOF); |
1234 | packet_send(); | |
5260325f | 1235 | } |
4bf9c10e | 1236 | } else if (escape_char == SSH_ESCAPECHAR_NONE) { |
aa3378df | 1237 | /* |
1238 | * Normal successful read, and no escape character. | |
1239 | * Just append the data to buffer. | |
1240 | */ | |
5260325f | 1241 | buffer_append(&stdin_buffer, buf, len); |
5260325f | 1242 | } else { |
aa3378df | 1243 | /* |
1244 | * Normal, successful read. But we have an escape character | |
1245 | * and have to process the characters one by one. | |
1246 | */ | |
7e8911cd | 1247 | if (process_escapes(&stdin_buffer, &stdout_buffer, |
1248 | &stderr_buffer, buf, len) == -1) | |
2e73a022 | 1249 | return; |
5260325f | 1250 | } |
1251 | } | |
8efc0c15 | 1252 | } |
1253 | ||
396c147e | 1254 | static void |
b476adfa | 1255 | client_process_output(fd_set *writeset) |
8efc0c15 | 1256 | { |
5260325f | 1257 | int len; |
1258 | char buf[100]; | |
1259 | ||
1260 | /* Write buffered output to stdout. */ | |
1261 | if (FD_ISSET(fileno(stdout), writeset)) { | |
1262 | /* Write as much data as possible. */ | |
1263 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), | |
a408af76 | 1264 | buffer_len(&stdout_buffer)); |
5260325f | 1265 | if (len <= 0) { |
cf6bc93c | 1266 | if (errno == EINTR || errno == EAGAIN) |
5260325f | 1267 | len = 0; |
1268 | else { | |
aa3378df | 1269 | /* |
1270 | * An error or EOF was encountered. Put an | |
1271 | * error message to stderr buffer. | |
1272 | */ | |
5260325f | 1273 | snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno)); |
1274 | buffer_append(&stderr_buffer, buf, strlen(buf)); | |
5260325f | 1275 | quit_pending = 1; |
1276 | return; | |
1277 | } | |
1278 | } | |
1279 | /* Consume printed data from the buffer. */ | |
1280 | buffer_consume(&stdout_buffer, len); | |
cd332296 | 1281 | stdout_bytes += len; |
5260325f | 1282 | } |
1283 | /* Write buffered output to stderr. */ | |
1284 | if (FD_ISSET(fileno(stderr), writeset)) { | |
1285 | /* Write as much data as possible. */ | |
1286 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), | |
a408af76 | 1287 | buffer_len(&stderr_buffer)); |
5260325f | 1288 | if (len <= 0) { |
cf6bc93c | 1289 | if (errno == EINTR || errno == EAGAIN) |
5260325f | 1290 | len = 0; |
1291 | else { | |
aa3378df | 1292 | /* EOF or error, but can't even print error message. */ |
5260325f | 1293 | quit_pending = 1; |
1294 | return; | |
1295 | } | |
1296 | } | |
1297 | /* Consume printed characters from the buffer. */ | |
1298 | buffer_consume(&stderr_buffer, len); | |
cd332296 | 1299 | stderr_bytes += len; |
8efc0c15 | 1300 | } |
8efc0c15 | 1301 | } |
1302 | ||
7368a6c8 | 1303 | /* |
1304 | * Get packets from the connection input buffer, and process them as long as | |
1305 | * there are packets available. | |
1306 | * | |
1307 | * Any unknown packets received during the actual | |
1308 | * session cause the session to terminate. This is | |
1309 | * intended to make debugging easier since no | |
1310 | * confirmations are sent. Any compatible protocol | |
1311 | * extensions must be negotiated during the | |
1312 | * preparatory phase. | |
1313 | */ | |
1314 | ||
396c147e | 1315 | static void |
5ca51e19 | 1316 | client_process_buffered_input_packets(void) |
7368a6c8 | 1317 | { |
e092ce67 | 1318 | dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL); |
7368a6c8 | 1319 | } |
1320 | ||
2e73a022 | 1321 | /* scan buf[] for '~' before sending data to the peer */ |
1322 | ||
396c147e | 1323 | static int |
2e73a022 | 1324 | simple_escape_filter(Channel *c, char *buf, int len) |
1325 | { | |
1326 | /* XXX we assume c->extended is writeable */ | |
1327 | return process_escapes(&c->input, &c->output, &c->extended, buf, len); | |
1328 | } | |
1329 | ||
396c147e | 1330 | static void |
eb0dd41f | 1331 | client_channel_closed(int id, void *arg) |
1332 | { | |
418e724c | 1333 | channel_cancel_cleanup(id); |
eb0dd41f | 1334 | session_closed = 1; |
82de775c | 1335 | leave_raw_mode(); |
eb0dd41f | 1336 | } |
1337 | ||
aa3378df | 1338 | /* |
1339 | * Implements the interactive session with the server. This is called after | |
1340 | * the user has been authenticated, and a command has been started on the | |
4bf9c10e | 1341 | * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character |
1342 | * used as an escape character for terminating or suspending the session. | |
aa3378df | 1343 | */ |
8efc0c15 | 1344 | |
6ae2364d | 1345 | int |
2e73a022 | 1346 | client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id) |
8efc0c15 | 1347 | { |
39929cdb | 1348 | fd_set *readset = NULL, *writeset = NULL; |
5260325f | 1349 | double start_time, total_time; |
9a995072 | 1350 | int max_fd = 0, max_fd2 = 0, len, rekeying = 0; |
1351 | u_int nalloc = 0; | |
5260325f | 1352 | char buf[100]; |
1353 | ||
1354 | debug("Entering interactive session."); | |
1355 | ||
1356 | start_time = get_current_time(); | |
1357 | ||
1358 | /* Initialize variables. */ | |
1359 | escape_pending = 0; | |
1360 | last_was_cr = 1; | |
1361 | exit_status = -1; | |
1362 | stdin_eof = 0; | |
1363 | buffer_high = 64 * 1024; | |
1364 | connection_in = packet_get_connection_in(); | |
1365 | connection_out = packet_get_connection_out(); | |
39929cdb | 1366 | max_fd = MAX(connection_in, connection_out); |
5e96b616 | 1367 | if (control_fd != -1) |
1368 | max_fd = MAX(max_fd, control_fd); | |
39929cdb | 1369 | |
1370 | if (!compat20) { | |
e968270c | 1371 | /* enable nonblocking unless tty */ |
1372 | if (!isatty(fileno(stdin))) | |
1373 | set_nonblock(fileno(stdin)); | |
1374 | if (!isatty(fileno(stdout))) | |
1375 | set_nonblock(fileno(stdout)); | |
1376 | if (!isatty(fileno(stderr))) | |
1377 | set_nonblock(fileno(stderr)); | |
39929cdb | 1378 | max_fd = MAX(max_fd, fileno(stdin)); |
1379 | max_fd = MAX(max_fd, fileno(stdout)); | |
1380 | max_fd = MAX(max_fd, fileno(stderr)); | |
1381 | } | |
5260325f | 1382 | stdin_bytes = 0; |
1383 | stdout_bytes = 0; | |
1384 | stderr_bytes = 0; | |
1385 | quit_pending = 0; | |
1386 | escape_char = escape_char_arg; | |
1387 | ||
1388 | /* Initialize buffers. */ | |
1389 | buffer_init(&stdin_buffer); | |
1390 | buffer_init(&stdout_buffer); | |
1391 | buffer_init(&stderr_buffer); | |
1392 | ||
7368a6c8 | 1393 | client_init_dispatch(); |
1394 | ||
3c93c917 | 1395 | /* |
1396 | * Set signal handlers, (e.g. to restore non-blocking mode) | |
1397 | * but don't overwrite SIG_IGN, matches behaviour from rsh(1) | |
1398 | */ | |
d3e5d1e9 | 1399 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) |
1400 | signal(SIGHUP, signal_handler); | |
3c93c917 | 1401 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
1402 | signal(SIGINT, signal_handler); | |
1403 | if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) | |
1404 | signal(SIGQUIT, signal_handler); | |
1405 | if (signal(SIGTERM, SIG_IGN) != SIG_IGN) | |
1406 | signal(SIGTERM, signal_handler); | |
2c9a4d41 | 1407 | signal(SIGWINCH, window_change_handler); |
5260325f | 1408 | |
5260325f | 1409 | if (have_pty) |
1410 | enter_raw_mode(); | |
1411 | ||
1fc8ccdf | 1412 | if (compat20) { |
1413 | session_ident = ssh2_chan_id; | |
4bf9c10e | 1414 | if (escape_char != SSH_ESCAPECHAR_NONE) |
1fc8ccdf | 1415 | channel_register_filter(session_ident, |
c1c6a032 | 1416 | simple_escape_filter, NULL); |
eb0dd41f | 1417 | if (session_ident != -1) |
1418 | channel_register_cleanup(session_ident, | |
8cd0437d | 1419 | client_channel_closed, 0); |
1fc8ccdf | 1420 | } else { |
1421 | /* Check if we should immediately send eof on stdin. */ | |
8ce64345 | 1422 | client_check_initial_eof_on_stdin(); |
1fc8ccdf | 1423 | } |
2e73a022 | 1424 | |
5260325f | 1425 | /* Main loop of the client for the interactive session mode. */ |
1426 | while (!quit_pending) { | |
5260325f | 1427 | |
aa3378df | 1428 | /* Process buffered packets sent by the server. */ |
5260325f | 1429 | client_process_buffered_input_packets(); |
1430 | ||
eb0dd41f | 1431 | if (compat20 && session_closed && !channel_still_open()) |
8ce64345 | 1432 | break; |
eb0dd41f | 1433 | |
1434 | rekeying = (xxx_kex != NULL && !xxx_kex->done); | |
8ce64345 | 1435 | |
c422989b | 1436 | if (rekeying) { |
1437 | debug("rekeying in progress"); | |
1438 | } else { | |
1439 | /* | |
1440 | * Make packets of buffered stdin data, and buffer | |
1441 | * them for sending to the server. | |
1442 | */ | |
1443 | if (!compat20) | |
1444 | client_make_packets_from_stdin_data(); | |
5260325f | 1445 | |
c422989b | 1446 | /* |
1447 | * Make packets from buffered channel data, and | |
1448 | * enqueue them for sending to the server. | |
1449 | */ | |
1450 | if (packet_not_very_much_data_to_write()) | |
1451 | channel_output_poll(); | |
5260325f | 1452 | |
c422989b | 1453 | /* |
1454 | * Check if the window size has changed, and buffer a | |
1455 | * message about it to the server if so. | |
1456 | */ | |
1457 | client_check_window_change(); | |
5260325f | 1458 | |
c422989b | 1459 | if (quit_pending) |
1460 | break; | |
1461 | } | |
aa3378df | 1462 | /* |
1463 | * Wait until we have something to do (something becomes | |
1464 | * available on one of the descriptors). | |
1465 | */ | |
489aa2e9 | 1466 | max_fd2 = max_fd; |
c422989b | 1467 | client_wait_until_can_do_something(&readset, &writeset, |
489aa2e9 | 1468 | &max_fd2, &nalloc, rekeying); |
5260325f | 1469 | |
1470 | if (quit_pending) | |
1471 | break; | |
1472 | ||
c422989b | 1473 | /* Do channel operations unless rekeying in progress. */ |
1474 | if (!rekeying) { | |
1475 | channel_after_select(readset, writeset); | |
ffd7b36b | 1476 | if (need_rekeying || packet_need_rekeying()) { |
1477 | debug("need rekeying"); | |
c422989b | 1478 | xxx_kex->done = 0; |
1479 | kex_send_kexinit(xxx_kex); | |
1480 | need_rekeying = 0; | |
1481 | } | |
1482 | } | |
5260325f | 1483 | |
8ce64345 | 1484 | /* Buffer input from the connection. */ |
39929cdb | 1485 | client_process_net_input(readset); |
5260325f | 1486 | |
5e96b616 | 1487 | /* Accept control connections. */ |
1488 | client_process_control(readset); | |
1489 | ||
8ce64345 | 1490 | if (quit_pending) |
1491 | break; | |
1492 | ||
1493 | if (!compat20) { | |
1494 | /* Buffer data from stdin */ | |
39929cdb | 1495 | client_process_input(readset); |
8ce64345 | 1496 | /* |
1497 | * Process output to stdout and stderr. Output to | |
1498 | * the connection is processed elsewhere (above). | |
1499 | */ | |
39929cdb | 1500 | client_process_output(writeset); |
8ce64345 | 1501 | } |
5260325f | 1502 | |
aa3378df | 1503 | /* Send as much buffered packet data as possible to the sender. */ |
39929cdb | 1504 | if (FD_ISSET(connection_out, writeset)) |
5260325f | 1505 | packet_write_poll(); |
1506 | } | |
39929cdb | 1507 | if (readset) |
1508 | xfree(readset); | |
1509 | if (writeset) | |
1510 | xfree(writeset); | |
5260325f | 1511 | |
1512 | /* Terminate the session. */ | |
1513 | ||
1514 | /* Stop watching for window change. */ | |
2c9a4d41 | 1515 | signal(SIGWINCH, SIG_DFL); |
5260325f | 1516 | |
d6746a0b | 1517 | channel_free_all(); |
5260325f | 1518 | |
c9130033 | 1519 | if (have_pty) |
1520 | leave_raw_mode(); | |
89aa792b | 1521 | |
1522 | /* restore blocking io */ | |
1523 | if (!isatty(fileno(stdin))) | |
1524 | unset_nonblock(fileno(stdin)); | |
1525 | if (!isatty(fileno(stdout))) | |
1526 | unset_nonblock(fileno(stdout)); | |
1527 | if (!isatty(fileno(stderr))) | |
1528 | unset_nonblock(fileno(stderr)); | |
1529 | ||
66357af5 | 1530 | /* |
1531 | * If there was no shell or command requested, there will be no remote | |
1532 | * exit status to be returned. In that case, clear error code if the | |
1533 | * connection was deliberately terminated at this end. | |
1534 | */ | |
1535 | if (no_shell_flag && received_signal == SIGTERM) { | |
1536 | received_signal = 0; | |
1537 | exit_status = 0; | |
1538 | } | |
1539 | ||
82de775c | 1540 | if (received_signal) |
010f9726 | 1541 | fatal("Killed by signal %d.", (int) received_signal); |
c9130033 | 1542 | |
1543 | /* | |
1544 | * In interactive mode (with pseudo tty) display a message indicating | |
1545 | * that the connection has been closed. | |
1546 | */ | |
1547 | if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { | |
1548 | snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host); | |
1549 | buffer_append(&stderr_buffer, buf, strlen(buf)); | |
1550 | } | |
1551 | ||
5260325f | 1552 | /* Output any buffered data for stdout. */ |
89aa792b | 1553 | while (buffer_len(&stdout_buffer) > 0) { |
1554 | len = write(fileno(stdout), buffer_ptr(&stdout_buffer), | |
a408af76 | 1555 | buffer_len(&stdout_buffer)); |
89aa792b | 1556 | if (len <= 0) { |
5260325f | 1557 | error("Write failed flushing stdout buffer."); |
89aa792b | 1558 | break; |
1559 | } | |
5260325f | 1560 | buffer_consume(&stdout_buffer, len); |
cd332296 | 1561 | stdout_bytes += len; |
8efc0c15 | 1562 | } |
5260325f | 1563 | |
1564 | /* Output any buffered data for stderr. */ | |
89aa792b | 1565 | while (buffer_len(&stderr_buffer) > 0) { |
1566 | len = write(fileno(stderr), buffer_ptr(&stderr_buffer), | |
a408af76 | 1567 | buffer_len(&stderr_buffer)); |
89aa792b | 1568 | if (len <= 0) { |
5260325f | 1569 | error("Write failed flushing stderr buffer."); |
89aa792b | 1570 | break; |
1571 | } | |
5260325f | 1572 | buffer_consume(&stderr_buffer, len); |
cd332296 | 1573 | stderr_bytes += len; |
5260325f | 1574 | } |
1575 | ||
5260325f | 1576 | /* Clear and free any buffers. */ |
1577 | memset(buf, 0, sizeof(buf)); | |
1578 | buffer_free(&stdin_buffer); | |
1579 | buffer_free(&stdout_buffer); | |
1580 | buffer_free(&stderr_buffer); | |
1581 | ||
1582 | /* Report bytes transferred, and transfer rates. */ | |
1583 | total_time = get_current_time() - start_time; | |
1584 | debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds", | |
184eed6a | 1585 | stdin_bytes, stdout_bytes, stderr_bytes, total_time); |
5260325f | 1586 | if (total_time > 0) |
1587 | debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f", | |
184eed6a | 1588 | stdin_bytes / total_time, stdout_bytes / total_time, |
1589 | stderr_bytes / total_time); | |
5260325f | 1590 | |
1591 | /* Return the exit status of the program. */ | |
1592 | debug("Exit status %d", exit_status); | |
1593 | return exit_status; | |
8efc0c15 | 1594 | } |
7368a6c8 | 1595 | |
1596 | /*********/ | |
1597 | ||
396c147e | 1598 | static void |
7819b5c3 | 1599 | client_input_stdout_data(int type, u_int32_t seq, void *ctxt) |
7368a6c8 | 1600 | { |
1e3b8b07 | 1601 | u_int data_len; |
7368a6c8 | 1602 | char *data = packet_get_string(&data_len); |
95500969 | 1603 | packet_check_eom(); |
7368a6c8 | 1604 | buffer_append(&stdout_buffer, data, data_len); |
7368a6c8 | 1605 | memset(data, 0, data_len); |
1606 | xfree(data); | |
1607 | } | |
396c147e | 1608 | static void |
7819b5c3 | 1609 | client_input_stderr_data(int type, u_int32_t seq, void *ctxt) |
7368a6c8 | 1610 | { |
1e3b8b07 | 1611 | u_int data_len; |
7368a6c8 | 1612 | char *data = packet_get_string(&data_len); |
95500969 | 1613 | packet_check_eom(); |
7368a6c8 | 1614 | buffer_append(&stderr_buffer, data, data_len); |
7368a6c8 | 1615 | memset(data, 0, data_len); |
1616 | xfree(data); | |
1617 | } | |
396c147e | 1618 | static void |
7819b5c3 | 1619 | client_input_exit_status(int type, u_int32_t seq, void *ctxt) |
7368a6c8 | 1620 | { |
7368a6c8 | 1621 | exit_status = packet_get_int(); |
95500969 | 1622 | packet_check_eom(); |
7368a6c8 | 1623 | /* Acknowledge the exit. */ |
1624 | packet_start(SSH_CMSG_EXIT_CONFIRMATION); | |
1625 | packet_send(); | |
1626 | /* | |
1627 | * Must wait for packet to be sent since we are | |
1628 | * exiting the loop. | |
1629 | */ | |
1630 | packet_write_wait(); | |
1631 | /* Flag that we want to exit. */ | |
1632 | quit_pending = 1; | |
1633 | } | |
c15706e2 | 1634 | static void |
1635 | client_input_agent_open(int type, u_int32_t seq, void *ctxt) | |
1636 | { | |
1637 | Channel *c = NULL; | |
1638 | int remote_id, sock; | |
1639 | ||
1640 | /* Read the remote channel number from the message. */ | |
1641 | remote_id = packet_get_int(); | |
1642 | packet_check_eom(); | |
1643 | ||
1644 | /* | |
1645 | * Get a connection to the local authentication agent (this may again | |
1646 | * get forwarded). | |
1647 | */ | |
1648 | sock = ssh_get_authentication_socket(); | |
1649 | ||
1650 | /* | |
1651 | * If we could not connect the agent, send an error message back to | |
1652 | * the server. This should never happen unless the agent dies, | |
1653 | * because authentication forwarding is only enabled if we have an | |
1654 | * agent. | |
1655 | */ | |
1656 | if (sock >= 0) { | |
1657 | c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, | |
1658 | -1, 0, 0, 0, "authentication agent connection", 1); | |
1659 | c->remote_id = remote_id; | |
1660 | c->force_drain = 1; | |
1661 | } | |
1662 | if (c == NULL) { | |
1663 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); | |
1664 | packet_put_int(remote_id); | |
1665 | } else { | |
1666 | /* Send a confirmation to the remote host. */ | |
1667 | debug("Forwarding authentication connection."); | |
1668 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); | |
1669 | packet_put_int(remote_id); | |
1670 | packet_put_int(c->self); | |
1671 | } | |
1672 | packet_send(); | |
1673 | } | |
7368a6c8 | 1674 | |
396c147e | 1675 | static Channel * |
fa08c86b | 1676 | client_request_forwarded_tcpip(const char *request_type, int rchan) |
1677 | { | |
343288b8 | 1678 | Channel *c = NULL; |
fa08c86b | 1679 | char *listen_address, *originator_address; |
1680 | int listen_port, originator_port; | |
719fc62f | 1681 | int sock; |
fa08c86b | 1682 | |
1683 | /* Get rest of the packet */ | |
1684 | listen_address = packet_get_string(NULL); | |
1685 | listen_port = packet_get_int(); | |
1686 | originator_address = packet_get_string(NULL); | |
1687 | originator_port = packet_get_int(); | |
95500969 | 1688 | packet_check_eom(); |
fa08c86b | 1689 | |
1690 | debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d", | |
1691 | listen_address, listen_port, originator_address, originator_port); | |
1692 | ||
b3f8a79c | 1693 | sock = channel_connect_by_listen_address(listen_port); |
719fc62f | 1694 | if (sock < 0) { |
1695 | xfree(originator_address); | |
1696 | xfree(listen_address); | |
1697 | return NULL; | |
1698 | } | |
1699 | c = channel_new("forwarded-tcpip", | |
1700 | SSH_CHANNEL_CONNECTING, sock, sock, -1, | |
1701 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, | |
0d942eff | 1702 | originator_address, 1); |
fa08c86b | 1703 | xfree(originator_address); |
1704 | xfree(listen_address); | |
1705 | return c; | |
1706 | } | |
1707 | ||
343288b8 | 1708 | static Channel * |
fa08c86b | 1709 | client_request_x11(const char *request_type, int rchan) |
1710 | { | |
1711 | Channel *c = NULL; | |
1712 | char *originator; | |
1713 | int originator_port; | |
719fc62f | 1714 | int sock; |
fa08c86b | 1715 | |
1716 | if (!options.forward_x11) { | |
1717 | error("Warning: ssh server tried X11 forwarding."); | |
1908529f | 1718 | error("Warning: this is probably a break-in attempt by a malicious server."); |
fa08c86b | 1719 | return NULL; |
1720 | } | |
1721 | originator = packet_get_string(NULL); | |
1722 | if (datafellows & SSH_BUG_X11FWD) { | |
1723 | debug2("buggy server: x11 request w/o originator_port"); | |
1724 | originator_port = 0; | |
1725 | } else { | |
1726 | originator_port = packet_get_int(); | |
1727 | } | |
95500969 | 1728 | packet_check_eom(); |
fa08c86b | 1729 | /* XXX check permission */ |
865ac82e | 1730 | debug("client_request_x11: request from %s %d", originator, |
1731 | originator_port); | |
719fc62f | 1732 | xfree(originator); |
fa08c86b | 1733 | sock = x11_connect_display(); |
719fc62f | 1734 | if (sock < 0) |
1735 | return NULL; | |
1736 | c = channel_new("x11", | |
1737 | SSH_CHANNEL_X11_OPEN, sock, sock, -1, | |
0d942eff | 1738 | CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1); |
a5f82435 | 1739 | c->force_drain = 1; |
fa08c86b | 1740 | return c; |
1741 | } | |
1742 | ||
343288b8 | 1743 | static Channel * |
fa08c86b | 1744 | client_request_agent(const char *request_type, int rchan) |
1745 | { | |
1746 | Channel *c = NULL; | |
719fc62f | 1747 | int sock; |
fa08c86b | 1748 | |
1749 | if (!options.forward_agent) { | |
1750 | error("Warning: ssh server tried agent forwarding."); | |
1908529f | 1751 | error("Warning: this is probably a break-in attempt by a malicious server."); |
fa08c86b | 1752 | return NULL; |
1753 | } | |
1754 | sock = ssh_get_authentication_socket(); | |
719fc62f | 1755 | if (sock < 0) |
1756 | return NULL; | |
1757 | c = channel_new("authentication agent connection", | |
1758 | SSH_CHANNEL_OPEN, sock, sock, -1, | |
1759 | CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, | |
0d942eff | 1760 | "authentication agent connection", 1); |
a5f82435 | 1761 | c->force_drain = 1; |
fa08c86b | 1762 | return c; |
1763 | } | |
1764 | ||
0b242b12 | 1765 | /* XXXX move to generic input handler */ |
396c147e | 1766 | static void |
7819b5c3 | 1767 | client_input_channel_open(int type, u_int32_t seq, void *ctxt) |
0b242b12 | 1768 | { |
1769 | Channel *c = NULL; | |
1770 | char *ctype; | |
0b242b12 | 1771 | int rchan; |
f91d9a89 | 1772 | u_int rmaxpack, rwindow, len; |
0b242b12 | 1773 | |
1774 | ctype = packet_get_string(&len); | |
1775 | rchan = packet_get_int(); | |
1776 | rwindow = packet_get_int(); | |
1777 | rmaxpack = packet_get_int(); | |
1778 | ||
1d1ffb87 | 1779 | debug("client_input_channel_open: ctype %s rchan %d win %d max %d", |
0b242b12 | 1780 | ctype, rchan, rwindow, rmaxpack); |
1781 | ||
fa08c86b | 1782 | if (strcmp(ctype, "forwarded-tcpip") == 0) { |
1783 | c = client_request_forwarded_tcpip(ctype, rchan); | |
1784 | } else if (strcmp(ctype, "x11") == 0) { | |
1785 | c = client_request_x11(ctype, rchan); | |
1786 | } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { | |
1787 | c = client_request_agent(ctype, rchan); | |
0b242b12 | 1788 | } |
1789 | /* XXX duplicate : */ | |
1790 | if (c != NULL) { | |
1791 | debug("confirm %s", ctype); | |
1792 | c->remote_id = rchan; | |
1793 | c->remote_window = rwindow; | |
1794 | c->remote_maxpacket = rmaxpack; | |
a01a10dd | 1795 | if (c->type != SSH_CHANNEL_CONNECTING) { |
1796 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); | |
1797 | packet_put_int(c->remote_id); | |
1798 | packet_put_int(c->self); | |
1799 | packet_put_int(c->local_window); | |
1800 | packet_put_int(c->local_maxpacket); | |
1801 | packet_send(); | |
1802 | } | |
0b242b12 | 1803 | } else { |
1804 | debug("failure %s", ctype); | |
1805 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); | |
1806 | packet_put_int(rchan); | |
1807 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); | |
fbe90f7b | 1808 | if (!(datafellows & SSH_BUG_OPENFAILURE)) { |
a01a10dd | 1809 | packet_put_cstring("open failed"); |
fbe90f7b | 1810 | packet_put_cstring(""); |
1811 | } | |
0b242b12 | 1812 | packet_send(); |
1813 | } | |
1814 | xfree(ctype); | |
1815 | } | |
396c147e | 1816 | static void |
7819b5c3 | 1817 | client_input_channel_req(int type, u_int32_t seq, void *ctxt) |
1fc8ccdf | 1818 | { |
1819 | Channel *c = NULL; | |
5e96b616 | 1820 | int exitval, id, reply, success = 0; |
1fc8ccdf | 1821 | char *rtype; |
1822 | ||
1823 | id = packet_get_int(); | |
1824 | rtype = packet_get_string(NULL); | |
1825 | reply = packet_get_char(); | |
1826 | ||
1827 | debug("client_input_channel_req: channel %d rtype %s reply %d", | |
1828 | id, rtype, reply); | |
1829 | ||
13de3560 | 1830 | if (id == -1) { |
1831 | error("client_input_channel_req: request for channel -1"); | |
1832 | } else if ((c = channel_lookup(id)) == NULL) { | |
1fc8ccdf | 1833 | error("client_input_channel_req: channel %d: unknown channel", id); |
1834 | } else if (strcmp(rtype, "exit-status") == 0) { | |
5e96b616 | 1835 | exitval = packet_get_int(); |
1836 | if (id == session_ident) { | |
1837 | success = 1; | |
1838 | exit_status = exitval; | |
1839 | } else if (c->ctl_fd == -1) { | |
1840 | error("client_input_channel_req: unexpected channel %d", | |
1841 | session_ident); | |
1842 | } else { | |
1843 | atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval)); | |
1844 | success = 1; | |
1845 | } | |
95500969 | 1846 | packet_check_eom(); |
1fc8ccdf | 1847 | } |
1848 | if (reply) { | |
1849 | packet_start(success ? | |
1850 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); | |
13de3560 | 1851 | packet_put_int(id); |
1fc8ccdf | 1852 | packet_send(); |
1853 | } | |
1854 | xfree(rtype); | |
1855 | } | |
3d209bbe | 1856 | static void |
1857 | client_input_global_request(int type, u_int32_t seq, void *ctxt) | |
1858 | { | |
1859 | char *rtype; | |
1860 | int want_reply; | |
1861 | int success = 0; | |
1862 | ||
1863 | rtype = packet_get_string(NULL); | |
1864 | want_reply = packet_get_char(); | |
5d8d32a3 | 1865 | debug("client_input_global_request: rtype %s want_reply %d", |
1866 | rtype, want_reply); | |
3d209bbe | 1867 | if (want_reply) { |
1868 | packet_start(success ? | |
1869 | SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); | |
1870 | packet_send(); | |
1871 | packet_write_wait(); | |
1872 | } | |
1873 | xfree(rtype); | |
1874 | } | |
0b242b12 | 1875 | |
5e96b616 | 1876 | void |
f2107e97 | 1877 | client_session2_setup(int id, int want_tty, int want_subsystem, |
1786be35 | 1878 | const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env, |
5e96b616 | 1879 | dispatch_fn *subsys_repl) |
1880 | { | |
1881 | int len; | |
2c9a4d41 | 1882 | Channel *c = NULL; |
5e96b616 | 1883 | |
1884 | debug2("%s: id %d", __func__, id); | |
1885 | ||
2c9a4d41 | 1886 | if ((c = channel_lookup(id)) == NULL) |
1887 | fatal("client_session2_setup: channel %d: unknown channel", id); | |
1888 | ||
5e96b616 | 1889 | if (want_tty) { |
1890 | struct winsize ws; | |
1891 | struct termios tio; | |
1892 | ||
1893 | /* Store window size in the packet. */ | |
1894 | if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0) | |
1895 | memset(&ws, 0, sizeof(ws)); | |
1896 | ||
1897 | channel_request_start(id, "pty-req", 0); | |
1898 | packet_put_cstring(term != NULL ? term : ""); | |
148de80c | 1899 | packet_put_int((u_int)ws.ws_col); |
1900 | packet_put_int((u_int)ws.ws_row); | |
1901 | packet_put_int((u_int)ws.ws_xpixel); | |
1902 | packet_put_int((u_int)ws.ws_ypixel); | |
5e96b616 | 1903 | tio = get_saved_tio(); |
1904 | tty_make_modes(-1, tiop != NULL ? tiop : &tio); | |
1905 | packet_send(); | |
1906 | /* XXX wait for reply */ | |
2c9a4d41 | 1907 | c->client_tty = 1; |
5e96b616 | 1908 | } |
1909 | ||
1910 | /* Transfer any environment variables from client to server */ | |
1786be35 | 1911 | if (options.num_send_env != 0 && env != NULL) { |
5e96b616 | 1912 | int i, j, matched; |
5e96b616 | 1913 | char *name, *val; |
1914 | ||
1915 | debug("Sending environment."); | |
1786be35 | 1916 | for (i = 0; env[i] != NULL; i++) { |
5e96b616 | 1917 | /* Split */ |
1786be35 | 1918 | name = xstrdup(env[i]); |
5e96b616 | 1919 | if ((val = strchr(name, '=')) == NULL) { |
8c4bd764 | 1920 | xfree(name); |
5e96b616 | 1921 | continue; |
1922 | } | |
1923 | *val++ = '\0'; | |
1924 | ||
1925 | matched = 0; | |
1926 | for (j = 0; j < options.num_send_env; j++) { | |
1927 | if (match_pattern(name, options.send_env[j])) { | |
1928 | matched = 1; | |
1929 | break; | |
1930 | } | |
1931 | } | |
1932 | if (!matched) { | |
1933 | debug3("Ignored env %s", name); | |
8c4bd764 | 1934 | xfree(name); |
5e96b616 | 1935 | continue; |
1936 | } | |
1937 | ||
1938 | debug("Sending env %s = %s", name, val); | |
1939 | channel_request_start(id, "env", 0); | |
1940 | packet_put_cstring(name); | |
1941 | packet_put_cstring(val); | |
1942 | packet_send(); | |
8c4bd764 | 1943 | xfree(name); |
5e96b616 | 1944 | } |
1945 | } | |
1946 | ||
1947 | len = buffer_len(cmd); | |
1948 | if (len > 0) { | |
1949 | if (len > 900) | |
1950 | len = 900; | |
1951 | if (want_subsystem) { | |
1952 | debug("Sending subsystem: %.*s", len, (u_char*)buffer_ptr(cmd)); | |
1953 | channel_request_start(id, "subsystem", subsys_repl != NULL); | |
1954 | if (subsys_repl != NULL) { | |
1955 | /* register callback for reply */ | |
1956 | /* XXX we assume that client_loop has already been called */ | |
1957 | dispatch_set(SSH2_MSG_CHANNEL_FAILURE, subsys_repl); | |
1958 | dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, subsys_repl); | |
1959 | } | |
1960 | } else { | |
1961 | debug("Sending command: %.*s", len, (u_char*)buffer_ptr(cmd)); | |
1962 | channel_request_start(id, "exec", 0); | |
1963 | } | |
1964 | packet_put_string(buffer_ptr(cmd), buffer_len(cmd)); | |
1965 | packet_send(); | |
1966 | } else { | |
1967 | channel_request_start(id, "shell", 0); | |
1968 | packet_send(); | |
1969 | } | |
1970 | } | |
1971 | ||
396c147e | 1972 | static void |
5ca51e19 | 1973 | client_init_dispatch_20(void) |
8ce64345 | 1974 | { |
7a37c112 | 1975 | dispatch_init(&dispatch_protocol_error); |
e0ae8728 | 1976 | |
8ce64345 | 1977 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
1978 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); | |
1979 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); | |
1980 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); | |
0b242b12 | 1981 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); |
8ce64345 | 1982 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
1983 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); | |
1fc8ccdf | 1984 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req); |
8ce64345 | 1985 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
3d209bbe | 1986 | dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request); |
7a37c112 | 1987 | |
1988 | /* rekeying */ | |
1989 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); | |
e0ae8728 | 1990 | |
1991 | /* global request reply messages */ | |
1992 | dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply); | |
1993 | dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply); | |
8ce64345 | 1994 | } |
396c147e | 1995 | static void |
5ca51e19 | 1996 | client_init_dispatch_13(void) |
7368a6c8 | 1997 | { |
1998 | dispatch_init(NULL); | |
1999 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); | |
2000 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); | |
2001 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); | |
7368a6c8 | 2002 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
2003 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); | |
2004 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); | |
7368a6c8 | 2005 | dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status); |
2006 | dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data); | |
2007 | dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data); | |
a22aff1f | 2008 | |
2009 | dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ? | |
c15706e2 | 2010 | &client_input_agent_open : &deny_input_open); |
a22aff1f | 2011 | dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ? |
2012 | &x11_input_open : &deny_input_open); | |
7368a6c8 | 2013 | } |
396c147e | 2014 | static void |
5ca51e19 | 2015 | client_init_dispatch_15(void) |
7368a6c8 | 2016 | { |
2017 | client_init_dispatch_13(); | |
2018 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); | |
2019 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose); | |
2020 | } | |
eea46d13 | 2021 | static void |
5ca51e19 | 2022 | client_init_dispatch(void) |
7368a6c8 | 2023 | { |
8ce64345 | 2024 | if (compat20) |
2025 | client_init_dispatch_20(); | |
2026 | else if (compat13) | |
7368a6c8 | 2027 | client_init_dispatch_13(); |
2028 | else | |
2029 | client_init_dispatch_15(); | |
2030 | } | |
82de775c | 2031 | |
2032 | /* client specific fatal cleanup */ | |
2033 | void | |
2362db19 | 2034 | cleanup_exit(int i) |
82de775c | 2035 | { |
82de775c | 2036 | leave_raw_mode(); |
2037 | leave_non_blocking(); | |
5e96b616 | 2038 | if (options.control_path != NULL && control_fd != -1) |
2039 | unlink(options.control_path); | |
2362db19 | 2040 | _exit(i); |
82de775c | 2041 | } |