blob: 67fa36d911af79820b610f16a868d61d7b8d813b [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * clientloop.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
10 *
Damien Miller95def091999-11-25 00:26:21 +110011 * Created: Sat Sep 23 12:23:57 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100012 *
Damien Miller95def091999-11-25 00:26:21 +110013 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +100014 *
Damien Miller1383bd82000-04-06 12:32:37 +100015 * SSH2 support added by Markus Friedl.
Damien Miller95def091999-11-25 00:26:21 +110016 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
18#include "includes.h"
Damien Miller994cf142000-07-21 10:19:44 +100019RCSID("$OpenBSD: clientloop.c,v 1.29 2000/07/16 08:27:21 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
21#include "xmalloc.h"
22#include "ssh.h"
23#include "packet.h"
24#include "buffer.h"
Damien Miller5ce662a1999-11-11 17:57:39 +110025#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Miller1383bd82000-04-06 12:32:37 +100027#include "ssh2.h"
Damien Millerb38eff82000-04-01 11:09:21 +100028#include "compat.h"
29#include "channels.h"
30#include "dispatch.h"
31
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032/* Flag indicating that stdin should be redirected from /dev/null. */
33extern int stdin_null_flag;
34
Damien Miller5428f641999-11-25 11:54:57 +110035/*
36 * Name of the host we are connecting to. This is the name given on the
37 * command line, or the HostName specified for the user-supplied name in a
38 * configuration file.
39 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040extern char *host;
41
Damien Miller5428f641999-11-25 11:54:57 +110042/*
43 * Flag to indicate that we have received a window change signal which has
44 * not yet been processed. This will cause a message indicating the new
45 * window size to be sent to the server a little later. This is volatile
46 * because this is updated in a signal handler.
47 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048static volatile int received_window_change_signal = 0;
49
50/* Terminal modes, as saved by enter_raw_mode. */
51static struct termios saved_tio;
52
Damien Miller5428f641999-11-25 11:54:57 +110053/*
54 * Flag indicating whether we are in raw mode. This is used by
55 * enter_raw_mode and leave_raw_mode.
56 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057static int in_raw_mode = 0;
58
59/* Flag indicating whether the user\'s terminal is in non-blocking mode. */
60static int in_non_blocking_mode = 0;
61
62/* Common data for the client loop code. */
Damien Miller95def091999-11-25 00:26:21 +110063static int escape_pending; /* Last character was the escape character */
64static int last_was_cr; /* Last character was a newline. */
65static int exit_status; /* Used to store the exit status of the command. */
66static int stdin_eof; /* EOF has been encountered on standard error. */
67static Buffer stdin_buffer; /* Buffer for stdin data. */
68static Buffer stdout_buffer; /* Buffer for stdout data. */
69static Buffer stderr_buffer; /* Buffer for stderr data. */
70static unsigned int buffer_high;/* Soft max buffer size. */
71static int max_fd; /* Maximum file descriptor number in select(). */
72static int connection_in; /* Connection to server (input). */
73static int connection_out; /* Connection to server (output). */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
Damien Miller95def091999-11-25 00:26:21 +110075static int quit_pending; /* Set to non-zero to quit the client loop. */
76static int escape_char; /* Escape character. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Damien Miller1383bd82000-04-06 12:32:37 +100078
79void client_init_dispatch(void);
80int session_ident = -1;
81
Damien Miller5428f641999-11-25 11:54:57 +110082/* Returns the user\'s terminal to normal mode if it had been put in raw mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Damien Miller4af51302000-04-16 11:18:38 +100084void
Damien Miller95def091999-11-25 00:26:21 +110085leave_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086{
Damien Miller95def091999-11-25 00:26:21 +110087 if (!in_raw_mode)
88 return;
89 in_raw_mode = 0;
90 if (tcsetattr(fileno(stdin), TCSADRAIN, &saved_tio) < 0)
91 perror("tcsetattr");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller95def091999-11-25 00:26:21 +110093 fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094}
95
96/* Puts the user\'s terminal in raw mode. */
97
Damien Miller4af51302000-04-16 11:18:38 +100098void
Damien Miller95def091999-11-25 00:26:21 +110099enter_raw_mode()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100{
Damien Miller95def091999-11-25 00:26:21 +1100101 struct termios tio;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Miller95def091999-11-25 00:26:21 +1100103 if (tcgetattr(fileno(stdin), &tio) < 0)
104 perror("tcgetattr");
105 saved_tio = tio;
106 tio.c_iflag |= IGNPAR;
107 tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
108 tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109#ifdef IEXTEN
Damien Miller95def091999-11-25 00:26:21 +1100110 tio.c_lflag &= ~IEXTEN;
111#endif /* IEXTEN */
112 tio.c_oflag &= ~OPOST;
113 tio.c_cc[VMIN] = 1;
114 tio.c_cc[VTIME] = 0;
115 if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) < 0)
116 perror("tcsetattr");
117 in_raw_mode = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Damien Miller95def091999-11-25 00:26:21 +1100119 fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
120}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
122/* Restores stdin to blocking mode. */
123
Damien Miller4af51302000-04-16 11:18:38 +1000124void
Damien Miller95def091999-11-25 00:26:21 +1100125leave_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126{
Damien Miller95def091999-11-25 00:26:21 +1100127 if (in_non_blocking_mode) {
128 (void) fcntl(fileno(stdin), F_SETFL, 0);
129 in_non_blocking_mode = 0;
130 fatal_remove_cleanup((void (*) (void *)) leave_non_blocking, NULL);
131 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132}
133
Damien Miller95def091999-11-25 00:26:21 +1100134/* Puts stdin terminal in non-blocking mode. */
135
Damien Miller4af51302000-04-16 11:18:38 +1000136void
Damien Miller95def091999-11-25 00:26:21 +1100137enter_non_blocking()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138{
Damien Miller95def091999-11-25 00:26:21 +1100139 in_non_blocking_mode = 1;
140 (void) fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
141 fatal_add_cleanup((void (*) (void *)) leave_non_blocking, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * Signal handler for the window change signal (SIGWINCH). This just sets a
146 * flag indicating that the window has changed.
147 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller4af51302000-04-16 11:18:38 +1000149void
Damien Miller95def091999-11-25 00:26:21 +1100150window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151{
Damien Miller95def091999-11-25 00:26:21 +1100152 received_window_change_signal = 1;
153 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154}
155
Damien Miller5428f641999-11-25 11:54:57 +1100156/*
157 * Signal handler for signals that cause the program to terminate. These
158 * signals must be trapped to restore terminal modes.
159 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller4af51302000-04-16 11:18:38 +1000161void
Damien Miller95def091999-11-25 00:26:21 +1100162signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163{
Damien Miller95def091999-11-25 00:26:21 +1100164 if (in_raw_mode)
165 leave_raw_mode();
166 if (in_non_blocking_mode)
167 leave_non_blocking();
168 channel_stop_listening();
169 packet_close();
170 fatal("Killed by signal %d.", sig);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171}
172
Damien Miller5428f641999-11-25 11:54:57 +1100173/*
174 * Returns current time in seconds from Jan 1, 1970 with the maximum
175 * available resolution.
176 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177
Damien Miller4af51302000-04-16 11:18:38 +1000178double
Damien Miller95def091999-11-25 00:26:21 +1100179get_current_time()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180{
Damien Miller95def091999-11-25 00:26:21 +1100181 struct timeval tv;
182 gettimeofday(&tv, NULL);
183 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184}
185
Damien Miller5428f641999-11-25 11:54:57 +1100186/*
187 * This is called when the interactive is entered. This checks if there is
188 * an EOF coming on stdin. We must check this explicitly, as select() does
189 * not appear to wake up when redirecting from /dev/null.
190 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191
Damien Miller4af51302000-04-16 11:18:38 +1000192void
Damien Miller95def091999-11-25 00:26:21 +1100193client_check_initial_eof_on_stdin()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194{
Damien Miller95def091999-11-25 00:26:21 +1100195 int len;
196 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197
Damien Miller5428f641999-11-25 11:54:57 +1100198 /*
199 * If standard input is to be "redirected from /dev/null", we simply
200 * mark that we have seen an EOF and send an EOF message to the
201 * server. Otherwise, we try to read a single character; it appears
202 * that for some files, such /dev/null, select() never wakes up for
203 * read for this descriptor, which means that we never get EOF. This
204 * way we will get the EOF if stdin comes from /dev/null or similar.
205 */
Damien Miller95def091999-11-25 00:26:21 +1100206 if (stdin_null_flag) {
207 /* Fake EOF on stdin. */
208 debug("Sending eof.");
209 stdin_eof = 1;
210 packet_start(SSH_CMSG_EOF);
211 packet_send();
212 } else {
Damien Miller95def091999-11-25 00:26:21 +1100213 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Miller95def091999-11-25 00:26:21 +1100215 /* Check for immediate EOF on stdin. */
216 len = read(fileno(stdin), buf, 1);
217 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100218 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100219 debug("Sending eof.");
220 stdin_eof = 1;
221 packet_start(SSH_CMSG_EOF);
222 packet_send();
223 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100224 /*
225 * Got data. We must store the data in the buffer,
226 * and also process it as an escape character if
227 * appropriate.
228 */
Damien Miller95def091999-11-25 00:26:21 +1100229 if ((unsigned char) buf[0] == escape_char)
230 escape_pending = 1;
231 else {
232 buffer_append(&stdin_buffer, buf, 1);
233 stdin_bytes += 1;
234 }
235 }
Damien Miller95def091999-11-25 00:26:21 +1100236 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238}
239
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Damien Miller5428f641999-11-25 11:54:57 +1100241/*
242 * Make packets from buffered stdin data, and buffer them for sending to the
243 * connection.
244 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller4af51302000-04-16 11:18:38 +1000246void
Damien Miller95def091999-11-25 00:26:21 +1100247client_make_packets_from_stdin_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248{
Damien Miller95def091999-11-25 00:26:21 +1100249 unsigned int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Damien Miller95def091999-11-25 00:26:21 +1100251 /* Send buffered stdin data to the server. */
252 while (buffer_len(&stdin_buffer) > 0 &&
253 packet_not_very_much_data_to_write()) {
254 len = buffer_len(&stdin_buffer);
255 /* Keep the packets at reasonable size. */
256 if (len > packet_get_maxsize())
257 len = packet_get_maxsize();
258 packet_start(SSH_CMSG_STDIN_DATA);
259 packet_put_string(buffer_ptr(&stdin_buffer), len);
260 packet_send();
261 buffer_consume(&stdin_buffer, len);
262 /* If we have a pending EOF, send it now. */
263 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
264 packet_start(SSH_CMSG_EOF);
265 packet_send();
266 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268}
269
Damien Miller5428f641999-11-25 11:54:57 +1100270/*
271 * Checks if the client window has changed, and sends a packet about it to
272 * the server if so. The actual change is detected elsewhere (by a software
273 * interrupt on Unix); this just checks the flag and sends a message if
274 * appropriate.
275 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276
Damien Miller4af51302000-04-16 11:18:38 +1000277void
Damien Miller95def091999-11-25 00:26:21 +1100278client_check_window_change()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279{
Damien Miller1383bd82000-04-06 12:32:37 +1000280 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller1383bd82000-04-06 12:32:37 +1000282 if (! received_window_change_signal)
283 return;
284 /** XXX race */
285 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286
Damien Miller1383bd82000-04-06 12:32:37 +1000287 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
288 return;
289
290 debug("client_check_window_change: changed");
291
292 if (compat20) {
293 channel_request_start(session_ident, "window-change", 0);
294 packet_put_int(ws.ws_col);
295 packet_put_int(ws.ws_row);
296 packet_put_int(ws.ws_xpixel);
297 packet_put_int(ws.ws_ypixel);
298 packet_send();
299 } else {
300 packet_start(SSH_CMSG_WINDOW_SIZE);
301 packet_put_int(ws.ws_row);
302 packet_put_int(ws.ws_col);
303 packet_put_int(ws.ws_xpixel);
304 packet_put_int(ws.ws_ypixel);
305 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307}
308
Damien Miller5428f641999-11-25 11:54:57 +1100309/*
310 * Waits until the client can do something (some data becomes available on
311 * one of the file descriptors).
312 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313
Damien Miller4af51302000-04-16 11:18:38 +1000314void
Damien Miller95def091999-11-25 00:26:21 +1100315client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316{
Damien Miller1383bd82000-04-06 12:32:37 +1000317 /*debug("client_wait_until_can_do_something"); */
318
Damien Miller95def091999-11-25 00:26:21 +1100319 /* Initialize select masks. */
320 FD_ZERO(readset);
Damien Miller95def091999-11-25 00:26:21 +1100321 FD_ZERO(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller1383bd82000-04-06 12:32:37 +1000323 if (!compat20) {
324 /* Read from the connection, unless our buffers are full. */
325 if (buffer_len(&stdout_buffer) < buffer_high &&
326 buffer_len(&stderr_buffer) < buffer_high &&
327 channel_not_very_much_buffered_data())
328 FD_SET(connection_in, readset);
329 /*
330 * Read from stdin, unless we have seen EOF or have very much
331 * buffered data to send to the server.
332 */
333 if (!stdin_eof && packet_not_very_much_data_to_write())
334 FD_SET(fileno(stdin), readset);
335
336 /* Select stdout/stderr if have data in buffer. */
337 if (buffer_len(&stdout_buffer) > 0)
338 FD_SET(fileno(stdout), writeset);
339 if (buffer_len(&stderr_buffer) > 0)
340 FD_SET(fileno(stderr), writeset);
341 } else {
342 FD_SET(connection_in, readset);
343 }
344
Damien Miller95def091999-11-25 00:26:21 +1100345 /* Add any selections by the channel mechanism. */
346 channel_prepare_select(readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347
Damien Miller95def091999-11-25 00:26:21 +1100348 /* Select server connection if have data to write to the server. */
349 if (packet_have_data_to_write())
350 FD_SET(connection_out, writeset);
351
Damien Miller1383bd82000-04-06 12:32:37 +1000352/* move UP XXX */
Damien Miller95def091999-11-25 00:26:21 +1100353 /* Update maximum file descriptor number, if appropriate. */
354 if (channel_max_fd() > max_fd)
355 max_fd = channel_max_fd();
356
Damien Miller5428f641999-11-25 11:54:57 +1100357 /*
358 * Wait for something to happen. This will suspend the process until
359 * some selected descriptor can be read, written, or has some other
360 * event pending. Note: if you want to implement SSH_MSG_IGNORE
361 * messages to fool traffic analysis, this might be the place to do
362 * it: just have a random timeout for the select, and send a random
363 * SSH_MSG_IGNORE packet when the timeout expires.
364 */
Damien Miller95def091999-11-25 00:26:21 +1100365
366 if (select(max_fd + 1, readset, writeset, NULL, NULL) < 0) {
367 char buf[100];
368 /* Some systems fail to clear these automatically. */
369 FD_ZERO(readset);
370 FD_ZERO(writeset);
371 if (errno == EINTR)
372 return;
373 /* Note: we might still have data in the buffers. */
374 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
375 buffer_append(&stderr_buffer, buf, strlen(buf));
376 stderr_bytes += strlen(buf);
377 quit_pending = 1;
378 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379}
380
Damien Miller4af51302000-04-16 11:18:38 +1000381void
Damien Miller95def091999-11-25 00:26:21 +1100382client_suspend_self()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383{
Damien Miller95def091999-11-25 00:26:21 +1100384 struct winsize oldws, newws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385
Damien Miller95def091999-11-25 00:26:21 +1100386 /* Flush stdout and stderr buffers. */
387 if (buffer_len(&stdout_buffer) > 0)
Damien Miller037a0dc1999-12-07 15:38:31 +1100388 atomicio(write, fileno(stdout), buffer_ptr(&stdout_buffer),
389 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100390 if (buffer_len(&stderr_buffer) > 0)
Damien Miller037a0dc1999-12-07 15:38:31 +1100391 atomicio(write, fileno(stderr), buffer_ptr(&stderr_buffer),
392 buffer_len(&stderr_buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393
Damien Miller95def091999-11-25 00:26:21 +1100394 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller5428f641999-11-25 11:54:57 +1100396 /*
397 * Free (and clear) the buffer to reduce the amount of data that gets
398 * written to swap.
399 */
Damien Miller95def091999-11-25 00:26:21 +1100400 buffer_free(&stdin_buffer);
401 buffer_free(&stdout_buffer);
402 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403
Damien Miller95def091999-11-25 00:26:21 +1100404 /* Save old window size. */
405 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
Damien Miller95def091999-11-25 00:26:21 +1100407 /* Send the suspend signal to the program itself. */
408 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409
Damien Miller95def091999-11-25 00:26:21 +1100410 /* Check if the window size has changed. */
411 if (ioctl(fileno(stdin), TIOCGWINSZ, &newws) >= 0 &&
412 (oldws.ws_row != newws.ws_row ||
413 oldws.ws_col != newws.ws_col ||
414 oldws.ws_xpixel != newws.ws_xpixel ||
415 oldws.ws_ypixel != newws.ws_ypixel))
416 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller95def091999-11-25 00:26:21 +1100418 /* OK, we have been continued by the user. Reinitialize buffers. */
419 buffer_init(&stdin_buffer);
420 buffer_init(&stdout_buffer);
421 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Damien Miller95def091999-11-25 00:26:21 +1100423 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424}
425
Damien Miller4af51302000-04-16 11:18:38 +1000426void
Damien Miller1383bd82000-04-06 12:32:37 +1000427client_process_net_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428{
Damien Miller1383bd82000-04-06 12:32:37 +1000429 int len;
430 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Damien Miller5428f641999-11-25 11:54:57 +1100432 /*
433 * Read input from the server, and add any such data to the buffer of
434 * the packet subsystem.
435 */
Damien Miller95def091999-11-25 00:26:21 +1100436 if (FD_ISSET(connection_in, readset)) {
437 /* Read as much as possible. */
438 len = read(connection_in, buf, sizeof(buf));
Damien Miller1383bd82000-04-06 12:32:37 +1000439/*debug("read connection_in len %d", len); XXX */
Damien Miller95def091999-11-25 00:26:21 +1100440 if (len == 0) {
441 /* Received EOF. The remote host has closed the connection. */
442 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
443 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444 buffer_append(&stderr_buffer, buf, strlen(buf));
445 stderr_bytes += strlen(buf);
446 quit_pending = 1;
447 return;
Damien Miller95def091999-11-25 00:26:21 +1100448 }
Damien Miller5428f641999-11-25 11:54:57 +1100449 /*
450 * There is a kernel bug on Solaris that causes select to
451 * sometimes wake up even though there is no data available.
452 */
Damien Miller95def091999-11-25 00:26:21 +1100453 if (len < 0 && errno == EAGAIN)
454 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455
Damien Miller95def091999-11-25 00:26:21 +1100456 if (len < 0) {
457 /* An error has encountered. Perhaps there is a network problem. */
458 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
459 host, strerror(errno));
460 buffer_append(&stderr_buffer, buf, strlen(buf));
461 stderr_bytes += strlen(buf);
462 quit_pending = 1;
463 return;
464 }
465 packet_process_incoming(buf, len);
466 }
Damien Miller1383bd82000-04-06 12:32:37 +1000467}
468
Damien Miller4af51302000-04-16 11:18:38 +1000469void
Damien Miller1383bd82000-04-06 12:32:37 +1000470client_process_input(fd_set * readset)
471{
Damien Miller166fca82000-04-20 07:42:21 +1000472 int len;
473 pid_t pid;
Damien Miller1383bd82000-04-06 12:32:37 +1000474 char buf[8192], *s;
475
Damien Miller95def091999-11-25 00:26:21 +1100476 /* Read input from stdin. */
477 if (FD_ISSET(fileno(stdin), readset)) {
478 /* Read as much as possible. */
479 len = read(fileno(stdin), buf, sizeof(buf));
480 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100481 /*
482 * Received EOF or error. They are treated
483 * similarly, except that an error message is printed
484 * if it was an error condition.
485 */
Damien Miller95def091999-11-25 00:26:21 +1100486 if (len < 0) {
487 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
488 buffer_append(&stderr_buffer, buf, strlen(buf));
489 stderr_bytes += strlen(buf);
490 }
491 /* Mark that we have seen EOF. */
492 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +1100493 /*
494 * Send an EOF message to the server unless there is
495 * data in the buffer. If there is data in the
496 * buffer, no message will be sent now. Code
497 * elsewhere will send the EOF when the buffer
498 * becomes empty if stdin_eof is set.
499 */
Damien Miller95def091999-11-25 00:26:21 +1100500 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501 packet_start(SSH_CMSG_EOF);
502 packet_send();
Damien Miller95def091999-11-25 00:26:21 +1100503 }
504 } else if (escape_char == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100505 /*
506 * Normal successful read, and no escape character.
507 * Just append the data to buffer.
508 */
Damien Miller95def091999-11-25 00:26:21 +1100509 buffer_append(&stdin_buffer, buf, len);
510 stdin_bytes += len;
511 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100512 /*
513 * Normal, successful read. But we have an escape character
514 * and have to process the characters one by one.
515 */
Damien Miller95def091999-11-25 00:26:21 +1100516 unsigned int i;
517 for (i = 0; i < len; i++) {
518 unsigned char ch;
519 /* Get one character at a time. */
520 ch = buf[i];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Miller95def091999-11-25 00:26:21 +1100522 if (escape_pending) {
523 /* We have previously seen an escape character. */
524 /* Clear the flag now. */
525 escape_pending = 0;
526 /* Process the escaped character. */
527 switch (ch) {
528 case '.':
529 /* Terminate the connection. */
530 snprintf(buf, sizeof buf, "%c.\r\n", escape_char);
531 buffer_append(&stderr_buffer, buf, strlen(buf));
532 stderr_bytes += strlen(buf);
533 quit_pending = 1;
534 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535
Damien Miller95def091999-11-25 00:26:21 +1100536 case 'Z' - 64:
537 /* Suspend the program. */
538 /* Print a message to that effect to the user. */
539 snprintf(buf, sizeof buf, "%c^Z\r\n", escape_char);
540 buffer_append(&stderr_buffer, buf, strlen(buf));
541 stderr_bytes += strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542
Damien Miller95def091999-11-25 00:26:21 +1100543 /* Restore terminal modes and suspend. */
544 client_suspend_self();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545
Damien Miller95def091999-11-25 00:26:21 +1100546 /* We have been continued. */
547 continue;
548
549 case '&':
Damien Miller5428f641999-11-25 11:54:57 +1100550 /*
551 * Detach the program (continue to serve connections,
552 * but put in background and no more new connections).
553 */
Damien Miller95def091999-11-25 00:26:21 +1100554 if (!stdin_eof) {
Damien Miller5428f641999-11-25 11:54:57 +1100555 /*
556 * Sending SSH_CMSG_EOF alone does not always appear
557 * to be enough. So we try to send an EOF character
558 * first.
559 */
Damien Miller95def091999-11-25 00:26:21 +1100560 packet_start(SSH_CMSG_STDIN_DATA);
561 packet_put_string("\004", 1);
562 packet_send();
563 /* Close stdin. */
564 stdin_eof = 1;
565 if (buffer_len(&stdin_buffer) == 0) {
566 packet_start(SSH_CMSG_EOF);
567 packet_send();
568 }
569 }
570 /* Restore tty modes. */
571 leave_raw_mode();
572
573 /* Stop listening for new connections. */
574 channel_stop_listening();
575
576 printf("%c& [backgrounded]\n", escape_char);
577
578 /* Fork into background. */
579 pid = fork();
580 if (pid < 0) {
581 error("fork: %.100s", strerror(errno));
582 continue;
583 }
584 if (pid != 0) { /* This is the parent. */
585 /* The parent just exits. */
586 exit(0);
587 }
588 /* The child continues serving connections. */
589 continue;
590
591 case '?':
592 snprintf(buf, sizeof buf,
593"%c?\r\n\
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594Supported escape sequences:\r\n\
595~. - terminate connection\r\n\
596~^Z - suspend ssh\r\n\
597~# - list forwarded connections\r\n\
598~& - background ssh (when waiting for connections to terminate)\r\n\
599~? - this message\r\n\
600~~ - send the escape character by typing it twice\r\n\
601(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller95def091999-11-25 00:26:21 +1100602 escape_char);
603 buffer_append(&stderr_buffer, buf, strlen(buf));
604 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Damien Miller95def091999-11-25 00:26:21 +1100606 case '#':
607 snprintf(buf, sizeof buf, "%c#\r\n", escape_char);
608 buffer_append(&stderr_buffer, buf, strlen(buf));
609 s = channel_open_message();
610 buffer_append(&stderr_buffer, s, strlen(s));
611 xfree(s);
612 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller95def091999-11-25 00:26:21 +1100614 default:
615 if (ch != escape_char) {
Damien Miller5428f641999-11-25 11:54:57 +1100616 /*
617 * Escape character followed by non-special character.
618 * Append both to the input buffer.
619 */
Damien Miller95def091999-11-25 00:26:21 +1100620 buf[0] = escape_char;
621 buf[1] = ch;
622 buffer_append(&stdin_buffer, buf, 2);
623 stdin_bytes += 2;
624 continue;
625 }
Damien Miller5428f641999-11-25 11:54:57 +1100626 /*
627 * Note that escape character typed twice
628 * falls through here; the latter gets processed
629 * as a normal character below.
630 */
Damien Miller95def091999-11-25 00:26:21 +1100631 break;
632 }
633 } else {
Damien Miller5428f641999-11-25 11:54:57 +1100634 /*
635 * The previous character was not an escape char. Check if this
636 * is an escape.
637 */
Damien Miller95def091999-11-25 00:26:21 +1100638 if (last_was_cr && ch == escape_char) {
639 /* It is. Set the flag and continue to next character. */
640 escape_pending = 1;
641 continue;
642 }
643 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644
Damien Miller5428f641999-11-25 11:54:57 +1100645 /*
646 * Normal character. Record whether it was a newline,
647 * and append it to the buffer.
648 */
Damien Miller95def091999-11-25 00:26:21 +1100649 last_was_cr = (ch == '\r' || ch == '\n');
650 buf[0] = ch;
651 buffer_append(&stdin_buffer, buf, 1);
652 stdin_bytes += 1;
653 continue;
654 }
655 }
656 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000657}
658
Damien Miller4af51302000-04-16 11:18:38 +1000659void
Damien Miller95def091999-11-25 00:26:21 +1100660client_process_output(fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661{
Damien Miller95def091999-11-25 00:26:21 +1100662 int len;
663 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664
Damien Miller95def091999-11-25 00:26:21 +1100665 /* Write buffered output to stdout. */
666 if (FD_ISSET(fileno(stdout), writeset)) {
667 /* Write as much data as possible. */
668 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100669 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100670 if (len <= 0) {
671 if (errno == EAGAIN)
672 len = 0;
673 else {
Damien Miller5428f641999-11-25 11:54:57 +1100674 /*
675 * An error or EOF was encountered. Put an
676 * error message to stderr buffer.
677 */
Damien Miller95def091999-11-25 00:26:21 +1100678 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
679 buffer_append(&stderr_buffer, buf, strlen(buf));
680 stderr_bytes += strlen(buf);
681 quit_pending = 1;
682 return;
683 }
684 }
685 /* Consume printed data from the buffer. */
686 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687 }
Damien Miller95def091999-11-25 00:26:21 +1100688 /* Write buffered output to stderr. */
689 if (FD_ISSET(fileno(stderr), writeset)) {
690 /* Write as much data as possible. */
691 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100692 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100693 if (len <= 0) {
694 if (errno == EAGAIN)
695 len = 0;
696 else {
Damien Miller5428f641999-11-25 11:54:57 +1100697 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +1100698 quit_pending = 1;
699 return;
700 }
701 }
702 /* Consume printed characters from the buffer. */
703 buffer_consume(&stderr_buffer, len);
704 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705}
706
Damien Miller5428f641999-11-25 11:54:57 +1100707/*
Damien Millerb38eff82000-04-01 11:09:21 +1000708 * Get packets from the connection input buffer, and process them as long as
709 * there are packets available.
710 *
711 * Any unknown packets received during the actual
712 * session cause the session to terminate. This is
713 * intended to make debugging easier since no
714 * confirmations are sent. Any compatible protocol
715 * extensions must be negotiated during the
716 * preparatory phase.
717 */
718
Damien Miller4af51302000-04-16 11:18:38 +1000719void
Damien Millerb38eff82000-04-01 11:09:21 +1000720client_process_buffered_input_packets()
721{
722 dispatch_run(DISPATCH_NONBLOCK, &quit_pending);
723}
724
725/*
Damien Miller5428f641999-11-25 11:54:57 +1100726 * Implements the interactive session with the server. This is called after
727 * the user has been authenticated, and a command has been started on the
728 * remote host. If escape_char != -1, it is the character used as an escape
729 * character for terminating or suspending the session.
730 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731
Damien Miller4af51302000-04-16 11:18:38 +1000732int
Damien Miller95def091999-11-25 00:26:21 +1100733client_loop(int have_pty, int escape_char_arg)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734{
Damien Miller95def091999-11-25 00:26:21 +1100735 extern Options options;
736 double start_time, total_time;
737 int len;
738 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739
Damien Miller95def091999-11-25 00:26:21 +1100740 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741
Damien Miller95def091999-11-25 00:26:21 +1100742 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743
Damien Miller95def091999-11-25 00:26:21 +1100744 /* Initialize variables. */
745 escape_pending = 0;
746 last_was_cr = 1;
747 exit_status = -1;
748 stdin_eof = 0;
749 buffer_high = 64 * 1024;
750 connection_in = packet_get_connection_in();
751 connection_out = packet_get_connection_out();
752 max_fd = connection_in;
753 if (connection_out > max_fd)
754 max_fd = connection_out;
755 stdin_bytes = 0;
756 stdout_bytes = 0;
757 stderr_bytes = 0;
758 quit_pending = 0;
759 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000760
Damien Miller95def091999-11-25 00:26:21 +1100761 /* Initialize buffers. */
762 buffer_init(&stdin_buffer);
763 buffer_init(&stdout_buffer);
764 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765
Damien Millerb38eff82000-04-01 11:09:21 +1000766 client_init_dispatch();
767
Damien Miller95def091999-11-25 00:26:21 +1100768 /* Set signal handlers to restore non-blocking mode. */
769 signal(SIGINT, signal_handler);
770 signal(SIGQUIT, signal_handler);
771 signal(SIGTERM, signal_handler);
772 signal(SIGPIPE, SIG_IGN);
773 if (have_pty)
774 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775
Damien Miller95def091999-11-25 00:26:21 +1100776 if (have_pty)
777 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Damien Millerbe484b52000-07-15 14:14:16 +1000779 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +1000780 if (!compat20)
781 client_check_initial_eof_on_stdin();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782
Damien Miller95def091999-11-25 00:26:21 +1100783 /* Main loop of the client for the interactive session mode. */
784 while (!quit_pending) {
785 fd_set readset, writeset;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786
Damien Miller5428f641999-11-25 11:54:57 +1100787 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +1100788 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789
Damien Miller1383bd82000-04-06 12:32:37 +1000790 if (compat20 && !channel_still_open()) {
791 debug("!channel_still_open.");
792 break;
793 }
794
Damien Miller5428f641999-11-25 11:54:57 +1100795 /*
796 * Make packets of buffered stdin data, and buffer them for
797 * sending to the server.
798 */
Damien Miller1383bd82000-04-06 12:32:37 +1000799 if (!compat20)
800 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801
Damien Miller5428f641999-11-25 11:54:57 +1100802 /*
803 * Make packets from buffered channel data, and buffer them
804 * for sending to the server.
805 */
Damien Miller95def091999-11-25 00:26:21 +1100806 if (packet_not_very_much_data_to_write())
807 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
Damien Miller5428f641999-11-25 11:54:57 +1100809 /*
810 * Check if the window size has changed, and buffer a message
811 * about it to the server if so.
812 */
Damien Miller95def091999-11-25 00:26:21 +1100813 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814
Damien Miller95def091999-11-25 00:26:21 +1100815 if (quit_pending)
816 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817
Damien Miller5428f641999-11-25 11:54:57 +1100818 /*
819 * Wait until we have something to do (something becomes
820 * available on one of the descriptors).
821 */
Damien Miller95def091999-11-25 00:26:21 +1100822 client_wait_until_can_do_something(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823
Damien Miller95def091999-11-25 00:26:21 +1100824 if (quit_pending)
825 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Damien Miller95def091999-11-25 00:26:21 +1100827 /* Do channel operations. */
828 channel_after_select(&readset, &writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829
Damien Miller1383bd82000-04-06 12:32:37 +1000830 /* Buffer input from the connection. */
831 client_process_net_input(&readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832
Damien Miller1383bd82000-04-06 12:32:37 +1000833 if (quit_pending)
834 break;
835
836 if (!compat20) {
837 /* Buffer data from stdin */
838 client_process_input(&readset);
839 /*
840 * Process output to stdout and stderr. Output to
841 * the connection is processed elsewhere (above).
842 */
843 client_process_output(&writeset);
844 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845
Damien Miller5428f641999-11-25 11:54:57 +1100846 /* Send as much buffered packet data as possible to the sender. */
Damien Miller95def091999-11-25 00:26:21 +1100847 if (FD_ISSET(connection_out, &writeset))
848 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850
Damien Miller95def091999-11-25 00:26:21 +1100851 /* Terminate the session. */
852
853 /* Stop watching for window change. */
854 if (have_pty)
855 signal(SIGWINCH, SIG_DFL);
856
857 /* Stop listening for connections. */
858 channel_stop_listening();
859
Damien Miller5428f641999-11-25 11:54:57 +1100860 /*
861 * In interactive mode (with pseudo tty) display a message indicating
862 * that the connection has been closed.
863 */
Damien Miller95def091999-11-25 00:26:21 +1100864 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
865 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
866 buffer_append(&stderr_buffer, buf, strlen(buf));
867 stderr_bytes += strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 }
Damien Miller95def091999-11-25 00:26:21 +1100869 /* Output any buffered data for stdout. */
870 while (buffer_len(&stdout_buffer) > 0) {
871 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100872 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100873 if (len <= 0) {
874 error("Write failed flushing stdout buffer.");
875 break;
876 }
877 buffer_consume(&stdout_buffer, len);
878 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
Damien Miller95def091999-11-25 00:26:21 +1100880 /* Output any buffered data for stderr. */
881 while (buffer_len(&stderr_buffer) > 0) {
882 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100883 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +1100884 if (len <= 0) {
885 error("Write failed flushing stderr buffer.");
886 break;
887 }
888 buffer_consume(&stderr_buffer, len);
889 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890
Damien Miller95def091999-11-25 00:26:21 +1100891 if (have_pty)
892 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller95def091999-11-25 00:26:21 +1100894 /* Clear and free any buffers. */
895 memset(buf, 0, sizeof(buf));
896 buffer_free(&stdin_buffer);
897 buffer_free(&stdout_buffer);
898 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Report bytes transferred, and transfer rates. */
901 total_time = get_current_time() - start_time;
902 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
903 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
904 if (total_time > 0)
905 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
906 stdin_bytes / total_time, stdout_bytes / total_time,
907 stderr_bytes / total_time);
908
909 /* Return the exit status of the program. */
910 debug("Exit status %d", exit_status);
911 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000912}
Damien Millerb38eff82000-04-01 11:09:21 +1000913
914/*********/
915
916void
917client_input_stdout_data(int type, int plen)
918{
919 unsigned int data_len;
920 char *data = packet_get_string(&data_len);
921 packet_integrity_check(plen, 4 + data_len, type);
922 buffer_append(&stdout_buffer, data, data_len);
923 stdout_bytes += data_len;
924 memset(data, 0, data_len);
925 xfree(data);
926}
927void
928client_input_stderr_data(int type, int plen)
929{
930 unsigned int data_len;
931 char *data = packet_get_string(&data_len);
932 packet_integrity_check(plen, 4 + data_len, type);
933 buffer_append(&stderr_buffer, data, data_len);
934 stdout_bytes += data_len;
935 memset(data, 0, data_len);
936 xfree(data);
937}
938void
939client_input_exit_status(int type, int plen)
940{
941 packet_integrity_check(plen, 4, type);
942 exit_status = packet_get_int();
943 /* Acknowledge the exit. */
944 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
945 packet_send();
946 /*
947 * Must wait for packet to be sent since we are
948 * exiting the loop.
949 */
950 packet_write_wait();
951 /* Flag that we want to exit. */
952 quit_pending = 1;
953}
954
Damien Millerbd483e72000-04-30 10:00:53 +1000955/* XXXX move to generic input handler */
956void
957client_input_channel_open(int type, int plen)
958{
959 Channel *c = NULL;
960 char *ctype;
961 int id;
962 unsigned int len;
963 int rchan;
964 int rmaxpack;
965 int rwindow;
966
967 ctype = packet_get_string(&len);
968 rchan = packet_get_int();
969 rwindow = packet_get_int();
970 rmaxpack = packet_get_int();
971
Damien Millere247cc42000-05-07 12:03:14 +1000972 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +1000973 ctype, rchan, rwindow, rmaxpack);
974
975 if (strcmp(ctype, "x11") == 0) {
976 int sock;
977 char *originator;
978 int originator_port;
979 originator = packet_get_string(NULL);
Damien Miller30c3d422000-05-09 11:02:59 +1000980 if (datafellows & SSH_BUG_X11FWD) {
Damien Miller6d488712000-05-08 13:44:52 +1000981 debug("buggy server: x11 request w/o originator_port");
982 originator_port = 0;
Damien Miller30c3d422000-05-09 11:02:59 +1000983 } else {
984 originator_port = packet_get_int();
Damien Miller6d488712000-05-08 13:44:52 +1000985 }
Damien Millerbd483e72000-04-30 10:00:53 +1000986 packet_done();
987 /* XXX check permission */
988 xfree(originator);
989 /* XXX move to channels.c */
990 sock = x11_connect_display();
991 if (sock >= 0) {
992 id = channel_new("x11", SSH_CHANNEL_X11_OPEN,
993 sock, sock, -1, 4*1024, 32*1024, 0,
994 xstrdup("x11"));
995 c = channel_lookup(id);
996 }
997 }
998/* XXX duplicate : */
999 if (c != NULL) {
1000 debug("confirm %s", ctype);
1001 c->remote_id = rchan;
1002 c->remote_window = rwindow;
1003 c->remote_maxpacket = rmaxpack;
1004
1005 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1006 packet_put_int(c->remote_id);
1007 packet_put_int(c->self);
1008 packet_put_int(c->local_window);
1009 packet_put_int(c->local_maxpacket);
1010 packet_send();
1011 } else {
1012 debug("failure %s", ctype);
1013 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1014 packet_put_int(rchan);
1015 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1016 packet_put_cstring("bla bla");
1017 packet_put_cstring("");
1018 packet_send();
1019 }
1020 xfree(ctype);
1021}
1022
Damien Miller4af51302000-04-16 11:18:38 +10001023void
Damien Miller1383bd82000-04-06 12:32:37 +10001024client_init_dispatch_20()
1025{
1026 dispatch_init(&dispatch_protocol_error);
1027 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1028 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1029 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1030 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001031 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001032 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1033 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1034 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
1035 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1036}
Damien Miller4af51302000-04-16 11:18:38 +10001037void
Damien Millerb38eff82000-04-01 11:09:21 +10001038client_init_dispatch_13()
1039{
1040 dispatch_init(NULL);
1041 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
1042 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
1043 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001044 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1045 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1046 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
1047 dispatch_set(SSH_SMSG_AGENT_OPEN, &auth_input_open_request);
1048 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
1049 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
1050 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
1051 dispatch_set(SSH_SMSG_X11_OPEN, &x11_input_open);
1052}
Damien Miller4af51302000-04-16 11:18:38 +10001053void
Damien Millerb38eff82000-04-01 11:09:21 +10001054client_init_dispatch_15()
1055{
1056 client_init_dispatch_13();
1057 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
1058 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
1059}
Damien Miller4af51302000-04-16 11:18:38 +10001060void
Damien Millerb38eff82000-04-01 11:09:21 +10001061client_init_dispatch()
1062{
Damien Miller1383bd82000-04-06 12:32:37 +10001063 if (compat20)
1064 client_init_dispatch_20();
1065 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001066 client_init_dispatch_13();
1067 else
1068 client_init_dispatch_15();
1069}
Damien Miller1383bd82000-04-06 12:32:37 +10001070
1071void
1072client_input_channel_req(int id, void *arg)
1073{
1074 Channel *c = NULL;
1075 unsigned int len;
1076 int success = 0;
1077 int reply;
1078 char *rtype;
1079
1080 rtype = packet_get_string(&len);
1081 reply = packet_get_char();
1082
Damien Millere247cc42000-05-07 12:03:14 +10001083 debug("client_input_channel_req: rtype %s reply %d", rtype, reply);
Damien Miller1383bd82000-04-06 12:32:37 +10001084
1085 c = channel_lookup(id);
1086 if (c == NULL)
1087 fatal("session_input_channel_req: channel %d: bad channel", id);
1088
1089 if (session_ident == -1) {
1090 error("client_input_channel_req: no channel %d", id);
1091 } else if (id != session_ident) {
1092 error("client_input_channel_req: bad channel %d != %d",
1093 id, session_ident);
1094 } else if (strcmp(rtype, "exit-status") == 0) {
1095 success = 1;
1096 exit_status = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001097 packet_done();
Damien Miller1383bd82000-04-06 12:32:37 +10001098 }
1099 if (reply) {
1100 packet_start(success ?
1101 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1102 packet_put_int(c->remote_id);
1103 packet_send();
1104 }
1105 xfree(rtype);
1106}
1107
1108void
1109client_set_session_ident(int id)
1110{
1111 debug("client_set_session_ident: id %d", id);
1112 session_ident = id;
1113 channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
1114 client_input_channel_req, (void *)0);
1115}