blob: 7bd1af60c95d45c046e8e975094c821c8997e717 [file] [log] [blame]
Damien Millerbd740252008-05-19 15:37:09 +10001/* $OpenBSD: clientloop.c,v 1.191 2008/05/09 04:55:56 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
Damien Miller5771ed72008-05-19 15:35:33 +1000176struct channel_reply_ctx {
177 const char *request_type;
178 int id, do_close;
179};
180
Ben Lindstromf28f6342001-04-04 02:03:04 +0000181/*XXX*/
182extern Kex *xxx_kex;
183
Damien Miller0e220db2004-06-15 10:34:08 +1000184void ssh_process_session2_setup(int, int, int, Buffer *);
185
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186/* Restores stdin to blocking mode. */
187
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000189leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190{
Damien Miller95def091999-11-25 00:26:21 +1100191 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000192 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100193 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100194 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195}
196
Damien Miller95def091999-11-25 00:26:21 +1100197/* Puts stdin terminal in non-blocking mode. */
198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000200enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201{
Damien Miller95def091999-11-25 00:26:21 +1100202 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000203 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204}
205
Damien Miller5428f641999-11-25 11:54:57 +1100206/*
207 * Signal handler for the window change signal (SIGWINCH). This just sets a
208 * flag indicating that the window has changed.
209 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100210/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static void
Damien Miller95def091999-11-25 00:26:21 +1100212window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213{
Damien Miller95def091999-11-25 00:26:21 +1100214 received_window_change_signal = 1;
215 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216}
217
Damien Miller5428f641999-11-25 11:54:57 +1100218/*
219 * Signal handler for signals that cause the program to terminate. These
220 * signals must be trapped to restore terminal modes.
221 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100222/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static void
Damien Miller95def091999-11-25 00:26:21 +1100224signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000226 received_signal = sig;
227 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228}
229
Damien Miller5428f641999-11-25 11:54:57 +1100230/*
231 * Returns current time in seconds from Jan 1, 1970 with the maximum
232 * available resolution.
233 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000236get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237{
Damien Miller95def091999-11-25 00:26:21 +1100238 struct timeval tv;
239 gettimeofday(&tv, NULL);
240 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241}
242
Damien Miller17e7ed02005-06-17 12:54:33 +1000243#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
244void
245client_x11_get_proto(const char *display, const char *xauth_path,
246 u_int trusted, char **_proto, char **_data)
247{
248 char cmd[1024];
249 char line[512];
250 char xdisplay[512];
251 static char proto[512], data[512];
252 FILE *f;
253 int got_data = 0, generated = 0, do_unlink = 0, i;
254 char *xauthdir, *xauthfile;
255 struct stat st;
256
257 xauthdir = xauthfile = NULL;
258 *_proto = proto;
259 *_data = data;
260 proto[0] = data[0] = '\0';
261
262 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
263 debug("No xauth program.");
264 } else {
265 if (display == NULL) {
266 debug("x11_get_proto: DISPLAY not set");
267 return;
268 }
269 /*
270 * Handle FamilyLocal case where $DISPLAY does
271 * not match an authorization entry. For this we
272 * just try "xauth list unix:displaynum.screennum".
273 * XXX: "localhost" match to determine FamilyLocal
274 * is not perfect.
275 */
276 if (strncmp(display, "localhost:", 10) == 0) {
277 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
278 display + 10);
279 display = xdisplay;
280 }
281 if (trusted == 0) {
282 xauthdir = xmalloc(MAXPATHLEN);
283 xauthfile = xmalloc(MAXPATHLEN);
284 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
285 if (mkdtemp(xauthdir) != NULL) {
286 do_unlink = 1;
287 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
288 xauthdir);
289 snprintf(cmd, sizeof(cmd),
290 "%s -f %s generate %s " SSH_X11_PROTO
291 " untrusted timeout 1200 2>" _PATH_DEVNULL,
292 xauth_path, xauthfile, display);
293 debug2("x11_get_proto: %s", cmd);
294 if (system(cmd) == 0)
295 generated = 1;
296 }
297 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000298
299 /*
300 * When in untrusted mode, we read the cookie only if it was
301 * successfully generated as an untrusted one in the step
302 * above.
303 */
304 if (trusted || generated) {
305 snprintf(cmd, sizeof(cmd),
306 "%s %s%s list %s 2>" _PATH_DEVNULL,
307 xauth_path,
308 generated ? "-f " : "" ,
309 generated ? xauthfile : "",
310 display);
311 debug2("x11_get_proto: %s", cmd);
312 f = popen(cmd, "r");
313 if (f && fgets(line, sizeof(line), f) &&
314 sscanf(line, "%*s %511s %511s", proto, data) == 2)
315 got_data = 1;
316 if (f)
317 pclose(f);
318 } else
319 error("Warning: untrusted X11 forwarding setup failed: "
320 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000321 }
322
323 if (do_unlink) {
324 unlink(xauthfile);
325 rmdir(xauthdir);
326 }
327 if (xauthdir)
328 xfree(xauthdir);
329 if (xauthfile)
330 xfree(xauthfile);
331
332 /*
333 * If we didn't get authentication data, just make up some
334 * data. The forwarding code will check the validity of the
335 * response anyway, and substitute this data. The X11
336 * server, however, will ignore this fake data and use
337 * whatever authentication mechanisms it was using otherwise
338 * for the local connection.
339 */
340 if (!got_data) {
341 u_int32_t rnd = 0;
342
343 logit("Warning: No xauth data; "
344 "using fake authentication data for X11 forwarding.");
345 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
346 for (i = 0; i < 16; i++) {
347 if (i % 4 == 0)
348 rnd = arc4random();
349 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
350 rnd & 0xff);
351 rnd >>= 8;
352 }
353 }
354}
355
Damien Miller5428f641999-11-25 11:54:57 +1100356/*
357 * This is called when the interactive is entered. This checks if there is
358 * an EOF coming on stdin. We must check this explicitly, as select() does
359 * not appear to wake up when redirecting from /dev/null.
360 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361
Ben Lindstrombba81212001-06-25 05:01:22 +0000362static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000363client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364{
Damien Miller95def091999-11-25 00:26:21 +1100365 int len;
366 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367
Damien Miller5428f641999-11-25 11:54:57 +1100368 /*
369 * If standard input is to be "redirected from /dev/null", we simply
370 * mark that we have seen an EOF and send an EOF message to the
371 * server. Otherwise, we try to read a single character; it appears
372 * that for some files, such /dev/null, select() never wakes up for
373 * read for this descriptor, which means that we never get EOF. This
374 * way we will get the EOF if stdin comes from /dev/null or similar.
375 */
Damien Miller95def091999-11-25 00:26:21 +1100376 if (stdin_null_flag) {
377 /* Fake EOF on stdin. */
378 debug("Sending eof.");
379 stdin_eof = 1;
380 packet_start(SSH_CMSG_EOF);
381 packet_send();
382 } else {
Damien Miller95def091999-11-25 00:26:21 +1100383 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384
Damien Miller95def091999-11-25 00:26:21 +1100385 /* Check for immediate EOF on stdin. */
386 len = read(fileno(stdin), buf, 1);
387 if (len == 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100388 /* EOF. Record that we have seen it and send EOF to server. */
Damien Miller95def091999-11-25 00:26:21 +1100389 debug("Sending eof.");
390 stdin_eof = 1;
391 packet_start(SSH_CMSG_EOF);
392 packet_send();
393 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100394 /*
395 * Got data. We must store the data in the buffer,
396 * and also process it as an escape character if
397 * appropriate.
398 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000399 if ((u_char) buf[0] == escape_char)
Damien Miller95def091999-11-25 00:26:21 +1100400 escape_pending = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000401 else
Damien Miller95def091999-11-25 00:26:21 +1100402 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100403 }
Damien Miller95def091999-11-25 00:26:21 +1100404 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406}
407
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Damien Miller5428f641999-11-25 11:54:57 +1100409/*
410 * Make packets from buffered stdin data, and buffer them for sending to the
411 * connection.
412 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Ben Lindstrombba81212001-06-25 05:01:22 +0000414static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000415client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000417 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller95def091999-11-25 00:26:21 +1100419 /* Send buffered stdin data to the server. */
420 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100421 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100422 len = buffer_len(&stdin_buffer);
423 /* Keep the packets at reasonable size. */
424 if (len > packet_get_maxsize())
425 len = packet_get_maxsize();
426 packet_start(SSH_CMSG_STDIN_DATA);
427 packet_put_string(buffer_ptr(&stdin_buffer), len);
428 packet_send();
429 buffer_consume(&stdin_buffer, len);
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000430 stdin_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +1100431 /* If we have a pending EOF, send it now. */
432 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
433 packet_start(SSH_CMSG_EOF);
434 packet_send();
435 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437}
438
Damien Miller5428f641999-11-25 11:54:57 +1100439/*
440 * Checks if the client window has changed, and sends a packet about it to
441 * the server if so. The actual change is detected elsewhere (by a software
442 * interrupt on Unix); this just checks the flag and sends a message if
443 * appropriate.
444 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445
Ben Lindstrombba81212001-06-25 05:01:22 +0000446static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000447client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448{
Damien Miller1383bd82000-04-06 12:32:37 +1000449 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Damien Miller1383bd82000-04-06 12:32:37 +1000451 if (! received_window_change_signal)
452 return;
453 /** XXX race */
454 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455
Damien Millerd3444942000-09-30 14:20:03 +1100456 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000457
458 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000459 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000460 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000461 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
462 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000463 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100464 packet_put_int((u_int)ws.ws_row);
465 packet_put_int((u_int)ws.ws_col);
466 packet_put_int((u_int)ws.ws_xpixel);
467 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000468 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470}
471
Damien Miller509b0102003-12-17 16:33:10 +1100472static void
473client_global_request_reply(int type, u_int32_t seq, void *ctxt)
474{
Damien Miller58226f62008-03-07 18:33:30 +1100475 keep_alive_timeouts = 0;
Damien Miller509b0102003-12-17 16:33:10 +1100476 client_global_request_reply_fwd(type, seq, ctxt);
477}
478
479static void
480server_alive_check(void)
481{
Damien Miller58226f62008-03-07 18:33:30 +1100482 if (++keep_alive_timeouts > options.server_alive_count_max) {
Damien Miller985a4482006-10-24 03:02:41 +1000483 logit("Timeout, server not responding.");
484 cleanup_exit(255);
485 }
Damien Miller509b0102003-12-17 16:33:10 +1100486 packet_start(SSH2_MSG_GLOBAL_REQUEST);
487 packet_put_cstring("keepalive@openssh.com");
488 packet_put_char(1); /* boolean: want reply */
489 packet_send();
490}
491
Damien Miller5428f641999-11-25 11:54:57 +1100492/*
493 * Waits until the client can do something (some data becomes available on
494 * one of the file descriptors).
495 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000496static void
Damien Miller5e953212001-01-30 09:14:00 +1100497client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000498 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499{
Damien Miller509b0102003-12-17 16:33:10 +1100500 struct timeval tv, *tvp;
501 int ret;
502
Damien Miller5e953212001-01-30 09:14:00 +1100503 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000504 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Damien Miller1383bd82000-04-06 12:32:37 +1000506 if (!compat20) {
507 /* Read from the connection, unless our buffers are full. */
508 if (buffer_len(&stdout_buffer) < buffer_high &&
509 buffer_len(&stderr_buffer) < buffer_high &&
510 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100511 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000512 /*
513 * Read from stdin, unless we have seen EOF or have very much
514 * buffered data to send to the server.
515 */
516 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100517 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000518
519 /* Select stdout/stderr if have data in buffer. */
520 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100521 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000522 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100523 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000524 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000525 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000526 if (session_closed && !channel_still_open() &&
527 !packet_have_data_to_write()) {
528 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100529 memset(*readsetp, 0, *nallocp);
530 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000531 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000532 } else {
533 FD_SET(connection_in, *readsetp);
534 }
Damien Miller1383bd82000-04-06 12:32:37 +1000535 }
536
Damien Miller95def091999-11-25 00:26:21 +1100537 /* Select server connection if have data to write to the server. */
538 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100539 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100540
Damien Miller0e220db2004-06-15 10:34:08 +1000541 if (control_fd != -1)
542 FD_SET(control_fd, *readsetp);
543
Damien Miller5428f641999-11-25 11:54:57 +1100544 /*
545 * Wait for something to happen. This will suspend the process until
546 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100547 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100548 */
Damien Miller95def091999-11-25 00:26:21 +1100549
Damien Miller509b0102003-12-17 16:33:10 +1100550 if (options.server_alive_interval == 0 || !compat20)
551 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000552 else {
Damien Miller509b0102003-12-17 16:33:10 +1100553 tv.tv_sec = options.server_alive_interval;
554 tv.tv_usec = 0;
555 tvp = &tv;
556 }
557 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
558 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100559 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000560
561 /*
562 * We have to clear the select masks, because we return.
563 * We have to return, because the mainloop checks for the flags
564 * set by the signal handlers.
565 */
Damien Miller79faeff2001-11-12 11:06:32 +1100566 memset(*readsetp, 0, *nallocp);
567 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000568
Damien Miller95def091999-11-25 00:26:21 +1100569 if (errno == EINTR)
570 return;
571 /* Note: we might still have data in the buffers. */
572 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
573 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100574 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100575 } else if (ret == 0)
576 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577}
578
Ben Lindstrombba81212001-06-25 05:01:22 +0000579static void
Damien Millerad833b32000-08-23 10:46:23 +1000580client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581{
Damien Miller95def091999-11-25 00:26:21 +1100582 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000583 if (buffer_len(bout) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000584 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000585 if (buffer_len(berr) > 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000586 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587
Damien Miller95def091999-11-25 00:26:21 +1100588 leave_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
Damien Miller5428f641999-11-25 11:54:57 +1100590 /*
591 * Free (and clear) the buffer to reduce the amount of data that gets
592 * written to swap.
593 */
Damien Millerad833b32000-08-23 10:46:23 +1000594 buffer_free(bin);
595 buffer_free(bout);
596 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597
Damien Miller95def091999-11-25 00:26:21 +1100598 /* Send the suspend signal to the program itself. */
599 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600
Darren Tucker5d78de62004-11-05 20:35:44 +1100601 /* Reset window sizes in case they have changed */
602 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603
Damien Miller95def091999-11-25 00:26:21 +1100604 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000605 buffer_init(bin);
606 buffer_init(bout);
607 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608
Damien Miller95def091999-11-25 00:26:21 +1100609 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610}
611
Ben Lindstrombba81212001-06-25 05:01:22 +0000612static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100613client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614{
Damien Miller1383bd82000-04-06 12:32:37 +1000615 int len;
616 char buf[8192];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617
Damien Miller5428f641999-11-25 11:54:57 +1100618 /*
619 * Read input from the server, and add any such data to the buffer of
620 * the packet subsystem.
621 */
Damien Miller95def091999-11-25 00:26:21 +1100622 if (FD_ISSET(connection_in, readset)) {
623 /* Read as much as possible. */
624 len = read(connection_in, buf, sizeof(buf));
625 if (len == 0) {
626 /* Received EOF. The remote host has closed the connection. */
627 snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
628 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630 quit_pending = 1;
631 return;
Damien Miller95def091999-11-25 00:26:21 +1100632 }
Damien Miller5428f641999-11-25 11:54:57 +1100633 /*
634 * There is a kernel bug on Solaris that causes select to
635 * sometimes wake up even though there is no data available.
636 */
Ben Lindstromebc88272001-03-06 03:34:40 +0000637 if (len < 0 && (errno == EAGAIN || errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100638 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639
Damien Miller95def091999-11-25 00:26:21 +1100640 if (len < 0) {
641 /* An error has encountered. Perhaps there is a network problem. */
642 snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
643 host, strerror(errno));
644 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100645 quit_pending = 1;
646 return;
647 }
648 packet_process_incoming(buf, len);
649 }
Damien Miller1383bd82000-04-06 12:32:37 +1000650}
651
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000652static void
Damien Miller5771ed72008-05-19 15:35:33 +1000653client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000654{
Damien Miller5771ed72008-05-19 15:35:33 +1000655 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
656 char errmsg[256];
657 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000658
Damien Miller5771ed72008-05-19 15:35:33 +1000659 /* XXX supress on mux _client_ quietmode */
660 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
661 c->ctl_fd != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000662
Damien Miller5771ed72008-05-19 15:35:33 +1000663 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
664 debug2("%s request accepted on channel %d",
665 cr->request_type, c->self);
666 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
667 if (tochan) {
668 snprintf(errmsg, sizeof(errmsg),
669 "%s request failed\r\n", cr->request_type);
670 } else {
671 snprintf(errmsg, sizeof(errmsg),
672 "%s request failed on channel %d",
673 cr->request_type, c->self);
674 }
675 /* If error occurred on primary session channel, then exit */
676 if (cr->do_close && c->self == session_ident)
677 fatal("%s", errmsg);
678 /* If error occurred on mux client, append to their stderr */
679 if (tochan)
680 buffer_append(&c->extended, errmsg, strlen(errmsg));
681 else
682 error("%s", errmsg);
683 if (cr->do_close) {
684 chan_read_failed(c);
685 chan_write_failed(c);
686 }
Damien Miller0e220db2004-06-15 10:34:08 +1000687 }
Damien Miller5771ed72008-05-19 15:35:33 +1000688 xfree(cr);
689}
Damien Miller0e220db2004-06-15 10:34:08 +1000690
Damien Miller5771ed72008-05-19 15:35:33 +1000691static void
692client_abandon_status_confirm(Channel *c, void *ctx)
693{
694 xfree(ctx);
695}
696
697static void
698client_expect_confirm(int id, const char *request, int do_close)
699{
700 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
701
702 cr->request_type = request;
703 cr->do_close = do_close;
704
705 channel_register_status_confirm(id, client_status_confirm,
706 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000707}
708
709static void
710client_extra_session2_setup(int id, void *arg)
711{
712 struct confirm_ctx *cctx = arg;
Damien Miller13390022005-07-06 09:44:19 +1000713 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +1000714 Channel *c;
Damien Miller3756dce2004-06-18 01:17:29 +1000715 int i;
Darren Tuckerfc959702004-07-17 16:12:08 +1000716
Damien Miller0e220db2004-06-15 10:34:08 +1000717 if (cctx == NULL)
718 fatal("%s: cctx == NULL", __func__);
719 if ((c = channel_lookup(id)) == NULL)
720 fatal("%s: no channel for id %d", __func__, id);
721
Damien Miller46d38de2005-07-17 17:02:09 +1000722 display = getenv("DISPLAY");
Damien Miller13390022005-07-06 09:44:19 +1000723 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
724 char *proto, *data;
725 /* Get reasonable local authentication information. */
726 client_x11_get_proto(display, options.xauth_location,
727 options.forward_x11_trusted, &proto, &data);
728 /* Request forwarding with authentication spoofing. */
729 debug("Requesting X11 forwarding with authentication spoofing.");
730 x11_request_forwarding_with_spoofing(id, display, proto, data);
731 /* XXX wait for reply */
732 }
733
734 if (cctx->want_agent_fwd && options.forward_agent) {
735 debug("Requesting authentication agent forwarding.");
736 channel_request_start(id, "auth-agent-req@openssh.com", 0);
737 packet_send();
738 }
739
Darren Tuckerfc959702004-07-17 16:12:08 +1000740 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
Damien Miller5771ed72008-05-19 15:35:33 +1000741 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
Damien Miller23f07702004-06-18 01:19:03 +1000742
Damien Millerb84886b2008-05-19 15:05:07 +1000743 c->open_confirm_ctx = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000744 buffer_free(&cctx->cmd);
Damien Miller3756dce2004-06-18 01:17:29 +1000745 xfree(cctx->term);
746 if (cctx->env != NULL) {
747 for (i = 0; cctx->env[i] != NULL; i++)
748 xfree(cctx->env[i]);
749 xfree(cctx->env);
Darren Tuckerfc959702004-07-17 16:12:08 +1000750 }
Damien Miller3756dce2004-06-18 01:17:29 +1000751 xfree(cctx);
Damien Miller0e220db2004-06-15 10:34:08 +1000752}
753
754static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100755client_process_control(fd_set *readset)
Damien Miller0e220db2004-06-15 10:34:08 +1000756{
757 Buffer m;
758 Channel *c;
Darren Tuckerd04188e2007-02-25 20:36:49 +1100759 int client_fd, new_fd[3], ver, allowed, window, packetmax;
Damien Miller0e220db2004-06-15 10:34:08 +1000760 socklen_t addrlen;
761 struct sockaddr_storage addr;
762 struct confirm_ctx *cctx;
763 char *cmd;
Damien Miller54fd7cf2007-09-17 12:04:08 +1000764 u_int i, j, len, env_len, command, flags;
Damien Miller0e220db2004-06-15 10:34:08 +1000765 uid_t euid;
766 gid_t egid;
767
768 /*
769 * Accept connection on control socket
770 */
771 if (control_fd == -1 || !FD_ISSET(control_fd, readset))
772 return;
773
774 memset(&addr, 0, sizeof(addr));
775 addrlen = sizeof(addr);
776 if ((client_fd = accept(control_fd,
777 (struct sockaddr*)&addr, &addrlen)) == -1) {
778 error("%s accept: %s", __func__, strerror(errno));
779 return;
780 }
781
782 if (getpeereid(client_fd, &euid, &egid) < 0) {
783 error("%s getpeereid failed: %s", __func__, strerror(errno));
784 close(client_fd);
785 return;
786 }
787 if ((euid != 0) && (getuid() != euid)) {
788 error("control mode uid mismatch: peer euid %u != uid %u",
789 (u_int) euid, (u_int) getuid());
790 close(client_fd);
791 return;
792 }
Damien Miller23f07702004-06-18 01:19:03 +1000793
Damien Miller0e220db2004-06-15 10:34:08 +1000794 unset_nonblock(client_fd);
795
Darren Tucker7ebfc102004-11-07 20:06:19 +1100796 /* Read command */
Damien Miller0e220db2004-06-15 10:34:08 +1000797 buffer_init(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100798 if (ssh_msg_recv(client_fd, &m) == -1) {
799 error("%s: client msg_recv failed", __func__);
800 close(client_fd);
801 buffer_free(&m);
802 return;
803 }
Damien Miller13390022005-07-06 09:44:19 +1000804 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100805 error("%s: wrong client version %d", __func__, ver);
806 buffer_free(&m);
807 close(client_fd);
808 return;
809 }
Damien Miller0e220db2004-06-15 10:34:08 +1000810
Darren Tucker7ebfc102004-11-07 20:06:19 +1100811 allowed = 1;
812 command = buffer_get_int(&m);
813 flags = buffer_get_int(&m);
814
815 buffer_clear(&m);
816
817 switch (command) {
818 case SSHMUX_COMMAND_OPEN:
Damien Millerd14b1e72005-06-16 13:19:41 +1000819 if (options.control_master == SSHCTL_MASTER_ASK ||
820 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100821 allowed = ask_permission("Allow shared connection "
822 "to %s? ", host);
823 /* continue below */
824 break;
825 case SSHMUX_COMMAND_TERMINATE:
Damien Millerd14b1e72005-06-16 13:19:41 +1000826 if (options.control_master == SSHCTL_MASTER_ASK ||
827 options.control_master == SSHCTL_MASTER_AUTO_ASK)
Darren Tucker7ebfc102004-11-07 20:06:19 +1100828 allowed = ask_permission("Terminate shared connection "
829 "to %s? ", host);
830 if (allowed)
831 quit_pending = 1;
Darren Tucker47eede72005-03-14 23:08:12 +1100832 /* FALLTHROUGH */
Darren Tucker7ebfc102004-11-07 20:06:19 +1100833 case SSHMUX_COMMAND_ALIVE_CHECK:
834 /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
835 buffer_clear(&m);
836 buffer_put_int(&m, allowed);
837 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000838 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Darren Tucker7ebfc102004-11-07 20:06:19 +1100839 error("%s: client msg_send failed", __func__);
840 close(client_fd);
841 buffer_free(&m);
842 return;
843 }
844 buffer_free(&m);
845 close(client_fd);
846 return;
847 default:
848 error("Unsupported command %d", command);
849 buffer_free(&m);
850 close(client_fd);
851 return;
852 }
853
854 /* Reply for SSHMUX_COMMAND_OPEN */
855 buffer_clear(&m);
Damien Miller23f07702004-06-18 01:19:03 +1000856 buffer_put_int(&m, allowed);
Damien Miller0e220db2004-06-15 10:34:08 +1000857 buffer_put_int(&m, getpid());
Damien Miller13390022005-07-06 09:44:19 +1000858 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000859 error("%s: client msg_send failed", __func__);
860 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000861 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000862 return;
863 }
Damien Miller0e220db2004-06-15 10:34:08 +1000864
Damien Miller23f07702004-06-18 01:19:03 +1000865 if (!allowed) {
866 error("Refused control connection");
867 close(client_fd);
868 buffer_free(&m);
869 return;
870 }
871
Darren Tucker7ebfc102004-11-07 20:06:19 +1100872 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000873 if (ssh_msg_recv(client_fd, &m) == -1) {
874 error("%s: client msg_recv failed", __func__);
875 close(client_fd);
Damien Miller23f07702004-06-18 01:19:03 +1000876 buffer_free(&m);
Damien Miller0e220db2004-06-15 10:34:08 +1000877 return;
878 }
Damien Miller13390022005-07-06 09:44:19 +1000879 if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
Damien Miller0e220db2004-06-15 10:34:08 +1000880 error("%s: wrong client version %d", __func__, ver);
881 buffer_free(&m);
882 close(client_fd);
883 return;
884 }
885
Damien Miller07d86be2006-03-26 14:19:21 +1100886 cctx = xcalloc(1, sizeof(*cctx));
Darren Tucker7ebfc102004-11-07 20:06:19 +1100887 cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
888 cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
Damien Miller13390022005-07-06 09:44:19 +1000889 cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
890 cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
Damien Miller0e220db2004-06-15 10:34:08 +1000891 cctx->term = buffer_get_string(&m, &len);
892
893 cmd = buffer_get_string(&m, &len);
894 buffer_init(&cctx->cmd);
895 buffer_append(&cctx->cmd, cmd, strlen(cmd));
896
Damien Miller3756dce2004-06-18 01:17:29 +1000897 env_len = buffer_get_int(&m);
898 env_len = MIN(env_len, 4096);
899 debug3("%s: receiving %d env vars", __func__, env_len);
900 if (env_len != 0) {
Damien Miller07d86be2006-03-26 14:19:21 +1100901 cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
Damien Miller3756dce2004-06-18 01:17:29 +1000902 for (i = 0; i < env_len; i++)
903 cctx->env[i] = buffer_get_string(&m, &len);
904 cctx->env[i] = NULL;
905 }
906
Damien Miller0e220db2004-06-15 10:34:08 +1000907 debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
908 cctx->want_tty, cctx->want_subsys, cmd);
Damien Miller24fd8dd2006-06-13 13:00:09 +1000909 xfree(cmd);
Damien Miller0e220db2004-06-15 10:34:08 +1000910
911 /* Gather fds from client */
Damien Miller54fd7cf2007-09-17 12:04:08 +1000912 for(i = 0; i < 3; i++) {
913 if ((new_fd[i] = mm_receive_fd(client_fd)) == -1) {
914 error("%s: failed to receive fd %d from slave",
915 __func__, i);
916 for (j = 0; j < i; j++)
917 close(new_fd[j]);
918 for (j = 0; j < env_len; j++)
919 xfree(cctx->env[j]);
920 if (env_len > 0)
921 xfree(cctx->env);
922 xfree(cctx->term);
923 buffer_free(&cctx->cmd);
Damien Millerb82f5dd2008-02-10 22:22:53 +1100924 close(client_fd);
Damien Miller54fd7cf2007-09-17 12:04:08 +1000925 xfree(cctx);
926 return;
927 }
928 }
Damien Miller0e220db2004-06-15 10:34:08 +1000929
930 debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
931 new_fd[0], new_fd[1], new_fd[2]);
932
933 /* Try to pick up ttymodes from client before it goes raw */
934 if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
935 error("%s: tcgetattr: %s", __func__, strerror(errno));
936
Darren Tucker7ebfc102004-11-07 20:06:19 +1100937 /* This roundtrip is just for synchronisation of ttymodes */
Damien Miller0e220db2004-06-15 10:34:08 +1000938 buffer_clear(&m);
Damien Miller13390022005-07-06 09:44:19 +1000939 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +1000940 error("%s: client msg_send failed", __func__);
941 close(client_fd);
942 close(new_fd[0]);
943 close(new_fd[1]);
944 close(new_fd[2]);
Damien Miller23f07702004-06-18 01:19:03 +1000945 buffer_free(&m);
Darren Tucker7ebfc102004-11-07 20:06:19 +1100946 xfree(cctx->term);
947 if (env_len != 0) {
948 for (i = 0; i < env_len; i++)
949 xfree(cctx->env[i]);
950 xfree(cctx->env);
951 }
Damien Miller0e220db2004-06-15 10:34:08 +1000952 return;
953 }
954 buffer_free(&m);
955
956 /* enable nonblocking unless tty */
957 if (!isatty(new_fd[0]))
958 set_nonblock(new_fd[0]);
959 if (!isatty(new_fd[1]))
960 set_nonblock(new_fd[1]);
961 if (!isatty(new_fd[2]))
962 set_nonblock(new_fd[2]);
963
964 set_nonblock(client_fd);
965
Darren Tuckerd04188e2007-02-25 20:36:49 +1100966 window = CHAN_SES_WINDOW_DEFAULT;
967 packetmax = CHAN_SES_PACKET_DEFAULT;
968 if (cctx->want_tty) {
969 window >>= 1;
970 packetmax >>= 1;
971 }
972
Darren Tuckerfc959702004-07-17 16:12:08 +1000973 c = channel_new("session", SSH_CHANNEL_OPENING,
Darren Tuckerd04188e2007-02-25 20:36:49 +1100974 new_fd[0], new_fd[1], new_fd[2], window, packetmax,
Damien Miller0e220db2004-06-15 10:34:08 +1000975 CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
976
977 /* XXX */
978 c->ctl_fd = client_fd;
979
980 debug3("%s: channel_new: %d", __func__, c->self);
981
982 channel_send_open(c->self);
Damien Millerb84886b2008-05-19 15:05:07 +1000983 channel_register_open_confirm(c->self,
984 client_extra_session2_setup, cctx);
Damien Miller0e220db2004-06-15 10:34:08 +1000985}
986
987static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000988process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000989{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000990 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100991 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000992 int delete = 0;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000993 int local = 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100994 u_short cancel_port;
995 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000996
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100997 bzero(&fwd, sizeof(fwd));
998 fwd.listen_host = fwd.connect_host = NULL;
999
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001000 leave_raw_mode();
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001001 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +00001002 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001003 if (s == NULL)
1004 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001005 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001006 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +10001007 if (*s == '-')
1008 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +10001009 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001010 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +10001011
Darren Tucker1973c882004-05-24 10:34:36 +10001012 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +10001013 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +10001014 logit(" -L[bind_address:]port:host:hostport "
1015 "Request local forward");
1016 logit(" -R[bind_address:]port:host:hostport "
1017 "Request remote forward");
Damien Miller57e8ad32006-07-10 20:20:52 +10001018 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +10001019 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +11001020 if (!options.permit_local_command)
1021 goto out;
Damien Miller43020952006-07-10 20:16:12 +10001022 logit(" !args "
1023 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +11001024 goto out;
1025 }
1026
1027 if (*s == '!' && options.permit_local_command) {
1028 s++;
1029 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +10001030 goto out;
1031 }
1032
1033 if (*s == 'K') {
1034 delete = 1;
1035 s++;
1036 }
1037 if (*s != 'L' && *s != 'R') {
Damien Miller996acd22003-04-09 20:59:48 +10001038 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001039 goto out;
1040 }
Darren Tuckere7066df2004-05-24 10:18:05 +10001041 if (*s == 'L')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001042 local = 1;
Darren Tuckere7066df2004-05-24 10:18:05 +10001043 if (local && delete) {
1044 logit("Not supported.");
1045 goto out;
1046 }
1047 if ((!local || delete) && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +10001048 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001049 goto out;
1050 }
Darren Tuckere7066df2004-05-24 10:18:05 +10001051
Darren Tucker03b1cdb2007-03-21 20:46:03 +11001052 while (isspace(*++s))
1053 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001054
Darren Tuckere7066df2004-05-24 10:18:05 +10001055 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001056 cancel_port = 0;
1057 cancel_host = hpdelim(&s); /* may be NULL */
1058 if (s != NULL) {
1059 cancel_port = a2port(s);
1060 cancel_host = cleanhostname(cancel_host);
1061 } else {
1062 cancel_port = a2port(cancel_host);
1063 cancel_host = NULL;
1064 }
1065 if (cancel_port == 0) {
1066 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001067 goto out;
1068 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001069 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +10001070 } else {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001071 if (!parse_forward(&fwd, s)) {
Darren Tuckere7066df2004-05-24 10:18:05 +10001072 logit("Bad forwarding specification.");
1073 goto out;
1074 }
Darren Tuckere7066df2004-05-24 10:18:05 +10001075 if (local) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001076 if (channel_setup_local_fwd_listener(fwd.listen_host,
1077 fwd.listen_port, fwd.connect_host,
1078 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +10001079 logit("Port forwarding failed.");
1080 goto out;
1081 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001082 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001083 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001084 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +10001085 fwd.connect_port) < 0) {
1086 logit("Port forwarding failed.");
1087 goto out;
1088 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001089 }
1090
Darren Tuckere7066df2004-05-24 10:18:05 +10001091 logit("Forwarding port.");
1092 }
1093
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001094out:
1095 signal(SIGINT, handler);
1096 enter_raw_mode();
1097 if (cmd)
1098 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +11001099 if (fwd.listen_host != NULL)
1100 xfree(fwd.listen_host);
1101 if (fwd.connect_host != NULL)
1102 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001103}
1104
Damien Millerad833b32000-08-23 10:46:23 +10001105/* process the characters one by one */
Ben Lindstrombba81212001-06-25 05:01:22 +00001106static int
Damien Millerad833b32000-08-23 10:46:23 +10001107process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
1108{
1109 char string[1024];
1110 pid_t pid;
1111 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001112 u_int i;
1113 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001114 char *s;
1115
Damien Millereccb9de2005-06-17 12:59:34 +10001116 if (len <= 0)
1117 return (0);
1118
1119 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001120 /* Get one character at a time. */
1121 ch = buf[i];
1122
1123 if (escape_pending) {
1124 /* We have previously seen an escape character. */
1125 /* Clear the flag now. */
1126 escape_pending = 0;
1127
1128 /* Process the escaped character. */
1129 switch (ch) {
1130 case '.':
1131 /* Terminate the connection. */
1132 snprintf(string, sizeof string, "%c.\r\n", escape_char);
1133 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001134
1135 quit_pending = 1;
1136 return -1;
1137
1138 case 'Z' - 64:
1139 /* Suspend the program. */
1140 /* Print a message to that effect to the user. */
1141 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
1142 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001143
1144 /* Restore terminal modes and suspend. */
1145 client_suspend_self(bin, bout, berr);
1146
1147 /* We have been continued. */
1148 continue;
1149
Damien Miller54c45982003-05-15 10:20:13 +10001150 case 'B':
1151 if (compat20) {
1152 snprintf(string, sizeof string,
1153 "%cB\r\n", escape_char);
1154 buffer_append(berr, string,
1155 strlen(string));
1156 channel_request_start(session_ident,
1157 "break", 0);
1158 packet_put_int(1000);
1159 packet_send();
1160 }
1161 continue;
1162
Ben Lindstromf28f6342001-04-04 02:03:04 +00001163 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001164 if (compat20) {
1165 if (datafellows & SSH_BUG_NOREKEY)
Damien Miller996acd22003-04-09 20:59:48 +10001166 logit("Server does not support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001167 else
1168 need_rekeying = 1;
1169 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001170 continue;
1171
Damien Millerad833b32000-08-23 10:46:23 +10001172 case '&':
Damien Millerad833b32000-08-23 10:46:23 +10001173 /*
1174 * Detach the program (continue to serve connections,
1175 * but put in background and no more new connections).
1176 */
Damien Miller96507ef2001-11-12 10:52:25 +11001177 /* Restore tty modes. */
1178 leave_raw_mode();
1179
1180 /* Stop listening for new connections. */
1181 channel_stop_listening();
1182
1183 snprintf(string, sizeof string,
1184 "%c& [backgrounded]\n", escape_char);
1185 buffer_append(berr, string, strlen(string));
1186
1187 /* Fork into background. */
1188 pid = fork();
1189 if (pid < 0) {
1190 error("fork: %.100s", strerror(errno));
1191 continue;
1192 }
1193 if (pid != 0) { /* This is the parent. */
1194 /* The parent just exits. */
1195 exit(0);
1196 }
1197 /* The child continues serving connections. */
1198 if (compat20) {
1199 buffer_append(bin, "\004", 1);
1200 /* fake EOF on stdin */
1201 return -1;
1202 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001203 /*
1204 * Sending SSH_CMSG_EOF alone does not always appear
1205 * to be enough. So we try to send an EOF character
1206 * first.
1207 */
1208 packet_start(SSH_CMSG_STDIN_DATA);
1209 packet_put_string("\004", 1);
1210 packet_send();
1211 /* Close stdin. */
1212 stdin_eof = 1;
1213 if (buffer_len(bin) == 0) {
1214 packet_start(SSH_CMSG_EOF);
1215 packet_send();
1216 }
1217 }
Damien Miller96507ef2001-11-12 10:52:25 +11001218 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001219
1220 case '?':
1221 snprintf(string, sizeof string,
1222"%c?\r\n\
1223Supported escape sequences:\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001224%c. - terminate connection\r\n\
Damien Miller54c45982003-05-15 10:20:13 +10001225%cB - send a BREAK to the remote system\r\n\
Damien Miller06692862002-09-04 16:32:10 +10001226%cC - open a command line\r\n\
1227%cR - Request rekey (SSH protocol 2 only)\r\n\
1228%c^Z - suspend ssh\r\n\
1229%c# - list forwarded connections\r\n\
1230%c& - background ssh (when waiting for connections to terminate)\r\n\
1231%c? - this message\r\n\
1232%c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001233(Note that escapes are only recognized immediately after newline.)\r\n",
Damien Miller06692862002-09-04 16:32:10 +10001234 escape_char, escape_char, escape_char, escape_char,
1235 escape_char, escape_char, escape_char, escape_char,
Damien Miller54c45982003-05-15 10:20:13 +10001236 escape_char, escape_char, escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001237 buffer_append(berr, string, strlen(string));
1238 continue;
1239
1240 case '#':
1241 snprintf(string, sizeof string, "%c#\r\n", escape_char);
1242 buffer_append(berr, string, strlen(string));
1243 s = channel_open_message();
1244 buffer_append(berr, s, strlen(s));
1245 xfree(s);
1246 continue;
1247
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001248 case 'C':
Ben Lindstrom681d9322002-03-22 03:53:00 +00001249 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001250 continue;
1251
Damien Millerad833b32000-08-23 10:46:23 +10001252 default:
1253 if (ch != escape_char) {
1254 buffer_put_char(bin, escape_char);
1255 bytes++;
1256 }
1257 /* Escaped characters fall through here */
1258 break;
1259 }
1260 } else {
1261 /*
1262 * The previous character was not an escape char. Check if this
1263 * is an escape.
1264 */
1265 if (last_was_cr && ch == escape_char) {
1266 /* It is. Set the flag and continue to next character. */
1267 escape_pending = 1;
1268 continue;
1269 }
1270 }
1271
1272 /*
1273 * Normal character. Record whether it was a newline,
1274 * and append it to the buffer.
1275 */
1276 last_was_cr = (ch == '\r' || ch == '\n');
1277 buffer_put_char(bin, ch);
1278 bytes++;
1279 }
1280 return bytes;
1281}
1282
Ben Lindstrombba81212001-06-25 05:01:22 +00001283static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001284client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001285{
Damien Miller166fca82000-04-20 07:42:21 +10001286 int len;
Damien Millerad833b32000-08-23 10:46:23 +10001287 char buf[8192];
Damien Miller1383bd82000-04-06 12:32:37 +10001288
Damien Miller95def091999-11-25 00:26:21 +11001289 /* Read input from stdin. */
1290 if (FD_ISSET(fileno(stdin), readset)) {
1291 /* Read as much as possible. */
1292 len = read(fileno(stdin), buf, sizeof(buf));
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001293 if (len < 0 && (errno == EAGAIN || errno == EINTR))
1294 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001295 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001296 /*
1297 * Received EOF or error. They are treated
1298 * similarly, except that an error message is printed
1299 * if it was an error condition.
1300 */
Damien Miller95def091999-11-25 00:26:21 +11001301 if (len < 0) {
1302 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
1303 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001304 }
1305 /* Mark that we have seen EOF. */
1306 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001307 /*
1308 * Send an EOF message to the server unless there is
1309 * data in the buffer. If there is data in the
1310 * buffer, no message will be sent now. Code
1311 * elsewhere will send the EOF when the buffer
1312 * becomes empty if stdin_eof is set.
1313 */
Damien Miller95def091999-11-25 00:26:21 +11001314 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315 packet_start(SSH_CMSG_EOF);
1316 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001317 }
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001318 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001319 /*
1320 * Normal successful read, and no escape character.
1321 * Just append the data to buffer.
1322 */
Damien Miller95def091999-11-25 00:26:21 +11001323 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001324 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001325 /*
1326 * Normal, successful read. But we have an escape character
1327 * and have to process the characters one by one.
1328 */
Ben Lindstrome9613cf2001-03-05 06:14:02 +00001329 if (process_escapes(&stdin_buffer, &stdout_buffer,
1330 &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001331 return;
Damien Miller95def091999-11-25 00:26:21 +11001332 }
1333 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001334}
1335
Ben Lindstrombba81212001-06-25 05:01:22 +00001336static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001337client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338{
Damien Miller95def091999-11-25 00:26:21 +11001339 int len;
1340 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001341
Damien Miller95def091999-11-25 00:26:21 +11001342 /* Write buffered output to stdout. */
1343 if (FD_ISSET(fileno(stdout), writeset)) {
1344 /* Write as much data as possible. */
1345 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001346 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001347 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001348 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001349 len = 0;
1350 else {
Damien Miller5428f641999-11-25 11:54:57 +11001351 /*
1352 * An error or EOF was encountered. Put an
1353 * error message to stderr buffer.
1354 */
Damien Miller95def091999-11-25 00:26:21 +11001355 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
1356 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001357 quit_pending = 1;
1358 return;
1359 }
1360 }
1361 /* Consume printed data from the buffer. */
1362 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001363 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364 }
Damien Miller95def091999-11-25 00:26:21 +11001365 /* Write buffered output to stderr. */
1366 if (FD_ISSET(fileno(stderr), writeset)) {
1367 /* Write as much data as possible. */
1368 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001369 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001370 if (len <= 0) {
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001371 if (errno == EINTR || errno == EAGAIN)
Damien Miller95def091999-11-25 00:26:21 +11001372 len = 0;
1373 else {
Damien Miller5428f641999-11-25 11:54:57 +11001374 /* EOF or error, but can't even print error message. */
Damien Miller95def091999-11-25 00:26:21 +11001375 quit_pending = 1;
1376 return;
1377 }
1378 }
1379 /* Consume printed characters from the buffer. */
1380 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001381 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001382 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001383}
1384
Damien Miller5428f641999-11-25 11:54:57 +11001385/*
Damien Millerb38eff82000-04-01 11:09:21 +10001386 * Get packets from the connection input buffer, and process them as long as
1387 * there are packets available.
1388 *
1389 * Any unknown packets received during the actual
1390 * session cause the session to terminate. This is
1391 * intended to make debugging easier since no
1392 * confirmations are sent. Any compatible protocol
1393 * extensions must be negotiated during the
1394 * preparatory phase.
1395 */
1396
Ben Lindstrombba81212001-06-25 05:01:22 +00001397static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001398client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001399{
Ben Lindstromf28f6342001-04-04 02:03:04 +00001400 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001401}
1402
Damien Millerad833b32000-08-23 10:46:23 +10001403/* scan buf[] for '~' before sending data to the peer */
1404
Ben Lindstrombba81212001-06-25 05:01:22 +00001405static int
Damien Millerad833b32000-08-23 10:46:23 +10001406simple_escape_filter(Channel *c, char *buf, int len)
1407{
Damien Miller5771ed72008-05-19 15:35:33 +10001408 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1409 return 0;
1410
Damien Millerad833b32000-08-23 10:46:23 +10001411 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1412}
1413
Ben Lindstrombba81212001-06-25 05:01:22 +00001414static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001415client_channel_closed(int id, void *arg)
1416{
Damien Miller3ec27592001-10-12 11:35:04 +10001417 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001418 session_closed = 1;
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001419 leave_raw_mode();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001420}
1421
Damien Millerb38eff82000-04-01 11:09:21 +10001422/*
Damien Miller5428f641999-11-25 11:54:57 +11001423 * Implements the interactive session with the server. This is called after
1424 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001425 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1426 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001427 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428
Damien Miller4af51302000-04-16 11:18:38 +10001429int
Damien Millerad833b32000-08-23 10:46:23 +10001430client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431{
Damien Miller5e953212001-01-30 09:14:00 +11001432 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001433 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001434 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1435 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001436 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437
Damien Miller95def091999-11-25 00:26:21 +11001438 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439
Damien Miller95def091999-11-25 00:26:21 +11001440 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441
Damien Miller95def091999-11-25 00:26:21 +11001442 /* Initialize variables. */
1443 escape_pending = 0;
1444 last_was_cr = 1;
1445 exit_status = -1;
1446 stdin_eof = 0;
1447 buffer_high = 64 * 1024;
1448 connection_in = packet_get_connection_in();
1449 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001450 max_fd = MAX(connection_in, connection_out);
Damien Miller0e220db2004-06-15 10:34:08 +10001451 if (control_fd != -1)
1452 max_fd = MAX(max_fd, control_fd);
Damien Miller5e953212001-01-30 09:14:00 +11001453
1454 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001455 /* enable nonblocking unless tty */
1456 if (!isatty(fileno(stdin)))
1457 set_nonblock(fileno(stdin));
1458 if (!isatty(fileno(stdout)))
1459 set_nonblock(fileno(stdout));
1460 if (!isatty(fileno(stderr)))
1461 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001462 max_fd = MAX(max_fd, fileno(stdin));
1463 max_fd = MAX(max_fd, fileno(stdout));
1464 max_fd = MAX(max_fd, fileno(stderr));
1465 }
Damien Miller95def091999-11-25 00:26:21 +11001466 stdin_bytes = 0;
1467 stdout_bytes = 0;
1468 stderr_bytes = 0;
1469 quit_pending = 0;
1470 escape_char = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001471
Damien Miller95def091999-11-25 00:26:21 +11001472 /* Initialize buffers. */
1473 buffer_init(&stdin_buffer);
1474 buffer_init(&stdout_buffer);
1475 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476
Damien Millerb38eff82000-04-01 11:09:21 +10001477 client_init_dispatch();
1478
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001479 /*
1480 * Set signal handlers, (e.g. to restore non-blocking mode)
1481 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1482 */
Darren Tucker07336da2004-11-05 20:02:16 +11001483 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1484 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001485 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1486 signal(SIGINT, signal_handler);
1487 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1488 signal(SIGQUIT, signal_handler);
1489 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1490 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001491 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
Damien Miller95def091999-11-25 00:26:21 +11001493 if (have_pty)
1494 enter_raw_mode();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495
Ben Lindstrom5b828322001-02-09 01:34:36 +00001496 if (compat20) {
1497 session_ident = ssh2_chan_id;
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001498 if (escape_char != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001499 channel_register_filter(session_ident,
Damien Miller077b2382005-12-31 16:22:32 +11001500 simple_escape_filter, NULL);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001501 if (session_ident != -1)
1502 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001503 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001504 } else {
1505 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001506 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001507 }
Damien Millerad833b32000-08-23 10:46:23 +10001508
Damien Miller95def091999-11-25 00:26:21 +11001509 /* Main loop of the client for the interactive session mode. */
1510 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511
Damien Miller5428f641999-11-25 11:54:57 +11001512 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001513 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001514
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001515 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001516 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001517
1518 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001519
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001520 if (rekeying) {
1521 debug("rekeying in progress");
1522 } else {
1523 /*
1524 * Make packets of buffered stdin data, and buffer
1525 * them for sending to the server.
1526 */
1527 if (!compat20)
1528 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001529
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001530 /*
1531 * Make packets from buffered channel data, and
1532 * enqueue them for sending to the server.
1533 */
1534 if (packet_not_very_much_data_to_write())
1535 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001537 /*
1538 * Check if the window size has changed, and buffer a
1539 * message about it to the server if so.
1540 */
1541 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001542
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001543 if (quit_pending)
1544 break;
1545 }
Damien Miller5428f641999-11-25 11:54:57 +11001546 /*
1547 * Wait until we have something to do (something becomes
1548 * available on one of the descriptors).
1549 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001550 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001551 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001552 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553
Damien Miller95def091999-11-25 00:26:21 +11001554 if (quit_pending)
1555 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001556
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001557 /* Do channel operations unless rekeying in progress. */
1558 if (!rekeying) {
1559 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001560 if (need_rekeying || packet_need_rekeying()) {
1561 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001562 xxx_kex->done = 0;
1563 kex_send_kexinit(xxx_kex);
1564 need_rekeying = 0;
1565 }
1566 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567
Damien Miller1383bd82000-04-06 12:32:37 +10001568 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001569 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001570
Damien Miller0e220db2004-06-15 10:34:08 +10001571 /* Accept control connections. */
1572 client_process_control(readset);
1573
Damien Miller1383bd82000-04-06 12:32:37 +10001574 if (quit_pending)
1575 break;
1576
1577 if (!compat20) {
1578 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001579 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001580 /*
1581 * Process output to stdout and stderr. Output to
1582 * the connection is processed elsewhere (above).
1583 */
Damien Miller5e953212001-01-30 09:14:00 +11001584 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001585 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001586
Damien Miller5428f641999-11-25 11:54:57 +11001587 /* Send as much buffered packet data as possible to the sender. */
Damien Miller5e953212001-01-30 09:14:00 +11001588 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001589 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001590 }
Damien Miller5e953212001-01-30 09:14:00 +11001591 if (readset)
1592 xfree(readset);
1593 if (writeset)
1594 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001595
Damien Miller95def091999-11-25 00:26:21 +11001596 /* Terminate the session. */
1597
1598 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001599 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001600
Ben Lindstrom601e4362001-06-21 03:19:23 +00001601 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001602
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001603 if (have_pty)
1604 leave_raw_mode();
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001605
1606 /* restore blocking io */
1607 if (!isatty(fileno(stdin)))
1608 unset_nonblock(fileno(stdin));
1609 if (!isatty(fileno(stdout)))
1610 unset_nonblock(fileno(stdout));
1611 if (!isatty(fileno(stderr)))
1612 unset_nonblock(fileno(stderr));
1613
Damien Millerd6965512003-12-17 16:31:53 +11001614 /*
1615 * If there was no shell or command requested, there will be no remote
1616 * exit status to be returned. In that case, clear error code if the
1617 * connection was deliberately terminated at this end.
1618 */
1619 if (no_shell_flag && received_signal == SIGTERM) {
1620 received_signal = 0;
1621 exit_status = 0;
1622 }
1623
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001624 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001625 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001626
1627 /*
1628 * In interactive mode (with pseudo tty) display a message indicating
1629 * that the connection has been closed.
1630 */
1631 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1632 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1633 buffer_append(&stderr_buffer, buf, strlen(buf));
1634 }
1635
Damien Miller95def091999-11-25 00:26:21 +11001636 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001637 while (buffer_len(&stdout_buffer) > 0) {
1638 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001639 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001640 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001641 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001642 break;
1643 }
Damien Miller95def091999-11-25 00:26:21 +11001644 buffer_consume(&stdout_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001645 stdout_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001646 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001647
Damien Miller95def091999-11-25 00:26:21 +11001648 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001649 while (buffer_len(&stderr_buffer) > 0) {
1650 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001651 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001652 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001653 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001654 break;
1655 }
Damien Miller95def091999-11-25 00:26:21 +11001656 buffer_consume(&stderr_buffer, len);
Ben Lindstroma3700052001-04-05 23:26:32 +00001657 stderr_bytes += len;
Damien Miller95def091999-11-25 00:26:21 +11001658 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001659
Damien Miller95def091999-11-25 00:26:21 +11001660 /* Clear and free any buffers. */
1661 memset(buf, 0, sizeof(buf));
1662 buffer_free(&stdin_buffer);
1663 buffer_free(&stdout_buffer);
1664 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001665
Damien Miller95def091999-11-25 00:26:21 +11001666 /* Report bytes transferred, and transfer rates. */
1667 total_time = get_current_time() - start_time;
1668 debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001669 stdin_bytes, stdout_bytes, stderr_bytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001670 if (total_time > 0)
1671 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001672 stdin_bytes / total_time, stdout_bytes / total_time,
1673 stderr_bytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001674
1675 /* Return the exit status of the program. */
1676 debug("Exit status %d", exit_status);
1677 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001678}
Damien Millerb38eff82000-04-01 11:09:21 +10001679
1680/*********/
1681
Ben Lindstrombba81212001-06-25 05:01:22 +00001682static void
Damien Miller630d6f42002-01-22 23:17:30 +11001683client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001684{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001685 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001686 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001687 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001688 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001689 memset(data, 0, data_len);
1690 xfree(data);
1691}
Ben Lindstrombba81212001-06-25 05:01:22 +00001692static void
Damien Miller630d6f42002-01-22 23:17:30 +11001693client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001694{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001695 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001696 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001697 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001698 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001699 memset(data, 0, data_len);
1700 xfree(data);
1701}
Ben Lindstrombba81212001-06-25 05:01:22 +00001702static void
Damien Miller630d6f42002-01-22 23:17:30 +11001703client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001704{
Damien Millerb38eff82000-04-01 11:09:21 +10001705 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001706 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001707 /* Acknowledge the exit. */
1708 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1709 packet_send();
1710 /*
1711 * Must wait for packet to be sent since we are
1712 * exiting the loop.
1713 */
1714 packet_write_wait();
1715 /* Flag that we want to exit. */
1716 quit_pending = 1;
1717}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001718static void
1719client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1720{
1721 Channel *c = NULL;
1722 int remote_id, sock;
1723
1724 /* Read the remote channel number from the message. */
1725 remote_id = packet_get_int();
1726 packet_check_eom();
1727
1728 /*
1729 * Get a connection to the local authentication agent (this may again
1730 * get forwarded).
1731 */
1732 sock = ssh_get_authentication_socket();
1733
1734 /*
1735 * If we could not connect the agent, send an error message back to
1736 * the server. This should never happen unless the agent dies,
1737 * because authentication forwarding is only enabled if we have an
1738 * agent.
1739 */
1740 if (sock >= 0) {
1741 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1742 -1, 0, 0, 0, "authentication agent connection", 1);
1743 c->remote_id = remote_id;
1744 c->force_drain = 1;
1745 }
1746 if (c == NULL) {
1747 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1748 packet_put_int(remote_id);
1749 } else {
1750 /* Send a confirmation to the remote host. */
1751 debug("Forwarding authentication connection.");
1752 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1753 packet_put_int(remote_id);
1754 packet_put_int(c->self);
1755 }
1756 packet_send();
1757}
Damien Millerb38eff82000-04-01 11:09:21 +10001758
Ben Lindstrombba81212001-06-25 05:01:22 +00001759static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001760client_request_forwarded_tcpip(const char *request_type, int rchan)
1761{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001762 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001763 char *listen_address, *originator_address;
1764 int listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001765
1766 /* Get rest of the packet */
1767 listen_address = packet_get_string(NULL);
1768 listen_port = packet_get_int();
1769 originator_address = packet_get_string(NULL);
1770 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001771 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001772
Damien Millerbd740252008-05-19 15:37:09 +10001773 debug("client_request_forwarded_tcpip: listen %s port %d, "
1774 "originator %s port %d", listen_address, listen_port,
1775 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001776
Damien Millerbd740252008-05-19 15:37:09 +10001777 c = channel_connect_by_listen_address(listen_port,
1778 "forwarded-tcpip", originator_address);
1779
Damien Miller0bc1bd82000-11-13 22:57:25 +11001780 xfree(originator_address);
1781 xfree(listen_address);
1782 return c;
1783}
1784
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001785static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001786client_request_x11(const char *request_type, int rchan)
1787{
1788 Channel *c = NULL;
1789 char *originator;
1790 int originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001791 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001792
1793 if (!options.forward_x11) {
1794 error("Warning: ssh server tried X11 forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001795 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001796 return NULL;
1797 }
1798 originator = packet_get_string(NULL);
1799 if (datafellows & SSH_BUG_X11FWD) {
1800 debug2("buggy server: x11 request w/o originator_port");
1801 originator_port = 0;
1802 } else {
1803 originator_port = packet_get_int();
1804 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001805 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001806 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001807 debug("client_request_x11: request from %s %d", originator,
1808 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001809 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001810 sock = x11_connect_display();
1811 if (sock < 0)
1812 return NULL;
1813 c = channel_new("x11",
1814 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001815 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001816 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001817 return c;
1818}
1819
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001820static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001821client_request_agent(const char *request_type, int rchan)
1822{
1823 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001824 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001825
1826 if (!options.forward_agent) {
1827 error("Warning: ssh server tried agent forwarding.");
Damien Miller5eb137c2005-12-31 16:19:53 +11001828 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001829 return NULL;
1830 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001831 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001832 if (sock < 0)
1833 return NULL;
1834 c = channel_new("authentication agent connection",
1835 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001836 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001837 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001838 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001839 return c;
1840}
1841
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001842int
1843client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1844{
1845 Channel *c;
1846 int fd;
1847
1848 if (tun_mode == SSH_TUNMODE_NO)
1849 return 0;
1850
1851 if (!compat20) {
1852 error("Tunnel forwarding is not support for protocol 1");
1853 return -1;
1854 }
1855
1856 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1857
1858 /* Open local tunnel device */
1859 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1860 error("Tunnel device open failed.");
1861 return -1;
1862 }
1863
1864 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1865 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1866 c->datagram = 1;
1867
1868#if defined(SSH_TUN_FILTER)
1869 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1870 channel_register_filter(c->self, sys_tun_infilter,
1871 sys_tun_outfilter);
1872#endif
1873
1874 packet_start(SSH2_MSG_CHANNEL_OPEN);
1875 packet_put_cstring("tun@openssh.com");
1876 packet_put_int(c->self);
1877 packet_put_int(c->local_window_max);
1878 packet_put_int(c->local_maxpacket);
1879 packet_put_int(tun_mode);
1880 packet_put_int(remote_tun);
1881 packet_send();
1882
1883 return 0;
1884}
1885
Damien Millerbd483e72000-04-30 10:00:53 +10001886/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001887static void
Damien Miller630d6f42002-01-22 23:17:30 +11001888client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001889{
1890 Channel *c = NULL;
1891 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001892 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001893 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001894
1895 ctype = packet_get_string(&len);
1896 rchan = packet_get_int();
1897 rwindow = packet_get_int();
1898 rmaxpack = packet_get_int();
1899
Damien Millere247cc42000-05-07 12:03:14 +10001900 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001901 ctype, rchan, rwindow, rmaxpack);
1902
Damien Miller0bc1bd82000-11-13 22:57:25 +11001903 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1904 c = client_request_forwarded_tcpip(ctype, rchan);
1905 } else if (strcmp(ctype, "x11") == 0) {
1906 c = client_request_x11(ctype, rchan);
1907 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1908 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001909 }
1910/* XXX duplicate : */
1911 if (c != NULL) {
1912 debug("confirm %s", ctype);
1913 c->remote_id = rchan;
1914 c->remote_window = rwindow;
1915 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001916 if (c->type != SSH_CHANNEL_CONNECTING) {
1917 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1918 packet_put_int(c->remote_id);
1919 packet_put_int(c->self);
1920 packet_put_int(c->local_window);
1921 packet_put_int(c->local_maxpacket);
1922 packet_send();
1923 }
Damien Millerbd483e72000-04-30 10:00:53 +10001924 } else {
1925 debug("failure %s", ctype);
1926 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1927 packet_put_int(rchan);
1928 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001929 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001930 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001931 packet_put_cstring("");
1932 }
Damien Millerbd483e72000-04-30 10:00:53 +10001933 packet_send();
1934 }
1935 xfree(ctype);
1936}
Ben Lindstrombba81212001-06-25 05:01:22 +00001937static void
Damien Miller630d6f42002-01-22 23:17:30 +11001938client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001939{
1940 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001941 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001942 char *rtype;
1943
1944 id = packet_get_int();
1945 rtype = packet_get_string(NULL);
1946 reply = packet_get_char();
1947
1948 debug("client_input_channel_req: channel %d rtype %s reply %d",
1949 id, rtype, reply);
1950
Damien Miller3bbd8782004-06-18 22:23:22 +10001951 if (id == -1) {
1952 error("client_input_channel_req: request for channel -1");
1953 } else if ((c = channel_lookup(id)) == NULL) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00001954 error("client_input_channel_req: channel %d: unknown channel", id);
1955 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001956 exitval = packet_get_int();
1957 if (id == session_ident) {
1958 success = 1;
1959 exit_status = exitval;
1960 } else if (c->ctl_fd == -1) {
1961 error("client_input_channel_req: unexpected channel %d",
1962 session_ident);
1963 } else {
1964 atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
1965 success = 1;
1966 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001967 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001968 }
1969 if (reply) {
1970 packet_start(success ?
1971 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller3bbd8782004-06-18 22:23:22 +10001972 packet_put_int(id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001973 packet_send();
1974 }
1975 xfree(rtype);
1976}
Damien Millerc3fa4072002-01-22 23:21:58 +11001977static void
1978client_input_global_request(int type, u_int32_t seq, void *ctxt)
1979{
1980 char *rtype;
1981 int want_reply;
1982 int success = 0;
1983
1984 rtype = packet_get_string(NULL);
1985 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001986 debug("client_input_global_request: rtype %s want_reply %d",
1987 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001988 if (want_reply) {
1989 packet_start(success ?
1990 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1991 packet_send();
1992 packet_write_wait();
1993 }
1994 xfree(rtype);
1995}
Damien Millerbd483e72000-04-30 10:00:53 +10001996
Damien Miller0e220db2004-06-15 10:34:08 +10001997void
Darren Tuckerfc959702004-07-17 16:12:08 +10001998client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10001999 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002000{
2001 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002002 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002003
2004 debug2("%s: id %d", __func__, id);
2005
Darren Tucker5d78de62004-11-05 20:35:44 +11002006 if ((c = channel_lookup(id)) == NULL)
2007 fatal("client_session2_setup: channel %d: unknown channel", id);
2008
Damien Miller0e220db2004-06-15 10:34:08 +10002009 if (want_tty) {
2010 struct winsize ws;
2011 struct termios tio;
2012
2013 /* Store window size in the packet. */
2014 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2015 memset(&ws, 0, sizeof(ws));
2016
Damien Miller5771ed72008-05-19 15:35:33 +10002017 channel_request_start(id, "pty-req", 1);
2018 client_expect_confirm(id, "PTY allocation", 0);
Damien Miller0e220db2004-06-15 10:34:08 +10002019 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002020 packet_put_int((u_int)ws.ws_col);
2021 packet_put_int((u_int)ws.ws_row);
2022 packet_put_int((u_int)ws.ws_xpixel);
2023 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002024 tio = get_saved_tio();
2025 tty_make_modes(-1, tiop != NULL ? tiop : &tio);
2026 packet_send();
2027 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002028 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002029 }
2030
2031 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002032 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002033 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002034 char *name, *val;
2035
2036 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002037 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002038 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002039 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002040 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11002041 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002042 continue;
2043 }
2044 *val++ = '\0';
2045
2046 matched = 0;
2047 for (j = 0; j < options.num_send_env; j++) {
2048 if (match_pattern(name, options.send_env[j])) {
2049 matched = 1;
2050 break;
2051 }
2052 }
2053 if (!matched) {
2054 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11002055 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002056 continue;
2057 }
2058
2059 debug("Sending env %s = %s", name, val);
2060 channel_request_start(id, "env", 0);
2061 packet_put_cstring(name);
2062 packet_put_cstring(val);
2063 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11002064 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002065 }
2066 }
2067
2068 len = buffer_len(cmd);
2069 if (len > 0) {
2070 if (len > 900)
2071 len = 900;
2072 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002073 debug("Sending subsystem: %.*s",
2074 len, (u_char*)buffer_ptr(cmd));
2075 channel_request_start(id, "subsystem", 1);
2076 client_expect_confirm(id, "subsystem", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002077 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002078 debug("Sending command: %.*s",
2079 len, (u_char*)buffer_ptr(cmd));
2080 channel_request_start(id, "exec", 1);
2081 client_expect_confirm(id, "exec", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002082 }
2083 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2084 packet_send();
2085 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002086 channel_request_start(id, "shell", 1);
2087 client_expect_confirm(id, "shell", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10002088 packet_send();
2089 }
2090}
2091
Ben Lindstrombba81212001-06-25 05:01:22 +00002092static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002093client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002094{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002095 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002096
Damien Miller1383bd82000-04-06 12:32:37 +10002097 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2098 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2099 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2100 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002101 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002102 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2103 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002104 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002105 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002106 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2107 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002108 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002109
2110 /* rekeying */
2111 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002112
2113 /* global request reply messages */
2114 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2115 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002116}
Ben Lindstrombba81212001-06-25 05:01:22 +00002117static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002118client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002119{
2120 dispatch_init(NULL);
2121 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2122 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2123 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002124 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2125 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2126 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002127 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2128 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2129 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002130
2131 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002132 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002133 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2134 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002135}
Ben Lindstrombba81212001-06-25 05:01:22 +00002136static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002137client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002138{
2139 client_init_dispatch_13();
2140 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2141 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2142}
Ben Lindstromdb47f382001-07-04 05:10:27 +00002143static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002144client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002145{
Damien Miller1383bd82000-04-06 12:32:37 +10002146 if (compat20)
2147 client_init_dispatch_20();
2148 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002149 client_init_dispatch_13();
2150 else
2151 client_init_dispatch_15();
2152}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002153
2154/* client specific fatal cleanup */
2155void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002156cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002157{
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002158 leave_raw_mode();
2159 leave_non_blocking();
Damien Miller0e220db2004-06-15 10:34:08 +10002160 if (options.control_path != NULL && control_fd != -1)
2161 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002162 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002163}