blob: edd80144007c989a99b0dccdc9d64bc0ab21ddc3 [file] [log] [blame]
Damien Millerb84886b2008-05-19 15:05:07 +10001/* $OpenBSD: clientloop.c,v 1.189 2008/05/08 12:02:23 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The main loop for the interactive session (client side).
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
15 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
Damien Miller1383bd82000-04-06 12:32:37 +100038 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000039 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110040 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110063
Damien Millerf17883e2006-03-15 11:45:54 +110064#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100065#include <sys/ioctl.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100066#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110067#ifdef HAVE_SYS_STAT_H
68# include <sys/stat.h>
69#endif
Damien Miller9aec9192006-08-05 10:57:45 +100070#ifdef HAVE_SYS_TIME_H
71# include <sys/time.h>
72#endif
Damien Millere3b60b52006-07-10 21:08:03 +100073#include <sys/socket.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110074
Damien Millerc7b06362006-03-15 11:53:45 +110075#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100076#include <errno.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110077#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110078#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110079#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110080#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100081#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100082#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100083#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100084#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110085#include <termios.h>
Damien Millerd7834352006-08-05 12:39:39 +100086#include <pwd.h>
Damien Millere6b3b612006-07-24 14:01:23 +100087#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerb84886b2008-05-19 15:05:07 +100089#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "ssh1.h"
93#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094#include "packet.h"
95#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100096#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000097#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100098#include "dispatch.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110099#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +1000100#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000101#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000102#include "log.h"
103#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000104#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100105#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000106#include "authfd.h"
107#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000108#include "sshpty.h"
Ben Lindstrom302ea6f2001-04-16 02:01:25 +0000109#include "misc.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000110#include "monitor_fdpass.h"
111#include "match.h"
112#include "msg.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100113
114/* import options */
115extern Options options;
116
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117/* Flag indicating that stdin should be redirected from /dev/null. */
118extern int stdin_null_flag;
119
Damien Millerd6965512003-12-17 16:31:53 +1100120/* Flag indicating that no shell has been requested */
121extern int no_shell_flag;
122
Damien Miller0e220db2004-06-15 10:34:08 +1000123/* Control socket */
124extern int control_fd;
125
Damien Miller5428f641999-11-25 11:54:57 +1100126/*
127 * Name of the host we are connecting to. This is the name given on the
128 * command line, or the HostName specified for the user-supplied name in a
129 * configuration file.
130 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131extern char *host;
132
Damien Miller5428f641999-11-25 11:54:57 +1100133/*
134 * Flag to indicate that we have received a window change signal which has
135 * not yet been processed. This will cause a message indicating the new
136 * window size to be sent to the server a little later. This is volatile
137 * because this is updated in a signal handler.
138 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000139static volatile sig_atomic_t received_window_change_signal = 0;
140static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller788f2122005-11-05 15:14:59 +1100142/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143static int in_non_blocking_mode = 0;
144
145/* Common data for the client loop code. */
Damien Miller97c91f62006-04-23 12:08:37 +1000146static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Damien Millerad833b32000-08-23 10:46:23 +1000147static int escape_char; /* Escape character. */
Damien Miller95def091999-11-25 00:26:21 +1100148static int escape_pending; /* Last character was the escape character */
149static int last_was_cr; /* Last character was a newline. */
150static int exit_status; /* Used to store the exit status of the command. */
151static int stdin_eof; /* EOF has been encountered on standard error. */
152static Buffer stdin_buffer; /* Buffer for stdin data. */
153static Buffer stdout_buffer; /* Buffer for stdout data. */
154static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000155static u_long stdin_bytes, stdout_bytes, stderr_bytes;
156static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100157static int connection_in; /* Connection to server (input). */
158static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000159static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000160static int session_closed = 0; /* In SSH2: login session closed. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000163int session_ident = -1;
164
Damien Miller0e220db2004-06-15 10:34:08 +1000165struct confirm_ctx {
166 int want_tty;
167 int want_subsys;
Damien Miller13390022005-07-06 09:44:19 +1000168 int want_x_fwd;
169 int want_agent_fwd;
Damien Miller0e220db2004-06-15 10:34:08 +1000170 Buffer cmd;
171 char *term;
172 struct termios tio;
Damien Miller3756dce2004-06-18 01:17:29 +1000173 char **env;
Damien Miller0e220db2004-06-15 10:34:08 +1000174};
175
Ben Lindstromf28f6342001-04-04 02:03:04 +0000176/*XXX*/
177extern Kex *xxx_kex;
178
Damien Miller0e220db2004-06-15 10:34:08 +1000179void ssh_process_session2_setup(int, int, int, Buffer *);
180
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181/* Restores stdin to blocking mode. */
182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000184leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Damien Miller95def091999-11-25 00:26:21 +1100186 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000187 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100188 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100189 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Damien Miller95def091999-11-25 00:26:21 +1100192/* Puts stdin terminal in non-blocking mode. */
193
Ben Lindstrombba81212001-06-25 05:01:22 +0000194static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000195enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196{
Damien Miller95def091999-11-25 00:26:21 +1100197 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000198 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199}
200
Damien Miller5428f641999-11-25 11:54:57 +1100201/*
202 * Signal handler for the window change signal (SIGWINCH). This just sets a
203 * flag indicating that the window has changed.
204 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100205/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static void
Damien Miller95def091999-11-25 00:26:21 +1100207window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208{
Damien Miller95def091999-11-25 00:26:21 +1100209 received_window_change_signal = 1;
210 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211}
212
Damien Miller5428f641999-11-25 11:54:57 +1100213/*
214 * Signal handler for signals that cause the program to terminate. These
215 * signals must be trapped to restore terminal modes.
216 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100217/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000218static void
Damien Miller95def091999-11-25 00:26:21 +1100219signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000221 received_signal = sig;
222 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223}
224
Damien Miller5428f641999-11-25 11:54:57 +1100225/*
226 * Returns current time in seconds from Jan 1, 1970 with the maximum
227 * available resolution.
228 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
Ben Lindstrombba81212001-06-25 05:01:22 +0000230static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000231get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232{
Damien Miller95def091999-11-25 00:26:21 +1100233 struct timeval tv;
234 gettimeofday(&tv, NULL);
235 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236}
237
Damien Miller17e7ed02005-06-17 12:54:33 +1000238#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
239void
240client_x11_get_proto(const char *display, const char *xauth_path,
241 u_int trusted, char **_proto, char **_data)
242{
243 char cmd[1024];
244 char line[512];
245 char xdisplay[512];
246 static char proto[512], data[512];
247 FILE *f;
248 int got_data = 0, generated = 0, do_unlink = 0, i;
249 char *xauthdir, *xauthfile;
250 struct stat st;
251
252 xauthdir = xauthfile = NULL;
253 *_proto = proto;
254 *_data = data;
255 proto[0] = data[0] = '\0';
256
257 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
258 debug("No xauth program.");
259 } else {
260 if (display == NULL) {
261 debug("x11_get_proto: DISPLAY not set");
262 return;
263 }
264 /*
265 * Handle FamilyLocal case where $DISPLAY does
266 * not match an authorization entry. For this we
267 * just try "xauth list unix:displaynum.screennum".
268 * XXX: "localhost" match to determine FamilyLocal
269 * is not perfect.
270 */
271 if (strncmp(display, "localhost:", 10) == 0) {
272 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
273 display + 10);
274 display = xdisplay;
275 }
276 if (trusted == 0) {
277 xauthdir = xmalloc(MAXPATHLEN);
278 xauthfile = xmalloc(MAXPATHLEN);
279 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
280 if (mkdtemp(xauthdir) != NULL) {
281 do_unlink = 1;
282 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
283 xauthdir);
284 snprintf(cmd, sizeof(cmd),
285 "%s -f %s generate %s " SSH_X11_PROTO
286 " untrusted timeout 1200 2>" _PATH_DEVNULL,
287 xauth_path, xauthfile, display);
288 debug2("x11_get_proto: %s", cmd);
289 if (system(cmd) == 0)
290 generated = 1;
291 }
292 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000293
294 /*
295 * When in untrusted mode, we read the cookie only if it was
296 * successfully generated as an untrusted one in the step
297 * above.
298 */
299 if (trusted || generated) {
300 snprintf(cmd, sizeof(cmd),
301 "%s %s%s list %s 2>" _PATH_DEVNULL,
302 xauth_path,
303 generated ? "-f " : "" ,
304 generated ? xauthfile : "",
305 display);
306 debug2("x11_get_proto: %s", cmd);
307 f = popen(cmd, "r");
308 if (f && fgets(line, sizeof(line), f) &&
309 sscanf(line, "%*s %511s %511s", proto, data) == 2)
310 got_data = 1;
311 if (f)
312 pclose(f);
313 } else
314 error("Warning: untrusted X11 forwarding setup failed: "
315 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000316 }
317
318 if (do_unlink) {
319 unlink(xauthfile);
320 rmdir(xauthdir);
321 }
322 if (xauthdir)
323 xfree(xauthdir);
324 if (xauthfile)
325 xfree(xauthfile);
326
327 /*
328 * If we didn't get authentication data, just make up some
329 * data. The forwarding code will check the validity of the
330 * response anyway, and substitute this data. The X11
331 * server, however, will ignore this fake data and use
332 * whatever authentication mechanisms it was using otherwise
333 * for the local connection.
334 */
335 if (!got_data) {
336 u_int32_t rnd = 0;
337
338 logit("Warning: No xauth data; "
339 "using fake authentication data for X11 forwarding.");
340 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
341 for (i = 0; i < 16; i++) {
342 if (i % 4 == 0)
343 rnd = arc4random();
344 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
345 rnd & 0xff);
346 rnd >>= 8;
347 }
348 }
349}
350
Damien Miller5428f641999-11-25 11:54:57 +1100351/*
352 * This is called when the interactive is entered. This checks if there is
353 * an EOF coming on stdin. We must check this explicitly, as select() does
354 * not appear to wake up when redirecting from /dev/null.
355 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000358client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359{
Damien Miller95def091999-11-25 00:26:21 +1100360 int len;
361 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Miller5428f641999-11-25 11:54:57 +1100363 /*
364 * If standard input is to be "redirected from /dev/null", we simply
365 * mark that we have seen an EOF and send an EOF message to the
366 * server. Otherwise, we try to read a single character; it appears
367 * that for some files, such /dev/null, select() never wakes up for
368 * read for this descriptor, which means that we never get EOF. This
369 * way we will get the EOF if stdin comes from /dev/null or similar.
370 */
Damien Miller95def091999-11-25 00:26:21 +1100371 if (stdin_null_flag) {
372 /* Fake EOF on stdin. */
373 debug("Sending eof.");
374 stdin_eof = 1;
375 packet_start(SSH_CMSG_EOF);
376 packet_send();
377 } else {
Damien Miller95def091999-11-25 00:26:21 +1100378 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Damien Miller95def091999-11-25 00:26:21 +1100380 /* Check for immediate EOF on stdin. */
381 len = read(fileno(stdin), buf, 1);
382 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100383 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100384 debug("Sending eof.");
385 stdin_eof = 1;
386 packet_start(SSH_CMSG_EOF);
387 packet_send();
388 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100389 /*
390 * Got data. We must store the data in the buffer,
391 * and also process it as an escape character if
392 * appropriate.
393 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000394 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100395 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000396 else
Damien Miller95def091999-11-25 00:26:21 +1100397 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100398 }
Damien Miller95def091999-11-25 00:26:21 +1100399 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401}
402
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403
Damien Miller5428f641999-11-25 11:54:57 +1100404/*
405 * Make packets from buffered stdin data, and buffer them for sending to the
406 * connection.
407 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Ben Lindstrombba81212001-06-25 05:01:22 +0000409static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000410client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000412 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Damien Miller95def091999-11-25 00:26:21 +1100414 /* Send buffered stdin data to the server. */
415 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100416 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100417 len = buffer_len(&stdin_buffer);
418 /* Keep the packets at reasonable size. */
419 if (len > packet_get_maxsize())
420 len = packet_get_maxsize();
421 packet_start(SSH_CMSG_STDIN_DATA);
422 packet_put_string(buffer_ptr(&stdin_buffer), len);
423 packet_send();
424 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000425 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100426 /* If we have a pending EOF, send it now. */
427 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
428 packet_start(SSH_CMSG_EOF);
429 packet_send();
430 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
Damien Miller5428f641999-11-25 11:54:57 +1100434/*
435 * Checks if the client window has changed, and sends a packet about it to
436 * the server if so. The actual change is detected elsewhere (by a software
437 * interrupt on Unix); this just checks the flag and sends a message if
438 * appropriate.
439 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
Ben Lindstrombba81212001-06-25 05:01:22 +0000441static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000442client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443{
Damien Miller1383bd82000-04-06 12:32:37 +1000444 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445
Damien Miller1383bd82000-04-06 12:32:37 +1000446 if (! received_window_change_signal)
447 return;
448 /** XXX race */
449 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Damien Millerd3444942000-09-30 14:20:03 +1100451 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000452
453 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000454 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000455 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000456 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
457 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000458 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100459 packet_put_int((u_int)ws.ws_row);
460 packet_put_int((u_int)ws.ws_col);
461 packet_put_int((u_int)ws.ws_xpixel);
462 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000463 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465}
466
Damien Miller509b0102003-12-17 16:33:10 +1100467static void
468client_global_request_reply(int type, u_int32_t seq, void *ctxt)
469{
Damien Miller58226f62008-03-07 18:33:30 +1100470 keep_alive_timeouts = 0;
Damien Miller509b0102003-12-17 16:33:10 +1100471 client_global_request_reply_fwd(type, seq, ctxt);
472}
473
474static void
475server_alive_check(void)
476{
Damien Miller58226f62008-03-07 18:33:30 +1100477 if (++keep_alive_timeouts > options.server_alive_count_max) {
Damien Miller985a4482006-10-24 03:02:41 +1000478 logit("Timeout, server not responding.");
479 cleanup_exit(255);
480 }
Damien Miller509b0102003-12-17 16:33:10 +1100481 packet_start(SSH2_MSG_GLOBAL_REQUEST);
482 packet_put_cstring("keepalive@openssh.com");
483 packet_put_char(1); /* boolean: want reply */
484 packet_send();
485}
486
Damien Miller5428f641999-11-25 11:54:57 +1100487/*
488 * Waits until the client can do something (some data becomes available on
489 * one of the file descriptors).
490 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000491static void
Damien Miller5e953212001-01-30 09:14:00 +1100492client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000493 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494{
Damien Miller509b0102003-12-17 16:33:10 +1100495 struct timeval tv, *tvp;
496 int ret;
497
Damien Miller5e953212001-01-30 09:14:00 +1100498 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000499 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller1383bd82000-04-06 12:32:37 +1000501 if (!compat20) {
502 /* Read from the connection, unless our buffers are full. */
503 if (buffer_len(&stdout_buffer) < buffer_high &&
504 buffer_len(&stderr_buffer) < buffer_high &&
505 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100506 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000507 /*
508 * Read from stdin, unless we have seen EOF or have very much
509 * buffered data to send to the server.
510 */
511 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100512 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000513
514 /* Select stdout/stderr if have data in buffer. */
515 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100516 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000517 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100518 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000519 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000520 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000521 if (session_closed && !channel_still_open() &&
522 !packet_have_data_to_write()) {
523 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100524 memset(*readsetp, 0, *nallocp);
525 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000526 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000527 } else {
528 FD_SET(connection_in, *readsetp);
529 }
Damien Miller1383bd82000-04-06 12:32:37 +1000530 }
531
Damien Miller95def091999-11-25 00:26:21 +1100532 /* Select server connection if have data to write to the server. */
533 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100534 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100535
Damien Miller0e220db2004-06-15 10:34:08 +1000536 if (control_fd != -1)
537 FD_SET(control_fd, *readsetp);
538
Damien Miller5428f641999-11-25 11:54:57 +1100539 /*
540 * Wait for something to happen. This will suspend the process until
541 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100542 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100543 */
Damien Miller95def091999-11-25 00:26:21 +1100544
Damien Miller509b0102003-12-17 16:33:10 +1100545 if (options.server_alive_interval == 0 || !compat20)
546 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000547 else {
Damien Miller509b0102003-12-17 16:33:10 +1100548 tv.tv_sec = options.server_alive_interval;
549 tv.tv_usec = 0;
550 tvp = &tv;
551 }
552 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
553 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100554 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000555
556 /*
557 * We have to clear the select masks, because we return.
558 * We have to return, because the mainloop checks for the flags
559 * set by the signal handlers.
560 */
Damien Miller79faeff2001-11-12 11:06:32 +1100561 memset(*readsetp, 0, *nallocp);
562 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000563
Damien Miller95def091999-11-25 00:26:21 +1100564 if (errno == EINTR)
565 return;
566 /* Note: we might still have data in the buffers. */
567 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
568 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100569 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100570 } else if (ret == 0)
571 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572}
573
Ben Lindstrombba81212001-06-25 05:01:22 +0000574static void
Damien Millerad833b32000-08-23 10:46:23 +1000575client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576{
Damien Miller95def091999-11-25 00:26:21 +1100577 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000578 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000579 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000580 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000581 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Damien Miller95def091999-11-25 00:26:21 +1100583 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584
Damien Miller5428f641999-11-25 11:54:57 +1100585 /*
586 * Free (and clear) the buffer to reduce the amount of data that gets
587 * written to swap.
588 */
Damien Millerad833b32000-08-23 10:46:23 +1000589 buffer_free(bin);
590 buffer_free(bout);
591 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592
Damien Miller95def091999-11-25 00:26:21 +1100593 /* Send the suspend signal to the program itself. */
594 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Darren Tucker5d78de62004-11-05 20:35:44 +1100596 /* Reset window sizes in case they have changed */
597 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598
Damien Miller95def091999-11-25 00:26:21 +1100599 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000600 buffer_init(bin);
601 buffer_init(bout);
602 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603
Damien Miller95def091999-11-25 00:26:21 +1100604 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605}
606
Ben Lindstrombba81212001-06-25 05:01:22 +0000607static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100608client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609{
Damien Miller1383bd82000-04-06 12:32:37 +1000610 int len;
611 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612
Damien Miller5428f641999-11-25 11:54:57 +1100613 /*
614 * Read input from the server, and add any such data to the buffer of
615 * the packet subsystem.
616 */
Damien Miller95def091999-11-25 00:26:21 +1100617 if (FD_ISSET(connection_in, readset)) {
618 /* Read as much as possible. */
619 len = read(connection_in, buf, sizeof(buf));
620 if (len == 0) {
621 /* Received EOF. The remote host has closed the connection. */
622 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
623 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625 quit_pending = 1;
626 return;
Damien Miller95def091999-11-25 00:26:21 +1100627 }
Damien Miller5428f641999-11-25 11:54:57 +1100628 /*
629 * There is a kernel bug on Solaris that causes select to
630 * sometimes wake up even though there is no data available.
631 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000632 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100633 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Damien Miller95def091999-11-25 00:26:21 +1100635 if (len < 0) {
636 /* An error has encountered. Perhaps there is a network problem. */
637 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
638 host, strerror(errno));
639 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100640 quit_pending = 1;
641 return;
642 }
643 packet_process_incoming(buf, len);
644 }
Damien Miller1383bd82000-04-06 12:32:37 +1000645}
646
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000647static void
Damien Miller0e220db2004-06-15 10:34:08 +1000648client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
649{
650 int id;
651 Channel *c;
Darren Tuckerfc959702004-07-17 16:12:08 +1000652
Damien Miller0e220db2004-06-15 10:34:08 +1000653 id = packet_get_int();
654 packet_check_eom();
655
656 if ((c = channel_lookup(id)) == NULL) {
657 error("%s: no channel for id %d", __func__, id);
658 return;
659 }
660
661 if (type == SSH2_MSG_CHANNEL_SUCCESS)
662 debug2("Request suceeded on channel %d", id);
663 else if (type == SSH2_MSG_CHANNEL_FAILURE) {
664 error("Request failed on channel %d", id);
665 channel_free(c);
666 }
667}
668
669static void
670client_extra_session2_setup(int id, void *arg)
671{
672 struct confirm_ctx *cctx = arg;
Damien Miller13390022005-07-06 09:44:19 +1000673 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +1000674 Channel *c;
Damien Miller3756dce2004-06-18 01:17:29 +1000675 int i;
Darren Tuckerfc959702004-07-17 16:12:08 +1000676
Damien Miller0e220db2004-06-15 10:34:08 +1000677 if (cctx == NULL)
678 fatal("%s: cctx == NULL", __func__);
679 if ((c = channel_lookup(id)) == NULL)
680 fatal("%s: no channel for id %d", __func__, id);
681
Damien Miller46d38de2005-07-17 17:02:09 +1000682 display = getenv("DISPLAY");
Damien Miller13390022005-07-06 09:44:19 +1000683 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
684 char *proto, *data;
685 /* Get reasonable local authentication information. */
686 client_x11_get_proto(display, options.xauth_location,
687 options.forward_x11_trusted, &proto, &data);
688 /* Request forwarding with authentication spoofing. */
689 debug("Requesting X11 forwarding with authentication spoofing.");
690 x11_request_forwarding_with_spoofing(id, display, proto, data);
691 /* XXX wait for reply */
692 }
693
694 if (cctx->want_agent_fwd && options.forward_agent) {
695 debug("Requesting authentication agent forwarding.");
696 channel_request_start(id, "auth-agent-req@openssh.com", 0);
697 packet_send();
698 }
699
Darren Tuckerfc959702004-07-17 16:12:08 +1000700 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
Damien Miller3756dce2004-06-18 01:17:29 +1000701 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env,
Damien Miller0e220db2004-06-15 10:34:08 +1000702 client_subsystem_reply);
Damien Miller23f07702004-06-18 01:19:03 +1000703
Damien Millerb84886b2008-05-19 15:05:07 +1000704 c->open_confirm_ctx = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000705 buffer_free(&cctx->cmd);
Damien Miller3756dce2004-06-18 01:17:29 +1000706 xfree(cctx->term);
707 if (cctx->env != NULL) {
708 for (i = 0; cctx->env[i] != NULL; i++)
709 xfree(cctx->env[i]);
710 xfree(cctx->env);
Darren Tuckerfc959702004-07-17 16:12:08 +1000711 }
Damien Miller3756dce2004-06-18 01:17:29 +1000712 xfree(cctx);
Damien Miller0e220db2004-06-15 10:34:08 +1000713}
714
715static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100716client_process_control(fd_set *readset)
Damien Miller0e220db2004-06-15 10:34:08 +1000717{
718 Buffer m;
719 Channel *c;
Darren Tuckerd04188e2007-02-25 20:36:49 +1100720 int client_fd, new_fd[3], ver, allowed, window, packetmax;
Damien Miller0e220db2004-06-15 10:34:08 +1000721 socklen_t addrlen;
722 struct sockaddr_storage addr;
723 struct confirm_ctx *cctx;
724 char *cmd;
Damien Miller54fd7cf2007-09-17 12:04:08 +1000725 u_int i, j, len, env_len, command, flags;
Damien Miller0e220db2004-06-15 10:34:08 +1000726 uid_t euid;
727 gid_t egid;
728
729 /*
730 * Accept connection on control socket
731 */
732 if (control_fd == -1 || !FD_ISSET(control_fd, readset))
733 return;
734
735 memset(&addr, 0, sizeof(addr));
736 addrlen = sizeof(addr);
737 if ((client_fd = accept(control_fd,
738 (struct sockaddr*)&addr, &addrlen)) == -1) {
739 error("%s accept: %s", __func__, strerror(errno));
740 return;
741 }
742
743 if (getpeereid(client_fd, &euid, &egid) < 0) {
744 error("%s getpeereid failed: %s", __func__, strerror(errno));
745 close(client_fd);
746 return;
747 }
748 if ((euid != 0) && (getuid() != euid)) {
749 error("control mode uid mismatch: peer euid %u != uid %u",
750 (u_int) euid, (u_int) getuid());
751 close(client_fd);
752 return;
753 }
Damien Miller23f07702004-06-18 01:19:03 +1000754
Damien Miller0e220db2004-06-15 10:34:08 +1000755 unset_nonblock(client_fd);
756
Darren Tucker7ebfc102004-11-07 20:06:19 +1100757 /* Read command */
Damien Miller0e220db2004-06-15 10:34:08 +1000758 buffer_init(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100759 if (ssh_msg_recv(client_fd, &m) == -1) {
760 error("%s: client msg_recv failed", __func__);
761 close(client_fd);
762 buffer_free(&m);
763 return;
764 }
Damien Miller13390022005-07-06 09:44:19 +1000765 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100766 error("%s: wrong client version %d", __func__, ver);
767 buffer_free(&m);
768 close(client_fd);
769 return;
770 }
Damien Miller0e220db2004-06-15 10:34:08 +1000771
Darren Tucker7ebfc102004-11-07 20:06:19 +1100772 allowed = 1;
773 command = buffer_get_int(&m);
774 flags = buffer_get_int(&m);
775
776 buffer_clear(&m);
777
778 switch (command) {
779 case SSHMUX_COMMAND_OPEN:
Damien Millerd14b1e72005-06-16 13:19:41 +1000780 if (options.control_master == SSHCTL_MASTER_ASK ||
781 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100782 allowed = ask_permission("Allow shared connection "
783 "to %s? ", host);
784 /* continue below */
785 break;
786 case SSHMUX_COMMAND_TERMINATE:
Damien Millerd14b1e72005-06-16 13:19:41 +1000787 if (options.control_master == SSHCTL_MASTER_ASK ||
788 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100789 allowed = ask_permission("Terminate shared connection "
790 "to %s? ", host);
791 if (allowed)
792 quit_pending = 1;
Darren Tucker47eede72005-03-14 23:08:12 +1100793 /* FALLTHROUGH */
Darren Tucker7ebfc102004-11-07 20:06:19 +1100794 case SSHMUX_COMMAND_ALIVE_CHECK:
795 /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
796 buffer_clear(&m);
797 buffer_put_int(&m, allowed);
798 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000799 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100800 error("%s: client msg_send failed", __func__);
801 close(client_fd);
802 buffer_free(&m);
803 return;
804 }
805 buffer_free(&m);
806 close(client_fd);
807 return;
808 default:
809 error("Unsupported command %d", command);
810 buffer_free(&m);
811 close(client_fd);
812 return;
813 }
814
815 /* Reply for SSHMUX_COMMAND_OPEN */
816 buffer_clear(&m);
Damien Miller23f07702004-06-18 01:19:03 +1000817 buffer_put_int(&m, allowed);
Damien Miller0e220db2004-06-15 10:34:08 +1000818 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000819 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000820 error("%s: client msg_send failed", __func__);
821 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000822 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000823 return;
824 }
Damien Miller0e220db2004-06-15 10:34:08 +1000825
Damien Miller23f07702004-06-18 01:19:03 +1000826 if (!allowed) {
827 error("Refused control connection");
828 close(client_fd);
829 buffer_free(&m);
830 return;
831 }
832
Darren Tucker7ebfc102004-11-07 20:06:19 +1100833 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000834 if (ssh_msg_recv(client_fd, &m) == -1) {
835 error("%s: client msg_recv failed", __func__);
836 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000837 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000838 return;
839 }
Damien Miller13390022005-07-06 09:44:19 +1000840 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Damien Miller0e220db2004-06-15 10:34:08 +1000841 error("%s: wrong client version %d", __func__, ver);
842 buffer_free(&m);
843 close(client_fd);
844 return;
845 }
846
Damien Miller07d86be2006-03-26 14:19:21 +1100847 cctx = xcalloc(1, sizeof(*cctx));
Darren Tucker7ebfc102004-11-07 20:06:19 +1100848 cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
849 cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
Damien Miller13390022005-07-06 09:44:19 +1000850 cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
851 cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
Damien Miller0e220db2004-06-15 10:34:08 +1000852 cctx->term = buffer_get_string(&m, &len);
853
854 cmd = buffer_get_string(&m, &len);
855 buffer_init(&cctx->cmd);
856 buffer_append(&cctx->cmd, cmd, strlen(cmd));
857
Damien Miller3756dce2004-06-18 01:17:29 +1000858 env_len = buffer_get_int(&m);
859 env_len = MIN(env_len, 4096);
860 debug3("%s: receiving %d env vars", __func__, env_len);
861 if (env_len != 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100862 cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
Damien Miller3756dce2004-06-18 01:17:29 +1000863 for (i = 0; i < env_len; i++)
864 cctx->env[i] = buffer_get_string(&m, &len);
865 cctx->env[i] = NULL;
866 }
867
Damien Miller0e220db2004-06-15 10:34:08 +1000868 debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
869 cctx->want_tty, cctx->want_subsys, cmd);
Damien Miller24fd8dd2006-06-13 13:00:09 +1000870 xfree(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +1000871
872 /* Gather fds from client */
Damien Miller54fd7cf2007-09-17 12:04:08 +1000873 for(i = 0; i < 3; i++) {
874 if ((new_fd[i] = mm_receive_fd(client_fd)) == -1) {
875 error("%s: failed to receive fd %d from slave",
876 __func__, i);
877 for (j = 0; j < i; j++)
878 close(new_fd[j]);
879 for (j = 0; j < env_len; j++)
880 xfree(cctx->env[j]);
881 if (env_len > 0)
882 xfree(cctx->env);
883 xfree(cctx->term);
884 buffer_free(&cctx->cmd);
Damien Millerb82f5dd2008-02-10 22:22:53 +1100885 close(client_fd);
Damien Miller54fd7cf2007-09-17 12:04:08 +1000886 xfree(cctx);
887 return;
888 }
889 }
Damien Miller0e220db2004-06-15 10:34:08 +1000890
891 debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
892 new_fd[0], new_fd[1], new_fd[2]);
893
894 /* Try to pick up ttymodes from client before it goes raw */
895 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
896 error("%s: tcgetattr: %s", __func__, strerror(errno));
897
Darren Tucker7ebfc102004-11-07 20:06:19 +1100898 /* This roundtrip is just for synchronisation of ttymodes */
Damien Miller0e220db2004-06-15 10:34:08 +1000899 buffer_clear(&m);
Damien Miller13390022005-07-06 09:44:19 +1000900 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000901 error("%s: client msg_send failed", __func__);
902 close(client_fd);
903 close(new_fd[0]);
904 close(new_fd[1]);
905 close(new_fd[2]);
Damien Miller23f07702004-06-18 01:19:03 +1000906 buffer_free(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100907 xfree(cctx->term);
908 if (env_len != 0) {
909 for (i = 0; i < env_len; i++)
910 xfree(cctx->env[i]);
911 xfree(cctx->env);
912 }
Damien Miller0e220db2004-06-15 10:34:08 +1000913 return;
914 }
915 buffer_free(&m);
916
917 /* enable nonblocking unless tty */
918 if (!isatty(new_fd[0]))
919 set_nonblock(new_fd[0]);
920 if (!isatty(new_fd[1]))
921 set_nonblock(new_fd[1]);
922 if (!isatty(new_fd[2]))
923 set_nonblock(new_fd[2]);
924
925 set_nonblock(client_fd);
926
Darren Tuckerd04188e2007-02-25 20:36:49 +1100927 window = CHAN_SES_WINDOW_DEFAULT;
928 packetmax = CHAN_SES_PACKET_DEFAULT;
929 if (cctx->want_tty) {
930 window >>= 1;
931 packetmax >>= 1;
932 }
933
Darren Tuckerfc959702004-07-17 16:12:08 +1000934 c = channel_new("session", SSH_CHANNEL_OPENING,
Darren Tuckerd04188e2007-02-25 20:36:49 +1100935 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
Damien Miller0e220db2004-06-15 10:34:08 +1000936 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
937
938 /* XXX */
939 c->ctl_fd = client_fd;
940
941 debug3("%s: channel_new: %d", __func__, c->self);
942
943 channel_send_open(c->self);
Damien Millerb84886b2008-05-19 15:05:07 +1000944 channel_register_open_confirm(c->self,
945 client_extra_session2_setup, cctx);
Damien Miller0e220db2004-06-15 10:34:08 +1000946}
947
948static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000949process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000950{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000951 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100952 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000953 int delete = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000954 int local = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100955 u_short cancel_port;
956 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000957
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100958 bzero(&fwd, sizeof(fwd));
959 fwd.listen_host = fwd.connect_host = NULL;
960
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000961 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000962 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000963 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000964 if (s == NULL)
965 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100966 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000967 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000968 if (*s == '-')
969 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000970 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000971 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000972
Darren Tucker1973c882004-05-24 10:34:36 +1000973 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000974 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000975 logit(" -L[bind_address:]port:host:hostport "
976 "Request local forward");
977 logit(" -R[bind_address:]port:host:hostport "
978 "Request remote forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000979 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000980 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100981 if (!options.permit_local_command)
982 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000983 logit(" !args "
984 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100985 goto out;
986 }
987
988 if (*s == '!' && options.permit_local_command) {
989 s++;
990 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000991 goto out;
992 }
993
994 if (*s == 'K') {
995 delete = 1;
996 s++;
997 }
998 if (*s != 'L' && *s != 'R') {
Damien Miller996acd22003-04-09 20:59:48 +1000999 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001000 goto out;
1001 }
Darren Tuckere7066df2004-05-24 10:18:05 +10001002 if (*s == 'L')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001003 local = 1;
Darren Tuckere7066df2004-05-24 10:18:05 +10001004 if (local && delete) {
1005 logit("Not supported.");
1006 goto out;
1007 }
1008 if ((!local || delete) && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +10001009 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001010 goto out;
1011 }
Darren Tuckere7066df2004-05-24 10:18:05 +10001012
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001013 while (isspace(*++s))
1014 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001015
Darren Tuckere7066df2004-05-24 10:18:05 +10001016 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001017 cancel_port = 0;
1018 cancel_host = hpdelim(&s); /* may be NULL */
1019 if (s != NULL) {
1020 cancel_port = a2port(s);
1021 cancel_host = cleanhostname(cancel_host);
1022 } else {
1023 cancel_port = a2port(cancel_host);
1024 cancel_host = NULL;
1025 }
1026 if (cancel_port == 0) {
1027 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001028 goto out;
1029 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001030 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +10001031 } else {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001032 if (!parse_forward(&fwd, s)) {
Darren Tuckere7066df2004-05-24 10:18:05 +10001033 logit("Bad forwarding specification.");
1034 goto out;
1035 }
Darren Tuckere7066df2004-05-24 10:18:05 +10001036 if (local) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001037 if (channel_setup_local_fwd_listener(fwd.listen_host,
1038 fwd.listen_port, fwd.connect_host,
1039 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +10001040 logit("Port forwarding failed.");
1041 goto out;
1042 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001043 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001044 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001045 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +10001046 fwd.connect_port) < 0) {
1047 logit("Port forwarding failed.");
1048 goto out;
1049 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001050 }
1051
Darren Tuckere7066df2004-05-24 10:18:05 +10001052 logit("Forwarding port.");
1053 }
1054
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001055out:
1056 signal(SIGINT, handler);
1057 enter_raw_mode();
1058 if (cmd)
1059 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +11001060 if (fwd.listen_host != NULL)
1061 xfree(fwd.listen_host);
1062 if (fwd.connect_host != NULL)
1063 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001064}
1065
Damien Millerad833b32000-08-23 10:46:23 +10001066/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +00001067static int
Damien Millerad833b32000-08-23 10:46:23 +10001068process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
1069{
1070 char string[1024];
1071 pid_t pid;
1072 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001073 u_int i;
1074 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001075 char *s;
1076
Damien Millereccb9de2005-06-17 12:59:34 +10001077 if (len <= 0)
1078 return (0);
1079
1080 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001081 /* Get one character at a time. */
1082 ch = buf[i];
1083
1084 if (escape_pending) {
1085 /* We have previously seen an escape character. */
1086 /* Clear the flag now. */
1087 escape_pending = 0;
1088
1089 /* Process the escaped character. */
1090 switch (ch) {
1091 case '.':
1092 /* Terminate the connection. */
1093 snprintf(string, sizeof string, "%c.\r\n", escape_char);
1094 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001095
1096 quit_pending = 1;
1097 return -1;
1098
1099 case 'Z' - 64:
1100 /* Suspend the program. */
1101 /* Print a message to that effect to the user. */
1102 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
1103 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001104
1105 /* Restore terminal modes and suspend. */
1106 client_suspend_self(bin, bout, berr);
1107
1108 /* We have been continued. */
1109 continue;
1110
Damien Miller54c45982003-05-15 10:20:13 +10001111 case 'B':
1112 if (compat20) {
1113 snprintf(string, sizeof string,
1114 "%cB\r\n", escape_char);
1115 buffer_append(berr, string,
1116 strlen(string));
1117 channel_request_start(session_ident,
1118 "break", 0);
1119 packet_put_int(1000);
1120 packet_send();
1121 }
1122 continue;
1123
Ben Lindstromf28f6342001-04-04 02:03:04 +00001124 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001125 if (compat20) {
1126 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +10001127 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001128 else
1129 need_rekeying = 1;
1130 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001131 continue;
1132
Damien Millerad833b32000-08-23 10:46:23 +10001133 case '&':
Damien Millerad833b32000-08-23 10:46:23 +10001134 /*
1135 * Detach the program (continue to serve connections,
1136 * but put in background and no more new connections).
1137 */
Damien Miller96507ef2001-11-12 10:52:25 +11001138 /* Restore tty modes. */
1139 leave_raw_mode();
1140
1141 /* Stop listening for new connections. */
1142 channel_stop_listening();
1143
1144 snprintf(string, sizeof string,
1145 "%c& [backgrounded]\n", escape_char);
1146 buffer_append(berr, string, strlen(string));
1147
1148 /* Fork into background. */
1149 pid = fork();
1150 if (pid < 0) {
1151 error("fork: %.100s", strerror(errno));
1152 continue;
1153 }
1154 if (pid != 0) { /* This is the parent. */
1155 /* The parent just exits. */
1156 exit(0);
1157 }
1158 /* The child continues serving connections. */
1159 if (compat20) {
1160 buffer_append(bin, "\004", 1);
1161 /* fake EOF on stdin */
1162 return -1;
1163 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001164 /*
1165 * Sending SSH_CMSG_EOF alone does not always appear
1166 * to be enough. So we try to send an EOF character
1167 * first.
1168 */
1169 packet_start(SSH_CMSG_STDIN_DATA);
1170 packet_put_string("\004", 1);
1171 packet_send();
1172 /* Close stdin. */
1173 stdin_eof = 1;
1174 if (buffer_len(bin) == 0) {
1175 packet_start(SSH_CMSG_EOF);
1176 packet_send();
1177 }
1178 }
Damien Miller96507ef2001-11-12 10:52:25 +11001179 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001180
1181 case '?':
1182 snprintf(string, sizeof string,
1183"%c?\r\n\
1184Supported escape sequences:\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001185%c. - terminate connection\r\n\
Damien Miller54c45982003-05-15 10:20:13 +10001186%cB - send a BREAK to the remote system\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001187%cC - open a command line\r\n\
1188%cR - Request rekey (SSH protocol 2 only)\r\n\
1189%c^Z - suspend ssh\r\n\
1190%c# - list forwarded connections\r\n\
1191%c& - background ssh (when waiting for connections to terminate)\r\n\
1192%c? - this message\r\n\
1193%c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001194(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller06692862002-09-04 16:32:10 +10001195 escape_char, escape_char, escape_char, escape_char,
1196 escape_char, escape_char, escape_char, escape_char,
Damien Miller54c45982003-05-15 10:20:13 +10001197 escape_char, escape_char, escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001198 buffer_append(berr, string, strlen(string));
1199 continue;
1200
1201 case '#':
1202 snprintf(string, sizeof string, "%c#\r\n", escape_char);
1203 buffer_append(berr, string, strlen(string));
1204 s = channel_open_message();
1205 buffer_append(berr, s, strlen(s));
1206 xfree(s);
1207 continue;
1208
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001209 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +00001210 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001211 continue;
1212
Damien Millerad833b32000-08-23 10:46:23 +10001213 default:
1214 if (ch != escape_char) {
1215 buffer_put_char(bin, escape_char);
1216 bytes++;
1217 }
1218 /* Escaped characters fall through here */
1219 break;
1220 }
1221 } else {
1222 /*
1223 * The previous character was not an escape char. Check if this
1224 * is an escape.
1225 */
1226 if (last_was_cr && ch == escape_char) {
1227 /* It is. Set the flag and continue to next character. */
1228 escape_pending = 1;
1229 continue;
1230 }
1231 }
1232
1233 /*
1234 * Normal character. Record whether it was a newline,
1235 * and append it to the buffer.
1236 */
1237 last_was_cr = (ch == '\r' || ch == '\n');
1238 buffer_put_char(bin, ch);
1239 bytes++;
1240 }
1241 return bytes;
1242}
1243
Ben Lindstrombba81212001-06-25 05:01:22 +00001244static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001245client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001246{
Damien Miller166fca82000-04-20 07:42:21 +10001247 int len;
Damien Millerad833b32000-08-23 10:46:23 +10001248 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +10001249
Damien Miller95def091999-11-25 00:26:21 +11001250 /* Read input from stdin. */
1251 if (FD_ISSET(fileno(stdin), readset)) {
1252 /* Read as much as possible. */
1253 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001254 if (len < 0 && (errno == EAGAIN || errno == EINTR))
1255 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001256 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001257 /*
1258 * Received EOF or error. They are treated
1259 * similarly, except that an error message is printed
1260 * if it was an error condition.
1261 */
Damien Miller95def091999-11-25 00:26:21 +11001262 if (len < 0) {
1263 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
1264 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001265 }
1266 /* Mark that we have seen EOF. */
1267 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001268 /*
1269 * Send an EOF message to the server unless there is
1270 * data in the buffer. If there is data in the
1271 * buffer, no message will be sent now. Code
1272 * elsewhere will send the EOF when the buffer
1273 * becomes empty if stdin_eof is set.
1274 */
Damien Miller95def091999-11-25 00:26:21 +11001275 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001276 packet_start(SSH_CMSG_EOF);
1277 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001278 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001279 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001280 /*
1281 * Normal successful read, and no escape character.
1282 * Just append the data to buffer.
1283 */
Damien Miller95def091999-11-25 00:26:21 +11001284 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001285 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001286 /*
1287 * Normal, successful read. But we have an escape character
1288 * and have to process the characters one by one.
1289 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +00001290 if (process_escapes(&stdin_buffer, &stdout_buffer,
1291 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001292 return;
Damien Miller95def091999-11-25 00:26:21 +11001293 }
1294 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001295}
1296
Ben Lindstrombba81212001-06-25 05:01:22 +00001297static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001298client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299{
Damien Miller95def091999-11-25 00:26:21 +11001300 int len;
1301 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302
Damien Miller95def091999-11-25 00:26:21 +11001303 /* Write buffered output to stdout. */
1304 if (FD_ISSET(fileno(stdout), writeset)) {
1305 /* Write as much data as possible. */
1306 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001307 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001308 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001309 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001310 len = 0;
1311 else {
Damien Miller5428f641999-11-25 11:54:57 +11001312 /*
1313 * An error or EOF was encountered. Put an
1314 * error message to stderr buffer.
1315 */
Damien Miller95def091999-11-25 00:26:21 +11001316 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
1317 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001318 quit_pending = 1;
1319 return;
1320 }
1321 }
1322 /* Consume printed data from the buffer. */
1323 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001324 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001325 }
Damien Miller95def091999-11-25 00:26:21 +11001326 /* Write buffered output to stderr. */
1327 if (FD_ISSET(fileno(stderr), writeset)) {
1328 /* Write as much data as possible. */
1329 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001330 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001331 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001332 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001333 len = 0;
1334 else {
Damien Miller5428f641999-11-25 11:54:57 +11001335 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +11001336 quit_pending = 1;
1337 return;
1338 }
1339 }
1340 /* Consume printed characters from the buffer. */
1341 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001342 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001343 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344}
1345
Damien Miller5428f641999-11-25 11:54:57 +11001346/*
Damien Millerb38eff82000-04-01 11:09:21 +10001347 * Get packets from the connection input buffer, and process them as long as
1348 * there are packets available.
1349 *
1350 * Any unknown packets received during the actual
1351 * session cause the session to terminate. This is
1352 * intended to make debugging easier since no
1353 * confirmations are sent. Any compatible protocol
1354 * extensions must be negotiated during the
1355 * preparatory phase.
1356 */
1357
Ben Lindstrombba81212001-06-25 05:01:22 +00001358static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001359client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001360{
Ben Lindstromf28f6342001-04-04 02:03:04 +00001361 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001362}
1363
Damien Millerad833b32000-08-23 10:46:23 +10001364/* scan buf[] for '~' before sending data to the peer */
1365
Ben Lindstrombba81212001-06-25 05:01:22 +00001366static int
Damien Millerad833b32000-08-23 10:46:23 +10001367simple_escape_filter(Channel *c, char *buf, int len)
1368{
1369 /* XXX we assume c->extended is writeable */
1370 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1371}
1372
Ben Lindstrombba81212001-06-25 05:01:22 +00001373static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001374client_channel_closed(int id, void *arg)
1375{
Damien Miller3ec27592001-10-12 11:35:04 +10001376 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001377 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001378 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001379}
1380
Damien Millerb38eff82000-04-01 11:09:21 +10001381/*
Damien Miller5428f641999-11-25 11:54:57 +11001382 * Implements the interactive session with the server. This is called after
1383 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001384 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1385 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001386 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387
Damien Miller4af51302000-04-16 11:18:38 +10001388int
Damien Millerad833b32000-08-23 10:46:23 +10001389client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001390{
Damien Miller5e953212001-01-30 09:14:00 +11001391 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001392 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001393 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1394 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001395 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396
Damien Miller95def091999-11-25 00:26:21 +11001397 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001398
Damien Miller95def091999-11-25 00:26:21 +11001399 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
Damien Miller95def091999-11-25 00:26:21 +11001401 /* Initialize variables. */
1402 escape_pending = 0;
1403 last_was_cr = 1;
1404 exit_status = -1;
1405 stdin_eof = 0;
1406 buffer_high = 64 * 1024;
1407 connection_in = packet_get_connection_in();
1408 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001409 max_fd = MAX(connection_in, connection_out);
Damien Miller0e220db2004-06-15 10:34:08 +10001410 if (control_fd != -1)
1411 max_fd = MAX(max_fd, control_fd);
Damien Miller5e953212001-01-30 09:14:00 +11001412
1413 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001414 /* enable nonblocking unless tty */
1415 if (!isatty(fileno(stdin)))
1416 set_nonblock(fileno(stdin));
1417 if (!isatty(fileno(stdout)))
1418 set_nonblock(fileno(stdout));
1419 if (!isatty(fileno(stderr)))
1420 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001421 max_fd = MAX(max_fd, fileno(stdin));
1422 max_fd = MAX(max_fd, fileno(stdout));
1423 max_fd = MAX(max_fd, fileno(stderr));
1424 }
Damien Miller95def091999-11-25 00:26:21 +11001425 stdin_bytes = 0;
1426 stdout_bytes = 0;
1427 stderr_bytes = 0;
1428 quit_pending = 0;
1429 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
Damien Miller95def091999-11-25 00:26:21 +11001431 /* Initialize buffers. */
1432 buffer_init(&stdin_buffer);
1433 buffer_init(&stdout_buffer);
1434 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435
Damien Millerb38eff82000-04-01 11:09:21 +10001436 client_init_dispatch();
1437
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001438 /*
1439 * Set signal handlers, (e.g. to restore non-blocking mode)
1440 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1441 */
Darren Tucker07336da2004-11-05 20:02:16 +11001442 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1443 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001444 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1445 signal(SIGINT, signal_handler);
1446 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1447 signal(SIGQUIT, signal_handler);
1448 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1449 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001450 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451
Damien Miller95def091999-11-25 00:26:21 +11001452 if (have_pty)
1453 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454
Ben Lindstrom5b828322001-02-09 01:34:36 +00001455 if (compat20) {
1456 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001457 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001458 channel_register_filter(session_ident,
Damien Miller077b2382005-12-31 16:22:32 +11001459 simple_escape_filter, NULL);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001460 if (session_ident != -1)
1461 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001462 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001463 } else {
1464 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001465 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001466 }
Damien Millerad833b32000-08-23 10:46:23 +10001467
Damien Miller95def091999-11-25 00:26:21 +11001468 /* Main loop of the client for the interactive session mode. */
1469 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470
Damien Miller5428f641999-11-25 11:54:57 +11001471 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001472 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001474 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001475 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001476
1477 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001478
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001479 if (rekeying) {
1480 debug("rekeying in progress");
1481 } else {
1482 /*
1483 * Make packets of buffered stdin data, and buffer
1484 * them for sending to the server.
1485 */
1486 if (!compat20)
1487 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001489 /*
1490 * Make packets from buffered channel data, and
1491 * enqueue them for sending to the server.
1492 */
1493 if (packet_not_very_much_data_to_write())
1494 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001496 /*
1497 * Check if the window size has changed, and buffer a
1498 * message about it to the server if so.
1499 */
1500 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001502 if (quit_pending)
1503 break;
1504 }
Damien Miller5428f641999-11-25 11:54:57 +11001505 /*
1506 * Wait until we have something to do (something becomes
1507 * available on one of the descriptors).
1508 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001509 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001510 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001511 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512
Damien Miller95def091999-11-25 00:26:21 +11001513 if (quit_pending)
1514 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001516 /* Do channel operations unless rekeying in progress. */
1517 if (!rekeying) {
1518 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001519 if (need_rekeying || packet_need_rekeying()) {
1520 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001521 xxx_kex->done = 0;
1522 kex_send_kexinit(xxx_kex);
1523 need_rekeying = 0;
1524 }
1525 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001526
Damien Miller1383bd82000-04-06 12:32:37 +10001527 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001528 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001529
Damien Miller0e220db2004-06-15 10:34:08 +10001530 /* Accept control connections. */
1531 client_process_control(readset);
1532
Damien Miller1383bd82000-04-06 12:32:37 +10001533 if (quit_pending)
1534 break;
1535
1536 if (!compat20) {
1537 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001538 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001539 /*
1540 * Process output to stdout and stderr. Output to
1541 * the connection is processed elsewhere (above).
1542 */
Damien Miller5e953212001-01-30 09:14:00 +11001543 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001544 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001545
Damien Miller5428f641999-11-25 11:54:57 +11001546 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001547 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001548 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549 }
Damien Miller5e953212001-01-30 09:14:00 +11001550 if (readset)
1551 xfree(readset);
1552 if (writeset)
1553 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001554
Damien Miller95def091999-11-25 00:26:21 +11001555 /* Terminate the session. */
1556
1557 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001558 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001559
Ben Lindstrom601e4362001-06-21 03:19:23 +00001560 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001561
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001562 if (have_pty)
1563 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001564
1565 /* restore blocking io */
1566 if (!isatty(fileno(stdin)))
1567 unset_nonblock(fileno(stdin));
1568 if (!isatty(fileno(stdout)))
1569 unset_nonblock(fileno(stdout));
1570 if (!isatty(fileno(stderr)))
1571 unset_nonblock(fileno(stderr));
1572
Damien Millerd6965512003-12-17 16:31:53 +11001573 /*
1574 * If there was no shell or command requested, there will be no remote
1575 * exit status to be returned. In that case, clear error code if the
1576 * connection was deliberately terminated at this end.
1577 */
1578 if (no_shell_flag && received_signal == SIGTERM) {
1579 received_signal = 0;
1580 exit_status = 0;
1581 }
1582
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001583 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001584 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001585
1586 /*
1587 * In interactive mode (with pseudo tty) display a message indicating
1588 * that the connection has been closed.
1589 */
1590 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1591 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1592 buffer_append(&stderr_buffer, buf, strlen(buf));
1593 }
1594
Damien Miller95def091999-11-25 00:26:21 +11001595 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001596 while (buffer_len(&stdout_buffer) > 0) {
1597 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001598 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001599 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001600 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001601 break;
1602 }
Damien Miller95def091999-11-25 00:26:21 +11001603 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001604 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001605 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001606
Damien Miller95def091999-11-25 00:26:21 +11001607 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001608 while (buffer_len(&stderr_buffer) > 0) {
1609 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001610 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001611 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001612 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001613 break;
1614 }
Damien Miller95def091999-11-25 00:26:21 +11001615 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001616 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001617 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001618
Damien Miller95def091999-11-25 00:26:21 +11001619 /* Clear and free any buffers. */
1620 memset(buf, 0, sizeof(buf));
1621 buffer_free(&stdin_buffer);
1622 buffer_free(&stdout_buffer);
1623 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624
Damien Miller95def091999-11-25 00:26:21 +11001625 /* Report bytes transferred, and transfer rates. */
1626 total_time = get_current_time() - start_time;
1627 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001628 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001629 if (total_time > 0)
1630 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001631 stdin_bytes / total_time, stdout_bytes / total_time,
1632 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001633
1634 /* Return the exit status of the program. */
1635 debug("Exit status %d", exit_status);
1636 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001637}
Damien Millerb38eff82000-04-01 11:09:21 +10001638
1639/*********/
1640
Ben Lindstrombba81212001-06-25 05:01:22 +00001641static void
Damien Miller630d6f42002-01-22 23:17:30 +11001642client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001643{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001644 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001645 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001646 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001647 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001648 memset(data, 0, data_len);
1649 xfree(data);
1650}
Ben Lindstrombba81212001-06-25 05:01:22 +00001651static void
Damien Miller630d6f42002-01-22 23:17:30 +11001652client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001653{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001654 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001655 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001656 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001657 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001658 memset(data, 0, data_len);
1659 xfree(data);
1660}
Ben Lindstrombba81212001-06-25 05:01:22 +00001661static void
Damien Miller630d6f42002-01-22 23:17:30 +11001662client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001663{
Damien Millerb38eff82000-04-01 11:09:21 +10001664 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001665 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001666 /* Acknowledge the exit. */
1667 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1668 packet_send();
1669 /*
1670 * Must wait for packet to be sent since we are
1671 * exiting the loop.
1672 */
1673 packet_write_wait();
1674 /* Flag that we want to exit. */
1675 quit_pending = 1;
1676}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001677static void
1678client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1679{
1680 Channel *c = NULL;
1681 int remote_id, sock;
1682
1683 /* Read the remote channel number from the message. */
1684 remote_id = packet_get_int();
1685 packet_check_eom();
1686
1687 /*
1688 * Get a connection to the local authentication agent (this may again
1689 * get forwarded).
1690 */
1691 sock = ssh_get_authentication_socket();
1692
1693 /*
1694 * If we could not connect the agent, send an error message back to
1695 * the server. This should never happen unless the agent dies,
1696 * because authentication forwarding is only enabled if we have an
1697 * agent.
1698 */
1699 if (sock >= 0) {
1700 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1701 -1, 0, 0, 0, "authentication agent connection", 1);
1702 c->remote_id = remote_id;
1703 c->force_drain = 1;
1704 }
1705 if (c == NULL) {
1706 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1707 packet_put_int(remote_id);
1708 } else {
1709 /* Send a confirmation to the remote host. */
1710 debug("Forwarding authentication connection.");
1711 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1712 packet_put_int(remote_id);
1713 packet_put_int(c->self);
1714 }
1715 packet_send();
1716}
Damien Millerb38eff82000-04-01 11:09:21 +10001717
Ben Lindstrombba81212001-06-25 05:01:22 +00001718static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001719client_request_forwarded_tcpip(const char *request_type, int rchan)
1720{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001721 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001722 char *listen_address, *originator_address;
1723 int listen_port, originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001724 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001725
1726 /* Get rest of the packet */
1727 listen_address = packet_get_string(NULL);
1728 listen_port = packet_get_int();
1729 originator_address = packet_get_string(NULL);
1730 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001731 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001732
1733 debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1734 listen_address, listen_port, originator_address, originator_port);
1735
Ben Lindstrom173e6462001-07-04 05:15:15 +00001736 sock = channel_connect_by_listen_address(listen_port);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001737 if (sock < 0) {
1738 xfree(originator_address);
1739 xfree(listen_address);
1740 return NULL;
1741 }
1742 c = channel_new("forwarded-tcpip",
1743 SSH_CHANNEL_CONNECTING, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001744 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001745 originator_address, 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001746 xfree(originator_address);
1747 xfree(listen_address);
1748 return c;
1749}
1750
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001751static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001752client_request_x11(const char *request_type, int rchan)
1753{
1754 Channel *c = NULL;
1755 char *originator;
1756 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001757 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001758
1759 if (!options.forward_x11) {
1760 error("Warning: ssh server tried X11 forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001761 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001762 return NULL;
1763 }
1764 originator = packet_get_string(NULL);
1765 if (datafellows & SSH_BUG_X11FWD) {
1766 debug2("buggy server: x11 request w/o originator_port");
1767 originator_port = 0;
1768 } else {
1769 originator_port = packet_get_int();
1770 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001771 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001772 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001773 debug("client_request_x11: request from %s %d", originator,
1774 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001775 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001776 sock = x11_connect_display();
1777 if (sock < 0)
1778 return NULL;
1779 c = channel_new("x11",
1780 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001781 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001782 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001783 return c;
1784}
1785
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001786static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001787client_request_agent(const char *request_type, int rchan)
1788{
1789 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001790 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001791
1792 if (!options.forward_agent) {
1793 error("Warning: ssh server tried agent forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001794 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001795 return NULL;
1796 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001797 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001798 if (sock < 0)
1799 return NULL;
1800 c = channel_new("authentication agent connection",
1801 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001802 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001803 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001804 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001805 return c;
1806}
1807
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001808int
1809client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1810{
1811 Channel *c;
1812 int fd;
1813
1814 if (tun_mode == SSH_TUNMODE_NO)
1815 return 0;
1816
1817 if (!compat20) {
1818 error("Tunnel forwarding is not support for protocol 1");
1819 return -1;
1820 }
1821
1822 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1823
1824 /* Open local tunnel device */
1825 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1826 error("Tunnel device open failed.");
1827 return -1;
1828 }
1829
1830 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1831 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1832 c->datagram = 1;
1833
1834#if defined(SSH_TUN_FILTER)
1835 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1836 channel_register_filter(c->self, sys_tun_infilter,
1837 sys_tun_outfilter);
1838#endif
1839
1840 packet_start(SSH2_MSG_CHANNEL_OPEN);
1841 packet_put_cstring("tun@openssh.com");
1842 packet_put_int(c->self);
1843 packet_put_int(c->local_window_max);
1844 packet_put_int(c->local_maxpacket);
1845 packet_put_int(tun_mode);
1846 packet_put_int(remote_tun);
1847 packet_send();
1848
1849 return 0;
1850}
1851
Damien Millerbd483e72000-04-30 10:00:53 +10001852/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001853static void
Damien Miller630d6f42002-01-22 23:17:30 +11001854client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001855{
1856 Channel *c = NULL;
1857 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001858 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001859 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001860
1861 ctype = packet_get_string(&len);
1862 rchan = packet_get_int();
1863 rwindow = packet_get_int();
1864 rmaxpack = packet_get_int();
1865
Damien Millere247cc42000-05-07 12:03:14 +10001866 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001867 ctype, rchan, rwindow, rmaxpack);
1868
Damien Miller0bc1bd82000-11-13 22:57:25 +11001869 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1870 c = client_request_forwarded_tcpip(ctype, rchan);
1871 } else if (strcmp(ctype, "x11") == 0) {
1872 c = client_request_x11(ctype, rchan);
1873 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1874 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001875 }
1876/* XXX duplicate : */
1877 if (c != NULL) {
1878 debug("confirm %s", ctype);
1879 c->remote_id = rchan;
1880 c->remote_window = rwindow;
1881 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001882 if (c->type != SSH_CHANNEL_CONNECTING) {
1883 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1884 packet_put_int(c->remote_id);
1885 packet_put_int(c->self);
1886 packet_put_int(c->local_window);
1887 packet_put_int(c->local_maxpacket);
1888 packet_send();
1889 }
Damien Millerbd483e72000-04-30 10:00:53 +10001890 } else {
1891 debug("failure %s", ctype);
1892 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1893 packet_put_int(rchan);
1894 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001895 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001896 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001897 packet_put_cstring("");
1898 }
Damien Millerbd483e72000-04-30 10:00:53 +10001899 packet_send();
1900 }
1901 xfree(ctype);
1902}
Ben Lindstrombba81212001-06-25 05:01:22 +00001903static void
Damien Miller630d6f42002-01-22 23:17:30 +11001904client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001905{
1906 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001907 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001908 char *rtype;
1909
1910 id = packet_get_int();
1911 rtype = packet_get_string(NULL);
1912 reply = packet_get_char();
1913
1914 debug("client_input_channel_req: channel %d rtype %s reply %d",
1915 id, rtype, reply);
1916
Damien Miller3bbd8782004-06-18 22:23:22 +10001917 if (id == -1) {
1918 error("client_input_channel_req: request for channel -1");
1919 } else if ((c = channel_lookup(id)) == NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001920 error("client_input_channel_req: channel %d: unknown channel", id);
1921 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001922 exitval = packet_get_int();
1923 if (id == session_ident) {
1924 success = 1;
1925 exit_status = exitval;
1926 } else if (c->ctl_fd == -1) {
1927 error("client_input_channel_req: unexpected channel %d",
1928 session_ident);
1929 } else {
1930 atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
1931 success = 1;
1932 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001933 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001934 }
1935 if (reply) {
1936 packet_start(success ?
1937 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller3bbd8782004-06-18 22:23:22 +10001938 packet_put_int(id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001939 packet_send();
1940 }
1941 xfree(rtype);
1942}
Damien Millerc3fa4072002-01-22 23:21:58 +11001943static void
1944client_input_global_request(int type, u_int32_t seq, void *ctxt)
1945{
1946 char *rtype;
1947 int want_reply;
1948 int success = 0;
1949
1950 rtype = packet_get_string(NULL);
1951 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001952 debug("client_input_global_request: rtype %s want_reply %d",
1953 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001954 if (want_reply) {
1955 packet_start(success ?
1956 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1957 packet_send();
1958 packet_write_wait();
1959 }
1960 xfree(rtype);
1961}
Damien Millerbd483e72000-04-30 10:00:53 +10001962
Damien Miller0e220db2004-06-15 10:34:08 +10001963void
Darren Tuckerfc959702004-07-17 16:12:08 +10001964client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller3756dce2004-06-18 01:17:29 +10001965 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env,
Damien Miller0e220db2004-06-15 10:34:08 +10001966 dispatch_fn *subsys_repl)
1967{
1968 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001969 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001970
1971 debug2("%s: id %d", __func__, id);
1972
Darren Tucker5d78de62004-11-05 20:35:44 +11001973 if ((c = channel_lookup(id)) == NULL)
1974 fatal("client_session2_setup: channel %d: unknown channel", id);
1975
Damien Miller0e220db2004-06-15 10:34:08 +10001976 if (want_tty) {
1977 struct winsize ws;
1978 struct termios tio;
1979
1980 /* Store window size in the packet. */
1981 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1982 memset(&ws, 0, sizeof(ws));
1983
1984 channel_request_start(id, "pty-req", 0);
1985 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001986 packet_put_int((u_int)ws.ws_col);
1987 packet_put_int((u_int)ws.ws_row);
1988 packet_put_int((u_int)ws.ws_xpixel);
1989 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10001990 tio = get_saved_tio();
1991 tty_make_modes(-1, tiop != NULL ? tiop : &tio);
1992 packet_send();
1993 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001994 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001995 }
1996
1997 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10001998 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10001999 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002000 char *name, *val;
2001
2002 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002003 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002004 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002005 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002006 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11002007 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002008 continue;
2009 }
2010 *val++ = '\0';
2011
2012 matched = 0;
2013 for (j = 0; j < options.num_send_env; j++) {
2014 if (match_pattern(name, options.send_env[j])) {
2015 matched = 1;
2016 break;
2017 }
2018 }
2019 if (!matched) {
2020 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11002021 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002022 continue;
2023 }
2024
2025 debug("Sending env %s = %s", name, val);
2026 channel_request_start(id, "env", 0);
2027 packet_put_cstring(name);
2028 packet_put_cstring(val);
2029 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11002030 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002031 }
2032 }
2033
2034 len = buffer_len(cmd);
2035 if (len > 0) {
2036 if (len > 900)
2037 len = 900;
2038 if (want_subsystem) {
2039 debug("Sending subsystem: %.*s", len, (u_char*)buffer_ptr(cmd));
2040 channel_request_start(id, "subsystem", subsys_repl != NULL);
2041 if (subsys_repl != NULL) {
2042 /* register callback for reply */
2043 /* XXX we assume that client_loop has already been called */
2044 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, subsys_repl);
2045 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, subsys_repl);
2046 }
2047 } else {
2048 debug("Sending command: %.*s", len, (u_char*)buffer_ptr(cmd));
2049 channel_request_start(id, "exec", 0);
2050 }
2051 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2052 packet_send();
2053 } else {
2054 channel_request_start(id, "shell", 0);
2055 packet_send();
2056 }
2057}
2058
Ben Lindstrombba81212001-06-25 05:01:22 +00002059static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002060client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002061{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002062 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002063
Damien Miller1383bd82000-04-06 12:32:37 +10002064 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2065 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2066 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2067 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002068 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002069 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2070 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002071 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002072 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002073 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2074 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002075 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002076
2077 /* rekeying */
2078 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002079
2080 /* global request reply messages */
2081 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2082 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002083}
Ben Lindstrombba81212001-06-25 05:01:22 +00002084static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002085client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002086{
2087 dispatch_init(NULL);
2088 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2089 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2090 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002091 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2092 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2093 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002094 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2095 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2096 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002097
2098 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002099 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002100 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2101 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002102}
Ben Lindstrombba81212001-06-25 05:01:22 +00002103static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002104client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002105{
2106 client_init_dispatch_13();
2107 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2108 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2109}
Ben Lindstromdb47f382001-07-04 05:10:27 +00002110static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002111client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002112{
Damien Miller1383bd82000-04-06 12:32:37 +10002113 if (compat20)
2114 client_init_dispatch_20();
2115 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002116 client_init_dispatch_13();
2117 else
2118 client_init_dispatch_15();
2119}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002120
2121/* client specific fatal cleanup */
2122void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002123cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002124{
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002125 leave_raw_mode();
2126 leave_non_blocking();
Damien Miller0e220db2004-06-15 10:34:08 +10002127 if (options.control_path != NULL && control_fd != -1)
2128 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002129 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002130}