17 #define SSH1_MSG_DISCONNECT 1 /* 0x1 */
18 #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */
19 #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */
20 #define SSH1_CMSG_USER 4 /* 0x4 */
21 #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */
22 #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */
23 #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */
24 #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */
25 #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */
26 #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */
27 #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */
28 #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */
29 #define SSH1_SMSG_SUCCESS 14 /* 0xe */
30 #define SSH1_SMSG_FAILURE 15 /* 0xf */
31 #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */
32 #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */
33 #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */
34 #define SSH1_CMSG_EOF 19 /* 0x13 */
35 #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */
36 #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */
37 #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */
38 #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */
39 #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */
40 #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */
41 #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */
42 #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */
43 #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */
44 #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */
45 #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */
46 #define SSH1_MSG_IGNORE 32 /* 0x20 */
47 #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */
48 #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */
49 #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */
50 #define SSH1_MSG_DEBUG 36 /* 0x24 */
51 #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */
52 #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */
53 #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */
54 #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */
55 #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */
56 #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */
57 #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */
59 #define SSH1_AUTH_TIS 5 /* 0x5 */
60 #define SSH1_AUTH_CCARD 16 /* 0x10 */
62 #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */
63 /* Mask for protoflags we will echo back to server if seen */
64 #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */
66 #define SSH2_MSG_DISCONNECT 1 /* 0x1 */
67 #define SSH2_MSG_IGNORE 2 /* 0x2 */
68 #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */
69 #define SSH2_MSG_DEBUG 4 /* 0x4 */
70 #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */
71 #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */
72 #define SSH2_MSG_KEXINIT 20 /* 0x14 */
73 #define SSH2_MSG_NEWKEYS 21 /* 0x15 */
74 #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */
75 #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */
76 #define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */
77 #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */
78 #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */
79 #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */
80 #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */
81 #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */
82 #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */
83 #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */
84 #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */
85 #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */
86 #define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */
87 #define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */
88 #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */
89 #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */
90 #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */
91 #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */
92 #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */
93 #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */
94 #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */
95 #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */
96 #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */
97 #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */
98 #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */
99 #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */
100 #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */
101 #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */
104 * Packet type contexts, so that ssh2_pkt_type can correctly decode
105 * the ambiguous type numbers back into the correct type strings.
107 #define SSH2_PKTCTX_DHGROUP1 0x0001
108 #define SSH2_PKTCTX_DHGEX 0x0002
109 #define SSH2_PKTCTX_PUBLICKEY 0x0010
110 #define SSH2_PKTCTX_PASSWORD 0x0020
111 #define SSH2_PKTCTX_KBDINTER 0x0040
112 #define SSH2_PKTCTX_AUTH_MASK 0x00F0
114 #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */
115 #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */
116 #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */
117 #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */
118 #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */
119 #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */
120 #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */
121 #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */
122 #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */
123 #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */
124 #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */
125 #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */
126 #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */
127 #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */
128 #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */
130 static const char *const ssh2_disconnect_reasons
[] = {
132 "SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT",
133 "SSH_DISCONNECT_PROTOCOL_ERROR",
134 "SSH_DISCONNECT_KEY_EXCHANGE_FAILED",
135 "SSH_DISCONNECT_HOST_AUTHENTICATION_FAILED",
136 "SSH_DISCONNECT_MAC_ERROR",
137 "SSH_DISCONNECT_COMPRESSION_ERROR",
138 "SSH_DISCONNECT_SERVICE_NOT_AVAILABLE",
139 "SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED",
140 "SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE",
141 "SSH_DISCONNECT_CONNECTION_LOST",
142 "SSH_DISCONNECT_BY_APPLICATION",
143 "SSH_DISCONNECT_TOO_MANY_CONNECTIONS",
144 "SSH_DISCONNECT_AUTH_CANCELLED_BY_USER",
145 "SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE",
146 "SSH_DISCONNECT_ILLEGAL_USER_NAME",
149 #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */
150 #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */
151 #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */
152 #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */
154 #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */
157 * Various remote-bug flags.
159 #define BUG_CHOKES_ON_SSH1_IGNORE 1
160 #define BUG_SSH2_HMAC 2
161 #define BUG_NEEDS_SSH1_PLAIN_PASSWORD 4
162 #define BUG_CHOKES_ON_RSA 8
163 #define BUG_SSH2_RSA_PADDING 16
164 #define BUG_SSH2_DERIVEKEY 32
165 #define BUG_SSH2_DH_GEX 64
166 #define BUG_SSH2_PK_SESSIONID 128
168 #define translate(x) if (type == x) return #x
169 #define translatec(x,ctx) if (type == x && (pkt_ctx & ctx)) return #x
170 static char *ssh1_pkt_type(int type
)
172 translate(SSH1_MSG_DISCONNECT
);
173 translate(SSH1_SMSG_PUBLIC_KEY
);
174 translate(SSH1_CMSG_SESSION_KEY
);
175 translate(SSH1_CMSG_USER
);
176 translate(SSH1_CMSG_AUTH_RSA
);
177 translate(SSH1_SMSG_AUTH_RSA_CHALLENGE
);
178 translate(SSH1_CMSG_AUTH_RSA_RESPONSE
);
179 translate(SSH1_CMSG_AUTH_PASSWORD
);
180 translate(SSH1_CMSG_REQUEST_PTY
);
181 translate(SSH1_CMSG_WINDOW_SIZE
);
182 translate(SSH1_CMSG_EXEC_SHELL
);
183 translate(SSH1_CMSG_EXEC_CMD
);
184 translate(SSH1_SMSG_SUCCESS
);
185 translate(SSH1_SMSG_FAILURE
);
186 translate(SSH1_CMSG_STDIN_DATA
);
187 translate(SSH1_SMSG_STDOUT_DATA
);
188 translate(SSH1_SMSG_STDERR_DATA
);
189 translate(SSH1_CMSG_EOF
);
190 translate(SSH1_SMSG_EXIT_STATUS
);
191 translate(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
);
192 translate(SSH1_MSG_CHANNEL_OPEN_FAILURE
);
193 translate(SSH1_MSG_CHANNEL_DATA
);
194 translate(SSH1_MSG_CHANNEL_CLOSE
);
195 translate(SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION
);
196 translate(SSH1_SMSG_X11_OPEN
);
197 translate(SSH1_CMSG_PORT_FORWARD_REQUEST
);
198 translate(SSH1_MSG_PORT_OPEN
);
199 translate(SSH1_CMSG_AGENT_REQUEST_FORWARDING
);
200 translate(SSH1_SMSG_AGENT_OPEN
);
201 translate(SSH1_MSG_IGNORE
);
202 translate(SSH1_CMSG_EXIT_CONFIRMATION
);
203 translate(SSH1_CMSG_X11_REQUEST_FORWARDING
);
204 translate(SSH1_CMSG_AUTH_RHOSTS_RSA
);
205 translate(SSH1_MSG_DEBUG
);
206 translate(SSH1_CMSG_REQUEST_COMPRESSION
);
207 translate(SSH1_CMSG_AUTH_TIS
);
208 translate(SSH1_SMSG_AUTH_TIS_CHALLENGE
);
209 translate(SSH1_CMSG_AUTH_TIS_RESPONSE
);
210 translate(SSH1_CMSG_AUTH_CCARD
);
211 translate(SSH1_SMSG_AUTH_CCARD_CHALLENGE
);
212 translate(SSH1_CMSG_AUTH_CCARD_RESPONSE
);
215 static char *ssh2_pkt_type(int pkt_ctx
, int type
)
217 translate(SSH2_MSG_DISCONNECT
);
218 translate(SSH2_MSG_IGNORE
);
219 translate(SSH2_MSG_UNIMPLEMENTED
);
220 translate(SSH2_MSG_DEBUG
);
221 translate(SSH2_MSG_SERVICE_REQUEST
);
222 translate(SSH2_MSG_SERVICE_ACCEPT
);
223 translate(SSH2_MSG_KEXINIT
);
224 translate(SSH2_MSG_NEWKEYS
);
225 translatec(SSH2_MSG_KEXDH_INIT
, SSH2_PKTCTX_DHGROUP1
);
226 translatec(SSH2_MSG_KEXDH_REPLY
, SSH2_PKTCTX_DHGROUP1
);
227 translatec(SSH2_MSG_KEX_DH_GEX_REQUEST
, SSH2_PKTCTX_DHGEX
);
228 translatec(SSH2_MSG_KEX_DH_GEX_GROUP
, SSH2_PKTCTX_DHGEX
);
229 translatec(SSH2_MSG_KEX_DH_GEX_INIT
, SSH2_PKTCTX_DHGEX
);
230 translatec(SSH2_MSG_KEX_DH_GEX_REPLY
, SSH2_PKTCTX_DHGEX
);
231 translate(SSH2_MSG_USERAUTH_REQUEST
);
232 translate(SSH2_MSG_USERAUTH_FAILURE
);
233 translate(SSH2_MSG_USERAUTH_SUCCESS
);
234 translate(SSH2_MSG_USERAUTH_BANNER
);
235 translatec(SSH2_MSG_USERAUTH_PK_OK
, SSH2_PKTCTX_PUBLICKEY
);
236 translatec(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
, SSH2_PKTCTX_PASSWORD
);
237 translatec(SSH2_MSG_USERAUTH_INFO_REQUEST
, SSH2_PKTCTX_KBDINTER
);
238 translatec(SSH2_MSG_USERAUTH_INFO_RESPONSE
, SSH2_PKTCTX_KBDINTER
);
239 translate(SSH2_MSG_GLOBAL_REQUEST
);
240 translate(SSH2_MSG_REQUEST_SUCCESS
);
241 translate(SSH2_MSG_REQUEST_FAILURE
);
242 translate(SSH2_MSG_CHANNEL_OPEN
);
243 translate(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
);
244 translate(SSH2_MSG_CHANNEL_OPEN_FAILURE
);
245 translate(SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
246 translate(SSH2_MSG_CHANNEL_DATA
);
247 translate(SSH2_MSG_CHANNEL_EXTENDED_DATA
);
248 translate(SSH2_MSG_CHANNEL_EOF
);
249 translate(SSH2_MSG_CHANNEL_CLOSE
);
250 translate(SSH2_MSG_CHANNEL_REQUEST
);
251 translate(SSH2_MSG_CHANNEL_SUCCESS
);
252 translate(SSH2_MSG_CHANNEL_FAILURE
);
258 #define GET_32BIT(cp) \
259 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
260 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
261 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
262 ((unsigned long)(unsigned char)(cp)[3]))
264 #define PUT_32BIT(cp, value) { \
265 (cp)[0] = (unsigned char)((value) >> 24); \
266 (cp)[1] = (unsigned char)((value) >> 16); \
267 (cp)[2] = (unsigned char)((value) >> 8); \
268 (cp)[3] = (unsigned char)(value); }
270 /* Enumeration values for fields in SSH-1 packets */
272 PKT_END
, PKT_INT
, PKT_CHAR
, PKT_DATA
, PKT_STR
, PKT_BIGNUM
,
273 /* These values are for communicating relevant semantics of
274 * fields to the packet logging code. */
275 PKTT_OTHER
, PKTT_PASSWORD
, PKTT_DATA
279 * Coroutine mechanics for the sillier bits of the code. If these
280 * macros look impenetrable to you, you might find it helpful to
283 * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
285 * which explains the theory behind these macros.
287 * In particular, if you are getting `case expression not constant'
288 * errors when building with MS Visual Studio, this is because MS's
289 * Edit and Continue debugging feature causes their compiler to
290 * violate ANSI C. To disable Edit and Continue debugging:
292 * - right-click ssh.c in the FileView
294 * - select the C/C++ tab and the General category
295 * - under `Debug info:', select anything _other_ than `Program
296 * Database for Edit and Continue'.
298 #define crBegin(v) { int *crLine = &v; switch(v) { case 0:;
301 if (!ssh->t) ssh->t = snew(struct t); \
303 #define crFinish(z) } *crLine = 0; return (z); }
304 #define crFinishV } *crLine = 0; return; }
305 #define crReturn(z) \
307 *crLine =__LINE__; return (z); case __LINE__:;\
311 *crLine=__LINE__; return; case __LINE__:;\
313 #define crStop(z) do{ *crLine = 0; return (z); }while(0)
314 #define crStopV do{ *crLine = 0; return; }while(0)
315 #define crWaitUntil(c) do { crReturn(0); } while (!(c))
316 #define crWaitUntilV(c) do { crReturnV; } while (!(c))
318 typedef struct ssh_tag
*Ssh
;
321 static struct Packet
*ssh2_pkt_init(int pkt_type
);
322 static void ssh2_pkt_addbool(struct Packet
*, unsigned char value
);
323 static void ssh2_pkt_adduint32(struct Packet
*, unsigned long value
);
324 static void ssh2_pkt_addstring_start(struct Packet
*);
325 static void ssh2_pkt_addstring_str(struct Packet
*, char *data
);
326 static void ssh2_pkt_addstring_data(struct Packet
*, char *data
, int len
);
327 static void ssh2_pkt_addstring(struct Packet
*, char *data
);
328 static unsigned char *ssh2_mpint_fmt(Bignum b
, int *len
);
329 static void ssh2_pkt_addmp(struct Packet
*, Bignum b
);
330 static int ssh2_pkt_construct(Ssh
, struct Packet
*);
331 static void ssh2_pkt_send(Ssh
, struct Packet
*);
332 static void ssh2_pkt_send_noqueue(Ssh
, struct Packet
*);
333 static int do_ssh1_login(Ssh ssh
, unsigned char *in
, int inlen
,
334 struct Packet
*pktin
);
335 static void do_ssh2_authconn(Ssh ssh
, unsigned char *in
, int inlen
,
336 struct Packet
*pktin
);
339 * Buffer management constants. There are several of these for
340 * various different purposes:
342 * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up
343 * on a local data stream before we throttle the whole SSH
344 * connection (in SSH1 only). Throttling the whole connection is
345 * pretty drastic so we set this high in the hope it won't
348 * - SSH_MAX_BACKLOG is the amount of backlog that must build up
349 * on the SSH connection itself before we defensively throttle
350 * _all_ local data streams. This is pretty drastic too (though
351 * thankfully unlikely in SSH2 since the window mechanism should
352 * ensure that the server never has any need to throttle its end
353 * of the connection), so we set this high as well.
355 * - OUR_V2_WINSIZE is the maximum window size we present on SSH2
359 #define SSH1_BUFFER_LIMIT 32768
360 #define SSH_MAX_BACKLOG 32768
361 #define OUR_V2_WINSIZE 16384
363 const static struct ssh_kex
*kex_algs
[] = {
364 &ssh_diffiehellman_gex
,
368 const static struct ssh_signkey
*hostkey_algs
[] = { &ssh_rsa
, &ssh_dss
};
370 static void *nullmac_make_context(void)
374 static void nullmac_free_context(void *handle
)
377 static void nullmac_key(void *handle
, unsigned char *key
)
380 static void nullmac_generate(void *handle
, unsigned char *blk
, int len
,
384 static int nullmac_verify(void *handle
, unsigned char *blk
, int len
,
389 const static struct ssh_mac ssh_mac_none
= {
390 nullmac_make_context
, nullmac_free_context
, nullmac_key
,
391 nullmac_generate
, nullmac_verify
, "none", 0
393 const static struct ssh_mac
*macs
[] = {
394 &ssh_sha1
, &ssh_md5
, &ssh_mac_none
396 const static struct ssh_mac
*buggymacs
[] = {
397 &ssh_sha1_buggy
, &ssh_md5
, &ssh_mac_none
400 static void *ssh_comp_none_init(void)
404 static void ssh_comp_none_cleanup(void *handle
)
407 static int ssh_comp_none_block(void *handle
, unsigned char *block
, int len
,
408 unsigned char **outblock
, int *outlen
)
412 static int ssh_comp_none_disable(void *handle
)
416 const static struct ssh_compress ssh_comp_none
= {
418 ssh_comp_none_init
, ssh_comp_none_cleanup
, ssh_comp_none_block
,
419 ssh_comp_none_init
, ssh_comp_none_cleanup
, ssh_comp_none_block
,
420 ssh_comp_none_disable
, NULL
422 extern const struct ssh_compress ssh_zlib
;
423 const static struct ssh_compress
*compressions
[] = {
424 &ssh_zlib
, &ssh_comp_none
427 enum { /* channel types */
432 CHAN_SOCKDATA_DORMANT
/* one the remote hasn't confirmed */
436 * 2-3-4 tree storing channels.
439 Ssh ssh
; /* pointer back to main context */
440 unsigned remoteid
, localid
;
443 * In SSH1, this value contains four bits:
445 * 1 We have sent SSH1_MSG_CHANNEL_CLOSE.
446 * 2 We have sent SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION.
447 * 4 We have received SSH1_MSG_CHANNEL_CLOSE.
448 * 8 We have received SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION.
450 * A channel is completely finished with when all four bits are set.
454 struct ssh1_data_channel
{
457 struct ssh2_data_channel
{
459 unsigned remwindow
, remmaxpkt
;
464 struct ssh_agent_channel
{
465 unsigned char *message
;
466 unsigned char msglen
[4];
467 unsigned lensofar
, totallen
;
469 struct ssh_x11_channel
{
472 struct ssh_pfd_channel
{
479 * 2-3-4 tree storing remote->local port forwardings. SSH 1 and SSH
480 * 2 use this structure in different ways, reflecting SSH 2's
481 * altogether saner approach to port forwarding.
483 * In SSH 1, you arrange a remote forwarding by sending the server
484 * the remote port number, and the local destination host:port.
485 * When a connection comes in, the server sends you back that
486 * host:port pair, and you connect to it. This is a ready-made
487 * security hole if you're not on the ball: a malicious server
488 * could send you back _any_ host:port pair, so if you trustingly
489 * connect to the address it gives you then you've just opened the
490 * entire inside of your corporate network just by connecting
491 * through it to a dodgy SSH server. Hence, we must store a list of
492 * host:port pairs we _are_ trying to forward to, and reject a
493 * connection request from the server if it's not in the list.
495 * In SSH 2, each side of the connection minds its own business and
496 * doesn't send unnecessary information to the other. You arrange a
497 * remote forwarding by sending the server just the remote port
498 * number. When a connection comes in, the server tells you which
499 * of its ports was connected to; and _you_ have to remember what
500 * local host:port pair went with that port number.
502 * Hence: in SSH 1 this structure stores host:port pairs we intend
503 * to allow connections to, and is indexed by those host:port
504 * pairs. In SSH 2 it stores a mapping from source port to
505 * destination host:port pair, and is indexed by source port.
507 struct ssh_rportfwd
{
508 unsigned sport
, dport
;
515 unsigned long sequence
;
522 * State associated with packet logging
526 struct logblank_t
*blanks
;
529 static void ssh1_protocol(Ssh ssh
, unsigned char *in
, int inlen
,
530 struct Packet
*pktin
);
531 static void ssh2_protocol(Ssh ssh
, unsigned char *in
, int inlen
,
532 struct Packet
*pktin
);
533 static void ssh1_protocol_setup(Ssh ssh
);
534 static void ssh2_protocol_setup(Ssh ssh
);
535 static void ssh_size(void *handle
, int width
, int height
);
536 static void ssh_special(void *handle
, Telnet_Special
);
537 static int ssh2_try_send(struct ssh_channel
*c
);
538 static void ssh2_add_channel_data(struct ssh_channel
*c
, char *buf
, int len
);
539 static void ssh_throttle_all(Ssh ssh
, int enable
, int bufsize
);
540 static void ssh2_set_window(struct ssh_channel
*c
, unsigned newwin
);
541 static int ssh_sendbuffer(void *handle
);
542 static void ssh_do_close(Ssh ssh
);
543 static unsigned long ssh_pkt_getuint32(struct Packet
*pkt
);
544 static int ssh2_pkt_getbool(struct Packet
*pkt
);
545 static void ssh_pkt_getstring(struct Packet
*pkt
, char **p
, int *length
);
547 struct rdpkt1_state_tag
{
548 long len
, pad
, biglen
, to_read
;
549 unsigned long realcrc
, gotcrc
;
553 struct Packet
*pktin
;
556 struct rdpkt2_state_tag
{
557 long len
, pad
, payload
, packetlen
, maclen
;
560 unsigned long incoming_sequence
;
561 struct Packet
*pktin
;
564 typedef void (*handler_fn_t
)(Ssh ssh
, struct Packet
*pktin
);
567 const struct plug_function_table
*fn
;
568 /* the above field _must_ be first in the structure */
570 SHA_State exhash
, exhashbase
;
577 unsigned char session_key
[32];
579 int v1_remote_protoflags
;
580 int v1_local_protoflags
;
581 int agentfwd_enabled
;
584 const struct ssh_cipher
*cipher
;
587 const struct ssh2_cipher
*cscipher
, *sccipher
;
588 void *cs_cipher_ctx
, *sc_cipher_ctx
;
589 const struct ssh_mac
*csmac
, *scmac
;
590 void *cs_mac_ctx
, *sc_mac_ctx
;
591 const struct ssh_compress
*cscomp
, *sccomp
;
592 void *cs_comp_ctx
, *sc_comp_ctx
;
593 const struct ssh_kex
*kex
;
594 const struct ssh_signkey
*hostkey
;
595 unsigned char v2_session_id
[20];
601 int echoing
, editing
;
605 int ospeed
, ispeed
; /* temporaries */
606 int term_width
, term_height
;
608 tree234
*channels
; /* indexed by local id */
609 struct ssh_channel
*mainchan
; /* primary session channel */
616 SSH_STATE_BEFORE_SIZE
,
622 int size_needed
, eof_needed
;
624 struct Packet
**queue
;
625 int queuelen
, queuesize
;
627 unsigned char *deferred_send_data
;
628 int deferred_len
, deferred_size
;
631 * Gross hack: pscp will try to start SFTP but fall back to
632 * scp1 if that fails. This variable is the means by which
633 * scp.c can reach into the SSH code and find out which one it
639 * Used for username and password input.
641 char *userpass_input_buffer
;
642 int userpass_input_buflen
;
643 int userpass_input_bufpos
;
644 int userpass_input_echo
;
646 char *portfwd_strptr
;
652 int v1_throttle_count
;
655 int v1_stdout_throttling
;
656 int v2_outgoing_sequence
;
658 int ssh1_rdpkt_crstate
;
659 int ssh2_rdpkt_crstate
;
660 int do_ssh_init_crstate
;
661 int ssh_gotdata_crstate
;
662 int do_ssh1_login_crstate
;
663 int do_ssh1_connection_crstate
;
664 int do_ssh2_transport_crstate
;
665 int do_ssh2_authconn_crstate
;
667 void *do_ssh_init_state
;
668 void *do_ssh1_login_state
;
669 void *do_ssh2_transport_state
;
670 void *do_ssh2_authconn_state
;
672 struct rdpkt1_state_tag rdpkt1_state
;
673 struct rdpkt2_state_tag rdpkt2_state
;
675 /* ssh1 and ssh2 use this for different things, but both use it */
676 int protocol_initial_phase_done
;
678 void (*protocol
) (Ssh ssh
, unsigned char *in
, int inlen
,
680 struct Packet
*(*s_rdpkt
) (Ssh ssh
, unsigned char **data
, int *datalen
);
683 * We maintain a full _copy_ of a Config structure here, not
684 * merely a pointer to it. That way, when we're passed a new
685 * one for reconfiguration, we can check the differences and
686 * potentially reconfigure port forwardings etc in mid-session.
691 * Used to transfer data back from async agent callbacks.
693 void *agent_response
;
694 int agent_response_len
;
697 * Dispatch table for packet types that we may have to deal
700 handler_fn_t packet_dispatch
[256];
703 * This module deals with sending keepalives.
708 #define logevent(s) logevent(ssh->frontend, s)
710 /* logevent, only printf-formatted. */
711 static void logeventf(Ssh ssh
, const char *fmt
, ...)
717 buf
= dupvprintf(fmt
, ap
);
723 #define bombout(msg) \
725 char *text = dupprintf msg; \
728 connection_fatal(ssh->frontend, "%s", text); \
732 /* Functions to leave bits out of the SSH packet log file. */
734 static void dont_log_password(Ssh ssh
, struct Packet
*pkt
, int blanktype
)
736 if (ssh
->cfg
.logomitpass
)
737 pkt
->logmode
= blanktype
;
740 static void dont_log_data(Ssh ssh
, struct Packet
*pkt
, int blanktype
)
742 if (ssh
->cfg
.logomitdata
)
743 pkt
->logmode
= blanktype
;
746 static void end_log_omission(Ssh ssh
, struct Packet
*pkt
)
748 pkt
->logmode
= PKTLOG_EMIT
;
751 static int ssh_channelcmp(void *av
, void *bv
)
753 struct ssh_channel
*a
= (struct ssh_channel
*) av
;
754 struct ssh_channel
*b
= (struct ssh_channel
*) bv
;
755 if (a
->localid
< b
->localid
)
757 if (a
->localid
> b
->localid
)
761 static int ssh_channelfind(void *av
, void *bv
)
763 unsigned *a
= (unsigned *) av
;
764 struct ssh_channel
*b
= (struct ssh_channel
*) bv
;
772 static int ssh_rportcmp_ssh1(void *av
, void *bv
)
774 struct ssh_rportfwd
*a
= (struct ssh_rportfwd
*) av
;
775 struct ssh_rportfwd
*b
= (struct ssh_rportfwd
*) bv
;
777 if ( (i
= strcmp(a
->dhost
, b
->dhost
)) != 0)
778 return i
< 0 ?
-1 : +1;
779 if (a
->dport
> b
->dport
)
781 if (a
->dport
< b
->dport
)
786 static int ssh_rportcmp_ssh2(void *av
, void *bv
)
788 struct ssh_rportfwd
*a
= (struct ssh_rportfwd
*) av
;
789 struct ssh_rportfwd
*b
= (struct ssh_rportfwd
*) bv
;
791 if (a
->sport
> b
->sport
)
793 if (a
->sport
< b
->sport
)
798 static int alloc_channel_id(Ssh ssh
)
800 const unsigned CHANNEL_NUMBER_OFFSET
= 256;
801 unsigned low
, high
, mid
;
803 struct ssh_channel
*c
;
806 * First-fit allocation of channel numbers: always pick the
807 * lowest unused one. To do this, binary-search using the
808 * counted B-tree to find the largest channel ID which is in a
809 * contiguous sequence from the beginning. (Precisely
810 * everything in that sequence must have ID equal to its tree
811 * index plus CHANNEL_NUMBER_OFFSET.)
813 tsize
= count234(ssh
->channels
);
817 while (high
- low
> 1) {
818 mid
= (high
+ low
) / 2;
819 c
= index234(ssh
->channels
, mid
);
820 if (c
->localid
== mid
+ CHANNEL_NUMBER_OFFSET
)
821 low
= mid
; /* this one is fine */
823 high
= mid
; /* this one is past it */
826 * Now low points to either -1, or the tree index of the
827 * largest ID in the initial sequence.
830 unsigned i
= low
+ 1 + CHANNEL_NUMBER_OFFSET
;
831 assert(NULL
== find234(ssh
->channels
, &i
, ssh_channelfind
));
833 return low
+ 1 + CHANNEL_NUMBER_OFFSET
;
836 static void c_write(Ssh ssh
, const char *buf
, int len
)
838 if ((flags
& FLAG_STDERR
)) {
840 for (i
= 0; i
< len
; i
++)
842 fputc(buf
[i
], stderr
);
845 from_backend(ssh
->frontend
, 1, buf
, len
);
848 static void c_write_untrusted(Ssh ssh
, const char *buf
, int len
)
851 for (i
= 0; i
< len
; i
++) {
853 c_write(ssh
, "\r\n", 2);
854 else if ((buf
[i
] & 0x60) || (buf
[i
] == '\r'))
855 c_write(ssh
, buf
+ i
, 1);
859 static void c_write_str(Ssh ssh
, const char *buf
)
861 c_write(ssh
, buf
, strlen(buf
));
864 static void ssh_free_packet(struct Packet
*pkt
)
869 static struct Packet
*ssh_new_packet(void)
871 struct Packet
*pkt
= snew(struct Packet
);
875 pkt
->logmode
= PKTLOG_EMIT
;
883 * Collect incoming data in the incoming packet buffer.
884 * Decipher and verify the packet when it is completely read.
885 * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets.
886 * Update the *data and *datalen variables.
887 * Return a Packet structure when a packet is completed.
889 static struct Packet
*ssh1_rdpkt(Ssh ssh
, unsigned char **data
, int *datalen
)
891 struct rdpkt1_state_tag
*st
= &ssh
->rdpkt1_state
;
893 crBegin(ssh
->ssh1_rdpkt_crstate
);
895 st
->pktin
= ssh_new_packet();
898 st
->pktin
->length
= 0;
900 for (st
->i
= st
->len
= 0; st
->i
< 4; st
->i
++) {
901 while ((*datalen
) == 0)
903 st
->len
= (st
->len
<< 8) + **data
;
904 (*data
)++, (*datalen
)--;
907 st
->pad
= 8 - (st
->len
% 8);
908 st
->biglen
= st
->len
+ st
->pad
;
909 st
->pktin
->length
= st
->len
- 5;
911 if (st
->biglen
< 0) {
912 bombout(("Extremely large packet length from server suggests"
913 " data stream corruption"));
914 ssh_free_packet(st
->pktin
);
918 st
->pktin
->maxlen
= st
->biglen
;
919 st
->pktin
->data
= snewn(st
->biglen
+ APIEXTRA
, unsigned char);
921 st
->to_read
= st
->biglen
;
922 st
->p
= st
->pktin
->data
;
923 while (st
->to_read
> 0) {
924 st
->chunk
= st
->to_read
;
925 while ((*datalen
) == 0)
927 if (st
->chunk
> (*datalen
))
928 st
->chunk
= (*datalen
);
929 memcpy(st
->p
, *data
, st
->chunk
);
931 *datalen
-= st
->chunk
;
933 st
->to_read
-= st
->chunk
;
936 if (ssh
->cipher
&& detect_attack(ssh
->crcda_ctx
, st
->pktin
->data
,
938 bombout(("Network attack (CRC compensation) detected!"));
939 ssh_free_packet(st
->pktin
);
944 ssh
->cipher
->decrypt(ssh
->v1_cipher_ctx
, st
->pktin
->data
, st
->biglen
);
946 st
->realcrc
= crc32_compute(st
->pktin
->data
, st
->biglen
- 4);
947 st
->gotcrc
= GET_32BIT(st
->pktin
->data
+ st
->biglen
- 4);
948 if (st
->gotcrc
!= st
->realcrc
) {
949 bombout(("Incorrect CRC received on packet"));
950 ssh_free_packet(st
->pktin
);
954 st
->pktin
->body
= st
->pktin
->data
+ st
->pad
+ 1;
955 st
->pktin
->savedpos
= 0;
957 if (ssh
->v1_compressing
) {
958 unsigned char *decompblk
;
960 if (!zlib_decompress_block(ssh
->sc_comp_ctx
,
961 st
->pktin
->body
- 1, st
->pktin
->length
+ 1,
962 &decompblk
, &decomplen
)) {
963 bombout(("Zlib decompression encountered invalid data"));
964 ssh_free_packet(st
->pktin
);
968 if (st
->pktin
->maxlen
< st
->pad
+ decomplen
) {
969 st
->pktin
->maxlen
= st
->pad
+ decomplen
;
970 st
->pktin
->data
= sresize(st
->pktin
->data
,
971 st
->pktin
->maxlen
+ APIEXTRA
,
973 st
->pktin
->body
= st
->pktin
->data
+ st
->pad
+ 1;
976 memcpy(st
->pktin
->body
- 1, decompblk
, decomplen
);
978 st
->pktin
->length
= decomplen
- 1;
981 st
->pktin
->type
= st
->pktin
->body
[-1];
984 * Log incoming packet, possibly omitting sensitive fields.
988 struct logblank_t blank
;
989 if (ssh
->cfg
.logomitdata
) {
990 int do_blank
= FALSE
, blank_prefix
= 0;
991 /* "Session data" packets - omit the data field */
992 if ((st
->pktin
->type
== SSH1_SMSG_STDOUT_DATA
) ||
993 (st
->pktin
->type
== SSH1_SMSG_STDERR_DATA
)) {
994 do_blank
= TRUE
; blank_prefix
= 0;
995 } else if (st
->pktin
->type
== SSH1_MSG_CHANNEL_DATA
) {
996 do_blank
= TRUE
; blank_prefix
= 4;
999 blank
.offset
= blank_prefix
;
1000 blank
.len
= st
->pktin
->length
;
1001 blank
.type
= PKTLOG_OMIT
;
1005 log_packet(ssh
->logctx
,
1006 PKT_INCOMING
, st
->pktin
->type
,
1007 ssh1_pkt_type(st
->pktin
->type
),
1008 st
->pktin
->body
, st
->pktin
->length
,
1012 crFinish(st
->pktin
);
1015 static struct Packet
*ssh2_rdpkt(Ssh ssh
, unsigned char **data
, int *datalen
)
1017 struct rdpkt2_state_tag
*st
= &ssh
->rdpkt2_state
;
1019 crBegin(ssh
->ssh2_rdpkt_crstate
);
1021 st
->pktin
= ssh_new_packet();
1023 st
->pktin
->type
= 0;
1024 st
->pktin
->length
= 0;
1026 st
->cipherblk
= ssh
->sccipher
->blksize
;
1029 if (st
->cipherblk
< 8)
1032 st
->pktin
->data
= snewn(st
->cipherblk
+ APIEXTRA
, unsigned char);
1035 * Acquire and decrypt the first block of the packet. This will
1036 * contain the length and padding details.
1038 for (st
->i
= st
->len
= 0; st
->i
< st
->cipherblk
; st
->i
++) {
1039 while ((*datalen
) == 0)
1041 st
->pktin
->data
[st
->i
] = *(*data
)++;
1046 ssh
->sccipher
->decrypt(ssh
->sc_cipher_ctx
,
1047 st
->pktin
->data
, st
->cipherblk
);
1050 * Now get the length and padding figures.
1052 st
->len
= GET_32BIT(st
->pktin
->data
);
1053 st
->pad
= st
->pktin
->data
[4];
1056 * _Completely_ silly lengths should be stomped on before they
1057 * do us any more damage.
1059 if (st
->len
< 0 || st
->pad
< 0 || st
->len
+ st
->pad
< 0) {
1060 bombout(("Incoming packet was garbled on decryption"));
1061 ssh_free_packet(st
->pktin
);
1066 * This enables us to deduce the payload length.
1068 st
->payload
= st
->len
- st
->pad
- 1;
1070 st
->pktin
->length
= st
->payload
+ 5;
1073 * So now we can work out the total packet length.
1075 st
->packetlen
= st
->len
+ 4;
1076 st
->maclen
= ssh
->scmac ? ssh
->scmac
->len
: 0;
1079 * Allocate memory for the rest of the packet.
1081 st
->pktin
->maxlen
= st
->packetlen
+ st
->maclen
;
1082 st
->pktin
->data
= sresize(st
->pktin
->data
,
1083 st
->pktin
->maxlen
+ APIEXTRA
,
1087 * Read and decrypt the remainder of the packet.
1089 for (st
->i
= st
->cipherblk
; st
->i
< st
->packetlen
+ st
->maclen
;
1091 while ((*datalen
) == 0)
1093 st
->pktin
->data
[st
->i
] = *(*data
)++;
1096 /* Decrypt everything _except_ the MAC. */
1098 ssh
->sccipher
->decrypt(ssh
->sc_cipher_ctx
,
1099 st
->pktin
->data
+ st
->cipherblk
,
1100 st
->packetlen
- st
->cipherblk
);
1106 && !ssh
->scmac
->verify(ssh
->sc_mac_ctx
, st
->pktin
->data
, st
->len
+ 4,
1107 st
->incoming_sequence
)) {
1108 bombout(("Incorrect MAC received on packet"));
1109 ssh_free_packet(st
->pktin
);
1113 st
->pktin
->sequence
= st
->incoming_sequence
++;
1116 * Decompress packet payload.
1119 unsigned char *newpayload
;
1122 ssh
->sccomp
->decompress(ssh
->sc_comp_ctx
,
1123 st
->pktin
->data
+ 5, st
->pktin
->length
- 5,
1124 &newpayload
, &newlen
)) {
1125 if (st
->pktin
->maxlen
< newlen
+ 5) {
1126 st
->pktin
->maxlen
= newlen
+ 5;
1127 st
->pktin
->data
= sresize(st
->pktin
->data
,
1128 st
->pktin
->maxlen
+ APIEXTRA
,
1131 st
->pktin
->length
= 5 + newlen
;
1132 memcpy(st
->pktin
->data
+ 5, newpayload
, newlen
);
1137 st
->pktin
->savedpos
= 6;
1138 st
->pktin
->body
= st
->pktin
->data
;
1139 st
->pktin
->type
= st
->pktin
->data
[5];
1142 * Log incoming packet, possibly omitting sensitive fields.
1146 struct logblank_t blank
;
1147 if (ssh
->cfg
.logomitdata
) {
1148 int do_blank
= FALSE
, blank_prefix
= 0;
1149 /* "Session data" packets - omit the data field */
1150 if (st
->pktin
->type
== SSH2_MSG_CHANNEL_DATA
) {
1151 do_blank
= TRUE
; blank_prefix
= 4;
1152 } else if (st
->pktin
->type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
) {
1153 do_blank
= TRUE
; blank_prefix
= 8;
1156 blank
.offset
= blank_prefix
;
1157 blank
.len
= (st
->pktin
->length
-6) - blank_prefix
;
1158 blank
.type
= PKTLOG_OMIT
;
1162 log_packet(ssh
->logctx
, PKT_INCOMING
, st
->pktin
->type
,
1163 ssh2_pkt_type(ssh
->pkt_ctx
, st
->pktin
->type
),
1164 st
->pktin
->data
+6, st
->pktin
->length
-6,
1168 crFinish(st
->pktin
);
1171 static void ssh1_pktout_size(struct Packet
*pkt
, int len
)
1175 len
+= 5; /* type and CRC */
1176 pad
= 8 - (len
% 8);
1179 pkt
->length
= len
- 5;
1180 if (pkt
->maxlen
< biglen
) {
1181 pkt
->maxlen
= biglen
;
1182 pkt
->data
= sresize(pkt
->data
, biglen
+ 4 + APIEXTRA
, unsigned char);
1184 pkt
->body
= pkt
->data
+ 4 + pad
+ 1;
1187 static struct Packet
*s_wrpkt_start(int type
, int len
)
1189 struct Packet
*pkt
= ssh_new_packet();
1190 ssh1_pktout_size(pkt
, len
);
1192 /* Initialise log omission state */
1198 static int s_wrpkt_prepare(Ssh ssh
, struct Packet
*pkt
)
1204 * XXX various versions of SC (including 8.8.4) screw up the
1205 * register allocation in this function and use the same register
1206 * (D6) for len and as a temporary, with predictable results. The
1207 * following sledgehammer prevents this.
1213 pkt
->body
[-1] = pkt
->type
;
1216 log_packet(ssh
->logctx
, PKT_OUTGOING
, pkt
->type
,
1217 ssh1_pkt_type(pkt
->type
),
1218 pkt
->body
, pkt
->length
,
1219 pkt
->nblanks
, pkt
->blanks
);
1220 sfree(pkt
->blanks
); pkt
->blanks
= NULL
;
1223 if (ssh
->v1_compressing
) {
1224 unsigned char *compblk
;
1226 zlib_compress_block(ssh
->cs_comp_ctx
,
1227 pkt
->body
- 1, pkt
->length
+ 1,
1228 &compblk
, &complen
);
1229 ssh1_pktout_size(pkt
, complen
- 1);
1230 memcpy(pkt
->body
- 1, compblk
, complen
);
1234 len
= pkt
->length
+ 5; /* type and CRC */
1235 pad
= 8 - (len
% 8);
1238 for (i
= 0; i
< pad
; i
++)
1239 pkt
->data
[i
+ 4] = random_byte();
1240 crc
= crc32_compute(pkt
->data
+ 4, biglen
- 4);
1241 PUT_32BIT(pkt
->data
+ biglen
, crc
);
1242 PUT_32BIT(pkt
->data
, len
);
1245 ssh
->cipher
->encrypt(ssh
->v1_cipher_ctx
, pkt
->data
+ 4, biglen
);
1250 static void s_wrpkt(Ssh ssh
, struct Packet
*pkt
)
1253 len
= s_wrpkt_prepare(ssh
, pkt
);
1254 backlog
= sk_write(ssh
->s
, (char *)pkt
->data
, len
);
1255 if (backlog
> SSH_MAX_BACKLOG
)
1256 ssh_throttle_all(ssh
, 1, backlog
);
1259 static void s_wrpkt_defer(Ssh ssh
, struct Packet
*pkt
)
1262 len
= s_wrpkt_prepare(ssh
, pkt
);
1263 if (ssh
->deferred_len
+ len
> ssh
->deferred_size
) {
1264 ssh
->deferred_size
= ssh
->deferred_len
+ len
+ 128;
1265 ssh
->deferred_send_data
= sresize(ssh
->deferred_send_data
,
1269 memcpy(ssh
->deferred_send_data
+ ssh
->deferred_len
, pkt
->data
, len
);
1270 ssh
->deferred_len
+= len
;
1274 * Construct a packet with the specified contents.
1276 static struct Packet
*construct_packet(Ssh ssh
, int pkttype
,
1277 va_list ap1
, va_list ap2
)
1279 unsigned char *p
, *argp
, argchar
;
1280 unsigned long argint
;
1281 int pktlen
, argtype
, arglen
;
1286 while ((argtype
= va_arg(ap1
, int)) != PKT_END
) {
1289 (void) va_arg(ap1
, int);
1293 (void) va_arg(ap1
, int);
1297 (void) va_arg(ap1
, unsigned char *);
1298 arglen
= va_arg(ap1
, int);
1302 argp
= va_arg(ap1
, unsigned char *);
1303 arglen
= strlen((char *)argp
);
1304 pktlen
+= 4 + arglen
;
1307 bn
= va_arg(ap1
, Bignum
);
1308 pktlen
+= ssh1_bignum_length(bn
);
1313 /* ignore this pass */
1320 pkt
= s_wrpkt_start(pkttype
, pktlen
);
1323 while ((argtype
= va_arg(ap2
, int)) != PKT_END
) {
1324 int offset
= p
- pkt
->body
, len
= 0;
1326 /* Actual fields in the packet */
1328 argint
= va_arg(ap2
, int);
1329 PUT_32BIT(p
, argint
);
1333 argchar
= (unsigned char) va_arg(ap2
, int);
1338 argp
= va_arg(ap2
, unsigned char *);
1339 arglen
= va_arg(ap2
, int);
1340 memcpy(p
, argp
, arglen
);
1344 argp
= va_arg(ap2
, unsigned char *);
1345 arglen
= strlen((char *)argp
);
1346 PUT_32BIT(p
, arglen
);
1347 memcpy(p
+ 4, argp
, arglen
);
1351 bn
= va_arg(ap2
, Bignum
);
1352 len
= ssh1_write_bignum(p
, bn
);
1354 /* Tokens for modifications to packet logging */
1356 dont_log_password(ssh
, pkt
, PKTLOG_BLANK
);
1359 dont_log_data(ssh
, pkt
, PKTLOG_OMIT
);
1362 end_log_omission(ssh
, pkt
);
1366 /* Deal with logfile omission, if required. */
1367 if (len
&& (pkt
->logmode
!= PKTLOG_EMIT
)) {
1369 pkt
->blanks
= sresize(pkt
->blanks
, pkt
->nblanks
,
1371 pkt
->blanks
[pkt
->nblanks
-1].offset
= offset
;
1372 pkt
->blanks
[pkt
->nblanks
-1].len
= len
;
1373 pkt
->blanks
[pkt
->nblanks
-1].type
= pkt
->logmode
;
1380 static void send_packet(Ssh ssh
, int pkttype
, ...)
1384 va_start(ap1
, pkttype
);
1385 va_start(ap2
, pkttype
);
1386 pkt
= construct_packet(ssh
, pkttype
, ap1
, ap2
);
1390 ssh_free_packet(pkt
);
1393 static void defer_packet(Ssh ssh
, int pkttype
, ...)
1397 va_start(ap1
, pkttype
);
1398 va_start(ap2
, pkttype
);
1399 pkt
= construct_packet(ssh
, pkttype
, ap1
, ap2
);
1402 s_wrpkt_defer(ssh
, pkt
);
1403 ssh_free_packet(pkt
);
1406 static int ssh_versioncmp(char *a
, char *b
)
1409 unsigned long av
, bv
;
1411 av
= strtoul(a
, &ae
, 10);
1412 bv
= strtoul(b
, &be
, 10);
1414 return (av
< bv ?
-1 : +1);
1419 av
= strtoul(ae
, &ae
, 10);
1420 bv
= strtoul(be
, &be
, 10);
1422 return (av
< bv ?
-1 : +1);
1427 * Utility routines for putting an SSH-protocol `string' and
1428 * `uint32' into a SHA state.
1431 static void sha_string(SHA_State
* s
, void *str
, int len
)
1433 unsigned char lenblk
[4];
1434 PUT_32BIT(lenblk
, len
);
1435 SHA_Bytes(s
, lenblk
, 4);
1436 SHA_Bytes(s
, str
, len
);
1439 static void sha_uint32(SHA_State
* s
, unsigned i
)
1441 unsigned char intblk
[4];
1442 PUT_32BIT(intblk
, i
);
1443 SHA_Bytes(s
, intblk
, 4);
1447 * SSH2 packet construction functions.
1449 static void ssh2_pkt_ensure(struct Packet
*pkt
, int length
)
1451 if (pkt
->maxlen
< length
) {
1452 pkt
->maxlen
= length
+ 256;
1453 pkt
->data
= sresize(pkt
->data
, pkt
->maxlen
+ APIEXTRA
, unsigned char);
1456 static void ssh2_pkt_adddata(struct Packet
*pkt
, void *data
, int len
)
1458 if (pkt
->logmode
!= PKTLOG_EMIT
) {
1460 pkt
->blanks
= sresize(pkt
->blanks
, pkt
->nblanks
, struct logblank_t
);
1461 pkt
->blanks
[pkt
->nblanks
-1].offset
= pkt
->length
- 6;
1462 pkt
->blanks
[pkt
->nblanks
-1].len
= len
;
1463 pkt
->blanks
[pkt
->nblanks
-1].type
= pkt
->logmode
;
1466 ssh2_pkt_ensure(pkt
, pkt
->length
);
1467 memcpy(pkt
->data
+ pkt
->length
- len
, data
, len
);
1469 static void ssh2_pkt_addbyte(struct Packet
*pkt
, unsigned char byte
)
1471 ssh2_pkt_adddata(pkt
, &byte
, 1);
1473 static struct Packet
*ssh2_pkt_init(int pkt_type
)
1475 struct Packet
*pkt
= ssh_new_packet();
1477 ssh2_pkt_addbyte(pkt
, (unsigned char) pkt_type
);
1480 static void ssh2_pkt_addbool(struct Packet
*pkt
, unsigned char value
)
1482 ssh2_pkt_adddata(pkt
, &value
, 1);
1484 static void ssh2_pkt_adduint32(struct Packet
*pkt
, unsigned long value
)
1487 PUT_32BIT(x
, value
);
1488 ssh2_pkt_adddata(pkt
, x
, 4);
1490 static void ssh2_pkt_addstring_start(struct Packet
*pkt
)
1492 ssh2_pkt_adduint32(pkt
, 0);
1493 pkt
->savedpos
= pkt
->length
;
1495 static void ssh2_pkt_addstring_str(struct Packet
*pkt
, char *data
)
1497 ssh2_pkt_adddata(pkt
, data
, strlen(data
));
1498 PUT_32BIT(pkt
->data
+ pkt
->savedpos
- 4, pkt
->length
- pkt
->savedpos
);
1500 static void ssh2_pkt_addstring_data(struct Packet
*pkt
, char *data
, int len
)
1502 ssh2_pkt_adddata(pkt
, data
, len
);
1503 PUT_32BIT(pkt
->data
+ pkt
->savedpos
- 4, pkt
->length
- pkt
->savedpos
);
1505 static void ssh2_pkt_addstring(struct Packet
*pkt
, char *data
)
1507 ssh2_pkt_addstring_start(pkt
);
1508 ssh2_pkt_addstring_str(pkt
, data
);
1510 static unsigned char *ssh2_mpint_fmt(Bignum b
, int *len
)
1513 int i
, n
= (bignum_bitcount(b
) + 7) / 8;
1514 p
= snewn(n
+ 1, unsigned char);
1516 fatalbox("out of memory");
1518 for (i
= 1; i
<= n
; i
++)
1519 p
[i
] = bignum_byte(b
, n
- i
);
1521 while (i
<= n
&& p
[i
] == 0 && (p
[i
+ 1] & 0x80) == 0)
1523 memmove(p
, p
+ i
, n
+ 1 - i
);
1527 static void ssh2_pkt_addmp(struct Packet
*pkt
, Bignum b
)
1531 p
= ssh2_mpint_fmt(b
, &len
);
1532 ssh2_pkt_addstring_start(pkt
);
1533 ssh2_pkt_addstring_data(pkt
, (char *)p
, len
);
1538 * Construct an SSH2 final-form packet: compress it, encrypt it,
1539 * put the MAC on it. Final packet, ready to be sent, is stored in
1540 * pkt->data. Total length is returned.
1542 static int ssh2_pkt_construct(Ssh ssh
, struct Packet
*pkt
)
1544 int cipherblk
, maclen
, padding
, i
;
1547 log_packet(ssh
->logctx
, PKT_OUTGOING
, pkt
->data
[5],
1548 ssh2_pkt_type(ssh
->pkt_ctx
, pkt
->data
[5]),
1549 pkt
->data
+ 6, pkt
->length
- 6,
1550 pkt
->nblanks
, pkt
->blanks
);
1551 sfree(pkt
->blanks
); pkt
->blanks
= NULL
;
1555 * Compress packet payload.
1558 unsigned char *newpayload
;
1561 ssh
->cscomp
->compress(ssh
->cs_comp_ctx
, pkt
->data
+ 5,
1563 &newpayload
, &newlen
)) {
1565 ssh2_pkt_adddata(pkt
, newpayload
, newlen
);
1571 * Add padding. At least four bytes, and must also bring total
1572 * length (minus MAC) up to a multiple of the block size.
1574 cipherblk
= ssh
->cscipher ? ssh
->cscipher
->blksize
: 8; /* block size */
1575 cipherblk
= cipherblk
< 8 ?
8 : cipherblk
; /* or 8 if blksize < 8 */
1578 (cipherblk
- (pkt
->length
+ padding
) % cipherblk
) % cipherblk
;
1579 maclen
= ssh
->csmac ? ssh
->csmac
->len
: 0;
1580 ssh2_pkt_ensure(pkt
, pkt
->length
+ padding
+ maclen
);
1581 pkt
->data
[4] = padding
;
1582 for (i
= 0; i
< padding
; i
++)
1583 pkt
->data
[pkt
->length
+ i
] = random_byte();
1584 PUT_32BIT(pkt
->data
, pkt
->length
+ padding
- 4);
1586 ssh
->csmac
->generate(ssh
->cs_mac_ctx
, pkt
->data
,
1587 pkt
->length
+ padding
,
1588 ssh
->v2_outgoing_sequence
);
1589 ssh
->v2_outgoing_sequence
++; /* whether or not we MACed */
1592 ssh
->cscipher
->encrypt(ssh
->cs_cipher_ctx
,
1593 pkt
->data
, pkt
->length
+ padding
);
1595 /* Ready-to-send packet starts at pkt->data. We return length. */
1596 return pkt
->length
+ padding
+ maclen
;
1600 * Routines called from the main SSH code to send packets. There
1601 * are quite a few of these, because we have two separate
1602 * mechanisms for delaying the sending of packets:
1604 * - In order to send an IGNORE message and a password message in
1605 * a single fixed-length blob, we require the ability to
1606 * concatenate the encrypted forms of those two packets _into_ a
1607 * single blob and then pass it to our <network.h> transport
1608 * layer in one go. Hence, there's a deferment mechanism which
1609 * works after packet encryption.
1611 * - In order to avoid sending any connection-layer messages
1612 * during repeat key exchange, we have to queue up any such
1613 * outgoing messages _before_ they are encrypted (and in
1614 * particular before they're allocated sequence numbers), and
1615 * then send them once we've finished.
1617 * I call these mechanisms `defer' and `queue' respectively, so as
1618 * to distinguish them reasonably easily.
1620 * The functions send_noqueue() and defer_noqueue() free the packet
1621 * structure they are passed. Every outgoing packet goes through
1622 * precisely one of these functions in its life; packets passed to
1623 * ssh2_pkt_send() or ssh2_pkt_defer() either go straight to one of
1624 * these or get queued, and then when the queue is later emptied
1625 * the packets are all passed to defer_noqueue().
1629 * Send an SSH2 packet immediately, without queuing or deferring.
1631 static void ssh2_pkt_send_noqueue(Ssh ssh
, struct Packet
*pkt
)
1635 len
= ssh2_pkt_construct(ssh
, pkt
);
1636 backlog
= sk_write(ssh
->s
, (char *)pkt
->data
, len
);
1637 if (backlog
> SSH_MAX_BACKLOG
)
1638 ssh_throttle_all(ssh
, 1, backlog
);
1639 ssh_free_packet(pkt
);
1643 * Defer an SSH2 packet.
1645 static void ssh2_pkt_defer_noqueue(Ssh ssh
, struct Packet
*pkt
)
1647 int len
= ssh2_pkt_construct(ssh
, pkt
);
1648 if (ssh
->deferred_len
+ len
> ssh
->deferred_size
) {
1649 ssh
->deferred_size
= ssh
->deferred_len
+ len
+ 128;
1650 ssh
->deferred_send_data
= sresize(ssh
->deferred_send_data
,
1654 memcpy(ssh
->deferred_send_data
+ ssh
->deferred_len
, pkt
->data
, len
);
1655 ssh
->deferred_len
+= len
;
1656 ssh_free_packet(pkt
);
1660 * Queue an SSH2 packet.
1662 static void ssh2_pkt_queue(Ssh ssh
, struct Packet
*pkt
)
1664 assert(ssh
->queueing
);
1666 if (ssh
->queuelen
>= ssh
->queuesize
) {
1667 ssh
->queuesize
= ssh
->queuelen
+ 32;
1668 ssh
->queue
= sresize(ssh
->queue
, ssh
->queuesize
, struct Packet
*);
1671 ssh
->queue
[ssh
->queuelen
++] = pkt
;
1675 * Either queue or send a packet, depending on whether queueing is
1678 static void ssh2_pkt_send(Ssh ssh
, struct Packet
*pkt
)
1681 ssh2_pkt_queue(ssh
, pkt
);
1683 ssh2_pkt_send_noqueue(ssh
, pkt
);
1687 * Either queue or defer a packet, depending on whether queueing is
1690 static void ssh2_pkt_defer(Ssh ssh
, struct Packet
*pkt
)
1693 ssh2_pkt_queue(ssh
, pkt
);
1695 ssh2_pkt_defer_noqueue(ssh
, pkt
);
1699 * Send the whole deferred data block constructed by
1700 * ssh2_pkt_defer() or SSH1's defer_packet().
1702 * The expected use of the defer mechanism is that you call
1703 * ssh2_pkt_defer() a few times, then call ssh_pkt_defersend(). If
1704 * not currently queueing, this simply sets up deferred_send_data
1705 * and then sends it. If we _are_ currently queueing, the calls to
1706 * ssh2_pkt_defer() put the deferred packets on to the queue
1707 * instead, and therefore ssh_pkt_defersend() has no deferred data
1708 * to send. Hence, there's no need to make it conditional on
1711 static void ssh_pkt_defersend(Ssh ssh
)
1714 backlog
= sk_write(ssh
->s
, (char *)ssh
->deferred_send_data
,
1716 ssh
->deferred_len
= ssh
->deferred_size
= 0;
1717 sfree(ssh
->deferred_send_data
);
1718 ssh
->deferred_send_data
= NULL
;
1719 if (backlog
> SSH_MAX_BACKLOG
)
1720 ssh_throttle_all(ssh
, 1, backlog
);
1724 * Send all queued SSH2 packets. We send them by means of
1725 * ssh2_pkt_defer_noqueue(), in case they included a pair of
1726 * packets that needed to be lumped together.
1728 static void ssh2_pkt_queuesend(Ssh ssh
)
1732 assert(!ssh
->queueing
);
1734 for (i
= 0; i
< ssh
->queuelen
; i
++)
1735 ssh2_pkt_defer_noqueue(ssh
, ssh
->queue
[i
]);
1738 ssh_pkt_defersend(ssh
);
1742 void bndebug(char *string
, Bignum b
)
1746 p
= ssh2_mpint_fmt(b
, &len
);
1747 debug(("%s", string
));
1748 for (i
= 0; i
< len
; i
++)
1749 debug((" %02x", p
[i
]));
1755 static void sha_mpint(SHA_State
* s
, Bignum b
)
1759 p
= ssh2_mpint_fmt(b
, &len
);
1760 sha_string(s
, p
, len
);
1765 * Packet decode functions for both SSH1 and SSH2.
1767 static unsigned long ssh_pkt_getuint32(struct Packet
*pkt
)
1769 unsigned long value
;
1770 if (pkt
->length
- pkt
->savedpos
< 4)
1771 return 0; /* arrgh, no way to decline (FIXME?) */
1772 value
= GET_32BIT(pkt
->body
+ pkt
->savedpos
);
1776 static int ssh2_pkt_getbool(struct Packet
*pkt
)
1778 unsigned long value
;
1779 if (pkt
->length
- pkt
->savedpos
< 1)
1780 return 0; /* arrgh, no way to decline (FIXME?) */
1781 value
= pkt
->body
[pkt
->savedpos
] != 0;
1785 static void ssh_pkt_getstring(struct Packet
*pkt
, char **p
, int *length
)
1790 if (pkt
->length
- pkt
->savedpos
< 4)
1792 len
= GET_32BIT(pkt
->body
+ pkt
->savedpos
);
1797 if (pkt
->length
- pkt
->savedpos
< *length
)
1799 *p
= (char *)(pkt
->body
+ pkt
->savedpos
);
1800 pkt
->savedpos
+= *length
;
1802 static void *ssh_pkt_getdata(struct Packet
*pkt
, int length
)
1804 if (pkt
->length
- pkt
->savedpos
< length
)
1806 pkt
->savedpos
+= length
;
1807 return pkt
->body
+ (pkt
->savedpos
- length
);
1809 static int ssh1_pkt_getrsakey(struct Packet
*pkt
, struct RSAKey
*key
,
1810 unsigned char **keystr
)
1814 j
= makekey(pkt
->body
+ pkt
->savedpos
,
1815 pkt
->length
- pkt
->savedpos
,
1822 assert(pkt
->savedpos
< pkt
->length
);
1826 static Bignum
ssh1_pkt_getmp(struct Packet
*pkt
)
1831 j
= ssh1_read_bignum(pkt
->body
+ pkt
->savedpos
,
1832 pkt
->length
- pkt
->savedpos
, &b
);
1840 static Bignum
ssh2_pkt_getmp(struct Packet
*pkt
)
1846 ssh_pkt_getstring(pkt
, &p
, &length
);
1851 b
= bignum_from_bytes((unsigned char *)p
, length
);
1856 * Helper function to add an SSH2 signature blob to a packet.
1857 * Expects to be shown the public key blob as well as the signature
1858 * blob. Normally works just like ssh2_pkt_addstring, but will
1859 * fiddle with the signature packet if necessary for
1860 * BUG_SSH2_RSA_PADDING.
1862 static void ssh2_add_sigblob(Ssh ssh
, struct Packet
*pkt
,
1863 void *pkblob_v
, int pkblob_len
,
1864 void *sigblob_v
, int sigblob_len
)
1866 unsigned char *pkblob
= (unsigned char *)pkblob_v
;
1867 unsigned char *sigblob
= (unsigned char *)sigblob_v
;
1869 /* dmemdump(pkblob, pkblob_len); */
1870 /* dmemdump(sigblob, sigblob_len); */
1873 * See if this is in fact an ssh-rsa signature and a buggy
1874 * server; otherwise we can just do this the easy way.
1876 if ((ssh
->remote_bugs
& BUG_SSH2_RSA_PADDING
) &&
1877 (GET_32BIT(pkblob
) == 7 && !memcmp(pkblob
+4, "ssh-rsa", 7))) {
1878 int pos
, len
, siglen
;
1881 * Find the byte length of the modulus.
1884 pos
= 4+7; /* skip over "ssh-rsa" */
1885 pos
+= 4 + GET_32BIT(pkblob
+pos
); /* skip over exponent */
1886 len
= GET_32BIT(pkblob
+pos
); /* find length of modulus */
1887 pos
+= 4; /* find modulus itself */
1888 while (len
> 0 && pkblob
[pos
] == 0)
1890 /* debug(("modulus length is %d\n", len)); */
1893 * Now find the signature integer.
1895 pos
= 4+7; /* skip over "ssh-rsa" */
1896 siglen
= GET_32BIT(sigblob
+pos
);
1897 /* debug(("signature length is %d\n", siglen)); */
1899 if (len
!= siglen
) {
1900 unsigned char newlen
[4];
1901 ssh2_pkt_addstring_start(pkt
);
1902 ssh2_pkt_addstring_data(pkt
, (char *)sigblob
, pos
);
1903 /* dmemdump(sigblob, pos); */
1904 pos
+= 4; /* point to start of actual sig */
1905 PUT_32BIT(newlen
, len
);
1906 ssh2_pkt_addstring_data(pkt
, (char *)newlen
, 4);
1907 /* dmemdump(newlen, 4); */
1909 while (len
-- > siglen
) {
1910 ssh2_pkt_addstring_data(pkt
, (char *)newlen
, 1);
1911 /* dmemdump(newlen, 1); */
1913 ssh2_pkt_addstring_data(pkt
, (char *)(sigblob
+pos
), siglen
);
1914 /* dmemdump(sigblob+pos, siglen); */
1918 /* Otherwise fall through and do it the easy way. */
1921 ssh2_pkt_addstring_start(pkt
);
1922 ssh2_pkt_addstring_data(pkt
, (char *)sigblob
, sigblob_len
);
1926 * Examine the remote side's version string and compare it against
1927 * a list of known buggy implementations.
1929 static void ssh_detect_bugs(Ssh ssh
, char *vstring
)
1931 char *imp
; /* pointer to implementation part */
1933 imp
+= strcspn(imp
, "-");
1935 imp
+= strcspn(imp
, "-");
1938 ssh
->remote_bugs
= 0;
1940 if (ssh
->cfg
.sshbug_ignore1
== FORCE_ON
||
1941 (ssh
->cfg
.sshbug_ignore1
== AUTO
&&
1942 (!strcmp(imp
, "1.2.18") || !strcmp(imp
, "1.2.19") ||
1943 !strcmp(imp
, "1.2.20") || !strcmp(imp
, "1.2.21") ||
1944 !strcmp(imp
, "1.2.22") || !strcmp(imp
, "Cisco-1.25") ||
1945 !strcmp(imp
, "OSU_1.4alpha3") || !strcmp(imp
, "OSU_1.5alpha4")))) {
1947 * These versions don't support SSH1_MSG_IGNORE, so we have
1948 * to use a different defence against password length
1951 ssh
->remote_bugs
|= BUG_CHOKES_ON_SSH1_IGNORE
;
1952 logevent("We believe remote version has SSH1 ignore bug");
1955 if (ssh
->cfg
.sshbug_plainpw1
== FORCE_ON
||
1956 (ssh
->cfg
.sshbug_plainpw1
== AUTO
&&
1957 (!strcmp(imp
, "Cisco-1.25") || !strcmp(imp
, "OSU_1.4alpha3")))) {
1959 * These versions need a plain password sent; they can't
1960 * handle having a null and a random length of data after
1963 ssh
->remote_bugs
|= BUG_NEEDS_SSH1_PLAIN_PASSWORD
;
1964 logevent("We believe remote version needs a plain SSH1 password");
1967 if (ssh
->cfg
.sshbug_rsa1
== FORCE_ON
||
1968 (ssh
->cfg
.sshbug_rsa1
== AUTO
&&
1969 (!strcmp(imp
, "Cisco-1.25")))) {
1971 * These versions apparently have no clue whatever about
1972 * RSA authentication and will panic and die if they see
1973 * an AUTH_RSA message.
1975 ssh
->remote_bugs
|= BUG_CHOKES_ON_RSA
;
1976 logevent("We believe remote version can't handle RSA authentication");
1979 if (ssh
->cfg
.sshbug_hmac2
== FORCE_ON
||
1980 (ssh
->cfg
.sshbug_hmac2
== AUTO
&&
1981 !wc_match("* VShell", imp
) &&
1982 (wc_match("2.1.0*", imp
) || wc_match("2.0.*", imp
) ||
1983 wc_match("2.2.0*", imp
) || wc_match("2.3.0*", imp
) ||
1984 wc_match("2.1 *", imp
)))) {
1986 * These versions have the HMAC bug.
1988 ssh
->remote_bugs
|= BUG_SSH2_HMAC
;
1989 logevent("We believe remote version has SSH2 HMAC bug");
1992 if (ssh
->cfg
.sshbug_derivekey2
== FORCE_ON
||
1993 (ssh
->cfg
.sshbug_derivekey2
== AUTO
&&
1994 !wc_match("* VShell", imp
) &&
1995 (wc_match("2.0.0*", imp
) || wc_match("2.0.10*", imp
) ))) {
1997 * These versions have the key-derivation bug (failing to
1998 * include the literal shared secret in the hashes that
1999 * generate the keys).
2001 ssh
->remote_bugs
|= BUG_SSH2_DERIVEKEY
;
2002 logevent("We believe remote version has SSH2 key-derivation bug");
2005 if (ssh
->cfg
.sshbug_rsapad2
== FORCE_ON
||
2006 (ssh
->cfg
.sshbug_rsapad2
== AUTO
&&
2007 (wc_match("OpenSSH_2.[5-9]*", imp
) ||
2008 wc_match("OpenSSH_3.[0-2]*", imp
)))) {
2010 * These versions have the SSH2 RSA padding bug.
2012 ssh
->remote_bugs
|= BUG_SSH2_RSA_PADDING
;
2013 logevent("We believe remote version has SSH2 RSA padding bug");
2016 if (ssh
->cfg
.sshbug_pksessid2
== FORCE_ON
||
2017 (ssh
->cfg
.sshbug_pksessid2
== AUTO
&&
2018 wc_match("OpenSSH_2.[0-2]*", imp
))) {
2020 * These versions have the SSH2 session-ID bug in
2021 * public-key authentication.
2023 ssh
->remote_bugs
|= BUG_SSH2_PK_SESSIONID
;
2024 logevent("We believe remote version has SSH2 public-key-session-ID bug");
2027 if (ssh
->cfg
.sshbug_dhgex2
== FORCE_ON
) {
2029 * User specified the SSH2 DH GEX bug.
2031 ssh
->remote_bugs
|= BUG_SSH2_DH_GEX
;
2032 logevent("We believe remote version has SSH2 DH group exchange bug");
2036 static int do_ssh_init(Ssh ssh
, unsigned char c
)
2038 struct do_ssh_init_state
{
2046 crState(do_ssh_init_state
);
2048 crBegin(ssh
->do_ssh_init_crstate
);
2050 /* Search for the string "SSH-" in the input. */
2053 static const int transS
[] = { 1, 2, 2, 1 };
2054 static const int transH
[] = { 0, 0, 3, 0 };
2055 static const int transminus
[] = { 0, 0, 0, -1 };
2057 s
->i
= transS
[s
->i
];
2059 s
->i
= transH
[s
->i
];
2061 s
->i
= transminus
[s
->i
];
2066 crReturn(1); /* get another character */
2070 s
->vstring
= snewn(s
->vstrsize
, char);
2071 strcpy(s
->vstring
, "SSH-");
2075 crReturn(1); /* get another char */
2076 if (s
->vslen
>= s
->vstrsize
- 1) {
2078 s
->vstring
= sresize(s
->vstring
, s
->vstrsize
, char);
2080 s
->vstring
[s
->vslen
++] = c
;
2083 s
->version
[s
->i
] = '\0';
2085 } else if (s
->i
< sizeof(s
->version
) - 1)
2086 s
->version
[s
->i
++] = c
;
2087 } else if (c
== '\012')
2091 ssh
->agentfwd_enabled
= FALSE
;
2092 ssh
->rdpkt2_state
.incoming_sequence
= 0;
2094 s
->vstring
[s
->vslen
] = 0;
2095 s
->vstring
[strcspn(s
->vstring
, "\r\n")] = '\0';/* remove EOL chars */
2098 vlog
= snewn(20 + s
->vslen
, char);
2099 sprintf(vlog
, "Server version: %s", s
->vstring
);
2103 ssh_detect_bugs(ssh
, s
->vstring
);
2106 * Decide which SSH protocol version to support.
2109 /* Anything strictly below "2.0" means protocol 1 is supported. */
2110 s
->proto1
= ssh_versioncmp(s
->version
, "2.0") < 0;
2111 /* Anything greater or equal to "1.99" means protocol 2 is supported. */
2112 s
->proto2
= ssh_versioncmp(s
->version
, "1.99") >= 0;
2114 if (ssh
->cfg
.sshprot
== 0 && !s
->proto1
) {
2115 bombout(("SSH protocol version 1 required by user but not provided by server"));
2118 if (ssh
->cfg
.sshprot
== 3 && !s
->proto2
) {
2119 bombout(("SSH protocol version 2 required by user but not provided by server"));
2123 if (s
->proto2
&& (ssh
->cfg
.sshprot
>= 2 || !s
->proto1
)) {
2127 char verstring
[80], vlog
[100];
2128 sprintf(verstring
, "SSH-2.0-%s", sshver
);
2129 SHA_Init(&ssh
->exhashbase
);
2131 * Hash our version string and their version string.
2133 sha_string(&ssh
->exhashbase
, verstring
, strlen(verstring
));
2134 sha_string(&ssh
->exhashbase
, s
->vstring
, strcspn(s
->vstring
, "\r\n"));
2135 sprintf(vlog
, "We claim version: %s", verstring
);
2137 strcat(verstring
, "\012");
2138 logevent("Using SSH protocol version 2");
2139 sk_write(ssh
->s
, verstring
, strlen(verstring
));
2140 ssh
->protocol
= ssh2_protocol
;
2141 ssh2_protocol_setup(ssh
);
2143 ssh
->s_rdpkt
= ssh2_rdpkt
;
2148 char verstring
[80], vlog
[100];
2149 sprintf(verstring
, "SSH-%s-%s",
2150 (ssh_versioncmp(s
->version
, "1.5") <= 0 ? s
->version
: "1.5"),
2152 sprintf(vlog
, "We claim version: %s", verstring
);
2154 strcat(verstring
, "\012");
2156 logevent("Using SSH protocol version 1");
2157 sk_write(ssh
->s
, verstring
, strlen(verstring
));
2158 ssh
->protocol
= ssh1_protocol
;
2159 ssh1_protocol_setup(ssh
);
2161 ssh
->s_rdpkt
= ssh1_rdpkt
;
2163 update_specials_menu(ssh
->frontend
);
2164 ssh
->state
= SSH_STATE_BEFORE_SIZE
;
2165 ssh
->pinger
= pinger_new(&ssh
->cfg
, &ssh_backend
, ssh
);
2172 static void ssh_gotdata(Ssh ssh
, unsigned char *data
, int datalen
)
2174 crBegin(ssh
->ssh_gotdata_crstate
);
2177 * To begin with, feed the characters one by one to the
2178 * protocol initialisation / selection function do_ssh_init().
2179 * When that returns 0, we're done with the initial greeting
2180 * exchange and can move on to packet discipline.
2183 int ret
; /* need not be kept across crReturn */
2185 crReturnV
; /* more data please */
2186 ret
= do_ssh_init(ssh
, *data
);
2194 * We emerge from that loop when the initial negotiation is
2195 * over and we have selected an s_rdpkt function. Now pass
2196 * everything to s_rdpkt, and then pass the resulting packets
2197 * to the proper protocol handler.
2202 while (datalen
> 0) {
2203 struct Packet
*pktin
= ssh
->s_rdpkt(ssh
, &data
, &datalen
);
2205 ssh
->protocol(ssh
, NULL
, 0, pktin
);
2206 ssh_free_packet(pktin
);
2208 if (ssh
->state
== SSH_STATE_CLOSED
)
2216 static void ssh_do_close(Ssh ssh
)
2219 struct ssh_channel
*c
;
2221 ssh
->state
= SSH_STATE_CLOSED
;
2225 notify_remote_exit(ssh
->frontend
);
2228 * Now we must shut down any port and X forwardings going
2229 * through this connection.
2231 if (ssh
->channels
) {
2232 for (i
= 0; NULL
!= (c
= index234(ssh
->channels
, i
)); i
++) {
2235 x11_close(c
->u
.x11
.s
);
2238 pfd_close(c
->u
.pfd
.s
);
2241 del234(ssh
->channels
, c
);
2242 if (ssh
->version
== 2)
2243 bufchain_clear(&c
->v
.v2
.outbuffer
);
2249 static int ssh_closing(Plug plug
, const char *error_msg
, int error_code
,
2252 Ssh ssh
= (Ssh
) plug
;
2255 /* A socket error has occurred. */
2256 logevent(error_msg
);
2257 connection_fatal(ssh
->frontend
, "%s", error_msg
);
2259 /* Otherwise, the remote side closed the connection normally. */
2264 static int ssh_receive(Plug plug
, int urgent
, char *data
, int len
)
2266 Ssh ssh
= (Ssh
) plug
;
2267 ssh_gotdata(ssh
, (unsigned char *)data
, len
);
2268 if (ssh
->state
== SSH_STATE_CLOSED
) {
2275 static void ssh_sent(Plug plug
, int bufsize
)
2277 Ssh ssh
= (Ssh
) plug
;
2279 * If the send backlog on the SSH socket itself clears, we
2280 * should unthrottle the whole world if it was throttled.
2282 if (bufsize
< SSH_MAX_BACKLOG
)
2283 ssh_throttle_all(ssh
, 0, bufsize
);
2287 * Connect to specified host and port.
2288 * Returns an error message, or NULL on success.
2289 * Also places the canonical host name into `realhost'. It must be
2290 * freed by the caller.
2292 static const char *connect_to_host(Ssh ssh
, char *host
, int port
,
2293 char **realhost
, int nodelay
, int keepalive
)
2295 static const struct plug_function_table fn_table
= {
2305 ssh
->savedhost
= snewn(1 + strlen(host
), char);
2306 if (!ssh
->savedhost
)
2307 fatalbox("Out of memory");
2308 strcpy(ssh
->savedhost
, host
);
2311 port
= 22; /* default ssh port */
2312 ssh
->savedport
= port
;
2317 logeventf(ssh
, "Looking up host \"%s\"", host
);
2318 addr
= name_lookup(host
, port
, realhost
, &ssh
->cfg
);
2319 if ((err
= sk_addr_error(addr
)) != NULL
) {
2329 sk_getaddr(addr
, addrbuf
, 100);
2330 logeventf(ssh
, "Connecting to %s port %d", addrbuf
, port
);
2332 ssh
->fn
= &fn_table
;
2333 ssh
->s
= new_connection(addr
, *realhost
, port
,
2334 0, 1, nodelay
, keepalive
, (Plug
) ssh
, &ssh
->cfg
);
2335 if ((err
= sk_socket_error(ssh
->s
)) != NULL
) {
2337 notify_remote_exit(ssh
->frontend
);
2345 * Throttle or unthrottle the SSH connection.
2347 static void ssh1_throttle(Ssh ssh
, int adjust
)
2349 int old_count
= ssh
->v1_throttle_count
;
2350 ssh
->v1_throttle_count
+= adjust
;
2351 assert(ssh
->v1_throttle_count
>= 0);
2352 if (ssh
->v1_throttle_count
&& !old_count
) {
2353 sk_set_frozen(ssh
->s
, 1);
2354 } else if (!ssh
->v1_throttle_count
&& old_count
) {
2355 sk_set_frozen(ssh
->s
, 0);
2360 * Throttle or unthrottle _all_ local data streams (for when sends
2361 * on the SSH connection itself back up).
2363 static void ssh_throttle_all(Ssh ssh
, int enable
, int bufsize
)
2366 struct ssh_channel
*c
;
2368 if (enable
== ssh
->throttled_all
)
2370 ssh
->throttled_all
= enable
;
2371 ssh
->overall_bufsize
= bufsize
;
2374 for (i
= 0; NULL
!= (c
= index234(ssh
->channels
, i
)); i
++) {
2376 case CHAN_MAINSESSION
:
2378 * This is treated separately, outside the switch.
2382 x11_override_throttle(c
->u
.x11
.s
, enable
);
2385 /* Agent channels require no buffer management. */
2388 pfd_override_throttle(c
->u
.pfd
.s
, enable
);
2395 * Username and password input, abstracted off into routines
2396 * reusable in several places - even between SSH1 and SSH2.
2399 /* Set up a username or password input loop on a given buffer. */
2400 static void setup_userpass_input(Ssh ssh
, char *buffer
, int buflen
, int echo
)
2402 ssh
->userpass_input_buffer
= buffer
;
2403 ssh
->userpass_input_buflen
= buflen
;
2404 ssh
->userpass_input_bufpos
= 0;
2405 ssh
->userpass_input_echo
= echo
;
2409 * Process some terminal data in the course of username/password
2410 * input. Returns >0 for success (line of input returned in
2411 * buffer), <0 for failure (user hit ^C/^D, bomb out and exit), 0
2412 * for inconclusive (keep waiting for more input please).
2414 static int process_userpass_input(Ssh ssh
, unsigned char *in
, int inlen
)
2419 switch (c
= *in
++) {
2422 ssh
->userpass_input_buffer
[ssh
->userpass_input_bufpos
] = 0;
2423 ssh
->userpass_input_buffer
[ssh
->userpass_input_buflen
-1] = 0;
2428 if (ssh
->userpass_input_bufpos
> 0) {
2429 if (ssh
->userpass_input_echo
)
2430 c_write_str(ssh
, "\b \b");
2431 ssh
->userpass_input_bufpos
--;
2436 while (ssh
->userpass_input_bufpos
> 0) {
2437 if (ssh
->userpass_input_echo
)
2438 c_write_str(ssh
, "\b \b");
2439 ssh
->userpass_input_bufpos
--;
2448 * This simplistic check for printability is disabled
2449 * when we're doing password input, because some people
2450 * have control characters in their passwords.o
2452 if ((!ssh
->userpass_input_echo
||
2453 (c
>= ' ' && c
<= '~') ||
2454 ((unsigned char) c
>= 160))
2455 && ssh
->userpass_input_bufpos
< ssh
->userpass_input_buflen
-1) {
2456 ssh
->userpass_input_buffer
[ssh
->userpass_input_bufpos
++] = c
;
2457 if (ssh
->userpass_input_echo
)
2458 c_write(ssh
, &c
, 1);
2466 static void ssh_agent_callback(void *sshv
, void *reply
, int replylen
)
2468 Ssh ssh
= (Ssh
) sshv
;
2470 ssh
->agent_response
= reply
;
2471 ssh
->agent_response_len
= replylen
;
2473 if (ssh
->version
== 1)
2474 do_ssh1_login(ssh
, NULL
, -1, NULL
);
2476 do_ssh2_authconn(ssh
, NULL
, -1, NULL
);
2479 static void ssh_agentf_callback(void *cv
, void *reply
, int replylen
)
2481 struct ssh_channel
*c
= (struct ssh_channel
*)cv
;
2483 void *sentreply
= reply
;
2486 /* Fake SSH_AGENT_FAILURE. */
2487 sentreply
= "\0\0\0\1\5";
2490 if (ssh
->version
== 2) {
2491 ssh2_add_channel_data(c
, sentreply
, replylen
);
2494 send_packet(ssh
, SSH1_MSG_CHANNEL_DATA
,
2495 PKT_INT
, c
->remoteid
,
2498 PKT_DATA
, sentreply
, replylen
,
2507 * Handle the key exchange and user authentication phases.
2509 static int do_ssh1_login(Ssh ssh
, unsigned char *in
, int inlen
,
2510 struct Packet
*pktin
)
2513 unsigned char cookie
[8], *ptr
;
2514 struct RSAKey servkey
, hostkey
;
2515 struct MD5Context md5c
;
2516 struct do_ssh1_login_state
{
2518 unsigned char *rsabuf
, *keystr1
, *keystr2
;
2519 unsigned long supported_ciphers_mask
, supported_auths_mask
;
2520 int tried_publickey
, tried_agent
;
2521 int tis_auth_refused
, ccard_auth_refused
;
2522 unsigned char session_id
[16];
2525 void *publickey_blob
;
2526 int publickey_bloblen
;
2532 unsigned char request
[5], *response
, *p
;
2541 crState(do_ssh1_login_state
);
2543 crBegin(ssh
->do_ssh1_login_crstate
);
2550 if (pktin
->type
!= SSH1_SMSG_PUBLIC_KEY
) {
2551 bombout(("Public key packet not received"));
2555 logevent("Received public keys");
2557 ptr
= ssh_pkt_getdata(pktin
, 8);
2559 bombout(("SSH1 public key packet stopped before random cookie"));
2562 memcpy(cookie
, ptr
, 8);
2564 if (!ssh1_pkt_getrsakey(pktin
, &servkey
, &s
->keystr1
) ||
2565 !ssh1_pkt_getrsakey(pktin
, &hostkey
, &s
->keystr2
)) {
2566 bombout(("Failed to read SSH1 public keys from public key packet"));
2571 * Log the host key fingerprint.
2575 logevent("Host key fingerprint is:");
2576 strcpy(logmsg
, " ");
2577 hostkey
.comment
= NULL
;
2578 rsa_fingerprint(logmsg
+ strlen(logmsg
),
2579 sizeof(logmsg
) - strlen(logmsg
), &hostkey
);
2583 ssh
->v1_remote_protoflags
= ssh_pkt_getuint32(pktin
);
2584 s
->supported_ciphers_mask
= ssh_pkt_getuint32(pktin
);
2585 s
->supported_auths_mask
= ssh_pkt_getuint32(pktin
);
2587 ssh
->v1_local_protoflags
=
2588 ssh
->v1_remote_protoflags
& SSH1_PROTOFLAGS_SUPPORTED
;
2589 ssh
->v1_local_protoflags
|= SSH1_PROTOFLAG_SCREEN_NUMBER
;
2592 MD5Update(&md5c
, s
->keystr2
, hostkey
.bytes
);
2593 MD5Update(&md5c
, s
->keystr1
, servkey
.bytes
);
2594 MD5Update(&md5c
, cookie
, 8);
2595 MD5Final(s
->session_id
, &md5c
);
2597 for (i
= 0; i
< 32; i
++)
2598 ssh
->session_key
[i
] = random_byte();
2601 * Verify that the `bits' and `bytes' parameters match.
2603 if (hostkey
.bits
> hostkey
.bytes
* 8 ||
2604 servkey
.bits
> servkey
.bytes
* 8) {
2605 bombout(("SSH1 public keys were badly formatted"));
2609 s
->len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
2611 s
->rsabuf
= snewn(s
->len
, unsigned char);
2613 fatalbox("Out of memory");
2616 * Verify the host key.
2620 * First format the key into a string.
2622 int len
= rsastr_len(&hostkey
);
2623 char fingerprint
[100];
2624 char *keystr
= snewn(len
, char);
2626 fatalbox("Out of memory");
2627 rsastr_fmt(keystr
, &hostkey
);
2628 rsa_fingerprint(fingerprint
, sizeof(fingerprint
), &hostkey
);
2629 verify_ssh_host_key(ssh
->frontend
,
2630 ssh
->savedhost
, ssh
->savedport
, "rsa", keystr
,
2635 for (i
= 0; i
< 32; i
++) {
2636 s
->rsabuf
[i
] = ssh
->session_key
[i
];
2638 s
->rsabuf
[i
] ^= s
->session_id
[i
];
2641 if (hostkey
.bytes
> servkey
.bytes
) {
2642 ret
= rsaencrypt(s
->rsabuf
, 32, &servkey
);
2644 ret
= rsaencrypt(s
->rsabuf
, servkey
.bytes
, &hostkey
);
2646 ret
= rsaencrypt(s
->rsabuf
, 32, &hostkey
);
2648 ret
= rsaencrypt(s
->rsabuf
, hostkey
.bytes
, &servkey
);
2651 bombout(("SSH1 public key encryptions failed due to bad formatting"));
2655 logevent("Encrypted session key");
2658 int cipher_chosen
= 0, warn
= 0;
2659 char *cipher_string
= NULL
;
2661 for (i
= 0; !cipher_chosen
&& i
< CIPHER_MAX
; i
++) {
2662 int next_cipher
= ssh
->cfg
.ssh_cipherlist
[i
];
2663 if (next_cipher
== CIPHER_WARN
) {
2664 /* If/when we choose a cipher, warn about it */
2666 } else if (next_cipher
== CIPHER_AES
) {
2667 /* XXX Probably don't need to mention this. */
2668 logevent("AES not supported in SSH1, skipping");
2670 switch (next_cipher
) {
2671 case CIPHER_3DES
: s
->cipher_type
= SSH_CIPHER_3DES
;
2672 cipher_string
= "3DES"; break;
2673 case CIPHER_BLOWFISH
: s
->cipher_type
= SSH_CIPHER_BLOWFISH
;
2674 cipher_string
= "Blowfish"; break;
2675 case CIPHER_DES
: s
->cipher_type
= SSH_CIPHER_DES
;
2676 cipher_string
= "single-DES"; break;
2678 if (s
->supported_ciphers_mask
& (1 << s
->cipher_type
))
2682 if (!cipher_chosen
) {
2683 if ((s
->supported_ciphers_mask
& (1 << SSH_CIPHER_3DES
)) == 0)
2684 bombout(("Server violates SSH 1 protocol by not "
2685 "supporting 3DES encryption"));
2687 /* shouldn't happen */
2688 bombout(("No supported ciphers found"));
2692 /* Warn about chosen cipher if necessary. */
2694 askcipher(ssh
->frontend
, cipher_string
, 0);
2697 switch (s
->cipher_type
) {
2698 case SSH_CIPHER_3DES
:
2699 logevent("Using 3DES encryption");
2701 case SSH_CIPHER_DES
:
2702 logevent("Using single-DES encryption");
2704 case SSH_CIPHER_BLOWFISH
:
2705 logevent("Using Blowfish encryption");
2709 send_packet(ssh
, SSH1_CMSG_SESSION_KEY
,
2710 PKT_CHAR
, s
->cipher_type
,
2711 PKT_DATA
, cookie
, 8,
2712 PKT_CHAR
, (s
->len
* 8) >> 8, PKT_CHAR
, (s
->len
* 8) & 0xFF,
2713 PKT_DATA
, s
->rsabuf
, s
->len
,
2714 PKT_INT
, ssh
->v1_local_protoflags
, PKT_END
);
2716 logevent("Trying to enable encryption...");
2720 ssh
->cipher
= (s
->cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish_ssh1
:
2721 s
->cipher_type
== SSH_CIPHER_DES ?
&ssh_des
:
2723 ssh
->v1_cipher_ctx
= ssh
->cipher
->make_context();
2724 ssh
->cipher
->sesskey(ssh
->v1_cipher_ctx
, ssh
->session_key
);
2725 logeventf(ssh
, "Initialised %s encryption", ssh
->cipher
->text_name
);
2727 ssh
->crcda_ctx
= crcda_make_context();
2728 logevent("Installing CRC compensation attack detector");
2730 if (servkey
.modulus
) {
2731 sfree(servkey
.modulus
);
2732 servkey
.modulus
= NULL
;
2734 if (servkey
.exponent
) {
2735 sfree(servkey
.exponent
);
2736 servkey
.exponent
= NULL
;
2738 if (hostkey
.modulus
) {
2739 sfree(hostkey
.modulus
);
2740 hostkey
.modulus
= NULL
;
2742 if (hostkey
.exponent
) {
2743 sfree(hostkey
.exponent
);
2744 hostkey
.exponent
= NULL
;
2748 if (pktin
->type
!= SSH1_SMSG_SUCCESS
) {
2749 bombout(("Encryption not successfully enabled"));
2753 logevent("Successfully started encryption");
2757 if (!*ssh
->cfg
.username
) {
2758 if (ssh_get_line
&& !ssh_getline_pw_only
) {
2759 if (!ssh_get_line("login as: ",
2760 s
->username
, sizeof(s
->username
), FALSE
)) {
2762 * get_line failed to get a username.
2765 logevent("No username provided. Abandoning session.");
2766 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
2770 int ret
; /* need not be kept over crReturn */
2771 c_write_str(ssh
, "login as: ");
2774 setup_userpass_input(ssh
, s
->username
, sizeof(s
->username
), 1);
2776 crWaitUntil(!pktin
);
2777 ret
= process_userpass_input(ssh
, in
, inlen
);
2781 c_write_str(ssh
, "\r\n");
2784 strncpy(s
->username
, ssh
->cfg
.username
, sizeof(s
->username
));
2785 s
->username
[sizeof(s
->username
)-1] = '\0';
2788 send_packet(ssh
, SSH1_CMSG_USER
, PKT_STR
, s
->username
, PKT_END
);
2790 char userlog
[22 + sizeof(s
->username
)];
2791 sprintf(userlog
, "Sent username \"%s\"", s
->username
);
2793 if (flags
& FLAG_INTERACTIVE
&&
2794 (!((flags
& FLAG_STDERR
) && (flags
& FLAG_VERBOSE
)))) {
2795 strcat(userlog
, "\r\n");
2796 c_write_str(ssh
, userlog
);
2803 if ((ssh
->remote_bugs
& BUG_CHOKES_ON_RSA
)) {
2804 /* We must not attempt PK auth. Pretend we've already tried it. */
2805 s
->tried_publickey
= s
->tried_agent
= 1;
2807 s
->tried_publickey
= s
->tried_agent
= 0;
2809 s
->tis_auth_refused
= s
->ccard_auth_refused
= 0;
2810 /* Load the public half of ssh->cfg.keyfile so we notice if it's in Pageant */
2811 if (!filename_is_null(ssh
->cfg
.keyfile
)) {
2812 if (!rsakey_pubblob(&ssh
->cfg
.keyfile
,
2813 &s
->publickey_blob
, &s
->publickey_bloblen
, NULL
))
2814 s
->publickey_blob
= NULL
;
2816 s
->publickey_blob
= NULL
;
2818 while (pktin
->type
== SSH1_SMSG_FAILURE
) {
2819 s
->pwpkt_type
= SSH1_CMSG_AUTH_PASSWORD
;
2821 if (agent_exists() && !s
->tried_agent
) {
2823 * Attempt RSA authentication using Pageant.
2829 logevent("Pageant is running. Requesting keys.");
2831 /* Request the keys held by the agent. */
2832 PUT_32BIT(s
->request
, 1);
2833 s
->request
[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES
;
2834 if (!agent_query(s
->request
, 5, &r
, &s
->responselen
,
2835 ssh_agent_callback
, ssh
)) {
2839 bombout(("Unexpected data from server while waiting"
2840 " for agent response"));
2843 } while (pktin
|| inlen
> 0);
2844 r
= ssh
->agent_response
;
2845 s
->responselen
= ssh
->agent_response_len
;
2847 s
->response
= (unsigned char *) r
;
2848 if (s
->response
&& s
->responselen
>= 5 &&
2849 s
->response
[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER
) {
2850 s
->p
= s
->response
+ 5;
2851 s
->nkeys
= GET_32BIT(s
->p
);
2855 sprintf(buf
, "Pageant has %d SSH1 keys", s
->nkeys
);
2858 for (s
->keyi
= 0; s
->keyi
< s
->nkeys
; s
->keyi
++) {
2861 sprintf(buf
, "Trying Pageant key #%d", s
->keyi
);
2864 if (s
->publickey_blob
&&
2865 !memcmp(s
->p
, s
->publickey_blob
,
2866 s
->publickey_bloblen
)) {
2867 logevent("This key matches configured key file");
2868 s
->tried_publickey
= 1;
2873 do { /* do while (0) to make breaking easy */
2874 n
= ssh1_read_bignum
2875 (s
->p
, s
->responselen
-(s
->p
-s
->response
),
2880 n
= ssh1_read_bignum
2881 (s
->p
, s
->responselen
-(s
->p
-s
->response
),
2886 if (s
->responselen
- (s
->p
-s
->response
) < 4)
2888 s
->commentlen
= GET_32BIT(s
->p
);
2890 if (s
->responselen
- (s
->p
-s
->response
) <
2893 s
->commentp
= (char *)s
->p
;
2894 s
->p
+= s
->commentlen
;
2898 logevent("Pageant key list packet was truncated");
2902 send_packet(ssh
, SSH1_CMSG_AUTH_RSA
,
2903 PKT_BIGNUM
, s
->key
.modulus
, PKT_END
);
2905 if (pktin
->type
!= SSH1_SMSG_AUTH_RSA_CHALLENGE
) {
2906 logevent("Key refused");
2909 logevent("Received RSA challenge");
2910 if ((s
->challenge
= ssh1_pkt_getmp(pktin
)) == NULL
) {
2911 bombout(("Server's RSA challenge was badly formatted"));
2916 char *agentreq
, *q
, *ret
;
2919 len
= 1 + 4; /* message type, bit count */
2920 len
+= ssh1_bignum_length(s
->key
.exponent
);
2921 len
+= ssh1_bignum_length(s
->key
.modulus
);
2922 len
+= ssh1_bignum_length(s
->challenge
);
2923 len
+= 16; /* session id */
2924 len
+= 4; /* response format */
2925 agentreq
= snewn(4 + len
, char);
2926 PUT_32BIT(agentreq
, len
);
2928 *q
++ = SSH1_AGENTC_RSA_CHALLENGE
;
2929 PUT_32BIT(q
, bignum_bitcount(s
->key
.modulus
));
2931 q
+= ssh1_write_bignum(q
, s
->key
.exponent
);
2932 q
+= ssh1_write_bignum(q
, s
->key
.modulus
);
2933 q
+= ssh1_write_bignum(q
, s
->challenge
);
2934 memcpy(q
, s
->session_id
, 16);
2936 PUT_32BIT(q
, 1); /* response format */
2937 if (!agent_query(agentreq
, len
+ 4, &vret
, &retlen
,
2938 ssh_agent_callback
, ssh
)) {
2943 bombout(("Unexpected data from server"
2944 " while waiting for agent"
2948 } while (pktin
|| inlen
> 0);
2949 vret
= ssh
->agent_response
;
2950 retlen
= ssh
->agent_response_len
;
2955 if (ret
[4] == SSH1_AGENT_RSA_RESPONSE
) {
2956 logevent("Sending Pageant's response");
2957 send_packet(ssh
, SSH1_CMSG_AUTH_RSA_RESPONSE
,
2958 PKT_DATA
, ret
+ 5, 16,
2962 if (pktin
->type
== SSH1_SMSG_SUCCESS
) {
2964 ("Pageant's response accepted");
2965 if (flags
& FLAG_VERBOSE
) {
2966 c_write_str(ssh
, "Authenticated using"
2968 c_write(ssh
, s
->commentp
,
2970 c_write_str(ssh
, "\" from agent\r\n");
2975 ("Pageant's response not accepted");
2978 ("Pageant failed to answer challenge");
2982 logevent("No reply received from Pageant");
2985 freebn(s
->key
.exponent
);
2986 freebn(s
->key
.modulus
);
2987 freebn(s
->challenge
);
2996 if (!filename_is_null(ssh
->cfg
.keyfile
) && !s
->tried_publickey
)
2997 s
->pwpkt_type
= SSH1_CMSG_AUTH_RSA
;
2999 if (ssh
->cfg
.try_tis_auth
&&
3000 (s
->supported_auths_mask
& (1 << SSH1_AUTH_TIS
)) &&
3001 !s
->tis_auth_refused
) {
3002 s
->pwpkt_type
= SSH1_CMSG_AUTH_TIS_RESPONSE
;
3003 logevent("Requested TIS authentication");
3004 send_packet(ssh
, SSH1_CMSG_AUTH_TIS
, PKT_END
);
3006 if (pktin
->type
!= SSH1_SMSG_AUTH_TIS_CHALLENGE
) {
3007 logevent("TIS authentication declined");
3008 if (flags
& FLAG_INTERACTIVE
)
3009 c_write_str(ssh
, "TIS authentication refused.\r\n");
3010 s
->tis_auth_refused
= 1;
3016 ssh_pkt_getstring(pktin
, &challenge
, &challengelen
);
3018 bombout(("TIS challenge packet was badly formed"));
3021 logevent("Received TIS challenge");
3022 if (challengelen
> sizeof(s
->prompt
) - 1)
3023 challengelen
= sizeof(s
->prompt
) - 1;/* prevent overrun */
3024 memcpy(s
->prompt
, challenge
, challengelen
);
3025 /* Prompt heuristic comes from OpenSSH */
3026 strncpy(s
->prompt
+ challengelen
,
3027 memchr(s
->prompt
, '\n', challengelen
) ?
3028 "": "\r\nResponse: ",
3029 (sizeof s
->prompt
) - challengelen
);
3030 s
->prompt
[(sizeof s
->prompt
) - 1] = '\0';
3033 if (ssh
->cfg
.try_tis_auth
&&
3034 (s
->supported_auths_mask
& (1 << SSH1_AUTH_CCARD
)) &&
3035 !s
->ccard_auth_refused
) {
3036 s
->pwpkt_type
= SSH1_CMSG_AUTH_CCARD_RESPONSE
;
3037 logevent("Requested CryptoCard authentication");
3038 send_packet(ssh
, SSH1_CMSG_AUTH_CCARD
, PKT_END
);
3040 if (pktin
->type
!= SSH1_SMSG_AUTH_CCARD_CHALLENGE
) {
3041 logevent("CryptoCard authentication declined");
3042 c_write_str(ssh
, "CryptoCard authentication refused.\r\n");
3043 s
->ccard_auth_refused
= 1;
3049 ssh_pkt_getstring(pktin
, &challenge
, &challengelen
);
3051 bombout(("CryptoCard challenge packet was badly formed"));
3054 logevent("Received CryptoCard challenge");
3055 if (challengelen
> sizeof(s
->prompt
) - 1)
3056 challengelen
= sizeof(s
->prompt
) - 1;/* prevent overrun */
3057 memcpy(s
->prompt
, challenge
, challengelen
);
3058 strncpy(s
->prompt
+ challengelen
,
3059 memchr(s
->prompt
, '\n', challengelen
) ?
3060 "" : "\r\nResponse: ",
3061 sizeof(s
->prompt
) - challengelen
);
3062 s
->prompt
[sizeof(s
->prompt
) - 1] = '\0';
3065 if (s
->pwpkt_type
== SSH1_CMSG_AUTH_PASSWORD
) {
3066 sprintf(s
->prompt
, "%.90s@%.90s's password: ",
3067 s
->username
, ssh
->savedhost
);
3069 if (s
->pwpkt_type
== SSH1_CMSG_AUTH_RSA
) {
3070 char *comment
= NULL
;
3073 if (flags
& FLAG_VERBOSE
)
3074 c_write_str(ssh
, "Trying public key authentication.\r\n");
3075 logeventf(ssh
, "Trying public key \"%s\"",
3076 filename_to_str(&ssh
->cfg
.keyfile
));
3077 type
= key_type(&ssh
->cfg
.keyfile
);
3078 if (type
!= SSH_KEYTYPE_SSH1
) {
3079 sprintf(msgbuf
, "Key is of wrong type (%s)",
3080 key_type_to_str(type
));
3082 c_write_str(ssh
, msgbuf
);
3083 c_write_str(ssh
, "\r\n");
3084 s
->tried_publickey
= 1;
3087 if (!rsakey_encrypted(&ssh
->cfg
.keyfile
, &comment
)) {
3088 if (flags
& FLAG_VERBOSE
)
3089 c_write_str(ssh
, "No passphrase required.\r\n");
3092 sprintf(s
->prompt
, "Passphrase for key \"%.100s\": ", comment
);
3097 * Show password prompt, having first obtained it via a TIS
3098 * or CryptoCard exchange if we're doing TIS or CryptoCard
3102 if (!ssh_get_line(s
->prompt
, s
->password
,
3103 sizeof(s
->password
), TRUE
)) {
3105 * get_line failed to get a password (for example
3106 * because one was supplied on the command line
3107 * which has already failed to work). Terminate.
3109 send_packet(ssh
, SSH1_MSG_DISCONNECT
,
3110 PKT_STR
, "No more passwords available to try",
3112 logevent("Unable to authenticate");
3113 connection_fatal(ssh
->frontend
, "Unable to authenticate");
3114 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
3118 /* Prompt may have come from server. We've munged it a bit, so
3119 * we know it to be zero-terminated at least once. */
3120 int ret
; /* need not be saved over crReturn */
3121 c_write_untrusted(ssh
, s
->prompt
, strlen(s
->prompt
));
3124 setup_userpass_input(ssh
, s
->password
, sizeof(s
->password
), 0);
3126 crWaitUntil(!pktin
);
3127 ret
= process_userpass_input(ssh
, in
, inlen
);
3131 c_write_str(ssh
, "\r\n");
3135 if (s
->pwpkt_type
== SSH1_CMSG_AUTH_RSA
) {
3137 * Try public key authentication with the specified
3140 s
->tried_publickey
= 1;
3143 const char *error
= NULL
;
3144 int ret
= loadrsakey(&ssh
->cfg
.keyfile
, &s
->key
, s
->password
,
3147 c_write_str(ssh
, "Couldn't load private key from ");
3148 c_write_str(ssh
, filename_to_str(&ssh
->cfg
.keyfile
));
3149 c_write_str(ssh
, " (");
3150 c_write_str(ssh
, error
);
3151 c_write_str(ssh
, ").\r\n");
3152 continue; /* go and try password */
3155 c_write_str(ssh
, "Wrong passphrase.\r\n");
3156 s
->tried_publickey
= 0;
3157 continue; /* try again */
3162 * Send a public key attempt.
3164 send_packet(ssh
, SSH1_CMSG_AUTH_RSA
,
3165 PKT_BIGNUM
, s
->key
.modulus
, PKT_END
);
3168 if (pktin
->type
== SSH1_SMSG_FAILURE
) {
3169 c_write_str(ssh
, "Server refused our public key.\r\n");
3170 continue; /* go and try password */
3172 if (pktin
->type
!= SSH1_SMSG_AUTH_RSA_CHALLENGE
) {
3173 bombout(("Bizarre response to offer of public key"));
3179 unsigned char buffer
[32];
3180 Bignum challenge
, response
;
3182 if ((challenge
= ssh1_pkt_getmp(pktin
)) == NULL
) {
3183 bombout(("Server's RSA challenge was badly formatted"));
3186 response
= rsadecrypt(challenge
, &s
->key
);
3187 freebn(s
->key
.private_exponent
);/* burn the evidence */
3189 for (i
= 0; i
< 32; i
++) {
3190 buffer
[i
] = bignum_byte(response
, 31 - i
);
3194 MD5Update(&md5c
, buffer
, 32);
3195 MD5Update(&md5c
, s
->session_id
, 16);
3196 MD5Final(buffer
, &md5c
);
3198 send_packet(ssh
, SSH1_CMSG_AUTH_RSA_RESPONSE
,
3199 PKT_DATA
, buffer
, 16, PKT_END
);
3206 if (pktin
->type
== SSH1_SMSG_FAILURE
) {
3207 if (flags
& FLAG_VERBOSE
)
3208 c_write_str(ssh
, "Failed to authenticate with"
3209 " our public key.\r\n");
3210 continue; /* go and try password */
3211 } else if (pktin
->type
!= SSH1_SMSG_SUCCESS
) {
3212 bombout(("Bizarre response to RSA authentication response"));
3216 break; /* we're through! */
3218 if (s
->pwpkt_type
== SSH1_CMSG_AUTH_PASSWORD
) {
3220 * Defence against traffic analysis: we send a
3221 * whole bunch of packets containing strings of
3222 * different lengths. One of these strings is the
3223 * password, in a SSH1_CMSG_AUTH_PASSWORD packet.
3224 * The others are all random data in
3225 * SSH1_MSG_IGNORE packets. This way a passive
3226 * listener can't tell which is the password, and
3227 * hence can't deduce the password length.
3229 * Anybody with a password length greater than 16
3230 * bytes is going to have enough entropy in their
3231 * password that a listener won't find it _that_
3232 * much help to know how long it is. So what we'll
3235 * - if password length < 16, we send 15 packets
3236 * containing string lengths 1 through 15
3238 * - otherwise, we let N be the nearest multiple
3239 * of 8 below the password length, and send 8
3240 * packets containing string lengths N through
3241 * N+7. This won't obscure the order of
3242 * magnitude of the password length, but it will
3243 * introduce a bit of extra uncertainty.
3245 * A few servers (the old 1.2.18 through 1.2.22)
3246 * can't deal with SSH1_MSG_IGNORE. For these
3247 * servers, we need an alternative defence. We make
3248 * use of the fact that the password is interpreted
3249 * as a C string: so we can append a NUL, then some
3252 * One server (a Cisco one) can deal with neither
3253 * SSH1_MSG_IGNORE _nor_ a padded password string.
3254 * For this server we are left with no defences
3255 * against password length sniffing.
3257 if (!(ssh
->remote_bugs
& BUG_CHOKES_ON_SSH1_IGNORE
)) {
3259 * The server can deal with SSH1_MSG_IGNORE, so
3260 * we can use the primary defence.
3262 int bottom
, top
, pwlen
, i
;
3265 pwlen
= strlen(s
->password
);
3267 bottom
= 0; /* zero length passwords are OK! :-) */
3270 bottom
= pwlen
& ~7;
3274 assert(pwlen
>= bottom
&& pwlen
<= top
);
3276 randomstr
= snewn(top
+ 1, char);
3278 for (i
= bottom
; i
<= top
; i
++) {
3280 defer_packet(ssh
, s
->pwpkt_type
,
3281 PKTT_PASSWORD
, PKT_STR
, s
->password
,
3282 PKTT_OTHER
, PKT_END
);
3284 for (j
= 0; j
< i
; j
++) {
3286 randomstr
[j
] = random_byte();
3287 } while (randomstr
[j
] == '\0');
3289 randomstr
[i
] = '\0';
3290 defer_packet(ssh
, SSH1_MSG_IGNORE
,
3291 PKT_STR
, randomstr
, PKT_END
);
3294 logevent("Sending password with camouflage packets");
3295 ssh_pkt_defersend(ssh
);
3298 else if (!(ssh
->remote_bugs
& BUG_NEEDS_SSH1_PLAIN_PASSWORD
)) {
3300 * The server can't deal with SSH1_MSG_IGNORE
3301 * but can deal with padded passwords, so we
3302 * can use the secondary defence.
3308 len
= strlen(s
->password
);
3309 if (len
< sizeof(string
)) {
3311 strcpy(string
, s
->password
);
3312 len
++; /* cover the zero byte */
3313 while (len
< sizeof(string
)) {
3314 string
[len
++] = (char) random_byte();
3319 logevent("Sending length-padded password");
3320 send_packet(ssh
, s
->pwpkt_type
, PKTT_PASSWORD
,
3321 PKT_INT
, len
, PKT_DATA
, ss
, len
,
3322 PKTT_OTHER
, PKT_END
);
3325 * The server has _both_
3326 * BUG_CHOKES_ON_SSH1_IGNORE and
3327 * BUG_NEEDS_SSH1_PLAIN_PASSWORD. There is
3328 * therefore nothing we can do.
3331 len
= strlen(s
->password
);
3332 logevent("Sending unpadded password");
3333 send_packet(ssh
, s
->pwpkt_type
,
3334 PKTT_PASSWORD
, PKT_INT
, len
,
3335 PKT_DATA
, s
->password
, len
,
3336 PKTT_OTHER
, PKT_END
);
3339 send_packet(ssh
, s
->pwpkt_type
, PKTT_PASSWORD
,
3340 PKT_STR
, s
->password
, PKTT_OTHER
, PKT_END
);
3343 logevent("Sent password");
3344 memset(s
->password
, 0, strlen(s
->password
));
3346 if (pktin
->type
== SSH1_SMSG_FAILURE
) {
3347 if (flags
& FLAG_VERBOSE
)
3348 c_write_str(ssh
, "Access denied\r\n");
3349 logevent("Authentication refused");
3350 } else if (pktin
->type
!= SSH1_SMSG_SUCCESS
) {
3351 bombout(("Strange packet received, type %d", pktin
->type
));
3356 logevent("Authentication successful");
3361 void sshfwd_close(struct ssh_channel
*c
)
3365 if (ssh
->state
!= SSH_STATE_SESSION
) {
3366 assert(ssh
->state
== SSH_STATE_CLOSED
);
3370 if (c
&& !c
->closes
) {
3372 * If the channel's remoteid is -1, we have sent
3373 * CHANNEL_OPEN for this channel, but it hasn't even been
3374 * acknowledged by the server. So we must set a close flag
3375 * on it now, and then when the server acks the channel
3376 * open, we can close it then.
3378 if (((int)c
->remoteid
) != -1) {
3379 if (ssh
->version
== 1) {
3380 send_packet(ssh
, SSH1_MSG_CHANNEL_CLOSE
, PKT_INT
, c
->remoteid
,
3383 struct Packet
*pktout
;
3384 pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE
);
3385 ssh2_pkt_adduint32(pktout
, c
->remoteid
);
3386 ssh2_pkt_send(ssh
, pktout
);
3389 c
->closes
= 1; /* sent MSG_CLOSE */
3390 if (c
->type
== CHAN_X11
) {
3392 logevent("Forwarded X11 connection terminated");
3393 } else if (c
->type
== CHAN_SOCKDATA
||
3394 c
->type
== CHAN_SOCKDATA_DORMANT
) {
3396 logevent("Forwarded port closed");
3401 int sshfwd_write(struct ssh_channel
*c
, char *buf
, int len
)
3405 if (ssh
->state
!= SSH_STATE_SESSION
) {
3406 assert(ssh
->state
== SSH_STATE_CLOSED
);
3410 if (ssh
->version
== 1) {
3411 send_packet(ssh
, SSH1_MSG_CHANNEL_DATA
,
3412 PKT_INT
, c
->remoteid
,
3414 PKT_INT
, len
, PKT_DATA
, buf
, len
,
3415 PKTT_OTHER
, PKT_END
);
3417 * In SSH1 we can return 0 here - implying that forwarded
3418 * connections are never individually throttled - because
3419 * the only circumstance that can cause throttling will be
3420 * the whole SSH connection backing up, in which case
3421 * _everything_ will be throttled as a whole.
3425 ssh2_add_channel_data(c
, buf
, len
);
3426 return ssh2_try_send(c
);
3430 void sshfwd_unthrottle(struct ssh_channel
*c
, int bufsize
)
3434 if (ssh
->state
!= SSH_STATE_SESSION
) {
3435 assert(ssh
->state
== SSH_STATE_CLOSED
);
3439 if (ssh
->version
== 1) {
3440 if (c
->v
.v1
.throttling
&& bufsize
< SSH1_BUFFER_LIMIT
) {
3441 c
->v
.v1
.throttling
= 0;
3442 ssh1_throttle(ssh
, -1);
3445 ssh2_set_window(c
, OUR_V2_WINSIZE
- bufsize
);
3449 static void do_ssh1_connection(Ssh ssh
, unsigned char *in
, int inlen
,
3450 struct Packet
*pktin
)
3452 crBegin(ssh
->do_ssh1_connection_crstate
);
3454 if (ssh
->cfg
.agentfwd
&& agent_exists()) {
3455 logevent("Requesting agent forwarding");
3456 send_packet(ssh
, SSH1_CMSG_AGENT_REQUEST_FORWARDING
, PKT_END
);
3460 if (pktin
->type
!= SSH1_SMSG_SUCCESS
3461 && pktin
->type
!= SSH1_SMSG_FAILURE
) {
3462 bombout(("Protocol confusion"));
3464 } else if (pktin
->type
== SSH1_SMSG_FAILURE
) {
3465 logevent("Agent forwarding refused");
3467 logevent("Agent forwarding enabled");
3468 ssh
->agentfwd_enabled
= TRUE
;
3472 if (ssh
->cfg
.x11_forward
) {
3473 char proto
[20], data
[64];
3474 logevent("Requesting X11 forwarding");
3475 ssh
->x11auth
= x11_invent_auth(proto
, sizeof(proto
),
3476 data
, sizeof(data
), ssh
->cfg
.x11_auth
);
3477 x11_get_real_auth(ssh
->x11auth
, ssh
->cfg
.x11_display
);
3478 if (ssh
->v1_local_protoflags
& SSH1_PROTOFLAG_SCREEN_NUMBER
) {
3479 send_packet(ssh
, SSH1_CMSG_X11_REQUEST_FORWARDING
,
3480 PKT_STR
, proto
, PKT_STR
, data
,
3481 PKT_INT
, x11_get_screen_number(ssh
->cfg
.x11_display
),
3484 send_packet(ssh
, SSH1_CMSG_X11_REQUEST_FORWARDING
,
3485 PKT_STR
, proto
, PKT_STR
, data
, PKT_END
);
3490 if (pktin
->type
!= SSH1_SMSG_SUCCESS
3491 && pktin
->type
!= SSH1_SMSG_FAILURE
) {
3492 bombout(("Protocol confusion"));
3494 } else if (pktin
->type
== SSH1_SMSG_FAILURE
) {
3495 logevent("X11 forwarding refused");
3497 logevent("X11 forwarding enabled");
3498 ssh
->X11_fwd_enabled
= TRUE
;
3505 int sport
,dport
,sserv
,dserv
;
3506 char sports
[256], dports
[256], saddr
[256], host
[256];
3508 ssh
->rportfwds
= newtree234(ssh_rportcmp_ssh1
);
3509 /* Add port forwardings. */
3510 ssh
->portfwd_strptr
= ssh
->cfg
.portfwd
;
3511 while (*ssh
->portfwd_strptr
) {
3512 type
= *ssh
->portfwd_strptr
++;
3515 while (*ssh
->portfwd_strptr
&& *ssh
->portfwd_strptr
!= '\t') {
3516 if (*ssh
->portfwd_strptr
== ':') {
3518 * We've seen a colon in the middle of the
3519 * source port number. This means that
3520 * everything we've seen until now is the
3521 * source _address_, so we'll move it into
3522 * saddr and start sports from the beginning
3525 ssh
->portfwd_strptr
++;
3527 strcpy(saddr
, sports
);
3530 if (n
< 255) sports
[n
++] = *ssh
->portfwd_strptr
++;
3534 if (*ssh
->portfwd_strptr
== '\t')
3535 ssh
->portfwd_strptr
++;
3537 while (*ssh
->portfwd_strptr
&& *ssh
->portfwd_strptr
!= ':') {
3538 if (n
< 255) host
[n
++] = *ssh
->portfwd_strptr
++;
3541 if (*ssh
->portfwd_strptr
== ':')
3542 ssh
->portfwd_strptr
++;
3544 while (*ssh
->portfwd_strptr
) {
3545 if (n
< 255) dports
[n
++] = *ssh
->portfwd_strptr
++;
3548 ssh
->portfwd_strptr
++;
3549 dport
= atoi(dports
);
3553 dport
= net_service_lookup(dports
);
3555 logeventf(ssh
, "Service lookup failed for"
3556 " destination port \"%s\"", dports
);
3560 while (*ssh
->portfwd_strptr
) ssh
->portfwd_strptr
++;
3562 ssh
->portfwd_strptr
++; /* eat the NUL and move to next one */
3564 sport
= atoi(sports
);
3568 sport
= net_service_lookup(sports
);
3570 logeventf(ssh
, "Service lookup failed for source"
3571 " port \"%s\"", sports
);
3574 if (sport
&& dport
) {
3575 /* Set up a description of the source port. */
3576 static char *sportdesc
;
3577 sportdesc
= dupprintf("%.*s%.*s%.*s%.*s%d%.*s",
3578 (int)(*saddr?
strlen(saddr
):0), *saddr?saddr
:NULL
,
3579 (int)(*saddr?
1:0), ":",
3580 (int)(sserv ?
strlen(sports
) : 0), sports
,
3581 sserv
, "(", sport
, sserv
, ")");
3583 /* Verbose description of the destination port */
3584 char *dportdesc
= dupprintf("%s:%.*s%.*s%d%.*s",
3586 (int)(dserv ?
strlen(dports
) : 0), dports
,
3587 dserv
, "(", dport
, dserv
, ")");
3588 const char *err
= pfd_addforward(host
, dport
,
3589 *saddr ? saddr
: NULL
,
3590 sport
, ssh
, &ssh
->cfg
);
3592 logeventf(ssh
, "Local port %s forward to %s"
3593 " failed: %s", sportdesc
, dportdesc
, err
);
3595 logeventf(ssh
, "Local port %s forwarding to %s",
3596 sportdesc
, dportdesc
);
3599 } else if (type
== 'D') {
3600 const char *err
= pfd_addforward(NULL
, -1,
3601 *saddr ? saddr
: NULL
,
3602 sport
, ssh
, &ssh
->cfg
);
3604 logeventf(ssh
, "Local port %s SOCKS dynamic forward"
3605 " setup failed: %s", sportdesc
, err
);
3607 logeventf(ssh
, "Local port %s doing SOCKS"
3608 " dynamic forwarding", sportdesc
);
3611 struct ssh_rportfwd
*pf
;
3612 pf
= snew(struct ssh_rportfwd
);
3613 strcpy(pf
->dhost
, host
);
3617 "SSH1 cannot handle source address spec \"%s:%d\"; ignoring",
3620 if (add234(ssh
->rportfwds
, pf
) != pf
) {
3622 "Duplicate remote port forwarding to %s:%d",
3626 logeventf(ssh
, "Requesting remote port %.*s%.*s%d%.*s"
3627 " forward to %s:%.*s%.*s%d%.*s",
3628 (int)(sserv ?
strlen(sports
) : 0), sports
,
3629 sserv
, "(", sport
, sserv
, ")",
3631 (int)(dserv ?
strlen(dports
) : 0), dports
,
3632 dserv
, "(", dport
, dserv
, ")");
3633 send_packet(ssh
, SSH1_CMSG_PORT_FORWARD_REQUEST
,
3641 if (pktin
->type
!= SSH1_SMSG_SUCCESS
3642 && pktin
->type
!= SSH1_SMSG_FAILURE
) {
3643 bombout(("Protocol confusion"));
3645 } else if (pktin
->type
== SSH1_SMSG_FAILURE
) {
3646 c_write_str(ssh
, "Server refused port"
3649 logevent("Remote port forwarding enabled");
3657 if (!ssh
->cfg
.nopty
) {
3658 /* Unpick the terminal-speed string. */
3659 /* XXX perhaps we should allow no speeds to be sent. */
3660 ssh
->ospeed
= 38400; ssh
->ispeed
= 38400; /* last-resort defaults */
3661 sscanf(ssh
->cfg
.termspeed
, "%d,%d", &ssh
->ospeed
, &ssh
->ispeed
);
3662 /* Send the pty request. */
3663 send_packet(ssh
, SSH1_CMSG_REQUEST_PTY
,
3664 PKT_STR
, ssh
->cfg
.termtype
,
3665 PKT_INT
, ssh
->term_height
,
3666 PKT_INT
, ssh
->term_width
,
3667 PKT_INT
, 0, PKT_INT
, 0, /* width,height in pixels */
3668 PKT_CHAR
, 192, PKT_INT
, ssh
->ispeed
, /* TTY_OP_ISPEED */
3669 PKT_CHAR
, 193, PKT_INT
, ssh
->ospeed
, /* TTY_OP_OSPEED */
3670 PKT_CHAR
, 0, PKT_END
);
3671 ssh
->state
= SSH_STATE_INTERMED
;
3675 if (pktin
->type
!= SSH1_SMSG_SUCCESS
3676 && pktin
->type
!= SSH1_SMSG_FAILURE
) {
3677 bombout(("Protocol confusion"));
3679 } else if (pktin
->type
== SSH1_SMSG_FAILURE
) {
3680 c_write_str(ssh
, "Server refused to allocate pty\r\n");
3681 ssh
->editing
= ssh
->echoing
= 1;
3683 logeventf(ssh
, "Allocated pty (ospeed %dbps, ispeed %dbps)",
3684 ssh
->ospeed
, ssh
->ispeed
);
3686 ssh
->editing
= ssh
->echoing
= 1;
3689 if (ssh
->cfg
.compression
) {
3690 send_packet(ssh
, SSH1_CMSG_REQUEST_COMPRESSION
, PKT_INT
, 6, PKT_END
);
3694 if (pktin
->type
!= SSH1_SMSG_SUCCESS
3695 && pktin
->type
!= SSH1_SMSG_FAILURE
) {
3696 bombout(("Protocol confusion"));
3698 } else if (pktin
->type
== SSH1_SMSG_FAILURE
) {
3699 c_write_str(ssh
, "Server refused to compress\r\n");
3701 logevent("Started compression");
3702 ssh
->v1_compressing
= TRUE
;
3703 ssh
->cs_comp_ctx
= zlib_compress_init();
3704 logevent("Initialised zlib (RFC1950) compression");
3705 ssh
->sc_comp_ctx
= zlib_decompress_init();
3706 logevent("Initialised zlib (RFC1950) decompression");
3710 * Start the shell or command.
3712 * Special case: if the first-choice command is an SSH2
3713 * subsystem (hence not usable here) and the second choice
3714 * exists, we fall straight back to that.
3717 char *cmd
= ssh
->cfg
.remote_cmd_ptr
;
3719 if (ssh
->cfg
.ssh_subsys
&& ssh
->cfg
.remote_cmd_ptr2
) {
3720 cmd
= ssh
->cfg
.remote_cmd_ptr2
;
3721 ssh
->fallback_cmd
= TRUE
;
3724 send_packet(ssh
, SSH1_CMSG_EXEC_CMD
, PKT_STR
, cmd
, PKT_END
);
3726 send_packet(ssh
, SSH1_CMSG_EXEC_SHELL
, PKT_END
);
3727 logevent("Started session");
3730 ssh
->state
= SSH_STATE_SESSION
;
3731 if (ssh
->size_needed
)
3732 ssh_size(ssh
, ssh
->term_width
, ssh
->term_height
);
3733 if (ssh
->eof_needed
)
3734 ssh_special(ssh
, TS_EOF
);
3737 ldisc_send(ssh
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
3739 ssh
->channels
= newtree234(ssh_channelcmp
);
3743 if (pktin
->type
== SSH1_SMSG_STDOUT_DATA
||
3744 pktin
->type
== SSH1_SMSG_STDERR_DATA
) {
3746 int stringlen
, bufsize
;
3748 ssh_pkt_getstring(pktin
, &string
, &stringlen
);
3749 if (string
== NULL
) {
3750 bombout(("Incoming terminal data packet was badly formed"));
3755 from_backend(ssh
->frontend
,
3756 pktin
->type
== SSH1_SMSG_STDERR_DATA
,
3758 if (!ssh
->v1_stdout_throttling
&& bufsize
> SSH1_BUFFER_LIMIT
) {
3759 ssh
->v1_stdout_throttling
= 1;
3760 ssh1_throttle(ssh
, +1);
3762 } else if (pktin
->type
== SSH1_MSG_DISCONNECT
) {
3763 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
3764 logevent("Received disconnect request");
3766 } else if (pktin
->type
== SSH1_SMSG_X11_OPEN
) {
3767 /* Remote side is trying to open a channel to talk to our
3768 * X-Server. Give them back a local channel number. */
3769 struct ssh_channel
*c
;
3770 int remoteid
= ssh_pkt_getuint32(pktin
);
3772 logevent("Received X11 connect request");
3773 /* Refuse if X11 forwarding is disabled. */
3774 if (!ssh
->X11_fwd_enabled
) {
3775 send_packet(ssh
, SSH1_MSG_CHANNEL_OPEN_FAILURE
,
3776 PKT_INT
, remoteid
, PKT_END
);
3777 logevent("Rejected X11 connect request");
3779 c
= snew(struct ssh_channel
);
3782 if (x11_init(&c
->u
.x11
.s
, ssh
->cfg
.x11_display
, c
,
3783 ssh
->x11auth
, NULL
, -1, &ssh
->cfg
) != NULL
) {
3784 logevent("Opening X11 forward connection failed");
3786 send_packet(ssh
, SSH1_MSG_CHANNEL_OPEN_FAILURE
,
3787 PKT_INT
, remoteid
, PKT_END
);
3790 ("Opening X11 forward connection succeeded");
3791 c
->remoteid
= remoteid
;
3792 c
->localid
= alloc_channel_id(ssh
);
3794 c
->v
.v1
.throttling
= 0;
3795 c
->type
= CHAN_X11
; /* identify channel type */
3796 add234(ssh
->channels
, c
);
3797 send_packet(ssh
, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
,
3798 PKT_INT
, c
->remoteid
, PKT_INT
,
3799 c
->localid
, PKT_END
);
3800 logevent("Opened X11 forward channel");
3803 } else if (pktin
->type
== SSH1_SMSG_AGENT_OPEN
) {
3804 /* Remote side is trying to open a channel to talk to our
3805 * agent. Give them back a local channel number. */
3806 struct ssh_channel
*c
;
3807 int remoteid
= ssh_pkt_getuint32(pktin
);
3809 /* Refuse if agent forwarding is disabled. */
3810 if (!ssh
->agentfwd_enabled
) {
3811 send_packet(ssh
, SSH1_MSG_CHANNEL_OPEN_FAILURE
,
3812 PKT_INT
, remoteid
, PKT_END
);
3814 c
= snew(struct ssh_channel
);
3816 c
->remoteid
= remoteid
;
3817 c
->localid
= alloc_channel_id(ssh
);
3819 c
->v
.v1
.throttling
= 0;
3820 c
->type
= CHAN_AGENT
; /* identify channel type */
3821 c
->u
.a
.lensofar
= 0;
3822 add234(ssh
->channels
, c
);
3823 send_packet(ssh
, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
,
3824 PKT_INT
, c
->remoteid
, PKT_INT
, c
->localid
,
3827 } else if (pktin
->type
== SSH1_MSG_PORT_OPEN
) {
3828 /* Remote side is trying to open a channel to talk to a
3829 * forwarded port. Give them back a local channel number. */
3830 struct ssh_channel
*c
;
3831 struct ssh_rportfwd pf
;
3834 char *host
, buf
[1024];
3836 c
= snew(struct ssh_channel
);
3839 remoteid
= ssh_pkt_getuint32(pktin
);
3840 ssh_pkt_getstring(pktin
, &host
, &hostsize
);
3841 port
= ssh_pkt_getuint32(pktin
);
3843 if (hostsize
>= lenof(pf
.dhost
))
3844 hostsize
= lenof(pf
.dhost
)-1;
3845 memcpy(pf
.dhost
, host
, hostsize
);
3846 pf
.dhost
[hostsize
] = '\0';
3849 if (find234(ssh
->rportfwds
, &pf
, NULL
) == NULL
) {
3850 sprintf(buf
, "Rejected remote port open request for %s:%d",
3853 send_packet(ssh
, SSH1_MSG_CHANNEL_OPEN_FAILURE
,
3854 PKT_INT
, remoteid
, PKT_END
);
3856 sprintf(buf
, "Received remote port open request for %s:%d",
3859 e
= pfd_newconnect(&c
->u
.pfd
.s
, pf
.dhost
, port
,
3863 sprintf(buf
, "Port open failed: %s", e
);
3866 send_packet(ssh
, SSH1_MSG_CHANNEL_OPEN_FAILURE
,
3867 PKT_INT
, remoteid
, PKT_END
);
3869 c
->remoteid
= remoteid
;
3870 c
->localid
= alloc_channel_id(ssh
);
3872 c
->v
.v1
.throttling
= 0;
3873 c
->type
= CHAN_SOCKDATA
; /* identify channel type */
3874 add234(ssh
->channels
, c
);
3875 send_packet(ssh
, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
,
3876 PKT_INT
, c
->remoteid
, PKT_INT
,
3877 c
->localid
, PKT_END
);
3878 logevent("Forwarded port opened successfully");
3882 } else if (pktin
->type
== SSH1_MSG_CHANNEL_OPEN_CONFIRMATION
) {
3883 unsigned int remoteid
= ssh_pkt_getuint32(pktin
);
3884 unsigned int localid
= ssh_pkt_getuint32(pktin
);
3885 struct ssh_channel
*c
;
3887 c
= find234(ssh
->channels
, &remoteid
, ssh_channelfind
);
3888 if (c
&& c
->type
== CHAN_SOCKDATA_DORMANT
) {
3889 c
->remoteid
= localid
;
3890 c
->type
= CHAN_SOCKDATA
;
3891 c
->v
.v1
.throttling
= 0;
3892 pfd_confirm(c
->u
.pfd
.s
);
3895 if (c
&& c
->closes
) {
3897 * We have a pending close on this channel,
3898 * which we decided on before the server acked
3899 * the channel open. So now we know the
3900 * remoteid, we can close it again.
3902 send_packet(ssh
, SSH1_MSG_CHANNEL_CLOSE
,
3903 PKT_INT
, c
->remoteid
, PKT_END
);
3906 } else if (pktin
->type
== SSH1_MSG_CHANNEL_OPEN_FAILURE
) {
3907 unsigned int remoteid
= ssh_pkt_getuint32(pktin
);
3908 struct ssh_channel
*c
;
3910 c
= find234(ssh
->channels
, &remoteid
, ssh_channelfind
);
3911 if (c
&& c
->type
== CHAN_SOCKDATA_DORMANT
) {
3912 logevent("Forwarded connection refused by server");
3913 pfd_close(c
->u
.pfd
.s
);
3914 del234(ssh
->channels
, c
);
3918 } else if (pktin
->type
== SSH1_MSG_CHANNEL_CLOSE
||
3919 pktin
->type
== SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION
) {
3920 /* Remote side closes a channel. */
3921 unsigned i
= ssh_pkt_getuint32(pktin
);
3922 struct ssh_channel
*c
;
3923 c
= find234(ssh
->channels
, &i
, ssh_channelfind
);
3924 if (c
&& ((int)c
->remoteid
) != -1) {
3927 (pktin
->type
== SSH1_MSG_CHANNEL_CLOSE ?
1 : 2);
3929 if ((c
->closes
== 0) && (c
->type
== CHAN_X11
)) {
3930 logevent("Forwarded X11 connection terminated");
3931 assert(c
->u
.x11
.s
!= NULL
);
3932 x11_close(c
->u
.x11
.s
);
3935 if ((c
->closes
== 0) && (c
->type
== CHAN_SOCKDATA
)) {
3936 logevent("Forwarded port closed");
3937 assert(c
->u
.pfd
.s
!= NULL
);
3938 pfd_close(c
->u
.pfd
.s
);
3942 c
->closes
|= (closetype
<< 2); /* seen this message */
3943 if (!(c
->closes
& closetype
)) {
3944 send_packet(ssh
, pktin
->type
, PKT_INT
, c
->remoteid
,
3946 c
->closes
|= closetype
; /* sent it too */
3949 if (c
->closes
== 15) {
3950 del234(ssh
->channels
, c
);
3954 bombout(("Received CHANNEL_CLOSE%s for %s channel %d\n",
3955 pktin
->type
== SSH1_MSG_CHANNEL_CLOSE ?
"" :
3956 "_CONFIRMATION", c ?
"half-open" : "nonexistent",
3960 } else if (pktin
->type
== SSH1_MSG_CHANNEL_DATA
) {
3961 /* Data sent down one of our channels. */
3962 int i
= ssh_pkt_getuint32(pktin
);
3965 struct ssh_channel
*c
;
3967 ssh_pkt_getstring(pktin
, &p
, &len
);
3969 c
= find234(ssh
->channels
, &i
, ssh_channelfind
);
3974 bufsize
= x11_send(c
->u
.x11
.s
, p
, len
);
3977 bufsize
= pfd_send(c
->u
.pfd
.s
, p
, len
);
3980 /* Data for an agent message. Buffer it. */
3982 if (c
->u
.a
.lensofar
< 4) {
3983 int l
= min(4 - c
->u
.a
.lensofar
, len
);
3984 memcpy(c
->u
.a
.msglen
+ c
->u
.a
.lensofar
, p
,
3988 c
->u
.a
.lensofar
+= l
;
3990 if (c
->u
.a
.lensofar
== 4) {
3992 4 + GET_32BIT(c
->u
.a
.msglen
);
3993 c
->u
.a
.message
= snewn(c
->u
.a
.totallen
,
3995 memcpy(c
->u
.a
.message
, c
->u
.a
.msglen
, 4);
3997 if (c
->u
.a
.lensofar
>= 4 && len
> 0) {
3999 min(c
->u
.a
.totallen
- c
->u
.a
.lensofar
,
4001 memcpy(c
->u
.a
.message
+ c
->u
.a
.lensofar
, p
,
4005 c
->u
.a
.lensofar
+= l
;
4007 if (c
->u
.a
.lensofar
== c
->u
.a
.totallen
) {
4010 if (agent_query(c
->u
.a
.message
,
4013 ssh_agentf_callback
, c
))
4014 ssh_agentf_callback(c
, reply
, replylen
);
4015 sfree(c
->u
.a
.message
);
4016 c
->u
.a
.lensofar
= 0;
4019 bufsize
= 0; /* agent channels never back up */
4022 if (!c
->v
.v1
.throttling
&& bufsize
> SSH1_BUFFER_LIMIT
) {
4023 c
->v
.v1
.throttling
= 1;
4024 ssh1_throttle(ssh
, +1);
4027 } else if (pktin
->type
== SSH1_SMSG_SUCCESS
) {
4028 /* may be from EXEC_SHELL on some servers */
4029 } else if (pktin
->type
== SSH1_SMSG_FAILURE
) {
4030 /* may be from EXEC_SHELL on some servers
4031 * if no pty is available or in other odd cases. Ignore */
4032 } else if (pktin
->type
== SSH1_SMSG_EXIT_STATUS
) {
4034 ssh
->exitcode
= ssh_pkt_getuint32(pktin
);
4035 sprintf(buf
, "Server sent command exit status %d",
4038 send_packet(ssh
, SSH1_CMSG_EXIT_CONFIRMATION
, PKT_END
);
4040 * In case `helpful' firewalls or proxies tack
4041 * extra human-readable text on the end of the
4042 * session which we might mistake for another
4043 * encrypted packet, we close the session once
4044 * we've sent EXIT_CONFIRMATION.
4046 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
4049 bombout(("Strange packet received: type %d", pktin
->type
));
4054 int len
= min(inlen
, 512);
4055 send_packet(ssh
, SSH1_CMSG_STDIN_DATA
, PKTT_DATA
,
4056 PKT_INT
, len
, PKT_DATA
, in
, len
,
4057 PKTT_OTHER
, PKT_END
);
4068 * Handle the top-level SSH2 protocol.
4070 static void ssh1_msg_debug(Ssh ssh
, struct Packet
*pktin
)
4075 ssh_pkt_getstring(pktin
, &msg
, &msglen
);
4076 buf
= dupprintf("Remote debug message: %.*s", msglen
, msg
);
4081 static void ssh1_msg_disconnect(Ssh ssh
, struct Packet
*pktin
)
4083 /* log reason code in disconnect message */
4087 ssh_pkt_getstring(pktin
, &msg
, &msglen
);
4088 bombout(("Server sent disconnect message:\n\"%.*s\"", msglen
, msg
));
4091 void ssh_msg_ignore(Ssh ssh
, struct Packet
*pktin
)
4093 /* Do nothing, because we're ignoring it! Duhh. */
4096 static void ssh1_protocol_setup(Ssh ssh
)
4101 * Most messages are handled by the coroutines.
4103 for (i
= 0; i
< 256; i
++)
4104 ssh
->packet_dispatch
[i
] = NULL
;
4107 * These special message types we install handlers for.
4109 ssh
->packet_dispatch
[SSH1_MSG_DISCONNECT
] = ssh1_msg_disconnect
;
4110 ssh
->packet_dispatch
[SSH1_MSG_IGNORE
] = ssh_msg_ignore
;
4111 ssh
->packet_dispatch
[SSH1_MSG_DEBUG
] = ssh1_msg_debug
;
4114 static void ssh1_protocol(Ssh ssh
, unsigned char *in
, int inlen
,
4115 struct Packet
*pktin
)
4117 if (ssh
->state
== SSH_STATE_CLOSED
)
4120 if (pktin
&& ssh
->packet_dispatch
[pktin
->type
]) {
4121 ssh
->packet_dispatch
[pktin
->type
](ssh
, pktin
);
4125 if (!ssh
->protocol_initial_phase_done
) {
4126 if (do_ssh1_login(ssh
, in
, inlen
, pktin
))
4127 ssh
->protocol_initial_phase_done
= TRUE
;
4132 do_ssh1_connection(ssh
, in
, inlen
, pktin
);
4136 * Utility routine for decoding comma-separated strings in KEXINIT.
4138 static int in_commasep_string(char *needle
, char *haystack
, int haylen
)
4141 if (!needle
|| !haystack
) /* protect against null pointers */
4143 needlen
= strlen(needle
);
4146 * Is it at the start of the string?
4148 if (haylen
>= needlen
&& /* haystack is long enough */
4149 !memcmp(needle
, haystack
, needlen
) && /* initial match */
4150 (haylen
== needlen
|| haystack
[needlen
] == ',')
4151 /* either , or EOS follows */
4155 * If not, search for the next comma and resume after that.
4156 * If no comma found, terminate.
4158 while (haylen
> 0 && *haystack
!= ',')
4159 haylen
--, haystack
++;
4162 haylen
--, haystack
++; /* skip over comma itself */
4167 * SSH2 key creation method.
4169 static void ssh2_mkkey(Ssh ssh
, Bignum K
, unsigned char *H
,
4170 unsigned char *sessid
, char chr
,
4171 unsigned char *keyspace
)
4174 /* First 20 bytes. */
4176 if (!(ssh
->remote_bugs
& BUG_SSH2_DERIVEKEY
))
4178 SHA_Bytes(&s
, H
, 20);
4179 SHA_Bytes(&s
, &chr
, 1);
4180 SHA_Bytes(&s
, sessid
, 20);
4181 SHA_Final(&s
, keyspace
);
4182 /* Next 20 bytes. */
4184 if (!(ssh
->remote_bugs
& BUG_SSH2_DERIVEKEY
))
4186 SHA_Bytes(&s
, H
, 20);
4187 SHA_Bytes(&s
, keyspace
, 20);
4188 SHA_Final(&s
, keyspace
+ 20);
4192 * Handle the SSH2 transport layer.
4194 static int do_ssh2_transport(Ssh ssh
, unsigned char *in
, int inlen
,
4195 struct Packet
*pktin
)
4197 struct do_ssh2_transport_state
{
4198 int nbits
, pbits
, warn
;
4199 Bignum p
, g
, e
, f
, K
;
4200 int kex_init_value
, kex_reply_value
;
4201 const struct ssh_mac
**maclist
;
4203 const struct ssh2_cipher
*cscipher_tobe
;
4204 const struct ssh2_cipher
*sccipher_tobe
;
4205 const struct ssh_mac
*csmac_tobe
;
4206 const struct ssh_mac
*scmac_tobe
;
4207 const struct ssh_compress
*cscomp_tobe
;
4208 const struct ssh_compress
*sccomp_tobe
;
4209 char *hostkeydata
, *sigdata
, *keystr
, *fingerprint
;
4210 int hostkeylen
, siglen
;
4211 void *hkey
; /* actual host key */
4212 unsigned char exchange_hash
[20];
4213 int n_preferred_ciphers
;
4214 const struct ssh2_ciphers
*preferred_ciphers
[CIPHER_MAX
];
4215 const struct ssh_compress
*preferred_comp
;
4217 struct Packet
*pktout
;
4219 crState(do_ssh2_transport_state
);
4221 crBegin(ssh
->do_ssh2_transport_crstate
);
4223 s
->cscipher_tobe
= s
->sccipher_tobe
= NULL
;
4224 s
->csmac_tobe
= s
->scmac_tobe
= NULL
;
4225 s
->cscomp_tobe
= s
->sccomp_tobe
= NULL
;
4233 * Set up the preferred ciphers. (NULL => warn below here)
4235 s
->n_preferred_ciphers
= 0;
4236 for (i
= 0; i
< CIPHER_MAX
; i
++) {
4237 switch (ssh
->cfg
.ssh_cipherlist
[i
]) {
4238 case CIPHER_BLOWFISH
:
4239 s
->preferred_ciphers
[s
->n_preferred_ciphers
++] = &ssh2_blowfish
;
4242 if (ssh
->cfg
.ssh2_des_cbc
) {
4243 s
->preferred_ciphers
[s
->n_preferred_ciphers
++] = &ssh2_des
;
4247 s
->preferred_ciphers
[s
->n_preferred_ciphers
++] = &ssh2_3des
;
4250 s
->preferred_ciphers
[s
->n_preferred_ciphers
++] = &ssh2_aes
;
4253 /* Flag for later. Don't bother if it's the last in
4255 if (i
< CIPHER_MAX
- 1) {
4256 s
->preferred_ciphers
[s
->n_preferred_ciphers
++] = NULL
;
4264 * Set up preferred compression.
4266 if (ssh
->cfg
.compression
)
4267 s
->preferred_comp
= &ssh_zlib
;
4269 s
->preferred_comp
= &ssh_comp_none
;
4272 * Be prepared to work around the buggy MAC problem.
4274 if (ssh
->remote_bugs
& BUG_SSH2_HMAC
)
4275 s
->maclist
= buggymacs
, s
->nmacs
= lenof(buggymacs
);
4277 s
->maclist
= macs
, s
->nmacs
= lenof(macs
);
4281 int i
, j
, cipherstr_started
;
4284 * Enable queueing of outgoing auth- or connection-layer
4285 * packets while we are in the middle of a key exchange.
4287 ssh
->queueing
= TRUE
;
4290 * Construct and send our key exchange packet.
4292 s
->pktout
= ssh2_pkt_init(SSH2_MSG_KEXINIT
);
4293 for (i
= 0; i
< 16; i
++)
4294 ssh2_pkt_addbyte(s
->pktout
, (unsigned char) random_byte());
4295 /* List key exchange algorithms. */
4296 ssh2_pkt_addstring_start(s
->pktout
);
4297 for (i
= 0; i
< lenof(kex_algs
); i
++) {
4298 if (kex_algs
[i
] == &ssh_diffiehellman_gex
&&
4299 (ssh
->remote_bugs
& BUG_SSH2_DH_GEX
))
4301 ssh2_pkt_addstring_str(s
->pktout
, kex_algs
[i
]->name
);
4302 if (i
< lenof(kex_algs
) - 1)
4303 ssh2_pkt_addstring_str(s
->pktout
, ",");
4305 /* List server host key algorithms. */
4306 ssh2_pkt_addstring_start(s
->pktout
);
4307 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
4308 ssh2_pkt_addstring_str(s
->pktout
, hostkey_algs
[i
]->name
);
4309 if (i
< lenof(hostkey_algs
) - 1)
4310 ssh2_pkt_addstring_str(s
->pktout
, ",");
4312 /* List client->server encryption algorithms. */
4313 ssh2_pkt_addstring_start(s
->pktout
);
4314 cipherstr_started
= 0;
4315 for (i
= 0; i
< s
->n_preferred_ciphers
; i
++) {
4316 const struct ssh2_ciphers
*c
= s
->preferred_ciphers
[i
];
4317 if (!c
) continue; /* warning flag */
4318 for (j
= 0; j
< c
->nciphers
; j
++) {
4319 if (cipherstr_started
)
4320 ssh2_pkt_addstring_str(s
->pktout
, ",");
4321 ssh2_pkt_addstring_str(s
->pktout
, c
->list
[j
]->name
);
4322 cipherstr_started
= 1;
4325 /* List server->client encryption algorithms. */
4326 ssh2_pkt_addstring_start(s
->pktout
);
4327 cipherstr_started
= 0;
4328 for (i
= 0; i
< s
->n_preferred_ciphers
; i
++) {
4329 const struct ssh2_ciphers
*c
= s
->preferred_ciphers
[i
];
4330 if (!c
) continue; /* warning flag */
4331 for (j
= 0; j
< c
->nciphers
; j
++) {
4332 if (cipherstr_started
)
4333 ssh2_pkt_addstring_str(s
->pktout
, ",");
4334 ssh2_pkt_addstring_str(s
->pktout
, c
->list
[j
]->name
);
4335 cipherstr_started
= 1;
4338 /* List client->server MAC algorithms. */
4339 ssh2_pkt_addstring_start(s
->pktout
);
4340 for (i
= 0; i
< s
->nmacs
; i
++) {
4341 ssh2_pkt_addstring_str(s
->pktout
, s
->maclist
[i
]->name
);
4342 if (i
< s
->nmacs
- 1)
4343 ssh2_pkt_addstring_str(s
->pktout
, ",");
4345 /* List server->client MAC algorithms. */
4346 ssh2_pkt_addstring_start(s
->pktout
);
4347 for (i
= 0; i
< s
->nmacs
; i
++) {
4348 ssh2_pkt_addstring_str(s
->pktout
, s
->maclist
[i
]->name
);
4349 if (i
< s
->nmacs
- 1)
4350 ssh2_pkt_addstring_str(s
->pktout
, ",");
4352 /* List client->server compression algorithms. */
4353 ssh2_pkt_addstring_start(s
->pktout
);
4354 assert(lenof(compressions
) > 1);
4355 ssh2_pkt_addstring_str(s
->pktout
, s
->preferred_comp
->name
);
4356 for (i
= 0; i
< lenof(compressions
); i
++) {
4357 const struct ssh_compress
*c
= compressions
[i
];
4358 if (c
!= s
->preferred_comp
) {
4359 ssh2_pkt_addstring_str(s
->pktout
, ",");
4360 ssh2_pkt_addstring_str(s
->pktout
, c
->name
);
4363 /* List server->client compression algorithms. */
4364 ssh2_pkt_addstring_start(s
->pktout
);
4365 assert(lenof(compressions
) > 1);
4366 ssh2_pkt_addstring_str(s
->pktout
, s
->preferred_comp
->name
);
4367 for (i
= 0; i
< lenof(compressions
); i
++) {
4368 const struct ssh_compress
*c
= compressions
[i
];
4369 if (c
!= s
->preferred_comp
) {
4370 ssh2_pkt_addstring_str(s
->pktout
, ",");
4371 ssh2_pkt_addstring_str(s
->pktout
, c
->name
);
4374 /* List client->server languages. Empty list. */
4375 ssh2_pkt_addstring_start(s
->pktout
);
4376 /* List server->client languages. Empty list. */
4377 ssh2_pkt_addstring_start(s
->pktout
);
4378 /* First KEX packet does _not_ follow, because we're not that brave. */
4379 ssh2_pkt_addbool(s
->pktout
, FALSE
);
4381 ssh2_pkt_adduint32(s
->pktout
, 0);
4384 ssh
->exhash
= ssh
->exhashbase
;
4385 sha_string(&ssh
->exhash
, s
->pktout
->data
+ 5, s
->pktout
->length
- 5);
4387 ssh2_pkt_send_noqueue(ssh
, s
->pktout
);
4391 if (pktin
->length
> 5)
4392 sha_string(&ssh
->exhash
, pktin
->data
+ 5, pktin
->length
- 5);
4395 * Now examine the other side's KEXINIT to see what we're up
4402 if (pktin
->type
!= SSH2_MSG_KEXINIT
) {
4403 bombout(("expected key exchange packet from server"));
4407 ssh
->hostkey
= NULL
;
4408 s
->cscipher_tobe
= NULL
;
4409 s
->sccipher_tobe
= NULL
;
4410 s
->csmac_tobe
= NULL
;
4411 s
->scmac_tobe
= NULL
;
4412 s
->cscomp_tobe
= NULL
;
4413 s
->sccomp_tobe
= NULL
;
4414 pktin
->savedpos
+= 16; /* skip garbage cookie */
4415 ssh_pkt_getstring(pktin
, &str
, &len
); /* key exchange algorithms */
4416 for (i
= 0; i
< lenof(kex_algs
); i
++) {
4417 if (kex_algs
[i
] == &ssh_diffiehellman_gex
&&
4418 (ssh
->remote_bugs
& BUG_SSH2_DH_GEX
))
4420 if (in_commasep_string(kex_algs
[i
]->name
, str
, len
)) {
4421 ssh
->kex
= kex_algs
[i
];
4425 ssh_pkt_getstring(pktin
, &str
, &len
); /* host key algorithms */
4426 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
4427 if (in_commasep_string(hostkey_algs
[i
]->name
, str
, len
)) {
4428 ssh
->hostkey
= hostkey_algs
[i
];
4432 ssh_pkt_getstring(pktin
, &str
, &len
); /* client->server cipher */
4434 for (i
= 0; i
< s
->n_preferred_ciphers
; i
++) {
4435 const struct ssh2_ciphers
*c
= s
->preferred_ciphers
[i
];
4439 for (j
= 0; j
< c
->nciphers
; j
++) {
4440 if (in_commasep_string(c
->list
[j
]->name
, str
, len
)) {
4441 s
->cscipher_tobe
= c
->list
[j
];
4446 if (s
->cscipher_tobe
) {
4448 askcipher(ssh
->frontend
, s
->cscipher_tobe
->name
, 1);
4452 if (!s
->cscipher_tobe
) {
4453 bombout(("Couldn't agree a client-to-server cipher (available: %s)",
4454 str ? str
: "(null)"));
4458 ssh_pkt_getstring(pktin
, &str
, &len
); /* server->client cipher */
4460 for (i
= 0; i
< s
->n_preferred_ciphers
; i
++) {
4461 const struct ssh2_ciphers
*c
= s
->preferred_ciphers
[i
];
4465 for (j
= 0; j
< c
->nciphers
; j
++) {
4466 if (in_commasep_string(c
->list
[j
]->name
, str
, len
)) {
4467 s
->sccipher_tobe
= c
->list
[j
];
4472 if (s
->sccipher_tobe
) {
4474 askcipher(ssh
->frontend
, s
->sccipher_tobe
->name
, 2);
4478 if (!s
->sccipher_tobe
) {
4479 bombout(("Couldn't agree a server-to-client cipher (available: %s)",
4480 str ? str
: "(null)"));
4484 ssh_pkt_getstring(pktin
, &str
, &len
); /* client->server mac */
4485 for (i
= 0; i
< s
->nmacs
; i
++) {
4486 if (in_commasep_string(s
->maclist
[i
]->name
, str
, len
)) {
4487 s
->csmac_tobe
= s
->maclist
[i
];
4491 ssh_pkt_getstring(pktin
, &str
, &len
); /* server->client mac */
4492 for (i
= 0; i
< s
->nmacs
; i
++) {
4493 if (in_commasep_string(s
->maclist
[i
]->name
, str
, len
)) {
4494 s
->scmac_tobe
= s
->maclist
[i
];
4498 ssh_pkt_getstring(pktin
, &str
, &len
); /* client->server compression */
4499 for (i
= 0; i
< lenof(compressions
) + 1; i
++) {
4500 const struct ssh_compress
*c
=
4501 i
== 0 ? s
->preferred_comp
: compressions
[i
- 1];
4502 if (in_commasep_string(c
->name
, str
, len
)) {
4507 ssh_pkt_getstring(pktin
, &str
, &len
); /* server->client compression */
4508 for (i
= 0; i
< lenof(compressions
) + 1; i
++) {
4509 const struct ssh_compress
*c
=
4510 i
== 0 ? s
->preferred_comp
: compressions
[i
- 1];
4511 if (in_commasep_string(c
->name
, str
, len
)) {
4519 * Work out the number of bits of key we will need from the key
4520 * exchange. We start with the maximum key length of either
4526 csbits
= s
->cscipher_tobe
->keylen
;
4527 scbits
= s
->sccipher_tobe
->keylen
;
4528 s
->nbits
= (csbits
> scbits ? csbits
: scbits
);
4530 /* The keys only have 160-bit entropy, since they're based on
4531 * a SHA-1 hash. So cap the key size at 160 bits. */
4536 * If we're doing Diffie-Hellman group exchange, start by
4537 * requesting a group.
4539 if (ssh
->kex
== &ssh_diffiehellman_gex
) {
4540 logevent("Doing Diffie-Hellman group exchange");
4541 ssh
->pkt_ctx
|= SSH2_PKTCTX_DHGEX
;
4543 * Work out how big a DH group we will need to allow that
4546 s
->pbits
= 512 << ((s
->nbits
- 1) / 64);
4547 s
->pktout
= ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST
);
4548 ssh2_pkt_adduint32(s
->pktout
, s
->pbits
);
4549 ssh2_pkt_send_noqueue(ssh
, s
->pktout
);
4552 if (pktin
->type
!= SSH2_MSG_KEX_DH_GEX_GROUP
) {
4553 bombout(("expected key exchange group packet from server"));
4556 s
->p
= ssh2_pkt_getmp(pktin
);
4557 s
->g
= ssh2_pkt_getmp(pktin
);
4558 if (!s
->p
|| !s
->g
) {
4559 bombout(("unable to read mp-ints from incoming group packet"));
4562 ssh
->kex_ctx
= dh_setup_group(s
->p
, s
->g
);
4563 s
->kex_init_value
= SSH2_MSG_KEX_DH_GEX_INIT
;
4564 s
->kex_reply_value
= SSH2_MSG_KEX_DH_GEX_REPLY
;
4566 ssh
->pkt_ctx
|= SSH2_PKTCTX_DHGROUP1
;
4567 ssh
->kex_ctx
= dh_setup_group1();
4568 s
->kex_init_value
= SSH2_MSG_KEXDH_INIT
;
4569 s
->kex_reply_value
= SSH2_MSG_KEXDH_REPLY
;
4572 logevent("Doing Diffie-Hellman key exchange");
4574 * Now generate and send e for Diffie-Hellman.
4576 s
->e
= dh_create_e(ssh
->kex_ctx
, s
->nbits
* 2);
4577 s
->pktout
= ssh2_pkt_init(s
->kex_init_value
);
4578 ssh2_pkt_addmp(s
->pktout
, s
->e
);
4579 ssh2_pkt_send_noqueue(ssh
, s
->pktout
);
4582 if (pktin
->type
!= s
->kex_reply_value
) {
4583 bombout(("expected key exchange reply packet from server"));
4586 ssh_pkt_getstring(pktin
, &s
->hostkeydata
, &s
->hostkeylen
);
4587 s
->f
= ssh2_pkt_getmp(pktin
);
4589 bombout(("unable to parse key exchange reply packet"));
4592 ssh_pkt_getstring(pktin
, &s
->sigdata
, &s
->siglen
);
4594 s
->K
= dh_find_K(ssh
->kex_ctx
, s
->f
);
4596 sha_string(&ssh
->exhash
, s
->hostkeydata
, s
->hostkeylen
);
4597 if (ssh
->kex
== &ssh_diffiehellman_gex
) {
4598 sha_uint32(&ssh
->exhash
, s
->pbits
);
4599 sha_mpint(&ssh
->exhash
, s
->p
);
4600 sha_mpint(&ssh
->exhash
, s
->g
);
4602 sha_mpint(&ssh
->exhash
, s
->e
);
4603 sha_mpint(&ssh
->exhash
, s
->f
);
4604 sha_mpint(&ssh
->exhash
, s
->K
);
4605 SHA_Final(&ssh
->exhash
, s
->exchange_hash
);
4607 dh_cleanup(ssh
->kex_ctx
);
4608 ssh
->kex_ctx
= NULL
;
4611 debug(("Exchange hash is:\n"));
4612 dmemdump(s
->exchange_hash
, 20);
4615 s
->hkey
= ssh
->hostkey
->newkey(s
->hostkeydata
, s
->hostkeylen
);
4617 !ssh
->hostkey
->verifysig(s
->hkey
, s
->sigdata
, s
->siglen
,
4618 (char *)s
->exchange_hash
, 20)) {
4619 bombout(("Server's host key did not match the signature supplied"));
4624 * Authenticate remote host: verify host key. (We've already
4625 * checked the signature of the exchange hash.)
4627 s
->keystr
= ssh
->hostkey
->fmtkey(s
->hkey
);
4628 s
->fingerprint
= ssh
->hostkey
->fingerprint(s
->hkey
);
4629 verify_ssh_host_key(ssh
->frontend
,
4630 ssh
->savedhost
, ssh
->savedport
, ssh
->hostkey
->keytype
,
4631 s
->keystr
, s
->fingerprint
);
4632 if (s
->first_kex
) { /* don't bother logging this in rekeys */
4633 logevent("Host key fingerprint is:");
4634 logevent(s
->fingerprint
);
4636 sfree(s
->fingerprint
);
4638 ssh
->hostkey
->freekey(s
->hkey
);
4641 * Send SSH2_MSG_NEWKEYS.
4643 s
->pktout
= ssh2_pkt_init(SSH2_MSG_NEWKEYS
);
4644 ssh2_pkt_send_noqueue(ssh
, s
->pktout
);
4647 * Now our end of the key exchange is complete, we can send all
4648 * our queued higher-layer packets.
4650 ssh
->queueing
= FALSE
;
4651 ssh2_pkt_queuesend(ssh
);
4654 * Expect SSH2_MSG_NEWKEYS from server.
4657 if (pktin
->type
!= SSH2_MSG_NEWKEYS
) {
4658 bombout(("expected new-keys packet from server"));
4663 * Create and initialise session keys.
4665 if (ssh
->cs_cipher_ctx
)
4666 ssh
->cscipher
->free_context(ssh
->cs_cipher_ctx
);
4667 ssh
->cscipher
= s
->cscipher_tobe
;
4668 ssh
->cs_cipher_ctx
= ssh
->cscipher
->make_context();
4670 if (ssh
->sc_cipher_ctx
)
4671 ssh
->sccipher
->free_context(ssh
->sc_cipher_ctx
);
4672 ssh
->sccipher
= s
->sccipher_tobe
;
4673 ssh
->sc_cipher_ctx
= ssh
->sccipher
->make_context();
4675 if (ssh
->cs_mac_ctx
)
4676 ssh
->csmac
->free_context(ssh
->cs_mac_ctx
);
4677 ssh
->csmac
= s
->csmac_tobe
;
4678 ssh
->cs_mac_ctx
= ssh
->csmac
->make_context();
4680 if (ssh
->sc_mac_ctx
)
4681 ssh
->scmac
->free_context(ssh
->sc_mac_ctx
);
4682 ssh
->scmac
= s
->scmac_tobe
;
4683 ssh
->sc_mac_ctx
= ssh
->scmac
->make_context();
4685 if (ssh
->cs_comp_ctx
)
4686 ssh
->cscomp
->compress_cleanup(ssh
->cs_comp_ctx
);
4687 ssh
->cscomp
= s
->cscomp_tobe
;
4688 ssh
->cs_comp_ctx
= ssh
->cscomp
->compress_init();
4690 if (ssh
->sc_comp_ctx
)
4691 ssh
->sccomp
->decompress_cleanup(ssh
->sc_comp_ctx
);
4692 ssh
->sccomp
= s
->sccomp_tobe
;
4693 ssh
->sc_comp_ctx
= ssh
->sccomp
->decompress_init();
4696 * Set IVs after keys. Here we use the exchange hash from the
4697 * _first_ key exchange.
4700 unsigned char keyspace
[40];
4702 memcpy(ssh
->v2_session_id
, s
->exchange_hash
,
4703 sizeof(s
->exchange_hash
));
4704 ssh2_mkkey(ssh
,s
->K
,s
->exchange_hash
,ssh
->v2_session_id
,'C',keyspace
);
4705 ssh
->cscipher
->setkey(ssh
->cs_cipher_ctx
, keyspace
);
4706 ssh2_mkkey(ssh
,s
->K
,s
->exchange_hash
,ssh
->v2_session_id
,'D',keyspace
);
4707 ssh
->sccipher
->setkey(ssh
->sc_cipher_ctx
, keyspace
);
4708 ssh2_mkkey(ssh
,s
->K
,s
->exchange_hash
,ssh
->v2_session_id
,'A',keyspace
);
4709 ssh
->cscipher
->setiv(ssh
->cs_cipher_ctx
, keyspace
);
4710 ssh2_mkkey(ssh
,s
->K
,s
->exchange_hash
,ssh
->v2_session_id
,'B',keyspace
);
4711 ssh
->sccipher
->setiv(ssh
->sc_cipher_ctx
, keyspace
);
4712 ssh2_mkkey(ssh
,s
->K
,s
->exchange_hash
,ssh
->v2_session_id
,'E',keyspace
);
4713 ssh
->csmac
->setkey(ssh
->cs_mac_ctx
, keyspace
);
4714 ssh2_mkkey(ssh
,s
->K
,s
->exchange_hash
,ssh
->v2_session_id
,'F',keyspace
);
4715 ssh
->scmac
->setkey(ssh
->sc_mac_ctx
, keyspace
);
4717 logeventf(ssh
, "Initialised %.200s client->server encryption",
4718 ssh
->cscipher
->text_name
);
4719 logeventf(ssh
, "Initialised %.200s server->client encryption",
4720 ssh
->sccipher
->text_name
);
4721 logeventf(ssh
, "Initialised %.200s client->server MAC algorithm",
4722 ssh
->csmac
->text_name
);
4723 logeventf(ssh
, "Initialised %.200s server->client MAC algorithm",
4724 ssh
->scmac
->text_name
);
4725 if (ssh
->cscomp
->text_name
)
4726 logeventf(ssh
, "Initialised %s compression",
4727 ssh
->cscomp
->text_name
);
4728 if (ssh
->sccomp
->text_name
)
4729 logeventf(ssh
, "Initialised %s decompression",
4730 ssh
->sccomp
->text_name
);
4733 if (ssh
->kex
== &ssh_diffiehellman_gex
) {
4739 * If this is the first key exchange phase, we must pass the
4740 * SSH2_MSG_NEWKEYS packet to the next layer, not because it
4741 * wants to see it but because it will need time to initialise
4742 * itself before it sees an actual packet. In subsequent key
4743 * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because
4744 * it would only confuse the layer above.
4746 if (!s
->first_kex
) {
4752 * Now we're encrypting. Begin returning 1 to the protocol main
4753 * function so that other things can run on top of the
4754 * transport. If we ever see a KEXINIT, we must go back to the
4757 while (!(pktin
&& pktin
->type
== SSH2_MSG_KEXINIT
)) {
4760 logevent("Server initiated key re-exchange");
4761 goto begin_key_exchange
;
4767 * Add data to an SSH2 channel output buffer.
4769 static void ssh2_add_channel_data(struct ssh_channel
*c
, char *buf
,
4772 bufchain_add(&c
->v
.v2
.outbuffer
, buf
, len
);
4776 * Attempt to send data on an SSH2 channel.
4778 static int ssh2_try_send(struct ssh_channel
*c
)
4781 struct Packet
*pktout
;
4783 while (c
->v
.v2
.remwindow
> 0 && bufchain_size(&c
->v
.v2
.outbuffer
) > 0) {
4786 bufchain_prefix(&c
->v
.v2
.outbuffer
, &data
, &len
);
4787 if ((unsigned)len
> c
->v
.v2
.remwindow
)
4788 len
= c
->v
.v2
.remwindow
;
4789 if ((unsigned)len
> c
->v
.v2
.remmaxpkt
)
4790 len
= c
->v
.v2
.remmaxpkt
;
4791 pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA
);
4792 ssh2_pkt_adduint32(pktout
, c
->remoteid
);
4793 dont_log_data(ssh
, pktout
, PKTLOG_OMIT
);
4794 ssh2_pkt_addstring_start(pktout
);
4795 ssh2_pkt_addstring_data(pktout
, data
, len
);
4796 end_log_omission(ssh
, pktout
);
4797 ssh2_pkt_send(ssh
, pktout
);
4798 bufchain_consume(&c
->v
.v2
.outbuffer
, len
);
4799 c
->v
.v2
.remwindow
-= len
;
4803 * After having sent as much data as we can, return the amount
4806 return bufchain_size(&c
->v
.v2
.outbuffer
);
4810 * Potentially enlarge the window on an SSH2 channel.
4812 static void ssh2_set_window(struct ssh_channel
*c
, unsigned newwin
)
4817 * Never send WINDOW_ADJUST for a channel that the remote side
4818 * already thinks it's closed; there's no point, since it won't
4819 * be sending any more data anyway.
4824 if (newwin
> c
->v
.v2
.locwindow
) {
4825 struct Packet
*pktout
;
4827 pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
4828 ssh2_pkt_adduint32(pktout
, c
->remoteid
);
4829 ssh2_pkt_adduint32(pktout
, newwin
- c
->v
.v2
.locwindow
);
4830 ssh2_pkt_send(ssh
, pktout
);
4831 c
->v
.v2
.locwindow
= newwin
;
4835 void ssh2_msg_channel_window_adjust(Ssh ssh
, struct Packet
*pktin
)
4837 unsigned i
= ssh_pkt_getuint32(pktin
);
4838 struct ssh_channel
*c
;
4839 c
= find234(ssh
->channels
, &i
, ssh_channelfind
);
4840 if (c
&& !c
->closes
)
4841 c
->v
.v2
.remwindow
+= ssh_pkt_getuint32(pktin
);
4845 * Handle the SSH2 userauth and connection layers.
4847 static void do_ssh2_authconn(Ssh ssh
, unsigned char *in
, int inlen
,
4848 struct Packet
*pktin
)
4850 struct do_ssh2_authconn_state
{
4852 AUTH_INVALID
, AUTH_PUBLICKEY_AGENT
, AUTH_PUBLICKEY_FILE
,
4854 AUTH_KEYBOARD_INTERACTIVE
4858 AUTH_TYPE_PUBLICKEY
,
4859 AUTH_TYPE_PUBLICKEY_OFFER_LOUD
,
4860 AUTH_TYPE_PUBLICKEY_OFFER_QUIET
,
4862 AUTH_TYPE_KEYBOARD_INTERACTIVE
,
4863 AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
4865 int gotit
, need_pw
, can_pubkey
, can_passwd
, can_keyb_inter
;
4866 int tried_pubkey_config
, tried_agent
, tried_keyb_inter
;
4867 int kbd_inter_running
;
4869 int num_prompts
, curr_prompt
, echo
;
4874 void *publickey_blob
;
4875 int publickey_bloblen
;
4876 unsigned char request
[5], *response
, *p
;
4880 char *pkblob
, *alg
, *commentp
;
4881 int pklen
, alglen
, commentlen
;
4882 int siglen
, retlen
, len
;
4883 char *q
, *agentreq
, *ret
;
4885 int num_env
, env_left
, env_ok
;
4886 struct Packet
*pktout
;
4888 crState(do_ssh2_authconn_state
);
4890 crBegin(ssh
->do_ssh2_authconn_crstate
);
4893 * Request userauth protocol, and await a response to it.
4895 s
->pktout
= ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST
);
4896 ssh2_pkt_addstring(s
->pktout
, "ssh-userauth");
4897 ssh2_pkt_send(ssh
, s
->pktout
);
4898 crWaitUntilV(pktin
);
4899 if (pktin
->type
!= SSH2_MSG_SERVICE_ACCEPT
) {
4900 bombout(("Server refused user authentication protocol"));
4905 * We repeat this whole loop, including the username prompt,
4906 * until we manage a successful authentication. If the user
4907 * types the wrong _password_, they can be sent back to the
4908 * beginning to try another username, if this is configured on.
4909 * (If they specify a username in the config, they are never
4910 * asked, even if they do give a wrong password.)
4912 * I think this best serves the needs of
4914 * - the people who have no configuration, no keys, and just
4915 * want to try repeated (username,password) pairs until they
4916 * type both correctly
4918 * - people who have keys and configuration but occasionally
4919 * need to fall back to passwords
4921 * - people with a key held in Pageant, who might not have
4922 * logged in to a particular machine before; so they want to
4923 * type a username, and then _either_ their key will be
4924 * accepted, _or_ they will type a password. If they mistype
4925 * the username they will want to be able to get back and
4928 s
->username
[0] = '\0';
4929 s
->got_username
= FALSE
;
4934 if (s
->got_username
&& !ssh
->cfg
.change_username
) {
4936 * We got a username last time round this loop, and
4937 * with change_username turned off we don't try to get
4940 } else if (!*ssh
->cfg
.username
) {
4941 if (ssh_get_line
&& !ssh_getline_pw_only
) {
4942 if (!ssh_get_line("login as: ",
4943 s
->username
, sizeof(s
->username
), FALSE
)) {
4945 * get_line failed to get a username.
4948 logevent("No username provided. Abandoning session.");
4949 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
4953 int ret
; /* need not be saved across crReturn */
4954 c_write_str(ssh
, "login as: ");
4956 setup_userpass_input(ssh
, s
->username
, sizeof(s
->username
), 1);
4958 crWaitUntilV(!pktin
);
4959 ret
= process_userpass_input(ssh
, in
, inlen
);
4963 c_write_str(ssh
, "\r\n");
4965 s
->username
[strcspn(s
->username
, "\n\r")] = '\0';
4968 strncpy(s
->username
, ssh
->cfg
.username
, sizeof(s
->username
));
4969 s
->username
[sizeof(s
->username
)-1] = '\0';
4970 if ((flags
& FLAG_VERBOSE
) || (flags
& FLAG_INTERACTIVE
)) {
4971 stuff
= dupprintf("Using username \"%s\".\r\n", s
->username
);
4972 c_write_str(ssh
, stuff
);
4976 s
->got_username
= TRUE
;
4979 * Send an authentication request using method "none": (a)
4980 * just in case it succeeds, and (b) so that we know what
4981 * authentication methods we can usefully try next.
4983 ssh
->pkt_ctx
&= ~SSH2_PKTCTX_AUTH_MASK
;
4985 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
4986 ssh2_pkt_addstring(s
->pktout
, s
->username
);
4987 ssh2_pkt_addstring(s
->pktout
, "ssh-connection");/* service requested */
4988 ssh2_pkt_addstring(s
->pktout
, "none"); /* method */
4989 ssh2_pkt_send(ssh
, s
->pktout
);
4990 s
->type
= AUTH_TYPE_NONE
;
4992 s
->we_are_in
= FALSE
;
4994 s
->tried_pubkey_config
= FALSE
;
4995 s
->tried_agent
= FALSE
;
4996 s
->tried_keyb_inter
= FALSE
;
4997 s
->kbd_inter_running
= FALSE
;
4998 /* Load the pub half of ssh->cfg.keyfile so we notice if it's in Pageant */
4999 if (!filename_is_null(ssh
->cfg
.keyfile
)) {
5001 logeventf(ssh
, "Reading private key file \"%.150s\"",
5002 filename_to_str(&ssh
->cfg
.keyfile
));
5003 keytype
= key_type(&ssh
->cfg
.keyfile
);
5004 if (keytype
== SSH_KEYTYPE_SSH2
) {
5006 ssh2_userkey_loadpub(&ssh
->cfg
.keyfile
, NULL
,
5007 &s
->publickey_bloblen
, NULL
);
5010 logeventf(ssh
, "Unable to use this key file (%s)",
5011 key_type_to_str(keytype
));
5012 msgbuf
= dupprintf("Unable to use key file \"%.150s\""
5014 filename_to_str(&ssh
->cfg
.keyfile
),
5015 key_type_to_str(keytype
));
5016 c_write_str(ssh
, msgbuf
);
5018 s
->publickey_blob
= NULL
;
5021 s
->publickey_blob
= NULL
;
5025 * Wait for the result of the last authentication request.
5028 crWaitUntilV(pktin
);
5029 while (pktin
->type
== SSH2_MSG_USERAUTH_BANNER
) {
5033 * Don't show the banner if we're operating in
5034 * non-verbose non-interactive mode. (It's probably
5035 * a script, which means nobody will read the
5036 * banner _anyway_, and moreover the printing of
5037 * the banner will screw up processing on the
5038 * output of (say) plink.)
5040 if (flags
& (FLAG_VERBOSE
| FLAG_INTERACTIVE
)) {
5041 ssh_pkt_getstring(pktin
, &banner
, &size
);
5043 c_write_untrusted(ssh
, banner
, size
);
5045 crWaitUntilV(pktin
);
5047 if (pktin
->type
== SSH2_MSG_USERAUTH_SUCCESS
) {
5048 logevent("Access granted");
5049 s
->we_are_in
= TRUE
;
5053 if (s
->kbd_inter_running
&&
5054 pktin
->type
== SSH2_MSG_USERAUTH_INFO_REQUEST
) {
5056 * This is either a further set-of-prompts packet
5057 * in keyboard-interactive authentication, or it's
5058 * the same one and we came back here with `gotit'
5059 * set. In the former case, we must reset the
5060 * curr_prompt variable.
5064 } else if (pktin
->type
!= SSH2_MSG_USERAUTH_FAILURE
) {
5065 bombout(("Strange packet received during authentication: type %d",
5073 * OK, we're now sitting on a USERAUTH_FAILURE message, so
5074 * we can look at the string in it and know what we can
5075 * helpfully try next.
5077 if (pktin
->type
== SSH2_MSG_USERAUTH_FAILURE
) {
5080 ssh_pkt_getstring(pktin
, &methods
, &methlen
);
5081 s
->kbd_inter_running
= FALSE
;
5082 if (!ssh2_pkt_getbool(pktin
)) {
5084 * We have received an unequivocal Access
5085 * Denied. This can translate to a variety of
5088 * - if we'd just tried "none" authentication,
5089 * it's not worth printing anything at all
5091 * - if we'd just tried a public key _offer_,
5092 * the message should be "Server refused our
5093 * key" (or no message at all if the key
5094 * came from Pageant)
5096 * - if we'd just tried anything else, the
5097 * message really should be "Access denied".
5099 * Additionally, if we'd just tried password
5100 * authentication, we should break out of this
5101 * whole loop so as to go back to the username
5102 * prompt (iff we're configured to allow
5103 * username change attempts).
5105 if (s
->type
== AUTH_TYPE_NONE
) {
5107 } else if (s
->type
== AUTH_TYPE_PUBLICKEY_OFFER_LOUD
||
5108 s
->type
== AUTH_TYPE_PUBLICKEY_OFFER_QUIET
) {
5109 if (s
->type
== AUTH_TYPE_PUBLICKEY_OFFER_LOUD
)
5110 c_write_str(ssh
, "Server refused our key\r\n");
5111 logevent("Server refused public key");
5112 } else if (s
->type
==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
) {
5113 /* server declined keyboard-interactive; ignore */
5115 c_write_str(ssh
, "Access denied\r\n");
5116 logevent("Access denied");
5117 if (s
->type
== AUTH_TYPE_PASSWORD
&&
5118 ssh
->cfg
.change_username
) {
5119 /* XXX perhaps we should allow
5120 * keyboard-interactive to do this too? */
5121 s
->we_are_in
= FALSE
;
5126 c_write_str(ssh
, "Further authentication required\r\n");
5127 logevent("Further authentication required");
5131 in_commasep_string("publickey", methods
, methlen
);
5133 in_commasep_string("password", methods
, methlen
);
5134 s
->can_keyb_inter
= ssh
->cfg
.try_ki_auth
&&
5135 in_commasep_string("keyboard-interactive", methods
, methlen
);
5139 ssh
->pkt_ctx
&= ~SSH2_PKTCTX_AUTH_MASK
;
5143 * Most password/passphrase prompts will be
5144 * non-echoing, so we set this to 0 by default.
5145 * Exception is that some keyboard-interactive prompts
5146 * can be echoing, in which case we'll set this to 1.
5150 if (!s
->method
&& s
->can_pubkey
&&
5151 agent_exists() && !s
->tried_agent
) {
5153 * Attempt public-key authentication using Pageant.
5158 ssh
->pkt_ctx
&= ~SSH2_PKTCTX_AUTH_MASK
;
5159 ssh
->pkt_ctx
|= SSH2_PKTCTX_PUBLICKEY
;
5161 s
->tried_agent
= TRUE
;
5163 logevent("Pageant is running. Requesting keys.");
5165 /* Request the keys held by the agent. */
5166 PUT_32BIT(s
->request
, 1);
5167 s
->request
[4] = SSH2_AGENTC_REQUEST_IDENTITIES
;
5168 if (!agent_query(s
->request
, 5, &r
, &s
->responselen
,
5169 ssh_agent_callback
, ssh
)) {
5173 bombout(("Unexpected data from server while"
5174 " waiting for agent response"));
5177 } while (pktin
|| inlen
> 0);
5178 r
= ssh
->agent_response
;
5179 s
->responselen
= ssh
->agent_response_len
;
5181 s
->response
= (unsigned char *) r
;
5182 if (s
->response
&& s
->responselen
>= 5 &&
5183 s
->response
[4] == SSH2_AGENT_IDENTITIES_ANSWER
) {
5184 s
->p
= s
->response
+ 5;
5185 s
->nkeys
= GET_32BIT(s
->p
);
5189 sprintf(buf
, "Pageant has %d SSH2 keys", s
->nkeys
);
5192 for (s
->keyi
= 0; s
->keyi
< s
->nkeys
; s
->keyi
++) {
5197 sprintf(buf
, "Trying Pageant key #%d", s
->keyi
);
5200 s
->pklen
= GET_32BIT(s
->p
);
5202 if (s
->publickey_blob
&&
5203 s
->pklen
== s
->publickey_bloblen
&&
5204 !memcmp(s
->p
, s
->publickey_blob
,
5205 s
->publickey_bloblen
)) {
5206 logevent("This key matches configured key file");
5207 s
->tried_pubkey_config
= 1;
5209 s
->pkblob
= (char *)s
->p
;
5211 s
->alglen
= GET_32BIT(s
->pkblob
);
5212 s
->alg
= s
->pkblob
+ 4;
5213 s
->commentlen
= GET_32BIT(s
->p
);
5215 s
->commentp
= (char *)s
->p
;
5216 s
->p
+= s
->commentlen
;
5217 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
5218 ssh2_pkt_addstring(s
->pktout
, s
->username
);
5219 ssh2_pkt_addstring(s
->pktout
, "ssh-connection"); /* service requested */
5220 ssh2_pkt_addstring(s
->pktout
, "publickey"); /* method */
5221 ssh2_pkt_addbool(s
->pktout
, FALSE
); /* no signature included */
5222 ssh2_pkt_addstring_start(s
->pktout
);
5223 ssh2_pkt_addstring_data(s
->pktout
, s
->alg
, s
->alglen
);
5224 ssh2_pkt_addstring_start(s
->pktout
);
5225 ssh2_pkt_addstring_data(s
->pktout
, s
->pkblob
, s
->pklen
);
5226 ssh2_pkt_send(ssh
, s
->pktout
);
5228 crWaitUntilV(pktin
);
5229 if (pktin
->type
!= SSH2_MSG_USERAUTH_PK_OK
) {
5230 logevent("Key refused");
5234 if (flags
& FLAG_VERBOSE
) {
5235 c_write_str(ssh
, "Authenticating with "
5237 c_write(ssh
, s
->commentp
, s
->commentlen
);
5238 c_write_str(ssh
, "\" from agent\r\n");
5242 * Server is willing to accept the key.
5243 * Construct a SIGN_REQUEST.
5245 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
5246 ssh2_pkt_addstring(s
->pktout
, s
->username
);
5247 ssh2_pkt_addstring(s
->pktout
, "ssh-connection"); /* service requested */
5248 ssh2_pkt_addstring(s
->pktout
, "publickey"); /* method */
5249 ssh2_pkt_addbool(s
->pktout
, TRUE
);
5250 ssh2_pkt_addstring_start(s
->pktout
);
5251 ssh2_pkt_addstring_data(s
->pktout
, s
->alg
, s
->alglen
);
5252 ssh2_pkt_addstring_start(s
->pktout
);
5253 ssh2_pkt_addstring_data(s
->pktout
, s
->pkblob
, s
->pklen
);
5255 s
->siglen
= s
->pktout
->length
- 5 + 4 + 20;
5256 if (ssh
->remote_bugs
& BUG_SSH2_PK_SESSIONID
)
5258 s
->len
= 1; /* message type */
5259 s
->len
+= 4 + s
->pklen
; /* key blob */
5260 s
->len
+= 4 + s
->siglen
; /* data to sign */
5261 s
->len
+= 4; /* flags */
5262 s
->agentreq
= snewn(4 + s
->len
, char);
5263 PUT_32BIT(s
->agentreq
, s
->len
);
5264 s
->q
= s
->agentreq
+ 4;
5265 *s
->q
++ = SSH2_AGENTC_SIGN_REQUEST
;
5266 PUT_32BIT(s
->q
, s
->pklen
);
5268 memcpy(s
->q
, s
->pkblob
, s
->pklen
);
5270 PUT_32BIT(s
->q
, s
->siglen
);
5272 /* Now the data to be signed... */
5273 if (!(ssh
->remote_bugs
& BUG_SSH2_PK_SESSIONID
)) {
5274 PUT_32BIT(s
->q
, 20);
5277 memcpy(s
->q
, ssh
->v2_session_id
, 20);
5279 memcpy(s
->q
, s
->pktout
->data
+ 5,
5280 s
->pktout
->length
- 5);
5281 s
->q
+= s
->pktout
->length
- 5;
5282 /* And finally the (zero) flags word. */
5284 if (!agent_query(s
->agentreq
, s
->len
+ 4,
5286 ssh_agent_callback
, ssh
)) {
5290 bombout(("Unexpected data from server"
5291 " while waiting for agent"
5295 } while (pktin
|| inlen
> 0);
5296 vret
= ssh
->agent_response
;
5297 s
->retlen
= ssh
->agent_response_len
;
5302 if (s
->ret
[4] == SSH2_AGENT_SIGN_RESPONSE
) {
5303 logevent("Sending Pageant's response");
5304 ssh2_add_sigblob(ssh
, s
->pktout
,
5305 s
->pkblob
, s
->pklen
,
5307 GET_32BIT(s
->ret
+ 5));
5308 ssh2_pkt_send(ssh
, s
->pktout
);
5313 ("Pageant failed to answer challenge");
5324 if (!s
->method
&& s
->can_pubkey
&& s
->publickey_blob
5325 && !s
->tried_pubkey_config
) {
5326 unsigned char *pub_blob
;
5327 char *algorithm
, *comment
;
5330 s
->tried_pubkey_config
= TRUE
;
5332 ssh
->pkt_ctx
&= ~SSH2_PKTCTX_AUTH_MASK
;
5333 ssh
->pkt_ctx
|= SSH2_PKTCTX_PUBLICKEY
;
5336 * Try the public key supplied in the configuration.
5338 * First, offer the public blob to see if the server is
5339 * willing to accept it.
5342 (unsigned char *)ssh2_userkey_loadpub(&ssh
->cfg
.keyfile
,
5347 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
5348 ssh2_pkt_addstring(s
->pktout
, s
->username
);
5349 ssh2_pkt_addstring(s
->pktout
, "ssh-connection"); /* service requested */
5350 ssh2_pkt_addstring(s
->pktout
, "publickey"); /* method */
5351 ssh2_pkt_addbool(s
->pktout
, FALSE
); /* no signature included */
5352 ssh2_pkt_addstring(s
->pktout
, algorithm
);
5353 ssh2_pkt_addstring_start(s
->pktout
);
5354 ssh2_pkt_addstring_data(s
->pktout
, (char *)pub_blob
,
5356 ssh2_pkt_send(ssh
, s
->pktout
);
5357 logevent("Offered public key");
5359 crWaitUntilV(pktin
);
5360 if (pktin
->type
!= SSH2_MSG_USERAUTH_PK_OK
) {
5362 s
->type
= AUTH_TYPE_PUBLICKEY_OFFER_LOUD
;
5363 continue; /* key refused; give up on it */
5366 logevent("Offer of public key accepted");
5368 * Actually attempt a serious authentication using
5371 if (ssh2_userkey_encrypted(&ssh
->cfg
.keyfile
, &comment
)) {
5372 sprintf(s
->pwprompt
,
5373 "Passphrase for key \"%.100s\": ",
5379 if (flags
& FLAG_VERBOSE
) {
5380 c_write_str(ssh
, "Authenticating with public key \"");
5381 c_write_str(ssh
, comment
);
5382 c_write_str(ssh
, "\"\r\n");
5384 s
->method
= AUTH_PUBLICKEY_FILE
;
5388 if (!s
->method
&& s
->can_keyb_inter
&& !s
->tried_keyb_inter
) {
5389 s
->method
= AUTH_KEYBOARD_INTERACTIVE
;
5390 s
->type
= AUTH_TYPE_KEYBOARD_INTERACTIVE
;
5391 s
->tried_keyb_inter
= TRUE
;
5393 ssh
->pkt_ctx
&= ~SSH2_PKTCTX_AUTH_MASK
;
5394 ssh
->pkt_ctx
|= SSH2_PKTCTX_KBDINTER
;
5396 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
5397 ssh2_pkt_addstring(s
->pktout
, s
->username
);
5398 ssh2_pkt_addstring(s
->pktout
, "ssh-connection"); /* service requested */
5399 ssh2_pkt_addstring(s
->pktout
, "keyboard-interactive"); /* method */
5400 ssh2_pkt_addstring(s
->pktout
, ""); /* lang */
5401 ssh2_pkt_addstring(s
->pktout
, "");
5402 ssh2_pkt_send(ssh
, s
->pktout
);
5404 crWaitUntilV(pktin
);
5405 if (pktin
->type
!= SSH2_MSG_USERAUTH_INFO_REQUEST
) {
5406 if (pktin
->type
== SSH2_MSG_USERAUTH_FAILURE
)
5408 logevent("Keyboard-interactive authentication refused");
5409 s
->type
= AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
;
5413 s
->kbd_inter_running
= TRUE
;
5417 if (s
->kbd_inter_running
) {
5418 s
->method
= AUTH_KEYBOARD_INTERACTIVE
;
5419 s
->type
= AUTH_TYPE_KEYBOARD_INTERACTIVE
;
5420 s
->tried_keyb_inter
= TRUE
;
5422 ssh
->pkt_ctx
&= ~SSH2_PKTCTX_AUTH_MASK
;
5423 ssh
->pkt_ctx
|= SSH2_PKTCTX_KBDINTER
;
5425 if (s
->curr_prompt
== 0) {
5427 * We've got a fresh USERAUTH_INFO_REQUEST.
5428 * Display header data, and start going through
5431 char *name
, *inst
, *lang
;
5432 int name_len
, inst_len
, lang_len
;
5434 ssh_pkt_getstring(pktin
, &name
, &name_len
);
5435 ssh_pkt_getstring(pktin
, &inst
, &inst_len
);
5436 ssh_pkt_getstring(pktin
, &lang
, &lang_len
);
5438 c_write_untrusted(ssh
, name
, name_len
);
5439 c_write_str(ssh
, "\r\n");
5442 c_write_untrusted(ssh
, inst
, inst_len
);
5443 c_write_str(ssh
, "\r\n");
5445 s
->num_prompts
= ssh_pkt_getuint32(pktin
);
5449 * If there are prompts remaining in the packet,
5450 * display one and get a response.
5452 if (s
->curr_prompt
< s
->num_prompts
) {
5456 ssh_pkt_getstring(pktin
, &prompt
, &prompt_len
);
5457 if (prompt_len
> 0) {
5458 static const char trunc
[] = "<prompt truncated>: ";
5459 static const int prlen
= sizeof(s
->pwprompt
) -
5461 if (prompt_len
> prlen
) {
5462 memcpy(s
->pwprompt
, prompt
, prlen
);
5463 strcpy(s
->pwprompt
+ prlen
, trunc
);
5465 memcpy(s
->pwprompt
, prompt
, prompt_len
);
5466 s
->pwprompt
[prompt_len
] = '\0';
5470 "<server failed to send prompt>: ");
5472 s
->echo
= ssh2_pkt_getbool(pktin
);
5478 if (!s
->method
&& s
->can_passwd
) {
5479 s
->method
= AUTH_PASSWORD
;
5480 ssh
->pkt_ctx
&= ~SSH2_PKTCTX_AUTH_MASK
;
5481 ssh
->pkt_ctx
|= SSH2_PKTCTX_PASSWORD
;
5482 sprintf(s
->pwprompt
, "%.90s@%.90s's password: ", s
->username
,
5489 if (!ssh_get_line(s
->pwprompt
, s
->password
,
5490 sizeof(s
->password
), TRUE
)) {
5492 * get_line failed to get a password (for
5493 * example because one was supplied on the
5494 * command line which has already failed to
5497 s
->pktout
= ssh2_pkt_init(SSH2_MSG_DISCONNECT
);
5498 ssh2_pkt_adduint32(s
->pktout
,SSH2_DISCONNECT_BY_APPLICATION
);
5499 ssh2_pkt_addstring(s
->pktout
, "No more passwords available"
5501 ssh2_pkt_addstring(s
->pktout
, "en"); /* language tag */
5502 ssh2_pkt_send_noqueue(ssh
, s
->pktout
);
5503 logevent("Unable to authenticate");
5504 connection_fatal(ssh
->frontend
,
5505 "Unable to authenticate");
5506 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
5510 int ret
; /* need not be saved across crReturn */
5511 c_write_untrusted(ssh
, s
->pwprompt
, strlen(s
->pwprompt
));
5514 setup_userpass_input(ssh
, s
->password
,
5515 sizeof(s
->password
), s
->echo
);
5517 crWaitUntilV(!pktin
);
5518 ret
= process_userpass_input(ssh
, in
, inlen
);
5522 c_write_str(ssh
, "\r\n");
5526 if (s
->method
== AUTH_PUBLICKEY_FILE
) {
5528 * We have our passphrase. Now try the actual authentication.
5530 struct ssh2_userkey
*key
;
5531 const char *error
= NULL
;
5533 key
= ssh2_load_userkey(&ssh
->cfg
.keyfile
, s
->password
,
5535 if (key
== SSH2_WRONG_PASSPHRASE
|| key
== NULL
) {
5536 if (key
== SSH2_WRONG_PASSPHRASE
) {
5537 c_write_str(ssh
, "Wrong passphrase\r\n");
5538 s
->tried_pubkey_config
= FALSE
;
5540 c_write_str(ssh
, "Unable to load private key (");
5541 c_write_str(ssh
, error
);
5542 c_write_str(ssh
, ")\r\n");
5543 s
->tried_pubkey_config
= TRUE
;
5545 /* Send a spurious AUTH_NONE to return to the top. */
5546 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
5547 ssh2_pkt_addstring(s
->pktout
, s
->username
);
5548 ssh2_pkt_addstring(s
->pktout
, "ssh-connection"); /* service requested */
5549 ssh2_pkt_addstring(s
->pktout
, "none"); /* method */
5550 ssh2_pkt_send(ssh
, s
->pktout
);
5551 s
->type
= AUTH_TYPE_NONE
;
5553 unsigned char *pkblob
, *sigblob
, *sigdata
;
5554 int pkblob_len
, sigblob_len
, sigdata_len
;
5558 * We have loaded the private key and the server
5559 * has announced that it's willing to accept it.
5560 * Hallelujah. Generate a signature and send it.
5562 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
5563 ssh2_pkt_addstring(s
->pktout
, s
->username
);
5564 ssh2_pkt_addstring(s
->pktout
, "ssh-connection"); /* service requested */
5565 ssh2_pkt_addstring(s
->pktout
, "publickey"); /* method */
5566 ssh2_pkt_addbool(s
->pktout
, TRUE
);
5567 ssh2_pkt_addstring(s
->pktout
, key
->alg
->name
);
5568 pkblob
= key
->alg
->public_blob(key
->data
, &pkblob_len
);
5569 ssh2_pkt_addstring_start(s
->pktout
);
5570 ssh2_pkt_addstring_data(s
->pktout
, (char *)pkblob
, pkblob_len
);
5573 * The data to be signed is:
5577 * followed by everything so far placed in the
5580 sigdata_len
= s
->pktout
->length
- 5 + 4 + 20;
5581 if (ssh
->remote_bugs
& BUG_SSH2_PK_SESSIONID
)
5583 sigdata
= snewn(sigdata_len
, unsigned char);
5585 if (!(ssh
->remote_bugs
& BUG_SSH2_PK_SESSIONID
)) {
5586 PUT_32BIT(sigdata
+p
, 20);
5589 memcpy(sigdata
+p
, ssh
->v2_session_id
, 20); p
+= 20;
5590 memcpy(sigdata
+p
, s
->pktout
->data
+ 5,
5591 s
->pktout
->length
- 5);
5592 p
+= s
->pktout
->length
- 5;
5593 assert(p
== sigdata_len
);
5594 sigblob
= key
->alg
->sign(key
->data
, (char *)sigdata
,
5595 sigdata_len
, &sigblob_len
);
5596 ssh2_add_sigblob(ssh
, s
->pktout
, pkblob
, pkblob_len
,
5597 sigblob
, sigblob_len
);
5602 ssh2_pkt_send(ssh
, s
->pktout
);
5603 s
->type
= AUTH_TYPE_PUBLICKEY
;
5604 key
->alg
->freekey(key
->data
);
5606 } else if (s
->method
== AUTH_PASSWORD
) {
5608 * We send the password packet lumped tightly together with
5609 * an SSH_MSG_IGNORE packet. The IGNORE packet contains a
5610 * string long enough to make the total length of the two
5611 * packets constant. This should ensure that a passive
5612 * listener doing traffic analyis can't work out the length
5615 * For this to work, we need an assumption about the
5616 * maximum length of the password packet. I think 256 is
5617 * pretty conservative. Anyone using a password longer than
5618 * that probably doesn't have much to worry about from
5619 * people who find out how long their password is!
5621 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
5622 ssh2_pkt_addstring(s
->pktout
, s
->username
);
5623 ssh2_pkt_addstring(s
->pktout
, "ssh-connection"); /* service requested */
5624 ssh2_pkt_addstring(s
->pktout
, "password");
5625 ssh2_pkt_addbool(s
->pktout
, FALSE
);
5626 dont_log_password(ssh
, s
->pktout
, PKTLOG_BLANK
);
5627 ssh2_pkt_addstring(s
->pktout
, s
->password
);
5628 memset(s
->password
, 0, sizeof(s
->password
));
5629 end_log_omission(ssh
, s
->pktout
);
5630 ssh2_pkt_defer(ssh
, s
->pktout
);
5632 * We'll include a string that's an exact multiple of the
5633 * cipher block size. If the cipher is NULL for some
5634 * reason, we don't do this trick at all because we gain
5637 if (ssh
->cscipher
) {
5640 stringlen
= (256 - ssh
->deferred_len
);
5641 stringlen
+= ssh
->cscipher
->blksize
- 1;
5642 stringlen
-= (stringlen
% ssh
->cscipher
->blksize
);
5645 * Temporarily disable actual compression,
5646 * so we can guarantee to get this string
5647 * exactly the length we want it. The
5648 * compression-disabling routine should
5649 * return an integer indicating how many
5650 * bytes we should adjust our string length
5654 ssh
->cscomp
->disable_compression(ssh
->cs_comp_ctx
);
5656 s
->pktout
= ssh2_pkt_init(SSH2_MSG_IGNORE
);
5657 ssh2_pkt_addstring_start(s
->pktout
);
5658 for (i
= 0; i
< stringlen
; i
++) {
5659 char c
= (char) random_byte();
5660 ssh2_pkt_addstring_data(s
->pktout
, &c
, 1);
5662 ssh2_pkt_defer(ssh
, s
->pktout
);
5664 ssh_pkt_defersend(ssh
);
5665 logevent("Sent password");
5666 s
->type
= AUTH_TYPE_PASSWORD
;
5667 } else if (s
->method
== AUTH_KEYBOARD_INTERACTIVE
) {
5668 if (s
->curr_prompt
== 0) {
5669 s
->pktout
= ssh2_pkt_init(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
5670 ssh2_pkt_adduint32(s
->pktout
, s
->num_prompts
);
5672 if (s
->need_pw
) { /* only add pw if we just got one! */
5673 dont_log_password(ssh
, s
->pktout
, PKTLOG_BLANK
);
5674 ssh2_pkt_addstring(s
->pktout
, s
->password
);
5675 memset(s
->password
, 0, sizeof(s
->password
));
5676 end_log_omission(ssh
, s
->pktout
);
5679 if (s
->curr_prompt
>= s
->num_prompts
) {
5680 ssh2_pkt_send(ssh
, s
->pktout
);
5683 * If there are prompts remaining, we set
5684 * `gotit' so that we won't attempt to get
5685 * another packet. Then we go back round the
5686 * loop and will end up retrieving another
5687 * prompt out of the existing packet. Funky or
5692 s
->type
= AUTH_TYPE_KEYBOARD_INTERACTIVE
;
5694 c_write_str(ssh
, "No supported authentication methods"
5695 " left to try!\r\n");
5696 logevent("No supported authentications offered."
5698 s
->pktout
= ssh2_pkt_init(SSH2_MSG_DISCONNECT
);
5699 ssh2_pkt_adduint32(s
->pktout
, SSH2_DISCONNECT_BY_APPLICATION
);
5700 ssh2_pkt_addstring(s
->pktout
, "No supported authentication"
5701 " methods available");
5702 ssh2_pkt_addstring(s
->pktout
, "en"); /* language tag */
5703 ssh2_pkt_send_noqueue(ssh
, s
->pktout
);
5704 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
5708 } while (!s
->we_are_in
);
5711 * Now we're authenticated for the connection protocol. The
5712 * connection protocol will automatically have started at this
5713 * point; there's no need to send SERVICE_REQUEST.
5716 ssh
->channels
= newtree234(ssh_channelcmp
);
5719 * Set up handlers for some connection protocol messages, so we
5720 * don't have to handle them repeatedly in this coroutine.
5722 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_WINDOW_ADJUST
] =
5723 ssh2_msg_channel_window_adjust
;
5726 * Create the main session channel.
5728 if (!ssh
->cfg
.ssh_no_shell
) {
5729 ssh
->mainchan
= snew(struct ssh_channel
);
5730 ssh
->mainchan
->ssh
= ssh
;
5731 ssh
->mainchan
->localid
= alloc_channel_id(ssh
);
5732 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN
);
5733 ssh2_pkt_addstring(s
->pktout
, "session");
5734 ssh2_pkt_adduint32(s
->pktout
, ssh
->mainchan
->localid
);
5735 ssh
->mainchan
->v
.v2
.locwindow
= OUR_V2_WINSIZE
;
5736 ssh2_pkt_adduint32(s
->pktout
, ssh
->mainchan
->v
.v2
.locwindow
);/* our window size */
5737 ssh2_pkt_adduint32(s
->pktout
, 0x4000UL
); /* our max pkt size */
5738 ssh2_pkt_send(ssh
, s
->pktout
);
5739 crWaitUntilV(pktin
);
5740 if (pktin
->type
!= SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
) {
5741 bombout(("Server refused to open a session"));
5743 /* FIXME: error data comes back in FAILURE packet */
5745 if (ssh_pkt_getuint32(pktin
) != ssh
->mainchan
->localid
) {
5746 bombout(("Server's channel confirmation cited wrong channel"));
5749 ssh
->mainchan
->remoteid
= ssh_pkt_getuint32(pktin
);
5750 ssh
->mainchan
->type
= CHAN_MAINSESSION
;
5751 ssh
->mainchan
->closes
= 0;
5752 ssh
->mainchan
->v
.v2
.remwindow
= ssh_pkt_getuint32(pktin
);
5753 ssh
->mainchan
->v
.v2
.remmaxpkt
= ssh_pkt_getuint32(pktin
);
5754 bufchain_init(&ssh
->mainchan
->v
.v2
.outbuffer
);
5755 add234(ssh
->channels
, ssh
->mainchan
);
5756 update_specials_menu(ssh
->frontend
);
5757 logevent("Opened channel for session");
5759 ssh
->mainchan
= NULL
;
5762 * Potentially enable X11 forwarding.
5764 if (ssh
->mainchan
&& ssh
->cfg
.x11_forward
) {
5765 char proto
[20], data
[64];
5766 logevent("Requesting X11 forwarding");
5767 ssh
->x11auth
= x11_invent_auth(proto
, sizeof(proto
),
5768 data
, sizeof(data
), ssh
->cfg
.x11_auth
);
5769 x11_get_real_auth(ssh
->x11auth
, ssh
->cfg
.x11_display
);
5770 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
5771 ssh2_pkt_adduint32(s
->pktout
, ssh
->mainchan
->remoteid
);
5772 ssh2_pkt_addstring(s
->pktout
, "x11-req");
5773 ssh2_pkt_addbool(s
->pktout
, 1); /* want reply */
5774 ssh2_pkt_addbool(s
->pktout
, 0); /* many connections */
5775 ssh2_pkt_addstring(s
->pktout
, proto
);
5776 ssh2_pkt_addstring(s
->pktout
, data
);
5777 ssh2_pkt_adduint32(s
->pktout
, x11_get_screen_number(ssh
->cfg
.x11_display
));
5778 ssh2_pkt_send(ssh
, s
->pktout
);
5780 crWaitUntilV(pktin
);
5782 if (pktin
->type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
5783 if (pktin
->type
!= SSH2_MSG_CHANNEL_FAILURE
) {
5784 bombout(("Unexpected response to X11 forwarding request:"
5785 " packet type %d", pktin
->type
));
5788 logevent("X11 forwarding refused");
5790 logevent("X11 forwarding enabled");
5791 ssh
->X11_fwd_enabled
= TRUE
;
5796 * Enable port forwardings.
5801 int sport
,dport
,sserv
,dserv
;
5802 char sports
[256], dports
[256], saddr
[256], host
[256];
5804 ssh
->rportfwds
= newtree234(ssh_rportcmp_ssh2
);
5805 /* Add port forwardings. */
5806 ssh
->portfwd_strptr
= ssh
->cfg
.portfwd
;
5807 while (*ssh
->portfwd_strptr
) {
5808 type
= *ssh
->portfwd_strptr
++;
5811 while (*ssh
->portfwd_strptr
&& *ssh
->portfwd_strptr
!= '\t') {
5812 if (*ssh
->portfwd_strptr
== ':') {
5814 * We've seen a colon in the middle of the
5815 * source port number. This means that
5816 * everything we've seen until now is the
5817 * source _address_, so we'll move it into
5818 * saddr and start sports from the beginning
5821 ssh
->portfwd_strptr
++;
5823 strcpy(saddr
, sports
);
5826 if (n
< 255) sports
[n
++] = *ssh
->portfwd_strptr
++;
5830 if (*ssh
->portfwd_strptr
== '\t')
5831 ssh
->portfwd_strptr
++;
5833 while (*ssh
->portfwd_strptr
&& *ssh
->portfwd_strptr
!= ':') {
5834 if (n
< 255) host
[n
++] = *ssh
->portfwd_strptr
++;
5837 if (*ssh
->portfwd_strptr
== ':')
5838 ssh
->portfwd_strptr
++;
5840 while (*ssh
->portfwd_strptr
) {
5841 if (n
< 255) dports
[n
++] = *ssh
->portfwd_strptr
++;
5844 ssh
->portfwd_strptr
++;
5845 dport
= atoi(dports
);
5849 dport
= net_service_lookup(dports
);
5851 logeventf(ssh
, "Service lookup failed for destination"
5852 " port \"%s\"", dports
);
5856 while (*ssh
->portfwd_strptr
) ssh
->portfwd_strptr
++;
5858 ssh
->portfwd_strptr
++; /* eat the NUL and move to next one */
5860 sport
= atoi(sports
);
5864 sport
= net_service_lookup(sports
);
5866 logeventf(ssh
, "Service lookup failed for source"
5867 " port \"%s\"", sports
);
5870 if (sport
&& dport
) {
5871 /* Set up a description of the source port. */
5872 static char *sportdesc
;
5873 sportdesc
= dupprintf("%.*s%.*s%.*s%.*s%d%.*s",
5874 (int)(*saddr?
strlen(saddr
):0), *saddr?saddr
:NULL
,
5875 (int)(*saddr?
1:0), ":",
5876 (int)(sserv ?
strlen(sports
) : 0), sports
,
5877 sserv
, "(", sport
, sserv
, ")");
5879 /* Verbose description of the destination port */
5880 char *dportdesc
= dupprintf("%s:%.*s%.*s%d%.*s",
5882 (int)(dserv ?
strlen(dports
) : 0), dports
,
5883 dserv
, "(", dport
, dserv
, ")");
5884 const char *err
= pfd_addforward(host
, dport
,
5885 *saddr ? saddr
: NULL
,
5886 sport
, ssh
, &ssh
->cfg
);
5888 logeventf(ssh
, "Local port %s forward to %s"
5889 " failed: %s", sportdesc
, dportdesc
, err
);
5891 logeventf(ssh
, "Local port %s forwarding to %s",
5892 sportdesc
, dportdesc
);
5895 } else if (type
== 'D') {
5896 const char *err
= pfd_addforward(NULL
, -1,
5897 *saddr ? saddr
: NULL
,
5898 sport
, ssh
, &ssh
->cfg
);
5900 logeventf(ssh
, "Local port %s SOCKS dynamic forward"
5901 " setup failed: %s", sportdesc
, err
);
5903 logeventf(ssh
, "Local port %s doing SOCKS"
5904 " dynamic forwarding", sportdesc
);
5907 struct ssh_rportfwd
*pf
;
5908 pf
= snew(struct ssh_rportfwd
);
5909 strcpy(pf
->dhost
, host
);
5912 if (add234(ssh
->rportfwds
, pf
) != pf
) {
5913 logeventf(ssh
, "Duplicate remote port forwarding"
5914 " to %s:%d", host
, dport
);
5917 logeventf(ssh
, "Requesting remote port %s"
5918 " forward to %s:%.*s%.*s%d%.*s",
5921 (int)(dserv ?
strlen(dports
) : 0), dports
,
5922 dserv
, "(", dport
, dserv
, ")");
5923 s
->pktout
= ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST
);
5924 ssh2_pkt_addstring(s
->pktout
, "tcpip-forward");
5925 ssh2_pkt_addbool(s
->pktout
, 1);/* want reply */
5927 ssh2_pkt_addstring(s
->pktout
, saddr
);
5928 } else if (ssh
->cfg
.rport_acceptall
) {
5929 ssh2_pkt_addstring(s
->pktout
, "0.0.0.0");
5931 ssh2_pkt_addstring(s
->pktout
, "127.0.0.1");
5933 ssh2_pkt_adduint32(s
->pktout
, sport
);
5934 ssh2_pkt_send(ssh
, s
->pktout
);
5936 crWaitUntilV(pktin
);
5938 if (pktin
->type
!= SSH2_MSG_REQUEST_SUCCESS
) {
5939 if (pktin
->type
!= SSH2_MSG_REQUEST_FAILURE
) {
5940 bombout(("Unexpected response to port "
5941 "forwarding request: packet type %d",
5945 logevent("Server refused this port forwarding");
5947 logevent("Remote port forwarding enabled");
5957 * Potentially enable agent forwarding.
5959 if (ssh
->mainchan
&& ssh
->cfg
.agentfwd
&& agent_exists()) {
5960 logevent("Requesting OpenSSH-style agent forwarding");
5961 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
5962 ssh2_pkt_adduint32(s
->pktout
, ssh
->mainchan
->remoteid
);
5963 ssh2_pkt_addstring(s
->pktout
, "auth-agent-req@openssh.com");
5964 ssh2_pkt_addbool(s
->pktout
, 1); /* want reply */
5965 ssh2_pkt_send(ssh
, s
->pktout
);
5967 crWaitUntilV(pktin
);
5969 if (pktin
->type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
5970 if (pktin
->type
!= SSH2_MSG_CHANNEL_FAILURE
) {
5971 bombout(("Unexpected response to agent forwarding request:"
5972 " packet type %d", pktin
->type
));
5975 logevent("Agent forwarding refused");
5977 logevent("Agent forwarding enabled");
5978 ssh
->agentfwd_enabled
= TRUE
;
5983 * Now allocate a pty for the session.
5985 if (ssh
->mainchan
&& !ssh
->cfg
.nopty
) {
5986 /* Unpick the terminal-speed string. */
5987 /* XXX perhaps we should allow no speeds to be sent. */
5988 ssh
->ospeed
= 38400; ssh
->ispeed
= 38400; /* last-resort defaults */
5989 sscanf(ssh
->cfg
.termspeed
, "%d,%d", &ssh
->ospeed
, &ssh
->ispeed
);
5990 /* Build the pty request. */
5991 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
5992 ssh2_pkt_adduint32(s
->pktout
, ssh
->mainchan
->remoteid
); /* recipient channel */
5993 ssh2_pkt_addstring(s
->pktout
, "pty-req");
5994 ssh2_pkt_addbool(s
->pktout
, 1); /* want reply */
5995 ssh2_pkt_addstring(s
->pktout
, ssh
->cfg
.termtype
);
5996 ssh2_pkt_adduint32(s
->pktout
, ssh
->term_width
);
5997 ssh2_pkt_adduint32(s
->pktout
, ssh
->term_height
);
5998 ssh2_pkt_adduint32(s
->pktout
, 0); /* pixel width */
5999 ssh2_pkt_adduint32(s
->pktout
, 0); /* pixel height */
6000 ssh2_pkt_addstring_start(s
->pktout
);
6001 ssh2_pkt_addbyte(s
->pktout
, 128); /* TTY_OP_ISPEED */
6002 ssh2_pkt_adduint32(s
->pktout
, ssh
->ispeed
);
6003 ssh2_pkt_addbyte(s
->pktout
, 129); /* TTY_OP_OSPEED */
6004 ssh2_pkt_adduint32(s
->pktout
, ssh
->ospeed
);
6005 ssh2_pkt_addstring_data(s
->pktout
, "\0", 1); /* TTY_OP_END */
6006 ssh2_pkt_send(ssh
, s
->pktout
);
6007 ssh
->state
= SSH_STATE_INTERMED
;
6009 crWaitUntilV(pktin
);
6011 if (pktin
->type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
6012 if (pktin
->type
!= SSH2_MSG_CHANNEL_FAILURE
) {
6013 bombout(("Unexpected response to pty request:"
6014 " packet type %d", pktin
->type
));
6017 c_write_str(ssh
, "Server refused to allocate pty\r\n");
6018 ssh
->editing
= ssh
->echoing
= 1;
6020 logeventf(ssh
, "Allocated pty (ospeed %dbps, ispeed %dbps)",
6021 ssh
->ospeed
, ssh
->ispeed
);
6024 ssh
->editing
= ssh
->echoing
= 1;
6028 * Send environment variables.
6030 * Simplest thing here is to send all the requests at once, and
6031 * then wait for a whole bunch of successes or failures.
6033 if (ssh
->mainchan
&& *ssh
->cfg
.environmt
) {
6034 char *e
= ssh
->cfg
.environmt
;
6035 char *var
, *varend
, *val
;
6041 while (*e
&& *e
!= '\t') e
++;
6043 if (*e
== '\t') e
++;
6048 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
6049 ssh2_pkt_adduint32(s
->pktout
, ssh
->mainchan
->remoteid
);
6050 ssh2_pkt_addstring(s
->pktout
, "env");
6051 ssh2_pkt_addbool(s
->pktout
, 1); /* want reply */
6052 ssh2_pkt_addstring_start(s
->pktout
);
6053 ssh2_pkt_addstring_data(s
->pktout
, var
, varend
-var
);
6054 ssh2_pkt_addstring(s
->pktout
, val
);
6055 ssh2_pkt_send(ssh
, s
->pktout
);
6060 logeventf(ssh
, "Sent %d environment variables", s
->num_env
);
6063 s
->env_left
= s
->num_env
;
6065 while (s
->env_left
> 0) {
6066 crWaitUntilV(pktin
);
6068 if (pktin
->type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
6069 if (pktin
->type
!= SSH2_MSG_CHANNEL_FAILURE
) {
6070 bombout(("Unexpected response to environment request:"
6071 " packet type %d", pktin
->type
));
6081 if (s
->env_ok
== s
->num_env
) {
6082 logevent("All environment variables successfully set");
6083 } else if (s
->env_ok
== 0) {
6084 logevent("All environment variables refused");
6085 c_write_str(ssh
, "Server refused to set environment variables\r\n");
6087 logeventf(ssh
, "%d environment variables refused",
6088 s
->num_env
- s
->env_ok
);
6089 c_write_str(ssh
, "Server refused to set all environment variables\r\n");
6094 * Start a shell or a remote command. We may have to attempt
6095 * this twice if the config data has provided a second choice
6098 if (ssh
->mainchan
) while (1) {
6102 if (ssh
->fallback_cmd
) {
6103 subsys
= ssh
->cfg
.ssh_subsys2
;
6104 cmd
= ssh
->cfg
.remote_cmd_ptr2
;
6106 subsys
= ssh
->cfg
.ssh_subsys
;
6107 cmd
= ssh
->cfg
.remote_cmd_ptr
;
6110 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
6111 ssh2_pkt_adduint32(s
->pktout
, ssh
->mainchan
->remoteid
); /* recipient channel */
6113 ssh2_pkt_addstring(s
->pktout
, "subsystem");
6114 ssh2_pkt_addbool(s
->pktout
, 1); /* want reply */
6115 ssh2_pkt_addstring(s
->pktout
, cmd
);
6117 ssh2_pkt_addstring(s
->pktout
, "exec");
6118 ssh2_pkt_addbool(s
->pktout
, 1); /* want reply */
6119 ssh2_pkt_addstring(s
->pktout
, cmd
);
6121 ssh2_pkt_addstring(s
->pktout
, "shell");
6122 ssh2_pkt_addbool(s
->pktout
, 1); /* want reply */
6124 ssh2_pkt_send(ssh
, s
->pktout
);
6126 crWaitUntilV(pktin
);
6128 if (pktin
->type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
6129 if (pktin
->type
!= SSH2_MSG_CHANNEL_FAILURE
) {
6130 bombout(("Unexpected response to shell/command request:"
6131 " packet type %d", pktin
->type
));
6135 * We failed to start the command. If this is the
6136 * fallback command, we really are finished; if it's
6137 * not, and if the fallback command exists, try falling
6138 * back to it before complaining.
6140 if (!ssh
->fallback_cmd
&& ssh
->cfg
.remote_cmd_ptr2
!= NULL
) {
6141 logevent("Primary command failed; attempting fallback");
6142 ssh
->fallback_cmd
= TRUE
;
6145 bombout(("Server refused to start a shell/command"));
6148 logevent("Started a shell/command");
6153 ssh
->state
= SSH_STATE_SESSION
;
6154 if (ssh
->size_needed
)
6155 ssh_size(ssh
, ssh
->term_width
, ssh
->term_height
);
6156 if (ssh
->eof_needed
)
6157 ssh_special(ssh
, TS_EOF
);
6163 ldisc_send(ssh
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
6168 s
->try_send
= FALSE
;
6170 if (pktin
->type
== SSH2_MSG_CHANNEL_DATA
||
6171 pktin
->type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
) {
6174 unsigned i
= ssh_pkt_getuint32(pktin
);
6175 struct ssh_channel
*c
;
6176 c
= find234(ssh
->channels
, &i
, ssh_channelfind
);
6178 continue; /* nonexistent channel */
6179 if (pktin
->type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
&&
6180 ssh_pkt_getuint32(pktin
) != SSH2_EXTENDED_DATA_STDERR
)
6181 continue; /* extended but not stderr */
6182 ssh_pkt_getstring(pktin
, &data
, &length
);
6185 c
->v
.v2
.locwindow
-= length
;
6187 case CHAN_MAINSESSION
:
6189 from_backend(ssh
->frontend
, pktin
->type
==
6190 SSH2_MSG_CHANNEL_EXTENDED_DATA
,
6194 bufsize
= x11_send(c
->u
.x11
.s
, data
, length
);
6197 bufsize
= pfd_send(c
->u
.pfd
.s
, data
, length
);
6200 while (length
> 0) {
6201 if (c
->u
.a
.lensofar
< 4) {
6202 int l
= min(4 - c
->u
.a
.lensofar
, length
);
6203 memcpy(c
->u
.a
.msglen
+ c
->u
.a
.lensofar
,
6207 c
->u
.a
.lensofar
+= l
;
6209 if (c
->u
.a
.lensofar
== 4) {
6211 4 + GET_32BIT(c
->u
.a
.msglen
);
6212 c
->u
.a
.message
= snewn(c
->u
.a
.totallen
,
6214 memcpy(c
->u
.a
.message
, c
->u
.a
.msglen
, 4);
6216 if (c
->u
.a
.lensofar
>= 4 && length
> 0) {
6218 min(c
->u
.a
.totallen
- c
->u
.a
.lensofar
,
6220 memcpy(c
->u
.a
.message
+ c
->u
.a
.lensofar
,
6224 c
->u
.a
.lensofar
+= l
;
6226 if (c
->u
.a
.lensofar
== c
->u
.a
.totallen
) {
6229 if (agent_query(c
->u
.a
.message
,
6232 ssh_agentf_callback
, c
))
6233 ssh_agentf_callback(c
, reply
, replylen
);
6234 sfree(c
->u
.a
.message
);
6235 c
->u
.a
.lensofar
= 0;
6242 * If we are not buffering too much data,
6243 * enlarge the window again at the remote side.
6245 if (bufsize
< OUR_V2_WINSIZE
)
6246 ssh2_set_window(c
, OUR_V2_WINSIZE
- bufsize
);
6248 } else if (pktin
->type
== SSH2_MSG_CHANNEL_EOF
) {
6249 unsigned i
= ssh_pkt_getuint32(pktin
);
6250 struct ssh_channel
*c
;
6252 c
= find234(ssh
->channels
, &i
, ssh_channelfind
);
6254 continue; /* nonexistent channel */
6256 if (c
->type
== CHAN_X11
) {
6258 * Remote EOF on an X11 channel means we should
6259 * wrap up and close the channel ourselves.
6261 x11_close(c
->u
.x11
.s
);
6263 } else if (c
->type
== CHAN_AGENT
) {
6265 } else if (c
->type
== CHAN_SOCKDATA
) {
6266 pfd_close(c
->u
.pfd
.s
);
6269 } else if (pktin
->type
== SSH2_MSG_CHANNEL_CLOSE
) {
6270 unsigned i
= ssh_pkt_getuint32(pktin
);
6271 struct ssh_channel
*c
;
6273 c
= find234(ssh
->channels
, &i
, ssh_channelfind
);
6274 if (!c
|| ((int)c
->remoteid
) == -1) {
6275 bombout(("Received CHANNEL_CLOSE for %s channel %d\n",
6276 c ?
"half-open" : "nonexistent", i
));
6279 /* Do pre-close processing on the channel. */
6281 case CHAN_MAINSESSION
:
6282 ssh
->mainchan
= NULL
;
6283 update_specials_menu(ssh
->frontend
);
6286 if (c
->u
.x11
.s
!= NULL
)
6287 x11_close(c
->u
.x11
.s
);
6294 if (c
->u
.pfd
.s
!= NULL
)
6295 pfd_close(c
->u
.pfd
.s
);
6299 if (c
->closes
== 0) {
6300 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE
);
6301 ssh2_pkt_adduint32(s
->pktout
, c
->remoteid
);
6302 ssh2_pkt_send(ssh
, s
->pktout
);
6304 del234(ssh
->channels
, c
);
6305 bufchain_clear(&c
->v
.v2
.outbuffer
);
6309 * See if that was the last channel left open.
6310 * (This is only our termination condition if we're
6311 * not running in -N mode.)
6313 if (!ssh
->cfg
.ssh_no_shell
&& count234(ssh
->channels
) == 0) {
6314 logevent("All channels closed. Disconnecting");
6317 * We used to send SSH_MSG_DISCONNECT here,
6318 * because I'd believed that _every_ conforming
6319 * SSH2 connection had to end with a disconnect
6320 * being sent by at least one side; apparently
6321 * I was wrong and it's perfectly OK to
6322 * unceremoniously slam the connection shut
6323 * when you're done, and indeed OpenSSH feels
6324 * this is more polite than sending a
6325 * DISCONNECT. So now we don't.
6327 s
->pktout
= ssh2_pkt_init(SSH2_MSG_DISCONNECT
);
6328 ssh2_pkt_adduint32(s
->pktout
, SSH2_DISCONNECT_BY_APPLICATION
);
6329 ssh2_pkt_addstring(s
->pktout
, "All open channels closed");
6330 ssh2_pkt_addstring(s
->pktout
, "en"); /* language tag */
6331 ssh2_pkt_send_noqueue(ssh
, s
->pktout
);
6333 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
6336 continue; /* remote sends close; ignore (FIXME) */
6337 } else if (pktin
->type
== SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
) {
6338 unsigned i
= ssh_pkt_getuint32(pktin
);
6339 struct ssh_channel
*c
;
6340 c
= find234(ssh
->channels
, &i
, ssh_channelfind
);
6342 continue; /* nonexistent channel */
6343 if (c
->type
!= CHAN_SOCKDATA_DORMANT
)
6344 continue; /* dunno why they're confirming this */
6345 c
->remoteid
= ssh_pkt_getuint32(pktin
);
6346 c
->type
= CHAN_SOCKDATA
;
6347 c
->v
.v2
.remwindow
= ssh_pkt_getuint32(pktin
);
6348 c
->v
.v2
.remmaxpkt
= ssh_pkt_getuint32(pktin
);
6350 pfd_confirm(c
->u
.pfd
.s
);
6353 * We have a pending close on this channel,
6354 * which we decided on before the server acked
6355 * the channel open. So now we know the
6356 * remoteid, we can close it again.
6358 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE
);
6359 ssh2_pkt_adduint32(s
->pktout
, c
->remoteid
);
6360 ssh2_pkt_send(ssh
, s
->pktout
);
6362 } else if (pktin
->type
== SSH2_MSG_CHANNEL_OPEN_FAILURE
) {
6363 static const char *const reasons
[] = {
6364 "<unknown reason code>",
6365 "Administratively prohibited",
6367 "Unknown channel type",
6368 "Resource shortage",
6370 unsigned i
= ssh_pkt_getuint32(pktin
);
6371 unsigned reason_code
;
6372 char *reason_string
;
6375 struct ssh_channel
*c
;
6376 c
= find234(ssh
->channels
, &i
, ssh_channelfind
);
6378 continue; /* nonexistent channel */
6379 if (c
->type
!= CHAN_SOCKDATA_DORMANT
)
6380 continue; /* dunno why they're failing this */
6382 reason_code
= ssh_pkt_getuint32(pktin
);
6383 if (reason_code
>= lenof(reasons
))
6384 reason_code
= 0; /* ensure reasons[reason_code] in range */
6385 ssh_pkt_getstring(pktin
, &reason_string
, &reason_length
);
6386 message
= dupprintf("Forwarded connection refused by"
6387 " server: %s [%.*s]", reasons
[reason_code
],
6388 reason_length
, reason_string
);
6392 pfd_close(c
->u
.pfd
.s
);
6394 del234(ssh
->channels
, c
);
6396 } else if (pktin
->type
== SSH2_MSG_CHANNEL_REQUEST
) {
6399 int typelen
, want_reply
;
6400 int reply
= SSH2_MSG_CHANNEL_FAILURE
; /* default */
6401 struct ssh_channel
*c
;
6403 localid
= ssh_pkt_getuint32(pktin
);
6404 ssh_pkt_getstring(pktin
, &type
, &typelen
);
6405 want_reply
= ssh2_pkt_getbool(pktin
);
6408 * First, check that the channel exists. Otherwise,
6409 * we can instantly disconnect with a rude message.
6411 c
= find234(ssh
->channels
, &localid
, ssh_channelfind
);
6414 sprintf(buf
, "Received channel request for nonexistent"
6415 " channel %d", localid
);
6417 s
->pktout
= ssh2_pkt_init(SSH2_MSG_DISCONNECT
);
6418 ssh2_pkt_adduint32(s
->pktout
, SSH2_DISCONNECT_BY_APPLICATION
);
6419 ssh2_pkt_addstring(s
->pktout
, buf
);
6420 ssh2_pkt_addstring(s
->pktout
, "en"); /* language tag */
6421 ssh2_pkt_send_noqueue(ssh
, s
->pktout
);
6422 connection_fatal(ssh
->frontend
, "%s", buf
);
6423 ssh_closing((Plug
)ssh
, NULL
, 0, 0);
6428 * Having got the channel number, we now look at
6429 * the request type string to see if it's something
6432 if (c
== ssh
->mainchan
) {
6434 * We recognise "exit-status" and "exit-signal" on
6435 * the primary channel.
6437 if (typelen
== 11 &&
6438 !memcmp(type
, "exit-status", 11)) {
6440 ssh
->exitcode
= ssh_pkt_getuint32(pktin
);
6441 logeventf(ssh
, "Server sent command exit status %d",
6443 reply
= SSH2_MSG_CHANNEL_SUCCESS
;
6445 } else if (typelen
== 11 &&
6446 !memcmp(type
, "exit-signal", 11)) {
6448 int is_plausible
= TRUE
, is_int
= FALSE
;
6449 char *fmt_sig
= "", *fmt_msg
= "";
6451 int msglen
= 0, core
= FALSE
;
6452 /* ICK: older versions of OpenSSH (e.g. 3.4p1)
6453 * provide an `int' for the signal, despite its
6454 * having been a `string' in the drafts since at
6455 * least 2001. (Fixed in session.c 1.147.) Try to
6456 * infer which we can safely parse it as. */
6458 unsigned char *p
= pktin
->body
+
6460 long len
= pktin
->length
- pktin
->savedpos
;
6461 unsigned long num
= GET_32BIT(p
); /* what is it? */
6462 /* If it's 0, it hardly matters; assume string */
6466 int maybe_int
= FALSE
, maybe_str
= FALSE
;
6467 #define CHECK_HYPOTHESIS(offset, result) \
6470 if (q >= 0 && q+4 <= len) { \
6471 q = q + 4 + GET_32BIT(p+q); \
6472 if (q >= 0 && q+4 <= len && \
6473 (q = q + 4 + GET_32BIT(p+q)) && q == len) \
6477 CHECK_HYPOTHESIS(4+1, maybe_int
);
6478 CHECK_HYPOTHESIS(4+num
+1, maybe_str
);
6479 #undef CHECK_HYPOTHESIS
6480 if (maybe_int
&& !maybe_str
)
6482 else if (!maybe_int
&& maybe_str
)
6485 /* Crikey. Either or neither. Panic. */
6486 is_plausible
= FALSE
;
6491 /* Old non-standard OpenSSH. */
6492 int signum
= ssh_pkt_getuint32(pktin
);
6493 fmt_sig
= dupprintf(" %d", signum
);
6495 /* As per the drafts. */
6498 ssh_pkt_getstring(pktin
, &sig
, &siglen
);
6499 /* Signal name isn't supposed to be blank, but
6500 * let's cope gracefully if it is. */
6502 fmt_sig
= dupprintf(" \"%.*s\"",
6506 core
= ssh2_pkt_getbool(pktin
);
6507 ssh_pkt_getstring(pktin
, &msg
, &msglen
);
6509 fmt_msg
= dupprintf(" (\"%.*s\")", msglen
, msg
);
6511 /* ignore lang tag */
6512 } /* else don't attempt to parse */
6513 logeventf(ssh
, "Server exited on signal%s%s%s",
6514 fmt_sig
, core ?
" (core dumped)" : "",
6516 if (*fmt_sig
) sfree(fmt_sig
);
6517 if (*fmt_msg
) sfree(fmt_msg
);
6518 reply
= SSH2_MSG_CHANNEL_SUCCESS
;
6523 * This is a channel request we don't know
6524 * about, so we now either ignore the request
6525 * or respond with CHANNEL_FAILURE, depending
6528 reply
= SSH2_MSG_CHANNEL_FAILURE
;
6531 s
->pktout
= ssh2_pkt_init(reply
);
6532 ssh2_pkt_adduint32(s
->pktout
, c
->remoteid
);
6533 ssh2_pkt_send(ssh
, s
->pktout
);
6535 } else if (pktin
->type
== SSH2_MSG_GLOBAL_REQUEST
) {
6537 int typelen
, want_reply
;
6539 ssh_pkt_getstring(pktin
, &type
, &typelen
);
6540 want_reply
= ssh2_pkt_getbool(pktin
);
6543 * We currently don't support any global requests
6544 * at all, so we either ignore the request or
6545 * respond with REQUEST_FAILURE, depending on
6549 s
->pktout
= ssh2_pkt_init(SSH2_MSG_REQUEST_FAILURE
);
6550 ssh2_pkt_send(ssh
, s
->pktout
);
6552 } else if (pktin
->type
== SSH2_MSG_CHANNEL_OPEN
) {
6559 struct ssh_channel
*c
;
6560 unsigned remid
, winsize
, pktsize
;
6561 ssh_pkt_getstring(pktin
, &type
, &typelen
);
6562 c
= snew(struct ssh_channel
);
6565 remid
= ssh_pkt_getuint32(pktin
);
6566 winsize
= ssh_pkt_getuint32(pktin
);
6567 pktsize
= ssh_pkt_getuint32(pktin
);
6569 if (typelen
== 3 && !memcmp(type
, "x11", 3)) {
6572 ssh_pkt_getstring(pktin
, &peeraddr
, &peeraddrlen
);
6573 addrstr
= snewn(peeraddrlen
+1, char);
6574 memcpy(addrstr
, peeraddr
, peeraddrlen
);
6575 addrstr
[peeraddrlen
] = '\0';
6576 peerport
= ssh_pkt_getuint32(pktin
);
6578 logeventf(ssh
, "Received X11 connect request from %s:%d",
6581 if (!ssh
->X11_fwd_enabled
)
6582 error
= "X11 forwarding is not enabled";
6583 else if (x11_init(&c
->u
.x11
.s
, ssh
->cfg
.x11_display
, c
,
6584 ssh
->x11auth
, addrstr
, peerport
,
6585 &ssh
->cfg
) != NULL
) {
6586 error
= "Unable to open an X11 connection";
6588 logevent("Opening X11 forward connection succeeded");
6593 } else if (typelen
== 15 &&
6594 !memcmp(type
, "forwarded-tcpip", 15)) {
6595 struct ssh_rportfwd pf
, *realpf
;
6598 ssh_pkt_getstring(pktin
, &dummy
, &dummylen
);/* skip address */
6599 pf
.sport
= ssh_pkt_getuint32(pktin
);
6600 ssh_pkt_getstring(pktin
, &peeraddr
, &peeraddrlen
);
6601 peerport
= ssh_pkt_getuint32(pktin
);
6602 realpf
= find234(ssh
->rportfwds
, &pf
, NULL
);
6603 logeventf(ssh
, "Received remote port %d open request "
6604 "from %s:%d", pf
.sport
, peeraddr
, peerport
);
6605 if (realpf
== NULL
) {
6606 error
= "Remote port is not recognised";
6608 const char *e
= pfd_newconnect(&c
->u
.pfd
.s
,
6612 logeventf(ssh
, "Attempting to forward remote port to "
6613 "%s:%d", realpf
->dhost
, realpf
->dport
);
6615 logeventf(ssh
, "Port open failed: %s", e
);
6616 error
= "Port open failed";
6618 logevent("Forwarded port opened successfully");
6619 c
->type
= CHAN_SOCKDATA
;
6622 } else if (typelen
== 22 &&
6623 !memcmp(type
, "auth-agent@openssh.com", 3)) {
6624 if (!ssh
->agentfwd_enabled
)
6625 error
= "Agent forwarding is not enabled";
6627 c
->type
= CHAN_AGENT
; /* identify channel type */
6628 c
->u
.a
.lensofar
= 0;
6631 error
= "Unsupported channel type requested";
6634 c
->remoteid
= remid
;
6636 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE
);
6637 ssh2_pkt_adduint32(s
->pktout
, c
->remoteid
);
6638 ssh2_pkt_adduint32(s
->pktout
, SSH2_OPEN_CONNECT_FAILED
);
6639 ssh2_pkt_addstring(s
->pktout
, error
);
6640 ssh2_pkt_addstring(s
->pktout
, "en"); /* language tag */
6641 ssh2_pkt_send(ssh
, s
->pktout
);
6642 logeventf(ssh
, "Rejected channel open: %s", error
);
6645 c
->localid
= alloc_channel_id(ssh
);
6647 c
->v
.v2
.locwindow
= OUR_V2_WINSIZE
;
6648 c
->v
.v2
.remwindow
= winsize
;
6649 c
->v
.v2
.remmaxpkt
= pktsize
;
6650 bufchain_init(&c
->v
.v2
.outbuffer
);
6651 add234(ssh
->channels
, c
);
6652 s
->pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
);
6653 ssh2_pkt_adduint32(s
->pktout
, c
->remoteid
);
6654 ssh2_pkt_adduint32(s
->pktout
, c
->localid
);
6655 ssh2_pkt_adduint32(s
->pktout
, c
->v
.v2
.locwindow
);
6656 ssh2_pkt_adduint32(s
->pktout
, 0x4000UL
); /* our max pkt size */
6657 ssh2_pkt_send(ssh
, s
->pktout
);
6660 bombout(("Strange packet received: type %d", pktin
->type
));
6663 } else if (ssh
->mainchan
) {
6665 * We have spare data. Add it to the channel buffer.
6667 ssh2_add_channel_data(ssh
->mainchan
, (char *)in
, inlen
);
6672 struct ssh_channel
*c
;
6674 * Try to send data on all channels if we can.
6676 for (i
= 0; NULL
!= (c
= index234(ssh
->channels
, i
)); i
++) {
6679 continue; /* don't send on closing channels */
6680 bufsize
= ssh2_try_send(c
);
6683 case CHAN_MAINSESSION
:
6684 /* stdin need not receive an unthrottle
6685 * notification since it will be polled */
6688 x11_unthrottle(c
->u
.x11
.s
);
6691 /* agent sockets are request/response and need no
6692 * buffer management */
6695 pfd_unthrottle(c
->u
.pfd
.s
);
6707 * Handlers for SSH2 messages that might arrive at any moment.
6709 void ssh2_msg_disconnect(Ssh ssh
, struct Packet
*pktin
)
6711 /* log reason code in disconnect message */
6713 int nowlen
, reason
, msglen
;
6715 reason
= ssh_pkt_getuint32(pktin
);
6716 ssh_pkt_getstring(pktin
, &msg
, &msglen
);
6718 if (reason
> 0 && reason
< lenof(ssh2_disconnect_reasons
)) {
6719 buf
= dupprintf("Received disconnect message (%s)",
6720 ssh2_disconnect_reasons
[reason
]);
6722 buf
= dupprintf("Received disconnect message (unknown"
6723 " type %d)", reason
);
6727 buf
= dupprintf("Disconnection message text: %n%.*s",
6728 &nowlen
, msglen
, msg
);
6730 bombout(("Server sent disconnect message\ntype %d (%s):\n\"%s\"",
6732 (reason
> 0 && reason
< lenof(ssh2_disconnect_reasons
)) ?
6733 ssh2_disconnect_reasons
[reason
] : "unknown",
6738 void ssh2_msg_debug(Ssh ssh
, struct Packet
*pktin
)
6740 /* log the debug message */
6745 /* XXX maybe we should actually take notice of this */
6746 always_display
= ssh2_pkt_getbool(pktin
);
6747 ssh_pkt_getstring(pktin
, &msg
, &msglen
);
6749 buf
= dupprintf("Remote debug message: %.*s", msglen
, msg
);
6754 void ssh2_msg_something_unimplemented(Ssh ssh
, struct Packet
*pktin
)
6756 struct Packet
*pktout
;
6757 pktout
= ssh2_pkt_init(SSH2_MSG_UNIMPLEMENTED
);
6758 ssh2_pkt_adduint32(pktout
, pktin
->sequence
);
6760 * UNIMPLEMENTED messages MUST appear in the same order as the
6761 * messages they respond to. Hence, never queue them.
6763 ssh2_pkt_send_noqueue(ssh
, pktout
);
6767 * Handle the top-level SSH2 protocol.
6769 static void ssh2_protocol_setup(Ssh ssh
)
6774 * Most messages cause SSH2_MSG_UNIMPLEMENTED.
6776 for (i
= 0; i
< 256; i
++)
6777 ssh
->packet_dispatch
[i
] = ssh2_msg_something_unimplemented
;
6780 * Any message we actually understand, we set to NULL so that
6781 * the coroutines will get it.
6783 ssh
->packet_dispatch
[SSH2_MSG_UNIMPLEMENTED
] = NULL
;
6784 ssh
->packet_dispatch
[SSH2_MSG_SERVICE_REQUEST
] = NULL
;
6785 ssh
->packet_dispatch
[SSH2_MSG_SERVICE_ACCEPT
] = NULL
;
6786 ssh
->packet_dispatch
[SSH2_MSG_KEXINIT
] = NULL
;
6787 ssh
->packet_dispatch
[SSH2_MSG_NEWKEYS
] = NULL
;
6788 ssh
->packet_dispatch
[SSH2_MSG_KEXDH_INIT
] = NULL
;
6789 ssh
->packet_dispatch
[SSH2_MSG_KEXDH_REPLY
] = NULL
;
6790 /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REQUEST] = NULL; duplicate case value */
6791 /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_GROUP] = NULL; duplicate case value */
6792 ssh
->packet_dispatch
[SSH2_MSG_KEX_DH_GEX_INIT
] = NULL
;
6793 ssh
->packet_dispatch
[SSH2_MSG_KEX_DH_GEX_REPLY
] = NULL
;
6794 ssh
->packet_dispatch
[SSH2_MSG_USERAUTH_REQUEST
] = NULL
;
6795 ssh
->packet_dispatch
[SSH2_MSG_USERAUTH_FAILURE
] = NULL
;
6796 ssh
->packet_dispatch
[SSH2_MSG_USERAUTH_SUCCESS
] = NULL
;
6797 ssh
->packet_dispatch
[SSH2_MSG_USERAUTH_BANNER
] = NULL
;
6798 ssh
->packet_dispatch
[SSH2_MSG_USERAUTH_PK_OK
] = NULL
;
6799 /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ] = NULL; duplicate case value */
6800 /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_REQUEST] = NULL; duplicate case value */
6801 ssh
->packet_dispatch
[SSH2_MSG_USERAUTH_INFO_RESPONSE
] = NULL
;
6802 ssh
->packet_dispatch
[SSH2_MSG_GLOBAL_REQUEST
] = NULL
;
6803 ssh
->packet_dispatch
[SSH2_MSG_REQUEST_SUCCESS
] = NULL
;
6804 ssh
->packet_dispatch
[SSH2_MSG_REQUEST_FAILURE
] = NULL
;
6805 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_OPEN
] = NULL
;
6806 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
] = NULL
;
6807 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_OPEN_FAILURE
] = NULL
;
6808 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_WINDOW_ADJUST
] = NULL
;
6809 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_DATA
] = NULL
;
6810 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_EXTENDED_DATA
] = NULL
;
6811 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_EOF
] = NULL
;
6812 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_CLOSE
] = NULL
;
6813 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_REQUEST
] = NULL
;
6814 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_SUCCESS
] = NULL
;
6815 ssh
->packet_dispatch
[SSH2_MSG_CHANNEL_FAILURE
] = NULL
;
6818 * These special message types we install handlers for.
6820 ssh
->packet_dispatch
[SSH2_MSG_DISCONNECT
] = ssh2_msg_disconnect
;
6821 ssh
->packet_dispatch
[SSH2_MSG_IGNORE
] = ssh_msg_ignore
; /* shared with ssh1 */
6822 ssh
->packet_dispatch
[SSH2_MSG_DEBUG
] = ssh2_msg_debug
;
6825 static void ssh2_protocol(Ssh ssh
, unsigned char *in
, int inlen
,
6826 struct Packet
*pktin
)
6828 if (ssh
->state
== SSH_STATE_CLOSED
)
6831 if (pktin
&& ssh
->packet_dispatch
[pktin
->type
]) {
6832 ssh
->packet_dispatch
[pktin
->type
](ssh
, pktin
);
6836 if (!ssh
->protocol_initial_phase_done
||
6837 (pktin
&& pktin
->type
>= 20 && pktin
->type
< 50)) {
6838 if (do_ssh2_transport(ssh
, in
, inlen
, pktin
) &&
6839 !ssh
->protocol_initial_phase_done
) {
6840 ssh
->protocol_initial_phase_done
= TRUE
;
6842 * Allow authconn to initialise itself.
6844 do_ssh2_authconn(ssh
, NULL
, 0, NULL
);
6847 do_ssh2_authconn(ssh
, in
, inlen
, pktin
);
6852 * Called to set up the connection.
6854 * Returns an error message, or NULL on success.
6856 static const char *ssh_init(void *frontend_handle
, void **backend_handle
,
6858 char *host
, int port
, char **realhost
, int nodelay
,
6864 ssh
= snew(struct ssh_tag
);
6865 ssh
->cfg
= *cfg
; /* STRUCTURE COPY */
6866 ssh
->version
= 0; /* when not ready yet */
6869 ssh
->v1_cipher_ctx
= NULL
;
6870 ssh
->crcda_ctx
= NULL
;
6871 ssh
->cscipher
= NULL
;
6872 ssh
->cs_cipher_ctx
= NULL
;
6873 ssh
->sccipher
= NULL
;
6874 ssh
->sc_cipher_ctx
= NULL
;
6876 ssh
->cs_mac_ctx
= NULL
;
6878 ssh
->sc_mac_ctx
= NULL
;
6880 ssh
->cs_comp_ctx
= NULL
;
6882 ssh
->sc_comp_ctx
= NULL
;
6884 ssh
->kex_ctx
= NULL
;
6885 ssh
->hostkey
= NULL
;
6887 ssh
->state
= SSH_STATE_PREPACKET
;
6888 ssh
->size_needed
= FALSE
;
6889 ssh
->eof_needed
= FALSE
;
6892 ssh
->deferred_send_data
= NULL
;
6893 ssh
->deferred_len
= 0;
6894 ssh
->deferred_size
= 0;
6895 ssh
->fallback_cmd
= 0;
6897 ssh
->x11auth
= NULL
;
6898 ssh
->v1_compressing
= FALSE
;
6899 ssh
->v2_outgoing_sequence
= 0;
6900 ssh
->ssh1_rdpkt_crstate
= 0;
6901 ssh
->ssh2_rdpkt_crstate
= 0;
6902 ssh
->do_ssh_init_crstate
= 0;
6903 ssh
->ssh_gotdata_crstate
= 0;
6904 ssh
->do_ssh1_connection_crstate
= 0;
6905 ssh
->do_ssh1_login_crstate
= 0;
6906 ssh
->do_ssh2_transport_crstate
= 0;
6907 ssh
->do_ssh2_authconn_crstate
= 0;
6908 ssh
->do_ssh_init_state
= NULL
;
6909 ssh
->do_ssh1_login_state
= NULL
;
6910 ssh
->do_ssh2_transport_state
= NULL
;
6911 ssh
->do_ssh2_authconn_state
= NULL
;
6912 ssh
->mainchan
= NULL
;
6913 ssh
->throttled_all
= 0;
6914 ssh
->v1_stdout_throttling
= 0;
6916 ssh
->queuelen
= ssh
->queuesize
= 0;
6917 ssh
->queueing
= FALSE
;
6919 *backend_handle
= ssh
;
6922 if (crypto_startup() == 0)
6923 return "Microsoft high encryption pack not installed!";
6926 ssh
->frontend
= frontend_handle
;
6927 ssh
->term_width
= ssh
->cfg
.width
;
6928 ssh
->term_height
= ssh
->cfg
.height
;
6930 ssh
->channels
= NULL
;
6931 ssh
->rportfwds
= NULL
;
6936 ssh
->v1_throttle_count
= 0;
6937 ssh
->overall_bufsize
= 0;
6938 ssh
->fallback_cmd
= 0;
6940 ssh
->protocol
= NULL
;
6942 ssh
->protocol_initial_phase_done
= FALSE
;
6946 p
= connect_to_host(ssh
, host
, port
, realhost
, nodelay
, keepalive
);
6953 static void ssh_free(void *handle
)
6955 Ssh ssh
= (Ssh
) handle
;
6956 struct ssh_channel
*c
;
6957 struct ssh_rportfwd
*pf
;
6959 if (ssh
->v1_cipher_ctx
)
6960 ssh
->cipher
->free_context(ssh
->v1_cipher_ctx
);
6961 if (ssh
->cs_cipher_ctx
)
6962 ssh
->cscipher
->free_context(ssh
->cs_cipher_ctx
);
6963 if (ssh
->sc_cipher_ctx
)
6964 ssh
->sccipher
->free_context(ssh
->sc_cipher_ctx
);
6965 if (ssh
->cs_mac_ctx
)
6966 ssh
->csmac
->free_context(ssh
->cs_mac_ctx
);
6967 if (ssh
->sc_mac_ctx
)
6968 ssh
->scmac
->free_context(ssh
->sc_mac_ctx
);
6969 if (ssh
->cs_comp_ctx
) {
6971 ssh
->cscomp
->compress_cleanup(ssh
->cs_comp_ctx
);
6973 zlib_compress_cleanup(ssh
->cs_comp_ctx
);
6975 if (ssh
->sc_comp_ctx
) {
6977 ssh
->sccomp
->decompress_cleanup(ssh
->sc_comp_ctx
);
6979 zlib_decompress_cleanup(ssh
->sc_comp_ctx
);
6982 dh_cleanup(ssh
->kex_ctx
);
6983 sfree(ssh
->savedhost
);
6985 while (ssh
->queuelen
-- > 0)
6986 ssh_free_packet(ssh
->queue
[ssh
->queuelen
]);
6989 if (ssh
->channels
) {
6990 while ((c
= delpos234(ssh
->channels
, 0)) != NULL
) {
6993 if (c
->u
.x11
.s
!= NULL
)
6994 x11_close(c
->u
.x11
.s
);
6997 if (c
->u
.pfd
.s
!= NULL
)
6998 pfd_close(c
->u
.pfd
.s
);
7003 freetree234(ssh
->channels
);
7006 if (ssh
->rportfwds
) {
7007 while ((pf
= delpos234(ssh
->rportfwds
, 0)) != NULL
)
7009 freetree234(ssh
->rportfwds
);
7011 sfree(ssh
->deferred_send_data
);
7013 x11_free_auth(ssh
->x11auth
);
7014 sfree(ssh
->do_ssh_init_state
);
7015 sfree(ssh
->do_ssh1_login_state
);
7016 sfree(ssh
->do_ssh2_transport_state
);
7017 sfree(ssh
->do_ssh2_authconn_state
);
7018 if (ssh
->crcda_ctx
) {
7019 crcda_free_context(ssh
->crcda_ctx
);
7020 ssh
->crcda_ctx
= NULL
;
7026 pinger_free(ssh
->pinger
);
7030 * Reconfigure the SSH backend.
7032 * Currently, this function does nothing very useful. In future,
7033 * however, we could do some handy things with it. For example, we
7034 * could make the port forwarding configurer active in the Change
7035 * Settings box, and this routine could close down existing
7036 * forwardings and open up new ones in response to changes.
7038 static void ssh_reconfig(void *handle
, Config
*cfg
)
7040 Ssh ssh
= (Ssh
) handle
;
7041 pinger_reconfig(ssh
->pinger
, &ssh
->cfg
, cfg
);
7042 ssh
->cfg
= *cfg
; /* STRUCTURE COPY */
7046 * Called to send data down the Telnet connection.
7048 static int ssh_send(void *handle
, char *buf
, int len
)
7050 Ssh ssh
= (Ssh
) handle
;
7052 if (ssh
== NULL
|| ssh
->s
== NULL
|| ssh
->protocol
== NULL
)
7055 ssh
->protocol(ssh
, (unsigned char *)buf
, len
, 0);
7057 return ssh_sendbuffer(ssh
);
7061 * Called to query the current amount of buffered stdin data.
7063 static int ssh_sendbuffer(void *handle
)
7065 Ssh ssh
= (Ssh
) handle
;
7068 if (ssh
== NULL
|| ssh
->s
== NULL
|| ssh
->protocol
== NULL
)
7072 * If the SSH socket itself has backed up, add the total backup
7073 * size on that to any individual buffer on the stdin channel.
7076 if (ssh
->throttled_all
)
7077 override_value
= ssh
->overall_bufsize
;
7079 if (ssh
->version
== 1) {
7080 return override_value
;
7081 } else if (ssh
->version
== 2) {
7082 if (!ssh
->mainchan
|| ssh
->mainchan
->closes
> 0)
7083 return override_value
;
7085 return (override_value
+
7086 bufchain_size(&ssh
->mainchan
->v
.v2
.outbuffer
));
7093 * Called to set the size of the window from SSH's POV.
7095 static void ssh_size(void *handle
, int width
, int height
)
7097 Ssh ssh
= (Ssh
) handle
;
7098 struct Packet
*pktout
;
7100 ssh
->term_width
= width
;
7101 ssh
->term_height
= height
;
7103 switch (ssh
->state
) {
7104 case SSH_STATE_BEFORE_SIZE
:
7105 case SSH_STATE_PREPACKET
:
7106 case SSH_STATE_CLOSED
:
7107 break; /* do nothing */
7108 case SSH_STATE_INTERMED
:
7109 ssh
->size_needed
= TRUE
; /* buffer for later */
7111 case SSH_STATE_SESSION
:
7112 if (!ssh
->cfg
.nopty
) {
7113 if (ssh
->version
== 1) {
7114 send_packet(ssh
, SSH1_CMSG_WINDOW_SIZE
,
7115 PKT_INT
, ssh
->term_height
,
7116 PKT_INT
, ssh
->term_width
,
7117 PKT_INT
, 0, PKT_INT
, 0, PKT_END
);
7118 } else if (ssh
->mainchan
) {
7119 pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
7120 ssh2_pkt_adduint32(pktout
, ssh
->mainchan
->remoteid
);
7121 ssh2_pkt_addstring(pktout
, "window-change");
7122 ssh2_pkt_addbool(pktout
, 0);
7123 ssh2_pkt_adduint32(pktout
, ssh
->term_width
);
7124 ssh2_pkt_adduint32(pktout
, ssh
->term_height
);
7125 ssh2_pkt_adduint32(pktout
, 0);
7126 ssh2_pkt_adduint32(pktout
, 0);
7127 ssh2_pkt_send(ssh
, pktout
);
7135 * Return a list of the special codes that make sense in this
7138 static const struct telnet_special
*ssh_get_specials(void *handle
)
7140 static const struct telnet_special ignore_special
[] = {
7141 {"IGNORE message", TS_NOP
},
7143 static const struct telnet_special ssh2_session_specials
[] = {
7146 /* These are the signal names defined by draft-ietf-secsh-connect-19.
7147 * They include all the ISO C signals, but are a subset of the POSIX
7148 * required signals. */
7149 {"SIGINT (Interrupt)", TS_SIGINT
},
7150 {"SIGTERM (Terminate)", TS_SIGTERM
},
7151 {"SIGKILL (Kill)", TS_SIGKILL
},
7152 {"SIGQUIT (Quit)", TS_SIGQUIT
},
7153 {"SIGHUP (Hangup)", TS_SIGHUP
},
7154 {"More signals", TS_SUBMENU
},
7155 {"SIGABRT", TS_SIGABRT
}, {"SIGALRM", TS_SIGALRM
},
7156 {"SIGFPE", TS_SIGFPE
}, {"SIGILL", TS_SIGILL
},
7157 {"SIGPIPE", TS_SIGPIPE
}, {"SIGSEGV", TS_SIGSEGV
},
7158 {"SIGUSR1", TS_SIGUSR1
}, {"SIGUSR2", TS_SIGUSR2
},
7161 static const struct telnet_special specials_end
[] = {
7164 static struct telnet_special ssh_specials
[lenof(ignore_special
) +
7165 lenof(ssh2_session_specials
) +
7166 lenof(specials_end
)];
7167 Ssh ssh
= (Ssh
) handle
;
7169 #define ADD_SPECIALS(name) \
7171 assert((i + lenof(name)) <= lenof(ssh_specials)); \
7172 memcpy(&ssh_specials[i], name, sizeof name); \
7176 if (ssh
->version
== 1) {
7177 /* Don't bother offering IGNORE if we've decided the remote
7178 * won't cope with it, since we wouldn't bother sending it if
7180 if (!(ssh
->remote_bugs
& BUG_CHOKES_ON_SSH1_IGNORE
))
7181 ADD_SPECIALS(ignore_special
);
7182 } else if (ssh
->version
== 2) {
7183 /* XXX add rekey, when implemented */
7184 ADD_SPECIALS(ignore_special
);
7186 ADD_SPECIALS(ssh2_session_specials
);
7187 } /* else we're not ready yet */
7190 ADD_SPECIALS(specials_end
);
7191 return ssh_specials
;
7199 * Send Telnet special codes. TS_EOF is useful for `plink', so you
7200 * can send an EOF and collect resulting output (e.g. `plink
7203 static void ssh_special(void *handle
, Telnet_Special code
)
7205 Ssh ssh
= (Ssh
) handle
;
7206 struct Packet
*pktout
;
7208 if (code
== TS_EOF
) {
7209 if (ssh
->state
!= SSH_STATE_SESSION
) {
7211 * Buffer the EOF in case we are pre-SESSION, so we can
7212 * send it as soon as we reach SESSION.
7215 ssh
->eof_needed
= TRUE
;
7218 if (ssh
->version
== 1) {
7219 send_packet(ssh
, SSH1_CMSG_EOF
, PKT_END
);
7220 } else if (ssh
->mainchan
) {
7221 struct Packet
*pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF
);
7222 ssh2_pkt_adduint32(pktout
, ssh
->mainchan
->remoteid
);
7223 ssh2_pkt_send(ssh
, pktout
);
7225 logevent("Sent EOF message");
7226 } else if (code
== TS_PING
|| code
== TS_NOP
) {
7227 if (ssh
->state
== SSH_STATE_CLOSED
7228 || ssh
->state
== SSH_STATE_PREPACKET
) return;
7229 if (ssh
->version
== 1) {
7230 if (!(ssh
->remote_bugs
& BUG_CHOKES_ON_SSH1_IGNORE
))
7231 send_packet(ssh
, SSH1_MSG_IGNORE
, PKT_STR
, "", PKT_END
);
7233 pktout
= ssh2_pkt_init(SSH2_MSG_IGNORE
);
7234 ssh2_pkt_addstring_start(pktout
);
7235 ssh2_pkt_send_noqueue(ssh
, pktout
);
7237 } else if (code
== TS_BRK
) {
7238 if (ssh
->state
== SSH_STATE_CLOSED
7239 || ssh
->state
== SSH_STATE_PREPACKET
) return;
7240 if (ssh
->version
== 1) {
7241 logevent("Unable to send BREAK signal in SSH1");
7242 } else if (ssh
->mainchan
) {
7243 pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
7244 ssh2_pkt_adduint32(pktout
, ssh
->mainchan
->remoteid
);
7245 ssh2_pkt_addstring(pktout
, "break");
7246 ssh2_pkt_addbool(pktout
, 0);
7247 ssh2_pkt_adduint32(pktout
, 0); /* default break length */
7248 ssh2_pkt_send(ssh
, pktout
);
7251 /* Is is a POSIX signal? */
7252 char *signame
= NULL
;
7253 if (code
== TS_SIGABRT
) signame
= "ABRT";
7254 if (code
== TS_SIGALRM
) signame
= "ALRM";
7255 if (code
== TS_SIGFPE
) signame
= "FPE";
7256 if (code
== TS_SIGHUP
) signame
= "HUP";
7257 if (code
== TS_SIGILL
) signame
= "ILL";
7258 if (code
== TS_SIGINT
) signame
= "INT";
7259 if (code
== TS_SIGKILL
) signame
= "KILL";
7260 if (code
== TS_SIGPIPE
) signame
= "PIPE";
7261 if (code
== TS_SIGQUIT
) signame
= "QUIT";
7262 if (code
== TS_SIGSEGV
) signame
= "SEGV";
7263 if (code
== TS_SIGTERM
) signame
= "TERM";
7264 if (code
== TS_SIGUSR1
) signame
= "USR1";
7265 if (code
== TS_SIGUSR2
) signame
= "USR2";
7266 /* The SSH-2 protocol does in principle support arbitrary named
7267 * signals, including signame@domain, but we don't support those. */
7269 /* It's a signal. */
7270 if (ssh
->version
== 2 && ssh
->mainchan
) {
7271 pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
7272 ssh2_pkt_adduint32(pktout
, ssh
->mainchan
->remoteid
);
7273 ssh2_pkt_addstring(pktout
, "signal");
7274 ssh2_pkt_addbool(pktout
, 0);
7275 ssh2_pkt_addstring(pktout
, signame
);
7276 ssh2_pkt_send(ssh
, pktout
);
7277 logeventf(ssh
, "Sent signal SIG%s", signame
);
7280 /* Never heard of it. Do nothing */
7285 void *new_sock_channel(void *handle
, Socket s
)
7287 Ssh ssh
= (Ssh
) handle
;
7288 struct ssh_channel
*c
;
7289 c
= snew(struct ssh_channel
);
7293 c
->remoteid
= -1; /* to be set when open confirmed */
7294 c
->localid
= alloc_channel_id(ssh
);
7296 c
->type
= CHAN_SOCKDATA_DORMANT
;/* identify channel type */
7298 bufchain_init(&c
->v
.v2
.outbuffer
);
7299 add234(ssh
->channels
, c
);
7305 * This is called when stdout/stderr (the entity to which
7306 * from_backend sends data) manages to clear some backlog.
7308 static void ssh_unthrottle(void *handle
, int bufsize
)
7310 Ssh ssh
= (Ssh
) handle
;
7311 if (ssh
->version
== 1) {
7312 if (ssh
->v1_stdout_throttling
&& bufsize
< SSH1_BUFFER_LIMIT
) {
7313 ssh
->v1_stdout_throttling
= 0;
7314 ssh1_throttle(ssh
, -1);
7317 if (ssh
->mainchan
&& ssh
->mainchan
->closes
== 0)
7318 ssh2_set_window(ssh
->mainchan
, OUR_V2_WINSIZE
- bufsize
);
7322 void ssh_send_port_open(void *channel
, char *hostname
, int port
, char *org
)
7324 struct ssh_channel
*c
= (struct ssh_channel
*)channel
;
7326 struct Packet
*pktout
;
7328 logeventf(ssh
, "Opening forwarded connection to %s:%d", hostname
, port
);
7330 if (ssh
->version
== 1) {
7331 send_packet(ssh
, SSH1_MSG_PORT_OPEN
,
7332 PKT_INT
, c
->localid
,
7335 //PKT_STR, <org:orgport>,
7338 pktout
= ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN
);
7339 ssh2_pkt_addstring(pktout
, "direct-tcpip");
7340 ssh2_pkt_adduint32(pktout
, c
->localid
);
7341 c
->v
.v2
.locwindow
= OUR_V2_WINSIZE
;
7342 ssh2_pkt_adduint32(pktout
, c
->v
.v2
.locwindow
);/* our window size */
7343 ssh2_pkt_adduint32(pktout
, 0x4000UL
); /* our max pkt size */
7344 ssh2_pkt_addstring(pktout
, hostname
);
7345 ssh2_pkt_adduint32(pktout
, port
);
7347 * We make up values for the originator data; partly it's
7348 * too much hassle to keep track, and partly I'm not
7349 * convinced the server should be told details like that
7350 * about my local network configuration.
7352 ssh2_pkt_addstring(pktout
, "client-side-connection");
7353 ssh2_pkt_adduint32(pktout
, 0);
7354 ssh2_pkt_send(ssh
, pktout
);
7358 static Socket
ssh_socket(void *handle
)
7360 Ssh ssh
= (Ssh
) handle
;
7364 static int ssh_sendok(void *handle
)
7366 Ssh ssh
= (Ssh
) handle
;
7367 return ssh
->send_ok
;
7370 static int ssh_ldisc(void *handle
, int option
)
7372 Ssh ssh
= (Ssh
) handle
;
7373 if (option
== LD_ECHO
)
7374 return ssh
->echoing
;
7375 if (option
== LD_EDIT
)
7376 return ssh
->editing
;
7380 static void ssh_provide_ldisc(void *handle
, void *ldisc
)
7382 Ssh ssh
= (Ssh
) handle
;
7386 static void ssh_provide_logctx(void *handle
, void *logctx
)
7388 Ssh ssh
= (Ssh
) handle
;
7389 ssh
->logctx
= logctx
;
7392 static int ssh_return_exitcode(void *handle
)
7394 Ssh ssh
= (Ssh
) handle
;
7398 return (ssh
->exitcode
>= 0 ? ssh
->exitcode
: 0);
7402 * Gross hack: pscp will try to start SFTP but fall back to scp1 if
7403 * that fails. This variable is the means by which scp.c can reach
7404 * into the SSH code and find out which one it got.
7406 extern int ssh_fallback_cmd(void *handle
)
7408 Ssh ssh
= (Ssh
) handle
;
7409 return ssh
->fallback_cmd
;
7412 Backend ssh_backend
= {
7422 ssh_return_exitcode
,