Fix markup error introduced in r9626.
[u/mdw/putty] / ssh.c
CommitLineData
eaf1e20a 1/*
2 * SSH backend.
3 */
4
374330e2 5#include <stdio.h>
6#include <stdlib.h>
fb09bf1c 7#include <stdarg.h>
8#include <assert.h>
3c112d53 9#include <limits.h>
d051d1b4 10#include <signal.h>
374330e2 11
12#include "putty.h"
dacbd0e8 13#include "tree234.h"
fb09bf1c 14#include "ssh.h"
bf84a03a 15#ifndef NO_GSSAPI
b3d375b2 16#include "sshgssc.h"
42af6a67 17#include "sshgss.h"
bf84a03a 18#endif
374330e2 19
20#ifndef FALSE
21#define FALSE 0
22#endif
23#ifndef TRUE
24#define TRUE 1
25#endif
26
32874aea 27#define SSH1_MSG_DISCONNECT 1 /* 0x1 */
28#define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */
29#define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */
30#define SSH1_CMSG_USER 4 /* 0x4 */
31#define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */
32#define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */
33#define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */
34#define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */
35#define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */
36#define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */
37#define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */
38#define SSH1_CMSG_EXEC_CMD 13 /* 0xd */
39#define SSH1_SMSG_SUCCESS 14 /* 0xe */
40#define SSH1_SMSG_FAILURE 15 /* 0xf */
41#define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */
42#define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */
43#define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */
44#define SSH1_CMSG_EOF 19 /* 0x13 */
45#define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */
46#define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */
47#define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */
48#define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */
49#define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */
50#define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */
51#define SSH1_SMSG_X11_OPEN 27 /* 0x1b */
52#define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */
53#define SSH1_MSG_PORT_OPEN 29 /* 0x1d */
54#define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */
55#define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */
56#define SSH1_MSG_IGNORE 32 /* 0x20 */
57#define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */
58#define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */
59#define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */
60#define SSH1_MSG_DEBUG 36 /* 0x24 */
61#define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */
62#define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */
63#define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */
64#define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */
65#define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */
66#define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */
67#define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */
68
8f9e3231 69#define SSH1_AUTH_RHOSTS 1 /* 0x1 */
70#define SSH1_AUTH_RSA 2 /* 0x2 */
71#define SSH1_AUTH_PASSWORD 3 /* 0x3 */
72#define SSH1_AUTH_RHOSTS_RSA 4 /* 0x4 */
32874aea 73#define SSH1_AUTH_TIS 5 /* 0x5 */
74#define SSH1_AUTH_CCARD 16 /* 0x10 */
75
76#define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */
b96dc54c 77/* Mask for protoflags we will echo back to server if seen */
32874aea 78#define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */
79
80#define SSH2_MSG_DISCONNECT 1 /* 0x1 */
81#define SSH2_MSG_IGNORE 2 /* 0x2 */
82#define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */
83#define SSH2_MSG_DEBUG 4 /* 0x4 */
84#define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */
85#define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */
86#define SSH2_MSG_KEXINIT 20 /* 0x14 */
87#define SSH2_MSG_NEWKEYS 21 /* 0x15 */
88#define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */
89#define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */
90#define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */
91#define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */
92#define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */
93#define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */
fae1a71b 94#define SSH2_MSG_KEXRSA_PUBKEY 30 /* 0x1e */
95#define SSH2_MSG_KEXRSA_SECRET 31 /* 0x1f */
96#define SSH2_MSG_KEXRSA_DONE 32 /* 0x20 */
32874aea 97#define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */
98#define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */
99#define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */
100#define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */
101#define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */
102#define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */
761187b6 103#define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */
104#define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */
32874aea 105#define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */
106#define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */
107#define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */
108#define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */
109#define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */
110#define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */
111#define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */
112#define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */
113#define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */
114#define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */
115#define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */
116#define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */
117#define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */
118#define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */
42af6a67 119#define SSH2_MSG_USERAUTH_GSSAPI_RESPONSE 60
120#define SSH2_MSG_USERAUTH_GSSAPI_TOKEN 61
121#define SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE 63
122#define SSH2_MSG_USERAUTH_GSSAPI_ERROR 64
123#define SSH2_MSG_USERAUTH_GSSAPI_ERRTOK 65
124#define SSH2_MSG_USERAUTH_GSSAPI_MIC 66
32874aea 125
00db133f 126/*
127 * Packet type contexts, so that ssh2_pkt_type can correctly decode
128 * the ambiguous type numbers back into the correct type strings.
129 */
acab36bc 130typedef enum {
131 SSH2_PKTCTX_NOKEX,
132 SSH2_PKTCTX_DHGROUP,
133 SSH2_PKTCTX_DHGEX,
134 SSH2_PKTCTX_RSAKEX
135} Pkt_KCtx;
136typedef enum {
137 SSH2_PKTCTX_NOAUTH,
138 SSH2_PKTCTX_PUBLICKEY,
139 SSH2_PKTCTX_PASSWORD,
42af6a67 140 SSH2_PKTCTX_GSSAPI,
acab36bc 141 SSH2_PKTCTX_KBDINTER
142} Pkt_ACtx;
00db133f 143
32874aea 144#define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
145#define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
146#define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
147#define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
148#define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
149#define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
150#define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
151#define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
152#define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
153#define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
154#define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
155#define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */
156#define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */
157#define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */
158#define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */
38c4a8da 159
160static const char *const ssh2_disconnect_reasons[] = {
161 NULL,
afb4d0dc 162 "host not allowed to connect",
163 "protocol error",
164 "key exchange failed",
165 "host authentication failed",
1da38d29 166 "MAC error",
afb4d0dc 167 "compression error",
168 "service not available",
169 "protocol version not supported",
170 "host key not verifiable",
171 "connection lost",
172 "by application",
173 "too many connections",
174 "auth cancelled by user",
175 "no more auth methods available",
176 "illegal user name",
38c4a8da 177};
9005f3ba 178
32874aea 179#define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
180#define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
181#define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
182#define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
d211621f 183
32874aea 184#define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
fb09bf1c 185
7d503c31 186/*
187 * Various remote-bug flags.
188 */
189#define BUG_CHOKES_ON_SSH1_IGNORE 1
190#define BUG_SSH2_HMAC 2
bd358db1 191#define BUG_NEEDS_SSH1_PLAIN_PASSWORD 4
0df73905 192#define BUG_CHOKES_ON_RSA 8
1dd353b5 193#define BUG_SSH2_RSA_PADDING 16
088bde77 194#define BUG_SSH2_DERIVEKEY 32
f382c87d 195#define BUG_SSH2_REKEY 64
dda87a28 196#define BUG_SSH2_PK_SESSIONID 128
c9739dba 197#define BUG_SSH2_MAXPKT 256
cf6ddb95 198#define BUG_CHOKES_ON_SSH2_IGNORE 512
2e0aae4f 199#define BUG_CHOKES_ON_WINADJ 1024
bd358db1 200
c6ccd5c2 201/*
202 * Codes for terminal modes.
203 * Most of these are the same in SSH-1 and SSH-2.
7646c432 204 * This list is derived from RFC 4254 and
c6ccd5c2 205 * SSH-1 RFC-1.2.31.
206 */
207static const struct {
208 const char* const mode;
209 int opcode;
210 enum { TTY_OP_CHAR, TTY_OP_BOOL } type;
211} ssh_ttymodes[] = {
212 /* "V" prefix discarded for special characters relative to SSH specs */
213 { "INTR", 1, TTY_OP_CHAR },
214 { "QUIT", 2, TTY_OP_CHAR },
215 { "ERASE", 3, TTY_OP_CHAR },
216 { "KILL", 4, TTY_OP_CHAR },
217 { "EOF", 5, TTY_OP_CHAR },
218 { "EOL", 6, TTY_OP_CHAR },
219 { "EOL2", 7, TTY_OP_CHAR },
220 { "START", 8, TTY_OP_CHAR },
221 { "STOP", 9, TTY_OP_CHAR },
222 { "SUSP", 10, TTY_OP_CHAR },
223 { "DSUSP", 11, TTY_OP_CHAR },
224 { "REPRINT", 12, TTY_OP_CHAR },
225 { "WERASE", 13, TTY_OP_CHAR },
226 { "LNEXT", 14, TTY_OP_CHAR },
227 { "FLUSH", 15, TTY_OP_CHAR },
228 { "SWTCH", 16, TTY_OP_CHAR },
229 { "STATUS", 17, TTY_OP_CHAR },
230 { "DISCARD", 18, TTY_OP_CHAR },
231 { "IGNPAR", 30, TTY_OP_BOOL },
232 { "PARMRK", 31, TTY_OP_BOOL },
233 { "INPCK", 32, TTY_OP_BOOL },
234 { "ISTRIP", 33, TTY_OP_BOOL },
235 { "INLCR", 34, TTY_OP_BOOL },
236 { "IGNCR", 35, TTY_OP_BOOL },
237 { "ICRNL", 36, TTY_OP_BOOL },
238 { "IUCLC", 37, TTY_OP_BOOL },
239 { "IXON", 38, TTY_OP_BOOL },
240 { "IXANY", 39, TTY_OP_BOOL },
241 { "IXOFF", 40, TTY_OP_BOOL },
242 { "IMAXBEL", 41, TTY_OP_BOOL },
243 { "ISIG", 50, TTY_OP_BOOL },
244 { "ICANON", 51, TTY_OP_BOOL },
245 { "XCASE", 52, TTY_OP_BOOL },
246 { "ECHO", 53, TTY_OP_BOOL },
247 { "ECHOE", 54, TTY_OP_BOOL },
248 { "ECHOK", 55, TTY_OP_BOOL },
249 { "ECHONL", 56, TTY_OP_BOOL },
250 { "NOFLSH", 57, TTY_OP_BOOL },
251 { "TOSTOP", 58, TTY_OP_BOOL },
252 { "IEXTEN", 59, TTY_OP_BOOL },
253 { "ECHOCTL", 60, TTY_OP_BOOL },
254 { "ECHOKE", 61, TTY_OP_BOOL },
255 { "PENDIN", 62, TTY_OP_BOOL }, /* XXX is this a real mode? */
256 { "OPOST", 70, TTY_OP_BOOL },
257 { "OLCUC", 71, TTY_OP_BOOL },
258 { "ONLCR", 72, TTY_OP_BOOL },
259 { "OCRNL", 73, TTY_OP_BOOL },
260 { "ONOCR", 74, TTY_OP_BOOL },
261 { "ONLRET", 75, TTY_OP_BOOL },
262 { "CS7", 90, TTY_OP_BOOL },
263 { "CS8", 91, TTY_OP_BOOL },
264 { "PARENB", 92, TTY_OP_BOOL },
265 { "PARODD", 93, TTY_OP_BOOL }
266};
267
268/* Miscellaneous other tty-related constants. */
269#define SSH_TTY_OP_END 0
270/* The opcodes for ISPEED/OSPEED differ between SSH-1 and SSH-2. */
271#define SSH1_TTY_OP_ISPEED 192
272#define SSH1_TTY_OP_OSPEED 193
273#define SSH2_TTY_OP_ISPEED 128
274#define SSH2_TTY_OP_OSPEED 129
275
276/* Helper functions for parsing tty-related config. */
277static unsigned int ssh_tty_parse_specchar(char *s)
278{
279 unsigned int ret;
280 if (*s) {
281 char *next = NULL;
282 ret = ctrlparse(s, &next);
283 if (!next) ret = s[0];
284 } else {
285 ret = 255; /* special value meaning "don't set" */
286 }
287 return ret;
288}
289static unsigned int ssh_tty_parse_boolean(char *s)
290{
291 if (stricmp(s, "yes") == 0 ||
292 stricmp(s, "on") == 0 ||
293 stricmp(s, "true") == 0 ||
294 stricmp(s, "+") == 0)
295 return 1; /* true */
296 else if (stricmp(s, "no") == 0 ||
297 stricmp(s, "off") == 0 ||
298 stricmp(s, "false") == 0 ||
299 stricmp(s, "-") == 0)
300 return 0; /* false */
301 else
302 return (atoi(s) != 0);
303}
304
00db133f 305#define translate(x) if (type == x) return #x
acab36bc 306#define translatek(x,ctx) if (type == x && (pkt_kctx == ctx)) return #x
307#define translatea(x,ctx) if (type == x && (pkt_actx == ctx)) return #x
ae9ae89f 308static char *ssh1_pkt_type(int type)
00db133f 309{
310 translate(SSH1_MSG_DISCONNECT);
311 translate(SSH1_SMSG_PUBLIC_KEY);
312 translate(SSH1_CMSG_SESSION_KEY);
313 translate(SSH1_CMSG_USER);
314 translate(SSH1_CMSG_AUTH_RSA);
315 translate(SSH1_SMSG_AUTH_RSA_CHALLENGE);
316 translate(SSH1_CMSG_AUTH_RSA_RESPONSE);
317 translate(SSH1_CMSG_AUTH_PASSWORD);
318 translate(SSH1_CMSG_REQUEST_PTY);
319 translate(SSH1_CMSG_WINDOW_SIZE);
320 translate(SSH1_CMSG_EXEC_SHELL);
321 translate(SSH1_CMSG_EXEC_CMD);
322 translate(SSH1_SMSG_SUCCESS);
323 translate(SSH1_SMSG_FAILURE);
324 translate(SSH1_CMSG_STDIN_DATA);
325 translate(SSH1_SMSG_STDOUT_DATA);
326 translate(SSH1_SMSG_STDERR_DATA);
327 translate(SSH1_CMSG_EOF);
328 translate(SSH1_SMSG_EXIT_STATUS);
329 translate(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);
330 translate(SSH1_MSG_CHANNEL_OPEN_FAILURE);
331 translate(SSH1_MSG_CHANNEL_DATA);
332 translate(SSH1_MSG_CHANNEL_CLOSE);
333 translate(SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION);
334 translate(SSH1_SMSG_X11_OPEN);
335 translate(SSH1_CMSG_PORT_FORWARD_REQUEST);
336 translate(SSH1_MSG_PORT_OPEN);
337 translate(SSH1_CMSG_AGENT_REQUEST_FORWARDING);
338 translate(SSH1_SMSG_AGENT_OPEN);
339 translate(SSH1_MSG_IGNORE);
340 translate(SSH1_CMSG_EXIT_CONFIRMATION);
341 translate(SSH1_CMSG_X11_REQUEST_FORWARDING);
342 translate(SSH1_CMSG_AUTH_RHOSTS_RSA);
343 translate(SSH1_MSG_DEBUG);
344 translate(SSH1_CMSG_REQUEST_COMPRESSION);
345 translate(SSH1_CMSG_AUTH_TIS);
346 translate(SSH1_SMSG_AUTH_TIS_CHALLENGE);
347 translate(SSH1_CMSG_AUTH_TIS_RESPONSE);
348 translate(SSH1_CMSG_AUTH_CCARD);
349 translate(SSH1_SMSG_AUTH_CCARD_CHALLENGE);
350 translate(SSH1_CMSG_AUTH_CCARD_RESPONSE);
351 return "unknown";
352}
acab36bc 353static char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type)
00db133f 354{
42af6a67 355 translatea(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,SSH2_PKTCTX_GSSAPI);
356 translatea(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,SSH2_PKTCTX_GSSAPI);
357 translatea(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,SSH2_PKTCTX_GSSAPI);
358 translatea(SSH2_MSG_USERAUTH_GSSAPI_ERROR,SSH2_PKTCTX_GSSAPI);
359 translatea(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,SSH2_PKTCTX_GSSAPI);
360 translatea(SSH2_MSG_USERAUTH_GSSAPI_MIC, SSH2_PKTCTX_GSSAPI);
00db133f 361 translate(SSH2_MSG_DISCONNECT);
362 translate(SSH2_MSG_IGNORE);
363 translate(SSH2_MSG_UNIMPLEMENTED);
364 translate(SSH2_MSG_DEBUG);
365 translate(SSH2_MSG_SERVICE_REQUEST);
366 translate(SSH2_MSG_SERVICE_ACCEPT);
367 translate(SSH2_MSG_KEXINIT);
368 translate(SSH2_MSG_NEWKEYS);
acab36bc 369 translatek(SSH2_MSG_KEXDH_INIT, SSH2_PKTCTX_DHGROUP);
370 translatek(SSH2_MSG_KEXDH_REPLY, SSH2_PKTCTX_DHGROUP);
371 translatek(SSH2_MSG_KEX_DH_GEX_REQUEST, SSH2_PKTCTX_DHGEX);
372 translatek(SSH2_MSG_KEX_DH_GEX_GROUP, SSH2_PKTCTX_DHGEX);
373 translatek(SSH2_MSG_KEX_DH_GEX_INIT, SSH2_PKTCTX_DHGEX);
374 translatek(SSH2_MSG_KEX_DH_GEX_REPLY, SSH2_PKTCTX_DHGEX);
375 translatek(SSH2_MSG_KEXRSA_PUBKEY, SSH2_PKTCTX_RSAKEX);
376 translatek(SSH2_MSG_KEXRSA_SECRET, SSH2_PKTCTX_RSAKEX);
377 translatek(SSH2_MSG_KEXRSA_DONE, SSH2_PKTCTX_RSAKEX);
00db133f 378 translate(SSH2_MSG_USERAUTH_REQUEST);
379 translate(SSH2_MSG_USERAUTH_FAILURE);
380 translate(SSH2_MSG_USERAUTH_SUCCESS);
381 translate(SSH2_MSG_USERAUTH_BANNER);
acab36bc 382 translatea(SSH2_MSG_USERAUTH_PK_OK, SSH2_PKTCTX_PUBLICKEY);
383 translatea(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, SSH2_PKTCTX_PASSWORD);
384 translatea(SSH2_MSG_USERAUTH_INFO_REQUEST, SSH2_PKTCTX_KBDINTER);
385 translatea(SSH2_MSG_USERAUTH_INFO_RESPONSE, SSH2_PKTCTX_KBDINTER);
00db133f 386 translate(SSH2_MSG_GLOBAL_REQUEST);
387 translate(SSH2_MSG_REQUEST_SUCCESS);
388 translate(SSH2_MSG_REQUEST_FAILURE);
389 translate(SSH2_MSG_CHANNEL_OPEN);
390 translate(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
391 translate(SSH2_MSG_CHANNEL_OPEN_FAILURE);
392 translate(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
393 translate(SSH2_MSG_CHANNEL_DATA);
394 translate(SSH2_MSG_CHANNEL_EXTENDED_DATA);
395 translate(SSH2_MSG_CHANNEL_EOF);
396 translate(SSH2_MSG_CHANNEL_CLOSE);
397 translate(SSH2_MSG_CHANNEL_REQUEST);
398 translate(SSH2_MSG_CHANNEL_SUCCESS);
399 translate(SSH2_MSG_CHANNEL_FAILURE);
400 return "unknown";
401}
402#undef translate
403#undef translatec
7d503c31 404
9a10ecf4 405/* Enumeration values for fields in SSH-1 packets */
406enum {
407 PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM,
408 /* These values are for communicating relevant semantics of
409 * fields to the packet logging code. */
410 PKTT_OTHER, PKTT_PASSWORD, PKTT_DATA
411};
972a41c8 412
acddebd9 413/*
414 * Coroutine mechanics for the sillier bits of the code. If these
415 * macros look impenetrable to you, you might find it helpful to
416 * read
417 *
418 * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
419 *
420 * which explains the theory behind these macros.
b624d1e6 421 *
422 * In particular, if you are getting `case expression not constant'
423 * errors when building with MS Visual Studio, this is because MS's
424 * Edit and Continue debugging feature causes their compiler to
425 * violate ANSI C. To disable Edit and Continue debugging:
426 *
427 * - right-click ssh.c in the FileView
428 * - click Settings
429 * - select the C/C++ tab and the General category
430 * - under `Debug info:', select anything _other_ than `Program
431 * Database for Edit and Continue'.
acddebd9 432 */
51470298 433#define crBegin(v) { int *crLine = &v; switch(v) { case 0:;
30919997 434#define crBeginState crBegin(s->crLine)
435#define crStateP(t, v) \
436 struct t *s; \
437 if (!(v)) { s = (v) = snew(struct t); s->crLine = 0; } \
438 s = (v);
439#define crState(t) crStateP(t, ssh->t)
51470298 440#define crFinish(z) } *crLine = 0; return (z); }
441#define crFinishV } *crLine = 0; return; }
374330e2 442#define crReturn(z) \
443 do {\
51470298 444 *crLine =__LINE__; return (z); case __LINE__:;\
374330e2 445 } while (0)
446#define crReturnV \
447 do {\
51470298 448 *crLine=__LINE__; return; case __LINE__:;\
374330e2 449 } while (0)
51470298 450#define crStop(z) do{ *crLine = 0; return (z); }while(0)
451#define crStopV do{ *crLine = 0; return; }while(0)
fb09bf1c 452#define crWaitUntil(c) do { crReturn(0); } while (!(c))
7cca0d81 453#define crWaitUntilV(c) do { crReturnV; } while (!(c))
374330e2 454
51470298 455typedef struct ssh_tag *Ssh;
ff3187f6 456struct Packet;
457
dacd8872 458static struct Packet *ssh1_pkt_init(int pkt_type);
ff3187f6 459static struct Packet *ssh2_pkt_init(int pkt_type);
dacd8872 460static void ssh_pkt_ensure(struct Packet *, int length);
461static void ssh_pkt_adddata(struct Packet *, void *data, int len);
462static void ssh_pkt_addbyte(struct Packet *, unsigned char value);
ff3187f6 463static void ssh2_pkt_addbool(struct Packet *, unsigned char value);
dacd8872 464static void ssh_pkt_adduint32(struct Packet *, unsigned long value);
465static void ssh_pkt_addstring_start(struct Packet *);
466static void ssh_pkt_addstring_str(struct Packet *, char *data);
467static void ssh_pkt_addstring_data(struct Packet *, char *data, int len);
468static void ssh_pkt_addstring(struct Packet *, char *data);
d8baa528 469static unsigned char *ssh2_mpint_fmt(Bignum b, int *len);
dacd8872 470static void ssh1_pkt_addmp(struct Packet *, Bignum b);
ff3187f6 471static void ssh2_pkt_addmp(struct Packet *, Bignum b);
472static int ssh2_pkt_construct(Ssh, struct Packet *);
473static void ssh2_pkt_send(Ssh, struct Packet *);
590f6a5f 474static void ssh2_pkt_send_noqueue(Ssh, struct Packet *);
ff3187f6 475static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
476 struct Packet *pktin);
477static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
478 struct Packet *pktin);
997097db 479static void ssh2_channel_check_close(struct ssh_channel *c);
bc06669b 480static void ssh_channel_destroy(struct ssh_channel *c);
3d63ca2e 481
5471d09a 482/*
483 * Buffer management constants. There are several of these for
484 * various different purposes:
485 *
486 * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up
487 * on a local data stream before we throttle the whole SSH
2e85c969 488 * connection (in SSH-1 only). Throttling the whole connection is
5471d09a 489 * pretty drastic so we set this high in the hope it won't
490 * happen very often.
491 *
492 * - SSH_MAX_BACKLOG is the amount of backlog that must build up
493 * on the SSH connection itself before we defensively throttle
494 * _all_ local data streams. This is pretty drastic too (though
2e85c969 495 * thankfully unlikely in SSH-2 since the window mechanism should
5471d09a 496 * ensure that the server never has any need to throttle its end
497 * of the connection), so we set this high as well.
498 *
2e85c969 499 * - OUR_V2_WINSIZE is the maximum window size we present on SSH-2
5471d09a 500 * channels.
9b53e9b8 501 *
502 * - OUR_V2_BIGWIN is the window size we advertise for the only
3361b80a 503 * channel in a simple connection. It must be <= INT_MAX.
9916cc1e 504 *
505 * - OUR_V2_MAXPKT is the official "maximum packet size" we send
506 * to the remote side. This actually has nothing to do with the
507 * size of the _packet_, but is instead a limit on the amount
508 * of data we're willing to receive in a single SSH2 channel
509 * data message.
510 *
511 * - OUR_V2_PACKETLIMIT is actually the maximum size of SSH
512 * _packet_ we're prepared to cope with. It must be a multiple
513 * of the cipher block size, and must be at least 35000.
5471d09a 514 */
515
516#define SSH1_BUFFER_LIMIT 32768
517#define SSH_MAX_BACKLOG 32768
518#define OUR_V2_WINSIZE 16384
3361b80a 519#define OUR_V2_BIGWIN 0x7fffffff
954d5c5a 520#define OUR_V2_MAXPKT 0x4000UL
9916cc1e 521#define OUR_V2_PACKETLIMIT 0x9000UL
d74d141c 522
85cc02bb 523const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss };
e5574168 524
8b2715b2 525const static struct ssh_mac *macs[] = {
6668a75e 526 &ssh_hmac_sha1, &ssh_hmac_sha1_96, &ssh_hmac_md5
32874aea 527};
8b2715b2 528const static struct ssh_mac *buggymacs[] = {
6668a75e 529 &ssh_hmac_sha1_buggy, &ssh_hmac_sha1_96_buggy, &ssh_hmac_md5
32874aea 530};
e5574168 531
5366aed8 532static void *ssh_comp_none_init(void)
533{
534 return NULL;
535}
536static void ssh_comp_none_cleanup(void *handle)
32874aea 537{
538}
5366aed8 539static int ssh_comp_none_block(void *handle, unsigned char *block, int len,
32874aea 540 unsigned char **outblock, int *outlen)
541{
542 return 0;
543}
5366aed8 544static int ssh_comp_none_disable(void *handle)
32874aea 545{
4ba9b64b 546 return 0;
547}
57476f6b 548const static struct ssh_compress ssh_comp_none = {
ab18ccff 549 "none", NULL,
5366aed8 550 ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block,
551 ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block,
552 ssh_comp_none_disable, NULL
e5574168 553};
4ba9b64b 554extern const struct ssh_compress ssh_zlib;
555const static struct ssh_compress *compressions[] = {
32874aea 556 &ssh_zlib, &ssh_comp_none
557};
374330e2 558
32874aea 559enum { /* channel types */
783415f8 560 CHAN_MAINSESSION,
561 CHAN_X11,
562 CHAN_AGENT,
bc240b21 563 CHAN_SOCKDATA,
c6b41791 564 CHAN_SOCKDATA_DORMANT, /* one the remote hasn't confirmed */
565 /*
566 * CHAN_ZOMBIE is used to indicate a channel for which we've
567 * already destroyed the local data source: for instance, if a
568 * forwarded port experiences a socket error on the local side, we
569 * immediately destroy its local socket and turn the SSH channel
570 * into CHAN_ZOMBIE.
571 */
572 CHAN_ZOMBIE
783415f8 573};
574
39d830e4 575typedef void (*handler_fn_t)(Ssh ssh, struct Packet *pktin);
576typedef void (*chandler_fn_t)(Ssh ssh, struct Packet *pktin, void *ctx);
577typedef void (*cchandler_fn_t)(struct ssh_channel *, struct Packet *, void *);
578
579/*
580 * Each channel has a queue of outstanding CHANNEL_REQUESTS and their
581 * handlers.
582 */
583struct outstanding_channel_request {
584 cchandler_fn_t handler;
585 void *ctx;
586 struct outstanding_channel_request *next;
587};
588
dacbd0e8 589/*
0e443c99 590 * little structure to keep track of outstanding WINDOW_ADJUSTs
591 */
592struct winadj {
593 struct winadj *next;
594 unsigned size;
595};
596
597/*
dacbd0e8 598 * 2-3-4 tree storing channels.
599 */
600struct ssh_channel {
51470298 601 Ssh ssh; /* pointer back to main context */
d211621f 602 unsigned remoteid, localid;
dacbd0e8 603 int type;
64d6ff88 604 /* True if we opened this channel but server hasn't confirmed. */
605 int halfopen;
0357890f 606 /*
2e85c969 607 * In SSH-1, this value contains four bits:
0357890f 608 *
609 * 1 We have sent SSH1_MSG_CHANNEL_CLOSE.
610 * 2 We have sent SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION.
611 * 4 We have received SSH1_MSG_CHANNEL_CLOSE.
612 * 8 We have received SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION.
613 *
614 * A channel is completely finished with when all four bits are set.
bc06669b 615 *
616 * In SSH-2, the four bits mean:
617 *
618 * 1 We have sent SSH2_MSG_CHANNEL_EOF.
619 * 2 We have sent SSH2_MSG_CHANNEL_CLOSE.
620 * 4 We have received SSH2_MSG_CHANNEL_EOF.
621 * 8 We have received SSH2_MSG_CHANNEL_CLOSE.
622 *
623 * A channel is completely finished with when we have both sent
624 * and received CLOSE.
625 *
626 * The symbolic constants below use the SSH-2 terminology, which
627 * is a bit confusing in SSH-1, but we have to use _something_.
0357890f 628 */
bc06669b 629#define CLOSES_SENT_EOF 1
630#define CLOSES_SENT_CLOSE 2
631#define CLOSES_RCVD_EOF 4
632#define CLOSES_RCVD_CLOSE 8
dacbd0e8 633 int closes;
78280721 634
635 /*
bc06669b 636 * This flag indicates that an EOF is pending on the outgoing side
637 * of the channel: that is, wherever we're getting the data for
638 * this channel has sent us some data followed by EOF. We can't
639 * actually send the EOF until we've finished sending the data, so
640 * we set this flag instead to remind us to do so once our buffer
641 * is clear.
78280721 642 */
bc06669b 643 int pending_eof;
78280721 644
ff68564b 645 /*
646 * True if this channel is causing the underlying connection to be
647 * throttled.
648 */
649 int throttling_conn;
5471d09a 650 union {
5471d09a 651 struct ssh2_data_channel {
652 bufchain outbuffer;
653 unsigned remwindow, remmaxpkt;
467e064d 654 /* locwindow is signed so we can cope with excess data. */
9b53e9b8 655 int locwindow, locmaxwin;
0e443c99 656 /*
657 * remlocwin is the amount of local window that we think
658 * the remote end had available to it after it sent the
659 * last data packet or window adjust ack.
660 */
661 int remlocwin;
662 /*
39d830e4 663 * These store the list of channel requests that haven't
0e443c99 664 * been acked.
665 */
39d830e4 666 struct outstanding_channel_request *chanreq_head, *chanreq_tail;
0e443c99 667 enum { THROTTLED, UNTHROTTLING, UNTHROTTLED } throttle_state;
5471d09a 668 } v2;
669 } v;
dacbd0e8 670 union {
32874aea 671 struct ssh_agent_channel {
672 unsigned char *message;
673 unsigned char msglen[4];
a03c9f9c 674 unsigned lensofar, totallen;
32874aea 675 } a;
676 struct ssh_x11_channel {
677 Socket s;
678 } x11;
d74d141c 679 struct ssh_pfd_channel {
680 Socket s;
681 } pfd;
dacbd0e8 682 } u;
683};
57476f6b 684
d74d141c 685/*
2e85c969 686 * 2-3-4 tree storing remote->local port forwardings. SSH-1 and SSH-2
687 * use this structure in different ways, reflecting SSH-2's
bc240b21 688 * altogether saner approach to port forwarding.
689 *
2e85c969 690 * In SSH-1, you arrange a remote forwarding by sending the server
bc240b21 691 * the remote port number, and the local destination host:port.
692 * When a connection comes in, the server sends you back that
693 * host:port pair, and you connect to it. This is a ready-made
694 * security hole if you're not on the ball: a malicious server
695 * could send you back _any_ host:port pair, so if you trustingly
696 * connect to the address it gives you then you've just opened the
697 * entire inside of your corporate network just by connecting
698 * through it to a dodgy SSH server. Hence, we must store a list of
699 * host:port pairs we _are_ trying to forward to, and reject a
700 * connection request from the server if it's not in the list.
701 *
2e85c969 702 * In SSH-2, each side of the connection minds its own business and
bc240b21 703 * doesn't send unnecessary information to the other. You arrange a
704 * remote forwarding by sending the server just the remote port
705 * number. When a connection comes in, the server tells you which
706 * of its ports was connected to; and _you_ have to remember what
707 * local host:port pair went with that port number.
708 *
2e85c969 709 * Hence, in SSH-1 this structure is indexed by destination
710 * host:port pair, whereas in SSH-2 it is indexed by source port.
d74d141c 711 */
fda2feb1 712struct ssh_portfwd; /* forward declaration */
713
d74d141c 714struct ssh_rportfwd {
bc240b21 715 unsigned sport, dport;
716 char dhost[256];
06fadff5 717 char *sportdesc;
fda2feb1 718 struct ssh_portfwd *pfrec;
d74d141c 719};
fda2feb1 720#define free_rportfwd(pf) ( \
721 ((pf) ? (sfree((pf)->sportdesc)) : (void)0 ), sfree(pf) )
722
723/*
724 * Separately to the rportfwd tree (which is for looking up port
725 * open requests from the server), a tree of _these_ structures is
726 * used to keep track of all the currently open port forwardings,
727 * so that we can reconfigure in mid-session if the user requests
728 * it.
729 */
730struct ssh_portfwd {
84328ddb 731 enum { DESTROY, KEEP, CREATE } status;
fda2feb1 732 int type;
733 unsigned sport, dport;
734 char *saddr, *daddr;
3fe92132 735 char *sserv, *dserv;
fda2feb1 736 struct ssh_rportfwd *remote;
05581745 737 int addressfamily;
fda2feb1 738 void *local;
739};
740#define free_portfwd(pf) ( \
3fe92132 741 ((pf) ? (sfree((pf)->saddr), sfree((pf)->daddr), \
742 sfree((pf)->sserv), sfree((pf)->dserv)) : (void)0 ), sfree(pf) )
d74d141c 743
57476f6b 744struct Packet {
dacd8872 745 long length; /* length of `data' actually used */
746 long forcepad; /* SSH-2: force padding to at least this length */
747 int type; /* only used for incoming packets */
748 unsigned long sequence; /* SSH-2 incoming sequence number */
749 unsigned char *data; /* allocated storage */
750 unsigned char *body; /* offset of payload within `data' */
751 long savedpos; /* temporary index into `data' (for strings) */
752 long maxlen; /* amount of storage allocated for `data' */
753 long encrypted_len; /* for SSH-2 total-size counting */
ff3187f6 754
755 /*
756 * State associated with packet logging
757 */
758 int logmode;
759 int nblanks;
760 struct logblank_t *blanks;
57476f6b 761};
762
1c1a7262 763static void ssh1_protocol(Ssh ssh, void *vin, int inlen,
ff3187f6 764 struct Packet *pktin);
1c1a7262 765static void ssh2_protocol(Ssh ssh, void *vin, int inlen,
ff3187f6 766 struct Packet *pktin);
b09eaa88 767static void ssh1_protocol_setup(Ssh ssh);
768static void ssh2_protocol_setup(Ssh ssh);
51470298 769static void ssh_size(void *handle, int width, int height);
770static void ssh_special(void *handle, Telnet_Special);
5471d09a 771static int ssh2_try_send(struct ssh_channel *c);
51470298 772static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len);
773static void ssh_throttle_all(Ssh ssh, int enable, int bufsize);
9fca3f8c 774static void ssh2_set_window(struct ssh_channel *c, int newwin);
51470298 775static int ssh_sendbuffer(void *handle);
ac934965 776static int ssh_do_close(Ssh ssh, int notify_exit);
ff3187f6 777static unsigned long ssh_pkt_getuint32(struct Packet *pkt);
778static int ssh2_pkt_getbool(struct Packet *pkt);
779static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length);
9442dd57 780static void ssh2_timer(void *ctx, long now);
1c1a7262 781static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
9442dd57 782 struct Packet *pktin);
39d830e4 783static void ssh2_msg_unexpected(Ssh ssh, struct Packet *pktin);
57476f6b 784
51470298 785struct rdpkt1_state_tag {
57476f6b 786 long len, pad, biglen, to_read;
787 unsigned long realcrc, gotcrc;
788 unsigned char *p;
789 int i;
790 int chunk;
ff3187f6 791 struct Packet *pktin;
51470298 792};
57476f6b 793
51470298 794struct rdpkt2_state_tag {
960e736a 795 long len, pad, payload, packetlen, maclen;
796 int i;
797 int cipherblk;
798 unsigned long incoming_sequence;
ff3187f6 799 struct Packet *pktin;
51470298 800};
801
06fadff5 802struct queued_handler;
803struct queued_handler {
804 int msg1, msg2;
805 chandler_fn_t handler;
806 void *ctx;
807 struct queued_handler *next;
808};
b09eaa88 809
51470298 810struct ssh_tag {
811 const struct plug_function_table *fn;
812 /* the above field _must_ be first in the structure */
813
4320baf7 814 char *v_c, *v_s;
b672f405 815 void *exhash;
51470298 816
817 Socket s;
818
b9d7bcad 819 void *ldisc;
a8327734 820 void *logctx;
b9d7bcad 821
51470298 822 unsigned char session_key[32];
823 int v1_compressing;
824 int v1_remote_protoflags;
825 int v1_local_protoflags;
826 int agentfwd_enabled;
827 int X11_fwd_enabled;
828 int remote_bugs;
829 const struct ssh_cipher *cipher;
371e569c 830 void *v1_cipher_ctx;
0183b242 831 void *crcda_ctx;
51470298 832 const struct ssh2_cipher *cscipher, *sccipher;
371e569c 833 void *cs_cipher_ctx, *sc_cipher_ctx;
51470298 834 const struct ssh_mac *csmac, *scmac;
e0e1a00d 835 void *cs_mac_ctx, *sc_mac_ctx;
51470298 836 const struct ssh_compress *cscomp, *sccomp;
5366aed8 837 void *cs_comp_ctx, *sc_comp_ctx;
51470298 838 const struct ssh_kex *kex;
839 const struct ssh_signkey *hostkey;
754c0df9 840 unsigned char v2_session_id[SSH2_KEX_MAX_HASH_LEN];
b672f405 841 int v2_session_id_len;
27cd7fc2 842 void *kex_ctx;
51470298 843
844 char *savedhost;
845 int savedport;
846 int send_ok;
847 int echoing, editing;
848
849 void *frontend;
850
db219738 851 int ospeed, ispeed; /* temporaries */
51470298 852 int term_width, term_height;
853
854 tree234 *channels; /* indexed by local id */
855 struct ssh_channel *mainchan; /* primary session channel */
feb02b4e 856 int ncmode; /* is primary channel direct-tcpip? */
51470298 857 int exitcode;
ac934965 858 int close_expected;
9e296bfa 859 int clean_exit;
51470298 860
fda2feb1 861 tree234 *rportfwds, *portfwds;
51470298 862
863 enum {
864 SSH_STATE_PREPACKET,
865 SSH_STATE_BEFORE_SIZE,
866 SSH_STATE_INTERMED,
867 SSH_STATE_SESSION,
868 SSH_STATE_CLOSED
869 } state;
870
871 int size_needed, eof_needed;
bc06669b 872 int sent_console_eof;
4a202fc6 873 int got_pty; /* affects EOF behaviour on main channel */
51470298 874
590f6a5f 875 struct Packet **queue;
876 int queuelen, queuesize;
877 int queueing;
51470298 878 unsigned char *deferred_send_data;
879 int deferred_len, deferred_size;
880
881 /*
882 * Gross hack: pscp will try to start SFTP but fall back to
883 * scp1 if that fails. This variable is the means by which
884 * scp.c can reach into the SSH code and find out which one it
885 * got.
886 */
887 int fallback_cmd;
888
6bbce591 889 bufchain banner; /* accumulates banners during do_ssh2_authconn */
51470298 890
acab36bc 891 Pkt_KCtx pkt_kctx;
892 Pkt_ACtx pkt_actx;
51470298 893
8def70c3 894 struct X11Display *x11disp;
302121de 895
51470298 896 int version;
ff68564b 897 int conn_throttle_count;
51470298 898 int overall_bufsize;
899 int throttled_all;
900 int v1_stdout_throttling;
a8756193 901 unsigned long v2_outgoing_sequence;
51470298 902
903 int ssh1_rdpkt_crstate;
904 int ssh2_rdpkt_crstate;
51470298 905 int ssh_gotdata_crstate;
b09eaa88 906 int do_ssh1_connection_crstate;
51470298 907
908 void *do_ssh_init_state;
909 void *do_ssh1_login_state;
910 void *do_ssh2_transport_state;
911 void *do_ssh2_authconn_state;
912
913 struct rdpkt1_state_tag rdpkt1_state;
914 struct rdpkt2_state_tag rdpkt2_state;
915
2e85c969 916 /* SSH-1 and SSH-2 use this for different things, but both use it */
b09eaa88 917 int protocol_initial_phase_done;
918
1c1a7262 919 void (*protocol) (Ssh ssh, void *vin, int inlen,
ff3187f6 920 struct Packet *pkt);
921 struct Packet *(*s_rdpkt) (Ssh ssh, unsigned char **data, int *datalen);
86916870 922
923 /*
4a693cfc 924 * We maintain our own copy of a Conf structure here. That way,
925 * when we're passed a new one for reconfiguration, we can check
926 * the differences and potentially reconfigure port forwardings
927 * etc in mid-session.
86916870 928 */
4a693cfc 929 Conf *conf;
930
931 /*
932 * Values cached out of conf so as to avoid the tree234 lookup
933 * cost every time they're used.
934 */
935 int logomitdata;
936
937 /*
938 * Dynamically allocated username string created during SSH
939 * login. Stored in here rather than in the coroutine state so
940 * that it'll be reliably freed if we shut down the SSH session
941 * at some unexpected moment.
942 */
943 char *username;
839f10db 944
945 /*
3d9449a1 946 * Used to transfer data back from async callbacks.
839f10db 947 */
948 void *agent_response;
949 int agent_response_len;
3d9449a1 950 int user_response;
951
952 /*
953 * The SSH connection can be set as `frozen', meaning we are
954 * not currently accepting incoming data from the network. This
955 * is slightly more serious than setting the _socket_ as
956 * frozen, because we may already have had data passed to us
957 * from the network which we need to delay processing until
958 * after the freeze is lifted, so we also need a bufchain to
959 * store that data.
960 */
961 int frozen;
962 bufchain queued_incoming_data;
b09eaa88 963
964 /*
965 * Dispatch table for packet types that we may have to deal
966 * with at any time.
967 */
968 handler_fn_t packet_dispatch[256];
39934deb 969
970 /*
06fadff5 971 * Queues of one-off handler functions for success/failure
972 * indications from a request.
973 */
974 struct queued_handler *qhead, *qtail;
bc074147 975 handler_fn_t q_saved_handler1, q_saved_handler2;
06fadff5 976
977 /*
39934deb 978 * This module deals with sending keepalives.
979 */
980 Pinger pinger;
9442dd57 981
982 /*
983 * Track incoming and outgoing data sizes and time, for
984 * size-based rekeys.
985 */
986 unsigned long incoming_data_size, outgoing_data_size, deferred_data_size;
d57f70af 987 unsigned long max_data_size;
9442dd57 988 int kex_in_progress;
e6c1536e 989 long next_rekey, last_rekey;
e13bba36 990 char *deferred_rekey_reason; /* points to STATIC string; don't free */
42af6a67 991
992 /*
993 * Fully qualified host name, which we need if doing GSSAPI.
994 */
995 char *fullhostname;
1e00c92b 996
997#ifndef NO_GSSAPI
998 /*
999 * GSSAPI libraries for this session.
1000 */
1001 struct ssh_gss_liblist *gsslibs;
1002#endif
51470298 1003};
960e736a 1004
382908ad 1005#define logevent(s) logevent(ssh->frontend, s)
a8327734 1006
1007/* logevent, only printf-formatted. */
cbe2d68f 1008static void logeventf(Ssh ssh, const char *fmt, ...)
a8327734 1009{
1010 va_list ap;
57356d63 1011 char *buf;
a8327734 1012
1013 va_start(ap, fmt);
57356d63 1014 buf = dupvprintf(fmt, ap);
a8327734 1015 va_end(ap);
57356d63 1016 logevent(buf);
57356d63 1017 sfree(buf);
a8327734 1018}
1019
6b5cf8b4 1020#define bombout(msg) \
1021 do { \
1022 char *text = dupprintf msg; \
ac934965 1023 ssh_do_close(ssh, FALSE); \
6b5cf8b4 1024 logevent(text); \
1025 connection_fatal(ssh->frontend, "%s", text); \
1026 sfree(text); \
1027 } while (0)
a8327734 1028
9a10ecf4 1029/* Functions to leave bits out of the SSH packet log file. */
1030
ff3187f6 1031static void dont_log_password(Ssh ssh, struct Packet *pkt, int blanktype)
9a10ecf4 1032{
4a693cfc 1033 if (conf_get_int(ssh->conf, CONF_logomitpass))
ff3187f6 1034 pkt->logmode = blanktype;
9a10ecf4 1035}
1036
ff3187f6 1037static void dont_log_data(Ssh ssh, struct Packet *pkt, int blanktype)
9a10ecf4 1038{
4a693cfc 1039 if (ssh->logomitdata)
ff3187f6 1040 pkt->logmode = blanktype;
9a10ecf4 1041}
1042
ff3187f6 1043static void end_log_omission(Ssh ssh, struct Packet *pkt)
9a10ecf4 1044{
ff3187f6 1045 pkt->logmode = PKTLOG_EMIT;
9a10ecf4 1046}
1047
4a693cfc 1048/* Helper function for common bits of parsing ttymodes. */
1049static void parse_ttymodes(Ssh ssh,
c6ccd5c2 1050 void (*do_mode)(void *data, char *mode, char *val),
1051 void *data)
1052{
4a693cfc 1053 char *key, *val;
1054
1055 for (val = conf_get_str_strs(ssh->conf, CONF_ttymodes, NULL, &key);
1056 val != NULL;
1057 val = conf_get_str_strs(ssh->conf, CONF_ttymodes, key, &key)) {
1058 /*
1059 * val[0] is either 'V', indicating that an explicit value
1060 * follows it, or 'A' indicating that we should pass the
1061 * value through from the local environment via get_ttymode.
1062 */
1063 if (val[0] == 'A')
1064 val = get_ttymode(ssh->frontend, key);
c6ccd5c2 1065 else
4a693cfc 1066 val++; /* skip the 'V' */
c6ccd5c2 1067 if (val)
4a693cfc 1068 do_mode(data, key, val);
c6ccd5c2 1069 }
1070}
1071
32874aea 1072static int ssh_channelcmp(void *av, void *bv)
1073{
1074 struct ssh_channel *a = (struct ssh_channel *) av;
1075 struct ssh_channel *b = (struct ssh_channel *) bv;
1076 if (a->localid < b->localid)
1077 return -1;
1078 if (a->localid > b->localid)
1079 return +1;
dacbd0e8 1080 return 0;
1081}
32874aea 1082static int ssh_channelfind(void *av, void *bv)
1083{
1084 unsigned *a = (unsigned *) av;
1085 struct ssh_channel *b = (struct ssh_channel *) bv;
1086 if (*a < b->localid)
1087 return -1;
1088 if (*a > b->localid)
1089 return +1;
dacbd0e8 1090 return 0;
1091}
1092
bc240b21 1093static int ssh_rportcmp_ssh1(void *av, void *bv)
d74d141c 1094{
1095 struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
1096 struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
1097 int i;
bc240b21 1098 if ( (i = strcmp(a->dhost, b->dhost)) != 0)
d74d141c 1099 return i < 0 ? -1 : +1;
bc240b21 1100 if (a->dport > b->dport)
1101 return +1;
1102 if (a->dport < b->dport)
1103 return -1;
1104 return 0;
1105}
1106
1107static int ssh_rportcmp_ssh2(void *av, void *bv)
1108{
1109 struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;
1110 struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;
cdcbdf3b 1111
bc240b21 1112 if (a->sport > b->sport)
d74d141c 1113 return +1;
bc240b21 1114 if (a->sport < b->sport)
1115 return -1;
d74d141c 1116 return 0;
1117}
1118
fda2feb1 1119/*
1120 * Special form of strcmp which can cope with NULL inputs. NULL is
1121 * defined to sort before even the empty string.
1122 */
1123static int nullstrcmp(const char *a, const char *b)
1124{
1125 if (a == NULL && b == NULL)
1126 return 0;
1127 if (a == NULL)
1128 return -1;
1129 if (b == NULL)
1130 return +1;
1131 return strcmp(a, b);
1132}
1133
1134static int ssh_portcmp(void *av, void *bv)
1135{
1136 struct ssh_portfwd *a = (struct ssh_portfwd *) av;
1137 struct ssh_portfwd *b = (struct ssh_portfwd *) bv;
1138 int i;
1139 if (a->type > b->type)
1140 return +1;
1141 if (a->type < b->type)
1142 return -1;
84328ddb 1143 if (a->addressfamily > b->addressfamily)
1144 return +1;
1145 if (a->addressfamily < b->addressfamily)
1146 return -1;
fda2feb1 1147 if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0)
1148 return i < 0 ? -1 : +1;
1149 if (a->sport > b->sport)
1150 return +1;
1151 if (a->sport < b->sport)
1152 return -1;
1153 if (a->type != 'D') {
1154 if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0)
1155 return i < 0 ? -1 : +1;
1156 if (a->dport > b->dport)
1157 return +1;
1158 if (a->dport < b->dport)
1159 return -1;
1160 }
1161 return 0;
1162}
1163
51470298 1164static int alloc_channel_id(Ssh ssh)
32874aea 1165{
260f3dec 1166 const unsigned CHANNEL_NUMBER_OFFSET = 256;
1167 unsigned low, high, mid;
d2371c81 1168 int tsize;
1169 struct ssh_channel *c;
1170
1171 /*
1172 * First-fit allocation of channel numbers: always pick the
1173 * lowest unused one. To do this, binary-search using the
1174 * counted B-tree to find the largest channel ID which is in a
1175 * contiguous sequence from the beginning. (Precisely
1176 * everything in that sequence must have ID equal to its tree
1177 * index plus CHANNEL_NUMBER_OFFSET.)
1178 */
51470298 1179 tsize = count234(ssh->channels);
d2371c81 1180
32874aea 1181 low = -1;
1182 high = tsize;
d2371c81 1183 while (high - low > 1) {
1184 mid = (high + low) / 2;
51470298 1185 c = index234(ssh->channels, mid);
d2371c81 1186 if (c->localid == mid + CHANNEL_NUMBER_OFFSET)
1187 low = mid; /* this one is fine */
1188 else
1189 high = mid; /* this one is past it */
1190 }
1191 /*
1192 * Now low points to either -1, or the tree index of the
1193 * largest ID in the initial sequence.
1194 */
1195 {
1196 unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET;
51470298 1197 assert(NULL == find234(ssh->channels, &i, ssh_channelfind));
d2371c81 1198 }
1199 return low + 1 + CHANNEL_NUMBER_OFFSET;
1200}
1201
edd0cb8a 1202static void c_write_stderr(int trusted, const char *buf, int len)
1203{
1204 int i;
1205 for (i = 0; i < len; i++)
667deca9 1206 if (buf[i] != '\r' && (trusted || buf[i] == '\n' || (buf[i] & 0x60)))
edd0cb8a 1207 fputc(buf[i], stderr);
1208}
1209
9fab77dc 1210static void c_write(Ssh ssh, const char *buf, int len)
32874aea 1211{
edd0cb8a 1212 if (flags & FLAG_STDERR)
1213 c_write_stderr(1, buf, len);
1214 else
1215 from_backend(ssh->frontend, 1, buf, len);
3bdaf79d 1216}
1217
9fab77dc 1218static void c_write_untrusted(Ssh ssh, const char *buf, int len)
32874aea 1219{
edd0cb8a 1220 if (flags & FLAG_STDERR)
1221 c_write_stderr(0, buf, len);
1222 else
1223 from_backend_untrusted(ssh->frontend, buf, len);
a209e957 1224}
1225
9fab77dc 1226static void c_write_str(Ssh ssh, const char *buf)
32874aea 1227{
51470298 1228 c_write(ssh, buf, strlen(buf));
1408a877 1229}
1230
ff3187f6 1231static void ssh_free_packet(struct Packet *pkt)
1232{
1233 sfree(pkt->data);
1234 sfree(pkt);
1235}
1236static struct Packet *ssh_new_packet(void)
1237{
1238 struct Packet *pkt = snew(struct Packet);
1239
dacd8872 1240 pkt->body = pkt->data = NULL;
ff3187f6 1241 pkt->maxlen = 0;
1242 pkt->logmode = PKTLOG_EMIT;
1243 pkt->nblanks = 0;
1244 pkt->blanks = NULL;
1245
1246 return pkt;
1247}
1248
fb09bf1c 1249/*
1250 * Collect incoming data in the incoming packet buffer.
e5574168 1251 * Decipher and verify the packet when it is completely read.
1252 * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets.
fb09bf1c 1253 * Update the *data and *datalen variables.
ff3187f6 1254 * Return a Packet structure when a packet is completed.
fb09bf1c 1255 */
ff3187f6 1256static struct Packet *ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
fb09bf1c 1257{
51470298 1258 struct rdpkt1_state_tag *st = &ssh->rdpkt1_state;
374330e2 1259
51470298 1260 crBegin(ssh->ssh1_rdpkt_crstate);
374330e2 1261
ff3187f6 1262 st->pktin = ssh_new_packet();
1263
1264 st->pktin->type = 0;
1265 st->pktin->length = 0;
374330e2 1266
57476f6b 1267 for (st->i = st->len = 0; st->i < 4; st->i++) {
fb09bf1c 1268 while ((*datalen) == 0)
ff3187f6 1269 crReturn(NULL);
57476f6b 1270 st->len = (st->len << 8) + **data;
fb09bf1c 1271 (*data)++, (*datalen)--;
1272 }
374330e2 1273
57476f6b 1274 st->pad = 8 - (st->len % 8);
1275 st->biglen = st->len + st->pad;
ff3187f6 1276 st->pktin->length = st->len - 5;
fb09bf1c 1277
ae0500e5 1278 if (st->biglen < 0) {
1279 bombout(("Extremely large packet length from server suggests"
1280 " data stream corruption"));
ff3187f6 1281 ssh_free_packet(st->pktin);
1282 crStop(NULL);
ae0500e5 1283 }
1284
ff3187f6 1285 st->pktin->maxlen = st->biglen;
1286 st->pktin->data = snewn(st->biglen + APIEXTRA, unsigned char);
374330e2 1287
57476f6b 1288 st->to_read = st->biglen;
ff3187f6 1289 st->p = st->pktin->data;
57476f6b 1290 while (st->to_read > 0) {
32874aea 1291 st->chunk = st->to_read;
fb09bf1c 1292 while ((*datalen) == 0)
ff3187f6 1293 crReturn(NULL);
57476f6b 1294 if (st->chunk > (*datalen))
1295 st->chunk = (*datalen);
1296 memcpy(st->p, *data, st->chunk);
1297 *data += st->chunk;
1298 *datalen -= st->chunk;
1299 st->p += st->chunk;
1300 st->to_read -= st->chunk;
fb09bf1c 1301 }
374330e2 1302
ff3187f6 1303 if (ssh->cipher && detect_attack(ssh->crcda_ctx, st->pktin->data,
0183b242 1304 st->biglen, NULL)) {
6b5cf8b4 1305 bombout(("Network attack (CRC compensation) detected!"));
ff3187f6 1306 ssh_free_packet(st->pktin);
1307 crStop(NULL);
9a3a93a5 1308 }
1309
51470298 1310 if (ssh->cipher)
ff3187f6 1311 ssh->cipher->decrypt(ssh->v1_cipher_ctx, st->pktin->data, st->biglen);
374330e2 1312
ff3187f6 1313 st->realcrc = crc32_compute(st->pktin->data, st->biglen - 4);
1314 st->gotcrc = GET_32BIT(st->pktin->data + st->biglen - 4);
57476f6b 1315 if (st->gotcrc != st->realcrc) {
6b5cf8b4 1316 bombout(("Incorrect CRC received on packet"));
ff3187f6 1317 ssh_free_packet(st->pktin);
1318 crStop(NULL);
fb09bf1c 1319 }
572f871e 1320
ff3187f6 1321 st->pktin->body = st->pktin->data + st->pad + 1;
1322 st->pktin->savedpos = 0;
4ba9b64b 1323
51470298 1324 if (ssh->v1_compressing) {
4ba9b64b 1325 unsigned char *decompblk;
1326 int decomplen;
36b8d9bb 1327 if (!zlib_decompress_block(ssh->sc_comp_ctx,
ff3187f6 1328 st->pktin->body - 1, st->pktin->length + 1,
36b8d9bb 1329 &decompblk, &decomplen)) {
1330 bombout(("Zlib decompression encountered invalid data"));
ff3187f6 1331 ssh_free_packet(st->pktin);
1332 crStop(NULL);
36b8d9bb 1333 }
4ba9b64b 1334
ff3187f6 1335 if (st->pktin->maxlen < st->pad + decomplen) {
1336 st->pktin->maxlen = st->pad + decomplen;
1337 st->pktin->data = sresize(st->pktin->data,
1338 st->pktin->maxlen + APIEXTRA,
3d88e64d 1339 unsigned char);
ff3187f6 1340 st->pktin->body = st->pktin->data + st->pad + 1;
4ba9b64b 1341 }
1342
ff3187f6 1343 memcpy(st->pktin->body - 1, decompblk, decomplen);
dcbde236 1344 sfree(decompblk);
ff3187f6 1345 st->pktin->length = decomplen - 1;
4ba9b64b 1346 }
1347
ff3187f6 1348 st->pktin->type = st->pktin->body[-1];
00db133f 1349
9a10ecf4 1350 /*
1351 * Log incoming packet, possibly omitting sensitive fields.
1352 */
1353 if (ssh->logctx) {
1354 int nblanks = 0;
1355 struct logblank_t blank;
4a693cfc 1356 if (ssh->logomitdata) {
9a10ecf4 1357 int do_blank = FALSE, blank_prefix = 0;
1358 /* "Session data" packets - omit the data field */
ff3187f6 1359 if ((st->pktin->type == SSH1_SMSG_STDOUT_DATA) ||
1360 (st->pktin->type == SSH1_SMSG_STDERR_DATA)) {
9a10ecf4 1361 do_blank = TRUE; blank_prefix = 4;
a2724fba 1362 } else if (st->pktin->type == SSH1_MSG_CHANNEL_DATA) {
1363 do_blank = TRUE; blank_prefix = 8;
9a10ecf4 1364 }
1365 if (do_blank) {
1366 blank.offset = blank_prefix;
ff3187f6 1367 blank.len = st->pktin->length;
9a10ecf4 1368 blank.type = PKTLOG_OMIT;
1369 nblanks = 1;
1370 }
1371 }
a8327734 1372 log_packet(ssh->logctx,
ff3187f6 1373 PKT_INCOMING, st->pktin->type,
1374 ssh1_pkt_type(st->pktin->type),
1375 st->pktin->body, st->pktin->length,
0d13bfcd 1376 nblanks, &blank, NULL);
9a10ecf4 1377 }
00db133f 1378
ff3187f6 1379 crFinish(st->pktin);
fb09bf1c 1380}
1381
ff3187f6 1382static struct Packet *ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
e5574168 1383{
51470298 1384 struct rdpkt2_state_tag *st = &ssh->rdpkt2_state;
e5574168 1385
51470298 1386 crBegin(ssh->ssh2_rdpkt_crstate);
e5574168 1387
ff3187f6 1388 st->pktin = ssh_new_packet();
1389
1390 st->pktin->type = 0;
1391 st->pktin->length = 0;
51470298 1392 if (ssh->sccipher)
1393 st->cipherblk = ssh->sccipher->blksize;
e5574168 1394 else
32874aea 1395 st->cipherblk = 8;
960e736a 1396 if (st->cipherblk < 8)
32874aea 1397 st->cipherblk = 8;
9916cc1e 1398 st->maclen = ssh->scmac ? ssh->scmac->len : 0;
960e736a 1399
9916cc1e 1400 if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_IS_CBC) &&
1401 ssh->scmac) {
1402 /*
1403 * When dealing with a CBC-mode cipher, we want to avoid the
1404 * possibility of an attacker's tweaking the ciphertext stream
1405 * so as to cause us to feed the same block to the block
1406 * cipher more than once and thus leak information
1407 * (VU#958563). The way we do this is not to take any
1408 * decisions on the basis of anything we've decrypted until
1409 * we've verified it with a MAC. That includes the packet
1410 * length, so we just read data and check the MAC repeatedly,
1411 * and when the MAC passes, see if the length we've got is
1412 * plausible.
1413 */
1414
1415 /* May as well allocate the whole lot now. */
1416 st->pktin->data = snewn(OUR_V2_PACKETLIMIT + st->maclen + APIEXTRA,
1417 unsigned char);
1418
1419 /* Read an amount corresponding to the MAC. */
1420 for (st->i = 0; st->i < st->maclen; st->i++) {
1421 while ((*datalen) == 0)
1422 crReturn(NULL);
1423 st->pktin->data[st->i] = *(*data)++;
1424 (*datalen)--;
1425 }
e5574168 1426
9916cc1e 1427 st->packetlen = 0;
1428 {
1429 unsigned char seq[4];
1430 ssh->scmac->start(ssh->sc_mac_ctx);
1431 PUT_32BIT(seq, st->incoming_sequence);
1432 ssh->scmac->bytes(ssh->sc_mac_ctx, seq, 4);
1433 }
4252c9cc 1434
9916cc1e 1435 for (;;) { /* Once around this loop per cipher block. */
1436 /* Read another cipher-block's worth, and tack it onto the end. */
1437 for (st->i = 0; st->i < st->cipherblk; st->i++) {
1438 while ((*datalen) == 0)
1439 crReturn(NULL);
1440 st->pktin->data[st->packetlen+st->maclen+st->i] = *(*data)++;
1441 (*datalen)--;
1442 }
1443 /* Decrypt one more block (a little further back in the stream). */
1444 ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
1445 st->pktin->data + st->packetlen,
1446 st->cipherblk);
1447 /* Feed that block to the MAC. */
1448 ssh->scmac->bytes(ssh->sc_mac_ctx,
1449 st->pktin->data + st->packetlen, st->cipherblk);
1450 st->packetlen += st->cipherblk;
1451 /* See if that gives us a valid packet. */
1452 if (ssh->scmac->verresult(ssh->sc_mac_ctx,
1453 st->pktin->data + st->packetlen) &&
1454 (st->len = GET_32BIT(st->pktin->data)) + 4 == st->packetlen)
1455 break;
1456 if (st->packetlen >= OUR_V2_PACKETLIMIT) {
1457 bombout(("No valid incoming packet found"));
1458 ssh_free_packet(st->pktin);
1459 crStop(NULL);
1460 }
1461 }
1462 st->pktin->maxlen = st->packetlen + st->maclen;
1463 st->pktin->data = sresize(st->pktin->data,
1464 st->pktin->maxlen + APIEXTRA,
1465 unsigned char);
1466 } else {
1467 st->pktin->data = snewn(st->cipherblk + APIEXTRA, unsigned char);
1468
1469 /*
1470 * Acquire and decrypt the first block of the packet. This will
1471 * contain the length and padding details.
1472 */
1473 for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) {
1474 while ((*datalen) == 0)
1475 crReturn(NULL);
1476 st->pktin->data[st->i] = *(*data)++;
1477 (*datalen)--;
1478 }
e5574168 1479
9916cc1e 1480 if (ssh->sccipher)
1481 ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
1482 st->pktin->data, st->cipherblk);
1483
1484 /*
1485 * Now get the length figure.
1486 */
1487 st->len = GET_32BIT(st->pktin->data);
1488
1489 /*
1490 * _Completely_ silly lengths should be stomped on before they
1491 * do us any more damage.
1492 */
1493 if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT ||
1494 (st->len + 4) % st->cipherblk != 0) {
1495 bombout(("Incoming packet was garbled on decryption"));
1496 ssh_free_packet(st->pktin);
1497 crStop(NULL);
1498 }
e5574168 1499
9916cc1e 1500 /*
1501 * So now we can work out the total packet length.
1502 */
1503 st->packetlen = st->len + 4;
1504
1505 /*
1506 * Allocate memory for the rest of the packet.
1507 */
1508 st->pktin->maxlen = st->packetlen + st->maclen;
1509 st->pktin->data = sresize(st->pktin->data,
1510 st->pktin->maxlen + APIEXTRA,
1511 unsigned char);
1512
1513 /*
1514 * Read and decrypt the remainder of the packet.
1515 */
1516 for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen;
1517 st->i++) {
1518 while ((*datalen) == 0)
1519 crReturn(NULL);
1520 st->pktin->data[st->i] = *(*data)++;
1521 (*datalen)--;
1522 }
1523 /* Decrypt everything _except_ the MAC. */
1524 if (ssh->sccipher)
1525 ssh->sccipher->decrypt(ssh->sc_cipher_ctx,
1526 st->pktin->data + st->cipherblk,
1527 st->packetlen - st->cipherblk);
1528
1529 /*
1530 * Check the MAC.
1531 */
1532 if (ssh->scmac
1533 && !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data,
1534 st->len + 4, st->incoming_sequence)) {
1535 bombout(("Incorrect MAC received on packet"));
1536 ssh_free_packet(st->pktin);
1537 crStop(NULL);
1538 }
1539 }
1540 /* Get and sanity-check the amount of random padding. */
1541 st->pad = st->pktin->data[4];
1542 if (st->pad < 4 || st->len - st->pad < 1) {
1543 bombout(("Invalid padding length on received packet"));
ff3187f6 1544 ssh_free_packet(st->pktin);
1545 crStop(NULL);
717dc483 1546 }
717dc483 1547 /*
e5574168 1548 * This enables us to deduce the payload length.
1549 */
960e736a 1550 st->payload = st->len - st->pad - 1;
e5574168 1551
ff3187f6 1552 st->pktin->length = st->payload + 5;
9442dd57 1553 st->pktin->encrypted_len = st->packetlen;
1554
b09eaa88 1555 st->pktin->sequence = st->incoming_sequence++;
e5574168 1556
4ba9b64b 1557 /*
1558 * Decompress packet payload.
1559 */
1560 {
1561 unsigned char *newpayload;
1562 int newlen;
51470298 1563 if (ssh->sccomp &&
5366aed8 1564 ssh->sccomp->decompress(ssh->sc_comp_ctx,
ff3187f6 1565 st->pktin->data + 5, st->pktin->length - 5,
51470298 1566 &newpayload, &newlen)) {
ff3187f6 1567 if (st->pktin->maxlen < newlen + 5) {
1568 st->pktin->maxlen = newlen + 5;
1569 st->pktin->data = sresize(st->pktin->data,
1570 st->pktin->maxlen + APIEXTRA,
3d88e64d 1571 unsigned char);
4ba9b64b 1572 }
ff3187f6 1573 st->pktin->length = 5 + newlen;
1574 memcpy(st->pktin->data + 5, newpayload, newlen);
dcbde236 1575 sfree(newpayload);
4ba9b64b 1576 }
1577 }
1578
ff3187f6 1579 st->pktin->savedpos = 6;
1580 st->pktin->body = st->pktin->data;
1581 st->pktin->type = st->pktin->data[5];
e5574168 1582
9a10ecf4 1583 /*
1584 * Log incoming packet, possibly omitting sensitive fields.
1585 */
1586 if (ssh->logctx) {
1587 int nblanks = 0;
1588 struct logblank_t blank;
4a693cfc 1589 if (ssh->logomitdata) {
9a10ecf4 1590 int do_blank = FALSE, blank_prefix = 0;
1591 /* "Session data" packets - omit the data field */
ff3187f6 1592 if (st->pktin->type == SSH2_MSG_CHANNEL_DATA) {
9a10ecf4 1593 do_blank = TRUE; blank_prefix = 8;
a2724fba 1594 } else if (st->pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA) {
1595 do_blank = TRUE; blank_prefix = 12;
9a10ecf4 1596 }
1597 if (do_blank) {
1598 blank.offset = blank_prefix;
ff3187f6 1599 blank.len = (st->pktin->length-6) - blank_prefix;
9a10ecf4 1600 blank.type = PKTLOG_OMIT;
1601 nblanks = 1;
1602 }
1603 }
ff3187f6 1604 log_packet(ssh->logctx, PKT_INCOMING, st->pktin->type,
acab36bc 1605 ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx,
1606 st->pktin->type),
ff3187f6 1607 st->pktin->data+6, st->pktin->length-6,
0d13bfcd 1608 nblanks, &blank, &st->pktin->sequence);
9a10ecf4 1609 }
00db133f 1610
ff3187f6 1611 crFinish(st->pktin);
e5574168 1612}
1613
dacd8872 1614static int s_wrpkt_prepare(Ssh ssh, struct Packet *pkt, int *offset_p)
32874aea 1615{
dacd8872 1616 int pad, biglen, i, pktoffs;
374330e2 1617 unsigned long crc;
fd7a4aad 1618#ifdef __SC__
1619 /*
1620 * XXX various versions of SC (including 8.8.4) screw up the
1621 * register allocation in this function and use the same register
1622 * (D6) for len and as a temporary, with predictable results. The
1623 * following sledgehammer prevents this.
1624 */
1625 volatile
1626#endif
1627 int len;
374330e2 1628
a8327734 1629 if (ssh->logctx)
dacd8872 1630 log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[12],
1631 ssh1_pkt_type(pkt->data[12]),
1632 pkt->body, pkt->length - (pkt->body - pkt->data),
0d13bfcd 1633 pkt->nblanks, pkt->blanks, NULL);
ff3187f6 1634 sfree(pkt->blanks); pkt->blanks = NULL;
1635 pkt->nblanks = 0;
00db133f 1636
51470298 1637 if (ssh->v1_compressing) {
4ba9b64b 1638 unsigned char *compblk;
1639 int complen;
5366aed8 1640 zlib_compress_block(ssh->cs_comp_ctx,
dacd8872 1641 pkt->data + 12, pkt->length - 12,
4ba9b64b 1642 &compblk, &complen);
48406e6b 1643 ssh_pkt_ensure(pkt, complen + 2); /* just in case it's got bigger */
dacd8872 1644 memcpy(pkt->data + 12, compblk, complen);
dcbde236 1645 sfree(compblk);
dacd8872 1646 pkt->length = complen + 12;
4ba9b64b 1647 }
1648
dacd8872 1649 ssh_pkt_ensure(pkt, pkt->length + 4); /* space for CRC */
1650 pkt->length += 4;
1651 len = pkt->length - 4 - 8; /* len(type+data+CRC) */
32874aea 1652 pad = 8 - (len % 8);
dacd8872 1653 pktoffs = 8 - pad;
1654 biglen = len + pad; /* len(padding+type+data+CRC) */
374330e2 1655
dacd8872 1656 for (i = pktoffs; i < 4+8; i++)
1657 pkt->data[i] = random_byte();
1658 crc = crc32_compute(pkt->data + pktoffs + 4, biglen - 4); /* all ex len */
1659 PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc);
1660 PUT_32BIT(pkt->data + pktoffs, len);
374330e2 1661
51470298 1662 if (ssh->cipher)
dacd8872 1663 ssh->cipher->encrypt(ssh->v1_cipher_ctx,
1664 pkt->data + pktoffs + 4, biglen);
374330e2 1665
dacd8872 1666 if (offset_p) *offset_p = pktoffs;
1667 return biglen + 4; /* len(length+padding+type+data+CRC) */
39065bed 1668}
1669
bf8a49a1 1670static int s_write(Ssh ssh, void *data, int len)
1671{
ff05dfef 1672 if (ssh->logctx)
0d13bfcd 1673 log_packet(ssh->logctx, PKT_OUTGOING, -1, NULL, data, len,
1674 0, NULL, NULL);
bf8a49a1 1675 return sk_write(ssh->s, (char *)data, len);
1676}
1677
ff3187f6 1678static void s_wrpkt(Ssh ssh, struct Packet *pkt)
32874aea 1679{
dacd8872 1680 int len, backlog, offset;
1681 len = s_wrpkt_prepare(ssh, pkt, &offset);
bf8a49a1 1682 backlog = s_write(ssh, pkt->data + offset, len);
5471d09a 1683 if (backlog > SSH_MAX_BACKLOG)
51470298 1684 ssh_throttle_all(ssh, 1, backlog);
dacd8872 1685 ssh_free_packet(pkt);
39065bed 1686}
1687
ff3187f6 1688static void s_wrpkt_defer(Ssh ssh, struct Packet *pkt)
32874aea 1689{
dacd8872 1690 int len, offset;
1691 len = s_wrpkt_prepare(ssh, pkt, &offset);
51470298 1692 if (ssh->deferred_len + len > ssh->deferred_size) {
1693 ssh->deferred_size = ssh->deferred_len + len + 128;
3d88e64d 1694 ssh->deferred_send_data = sresize(ssh->deferred_send_data,
1695 ssh->deferred_size,
1696 unsigned char);
39065bed 1697 }
dacd8872 1698 memcpy(ssh->deferred_send_data + ssh->deferred_len,
1699 pkt->data + offset, len);
51470298 1700 ssh->deferred_len += len;
dacd8872 1701 ssh_free_packet(pkt);
374330e2 1702}
1703
fb09bf1c 1704/*
dacd8872 1705 * Construct a SSH-1 packet with the specified contents.
1706 * (This all-at-once interface used to be the only one, but now SSH-1
1707 * packets can also be constructed incrementally.)
fb09bf1c 1708 */
dacd8872 1709static struct Packet *construct_packet(Ssh ssh, int pkttype, va_list ap)
fb09bf1c 1710{
dacd8872 1711 int argtype;
7cca0d81 1712 Bignum bn;
ff3187f6 1713 struct Packet *pkt;
fb09bf1c 1714
dacd8872 1715 pkt = ssh1_pkt_init(pkttype);
fb09bf1c 1716
dacd8872 1717 while ((argtype = va_arg(ap, int)) != PKT_END) {
1718 unsigned char *argp, argchar;
8a9977e5 1719 char *sargp;
dacd8872 1720 unsigned long argint;
1721 int arglen;
fb09bf1c 1722 switch (argtype) {
9a10ecf4 1723 /* Actual fields in the packet */
fb09bf1c 1724 case PKT_INT:
dacd8872 1725 argint = va_arg(ap, int);
1726 ssh_pkt_adduint32(pkt, argint);
fb09bf1c 1727 break;
1728 case PKT_CHAR:
dacd8872 1729 argchar = (unsigned char) va_arg(ap, int);
1730 ssh_pkt_addbyte(pkt, argchar);
fb09bf1c 1731 break;
1732 case PKT_DATA:
dacd8872 1733 argp = va_arg(ap, unsigned char *);
1734 arglen = va_arg(ap, int);
1735 ssh_pkt_adddata(pkt, argp, arglen);
fb09bf1c 1736 break;
1737 case PKT_STR:
8a9977e5 1738 sargp = va_arg(ap, char *);
1739 ssh_pkt_addstring(pkt, sargp);
fb09bf1c 1740 break;
7cca0d81 1741 case PKT_BIGNUM:
dacd8872 1742 bn = va_arg(ap, Bignum);
1743 ssh1_pkt_addmp(pkt, bn);
9a10ecf4 1744 break;
1745 /* Tokens for modifications to packet logging */
1746 case PKTT_PASSWORD:
ff3187f6 1747 dont_log_password(ssh, pkt, PKTLOG_BLANK);
9a10ecf4 1748 break;
1749 case PKTT_DATA:
ff3187f6 1750 dont_log_data(ssh, pkt, PKTLOG_OMIT);
7cca0d81 1751 break;
9a10ecf4 1752 case PKTT_OTHER:
ff3187f6 1753 end_log_omission(ssh, pkt);
9a10ecf4 1754 break;
1755 }
fb09bf1c 1756 }
ff3187f6 1757
1758 return pkt;
39065bed 1759}
fb09bf1c 1760
51470298 1761static void send_packet(Ssh ssh, int pkttype, ...)
32874aea 1762{
ff3187f6 1763 struct Packet *pkt;
dacd8872 1764 va_list ap;
1765 va_start(ap, pkttype);
1766 pkt = construct_packet(ssh, pkttype, ap);
1767 va_end(ap);
ff3187f6 1768 s_wrpkt(ssh, pkt);
fb09bf1c 1769}
1770
51470298 1771static void defer_packet(Ssh ssh, int pkttype, ...)
32874aea 1772{
ff3187f6 1773 struct Packet *pkt;
dacd8872 1774 va_list ap;
1775 va_start(ap, pkttype);
1776 pkt = construct_packet(ssh, pkttype, ap);
1777 va_end(ap);
ff3187f6 1778 s_wrpkt_defer(ssh, pkt);
39065bed 1779}
1780
32874aea 1781static int ssh_versioncmp(char *a, char *b)
1782{
9697bfd2 1783 char *ae, *be;
1784 unsigned long av, bv;
1785
43aa02a7 1786 av = strtoul(a, &ae, 10);
1787 bv = strtoul(b, &be, 10);
32874aea 1788 if (av != bv)
1789 return (av < bv ? -1 : +1);
1790 if (*ae == '.')
1791 ae++;
1792 if (*be == '.')
1793 be++;
43aa02a7 1794 av = strtoul(ae, &ae, 10);
1795 bv = strtoul(be, &be, 10);
32874aea 1796 if (av != bv)
1797 return (av < bv ? -1 : +1);
9697bfd2 1798 return 0;
1799}
1800
e5574168 1801/*
a92dd380 1802 * Utility routines for putting an SSH-protocol `string' and
b672f405 1803 * `uint32' into a hash state.
e5574168 1804 */
b672f405 1805static void hash_string(const struct ssh_hash *h, void *s, void *str, int len)
32874aea 1806{
e5574168 1807 unsigned char lenblk[4];
e5574168 1808 PUT_32BIT(lenblk, len);
b672f405 1809 h->bytes(s, lenblk, 4);
1810 h->bytes(s, str, len);
e5574168 1811}
1812
b672f405 1813static void hash_uint32(const struct ssh_hash *h, void *s, unsigned i)
32874aea 1814{
a92dd380 1815 unsigned char intblk[4];
1816 PUT_32BIT(intblk, i);
b672f405 1817 h->bytes(s, intblk, 4);
a92dd380 1818}
1819
7cca0d81 1820/*
dacd8872 1821 * Packet construction functions. Mostly shared between SSH-1 and SSH-2.
7cca0d81 1822 */
dacd8872 1823static void ssh_pkt_ensure(struct Packet *pkt, int length)
32874aea 1824{
ff3187f6 1825 if (pkt->maxlen < length) {
dacd8872 1826 unsigned char *body = pkt->body;
3d71de0b 1827 int offset = body ? body - pkt->data : 0;
ff3187f6 1828 pkt->maxlen = length + 256;
1829 pkt->data = sresize(pkt->data, pkt->maxlen + APIEXTRA, unsigned char);
dacd8872 1830 if (body) pkt->body = pkt->data + offset;
7cca0d81 1831 }
783415f8 1832}
dacd8872 1833static void ssh_pkt_adddata(struct Packet *pkt, void *data, int len)
32874aea 1834{
ff3187f6 1835 if (pkt->logmode != PKTLOG_EMIT) {
1836 pkt->nblanks++;
1837 pkt->blanks = sresize(pkt->blanks, pkt->nblanks, struct logblank_t);
dacd8872 1838 assert(pkt->body);
1839 pkt->blanks[pkt->nblanks-1].offset = pkt->length -
1840 (pkt->body - pkt->data);
ff3187f6 1841 pkt->blanks[pkt->nblanks-1].len = len;
1842 pkt->blanks[pkt->nblanks-1].type = pkt->logmode;
1843 }
1844 pkt->length += len;
dacd8872 1845 ssh_pkt_ensure(pkt, pkt->length);
ff3187f6 1846 memcpy(pkt->data + pkt->length - len, data, len);
7cca0d81 1847}
dacd8872 1848static void ssh_pkt_addbyte(struct Packet *pkt, unsigned char byte)
32874aea 1849{
dacd8872 1850 ssh_pkt_adddata(pkt, &byte, 1);
7cca0d81 1851}
ff3187f6 1852static void ssh2_pkt_addbool(struct Packet *pkt, unsigned char value)
32874aea 1853{
dacd8872 1854 ssh_pkt_adddata(pkt, &value, 1);
7cca0d81 1855}
dacd8872 1856static void ssh_pkt_adduint32(struct Packet *pkt, unsigned long value)
32874aea 1857{
7cca0d81 1858 unsigned char x[4];
1859 PUT_32BIT(x, value);
dacd8872 1860 ssh_pkt_adddata(pkt, x, 4);
7cca0d81 1861}
dacd8872 1862static void ssh_pkt_addstring_start(struct Packet *pkt)
32874aea 1863{
dacd8872 1864 ssh_pkt_adduint32(pkt, 0);
ff3187f6 1865 pkt->savedpos = pkt->length;
7cca0d81 1866}
dacd8872 1867static void ssh_pkt_addstring_str(struct Packet *pkt, char *data)
32874aea 1868{
dacd8872 1869 ssh_pkt_adddata(pkt, data, strlen(data));
ff3187f6 1870 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
7cca0d81 1871}
dacd8872 1872static void ssh_pkt_addstring_data(struct Packet *pkt, char *data, int len)
32874aea 1873{
dacd8872 1874 ssh_pkt_adddata(pkt, data, len);
ff3187f6 1875 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
7cca0d81 1876}
dacd8872 1877static void ssh_pkt_addstring(struct Packet *pkt, char *data)
1878{
1879 ssh_pkt_addstring_start(pkt);
1880 ssh_pkt_addstring_str(pkt, data);
1881}
1882static void ssh1_pkt_addmp(struct Packet *pkt, Bignum b)
32874aea 1883{
dacd8872 1884 int len = ssh1_bignum_length(b);
8a9977e5 1885 unsigned char *data = snewn(len, unsigned char);
dacd8872 1886 (void) ssh1_write_bignum(data, b);
1887 ssh_pkt_adddata(pkt, data, len);
1888 sfree(data);
7cca0d81 1889}
d8baa528 1890static unsigned char *ssh2_mpint_fmt(Bignum b, int *len)
32874aea 1891{
7cca0d81 1892 unsigned char *p;
32874aea 1893 int i, n = (bignum_bitcount(b) + 7) / 8;
3d88e64d 1894 p = snewn(n + 1, unsigned char);
7cca0d81 1895 p[0] = 0;
3709bfe9 1896 for (i = 1; i <= n; i++)
32874aea 1897 p[i] = bignum_byte(b, n - i);
7cca0d81 1898 i = 0;
32874aea 1899 while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0)
1900 i++;
1901 memmove(p, p + i, n + 1 - i);
1902 *len = n + 1 - i;
7cca0d81 1903 return p;
1904}
ff3187f6 1905static void ssh2_pkt_addmp(struct Packet *pkt, Bignum b)
32874aea 1906{
7cca0d81 1907 unsigned char *p;
1908 int len;
1909 p = ssh2_mpint_fmt(b, &len);
dacd8872 1910 ssh_pkt_addstring_start(pkt);
1911 ssh_pkt_addstring_data(pkt, (char *)p, len);
dcbde236 1912 sfree(p);
7cca0d81 1913}
b185170a 1914
dacd8872 1915static struct Packet *ssh1_pkt_init(int pkt_type)
1916{
1917 struct Packet *pkt = ssh_new_packet();
1918 pkt->length = 4 + 8; /* space for length + max padding */
1919 ssh_pkt_addbyte(pkt, pkt_type);
1920 pkt->body = pkt->data + pkt->length;
1921 return pkt;
1922}
1923
1924/* For legacy code (SSH-1 and -2 packet construction used to be separate) */
1925#define ssh2_pkt_ensure(pkt, length) ssh_pkt_ensure(pkt, length)
1926#define ssh2_pkt_adddata(pkt, data, len) ssh_pkt_adddata(pkt, data, len)
1927#define ssh2_pkt_addbyte(pkt, byte) ssh_pkt_addbyte(pkt, byte)
1928#define ssh2_pkt_adduint32(pkt, value) ssh_pkt_adduint32(pkt, value)
1929#define ssh2_pkt_addstring_start(pkt) ssh_pkt_addstring_start(pkt)
1930#define ssh2_pkt_addstring_str(pkt, data) ssh_pkt_addstring_str(pkt, data)
1931#define ssh2_pkt_addstring_data(pkt, data, len) ssh_pkt_addstring_data(pkt, data, len)
1932#define ssh2_pkt_addstring(pkt, data) ssh_pkt_addstring(pkt, data)
1933
1934static struct Packet *ssh2_pkt_init(int pkt_type)
1935{
1936 struct Packet *pkt = ssh_new_packet();
3d71de0b 1937 pkt->length = 5; /* space for packet length + padding length */
dacd8872 1938 pkt->forcepad = 0;
1939 ssh_pkt_addbyte(pkt, (unsigned char) pkt_type);
3d71de0b 1940 pkt->body = pkt->data + pkt->length; /* after packet type */
dacd8872 1941 return pkt;
1942}
1943
b185170a 1944/*
2e85c969 1945 * Construct an SSH-2 final-form packet: compress it, encrypt it,
b185170a 1946 * put the MAC on it. Final packet, ready to be sent, is stored in
ff3187f6 1947 * pkt->data. Total length is returned.
b185170a 1948 */
ff3187f6 1949static int ssh2_pkt_construct(Ssh ssh, struct Packet *pkt)
32874aea 1950{
7cca0d81 1951 int cipherblk, maclen, padding, i;
7cca0d81 1952
a8327734 1953 if (ssh->logctx)
ff3187f6 1954 log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[5],
acab36bc 1955 ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->data[5]),
dacd8872 1956 pkt->body, pkt->length - (pkt->body - pkt->data),
0d13bfcd 1957 pkt->nblanks, pkt->blanks, &ssh->v2_outgoing_sequence);
ff3187f6 1958 sfree(pkt->blanks); pkt->blanks = NULL;
1959 pkt->nblanks = 0;
00db133f 1960
7cca0d81 1961 /*
4ba9b64b 1962 * Compress packet payload.
1963 */
4ba9b64b 1964 {
1965 unsigned char *newpayload;
1966 int newlen;
51470298 1967 if (ssh->cscomp &&
ff3187f6 1968 ssh->cscomp->compress(ssh->cs_comp_ctx, pkt->data + 5,
1969 pkt->length - 5,
51470298 1970 &newpayload, &newlen)) {
ff3187f6 1971 pkt->length = 5;
1972 ssh2_pkt_adddata(pkt, newpayload, newlen);
dcbde236 1973 sfree(newpayload);
4ba9b64b 1974 }
1975 }
1976
1977 /*
7cca0d81 1978 * Add padding. At least four bytes, and must also bring total
1979 * length (minus MAC) up to a multiple of the block size.
95d2d262 1980 * If pkt->forcepad is set, make sure the packet is at least that size
1981 * after padding.
7cca0d81 1982 */
51470298 1983 cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */
32874aea 1984 cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */
7cca0d81 1985 padding = 4;
95d2d262 1986 if (pkt->length + padding < pkt->forcepad)
1987 padding = pkt->forcepad - pkt->length;
32874aea 1988 padding +=
ff3187f6 1989 (cipherblk - (pkt->length + padding) % cipherblk) % cipherblk;
95d2d262 1990 assert(padding <= 255);
51470298 1991 maclen = ssh->csmac ? ssh->csmac->len : 0;
ff3187f6 1992 ssh2_pkt_ensure(pkt, pkt->length + padding + maclen);
1993 pkt->data[4] = padding;
7cca0d81 1994 for (i = 0; i < padding; i++)
ff3187f6 1995 pkt->data[pkt->length + i] = random_byte();
1996 PUT_32BIT(pkt->data, pkt->length + padding - 4);
51470298 1997 if (ssh->csmac)
ff3187f6 1998 ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data,
1999 pkt->length + padding,
51470298 2000 ssh->v2_outgoing_sequence);
2001 ssh->v2_outgoing_sequence++; /* whether or not we MACed */
2002
2003 if (ssh->cscipher)
371e569c 2004 ssh->cscipher->encrypt(ssh->cs_cipher_ctx,
ff3187f6 2005 pkt->data, pkt->length + padding);
51470298 2006
9442dd57 2007 pkt->encrypted_len = pkt->length + padding;
2008
ff3187f6 2009 /* Ready-to-send packet starts at pkt->data. We return length. */
2010 return pkt->length + padding + maclen;
b185170a 2011}
2012
2013/*
590f6a5f 2014 * Routines called from the main SSH code to send packets. There
2015 * are quite a few of these, because we have two separate
2016 * mechanisms for delaying the sending of packets:
2017 *
2018 * - In order to send an IGNORE message and a password message in
2019 * a single fixed-length blob, we require the ability to
2020 * concatenate the encrypted forms of those two packets _into_ a
2021 * single blob and then pass it to our <network.h> transport
2022 * layer in one go. Hence, there's a deferment mechanism which
2023 * works after packet encryption.
2024 *
2025 * - In order to avoid sending any connection-layer messages
2026 * during repeat key exchange, we have to queue up any such
2027 * outgoing messages _before_ they are encrypted (and in
2028 * particular before they're allocated sequence numbers), and
2029 * then send them once we've finished.
2030 *
2031 * I call these mechanisms `defer' and `queue' respectively, so as
2032 * to distinguish them reasonably easily.
2033 *
2034 * The functions send_noqueue() and defer_noqueue() free the packet
2035 * structure they are passed. Every outgoing packet goes through
2036 * precisely one of these functions in its life; packets passed to
2037 * ssh2_pkt_send() or ssh2_pkt_defer() either go straight to one of
2038 * these or get queued, and then when the queue is later emptied
2039 * the packets are all passed to defer_noqueue().
97ab28d7 2040 *
2041 * When using a CBC-mode cipher, it's necessary to ensure that an
2042 * attacker can't provide data to be encrypted using an IV that they
2043 * know. We ensure this by prefixing each packet that might contain
2044 * user data with an SSH_MSG_IGNORE. This is done using the deferral
2045 * mechanism, so in this case send_noqueue() ends up redirecting to
2046 * defer_noqueue(). If you don't like this inefficiency, don't use
2047 * CBC.
b185170a 2048 */
590f6a5f 2049
97ab28d7 2050static void ssh2_pkt_defer_noqueue(Ssh, struct Packet *, int);
2051static void ssh_pkt_defersend(Ssh);
2052
590f6a5f 2053/*
2e85c969 2054 * Send an SSH-2 packet immediately, without queuing or deferring.
590f6a5f 2055 */
2056static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt)
32874aea 2057{
5471d09a 2058 int len;
2059 int backlog;
97ab28d7 2060 if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC)) {
2061 /* We need to send two packets, so use the deferral mechanism. */
2062 ssh2_pkt_defer_noqueue(ssh, pkt, FALSE);
2063 ssh_pkt_defersend(ssh);
2064 return;
2065 }
ff3187f6 2066 len = ssh2_pkt_construct(ssh, pkt);
bf8a49a1 2067 backlog = s_write(ssh, pkt->data, len);
5471d09a 2068 if (backlog > SSH_MAX_BACKLOG)
51470298 2069 ssh_throttle_all(ssh, 1, backlog);
9442dd57 2070
2071 ssh->outgoing_data_size += pkt->encrypted_len;
2072 if (!ssh->kex_in_progress &&
d57f70af 2073 ssh->max_data_size != 0 &&
2074 ssh->outgoing_data_size > ssh->max_data_size)
f382c87d 2075 do_ssh2_transport(ssh, "too much data sent", -1, NULL);
9442dd57 2076
ff3187f6 2077 ssh_free_packet(pkt);
b185170a 2078}
2079
2080/*
2e85c969 2081 * Defer an SSH-2 packet.
b185170a 2082 */
97ab28d7 2083static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt, int noignore)
32874aea 2084{
97ab28d7 2085 int len;
2086 if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC) &&
cf6ddb95 2087 ssh->deferred_len == 0 && !noignore &&
2088 !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
97ab28d7 2089 /*
2090 * Interpose an SSH_MSG_IGNORE to ensure that user data don't
2091 * get encrypted with a known IV.
2092 */
2093 struct Packet *ipkt = ssh2_pkt_init(SSH2_MSG_IGNORE);
27311cc7 2094 ssh2_pkt_addstring_start(ipkt);
97ab28d7 2095 ssh2_pkt_defer_noqueue(ssh, ipkt, TRUE);
2096 }
2097 len = ssh2_pkt_construct(ssh, pkt);
51470298 2098 if (ssh->deferred_len + len > ssh->deferred_size) {
2099 ssh->deferred_size = ssh->deferred_len + len + 128;
3d88e64d 2100 ssh->deferred_send_data = sresize(ssh->deferred_send_data,
2101 ssh->deferred_size,
2102 unsigned char);
b185170a 2103 }
ff3187f6 2104 memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->data, len);
51470298 2105 ssh->deferred_len += len;
9442dd57 2106 ssh->deferred_data_size += pkt->encrypted_len;
ff3187f6 2107 ssh_free_packet(pkt);
b185170a 2108}
2109
2110/*
2e85c969 2111 * Queue an SSH-2 packet.
590f6a5f 2112 */
2113static void ssh2_pkt_queue(Ssh ssh, struct Packet *pkt)
2114{
2115 assert(ssh->queueing);
2116
2117 if (ssh->queuelen >= ssh->queuesize) {
2118 ssh->queuesize = ssh->queuelen + 32;
2119 ssh->queue = sresize(ssh->queue, ssh->queuesize, struct Packet *);
2120 }
2121
2122 ssh->queue[ssh->queuelen++] = pkt;
2123}
2124
2125/*
2126 * Either queue or send a packet, depending on whether queueing is
2127 * set.
2128 */
2129static void ssh2_pkt_send(Ssh ssh, struct Packet *pkt)
2130{
2131 if (ssh->queueing)
2132 ssh2_pkt_queue(ssh, pkt);
2133 else
2134 ssh2_pkt_send_noqueue(ssh, pkt);
2135}
2136
2137/*
2138 * Either queue or defer a packet, depending on whether queueing is
2139 * set.
2140 */
2141static void ssh2_pkt_defer(Ssh ssh, struct Packet *pkt)
2142{
2143 if (ssh->queueing)
2144 ssh2_pkt_queue(ssh, pkt);
2145 else
97ab28d7 2146 ssh2_pkt_defer_noqueue(ssh, pkt, FALSE);
590f6a5f 2147}
2148
2149/*
b185170a 2150 * Send the whole deferred data block constructed by
2e85c969 2151 * ssh2_pkt_defer() or SSH-1's defer_packet().
590f6a5f 2152 *
2153 * The expected use of the defer mechanism is that you call
2154 * ssh2_pkt_defer() a few times, then call ssh_pkt_defersend(). If
2155 * not currently queueing, this simply sets up deferred_send_data
2156 * and then sends it. If we _are_ currently queueing, the calls to
2157 * ssh2_pkt_defer() put the deferred packets on to the queue
2158 * instead, and therefore ssh_pkt_defersend() has no deferred data
2159 * to send. Hence, there's no need to make it conditional on
2160 * ssh->queueing.
b185170a 2161 */
51470298 2162static void ssh_pkt_defersend(Ssh ssh)
32874aea 2163{
5471d09a 2164 int backlog;
bf8a49a1 2165 backlog = s_write(ssh, ssh->deferred_send_data, ssh->deferred_len);
51470298 2166 ssh->deferred_len = ssh->deferred_size = 0;
2167 sfree(ssh->deferred_send_data);
2168 ssh->deferred_send_data = NULL;
5471d09a 2169 if (backlog > SSH_MAX_BACKLOG)
51470298 2170 ssh_throttle_all(ssh, 1, backlog);
9442dd57 2171
2172 ssh->outgoing_data_size += ssh->deferred_data_size;
2173 if (!ssh->kex_in_progress &&
d57f70af 2174 ssh->max_data_size != 0 &&
2175 ssh->outgoing_data_size > ssh->max_data_size)
f382c87d 2176 do_ssh2_transport(ssh, "too much data sent", -1, NULL);
9442dd57 2177 ssh->deferred_data_size = 0;
7cca0d81 2178}
2179
590f6a5f 2180/*
18524056 2181 * Send a packet whose length needs to be disguised (typically
2182 * passwords or keyboard-interactive responses).
2183 */
2184static void ssh2_pkt_send_with_padding(Ssh ssh, struct Packet *pkt,
2185 int padsize)
2186{
2187#if 0
2188 if (0) {
2189 /*
2190 * The simplest way to do this is to adjust the
2191 * variable-length padding field in the outgoing packet.
2192 *
2193 * Currently compiled out, because some Cisco SSH servers
2194 * don't like excessively padded packets (bah, why's it
2195 * always Cisco?)
2196 */
2197 pkt->forcepad = padsize;
2198 ssh2_pkt_send(ssh, pkt);
2199 } else
2200#endif
2201 {
2202 /*
2203 * If we can't do that, however, an alternative approach is
2204 * to use the pkt_defer mechanism to bundle the packet
2205 * tightly together with an SSH_MSG_IGNORE such that their
2206 * combined length is a constant. So first we construct the
2207 * final form of this packet and defer its sending.
2208 */
2209 ssh2_pkt_defer(ssh, pkt);
2210
2211 /*
2212 * Now construct an SSH_MSG_IGNORE which includes a string
2213 * that's an exact multiple of the cipher block size. (If
2214 * the cipher is NULL so that the block size is
2215 * unavailable, we don't do this trick at all, because we
2216 * gain nothing by it.)
2217 */
cf6ddb95 2218 if (ssh->cscipher &&
2219 !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
18524056 2220 int stringlen, i;
2221
2222 stringlen = (256 - ssh->deferred_len);
2223 stringlen += ssh->cscipher->blksize - 1;
2224 stringlen -= (stringlen % ssh->cscipher->blksize);
2225 if (ssh->cscomp) {
2226 /*
2227 * Temporarily disable actual compression, so we
2228 * can guarantee to get this string exactly the
2229 * length we want it. The compression-disabling
2230 * routine should return an integer indicating how
2231 * many bytes we should adjust our string length
2232 * by.
2233 */
2234 stringlen -=
2235 ssh->cscomp->disable_compression(ssh->cs_comp_ctx);
2236 }
2237 pkt = ssh2_pkt_init(SSH2_MSG_IGNORE);
2238 ssh2_pkt_addstring_start(pkt);
2239 for (i = 0; i < stringlen; i++) {
2240 char c = (char) random_byte();
2241 ssh2_pkt_addstring_data(pkt, &c, 1);
2242 }
2243 ssh2_pkt_defer(ssh, pkt);
2244 }
2245 ssh_pkt_defersend(ssh);
2246 }
2247}
2248
2249/*
2e85c969 2250 * Send all queued SSH-2 packets. We send them by means of
590f6a5f 2251 * ssh2_pkt_defer_noqueue(), in case they included a pair of
2252 * packets that needed to be lumped together.
2253 */
2254static void ssh2_pkt_queuesend(Ssh ssh)
2255{
2256 int i;
2257
2258 assert(!ssh->queueing);
2259
2260 for (i = 0; i < ssh->queuelen; i++)
97ab28d7 2261 ssh2_pkt_defer_noqueue(ssh, ssh->queue[i], FALSE);
590f6a5f 2262 ssh->queuelen = 0;
2263
2264 ssh_pkt_defersend(ssh);
2265}
2266
7cca0d81 2267#if 0
32874aea 2268void bndebug(char *string, Bignum b)
2269{
7cca0d81 2270 unsigned char *p;
2271 int i, len;
2272 p = ssh2_mpint_fmt(b, &len);
2273 debug(("%s", string));
2274 for (i = 0; i < len; i++)
32874aea 2275 debug((" %02x", p[i]));
765c4200 2276 debug(("\n"));
dcbde236 2277 sfree(p);
7cca0d81 2278}
2279#endif
2280
b672f405 2281static void hash_mpint(const struct ssh_hash *h, void *s, Bignum b)
32874aea 2282{
7cca0d81 2283 unsigned char *p;
2284 int len;
2285 p = ssh2_mpint_fmt(b, &len);
b672f405 2286 hash_string(h, s, p, len);
dcbde236 2287 sfree(p);
7cca0d81 2288}
2289
2290/*
2e85c969 2291 * Packet decode functions for both SSH-1 and SSH-2.
7cca0d81 2292 */
ff3187f6 2293static unsigned long ssh_pkt_getuint32(struct Packet *pkt)
32874aea 2294{
7cca0d81 2295 unsigned long value;
ff3187f6 2296 if (pkt->length - pkt->savedpos < 4)
32874aea 2297 return 0; /* arrgh, no way to decline (FIXME?) */
ff3187f6 2298 value = GET_32BIT(pkt->body + pkt->savedpos);
2299 pkt->savedpos += 4;
7cca0d81 2300 return value;
2301}
ff3187f6 2302static int ssh2_pkt_getbool(struct Packet *pkt)
32874aea 2303{
65a22376 2304 unsigned long value;
ff3187f6 2305 if (pkt->length - pkt->savedpos < 1)
32874aea 2306 return 0; /* arrgh, no way to decline (FIXME?) */
ff3187f6 2307 value = pkt->body[pkt->savedpos] != 0;
2308 pkt->savedpos++;
65a22376 2309 return value;
2310}
ff3187f6 2311static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length)
32874aea 2312{
57356d63 2313 int len;
7cca0d81 2314 *p = NULL;
45068b27 2315 *length = 0;
ff3187f6 2316 if (pkt->length - pkt->savedpos < 4)
32874aea 2317 return;
ff3187f6 2318 len = GET_32BIT(pkt->body + pkt->savedpos);
57356d63 2319 if (len < 0)
2320 return;
2321 *length = len;
ff3187f6 2322 pkt->savedpos += 4;
2323 if (pkt->length - pkt->savedpos < *length)
32874aea 2324 return;
ff3187f6 2325 *p = (char *)(pkt->body + pkt->savedpos);
2326 pkt->savedpos += *length;
7cca0d81 2327}
ff3187f6 2328static void *ssh_pkt_getdata(struct Packet *pkt, int length)
0016d70b 2329{
ff3187f6 2330 if (pkt->length - pkt->savedpos < length)
0016d70b 2331 return NULL;
ff3187f6 2332 pkt->savedpos += length;
2333 return pkt->body + (pkt->savedpos - length);
0016d70b 2334}
ff3187f6 2335static int ssh1_pkt_getrsakey(struct Packet *pkt, struct RSAKey *key,
0016d70b 2336 unsigned char **keystr)
2337{
2338 int j;
2339
ff3187f6 2340 j = makekey(pkt->body + pkt->savedpos,
2341 pkt->length - pkt->savedpos,
0016d70b 2342 key, keystr, 0);
2343
2344 if (j < 0)
2345 return FALSE;
2346
ff3187f6 2347 pkt->savedpos += j;
2348 assert(pkt->savedpos < pkt->length);
0016d70b 2349
2350 return TRUE;
2351}
ff3187f6 2352static Bignum ssh1_pkt_getmp(struct Packet *pkt)
0016d70b 2353{
2354 int j;
2355 Bignum b;
2356
ff3187f6 2357 j = ssh1_read_bignum(pkt->body + pkt->savedpos,
2358 pkt->length - pkt->savedpos, &b);
0016d70b 2359
2360 if (j < 0)
2361 return NULL;
2362
ff3187f6 2363 pkt->savedpos += j;
0016d70b 2364 return b;
2365}
ff3187f6 2366static Bignum ssh2_pkt_getmp(struct Packet *pkt)
32874aea 2367{
7cca0d81 2368 char *p;
3709bfe9 2369 int length;
7cca0d81 2370 Bignum b;
2371
ff3187f6 2372 ssh_pkt_getstring(pkt, &p, &length);
7cca0d81 2373 if (!p)
32874aea 2374 return NULL;
ff3187f6 2375 if (p[0] & 0x80)
32874aea 2376 return NULL;
d8baa528 2377 b = bignum_from_bytes((unsigned char *)p, length);
7cca0d81 2378 return b;
2379}
2380
7d503c31 2381/*
2e85c969 2382 * Helper function to add an SSH-2 signature blob to a packet.
1dd353b5 2383 * Expects to be shown the public key blob as well as the signature
2384 * blob. Normally works just like ssh2_pkt_addstring, but will
2385 * fiddle with the signature packet if necessary for
2386 * BUG_SSH2_RSA_PADDING.
2387 */
ff3187f6 2388static void ssh2_add_sigblob(Ssh ssh, struct Packet *pkt,
2389 void *pkblob_v, int pkblob_len,
1dd353b5 2390 void *sigblob_v, int sigblob_len)
2391{
2392 unsigned char *pkblob = (unsigned char *)pkblob_v;
2393 unsigned char *sigblob = (unsigned char *)sigblob_v;
2394
2395 /* dmemdump(pkblob, pkblob_len); */
2396 /* dmemdump(sigblob, sigblob_len); */
2397
2398 /*
2399 * See if this is in fact an ssh-rsa signature and a buggy
2400 * server; otherwise we can just do this the easy way.
2401 */
51470298 2402 if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) &&
1dd353b5 2403 (GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) {
2404 int pos, len, siglen;
2405
2406 /*
2407 * Find the byte length of the modulus.
2408 */
2409
2410 pos = 4+7; /* skip over "ssh-rsa" */
2411 pos += 4 + GET_32BIT(pkblob+pos); /* skip over exponent */
2412 len = GET_32BIT(pkblob+pos); /* find length of modulus */
2413 pos += 4; /* find modulus itself */
2414 while (len > 0 && pkblob[pos] == 0)
2415 len--, pos++;
2416 /* debug(("modulus length is %d\n", len)); */
2417
2418 /*
2419 * Now find the signature integer.
2420 */
2421 pos = 4+7; /* skip over "ssh-rsa" */
2422 siglen = GET_32BIT(sigblob+pos);
2423 /* debug(("signature length is %d\n", siglen)); */
2424
2425 if (len != siglen) {
2426 unsigned char newlen[4];
ff3187f6 2427 ssh2_pkt_addstring_start(pkt);
2428 ssh2_pkt_addstring_data(pkt, (char *)sigblob, pos);
1dd353b5 2429 /* dmemdump(sigblob, pos); */
2430 pos += 4; /* point to start of actual sig */
2431 PUT_32BIT(newlen, len);
ff3187f6 2432 ssh2_pkt_addstring_data(pkt, (char *)newlen, 4);
1dd353b5 2433 /* dmemdump(newlen, 4); */
2434 newlen[0] = 0;
2435 while (len-- > siglen) {
ff3187f6 2436 ssh2_pkt_addstring_data(pkt, (char *)newlen, 1);
1dd353b5 2437 /* dmemdump(newlen, 1); */
2438 }
ff3187f6 2439 ssh2_pkt_addstring_data(pkt, (char *)(sigblob+pos), siglen);
1dd353b5 2440 /* dmemdump(sigblob+pos, siglen); */
2441 return;
2442 }
2443
2444 /* Otherwise fall through and do it the easy way. */
2445 }
2446
ff3187f6 2447 ssh2_pkt_addstring_start(pkt);
2448 ssh2_pkt_addstring_data(pkt, (char *)sigblob, sigblob_len);
1dd353b5 2449}
2450
2451/*
7d503c31 2452 * Examine the remote side's version string and compare it against
2453 * a list of known buggy implementations.
2454 */
51470298 2455static void ssh_detect_bugs(Ssh ssh, char *vstring)
32874aea 2456{
2457 char *imp; /* pointer to implementation part */
7d503c31 2458 imp = vstring;
2459 imp += strcspn(imp, "-");
bd358db1 2460 if (*imp) imp++;
7d503c31 2461 imp += strcspn(imp, "-");
bd358db1 2462 if (*imp) imp++;
7d503c31 2463
51470298 2464 ssh->remote_bugs = 0;
7d503c31 2465
bf982899 2466 /*
2467 * General notes on server version strings:
2468 * - Not all servers reporting "Cisco-1.25" have all the bugs listed
2469 * here -- in particular, we've heard of one that's perfectly happy
2470 * with SSH1_MSG_IGNOREs -- but this string never seems to change,
2471 * so we can't distinguish them.
2472 */
4a693cfc 2473 if (conf_get_int(ssh->conf, CONF_sshbug_ignore1) == FORCE_ON ||
2474 (conf_get_int(ssh->conf, CONF_sshbug_ignore1) == AUTO &&
2c9c6388 2475 (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") ||
2476 !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") ||
46ac09aa 2477 !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25") ||
bd0b4caf 2478 !strcmp(imp, "OSU_1.4alpha3") || !strcmp(imp, "OSU_1.5alpha4")))) {
32874aea 2479 /*
2480 * These versions don't support SSH1_MSG_IGNORE, so we have
2481 * to use a different defence against password length
2482 * sniffing.
2483 */
51470298 2484 ssh->remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE;
2e85c969 2485 logevent("We believe remote version has SSH-1 ignore bug");
7d503c31 2486 }
2487
4a693cfc 2488 if (conf_get_int(ssh->conf, CONF_sshbug_plainpw1) == FORCE_ON ||
2489 (conf_get_int(ssh->conf, CONF_sshbug_plainpw1) == AUTO &&
46ac09aa 2490 (!strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3")))) {
bd358db1 2491 /*
2492 * These versions need a plain password sent; they can't
2493 * handle having a null and a random length of data after
2494 * the password.
2495 */
51470298 2496 ssh->remote_bugs |= BUG_NEEDS_SSH1_PLAIN_PASSWORD;
2e85c969 2497 logevent("We believe remote version needs a plain SSH-1 password");
bd358db1 2498 }
2499
4a693cfc 2500 if (conf_get_int(ssh->conf, CONF_sshbug_rsa1) == FORCE_ON ||
2501 (conf_get_int(ssh->conf, CONF_sshbug_rsa1) == AUTO &&
2c9c6388 2502 (!strcmp(imp, "Cisco-1.25")))) {
0df73905 2503 /*
2504 * These versions apparently have no clue whatever about
2505 * RSA authentication and will panic and die if they see
2506 * an AUTH_RSA message.
2507 */
51470298 2508 ssh->remote_bugs |= BUG_CHOKES_ON_RSA;
19f47a7d 2509 logevent("We believe remote version can't handle SSH-1 RSA authentication");
0df73905 2510 }
2511
4a693cfc 2512 if (conf_get_int(ssh->conf, CONF_sshbug_hmac2) == FORCE_ON ||
2513 (conf_get_int(ssh->conf, CONF_sshbug_hmac2) == AUTO &&
b9f387af 2514 !wc_match("* VShell", imp) &&
831301f6 2515 (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) ||
2516 wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) ||
2517 wc_match("2.1 *", imp)))) {
32874aea 2518 /*
2519 * These versions have the HMAC bug.
2520 */
51470298 2521 ssh->remote_bugs |= BUG_SSH2_HMAC;
2e85c969 2522 logevent("We believe remote version has SSH-2 HMAC bug");
7d503c31 2523 }
1dd353b5 2524
4a693cfc 2525 if (conf_get_int(ssh->conf, CONF_sshbug_derivekey2) == FORCE_ON ||
2526 (conf_get_int(ssh->conf, CONF_sshbug_derivekey2) == AUTO &&
b9f387af 2527 !wc_match("* VShell", imp) &&
2856a1b9 2528 (wc_match("2.0.0*", imp) || wc_match("2.0.10*", imp) ))) {
088bde77 2529 /*
2530 * These versions have the key-derivation bug (failing to
2531 * include the literal shared secret in the hashes that
2532 * generate the keys).
2533 */
51470298 2534 ssh->remote_bugs |= BUG_SSH2_DERIVEKEY;
2e85c969 2535 logevent("We believe remote version has SSH-2 key-derivation bug");
088bde77 2536 }
2537
4a693cfc 2538 if (conf_get_int(ssh->conf, CONF_sshbug_rsapad2) == FORCE_ON ||
2539 (conf_get_int(ssh->conf, CONF_sshbug_rsapad2) == AUTO &&
831301f6 2540 (wc_match("OpenSSH_2.[5-9]*", imp) ||
2541 wc_match("OpenSSH_3.[0-2]*", imp)))) {
1dd353b5 2542 /*
2e85c969 2543 * These versions have the SSH-2 RSA padding bug.
1dd353b5 2544 */
51470298 2545 ssh->remote_bugs |= BUG_SSH2_RSA_PADDING;
2e85c969 2546 logevent("We believe remote version has SSH-2 RSA padding bug");
1dd353b5 2547 }
8e975795 2548
4a693cfc 2549 if (conf_get_int(ssh->conf, CONF_sshbug_pksessid2) == FORCE_ON ||
2550 (conf_get_int(ssh->conf, CONF_sshbug_pksessid2) == AUTO &&
dda87a28 2551 wc_match("OpenSSH_2.[0-2]*", imp))) {
2552 /*
2e85c969 2553 * These versions have the SSH-2 session-ID bug in
dda87a28 2554 * public-key authentication.
2555 */
2556 ssh->remote_bugs |= BUG_SSH2_PK_SESSIONID;
2e85c969 2557 logevent("We believe remote version has SSH-2 public-key-session-ID bug");
dda87a28 2558 }
f382c87d 2559
4a693cfc 2560 if (conf_get_int(ssh->conf, CONF_sshbug_rekey2) == FORCE_ON ||
2561 (conf_get_int(ssh->conf, CONF_sshbug_rekey2) == AUTO &&
39b8712f 2562 (wc_match("DigiSSH_2.0", imp) ||
2563 wc_match("OpenSSH_2.[0-4]*", imp) ||
e12d95a5 2564 wc_match("OpenSSH_2.5.[0-3]*", imp) ||
2565 wc_match("Sun_SSH_1.0", imp) ||
f4275b53 2566 wc_match("Sun_SSH_1.0.1", imp) ||
96bd26de 2567 /* All versions <= 1.2.6 (they changed their format in 1.2.7) */
2568 wc_match("WeOnlyDo-*", imp)))) {
f382c87d 2569 /*
2e85c969 2570 * These versions have the SSH-2 rekey bug.
f382c87d 2571 */
2572 ssh->remote_bugs |= BUG_SSH2_REKEY;
2e85c969 2573 logevent("We believe remote version has SSH-2 rekey bug");
f382c87d 2574 }
c9739dba 2575
4a693cfc 2576 if (conf_get_int(ssh->conf, CONF_sshbug_maxpkt2) == FORCE_ON ||
2577 (conf_get_int(ssh->conf, CONF_sshbug_maxpkt2) == AUTO &&
5ca836f2 2578 (wc_match("1.36_sshlib GlobalSCAPE", imp) ||
2579 wc_match("1.36 sshlib: GlobalScape", imp)))) {
c9739dba 2580 /*
2581 * This version ignores our makpkt and needs to be throttled.
2582 */
2583 ssh->remote_bugs |= BUG_SSH2_MAXPKT;
2584 logevent("We believe remote version ignores SSH-2 maximum packet size");
2585 }
cf6ddb95 2586
4a693cfc 2587 if (conf_get_int(ssh->conf, CONF_sshbug_ignore2) == FORCE_ON) {
cf6ddb95 2588 /*
2589 * Servers that don't support SSH2_MSG_IGNORE. Currently,
2590 * none detected automatically.
2591 */
2592 ssh->remote_bugs |= BUG_CHOKES_ON_SSH2_IGNORE;
2593 logevent("We believe remote version has SSH-2 ignore bug");
2594 }
2e0aae4f 2595
2596 if (conf_get_int(ssh->conf, CONF_sshbug_winadj) == FORCE_ON) {
2597 /*
2598 * Servers that don't support our winadj request for one
2599 * reason or another. Currently, none detected automatically.
2600 */
2601 ssh->remote_bugs |= BUG_CHOKES_ON_WINADJ;
2602 logevent("We believe remote version has winadj bug");
2603 }
7d503c31 2604}
2605
d38d6a1f 2606/*
2607 * The `software version' part of an SSH version string is required
2608 * to contain no spaces or minus signs.
2609 */
2610static void ssh_fix_verstring(char *str)
2611{
2612 /* Eat "SSH-<protoversion>-". */
2613 assert(*str == 'S'); str++;
2614 assert(*str == 'S'); str++;
2615 assert(*str == 'H'); str++;
2616 assert(*str == '-'); str++;
2617 while (*str && *str != '-') str++;
2618 assert(*str == '-'); str++;
2619
2620 /* Convert minus signs and spaces in the remaining string into
2621 * underscores. */
2622 while (*str) {
2623 if (*str == '-' || *str == ' ')
2624 *str = '_';
2625 str++;
2626 }
2627}
2628
1fcd0d98 2629/*
2630 * Send an appropriate SSH version string.
2631 */
2632static void ssh_send_verstring(Ssh ssh, char *svers)
2633{
2634 char *verstring;
2635
2636 if (ssh->version == 2) {
2637 /*
2638 * Construct a v2 version string.
2639 */
2640 verstring = dupprintf("SSH-2.0-%s\015\012", sshver);
2641 } else {
2642 /*
2643 * Construct a v1 version string.
2644 */
2645 verstring = dupprintf("SSH-%s-%s\012",
2646 (ssh_versioncmp(svers, "1.5") <= 0 ?
2647 svers : "1.5"),
2648 sshver);
2649 }
2650
2651 ssh_fix_verstring(verstring);
2652
2653 if (ssh->version == 2) {
2654 size_t len;
2655 /*
2656 * Record our version string.
2657 */
2658 len = strcspn(verstring, "\015\012");
2659 ssh->v_c = snewn(len + 1, char);
2660 memcpy(ssh->v_c, verstring, len);
2661 ssh->v_c[len] = 0;
2662 }
2663
2664 logeventf(ssh, "We claim version: %.*s",
2665 strcspn(verstring, "\015\012"), verstring);
2666 s_write(ssh, verstring, strlen(verstring));
2667 sfree(verstring);
2668}
2669
51470298 2670static int do_ssh_init(Ssh ssh, unsigned char c)
32874aea 2671{
51470298 2672 struct do_ssh_init_state {
30919997 2673 int crLine;
51470298 2674 int vslen;
2675 char version[10];
2676 char *vstring;
2677 int vstrsize;
2678 int i;
2679 int proto1, proto2;
2680 };
2681 crState(do_ssh_init_state);
30919997 2682
2683 crBeginState;
8df7a775 2684
8c1835c0 2685 /* Search for a line beginning with the string "SSH-" in the input. */
2686 for (;;) {
2687 if (c != 'S') goto no;
2688 crReturn(1);
2689 if (c != 'S') goto no;
2690 crReturn(1);
2691 if (c != 'H') goto no;
2692 crReturn(1);
2693 if (c != '-') goto no;
2694 break;
2695 no:
2696 while (c != '\012')
2697 crReturn(1);
2698 crReturn(1);
374330e2 2699 }
8df7a775 2700
51470298 2701 s->vstrsize = 16;
3d88e64d 2702 s->vstring = snewn(s->vstrsize, char);
51470298 2703 strcpy(s->vstring, "SSH-");
2704 s->vslen = 4;
2705 s->i = 0;
374330e2 2706 while (1) {
8df7a775 2707 crReturn(1); /* get another char */
51470298 2708 if (s->vslen >= s->vstrsize - 1) {
2709 s->vstrsize += 16;
3d88e64d 2710 s->vstring = sresize(s->vstring, s->vstrsize, char);
32874aea 2711 }
51470298 2712 s->vstring[s->vslen++] = c;
2713 if (s->i >= 0) {
374330e2 2714 if (c == '-') {
51470298 2715 s->version[s->i] = '\0';
2716 s->i = -1;
2717 } else if (s->i < sizeof(s->version) - 1)
2718 s->version[s->i++] = c;
c4ffc4d0 2719 } else if (c == '\012')
374330e2 2720 break;
2721 }
2722
51470298 2723 ssh->agentfwd_enabled = FALSE;
2724 ssh->rdpkt2_state.incoming_sequence = 0;
960e736a 2725
51470298 2726 s->vstring[s->vslen] = 0;
a7d4653a 2727 s->vstring[strcspn(s->vstring, "\015\012")] = '\0';/* remove EOL chars */
fb983202 2728 logeventf(ssh, "Server version: %s", s->vstring);
51470298 2729 ssh_detect_bugs(ssh, s->vstring);
c5e9c988 2730
adf799dd 2731 /*
38d228a2 2732 * Decide which SSH protocol version to support.
adf799dd 2733 */
38d228a2 2734
2735 /* Anything strictly below "2.0" means protocol 1 is supported. */
51470298 2736 s->proto1 = ssh_versioncmp(s->version, "2.0") < 0;
38d228a2 2737 /* Anything greater or equal to "1.99" means protocol 2 is supported. */
51470298 2738 s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0;
38d228a2 2739
4a693cfc 2740 if (conf_get_int(ssh->conf, CONF_sshprot) == 0 && !s->proto1) {
6b5cf8b4 2741 bombout(("SSH protocol version 1 required by user but not provided by server"));
7ffdbc1a 2742 crStop(0);
38d228a2 2743 }
4a693cfc 2744 if (conf_get_int(ssh->conf, CONF_sshprot) == 3 && !s->proto2) {
6b5cf8b4 2745 bombout(("SSH protocol version 2 required by user but not provided by server"));
7ffdbc1a 2746 crStop(0);
38d228a2 2747 }
2748
4a693cfc 2749 if (s->proto2 && (conf_get_int(ssh->conf, CONF_sshprot) >= 2 || !s->proto1))
1fcd0d98 2750 ssh->version = 2;
2751 else
2752 ssh->version = 1;
d38d6a1f 2753
2754 logeventf(ssh, "Using SSH protocol version %d", ssh->version);
2755
1fcd0d98 2756 /* Send the version string, if we haven't already */
4a693cfc 2757 if (conf_get_int(ssh->conf, CONF_sshprot) != 3)
1fcd0d98 2758 ssh_send_verstring(ssh, s->version);
2759
2760 if (ssh->version == 2) {
2761 size_t len;
2762 /*
2763 * Record their version string.
2764 */
2765 len = strcspn(s->vstring, "\015\012");
2766 ssh->v_s = snewn(len + 1, char);
2767 memcpy(ssh->v_s, s->vstring, len);
2768 ssh->v_s[len] = 0;
2769
2770 /*
2771 * Initialise SSH-2 protocol.
2772 */
2773 ssh->protocol = ssh2_protocol;
2774 ssh2_protocol_setup(ssh);
2775 ssh->s_rdpkt = ssh2_rdpkt;
2776 } else {
2777 /*
2778 * Initialise SSH-1 protocol.
2779 */
2780 ssh->protocol = ssh1_protocol;
2781 ssh1_protocol_setup(ssh);
2782 ssh->s_rdpkt = ssh1_rdpkt;
2783 }
2784 if (ssh->version == 2)
2785 do_ssh2_transport(ssh, NULL, -1, NULL);
2786
125105d1 2787 update_specials_menu(ssh->frontend);
51470298 2788 ssh->state = SSH_STATE_BEFORE_SIZE;
4a693cfc 2789 ssh->pinger = pinger_new(ssh->conf, &ssh_backend, ssh);
8df7a775 2790
51470298 2791 sfree(s->vstring);
50526e47 2792
8df7a775 2793 crFinish(0);
2794}
2795
3d9449a1 2796static void ssh_process_incoming_data(Ssh ssh,
2797 unsigned char **data, int *datalen)
2798{
bf8a49a1 2799 struct Packet *pktin;
2800
2801 pktin = ssh->s_rdpkt(ssh, data, datalen);
3d9449a1 2802 if (pktin) {
2803 ssh->protocol(ssh, NULL, 0, pktin);
2804 ssh_free_packet(pktin);
2805 }
2806}
2807
2808static void ssh_queue_incoming_data(Ssh ssh,
2809 unsigned char **data, int *datalen)
2810{
2811 bufchain_add(&ssh->queued_incoming_data, *data, *datalen);
2812 *data += *datalen;
2813 *datalen = 0;
2814}
2815
2816static void ssh_process_queued_incoming_data(Ssh ssh)
2817{
2818 void *vdata;
2819 unsigned char *data;
2820 int len, origlen;
2821
2822 while (!ssh->frozen && bufchain_size(&ssh->queued_incoming_data)) {
2823 bufchain_prefix(&ssh->queued_incoming_data, &vdata, &len);
2824 data = vdata;
2825 origlen = len;
2826
2827 while (!ssh->frozen && len > 0)
2828 ssh_process_incoming_data(ssh, &data, &len);
2829
2830 if (origlen > len)
2831 bufchain_consume(&ssh->queued_incoming_data, origlen - len);
2832 }
2833}
2834
2835static void ssh_set_frozen(Ssh ssh, int frozen)
2836{
a5a6f839 2837 if (ssh->s)
2838 sk_set_frozen(ssh->s, frozen);
3d9449a1 2839 ssh->frozen = frozen;
2840}
2841
51470298 2842static void ssh_gotdata(Ssh ssh, unsigned char *data, int datalen)
8df7a775 2843{
bf8a49a1 2844 /* Log raw data, if we're in that mode. */
ff05dfef 2845 if (ssh->logctx)
2846 log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, datalen,
0d13bfcd 2847 0, NULL, NULL);
bf8a49a1 2848
51470298 2849 crBegin(ssh->ssh_gotdata_crstate);
8df7a775 2850
2851 /*
2852 * To begin with, feed the characters one by one to the
2853 * protocol initialisation / selection function do_ssh_init().
2854 * When that returns 0, we're done with the initial greeting
2855 * exchange and can move on to packet discipline.
2856 */
2857 while (1) {
51470298 2858 int ret; /* need not be kept across crReturn */
8df7a775 2859 if (datalen == 0)
2860 crReturnV; /* more data please */
51470298 2861 ret = do_ssh_init(ssh, *data);
32874aea 2862 data++;
2863 datalen--;
8df7a775 2864 if (ret == 0)
2865 break;
2866 }
2867
2868 /*
2869 * We emerge from that loop when the initial negotiation is
2870 * over and we have selected an s_rdpkt function. Now pass
2871 * everything to s_rdpkt, and then pass the resulting packets
2872 * to the proper protocol handler.
2873 */
3d9449a1 2874
8df7a775 2875 while (1) {
e375106c 2876 while (bufchain_size(&ssh->queued_incoming_data) > 0 || datalen > 0) {
2877 if (ssh->frozen) {
3d9449a1 2878 ssh_queue_incoming_data(ssh, &data, &datalen);
e375106c 2879 /* This uses up all data and cannot cause anything interesting
2880 * to happen; indeed, for anything to happen at all, we must
2881 * return, so break out. */
2882 break;
2883 } else if (bufchain_size(&ssh->queued_incoming_data) > 0) {
2884 /* This uses up some or all data, and may freeze the
2885 * session. */
2886 ssh_process_queued_incoming_data(ssh);
2887 } else {
2888 /* This uses up some or all data, and may freeze the
2889 * session. */
2890 ssh_process_incoming_data(ssh, &data, &datalen);
2891 }
2892 /* FIXME this is probably EBW. */
ff3187f6 2893 if (ssh->state == SSH_STATE_CLOSED)
2894 return;
8df7a775 2895 }
e375106c 2896 /* We're out of data. Go and get some more. */
8df7a775 2897 crReturnV;
2898 }
2899 crFinishV;
2900}
2901
ac934965 2902static int ssh_do_close(Ssh ssh, int notify_exit)
32874aea 2903{
80ffa58b 2904 int ret = 0;
36f94d1f 2905 struct ssh_channel *c;
2906
51470298 2907 ssh->state = SSH_STATE_CLOSED;
ecbb0000 2908 expire_timer_context(ssh);
51470298 2909 if (ssh->s) {
2910 sk_close(ssh->s);
2911 ssh->s = NULL;
ac934965 2912 if (notify_exit)
2913 notify_remote_exit(ssh->frontend);
2914 else
2915 ret = 1;
f3ab576e 2916 }
36f94d1f 2917 /*
80ffa58b 2918 * Now we must shut down any port- and X-forwarded channels going
36f94d1f 2919 * through this connection.
2920 */
74a98066 2921 if (ssh->channels) {
80ffa58b 2922 while (NULL != (c = index234(ssh->channels, 0))) {
74a98066 2923 switch (c->type) {
2924 case CHAN_X11:
2925 x11_close(c->u.x11.s);
2926 break;
2927 case CHAN_SOCKDATA:
409eca86 2928 case CHAN_SOCKDATA_DORMANT:
74a98066 2929 pfd_close(c->u.pfd.s);
2930 break;
2931 }
80ffa58b 2932 del234(ssh->channels, c); /* moving next one to index 0 */
74a98066 2933 if (ssh->version == 2)
2934 bufchain_clear(&c->v.v2.outbuffer);
2935 sfree(c);
36f94d1f 2936 }
36f94d1f 2937 }
f8c9f9df 2938 /*
2939 * Go through port-forwardings, and close any associated
2940 * listening sockets.
2941 */
2942 if (ssh->portfwds) {
2943 struct ssh_portfwd *pf;
2944 while (NULL != (pf = index234(ssh->portfwds, 0))) {
2945 /* Dispose of any listening socket. */
2946 if (pf->local)
2947 pfd_terminate(pf->local);
2948 del234(ssh->portfwds, pf); /* moving next one to index 0 */
2949 free_portfwd(pf);
2950 }
b72bdf11 2951 freetree234(ssh->portfwds);
2952 ssh->portfwds = NULL;
f8c9f9df 2953 }
ac934965 2954
2955 return ret;
36f94d1f 2956}
2957
7555d6a5 2958static void ssh_log(Plug plug, int type, SockAddr addr, int port,
2959 const char *error_msg, int error_code)
2960{
2961 Ssh ssh = (Ssh) plug;
2962 char addrbuf[256], *msg;
2963
2964 sk_getaddr(addr, addrbuf, lenof(addrbuf));
2965
2966 if (type == 0)
2967 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
2968 else
2969 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
2970
2971 logevent(msg);
fb983202 2972 sfree(msg);
7555d6a5 2973}
2974
cbe2d68f 2975static int ssh_closing(Plug plug, const char *error_msg, int error_code,
36f94d1f 2976 int calling_back)
2977{
2978 Ssh ssh = (Ssh) plug;
ac934965 2979 int need_notify = ssh_do_close(ssh, FALSE);
2980
9e296bfa 2981 if (!error_msg) {
2982 if (!ssh->close_expected)
2983 error_msg = "Server unexpectedly closed network connection";
2984 else
2985 error_msg = "Server closed network connection";
ac934965 2986 }
2987
d051d1b4 2988 if (ssh->close_expected && ssh->clean_exit && ssh->exitcode < 0)
2989 ssh->exitcode = 0;
2990
2425184b 2991 if (need_notify)
2992 notify_remote_exit(ssh->frontend);
2993
9e296bfa 2994 if (error_msg)
247308b5 2995 logevent(error_msg);
9e296bfa 2996 if (!ssh->close_expected || !ssh->clean_exit)
971bcc0a 2997 connection_fatal(ssh->frontend, "%s", error_msg);
7e78000d 2998 return 0;
2999}
3000
32874aea 3001static int ssh_receive(Plug plug, int urgent, char *data, int len)
3002{
51470298 3003 Ssh ssh = (Ssh) plug;
d8baa528 3004 ssh_gotdata(ssh, (unsigned char *)data, len);
51470298 3005 if (ssh->state == SSH_STATE_CLOSED) {
ac934965 3006 ssh_do_close(ssh, TRUE);
32874aea 3007 return 0;
3257deae 3008 }
fef97f43 3009 return 1;
374330e2 3010}
3011
5471d09a 3012static void ssh_sent(Plug plug, int bufsize)
3013{
51470298 3014 Ssh ssh = (Ssh) plug;
5471d09a 3015 /*
3016 * If the send backlog on the SSH socket itself clears, we
3017 * should unthrottle the whole world if it was throttled.
3018 */
3019 if (bufsize < SSH_MAX_BACKLOG)
51470298 3020 ssh_throttle_all(ssh, 0, bufsize);
5471d09a 3021}
3022
fb09bf1c 3023/*
8df7a775 3024 * Connect to specified host and port.
3025 * Returns an error message, or NULL on success.
6e1ebb76 3026 * Also places the canonical host name into `realhost'. It must be
3027 * freed by the caller.
8df7a775 3028 */
cbe2d68f 3029static const char *connect_to_host(Ssh ssh, char *host, int port,
79bf227b 3030 char **realhost, int nodelay, int keepalive)
8df7a775 3031{
51470298 3032 static const struct plug_function_table fn_table = {
7555d6a5 3033 ssh_log,
7e78000d 3034 ssh_closing,
5471d09a 3035 ssh_receive,
3036 ssh_sent,
3037 NULL
51470298 3038 };
7e78000d 3039
8df7a775 3040 SockAddr addr;
cbe2d68f 3041 const char *err;
4a693cfc 3042 char *loghost;
3043 int addressfamily, sshprot;
3044
3045 loghost = conf_get_str(ssh->conf, CONF_loghost);
3046 if (*loghost) {
881da168 3047 char *colon;
8df7a775 3048
4a693cfc 3049 ssh->savedhost = dupstr(loghost);
881da168 3050 ssh->savedport = 22; /* default ssh port */
3051
3052 /*
3053 * A colon suffix on savedhost also lets us affect
3054 * savedport.
3055 *
3056 * (FIXME: do something about IPv6 address literals here.)
3057 */
3058 colon = strrchr(ssh->savedhost, ':');
3059 if (colon) {
3060 *colon++ = '\0';
3061 if (*colon)
3062 ssh->savedport = atoi(colon);
3063 }
3064 } else {
3065 ssh->savedhost = dupstr(host);
3066 if (port < 0)
3067 port = 22; /* default ssh port */
3068 ssh->savedport = port;
3069 }
8df7a775 3070
3071 /*
3072 * Try to find host.
3073 */
4a693cfc 3074 addressfamily = conf_get_int(ssh->conf, CONF_addressfamily);
05581745 3075 logeventf(ssh, "Looking up host \"%s\"%s", host,
4a693cfc 3076 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
3077 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : "")));
3078 addr = name_lookup(host, port, realhost, ssh->conf, addressfamily);
170c1e6e 3079 if ((err = sk_addr_error(addr)) != NULL) {
3080 sk_addr_free(addr);
8df7a775 3081 return err;
170c1e6e 3082 }
42af6a67 3083 ssh->fullhostname = dupstr(*realhost); /* save in case of GSSAPI */
8df7a775 3084
8df7a775 3085 /*
3086 * Open socket.
3087 */
51470298 3088 ssh->fn = &fn_table;
e8fa8f62 3089 ssh->s = new_connection(addr, *realhost, port,
4a693cfc 3090 0, 1, nodelay, keepalive, (Plug) ssh, ssh->conf);
70e5d0fd 3091 if ((err = sk_socket_error(ssh->s)) != NULL) {
51470298 3092 ssh->s = NULL;
39934deb 3093 notify_remote_exit(ssh->frontend);
8df7a775 3094 return err;
67c4ba2e 3095 }
8df7a775 3096
ff05dfef 3097 /*
3098 * If the SSH version number's fixed, set it now, and if it's SSH-2,
3099 * send the version string too.
3100 */
4a693cfc 3101 sshprot = conf_get_int(ssh->conf, CONF_sshprot);
3102 if (sshprot == 0)
ff05dfef 3103 ssh->version = 1;
4a693cfc 3104 if (sshprot == 3) {
ff05dfef 3105 ssh->version = 2;
3106 ssh_send_verstring(ssh, NULL);
3107 }
3108
881da168 3109 /*
3110 * loghost, if configured, overrides realhost.
3111 */
4a693cfc 3112 if (*loghost) {
881da168 3113 sfree(*realhost);
4a693cfc 3114 *realhost = dupstr(loghost);
881da168 3115 }
3116
8df7a775 3117 return NULL;
3118}
3119
3120/*
5471d09a 3121 * Throttle or unthrottle the SSH connection.
3122 */
ff68564b 3123static void ssh_throttle_conn(Ssh ssh, int adjust)
5471d09a 3124{
ff68564b 3125 int old_count = ssh->conn_throttle_count;
3126 ssh->conn_throttle_count += adjust;
3127 assert(ssh->conn_throttle_count >= 0);
3128 if (ssh->conn_throttle_count && !old_count) {
3d9449a1 3129 ssh_set_frozen(ssh, 1);
ff68564b 3130 } else if (!ssh->conn_throttle_count && old_count) {
3d9449a1 3131 ssh_set_frozen(ssh, 0);
5471d09a 3132 }
3133}
3134
3135/*
3136 * Throttle or unthrottle _all_ local data streams (for when sends
3137 * on the SSH connection itself back up).
3138 */
51470298 3139static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)
5471d09a 3140{
3141 int i;
3142 struct ssh_channel *c;
3143
51470298 3144 if (enable == ssh->throttled_all)
5471d09a 3145 return;
51470298 3146 ssh->throttled_all = enable;
3147 ssh->overall_bufsize = bufsize;
3148 if (!ssh->channels)
5471d09a 3149 return;
51470298 3150 for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) {
5471d09a 3151 switch (c->type) {
3152 case CHAN_MAINSESSION:
3153 /*
3154 * This is treated separately, outside the switch.
3155 */
3156 break;
3157 case CHAN_X11:
3158 x11_override_throttle(c->u.x11.s, enable);
3159 break;
3160 case CHAN_AGENT:
3161 /* Agent channels require no buffer management. */
3162 break;
3163 case CHAN_SOCKDATA:
36f94d1f 3164 pfd_override_throttle(c->u.pfd.s, enable);
5471d09a 3165 break;
3166 }
3167 }
3168}
3169
f11d78f2 3170static void ssh_agent_callback(void *sshv, void *reply, int replylen)
839f10db 3171{
3172 Ssh ssh = (Ssh) sshv;
3173
3174 ssh->agent_response = reply;
3175 ssh->agent_response_len = replylen;
3176
3177 if (ssh->version == 1)
ff3187f6 3178 do_ssh1_login(ssh, NULL, -1, NULL);
839f10db 3179 else
ff3187f6 3180 do_ssh2_authconn(ssh, NULL, -1, NULL);
839f10db 3181}
3182
3d9449a1 3183static void ssh_dialog_callback(void *sshv, int ret)
3184{
3185 Ssh ssh = (Ssh) sshv;
3186
3187 ssh->user_response = ret;
3188
3189 if (ssh->version == 1)
3190 do_ssh1_login(ssh, NULL, -1, NULL);
3191 else
3192 do_ssh2_transport(ssh, NULL, -1, NULL);
3193
3194 /*
3195 * This may have unfrozen the SSH connection, so do a
3196 * queued-data run.
3197 */
3198 ssh_process_queued_incoming_data(ssh);
3199}
3200
f11d78f2 3201static void ssh_agentf_callback(void *cv, void *reply, int replylen)
839f10db 3202{
3203 struct ssh_channel *c = (struct ssh_channel *)cv;
3204 Ssh ssh = c->ssh;
3205 void *sentreply = reply;
3206
3207 if (!sentreply) {
3208 /* Fake SSH_AGENT_FAILURE. */
3209 sentreply = "\0\0\0\1\5";
3210 replylen = 5;
3211 }
3212 if (ssh->version == 2) {
3213 ssh2_add_channel_data(c, sentreply, replylen);
3214 ssh2_try_send(c);
3215 } else {
3216 send_packet(ssh, SSH1_MSG_CHANNEL_DATA,
3217 PKT_INT, c->remoteid,
3218 PKT_INT, replylen,
a2724fba 3219 PKTT_DATA,
839f10db 3220 PKT_DATA, sentreply, replylen,
9a10ecf4 3221 PKTT_OTHER,
839f10db 3222 PKT_END);
3223 }
3224 if (reply)
3225 sfree(reply);
3226}
3227
0405e71f 3228/*
9e296bfa 3229 * Client-initiated disconnection. Send a DISCONNECT if `wire_reason'
3230 * non-NULL, otherwise just close the connection. `client_reason' == NULL
3231 * => log `wire_reason'.
3232 */
3233static void ssh_disconnect(Ssh ssh, char *client_reason, char *wire_reason,
3234 int code, int clean_exit)
3235{
3236 char *error;
3237 if (!client_reason)
3238 client_reason = wire_reason;
3239 if (client_reason)
3240 error = dupprintf("Disconnected: %s", client_reason);
3241 else
3242 error = dupstr("Disconnected");
3243 if (wire_reason) {
3244 if (ssh->version == 1) {
3245 send_packet(ssh, SSH1_MSG_DISCONNECT, PKT_STR, wire_reason,
3246 PKT_END);
3247 } else if (ssh->version == 2) {
3248 struct Packet *pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT);
3249 ssh2_pkt_adduint32(pktout, code);
3250 ssh2_pkt_addstring(pktout, wire_reason);
3251 ssh2_pkt_addstring(pktout, "en"); /* language tag */
3252 ssh2_pkt_send_noqueue(ssh, pktout);
3253 }
3254 }
3255 ssh->close_expected = TRUE;
3256 ssh->clean_exit = clean_exit;
3257 ssh_closing((Plug)ssh, error, 0, 0);
3258 sfree(error);
3259}
3260
3261/*
fb09bf1c 3262 * Handle the key exchange and user authentication phases.
3263 */
ff3187f6 3264static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
3265 struct Packet *pktin)
fb09bf1c 3266{
0016d70b 3267 int i, j, ret;
3268 unsigned char cookie[8], *ptr;
374330e2 3269 struct RSAKey servkey, hostkey;
3270 struct MD5Context md5c;
51470298 3271 struct do_ssh1_login_state {
30919997 3272 int crLine;
51470298 3273 int len;
3274 unsigned char *rsabuf, *keystr1, *keystr2;
3275 unsigned long supported_ciphers_mask, supported_auths_mask;
3276 int tried_publickey, tried_agent;
3277 int tis_auth_refused, ccard_auth_refused;
3278 unsigned char session_id[16];
3279 int cipher_type;
51470298 3280 void *publickey_blob;
3281 int publickey_bloblen;
edd0cb8a 3282 char *publickey_comment;
3283 int publickey_encrypted;
3284 prompts_t *cur_prompt;
51470298 3285 char c;
3286 int pwpkt_type;
3287 unsigned char request[5], *response, *p;
3288 int responselen;
3289 int keyi, nkeys;
3290 int authed;
3291 struct RSAKey key;
3292 Bignum challenge;
3293 char *commentp;
3294 int commentlen;
3d9449a1 3295 int dlgret;
4a693cfc 3296 Filename *keyfile;
51470298 3297 };
3298 crState(do_ssh1_login_state);
3299
30919997 3300 crBeginState;
374330e2 3301
ff3187f6 3302 if (!pktin)
3303 crWaitUntil(pktin);
374330e2 3304
ff3187f6 3305 if (pktin->type != SSH1_SMSG_PUBLIC_KEY) {
6b5cf8b4 3306 bombout(("Public key packet not received"));
7ffdbc1a 3307 crStop(0);
8d5de777 3308 }
374330e2 3309
c5e9c988 3310 logevent("Received public keys");
374330e2 3311
ff3187f6 3312 ptr = ssh_pkt_getdata(pktin, 8);
0016d70b 3313 if (!ptr) {
2e85c969 3314 bombout(("SSH-1 public key packet stopped before random cookie"));
0016d70b 3315 crStop(0);
3316 }
3317 memcpy(cookie, ptr, 8);
374330e2 3318
ff3187f6 3319 if (!ssh1_pkt_getrsakey(pktin, &servkey, &s->keystr1) ||
3320 !ssh1_pkt_getrsakey(pktin, &hostkey, &s->keystr2)) {
2e85c969 3321 bombout(("Failed to read SSH-1 public keys from public key packet"));
0016d70b 3322 crStop(0);
3323 }
374330e2 3324
c5e9c988 3325 /*
1c2a93c4 3326 * Log the host key fingerprint.
c5e9c988 3327 */
c5e9c988 3328 {
3329 char logmsg[80];
1c2a93c4 3330 logevent("Host key fingerprint is:");
c5e9c988 3331 strcpy(logmsg, " ");
32874aea 3332 hostkey.comment = NULL;
3333 rsa_fingerprint(logmsg + strlen(logmsg),
3334 sizeof(logmsg) - strlen(logmsg), &hostkey);
c5e9c988 3335 logevent(logmsg);
3336 }
3337
ff3187f6 3338 ssh->v1_remote_protoflags = ssh_pkt_getuint32(pktin);
3339 s->supported_ciphers_mask = ssh_pkt_getuint32(pktin);
3340 s->supported_auths_mask = ssh_pkt_getuint32(pktin);
d5b2c841 3341 if ((ssh->remote_bugs & BUG_CHOKES_ON_RSA))
3342 s->supported_auths_mask &= ~(1 << SSH1_AUTH_RSA);
bea1ef5f 3343
51470298 3344 ssh->v1_local_protoflags =
3345 ssh->v1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED;
3346 ssh->v1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER;
b96dc54c 3347
c5e9c988 3348 MD5Init(&md5c);
51470298 3349 MD5Update(&md5c, s->keystr2, hostkey.bytes);
3350 MD5Update(&md5c, s->keystr1, servkey.bytes);
0016d70b 3351 MD5Update(&md5c, cookie, 8);
51470298 3352 MD5Final(s->session_id, &md5c);
374330e2 3353
32874aea 3354 for (i = 0; i < 32; i++)
51470298 3355 ssh->session_key[i] = random_byte();
374330e2 3356
0016d70b 3357 /*
3358 * Verify that the `bits' and `bytes' parameters match.
3359 */
3360 if (hostkey.bits > hostkey.bytes * 8 ||
3361 servkey.bits > servkey.bytes * 8) {
2e85c969 3362 bombout(("SSH-1 public keys were badly formatted"));
0016d70b 3363 crStop(0);
3364 }
3365
51470298 3366 s->len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
374330e2 3367
3d88e64d 3368 s->rsabuf = snewn(s->len, unsigned char);
374330e2 3369
89ee5268 3370 /*
3371 * Verify the host key.
3372 */
3373 {
32874aea 3374 /*
3375 * First format the key into a string.
3376 */
3377 int len = rsastr_len(&hostkey);
3378 char fingerprint[100];
3d88e64d 3379 char *keystr = snewn(len, char);
32874aea 3380 rsastr_fmt(keystr, &hostkey);
3381 rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey);
3d9449a1 3382
3383 ssh_set_frozen(ssh, 1);
3384 s->dlgret = verify_ssh_host_key(ssh->frontend,
3385 ssh->savedhost, ssh->savedport,
3386 "rsa", keystr, fingerprint,
3387 ssh_dialog_callback, ssh);
32874aea 3388 sfree(keystr);
3d9449a1 3389 if (s->dlgret < 0) {
3390 do {
3391 crReturn(0);
3392 if (pktin) {
3393 bombout(("Unexpected data from server while waiting"
3394 " for user host key response"));
3395 crStop(0);
3396 }
3397 } while (pktin || inlen > 0);
3398 s->dlgret = ssh->user_response;
3399 }
3400 ssh_set_frozen(ssh, 0);
3401
3402 if (s->dlgret == 0) {
9e296bfa 3403 ssh_disconnect(ssh, "User aborted at host key verification",
3404 NULL, 0, TRUE);
2b3f6c19 3405 crStop(0);
3d9449a1 3406 }
32874aea 3407 }
3408
3409 for (i = 0; i < 32; i++) {
51470298 3410 s->rsabuf[i] = ssh->session_key[i];
374330e2 3411 if (i < 16)
51470298 3412 s->rsabuf[i] ^= s->session_id[i];
374330e2 3413 }
3414
3415 if (hostkey.bytes > servkey.bytes) {
0016d70b 3416 ret = rsaencrypt(s->rsabuf, 32, &servkey);
3417 if (ret)
3418 ret = rsaencrypt(s->rsabuf, servkey.bytes, &hostkey);
374330e2 3419 } else {
0016d70b 3420 ret = rsaencrypt(s->rsabuf, 32, &hostkey);
3421 if (ret)
3422 ret = rsaencrypt(s->rsabuf, hostkey.bytes, &servkey);
3423 }
3424 if (!ret) {
2e85c969 3425 bombout(("SSH-1 public key encryptions failed due to bad formatting"));
0016d70b 3426 crStop(0);
374330e2 3427 }
3428
c5e9c988 3429 logevent("Encrypted session key");
3430
ca20bfcf 3431 {
3432 int cipher_chosen = 0, warn = 0;
3433 char *cipher_string = NULL;
51470298 3434 int i;
ca20bfcf 3435 for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) {
4a693cfc 3436 int next_cipher = conf_get_int_int(ssh->conf,
3437 CONF_ssh_cipherlist, i);
ca20bfcf 3438 if (next_cipher == CIPHER_WARN) {
3439 /* If/when we choose a cipher, warn about it */
3440 warn = 1;
3441 } else if (next_cipher == CIPHER_AES) {
3442 /* XXX Probably don't need to mention this. */
2e85c969 3443 logevent("AES not supported in SSH-1, skipping");
ca20bfcf 3444 } else {
3445 switch (next_cipher) {
51470298 3446 case CIPHER_3DES: s->cipher_type = SSH_CIPHER_3DES;
ca20bfcf 3447 cipher_string = "3DES"; break;
51470298 3448 case CIPHER_BLOWFISH: s->cipher_type = SSH_CIPHER_BLOWFISH;
ca20bfcf 3449 cipher_string = "Blowfish"; break;
51470298 3450 case CIPHER_DES: s->cipher_type = SSH_CIPHER_DES;
ca20bfcf 3451 cipher_string = "single-DES"; break;
3452 }
51470298 3453 if (s->supported_ciphers_mask & (1 << s->cipher_type))
ca20bfcf 3454 cipher_chosen = 1;
3455 }
3456 }
3457 if (!cipher_chosen) {
51470298 3458 if ((s->supported_ciphers_mask & (1 << SSH_CIPHER_3DES)) == 0)
2e85c969 3459 bombout(("Server violates SSH-1 protocol by not "
ca20bfcf 3460 "supporting 3DES encryption"));
3461 else
3462 /* shouldn't happen */
6b5cf8b4 3463 bombout(("No supported ciphers found"));
7ffdbc1a 3464 crStop(0);
a99a05c0 3465 }
ca20bfcf 3466
3467 /* Warn about chosen cipher if necessary. */
bb348ab1 3468 if (warn) {
3d9449a1 3469 ssh_set_frozen(ssh, 1);
3470 s->dlgret = askalg(ssh->frontend, "cipher", cipher_string,
3471 ssh_dialog_callback, ssh);
3472 if (s->dlgret < 0) {
3473 do {
3474 crReturn(0);
3475 if (pktin) {
3476 bombout(("Unexpected data from server while waiting"
3477 " for user response"));
3478 crStop(0);
3479 }
3480 } while (pktin || inlen > 0);
3481 s->dlgret = ssh->user_response;
3482 }
3483 ssh_set_frozen(ssh, 0);
3484 if (s->dlgret == 0) {
9e296bfa 3485 ssh_disconnect(ssh, "User aborted at cipher warning", NULL,
3486 0, TRUE);
96ccde8b 3487 crStop(0);
3d9449a1 3488 }
bb348ab1 3489 }
bea1ef5f 3490 }
ca20bfcf 3491
51470298 3492 switch (s->cipher_type) {
32874aea 3493 case SSH_CIPHER_3DES:
3494 logevent("Using 3DES encryption");
3495 break;
3496 case SSH_CIPHER_DES:
3497 logevent("Using single-DES encryption");
3498 break;
3499 case SSH_CIPHER_BLOWFISH:
3500 logevent("Using Blowfish encryption");
3501 break;
c5e9c988 3502 }
bea1ef5f 3503
51470298 3504 send_packet(ssh, SSH1_CMSG_SESSION_KEY,
3505 PKT_CHAR, s->cipher_type,
32874aea 3506 PKT_DATA, cookie, 8,
51470298 3507 PKT_CHAR, (s->len * 8) >> 8, PKT_CHAR, (s->len * 8) & 0xFF,
3508 PKT_DATA, s->rsabuf, s->len,
3509 PKT_INT, ssh->v1_local_protoflags, PKT_END);
fb09bf1c 3510
c5e9c988 3511 logevent("Trying to enable encryption...");
374330e2 3512
51470298 3513 sfree(s->rsabuf);
374330e2 3514
51470298 3515 ssh->cipher = (s->cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 :
3516 s->cipher_type == SSH_CIPHER_DES ? &ssh_des :
3517 &ssh_3des);
371e569c 3518 ssh->v1_cipher_ctx = ssh->cipher->make_context();
3519 ssh->cipher->sesskey(ssh->v1_cipher_ctx, ssh->session_key);
57356d63 3520 logeventf(ssh, "Initialised %s encryption", ssh->cipher->text_name);
374330e2 3521
0183b242 3522 ssh->crcda_ctx = crcda_make_context();
3523 logevent("Installing CRC compensation attack detector");
3524
679539d7 3525 if (servkey.modulus) {
3526 sfree(servkey.modulus);
3527 servkey.modulus = NULL;
3528 }
3529 if (servkey.exponent) {
3530 sfree(servkey.exponent);
3531 servkey.exponent = NULL;
3532 }
3533 if (hostkey.modulus) {
3534 sfree(hostkey.modulus);
3535 hostkey.modulus = NULL;
3536 }
3537 if (hostkey.exponent) {
3538 sfree(hostkey.exponent);
3539 hostkey.exponent = NULL;
3540 }
ff3187f6 3541 crWaitUntil(pktin);
374330e2 3542
ff3187f6 3543 if (pktin->type != SSH1_SMSG_SUCCESS) {
6b5cf8b4 3544 bombout(("Encryption not successfully enabled"));
7ffdbc1a 3545 crStop(0);
8d5de777 3546 }
374330e2 3547
c5e9c988 3548 logevent("Successfully started encryption");
3549
edd0cb8a 3550 fflush(stdout); /* FIXME eh? */
374330e2 3551 {
4a693cfc 3552 if ((ssh->username = get_remote_username(ssh->conf)) == NULL) {
edd0cb8a 3553 int ret; /* need not be kept over crReturn */
3554 s->cur_prompt = new_prompts(ssh->frontend);
3555 s->cur_prompt->to_server = TRUE;
3556 s->cur_prompt->name = dupstr("SSH login name");
b61f81bc 3557 add_prompt(s->cur_prompt, dupstr("login as: "), TRUE);
edd0cb8a 3558 ret = get_userpass_input(s->cur_prompt, NULL, 0);
3559 while (ret < 0) {
51470298 3560 ssh->send_ok = 1;
edd0cb8a 3561 crWaitUntil(!pktin);
3562 ret = get_userpass_input(s->cur_prompt, in, inlen);
3563 ssh->send_ok = 0;
3564 }
3565 if (!ret) {
3566 /*
3567 * Failed to get a username. Terminate.
3568 */
3569 free_prompts(s->cur_prompt);
3570 ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE);
3571 crStop(0);
32874aea 3572 }
4a693cfc 3573 ssh->username = dupstr(s->cur_prompt->prompts[0]->result);
edd0cb8a 3574 free_prompts(s->cur_prompt);
374330e2 3575 }
fb09bf1c 3576
4a693cfc 3577 send_packet(ssh, SSH1_CMSG_USER, PKT_STR, ssh->username, PKT_END);
c5e9c988 3578 {
4a693cfc 3579 char *userlog = dupprintf("Sent username \"%s\"", ssh->username);
c5e9c988 3580 logevent(userlog);
32874aea 3581 if (flags & FLAG_INTERACTIVE &&
3582 (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) {
51470298 3583 c_write_str(ssh, userlog);
edd0cb8a 3584 c_write_str(ssh, "\r\n");
3c8e959b 3585 }
edd0cb8a 3586 sfree(userlog);
c5e9c988 3587 }
374330e2 3588 }
3589
ff3187f6 3590 crWaitUntil(pktin);
374330e2 3591
d5b2c841 3592 if ((s->supported_auths_mask & (1 << SSH1_AUTH_RSA)) == 0) {
0df73905 3593 /* We must not attempt PK auth. Pretend we've already tried it. */
51470298 3594 s->tried_publickey = s->tried_agent = 1;
0df73905 3595 } else {
51470298 3596 s->tried_publickey = s->tried_agent = 0;
0df73905 3597 }
51470298 3598 s->tis_auth_refused = s->ccard_auth_refused = 0;
edd0cb8a 3599 /*
3600 * Load the public half of any configured keyfile for later use.
3601 */
4a693cfc 3602 s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
962468d4 3603 if (!filename_is_null(s->keyfile)) {
edd0cb8a 3604 int keytype;
3605 logeventf(ssh, "Reading private key file \"%.150s\"",
4a693cfc 3606 filename_to_str(s->keyfile));
3607 keytype = key_type(s->keyfile);
edd0cb8a 3608 if (keytype == SSH_KEYTYPE_SSH1) {
3609 const char *error;
4a693cfc 3610 if (rsakey_pubblob(s->keyfile,
edd0cb8a 3611 &s->publickey_blob, &s->publickey_bloblen,
3612 &s->publickey_comment, &error)) {
4a693cfc 3613 s->publickey_encrypted = rsakey_encrypted(s->keyfile,
edd0cb8a 3614 NULL);
3615 } else {
3616 char *msgbuf;
3617 logeventf(ssh, "Unable to load private key (%s)", error);
3618 msgbuf = dupprintf("Unable to load private key file "
3619 "\"%.150s\" (%s)\r\n",
4a693cfc 3620 filename_to_str(s->keyfile),
edd0cb8a 3621 error);
3622 c_write_str(ssh, msgbuf);
3623 sfree(msgbuf);
3624 s->publickey_blob = NULL;
3625 }
3626 } else {
3627 char *msgbuf;
3628 logeventf(ssh, "Unable to use this key file (%s)",
3629 key_type_to_str(keytype));
3630 msgbuf = dupprintf("Unable to use key file \"%.150s\""
3631 " (%s)\r\n",
4a693cfc 3632 filename_to_str(s->keyfile),
edd0cb8a 3633 key_type_to_str(keytype));
3634 c_write_str(ssh, msgbuf);
3635 sfree(msgbuf);
51470298 3636 s->publickey_blob = NULL;
edd0cb8a 3637 }
396778f1 3638 } else
51470298 3639 s->publickey_blob = NULL;
7cca0d81 3640
ff3187f6 3641 while (pktin->type == SSH1_SMSG_FAILURE) {
51470298 3642 s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD;
614a20a0 3643
4a693cfc 3644 if (conf_get_int(ssh->conf, CONF_tryagent) && agent_exists() && !s->tried_agent) {
32874aea 3645 /*
3646 * Attempt RSA authentication using Pageant.
3647 */
32874aea 3648 void *r;
3649
51470298 3650 s->authed = FALSE;
3651 s->tried_agent = 1;
32874aea 3652 logevent("Pageant is running. Requesting keys.");
3653
3654 /* Request the keys held by the agent. */
51470298 3655 PUT_32BIT(s->request, 1);
3656 s->request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES;
839f10db 3657 if (!agent_query(s->request, 5, &r, &s->responselen,
3658 ssh_agent_callback, ssh)) {
3659 do {
3660 crReturn(0);
ff3187f6 3661 if (pktin) {
839f10db 3662 bombout(("Unexpected data from server while waiting"
3663 " for agent response"));
3664 crStop(0);
3665 }
ff3187f6 3666 } while (pktin || inlen > 0);
839f10db 3667 r = ssh->agent_response;
3668 s->responselen = ssh->agent_response_len;
3669 }
51470298 3670 s->response = (unsigned char *) r;
3671 if (s->response && s->responselen >= 5 &&
3672 s->response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) {
3673 s->p = s->response + 5;
3674 s->nkeys = GET_32BIT(s->p);
3675 s->p += 4;
2e85c969 3676 logeventf(ssh, "Pageant has %d SSH-1 keys", s->nkeys);
51470298 3677 for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) {
94cd7c3a 3678 unsigned char *pkblob = s->p;
51470298 3679 s->p += 4;
0016d70b 3680 {
3681 int n, ok = FALSE;
3682 do { /* do while (0) to make breaking easy */
3683 n = ssh1_read_bignum
3684 (s->p, s->responselen-(s->p-s->response),
3685 &s->key.exponent);
3686 if (n < 0)
3687 break;
3688 s->p += n;
3689 n = ssh1_read_bignum
3690 (s->p, s->responselen-(s->p-s->response),
3691 &s->key.modulus);
3692 if (n < 0)
3693 break;
3694 s->p += n;
3695 if (s->responselen - (s->p-s->response) < 4)
3696 break;
3697 s->commentlen = GET_32BIT(s->p);
3698 s->p += 4;
3699 if (s->responselen - (s->p-s->response) <
3700 s->commentlen)
3701 break;
3702 s->commentp = (char *)s->p;
3703 s->p += s->commentlen;
3704 ok = TRUE;
3705 } while (0);
3706 if (!ok) {
3707 logevent("Pageant key list packet was truncated");
3708 break;
3709 }
3710 }
94cd7c3a 3711 if (s->publickey_blob) {
3712 if (!memcmp(pkblob, s->publickey_blob,
3713 s->publickey_bloblen)) {
3714 logeventf(ssh, "Pageant key #%d matches "
3715 "configured key file", s->keyi);
3716 s->tried_publickey = 1;
3717 } else
3718 /* Skip non-configured key */
3719 continue;
3720 }
3721 logeventf(ssh, "Trying Pageant key #%d", s->keyi);
51470298 3722 send_packet(ssh, SSH1_CMSG_AUTH_RSA,
3723 PKT_BIGNUM, s->key.modulus, PKT_END);
ff3187f6 3724 crWaitUntil(pktin);
3725 if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {
32874aea 3726 logevent("Key refused");
3727 continue;
3728 }
3729 logevent("Received RSA challenge");
ff3187f6 3730 if ((s->challenge = ssh1_pkt_getmp(pktin)) == NULL) {
0016d70b 3731 bombout(("Server's RSA challenge was badly formatted"));
3732 crStop(0);
3733 }
3734
32874aea 3735 {
3736 char *agentreq, *q, *ret;
2d466ffd 3737 void *vret;
32874aea 3738 int len, retlen;
3739 len = 1 + 4; /* message type, bit count */
51470298 3740 len += ssh1_bignum_length(s->key.exponent);
3741 len += ssh1_bignum_length(s->key.modulus);
3742 len += ssh1_bignum_length(s->challenge);
32874aea 3743 len += 16; /* session id */
3744 len += 4; /* response format */
3d88e64d 3745 agentreq = snewn(4 + len, char);
32874aea 3746 PUT_32BIT(agentreq, len);
3747 q = agentreq + 4;
3748 *q++ = SSH1_AGENTC_RSA_CHALLENGE;
51470298 3749 PUT_32BIT(q, bignum_bitcount(s->key.modulus));
32874aea 3750 q += 4;
51470298 3751 q += ssh1_write_bignum(q, s->key.exponent);
3752 q += ssh1_write_bignum(q, s->key.modulus);
3753 q += ssh1_write_bignum(q, s->challenge);
3754 memcpy(q, s->session_id, 16);
32874aea 3755 q += 16;
3756 PUT_32BIT(q, 1); /* response format */
839f10db 3757 if (!agent_query(agentreq, len + 4, &vret, &retlen,
3758 ssh_agent_callback, ssh)) {
3759 sfree(agentreq);
3760 do {
3761 crReturn(0);
ff3187f6 3762 if (pktin) {
839f10db 3763 bombout(("Unexpected data from server"
3764 " while waiting for agent"
3765 " response"));
3766 crStop(0);
3767 }
ff3187f6 3768 } while (pktin || inlen > 0);
839f10db 3769 vret = ssh->agent_response;
3770 retlen = ssh->agent_response_len;
3771 } else
3772 sfree(agentreq);
2d466ffd 3773 ret = vret;
32874aea 3774 if (ret) {
3775 if (ret[4] == SSH1_AGENT_RSA_RESPONSE) {
3776 logevent("Sending Pageant's response");
51470298 3777 send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE,
32874aea 3778 PKT_DATA, ret + 5, 16,
3779 PKT_END);
3780 sfree(ret);
ff3187f6 3781 crWaitUntil(pktin);
3782 if (pktin->type == SSH1_SMSG_SUCCESS) {
32874aea 3783 logevent
3784 ("Pageant's response accepted");
3785 if (flags & FLAG_VERBOSE) {
51470298 3786 c_write_str(ssh, "Authenticated using"
3787 " RSA key \"");
3788 c_write(ssh, s->commentp,
3789 s->commentlen);
3790 c_write_str(ssh, "\" from agent\r\n");
32874aea 3791 }
51470298 3792 s->authed = TRUE;
32874aea 3793 } else
3794 logevent
3795 ("Pageant's response not accepted");
3796 } else {
3797 logevent
3798 ("Pageant failed to answer challenge");
3799 sfree(ret);
3800 }
3801 } else {
3802 logevent("No reply received from Pageant");
3803 }
3804 }
51470298 3805 freebn(s->key.exponent);
3806 freebn(s->key.modulus);
3807 freebn(s->challenge);
3808 if (s->authed)
32874aea 3809 break;
3810 }
29b1d0b3 3811 sfree(s->response);
94cd7c3a 3812 if (s->publickey_blob && !s->tried_publickey)
3813 logevent("Configured key file not in Pageant");
5ff99b8e 3814 } else {
3815 logevent("Failed to get reply from Pageant");
3816 }
51470298 3817 if (s->authed)
32874aea 3818 break;
3819 }
edd0cb8a 3820 if (s->publickey_blob && !s->tried_publickey) {
3821 /*
3822 * Try public key authentication with the specified
3823 * key file.
3824 */
3825 int got_passphrase; /* need not be kept over crReturn */
3826 if (flags & FLAG_VERBOSE)
3827 c_write_str(ssh, "Trying public key authentication.\r\n");
4a693cfc 3828 s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
edd0cb8a 3829 logeventf(ssh, "Trying public key \"%s\"",
4a693cfc 3830 filename_to_str(s->keyfile));
edd0cb8a 3831 s->tried_publickey = 1;
3832 got_passphrase = FALSE;
3833 while (!got_passphrase) {
3834 /*
3835 * Get a passphrase, if necessary.
3836 */
3837 char *passphrase = NULL; /* only written after crReturn */
3838 const char *error;
3839 if (!s->publickey_encrypted) {
3840 if (flags & FLAG_VERBOSE)
3841 c_write_str(ssh, "No passphrase required.\r\n");
3842 passphrase = NULL;
3843 } else {
3844 int ret; /* need not be kept over crReturn */
3845 s->cur_prompt = new_prompts(ssh->frontend);
3846 s->cur_prompt->to_server = FALSE;
3847 s->cur_prompt->name = dupstr("SSH key passphrase");
3848 add_prompt(s->cur_prompt,
3849 dupprintf("Passphrase for key \"%.100s\": ",
b61f81bc 3850 s->publickey_comment), FALSE);
edd0cb8a 3851 ret = get_userpass_input(s->cur_prompt, NULL, 0);
3852 while (ret < 0) {
3853 ssh->send_ok = 1;
3854 crWaitUntil(!pktin);
3855 ret = get_userpass_input(s->cur_prompt, in, inlen);
3856 ssh->send_ok = 0;
3857 }
3858 if (!ret) {
3859 /* Failed to get a passphrase. Terminate. */
3860 free_prompts(s->cur_prompt);
3861 ssh_disconnect(ssh, NULL, "Unable to authenticate",
3862 0, TRUE);
3863 crStop(0);
3864 }
3865 passphrase = dupstr(s->cur_prompt->prompts[0]->result);
3866 free_prompts(s->cur_prompt);
3867 }
3868 /*
3869 * Try decrypting key with passphrase.
3870 */
4a693cfc 3871 s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
3872 ret = loadrsakey(s->keyfile, &s->key, passphrase,
edd0cb8a 3873 &error);
3874 if (passphrase) {
dfb88efd 3875 smemclr(passphrase, strlen(passphrase));
edd0cb8a 3876 sfree(passphrase);
3877 }
3878 if (ret == 1) {
3879 /* Correct passphrase. */
3880 got_passphrase = TRUE;
3881 } else if (ret == 0) {
3882 c_write_str(ssh, "Couldn't load private key from ");
4a693cfc 3883 c_write_str(ssh, filename_to_str(s->keyfile));
edd0cb8a 3884 c_write_str(ssh, " (");
3885 c_write_str(ssh, error);
3886 c_write_str(ssh, ").\r\n");
3887 got_passphrase = FALSE;
3888 break; /* go and try something else */
3889 } else if (ret == -1) {
3890 c_write_str(ssh, "Wrong passphrase.\r\n"); /* FIXME */
edd0cb8a 3891 got_passphrase = FALSE;
3892 /* and try again */
43536490 3893 } else {
3894 assert(0 && "unexpected return from loadrsakey()");
edc23a8a 3895 got_passphrase = FALSE; /* placate optimisers */
edd0cb8a 3896 }
3897 }
3898
3899 if (got_passphrase) {
3900
3901 /*
3902 * Send a public key attempt.
3903 */
3904 send_packet(ssh, SSH1_CMSG_AUTH_RSA,
3905 PKT_BIGNUM, s->key.modulus, PKT_END);
3906
3907 crWaitUntil(pktin);
3908 if (pktin->type == SSH1_SMSG_FAILURE) {
3909 c_write_str(ssh, "Server refused our public key.\r\n");
7b575324 3910 continue; /* go and try something else */
edd0cb8a 3911 }
3912 if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {
3913 bombout(("Bizarre response to offer of public key"));
3914 crStop(0);
3915 }
3916
3917 {
3918 int i;
3919 unsigned char buffer[32];
3920 Bignum challenge, response;
3921
3922 if ((challenge = ssh1_pkt_getmp(pktin)) == NULL) {
3923 bombout(("Server's RSA challenge was badly formatted"));
3924 crStop(0);
3925 }
3926 response = rsadecrypt(challenge, &s->key);
3927 freebn(s->key.private_exponent);/* burn the evidence */
3928
3929 for (i = 0; i < 32; i++) {
3930 buffer[i] = bignum_byte(response, 31 - i);
3931 }
3932
3933 MD5Init(&md5c);
3934 MD5Update(&md5c, buffer, 32);
3935 MD5Update(&md5c, s->session_id, 16);
3936 MD5Final(buffer, &md5c);
3937
3938 send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE,
3939 PKT_DATA, buffer, 16, PKT_END);
3940
3941 freebn(challenge);
3942 freebn(response);
3943 }
3944
3945 crWaitUntil(pktin);
3946 if (pktin->type == SSH1_SMSG_FAILURE) {
3947 if (flags & FLAG_VERBOSE)
3948 c_write_str(ssh, "Failed to authenticate with"
3949 " our public key.\r\n");
7b575324 3950 continue; /* go and try something else */
edd0cb8a 3951 } else if (pktin->type != SSH1_SMSG_SUCCESS) {
3952 bombout(("Bizarre response to RSA authentication response"));
3953 crStop(0);
3954 }
3955
3956 break; /* we're through! */
3957 }
3958
3959 }
3960
3961 /*
3962 * Otherwise, try various forms of password-like authentication.
3963 */
3964 s->cur_prompt = new_prompts(ssh->frontend);
32874aea 3965
4a693cfc 3966 if (conf_get_int(ssh->conf, CONF_try_tis_auth) &&
51470298 3967 (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) &&
3968 !s->tis_auth_refused) {
3969 s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE;
32874aea 3970 logevent("Requested TIS authentication");
51470298 3971 send_packet(ssh, SSH1_CMSG_AUTH_TIS, PKT_END);
ff3187f6 3972 crWaitUntil(pktin);
3973 if (pktin->type != SSH1_SMSG_AUTH_TIS_CHALLENGE) {
32874aea 3974 logevent("TIS authentication declined");
3975 if (flags & FLAG_INTERACTIVE)
51470298 3976 c_write_str(ssh, "TIS authentication refused.\r\n");
3977 s->tis_auth_refused = 1;
614a20a0 3978 continue;
32874aea 3979 } else {
0016d70b 3980 char *challenge;
3981 int challengelen;
edd0cb8a 3982 char *instr_suf, *prompt;
0016d70b 3983
ff3187f6 3984 ssh_pkt_getstring(pktin, &challenge, &challengelen);
0016d70b 3985 if (!challenge) {
3986 bombout(("TIS challenge packet was badly formed"));
3987 crStop(0);
3988 }
32874aea 3989 logevent("Received TIS challenge");
edd0cb8a 3990 s->cur_prompt->to_server = TRUE;
3991 s->cur_prompt->name = dupstr("SSH TIS authentication");
614a20a0 3992 /* Prompt heuristic comes from OpenSSH */
edd0cb8a 3993 if (memchr(challenge, '\n', challengelen)) {
3994 instr_suf = dupstr("");
3995 prompt = dupprintf("%.*s", challengelen, challenge);
3996 } else {
3997 instr_suf = dupprintf("%.*s", challengelen, challenge);
3998 prompt = dupstr("Response: ");
3999 }
4000 s->cur_prompt->instruction =
4001 dupprintf("Using TIS authentication.%s%s",
4002 (*instr_suf) ? "\n" : "",
4003 instr_suf);
4004 s->cur_prompt->instr_reqd = TRUE;
b61f81bc 4005 add_prompt(s->cur_prompt, prompt, FALSE);
edd0cb8a 4006 sfree(instr_suf);
32874aea 4007 }
4008 }
4a693cfc 4009 if (conf_get_int(ssh->conf, CONF_try_tis_auth) &&
51470298 4010 (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) &&
4011 !s->ccard_auth_refused) {
4012 s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE;
32874aea 4013 logevent("Requested CryptoCard authentication");
51470298 4014 send_packet(ssh, SSH1_CMSG_AUTH_CCARD, PKT_END);
ff3187f6 4015 crWaitUntil(pktin);
4016 if (pktin->type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) {
32874aea 4017 logevent("CryptoCard authentication declined");
51470298 4018 c_write_str(ssh, "CryptoCard authentication refused.\r\n");
4019 s->ccard_auth_refused = 1;
614a20a0 4020 continue;
32874aea 4021 } else {
0016d70b 4022 char *challenge;
4023 int challengelen;
edd0cb8a 4024 char *instr_suf, *prompt;
0016d70b 4025
ff3187f6 4026 ssh_pkt_getstring(pktin, &challenge, &challengelen);
0016d70b 4027 if (!challenge) {
4028 bombout(("CryptoCard challenge packet was badly formed"));
4029 crStop(0);
4030 }
32874aea 4031 logevent("Received CryptoCard challenge");
edd0cb8a 4032 s->cur_prompt->to_server = TRUE;
4033 s->cur_prompt->name = dupstr("SSH CryptoCard authentication");
4034 s->cur_prompt->name_reqd = FALSE;
4035 /* Prompt heuristic comes from OpenSSH */
4036 if (memchr(challenge, '\n', challengelen)) {
4037 instr_suf = dupstr("");
4038 prompt = dupprintf("%.*s", challengelen, challenge);
4039 } else {
4040 instr_suf = dupprintf("%.*s", challengelen, challenge);
4041 prompt = dupstr("Response: ");
4042 }
4043 s->cur_prompt->instruction =
4044 dupprintf("Using CryptoCard authentication.%s%s",
4045 (*instr_suf) ? "\n" : "",
4046 instr_suf);
4047 s->cur_prompt->instr_reqd = TRUE;
b61f81bc 4048 add_prompt(s->cur_prompt, prompt, FALSE);
edd0cb8a 4049 sfree(instr_suf);
32874aea 4050 }
4051 }
51470298 4052 if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {
8f9e3231 4053 if ((s->supported_auths_mask & (1 << SSH1_AUTH_PASSWORD)) == 0) {
4054 bombout(("No supported authentication methods available"));
4055 crStop(0);
4056 }
edd0cb8a 4057 s->cur_prompt->to_server = TRUE;
4058 s->cur_prompt->name = dupstr("SSH password");
4a693cfc 4059 add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
4060 ssh->username, ssh->savedhost),
b61f81bc 4061 FALSE);
32874aea 4062 }
a52f067e 4063
614a20a0 4064 /*
4065 * Show password prompt, having first obtained it via a TIS
4066 * or CryptoCard exchange if we're doing TIS or CryptoCard
4067 * authentication.
4068 */
edd0cb8a 4069 {
4070 int ret; /* need not be kept over crReturn */
4071 ret = get_userpass_input(s->cur_prompt, NULL, 0);
4072 while (ret < 0) {
4073 ssh->send_ok = 1;
4074 crWaitUntil(!pktin);
4075 ret = get_userpass_input(s->cur_prompt, in, inlen);
4076 ssh->send_ok = 0;
4077 }
4078 if (!ret) {
32874aea 4079 /*
edd0cb8a 4080 * Failed to get a password (for example
32874aea 4081 * because one was supplied on the command line
4082 * which has already failed to work). Terminate.
4083 */
edd0cb8a 4084 free_prompts(s->cur_prompt);
4085 ssh_disconnect(ssh, NULL, "Unable to authenticate", 0, TRUE);
4086 crStop(0);
32874aea 4087 }
32874aea 4088 }
4089
edd0cb8a 4090 if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {
32874aea 4091 /*
edd0cb8a 4092 * Defence against traffic analysis: we send a
4093 * whole bunch of packets containing strings of
4094 * different lengths. One of these strings is the
4095 * password, in a SSH1_CMSG_AUTH_PASSWORD packet.
4096 * The others are all random data in
4097 * SSH1_MSG_IGNORE packets. This way a passive
4098 * listener can't tell which is the password, and
4099 * hence can't deduce the password length.
4100 *
4101 * Anybody with a password length greater than 16
4102 * bytes is going to have enough entropy in their
4103 * password that a listener won't find it _that_
4104 * much help to know how long it is. So what we'll
4105 * do is:
4106 *
4107 * - if password length < 16, we send 15 packets
4108 * containing string lengths 1 through 15
4109 *
4110 * - otherwise, we let N be the nearest multiple
4111 * of 8 below the password length, and send 8
4112 * packets containing string lengths N through
4113 * N+7. This won't obscure the order of
4114 * magnitude of the password length, but it will
4115 * introduce a bit of extra uncertainty.
4116 *
bf982899 4117 * A few servers can't deal with SSH1_MSG_IGNORE, at
4118 * least in this context. For these servers, we need
4119 * an alternative defence. We make use of the fact
4120 * that the password is interpreted as a C string:
4121 * so we can append a NUL, then some random data.
edd0cb8a 4122 *
bf982899 4123 * A few servers can deal with neither SSH1_MSG_IGNORE
4124 * here _nor_ a padded password string.
4125 * For these servers we are left with no defences
edd0cb8a 4126 * against password length sniffing.
32874aea 4127 */
bf982899 4128 if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE) &&
4129 !(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) {
32874aea 4130 /*
edd0cb8a 4131 * The server can deal with SSH1_MSG_IGNORE, so
4132 * we can use the primary defence.
32874aea 4133 */
edd0cb8a 4134 int bottom, top, pwlen, i;
4135 char *randomstr;
32874aea 4136
edd0cb8a 4137 pwlen = strlen(s->cur_prompt->prompts[0]->result);
4138 if (pwlen < 16) {
4139 bottom = 0; /* zero length passwords are OK! :-) */
4140 top = 15;
4141 } else {
4142 bottom = pwlen & ~7;
4143 top = bottom + 7;
4144 }
32874aea 4145
edd0cb8a 4146 assert(pwlen >= bottom && pwlen <= top);
32874aea 4147
edd0cb8a 4148 randomstr = snewn(top + 1, char);
32874aea 4149
edd0cb8a 4150 for (i = bottom; i <= top; i++) {
4151 if (i == pwlen) {
4152 defer_packet(ssh, s->pwpkt_type,
4153 PKTT_PASSWORD, PKT_STR,
4154 s->cur_prompt->prompts[0]->result,
4155 PKTT_OTHER, PKT_END);
4156 } else {
4157 for (j = 0; j < i; j++) {
4158 do {
4159 randomstr[j] = random_byte();
4160 } while (randomstr[j] == '\0');
32874aea 4161 }
edd0cb8a 4162 randomstr[i] = '\0';
4163 defer_packet(ssh, SSH1_MSG_IGNORE,
4164 PKT_STR, randomstr, PKT_END);
32874aea 4165 }
edd0cb8a 4166 }
4167 logevent("Sending password with camouflage packets");
4168 ssh_pkt_defersend(ssh);
4169 sfree(randomstr);
4170 }
4171 else if (!(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) {
4172 /*
4173 * The server can't deal with SSH1_MSG_IGNORE
4174 * but can deal with padded passwords, so we
4175 * can use the secondary defence.
4176 */
4177 char string[64];
4178 char *ss;
4179 int len;
4180
4181 len = strlen(s->cur_prompt->prompts[0]->result);
4182 if (len < sizeof(string)) {
4183 ss = string;
4184 strcpy(string, s->cur_prompt->prompts[0]->result);
4185 len++; /* cover the zero byte */
4186 while (len < sizeof(string)) {
4187 string[len++] = (char) random_byte();
bd358db1 4188 }
bd358db1 4189 } else {
edd0cb8a 4190 ss = s->cur_prompt->prompts[0]->result;
32874aea 4191 }
edd0cb8a 4192 logevent("Sending length-padded password");
9a10ecf4 4193 send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD,
edd0cb8a 4194 PKT_INT, len, PKT_DATA, ss, len,
4195 PKTT_OTHER, PKT_END);
4196 } else {
4197 /*
bf982899 4198 * The server is believed unable to cope with
4199 * any of our password camouflage methods.
edd0cb8a 4200 */
4201 int len;
4202 len = strlen(s->cur_prompt->prompts[0]->result);
4203 logevent("Sending unpadded password");
4204 send_packet(ssh, s->pwpkt_type,
4205 PKTT_PASSWORD, PKT_INT, len,
4206 PKT_DATA, s->cur_prompt->prompts[0]->result, len,
4207 PKTT_OTHER, PKT_END);
32874aea 4208 }
edd0cb8a 4209 } else {
4210 send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD,
4211 PKT_STR, s->cur_prompt->prompts[0]->result,
4212 PKTT_OTHER, PKT_END);
32874aea 4213 }
c5e9c988 4214 logevent("Sent password");
edd0cb8a 4215 free_prompts(s->cur_prompt);
ff3187f6 4216 crWaitUntil(pktin);
4217 if (pktin->type == SSH1_SMSG_FAILURE) {
32874aea 4218 if (flags & FLAG_VERBOSE)
51470298 4219 c_write_str(ssh, "Access denied\r\n");
c5e9c988 4220 logevent("Authentication refused");
ff3187f6 4221 } else if (pktin->type != SSH1_SMSG_SUCCESS) {
4222 bombout(("Strange packet received, type %d", pktin->type));
7ffdbc1a 4223 crStop(0);
374330e2 4224 }
4225 }
4226
edd0cb8a 4227 /* Clear up */
4228 if (s->publickey_blob) {
4229 sfree(s->publickey_blob);
4230 sfree(s->publickey_comment);
4231 }
4232
c5e9c988 4233 logevent("Authentication successful");
4234
fb09bf1c 4235 crFinish(1);
4236}
4237
bc06669b 4238static void ssh_channel_try_eof(struct ssh_channel *c)
4239{
4240 Ssh ssh = c->ssh;
4241 assert(c->pending_eof); /* precondition for calling us */
4242 if (c->halfopen)
4243 return; /* can't close: not even opened yet */
4244 if (ssh->version == 2 && bufchain_size(&c->v.v2.outbuffer) > 0)
4245 return; /* can't send EOF: pending outgoing data */
4246
3620f307 4247 c->pending_eof = FALSE; /* we're about to send it */
bc06669b 4248 if (ssh->version == 1) {
4249 send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid,
4250 PKT_END);
4251 c->closes |= CLOSES_SENT_EOF;
4252 } else {
4253 struct Packet *pktout;
4254 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF);
4255 ssh2_pkt_adduint32(pktout, c->remoteid);
4256 ssh2_pkt_send(ssh, pktout);
4257 c->closes |= CLOSES_SENT_EOF;
3620f307 4258 ssh2_channel_check_close(c);
bc06669b 4259 }
bc06669b 4260}
4261
4262void sshfwd_write_eof(struct ssh_channel *c)
32874aea 4263{
51470298 4264 Ssh ssh = c->ssh;
4265
1ef619ae 4266 if (ssh->state == SSH_STATE_CLOSED)
36f94d1f 4267 return;
36f94d1f 4268
bc06669b 4269 if (c->closes & CLOSES_SENT_EOF)
4270 return;
78280721 4271
bc06669b 4272 c->pending_eof = TRUE;
4273 ssh_channel_try_eof(c);
32874aea 4274}
4275
c6b41791 4276void sshfwd_unclean_close(struct ssh_channel *c)
4277{
4278 Ssh ssh = c->ssh;
4279 struct Packet *pktout;
4280
4281 if (ssh->state == SSH_STATE_CLOSED)
4282 return;
4283
96210393 4284 if (!(c->closes & CLOSES_SENT_CLOSE)) {
4285 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
4286 ssh2_pkt_adduint32(pktout, c->remoteid);
4287 ssh2_pkt_send(ssh, pktout);
4288 c->closes |= CLOSES_SENT_EOF | CLOSES_SENT_CLOSE;
4289 }
c6b41791 4290
c6b41791 4291 switch (c->type) {
4292 case CHAN_X11:
4293 x11_close(c->u.x11.s);
4294 break;
4295 case CHAN_SOCKDATA:
4296 case CHAN_SOCKDATA_DORMANT:
4297 pfd_close(c->u.pfd.s);
4298 break;
4299 }
4300 c->type = CHAN_ZOMBIE;
96210393 4301
c6b41791 4302 ssh2_channel_check_close(c);
4303}
4304
5471d09a 4305int sshfwd_write(struct ssh_channel *c, char *buf, int len)
32874aea 4306{
51470298 4307 Ssh ssh = c->ssh;
4308
1ef619ae 4309 if (ssh->state == SSH_STATE_CLOSED)
36f94d1f 4310 return 0;
36f94d1f 4311
51470298 4312 if (ssh->version == 1) {
4313 send_packet(ssh, SSH1_MSG_CHANNEL_DATA,
32874aea 4314 PKT_INT, c->remoteid,
a2724fba 4315 PKT_INT, len, PKTT_DATA, PKT_DATA, buf, len,
9a10ecf4 4316 PKTT_OTHER, PKT_END);
5471d09a 4317 /*
2e85c969 4318 * In SSH-1 we can return 0 here - implying that forwarded
5471d09a 4319 * connections are never individually throttled - because
4320 * the only circumstance that can cause throttling will be
4321 * the whole SSH connection backing up, in which case
4322 * _everything_ will be throttled as a whole.
4323 */
4324 return 0;
783415f8 4325 } else {
32874aea 4326 ssh2_add_channel_data(c, buf, len);
5471d09a 4327 return ssh2_try_send(c);
4328 }
4329}
4330
4331void sshfwd_unthrottle(struct ssh_channel *c, int bufsize)
4332{
51470298 4333 Ssh ssh = c->ssh;
ff68564b 4334 int buflimit;
51470298 4335
1ef619ae 4336 if (ssh->state == SSH_STATE_CLOSED)
36f94d1f 4337 return;
36f94d1f 4338
51470298 4339 if (ssh->version == 1) {
ff68564b 4340 buflimit = SSH1_BUFFER_LIMIT;
5471d09a 4341 } else {
ff68564b 4342 buflimit = c->v.v2.locmaxwin;
4343 ssh2_set_window(c, bufsize < buflimit ? buflimit - bufsize : 0);
4344 }
4345 if (c->throttling_conn && bufsize <= buflimit) {
4346 c->throttling_conn = 0;
4347 ssh_throttle_conn(ssh, -1);
783415f8 4348 }
9c964e85 4349}
4350
06fadff5 4351static void ssh_queueing_handler(Ssh ssh, struct Packet *pktin)
4352{
4353 struct queued_handler *qh = ssh->qhead;
4354
4355 assert(qh != NULL);
4356
4357 assert(pktin->type == qh->msg1 || pktin->type == qh->msg2);
4358
4359 if (qh->msg1 > 0) {
4360 assert(ssh->packet_dispatch[qh->msg1] == ssh_queueing_handler);
bc074147 4361 ssh->packet_dispatch[qh->msg1] = ssh->q_saved_handler1;
06fadff5 4362 }
4363 if (qh->msg2 > 0) {
4364 assert(ssh->packet_dispatch[qh->msg2] == ssh_queueing_handler);
bc074147 4365 ssh->packet_dispatch[qh->msg2] = ssh->q_saved_handler2;
06fadff5 4366 }
4367
4368 if (qh->next) {
4369 ssh->qhead = qh->next;
4370
4371 if (ssh->qhead->msg1 > 0) {
bc074147 4372 ssh->q_saved_handler1 = ssh->packet_dispatch[ssh->qhead->msg1];
06fadff5 4373 ssh->packet_dispatch[ssh->qhead->msg1] = ssh_queueing_handler;
4374 }
4375 if (ssh->qhead->msg2 > 0) {
bc074147 4376 ssh->q_saved_handler2 = ssh->packet_dispatch[ssh->qhead->msg2];
06fadff5 4377 ssh->packet_dispatch[ssh->qhead->msg2] = ssh_queueing_handler;
4378 }
4379 } else {
4380 ssh->qhead = ssh->qtail = NULL;
06fadff5 4381 }
4382
4383 qh->handler(ssh, pktin, qh->ctx);
4384
4385 sfree(qh);
4386}
4387
4388static void ssh_queue_handler(Ssh ssh, int msg1, int msg2,
4389 chandler_fn_t handler, void *ctx)
4390{
4391 struct queued_handler *qh;
4392
4393 qh = snew(struct queued_handler);
4394 qh->msg1 = msg1;
4395 qh->msg2 = msg2;
4396 qh->handler = handler;
4397 qh->ctx = ctx;
4398 qh->next = NULL;
4399
4400 if (ssh->qtail == NULL) {
4401 ssh->qhead = qh;
4402
4403 if (qh->msg1 > 0) {
bc074147 4404 ssh->q_saved_handler1 = ssh->packet_dispatch[ssh->qhead->msg1];
06fadff5 4405 ssh->packet_dispatch[qh->msg1] = ssh_queueing_handler;
4406 }
4407 if (qh->msg2 > 0) {
bc074147 4408 ssh->q_saved_handler2 = ssh->packet_dispatch[ssh->qhead->msg2];
06fadff5 4409 ssh->packet_dispatch[qh->msg2] = ssh_queueing_handler;
4410 }
4411 } else {
4412 ssh->qtail->next = qh;
4413 }
4414 ssh->qtail = qh;
4415}
4416
4417static void ssh_rportfwd_succfail(Ssh ssh, struct Packet *pktin, void *ctx)
4418{
4419 struct ssh_rportfwd *rpf, *pf = (struct ssh_rportfwd *)ctx;
4420
4421 if (pktin->type == (ssh->version == 1 ? SSH1_SMSG_SUCCESS :
4422 SSH2_MSG_REQUEST_SUCCESS)) {
4423 logeventf(ssh, "Remote port forwarding from %s enabled",
4424 pf->sportdesc);
4425 } else {
4426 logeventf(ssh, "Remote port forwarding from %s refused",
4427 pf->sportdesc);
4428
4429 rpf = del234(ssh->rportfwds, pf);
4430 assert(rpf == pf);
8aea57fd 4431 pf->pfrec->remote = NULL;
fda2feb1 4432 free_rportfwd(pf);
06fadff5 4433 }
4434}
4435
4a693cfc 4436static void ssh_setup_portfwd(Ssh ssh, Conf *conf)
06fadff5 4437{
84328ddb 4438 struct ssh_portfwd *epf;
4439 int i;
4a693cfc 4440 char *key, *val;
06fadff5 4441
fda2feb1 4442 if (!ssh->portfwds) {
4443 ssh->portfwds = newtree234(ssh_portcmp);
4444 } else {
4445 /*
4446 * Go through the existing port forwardings and tag them
84328ddb 4447 * with status==DESTROY. Any that we want to keep will be
4448 * re-enabled (status==KEEP) as we go through the
4449 * configuration and find out which bits are the same as
4450 * they were before.
fda2feb1 4451 */
4452 struct ssh_portfwd *epf;
4453 int i;
4454 for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++)
84328ddb 4455 epf->status = DESTROY;
fda2feb1 4456 }
4457
4a693cfc 4458 for (val = conf_get_str_strs(conf, CONF_portfwd, NULL, &key);
4459 val != NULL;
4460 val = conf_get_str_strs(conf, CONF_portfwd, key, &key)) {
4461 char *kp, *kp2, *vp, *vp2;
84328ddb 4462 char address_family, type;
4463 int sport,dport,sserv,dserv;
4a693cfc 4464 char *sports, *dports, *saddr, *host;
4465
4466 kp = key;
84328ddb 4467
05581745 4468 address_family = 'A';
4469 type = 'L';
4a693cfc 4470 if (*kp == 'A' || *kp == '4' || *kp == '6')
4471 address_family = *kp++;
4472 if (*kp == 'L' || *kp == 'R')
4473 type = *kp++;
4474
4475 if ((kp2 = strchr(kp, ':')) != NULL) {
4476 /*
4477 * There's a colon in the middle of the source port
4478 * string, which means that the part before it is
4479 * actually a source address.
4480 */
4481 saddr = dupprintf("%.*s", (int)(kp2 - kp), kp);
4482 sports = kp2+1;
06fadff5 4483 } else {
4a693cfc 4484 saddr = NULL;
4485 sports = kp;
06fadff5 4486 }
4487 sport = atoi(sports);
4488 sserv = 0;
4489 if (sport == 0) {
4490 sserv = 1;
4491 sport = net_service_lookup(sports);
4492 if (!sport) {
4493 logeventf(ssh, "Service lookup failed for source"
4494 " port \"%s\"", sports);
4495 }
4496 }
4a693cfc 4497
4498 if (type == 'L' && !strcmp(val, "D")) {
4499 /* dynamic forwarding */
4500 host = NULL;
4501 dports = NULL;
4502 dport = -1;
4503 dserv = 0;
4504 type = 'D';
4505 } else {
4506 /* ordinary forwarding */
4507 vp = val;
4508 vp2 = vp + strcspn(vp, ":");
4509 host = dupprintf("%.*s", (int)(vp2 - vp), vp);
4510 if (vp2)
4511 vp2++;
4512 dports = vp2;
4513 dport = atoi(dports);
4514 dserv = 0;
4515 if (dport == 0) {
4516 dserv = 1;
4517 dport = net_service_lookup(dports);
4518 if (!dport) {
4519 logeventf(ssh, "Service lookup failed for destination"
4520 " port \"%s\"", dports);
4521 }
4522 }
4523 }
4524
06fadff5 4525 if (sport && dport) {
4526 /* Set up a description of the source port. */
fda2feb1 4527 struct ssh_portfwd *pfrec, *epfrec;
fda2feb1 4528
4529 pfrec = snew(struct ssh_portfwd);
4530 pfrec->type = type;
4a693cfc 4531 pfrec->saddr = saddr;
3fe92132 4532 pfrec->sserv = sserv ? dupstr(sports) : NULL;
fda2feb1 4533 pfrec->sport = sport;
4a693cfc 4534 pfrec->daddr = host;
3fe92132 4535 pfrec->dserv = dserv ? dupstr(dports) : NULL;
fda2feb1 4536 pfrec->dport = dport;
4537 pfrec->local = NULL;
4538 pfrec->remote = NULL;
05581745 4539 pfrec->addressfamily = (address_family == '4' ? ADDRTYPE_IPV4 :
4540 address_family == '6' ? ADDRTYPE_IPV6 :
4541 ADDRTYPE_UNSPEC);
fda2feb1 4542
4543 epfrec = add234(ssh->portfwds, pfrec);
4544 if (epfrec != pfrec) {
c00c1931 4545 if (epfrec->status == DESTROY) {
4546 /*
4547 * We already have a port forwarding up and running
4548 * with precisely these parameters. Hence, no need
4549 * to do anything; simply re-tag the existing one
4550 * as KEEP.
4551 */
4552 epfrec->status = KEEP;
4553 }
fda2feb1 4554 /*
c00c1931 4555 * Anything else indicates that there was a duplicate
4556 * in our input, which we'll silently ignore.
fda2feb1 4557 */
fda2feb1 4558 free_portfwd(pfrec);
84328ddb 4559 } else {
4560 pfrec->status = CREATE;
4561 }
4a693cfc 4562 } else {
4563 sfree(saddr);
4564 sfree(host);
84328ddb 4565 }
4566 }
4567
4568 /*
4569 * Now go through and destroy any port forwardings which were
4570 * not re-enabled.
4571 */
4572 for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++)
4573 if (epf->status == DESTROY) {
4574 char *message;
4575
4576 message = dupprintf("%s port forwarding from %s%s%d",
4577 epf->type == 'L' ? "local" :
4578 epf->type == 'R' ? "remote" : "dynamic",
4579 epf->saddr ? epf->saddr : "",
4580 epf->saddr ? ":" : "",
4581 epf->sport);
4582
4583 if (epf->type != 'D') {
4584 char *msg2 = dupprintf("%s to %s:%d", message,
4585 epf->daddr, epf->dport);
4586 sfree(message);
4587 message = msg2;
4588 }
4589
4590 logeventf(ssh, "Cancelling %s", message);
4591 sfree(message);
4592
8aea57fd 4593 /* epf->remote or epf->local may be NULL if setting up a
4594 * forwarding failed. */
84328ddb 4595 if (epf->remote) {
4596 struct ssh_rportfwd *rpf = epf->remote;
4597 struct Packet *pktout;
4598
4599 /*
4600 * Cancel the port forwarding at the server
4601 * end.
4602 */
4603 if (ssh->version == 1) {
4604 /*
4605 * We cannot cancel listening ports on the
2e85c969 4606 * server side in SSH-1! There's no message
84328ddb 4607 * to support it. Instead, we simply remove
4608 * the rportfwd record from the local end
4609 * so that any connections the server tries
4610 * to make on it are rejected.
4611 */
4612 } else {
4613 pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST);
4614 ssh2_pkt_addstring(pktout, "cancel-tcpip-forward");
4615 ssh2_pkt_addbool(pktout, 0);/* _don't_ want reply */
4616 if (epf->saddr) {
4617 ssh2_pkt_addstring(pktout, epf->saddr);
4a693cfc 4618 } else if (conf_get_int(conf, CONF_rport_acceptall)) {
4619 /* XXX: rport_acceptall may not represent
5188540b 4620 * what was used to open the original connection,
4621 * since it's reconfigurable. */
84328ddb 4622 ssh2_pkt_addstring(pktout, "0.0.0.0");
4623 } else {
4624 ssh2_pkt_addstring(pktout, "127.0.0.1");
4625 }
4626 ssh2_pkt_adduint32(pktout, epf->sport);
4627 ssh2_pkt_send(ssh, pktout);
4628 }
4629
4630 del234(ssh->rportfwds, rpf);
4631 free_rportfwd(rpf);
4632 } else if (epf->local) {
4633 pfd_terminate(epf->local);
4634 }
4635
4636 delpos234(ssh->portfwds, i);
4637 free_portfwd(epf);
4638 i--; /* so we don't skip one in the list */
4639 }
4640
4641 /*
4642 * And finally, set up any new port forwardings (status==CREATE).
4643 */
4644 for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++)
4645 if (epf->status == CREATE) {
4646 char *sportdesc, *dportdesc;
3fe92132 4647 sportdesc = dupprintf("%s%s%s%s%d%s",
84328ddb 4648 epf->saddr ? epf->saddr : "",
4649 epf->saddr ? ":" : "",
3fe92132 4650 epf->sserv ? epf->sserv : "",
4651 epf->sserv ? "(" : "",
4652 epf->sport,
4653 epf->sserv ? ")" : "");
84328ddb 4654 if (epf->type == 'D') {
4655 dportdesc = NULL;
4656 } else {
3fe92132 4657 dportdesc = dupprintf("%s:%s%s%d%s",
4658 epf->daddr,
4659 epf->dserv ? epf->dserv : "",
4660 epf->dserv ? "(" : "",
4661 epf->dport,
4662 epf->dserv ? ")" : "");
84328ddb 4663 }
05581745 4664
84328ddb 4665 if (epf->type == 'L') {
4666 const char *err = pfd_addforward(epf->daddr, epf->dport,
4667 epf->saddr, epf->sport,
4a693cfc 4668 ssh, conf,
84328ddb 4669 &epf->local,
4670 epf->addressfamily);
4671
4672 logeventf(ssh, "Local %sport %s forwarding to %s%s%s",
4673 epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " :
4674 epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "",
4675 sportdesc, dportdesc,
4676 err ? " failed: " : "", err ? err : "");
4677 } else if (epf->type == 'D') {
06fadff5 4678 const char *err = pfd_addforward(NULL, -1,
84328ddb 4679 epf->saddr, epf->sport,
4a693cfc 4680 ssh, conf,
84328ddb 4681 &epf->local,
4682 epf->addressfamily);
4683
4684 logeventf(ssh, "Local %sport %s SOCKS dynamic forwarding%s%s",
4685 epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " :
4686 epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "",
4687 sportdesc,
4688 err ? " failed: " : "", err ? err : "");
06fadff5 4689 } else {
4690 struct ssh_rportfwd *pf;
4691
4692 /*
4693 * Ensure the remote port forwardings tree exists.
4694 */
4695 if (!ssh->rportfwds) {
4696 if (ssh->version == 1)
4697 ssh->rportfwds = newtree234(ssh_rportcmp_ssh1);
4698 else
4699 ssh->rportfwds = newtree234(ssh_rportcmp_ssh2);
4700 }
4701
4702 pf = snew(struct ssh_rportfwd);
84328ddb 4703 strncpy(pf->dhost, epf->daddr, lenof(pf->dhost)-1);
4704 pf->dhost[lenof(pf->dhost)-1] = '\0';
4705 pf->dport = epf->dport;
4706 pf->sport = epf->sport;
06fadff5 4707 if (add234(ssh->rportfwds, pf) != pf) {
4708 logeventf(ssh, "Duplicate remote port forwarding to %s:%d",
84328ddb 4709 epf->daddr, epf->dport);
06fadff5 4710 sfree(pf);
4711 } else {
4712 logeventf(ssh, "Requesting remote port %s"
84328ddb 4713 " forward to %s", sportdesc, dportdesc);
06fadff5 4714
4715 pf->sportdesc = sportdesc;
4716 sportdesc = NULL;
84328ddb 4717 epf->remote = pf;
4718 pf->pfrec = epf;
06fadff5 4719
4720 if (ssh->version == 1) {
4721 send_packet(ssh, SSH1_CMSG_PORT_FORWARD_REQUEST,
84328ddb 4722 PKT_INT, epf->sport,
4723 PKT_STR, epf->daddr,
4724 PKT_INT, epf->dport,
06fadff5 4725 PKT_END);
4726 ssh_queue_handler(ssh, SSH1_SMSG_SUCCESS,
4727 SSH1_SMSG_FAILURE,
4728 ssh_rportfwd_succfail, pf);
4729 } else {
4730 struct Packet *pktout;
4731 pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST);
4732 ssh2_pkt_addstring(pktout, "tcpip-forward");
4733 ssh2_pkt_addbool(pktout, 1);/* want reply */
84328ddb 4734 if (epf->saddr) {
4735 ssh2_pkt_addstring(pktout, epf->saddr);
4a693cfc 4736 } else if (conf_get_int(conf, CONF_rport_acceptall)) {
06fadff5 4737 ssh2_pkt_addstring(pktout, "0.0.0.0");
4738 } else {
4739 ssh2_pkt_addstring(pktout, "127.0.0.1");
4740 }
84328ddb 4741 ssh2_pkt_adduint32(pktout, epf->sport);
06fadff5 4742 ssh2_pkt_send(ssh, pktout);
4743
4744 ssh_queue_handler(ssh, SSH2_MSG_REQUEST_SUCCESS,
4745 SSH2_MSG_REQUEST_FAILURE,
4746 ssh_rportfwd_succfail, pf);
4747 }
4748 }
4749 }
4750 sfree(sportdesc);
84328ddb 4751 sfree(dportdesc);
06fadff5 4752 }
06fadff5 4753}
4754
51df0ab5 4755static void ssh1_smsg_stdout_stderr_data(Ssh ssh, struct Packet *pktin)
4756{
4757 char *string;
4758 int stringlen, bufsize;
4759
4760 ssh_pkt_getstring(pktin, &string, &stringlen);
4761 if (string == NULL) {
4762 bombout(("Incoming terminal data packet was badly formed"));
4763 return;
4764 }
4765
4766 bufsize = from_backend(ssh->frontend, pktin->type == SSH1_SMSG_STDERR_DATA,
4767 string, stringlen);
4768 if (!ssh->v1_stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) {
4769 ssh->v1_stdout_throttling = 1;
ff68564b 4770 ssh_throttle_conn(ssh, +1);
51df0ab5 4771 }
4772}
4773
4774static void ssh1_smsg_x11_open(Ssh ssh, struct Packet *pktin)
4775{
4776 /* Remote side is trying to open a channel to talk to our
4777 * X-Server. Give them back a local channel number. */
4778 struct ssh_channel *c;
4779 int remoteid = ssh_pkt_getuint32(pktin);
4780
4781 logevent("Received X11 connect request");
4782 /* Refuse if X11 forwarding is disabled. */
4783 if (!ssh->X11_fwd_enabled) {
4784 send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,
4785 PKT_INT, remoteid, PKT_END);
4786 logevent("Rejected X11 connect request");
4787 } else {
4788 c = snew(struct ssh_channel);
4789 c->ssh = ssh;
4790
8def70c3 4791 if (x11_init(&c->u.x11.s, ssh->x11disp, c,
4a693cfc 4792 NULL, -1, ssh->conf) != NULL) {
51df0ab5 4793 logevent("Opening X11 forward connection failed");
4794 sfree(c);
4795 send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,
4796 PKT_INT, remoteid, PKT_END);
4797 } else {
4798 logevent
4799 ("Opening X11 forward connection succeeded");
4800 c->remoteid = remoteid;
64d6ff88 4801 c->halfopen = FALSE;
51df0ab5 4802 c->localid = alloc_channel_id(ssh);
4803 c->closes = 0;
bc06669b 4804 c->pending_eof = FALSE;
ff68564b 4805 c->throttling_conn = 0;
51df0ab5 4806 c->type = CHAN_X11; /* identify channel type */
4807 add234(ssh->channels, c);
4808 send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
4809 PKT_INT, c->remoteid, PKT_INT,
4810 c->localid, PKT_END);
4811 logevent("Opened X11 forward channel");
4812 }
4813 }
4814}
4815
4816static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
4817{
4818 /* Remote side is trying to open a channel to talk to our
4819 * agent. Give them back a local channel number. */
4820 struct ssh_channel *c;
4821 int remoteid = ssh_pkt_getuint32(pktin);
4822
4823 /* Refuse if agent forwarding is disabled. */
4824 if (!ssh->agentfwd_enabled) {
4825 send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,
4826 PKT_INT, remoteid, PKT_END);
4827 } else {
4828 c = snew(struct ssh_channel);
4829 c->ssh = ssh;
4830 c->remoteid = remoteid;
64d6ff88 4831 c->halfopen = FALSE;
51df0ab5 4832 c->localid = alloc_channel_id(ssh);
4833 c->closes = 0;
bc06669b 4834 c->pending_eof = FALSE;
ff68564b 4835 c->throttling_conn = 0;
51df0ab5 4836 c->type = CHAN_AGENT; /* identify channel type */
4837 c->u.a.lensofar = 0;
bc06669b 4838 c->u.a.message = NULL;
51df0ab5 4839 add234(ssh->channels, c);
4840 send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
4841 PKT_INT, c->remoteid, PKT_INT, c->localid,
4842 PKT_END);
4843 }
4844}
4845
4846static void ssh1_msg_port_open(Ssh ssh, struct Packet *pktin)
4847{
4848 /* Remote side is trying to open a channel to talk to a
4849 * forwarded port. Give them back a local channel number. */
4850 struct ssh_channel *c;
05581745 4851 struct ssh_rportfwd pf, *pfp;
51df0ab5 4852 int remoteid;
4853 int hostsize, port;
fb983202 4854 char *host;
51df0ab5 4855 const char *e;
4856 c = snew(struct ssh_channel);
4857 c->ssh = ssh;
4858
4859 remoteid = ssh_pkt_getuint32(pktin);
4860 ssh_pkt_getstring(pktin, &host, &hostsize);
4861 port = ssh_pkt_getuint32(pktin);
4862
4863 if (hostsize >= lenof(pf.dhost))
4864 hostsize = lenof(pf.dhost)-1;
4865 memcpy(pf.dhost, host, hostsize);
4866 pf.dhost[hostsize] = '\0';
4867 pf.dport = port;
05581745 4868 pfp = find234(ssh->rportfwds, &pf, NULL);
51df0ab5 4869
05581745 4870 if (pfp == NULL) {
fb983202 4871 logeventf(ssh, "Rejected remote port open request for %s:%d",
4872 pf.dhost, port);
51df0ab5 4873 send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,
4874 PKT_INT, remoteid, PKT_END);
4875 } else {
fb983202 4876 logeventf(ssh, "Received remote port open request for %s:%d",
4877 pf.dhost, port);
51df0ab5 4878 e = pfd_newconnect(&c->u.pfd.s, pf.dhost, port,
4a693cfc 4879 c, ssh->conf, pfp->pfrec->addressfamily);
51df0ab5 4880 if (e != NULL) {
fb983202 4881 logeventf(ssh, "Port open failed: %s", e);
51df0ab5 4882 sfree(c);
4883 send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,
4884 PKT_INT, remoteid, PKT_END);
4885 } else {
4886 c->remoteid = remoteid;
64d6ff88 4887 c->halfopen = FALSE;
51df0ab5 4888 c->localid = alloc_channel_id(ssh);
4889 c->closes = 0;
bc06669b 4890 c->pending_eof = FALSE;
ff68564b 4891 c->throttling_conn = 0;
51df0ab5 4892 c->type = CHAN_SOCKDATA; /* identify channel type */
4893 add234(ssh->channels, c);
4894 send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
4895 PKT_INT, c->remoteid, PKT_INT,
4896 c->localid, PKT_END);
4897 logevent("Forwarded port opened successfully");
4898 }
4899 }
4900}
4901
4902static void ssh1_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin)
4903{
4904 unsigned int remoteid = ssh_pkt_getuint32(pktin);
4905 unsigned int localid = ssh_pkt_getuint32(pktin);
4906 struct ssh_channel *c;
4907
4908 c = find234(ssh->channels, &remoteid, ssh_channelfind);
4909 if (c && c->type == CHAN_SOCKDATA_DORMANT) {
4910 c->remoteid = localid;
64d6ff88 4911 c->halfopen = FALSE;
51df0ab5 4912 c->type = CHAN_SOCKDATA;
ff68564b 4913 c->throttling_conn = 0;
51df0ab5 4914 pfd_confirm(c->u.pfd.s);
4915 }
4916
bc06669b 4917 if (c && c->pending_eof) {
51df0ab5 4918 /*
4919 * We have a pending close on this channel,
4920 * which we decided on before the server acked
4921 * the channel open. So now we know the
4922 * remoteid, we can close it again.
4923 */
bc06669b 4924 ssh_channel_try_eof(c);
51df0ab5 4925 }
4926}
4927
4928static void ssh1_msg_channel_open_failure(Ssh ssh, struct Packet *pktin)
4929{
4930 unsigned int remoteid = ssh_pkt_getuint32(pktin);
4931 struct ssh_channel *c;
4932
4933 c = find234(ssh->channels, &remoteid, ssh_channelfind);
4934 if (c && c->type == CHAN_SOCKDATA_DORMANT) {
4935 logevent("Forwarded connection refused by server");
4936 pfd_close(c->u.pfd.s);
4937 del234(ssh->channels, c);
4938 sfree(c);
4939 }
4940}
4941
4942static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)
4943{
4944 /* Remote side closes a channel. */
4945 unsigned i = ssh_pkt_getuint32(pktin);
4946 struct ssh_channel *c;
4947 c = find234(ssh->channels, &i, ssh_channelfind);
64d6ff88 4948 if (c && !c->halfopen) {
51df0ab5 4949
bc06669b 4950 if (pktin->type == SSH1_MSG_CHANNEL_CLOSE &&
4951 !(c->closes & CLOSES_RCVD_EOF)) {
4952 /*
4953 * Received CHANNEL_CLOSE, which we translate into
4954 * outgoing EOF.
4955 */
4956 int send_close = FALSE;
4957
4958 c->closes |= CLOSES_RCVD_EOF;
4959
4960 switch (c->type) {
4961 case CHAN_X11:
4962 if (c->u.x11.s)
4963 x11_send_eof(c->u.x11.s);
4964 else
4965 send_close = TRUE;
9fb91415 4966 break;
bc06669b 4967 case CHAN_SOCKDATA:
4968 if (c->u.pfd.s)
9fb91415 4969 pfd_send_eof(c->u.pfd.s);
bc06669b 4970 else
4971 send_close = TRUE;
9fb91415 4972 break;
bc06669b 4973 case CHAN_AGENT:
4974 send_close = TRUE;
9fb91415 4975 break;
bc06669b 4976 }
51df0ab5 4977
bc06669b 4978 if (send_close && !(c->closes & CLOSES_SENT_EOF)) {
4979 send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid,
4980 PKT_END);
4981 c->closes |= CLOSES_SENT_EOF;
4982 }
4983 }
4984
4985 if (pktin->type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION &&
4986 !(c->closes & CLOSES_RCVD_CLOSE)) {
4987
4988 if (!(c->closes & CLOSES_SENT_EOF)) {
4989 bombout(("Received CHANNEL_CLOSE_CONFIRMATION for channel %d"
4990 " for which we never sent CHANNEL_CLOSE\n", i));
4991 }
4992
4993 c->closes |= CLOSES_RCVD_CLOSE;
4994 }
4995
4996 if (!((CLOSES_SENT_EOF | CLOSES_RCVD_EOF) & ~c->closes) &&
4997 !(c->closes & CLOSES_SENT_CLOSE)) {
4998 send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION,
4999 PKT_INT, c->remoteid, PKT_END);
5000 c->closes |= CLOSES_SENT_CLOSE;
5001 }
5002
5003 if (!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes))
5004 ssh_channel_destroy(c);
51df0ab5 5005 } else {
5006 bombout(("Received CHANNEL_CLOSE%s for %s channel %d\n",
5007 pktin->type == SSH1_MSG_CHANNEL_CLOSE ? "" :
5008 "_CONFIRMATION", c ? "half-open" : "nonexistent",
5009 i));
5010 }
5011}
5012
5013static void ssh1_msg_channel_data(Ssh ssh, struct Packet *pktin)
5014{
5015 /* Data sent down one of our channels. */
5016 int i = ssh_pkt_getuint32(pktin);
5017 char *p;
6d44acc9 5018 int len;
51df0ab5 5019 struct ssh_channel *c;
5020
5021 ssh_pkt_getstring(pktin, &p, &len);
5022
5023 c = find234(ssh->channels, &i, ssh_channelfind);
5024 if (c) {
5025 int bufsize = 0;
5026 switch (c->type) {
5027 case CHAN_X11:
5028 bufsize = x11_send(c->u.x11.s, p, len);
5029 break;
5030 case CHAN_SOCKDATA:
5031 bufsize = pfd_send(c->u.pfd.s, p, len);
5032 break;
5033 case CHAN_AGENT:
5034 /* Data for an agent message. Buffer it. */
5035 while (len > 0) {
5036 if (c->u.a.lensofar < 4) {
62ddb51e 5037 unsigned int l = min(4 - c->u.a.lensofar, (unsigned)len);
51df0ab5 5038 memcpy(c->u.a.msglen + c->u.a.lensofar, p,
5039 l);
5040 p += l;
5041 len -= l;
5042 c->u.a.lensofar += l;
5043 }
5044 if (c->u.a.lensofar == 4) {
5045 c->u.a.totallen =
5046 4 + GET_32BIT(c->u.a.msglen);
5047 c->u.a.message = snewn(c->u.a.totallen,
5048 unsigned char);
5049 memcpy(c->u.a.message, c->u.a.msglen, 4);
5050 }
5051 if (c->u.a.lensofar >= 4 && len > 0) {
aa63ab7e 5052 unsigned int l =
51df0ab5 5053 min(c->u.a.totallen - c->u.a.lensofar,
62ddb51e 5054 (unsigned)len);
51df0ab5 5055 memcpy(c->u.a.message + c->u.a.lensofar, p,
5056 l);
5057 p += l;
5058 len -= l;
5059 c->u.a.lensofar += l;
5060 }
5061 if (c->u.a.lensofar == c->u.a.totallen) {
5062 void *reply;
5063 int replylen;
5064 if (agent_query(c->u.a.message,
5065 c->u.a.totallen,
5066 &reply, &replylen,
5067 ssh_agentf_callback, c))
5068 ssh_agentf_callback(c, reply, replylen);
5069 sfree(c->u.a.message);
5070 c->u.a.lensofar = 0;
5071 }
5072 }
5073 bufsize = 0; /* agent channels never back up */
5074 break;
5075 }
ff68564b 5076 if (!c->throttling_conn && bufsize > SSH1_BUFFER_LIMIT) {
5077 c->throttling_conn = 1;
5078 ssh_throttle_conn(ssh, +1);
51df0ab5 5079 }
5080 }
5081}
5082
5083static void ssh1_smsg_exit_status(Ssh ssh, struct Packet *pktin)
5084{
51df0ab5 5085 ssh->exitcode = ssh_pkt_getuint32(pktin);
fb983202 5086 logeventf(ssh, "Server sent command exit status %d", ssh->exitcode);
51df0ab5 5087 send_packet(ssh, SSH1_CMSG_EXIT_CONFIRMATION, PKT_END);
5088 /*
5089 * In case `helpful' firewalls or proxies tack
5090 * extra human-readable text on the end of the
5091 * session which we might mistake for another
5092 * encrypted packet, we close the session once
5093 * we've sent EXIT_CONFIRMATION.
5094 */
9e296bfa 5095 ssh_disconnect(ssh, NULL, NULL, 0, TRUE);
51df0ab5 5096}
5097
c6ccd5c2 5098/* Helper function to deal with sending tty modes for REQUEST_PTY */
5099static void ssh1_send_ttymode(void *data, char *mode, char *val)
5100{
5101 struct Packet *pktout = (struct Packet *)data;
5102 int i = 0;
5103 unsigned int arg = 0;
5104 while (strcmp(mode, ssh_ttymodes[i].mode) != 0) i++;
5105 if (i == lenof(ssh_ttymodes)) return;
5106 switch (ssh_ttymodes[i].type) {
5107 case TTY_OP_CHAR:
5108 arg = ssh_tty_parse_specchar(val);
5109 break;
5110 case TTY_OP_BOOL:
5111 arg = ssh_tty_parse_boolean(val);
5112 break;
5113 }
5114 ssh2_pkt_addbyte(pktout, ssh_ttymodes[i].opcode);
5115 ssh2_pkt_addbyte(pktout, arg);
5116}
5117
5118
b09eaa88 5119static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen,
5120 struct Packet *pktin)
32874aea 5121{
b09eaa88 5122 crBegin(ssh->do_ssh1_connection_crstate);
fb09bf1c 5123
51df0ab5 5124 ssh->packet_dispatch[SSH1_SMSG_STDOUT_DATA] =
5125 ssh->packet_dispatch[SSH1_SMSG_STDERR_DATA] =
5126 ssh1_smsg_stdout_stderr_data;
5127
5128 ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_CONFIRMATION] =
5129 ssh1_msg_channel_open_confirmation;
5130 ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_FAILURE] =
5131 ssh1_msg_channel_open_failure;
5132 ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE] =
5133 ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION] =
5134 ssh1_msg_channel_close;
5135 ssh->packet_dispatch[SSH1_MSG_CHANNEL_DATA] = ssh1_msg_channel_data;
5136 ssh->packet_dispatch[SSH1_SMSG_EXIT_STATUS] = ssh1_smsg_exit_status;
5137
4a693cfc 5138 if (conf_get_int(ssh->conf, CONF_agentfwd) && agent_exists()) {
32874aea 5139 logevent("Requesting agent forwarding");
51470298 5140 send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END);
32874aea 5141 do {
5142 crReturnV;
ff3187f6 5143 } while (!pktin);
5144 if (pktin->type != SSH1_SMSG_SUCCESS
5145 && pktin->type != SSH1_SMSG_FAILURE) {
6b5cf8b4 5146 bombout(("Protocol confusion"));
7ffdbc1a 5147 crStopV;
ff3187f6 5148 } else if (pktin->type == SSH1_SMSG_FAILURE) {
32874aea 5149 logevent("Agent forwarding refused");
5150 } else {
5151 logevent("Agent forwarding enabled");
51470298 5152 ssh->agentfwd_enabled = TRUE;
51df0ab5 5153 ssh->packet_dispatch[SSH1_SMSG_AGENT_OPEN] = ssh1_smsg_agent_open;
db7d555c 5154 }
dacbd0e8 5155 }
5156
4a693cfc 5157 if (conf_get_int(ssh->conf, CONF_x11_forward) &&
5158 (ssh->x11disp = x11_setup_display(conf_get_str(ssh->conf, CONF_x11_display),
5159 conf_get_int(ssh->conf, CONF_x11_auth), ssh->conf))) {
32874aea 5160 logevent("Requesting X11 forwarding");
f68353be 5161 /*
5162 * Note that while we blank the X authentication data here, we don't
5163 * take any special action to blank the start of an X11 channel,
5164 * so using MIT-MAGIC-COOKIE-1 and actually opening an X connection
5165 * without having session blanking enabled is likely to leak your
5166 * cookie into the log.
5167 */
51470298 5168 if (ssh->v1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) {
5169 send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING,
8def70c3 5170 PKT_STR, ssh->x11disp->remoteauthprotoname,
5171 PKTT_PASSWORD,
5172 PKT_STR, ssh->x11disp->remoteauthdatastring,
5173 PKTT_OTHER,
5174 PKT_INT, ssh->x11disp->screennum,
421d6835 5175 PKT_END);
32874aea 5176 } else {
51470298 5177 send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING,
8def70c3 5178 PKT_STR, ssh->x11disp->remoteauthprotoname,
5179 PKTT_PASSWORD,
5180 PKT_STR, ssh->x11disp->remoteauthdatastring,
5181 PKTT_OTHER,
5182 PKT_END);
32874aea 5183 }
5184 do {
5185 crReturnV;
ff3187f6 5186 } while (!pktin);
5187 if (pktin->type != SSH1_SMSG_SUCCESS
5188 && pktin->type != SSH1_SMSG_FAILURE) {
6b5cf8b4 5189 bombout(("Protocol confusion"));
7ffdbc1a 5190 crStopV;
ff3187f6 5191 } else if (pktin->type == SSH1_SMSG_FAILURE) {
32874aea 5192 logevent("X11 forwarding refused");
5193 } else {
5194 logevent("X11 forwarding enabled");
51470298 5195 ssh->X11_fwd_enabled = TRUE;
51df0ab5 5196 ssh->packet_dispatch[SSH1_SMSG_X11_OPEN] = ssh1_smsg_x11_open;
9c964e85 5197 }
5198 }
5199
4a693cfc 5200 ssh_setup_portfwd(ssh, ssh->conf);
06fadff5 5201 ssh->packet_dispatch[SSH1_MSG_PORT_OPEN] = ssh1_msg_port_open;
d74d141c 5202
4a693cfc 5203 if (!conf_get_int(ssh->conf, CONF_nopty)) {
c6ccd5c2 5204 struct Packet *pkt;
a5dd8467 5205 /* Unpick the terminal-speed string. */
5206 /* XXX perhaps we should allow no speeds to be sent. */
db219738 5207 ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */
4a693cfc 5208 sscanf(conf_get_str(ssh->conf, CONF_termspeed), "%d,%d", &ssh->ospeed, &ssh->ispeed);
a5dd8467 5209 /* Send the pty request. */
c6ccd5c2 5210 pkt = ssh1_pkt_init(SSH1_CMSG_REQUEST_PTY);
4a693cfc 5211 ssh_pkt_addstring(pkt, conf_get_str(ssh->conf, CONF_termtype));
c6ccd5c2 5212 ssh_pkt_adduint32(pkt, ssh->term_height);
5213 ssh_pkt_adduint32(pkt, ssh->term_width);
5214 ssh_pkt_adduint32(pkt, 0); /* width in pixels */
5215 ssh_pkt_adduint32(pkt, 0); /* height in pixels */
4a693cfc 5216 parse_ttymodes(ssh, ssh1_send_ttymode, (void *)pkt);
c6ccd5c2 5217 ssh_pkt_addbyte(pkt, SSH1_TTY_OP_ISPEED);
5218 ssh_pkt_adduint32(pkt, ssh->ispeed);
5219 ssh_pkt_addbyte(pkt, SSH1_TTY_OP_OSPEED);
5220 ssh_pkt_adduint32(pkt, ssh->ospeed);
5221 ssh_pkt_addbyte(pkt, SSH_TTY_OP_END);
5222 s_wrpkt(ssh, pkt);
51470298 5223 ssh->state = SSH_STATE_INTERMED;
32874aea 5224 do {
5225 crReturnV;
ff3187f6 5226 } while (!pktin);
5227 if (pktin->type != SSH1_SMSG_SUCCESS
5228 && pktin->type != SSH1_SMSG_FAILURE) {
6b5cf8b4 5229 bombout(("Protocol confusion"));
7ffdbc1a 5230 crStopV;
ff3187f6 5231 } else if (pktin->type == SSH1_SMSG_FAILURE) {
51470298 5232 c_write_str(ssh, "Server refused to allocate pty\r\n");
5233 ssh->editing = ssh->echoing = 1;
4a202fc6 5234 } else {
5235 logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)",
5236 ssh->ospeed, ssh->ispeed);
5237 ssh->got_pty = TRUE;
5238 }
0965bee0 5239 } else {
51470298 5240 ssh->editing = ssh->echoing = 1;
374330e2 5241 }
5242
4a693cfc 5243 if (conf_get_int(ssh->conf, CONF_compression)) {
51470298 5244 send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END);
32874aea 5245 do {
5246 crReturnV;
ff3187f6 5247 } while (!pktin);
5248 if (pktin->type != SSH1_SMSG_SUCCESS
5249 && pktin->type != SSH1_SMSG_FAILURE) {
6b5cf8b4 5250 bombout(("Protocol confusion"));
7ffdbc1a 5251 crStopV;
ff3187f6 5252 } else if (pktin->type == SSH1_SMSG_FAILURE) {
51470298 5253 c_write_str(ssh, "Server refused to compress\r\n");
32874aea 5254 }
4ba9b64b 5255 logevent("Started compression");
51470298 5256 ssh->v1_compressing = TRUE;
5366aed8 5257 ssh->cs_comp_ctx = zlib_compress_init();
5258 logevent("Initialised zlib (RFC1950) compression");
5259 ssh->sc_comp_ctx = zlib_decompress_init();
5260 logevent("Initialised zlib (RFC1950) decompression");
4ba9b64b 5261 }
5262
fd5e5847 5263 /*
5264 * Start the shell or command.
5265 *
2e85c969 5266 * Special case: if the first-choice command is an SSH-2
fd5e5847 5267 * subsystem (hence not usable here) and the second choice
5268 * exists, we fall straight back to that.
5269 */
5270 {
4a693cfc 5271 char *cmd = conf_get_str(ssh->conf, CONF_remote_cmd);
fd5e5847 5272
4a693cfc 5273 if (conf_get_int(ssh->conf, CONF_ssh_subsys) &&
5274 conf_get_str(ssh->conf, CONF_remote_cmd2)) {
5275 cmd = conf_get_str(ssh->conf, CONF_remote_cmd2);
51470298 5276 ssh->fallback_cmd = TRUE;
fd5e5847 5277 }
5278 if (*cmd)
51470298 5279 send_packet(ssh, SSH1_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END);
fd5e5847 5280 else
51470298 5281 send_packet(ssh, SSH1_CMSG_EXEC_SHELL, PKT_END);
fd5e5847 5282 logevent("Started session");
5283 }
374330e2 5284
51470298 5285 ssh->state = SSH_STATE_SESSION;
5286 if (ssh->size_needed)
5287 ssh_size(ssh, ssh->term_width, ssh->term_height);
5288 if (ssh->eof_needed)
5289 ssh_special(ssh, TS_EOF);
374330e2 5290
b9d7bcad 5291 if (ssh->ldisc)
5292 ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
51470298 5293 ssh->send_ok = 1;
5294 ssh->channels = newtree234(ssh_channelcmp);
374330e2 5295 while (1) {
d74d141c 5296
51df0ab5 5297 /*
5298 * By this point, most incoming packets are already being
5299 * handled by the dispatch table, and we need only pay
5300 * attention to the unusual ones.
5301 */
0357890f 5302
51df0ab5 5303 crReturnV;
5304 if (pktin) {
5305 if (pktin->type == SSH1_SMSG_SUCCESS) {
972a41c8 5306 /* may be from EXEC_SHELL on some servers */
ff3187f6 5307 } else if (pktin->type == SSH1_SMSG_FAILURE) {
972a41c8 5308 /* may be from EXEC_SHELL on some servers
374330e2 5309 * if no pty is available or in other odd cases. Ignore */
374330e2 5310 } else {
ff3187f6 5311 bombout(("Strange packet received: type %d", pktin->type));
7ffdbc1a 5312 crStopV;
374330e2 5313 }
5314 } else {
8df7a775 5315 while (inlen > 0) {
5316 int len = min(inlen, 512);
a2724fba 5317 send_packet(ssh, SSH1_CMSG_STDIN_DATA,
5318 PKT_INT, len, PKTT_DATA, PKT_DATA, in, len,
9a10ecf4 5319 PKTT_OTHER, PKT_END);
8df7a775 5320 in += len;
5321 inlen -= len;
5322 }
374330e2 5323 }
5324 }
5325
5326 crFinishV;
5327}
5328
5329/*
2e85c969 5330 * Handle the top-level SSH-2 protocol.
b09eaa88 5331 */
5332static void ssh1_msg_debug(Ssh ssh, struct Packet *pktin)
5333{
fb983202 5334 char *msg;
b09eaa88 5335 int msglen;
5336
5337 ssh_pkt_getstring(pktin, &msg, &msglen);
fb983202 5338 logeventf(ssh, "Remote debug message: %.*s", msglen, msg);
b09eaa88 5339}
5340
5341static void ssh1_msg_disconnect(Ssh ssh, struct Packet *pktin)
5342{
5343 /* log reason code in disconnect message */
5344 char *msg;
5345 int msglen;
5346
5347 ssh_pkt_getstring(pktin, &msg, &msglen);
5348 bombout(("Server sent disconnect message:\n\"%.*s\"", msglen, msg));
5349}
5350
409bfa77 5351static void ssh_msg_ignore(Ssh ssh, struct Packet *pktin)
b09eaa88 5352{
5353 /* Do nothing, because we're ignoring it! Duhh. */
5354}
5355
5356static void ssh1_protocol_setup(Ssh ssh)
5357{
5358 int i;
5359
5360 /*
5361 * Most messages are handled by the coroutines.
5362 */
5363 for (i = 0; i < 256; i++)
5364 ssh->packet_dispatch[i] = NULL;
5365
5366 /*
5367 * These special message types we install handlers for.
5368 */
5369 ssh->packet_dispatch[SSH1_MSG_DISCONNECT] = ssh1_msg_disconnect;
5370 ssh->packet_dispatch[SSH1_MSG_IGNORE] = ssh_msg_ignore;
5371 ssh->packet_dispatch[SSH1_MSG_DEBUG] = ssh1_msg_debug;
5372}
5373
1c1a7262 5374static void ssh1_protocol(Ssh ssh, void *vin, int inlen,
b09eaa88 5375 struct Packet *pktin)
5376{
1c1a7262 5377 unsigned char *in=(unsigned char*)vin;
b09eaa88 5378 if (ssh->state == SSH_STATE_CLOSED)
5379 return;
5380
5381 if (pktin && ssh->packet_dispatch[pktin->type]) {
5382 ssh->packet_dispatch[pktin->type](ssh, pktin);
5383 return;
5384 }
5385
5386 if (!ssh->protocol_initial_phase_done) {
5387 if (do_ssh1_login(ssh, in, inlen, pktin))
5388 ssh->protocol_initial_phase_done = TRUE;
5389 else
5390 return;
5391 }
5392
5393 do_ssh1_connection(ssh, in, inlen, pktin);
5394}
5395
5396/*
e5574168 5397 * Utility routine for decoding comma-separated strings in KEXINIT.
5398 */
32874aea 5399static int in_commasep_string(char *needle, char *haystack, int haylen)
5400{
57356d63 5401 int needlen;
5402 if (!needle || !haystack) /* protect against null pointers */
5403 return 0;
5404 needlen = strlen(needle);
e5574168 5405 while (1) {
32874aea 5406 /*
5407 * Is it at the start of the string?
5408 */
5409 if (haylen >= needlen && /* haystack is long enough */
5410 !memcmp(needle, haystack, needlen) && /* initial match */
5411 (haylen == needlen || haystack[needlen] == ',')
5412 /* either , or EOS follows */
5413 )
5414 return 1;
5415 /*
5416 * If not, search for the next comma and resume after that.
5417 * If no comma found, terminate.
5418 */
5419 while (haylen > 0 && *haystack != ',')
5420 haylen--, haystack++;
5421 if (haylen == 0)
5422 return 0;
5423 haylen--, haystack++; /* skip over comma itself */
e5574168 5424 }
5425}
5426
5427/*
b59743d5 5428 * Similar routine for checking whether we have the first string in a list.
5429 */
5430static int first_in_commasep_string(char *needle, char *haystack, int haylen)
5431{
5432 int needlen;
5433 if (!needle || !haystack) /* protect against null pointers */
5434 return 0;
5435 needlen = strlen(needle);
5436 /*
5437 * Is it at the start of the string?
5438 */
5439 if (haylen >= needlen && /* haystack is long enough */
5440 !memcmp(needle, haystack, needlen) && /* initial match */
5441 (haylen == needlen || haystack[needlen] == ',')
5442 /* either , or EOS follows */
5443 )
5444 return 1;
5445 return 0;
5446}
5447
5448
5449/*
2e85c969 5450 * SSH-2 key creation method.
754c0df9 5451 * (Currently assumes 2 lots of any hash are sufficient to generate
5452 * keys/IVs for any cipher/MAC. SSH2_MKKEY_ITERS documents this assumption.)
d39f364a 5453 */
754c0df9 5454#define SSH2_MKKEY_ITERS (2)
b672f405 5455static void ssh2_mkkey(Ssh ssh, Bignum K, unsigned char *H, char chr,
d8baa528 5456 unsigned char *keyspace)
32874aea 5457{
b672f405 5458 const struct ssh_hash *h = ssh->kex->hash;
5459 void *s;
5460 /* First hlen bytes. */
5461 s = h->init();
51470298 5462 if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY))
b672f405 5463 hash_mpint(h, s, K);
5464 h->bytes(s, H, h->hlen);
5465 h->bytes(s, &chr, 1);
5466 h->bytes(s, ssh->v2_session_id, ssh->v2_session_id_len);
5467 h->final(s, keyspace);
5468 /* Next hlen bytes. */
5469 s = h->init();
51470298 5470 if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY))
b672f405 5471 hash_mpint(h, s, K);
5472 h->bytes(s, H, h->hlen);
5473 h->bytes(s, keyspace, h->hlen);
5474 h->final(s, keyspace + h->hlen);
d39f364a 5475}
5476
5477/*
2e85c969 5478 * Handle the SSH-2 transport layer.
e5574168 5479 */
1c1a7262 5480static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
ff3187f6 5481 struct Packet *pktin)
e5574168 5482{
1c1a7262 5483 unsigned char *in = (unsigned char *)vin;
51470298 5484 struct do_ssh2_transport_state {
30919997 5485 int crLine;
4763c1c2 5486 int nbits, pbits, warn_kex, warn_cscipher, warn_sccipher;
51470298 5487 Bignum p, g, e, f, K;
3dc9a6a7 5488 void *our_kexinit;
5489 int our_kexinitlen;
51470298 5490 int kex_init_value, kex_reply_value;
5491 const struct ssh_mac **maclist;
5492 int nmacs;
5493 const struct ssh2_cipher *cscipher_tobe;
5494 const struct ssh2_cipher *sccipher_tobe;
5495 const struct ssh_mac *csmac_tobe;
5496 const struct ssh_mac *scmac_tobe;
5497 const struct ssh_compress *cscomp_tobe;
5498 const struct ssh_compress *sccomp_tobe;
fae1a71b 5499 char *hostkeydata, *sigdata, *rsakeydata, *keystr, *fingerprint;
5500 int hostkeylen, siglen, rsakeylen;
51470298 5501 void *hkey; /* actual host key */
fae1a71b 5502 void *rsakey; /* for RSA kex */
754c0df9 5503 unsigned char exchange_hash[SSH2_KEX_MAX_HASH_LEN];
83e7d008 5504 int n_preferred_kex;
34557659 5505 const struct ssh_kexes *preferred_kex[KEX_MAX];
51470298 5506 int n_preferred_ciphers;
5507 const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX];
5508 const struct ssh_compress *preferred_comp;
ab18ccff 5509 int userauth_succeeded; /* for delayed compression */
5510 int pending_compression;
e13bba36 5511 int got_session_id, activated_authconn;
ff3187f6 5512 struct Packet *pktout;
3d9449a1 5513 int dlgret;
5514 int guessok;
4763c1c2 5515 int ignorepkt;
51470298 5516 };
5517 crState(do_ssh2_transport_state);
5518
30919997 5519 crBeginState;
51470298 5520
5521 s->cscipher_tobe = s->sccipher_tobe = NULL;
5522 s->csmac_tobe = s->scmac_tobe = NULL;
5523 s->cscomp_tobe = s->sccomp_tobe = NULL;
5524
e13bba36 5525 s->got_session_id = s->activated_authconn = FALSE;
ab18ccff 5526 s->userauth_succeeded = FALSE;
5527 s->pending_compression = FALSE;
e5574168 5528
e13bba36 5529 /*
5530 * Be prepared to work around the buggy MAC problem.
5531 */
5532 if (ssh->remote_bugs & BUG_SSH2_HMAC)
5533 s->maclist = buggymacs, s->nmacs = lenof(buggymacs);
5534 else
5535 s->maclist = macs, s->nmacs = lenof(macs);
5536
5537 begin_key_exchange:
acab36bc 5538 ssh->pkt_kctx = SSH2_PKTCTX_NOKEX;
51470298 5539 {
cc18f256 5540 int i, j, k, commalist_started;
e13bba36 5541
51470298 5542 /*
83e7d008 5543 * Set up the preferred key exchange. (NULL => warn below here)
5544 */
5545 s->n_preferred_kex = 0;
5546 for (i = 0; i < KEX_MAX; i++) {
4a693cfc 5547 switch (conf_get_int_int(ssh->conf, CONF_ssh_kexlist, i)) {
83e7d008 5548 case KEX_DHGEX:
5549 s->preferred_kex[s->n_preferred_kex++] =
5550 &ssh_diffiehellman_gex;
5551 break;
5552 case KEX_DHGROUP14:
5553 s->preferred_kex[s->n_preferred_kex++] =
5554 &ssh_diffiehellman_group14;
5555 break;
5556 case KEX_DHGROUP1:
5557 s->preferred_kex[s->n_preferred_kex++] =
5558 &ssh_diffiehellman_group1;
5559 break;
fae1a71b 5560 case KEX_RSA:
5561 s->preferred_kex[s->n_preferred_kex++] =
5562 &ssh_rsa_kex;
5563 break;
754c0df9 5564 case KEX_WARN:
83e7d008 5565 /* Flag for later. Don't bother if it's the last in
5566 * the list. */
5567 if (i < KEX_MAX - 1) {
5568 s->preferred_kex[s->n_preferred_kex++] = NULL;
5569 }
5570 break;
5571 }
5572 }
83e7d008 5573
83e7d008 5574 /*
51470298 5575 * Set up the preferred ciphers. (NULL => warn below here)
5576 */
5577 s->n_preferred_ciphers = 0;
5578 for (i = 0; i < CIPHER_MAX; i++) {
4a693cfc 5579 switch (conf_get_int_int(ssh->conf, CONF_ssh_cipherlist, i)) {
51470298 5580 case CIPHER_BLOWFISH:
5581 s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_blowfish;
5582 break;
5583 case CIPHER_DES:
4a693cfc 5584 if (conf_get_int(ssh->conf, CONF_ssh2_des_cbc)) {
51470298 5585 s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_des;
5586 }
5587 break;
5588 case CIPHER_3DES:
5589 s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_3des;
5590 break;
5591 case CIPHER_AES:
5592 s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_aes;
5593 break;
a2add208 5594 case CIPHER_ARCFOUR:
5595 s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_arcfour;
5596 break;
51470298 5597 case CIPHER_WARN:
5598 /* Flag for later. Don't bother if it's the last in
5599 * the list. */
5600 if (i < CIPHER_MAX - 1) {
5601 s->preferred_ciphers[s->n_preferred_ciphers++] = NULL;
5602 }
5603 break;
ca20bfcf 5604 }
ca20bfcf 5605 }
7591b9ff 5606
e13bba36 5607 /*
5608 * Set up preferred compression.
5609 */
4a693cfc 5610 if (conf_get_int(ssh->conf, CONF_compression))
e13bba36 5611 s->preferred_comp = &ssh_zlib;
5612 else
5613 s->preferred_comp = &ssh_comp_none;
51470298 5614
5615 /*
590f6a5f 5616 * Enable queueing of outgoing auth- or connection-layer
5617 * packets while we are in the middle of a key exchange.
5618 */
5619 ssh->queueing = TRUE;
5620
5621 /*
9442dd57 5622 * Flag that KEX is in progress.
5623 */
5624 ssh->kex_in_progress = TRUE;
5625
5626 /*
51470298 5627 * Construct and send our key exchange packet.
5628 */
ff3187f6 5629 s->pktout = ssh2_pkt_init(SSH2_MSG_KEXINIT);
51470298 5630 for (i = 0; i < 16; i++)
ff3187f6 5631 ssh2_pkt_addbyte(s->pktout, (unsigned char) random_byte());
51470298 5632 /* List key exchange algorithms. */
ff3187f6 5633 ssh2_pkt_addstring_start(s->pktout);
83e7d008 5634 commalist_started = 0;
5635 for (i = 0; i < s->n_preferred_kex; i++) {
34557659 5636 const struct ssh_kexes *k = s->preferred_kex[i];
83e7d008 5637 if (!k) continue; /* warning flag */
34557659 5638 for (j = 0; j < k->nkexes; j++) {
5639 if (commalist_started)
5640 ssh2_pkt_addstring_str(s->pktout, ",");
5641 ssh2_pkt_addstring_str(s->pktout, k->list[j]->name);
5642 commalist_started = 1;
5643 }
32874aea 5644 }
51470298 5645 /* List server host key algorithms. */
ff3187f6 5646 ssh2_pkt_addstring_start(s->pktout);
51470298 5647 for (i = 0; i < lenof(hostkey_algs); i++) {
ff3187f6 5648 ssh2_pkt_addstring_str(s->pktout, hostkey_algs[i]->name);
51470298 5649 if (i < lenof(hostkey_algs) - 1)
ff3187f6 5650 ssh2_pkt_addstring_str(s->pktout, ",");
32874aea 5651 }
cc18f256 5652 /* List encryption algorithms (client->server then server->client). */
5653 for (k = 0; k < 2; k++) {
5654 ssh2_pkt_addstring_start(s->pktout);
5655 commalist_started = 0;
5656 for (i = 0; i < s->n_preferred_ciphers; i++) {
5657 const struct ssh2_ciphers *c = s->preferred_ciphers[i];
5658 if (!c) continue; /* warning flag */
5659 for (j = 0; j < c->nciphers; j++) {
5660 if (commalist_started)
5661 ssh2_pkt_addstring_str(s->pktout, ",");
5662 ssh2_pkt_addstring_str(s->pktout, c->list[j]->name);
5663 commalist_started = 1;
5664 }
51470298 5665 }
5666 }
cc18f256 5667 /* List MAC algorithms (client->server then server->client). */
5668 for (j = 0; j < 2; j++) {
5669 ssh2_pkt_addstring_start(s->pktout);
5670 for (i = 0; i < s->nmacs; i++) {
5671 ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name);
5672 if (i < s->nmacs - 1)
ff3187f6 5673 ssh2_pkt_addstring_str(s->pktout, ",");
51470298 5674 }
5675 }
ab18ccff 5676 /* List client->server compression algorithms,
5677 * then server->client compression algorithms. (We use the
5678 * same set twice.) */
5679 for (j = 0; j < 2; j++) {
5680 ssh2_pkt_addstring_start(s->pktout);
5681 assert(lenof(compressions) > 1);
5682 /* Prefer non-delayed versions */
5683 ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name);
5684 /* We don't even list delayed versions of algorithms until
5685 * they're allowed to be used, to avoid a race. See the end of
5686 * this function. */
5687 if (s->userauth_succeeded && s->preferred_comp->delayed_name) {
ff3187f6 5688 ssh2_pkt_addstring_str(s->pktout, ",");
ab18ccff 5689 ssh2_pkt_addstring_str(s->pktout,
5690 s->preferred_comp->delayed_name);
f6e0abe2 5691 }
ab18ccff 5692 for (i = 0; i < lenof(compressions); i++) {
5693 const struct ssh_compress *c = compressions[i];
5694 if (c != s->preferred_comp) {
5695 ssh2_pkt_addstring_str(s->pktout, ",");
5696 ssh2_pkt_addstring_str(s->pktout, c->name);
5697 if (s->userauth_succeeded && c->delayed_name) {
5698 ssh2_pkt_addstring_str(s->pktout, ",");
5699 ssh2_pkt_addstring_str(s->pktout, c->delayed_name);
5700 }
5701 }
f6e0abe2 5702 }
51470298 5703 }
5704 /* List client->server languages. Empty list. */
ff3187f6 5705 ssh2_pkt_addstring_start(s->pktout);
51470298 5706 /* List server->client languages. Empty list. */
ff3187f6 5707 ssh2_pkt_addstring_start(s->pktout);
51470298 5708 /* First KEX packet does _not_ follow, because we're not that brave. */
ff3187f6 5709 ssh2_pkt_addbool(s->pktout, FALSE);
51470298 5710 /* Reserved. */
ff3187f6 5711 ssh2_pkt_adduint32(s->pktout, 0);
e5574168 5712 }
0db56f73 5713
3dc9a6a7 5714 s->our_kexinitlen = s->pktout->length - 5;
5715 s->our_kexinit = snewn(s->our_kexinitlen, unsigned char);
5716 memcpy(s->our_kexinit, s->pktout->data + 5, s->our_kexinitlen);
0db56f73 5717
590f6a5f 5718 ssh2_pkt_send_noqueue(ssh, s->pktout);
e5574168 5719
ff3187f6 5720 if (!pktin)
5721 crWaitUntil(pktin);
e5574168 5722
5723 /*
5724 * Now examine the other side's KEXINIT to see what we're up
5725 * to.
5726 */
51470298 5727 {
4763c1c2 5728 char *str, *preferred;
3d9449a1 5729 int i, j, len;
51470298 5730
ff3187f6 5731 if (pktin->type != SSH2_MSG_KEXINIT) {
6b5cf8b4 5732 bombout(("expected key exchange packet from server"));
7ffdbc1a 5733 crStop(0);
32874aea 5734 }
51470298 5735 ssh->kex = NULL;
5736 ssh->hostkey = NULL;
5737 s->cscipher_tobe = NULL;
5738 s->sccipher_tobe = NULL;
5739 s->csmac_tobe = NULL;
5740 s->scmac_tobe = NULL;
5741 s->cscomp_tobe = NULL;
5742 s->sccomp_tobe = NULL;
4763c1c2 5743 s->warn_kex = s->warn_cscipher = s->warn_sccipher = FALSE;
5744
ff3187f6 5745 pktin->savedpos += 16; /* skip garbage cookie */
5746 ssh_pkt_getstring(pktin, &str, &len); /* key exchange algorithms */
4763c1c2 5747
5748 preferred = NULL;
83e7d008 5749 for (i = 0; i < s->n_preferred_kex; i++) {
34557659 5750 const struct ssh_kexes *k = s->preferred_kex[i];
83e7d008 5751 if (!k) {
4763c1c2 5752 s->warn_kex = TRUE;
5753 } else {
34557659 5754 for (j = 0; j < k->nkexes; j++) {
5755 if (!preferred) preferred = k->list[j]->name;
5756 if (in_commasep_string(k->list[j]->name, str, len)) {
5757 ssh->kex = k->list[j];
5758 break;
5759 }
5760 }
83e7d008 5761 }
4763c1c2 5762 if (ssh->kex)
51470298 5763 break;
32874aea 5764 }
83e7d008 5765 if (!ssh->kex) {
5766 bombout(("Couldn't agree a key exchange algorithm (available: %s)",
5767 str ? str : "(null)"));
5768 crStop(0);
5769 }
b59743d5 5770 /*
5771 * Note that the server's guess is considered wrong if it doesn't match
5772 * the first algorithm in our list, even if it's still the algorithm
5773 * we end up using.
5774 */
4763c1c2 5775 s->guessok = first_in_commasep_string(preferred, str, len);
ff3187f6 5776 ssh_pkt_getstring(pktin, &str, &len); /* host key algorithms */
51470298 5777 for (i = 0; i < lenof(hostkey_algs); i++) {
5778 if (in_commasep_string(hostkey_algs[i]->name, str, len)) {
5779 ssh->hostkey = hostkey_algs[i];
5780 break;
5781 }
5782 }
fc40b431 5783 if (!ssh->hostkey) {
5784 bombout(("Couldn't agree a host key algorithm (available: %s)",
5785 str ? str : "(null)"));
5786 crStop(0);
5787 }
5788
3d9449a1 5789 s->guessok = s->guessok &&
b59743d5 5790 first_in_commasep_string(hostkey_algs[0]->name, str, len);
ff3187f6 5791 ssh_pkt_getstring(pktin, &str, &len); /* client->server cipher */
51470298 5792 for (i = 0; i < s->n_preferred_ciphers; i++) {
5793 const struct ssh2_ciphers *c = s->preferred_ciphers[i];
5794 if (!c) {
4763c1c2 5795 s->warn_cscipher = TRUE;
51470298 5796 } else {
5797 for (j = 0; j < c->nciphers; j++) {
5798 if (in_commasep_string(c->list[j]->name, str, len)) {
5799 s->cscipher_tobe = c->list[j];
5800 break;
5801 }
ca20bfcf 5802 }
32874aea 5803 }
4763c1c2 5804 if (s->cscipher_tobe)
51470298 5805 break;
32874aea 5806 }
51470298 5807 if (!s->cscipher_tobe) {
6b5cf8b4 5808 bombout(("Couldn't agree a client-to-server cipher (available: %s)",
57356d63 5809 str ? str : "(null)"));
7ffdbc1a 5810 crStop(0);
ca20bfcf 5811 }
0ef8f407 5812
ff3187f6 5813 ssh_pkt_getstring(pktin, &str, &len); /* server->client cipher */
51470298 5814 for (i = 0; i < s->n_preferred_ciphers; i++) {
5815 const struct ssh2_ciphers *c = s->preferred_ciphers[i];
5816 if (!c) {
4763c1c2 5817 s->warn_sccipher = TRUE;
51470298 5818 } else {
5819 for (j = 0; j < c->nciphers; j++) {
5820 if (in_commasep_string(c->list[j]->name, str, len)) {
5821 s->sccipher_tobe = c->list[j];
5822 break;
5823 }
ca20bfcf 5824 }
32874aea 5825 }
4763c1c2 5826 if (s->sccipher_tobe)
51470298 5827 break;
32874aea 5828 }
51470298 5829 if (!s->sccipher_tobe) {
6b5cf8b4 5830 bombout(("Couldn't agree a server-to-client cipher (available: %s)",
57356d63 5831 str ? str : "(null)"));
7ffdbc1a 5832 crStop(0);
ca20bfcf 5833 }
0ef8f407 5834
ff3187f6 5835 ssh_pkt_getstring(pktin, &str, &len); /* client->server mac */
51470298 5836 for (i = 0; i < s->nmacs; i++) {
5837 if (in_commasep_string(s->maclist[i]->name, str, len)) {
5838 s->csmac_tobe = s->maclist[i];
5839 break;
5840 }
32874aea 5841 }
ff3187f6 5842 ssh_pkt_getstring(pktin, &str, &len); /* server->client mac */
51470298 5843 for (i = 0; i < s->nmacs; i++) {
5844 if (in_commasep_string(s->maclist[i]->name, str, len)) {
5845 s->scmac_tobe = s->maclist[i];
5846 break;
5847 }
32874aea 5848 }
ff3187f6 5849 ssh_pkt_getstring(pktin, &str, &len); /* client->server compression */
51470298 5850 for (i = 0; i < lenof(compressions) + 1; i++) {
5851 const struct ssh_compress *c =
5852 i == 0 ? s->preferred_comp : compressions[i - 1];
5853 if (in_commasep_string(c->name, str, len)) {
5854 s->cscomp_tobe = c;
5855 break;
ab18ccff 5856 } else if (in_commasep_string(c->delayed_name, str, len)) {
5857 if (s->userauth_succeeded) {
5858 s->cscomp_tobe = c;
5859 break;
5860 } else {
5861 s->pending_compression = TRUE; /* try this later */
5862 }
51470298 5863 }
32874aea 5864 }
ff3187f6 5865 ssh_pkt_getstring(pktin, &str, &len); /* server->client compression */
51470298 5866 for (i = 0; i < lenof(compressions) + 1; i++) {
5867 const struct ssh_compress *c =
5868 i == 0 ? s->preferred_comp : compressions[i - 1];
5869 if (in_commasep_string(c->name, str, len)) {
5870 s->sccomp_tobe = c;
5871 break;
ab18ccff 5872 } else if (in_commasep_string(c->delayed_name, str, len)) {
5873 if (s->userauth_succeeded) {
5874 s->sccomp_tobe = c;
5875 break;
5876 } else {
5877 s->pending_compression = TRUE; /* try this later */
5878 }
51470298 5879 }
32874aea 5880 }
ab18ccff 5881 if (s->pending_compression) {
5882 logevent("Server supports delayed compression; "
5883 "will try this later");
5884 }
b59743d5 5885 ssh_pkt_getstring(pktin, &str, &len); /* client->server language */
5886 ssh_pkt_getstring(pktin, &str, &len); /* server->client language */
4763c1c2 5887 s->ignorepkt = ssh2_pkt_getbool(pktin) && !s->guessok;
5888
5889 if (s->warn_kex) {
5890 ssh_set_frozen(ssh, 1);
5891 s->dlgret = askalg(ssh->frontend, "key-exchange algorithm",
5892 ssh->kex->name,
5893 ssh_dialog_callback, ssh);
5894 if (s->dlgret < 0) {
5895 do {
5896 crReturn(0);
5897 if (pktin) {
5898 bombout(("Unexpected data from server while"
5899 " waiting for user response"));
5900 crStop(0);
5901 }
5902 } while (pktin || inlen > 0);
5903 s->dlgret = ssh->user_response;
5904 }
5905 ssh_set_frozen(ssh, 0);
5906 if (s->dlgret == 0) {
9e296bfa 5907 ssh_disconnect(ssh, "User aborted at kex warning", NULL,
5908 0, TRUE);
a5a6f839 5909 crStop(0);
4763c1c2 5910 }
5911 }
5912
5913 if (s->warn_cscipher) {
5914 ssh_set_frozen(ssh, 1);
5915 s->dlgret = askalg(ssh->frontend,
5916 "client-to-server cipher",
5917 s->cscipher_tobe->name,
5918 ssh_dialog_callback, ssh);
5919 if (s->dlgret < 0) {
5920 do {
5921 crReturn(0);
5922 if (pktin) {
5923 bombout(("Unexpected data from server while"
5924 " waiting for user response"));
5925 crStop(0);
5926 }
5927 } while (pktin || inlen > 0);
5928 s->dlgret = ssh->user_response;
5929 }
5930 ssh_set_frozen(ssh, 0);
5931 if (s->dlgret == 0) {
9e296bfa 5932 ssh_disconnect(ssh, "User aborted at cipher warning", NULL,
5933 0, TRUE);
a5a6f839 5934 crStop(0);
4763c1c2 5935 }
5936 }
5937
5938 if (s->warn_sccipher) {
5939 ssh_set_frozen(ssh, 1);
5940 s->dlgret = askalg(ssh->frontend,
5941 "server-to-client cipher",
5942 s->sccipher_tobe->name,
5943 ssh_dialog_callback, ssh);
5944 if (s->dlgret < 0) {
5945 do {
5946 crReturn(0);
5947 if (pktin) {
5948 bombout(("Unexpected data from server while"
5949 " waiting for user response"));
5950 crStop(0);
5951 }
5952 } while (pktin || inlen > 0);
5953 s->dlgret = ssh->user_response;
5954 }
5955 ssh_set_frozen(ssh, 0);
5956 if (s->dlgret == 0) {
9e296bfa 5957 ssh_disconnect(ssh, "User aborted at cipher warning", NULL,
5958 0, TRUE);
a5a6f839 5959 crStop(0);
4763c1c2 5960 }
5961 }
5962
b672f405 5963 ssh->exhash = ssh->kex->hash->init();
5964 hash_string(ssh->kex->hash, ssh->exhash, ssh->v_c, strlen(ssh->v_c));
5965 hash_string(ssh->kex->hash, ssh->exhash, ssh->v_s, strlen(ssh->v_s));
5966 hash_string(ssh->kex->hash, ssh->exhash,
5967 s->our_kexinit, s->our_kexinitlen);
3dc9a6a7 5968 sfree(s->our_kexinit);
5969 if (pktin->length > 5)
b672f405 5970 hash_string(ssh->kex->hash, ssh->exhash,
5971 pktin->data + 5, pktin->length - 5);
3dc9a6a7 5972
4763c1c2 5973 if (s->ignorepkt) /* first_kex_packet_follows */
b59743d5 5974 crWaitUntil(pktin); /* Ignore packet */
e5574168 5975 }
e5574168 5976
fae1a71b 5977 if (ssh->kex->main_type == KEXTYPE_DH) {
30774ba3 5978 /*
5979 * Work out the number of bits of key we will need from the
5980 * key exchange. We start with the maximum key length of
5981 * either cipher...
5982 */
5983 {
5984 int csbits, scbits;
5985
5986 csbits = s->cscipher_tobe->keylen;
5987 scbits = s->sccipher_tobe->keylen;
5988 s->nbits = (csbits > scbits ? csbits : scbits);
5989 }
5990 /* The keys only have hlen-bit entropy, since they're based on
5991 * a hash. So cap the key size at hlen bits. */
5992 if (s->nbits > ssh->kex->hash->hlen * 8)
5993 s->nbits = ssh->kex->hash->hlen * 8;
5994
5995 /*
5996 * If we're doing Diffie-Hellman group exchange, start by
5997 * requesting a group.
5998 */
5999 if (!ssh->kex->pdata) {
6000 logevent("Doing Diffie-Hellman group exchange");
acab36bc 6001 ssh->pkt_kctx = SSH2_PKTCTX_DHGEX;
30774ba3 6002 /*
6003 * Work out how big a DH group we will need to allow that
6004 * much data.
6005 */
6006 s->pbits = 512 << ((s->nbits - 1) / 64);
6007 s->pktout = ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST);
6008 ssh2_pkt_adduint32(s->pktout, s->pbits);
6009 ssh2_pkt_send_noqueue(ssh, s->pktout);
6010
6011 crWaitUntil(pktin);
6012 if (pktin->type != SSH2_MSG_KEX_DH_GEX_GROUP) {
6013 bombout(("expected key exchange group packet from server"));
6014 crStop(0);
6015 }
6016 s->p = ssh2_pkt_getmp(pktin);
6017 s->g = ssh2_pkt_getmp(pktin);
6018 if (!s->p || !s->g) {
6019 bombout(("unable to read mp-ints from incoming group packet"));
6020 crStop(0);
6021 }
6022 ssh->kex_ctx = dh_setup_gex(s->p, s->g);
6023 s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT;
6024 s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY;
6025 } else {
acab36bc 6026 ssh->pkt_kctx = SSH2_PKTCTX_DHGROUP;
30774ba3 6027 ssh->kex_ctx = dh_setup_group(ssh->kex);
6028 s->kex_init_value = SSH2_MSG_KEXDH_INIT;
6029 s->kex_reply_value = SSH2_MSG_KEXDH_REPLY;
6030 logeventf(ssh, "Using Diffie-Hellman with standard group \"%s\"",
6031 ssh->kex->groupname);
6032 }
e5574168 6033
30774ba3 6034 logeventf(ssh, "Doing Diffie-Hellman key exchange with hash %s",
6035 ssh->kex->hash->text_name);
6036 /*
6037 * Now generate and send e for Diffie-Hellman.
6038 */
6039 set_busy_status(ssh->frontend, BUSY_CPU); /* this can take a while */
6040 s->e = dh_create_e(ssh->kex_ctx, s->nbits * 2);
6041 s->pktout = ssh2_pkt_init(s->kex_init_value);
6042 ssh2_pkt_addmp(s->pktout, s->e);
6043 ssh2_pkt_send_noqueue(ssh, s->pktout);
6044
6045 set_busy_status(ssh->frontend, BUSY_WAITING); /* wait for server */
6046 crWaitUntil(pktin);
6047 if (pktin->type != s->kex_reply_value) {
6048 bombout(("expected key exchange reply packet from server"));
6049 crStop(0);
6050 }
6051 set_busy_status(ssh->frontend, BUSY_CPU); /* cogitate */
6052 ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen);
6053 s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen);
6054 s->f = ssh2_pkt_getmp(pktin);
6055 if (!s->f) {
6056 bombout(("unable to parse key exchange reply packet"));
6057 crStop(0);
6058 }
6059 ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen);
e5574168 6060
30774ba3 6061 s->K = dh_find_K(ssh->kex_ctx, s->f);
e5574168 6062
30774ba3 6063 /* We assume everything from now on will be quick, and it might
6064 * involve user interaction. */
6065 set_busy_status(ssh->frontend, BUSY_NOT);
755e0524 6066
30774ba3 6067 hash_string(ssh->kex->hash, ssh->exhash, s->hostkeydata, s->hostkeylen);
6068 if (!ssh->kex->pdata) {
6069 hash_uint32(ssh->kex->hash, ssh->exhash, s->pbits);
6070 hash_mpint(ssh->kex->hash, ssh->exhash, s->p);
6071 hash_mpint(ssh->kex->hash, ssh->exhash, s->g);
6072 }
6073 hash_mpint(ssh->kex->hash, ssh->exhash, s->e);
6074 hash_mpint(ssh->kex->hash, ssh->exhash, s->f);
6075
6076 dh_cleanup(ssh->kex_ctx);
6077 freebn(s->f);
6078 if (!ssh->kex->pdata) {
6079 freebn(s->g);
6080 freebn(s->p);
6081 }
fae1a71b 6082 } else {
6083 logeventf(ssh, "Doing RSA key exchange with hash %s",
6084 ssh->kex->hash->text_name);
acab36bc 6085 ssh->pkt_kctx = SSH2_PKTCTX_RSAKEX;
fae1a71b 6086 /*
6087 * RSA key exchange. First expect a KEXRSA_PUBKEY packet
6088 * from the server.
6089 */
6090 crWaitUntil(pktin);
6091 if (pktin->type != SSH2_MSG_KEXRSA_PUBKEY) {
6092 bombout(("expected RSA public key packet from server"));
6093 crStop(0);
6094 }
6095
6096 ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen);
6097 hash_string(ssh->kex->hash, ssh->exhash,
6098 s->hostkeydata, s->hostkeylen);
6099 s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen);
6100
6101 {
6102 char *keydata;
6103 ssh_pkt_getstring(pktin, &keydata, &s->rsakeylen);
6104 s->rsakeydata = snewn(s->rsakeylen, char);
6105 memcpy(s->rsakeydata, keydata, s->rsakeylen);
6106 }
6107
6108 s->rsakey = ssh_rsakex_newkey(s->rsakeydata, s->rsakeylen);
6109 if (!s->rsakey) {
6110 sfree(s->rsakeydata);
6111 bombout(("unable to parse RSA public key from server"));
6112 crStop(0);
6113 }
6114
6115 hash_string(ssh->kex->hash, ssh->exhash, s->rsakeydata, s->rsakeylen);
6116
6117 /*
6118 * Next, set up a shared secret K, of precisely KLEN -
6119 * 2*HLEN - 49 bits, where KLEN is the bit length of the
6120 * RSA key modulus and HLEN is the bit length of the hash
6121 * we're using.
6122 */
6123 {
6124 int klen = ssh_rsakex_klen(s->rsakey);
6125 int nbits = klen - (2*ssh->kex->hash->hlen*8 + 49);
6126 int i, byte = 0;
6127 unsigned char *kstr1, *kstr2, *outstr;
6128 int kstr1len, kstr2len, outstrlen;
6129
6130 s->K = bn_power_2(nbits - 1);
6131
6132 for (i = 0; i < nbits; i++) {
6133 if ((i & 7) == 0) {
6134 byte = random_byte();
6135 }
6136 bignum_set_bit(s->K, i, (byte >> (i & 7)) & 1);
6137 }
6138
6139 /*
6140 * Encode this as an mpint.
6141 */
6142 kstr1 = ssh2_mpint_fmt(s->K, &kstr1len);
6143 kstr2 = snewn(kstr2len = 4 + kstr1len, unsigned char);
6144 PUT_32BIT(kstr2, kstr1len);
6145 memcpy(kstr2 + 4, kstr1, kstr1len);
6146
6147 /*
6148 * Encrypt it with the given RSA key.
6149 */
6150 outstrlen = (klen + 7) / 8;
6151 outstr = snewn(outstrlen, unsigned char);
6152 ssh_rsakex_encrypt(ssh->kex->hash, kstr2, kstr2len,
6153 outstr, outstrlen, s->rsakey);
6154
6155 /*
6156 * And send it off in a return packet.
6157 */
6158 s->pktout = ssh2_pkt_init(SSH2_MSG_KEXRSA_SECRET);
6159 ssh2_pkt_addstring_start(s->pktout);
7108a872 6160 ssh2_pkt_addstring_data(s->pktout, (char *)outstr, outstrlen);
fae1a71b 6161 ssh2_pkt_send_noqueue(ssh, s->pktout);
6162
6163 hash_string(ssh->kex->hash, ssh->exhash, outstr, outstrlen);
6164
6165 sfree(kstr2);
6166 sfree(kstr1);
6167 sfree(outstr);
6168 }
6169
6170 ssh_rsakex_freekey(s->rsakey);
6171
6172 crWaitUntil(pktin);
6173 if (pktin->type != SSH2_MSG_KEXRSA_DONE) {
6174 sfree(s->rsakeydata);
6175 bombout(("expected signature packet from server"));
6176 crStop(0);
6177 }
6178
6179 ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen);
6180
6181 sfree(s->rsakeydata);
6182 }
6183
b672f405 6184 hash_mpint(ssh->kex->hash, ssh->exhash, s->K);
6185 assert(ssh->kex->hash->hlen <= sizeof(s->exchange_hash));
6186 ssh->kex->hash->final(ssh->exhash, s->exchange_hash);
e5574168 6187
fabd1805 6188 ssh->kex_ctx = NULL;
3709bfe9 6189
7cca0d81 6190#if 0
765c4200 6191 debug(("Exchange hash is:\n"));
b672f405 6192 dmemdump(s->exchange_hash, ssh->kex->hash->hlen);
7cca0d81 6193#endif
6194
51470298 6195 if (!s->hkey ||
6196 !ssh->hostkey->verifysig(s->hkey, s->sigdata, s->siglen,
b672f405 6197 (char *)s->exchange_hash,
6198 ssh->kex->hash->hlen)) {
6b5cf8b4 6199 bombout(("Server's host key did not match the signature supplied"));
7ffdbc1a 6200 crStop(0);
8d5de777 6201 }
e5574168 6202
6203 /*
7cca0d81 6204 * Authenticate remote host: verify host key. (We've already
6205 * checked the signature of the exchange hash.)
e5574168 6206 */
51470298 6207 s->keystr = ssh->hostkey->fmtkey(s->hkey);
6208 s->fingerprint = ssh->hostkey->fingerprint(s->hkey);
3d9449a1 6209 ssh_set_frozen(ssh, 1);
6210 s->dlgret = verify_ssh_host_key(ssh->frontend,
6211 ssh->savedhost, ssh->savedport,
6212 ssh->hostkey->keytype, s->keystr,
6213 s->fingerprint,
6214 ssh_dialog_callback, ssh);
6215 if (s->dlgret < 0) {
6216 do {
6217 crReturn(0);
6218 if (pktin) {
6219 bombout(("Unexpected data from server while waiting"
6220 " for user host key response"));
6221 crStop(0);
6222 }
6223 } while (pktin || inlen > 0);
6224 s->dlgret = ssh->user_response;
6225 }
6226 ssh_set_frozen(ssh, 0);
6227 if (s->dlgret == 0) {
9e296bfa 6228 ssh_disconnect(ssh, "User aborted at host key verification", NULL,
6229 0, TRUE);
3d9449a1 6230 crStop(0);
6231 }
e13bba36 6232 if (!s->got_session_id) { /* don't bother logging this in rekeys */
5e0d7cb8 6233 logevent("Host key fingerprint is:");
51470298 6234 logevent(s->fingerprint);
5e0d7cb8 6235 }
51470298 6236 sfree(s->fingerprint);
6237 sfree(s->keystr);
6238 ssh->hostkey->freekey(s->hkey);
d39f364a 6239
6240 /*
9442dd57 6241 * The exchange hash from the very first key exchange is also
6242 * the session id, used in session key construction and
6243 * authentication.
6244 */
e13bba36 6245 if (!s->got_session_id) {
b672f405 6246 assert(sizeof(s->exchange_hash) <= sizeof(ssh->v2_session_id));
9442dd57 6247 memcpy(ssh->v2_session_id, s->exchange_hash,
6248 sizeof(s->exchange_hash));
b672f405 6249 ssh->v2_session_id_len = ssh->kex->hash->hlen;
6052bb76 6250 assert(ssh->v2_session_id_len <= sizeof(ssh->v2_session_id));
e13bba36 6251 s->got_session_id = TRUE;
6252 }
9442dd57 6253
6254 /*
7cca0d81 6255 * Send SSH2_MSG_NEWKEYS.
d39f364a 6256 */
ff3187f6 6257 s->pktout = ssh2_pkt_init(SSH2_MSG_NEWKEYS);
590f6a5f 6258 ssh2_pkt_send_noqueue(ssh, s->pktout);
9442dd57 6259 ssh->outgoing_data_size = 0; /* start counting from here */
6260
6261 /*
6262 * We've sent client NEWKEYS, so create and initialise
c64fe7d4 6263 * client-to-server session keys.
9442dd57 6264 */
6265 if (ssh->cs_cipher_ctx)
6266 ssh->cscipher->free_context(ssh->cs_cipher_ctx);
6267 ssh->cscipher = s->cscipher_tobe;
6268 ssh->cs_cipher_ctx = ssh->cscipher->make_context();
6269
6270 if (ssh->cs_mac_ctx)
6271 ssh->csmac->free_context(ssh->cs_mac_ctx);
6272 ssh->csmac = s->csmac_tobe;
6273 ssh->cs_mac_ctx = ssh->csmac->make_context();
6274
6275 if (ssh->cs_comp_ctx)
6276 ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx);
6277 ssh->cscomp = s->cscomp_tobe;
6278 ssh->cs_comp_ctx = ssh->cscomp->compress_init();
6279
6280 /*
6281 * Set IVs on client-to-server keys. Here we use the exchange
6282 * hash from the _first_ key exchange.
6283 */
6284 {
754c0df9 6285 unsigned char keyspace[SSH2_KEX_MAX_HASH_LEN * SSH2_MKKEY_ITERS];
6286 assert(sizeof(keyspace) >= ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);
b672f405 6287 ssh2_mkkey(ssh,s->K,s->exchange_hash,'C',keyspace);
754c0df9 6288 assert((ssh->cscipher->keylen+7) / 8 <=
6289 ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);
9442dd57 6290 ssh->cscipher->setkey(ssh->cs_cipher_ctx, keyspace);
b672f405 6291 ssh2_mkkey(ssh,s->K,s->exchange_hash,'A',keyspace);
754c0df9 6292 assert(ssh->cscipher->blksize <=
6293 ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);
9442dd57 6294 ssh->cscipher->setiv(ssh->cs_cipher_ctx, keyspace);
b672f405 6295 ssh2_mkkey(ssh,s->K,s->exchange_hash,'E',keyspace);
754c0df9 6296 assert(ssh->csmac->len <=
6297 ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);
9442dd57 6298 ssh->csmac->setkey(ssh->cs_mac_ctx, keyspace);
dfb88efd 6299 smemclr(keyspace, sizeof(keyspace));
9442dd57 6300 }
6301
6302 logeventf(ssh, "Initialised %.200s client->server encryption",
6303 ssh->cscipher->text_name);
6304 logeventf(ssh, "Initialised %.200s client->server MAC algorithm",
6305 ssh->csmac->text_name);
6306 if (ssh->cscomp->text_name)
6307 logeventf(ssh, "Initialised %s compression",
6308 ssh->cscomp->text_name);
590f6a5f 6309
6310 /*
6311 * Now our end of the key exchange is complete, we can send all
6312 * our queued higher-layer packets.
6313 */
6314 ssh->queueing = FALSE;
6315 ssh2_pkt_queuesend(ssh);
d39f364a 6316
6317 /*
8406eaf9 6318 * Expect SSH2_MSG_NEWKEYS from server.
6319 */
ff3187f6 6320 crWaitUntil(pktin);
6321 if (pktin->type != SSH2_MSG_NEWKEYS) {
6b5cf8b4 6322 bombout(("expected new-keys packet from server"));
7ffdbc1a 6323 crStop(0);
8406eaf9 6324 }
9442dd57 6325 ssh->incoming_data_size = 0; /* start counting from here */
8406eaf9 6326
6327 /*
9442dd57 6328 * We've seen server NEWKEYS, so create and initialise
6329 * server-to-client session keys.
d39f364a 6330 */
371e569c 6331 if (ssh->sc_cipher_ctx)
6332 ssh->sccipher->free_context(ssh->sc_cipher_ctx);
51470298 6333 ssh->sccipher = s->sccipher_tobe;
371e569c 6334 ssh->sc_cipher_ctx = ssh->sccipher->make_context();
e0e1a00d 6335
e0e1a00d 6336 if (ssh->sc_mac_ctx)
6337 ssh->scmac->free_context(ssh->sc_mac_ctx);
51470298 6338 ssh->scmac = s->scmac_tobe;
e0e1a00d 6339 ssh->sc_mac_ctx = ssh->scmac->make_context();
6340
5366aed8 6341 if (ssh->sc_comp_ctx)
6342 ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx);
51470298 6343 ssh->sccomp = s->sccomp_tobe;
5366aed8 6344 ssh->sc_comp_ctx = ssh->sccomp->decompress_init();
6345
d39f364a 6346 /*
9442dd57 6347 * Set IVs on server-to-client keys. Here we use the exchange
6348 * hash from the _first_ key exchange.
d39f364a 6349 */
51470298 6350 {
754c0df9 6351 unsigned char keyspace[SSH2_KEX_MAX_HASH_LEN * SSH2_MKKEY_ITERS];
6352 assert(sizeof(keyspace) >= ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);
b672f405 6353 ssh2_mkkey(ssh,s->K,s->exchange_hash,'D',keyspace);
754c0df9 6354 assert((ssh->sccipher->keylen+7) / 8 <=
6355 ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);
371e569c 6356 ssh->sccipher->setkey(ssh->sc_cipher_ctx, keyspace);
b672f405 6357 ssh2_mkkey(ssh,s->K,s->exchange_hash,'B',keyspace);
754c0df9 6358 assert(ssh->sccipher->blksize <=
6359 ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);
371e569c 6360 ssh->sccipher->setiv(ssh->sc_cipher_ctx, keyspace);
b672f405 6361 ssh2_mkkey(ssh,s->K,s->exchange_hash,'F',keyspace);
754c0df9 6362 assert(ssh->scmac->len <=
6363 ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);
e0e1a00d 6364 ssh->scmac->setkey(ssh->sc_mac_ctx, keyspace);
dfb88efd 6365 smemclr(keyspace, sizeof(keyspace));
51470298 6366 }
57356d63 6367 logeventf(ssh, "Initialised %.200s server->client encryption",
6368 ssh->sccipher->text_name);
6c135243 6369 logeventf(ssh, "Initialised %.200s server->client MAC algorithm",
6370 ssh->scmac->text_name);
57356d63 6371 if (ssh->sccomp->text_name)
6372 logeventf(ssh, "Initialised %s decompression",
6373 ssh->sccomp->text_name);
9442dd57 6374
6375 /*
fae1a71b 6376 * Free shared secret.
9442dd57 6377 */
679539d7 6378 freebn(s->K);
d39f364a 6379
033b4cef 6380 /*
e13bba36 6381 * Key exchange is over. Loop straight back round if we have a
6382 * deferred rekey reason.
6383 */
6384 if (ssh->deferred_rekey_reason) {
6385 logevent(ssh->deferred_rekey_reason);
6386 pktin = NULL;
6387 ssh->deferred_rekey_reason = NULL;
6388 goto begin_key_exchange;
6389 }
6390
6391 /*
6392 * Otherwise, schedule a timer for our next rekey.
9442dd57 6393 */
6394 ssh->kex_in_progress = FALSE;
e6c1536e 6395 ssh->last_rekey = GETTICKCOUNT();
4a693cfc 6396 if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0)
6397 ssh->next_rekey = schedule_timer(conf_get_int(ssh->conf, CONF_ssh_rekey_time)*60*TICKSPERSEC,
d57f70af 6398 ssh2_timer, ssh);
e13bba36 6399
9442dd57 6400 /*
7cca0d81 6401 * Now we're encrypting. Begin returning 1 to the protocol main
6402 * function so that other things can run on top of the
6403 * transport. If we ever see a KEXINIT, we must go back to the
6404 * start.
9442dd57 6405 *
6406 * We _also_ go back to the start if we see pktin==NULL and
ab18ccff 6407 * inlen negative, because this is a special signal meaning
9442dd57 6408 * `initiate client-driven rekey', and `in' contains a message
6409 * giving the reason for the rekey.
ab18ccff 6410 *
6411 * inlen==-1 means always initiate a rekey;
6412 * inlen==-2 means that userauth has completed successfully and
6413 * we should consider rekeying (for delayed compression).
033b4cef 6414 */
9442dd57 6415 while (!((pktin && pktin->type == SSH2_MSG_KEXINIT) ||
ab18ccff 6416 (!pktin && inlen < 0))) {
f382c87d 6417 wait_for_rekey:
bc074147 6418 if (!ssh->protocol_initial_phase_done) {
6419 ssh->protocol_initial_phase_done = TRUE;
6420 /*
6421 * Allow authconn to initialise itself.
6422 */
6423 do_ssh2_authconn(ssh, NULL, 0, NULL);
6424 }
32874aea 6425 crReturn(1);
e96adf72 6426 }
9442dd57 6427 if (pktin) {
6428 logevent("Server initiated key re-exchange");
6429 } else {
ab18ccff 6430 if (inlen == -2) {
6431 /*
6432 * authconn has seen a USERAUTH_SUCCEEDED. Time to enable
6433 * delayed compression, if it's available.
6434 *
6435 * draft-miller-secsh-compression-delayed-00 says that you
6436 * negotiate delayed compression in the first key exchange, and
6437 * both sides start compressing when the server has sent
6438 * USERAUTH_SUCCESS. This has a race condition -- the server
6439 * can't know when the client has seen it, and thus which incoming
6440 * packets it should treat as compressed.
6441 *
6442 * Instead, we do the initial key exchange without offering the
6443 * delayed methods, but note if the server offers them; when we
6444 * get here, if a delayed method was available that was higher
6445 * on our list than what we got, we initiate a rekey in which we
6446 * _do_ list the delayed methods (and hopefully get it as a
6447 * result). Subsequent rekeys will do the same.
6448 */
6449 assert(!s->userauth_succeeded); /* should only happen once */
6450 s->userauth_succeeded = TRUE;
6451 if (!s->pending_compression)
6452 /* Can't see any point rekeying. */
6453 goto wait_for_rekey; /* this is utterly horrid */
6454 /* else fall through to rekey... */
6455 s->pending_compression = FALSE;
6456 }
f382c87d 6457 /*
ab18ccff 6458 * Now we've decided to rekey.
6459 *
f382c87d 6460 * Special case: if the server bug is set that doesn't
6461 * allow rekeying, we give a different log message and
6462 * continue waiting. (If such a server _initiates_ a rekey,
6463 * we process it anyway!)
6464 */
6465 if ((ssh->remote_bugs & BUG_SSH2_REKEY)) {
6466 logeventf(ssh, "Server bug prevents key re-exchange (%s)",
6467 (char *)in);
6468 /* Reset the counters, so that at least this message doesn't
6469 * hit the event log _too_ often. */
6470 ssh->outgoing_data_size = 0;
6471 ssh->incoming_data_size = 0;
4a693cfc 6472 if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0) {
f382c87d 6473 ssh->next_rekey =
4a693cfc 6474 schedule_timer(conf_get_int(ssh->conf, CONF_ssh_rekey_time)*60*TICKSPERSEC,
f382c87d 6475 ssh2_timer, ssh);
6476 }
ab18ccff 6477 goto wait_for_rekey; /* this is still utterly horrid */
f382c87d 6478 } else {
6479 logeventf(ssh, "Initiating key re-exchange (%s)", (char *)in);
f382c87d 6480 }
9442dd57 6481 }
7cca0d81 6482 goto begin_key_exchange;
e5574168 6483
6484 crFinish(1);
6485}
6486
7cca0d81 6487/*
2e85c969 6488 * Add data to an SSH-2 channel output buffer.
783415f8 6489 */
32874aea 6490static void ssh2_add_channel_data(struct ssh_channel *c, char *buf,
6491 int len)
6492{
5471d09a 6493 bufchain_add(&c->v.v2.outbuffer, buf, len);
783415f8 6494}
6495
6496/*
2e85c969 6497 * Attempt to send data on an SSH-2 channel.
783415f8 6498 */
5471d09a 6499static int ssh2_try_send(struct ssh_channel *c)
32874aea 6500{
51470298 6501 Ssh ssh = c->ssh;
ff3187f6 6502 struct Packet *pktout;
bc06669b 6503 int ret;
51470298 6504
5471d09a 6505 while (c->v.v2.remwindow > 0 && bufchain_size(&c->v.v2.outbuffer) > 0) {
6506 int len;
6507 void *data;
6508 bufchain_prefix(&c->v.v2.outbuffer, &data, &len);
6509 if ((unsigned)len > c->v.v2.remwindow)
6510 len = c->v.v2.remwindow;
6511 if ((unsigned)len > c->v.v2.remmaxpkt)
6512 len = c->v.v2.remmaxpkt;
ff3187f6 6513 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA);
6514 ssh2_pkt_adduint32(pktout, c->remoteid);
ff3187f6 6515 ssh2_pkt_addstring_start(pktout);
a2724fba 6516 dont_log_data(ssh, pktout, PKTLOG_OMIT);
ff3187f6 6517 ssh2_pkt_addstring_data(pktout, data, len);
6518 end_log_omission(ssh, pktout);
6519 ssh2_pkt_send(ssh, pktout);
5471d09a 6520 bufchain_consume(&c->v.v2.outbuffer, len);
6521 c->v.v2.remwindow -= len;
6522 }
6523
6524 /*
6525 * After having sent as much data as we can, return the amount
6526 * still buffered.
6527 */
bc06669b 6528 ret = bufchain_size(&c->v.v2.outbuffer);
6529
6530 /*
6531 * And if there's no data pending but we need to send an EOF, send
6532 * it.
6533 */
6534 if (!ret && c->pending_eof)
6535 ssh_channel_try_eof(c);
6536
6537 return ret;
5471d09a 6538}
6539
78280721 6540static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
1bfc7e93 6541{
6542 int bufsize;
bc06669b 6543 if (c->closes & CLOSES_SENT_EOF)
6544 return; /* don't send on channels we've EOFed */
1bfc7e93 6545 bufsize = ssh2_try_send(c);
6546 if (bufsize == 0) {
6547 switch (c->type) {
6548 case CHAN_MAINSESSION:
6549 /* stdin need not receive an unthrottle
6550 * notification since it will be polled */
6551 break;
6552 case CHAN_X11:
6553 x11_unthrottle(c->u.x11.s);
6554 break;
6555 case CHAN_AGENT:
6556 /* agent sockets are request/response and need no
6557 * buffer management */
6558 break;
6559 case CHAN_SOCKDATA:
6560 pfd_unthrottle(c->u.pfd.s);
6561 break;
6562 }
6563 }
6564}
6565
5471d09a 6566/*
94bdfe40 6567 * Set up most of a new ssh_channel for SSH-2.
6568 */
6569static void ssh2_channel_init(struct ssh_channel *c)
6570{
6571 Ssh ssh = c->ssh;
6572 c->localid = alloc_channel_id(ssh);
6573 c->closes = 0;
bc06669b 6574 c->pending_eof = FALSE;
94bdfe40 6575 c->throttling_conn = FALSE;
6576 c->v.v2.locwindow = c->v.v2.locmaxwin = c->v.v2.remlocwin =
4a693cfc 6577 conf_get_int(ssh->conf, CONF_ssh_simple) ? OUR_V2_BIGWIN : OUR_V2_WINSIZE;
39d830e4 6578 c->v.v2.chanreq_head = NULL;
94bdfe40 6579 c->v.v2.throttle_state = UNTHROTTLED;
6580 bufchain_init(&c->v.v2.outbuffer);
6581}
6582
6583/*
39d830e4 6584 * CHANNEL_FAILURE doesn't come with any indication of what message
6585 * caused it, so we have to keep track of the outstanding
6586 * CHANNEL_REQUESTs ourselves.
6587 */
6588static void ssh2_queue_chanreq_handler(struct ssh_channel *c,
6589 cchandler_fn_t handler, void *ctx)
6590{
6591 struct outstanding_channel_request *ocr =
6592 snew(struct outstanding_channel_request);
6593
3620f307 6594 assert(!(c->closes & (CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE)));
39d830e4 6595 ocr->handler = handler;
6596 ocr->ctx = ctx;
6597 ocr->next = NULL;
6598 if (!c->v.v2.chanreq_head)
6599 c->v.v2.chanreq_head = ocr;
6600 else
6601 c->v.v2.chanreq_tail->next = ocr;
6602 c->v.v2.chanreq_tail = ocr;
6603}
6604
6605/*
134f5ece 6606 * Construct the common parts of a CHANNEL_REQUEST. If handler is not
6607 * NULL then a reply will be requested and the handler will be called
6608 * when it arrives. The returned packet is ready to have any
6609 * request-specific data added and be sent. Note that if a handler is
6610 * provided, it's essential that the request actually be sent.
3620f307 6611 *
6612 * The handler will usually be passed the response packet in pktin.
6613 * If pktin is NULL, this means that no reply will ever be forthcoming
6614 * (e.g. because the entire connection is being destroyed) and the
6615 * handler should free any storage it's holding.
134f5ece 6616 */
6617static struct Packet *ssh2_chanreq_init(struct ssh_channel *c, char *type,
6618 cchandler_fn_t handler, void *ctx)
6619{
6620 struct Packet *pktout;
6621
3620f307 6622 assert(!(c->closes & (CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE)));
134f5ece 6623 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
6624 ssh2_pkt_adduint32(pktout, c->remoteid);
6625 ssh2_pkt_addstring(pktout, type);
6626 ssh2_pkt_addbool(pktout, handler != NULL);
6627 if (handler != NULL)
6628 ssh2_queue_chanreq_handler(c, handler, ctx);
6629 return pktout;
6630}
6631
6632/*
2e85c969 6633 * Potentially enlarge the window on an SSH-2 channel.
5471d09a 6634 */
39d830e4 6635static void ssh2_handle_winadj_response(struct ssh_channel *, struct Packet *,
6636 void *);
9fca3f8c 6637static void ssh2_set_window(struct ssh_channel *c, int newwin)
5471d09a 6638{
51470298 6639 Ssh ssh = c->ssh;
6640
6b69f42e 6641 /*
bc06669b 6642 * Never send WINDOW_ADJUST for a channel that the remote side has
6643 * already sent EOF on; there's no point, since it won't be
beff003a 6644 * sending any more data anyway. Ditto if _we've_ already sent
6645 * CLOSE.
6b69f42e 6646 */
beff003a 6647 if (c->closes & (CLOSES_RCVD_EOF | CLOSES_SENT_CLOSE))
6b69f42e 6648 return;
6649
d252310a 6650 /*
c9739dba 6651 * If the remote end has a habit of ignoring maxpkt, limit the
6652 * window so that it has no choice (assuming it doesn't ignore the
6653 * window as well).
6654 */
6655 if ((ssh->remote_bugs & BUG_SSH2_MAXPKT) && newwin > OUR_V2_MAXPKT)
6656 newwin = OUR_V2_MAXPKT;
c9739dba 6657
6658 /*
d252310a 6659 * Only send a WINDOW_ADJUST if there's significantly more window
6660 * available than the other end thinks there is. This saves us
6661 * sending a WINDOW_ADJUST for every character in a shell session.
6662 *
6663 * "Significant" is arbitrarily defined as half the window size.
6664 */
3361b80a 6665 if (newwin / 2 >= c->v.v2.locwindow) {
ff3187f6 6666 struct Packet *pktout;
39d830e4 6667 unsigned *up;
ff3187f6 6668
0e443c99 6669 /*
6670 * In order to keep track of how much window the client
6671 * actually has available, we'd like it to acknowledge each
6672 * WINDOW_ADJUST. We can't do that directly, so we accompany
6673 * it with a CHANNEL_REQUEST that has to be acknowledged.
6674 *
6675 * This is only necessary if we're opening the window wide.
6676 * If we're not, then throughput is being constrained by
6677 * something other than the maximum window size anyway.
0e443c99 6678 */
6679 if (newwin == c->v.v2.locmaxwin &&
2e0aae4f 6680 !(ssh->remote_bugs & BUG_CHOKES_ON_WINADJ)) {
39d830e4 6681 up = snew(unsigned);
6682 *up = newwin - c->v.v2.locwindow;
134f5ece 6683 pktout = ssh2_chanreq_init(c, "winadj@putty.projects.tartarus.org",
6684 ssh2_handle_winadj_response, up);
6685 ssh2_pkt_send(ssh, pktout);
6686
0e443c99 6687 if (c->v.v2.throttle_state != UNTHROTTLED)
6688 c->v.v2.throttle_state = UNTHROTTLING;
6689 } else {
6690 /* Pretend the WINDOW_ADJUST was acked immediately. */
6691 c->v.v2.remlocwin = newwin;
6692 c->v.v2.throttle_state = THROTTLED;
6693 }
ff3187f6 6694 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
6695 ssh2_pkt_adduint32(pktout, c->remoteid);
6696 ssh2_pkt_adduint32(pktout, newwin - c->v.v2.locwindow);
6697 ssh2_pkt_send(ssh, pktout);
5471d09a 6698 c->v.v2.locwindow = newwin;
783415f8 6699 }
6700}
6701
e223b7c7 6702/*
6703 * Find the channel associated with a message. If there's no channel,
6704 * or it's not properly open, make a noise about it and return NULL.
6705 */
6706static struct ssh_channel *ssh2_channel_msg(Ssh ssh, struct Packet *pktin)
6707{
6708 unsigned localid = ssh_pkt_getuint32(pktin);
6709 struct ssh_channel *c;
6710
6711 c = find234(ssh->channels, &localid, ssh_channelfind);
6712 if (!c ||
6713 (c->halfopen && pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION &&
6714 pktin->type != SSH2_MSG_CHANNEL_OPEN_FAILURE)) {
6715 char *buf = dupprintf("Received %s for %s channel %u",
6716 ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx,
6717 pktin->type),
6718 c ? "half-open" : "nonexistent", localid);
6719 ssh_disconnect(ssh, NULL, buf, SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE);
6720 sfree(buf);
6721 return NULL;
6722 }
6723 return c;
6724}
6725
39d830e4 6726static void ssh2_handle_winadj_response(struct ssh_channel *c,
6727 struct Packet *pktin, void *ctx)
e030765f 6728{
39d830e4 6729 unsigned *sizep = ctx;
6730
6731 /*
6732 * Winadj responses should always be failures. However, at least
6733 * one server ("boks_sshd") is known to return SUCCESS for channel
6734 * requests it's never heard of, such as "winadj@putty". Raised
6735 * with foxt.com as bug 090916-090424, but for the sake of a quiet
6736 * life, we don't worry about what kind of response we got.
6737 */
6738
6739 c->v.v2.remlocwin += *sizep;
6740 sfree(sizep);
e030765f 6741 /*
6742 * winadj messages are only sent when the window is fully open, so
6743 * if we get an ack of one, we know any pending unthrottle is
6744 * complete.
6745 */
6746 if (c->v.v2.throttle_state == UNTHROTTLING)
6747 c->v.v2.throttle_state = UNTHROTTLED;
e030765f 6748}
6749
39d830e4 6750static void ssh2_msg_channel_response(Ssh ssh, struct Packet *pktin)
837d6ba6 6751{
39d830e4 6752 struct ssh_channel *c = ssh2_channel_msg(ssh, pktin);
6753 struct outstanding_channel_request *ocr;
837d6ba6 6754
39d830e4 6755 if (!c) return;
6756 ocr = c->v.v2.chanreq_head;
6757 if (!ocr) {
6758 ssh2_msg_unexpected(ssh, pktin);
837d6ba6 6759 return;
39d830e4 6760 }
6761 ocr->handler(c, pktin, ocr->ctx);
6762 c->v.v2.chanreq_head = ocr->next;
6763 sfree(ocr);
0e443c99 6764 /*
39d830e4 6765 * We may now initiate channel-closing procedures, if that
6766 * CHANNEL_REQUEST was the last thing outstanding before we send
6767 * CHANNEL_CLOSE.
0e443c99 6768 */
39d830e4 6769 ssh2_channel_check_close(c);
0e443c99 6770}
6771
51df0ab5 6772static void ssh2_msg_channel_window_adjust(Ssh ssh, struct Packet *pktin)
b09eaa88 6773{
b09eaa88 6774 struct ssh_channel *c;
e223b7c7 6775 c = ssh2_channel_msg(ssh, pktin);
6776 if (!c)
6777 return;
bc06669b 6778 if (!(c->closes & CLOSES_SENT_EOF)) {
b09eaa88 6779 c->v.v2.remwindow += ssh_pkt_getuint32(pktin);
78280721 6780 ssh2_try_send_and_unthrottle(ssh, c);
1bfc7e93 6781 }
b09eaa88 6782}
6783
51df0ab5 6784static void ssh2_msg_channel_data(Ssh ssh, struct Packet *pktin)
6785{
6786 char *data;
6d44acc9 6787 int length;
51df0ab5 6788 struct ssh_channel *c;
e223b7c7 6789 c = ssh2_channel_msg(ssh, pktin);
51df0ab5 6790 if (!c)
e223b7c7 6791 return;
51df0ab5 6792 if (pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA &&
6793 ssh_pkt_getuint32(pktin) != SSH2_EXTENDED_DATA_STDERR)
6794 return; /* extended but not stderr */
6795 ssh_pkt_getstring(pktin, &data, &length);
6796 if (data) {
6797 int bufsize = 0;
6798 c->v.v2.locwindow -= length;
0e443c99 6799 c->v.v2.remlocwin -= length;
51df0ab5 6800 switch (c->type) {
6801 case CHAN_MAINSESSION:
6802 bufsize =
6803 from_backend(ssh->frontend, pktin->type ==
6804 SSH2_MSG_CHANNEL_EXTENDED_DATA,
6805 data, length);
6806 break;
6807 case CHAN_X11:
6808 bufsize = x11_send(c->u.x11.s, data, length);
6809 break;
6810 case CHAN_SOCKDATA:
6811 bufsize = pfd_send(c->u.pfd.s, data, length);
6812 break;
6813 case CHAN_AGENT:
6814 while (length > 0) {
6815 if (c->u.a.lensofar < 4) {
62ddb51e 6816 unsigned int l = min(4 - c->u.a.lensofar,
6817 (unsigned)length);
51df0ab5 6818 memcpy(c->u.a.msglen + c->u.a.lensofar,
6819 data, l);
6820 data += l;
6821 length -= l;
6822 c->u.a.lensofar += l;
6823 }
6824 if (c->u.a.lensofar == 4) {
6825 c->u.a.totallen =
6826 4 + GET_32BIT(c->u.a.msglen);
6827 c->u.a.message = snewn(c->u.a.totallen,
6828 unsigned char);
6829 memcpy(c->u.a.message, c->u.a.msglen, 4);
6830 }
6831 if (c->u.a.lensofar >= 4 && length > 0) {
aa63ab7e 6832 unsigned int l =
51df0ab5 6833 min(c->u.a.totallen - c->u.a.lensofar,
62ddb51e 6834 (unsigned)length);
51df0ab5 6835 memcpy(c->u.a.message + c->u.a.lensofar,
6836 data, l);
6837 data += l;
6838 length -= l;
6839 c->u.a.lensofar += l;
6840 }
6841 if (c->u.a.lensofar == c->u.a.totallen) {
6842 void *reply;
6843 int replylen;
6844 if (agent_query(c->u.a.message,
6845 c->u.a.totallen,
6846 &reply, &replylen,
6847 ssh_agentf_callback, c))
6848 ssh_agentf_callback(c, reply, replylen);
6849 sfree(c->u.a.message);
bc06669b 6850 c->u.a.message = NULL;
51df0ab5 6851 c->u.a.lensofar = 0;
6852 }
6853 }
6854 bufsize = 0;
6855 break;
6856 }
6857 /*
0e443c99 6858 * If it looks like the remote end hit the end of its window,
6859 * and we didn't want it to do that, think about using a
6860 * larger window.
6861 */
6862 if (c->v.v2.remlocwin <= 0 && c->v.v2.throttle_state == UNTHROTTLED &&
6863 c->v.v2.locmaxwin < 0x40000000)
6864 c->v.v2.locmaxwin += OUR_V2_WINSIZE;
6865 /*
51df0ab5 6866 * If we are not buffering too much data,
6867 * enlarge the window again at the remote side.
467e064d 6868 * If we are buffering too much, we may still
6869 * need to adjust the window if the server's
6870 * sent excess data.
51df0ab5 6871 */
9b53e9b8 6872 ssh2_set_window(c, bufsize < c->v.v2.locmaxwin ?
6873 c->v.v2.locmaxwin - bufsize : 0);
ff68564b 6874 /*
6875 * If we're either buffering way too much data, or if we're
6876 * buffering anything at all and we're in "simple" mode,
6877 * throttle the whole channel.
6878 */
6879 if ((bufsize > c->v.v2.locmaxwin ||
4a693cfc 6880 (conf_get_int(ssh->conf, CONF_ssh_simple) && bufsize > 0)) &&
ff68564b 6881 !c->throttling_conn) {
6882 c->throttling_conn = 1;
6883 ssh_throttle_conn(ssh, +1);
6884 }
51df0ab5 6885 }
6886}
6887
bc06669b 6888static void ssh_channel_destroy(struct ssh_channel *c)
51df0ab5 6889{
bc06669b 6890 Ssh ssh = c->ssh;
51df0ab5 6891
bc06669b 6892 switch (c->type) {
6893 case CHAN_MAINSESSION:
6894 ssh->mainchan = NULL;
6895 update_specials_menu(ssh->frontend);
6896 break;
6897 case CHAN_X11:
6898 if (c->u.x11.s != NULL)
6899 x11_close(c->u.x11.s);
6900 logevent("Forwarded X11 connection terminated");
6901 break;
6902 case CHAN_AGENT:
6903 sfree(c->u.a.message);
6904 break;
6905 case CHAN_SOCKDATA:
6906 if (c->u.pfd.s != NULL)
6907 pfd_close(c->u.pfd.s);
6908 logevent("Forwarded port closed");
6909 break;
6910 }
6911
6912 del234(ssh->channels, c);
3620f307 6913 if (ssh->version == 2) {
bc06669b 6914 bufchain_clear(&c->v.v2.outbuffer);
3620f307 6915 assert(c->v.v2.chanreq_head == NULL);
6916 }
bc06669b 6917 sfree(c);
6918
6919 /*
6920 * See if that was the last channel left open.
6921 * (This is only our termination condition if we're
6922 * not running in -N mode.)
6923 */
6924 if (ssh->version == 2 &&
6925 !conf_get_int(ssh->conf, CONF_ssh_no_shell) &&
6926 count234(ssh->channels) == 0) {
6927 /*
6928 * We used to send SSH_MSG_DISCONNECT here,
6929 * because I'd believed that _every_ conforming
6930 * SSH-2 connection had to end with a disconnect
6931 * being sent by at least one side; apparently
6932 * I was wrong and it's perfectly OK to
6933 * unceremoniously slam the connection shut
6934 * when you're done, and indeed OpenSSH feels
6935 * this is more polite than sending a
6936 * DISCONNECT. So now we don't.
6937 */
6938 ssh_disconnect(ssh, "All channels closed", NULL, 0, TRUE);
6939 }
6940}
6941
6942static void ssh2_channel_check_close(struct ssh_channel *c)
6943{
6944 Ssh ssh = c->ssh;
6945 struct Packet *pktout;
6946
6947 if ((c->closes & (CLOSES_SENT_EOF | CLOSES_RCVD_EOF | CLOSES_SENT_CLOSE))
39d830e4 6948 == (CLOSES_SENT_EOF | CLOSES_RCVD_EOF) && !c->v.v2.chanreq_head) {
bc06669b 6949 /*
997097db 6950 * We have both sent and received EOF, and we have no
134f5ece 6951 * outstanding channel requests, which means the
997097db 6952 * channel is in final wind-up. But we haven't sent CLOSE, so
6953 * let's do so now.
bc06669b 6954 */
6955 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
6956 ssh2_pkt_adduint32(pktout, c->remoteid);
6957 ssh2_pkt_send(ssh, pktout);
6958 c->closes |= CLOSES_SENT_CLOSE;
6959 }
6960
6961 if (!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes)) {
3620f307 6962 assert(c->v.v2.chanreq_head == NULL);
bc06669b 6963 /*
6964 * We have both sent and received CLOSE, which means we're
6965 * completely done with the channel.
6966 */
6967 ssh_channel_destroy(c);
6968 }
6969}
6970
6971static void ssh2_channel_got_eof(struct ssh_channel *c)
6972{
6973 if (c->closes & CLOSES_RCVD_EOF)
6974 return; /* already seen EOF */
6975 c->closes |= CLOSES_RCVD_EOF;
51df0ab5 6976
6977 if (c->type == CHAN_X11) {
bc06669b 6978 x11_send_eof(c->u.x11.s);
51df0ab5 6979 } else if (c->type == CHAN_AGENT) {
bc06669b 6980 /* Manufacture an outgoing EOF in response to the incoming one. */
6981 sshfwd_write_eof(c);
51df0ab5 6982 } else if (c->type == CHAN_SOCKDATA) {
bc06669b 6983 pfd_send_eof(c->u.pfd.s);
6984 } else if (c->type == CHAN_MAINSESSION) {
6985 Ssh ssh = c->ssh;
6986
4a202fc6 6987 if (!ssh->sent_console_eof &&
6988 (from_backend_eof(ssh->frontend) || ssh->got_pty)) {
bc06669b 6989 /*
4a202fc6 6990 * Either from_backend_eof told us that the front end
6991 * wants us to close the outgoing side of the connection
6992 * as soon as we see EOF from the far end, or else we've
6993 * unilaterally decided to do that because we've allocated
6994 * a remote pty and hence EOF isn't a particularly
6995 * meaningful concept.
bc06669b 6996 */
6997 sshfwd_write_eof(c);
6998 }
6999 ssh->sent_console_eof = TRUE;
51df0ab5 7000 }
bc06669b 7001
7002 ssh2_channel_check_close(c);
7003}
7004
7005static void ssh2_msg_channel_eof(Ssh ssh, struct Packet *pktin)
7006{
7007 struct ssh_channel *c;
7008
7009 c = ssh2_channel_msg(ssh, pktin);
7010 if (!c)
7011 return;
7012 ssh2_channel_got_eof(c);
51df0ab5 7013}
7014
7015static void ssh2_msg_channel_close(Ssh ssh, struct Packet *pktin)
7016{
51df0ab5 7017 struct ssh_channel *c;
51df0ab5 7018
e223b7c7 7019 c = ssh2_channel_msg(ssh, pktin);
7020 if (!c)
51df0ab5 7021 return;
51df0ab5 7022
7023 /*
bc06669b 7024 * When we receive CLOSE on a channel, we assume it comes with an
7025 * implied EOF if we haven't seen EOF yet.
51df0ab5 7026 */
bc06669b 7027 ssh2_channel_got_eof(c);
7028
7029 /*
3effbcf2 7030 * And we also send an outgoing EOF, if we haven't already, on the
7031 * assumption that CLOSE is a pretty forceful announcement that
7032 * the remote side is doing away with the entire channel. (If it
7033 * had wanted to send us EOF and continue receiving data from us,
7034 * it would have just sent CHANNEL_EOF.)
3effbcf2 7035 */
a699eb41 7036 if (!(c->closes & CLOSES_SENT_EOF)) {
7037 /*
7038 * Make sure we don't read any more from whatever our local
7039 * data source is for this channel.
7040 */
7041 switch (c->type) {
7042 case CHAN_MAINSESSION:
7043 ssh->send_ok = 0; /* stop trying to read from stdin */
7044 break;
7045 case CHAN_X11:
7046 x11_override_throttle(c->u.x11.s, 1);
7047 break;
7048 case CHAN_SOCKDATA:
7049 pfd_override_throttle(c->u.pfd.s, 1);
7050 break;
7051 }
7052
7053 /*
7054 * Send outgoing EOF.
7055 */
7539afa2 7056 sshfwd_write_eof(c);
3effbcf2 7057 }
7058
7059 /*
bc06669b 7060 * Now process the actual close.
7061 */
7062 if (!(c->closes & CLOSES_RCVD_CLOSE)) {
7063 c->closes |= CLOSES_RCVD_CLOSE;
7064 ssh2_channel_check_close(c);
51df0ab5 7065 }
7066}
7067
7068static void ssh2_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin)
7069{
51df0ab5 7070 struct ssh_channel *c;
51df0ab5 7071
e223b7c7 7072 c = ssh2_channel_msg(ssh, pktin);
51df0ab5 7073 if (!c)
e223b7c7 7074 return;
51df0ab5 7075 if (c->type != CHAN_SOCKDATA_DORMANT)
7076 return; /* dunno why they're confirming this */
7077 c->remoteid = ssh_pkt_getuint32(pktin);
64d6ff88 7078 c->halfopen = FALSE;
51df0ab5 7079 c->type = CHAN_SOCKDATA;
7080 c->v.v2.remwindow = ssh_pkt_getuint32(pktin);
7081 c->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin);
7082 if (c->u.pfd.s)
7083 pfd_confirm(c->u.pfd.s);
bc06669b 7084 if (c->pending_eof)
7085 ssh_channel_try_eof(c);
51df0ab5 7086}
7087
7088static void ssh2_msg_channel_open_failure(Ssh ssh, struct Packet *pktin)
7089{
7090 static const char *const reasons[] = {
7091 "<unknown reason code>",
7092 "Administratively prohibited",
7093 "Connect failed",
7094 "Unknown channel type",
7095 "Resource shortage",
7096 };
51df0ab5 7097 unsigned reason_code;
7098 char *reason_string;
7099 int reason_length;
51df0ab5 7100 struct ssh_channel *c;
e223b7c7 7101 c = ssh2_channel_msg(ssh, pktin);
51df0ab5 7102 if (!c)
e223b7c7 7103 return;
51df0ab5 7104 if (c->type != CHAN_SOCKDATA_DORMANT)
7105 return; /* dunno why they're failing this */
7106
7107 reason_code = ssh_pkt_getuint32(pktin);
7108 if (reason_code >= lenof(reasons))
7109 reason_code = 0; /* ensure reasons[reason_code] in range */
7110 ssh_pkt_getstring(pktin, &reason_string, &reason_length);
fb983202 7111 logeventf(ssh, "Forwarded connection refused by server: %s [%.*s]",
7112 reasons[reason_code], reason_length, reason_string);
51df0ab5 7113
7114 pfd_close(c->u.pfd.s);
7115
7116 del234(ssh->channels, c);
7117 sfree(c);
7118}
7119
7120static void ssh2_msg_channel_request(Ssh ssh, struct Packet *pktin)
7121{
51df0ab5 7122 char *type;
7123 int typelen, want_reply;
7124 int reply = SSH2_MSG_CHANNEL_FAILURE; /* default */
7125 struct ssh_channel *c;
7126 struct Packet *pktout;
7127
e223b7c7 7128 c = ssh2_channel_msg(ssh, pktin);
7129 if (!c)
7130 return;
51df0ab5 7131 ssh_pkt_getstring(pktin, &type, &typelen);
7132 want_reply = ssh2_pkt_getbool(pktin);
7133
7134 /*
51df0ab5 7135 * Having got the channel number, we now look at
7136 * the request type string to see if it's something
7137 * we recognise.
7138 */
7139 if (c == ssh->mainchan) {
7140 /*
7141 * We recognise "exit-status" and "exit-signal" on
7142 * the primary channel.
7143 */
7144 if (typelen == 11 &&
7145 !memcmp(type, "exit-status", 11)) {
7146
7147 ssh->exitcode = ssh_pkt_getuint32(pktin);
7148 logeventf(ssh, "Server sent command exit status %d",
7149 ssh->exitcode);
7150 reply = SSH2_MSG_CHANNEL_SUCCESS;
7151
7152 } else if (typelen == 11 &&
7153 !memcmp(type, "exit-signal", 11)) {
7154
7155 int is_plausible = TRUE, is_int = FALSE;
7156 char *fmt_sig = "", *fmt_msg = "";
7157 char *msg;
7158 int msglen = 0, core = FALSE;
7159 /* ICK: older versions of OpenSSH (e.g. 3.4p1)
7160 * provide an `int' for the signal, despite its
bccbeb71 7161 * having been a `string' in the drafts of RFC 4254 since at
51df0ab5 7162 * least 2001. (Fixed in session.c 1.147.) Try to
7163 * infer which we can safely parse it as. */
7164 {
7165 unsigned char *p = pktin->body +
7166 pktin->savedpos;
7167 long len = pktin->length - pktin->savedpos;
7168 unsigned long num = GET_32BIT(p); /* what is it? */
7169 /* If it's 0, it hardly matters; assume string */
7170 if (num == 0) {
7171 is_int = FALSE;
7172 } else {
7173 int maybe_int = FALSE, maybe_str = FALSE;
7174#define CHECK_HYPOTHESIS(offset, result) \
7175 do { \
7176 long q = offset; \
7177 if (q >= 0 && q+4 <= len) { \
7178 q = q + 4 + GET_32BIT(p+q); \
7179 if (q >= 0 && q+4 <= len && \
c849ff23 7180 ((q = q + 4 + GET_32BIT(p+q))!= 0) && q == len) \
51df0ab5 7181 result = TRUE; \
7182 } \
7183 } while(0)
7184 CHECK_HYPOTHESIS(4+1, maybe_int);
7185 CHECK_HYPOTHESIS(4+num+1, maybe_str);
7186#undef CHECK_HYPOTHESIS
7187 if (maybe_int && !maybe_str)
7188 is_int = TRUE;
7189 else if (!maybe_int && maybe_str)
7190 is_int = FALSE;
7191 else
7192 /* Crikey. Either or neither. Panic. */
7193 is_plausible = FALSE;
7194 }
7195 }
d051d1b4 7196 ssh->exitcode = 128; /* means `unknown signal' */
51df0ab5 7197 if (is_plausible) {
7198 if (is_int) {
7199 /* Old non-standard OpenSSH. */
7200 int signum = ssh_pkt_getuint32(pktin);
7201 fmt_sig = dupprintf(" %d", signum);
d051d1b4 7202 ssh->exitcode = 128 + signum;
51df0ab5 7203 } else {
bccbeb71 7204 /* As per RFC 4254. */
51df0ab5 7205 char *sig;
7206 int siglen;
7207 ssh_pkt_getstring(pktin, &sig, &siglen);
7208 /* Signal name isn't supposed to be blank, but
7209 * let's cope gracefully if it is. */
7210 if (siglen) {
7211 fmt_sig = dupprintf(" \"%.*s\"",
7212 siglen, sig);
7213 }
d051d1b4 7214
7215 /*
7216 * Really hideous method of translating the
7217 * signal description back into a locally
7218 * meaningful number.
7219 */
7220
7221 if (0)
7222 ;
b707973a 7223#define TRANSLATE_SIGNAL(s) \
7224 else if (siglen == lenof(#s)-1 && !memcmp(sig, #s, siglen)) \
7225 ssh->exitcode = 128 + SIG ## s
d051d1b4 7226#ifdef SIGABRT
b707973a 7227 TRANSLATE_SIGNAL(ABRT);
d051d1b4 7228#endif
7229#ifdef SIGALRM
b707973a 7230 TRANSLATE_SIGNAL(ALRM);
d051d1b4 7231#endif
7232#ifdef SIGFPE
b707973a 7233 TRANSLATE_SIGNAL(FPE);
d051d1b4 7234#endif
7235#ifdef SIGHUP
b707973a 7236 TRANSLATE_SIGNAL(HUP);
d051d1b4 7237#endif
7238#ifdef SIGILL
b707973a 7239 TRANSLATE_SIGNAL(ILL);
d051d1b4 7240#endif
7241#ifdef SIGINT
b707973a 7242 TRANSLATE_SIGNAL(INT);
d051d1b4 7243#endif
7244#ifdef SIGKILL
b707973a 7245 TRANSLATE_SIGNAL(KILL);
d051d1b4 7246#endif
7247#ifdef SIGPIPE
b707973a 7248 TRANSLATE_SIGNAL(PIPE);
d051d1b4 7249#endif
7250#ifdef SIGQUIT
b707973a 7251 TRANSLATE_SIGNAL(QUIT);
d051d1b4 7252#endif
7253#ifdef SIGSEGV
b707973a 7254 TRANSLATE_SIGNAL(SEGV);
d051d1b4 7255#endif
7256#ifdef SIGTERM
b707973a 7257 TRANSLATE_SIGNAL(TERM);
d051d1b4 7258#endif
7259#ifdef SIGUSR1
b707973a 7260 TRANSLATE_SIGNAL(USR1);
d051d1b4 7261#endif
7262#ifdef SIGUSR2
b707973a 7263 TRANSLATE_SIGNAL(USR2);
d051d1b4 7264#endif
b707973a 7265#undef TRANSLATE_SIGNAL
d051d1b4 7266 else
7267 ssh->exitcode = 128;
51df0ab5 7268 }
7269 core = ssh2_pkt_getbool(pktin);
7270 ssh_pkt_getstring(pktin, &msg, &msglen);
7271 if (msglen) {
7272 fmt_msg = dupprintf(" (\"%.*s\")", msglen, msg);
7273 }
7274 /* ignore lang tag */
7275 } /* else don't attempt to parse */
7276 logeventf(ssh, "Server exited on signal%s%s%s",
7277 fmt_sig, core ? " (core dumped)" : "",
7278 fmt_msg);
7279 if (*fmt_sig) sfree(fmt_sig);
7280 if (*fmt_msg) sfree(fmt_msg);
7281 reply = SSH2_MSG_CHANNEL_SUCCESS;
7282
7283 }
7284 } else {
7285 /*
7286 * This is a channel request we don't know
7287 * about, so we now either ignore the request
7288 * or respond with CHANNEL_FAILURE, depending
7289 * on want_reply.
7290 */
7291 reply = SSH2_MSG_CHANNEL_FAILURE;
7292 }
7293 if (want_reply) {
7294 pktout = ssh2_pkt_init(reply);
7295 ssh2_pkt_adduint32(pktout, c->remoteid);
7296 ssh2_pkt_send(ssh, pktout);
7297 }
7298}
7299
7300static void ssh2_msg_global_request(Ssh ssh, struct Packet *pktin)
7301{
7302 char *type;
7303 int typelen, want_reply;
7304 struct Packet *pktout;
7305
7306 ssh_pkt_getstring(pktin, &type, &typelen);
7307 want_reply = ssh2_pkt_getbool(pktin);
7308
7309 /*
7310 * We currently don't support any global requests
7311 * at all, so we either ignore the request or
7312 * respond with REQUEST_FAILURE, depending on
7313 * want_reply.
7314 */
7315 if (want_reply) {
7316 pktout = ssh2_pkt_init(SSH2_MSG_REQUEST_FAILURE);
7317 ssh2_pkt_send(ssh, pktout);
7318 }
7319}
7320
7321static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
7322{
7323 char *type;
7324 int typelen;
7325 char *peeraddr;
7326 int peeraddrlen;
7327 int peerport;
7328 char *error = NULL;
7329 struct ssh_channel *c;
7330 unsigned remid, winsize, pktsize;
7331 struct Packet *pktout;
7332
7333 ssh_pkt_getstring(pktin, &type, &typelen);
7334 c = snew(struct ssh_channel);
7335 c->ssh = ssh;
7336
7337 remid = ssh_pkt_getuint32(pktin);
7338 winsize = ssh_pkt_getuint32(pktin);
7339 pktsize = ssh_pkt_getuint32(pktin);
7340
7341 if (typelen == 3 && !memcmp(type, "x11", 3)) {
7342 char *addrstr;
5907ebda 7343 const char *x11err;
51df0ab5 7344
7345 ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen);
7346 addrstr = snewn(peeraddrlen+1, char);
7347 memcpy(addrstr, peeraddr, peeraddrlen);
7348 addrstr[peeraddrlen] = '\0';
7349 peerport = ssh_pkt_getuint32(pktin);
7350
7351 logeventf(ssh, "Received X11 connect request from %s:%d",
7352 addrstr, peerport);
7353
7354 if (!ssh->X11_fwd_enabled)
7355 error = "X11 forwarding is not enabled";
5907ebda 7356 else if ((x11err = x11_init(&c->u.x11.s, ssh->x11disp, c,
4a693cfc 7357 addrstr, peerport, ssh->conf)) != NULL) {
5907ebda 7358 logeventf(ssh, "Local X11 connection failed: %s", x11err);
51df0ab5 7359 error = "Unable to open an X11 connection";
7360 } else {
7361 logevent("Opening X11 forward connection succeeded");
7362 c->type = CHAN_X11;
7363 }
7364
7365 sfree(addrstr);
7366 } else if (typelen == 15 &&
7367 !memcmp(type, "forwarded-tcpip", 15)) {
7368 struct ssh_rportfwd pf, *realpf;
7369 char *dummy;
7370 int dummylen;
7371 ssh_pkt_getstring(pktin, &dummy, &dummylen);/* skip address */
7372 pf.sport = ssh_pkt_getuint32(pktin);
7373 ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen);
7374 peerport = ssh_pkt_getuint32(pktin);
7375 realpf = find234(ssh->rportfwds, &pf, NULL);
7376 logeventf(ssh, "Received remote port %d open request "
7377 "from %s:%d", pf.sport, peeraddr, peerport);
7378 if (realpf == NULL) {
7379 error = "Remote port is not recognised";
7380 } else {
7381 const char *e = pfd_newconnect(&c->u.pfd.s,
7382 realpf->dhost,
7383 realpf->dport, c,
4a693cfc 7384 ssh->conf,
05581745 7385 realpf->pfrec->addressfamily);
51df0ab5 7386 logeventf(ssh, "Attempting to forward remote port to "
7387 "%s:%d", realpf->dhost, realpf->dport);
7388 if (e != NULL) {
7389 logeventf(ssh, "Port open failed: %s", e);
7390 error = "Port open failed";
7391 } else {
7392 logevent("Forwarded port opened successfully");
7393 c->type = CHAN_SOCKDATA;
7394 }
7395 }
7396 } else if (typelen == 22 &&
c49cf994 7397 !memcmp(type, "auth-agent@openssh.com", 22)) {
51df0ab5 7398 if (!ssh->agentfwd_enabled)
7399 error = "Agent forwarding is not enabled";
7400 else {
7401 c->type = CHAN_AGENT; /* identify channel type */
7402 c->u.a.lensofar = 0;
7403 }
7404 } else {
7405 error = "Unsupported channel type requested";
7406 }
7407
7408 c->remoteid = remid;
64d6ff88 7409 c->halfopen = FALSE;
51df0ab5 7410 if (error) {
7411 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE);
7412 ssh2_pkt_adduint32(pktout, c->remoteid);
7413 ssh2_pkt_adduint32(pktout, SSH2_OPEN_CONNECT_FAILED);
7414 ssh2_pkt_addstring(pktout, error);
7415 ssh2_pkt_addstring(pktout, "en"); /* language tag */
7416 ssh2_pkt_send(ssh, pktout);
7417 logeventf(ssh, "Rejected channel open: %s", error);
7418 sfree(c);
7419 } else {
94bdfe40 7420 ssh2_channel_init(c);
51df0ab5 7421 c->v.v2.remwindow = winsize;
7422 c->v.v2.remmaxpkt = pktsize;
51df0ab5 7423 add234(ssh->channels, c);
7424 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
7425 ssh2_pkt_adduint32(pktout, c->remoteid);
7426 ssh2_pkt_adduint32(pktout, c->localid);
7427 ssh2_pkt_adduint32(pktout, c->v.v2.locwindow);
954d5c5a 7428 ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */
51df0ab5 7429 ssh2_pkt_send(ssh, pktout);
7430 }
7431}
7432
6bbce591 7433/*
adb6167a 7434 * Buffer banner messages for later display at some convenient point,
7435 * if we're going to display them.
6bbce591 7436 */
7437static void ssh2_msg_userauth_banner(Ssh ssh, struct Packet *pktin)
7438{
7439 /* Arbitrary limit to prevent unbounded inflation of buffer */
4a693cfc 7440 if (conf_get_int(ssh->conf, CONF_ssh_show_banner) &&
adb6167a 7441 bufchain_size(&ssh->banner) <= 131072) {
6bbce591 7442 char *banner = NULL;
7443 int size = 0;
7444 ssh_pkt_getstring(pktin, &banner, &size);
7445 if (banner)
7446 bufchain_add(&ssh->banner, banner, size);
7447 }
7448}
7449
c6ccd5c2 7450/* Helper function to deal with sending tty modes for "pty-req" */
7451static void ssh2_send_ttymode(void *data, char *mode, char *val)
7452{
7453 struct Packet *pktout = (struct Packet *)data;
7454 int i = 0;
7455 unsigned int arg = 0;
7456 while (strcmp(mode, ssh_ttymodes[i].mode) != 0) i++;
7457 if (i == lenof(ssh_ttymodes)) return;
7458 switch (ssh_ttymodes[i].type) {
7459 case TTY_OP_CHAR:
7460 arg = ssh_tty_parse_specchar(val);
7461 break;
7462 case TTY_OP_BOOL:
7463 arg = ssh_tty_parse_boolean(val);
7464 break;
7465 }
7466 ssh2_pkt_addbyte(pktout, ssh_ttymodes[i].opcode);
7467 ssh2_pkt_adduint32(pktout, arg);
7468}
7469
ebbc254d 7470static void ssh2_maybe_setup_x11(struct ssh_channel *c, struct Packet *pktin,
7471 void *ctx)
7472{
7473 struct ssh2_maybe_setup_x11_state {
7474 int crLine;
7475 };
7476 Ssh ssh = c->ssh;
7477 struct Packet *pktout;
7478 crStateP(ssh2_maybe_setup_x11_state, ctx);
7479
7480 crBeginState;
7481
7482 /*
7483 * Potentially enable X11 forwarding.
7484 */
7485 if (ssh->mainchan && !ssh->ncmode && conf_get_int(ssh->conf, CONF_x11_forward) &&
7486 (ssh->x11disp = x11_setup_display(conf_get_str(ssh->conf, CONF_x11_display),
7487 conf_get_int(ssh->conf, CONF_x11_auth), ssh->conf))) {
7488 logevent("Requesting X11 forwarding");
134f5ece 7489 pktout = ssh2_chanreq_init(ssh->mainchan, "x11-req",
7490 ssh2_maybe_setup_x11, s);
ebbc254d 7491 ssh2_pkt_addbool(pktout, 0); /* many connections */
7492 ssh2_pkt_addstring(pktout, ssh->x11disp->remoteauthprotoname);
7493 /*
7494 * Note that while we blank the X authentication data here, we don't
7495 * take any special action to blank the start of an X11 channel,
7496 * so using MIT-MAGIC-COOKIE-1 and actually opening an X connection
7497 * without having session blanking enabled is likely to leak your
7498 * cookie into the log.
7499 */
7500 dont_log_password(ssh, pktout, PKTLOG_BLANK);
7501 ssh2_pkt_addstring(pktout, ssh->x11disp->remoteauthdatastring);
7502 end_log_omission(ssh, pktout);
7503 ssh2_pkt_adduint32(pktout, ssh->x11disp->screennum);
7504 ssh2_pkt_send(ssh, pktout);
7505
ebbc254d 7506 crWaitUntilV(pktin);
7507
3620f307 7508 if (pktin) {
7509 if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) {
7510 logevent("X11 forwarding enabled");
7511 ssh->X11_fwd_enabled = TRUE;
7512 } else
7513 logevent("X11 forwarding refused");
7514 }
ebbc254d 7515 }
7516 sfree(s);
7517 crFinishV;
7518}
7519
7520static void ssh2_maybe_setup_agent(struct ssh_channel *c, struct Packet *pktin,
7521 void *ctx)
7522{
7523 struct ssh2_maybe_setup_agent_state {
7524 int crLine;
7525 };
7526 Ssh ssh = c->ssh;
7527 struct Packet *pktout;
7528 crStateP(ssh2_maybe_setup_agent_state, ctx);
7529
7530 crBeginState;
7531
7532 if (ssh->mainchan && !ssh->ncmode && conf_get_int(ssh->conf, CONF_agentfwd) && agent_exists()) {
7533 logevent("Requesting OpenSSH-style agent forwarding");
134f5ece 7534 pktout = ssh2_chanreq_init(ssh->mainchan, "auth-agent-req@openssh.com",
7535 ssh2_maybe_setup_agent, s);
ebbc254d 7536 ssh2_pkt_send(ssh, pktout);
7537
ebbc254d 7538 crWaitUntilV(pktin);
7539
3620f307 7540 if (pktin) {
7541 if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) {
7542 logevent("Agent forwarding enabled");
7543 ssh->agentfwd_enabled = TRUE;
7544 } else
7545 logevent("Agent forwarding refused");
7546 }
ebbc254d 7547 }
7548 sfree(s);
7549 crFinishV;
7550}
7551
7552static void ssh2_maybe_setup_pty(struct ssh_channel *c, struct Packet *pktin,
7553 void *ctx)
7554{
7555 struct ssh2_maybe_setup_pty_state {
7556 int crLine;
7557 };
7558 Ssh ssh = c->ssh;
7559 struct Packet *pktout;
7560 crStateP(ssh2_maybe_setup_pty_state, ctx);
7561
7562 crBeginState;
7563
7564 if (ssh->mainchan && !ssh->ncmode && !conf_get_int(ssh->conf, CONF_nopty)) {
7565 /* Unpick the terminal-speed string. */
7566 /* XXX perhaps we should allow no speeds to be sent. */
7567 ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */
7568 sscanf(conf_get_str(ssh->conf, CONF_termspeed), "%d,%d", &ssh->ospeed, &ssh->ispeed);
7569 /* Build the pty request. */
134f5ece 7570 pktout = ssh2_chanreq_init(ssh->mainchan, "pty-req",
7571 ssh2_maybe_setup_pty, s);
ebbc254d 7572 ssh2_pkt_addstring(pktout, conf_get_str(ssh->conf, CONF_termtype));
7573 ssh2_pkt_adduint32(pktout, ssh->term_width);
7574 ssh2_pkt_adduint32(pktout, ssh->term_height);
7575 ssh2_pkt_adduint32(pktout, 0); /* pixel width */
7576 ssh2_pkt_adduint32(pktout, 0); /* pixel height */
7577 ssh2_pkt_addstring_start(pktout);
7578 parse_ttymodes(ssh, ssh2_send_ttymode, (void *)pktout);
7579 ssh2_pkt_addbyte(pktout, SSH2_TTY_OP_ISPEED);
7580 ssh2_pkt_adduint32(pktout, ssh->ispeed);
7581 ssh2_pkt_addbyte(pktout, SSH2_TTY_OP_OSPEED);
7582 ssh2_pkt_adduint32(pktout, ssh->ospeed);
7583 ssh2_pkt_addstring_data(pktout, "\0", 1); /* TTY_OP_END */
7584 ssh2_pkt_send(ssh, pktout);
7585 ssh->state = SSH_STATE_INTERMED;
7586
ebbc254d 7587 crWaitUntilV(pktin);
7588
3620f307 7589 if (pktin) {
7590 if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) {
7591 logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)",
7592 ssh->ospeed, ssh->ispeed);
7593 ssh->got_pty = TRUE;
7594 } else {
7595 c_write_str(ssh, "Server refused to allocate pty\r\n");
7596 ssh->editing = ssh->echoing = 1;
7597 }
ebbc254d 7598 }
7599 } else {
7600 ssh->editing = ssh->echoing = 1;
7601 }
7602 sfree(s);
7603 crFinishV;
7604}
7605
7606static void ssh2_setup_env(struct ssh_channel *c, struct Packet *pktin,
7607 void *ctx)
7608{
7609 struct ssh2_setup_env_state {
7610 int crLine;
7611 int num_env, env_left, env_ok;
7612 };
7613 Ssh ssh = c->ssh;
7614 struct Packet *pktout;
7615 crStateP(ssh2_setup_env_state, ctx);
7616
7617 crBeginState;
7618
7619 /*
7620 * Send environment variables.
7621 *
7622 * Simplest thing here is to send all the requests at once, and
7623 * then wait for a whole bunch of successes or failures.
7624 */
7625 s->num_env = 0;
7626 if (ssh->mainchan && !ssh->ncmode) {
7627 char *key, *val;
7628
7629 for (val = conf_get_str_strs(ssh->conf, CONF_environmt, NULL, &key);
7630 val != NULL;
7631 val = conf_get_str_strs(ssh->conf, CONF_environmt, key, &key)) {
134f5ece 7632 pktout = ssh2_chanreq_init(ssh->mainchan, "env", ssh2_setup_env, s);
ebbc254d 7633 ssh2_pkt_addstring(pktout, key);
7634 ssh2_pkt_addstring(pktout, val);
7635 ssh2_pkt_send(ssh, pktout);
ebbc254d 7636
7637 s->num_env++;
7638 }
7639 if (s->num_env)
7640 logeventf(ssh, "Sent %d environment variables", s->num_env);
7641 }
7642
7643 if (s->num_env) {
7644 s->env_ok = 0;
7645 s->env_left = s->num_env;
7646
7647 while (s->env_left > 0) {
7648 crWaitUntilV(pktin);
3620f307 7649 if (!pktin) goto out;
c330b1cc 7650 if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS)
ebbc254d 7651 s->env_ok++;
ebbc254d 7652 s->env_left--;
7653 }
7654
7655 if (s->env_ok == s->num_env) {
7656 logevent("All environment variables successfully set");
7657 } else if (s->env_ok == 0) {
7658 logevent("All environment variables refused");
7659 c_write_str(ssh, "Server refused to set environment variables\r\n");
7660 } else {
7661 logeventf(ssh, "%d environment variables refused",
7662 s->num_env - s->env_ok);
7663 c_write_str(ssh, "Server refused to set all environment variables\r\n");
7664 }
7665 }
3620f307 7666out:
ebbc254d 7667 sfree(s);
7668 crFinishV;
7669}
7670
783415f8 7671/*
2e85c969 7672 * Handle the SSH-2 userauth and connection layers.
7cca0d81 7673 */
bc074147 7674static void ssh2_msg_authconn(Ssh ssh, struct Packet *pktin)
7675{
7676 do_ssh2_authconn(ssh, NULL, 0, pktin);
7677}
7678
ebbc254d 7679static void ssh2_response_authconn(struct ssh_channel *c, struct Packet *pktin,
7680 void *ctx)
7681{
7682 do_ssh2_authconn(c->ssh, NULL, 0, pktin);
7683}
7684
ff3187f6 7685static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
7686 struct Packet *pktin)
7cca0d81 7687{
51470298 7688 struct do_ssh2_authconn_state {
30919997 7689 int crLine;
51470298 7690 enum {
51470298 7691 AUTH_TYPE_NONE,
7692 AUTH_TYPE_PUBLICKEY,
7693 AUTH_TYPE_PUBLICKEY_OFFER_LOUD,
7694 AUTH_TYPE_PUBLICKEY_OFFER_QUIET,
7695 AUTH_TYPE_PASSWORD,
8221906d 7696 AUTH_TYPE_GSSAPI, /* always QUIET */
51470298 7697 AUTH_TYPE_KEYBOARD_INTERACTIVE,
7698 AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
7699 } type;
a1a1fae4 7700 int done_service_req;
51470298 7701 int gotit, need_pw, can_pubkey, can_passwd, can_keyb_inter;
94cd7c3a 7702 int tried_pubkey_config, done_agent;
bf84a03a 7703#ifndef NO_GSSAPI
42af6a67 7704 int can_gssapi;
7705 int tried_gssapi;
bf84a03a 7706#endif
edd0cb8a 7707 int kbd_inter_refused;
ab18ccff 7708 int we_are_in, userauth_success;
edd0cb8a 7709 prompts_t *cur_prompt;
7710 int num_prompts;
4a693cfc 7711 char *username;
e955d348 7712 char *password;
51470298 7713 int got_username;
51470298 7714 void *publickey_blob;
7715 int publickey_bloblen;
edd0cb8a 7716 int publickey_encrypted;
7717 char *publickey_algorithm;
7718 char *publickey_comment;
94cd7c3a 7719 unsigned char agent_request[5], *agent_response, *agentp;
7720 int agent_responselen;
7721 unsigned char *pkblob_in_agent;
51470298 7722 int keyi, nkeys;
51470298 7723 char *pkblob, *alg, *commentp;
7724 int pklen, alglen, commentlen;
7725 int siglen, retlen, len;
7726 char *q, *agentreq, *ret;
7727 int try_send;
ff3187f6 7728 struct Packet *pktout;
4a693cfc 7729 Filename *keyfile;
bf84a03a 7730#ifndef NO_GSSAPI
b3d375b2 7731 struct ssh_gss_library *gsslib;
42af6a67 7732 Ssh_gss_ctx gss_ctx;
7733 Ssh_gss_buf gss_buf;
7734 Ssh_gss_buf gss_rcvtok, gss_sndtok;
7735 Ssh_gss_name gss_srv_name;
7736 Ssh_gss_stat gss_stat;
bf84a03a 7737#endif
51470298 7738 };
7739 crState(do_ssh2_authconn_state);
7740
30919997 7741 crBeginState;
e5574168 7742
bc074147 7743 /* Register as a handler for all the messages this coroutine handles. */
7744 ssh->packet_dispatch[SSH2_MSG_SERVICE_ACCEPT] = ssh2_msg_authconn;
7745 ssh->packet_dispatch[SSH2_MSG_USERAUTH_REQUEST] = ssh2_msg_authconn;
7746 ssh->packet_dispatch[SSH2_MSG_USERAUTH_FAILURE] = ssh2_msg_authconn;
7747 ssh->packet_dispatch[SSH2_MSG_USERAUTH_SUCCESS] = ssh2_msg_authconn;
7748 ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = ssh2_msg_authconn;
7749 ssh->packet_dispatch[SSH2_MSG_USERAUTH_PK_OK] = ssh2_msg_authconn;
7750 /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ] = ssh2_msg_authconn; duplicate case value */
7751 /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_REQUEST] = ssh2_msg_authconn; duplicate case value */
7752 ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_RESPONSE] = ssh2_msg_authconn;
7753 ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = ssh2_msg_authconn;
7754 ssh->packet_dispatch[SSH2_MSG_REQUEST_SUCCESS] = ssh2_msg_authconn;
7755 ssh->packet_dispatch[SSH2_MSG_REQUEST_FAILURE] = ssh2_msg_authconn;
7756 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = ssh2_msg_authconn;
7757 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = ssh2_msg_authconn;
7758 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = ssh2_msg_authconn;
7759 ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = ssh2_msg_authconn;
7760 ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = ssh2_msg_authconn;
7761 ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = ssh2_msg_authconn;
7762 ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_authconn;
7763 ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_authconn;
bc074147 7764
a1a1fae4 7765 s->done_service_req = FALSE;
ab18ccff 7766 s->we_are_in = s->userauth_success = FALSE;
bf84a03a 7767#ifndef NO_GSSAPI
42af6a67 7768 s->tried_gssapi = FALSE;
bf84a03a 7769#endif
42af6a67 7770
4a693cfc 7771 if (!conf_get_int(ssh->conf, CONF_ssh_no_userauth)) {
a1a1fae4 7772 /*
7773 * Request userauth protocol, and await a response to it.
7774 */
7775 s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST);
7776 ssh2_pkt_addstring(s->pktout, "ssh-userauth");
7777 ssh2_pkt_send(ssh, s->pktout);
7778 crWaitUntilV(pktin);
7779 if (pktin->type == SSH2_MSG_SERVICE_ACCEPT)
7780 s->done_service_req = TRUE;
7781 }
7782 if (!s->done_service_req) {
7783 /*
7784 * Request connection protocol directly, without authentication.
7785 */
7786 s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST);
7787 ssh2_pkt_addstring(s->pktout, "ssh-connection");
7788 ssh2_pkt_send(ssh, s->pktout);
7789 crWaitUntilV(pktin);
7790 if (pktin->type == SSH2_MSG_SERVICE_ACCEPT) {
7791 s->we_are_in = TRUE; /* no auth required */
7792 } else {
7793 bombout(("Server refused service request"));
7794 crStopV;
7795 }
8d5de777 7796 }
7cca0d81 7797
94cd7c3a 7798 /* Arrange to be able to deal with any BANNERs that come in.
7799 * (We do this now as packets may come in during the next bit.) */
7800 bufchain_init(&ssh->banner);
7801 ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] =
7802 ssh2_msg_userauth_banner;
7803
7cca0d81 7804 /*
edd0cb8a 7805 * Misc one-time setup for authentication.
7806 */
7807 s->publickey_blob = NULL;
7808 if (!s->we_are_in) {
7809
7810 /*
7811 * Load the public half of any configured public key file
7812 * for later use.
7813 */
4a693cfc 7814 s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
962468d4 7815 if (!filename_is_null(s->keyfile)) {
edd0cb8a 7816 int keytype;
7817 logeventf(ssh, "Reading private key file \"%.150s\"",
4a693cfc 7818 filename_to_str(s->keyfile));
7819 keytype = key_type(s->keyfile);
edd0cb8a 7820 if (keytype == SSH_KEYTYPE_SSH2) {
7821 const char *error;
7822 s->publickey_blob =
4a693cfc 7823 ssh2_userkey_loadpub(s->keyfile,
edd0cb8a 7824 &s->publickey_algorithm,
7825 &s->publickey_bloblen,
7826 &s->publickey_comment, &error);
7827 if (s->publickey_blob) {
7828 s->publickey_encrypted =
4a693cfc 7829 ssh2_userkey_encrypted(s->keyfile, NULL);
edd0cb8a 7830 } else {
7831 char *msgbuf;
7832 logeventf(ssh, "Unable to load private key (%s)",
7833 error);
7834 msgbuf = dupprintf("Unable to load private key file "
7835 "\"%.150s\" (%s)\r\n",
4a693cfc 7836 filename_to_str(s->keyfile),
edd0cb8a 7837 error);
7838 c_write_str(ssh, msgbuf);
7839 sfree(msgbuf);
7840 }
7841 } else {
7842 char *msgbuf;
7843 logeventf(ssh, "Unable to use this key file (%s)",
7844 key_type_to_str(keytype));
7845 msgbuf = dupprintf("Unable to use key file \"%.150s\""
7846 " (%s)\r\n",
4a693cfc 7847 filename_to_str(s->keyfile),
edd0cb8a 7848 key_type_to_str(keytype));
7849 c_write_str(ssh, msgbuf);
7850 sfree(msgbuf);
7851 s->publickey_blob = NULL;
7852 }
7853 }
7854
94cd7c3a 7855 /*
7856 * Find out about any keys Pageant has (but if there's a
7857 * public key configured, filter out all others).
7858 */
7859 s->nkeys = 0;
7860 s->agent_response = NULL;
7861 s->pkblob_in_agent = NULL;
4a693cfc 7862 if (conf_get_int(ssh->conf, CONF_tryagent) && agent_exists()) {
94cd7c3a 7863
7864 void *r;
7865
7866 logevent("Pageant is running. Requesting keys.");
7867
7868 /* Request the keys held by the agent. */
7869 PUT_32BIT(s->agent_request, 1);
7870 s->agent_request[4] = SSH2_AGENTC_REQUEST_IDENTITIES;
7871 if (!agent_query(s->agent_request, 5, &r, &s->agent_responselen,
7872 ssh_agent_callback, ssh)) {
7873 do {
7874 crReturnV;
7875 if (pktin) {
7876 bombout(("Unexpected data from server while"
7877 " waiting for agent response"));
7878 crStopV;
7879 }
7880 } while (pktin || inlen > 0);
7881 r = ssh->agent_response;
7882 s->agent_responselen = ssh->agent_response_len;
7883 }
7884 s->agent_response = (unsigned char *) r;
7885 if (s->agent_response && s->agent_responselen >= 5 &&
7886 s->agent_response[4] == SSH2_AGENT_IDENTITIES_ANSWER) {
7887 int keyi;
7888 unsigned char *p;
7889 p = s->agent_response + 5;
7890 s->nkeys = GET_32BIT(p);
7891 p += 4;
7892 logeventf(ssh, "Pageant has %d SSH-2 keys", s->nkeys);
7893 if (s->publickey_blob) {
7894 /* See if configured key is in agent. */
7895 for (keyi = 0; keyi < s->nkeys; keyi++) {
7896 s->pklen = GET_32BIT(p);
7897 if (s->pklen == s->publickey_bloblen &&
7898 !memcmp(p+4, s->publickey_blob,
7899 s->publickey_bloblen)) {
7900 logeventf(ssh, "Pageant key #%d matches "
7901 "configured key file", keyi);
7902 s->keyi = keyi;
7903 s->pkblob_in_agent = p;
7904 break;
7905 }
7906 p += 4 + s->pklen;
7907 p += GET_32BIT(p) + 4; /* comment */
7908 }
7909 if (!s->pkblob_in_agent) {
7910 logevent("Configured key file not in Pageant");
7911 s->nkeys = 0;
7912 }
7913 }
5ff99b8e 7914 } else {
7915 logevent("Failed to get reply from Pageant");
94cd7c3a 7916 }
7917 }
7918
edd0cb8a 7919 }
7920
7921 /*
1408a877 7922 * We repeat this whole loop, including the username prompt,
7923 * until we manage a successful authentication. If the user
51470298 7924 * types the wrong _password_, they can be sent back to the
7925 * beginning to try another username, if this is configured on.
7926 * (If they specify a username in the config, they are never
7927 * asked, even if they do give a wrong password.)
1408a877 7928 *
7929 * I think this best serves the needs of
7930 *
7931 * - the people who have no configuration, no keys, and just
7932 * want to try repeated (username,password) pairs until they
7933 * type both correctly
7934 *
7935 * - people who have keys and configuration but occasionally
7936 * need to fall back to passwords
7937 *
7938 * - people with a key held in Pageant, who might not have
7939 * logged in to a particular machine before; so they want to
7940 * type a username, and then _either_ their key will be
7941 * accepted, _or_ they will type a password. If they mistype
7942 * the username they will want to be able to get back and
7943 * retype it!
7cca0d81 7944 */
51470298 7945 s->got_username = FALSE;
a1a1fae4 7946 while (!s->we_are_in) {
1408a877 7947 /*
7948 * Get a username.
7949 */
4a693cfc 7950 if (s->got_username && !conf_get_int(ssh->conf, CONF_change_username)) {
5bb641e1 7951 /*
7952 * We got a username last time round this loop, and
7953 * with change_username turned off we don't try to get
7954 * it again.
7955 */
4a693cfc 7956 } else if ((ssh->username = get_remote_username(ssh->conf)) == NULL) {
edd0cb8a 7957 int ret; /* need not be kept over crReturn */
7958 s->cur_prompt = new_prompts(ssh->frontend);
7959 s->cur_prompt->to_server = TRUE;
7960 s->cur_prompt->name = dupstr("SSH login name");
b61f81bc 7961 add_prompt(s->cur_prompt, dupstr("login as: "), TRUE);
edd0cb8a 7962 ret = get_userpass_input(s->cur_prompt, NULL, 0);
7963 while (ret < 0) {
51470298 7964 ssh->send_ok = 1;
edd0cb8a 7965 crWaitUntilV(!pktin);
7966 ret = get_userpass_input(s->cur_prompt, in, inlen);
7967 ssh->send_ok = 0;
32874aea 7968 }
edd0cb8a 7969 if (!ret) {
7970 /*
7971 * get_userpass_input() failed to get a username.
7972 * Terminate.
7973 */
7974 free_prompts(s->cur_prompt);
7975 ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE);
7976 crStopV;
7977 }
4a693cfc 7978 ssh->username = dupstr(s->cur_prompt->prompts[0]->result);
edd0cb8a 7979 free_prompts(s->cur_prompt);
7cca0d81 7980 } else {
57356d63 7981 char *stuff;
65a22376 7982 if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
4a693cfc 7983 stuff = dupprintf("Using username \"%s\".\r\n", ssh->username);
51470298 7984 c_write_str(ssh, stuff);
57356d63 7985 sfree(stuff);
7cca0d81 7986 }
7987 }
51470298 7988 s->got_username = TRUE;
7cca0d81 7989
65a22376 7990 /*
1408a877 7991 * Send an authentication request using method "none": (a)
7992 * just in case it succeeds, and (b) so that we know what
7993 * authentication methods we can usefully try next.
65a22376 7994 */
acab36bc 7995 ssh->pkt_actx = SSH2_PKTCTX_NOAUTH;
51470298 7996
ff3187f6 7997 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 7998 ssh2_pkt_addstring(s->pktout, ssh->username);
ff3187f6 7999 ssh2_pkt_addstring(s->pktout, "ssh-connection");/* service requested */
8000 ssh2_pkt_addstring(s->pktout, "none"); /* method */
8001 ssh2_pkt_send(ssh, s->pktout);
51470298 8002 s->type = AUTH_TYPE_NONE;
8003 s->gotit = FALSE;
8004 s->we_are_in = FALSE;
8005
8006 s->tried_pubkey_config = FALSE;
6ac3a551 8007 s->kbd_inter_refused = FALSE;
65a22376 8008
94cd7c3a 8009 /* Reset agent request state. */
8010 s->done_agent = FALSE;
8011 if (s->agent_response) {
8012 if (s->pkblob_in_agent) {
8013 s->agentp = s->pkblob_in_agent;
8014 } else {
8015 s->agentp = s->agent_response + 5 + 4;
8016 s->keyi = 0;
8017 }
8018 }
8019
1408a877 8020 while (1) {
854caca5 8021 char *methods = NULL;
8022 int methlen = 0;
8023
1408a877 8024 /*
8025 * Wait for the result of the last authentication request.
8026 */
51470298 8027 if (!s->gotit)
ff3187f6 8028 crWaitUntilV(pktin);
6bbce591 8029 /*
8030 * Now is a convenient point to spew any banner material
8031 * that we've accumulated. (This should ensure that when
8032 * we exit the auth loop, we haven't any left to deal
8033 * with.)
8034 */
8035 {
8036 int size = bufchain_size(&ssh->banner);
32874aea 8037 /*
8038 * Don't show the banner if we're operating in
8039 * non-verbose non-interactive mode. (It's probably
8040 * a script, which means nobody will read the
8041 * banner _anyway_, and moreover the printing of
8042 * the banner will screw up processing on the
8043 * output of (say) plink.)
8044 */
6bbce591 8045 if (size && (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE))) {
8046 char *banner = snewn(size, char);
8047 bufchain_fetch(&ssh->banner, banner, size);
8048 c_write_untrusted(ssh, banner, size);
8049 sfree(banner);
32874aea 8050 }
6bbce591 8051 bufchain_clear(&ssh->banner);
1408a877 8052 }
ff3187f6 8053 if (pktin->type == SSH2_MSG_USERAUTH_SUCCESS) {
1408a877 8054 logevent("Access granted");
ab18ccff 8055 s->we_are_in = s->userauth_success = TRUE;
1408a877 8056 break;
8057 }
65a22376 8058
42af6a67 8059 if (pktin->type != SSH2_MSG_USERAUTH_FAILURE && s->type != AUTH_TYPE_GSSAPI) {
e955d348 8060 bombout(("Strange packet received during authentication: "
8061 "type %d", pktin->type));
7ffdbc1a 8062 crStopV;
65a22376 8063 }
8064
51470298 8065 s->gotit = FALSE;
65a22376 8066
1408a877 8067 /*
8068 * OK, we're now sitting on a USERAUTH_FAILURE message, so
8069 * we can look at the string in it and know what we can
8070 * helpfully try next.
8071 */
ff3187f6 8072 if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) {
ff3187f6 8073 ssh_pkt_getstring(pktin, &methods, &methlen);
ff3187f6 8074 if (!ssh2_pkt_getbool(pktin)) {
1408a877 8075 /*
8076 * We have received an unequivocal Access
8077 * Denied. This can translate to a variety of
8221906d 8078 * messages, or no message at all.
8079 *
8080 * For forms of authentication which are attempted
8081 * implicitly, by which I mean without printing
8082 * anything in the window indicating that we're
8083 * trying them, we should never print 'Access
8084 * denied'.
8085 *
8086 * If we do print a message saying that we're
8087 * attempting some kind of authentication, it's OK
8088 * to print a followup message saying it failed -
8089 * but the message may sometimes be more specific
8090 * than simply 'Access denied'.
8091 *
1408a877 8092 * Additionally, if we'd just tried password
8093 * authentication, we should break out of this
8094 * whole loop so as to go back to the username
91f57d1f 8095 * prompt (iff we're configured to allow
8096 * username change attempts).
1408a877 8097 */
51470298 8098 if (s->type == AUTH_TYPE_NONE) {
1408a877 8099 /* do nothing */
51470298 8100 } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD ||
8101 s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) {
8102 if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD)
8103 c_write_str(ssh, "Server refused our key\r\n");
8221906d 8104 logevent("Server refused our key");
8105 } else if (s->type == AUTH_TYPE_PUBLICKEY) {
8106 /* This _shouldn't_ happen except by a
8107 * protocol bug causing client and server to
8108 * disagree on what is a correct signature. */
8109 c_write_str(ssh, "Server refused public-key signature"
8110 " despite accepting key!\r\n");
8111 logevent("Server refused public-key signature"
8112 " despite accepting key!");
51470298 8113 } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) {
8221906d 8114 /* quiet, so no c_write */
8115 logevent("Server refused keyboard-interactive authentication");
8116 } else if (s->type==AUTH_TYPE_GSSAPI) {
8117 /* always quiet, so no c_write */
2af4fe8f 8118 /* also, the code down in the GSSAPI block has
8119 * already logged this in the Event Log */
8221906d 8120 } else if (s->type == AUTH_TYPE_KEYBOARD_INTERACTIVE) {
8121 logevent("Keyboard-interactive authentication failed");
8122 c_write_str(ssh, "Access denied\r\n");
8123 } else {
8124 assert(s->type == AUTH_TYPE_PASSWORD);
8125 logevent("Password authentication failed");
51470298 8126 c_write_str(ssh, "Access denied\r\n");
8221906d 8127
8128 if (conf_get_int(ssh->conf, CONF_change_username)) {
6c9dce7c 8129 /* XXX perhaps we should allow
8130 * keyboard-interactive to do this too? */
51470298 8131 s->we_are_in = FALSE;
1408a877 8132 break;
8133 }
8134 }
8135 } else {
51470298 8136 c_write_str(ssh, "Further authentication required\r\n");
1408a877 8137 logevent("Further authentication required");
8138 }
65a22376 8139
51470298 8140 s->can_pubkey =
32874aea 8141 in_commasep_string("publickey", methods, methlen);
51470298 8142 s->can_passwd =
32874aea 8143 in_commasep_string("password", methods, methlen);
4a693cfc 8144 s->can_keyb_inter = conf_get_int(ssh->conf, CONF_try_ki_auth) &&
761187b6 8145 in_commasep_string("keyboard-interactive", methods, methlen);
1e00c92b 8146#ifndef NO_GSSAPI
8147 if (!ssh->gsslibs)
4a693cfc 8148 ssh->gsslibs = ssh_gss_setup(ssh->conf);
8149 s->can_gssapi = conf_get_int(ssh->conf, CONF_try_gssapi_auth) &&
b3d375b2 8150 in_commasep_string("gssapi-with-mic", methods, methlen) &&
1e00c92b 8151 ssh->gsslibs->nlibraries > 0;
42af6a67 8152#endif
1408a877 8153 }
65a22376 8154
acab36bc 8155 ssh->pkt_actx = SSH2_PKTCTX_NOAUTH;
65a22376 8156
94cd7c3a 8157 if (s->can_pubkey && !s->done_agent && s->nkeys) {
0405e71f 8158
1983e559 8159 /*
94cd7c3a 8160 * Attempt public-key authentication using a key from Pageant.
1983e559 8161 */
1983e559 8162
acab36bc 8163 ssh->pkt_actx = SSH2_PKTCTX_PUBLICKEY;
00db133f 8164
94cd7c3a 8165 logeventf(ssh, "Trying Pageant key #%d", s->keyi);
8166
8167 /* Unpack key from agent response */
8168 s->pklen = GET_32BIT(s->agentp);
8169 s->agentp += 4;
8170 s->pkblob = (char *)s->agentp;
8171 s->agentp += s->pklen;
8172 s->alglen = GET_32BIT(s->pkblob);
8173 s->alg = s->pkblob + 4;
8174 s->commentlen = GET_32BIT(s->agentp);
8175 s->agentp += 4;
8176 s->commentp = (char *)s->agentp;
8177 s->agentp += s->commentlen;
8178 /* s->agentp now points at next key, if any */
8179
8180 /* See if server will accept it */
8181 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 8182 ssh2_pkt_addstring(s->pktout, ssh->username);
94cd7c3a 8183 ssh2_pkt_addstring(s->pktout, "ssh-connection");
8184 /* service requested */
8185 ssh2_pkt_addstring(s->pktout, "publickey");
8186 /* method */
8187 ssh2_pkt_addbool(s->pktout, FALSE); /* no signature included */
8188 ssh2_pkt_addstring_start(s->pktout);
8189 ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen);
8190 ssh2_pkt_addstring_start(s->pktout);
8191 ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen);
8192 ssh2_pkt_send(ssh, s->pktout);
8193 s->type = AUTH_TYPE_PUBLICKEY_OFFER_QUIET;
1983e559 8194
94cd7c3a 8195 crWaitUntilV(pktin);
8196 if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
1983e559 8197
94cd7c3a 8198 /* Offer of key refused. */
8199 s->gotit = TRUE;
1983e559 8200
94cd7c3a 8201 } else {
8202
8203 void *vret;
1983e559 8204
94cd7c3a 8205 if (flags & FLAG_VERBOSE) {
8206 c_write_str(ssh, "Authenticating with "
8207 "public key \"");
8208 c_write(ssh, s->commentp, s->commentlen);
8209 c_write_str(ssh, "\" from agent\r\n");
8210 }
1983e559 8211
94cd7c3a 8212 /*
8213 * Server is willing to accept the key.
8214 * Construct a SIGN_REQUEST.
8215 */
8216 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 8217 ssh2_pkt_addstring(s->pktout, ssh->username);
94cd7c3a 8218 ssh2_pkt_addstring(s->pktout, "ssh-connection");
8219 /* service requested */
8220 ssh2_pkt_addstring(s->pktout, "publickey");
8221 /* method */
8222 ssh2_pkt_addbool(s->pktout, TRUE); /* signature included */
8223 ssh2_pkt_addstring_start(s->pktout);
8224 ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen);
8225 ssh2_pkt_addstring_start(s->pktout);
8226 ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen);
8227
8228 /* Ask agent for signature. */
8229 s->siglen = s->pktout->length - 5 + 4 +
8230 ssh->v2_session_id_len;
8231 if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)
8232 s->siglen -= 4;
8233 s->len = 1; /* message type */
8234 s->len += 4 + s->pklen; /* key blob */
8235 s->len += 4 + s->siglen; /* data to sign */
8236 s->len += 4; /* flags */
8237 s->agentreq = snewn(4 + s->len, char);
8238 PUT_32BIT(s->agentreq, s->len);
8239 s->q = s->agentreq + 4;
8240 *s->q++ = SSH2_AGENTC_SIGN_REQUEST;
8241 PUT_32BIT(s->q, s->pklen);
8242 s->q += 4;
8243 memcpy(s->q, s->pkblob, s->pklen);
8244 s->q += s->pklen;
8245 PUT_32BIT(s->q, s->siglen);
8246 s->q += 4;
8247 /* Now the data to be signed... */
8248 if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) {
8249 PUT_32BIT(s->q, ssh->v2_session_id_len);
51470298 8250 s->q += 4;
94cd7c3a 8251 }
8252 memcpy(s->q, ssh->v2_session_id,
8253 ssh->v2_session_id_len);
8254 s->q += ssh->v2_session_id_len;
8255 memcpy(s->q, s->pktout->data + 5,
8256 s->pktout->length - 5);
8257 s->q += s->pktout->length - 5;
8258 /* And finally the (zero) flags word. */
8259 PUT_32BIT(s->q, 0);
8260 if (!agent_query(s->agentreq, s->len + 4,
8261 &vret, &s->retlen,
8262 ssh_agent_callback, ssh)) {
8263 do {
8264 crReturnV;
8265 if (pktin) {
8266 bombout(("Unexpected data from server"
8267 " while waiting for agent"
8268 " response"));
8269 crStopV;
1983e559 8270 }
94cd7c3a 8271 } while (pktin || inlen > 0);
8272 vret = ssh->agent_response;
8273 s->retlen = ssh->agent_response_len;
8274 }
8275 s->ret = vret;
8276 sfree(s->agentreq);
8277 if (s->ret) {
8278 if (s->ret[4] == SSH2_AGENT_SIGN_RESPONSE) {
8279 logevent("Sending Pageant's response");
8280 ssh2_add_sigblob(ssh, s->pktout,
8281 s->pkblob, s->pklen,
8282 s->ret + 9,
8283 GET_32BIT(s->ret + 5));
8284 ssh2_pkt_send(ssh, s->pktout);
8285 s->type = AUTH_TYPE_PUBLICKEY;
8286 } else {
8287 /* FIXME: less drastic response */
8288 bombout(("Pageant failed to answer challenge"));
8289 crStopV;
1983e559 8290 }
8291 }
1983e559 8292 }
94cd7c3a 8293
8294 /* Do we have any keys left to try? */
8295 if (s->pkblob_in_agent) {
8296 s->done_agent = TRUE;
8297 s->tried_pubkey_config = TRUE;
8298 } else {
8299 s->keyi++;
8300 if (s->keyi >= s->nkeys)
8301 s->done_agent = TRUE;
8302 }
1983e559 8303
edd0cb8a 8304 } else if (s->can_pubkey && s->publickey_blob &&
8305 !s->tried_pubkey_config) {
65a22376 8306
edd0cb8a 8307 struct ssh2_userkey *key; /* not live over crReturn */
8308 char *passphrase; /* not live over crReturn */
65a22376 8309
acab36bc 8310 ssh->pkt_actx = SSH2_PKTCTX_PUBLICKEY;
00db133f 8311
edd0cb8a 8312 s->tried_pubkey_config = TRUE;
8313
65a22376 8314 /*
1408a877 8315 * Try the public key supplied in the configuration.
8316 *
8317 * First, offer the public blob to see if the server is
8318 * willing to accept it.
65a22376 8319 */
ff3187f6 8320 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 8321 ssh2_pkt_addstring(s->pktout, ssh->username);
edd0cb8a 8322 ssh2_pkt_addstring(s->pktout, "ssh-connection");
8323 /* service requested */
8324 ssh2_pkt_addstring(s->pktout, "publickey"); /* method */
8325 ssh2_pkt_addbool(s->pktout, FALSE);
8326 /* no signature included */
8327 ssh2_pkt_addstring(s->pktout, s->publickey_algorithm);
8328 ssh2_pkt_addstring_start(s->pktout);
8329 ssh2_pkt_addstring_data(s->pktout,
8330 (char *)s->publickey_blob,
8331 s->publickey_bloblen);
ff3187f6 8332 ssh2_pkt_send(ssh, s->pktout);
edd0cb8a 8333 logevent("Offered public key");
ff3187f6 8334
8335 crWaitUntilV(pktin);
edd0cb8a 8336 if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
8337 /* Key refused. Give up. */
8338 s->gotit = TRUE; /* reconsider message next loop */
8339 s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD;
8340 continue; /* process this new message */
45068b27 8341 }
edd0cb8a 8342 logevent("Offer of public key accepted");
af659722 8343
45068b27 8344 /*
edd0cb8a 8345 * Actually attempt a serious authentication using
8346 * the key.
45068b27 8347 */
edd0cb8a 8348 if (flags & FLAG_VERBOSE) {
8349 c_write_str(ssh, "Authenticating with public key \"");
8350 c_write_str(ssh, s->publickey_comment);
8351 c_write_str(ssh, "\"\r\n");
8352 }
8353 key = NULL;
8354 while (!key) {
8355 const char *error; /* not live over crReturn */
8356 if (s->publickey_encrypted) {
8357 /*
8358 * Get a passphrase from the user.
8359 */
8360 int ret; /* need not be kept over crReturn */
8361 s->cur_prompt = new_prompts(ssh->frontend);
8362 s->cur_prompt->to_server = FALSE;
8363 s->cur_prompt->name = dupstr("SSH key passphrase");
8364 add_prompt(s->cur_prompt,
8365 dupprintf("Passphrase for key \"%.100s\": ",
8366 s->publickey_comment),
b61f81bc 8367 FALSE);
edd0cb8a 8368 ret = get_userpass_input(s->cur_prompt, NULL, 0);
8369 while (ret < 0) {
8370 ssh->send_ok = 1;
8371 crWaitUntilV(!pktin);
8372 ret = get_userpass_input(s->cur_prompt,
8373 in, inlen);
8374 ssh->send_ok = 0;
8375 }
8376 if (!ret) {
8377 /* Failed to get a passphrase. Terminate. */
8378 free_prompts(s->cur_prompt);
8379 ssh_disconnect(ssh, NULL,
8380 "Unable to authenticate",
8381 SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER,
8382 TRUE);
8383 crStopV;
85fdbe25 8384 }
edd0cb8a 8385 passphrase =
8386 dupstr(s->cur_prompt->prompts[0]->result);
8387 free_prompts(s->cur_prompt);
45068b27 8388 } else {
edd0cb8a 8389 passphrase = NULL; /* no passphrase needed */
45068b27 8390 }
1408a877 8391
edd0cb8a 8392 /*
8393 * Try decrypting the key.
8394 */
4a693cfc 8395 s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
8396 key = ssh2_load_userkey(s->keyfile, passphrase, &error);
edd0cb8a 8397 if (passphrase) {
8398 /* burn the evidence */
dfb88efd 8399 smemclr(passphrase, strlen(passphrase));
edd0cb8a 8400 sfree(passphrase);
8401 }
8402 if (key == SSH2_WRONG_PASSPHRASE || key == NULL) {
8403 if (passphrase &&
8404 (key == SSH2_WRONG_PASSPHRASE)) {
8405 c_write_str(ssh, "Wrong passphrase\r\n");
8406 key = NULL;
8407 /* and loop again */
8408 } else {
8409 c_write_str(ssh, "Unable to load private key (");
8410 c_write_str(ssh, error);
8411 c_write_str(ssh, ")\r\n");
8412 key = NULL;
8413 break; /* try something else */
8414 }
1408a877 8415 }
65a22376 8416 }
65a22376 8417
edd0cb8a 8418 if (key) {
1dd353b5 8419 unsigned char *pkblob, *sigblob, *sigdata;
8420 int pkblob_len, sigblob_len, sigdata_len;
edd0cb8a 8421 int p;
65a22376 8422
1408a877 8423 /*
8424 * We have loaded the private key and the server
8425 * has announced that it's willing to accept it.
8426 * Hallelujah. Generate a signature and send it.
8427 */
ff3187f6 8428 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 8429 ssh2_pkt_addstring(s->pktout, ssh->username);
edd0cb8a 8430 ssh2_pkt_addstring(s->pktout, "ssh-connection");
8431 /* service requested */
8432 ssh2_pkt_addstring(s->pktout, "publickey");
8433 /* method */
ff3187f6 8434 ssh2_pkt_addbool(s->pktout, TRUE);
edd0cb8a 8435 /* signature follows */
ff3187f6 8436 ssh2_pkt_addstring(s->pktout, key->alg->name);
edd0cb8a 8437 pkblob = key->alg->public_blob(key->data,
8438 &pkblob_len);
ff3187f6 8439 ssh2_pkt_addstring_start(s->pktout);
edd0cb8a 8440 ssh2_pkt_addstring_data(s->pktout, (char *)pkblob,
8441 pkblob_len);
1408a877 8442
8443 /*
8444 * The data to be signed is:
8445 *
8446 * string session-id
8447 *
8448 * followed by everything so far placed in the
8449 * outgoing packet.
8450 */
b672f405 8451 sigdata_len = s->pktout->length - 5 + 4 +
8452 ssh->v2_session_id_len;
edd0cb8a 8453 if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)
8454 sigdata_len -= 4;
92d60585 8455 sigdata = snewn(sigdata_len, unsigned char);
edd0cb8a 8456 p = 0;
8457 if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) {
8458 PUT_32BIT(sigdata+p, ssh->v2_session_id_len);
8459 p += 4;
8460 }
b672f405 8461 memcpy(sigdata+p, ssh->v2_session_id,
8462 ssh->v2_session_id_len);
8463 p += ssh->v2_session_id_len;
ff3187f6 8464 memcpy(sigdata+p, s->pktout->data + 5,
8465 s->pktout->length - 5);
edd0cb8a 8466 p += s->pktout->length - 5;
8467 assert(p == sigdata_len);
d8baa528 8468 sigblob = key->alg->sign(key->data, (char *)sigdata,
1dd353b5 8469 sigdata_len, &sigblob_len);
ff3187f6 8470 ssh2_add_sigblob(ssh, s->pktout, pkblob, pkblob_len,
1dd353b5 8471 sigblob, sigblob_len);
8472 sfree(pkblob);
8473 sfree(sigblob);
1408a877 8474 sfree(sigdata);
8475
ff3187f6 8476 ssh2_pkt_send(ssh, s->pktout);
8221906d 8477 logevent("Sent public key signature");
51470298 8478 s->type = AUTH_TYPE_PUBLICKEY;
75374b2f 8479 key->alg->freekey(key->data);
1408a877 8480 }
edd0cb8a 8481
42af6a67 8482#ifndef NO_GSSAPI
8483 } else if (s->can_gssapi && !s->tried_gssapi) {
8484
8485 /* GSSAPI Authentication */
8486
86557057 8487 int micoffset, len;
8488 char *data;
42af6a67 8489 Ssh_gss_buf mic;
8490 s->type = AUTH_TYPE_GSSAPI;
8491 s->tried_gssapi = TRUE;
8492 s->gotit = TRUE;
8493 ssh->pkt_actx = SSH2_PKTCTX_GSSAPI;
8494
b3d375b2 8495 /*
8496 * Pick the highest GSS library on the preference
8497 * list.
8498 */
8499 {
8500 int i, j;
8501 s->gsslib = NULL;
8502 for (i = 0; i < ngsslibs; i++) {
4a693cfc 8503 int want_id = conf_get_int_int(ssh->conf,
8504 CONF_ssh_gsslist, i);
1e00c92b 8505 for (j = 0; j < ssh->gsslibs->nlibraries; j++)
8506 if (ssh->gsslibs->libraries[j].id == want_id) {
8507 s->gsslib = &ssh->gsslibs->libraries[j];
b3d375b2 8508 goto got_gsslib; /* double break */
8509 }
8510 }
8511 got_gsslib:
8512 /*
8513 * We always expect to have found something in
8514 * the above loop: we only came here if there
8515 * was at least one viable GSS library, and the
8516 * preference list should always mention
8517 * everything and only change the order.
8518 */
8519 assert(s->gsslib);
8520 }
8521
8522 if (s->gsslib->gsslogmsg)
8523 logevent(s->gsslib->gsslogmsg);
8524
42af6a67 8525 /* Sending USERAUTH_REQUEST with "gssapi-with-mic" method */
8526 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 8527 ssh2_pkt_addstring(s->pktout, ssh->username);
42af6a67 8528 ssh2_pkt_addstring(s->pktout, "ssh-connection");
8529 ssh2_pkt_addstring(s->pktout, "gssapi-with-mic");
8221906d 8530 logevent("Attempting GSSAPI authentication");
42af6a67 8531
8532 /* add mechanism info */
b3d375b2 8533 s->gsslib->indicate_mech(s->gsslib, &s->gss_buf);
42af6a67 8534
8535 /* number of GSSAPI mechanisms */
8536 ssh2_pkt_adduint32(s->pktout,1);
8537
8538 /* length of OID + 2 */
86557057 8539 ssh2_pkt_adduint32(s->pktout, s->gss_buf.length + 2);
42af6a67 8540 ssh2_pkt_addbyte(s->pktout, SSH2_GSS_OIDTYPE);
8541
8542 /* length of OID */
86557057 8543 ssh2_pkt_addbyte(s->pktout, (unsigned char) s->gss_buf.length);
42af6a67 8544
86557057 8545 ssh_pkt_adddata(s->pktout, s->gss_buf.value,
8546 s->gss_buf.length);
42af6a67 8547 ssh2_pkt_send(ssh, s->pktout);
8548 crWaitUntilV(pktin);
8549 if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_RESPONSE) {
8550 logevent("GSSAPI authentication request refused");
8551 continue;
8552 }
8553
8554 /* check returned packet ... */
8555
86557057 8556 ssh_pkt_getstring(pktin, &data, &len);
8557 s->gss_rcvtok.value = data;
8558 s->gss_rcvtok.length = len;
8559 if (s->gss_rcvtok.length != s->gss_buf.length + 2 ||
8560 ((char *)s->gss_rcvtok.value)[0] != SSH2_GSS_OIDTYPE ||
8561 ((char *)s->gss_rcvtok.value)[1] != s->gss_buf.length ||
8562 memcmp((char *)s->gss_rcvtok.value + 2,
8563 s->gss_buf.value,s->gss_buf.length) ) {
42af6a67 8564 logevent("GSSAPI authentication - wrong response from server");
8565 continue;
8566 }
8567
8568 /* now start running */
b3d375b2 8569 s->gss_stat = s->gsslib->import_name(s->gsslib,
8570 ssh->fullhostname,
8571 &s->gss_srv_name);
42af6a67 8572 if (s->gss_stat != SSH_GSS_OK) {
8573 if (s->gss_stat == SSH_GSS_BAD_HOST_NAME)
8574 logevent("GSSAPI import name failed - Bad service name");
8575 else
8576 logevent("GSSAPI import name failed");
8577 continue;
8578 }
8579
8580 /* fetch TGT into GSS engine */
b3d375b2 8581 s->gss_stat = s->gsslib->acquire_cred(s->gsslib, &s->gss_ctx);
42af6a67 8582
8583 if (s->gss_stat != SSH_GSS_OK) {
8584 logevent("GSSAPI authentication failed to get credentials");
b3d375b2 8585 s->gsslib->release_name(s->gsslib, &s->gss_srv_name);
42af6a67 8586 continue;
8587 }
8588
8589 /* initial tokens are empty */
86557057 8590 SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
99a3b3c9 8591 SSH_GSS_CLEAR_BUF(&s->gss_sndtok);
42af6a67 8592
8593 /* now enter the loop */
8594 do {
b3d375b2 8595 s->gss_stat = s->gsslib->init_sec_context
8596 (s->gsslib,
8597 &s->gss_ctx,
8598 s->gss_srv_name,
4a693cfc 8599 conf_get_int(ssh->conf, CONF_gssapifwd),
b3d375b2 8600 &s->gss_rcvtok,
8601 &s->gss_sndtok);
42af6a67 8602
8603 if (s->gss_stat!=SSH_GSS_S_COMPLETE &&
8604 s->gss_stat!=SSH_GSS_S_CONTINUE_NEEDED) {
8605 logevent("GSSAPI authentication initialisation failed");
8606
b3d375b2 8607 if (s->gsslib->display_status(s->gsslib, s->gss_ctx,
8608 &s->gss_buf) == SSH_GSS_OK) {
86557057 8609 logevent(s->gss_buf.value);
8610 sfree(s->gss_buf.value);
42af6a67 8611 }
8612
8613 break;
8614 }
8615 logevent("GSSAPI authentication initialised");
8616
8617 /* Client and server now exchange tokens until GSSAPI
8618 * no longer says CONTINUE_NEEDED */
8619
86557057 8620 if (s->gss_sndtok.length != 0) {
42af6a67 8621 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
8622 ssh_pkt_addstring_start(s->pktout);
86557057 8623 ssh_pkt_addstring_data(s->pktout,s->gss_sndtok.value,s->gss_sndtok.length);
42af6a67 8624 ssh2_pkt_send(ssh, s->pktout);
b3d375b2 8625 s->gsslib->free_tok(s->gsslib, &s->gss_sndtok);
42af6a67 8626 }
8627
8628 if (s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED) {
8629 crWaitUntilV(pktin);
8630 if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_TOKEN) {
8631 logevent("GSSAPI authentication - bad server response");
8632 s->gss_stat = SSH_GSS_FAILURE;
8633 break;
8634 }
86557057 8635 ssh_pkt_getstring(pktin, &data, &len);
8636 s->gss_rcvtok.value = data;
8637 s->gss_rcvtok.length = len;
42af6a67 8638 }
8639 } while (s-> gss_stat == SSH_GSS_S_CONTINUE_NEEDED);
8640
8641 if (s->gss_stat != SSH_GSS_OK) {
b3d375b2 8642 s->gsslib->release_name(s->gsslib, &s->gss_srv_name);
8643 s->gsslib->release_cred(s->gsslib, &s->gss_ctx);
42af6a67 8644 continue;
8645 }
8646 logevent("GSSAPI authentication loop finished OK");
8647
8648 /* Now send the MIC */
8649
8650 s->pktout = ssh2_pkt_init(0);
8651 micoffset = s->pktout->length;
8652 ssh_pkt_addstring_start(s->pktout);
8653 ssh_pkt_addstring_data(s->pktout, (char *)ssh->v2_session_id, ssh->v2_session_id_len);
8654 ssh_pkt_addbyte(s->pktout, SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 8655 ssh_pkt_addstring(s->pktout, ssh->username);
42af6a67 8656 ssh_pkt_addstring(s->pktout, "ssh-connection");
8657 ssh_pkt_addstring(s->pktout, "gssapi-with-mic");
8658
86557057 8659 s->gss_buf.value = (char *)s->pktout->data + micoffset;
8660 s->gss_buf.length = s->pktout->length - micoffset;
42af6a67 8661
b3d375b2 8662 s->gsslib->get_mic(s->gsslib, s->gss_ctx, &s->gss_buf, &mic);
42af6a67 8663 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_GSSAPI_MIC);
8664 ssh_pkt_addstring_start(s->pktout);
86557057 8665 ssh_pkt_addstring_data(s->pktout, mic.value, mic.length);
42af6a67 8666 ssh2_pkt_send(ssh, s->pktout);
b3d375b2 8667 s->gsslib->free_mic(s->gsslib, &mic);
42af6a67 8668
8669 s->gotit = FALSE;
8670
b3d375b2 8671 s->gsslib->release_name(s->gsslib, &s->gss_srv_name);
8672 s->gsslib->release_cred(s->gsslib, &s->gss_ctx);
42af6a67 8673 continue;
8674#endif
edd0cb8a 8675 } else if (s->can_keyb_inter && !s->kbd_inter_refused) {
8676
8677 /*
8678 * Keyboard-interactive authentication.
8679 */
edd0cb8a 8680
edd0cb8a 8681 s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE;
8682
acab36bc 8683 ssh->pkt_actx = SSH2_PKTCTX_KBDINTER;
edd0cb8a 8684
8685 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 8686 ssh2_pkt_addstring(s->pktout, ssh->username);
edd0cb8a 8687 ssh2_pkt_addstring(s->pktout, "ssh-connection");
8688 /* service requested */
8689 ssh2_pkt_addstring(s->pktout, "keyboard-interactive");
8690 /* method */
8691 ssh2_pkt_addstring(s->pktout, ""); /* lang */
8692 ssh2_pkt_addstring(s->pktout, ""); /* submethods */
8693 ssh2_pkt_send(ssh, s->pktout);
8221906d 8694
8695 logevent("Attempting keyboard-interactive authentication");
edd0cb8a 8696
8697 crWaitUntilV(pktin);
8698 if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) {
8699 /* Server is not willing to do keyboard-interactive
b8e3173d 8700 * at all (or, bizarrely but legally, accepts the
8701 * user without actually issuing any prompts).
8702 * Give up on it entirely. */
edd0cb8a 8703 s->gotit = TRUE;
edd0cb8a 8704 s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET;
8705 s->kbd_inter_refused = TRUE; /* don't try it again */
8706 continue;
8707 }
8708
8709 /*
b8e3173d 8710 * Loop while the server continues to send INFO_REQUESTs.
edd0cb8a 8711 */
b8e3173d 8712 while (pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) {
edd0cb8a 8713
b8e3173d 8714 char *name, *inst, *lang;
8715 int name_len, inst_len, lang_len;
8716 int i;
8717
8718 /*
8719 * We've got a fresh USERAUTH_INFO_REQUEST.
8720 * Get the preamble and start building a prompt.
8721 */
8722 ssh_pkt_getstring(pktin, &name, &name_len);
8723 ssh_pkt_getstring(pktin, &inst, &inst_len);
8724 ssh_pkt_getstring(pktin, &lang, &lang_len);
8725 s->cur_prompt = new_prompts(ssh->frontend);
8726 s->cur_prompt->to_server = TRUE;
edd0cb8a 8727
b8e3173d 8728 /*
9ea10e78 8729 * Get any prompt(s) from the packet.
b8e3173d 8730 */
8731 s->num_prompts = ssh_pkt_getuint32(pktin);
8732 for (i = 0; i < s->num_prompts; i++) {
8733 char *prompt;
8734 int prompt_len;
8735 int echo;
8736 static char noprompt[] =
8737 "<server failed to send prompt>: ";
8738
8739 ssh_pkt_getstring(pktin, &prompt, &prompt_len);
8740 echo = ssh2_pkt_getbool(pktin);
8741 if (!prompt_len) {
8742 prompt = noprompt;
8743 prompt_len = lenof(noprompt)-1;
8744 }
8745 add_prompt(s->cur_prompt,
8746 dupprintf("%.*s", prompt_len, prompt),
b61f81bc 8747 echo);
edd0cb8a 8748 }
b8e3173d 8749
9874c9d7 8750 if (name_len) {
8751 /* FIXME: better prefix to distinguish from
8752 * local prompts? */
8753 s->cur_prompt->name =
8754 dupprintf("SSH server: %.*s", name_len, name);
8755 s->cur_prompt->name_reqd = TRUE;
8756 } else {
8757 s->cur_prompt->name =
8758 dupstr("SSH server authentication");
8759 s->cur_prompt->name_reqd = FALSE;
8760 }
8761 /* We add a prefix to try to make it clear that a prompt
8762 * has come from the server.
8763 * FIXME: ugly to print "Using..." in prompt _every_
8764 * time round. Can this be done more subtly? */
8765 /* Special case: for reasons best known to themselves,
8766 * some servers send k-i requests with no prompts and
8767 * nothing to display. Keep quiet in this case. */
8768 if (s->num_prompts || name_len || inst_len) {
8769 s->cur_prompt->instruction =
8770 dupprintf("Using keyboard-interactive authentication.%s%.*s",
8771 inst_len ? "\n" : "", inst_len, inst);
8772 s->cur_prompt->instr_reqd = TRUE;
8773 } else {
8774 s->cur_prompt->instr_reqd = FALSE;
8775 }
8776
b8e3173d 8777 /*
9ea10e78 8778 * Display any instructions, and get the user's
8779 * response(s).
b8e3173d 8780 */
9ea10e78 8781 {
b8e3173d 8782 int ret; /* not live over crReturn */
8783 ret = get_userpass_input(s->cur_prompt, NULL, 0);
8784 while (ret < 0) {
8785 ssh->send_ok = 1;
8786 crWaitUntilV(!pktin);
8787 ret = get_userpass_input(s->cur_prompt, in, inlen);
8788 ssh->send_ok = 0;
8789 }
8790 if (!ret) {
8791 /*
8792 * Failed to get responses. Terminate.
8793 */
8794 free_prompts(s->cur_prompt);
8795 ssh_disconnect(ssh, NULL, "Unable to authenticate",
8796 SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER,
8797 TRUE);
8798 crStopV;
8799 }
edd0cb8a 8800 }
b8e3173d 8801
8802 /*
9ea10e78 8803 * Send the response(s) to the server.
b8e3173d 8804 */
8805 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_INFO_RESPONSE);
b8e3173d 8806 ssh2_pkt_adduint32(s->pktout, s->num_prompts);
8807 for (i=0; i < s->num_prompts; i++) {
8808 dont_log_password(ssh, s->pktout, PKTLOG_BLANK);
8809 ssh2_pkt_addstring(s->pktout,
8810 s->cur_prompt->prompts[i]->result);
8811 end_log_omission(ssh, s->pktout);
8812 }
18524056 8813 ssh2_pkt_send_with_padding(ssh, s->pktout, 256);
b8e3173d 8814
c69784c0 8815 /*
8816 * Free the prompts structure from this iteration.
8817 * If there's another, a new one will be allocated
8818 * when we return to the top of this while loop.
8819 */
8820 free_prompts(s->cur_prompt);
8821
b8e3173d 8822 /*
8823 * Get the next packet in case it's another
8824 * INFO_REQUEST.
8825 */
8826 crWaitUntilV(pktin);
8827
edd0cb8a 8828 }
8829
8830 /*
b8e3173d 8831 * We should have SUCCESS or FAILURE now.
edd0cb8a 8832 */
b8e3173d 8833 s->gotit = TRUE;
edd0cb8a 8834
8835 } else if (s->can_passwd) {
8836
8837 /*
8838 * Plain old password authentication.
8839 */
8840 int ret; /* not live over crReturn */
e955d348 8841 int changereq_first_time; /* not live over crReturn */
edd0cb8a 8842
acab36bc 8843 ssh->pkt_actx = SSH2_PKTCTX_PASSWORD;
edd0cb8a 8844
8845 s->cur_prompt = new_prompts(ssh->frontend);
8846 s->cur_prompt->to_server = TRUE;
8847 s->cur_prompt->name = dupstr("SSH password");
4a693cfc 8848 add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
8849 ssh->username,
edd0cb8a 8850 ssh->savedhost),
b61f81bc 8851 FALSE);
edd0cb8a 8852
8853 ret = get_userpass_input(s->cur_prompt, NULL, 0);
8854 while (ret < 0) {
8855 ssh->send_ok = 1;
8856 crWaitUntilV(!pktin);
8857 ret = get_userpass_input(s->cur_prompt, in, inlen);
8858 ssh->send_ok = 0;
8859 }
8860 if (!ret) {
8861 /*
8862 * Failed to get responses. Terminate.
8863 */
8864 free_prompts(s->cur_prompt);
8865 ssh_disconnect(ssh, NULL, "Unable to authenticate",
8866 SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER,
8867 TRUE);
8868 crStopV;
8869 }
e955d348 8870 /*
8871 * Squirrel away the password. (We may need it later if
8872 * asked to change it.)
8873 */
8874 s->password = dupstr(s->cur_prompt->prompts[0]->result);
8875 free_prompts(s->cur_prompt);
edd0cb8a 8876
8877 /*
8878 * Send the password packet.
8879 *
95d2d262 8880 * We pad out the password packet to 256 bytes to make
8881 * it harder for an attacker to find the length of the
8882 * user's password.
1408a877 8883 *
95d2d262 8884 * Anyone using a password longer than 256 bytes
8885 * probably doesn't have much to worry about from
1408a877 8886 * people who find out how long their password is!
65a22376 8887 */
ff3187f6 8888 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 8889 ssh2_pkt_addstring(s->pktout, ssh->username);
edd0cb8a 8890 ssh2_pkt_addstring(s->pktout, "ssh-connection");
8891 /* service requested */
ff3187f6 8892 ssh2_pkt_addstring(s->pktout, "password");
8893 ssh2_pkt_addbool(s->pktout, FALSE);
8894 dont_log_password(ssh, s->pktout, PKTLOG_BLANK);
e955d348 8895 ssh2_pkt_addstring(s->pktout, s->password);
ff3187f6 8896 end_log_omission(ssh, s->pktout);
18524056 8897 ssh2_pkt_send_with_padding(ssh, s->pktout, 256);
0d43337a 8898 logevent("Sent password");
28f4d4f0 8899 s->type = AUTH_TYPE_PASSWORD;
edd0cb8a 8900
e955d348 8901 /*
8902 * Wait for next packet, in case it's a password change
8903 * request.
8904 */
8905 crWaitUntilV(pktin);
8906 changereq_first_time = TRUE;
8907
8908 while (pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) {
8909
8910 /*
8911 * We're being asked for a new password
8912 * (perhaps not for the first time).
8913 * Loop until the server accepts it.
8914 */
8915
8916 int got_new = FALSE; /* not live over crReturn */
8917 char *prompt; /* not live over crReturn */
8918 int prompt_len; /* not live over crReturn */
8919
8920 {
8921 char *msg;
8922 if (changereq_first_time)
8923 msg = "Server requested password change";
8924 else
8925 msg = "Server rejected new password";
8926 logevent(msg);
8927 c_write_str(ssh, msg);
8928 c_write_str(ssh, "\r\n");
8929 }
8930
8931 ssh_pkt_getstring(pktin, &prompt, &prompt_len);
8932
8933 s->cur_prompt = new_prompts(ssh->frontend);
8934 s->cur_prompt->to_server = TRUE;
8935 s->cur_prompt->name = dupstr("New SSH password");
8936 s->cur_prompt->instruction =
8937 dupprintf("%.*s", prompt_len, prompt);
8938 s->cur_prompt->instr_reqd = TRUE;
d9add7bc 8939 /*
8940 * There's no explicit requirement in the protocol
8941 * for the "old" passwords in the original and
8942 * password-change messages to be the same, and
8943 * apparently some Cisco kit supports password change
8944 * by the user entering a blank password originally
8945 * and the real password subsequently, so,
8946 * reluctantly, we prompt for the old password again.
8947 *
8948 * (On the other hand, some servers don't even bother
8949 * to check this field.)
8950 */
8951 add_prompt(s->cur_prompt,
8952 dupstr("Current password (blank for previously entered password): "),
b61f81bc 8953 FALSE);
e955d348 8954 add_prompt(s->cur_prompt, dupstr("Enter new password: "),
b61f81bc 8955 FALSE);
e955d348 8956 add_prompt(s->cur_prompt, dupstr("Confirm new password: "),
b61f81bc 8957 FALSE);
e955d348 8958
8959 /*
8960 * Loop until the user manages to enter the same
8961 * password twice.
8962 */
8963 while (!got_new) {
8964
8965 ret = get_userpass_input(s->cur_prompt, NULL, 0);
8966 while (ret < 0) {
8967 ssh->send_ok = 1;
8968 crWaitUntilV(!pktin);
8969 ret = get_userpass_input(s->cur_prompt, in, inlen);
8970 ssh->send_ok = 0;
8971 }
8972 if (!ret) {
8973 /*
8974 * Failed to get responses. Terminate.
8975 */
8976 /* burn the evidence */
8977 free_prompts(s->cur_prompt);
dfb88efd 8978 smemclr(s->password, strlen(s->password));
e955d348 8979 sfree(s->password);
8980 ssh_disconnect(ssh, NULL, "Unable to authenticate",
8981 SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER,
8982 TRUE);
8983 crStopV;
8984 }
8985
8986 /*
d9add7bc 8987 * If the user specified a new original password
8988 * (IYSWIM), overwrite any previously specified
8989 * one.
8990 * (A side effect is that the user doesn't have to
8991 * re-enter it if they louse up the new password.)
8992 */
8993 if (s->cur_prompt->prompts[0]->result[0]) {
dfb88efd 8994 smemclr(s->password, strlen(s->password));
d9add7bc 8995 /* burn the evidence */
8996 sfree(s->password);
8997 s->password =
8998 dupstr(s->cur_prompt->prompts[0]->result);
8999 }
9000
9001 /*
9002 * Check the two new passwords match.
e955d348 9003 */
d9add7bc 9004 got_new = (strcmp(s->cur_prompt->prompts[1]->result,
9005 s->cur_prompt->prompts[2]->result)
e955d348 9006 == 0);
9007 if (!got_new)
9008 /* They don't. Silly user. */
9009 c_write_str(ssh, "Passwords do not match\r\n");
9010
9011 }
9012
9013 /*
9014 * Send the new password (along with the old one).
9015 * (see above for padding rationale)
9016 */
9017 s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
4a693cfc 9018 ssh2_pkt_addstring(s->pktout, ssh->username);
e955d348 9019 ssh2_pkt_addstring(s->pktout, "ssh-connection");
9020 /* service requested */
9021 ssh2_pkt_addstring(s->pktout, "password");
9022 ssh2_pkt_addbool(s->pktout, TRUE);
9023 dont_log_password(ssh, s->pktout, PKTLOG_BLANK);
9024 ssh2_pkt_addstring(s->pktout, s->password);
9025 ssh2_pkt_addstring(s->pktout,
d9add7bc 9026 s->cur_prompt->prompts[1]->result);
e955d348 9027 free_prompts(s->cur_prompt);
9028 end_log_omission(ssh, s->pktout);
18524056 9029 ssh2_pkt_send_with_padding(ssh, s->pktout, 256);
e955d348 9030 logevent("Sent new password");
9031
9032 /*
9033 * Now see what the server has to say about it.
9034 * (If it's CHANGEREQ again, it's not happy with the
9035 * new password.)
9036 */
9037 crWaitUntilV(pktin);
9038 changereq_first_time = FALSE;
9039
9040 }
9041
9042 /*
9043 * We need to reexamine the current pktin at the top
9044 * of the loop. Either:
9045 * - we weren't asked to change password at all, in
9046 * which case it's a SUCCESS or FAILURE with the
9047 * usual meaning
9048 * - we sent a new password, and the server was
9049 * either OK with it (SUCCESS or FAILURE w/partial
9050 * success) or unhappy with the _old_ password
9051 * (FAILURE w/o partial success)
9052 * In any of these cases, we go back to the top of
9053 * the loop and start again.
9054 */
9055 s->gotit = TRUE;
9056
9057 /*
9058 * We don't need the old password any more, in any
9059 * case. Burn the evidence.
9060 */
dfb88efd 9061 smemclr(s->password, strlen(s->password));
e955d348 9062 sfree(s->password);
9063
1408a877 9064 } else {
854caca5 9065 char *str = dupprintf("No supported authentication methods available"
9066 " (server sent: %.*s)",
9067 methlen, methods);
edd0cb8a 9068
854caca5 9069 ssh_disconnect(ssh, str,
9e296bfa 9070 "No supported authentication methods available",
9071 SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE,
9072 FALSE);
854caca5 9073 sfree(str);
9074
7ffdbc1a 9075 crStopV;
edd0cb8a 9076
65a22376 9077 }
edd0cb8a 9078
65a22376 9079 }
a1a1fae4 9080 }
6bbce591 9081 ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = NULL;
7cca0d81 9082
edd0cb8a 9083 /* Clear up various bits and pieces from authentication. */
9084 if (s->publickey_blob) {
9085 sfree(s->publickey_blob);
9086 sfree(s->publickey_comment);
9087 }
94cd7c3a 9088 if (s->agent_response)
9089 sfree(s->agent_response);
edd0cb8a 9090
ab18ccff 9091 if (s->userauth_success) {
9092 /*
9093 * We've just received USERAUTH_SUCCESS, and we haven't sent any
9094 * packets since. Signal the transport layer to consider enacting
9095 * delayed compression.
9096 *
9097 * (Relying on we_are_in is not sufficient, as
9098 * draft-miller-secsh-compression-delayed is quite clear that it
9099 * triggers on USERAUTH_SUCCESS specifically, and we_are_in can
9100 * become set for other reasons.)
9101 */
9102 do_ssh2_transport(ssh, "enabling delayed compression", -2, NULL);
9103 }
9104
7cca0d81 9105 /*
a1a1fae4 9106 * Now the connection protocol has started, one way or another.
7cca0d81 9107 */
9108
0ed48730 9109 ssh->channels = newtree234(ssh_channelcmp);
9110
7cca0d81 9111 /*
b09eaa88 9112 * Set up handlers for some connection protocol messages, so we
9113 * don't have to handle them repeatedly in this coroutine.
9114 */
9115 ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] =
9116 ssh2_msg_channel_window_adjust;
51df0ab5 9117 ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] =
9118 ssh2_msg_global_request;
b09eaa88 9119
9120 /*
0ed48730 9121 * Create the main session channel.
7cca0d81 9122 */
4a693cfc 9123 if (conf_get_int(ssh->conf, CONF_ssh_no_shell)) {
feb02b4e 9124 ssh->mainchan = NULL;
4a693cfc 9125 } else if (*conf_get_str(ssh->conf, CONF_ssh_nc_host)) {
feb02b4e 9126 /*
9127 * Just start a direct-tcpip channel and use it as the main
9128 * channel.
9129 */
9130 ssh->mainchan = snew(struct ssh_channel);
9131 ssh->mainchan->ssh = ssh;
94bdfe40 9132 ssh2_channel_init(ssh->mainchan);
23828b7e 9133 logeventf(ssh,
9134 "Opening direct-tcpip channel to %s:%d in place of session",
4a693cfc 9135 conf_get_str(ssh->conf, CONF_ssh_nc_host),
9136 conf_get_int(ssh->conf, CONF_ssh_nc_port));
feb02b4e 9137 s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
9138 ssh2_pkt_addstring(s->pktout, "direct-tcpip");
9139 ssh2_pkt_adduint32(s->pktout, ssh->mainchan->localid);
feb02b4e 9140 ssh2_pkt_adduint32(s->pktout, ssh->mainchan->v.v2.locwindow);/* our window size */
9141 ssh2_pkt_adduint32(s->pktout, OUR_V2_MAXPKT); /* our max pkt size */
4a693cfc 9142 ssh2_pkt_addstring(s->pktout, conf_get_str(ssh->conf, CONF_ssh_nc_host));
9143 ssh2_pkt_adduint32(s->pktout, conf_get_int(ssh->conf, CONF_ssh_nc_port));
feb02b4e 9144 /*
23828b7e 9145 * There's nothing meaningful to put in the originator
9146 * fields, but some servers insist on syntactically correct
9147 * information.
feb02b4e 9148 */
9149 ssh2_pkt_addstring(s->pktout, "0.0.0.0");
9150 ssh2_pkt_adduint32(s->pktout, 0);
9151 ssh2_pkt_send(ssh, s->pktout);
9152
9153 crWaitUntilV(pktin);
9154 if (pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) {
9155 bombout(("Server refused to open a direct-tcpip channel"));
9156 crStopV;
9157 /* FIXME: error data comes back in FAILURE packet */
9158 }
9159 if (ssh_pkt_getuint32(pktin) != ssh->mainchan->localid) {
9160 bombout(("Server's channel confirmation cited wrong channel"));
9161 crStopV;
9162 }
9163 ssh->mainchan->remoteid = ssh_pkt_getuint32(pktin);
9164 ssh->mainchan->halfopen = FALSE;
9165 ssh->mainchan->type = CHAN_MAINSESSION;
feb02b4e 9166 ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(pktin);
9167 ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin);
feb02b4e 9168 add234(ssh->channels, ssh->mainchan);
9169 update_specials_menu(ssh->frontend);
23828b7e 9170 logevent("Opened direct-tcpip channel");
feb02b4e 9171 ssh->ncmode = TRUE;
9172 } else {
0ed48730 9173 ssh->mainchan = snew(struct ssh_channel);
9174 ssh->mainchan->ssh = ssh;
94bdfe40 9175 ssh2_channel_init(ssh->mainchan);
ff3187f6 9176 s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
9177 ssh2_pkt_addstring(s->pktout, "session");
9178 ssh2_pkt_adduint32(s->pktout, ssh->mainchan->localid);
ff3187f6 9179 ssh2_pkt_adduint32(s->pktout, ssh->mainchan->v.v2.locwindow);/* our window size */
954d5c5a 9180 ssh2_pkt_adduint32(s->pktout, OUR_V2_MAXPKT); /* our max pkt size */
ff3187f6 9181 ssh2_pkt_send(ssh, s->pktout);
9182 crWaitUntilV(pktin);
9183 if (pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) {
0ed48730 9184 bombout(("Server refused to open a session"));
9185 crStopV;
9186 /* FIXME: error data comes back in FAILURE packet */
9187 }
ff3187f6 9188 if (ssh_pkt_getuint32(pktin) != ssh->mainchan->localid) {
0ed48730 9189 bombout(("Server's channel confirmation cited wrong channel"));
9190 crStopV;
9191 }
ff3187f6 9192 ssh->mainchan->remoteid = ssh_pkt_getuint32(pktin);
64d6ff88 9193 ssh->mainchan->halfopen = FALSE;
0ed48730 9194 ssh->mainchan->type = CHAN_MAINSESSION;
ff3187f6 9195 ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(pktin);
9196 ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin);
0ed48730 9197 add234(ssh->channels, ssh->mainchan);
62638676 9198 update_specials_menu(ssh->frontend);
0ed48730 9199 logevent("Opened channel for session");
feb02b4e 9200 ssh->ncmode = FALSE;
9201 }
7cca0d81 9202
9203 /*
51df0ab5 9204 * Now we have a channel, make dispatch table entries for
9205 * general channel-based messages.
9206 */
9207 ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] =
9208 ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] =
9209 ssh2_msg_channel_data;
9210 ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_channel_eof;
9211 ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_channel_close;
9212 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] =
9213 ssh2_msg_channel_open_confirmation;
9214 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] =
9215 ssh2_msg_channel_open_failure;
9216 ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] =
9217 ssh2_msg_channel_request;
9218 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] =
9219 ssh2_msg_channel_open;
ebbc254d 9220 ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = ssh2_msg_channel_response;
9221 ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = ssh2_msg_channel_response;
9222
51df0ab5 9223
4a693cfc 9224 if (ssh->mainchan && conf_get_int(ssh->conf, CONF_ssh_simple)) {
3361b80a 9225 /*
9226 * This message indicates to the server that we promise
9227 * not to try to run any other channel in parallel with
9228 * this one, so it's safe for it to advertise a very large
9229 * window and leave the flow control to TCP.
9230 */
134f5ece 9231 s->pktout = ssh2_chanreq_init(ssh->mainchan,
9232 "simple@putty.projects.tartarus.org",
9233 NULL, NULL);
3361b80a 9234 ssh2_pkt_send(ssh, s->pktout);
9235 }
9236
51df0ab5 9237 /*
c99deca4 9238 * Enable port forwardings.
9239 */
9240 ssh_setup_portfwd(ssh, ssh->conf);
9241
9242 /*
ebbc254d 9243 * Send the CHANNEL_REQUESTS for the main channel. Each one is
9244 * handled by its own little asynchronous co-routine.
c99deca4 9245 */
9246
9247 /*
783415f8 9248 * Potentially enable X11 forwarding.
9249 */
ebbc254d 9250 ssh2_maybe_setup_x11(ssh->mainchan, NULL, NULL);
bc240b21 9251
9252 /*
36c2a3e9 9253 * Potentially enable agent forwarding.
9254 */
ebbc254d 9255 ssh2_maybe_setup_agent(ssh->mainchan, NULL, NULL);
36c2a3e9 9256
9257 /*
7cca0d81 9258 * Now allocate a pty for the session.
9259 */
ebbc254d 9260 ssh2_maybe_setup_pty(ssh->mainchan, NULL, NULL);
7cca0d81 9261
9262 /*
73feed4f 9263 * Send environment variables.
73feed4f 9264 */
ebbc254d 9265 ssh2_setup_env(ssh->mainchan, NULL, NULL);
73feed4f 9266
9267 /*
fd5e5847 9268 * Start a shell or a remote command. We may have to attempt
9269 * this twice if the config data has provided a second choice
9270 * of command.
7cca0d81 9271 */
feb02b4e 9272 if (ssh->mainchan && !ssh->ncmode) while (1) {
fd5e5847 9273 int subsys;
9274 char *cmd;
9275
51470298 9276 if (ssh->fallback_cmd) {
4a693cfc 9277 subsys = conf_get_int(ssh->conf, CONF_ssh_subsys2);
9278 cmd = conf_get_str(ssh->conf, CONF_remote_cmd2);
fd5e5847 9279 } else {
4a693cfc 9280 subsys = conf_get_int(ssh->conf, CONF_ssh_subsys);
9281 cmd = conf_get_str(ssh->conf, CONF_remote_cmd);
fd5e5847 9282 }
9283
fd5e5847 9284 if (subsys) {
134f5ece 9285 s->pktout = ssh2_chanreq_init(ssh->mainchan, "subsystem",
9286 ssh2_response_authconn, NULL);
ff3187f6 9287 ssh2_pkt_addstring(s->pktout, cmd);
fd5e5847 9288 } else if (*cmd) {
134f5ece 9289 s->pktout = ssh2_chanreq_init(ssh->mainchan, "exec",
9290 ssh2_response_authconn, NULL);
ff3187f6 9291 ssh2_pkt_addstring(s->pktout, cmd);
fd5e5847 9292 } else {
134f5ece 9293 s->pktout = ssh2_chanreq_init(ssh->mainchan, "shell",
9294 ssh2_response_authconn, NULL);
32874aea 9295 }
ff3187f6 9296 ssh2_pkt_send(ssh, s->pktout);
b09eaa88 9297
9298 crWaitUntilV(pktin);
9299
ff3187f6 9300 if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) {
9301 if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) {
6b5cf8b4 9302 bombout(("Unexpected response to shell/command request:"
ff3187f6 9303 " packet type %d", pktin->type));
7ffdbc1a 9304 crStopV;
fd5e5847 9305 }
9306 /*
9307 * We failed to start the command. If this is the
9308 * fallback command, we really are finished; if it's
9309 * not, and if the fallback command exists, try falling
9310 * back to it before complaining.
9311 */
4a693cfc 9312 if (!ssh->fallback_cmd &&
9313 *conf_get_str(ssh->conf, CONF_remote_cmd2)) {
fd5e5847 9314 logevent("Primary command failed; attempting fallback");
51470298 9315 ssh->fallback_cmd = TRUE;
fd5e5847 9316 continue;
9317 }
6b5cf8b4 9318 bombout(("Server refused to start a shell/command"));
7ffdbc1a 9319 crStopV;
fd5e5847 9320 } else {
9321 logevent("Started a shell/command");
32874aea 9322 }
fd5e5847 9323 break;
7cca0d81 9324 }
9325
51470298 9326 ssh->state = SSH_STATE_SESSION;
9327 if (ssh->size_needed)
9328 ssh_size(ssh, ssh->term_width, ssh->term_height);
9329 if (ssh->eof_needed)
9330 ssh_special(ssh, TS_EOF);
6e48c3fe 9331
7cca0d81 9332 /*
0e443c99 9333 * All the initial channel requests are done, so install the default
39d830e4 9334 * response handler.
0e443c99 9335 */
39d830e4 9336 ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = ssh2_msg_channel_response;
9337 ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = ssh2_msg_channel_response;
0e443c99 9338
9339 /*
7cca0d81 9340 * Transfer data!
9341 */
b9d7bcad 9342 if (ssh->ldisc)
9343 ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
0ed48730 9344 if (ssh->mainchan)
9345 ssh->send_ok = 1;
7cca0d81 9346 while (1) {
e5574168 9347 crReturnV;
51470298 9348 s->try_send = FALSE;
ff3187f6 9349 if (pktin) {
2b7540a7 9350
51df0ab5 9351 /*
9352 * _All_ the connection-layer packets we expect to
9353 * receive are now handled by the dispatch table.
9354 * Anything that reaches here must be bogus.
9355 */
32874aea 9356
51df0ab5 9357 bombout(("Strange packet received: type %d", pktin->type));
9358 crStopV;
0ed48730 9359 } else if (ssh->mainchan) {
32874aea 9360 /*
9361 * We have spare data. Add it to the channel buffer.
9362 */
d8baa528 9363 ssh2_add_channel_data(ssh->mainchan, (char *)in, inlen);
51470298 9364 s->try_send = TRUE;
32874aea 9365 }
51470298 9366 if (s->try_send) {
32874aea 9367 int i;
9368 struct ssh_channel *c;
9369 /*
9370 * Try to send data on all channels if we can.
9371 */
1bfc7e93 9372 for (i = 0; NULL != (c = index234(ssh->channels, i)); i++)
78280721 9373 ssh2_try_send_and_unthrottle(ssh, c);
7cca0d81 9374 }
e5574168 9375 }
9376
9377 crFinishV;
9378}
9379
9380/*
2e85c969 9381 * Handlers for SSH-2 messages that might arrive at any moment.
b09eaa88 9382 */
409bfa77 9383static void ssh2_msg_disconnect(Ssh ssh, struct Packet *pktin)
b09eaa88 9384{
9385 /* log reason code in disconnect message */
9386 char *buf, *msg;
60860bc3 9387 int reason, msglen;
b09eaa88 9388
9389 reason = ssh_pkt_getuint32(pktin);
9390 ssh_pkt_getstring(pktin, &msg, &msglen);
9391
9392 if (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) {
9393 buf = dupprintf("Received disconnect message (%s)",
9394 ssh2_disconnect_reasons[reason]);
9395 } else {
9396 buf = dupprintf("Received disconnect message (unknown"
9397 " type %d)", reason);
9398 }
9399 logevent(buf);
9400 sfree(buf);
60860bc3 9401 buf = dupprintf("Disconnection message text: %.*s",
9402 msglen, msg);
b09eaa88 9403 logevent(buf);
60860bc3 9404 bombout(("Server sent disconnect message\ntype %d (%s):\n\"%.*s\"",
b09eaa88 9405 reason,
9406 (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) ?
9407 ssh2_disconnect_reasons[reason] : "unknown",
60860bc3 9408 msglen, msg));
b09eaa88 9409 sfree(buf);
9410}
9411
409bfa77 9412static void ssh2_msg_debug(Ssh ssh, struct Packet *pktin)
b09eaa88 9413{
9414 /* log the debug message */
fb983202 9415 char *msg;
b09eaa88 9416 int msglen;
b09eaa88 9417
3ab79841 9418 /* XXX maybe we should actually take notice of the return value */
9419 ssh2_pkt_getbool(pktin);
b09eaa88 9420 ssh_pkt_getstring(pktin, &msg, &msglen);
9421
fb983202 9422 logeventf(ssh, "Remote debug message: %.*s", msglen, msg);
b09eaa88 9423}
9424
bc074147 9425static void ssh2_msg_transport(Ssh ssh, struct Packet *pktin)
9426{
9427 do_ssh2_transport(ssh, NULL, 0, pktin);
9428}
9429
9430/*
9431 * Called if we receive a packet that isn't allowed by the protocol.
9432 * This only applies to packets whose meaning PuTTY understands.
9433 * Entirely unknown packets are handled below.
9434 */
9435static void ssh2_msg_unexpected(Ssh ssh, struct Packet *pktin)
9436{
9437 char *buf = dupprintf("Server protocol violation: unexpected %s packet",
9438 ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx,
9439 pktin->type));
9440 ssh_disconnect(ssh, NULL, buf, SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE);
1418b055 9441 sfree(buf);
bc074147 9442}
9443
409bfa77 9444static void ssh2_msg_something_unimplemented(Ssh ssh, struct Packet *pktin)
b09eaa88 9445{
9446 struct Packet *pktout;
9447 pktout = ssh2_pkt_init(SSH2_MSG_UNIMPLEMENTED);
9448 ssh2_pkt_adduint32(pktout, pktin->sequence);
9449 /*
9450 * UNIMPLEMENTED messages MUST appear in the same order as the
9451 * messages they respond to. Hence, never queue them.
9452 */
9453 ssh2_pkt_send_noqueue(ssh, pktout);
9454}
9455
9456/*
2e85c969 9457 * Handle the top-level SSH-2 protocol.
7cca0d81 9458 */
b09eaa88 9459static void ssh2_protocol_setup(Ssh ssh)
9460{
9461 int i;
9462
9463 /*
9464 * Most messages cause SSH2_MSG_UNIMPLEMENTED.
9465 */
9466 for (i = 0; i < 256; i++)
9467 ssh->packet_dispatch[i] = ssh2_msg_something_unimplemented;
9468
9469 /*
bc074147 9470 * Initially, we only accept transport messages (and a few generic
9471 * ones). do_ssh2_authconn will add more when it starts.
9472 * Messages that are understood but not currently acceptable go to
9473 * ssh2_msg_unexpected.
b09eaa88 9474 */
bc074147 9475 ssh->packet_dispatch[SSH2_MSG_UNIMPLEMENTED] = ssh2_msg_unexpected;
9476 ssh->packet_dispatch[SSH2_MSG_SERVICE_REQUEST] = ssh2_msg_unexpected;
9477 ssh->packet_dispatch[SSH2_MSG_SERVICE_ACCEPT] = ssh2_msg_unexpected;
9478 ssh->packet_dispatch[SSH2_MSG_KEXINIT] = ssh2_msg_transport;
9479 ssh->packet_dispatch[SSH2_MSG_NEWKEYS] = ssh2_msg_transport;
9480 ssh->packet_dispatch[SSH2_MSG_KEXDH_INIT] = ssh2_msg_transport;
9481 ssh->packet_dispatch[SSH2_MSG_KEXDH_REPLY] = ssh2_msg_transport;
9482 /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REQUEST] = ssh2_msg_transport; duplicate case value */
9483 /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_GROUP] = ssh2_msg_transport; duplicate case value */
9484 ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_INIT] = ssh2_msg_transport;
9485 ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REPLY] = ssh2_msg_transport;
9486 ssh->packet_dispatch[SSH2_MSG_USERAUTH_REQUEST] = ssh2_msg_unexpected;
9487 ssh->packet_dispatch[SSH2_MSG_USERAUTH_FAILURE] = ssh2_msg_unexpected;
9488 ssh->packet_dispatch[SSH2_MSG_USERAUTH_SUCCESS] = ssh2_msg_unexpected;
9489 ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = ssh2_msg_unexpected;
9490 ssh->packet_dispatch[SSH2_MSG_USERAUTH_PK_OK] = ssh2_msg_unexpected;
9491 /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ] = ssh2_msg_unexpected; duplicate case value */
9492 /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_REQUEST] = ssh2_msg_unexpected; duplicate case value */
9493 ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_RESPONSE] = ssh2_msg_unexpected;
9494 ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = ssh2_msg_unexpected;
9495 ssh->packet_dispatch[SSH2_MSG_REQUEST_SUCCESS] = ssh2_msg_unexpected;
9496 ssh->packet_dispatch[SSH2_MSG_REQUEST_FAILURE] = ssh2_msg_unexpected;
9497 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = ssh2_msg_unexpected;
9498 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = ssh2_msg_unexpected;
9499 ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = ssh2_msg_unexpected;
9500 ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = ssh2_msg_unexpected;
9501 ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = ssh2_msg_unexpected;
9502 ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = ssh2_msg_unexpected;
9503 ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_unexpected;
9504 ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_unexpected;
9505 ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] = ssh2_msg_unexpected;
9506 ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = ssh2_msg_unexpected;
9507 ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = ssh2_msg_unexpected;
b09eaa88 9508
9509 /*
bc074147 9510 * These messages have a special handler from the start.
b09eaa88 9511 */
9512 ssh->packet_dispatch[SSH2_MSG_DISCONNECT] = ssh2_msg_disconnect;
2e85c969 9513 ssh->packet_dispatch[SSH2_MSG_IGNORE] = ssh_msg_ignore; /* shared with SSH-1 */
b09eaa88 9514 ssh->packet_dispatch[SSH2_MSG_DEBUG] = ssh2_msg_debug;
9515}
9516
9442dd57 9517static void ssh2_timer(void *ctx, long now)
9518{
9519 Ssh ssh = (Ssh)ctx;
9520
ecbb0000 9521 if (ssh->state == SSH_STATE_CLOSED)
9522 return;
9523
4a693cfc 9524 if (!ssh->kex_in_progress && conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0 &&
9442dd57 9525 now - ssh->next_rekey >= 0) {
f382c87d 9526 do_ssh2_transport(ssh, "timeout", -1, NULL);
9442dd57 9527 }
9528}
9529
1c1a7262 9530static void ssh2_protocol(Ssh ssh, void *vin, int inlen,
ff3187f6 9531 struct Packet *pktin)
7cca0d81 9532{
1c1a7262 9533 unsigned char *in = (unsigned char *)vin;
b09eaa88 9534 if (ssh->state == SSH_STATE_CLOSED)
9535 return;
9536
9442dd57 9537 if (pktin) {
9538 ssh->incoming_data_size += pktin->encrypted_len;
9539 if (!ssh->kex_in_progress &&
d57f70af 9540 ssh->max_data_size != 0 &&
9541 ssh->incoming_data_size > ssh->max_data_size)
f382c87d 9542 do_ssh2_transport(ssh, "too much data received", -1, NULL);
9442dd57 9543 }
9544
bc074147 9545 if (pktin)
b09eaa88 9546 ssh->packet_dispatch[pktin->type](ssh, pktin);
bc074147 9547 else if (!ssh->protocol_initial_phase_done)
9548 do_ssh2_transport(ssh, in, inlen, pktin);
9549 else
b09eaa88 9550 do_ssh2_authconn(ssh, in, inlen, pktin);
7cca0d81 9551}
9552
4a693cfc 9553static void ssh_cache_conf_values(Ssh ssh)
9554{
9555 ssh->logomitdata = conf_get_int(ssh->conf, CONF_logomitdata);
9556}
9557
7cca0d81 9558/*
8df7a775 9559 * Called to set up the connection.
374330e2 9560 *
9561 * Returns an error message, or NULL on success.
374330e2 9562 */
cbe2d68f 9563static const char *ssh_init(void *frontend_handle, void **backend_handle,
4a693cfc 9564 Conf *conf, char *host, int port, char **realhost,
9565 int nodelay, int keepalive)
32874aea 9566{
cbe2d68f 9567 const char *p;
51470298 9568 Ssh ssh;
9569
3d88e64d 9570 ssh = snew(struct ssh_tag);
4a693cfc 9571 ssh->conf = conf_copy(conf);
9572 ssh_cache_conf_values(ssh);
125105d1 9573 ssh->version = 0; /* when not ready yet */
51470298 9574 ssh->s = NULL;
9575 ssh->cipher = NULL;
371e569c 9576 ssh->v1_cipher_ctx = NULL;
0183b242 9577 ssh->crcda_ctx = NULL;
51470298 9578 ssh->cscipher = NULL;
371e569c 9579 ssh->cs_cipher_ctx = NULL;
51470298 9580 ssh->sccipher = NULL;
371e569c 9581 ssh->sc_cipher_ctx = NULL;
51470298 9582 ssh->csmac = NULL;
a8327734 9583 ssh->cs_mac_ctx = NULL;
51470298 9584 ssh->scmac = NULL;
e0e1a00d 9585 ssh->sc_mac_ctx = NULL;
51470298 9586 ssh->cscomp = NULL;
5366aed8 9587 ssh->cs_comp_ctx = NULL;
51470298 9588 ssh->sccomp = NULL;
5366aed8 9589 ssh->sc_comp_ctx = NULL;
51470298 9590 ssh->kex = NULL;
389aa499 9591 ssh->kex_ctx = NULL;
51470298 9592 ssh->hostkey = NULL;
9593 ssh->exitcode = -1;
ac934965 9594 ssh->close_expected = FALSE;
9e296bfa 9595 ssh->clean_exit = FALSE;
51470298 9596 ssh->state = SSH_STATE_PREPACKET;
9597 ssh->size_needed = FALSE;
9598 ssh->eof_needed = FALSE;
b9d7bcad 9599 ssh->ldisc = NULL;
a8327734 9600 ssh->logctx = NULL;
51470298 9601 ssh->deferred_send_data = NULL;
9602 ssh->deferred_len = 0;
9603 ssh->deferred_size = 0;
9604 ssh->fallback_cmd = 0;
acab36bc 9605 ssh->pkt_kctx = SSH2_PKTCTX_NOKEX;
9606 ssh->pkt_actx = SSH2_PKTCTX_NOAUTH;
8def70c3 9607 ssh->x11disp = NULL;
be738459 9608 ssh->v1_compressing = FALSE;
51470298 9609 ssh->v2_outgoing_sequence = 0;
9610 ssh->ssh1_rdpkt_crstate = 0;
9611 ssh->ssh2_rdpkt_crstate = 0;
51470298 9612 ssh->ssh_gotdata_crstate = 0;
b09eaa88 9613 ssh->do_ssh1_connection_crstate = 0;
51470298 9614 ssh->do_ssh_init_state = NULL;
9615 ssh->do_ssh1_login_state = NULL;
9616 ssh->do_ssh2_transport_state = NULL;
9617 ssh->do_ssh2_authconn_state = NULL;
4320baf7 9618 ssh->v_c = NULL;
9619 ssh->v_s = NULL;
6571dbfd 9620 ssh->mainchan = NULL;
968d2d92 9621 ssh->throttled_all = 0;
9622 ssh->v1_stdout_throttling = 0;
590f6a5f 9623 ssh->queue = NULL;
9624 ssh->queuelen = ssh->queuesize = 0;
9625 ssh->queueing = FALSE;
06fadff5 9626 ssh->qhead = ssh->qtail = NULL;
e13bba36 9627 ssh->deferred_rekey_reason = NULL;
3d9449a1 9628 bufchain_init(&ssh->queued_incoming_data);
9629 ssh->frozen = FALSE;
4a693cfc 9630 ssh->username = NULL;
bc06669b 9631 ssh->sent_console_eof = FALSE;
4a202fc6 9632 ssh->got_pty = FALSE;
51470298 9633
9634 *backend_handle = ssh;
32874aea 9635
8f203108 9636#ifdef MSCRYPTOAPI
32874aea 9637 if (crypto_startup() == 0)
8f203108 9638 return "Microsoft high encryption pack not installed!";
9639#endif
374330e2 9640
51470298 9641 ssh->frontend = frontend_handle;
4a693cfc 9642 ssh->term_width = conf_get_int(ssh->conf, CONF_width);
9643 ssh->term_height = conf_get_int(ssh->conf, CONF_height);
887035a5 9644
fabd1805 9645 ssh->channels = NULL;
9646 ssh->rportfwds = NULL;
f47a5ffb 9647 ssh->portfwds = NULL;
fabd1805 9648
51470298 9649 ssh->send_ok = 0;
9650 ssh->editing = 0;
9651 ssh->echoing = 0;
ff68564b 9652 ssh->conn_throttle_count = 0;
51470298 9653 ssh->overall_bufsize = 0;
9654 ssh->fallback_cmd = 0;
8df7a775 9655
3648d4c5 9656 ssh->protocol = NULL;
9657
b09eaa88 9658 ssh->protocol_initial_phase_done = FALSE;
9659
39934deb 9660 ssh->pinger = NULL;
9661
9442dd57 9662 ssh->incoming_data_size = ssh->outgoing_data_size =
9663 ssh->deferred_data_size = 0L;
4a693cfc 9664 ssh->max_data_size = parse_blocksize(conf_get_str(ssh->conf,
9665 CONF_ssh_rekey_data));
9442dd57 9666 ssh->kex_in_progress = FALSE;
9667
1e00c92b 9668#ifndef NO_GSSAPI
9669 ssh->gsslibs = NULL;
9670#endif
9671
79bf227b 9672 p = connect_to_host(ssh, host, port, realhost, nodelay, keepalive);
fb09bf1c 9673 if (p != NULL)
9674 return p;
374330e2 9675
5d17ccfc 9676 random_ref();
9677
374330e2 9678 return NULL;
9679}
9680
fabd1805 9681static void ssh_free(void *handle)
9682{
9683 Ssh ssh = (Ssh) handle;
9684 struct ssh_channel *c;
9685 struct ssh_rportfwd *pf;
9686
9687 if (ssh->v1_cipher_ctx)
9688 ssh->cipher->free_context(ssh->v1_cipher_ctx);
9689 if (ssh->cs_cipher_ctx)
9690 ssh->cscipher->free_context(ssh->cs_cipher_ctx);
9691 if (ssh->sc_cipher_ctx)
9692 ssh->sccipher->free_context(ssh->sc_cipher_ctx);
9693 if (ssh->cs_mac_ctx)
9694 ssh->csmac->free_context(ssh->cs_mac_ctx);
9695 if (ssh->sc_mac_ctx)
9696 ssh->scmac->free_context(ssh->sc_mac_ctx);
29b1d0b3 9697 if (ssh->cs_comp_ctx) {
9698 if (ssh->cscomp)
9699 ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx);
9700 else
9701 zlib_compress_cleanup(ssh->cs_comp_ctx);
9702 }
9703 if (ssh->sc_comp_ctx) {
9704 if (ssh->sccomp)
9705 ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx);
9706 else
9707 zlib_decompress_cleanup(ssh->sc_comp_ctx);
9708 }
fabd1805 9709 if (ssh->kex_ctx)
9710 dh_cleanup(ssh->kex_ctx);
9711 sfree(ssh->savedhost);
9712
590f6a5f 9713 while (ssh->queuelen-- > 0)
9714 ssh_free_packet(ssh->queue[ssh->queuelen]);
9715 sfree(ssh->queue);
9716
06fadff5 9717 while (ssh->qhead) {
9718 struct queued_handler *qh = ssh->qhead;
9719 ssh->qhead = qh->next;
9720 sfree(ssh->qhead);
9721 }
9722 ssh->qhead = ssh->qtail = NULL;
9723
fabd1805 9724 if (ssh->channels) {
9725 while ((c = delpos234(ssh->channels, 0)) != NULL) {
9726 switch (c->type) {
9727 case CHAN_X11:
9728 if (c->u.x11.s != NULL)
9729 x11_close(c->u.x11.s);
9730 break;
9731 case CHAN_SOCKDATA:
409eca86 9732 case CHAN_SOCKDATA_DORMANT:
fabd1805 9733 if (c->u.pfd.s != NULL)
9734 pfd_close(c->u.pfd.s);
9735 break;
9736 }
3620f307 9737 if (ssh->version == 2) {
9738 struct outstanding_channel_request *ocr, *nocr;
9739 ocr = c->v.v2.chanreq_head;
9740 while (ocr) {
9741 ocr->handler(c, NULL, ocr->ctx);
9742 nocr = ocr->next;
9743 sfree(ocr);
9744 ocr = nocr;
9745 }
9746 bufchain_clear(&c->v.v2.outbuffer);
9747 }
fabd1805 9748 sfree(c);
9749 }
9750 freetree234(ssh->channels);
cdb52705 9751 ssh->channels = NULL;
fabd1805 9752 }
9753
9754 if (ssh->rportfwds) {
9755 while ((pf = delpos234(ssh->rportfwds, 0)) != NULL)
8aea57fd 9756 free_rportfwd(pf);
fabd1805 9757 freetree234(ssh->rportfwds);
cdb52705 9758 ssh->rportfwds = NULL;
fabd1805 9759 }
9760 sfree(ssh->deferred_send_data);
8def70c3 9761 if (ssh->x11disp)
9762 x11_free_display(ssh->x11disp);
fabd1805 9763 sfree(ssh->do_ssh_init_state);
9764 sfree(ssh->do_ssh1_login_state);
9765 sfree(ssh->do_ssh2_transport_state);
9766 sfree(ssh->do_ssh2_authconn_state);
4320baf7 9767 sfree(ssh->v_c);
9768 sfree(ssh->v_s);
42af6a67 9769 sfree(ssh->fullhostname);
679539d7 9770 if (ssh->crcda_ctx) {
9771 crcda_free_context(ssh->crcda_ctx);
9772 ssh->crcda_ctx = NULL;
9773 }
fabd1805 9774 if (ssh->s)
ac934965 9775 ssh_do_close(ssh, TRUE);
9442dd57 9776 expire_timer_context(ssh);
39934deb 9777 if (ssh->pinger)
9778 pinger_free(ssh->pinger);
3d9449a1 9779 bufchain_clear(&ssh->queued_incoming_data);
4a693cfc 9780 sfree(ssh->username);
9781 conf_free(ssh->conf);
1e00c92b 9782#ifndef NO_GSSAPI
9783 if (ssh->gsslibs)
9784 ssh_gss_cleanup(ssh->gsslibs);
9785#endif
ee50e8b6 9786 sfree(ssh);
5d17ccfc 9787
9788 random_unref();
fabd1805 9789}
9790
374330e2 9791/*
86916870 9792 * Reconfigure the SSH backend.
86916870 9793 */
4a693cfc 9794static void ssh_reconfig(void *handle, Conf *conf)
86916870 9795{
9796 Ssh ssh = (Ssh) handle;
e13bba36 9797 char *rekeying = NULL, rekey_mandatory = FALSE;
e6c1536e 9798 unsigned long old_max_data_size;
4a693cfc 9799 int i, rekey_time;
e6c1536e 9800
4a693cfc 9801 pinger_reconfig(ssh->pinger, ssh->conf, conf);
3b6606c7 9802 if (ssh->portfwds)
4a693cfc 9803 ssh_setup_portfwd(ssh, conf);
e6c1536e 9804
4a693cfc 9805 rekey_time = conf_get_int(conf, CONF_ssh_rekey_time);
9806 if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != rekey_time &&
9807 rekey_time != 0) {
9808 long new_next = ssh->last_rekey + rekey_time*60*TICKSPERSEC;
e6c1536e 9809 long now = GETTICKCOUNT();
9810
9811 if (new_next - now < 0) {
f382c87d 9812 rekeying = "timeout shortened";
e6c1536e 9813 } else {
9814 ssh->next_rekey = schedule_timer(new_next - now, ssh2_timer, ssh);
9815 }
9816 }
9817
9818 old_max_data_size = ssh->max_data_size;
4a693cfc 9819 ssh->max_data_size = parse_blocksize(conf_get_str(ssh->conf,
9820 CONF_ssh_rekey_data));
e6c1536e 9821 if (old_max_data_size != ssh->max_data_size &&
9822 ssh->max_data_size != 0) {
9823 if (ssh->outgoing_data_size > ssh->max_data_size ||
9824 ssh->incoming_data_size > ssh->max_data_size)
f382c87d 9825 rekeying = "data limit lowered";
e6c1536e 9826 }
9827
4a693cfc 9828 if (conf_get_int(ssh->conf, CONF_compression) !=
9829 conf_get_int(conf, CONF_compression)) {
f382c87d 9830 rekeying = "compression setting changed";
e13bba36 9831 rekey_mandatory = TRUE;
9832 }
9833
4a693cfc 9834 for (i = 0; i < CIPHER_MAX; i++)
9835 if (conf_get_int_int(ssh->conf, CONF_ssh_cipherlist, i) !=
9836 conf_get_int_int(conf, CONF_ssh_cipherlist, i)) {
9837 rekeying = "cipher settings changed";
9838 rekey_mandatory = TRUE;
9839 }
9840 if (conf_get_int(ssh->conf, CONF_ssh2_des_cbc) !=
9841 conf_get_int(conf, CONF_ssh2_des_cbc)) {
f382c87d 9842 rekeying = "cipher settings changed";
e13bba36 9843 rekey_mandatory = TRUE;
e6c1536e 9844 }
9845
4a693cfc 9846 conf_free(ssh->conf);
9847 ssh->conf = conf_copy(conf);
9848 ssh_cache_conf_values(ssh);
e13bba36 9849
9850 if (rekeying) {
9851 if (!ssh->kex_in_progress) {
9852 do_ssh2_transport(ssh, rekeying, -1, NULL);
9853 } else if (rekey_mandatory) {
9854 ssh->deferred_rekey_reason = rekeying;
9855 }
9856 }
86916870 9857}
9858
9859/*
86dc445a 9860 * Called to send data down the SSH connection.
374330e2 9861 */
51470298 9862static int ssh_send(void *handle, char *buf, int len)
32874aea 9863{
51470298 9864 Ssh ssh = (Ssh) handle;
9865
9866 if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL)
5471d09a 9867 return 0;
374330e2 9868
d8baa528 9869 ssh->protocol(ssh, (unsigned char *)buf, len, 0);
5471d09a 9870
51470298 9871 return ssh_sendbuffer(ssh);
5471d09a 9872}
9873
9874/*
9875 * Called to query the current amount of buffered stdin data.
9876 */
51470298 9877static int ssh_sendbuffer(void *handle)
5471d09a 9878{
51470298 9879 Ssh ssh = (Ssh) handle;
5471d09a 9880 int override_value;
9881
51470298 9882 if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL)
5471d09a 9883 return 0;
9884
9885 /*
9886 * If the SSH socket itself has backed up, add the total backup
9887 * size on that to any individual buffer on the stdin channel.
9888 */
9889 override_value = 0;
51470298 9890 if (ssh->throttled_all)
9891 override_value = ssh->overall_bufsize;
5471d09a 9892
51470298 9893 if (ssh->version == 1) {
5471d09a 9894 return override_value;
51470298 9895 } else if (ssh->version == 2) {
bc06669b 9896 if (!ssh->mainchan)
5471d09a 9897 return override_value;
9898 else
51470298 9899 return (override_value +
9900 bufchain_size(&ssh->mainchan->v.v2.outbuffer));
5471d09a 9901 }
9902
9903 return 0;
374330e2 9904}
9905
9906/*
6e48c3fe 9907 * Called to set the size of the window from SSH's POV.
374330e2 9908 */
51470298 9909static void ssh_size(void *handle, int width, int height)
32874aea 9910{
51470298 9911 Ssh ssh = (Ssh) handle;
ff3187f6 9912 struct Packet *pktout;
51470298 9913
9914 ssh->term_width = width;
9915 ssh->term_height = height;
f278d6f8 9916
51470298 9917 switch (ssh->state) {
374330e2 9918 case SSH_STATE_BEFORE_SIZE:
3687d221 9919 case SSH_STATE_PREPACKET:
21248260 9920 case SSH_STATE_CLOSED:
374330e2 9921 break; /* do nothing */
9922 case SSH_STATE_INTERMED:
51470298 9923 ssh->size_needed = TRUE; /* buffer for later */
374330e2 9924 break;
9925 case SSH_STATE_SESSION:
4a693cfc 9926 if (!conf_get_int(ssh->conf, CONF_nopty)) {
51470298 9927 if (ssh->version == 1) {
9928 send_packet(ssh, SSH1_CMSG_WINDOW_SIZE,
9929 PKT_INT, ssh->term_height,
9930 PKT_INT, ssh->term_width,
32874aea 9931 PKT_INT, 0, PKT_INT, 0, PKT_END);
0ed48730 9932 } else if (ssh->mainchan) {
134f5ece 9933 pktout = ssh2_chanreq_init(ssh->mainchan, "window-change",
9934 NULL, NULL);
ff3187f6 9935 ssh2_pkt_adduint32(pktout, ssh->term_width);
9936 ssh2_pkt_adduint32(pktout, ssh->term_height);
9937 ssh2_pkt_adduint32(pktout, 0);
9938 ssh2_pkt_adduint32(pktout, 0);
9939 ssh2_pkt_send(ssh, pktout);
32874aea 9940 }
9941 }
9942 break;
374330e2 9943 }
9944}
9945
9946/*
125105d1 9947 * Return a list of the special codes that make sense in this
9948 * protocol.
9949 */
9950static const struct telnet_special *ssh_get_specials(void *handle)
9951{
786ba75e 9952 static const struct telnet_special ssh1_ignore_special[] = {
9953 {"IGNORE message", TS_NOP}
9954 };
cf6ddb95 9955 static const struct telnet_special ssh2_ignore_special[] = {
62638676 9956 {"IGNORE message", TS_NOP},
cf6ddb95 9957 };
9958 static const struct telnet_special ssh2_rekey_special[] = {
9442dd57 9959 {"Repeat key exchange", TS_REKEY},
62638676 9960 };
9961 static const struct telnet_special ssh2_session_specials[] = {
6f2d0cde 9962 {NULL, TS_SEP},
9963 {"Break", TS_BRK},
bccbeb71 9964 /* These are the signal names defined by RFC 4254.
6f2d0cde 9965 * They include all the ISO C signals, but are a subset of the POSIX
9966 * required signals. */
9967 {"SIGINT (Interrupt)", TS_SIGINT},
9968 {"SIGTERM (Terminate)", TS_SIGTERM},
9969 {"SIGKILL (Kill)", TS_SIGKILL},
9970 {"SIGQUIT (Quit)", TS_SIGQUIT},
9971 {"SIGHUP (Hangup)", TS_SIGHUP},
9972 {"More signals", TS_SUBMENU},
9973 {"SIGABRT", TS_SIGABRT}, {"SIGALRM", TS_SIGALRM},
9974 {"SIGFPE", TS_SIGFPE}, {"SIGILL", TS_SIGILL},
9975 {"SIGPIPE", TS_SIGPIPE}, {"SIGSEGV", TS_SIGSEGV},
9976 {"SIGUSR1", TS_SIGUSR1}, {"SIGUSR2", TS_SIGUSR2},
9977 {NULL, TS_EXITMENU}
62638676 9978 };
9979 static const struct telnet_special specials_end[] = {
6f2d0cde 9980 {NULL, TS_EXITMENU}
62638676 9981 };
786ba75e 9982 /* XXX review this length for any changes: */
cf6ddb95 9983 static struct telnet_special ssh_specials[lenof(ssh2_ignore_special) +
9984 lenof(ssh2_rekey_special) +
62638676 9985 lenof(ssh2_session_specials) +
9986 lenof(specials_end)];
125105d1 9987 Ssh ssh = (Ssh) handle;
62638676 9988 int i = 0;
9989#define ADD_SPECIALS(name) \
9990 do { \
9991 assert((i + lenof(name)) <= lenof(ssh_specials)); \
9992 memcpy(&ssh_specials[i], name, sizeof name); \
9993 i += lenof(name); \
9994 } while(0)
125105d1 9995
9996 if (ssh->version == 1) {
62638676 9997 /* Don't bother offering IGNORE if we've decided the remote
9998 * won't cope with it, since we wouldn't bother sending it if
9999 * asked anyway. */
10000 if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE))
786ba75e 10001 ADD_SPECIALS(ssh1_ignore_special);
125105d1 10002 } else if (ssh->version == 2) {
cf6ddb95 10003 if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE))
10004 ADD_SPECIALS(ssh2_ignore_special);
10005 if (!(ssh->remote_bugs & BUG_SSH2_REKEY))
10006 ADD_SPECIALS(ssh2_rekey_special);
62638676 10007 if (ssh->mainchan)
10008 ADD_SPECIALS(ssh2_session_specials);
10009 } /* else we're not ready yet */
10010
10011 if (i) {
10012 ADD_SPECIALS(specials_end);
10013 return ssh_specials;
10014 } else {
125105d1 10015 return NULL;
62638676 10016 }
10017#undef ADD_SPECIALS
125105d1 10018}
10019
10020/*
86dc445a 10021 * Send special codes. TS_EOF is useful for `plink', so you
6abbf9e3 10022 * can send an EOF and collect resulting output (e.g. `plink
10023 * hostname sort').
374330e2 10024 */
51470298 10025static void ssh_special(void *handle, Telnet_Special code)
32874aea 10026{
51470298 10027 Ssh ssh = (Ssh) handle;
ff3187f6 10028 struct Packet *pktout;
51470298 10029
6abbf9e3 10030 if (code == TS_EOF) {
51470298 10031 if (ssh->state != SSH_STATE_SESSION) {
32874aea 10032 /*
10033 * Buffer the EOF in case we are pre-SESSION, so we can
10034 * send it as soon as we reach SESSION.
10035 */
10036 if (code == TS_EOF)
51470298 10037 ssh->eof_needed = TRUE;
32874aea 10038 return;
10039 }
51470298 10040 if (ssh->version == 1) {
10041 send_packet(ssh, SSH1_CMSG_EOF, PKT_END);
0ed48730 10042 } else if (ssh->mainchan) {
bc06669b 10043 sshfwd_write_eof(ssh->mainchan);
00a0b113 10044 ssh->send_ok = 0; /* now stop trying to read from stdin */
32874aea 10045 }
10046 logevent("Sent EOF message");
125105d1 10047 } else if (code == TS_PING || code == TS_NOP) {
51470298 10048 if (ssh->state == SSH_STATE_CLOSED
10049 || ssh->state == SSH_STATE_PREPACKET) return;
10050 if (ssh->version == 1) {
10051 if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE))
10052 send_packet(ssh, SSH1_MSG_IGNORE, PKT_STR, "", PKT_END);
32874aea 10053 } else {
cf6ddb95 10054 if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
10055 pktout = ssh2_pkt_init(SSH2_MSG_IGNORE);
10056 ssh2_pkt_addstring_start(pktout);
10057 ssh2_pkt_send_noqueue(ssh, pktout);
10058 }
32874aea 10059 }
9442dd57 10060 } else if (code == TS_REKEY) {
10061 if (!ssh->kex_in_progress && ssh->version == 2) {
f382c87d 10062 do_ssh2_transport(ssh, "at user request", -1, NULL);
9442dd57 10063 }
125105d1 10064 } else if (code == TS_BRK) {
10065 if (ssh->state == SSH_STATE_CLOSED
10066 || ssh->state == SSH_STATE_PREPACKET) return;
10067 if (ssh->version == 1) {
2e85c969 10068 logevent("Unable to send BREAK signal in SSH-1");
0ed48730 10069 } else if (ssh->mainchan) {
134f5ece 10070 pktout = ssh2_chanreq_init(ssh->mainchan, "break", NULL, NULL);
ff3187f6 10071 ssh2_pkt_adduint32(pktout, 0); /* default break length */
10072 ssh2_pkt_send(ssh, pktout);
125105d1 10073 }
6abbf9e3 10074 } else {
6f2d0cde 10075 /* Is is a POSIX signal? */
10076 char *signame = NULL;
10077 if (code == TS_SIGABRT) signame = "ABRT";
10078 if (code == TS_SIGALRM) signame = "ALRM";
10079 if (code == TS_SIGFPE) signame = "FPE";
10080 if (code == TS_SIGHUP) signame = "HUP";
10081 if (code == TS_SIGILL) signame = "ILL";
10082 if (code == TS_SIGINT) signame = "INT";
10083 if (code == TS_SIGKILL) signame = "KILL";
10084 if (code == TS_SIGPIPE) signame = "PIPE";
10085 if (code == TS_SIGQUIT) signame = "QUIT";
10086 if (code == TS_SIGSEGV) signame = "SEGV";
10087 if (code == TS_SIGTERM) signame = "TERM";
10088 if (code == TS_SIGUSR1) signame = "USR1";
10089 if (code == TS_SIGUSR2) signame = "USR2";
10090 /* The SSH-2 protocol does in principle support arbitrary named
10091 * signals, including signame@domain, but we don't support those. */
10092 if (signame) {
10093 /* It's a signal. */
10094 if (ssh->version == 2 && ssh->mainchan) {
134f5ece 10095 pktout = ssh2_chanreq_init(ssh->mainchan, "signal", NULL, NULL);
ff3187f6 10096 ssh2_pkt_addstring(pktout, signame);
10097 ssh2_pkt_send(ssh, pktout);
6f2d0cde 10098 logeventf(ssh, "Sent signal SIG%s", signame);
10099 }
10100 } else {
10101 /* Never heard of it. Do nothing */
10102 }
6abbf9e3 10103 }
374330e2 10104}
10105
51470298 10106void *new_sock_channel(void *handle, Socket s)
d74d141c 10107{
51470298 10108 Ssh ssh = (Ssh) handle;
d74d141c 10109 struct ssh_channel *c;
3d88e64d 10110 c = snew(struct ssh_channel);
d74d141c 10111
70c0cbe1 10112 c->ssh = ssh;
10113 ssh2_channel_init(c);
10114 c->halfopen = TRUE;
10115 c->type = CHAN_SOCKDATA_DORMANT;/* identify channel type */
10116 c->u.pfd.s = s;
10117 add234(ssh->channels, c);
d74d141c 10118 return c;
10119}
10120
5471d09a 10121/*
10122 * This is called when stdout/stderr (the entity to which
10123 * from_backend sends data) manages to clear some backlog.
10124 */
ae9ae89f 10125static void ssh_unthrottle(void *handle, int bufsize)
5471d09a 10126{
51470298 10127 Ssh ssh = (Ssh) handle;
ff68564b 10128 int buflimit;
10129
51470298 10130 if (ssh->version == 1) {
10131 if (ssh->v1_stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) {
10132 ssh->v1_stdout_throttling = 0;
ff68564b 10133 ssh_throttle_conn(ssh, -1);
5471d09a 10134 }
10135 } else {
ff68564b 10136 if (ssh->mainchan) {
ab87bf1d 10137 ssh2_set_window(ssh->mainchan,
ff68564b 10138 bufsize < ssh->mainchan->v.v2.locmaxwin ?
10139 ssh->mainchan->v.v2.locmaxwin - bufsize : 0);
4a693cfc 10140 if (conf_get_int(ssh->conf, CONF_ssh_simple))
ff68564b 10141 buflimit = 0;
10142 else
10143 buflimit = ssh->mainchan->v.v2.locmaxwin;
10144 if (ssh->mainchan->throttling_conn && bufsize <= buflimit) {
10145 ssh->mainchan->throttling_conn = 0;
10146 ssh_throttle_conn(ssh, -1);
10147 }
10148 }
5471d09a 10149 }
78e71de1 10150
10151 /*
10152 * Now process any SSH connection data that was stashed in our
10153 * queue while we were frozen.
10154 */
10155 ssh_process_queued_incoming_data(ssh);
5471d09a 10156}
10157
6b78788a 10158void ssh_send_port_open(void *channel, char *hostname, int port, char *org)
d74d141c 10159{
10160 struct ssh_channel *c = (struct ssh_channel *)channel;
6b78788a 10161 Ssh ssh = c->ssh;
ff3187f6 10162 struct Packet *pktout;
d74d141c 10163
57356d63 10164 logeventf(ssh, "Opening forwarded connection to %s:%d", hostname, port);
d74d141c 10165
51470298 10166 if (ssh->version == 1) {
10167 send_packet(ssh, SSH1_MSG_PORT_OPEN,
bc240b21 10168 PKT_INT, c->localid,
10169 PKT_STR, hostname,
10170 PKT_INT, port,
31fb1866 10171 /* PKT_STR, <org:orgport>, */
bc240b21 10172 PKT_END);
10173 } else {
ff3187f6 10174 pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
10175 ssh2_pkt_addstring(pktout, "direct-tcpip");
10176 ssh2_pkt_adduint32(pktout, c->localid);
ff3187f6 10177 ssh2_pkt_adduint32(pktout, c->v.v2.locwindow);/* our window size */
954d5c5a 10178 ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */
ff3187f6 10179 ssh2_pkt_addstring(pktout, hostname);
10180 ssh2_pkt_adduint32(pktout, port);
bc240b21 10181 /*
10182 * We make up values for the originator data; partly it's
10183 * too much hassle to keep track, and partly I'm not
10184 * convinced the server should be told details like that
10185 * about my local network configuration.
1f182589 10186 * The "originator IP address" is syntactically a numeric
10187 * IP address, and some servers (e.g., Tectia) get upset
10188 * if it doesn't match this syntax.
bc240b21 10189 */
1f182589 10190 ssh2_pkt_addstring(pktout, "0.0.0.0");
ff3187f6 10191 ssh2_pkt_adduint32(pktout, 0);
10192 ssh2_pkt_send(ssh, pktout);
bc240b21 10193 }
d74d141c 10194}
10195
6226c939 10196static int ssh_connected(void *handle)
32874aea 10197{
51470298 10198 Ssh ssh = (Ssh) handle;
6226c939 10199 return ssh->s != NULL;
32874aea 10200}
8ccc75b0 10201
51470298 10202static int ssh_sendok(void *handle)
32874aea 10203{
51470298 10204 Ssh ssh = (Ssh) handle;
10205 return ssh->send_ok;
32874aea 10206}
fb09bf1c 10207
51470298 10208static int ssh_ldisc(void *handle, int option)
32874aea 10209{
51470298 10210 Ssh ssh = (Ssh) handle;
32874aea 10211 if (option == LD_ECHO)
51470298 10212 return ssh->echoing;
32874aea 10213 if (option == LD_EDIT)
51470298 10214 return ssh->editing;
0965bee0 10215 return FALSE;
10216}
10217
b9d7bcad 10218static void ssh_provide_ldisc(void *handle, void *ldisc)
10219{
10220 Ssh ssh = (Ssh) handle;
10221 ssh->ldisc = ldisc;
10222}
10223
a8327734 10224static void ssh_provide_logctx(void *handle, void *logctx)
10225{
10226 Ssh ssh = (Ssh) handle;
10227 ssh->logctx = logctx;
10228}
10229
51470298 10230static int ssh_return_exitcode(void *handle)
10231{
10232 Ssh ssh = (Ssh) handle;
3bb2f322 10233 if (ssh->s != NULL)
10234 return -1;
10235 else
3c112d53 10236 return (ssh->exitcode >= 0 ? ssh->exitcode : INT_MAX);
51470298 10237}
10238
10239/*
f89c3294 10240 * cfg_info for SSH is the currently running version of the
10241 * protocol. (1 for 1; 2 for 2; 0 for not-decided-yet.)
10242 */
10243static int ssh_cfg_info(void *handle)
10244{
10245 Ssh ssh = (Ssh) handle;
10246 return ssh->version;
10247}
10248
10249/*
51470298 10250 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
10251 * that fails. This variable is the means by which scp.c can reach
10252 * into the SSH code and find out which one it got.
10253 */
10254extern int ssh_fallback_cmd(void *handle)
d8d6c7e5 10255{
51470298 10256 Ssh ssh = (Ssh) handle;
10257 return ssh->fallback_cmd;
d8d6c7e5 10258}
10259
374330e2 10260Backend ssh_backend = {
10261 ssh_init,
fabd1805 10262 ssh_free,
86916870 10263 ssh_reconfig,
374330e2 10264 ssh_send,
5471d09a 10265 ssh_sendbuffer,
374330e2 10266 ssh_size,
4017be6d 10267 ssh_special,
125105d1 10268 ssh_get_specials,
6226c939 10269 ssh_connected,
d8d6c7e5 10270 ssh_return_exitcode,
97db3be4 10271 ssh_sendok,
0965bee0 10272 ssh_ldisc,
b9d7bcad 10273 ssh_provide_ldisc,
a8327734 10274 ssh_provide_logctx,
5471d09a 10275 ssh_unthrottle,
f89c3294 10276 ssh_cfg_info,
9e164d82 10277 "ssh",
10278 PROT_SSH,
97db3be4 10279 22
bc240b21 10280};