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