blob: 05f4720a1d5b7fb0678bac3a75e7795c6c2cab1d [file] [log] [blame]
Damien Millere1537f92010-01-26 13:26:22 +11001/* $OpenBSD: clientloop.c,v 1.217 2010/01/26 01:28:35 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 Millere1537f92010-01-26 13:26:22 +1100124extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
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 Millere1537f92010-01-26 13:26:22 +1100149volatile 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 Miller5428f641999-11-25 11:54:57 +1100567 /*
568 * Wait for something to happen. This will suspend the process until
569 * some selected descriptor can be read, written, or has some other
Damien Miller509b0102003-12-17 16:33:10 +1100570 * event pending.
Damien Miller5428f641999-11-25 11:54:57 +1100571 */
Damien Miller95def091999-11-25 00:26:21 +1100572
Damien Miller509b0102003-12-17 16:33:10 +1100573 if (options.server_alive_interval == 0 || !compat20)
574 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000575 else {
Damien Miller509b0102003-12-17 16:33:10 +1100576 tv.tv_sec = options.server_alive_interval;
577 tv.tv_usec = 0;
578 tvp = &tv;
579 }
580 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
581 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100582 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000583
584 /*
585 * We have to clear the select masks, because we return.
586 * We have to return, because the mainloop checks for the flags
587 * set by the signal handlers.
588 */
Damien Miller79faeff2001-11-12 11:06:32 +1100589 memset(*readsetp, 0, *nallocp);
590 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000591
Damien Miller95def091999-11-25 00:26:21 +1100592 if (errno == EINTR)
593 return;
594 /* Note: we might still have data in the buffers. */
595 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
596 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100597 quit_pending = 1;
Damien Miller509b0102003-12-17 16:33:10 +1100598 } else if (ret == 0)
599 server_alive_check();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600}
601
Ben Lindstrombba81212001-06-25 05:01:22 +0000602static void
Damien Millerad833b32000-08-23 10:46:23 +1000603client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604{
Damien Miller95def091999-11-25 00:26:21 +1100605 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000606 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000607 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
608 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000609 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000610 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
611 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100613 leave_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Damien Miller5428f641999-11-25 11:54:57 +1100615 /*
616 * Free (and clear) the buffer to reduce the amount of data that gets
617 * written to swap.
618 */
Damien Millerad833b32000-08-23 10:46:23 +1000619 buffer_free(bin);
620 buffer_free(bout);
621 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622
Damien Miller95def091999-11-25 00:26:21 +1100623 /* Send the suspend signal to the program itself. */
624 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625
Darren Tucker5d78de62004-11-05 20:35:44 +1100626 /* Reset window sizes in case they have changed */
627 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628
Damien Miller95def091999-11-25 00:26:21 +1100629 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000630 buffer_init(bin);
631 buffer_init(bout);
632 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100634 enter_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635}
636
Ben Lindstrombba81212001-06-25 05:01:22 +0000637static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100638client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000640 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000641 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller5428f641999-11-25 11:54:57 +1100643 /*
644 * Read input from the server, and add any such data to the buffer of
645 * the packet subsystem.
646 */
Damien Miller95def091999-11-25 00:26:21 +1100647 if (FD_ISSET(connection_in, readset)) {
648 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000649 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
650 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000651 /*
652 * Received EOF. The remote host has closed the
653 * connection.
654 */
655 snprintf(buf, sizeof buf,
656 "Connection to %.300s closed by remote host.\r\n",
657 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659 quit_pending = 1;
660 return;
Damien Miller95def091999-11-25 00:26:21 +1100661 }
Damien Miller5428f641999-11-25 11:54:57 +1100662 /*
663 * There is a kernel bug on Solaris that causes select to
664 * sometimes wake up even though there is no data available.
665 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000666 if (len < 0 &&
667 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100668 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000669
Damien Miller95def091999-11-25 00:26:21 +1100670 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000671 /*
672 * An error has encountered. Perhaps there is a
673 * network problem.
674 */
675 snprintf(buf, sizeof buf,
676 "Read from remote host %.300s: %.100s\r\n",
677 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100678 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100679 quit_pending = 1;
680 return;
681 }
682 packet_process_incoming(buf, len);
683 }
Damien Miller1383bd82000-04-06 12:32:37 +1000684}
685
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000686static void
Damien Miller5771ed72008-05-19 15:35:33 +1000687client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000688{
Damien Miller5771ed72008-05-19 15:35:33 +1000689 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
690 char errmsg[256];
691 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000692
Damien Miller5771ed72008-05-19 15:35:33 +1000693 /* XXX supress on mux _client_ quietmode */
694 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100695 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000696
Damien Miller5771ed72008-05-19 15:35:33 +1000697 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
698 debug2("%s request accepted on channel %d",
699 cr->request_type, c->self);
700 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
701 if (tochan) {
702 snprintf(errmsg, sizeof(errmsg),
703 "%s request failed\r\n", cr->request_type);
704 } else {
705 snprintf(errmsg, sizeof(errmsg),
706 "%s request failed on channel %d",
707 cr->request_type, c->self);
708 }
709 /* If error occurred on primary session channel, then exit */
710 if (cr->do_close && c->self == session_ident)
711 fatal("%s", errmsg);
712 /* If error occurred on mux client, append to their stderr */
713 if (tochan)
714 buffer_append(&c->extended, errmsg, strlen(errmsg));
715 else
716 error("%s", errmsg);
717 if (cr->do_close) {
718 chan_read_failed(c);
719 chan_write_failed(c);
720 }
Damien Miller0e220db2004-06-15 10:34:08 +1000721 }
Damien Miller5771ed72008-05-19 15:35:33 +1000722 xfree(cr);
723}
Damien Miller0e220db2004-06-15 10:34:08 +1000724
Damien Miller5771ed72008-05-19 15:35:33 +1000725static void
726client_abandon_status_confirm(Channel *c, void *ctx)
727{
728 xfree(ctx);
729}
730
731static void
732client_expect_confirm(int id, const char *request, int do_close)
733{
734 struct channel_reply_ctx *cr = xmalloc(sizeof(*cr));
735
736 cr->request_type = request;
737 cr->do_close = do_close;
738
739 channel_register_status_confirm(id, client_status_confirm,
740 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000741}
742
Darren Tucker9f407c42008-06-13 04:50:27 +1000743void
744client_register_global_confirm(global_confirm_cb *cb, void *ctx)
745{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000746 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000747
748 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000749 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
750 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
751 if (++last_gc->ref_count >= INT_MAX)
752 fatal("%s: last_gc->ref_count = %d",
753 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000754 return;
755 }
756
757 gc = xmalloc(sizeof(*gc));
758 gc->cb = cb;
759 gc->ctx = ctx;
760 gc->ref_count = 1;
761 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
762}
763
Damien Miller0e220db2004-06-15 10:34:08 +1000764static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000765process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000766{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000767 void (*handler)(int);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100768 char *s, *cmd, *cancel_host;
Darren Tuckere7066df2004-05-24 10:18:05 +1000769 int delete = 0;
Damien Miller0164cb82008-11-05 16:30:31 +1100770 int local = 0, remote = 0, dynamic = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100771 int cancel_port;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100772 Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000773
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100774 bzero(&fwd, sizeof(fwd));
775 fwd.listen_host = fwd.connect_host = NULL;
776
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100777 leave_raw_mode(force_tty_flag);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000778 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000779 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000780 if (s == NULL)
781 goto out;
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100782 while (isspace(*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000783 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000784 if (*s == '-')
785 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000786 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000787 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000788
Darren Tucker1973c882004-05-24 10:34:36 +1000789 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000790 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000791 logit(" -L[bind_address:]port:host:hostport "
792 "Request local forward");
793 logit(" -R[bind_address:]port:host:hostport "
794 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100795 logit(" -D[bind_address:]port "
796 "Request dynamic forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000797 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000798 "Cancel remote forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100799 if (!options.permit_local_command)
800 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000801 logit(" !args "
802 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100803 goto out;
804 }
805
806 if (*s == '!' && options.permit_local_command) {
807 s++;
808 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000809 goto out;
810 }
811
812 if (*s == 'K') {
813 delete = 1;
814 s++;
815 }
Damien Miller0164cb82008-11-05 16:30:31 +1100816 if (*s == 'L')
817 local = 1;
818 else if (*s == 'R')
819 remote = 1;
820 else if (*s == 'D')
821 dynamic = 1;
822 else {
Damien Miller996acd22003-04-09 20:59:48 +1000823 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000824 goto out;
825 }
Damien Miller0164cb82008-11-05 16:30:31 +1100826
827 if ((local || dynamic) && delete) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000828 logit("Not supported.");
829 goto out;
830 }
Damien Miller0164cb82008-11-05 16:30:31 +1100831 if (remote && delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000832 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000833 goto out;
834 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000835
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100836 while (isspace(*++s))
837 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000838
Damien Millere1537f92010-01-26 13:26:22 +1100839 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000840 if (delete) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100841 cancel_port = 0;
842 cancel_host = hpdelim(&s); /* may be NULL */
843 if (s != NULL) {
844 cancel_port = a2port(s);
845 cancel_host = cleanhostname(cancel_host);
846 } else {
847 cancel_port = a2port(cancel_host);
848 cancel_host = NULL;
849 }
Damien Miller3dc71ad2009-01-28 16:31:22 +1100850 if (cancel_port <= 0) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100851 logit("Bad forwarding close port");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000852 goto out;
853 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100854 channel_request_rforward_cancel(cancel_host, cancel_port);
Darren Tuckere7066df2004-05-24 10:18:05 +1000855 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100856 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000857 logit("Bad forwarding specification.");
858 goto out;
859 }
Damien Miller0164cb82008-11-05 16:30:31 +1100860 if (local || dynamic) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100861 if (channel_setup_local_fwd_listener(fwd.listen_host,
862 fwd.listen_port, fwd.connect_host,
863 fwd.connect_port, options.gateway_ports) < 0) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000864 logit("Port forwarding failed.");
865 goto out;
866 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100867 } else {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000868 if (channel_request_remote_forwarding(fwd.listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100869 fwd.listen_port, fwd.connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000870 fwd.connect_port) < 0) {
871 logit("Port forwarding failed.");
872 goto out;
873 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100874 }
875
Darren Tuckere7066df2004-05-24 10:18:05 +1000876 logit("Forwarding port.");
877 }
878
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000879out:
880 signal(SIGINT, handler);
Darren Tucker37c1b3d2010-01-09 22:26:23 +1100881 enter_raw_mode(force_tty_flag);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000882 if (cmd)
883 xfree(cmd);
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100884 if (fwd.listen_host != NULL)
885 xfree(fwd.listen_host);
886 if (fwd.connect_host != NULL)
887 xfree(fwd.connect_host);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000888}
889
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000890/*
891 * Process the characters one by one, call with c==NULL for proto1 case.
892 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000893static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000894process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
895 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +1000896{
897 char string[1024];
898 pid_t pid;
899 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000900 u_int i;
901 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +1000902 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000903 int *escape_pendingp, escape_char;
904 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +1000905
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000906 if (c == NULL) {
907 escape_pendingp = &escape_pending1;
908 escape_char = escape_char1;
909 } else {
910 if (c->filter_ctx == NULL)
911 return 0;
912 efc = (struct escape_filter_ctx *)c->filter_ctx;
913 escape_pendingp = &efc->escape_pending;
914 escape_char = efc->escape_char;
915 }
916
Damien Millereccb9de2005-06-17 12:59:34 +1000917 if (len <= 0)
918 return (0);
919
920 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000921 /* Get one character at a time. */
922 ch = buf[i];
923
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000924 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +1000925 /* We have previously seen an escape character. */
926 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000927 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000928
929 /* Process the escaped character. */
930 switch (ch) {
931 case '.':
932 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000933 snprintf(string, sizeof string, "%c.\r\n",
934 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000935 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000936
Damien Millere1537f92010-01-26 13:26:22 +1100937 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000938 chan_read_failed(c);
939 chan_write_failed(c);
940 return 0;
941 } else
942 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000943 return -1;
944
945 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000946 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +1100947 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000948 noescape:
949 snprintf(string, sizeof string,
950 "%c%c escape not available to "
951 "multiplexed sessions\r\n",
952 escape_char, ch);
953 buffer_append(berr, string,
954 strlen(string));
955 continue;
956 }
Darren Tucker4d5cd332008-06-13 04:51:14 +1000957 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000958 snprintf(string, sizeof string,
959 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +1000960 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +1000961
962 /* Restore terminal modes and suspend. */
963 client_suspend_self(bin, bout, berr);
964
965 /* We have been continued. */
966 continue;
967
Damien Miller54c45982003-05-15 10:20:13 +1000968 case 'B':
969 if (compat20) {
970 snprintf(string, sizeof string,
971 "%cB\r\n", escape_char);
972 buffer_append(berr, string,
973 strlen(string));
974 channel_request_start(session_ident,
975 "break", 0);
976 packet_put_int(1000);
977 packet_send();
978 }
979 continue;
980
Ben Lindstromf28f6342001-04-04 02:03:04 +0000981 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000982 if (compat20) {
983 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000984 logit("Server does not "
985 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +0000986 else
987 need_rekeying = 1;
988 }
Ben Lindstromf28f6342001-04-04 02:03:04 +0000989 continue;
990
Damien Millerad833b32000-08-23 10:46:23 +1000991 case '&':
Damien Millere1537f92010-01-26 13:26:22 +1100992 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000993 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +1000994 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +1000995 * Detach the program (continue to serve
996 * connections, but put in background and no
997 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +1000998 */
Damien Miller96507ef2001-11-12 10:52:25 +1100999 /* Restore tty modes. */
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001000 leave_raw_mode(force_tty_flag);
Damien Miller96507ef2001-11-12 10:52:25 +11001001
1002 /* Stop listening for new connections. */
1003 channel_stop_listening();
1004
1005 snprintf(string, sizeof string,
1006 "%c& [backgrounded]\n", escape_char);
1007 buffer_append(berr, string, strlen(string));
1008
1009 /* Fork into background. */
1010 pid = fork();
1011 if (pid < 0) {
1012 error("fork: %.100s", strerror(errno));
1013 continue;
1014 }
1015 if (pid != 0) { /* This is the parent. */
1016 /* The parent just exits. */
1017 exit(0);
1018 }
1019 /* The child continues serving connections. */
1020 if (compat20) {
1021 buffer_append(bin, "\004", 1);
1022 /* fake EOF on stdin */
1023 return -1;
1024 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001025 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001026 * Sending SSH_CMSG_EOF alone does not
1027 * always appear to be enough. So we
1028 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001029 */
1030 packet_start(SSH_CMSG_STDIN_DATA);
1031 packet_put_string("\004", 1);
1032 packet_send();
1033 /* Close stdin. */
1034 stdin_eof = 1;
1035 if (buffer_len(bin) == 0) {
1036 packet_start(SSH_CMSG_EOF);
1037 packet_send();
1038 }
1039 }
Damien Miller96507ef2001-11-12 10:52:25 +11001040 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001041
1042 case '?':
Damien Millere1537f92010-01-26 13:26:22 +11001043 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001044 snprintf(string, sizeof string,
Damien Millerad833b32000-08-23 10:46:23 +10001045"%c?\r\n\
1046Supported escape sequences:\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001047 %c. - terminate session\r\n\
1048 %cB - send a BREAK to the remote system\r\n\
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001049 %cR - Request rekey (SSH protocol 2 only)\r\n\
1050 %c# - list forwarded connections\r\n\
1051 %c? - this message\r\n\
1052 %c%c - send the escape character by typing it twice\r\n\
Damien Millerad833b32000-08-23 10:46:23 +10001053(Note that escapes are only recognized immediately after newline.)\r\n",
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001054 escape_char, escape_char,
1055 escape_char, escape_char,
1056 escape_char, escape_char,
Damien Miller586b0052008-12-09 14:11:32 +11001057 escape_char, escape_char);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001058 } else {
1059 snprintf(string, sizeof string,
1060"%c?\r\n\
1061Supported escape sequences:\r\n\
1062 %c. - terminate connection (and any multiplexed sessions)\r\n\
1063 %cB - send a BREAK to the remote system\r\n\
1064 %cC - open a command line\r\n\
1065 %cR - Request rekey (SSH protocol 2 only)\r\n\
1066 %c^Z - suspend ssh\r\n\
1067 %c# - list forwarded connections\r\n\
1068 %c& - background ssh (when waiting for connections to terminate)\r\n\
1069 %c? - this message\r\n\
1070 %c%c - send the escape character by typing it twice\r\n\
1071(Note that escapes are only recognized immediately after newline.)\r\n",
1072 escape_char, escape_char,
1073 escape_char, escape_char,
1074 escape_char, escape_char,
1075 escape_char, escape_char,
1076 escape_char, escape_char,
1077 escape_char);
1078 }
Damien Millerad833b32000-08-23 10:46:23 +10001079 buffer_append(berr, string, strlen(string));
1080 continue;
1081
1082 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001083 snprintf(string, sizeof string, "%c#\r\n",
1084 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001085 buffer_append(berr, string, strlen(string));
1086 s = channel_open_message();
1087 buffer_append(berr, s, strlen(s));
1088 xfree(s);
1089 continue;
1090
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001091 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001092 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001093 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001094 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001095 continue;
1096
Damien Millerad833b32000-08-23 10:46:23 +10001097 default:
1098 if (ch != escape_char) {
1099 buffer_put_char(bin, escape_char);
1100 bytes++;
1101 }
1102 /* Escaped characters fall through here */
1103 break;
1104 }
1105 } else {
1106 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001107 * The previous character was not an escape char.
1108 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001109 */
1110 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001111 /*
1112 * It is. Set the flag and continue to
1113 * next character.
1114 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001115 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001116 continue;
1117 }
1118 }
1119
1120 /*
1121 * Normal character. Record whether it was a newline,
1122 * and append it to the buffer.
1123 */
1124 last_was_cr = (ch == '\r' || ch == '\n');
1125 buffer_put_char(bin, ch);
1126 bytes++;
1127 }
1128 return bytes;
1129}
1130
Ben Lindstrombba81212001-06-25 05:01:22 +00001131static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001132client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001133{
Damien Miller166fca82000-04-20 07:42:21 +10001134 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001135 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001136
Damien Miller95def091999-11-25 00:26:21 +11001137 /* Read input from stdin. */
1138 if (FD_ISSET(fileno(stdin), readset)) {
1139 /* Read as much as possible. */
1140 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001141 if (len < 0 &&
1142 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001143 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001144 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001145 /*
1146 * Received EOF or error. They are treated
1147 * similarly, except that an error message is printed
1148 * if it was an error condition.
1149 */
Damien Miller95def091999-11-25 00:26:21 +11001150 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001151 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1152 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001153 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001154 }
1155 /* Mark that we have seen EOF. */
1156 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001157 /*
1158 * Send an EOF message to the server unless there is
1159 * data in the buffer. If there is data in the
1160 * buffer, no message will be sent now. Code
1161 * elsewhere will send the EOF when the buffer
1162 * becomes empty if stdin_eof is set.
1163 */
Damien Miller95def091999-11-25 00:26:21 +11001164 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165 packet_start(SSH_CMSG_EOF);
1166 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001167 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001168 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001169 /*
1170 * Normal successful read, and no escape character.
1171 * Just append the data to buffer.
1172 */
Damien Miller95def091999-11-25 00:26:21 +11001173 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001174 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001175 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001176 * Normal, successful read. But we have an escape
1177 * character and have to process the characters one
1178 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001179 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001180 if (process_escapes(NULL, &stdin_buffer,
1181 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001182 return;
Damien Miller95def091999-11-25 00:26:21 +11001183 }
1184 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185}
1186
Ben Lindstrombba81212001-06-25 05:01:22 +00001187static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001188client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001189{
Damien Miller95def091999-11-25 00:26:21 +11001190 int len;
1191 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192
Damien Miller95def091999-11-25 00:26:21 +11001193 /* Write buffered output to stdout. */
1194 if (FD_ISSET(fileno(stdout), writeset)) {
1195 /* Write as much data as possible. */
1196 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001197 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001198 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001199 if (errno == EINTR || errno == EAGAIN ||
1200 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001201 len = 0;
1202 else {
Damien Miller5428f641999-11-25 11:54:57 +11001203 /*
1204 * An error or EOF was encountered. Put an
1205 * error message to stderr buffer.
1206 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001207 snprintf(buf, sizeof buf,
1208 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001209 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001210 quit_pending = 1;
1211 return;
1212 }
1213 }
1214 /* Consume printed data from the buffer. */
1215 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216 }
Damien Miller95def091999-11-25 00:26:21 +11001217 /* Write buffered output to stderr. */
1218 if (FD_ISSET(fileno(stderr), writeset)) {
1219 /* Write as much data as possible. */
1220 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001221 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001222 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001223 if (errno == EINTR || errno == EAGAIN ||
1224 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001225 len = 0;
1226 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001227 /*
1228 * EOF or error, but can't even print
1229 * error message.
1230 */
Damien Miller95def091999-11-25 00:26:21 +11001231 quit_pending = 1;
1232 return;
1233 }
1234 }
1235 /* Consume printed characters from the buffer. */
1236 buffer_consume(&stderr_buffer, len);
1237 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001238}
1239
Damien Miller5428f641999-11-25 11:54:57 +11001240/*
Damien Millerb38eff82000-04-01 11:09:21 +10001241 * Get packets from the connection input buffer, and process them as long as
1242 * there are packets available.
1243 *
1244 * Any unknown packets received during the actual
1245 * session cause the session to terminate. This is
1246 * intended to make debugging easier since no
1247 * confirmations are sent. Any compatible protocol
1248 * extensions must be negotiated during the
1249 * preparatory phase.
1250 */
1251
Ben Lindstrombba81212001-06-25 05:01:22 +00001252static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001253client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001254{
Darren Tucker4d5cd332008-06-13 04:51:14 +10001255 dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
1256 compat20 ? xxx_kex : NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001257}
1258
Damien Millerad833b32000-08-23 10:46:23 +10001259/* scan buf[] for '~' before sending data to the peer */
1260
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001261/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1262void *
1263client_new_escape_filter_ctx(int escape_char)
1264{
1265 struct escape_filter_ctx *ret;
1266
1267 ret = xmalloc(sizeof(*ret));
1268 ret->escape_pending = 0;
1269 ret->escape_char = escape_char;
1270 return (void *)ret;
1271}
1272
Darren Tucker84c56f52008-06-13 04:55:46 +10001273/* Free the escape filter context on channel free */
1274void
1275client_filter_cleanup(int cid, void *ctx)
1276{
1277 xfree(ctx);
1278}
1279
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001280int
1281client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001282{
Damien Miller5771ed72008-05-19 15:35:33 +10001283 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1284 return 0;
1285
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001286 return process_escapes(c, &c->input, &c->output, &c->extended,
1287 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001288}
1289
Ben Lindstrombba81212001-06-25 05:01:22 +00001290static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001291client_channel_closed(int id, void *arg)
1292{
Damien Miller3ec27592001-10-12 11:35:04 +10001293 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001294 session_closed = 1;
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001295 leave_raw_mode(force_tty_flag);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001296}
1297
Damien Millerb38eff82000-04-01 11:09:21 +10001298/*
Damien Miller5428f641999-11-25 11:54:57 +11001299 * Implements the interactive session with the server. This is called after
1300 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001301 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1302 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001303 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001304
Damien Miller4af51302000-04-16 11:18:38 +10001305int
Damien Millerad833b32000-08-23 10:46:23 +10001306client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307{
Damien Miller5e953212001-01-30 09:14:00 +11001308 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001309 double start_time, total_time;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001310 int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001311 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001312 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001313 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001314
Damien Miller95def091999-11-25 00:26:21 +11001315 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
Damien Miller95def091999-11-25 00:26:21 +11001317 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318
Damien Miller95def091999-11-25 00:26:21 +11001319 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001320 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001321 last_was_cr = 1;
1322 exit_status = -1;
1323 stdin_eof = 0;
1324 buffer_high = 64 * 1024;
1325 connection_in = packet_get_connection_in();
1326 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001327 max_fd = MAX(connection_in, connection_out);
1328
1329 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001330 /* enable nonblocking unless tty */
1331 if (!isatty(fileno(stdin)))
1332 set_nonblock(fileno(stdin));
1333 if (!isatty(fileno(stdout)))
1334 set_nonblock(fileno(stdout));
1335 if (!isatty(fileno(stderr)))
1336 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001337 max_fd = MAX(max_fd, fileno(stdin));
1338 max_fd = MAX(max_fd, fileno(stdout));
1339 max_fd = MAX(max_fd, fileno(stderr));
1340 }
Damien Miller95def091999-11-25 00:26:21 +11001341 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001342 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343
Damien Miller95def091999-11-25 00:26:21 +11001344 /* Initialize buffers. */
1345 buffer_init(&stdin_buffer);
1346 buffer_init(&stdout_buffer);
1347 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348
Damien Millerb38eff82000-04-01 11:09:21 +10001349 client_init_dispatch();
1350
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001351 /*
1352 * Set signal handlers, (e.g. to restore non-blocking mode)
1353 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1354 */
Darren Tucker07336da2004-11-05 20:02:16 +11001355 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1356 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001357 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1358 signal(SIGINT, signal_handler);
1359 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1360 signal(SIGQUIT, signal_handler);
1361 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1362 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001363 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364
Damien Miller95def091999-11-25 00:26:21 +11001365 if (have_pty)
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001366 enter_raw_mode(force_tty_flag);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001367
Ben Lindstrom5b828322001-02-09 01:34:36 +00001368 if (compat20) {
1369 session_ident = ssh2_chan_id;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001370 if (escape_char_arg != SSH_ESCAPECHAR_NONE)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001371 channel_register_filter(session_ident,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001372 client_simple_escape_filter, NULL,
Darren Tucker84c56f52008-06-13 04:55:46 +10001373 client_filter_cleanup,
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001374 client_new_escape_filter_ctx(escape_char_arg));
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001375 if (session_ident != -1)
1376 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001377 client_channel_closed, 0);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001378 } else {
1379 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001380 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001381 }
Damien Millerad833b32000-08-23 10:46:23 +10001382
Damien Miller95def091999-11-25 00:26:21 +11001383 /* Main loop of the client for the interactive session mode. */
1384 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001385
Damien Miller5428f641999-11-25 11:54:57 +11001386 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001387 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001388
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001389 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001390 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001391
1392 rekeying = (xxx_kex != NULL && !xxx_kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001393
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001394 if (rekeying) {
1395 debug("rekeying in progress");
1396 } else {
1397 /*
1398 * Make packets of buffered stdin data, and buffer
1399 * them for sending to the server.
1400 */
1401 if (!compat20)
1402 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001404 /*
1405 * Make packets from buffered channel data, and
1406 * enqueue them for sending to the server.
1407 */
1408 if (packet_not_very_much_data_to_write())
1409 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001411 /*
1412 * Check if the window size has changed, and buffer a
1413 * message about it to the server if so.
1414 */
1415 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001417 if (quit_pending)
1418 break;
1419 }
Damien Miller5428f641999-11-25 11:54:57 +11001420 /*
1421 * Wait until we have something to do (something becomes
1422 * available on one of the descriptors).
1423 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001424 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001425 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001426 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001427
Damien Miller95def091999-11-25 00:26:21 +11001428 if (quit_pending)
1429 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001431 /* Do channel operations unless rekeying in progress. */
1432 if (!rekeying) {
1433 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001434 if (need_rekeying || packet_need_rekeying()) {
1435 debug("need rekeying");
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001436 xxx_kex->done = 0;
1437 kex_send_kexinit(xxx_kex);
1438 need_rekeying = 0;
1439 }
1440 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441
Damien Miller1383bd82000-04-06 12:32:37 +10001442 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001443 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444
Damien Miller1383bd82000-04-06 12:32:37 +10001445 if (quit_pending)
1446 break;
1447
1448 if (!compat20) {
1449 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001450 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001451 /*
1452 * Process output to stdout and stderr. Output to
1453 * the connection is processed elsewhere (above).
1454 */
Damien Miller5e953212001-01-30 09:14:00 +11001455 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001456 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001457
Darren Tuckere32cf432010-01-08 16:51:40 +11001458 if (session_resumed) {
1459 connection_in = packet_get_connection_in();
1460 connection_out = packet_get_connection_out();
1461 max_fd = MAX(max_fd, connection_out);
1462 max_fd = MAX(max_fd, connection_in);
1463 session_resumed = 0;
1464 }
1465
Darren Tucker4d5cd332008-06-13 04:51:14 +10001466 /*
1467 * Send as much buffered packet data as possible to the
1468 * sender.
1469 */
Damien Miller5e953212001-01-30 09:14:00 +11001470 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001471 packet_write_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472 }
Damien Miller5e953212001-01-30 09:14:00 +11001473 if (readset)
1474 xfree(readset);
1475 if (writeset)
1476 xfree(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477
Damien Miller95def091999-11-25 00:26:21 +11001478 /* Terminate the session. */
1479
1480 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001481 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001482
Darren Tuckerde0c0252009-07-06 07:17:35 +10001483 if (compat20) {
1484 packet_start(SSH2_MSG_DISCONNECT);
1485 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1486 packet_put_cstring("disconnected by user");
1487 packet_send();
1488 packet_write_wait();
1489 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001490
Ben Lindstrom601e4362001-06-21 03:19:23 +00001491 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001492
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001493 if (have_pty)
Darren Tucker37c1b3d2010-01-09 22:26:23 +11001494 leave_raw_mode(force_tty_flag);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001495
1496 /* restore blocking io */
1497 if (!isatty(fileno(stdin)))
1498 unset_nonblock(fileno(stdin));
1499 if (!isatty(fileno(stdout)))
1500 unset_nonblock(fileno(stdout));
1501 if (!isatty(fileno(stderr)))
1502 unset_nonblock(fileno(stderr));
1503
Damien Millerd6965512003-12-17 16:31:53 +11001504 /*
1505 * If there was no shell or command requested, there will be no remote
1506 * exit status to be returned. In that case, clear error code if the
1507 * connection was deliberately terminated at this end.
1508 */
1509 if (no_shell_flag && received_signal == SIGTERM) {
1510 received_signal = 0;
1511 exit_status = 0;
1512 }
1513
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001514 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001515 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001516
1517 /*
1518 * In interactive mode (with pseudo tty) display a message indicating
1519 * that the connection has been closed.
1520 */
1521 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001522 snprintf(buf, sizeof buf,
1523 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001524 buffer_append(&stderr_buffer, buf, strlen(buf));
1525 }
1526
Damien Miller95def091999-11-25 00:26:21 +11001527 /* Output any buffered data for stdout. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001528 while (buffer_len(&stdout_buffer) > 0) {
1529 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001530 buffer_len(&stdout_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001531 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001532 error("Write failed flushing stdout buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001533 break;
1534 }
Damien Miller95def091999-11-25 00:26:21 +11001535 buffer_consume(&stdout_buffer, len);
1536 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001537
Damien Miller95def091999-11-25 00:26:21 +11001538 /* Output any buffered data for stderr. */
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001539 while (buffer_len(&stderr_buffer) > 0) {
1540 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001541 buffer_len(&stderr_buffer));
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001542 if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +11001543 error("Write failed flushing stderr buffer.");
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001544 break;
1545 }
Damien Miller95def091999-11-25 00:26:21 +11001546 buffer_consume(&stderr_buffer, len);
1547 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001548
Damien Miller95def091999-11-25 00:26:21 +11001549 /* Clear and free any buffers. */
1550 memset(buf, 0, sizeof(buf));
1551 buffer_free(&stdin_buffer);
1552 buffer_free(&stdout_buffer);
1553 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001554
Damien Miller95def091999-11-25 00:26:21 +11001555 /* Report bytes transferred, and transfer rates. */
1556 total_time = get_current_time() - start_time;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001557 packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
1558 packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
1559 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1560 obytes, ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001561 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001562 verbose("Bytes per second: sent %.1f, received %.1f",
1563 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001564 /* Return the exit status of the program. */
1565 debug("Exit status %d", exit_status);
1566 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567}
Damien Millerb38eff82000-04-01 11:09:21 +10001568
1569/*********/
1570
Ben Lindstrombba81212001-06-25 05:01:22 +00001571static void
Damien Miller630d6f42002-01-22 23:17:30 +11001572client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001573{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001574 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001575 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001576 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001577 buffer_append(&stdout_buffer, data, data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001578 memset(data, 0, data_len);
1579 xfree(data);
1580}
Ben Lindstrombba81212001-06-25 05:01:22 +00001581static void
Damien Miller630d6f42002-01-22 23:17:30 +11001582client_input_stderr_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(&stderr_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_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001593{
Damien Millerb38eff82000-04-01 11:09:21 +10001594 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001595 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001596 /* Acknowledge the exit. */
1597 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1598 packet_send();
1599 /*
1600 * Must wait for packet to be sent since we are
1601 * exiting the loop.
1602 */
1603 packet_write_wait();
1604 /* Flag that we want to exit. */
1605 quit_pending = 1;
1606}
Darren Tucker5dcdd212003-10-02 16:17:00 +10001607static void
1608client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1609{
1610 Channel *c = NULL;
1611 int remote_id, sock;
1612
1613 /* Read the remote channel number from the message. */
1614 remote_id = packet_get_int();
1615 packet_check_eom();
1616
1617 /*
1618 * Get a connection to the local authentication agent (this may again
1619 * get forwarded).
1620 */
1621 sock = ssh_get_authentication_socket();
1622
1623 /*
1624 * If we could not connect the agent, send an error message back to
1625 * the server. This should never happen unless the agent dies,
1626 * because authentication forwarding is only enabled if we have an
1627 * agent.
1628 */
1629 if (sock >= 0) {
1630 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1631 -1, 0, 0, 0, "authentication agent connection", 1);
1632 c->remote_id = remote_id;
1633 c->force_drain = 1;
1634 }
1635 if (c == NULL) {
1636 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1637 packet_put_int(remote_id);
1638 } else {
1639 /* Send a confirmation to the remote host. */
1640 debug("Forwarding authentication connection.");
1641 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1642 packet_put_int(remote_id);
1643 packet_put_int(c->self);
1644 }
1645 packet_send();
1646}
Damien Millerb38eff82000-04-01 11:09:21 +10001647
Ben Lindstrombba81212001-06-25 05:01:22 +00001648static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001649client_request_forwarded_tcpip(const char *request_type, int rchan)
1650{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001651 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001652 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001653 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001654
1655 /* Get rest of the packet */
1656 listen_address = packet_get_string(NULL);
1657 listen_port = packet_get_int();
1658 originator_address = packet_get_string(NULL);
1659 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001660 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001661
Damien Millerbd740252008-05-19 15:37:09 +10001662 debug("client_request_forwarded_tcpip: listen %s port %d, "
1663 "originator %s port %d", listen_address, listen_port,
1664 originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001665
Damien Millerbd740252008-05-19 15:37:09 +10001666 c = channel_connect_by_listen_address(listen_port,
1667 "forwarded-tcpip", originator_address);
1668
Damien Miller0bc1bd82000-11-13 22:57:25 +11001669 xfree(originator_address);
1670 xfree(listen_address);
1671 return c;
1672}
1673
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001674static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001675client_request_x11(const char *request_type, int rchan)
1676{
1677 Channel *c = NULL;
1678 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001679 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001680 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001681
1682 if (!options.forward_x11) {
1683 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001684 error("Warning: this is probably a break-in attempt by a "
1685 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001686 return NULL;
1687 }
1688 originator = packet_get_string(NULL);
1689 if (datafellows & SSH_BUG_X11FWD) {
1690 debug2("buggy server: x11 request w/o originator_port");
1691 originator_port = 0;
1692 } else {
1693 originator_port = packet_get_int();
1694 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001695 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001696 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001697 debug("client_request_x11: request from %s %d", originator,
1698 originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001699 xfree(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001700 sock = x11_connect_display();
1701 if (sock < 0)
1702 return NULL;
1703 c = channel_new("x11",
1704 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001705 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001706 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001707 return c;
1708}
1709
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001710static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001711client_request_agent(const char *request_type, int rchan)
1712{
1713 Channel *c = NULL;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001714 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001715
1716 if (!options.forward_agent) {
1717 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001718 error("Warning: this is probably a break-in attempt by a "
1719 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001720 return NULL;
1721 }
Darren Tucker82a3d2b2007-02-19 22:10:25 +11001722 sock = ssh_get_authentication_socket();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001723 if (sock < 0)
1724 return NULL;
1725 c = channel_new("authentication agent connection",
1726 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001727 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001728 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001729 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001730 return c;
1731}
1732
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001733int
1734client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1735{
1736 Channel *c;
1737 int fd;
1738
1739 if (tun_mode == SSH_TUNMODE_NO)
1740 return 0;
1741
1742 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001743 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001744 return -1;
1745 }
1746
1747 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1748
1749 /* Open local tunnel device */
1750 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1751 error("Tunnel device open failed.");
1752 return -1;
1753 }
1754
1755 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1756 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1757 c->datagram = 1;
1758
1759#if defined(SSH_TUN_FILTER)
1760 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1761 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001762 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001763#endif
1764
1765 packet_start(SSH2_MSG_CHANNEL_OPEN);
1766 packet_put_cstring("tun@openssh.com");
1767 packet_put_int(c->self);
1768 packet_put_int(c->local_window_max);
1769 packet_put_int(c->local_maxpacket);
1770 packet_put_int(tun_mode);
1771 packet_put_int(remote_tun);
1772 packet_send();
1773
1774 return 0;
1775}
1776
Damien Millerbd483e72000-04-30 10:00:53 +10001777/* XXXX move to generic input handler */
Ben Lindstrombba81212001-06-25 05:01:22 +00001778static void
Damien Miller630d6f42002-01-22 23:17:30 +11001779client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001780{
1781 Channel *c = NULL;
1782 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001783 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001784 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001785
1786 ctype = packet_get_string(&len);
1787 rchan = packet_get_int();
1788 rwindow = packet_get_int();
1789 rmaxpack = packet_get_int();
1790
Damien Millere247cc42000-05-07 12:03:14 +10001791 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10001792 ctype, rchan, rwindow, rmaxpack);
1793
Damien Miller0bc1bd82000-11-13 22:57:25 +11001794 if (strcmp(ctype, "forwarded-tcpip") == 0) {
1795 c = client_request_forwarded_tcpip(ctype, rchan);
1796 } else if (strcmp(ctype, "x11") == 0) {
1797 c = client_request_x11(ctype, rchan);
1798 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1799 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10001800 }
1801/* XXX duplicate : */
1802 if (c != NULL) {
1803 debug("confirm %s", ctype);
1804 c->remote_id = rchan;
1805 c->remote_window = rwindow;
1806 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001807 if (c->type != SSH_CHANNEL_CONNECTING) {
1808 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1809 packet_put_int(c->remote_id);
1810 packet_put_int(c->self);
1811 packet_put_int(c->local_window);
1812 packet_put_int(c->local_maxpacket);
1813 packet_send();
1814 }
Damien Millerbd483e72000-04-30 10:00:53 +10001815 } else {
1816 debug("failure %s", ctype);
1817 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1818 packet_put_int(rchan);
1819 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00001820 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00001821 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00001822 packet_put_cstring("");
1823 }
Damien Millerbd483e72000-04-30 10:00:53 +10001824 packet_send();
1825 }
1826 xfree(ctype);
1827}
Ben Lindstrombba81212001-06-25 05:01:22 +00001828static void
Damien Miller630d6f42002-01-22 23:17:30 +11001829client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00001830{
1831 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001832 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00001833 char *rtype;
1834
1835 id = packet_get_int();
1836 rtype = packet_get_string(NULL);
1837 reply = packet_get_char();
1838
1839 debug("client_input_channel_req: channel %d rtype %s reply %d",
1840 id, rtype, reply);
1841
Damien Miller3bbd8782004-06-18 22:23:22 +10001842 if (id == -1) {
1843 error("client_input_channel_req: request for channel -1");
1844 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001845 error("client_input_channel_req: channel %d: "
1846 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10001847 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1848 packet_check_eom();
1849 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001850 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10001851 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11001852 if (c->ctl_chan != -1) {
1853 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10001854 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11001855 } else if (id == session_ident) {
1856 /* Record exit value of local session */
1857 success = 1;
1858 exit_status = exitval;
1859 } else {
1860 error("client_input_channel_req: unexpected channel %d",
1861 session_ident);
Damien Miller0e220db2004-06-15 10:34:08 +10001862 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001863 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001864 }
1865 if (reply) {
1866 packet_start(success ?
1867 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11001868 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00001869 packet_send();
1870 }
1871 xfree(rtype);
1872}
Damien Millerc3fa4072002-01-22 23:21:58 +11001873static void
1874client_input_global_request(int type, u_int32_t seq, void *ctxt)
1875{
1876 char *rtype;
1877 int want_reply;
1878 int success = 0;
1879
1880 rtype = packet_get_string(NULL);
1881 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11001882 debug("client_input_global_request: rtype %s want_reply %d",
1883 rtype, want_reply);
Damien Millerc3fa4072002-01-22 23:21:58 +11001884 if (want_reply) {
1885 packet_start(success ?
1886 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1887 packet_send();
1888 packet_write_wait();
1889 }
1890 xfree(rtype);
1891}
Damien Millerbd483e72000-04-30 10:00:53 +10001892
Damien Miller0e220db2004-06-15 10:34:08 +10001893void
Darren Tuckerfc959702004-07-17 16:12:08 +10001894client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10001895 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10001896{
1897 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11001898 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10001899
1900 debug2("%s: id %d", __func__, id);
1901
Darren Tucker5d78de62004-11-05 20:35:44 +11001902 if ((c = channel_lookup(id)) == NULL)
1903 fatal("client_session2_setup: channel %d: unknown channel", id);
1904
Damien Miller0e220db2004-06-15 10:34:08 +10001905 if (want_tty) {
1906 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10001907
1908 /* Store window size in the packet. */
1909 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1910 memset(&ws, 0, sizeof(ws));
1911
Damien Miller5771ed72008-05-19 15:35:33 +10001912 channel_request_start(id, "pty-req", 1);
1913 client_expect_confirm(id, "PTY allocation", 0);
Damien Miller0e220db2004-06-15 10:34:08 +10001914 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11001915 packet_put_int((u_int)ws.ws_col);
1916 packet_put_int((u_int)ws.ws_row);
1917 packet_put_int((u_int)ws.ws_xpixel);
1918 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10001919 if (tiop == NULL)
1920 tiop = get_saved_tio();
1921 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10001922 packet_send();
1923 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11001924 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10001925 }
1926
1927 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10001928 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10001929 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10001930 char *name, *val;
1931
1932 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10001933 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10001934 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10001935 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10001936 if ((val = strchr(name, '=')) == NULL) {
Damien Miller0a0176e2005-11-05 15:07:59 +11001937 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001938 continue;
1939 }
1940 *val++ = '\0';
1941
1942 matched = 0;
1943 for (j = 0; j < options.num_send_env; j++) {
1944 if (match_pattern(name, options.send_env[j])) {
1945 matched = 1;
1946 break;
1947 }
1948 }
1949 if (!matched) {
1950 debug3("Ignored env %s", name);
Damien Miller0a0176e2005-11-05 15:07:59 +11001951 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001952 continue;
1953 }
1954
1955 debug("Sending env %s = %s", name, val);
1956 channel_request_start(id, "env", 0);
1957 packet_put_cstring(name);
1958 packet_put_cstring(val);
1959 packet_send();
Damien Miller0a0176e2005-11-05 15:07:59 +11001960 xfree(name);
Damien Miller0e220db2004-06-15 10:34:08 +10001961 }
1962 }
1963
1964 len = buffer_len(cmd);
1965 if (len > 0) {
1966 if (len > 900)
1967 len = 900;
1968 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10001969 debug("Sending subsystem: %.*s",
1970 len, (u_char*)buffer_ptr(cmd));
1971 channel_request_start(id, "subsystem", 1);
1972 client_expect_confirm(id, "subsystem", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001973 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10001974 debug("Sending command: %.*s",
1975 len, (u_char*)buffer_ptr(cmd));
1976 channel_request_start(id, "exec", 1);
1977 client_expect_confirm(id, "exec", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001978 }
1979 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
1980 packet_send();
1981 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10001982 channel_request_start(id, "shell", 1);
1983 client_expect_confirm(id, "shell", 1);
Damien Miller0e220db2004-06-15 10:34:08 +10001984 packet_send();
1985 }
1986}
1987
Ben Lindstrombba81212001-06-25 05:01:22 +00001988static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001989client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001990{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00001991 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10001992
Damien Miller1383bd82000-04-06 12:32:37 +10001993 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1994 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1995 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1996 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10001997 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10001998 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1999 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002000 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002001 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002002 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2003 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002004 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002005
2006 /* rekeying */
2007 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002008
2009 /* global request reply messages */
2010 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2011 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002012}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002013
Ben Lindstrombba81212001-06-25 05:01:22 +00002014static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002015client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002016{
2017 dispatch_init(NULL);
2018 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2019 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2020 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002021 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2022 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2023 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002024 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2025 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2026 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002027
2028 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002029 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002030 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2031 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002032}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002033
Ben Lindstrombba81212001-06-25 05:01:22 +00002034static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002035client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002036{
2037 client_init_dispatch_13();
2038 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2039 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2040}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002041
Ben Lindstromdb47f382001-07-04 05:10:27 +00002042static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002043client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002044{
Damien Miller1383bd82000-04-06 12:32:37 +10002045 if (compat20)
2046 client_init_dispatch_20();
2047 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002048 client_init_dispatch_13();
2049 else
2050 client_init_dispatch_15();
2051}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002052
2053/* client specific fatal cleanup */
2054void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002055cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002056{
Darren Tucker37c1b3d2010-01-09 22:26:23 +11002057 leave_raw_mode(force_tty_flag);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002058 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002059 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002060 unlink(options.control_path);
Darren Tucker3e33cec2003-10-02 16:12:36 +10002061 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002062}