Mon Apr 13 17:59:21 2015 UTC ()
CID 1293652: Forward NULL deref


(christos)
diff -r1.12 -r1.13 src/crypto/external/bsd/openssh/dist/clientloop.c

cvs diff -r1.12 -r1.13 src/crypto/external/bsd/openssh/dist/clientloop.c (switch to unified diff)

--- src/crypto/external/bsd/openssh/dist/clientloop.c 2015/04/03 23:58:19 1.12
+++ src/crypto/external/bsd/openssh/dist/clientloop.c 2015/04/13 17:59:21 1.13
@@ -1,2596 +1,2597 @@ @@ -1,2596 +1,2597 @@
1/* $NetBSD: clientloop.c,v 1.12 2015/04/03 23:58:19 christos Exp $ */ 1/* $NetBSD: clientloop.c,v 1.13 2015/04/13 17:59:21 christos Exp $ */
2/* $OpenBSD: clientloop.c,v 1.272 2015/02/25 19:54:02 djm Exp $ */ 2/* $OpenBSD: clientloop.c,v 1.272 2015/02/25 19:54:02 djm Exp $ */
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved 6 * All rights reserved
7 * The main loop for the interactive session (client side). 7 * The main loop for the interactive session (client side).
8 * 8 *
9 * As far as I am concerned, the code I have written for this software 9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this 10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is 11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be 12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell". 13 * called by a name other than "ssh" or "Secure Shell".
14 * 14 *
15 * 15 *
16 * Copyright (c) 1999 Theo de Raadt. All rights reserved. 16 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
17 * 17 *
18 * Redistribution and use in source and binary forms, with or without 18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions 19 * modification, are permitted provided that the following conditions
20 * are met: 20 * are met:
21 * 1. Redistributions of source code must retain the above copyright 21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer. 22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright 23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the 24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution. 25 * documentation and/or other materials provided with the distribution.
26 * 26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * 37 *
38 * 38 *
39 * SSH2 support added by Markus Friedl. 39 * SSH2 support added by Markus Friedl.
40 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved. 40 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
41 * 41 *
42 * Redistribution and use in source and binary forms, with or without 42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions 43 * modification, are permitted provided that the following conditions
44 * are met: 44 * are met:
45 * 1. Redistributions of source code must retain the above copyright 45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer. 46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright 47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the 48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution. 49 * documentation and/or other materials provided with the distribution.
50 * 50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */ 61 */
62 62
63#include "includes.h" 63#include "includes.h"
64__RCSID("$NetBSD: clientloop.c,v 1.12 2015/04/03 23:58:19 christos Exp $"); 64__RCSID("$NetBSD: clientloop.c,v 1.13 2015/04/13 17:59:21 christos Exp $");
65 65
66#include <sys/param.h> /* MIN MAX */ 66#include <sys/param.h> /* MIN MAX */
67#include <sys/types.h> 67#include <sys/types.h>
68#include <sys/ioctl.h> 68#include <sys/ioctl.h>
69#include <sys/stat.h> 69#include <sys/stat.h>
70#include <sys/socket.h> 70#include <sys/socket.h>
71#include <sys/time.h> 71#include <sys/time.h>
72#include <sys/queue.h> 72#include <sys/queue.h>
73 73
74#include <ctype.h> 74#include <ctype.h>
75#include <errno.h> 75#include <errno.h>
76#include <paths.h> 76#include <paths.h>
77#include <signal.h> 77#include <signal.h>
78#include <stdio.h> 78#include <stdio.h>
79#include <stdlib.h> 79#include <stdlib.h>
80#include <string.h> 80#include <string.h>
81#include <termios.h> 81#include <termios.h>
82#include <pwd.h> 82#include <pwd.h>
83#include <unistd.h> 83#include <unistd.h>
84#include <limits.h> 84#include <limits.h>
85 85
86#include "xmalloc.h" 86#include "xmalloc.h"
87#include "ssh.h" 87#include "ssh.h"
88#include "ssh1.h" 88#include "ssh1.h"
89#include "ssh2.h" 89#include "ssh2.h"
90#include "packet.h" 90#include "packet.h"
91#include "buffer.h" 91#include "buffer.h"
92#include "compat.h" 92#include "compat.h"
93#include "channels.h" 93#include "channels.h"
94#include "dispatch.h" 94#include "dispatch.h"
95#include "key.h" 95#include "key.h"
96#include "cipher.h" 96#include "cipher.h"
97#include "kex.h" 97#include "kex.h"
98#include "log.h" 98#include "log.h"
99#include "misc.h" 99#include "misc.h"
100#include "readconf.h" 100#include "readconf.h"
101#include "clientloop.h" 101#include "clientloop.h"
102#include "sshconnect.h" 102#include "sshconnect.h"
103#include "authfd.h" 103#include "authfd.h"
104#include "atomicio.h" 104#include "atomicio.h"
105#include "sshpty.h" 105#include "sshpty.h"
106#include "match.h" 106#include "match.h"
107#include "msg.h" 107#include "msg.h"
108#include "roaming.h" 108#include "roaming.h"
109#include "getpeereid.h" 109#include "getpeereid.h"
110#include "ssherr.h" 110#include "ssherr.h"
111#include "hostfile.h" 111#include "hostfile.h"
112 112
113/* import options */ 113/* import options */
114extern Options options; 114extern Options options;
115 115
116/* Flag indicating that stdin should be redirected from /dev/null. */ 116/* Flag indicating that stdin should be redirected from /dev/null. */
117extern int stdin_null_flag; 117extern int stdin_null_flag;
118 118
119/* Flag indicating that no shell has been requested */ 119/* Flag indicating that no shell has been requested */
120extern int no_shell_flag; 120extern int no_shell_flag;
121 121
122/* Control socket */ 122/* Control socket */
123extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */ 123extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
124 124
125/* 125/*
126 * Name of the host we are connecting to. This is the name given on the 126 * Name of the host we are connecting to. This is the name given on the
127 * command line, or the HostName specified for the user-supplied name in a 127 * command line, or the HostName specified for the user-supplied name in a
128 * configuration file. 128 * configuration file.
129 */ 129 */
130extern char *host; 130extern char *host;
131 131
132/* 132/*
133 * Flag to indicate that we have received a window change signal which has 133 * Flag to indicate that we have received a window change signal which has
134 * not yet been processed. This will cause a message indicating the new 134 * not yet been processed. This will cause a message indicating the new
135 * window size to be sent to the server a little later. This is volatile 135 * window size to be sent to the server a little later. This is volatile
136 * because this is updated in a signal handler. 136 * because this is updated in a signal handler.
137 */ 137 */
138static volatile sig_atomic_t received_window_change_signal = 0; 138static volatile sig_atomic_t received_window_change_signal = 0;
139static volatile sig_atomic_t received_signal = 0; 139static volatile sig_atomic_t received_signal = 0;
140 140
141/* Flag indicating whether the user's terminal is in non-blocking mode. */ 141/* Flag indicating whether the user's terminal is in non-blocking mode. */
142static int in_non_blocking_mode = 0; 142static int in_non_blocking_mode = 0;
143 143
144/* Time when backgrounded control master using ControlPersist should exit */ 144/* Time when backgrounded control master using ControlPersist should exit */
145static time_t control_persist_exit_time = 0; 145static time_t control_persist_exit_time = 0;
146 146
147/* Common data for the client loop code. */ 147/* Common data for the client loop code. */
148volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */ 148volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
149static int escape_char1; /* Escape character. (proto1 only) */ 149static int escape_char1; /* Escape character. (proto1 only) */
150static int escape_pending1; /* Last character was an escape (proto1 only) */ 150static int escape_pending1; /* Last character was an escape (proto1 only) */
151static int last_was_cr; /* Last character was a newline. */ 151static int last_was_cr; /* Last character was a newline. */
152static int exit_status; /* Used to store the command exit status. */ 152static int exit_status; /* Used to store the command exit status. */
153static int stdin_eof; /* EOF has been encountered on stderr. */ 153static int stdin_eof; /* EOF has been encountered on stderr. */
154static Buffer stdin_buffer; /* Buffer for stdin data. */ 154static Buffer stdin_buffer; /* Buffer for stdin data. */
155static Buffer stdout_buffer; /* Buffer for stdout data. */ 155static Buffer stdout_buffer; /* Buffer for stdout data. */
156static Buffer stderr_buffer; /* Buffer for stderr data. */ 156static Buffer stderr_buffer; /* Buffer for stderr data. */
157static u_int buffer_high; /* Soft max buffer size. */ 157static u_int buffer_high; /* Soft max buffer size. */
158static int connection_in; /* Connection to server (input). */ 158static int connection_in; /* Connection to server (input). */
159static int connection_out; /* Connection to server (output). */ 159static int connection_out; /* Connection to server (output). */
160static int need_rekeying; /* Set to non-zero if rekeying is requested. */ 160static int need_rekeying; /* Set to non-zero if rekeying is requested. */
161static int session_closed; /* In SSH2: login session closed. */ 161static int session_closed; /* In SSH2: login session closed. */
162static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */ 162static int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
163 163
164static void client_init_dispatch(void); 164static void client_init_dispatch(void);
165int session_ident = -1; 165int session_ident = -1;
166 166
167int session_resumed = 0; 167int session_resumed = 0;
168 168
169/* Track escape per proto2 channel */ 169/* Track escape per proto2 channel */
170struct escape_filter_ctx { 170struct escape_filter_ctx {
171 int escape_pending; 171 int escape_pending;
172 int escape_char; 172 int escape_char;
173}; 173};
174 174
175/* Context for channel confirmation replies */ 175/* Context for channel confirmation replies */
176struct channel_reply_ctx { 176struct channel_reply_ctx {
177 const char *request_type; 177 const char *request_type;
178 int id; 178 int id;
179 enum confirm_action action; 179 enum confirm_action action;
180}; 180};
181 181
182/* Global request success/failure callbacks */ 182/* Global request success/failure callbacks */
183struct global_confirm { 183struct global_confirm {
184 TAILQ_ENTRY(global_confirm) entry; 184 TAILQ_ENTRY(global_confirm) entry;
185 global_confirm_cb *cb; 185 global_confirm_cb *cb;
186 void *ctx; 186 void *ctx;
187 int ref_count; 187 int ref_count;
188}; 188};
189TAILQ_HEAD(global_confirms, global_confirm); 189TAILQ_HEAD(global_confirms, global_confirm);
190static struct global_confirms global_confirms = 190static struct global_confirms global_confirms =
191 TAILQ_HEAD_INITIALIZER(global_confirms); 191 TAILQ_HEAD_INITIALIZER(global_confirms);
192 192
193void ssh_process_session2_setup(int, int, int, Buffer *); 193void ssh_process_session2_setup(int, int, int, Buffer *);
194 194
195/* Restores stdin to blocking mode. */ 195/* Restores stdin to blocking mode. */
196 196
197static void 197static void
198leave_non_blocking(void) 198leave_non_blocking(void)
199{ 199{
200 if (in_non_blocking_mode) { 200 if (in_non_blocking_mode) {
201 unset_nonblock(fileno(stdin)); 201 unset_nonblock(fileno(stdin));
202 in_non_blocking_mode = 0; 202 in_non_blocking_mode = 0;
203 } 203 }
204} 204}
205 205
206/* Puts stdin terminal in non-blocking mode. */ 206/* Puts stdin terminal in non-blocking mode. */
207 207
208static void 208static void
209enter_non_blocking(void) 209enter_non_blocking(void)
210{ 210{
211 in_non_blocking_mode = 1; 211 in_non_blocking_mode = 1;
212 set_nonblock(fileno(stdin)); 212 set_nonblock(fileno(stdin));
213} 213}
214 214
215/* 215/*
216 * Signal handler for the window change signal (SIGWINCH). This just sets a 216 * Signal handler for the window change signal (SIGWINCH). This just sets a
217 * flag indicating that the window has changed. 217 * flag indicating that the window has changed.
218 */ 218 */
219/*ARGSUSED */ 219/*ARGSUSED */
220static void 220static void
221window_change_handler(int sig) 221window_change_handler(int sig)
222{ 222{
223 received_window_change_signal = 1; 223 received_window_change_signal = 1;
224 signal(SIGWINCH, window_change_handler); 224 signal(SIGWINCH, window_change_handler);
225} 225}
226 226
227/* 227/*
228 * Signal handler for signals that cause the program to terminate. These 228 * Signal handler for signals that cause the program to terminate. These
229 * signals must be trapped to restore terminal modes. 229 * signals must be trapped to restore terminal modes.
230 */ 230 */
231/*ARGSUSED */ 231/*ARGSUSED */
232static void 232static void
233signal_handler(int sig) 233signal_handler(int sig)
234{ 234{
235 received_signal = sig; 235 received_signal = sig;
236 quit_pending = 1; 236 quit_pending = 1;
237} 237}
238 238
239/* 239/*
240 * Returns current time in seconds from Jan 1, 1970 with the maximum 240 * Returns current time in seconds from Jan 1, 1970 with the maximum
241 * available resolution. 241 * available resolution.
242 */ 242 */
243 243
244static double 244static double
245get_current_time(void) 245get_current_time(void)
246{ 246{
247 struct timeval tv; 247 struct timeval tv;
248 gettimeofday(&tv, NULL); 248 gettimeofday(&tv, NULL);
249 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0; 249 return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
250} 250}
251 251
252/* 252/*
253 * Sets control_persist_exit_time to the absolute time when the 253 * Sets control_persist_exit_time to the absolute time when the
254 * backgrounded control master should exit due to expiry of the 254 * backgrounded control master should exit due to expiry of the
255 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded 255 * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
256 * control master process, or if there is no ControlPersist timeout. 256 * control master process, or if there is no ControlPersist timeout.
257 */ 257 */
258static void 258static void
259set_control_persist_exit_time(void) 259set_control_persist_exit_time(void)
260{ 260{
261 if (muxserver_sock == -1 || !options.control_persist 261 if (muxserver_sock == -1 || !options.control_persist
262 || options.control_persist_timeout == 0) { 262 || options.control_persist_timeout == 0) {
263 /* not using a ControlPersist timeout */ 263 /* not using a ControlPersist timeout */
264 control_persist_exit_time = 0; 264 control_persist_exit_time = 0;
265 } else if (channel_still_open()) { 265 } else if (channel_still_open()) {
266 /* some client connections are still open */ 266 /* some client connections are still open */
267 if (control_persist_exit_time > 0) 267 if (control_persist_exit_time > 0)
268 debug2("%s: cancel scheduled exit", __func__); 268 debug2("%s: cancel scheduled exit", __func__);
269 control_persist_exit_time = 0; 269 control_persist_exit_time = 0;
270 } else if (control_persist_exit_time <= 0) { 270 } else if (control_persist_exit_time <= 0) {
271 /* a client connection has recently closed */ 271 /* a client connection has recently closed */
272 control_persist_exit_time = monotime() + 272 control_persist_exit_time = monotime() +
273 (time_t)options.control_persist_timeout; 273 (time_t)options.control_persist_timeout;
274 debug2("%s: schedule exit in %d seconds", __func__, 274 debug2("%s: schedule exit in %d seconds", __func__,
275 options.control_persist_timeout); 275 options.control_persist_timeout);
276 } 276 }
277 /* else we are already counting down to the timeout */ 277 /* else we are already counting down to the timeout */
278} 278}
279 279
280#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_" 280#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
281static int 281static int
282client_x11_display_valid(const char *display) 282client_x11_display_valid(const char *display)
283{ 283{
284 size_t i, dlen; 284 size_t i, dlen;
285 285
286 dlen = strlen(display); 286 dlen = strlen(display);
287 for (i = 0; i < dlen; i++) { 287 for (i = 0; i < dlen; i++) {
288 if (!isalnum((u_char)display[i]) && 288 if (!isalnum((u_char)display[i]) &&
289 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) { 289 strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
290 debug("Invalid character '%c' in DISPLAY", display[i]); 290 debug("Invalid character '%c' in DISPLAY", display[i]);
291 return 0; 291 return 0;
292 } 292 }
293 } 293 }
294 return 1; 294 return 1;
295} 295}
296 296
297#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1" 297#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
298void 298void
299client_x11_get_proto(const char *display, const char *xauth_path, 299client_x11_get_proto(const char *display, const char *xauth_path,
300 u_int trusted, u_int timeout, char **_proto, char **_data) 300 u_int trusted, u_int timeout, char **_proto, char **_data)
301{ 301{
302 char cmd[1024]; 302 char cmd[1024];
303 char line[512]; 303 char line[512];
304 char xdisplay[512]; 304 char xdisplay[512];
305 static char proto[512], data[512]; 305 static char proto[512], data[512];
306 FILE *f; 306 FILE *f;
307 int got_data = 0, generated = 0, do_unlink = 0, i; 307 int got_data = 0, generated = 0, do_unlink = 0, i;
308 char *xauthdir, *xauthfile; 308 char *xauthdir, *xauthfile;
309 struct stat st; 309 struct stat st;
310 u_int now; 310 u_int now;
311 311
312 xauthdir = xauthfile = NULL; 312 xauthdir = xauthfile = NULL;
313 *_proto = proto; 313 *_proto = proto;
314 *_data = data; 314 *_data = data;
315 proto[0] = data[0] = '\0'; 315 proto[0] = data[0] = '\0';
316 316
317 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) { 317 if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
318 debug("No xauth program."); 318 debug("No xauth program.");
319 } else if (!client_x11_display_valid(display)) { 319 } else if (!client_x11_display_valid(display)) {
320 logit("DISPLAY '%s' invalid, falling back to fake xauth data", 320 logit("DISPLAY '%s' invalid, falling back to fake xauth data",
321 display); 321 display);
322 } else { 322 } else {
323 if (display == NULL) { 323 if (display == NULL) {
324 debug("x11_get_proto: DISPLAY not set"); 324 debug("x11_get_proto: DISPLAY not set");
325 return; 325 return;
326 } 326 }
327 /* 327 /*
328 * Handle FamilyLocal case where $DISPLAY does 328 * Handle FamilyLocal case where $DISPLAY does
329 * not match an authorization entry. For this we 329 * not match an authorization entry. For this we
330 * just try "xauth list unix:displaynum.screennum". 330 * just try "xauth list unix:displaynum.screennum".
331 * XXX: "localhost" match to determine FamilyLocal 331 * XXX: "localhost" match to determine FamilyLocal
332 * is not perfect. 332 * is not perfect.
333 */ 333 */
334 if (strncmp(display, "localhost:", 10) == 0) { 334 if (strncmp(display, "localhost:", 10) == 0) {
335 snprintf(xdisplay, sizeof(xdisplay), "unix:%s", 335 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
336 display + 10); 336 display + 10);
337 display = xdisplay; 337 display = xdisplay;
338 } 338 }
339 if (trusted == 0) { 339 if (trusted == 0) {
340 xauthdir = xmalloc(PATH_MAX); 340 xauthdir = xmalloc(PATH_MAX);
341 xauthfile = xmalloc(PATH_MAX); 341 xauthfile = xmalloc(PATH_MAX);
342 mktemp_proto(xauthdir, PATH_MAX); 342 mktemp_proto(xauthdir, PATH_MAX);
343 if (mkdtemp(xauthdir) != NULL) { 343 if (mkdtemp(xauthdir) != NULL) {
344 do_unlink = 1; 344 do_unlink = 1;
345 snprintf(xauthfile, PATH_MAX, "%s/xauthfile", 345 snprintf(xauthfile, PATH_MAX, "%s/xauthfile",
346 xauthdir); 346 xauthdir);
347 snprintf(cmd, sizeof(cmd), 347 snprintf(cmd, sizeof(cmd),
348 "%s -f %s generate %s " SSH_X11_PROTO 348 "%s -f %s generate %s " SSH_X11_PROTO
349 " untrusted timeout %u 2>" _PATH_DEVNULL, 349 " untrusted timeout %u 2>" _PATH_DEVNULL,
350 xauth_path, xauthfile, display, timeout); 350 xauth_path, xauthfile, display, timeout);
351 debug2("x11_get_proto: %s", cmd); 351 debug2("x11_get_proto: %s", cmd);
352 if (system(cmd) == 0) 352 if (system(cmd) == 0)
353 generated = 1; 353 generated = 1;
354 if (x11_refuse_time == 0) { 354 if (x11_refuse_time == 0) {
355 now = monotime() + 1; 355 now = monotime() + 1;
356 if (UINT_MAX - timeout < now) 356 if (UINT_MAX - timeout < now)
357 x11_refuse_time = UINT_MAX; 357 x11_refuse_time = UINT_MAX;
358 else 358 else
359 x11_refuse_time = now + timeout; 359 x11_refuse_time = now + timeout;
360 } 360 }
361 } 361 }
362 } 362 }
363 363
364 /* 364 /*
365 * When in untrusted mode, we read the cookie only if it was 365 * When in untrusted mode, we read the cookie only if it was
366 * successfully generated as an untrusted one in the step 366 * successfully generated as an untrusted one in the step
367 * above. 367 * above.
368 */ 368 */
369 if (trusted || generated) { 369 if (trusted || generated) {
370 snprintf(cmd, sizeof(cmd), 370 snprintf(cmd, sizeof(cmd),
371 "%s %s%s list %s 2>" _PATH_DEVNULL, 371 "%s %s%s list %s 2>" _PATH_DEVNULL,
372 xauth_path, 372 xauth_path,
373 generated ? "-f " : "" , 373 generated ? "-f " : "" ,
374 generated ? xauthfile : "", 374 generated ? xauthfile : "",
375 display); 375 display);
376 debug2("x11_get_proto: %s", cmd); 376 debug2("x11_get_proto: %s", cmd);
377 f = popen(cmd, "r"); 377 f = popen(cmd, "r");
378 if (f && fgets(line, sizeof(line), f) && 378 if (f && fgets(line, sizeof(line), f) &&
379 sscanf(line, "%*s %511s %511s", proto, data) == 2) 379 sscanf(line, "%*s %511s %511s", proto, data) == 2)
380 got_data = 1; 380 got_data = 1;
381 if (f) 381 if (f)
382 pclose(f); 382 pclose(f);
383 } else 383 } else
384 error("Warning: untrusted X11 forwarding setup failed: " 384 error("Warning: untrusted X11 forwarding setup failed: "
385 "xauth key data not generated"); 385 "xauth key data not generated");
386 } 386 }
387 387
388 if (do_unlink) { 388 if (do_unlink) {
389 unlink(xauthfile); 389 unlink(xauthfile);
390 rmdir(xauthdir); 390 rmdir(xauthdir);
391 } 391 }
392 free(xauthdir); 392 free(xauthdir);
393 free(xauthfile); 393 free(xauthfile);
394 394
395 /* 395 /*
396 * If we didn't get authentication data, just make up some 396 * If we didn't get authentication data, just make up some
397 * data. The forwarding code will check the validity of the 397 * data. The forwarding code will check the validity of the
398 * response anyway, and substitute this data. The X11 398 * response anyway, and substitute this data. The X11
399 * server, however, will ignore this fake data and use 399 * server, however, will ignore this fake data and use
400 * whatever authentication mechanisms it was using otherwise 400 * whatever authentication mechanisms it was using otherwise
401 * for the local connection. 401 * for the local connection.
402 */ 402 */
403 if (!got_data) { 403 if (!got_data) {
404 u_int32_t rnd = 0; 404 u_int32_t rnd = 0;
405 405
406 logit("Warning: No xauth data; " 406 logit("Warning: No xauth data; "
407 "using fake authentication data for X11 forwarding."); 407 "using fake authentication data for X11 forwarding.");
408 strlcpy(proto, SSH_X11_PROTO, sizeof proto); 408 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
409 for (i = 0; i < 16; i++) { 409 for (i = 0; i < 16; i++) {
410 if (i % 4 == 0) 410 if (i % 4 == 0)
411 rnd = arc4random(); 411 rnd = arc4random();
412 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", 412 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
413 rnd & 0xff); 413 rnd & 0xff);
414 rnd >>= 8; 414 rnd >>= 8;
415 } 415 }
416 } 416 }
417} 417}
418 418
419/* 419/*
420 * This is called when the interactive is entered. This checks if there is 420 * This is called when the interactive is entered. This checks if there is
421 * an EOF coming on stdin. We must check this explicitly, as select() does 421 * an EOF coming on stdin. We must check this explicitly, as select() does
422 * not appear to wake up when redirecting from /dev/null. 422 * not appear to wake up when redirecting from /dev/null.
423 */ 423 */
424 424
425static void 425static void
426client_check_initial_eof_on_stdin(void) 426client_check_initial_eof_on_stdin(void)
427{ 427{
428 int len; 428 int len;
429 char buf[1]; 429 char buf[1];
430 430
431 /* 431 /*
432 * If standard input is to be "redirected from /dev/null", we simply 432 * If standard input is to be "redirected from /dev/null", we simply
433 * mark that we have seen an EOF and send an EOF message to the 433 * mark that we have seen an EOF and send an EOF message to the
434 * server. Otherwise, we try to read a single character; it appears 434 * server. Otherwise, we try to read a single character; it appears
435 * that for some files, such /dev/null, select() never wakes up for 435 * that for some files, such /dev/null, select() never wakes up for
436 * read for this descriptor, which means that we never get EOF. This 436 * read for this descriptor, which means that we never get EOF. This
437 * way we will get the EOF if stdin comes from /dev/null or similar. 437 * way we will get the EOF if stdin comes from /dev/null or similar.
438 */ 438 */
439 if (stdin_null_flag) { 439 if (stdin_null_flag) {
440 /* Fake EOF on stdin. */ 440 /* Fake EOF on stdin. */
441 debug("Sending eof."); 441 debug("Sending eof.");
442 stdin_eof = 1; 442 stdin_eof = 1;
443 packet_start(SSH_CMSG_EOF); 443 packet_start(SSH_CMSG_EOF);
444 packet_send(); 444 packet_send();
445 } else { 445 } else {
446 enter_non_blocking(); 446 enter_non_blocking();
447 447
448 /* Check for immediate EOF on stdin. */ 448 /* Check for immediate EOF on stdin. */
449 len = read(fileno(stdin), buf, 1); 449 len = read(fileno(stdin), buf, 1);
450 if (len == 0) { 450 if (len == 0) {
451 /* 451 /*
452 * EOF. Record that we have seen it and send 452 * EOF. Record that we have seen it and send
453 * EOF to server. 453 * EOF to server.
454 */ 454 */
455 debug("Sending eof."); 455 debug("Sending eof.");
456 stdin_eof = 1; 456 stdin_eof = 1;
457 packet_start(SSH_CMSG_EOF); 457 packet_start(SSH_CMSG_EOF);
458 packet_send(); 458 packet_send();
459 } else if (len > 0) { 459 } else if (len > 0) {
460 /* 460 /*
461 * Got data. We must store the data in the buffer, 461 * Got data. We must store the data in the buffer,
462 * and also process it as an escape character if 462 * and also process it as an escape character if
463 * appropriate. 463 * appropriate.
464 */ 464 */
465 if ((u_char) buf[0] == escape_char1) 465 if ((u_char) buf[0] == escape_char1)
466 escape_pending1 = 1; 466 escape_pending1 = 1;
467 else 467 else
468 buffer_append(&stdin_buffer, buf, 1); 468 buffer_append(&stdin_buffer, buf, 1);
469 } 469 }
470 leave_non_blocking(); 470 leave_non_blocking();
471 } 471 }
472} 472}
473 473
474 474
475/* 475/*
476 * Make packets from buffered stdin data, and buffer them for sending to the 476 * Make packets from buffered stdin data, and buffer them for sending to the
477 * connection. 477 * connection.
478 */ 478 */
479 479
480static void 480static void
481client_make_packets_from_stdin_data(void) 481client_make_packets_from_stdin_data(void)
482{ 482{
483 u_int len; 483 u_int len;
484 484
485 /* Send buffered stdin data to the server. */ 485 /* Send buffered stdin data to the server. */
486 while (buffer_len(&stdin_buffer) > 0 && 486 while (buffer_len(&stdin_buffer) > 0 &&
487 packet_not_very_much_data_to_write()) { 487 packet_not_very_much_data_to_write()) {
488 len = buffer_len(&stdin_buffer); 488 len = buffer_len(&stdin_buffer);
489 /* Keep the packets at reasonable size. */ 489 /* Keep the packets at reasonable size. */
490 if (len > packet_get_maxsize()) 490 if (len > packet_get_maxsize())
491 len = packet_get_maxsize(); 491 len = packet_get_maxsize();
492 packet_start(SSH_CMSG_STDIN_DATA); 492 packet_start(SSH_CMSG_STDIN_DATA);
493 packet_put_string(buffer_ptr(&stdin_buffer), len); 493 packet_put_string(buffer_ptr(&stdin_buffer), len);
494 packet_send(); 494 packet_send();
495 buffer_consume(&stdin_buffer, len); 495 buffer_consume(&stdin_buffer, len);
496 /* If we have a pending EOF, send it now. */ 496 /* If we have a pending EOF, send it now. */
497 if (stdin_eof && buffer_len(&stdin_buffer) == 0) { 497 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
498 packet_start(SSH_CMSG_EOF); 498 packet_start(SSH_CMSG_EOF);
499 packet_send(); 499 packet_send();
500 } 500 }
501 } 501 }
502} 502}
503 503
504/* 504/*
505 * Checks if the client window has changed, and sends a packet about it to 505 * Checks if the client window has changed, and sends a packet about it to
506 * the server if so. The actual change is detected elsewhere (by a software 506 * the server if so. The actual change is detected elsewhere (by a software
507 * interrupt on Unix); this just checks the flag and sends a message if 507 * interrupt on Unix); this just checks the flag and sends a message if
508 * appropriate. 508 * appropriate.
509 */ 509 */
510 510
511static void 511static void
512client_check_window_change(void) 512client_check_window_change(void)
513{ 513{
514 struct winsize ws; 514 struct winsize ws;
515 515
516 if (! received_window_change_signal) 516 if (! received_window_change_signal)
517 return; 517 return;
518 /** XXX race */ 518 /** XXX race */
519 received_window_change_signal = 0; 519 received_window_change_signal = 0;
520 520
521 debug2("client_check_window_change: changed"); 521 debug2("client_check_window_change: changed");
522 522
523 if (compat20) { 523 if (compat20) {
524 channel_send_window_changes(); 524 channel_send_window_changes();
525 } else { 525 } else {
526 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) 526 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
527 return; 527 return;
528 packet_start(SSH_CMSG_WINDOW_SIZE); 528 packet_start(SSH_CMSG_WINDOW_SIZE);
529 packet_put_int((u_int)ws.ws_row); 529 packet_put_int((u_int)ws.ws_row);
530 packet_put_int((u_int)ws.ws_col); 530 packet_put_int((u_int)ws.ws_col);
531 packet_put_int((u_int)ws.ws_xpixel); 531 packet_put_int((u_int)ws.ws_xpixel);
532 packet_put_int((u_int)ws.ws_ypixel); 532 packet_put_int((u_int)ws.ws_ypixel);
533 packet_send(); 533 packet_send();
534 } 534 }
535} 535}
536 536
537static int 537static int
538client_global_request_reply(int type, u_int32_t seq, void *ctxt) 538client_global_request_reply(int type, u_int32_t seq, void *ctxt)
539{ 539{
540 struct global_confirm *gc; 540 struct global_confirm *gc;
541 541
542 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL) 542 if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
543 return 0; 543 return 0;
544 if (gc->cb != NULL) 544 if (gc->cb != NULL)
545 gc->cb(type, seq, gc->ctx); 545 gc->cb(type, seq, gc->ctx);
546 if (--gc->ref_count <= 0) { 546 if (--gc->ref_count <= 0) {
547 TAILQ_REMOVE(&global_confirms, gc, entry); 547 TAILQ_REMOVE(&global_confirms, gc, entry);
548 explicit_bzero(gc, sizeof(*gc)); 548 explicit_bzero(gc, sizeof(*gc));
549 free(gc); 549 free(gc);
550 } 550 }
551 551
552 packet_set_alive_timeouts(0); 552 packet_set_alive_timeouts(0);
553 return 0; 553 return 0;
554} 554}
555 555
556static void 556static void
557server_alive_check(void) 557server_alive_check(void)
558{ 558{
559 if (packet_inc_alive_timeouts() > options.server_alive_count_max) { 559 if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
560 logit("Timeout, server %s not responding.", host); 560 logit("Timeout, server %s not responding.", host);
561 cleanup_exit(255); 561 cleanup_exit(255);
562 } 562 }
563 packet_start(SSH2_MSG_GLOBAL_REQUEST); 563 packet_start(SSH2_MSG_GLOBAL_REQUEST);
564 packet_put_cstring("keepalive@openssh.com"); 564 packet_put_cstring("keepalive@openssh.com");
565 packet_put_char(1); /* boolean: want reply */ 565 packet_put_char(1); /* boolean: want reply */
566 packet_send(); 566 packet_send();
567 /* Insert an empty placeholder to maintain ordering */ 567 /* Insert an empty placeholder to maintain ordering */
568 client_register_global_confirm(NULL, NULL); 568 client_register_global_confirm(NULL, NULL);
569} 569}
570 570
571/* 571/*
572 * Waits until the client can do something (some data becomes available on 572 * Waits until the client can do something (some data becomes available on
573 * one of the file descriptors). 573 * one of the file descriptors).
574 */ 574 */
575static void 575static void
576client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, 576client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
577 int *maxfdp, u_int *nallocp, int rekeying) 577 int *maxfdp, u_int *nallocp, int rekeying)
578{ 578{
579 struct timeval tv, *tvp; 579 struct timeval tv, *tvp;
580 int timeout_secs; 580 int timeout_secs;
581 time_t minwait_secs = 0, server_alive_time = 0, now = monotime(); 581 time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
582 int ret; 582 int ret;
583 583
584 /* Add any selections by the channel mechanism. */ 584 /* Add any selections by the channel mechanism. */
585 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, 585 channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
586 &minwait_secs, rekeying); 586 &minwait_secs, rekeying);
587 587
588 if (!compat20) { 588 if (!compat20) {
589 /* Read from the connection, unless our buffers are full. */ 589 /* Read from the connection, unless our buffers are full. */
590 if (buffer_len(&stdout_buffer) < buffer_high && 590 if (buffer_len(&stdout_buffer) < buffer_high &&
591 buffer_len(&stderr_buffer) < buffer_high && 591 buffer_len(&stderr_buffer) < buffer_high &&
592 channel_not_very_much_buffered_data()) 592 channel_not_very_much_buffered_data())
593 FD_SET(connection_in, *readsetp); 593 FD_SET(connection_in, *readsetp);
594 /* 594 /*
595 * Read from stdin, unless we have seen EOF or have very much 595 * Read from stdin, unless we have seen EOF or have very much
596 * buffered data to send to the server. 596 * buffered data to send to the server.
597 */ 597 */
598 if (!stdin_eof && packet_not_very_much_data_to_write()) 598 if (!stdin_eof && packet_not_very_much_data_to_write())
599 if ((ret = fileno(stdin)) != -1) 599 if ((ret = fileno(stdin)) != -1)
600 FD_SET(ret, *readsetp); 600 FD_SET(ret, *readsetp);
601 601
602 /* Select stdout/stderr if have data in buffer. */ 602 /* Select stdout/stderr if have data in buffer. */
603 if (buffer_len(&stdout_buffer) > 0) 603 if (buffer_len(&stdout_buffer) > 0)
604 if ((ret = fileno(stdout)) != -1) 604 if ((ret = fileno(stdout)) != -1)
605 FD_SET(ret, *writesetp); 605 FD_SET(ret, *writesetp);
606 if (buffer_len(&stderr_buffer) > 0) 606 if (buffer_len(&stderr_buffer) > 0)
607 if ((ret = fileno(stderr)) != -1) 607 if ((ret = fileno(stderr)) != -1)
608 FD_SET(ret, *writesetp); 608 FD_SET(ret, *writesetp);
609 } else { 609 } else {
610 /* channel_prepare_select could have closed the last channel */ 610 /* channel_prepare_select could have closed the last channel */
611 if (session_closed && !channel_still_open() && 611 if (session_closed && !channel_still_open() &&
612 !packet_have_data_to_write()) { 612 !packet_have_data_to_write()) {
613 /* clear mask since we did not call select() */ 613 /* clear mask since we did not call select() */
614 memset(*readsetp, 0, *nallocp); 614 memset(*readsetp, 0, *nallocp);
615 memset(*writesetp, 0, *nallocp); 615 memset(*writesetp, 0, *nallocp);
616 return; 616 return;
617 } else { 617 } else {
618 FD_SET(connection_in, *readsetp); 618 FD_SET(connection_in, *readsetp);
619 } 619 }
620 } 620 }
621 621
622 /* Select server connection if have data to write to the server. */ 622 /* Select server connection if have data to write to the server. */
623 if (packet_have_data_to_write()) 623 if (packet_have_data_to_write())
624 FD_SET(connection_out, *writesetp); 624 FD_SET(connection_out, *writesetp);
625 625
626 /* 626 /*
627 * Wait for something to happen. This will suspend the process until 627 * Wait for something to happen. This will suspend the process until
628 * some selected descriptor can be read, written, or has some other 628 * some selected descriptor can be read, written, or has some other
629 * event pending, or a timeout expires. 629 * event pending, or a timeout expires.
630 */ 630 */
631 631
632 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */ 632 timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
633 if (options.server_alive_interval > 0 && compat20) { 633 if (options.server_alive_interval > 0 && compat20) {
634 timeout_secs = options.server_alive_interval; 634 timeout_secs = options.server_alive_interval;
635 server_alive_time = now + options.server_alive_interval; 635 server_alive_time = now + options.server_alive_interval;
636 } 636 }
637 if (options.rekey_interval > 0 && compat20 && !rekeying) 637 if (options.rekey_interval > 0 && compat20 && !rekeying)
638 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout()); 638 timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout());
639 set_control_persist_exit_time(); 639 set_control_persist_exit_time();
640 if (control_persist_exit_time > 0) { 640 if (control_persist_exit_time > 0) {
641 timeout_secs = MIN(timeout_secs, 641 timeout_secs = MIN(timeout_secs,
642 control_persist_exit_time - now); 642 control_persist_exit_time - now);
643 if (timeout_secs < 0) 643 if (timeout_secs < 0)
644 timeout_secs = 0; 644 timeout_secs = 0;
645 } 645 }
646 if (minwait_secs != 0) 646 if (minwait_secs != 0)
647 timeout_secs = MIN(timeout_secs, (int)minwait_secs); 647 timeout_secs = MIN(timeout_secs, (int)minwait_secs);
648 if (timeout_secs == INT_MAX) 648 if (timeout_secs == INT_MAX)
649 tvp = NULL; 649 tvp = NULL;
650 else { 650 else {
651 tv.tv_sec = timeout_secs; 651 tv.tv_sec = timeout_secs;
652 tv.tv_usec = 0; 652 tv.tv_usec = 0;
653 tvp = &tv; 653 tvp = &tv;
654 } 654 }
655 655
656 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); 656 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
657 if (ret < 0) { 657 if (ret < 0) {
658 char buf[100]; 658 char buf[100];
659 659
660 /* 660 /*
661 * We have to clear the select masks, because we return. 661 * We have to clear the select masks, because we return.
662 * We have to return, because the mainloop checks for the flags 662 * We have to return, because the mainloop checks for the flags
663 * set by the signal handlers. 663 * set by the signal handlers.
664 */ 664 */
665 memset(*readsetp, 0, *nallocp); 665 memset(*readsetp, 0, *nallocp);
666 memset(*writesetp, 0, *nallocp); 666 memset(*writesetp, 0, *nallocp);
667 667
668 if (errno == EINTR) 668 if (errno == EINTR)
669 return; 669 return;
670 /* Note: we might still have data in the buffers. */ 670 /* Note: we might still have data in the buffers. */
671 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno)); 671 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
672 buffer_append(&stderr_buffer, buf, strlen(buf)); 672 buffer_append(&stderr_buffer, buf, strlen(buf));
673 quit_pending = 1; 673 quit_pending = 1;
674 } else if (ret == 0) { 674 } else if (ret == 0) {
675 /* 675 /*
676 * Timeout. Could have been either keepalive or rekeying. 676 * Timeout. Could have been either keepalive or rekeying.
677 * Keepalive we check here, rekeying is checked in clientloop. 677 * Keepalive we check here, rekeying is checked in clientloop.
678 */ 678 */
679 if (server_alive_time != 0 && server_alive_time <= monotime()) 679 if (server_alive_time != 0 && server_alive_time <= monotime())
680 server_alive_check(); 680 server_alive_check();
681 } 681 }
682 682
683} 683}
684 684
685static void 685static void
686client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr) 686client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
687{ 687{
688 /* Flush stdout and stderr buffers. */ 688 /* Flush stdout and stderr buffers. */
689 if (buffer_len(bout) > 0) 689 if (buffer_len(bout) > 0)
690 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), 690 atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
691 buffer_len(bout)); 691 buffer_len(bout));
692 if (buffer_len(berr) > 0) 692 if (buffer_len(berr) > 0)
693 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), 693 atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
694 buffer_len(berr)); 694 buffer_len(berr));
695 695
696 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 696 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
697 697
698 /* 698 /*
699 * Free (and clear) the buffer to reduce the amount of data that gets 699 * Free (and clear) the buffer to reduce the amount of data that gets
700 * written to swap. 700 * written to swap.
701 */ 701 */
702 buffer_free(bin); 702 buffer_free(bin);
703 buffer_free(bout); 703 buffer_free(bout);
704 buffer_free(berr); 704 buffer_free(berr);
705 705
706 /* Send the suspend signal to the program itself. */ 706 /* Send the suspend signal to the program itself. */
707 kill(getpid(), SIGTSTP); 707 kill(getpid(), SIGTSTP);
708 708
709 /* Reset window sizes in case they have changed */ 709 /* Reset window sizes in case they have changed */
710 received_window_change_signal = 1; 710 received_window_change_signal = 1;
711 711
712 /* OK, we have been continued by the user. Reinitialize buffers. */ 712 /* OK, we have been continued by the user. Reinitialize buffers. */
713 buffer_init(bin); 713 buffer_init(bin);
714 buffer_init(bout); 714 buffer_init(bout);
715 buffer_init(berr); 715 buffer_init(berr);
716 716
717 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 717 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
718} 718}
719 719
720static void 720static void
721client_process_net_input(fd_set *readset) 721client_process_net_input(fd_set *readset)
722{ 722{
723 int len, cont = 0; 723 int len, cont = 0;
724 char buf[8192]; 724 char buf[8192];
725 725
726 /* 726 /*
727 * Read input from the server, and add any such data to the buffer of 727 * Read input from the server, and add any such data to the buffer of
728 * the packet subsystem. 728 * the packet subsystem.
729 */ 729 */
730 if (FD_ISSET(connection_in, readset)) { 730 if (FD_ISSET(connection_in, readset)) {
731 /* Read as much as possible. */ 731 /* Read as much as possible. */
732 len = roaming_read(connection_in, buf, sizeof(buf), &cont); 732 len = roaming_read(connection_in, buf, sizeof(buf), &cont);
733 if (len == 0 && cont == 0) { 733 if (len == 0 && cont == 0) {
734 /* 734 /*
735 * Received EOF. The remote host has closed the 735 * Received EOF. The remote host has closed the
736 * connection. 736 * connection.
737 */ 737 */
738 snprintf(buf, sizeof buf, 738 snprintf(buf, sizeof buf,
739 "Connection to %.300s closed by remote host.\r\n", 739 "Connection to %.300s closed by remote host.\r\n",
740 host); 740 host);
741 buffer_append(&stderr_buffer, buf, strlen(buf)); 741 buffer_append(&stderr_buffer, buf, strlen(buf));
742 quit_pending = 1; 742 quit_pending = 1;
743 return; 743 return;
744 } 744 }
745 /* 745 /*
746 * There is a kernel bug on Solaris that causes select to 746 * There is a kernel bug on Solaris that causes select to
747 * sometimes wake up even though there is no data available. 747 * sometimes wake up even though there is no data available.
748 */ 748 */
749 if (len < 0 && (errno == EAGAIN || errno == EINTR)) 749 if (len < 0 && (errno == EAGAIN || errno == EINTR))
750 len = 0; 750 len = 0;
751 751
752 if (len < 0) { 752 if (len < 0) {
753 /* 753 /*
754 * An error has encountered. Perhaps there is a 754 * An error has encountered. Perhaps there is a
755 * network problem. 755 * network problem.
756 */ 756 */
757 snprintf(buf, sizeof buf, 757 snprintf(buf, sizeof buf,
758 "Read from remote host %.300s: %.100s\r\n", 758 "Read from remote host %.300s: %.100s\r\n",
759 host, strerror(errno)); 759 host, strerror(errno));
760 buffer_append(&stderr_buffer, buf, strlen(buf)); 760 buffer_append(&stderr_buffer, buf, strlen(buf));
761 quit_pending = 1; 761 quit_pending = 1;
762 return; 762 return;
763 } 763 }
764 packet_process_incoming(buf, len); 764 packet_process_incoming(buf, len);
765 } 765 }
766} 766}
767 767
768static void 768static void
769client_status_confirm(int type, Channel *c, void *ctx) 769client_status_confirm(int type, Channel *c, void *ctx)
770{ 770{
771 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx; 771 struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
772 char errmsg[256]; 772 char errmsg[256];
773 int tochan; 773 int tochan;
774 774
775 /* 775 /*
776 * If a TTY was explicitly requested, then a failure to allocate 776 * If a TTY was explicitly requested, then a failure to allocate
777 * one is fatal. 777 * one is fatal.
778 */ 778 */
779 if (cr->action == CONFIRM_TTY && 779 if (cr->action == CONFIRM_TTY &&
780 (options.request_tty == REQUEST_TTY_FORCE || 780 (options.request_tty == REQUEST_TTY_FORCE ||
781 options.request_tty == REQUEST_TTY_YES)) 781 options.request_tty == REQUEST_TTY_YES))
782 cr->action = CONFIRM_CLOSE; 782 cr->action = CONFIRM_CLOSE;
783 783
784 /* XXX supress on mux _client_ quietmode */ 784 /* XXX supress on mux _client_ quietmode */
785 tochan = options.log_level >= SYSLOG_LEVEL_ERROR && 785 tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
786 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE; 786 c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
787 787
788 if (type == SSH2_MSG_CHANNEL_SUCCESS) { 788 if (type == SSH2_MSG_CHANNEL_SUCCESS) {
789 debug2("%s request accepted on channel %d", 789 debug2("%s request accepted on channel %d",
790 cr->request_type, c->self); 790 cr->request_type, c->self);
791 } else if (type == SSH2_MSG_CHANNEL_FAILURE) { 791 } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
792 if (tochan) { 792 if (tochan) {
793 snprintf(errmsg, sizeof(errmsg), 793 snprintf(errmsg, sizeof(errmsg),
794 "%s request failed\r\n", cr->request_type); 794 "%s request failed\r\n", cr->request_type);
795 } else { 795 } else {
796 snprintf(errmsg, sizeof(errmsg), 796 snprintf(errmsg, sizeof(errmsg),
797 "%s request failed on channel %d", 797 "%s request failed on channel %d",
798 cr->request_type, c->self); 798 cr->request_type, c->self);
799 } 799 }
800 /* If error occurred on primary session channel, then exit */ 800 /* If error occurred on primary session channel, then exit */
801 if (cr->action == CONFIRM_CLOSE && c->self == session_ident) 801 if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
802 fatal("%s", errmsg); 802 fatal("%s", errmsg);
803 /* 803 /*
804 * If error occurred on mux client, append to 804 * If error occurred on mux client, append to
805 * their stderr. 805 * their stderr.
806 */ 806 */
807 if (tochan) { 807 if (tochan) {
808 buffer_append(&c->extended, errmsg, 808 buffer_append(&c->extended, errmsg,
809 strlen(errmsg)); 809 strlen(errmsg));
810 } else 810 } else
811 error("%s", errmsg); 811 error("%s", errmsg);
812 if (cr->action == CONFIRM_TTY) { 812 if (cr->action == CONFIRM_TTY) {
813 /* 813 /*
814 * If a TTY allocation error occurred, then arrange 814 * If a TTY allocation error occurred, then arrange
815 * for the correct TTY to leave raw mode. 815 * for the correct TTY to leave raw mode.
816 */ 816 */
817 if (c->self == session_ident) 817 if (c->self == session_ident)
818 leave_raw_mode(0); 818 leave_raw_mode(0);
819 else 819 else
820 mux_tty_alloc_failed(c); 820 mux_tty_alloc_failed(c);
821 } else if (cr->action == CONFIRM_CLOSE) { 821 } else if (cr->action == CONFIRM_CLOSE) {
822 chan_read_failed(c); 822 chan_read_failed(c);
823 chan_write_failed(c); 823 chan_write_failed(c);
824 } 824 }
825 } 825 }
826 free(cr); 826 free(cr);
827} 827}
828 828
829static void 829static void
830client_abandon_status_confirm(Channel *c, void *ctx) 830client_abandon_status_confirm(Channel *c, void *ctx)
831{ 831{
832 free(ctx); 832 free(ctx);
833} 833}
834 834
835void 835void
836client_expect_confirm(int id, const char *request, 836client_expect_confirm(int id, const char *request,
837 enum confirm_action action) 837 enum confirm_action action)
838{ 838{
839 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr)); 839 struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
840 840
841 cr->request_type = request; 841 cr->request_type = request;
842 cr->action = action; 842 cr->action = action;
843 843
844 channel_register_status_confirm(id, client_status_confirm, 844 channel_register_status_confirm(id, client_status_confirm,
845 client_abandon_status_confirm, cr); 845 client_abandon_status_confirm, cr);
846} 846}
847 847
848void 848void
849client_register_global_confirm(global_confirm_cb *cb, void *ctx) 849client_register_global_confirm(global_confirm_cb *cb, void *ctx)
850{ 850{
851 struct global_confirm *gc, *last_gc; 851 struct global_confirm *gc, *last_gc;
852 852
853 /* Coalesce identical callbacks */ 853 /* Coalesce identical callbacks */
854 last_gc = TAILQ_LAST(&global_confirms, global_confirms); 854 last_gc = TAILQ_LAST(&global_confirms, global_confirms);
855 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) { 855 if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
856 if (++last_gc->ref_count >= INT_MAX) 856 if (++last_gc->ref_count >= INT_MAX)
857 fatal("%s: last_gc->ref_count = %d", 857 fatal("%s: last_gc->ref_count = %d",
858 __func__, last_gc->ref_count); 858 __func__, last_gc->ref_count);
859 return; 859 return;
860 } 860 }
861 861
862 gc = xcalloc(1, sizeof(*gc)); 862 gc = xcalloc(1, sizeof(*gc));
863 gc->cb = cb; 863 gc->cb = cb;
864 gc->ctx = ctx; 864 gc->ctx = ctx;
865 gc->ref_count = 1; 865 gc->ref_count = 1;
866 TAILQ_INSERT_TAIL(&global_confirms, gc, entry); 866 TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
867} 867}
868 868
869static void 869static void
870process_cmdline(void) 870process_cmdline(void)
871{ 871{
872 void (*handler)(int); 872 void (*handler)(int);
873 char *s, *cmd; 873 char *s, *cmd;
874 int ok, delete = 0, local = 0, remote = 0, dynamic = 0; 874 int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
875 struct Forward fwd; 875 struct Forward fwd;
876 876
877 memset(&fwd, 0, sizeof(fwd)); 877 memset(&fwd, 0, sizeof(fwd));
878 878
879 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 879 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
880 handler = signal(SIGINT, SIG_IGN); 880 handler = signal(SIGINT, SIG_IGN);
881 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO); 881 cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
882 if (s == NULL) 882 if (s == NULL)
883 goto out; 883 goto out;
884 while (isspace((u_char)*s)) 884 while (isspace((u_char)*s))
885 s++; 885 s++;
886 if (*s == '-') 886 if (*s == '-')
887 s++; /* Skip cmdline '-', if any */ 887 s++; /* Skip cmdline '-', if any */
888 if (*s == '\0') 888 if (*s == '\0')
889 goto out; 889 goto out;
890 890
891 if (*s == 'h' || *s == 'H' || *s == '?') { 891 if (*s == 'h' || *s == 'H' || *s == '?') {
892 logit("Commands:"); 892 logit("Commands:");
893 logit(" -L[bind_address:]port:host:hostport " 893 logit(" -L[bind_address:]port:host:hostport "
894 "Request local forward"); 894 "Request local forward");
895 logit(" -R[bind_address:]port:host:hostport " 895 logit(" -R[bind_address:]port:host:hostport "
896 "Request remote forward"); 896 "Request remote forward");
897 logit(" -D[bind_address:]port " 897 logit(" -D[bind_address:]port "
898 "Request dynamic forward"); 898 "Request dynamic forward");
899 logit(" -KL[bind_address:]port " 899 logit(" -KL[bind_address:]port "
900 "Cancel local forward"); 900 "Cancel local forward");
901 logit(" -KR[bind_address:]port " 901 logit(" -KR[bind_address:]port "
902 "Cancel remote forward"); 902 "Cancel remote forward");
903 logit(" -KD[bind_address:]port " 903 logit(" -KD[bind_address:]port "
904 "Cancel dynamic forward"); 904 "Cancel dynamic forward");
905 if (!options.permit_local_command) 905 if (!options.permit_local_command)
906 goto out; 906 goto out;
907 logit(" !args " 907 logit(" !args "
908 "Execute local command"); 908 "Execute local command");
909 goto out; 909 goto out;
910 } 910 }
911 911
912 if (*s == '!' && options.permit_local_command) { 912 if (*s == '!' && options.permit_local_command) {
913 s++; 913 s++;
914 ssh_local_cmd(s); 914 ssh_local_cmd(s);
915 goto out; 915 goto out;
916 } 916 }
917 917
918 if (*s == 'K') { 918 if (*s == 'K') {
919 delete = 1; 919 delete = 1;
920 s++; 920 s++;
921 } 921 }
922 if (*s == 'L') 922 if (*s == 'L')
923 local = 1; 923 local = 1;
924 else if (*s == 'R') 924 else if (*s == 'R')
925 remote = 1; 925 remote = 1;
926 else if (*s == 'D') 926 else if (*s == 'D')
927 dynamic = 1; 927 dynamic = 1;
928 else { 928 else {
929 logit("Invalid command."); 929 logit("Invalid command.");
930 goto out; 930 goto out;
931 } 931 }
932 932
933 if (delete && !compat20) { 933 if (delete && !compat20) {
934 logit("Not supported for SSH protocol version 1."); 934 logit("Not supported for SSH protocol version 1.");
935 goto out; 935 goto out;
936 } 936 }
937 937
938 s++; 938 s++;
939 while (isspace((u_char)*s)) 939 while (isspace((u_char)*s))
940 s++; 940 s++;
941 941
942 /* XXX update list of forwards in options */ 942 /* XXX update list of forwards in options */
943 if (delete) { 943 if (delete) {
944 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */ 944 /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
945 if (!parse_forward(&fwd, s, 1, 0)) { 945 if (!parse_forward(&fwd, s, 1, 0)) {
946 logit("Bad forwarding close specification."); 946 logit("Bad forwarding close specification.");
947 goto out; 947 goto out;
948 } 948 }
949 if (remote) 949 if (remote)
950 ok = channel_request_rforward_cancel(&fwd) == 0; 950 ok = channel_request_rforward_cancel(&fwd) == 0;
951 else if (dynamic) 951 else if (dynamic)
952 ok = channel_cancel_lport_listener(&fwd, 952 ok = channel_cancel_lport_listener(&fwd,
953 0, &options.fwd_opts) > 0; 953 0, &options.fwd_opts) > 0;
954 else 954 else
955 ok = channel_cancel_lport_listener(&fwd, 955 ok = channel_cancel_lport_listener(&fwd,
956 CHANNEL_CANCEL_PORT_STATIC, 956 CHANNEL_CANCEL_PORT_STATIC,
957 &options.fwd_opts) > 0; 957 &options.fwd_opts) > 0;
958 if (!ok) { 958 if (!ok) {
959 logit("Unkown port forwarding."); 959 logit("Unkown port forwarding.");
960 goto out; 960 goto out;
961 } 961 }
962 logit("Canceled forwarding."); 962 logit("Canceled forwarding.");
963 } else { 963 } else {
964 if (!parse_forward(&fwd, s, dynamic, remote)) { 964 if (!parse_forward(&fwd, s, dynamic, remote)) {
965 logit("Bad forwarding specification."); 965 logit("Bad forwarding specification.");
966 goto out; 966 goto out;
967 } 967 }
968 if (local || dynamic) { 968 if (local || dynamic) {
969 if (!channel_setup_local_fwd_listener(&fwd, 969 if (!channel_setup_local_fwd_listener(&fwd,
970 &options.fwd_opts)) { 970 &options.fwd_opts)) {
971 logit("Port forwarding failed."); 971 logit("Port forwarding failed.");
972 goto out; 972 goto out;
973 } 973 }
974 } else { 974 } else {
975 if (channel_request_remote_forwarding(&fwd) < 0) { 975 if (channel_request_remote_forwarding(&fwd) < 0) {
976 logit("Port forwarding failed."); 976 logit("Port forwarding failed.");
977 goto out; 977 goto out;
978 } 978 }
979 } 979 }
980 logit("Forwarding port."); 980 logit("Forwarding port.");
981 } 981 }
982 982
983out: 983out:
984 signal(SIGINT, handler); 984 signal(SIGINT, handler);
985 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 985 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
986 free(cmd); 986 free(cmd);
987 free(fwd.listen_host); 987 free(fwd.listen_host);
988 free(fwd.listen_path); 988 free(fwd.listen_path);
989 free(fwd.connect_host); 989 free(fwd.connect_host);
990 free(fwd.connect_path); 990 free(fwd.connect_path);
991} 991}
992 992
993/* reasons to suppress output of an escape command in help output */ 993/* reasons to suppress output of an escape command in help output */
994#define SUPPRESS_NEVER 0 /* never suppress, always show */ 994#define SUPPRESS_NEVER 0 /* never suppress, always show */
995#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */ 995#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */
996#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */ 996#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */
997#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */ 997#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */
998#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */ 998#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */
999struct escape_help_text { 999struct escape_help_text {
1000 const char *cmd; 1000 const char *cmd;
1001 const char *text; 1001 const char *text;
1002 unsigned int flags; 1002 unsigned int flags;
1003}; 1003};
1004static struct escape_help_text esc_txt[] = { 1004static struct escape_help_text esc_txt[] = {
1005 {".", "terminate session", SUPPRESS_MUXMASTER}, 1005 {".", "terminate session", SUPPRESS_MUXMASTER},
1006 {".", "terminate connection (and any multiplexed sessions)", 1006 {".", "terminate connection (and any multiplexed sessions)",
1007 SUPPRESS_MUXCLIENT}, 1007 SUPPRESS_MUXCLIENT},
1008 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1}, 1008 {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1},
1009 {"C", "open a command line", SUPPRESS_MUXCLIENT}, 1009 {"C", "open a command line", SUPPRESS_MUXCLIENT},
1010 {"R", "request rekey", SUPPRESS_PROTO1}, 1010 {"R", "request rekey", SUPPRESS_PROTO1},
1011 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT}, 1011 {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
1012 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT}, 1012 {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
1013 {"#", "list forwarded connections", SUPPRESS_NEVER}, 1013 {"#", "list forwarded connections", SUPPRESS_NEVER},
1014 {"&", "background ssh (when waiting for connections to terminate)", 1014 {"&", "background ssh (when waiting for connections to terminate)",
1015 SUPPRESS_MUXCLIENT}, 1015 SUPPRESS_MUXCLIENT},
1016 {"?", "this message", SUPPRESS_NEVER}, 1016 {"?", "this message", SUPPRESS_NEVER},
1017}; 1017};
1018 1018
1019static void 1019static void
1020print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client, 1020print_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client,
1021 int using_stderr) 1021 int using_stderr)
1022{ 1022{
1023 unsigned int i, suppress_flags; 1023 unsigned int i, suppress_flags;
1024 char string[1024]; 1024 char string[1024];
1025 1025
1026 snprintf(string, sizeof string, "%c?\r\n" 1026 snprintf(string, sizeof string, "%c?\r\n"
1027 "Supported escape sequences:\r\n", escape_char); 1027 "Supported escape sequences:\r\n", escape_char);
1028 buffer_append(b, string, strlen(string)); 1028 buffer_append(b, string, strlen(string));
1029 1029
1030 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) | 1030 suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) |
1031 (mux_client ? SUPPRESS_MUXCLIENT : 0) | 1031 (mux_client ? SUPPRESS_MUXCLIENT : 0) |
1032 (mux_client ? 0 : SUPPRESS_MUXMASTER) | 1032 (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1033 (using_stderr ? 0 : SUPPRESS_SYSLOG); 1033 (using_stderr ? 0 : SUPPRESS_SYSLOG);
1034 1034
1035 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) { 1035 for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
1036 if (esc_txt[i].flags & suppress_flags) 1036 if (esc_txt[i].flags & suppress_flags)
1037 continue; 1037 continue;
1038 snprintf(string, sizeof string, " %c%-3s - %s\r\n", 1038 snprintf(string, sizeof string, " %c%-3s - %s\r\n",
1039 escape_char, esc_txt[i].cmd, esc_txt[i].text); 1039 escape_char, esc_txt[i].cmd, esc_txt[i].text);
1040 buffer_append(b, string, strlen(string)); 1040 buffer_append(b, string, strlen(string));
1041 } 1041 }
1042 1042
1043 snprintf(string, sizeof string, 1043 snprintf(string, sizeof string,
1044 " %c%c - send the escape character by typing it twice\r\n" 1044 " %c%c - send the escape character by typing it twice\r\n"
1045 "(Note that escapes are only recognized immediately after " 1045 "(Note that escapes are only recognized immediately after "
1046 "newline.)\r\n", escape_char, escape_char); 1046 "newline.)\r\n", escape_char, escape_char);
1047 buffer_append(b, string, strlen(string)); 1047 buffer_append(b, string, strlen(string));
1048} 1048}
1049 1049
1050/*  1050/*
1051 * Process the characters one by one, call with c==NULL for proto1 case. 1051 * Process the characters one by one, call with c==NULL for proto1 case.
1052 */ 1052 */
1053static int 1053static int
1054process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr, 1054process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
1055 const char *buf, int len) 1055 const char *buf, int len)
1056{ 1056{
1057 char string[1024]; 1057 char string[1024];
1058 pid_t pid; 1058 pid_t pid;
1059 int bytes = 0; 1059 int bytes = 0;
1060 u_int i; 1060 u_int i;
1061 u_char ch; 1061 u_char ch;
1062 char *s; 1062 char *s;
1063 int *escape_pendingp, escape_char; 1063 int *escape_pendingp, escape_char;
1064 struct escape_filter_ctx *efc; 1064 struct escape_filter_ctx *efc;
1065 1065
1066 if (c == NULL) { 1066 if (c == NULL) {
1067 escape_pendingp = &escape_pending1; 1067 escape_pendingp = &escape_pending1;
1068 escape_char = escape_char1; 1068 escape_char = escape_char1;
1069 } else { 1069 } else {
1070 if (c->filter_ctx == NULL) 1070 if (c->filter_ctx == NULL)
1071 return 0; 1071 return 0;
1072 efc = (struct escape_filter_ctx *)c->filter_ctx; 1072 efc = (struct escape_filter_ctx *)c->filter_ctx;
1073 escape_pendingp = &efc->escape_pending; 1073 escape_pendingp = &efc->escape_pending;
1074 escape_char = efc->escape_char; 1074 escape_char = efc->escape_char;
1075 } 1075 }
1076  1076
1077 if (len <= 0) 1077 if (len <= 0)
1078 return (0); 1078 return (0);
1079 1079
1080 for (i = 0; i < (u_int)len; i++) { 1080 for (i = 0; i < (u_int)len; i++) {
1081 /* Get one character at a time. */ 1081 /* Get one character at a time. */
1082 ch = buf[i]; 1082 ch = buf[i];
1083 1083
1084 if (*escape_pendingp) { 1084 if (*escape_pendingp) {
1085 /* We have previously seen an escape character. */ 1085 /* We have previously seen an escape character. */
1086 /* Clear the flag now. */ 1086 /* Clear the flag now. */
1087 *escape_pendingp = 0; 1087 *escape_pendingp = 0;
1088 1088
1089 /* Process the escaped character. */ 1089 /* Process the escaped character. */
1090 switch (ch) { 1090 switch (ch) {
1091 case '.': 1091 case '.':
1092 /* Terminate the connection. */ 1092 /* Terminate the connection. */
1093 snprintf(string, sizeof string, "%c.\r\n", 1093 snprintf(string, sizeof string, "%c.\r\n",
1094 escape_char); 1094 escape_char);
1095 buffer_append(berr, string, strlen(string)); 1095 buffer_append(berr, string, strlen(string));
1096 1096
1097 if (c && c->ctl_chan != -1) { 1097 if (c && c->ctl_chan != -1) {
1098 chan_read_failed(c); 1098 chan_read_failed(c);
1099 chan_write_failed(c); 1099 chan_write_failed(c);
1100 if (c->detach_user) 1100 if (c->detach_user)
1101 c->detach_user(c->self, NULL); 1101 c->detach_user(c->self, NULL);
1102 c->type = SSH_CHANNEL_ABANDONED; 1102 c->type = SSH_CHANNEL_ABANDONED;
1103 buffer_clear(&c->input); 1103 buffer_clear(&c->input);
1104 chan_ibuf_empty(c); 1104 chan_ibuf_empty(c);
1105 return 0; 1105 return 0;
1106 } else 1106 } else
1107 quit_pending = 1; 1107 quit_pending = 1;
1108 return -1; 1108 return -1;
1109 1109
1110 case 'Z' - 64: 1110 case 'Z' - 64:
1111 /* XXX support this for mux clients */ 1111 /* XXX support this for mux clients */
1112 if (c && c->ctl_chan != -1) { 1112 if (c && c->ctl_chan != -1) {
1113 char b[16]; 1113 char b[16];
1114 noescape: 1114 noescape:
1115 if (ch == 'Z' - 64) 1115 if (ch == 'Z' - 64)
1116 snprintf(b, sizeof b, "^Z"); 1116 snprintf(b, sizeof b, "^Z");
1117 else 1117 else
1118 snprintf(b, sizeof b, "%c", ch); 1118 snprintf(b, sizeof b, "%c", ch);
1119 snprintf(string, sizeof string, 1119 snprintf(string, sizeof string,
1120 "%c%s escape not available to " 1120 "%c%s escape not available to "
1121 "multiplexed sessions\r\n", 1121 "multiplexed sessions\r\n",
1122 escape_char, b); 1122 escape_char, b);
1123 buffer_append(berr, string, 1123 buffer_append(berr, string,
1124 strlen(string)); 1124 strlen(string));
1125 continue; 1125 continue;
1126 } 1126 }
1127 /* Suspend the program. Inform the user */ 1127 /* Suspend the program. Inform the user */
1128 snprintf(string, sizeof string, 1128 snprintf(string, sizeof string,
1129 "%c^Z [suspend ssh]\r\n", escape_char); 1129 "%c^Z [suspend ssh]\r\n", escape_char);
1130 buffer_append(berr, string, strlen(string)); 1130 buffer_append(berr, string, strlen(string));
1131 1131
1132 /* Restore terminal modes and suspend. */ 1132 /* Restore terminal modes and suspend. */
1133 client_suspend_self(bin, bout, berr); 1133 client_suspend_self(bin, bout, berr);
1134 1134
1135 /* We have been continued. */ 1135 /* We have been continued. */
1136 continue; 1136 continue;
1137 1137
1138 case 'B': 1138 case 'B':
1139 if (compat20) { 1139 if (compat20) {
1140 snprintf(string, sizeof string, 1140 snprintf(string, sizeof string,
1141 "%cB\r\n", escape_char); 1141 "%cB\r\n", escape_char);
1142 buffer_append(berr, string, 1142 buffer_append(berr, string,
1143 strlen(string)); 1143 strlen(string));
1144 channel_request_start(c->self, 1144 channel_request_start(c->self,
1145 "break", 0); 1145 "break", 0);
1146 packet_put_int(1000); 1146 packet_put_int(1000);
1147 packet_send(); 1147 packet_send();
1148 } 1148 }
1149 continue; 1149 continue;
1150 1150
1151 case 'R': 1151 case 'R':
1152 if (compat20) { 1152 if (compat20) {
1153 if (datafellows & SSH_BUG_NOREKEY) 1153 if (datafellows & SSH_BUG_NOREKEY)
1154 logit("Server does not " 1154 logit("Server does not "
1155 "support re-keying"); 1155 "support re-keying");
1156 else 1156 else
1157 need_rekeying = 1; 1157 need_rekeying = 1;
1158 } 1158 }
1159 continue; 1159 continue;
1160 1160
1161 case 'V': 1161 case 'V':
1162 /* FALLTHROUGH */ 1162 /* FALLTHROUGH */
1163 case 'v': 1163 case 'v':
1164 if (c && c->ctl_chan != -1) 1164 if (c && c->ctl_chan != -1)
1165 goto noescape; 1165 goto noescape;
1166 if (!log_is_on_stderr()) { 1166 if (!log_is_on_stderr()) {
1167 snprintf(string, sizeof string, 1167 snprintf(string, sizeof string,
1168 "%c%c [Logging to syslog]\r\n", 1168 "%c%c [Logging to syslog]\r\n",
1169 escape_char, ch); 1169 escape_char, ch);
1170 buffer_append(berr, string, 1170 buffer_append(berr, string,
1171 strlen(string)); 1171 strlen(string));
1172 continue; 1172 continue;
1173 } 1173 }
1174 if (ch == 'V' && options.log_level > 1174 if (ch == 'V' && options.log_level >
1175 SYSLOG_LEVEL_QUIET) 1175 SYSLOG_LEVEL_QUIET)
1176 log_change_level(--options.log_level); 1176 log_change_level(--options.log_level);
1177 if (ch == 'v' && options.log_level < 1177 if (ch == 'v' && options.log_level <
1178 SYSLOG_LEVEL_DEBUG3) 1178 SYSLOG_LEVEL_DEBUG3)
1179 log_change_level(++options.log_level); 1179 log_change_level(++options.log_level);
1180 snprintf(string, sizeof string, 1180 snprintf(string, sizeof string,
1181 "%c%c [LogLevel %s]\r\n", escape_char, ch, 1181 "%c%c [LogLevel %s]\r\n", escape_char, ch,
1182 log_level_name(options.log_level)); 1182 log_level_name(options.log_level));
1183 buffer_append(berr, string, strlen(string)); 1183 buffer_append(berr, string, strlen(string));
1184 continue; 1184 continue;
1185 1185
1186 case '&': 1186 case '&':
1187 if (c && c->ctl_chan != -1) 1187 if (c && c->ctl_chan != -1)
1188 goto noescape; 1188 goto noescape;
1189 /* 1189 /*
1190 * Detach the program (continue to serve 1190 * Detach the program (continue to serve
1191 * connections, but put in background and no 1191 * connections, but put in background and no
1192 * more new connections). 1192 * more new connections).
1193 */ 1193 */
1194 /* Restore tty modes. */ 1194 /* Restore tty modes. */
1195 leave_raw_mode( 1195 leave_raw_mode(
1196 options.request_tty == REQUEST_TTY_FORCE); 1196 options.request_tty == REQUEST_TTY_FORCE);
1197 1197
1198 /* Stop listening for new connections. */ 1198 /* Stop listening for new connections. */
1199 channel_stop_listening(); 1199 channel_stop_listening();
1200 1200
1201 snprintf(string, sizeof string, 1201 snprintf(string, sizeof string,
1202 "%c& [backgrounded]\n", escape_char); 1202 "%c& [backgrounded]\n", escape_char);
1203 buffer_append(berr, string, strlen(string)); 1203 buffer_append(berr, string, strlen(string));
1204 1204
1205 /* Fork into background. */ 1205 /* Fork into background. */
1206 pid = fork(); 1206 pid = fork();
1207 if (pid < 0) { 1207 if (pid < 0) {
1208 error("fork: %.100s", strerror(errno)); 1208 error("fork: %.100s", strerror(errno));
1209 continue; 1209 continue;
1210 } 1210 }
1211 if (pid != 0) { /* This is the parent. */ 1211 if (pid != 0) { /* This is the parent. */
1212 /* The parent just exits. */ 1212 /* The parent just exits. */
1213 exit(0); 1213 exit(0);
1214 } 1214 }
1215 /* The child continues serving connections. */ 1215 /* The child continues serving connections. */
1216 if (compat20) { 1216 if (compat20) {
1217 buffer_append(bin, "\004", 1); 1217 buffer_append(bin, "\004", 1);
1218 /* fake EOF on stdin */ 1218 /* fake EOF on stdin */
1219 return -1; 1219 return -1;
1220 } else if (!stdin_eof) { 1220 } else if (!stdin_eof) {
1221 /* 1221 /*
1222 * Sending SSH_CMSG_EOF alone does not 1222 * Sending SSH_CMSG_EOF alone does not
1223 * always appear to be enough. So we 1223 * always appear to be enough. So we
1224 * try to send an EOF character first. 1224 * try to send an EOF character first.
1225 */ 1225 */
1226 packet_start(SSH_CMSG_STDIN_DATA); 1226 packet_start(SSH_CMSG_STDIN_DATA);
1227 packet_put_string("\004", 1); 1227 packet_put_string("\004", 1);
1228 packet_send(); 1228 packet_send();
1229 /* Close stdin. */ 1229 /* Close stdin. */
1230 stdin_eof = 1; 1230 stdin_eof = 1;
1231 if (buffer_len(bin) == 0) { 1231 if (buffer_len(bin) == 0) {
1232 packet_start(SSH_CMSG_EOF); 1232 packet_start(SSH_CMSG_EOF);
1233 packet_send(); 1233 packet_send();
1234 } 1234 }
1235 } 1235 }
1236 continue; 1236 continue;
1237 1237
1238 case '?': 1238 case '?':
1239 print_escape_help(berr, escape_char, compat20, 1239 print_escape_help(berr, escape_char, compat20,
1240 (c && c->ctl_chan != -1), 1240 (c && c->ctl_chan != -1),
1241 log_is_on_stderr()); 1241 log_is_on_stderr());
1242 continue; 1242 continue;
1243 1243
1244 case '#': 1244 case '#':
1245 snprintf(string, sizeof string, "%c#\r\n", 1245 snprintf(string, sizeof string, "%c#\r\n",
1246 escape_char); 1246 escape_char);
1247 buffer_append(berr, string, strlen(string)); 1247 buffer_append(berr, string, strlen(string));
1248 s = channel_open_message(); 1248 s = channel_open_message();
1249 buffer_append(berr, s, strlen(s)); 1249 buffer_append(berr, s, strlen(s));
1250 free(s); 1250 free(s);
1251 continue; 1251 continue;
1252 1252
1253 case 'C': 1253 case 'C':
1254 if (c && c->ctl_chan != -1) 1254 if (c && c->ctl_chan != -1)
1255 goto noescape; 1255 goto noescape;
1256 process_cmdline(); 1256 process_cmdline();
1257 continue; 1257 continue;
1258 1258
1259 default: 1259 default:
1260 if (ch != escape_char) { 1260 if (ch != escape_char) {
1261 buffer_put_char(bin, escape_char); 1261 buffer_put_char(bin, escape_char);
1262 bytes++; 1262 bytes++;
1263 } 1263 }
1264 /* Escaped characters fall through here */ 1264 /* Escaped characters fall through here */
1265 break; 1265 break;
1266 } 1266 }
1267 } else { 1267 } else {
1268 /* 1268 /*
1269 * The previous character was not an escape char. 1269 * The previous character was not an escape char.
1270 * Check if this is an escape. 1270 * Check if this is an escape.
1271 */ 1271 */
1272 if (last_was_cr && ch == escape_char) { 1272 if (last_was_cr && ch == escape_char) {
1273 /* 1273 /*
1274 * It is. Set the flag and continue to 1274 * It is. Set the flag and continue to
1275 * next character. 1275 * next character.
1276 */ 1276 */
1277 *escape_pendingp = 1; 1277 *escape_pendingp = 1;
1278 continue; 1278 continue;
1279 } 1279 }
1280 } 1280 }
1281 1281
1282 /* 1282 /*
1283 * Normal character. Record whether it was a newline, 1283 * Normal character. Record whether it was a newline,
1284 * and append it to the buffer. 1284 * and append it to the buffer.
1285 */ 1285 */
1286 last_was_cr = (ch == '\r' || ch == '\n'); 1286 last_was_cr = (ch == '\r' || ch == '\n');
1287 buffer_put_char(bin, ch); 1287 buffer_put_char(bin, ch);
1288 bytes++; 1288 bytes++;
1289 } 1289 }
1290 return bytes; 1290 return bytes;
1291} 1291}
1292 1292
1293static void 1293static void
1294client_process_input(fd_set *readset) 1294client_process_input(fd_set *readset)
1295{ 1295{
1296 int len, fd; 1296 int len, fd;
1297 char buf[8192]; 1297 char buf[8192];
1298 1298
1299 /* Read input from stdin. */ 1299 /* Read input from stdin. */
1300 if ((fd = fileno(stdin)) == -1 || !FD_ISSET(fd, readset)) 1300 if ((fd = fileno(stdin)) == -1 || !FD_ISSET(fd, readset))
1301 return; 1301 return;
1302 /* Read as much as possible. */ 1302 /* Read as much as possible. */
1303 len = read(fd, buf, sizeof(buf)); 1303 len = read(fd, buf, sizeof(buf));
1304 if (len < 0 && (errno == EAGAIN || errno == EINTR)) 1304 if (len < 0 && (errno == EAGAIN || errno == EINTR))
1305 return; /* we'll try again later */ 1305 return; /* we'll try again later */
1306 if (len <= 0) { 1306 if (len <= 0) {
1307 /* 1307 /*
1308 * Received EOF or error. They are treated 1308 * Received EOF or error. They are treated
1309 * similarly, except that an error message is printed 1309 * similarly, except that an error message is printed
1310 * if it was an error condition. 1310 * if it was an error condition.
1311 */ 1311 */
1312 if (len < 0) { 1312 if (len < 0) {
1313 snprintf(buf, sizeof buf, "read: %.100s\r\n", 1313 snprintf(buf, sizeof buf, "read: %.100s\r\n",
1314 strerror(errno)); 1314 strerror(errno));
1315 buffer_append(&stderr_buffer, buf, strlen(buf)); 1315 buffer_append(&stderr_buffer, buf, strlen(buf));
1316 } 1316 }
1317 /* Mark that we have seen EOF. */ 1317 /* Mark that we have seen EOF. */
1318 stdin_eof = 1; 1318 stdin_eof = 1;
1319 /* 1319 /*
1320 * Send an EOF message to the server unless there is 1320 * Send an EOF message to the server unless there is
1321 * data in the buffer. If there is data in the 1321 * data in the buffer. If there is data in the
1322 * buffer, no message will be sent now. Code 1322 * buffer, no message will be sent now. Code
1323 * elsewhere will send the EOF when the buffer 1323 * elsewhere will send the EOF when the buffer
1324 * becomes empty if stdin_eof is set. 1324 * becomes empty if stdin_eof is set.
1325 */ 1325 */
1326 if (buffer_len(&stdin_buffer) == 0) { 1326 if (buffer_len(&stdin_buffer) == 0) {
1327 packet_start(SSH_CMSG_EOF); 1327 packet_start(SSH_CMSG_EOF);
1328 packet_send(); 1328 packet_send();
1329 } 1329 }
1330 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) { 1330 } else if (escape_char1 == SSH_ESCAPECHAR_NONE) {
1331 /* 1331 /*
1332 * Normal successful read, and no escape character. 1332 * Normal successful read, and no escape character.
1333 * Just append the data to buffer. 1333 * Just append the data to buffer.
1334 */ 1334 */
1335 buffer_append(&stdin_buffer, buf, len); 1335 buffer_append(&stdin_buffer, buf, len);
1336 } else { 1336 } else {
1337 /* 1337 /*
1338 * Normal, successful read. But we have an escape 1338 * Normal, successful read. But we have an escape
1339 * character and have to process the characters one 1339 * character and have to process the characters one
1340 * by one. 1340 * by one.
1341 */ 1341 */
1342 if (process_escapes(NULL, &stdin_buffer, 1342 if (process_escapes(NULL, &stdin_buffer,
1343 &stdout_buffer, &stderr_buffer, buf, len) == -1) 1343 &stdout_buffer, &stderr_buffer, buf, len) == -1)
1344 return; 1344 return;
1345 } 1345 }
1346} 1346}
1347 1347
1348static void 1348static void
1349client_process_output(fd_set *writeset) 1349client_process_output(fd_set *writeset)
1350{ 1350{
1351 int len, fd; 1351 int len, fd;
1352 char buf[100]; 1352 char buf[100];
1353 1353
1354 /* Write buffered output to stdout. */ 1354 /* Write buffered output to stdout. */
1355 if ((fd = fileno(stdout)) != -1 && FD_ISSET(fd, writeset)) { 1355 if ((fd = fileno(stdout)) != -1 && FD_ISSET(fd, writeset)) {
1356 /* Write as much data as possible. */ 1356 /* Write as much data as possible. */
1357 len = write(fd, buffer_ptr(&stdout_buffer), 1357 len = write(fd, buffer_ptr(&stdout_buffer),
1358 buffer_len(&stdout_buffer)); 1358 buffer_len(&stdout_buffer));
1359 if (len <= 0) { 1359 if (len <= 0) {
1360 if (errno == EINTR || errno == EAGAIN) 1360 if (errno == EINTR || errno == EAGAIN)
1361 len = 0; 1361 len = 0;
1362 else { 1362 else {
1363 /* 1363 /*
1364 * An error or EOF was encountered. Put an 1364 * An error or EOF was encountered. Put an
1365 * error message to stderr buffer. 1365 * error message to stderr buffer.
1366 */ 1366 */
1367 snprintf(buf, sizeof buf, 1367 snprintf(buf, sizeof buf,
1368 "write stdout: %.50s\r\n", strerror(errno)); 1368 "write stdout: %.50s\r\n", strerror(errno));
1369 buffer_append(&stderr_buffer, buf, strlen(buf)); 1369 buffer_append(&stderr_buffer, buf, strlen(buf));
1370 quit_pending = 1; 1370 quit_pending = 1;
1371 return; 1371 return;
1372 } 1372 }
1373 } 1373 }
1374 /* Consume printed data from the buffer. */ 1374 /* Consume printed data from the buffer. */
1375 buffer_consume(&stdout_buffer, len); 1375 buffer_consume(&stdout_buffer, len);
1376 } 1376 }
1377 /* Write buffered output to stderr. */ 1377 /* Write buffered output to stderr. */
1378 if ((fd = fileno(stderr)) != -1 && FD_ISSET(fd, writeset)) { 1378 if ((fd = fileno(stderr)) != -1 && FD_ISSET(fd, writeset)) {
1379 /* Write as much data as possible. */ 1379 /* Write as much data as possible. */
1380 len = write(fd, buffer_ptr(&stderr_buffer), 1380 len = write(fd, buffer_ptr(&stderr_buffer),
1381 buffer_len(&stderr_buffer)); 1381 buffer_len(&stderr_buffer));
1382 if (len <= 0) { 1382 if (len <= 0) {
1383 if (errno == EINTR || errno == EAGAIN) 1383 if (errno == EINTR || errno == EAGAIN)
1384 len = 0; 1384 len = 0;
1385 else { 1385 else {
1386 /* 1386 /*
1387 * EOF or error, but can't even print 1387 * EOF or error, but can't even print
1388 * error message. 1388 * error message.
1389 */ 1389 */
1390 quit_pending = 1; 1390 quit_pending = 1;
1391 return; 1391 return;
1392 } 1392 }
1393 } 1393 }
1394 /* Consume printed characters from the buffer. */ 1394 /* Consume printed characters from the buffer. */
1395 buffer_consume(&stderr_buffer, len); 1395 buffer_consume(&stderr_buffer, len);
1396 } 1396 }
1397} 1397}
1398 1398
1399/* 1399/*
1400 * Get packets from the connection input buffer, and process them as long as 1400 * Get packets from the connection input buffer, and process them as long as
1401 * there are packets available. 1401 * there are packets available.
1402 * 1402 *
1403 * Any unknown packets received during the actual 1403 * Any unknown packets received during the actual
1404 * session cause the session to terminate. This is 1404 * session cause the session to terminate. This is
1405 * intended to make debugging easier since no 1405 * intended to make debugging easier since no
1406 * confirmations are sent. Any compatible protocol 1406 * confirmations are sent. Any compatible protocol
1407 * extensions must be negotiated during the 1407 * extensions must be negotiated during the
1408 * preparatory phase. 1408 * preparatory phase.
1409 */ 1409 */
1410 1410
1411static void 1411static void
1412client_process_buffered_input_packets(void) 1412client_process_buffered_input_packets(void)
1413{ 1413{
1414 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state); 1414 dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state);
1415} 1415}
1416 1416
1417/* scan buf[] for '~' before sending data to the peer */ 1417/* scan buf[] for '~' before sending data to the peer */
1418 1418
1419/* Helper: allocate a new escape_filter_ctx and fill in its escape char */ 1419/* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1420void * 1420void *
1421client_new_escape_filter_ctx(int escape_char) 1421client_new_escape_filter_ctx(int escape_char)
1422{ 1422{
1423 struct escape_filter_ctx *ret; 1423 struct escape_filter_ctx *ret;
1424 1424
1425 ret = xcalloc(1, sizeof(*ret)); 1425 ret = xcalloc(1, sizeof(*ret));
1426 ret->escape_pending = 0; 1426 ret->escape_pending = 0;
1427 ret->escape_char = escape_char; 1427 ret->escape_char = escape_char;
1428 return (void *)ret; 1428 return (void *)ret;
1429} 1429}
1430 1430
1431/* Free the escape filter context on channel free */ 1431/* Free the escape filter context on channel free */
1432void 1432void
1433client_filter_cleanup(int cid, void *ctx) 1433client_filter_cleanup(int cid, void *ctx)
1434{ 1434{
1435 free(ctx); 1435 free(ctx);
1436} 1436}
1437 1437
1438int 1438int
1439client_simple_escape_filter(Channel *c, const char *buf, int len) 1439client_simple_escape_filter(Channel *c, const char *buf, int len)
1440{ 1440{
1441 if (c->extended_usage != CHAN_EXTENDED_WRITE) 1441 if (c->extended_usage != CHAN_EXTENDED_WRITE)
1442 return 0; 1442 return 0;
1443 1443
1444 return process_escapes(c, &c->input, &c->output, &c->extended, 1444 return process_escapes(c, &c->input, &c->output, &c->extended,
1445 buf, len); 1445 buf, len);
1446} 1446}
1447 1447
1448static void 1448static void
1449client_channel_closed(int id, void *arg) 1449client_channel_closed(int id, void *arg)
1450{ 1450{
1451 channel_cancel_cleanup(id); 1451 channel_cancel_cleanup(id);
1452 session_closed = 1; 1452 session_closed = 1;
1453 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 1453 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1454} 1454}
1455 1455
1456/* 1456/*
1457 * Implements the interactive session with the server. This is called after 1457 * Implements the interactive session with the server. This is called after
1458 * the user has been authenticated, and a command has been started on the 1458 * the user has been authenticated, and a command has been started on the
1459 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character 1459 * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1460 * used as an escape character for terminating or suspending the session. 1460 * used as an escape character for terminating or suspending the session.
1461 */ 1461 */
1462 1462
1463int 1463int
1464client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id) 1464client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1465{ 1465{
1466 fd_set *readset = NULL, *writeset = NULL; 1466 fd_set *readset = NULL, *writeset = NULL;
1467 double start_time, total_time; 1467 double start_time, total_time;
1468 int r, max_fd = 0, max_fd2 = 0, len, rekeying = 0; 1468 int r, max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1469 u_int64_t ibytes, obytes; 1469 u_int64_t ibytes, obytes;
1470 u_int nalloc = 0; 1470 u_int nalloc = 0;
1471 char buf[100]; 1471 char buf[100];
1472 1472
1473 debug("Entering interactive session."); 1473 debug("Entering interactive session.");
1474 1474
1475 start_time = get_current_time(); 1475 start_time = get_current_time();
1476 1476
1477 /* Initialize variables. */ 1477 /* Initialize variables. */
1478 escape_pending1 = 0; 1478 escape_pending1 = 0;
1479 last_was_cr = 1; 1479 last_was_cr = 1;
1480 exit_status = -1; 1480 exit_status = -1;
1481 stdin_eof = 0; 1481 stdin_eof = 0;
1482 buffer_high = 64 * 1024; 1482 buffer_high = 64 * 1024;
1483 connection_in = packet_get_connection_in(); 1483 connection_in = packet_get_connection_in();
1484 connection_out = packet_get_connection_out(); 1484 connection_out = packet_get_connection_out();
1485 max_fd = MAX(connection_in, connection_out); 1485 max_fd = MAX(connection_in, connection_out);
1486 1486
1487 if (!compat20) { 1487 if (!compat20) {
1488 /* enable nonblocking unless tty */ 1488 /* enable nonblocking unless tty */
1489 if (!isatty(fileno(stdin))) 1489 if (!isatty(fileno(stdin)))
1490 set_nonblock(fileno(stdin)); 1490 set_nonblock(fileno(stdin));
1491 if (!isatty(fileno(stdout))) 1491 if (!isatty(fileno(stdout)))
1492 set_nonblock(fileno(stdout)); 1492 set_nonblock(fileno(stdout));
1493 if (!isatty(fileno(stderr))) 1493 if (!isatty(fileno(stderr)))
1494 set_nonblock(fileno(stderr)); 1494 set_nonblock(fileno(stderr));
1495 max_fd = MAX(max_fd, fileno(stdin)); 1495 max_fd = MAX(max_fd, fileno(stdin));
1496 max_fd = MAX(max_fd, fileno(stdout)); 1496 max_fd = MAX(max_fd, fileno(stdout));
1497 max_fd = MAX(max_fd, fileno(stderr)); 1497 max_fd = MAX(max_fd, fileno(stderr));
1498 } 1498 }
1499 quit_pending = 0; 1499 quit_pending = 0;
1500 escape_char1 = escape_char_arg; 1500 escape_char1 = escape_char_arg;
1501 1501
1502 /* Initialize buffers. */ 1502 /* Initialize buffers. */
1503 buffer_init(&stdin_buffer); 1503 buffer_init(&stdin_buffer);
1504 buffer_init(&stdout_buffer); 1504 buffer_init(&stdout_buffer);
1505 buffer_init(&stderr_buffer); 1505 buffer_init(&stderr_buffer);
1506 1506
1507 client_init_dispatch(); 1507 client_init_dispatch();
1508 1508
1509 /* 1509 /*
1510 * Set signal handlers, (e.g. to restore non-blocking mode) 1510 * Set signal handlers, (e.g. to restore non-blocking mode)
1511 * but don't overwrite SIG_IGN, matches behaviour from rsh(1) 1511 * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1512 */ 1512 */
1513 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 1513 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1514 signal(SIGHUP, signal_handler); 1514 signal(SIGHUP, signal_handler);
1515 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 1515 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1516 signal(SIGINT, signal_handler); 1516 signal(SIGINT, signal_handler);
1517 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) 1517 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1518 signal(SIGQUIT, signal_handler); 1518 signal(SIGQUIT, signal_handler);
1519 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 1519 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1520 signal(SIGTERM, signal_handler); 1520 signal(SIGTERM, signal_handler);
1521 signal(SIGWINCH, window_change_handler); 1521 signal(SIGWINCH, window_change_handler);
1522 1522
1523 if (have_pty) 1523 if (have_pty)
1524 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 1524 enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1525 1525
1526 if (compat20) { 1526 if (compat20) {
1527 session_ident = ssh2_chan_id; 1527 session_ident = ssh2_chan_id;
1528 if (session_ident != -1) { 1528 if (session_ident != -1) {
1529 if (escape_char_arg != SSH_ESCAPECHAR_NONE) { 1529 if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1530 channel_register_filter(session_ident, 1530 channel_register_filter(session_ident,
1531 client_simple_escape_filter, NULL, 1531 client_simple_escape_filter, NULL,
1532 client_filter_cleanup, 1532 client_filter_cleanup,
1533 client_new_escape_filter_ctx( 1533 client_new_escape_filter_ctx(
1534 escape_char_arg)); 1534 escape_char_arg));
1535 } 1535 }
1536 channel_register_cleanup(session_ident, 1536 channel_register_cleanup(session_ident,
1537 client_channel_closed, 0); 1537 client_channel_closed, 0);
1538 } 1538 }
1539 } else { 1539 } else {
1540 /* Check if we should immediately send eof on stdin. */ 1540 /* Check if we should immediately send eof on stdin. */
1541 client_check_initial_eof_on_stdin(); 1541 client_check_initial_eof_on_stdin();
1542 } 1542 }
1543 1543
1544 /* Main loop of the client for the interactive session mode. */ 1544 /* Main loop of the client for the interactive session mode. */
1545 while (!quit_pending) { 1545 while (!quit_pending) {
1546 1546
1547 /* Process buffered packets sent by the server. */ 1547 /* Process buffered packets sent by the server. */
1548 client_process_buffered_input_packets(); 1548 client_process_buffered_input_packets();
1549 1549
1550 if (compat20 && session_closed && !channel_still_open()) 1550 if (compat20 && session_closed && !channel_still_open())
1551 break; 1551 break;
1552 1552
1553 rekeying = (active_state->kex != NULL && !active_state->kex->done); 1553 rekeying = (active_state->kex != NULL && !active_state->kex->done);
1554 1554
1555 if (rekeying) { 1555 if (rekeying) {
1556 debug("rekeying in progress"); 1556 debug("rekeying in progress");
1557 } else { 1557 } else {
1558 /* 1558 /*
1559 * Make packets of buffered stdin data, and buffer 1559 * Make packets of buffered stdin data, and buffer
1560 * them for sending to the server. 1560 * them for sending to the server.
1561 */ 1561 */
1562 if (!compat20) 1562 if (!compat20)
1563 client_make_packets_from_stdin_data(); 1563 client_make_packets_from_stdin_data();
1564 1564
1565 /* 1565 /*
1566 * Make packets from buffered channel data, and 1566 * Make packets from buffered channel data, and
1567 * enqueue them for sending to the server. 1567 * enqueue them for sending to the server.
1568 */ 1568 */
1569 if (packet_not_very_much_data_to_write()) 1569 if (packet_not_very_much_data_to_write())
1570 channel_output_poll(); 1570 channel_output_poll();
1571 1571
1572 /* 1572 /*
1573 * Check if the window size has changed, and buffer a 1573 * Check if the window size has changed, and buffer a
1574 * message about it to the server if so. 1574 * message about it to the server if so.
1575 */ 1575 */
1576 client_check_window_change(); 1576 client_check_window_change();
1577 1577
1578 if (quit_pending) 1578 if (quit_pending)
1579 break; 1579 break;
1580 } 1580 }
1581 /* 1581 /*
1582 * Wait until we have something to do (something becomes 1582 * Wait until we have something to do (something becomes
1583 * available on one of the descriptors). 1583 * available on one of the descriptors).
1584 */ 1584 */
1585 max_fd2 = max_fd; 1585 max_fd2 = max_fd;
1586 client_wait_until_can_do_something(&readset, &writeset, 1586 client_wait_until_can_do_something(&readset, &writeset,
1587 &max_fd2, &nalloc, rekeying); 1587 &max_fd2, &nalloc, rekeying);
1588 1588
1589 if (quit_pending) 1589 if (quit_pending)
1590 break; 1590 break;
1591 1591
1592 /* Do channel operations unless rekeying in progress. */ 1592 /* Do channel operations unless rekeying in progress. */
1593 if (!rekeying) { 1593 if (!rekeying) {
1594 channel_after_select(readset, writeset); 1594 channel_after_select(readset, writeset);
1595 if (need_rekeying || packet_need_rekeying()) { 1595 if (need_rekeying || packet_need_rekeying()) {
1596 debug("need rekeying"); 1596 debug("need rekeying");
1597 active_state->kex->done = 0; 1597 if (active_state->kex != NULL)
 1598 active_state->kex->done = 0;
1598 if ((r = kex_send_kexinit(active_state)) != 0) 1599 if ((r = kex_send_kexinit(active_state)) != 0)
1599 fatal("%s: kex_send_kexinit: %s", 1600 fatal("%s: kex_send_kexinit: %s",
1600 __func__, ssh_err(r)); 1601 __func__, ssh_err(r));
1601 need_rekeying = 0; 1602 need_rekeying = 0;
1602 } 1603 }
1603 } 1604 }
1604 1605
1605 /* Buffer input from the connection. */ 1606 /* Buffer input from the connection. */
1606 client_process_net_input(readset); 1607 client_process_net_input(readset);
1607 1608
1608 if (quit_pending) 1609 if (quit_pending)
1609 break; 1610 break;
1610 1611
1611 if (!compat20) { 1612 if (!compat20) {
1612 /* Buffer data from stdin */ 1613 /* Buffer data from stdin */
1613 client_process_input(readset); 1614 client_process_input(readset);
1614 /* 1615 /*
1615 * Process output to stdout and stderr. Output to 1616 * Process output to stdout and stderr. Output to
1616 * the connection is processed elsewhere (above). 1617 * the connection is processed elsewhere (above).
1617 */ 1618 */
1618 client_process_output(writeset); 1619 client_process_output(writeset);
1619 } 1620 }
1620 1621
1621 if (session_resumed) { 1622 if (session_resumed) {
1622 connection_in = packet_get_connection_in(); 1623 connection_in = packet_get_connection_in();
1623 connection_out = packet_get_connection_out(); 1624 connection_out = packet_get_connection_out();
1624 max_fd = MAX(max_fd, connection_out); 1625 max_fd = MAX(max_fd, connection_out);
1625 max_fd = MAX(max_fd, connection_in); 1626 max_fd = MAX(max_fd, connection_in);
1626 session_resumed = 0; 1627 session_resumed = 0;
1627 } 1628 }
1628 1629
1629 /* 1630 /*
1630 * Send as much buffered packet data as possible to the 1631 * Send as much buffered packet data as possible to the
1631 * sender. 1632 * sender.
1632 */ 1633 */
1633 if (FD_ISSET(connection_out, writeset)) 1634 if (FD_ISSET(connection_out, writeset))
1634 packet_write_poll(); 1635 packet_write_poll();
1635 1636
1636 /* 1637 /*
1637 * If we are a backgrounded control master, and the 1638 * If we are a backgrounded control master, and the
1638 * timeout has expired without any active client 1639 * timeout has expired without any active client
1639 * connections, then quit. 1640 * connections, then quit.
1640 */ 1641 */
1641 if (control_persist_exit_time > 0) { 1642 if (control_persist_exit_time > 0) {
1642 if (monotime() >= control_persist_exit_time) { 1643 if (monotime() >= control_persist_exit_time) {
1643 debug("ControlPersist timeout expired"); 1644 debug("ControlPersist timeout expired");
1644 break; 1645 break;
1645 } 1646 }
1646 } 1647 }
1647 } 1648 }
1648 free(readset); 1649 free(readset);
1649 free(writeset); 1650 free(writeset);
1650 1651
1651 /* Terminate the session. */ 1652 /* Terminate the session. */
1652 1653
1653 /* Stop watching for window change. */ 1654 /* Stop watching for window change. */
1654 signal(SIGWINCH, SIG_DFL); 1655 signal(SIGWINCH, SIG_DFL);
1655 1656
1656 if (compat20) { 1657 if (compat20) {
1657 packet_start(SSH2_MSG_DISCONNECT); 1658 packet_start(SSH2_MSG_DISCONNECT);
1658 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION); 1659 packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
1659 packet_put_cstring("disconnected by user"); 1660 packet_put_cstring("disconnected by user");
1660 packet_put_cstring(""); /* language tag */ 1661 packet_put_cstring(""); /* language tag */
1661 packet_send(); 1662 packet_send();
1662 packet_write_wait(); 1663 packet_write_wait();
1663 } 1664 }
1664 1665
1665 channel_free_all(); 1666 channel_free_all();
1666 1667
1667 if (have_pty) 1668 if (have_pty)
1668 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 1669 leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1669 1670
1670 /* restore blocking io */ 1671 /* restore blocking io */
1671 if (!isatty(fileno(stdin))) 1672 if (!isatty(fileno(stdin)))
1672 unset_nonblock(fileno(stdin)); 1673 unset_nonblock(fileno(stdin));
1673 if (!isatty(fileno(stdout))) 1674 if (!isatty(fileno(stdout)))
1674 unset_nonblock(fileno(stdout)); 1675 unset_nonblock(fileno(stdout));
1675 if (!isatty(fileno(stderr))) 1676 if (!isatty(fileno(stderr)))
1676 unset_nonblock(fileno(stderr)); 1677 unset_nonblock(fileno(stderr));
1677 1678
1678 /* 1679 /*
1679 * If there was no shell or command requested, there will be no remote 1680 * If there was no shell or command requested, there will be no remote
1680 * exit status to be returned. In that case, clear error code if the 1681 * exit status to be returned. In that case, clear error code if the
1681 * connection was deliberately terminated at this end. 1682 * connection was deliberately terminated at this end.
1682 */ 1683 */
1683 if (no_shell_flag && received_signal == SIGTERM) { 1684 if (no_shell_flag && received_signal == SIGTERM) {
1684 received_signal = 0; 1685 received_signal = 0;
1685 exit_status = 0; 1686 exit_status = 0;
1686 } 1687 }
1687 1688
1688 if (received_signal) 1689 if (received_signal)
1689 fatal("Killed by signal %d.", (int) received_signal); 1690 fatal("Killed by signal %d.", (int) received_signal);
1690 1691
1691 /* 1692 /*
1692 * In interactive mode (with pseudo tty) display a message indicating 1693 * In interactive mode (with pseudo tty) display a message indicating
1693 * that the connection has been closed. 1694 * that the connection has been closed.
1694 */ 1695 */
1695 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { 1696 if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1696 snprintf(buf, sizeof buf, 1697 snprintf(buf, sizeof buf,
1697 "Connection to %.64s closed.\r\n", host); 1698 "Connection to %.64s closed.\r\n", host);
1698 buffer_append(&stderr_buffer, buf, strlen(buf)); 1699 buffer_append(&stderr_buffer, buf, strlen(buf));
1699 } 1700 }
1700 1701
1701 /* Output any buffered data for stdout. */ 1702 /* Output any buffered data for stdout. */
1702 if (buffer_len(&stdout_buffer) > 0) { 1703 if (buffer_len(&stdout_buffer) > 0) {
1703 len = atomicio(vwrite, fileno(stdout), 1704 len = atomicio(vwrite, fileno(stdout),
1704 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer)); 1705 buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
1705 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer)) 1706 if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
1706 error("Write failed flushing stdout buffer."); 1707 error("Write failed flushing stdout buffer.");
1707 else 1708 else
1708 buffer_consume(&stdout_buffer, len); 1709 buffer_consume(&stdout_buffer, len);
1709 } 1710 }
1710 1711
1711 /* Output any buffered data for stderr. */ 1712 /* Output any buffered data for stderr. */
1712 if (buffer_len(&stderr_buffer) > 0) { 1713 if (buffer_len(&stderr_buffer) > 0) {
1713 len = atomicio(vwrite, fileno(stderr), 1714 len = atomicio(vwrite, fileno(stderr),
1714 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer)); 1715 buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
1715 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer)) 1716 if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
1716 error("Write failed flushing stderr buffer."); 1717 error("Write failed flushing stderr buffer.");
1717 else 1718 else
1718 buffer_consume(&stderr_buffer, len); 1719 buffer_consume(&stderr_buffer, len);
1719 } 1720 }
1720 1721
1721 /* Clear and free any buffers. */ 1722 /* Clear and free any buffers. */
1722 memset(buf, 0, sizeof(buf)); 1723 memset(buf, 0, sizeof(buf));
1723 buffer_free(&stdin_buffer); 1724 buffer_free(&stdin_buffer);
1724 buffer_free(&stdout_buffer); 1725 buffer_free(&stdout_buffer);
1725 buffer_free(&stderr_buffer); 1726 buffer_free(&stderr_buffer);
1726 1727
1727 /* Report bytes transferred, and transfer rates. */ 1728 /* Report bytes transferred, and transfer rates. */
1728 total_time = get_current_time() - start_time; 1729 total_time = get_current_time() - start_time;
1729 packet_get_bytes(&ibytes, &obytes); 1730 packet_get_bytes(&ibytes, &obytes);
1730 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds", 1731 verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1731 (unsigned long long)obytes, (unsigned long long)ibytes, total_time); 1732 (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
1732 if (total_time > 0) 1733 if (total_time > 0)
1733 verbose("Bytes per second: sent %.1f, received %.1f", 1734 verbose("Bytes per second: sent %.1f, received %.1f",
1734 obytes / total_time, ibytes / total_time); 1735 obytes / total_time, ibytes / total_time);
1735 /* Return the exit status of the program. */ 1736 /* Return the exit status of the program. */
1736 debug("Exit status %d", exit_status); 1737 debug("Exit status %d", exit_status);
1737 return exit_status; 1738 return exit_status;
1738} 1739}
1739 1740
1740/*********/ 1741/*********/
1741 1742
1742static int 1743static int
1743client_input_stdout_data(int type, u_int32_t seq, void *ctxt) 1744client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
1744{ 1745{
1745 u_int data_len; 1746 u_int data_len;
1746 char *data = packet_get_string(&data_len); 1747 char *data = packet_get_string(&data_len);
1747 packet_check_eom(); 1748 packet_check_eom();
1748 buffer_append(&stdout_buffer, data, data_len); 1749 buffer_append(&stdout_buffer, data, data_len);
1749 explicit_bzero(data, data_len); 1750 explicit_bzero(data, data_len);
1750 free(data); 1751 free(data);
1751 return 0; 1752 return 0;
1752} 1753}
1753static int 1754static int
1754client_input_stderr_data(int type, u_int32_t seq, void *ctxt) 1755client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
1755{ 1756{
1756 u_int data_len; 1757 u_int data_len;
1757 char *data = packet_get_string(&data_len); 1758 char *data = packet_get_string(&data_len);
1758 packet_check_eom(); 1759 packet_check_eom();
1759 buffer_append(&stderr_buffer, data, data_len); 1760 buffer_append(&stderr_buffer, data, data_len);
1760 explicit_bzero(data, data_len); 1761 explicit_bzero(data, data_len);
1761 free(data); 1762 free(data);
1762 return 0; 1763 return 0;
1763} 1764}
1764static int 1765static int
1765client_input_exit_status(int type, u_int32_t seq, void *ctxt) 1766client_input_exit_status(int type, u_int32_t seq, void *ctxt)
1766{ 1767{
1767 exit_status = packet_get_int(); 1768 exit_status = packet_get_int();
1768 packet_check_eom(); 1769 packet_check_eom();
1769 /* Acknowledge the exit. */ 1770 /* Acknowledge the exit. */
1770 packet_start(SSH_CMSG_EXIT_CONFIRMATION); 1771 packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1771 packet_send(); 1772 packet_send();
1772 /* 1773 /*
1773 * Must wait for packet to be sent since we are 1774 * Must wait for packet to be sent since we are
1774 * exiting the loop. 1775 * exiting the loop.
1775 */ 1776 */
1776 packet_write_wait(); 1777 packet_write_wait();
1777 /* Flag that we want to exit. */ 1778 /* Flag that we want to exit. */
1778 quit_pending = 1; 1779 quit_pending = 1;
1779 return 0; 1780 return 0;
1780} 1781}
1781 1782
1782static int 1783static int
1783client_input_agent_open(int type, u_int32_t seq, void *ctxt) 1784client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1784{ 1785{
1785 Channel *c = NULL; 1786 Channel *c = NULL;
1786 int r, remote_id, sock; 1787 int r, remote_id, sock;
1787 1788
1788 /* Read the remote channel number from the message. */ 1789 /* Read the remote channel number from the message. */
1789 remote_id = packet_get_int(); 1790 remote_id = packet_get_int();
1790 packet_check_eom(); 1791 packet_check_eom();
1791 1792
1792 /* 1793 /*
1793 * Get a connection to the local authentication agent (this may again 1794 * Get a connection to the local authentication agent (this may again
1794 * get forwarded). 1795 * get forwarded).
1795 */ 1796 */
1796 if ((r = ssh_get_authentication_socket(&sock)) != 0 && 1797 if ((r = ssh_get_authentication_socket(&sock)) != 0 &&
1797 r != SSH_ERR_AGENT_NOT_PRESENT) 1798 r != SSH_ERR_AGENT_NOT_PRESENT)
1798 debug("%s: ssh_get_authentication_socket: %s", 1799 debug("%s: ssh_get_authentication_socket: %s",
1799 __func__, ssh_err(r)); 1800 __func__, ssh_err(r));
1800 1801
1801 1802
1802 /* 1803 /*
1803 * If we could not connect the agent, send an error message back to 1804 * If we could not connect the agent, send an error message back to
1804 * the server. This should never happen unless the agent dies, 1805 * the server. This should never happen unless the agent dies,
1805 * because authentication forwarding is only enabled if we have an 1806 * because authentication forwarding is only enabled if we have an
1806 * agent. 1807 * agent.
1807 */ 1808 */
1808 if (sock >= 0) { 1809 if (sock >= 0) {
1809 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, 1810 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1810 -1, 0, 0, 0, "authentication agent connection", 1); 1811 -1, 0, 0, 0, "authentication agent connection", 1);
1811 c->remote_id = remote_id; 1812 c->remote_id = remote_id;
1812 c->force_drain = 1; 1813 c->force_drain = 1;
1813 } 1814 }
1814 if (c == NULL) { 1815 if (c == NULL) {
1815 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); 1816 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1816 packet_put_int(remote_id); 1817 packet_put_int(remote_id);
1817 } else { 1818 } else {
1818 /* Send a confirmation to the remote host. */ 1819 /* Send a confirmation to the remote host. */
1819 debug("Forwarding authentication connection."); 1820 debug("Forwarding authentication connection.");
1820 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1821 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1821 packet_put_int(remote_id); 1822 packet_put_int(remote_id);
1822 packet_put_int(c->self); 1823 packet_put_int(c->self);
1823 } 1824 }
1824 packet_send(); 1825 packet_send();
1825 return 0; 1826 return 0;
1826} 1827}
1827 1828
1828static Channel * 1829static Channel *
1829client_request_forwarded_tcpip(const char *request_type, int rchan) 1830client_request_forwarded_tcpip(const char *request_type, int rchan)
1830{ 1831{
1831 Channel *c = NULL; 1832 Channel *c = NULL;
1832 char *listen_address, *originator_address; 1833 char *listen_address, *originator_address;
1833 u_short listen_port, originator_port; 1834 u_short listen_port, originator_port;
1834 1835
1835 /* Get rest of the packet */ 1836 /* Get rest of the packet */
1836 listen_address = packet_get_string(NULL); 1837 listen_address = packet_get_string(NULL);
1837 listen_port = packet_get_int(); 1838 listen_port = packet_get_int();
1838 originator_address = packet_get_string(NULL); 1839 originator_address = packet_get_string(NULL);
1839 originator_port = packet_get_int(); 1840 originator_port = packet_get_int();
1840 packet_check_eom(); 1841 packet_check_eom();
1841 1842
1842 debug("%s: listen %s port %d, originator %s port %d", __func__, 1843 debug("%s: listen %s port %d, originator %s port %d", __func__,
1843 listen_address, listen_port, originator_address, originator_port); 1844 listen_address, listen_port, originator_address, originator_port);
1844 1845
1845 c = channel_connect_by_listen_address(listen_address, listen_port, 1846 c = channel_connect_by_listen_address(listen_address, listen_port,
1846 "forwarded-tcpip", originator_address); 1847 "forwarded-tcpip", originator_address);
1847 1848
1848 free(originator_address); 1849 free(originator_address);
1849 free(listen_address); 1850 free(listen_address);
1850 return c; 1851 return c;
1851} 1852}
1852 1853
1853static Channel * 1854static Channel *
1854client_request_forwarded_streamlocal(const char *request_type, int rchan) 1855client_request_forwarded_streamlocal(const char *request_type, int rchan)
1855{ 1856{
1856 Channel *c = NULL; 1857 Channel *c = NULL;
1857 char *listen_path; 1858 char *listen_path;
1858 1859
1859 /* Get the remote path. */ 1860 /* Get the remote path. */
1860 listen_path = packet_get_string(NULL); 1861 listen_path = packet_get_string(NULL);
1861 /* XXX: Skip reserved field for now. */ 1862 /* XXX: Skip reserved field for now. */
1862 if (packet_get_string_ptr(NULL) == NULL) 1863 if (packet_get_string_ptr(NULL) == NULL)
1863 fatal("%s: packet_get_string_ptr failed", __func__); 1864 fatal("%s: packet_get_string_ptr failed", __func__);
1864 packet_check_eom(); 1865 packet_check_eom();
1865 1866
1866 debug("%s: %s", __func__, listen_path); 1867 debug("%s: %s", __func__, listen_path);
1867 1868
1868 c = channel_connect_by_listen_path(listen_path, 1869 c = channel_connect_by_listen_path(listen_path,
1869 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal"); 1870 "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1870 free(listen_path); 1871 free(listen_path);
1871 return c; 1872 return c;
1872} 1873}
1873 1874
1874static Channel * 1875static Channel *
1875client_request_x11(const char *request_type, int rchan) 1876client_request_x11(const char *request_type, int rchan)
1876{ 1877{
1877 Channel *c = NULL; 1878 Channel *c = NULL;
1878 char *originator; 1879 char *originator;
1879 u_short originator_port; 1880 u_short originator_port;
1880 int sock; 1881 int sock;
1881 1882
1882 if (!options.forward_x11) { 1883 if (!options.forward_x11) {
1883 error("Warning: ssh server tried X11 forwarding."); 1884 error("Warning: ssh server tried X11 forwarding.");
1884 error("Warning: this is probably a break-in attempt by a " 1885 error("Warning: this is probably a break-in attempt by a "
1885 "malicious server."); 1886 "malicious server.");
1886 return NULL; 1887 return NULL;
1887 } 1888 }
1888 if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) { 1889 if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) {
1889 verbose("Rejected X11 connection after ForwardX11Timeout " 1890 verbose("Rejected X11 connection after ForwardX11Timeout "
1890 "expired"); 1891 "expired");
1891 return NULL; 1892 return NULL;
1892 } 1893 }
1893 originator = packet_get_string(NULL); 1894 originator = packet_get_string(NULL);
1894 if (datafellows & SSH_BUG_X11FWD) { 1895 if (datafellows & SSH_BUG_X11FWD) {
1895 debug2("buggy server: x11 request w/o originator_port"); 1896 debug2("buggy server: x11 request w/o originator_port");
1896 originator_port = 0; 1897 originator_port = 0;
1897 } else { 1898 } else {
1898 originator_port = packet_get_int(); 1899 originator_port = packet_get_int();
1899 } 1900 }
1900 packet_check_eom(); 1901 packet_check_eom();
1901 /* XXX check permission */ 1902 /* XXX check permission */
1902 debug("client_request_x11: request from %s %d", originator, 1903 debug("client_request_x11: request from %s %d", originator,
1903 originator_port); 1904 originator_port);
1904 free(originator); 1905 free(originator);
1905 sock = x11_connect_display(); 1906 sock = x11_connect_display();
1906 if (sock < 0) 1907 if (sock < 0)
1907 return NULL; 1908 return NULL;
1908 /* again is this really necessary for X11? */ 1909 /* again is this really necessary for X11? */
1909 if (options.hpn_disabled)  1910 if (options.hpn_disabled)
1910 c = channel_new("x11", 1911 c = channel_new("x11",
1911 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 1912 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1912 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1); 1913 CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1913 else  1914 else
1914 c = channel_new("x11", 1915 c = channel_new("x11",
1915 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 1916 SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1916 options.hpn_buffer_size, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1); 1917 options.hpn_buffer_size, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1917 c->force_drain = 1; 1918 c->force_drain = 1;
1918 return c; 1919 return c;
1919} 1920}
1920 1921
1921static Channel * 1922static Channel *
1922client_request_agent(const char *request_type, int rchan) 1923client_request_agent(const char *request_type, int rchan)
1923{ 1924{
1924 Channel *c = NULL; 1925 Channel *c = NULL;
1925 int r, sock; 1926 int r, sock;
1926 1927
1927 if (!options.forward_agent) { 1928 if (!options.forward_agent) {
1928 error("Warning: ssh server tried agent forwarding."); 1929 error("Warning: ssh server tried agent forwarding.");
1929 error("Warning: this is probably a break-in attempt by a " 1930 error("Warning: this is probably a break-in attempt by a "
1930 "malicious server."); 1931 "malicious server.");
1931 return NULL; 1932 return NULL;
1932 } 1933 }
1933 if ((r = ssh_get_authentication_socket(&sock)) != 0) { 1934 if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1934 if (r != SSH_ERR_AGENT_NOT_PRESENT) 1935 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1935 debug("%s: ssh_get_authentication_socket: %s", 1936 debug("%s: ssh_get_authentication_socket: %s",
1936 __func__, ssh_err(r)); 1937 __func__, ssh_err(r));
1937 return NULL; 1938 return NULL;
1938 } 1939 }
1939 if (options.hpn_disabled)  1940 if (options.hpn_disabled)
1940 c = channel_new("authentication agent connection", 1941 c = channel_new("authentication agent connection",
1941 SSH_CHANNEL_OPEN, sock, sock, -1, 1942 SSH_CHANNEL_OPEN, sock, sock, -1,
1942 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, 1943 CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
1943 "authentication agent connection", 1); 1944 "authentication agent connection", 1);
1944 else 1945 else
1945 c = channel_new("authentication agent connection", 1946 c = channel_new("authentication agent connection",
1946 SSH_CHANNEL_OPEN, sock, sock, -1, 1947 SSH_CHANNEL_OPEN, sock, sock, -1,
1947 options.hpn_buffer_size, options.hpn_buffer_size, 0, 1948 options.hpn_buffer_size, options.hpn_buffer_size, 0,
1948 "authentication agent connection", 1); 1949 "authentication agent connection", 1);
1949 c->force_drain = 1; 1950 c->force_drain = 1;
1950 return c; 1951 return c;
1951} 1952}
1952 1953
1953int 1954int
1954client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun) 1955client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1955{ 1956{
1956 Channel *c; 1957 Channel *c;
1957 int fd; 1958 int fd;
1958 1959
1959 if (tun_mode == SSH_TUNMODE_NO) 1960 if (tun_mode == SSH_TUNMODE_NO)
1960 return 0; 1961 return 0;
1961 1962
1962 if (!compat20) { 1963 if (!compat20) {
1963 error("Tunnel forwarding is not supported for protocol 1"); 1964 error("Tunnel forwarding is not supported for protocol 1");
1964 return -1; 1965 return -1;
1965 } 1966 }
1966 1967
1967 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode); 1968 debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1968 1969
1969 /* Open local tunnel device */ 1970 /* Open local tunnel device */
1970 if ((fd = tun_open(local_tun, tun_mode)) == -1) { 1971 if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1971 error("Tunnel device open failed."); 1972 error("Tunnel device open failed.");
1972 return -1; 1973 return -1;
1973 } 1974 }
1974 1975
1975 if(options.hpn_disabled) 1976 if(options.hpn_disabled)
1976 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1, 1977 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1977 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 1978 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1978 else 1979 else
1979 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1, 1980 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1980 options.hpn_buffer_size, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 1981 options.hpn_buffer_size, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1981 c->datagram = 1; 1982 c->datagram = 1;
1982 1983
1983 packet_start(SSH2_MSG_CHANNEL_OPEN); 1984 packet_start(SSH2_MSG_CHANNEL_OPEN);
1984 packet_put_cstring("tun@openssh.com"); 1985 packet_put_cstring("tun@openssh.com");
1985 packet_put_int(c->self); 1986 packet_put_int(c->self);
1986 packet_put_int(c->local_window_max); 1987 packet_put_int(c->local_window_max);
1987 packet_put_int(c->local_maxpacket); 1988 packet_put_int(c->local_maxpacket);
1988 packet_put_int(tun_mode); 1989 packet_put_int(tun_mode);
1989 packet_put_int(remote_tun); 1990 packet_put_int(remote_tun);
1990 packet_send(); 1991 packet_send();
1991 1992
1992 return 0; 1993 return 0;
1993} 1994}
1994 1995
1995/* XXXX move to generic input handler */ 1996/* XXXX move to generic input handler */
1996static int 1997static int
1997client_input_channel_open(int type, u_int32_t seq, void *ctxt) 1998client_input_channel_open(int type, u_int32_t seq, void *ctxt)
1998{ 1999{
1999 Channel *c = NULL; 2000 Channel *c = NULL;
2000 char *ctype; 2001 char *ctype;
2001 int rchan; 2002 int rchan;
2002 u_int rmaxpack, rwindow, len; 2003 u_int rmaxpack, rwindow, len;
2003 2004
2004 ctype = packet_get_string(&len); 2005 ctype = packet_get_string(&len);
2005 rchan = packet_get_int(); 2006 rchan = packet_get_int();
2006 rwindow = packet_get_int(); 2007 rwindow = packet_get_int();
2007 rmaxpack = packet_get_int(); 2008 rmaxpack = packet_get_int();
2008 2009
2009 debug("client_input_channel_open: ctype %s rchan %d win %d max %d", 2010 debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
2010 ctype, rchan, rwindow, rmaxpack); 2011 ctype, rchan, rwindow, rmaxpack);
2011 2012
2012 if (strcmp(ctype, "forwarded-tcpip") == 0) { 2013 if (strcmp(ctype, "forwarded-tcpip") == 0) {
2013 c = client_request_forwarded_tcpip(ctype, rchan); 2014 c = client_request_forwarded_tcpip(ctype, rchan);
2014 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) { 2015 } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
2015 c = client_request_forwarded_streamlocal(ctype, rchan); 2016 c = client_request_forwarded_streamlocal(ctype, rchan);
2016 } else if (strcmp(ctype, "x11") == 0) { 2017 } else if (strcmp(ctype, "x11") == 0) {
2017 c = client_request_x11(ctype, rchan); 2018 c = client_request_x11(ctype, rchan);
2018 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { 2019 } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
2019 c = client_request_agent(ctype, rchan); 2020 c = client_request_agent(ctype, rchan);
2020 } 2021 }
2021/* XXX duplicate : */ 2022/* XXX duplicate : */
2022 if (c != NULL) { 2023 if (c != NULL) {
2023 debug("confirm %s", ctype); 2024 debug("confirm %s", ctype);
2024 c->remote_id = rchan; 2025 c->remote_id = rchan;
2025 c->remote_window = rwindow; 2026 c->remote_window = rwindow;
2026 c->remote_maxpacket = rmaxpack; 2027 c->remote_maxpacket = rmaxpack;
2027 if (c->type != SSH_CHANNEL_CONNECTING) { 2028 if (c->type != SSH_CHANNEL_CONNECTING) {
2028 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); 2029 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
2029 packet_put_int(c->remote_id); 2030 packet_put_int(c->remote_id);
2030 packet_put_int(c->self); 2031 packet_put_int(c->self);
2031 packet_put_int(c->local_window); 2032 packet_put_int(c->local_window);
2032 packet_put_int(c->local_maxpacket); 2033 packet_put_int(c->local_maxpacket);
2033 packet_send(); 2034 packet_send();
2034 } 2035 }
2035 } else { 2036 } else {
2036 debug("failure %s", ctype); 2037 debug("failure %s", ctype);
2037 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); 2038 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
2038 packet_put_int(rchan); 2039 packet_put_int(rchan);
2039 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); 2040 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
2040 if (!(datafellows & SSH_BUG_OPENFAILURE)) { 2041 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
2041 packet_put_cstring("open failed"); 2042 packet_put_cstring("open failed");
2042 packet_put_cstring(""); 2043 packet_put_cstring("");
2043 } 2044 }
2044 packet_send(); 2045 packet_send();
2045 } 2046 }
2046 free(ctype); 2047 free(ctype);
2047 return 0; 2048 return 0;
2048} 2049}
2049 2050
2050static int 2051static int
2051client_input_channel_req(int type, u_int32_t seq, void *ctxt) 2052client_input_channel_req(int type, u_int32_t seq, void *ctxt)
2052{ 2053{
2053 Channel *c = NULL; 2054 Channel *c = NULL;
2054 int exitval, id, reply, success = 0; 2055 int exitval, id, reply, success = 0;
2055 char *rtype; 2056 char *rtype;
2056 2057
2057 id = packet_get_int(); 2058 id = packet_get_int();
2058 rtype = packet_get_string(NULL); 2059 rtype = packet_get_string(NULL);
2059 reply = packet_get_char(); 2060 reply = packet_get_char();
2060 2061
2061 debug("client_input_channel_req: channel %d rtype %s reply %d", 2062 debug("client_input_channel_req: channel %d rtype %s reply %d",
2062 id, rtype, reply); 2063 id, rtype, reply);
2063 2064
2064 if (id == -1) { 2065 if (id == -1) {
2065 error("client_input_channel_req: request for channel -1"); 2066 error("client_input_channel_req: request for channel -1");
2066 } else if ((c = channel_lookup(id)) == NULL) { 2067 } else if ((c = channel_lookup(id)) == NULL) {
2067 error("client_input_channel_req: channel %d: " 2068 error("client_input_channel_req: channel %d: "
2068 "unknown channel", id); 2069 "unknown channel", id);
2069 } else if (strcmp(rtype, "eow@openssh.com") == 0) { 2070 } else if (strcmp(rtype, "eow@openssh.com") == 0) {
2070 packet_check_eom(); 2071 packet_check_eom();
2071 chan_rcvd_eow(c); 2072 chan_rcvd_eow(c);
2072 } else if (strcmp(rtype, "exit-status") == 0) { 2073 } else if (strcmp(rtype, "exit-status") == 0) {
2073 exitval = packet_get_int(); 2074 exitval = packet_get_int();
2074 if (c->ctl_chan != -1) { 2075 if (c->ctl_chan != -1) {
2075 mux_exit_message(c, exitval); 2076 mux_exit_message(c, exitval);
2076 success = 1; 2077 success = 1;
2077 } else if (id == session_ident) { 2078 } else if (id == session_ident) {
2078 /* Record exit value of local session */ 2079 /* Record exit value of local session */
2079 success = 1; 2080 success = 1;
2080 exit_status = exitval; 2081 exit_status = exitval;
2081 } else { 2082 } else {
2082 /* Probably for a mux channel that has already closed */ 2083 /* Probably for a mux channel that has already closed */
2083 debug("%s: no sink for exit-status on channel %d", 2084 debug("%s: no sink for exit-status on channel %d",
2084 __func__, id); 2085 __func__, id);
2085 } 2086 }
2086 packet_check_eom(); 2087 packet_check_eom();
2087 } 2088 }
2088 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) { 2089 if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
2089 packet_start(success ? 2090 packet_start(success ?
2090 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); 2091 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
2091 packet_put_int(c->remote_id); 2092 packet_put_int(c->remote_id);
2092 packet_send(); 2093 packet_send();
2093 } 2094 }
2094 free(rtype); 2095 free(rtype);
2095 return 0; 2096 return 0;
2096} 2097}
2097 2098
2098struct hostkeys_update_ctx { 2099struct hostkeys_update_ctx {
2099 /* The hostname and (optionally) IP address string for the server */ 2100 /* The hostname and (optionally) IP address string for the server */
2100 char *host_str, *ip_str; 2101 char *host_str, *ip_str;
2101 2102
2102 /* 2103 /*
2103 * Keys received from the server and a flag for each indicating 2104 * Keys received from the server and a flag for each indicating
2104 * whether they already exist in known_hosts. 2105 * whether they already exist in known_hosts.
2105 * keys_seen is filled in by hostkeys_find() and later (for new 2106 * keys_seen is filled in by hostkeys_find() and later (for new
2106 * keys) by client_global_hostkeys_private_confirm(). 2107 * keys) by client_global_hostkeys_private_confirm().
2107 */ 2108 */
2108 struct sshkey **keys; 2109 struct sshkey **keys;
2109 int *keys_seen; 2110 int *keys_seen;
2110 size_t nkeys; 2111 size_t nkeys;
2111 2112
2112 size_t nnew; 2113 size_t nnew;
2113 2114
2114 /* 2115 /*
2115 * Keys that are in known_hosts, but were not present in the update 2116 * Keys that are in known_hosts, but were not present in the update
2116 * from the server (i.e. scheduled to be deleted). 2117 * from the server (i.e. scheduled to be deleted).
2117 * Filled in by hostkeys_find(). 2118 * Filled in by hostkeys_find().
2118 */ 2119 */
2119 struct sshkey **old_keys; 2120 struct sshkey **old_keys;
2120 size_t nold; 2121 size_t nold;
2121}; 2122};
2122 2123
2123static void 2124static void
2124hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx) 2125hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
2125{ 2126{
2126 size_t i; 2127 size_t i;
2127 2128
2128 if (ctx == NULL) 2129 if (ctx == NULL)
2129 return; 2130 return;
2130 for (i = 0; i < ctx->nkeys; i++) 2131 for (i = 0; i < ctx->nkeys; i++)
2131 sshkey_free(ctx->keys[i]); 2132 sshkey_free(ctx->keys[i]);
2132 free(ctx->keys); 2133 free(ctx->keys);
2133 free(ctx->keys_seen); 2134 free(ctx->keys_seen);
2134 for (i = 0; i < ctx->nold; i++) 2135 for (i = 0; i < ctx->nold; i++)
2135 sshkey_free(ctx->old_keys[i]); 2136 sshkey_free(ctx->old_keys[i]);
2136 free(ctx->old_keys); 2137 free(ctx->old_keys);
2137 free(ctx->host_str); 2138 free(ctx->host_str);
2138 free(ctx->ip_str); 2139 free(ctx->ip_str);
2139 free(ctx); 2140 free(ctx);
2140} 2141}
2141 2142
2142static int 2143static int
2143hostkeys_find(struct hostkey_foreach_line *l, void *_ctx) 2144hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
2144{ 2145{
2145 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; 2146 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2146 size_t i; 2147 size_t i;
2147 struct sshkey **tmp; 2148 struct sshkey **tmp;
2148 2149
2149 if (l->status != HKF_STATUS_MATCHED || l->key == NULL || 2150 if (l->status != HKF_STATUS_MATCHED || l->key == NULL ||
2150 l->key->type == KEY_RSA1) 2151 l->key->type == KEY_RSA1)
2151 return 0; 2152 return 0;
2152 2153
2153 /* Mark off keys we've already seen for this host */ 2154 /* Mark off keys we've already seen for this host */
2154 for (i = 0; i < ctx->nkeys; i++) { 2155 for (i = 0; i < ctx->nkeys; i++) {
2155 if (sshkey_equal(l->key, ctx->keys[i])) { 2156 if (sshkey_equal(l->key, ctx->keys[i])) {
2156 debug3("%s: found %s key at %s:%ld", __func__, 2157 debug3("%s: found %s key at %s:%ld", __func__,
2157 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum); 2158 sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
2158 ctx->keys_seen[i] = 1; 2159 ctx->keys_seen[i] = 1;
2159 return 0; 2160 return 0;
2160 } 2161 }
2161 } 2162 }
2162 /* This line contained a key that not offered by the server */ 2163 /* This line contained a key that not offered by the server */
2163 debug3("%s: deprecated %s key at %s:%ld", __func__, 2164 debug3("%s: deprecated %s key at %s:%ld", __func__,
2164 sshkey_ssh_name(l->key), l->path, l->linenum); 2165 sshkey_ssh_name(l->key), l->path, l->linenum);
2165 if ((tmp = reallocarray(ctx->old_keys, ctx->nold + 1, 2166 if ((tmp = reallocarray(ctx->old_keys, ctx->nold + 1,
2166 sizeof(*ctx->old_keys))) == NULL) 2167 sizeof(*ctx->old_keys))) == NULL)
2167 fatal("%s: reallocarray failed nold = %zu", 2168 fatal("%s: reallocarray failed nold = %zu",
2168 __func__, ctx->nold); 2169 __func__, ctx->nold);
2169 ctx->old_keys = tmp; 2170 ctx->old_keys = tmp;
2170 ctx->old_keys[ctx->nold++] = l->key; 2171 ctx->old_keys[ctx->nold++] = l->key;
2171 l->key = NULL; 2172 l->key = NULL;
2172 2173
2173 return 0; 2174 return 0;
2174} 2175}
2175 2176
2176static void 2177static void
2177update_known_hosts(struct hostkeys_update_ctx *ctx) 2178update_known_hosts(struct hostkeys_update_ctx *ctx)
2178{ 2179{
2179 int r, was_raw = 0; 2180 int r, was_raw = 0;
2180 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ? 2181 int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
2181 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE; 2182 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
2182 char *fp, *response; 2183 char *fp, *response;
2183 size_t i; 2184 size_t i;
2184 2185
2185 for (i = 0; i < ctx->nkeys; i++) { 2186 for (i = 0; i < ctx->nkeys; i++) {
2186 if (ctx->keys_seen[i] != 2) 2187 if (ctx->keys_seen[i] != 2)
2187 continue; 2188 continue;
2188 if ((fp = sshkey_fingerprint(ctx->keys[i], 2189 if ((fp = sshkey_fingerprint(ctx->keys[i],
2189 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 2190 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2190 fatal("%s: sshkey_fingerprint failed", __func__); 2191 fatal("%s: sshkey_fingerprint failed", __func__);
2191 do_log2(loglevel, "Learned new hostkey: %s %s", 2192 do_log2(loglevel, "Learned new hostkey: %s %s",
2192 sshkey_type(ctx->keys[i]), fp); 2193 sshkey_type(ctx->keys[i]), fp);
2193 free(fp); 2194 free(fp);
2194 } 2195 }
2195 for (i = 0; i < ctx->nold; i++) { 2196 for (i = 0; i < ctx->nold; i++) {
2196 if ((fp = sshkey_fingerprint(ctx->old_keys[i], 2197 if ((fp = sshkey_fingerprint(ctx->old_keys[i],
2197 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 2198 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
2198 fatal("%s: sshkey_fingerprint failed", __func__); 2199 fatal("%s: sshkey_fingerprint failed", __func__);
2199 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s", 2200 do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
2200 sshkey_type(ctx->old_keys[i]), fp); 2201 sshkey_type(ctx->old_keys[i]), fp);
2201 free(fp); 2202 free(fp);
2202 } 2203 }
2203 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) { 2204 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
2204 if (get_saved_tio() != NULL) { 2205 if (get_saved_tio() != NULL) {
2205 leave_raw_mode(1); 2206 leave_raw_mode(1);
2206 was_raw = 1; 2207 was_raw = 1;
2207 } 2208 }
2208 response = NULL; 2209 response = NULL;
2209 for (i = 0; !quit_pending && i < 3; i++) { 2210 for (i = 0; !quit_pending && i < 3; i++) {
2210 free(response); 2211 free(response);
2211 response = read_passphrase("Accept updated hostkeys? " 2212 response = read_passphrase("Accept updated hostkeys? "
2212 "(yes/no): ", RP_ECHO); 2213 "(yes/no): ", RP_ECHO);
2213 if (strcasecmp(response, "yes") == 0) 2214 if (strcasecmp(response, "yes") == 0)
2214 break; 2215 break;
2215 else if (quit_pending || response == NULL || 2216 else if (quit_pending || response == NULL ||
2216 strcasecmp(response, "no") == 0) { 2217 strcasecmp(response, "no") == 0) {
2217 options.update_hostkeys = 0; 2218 options.update_hostkeys = 0;
2218 break; 2219 break;
2219 } else { 2220 } else {
2220 do_log2(loglevel, "Please enter " 2221 do_log2(loglevel, "Please enter "
2221 "\"yes\" or \"no\""); 2222 "\"yes\" or \"no\"");
2222 } 2223 }
2223 } 2224 }
2224 if (quit_pending || i >= 3 || response == NULL) 2225 if (quit_pending || i >= 3 || response == NULL)
2225 options.update_hostkeys = 0; 2226 options.update_hostkeys = 0;
2226 free(response); 2227 free(response);
2227 if (was_raw) 2228 if (was_raw)
2228 enter_raw_mode(1); 2229 enter_raw_mode(1);
2229 } 2230 }
2230 2231
2231 /* 2232 /*
2232 * Now that all the keys are verified, we can go ahead and replace 2233 * Now that all the keys are verified, we can go ahead and replace
2233 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't 2234 * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
2234 * cancel the operation). 2235 * cancel the operation).
2235 */ 2236 */
2236 if (options.update_hostkeys != 0 && 2237 if (options.update_hostkeys != 0 &&
2237 (r = hostfile_replace_entries(options.user_hostfiles[0], 2238 (r = hostfile_replace_entries(options.user_hostfiles[0],
2238 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys, 2239 ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
2239 options.hash_known_hosts, 0, 2240 options.hash_known_hosts, 0,
2240 options.fingerprint_hash)) != 0) 2241 options.fingerprint_hash)) != 0)
2241 error("%s: hostfile_replace_entries failed: %s", 2242 error("%s: hostfile_replace_entries failed: %s",
2242 __func__, ssh_err(r)); 2243 __func__, ssh_err(r));
2243} 2244}
2244 2245
2245static void 2246static void
2246client_global_hostkeys_private_confirm(int type, u_int32_t seq, void *_ctx) 2247client_global_hostkeys_private_confirm(int type, u_int32_t seq, void *_ctx)
2247{ 2248{
2248 struct ssh *ssh = active_state; /* XXX */ 2249 struct ssh *ssh = active_state; /* XXX */
2249 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; 2250 struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2250 size_t i, ndone; 2251 size_t i, ndone;
2251 struct sshbuf *signdata; 2252 struct sshbuf *signdata;
2252 int r; 2253 int r;
2253 const u_char *sig; 2254 const u_char *sig;
2254 size_t siglen; 2255 size_t siglen;
2255 2256
2256 if (ctx->nnew == 0) 2257 if (ctx->nnew == 0)
2257 fatal("%s: ctx->nnew == 0", __func__); /* sanity */ 2258 fatal("%s: ctx->nnew == 0", __func__); /* sanity */
2258 if (type != SSH2_MSG_REQUEST_SUCCESS) { 2259 if (type != SSH2_MSG_REQUEST_SUCCESS) {
2259 error("Server failed to confirm ownership of " 2260 error("Server failed to confirm ownership of "
2260 "private host keys"); 2261 "private host keys");
2261 hostkeys_update_ctx_free(ctx); 2262 hostkeys_update_ctx_free(ctx);
2262 return; 2263 return;
2263 } 2264 }
2264 if ((signdata = sshbuf_new()) == NULL) 2265 if ((signdata = sshbuf_new()) == NULL)
2265 fatal("%s: sshbuf_new failed", __func__); 2266 fatal("%s: sshbuf_new failed", __func__);
2266 /* Don't want to accidentally accept an unbound signature */ 2267 /* Don't want to accidentally accept an unbound signature */
2267 if (ssh->kex->session_id_len == 0) 2268 if (ssh->kex->session_id_len == 0)
2268 fatal("%s: ssh->kex->session_id_len == 0", __func__); 2269 fatal("%s: ssh->kex->session_id_len == 0", __func__);
2269 /* 2270 /*
2270 * Expect a signature for each of the ctx->nnew private keys we 2271 * Expect a signature for each of the ctx->nnew private keys we
2271 * haven't seen before. They will be in the same order as the 2272 * haven't seen before. They will be in the same order as the
2272 * ctx->keys where the corresponding ctx->keys_seen[i] == 0. 2273 * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
2273 */ 2274 */
2274 for (ndone = i = 0; i < ctx->nkeys; i++) { 2275 for (ndone = i = 0; i < ctx->nkeys; i++) {
2275 if (ctx->keys_seen[i]) 2276 if (ctx->keys_seen[i])
2276 continue; 2277 continue;
2277 /* Prepare data to be signed: session ID, unique string, key */ 2278 /* Prepare data to be signed: session ID, unique string, key */
2278 sshbuf_reset(signdata); 2279 sshbuf_reset(signdata);
2279 if ( (r = sshbuf_put_cstring(signdata, 2280 if ( (r = sshbuf_put_cstring(signdata,
2280 "hostkeys-prove-00@openssh.com")) != 0 || 2281 "hostkeys-prove-00@openssh.com")) != 0 ||
2281 (r = sshbuf_put_string(signdata, ssh->kex->session_id, 2282 (r = sshbuf_put_string(signdata, ssh->kex->session_id,
2282 ssh->kex->session_id_len)) != 0 || 2283 ssh->kex->session_id_len)) != 0 ||
2283 (r = sshkey_puts(ctx->keys[i], signdata)) != 0) 2284 (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
2284 fatal("%s: failed to prepare signature: %s", 2285 fatal("%s: failed to prepare signature: %s",
2285 __func__, ssh_err(r)); 2286 __func__, ssh_err(r));
2286 /* Extract and verify signature */ 2287 /* Extract and verify signature */
2287 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) { 2288 if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
2288 error("%s: couldn't parse message: %s", 2289 error("%s: couldn't parse message: %s",
2289 __func__, ssh_err(r)); 2290 __func__, ssh_err(r));
2290 goto out; 2291 goto out;
2291 } 2292 }
2292 if ((r = sshkey_verify(ctx->keys[i], sig, siglen, 2293 if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
2293 sshbuf_ptr(signdata), sshbuf_len(signdata), 0)) != 0) { 2294 sshbuf_ptr(signdata), sshbuf_len(signdata), 0)) != 0) {
2294 error("%s: server gave bad signature for %s key %zu", 2295 error("%s: server gave bad signature for %s key %zu",
2295 __func__, sshkey_type(ctx->keys[i]), i); 2296 __func__, sshkey_type(ctx->keys[i]), i);
2296 goto out; 2297 goto out;
2297 } 2298 }
2298 /* Key is good. Mark it as 'seen' */ 2299 /* Key is good. Mark it as 'seen' */
2299 ctx->keys_seen[i] = 2; 2300 ctx->keys_seen[i] = 2;
2300 ndone++; 2301 ndone++;
2301 } 2302 }
2302 if (ndone != ctx->nnew) 2303 if (ndone != ctx->nnew)
2303 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__, 2304 fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
2304 ndone, ctx->nnew); /* Shouldn't happen */ 2305 ndone, ctx->nnew); /* Shouldn't happen */
2305 ssh_packet_check_eom(ssh); 2306 ssh_packet_check_eom(ssh);
2306 2307
2307 /* Make the edits to known_hosts */ 2308 /* Make the edits to known_hosts */
2308 update_known_hosts(ctx); 2309 update_known_hosts(ctx);
2309 out: 2310 out:
2310 hostkeys_update_ctx_free(ctx); 2311 hostkeys_update_ctx_free(ctx);
2311} 2312}
2312 2313
2313/* 2314/*
2314 * Handle hostkeys-00@openssh.com global request to inform the client of all 2315 * Handle hostkeys-00@openssh.com global request to inform the client of all
2315 * the server's hostkeys. The keys are checked against the user's 2316 * the server's hostkeys. The keys are checked against the user's
2316 * HostkeyAlgorithms preference before they are accepted. 2317 * HostkeyAlgorithms preference before they are accepted.
2317 */ 2318 */
2318static int 2319static int
2319client_input_hostkeys(void) 2320client_input_hostkeys(void)
2320{ 2321{
2321 struct ssh *ssh = active_state; /* XXX */ 2322 struct ssh *ssh = active_state; /* XXX */
2322 const u_char *blob = NULL; 2323 const u_char *blob = NULL;
2323 size_t i, len = 0; 2324 size_t i, len = 0;
2324 struct sshbuf *buf = NULL; 2325 struct sshbuf *buf = NULL;
2325 struct sshkey *key = NULL, **tmp; 2326 struct sshkey *key = NULL, **tmp;
2326 int r; 2327 int r;
2327 char *fp; 2328 char *fp;
2328 static int hostkeys_seen = 0; /* XXX use struct ssh */ 2329 static int hostkeys_seen = 0; /* XXX use struct ssh */
2329 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */ 2330 extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
2330 struct hostkeys_update_ctx *ctx = NULL; 2331 struct hostkeys_update_ctx *ctx = NULL;
2331 2332
2332 if (hostkeys_seen) 2333 if (hostkeys_seen)
2333 fatal("%s: server already sent hostkeys", __func__); 2334 fatal("%s: server already sent hostkeys", __func__);
2334 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK && 2335 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2335 options.batch_mode) 2336 options.batch_mode)
2336 return 1; /* won't ask in batchmode, so don't even try */ 2337 return 1; /* won't ask in batchmode, so don't even try */
2337 if (!options.update_hostkeys || options.num_user_hostfiles <= 0) 2338 if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2338 return 1; 2339 return 1;
2339 2340
2340 ctx = xcalloc(1, sizeof(*ctx)); 2341 ctx = xcalloc(1, sizeof(*ctx));
2341 while (ssh_packet_remaining(ssh) > 0) { 2342 while (ssh_packet_remaining(ssh) > 0) {
2342 sshkey_free(key); 2343 sshkey_free(key);
2343 key = NULL; 2344 key = NULL;
2344 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) { 2345 if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2345 error("%s: couldn't parse message: %s", 2346 error("%s: couldn't parse message: %s",
2346 __func__, ssh_err(r)); 2347 __func__, ssh_err(r));
2347 goto out; 2348 goto out;
2348 } 2349 }
2349 if ((r = sshkey_from_blob(blob, len, &key)) != 0) { 2350 if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2350 error("%s: parse key: %s", __func__, ssh_err(r)); 2351 error("%s: parse key: %s", __func__, ssh_err(r));
2351 goto out; 2352 goto out;
2352 } 2353 }
2353 fp = sshkey_fingerprint(key, options.fingerprint_hash, 2354 fp = sshkey_fingerprint(key, options.fingerprint_hash,
2354 SSH_FP_DEFAULT); 2355 SSH_FP_DEFAULT);
2355 debug3("%s: received %s key %s", __func__, 2356 debug3("%s: received %s key %s", __func__,
2356 sshkey_type(key), fp); 2357 sshkey_type(key), fp);
2357 free(fp); 2358 free(fp);
2358 /* Check that the key is accepted in HostkeyAlgorithms */ 2359 /* Check that the key is accepted in HostkeyAlgorithms */
2359 if (options.hostkeyalgorithms != NULL && 2360 if (options.hostkeyalgorithms != NULL &&
2360 match_pattern_list(sshkey_ssh_name(key), 2361 match_pattern_list(sshkey_ssh_name(key),
2361 options.hostkeyalgorithms, 2362 options.hostkeyalgorithms,
2362 strlen(options.hostkeyalgorithms), 0) != 1) { 2363 strlen(options.hostkeyalgorithms), 0) != 1) {
2363 debug3("%s: %s key not permitted by HostkeyAlgorithms", 2364 debug3("%s: %s key not permitted by HostkeyAlgorithms",
2364 __func__, sshkey_ssh_name(key)); 2365 __func__, sshkey_ssh_name(key));
2365 continue; 2366 continue;
2366 } 2367 }
2367 /* Skip certs */ 2368 /* Skip certs */
2368 if (sshkey_is_cert(key)) { 2369 if (sshkey_is_cert(key)) {
2369 debug3("%s: %s key is a certificate; skipping", 2370 debug3("%s: %s key is a certificate; skipping",
2370 __func__, sshkey_ssh_name(key)); 2371 __func__, sshkey_ssh_name(key));
2371 continue; 2372 continue;
2372 } 2373 }
2373 /* Ensure keys are unique */ 2374 /* Ensure keys are unique */
2374 for (i = 0; i < ctx->nkeys; i++) { 2375 for (i = 0; i < ctx->nkeys; i++) {
2375 if (sshkey_equal(key, ctx->keys[i])) { 2376 if (sshkey_equal(key, ctx->keys[i])) {
2376 error("%s: received duplicated %s host key", 2377 error("%s: received duplicated %s host key",
2377 __func__, sshkey_ssh_name(key)); 2378 __func__, sshkey_ssh_name(key));
2378 goto out; 2379 goto out;
2379 } 2380 }
2380 } 2381 }
2381 /* Key is good, record it */ 2382 /* Key is good, record it */
2382 if ((tmp = reallocarray(ctx->keys, ctx->nkeys + 1, 2383 if ((tmp = reallocarray(ctx->keys, ctx->nkeys + 1,
2383 sizeof(*ctx->keys))) == NULL) 2384 sizeof(*ctx->keys))) == NULL)
2384 fatal("%s: reallocarray failed nkeys = %zu", 2385 fatal("%s: reallocarray failed nkeys = %zu",
2385 __func__, ctx->nkeys); 2386 __func__, ctx->nkeys);
2386 ctx->keys = tmp; 2387 ctx->keys = tmp;
2387 ctx->keys[ctx->nkeys++] = key; 2388 ctx->keys[ctx->nkeys++] = key;
2388 key = NULL; 2389 key = NULL;
2389 } 2390 }
2390 2391
2391 if (ctx->nkeys == 0) { 2392 if (ctx->nkeys == 0) {
2392 debug("%s: server sent no hostkeys", __func__); 2393 debug("%s: server sent no hostkeys", __func__);
2393 goto out; 2394 goto out;
2394 } 2395 }
2395 2396
2396 if ((ctx->keys_seen = calloc(ctx->nkeys, 2397 if ((ctx->keys_seen = calloc(ctx->nkeys,
2397 sizeof(*ctx->keys_seen))) == NULL) 2398 sizeof(*ctx->keys_seen))) == NULL)
2398 fatal("%s: calloc failed", __func__); 2399 fatal("%s: calloc failed", __func__);
2399 2400
2400 get_hostfile_hostname_ipaddr(host, 2401 get_hostfile_hostname_ipaddr(host,
2401 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL, 2402 options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
2402 options.port, &ctx->host_str, 2403 options.port, &ctx->host_str,
2403 options.check_host_ip ? &ctx->ip_str : NULL); 2404 options.check_host_ip ? &ctx->ip_str : NULL);
2404 2405
2405 /* Find which keys we already know about. */ 2406 /* Find which keys we already know about. */
2406 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find, 2407 if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2407 ctx, ctx->host_str, ctx->ip_str, 2408 ctx, ctx->host_str, ctx->ip_str,
2408 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) { 2409 HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2409 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r)); 2410 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
2410 goto out; 2411 goto out;
2411 } 2412 }
2412 2413
2413 /* Figure out if we have any new keys to add */ 2414 /* Figure out if we have any new keys to add */
2414 ctx->nnew = 0; 2415 ctx->nnew = 0;
2415 for (i = 0; i < ctx->nkeys; i++) { 2416 for (i = 0; i < ctx->nkeys; i++) {
2416 if (!ctx->keys_seen[i]) 2417 if (!ctx->keys_seen[i])
2417 ctx->nnew++; 2418 ctx->nnew++;
2418 } 2419 }
2419 2420
2420 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove", 2421 debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2421 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold); 2422 __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2422 2423
2423 if (ctx->nnew == 0 && ctx->nold != 0) { 2424 if (ctx->nnew == 0 && ctx->nold != 0) {
2424 /* We have some keys to remove. Just do it. */ 2425 /* We have some keys to remove. Just do it. */
2425 update_known_hosts(ctx); 2426 update_known_hosts(ctx);
2426 } else if (ctx->nnew != 0) { 2427 } else if (ctx->nnew != 0) {
2427 /* 2428 /*
2428 * We have received hitherto-unseen keys from the server. 2429 * We have received hitherto-unseen keys from the server.
2429 * Ask the server to confirm ownership of the private halves. 2430 * Ask the server to confirm ownership of the private halves.
2430 */ 2431 */
2431 debug3("%s: asking server to prove ownership for %zu keys", 2432 debug3("%s: asking server to prove ownership for %zu keys",
2432 __func__, ctx->nnew); 2433 __func__, ctx->nnew);
2433 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || 2434 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2434 (r = sshpkt_put_cstring(ssh, 2435 (r = sshpkt_put_cstring(ssh,
2435 "hostkeys-prove-00@openssh.com")) != 0 || 2436 "hostkeys-prove-00@openssh.com")) != 0 ||
2436 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */ 2437 (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2437 fatal("%s: cannot prepare packet: %s", 2438 fatal("%s: cannot prepare packet: %s",
2438 __func__, ssh_err(r)); 2439 __func__, ssh_err(r));
2439 if ((buf = sshbuf_new()) == NULL) 2440 if ((buf = sshbuf_new()) == NULL)
2440 fatal("%s: sshbuf_new", __func__); 2441 fatal("%s: sshbuf_new", __func__);
2441 for (i = 0; i < ctx->nkeys; i++) { 2442 for (i = 0; i < ctx->nkeys; i++) {
2442 if (ctx->keys_seen[i]) 2443 if (ctx->keys_seen[i])
2443 continue; 2444 continue;
2444 sshbuf_reset(buf); 2445 sshbuf_reset(buf);
2445 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0) 2446 if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2446 fatal("%s: sshkey_putb: %s", 2447 fatal("%s: sshkey_putb: %s",
2447 __func__, ssh_err(r)); 2448 __func__, ssh_err(r));
2448 if ((r = sshpkt_put_stringb(ssh, buf)) != 0) 2449 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2449 fatal("%s: sshpkt_put_string: %s", 2450 fatal("%s: sshpkt_put_string: %s",
2450 __func__, ssh_err(r)); 2451 __func__, ssh_err(r));
2451 } 2452 }
2452 if ((r = sshpkt_send(ssh)) != 0) 2453 if ((r = sshpkt_send(ssh)) != 0)
2453 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r)); 2454 fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2454 client_register_global_confirm( 2455 client_register_global_confirm(
2455 client_global_hostkeys_private_confirm, ctx); 2456 client_global_hostkeys_private_confirm, ctx);
2456 ctx = NULL; /* will be freed in callback */ 2457 ctx = NULL; /* will be freed in callback */
2457 } 2458 }
2458 2459
2459 /* Success */ 2460 /* Success */
2460 out: 2461 out:
2461 hostkeys_update_ctx_free(ctx); 2462 hostkeys_update_ctx_free(ctx);
2462 sshkey_free(key); 2463 sshkey_free(key);
2463 sshbuf_free(buf); 2464 sshbuf_free(buf);
2464 /* 2465 /*
2465 * NB. Return success for all cases. The server doesn't need to know 2466 * NB. Return success for all cases. The server doesn't need to know
2466 * what the client does with its hosts file. 2467 * what the client does with its hosts file.
2467 */ 2468 */
2468 return 1; 2469 return 1;
2469} 2470}
2470 2471
2471static int 2472static int
2472client_input_global_request(int type, u_int32_t seq, void *ctxt) 2473client_input_global_request(int type, u_int32_t seq, void *ctxt)
2473{ 2474{
2474 char *rtype; 2475 char *rtype;
2475 int want_reply; 2476 int want_reply;
2476 int success = 0; 2477 int success = 0;
2477 2478
2478 rtype = packet_get_cstring(NULL); 2479 rtype = packet_get_cstring(NULL);
2479 want_reply = packet_get_char(); 2480 want_reply = packet_get_char();
2480 debug("client_input_global_request: rtype %s want_reply %d", 2481 debug("client_input_global_request: rtype %s want_reply %d",
2481 rtype, want_reply); 2482 rtype, want_reply);
2482 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0) 2483 if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
2483 success = client_input_hostkeys(); 2484 success = client_input_hostkeys();
2484 if (want_reply) { 2485 if (want_reply) {
2485 packet_start(success ? 2486 packet_start(success ?
2486 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); 2487 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2487 packet_send(); 2488 packet_send();
2488 packet_write_wait(); 2489 packet_write_wait();
2489 } 2490 }
2490 free(rtype); 2491 free(rtype);
2491 return 0; 2492 return 0;
2492} 2493}
2493 2494
2494void 2495void
2495client_session2_setup(int id, int want_tty, int want_subsystem, 2496client_session2_setup(int id, int want_tty, int want_subsystem,
2496 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env) 2497 const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
2497{ 2498{
2498 int len; 2499 int len;
2499 Channel *c = NULL; 2500 Channel *c = NULL;
2500 2501
2501 debug2("%s: id %d", __func__, id); 2502 debug2("%s: id %d", __func__, id);
2502 2503
2503 if ((c = channel_lookup(id)) == NULL) 2504 if ((c = channel_lookup(id)) == NULL)
2504 fatal("client_session2_setup: channel %d: unknown channel", id); 2505 fatal("client_session2_setup: channel %d: unknown channel", id);
2505 2506
2506 packet_set_interactive(want_tty, 2507 packet_set_interactive(want_tty,
2507 options.ip_qos_interactive, options.ip_qos_bulk); 2508 options.ip_qos_interactive, options.ip_qos_bulk);
2508 2509
2509 if (want_tty) { 2510 if (want_tty) {
2510 struct winsize ws; 2511 struct winsize ws;
2511 2512
2512 /* Store window size in the packet. */ 2513 /* Store window size in the packet. */
2513 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0) 2514 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2514 memset(&ws, 0, sizeof(ws)); 2515 memset(&ws, 0, sizeof(ws));
2515 2516
2516 channel_request_start(id, "pty-req", 1); 2517 channel_request_start(id, "pty-req", 1);
2517 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY); 2518 client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
2518 packet_put_cstring(term != NULL ? term : ""); 2519 packet_put_cstring(term != NULL ? term : "");
2519 packet_put_int((u_int)ws.ws_col); 2520 packet_put_int((u_int)ws.ws_col);
2520 packet_put_int((u_int)ws.ws_row); 2521 packet_put_int((u_int)ws.ws_row);
2521 packet_put_int((u_int)ws.ws_xpixel); 2522 packet_put_int((u_int)ws.ws_xpixel);
2522 packet_put_int((u_int)ws.ws_ypixel); 2523 packet_put_int((u_int)ws.ws_ypixel);
2523 if (tiop == NULL) 2524 if (tiop == NULL)
2524 tiop = get_saved_tio(); 2525 tiop = get_saved_tio();
2525 tty_make_modes(-1, tiop); 2526 tty_make_modes(-1, tiop);
2526 packet_send(); 2527 packet_send();
2527 /* XXX wait for reply */ 2528 /* XXX wait for reply */
2528 c->client_tty = 1; 2529 c->client_tty = 1;
2529 } 2530 }
2530 2531
2531 /* Transfer any environment variables from client to server */ 2532 /* Transfer any environment variables from client to server */
2532 if (options.num_send_env != 0 && env != NULL) { 2533 if (options.num_send_env != 0 && env != NULL) {
2533 int i, j, matched; 2534 int i, j, matched;
2534 char *name, *val; 2535 char *name, *val;
2535 2536
2536 debug("Sending environment."); 2537 debug("Sending environment.");
2537 for (i = 0; env[i] != NULL; i++) { 2538 for (i = 0; env[i] != NULL; i++) {
2538 /* Split */ 2539 /* Split */
2539 name = xstrdup(env[i]); 2540 name = xstrdup(env[i]);
2540 if ((val = strchr(name, '=')) == NULL) { 2541 if ((val = strchr(name, '=')) == NULL) {
2541 free(name); 2542 free(name);
2542 continue; 2543 continue;
2543 } 2544 }
2544 *val++ = '\0'; 2545 *val++ = '\0';
2545 2546
2546 matched = 0; 2547 matched = 0;
2547 for (j = 0; j < options.num_send_env; j++) { 2548 for (j = 0; j < options.num_send_env; j++) {
2548 if (match_pattern(name, options.send_env[j])) { 2549 if (match_pattern(name, options.send_env[j])) {
2549 matched = 1; 2550 matched = 1;
2550 break; 2551 break;
2551 } 2552 }
2552 } 2553 }
2553 if (!matched) { 2554 if (!matched) {
2554 debug3("Ignored env %s", name); 2555 debug3("Ignored env %s", name);
2555 free(name); 2556 free(name);
2556 continue; 2557 continue;
2557 } 2558 }
2558 2559
2559 debug("Sending env %s = %s", name, val); 2560 debug("Sending env %s = %s", name, val);
2560 channel_request_start(id, "env", 0); 2561 channel_request_start(id, "env", 0);
2561 packet_put_cstring(name); 2562 packet_put_cstring(name);
2562 packet_put_cstring(val); 2563 packet_put_cstring(val);
2563 packet_send(); 2564 packet_send();
2564 free(name); 2565 free(name);
2565 } 2566 }
2566 } 2567 }
2567 2568
2568 len = buffer_len(cmd); 2569 len = buffer_len(cmd);
2569 if (len > 0) { 2570 if (len > 0) {
2570 if (len > 900) 2571 if (len > 900)
2571 len = 900; 2572 len = 900;
2572 if (want_subsystem) { 2573 if (want_subsystem) {
2573 debug("Sending subsystem: %.*s", 2574 debug("Sending subsystem: %.*s",
2574 len, (u_char*)buffer_ptr(cmd)); 2575 len, (u_char*)buffer_ptr(cmd));
2575 channel_request_start(id, "subsystem", 1); 2576 channel_request_start(id, "subsystem", 1);
2576 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE); 2577 client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
2577 } else { 2578 } else {
2578 debug("Sending command: %.*s", 2579 debug("Sending command: %.*s",
2579 len, (u_char*)buffer_ptr(cmd)); 2580 len, (u_char*)buffer_ptr(cmd));
2580 channel_request_start(id, "exec", 1); 2581 channel_request_start(id, "exec", 1);
2581 client_expect_confirm(id, "exec", CONFIRM_CLOSE); 2582 client_expect_confirm(id, "exec", CONFIRM_CLOSE);
2582 } 2583 }
2583 packet_put_string(buffer_ptr(cmd), buffer_len(cmd)); 2584 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2584 packet_send(); 2585 packet_send();
2585 } else { 2586 } else {
2586 channel_request_start(id, "shell", 1); 2587 channel_request_start(id, "shell", 1);
2587 client_expect_confirm(id, "shell", CONFIRM_CLOSE); 2588 client_expect_confirm(id, "shell", CONFIRM_CLOSE);
2588 packet_send(); 2589 packet_send();
2589 } 2590 }
2590} 2591}
2591 2592
2592static void 2593static void
2593client_init_dispatch_20(void) 2594client_init_dispatch_20(void)
2594{ 2595{
2595 dispatch_init(&dispatch_protocol_error); 2596 dispatch_init(&dispatch_protocol_error);
2596 2597