blob: 5793a6e91e2fca9d9a7dc10b84a68f6ab17b5313 [file] [log] [blame]
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001/* $OpenBSD: clientloop.c,v 1.216 2010/01/09 05:04:24 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 "match.h"
111#include "msg.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +1000112#include "roaming.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 */
Damien Millerb1cbfa22008-05-19 16:00:08 +1000124extern int muxserver_sock;
Damien Miller0e220db2004-06-15 10:34:08 +1000125
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
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100133/* Force TTY allocation */
134extern int force_tty_flag;
135
Damien Miller5428f641999-11-25 11:54:57 +1100136/*
137 * Flag to indicate that we have received a window change signal which has
138 * not yet been processed. This will cause a message indicating the new
139 * window size to be sent to the server a little later. This is volatile
140 * because this is updated in a signal handler.
141 */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000142static volatile sig_atomic_t received_window_change_signal = 0;
143static volatile sig_atomic_t received_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller788f2122005-11-05 15:14:59 +1100145/* Flag indicating whether the user's terminal is in non-blocking mode. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146static int in_non_blocking_mode = 0;
147
148/* Common data for the client loop code. */
Damien Miller97c91f62006-04-23 12:08:37 +1000149static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000150static int escape_char1; /* Escape character. (proto1 only) */
151static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100152static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000153static int exit_status; /* Used to store the command exit status. */
154static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100155static Buffer stdin_buffer; /* Buffer for stdin data. */
156static Buffer stdout_buffer; /* Buffer for stdout data. */
157static Buffer stderr_buffer; /* Buffer for stderr data. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000158static u_int buffer_high;/* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100159static int connection_in; /* Connection to server (input). */
160static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000161static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +0000162static int session_closed = 0; /* In SSH2: login session closed. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000165int session_ident = -1;
166
Darren Tuckere32cf432010-01-08 16:51:40 +1100167int session_resumed = 0;
168
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000169/* Track escape per proto2 channel */
170struct escape_filter_ctx {
171 int escape_pending;
172 int escape_char;
173};
174
175/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000176struct channel_reply_ctx {
177 const char *request_type;
178 int id, do_close;
179};
180
Darren Tucker9f407c42008-06-13 04:50:27 +1000181/* Global request success/failure callbacks */
182struct global_confirm {
183 TAILQ_ENTRY(global_confirm) entry;
184 global_confirm_cb *cb;
185 void *ctx;
186 int ref_count;
187};
188TAILQ_HEAD(global_confirms, global_confirm);
189static struct global_confirms global_confirms =
190 TAILQ_HEAD_INITIALIZER(global_confirms);
191
Ben Lindstromf28f6342001-04-04 02:03:04 +0000192/*XXX*/
193extern Kex *xxx_kex;
194
Damien Miller0e220db2004-06-15 10:34:08 +1000195void ssh_process_session2_setup(int, int, int, Buffer *);
196
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197/* Restores stdin to blocking mode. */
198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000200leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201{
Damien Miller95def091999-11-25 00:26:21 +1100202 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000203 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100204 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100205 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206}
207
Damien Miller95def091999-11-25 00:26:21 +1100208/* Puts stdin terminal in non-blocking mode. */
209
Ben Lindstrombba81212001-06-25 05:01:22 +0000210static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000211enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212{
Damien Miller95def091999-11-25 00:26:21 +1100213 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000214 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215}
216
Damien Miller5428f641999-11-25 11:54:57 +1100217/*
218 * Signal handler for the window change signal (SIGWINCH). This just sets a
219 * flag indicating that the window has changed.
220 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100221/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000222static void
Damien Miller95def091999-11-25 00:26:21 +1100223window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224{
Damien Miller95def091999-11-25 00:26:21 +1100225 received_window_change_signal = 1;
226 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227}
228
Damien Miller5428f641999-11-25 11:54:57 +1100229/*
230 * Signal handler for signals that cause the program to terminate. These
231 * signals must be trapped to restore terminal modes.
232 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100233/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static void
Damien Miller95def091999-11-25 00:26:21 +1100235signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000237 received_signal = sig;
238 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
Damien Miller5428f641999-11-25 11:54:57 +1100241/*
242 * Returns current time in seconds from Jan 1, 1970 with the maximum
243 * available resolution.
244 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Ben Lindstrombba81212001-06-25 05:01:22 +0000246static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000247get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248{
Damien Miller95def091999-11-25 00:26:21 +1100249 struct timeval tv;
250 gettimeofday(&tv, NULL);
251 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252}
253
Damien Miller17e7ed02005-06-17 12:54:33 +1000254#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
255void
256client_x11_get_proto(const char *display, const char *xauth_path,
257 u_int trusted, char **_proto, char **_data)
258{
259 char cmd[1024];
260 char line[512];
261 char xdisplay[512];
262 static char proto[512], data[512];
263 FILE *f;
264 int got_data = 0, generated = 0, do_unlink = 0, i;
265 char *xauthdir, *xauthfile;
266 struct stat st;
267
268 xauthdir = xauthfile = NULL;
269 *_proto = proto;
270 *_data = data;
271 proto[0] = data[0] = '\0';
272
273 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
274 debug("No xauth program.");
275 } else {
276 if (display == NULL) {
277 debug("x11_get_proto: DISPLAY not set");
278 return;
279 }
280 /*
281 * Handle FamilyLocal case where $DISPLAY does
282 * not match an authorization entry. For this we
283 * just try "xauth list unix:displaynum.screennum".
284 * XXX: "localhost" match to determine FamilyLocal
285 * is not perfect.
286 */
287 if (strncmp(display, "localhost:", 10) == 0) {
288 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
289 display + 10);
290 display = xdisplay;
291 }
292 if (trusted == 0) {
293 xauthdir = xmalloc(MAXPATHLEN);
294 xauthfile = xmalloc(MAXPATHLEN);
295 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
296 if (mkdtemp(xauthdir) != NULL) {
297 do_unlink = 1;
298 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
299 xauthdir);
300 snprintf(cmd, sizeof(cmd),
301 "%s -f %s generate %s " SSH_X11_PROTO
302 " untrusted timeout 1200 2>" _PATH_DEVNULL,
303 xauth_path, xauthfile, display);
304 debug2("x11_get_proto: %s", cmd);
305 if (system(cmd) == 0)
306 generated = 1;
307 }
308 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000309
310 /*
311 * When in untrusted mode, we read the cookie only if it was
312 * successfully generated as an untrusted one in the step
313 * above.
314 */
315 if (trusted || generated) {
316 snprintf(cmd, sizeof(cmd),
317 "%s %s%s list %s 2>" _PATH_DEVNULL,
318 xauth_path,
319 generated ? "-f " : "" ,
320 generated ? xauthfile : "",
321 display);
322 debug2("x11_get_proto: %s", cmd);
323 f = popen(cmd, "r");
324 if (f && fgets(line, sizeof(line), f) &&
325 sscanf(line, "%*s %511s %511s", proto, data) == 2)
326 got_data = 1;
327 if (f)
328 pclose(f);
329 } else
330 error("Warning: untrusted X11 forwarding setup failed: "
331 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000332 }
333
334 if (do_unlink) {
335 unlink(xauthfile);
336 rmdir(xauthdir);
337 }
338 if (xauthdir)
339 xfree(xauthdir);
340 if (xauthfile)
341 xfree(xauthfile);
342
343 /*
344 * If we didn't get authentication data, just make up some
345 * data. The forwarding code will check the validity of the
346 * response anyway, and substitute this data. The X11
347 * server, however, will ignore this fake data and use
348 * whatever authentication mechanisms it was using otherwise
349 * for the local connection.
350 */
351 if (!got_data) {
352 u_int32_t rnd = 0;
353
354 logit("Warning: No xauth data; "
355 "using fake authentication data for X11 forwarding.");
356 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
357 for (i = 0; i < 16; i++) {
358 if (i % 4 == 0)
359 rnd = arc4random();
360 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
361 rnd & 0xff);
362 rnd >>= 8;
363 }
364 }
365}
366
Damien Miller5428f641999-11-25 11:54:57 +1100367/*
368 * This is called when the interactive is entered. This checks if there is
369 * an EOF coming on stdin. We must check this explicitly, as select() does
370 * not appear to wake up when redirecting from /dev/null.
371 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Ben Lindstrombba81212001-06-25 05:01:22 +0000373static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000374client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375{
Damien Miller95def091999-11-25 00:26:21 +1100376 int len;
377 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378
Damien Miller5428f641999-11-25 11:54:57 +1100379 /*
380 * If standard input is to be "redirected from /dev/null", we simply
381 * mark that we have seen an EOF and send an EOF message to the
382 * server. Otherwise, we try to read a single character; it appears
383 * that for some files, such /dev/null, select() never wakes up for
384 * read for this descriptor, which means that we never get EOF. This
385 * way we will get the EOF if stdin comes from /dev/null or similar.
386 */
Damien Miller95def091999-11-25 00:26:21 +1100387 if (stdin_null_flag) {
388 /* Fake EOF on stdin. */
389 debug("Sending eof.");
390 stdin_eof = 1;
391 packet_start(SSH_CMSG_EOF);
392 packet_send();
393 } else {
Damien Miller95def091999-11-25 00:26:21 +1100394 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller95def091999-11-25 00:26:21 +1100396 /* Check for immediate EOF on stdin. */
397 len = read(fileno(stdin), buf, 1);
398 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000399 /*
400 * EOF. Record that we have seen it and send
401 * EOF to server.
402 */
Damien Miller95def091999-11-25 00:26:21 +1100403 debug("Sending eof.");
404 stdin_eof = 1;
405 packet_start(SSH_CMSG_EOF);
406 packet_send();
407 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100408 /*
409 * Got data. We must store the data in the buffer,
410 * and also process it as an escape character if
411 * appropriate.
412 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000413 if ((u_char) buf[0] == escape_char1)
414 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000415 else
Damien Miller95def091999-11-25 00:26:21 +1100416 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100417 }
Damien Miller95def091999-11-25 00:26:21 +1100418 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420}
421
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Damien Miller5428f641999-11-25 11:54:57 +1100423/*
424 * Make packets from buffered stdin data, and buffer them for sending to the
425 * connection.
426 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427
Ben Lindstrombba81212001-06-25 05:01:22 +0000428static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000429client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000431 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Damien Miller95def091999-11-25 00:26:21 +1100433 /* Send buffered stdin data to the server. */
434 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100435 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100436 len = buffer_len(&stdin_buffer);
437 /* Keep the packets at reasonable size. */
438 if (len > packet_get_maxsize())
439 len = packet_get_maxsize();
440 packet_start(SSH_CMSG_STDIN_DATA);
441 packet_put_string(buffer_ptr(&stdin_buffer), len);
442 packet_send();
443 buffer_consume(&stdin_buffer, len);
444 /* If we have a pending EOF, send it now. */
445 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
446 packet_start(SSH_CMSG_EOF);
447 packet_send();
448 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450}
451
Damien Miller5428f641999-11-25 11:54:57 +1100452/*
453 * Checks if the client window has changed, and sends a packet about it to
454 * the server if so. The actual change is detected elsewhere (by a software
455 * interrupt on Unix); this just checks the flag and sends a message if
456 * appropriate.
457 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458
Ben Lindstrombba81212001-06-25 05:01:22 +0000459static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000460client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461{
Damien Miller1383bd82000-04-06 12:32:37 +1000462 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
Damien Miller1383bd82000-04-06 12:32:37 +1000464 if (! received_window_change_signal)
465 return;
466 /** XXX race */
467 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Millerd3444942000-09-30 14:20:03 +1100469 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000470
471 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000472 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000473 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000474 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
475 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000476 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100477 packet_put_int((u_int)ws.ws_row);
478 packet_put_int((u_int)ws.ws_col);
479 packet_put_int((u_int)ws.ws_xpixel);
480 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000481 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483}
484
Damien Miller509b0102003-12-17 16:33:10 +1100485static void
486client_global_request_reply(int type, u_int32_t seq, void *ctxt)
487{
Darren Tucker9f407c42008-06-13 04:50:27 +1000488 struct global_confirm *gc;
489
490 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
491 return;
492 if (gc->cb != NULL)
493 gc->cb(type, seq, gc->ctx);
494 if (--gc->ref_count <= 0) {
495 TAILQ_REMOVE(&global_confirms, gc, entry);
496 bzero(gc, sizeof(*gc));
497 xfree(gc);
498 }
499
Darren Tuckerf7288d72009-06-21 18:12:20 +1000500 packet_set_alive_timeouts(0);
Damien Miller509b0102003-12-17 16:33:10 +1100501}
502
503static void
504server_alive_check(void)
505{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000506 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Miller985a4482006-10-24 03:02:41 +1000507 logit("Timeout, server not responding.");
508 cleanup_exit(255);
509 }
Damien Miller509b0102003-12-17 16:33:10 +1100510 packet_start(SSH2_MSG_GLOBAL_REQUEST);
511 packet_put_cstring("keepalive@openssh.com");
512 packet_put_char(1); /* boolean: want reply */
513 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000514 /* Insert an empty placeholder to maintain ordering */
515 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100516}
517
Damien Miller5428f641999-11-25 11:54:57 +1100518/*
519 * Waits until the client can do something (some data becomes available on
520 * one of the file descriptors).
521 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000522static void
Damien Miller5e953212001-01-30 09:14:00 +1100523client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000524 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525{
Damien Miller509b0102003-12-17 16:33:10 +1100526 struct timeval tv, *tvp;
527 int ret;
528
Damien Miller5e953212001-01-30 09:14:00 +1100529 /* Add any selections by the channel mechanism. */
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000530 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531
Damien Miller1383bd82000-04-06 12:32:37 +1000532 if (!compat20) {
533 /* Read from the connection, unless our buffers are full. */
534 if (buffer_len(&stdout_buffer) < buffer_high &&
535 buffer_len(&stderr_buffer) < buffer_high &&
536 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100537 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000538 /*
539 * Read from stdin, unless we have seen EOF or have very much
540 * buffered data to send to the server.
541 */
542 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100543 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000544
545 /* Select stdout/stderr if have data in buffer. */
546 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100547 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000548 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100549 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000550 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000551 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000552 if (session_closed && !channel_still_open() &&
553 !packet_have_data_to_write()) {
554 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100555 memset(*readsetp, 0, *nallocp);
556 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000557 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000558 } else {
559 FD_SET(connection_in, *readsetp);
560 }
Damien Miller1383bd82000-04-06 12:32:37 +1000561 }
562
Damien Miller95def091999-11-25 00:26:21 +1100563 /* Select server connection if have data to write to the server. */
564 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100565 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100566
Damien Millerb1cbfa22008-05-19 16:00:08 +1000567 if (muxserver_sock != -1)
568 FD_SET(muxserver_sock, *readsetp);
Damien Miller0e220db2004-06-15 10:34:08 +1000569
Damien Miller5428f641999-11-25 11:54:57 +1100570 /*
571 * Wait for something to happen. This will suspend the process until
572 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100573 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100574 */
Damien Miller95def091999-11-25 00:26:21 +1100575
Damien Miller509b0102003-12-17 16:33:10 +1100576 if (options.server_alive_interval == 0 || !compat20)
577 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000578 else {
Damien Miller509b0102003-12-17 16:33:10 +1100579 tv.tv_sec = options.server_alive_interval;
580 tv.tv_usec = 0;
581 tvp = &tv;
582 }
583 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
584 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100585 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000586
587 /*
588 * We have to clear the select masks, because we return.
589 * We have to return, because the mainloop checks for the flags
590 * set by the signal handlers.
591 */
Damien Miller79faeff2001-11-12 11:06:32 +1100592 memset(*readsetp, 0, *nallocp);
593 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000594
Damien Miller95def091999-11-25 00:26:21 +1100595 if (errno == EINTR)
596 return;
597 /* Note: we might still have data in the buffers. */
598 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
599 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100600 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100601 } else if (ret == 0)
602 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603}
604
Ben Lindstrombba81212001-06-25 05:01:22 +0000605static void
Damien Millerad833b32000-08-23 10:46:23 +1000606client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607{
Damien Miller95def091999-11-25 00:26:21 +1100608 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000609 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000610 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
611 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000612 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000613 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
614 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100616 leave_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617
Damien Miller5428f641999-11-25 11:54:57 +1100618 /*
619 * Free (and clear) the buffer to reduce the amount of data that gets
620 * written to swap.
621 */
Damien Millerad833b32000-08-23 10:46:23 +1000622 buffer_free(bin);
623 buffer_free(bout);
624 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625
Damien Miller95def091999-11-25 00:26:21 +1100626 /* Send the suspend signal to the program itself. */
627 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628
Darren Tucker5d78de62004-11-05 20:35:44 +1100629 /* Reset window sizes in case they have changed */
630 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631
Damien Miller95def091999-11-25 00:26:21 +1100632 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000633 buffer_init(bin);
634 buffer_init(bout);
635 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100637 enter_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638}
639
Ben Lindstrombba81212001-06-25 05:01:22 +0000640static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100641client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000643 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000644 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000645
Damien Miller5428f641999-11-25 11:54:57 +1100646 /*
647 * Read input from the server, and add any such data to the buffer of
648 * the packet subsystem.
649 */
Damien Miller95def091999-11-25 00:26:21 +1100650 if (FD_ISSET(connection_in, readset)) {
651 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000652 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
653 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000654 /*
655 * Received EOF. The remote host has closed the
656 * connection.
657 */
658 snprintf(buf, sizeof buf,
659 "Connection to %.300s closed by remote host.\r\n",
660 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662 quit_pending = 1;
663 return;
Damien Miller95def091999-11-25 00:26:21 +1100664 }
Damien Miller5428f641999-11-25 11:54:57 +1100665 /*
666 * There is a kernel bug on Solaris that causes select to
667 * sometimes wake up even though there is no data available.
668 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000669 if (len < 0 &&
670 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100671 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672
Damien Miller95def091999-11-25 00:26:21 +1100673 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000674 /*
675 * An error has encountered. Perhaps there is a
676 * network problem.
677 */
678 snprintf(buf, sizeof buf,
679 "Read from remote host %.300s: %.100s\r\n",
680 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100681 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100682 quit_pending = 1;
683 return;
684 }
685 packet_process_incoming(buf, len);
686 }
Damien Miller1383bd82000-04-06 12:32:37 +1000687}
688
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000689static void
Damien Miller5771ed72008-05-19 15:35:33 +1000690client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000691{
Damien Miller5771ed72008-05-19 15:35:33 +1000692 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
693 char errmsg[256];
694 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000695
Damien Miller5771ed72008-05-19 15:35:33 +1000696 /* XXX supress on mux _client_ quietmode */
697 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
698 c->ctl_fd != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000699
Damien Miller5771ed72008-05-19 15:35:33 +1000700 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
701 debug2("%s request accepted on channel %d",
702 cr->request_type, c->self);
703 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
704 if (tochan) {
705 snprintf(errmsg, sizeof(errmsg),
706 "%s request failed\r\n", cr->request_type);
707 } else {
708 snprintf(errmsg, sizeof(errmsg),
709 "%s request failed on channel %d",
710 cr->request_type, c->self);
711 }
712 /* If error occurred on primary session channel, then exit */
713 if (cr->do_close && c->self == session_ident)
714 fatal("%s", errmsg);
715 /* If error occurred on mux client, append to their stderr */
716 if (tochan)
717 buffer_append(&c->extended, errmsg, strlen(errmsg));
718 else
719 error("%s", errmsg);
720 if (cr->do_close) {
721 chan_read_failed(c);
722 chan_write_failed(c);
723 }
Damien Miller0e220db2004-06-15 10:34:08 +1000724 }
Damien Miller5771ed72008-05-19 15:35:33 +1000725 xfree(cr);
726}
Damien Miller0e220db2004-06-15 10:34:08 +1000727
Damien Miller5771ed72008-05-19 15:35:33 +1000728static void
729client_abandon_status_confirm(Channel *c, void *ctx)
730{
731 xfree(ctx);
732}
733
734static void
735client_expect_confirm(int id, const char *request, int do_close)
736{
737 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
738
739 cr->request_type = request;
740 cr->do_close = do_close;
741
742 channel_register_status_confirm(id, client_status_confirm,
743 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000744}
745
Darren Tucker9f407c42008-06-13 04:50:27 +1000746void
747client_register_global_confirm(global_confirm_cb *cb, void *ctx)
748{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000749 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000750
751 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000752 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
753 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
754 if (++last_gc->ref_count >= INT_MAX)
755 fatal("%s: last_gc->ref_count = %d",
756 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000757 return;
758 }
759
760 gc = xmalloc(sizeof(*gc));
761 gc->cb = cb;
762 gc->ctx = ctx;
763 gc->ref_count = 1;
764 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
765}
766
Damien Miller0e220db2004-06-15 10:34:08 +1000767static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000768process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000769{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000770 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100771 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000772 int delete = 0;
Damien Miller0164cb82008-11-05 16:30:31 +1100773 int local = 0, remote = 0, dynamic = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100774 int cancel_port;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100775 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000776
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100777 bzero(&fwd, sizeof(fwd));
778 fwd.listen_host = fwd.connect_host = NULL;
779
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100780 leave_raw_mode(force_tty_flag);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000781 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000782 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000783 if (s == NULL)
784 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100785 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000786 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000787 if (*s == '-')
788 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000789 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000790 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000791
Darren Tucker1973c882004-05-24 10:34:36 +1000792 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000793 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000794 logit(" -L[bind_address:]port:host:hostport "
795 "Request local forward");
796 logit(" -R[bind_address:]port:host:hostport "
797 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100798 logit(" -D[bind_address:]port "
799 "Request dynamic forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000800 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000801 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100802 if (!options.permit_local_command)
803 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000804 logit(" !args "
805 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100806 goto out;
807 }
808
809 if (*s == '!' && options.permit_local_command) {
810 s++;
811 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000812 goto out;
813 }
814
815 if (*s == 'K') {
816 delete = 1;
817 s++;
818 }
Damien Miller0164cb82008-11-05 16:30:31 +1100819 if (*s == 'L')
820 local = 1;
821 else if (*s == 'R')
822 remote = 1;
823 else if (*s == 'D')
824 dynamic = 1;
825 else {
Damien Miller996acd22003-04-09 20:59:48 +1000826 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000827 goto out;
828 }
Damien Miller0164cb82008-11-05 16:30:31 +1100829
830 if ((local || dynamic) && delete) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000831 logit("Not supported.");
832 goto out;
833 }
Damien Miller0164cb82008-11-05 16:30:31 +1100834 if (remote && delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000835 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000836 goto out;
837 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000838
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100839 while (isspace(*++s))
840 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000841
Darren Tuckere7066df2004-05-24 10:18:05 +1000842 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100843 cancel_port = 0;
844 cancel_host = hpdelim(&s); /* may be NULL */
845 if (s != NULL) {
846 cancel_port = a2port(s);
847 cancel_host = cleanhostname(cancel_host);
848 } else {
849 cancel_port = a2port(cancel_host);
850 cancel_host = NULL;
851 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100852 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100853 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000854 goto out;
855 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100856 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000857 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100858 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000859 logit("Bad forwarding specification.");
860 goto out;
861 }
Damien Miller0164cb82008-11-05 16:30:31 +1100862 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100863 if (channel_setup_local_fwd_listener(fwd.listen_host,
864 fwd.listen_port, fwd.connect_host,
865 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000866 logit("Port forwarding failed.");
867 goto out;
868 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100869 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000870 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100871 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000872 fwd.connect_port) < 0) {
873 logit("Port forwarding failed.");
874 goto out;
875 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100876 }
877
Darren Tuckere7066df2004-05-24 10:18:05 +1000878 logit("Forwarding port.");
879 }
880
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000881out:
882 signal(SIGINT, handler);
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100883 enter_raw_mode(force_tty_flag);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000884 if (cmd)
885 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100886 if (fwd.listen_host != NULL)
887 xfree(fwd.listen_host);
888 if (fwd.connect_host != NULL)
889 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000890}
891
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000892/*
893 * Process the characters one by one, call with c==NULL for proto1 case.
894 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000895static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000896process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
897 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000898{
899 char string[1024];
900 pid_t pid;
901 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000902 u_int i;
903 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000904 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000905 int *escape_pendingp, escape_char;
906 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +1000907
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000908 if (c == NULL) {
909 escape_pendingp = &escape_pending1;
910 escape_char = escape_char1;
911 } else {
912 if (c->filter_ctx == NULL)
913 return 0;
914 efc = (struct escape_filter_ctx *)c->filter_ctx;
915 escape_pendingp = &efc->escape_pending;
916 escape_char = efc->escape_char;
917 }
918
Damien Millereccb9de2005-06-17 12:59:34 +1000919 if (len <= 0)
920 return (0);
921
922 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000923 /* Get one character at a time. */
924 ch = buf[i];
925
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000926 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +1000927 /* We have previously seen an escape character. */
928 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000929 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000930
931 /* Process the escaped character. */
932 switch (ch) {
933 case '.':
934 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000935 snprintf(string, sizeof string, "%c.\r\n",
936 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000937 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000938
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000939 if (c && c->ctl_fd != -1) {
940 chan_read_failed(c);
941 chan_write_failed(c);
942 return 0;
943 } else
944 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000945 return -1;
946
947 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000948 /* XXX support this for mux clients */
949 if (c && c->ctl_fd != -1) {
950 noescape:
951 snprintf(string, sizeof string,
952 "%c%c escape not available to "
953 "multiplexed sessions\r\n",
954 escape_char, ch);
955 buffer_append(berr, string,
956 strlen(string));
957 continue;
958 }
Darren Tucker4d5cd332008-06-13 04:51:14 +1000959 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000960 snprintf(string, sizeof string,
961 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000962 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000963
964 /* Restore terminal modes and suspend. */
965 client_suspend_self(bin, bout, berr);
966
967 /* We have been continued. */
968 continue;
969
Damien Miller54c45982003-05-15 10:20:13 +1000970 case 'B':
971 if (compat20) {
972 snprintf(string, sizeof string,
973 "%cB\r\n", escape_char);
974 buffer_append(berr, string,
975 strlen(string));
976 channel_request_start(session_ident,
977 "break", 0);
978 packet_put_int(1000);
979 packet_send();
980 }
981 continue;
982
Ben Lindstromf28f6342001-04-04 02:03:04 +0000983 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000984 if (compat20) {
985 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000986 logit("Server does not "
987 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000988 else
989 need_rekeying = 1;
990 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000991 continue;
992
Damien Millerad833b32000-08-23 10:46:23 +1000993 case '&':
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000994 if (c && c->ctl_fd != -1)
995 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +1000996 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +1000997 * Detach the program (continue to serve
998 * connections, but put in background and no
999 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001000 */
Damien Miller96507ef2001-11-12 10:52:25 +11001001 /* Restore tty modes. */
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001002 leave_raw_mode(force_tty_flag);
Damien Miller96507ef2001-11-12 10:52:25 +11001003
1004 /* Stop listening for new connections. */
1005 channel_stop_listening();
1006
1007 snprintf(string, sizeof string,
1008 "%c& [backgrounded]\n", escape_char);
1009 buffer_append(berr, string, strlen(string));
1010
1011 /* Fork into background. */
1012 pid = fork();
1013 if (pid < 0) {
1014 error("fork: %.100s", strerror(errno));
1015 continue;
1016 }
1017 if (pid != 0) { /* This is the parent. */
1018 /* The parent just exits. */
1019 exit(0);
1020 }
1021 /* The child continues serving connections. */
1022 if (compat20) {
1023 buffer_append(bin, "\004", 1);
1024 /* fake EOF on stdin */
1025 return -1;
1026 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001027 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001028 * Sending SSH_CMSG_EOF alone does not
1029 * always appear to be enough. So we
1030 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001031 */
1032 packet_start(SSH_CMSG_STDIN_DATA);
1033 packet_put_string("\004", 1);
1034 packet_send();
1035 /* Close stdin. */
1036 stdin_eof = 1;
1037 if (buffer_len(bin) == 0) {
1038 packet_start(SSH_CMSG_EOF);
1039 packet_send();
1040 }
1041 }
Damien Miller96507ef2001-11-12 10:52:25 +11001042 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001043
1044 case '?':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001045 if (c && c->ctl_fd != -1) {
1046 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +10001047"%c?\r\n\
1048Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001049 %c. - terminate session\r\n\
1050 %cB - send a BREAK to the remote system\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001051 %cR - Request rekey (SSH protocol 2 only)\r\n\
1052 %c# - list forwarded connections\r\n\
1053 %c? - this message\r\n\
1054 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001055(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001056 escape_char, escape_char,
1057 escape_char, escape_char,
1058 escape_char, escape_char,
Damien Miller586b0052008-12-09 14:11:32 +11001059 escape_char, escape_char);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001060 } else {
1061 snprintf(string, sizeof string,
1062"%c?\r\n\
1063Supported escape sequences:\r\n\
1064 %c. - terminate connection (and any multiplexed sessions)\r\n\
1065 %cB - send a BREAK to the remote system\r\n\
1066 %cC - open a command line\r\n\
1067 %cR - Request rekey (SSH protocol 2 only)\r\n\
1068 %c^Z - suspend ssh\r\n\
1069 %c# - list forwarded connections\r\n\
1070 %c& - background ssh (when waiting for connections to terminate)\r\n\
1071 %c? - this message\r\n\
1072 %c%c - send the escape character by typing it twice\r\n\
1073(Note that escapes are only recognized immediately after newline.)\r\n",
1074 escape_char, escape_char,
1075 escape_char, escape_char,
1076 escape_char, escape_char,
1077 escape_char, escape_char,
1078 escape_char, escape_char,
1079 escape_char);
1080 }
Damien Millerad833b32000-08-23 10:46:23 +10001081 buffer_append(berr, string, strlen(string));
1082 continue;
1083
1084 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001085 snprintf(string, sizeof string, "%c#\r\n",
1086 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001087 buffer_append(berr, string, strlen(string));
1088 s = channel_open_message();
1089 buffer_append(berr, s, strlen(s));
1090 xfree(s);
1091 continue;
1092
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001093 case 'C':
Damien Miller586b0052008-12-09 14:11:32 +11001094 if (c && c->ctl_fd != -1)
1095 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001096 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001097 continue;
1098
Damien Millerad833b32000-08-23 10:46:23 +10001099 default:
1100 if (ch != escape_char) {
1101 buffer_put_char(bin, escape_char);
1102 bytes++;
1103 }
1104 /* Escaped characters fall through here */
1105 break;
1106 }
1107 } else {
1108 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001109 * The previous character was not an escape char.
1110 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001111 */
1112 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001113 /*
1114 * It is. Set the flag and continue to
1115 * next character.
1116 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001117 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001118 continue;
1119 }
1120 }
1121
1122 /*
1123 * Normal character. Record whether it was a newline,
1124 * and append it to the buffer.
1125 */
1126 last_was_cr = (ch == '\r' || ch == '\n');
1127 buffer_put_char(bin, ch);
1128 bytes++;
1129 }
1130 return bytes;
1131}
1132
Ben Lindstrombba81212001-06-25 05:01:22 +00001133static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001134client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001135{
Damien Miller166fca82000-04-20 07:42:21 +10001136 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001137 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001138
Damien Miller95def091999-11-25 00:26:21 +11001139 /* Read input from stdin. */
1140 if (FD_ISSET(fileno(stdin), readset)) {
1141 /* Read as much as possible. */
1142 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001143 if (len < 0 &&
1144 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001145 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001146 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001147 /*
1148 * Received EOF or error. They are treated
1149 * similarly, except that an error message is printed
1150 * if it was an error condition.
1151 */
Damien Miller95def091999-11-25 00:26:21 +11001152 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001153 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1154 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001155 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001156 }
1157 /* Mark that we have seen EOF. */
1158 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001159 /*
1160 * Send an EOF message to the server unless there is
1161 * data in the buffer. If there is data in the
1162 * buffer, no message will be sent now. Code
1163 * elsewhere will send the EOF when the buffer
1164 * becomes empty if stdin_eof is set.
1165 */
Damien Miller95def091999-11-25 00:26:21 +11001166 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167 packet_start(SSH_CMSG_EOF);
1168 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001169 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001170 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001171 /*
1172 * Normal successful read, and no escape character.
1173 * Just append the data to buffer.
1174 */
Damien Miller95def091999-11-25 00:26:21 +11001175 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001176 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001177 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001178 * Normal, successful read. But we have an escape
1179 * character and have to process the characters one
1180 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001181 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001182 if (process_escapes(NULL, &stdin_buffer,
1183 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001184 return;
Damien Miller95def091999-11-25 00:26:21 +11001185 }
1186 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187}
1188
Ben Lindstrombba81212001-06-25 05:01:22 +00001189static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001190client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191{
Damien Miller95def091999-11-25 00:26:21 +11001192 int len;
1193 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194
Damien Miller95def091999-11-25 00:26:21 +11001195 /* Write buffered output to stdout. */
1196 if (FD_ISSET(fileno(stdout), writeset)) {
1197 /* Write as much data as possible. */
1198 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001199 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001200 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001201 if (errno == EINTR || errno == EAGAIN ||
1202 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001203 len = 0;
1204 else {
Damien Miller5428f641999-11-25 11:54:57 +11001205 /*
1206 * An error or EOF was encountered. Put an
1207 * error message to stderr buffer.
1208 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001209 snprintf(buf, sizeof buf,
1210 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001211 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001212 quit_pending = 1;
1213 return;
1214 }
1215 }
1216 /* Consume printed data from the buffer. */
1217 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218 }
Damien Miller95def091999-11-25 00:26:21 +11001219 /* Write buffered output to stderr. */
1220 if (FD_ISSET(fileno(stderr), writeset)) {
1221 /* Write as much data as possible. */
1222 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001223 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001224 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001225 if (errno == EINTR || errno == EAGAIN ||
1226 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001227 len = 0;
1228 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001229 /*
1230 * EOF or error, but can't even print
1231 * error message.
1232 */
Damien Miller95def091999-11-25 00:26:21 +11001233 quit_pending = 1;
1234 return;
1235 }
1236 }
1237 /* Consume printed characters from the buffer. */
1238 buffer_consume(&stderr_buffer, len);
1239 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240}
1241
Damien Miller5428f641999-11-25 11:54:57 +11001242/*
Damien Millerb38eff82000-04-01 11:09:21 +10001243 * Get packets from the connection input buffer, and process them as long as
1244 * there are packets available.
1245 *
1246 * Any unknown packets received during the actual
1247 * session cause the session to terminate. This is
1248 * intended to make debugging easier since no
1249 * confirmations are sent. Any compatible protocol
1250 * extensions must be negotiated during the
1251 * preparatory phase.
1252 */
1253
Ben Lindstrombba81212001-06-25 05:01:22 +00001254static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001255client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001256{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001257 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1258 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001259}
1260
Damien Millerad833b32000-08-23 10:46:23 +10001261/* scan buf[] for '~' before sending data to the peer */
1262
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001263/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1264void *
1265client_new_escape_filter_ctx(int escape_char)
1266{
1267 struct escape_filter_ctx *ret;
1268
1269 ret = xmalloc(sizeof(*ret));
1270 ret->escape_pending = 0;
1271 ret->escape_char = escape_char;
1272 return (void *)ret;
1273}
1274
Darren Tucker84c56f52008-06-13 04:55:46 +10001275/* Free the escape filter context on channel free */
1276void
1277client_filter_cleanup(int cid, void *ctx)
1278{
1279 xfree(ctx);
1280}
1281
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001282int
1283client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001284{
Damien Miller5771ed72008-05-19 15:35:33 +10001285 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1286 return 0;
1287
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001288 return process_escapes(c, &c->input, &c->output, &c->extended,
1289 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001290}
1291
Ben Lindstrombba81212001-06-25 05:01:22 +00001292static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001293client_channel_closed(int id, void *arg)
1294{
Damien Miller3ec27592001-10-12 11:35:04 +10001295 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001296 session_closed = 1;
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001297 leave_raw_mode(force_tty_flag);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001298}
1299
Damien Millerb38eff82000-04-01 11:09:21 +10001300/*
Damien Miller5428f641999-11-25 11:54:57 +11001301 * Implements the interactive session with the server. This is called after
1302 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001303 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1304 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001305 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001306
Damien Miller4af51302000-04-16 11:18:38 +10001307int
Damien Millerad833b32000-08-23 10:46:23 +10001308client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309{
Damien Miller5e953212001-01-30 09:14:00 +11001310 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001311 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001312 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001313 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001314 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001315 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
Damien Miller95def091999-11-25 00:26:21 +11001317 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318
Damien Miller95def091999-11-25 00:26:21 +11001319 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320
Damien Miller95def091999-11-25 00:26:21 +11001321 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001322 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001323 last_was_cr = 1;
1324 exit_status = -1;
1325 stdin_eof = 0;
1326 buffer_high = 64 * 1024;
1327 connection_in = packet_get_connection_in();
1328 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001329 max_fd = MAX(connection_in, connection_out);
Damien Millerb1cbfa22008-05-19 16:00:08 +10001330 if (muxserver_sock != -1)
1331 max_fd = MAX(max_fd, muxserver_sock);
Damien Miller5e953212001-01-30 09:14:00 +11001332
1333 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001334 /* enable nonblocking unless tty */
1335 if (!isatty(fileno(stdin)))
1336 set_nonblock(fileno(stdin));
1337 if (!isatty(fileno(stdout)))
1338 set_nonblock(fileno(stdout));
1339 if (!isatty(fileno(stderr)))
1340 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001341 max_fd = MAX(max_fd, fileno(stdin));
1342 max_fd = MAX(max_fd, fileno(stdout));
1343 max_fd = MAX(max_fd, fileno(stderr));
1344 }
Damien Miller95def091999-11-25 00:26:21 +11001345 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001346 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001347
Damien Miller95def091999-11-25 00:26:21 +11001348 /* Initialize buffers. */
1349 buffer_init(&stdin_buffer);
1350 buffer_init(&stdout_buffer);
1351 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352
Damien Millerb38eff82000-04-01 11:09:21 +10001353 client_init_dispatch();
1354
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001355 /*
1356 * Set signal handlers, (e.g. to restore non-blocking mode)
1357 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1358 */
Darren Tucker07336da2004-11-05 20:02:16 +11001359 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1360 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001361 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1362 signal(SIGINT, signal_handler);
1363 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1364 signal(SIGQUIT, signal_handler);
1365 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1366 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001367 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Damien Miller95def091999-11-25 00:26:21 +11001369 if (have_pty)
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001370 enter_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371
Ben Lindstrom5b828322001-02-09 01:34:36 +00001372 if (compat20) {
1373 session_ident = ssh2_chan_id;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001374 if (escape_char_arg != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001375 channel_register_filter(session_ident,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001376 client_simple_escape_filter, NULL,
Darren Tucker84c56f52008-06-13 04:55:46 +10001377 client_filter_cleanup,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001378 client_new_escape_filter_ctx(escape_char_arg));
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001379 if (session_ident != -1)
1380 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001381 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001382 } else {
1383 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001384 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001385 }
Damien Millerad833b32000-08-23 10:46:23 +10001386
Damien Miller95def091999-11-25 00:26:21 +11001387 /* Main loop of the client for the interactive session mode. */
1388 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389
Damien Miller5428f641999-11-25 11:54:57 +11001390 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001391 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001393 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001394 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001395
1396 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001397
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001398 if (rekeying) {
1399 debug("rekeying in progress");
1400 } else {
1401 /*
1402 * Make packets of buffered stdin data, and buffer
1403 * them for sending to the server.
1404 */
1405 if (!compat20)
1406 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001408 /*
1409 * Make packets from buffered channel data, and
1410 * enqueue them for sending to the server.
1411 */
1412 if (packet_not_very_much_data_to_write())
1413 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001415 /*
1416 * Check if the window size has changed, and buffer a
1417 * message about it to the server if so.
1418 */
1419 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001421 if (quit_pending)
1422 break;
1423 }
Damien Miller5428f641999-11-25 11:54:57 +11001424 /*
1425 * Wait until we have something to do (something becomes
1426 * available on one of the descriptors).
1427 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001428 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001429 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001430 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431
Damien Miller95def091999-11-25 00:26:21 +11001432 if (quit_pending)
1433 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001435 /* Do channel operations unless rekeying in progress. */
1436 if (!rekeying) {
1437 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001438 if (need_rekeying || packet_need_rekeying()) {
1439 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001440 xxx_kex->done = 0;
1441 kex_send_kexinit(xxx_kex);
1442 need_rekeying = 0;
1443 }
1444 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445
Damien Miller1383bd82000-04-06 12:32:37 +10001446 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001447 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448
Damien Miller0e220db2004-06-15 10:34:08 +10001449 /* Accept control connections. */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001450 if (muxserver_sock != -1 &&FD_ISSET(muxserver_sock, readset)) {
1451 if (muxserver_accept_control())
1452 quit_pending = 1;
1453 }
Damien Miller0e220db2004-06-15 10:34:08 +10001454
Damien Miller1383bd82000-04-06 12:32:37 +10001455 if (quit_pending)
1456 break;
1457
1458 if (!compat20) {
1459 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001460 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001461 /*
1462 * Process output to stdout and stderr. Output to
1463 * the connection is processed elsewhere (above).
1464 */
Damien Miller5e953212001-01-30 09:14:00 +11001465 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001466 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467
Darren Tuckere32cf432010-01-08 16:51:40 +11001468 if (session_resumed) {
1469 connection_in = packet_get_connection_in();
1470 connection_out = packet_get_connection_out();
1471 max_fd = MAX(max_fd, connection_out);
1472 max_fd = MAX(max_fd, connection_in);
1473 session_resumed = 0;
1474 }
1475
Darren Tucker4d5cd332008-06-13 04:51:14 +10001476 /*
1477 * Send as much buffered packet data as possible to the
1478 * sender.
1479 */
Damien Miller5e953212001-01-30 09:14:00 +11001480 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001481 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001482 }
Damien Miller5e953212001-01-30 09:14:00 +11001483 if (readset)
1484 xfree(readset);
1485 if (writeset)
1486 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487
Damien Miller95def091999-11-25 00:26:21 +11001488 /* Terminate the session. */
1489
1490 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001491 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001492
Darren Tuckerde0c0252009-07-06 07:17:35 +10001493 if (compat20) {
1494 packet_start(SSH2_MSG_DISCONNECT);
1495 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1496 packet_put_cstring("disconnected by user");
1497 packet_send();
1498 packet_write_wait();
1499 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001500
Ben Lindstrom601e4362001-06-21 03:19:23 +00001501 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001502
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001503 if (have_pty)
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001504 leave_raw_mode(force_tty_flag);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001505
1506 /* restore blocking io */
1507 if (!isatty(fileno(stdin)))
1508 unset_nonblock(fileno(stdin));
1509 if (!isatty(fileno(stdout)))
1510 unset_nonblock(fileno(stdout));
1511 if (!isatty(fileno(stderr)))
1512 unset_nonblock(fileno(stderr));
1513
Damien Millerd6965512003-12-17 16:31:53 +11001514 /*
1515 * If there was no shell or command requested, there will be no remote
1516 * exit status to be returned. In that case, clear error code if the
1517 * connection was deliberately terminated at this end.
1518 */
1519 if (no_shell_flag && received_signal == SIGTERM) {
1520 received_signal = 0;
1521 exit_status = 0;
1522 }
1523
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001524 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001525 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001526
1527 /*
1528 * In interactive mode (with pseudo tty) display a message indicating
1529 * that the connection has been closed.
1530 */
1531 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001532 snprintf(buf, sizeof buf,
1533 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001534 buffer_append(&stderr_buffer, buf, strlen(buf));
1535 }
1536
Damien Miller95def091999-11-25 00:26:21 +11001537 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001538 while (buffer_len(&stdout_buffer) > 0) {
1539 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001540 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001541 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001542 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001543 break;
1544 }
Damien Miller95def091999-11-25 00:26:21 +11001545 buffer_consume(&stdout_buffer, len);
1546 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001547
Damien Miller95def091999-11-25 00:26:21 +11001548 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001549 while (buffer_len(&stderr_buffer) > 0) {
1550 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001551 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001552 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001553 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001554 break;
1555 }
Damien Miller95def091999-11-25 00:26:21 +11001556 buffer_consume(&stderr_buffer, len);
1557 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001558
Damien Miller95def091999-11-25 00:26:21 +11001559 /* Clear and free any buffers. */
1560 memset(buf, 0, sizeof(buf));
1561 buffer_free(&stdin_buffer);
1562 buffer_free(&stdout_buffer);
1563 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564
Damien Miller95def091999-11-25 00:26:21 +11001565 /* Report bytes transferred, and transfer rates. */
1566 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001567 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1568 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1569 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1570 obytes, ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001571 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001572 verbose("Bytes per second: sent %.1f, received %.1f",
1573 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001574 /* Return the exit status of the program. */
1575 debug("Exit status %d", exit_status);
1576 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001577}
Damien Millerb38eff82000-04-01 11:09:21 +10001578
1579/*********/
1580
Ben Lindstrombba81212001-06-25 05:01:22 +00001581static void
Damien Miller630d6f42002-01-22 23:17:30 +11001582client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001583{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001584 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001585 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001586 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001587 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001588 memset(data, 0, data_len);
1589 xfree(data);
1590}
Ben Lindstrombba81212001-06-25 05:01:22 +00001591static void
Damien Miller630d6f42002-01-22 23:17:30 +11001592client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001593{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001594 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001595 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001596 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001597 buffer_append(&stderr_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001598 memset(data, 0, data_len);
1599 xfree(data);
1600}
Ben Lindstrombba81212001-06-25 05:01:22 +00001601static void
Damien Miller630d6f42002-01-22 23:17:30 +11001602client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001603{
Damien Millerb38eff82000-04-01 11:09:21 +10001604 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001605 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001606 /* Acknowledge the exit. */
1607 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1608 packet_send();
1609 /*
1610 * Must wait for packet to be sent since we are
1611 * exiting the loop.
1612 */
1613 packet_write_wait();
1614 /* Flag that we want to exit. */
1615 quit_pending = 1;
1616}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001617static void
1618client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1619{
1620 Channel *c = NULL;
1621 int remote_id, sock;
1622
1623 /* Read the remote channel number from the message. */
1624 remote_id = packet_get_int();
1625 packet_check_eom();
1626
1627 /*
1628 * Get a connection to the local authentication agent (this may again
1629 * get forwarded).
1630 */
1631 sock = ssh_get_authentication_socket();
1632
1633 /*
1634 * If we could not connect the agent, send an error message back to
1635 * the server. This should never happen unless the agent dies,
1636 * because authentication forwarding is only enabled if we have an
1637 * agent.
1638 */
1639 if (sock >= 0) {
1640 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1641 -1, 0, 0, 0, "authentication agent connection", 1);
1642 c->remote_id = remote_id;
1643 c->force_drain = 1;
1644 }
1645 if (c == NULL) {
1646 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1647 packet_put_int(remote_id);
1648 } else {
1649 /* Send a confirmation to the remote host. */
1650 debug("Forwarding authentication connection.");
1651 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1652 packet_put_int(remote_id);
1653 packet_put_int(c->self);
1654 }
1655 packet_send();
1656}
Damien Millerb38eff82000-04-01 11:09:21 +10001657
Ben Lindstrombba81212001-06-25 05:01:22 +00001658static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001659client_request_forwarded_tcpip(const char *request_type, int rchan)
1660{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001661 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001662 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001663 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001664
1665 /* Get rest of the packet */
1666 listen_address = packet_get_string(NULL);
1667 listen_port = packet_get_int();
1668 originator_address = packet_get_string(NULL);
1669 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001670 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001671
Damien Millerbd740252008-05-19 15:37:09 +10001672 debug("client_request_forwarded_tcpip: listen %s port %d, "
1673 "originator %s port %d", listen_address, listen_port,
1674 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001675
Damien Millerbd740252008-05-19 15:37:09 +10001676 c = channel_connect_by_listen_address(listen_port,
1677 "forwarded-tcpip", originator_address);
1678
Damien Miller0bc1bd82000-11-13 22:57:25 +11001679 xfree(originator_address);
1680 xfree(listen_address);
1681 return c;
1682}
1683
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001684static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001685client_request_x11(const char *request_type, int rchan)
1686{
1687 Channel *c = NULL;
1688 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001689 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001690 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001691
1692 if (!options.forward_x11) {
1693 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001694 error("Warning: this is probably a break-in attempt by a "
1695 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001696 return NULL;
1697 }
1698 originator = packet_get_string(NULL);
1699 if (datafellows & SSH_BUG_X11FWD) {
1700 debug2("buggy server: x11 request w/o originator_port");
1701 originator_port = 0;
1702 } else {
1703 originator_port = packet_get_int();
1704 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001705 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001706 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001707 debug("client_request_x11: request from %s %d", originator,
1708 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001709 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001710 sock = x11_connect_display();
1711 if (sock < 0)
1712 return NULL;
1713 c = channel_new("x11",
1714 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001715 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001716 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001717 return c;
1718}
1719
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001720static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001721client_request_agent(const char *request_type, int rchan)
1722{
1723 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001724 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001725
1726 if (!options.forward_agent) {
1727 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001728 error("Warning: this is probably a break-in attempt by a "
1729 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001730 return NULL;
1731 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001732 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001733 if (sock < 0)
1734 return NULL;
1735 c = channel_new("authentication agent connection",
1736 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001737 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001738 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001739 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001740 return c;
1741}
1742
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001743int
1744client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1745{
1746 Channel *c;
1747 int fd;
1748
1749 if (tun_mode == SSH_TUNMODE_NO)
1750 return 0;
1751
1752 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001753 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001754 return -1;
1755 }
1756
1757 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1758
1759 /* Open local tunnel device */
1760 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1761 error("Tunnel device open failed.");
1762 return -1;
1763 }
1764
1765 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1766 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1767 c->datagram = 1;
1768
1769#if defined(SSH_TUN_FILTER)
1770 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1771 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001772 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001773#endif
1774
1775 packet_start(SSH2_MSG_CHANNEL_OPEN);
1776 packet_put_cstring("tun@openssh.com");
1777 packet_put_int(c->self);
1778 packet_put_int(c->local_window_max);
1779 packet_put_int(c->local_maxpacket);
1780 packet_put_int(tun_mode);
1781 packet_put_int(remote_tun);
1782 packet_send();
1783
1784 return 0;
1785}
1786
Damien Millerbd483e72000-04-30 10:00:53 +10001787/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001788static void
Damien Miller630d6f42002-01-22 23:17:30 +11001789client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001790{
1791 Channel *c = NULL;
1792 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001793 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001794 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001795
1796 ctype = packet_get_string(&len);
1797 rchan = packet_get_int();
1798 rwindow = packet_get_int();
1799 rmaxpack = packet_get_int();
1800
Damien Millere247cc42000-05-07 12:03:14 +10001801 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001802 ctype, rchan, rwindow, rmaxpack);
1803
Damien Miller0bc1bd82000-11-13 22:57:25 +11001804 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1805 c = client_request_forwarded_tcpip(ctype, rchan);
1806 } else if (strcmp(ctype, "x11") == 0) {
1807 c = client_request_x11(ctype, rchan);
1808 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1809 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001810 }
1811/* XXX duplicate : */
1812 if (c != NULL) {
1813 debug("confirm %s", ctype);
1814 c->remote_id = rchan;
1815 c->remote_window = rwindow;
1816 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001817 if (c->type != SSH_CHANNEL_CONNECTING) {
1818 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1819 packet_put_int(c->remote_id);
1820 packet_put_int(c->self);
1821 packet_put_int(c->local_window);
1822 packet_put_int(c->local_maxpacket);
1823 packet_send();
1824 }
Damien Millerbd483e72000-04-30 10:00:53 +10001825 } else {
1826 debug("failure %s", ctype);
1827 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1828 packet_put_int(rchan);
1829 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001830 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001831 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001832 packet_put_cstring("");
1833 }
Damien Millerbd483e72000-04-30 10:00:53 +10001834 packet_send();
1835 }
1836 xfree(ctype);
1837}
Ben Lindstrombba81212001-06-25 05:01:22 +00001838static void
Damien Miller630d6f42002-01-22 23:17:30 +11001839client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001840{
1841 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001842 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001843 char *rtype;
1844
1845 id = packet_get_int();
1846 rtype = packet_get_string(NULL);
1847 reply = packet_get_char();
1848
1849 debug("client_input_channel_req: channel %d rtype %s reply %d",
1850 id, rtype, reply);
1851
Damien Miller3bbd8782004-06-18 22:23:22 +10001852 if (id == -1) {
1853 error("client_input_channel_req: request for channel -1");
1854 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001855 error("client_input_channel_req: channel %d: "
1856 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001857 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1858 packet_check_eom();
1859 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001860 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001861 exitval = packet_get_int();
Darren Tucker29440822010-01-08 17:08:35 +11001862 if (c->ctl_fd != -1) {
1863 /* Dispatch to mux client */
Damien Miller0e220db2004-06-15 10:34:08 +10001864 atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
1865 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001866 } else if (id == session_ident) {
1867 /* Record exit value of local session */
1868 success = 1;
1869 exit_status = exitval;
1870 } else {
1871 error("client_input_channel_req: unexpected channel %d",
1872 session_ident);
Damien Miller0e220db2004-06-15 10:34:08 +10001873 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001874 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001875 }
1876 if (reply) {
1877 packet_start(success ?
1878 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001879 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001880 packet_send();
1881 }
1882 xfree(rtype);
1883}
Damien Millerc3fa4072002-01-22 23:21:58 +11001884static void
1885client_input_global_request(int type, u_int32_t seq, void *ctxt)
1886{
1887 char *rtype;
1888 int want_reply;
1889 int success = 0;
1890
1891 rtype = packet_get_string(NULL);
1892 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001893 debug("client_input_global_request: rtype %s want_reply %d",
1894 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001895 if (want_reply) {
1896 packet_start(success ?
1897 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1898 packet_send();
1899 packet_write_wait();
1900 }
1901 xfree(rtype);
1902}
Damien Millerbd483e72000-04-30 10:00:53 +10001903
Damien Miller0e220db2004-06-15 10:34:08 +10001904void
Darren Tuckerfc959702004-07-17 16:12:08 +10001905client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10001906 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10001907{
1908 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001909 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001910
1911 debug2("%s: id %d", __func__, id);
1912
Darren Tucker5d78de62004-11-05 20:35:44 +11001913 if ((c = channel_lookup(id)) == NULL)
1914 fatal("client_session2_setup: channel %d: unknown channel", id);
1915
Damien Miller0e220db2004-06-15 10:34:08 +10001916 if (want_tty) {
1917 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10001918
1919 /* Store window size in the packet. */
1920 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1921 memset(&ws, 0, sizeof(ws));
1922
Damien Miller5771ed72008-05-19 15:35:33 +10001923 channel_request_start(id, "pty-req", 1);
1924 client_expect_confirm(id, "PTY allocation", 0);
Damien Miller0e220db2004-06-15 10:34:08 +10001925 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001926 packet_put_int((u_int)ws.ws_col);
1927 packet_put_int((u_int)ws.ws_row);
1928 packet_put_int((u_int)ws.ws_xpixel);
1929 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10001930 if (tiop == NULL)
1931 tiop = get_saved_tio();
1932 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10001933 packet_send();
1934 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001935 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001936 }
1937
1938 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10001939 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10001940 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10001941 char *name, *val;
1942
1943 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10001944 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10001945 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10001946 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10001947 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11001948 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001949 continue;
1950 }
1951 *val++ = '\0';
1952
1953 matched = 0;
1954 for (j = 0; j < options.num_send_env; j++) {
1955 if (match_pattern(name, options.send_env[j])) {
1956 matched = 1;
1957 break;
1958 }
1959 }
1960 if (!matched) {
1961 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11001962 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001963 continue;
1964 }
1965
1966 debug("Sending env %s = %s", name, val);
1967 channel_request_start(id, "env", 0);
1968 packet_put_cstring(name);
1969 packet_put_cstring(val);
1970 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11001971 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001972 }
1973 }
1974
1975 len = buffer_len(cmd);
1976 if (len > 0) {
1977 if (len > 900)
1978 len = 900;
1979 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10001980 debug("Sending subsystem: %.*s",
1981 len, (u_char*)buffer_ptr(cmd));
1982 channel_request_start(id, "subsystem", 1);
1983 client_expect_confirm(id, "subsystem", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001984 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10001985 debug("Sending command: %.*s",
1986 len, (u_char*)buffer_ptr(cmd));
1987 channel_request_start(id, "exec", 1);
1988 client_expect_confirm(id, "exec", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001989 }
1990 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
1991 packet_send();
1992 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10001993 channel_request_start(id, "shell", 1);
1994 client_expect_confirm(id, "shell", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001995 packet_send();
1996 }
1997}
1998
Ben Lindstrombba81212001-06-25 05:01:22 +00001999static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002000client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002001{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002002 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002003
Damien Miller1383bd82000-04-06 12:32:37 +10002004 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2005 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2006 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2007 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002008 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002009 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2010 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002011 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002012 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002013 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2014 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002015 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002016
2017 /* rekeying */
2018 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002019
2020 /* global request reply messages */
2021 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2022 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002023}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002024
Ben Lindstrombba81212001-06-25 05:01:22 +00002025static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002026client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002027{
2028 dispatch_init(NULL);
2029 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2030 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2031 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002032 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2033 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2034 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002035 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2036 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2037 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002038
2039 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002040 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002041 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2042 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002043}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002044
Ben Lindstrombba81212001-06-25 05:01:22 +00002045static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002046client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002047{
2048 client_init_dispatch_13();
2049 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2050 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2051}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002052
Ben Lindstromdb47f382001-07-04 05:10:27 +00002053static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002054client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002055{
Damien Miller1383bd82000-04-06 12:32:37 +10002056 if (compat20)
2057 client_init_dispatch_20();
2058 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002059 client_init_dispatch_13();
2060 else
2061 client_init_dispatch_15();
2062}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002063
2064/* client specific fatal cleanup */
2065void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002066cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002067{
Darren Tucker37c1b3d2010-01-09 22:26:23 +11002068 leave_raw_mode(force_tty_flag);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002069 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002070 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002071 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002072 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002073}