Add dependencies for x11fwd.obj to Makefile (thanks Catbells :-)
[u/mdw/putty] / ssh.c
CommitLineData
8df7a775 1#include <windows.h>
374330e2 2#include <stdio.h>
3#include <stdlib.h>
fb09bf1c 4#include <stdarg.h>
5#include <assert.h>
374330e2 6
7#include "putty.h"
dacbd0e8 8#include "tree234.h"
fb09bf1c 9#include "ssh.h"
374330e2 10
11#ifndef FALSE
12#define FALSE 0
13#endif
14#ifndef TRUE
15#define TRUE 1
16#endif
17
fb09bf1c 18#define logevent(s) { logevent(s); \
67779be7 19 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) \
fb09bf1c 20 fprintf(stderr, "%s\n", s); }
21
3cd52910 22#define bombout(msg) ( ssh_state = SSH_STATE_CLOSED, \
4a8fc3c4 23 (s ? sk_close(s), s = NULL : 0), \
3cd52910 24 connection_fatal msg )
8d5de777 25
d211621f 26#define SSH1_MSG_DISCONNECT 1 /* 0x1 */
27#define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */
28#define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */
29#define SSH1_CMSG_USER 4 /* 0x4 */
30#define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */
31#define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */
32#define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */
33#define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */
34#define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */
35#define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */
36#define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */
37#define SSH1_CMSG_EXEC_CMD 13 /* 0xd */
38#define SSH1_SMSG_SUCCESS 14 /* 0xe */
39#define SSH1_SMSG_FAILURE 15 /* 0xf */
40#define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */
41#define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */
42#define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */
43#define SSH1_CMSG_EOF 19 /* 0x13 */
44#define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */
45#define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */
46#define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */
47#define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */
48#define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */
49#define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */
9c964e85 50#define SSH1_SMSG_X11_OPEN 27 /* 0x1b */
51#define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */
52#define SSH1_MSG_PORT_OPEN 29 /* 0x1d */
d211621f 53#define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */
54#define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */
d211621f 55#define SSH1_MSG_IGNORE 32 /* 0x20 */
9c964e85 56#define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */
57#define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */
58#define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */
d211621f 59#define SSH1_MSG_DEBUG 36 /* 0x24 */
4ba9b64b 60#define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */
d211621f 61#define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */
62#define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */
63#define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */
64#define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */
65#define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */
66#define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */
67
68#define SSH1_AUTH_TIS 5 /* 0x5 */
69#define SSH1_AUTH_CCARD 16 /* 0x10 */
70
d211621f 71#define SSH2_MSG_DISCONNECT 1 /* 0x1 */
72#define SSH2_MSG_IGNORE 2 /* 0x2 */
73#define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */
74#define SSH2_MSG_DEBUG 4 /* 0x4 */
75#define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */
76#define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */
77#define SSH2_MSG_KEXINIT 20 /* 0x14 */
78#define SSH2_MSG_NEWKEYS 21 /* 0x15 */
79#define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */
80#define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */
a92dd380 81#define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */
82#define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */
83#define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */
84#define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */
d211621f 85#define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */
86#define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */
87#define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */
88#define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */
89#define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */
90#define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */
91#define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */
92#define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */
93#define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */
94#define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */
95#define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */
96#define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */
97#define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */
98#define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */
99#define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */
100#define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */
101#define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */
102#define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */
103#define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */
104#define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */
105
9005f3ba 106#define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
107#define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
108#define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
109#define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
110#define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
111#define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
112#define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
113#define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
114#define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
115#define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
116#define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
117
d211621f 118#define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
119#define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
120#define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
121#define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
122
123#define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
fb09bf1c 124
7d503c31 125/*
126 * Various remote-bug flags.
127 */
128#define BUG_CHOKES_ON_SSH1_IGNORE 1
129#define BUG_SSH2_HMAC 2
130
fb09bf1c 131#define GET_32BIT(cp) \
132 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
133 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
134 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
135 ((unsigned long)(unsigned char)(cp)[3]))
136
137#define PUT_32BIT(cp, value) { \
138 (cp)[0] = (unsigned char)((value) >> 24); \
139 (cp)[1] = (unsigned char)((value) >> 16); \
140 (cp)[2] = (unsigned char)((value) >> 8); \
141 (cp)[3] = (unsigned char)(value); }
142
7cca0d81 143enum { PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM };
972a41c8 144
374330e2 145/* Coroutine mechanics for the sillier bits of the code */
146#define crBegin1 static int crLine = 0;
147#define crBegin2 switch(crLine) { case 0:;
148#define crBegin crBegin1; crBegin2;
149#define crFinish(z) } crLine = 0; return (z)
150#define crFinishV } crLine = 0; return
151#define crReturn(z) \
152 do {\
153 crLine=__LINE__; return (z); case __LINE__:;\
154 } while (0)
155#define crReturnV \
156 do {\
157 crLine=__LINE__; return; case __LINE__:;\
158 } while (0)
159#define crStop(z) do{ crLine = 0; return (z); }while(0)
160#define crStopV do{ crLine = 0; return; }while(0)
fb09bf1c 161#define crWaitUntil(c) do { crReturn(0); } while (!(c))
7cca0d81 162#define crWaitUntilV(c) do { crReturnV; } while (!(c))
374330e2 163
0c6c9a70 164extern char *x11_init (Socket *, char *, void *);
9c964e85 165extern void x11_close (Socket);
166extern void x11_send (Socket , char *, int);
167extern void x11_invent_auth(char *, int, char *, int);
168
5e8358ad 169/*
170 * Ciphers for SSH2. We miss out single-DES because it isn't
171 * supported; also 3DES and Blowfish are both done differently from
172 * SSH1. (3DES uses outer chaining; Blowfish has the opposite
173 * endianness and different-sized keys.)
5e8358ad 174 */
0a3f1d48 175const static struct ssh2_ciphers *ciphers[] = {
176 &ssh2_aes,
177 &ssh2_blowfish,
178 &ssh2_3des,
8479e33c 179};
e5574168 180
a92dd380 181const static struct ssh_kex *kex_algs[] = {
a92dd380 182 &ssh_diffiehellman_gex,
a92dd380 183 &ssh_diffiehellman };
e5574168 184
85cc02bb 185const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss };
e5574168 186
d39f364a 187static void nullmac_key(unsigned char *key) { }
e5574168 188static void nullmac_generate(unsigned char *blk, int len, unsigned long seq) { }
189static int nullmac_verify(unsigned char *blk, int len, unsigned long seq) { return 1; }
57476f6b 190const static struct ssh_mac ssh_mac_none = {
d39f364a 191 nullmac_key, nullmac_key, nullmac_generate, nullmac_verify, "none", 0
e5574168 192};
8b2715b2 193const static struct ssh_mac *macs[] = {
194 &ssh_sha1, &ssh_md5, &ssh_mac_none };
195const static struct ssh_mac *buggymacs[] = {
196 &ssh_sha1_buggy, &ssh_md5, &ssh_mac_none };
e5574168 197
4ba9b64b 198static void ssh_comp_none_init(void) { }
199static int ssh_comp_none_block(unsigned char *block, int len,
200 unsigned char **outblock, int *outlen) {
201 return 0;
202}
63b00c83 203static int ssh_comp_none_disable(void) { return 0; }
57476f6b 204const static struct ssh_compress ssh_comp_none = {
4ba9b64b 205 "none",
206 ssh_comp_none_init, ssh_comp_none_block,
63b00c83 207 ssh_comp_none_init, ssh_comp_none_block,
208 ssh_comp_none_disable
e5574168 209};
4ba9b64b 210extern const struct ssh_compress ssh_zlib;
211const static struct ssh_compress *compressions[] = {
212 &ssh_zlib, &ssh_comp_none };
374330e2 213
783415f8 214enum { /* channel types */
215 CHAN_MAINSESSION,
216 CHAN_X11,
217 CHAN_AGENT,
218};
219
dacbd0e8 220/*
221 * 2-3-4 tree storing channels.
222 */
223struct ssh_channel {
d211621f 224 unsigned remoteid, localid;
dacbd0e8 225 int type;
226 int closes;
783415f8 227 struct ssh2_data_channel {
228 unsigned char *outbuffer;
229 unsigned outbuflen, outbufsize;
230 unsigned remwindow, remmaxpkt;
231 } v2;
dacbd0e8 232 union {
233 struct ssh_agent_channel {
234 unsigned char *message;
235 unsigned char msglen[4];
236 int lensofar, totallen;
237 } a;
9c964e85 238 struct ssh_x11_channel {
239 Socket s;
783415f8 240 } x11;
dacbd0e8 241 } u;
242};
57476f6b 243
244struct Packet {
245 long length;
246 int type;
247 unsigned char *data;
248 unsigned char *body;
249 long savedpos;
250 long maxlen;
251};
252
0db56f73 253static SHA_State exhash, exhashbase;
57476f6b 254
8df7a775 255static Socket s = NULL;
57476f6b 256
257static unsigned char session_key[32];
4ba9b64b 258static int ssh1_compressing;
db7d555c 259static int ssh_agentfwd_enabled;
9c964e85 260static int ssh_X11_fwd_enabled;
7d503c31 261static int ssh_remote_bugs;
57476f6b 262static const struct ssh_cipher *cipher = NULL;
0a3f1d48 263static const struct ssh2_cipher *cscipher = NULL;
264static const struct ssh2_cipher *sccipher = NULL;
57476f6b 265static const struct ssh_mac *csmac = NULL;
266static const struct ssh_mac *scmac = NULL;
267static const struct ssh_compress *cscomp = NULL;
268static const struct ssh_compress *sccomp = NULL;
269static const struct ssh_kex *kex = NULL;
e055a386 270static const struct ssh_signkey *hostkey = NULL;
65a22376 271static unsigned char ssh2_session_id[20];
fa17a66e 272int (*ssh_get_line)(const char *prompt, char *str, int maxlen,
273 int is_pw) = NULL;
57476f6b 274
275static char *savedhost;
276static int savedport;
277static int ssh_send_ok;
0965bee0 278static int ssh_echoing, ssh_editing;
57476f6b 279
dacbd0e8 280static tree234 *ssh_channels; /* indexed by local id */
57476f6b 281static struct ssh_channel *mainchan; /* primary session channel */
282
283static enum {
3687d221 284 SSH_STATE_PREPACKET,
57476f6b 285 SSH_STATE_BEFORE_SIZE,
286 SSH_STATE_INTERMED,
287 SSH_STATE_SESSION,
288 SSH_STATE_CLOSED
3687d221 289} ssh_state = SSH_STATE_PREPACKET;
57476f6b 290
3687d221 291static int size_needed = FALSE, eof_needed = FALSE;
57476f6b 292
293static struct Packet pktin = { 0, 0, NULL, NULL, 0 };
294static struct Packet pktout = { 0, 0, NULL, NULL, 0 };
b185170a 295static unsigned char *deferred_send_data = NULL;
296static int deferred_len = 0, deferred_size = 0;
57476f6b 297
298static int ssh_version;
299static void (*ssh_protocol)(unsigned char *in, int inlen, int ispkt);
300static void ssh1_protocol(unsigned char *in, int inlen, int ispkt);
301static void ssh2_protocol(unsigned char *in, int inlen, int ispkt);
302static void ssh_size(void);
3687d221 303static void ssh_special (Telnet_Special);
783415f8 304static void ssh2_try_send(struct ssh_channel *c);
305static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len);
57476f6b 306
307static int (*s_rdpkt)(unsigned char **data, int *datalen);
308
309static struct rdpkt1_state_tag {
310 long len, pad, biglen, to_read;
311 unsigned long realcrc, gotcrc;
312 unsigned char *p;
313 int i;
314 int chunk;
315} rdpkt1_state;
316
960e736a 317static struct rdpkt2_state_tag {
318 long len, pad, payload, packetlen, maclen;
319 int i;
320 int cipherblk;
321 unsigned long incoming_sequence;
322} rdpkt2_state;
323
dacbd0e8 324static int ssh_channelcmp(void *av, void *bv) {
325 struct ssh_channel *a = (struct ssh_channel *)av;
326 struct ssh_channel *b = (struct ssh_channel *)bv;
327 if (a->localid < b->localid) return -1;
328 if (a->localid > b->localid) return +1;
329 return 0;
330}
331static int ssh_channelfind(void *av, void *bv) {
d211621f 332 unsigned *a = (unsigned *)av;
dacbd0e8 333 struct ssh_channel *b = (struct ssh_channel *)bv;
334 if (*a < b->localid) return -1;
335 if (*a > b->localid) return +1;
336 return 0;
337}
338
374330e2 339static void c_write (char *buf, int len) {
67779be7 340 if ((flags & FLAG_STDERR)) {
4017be6d 341 int i;
342 for (i = 0; i < len; i++)
343 if (buf[i] != '\r')
344 fputc(buf[i], stderr);
fb09bf1c 345 return;
346 }
fe50e814 347 from_backend(1, buf, len);
3bdaf79d 348}
349
1408a877 350static void c_write_str (char *buf) {
351 c_write(buf, strlen(buf));
352}
353
fb09bf1c 354/*
355 * Collect incoming data in the incoming packet buffer.
e5574168 356 * Decipher and verify the packet when it is completely read.
357 * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets.
fb09bf1c 358 * Update the *data and *datalen variables.
359 * Return the additional nr of bytes needed, or 0 when
360 * a complete packet is available.
361 */
e5574168 362static int ssh1_rdpkt(unsigned char **data, int *datalen)
fb09bf1c 363{
57476f6b 364 struct rdpkt1_state_tag *st = &rdpkt1_state;
374330e2 365
366 crBegin;
374330e2 367
fb09bf1c 368next_packet:
37508af4 369
fb09bf1c 370 pktin.type = 0;
371 pktin.length = 0;
374330e2 372
57476f6b 373 for (st->i = st->len = 0; st->i < 4; st->i++) {
fb09bf1c 374 while ((*datalen) == 0)
57476f6b 375 crReturn(4-st->i);
376 st->len = (st->len << 8) + **data;
fb09bf1c 377 (*data)++, (*datalen)--;
378 }
374330e2 379
fb09bf1c 380#ifdef FWHACK
57476f6b 381 if (st->len == 0x52656d6f) { /* "Remo"te server has closed ... */
382 st->len = 0x300; /* big enough to carry to end */
fb09bf1c 383 }
384#endif
374330e2 385
57476f6b 386 st->pad = 8 - (st->len % 8);
387 st->biglen = st->len + st->pad;
388 pktin.length = st->len - 5;
fb09bf1c 389
57476f6b 390 if (pktin.maxlen < st->biglen) {
391 pktin.maxlen = st->biglen;
dcbde236 392 pktin.data = (pktin.data == NULL ? smalloc(st->biglen+APIEXTRA) :
393 srealloc(pktin.data, st->biglen+APIEXTRA));
fb09bf1c 394 if (!pktin.data)
395 fatalbox("Out of memory");
396 }
374330e2 397
57476f6b 398 st->to_read = st->biglen;
399 st->p = pktin.data;
400 while (st->to_read > 0) {
401 st->chunk = st->to_read;
fb09bf1c 402 while ((*datalen) == 0)
57476f6b 403 crReturn(st->to_read);
404 if (st->chunk > (*datalen))
405 st->chunk = (*datalen);
406 memcpy(st->p, *data, st->chunk);
407 *data += st->chunk;
408 *datalen -= st->chunk;
409 st->p += st->chunk;
410 st->to_read -= st->chunk;
fb09bf1c 411 }
374330e2 412
fb09bf1c 413 if (cipher)
57476f6b 414 cipher->decrypt(pktin.data, st->biglen);
698a5108 415#if 0
416 debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad));
417 for (st->i = 0; st->i < st->biglen; st->i++)
418 debug((" %02x", (unsigned char)pktin.data[st->i]));
419 debug(("\r\n"));
420#endif
374330e2 421
57476f6b 422 st->realcrc = crc32(pktin.data, st->biglen-4);
423 st->gotcrc = GET_32BIT(pktin.data+st->biglen-4);
424 if (st->gotcrc != st->realcrc) {
8d5de777 425 bombout(("Incorrect CRC received on packet"));
426 crReturn(0);
fb09bf1c 427 }
572f871e 428
4ba9b64b 429 pktin.body = pktin.data + st->pad + 1;
430
431 if (ssh1_compressing) {
432 unsigned char *decompblk;
433 int decomplen;
434#if 0
435 int i;
436 debug(("Packet payload pre-decompression:\n"));
437 for (i = -1; i < pktin.length; i++)
438 debug((" %02x", (unsigned char)pktin.body[i]));
439 debug(("\r\n"));
440#endif
441 zlib_decompress_block(pktin.body-1, pktin.length+1,
442 &decompblk, &decomplen);
443
444 if (pktin.maxlen < st->pad + decomplen) {
445 pktin.maxlen = st->pad + decomplen;
dcbde236 446 pktin.data = srealloc(pktin.data, pktin.maxlen+APIEXTRA);
58298665 447 pktin.body = pktin.data + st->pad + 1;
4ba9b64b 448 if (!pktin.data)
449 fatalbox("Out of memory");
450 }
451
452 memcpy(pktin.body-1, decompblk, decomplen);
dcbde236 453 sfree(decompblk);
4ba9b64b 454 pktin.length = decomplen-1;
455#if 0
456 debug(("Packet payload post-decompression:\n"));
457 for (i = -1; i < pktin.length; i++)
458 debug((" %02x", (unsigned char)pktin.body[i]));
459 debug(("\r\n"));
460#endif
461 }
462
e5574168 463 if (pktin.type == SSH1_SMSG_STDOUT_DATA ||
464 pktin.type == SSH1_SMSG_STDERR_DATA ||
465 pktin.type == SSH1_MSG_DEBUG ||
18515c51 466 pktin.type == SSH1_SMSG_AUTH_TIS_CHALLENGE ||
467 pktin.type == SSH1_SMSG_AUTH_CCARD_CHALLENGE) {
fb09bf1c 468 long strlen = GET_32BIT(pktin.body);
8d5de777 469 if (strlen + 4 != pktin.length) {
470 bombout(("Received data packet with bogus string length"));
471 crReturn(0);
472 }
fb09bf1c 473 }
474
4ba9b64b 475 pktin.type = pktin.body[-1];
476
e5574168 477 if (pktin.type == SSH1_MSG_DEBUG) {
fb09bf1c 478 /* log debug message */
479 char buf[80];
480 int strlen = GET_32BIT(pktin.body);
481 strcpy(buf, "Remote: ");
482 if (strlen > 70) strlen = 70;
483 memcpy(buf+8, pktin.body+4, strlen);
484 buf[8+strlen] = '\0';
485 logevent(buf);
486 goto next_packet;
e5574168 487 } else if (pktin.type == SSH1_MSG_IGNORE) {
fb09bf1c 488 /* do nothing */
489 goto next_packet;
490 }
491
492 crFinish(0);
493}
494
e5574168 495static int ssh2_rdpkt(unsigned char **data, int *datalen)
496{
960e736a 497 struct rdpkt2_state_tag *st = &rdpkt2_state;
e5574168 498
499 crBegin;
500
501next_packet:
e5574168 502 pktin.type = 0;
503 pktin.length = 0;
960e736a 504 if (sccipher)
505 st->cipherblk = sccipher->blksize;
e5574168 506 else
960e736a 507 st->cipherblk = 8;
508 if (st->cipherblk < 8)
509 st->cipherblk = 8;
510
511 if (pktin.maxlen < st->cipherblk) {
512 pktin.maxlen = st->cipherblk;
dcbde236 513 pktin.data = (pktin.data == NULL ? smalloc(st->cipherblk+APIEXTRA) :
514 srealloc(pktin.data, st->cipherblk+APIEXTRA));
e5574168 515 if (!pktin.data)
516 fatalbox("Out of memory");
517 }
518
519 /*
520 * Acquire and decrypt the first block of the packet. This will
521 * contain the length and padding details.
522 */
960e736a 523 for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) {
e5574168 524 while ((*datalen) == 0)
960e736a 525 crReturn(st->cipherblk-st->i);
526 pktin.data[st->i] = *(*data)++;
e5574168 527 (*datalen)--;
528 }
529#ifdef FWHACK
530 if (!memcmp(pktin.data, "Remo", 4)) {/* "Remo"te server has closed ... */
531 /* FIXME */
532 }
533#endif
534 if (sccipher)
960e736a 535 sccipher->decrypt(pktin.data, st->cipherblk);
e5574168 536
537 /*
538 * Now get the length and padding figures.
539 */
960e736a 540 st->len = GET_32BIT(pktin.data);
541 st->pad = pktin.data[4];
e5574168 542
543 /*
544 * This enables us to deduce the payload length.
545 */
960e736a 546 st->payload = st->len - st->pad - 1;
e5574168 547
960e736a 548 pktin.length = st->payload + 5;
e5574168 549
550 /*
551 * So now we can work out the total packet length.
552 */
960e736a 553 st->packetlen = st->len + 4;
554 st->maclen = scmac ? scmac->len : 0;
e5574168 555
556 /*
557 * Adjust memory allocation if packet is too big.
558 */
960e736a 559 if (pktin.maxlen < st->packetlen+st->maclen) {
560 pktin.maxlen = st->packetlen+st->maclen;
dcbde236 561 pktin.data = (pktin.data == NULL ? smalloc(pktin.maxlen+APIEXTRA) :
562 srealloc(pktin.data, pktin.maxlen+APIEXTRA));
e5574168 563 if (!pktin.data)
564 fatalbox("Out of memory");
565 }
566
567 /*
568 * Read and decrypt the remainder of the packet.
569 */
960e736a 570 for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; st->i++) {
e5574168 571 while ((*datalen) == 0)
960e736a 572 crReturn(st->packetlen + st->maclen - st->i);
573 pktin.data[st->i] = *(*data)++;
e5574168 574 (*datalen)--;
575 }
576 /* Decrypt everything _except_ the MAC. */
577 if (sccipher)
960e736a 578 sccipher->decrypt(pktin.data + st->cipherblk,
579 st->packetlen - st->cipherblk);
e5574168 580
7cca0d81 581#if 0
960e736a 582 debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad));
583 for (st->i = 0; st->i < st->packetlen; st->i++)
584 debug((" %02x", (unsigned char)pktin.data[st->i]));
d39f364a 585 debug(("\r\n"));
7cca0d81 586#endif
d39f364a 587
e5574168 588 /*
589 * Check the MAC.
590 */
960e736a 591 if (scmac && !scmac->verify(pktin.data, st->len+4, st->incoming_sequence)) {
8d5de777 592 bombout(("Incorrect MAC received on packet"));
593 crReturn(0);
594 }
960e736a 595 st->incoming_sequence++; /* whether or not we MACed */
e5574168 596
4ba9b64b 597 /*
598 * Decompress packet payload.
599 */
600 {
601 unsigned char *newpayload;
602 int newlen;
603 if (sccomp && sccomp->decompress(pktin.data+5, pktin.length-5,
604 &newpayload, &newlen)) {
605 if (pktin.maxlen < newlen+5) {
606 pktin.maxlen = newlen+5;
dcbde236 607 pktin.data = (pktin.data == NULL ? smalloc(pktin.maxlen+APIEXTRA) :
608 srealloc(pktin.data, pktin.maxlen+APIEXTRA));
4ba9b64b 609 if (!pktin.data)
610 fatalbox("Out of memory");
611 }
612 pktin.length = 5 + newlen;
613 memcpy(pktin.data+5, newpayload, newlen);
614#if 0
615 debug(("Post-decompression payload:\r\n"));
616 for (st->i = 0; st->i < newlen; st->i++)
617 debug((" %02x", (unsigned char)pktin.data[5+st->i]));
618 debug(("\r\n"));
619#endif
620
dcbde236 621 sfree(newpayload);
4ba9b64b 622 }
623 }
624
e5574168 625 pktin.savedpos = 6;
626 pktin.type = pktin.data[5];
e5574168 627
7cca0d81 628 if (pktin.type == SSH2_MSG_IGNORE || pktin.type == SSH2_MSG_DEBUG)
629 goto next_packet; /* FIXME: print DEBUG message */
e5574168 630
631 crFinish(0);
632}
633
4ba9b64b 634static void ssh1_pktout_size(int len) {
374330e2 635 int pad, biglen;
636
637 len += 5; /* type and CRC */
638 pad = 8 - (len%8);
639 biglen = len + pad;
640
641 pktout.length = len-5;
642 if (pktout.maxlen < biglen) {
643 pktout.maxlen = biglen;
8f203108 644#ifdef MSCRYPTOAPI
fb09bf1c 645 /* Allocate enough buffer space for extra block
8f203108 646 * for MS CryptEncrypt() */
dcbde236 647 pktout.data = (pktout.data == NULL ? smalloc(biglen+12) :
648 srealloc(pktout.data, biglen+12));
8f203108 649#else
dcbde236 650 pktout.data = (pktout.data == NULL ? smalloc(biglen+4) :
651 srealloc(pktout.data, biglen+4));
8f203108 652#endif
374330e2 653 if (!pktout.data)
654 fatalbox("Out of memory");
655 }
4ba9b64b 656 pktout.body = pktout.data+4+pad+1;
657}
374330e2 658
4ba9b64b 659static void s_wrpkt_start(int type, int len) {
660 ssh1_pktout_size(len);
374330e2 661 pktout.type = type;
374330e2 662}
663
39065bed 664static int s_wrpkt_prepare(void) {
374330e2 665 int pad, len, biglen, i;
666 unsigned long crc;
667
4ba9b64b 668 pktout.body[-1] = pktout.type;
669
7d503c31 670#if 0
671 debug(("Packet payload pre-compression:\n"));
672 for (i = -1; i < pktout.length; i++)
673 debug((" %02x", (unsigned char)pktout.body[i]));
674 debug(("\r\n"));
675#endif
676
4ba9b64b 677 if (ssh1_compressing) {
678 unsigned char *compblk;
679 int complen;
4ba9b64b 680 zlib_compress_block(pktout.body-1, pktout.length+1,
681 &compblk, &complen);
682 ssh1_pktout_size(complen-1);
683 memcpy(pktout.body-1, compblk, complen);
dcbde236 684 sfree(compblk);
4ba9b64b 685#if 0
686 debug(("Packet payload post-compression:\n"));
687 for (i = -1; i < pktout.length; i++)
688 debug((" %02x", (unsigned char)pktout.body[i]));
689 debug(("\r\n"));
690#endif
691 }
692
374330e2 693 len = pktout.length + 5; /* type and CRC */
694 pad = 8 - (len%8);
695 biglen = len + pad;
696
374330e2 697 for (i=0; i<pad; i++)
698 pktout.data[i+4] = random_byte();
699 crc = crc32(pktout.data+4, biglen-4);
fb09bf1c 700 PUT_32BIT(pktout.data+biglen, crc);
701 PUT_32BIT(pktout.data, len);
374330e2 702
698a5108 703#if 0
704 debug(("Sending packet len=%d\r\n", biglen+4));
705 for (i = 0; i < biglen+4; i++)
706 debug((" %02x", (unsigned char)pktout.data[i]));
707 debug(("\r\n"));
708#endif
374330e2 709 if (cipher)
710 cipher->encrypt(pktout.data+4, biglen);
711
39065bed 712 return biglen+4;
713}
714
715static void s_wrpkt(void) {
716 int len;
717 len = s_wrpkt_prepare();
718 sk_write(s, pktout.data, len);
719}
720
721static void s_wrpkt_defer(void) {
722 int len;
723 len = s_wrpkt_prepare();
724 if (deferred_len + len > deferred_size) {
725 deferred_size = deferred_len + len + 128;
726 deferred_send_data = srealloc(deferred_send_data, deferred_size);
727 }
728 memcpy(deferred_send_data+deferred_len, pktout.data, len);
729 deferred_len += len;
374330e2 730}
731
fb09bf1c 732/*
39065bed 733 * Construct a packet with the specified contents.
fb09bf1c 734 */
39065bed 735static void construct_packet(int pkttype, va_list ap1, va_list ap2)
fb09bf1c 736{
fb09bf1c 737 unsigned char *p, *argp, argchar;
738 unsigned long argint;
739 int pktlen, argtype, arglen;
7cca0d81 740 Bignum bn;
fb09bf1c 741
742 pktlen = 0;
39065bed 743 while ((argtype = va_arg(ap1, int)) != PKT_END) {
fb09bf1c 744 switch (argtype) {
745 case PKT_INT:
39065bed 746 (void) va_arg(ap1, int);
fb09bf1c 747 pktlen += 4;
748 break;
749 case PKT_CHAR:
39065bed 750 (void) va_arg(ap1, char);
fb09bf1c 751 pktlen++;
752 break;
753 case PKT_DATA:
39065bed 754 (void) va_arg(ap1, unsigned char *);
755 arglen = va_arg(ap1, int);
fb09bf1c 756 pktlen += arglen;
757 break;
758 case PKT_STR:
39065bed 759 argp = va_arg(ap1, unsigned char *);
fb09bf1c 760 arglen = strlen(argp);
761 pktlen += 4 + arglen;
762 break;
7cca0d81 763 case PKT_BIGNUM:
39065bed 764 bn = va_arg(ap1, Bignum);
5c58ad2d 765 pktlen += ssh1_bignum_length(bn);
7cca0d81 766 break;
fb09bf1c 767 default:
768 assert(0);
769 }
770 }
fb09bf1c 771
772 s_wrpkt_start(pkttype, pktlen);
773 p = pktout.body;
774
39065bed 775 while ((argtype = va_arg(ap2, int)) != PKT_END) {
fb09bf1c 776 switch (argtype) {
777 case PKT_INT:
39065bed 778 argint = va_arg(ap2, int);
fb09bf1c 779 PUT_32BIT(p, argint);
780 p += 4;
781 break;
782 case PKT_CHAR:
39065bed 783 argchar = va_arg(ap2, unsigned char);
fb09bf1c 784 *p = argchar;
785 p++;
786 break;
787 case PKT_DATA:
39065bed 788 argp = va_arg(ap2, unsigned char *);
789 arglen = va_arg(ap2, int);
fb09bf1c 790 memcpy(p, argp, arglen);
791 p += arglen;
792 break;
793 case PKT_STR:
39065bed 794 argp = va_arg(ap2, unsigned char *);
fb09bf1c 795 arglen = strlen(argp);
796 PUT_32BIT(p, arglen);
797 memcpy(p + 4, argp, arglen);
798 p += 4 + arglen;
799 break;
7cca0d81 800 case PKT_BIGNUM:
39065bed 801 bn = va_arg(ap2, Bignum);
5c58ad2d 802 p += ssh1_write_bignum(p, bn);
7cca0d81 803 break;
fb09bf1c 804 }
805 }
39065bed 806}
fb09bf1c 807
39065bed 808static void send_packet(int pkttype, ...) {
809 va_list ap1, ap2;
810 va_start(ap1, pkttype);
811 va_start(ap2, pkttype);
812 construct_packet(pkttype, ap1, ap2);
fb09bf1c 813 s_wrpkt();
814}
815
39065bed 816static void defer_packet(int pkttype, ...) {
817 va_list ap1, ap2;
818 va_start(ap1, pkttype);
819 va_start(ap2, pkttype);
820 construct_packet(pkttype, ap1, ap2);
821 s_wrpkt_defer();
822}
823
9697bfd2 824static int ssh_versioncmp(char *a, char *b) {
825 char *ae, *be;
826 unsigned long av, bv;
827
43aa02a7 828 av = strtoul(a, &ae, 10);
829 bv = strtoul(b, &be, 10);
9697bfd2 830 if (av != bv) return (av < bv ? -1 : +1);
831 if (*ae == '.') ae++;
832 if (*be == '.') be++;
43aa02a7 833 av = strtoul(ae, &ae, 10);
834 bv = strtoul(be, &be, 10);
9697bfd2 835 if (av != bv) return (av < bv ? -1 : +1);
836 return 0;
837}
838
e5574168 839
840/*
a92dd380 841 * Utility routines for putting an SSH-protocol `string' and
842 * `uint32' into a SHA state.
e5574168 843 */
844#include <stdio.h>
75cab814 845static void sha_string(SHA_State *s, void *str, int len) {
e5574168 846 unsigned char lenblk[4];
e5574168 847 PUT_32BIT(lenblk, len);
e5574168 848 SHA_Bytes(s, lenblk, 4);
e5574168 849 SHA_Bytes(s, str, len);
850}
851
a92dd380 852static void sha_uint32(SHA_State *s, unsigned i) {
853 unsigned char intblk[4];
854 PUT_32BIT(intblk, i);
855 SHA_Bytes(s, intblk, 4);
856}
857
7cca0d81 858/*
859 * SSH2 packet construction functions.
860 */
783415f8 861static void ssh2_pkt_ensure(int length) {
862 if (pktout.maxlen < length) {
863 pktout.maxlen = length + 256;
dcbde236 864 pktout.data = (pktout.data == NULL ? smalloc(pktout.maxlen+APIEXTRA) :
865 srealloc(pktout.data, pktout.maxlen+APIEXTRA));
7cca0d81 866 if (!pktout.data)
867 fatalbox("Out of memory");
868 }
783415f8 869}
870static void ssh2_pkt_adddata(void *data, int len) {
871 pktout.length += len;
872 ssh2_pkt_ensure(pktout.length);
7cca0d81 873 memcpy(pktout.data+pktout.length-len, data, len);
874}
75cab814 875static void ssh2_pkt_addbyte(unsigned char byte) {
7cca0d81 876 ssh2_pkt_adddata(&byte, 1);
877}
75cab814 878static void ssh2_pkt_init(int pkt_type) {
7cca0d81 879 pktout.length = 5;
880 ssh2_pkt_addbyte((unsigned char)pkt_type);
881}
75cab814 882static void ssh2_pkt_addbool(unsigned char value) {
7cca0d81 883 ssh2_pkt_adddata(&value, 1);
884}
75cab814 885static void ssh2_pkt_adduint32(unsigned long value) {
7cca0d81 886 unsigned char x[4];
887 PUT_32BIT(x, value);
888 ssh2_pkt_adddata(x, 4);
889}
75cab814 890static void ssh2_pkt_addstring_start(void) {
7cca0d81 891 ssh2_pkt_adduint32(0);
892 pktout.savedpos = pktout.length;
893}
75cab814 894static void ssh2_pkt_addstring_str(char *data) {
7cca0d81 895 ssh2_pkt_adddata(data, strlen(data));
896 PUT_32BIT(pktout.data + pktout.savedpos - 4,
897 pktout.length - pktout.savedpos);
898}
75cab814 899static void ssh2_pkt_addstring_data(char *data, int len) {
7cca0d81 900 ssh2_pkt_adddata(data, len);
901 PUT_32BIT(pktout.data + pktout.savedpos - 4,
902 pktout.length - pktout.savedpos);
903}
75cab814 904static void ssh2_pkt_addstring(char *data) {
7cca0d81 905 ssh2_pkt_addstring_start();
906 ssh2_pkt_addstring_str(data);
907}
75cab814 908static char *ssh2_mpint_fmt(Bignum b, int *len) {
7cca0d81 909 unsigned char *p;
3709bfe9 910 int i, n = (ssh1_bignum_bitcount(b)+7)/8;
911 p = smalloc(n + 1);
7cca0d81 912 if (!p)
913 fatalbox("out of memory");
914 p[0] = 0;
3709bfe9 915 for (i = 1; i <= n; i++)
916 p[i] = bignum_byte(b, n-i);
7cca0d81 917 i = 0;
3709bfe9 918 while (i <= n && p[i] == 0 && (p[i+1] & 0x80) == 0)
7cca0d81 919 i++;
3709bfe9 920 memmove(p, p+i, n+1-i);
921 *len = n+1-i;
7cca0d81 922 return p;
923}
75cab814 924static void ssh2_pkt_addmp(Bignum b) {
7cca0d81 925 unsigned char *p;
926 int len;
927 p = ssh2_mpint_fmt(b, &len);
928 ssh2_pkt_addstring_start();
929 ssh2_pkt_addstring_data(p, len);
dcbde236 930 sfree(p);
7cca0d81 931}
b185170a 932
933/*
934 * Construct an SSH2 final-form packet: compress it, encrypt it,
935 * put the MAC on it. Final packet, ready to be sent, is stored in
936 * pktout.data. Total length is returned.
937 */
938static int ssh2_pkt_construct(void) {
7cca0d81 939 int cipherblk, maclen, padding, i;
940 static unsigned long outgoing_sequence = 0;
941
942 /*
4ba9b64b 943 * Compress packet payload.
944 */
945#if 0
946 debug(("Pre-compression payload:\r\n"));
947 for (i = 5; i < pktout.length; i++)
948 debug((" %02x", (unsigned char)pktout.data[i]));
949 debug(("\r\n"));
950#endif
951 {
952 unsigned char *newpayload;
953 int newlen;
954 if (cscomp && cscomp->compress(pktout.data+5, pktout.length-5,
955 &newpayload, &newlen)) {
956 pktout.length = 5;
957 ssh2_pkt_adddata(newpayload, newlen);
dcbde236 958 sfree(newpayload);
4ba9b64b 959 }
960 }
961
962 /*
7cca0d81 963 * Add padding. At least four bytes, and must also bring total
964 * length (minus MAC) up to a multiple of the block size.
965 */
8479e33c 966 cipherblk = cscipher ? cscipher->blksize : 8; /* block size */
7cca0d81 967 cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */
968 padding = 4;
969 padding += (cipherblk - (pktout.length + padding) % cipherblk) % cipherblk;
783415f8 970 maclen = csmac ? csmac->len : 0;
971 ssh2_pkt_ensure(pktout.length + padding + maclen);
7cca0d81 972 pktout.data[4] = padding;
973 for (i = 0; i < padding; i++)
974 pktout.data[pktout.length + i] = random_byte();
975 PUT_32BIT(pktout.data, pktout.length + padding - 4);
976 if (csmac)
977 csmac->generate(pktout.data, pktout.length + padding,
978 outgoing_sequence);
979 outgoing_sequence++; /* whether or not we MACed */
980
981#if 0
982 debug(("Sending packet len=%d\r\n", pktout.length+padding));
983 for (i = 0; i < pktout.length+padding; i++)
984 debug((" %02x", (unsigned char)pktout.data[i]));
985 debug(("\r\n"));
986#endif
987
988 if (cscipher)
989 cscipher->encrypt(pktout.data, pktout.length + padding);
7cca0d81 990
b185170a 991 /* Ready-to-send packet starts at pktout.data. We return length. */
992 return pktout.length + padding + maclen;
993}
994
995/*
996 * Construct and send an SSH2 packet immediately.
997 */
998static void ssh2_pkt_send(void) {
999 int len = ssh2_pkt_construct();
1000 sk_write(s, pktout.data, len);
1001}
1002
1003/*
1004 * Construct an SSH2 packet and add it to a deferred data block.
1005 * Useful for sending multiple packets in a single sk_write() call,
1006 * to prevent a traffic-analysing listener from being able to work
1007 * out the length of any particular packet (such as the password
1008 * packet).
1009 *
1010 * Note that because SSH2 sequence-numbers its packets, this can
1011 * NOT be used as an m4-style `defer' allowing packets to be
1012 * constructed in one order and sent in another.
1013 */
1014static void ssh2_pkt_defer(void) {
1015 int len = ssh2_pkt_construct();
1016 if (deferred_len + len > deferred_size) {
1017 deferred_size = deferred_len + len + 128;
1018 deferred_send_data = srealloc(deferred_send_data, deferred_size);
1019 }
1020 memcpy(deferred_send_data+deferred_len, pktout.data, len);
1021 deferred_len += len;
1022}
1023
1024/*
1025 * Send the whole deferred data block constructed by
39065bed 1026 * ssh2_pkt_defer() or SSH1's defer_packet().
b185170a 1027 */
39065bed 1028static void ssh_pkt_defersend(void) {
b185170a 1029 sk_write(s, deferred_send_data, deferred_len);
1030 deferred_len = deferred_size = 0;
1031 sfree(deferred_send_data);
1032 deferred_send_data = NULL;
7cca0d81 1033}
1034
1035#if 0
1036void bndebug(char *string, Bignum b) {
1037 unsigned char *p;
1038 int i, len;
1039 p = ssh2_mpint_fmt(b, &len);
1040 debug(("%s", string));
1041 for (i = 0; i < len; i++)
1042 debug((" %02x", p[i]));
1043 debug(("\r\n"));
dcbde236 1044 sfree(p);
7cca0d81 1045}
1046#endif
1047
75cab814 1048static void sha_mpint(SHA_State *s, Bignum b) {
7cca0d81 1049 unsigned char *p;
1050 int len;
1051 p = ssh2_mpint_fmt(b, &len);
1052 sha_string(s, p, len);
dcbde236 1053 sfree(p);
7cca0d81 1054}
1055
1056/*
1057 * SSH2 packet decode functions.
1058 */
75cab814 1059static unsigned long ssh2_pkt_getuint32(void) {
7cca0d81 1060 unsigned long value;
1061 if (pktin.length - pktin.savedpos < 4)
1062 return 0; /* arrgh, no way to decline (FIXME?) */
1063 value = GET_32BIT(pktin.data+pktin.savedpos);
1064 pktin.savedpos += 4;
1065 return value;
1066}
65a22376 1067static int ssh2_pkt_getbool(void) {
1068 unsigned long value;
1069 if (pktin.length - pktin.savedpos < 1)
1070 return 0; /* arrgh, no way to decline (FIXME?) */
1071 value = pktin.data[pktin.savedpos] != 0;
1072 pktin.savedpos++;
1073 return value;
1074}
75cab814 1075static void ssh2_pkt_getstring(char **p, int *length) {
7cca0d81 1076 *p = NULL;
1077 if (pktin.length - pktin.savedpos < 4)
1078 return;
1079 *length = GET_32BIT(pktin.data+pktin.savedpos);
1080 pktin.savedpos += 4;
1081 if (pktin.length - pktin.savedpos < *length)
1082 return;
1083 *p = pktin.data+pktin.savedpos;
1084 pktin.savedpos += *length;
1085}
75cab814 1086static Bignum ssh2_pkt_getmp(void) {
7cca0d81 1087 char *p;
3709bfe9 1088 int length;
7cca0d81 1089 Bignum b;
1090
1091 ssh2_pkt_getstring(&p, &length);
1092 if (!p)
1093 return NULL;
8d5de777 1094 if (p[0] & 0x80) {
1095 bombout(("internal error: Can't handle negative mpints"));
1096 return NULL;
1097 }
3709bfe9 1098 b = bignum_from_bytes(p, length);
7cca0d81 1099 return b;
1100}
1101
7d503c31 1102/*
1103 * Examine the remote side's version string and compare it against
1104 * a list of known buggy implementations.
1105 */
1106static void ssh_detect_bugs(char *vstring) {
1107 char *imp; /* pointer to implementation part */
1108 imp = vstring;
1109 imp += strcspn(imp, "-");
1110 imp += strcspn(imp, "-");
1111
1112 ssh_remote_bugs = 0;
1113
1114 if (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") ||
1115 !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") ||
1116 !strcmp(imp, "1.2.22")) {
1117 /*
1118 * These versions don't support SSH1_MSG_IGNORE, so we have
1119 * to use a different defence against password length
1120 * sniffing.
1121 */
1122 ssh_remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE;
1123 logevent("We believe remote version has SSH1 ignore bug");
1124 }
1125
1126 if (!strncmp(imp, "2.1.0", 5) || !strncmp(imp, "2.0.", 4) ||
1127 !strncmp(imp, "2.2.0", 5) || !strncmp(imp, "2.3.0", 5) ||
1128 !strncmp(imp, "2.1 ", 4)) {
1129 /*
1130 * These versions have the HMAC bug.
1131 */
1132 ssh_remote_bugs |= BUG_SSH2_HMAC;
1133 logevent("We believe remote version has SSH2 HMAC bug");
1134 }
1135}
1136
8df7a775 1137static int do_ssh_init(unsigned char c) {
1138 static char *vsp;
1139 static char version[10];
1140 static char vstring[80];
1141 static char vlog[sizeof(vstring)+20];
1142 static int i;
374330e2 1143
8df7a775 1144 crBegin;
1145
1146 /* Search for the string "SSH-" in the input. */
374330e2 1147 i = 0;
8df7a775 1148 while (1) {
1149 static const int transS[] = { 1, 2, 2, 1 };
1150 static const int transH[] = { 0, 0, 3, 0 };
1151 static const int transminus[] = { 0, 0, 0, -1 };
1152 if (c == 'S') i = transS[i];
1153 else if (c == 'H') i = transH[i];
1154 else if (c == '-') i = transminus[i];
374330e2 1155 else i = 0;
8df7a775 1156 if (i < 0)
1157 break;
1158 crReturn(1); /* get another character */
374330e2 1159 }
8df7a775 1160
c5e9c988 1161 strcpy(vstring, "SSH-");
1162 vsp = vstring+4;
374330e2 1163 i = 0;
1164 while (1) {
8df7a775 1165 crReturn(1); /* get another char */
c5e9c988 1166 if (vsp < vstring+sizeof(vstring)-1)
1167 *vsp++ = c;
374330e2 1168 if (i >= 0) {
1169 if (c == '-') {
1170 version[i] = '\0';
1171 i = -1;
1172 } else if (i < sizeof(version)-1)
1173 version[i++] = c;
1174 }
1175 else if (c == '\n')
1176 break;
1177 }
1178
db7d555c 1179 ssh_agentfwd_enabled = FALSE;
960e736a 1180 rdpkt2_state.incoming_sequence = 0;
1181
c5e9c988 1182 *vsp = 0;
1183 sprintf(vlog, "Server version: %s", vstring);
7d503c31 1184 ssh_detect_bugs(vstring);
c5e9c988 1185 vlog[strcspn(vlog, "\r\n")] = '\0';
1186 logevent(vlog);
1187
adf799dd 1188 /*
1189 * Server version "1.99" means we can choose whether we use v1
1190 * or v2 protocol. Choice is based on cfg.sshprot.
1191 */
1192 if (ssh_versioncmp(version, cfg.sshprot == 1 ? "2.0" : "1.99") >= 0) {
e5574168 1193 /*
1194 * This is a v2 server. Begin v2 protocol.
1195 */
1196 char *verstring = "SSH-2.0-PuTTY";
0db56f73 1197 SHA_Init(&exhashbase);
e5574168 1198 /*
1199 * Hash our version string and their version string.
1200 */
0db56f73 1201 sha_string(&exhashbase, verstring, strlen(verstring));
1202 sha_string(&exhashbase, vstring, strcspn(vstring, "\r\n"));
e5574168 1203 sprintf(vstring, "%s\n", verstring);
1204 sprintf(vlog, "We claim version: %s", verstring);
1205 logevent(vlog);
1206 logevent("Using SSH protocol version 2");
8df7a775 1207 sk_write(s, vstring, strlen(vstring));
e5574168 1208 ssh_protocol = ssh2_protocol;
6abbf9e3 1209 ssh_version = 2;
e5574168 1210 s_rdpkt = ssh2_rdpkt;
1211 } else {
1212 /*
1213 * This is a v1 server. Begin v1 protocol.
1214 */
1215 sprintf(vstring, "SSH-%s-PuTTY\n",
1216 (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5"));
1217 sprintf(vlog, "We claim version: %s", vstring);
1218 vlog[strcspn(vlog, "\r\n")] = '\0';
1219 logevent(vlog);
1220 logevent("Using SSH protocol version 1");
8df7a775 1221 sk_write(s, vstring, strlen(vstring));
e5574168 1222 ssh_protocol = ssh1_protocol;
6abbf9e3 1223 ssh_version = 1;
e5574168 1224 s_rdpkt = ssh1_rdpkt;
1225 }
3687d221 1226 ssh_state = SSH_STATE_BEFORE_SIZE;
8df7a775 1227
1228 crFinish(0);
1229}
1230
1231static void ssh_gotdata(unsigned char *data, int datalen)
1232{
1233 crBegin;
1234
1235 /*
1236 * To begin with, feed the characters one by one to the
1237 * protocol initialisation / selection function do_ssh_init().
1238 * When that returns 0, we're done with the initial greeting
1239 * exchange and can move on to packet discipline.
1240 */
1241 while (1) {
1242 int ret;
1243 if (datalen == 0)
1244 crReturnV; /* more data please */
1245 ret = do_ssh_init(*data);
1246 data++; datalen--;
1247 if (ret == 0)
1248 break;
1249 }
1250
1251 /*
1252 * We emerge from that loop when the initial negotiation is
1253 * over and we have selected an s_rdpkt function. Now pass
1254 * everything to s_rdpkt, and then pass the resulting packets
1255 * to the proper protocol handler.
1256 */
1257 if (datalen == 0)
1258 crReturnV;
1259 while (1) {
1260 while (datalen > 0) {
1261 if ( s_rdpkt(&data, &datalen) == 0 ) {
1262 ssh_protocol(NULL, 0, 1);
1263 if (ssh_state == SSH_STATE_CLOSED) {
1264 return;
1265 }
1266 }
1267 }
1268 crReturnV;
1269 }
1270 crFinishV;
1271}
1272
340cf649 1273static int ssh_receive(Socket skt, int urgent, char *data, int len) {
2c94fd1c 1274 if (urgent==3) {
1275 /* A socket error has occurred. */
7dbb9f56 1276 ssh_state = SSH_STATE_CLOSED;
b47b2198 1277 sk_close(s);
7dbb9f56 1278 s = NULL;
2c94fd1c 1279 connection_fatal(data);
b47b2198 1280 return 0;
1281 } else if (!len) {
8df7a775 1282 /* Connection has closed. */
6abb6cd7 1283 ssh_state = SSH_STATE_CLOSED;
8df7a775 1284 sk_close(s);
1285 s = NULL;
1286 return 0;
1287 }
1288 ssh_gotdata (data, len);
3257deae 1289 if (ssh_state == SSH_STATE_CLOSED) {
1290 if (s) {
1291 sk_close(s);
1292 s = NULL;
1293 }
1294 return 0;
1295 }
fef97f43 1296 return 1;
374330e2 1297}
1298
fb09bf1c 1299/*
8df7a775 1300 * Connect to specified host and port.
1301 * Returns an error message, or NULL on success.
1302 * Also places the canonical host name into `realhost'.
1303 */
1304static char *connect_to_host(char *host, int port, char **realhost)
1305{
1306 SockAddr addr;
1307 char *err;
1308#ifdef FWHACK
1309 char *FWhost;
1310 int FWport;
1311#endif
1312
dcbde236 1313 savedhost = smalloc(1+strlen(host));
8df7a775 1314 if (!savedhost)
1315 fatalbox("Out of memory");
1316 strcpy(savedhost, host);
1317
1318 if (port < 0)
1319 port = 22; /* default ssh port */
1320 savedport = port;
1321
1322#ifdef FWHACK
1323 FWhost = host;
1324 FWport = port;
1325 host = FWSTR;
1326 port = 23;
1327#endif
1328
1329 /*
1330 * Try to find host.
1331 */
1332 addr = sk_namelookup(host, realhost);
1333 if ( (err = sk_addr_error(addr)) )
1334 return err;
1335
1336#ifdef FWHACK
1337 *realhost = FWhost;
1338#endif
1339
1340 /*
1341 * Open socket.
1342 */
4b1e8acc 1343 s = sk_new(addr, port, 0, 1, ssh_receive);
8df7a775 1344 if ( (err = sk_socket_error(s)) )
1345 return err;
1346
1347#ifdef FWHACK
1348 sk_write(s, "connect ", 8);
1349 sk_write(s, FWhost, strlen(FWhost));
1350 {
1351 char buf[20];
1352 sprintf(buf, " %d\n", FWport);
1353 sk_write(s, buf, strlen(buf));
1354 }
1355#endif
1356
1357 return NULL;
1358}
1359
1360/*
fb09bf1c 1361 * Handle the key exchange and user authentication phases.
1362 */
e5574168 1363static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
fb09bf1c 1364{
374330e2 1365 int i, j, len;
374330e2 1366 unsigned char *rsabuf, *keystr1, *keystr2;
1367 unsigned char cookie[8];
1368 struct RSAKey servkey, hostkey;
1369 struct MD5Context md5c;
ccbfb941 1370 static unsigned long supported_ciphers_mask, supported_auths_mask;
7cca0d81 1371 static int tried_publickey;
1372 static unsigned char session_id[16];
bea1ef5f 1373 int cipher_type;
b585ec46 1374 static char username[100];
374330e2 1375
374330e2 1376 crBegin;
1377
fb09bf1c 1378 if (!ispkt) crWaitUntil(ispkt);
374330e2 1379
8d5de777 1380 if (pktin.type != SSH1_SMSG_PUBLIC_KEY) {
1381 bombout(("Public key packet not received"));
1382 crReturn(0);
1383 }
374330e2 1384
c5e9c988 1385 logevent("Received public keys");
374330e2 1386
c5e9c988 1387 memcpy(cookie, pktin.body, 8);
374330e2 1388
7cca0d81 1389 i = makekey(pktin.body+8, &servkey, &keystr1, 0);
1390 j = makekey(pktin.body+8+i, &hostkey, &keystr2, 0);
374330e2 1391
c5e9c988 1392 /*
1c2a93c4 1393 * Log the host key fingerprint.
c5e9c988 1394 */
c5e9c988 1395 {
1396 char logmsg[80];
1c2a93c4 1397 logevent("Host key fingerprint is:");
c5e9c988 1398 strcpy(logmsg, " ");
1c2a93c4 1399 hostkey.comment = NULL;
1400 rsa_fingerprint(logmsg+strlen(logmsg), sizeof(logmsg)-strlen(logmsg),
1401 &hostkey);
c5e9c988 1402 logevent(logmsg);
1403 }
1404
fb09bf1c 1405 supported_ciphers_mask = GET_32BIT(pktin.body+12+i+j);
1406 supported_auths_mask = GET_32BIT(pktin.body+16+i+j);
bea1ef5f 1407
c5e9c988 1408 MD5Init(&md5c);
374330e2 1409 MD5Update(&md5c, keystr2, hostkey.bytes);
1410 MD5Update(&md5c, keystr1, servkey.bytes);
1411 MD5Update(&md5c, pktin.body, 8);
374330e2 1412 MD5Final(session_id, &md5c);
1413
1414 for (i=0; i<32; i++)
1415 session_key[i] = random_byte();
1416
1417 len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
1418
dcbde236 1419 rsabuf = smalloc(len);
374330e2 1420 if (!rsabuf)
1421 fatalbox("Out of memory");
1422
89ee5268 1423 /*
1424 * Verify the host key.
1425 */
1426 {
1427 /*
1428 * First format the key into a string.
1429 */
1430 int len = rsastr_len(&hostkey);
d5859615 1431 char fingerprint[100];
dcbde236 1432 char *keystr = smalloc(len);
89ee5268 1433 if (!keystr)
1434 fatalbox("Out of memory");
1435 rsastr_fmt(keystr, &hostkey);
d5859615 1436 rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey);
d4857987 1437 verify_ssh_host_key(savedhost, savedport, "rsa", keystr, fingerprint);
dcbde236 1438 sfree(keystr);
89ee5268 1439 }
374330e2 1440
1441 for (i=0; i<32; i++) {
1442 rsabuf[i] = session_key[i];
1443 if (i < 16)
1444 rsabuf[i] ^= session_id[i];
1445 }
1446
1447 if (hostkey.bytes > servkey.bytes) {
1448 rsaencrypt(rsabuf, 32, &servkey);
1449 rsaencrypt(rsabuf, servkey.bytes, &hostkey);
1450 } else {
1451 rsaencrypt(rsabuf, 32, &hostkey);
1452 rsaencrypt(rsabuf, hostkey.bytes, &servkey);
1453 }
1454
c5e9c988 1455 logevent("Encrypted session key");
1456
0a3f1d48 1457 switch (cfg.cipher) {
1458 case CIPHER_BLOWFISH: cipher_type = SSH_CIPHER_BLOWFISH; break;
1459 case CIPHER_DES: cipher_type = SSH_CIPHER_DES; break;
1460 case CIPHER_3DES: cipher_type = SSH_CIPHER_3DES; break;
1461 case CIPHER_AES:
1408a877 1462 c_write_str("AES not supported in SSH1, falling back to 3DES\r\n");
0a3f1d48 1463 cipher_type = SSH_CIPHER_3DES;
1464 break;
1465 }
bea1ef5f 1466 if ((supported_ciphers_mask & (1 << cipher_type)) == 0) {
1408a877 1467 c_write_str("Selected cipher not supported, falling back to 3DES\r\n");
bea1ef5f 1468 cipher_type = SSH_CIPHER_3DES;
1469 }
c5e9c988 1470 switch (cipher_type) {
1471 case SSH_CIPHER_3DES: logevent("Using 3DES encryption"); break;
1472 case SSH_CIPHER_DES: logevent("Using single-DES encryption"); break;
1473 case SSH_CIPHER_BLOWFISH: logevent("Using Blowfish encryption"); break;
1474 }
bea1ef5f 1475
e5574168 1476 send_packet(SSH1_CMSG_SESSION_KEY,
fb09bf1c 1477 PKT_CHAR, cipher_type,
1478 PKT_DATA, cookie, 8,
1479 PKT_CHAR, (len*8) >> 8, PKT_CHAR, (len*8) & 0xFF,
1480 PKT_DATA, rsabuf, len,
1481 PKT_INT, 0,
1482 PKT_END);
1483
c5e9c988 1484 logevent("Trying to enable encryption...");
374330e2 1485
dcbde236 1486 sfree(rsabuf);
374330e2 1487
5e8358ad 1488 cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 :
9697bfd2 1489 cipher_type == SSH_CIPHER_DES ? &ssh_des :
bea1ef5f 1490 &ssh_3des;
374330e2 1491 cipher->sesskey(session_key);
1492
fb09bf1c 1493 crWaitUntil(ispkt);
374330e2 1494
8d5de777 1495 if (pktin.type != SSH1_SMSG_SUCCESS) {
1496 bombout(("Encryption not successfully enabled"));
1497 crReturn(0);
1498 }
374330e2 1499
c5e9c988 1500 logevent("Successfully started encryption");
1501
374330e2 1502 fflush(stdout);
1503 {
374330e2 1504 static int pos = 0;
1505 static char c;
67779be7 1506 if ((flags & FLAG_INTERACTIVE) && !*cfg.username) {
fa17a66e 1507 if (ssh_get_line) {
1508 if (!ssh_get_line("login as: ",
1509 username, sizeof(username), FALSE)) {
1510 /*
1511 * get_line failed to get a username.
1512 * Terminate.
1513 */
1514 logevent("No username provided. Abandoning session.");
1515 ssh_state = SSH_STATE_CLOSED;
1516 crReturn(1);
1517 }
1518 } else {
1519 c_write_str("login as: ");
1520 ssh_send_ok = 1;
1521 while (pos >= 0) {
1522 crWaitUntil(!ispkt);
1523 while (inlen--) switch (c = *in++) {
1524 case 10: case 13:
1525 username[pos] = 0;
1526 pos = -1;
1527 break;
1528 case 8: case 127:
1529 if (pos > 0) {
1530 c_write_str("\b \b");
1531 pos--;
1532 }
1533 break;
1534 case 21: case 27:
1535 while (pos > 0) {
1536 c_write_str("\b \b");
1537 pos--;
1538 }
1539 break;
1540 case 3: case 4:
1541 random_save_seed();
1542 exit(0);
1543 break;
1544 default:
1545 if (((c >= ' ' && c <= '~') ||
1546 ((unsigned char)c >= 160)) && pos < 40) {
1547 username[pos++] = c;
1548 c_write(&c, 1);
1549 }
1550 break;
1551 }
1552 }
1553 c_write_str("\r\n");
1554 username[strcspn(username, "\n\r")] = '\0';
1555 }
1556 } else {
374330e2 1557 strncpy(username, cfg.username, 99);
1558 username[99] = '\0';
374330e2 1559 }
fb09bf1c 1560
e5574168 1561 send_packet(SSH1_CMSG_USER, PKT_STR, username, PKT_END);
c5e9c988 1562 {
3c8e959b 1563 char userlog[22+sizeof(username)];
c5e9c988 1564 sprintf(userlog, "Sent username \"%s\"", username);
1565 logevent(userlog);
3c8e959b 1566 if (flags & FLAG_INTERACTIVE &&
1567 (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) {
1568 strcat(userlog, "\r\n");
1408a877 1569 c_write_str(userlog);
3c8e959b 1570 }
c5e9c988 1571 }
374330e2 1572 }
1573
fb09bf1c 1574 crWaitUntil(ispkt);
374330e2 1575
7cca0d81 1576 tried_publickey = 0;
1577
e5574168 1578 while (pktin.type == SSH1_SMSG_FAILURE) {
374330e2 1579 static char password[100];
a52f067e 1580 static char prompt[200];
374330e2 1581 static int pos;
1582 static char c;
ccbfb941 1583 static int pwpkt_type;
ccbfb941 1584 /*
1585 * Show password prompt, having first obtained it via a TIS
18515c51 1586 * or CryptoCard exchange if we're doing TIS or CryptoCard
1587 * authentication.
ccbfb941 1588 */
e5574168 1589 pwpkt_type = SSH1_CMSG_AUTH_PASSWORD;
5c58ad2d 1590 if (agent_exists()) {
1591 /*
1592 * Attempt RSA authentication using Pageant.
1593 */
1594 static unsigned char request[5], *response, *p;
1595 static int responselen;
1596 static int i, nkeys;
1597 static int authed = FALSE;
1598 void *r;
1599
1600 logevent("Pageant is running. Requesting keys.");
1601
1602 /* Request the keys held by the agent. */
1603 PUT_32BIT(request, 1);
1983e559 1604 request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES;
5c58ad2d 1605 agent_query(request, 5, &r, &responselen);
1606 response = (unsigned char *)r;
1607 if (response) {
1608 p = response + 5;
1609 nkeys = GET_32BIT(p); p += 4;
6328ea7a 1610 { char buf[64]; sprintf(buf, "Pageant has %d SSH1 keys", nkeys);
5c58ad2d 1611 logevent(buf); }
1612 for (i = 0; i < nkeys; i++) {
1613 static struct RSAKey key;
1614 static Bignum challenge;
b8bb811a 1615 static char *commentp;
1616 static int commentlen;
5c58ad2d 1617
1618 { char buf[64]; sprintf(buf, "Trying Pageant key #%d", i);
1619 logevent(buf); }
1620 p += 4;
1621 p += ssh1_read_bignum(p, &key.exponent);
1622 p += ssh1_read_bignum(p, &key.modulus);
b8bb811a 1623 commentlen = GET_32BIT(p); p += 4;
1624 commentp = p; p += commentlen;
5c58ad2d 1625 send_packet(SSH1_CMSG_AUTH_RSA,
1626 PKT_BIGNUM, key.modulus, PKT_END);
1627 crWaitUntil(ispkt);
1628 if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {
1629 logevent("Key refused");
1630 continue;
1631 }
1632 logevent("Received RSA challenge");
1633 ssh1_read_bignum(pktin.body, &challenge);
1634 {
1635 char *agentreq, *q, *ret;
1636 int len, retlen;
1637 len = 1 + 4; /* message type, bit count */
1638 len += ssh1_bignum_length(key.exponent);
1639 len += ssh1_bignum_length(key.modulus);
1640 len += ssh1_bignum_length(challenge);
1641 len += 16; /* session id */
1642 len += 4; /* response format */
dcbde236 1643 agentreq = smalloc(4 + len);
5c58ad2d 1644 PUT_32BIT(agentreq, len);
1645 q = agentreq + 4;
1983e559 1646 *q++ = SSH1_AGENTC_RSA_CHALLENGE;
5c58ad2d 1647 PUT_32BIT(q, ssh1_bignum_bitcount(key.modulus));
1648 q += 4;
1649 q += ssh1_write_bignum(q, key.exponent);
1650 q += ssh1_write_bignum(q, key.modulus);
1651 q += ssh1_write_bignum(q, challenge);
1652 memcpy(q, session_id, 16); q += 16;
1653 PUT_32BIT(q, 1); /* response format */
1654 agent_query(agentreq, len+4, &ret, &retlen);
dcbde236 1655 sfree(agentreq);
5c58ad2d 1656 if (ret) {
1983e559 1657 if (ret[4] == SSH1_AGENT_RSA_RESPONSE) {
5c58ad2d 1658 logevent("Sending Pageant's response");
1659 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE,
1660 PKT_DATA, ret+5, 16, PKT_END);
dcbde236 1661 sfree(ret);
5c58ad2d 1662 crWaitUntil(ispkt);
1663 if (pktin.type == SSH1_SMSG_SUCCESS) {
1664 logevent("Pageant's response accepted");
c4fa2fce 1665 if (flags & FLAG_VERBOSE) {
1408a877 1666 c_write_str("Authenticated using RSA key \"");
c4fa2fce 1667 c_write(commentp, commentlen);
1408a877 1668 c_write_str("\" from agent\r\n");
c4fa2fce 1669 }
5c58ad2d 1670 authed = TRUE;
1671 } else
1672 logevent("Pageant's response not accepted");
1673 } else {
1674 logevent("Pageant failed to answer challenge");
dcbde236 1675 sfree(ret);
5c58ad2d 1676 }
1677 } else {
1678 logevent("No reply received from Pageant");
1679 }
1680 }
1681 freebn(key.exponent);
1682 freebn(key.modulus);
1683 freebn(challenge);
1684 if (authed)
1685 break;
1686 }
1687 }
1688 if (authed)
1689 break;
1690 }
7cca0d81 1691 if (*cfg.keyfile && !tried_publickey)
1692 pwpkt_type = SSH1_CMSG_AUTH_RSA;
fb09bf1c 1693
a52f067e 1694 if (pktin.type == SSH1_SMSG_FAILURE &&
1695 cfg.try_tis_auth &&
1696 (supported_auths_mask & (1<<SSH1_AUTH_TIS))) {
1697 pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE;
1698 logevent("Requested TIS authentication");
1699 send_packet(SSH1_CMSG_AUTH_TIS, PKT_END);
1700 crWaitUntil(ispkt);
1701 if (pktin.type != SSH1_SMSG_AUTH_TIS_CHALLENGE) {
1702 logevent("TIS authentication declined");
1703 if (flags & FLAG_INTERACTIVE)
1408a877 1704 c_write_str("TIS authentication refused.\r\n");
a52f067e 1705 } else {
1706 int challengelen = ((pktin.body[0] << 24) |
1707 (pktin.body[1] << 16) |
1708 (pktin.body[2] << 8) |
1709 (pktin.body[3]));
1710 logevent("Received TIS challenge");
1711 if (challengelen > sizeof(prompt)-1)
1712 challengelen = sizeof(prompt)-1; /* prevent overrun */
1713 memcpy(prompt, pktin.body+4, challengelen);
1714 prompt[challengelen] = '\0';
1715 }
1716 }
1717 if (pktin.type == SSH1_SMSG_FAILURE &&
1718 cfg.try_tis_auth &&
1719 (supported_auths_mask & (1<<SSH1_AUTH_CCARD))) {
1720 pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE;
1721 logevent("Requested CryptoCard authentication");
1722 send_packet(SSH1_CMSG_AUTH_CCARD, PKT_END);
1723 crWaitUntil(ispkt);
1724 if (pktin.type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) {
1725 logevent("CryptoCard authentication declined");
1408a877 1726 c_write_str("CryptoCard authentication refused.\r\n");
a52f067e 1727 } else {
1728 int challengelen = ((pktin.body[0] << 24) |
1729 (pktin.body[1] << 16) |
1730 (pktin.body[2] << 8) |
1731 (pktin.body[3]));
1732 logevent("Received CryptoCard challenge");
1733 if (challengelen > sizeof(prompt)-1)
1734 challengelen = sizeof(prompt)-1; /* prevent overrun */
1735 memcpy(prompt, pktin.body+4, challengelen);
1736 strncpy(prompt + challengelen, "\r\nResponse : ",
1737 sizeof(prompt)-challengelen);
1738 prompt[sizeof(prompt)-1] = '\0';
1739 }
1740 }
1741 if (pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {
1742 sprintf(prompt, "%.90s@%.90s's password: ",
b585ec46 1743 username, savedhost);
a52f067e 1744 }
1745 if (pwpkt_type == SSH1_CMSG_AUTH_RSA) {
1746 char *comment = NULL;
1747 if (flags & FLAG_VERBOSE)
1408a877 1748 c_write_str("Trying public key authentication.\r\n");
a52f067e 1749 if (!rsakey_encrypted(cfg.keyfile, &comment)) {
1750 if (flags & FLAG_VERBOSE)
1408a877 1751 c_write_str("No passphrase required.\r\n");
a52f067e 1752 goto tryauth;
1753 }
1754 sprintf(prompt, "Passphrase for key \"%.100s\": ", comment);
dcbde236 1755 sfree(comment);
a52f067e 1756 }
1757
fa17a66e 1758 if (ssh_get_line) {
1759 if (!ssh_get_line(prompt, password, sizeof(password), TRUE)) {
85ee8208 1760 /*
fa17a66e 1761 * get_line failed to get a password (for example
1762 * because one was supplied on the command line
1763 * which has already failed to work). Terminate.
85ee8208 1764 */
1765 logevent("No more passwords to try");
1766 ssh_state = SSH_STATE_CLOSED;
1767 crReturn(1);
1768 }
fb09bf1c 1769 } else {
1408a877 1770 c_write_str(prompt);
7cca0d81 1771 pos = 0;
67779be7 1772 ssh_send_ok = 1;
7cca0d81 1773 while (pos >= 0) {
1774 crWaitUntil(!ispkt);
1775 while (inlen--) switch (c = *in++) {
1776 case 10: case 13:
1777 password[pos] = 0;
1778 pos = -1;
1779 break;
1780 case 8: case 127:
1781 if (pos > 0)
1782 pos--;
1783 break;
1784 case 21: case 27:
1785 pos = 0;
1786 break;
1787 case 3: case 4:
1788 random_save_seed();
1789 exit(0);
1790 break;
1791 default:
1792 if (((c >= ' ' && c <= '~') ||
1793 ((unsigned char)c >= 160)) && pos < sizeof(password))
1794 password[pos++] = c;
1795 break;
1796 }
1797 }
1408a877 1798 c_write_str("\r\n");
a52f067e 1799 }
fb09bf1c 1800
7cca0d81 1801 tryauth:
1802 if (pwpkt_type == SSH1_CMSG_AUTH_RSA) {
1803 /*
1804 * Try public key authentication with the specified
1805 * key file.
1806 */
1807 static struct RSAKey pubkey;
1808 static Bignum challenge, response;
1809 static int i;
1810 static unsigned char buffer[32];
1811
1812 tried_publickey = 1;
65a22376 1813 i = loadrsakey(cfg.keyfile, &pubkey, password);
7cca0d81 1814 if (i == 0) {
1408a877 1815 c_write_str("Couldn't load public key from ");
1816 c_write_str(cfg.keyfile);
1817 c_write_str(".\r\n");
7cca0d81 1818 continue; /* go and try password */
1819 }
1820 if (i == -1) {
1408a877 1821 c_write_str("Wrong passphrase.\r\n");
7cca0d81 1822 tried_publickey = 0;
1823 continue; /* try again */
1824 }
1825
1826 /*
1827 * Send a public key attempt.
1828 */
1829 send_packet(SSH1_CMSG_AUTH_RSA,
1830 PKT_BIGNUM, pubkey.modulus, PKT_END);
1831
1832 crWaitUntil(ispkt);
1833 if (pktin.type == SSH1_SMSG_FAILURE) {
1408a877 1834 c_write_str("Server refused our public key.\r\n");
7cca0d81 1835 continue; /* go and try password */
1836 }
8d5de777 1837 if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {
1838 bombout(("Bizarre response to offer of public key"));
1839 crReturn(0);
1840 }
7cca0d81 1841 ssh1_read_bignum(pktin.body, &challenge);
1842 response = rsadecrypt(challenge, &pubkey);
1843 freebn(pubkey.private_exponent); /* burn the evidence */
1844
3709bfe9 1845 for (i = 0; i < 32; i++) {
1846 buffer[i] = bignum_byte(response, 31-i);
7cca0d81 1847 }
1848
1849 MD5Init(&md5c);
1850 MD5Update(&md5c, buffer, 32);
1851 MD5Update(&md5c, session_id, 16);
1852 MD5Final(buffer, &md5c);
1853
1854 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE,
1855 PKT_DATA, buffer, 16, PKT_END);
1856
1857 crWaitUntil(ispkt);
1858 if (pktin.type == SSH1_SMSG_FAILURE) {
4017be6d 1859 if (flags & FLAG_VERBOSE)
1408a877 1860 c_write_str("Failed to authenticate with our public key.\r\n");
7cca0d81 1861 continue; /* go and try password */
1862 } else if (pktin.type != SSH1_SMSG_SUCCESS) {
8d5de777 1863 bombout(("Bizarre response to RSA authentication response"));
1864 crReturn(0);
7cca0d81 1865 }
1866
1867 break; /* we're through! */
1868 } else {
39065bed 1869 if (pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {
1870 /*
1871 * Defence against traffic analysis: we send a
1872 * whole bunch of packets containing strings of
1873 * different lengths. One of these strings is the
1874 * password, in a SSH1_CMSG_AUTH_PASSWORD packet.
1875 * The others are all random data in
1876 * SSH1_MSG_IGNORE packets. This way a passive
1877 * listener can't tell which is the password, and
1878 * hence can't deduce the password length.
1879 *
1880 * Anybody with a password length greater than 16
1881 * bytes is going to have enough entropy in their
1882 * password that a listener won't find it _that_
1883 * much help to know how long it is. So what we'll
1884 * do is:
1885 *
1886 * - if password length < 16, we send 15 packets
1887 * containing string lengths 1 through 15
1888 *
1889 * - otherwise, we let N be the nearest multiple
1890 * of 8 below the password length, and send 8
1891 * packets containing string lengths N through
1892 * N+7. This won't obscure the order of
1893 * magnitude of the password length, but it will
1894 * introduce a bit of extra uncertainty.
7d503c31 1895 *
1896 * A few servers (the old 1.2.18 through 1.2.22)
1897 * can't deal with SSH1_MSG_IGNORE. For these
1898 * servers, we need an alternative defence. We make
1899 * use of the fact that the password is interpreted
1900 * as a C string: so we can append a NUL, then some
1901 * random data.
39065bed 1902 */
7d503c31 1903 if (ssh_remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE) {
1904 char string[64];
1905 char *s;
1906 int len;
1907
1908 len = strlen(password);
1909 if (len < sizeof(string)) {
1910 s = string;
1911 strcpy(string, password);
1912 len++; /* cover the zero byte */
1913 while (len < sizeof(string)) {
1914 string[len++] = (char)random_byte();
1915 }
1916 } else {
1917 s = password;
1918 }
1919 send_packet(pwpkt_type, PKT_INT, len,
1920 PKT_DATA, s, len, PKT_END);
39065bed 1921 } else {
7d503c31 1922 int bottom, top, pwlen, i;
1923 char *randomstr;
1924
1925 pwlen = strlen(password);
1926 if (pwlen < 16) {
98c84eb4 1927 bottom = 0; /* zero length passwords are OK! :-) */
7d503c31 1928 top = 15;
1929 } else {
1930 bottom = pwlen &~ 7;
1931 top = bottom + 7;
1932 }
39065bed 1933
7d503c31 1934 assert(pwlen >= bottom && pwlen <= top);
39065bed 1935
7d503c31 1936 randomstr = smalloc(top+1);
39065bed 1937
7d503c31 1938 for (i = bottom; i <= top; i++) {
1939 if (i == pwlen)
1940 defer_packet(pwpkt_type, PKT_STR, password, PKT_END);
1941 else {
1942 for (j = 0; j < i; j++) {
1943 do {
1944 randomstr[j] = random_byte();
1945 } while (randomstr[j] == '\0');
1946 }
1947 randomstr[i] = '\0';
1948 defer_packet(SSH1_MSG_IGNORE,
1949 PKT_STR, randomstr, PKT_END);
39065bed 1950 }
39065bed 1951 }
7d503c31 1952 ssh_pkt_defersend();
39065bed 1953 }
39065bed 1954 } else {
1955 send_packet(pwpkt_type, PKT_STR, password, PKT_END);
1956 }
7cca0d81 1957 }
c5e9c988 1958 logevent("Sent password");
374330e2 1959 memset(password, 0, strlen(password));
fb09bf1c 1960 crWaitUntil(ispkt);
e5574168 1961 if (pktin.type == SSH1_SMSG_FAILURE) {
4017be6d 1962 if (flags & FLAG_VERBOSE)
1408a877 1963 c_write_str("Access denied\r\n");
c5e9c988 1964 logevent("Authentication refused");
e5574168 1965 } else if (pktin.type == SSH1_MSG_DISCONNECT) {
fb09bf1c 1966 logevent("Received disconnect request");
85ee8208 1967 ssh_state = SSH_STATE_CLOSED;
1968 crReturn(1);
e5574168 1969 } else if (pktin.type != SSH1_SMSG_SUCCESS) {
8d5de777 1970 bombout(("Strange packet received, type %d", pktin.type));
1971 crReturn(0);
374330e2 1972 }
1973 }
1974
c5e9c988 1975 logevent("Authentication successful");
1976
fb09bf1c 1977 crFinish(1);
1978}
1979
9c964e85 1980void sshfwd_close(struct ssh_channel *c) {
783415f8 1981 if (c) {
1982 if (ssh_version == 1) {
1983 send_packet(SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, PKT_END);
1984 } else {
1985 ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
1986 ssh2_pkt_adduint32(c->remoteid);
1987 ssh2_pkt_send();
1988 }
1989 c->closes = 1;
1990 if (c->type == CHAN_X11) {
1991 c->u.x11.s = NULL;
1992 logevent("X11 connection terminated");
1993 }
1994 }
9c964e85 1995}
1996
1997void sshfwd_write(struct ssh_channel *c, char *buf, int len) {
783415f8 1998 if (ssh_version == 1) {
1999 send_packet(SSH1_MSG_CHANNEL_DATA,
2000 PKT_INT, c->remoteid,
2001 PKT_INT, len,
2002 PKT_DATA, buf, len,
2003 PKT_END);
2004 } else {
2005 ssh2_add_channel_data(c, buf, len);
2006 ssh2_try_send(c);
2007 }
9c964e85 2008}
2009
e5574168 2010static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
fb09bf1c 2011 crBegin;
2012
2013 random_init();
2014
e5574168 2015 while (!do_ssh1_login(in, inlen, ispkt)) {
fb09bf1c 2016 crReturnV;
85ee8208 2017 }
2018 if (ssh_state == SSH_STATE_CLOSED)
2019 crReturnV;
fb09bf1c 2020
979310f1 2021 if (cfg.agentfwd && agent_exists()) {
dacbd0e8 2022 logevent("Requesting agent forwarding");
2023 send_packet(SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END);
2024 do { crReturnV; } while (!ispkt);
2025 if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) {
8d5de777 2026 bombout(("Protocol confusion"));
2027 crReturnV;
dacbd0e8 2028 } else if (pktin.type == SSH1_SMSG_FAILURE) {
2029 logevent("Agent forwarding refused");
db7d555c 2030 } else {
dacbd0e8 2031 logevent("Agent forwarding enabled");
db7d555c 2032 ssh_agentfwd_enabled = TRUE;
2033 }
dacbd0e8 2034 }
2035
9c964e85 2036 if (cfg.x11_forward) {
2037 char proto[20], data[64];
2038 logevent("Requesting X11 forwarding");
2039 x11_invent_auth(proto, sizeof(proto), data, sizeof(data));
2040 send_packet(SSH1_CMSG_X11_REQUEST_FORWARDING,
2041 PKT_STR, proto, PKT_STR, data,
2042 PKT_INT, 0,
2043 PKT_END);
2044 do { crReturnV; } while (!ispkt);
2045 if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) {
2046 bombout(("Protocol confusion"));
2047 crReturnV;
2048 } else if (pktin.type == SSH1_SMSG_FAILURE) {
2049 logevent("X11 forwarding refused");
2050 } else {
2051 logevent("X11 forwarding enabled");
2052 ssh_X11_fwd_enabled = TRUE;
2053 }
2054 }
2055
fef97f43 2056 if (!cfg.nopty) {
e5574168 2057 send_packet(SSH1_CMSG_REQUEST_PTY,
fb09bf1c 2058 PKT_STR, cfg.termtype,
2059 PKT_INT, rows, PKT_INT, cols,
2060 PKT_INT, 0, PKT_INT, 0,
2061 PKT_CHAR, 0,
2062 PKT_END);
fef97f43 2063 ssh_state = SSH_STATE_INTERMED;
2064 do { crReturnV; } while (!ispkt);
e5574168 2065 if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) {
8d5de777 2066 bombout(("Protocol confusion"));
2067 crReturnV;
e5574168 2068 } else if (pktin.type == SSH1_SMSG_FAILURE) {
1408a877 2069 c_write_str("Server refused to allocate pty\r\n");
0965bee0 2070 ssh_editing = ssh_echoing = 1;
fef97f43 2071 }
c5e9c988 2072 logevent("Allocated pty");
0965bee0 2073 } else {
2074 ssh_editing = ssh_echoing = 1;
374330e2 2075 }
2076
4ba9b64b 2077 if (cfg.compression) {
2078 send_packet(SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END);
2079 do { crReturnV; } while (!ispkt);
2080 if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) {
2081 bombout(("Protocol confusion"));
2082 crReturnV;
2083 } else if (pktin.type == SSH1_SMSG_FAILURE) {
1408a877 2084 c_write_str("Server refused to compress\r\n");
4ba9b64b 2085 }
2086 logevent("Started compression");
2087 ssh1_compressing = TRUE;
2088 zlib_compress_init();
2089 zlib_decompress_init();
2090 }
2091
6abbf9e3 2092 if (*cfg.remote_cmd)
2093 send_packet(SSH1_CMSG_EXEC_CMD, PKT_STR, cfg.remote_cmd, PKT_END);
2094 else
2095 send_packet(SSH1_CMSG_EXEC_SHELL, PKT_END);
c5e9c988 2096 logevent("Started session");
374330e2 2097
2098 ssh_state = SSH_STATE_SESSION;
2099 if (size_needed)
2100 ssh_size();
3687d221 2101 if (eof_needed)
2102 ssh_special(TS_EOF);
374330e2 2103
0965bee0 2104 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
8ccc75b0 2105 ssh_send_ok = 1;
dacbd0e8 2106 ssh_channels = newtree234(ssh_channelcmp);
374330e2 2107 while (1) {
2108 crReturnV;
2109 if (ispkt) {
e5574168 2110 if (pktin.type == SSH1_SMSG_STDOUT_DATA ||
2111 pktin.type == SSH1_SMSG_STDERR_DATA) {
fb09bf1c 2112 long len = GET_32BIT(pktin.body);
fe50e814 2113 from_backend(pktin.type == SSH1_SMSG_STDERR_DATA,
2114 pktin.body+4, len);
e5574168 2115 } else if (pktin.type == SSH1_MSG_DISCONNECT) {
21248260 2116 ssh_state = SSH_STATE_CLOSED;
c5e9c988 2117 logevent("Received disconnect request");
3257deae 2118 crReturnV;
9c964e85 2119 } else if (pktin.type == SSH1_SMSG_X11_OPEN) {
2120 /* Remote side is trying to open a channel to talk to our
2121 * X-Server. Give them back a local channel number. */
2122 unsigned i;
2123 struct ssh_channel *c, *d;
2124 enum234 e;
2125
2126 logevent("Received X11 connect request");
2127 /* Refuse if X11 forwarding is disabled. */
2128 if (!ssh_X11_fwd_enabled) {
2129 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE,
2130 PKT_INT, GET_32BIT(pktin.body),
2131 PKT_END);
2132 logevent("Rejected X11 connect request");
2133 } else {
9c964e85 2134 c = smalloc(sizeof(struct ssh_channel));
2135
0c6c9a70 2136 if ( x11_init(&c->u.x11.s, cfg.x11_display, c) != NULL ) {
9c964e85 2137 logevent("opening X11 forward connection failed");
2138 sfree(c);
2139 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE,
2140 PKT_INT, GET_32BIT(pktin.body),
2141 PKT_END);
2142 } else {
2143 logevent("opening X11 forward connection succeeded");
2144 for (i=1, d = first234(ssh_channels, &e); d; d = next234(&e)) {
783415f8 2145 if (d->localid > i)
2146 break; /* found a free number */
2147 i = d->localid + 1;
9c964e85 2148 }
2149 c->remoteid = GET_32BIT(pktin.body);
2150 c->localid = i;
2151 c->closes = 0;
783415f8 2152 c->type = CHAN_X11; /* identify channel type */
9c964e85 2153 add234(ssh_channels, c);
2154 send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
2155 PKT_INT, c->remoteid, PKT_INT, c->localid,
2156 PKT_END);
2157 logevent("Opened X11 forward channel");
2158 }
2159 }
dacbd0e8 2160 } else if (pktin.type == SSH1_SMSG_AGENT_OPEN) {
2161 /* Remote side is trying to open a channel to talk to our
2162 * agent. Give them back a local channel number. */
db7d555c 2163 unsigned i;
dacbd0e8 2164 struct ssh_channel *c;
2165 enum234 e;
db7d555c 2166
2167 /* Refuse if agent forwarding is disabled. */
2168 if (!ssh_agentfwd_enabled) {
2169 send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE,
2170 PKT_INT, GET_32BIT(pktin.body),
2171 PKT_END);
2172 } else {
2173 i = 1;
2174 for (c = first234(ssh_channels, &e); c; c = next234(&e)) {
2175 if (c->localid > i)
2176 break; /* found a free number */
2177 i = c->localid + 1;
2178 }
dcbde236 2179 c = smalloc(sizeof(struct ssh_channel));
db7d555c 2180 c->remoteid = GET_32BIT(pktin.body);
2181 c->localid = i;
2182 c->closes = 0;
783415f8 2183 c->type = CHAN_AGENT; /* identify channel type */
db7d555c 2184 c->u.a.lensofar = 0;
2185 add234(ssh_channels, c);
2186 send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,
2187 PKT_INT, c->remoteid, PKT_INT, c->localid,
2188 PKT_END);
2189 }
2190 } else if (pktin.type == SSH1_MSG_CHANNEL_CLOSE ||
2191 pktin.type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION) {
dacbd0e8 2192 /* Remote side closes a channel. */
d211621f 2193 unsigned i = GET_32BIT(pktin.body);
dacbd0e8 2194 struct ssh_channel *c;
2195 c = find234(ssh_channels, &i, ssh_channelfind);
2196 if (c) {
2197 int closetype;
2198 closetype = (pktin.type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2);
2199 send_packet(pktin.type, PKT_INT, c->remoteid, PKT_END);
783415f8 2200 if ((c->closes == 0) && (c->type == CHAN_X11)) {
9c964e85 2201 logevent("X11 connection closed");
2202 assert(c->u.x11.s != NULL);
2203 x11_close(c->u.x11.s);
2204 c->u.x11.s = NULL;
2205 }
dacbd0e8 2206 c->closes |= closetype;
2207 if (c->closes == 3) {
2208 del234(ssh_channels, c);
dcbde236 2209 sfree(c);
dacbd0e8 2210 }
2211 }
2212 } else if (pktin.type == SSH1_MSG_CHANNEL_DATA) {
2213 /* Data sent down one of our channels. */
2214 int i = GET_32BIT(pktin.body);
2215 int len = GET_32BIT(pktin.body+4);
2216 unsigned char *p = pktin.body+8;
2217 struct ssh_channel *c;
2218 c = find234(ssh_channels, &i, ssh_channelfind);
2219 if (c) {
2220 switch(c->type) {
783415f8 2221 case CHAN_X11:
9c964e85 2222 x11_send(c->u.x11.s, p, len);
2223 break;
783415f8 2224 case CHAN_AGENT:
dacbd0e8 2225 /* Data for an agent message. Buffer it. */
2226 while (len > 0) {
2227 if (c->u.a.lensofar < 4) {
2228 int l = min(4 - c->u.a.lensofar, len);
2229 memcpy(c->u.a.msglen + c->u.a.lensofar, p, l);
2230 p += l; len -= l; c->u.a.lensofar += l;
2231 }
2232 if (c->u.a.lensofar == 4) {
2233 c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
dcbde236 2234 c->u.a.message = smalloc(c->u.a.totallen);
dacbd0e8 2235 memcpy(c->u.a.message, c->u.a.msglen, 4);
2236 }
2237 if (c->u.a.lensofar >= 4 && len > 0) {
2238 int l = min(c->u.a.totallen - c->u.a.lensofar, len);
2239 memcpy(c->u.a.message + c->u.a.lensofar, p, l);
2240 p += l; len -= l; c->u.a.lensofar += l;
2241 }
2242 if (c->u.a.lensofar == c->u.a.totallen) {
2243 void *reply, *sentreply;
2244 int replylen;
2245 agent_query(c->u.a.message, c->u.a.totallen,
2246 &reply, &replylen);
2247 if (reply)
2248 sentreply = reply;
2249 else {
2250 /* Fake SSH_AGENT_FAILURE. */
2251 sentreply = "\0\0\0\1\5";
2252 replylen = 5;
2253 }
2254 send_packet(SSH1_MSG_CHANNEL_DATA,
2255 PKT_INT, c->remoteid,
2256 PKT_INT, replylen,
2257 PKT_DATA, sentreply, replylen,
2258 PKT_END);
2259 if (reply)
dcbde236 2260 sfree(reply);
2261 sfree(c->u.a.message);
dacbd0e8 2262 c->u.a.lensofar = 0;
2263 }
2264 }
2265 break;
2266 }
2267 }
e5574168 2268 } else if (pktin.type == SSH1_SMSG_SUCCESS) {
972a41c8 2269 /* may be from EXEC_SHELL on some servers */
e5574168 2270 } else if (pktin.type == SSH1_SMSG_FAILURE) {
972a41c8 2271 /* may be from EXEC_SHELL on some servers
374330e2 2272 * if no pty is available or in other odd cases. Ignore */
e5574168 2273 } else if (pktin.type == SSH1_SMSG_EXIT_STATUS) {
2274 send_packet(SSH1_CMSG_EXIT_CONFIRMATION, PKT_END);
374330e2 2275 } else {
8d5de777 2276 bombout(("Strange packet received: type %d", pktin.type));
2277 crReturnV;
374330e2 2278 }
2279 } else {
8df7a775 2280 while (inlen > 0) {
2281 int len = min(inlen, 512);
2282 send_packet(SSH1_CMSG_STDIN_DATA,
2283 PKT_INT, len, PKT_DATA, in, len, PKT_END);
2284 in += len;
2285 inlen -= len;
2286 }
374330e2 2287 }
2288 }
2289
2290 crFinishV;
2291}
2292
2293/*
e5574168 2294 * Utility routine for decoding comma-separated strings in KEXINIT.
2295 */
75cab814 2296static int in_commasep_string(char *needle, char *haystack, int haylen) {
e5574168 2297 int needlen = strlen(needle);
2298 while (1) {
2299 /*
2300 * Is it at the start of the string?
2301 */
2302 if (haylen >= needlen && /* haystack is long enough */
2303 !memcmp(needle, haystack, needlen) && /* initial match */
2304 (haylen == needlen || haystack[needlen] == ',')
2305 /* either , or EOS follows */
2306 )
2307 return 1;
2308 /*
2309 * If not, search for the next comma and resume after that.
2310 * If no comma found, terminate.
2311 */
2312 while (haylen > 0 && *haystack != ',')
2313 haylen--, haystack++;
2314 if (haylen == 0)
2315 return 0;
2316 haylen--, haystack++; /* skip over comma itself */
2317 }
2318}
2319
2320/*
d39f364a 2321 * SSH2 key creation method.
2322 */
5e0d7cb8 2323static void ssh2_mkkey(Bignum K, char *H, char *sessid, char chr, char *keyspace) {
d39f364a 2324 SHA_State s;
2325 /* First 20 bytes. */
2326 SHA_Init(&s);
2327 sha_mpint(&s, K);
2328 SHA_Bytes(&s, H, 20);
2329 SHA_Bytes(&s, &chr, 1);
5e0d7cb8 2330 SHA_Bytes(&s, sessid, 20);
d39f364a 2331 SHA_Final(&s, keyspace);
2332 /* Next 20 bytes. */
2333 SHA_Init(&s);
2334 sha_mpint(&s, K);
2335 SHA_Bytes(&s, H, 20);
2336 SHA_Bytes(&s, keyspace, 20);
2337 SHA_Final(&s, keyspace+20);
2338}
2339
2340/*
7cca0d81 2341 * Handle the SSH2 transport layer.
e5574168 2342 */
7cca0d81 2343static int do_ssh2_transport(unsigned char *in, int inlen, int ispkt)
e5574168 2344{
7bd5a860 2345 static int i, j, len, nbits, pbits;
e5574168 2346 static char *str;
a92dd380 2347 static Bignum p, g, e, f, K;
2348 static int kex_init_value, kex_reply_value;
7591b9ff 2349 static const struct ssh_mac **maclist;
8b2715b2 2350 static int nmacs;
0a3f1d48 2351 static const struct ssh2_cipher *cscipher_tobe = NULL;
2352 static const struct ssh2_cipher *sccipher_tobe = NULL;
57476f6b 2353 static const struct ssh_mac *csmac_tobe = NULL;
2354 static const struct ssh_mac *scmac_tobe = NULL;
2355 static const struct ssh_compress *cscomp_tobe = NULL;
2356 static const struct ssh_compress *sccomp_tobe = NULL;
d5859615 2357 static char *hostkeydata, *sigdata, *keystr, *fingerprint;
e5574168 2358 static int hostkeylen, siglen;
e055a386 2359 static void *hkey; /* actual host key */
e5574168 2360 static unsigned char exchange_hash[20];
d39f364a 2361 static unsigned char keyspace[40];
0a3f1d48 2362 static const struct ssh2_ciphers *preferred_cipher;
4ba9b64b 2363 static const struct ssh_compress *preferred_comp;
0db56f73 2364 static int first_kex;
e5574168 2365
2366 crBegin;
7cca0d81 2367 random_init();
0db56f73 2368 first_kex = 1;
e5574168 2369
5e8358ad 2370 /*
4ba9b64b 2371 * Set up the preferred cipher and compression.
5e8358ad 2372 */
2373 if (cfg.cipher == CIPHER_BLOWFISH) {
0a3f1d48 2374 preferred_cipher = &ssh2_blowfish;
5e8358ad 2375 } else if (cfg.cipher == CIPHER_DES) {
2376 logevent("Single DES not supported in SSH2; using 3DES");
0a3f1d48 2377 preferred_cipher = &ssh2_3des;
5e8358ad 2378 } else if (cfg.cipher == CIPHER_3DES) {
0a3f1d48 2379 preferred_cipher = &ssh2_3des;
2380 } else if (cfg.cipher == CIPHER_AES) {
2381 preferred_cipher = &ssh2_aes;
5e8358ad 2382 } else {
2383 /* Shouldn't happen, but we do want to initialise to _something_. */
0a3f1d48 2384 preferred_cipher = &ssh2_3des;
5e8358ad 2385 }
4ba9b64b 2386 if (cfg.compression)
2387 preferred_comp = &ssh_zlib;
2388 else
2389 preferred_comp = &ssh_comp_none;
5e8358ad 2390
7591b9ff 2391 /*
2392 * Be prepared to work around the buggy MAC problem.
2393 */
7d503c31 2394 if (cfg.buggymac || (ssh_remote_bugs & BUG_SSH2_HMAC))
8b2715b2 2395 maclist = buggymacs, nmacs = lenof(buggymacs);
7591b9ff 2396 else
8b2715b2 2397 maclist = macs, nmacs = lenof(macs);
7591b9ff 2398
7cca0d81 2399 begin_key_exchange:
e5574168 2400 /*
2401 * Construct and send our key exchange packet.
2402 */
2403 ssh2_pkt_init(SSH2_MSG_KEXINIT);
2404 for (i = 0; i < 16; i++)
2405 ssh2_pkt_addbyte((unsigned char)random_byte());
2406 /* List key exchange algorithms. */
2407 ssh2_pkt_addstring_start();
2408 for (i = 0; i < lenof(kex_algs); i++) {
2409 ssh2_pkt_addstring_str(kex_algs[i]->name);
2410 if (i < lenof(kex_algs)-1)
2411 ssh2_pkt_addstring_str(",");
2412 }
2413 /* List server host key algorithms. */
2414 ssh2_pkt_addstring_start();
2415 for (i = 0; i < lenof(hostkey_algs); i++) {
2416 ssh2_pkt_addstring_str(hostkey_algs[i]->name);
2417 if (i < lenof(hostkey_algs)-1)
2418 ssh2_pkt_addstring_str(",");
2419 }
2420 /* List client->server encryption algorithms. */
2421 ssh2_pkt_addstring_start();
729e54b4 2422 for (i = 0; i < lenof(ciphers)+1; i++) {
0a3f1d48 2423 const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1];
2424 for (j = 0; j < c->nciphers; j++) {
2425 ssh2_pkt_addstring_str(c->list[j]->name);
2426 if (i < lenof(ciphers) || j < c->nciphers-1)
2427 ssh2_pkt_addstring_str(",");
2428 }
e5574168 2429 }
2430 /* List server->client encryption algorithms. */
2431 ssh2_pkt_addstring_start();
729e54b4 2432 for (i = 0; i < lenof(ciphers)+1; i++) {
0a3f1d48 2433 const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1];
2434 for (j = 0; j < c->nciphers; j++) {
2435 ssh2_pkt_addstring_str(c->list[j]->name);
2436 if (i < lenof(ciphers) || j < c->nciphers-1)
2437 ssh2_pkt_addstring_str(",");
2438 }
e5574168 2439 }
2440 /* List client->server MAC algorithms. */
2441 ssh2_pkt_addstring_start();
8b2715b2 2442 for (i = 0; i < nmacs; i++) {
7591b9ff 2443 ssh2_pkt_addstring_str(maclist[i]->name);
8b2715b2 2444 if (i < nmacs-1)
e5574168 2445 ssh2_pkt_addstring_str(",");
2446 }
2447 /* List server->client MAC algorithms. */
2448 ssh2_pkt_addstring_start();
8b2715b2 2449 for (i = 0; i < nmacs; i++) {
7591b9ff 2450 ssh2_pkt_addstring_str(maclist[i]->name);
8b2715b2 2451 if (i < nmacs-1)
e5574168 2452 ssh2_pkt_addstring_str(",");
2453 }
2454 /* List client->server compression algorithms. */
2455 ssh2_pkt_addstring_start();
4ba9b64b 2456 for (i = 0; i < lenof(compressions)+1; i++) {
2457 const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1];
2458 ssh2_pkt_addstring_str(c->name);
2459 if (i < lenof(compressions))
e5574168 2460 ssh2_pkt_addstring_str(",");
2461 }
2462 /* List server->client compression algorithms. */
2463 ssh2_pkt_addstring_start();
4ba9b64b 2464 for (i = 0; i < lenof(compressions)+1; i++) {
2465 const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1];
2466 ssh2_pkt_addstring_str(c->name);
2467 if (i < lenof(compressions))
e5574168 2468 ssh2_pkt_addstring_str(",");
2469 }
2470 /* List client->server languages. Empty list. */
2471 ssh2_pkt_addstring_start();
2472 /* List server->client languages. Empty list. */
2473 ssh2_pkt_addstring_start();
2474 /* First KEX packet does _not_ follow, because we're not that brave. */
2475 ssh2_pkt_addbool(FALSE);
2476 /* Reserved. */
2477 ssh2_pkt_adduint32(0);
0db56f73 2478
2479 exhash = exhashbase;
e5574168 2480 sha_string(&exhash, pktout.data+5, pktout.length-5);
0db56f73 2481
e5574168 2482 ssh2_pkt_send();
2483
2484 if (!ispkt) crWaitUntil(ispkt);
2485 sha_string(&exhash, pktin.data+5, pktin.length-5);
2486
2487 /*
2488 * Now examine the other side's KEXINIT to see what we're up
2489 * to.
2490 */
7cca0d81 2491 if (pktin.type != SSH2_MSG_KEXINIT) {
8d5de777 2492 bombout(("expected key exchange packet from server"));
2493 crReturn(0);
7cca0d81 2494 }
e5574168 2495 kex = NULL; hostkey = NULL; cscipher_tobe = NULL; sccipher_tobe = NULL;
2496 csmac_tobe = NULL; scmac_tobe = NULL; cscomp_tobe = NULL; sccomp_tobe = NULL;
2497 pktin.savedpos += 16; /* skip garbage cookie */
2498 ssh2_pkt_getstring(&str, &len); /* key exchange algorithms */
2499 for (i = 0; i < lenof(kex_algs); i++) {
2500 if (in_commasep_string(kex_algs[i]->name, str, len)) {
2501 kex = kex_algs[i];
2502 break;
2503 }
2504 }
2505 ssh2_pkt_getstring(&str, &len); /* host key algorithms */
2506 for (i = 0; i < lenof(hostkey_algs); i++) {
2507 if (in_commasep_string(hostkey_algs[i]->name, str, len)) {
2508 hostkey = hostkey_algs[i];
2509 break;
2510 }
2511 }
2512 ssh2_pkt_getstring(&str, &len); /* client->server cipher */
729e54b4 2513 for (i = 0; i < lenof(ciphers)+1; i++) {
0a3f1d48 2514 const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1];
2515 for (j = 0; j < c->nciphers; j++) {
2516 if (in_commasep_string(c->list[j]->name, str, len)) {
2517 cscipher_tobe = c->list[j];
2518 break;
2519 }
e5574168 2520 }
0a3f1d48 2521 if (cscipher_tobe)
2522 break;
e5574168 2523 }
2524 ssh2_pkt_getstring(&str, &len); /* server->client cipher */
729e54b4 2525 for (i = 0; i < lenof(ciphers)+1; i++) {
0a3f1d48 2526 const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1];
2527 for (j = 0; j < c->nciphers; j++) {
2528 if (in_commasep_string(c->list[j]->name, str, len)) {
2529 sccipher_tobe = c->list[j];
2530 break;
2531 }
e5574168 2532 }
0a3f1d48 2533 if (sccipher_tobe)
2534 break;
e5574168 2535 }
2536 ssh2_pkt_getstring(&str, &len); /* client->server mac */
8b2715b2 2537 for (i = 0; i < nmacs; i++) {
7591b9ff 2538 if (in_commasep_string(maclist[i]->name, str, len)) {
2539 csmac_tobe = maclist[i];
e5574168 2540 break;
2541 }
2542 }
2543 ssh2_pkt_getstring(&str, &len); /* server->client mac */
8b2715b2 2544 for (i = 0; i < nmacs; i++) {
7591b9ff 2545 if (in_commasep_string(maclist[i]->name, str, len)) {
2546 scmac_tobe = maclist[i];
e5574168 2547 break;
2548 }
2549 }
2550 ssh2_pkt_getstring(&str, &len); /* client->server compression */
4ba9b64b 2551 for (i = 0; i < lenof(compressions)+1; i++) {
2552 const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1];
2553 if (in_commasep_string(c->name, str, len)) {
2554 cscomp_tobe = c;
e5574168 2555 break;
2556 }
2557 }
2558 ssh2_pkt_getstring(&str, &len); /* server->client compression */
4ba9b64b 2559 for (i = 0; i < lenof(compressions)+1; i++) {
2560 const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1];
2561 if (in_commasep_string(c->name, str, len)) {
2562 sccomp_tobe = c;
e5574168 2563 break;
2564 }
2565 }
e5574168 2566
2567 /*
7bd5a860 2568 * Work out the number of bits of key we will need from the key
2569 * exchange. We start with the maximum key length of either
2570 * cipher...
2571 */
2572 {
2573 int csbits, scbits;
2574
2575 csbits = cscipher_tobe->keylen;
2576 scbits = sccipher_tobe->keylen;
2577 nbits = (csbits > scbits ? csbits : scbits);
2578 }
2579 /* The keys only have 160-bit entropy, since they're based on
2580 * a SHA-1 hash. So cap the key size at 160 bits. */
2581 if (nbits > 160) nbits = 160;
2582
2583 /*
a92dd380 2584 * If we're doing Diffie-Hellman group exchange, start by
2585 * requesting a group.
e5574168 2586 */
a92dd380 2587 if (kex == &ssh_diffiehellman_gex) {
a92dd380 2588 logevent("Doing Diffie-Hellman group exchange");
2589 /*
7bd5a860 2590 * Work out how big a DH group we will need to allow that
2591 * much data.
2592 */
2593 pbits = 512 << ((nbits-1) / 64);
a92dd380 2594 ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST);
7bd5a860 2595 ssh2_pkt_adduint32(pbits);
a92dd380 2596 ssh2_pkt_send();
2597
2598 crWaitUntil(ispkt);
2599 if (pktin.type != SSH2_MSG_KEX_DH_GEX_GROUP) {
2600 bombout(("expected key exchange group packet from server"));
2601 crReturn(0);
2602 }
2603 p = ssh2_pkt_getmp();
2604 g = ssh2_pkt_getmp();
2605 dh_setup_group(p, g);
2606 kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT;
2607 kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY;
2608 } else {
2609 dh_setup_group1();
2610 kex_init_value = SSH2_MSG_KEXDH_INIT;
2611 kex_reply_value = SSH2_MSG_KEXDH_REPLY;
8d5de777 2612 }
e5574168 2613
a92dd380 2614 logevent("Doing Diffie-Hellman key exchange");
e5574168 2615 /*
a92dd380 2616 * Now generate and send e for Diffie-Hellman.
e5574168 2617 */
7bd5a860 2618 e = dh_create_e(nbits*2);
a92dd380 2619 ssh2_pkt_init(kex_init_value);
e5574168 2620 ssh2_pkt_addmp(e);
2621 ssh2_pkt_send();
2622
2623 crWaitUntil(ispkt);
a92dd380 2624 if (pktin.type != kex_reply_value) {
2625 bombout(("expected key exchange reply packet from server"));
8d5de777 2626 crReturn(0);
7cca0d81 2627 }
e5574168 2628 ssh2_pkt_getstring(&hostkeydata, &hostkeylen);
2629 f = ssh2_pkt_getmp();
e5574168 2630 ssh2_pkt_getstring(&sigdata, &siglen);
2631
2632 K = dh_find_K(f);
e5574168 2633
2634 sha_string(&exhash, hostkeydata, hostkeylen);
a92dd380 2635 if (kex == &ssh_diffiehellman_gex) {
7bd5a860 2636 sha_uint32(&exhash, pbits);
a92dd380 2637 sha_mpint(&exhash, p);
2638 sha_mpint(&exhash, g);
2639 }
e5574168 2640 sha_mpint(&exhash, e);
2641 sha_mpint(&exhash, f);
2642 sha_mpint(&exhash, K);
2643 SHA_Final(&exhash, exchange_hash);
2644
3709bfe9 2645 dh_cleanup();
2646
7cca0d81 2647#if 0
e5574168 2648 debug(("Exchange hash is:\r\n"));
2649 for (i = 0; i < 20; i++)
2650 debug((" %02x", exchange_hash[i]));
2651 debug(("\r\n"));
7cca0d81 2652#endif
2653
e055a386 2654 hkey = hostkey->newkey(hostkeydata, hostkeylen);
2655 if (!hostkey->verifysig(hkey, sigdata, siglen, exchange_hash, 20)) {
8d5de777 2656 bombout(("Server failed host key check"));
2657 crReturn(0);
2658 }
e5574168 2659
2660 /*
7cca0d81 2661 * Expect SSH2_MSG_NEWKEYS from server.
d39f364a 2662 */
7cca0d81 2663 crWaitUntil(ispkt);
8d5de777 2664 if (pktin.type != SSH2_MSG_NEWKEYS) {
2665 bombout(("expected new-keys packet from server"));
2666 crReturn(0);
2667 }
d39f364a 2668
2669 /*
7cca0d81 2670 * Authenticate remote host: verify host key. (We've already
2671 * checked the signature of the exchange hash.)
e5574168 2672 */
e055a386 2673 keystr = hostkey->fmtkey(hkey);
2674 fingerprint = hostkey->fingerprint(hkey);
d4857987 2675 verify_ssh_host_key(savedhost, savedport, hostkey->keytype,
2676 keystr, fingerprint);
a92dd380 2677 if (first_kex) { /* don't bother logging this in rekeys */
5e0d7cb8 2678 logevent("Host key fingerprint is:");
2679 logevent(fingerprint);
2680 }
dcbde236 2681 sfree(fingerprint);
2682 sfree(keystr);
e055a386 2683 hostkey->freekey(hkey);
d39f364a 2684
2685 /*
7cca0d81 2686 * Send SSH2_MSG_NEWKEYS.
d39f364a 2687 */
2688 ssh2_pkt_init(SSH2_MSG_NEWKEYS);
2689 ssh2_pkt_send();
d39f364a 2690
2691 /*
2692 * Create and initialise session keys.
2693 */
2694 cscipher = cscipher_tobe;
2695 sccipher = sccipher_tobe;
2696 csmac = csmac_tobe;
2697 scmac = scmac_tobe;
2698 cscomp = cscomp_tobe;
2699 sccomp = sccomp_tobe;
4ba9b64b 2700 cscomp->compress_init();
2701 sccomp->decompress_init();
d39f364a 2702 /*
5e0d7cb8 2703 * Set IVs after keys. Here we use the exchange hash from the
2704 * _first_ key exchange.
d39f364a 2705 */
5e0d7cb8 2706 if (first_kex)
65a22376 2707 memcpy(ssh2_session_id, exchange_hash, sizeof(exchange_hash));
2708 ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'C', keyspace);
5e0d7cb8 2709 cscipher->setcskey(keyspace);
65a22376 2710 ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'D', keyspace);
5e0d7cb8 2711 sccipher->setsckey(keyspace);
65a22376 2712 ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'A', keyspace);
5e0d7cb8 2713 cscipher->setcsiv(keyspace);
65a22376 2714 ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'B', keyspace);
5e0d7cb8 2715 sccipher->setsciv(keyspace);
65a22376 2716 ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'E', keyspace);
5e0d7cb8 2717 csmac->setcskey(keyspace);
65a22376 2718 ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'F', keyspace);
5e0d7cb8 2719 scmac->setsckey(keyspace);
d39f364a 2720
033b4cef 2721 /*
0db56f73 2722 * If this is the first key exchange phase, we must pass the
2723 * SSH2_MSG_NEWKEYS packet to the next layer, not because it
2724 * wants to see it but because it will need time to initialise
2725 * itself before it sees an actual packet. In subsequent key
2726 * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because
2727 * it would only confuse the layer above.
2728 */
2729 if (!first_kex) {
2730 crReturn(0);
2731 }
2732 first_kex = 0;
2733
2734 /*
7cca0d81 2735 * Now we're encrypting. Begin returning 1 to the protocol main
2736 * function so that other things can run on top of the
2737 * transport. If we ever see a KEXINIT, we must go back to the
2738 * start.
033b4cef 2739 */
e96adf72 2740 while (!(ispkt && pktin.type == SSH2_MSG_KEXINIT)) {
7cca0d81 2741 crReturn(1);
e96adf72 2742 }
5e0d7cb8 2743 logevent("Server initiated key re-exchange");
7cca0d81 2744 goto begin_key_exchange;
e5574168 2745
2746 crFinish(1);
2747}
2748
7cca0d81 2749/*
783415f8 2750 * Add data to an SSH2 channel output buffer.
2751 */
2752static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len) {
2753 if (c->v2.outbufsize <
2754 c->v2.outbuflen + len) {
2755 c->v2.outbufsize =
2756 c->v2.outbuflen + len + 1024;
2757 c->v2.outbuffer = srealloc(c->v2.outbuffer,
2758 c->v2.outbufsize);
2759 }
2760 memcpy(c->v2.outbuffer + c->v2.outbuflen,
2761 buf, len);
2762 c->v2.outbuflen += len;
2763}
2764
2765/*
2766 * Attempt to send data on an SSH2 channel.
2767 */
2768static void ssh2_try_send(struct ssh_channel *c) {
2769 while (c->v2.remwindow > 0 &&
2770 c->v2.outbuflen > 0) {
2771 unsigned len = c->v2.remwindow;
2772 if (len > c->v2.outbuflen)
2773 len = c->v2.outbuflen;
2774 if (len > c->v2.remmaxpkt)
2775 len = c->v2.remmaxpkt;
2776 ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA);
2777 ssh2_pkt_adduint32(c->remoteid);
2778 ssh2_pkt_addstring_start();
2779 ssh2_pkt_addstring_data(c->v2.outbuffer, len);
2780 ssh2_pkt_send();
2781 c->v2.outbuflen -= len;
2782 memmove(c->v2.outbuffer, c->v2.outbuffer+len,
2783 c->v2.outbuflen);
2784 c->v2.remwindow -= len;
2785 }
2786}
2787
2788/*
7cca0d81 2789 * Handle the SSH2 userauth and connection layers.
2790 */
2791static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
2792{
7cca0d81 2793 static unsigned long remote_winsize;
2794 static unsigned long remote_maxpkt;
65a22376 2795 static enum {
2796 AUTH_INVALID, AUTH_PUBLICKEY_AGENT, AUTH_PUBLICKEY_FILE, AUTH_PASSWORD
2797 } method;
1408a877 2798 static enum {
2799 AUTH_TYPE_NONE,
2800 AUTH_TYPE_PUBLICKEY,
2801 AUTH_TYPE_PUBLICKEY_OFFER_LOUD,
2802 AUTH_TYPE_PUBLICKEY_OFFER_QUIET,
2803 AUTH_TYPE_PASSWORD
2804 } type;
1983e559 2805 static int gotit, need_pw, can_pubkey, can_passwd;
2806 static int tried_pubkey_config, tried_agent;
1408a877 2807 static int we_are_in;
65a22376 2808 static char username[100];
2809 static char pwprompt[200];
2810 static char password[100];
7cca0d81 2811
e5574168 2812 crBegin;
2813
7cca0d81 2814 /*
2815 * Request userauth protocol, and await a response to it.
2816 */
2817 ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST);
2818 ssh2_pkt_addstring("ssh-userauth");
2819 ssh2_pkt_send();
2820 crWaitUntilV(ispkt);
8d5de777 2821 if (pktin.type != SSH2_MSG_SERVICE_ACCEPT) {
2822 bombout(("Server refused user authentication protocol"));
2823 crReturnV;
2824 }
7cca0d81 2825
2826 /*
1408a877 2827 * We repeat this whole loop, including the username prompt,
2828 * until we manage a successful authentication. If the user
2829 * types the wrong _password_, they are sent back to the
2830 * beginning to try another username. (If they specify a
2831 * username in the config, they are never asked, even if they
2832 * do give a wrong password.)
2833 *
2834 * I think this best serves the needs of
2835 *
2836 * - the people who have no configuration, no keys, and just
2837 * want to try repeated (username,password) pairs until they
2838 * type both correctly
2839 *
2840 * - people who have keys and configuration but occasionally
2841 * need to fall back to passwords
2842 *
2843 * - people with a key held in Pageant, who might not have
2844 * logged in to a particular machine before; so they want to
2845 * type a username, and then _either_ their key will be
2846 * accepted, _or_ they will type a password. If they mistype
2847 * the username they will want to be able to get back and
2848 * retype it!
7cca0d81 2849 */
1408a877 2850 do {
2851 static int pos;
7cca0d81 2852 static char c;
e5574168 2853
1408a877 2854 /*
2855 * Get a username.
2856 */
2857 pos = 0;
67779be7 2858 if ((flags & FLAG_INTERACTIVE) && !*cfg.username) {
fa17a66e 2859 if (ssh_get_line) {
2860 if (!ssh_get_line("login as: ",
2861 username, sizeof(username), FALSE)) {
2862 /*
2863 * get_line failed to get a username.
2864 * Terminate.
2865 */
2866 logevent("No username provided. Abandoning session.");
2867 ssh_state = SSH_STATE_CLOSED;
2868 crReturn(1);
2869 }
2870 } else {
2871 c_write_str("login as: ");
2872 ssh_send_ok = 1;
2873 while (pos >= 0) {
2874 crWaitUntilV(!ispkt);
2875 while (inlen--) switch (c = *in++) {
2876 case 10: case 13:
2877 username[pos] = 0;
2878 pos = -1;
2879 break;
2880 case 8: case 127:
2881 if (pos > 0) {
2882 c_write_str("\b \b");
2883 pos--;
2884 }
2885 break;
2886 case 21: case 27:
2887 while (pos > 0) {
2888 c_write_str("\b \b");
2889 pos--;
2890 }
2891 break;
2892 case 3: case 4:
2893 random_save_seed();
2894 exit(0);
2895 break;
2896 default:
2897 if (((c >= ' ' && c <= '~') ||
2898 ((unsigned char)c >= 160)) && pos < 40) {
2899 username[pos++] = c;
2900 c_write(&c, 1);
2901 }
2902 break;
2903 }
2904 }
2905 }
1408a877 2906 c_write_str("\r\n");
7cca0d81 2907 username[strcspn(username, "\n\r")] = '\0';
2908 } else {
2909 char stuff[200];
2910 strncpy(username, cfg.username, 99);
2911 username[99] = '\0';
65a22376 2912 if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
7cca0d81 2913 sprintf(stuff, "Using username \"%s\".\r\n", username);
1408a877 2914 c_write_str(stuff);
7cca0d81 2915 }
2916 }
2917
65a22376 2918 /*
1408a877 2919 * Send an authentication request using method "none": (a)
2920 * just in case it succeeds, and (b) so that we know what
2921 * authentication methods we can usefully try next.
65a22376 2922 */
1408a877 2923 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
2924 ssh2_pkt_addstring(username);
2925 ssh2_pkt_addstring("ssh-connection"); /* service requested */
2926 ssh2_pkt_addstring("none"); /* method */
2927 ssh2_pkt_send();
2928 type = AUTH_TYPE_NONE;
2929 gotit = FALSE;
2930 we_are_in = FALSE;
7cca0d81 2931
1408a877 2932 tried_pubkey_config = FALSE;
1983e559 2933 tried_agent = FALSE;
65a22376 2934
1408a877 2935 while (1) {
2936 /*
2937 * Wait for the result of the last authentication request.
2938 */
2939 if (!gotit)
2940 crWaitUntilV(ispkt);
2941 while (pktin.type == SSH2_MSG_USERAUTH_BANNER) {
2942 /* FIXME: should support this */
2943 crWaitUntilV(ispkt);
2944 }
2945 if (pktin.type == SSH2_MSG_USERAUTH_SUCCESS) {
2946 logevent("Access granted");
2947 we_are_in = TRUE;
2948 break;
2949 }
65a22376 2950
1408a877 2951 if (pktin.type != SSH2_MSG_USERAUTH_FAILURE) {
2952 bombout(("Strange packet received during authentication: type %d",
2953 pktin.type));
65a22376 2954 }
2955
1408a877 2956 gotit = FALSE;
65a22376 2957
1408a877 2958 /*
2959 * OK, we're now sitting on a USERAUTH_FAILURE message, so
2960 * we can look at the string in it and know what we can
2961 * helpfully try next.
2962 */
2963 {
2964 char *methods;
2965 int methlen;
2966 ssh2_pkt_getstring(&methods, &methlen);
2967 if (!ssh2_pkt_getbool()) {
2968 /*
2969 * We have received an unequivocal Access
2970 * Denied. This can translate to a variety of
2971 * messages:
2972 *
2973 * - if we'd just tried "none" authentication,
2974 * it's not worth printing anything at all
2975 *
2976 * - if we'd just tried a public key _offer_,
2977 * the message should be "Server refused our
2978 * key" (or no message at all if the key
2979 * came from Pageant)
2980 *
2981 * - if we'd just tried anything else, the
2982 * message really should be "Access denied".
2983 *
2984 * Additionally, if we'd just tried password
2985 * authentication, we should break out of this
2986 * whole loop so as to go back to the username
2987 * prompt.
2988 */
2989 if (type == AUTH_TYPE_NONE) {
2990 /* do nothing */
2991 } else if (type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD ||
2992 type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) {
2993 if (type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD)
2994 c_write_str("Server refused our key\r\n");
2995 logevent("Server refused public key");
2996 } else {
2997 c_write_str("Access denied\r\n");
2998 logevent("Access denied");
2999 if (type == AUTH_TYPE_PASSWORD) {
3000 we_are_in = FALSE;
3001 break;
3002 }
3003 }
3004 } else {
3005 c_write_str("Further authentication required\r\n");
3006 logevent("Further authentication required");
3007 }
65a22376 3008
1408a877 3009 can_pubkey = in_commasep_string("publickey", methods, methlen);
3010 can_passwd = in_commasep_string("password", methods, methlen);
3011 }
65a22376 3012
1408a877 3013 method = 0;
65a22376 3014
1983e559 3015 if (!method && can_pubkey && agent_exists && !tried_agent) {
3016 /*
3017 * Attempt public-key authentication using Pageant.
3018 */
3019 static unsigned char request[5], *response, *p;
3020 static int responselen;
3021 static int i, nkeys;
3022 static int authed = FALSE;
3023 void *r;
3024
3025 tried_agent = TRUE;
3026
3027 logevent("Pageant is running. Requesting keys.");
3028
3029 /* Request the keys held by the agent. */
3030 PUT_32BIT(request, 1);
3031 request[4] = SSH2_AGENTC_REQUEST_IDENTITIES;
3032 agent_query(request, 5, &r, &responselen);
3033 response = (unsigned char *)r;
3034 if (response) {
3035 p = response + 5;
3036 nkeys = GET_32BIT(p); p += 4;
6328ea7a 3037 { char buf[64]; sprintf(buf, "Pageant has %d SSH2 keys", nkeys);
1983e559 3038 logevent(buf); }
3039 for (i = 0; i < nkeys; i++) {
3040 static char *pkblob, *alg, *commentp;
3041 static int pklen, alglen, commentlen;
3042 static int siglen, retlen, len;
3043 static char *q, *agentreq, *ret;
3044
3045 { char buf[64]; sprintf(buf, "Trying Pageant key #%d", i);
3046 logevent(buf); }
3047 pklen = GET_32BIT(p); p += 4;
3048 pkblob = p; p += pklen;
3049 alglen = GET_32BIT(pkblob);
3050 alg = pkblob + 4;
3051 commentlen = GET_32BIT(p); p += 4;
3052 commentp = p; p += commentlen;
3053 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
3054 ssh2_pkt_addstring(username);
3055 ssh2_pkt_addstring("ssh-connection");/* service requested */
3056 ssh2_pkt_addstring("publickey");/* method */
3057 ssh2_pkt_addbool(FALSE); /* no signature included */
3058 ssh2_pkt_addstring_start();
3059 ssh2_pkt_addstring_data(alg, alglen);
3060 ssh2_pkt_addstring_start();
3061 ssh2_pkt_addstring_data(pkblob, pklen);
3062 ssh2_pkt_send();
3063
3064 crWaitUntilV(ispkt);
3065 if (pktin.type != SSH2_MSG_USERAUTH_PK_OK) {
3066 logevent("Key refused");
3067 continue;
3068 }
3069
3070 c_write_str("Authenticating with public key \"");
3071 c_write(commentp, commentlen);
3072 c_write_str("\" from agent\r\n");
3073
3074 /*
3075 * Server is willing to accept the key.
3076 * Construct a SIGN_REQUEST.
3077 */
3078 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
3079 ssh2_pkt_addstring(username);
3080 ssh2_pkt_addstring("ssh-connection"); /* service requested */
3081 ssh2_pkt_addstring("publickey"); /* method */
3082 ssh2_pkt_addbool(TRUE);
3083 ssh2_pkt_addstring_start();
3084 ssh2_pkt_addstring_data(alg, alglen);
3085 ssh2_pkt_addstring_start();
3086 ssh2_pkt_addstring_data(pkblob, pklen);
3087
3088 siglen = pktout.length - 5 + 4 + 20;
3089 len = 1; /* message type */
3090 len += 4 + pklen; /* key blob */
3091 len += 4 + siglen; /* data to sign */
3092 len += 4; /* flags */
3093 agentreq = smalloc(4 + len);
3094 PUT_32BIT(agentreq, len);
3095 q = agentreq + 4;
3096 *q++ = SSH2_AGENTC_SIGN_REQUEST;
3097 PUT_32BIT(q, pklen); q += 4;
3098 memcpy(q, pkblob, pklen); q += pklen;
3099 PUT_32BIT(q, siglen); q += 4;
3100 /* Now the data to be signed... */
3101 PUT_32BIT(q, 20); q += 4;
3102 memcpy(q, ssh2_session_id, 20); q += 20;
3103 memcpy(q, pktout.data+5, pktout.length-5);
3104 q += pktout.length-5;
3105 /* And finally the (zero) flags word. */
3106 PUT_32BIT(q, 0);
3107 agent_query(agentreq, len+4, &ret, &retlen);
3108 sfree(agentreq);
3109 if (ret) {
3110 if (ret[4] == SSH2_AGENT_SIGN_RESPONSE) {
3111 logevent("Sending Pageant's response");
3112 ssh2_pkt_addstring_start();
3113 ssh2_pkt_addstring_data(ret+9, GET_32BIT(ret+5));
3114 ssh2_pkt_send();
3115 authed = TRUE;
3116 break;
3117 } else {
3118 logevent("Pageant failed to answer challenge");
3119 sfree(ret);
3120 }
3121 }
3122 }
3123 if (authed)
3124 continue;
3125 }
3126 }
3127
1408a877 3128 if (!method && can_pubkey && *cfg.keyfile && !tried_pubkey_config) {
3129 unsigned char *pub_blob;
3130 char *algorithm, *comment;
3131 int pub_blob_len;
65a22376 3132
1408a877 3133 tried_pubkey_config = TRUE;
65a22376 3134
65a22376 3135 /*
1408a877 3136 * Try the public key supplied in the configuration.
3137 *
3138 * First, offer the public blob to see if the server is
3139 * willing to accept it.
65a22376 3140 */
1408a877 3141 pub_blob = ssh2_userkey_loadpub(cfg.keyfile, &algorithm,
3142 &pub_blob_len);
3143 if (pub_blob) {
3144 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
3145 ssh2_pkt_addstring(username);
3146 ssh2_pkt_addstring("ssh-connection"); /* service requested */
3147 ssh2_pkt_addstring("publickey");/* method */
3148 ssh2_pkt_addbool(FALSE); /* no signature included */
3149 ssh2_pkt_addstring(algorithm);
3150 ssh2_pkt_addstring_start();
3151 ssh2_pkt_addstring_data(pub_blob, pub_blob_len);
3152 ssh2_pkt_send();
3153 logevent("Offered public key"); /* FIXME */
3154
3155 crWaitUntilV(ispkt);
3156 if (pktin.type != SSH2_MSG_USERAUTH_PK_OK) {
3157 gotit = TRUE;
3158 type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD;
3159 continue; /* key refused; give up on it */
3160 }
65a22376 3161
1408a877 3162 logevent("Offer of public key accepted");
65a22376 3163 /*
1408a877 3164 * Actually attempt a serious authentication using
3165 * the key.
65a22376 3166 */
1408a877 3167 if (ssh2_userkey_encrypted(cfg.keyfile, &comment)) {
3168 sprintf(pwprompt, "Passphrase for key \"%.100s\": ", comment);
3169 need_pw = TRUE;
3170 } else {
3171 need_pw = FALSE;
3172 }
3173 c_write_str("Authenticating with public key \"");
3174 c_write_str(comment);
3175 c_write_str("\"\r\n");
3176 method = AUTH_PUBLICKEY_FILE;
65a22376 3177 }
1408a877 3178 }
3179
3180 if (!method && can_passwd) {
3181 method = AUTH_PASSWORD;
3182 sprintf(pwprompt, "%.90s@%.90s's password: ", username, savedhost);
3183 need_pw = TRUE;
3184 }
3185
3186 if (need_pw) {
fa17a66e 3187 if (ssh_get_line) {
3188 if (!ssh_get_line(pwprompt, password,
3189 sizeof(password), TRUE)) {
1408a877 3190 /*
fa17a66e 3191 * get_line failed to get a password (for
3192 * example because one was supplied on the
3193 * command line which has already failed to
3194 * work). Terminate.
1408a877 3195 */
3196 logevent("No more passwords to try");
3197 ssh_state = SSH_STATE_CLOSED;
3198 crReturnV;
3199 }
3200 } else {
3201 static int pos = 0;
3202 static char c;
3203
3204 c_write_str(pwprompt);
3205 ssh_send_ok = 1;
3206
3207 pos = 0;
3208 while (pos >= 0) {
3209 crWaitUntilV(!ispkt);
3210 while (inlen--) switch (c = *in++) {
3211 case 10: case 13:
3212 password[pos] = 0;
3213 pos = -1;
3214 break;
3215 case 8: case 127:
3216 if (pos > 0)
3217 pos--;
3218 break;
3219 case 21: case 27:
3220 pos = 0;
3221 break;
3222 case 3: case 4:
3223 random_save_seed();
3224 exit(0);
3225 break;
3226 default:
3227 if (((c >= ' ' && c <= '~') ||
3228 ((unsigned char)c >= 160)) && pos < 40)
3229 password[pos++] = c;
3230 break;
3231 }
65a22376 3232 }
1408a877 3233 c_write_str("\r\n");
65a22376 3234 }
65a22376 3235 }
65a22376 3236
1408a877 3237 if (method == AUTH_PUBLICKEY_FILE) {
3238 /*
3239 * We have our passphrase. Now try the actual authentication.
3240 */
3241 struct ssh2_userkey *key;
65a22376 3242
1408a877 3243 key = ssh2_load_userkey(cfg.keyfile, password);
3244 if (key == SSH2_WRONG_PASSPHRASE || key == NULL) {
3245 if (key == SSH2_WRONG_PASSPHRASE) {
3246 c_write_str("Wrong passphrase\r\n");
3247 tried_pubkey_config = FALSE;
3248 } else {
3249 c_write_str("Unable to load private key\r\n");
3250 tried_pubkey_config = TRUE;
3251 }
3252 /* Send a spurious AUTH_NONE to return to the top. */
3253 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
3254 ssh2_pkt_addstring(username);
3255 ssh2_pkt_addstring("ssh-connection"); /* service requested */
3256 ssh2_pkt_addstring("none"); /* method */
3257 ssh2_pkt_send();
3258 type = AUTH_TYPE_NONE;
3259 } else {
3260 unsigned char *blob, *sigdata;
3261 int blob_len, sigdata_len;
65a22376 3262
1408a877 3263 /*
3264 * We have loaded the private key and the server
3265 * has announced that it's willing to accept it.
3266 * Hallelujah. Generate a signature and send it.
3267 */
3268 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
3269 ssh2_pkt_addstring(username);
3270 ssh2_pkt_addstring("ssh-connection"); /* service requested */
3271 ssh2_pkt_addstring("publickey"); /* method */
3272 ssh2_pkt_addbool(TRUE);
3273 ssh2_pkt_addstring(key->alg->name);
3274 blob = key->alg->public_blob(key->data, &blob_len);
3275 ssh2_pkt_addstring_start();
3276 ssh2_pkt_addstring_data(blob, blob_len);
3277 sfree(blob);
3278
3279 /*
3280 * The data to be signed is:
3281 *
3282 * string session-id
3283 *
3284 * followed by everything so far placed in the
3285 * outgoing packet.
3286 */
3287 sigdata_len = pktout.length - 5 + 4 + 20;
3288 sigdata = smalloc(sigdata_len);
3289 PUT_32BIT(sigdata, 20);
3290 memcpy(sigdata+4, ssh2_session_id, 20);
3291 memcpy(sigdata+24, pktout.data+5, pktout.length-5);
3292 blob = key->alg->sign(key->data, sigdata, sigdata_len, &blob_len);
3293 ssh2_pkt_addstring_start();
3294 ssh2_pkt_addstring_data(blob, blob_len);
3295 sfree(blob);
3296 sfree(sigdata);
3297
3298 ssh2_pkt_send();
3299 type = AUTH_TYPE_PUBLICKEY;
3300 }
3301 } else if (method == AUTH_PASSWORD) {
65a22376 3302 /*
1408a877 3303 * We send the password packet lumped tightly together with
3304 * an SSH_MSG_IGNORE packet. The IGNORE packet contains a
3305 * string long enough to make the total length of the two
3306 * packets constant. This should ensure that a passive
3307 * listener doing traffic analyis can't work out the length
3308 * of the password.
3309 *
3310 * For this to work, we need an assumption about the
3311 * maximum length of the password packet. I think 256 is
3312 * pretty conservative. Anyone using a password longer than
3313 * that probably doesn't have much to worry about from
3314 * people who find out how long their password is!
65a22376 3315 */
3316 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
3317 ssh2_pkt_addstring(username);
3318 ssh2_pkt_addstring("ssh-connection"); /* service requested */
1408a877 3319 ssh2_pkt_addstring("password");
3320 ssh2_pkt_addbool(FALSE);
3321 ssh2_pkt_addstring(password);
3322 ssh2_pkt_defer();
65a22376 3323 /*
1408a877 3324 * We'll include a string that's an exact multiple of the
3325 * cipher block size. If the cipher is NULL for some
3326 * reason, we don't do this trick at all because we gain
3327 * nothing by it.
65a22376 3328 */
6e9e9520 3329 if (cscipher) {
3330 int stringlen, i;
3331
3332 stringlen = (256 - deferred_len);
3333 stringlen += cscipher->blksize - 1;
3334 stringlen -= (stringlen % cscipher->blksize);
3335 if (cscomp) {
3336 /*
3337 * Temporarily disable actual compression,
3338 * so we can guarantee to get this string
3339 * exactly the length we want it. The
3340 * compression-disabling routine should
3341 * return an integer indicating how many
3342 * bytes we should adjust our string length
3343 * by.
3344 */
3345 stringlen -= cscomp->disable_compression();
3346 }
1408a877 3347 ssh2_pkt_init(SSH2_MSG_IGNORE);
3348 ssh2_pkt_addstring_start();
6e9e9520 3349 for (i = 0; i < stringlen; i++) {
3350 char c = (char)random_byte();
3351 ssh2_pkt_addstring_data(&c, 1);
65a22376 3352 }
1408a877 3353 ssh2_pkt_defer();
65a22376 3354 }
39065bed 3355 ssh_pkt_defersend();
0d43337a 3356 logevent("Sent password");
1408a877 3357 type = AUTH_TYPE_PASSWORD;
3358 } else {
3359 c_write_str("No supported authentication methods left to try!\r\n");
3360 logevent("No supported authentications offered. Disconnecting");
3361 ssh2_pkt_init(SSH2_MSG_DISCONNECT);
3362 ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION);
3363 ssh2_pkt_addstring("No supported authentication methods available");
3364 ssh2_pkt_addstring("en"); /* language tag */
3365 ssh2_pkt_send();
3366 ssh_state = SSH_STATE_CLOSED;
3367 crReturnV;
65a22376 3368 }
65a22376 3369 }
1408a877 3370 } while (!we_are_in);
7cca0d81 3371
3372 /*
3373 * Now we're authenticated for the connection protocol. The
3374 * connection protocol will automatically have started at this
3375 * point; there's no need to send SERVICE_REQUEST.
3376 */
3377
3378 /*
3379 * So now create a channel with a session in it.
3380 */
dcbde236 3381 mainchan = smalloc(sizeof(struct ssh_channel));
d211621f 3382 mainchan->localid = 100; /* as good as any */
7cca0d81 3383 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
3384 ssh2_pkt_addstring("session");
d211621f 3385 ssh2_pkt_adduint32(mainchan->localid);
cf6e59d6 3386 ssh2_pkt_adduint32(0x8000UL); /* our window size */
d211621f 3387 ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */
7cca0d81 3388 ssh2_pkt_send();
3389 crWaitUntilV(ispkt);
3390 if (pktin.type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) {
8d5de777 3391 bombout(("Server refused to open a session"));
3392 crReturnV;
7cca0d81 3393 /* FIXME: error data comes back in FAILURE packet */
3394 }
d211621f 3395 if (ssh2_pkt_getuint32() != mainchan->localid) {
8d5de777 3396 bombout(("Server's channel confirmation cited wrong channel"));
3397 crReturnV;
7cca0d81 3398 }
d211621f 3399 mainchan->remoteid = ssh2_pkt_getuint32();
783415f8 3400 mainchan->type = CHAN_MAINSESSION;
3401 mainchan->closes = 0;
3402 mainchan->v2.remwindow = ssh2_pkt_getuint32();
3403 mainchan->v2.remmaxpkt = ssh2_pkt_getuint32();
3404 mainchan->v2.outbuffer = NULL;
3405 mainchan->v2.outbuflen = mainchan->v2.outbufsize = 0;
3406 ssh_channels = newtree234(ssh_channelcmp);
3407 add234(ssh_channels, mainchan);
7cca0d81 3408 logevent("Opened channel for session");
3409
3410 /*
783415f8 3411 * Potentially enable X11 forwarding.
3412 */
3413 if (cfg.x11_forward) {
3414 char proto[20], data[64];
3415 logevent("Requesting X11 forwarding");
3416 x11_invent_auth(proto, sizeof(proto), data, sizeof(data));
3417 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
3418 ssh2_pkt_adduint32(mainchan->remoteid);
3419 ssh2_pkt_addstring("x11-req");
3420 ssh2_pkt_addbool(1); /* want reply */
3421 ssh2_pkt_addbool(0); /* many connections */
3422 ssh2_pkt_addstring(proto);
3423 ssh2_pkt_addstring(data);
3424 ssh2_pkt_adduint32(0); /* screen number */
3425 ssh2_pkt_send();
3426
3427 do {
3428 crWaitUntilV(ispkt);
3429 if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
3430 unsigned i = ssh2_pkt_getuint32();
3431 struct ssh_channel *c;
3432 c = find234(ssh_channels, &i, ssh_channelfind);
3433 if (!c)
3434 continue; /* nonexistent channel */
3435 c->v2.remwindow += ssh2_pkt_getuint32();
3436 }
3437 } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
3438
3439 if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
3440 if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
3441 bombout(("Server got confused by X11 forwarding request"));
3442 crReturnV;
3443 }
3444 logevent("X11 forwarding refused");
3445 } else {
3446 logevent("X11 forwarding enabled");
3447 ssh_X11_fwd_enabled = TRUE;
3448 }
3449 }
3450
3451 /*
36c2a3e9 3452 * Potentially enable agent forwarding.
3453 */
3454 if (cfg.agentfwd && agent_exists()) {
3455 char proto[20], data[64];
3456 logevent("Requesting OpenSSH-style agent forwarding");
3457 x11_invent_auth(proto, sizeof(proto), data, sizeof(data));
3458 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
3459 ssh2_pkt_adduint32(mainchan->remoteid);
3460 ssh2_pkt_addstring("auth-agent-req@openssh.com");
3461 ssh2_pkt_addbool(1); /* want reply */
3462 ssh2_pkt_send();
3463
3464 do {
3465 crWaitUntilV(ispkt);
3466 if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
3467 unsigned i = ssh2_pkt_getuint32();
3468 struct ssh_channel *c;
3469 c = find234(ssh_channels, &i, ssh_channelfind);
3470 if (!c)
3471 continue; /* nonexistent channel */
3472 c->v2.remwindow += ssh2_pkt_getuint32();
3473 }
3474 } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
3475
3476 if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
3477 if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
3478 bombout(("Server got confused by agent forwarding request"));
3479 crReturnV;
3480 }
3481 logevent("Agent forwarding refused");
3482 } else {
3483 logevent("Agent forwarding enabled");
3484 ssh_agentfwd_enabled = TRUE;
3485 }
3486 }
3487
3488 /*
7cca0d81 3489 * Now allocate a pty for the session.
3490 */
67779be7 3491 if (!cfg.nopty) {
3492 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
3493 ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */
3494 ssh2_pkt_addstring("pty-req");
3495 ssh2_pkt_addbool(1); /* want reply */
3496 ssh2_pkt_addstring(cfg.termtype);
3497 ssh2_pkt_adduint32(cols);
3498 ssh2_pkt_adduint32(rows);
3499 ssh2_pkt_adduint32(0); /* pixel width */
3500 ssh2_pkt_adduint32(0); /* pixel height */
3501 ssh2_pkt_addstring_start();
3502 ssh2_pkt_addstring_data("\0", 1);/* TTY_OP_END, no special options */
3503 ssh2_pkt_send();
6e48c3fe 3504 ssh_state = SSH_STATE_INTERMED;
7cca0d81 3505
67779be7 3506 do {
3507 crWaitUntilV(ispkt);
3508 if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
783415f8 3509 unsigned i = ssh2_pkt_getuint32();
3510 struct ssh_channel *c;
3511 c = find234(ssh_channels, &i, ssh_channelfind);
3512 if (!c)
3513 continue; /* nonexistent channel */
3514 c->v2.remwindow += ssh2_pkt_getuint32();
67779be7 3515 }
3516 } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
7cca0d81 3517
67779be7 3518 if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
3519 if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
8d5de777 3520 bombout(("Server got confused by pty request"));
3521 crReturnV;
67779be7 3522 }
1408a877 3523 c_write_str("Server refused to allocate pty\r\n");
0965bee0 3524 ssh_editing = ssh_echoing = 1;
67779be7 3525 } else {
3526 logevent("Allocated pty");
7cca0d81 3527 }
0965bee0 3528 } else {
3529 ssh_editing = ssh_echoing = 1;
7cca0d81 3530 }
3531
3532 /*
67779be7 3533 * Start a shell or a remote command.
7cca0d81 3534 */
3535 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
d211621f 3536 ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */
4a8fc3c4 3537 if (cfg.ssh_subsys) {
3538 ssh2_pkt_addstring("subsystem");
3539 ssh2_pkt_addbool(1); /* want reply */
3540 ssh2_pkt_addstring(cfg.remote_cmd);
3541 } else if (*cfg.remote_cmd) {
67779be7 3542 ssh2_pkt_addstring("exec");
3543 ssh2_pkt_addbool(1); /* want reply */
3544 ssh2_pkt_addstring(cfg.remote_cmd);
3545 } else {
3546 ssh2_pkt_addstring("shell");
3547 ssh2_pkt_addbool(1); /* want reply */
3548 }
7cca0d81 3549 ssh2_pkt_send();
d211621f 3550 do {
7cca0d81 3551 crWaitUntilV(ispkt);
d211621f 3552 if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
783415f8 3553 unsigned i = ssh2_pkt_getuint32();
3554 struct ssh_channel *c;
3555 c = find234(ssh_channels, &i, ssh_channelfind);
3556 if (!c)
3557 continue; /* nonexistent channel */
3558 c->v2.remwindow += ssh2_pkt_getuint32();
d211621f 3559 }
7cca0d81 3560 } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST);
3561 if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) {
3562 if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) {
8d5de777 3563 bombout(("Server got confused by shell/command request"));
3564 crReturnV;
7cca0d81 3565 }
8d5de777 3566 bombout(("Server refused to start a shell/command"));
3567 crReturnV;
7cca0d81 3568 } else {
8d5de777 3569 logevent("Started a shell/command");
7cca0d81 3570 }
3571
6e48c3fe 3572 ssh_state = SSH_STATE_SESSION;
3573 if (size_needed)
3574 ssh_size();
3687d221 3575 if (eof_needed)
3576 ssh_special(TS_EOF);
6e48c3fe 3577
7cca0d81 3578 /*
3579 * Transfer data!
3580 */
0965bee0 3581 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
8ccc75b0 3582 ssh_send_ok = 1;
7cca0d81 3583 while (1) {
d211621f 3584 static int try_send;
e5574168 3585 crReturnV;
d211621f 3586 try_send = FALSE;
7cca0d81 3587 if (ispkt) {
3588 if (pktin.type == SSH2_MSG_CHANNEL_DATA ||
3589 pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) {
3590 char *data;
3591 int length;
783415f8 3592 unsigned i = ssh2_pkt_getuint32();
3593 struct ssh_channel *c;
3594 c = find234(ssh_channels, &i, ssh_channelfind);
3595 if (!c)
3596 continue; /* nonexistent channel */
7cca0d81 3597 if (pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA &&
3598 ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR)
3599 continue; /* extended but not stderr */
3600 ssh2_pkt_getstring(&data, &length);
d211621f 3601 if (data) {
783415f8 3602 switch (c->type) {
3603 case CHAN_MAINSESSION:
3604 from_backend(pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA,
3605 data, length);
3606 break;
3607 case CHAN_X11:
3608 x11_send(c->u.x11.s, data, length);
3609 break;
36c2a3e9 3610 case CHAN_AGENT:
3611 while (length > 0) {
3612 if (c->u.a.lensofar < 4) {
3613 int l = min(4 - c->u.a.lensofar, length);
3614 memcpy(c->u.a.msglen + c->u.a.lensofar, data, l);
3615 data += l; length -= l; c->u.a.lensofar += l;
3616 }
3617 if (c->u.a.lensofar == 4) {
3618 c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen);
3619 c->u.a.message = smalloc(c->u.a.totallen);
3620 memcpy(c->u.a.message, c->u.a.msglen, 4);
3621 }
3622 if (c->u.a.lensofar >= 4 && length > 0) {
3623 int l = min(c->u.a.totallen - c->u.a.lensofar,
3624 length);
3625 memcpy(c->u.a.message + c->u.a.lensofar, data, l);
3626 data += l; length -= l; c->u.a.lensofar += l;
3627 }
3628 if (c->u.a.lensofar == c->u.a.totallen) {
3629 void *reply, *sentreply;
3630 int replylen;
3631 agent_query(c->u.a.message, c->u.a.totallen,
3632 &reply, &replylen);
3633 if (reply)
3634 sentreply = reply;
3635 else {
3636 /* Fake SSH_AGENT_FAILURE. */
3637 sentreply = "\0\0\0\1\5";
3638 replylen = 5;
3639 }
3640 ssh2_add_channel_data(c, sentreply, replylen);
3641 try_send = TRUE;
3642 if (reply)
3643 sfree(reply);
3644 sfree(c->u.a.message);
3645 c->u.a.lensofar = 0;
3646 }
3647 }
3648 break;
783415f8 3649 }
d211621f 3650 /*
783415f8 3651 * Enlarge the window again at the remote
3652 * side, just in case it ever runs down and
3653 * they fail to send us any more data.
d211621f 3654 */
3655 ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
783415f8 3656 ssh2_pkt_adduint32(c->remoteid);
d211621f 3657 ssh2_pkt_adduint32(length);
3658 ssh2_pkt_send();
3659 }
7cca0d81 3660 } else if (pktin.type == SSH2_MSG_DISCONNECT) {
3661 ssh_state = SSH_STATE_CLOSED;
d211621f 3662 logevent("Received disconnect message");
3257deae 3663 crReturnV;
7cca0d81 3664 } else if (pktin.type == SSH2_MSG_CHANNEL_REQUEST) {
3665 continue; /* exit status et al; ignore (FIXME?) */
d211621f 3666 } else if (pktin.type == SSH2_MSG_CHANNEL_EOF) {
783415f8 3667 unsigned i = ssh2_pkt_getuint32();
3668 struct ssh_channel *c;
3669
3670 c = find234(ssh_channels, &i, ssh_channelfind);
3671 if (!c)
3672 continue; /* nonexistent channel */
3673
3674 if (c->type == CHAN_X11) {
3675 /*
3676 * Remote EOF on an X11 channel means we should
3677 * wrap up and close the channel ourselves.
3678 */
3679 x11_close(c->u.x11.s);
3680 sshfwd_close(c);
36c2a3e9 3681 } else if (c->type == CHAN_AGENT) {
3682 sshfwd_close(c);
3683 }
d211621f 3684 } else if (pktin.type == SSH2_MSG_CHANNEL_CLOSE) {
783415f8 3685 unsigned i = ssh2_pkt_getuint32();
3686 struct ssh_channel *c;
3687 enum234 e;
3688
3689 c = find234(ssh_channels, &i, ssh_channelfind);
3690 if (!c)
3691 continue; /* nonexistent channel */
3692 if (c->closes == 0) {
3693 ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
3694 ssh2_pkt_adduint32(c->remoteid);
3695 ssh2_pkt_send();
3696 }
3697 /* Do pre-close processing on the channel. */
3698 switch (c->type) {
3699 case CHAN_MAINSESSION:
3700 break; /* nothing to see here, move along */
3701 case CHAN_X11:
3702 break;
36c2a3e9 3703 case CHAN_AGENT:
3704 break;
783415f8 3705 }
3706 del234(ssh_channels, c);
3707 sfree(c->v2.outbuffer);
3708 sfree(c);
3709
3710 /*
3711 * See if that was the last channel left open.
3712 */
3713 c = first234(ssh_channels, &e);
3714 if (!c) {
d211621f 3715 logevent("All channels closed. Disconnecting");
3716 ssh2_pkt_init(SSH2_MSG_DISCONNECT);
9005f3ba 3717 ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION);
3718 ssh2_pkt_addstring("All open channels closed");
3719 ssh2_pkt_addstring("en"); /* language tag */
d211621f 3720 ssh2_pkt_send();
cf6e59d6 3721 ssh_state = SSH_STATE_CLOSED;
3257deae 3722 crReturnV;
d211621f 3723 }
3724 continue; /* remote sends close; ignore (FIXME) */
7cca0d81 3725 } else if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) {
783415f8 3726 unsigned i = ssh2_pkt_getuint32();
3727 struct ssh_channel *c;
3728 c = find234(ssh_channels, &i, ssh_channelfind);
3729 if (!c)
3730 continue; /* nonexistent channel */
3731 mainchan->v2.remwindow += ssh2_pkt_getuint32();
d211621f 3732 try_send = TRUE;
783415f8 3733 } else if (pktin.type == SSH2_MSG_CHANNEL_OPEN) {
3734 char *type;
3735 int typelen;
3736 char *error = NULL;
3737 struct ssh_channel *c;
3738 ssh2_pkt_getstring(&type, &typelen);
3739 c = smalloc(sizeof(struct ssh_channel));
3740
3741 if (typelen == 3 && !memcmp(type, "x11", 3)) {
783415f8 3742 if (!ssh_X11_fwd_enabled)
3743 error = "X11 forwarding is not enabled";
0c6c9a70 3744 else if ( x11_init(&c->u.x11.s, cfg.x11_display, c) != NULL ) {
783415f8 3745 error = "Unable to open an X11 connection";
3746 } else {
3747 c->type = CHAN_X11;
3748 }
36c2a3e9 3749 } else if (typelen == 22 &&
3750 !memcmp(type, "auth-agent@openssh.com", 3)) {
3751 if (!ssh_agentfwd_enabled)
3752 error = "Agent forwarding is not enabled";
3753 else {
3754 c->type = CHAN_AGENT; /* identify channel type */
3755 c->u.a.lensofar = 0;
3756 }
783415f8 3757 } else {
3758 error = "Unsupported channel type requested";
3759 }
3760
3761 c->remoteid = ssh2_pkt_getuint32();
3762 if (error) {
3763 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE);
3764 ssh2_pkt_adduint32(c->remoteid);
3765 ssh2_pkt_adduint32(SSH2_OPEN_CONNECT_FAILED);
3766 ssh2_pkt_addstring(error);
3767 ssh2_pkt_addstring("en"); /* language tag */
3768 ssh2_pkt_send();
3769 sfree(c);
3770 } else {
3771 struct ssh_channel *d;
3772 unsigned i;
3773 enum234 e;
3774
3775 for (i=1, d = first234(ssh_channels, &e); d;
3776 d = next234(&e)) {
3777 if (d->localid > i)
3778 break; /* found a free number */
3779 i = d->localid + 1;
3780 }
3781 c->localid = i;
3782 c->closes = 0;
3783 c->v2.remwindow = ssh2_pkt_getuint32();
3784 c->v2.remmaxpkt = ssh2_pkt_getuint32();
3785 c->v2.outbuffer = NULL;
3786 c->v2.outbuflen = c->v2.outbufsize = 0;
3787 add234(ssh_channels, c);
3788 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
3789 ssh2_pkt_adduint32(c->remoteid);
3790 ssh2_pkt_adduint32(c->localid);
3791 ssh2_pkt_adduint32(0x8000UL); /* our window size */
3792 ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */
3793 ssh2_pkt_send();
3794 }
7cca0d81 3795 } else {
8d5de777 3796 bombout(("Strange packet received: type %d", pktin.type));
3797 crReturnV;
7cca0d81 3798 }
3799 } else {
d211621f 3800 /*
3801 * We have spare data. Add it to the channel buffer.
3802 */
783415f8 3803 ssh2_add_channel_data(mainchan, in, inlen);
d211621f 3804 try_send = TRUE;
7cca0d81 3805 }
d211621f 3806 if (try_send) {
783415f8 3807 enum234 e;
3808 struct ssh_channel *c;
d211621f 3809 /*
783415f8 3810 * Try to send data on all channels if we can.
d211621f 3811 */
783415f8 3812 for (c = first234(ssh_channels, &e); c; c = next234(&e))
3813 ssh2_try_send(c);
d211621f 3814 }
e5574168 3815 }
3816
3817 crFinishV;
3818}
3819
3820/*
7cca0d81 3821 * Handle the top-level SSH2 protocol.
3822 */
3823static void ssh2_protocol(unsigned char *in, int inlen, int ispkt)
3824{
3825 if (do_ssh2_transport(in, inlen, ispkt) == 0)
3826 return;
3827 do_ssh2_authconn(in, inlen, ispkt);
3828}
3829
3830/*
8df7a775 3831 * Called to set up the connection.
374330e2 3832 *
3833 * Returns an error message, or NULL on success.
374330e2 3834 */
8df7a775 3835static char *ssh_init (char *host, int port, char **realhost) {
fb09bf1c 3836 char *p;
8f203108 3837
3838#ifdef MSCRYPTOAPI
3839 if(crypto_startup() == 0)
3840 return "Microsoft high encryption pack not installed!";
3841#endif
374330e2 3842
8df7a775 3843 ssh_send_ok = 0;
0965bee0 3844 ssh_editing = 0;
3845 ssh_echoing = 0;
8df7a775 3846
fb09bf1c 3847 p = connect_to_host(host, port, realhost);
3848 if (p != NULL)
3849 return p;
374330e2 3850
374330e2 3851 return NULL;
3852}
3853
3854/*
374330e2 3855 * Called to send data down the Telnet connection.
3856 */
3857static void ssh_send (char *buf, int len) {
f78051a1 3858 if (s == NULL || ssh_protocol == NULL)
374330e2 3859 return;
3860
3861 ssh_protocol(buf, len, 0);
3862}
3863
3864/*
6e48c3fe 3865 * Called to set the size of the window from SSH's POV.
374330e2 3866 */
3867static void ssh_size(void) {
3868 switch (ssh_state) {
3869 case SSH_STATE_BEFORE_SIZE:
3687d221 3870 case SSH_STATE_PREPACKET:
21248260 3871 case SSH_STATE_CLOSED:
374330e2 3872 break; /* do nothing */
3873 case SSH_STATE_INTERMED:
3874 size_needed = TRUE; /* buffer for later */
3875 break;
3876 case SSH_STATE_SESSION:
fef97f43 3877 if (!cfg.nopty) {
6e48c3fe 3878 if (ssh_version == 1) {
3879 send_packet(SSH1_CMSG_WINDOW_SIZE,
3880 PKT_INT, rows, PKT_INT, cols,
3881 PKT_INT, 0, PKT_INT, 0, PKT_END);
3882 } else {
3883 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
3884 ssh2_pkt_adduint32(mainchan->remoteid);
3885 ssh2_pkt_addstring("window-change");
3886 ssh2_pkt_addbool(0);
3887 ssh2_pkt_adduint32(cols);
3888 ssh2_pkt_adduint32(rows);
3889 ssh2_pkt_adduint32(0);
3890 ssh2_pkt_adduint32(0);
3891 ssh2_pkt_send();
3892 }
fef97f43 3893 }
3687d221 3894 break;
374330e2 3895 }
3896}
3897
3898/*
6abbf9e3 3899 * Send Telnet special codes. TS_EOF is useful for `plink', so you
3900 * can send an EOF and collect resulting output (e.g. `plink
3901 * hostname sort').
374330e2 3902 */
3903static void ssh_special (Telnet_Special code) {
6abbf9e3 3904 if (code == TS_EOF) {
3687d221 3905 if (ssh_state != SSH_STATE_SESSION) {
3906 /*
3907 * Buffer the EOF in case we are pre-SESSION, so we can
3908 * send it as soon as we reach SESSION.
3909 */
3910 if (code == TS_EOF)
3911 eof_needed = TRUE;
3912 return;
3913 }
67779be7 3914 if (ssh_version == 1) {
6abbf9e3 3915 send_packet(SSH1_CMSG_EOF, PKT_END);
3916 } else {
3917 ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF);
d211621f 3918 ssh2_pkt_adduint32(mainchan->remoteid);
6abbf9e3 3919 ssh2_pkt_send();
3920 }
67779be7 3921 logevent("Sent EOF message");
ec55b220 3922 } else if (code == TS_PING) {
3687d221 3923 if (ssh_state == SSH_STATE_CLOSED || ssh_state == SSH_STATE_PREPACKET)
3924 return;
ec55b220 3925 if (ssh_version == 1) {
3926 send_packet(SSH1_MSG_IGNORE, PKT_STR, "", PKT_END);
3927 } else {
3928 ssh2_pkt_init(SSH2_MSG_IGNORE);
3929 ssh2_pkt_addstring_start();
3930 ssh2_pkt_send();
3931 }
6abbf9e3 3932 } else {
3933 /* do nothing */
3934 }
374330e2 3935}
3936
8df7a775 3937static Socket ssh_socket(void) { return s; }
8ccc75b0 3938
3939static int ssh_sendok(void) { return ssh_send_ok; }
fb09bf1c 3940
0965bee0 3941static int ssh_ldisc(int option) {
3942 if (option == LD_ECHO) return ssh_echoing;
3943 if (option == LD_EDIT) return ssh_editing;
3944 return FALSE;
3945}
3946
374330e2 3947Backend ssh_backend = {
3948 ssh_init,
374330e2 3949 ssh_send,
3950 ssh_size,
4017be6d 3951 ssh_special,
8ccc75b0 3952 ssh_socket,
97db3be4 3953 ssh_sendok,
0965bee0 3954 ssh_ldisc,
97db3be4 3955 22
374330e2 3956};