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