blob: 8969869b6e01318e1e271bb0fe1f0fff22f2ca6c [file] [log] [blame]
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00001/* $OpenBSD: clientloop.c,v 1.271 2015/02/23 16:33:25 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
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000064#include <sys/param.h> /* MIN MAX */
Damien Millerf17883e2006-03-15 11:45:54 +110065#include <sys/types.h>
Damien Miller9aec9192006-08-05 10:57:45 +100066#include <sys/ioctl.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>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000088#include <limits.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Millerb84886b2008-05-19 15:05:07 +100090#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100091#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000093#include "ssh1.h"
94#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095#include "packet.h"
96#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100097#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000098#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100099#include "dispatch.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +1100100#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +1000101#include "cipher.h"
Ben Lindstromf28f6342001-04-04 02:03:04 +0000102#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000103#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +1000104#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000105#include "readconf.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000106#include "clientloop.h"
Damien Milleraeb31d62005-12-13 19:29:36 +1100107#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000108#include "authfd.h"
109#include "atomicio.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000110#include "sshpty.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000111#include "match.h"
112#include "msg.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +1000113#include "roaming.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000114#include "ssherr.h"
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000115#include "hostfile.h"
Damien Miller69b69aa2000-10-28 14:19:58 +1100116
117/* import options */
118extern Options options;
119
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120/* Flag indicating that stdin should be redirected from /dev/null. */
121extern int stdin_null_flag;
122
Damien Millerd6965512003-12-17 16:31:53 +1100123/* Flag indicating that no shell has been requested */
124extern int no_shell_flag;
125
Damien Miller0e220db2004-06-15 10:34:08 +1000126/* Control socket */
Damien Millere1537f92010-01-26 13:26:22 +1100127extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
Damien Miller0e220db2004-06-15 10:34:08 +1000128
Damien Miller5428f641999-11-25 11:54:57 +1100129/*
130 * Name of the host we are connecting to. This is the name given on the
131 * command line, or the HostName specified for the user-supplied name in a
132 * configuration file.
133 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134extern char *host;
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
Damien Millere11e1ea2010-08-03 16:04:46 +1000148/* Time when backgrounded control master using ControlPersist should exit */
149static time_t control_persist_exit_time = 0;
150
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151/* Common data for the client loop code. */
Damien Millere1537f92010-01-26 13:26:22 +1100152volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000153static int escape_char1; /* Escape character. (proto1 only) */
154static int escape_pending1; /* Last character was an escape (proto1 only) */
Damien Miller95def091999-11-25 00:26:21 +1100155static int last_was_cr; /* Last character was a newline. */
Darren Tucker4d5cd332008-06-13 04:51:14 +1000156static int exit_status; /* Used to store the command exit status. */
157static int stdin_eof; /* EOF has been encountered on stderr. */
Damien Miller95def091999-11-25 00:26:21 +1100158static Buffer stdin_buffer; /* Buffer for stdin data. */
159static Buffer stdout_buffer; /* Buffer for stdout data. */
160static Buffer stderr_buffer; /* Buffer for stderr data. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000161static u_int buffer_high; /* Soft max buffer size. */
Damien Miller95def091999-11-25 00:26:21 +1100162static int connection_in; /* Connection to server (input). */
163static int connection_out; /* Connection to server (output). */
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000164static int need_rekeying; /* Set to non-zero if rekeying is requested. */
Damien Miller1ab6a512010-06-26 10:02:24 +1000165static int session_closed; /* In SSH2: login session closed. */
166static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static void client_init_dispatch(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000169int session_ident = -1;
170
Darren Tuckere32cf432010-01-08 16:51:40 +1100171int session_resumed = 0;
172
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000173/* Track escape per proto2 channel */
174struct escape_filter_ctx {
175 int escape_pending;
176 int escape_char;
177};
178
179/* Context for channel confirmation replies */
Damien Miller5771ed72008-05-19 15:35:33 +1000180struct channel_reply_ctx {
181 const char *request_type;
Damien Miller555f3b82011-05-15 08:48:05 +1000182 int id;
183 enum confirm_action action;
Damien Miller5771ed72008-05-19 15:35:33 +1000184};
185
Darren Tucker9f407c42008-06-13 04:50:27 +1000186/* Global request success/failure callbacks */
187struct global_confirm {
188 TAILQ_ENTRY(global_confirm) entry;
189 global_confirm_cb *cb;
190 void *ctx;
191 int ref_count;
192};
193TAILQ_HEAD(global_confirms, global_confirm);
194static struct global_confirms global_confirms =
195 TAILQ_HEAD_INITIALIZER(global_confirms);
196
Damien Miller0e220db2004-06-15 10:34:08 +1000197void ssh_process_session2_setup(int, int, int, Buffer *);
198
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199/* Restores stdin to blocking mode. */
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000202leave_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller95def091999-11-25 00:26:21 +1100204 if (in_non_blocking_mode) {
Damien Miller03e66f62004-06-15 15:47:51 +1000205 unset_nonblock(fileno(stdin));
Damien Miller95def091999-11-25 00:26:21 +1100206 in_non_blocking_mode = 0;
Damien Miller95def091999-11-25 00:26:21 +1100207 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208}
209
Damien Miller95def091999-11-25 00:26:21 +1100210/* Puts stdin terminal in non-blocking mode. */
211
Ben Lindstrombba81212001-06-25 05:01:22 +0000212static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000213enter_non_blocking(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214{
Damien Miller95def091999-11-25 00:26:21 +1100215 in_non_blocking_mode = 1;
Damien Miller232711f2004-06-15 10:35:30 +1000216 set_nonblock(fileno(stdin));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217}
218
Damien Miller5428f641999-11-25 11:54:57 +1100219/*
220 * Signal handler for the window change signal (SIGWINCH). This just sets a
221 * flag indicating that the window has changed.
222 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100223/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000224static void
Damien Miller95def091999-11-25 00:26:21 +1100225window_change_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226{
Damien Miller95def091999-11-25 00:26:21 +1100227 received_window_change_signal = 1;
228 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229}
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
232 * Signal handler for signals that cause the program to terminate. These
233 * signals must be trapped to restore terminal modes.
234 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100235/*ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static void
Damien Miller95def091999-11-25 00:26:21 +1100237signal_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000239 received_signal = sig;
240 quit_pending = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241}
242
Damien Miller5428f641999-11-25 11:54:57 +1100243/*
244 * Returns current time in seconds from Jan 1, 1970 with the maximum
245 * available resolution.
246 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Ben Lindstrombba81212001-06-25 05:01:22 +0000248static double
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000249get_current_time(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250{
Damien Miller95def091999-11-25 00:26:21 +1100251 struct timeval tv;
252 gettimeofday(&tv, NULL);
253 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254}
255
Damien Millere11e1ea2010-08-03 16:04:46 +1000256/*
257 * Sets control_persist_exit_time to the absolute time when the
258 * backgrounded control master should exit due to expiry of the
259 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
260 * control master process, or if there is no ControlPersist timeout.
261 */
262static void
263set_control_persist_exit_time(void)
264{
265 if (muxserver_sock == -1 || !options.control_persist
Damien Miller6c3eec72011-05-05 14:16:22 +1000266 || options.control_persist_timeout == 0) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000267 /* not using a ControlPersist timeout */
268 control_persist_exit_time = 0;
Damien Miller6c3eec72011-05-05 14:16:22 +1000269 } else if (channel_still_open()) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000270 /* some client connections are still open */
271 if (control_persist_exit_time > 0)
272 debug2("%s: cancel scheduled exit", __func__);
273 control_persist_exit_time = 0;
274 } else if (control_persist_exit_time <= 0) {
275 /* a client connection has recently closed */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000276 control_persist_exit_time = monotime() +
Damien Millere11e1ea2010-08-03 16:04:46 +1000277 (time_t)options.control_persist_timeout;
278 debug2("%s: schedule exit in %d seconds", __func__,
279 options.control_persist_timeout);
280 }
281 /* else we are already counting down to the timeout */
282}
283
Damien Miller8d60be52012-02-11 08:18:17 +1100284#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
285static int
286client_x11_display_valid(const char *display)
287{
288 size_t i, dlen;
289
290 dlen = strlen(display);
291 for (i = 0; i < dlen; i++) {
Damien Millerfdb23062013-11-21 13:57:15 +1100292 if (!isalnum((u_char)display[i]) &&
Damien Miller8d60be52012-02-11 08:18:17 +1100293 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
294 debug("Invalid character '%c' in DISPLAY", display[i]);
295 return 0;
296 }
297 }
298 return 1;
299}
300
Damien Miller17e7ed02005-06-17 12:54:33 +1000301#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
302void
303client_x11_get_proto(const char *display, const char *xauth_path,
Damien Miller1ab6a512010-06-26 10:02:24 +1000304 u_int trusted, u_int timeout, char **_proto, char **_data)
Damien Miller17e7ed02005-06-17 12:54:33 +1000305{
306 char cmd[1024];
307 char line[512];
308 char xdisplay[512];
309 static char proto[512], data[512];
310 FILE *f;
311 int got_data = 0, generated = 0, do_unlink = 0, i;
312 char *xauthdir, *xauthfile;
313 struct stat st;
Damien Miller1ab6a512010-06-26 10:02:24 +1000314 u_int now;
Damien Miller17e7ed02005-06-17 12:54:33 +1000315
316 xauthdir = xauthfile = NULL;
317 *_proto = proto;
318 *_data = data;
319 proto[0] = data[0] = '\0';
320
321 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
322 debug("No xauth program.");
Damien Miller8d60be52012-02-11 08:18:17 +1100323 } else if (!client_x11_display_valid(display)) {
324 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
325 display);
Damien Miller17e7ed02005-06-17 12:54:33 +1000326 } else {
327 if (display == NULL) {
328 debug("x11_get_proto: DISPLAY not set");
329 return;
330 }
331 /*
332 * Handle FamilyLocal case where $DISPLAY does
333 * not match an authorization entry. For this we
334 * just try "xauth list unix:displaynum.screennum".
335 * XXX: "localhost" match to determine FamilyLocal
336 * is not perfect.
337 */
338 if (strncmp(display, "localhost:", 10) == 0) {
339 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
340 display + 10);
341 display = xdisplay;
342 }
343 if (trusted == 0) {
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000344 xauthdir = xmalloc(PATH_MAX);
345 xauthfile = xmalloc(PATH_MAX);
346 mktemp_proto(xauthdir, PATH_MAX);
Damien Miller17e7ed02005-06-17 12:54:33 +1000347 if (mkdtemp(xauthdir) != NULL) {
348 do_unlink = 1;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000349 snprintf(xauthfile, PATH_MAX, "%s/xauthfile",
Damien Miller17e7ed02005-06-17 12:54:33 +1000350 xauthdir);
351 snprintf(cmd, sizeof(cmd),
352 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller1ab6a512010-06-26 10:02:24 +1000353 " untrusted timeout %u 2>" _PATH_DEVNULL,
354 xauth_path, xauthfile, display, timeout);
Damien Miller17e7ed02005-06-17 12:54:33 +1000355 debug2("x11_get_proto: %s", cmd);
356 if (system(cmd) == 0)
357 generated = 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000358 if (x11_refuse_time == 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000359 now = monotime() + 1;
Damien Miller1ab6a512010-06-26 10:02:24 +1000360 if (UINT_MAX - timeout < now)
361 x11_refuse_time = UINT_MAX;
362 else
363 x11_refuse_time = now + timeout;
364 }
Damien Miller17e7ed02005-06-17 12:54:33 +1000365 }
366 }
Darren Tucker513d13a2007-08-15 19:13:41 +1000367
368 /*
369 * When in untrusted mode, we read the cookie only if it was
370 * successfully generated as an untrusted one in the step
371 * above.
372 */
373 if (trusted || generated) {
374 snprintf(cmd, sizeof(cmd),
375 "%s %s%s list %s 2>" _PATH_DEVNULL,
376 xauth_path,
377 generated ? "-f " : "" ,
378 generated ? xauthfile : "",
379 display);
380 debug2("x11_get_proto: %s", cmd);
381 f = popen(cmd, "r");
382 if (f && fgets(line, sizeof(line), f) &&
383 sscanf(line, "%*s %511s %511s", proto, data) == 2)
384 got_data = 1;
385 if (f)
386 pclose(f);
387 } else
388 error("Warning: untrusted X11 forwarding setup failed: "
389 "xauth key data not generated");
Damien Miller17e7ed02005-06-17 12:54:33 +1000390 }
391
392 if (do_unlink) {
393 unlink(xauthfile);
394 rmdir(xauthdir);
395 }
Darren Tuckera627d422013-06-02 07:31:17 +1000396 free(xauthdir);
397 free(xauthfile);
Damien Miller17e7ed02005-06-17 12:54:33 +1000398
399 /*
400 * If we didn't get authentication data, just make up some
401 * data. The forwarding code will check the validity of the
402 * response anyway, and substitute this data. The X11
403 * server, however, will ignore this fake data and use
404 * whatever authentication mechanisms it was using otherwise
405 * for the local connection.
406 */
407 if (!got_data) {
408 u_int32_t rnd = 0;
409
410 logit("Warning: No xauth data; "
411 "using fake authentication data for X11 forwarding.");
412 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
413 for (i = 0; i < 16; i++) {
414 if (i % 4 == 0)
415 rnd = arc4random();
416 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
417 rnd & 0xff);
418 rnd >>= 8;
419 }
420 }
421}
422
Damien Miller5428f641999-11-25 11:54:57 +1100423/*
424 * This is called when the interactive is entered. This checks if there is
425 * an EOF coming on stdin. We must check this explicitly, as select() does
426 * not appear to wake up when redirecting from /dev/null.
427 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
Ben Lindstrombba81212001-06-25 05:01:22 +0000429static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000430client_check_initial_eof_on_stdin(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431{
Damien Miller95def091999-11-25 00:26:21 +1100432 int len;
433 char buf[1];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434
Damien Miller5428f641999-11-25 11:54:57 +1100435 /*
436 * If standard input is to be "redirected from /dev/null", we simply
437 * mark that we have seen an EOF and send an EOF message to the
438 * server. Otherwise, we try to read a single character; it appears
439 * that for some files, such /dev/null, select() never wakes up for
440 * read for this descriptor, which means that we never get EOF. This
441 * way we will get the EOF if stdin comes from /dev/null or similar.
442 */
Damien Miller95def091999-11-25 00:26:21 +1100443 if (stdin_null_flag) {
444 /* Fake EOF on stdin. */
445 debug("Sending eof.");
446 stdin_eof = 1;
447 packet_start(SSH_CMSG_EOF);
448 packet_send();
449 } else {
Damien Miller95def091999-11-25 00:26:21 +1100450 enter_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Damien Miller95def091999-11-25 00:26:21 +1100452 /* Check for immediate EOF on stdin. */
453 len = read(fileno(stdin), buf, 1);
454 if (len == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000455 /*
456 * EOF. Record that we have seen it and send
457 * EOF to server.
458 */
Damien Miller95def091999-11-25 00:26:21 +1100459 debug("Sending eof.");
460 stdin_eof = 1;
461 packet_start(SSH_CMSG_EOF);
462 packet_send();
463 } else if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100464 /*
465 * Got data. We must store the data in the buffer,
466 * and also process it as an escape character if
467 * appropriate.
468 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000469 if ((u_char) buf[0] == escape_char1)
470 escape_pending1 = 1;
Ben Lindstrome9613cf2001-03-05 06:14:02 +0000471 else
Damien Miller95def091999-11-25 00:26:21 +1100472 buffer_append(&stdin_buffer, buf, 1);
Damien Miller95def091999-11-25 00:26:21 +1100473 }
Damien Miller95def091999-11-25 00:26:21 +1100474 leave_non_blocking();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476}
477
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478
Damien Miller5428f641999-11-25 11:54:57 +1100479/*
480 * Make packets from buffered stdin data, and buffer them for sending to the
481 * connection.
482 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483
Ben Lindstrombba81212001-06-25 05:01:22 +0000484static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000485client_make_packets_from_stdin_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000487 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488
Damien Miller95def091999-11-25 00:26:21 +1100489 /* Send buffered stdin data to the server. */
490 while (buffer_len(&stdin_buffer) > 0 &&
Damien Miller9f0f5c62001-12-21 14:45:46 +1100491 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100492 len = buffer_len(&stdin_buffer);
493 /* Keep the packets at reasonable size. */
494 if (len > packet_get_maxsize())
495 len = packet_get_maxsize();
496 packet_start(SSH_CMSG_STDIN_DATA);
497 packet_put_string(buffer_ptr(&stdin_buffer), len);
498 packet_send();
499 buffer_consume(&stdin_buffer, len);
500 /* If we have a pending EOF, send it now. */
501 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
502 packet_start(SSH_CMSG_EOF);
503 packet_send();
504 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506}
507
Damien Miller5428f641999-11-25 11:54:57 +1100508/*
509 * Checks if the client window has changed, and sends a packet about it to
510 * the server if so. The actual change is detected elsewhere (by a software
511 * interrupt on Unix); this just checks the flag and sends a message if
512 * appropriate.
513 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Ben Lindstrombba81212001-06-25 05:01:22 +0000515static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000516client_check_window_change(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517{
Damien Miller1383bd82000-04-06 12:32:37 +1000518 struct winsize ws;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller1383bd82000-04-06 12:32:37 +1000520 if (! received_window_change_signal)
521 return;
522 /** XXX race */
523 received_window_change_signal = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524
Damien Millerd3444942000-09-30 14:20:03 +1100525 debug2("client_check_window_change: changed");
Damien Miller1383bd82000-04-06 12:32:37 +1000526
527 if (compat20) {
Damien Miller0e220db2004-06-15 10:34:08 +1000528 channel_send_window_changes();
Damien Miller1383bd82000-04-06 12:32:37 +1000529 } else {
Damien Miller0e220db2004-06-15 10:34:08 +1000530 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
531 return;
Damien Miller1383bd82000-04-06 12:32:37 +1000532 packet_start(SSH_CMSG_WINDOW_SIZE);
Damien Miller71a73672006-03-26 14:04:36 +1100533 packet_put_int((u_int)ws.ws_row);
534 packet_put_int((u_int)ws.ws_col);
535 packet_put_int((u_int)ws.ws_xpixel);
536 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller1383bd82000-04-06 12:32:37 +1000537 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539}
540
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000541static int
Damien Miller509b0102003-12-17 16:33:10 +1100542client_global_request_reply(int type, u_int32_t seq, void *ctxt)
543{
Darren Tucker9f407c42008-06-13 04:50:27 +1000544 struct global_confirm *gc;
545
546 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000547 return 0;
Darren Tucker9f407c42008-06-13 04:50:27 +1000548 if (gc->cb != NULL)
549 gc->cb(type, seq, gc->ctx);
550 if (--gc->ref_count <= 0) {
551 TAILQ_REMOVE(&global_confirms, gc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100552 explicit_bzero(gc, sizeof(*gc));
Darren Tuckera627d422013-06-02 07:31:17 +1000553 free(gc);
Darren Tucker9f407c42008-06-13 04:50:27 +1000554 }
555
Darren Tuckerf7288d72009-06-21 18:12:20 +1000556 packet_set_alive_timeouts(0);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000557 return 0;
Damien Miller509b0102003-12-17 16:33:10 +1100558}
559
560static void
561server_alive_check(void)
562{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000563 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
Damien Millerb73b6fd2011-01-11 17:18:56 +1100564 logit("Timeout, server %s not responding.", host);
Damien Miller985a4482006-10-24 03:02:41 +1000565 cleanup_exit(255);
566 }
Damien Miller509b0102003-12-17 16:33:10 +1100567 packet_start(SSH2_MSG_GLOBAL_REQUEST);
568 packet_put_cstring("keepalive@openssh.com");
569 packet_put_char(1); /* boolean: want reply */
570 packet_send();
Darren Tucker9f407c42008-06-13 04:50:27 +1000571 /* Insert an empty placeholder to maintain ordering */
572 client_register_global_confirm(NULL, NULL);
Damien Miller509b0102003-12-17 16:33:10 +1100573}
574
Damien Miller5428f641999-11-25 11:54:57 +1100575/*
576 * Waits until the client can do something (some data becomes available on
577 * one of the file descriptors).
578 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000579static void
Damien Miller5e953212001-01-30 09:14:00 +1100580client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000581 int *maxfdp, u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582{
Damien Miller509b0102003-12-17 16:33:10 +1100583 struct timeval tv, *tvp;
Damien Millere11e1ea2010-08-03 16:04:46 +1000584 int timeout_secs;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000585 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
Damien Miller509b0102003-12-17 16:33:10 +1100586 int ret;
587
Damien Miller5e953212001-01-30 09:14:00 +1100588 /* Add any selections by the channel mechanism. */
Damien Millera6508752012-04-22 11:21:10 +1000589 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
590 &minwait_secs, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591
Damien Miller1383bd82000-04-06 12:32:37 +1000592 if (!compat20) {
593 /* Read from the connection, unless our buffers are full. */
594 if (buffer_len(&stdout_buffer) < buffer_high &&
595 buffer_len(&stderr_buffer) < buffer_high &&
596 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100597 FD_SET(connection_in, *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000598 /*
599 * Read from stdin, unless we have seen EOF or have very much
600 * buffered data to send to the server.
601 */
602 if (!stdin_eof && packet_not_very_much_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100603 FD_SET(fileno(stdin), *readsetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000604
605 /* Select stdout/stderr if have data in buffer. */
606 if (buffer_len(&stdout_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100607 FD_SET(fileno(stdout), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000608 if (buffer_len(&stderr_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100609 FD_SET(fileno(stderr), *writesetp);
Damien Miller1383bd82000-04-06 12:32:37 +1000610 } else {
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000611 /* channel_prepare_select could have closed the last channel */
Damien Miller164a7f42001-10-12 11:36:09 +1000612 if (session_closed && !channel_still_open() &&
613 !packet_have_data_to_write()) {
614 /* clear mask since we did not call select() */
Damien Miller79faeff2001-11-12 11:06:32 +1100615 memset(*readsetp, 0, *nallocp);
616 memset(*writesetp, 0, *nallocp);
Damien Miller164a7f42001-10-12 11:36:09 +1000617 return;
Ben Lindstromc8b3f472001-05-17 03:19:40 +0000618 } else {
619 FD_SET(connection_in, *readsetp);
620 }
Damien Miller1383bd82000-04-06 12:32:37 +1000621 }
622
Damien Miller95def091999-11-25 00:26:21 +1100623 /* Select server connection if have data to write to the server. */
624 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100625 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100626
Damien Miller5428f641999-11-25 11:54:57 +1100627 /*
628 * Wait for something to happen. This will suspend the process until
629 * some selected descriptor can be read, written, or has some other
Damien Millere11e1ea2010-08-03 16:04:46 +1000630 * event pending, or a timeout expires.
Damien Miller5428f641999-11-25 11:54:57 +1100631 */
Damien Miller95def091999-11-25 00:26:21 +1100632
Damien Millere11e1ea2010-08-03 16:04:46 +1000633 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000634 if (options.server_alive_interval > 0 && compat20) {
Damien Millere11e1ea2010-08-03 16:04:46 +1000635 timeout_secs = options.server_alive_interval;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000636 server_alive_time = now + options.server_alive_interval;
637 }
638 if (options.rekey_interval > 0 && compat20 && !rekeying)
639 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout());
Damien Millere11e1ea2010-08-03 16:04:46 +1000640 set_control_persist_exit_time();
641 if (control_persist_exit_time > 0) {
642 timeout_secs = MIN(timeout_secs,
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000643 control_persist_exit_time - now);
Damien Millere11e1ea2010-08-03 16:04:46 +1000644 if (timeout_secs < 0)
645 timeout_secs = 0;
646 }
Damien Millera6508752012-04-22 11:21:10 +1000647 if (minwait_secs != 0)
648 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
Damien Millere11e1ea2010-08-03 16:04:46 +1000649 if (timeout_secs == INT_MAX)
Damien Miller509b0102003-12-17 16:33:10 +1100650 tvp = NULL;
Darren Tuckerfc959702004-07-17 16:12:08 +1000651 else {
Damien Millere11e1ea2010-08-03 16:04:46 +1000652 tv.tv_sec = timeout_secs;
Damien Miller509b0102003-12-17 16:33:10 +1100653 tv.tv_usec = 0;
654 tvp = &tv;
655 }
Damien Millere11e1ea2010-08-03 16:04:46 +1000656
Damien Miller509b0102003-12-17 16:33:10 +1100657 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
658 if (ret < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100659 char buf[100];
Ben Lindstromf9452512001-02-15 03:12:08 +0000660
661 /*
662 * We have to clear the select masks, because we return.
663 * We have to return, because the mainloop checks for the flags
664 * set by the signal handlers.
665 */
Damien Miller79faeff2001-11-12 11:06:32 +1100666 memset(*readsetp, 0, *nallocp);
667 memset(*writesetp, 0, *nallocp);
Ben Lindstromf9452512001-02-15 03:12:08 +0000668
Damien Miller95def091999-11-25 00:26:21 +1100669 if (errno == EINTR)
670 return;
671 /* Note: we might still have data in the buffers. */
672 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
673 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100674 quit_pending = 1;
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000675 } else if (ret == 0) {
676 /*
677 * Timeout. Could have been either keepalive or rekeying.
678 * Keepalive we check here, rekeying is checked in clientloop.
679 */
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000680 if (server_alive_time != 0 && server_alive_time <= monotime())
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000681 server_alive_check();
682 }
683
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684}
685
Ben Lindstrombba81212001-06-25 05:01:22 +0000686static void
Damien Millerad833b32000-08-23 10:46:23 +1000687client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688{
Damien Miller95def091999-11-25 00:26:21 +1100689 /* Flush stdout and stderr buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000690 if (buffer_len(bout) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000691 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
692 buffer_len(bout));
Damien Millerad833b32000-08-23 10:46:23 +1000693 if (buffer_len(berr) > 0)
Darren Tucker4d5cd332008-06-13 04:51:14 +1000694 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
695 buffer_len(berr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696
Damien Miller21771e22011-05-15 08:45:50 +1000697 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698
Damien Miller5428f641999-11-25 11:54:57 +1100699 /*
700 * Free (and clear) the buffer to reduce the amount of data that gets
701 * written to swap.
702 */
Damien Millerad833b32000-08-23 10:46:23 +1000703 buffer_free(bin);
704 buffer_free(bout);
705 buffer_free(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706
Damien Miller95def091999-11-25 00:26:21 +1100707 /* Send the suspend signal to the program itself. */
708 kill(getpid(), SIGTSTP);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709
Darren Tucker5d78de62004-11-05 20:35:44 +1100710 /* Reset window sizes in case they have changed */
711 received_window_change_signal = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712
Damien Miller95def091999-11-25 00:26:21 +1100713 /* OK, we have been continued by the user. Reinitialize buffers. */
Damien Millerad833b32000-08-23 10:46:23 +1000714 buffer_init(bin);
715 buffer_init(bout);
716 buffer_init(berr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717
Damien Miller21771e22011-05-15 08:45:50 +1000718 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719}
720
Ben Lindstrombba81212001-06-25 05:01:22 +0000721static void
Damien Miller90fdfaf2006-03-26 14:25:37 +1100722client_process_net_input(fd_set *readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723{
Darren Tuckerc5564e12009-06-21 18:53:53 +1000724 int len, cont = 0;
Darren Tucker86e30a02009-08-28 11:21:06 +1000725 char buf[SSH_IOBUFSZ];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726
Damien Miller5428f641999-11-25 11:54:57 +1100727 /*
728 * Read input from the server, and add any such data to the buffer of
729 * the packet subsystem.
730 */
Damien Miller95def091999-11-25 00:26:21 +1100731 if (FD_ISSET(connection_in, readset)) {
732 /* Read as much as possible. */
Darren Tuckerc5564e12009-06-21 18:53:53 +1000733 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
734 if (len == 0 && cont == 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000735 /*
736 * Received EOF. The remote host has closed the
737 * connection.
738 */
739 snprintf(buf, sizeof buf,
740 "Connection to %.300s closed by remote host.\r\n",
741 host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743 quit_pending = 1;
744 return;
Damien Miller95def091999-11-25 00:26:21 +1100745 }
Damien Miller5428f641999-11-25 11:54:57 +1100746 /*
747 * There is a kernel bug on Solaris that causes select to
748 * sometimes wake up even though there is no data available.
749 */
Damien Millerd8968ad2008-07-04 23:10:49 +1000750 if (len < 0 &&
751 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Damien Miller95def091999-11-25 00:26:21 +1100752 len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753
Damien Miller95def091999-11-25 00:26:21 +1100754 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +1000755 /*
756 * An error has encountered. Perhaps there is a
757 * network problem.
758 */
759 snprintf(buf, sizeof buf,
760 "Read from remote host %.300s: %.100s\r\n",
761 host, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100762 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100763 quit_pending = 1;
764 return;
765 }
766 packet_process_incoming(buf, len);
767 }
Damien Miller1383bd82000-04-06 12:32:37 +1000768}
769
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000770static void
Damien Miller5771ed72008-05-19 15:35:33 +1000771client_status_confirm(int type, Channel *c, void *ctx)
Damien Miller0e220db2004-06-15 10:34:08 +1000772{
Damien Miller5771ed72008-05-19 15:35:33 +1000773 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
774 char errmsg[256];
775 int tochan;
Darren Tuckerfc959702004-07-17 16:12:08 +1000776
Damien Miller555f3b82011-05-15 08:48:05 +1000777 /*
778 * If a TTY was explicitly requested, then a failure to allocate
779 * one is fatal.
780 */
781 if (cr->action == CONFIRM_TTY &&
782 (options.request_tty == REQUEST_TTY_FORCE ||
783 options.request_tty == REQUEST_TTY_YES))
784 cr->action = CONFIRM_CLOSE;
785
Damien Miller5771ed72008-05-19 15:35:33 +1000786 /* XXX supress on mux _client_ quietmode */
787 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
Damien Millere1537f92010-01-26 13:26:22 +1100788 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
Damien Miller0e220db2004-06-15 10:34:08 +1000789
Damien Miller5771ed72008-05-19 15:35:33 +1000790 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
791 debug2("%s request accepted on channel %d",
792 cr->request_type, c->self);
793 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
794 if (tochan) {
795 snprintf(errmsg, sizeof(errmsg),
796 "%s request failed\r\n", cr->request_type);
797 } else {
798 snprintf(errmsg, sizeof(errmsg),
799 "%s request failed on channel %d",
800 cr->request_type, c->self);
801 }
802 /* If error occurred on primary session channel, then exit */
Damien Miller555f3b82011-05-15 08:48:05 +1000803 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
Damien Miller5771ed72008-05-19 15:35:33 +1000804 fatal("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000805 /*
806 * If error occurred on mux client, append to
807 * their stderr.
808 */
809 if (tochan) {
810 buffer_append(&c->extended, errmsg,
811 strlen(errmsg));
812 } else
Damien Miller5771ed72008-05-19 15:35:33 +1000813 error("%s", errmsg);
Damien Miller555f3b82011-05-15 08:48:05 +1000814 if (cr->action == CONFIRM_TTY) {
815 /*
816 * If a TTY allocation error occurred, then arrange
817 * for the correct TTY to leave raw mode.
818 */
819 if (c->self == session_ident)
820 leave_raw_mode(0);
821 else
822 mux_tty_alloc_failed(c);
823 } else if (cr->action == CONFIRM_CLOSE) {
Damien Miller5771ed72008-05-19 15:35:33 +1000824 chan_read_failed(c);
825 chan_write_failed(c);
826 }
Damien Miller0e220db2004-06-15 10:34:08 +1000827 }
Darren Tuckera627d422013-06-02 07:31:17 +1000828 free(cr);
Damien Miller5771ed72008-05-19 15:35:33 +1000829}
Damien Miller0e220db2004-06-15 10:34:08 +1000830
Damien Miller5771ed72008-05-19 15:35:33 +1000831static void
832client_abandon_status_confirm(Channel *c, void *ctx)
833{
Darren Tuckera627d422013-06-02 07:31:17 +1000834 free(ctx);
Damien Miller5771ed72008-05-19 15:35:33 +1000835}
836
Damien Miller6d7b4372011-06-23 08:31:57 +1000837void
Damien Miller555f3b82011-05-15 08:48:05 +1000838client_expect_confirm(int id, const char *request,
839 enum confirm_action action)
Damien Miller5771ed72008-05-19 15:35:33 +1000840{
Damien Miller6c81fee2013-11-08 12:19:55 +1100841 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
Damien Miller5771ed72008-05-19 15:35:33 +1000842
843 cr->request_type = request;
Damien Miller555f3b82011-05-15 08:48:05 +1000844 cr->action = action;
Damien Miller5771ed72008-05-19 15:35:33 +1000845
846 channel_register_status_confirm(id, client_status_confirm,
847 client_abandon_status_confirm, cr);
Damien Miller0e220db2004-06-15 10:34:08 +1000848}
849
Darren Tucker9f407c42008-06-13 04:50:27 +1000850void
851client_register_global_confirm(global_confirm_cb *cb, void *ctx)
852{
Damien Millerb9d3bee2008-07-16 22:40:52 +1000853 struct global_confirm *gc, *last_gc;
Darren Tucker9f407c42008-06-13 04:50:27 +1000854
855 /* Coalesce identical callbacks */
Damien Millerb9d3bee2008-07-16 22:40:52 +1000856 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
857 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
858 if (++last_gc->ref_count >= INT_MAX)
859 fatal("%s: last_gc->ref_count = %d",
860 __func__, last_gc->ref_count);
Darren Tucker9f407c42008-06-13 04:50:27 +1000861 return;
862 }
863
Damien Miller6c81fee2013-11-08 12:19:55 +1100864 gc = xcalloc(1, sizeof(*gc));
Darren Tucker9f407c42008-06-13 04:50:27 +1000865 gc->cb = cb;
866 gc->ctx = ctx;
867 gc->ref_count = 1;
868 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
869}
870
Damien Miller0e220db2004-06-15 10:34:08 +1000871static void
Ben Lindstrom681d9322002-03-22 03:53:00 +0000872process_cmdline(void)
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000873{
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000874 void (*handler)(int);
Damien Miller7acefbb2014-07-18 14:11:24 +1000875 char *s, *cmd;
876 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
877 struct Forward fwd;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000878
Damien Miller1d2c4562014-02-04 11:18:20 +1100879 memset(&fwd, 0, sizeof(fwd));
Darren Tucker23ae8ca2007-12-02 23:12:30 +1100880
Damien Miller21771e22011-05-15 08:45:50 +1000881 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000882 handler = signal(SIGINT, SIG_IGN);
Ben Lindstrom681d9322002-03-22 03:53:00 +0000883 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000884 if (s == NULL)
885 goto out;
Damien Millerfdb23062013-11-21 13:57:15 +1100886 while (isspace((u_char)*s))
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000887 s++;
Darren Tuckere7066df2004-05-24 10:18:05 +1000888 if (*s == '-')
889 s++; /* Skip cmdline '-', if any */
Darren Tuckere1675822004-05-24 10:13:07 +1000890 if (*s == '\0')
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000891 goto out;
Darren Tuckere7066df2004-05-24 10:18:05 +1000892
Darren Tucker1973c882004-05-24 10:34:36 +1000893 if (*s == 'h' || *s == 'H' || *s == '?') {
Darren Tuckere7066df2004-05-24 10:18:05 +1000894 logit("Commands:");
Damien Miller43020952006-07-10 20:16:12 +1000895 logit(" -L[bind_address:]port:host:hostport "
896 "Request local forward");
897 logit(" -R[bind_address:]port:host:hostport "
898 "Request remote forward");
Damien Miller0164cb82008-11-05 16:30:31 +1100899 logit(" -D[bind_address:]port "
900 "Request dynamic forward");
Damien Millerff773642011-09-22 21:39:48 +1000901 logit(" -KL[bind_address:]port "
902 "Cancel local forward");
Damien Miller57e8ad32006-07-10 20:20:52 +1000903 logit(" -KR[bind_address:]port "
Damien Miller43020952006-07-10 20:16:12 +1000904 "Cancel remote forward");
Damien Millerff773642011-09-22 21:39:48 +1000905 logit(" -KD[bind_address:]port "
906 "Cancel dynamic forward");
Damien Millerd27b9472005-12-13 19:29:02 +1100907 if (!options.permit_local_command)
908 goto out;
Damien Miller43020952006-07-10 20:16:12 +1000909 logit(" !args "
910 "Execute local command");
Damien Millerd27b9472005-12-13 19:29:02 +1100911 goto out;
912 }
913
914 if (*s == '!' && options.permit_local_command) {
915 s++;
916 ssh_local_cmd(s);
Darren Tuckere7066df2004-05-24 10:18:05 +1000917 goto out;
918 }
919
920 if (*s == 'K') {
921 delete = 1;
922 s++;
923 }
Damien Miller0164cb82008-11-05 16:30:31 +1100924 if (*s == 'L')
925 local = 1;
926 else if (*s == 'R')
927 remote = 1;
928 else if (*s == 'D')
929 dynamic = 1;
930 else {
Damien Miller996acd22003-04-09 20:59:48 +1000931 logit("Invalid command.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000932 goto out;
933 }
Damien Miller0164cb82008-11-05 16:30:31 +1100934
Damien Millerff773642011-09-22 21:39:48 +1000935 if (delete && !compat20) {
Damien Miller996acd22003-04-09 20:59:48 +1000936 logit("Not supported for SSH protocol version 1.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000937 goto out;
938 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000939
Damien Millerfdb23062013-11-21 13:57:15 +1100940 while (isspace((u_char)*++s))
Darren Tucker03b1cdb2007-03-21 20:46:03 +1100941 ;
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000942
Damien Millere1537f92010-01-26 13:26:22 +1100943 /* XXX update list of forwards in options */
Darren Tuckere7066df2004-05-24 10:18:05 +1000944 if (delete) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000945 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
946 if (!parse_forward(&fwd, s, 1, 0)) {
947 logit("Bad forwarding close specification.");
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000948 goto out;
949 }
Damien Millerff773642011-09-22 21:39:48 +1000950 if (remote)
Damien Miller7acefbb2014-07-18 14:11:24 +1000951 ok = channel_request_rforward_cancel(&fwd) == 0;
Damien Millerff773642011-09-22 21:39:48 +1000952 else if (dynamic)
Damien Miller7acefbb2014-07-18 14:11:24 +1000953 ok = channel_cancel_lport_listener(&fwd,
954 0, &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000955 else
Damien Miller7acefbb2014-07-18 14:11:24 +1000956 ok = channel_cancel_lport_listener(&fwd,
957 CHANNEL_CANCEL_PORT_STATIC,
958 &options.fwd_opts) > 0;
Damien Millerff773642011-09-22 21:39:48 +1000959 if (!ok) {
960 logit("Unkown port forwarding.");
961 goto out;
962 }
963 logit("Canceled forwarding.");
Darren Tuckere7066df2004-05-24 10:18:05 +1000964 } else {
Damien Miller4bf648f2009-02-14 16:28:21 +1100965 if (!parse_forward(&fwd, s, dynamic, remote)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000966 logit("Bad forwarding specification.");
967 goto out;
968 }
Damien Miller0164cb82008-11-05 16:30:31 +1100969 if (local || dynamic) {
Damien Miller7acefbb2014-07-18 14:11:24 +1000970 if (!channel_setup_local_fwd_listener(&fwd,
971 &options.fwd_opts)) {
Darren Tuckere7066df2004-05-24 10:18:05 +1000972 logit("Port forwarding failed.");
973 goto out;
974 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100975 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +1000976 if (channel_request_remote_forwarding(&fwd) < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +1000977 logit("Port forwarding failed.");
978 goto out;
979 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100980 }
Darren Tuckere7066df2004-05-24 10:18:05 +1000981 logit("Forwarding port.");
982 }
983
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000984out:
985 signal(SIGINT, handler);
Damien Miller21771e22011-05-15 08:45:50 +1000986 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tuckera627d422013-06-02 07:31:17 +1000987 free(cmd);
988 free(fwd.listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000989 free(fwd.listen_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000990 free(fwd.connect_host);
Damien Miller7acefbb2014-07-18 14:11:24 +1000991 free(fwd.connect_path);
Ben Lindstrom5589f4b2002-03-22 03:24:32 +0000992}
993
Darren Tucker92a39cf2012-09-07 11:20:20 +1000994/* reasons to suppress output of an escape command in help output */
995#define SUPPRESS_NEVER 0 /* never suppress, always show */
996#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
997#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
998#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
999#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
1000struct escape_help_text {
1001 const char *cmd;
1002 const char *text;
1003 unsigned int flags;
1004};
1005static struct escape_help_text esc_txt[] = {
1006 {".", "terminate session", SUPPRESS_MUXMASTER},
1007 {".", "terminate connection (and any multiplexed sessions)",
1008 SUPPRESS_MUXCLIENT},
1009 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1010 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1011 {"R", "request rekey", SUPPRESS_PROTO1},
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001012 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
Darren Tucker92a39cf2012-09-07 11:20:20 +10001013 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1014 {"#", "list forwarded connections", SUPPRESS_NEVER},
1015 {"&", "background ssh (when waiting for connections to terminate)",
1016 SUPPRESS_MUXCLIENT},
1017 {"?", "this message", SUPPRESS_NEVER},
1018};
1019
1020static void
1021print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1022 int using_stderr)
1023{
1024 unsigned int i, suppress_flags;
1025 char string[1024];
1026
1027 snprintf(string, sizeof string, "%c?\r\n"
1028 "Supported escape sequences:\r\n", escape_char);
1029 buffer_append(b, string, strlen(string));
1030
1031 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1032 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1033 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1034 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1035
1036 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1037 if (esc_txt[i].flags & suppress_flags)
1038 continue;
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001039 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
Darren Tucker92a39cf2012-09-07 11:20:20 +10001040 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1041 buffer_append(b, string, strlen(string));
1042 }
1043
1044 snprintf(string, sizeof string,
Darren Tuckerca0d0fd2012-09-07 11:22:24 +10001045 " %c%c - send the escape character by typing it twice\r\n"
Darren Tucker92a39cf2012-09-07 11:20:20 +10001046 "(Note that escapes are only recognized immediately after "
1047 "newline.)\r\n", escape_char, escape_char);
1048 buffer_append(b, string, strlen(string));
1049}
1050
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001051/*
1052 * Process the characters one by one, call with c==NULL for proto1 case.
1053 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001054static int
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001055process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1056 char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001057{
1058 char string[1024];
1059 pid_t pid;
1060 int bytes = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001061 u_int i;
1062 u_char ch;
Damien Millerad833b32000-08-23 10:46:23 +10001063 char *s;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001064 int *escape_pendingp, escape_char;
1065 struct escape_filter_ctx *efc;
Damien Millerad833b32000-08-23 10:46:23 +10001066
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001067 if (c == NULL) {
1068 escape_pendingp = &escape_pending1;
1069 escape_char = escape_char1;
1070 } else {
1071 if (c->filter_ctx == NULL)
1072 return 0;
1073 efc = (struct escape_filter_ctx *)c->filter_ctx;
1074 escape_pendingp = &efc->escape_pending;
1075 escape_char = efc->escape_char;
1076 }
1077
Damien Millereccb9de2005-06-17 12:59:34 +10001078 if (len <= 0)
1079 return (0);
1080
1081 for (i = 0; i < (u_int)len; i++) {
Damien Millerad833b32000-08-23 10:46:23 +10001082 /* Get one character at a time. */
1083 ch = buf[i];
1084
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001085 if (*escape_pendingp) {
Damien Millerad833b32000-08-23 10:46:23 +10001086 /* We have previously seen an escape character. */
1087 /* Clear the flag now. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001088 *escape_pendingp = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001089
1090 /* Process the escaped character. */
1091 switch (ch) {
1092 case '.':
1093 /* Terminate the connection. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001094 snprintf(string, sizeof string, "%c.\r\n",
1095 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001096 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001097
Damien Millere1537f92010-01-26 13:26:22 +11001098 if (c && c->ctl_chan != -1) {
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001099 chan_read_failed(c);
1100 chan_write_failed(c);
Darren Tuckerea8342c2013-06-06 08:11:40 +10001101 if (c->detach_user)
1102 c->detach_user(c->self, NULL);
Damien Miller36187092013-06-10 13:07:11 +10001103 c->type = SSH_CHANNEL_ABANDONED;
1104 buffer_clear(&c->input);
1105 chan_ibuf_empty(c);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001106 return 0;
1107 } else
1108 quit_pending = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001109 return -1;
1110
1111 case 'Z' - 64:
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001112 /* XXX support this for mux clients */
Damien Millere1537f92010-01-26 13:26:22 +11001113 if (c && c->ctl_chan != -1) {
Darren Tuckerf111d402012-09-07 11:21:42 +10001114 char b[16];
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001115 noescape:
Darren Tuckerf111d402012-09-07 11:21:42 +10001116 if (ch == 'Z' - 64)
1117 snprintf(b, sizeof b, "^Z");
1118 else
1119 snprintf(b, sizeof b, "%c", ch);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001120 snprintf(string, sizeof string,
Darren Tuckerf111d402012-09-07 11:21:42 +10001121 "%c%s escape not available to "
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001122 "multiplexed sessions\r\n",
Darren Tuckerf111d402012-09-07 11:21:42 +10001123 escape_char, b);
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001124 buffer_append(berr, string,
1125 strlen(string));
1126 continue;
1127 }
Darren Tucker4d5cd332008-06-13 04:51:14 +10001128 /* Suspend the program. Inform the user */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001129 snprintf(string, sizeof string,
1130 "%c^Z [suspend ssh]\r\n", escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001131 buffer_append(berr, string, strlen(string));
Damien Millerad833b32000-08-23 10:46:23 +10001132
1133 /* Restore terminal modes and suspend. */
1134 client_suspend_self(bin, bout, berr);
1135
1136 /* We have been continued. */
1137 continue;
1138
Damien Miller54c45982003-05-15 10:20:13 +10001139 case 'B':
1140 if (compat20) {
1141 snprintf(string, sizeof string,
1142 "%cB\r\n", escape_char);
1143 buffer_append(berr, string,
1144 strlen(string));
Damien Miller70182522013-09-14 09:49:19 +10001145 channel_request_start(c->self,
Damien Miller54c45982003-05-15 10:20:13 +10001146 "break", 0);
1147 packet_put_int(1000);
1148 packet_send();
1149 }
1150 continue;
1151
Ben Lindstromf28f6342001-04-04 02:03:04 +00001152 case 'R':
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001153 if (compat20) {
1154 if (datafellows & SSH_BUG_NOREKEY)
Darren Tucker4d5cd332008-06-13 04:51:14 +10001155 logit("Server does not "
1156 "support re-keying");
Ben Lindstrom11bd8992001-04-05 23:34:29 +00001157 else
1158 need_rekeying = 1;
1159 }
Ben Lindstromf28f6342001-04-04 02:03:04 +00001160 continue;
1161
Darren Tucker50a48d02012-09-06 21:25:37 +10001162 case 'V':
1163 /* FALLTHROUGH */
1164 case 'v':
1165 if (c && c->ctl_chan != -1)
1166 goto noescape;
1167 if (!log_is_on_stderr()) {
1168 snprintf(string, sizeof string,
1169 "%c%c [Logging to syslog]\r\n",
1170 escape_char, ch);
1171 buffer_append(berr, string,
1172 strlen(string));
1173 continue;
1174 }
1175 if (ch == 'V' && options.log_level >
1176 SYSLOG_LEVEL_QUIET)
1177 log_change_level(--options.log_level);
1178 if (ch == 'v' && options.log_level <
1179 SYSLOG_LEVEL_DEBUG3)
1180 log_change_level(++options.log_level);
1181 snprintf(string, sizeof string,
1182 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1183 log_level_name(options.log_level));
1184 buffer_append(berr, string, strlen(string));
1185 continue;
1186
Damien Millerad833b32000-08-23 10:46:23 +10001187 case '&':
Damien Millere1537f92010-01-26 13:26:22 +11001188 if (c && c->ctl_chan != -1)
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001189 goto noescape;
Damien Millerad833b32000-08-23 10:46:23 +10001190 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001191 * Detach the program (continue to serve
1192 * connections, but put in background and no
1193 * more new connections).
Damien Millerad833b32000-08-23 10:46:23 +10001194 */
Damien Miller96507ef2001-11-12 10:52:25 +11001195 /* Restore tty modes. */
Damien Miller21771e22011-05-15 08:45:50 +10001196 leave_raw_mode(
1197 options.request_tty == REQUEST_TTY_FORCE);
Damien Miller96507ef2001-11-12 10:52:25 +11001198
1199 /* Stop listening for new connections. */
1200 channel_stop_listening();
1201
1202 snprintf(string, sizeof string,
1203 "%c& [backgrounded]\n", escape_char);
1204 buffer_append(berr, string, strlen(string));
1205
1206 /* Fork into background. */
1207 pid = fork();
1208 if (pid < 0) {
1209 error("fork: %.100s", strerror(errno));
1210 continue;
1211 }
1212 if (pid != 0) { /* This is the parent. */
1213 /* The parent just exits. */
1214 exit(0);
1215 }
1216 /* The child continues serving connections. */
1217 if (compat20) {
1218 buffer_append(bin, "\004", 1);
1219 /* fake EOF on stdin */
1220 return -1;
1221 } else if (!stdin_eof) {
Damien Millerad833b32000-08-23 10:46:23 +10001222 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001223 * Sending SSH_CMSG_EOF alone does not
1224 * always appear to be enough. So we
1225 * try to send an EOF character first.
Damien Millerad833b32000-08-23 10:46:23 +10001226 */
1227 packet_start(SSH_CMSG_STDIN_DATA);
1228 packet_put_string("\004", 1);
1229 packet_send();
1230 /* Close stdin. */
1231 stdin_eof = 1;
1232 if (buffer_len(bin) == 0) {
1233 packet_start(SSH_CMSG_EOF);
1234 packet_send();
1235 }
1236 }
Damien Miller96507ef2001-11-12 10:52:25 +11001237 continue;
Damien Millerad833b32000-08-23 10:46:23 +10001238
1239 case '?':
Darren Tucker92a39cf2012-09-07 11:20:20 +10001240 print_escape_help(berr, escape_char, compat20,
1241 (c && c->ctl_chan != -1),
1242 log_is_on_stderr());
Damien Millerad833b32000-08-23 10:46:23 +10001243 continue;
1244
1245 case '#':
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001246 snprintf(string, sizeof string, "%c#\r\n",
1247 escape_char);
Damien Millerad833b32000-08-23 10:46:23 +10001248 buffer_append(berr, string, strlen(string));
1249 s = channel_open_message();
1250 buffer_append(berr, s, strlen(s));
Darren Tuckera627d422013-06-02 07:31:17 +10001251 free(s);
Damien Millerad833b32000-08-23 10:46:23 +10001252 continue;
1253
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001254 case 'C':
Damien Millere1537f92010-01-26 13:26:22 +11001255 if (c && c->ctl_chan != -1)
Damien Miller586b0052008-12-09 14:11:32 +11001256 goto noescape;
Ben Lindstrom681d9322002-03-22 03:53:00 +00001257 process_cmdline();
Ben Lindstrom5589f4b2002-03-22 03:24:32 +00001258 continue;
1259
Damien Millerad833b32000-08-23 10:46:23 +10001260 default:
1261 if (ch != escape_char) {
1262 buffer_put_char(bin, escape_char);
1263 bytes++;
1264 }
1265 /* Escaped characters fall through here */
1266 break;
1267 }
1268 } else {
1269 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001270 * The previous character was not an escape char.
1271 * Check if this is an escape.
Damien Millerad833b32000-08-23 10:46:23 +10001272 */
1273 if (last_was_cr && ch == escape_char) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001274 /*
1275 * It is. Set the flag and continue to
1276 * next character.
1277 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001278 *escape_pendingp = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001279 continue;
1280 }
1281 }
1282
1283 /*
1284 * Normal character. Record whether it was a newline,
1285 * and append it to the buffer.
1286 */
1287 last_was_cr = (ch == '\r' || ch == '\n');
1288 buffer_put_char(bin, ch);
1289 bytes++;
1290 }
1291 return bytes;
1292}
1293
Ben Lindstrombba81212001-06-25 05:01:22 +00001294static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001295client_process_input(fd_set *readset)
Damien Miller1383bd82000-04-06 12:32:37 +10001296{
Damien Miller166fca82000-04-20 07:42:21 +10001297 int len;
Darren Tucker86e30a02009-08-28 11:21:06 +10001298 char buf[SSH_IOBUFSZ];
Damien Miller1383bd82000-04-06 12:32:37 +10001299
Damien Miller95def091999-11-25 00:26:21 +11001300 /* Read input from stdin. */
1301 if (FD_ISSET(fileno(stdin), readset)) {
1302 /* Read as much as possible. */
1303 len = read(fileno(stdin), buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001304 if (len < 0 &&
1305 (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
Ben Lindstrom4c8cff12001-04-17 18:09:42 +00001306 return; /* we'll try again later */
Damien Miller95def091999-11-25 00:26:21 +11001307 if (len <= 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001308 /*
1309 * Received EOF or error. They are treated
1310 * similarly, except that an error message is printed
1311 * if it was an error condition.
1312 */
Damien Miller95def091999-11-25 00:26:21 +11001313 if (len < 0) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001314 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1315 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001316 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001317 }
1318 /* Mark that we have seen EOF. */
1319 stdin_eof = 1;
Damien Miller5428f641999-11-25 11:54:57 +11001320 /*
1321 * Send an EOF message to the server unless there is
1322 * data in the buffer. If there is data in the
1323 * buffer, no message will be sent now. Code
1324 * elsewhere will send the EOF when the buffer
1325 * becomes empty if stdin_eof is set.
1326 */
Damien Miller95def091999-11-25 00:26:21 +11001327 if (buffer_len(&stdin_buffer) == 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001328 packet_start(SSH_CMSG_EOF);
1329 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11001330 }
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001331 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
Damien Miller5428f641999-11-25 11:54:57 +11001332 /*
1333 * Normal successful read, and no escape character.
1334 * Just append the data to buffer.
1335 */
Damien Miller95def091999-11-25 00:26:21 +11001336 buffer_append(&stdin_buffer, buf, len);
Damien Miller95def091999-11-25 00:26:21 +11001337 } else {
Damien Miller5428f641999-11-25 11:54:57 +11001338 /*
Darren Tucker4d5cd332008-06-13 04:51:14 +10001339 * Normal, successful read. But we have an escape
1340 * character and have to process the characters one
1341 * by one.
Damien Miller5428f641999-11-25 11:54:57 +11001342 */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001343 if (process_escapes(NULL, &stdin_buffer,
1344 &stdout_buffer, &stderr_buffer, buf, len) == -1)
Damien Millerad833b32000-08-23 10:46:23 +10001345 return;
Damien Miller95def091999-11-25 00:26:21 +11001346 }
1347 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348}
1349
Ben Lindstrombba81212001-06-25 05:01:22 +00001350static void
Damien Miller90fdfaf2006-03-26 14:25:37 +11001351client_process_output(fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352{
Damien Miller95def091999-11-25 00:26:21 +11001353 int len;
1354 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355
Damien Miller95def091999-11-25 00:26:21 +11001356 /* Write buffered output to stdout. */
1357 if (FD_ISSET(fileno(stdout), writeset)) {
1358 /* Write as much data as possible. */
1359 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001360 buffer_len(&stdout_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001361 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001362 if (errno == EINTR || errno == EAGAIN ||
1363 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001364 len = 0;
1365 else {
Damien Miller5428f641999-11-25 11:54:57 +11001366 /*
1367 * An error or EOF was encountered. Put an
1368 * error message to stderr buffer.
1369 */
Darren Tucker4d5cd332008-06-13 04:51:14 +10001370 snprintf(buf, sizeof buf,
1371 "write stdout: %.50s\r\n", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001372 buffer_append(&stderr_buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001373 quit_pending = 1;
1374 return;
1375 }
1376 }
1377 /* Consume printed data from the buffer. */
1378 buffer_consume(&stdout_buffer, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379 }
Damien Miller95def091999-11-25 00:26:21 +11001380 /* Write buffered output to stderr. */
1381 if (FD_ISSET(fileno(stderr), writeset)) {
1382 /* Write as much data as possible. */
1383 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +11001384 buffer_len(&stderr_buffer));
Damien Miller95def091999-11-25 00:26:21 +11001385 if (len <= 0) {
Damien Millerd8968ad2008-07-04 23:10:49 +10001386 if (errno == EINTR || errno == EAGAIN ||
1387 errno == EWOULDBLOCK)
Damien Miller95def091999-11-25 00:26:21 +11001388 len = 0;
1389 else {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001390 /*
1391 * EOF or error, but can't even print
1392 * error message.
1393 */
Damien Miller95def091999-11-25 00:26:21 +11001394 quit_pending = 1;
1395 return;
1396 }
1397 }
1398 /* Consume printed characters from the buffer. */
1399 buffer_consume(&stderr_buffer, len);
1400 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401}
1402
Damien Miller5428f641999-11-25 11:54:57 +11001403/*
Damien Millerb38eff82000-04-01 11:09:21 +10001404 * Get packets from the connection input buffer, and process them as long as
1405 * there are packets available.
1406 *
1407 * Any unknown packets received during the actual
1408 * session cause the session to terminate. This is
1409 * intended to make debugging easier since no
1410 * confirmations are sent. Any compatible protocol
1411 * extensions must be negotiated during the
1412 * preparatory phase.
1413 */
1414
Ben Lindstrombba81212001-06-25 05:01:22 +00001415static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001416client_process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001417{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001418 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state);
Damien Millerb38eff82000-04-01 11:09:21 +10001419}
1420
Damien Millerad833b32000-08-23 10:46:23 +10001421/* scan buf[] for '~' before sending data to the peer */
1422
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001423/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1424void *
1425client_new_escape_filter_ctx(int escape_char)
1426{
1427 struct escape_filter_ctx *ret;
1428
Damien Miller6c81fee2013-11-08 12:19:55 +11001429 ret = xcalloc(1, sizeof(*ret));
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001430 ret->escape_pending = 0;
1431 ret->escape_char = escape_char;
1432 return (void *)ret;
1433}
1434
Darren Tucker84c56f52008-06-13 04:55:46 +10001435/* Free the escape filter context on channel free */
1436void
1437client_filter_cleanup(int cid, void *ctx)
1438{
Darren Tuckera627d422013-06-02 07:31:17 +10001439 free(ctx);
Darren Tucker84c56f52008-06-13 04:55:46 +10001440}
1441
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001442int
1443client_simple_escape_filter(Channel *c, char *buf, int len)
Damien Millerad833b32000-08-23 10:46:23 +10001444{
Damien Miller5771ed72008-05-19 15:35:33 +10001445 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1446 return 0;
1447
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001448 return process_escapes(c, &c->input, &c->output, &c->extended,
1449 buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001450}
1451
Ben Lindstrombba81212001-06-25 05:01:22 +00001452static void
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001453client_channel_closed(int id, void *arg)
1454{
Damien Miller3ec27592001-10-12 11:35:04 +10001455 channel_cancel_cleanup(id);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001456 session_closed = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001457 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001458}
1459
Damien Millerb38eff82000-04-01 11:09:21 +10001460/*
Damien Miller5428f641999-11-25 11:54:57 +11001461 * Implements the interactive session with the server. This is called after
1462 * the user has been authenticated, and a command has been started on the
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001463 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1464 * used as an escape character for terminating or suspending the session.
Damien Miller5428f641999-11-25 11:54:57 +11001465 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466
Damien Miller4af51302000-04-16 11:18:38 +10001467int
Damien Millerad833b32000-08-23 10:46:23 +10001468client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469{
Damien Miller5e953212001-01-30 09:14:00 +11001470 fd_set *readset = NULL, *writeset = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001471 double start_time, total_time;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001472 int r, max_fd = 0, max_fd2 = 0, len, rekeying = 0;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001473 u_int64_t ibytes, obytes;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001474 u_int nalloc = 0;
Damien Miller95def091999-11-25 00:26:21 +11001475 char buf[100];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476
Damien Miller95def091999-11-25 00:26:21 +11001477 debug("Entering interactive session.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001478
Damien Miller95def091999-11-25 00:26:21 +11001479 start_time = get_current_time();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480
Damien Miller95def091999-11-25 00:26:21 +11001481 /* Initialize variables. */
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001482 escape_pending1 = 0;
Damien Miller95def091999-11-25 00:26:21 +11001483 last_was_cr = 1;
1484 exit_status = -1;
1485 stdin_eof = 0;
1486 buffer_high = 64 * 1024;
1487 connection_in = packet_get_connection_in();
1488 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +11001489 max_fd = MAX(connection_in, connection_out);
1490
1491 if (!compat20) {
Ben Lindstrom302ea6f2001-04-16 02:01:25 +00001492 /* enable nonblocking unless tty */
1493 if (!isatty(fileno(stdin)))
1494 set_nonblock(fileno(stdin));
1495 if (!isatty(fileno(stdout)))
1496 set_nonblock(fileno(stdout));
1497 if (!isatty(fileno(stderr)))
1498 set_nonblock(fileno(stderr));
Damien Miller5e953212001-01-30 09:14:00 +11001499 max_fd = MAX(max_fd, fileno(stdin));
1500 max_fd = MAX(max_fd, fileno(stdout));
1501 max_fd = MAX(max_fd, fileno(stderr));
1502 }
Damien Miller95def091999-11-25 00:26:21 +11001503 quit_pending = 0;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001504 escape_char1 = escape_char_arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001505
Damien Miller95def091999-11-25 00:26:21 +11001506 /* Initialize buffers. */
1507 buffer_init(&stdin_buffer);
1508 buffer_init(&stdout_buffer);
1509 buffer_init(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001510
Damien Millerb38eff82000-04-01 11:09:21 +10001511 client_init_dispatch();
1512
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001513 /*
1514 * Set signal handlers, (e.g. to restore non-blocking mode)
1515 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1516 */
Darren Tucker07336da2004-11-05 20:02:16 +11001517 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1518 signal(SIGHUP, signal_handler);
Ben Lindstromf49dbff2002-12-23 02:01:55 +00001519 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1520 signal(SIGINT, signal_handler);
1521 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1522 signal(SIGQUIT, signal_handler);
1523 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1524 signal(SIGTERM, signal_handler);
Darren Tucker5d78de62004-11-05 20:35:44 +11001525 signal(SIGWINCH, window_change_handler);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001526
Damien Miller95def091999-11-25 00:26:21 +11001527 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001528 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001529
Ben Lindstrom5b828322001-02-09 01:34:36 +00001530 if (compat20) {
1531 session_ident = ssh2_chan_id;
Damien Miller6c3eec72011-05-05 14:16:22 +10001532 if (session_ident != -1) {
1533 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1534 channel_register_filter(session_ident,
1535 client_simple_escape_filter, NULL,
1536 client_filter_cleanup,
1537 client_new_escape_filter_ctx(
1538 escape_char_arg));
1539 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001540 channel_register_cleanup(session_ident,
Damien Miller39eda6e2005-11-05 14:52:50 +11001541 client_channel_closed, 0);
Damien Miller6c3eec72011-05-05 14:16:22 +10001542 }
Ben Lindstrom5b828322001-02-09 01:34:36 +00001543 } else {
1544 /* Check if we should immediately send eof on stdin. */
Damien Miller1383bd82000-04-06 12:32:37 +10001545 client_check_initial_eof_on_stdin();
Ben Lindstrom5b828322001-02-09 01:34:36 +00001546 }
Damien Millerad833b32000-08-23 10:46:23 +10001547
Damien Miller95def091999-11-25 00:26:21 +11001548 /* Main loop of the client for the interactive session mode. */
1549 while (!quit_pending) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001550
Damien Miller5428f641999-11-25 11:54:57 +11001551 /* Process buffered packets sent by the server. */
Damien Miller95def091999-11-25 00:26:21 +11001552 client_process_buffered_input_packets();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001554 if (compat20 && session_closed && !channel_still_open())
Damien Miller1383bd82000-04-06 12:32:37 +10001555 break;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001556
markus@openbsd.org091c3022015-01-19 19:52:16 +00001557 rekeying = (active_state->kex != NULL && !active_state->kex->done);
Damien Miller1383bd82000-04-06 12:32:37 +10001558
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001559 if (rekeying) {
1560 debug("rekeying in progress");
1561 } else {
1562 /*
1563 * Make packets of buffered stdin data, and buffer
1564 * them for sending to the server.
1565 */
1566 if (!compat20)
1567 client_make_packets_from_stdin_data();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001568
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001569 /*
1570 * Make packets from buffered channel data, and
1571 * enqueue them for sending to the server.
1572 */
1573 if (packet_not_very_much_data_to_write())
1574 channel_output_poll();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001576 /*
1577 * Check if the window size has changed, and buffer a
1578 * message about it to the server if so.
1579 */
1580 client_check_window_change();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001581
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001582 if (quit_pending)
1583 break;
1584 }
Damien Miller5428f641999-11-25 11:54:57 +11001585 /*
1586 * Wait until we have something to do (something becomes
1587 * available on one of the descriptors).
1588 */
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001589 max_fd2 = max_fd;
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001590 client_wait_until_can_do_something(&readset, &writeset,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001591 &max_fd2, &nalloc, rekeying);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001592
Damien Miller95def091999-11-25 00:26:21 +11001593 if (quit_pending)
1594 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001595
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001596 /* Do channel operations unless rekeying in progress. */
1597 if (!rekeying) {
1598 channel_after_select(readset, writeset);
Damien Millera5539d22003-04-09 20:50:06 +10001599 if (need_rekeying || packet_need_rekeying()) {
1600 debug("need rekeying");
markus@openbsd.org091c3022015-01-19 19:52:16 +00001601 active_state->kex->done = 0;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001602 if ((r = kex_send_kexinit(active_state)) != 0)
1603 fatal("%s: kex_send_kexinit: %s",
1604 __func__, ssh_err(r));
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001605 need_rekeying = 0;
1606 }
1607 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001608
Damien Miller1383bd82000-04-06 12:32:37 +10001609 /* Buffer input from the connection. */
Damien Miller5e953212001-01-30 09:14:00 +11001610 client_process_net_input(readset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001611
Damien Miller1383bd82000-04-06 12:32:37 +10001612 if (quit_pending)
1613 break;
1614
1615 if (!compat20) {
1616 /* Buffer data from stdin */
Damien Miller5e953212001-01-30 09:14:00 +11001617 client_process_input(readset);
Damien Miller1383bd82000-04-06 12:32:37 +10001618 /*
1619 * Process output to stdout and stderr. Output to
1620 * the connection is processed elsewhere (above).
1621 */
Damien Miller5e953212001-01-30 09:14:00 +11001622 client_process_output(writeset);
Damien Miller1383bd82000-04-06 12:32:37 +10001623 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624
Darren Tuckere32cf432010-01-08 16:51:40 +11001625 if (session_resumed) {
1626 connection_in = packet_get_connection_in();
1627 connection_out = packet_get_connection_out();
1628 max_fd = MAX(max_fd, connection_out);
1629 max_fd = MAX(max_fd, connection_in);
1630 session_resumed = 0;
1631 }
1632
Darren Tucker4d5cd332008-06-13 04:51:14 +10001633 /*
1634 * Send as much buffered packet data as possible to the
1635 * sender.
1636 */
Damien Miller5e953212001-01-30 09:14:00 +11001637 if (FD_ISSET(connection_out, writeset))
Damien Miller95def091999-11-25 00:26:21 +11001638 packet_write_poll();
Damien Millere11e1ea2010-08-03 16:04:46 +10001639
1640 /*
1641 * If we are a backgrounded control master, and the
1642 * timeout has expired without any active client
1643 * connections, then quit.
1644 */
1645 if (control_persist_exit_time > 0) {
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001646 if (monotime() >= control_persist_exit_time) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001647 debug("ControlPersist timeout expired");
1648 break;
1649 }
1650 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001651 }
Darren Tuckera627d422013-06-02 07:31:17 +10001652 free(readset);
1653 free(writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001654
Damien Miller95def091999-11-25 00:26:21 +11001655 /* Terminate the session. */
1656
1657 /* Stop watching for window change. */
Darren Tucker5d78de62004-11-05 20:35:44 +11001658 signal(SIGWINCH, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11001659
Darren Tuckerde0c0252009-07-06 07:17:35 +10001660 if (compat20) {
1661 packet_start(SSH2_MSG_DISCONNECT);
1662 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1663 packet_put_cstring("disconnected by user");
Damien Miller8ddc71c2010-03-22 05:54:02 +11001664 packet_put_cstring(""); /* language tag */
Darren Tuckerde0c0252009-07-06 07:17:35 +10001665 packet_send();
1666 packet_write_wait();
1667 }
Darren Tucker12b4a652009-06-21 18:14:48 +10001668
Ben Lindstrom601e4362001-06-21 03:19:23 +00001669 channel_free_all();
Damien Miller95def091999-11-25 00:26:21 +11001670
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001671 if (have_pty)
Damien Miller21771e22011-05-15 08:45:50 +10001672 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001673
1674 /* restore blocking io */
1675 if (!isatty(fileno(stdin)))
1676 unset_nonblock(fileno(stdin));
1677 if (!isatty(fileno(stdout)))
1678 unset_nonblock(fileno(stdout));
1679 if (!isatty(fileno(stderr)))
1680 unset_nonblock(fileno(stderr));
1681
Damien Millerd6965512003-12-17 16:31:53 +11001682 /*
1683 * If there was no shell or command requested, there will be no remote
1684 * exit status to be returned. In that case, clear error code if the
1685 * connection was deliberately terminated at this end.
1686 */
1687 if (no_shell_flag && received_signal == SIGTERM) {
1688 received_signal = 0;
1689 exit_status = 0;
1690 }
1691
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10001692 if (received_signal)
Ben Lindstromf8f065b2001-12-06 17:52:16 +00001693 fatal("Killed by signal %d.", (int) received_signal);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001694
1695 /*
1696 * In interactive mode (with pseudo tty) display a message indicating
1697 * that the connection has been closed.
1698 */
1699 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10001700 snprintf(buf, sizeof buf,
1701 "Connection to %.64s closed.\r\n", host);
Ben Lindstromec46e0b2001-06-09 01:27:31 +00001702 buffer_append(&stderr_buffer, buf, strlen(buf));
1703 }
1704
Damien Miller95def091999-11-25 00:26:21 +11001705 /* Output any buffered data for stdout. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001706 if (buffer_len(&stdout_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001707 len = atomicio(vwrite, fileno(stdout),
1708 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001709 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001710 error("Write failed flushing stdout buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001711 else
1712 buffer_consume(&stdout_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001713 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001714
Damien Miller95def091999-11-25 00:26:21 +11001715 /* Output any buffered data for stderr. */
Damien Millercfd6e4f2011-01-16 23:18:33 +11001716 if (buffer_len(&stderr_buffer) > 0) {
Damien Miller4791f9d2011-01-16 23:16:53 +11001717 len = atomicio(vwrite, fileno(stderr),
1718 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
Damien Millercfd6e4f2011-01-16 23:18:33 +11001719 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
Damien Miller95def091999-11-25 00:26:21 +11001720 error("Write failed flushing stderr buffer.");
Damien Millercfd6e4f2011-01-16 23:18:33 +11001721 else
1722 buffer_consume(&stderr_buffer, len);
Damien Miller95def091999-11-25 00:26:21 +11001723 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001724
Damien Miller95def091999-11-25 00:26:21 +11001725 /* Clear and free any buffers. */
1726 memset(buf, 0, sizeof(buf));
1727 buffer_free(&stdin_buffer);
1728 buffer_free(&stdout_buffer);
1729 buffer_free(&stderr_buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001730
Damien Miller95def091999-11-25 00:26:21 +11001731 /* Report bytes transferred, and transfer rates. */
1732 total_time = get_current_time() - start_time;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001733 packet_get_bytes(&ibytes, &obytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001734 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
Damien Miller821de0a2011-01-11 17:20:29 +11001735 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
Damien Miller95def091999-11-25 00:26:21 +11001736 if (total_time > 0)
Damien Millerb61f3fc2008-07-11 17:36:48 +10001737 verbose("Bytes per second: sent %.1f, received %.1f",
1738 obytes / total_time, ibytes / total_time);
Damien Miller95def091999-11-25 00:26:21 +11001739 /* Return the exit status of the program. */
1740 debug("Exit status %d", exit_status);
1741 return exit_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001742}
Damien Millerb38eff82000-04-01 11:09:21 +10001743
1744/*********/
1745
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001746static int
Damien Miller630d6f42002-01-22 23:17:30 +11001747client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001748{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001749 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001750 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001751 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001752 buffer_append(&stdout_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001753 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001754 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001755 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001756}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001757static int
Damien Miller630d6f42002-01-22 23:17:30 +11001758client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001759{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001760 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001761 char *data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001762 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001763 buffer_append(&stderr_buffer, data, data_len);
Damien Millera5103f42014-02-04 11:20:14 +11001764 explicit_bzero(data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001765 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001766 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001767}
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001768static int
Damien Miller630d6f42002-01-22 23:17:30 +11001769client_input_exit_status(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001770{
Damien Millerb38eff82000-04-01 11:09:21 +10001771 exit_status = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001772 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001773 /* Acknowledge the exit. */
1774 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1775 packet_send();
1776 /*
1777 * Must wait for packet to be sent since we are
1778 * exiting the loop.
1779 */
1780 packet_write_wait();
1781 /* Flag that we want to exit. */
1782 quit_pending = 1;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001783 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001784}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001785
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001786static int
Darren Tucker5dcdd212003-10-02 16:17:00 +10001787client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1788{
1789 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001790 int r, remote_id, sock;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001791
1792 /* Read the remote channel number from the message. */
1793 remote_id = packet_get_int();
1794 packet_check_eom();
1795
1796 /*
1797 * Get a connection to the local authentication agent (this may again
1798 * get forwarded).
1799 */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001800 if ((r = ssh_get_authentication_socket(&sock)) != 0 &&
1801 r != SSH_ERR_AGENT_NOT_PRESENT)
1802 debug("%s: ssh_get_authentication_socket: %s",
1803 __func__, ssh_err(r));
1804
Darren Tucker5dcdd212003-10-02 16:17:00 +10001805
1806 /*
1807 * If we could not connect the agent, send an error message back to
1808 * the server. This should never happen unless the agent dies,
1809 * because authentication forwarding is only enabled if we have an
1810 * agent.
1811 */
1812 if (sock >= 0) {
1813 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1814 -1, 0, 0, 0, "authentication agent connection", 1);
1815 c->remote_id = remote_id;
1816 c->force_drain = 1;
1817 }
1818 if (c == NULL) {
1819 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1820 packet_put_int(remote_id);
1821 } else {
1822 /* Send a confirmation to the remote host. */
1823 debug("Forwarding authentication connection.");
1824 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1825 packet_put_int(remote_id);
1826 packet_put_int(c->self);
1827 }
1828 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001829 return 0;
Darren Tucker5dcdd212003-10-02 16:17:00 +10001830}
Damien Millerb38eff82000-04-01 11:09:21 +10001831
Ben Lindstrombba81212001-06-25 05:01:22 +00001832static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001833client_request_forwarded_tcpip(const char *request_type, int rchan)
1834{
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001835 Channel *c = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001836 char *listen_address, *originator_address;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001837 u_short listen_port, originator_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001838
1839 /* Get rest of the packet */
1840 listen_address = packet_get_string(NULL);
1841 listen_port = packet_get_int();
1842 originator_address = packet_get_string(NULL);
1843 originator_port = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001844 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001845
Damien Miller7acefbb2014-07-18 14:11:24 +10001846 debug("%s: listen %s port %d, originator %s port %d", __func__,
1847 listen_address, listen_port, originator_address, originator_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001848
Damien Miller4b3ed642014-07-02 15:29:40 +10001849 c = channel_connect_by_listen_address(listen_address, listen_port,
Damien Millerbd740252008-05-19 15:37:09 +10001850 "forwarded-tcpip", originator_address);
1851
Darren Tuckera627d422013-06-02 07:31:17 +10001852 free(originator_address);
1853 free(listen_address);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001854 return c;
1855}
1856
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001857static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10001858client_request_forwarded_streamlocal(const char *request_type, int rchan)
1859{
1860 Channel *c = NULL;
1861 char *listen_path;
1862
1863 /* Get the remote path. */
1864 listen_path = packet_get_string(NULL);
1865 /* XXX: Skip reserved field for now. */
1866 if (packet_get_string_ptr(NULL) == NULL)
1867 fatal("%s: packet_get_string_ptr failed", __func__);
1868 packet_check_eom();
1869
1870 debug("%s: %s", __func__, listen_path);
1871
1872 c = channel_connect_by_listen_path(listen_path,
1873 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1874 free(listen_path);
1875 return c;
1876}
1877
1878static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001879client_request_x11(const char *request_type, int rchan)
1880{
1881 Channel *c = NULL;
1882 char *originator;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001883 u_short originator_port;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001884 int sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001885
1886 if (!options.forward_x11) {
1887 error("Warning: ssh server tried X11 forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001888 error("Warning: this is probably a break-in attempt by a "
1889 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001890 return NULL;
1891 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001892 if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) {
Damien Miller1ab6a512010-06-26 10:02:24 +10001893 verbose("Rejected X11 connection after ForwardX11Timeout "
1894 "expired");
1895 return NULL;
1896 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001897 originator = packet_get_string(NULL);
1898 if (datafellows & SSH_BUG_X11FWD) {
1899 debug2("buggy server: x11 request w/o originator_port");
1900 originator_port = 0;
1901 } else {
1902 originator_port = packet_get_int();
1903 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001904 packet_check_eom();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001905 /* XXX check permission */
Damien Millerd83ff352001-01-30 09:19:34 +11001906 debug("client_request_x11: request from %s %d", originator,
1907 originator_port);
Darren Tuckera627d422013-06-02 07:31:17 +10001908 free(originator);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001909 sock = x11_connect_display();
1910 if (sock < 0)
1911 return NULL;
1912 c = channel_new("x11",
1913 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001914 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001915 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001916 return c;
1917}
1918
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001919static Channel *
Damien Miller0bc1bd82000-11-13 22:57:25 +11001920client_request_agent(const char *request_type, int rchan)
1921{
1922 Channel *c = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001923 int r, sock;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001924
1925 if (!options.forward_agent) {
1926 error("Warning: ssh server tried agent forwarding.");
Darren Tucker4d5cd332008-06-13 04:51:14 +10001927 error("Warning: this is probably a break-in attempt by a "
1928 "malicious server.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001929 return NULL;
1930 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001931 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1932 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1933 debug("%s: ssh_get_authentication_socket: %s",
1934 __func__, ssh_err(r));
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001935 return NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001936 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001937 c = channel_new("authentication agent connection",
1938 SSH_CHANNEL_OPEN, sock, sock, -1,
Darren Tucker5baa1702007-12-29 09:37:10 +11001939 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001940 "authentication agent connection", 1);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001941 c->force_drain = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001942 return c;
1943}
1944
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001945int
1946client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1947{
1948 Channel *c;
1949 int fd;
1950
1951 if (tun_mode == SSH_TUNMODE_NO)
1952 return 0;
1953
1954 if (!compat20) {
Damien Millerb3f2c9f2009-01-28 16:15:30 +11001955 error("Tunnel forwarding is not supported for protocol 1");
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001956 return -1;
1957 }
1958
1959 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1960
1961 /* Open local tunnel device */
1962 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1963 error("Tunnel device open failed.");
1964 return -1;
1965 }
1966
1967 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1968 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1969 c->datagram = 1;
1970
1971#if defined(SSH_TUN_FILTER)
1972 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1973 channel_register_filter(c->self, sys_tun_infilter,
Darren Tucker1cf65ae2008-06-13 05:09:18 +10001974 sys_tun_outfilter, NULL, NULL);
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001975#endif
1976
1977 packet_start(SSH2_MSG_CHANNEL_OPEN);
1978 packet_put_cstring("tun@openssh.com");
1979 packet_put_int(c->self);
1980 packet_put_int(c->local_window_max);
1981 packet_put_int(c->local_maxpacket);
1982 packet_put_int(tun_mode);
1983 packet_put_int(remote_tun);
1984 packet_send();
1985
1986 return 0;
1987}
1988
Damien Millerbd483e72000-04-30 10:00:53 +10001989/* XXXX move to generic input handler */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001990static int
Damien Miller630d6f42002-01-22 23:17:30 +11001991client_input_channel_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10001992{
1993 Channel *c = NULL;
1994 char *ctype;
Damien Millerbd483e72000-04-30 10:00:53 +10001995 int rchan;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001996 u_int rmaxpack, rwindow, len;
Damien Millerbd483e72000-04-30 10:00:53 +10001997
1998 ctype = packet_get_string(&len);
1999 rchan = packet_get_int();
2000 rwindow = packet_get_int();
2001 rmaxpack = packet_get_int();
2002
Damien Millere247cc42000-05-07 12:03:14 +10002003 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerbd483e72000-04-30 10:00:53 +10002004 ctype, rchan, rwindow, rmaxpack);
2005
Damien Miller0bc1bd82000-11-13 22:57:25 +11002006 if (strcmp(ctype, "forwarded-tcpip") == 0) {
2007 c = client_request_forwarded_tcpip(ctype, rchan);
Damien Miller7acefbb2014-07-18 14:11:24 +10002008 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
2009 c = client_request_forwarded_streamlocal(ctype, rchan);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002010 } else if (strcmp(ctype, "x11") == 0) {
2011 c = client_request_x11(ctype, rchan);
2012 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
2013 c = client_request_agent(ctype, rchan);
Damien Millerbd483e72000-04-30 10:00:53 +10002014 }
2015/* XXX duplicate : */
2016 if (c != NULL) {
2017 debug("confirm %s", ctype);
2018 c->remote_id = rchan;
2019 c->remote_window = rwindow;
2020 c->remote_maxpacket = rmaxpack;
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002021 if (c->type != SSH_CHANNEL_CONNECTING) {
2022 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
2023 packet_put_int(c->remote_id);
2024 packet_put_int(c->self);
2025 packet_put_int(c->local_window);
2026 packet_put_int(c->local_maxpacket);
2027 packet_send();
2028 }
Damien Millerbd483e72000-04-30 10:00:53 +10002029 } else {
2030 debug("failure %s", ctype);
2031 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2032 packet_put_int(rchan);
2033 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
Ben Lindstromf3436742001-04-29 19:52:00 +00002034 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Ben Lindstroma69d89b2001-05-09 00:01:18 +00002035 packet_put_cstring("open failed");
Ben Lindstromf3436742001-04-29 19:52:00 +00002036 packet_put_cstring("");
2037 }
Damien Millerbd483e72000-04-30 10:00:53 +10002038 packet_send();
2039 }
Darren Tuckera627d422013-06-02 07:31:17 +10002040 free(ctype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002041 return 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002042}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002043
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002044static int
Damien Miller630d6f42002-01-22 23:17:30 +11002045client_input_channel_req(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom5b828322001-02-09 01:34:36 +00002046{
2047 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002048 int exitval, id, reply, success = 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002049 char *rtype;
2050
2051 id = packet_get_int();
2052 rtype = packet_get_string(NULL);
2053 reply = packet_get_char();
2054
2055 debug("client_input_channel_req: channel %d rtype %s reply %d",
2056 id, rtype, reply);
2057
Damien Miller3bbd8782004-06-18 22:23:22 +10002058 if (id == -1) {
2059 error("client_input_channel_req: request for channel -1");
2060 } else if ((c = channel_lookup(id)) == NULL) {
Darren Tucker4d5cd332008-06-13 04:51:14 +10002061 error("client_input_channel_req: channel %d: "
2062 "unknown channel", id);
Damien Millerbab9bd42008-05-19 16:06:47 +10002063 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2064 packet_check_eom();
2065 chan_rcvd_eow(c);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002066 } else if (strcmp(rtype, "exit-status") == 0) {
Damien Miller0e220db2004-06-15 10:34:08 +10002067 exitval = packet_get_int();
Damien Millere1537f92010-01-26 13:26:22 +11002068 if (c->ctl_chan != -1) {
2069 mux_exit_message(c, exitval);
Damien Miller0e220db2004-06-15 10:34:08 +10002070 success = 1;
Darren Tucker29440822010-01-08 17:08:35 +11002071 } else if (id == session_ident) {
2072 /* Record exit value of local session */
2073 success = 1;
2074 exit_status = exitval;
2075 } else {
Damien Miller36f57eb2010-01-30 17:28:34 +11002076 /* Probably for a mux channel that has already closed */
2077 debug("%s: no sink for exit-status on channel %d",
2078 __func__, id);
Damien Miller0e220db2004-06-15 10:34:08 +10002079 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002080 packet_check_eom();
Ben Lindstrom5b828322001-02-09 01:34:36 +00002081 }
Damien Millerc5893782014-05-15 13:48:49 +10002082 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
Ben Lindstrom5b828322001-02-09 01:34:36 +00002083 packet_start(success ?
2084 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
Damien Miller8533c782008-12-08 09:54:40 +11002085 packet_put_int(c->remote_id);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002086 packet_send();
2087 }
Darren Tuckera627d422013-06-02 07:31:17 +10002088 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002089 return 0;
Ben Lindstrom5b828322001-02-09 01:34:36 +00002090}
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002091
djm@openbsd.org523463a2015-02-16 22:13:32 +00002092struct hostkeys_update_ctx {
2093 /* The hostname and (optionally) IP address string for the server */
2094 char *host_str, *ip_str;
2095
2096 /*
2097 * Keys received from the server and a flag for each indicating
2098 * whether they already exist in known_hosts.
2099 * keys_seen is filled in by hostkeys_find() and later (for new
2100 * keys) by client_global_hostkeys_private_confirm().
2101 */
2102 struct sshkey **keys;
2103 int *keys_seen;
2104 size_t nkeys;
2105
2106 size_t nnew;
2107
2108 /*
2109 * Keys that are in known_hosts, but were not present in the update
2110 * from the server (i.e. scheduled to be deleted).
2111 * Filled in by hostkeys_find().
2112 */
2113 struct sshkey **old_keys;
2114 size_t nold;
2115};
2116
2117static void
2118hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
2119{
2120 size_t i;
2121
2122 if (ctx == NULL)
2123 return;
2124 for (i = 0; i < ctx->nkeys; i++)
2125 sshkey_free(ctx->keys[i]);
2126 free(ctx->keys);
2127 free(ctx->keys_seen);
2128 for (i = 0; i < ctx->nold; i++)
2129 sshkey_free(ctx->old_keys[i]);
2130 free(ctx->old_keys);
2131 free(ctx->host_str);
2132 free(ctx->ip_str);
2133 free(ctx);
2134}
2135
2136static int
2137hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
2138{
2139 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2140 size_t i;
2141 struct sshkey **tmp;
2142
2143 if (l->status != HKF_STATUS_MATCHED || l->key == NULL ||
2144 l->key->type == KEY_RSA1)
2145 return 0;
2146
2147 /* Mark off keys we've already seen for this host */
2148 for (i = 0; i < ctx->nkeys; i++) {
2149 if (sshkey_equal(l->key, ctx->keys[i])) {
2150 debug3("%s: found %s key at %s:%ld", __func__,
2151 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
2152 ctx->keys_seen[i] = 1;
2153 return 0;
2154 }
2155 }
2156 /* This line contained a key that not offered by the server */
2157 debug3("%s: deprecated %s key at %s:%ld", __func__,
2158 sshkey_ssh_name(l->key), l->path, l->linenum);
2159 if ((tmp = reallocarray(ctx->old_keys, ctx->nold + 1,
2160 sizeof(*ctx->old_keys))) == NULL)
2161 fatal("%s: reallocarray failed nold = %zu",
2162 __func__, ctx->nold);
2163 ctx->old_keys = tmp;
2164 ctx->old_keys[ctx->nold++] = l->key;
2165 l->key = NULL;
2166
2167 return 0;
2168}
2169
2170static void
2171update_known_hosts(struct hostkeys_update_ctx *ctx)
2172{
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002173 int r, was_raw = 0;
2174 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
djm@openbsd.org523463a2015-02-16 22:13:32 +00002175 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
2176 char *fp, *response;
2177 size_t i;
2178
2179 for (i = 0; i < ctx->nkeys; i++) {
2180 if (ctx->keys_seen[i] != 2)
2181 continue;
2182 if ((fp = sshkey_fingerprint(ctx->keys[i],
2183 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2184 fatal("%s: sshkey_fingerprint failed", __func__);
2185 do_log2(loglevel, "Learned new hostkey: %s %s",
2186 sshkey_type(ctx->keys[i]), fp);
2187 free(fp);
2188 }
2189 for (i = 0; i < ctx->nold; i++) {
2190 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
2191 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2192 fatal("%s: sshkey_fingerprint failed", __func__);
2193 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
2194 sshkey_type(ctx->old_keys[i]), fp);
2195 free(fp);
2196 }
2197 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002198 if (get_saved_tio() != NULL) {
2199 leave_raw_mode(1);
2200 was_raw = 1;
2201 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002202 response = NULL;
2203 for (i = 0; !quit_pending && i < 3; i++) {
2204 free(response);
2205 response = read_passphrase("Accept updated hostkeys? "
2206 "(yes/no): ", RP_ECHO);
2207 if (strcasecmp(response, "yes") == 0)
2208 break;
2209 else if (quit_pending || response == NULL ||
2210 strcasecmp(response, "no") == 0) {
2211 options.update_hostkeys = 0;
2212 break;
2213 } else {
2214 do_log2(loglevel, "Please enter "
2215 "\"yes\" or \"no\"");
2216 }
2217 }
2218 if (quit_pending || i >= 3 || response == NULL)
2219 options.update_hostkeys = 0;
2220 free(response);
djm@openbsd.orgf2293a62015-02-23 16:33:25 +00002221 if (was_raw)
2222 enter_raw_mode(1);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002223 }
2224
2225 /*
2226 * Now that all the keys are verified, we can go ahead and replace
2227 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
2228 * cancel the operation).
2229 */
2230 if (options.update_hostkeys != 0 &&
2231 (r = hostfile_replace_entries(options.user_hostfiles[0],
2232 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
2233 options.hash_known_hosts, 0,
2234 options.fingerprint_hash)) != 0)
2235 error("%s: hostfile_replace_entries failed: %s",
2236 __func__, ssh_err(r));
2237}
2238
2239static void
2240client_global_hostkeys_private_confirm(int type, u_int32_t seq, void *_ctx)
2241{
2242 struct ssh *ssh = active_state; /* XXX */
2243 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2244 size_t i, ndone;
2245 struct sshbuf *signdata;
2246 int r;
2247 const u_char *sig;
2248 size_t siglen;
2249
2250 if (ctx->nnew == 0)
2251 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
2252 if (type != SSH2_MSG_REQUEST_SUCCESS) {
2253 error("Server failed to confirm ownership of "
2254 "private host keys");
2255 hostkeys_update_ctx_free(ctx);
2256 return;
2257 }
2258 if ((signdata = sshbuf_new()) == NULL)
2259 fatal("%s: sshbuf_new failed", __func__);
2260 /* Don't want to accidentally accept an unbound signature */
2261 if (ssh->kex->session_id_len == 0)
2262 fatal("%s: ssh->kex->session_id_len == 0", __func__);
2263 /*
2264 * Expect a signature for each of the ctx->nnew private keys we
2265 * haven't seen before. They will be in the same order as the
2266 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
2267 */
2268 for (ndone = i = 0; i < ctx->nkeys; i++) {
2269 if (ctx->keys_seen[i])
2270 continue;
2271 /* Prepare data to be signed: session ID, unique string, key */
2272 sshbuf_reset(signdata);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002273 if ( (r = sshbuf_put_cstring(signdata,
2274 "hostkeys-prove-00@openssh.com")) != 0 ||
2275 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002276 ssh->kex->session_id_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002277 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
2278 fatal("%s: failed to prepare signature: %s",
2279 __func__, ssh_err(r));
2280 /* Extract and verify signature */
2281 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
2282 error("%s: couldn't parse message: %s",
2283 __func__, ssh_err(r));
2284 goto out;
2285 }
2286 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
2287 sshbuf_ptr(signdata), sshbuf_len(signdata), 0)) != 0) {
2288 error("%s: server gave bad signature for %s key %zu",
2289 __func__, sshkey_type(ctx->keys[i]), i);
2290 goto out;
2291 }
2292 /* Key is good. Mark it as 'seen' */
2293 ctx->keys_seen[i] = 2;
2294 ndone++;
2295 }
2296 if (ndone != ctx->nnew)
2297 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
2298 ndone, ctx->nnew); /* Shouldn't happen */
2299 ssh_packet_check_eom(ssh);
2300
2301 /* Make the edits to known_hosts */
2302 update_known_hosts(ctx);
2303 out:
2304 hostkeys_update_ctx_free(ctx);
2305}
2306
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002307/*
djm@openbsd.org44732de2015-02-20 22:17:21 +00002308 * Handle hostkeys-00@openssh.com global request to inform the client of all
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002309 * the server's hostkeys. The keys are checked against the user's
2310 * HostkeyAlgorithms preference before they are accepted.
2311 */
2312static int
2313client_input_hostkeys(void)
2314{
djm@openbsd.org523463a2015-02-16 22:13:32 +00002315 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002316 const u_char *blob = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002317 size_t i, len = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002318 struct sshbuf *buf = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002319 struct sshkey *key = NULL, **tmp;
2320 int r;
2321 char *fp;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002322 static int hostkeys_seen = 0; /* XXX use struct ssh */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002323 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
djm@openbsd.org523463a2015-02-16 22:13:32 +00002324 struct hostkeys_update_ctx *ctx;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002325
djm@openbsd.org523463a2015-02-16 22:13:32 +00002326 ctx = xcalloc(1, sizeof(*ctx));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002327
2328 if (hostkeys_seen)
2329 fatal("%s: server already sent hostkeys", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002330 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2331 options.batch_mode)
2332 return 1; /* won't ask in batchmode, so don't even try */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002333 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2334 return 1;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002335 while (ssh_packet_remaining(ssh) > 0) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002336 sshkey_free(key);
2337 key = NULL;
djm@openbsd.org523463a2015-02-16 22:13:32 +00002338 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2339 error("%s: couldn't parse message: %s",
2340 __func__, ssh_err(r));
2341 goto out;
2342 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002343 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2344 error("%s: parse key: %s", __func__, ssh_err(r));
2345 goto out;
2346 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002347 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2348 SSH_FP_DEFAULT);
2349 debug3("%s: received %s key %s", __func__,
2350 sshkey_type(key), fp);
2351 free(fp);
2352 /* Check that the key is accepted in HostkeyAlgorithms */
2353 if (options.hostkeyalgorithms != NULL &&
2354 match_pattern_list(sshkey_ssh_name(key),
2355 options.hostkeyalgorithms,
2356 strlen(options.hostkeyalgorithms), 0) != 1) {
2357 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2358 __func__, sshkey_ssh_name(key));
2359 continue;
2360 }
djm@openbsd.org523463a2015-02-16 22:13:32 +00002361 /* Skip certs */
2362 if (sshkey_is_cert(key)) {
2363 debug3("%s: %s key is a certificate; skipping",
2364 __func__, sshkey_ssh_name(key));
2365 continue;
2366 }
2367 /* Ensure keys are unique */
2368 for (i = 0; i < ctx->nkeys; i++) {
2369 if (sshkey_equal(key, ctx->keys[i])) {
2370 error("%s: received duplicated %s host key",
2371 __func__, sshkey_ssh_name(key));
2372 goto out;
2373 }
2374 }
2375 /* Key is good, record it */
2376 if ((tmp = reallocarray(ctx->keys, ctx->nkeys + 1,
2377 sizeof(*ctx->keys))) == NULL)
2378 fatal("%s: reallocarray failed nkeys = %zu",
2379 __func__, ctx->nkeys);
2380 ctx->keys = tmp;
2381 ctx->keys[ctx->nkeys++] = key;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002382 key = NULL;
2383 }
2384
djm@openbsd.org523463a2015-02-16 22:13:32 +00002385 if (ctx->nkeys == 0) {
djm@openbsd.org44732de2015-02-20 22:17:21 +00002386 debug("%s: server sent no hostkeys", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002387 goto out;
2388 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00002389
djm@openbsd.org523463a2015-02-16 22:13:32 +00002390 if ((ctx->keys_seen = calloc(ctx->nkeys,
2391 sizeof(*ctx->keys_seen))) == NULL)
2392 fatal("%s: calloc failed", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002393
djm@openbsd.org6c5c9492015-02-16 22:08:57 +00002394 get_hostfile_hostname_ipaddr(host,
2395 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
djm@openbsd.org523463a2015-02-16 22:13:32 +00002396 options.port, &ctx->host_str,
2397 options.check_host_ip ? &ctx->ip_str : NULL);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002398
djm@openbsd.org523463a2015-02-16 22:13:32 +00002399 /* Find which keys we already know about. */
2400 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2401 ctx, ctx->host_str, ctx->ip_str,
2402 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2403 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002404 goto out;
2405 }
2406
djm@openbsd.org523463a2015-02-16 22:13:32 +00002407 /* Figure out if we have any new keys to add */
2408 ctx->nnew = 0;
2409 for (i = 0; i < ctx->nkeys; i++) {
2410 if (!ctx->keys_seen[i])
2411 ctx->nnew++;
2412 }
2413
2414 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2415 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2416
2417 if (ctx->nnew == 0 && ctx->nold != 0) {
2418 /* We have some keys to remove. Just do it. */
2419 update_known_hosts(ctx);
2420 } else if (ctx->nnew != 0) {
2421 /*
2422 * We have received hitherto-unseen keys from the server.
2423 * Ask the server to confirm ownership of the private halves.
2424 */
2425 debug3("%s: asking server to prove ownership for %zu keys",
2426 __func__, ctx->nnew);
2427 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2428 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org44732de2015-02-20 22:17:21 +00002429 "hostkeys-prove-00@openssh.com")) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +00002430 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2431 fatal("%s: cannot prepare packet: %s",
2432 __func__, ssh_err(r));
2433 if ((buf = sshbuf_new()) == NULL)
2434 fatal("%s: sshbuf_new", __func__);
2435 for (i = 0; i < ctx->nkeys; i++) {
2436 if (ctx->keys_seen[i])
2437 continue;
2438 sshbuf_reset(buf);
2439 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2440 fatal("%s: sshkey_putb: %s",
2441 __func__, ssh_err(r));
2442 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2443 fatal("%s: sshpkt_put_string: %s",
2444 __func__, ssh_err(r));
2445 }
2446 if ((r = sshpkt_send(ssh)) != 0)
2447 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2448 client_register_global_confirm(
2449 client_global_hostkeys_private_confirm, ctx);
2450 ctx = NULL; /* will be freed in callback */
2451 }
2452
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002453 /* Success */
2454 out:
djm@openbsd.org523463a2015-02-16 22:13:32 +00002455 hostkeys_update_ctx_free(ctx);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002456 sshkey_free(key);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002457 sshbuf_free(buf);
djm@openbsd.org523463a2015-02-16 22:13:32 +00002458 /*
2459 * NB. Return success for all cases. The server doesn't need to know
2460 * what the client does with its hosts file.
2461 */
2462 return 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002463}
2464
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002465static int
Damien Millerc3fa4072002-01-22 23:21:58 +11002466client_input_global_request(int type, u_int32_t seq, void *ctxt)
2467{
2468 char *rtype;
2469 int want_reply;
2470 int success = 0;
2471
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002472 rtype = packet_get_cstring(NULL);
Damien Millerc3fa4072002-01-22 23:21:58 +11002473 want_reply = packet_get_char();
Damien Miller509b0102003-12-17 16:33:10 +11002474 debug("client_input_global_request: rtype %s want_reply %d",
2475 rtype, want_reply);
djm@openbsd.org44732de2015-02-20 22:17:21 +00002476 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002477 success = client_input_hostkeys();
Damien Millerc3fa4072002-01-22 23:21:58 +11002478 if (want_reply) {
2479 packet_start(success ?
2480 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2481 packet_send();
2482 packet_write_wait();
2483 }
Darren Tuckera627d422013-06-02 07:31:17 +10002484 free(rtype);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002485 return 0;
Damien Millerc3fa4072002-01-22 23:21:58 +11002486}
Damien Millerbd483e72000-04-30 10:00:53 +10002487
Damien Miller0e220db2004-06-15 10:34:08 +10002488void
Darren Tuckerfc959702004-07-17 16:12:08 +10002489client_session2_setup(int id, int want_tty, int want_subsystem,
Damien Miller5771ed72008-05-19 15:35:33 +10002490 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
Damien Miller0e220db2004-06-15 10:34:08 +10002491{
2492 int len;
Darren Tucker5d78de62004-11-05 20:35:44 +11002493 Channel *c = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +10002494
2495 debug2("%s: id %d", __func__, id);
2496
Darren Tucker5d78de62004-11-05 20:35:44 +11002497 if ((c = channel_lookup(id)) == NULL)
2498 fatal("client_session2_setup: channel %d: unknown channel", id);
2499
Damien Miller0dac6fb2010-11-20 15:19:38 +11002500 packet_set_interactive(want_tty,
2501 options.ip_qos_interactive, options.ip_qos_bulk);
2502
Damien Miller0e220db2004-06-15 10:34:08 +10002503 if (want_tty) {
2504 struct winsize ws;
Damien Miller0e220db2004-06-15 10:34:08 +10002505
2506 /* Store window size in the packet. */
2507 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2508 memset(&ws, 0, sizeof(ws));
2509
Damien Miller5771ed72008-05-19 15:35:33 +10002510 channel_request_start(id, "pty-req", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002511 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
Damien Miller0e220db2004-06-15 10:34:08 +10002512 packet_put_cstring(term != NULL ? term : "");
Damien Miller71a73672006-03-26 14:04:36 +11002513 packet_put_int((u_int)ws.ws_col);
2514 packet_put_int((u_int)ws.ws_row);
2515 packet_put_int((u_int)ws.ws_xpixel);
2516 packet_put_int((u_int)ws.ws_ypixel);
Darren Tuckerdf189fb2008-06-08 12:55:32 +10002517 if (tiop == NULL)
2518 tiop = get_saved_tio();
2519 tty_make_modes(-1, tiop);
Damien Miller0e220db2004-06-15 10:34:08 +10002520 packet_send();
2521 /* XXX wait for reply */
Darren Tucker5d78de62004-11-05 20:35:44 +11002522 c->client_tty = 1;
Damien Miller0e220db2004-06-15 10:34:08 +10002523 }
2524
2525 /* Transfer any environment variables from client to server */
Damien Miller3756dce2004-06-18 01:17:29 +10002526 if (options.num_send_env != 0 && env != NULL) {
Damien Miller0e220db2004-06-15 10:34:08 +10002527 int i, j, matched;
Damien Miller0e220db2004-06-15 10:34:08 +10002528 char *name, *val;
2529
2530 debug("Sending environment.");
Damien Miller3756dce2004-06-18 01:17:29 +10002531 for (i = 0; env[i] != NULL; i++) {
Damien Miller0e220db2004-06-15 10:34:08 +10002532 /* Split */
Damien Miller3756dce2004-06-18 01:17:29 +10002533 name = xstrdup(env[i]);
Damien Miller0e220db2004-06-15 10:34:08 +10002534 if ((val = strchr(name, '=')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002535 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002536 continue;
2537 }
2538 *val++ = '\0';
2539
2540 matched = 0;
2541 for (j = 0; j < options.num_send_env; j++) {
2542 if (match_pattern(name, options.send_env[j])) {
2543 matched = 1;
2544 break;
2545 }
2546 }
2547 if (!matched) {
2548 debug3("Ignored env %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002549 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002550 continue;
2551 }
2552
2553 debug("Sending env %s = %s", name, val);
2554 channel_request_start(id, "env", 0);
2555 packet_put_cstring(name);
2556 packet_put_cstring(val);
2557 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10002558 free(name);
Damien Miller0e220db2004-06-15 10:34:08 +10002559 }
2560 }
2561
2562 len = buffer_len(cmd);
2563 if (len > 0) {
2564 if (len > 900)
2565 len = 900;
2566 if (want_subsystem) {
Damien Miller5771ed72008-05-19 15:35:33 +10002567 debug("Sending subsystem: %.*s",
2568 len, (u_char*)buffer_ptr(cmd));
2569 channel_request_start(id, "subsystem", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002570 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002571 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002572 debug("Sending command: %.*s",
2573 len, (u_char*)buffer_ptr(cmd));
2574 channel_request_start(id, "exec", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002575 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002576 }
2577 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2578 packet_send();
2579 } else {
Damien Miller5771ed72008-05-19 15:35:33 +10002580 channel_request_start(id, "shell", 1);
Damien Miller555f3b82011-05-15 08:48:05 +10002581 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
Damien Miller0e220db2004-06-15 10:34:08 +10002582 packet_send();
2583 }
2584}
2585
Ben Lindstrombba81212001-06-25 05:01:22 +00002586static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002587client_init_dispatch_20(void)
Damien Miller1383bd82000-04-06 12:32:37 +10002588{
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002589 dispatch_init(&dispatch_protocol_error);
Damien Miller2797f7f2002-04-23 21:09:44 +10002590
Damien Miller1383bd82000-04-06 12:32:37 +10002591 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2592 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2593 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2594 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
Damien Millerbd483e72000-04-30 10:00:53 +10002595 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
Damien Miller1383bd82000-04-06 12:32:37 +10002596 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2597 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
Ben Lindstrom5b828322001-02-09 01:34:36 +00002598 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
Damien Miller1383bd82000-04-06 12:32:37 +10002599 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Millerb84886b2008-05-19 15:05:07 +10002600 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2601 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
Damien Millerc3fa4072002-01-22 23:21:58 +11002602 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002603
2604 /* rekeying */
2605 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
Damien Miller2797f7f2002-04-23 21:09:44 +10002606
2607 /* global request reply messages */
2608 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2609 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
Damien Miller1383bd82000-04-06 12:32:37 +10002610}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002611
Ben Lindstrombba81212001-06-25 05:01:22 +00002612static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002613client_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002614{
2615 dispatch_init(NULL);
2616 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2617 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2618 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
Damien Millerb38eff82000-04-01 11:09:21 +10002619 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2620 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2621 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002622 dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2623 dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2624 dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
Damien Miller69b69aa2000-10-28 14:19:58 +11002625
2626 dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
Darren Tucker5dcdd212003-10-02 16:17:00 +10002627 &client_input_agent_open : &deny_input_open);
Damien Miller69b69aa2000-10-28 14:19:58 +11002628 dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2629 &x11_input_open : &deny_input_open);
Damien Millerb38eff82000-04-01 11:09:21 +10002630}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002631
Ben Lindstrombba81212001-06-25 05:01:22 +00002632static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002633client_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002634{
2635 client_init_dispatch_13();
2636 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2637 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2638}
Darren Tucker4d5cd332008-06-13 04:51:14 +10002639
Ben Lindstromdb47f382001-07-04 05:10:27 +00002640static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00002641client_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +10002642{
Damien Miller1383bd82000-04-06 12:32:37 +10002643 if (compat20)
2644 client_init_dispatch_20();
2645 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002646 client_init_dispatch_13();
2647 else
2648 client_init_dispatch_15();
2649}
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002650
Damien Miller6c3eec72011-05-05 14:16:22 +10002651void
2652client_stop_mux(void)
2653{
2654 if (options.control_path != NULL && muxserver_sock != -1)
2655 unlink(options.control_path);
2656 /*
Darren Tucker48bf4b02012-09-07 16:38:53 +10002657 * If we are in persist mode, or don't have a shell, signal that we
2658 * should close when all active channels are closed.
Damien Miller6c3eec72011-05-05 14:16:22 +10002659 */
Darren Tucker48bf4b02012-09-07 16:38:53 +10002660 if (options.control_persist || no_shell_flag) {
Damien Miller6c3eec72011-05-05 14:16:22 +10002661 session_closed = 1;
Damien Miller4ac99c32011-06-20 14:43:31 +10002662 setproctitle("[stopped mux]");
2663 }
Damien Miller6c3eec72011-05-05 14:16:22 +10002664}
2665
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002666/* client specific fatal cleanup */
2667void
Darren Tucker3e33cec2003-10-02 16:12:36 +10002668cleanup_exit(int i)
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002669{
Damien Miller21771e22011-05-15 08:45:50 +10002670 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002671 leave_non_blocking();
Damien Millerb1cbfa22008-05-19 16:00:08 +10002672 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10002673 unlink(options.control_path);
Damien Millera41ccca2010-10-07 22:07:32 +11002674 ssh_kill_proxy_command();
Darren Tucker3e33cec2003-10-02 16:12:36 +10002675 _exit(i);
Darren Tucker9a2c4cd2003-09-22 21:16:05 +10002676}