374330e2 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
fb09bf1c |
3 | #include <stdarg.h> |
4 | #include <assert.h> |
374330e2 |
5 | |
6 | #include "putty.h" |
dacbd0e8 |
7 | #include "tree234.h" |
fb09bf1c |
8 | #include "ssh.h" |
374330e2 |
9 | |
10 | #ifndef FALSE |
11 | #define FALSE 0 |
12 | #endif |
13 | #ifndef TRUE |
14 | #define TRUE 1 |
15 | #endif |
16 | |
32874aea |
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 */ |
58 | |
59 | #define SSH1_AUTH_TIS 5 /* 0x5 */ |
60 | #define SSH1_AUTH_CCARD 16 /* 0x10 */ |
61 | |
62 | #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */ |
b96dc54c |
63 | /* Mask for protoflags we will echo back to server if seen */ |
32874aea |
64 | #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */ |
65 | |
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 */ |
761187b6 |
86 | #define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */ |
87 | #define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */ |
32874aea |
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 */ |
102 | |
00db133f |
103 | /* |
104 | * Packet type contexts, so that ssh2_pkt_type can correctly decode |
105 | * the ambiguous type numbers back into the correct type strings. |
106 | */ |
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 |
113 | |
32874aea |
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 */ |
38c4a8da |
129 | |
130 | static const char *const ssh2_disconnect_reasons[] = { |
131 | NULL, |
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", |
147 | }; |
9005f3ba |
148 | |
32874aea |
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 */ |
d211621f |
153 | |
32874aea |
154 | #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */ |
fb09bf1c |
155 | |
7d503c31 |
156 | /* |
157 | * Various remote-bug flags. |
158 | */ |
159 | #define BUG_CHOKES_ON_SSH1_IGNORE 1 |
160 | #define BUG_SSH2_HMAC 2 |
bd358db1 |
161 | #define BUG_NEEDS_SSH1_PLAIN_PASSWORD 4 |
0df73905 |
162 | #define BUG_CHOKES_ON_RSA 8 |
1dd353b5 |
163 | #define BUG_SSH2_RSA_PADDING 16 |
088bde77 |
164 | #define BUG_SSH2_DERIVEKEY 32 |
8e975795 |
165 | #define BUG_SSH2_DH_GEX 64 |
dda87a28 |
166 | #define BUG_SSH2_PK_SESSIONID 128 |
bd358db1 |
167 | |
00db133f |
168 | #define translate(x) if (type == x) return #x |
51470298 |
169 | #define translatec(x,ctx) if (type == x && (pkt_ctx & ctx)) return #x |
ae9ae89f |
170 | static char *ssh1_pkt_type(int type) |
00db133f |
171 | { |
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); |
213 | return "unknown"; |
214 | } |
ae9ae89f |
215 | static char *ssh2_pkt_type(int pkt_ctx, int type) |
00db133f |
216 | { |
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); |
253 | return "unknown"; |
254 | } |
255 | #undef translate |
256 | #undef translatec |
7d503c31 |
257 | |
fb09bf1c |
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])) |
263 | |
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); } |
269 | |
9a10ecf4 |
270 | /* Enumeration values for fields in SSH-1 packets */ |
271 | enum { |
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 |
276 | }; |
972a41c8 |
277 | |
acddebd9 |
278 | /* |
279 | * Coroutine mechanics for the sillier bits of the code. If these |
280 | * macros look impenetrable to you, you might find it helpful to |
281 | * read |
282 | * |
283 | * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html |
284 | * |
285 | * which explains the theory behind these macros. |
b624d1e6 |
286 | * |
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: |
291 | * |
292 | * - right-click ssh.c in the FileView |
293 | * - click Settings |
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'. |
acddebd9 |
297 | */ |
51470298 |
298 | #define crBegin(v) { int *crLine = &v; switch(v) { case 0:; |
299 | #define crState(t) \ |
300 | struct t *s; \ |
3d88e64d |
301 | if (!ssh->t) ssh->t = snew(struct t); \ |
51470298 |
302 | s = ssh->t; |
303 | #define crFinish(z) } *crLine = 0; return (z); } |
304 | #define crFinishV } *crLine = 0; return; } |
374330e2 |
305 | #define crReturn(z) \ |
306 | do {\ |
51470298 |
307 | *crLine =__LINE__; return (z); case __LINE__:;\ |
374330e2 |
308 | } while (0) |
309 | #define crReturnV \ |
310 | do {\ |
51470298 |
311 | *crLine=__LINE__; return; case __LINE__:;\ |
374330e2 |
312 | } while (0) |
51470298 |
313 | #define crStop(z) do{ *crLine = 0; return (z); }while(0) |
314 | #define crStopV do{ *crLine = 0; return; }while(0) |
fb09bf1c |
315 | #define crWaitUntil(c) do { crReturn(0); } while (!(c)) |
7cca0d81 |
316 | #define crWaitUntilV(c) do { crReturnV; } while (!(c)) |
374330e2 |
317 | |
51470298 |
318 | typedef struct ssh_tag *Ssh; |
319 | |
51470298 |
320 | static void ssh2_pkt_init(Ssh, int pkt_type); |
321 | static void ssh2_pkt_addbool(Ssh, unsigned char value); |
322 | static void ssh2_pkt_adduint32(Ssh, unsigned long value); |
323 | static void ssh2_pkt_addstring_start(Ssh); |
324 | static void ssh2_pkt_addstring_str(Ssh, char *data); |
325 | static void ssh2_pkt_addstring_data(Ssh, char *data, int len); |
326 | static void ssh2_pkt_addstring(Ssh, char *data); |
d8baa528 |
327 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len); |
51470298 |
328 | static void ssh2_pkt_addmp(Ssh, Bignum b); |
329 | static int ssh2_pkt_construct(Ssh); |
330 | static void ssh2_pkt_send(Ssh); |
839f10db |
331 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, int ispkt); |
332 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt); |
3d63ca2e |
333 | |
5471d09a |
334 | /* |
335 | * Buffer management constants. There are several of these for |
336 | * various different purposes: |
337 | * |
338 | * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up |
339 | * on a local data stream before we throttle the whole SSH |
340 | * connection (in SSH1 only). Throttling the whole connection is |
341 | * pretty drastic so we set this high in the hope it won't |
342 | * happen very often. |
343 | * |
344 | * - SSH_MAX_BACKLOG is the amount of backlog that must build up |
345 | * on the SSH connection itself before we defensively throttle |
346 | * _all_ local data streams. This is pretty drastic too (though |
347 | * thankfully unlikely in SSH2 since the window mechanism should |
348 | * ensure that the server never has any need to throttle its end |
349 | * of the connection), so we set this high as well. |
350 | * |
351 | * - OUR_V2_WINSIZE is the maximum window size we present on SSH2 |
352 | * channels. |
353 | */ |
354 | |
355 | #define SSH1_BUFFER_LIMIT 32768 |
356 | #define SSH_MAX_BACKLOG 32768 |
357 | #define OUR_V2_WINSIZE 16384 |
d74d141c |
358 | |
a92dd380 |
359 | const static struct ssh_kex *kex_algs[] = { |
a92dd380 |
360 | &ssh_diffiehellman_gex, |
32874aea |
361 | &ssh_diffiehellman |
362 | }; |
e5574168 |
363 | |
85cc02bb |
364 | const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss }; |
e5574168 |
365 | |
e0e1a00d |
366 | static void *nullmac_make_context(void) |
367 | { |
368 | return NULL; |
369 | } |
370 | static void nullmac_free_context(void *handle) |
371 | { |
372 | } |
373 | static void nullmac_key(void *handle, unsigned char *key) |
32874aea |
374 | { |
375 | } |
e0e1a00d |
376 | static void nullmac_generate(void *handle, unsigned char *blk, int len, |
32874aea |
377 | unsigned long seq) |
378 | { |
379 | } |
e0e1a00d |
380 | static int nullmac_verify(void *handle, unsigned char *blk, int len, |
381 | unsigned long seq) |
32874aea |
382 | { |
383 | return 1; |
384 | } |
57476f6b |
385 | const static struct ssh_mac ssh_mac_none = { |
e0e1a00d |
386 | nullmac_make_context, nullmac_free_context, nullmac_key, |
387 | nullmac_generate, nullmac_verify, "none", 0 |
e5574168 |
388 | }; |
8b2715b2 |
389 | const static struct ssh_mac *macs[] = { |
32874aea |
390 | &ssh_sha1, &ssh_md5, &ssh_mac_none |
391 | }; |
8b2715b2 |
392 | const static struct ssh_mac *buggymacs[] = { |
32874aea |
393 | &ssh_sha1_buggy, &ssh_md5, &ssh_mac_none |
394 | }; |
e5574168 |
395 | |
5366aed8 |
396 | static void *ssh_comp_none_init(void) |
397 | { |
398 | return NULL; |
399 | } |
400 | static void ssh_comp_none_cleanup(void *handle) |
32874aea |
401 | { |
402 | } |
5366aed8 |
403 | static int ssh_comp_none_block(void *handle, unsigned char *block, int len, |
32874aea |
404 | unsigned char **outblock, int *outlen) |
405 | { |
406 | return 0; |
407 | } |
5366aed8 |
408 | static int ssh_comp_none_disable(void *handle) |
32874aea |
409 | { |
4ba9b64b |
410 | return 0; |
411 | } |
57476f6b |
412 | const static struct ssh_compress ssh_comp_none = { |
4ba9b64b |
413 | "none", |
5366aed8 |
414 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
415 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
416 | ssh_comp_none_disable, NULL |
e5574168 |
417 | }; |
4ba9b64b |
418 | extern const struct ssh_compress ssh_zlib; |
419 | const static struct ssh_compress *compressions[] = { |
32874aea |
420 | &ssh_zlib, &ssh_comp_none |
421 | }; |
374330e2 |
422 | |
32874aea |
423 | enum { /* channel types */ |
783415f8 |
424 | CHAN_MAINSESSION, |
425 | CHAN_X11, |
426 | CHAN_AGENT, |
bc240b21 |
427 | CHAN_SOCKDATA, |
428 | CHAN_SOCKDATA_DORMANT /* one the remote hasn't confirmed */ |
783415f8 |
429 | }; |
430 | |
dacbd0e8 |
431 | /* |
432 | * 2-3-4 tree storing channels. |
433 | */ |
434 | struct ssh_channel { |
51470298 |
435 | Ssh ssh; /* pointer back to main context */ |
d211621f |
436 | unsigned remoteid, localid; |
dacbd0e8 |
437 | int type; |
0357890f |
438 | /* |
439 | * In SSH1, this value contains four bits: |
440 | * |
441 | * 1 We have sent SSH1_MSG_CHANNEL_CLOSE. |
442 | * 2 We have sent SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
443 | * 4 We have received SSH1_MSG_CHANNEL_CLOSE. |
444 | * 8 We have received SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
445 | * |
446 | * A channel is completely finished with when all four bits are set. |
447 | */ |
dacbd0e8 |
448 | int closes; |
5471d09a |
449 | union { |
450 | struct ssh1_data_channel { |
451 | int throttling; |
452 | } v1; |
453 | struct ssh2_data_channel { |
454 | bufchain outbuffer; |
455 | unsigned remwindow, remmaxpkt; |
456 | unsigned locwindow; |
457 | } v2; |
458 | } v; |
dacbd0e8 |
459 | union { |
32874aea |
460 | struct ssh_agent_channel { |
461 | unsigned char *message; |
462 | unsigned char msglen[4]; |
463 | int lensofar, totallen; |
464 | } a; |
465 | struct ssh_x11_channel { |
466 | Socket s; |
467 | } x11; |
d74d141c |
468 | struct ssh_pfd_channel { |
469 | Socket s; |
470 | } pfd; |
dacbd0e8 |
471 | } u; |
472 | }; |
57476f6b |
473 | |
d74d141c |
474 | /* |
bc240b21 |
475 | * 2-3-4 tree storing remote->local port forwardings. SSH 1 and SSH |
476 | * 2 use this structure in different ways, reflecting SSH 2's |
477 | * altogether saner approach to port forwarding. |
478 | * |
479 | * In SSH 1, you arrange a remote forwarding by sending the server |
480 | * the remote port number, and the local destination host:port. |
481 | * When a connection comes in, the server sends you back that |
482 | * host:port pair, and you connect to it. This is a ready-made |
483 | * security hole if you're not on the ball: a malicious server |
484 | * could send you back _any_ host:port pair, so if you trustingly |
485 | * connect to the address it gives you then you've just opened the |
486 | * entire inside of your corporate network just by connecting |
487 | * through it to a dodgy SSH server. Hence, we must store a list of |
488 | * host:port pairs we _are_ trying to forward to, and reject a |
489 | * connection request from the server if it's not in the list. |
490 | * |
491 | * In SSH 2, each side of the connection minds its own business and |
492 | * doesn't send unnecessary information to the other. You arrange a |
493 | * remote forwarding by sending the server just the remote port |
494 | * number. When a connection comes in, the server tells you which |
495 | * of its ports was connected to; and _you_ have to remember what |
496 | * local host:port pair went with that port number. |
497 | * |
498 | * Hence: in SSH 1 this structure stores host:port pairs we intend |
499 | * to allow connections to, and is indexed by those host:port |
500 | * pairs. In SSH 2 it stores a mapping from source port to |
501 | * destination host:port pair, and is indexed by source port. |
d74d141c |
502 | */ |
503 | struct ssh_rportfwd { |
bc240b21 |
504 | unsigned sport, dport; |
505 | char dhost[256]; |
d74d141c |
506 | }; |
507 | |
57476f6b |
508 | struct Packet { |
509 | long length; |
510 | int type; |
511 | unsigned char *data; |
512 | unsigned char *body; |
513 | long savedpos; |
514 | long maxlen; |
515 | }; |
516 | |
51470298 |
517 | static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt); |
518 | static void ssh2_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt); |
519 | static void ssh_size(void *handle, int width, int height); |
520 | static void ssh_special(void *handle, Telnet_Special); |
5471d09a |
521 | static int ssh2_try_send(struct ssh_channel *c); |
51470298 |
522 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len); |
523 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize); |
5471d09a |
524 | static void ssh2_set_window(struct ssh_channel *c, unsigned newwin); |
51470298 |
525 | static int ssh_sendbuffer(void *handle); |
36f94d1f |
526 | static void ssh_do_close(Ssh ssh); |
57476f6b |
527 | |
51470298 |
528 | struct rdpkt1_state_tag { |
57476f6b |
529 | long len, pad, biglen, to_read; |
530 | unsigned long realcrc, gotcrc; |
531 | unsigned char *p; |
532 | int i; |
533 | int chunk; |
51470298 |
534 | }; |
57476f6b |
535 | |
51470298 |
536 | struct rdpkt2_state_tag { |
960e736a |
537 | long len, pad, payload, packetlen, maclen; |
538 | int i; |
539 | int cipherblk; |
540 | unsigned long incoming_sequence; |
51470298 |
541 | }; |
542 | |
543 | struct ssh_tag { |
544 | const struct plug_function_table *fn; |
545 | /* the above field _must_ be first in the structure */ |
546 | |
547 | SHA_State exhash, exhashbase; |
548 | |
549 | Socket s; |
550 | |
b9d7bcad |
551 | void *ldisc; |
a8327734 |
552 | void *logctx; |
b9d7bcad |
553 | |
51470298 |
554 | unsigned char session_key[32]; |
555 | int v1_compressing; |
556 | int v1_remote_protoflags; |
557 | int v1_local_protoflags; |
558 | int agentfwd_enabled; |
559 | int X11_fwd_enabled; |
560 | int remote_bugs; |
561 | const struct ssh_cipher *cipher; |
371e569c |
562 | void *v1_cipher_ctx; |
0183b242 |
563 | void *crcda_ctx; |
51470298 |
564 | const struct ssh2_cipher *cscipher, *sccipher; |
371e569c |
565 | void *cs_cipher_ctx, *sc_cipher_ctx; |
51470298 |
566 | const struct ssh_mac *csmac, *scmac; |
e0e1a00d |
567 | void *cs_mac_ctx, *sc_mac_ctx; |
51470298 |
568 | const struct ssh_compress *cscomp, *sccomp; |
5366aed8 |
569 | void *cs_comp_ctx, *sc_comp_ctx; |
51470298 |
570 | const struct ssh_kex *kex; |
571 | const struct ssh_signkey *hostkey; |
572 | unsigned char v2_session_id[20]; |
27cd7fc2 |
573 | void *kex_ctx; |
51470298 |
574 | |
575 | char *savedhost; |
576 | int savedport; |
577 | int send_ok; |
578 | int echoing, editing; |
579 | |
580 | void *frontend; |
581 | |
db219738 |
582 | int ospeed, ispeed; /* temporaries */ |
51470298 |
583 | int term_width, term_height; |
584 | |
585 | tree234 *channels; /* indexed by local id */ |
586 | struct ssh_channel *mainchan; /* primary session channel */ |
587 | int exitcode; |
588 | |
589 | tree234 *rportfwds; |
590 | |
591 | enum { |
592 | SSH_STATE_PREPACKET, |
593 | SSH_STATE_BEFORE_SIZE, |
594 | SSH_STATE_INTERMED, |
595 | SSH_STATE_SESSION, |
596 | SSH_STATE_CLOSED |
597 | } state; |
598 | |
599 | int size_needed, eof_needed; |
600 | |
601 | struct Packet pktin; |
602 | struct Packet pktout; |
603 | unsigned char *deferred_send_data; |
604 | int deferred_len, deferred_size; |
605 | |
606 | /* |
9a10ecf4 |
607 | * State associated with packet logging |
608 | */ |
609 | int pktout_logmode; |
610 | int pktout_nblanks; |
611 | struct logblank_t *pktout_blanks; |
612 | |
613 | /* |
51470298 |
614 | * Gross hack: pscp will try to start SFTP but fall back to |
615 | * scp1 if that fails. This variable is the means by which |
616 | * scp.c can reach into the SSH code and find out which one it |
617 | * got. |
618 | */ |
619 | int fallback_cmd; |
620 | |
621 | /* |
622 | * Used for username and password input. |
623 | */ |
624 | char *userpass_input_buffer; |
625 | int userpass_input_buflen; |
626 | int userpass_input_bufpos; |
627 | int userpass_input_echo; |
628 | |
629 | char *portfwd_strptr; |
630 | int pkt_ctx; |
631 | |
302121de |
632 | void *x11auth; |
633 | |
51470298 |
634 | int version; |
635 | int v1_throttle_count; |
636 | int overall_bufsize; |
637 | int throttled_all; |
638 | int v1_stdout_throttling; |
639 | int v2_outgoing_sequence; |
640 | |
641 | int ssh1_rdpkt_crstate; |
642 | int ssh2_rdpkt_crstate; |
643 | int do_ssh_init_crstate; |
644 | int ssh_gotdata_crstate; |
645 | int ssh1_protocol_crstate; |
646 | int do_ssh1_login_crstate; |
647 | int do_ssh2_transport_crstate; |
648 | int do_ssh2_authconn_crstate; |
649 | |
650 | void *do_ssh_init_state; |
651 | void *do_ssh1_login_state; |
652 | void *do_ssh2_transport_state; |
653 | void *do_ssh2_authconn_state; |
654 | |
655 | struct rdpkt1_state_tag rdpkt1_state; |
656 | struct rdpkt2_state_tag rdpkt2_state; |
657 | |
658 | void (*protocol) (Ssh ssh, unsigned char *in, int inlen, int ispkt); |
659 | int (*s_rdpkt) (Ssh ssh, unsigned char **data, int *datalen); |
86916870 |
660 | |
661 | /* |
662 | * We maintain a full _copy_ of a Config structure here, not |
663 | * merely a pointer to it. That way, when we're passed a new |
664 | * one for reconfiguration, we can check the differences and |
665 | * potentially reconfigure port forwardings etc in mid-session. |
666 | */ |
667 | Config cfg; |
839f10db |
668 | |
669 | /* |
670 | * Used to transfer data back from async agent callbacks. |
671 | */ |
672 | void *agent_response; |
673 | int agent_response_len; |
51470298 |
674 | }; |
960e736a |
675 | |
382908ad |
676 | #define logevent(s) logevent(ssh->frontend, s) |
a8327734 |
677 | |
678 | /* logevent, only printf-formatted. */ |
cbe2d68f |
679 | static void logeventf(Ssh ssh, const char *fmt, ...) |
a8327734 |
680 | { |
681 | va_list ap; |
57356d63 |
682 | char *buf; |
a8327734 |
683 | |
684 | va_start(ap, fmt); |
57356d63 |
685 | buf = dupvprintf(fmt, ap); |
a8327734 |
686 | va_end(ap); |
57356d63 |
687 | logevent(buf); |
57356d63 |
688 | sfree(buf); |
a8327734 |
689 | } |
690 | |
6b5cf8b4 |
691 | #define bombout(msg) \ |
692 | do { \ |
693 | char *text = dupprintf msg; \ |
36f94d1f |
694 | ssh_do_close(ssh); \ |
6b5cf8b4 |
695 | logevent(text); \ |
696 | connection_fatal(ssh->frontend, "%s", text); \ |
697 | sfree(text); \ |
698 | } while (0) |
a8327734 |
699 | |
9a10ecf4 |
700 | /* Functions to leave bits out of the SSH packet log file. */ |
701 | |
702 | static void dont_log_password(Ssh ssh, int blanktype) |
703 | { |
704 | if (ssh->cfg.logomitpass) |
705 | ssh->pktout_logmode = blanktype; |
706 | } |
707 | |
708 | static void dont_log_data(Ssh ssh, int blanktype) |
709 | { |
710 | if (ssh->cfg.logomitdata) |
711 | ssh->pktout_logmode = blanktype; |
712 | } |
713 | |
714 | static void end_log_omission(Ssh ssh) |
715 | { |
716 | ssh->pktout_logmode = PKTLOG_EMIT; |
717 | } |
718 | |
32874aea |
719 | static int ssh_channelcmp(void *av, void *bv) |
720 | { |
721 | struct ssh_channel *a = (struct ssh_channel *) av; |
722 | struct ssh_channel *b = (struct ssh_channel *) bv; |
723 | if (a->localid < b->localid) |
724 | return -1; |
725 | if (a->localid > b->localid) |
726 | return +1; |
dacbd0e8 |
727 | return 0; |
728 | } |
32874aea |
729 | static int ssh_channelfind(void *av, void *bv) |
730 | { |
731 | unsigned *a = (unsigned *) av; |
732 | struct ssh_channel *b = (struct ssh_channel *) bv; |
733 | if (*a < b->localid) |
734 | return -1; |
735 | if (*a > b->localid) |
736 | return +1; |
dacbd0e8 |
737 | return 0; |
738 | } |
739 | |
bc240b21 |
740 | static int ssh_rportcmp_ssh1(void *av, void *bv) |
d74d141c |
741 | { |
742 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
743 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
744 | int i; |
bc240b21 |
745 | if ( (i = strcmp(a->dhost, b->dhost)) != 0) |
d74d141c |
746 | return i < 0 ? -1 : +1; |
bc240b21 |
747 | if (a->dport > b->dport) |
748 | return +1; |
749 | if (a->dport < b->dport) |
750 | return -1; |
751 | return 0; |
752 | } |
753 | |
754 | static int ssh_rportcmp_ssh2(void *av, void *bv) |
755 | { |
756 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
757 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
cdcbdf3b |
758 | |
bc240b21 |
759 | if (a->sport > b->sport) |
d74d141c |
760 | return +1; |
bc240b21 |
761 | if (a->sport < b->sport) |
762 | return -1; |
d74d141c |
763 | return 0; |
764 | } |
765 | |
51470298 |
766 | static int alloc_channel_id(Ssh ssh) |
32874aea |
767 | { |
260f3dec |
768 | const unsigned CHANNEL_NUMBER_OFFSET = 256; |
769 | unsigned low, high, mid; |
d2371c81 |
770 | int tsize; |
771 | struct ssh_channel *c; |
772 | |
773 | /* |
774 | * First-fit allocation of channel numbers: always pick the |
775 | * lowest unused one. To do this, binary-search using the |
776 | * counted B-tree to find the largest channel ID which is in a |
777 | * contiguous sequence from the beginning. (Precisely |
778 | * everything in that sequence must have ID equal to its tree |
779 | * index plus CHANNEL_NUMBER_OFFSET.) |
780 | */ |
51470298 |
781 | tsize = count234(ssh->channels); |
d2371c81 |
782 | |
32874aea |
783 | low = -1; |
784 | high = tsize; |
d2371c81 |
785 | while (high - low > 1) { |
786 | mid = (high + low) / 2; |
51470298 |
787 | c = index234(ssh->channels, mid); |
d2371c81 |
788 | if (c->localid == mid + CHANNEL_NUMBER_OFFSET) |
789 | low = mid; /* this one is fine */ |
790 | else |
791 | high = mid; /* this one is past it */ |
792 | } |
793 | /* |
794 | * Now low points to either -1, or the tree index of the |
795 | * largest ID in the initial sequence. |
796 | */ |
797 | { |
798 | unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET; |
51470298 |
799 | assert(NULL == find234(ssh->channels, &i, ssh_channelfind)); |
d2371c81 |
800 | } |
801 | return low + 1 + CHANNEL_NUMBER_OFFSET; |
802 | } |
803 | |
9fab77dc |
804 | static void c_write(Ssh ssh, const char *buf, int len) |
32874aea |
805 | { |
67779be7 |
806 | if ((flags & FLAG_STDERR)) { |
32874aea |
807 | int i; |
808 | for (i = 0; i < len; i++) |
809 | if (buf[i] != '\r') |
810 | fputc(buf[i], stderr); |
fb09bf1c |
811 | return; |
812 | } |
51470298 |
813 | from_backend(ssh->frontend, 1, buf, len); |
3bdaf79d |
814 | } |
815 | |
9fab77dc |
816 | static void c_write_untrusted(Ssh ssh, const char *buf, int len) |
32874aea |
817 | { |
a209e957 |
818 | int i; |
819 | for (i = 0; i < len; i++) { |
32874aea |
820 | if (buf[i] == '\n') |
51470298 |
821 | c_write(ssh, "\r\n", 2); |
32874aea |
822 | else if ((buf[i] & 0x60) || (buf[i] == '\r')) |
51470298 |
823 | c_write(ssh, buf + i, 1); |
a209e957 |
824 | } |
825 | } |
826 | |
9fab77dc |
827 | static void c_write_str(Ssh ssh, const char *buf) |
32874aea |
828 | { |
51470298 |
829 | c_write(ssh, buf, strlen(buf)); |
1408a877 |
830 | } |
831 | |
fb09bf1c |
832 | /* |
833 | * Collect incoming data in the incoming packet buffer. |
e5574168 |
834 | * Decipher and verify the packet when it is completely read. |
835 | * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets. |
fb09bf1c |
836 | * Update the *data and *datalen variables. |
837 | * Return the additional nr of bytes needed, or 0 when |
838 | * a complete packet is available. |
839 | */ |
51470298 |
840 | static int ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
fb09bf1c |
841 | { |
51470298 |
842 | struct rdpkt1_state_tag *st = &ssh->rdpkt1_state; |
374330e2 |
843 | |
51470298 |
844 | crBegin(ssh->ssh1_rdpkt_crstate); |
374330e2 |
845 | |
32874aea |
846 | next_packet: |
37508af4 |
847 | |
51470298 |
848 | ssh->pktin.type = 0; |
849 | ssh->pktin.length = 0; |
374330e2 |
850 | |
57476f6b |
851 | for (st->i = st->len = 0; st->i < 4; st->i++) { |
fb09bf1c |
852 | while ((*datalen) == 0) |
32874aea |
853 | crReturn(4 - st->i); |
57476f6b |
854 | st->len = (st->len << 8) + **data; |
fb09bf1c |
855 | (*data)++, (*datalen)--; |
856 | } |
374330e2 |
857 | |
57476f6b |
858 | st->pad = 8 - (st->len % 8); |
859 | st->biglen = st->len + st->pad; |
51470298 |
860 | ssh->pktin.length = st->len - 5; |
fb09bf1c |
861 | |
ae0500e5 |
862 | if (st->biglen < 0) { |
863 | bombout(("Extremely large packet length from server suggests" |
864 | " data stream corruption")); |
865 | crStop(0); |
866 | } |
867 | |
51470298 |
868 | if (ssh->pktin.maxlen < st->biglen) { |
869 | ssh->pktin.maxlen = st->biglen; |
3d88e64d |
870 | ssh->pktin.data = sresize(ssh->pktin.data, st->biglen + APIEXTRA, |
871 | unsigned char); |
fb09bf1c |
872 | } |
374330e2 |
873 | |
57476f6b |
874 | st->to_read = st->biglen; |
51470298 |
875 | st->p = ssh->pktin.data; |
57476f6b |
876 | while (st->to_read > 0) { |
32874aea |
877 | st->chunk = st->to_read; |
fb09bf1c |
878 | while ((*datalen) == 0) |
57476f6b |
879 | crReturn(st->to_read); |
880 | if (st->chunk > (*datalen)) |
881 | st->chunk = (*datalen); |
882 | memcpy(st->p, *data, st->chunk); |
883 | *data += st->chunk; |
884 | *datalen -= st->chunk; |
885 | st->p += st->chunk; |
886 | st->to_read -= st->chunk; |
fb09bf1c |
887 | } |
374330e2 |
888 | |
0183b242 |
889 | if (ssh->cipher && detect_attack(ssh->crcda_ctx, ssh->pktin.data, |
890 | st->biglen, NULL)) { |
6b5cf8b4 |
891 | bombout(("Network attack (CRC compensation) detected!")); |
7ffdbc1a |
892 | crStop(0); |
9a3a93a5 |
893 | } |
894 | |
51470298 |
895 | if (ssh->cipher) |
371e569c |
896 | ssh->cipher->decrypt(ssh->v1_cipher_ctx, ssh->pktin.data, st->biglen); |
374330e2 |
897 | |
71bedafe |
898 | st->realcrc = crc32_compute(ssh->pktin.data, st->biglen - 4); |
51470298 |
899 | st->gotcrc = GET_32BIT(ssh->pktin.data + st->biglen - 4); |
57476f6b |
900 | if (st->gotcrc != st->realcrc) { |
6b5cf8b4 |
901 | bombout(("Incorrect CRC received on packet")); |
7ffdbc1a |
902 | crStop(0); |
fb09bf1c |
903 | } |
572f871e |
904 | |
51470298 |
905 | ssh->pktin.body = ssh->pktin.data + st->pad + 1; |
0016d70b |
906 | ssh->pktin.savedpos = 0; |
4ba9b64b |
907 | |
51470298 |
908 | if (ssh->v1_compressing) { |
4ba9b64b |
909 | unsigned char *decompblk; |
910 | int decomplen; |
36b8d9bb |
911 | if (!zlib_decompress_block(ssh->sc_comp_ctx, |
912 | ssh->pktin.body - 1, ssh->pktin.length + 1, |
913 | &decompblk, &decomplen)) { |
914 | bombout(("Zlib decompression encountered invalid data")); |
915 | crStop(0); |
916 | } |
4ba9b64b |
917 | |
51470298 |
918 | if (ssh->pktin.maxlen < st->pad + decomplen) { |
919 | ssh->pktin.maxlen = st->pad + decomplen; |
3d88e64d |
920 | ssh->pktin.data = sresize(ssh->pktin.data, |
921 | ssh->pktin.maxlen + APIEXTRA, |
922 | unsigned char); |
51470298 |
923 | ssh->pktin.body = ssh->pktin.data + st->pad + 1; |
4ba9b64b |
924 | } |
925 | |
51470298 |
926 | memcpy(ssh->pktin.body - 1, decompblk, decomplen); |
dcbde236 |
927 | sfree(decompblk); |
51470298 |
928 | ssh->pktin.length = decomplen - 1; |
4ba9b64b |
929 | } |
930 | |
51470298 |
931 | ssh->pktin.type = ssh->pktin.body[-1]; |
00db133f |
932 | |
9a10ecf4 |
933 | /* |
934 | * Log incoming packet, possibly omitting sensitive fields. |
935 | */ |
936 | if (ssh->logctx) { |
937 | int nblanks = 0; |
938 | struct logblank_t blank; |
939 | if (ssh->cfg.logomitdata) { |
940 | int do_blank = FALSE, blank_prefix = 0; |
941 | /* "Session data" packets - omit the data field */ |
942 | if ((ssh->pktin.type == SSH1_SMSG_STDOUT_DATA) || |
943 | (ssh->pktin.type == SSH1_SMSG_STDERR_DATA)) { |
944 | do_blank = TRUE; blank_prefix = 0; |
945 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_DATA) { |
946 | do_blank = TRUE; blank_prefix = 4; |
947 | } |
948 | if (do_blank) { |
949 | blank.offset = blank_prefix; |
950 | blank.len = ssh->pktin.length; |
951 | blank.type = PKTLOG_OMIT; |
952 | nblanks = 1; |
953 | } |
954 | } |
a8327734 |
955 | log_packet(ssh->logctx, |
956 | PKT_INCOMING, ssh->pktin.type, |
957 | ssh1_pkt_type(ssh->pktin.type), |
9a10ecf4 |
958 | ssh->pktin.body, ssh->pktin.length, |
959 | nblanks, &blank); |
960 | } |
00db133f |
961 | |
51470298 |
962 | if (ssh->pktin.type == SSH1_SMSG_STDOUT_DATA || |
963 | ssh->pktin.type == SSH1_SMSG_STDERR_DATA || |
964 | ssh->pktin.type == SSH1_MSG_DEBUG || |
965 | ssh->pktin.type == SSH1_SMSG_AUTH_TIS_CHALLENGE || |
966 | ssh->pktin.type == SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
967 | long stringlen = GET_32BIT(ssh->pktin.body); |
968 | if (stringlen + 4 != ssh->pktin.length) { |
6b5cf8b4 |
969 | bombout(("Received data packet with bogus string length")); |
7ffdbc1a |
970 | crStop(0); |
32874aea |
971 | } |
fb09bf1c |
972 | } |
973 | |
51470298 |
974 | if (ssh->pktin.type == SSH1_MSG_DEBUG) { |
fb09bf1c |
975 | /* log debug message */ |
eb4504cc |
976 | char buf[512]; |
51470298 |
977 | int stringlen = GET_32BIT(ssh->pktin.body); |
eb4504cc |
978 | strcpy(buf, "Remote debug message: "); |
979 | if (stringlen > 480) |
980 | stringlen = 480; |
51470298 |
981 | memcpy(buf + 8, ssh->pktin.body + 4, stringlen); |
3d63ca2e |
982 | buf[8 + stringlen] = '\0'; |
fb09bf1c |
983 | logevent(buf); |
984 | goto next_packet; |
51470298 |
985 | } else if (ssh->pktin.type == SSH1_MSG_IGNORE) { |
fb09bf1c |
986 | /* do nothing */ |
987 | goto next_packet; |
988 | } |
989 | |
51470298 |
990 | if (ssh->pktin.type == SSH1_MSG_DISCONNECT) { |
38c4a8da |
991 | /* log reason code in disconnect message */ |
992 | char buf[256]; |
51470298 |
993 | unsigned msglen = GET_32BIT(ssh->pktin.body); |
260f3dec |
994 | unsigned nowlen; |
38c4a8da |
995 | strcpy(buf, "Remote sent disconnect: "); |
996 | nowlen = strlen(buf); |
32874aea |
997 | if (msglen > sizeof(buf) - nowlen - 1) |
998 | msglen = sizeof(buf) - nowlen - 1; |
51470298 |
999 | memcpy(buf + nowlen, ssh->pktin.body + 4, msglen); |
32874aea |
1000 | buf[nowlen + msglen] = '\0'; |
247308b5 |
1001 | /* logevent(buf); (this is now done within the bombout macro) */ |
6b5cf8b4 |
1002 | bombout(("Server sent disconnect message:\n\"%s\"", buf+nowlen)); |
7ffdbc1a |
1003 | crStop(0); |
38c4a8da |
1004 | } |
1005 | |
fb09bf1c |
1006 | crFinish(0); |
1007 | } |
1008 | |
51470298 |
1009 | static int ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
e5574168 |
1010 | { |
51470298 |
1011 | struct rdpkt2_state_tag *st = &ssh->rdpkt2_state; |
e5574168 |
1012 | |
51470298 |
1013 | crBegin(ssh->ssh2_rdpkt_crstate); |
e5574168 |
1014 | |
32874aea |
1015 | next_packet: |
51470298 |
1016 | ssh->pktin.type = 0; |
1017 | ssh->pktin.length = 0; |
1018 | if (ssh->sccipher) |
1019 | st->cipherblk = ssh->sccipher->blksize; |
e5574168 |
1020 | else |
32874aea |
1021 | st->cipherblk = 8; |
960e736a |
1022 | if (st->cipherblk < 8) |
32874aea |
1023 | st->cipherblk = 8; |
960e736a |
1024 | |
51470298 |
1025 | if (ssh->pktin.maxlen < st->cipherblk) { |
1026 | ssh->pktin.maxlen = st->cipherblk; |
3d88e64d |
1027 | ssh->pktin.data = sresize(ssh->pktin.data, st->cipherblk + APIEXTRA, |
1028 | unsigned char); |
e5574168 |
1029 | } |
1030 | |
1031 | /* |
1032 | * Acquire and decrypt the first block of the packet. This will |
1033 | * contain the length and padding details. |
1034 | */ |
32874aea |
1035 | for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { |
e5574168 |
1036 | while ((*datalen) == 0) |
32874aea |
1037 | crReturn(st->cipherblk - st->i); |
51470298 |
1038 | ssh->pktin.data[st->i] = *(*data)++; |
32874aea |
1039 | (*datalen)--; |
e5574168 |
1040 | } |
4252c9cc |
1041 | |
51470298 |
1042 | if (ssh->sccipher) |
371e569c |
1043 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
1044 | ssh->pktin.data, st->cipherblk); |
e5574168 |
1045 | |
1046 | /* |
1047 | * Now get the length and padding figures. |
1048 | */ |
51470298 |
1049 | st->len = GET_32BIT(ssh->pktin.data); |
1050 | st->pad = ssh->pktin.data[4]; |
e5574168 |
1051 | |
1052 | /* |
717dc483 |
1053 | * _Completely_ silly lengths should be stomped on before they |
1054 | * do us any more damage. |
1055 | */ |
1056 | if (st->len < 0 || st->pad < 0 || st->len + st->pad < 0) { |
6b5cf8b4 |
1057 | bombout(("Incoming packet was garbled on decryption")); |
7ffdbc1a |
1058 | crStop(0); |
717dc483 |
1059 | } |
1060 | |
1061 | /* |
e5574168 |
1062 | * This enables us to deduce the payload length. |
1063 | */ |
960e736a |
1064 | st->payload = st->len - st->pad - 1; |
e5574168 |
1065 | |
51470298 |
1066 | ssh->pktin.length = st->payload + 5; |
e5574168 |
1067 | |
1068 | /* |
1069 | * So now we can work out the total packet length. |
1070 | */ |
960e736a |
1071 | st->packetlen = st->len + 4; |
51470298 |
1072 | st->maclen = ssh->scmac ? ssh->scmac->len : 0; |
e5574168 |
1073 | |
1074 | /* |
1075 | * Adjust memory allocation if packet is too big. |
1076 | */ |
51470298 |
1077 | if (ssh->pktin.maxlen < st->packetlen + st->maclen) { |
1078 | ssh->pktin.maxlen = st->packetlen + st->maclen; |
3d88e64d |
1079 | ssh->pktin.data = sresize(ssh->pktin.data, |
1080 | ssh->pktin.maxlen + APIEXTRA, |
1081 | unsigned char); |
e5574168 |
1082 | } |
1083 | |
1084 | /* |
1085 | * Read and decrypt the remainder of the packet. |
1086 | */ |
32874aea |
1087 | for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; |
1088 | st->i++) { |
e5574168 |
1089 | while ((*datalen) == 0) |
960e736a |
1090 | crReturn(st->packetlen + st->maclen - st->i); |
51470298 |
1091 | ssh->pktin.data[st->i] = *(*data)++; |
32874aea |
1092 | (*datalen)--; |
e5574168 |
1093 | } |
1094 | /* Decrypt everything _except_ the MAC. */ |
51470298 |
1095 | if (ssh->sccipher) |
371e569c |
1096 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
1097 | ssh->pktin.data + st->cipherblk, |
51470298 |
1098 | st->packetlen - st->cipherblk); |
e5574168 |
1099 | |
1100 | /* |
1101 | * Check the MAC. |
1102 | */ |
51470298 |
1103 | if (ssh->scmac |
e0e1a00d |
1104 | && !ssh->scmac->verify(ssh->sc_mac_ctx, ssh->pktin.data, st->len + 4, |
51470298 |
1105 | st->incoming_sequence)) { |
6b5cf8b4 |
1106 | bombout(("Incorrect MAC received on packet")); |
7ffdbc1a |
1107 | crStop(0); |
8d5de777 |
1108 | } |
32874aea |
1109 | st->incoming_sequence++; /* whether or not we MACed */ |
e5574168 |
1110 | |
4ba9b64b |
1111 | /* |
1112 | * Decompress packet payload. |
1113 | */ |
1114 | { |
1115 | unsigned char *newpayload; |
1116 | int newlen; |
51470298 |
1117 | if (ssh->sccomp && |
5366aed8 |
1118 | ssh->sccomp->decompress(ssh->sc_comp_ctx, |
1119 | ssh->pktin.data + 5, ssh->pktin.length - 5, |
51470298 |
1120 | &newpayload, &newlen)) { |
1121 | if (ssh->pktin.maxlen < newlen + 5) { |
1122 | ssh->pktin.maxlen = newlen + 5; |
3d88e64d |
1123 | ssh->pktin.data = sresize(ssh->pktin.data, |
1124 | ssh->pktin.maxlen + APIEXTRA, |
1125 | unsigned char); |
4ba9b64b |
1126 | } |
51470298 |
1127 | ssh->pktin.length = 5 + newlen; |
1128 | memcpy(ssh->pktin.data + 5, newpayload, newlen); |
dcbde236 |
1129 | sfree(newpayload); |
4ba9b64b |
1130 | } |
1131 | } |
1132 | |
51470298 |
1133 | ssh->pktin.savedpos = 6; |
0016d70b |
1134 | ssh->pktin.body = ssh->pktin.data; |
51470298 |
1135 | ssh->pktin.type = ssh->pktin.data[5]; |
e5574168 |
1136 | |
9a10ecf4 |
1137 | /* |
1138 | * Log incoming packet, possibly omitting sensitive fields. |
1139 | */ |
1140 | if (ssh->logctx) { |
1141 | int nblanks = 0; |
1142 | struct logblank_t blank; |
1143 | if (ssh->cfg.logomitdata) { |
1144 | int do_blank = FALSE, blank_prefix = 0; |
1145 | /* "Session data" packets - omit the data field */ |
1146 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_DATA) { |
1147 | do_blank = TRUE; blank_prefix = 4; |
1148 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
1149 | do_blank = TRUE; blank_prefix = 8; |
1150 | } |
1151 | if (do_blank) { |
1152 | blank.offset = blank_prefix; |
1153 | blank.len = (ssh->pktin.length-6) - blank_prefix; |
1154 | blank.type = PKTLOG_OMIT; |
1155 | nblanks = 1; |
1156 | } |
1157 | } |
a8327734 |
1158 | log_packet(ssh->logctx, PKT_INCOMING, ssh->pktin.type, |
1159 | ssh2_pkt_type(ssh->pkt_ctx, ssh->pktin.type), |
9a10ecf4 |
1160 | ssh->pktin.data+6, ssh->pktin.length-6, |
1161 | nblanks, &blank); |
1162 | } |
00db133f |
1163 | |
51470298 |
1164 | switch (ssh->pktin.type) { |
3d63ca2e |
1165 | /* |
1166 | * These packets we must handle instantly. |
1167 | */ |
1168 | case SSH2_MSG_DISCONNECT: |
1169 | { |
1170 | /* log reason code in disconnect message */ |
57356d63 |
1171 | char *buf; |
1172 | int nowlen; |
51470298 |
1173 | int reason = GET_32BIT(ssh->pktin.data + 6); |
1174 | unsigned msglen = GET_32BIT(ssh->pktin.data + 10); |
57356d63 |
1175 | |
3d63ca2e |
1176 | if (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) { |
57356d63 |
1177 | buf = dupprintf("Received disconnect message (%s)", |
1178 | ssh2_disconnect_reasons[reason]); |
3d63ca2e |
1179 | } else { |
57356d63 |
1180 | buf = dupprintf("Received disconnect message (unknown" |
1181 | " type %d)", reason); |
3d63ca2e |
1182 | } |
1183 | logevent(buf); |
57356d63 |
1184 | sfree(buf); |
1185 | buf = dupprintf("Disconnection message text: %n%.*s", |
33b82b22 |
1186 | &nowlen, msglen, ssh->pktin.data + 14); |
3d63ca2e |
1187 | logevent(buf); |
6b5cf8b4 |
1188 | bombout(("Server sent disconnect message\ntype %d (%s):\n\"%s\"", |
3d63ca2e |
1189 | reason, |
1190 | (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) ? |
1191 | ssh2_disconnect_reasons[reason] : "unknown", |
1192 | buf+nowlen)); |
57356d63 |
1193 | sfree(buf); |
7ffdbc1a |
1194 | crStop(0); |
3d63ca2e |
1195 | } |
1196 | break; |
1197 | case SSH2_MSG_IGNORE: |
1198 | goto next_packet; |
1199 | case SSH2_MSG_DEBUG: |
1200 | { |
1201 | /* log the debug message */ |
1202 | char buf[512]; |
51470298 |
1203 | /* int display = ssh->pktin.body[6]; */ |
1204 | int stringlen = GET_32BIT(ssh->pktin.data+7); |
3d63ca2e |
1205 | int prefix; |
1206 | strcpy(buf, "Remote debug message: "); |
1207 | prefix = strlen(buf); |
51470298 |
1208 | if (stringlen > (int)(sizeof(buf)-prefix-1)) |
3d63ca2e |
1209 | stringlen = sizeof(buf)-prefix-1; |
51470298 |
1210 | memcpy(buf + prefix, ssh->pktin.data + 11, stringlen); |
3d63ca2e |
1211 | buf[prefix + stringlen] = '\0'; |
1212 | logevent(buf); |
38c4a8da |
1213 | } |
3d63ca2e |
1214 | goto next_packet; /* FIXME: print the debug message */ |
1215 | |
1216 | /* |
1217 | * These packets we need do nothing about here. |
1218 | */ |
1219 | case SSH2_MSG_UNIMPLEMENTED: |
1220 | case SSH2_MSG_SERVICE_REQUEST: |
1221 | case SSH2_MSG_SERVICE_ACCEPT: |
1222 | case SSH2_MSG_KEXINIT: |
1223 | case SSH2_MSG_NEWKEYS: |
1224 | case SSH2_MSG_KEXDH_INIT: |
1225 | case SSH2_MSG_KEXDH_REPLY: |
1226 | /* case SSH2_MSG_KEX_DH_GEX_REQUEST: duplicate case value */ |
1227 | /* case SSH2_MSG_KEX_DH_GEX_GROUP: duplicate case value */ |
1228 | case SSH2_MSG_KEX_DH_GEX_INIT: |
1229 | case SSH2_MSG_KEX_DH_GEX_REPLY: |
1230 | case SSH2_MSG_USERAUTH_REQUEST: |
1231 | case SSH2_MSG_USERAUTH_FAILURE: |
1232 | case SSH2_MSG_USERAUTH_SUCCESS: |
1233 | case SSH2_MSG_USERAUTH_BANNER: |
1234 | case SSH2_MSG_USERAUTH_PK_OK: |
1235 | /* case SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ: duplicate case value */ |
1236 | /* case SSH2_MSG_USERAUTH_INFO_REQUEST: duplicate case value */ |
1237 | case SSH2_MSG_USERAUTH_INFO_RESPONSE: |
1238 | case SSH2_MSG_GLOBAL_REQUEST: |
1239 | case SSH2_MSG_REQUEST_SUCCESS: |
1240 | case SSH2_MSG_REQUEST_FAILURE: |
1241 | case SSH2_MSG_CHANNEL_OPEN: |
1242 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
1243 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
1244 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
1245 | case SSH2_MSG_CHANNEL_DATA: |
1246 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
1247 | case SSH2_MSG_CHANNEL_EOF: |
1248 | case SSH2_MSG_CHANNEL_CLOSE: |
1249 | case SSH2_MSG_CHANNEL_REQUEST: |
1250 | case SSH2_MSG_CHANNEL_SUCCESS: |
1251 | case SSH2_MSG_CHANNEL_FAILURE: |
1252 | break; |
1253 | |
1254 | /* |
1255 | * For anything else we send SSH2_MSG_UNIMPLEMENTED. |
1256 | */ |
1257 | default: |
51470298 |
1258 | ssh2_pkt_init(ssh, SSH2_MSG_UNIMPLEMENTED); |
1259 | ssh2_pkt_adduint32(ssh, st->incoming_sequence - 1); |
1260 | ssh2_pkt_send(ssh); |
3d63ca2e |
1261 | break; |
38c4a8da |
1262 | } |
1263 | |
e5574168 |
1264 | crFinish(0); |
1265 | } |
1266 | |
51470298 |
1267 | static void ssh1_pktout_size(Ssh ssh, int len) |
32874aea |
1268 | { |
374330e2 |
1269 | int pad, biglen; |
1270 | |
1271 | len += 5; /* type and CRC */ |
32874aea |
1272 | pad = 8 - (len % 8); |
374330e2 |
1273 | biglen = len + pad; |
1274 | |
51470298 |
1275 | ssh->pktout.length = len - 5; |
1276 | if (ssh->pktout.maxlen < biglen) { |
1277 | ssh->pktout.maxlen = biglen; |
8f203108 |
1278 | #ifdef MSCRYPTOAPI |
fb09bf1c |
1279 | /* Allocate enough buffer space for extra block |
8f203108 |
1280 | * for MS CryptEncrypt() */ |
3d88e64d |
1281 | ssh->pktout.data = sresize(ssh->pktout.data, biglen + 12, |
1282 | unsigned char); |
8f203108 |
1283 | #else |
3d88e64d |
1284 | ssh->pktout.data = sresize(ssh->pktout.data, biglen + 4, |
1285 | unsigned char); |
8f203108 |
1286 | #endif |
374330e2 |
1287 | } |
51470298 |
1288 | ssh->pktout.body = ssh->pktout.data + 4 + pad + 1; |
4ba9b64b |
1289 | } |
374330e2 |
1290 | |
51470298 |
1291 | static void s_wrpkt_start(Ssh ssh, int type, int len) |
32874aea |
1292 | { |
51470298 |
1293 | ssh1_pktout_size(ssh, len); |
1294 | ssh->pktout.type = type; |
9a10ecf4 |
1295 | /* Initialise log omission state */ |
1296 | ssh->pktout_nblanks = 0; |
1297 | ssh->pktout_blanks = NULL; |
374330e2 |
1298 | } |
1299 | |
51470298 |
1300 | static int s_wrpkt_prepare(Ssh ssh) |
32874aea |
1301 | { |
fd7a4aad |
1302 | int pad, biglen, i; |
374330e2 |
1303 | unsigned long crc; |
fd7a4aad |
1304 | #ifdef __SC__ |
1305 | /* |
1306 | * XXX various versions of SC (including 8.8.4) screw up the |
1307 | * register allocation in this function and use the same register |
1308 | * (D6) for len and as a temporary, with predictable results. The |
1309 | * following sledgehammer prevents this. |
1310 | */ |
1311 | volatile |
1312 | #endif |
1313 | int len; |
374330e2 |
1314 | |
51470298 |
1315 | ssh->pktout.body[-1] = ssh->pktout.type; |
4ba9b64b |
1316 | |
a8327734 |
1317 | if (ssh->logctx) |
1318 | log_packet(ssh->logctx, PKT_OUTGOING, ssh->pktout.type, |
1319 | ssh1_pkt_type(ssh->pktout.type), |
9a10ecf4 |
1320 | ssh->pktout.body, ssh->pktout.length, |
1321 | ssh->pktout_nblanks, ssh->pktout_blanks); |
1322 | sfree(ssh->pktout_blanks); ssh->pktout_blanks = NULL; |
1323 | ssh->pktout_nblanks = 0; |
00db133f |
1324 | |
51470298 |
1325 | if (ssh->v1_compressing) { |
4ba9b64b |
1326 | unsigned char *compblk; |
1327 | int complen; |
5366aed8 |
1328 | zlib_compress_block(ssh->cs_comp_ctx, |
1329 | ssh->pktout.body - 1, ssh->pktout.length + 1, |
4ba9b64b |
1330 | &compblk, &complen); |
51470298 |
1331 | ssh1_pktout_size(ssh, complen - 1); |
1332 | memcpy(ssh->pktout.body - 1, compblk, complen); |
dcbde236 |
1333 | sfree(compblk); |
4ba9b64b |
1334 | } |
1335 | |
51470298 |
1336 | len = ssh->pktout.length + 5; /* type and CRC */ |
32874aea |
1337 | pad = 8 - (len % 8); |
374330e2 |
1338 | biglen = len + pad; |
1339 | |
32874aea |
1340 | for (i = 0; i < pad; i++) |
51470298 |
1341 | ssh->pktout.data[i + 4] = random_byte(); |
71bedafe |
1342 | crc = crc32_compute(ssh->pktout.data + 4, biglen - 4); |
51470298 |
1343 | PUT_32BIT(ssh->pktout.data + biglen, crc); |
1344 | PUT_32BIT(ssh->pktout.data, len); |
374330e2 |
1345 | |
51470298 |
1346 | if (ssh->cipher) |
371e569c |
1347 | ssh->cipher->encrypt(ssh->v1_cipher_ctx, ssh->pktout.data + 4, biglen); |
374330e2 |
1348 | |
32874aea |
1349 | return biglen + 4; |
39065bed |
1350 | } |
1351 | |
51470298 |
1352 | static void s_wrpkt(Ssh ssh) |
32874aea |
1353 | { |
5471d09a |
1354 | int len, backlog; |
51470298 |
1355 | len = s_wrpkt_prepare(ssh); |
d8baa528 |
1356 | backlog = sk_write(ssh->s, (char *)ssh->pktout.data, len); |
5471d09a |
1357 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1358 | ssh_throttle_all(ssh, 1, backlog); |
39065bed |
1359 | } |
1360 | |
51470298 |
1361 | static void s_wrpkt_defer(Ssh ssh) |
32874aea |
1362 | { |
39065bed |
1363 | int len; |
51470298 |
1364 | len = s_wrpkt_prepare(ssh); |
1365 | if (ssh->deferred_len + len > ssh->deferred_size) { |
1366 | ssh->deferred_size = ssh->deferred_len + len + 128; |
3d88e64d |
1367 | ssh->deferred_send_data = sresize(ssh->deferred_send_data, |
1368 | ssh->deferred_size, |
1369 | unsigned char); |
39065bed |
1370 | } |
51470298 |
1371 | memcpy(ssh->deferred_send_data + ssh->deferred_len, ssh->pktout.data, len); |
1372 | ssh->deferred_len += len; |
374330e2 |
1373 | } |
1374 | |
fb09bf1c |
1375 | /* |
39065bed |
1376 | * Construct a packet with the specified contents. |
fb09bf1c |
1377 | */ |
51470298 |
1378 | static void construct_packet(Ssh ssh, int pkttype, va_list ap1, va_list ap2) |
fb09bf1c |
1379 | { |
fb09bf1c |
1380 | unsigned char *p, *argp, argchar; |
1381 | unsigned long argint; |
1382 | int pktlen, argtype, arglen; |
7cca0d81 |
1383 | Bignum bn; |
fb09bf1c |
1384 | |
1385 | pktlen = 0; |
39065bed |
1386 | while ((argtype = va_arg(ap1, int)) != PKT_END) { |
fb09bf1c |
1387 | switch (argtype) { |
1388 | case PKT_INT: |
39065bed |
1389 | (void) va_arg(ap1, int); |
fb09bf1c |
1390 | pktlen += 4; |
1391 | break; |
1392 | case PKT_CHAR: |
1ec39246 |
1393 | (void) va_arg(ap1, int); |
fb09bf1c |
1394 | pktlen++; |
1395 | break; |
1396 | case PKT_DATA: |
39065bed |
1397 | (void) va_arg(ap1, unsigned char *); |
1398 | arglen = va_arg(ap1, int); |
fb09bf1c |
1399 | pktlen += arglen; |
1400 | break; |
1401 | case PKT_STR: |
39065bed |
1402 | argp = va_arg(ap1, unsigned char *); |
d8baa528 |
1403 | arglen = strlen((char *)argp); |
fb09bf1c |
1404 | pktlen += 4 + arglen; |
1405 | break; |
7cca0d81 |
1406 | case PKT_BIGNUM: |
39065bed |
1407 | bn = va_arg(ap1, Bignum); |
32874aea |
1408 | pktlen += ssh1_bignum_length(bn); |
7cca0d81 |
1409 | break; |
9a10ecf4 |
1410 | case PKTT_PASSWORD: |
1411 | case PKTT_DATA: |
1412 | case PKTT_OTHER: |
1413 | /* ignore this pass */ |
1414 | break; |
fb09bf1c |
1415 | default: |
1416 | assert(0); |
1417 | } |
1418 | } |
fb09bf1c |
1419 | |
51470298 |
1420 | s_wrpkt_start(ssh, pkttype, pktlen); |
1421 | p = ssh->pktout.body; |
fb09bf1c |
1422 | |
39065bed |
1423 | while ((argtype = va_arg(ap2, int)) != PKT_END) { |
9a10ecf4 |
1424 | int offset = p - ssh->pktout.body, len = 0; |
fb09bf1c |
1425 | switch (argtype) { |
9a10ecf4 |
1426 | /* Actual fields in the packet */ |
fb09bf1c |
1427 | case PKT_INT: |
39065bed |
1428 | argint = va_arg(ap2, int); |
fb09bf1c |
1429 | PUT_32BIT(p, argint); |
9a10ecf4 |
1430 | len = 4; |
fb09bf1c |
1431 | break; |
1432 | case PKT_CHAR: |
1ec39246 |
1433 | argchar = (unsigned char) va_arg(ap2, int); |
fb09bf1c |
1434 | *p = argchar; |
9a10ecf4 |
1435 | len = 1; |
fb09bf1c |
1436 | break; |
1437 | case PKT_DATA: |
39065bed |
1438 | argp = va_arg(ap2, unsigned char *); |
1439 | arglen = va_arg(ap2, int); |
fb09bf1c |
1440 | memcpy(p, argp, arglen); |
9a10ecf4 |
1441 | len = arglen; |
fb09bf1c |
1442 | break; |
1443 | case PKT_STR: |
39065bed |
1444 | argp = va_arg(ap2, unsigned char *); |
d8baa528 |
1445 | arglen = strlen((char *)argp); |
fb09bf1c |
1446 | PUT_32BIT(p, arglen); |
1447 | memcpy(p + 4, argp, arglen); |
9a10ecf4 |
1448 | len = arglen + 4; |
fb09bf1c |
1449 | break; |
7cca0d81 |
1450 | case PKT_BIGNUM: |
39065bed |
1451 | bn = va_arg(ap2, Bignum); |
9a10ecf4 |
1452 | len = ssh1_write_bignum(p, bn); |
1453 | break; |
1454 | /* Tokens for modifications to packet logging */ |
1455 | case PKTT_PASSWORD: |
1456 | dont_log_password(ssh, PKTLOG_BLANK); |
1457 | break; |
1458 | case PKTT_DATA: |
1459 | dont_log_data(ssh, PKTLOG_OMIT); |
7cca0d81 |
1460 | break; |
9a10ecf4 |
1461 | case PKTT_OTHER: |
1462 | end_log_omission(ssh); |
1463 | break; |
1464 | } |
1465 | p += len; |
1466 | /* Deal with logfile omission, if required. */ |
1467 | if (len && (ssh->pktout_logmode != PKTLOG_EMIT)) { |
1468 | ssh->pktout_nblanks++; |
1469 | ssh->pktout_blanks = sresize(ssh->pktout_blanks, |
1470 | ssh->pktout_nblanks, |
1471 | struct logblank_t); |
1472 | ssh->pktout_blanks[ssh->pktout_nblanks-1].offset = offset; |
1473 | ssh->pktout_blanks[ssh->pktout_nblanks-1].len = len; |
1474 | ssh->pktout_blanks[ssh->pktout_nblanks-1].type = |
1475 | ssh->pktout_logmode; |
fb09bf1c |
1476 | } |
1477 | } |
39065bed |
1478 | } |
fb09bf1c |
1479 | |
51470298 |
1480 | static void send_packet(Ssh ssh, int pkttype, ...) |
32874aea |
1481 | { |
39065bed |
1482 | va_list ap1, ap2; |
1483 | va_start(ap1, pkttype); |
1484 | va_start(ap2, pkttype); |
51470298 |
1485 | construct_packet(ssh, pkttype, ap1, ap2); |
1486 | s_wrpkt(ssh); |
fb09bf1c |
1487 | } |
1488 | |
51470298 |
1489 | static void defer_packet(Ssh ssh, int pkttype, ...) |
32874aea |
1490 | { |
39065bed |
1491 | va_list ap1, ap2; |
1492 | va_start(ap1, pkttype); |
1493 | va_start(ap2, pkttype); |
51470298 |
1494 | construct_packet(ssh, pkttype, ap1, ap2); |
1495 | s_wrpkt_defer(ssh); |
39065bed |
1496 | } |
1497 | |
32874aea |
1498 | static int ssh_versioncmp(char *a, char *b) |
1499 | { |
9697bfd2 |
1500 | char *ae, *be; |
1501 | unsigned long av, bv; |
1502 | |
43aa02a7 |
1503 | av = strtoul(a, &ae, 10); |
1504 | bv = strtoul(b, &be, 10); |
32874aea |
1505 | if (av != bv) |
1506 | return (av < bv ? -1 : +1); |
1507 | if (*ae == '.') |
1508 | ae++; |
1509 | if (*be == '.') |
1510 | be++; |
43aa02a7 |
1511 | av = strtoul(ae, &ae, 10); |
1512 | bv = strtoul(be, &be, 10); |
32874aea |
1513 | if (av != bv) |
1514 | return (av < bv ? -1 : +1); |
9697bfd2 |
1515 | return 0; |
1516 | } |
1517 | |
e5574168 |
1518 | /* |
a92dd380 |
1519 | * Utility routines for putting an SSH-protocol `string' and |
1520 | * `uint32' into a SHA state. |
e5574168 |
1521 | */ |
1522 | #include <stdio.h> |
32874aea |
1523 | static void sha_string(SHA_State * s, void *str, int len) |
1524 | { |
e5574168 |
1525 | unsigned char lenblk[4]; |
e5574168 |
1526 | PUT_32BIT(lenblk, len); |
e5574168 |
1527 | SHA_Bytes(s, lenblk, 4); |
e5574168 |
1528 | SHA_Bytes(s, str, len); |
1529 | } |
1530 | |
32874aea |
1531 | static void sha_uint32(SHA_State * s, unsigned i) |
1532 | { |
a92dd380 |
1533 | unsigned char intblk[4]; |
1534 | PUT_32BIT(intblk, i); |
1535 | SHA_Bytes(s, intblk, 4); |
1536 | } |
1537 | |
7cca0d81 |
1538 | /* |
1539 | * SSH2 packet construction functions. |
1540 | */ |
51470298 |
1541 | static void ssh2_pkt_ensure(Ssh ssh, int length) |
32874aea |
1542 | { |
51470298 |
1543 | if (ssh->pktout.maxlen < length) { |
1544 | ssh->pktout.maxlen = length + 256; |
3d88e64d |
1545 | ssh->pktout.data = sresize(ssh->pktout.data, |
1546 | ssh->pktout.maxlen + APIEXTRA, |
1547 | unsigned char); |
51470298 |
1548 | if (!ssh->pktout.data) |
32874aea |
1549 | fatalbox("Out of memory"); |
7cca0d81 |
1550 | } |
783415f8 |
1551 | } |
51470298 |
1552 | static void ssh2_pkt_adddata(Ssh ssh, void *data, int len) |
32874aea |
1553 | { |
9a10ecf4 |
1554 | if (ssh->pktout_logmode != PKTLOG_EMIT) { |
1555 | ssh->pktout_nblanks++; |
1556 | ssh->pktout_blanks = sresize(ssh->pktout_blanks, ssh->pktout_nblanks, |
1557 | struct logblank_t); |
1558 | ssh->pktout_blanks[ssh->pktout_nblanks-1].offset = |
1559 | ssh->pktout.length - 6; |
1560 | ssh->pktout_blanks[ssh->pktout_nblanks-1].len = len; |
1561 | ssh->pktout_blanks[ssh->pktout_nblanks-1].type = ssh->pktout_logmode; |
1562 | } |
51470298 |
1563 | ssh->pktout.length += len; |
1564 | ssh2_pkt_ensure(ssh, ssh->pktout.length); |
1565 | memcpy(ssh->pktout.data + ssh->pktout.length - len, data, len); |
7cca0d81 |
1566 | } |
51470298 |
1567 | static void ssh2_pkt_addbyte(Ssh ssh, unsigned char byte) |
32874aea |
1568 | { |
51470298 |
1569 | ssh2_pkt_adddata(ssh, &byte, 1); |
7cca0d81 |
1570 | } |
51470298 |
1571 | static void ssh2_pkt_init(Ssh ssh, int pkt_type) |
32874aea |
1572 | { |
51470298 |
1573 | ssh->pktout.length = 5; |
9a10ecf4 |
1574 | ssh->pktout_nblanks = 0; ssh->pktout_blanks = NULL; |
51470298 |
1575 | ssh2_pkt_addbyte(ssh, (unsigned char) pkt_type); |
7cca0d81 |
1576 | } |
51470298 |
1577 | static void ssh2_pkt_addbool(Ssh ssh, unsigned char value) |
32874aea |
1578 | { |
51470298 |
1579 | ssh2_pkt_adddata(ssh, &value, 1); |
7cca0d81 |
1580 | } |
51470298 |
1581 | static void ssh2_pkt_adduint32(Ssh ssh, unsigned long value) |
32874aea |
1582 | { |
7cca0d81 |
1583 | unsigned char x[4]; |
1584 | PUT_32BIT(x, value); |
51470298 |
1585 | ssh2_pkt_adddata(ssh, x, 4); |
7cca0d81 |
1586 | } |
51470298 |
1587 | static void ssh2_pkt_addstring_start(Ssh ssh) |
32874aea |
1588 | { |
51470298 |
1589 | ssh2_pkt_adduint32(ssh, 0); |
1590 | ssh->pktout.savedpos = ssh->pktout.length; |
7cca0d81 |
1591 | } |
51470298 |
1592 | static void ssh2_pkt_addstring_str(Ssh ssh, char *data) |
32874aea |
1593 | { |
51470298 |
1594 | ssh2_pkt_adddata(ssh, data, strlen(data)); |
1595 | PUT_32BIT(ssh->pktout.data + ssh->pktout.savedpos - 4, |
1596 | ssh->pktout.length - ssh->pktout.savedpos); |
7cca0d81 |
1597 | } |
51470298 |
1598 | static void ssh2_pkt_addstring_data(Ssh ssh, char *data, int len) |
32874aea |
1599 | { |
51470298 |
1600 | ssh2_pkt_adddata(ssh, data, len); |
1601 | PUT_32BIT(ssh->pktout.data + ssh->pktout.savedpos - 4, |
1602 | ssh->pktout.length - ssh->pktout.savedpos); |
7cca0d81 |
1603 | } |
51470298 |
1604 | static void ssh2_pkt_addstring(Ssh ssh, char *data) |
32874aea |
1605 | { |
51470298 |
1606 | ssh2_pkt_addstring_start(ssh); |
1607 | ssh2_pkt_addstring_str(ssh, data); |
7cca0d81 |
1608 | } |
d8baa528 |
1609 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len) |
32874aea |
1610 | { |
7cca0d81 |
1611 | unsigned char *p; |
32874aea |
1612 | int i, n = (bignum_bitcount(b) + 7) / 8; |
3d88e64d |
1613 | p = snewn(n + 1, unsigned char); |
7cca0d81 |
1614 | if (!p) |
32874aea |
1615 | fatalbox("out of memory"); |
7cca0d81 |
1616 | p[0] = 0; |
3709bfe9 |
1617 | for (i = 1; i <= n; i++) |
32874aea |
1618 | p[i] = bignum_byte(b, n - i); |
7cca0d81 |
1619 | i = 0; |
32874aea |
1620 | while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0) |
1621 | i++; |
1622 | memmove(p, p + i, n + 1 - i); |
1623 | *len = n + 1 - i; |
7cca0d81 |
1624 | return p; |
1625 | } |
51470298 |
1626 | static void ssh2_pkt_addmp(Ssh ssh, Bignum b) |
32874aea |
1627 | { |
7cca0d81 |
1628 | unsigned char *p; |
1629 | int len; |
1630 | p = ssh2_mpint_fmt(b, &len); |
51470298 |
1631 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
1632 | ssh2_pkt_addstring_data(ssh, (char *)p, len); |
dcbde236 |
1633 | sfree(p); |
7cca0d81 |
1634 | } |
b185170a |
1635 | |
1636 | /* |
1637 | * Construct an SSH2 final-form packet: compress it, encrypt it, |
1638 | * put the MAC on it. Final packet, ready to be sent, is stored in |
51470298 |
1639 | * ssh->pktout.data. Total length is returned. |
b185170a |
1640 | */ |
51470298 |
1641 | static int ssh2_pkt_construct(Ssh ssh) |
32874aea |
1642 | { |
7cca0d81 |
1643 | int cipherblk, maclen, padding, i; |
7cca0d81 |
1644 | |
a8327734 |
1645 | if (ssh->logctx) |
1646 | log_packet(ssh->logctx, PKT_OUTGOING, ssh->pktout.data[5], |
1647 | ssh2_pkt_type(ssh->pkt_ctx, ssh->pktout.data[5]), |
9a10ecf4 |
1648 | ssh->pktout.data + 6, ssh->pktout.length - 6, |
1649 | ssh->pktout_nblanks, ssh->pktout_blanks); |
1650 | sfree(ssh->pktout_blanks); ssh->pktout_blanks = NULL; |
1651 | ssh->pktout_nblanks = 0; |
00db133f |
1652 | |
7cca0d81 |
1653 | /* |
4ba9b64b |
1654 | * Compress packet payload. |
1655 | */ |
4ba9b64b |
1656 | { |
1657 | unsigned char *newpayload; |
1658 | int newlen; |
51470298 |
1659 | if (ssh->cscomp && |
5366aed8 |
1660 | ssh->cscomp->compress(ssh->cs_comp_ctx, ssh->pktout.data + 5, |
1661 | ssh->pktout.length - 5, |
51470298 |
1662 | &newpayload, &newlen)) { |
1663 | ssh->pktout.length = 5; |
1664 | ssh2_pkt_adddata(ssh, newpayload, newlen); |
dcbde236 |
1665 | sfree(newpayload); |
4ba9b64b |
1666 | } |
1667 | } |
1668 | |
1669 | /* |
7cca0d81 |
1670 | * Add padding. At least four bytes, and must also bring total |
1671 | * length (minus MAC) up to a multiple of the block size. |
1672 | */ |
51470298 |
1673 | cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */ |
32874aea |
1674 | cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ |
7cca0d81 |
1675 | padding = 4; |
32874aea |
1676 | padding += |
51470298 |
1677 | (cipherblk - (ssh->pktout.length + padding) % cipherblk) % cipherblk; |
1678 | maclen = ssh->csmac ? ssh->csmac->len : 0; |
1679 | ssh2_pkt_ensure(ssh, ssh->pktout.length + padding + maclen); |
1680 | ssh->pktout.data[4] = padding; |
7cca0d81 |
1681 | for (i = 0; i < padding; i++) |
51470298 |
1682 | ssh->pktout.data[ssh->pktout.length + i] = random_byte(); |
1683 | PUT_32BIT(ssh->pktout.data, ssh->pktout.length + padding - 4); |
1684 | if (ssh->csmac) |
e0e1a00d |
1685 | ssh->csmac->generate(ssh->cs_mac_ctx, ssh->pktout.data, |
1686 | ssh->pktout.length + padding, |
51470298 |
1687 | ssh->v2_outgoing_sequence); |
1688 | ssh->v2_outgoing_sequence++; /* whether or not we MACed */ |
1689 | |
1690 | if (ssh->cscipher) |
371e569c |
1691 | ssh->cscipher->encrypt(ssh->cs_cipher_ctx, |
1692 | ssh->pktout.data, ssh->pktout.length + padding); |
51470298 |
1693 | |
1694 | /* Ready-to-send packet starts at ssh->pktout.data. We return length. */ |
1695 | return ssh->pktout.length + padding + maclen; |
b185170a |
1696 | } |
1697 | |
1698 | /* |
1699 | * Construct and send an SSH2 packet immediately. |
1700 | */ |
51470298 |
1701 | static void ssh2_pkt_send(Ssh ssh) |
32874aea |
1702 | { |
5471d09a |
1703 | int len; |
1704 | int backlog; |
51470298 |
1705 | len = ssh2_pkt_construct(ssh); |
d8baa528 |
1706 | backlog = sk_write(ssh->s, (char *)ssh->pktout.data, len); |
5471d09a |
1707 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1708 | ssh_throttle_all(ssh, 1, backlog); |
b185170a |
1709 | } |
1710 | |
1711 | /* |
1712 | * Construct an SSH2 packet and add it to a deferred data block. |
1713 | * Useful for sending multiple packets in a single sk_write() call, |
1714 | * to prevent a traffic-analysing listener from being able to work |
1715 | * out the length of any particular packet (such as the password |
1716 | * packet). |
1717 | * |
1718 | * Note that because SSH2 sequence-numbers its packets, this can |
1719 | * NOT be used as an m4-style `defer' allowing packets to be |
1720 | * constructed in one order and sent in another. |
1721 | */ |
51470298 |
1722 | static void ssh2_pkt_defer(Ssh ssh) |
32874aea |
1723 | { |
51470298 |
1724 | int len = ssh2_pkt_construct(ssh); |
1725 | if (ssh->deferred_len + len > ssh->deferred_size) { |
1726 | ssh->deferred_size = ssh->deferred_len + len + 128; |
3d88e64d |
1727 | ssh->deferred_send_data = sresize(ssh->deferred_send_data, |
1728 | ssh->deferred_size, |
1729 | unsigned char); |
b185170a |
1730 | } |
51470298 |
1731 | memcpy(ssh->deferred_send_data + ssh->deferred_len, ssh->pktout.data, len); |
1732 | ssh->deferred_len += len; |
b185170a |
1733 | } |
1734 | |
1735 | /* |
1736 | * Send the whole deferred data block constructed by |
39065bed |
1737 | * ssh2_pkt_defer() or SSH1's defer_packet(). |
b185170a |
1738 | */ |
51470298 |
1739 | static void ssh_pkt_defersend(Ssh ssh) |
32874aea |
1740 | { |
5471d09a |
1741 | int backlog; |
d8baa528 |
1742 | backlog = sk_write(ssh->s, (char *)ssh->deferred_send_data, |
1743 | ssh->deferred_len); |
51470298 |
1744 | ssh->deferred_len = ssh->deferred_size = 0; |
1745 | sfree(ssh->deferred_send_data); |
1746 | ssh->deferred_send_data = NULL; |
5471d09a |
1747 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1748 | ssh_throttle_all(ssh, 1, backlog); |
7cca0d81 |
1749 | } |
1750 | |
1751 | #if 0 |
32874aea |
1752 | void bndebug(char *string, Bignum b) |
1753 | { |
7cca0d81 |
1754 | unsigned char *p; |
1755 | int i, len; |
1756 | p = ssh2_mpint_fmt(b, &len); |
1757 | debug(("%s", string)); |
1758 | for (i = 0; i < len; i++) |
32874aea |
1759 | debug((" %02x", p[i])); |
765c4200 |
1760 | debug(("\n")); |
dcbde236 |
1761 | sfree(p); |
7cca0d81 |
1762 | } |
1763 | #endif |
1764 | |
32874aea |
1765 | static void sha_mpint(SHA_State * s, Bignum b) |
1766 | { |
7cca0d81 |
1767 | unsigned char *p; |
1768 | int len; |
1769 | p = ssh2_mpint_fmt(b, &len); |
1770 | sha_string(s, p, len); |
dcbde236 |
1771 | sfree(p); |
7cca0d81 |
1772 | } |
1773 | |
1774 | /* |
0016d70b |
1775 | * Packet decode functions for both SSH1 and SSH2. |
7cca0d81 |
1776 | */ |
0016d70b |
1777 | static unsigned long ssh_pkt_getuint32(Ssh ssh) |
32874aea |
1778 | { |
7cca0d81 |
1779 | unsigned long value; |
51470298 |
1780 | if (ssh->pktin.length - ssh->pktin.savedpos < 4) |
32874aea |
1781 | return 0; /* arrgh, no way to decline (FIXME?) */ |
0016d70b |
1782 | value = GET_32BIT(ssh->pktin.body + ssh->pktin.savedpos); |
51470298 |
1783 | ssh->pktin.savedpos += 4; |
7cca0d81 |
1784 | return value; |
1785 | } |
51470298 |
1786 | static int ssh2_pkt_getbool(Ssh ssh) |
32874aea |
1787 | { |
65a22376 |
1788 | unsigned long value; |
51470298 |
1789 | if (ssh->pktin.length - ssh->pktin.savedpos < 1) |
32874aea |
1790 | return 0; /* arrgh, no way to decline (FIXME?) */ |
0016d70b |
1791 | value = ssh->pktin.body[ssh->pktin.savedpos] != 0; |
51470298 |
1792 | ssh->pktin.savedpos++; |
65a22376 |
1793 | return value; |
1794 | } |
0016d70b |
1795 | static void ssh_pkt_getstring(Ssh ssh, char **p, int *length) |
32874aea |
1796 | { |
57356d63 |
1797 | int len; |
7cca0d81 |
1798 | *p = NULL; |
45068b27 |
1799 | *length = 0; |
51470298 |
1800 | if (ssh->pktin.length - ssh->pktin.savedpos < 4) |
32874aea |
1801 | return; |
0016d70b |
1802 | len = GET_32BIT(ssh->pktin.body + ssh->pktin.savedpos); |
57356d63 |
1803 | if (len < 0) |
1804 | return; |
1805 | *length = len; |
51470298 |
1806 | ssh->pktin.savedpos += 4; |
1807 | if (ssh->pktin.length - ssh->pktin.savedpos < *length) |
32874aea |
1808 | return; |
0016d70b |
1809 | *p = (char *)(ssh->pktin.body + ssh->pktin.savedpos); |
51470298 |
1810 | ssh->pktin.savedpos += *length; |
7cca0d81 |
1811 | } |
0016d70b |
1812 | static void *ssh_pkt_getdata(Ssh ssh, int length) |
1813 | { |
1814 | if (ssh->pktin.length - ssh->pktin.savedpos < length) |
1815 | return NULL; |
1816 | ssh->pktin.savedpos += length; |
1817 | return ssh->pktin.body + (ssh->pktin.savedpos - length); |
1818 | } |
1819 | static int ssh1_pkt_getrsakey(Ssh ssh, struct RSAKey *key, |
1820 | unsigned char **keystr) |
1821 | { |
1822 | int j; |
1823 | |
1824 | j = makekey(ssh->pktin.body + ssh->pktin.savedpos, |
1825 | ssh->pktin.length - ssh->pktin.savedpos, |
1826 | key, keystr, 0); |
1827 | |
1828 | if (j < 0) |
1829 | return FALSE; |
1830 | |
1831 | ssh->pktin.savedpos += j; |
1832 | assert(ssh->pktin.savedpos < ssh->pktin.length); |
1833 | |
1834 | return TRUE; |
1835 | } |
1836 | static Bignum ssh1_pkt_getmp(Ssh ssh) |
1837 | { |
1838 | int j; |
1839 | Bignum b; |
1840 | |
1841 | j = ssh1_read_bignum(ssh->pktin.body + ssh->pktin.savedpos, |
1842 | ssh->pktin.length - ssh->pktin.savedpos, &b); |
1843 | |
1844 | if (j < 0) |
1845 | return NULL; |
1846 | |
1847 | ssh->pktin.savedpos += j; |
1848 | return b; |
1849 | } |
51470298 |
1850 | static Bignum ssh2_pkt_getmp(Ssh ssh) |
32874aea |
1851 | { |
7cca0d81 |
1852 | char *p; |
3709bfe9 |
1853 | int length; |
7cca0d81 |
1854 | Bignum b; |
1855 | |
0016d70b |
1856 | ssh_pkt_getstring(ssh, &p, &length); |
7cca0d81 |
1857 | if (!p) |
32874aea |
1858 | return NULL; |
8d5de777 |
1859 | if (p[0] & 0x80) { |
6b5cf8b4 |
1860 | bombout(("internal error: Can't handle negative mpints")); |
32874aea |
1861 | return NULL; |
8d5de777 |
1862 | } |
d8baa528 |
1863 | b = bignum_from_bytes((unsigned char *)p, length); |
7cca0d81 |
1864 | return b; |
1865 | } |
1866 | |
7d503c31 |
1867 | /* |
1dd353b5 |
1868 | * Helper function to add an SSH2 signature blob to a packet. |
1869 | * Expects to be shown the public key blob as well as the signature |
1870 | * blob. Normally works just like ssh2_pkt_addstring, but will |
1871 | * fiddle with the signature packet if necessary for |
1872 | * BUG_SSH2_RSA_PADDING. |
1873 | */ |
51470298 |
1874 | static void ssh2_add_sigblob(Ssh ssh, void *pkblob_v, int pkblob_len, |
1dd353b5 |
1875 | void *sigblob_v, int sigblob_len) |
1876 | { |
1877 | unsigned char *pkblob = (unsigned char *)pkblob_v; |
1878 | unsigned char *sigblob = (unsigned char *)sigblob_v; |
1879 | |
1880 | /* dmemdump(pkblob, pkblob_len); */ |
1881 | /* dmemdump(sigblob, sigblob_len); */ |
1882 | |
1883 | /* |
1884 | * See if this is in fact an ssh-rsa signature and a buggy |
1885 | * server; otherwise we can just do this the easy way. |
1886 | */ |
51470298 |
1887 | if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) && |
1dd353b5 |
1888 | (GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) { |
1889 | int pos, len, siglen; |
1890 | |
1891 | /* |
1892 | * Find the byte length of the modulus. |
1893 | */ |
1894 | |
1895 | pos = 4+7; /* skip over "ssh-rsa" */ |
1896 | pos += 4 + GET_32BIT(pkblob+pos); /* skip over exponent */ |
1897 | len = GET_32BIT(pkblob+pos); /* find length of modulus */ |
1898 | pos += 4; /* find modulus itself */ |
1899 | while (len > 0 && pkblob[pos] == 0) |
1900 | len--, pos++; |
1901 | /* debug(("modulus length is %d\n", len)); */ |
1902 | |
1903 | /* |
1904 | * Now find the signature integer. |
1905 | */ |
1906 | pos = 4+7; /* skip over "ssh-rsa" */ |
1907 | siglen = GET_32BIT(sigblob+pos); |
1908 | /* debug(("signature length is %d\n", siglen)); */ |
1909 | |
1910 | if (len != siglen) { |
1911 | unsigned char newlen[4]; |
51470298 |
1912 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
1913 | ssh2_pkt_addstring_data(ssh, (char *)sigblob, pos); |
1dd353b5 |
1914 | /* dmemdump(sigblob, pos); */ |
1915 | pos += 4; /* point to start of actual sig */ |
1916 | PUT_32BIT(newlen, len); |
d8baa528 |
1917 | ssh2_pkt_addstring_data(ssh, (char *)newlen, 4); |
1dd353b5 |
1918 | /* dmemdump(newlen, 4); */ |
1919 | newlen[0] = 0; |
1920 | while (len-- > siglen) { |
d8baa528 |
1921 | ssh2_pkt_addstring_data(ssh, (char *)newlen, 1); |
1dd353b5 |
1922 | /* dmemdump(newlen, 1); */ |
1923 | } |
d8baa528 |
1924 | ssh2_pkt_addstring_data(ssh, (char *)(sigblob+pos), siglen); |
1dd353b5 |
1925 | /* dmemdump(sigblob+pos, siglen); */ |
1926 | return; |
1927 | } |
1928 | |
1929 | /* Otherwise fall through and do it the easy way. */ |
1930 | } |
1931 | |
51470298 |
1932 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
1933 | ssh2_pkt_addstring_data(ssh, (char *)sigblob, sigblob_len); |
1dd353b5 |
1934 | } |
1935 | |
1936 | /* |
7d503c31 |
1937 | * Examine the remote side's version string and compare it against |
1938 | * a list of known buggy implementations. |
1939 | */ |
51470298 |
1940 | static void ssh_detect_bugs(Ssh ssh, char *vstring) |
32874aea |
1941 | { |
1942 | char *imp; /* pointer to implementation part */ |
7d503c31 |
1943 | imp = vstring; |
1944 | imp += strcspn(imp, "-"); |
bd358db1 |
1945 | if (*imp) imp++; |
7d503c31 |
1946 | imp += strcspn(imp, "-"); |
bd358db1 |
1947 | if (*imp) imp++; |
7d503c31 |
1948 | |
51470298 |
1949 | ssh->remote_bugs = 0; |
7d503c31 |
1950 | |
5ecd7ad0 |
1951 | if (ssh->cfg.sshbug_ignore1 == FORCE_ON || |
1952 | (ssh->cfg.sshbug_ignore1 == AUTO && |
2c9c6388 |
1953 | (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") || |
1954 | !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") || |
46ac09aa |
1955 | !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25") || |
bd0b4caf |
1956 | !strcmp(imp, "OSU_1.4alpha3") || !strcmp(imp, "OSU_1.5alpha4")))) { |
32874aea |
1957 | /* |
1958 | * These versions don't support SSH1_MSG_IGNORE, so we have |
1959 | * to use a different defence against password length |
1960 | * sniffing. |
1961 | */ |
51470298 |
1962 | ssh->remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE; |
32874aea |
1963 | logevent("We believe remote version has SSH1 ignore bug"); |
7d503c31 |
1964 | } |
1965 | |
5ecd7ad0 |
1966 | if (ssh->cfg.sshbug_plainpw1 == FORCE_ON || |
1967 | (ssh->cfg.sshbug_plainpw1 == AUTO && |
46ac09aa |
1968 | (!strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3")))) { |
bd358db1 |
1969 | /* |
1970 | * These versions need a plain password sent; they can't |
1971 | * handle having a null and a random length of data after |
1972 | * the password. |
1973 | */ |
51470298 |
1974 | ssh->remote_bugs |= BUG_NEEDS_SSH1_PLAIN_PASSWORD; |
bd358db1 |
1975 | logevent("We believe remote version needs a plain SSH1 password"); |
1976 | } |
1977 | |
5ecd7ad0 |
1978 | if (ssh->cfg.sshbug_rsa1 == FORCE_ON || |
1979 | (ssh->cfg.sshbug_rsa1 == AUTO && |
2c9c6388 |
1980 | (!strcmp(imp, "Cisco-1.25")))) { |
0df73905 |
1981 | /* |
1982 | * These versions apparently have no clue whatever about |
1983 | * RSA authentication and will panic and die if they see |
1984 | * an AUTH_RSA message. |
1985 | */ |
51470298 |
1986 | ssh->remote_bugs |= BUG_CHOKES_ON_RSA; |
0df73905 |
1987 | logevent("We believe remote version can't handle RSA authentication"); |
1988 | } |
1989 | |
5ecd7ad0 |
1990 | if (ssh->cfg.sshbug_hmac2 == FORCE_ON || |
1991 | (ssh->cfg.sshbug_hmac2 == AUTO && |
b9f387af |
1992 | !wc_match("* VShell", imp) && |
831301f6 |
1993 | (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) || |
1994 | wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) || |
1995 | wc_match("2.1 *", imp)))) { |
32874aea |
1996 | /* |
1997 | * These versions have the HMAC bug. |
1998 | */ |
51470298 |
1999 | ssh->remote_bugs |= BUG_SSH2_HMAC; |
32874aea |
2000 | logevent("We believe remote version has SSH2 HMAC bug"); |
7d503c31 |
2001 | } |
1dd353b5 |
2002 | |
5ecd7ad0 |
2003 | if (ssh->cfg.sshbug_derivekey2 == FORCE_ON || |
2004 | (ssh->cfg.sshbug_derivekey2 == AUTO && |
b9f387af |
2005 | !wc_match("* VShell", imp) && |
2856a1b9 |
2006 | (wc_match("2.0.0*", imp) || wc_match("2.0.10*", imp) ))) { |
088bde77 |
2007 | /* |
2008 | * These versions have the key-derivation bug (failing to |
2009 | * include the literal shared secret in the hashes that |
2010 | * generate the keys). |
2011 | */ |
51470298 |
2012 | ssh->remote_bugs |= BUG_SSH2_DERIVEKEY; |
088bde77 |
2013 | logevent("We believe remote version has SSH2 key-derivation bug"); |
2014 | } |
2015 | |
5ecd7ad0 |
2016 | if (ssh->cfg.sshbug_rsapad2 == FORCE_ON || |
2017 | (ssh->cfg.sshbug_rsapad2 == AUTO && |
831301f6 |
2018 | (wc_match("OpenSSH_2.[5-9]*", imp) || |
2019 | wc_match("OpenSSH_3.[0-2]*", imp)))) { |
1dd353b5 |
2020 | /* |
2021 | * These versions have the SSH2 RSA padding bug. |
2022 | */ |
51470298 |
2023 | ssh->remote_bugs |= BUG_SSH2_RSA_PADDING; |
1dd353b5 |
2024 | logevent("We believe remote version has SSH2 RSA padding bug"); |
2025 | } |
8e975795 |
2026 | |
dda87a28 |
2027 | if (ssh->cfg.sshbug_pksessid2 == FORCE_ON || |
2028 | (ssh->cfg.sshbug_pksessid2 == AUTO && |
2029 | wc_match("OpenSSH_2.[0-2]*", imp))) { |
2030 | /* |
2031 | * These versions have the SSH2 session-ID bug in |
2032 | * public-key authentication. |
2033 | */ |
2034 | ssh->remote_bugs |= BUG_SSH2_PK_SESSIONID; |
2035 | logevent("We believe remote version has SSH2 public-key-session-ID bug"); |
2036 | } |
2037 | |
5ecd7ad0 |
2038 | if (ssh->cfg.sshbug_dhgex2 == FORCE_ON) { |
8e975795 |
2039 | /* |
831301f6 |
2040 | * User specified the SSH2 DH GEX bug. |
8e975795 |
2041 | */ |
51470298 |
2042 | ssh->remote_bugs |= BUG_SSH2_DH_GEX; |
8e975795 |
2043 | logevent("We believe remote version has SSH2 DH group exchange bug"); |
2044 | } |
7d503c31 |
2045 | } |
2046 | |
51470298 |
2047 | static int do_ssh_init(Ssh ssh, unsigned char c) |
32874aea |
2048 | { |
51470298 |
2049 | struct do_ssh_init_state { |
2050 | int vslen; |
2051 | char version[10]; |
2052 | char *vstring; |
2053 | int vstrsize; |
2054 | int i; |
2055 | int proto1, proto2; |
2056 | }; |
2057 | crState(do_ssh_init_state); |
374330e2 |
2058 | |
51470298 |
2059 | crBegin(ssh->do_ssh_init_crstate); |
8df7a775 |
2060 | |
2061 | /* Search for the string "SSH-" in the input. */ |
51470298 |
2062 | s->i = 0; |
8df7a775 |
2063 | while (1) { |
2064 | static const int transS[] = { 1, 2, 2, 1 }; |
2065 | static const int transH[] = { 0, 0, 3, 0 }; |
2066 | static const int transminus[] = { 0, 0, 0, -1 }; |
32874aea |
2067 | if (c == 'S') |
51470298 |
2068 | s->i = transS[s->i]; |
32874aea |
2069 | else if (c == 'H') |
51470298 |
2070 | s->i = transH[s->i]; |
32874aea |
2071 | else if (c == '-') |
51470298 |
2072 | s->i = transminus[s->i]; |
32874aea |
2073 | else |
51470298 |
2074 | s->i = 0; |
2075 | if (s->i < 0) |
8df7a775 |
2076 | break; |
2077 | crReturn(1); /* get another character */ |
374330e2 |
2078 | } |
8df7a775 |
2079 | |
51470298 |
2080 | s->vstrsize = 16; |
3d88e64d |
2081 | s->vstring = snewn(s->vstrsize, char); |
51470298 |
2082 | strcpy(s->vstring, "SSH-"); |
2083 | s->vslen = 4; |
2084 | s->i = 0; |
374330e2 |
2085 | while (1) { |
8df7a775 |
2086 | crReturn(1); /* get another char */ |
51470298 |
2087 | if (s->vslen >= s->vstrsize - 1) { |
2088 | s->vstrsize += 16; |
3d88e64d |
2089 | s->vstring = sresize(s->vstring, s->vstrsize, char); |
32874aea |
2090 | } |
51470298 |
2091 | s->vstring[s->vslen++] = c; |
2092 | if (s->i >= 0) { |
374330e2 |
2093 | if (c == '-') { |
51470298 |
2094 | s->version[s->i] = '\0'; |
2095 | s->i = -1; |
2096 | } else if (s->i < sizeof(s->version) - 1) |
2097 | s->version[s->i++] = c; |
c4ffc4d0 |
2098 | } else if (c == '\012') |
374330e2 |
2099 | break; |
2100 | } |
2101 | |
51470298 |
2102 | ssh->agentfwd_enabled = FALSE; |
2103 | ssh->rdpkt2_state.incoming_sequence = 0; |
960e736a |
2104 | |
51470298 |
2105 | s->vstring[s->vslen] = 0; |
2106 | s->vstring[strcspn(s->vstring, "\r\n")] = '\0';/* remove EOL chars */ |
2107 | { |
2108 | char *vlog; |
3d88e64d |
2109 | vlog = snewn(20 + s->vslen, char); |
51470298 |
2110 | sprintf(vlog, "Server version: %s", s->vstring); |
2111 | logevent(vlog); |
2112 | sfree(vlog); |
2113 | } |
2114 | ssh_detect_bugs(ssh, s->vstring); |
c5e9c988 |
2115 | |
adf799dd |
2116 | /* |
38d228a2 |
2117 | * Decide which SSH protocol version to support. |
adf799dd |
2118 | */ |
38d228a2 |
2119 | |
2120 | /* Anything strictly below "2.0" means protocol 1 is supported. */ |
51470298 |
2121 | s->proto1 = ssh_versioncmp(s->version, "2.0") < 0; |
38d228a2 |
2122 | /* Anything greater or equal to "1.99" means protocol 2 is supported. */ |
51470298 |
2123 | s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0; |
38d228a2 |
2124 | |
86916870 |
2125 | if (ssh->cfg.sshprot == 0 && !s->proto1) { |
6b5cf8b4 |
2126 | bombout(("SSH protocol version 1 required by user but not provided by server")); |
7ffdbc1a |
2127 | crStop(0); |
38d228a2 |
2128 | } |
86916870 |
2129 | if (ssh->cfg.sshprot == 3 && !s->proto2) { |
6b5cf8b4 |
2130 | bombout(("SSH protocol version 2 required by user but not provided by server")); |
7ffdbc1a |
2131 | crStop(0); |
38d228a2 |
2132 | } |
2133 | |
86916870 |
2134 | if (s->proto2 && (ssh->cfg.sshprot >= 2 || !s->proto1)) { |
32874aea |
2135 | /* |
38d228a2 |
2136 | * Use v2 protocol. |
32874aea |
2137 | */ |
2138 | char verstring[80], vlog[100]; |
2139 | sprintf(verstring, "SSH-2.0-%s", sshver); |
51470298 |
2140 | SHA_Init(&ssh->exhashbase); |
32874aea |
2141 | /* |
2142 | * Hash our version string and their version string. |
2143 | */ |
51470298 |
2144 | sha_string(&ssh->exhashbase, verstring, strlen(verstring)); |
2145 | sha_string(&ssh->exhashbase, s->vstring, strcspn(s->vstring, "\r\n")); |
32874aea |
2146 | sprintf(vlog, "We claim version: %s", verstring); |
2147 | logevent(vlog); |
c4ffc4d0 |
2148 | strcat(verstring, "\012"); |
32874aea |
2149 | logevent("Using SSH protocol version 2"); |
51470298 |
2150 | sk_write(ssh->s, verstring, strlen(verstring)); |
2151 | ssh->protocol = ssh2_protocol; |
2152 | ssh->version = 2; |
2153 | ssh->s_rdpkt = ssh2_rdpkt; |
e5574168 |
2154 | } else { |
32874aea |
2155 | /* |
38d228a2 |
2156 | * Use v1 protocol. |
32874aea |
2157 | */ |
2158 | char verstring[80], vlog[100]; |
2159 | sprintf(verstring, "SSH-%s-%s", |
51470298 |
2160 | (ssh_versioncmp(s->version, "1.5") <= 0 ? s->version : "1.5"), |
32874aea |
2161 | sshver); |
2162 | sprintf(vlog, "We claim version: %s", verstring); |
2163 | logevent(vlog); |
c4ffc4d0 |
2164 | strcat(verstring, "\012"); |
759712a6 |
2165 | |
32874aea |
2166 | logevent("Using SSH protocol version 1"); |
51470298 |
2167 | sk_write(ssh->s, verstring, strlen(verstring)); |
2168 | ssh->protocol = ssh1_protocol; |
2169 | ssh->version = 1; |
2170 | ssh->s_rdpkt = ssh1_rdpkt; |
e5574168 |
2171 | } |
125105d1 |
2172 | update_specials_menu(ssh->frontend); |
51470298 |
2173 | ssh->state = SSH_STATE_BEFORE_SIZE; |
8df7a775 |
2174 | |
51470298 |
2175 | sfree(s->vstring); |
50526e47 |
2176 | |
8df7a775 |
2177 | crFinish(0); |
2178 | } |
2179 | |
51470298 |
2180 | static void ssh_gotdata(Ssh ssh, unsigned char *data, int datalen) |
8df7a775 |
2181 | { |
51470298 |
2182 | crBegin(ssh->ssh_gotdata_crstate); |
8df7a775 |
2183 | |
2184 | /* |
2185 | * To begin with, feed the characters one by one to the |
2186 | * protocol initialisation / selection function do_ssh_init(). |
2187 | * When that returns 0, we're done with the initial greeting |
2188 | * exchange and can move on to packet discipline. |
2189 | */ |
2190 | while (1) { |
51470298 |
2191 | int ret; /* need not be kept across crReturn */ |
8df7a775 |
2192 | if (datalen == 0) |
2193 | crReturnV; /* more data please */ |
51470298 |
2194 | ret = do_ssh_init(ssh, *data); |
32874aea |
2195 | data++; |
2196 | datalen--; |
8df7a775 |
2197 | if (ret == 0) |
2198 | break; |
2199 | } |
2200 | |
2201 | /* |
2202 | * We emerge from that loop when the initial negotiation is |
2203 | * over and we have selected an s_rdpkt function. Now pass |
2204 | * everything to s_rdpkt, and then pass the resulting packets |
2205 | * to the proper protocol handler. |
2206 | */ |
2207 | if (datalen == 0) |
2208 | crReturnV; |
2209 | while (1) { |
2210 | while (datalen > 0) { |
51470298 |
2211 | if (ssh->s_rdpkt(ssh, &data, &datalen) == 0) { |
2212 | if (ssh->state == SSH_STATE_CLOSED) { |
e14d0f19 |
2213 | return; |
2214 | } |
51470298 |
2215 | ssh->protocol(ssh, NULL, 0, 1); |
2216 | if (ssh->state == SSH_STATE_CLOSED) { |
8df7a775 |
2217 | return; |
2218 | } |
2219 | } |
2220 | } |
2221 | crReturnV; |
2222 | } |
2223 | crFinishV; |
2224 | } |
2225 | |
36f94d1f |
2226 | static void ssh_do_close(Ssh ssh) |
32874aea |
2227 | { |
36f94d1f |
2228 | int i; |
2229 | struct ssh_channel *c; |
2230 | |
51470298 |
2231 | ssh->state = SSH_STATE_CLOSED; |
2232 | if (ssh->s) { |
2233 | sk_close(ssh->s); |
2234 | ssh->s = NULL; |
f3ab576e |
2235 | } |
36f94d1f |
2236 | /* |
2237 | * Now we must shut down any port and X forwardings going |
2238 | * through this connection. |
2239 | */ |
74a98066 |
2240 | if (ssh->channels) { |
2241 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
2242 | switch (c->type) { |
2243 | case CHAN_X11: |
2244 | x11_close(c->u.x11.s); |
2245 | break; |
2246 | case CHAN_SOCKDATA: |
2247 | pfd_close(c->u.pfd.s); |
2248 | break; |
2249 | } |
2250 | del234(ssh->channels, c); |
2251 | if (ssh->version == 2) |
2252 | bufchain_clear(&c->v.v2.outbuffer); |
2253 | sfree(c); |
36f94d1f |
2254 | } |
36f94d1f |
2255 | } |
2256 | } |
2257 | |
cbe2d68f |
2258 | static int ssh_closing(Plug plug, const char *error_msg, int error_code, |
36f94d1f |
2259 | int calling_back) |
2260 | { |
2261 | Ssh ssh = (Ssh) plug; |
2262 | ssh_do_close(ssh); |
7e78000d |
2263 | if (error_msg) { |
32874aea |
2264 | /* A socket error has occurred. */ |
247308b5 |
2265 | logevent(error_msg); |
971bcc0a |
2266 | connection_fatal(ssh->frontend, "%s", error_msg); |
7e78000d |
2267 | } else { |
2268 | /* Otherwise, the remote side closed the connection normally. */ |
8df7a775 |
2269 | } |
7e78000d |
2270 | return 0; |
2271 | } |
2272 | |
32874aea |
2273 | static int ssh_receive(Plug plug, int urgent, char *data, int len) |
2274 | { |
51470298 |
2275 | Ssh ssh = (Ssh) plug; |
d8baa528 |
2276 | ssh_gotdata(ssh, (unsigned char *)data, len); |
51470298 |
2277 | if (ssh->state == SSH_STATE_CLOSED) { |
36f94d1f |
2278 | ssh_do_close(ssh); |
32874aea |
2279 | return 0; |
3257deae |
2280 | } |
fef97f43 |
2281 | return 1; |
374330e2 |
2282 | } |
2283 | |
5471d09a |
2284 | static void ssh_sent(Plug plug, int bufsize) |
2285 | { |
51470298 |
2286 | Ssh ssh = (Ssh) plug; |
5471d09a |
2287 | /* |
2288 | * If the send backlog on the SSH socket itself clears, we |
2289 | * should unthrottle the whole world if it was throttled. |
2290 | */ |
2291 | if (bufsize < SSH_MAX_BACKLOG) |
51470298 |
2292 | ssh_throttle_all(ssh, 0, bufsize); |
5471d09a |
2293 | } |
2294 | |
fb09bf1c |
2295 | /* |
8df7a775 |
2296 | * Connect to specified host and port. |
2297 | * Returns an error message, or NULL on success. |
6e1ebb76 |
2298 | * Also places the canonical host name into `realhost'. It must be |
2299 | * freed by the caller. |
8df7a775 |
2300 | */ |
cbe2d68f |
2301 | static const char *connect_to_host(Ssh ssh, char *host, int port, |
79bf227b |
2302 | char **realhost, int nodelay, int keepalive) |
8df7a775 |
2303 | { |
51470298 |
2304 | static const struct plug_function_table fn_table = { |
7e78000d |
2305 | ssh_closing, |
5471d09a |
2306 | ssh_receive, |
2307 | ssh_sent, |
2308 | NULL |
51470298 |
2309 | }; |
7e78000d |
2310 | |
8df7a775 |
2311 | SockAddr addr; |
cbe2d68f |
2312 | const char *err; |
8df7a775 |
2313 | |
3d88e64d |
2314 | ssh->savedhost = snewn(1 + strlen(host), char); |
51470298 |
2315 | if (!ssh->savedhost) |
8df7a775 |
2316 | fatalbox("Out of memory"); |
51470298 |
2317 | strcpy(ssh->savedhost, host); |
8df7a775 |
2318 | |
2319 | if (port < 0) |
2320 | port = 22; /* default ssh port */ |
51470298 |
2321 | ssh->savedport = port; |
8df7a775 |
2322 | |
2323 | /* |
2324 | * Try to find host. |
2325 | */ |
57356d63 |
2326 | logeventf(ssh, "Looking up host \"%s\"", host); |
e8fa8f62 |
2327 | addr = name_lookup(host, port, realhost, &ssh->cfg); |
170c1e6e |
2328 | if ((err = sk_addr_error(addr)) != NULL) { |
2329 | sk_addr_free(addr); |
8df7a775 |
2330 | return err; |
170c1e6e |
2331 | } |
8df7a775 |
2332 | |
8df7a775 |
2333 | /* |
2334 | * Open socket. |
2335 | */ |
3ad9d396 |
2336 | { |
57356d63 |
2337 | char addrbuf[100]; |
3ad9d396 |
2338 | sk_getaddr(addr, addrbuf, 100); |
57356d63 |
2339 | logeventf(ssh, "Connecting to %s port %d", addrbuf, port); |
3ad9d396 |
2340 | } |
51470298 |
2341 | ssh->fn = &fn_table; |
e8fa8f62 |
2342 | ssh->s = new_connection(addr, *realhost, port, |
79bf227b |
2343 | 0, 1, nodelay, keepalive, (Plug) ssh, &ssh->cfg); |
70e5d0fd |
2344 | if ((err = sk_socket_error(ssh->s)) != NULL) { |
51470298 |
2345 | ssh->s = NULL; |
8df7a775 |
2346 | return err; |
67c4ba2e |
2347 | } |
8df7a775 |
2348 | |
8df7a775 |
2349 | return NULL; |
2350 | } |
2351 | |
2352 | /* |
5471d09a |
2353 | * Throttle or unthrottle the SSH connection. |
2354 | */ |
51470298 |
2355 | static void ssh1_throttle(Ssh ssh, int adjust) |
5471d09a |
2356 | { |
51470298 |
2357 | int old_count = ssh->v1_throttle_count; |
2358 | ssh->v1_throttle_count += adjust; |
2359 | assert(ssh->v1_throttle_count >= 0); |
2360 | if (ssh->v1_throttle_count && !old_count) { |
2361 | sk_set_frozen(ssh->s, 1); |
2362 | } else if (!ssh->v1_throttle_count && old_count) { |
2363 | sk_set_frozen(ssh->s, 0); |
5471d09a |
2364 | } |
2365 | } |
2366 | |
2367 | /* |
2368 | * Throttle or unthrottle _all_ local data streams (for when sends |
2369 | * on the SSH connection itself back up). |
2370 | */ |
51470298 |
2371 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize) |
5471d09a |
2372 | { |
2373 | int i; |
2374 | struct ssh_channel *c; |
2375 | |
51470298 |
2376 | if (enable == ssh->throttled_all) |
5471d09a |
2377 | return; |
51470298 |
2378 | ssh->throttled_all = enable; |
2379 | ssh->overall_bufsize = bufsize; |
2380 | if (!ssh->channels) |
5471d09a |
2381 | return; |
51470298 |
2382 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
5471d09a |
2383 | switch (c->type) { |
2384 | case CHAN_MAINSESSION: |
2385 | /* |
2386 | * This is treated separately, outside the switch. |
2387 | */ |
2388 | break; |
2389 | case CHAN_X11: |
2390 | x11_override_throttle(c->u.x11.s, enable); |
2391 | break; |
2392 | case CHAN_AGENT: |
2393 | /* Agent channels require no buffer management. */ |
2394 | break; |
2395 | case CHAN_SOCKDATA: |
36f94d1f |
2396 | pfd_override_throttle(c->u.pfd.s, enable); |
5471d09a |
2397 | break; |
2398 | } |
2399 | } |
2400 | } |
2401 | |
2402 | /* |
51470298 |
2403 | * Username and password input, abstracted off into routines |
2404 | * reusable in several places - even between SSH1 and SSH2. |
0405e71f |
2405 | */ |
0405e71f |
2406 | |
2407 | /* Set up a username or password input loop on a given buffer. */ |
ae9ae89f |
2408 | static void setup_userpass_input(Ssh ssh, char *buffer, int buflen, int echo) |
0405e71f |
2409 | { |
51470298 |
2410 | ssh->userpass_input_buffer = buffer; |
2411 | ssh->userpass_input_buflen = buflen; |
2412 | ssh->userpass_input_bufpos = 0; |
2413 | ssh->userpass_input_echo = echo; |
0405e71f |
2414 | } |
2415 | |
2416 | /* |
2417 | * Process some terminal data in the course of username/password |
2418 | * input. Returns >0 for success (line of input returned in |
2419 | * buffer), <0 for failure (user hit ^C/^D, bomb out and exit), 0 |
2420 | * for inconclusive (keep waiting for more input please). |
2421 | */ |
ae9ae89f |
2422 | static int process_userpass_input(Ssh ssh, unsigned char *in, int inlen) |
0405e71f |
2423 | { |
2424 | char c; |
2425 | |
2426 | while (inlen--) { |
2427 | switch (c = *in++) { |
2428 | case 10: |
2429 | case 13: |
51470298 |
2430 | ssh->userpass_input_buffer[ssh->userpass_input_bufpos] = 0; |
2431 | ssh->userpass_input_buffer[ssh->userpass_input_buflen-1] = 0; |
0405e71f |
2432 | return +1; |
2433 | break; |
2434 | case 8: |
2435 | case 127: |
51470298 |
2436 | if (ssh->userpass_input_bufpos > 0) { |
2437 | if (ssh->userpass_input_echo) |
2438 | c_write_str(ssh, "\b \b"); |
2439 | ssh->userpass_input_bufpos--; |
0405e71f |
2440 | } |
2441 | break; |
2442 | case 21: |
2443 | case 27: |
51470298 |
2444 | while (ssh->userpass_input_bufpos > 0) { |
2445 | if (ssh->userpass_input_echo) |
2446 | c_write_str(ssh, "\b \b"); |
2447 | ssh->userpass_input_bufpos--; |
0405e71f |
2448 | } |
2449 | break; |
2450 | case 3: |
2451 | case 4: |
2452 | return -1; |
2453 | break; |
2454 | default: |
4692a14a |
2455 | /* |
2456 | * This simplistic check for printability is disabled |
2457 | * when we're doing password input, because some people |
2458 | * have control characters in their passwords.o |
2459 | */ |
2460 | if ((!ssh->userpass_input_echo || |
2461 | (c >= ' ' && c <= '~') || |
0405e71f |
2462 | ((unsigned char) c >= 160)) |
51470298 |
2463 | && ssh->userpass_input_bufpos < ssh->userpass_input_buflen-1) { |
2464 | ssh->userpass_input_buffer[ssh->userpass_input_bufpos++] = c; |
2465 | if (ssh->userpass_input_echo) |
2466 | c_write(ssh, &c, 1); |
0405e71f |
2467 | } |
2468 | break; |
2469 | } |
2470 | } |
2471 | return 0; |
2472 | } |
2473 | |
f11d78f2 |
2474 | static void ssh_agent_callback(void *sshv, void *reply, int replylen) |
839f10db |
2475 | { |
2476 | Ssh ssh = (Ssh) sshv; |
2477 | |
2478 | ssh->agent_response = reply; |
2479 | ssh->agent_response_len = replylen; |
2480 | |
2481 | if (ssh->version == 1) |
2482 | do_ssh1_login(ssh, NULL, -1, 0); |
2483 | else |
2484 | do_ssh2_authconn(ssh, NULL, -1, 0); |
2485 | } |
2486 | |
f11d78f2 |
2487 | static void ssh_agentf_callback(void *cv, void *reply, int replylen) |
839f10db |
2488 | { |
2489 | struct ssh_channel *c = (struct ssh_channel *)cv; |
2490 | Ssh ssh = c->ssh; |
2491 | void *sentreply = reply; |
2492 | |
2493 | if (!sentreply) { |
2494 | /* Fake SSH_AGENT_FAILURE. */ |
2495 | sentreply = "\0\0\0\1\5"; |
2496 | replylen = 5; |
2497 | } |
2498 | if (ssh->version == 2) { |
2499 | ssh2_add_channel_data(c, sentreply, replylen); |
2500 | ssh2_try_send(c); |
2501 | } else { |
2502 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
2503 | PKT_INT, c->remoteid, |
9a10ecf4 |
2504 | PKTT_DATA, |
839f10db |
2505 | PKT_INT, replylen, |
2506 | PKT_DATA, sentreply, replylen, |
9a10ecf4 |
2507 | PKTT_OTHER, |
839f10db |
2508 | PKT_END); |
2509 | } |
2510 | if (reply) |
2511 | sfree(reply); |
2512 | } |
2513 | |
0405e71f |
2514 | /* |
fb09bf1c |
2515 | * Handle the key exchange and user authentication phases. |
2516 | */ |
51470298 |
2517 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
fb09bf1c |
2518 | { |
0016d70b |
2519 | int i, j, ret; |
2520 | unsigned char cookie[8], *ptr; |
374330e2 |
2521 | struct RSAKey servkey, hostkey; |
2522 | struct MD5Context md5c; |
51470298 |
2523 | struct do_ssh1_login_state { |
2524 | int len; |
2525 | unsigned char *rsabuf, *keystr1, *keystr2; |
2526 | unsigned long supported_ciphers_mask, supported_auths_mask; |
2527 | int tried_publickey, tried_agent; |
2528 | int tis_auth_refused, ccard_auth_refused; |
2529 | unsigned char session_id[16]; |
2530 | int cipher_type; |
2531 | char username[100]; |
2532 | void *publickey_blob; |
2533 | int publickey_bloblen; |
2534 | char password[100]; |
2535 | char prompt[200]; |
2536 | int pos; |
2537 | char c; |
2538 | int pwpkt_type; |
2539 | unsigned char request[5], *response, *p; |
2540 | int responselen; |
2541 | int keyi, nkeys; |
2542 | int authed; |
2543 | struct RSAKey key; |
2544 | Bignum challenge; |
2545 | char *commentp; |
2546 | int commentlen; |
2547 | }; |
2548 | crState(do_ssh1_login_state); |
2549 | |
2550 | crBegin(ssh->do_ssh1_login_crstate); |
374330e2 |
2551 | |
32874aea |
2552 | if (!ispkt) |
2553 | crWaitUntil(ispkt); |
374330e2 |
2554 | |
51470298 |
2555 | if (ssh->pktin.type != SSH1_SMSG_PUBLIC_KEY) { |
6b5cf8b4 |
2556 | bombout(("Public key packet not received")); |
7ffdbc1a |
2557 | crStop(0); |
8d5de777 |
2558 | } |
374330e2 |
2559 | |
c5e9c988 |
2560 | logevent("Received public keys"); |
374330e2 |
2561 | |
0016d70b |
2562 | ptr = ssh_pkt_getdata(ssh, 8); |
2563 | if (!ptr) { |
2564 | bombout(("SSH1 public key packet stopped before random cookie")); |
2565 | crStop(0); |
2566 | } |
2567 | memcpy(cookie, ptr, 8); |
374330e2 |
2568 | |
0016d70b |
2569 | if (!ssh1_pkt_getrsakey(ssh, &servkey, &s->keystr1) || |
2570 | !ssh1_pkt_getrsakey(ssh, &hostkey, &s->keystr2)) { |
ae0500e5 |
2571 | bombout(("Failed to read SSH1 public keys from public key packet")); |
0016d70b |
2572 | crStop(0); |
2573 | } |
374330e2 |
2574 | |
c5e9c988 |
2575 | /* |
1c2a93c4 |
2576 | * Log the host key fingerprint. |
c5e9c988 |
2577 | */ |
c5e9c988 |
2578 | { |
2579 | char logmsg[80]; |
1c2a93c4 |
2580 | logevent("Host key fingerprint is:"); |
c5e9c988 |
2581 | strcpy(logmsg, " "); |
32874aea |
2582 | hostkey.comment = NULL; |
2583 | rsa_fingerprint(logmsg + strlen(logmsg), |
2584 | sizeof(logmsg) - strlen(logmsg), &hostkey); |
c5e9c988 |
2585 | logevent(logmsg); |
2586 | } |
2587 | |
0016d70b |
2588 | ssh->v1_remote_protoflags = ssh_pkt_getuint32(ssh); |
2589 | s->supported_ciphers_mask = ssh_pkt_getuint32(ssh); |
2590 | s->supported_auths_mask = ssh_pkt_getuint32(ssh); |
bea1ef5f |
2591 | |
51470298 |
2592 | ssh->v1_local_protoflags = |
2593 | ssh->v1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED; |
2594 | ssh->v1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER; |
b96dc54c |
2595 | |
c5e9c988 |
2596 | MD5Init(&md5c); |
51470298 |
2597 | MD5Update(&md5c, s->keystr2, hostkey.bytes); |
2598 | MD5Update(&md5c, s->keystr1, servkey.bytes); |
0016d70b |
2599 | MD5Update(&md5c, cookie, 8); |
51470298 |
2600 | MD5Final(s->session_id, &md5c); |
374330e2 |
2601 | |
32874aea |
2602 | for (i = 0; i < 32; i++) |
51470298 |
2603 | ssh->session_key[i] = random_byte(); |
374330e2 |
2604 | |
0016d70b |
2605 | /* |
2606 | * Verify that the `bits' and `bytes' parameters match. |
2607 | */ |
2608 | if (hostkey.bits > hostkey.bytes * 8 || |
2609 | servkey.bits > servkey.bytes * 8) { |
2610 | bombout(("SSH1 public keys were badly formatted")); |
2611 | crStop(0); |
2612 | } |
2613 | |
51470298 |
2614 | s->len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); |
374330e2 |
2615 | |
3d88e64d |
2616 | s->rsabuf = snewn(s->len, unsigned char); |
51470298 |
2617 | if (!s->rsabuf) |
374330e2 |
2618 | fatalbox("Out of memory"); |
2619 | |
89ee5268 |
2620 | /* |
2621 | * Verify the host key. |
2622 | */ |
2623 | { |
32874aea |
2624 | /* |
2625 | * First format the key into a string. |
2626 | */ |
2627 | int len = rsastr_len(&hostkey); |
2628 | char fingerprint[100]; |
3d88e64d |
2629 | char *keystr = snewn(len, char); |
32874aea |
2630 | if (!keystr) |
2631 | fatalbox("Out of memory"); |
2632 | rsastr_fmt(keystr, &hostkey); |
2633 | rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey); |
a8327734 |
2634 | verify_ssh_host_key(ssh->frontend, |
2635 | ssh->savedhost, ssh->savedport, "rsa", keystr, |
32874aea |
2636 | fingerprint); |
2637 | sfree(keystr); |
2638 | } |
2639 | |
2640 | for (i = 0; i < 32; i++) { |
51470298 |
2641 | s->rsabuf[i] = ssh->session_key[i]; |
374330e2 |
2642 | if (i < 16) |
51470298 |
2643 | s->rsabuf[i] ^= s->session_id[i]; |
374330e2 |
2644 | } |
2645 | |
2646 | if (hostkey.bytes > servkey.bytes) { |
0016d70b |
2647 | ret = rsaencrypt(s->rsabuf, 32, &servkey); |
2648 | if (ret) |
2649 | ret = rsaencrypt(s->rsabuf, servkey.bytes, &hostkey); |
374330e2 |
2650 | } else { |
0016d70b |
2651 | ret = rsaencrypt(s->rsabuf, 32, &hostkey); |
2652 | if (ret) |
2653 | ret = rsaencrypt(s->rsabuf, hostkey.bytes, &servkey); |
2654 | } |
2655 | if (!ret) { |
2656 | bombout(("SSH1 public key encryptions failed due to bad formatting")); |
2657 | crStop(0); |
374330e2 |
2658 | } |
2659 | |
c5e9c988 |
2660 | logevent("Encrypted session key"); |
2661 | |
ca20bfcf |
2662 | { |
2663 | int cipher_chosen = 0, warn = 0; |
2664 | char *cipher_string = NULL; |
51470298 |
2665 | int i; |
ca20bfcf |
2666 | for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) { |
86916870 |
2667 | int next_cipher = ssh->cfg.ssh_cipherlist[i]; |
ca20bfcf |
2668 | if (next_cipher == CIPHER_WARN) { |
2669 | /* If/when we choose a cipher, warn about it */ |
2670 | warn = 1; |
2671 | } else if (next_cipher == CIPHER_AES) { |
2672 | /* XXX Probably don't need to mention this. */ |
2673 | logevent("AES not supported in SSH1, skipping"); |
2674 | } else { |
2675 | switch (next_cipher) { |
51470298 |
2676 | case CIPHER_3DES: s->cipher_type = SSH_CIPHER_3DES; |
ca20bfcf |
2677 | cipher_string = "3DES"; break; |
51470298 |
2678 | case CIPHER_BLOWFISH: s->cipher_type = SSH_CIPHER_BLOWFISH; |
ca20bfcf |
2679 | cipher_string = "Blowfish"; break; |
51470298 |
2680 | case CIPHER_DES: s->cipher_type = SSH_CIPHER_DES; |
ca20bfcf |
2681 | cipher_string = "single-DES"; break; |
2682 | } |
51470298 |
2683 | if (s->supported_ciphers_mask & (1 << s->cipher_type)) |
ca20bfcf |
2684 | cipher_chosen = 1; |
2685 | } |
2686 | } |
2687 | if (!cipher_chosen) { |
51470298 |
2688 | if ((s->supported_ciphers_mask & (1 << SSH_CIPHER_3DES)) == 0) |
6b5cf8b4 |
2689 | bombout(("Server violates SSH 1 protocol by not " |
ca20bfcf |
2690 | "supporting 3DES encryption")); |
2691 | else |
2692 | /* shouldn't happen */ |
6b5cf8b4 |
2693 | bombout(("No supported ciphers found")); |
7ffdbc1a |
2694 | crStop(0); |
a99a05c0 |
2695 | } |
ca20bfcf |
2696 | |
2697 | /* Warn about chosen cipher if necessary. */ |
2698 | if (warn) |
a8327734 |
2699 | askcipher(ssh->frontend, cipher_string, 0); |
bea1ef5f |
2700 | } |
ca20bfcf |
2701 | |
51470298 |
2702 | switch (s->cipher_type) { |
32874aea |
2703 | case SSH_CIPHER_3DES: |
2704 | logevent("Using 3DES encryption"); |
2705 | break; |
2706 | case SSH_CIPHER_DES: |
2707 | logevent("Using single-DES encryption"); |
2708 | break; |
2709 | case SSH_CIPHER_BLOWFISH: |
2710 | logevent("Using Blowfish encryption"); |
2711 | break; |
c5e9c988 |
2712 | } |
bea1ef5f |
2713 | |
51470298 |
2714 | send_packet(ssh, SSH1_CMSG_SESSION_KEY, |
2715 | PKT_CHAR, s->cipher_type, |
32874aea |
2716 | PKT_DATA, cookie, 8, |
51470298 |
2717 | PKT_CHAR, (s->len * 8) >> 8, PKT_CHAR, (s->len * 8) & 0xFF, |
2718 | PKT_DATA, s->rsabuf, s->len, |
2719 | PKT_INT, ssh->v1_local_protoflags, PKT_END); |
fb09bf1c |
2720 | |
c5e9c988 |
2721 | logevent("Trying to enable encryption..."); |
374330e2 |
2722 | |
51470298 |
2723 | sfree(s->rsabuf); |
374330e2 |
2724 | |
51470298 |
2725 | ssh->cipher = (s->cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 : |
2726 | s->cipher_type == SSH_CIPHER_DES ? &ssh_des : |
2727 | &ssh_3des); |
371e569c |
2728 | ssh->v1_cipher_ctx = ssh->cipher->make_context(); |
2729 | ssh->cipher->sesskey(ssh->v1_cipher_ctx, ssh->session_key); |
57356d63 |
2730 | logeventf(ssh, "Initialised %s encryption", ssh->cipher->text_name); |
374330e2 |
2731 | |
0183b242 |
2732 | ssh->crcda_ctx = crcda_make_context(); |
2733 | logevent("Installing CRC compensation attack detector"); |
2734 | |
679539d7 |
2735 | if (servkey.modulus) { |
2736 | sfree(servkey.modulus); |
2737 | servkey.modulus = NULL; |
2738 | } |
2739 | if (servkey.exponent) { |
2740 | sfree(servkey.exponent); |
2741 | servkey.exponent = NULL; |
2742 | } |
2743 | if (hostkey.modulus) { |
2744 | sfree(hostkey.modulus); |
2745 | hostkey.modulus = NULL; |
2746 | } |
2747 | if (hostkey.exponent) { |
2748 | sfree(hostkey.exponent); |
2749 | hostkey.exponent = NULL; |
2750 | } |
fb09bf1c |
2751 | crWaitUntil(ispkt); |
374330e2 |
2752 | |
51470298 |
2753 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS) { |
6b5cf8b4 |
2754 | bombout(("Encryption not successfully enabled")); |
7ffdbc1a |
2755 | crStop(0); |
8d5de777 |
2756 | } |
374330e2 |
2757 | |
c5e9c988 |
2758 | logevent("Successfully started encryption"); |
2759 | |
374330e2 |
2760 | fflush(stdout); |
2761 | { |
86916870 |
2762 | if ((flags & FLAG_INTERACTIVE) && !*ssh->cfg.username) { |
c0a81592 |
2763 | if (ssh_get_line && !ssh_getline_pw_only) { |
32874aea |
2764 | if (!ssh_get_line("login as: ", |
51470298 |
2765 | s->username, sizeof(s->username), FALSE)) { |
32874aea |
2766 | /* |
2767 | * get_line failed to get a username. |
2768 | * Terminate. |
2769 | */ |
2770 | logevent("No username provided. Abandoning session."); |
3bb2f322 |
2771 | ssh_closing((Plug)ssh, NULL, 0, 0); |
7ffdbc1a |
2772 | crStop(1); |
32874aea |
2773 | } |
2774 | } else { |
51470298 |
2775 | int ret; /* need not be kept over crReturn */ |
2776 | c_write_str(ssh, "login as: "); |
2777 | ssh->send_ok = 1; |
0405e71f |
2778 | |
51470298 |
2779 | setup_userpass_input(ssh, s->username, sizeof(s->username), 1); |
0405e71f |
2780 | do { |
32874aea |
2781 | crWaitUntil(!ispkt); |
51470298 |
2782 | ret = process_userpass_input(ssh, in, inlen); |
0405e71f |
2783 | } while (ret == 0); |
2784 | if (ret < 0) |
2785 | cleanup_exit(0); |
51470298 |
2786 | c_write_str(ssh, "\r\n"); |
32874aea |
2787 | } |
2788 | } else { |
86916870 |
2789 | strncpy(s->username, ssh->cfg.username, sizeof(s->username)); |
51470298 |
2790 | s->username[sizeof(s->username)-1] = '\0'; |
374330e2 |
2791 | } |
fb09bf1c |
2792 | |
51470298 |
2793 | send_packet(ssh, SSH1_CMSG_USER, PKT_STR, s->username, PKT_END); |
c5e9c988 |
2794 | { |
51470298 |
2795 | char userlog[22 + sizeof(s->username)]; |
2796 | sprintf(userlog, "Sent username \"%s\"", s->username); |
c5e9c988 |
2797 | logevent(userlog); |
32874aea |
2798 | if (flags & FLAG_INTERACTIVE && |
2799 | (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) { |
3c8e959b |
2800 | strcat(userlog, "\r\n"); |
51470298 |
2801 | c_write_str(ssh, userlog); |
3c8e959b |
2802 | } |
c5e9c988 |
2803 | } |
374330e2 |
2804 | } |
2805 | |
fb09bf1c |
2806 | crWaitUntil(ispkt); |
374330e2 |
2807 | |
51470298 |
2808 | if ((ssh->remote_bugs & BUG_CHOKES_ON_RSA)) { |
0df73905 |
2809 | /* We must not attempt PK auth. Pretend we've already tried it. */ |
51470298 |
2810 | s->tried_publickey = s->tried_agent = 1; |
0df73905 |
2811 | } else { |
51470298 |
2812 | s->tried_publickey = s->tried_agent = 0; |
0df73905 |
2813 | } |
51470298 |
2814 | s->tis_auth_refused = s->ccard_auth_refused = 0; |
86916870 |
2815 | /* Load the public half of ssh->cfg.keyfile so we notice if it's in Pageant */ |
9a30e26b |
2816 | if (!filename_is_null(ssh->cfg.keyfile)) { |
2817 | if (!rsakey_pubblob(&ssh->cfg.keyfile, |
222d54dc |
2818 | &s->publickey_blob, &s->publickey_bloblen, NULL)) |
51470298 |
2819 | s->publickey_blob = NULL; |
396778f1 |
2820 | } else |
51470298 |
2821 | s->publickey_blob = NULL; |
7cca0d81 |
2822 | |
51470298 |
2823 | while (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
2824 | s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD; |
614a20a0 |
2825 | |
51470298 |
2826 | if (agent_exists() && !s->tried_agent) { |
32874aea |
2827 | /* |
2828 | * Attempt RSA authentication using Pageant. |
2829 | */ |
32874aea |
2830 | void *r; |
2831 | |
51470298 |
2832 | s->authed = FALSE; |
2833 | s->tried_agent = 1; |
32874aea |
2834 | logevent("Pageant is running. Requesting keys."); |
2835 | |
2836 | /* Request the keys held by the agent. */ |
51470298 |
2837 | PUT_32BIT(s->request, 1); |
2838 | s->request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
839f10db |
2839 | if (!agent_query(s->request, 5, &r, &s->responselen, |
2840 | ssh_agent_callback, ssh)) { |
2841 | do { |
2842 | crReturn(0); |
2843 | if (ispkt) { |
2844 | bombout(("Unexpected data from server while waiting" |
2845 | " for agent response")); |
2846 | crStop(0); |
2847 | } |
2848 | } while (ispkt || inlen > 0); |
2849 | r = ssh->agent_response; |
2850 | s->responselen = ssh->agent_response_len; |
2851 | } |
51470298 |
2852 | s->response = (unsigned char *) r; |
2853 | if (s->response && s->responselen >= 5 && |
2854 | s->response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) { |
2855 | s->p = s->response + 5; |
2856 | s->nkeys = GET_32BIT(s->p); |
2857 | s->p += 4; |
32874aea |
2858 | { |
2859 | char buf[64]; |
51470298 |
2860 | sprintf(buf, "Pageant has %d SSH1 keys", s->nkeys); |
32874aea |
2861 | logevent(buf); |
2862 | } |
51470298 |
2863 | for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) { |
32874aea |
2864 | { |
2865 | char buf[64]; |
51470298 |
2866 | sprintf(buf, "Trying Pageant key #%d", s->keyi); |
32874aea |
2867 | logevent(buf); |
2868 | } |
51470298 |
2869 | if (s->publickey_blob && |
2870 | !memcmp(s->p, s->publickey_blob, |
2871 | s->publickey_bloblen)) { |
396778f1 |
2872 | logevent("This key matches configured key file"); |
51470298 |
2873 | s->tried_publickey = 1; |
396778f1 |
2874 | } |
51470298 |
2875 | s->p += 4; |
0016d70b |
2876 | { |
2877 | int n, ok = FALSE; |
2878 | do { /* do while (0) to make breaking easy */ |
2879 | n = ssh1_read_bignum |
2880 | (s->p, s->responselen-(s->p-s->response), |
2881 | &s->key.exponent); |
2882 | if (n < 0) |
2883 | break; |
2884 | s->p += n; |
2885 | n = ssh1_read_bignum |
2886 | (s->p, s->responselen-(s->p-s->response), |
2887 | &s->key.modulus); |
2888 | if (n < 0) |
2889 | break; |
2890 | s->p += n; |
2891 | if (s->responselen - (s->p-s->response) < 4) |
2892 | break; |
2893 | s->commentlen = GET_32BIT(s->p); |
2894 | s->p += 4; |
2895 | if (s->responselen - (s->p-s->response) < |
2896 | s->commentlen) |
2897 | break; |
2898 | s->commentp = (char *)s->p; |
2899 | s->p += s->commentlen; |
2900 | ok = TRUE; |
2901 | } while (0); |
2902 | if (!ok) { |
2903 | logevent("Pageant key list packet was truncated"); |
2904 | break; |
2905 | } |
2906 | } |
51470298 |
2907 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
2908 | PKT_BIGNUM, s->key.modulus, PKT_END); |
32874aea |
2909 | crWaitUntil(ispkt); |
51470298 |
2910 | if (ssh->pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
32874aea |
2911 | logevent("Key refused"); |
2912 | continue; |
2913 | } |
2914 | logevent("Received RSA challenge"); |
0016d70b |
2915 | if ((s->challenge = ssh1_pkt_getmp(ssh)) == NULL) { |
2916 | bombout(("Server's RSA challenge was badly formatted")); |
2917 | crStop(0); |
2918 | } |
2919 | |
32874aea |
2920 | { |
2921 | char *agentreq, *q, *ret; |
2d466ffd |
2922 | void *vret; |
32874aea |
2923 | int len, retlen; |
2924 | len = 1 + 4; /* message type, bit count */ |
51470298 |
2925 | len += ssh1_bignum_length(s->key.exponent); |
2926 | len += ssh1_bignum_length(s->key.modulus); |
2927 | len += ssh1_bignum_length(s->challenge); |
32874aea |
2928 | len += 16; /* session id */ |
2929 | len += 4; /* response format */ |
3d88e64d |
2930 | agentreq = snewn(4 + len, char); |
32874aea |
2931 | PUT_32BIT(agentreq, len); |
2932 | q = agentreq + 4; |
2933 | *q++ = SSH1_AGENTC_RSA_CHALLENGE; |
51470298 |
2934 | PUT_32BIT(q, bignum_bitcount(s->key.modulus)); |
32874aea |
2935 | q += 4; |
51470298 |
2936 | q += ssh1_write_bignum(q, s->key.exponent); |
2937 | q += ssh1_write_bignum(q, s->key.modulus); |
2938 | q += ssh1_write_bignum(q, s->challenge); |
2939 | memcpy(q, s->session_id, 16); |
32874aea |
2940 | q += 16; |
2941 | PUT_32BIT(q, 1); /* response format */ |
839f10db |
2942 | if (!agent_query(agentreq, len + 4, &vret, &retlen, |
2943 | ssh_agent_callback, ssh)) { |
2944 | sfree(agentreq); |
2945 | do { |
2946 | crReturn(0); |
2947 | if (ispkt) { |
2948 | bombout(("Unexpected data from server" |
2949 | " while waiting for agent" |
2950 | " response")); |
2951 | crStop(0); |
2952 | } |
2953 | } while (ispkt || inlen > 0); |
2954 | vret = ssh->agent_response; |
2955 | retlen = ssh->agent_response_len; |
2956 | } else |
2957 | sfree(agentreq); |
2d466ffd |
2958 | ret = vret; |
32874aea |
2959 | if (ret) { |
2960 | if (ret[4] == SSH1_AGENT_RSA_RESPONSE) { |
2961 | logevent("Sending Pageant's response"); |
51470298 |
2962 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
32874aea |
2963 | PKT_DATA, ret + 5, 16, |
2964 | PKT_END); |
2965 | sfree(ret); |
2966 | crWaitUntil(ispkt); |
51470298 |
2967 | if (ssh->pktin.type == SSH1_SMSG_SUCCESS) { |
32874aea |
2968 | logevent |
2969 | ("Pageant's response accepted"); |
2970 | if (flags & FLAG_VERBOSE) { |
51470298 |
2971 | c_write_str(ssh, "Authenticated using" |
2972 | " RSA key \""); |
2973 | c_write(ssh, s->commentp, |
2974 | s->commentlen); |
2975 | c_write_str(ssh, "\" from agent\r\n"); |
32874aea |
2976 | } |
51470298 |
2977 | s->authed = TRUE; |
32874aea |
2978 | } else |
2979 | logevent |
2980 | ("Pageant's response not accepted"); |
2981 | } else { |
2982 | logevent |
2983 | ("Pageant failed to answer challenge"); |
2984 | sfree(ret); |
2985 | } |
2986 | } else { |
2987 | logevent("No reply received from Pageant"); |
2988 | } |
2989 | } |
51470298 |
2990 | freebn(s->key.exponent); |
2991 | freebn(s->key.modulus); |
2992 | freebn(s->challenge); |
2993 | if (s->authed) |
32874aea |
2994 | break; |
2995 | } |
29b1d0b3 |
2996 | sfree(s->response); |
32874aea |
2997 | } |
51470298 |
2998 | if (s->authed) |
32874aea |
2999 | break; |
3000 | } |
9a30e26b |
3001 | if (!filename_is_null(ssh->cfg.keyfile) && !s->tried_publickey) |
51470298 |
3002 | s->pwpkt_type = SSH1_CMSG_AUTH_RSA; |
32874aea |
3003 | |
86916870 |
3004 | if (ssh->cfg.try_tis_auth && |
51470298 |
3005 | (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) && |
3006 | !s->tis_auth_refused) { |
3007 | s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE; |
32874aea |
3008 | logevent("Requested TIS authentication"); |
51470298 |
3009 | send_packet(ssh, SSH1_CMSG_AUTH_TIS, PKT_END); |
32874aea |
3010 | crWaitUntil(ispkt); |
51470298 |
3011 | if (ssh->pktin.type != SSH1_SMSG_AUTH_TIS_CHALLENGE) { |
32874aea |
3012 | logevent("TIS authentication declined"); |
3013 | if (flags & FLAG_INTERACTIVE) |
51470298 |
3014 | c_write_str(ssh, "TIS authentication refused.\r\n"); |
3015 | s->tis_auth_refused = 1; |
614a20a0 |
3016 | continue; |
32874aea |
3017 | } else { |
0016d70b |
3018 | char *challenge; |
3019 | int challengelen; |
3020 | |
3021 | ssh_pkt_getstring(ssh, &challenge, &challengelen); |
3022 | if (!challenge) { |
3023 | bombout(("TIS challenge packet was badly formed")); |
3024 | crStop(0); |
3025 | } |
32874aea |
3026 | logevent("Received TIS challenge"); |
51470298 |
3027 | if (challengelen > sizeof(s->prompt) - 1) |
3028 | challengelen = sizeof(s->prompt) - 1;/* prevent overrun */ |
0016d70b |
3029 | memcpy(s->prompt, challenge, challengelen); |
614a20a0 |
3030 | /* Prompt heuristic comes from OpenSSH */ |
51470298 |
3031 | strncpy(s->prompt + challengelen, |
3032 | memchr(s->prompt, '\n', challengelen) ? |
614a20a0 |
3033 | "": "\r\nResponse: ", |
51470298 |
3034 | (sizeof s->prompt) - challengelen); |
3035 | s->prompt[(sizeof s->prompt) - 1] = '\0'; |
32874aea |
3036 | } |
3037 | } |
86916870 |
3038 | if (ssh->cfg.try_tis_auth && |
51470298 |
3039 | (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) && |
3040 | !s->ccard_auth_refused) { |
3041 | s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE; |
32874aea |
3042 | logevent("Requested CryptoCard authentication"); |
51470298 |
3043 | send_packet(ssh, SSH1_CMSG_AUTH_CCARD, PKT_END); |
32874aea |
3044 | crWaitUntil(ispkt); |
51470298 |
3045 | if (ssh->pktin.type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
32874aea |
3046 | logevent("CryptoCard authentication declined"); |
51470298 |
3047 | c_write_str(ssh, "CryptoCard authentication refused.\r\n"); |
3048 | s->ccard_auth_refused = 1; |
614a20a0 |
3049 | continue; |
32874aea |
3050 | } else { |
0016d70b |
3051 | char *challenge; |
3052 | int challengelen; |
3053 | |
3054 | ssh_pkt_getstring(ssh, &challenge, &challengelen); |
3055 | if (!challenge) { |
3056 | bombout(("CryptoCard challenge packet was badly formed")); |
3057 | crStop(0); |
3058 | } |
32874aea |
3059 | logevent("Received CryptoCard challenge"); |
51470298 |
3060 | if (challengelen > sizeof(s->prompt) - 1) |
3061 | challengelen = sizeof(s->prompt) - 1;/* prevent overrun */ |
0016d70b |
3062 | memcpy(s->prompt, challenge, challengelen); |
51470298 |
3063 | strncpy(s->prompt + challengelen, |
3064 | memchr(s->prompt, '\n', challengelen) ? |
614a20a0 |
3065 | "" : "\r\nResponse: ", |
51470298 |
3066 | sizeof(s->prompt) - challengelen); |
3067 | s->prompt[sizeof(s->prompt) - 1] = '\0'; |
32874aea |
3068 | } |
3069 | } |
51470298 |
3070 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
3071 | sprintf(s->prompt, "%.90s@%.90s's password: ", |
3072 | s->username, ssh->savedhost); |
32874aea |
3073 | } |
51470298 |
3074 | if (s->pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
32874aea |
3075 | char *comment = NULL; |
231ee168 |
3076 | int type; |
3077 | char msgbuf[256]; |
32874aea |
3078 | if (flags & FLAG_VERBOSE) |
51470298 |
3079 | c_write_str(ssh, "Trying public key authentication.\r\n"); |
6c6d9ed9 |
3080 | logeventf(ssh, "Trying public key \"%s\"", |
3081 | filename_to_str(&ssh->cfg.keyfile)); |
9a30e26b |
3082 | type = key_type(&ssh->cfg.keyfile); |
231ee168 |
3083 | if (type != SSH_KEYTYPE_SSH1) { |
3084 | sprintf(msgbuf, "Key is of wrong type (%s)", |
3085 | key_type_to_str(type)); |
3086 | logevent(msgbuf); |
51470298 |
3087 | c_write_str(ssh, msgbuf); |
3088 | c_write_str(ssh, "\r\n"); |
3089 | s->tried_publickey = 1; |
231ee168 |
3090 | continue; |
3091 | } |
9a30e26b |
3092 | if (!rsakey_encrypted(&ssh->cfg.keyfile, &comment)) { |
32874aea |
3093 | if (flags & FLAG_VERBOSE) |
51470298 |
3094 | c_write_str(ssh, "No passphrase required.\r\n"); |
32874aea |
3095 | goto tryauth; |
3096 | } |
51470298 |
3097 | sprintf(s->prompt, "Passphrase for key \"%.100s\": ", comment); |
32874aea |
3098 | sfree(comment); |
3099 | } |
a52f067e |
3100 | |
614a20a0 |
3101 | /* |
3102 | * Show password prompt, having first obtained it via a TIS |
3103 | * or CryptoCard exchange if we're doing TIS or CryptoCard |
3104 | * authentication. |
3105 | */ |
fa17a66e |
3106 | if (ssh_get_line) { |
51470298 |
3107 | if (!ssh_get_line(s->prompt, s->password, |
3108 | sizeof(s->password), TRUE)) { |
32874aea |
3109 | /* |
3110 | * get_line failed to get a password (for example |
3111 | * because one was supplied on the command line |
3112 | * which has already failed to work). Terminate. |
3113 | */ |
51470298 |
3114 | send_packet(ssh, SSH1_MSG_DISCONNECT, |
2bc6a386 |
3115 | PKT_STR, "No more passwords available to try", |
3116 | PKT_END); |
247308b5 |
3117 | logevent("Unable to authenticate"); |
a8327734 |
3118 | connection_fatal(ssh->frontend, "Unable to authenticate"); |
3bb2f322 |
3119 | ssh_closing((Plug)ssh, NULL, 0, 0); |
7ffdbc1a |
3120 | crStop(1); |
32874aea |
3121 | } |
fb09bf1c |
3122 | } else { |
614a20a0 |
3123 | /* Prompt may have come from server. We've munged it a bit, so |
3124 | * we know it to be zero-terminated at least once. */ |
51470298 |
3125 | int ret; /* need not be saved over crReturn */ |
3126 | c_write_untrusted(ssh, s->prompt, strlen(s->prompt)); |
3127 | s->pos = 0; |
0405e71f |
3128 | |
51470298 |
3129 | setup_userpass_input(ssh, s->password, sizeof(s->password), 0); |
0405e71f |
3130 | do { |
32874aea |
3131 | crWaitUntil(!ispkt); |
51470298 |
3132 | ret = process_userpass_input(ssh, in, inlen); |
0405e71f |
3133 | } while (ret == 0); |
3134 | if (ret < 0) |
3135 | cleanup_exit(0); |
51470298 |
3136 | c_write_str(ssh, "\r\n"); |
32874aea |
3137 | } |
3138 | |
3139 | tryauth: |
51470298 |
3140 | if (s->pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
32874aea |
3141 | /* |
3142 | * Try public key authentication with the specified |
3143 | * key file. |
3144 | */ |
51470298 |
3145 | s->tried_publickey = 1; |
3146 | |
3147 | { |
222d54dc |
3148 | const char *error = NULL; |
3149 | int ret = loadrsakey(&ssh->cfg.keyfile, &s->key, s->password, |
3150 | &error); |
51470298 |
3151 | if (ret == 0) { |
3152 | c_write_str(ssh, "Couldn't load private key from "); |
9fab77dc |
3153 | c_write_str(ssh, filename_to_str(&ssh->cfg.keyfile)); |
222d54dc |
3154 | c_write_str(ssh, " ("); |
3155 | c_write_str(ssh, error); |
3156 | c_write_str(ssh, ").\r\n"); |
51470298 |
3157 | continue; /* go and try password */ |
3158 | } |
3159 | if (ret == -1) { |
3160 | c_write_str(ssh, "Wrong passphrase.\r\n"); |
3161 | s->tried_publickey = 0; |
3162 | continue; /* try again */ |
3163 | } |
32874aea |
3164 | } |
3165 | |
3166 | /* |
3167 | * Send a public key attempt. |
3168 | */ |
51470298 |
3169 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
3170 | PKT_BIGNUM, s->key.modulus, PKT_END); |
32874aea |
3171 | |
3172 | crWaitUntil(ispkt); |
51470298 |
3173 | if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
3174 | c_write_str(ssh, "Server refused our public key.\r\n"); |
32874aea |
3175 | continue; /* go and try password */ |
3176 | } |
51470298 |
3177 | if (ssh->pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
6b5cf8b4 |
3178 | bombout(("Bizarre response to offer of public key")); |
7ffdbc1a |
3179 | crStop(0); |
32874aea |
3180 | } |
32874aea |
3181 | |
51470298 |
3182 | { |
3183 | int i; |
3184 | unsigned char buffer[32]; |
3185 | Bignum challenge, response; |
3186 | |
0016d70b |
3187 | if ((challenge = ssh1_pkt_getmp(ssh)) == NULL) { |
3188 | bombout(("Server's RSA challenge was badly formatted")); |
3189 | crStop(0); |
3190 | } |
51470298 |
3191 | response = rsadecrypt(challenge, &s->key); |
3192 | freebn(s->key.private_exponent);/* burn the evidence */ |
32874aea |
3193 | |
51470298 |
3194 | for (i = 0; i < 32; i++) { |
3195 | buffer[i] = bignum_byte(response, 31 - i); |
3196 | } |
3197 | |
3198 | MD5Init(&md5c); |
3199 | MD5Update(&md5c, buffer, 32); |
3200 | MD5Update(&md5c, s->session_id, 16); |
3201 | MD5Final(buffer, &md5c); |
32874aea |
3202 | |
51470298 |
3203 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
3204 | PKT_DATA, buffer, 16, PKT_END); |
3205 | |
3206 | freebn(challenge); |
3207 | freebn(response); |
3208 | } |
32874aea |
3209 | |
3210 | crWaitUntil(ispkt); |
51470298 |
3211 | if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
32874aea |
3212 | if (flags & FLAG_VERBOSE) |
51470298 |
3213 | c_write_str(ssh, "Failed to authenticate with" |
3214 | " our public key.\r\n"); |
32874aea |
3215 | continue; /* go and try password */ |
51470298 |
3216 | } else if (ssh->pktin.type != SSH1_SMSG_SUCCESS) { |
6b5cf8b4 |
3217 | bombout(("Bizarre response to RSA authentication response")); |
7ffdbc1a |
3218 | crStop(0); |
32874aea |
3219 | } |
3220 | |
3221 | break; /* we're through! */ |
3222 | } else { |
51470298 |
3223 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
32874aea |
3224 | /* |
3225 | * Defence against traffic analysis: we send a |
3226 | * whole bunch of packets containing strings of |
3227 | * different lengths. One of these strings is the |
3228 | * password, in a SSH1_CMSG_AUTH_PASSWORD packet. |
3229 | * The others are all random data in |
3230 | * SSH1_MSG_IGNORE packets. This way a passive |
3231 | * listener can't tell which is the password, and |
3232 | * hence can't deduce the password length. |
3233 | * |
3234 | * Anybody with a password length greater than 16 |
3235 | * bytes is going to have enough entropy in their |
3236 | * password that a listener won't find it _that_ |
3237 | * much help to know how long it is. So what we'll |
3238 | * do is: |
3239 | * |
3240 | * - if password length < 16, we send 15 packets |
3241 | * containing string lengths 1 through 15 |
3242 | * |
3243 | * - otherwise, we let N be the nearest multiple |
3244 | * of 8 below the password length, and send 8 |
3245 | * packets containing string lengths N through |
3246 | * N+7. This won't obscure the order of |
3247 | * magnitude of the password length, but it will |
3248 | * introduce a bit of extra uncertainty. |
3249 | * |
3250 | * A few servers (the old 1.2.18 through 1.2.22) |
3251 | * can't deal with SSH1_MSG_IGNORE. For these |
3252 | * servers, we need an alternative defence. We make |
3253 | * use of the fact that the password is interpreted |
3254 | * as a C string: so we can append a NUL, then some |
3255 | * random data. |
bd358db1 |
3256 | * |
3257 | * One server (a Cisco one) can deal with neither |
3258 | * SSH1_MSG_IGNORE _nor_ a padded password string. |
3259 | * For this server we are left with no defences |
3260 | * against password length sniffing. |
32874aea |
3261 | */ |
51470298 |
3262 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) { |
b17c8428 |
3263 | /* |
3264 | * The server can deal with SSH1_MSG_IGNORE, so |
3265 | * we can use the primary defence. |
3266 | */ |
32874aea |
3267 | int bottom, top, pwlen, i; |
3268 | char *randomstr; |
3269 | |
51470298 |
3270 | pwlen = strlen(s->password); |
32874aea |
3271 | if (pwlen < 16) { |
3272 | bottom = 0; /* zero length passwords are OK! :-) */ |
3273 | top = 15; |
3274 | } else { |
3275 | bottom = pwlen & ~7; |
3276 | top = bottom + 7; |
3277 | } |
3278 | |
3279 | assert(pwlen >= bottom && pwlen <= top); |
3280 | |
3d88e64d |
3281 | randomstr = snewn(top + 1, char); |
32874aea |
3282 | |
3283 | for (i = bottom; i <= top; i++) { |
9a10ecf4 |
3284 | if (i == pwlen) { |
51470298 |
3285 | defer_packet(ssh, s->pwpkt_type, |
9a10ecf4 |
3286 | PKTT_PASSWORD, PKT_STR, s->password, |
3287 | PKTT_OTHER, PKT_END); |
3288 | } else { |
32874aea |
3289 | for (j = 0; j < i; j++) { |
3290 | do { |
3291 | randomstr[j] = random_byte(); |
3292 | } while (randomstr[j] == '\0'); |
3293 | } |
3294 | randomstr[i] = '\0'; |
51470298 |
3295 | defer_packet(ssh, SSH1_MSG_IGNORE, |
32874aea |
3296 | PKT_STR, randomstr, PKT_END); |
3297 | } |
3298 | } |
bd358db1 |
3299 | logevent("Sending password with camouflage packets"); |
51470298 |
3300 | ssh_pkt_defersend(ssh); |
679539d7 |
3301 | sfree(randomstr); |
bd358db1 |
3302 | } |
51470298 |
3303 | else if (!(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) { |
b17c8428 |
3304 | /* |
3305 | * The server can't deal with SSH1_MSG_IGNORE |
3306 | * but can deal with padded passwords, so we |
3307 | * can use the secondary defence. |
3308 | */ |
bd358db1 |
3309 | char string[64]; |
51470298 |
3310 | char *ss; |
bd358db1 |
3311 | int len; |
3312 | |
51470298 |
3313 | len = strlen(s->password); |
bd358db1 |
3314 | if (len < sizeof(string)) { |
51470298 |
3315 | ss = string; |
3316 | strcpy(string, s->password); |
bd358db1 |
3317 | len++; /* cover the zero byte */ |
3318 | while (len < sizeof(string)) { |
3319 | string[len++] = (char) random_byte(); |
3320 | } |
3321 | } else { |
51470298 |
3322 | ss = s->password; |
bd358db1 |
3323 | } |
3324 | logevent("Sending length-padded password"); |
9a10ecf4 |
3325 | send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD, |
3326 | PKT_INT, len, PKT_DATA, ss, len, |
3327 | PKTT_OTHER, PKT_END); |
bd358db1 |
3328 | } else { |
3329 | /* |
3330 | * The server has _both_ |
3331 | * BUG_CHOKES_ON_SSH1_IGNORE and |
3332 | * BUG_NEEDS_SSH1_PLAIN_PASSWORD. There is |
3333 | * therefore nothing we can do. |
3334 | */ |
3335 | int len; |
51470298 |
3336 | len = strlen(s->password); |
bd358db1 |
3337 | logevent("Sending unpadded password"); |
9a10ecf4 |
3338 | send_packet(ssh, s->pwpkt_type, |
3339 | PKTT_PASSWORD, PKT_INT, len, |
3340 | PKT_DATA, s->password, len, |
3341 | PKTT_OTHER, PKT_END); |
32874aea |
3342 | } |
3343 | } else { |
9a10ecf4 |
3344 | send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD, |
3345 | PKT_STR, s->password, PKTT_OTHER, PKT_END); |
32874aea |
3346 | } |
3347 | } |
c5e9c988 |
3348 | logevent("Sent password"); |
51470298 |
3349 | memset(s->password, 0, strlen(s->password)); |
fb09bf1c |
3350 | crWaitUntil(ispkt); |
51470298 |
3351 | if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
32874aea |
3352 | if (flags & FLAG_VERBOSE) |
51470298 |
3353 | c_write_str(ssh, "Access denied\r\n"); |
c5e9c988 |
3354 | logevent("Authentication refused"); |
51470298 |
3355 | } else if (ssh->pktin.type != SSH1_SMSG_SUCCESS) { |
6b5cf8b4 |
3356 | bombout(("Strange packet received, type %d", ssh->pktin.type)); |
7ffdbc1a |
3357 | crStop(0); |
374330e2 |
3358 | } |
3359 | } |
3360 | |
c5e9c988 |
3361 | logevent("Authentication successful"); |
3362 | |
fb09bf1c |
3363 | crFinish(1); |
3364 | } |
3365 | |
32874aea |
3366 | void sshfwd_close(struct ssh_channel *c) |
3367 | { |
51470298 |
3368 | Ssh ssh = c->ssh; |
3369 | |
36f94d1f |
3370 | if (ssh->state != SSH_STATE_SESSION) { |
3371 | assert(ssh->state == SSH_STATE_CLOSED); |
3372 | return; |
3373 | } |
3374 | |
80e48603 |
3375 | if (c && !c->closes) { |
4ed34d25 |
3376 | /* |
3377 | * If the channel's remoteid is -1, we have sent |
3378 | * CHANNEL_OPEN for this channel, but it hasn't even been |
3379 | * acknowledged by the server. So we must set a close flag |
3380 | * on it now, and then when the server acks the channel |
3381 | * open, we can close it then. |
3382 | */ |
36cac739 |
3383 | if (((int)c->remoteid) != -1) { |
51470298 |
3384 | if (ssh->version == 1) { |
3385 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, |
4ed34d25 |
3386 | PKT_END); |
3387 | } else { |
51470298 |
3388 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_CLOSE); |
3389 | ssh2_pkt_adduint32(ssh, c->remoteid); |
3390 | ssh2_pkt_send(ssh); |
4ed34d25 |
3391 | } |
32874aea |
3392 | } |
0357890f |
3393 | c->closes = 1; /* sent MSG_CLOSE */ |
32874aea |
3394 | if (c->type == CHAN_X11) { |
3395 | c->u.x11.s = NULL; |
d74d141c |
3396 | logevent("Forwarded X11 connection terminated"); |
4ed34d25 |
3397 | } else if (c->type == CHAN_SOCKDATA || |
3398 | c->type == CHAN_SOCKDATA_DORMANT) { |
d74d141c |
3399 | c->u.pfd.s = NULL; |
3400 | logevent("Forwarded port closed"); |
32874aea |
3401 | } |
3402 | } |
3403 | } |
3404 | |
5471d09a |
3405 | int sshfwd_write(struct ssh_channel *c, char *buf, int len) |
32874aea |
3406 | { |
51470298 |
3407 | Ssh ssh = c->ssh; |
3408 | |
36f94d1f |
3409 | if (ssh->state != SSH_STATE_SESSION) { |
3410 | assert(ssh->state == SSH_STATE_CLOSED); |
3411 | return 0; |
3412 | } |
3413 | |
51470298 |
3414 | if (ssh->version == 1) { |
3415 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
32874aea |
3416 | PKT_INT, c->remoteid, |
9a10ecf4 |
3417 | PKTT_DATA, |
3418 | PKT_INT, len, PKT_DATA, buf, len, |
3419 | PKTT_OTHER, PKT_END); |
5471d09a |
3420 | /* |
3421 | * In SSH1 we can return 0 here - implying that forwarded |
3422 | * connections are never individually throttled - because |
3423 | * the only circumstance that can cause throttling will be |
3424 | * the whole SSH connection backing up, in which case |
3425 | * _everything_ will be throttled as a whole. |
3426 | */ |
3427 | return 0; |
783415f8 |
3428 | } else { |
32874aea |
3429 | ssh2_add_channel_data(c, buf, len); |
5471d09a |
3430 | return ssh2_try_send(c); |
3431 | } |
3432 | } |
3433 | |
3434 | void sshfwd_unthrottle(struct ssh_channel *c, int bufsize) |
3435 | { |
51470298 |
3436 | Ssh ssh = c->ssh; |
3437 | |
36f94d1f |
3438 | if (ssh->state != SSH_STATE_SESSION) { |
3439 | assert(ssh->state == SSH_STATE_CLOSED); |
3440 | return; |
3441 | } |
3442 | |
51470298 |
3443 | if (ssh->version == 1) { |
5471d09a |
3444 | if (c->v.v1.throttling && bufsize < SSH1_BUFFER_LIMIT) { |
3445 | c->v.v1.throttling = 0; |
51470298 |
3446 | ssh1_throttle(ssh, -1); |
5471d09a |
3447 | } |
3448 | } else { |
3449 | ssh2_set_window(c, OUR_V2_WINSIZE - bufsize); |
783415f8 |
3450 | } |
9c964e85 |
3451 | } |
3452 | |
51470298 |
3453 | static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
32874aea |
3454 | { |
51470298 |
3455 | crBegin(ssh->ssh1_protocol_crstate); |
fb09bf1c |
3456 | |
3457 | random_init(); |
3458 | |
51470298 |
3459 | while (!do_ssh1_login(ssh, in, inlen, ispkt)) { |
fb09bf1c |
3460 | crReturnV; |
85ee8208 |
3461 | } |
51470298 |
3462 | if (ssh->state == SSH_STATE_CLOSED) |
32874aea |
3463 | crReturnV; |
fb09bf1c |
3464 | |
86916870 |
3465 | if (ssh->cfg.agentfwd && agent_exists()) { |
32874aea |
3466 | logevent("Requesting agent forwarding"); |
51470298 |
3467 | send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END); |
32874aea |
3468 | do { |
3469 | crReturnV; |
3470 | } while (!ispkt); |
51470298 |
3471 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3472 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
3473 | bombout(("Protocol confusion")); |
7ffdbc1a |
3474 | crStopV; |
51470298 |
3475 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
32874aea |
3476 | logevent("Agent forwarding refused"); |
3477 | } else { |
3478 | logevent("Agent forwarding enabled"); |
51470298 |
3479 | ssh->agentfwd_enabled = TRUE; |
db7d555c |
3480 | } |
dacbd0e8 |
3481 | } |
3482 | |
86916870 |
3483 | if (ssh->cfg.x11_forward) { |
32874aea |
3484 | char proto[20], data[64]; |
3485 | logevent("Requesting X11 forwarding"); |
302121de |
3486 | ssh->x11auth = x11_invent_auth(proto, sizeof(proto), |
86916870 |
3487 | data, sizeof(data), ssh->cfg.x11_auth); |
3488 | x11_get_real_auth(ssh->x11auth, ssh->cfg.x11_display); |
51470298 |
3489 | if (ssh->v1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) { |
3490 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
32874aea |
3491 | PKT_STR, proto, PKT_STR, data, |
86916870 |
3492 | PKT_INT, x11_get_screen_number(ssh->cfg.x11_display), |
421d6835 |
3493 | PKT_END); |
32874aea |
3494 | } else { |
51470298 |
3495 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
32874aea |
3496 | PKT_STR, proto, PKT_STR, data, PKT_END); |
3497 | } |
3498 | do { |
3499 | crReturnV; |
3500 | } while (!ispkt); |
51470298 |
3501 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3502 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
3503 | bombout(("Protocol confusion")); |
7ffdbc1a |
3504 | crStopV; |
51470298 |
3505 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
32874aea |
3506 | logevent("X11 forwarding refused"); |
3507 | } else { |
3508 | logevent("X11 forwarding enabled"); |
51470298 |
3509 | ssh->X11_fwd_enabled = TRUE; |
9c964e85 |
3510 | } |
3511 | } |
3512 | |
d74d141c |
3513 | { |
9bf696b1 |
3514 | char type; |
d74d141c |
3515 | int n; |
a4fc0d74 |
3516 | int sport,dport,sserv,dserv; |
6ee9b735 |
3517 | char sports[256], dports[256], saddr[256], host[256]; |
d74d141c |
3518 | |
51470298 |
3519 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh1); |
d74d141c |
3520 | /* Add port forwardings. */ |
86916870 |
3521 | ssh->portfwd_strptr = ssh->cfg.portfwd; |
51470298 |
3522 | while (*ssh->portfwd_strptr) { |
3523 | type = *ssh->portfwd_strptr++; |
6ee9b735 |
3524 | saddr[0] = '\0'; |
d74d141c |
3525 | n = 0; |
6ee9b735 |
3526 | while (*ssh->portfwd_strptr && *ssh->portfwd_strptr != '\t') { |
3527 | if (*ssh->portfwd_strptr == ':') { |
3528 | /* |
3529 | * We've seen a colon in the middle of the |
3530 | * source port number. This means that |
3531 | * everything we've seen until now is the |
3532 | * source _address_, so we'll move it into |
3533 | * saddr and start sports from the beginning |
3534 | * again. |
3535 | */ |
3536 | ssh->portfwd_strptr++; |
3537 | sports[n] = '\0'; |
3538 | strcpy(saddr, sports); |
3539 | n = 0; |
3540 | } |
3541 | if (n < 255) sports[n++] = *ssh->portfwd_strptr++; |
3542 | } |
d74d141c |
3543 | sports[n] = 0; |
820ebe3b |
3544 | if (type != 'D') { |
3545 | if (*ssh->portfwd_strptr == '\t') |
3546 | ssh->portfwd_strptr++; |
3547 | n = 0; |
3548 | while (*ssh->portfwd_strptr && *ssh->portfwd_strptr != ':') { |
3549 | if (n < 255) host[n++] = *ssh->portfwd_strptr++; |
3550 | } |
3551 | host[n] = 0; |
3552 | if (*ssh->portfwd_strptr == ':') |
3553 | ssh->portfwd_strptr++; |
3554 | n = 0; |
3555 | while (*ssh->portfwd_strptr) { |
3556 | if (n < 255) dports[n++] = *ssh->portfwd_strptr++; |
3557 | } |
3558 | dports[n] = 0; |
51470298 |
3559 | ssh->portfwd_strptr++; |
820ebe3b |
3560 | dport = atoi(dports); |
3561 | dserv = 0; |
3562 | if (dport == 0) { |
3563 | dserv = 1; |
3564 | dport = net_service_lookup(dports); |
3565 | if (!dport) { |
3566 | logeventf(ssh, "Service lookup failed for" |
3567 | " destination port \"%s\"", dports); |
3568 | } |
a4fc0d74 |
3569 | } |
820ebe3b |
3570 | } else { |
3571 | while (*ssh->portfwd_strptr) ssh->portfwd_strptr++; |
7ffdbc1a |
3572 | dport = dserv = -1; |
7d5b0f4d |
3573 | ssh->portfwd_strptr++; /* eat the NUL and move to next one */ |
a4fc0d74 |
3574 | } |
d74d141c |
3575 | sport = atoi(sports); |
a4fc0d74 |
3576 | sserv = 0; |
3577 | if (sport == 0) { |
3578 | sserv = 1; |
68a49acb |
3579 | sport = net_service_lookup(sports); |
3580 | if (!sport) { |
57356d63 |
3581 | logeventf(ssh, "Service lookup failed for source" |
3582 | " port \"%s\"", sports); |
a4fc0d74 |
3583 | } |
3584 | } |
d74d141c |
3585 | if (sport && dport) { |
94ab1ddf |
3586 | /* Set up a description of the source port. */ |
3587 | char *sportdesc = dupprintf("%.*s%.*s%.*s%.*s%d%.*s", |
3588 | (int)(*saddr?strlen(saddr):0), *saddr?saddr:NULL, |
3589 | (int)(*saddr?1:0), ":", |
3590 | (int)(sserv ? strlen(sports) : 0), sports, |
3591 | sserv, "(", sport, sserv, ")"); |
d74d141c |
3592 | if (type == 'L') { |
94ab1ddf |
3593 | /* Verbose description of the destination port */ |
3594 | char *dportdesc = dupprintf("%s:%.*s%.*s%d%.*s", |
3595 | host, |
3596 | (int)(dserv ? strlen(dports) : 0), dports, |
3597 | dserv, "(", dport, dserv, ")"); |
3598 | const char *err = pfd_addforward(host, dport, |
3599 | *saddr ? saddr : NULL, |
3600 | sport, ssh, &ssh->cfg); |
3601 | if (err) { |
3602 | logeventf(ssh, "Local port %s forward to %s" |
3603 | " failed: %s", sportdesc, dportdesc, err); |
3604 | } else { |
3605 | logeventf(ssh, "Local port %s forwarding to %s", |
3606 | sportdesc, dportdesc); |
3607 | } |
3608 | sfree(dportdesc); |
820ebe3b |
3609 | } else if (type == 'D') { |
94ab1ddf |
3610 | const char *err = pfd_addforward(NULL, -1, |
3611 | *saddr ? saddr : NULL, |
3612 | sport, ssh, &ssh->cfg); |
3613 | if (err) { |
3614 | logeventf(ssh, "Local port %s SOCKS dynamic forward" |
3615 | " setup failed: %s", sportdesc, err); |
3616 | } else { |
3617 | logeventf(ssh, "Local port %s doing SOCKS" |
3618 | " dynamic forwarding", sportdesc); |
3619 | } |
d74d141c |
3620 | } else { |
3621 | struct ssh_rportfwd *pf; |
3d88e64d |
3622 | pf = snew(struct ssh_rportfwd); |
bc240b21 |
3623 | strcpy(pf->dhost, host); |
3624 | pf->dport = dport; |
0cd494d7 |
3625 | if (*saddr) { |
6ee9b735 |
3626 | logeventf(ssh, |
3627 | "SSH1 cannot handle source address spec \"%s:%d\"; ignoring", |
3628 | saddr, sport); |
3629 | } |
51470298 |
3630 | if (add234(ssh->rportfwds, pf) != pf) { |
57356d63 |
3631 | logeventf(ssh, |
3632 | "Duplicate remote port forwarding to %s:%d", |
3633 | host, dport); |
bc240b21 |
3634 | sfree(pf); |
d74d141c |
3635 | } else { |
57356d63 |
3636 | logeventf(ssh, "Requesting remote port %.*s%.*s%d%.*s" |
3637 | " forward to %s:%.*s%.*s%d%.*s", |
3638 | (int)(sserv ? strlen(sports) : 0), sports, |
3639 | sserv, "(", sport, sserv, ")", |
3640 | host, |
3641 | (int)(dserv ? strlen(dports) : 0), dports, |
3642 | dserv, "(", dport, dserv, ")"); |
51470298 |
3643 | send_packet(ssh, SSH1_CMSG_PORT_FORWARD_REQUEST, |
d74d141c |
3644 | PKT_INT, sport, |
3645 | PKT_STR, host, |
3646 | PKT_INT, dport, |
3647 | PKT_END); |
9bf696b1 |
3648 | do { |
3649 | crReturnV; |
3650 | } while (!ispkt); |
51470298 |
3651 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3652 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
3653 | bombout(("Protocol confusion")); |
7ffdbc1a |
3654 | crStopV; |
51470298 |
3655 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
3656 | c_write_str(ssh, "Server refused port" |
3657 | " forwarding\r\n"); |
9bf696b1 |
3658 | } |
3659 | logevent("Remote port forwarding enabled"); |
d74d141c |
3660 | } |
3661 | } |
94ab1ddf |
3662 | sfree(sportdesc); |
d74d141c |
3663 | } |
3664 | } |
3665 | } |
3666 | |
86916870 |
3667 | if (!ssh->cfg.nopty) { |
a5dd8467 |
3668 | /* Unpick the terminal-speed string. */ |
3669 | /* XXX perhaps we should allow no speeds to be sent. */ |
db219738 |
3670 | ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */ |
3671 | sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed); |
a5dd8467 |
3672 | /* Send the pty request. */ |
51470298 |
3673 | send_packet(ssh, SSH1_CMSG_REQUEST_PTY, |
86916870 |
3674 | PKT_STR, ssh->cfg.termtype, |
51470298 |
3675 | PKT_INT, ssh->term_height, |
3676 | PKT_INT, ssh->term_width, |
a5dd8467 |
3677 | PKT_INT, 0, PKT_INT, 0, /* width,height in pixels */ |
db219738 |
3678 | PKT_CHAR, 192, PKT_INT, ssh->ispeed, /* TTY_OP_ISPEED */ |
3679 | PKT_CHAR, 193, PKT_INT, ssh->ospeed, /* TTY_OP_OSPEED */ |
a5dd8467 |
3680 | PKT_CHAR, 0, PKT_END); |
51470298 |
3681 | ssh->state = SSH_STATE_INTERMED; |
32874aea |
3682 | do { |
3683 | crReturnV; |
3684 | } while (!ispkt); |
51470298 |
3685 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3686 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
3687 | bombout(("Protocol confusion")); |
7ffdbc1a |
3688 | crStopV; |
51470298 |
3689 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
3690 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
3691 | ssh->editing = ssh->echoing = 1; |
32874aea |
3692 | } |
a5dd8467 |
3693 | logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)", |
db219738 |
3694 | ssh->ospeed, ssh->ispeed); |
0965bee0 |
3695 | } else { |
51470298 |
3696 | ssh->editing = ssh->echoing = 1; |
374330e2 |
3697 | } |
3698 | |
86916870 |
3699 | if (ssh->cfg.compression) { |
51470298 |
3700 | send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END); |
32874aea |
3701 | do { |
3702 | crReturnV; |
3703 | } while (!ispkt); |
51470298 |
3704 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3705 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
3706 | bombout(("Protocol confusion")); |
7ffdbc1a |
3707 | crStopV; |
51470298 |
3708 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
3709 | c_write_str(ssh, "Server refused to compress\r\n"); |
32874aea |
3710 | } |
4ba9b64b |
3711 | logevent("Started compression"); |
51470298 |
3712 | ssh->v1_compressing = TRUE; |
5366aed8 |
3713 | ssh->cs_comp_ctx = zlib_compress_init(); |
3714 | logevent("Initialised zlib (RFC1950) compression"); |
3715 | ssh->sc_comp_ctx = zlib_decompress_init(); |
3716 | logevent("Initialised zlib (RFC1950) decompression"); |
4ba9b64b |
3717 | } |
3718 | |
fd5e5847 |
3719 | /* |
3720 | * Start the shell or command. |
3721 | * |
3722 | * Special case: if the first-choice command is an SSH2 |
3723 | * subsystem (hence not usable here) and the second choice |
3724 | * exists, we fall straight back to that. |
3725 | */ |
3726 | { |
86916870 |
3727 | char *cmd = ssh->cfg.remote_cmd_ptr; |
fd5e5847 |
3728 | |
86916870 |
3729 | if (ssh->cfg.ssh_subsys && ssh->cfg.remote_cmd_ptr2) { |
3730 | cmd = ssh->cfg.remote_cmd_ptr2; |
51470298 |
3731 | ssh->fallback_cmd = TRUE; |
fd5e5847 |
3732 | } |
3733 | if (*cmd) |
51470298 |
3734 | send_packet(ssh, SSH1_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END); |
fd5e5847 |
3735 | else |
51470298 |
3736 | send_packet(ssh, SSH1_CMSG_EXEC_SHELL, PKT_END); |
fd5e5847 |
3737 | logevent("Started session"); |
3738 | } |
374330e2 |
3739 | |
51470298 |
3740 | ssh->state = SSH_STATE_SESSION; |
3741 | if (ssh->size_needed) |
3742 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
3743 | if (ssh->eof_needed) |
3744 | ssh_special(ssh, TS_EOF); |
374330e2 |
3745 | |
b9d7bcad |
3746 | if (ssh->ldisc) |
3747 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
51470298 |
3748 | ssh->send_ok = 1; |
3749 | ssh->channels = newtree234(ssh_channelcmp); |
374330e2 |
3750 | while (1) { |
3751 | crReturnV; |
3752 | if (ispkt) { |
51470298 |
3753 | if (ssh->pktin.type == SSH1_SMSG_STDOUT_DATA || |
3754 | ssh->pktin.type == SSH1_SMSG_STDERR_DATA) { |
0016d70b |
3755 | char *string; |
3756 | int stringlen, bufsize; |
3757 | |
3758 | ssh_pkt_getstring(ssh, &string, &stringlen); |
3759 | if (string == NULL) { |
3760 | bombout(("Incoming terminal data packet was badly formed")); |
3761 | crStopV; |
3762 | } |
3763 | |
3764 | bufsize = |
51470298 |
3765 | from_backend(ssh->frontend, |
3766 | ssh->pktin.type == SSH1_SMSG_STDERR_DATA, |
0016d70b |
3767 | string, stringlen); |
51470298 |
3768 | if (!ssh->v1_stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) { |
3769 | ssh->v1_stdout_throttling = 1; |
3770 | ssh1_throttle(ssh, +1); |
5471d09a |
3771 | } |
51470298 |
3772 | } else if (ssh->pktin.type == SSH1_MSG_DISCONNECT) { |
3bb2f322 |
3773 | ssh_closing((Plug)ssh, NULL, 0, 0); |
c5e9c988 |
3774 | logevent("Received disconnect request"); |
7ffdbc1a |
3775 | crStopV; |
51470298 |
3776 | } else if (ssh->pktin.type == SSH1_SMSG_X11_OPEN) { |
32874aea |
3777 | /* Remote side is trying to open a channel to talk to our |
3778 | * X-Server. Give them back a local channel number. */ |
3779 | struct ssh_channel *c; |
0016d70b |
3780 | int remoteid = ssh_pkt_getuint32(ssh); |
9c964e85 |
3781 | |
3782 | logevent("Received X11 connect request"); |
3783 | /* Refuse if X11 forwarding is disabled. */ |
51470298 |
3784 | if (!ssh->X11_fwd_enabled) { |
3785 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
0016d70b |
3786 | PKT_INT, remoteid, PKT_END); |
9c964e85 |
3787 | logevent("Rejected X11 connect request"); |
3788 | } else { |
3d88e64d |
3789 | c = snew(struct ssh_channel); |
51470298 |
3790 | c->ssh = ssh; |
9c964e85 |
3791 | |
86916870 |
3792 | if (x11_init(&c->u.x11.s, ssh->cfg.x11_display, c, |
e8fa8f62 |
3793 | ssh->x11auth, NULL, -1, &ssh->cfg) != NULL) { |
0016d70b |
3794 | logevent("Opening X11 forward connection failed"); |
32874aea |
3795 | sfree(c); |
51470298 |
3796 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
0016d70b |
3797 | PKT_INT, remoteid, PKT_END); |
9c964e85 |
3798 | } else { |
32874aea |
3799 | logevent |
0016d70b |
3800 | ("Opening X11 forward connection succeeded"); |
3801 | c->remoteid = remoteid; |
51470298 |
3802 | c->localid = alloc_channel_id(ssh); |
32874aea |
3803 | c->closes = 0; |
5471d09a |
3804 | c->v.v1.throttling = 0; |
32874aea |
3805 | c->type = CHAN_X11; /* identify channel type */ |
51470298 |
3806 | add234(ssh->channels, c); |
3807 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
32874aea |
3808 | PKT_INT, c->remoteid, PKT_INT, |
3809 | c->localid, PKT_END); |
3810 | logevent("Opened X11 forward channel"); |
9c964e85 |
3811 | } |
3812 | } |
51470298 |
3813 | } else if (ssh->pktin.type == SSH1_SMSG_AGENT_OPEN) { |
32874aea |
3814 | /* Remote side is trying to open a channel to talk to our |
3815 | * agent. Give them back a local channel number. */ |
3816 | struct ssh_channel *c; |
0016d70b |
3817 | int remoteid = ssh_pkt_getuint32(ssh); |
db7d555c |
3818 | |
3819 | /* Refuse if agent forwarding is disabled. */ |
51470298 |
3820 | if (!ssh->agentfwd_enabled) { |
3821 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
0016d70b |
3822 | PKT_INT, remoteid, PKT_END); |
db7d555c |
3823 | } else { |
3d88e64d |
3824 | c = snew(struct ssh_channel); |
51470298 |
3825 | c->ssh = ssh; |
0016d70b |
3826 | c->remoteid = remoteid; |
51470298 |
3827 | c->localid = alloc_channel_id(ssh); |
db7d555c |
3828 | c->closes = 0; |
5471d09a |
3829 | c->v.v1.throttling = 0; |
32874aea |
3830 | c->type = CHAN_AGENT; /* identify channel type */ |
db7d555c |
3831 | c->u.a.lensofar = 0; |
51470298 |
3832 | add234(ssh->channels, c); |
3833 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
db7d555c |
3834 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
3835 | PKT_END); |
3836 | } |
51470298 |
3837 | } else if (ssh->pktin.type == SSH1_MSG_PORT_OPEN) { |
d74d141c |
3838 | /* Remote side is trying to open a channel to talk to a |
3839 | * forwarded port. Give them back a local channel number. */ |
3840 | struct ssh_channel *c; |
3841 | struct ssh_rportfwd pf; |
0016d70b |
3842 | int remoteid; |
d74d141c |
3843 | int hostsize, port; |
0016d70b |
3844 | char *host, buf[1024]; |
cbe2d68f |
3845 | const char *e; |
3d88e64d |
3846 | c = snew(struct ssh_channel); |
51470298 |
3847 | c->ssh = ssh; |
d74d141c |
3848 | |
0016d70b |
3849 | remoteid = ssh_pkt_getuint32(ssh); |
3850 | ssh_pkt_getstring(ssh, &host, &hostsize); |
3851 | port = ssh_pkt_getuint32(ssh); |
d74d141c |
3852 | |
0016d70b |
3853 | if (hostsize >= lenof(pf.dhost)) |
3854 | hostsize = lenof(pf.dhost)-1; |
3855 | memcpy(pf.dhost, host, hostsize); |
3856 | pf.dhost[hostsize] = '\0'; |
bc240b21 |
3857 | pf.dport = port; |
d74d141c |
3858 | |
51470298 |
3859 | if (find234(ssh->rportfwds, &pf, NULL) == NULL) { |
d74d141c |
3860 | sprintf(buf, "Rejected remote port open request for %s:%d", |
0016d70b |
3861 | pf.dhost, port); |
d74d141c |
3862 | logevent(buf); |
51470298 |
3863 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
0016d70b |
3864 | PKT_INT, remoteid, PKT_END); |
d74d141c |
3865 | } else { |
3866 | sprintf(buf, "Received remote port open request for %s:%d", |
0016d70b |
3867 | pf.dhost, port); |
d74d141c |
3868 | logevent(buf); |
0016d70b |
3869 | e = pfd_newconnect(&c->u.pfd.s, pf.dhost, port, |
3870 | c, &ssh->cfg); |
d74d141c |
3871 | if (e != NULL) { |
3872 | char buf[256]; |
3873 | sprintf(buf, "Port open failed: %s", e); |
3874 | logevent(buf); |
3875 | sfree(c); |
51470298 |
3876 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
0016d70b |
3877 | PKT_INT, remoteid, PKT_END); |
d74d141c |
3878 | } else { |
0016d70b |
3879 | c->remoteid = remoteid; |
51470298 |
3880 | c->localid = alloc_channel_id(ssh); |
d74d141c |
3881 | c->closes = 0; |
5471d09a |
3882 | c->v.v1.throttling = 0; |
d74d141c |
3883 | c->type = CHAN_SOCKDATA; /* identify channel type */ |
51470298 |
3884 | add234(ssh->channels, c); |
3885 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
d74d141c |
3886 | PKT_INT, c->remoteid, PKT_INT, |
3887 | c->localid, PKT_END); |
3888 | logevent("Forwarded port opened successfully"); |
3889 | } |
3890 | } |
3891 | |
51470298 |
3892 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_OPEN_CONFIRMATION) { |
0016d70b |
3893 | unsigned int remoteid = ssh_pkt_getuint32(ssh); |
3894 | unsigned int localid = ssh_pkt_getuint32(ssh); |
bc240b21 |
3895 | struct ssh_channel *c; |
3896 | |
51470298 |
3897 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
bc240b21 |
3898 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
3899 | c->remoteid = localid; |
3900 | c->type = CHAN_SOCKDATA; |
7781f316 |
3901 | c->v.v1.throttling = 0; |
bc240b21 |
3902 | pfd_confirm(c->u.pfd.s); |
3903 | } |
d74d141c |
3904 | |
4ed34d25 |
3905 | if (c && c->closes) { |
3906 | /* |
3907 | * We have a pending close on this channel, |
3908 | * which we decided on before the server acked |
3909 | * the channel open. So now we know the |
3910 | * remoteid, we can close it again. |
3911 | */ |
51470298 |
3912 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, |
3913 | PKT_INT, c->remoteid, PKT_END); |
4ed34d25 |
3914 | } |
3915 | |
51470298 |
3916 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_OPEN_FAILURE) { |
0016d70b |
3917 | unsigned int remoteid = ssh_pkt_getuint32(ssh); |
724cface |
3918 | struct ssh_channel *c; |
3919 | |
51470298 |
3920 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
724cface |
3921 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
3922 | logevent("Forwarded connection refused by server"); |
3923 | pfd_close(c->u.pfd.s); |
51470298 |
3924 | del234(ssh->channels, c); |
724cface |
3925 | sfree(c); |
3926 | } |
3927 | |
51470298 |
3928 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_CLOSE || |
3929 | ssh->pktin.type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION) { |
32874aea |
3930 | /* Remote side closes a channel. */ |
0016d70b |
3931 | unsigned i = ssh_pkt_getuint32(ssh); |
32874aea |
3932 | struct ssh_channel *c; |
51470298 |
3933 | c = find234(ssh->channels, &i, ssh_channelfind); |
d0d844c1 |
3934 | if (c && ((int)c->remoteid) != -1) { |
32874aea |
3935 | int closetype; |
3936 | closetype = |
51470298 |
3937 | (ssh->pktin.type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2); |
0357890f |
3938 | |
783415f8 |
3939 | if ((c->closes == 0) && (c->type == CHAN_X11)) { |
d74d141c |
3940 | logevent("Forwarded X11 connection terminated"); |
9c964e85 |
3941 | assert(c->u.x11.s != NULL); |
3942 | x11_close(c->u.x11.s); |
3943 | c->u.x11.s = NULL; |
3944 | } |
d74d141c |
3945 | if ((c->closes == 0) && (c->type == CHAN_SOCKDATA)) { |
3946 | logevent("Forwarded port closed"); |
3947 | assert(c->u.pfd.s != NULL); |
3948 | pfd_close(c->u.pfd.s); |
3949 | c->u.pfd.s = NULL; |
3950 | } |
0357890f |
3951 | |
3952 | c->closes |= (closetype << 2); /* seen this message */ |
3953 | if (!(c->closes & closetype)) { |
51470298 |
3954 | send_packet(ssh, ssh->pktin.type, PKT_INT, c->remoteid, |
0357890f |
3955 | PKT_END); |
3956 | c->closes |= closetype; /* sent it too */ |
3957 | } |
3958 | |
3959 | if (c->closes == 15) { |
51470298 |
3960 | del234(ssh->channels, c); |
32874aea |
3961 | sfree(c); |
3962 | } |
d0d844c1 |
3963 | } else { |
6b5cf8b4 |
3964 | bombout(("Received CHANNEL_CLOSE%s for %s channel %d\n", |
51470298 |
3965 | ssh->pktin.type == SSH1_MSG_CHANNEL_CLOSE ? "" : |
d0d844c1 |
3966 | "_CONFIRMATION", c ? "half-open" : "nonexistent", |
3967 | i)); |
7ffdbc1a |
3968 | crStopV; |
32874aea |
3969 | } |
51470298 |
3970 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_DATA) { |
32874aea |
3971 | /* Data sent down one of our channels. */ |
0016d70b |
3972 | int i = ssh_pkt_getuint32(ssh); |
3973 | char *p; |
3974 | int len; |
32874aea |
3975 | struct ssh_channel *c; |
0016d70b |
3976 | |
3977 | ssh_pkt_getstring(ssh, &p, &len); |
3978 | |
51470298 |
3979 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
3980 | if (c) { |
7ffdbc1a |
3981 | int bufsize = 0; |
32874aea |
3982 | switch (c->type) { |
3983 | case CHAN_X11: |
0016d70b |
3984 | bufsize = x11_send(c->u.x11.s, p, len); |
9c964e85 |
3985 | break; |
d74d141c |
3986 | case CHAN_SOCKDATA: |
0016d70b |
3987 | bufsize = pfd_send(c->u.pfd.s, p, len); |
bc240b21 |
3988 | break; |
32874aea |
3989 | case CHAN_AGENT: |
3990 | /* Data for an agent message. Buffer it. */ |
3991 | while (len > 0) { |
3992 | if (c->u.a.lensofar < 4) { |
3993 | int l = min(4 - c->u.a.lensofar, len); |
3994 | memcpy(c->u.a.msglen + c->u.a.lensofar, p, |
3995 | l); |
3996 | p += l; |
3997 | len -= l; |
3998 | c->u.a.lensofar += l; |
3999 | } |
4000 | if (c->u.a.lensofar == 4) { |
4001 | c->u.a.totallen = |
4002 | 4 + GET_32BIT(c->u.a.msglen); |
3d88e64d |
4003 | c->u.a.message = snewn(c->u.a.totallen, |
4004 | unsigned char); |
32874aea |
4005 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
4006 | } |
4007 | if (c->u.a.lensofar >= 4 && len > 0) { |
4008 | int l = |
4009 | min(c->u.a.totallen - c->u.a.lensofar, |
4010 | len); |
4011 | memcpy(c->u.a.message + c->u.a.lensofar, p, |
4012 | l); |
4013 | p += l; |
4014 | len -= l; |
4015 | c->u.a.lensofar += l; |
4016 | } |
4017 | if (c->u.a.lensofar == c->u.a.totallen) { |
839f10db |
4018 | void *reply; |
32874aea |
4019 | int replylen; |
839f10db |
4020 | if (agent_query(c->u.a.message, |
4021 | c->u.a.totallen, |
4022 | &reply, &replylen, |
4023 | ssh_agentf_callback, c)) |
4024 | ssh_agentf_callback(c, reply, replylen); |
32874aea |
4025 | sfree(c->u.a.message); |
4026 | c->u.a.lensofar = 0; |
4027 | } |
4028 | } |
5471d09a |
4029 | bufsize = 0; /* agent channels never back up */ |
32874aea |
4030 | break; |
4031 | } |
90347b95 |
4032 | if (!c->v.v1.throttling && bufsize > SSH1_BUFFER_LIMIT) { |
5471d09a |
4033 | c->v.v1.throttling = 1; |
51470298 |
4034 | ssh1_throttle(ssh, +1); |
5471d09a |
4035 | } |
32874aea |
4036 | } |
51470298 |
4037 | } else if (ssh->pktin.type == SSH1_SMSG_SUCCESS) { |
972a41c8 |
4038 | /* may be from EXEC_SHELL on some servers */ |
51470298 |
4039 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
972a41c8 |
4040 | /* may be from EXEC_SHELL on some servers |
374330e2 |
4041 | * if no pty is available or in other odd cases. Ignore */ |
51470298 |
4042 | } else if (ssh->pktin.type == SSH1_SMSG_EXIT_STATUS) { |
d8d6c7e5 |
4043 | char buf[100]; |
0016d70b |
4044 | ssh->exitcode = ssh_pkt_getuint32(ssh); |
d8d6c7e5 |
4045 | sprintf(buf, "Server sent command exit status %d", |
51470298 |
4046 | ssh->exitcode); |
d8d6c7e5 |
4047 | logevent(buf); |
51470298 |
4048 | send_packet(ssh, SSH1_CMSG_EXIT_CONFIRMATION, PKT_END); |
4252c9cc |
4049 | /* |
4050 | * In case `helpful' firewalls or proxies tack |
4051 | * extra human-readable text on the end of the |
4052 | * session which we might mistake for another |
4053 | * encrypted packet, we close the session once |
4054 | * we've sent EXIT_CONFIRMATION. |
4055 | */ |
3bb2f322 |
4056 | ssh_closing((Plug)ssh, NULL, 0, 0); |
7ffdbc1a |
4057 | crStopV; |
374330e2 |
4058 | } else { |
6b5cf8b4 |
4059 | bombout(("Strange packet received: type %d", ssh->pktin.type)); |
7ffdbc1a |
4060 | crStopV; |
374330e2 |
4061 | } |
4062 | } else { |
8df7a775 |
4063 | while (inlen > 0) { |
4064 | int len = min(inlen, 512); |
9a10ecf4 |
4065 | send_packet(ssh, SSH1_CMSG_STDIN_DATA, PKTT_DATA, |
4066 | PKT_INT, len, PKT_DATA, in, len, |
4067 | PKTT_OTHER, PKT_END); |
8df7a775 |
4068 | in += len; |
4069 | inlen -= len; |
4070 | } |
374330e2 |
4071 | } |
4072 | } |
4073 | |
4074 | crFinishV; |
4075 | } |
4076 | |
4077 | /* |
e5574168 |
4078 | * Utility routine for decoding comma-separated strings in KEXINIT. |
4079 | */ |
32874aea |
4080 | static int in_commasep_string(char *needle, char *haystack, int haylen) |
4081 | { |
57356d63 |
4082 | int needlen; |
4083 | if (!needle || !haystack) /* protect against null pointers */ |
4084 | return 0; |
4085 | needlen = strlen(needle); |
e5574168 |
4086 | while (1) { |
32874aea |
4087 | /* |
4088 | * Is it at the start of the string? |
4089 | */ |
4090 | if (haylen >= needlen && /* haystack is long enough */ |
4091 | !memcmp(needle, haystack, needlen) && /* initial match */ |
4092 | (haylen == needlen || haystack[needlen] == ',') |
4093 | /* either , or EOS follows */ |
4094 | ) |
4095 | return 1; |
4096 | /* |
4097 | * If not, search for the next comma and resume after that. |
4098 | * If no comma found, terminate. |
4099 | */ |
4100 | while (haylen > 0 && *haystack != ',') |
4101 | haylen--, haystack++; |
4102 | if (haylen == 0) |
4103 | return 0; |
4104 | haylen--, haystack++; /* skip over comma itself */ |
e5574168 |
4105 | } |
4106 | } |
4107 | |
4108 | /* |
d39f364a |
4109 | * SSH2 key creation method. |
4110 | */ |
d8baa528 |
4111 | static void ssh2_mkkey(Ssh ssh, Bignum K, unsigned char *H, |
4112 | unsigned char *sessid, char chr, |
4113 | unsigned char *keyspace) |
32874aea |
4114 | { |
d39f364a |
4115 | SHA_State s; |
4116 | /* First 20 bytes. */ |
4117 | SHA_Init(&s); |
51470298 |
4118 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
088bde77 |
4119 | sha_mpint(&s, K); |
d39f364a |
4120 | SHA_Bytes(&s, H, 20); |
4121 | SHA_Bytes(&s, &chr, 1); |
5e0d7cb8 |
4122 | SHA_Bytes(&s, sessid, 20); |
d39f364a |
4123 | SHA_Final(&s, keyspace); |
4124 | /* Next 20 bytes. */ |
4125 | SHA_Init(&s); |
51470298 |
4126 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
088bde77 |
4127 | sha_mpint(&s, K); |
d39f364a |
4128 | SHA_Bytes(&s, H, 20); |
4129 | SHA_Bytes(&s, keyspace, 20); |
32874aea |
4130 | SHA_Final(&s, keyspace + 20); |
d39f364a |
4131 | } |
4132 | |
4133 | /* |
7cca0d81 |
4134 | * Handle the SSH2 transport layer. |
e5574168 |
4135 | */ |
51470298 |
4136 | static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
e5574168 |
4137 | { |
51470298 |
4138 | struct do_ssh2_transport_state { |
4139 | int nbits, pbits, warn; |
4140 | Bignum p, g, e, f, K; |
4141 | int kex_init_value, kex_reply_value; |
4142 | const struct ssh_mac **maclist; |
4143 | int nmacs; |
4144 | const struct ssh2_cipher *cscipher_tobe; |
4145 | const struct ssh2_cipher *sccipher_tobe; |
4146 | const struct ssh_mac *csmac_tobe; |
4147 | const struct ssh_mac *scmac_tobe; |
4148 | const struct ssh_compress *cscomp_tobe; |
4149 | const struct ssh_compress *sccomp_tobe; |
4150 | char *hostkeydata, *sigdata, *keystr, *fingerprint; |
4151 | int hostkeylen, siglen; |
4152 | void *hkey; /* actual host key */ |
4153 | unsigned char exchange_hash[20]; |
4154 | int n_preferred_ciphers; |
4155 | const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX]; |
4156 | const struct ssh_compress *preferred_comp; |
4157 | int first_kex; |
4158 | }; |
4159 | crState(do_ssh2_transport_state); |
4160 | |
4161 | crBegin(ssh->do_ssh2_transport_crstate); |
4162 | |
4163 | s->cscipher_tobe = s->sccipher_tobe = NULL; |
4164 | s->csmac_tobe = s->scmac_tobe = NULL; |
4165 | s->cscomp_tobe = s->sccomp_tobe = NULL; |
4166 | |
7cca0d81 |
4167 | random_init(); |
51470298 |
4168 | s->first_kex = 1; |
e5574168 |
4169 | |
51470298 |
4170 | { |
4171 | int i; |
4172 | /* |
4173 | * Set up the preferred ciphers. (NULL => warn below here) |
4174 | */ |
4175 | s->n_preferred_ciphers = 0; |
4176 | for (i = 0; i < CIPHER_MAX; i++) { |
86916870 |
4177 | switch (ssh->cfg.ssh_cipherlist[i]) { |
51470298 |
4178 | case CIPHER_BLOWFISH: |
4179 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_blowfish; |
4180 | break; |
4181 | case CIPHER_DES: |
86916870 |
4182 | if (ssh->cfg.ssh2_des_cbc) { |
51470298 |
4183 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_des; |
4184 | } |
4185 | break; |
4186 | case CIPHER_3DES: |
4187 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_3des; |
4188 | break; |
4189 | case CIPHER_AES: |
4190 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_aes; |
4191 | break; |
4192 | case CIPHER_WARN: |
4193 | /* Flag for later. Don't bother if it's the last in |
4194 | * the list. */ |
4195 | if (i < CIPHER_MAX - 1) { |
4196 | s->preferred_ciphers[s->n_preferred_ciphers++] = NULL; |
4197 | } |
4198 | break; |
ca20bfcf |
4199 | } |
ca20bfcf |
4200 | } |
5e8358ad |
4201 | } |
ca20bfcf |
4202 | |
4203 | /* |
4204 | * Set up preferred compression. |
4205 | */ |
86916870 |
4206 | if (ssh->cfg.compression) |
51470298 |
4207 | s->preferred_comp = &ssh_zlib; |
4ba9b64b |
4208 | else |
51470298 |
4209 | s->preferred_comp = &ssh_comp_none; |
5e8358ad |
4210 | |
7591b9ff |
4211 | /* |
4212 | * Be prepared to work around the buggy MAC problem. |
4213 | */ |
51470298 |
4214 | if (ssh->remote_bugs & BUG_SSH2_HMAC) |
4215 | s->maclist = buggymacs, s->nmacs = lenof(buggymacs); |
7591b9ff |
4216 | else |
51470298 |
4217 | s->maclist = macs, s->nmacs = lenof(macs); |
7591b9ff |
4218 | |
32874aea |
4219 | begin_key_exchange: |
51470298 |
4220 | { |
4221 | int i, j, cipherstr_started; |
4222 | |
4223 | /* |
4224 | * Construct and send our key exchange packet. |
4225 | */ |
4226 | ssh2_pkt_init(ssh, SSH2_MSG_KEXINIT); |
4227 | for (i = 0; i < 16; i++) |
4228 | ssh2_pkt_addbyte(ssh, (unsigned char) random_byte()); |
4229 | /* List key exchange algorithms. */ |
4230 | ssh2_pkt_addstring_start(ssh); |
4231 | for (i = 0; i < lenof(kex_algs); i++) { |
4232 | if (kex_algs[i] == &ssh_diffiehellman_gex && |
4233 | (ssh->remote_bugs & BUG_SSH2_DH_GEX)) |
4234 | continue; |
4235 | ssh2_pkt_addstring_str(ssh, kex_algs[i]->name); |
4236 | if (i < lenof(kex_algs) - 1) |
4237 | ssh2_pkt_addstring_str(ssh, ","); |
32874aea |
4238 | } |
51470298 |
4239 | /* List server host key algorithms. */ |
4240 | ssh2_pkt_addstring_start(ssh); |
4241 | for (i = 0; i < lenof(hostkey_algs); i++) { |
4242 | ssh2_pkt_addstring_str(ssh, hostkey_algs[i]->name); |
4243 | if (i < lenof(hostkey_algs) - 1) |
4244 | ssh2_pkt_addstring_str(ssh, ","); |
32874aea |
4245 | } |
51470298 |
4246 | /* List client->server encryption algorithms. */ |
4247 | ssh2_pkt_addstring_start(ssh); |
4248 | cipherstr_started = 0; |
4249 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
4250 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
4251 | if (!c) continue; /* warning flag */ |
4252 | for (j = 0; j < c->nciphers; j++) { |
4253 | if (cipherstr_started) |
4254 | ssh2_pkt_addstring_str(ssh, ","); |
4255 | ssh2_pkt_addstring_str(ssh, c->list[j]->name); |
4256 | cipherstr_started = 1; |
4257 | } |
4258 | } |
4259 | /* List server->client encryption algorithms. */ |
4260 | ssh2_pkt_addstring_start(ssh); |
4261 | cipherstr_started = 0; |
4262 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
4263 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
4264 | if (!c) continue; /* warning flag */ |
4265 | for (j = 0; j < c->nciphers; j++) { |
4266 | if (cipherstr_started) |
4267 | ssh2_pkt_addstring_str(ssh, ","); |
4268 | ssh2_pkt_addstring_str(ssh, c->list[j]->name); |
4269 | cipherstr_started = 1; |
4270 | } |
4271 | } |
4272 | /* List client->server MAC algorithms. */ |
4273 | ssh2_pkt_addstring_start(ssh); |
4274 | for (i = 0; i < s->nmacs; i++) { |
4275 | ssh2_pkt_addstring_str(ssh, s->maclist[i]->name); |
4276 | if (i < s->nmacs - 1) |
4277 | ssh2_pkt_addstring_str(ssh, ","); |
4278 | } |
4279 | /* List server->client MAC algorithms. */ |
4280 | ssh2_pkt_addstring_start(ssh); |
4281 | for (i = 0; i < s->nmacs; i++) { |
4282 | ssh2_pkt_addstring_str(ssh, s->maclist[i]->name); |
4283 | if (i < s->nmacs - 1) |
4284 | ssh2_pkt_addstring_str(ssh, ","); |
4285 | } |
4286 | /* List client->server compression algorithms. */ |
4287 | ssh2_pkt_addstring_start(ssh); |
f6e0abe2 |
4288 | assert(lenof(compressions) > 1); |
4289 | ssh2_pkt_addstring_str(ssh, s->preferred_comp->name); |
4290 | for (i = 0; i < lenof(compressions); i++) { |
4291 | const struct ssh_compress *c = compressions[i]; |
4292 | if (c != s->preferred_comp) { |
51470298 |
4293 | ssh2_pkt_addstring_str(ssh, ","); |
f6e0abe2 |
4294 | ssh2_pkt_addstring_str(ssh, c->name); |
4295 | } |
51470298 |
4296 | } |
4297 | /* List server->client compression algorithms. */ |
4298 | ssh2_pkt_addstring_start(ssh); |
f6e0abe2 |
4299 | assert(lenof(compressions) > 1); |
4300 | ssh2_pkt_addstring_str(ssh, s->preferred_comp->name); |
4301 | for (i = 0; i < lenof(compressions); i++) { |
4302 | const struct ssh_compress *c = compressions[i]; |
4303 | if (c != s->preferred_comp) { |
51470298 |
4304 | ssh2_pkt_addstring_str(ssh, ","); |
f6e0abe2 |
4305 | ssh2_pkt_addstring_str(ssh, c->name); |
4306 | } |
51470298 |
4307 | } |
4308 | /* List client->server languages. Empty list. */ |
4309 | ssh2_pkt_addstring_start(ssh); |
4310 | /* List server->client languages. Empty list. */ |
4311 | ssh2_pkt_addstring_start(ssh); |
4312 | /* First KEX packet does _not_ follow, because we're not that brave. */ |
4313 | ssh2_pkt_addbool(ssh, FALSE); |
4314 | /* Reserved. */ |
4315 | ssh2_pkt_adduint32(ssh, 0); |
e5574168 |
4316 | } |
0db56f73 |
4317 | |
51470298 |
4318 | ssh->exhash = ssh->exhashbase; |
4319 | sha_string(&ssh->exhash, ssh->pktout.data + 5, ssh->pktout.length - 5); |
0db56f73 |
4320 | |
51470298 |
4321 | ssh2_pkt_send(ssh); |
e5574168 |
4322 | |
32874aea |
4323 | if (!ispkt) |
4324 | crWaitUntil(ispkt); |
57356d63 |
4325 | if (ssh->pktin.length > 5) |
4326 | sha_string(&ssh->exhash, ssh->pktin.data + 5, ssh->pktin.length - 5); |
e5574168 |
4327 | |
4328 | /* |
4329 | * Now examine the other side's KEXINIT to see what we're up |
4330 | * to. |
4331 | */ |
51470298 |
4332 | { |
4333 | char *str; |
4334 | int i, j, len; |
4335 | |
4336 | if (ssh->pktin.type != SSH2_MSG_KEXINIT) { |
6b5cf8b4 |
4337 | bombout(("expected key exchange packet from server")); |
7ffdbc1a |
4338 | crStop(0); |
32874aea |
4339 | } |
51470298 |
4340 | ssh->kex = NULL; |
4341 | ssh->hostkey = NULL; |
4342 | s->cscipher_tobe = NULL; |
4343 | s->sccipher_tobe = NULL; |
4344 | s->csmac_tobe = NULL; |
4345 | s->scmac_tobe = NULL; |
4346 | s->cscomp_tobe = NULL; |
4347 | s->sccomp_tobe = NULL; |
4348 | ssh->pktin.savedpos += 16; /* skip garbage cookie */ |
0016d70b |
4349 | ssh_pkt_getstring(ssh, &str, &len); /* key exchange algorithms */ |
51470298 |
4350 | for (i = 0; i < lenof(kex_algs); i++) { |
4351 | if (kex_algs[i] == &ssh_diffiehellman_gex && |
4352 | (ssh->remote_bugs & BUG_SSH2_DH_GEX)) |
4353 | continue; |
4354 | if (in_commasep_string(kex_algs[i]->name, str, len)) { |
4355 | ssh->kex = kex_algs[i]; |
4356 | break; |
4357 | } |
32874aea |
4358 | } |
0016d70b |
4359 | ssh_pkt_getstring(ssh, &str, &len); /* host key algorithms */ |
51470298 |
4360 | for (i = 0; i < lenof(hostkey_algs); i++) { |
4361 | if (in_commasep_string(hostkey_algs[i]->name, str, len)) { |
4362 | ssh->hostkey = hostkey_algs[i]; |
4363 | break; |
4364 | } |
4365 | } |
0016d70b |
4366 | ssh_pkt_getstring(ssh, &str, &len); /* client->server cipher */ |
51470298 |
4367 | s->warn = 0; |
4368 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
4369 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
4370 | if (!c) { |
4371 | s->warn = 1; |
4372 | } else { |
4373 | for (j = 0; j < c->nciphers; j++) { |
4374 | if (in_commasep_string(c->list[j]->name, str, len)) { |
4375 | s->cscipher_tobe = c->list[j]; |
4376 | break; |
4377 | } |
ca20bfcf |
4378 | } |
32874aea |
4379 | } |
51470298 |
4380 | if (s->cscipher_tobe) { |
4381 | if (s->warn) |
a8327734 |
4382 | askcipher(ssh->frontend, s->cscipher_tobe->name, 1); |
51470298 |
4383 | break; |
4384 | } |
32874aea |
4385 | } |
51470298 |
4386 | if (!s->cscipher_tobe) { |
6b5cf8b4 |
4387 | bombout(("Couldn't agree a client-to-server cipher (available: %s)", |
57356d63 |
4388 | str ? str : "(null)")); |
7ffdbc1a |
4389 | crStop(0); |
ca20bfcf |
4390 | } |
0ef8f407 |
4391 | |
0016d70b |
4392 | ssh_pkt_getstring(ssh, &str, &len); /* server->client cipher */ |
51470298 |
4393 | s->warn = 0; |
4394 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
4395 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
4396 | if (!c) { |
4397 | s->warn = 1; |
4398 | } else { |
4399 | for (j = 0; j < c->nciphers; j++) { |
4400 | if (in_commasep_string(c->list[j]->name, str, len)) { |
4401 | s->sccipher_tobe = c->list[j]; |
4402 | break; |
4403 | } |
ca20bfcf |
4404 | } |
32874aea |
4405 | } |
51470298 |
4406 | if (s->sccipher_tobe) { |
4407 | if (s->warn) |
a8327734 |
4408 | askcipher(ssh->frontend, s->sccipher_tobe->name, 2); |
51470298 |
4409 | break; |
4410 | } |
32874aea |
4411 | } |
51470298 |
4412 | if (!s->sccipher_tobe) { |
6b5cf8b4 |
4413 | bombout(("Couldn't agree a server-to-client cipher (available: %s)", |
57356d63 |
4414 | str ? str : "(null)")); |
7ffdbc1a |
4415 | crStop(0); |
ca20bfcf |
4416 | } |
0ef8f407 |
4417 | |
0016d70b |
4418 | ssh_pkt_getstring(ssh, &str, &len); /* client->server mac */ |
51470298 |
4419 | for (i = 0; i < s->nmacs; i++) { |
4420 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
4421 | s->csmac_tobe = s->maclist[i]; |
4422 | break; |
4423 | } |
32874aea |
4424 | } |
0016d70b |
4425 | ssh_pkt_getstring(ssh, &str, &len); /* server->client mac */ |
51470298 |
4426 | for (i = 0; i < s->nmacs; i++) { |
4427 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
4428 | s->scmac_tobe = s->maclist[i]; |
4429 | break; |
4430 | } |
32874aea |
4431 | } |
0016d70b |
4432 | ssh_pkt_getstring(ssh, &str, &len); /* client->server compression */ |
51470298 |
4433 | for (i = 0; i < lenof(compressions) + 1; i++) { |
4434 | const struct ssh_compress *c = |
4435 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
4436 | if (in_commasep_string(c->name, str, len)) { |
4437 | s->cscomp_tobe = c; |
4438 | break; |
4439 | } |
32874aea |
4440 | } |
0016d70b |
4441 | ssh_pkt_getstring(ssh, &str, &len); /* server->client compression */ |
51470298 |
4442 | for (i = 0; i < lenof(compressions) + 1; i++) { |
4443 | const struct ssh_compress *c = |
4444 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
4445 | if (in_commasep_string(c->name, str, len)) { |
4446 | s->sccomp_tobe = c; |
4447 | break; |
4448 | } |
32874aea |
4449 | } |
e5574168 |
4450 | } |
e5574168 |
4451 | |
4452 | /* |
7bd5a860 |
4453 | * Work out the number of bits of key we will need from the key |
4454 | * exchange. We start with the maximum key length of either |
4455 | * cipher... |
4456 | */ |
4457 | { |
32874aea |
4458 | int csbits, scbits; |
7bd5a860 |
4459 | |
51470298 |
4460 | csbits = s->cscipher_tobe->keylen; |
4461 | scbits = s->sccipher_tobe->keylen; |
4462 | s->nbits = (csbits > scbits ? csbits : scbits); |
7bd5a860 |
4463 | } |
4464 | /* The keys only have 160-bit entropy, since they're based on |
4465 | * a SHA-1 hash. So cap the key size at 160 bits. */ |
51470298 |
4466 | if (s->nbits > 160) |
4467 | s->nbits = 160; |
7bd5a860 |
4468 | |
4469 | /* |
a92dd380 |
4470 | * If we're doing Diffie-Hellman group exchange, start by |
4471 | * requesting a group. |
e5574168 |
4472 | */ |
51470298 |
4473 | if (ssh->kex == &ssh_diffiehellman_gex) { |
32874aea |
4474 | logevent("Doing Diffie-Hellman group exchange"); |
51470298 |
4475 | ssh->pkt_ctx |= SSH2_PKTCTX_DHGEX; |
32874aea |
4476 | /* |
4477 | * Work out how big a DH group we will need to allow that |
4478 | * much data. |
7bd5a860 |
4479 | */ |
51470298 |
4480 | s->pbits = 512 << ((s->nbits - 1) / 64); |
4481 | ssh2_pkt_init(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST); |
4482 | ssh2_pkt_adduint32(ssh, s->pbits); |
4483 | ssh2_pkt_send(ssh); |
32874aea |
4484 | |
4485 | crWaitUntil(ispkt); |
51470298 |
4486 | if (ssh->pktin.type != SSH2_MSG_KEX_DH_GEX_GROUP) { |
6b5cf8b4 |
4487 | bombout(("expected key exchange group packet from server")); |
7ffdbc1a |
4488 | crStop(0); |
32874aea |
4489 | } |
51470298 |
4490 | s->p = ssh2_pkt_getmp(ssh); |
4491 | s->g = ssh2_pkt_getmp(ssh); |
27cd7fc2 |
4492 | ssh->kex_ctx = dh_setup_group(s->p, s->g); |
51470298 |
4493 | s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT; |
4494 | s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY; |
a92dd380 |
4495 | } else { |
51470298 |
4496 | ssh->pkt_ctx |= SSH2_PKTCTX_DHGROUP1; |
27cd7fc2 |
4497 | ssh->kex_ctx = dh_setup_group1(); |
51470298 |
4498 | s->kex_init_value = SSH2_MSG_KEXDH_INIT; |
4499 | s->kex_reply_value = SSH2_MSG_KEXDH_REPLY; |
8d5de777 |
4500 | } |
e5574168 |
4501 | |
a92dd380 |
4502 | logevent("Doing Diffie-Hellman key exchange"); |
e5574168 |
4503 | /* |
a92dd380 |
4504 | * Now generate and send e for Diffie-Hellman. |
e5574168 |
4505 | */ |
27cd7fc2 |
4506 | s->e = dh_create_e(ssh->kex_ctx, s->nbits * 2); |
51470298 |
4507 | ssh2_pkt_init(ssh, s->kex_init_value); |
4508 | ssh2_pkt_addmp(ssh, s->e); |
4509 | ssh2_pkt_send(ssh); |
e5574168 |
4510 | |
4511 | crWaitUntil(ispkt); |
51470298 |
4512 | if (ssh->pktin.type != s->kex_reply_value) { |
6b5cf8b4 |
4513 | bombout(("expected key exchange reply packet from server")); |
7ffdbc1a |
4514 | crStop(0); |
7cca0d81 |
4515 | } |
0016d70b |
4516 | ssh_pkt_getstring(ssh, &s->hostkeydata, &s->hostkeylen); |
51470298 |
4517 | s->f = ssh2_pkt_getmp(ssh); |
0016d70b |
4518 | ssh_pkt_getstring(ssh, &s->sigdata, &s->siglen); |
e5574168 |
4519 | |
27cd7fc2 |
4520 | s->K = dh_find_K(ssh->kex_ctx, s->f); |
e5574168 |
4521 | |
51470298 |
4522 | sha_string(&ssh->exhash, s->hostkeydata, s->hostkeylen); |
4523 | if (ssh->kex == &ssh_diffiehellman_gex) { |
4524 | sha_uint32(&ssh->exhash, s->pbits); |
4525 | sha_mpint(&ssh->exhash, s->p); |
4526 | sha_mpint(&ssh->exhash, s->g); |
a92dd380 |
4527 | } |
51470298 |
4528 | sha_mpint(&ssh->exhash, s->e); |
4529 | sha_mpint(&ssh->exhash, s->f); |
4530 | sha_mpint(&ssh->exhash, s->K); |
4531 | SHA_Final(&ssh->exhash, s->exchange_hash); |
e5574168 |
4532 | |
27cd7fc2 |
4533 | dh_cleanup(ssh->kex_ctx); |
fabd1805 |
4534 | ssh->kex_ctx = NULL; |
3709bfe9 |
4535 | |
7cca0d81 |
4536 | #if 0 |
765c4200 |
4537 | debug(("Exchange hash is:\n")); |
51470298 |
4538 | dmemdump(s->exchange_hash, 20); |
7cca0d81 |
4539 | #endif |
4540 | |
51470298 |
4541 | s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen); |
4542 | if (!s->hkey || |
4543 | !ssh->hostkey->verifysig(s->hkey, s->sigdata, s->siglen, |
d8baa528 |
4544 | (char *)s->exchange_hash, 20)) { |
6b5cf8b4 |
4545 | bombout(("Server's host key did not match the signature supplied")); |
7ffdbc1a |
4546 | crStop(0); |
8d5de777 |
4547 | } |
e5574168 |
4548 | |
4549 | /* |
7cca0d81 |
4550 | * Authenticate remote host: verify host key. (We've already |
4551 | * checked the signature of the exchange hash.) |
e5574168 |
4552 | */ |
51470298 |
4553 | s->keystr = ssh->hostkey->fmtkey(s->hkey); |
4554 | s->fingerprint = ssh->hostkey->fingerprint(s->hkey); |
a8327734 |
4555 | verify_ssh_host_key(ssh->frontend, |
4556 | ssh->savedhost, ssh->savedport, ssh->hostkey->keytype, |
51470298 |
4557 | s->keystr, s->fingerprint); |
4558 | if (s->first_kex) { /* don't bother logging this in rekeys */ |
5e0d7cb8 |
4559 | logevent("Host key fingerprint is:"); |
51470298 |
4560 | logevent(s->fingerprint); |
5e0d7cb8 |
4561 | } |
51470298 |
4562 | sfree(s->fingerprint); |
4563 | sfree(s->keystr); |
4564 | ssh->hostkey->freekey(s->hkey); |
d39f364a |
4565 | |
4566 | /* |
7cca0d81 |
4567 | * Send SSH2_MSG_NEWKEYS. |
d39f364a |
4568 | */ |
51470298 |
4569 | ssh2_pkt_init(ssh, SSH2_MSG_NEWKEYS); |
4570 | ssh2_pkt_send(ssh); |
d39f364a |
4571 | |
4572 | /* |
8406eaf9 |
4573 | * Expect SSH2_MSG_NEWKEYS from server. |
4574 | */ |
4575 | crWaitUntil(ispkt); |
51470298 |
4576 | if (ssh->pktin.type != SSH2_MSG_NEWKEYS) { |
6b5cf8b4 |
4577 | bombout(("expected new-keys packet from server")); |
7ffdbc1a |
4578 | crStop(0); |
8406eaf9 |
4579 | } |
4580 | |
4581 | /* |
d39f364a |
4582 | * Create and initialise session keys. |
4583 | */ |
371e569c |
4584 | if (ssh->cs_cipher_ctx) |
4585 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
51470298 |
4586 | ssh->cscipher = s->cscipher_tobe; |
371e569c |
4587 | ssh->cs_cipher_ctx = ssh->cscipher->make_context(); |
e0e1a00d |
4588 | |
371e569c |
4589 | if (ssh->sc_cipher_ctx) |
4590 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
51470298 |
4591 | ssh->sccipher = s->sccipher_tobe; |
371e569c |
4592 | ssh->sc_cipher_ctx = ssh->sccipher->make_context(); |
e0e1a00d |
4593 | |
4594 | if (ssh->cs_mac_ctx) |
4595 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
51470298 |
4596 | ssh->csmac = s->csmac_tobe; |
e0e1a00d |
4597 | ssh->cs_mac_ctx = ssh->csmac->make_context(); |
4598 | |
4599 | if (ssh->sc_mac_ctx) |
4600 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
51470298 |
4601 | ssh->scmac = s->scmac_tobe; |
e0e1a00d |
4602 | ssh->sc_mac_ctx = ssh->scmac->make_context(); |
4603 | |
5366aed8 |
4604 | if (ssh->cs_comp_ctx) |
4605 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
51470298 |
4606 | ssh->cscomp = s->cscomp_tobe; |
5366aed8 |
4607 | ssh->cs_comp_ctx = ssh->cscomp->compress_init(); |
4608 | |
4609 | if (ssh->sc_comp_ctx) |
4610 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
51470298 |
4611 | ssh->sccomp = s->sccomp_tobe; |
5366aed8 |
4612 | ssh->sc_comp_ctx = ssh->sccomp->decompress_init(); |
4613 | |
d39f364a |
4614 | /* |
5e0d7cb8 |
4615 | * Set IVs after keys. Here we use the exchange hash from the |
4616 | * _first_ key exchange. |
d39f364a |
4617 | */ |
51470298 |
4618 | { |
4619 | unsigned char keyspace[40]; |
4620 | if (s->first_kex) |
4621 | memcpy(ssh->v2_session_id, s->exchange_hash, |
4622 | sizeof(s->exchange_hash)); |
4623 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'C',keyspace); |
371e569c |
4624 | ssh->cscipher->setkey(ssh->cs_cipher_ctx, keyspace); |
51470298 |
4625 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'D',keyspace); |
371e569c |
4626 | ssh->sccipher->setkey(ssh->sc_cipher_ctx, keyspace); |
51470298 |
4627 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'A',keyspace); |
371e569c |
4628 | ssh->cscipher->setiv(ssh->cs_cipher_ctx, keyspace); |
51470298 |
4629 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'B',keyspace); |
371e569c |
4630 | ssh->sccipher->setiv(ssh->sc_cipher_ctx, keyspace); |
51470298 |
4631 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'E',keyspace); |
e0e1a00d |
4632 | ssh->csmac->setkey(ssh->cs_mac_ctx, keyspace); |
51470298 |
4633 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'F',keyspace); |
e0e1a00d |
4634 | ssh->scmac->setkey(ssh->sc_mac_ctx, keyspace); |
51470298 |
4635 | } |
57356d63 |
4636 | logeventf(ssh, "Initialised %.200s client->server encryption", |
4637 | ssh->cscipher->text_name); |
4638 | logeventf(ssh, "Initialised %.200s server->client encryption", |
4639 | ssh->sccipher->text_name); |
6c135243 |
4640 | logeventf(ssh, "Initialised %.200s client->server MAC algorithm", |
4641 | ssh->csmac->text_name); |
4642 | logeventf(ssh, "Initialised %.200s server->client MAC algorithm", |
4643 | ssh->scmac->text_name); |
57356d63 |
4644 | if (ssh->cscomp->text_name) |
4645 | logeventf(ssh, "Initialised %s compression", |
4646 | ssh->cscomp->text_name); |
4647 | if (ssh->sccomp->text_name) |
4648 | logeventf(ssh, "Initialised %s decompression", |
4649 | ssh->sccomp->text_name); |
679539d7 |
4650 | freebn(s->f); |
679539d7 |
4651 | freebn(s->K); |
b3949e7e |
4652 | if (ssh->kex == &ssh_diffiehellman_gex) { |
4653 | freebn(s->g); |
4654 | freebn(s->p); |
4655 | } |
d39f364a |
4656 | |
033b4cef |
4657 | /* |
0db56f73 |
4658 | * If this is the first key exchange phase, we must pass the |
4659 | * SSH2_MSG_NEWKEYS packet to the next layer, not because it |
4660 | * wants to see it but because it will need time to initialise |
4661 | * itself before it sees an actual packet. In subsequent key |
4662 | * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because |
4663 | * it would only confuse the layer above. |
4664 | */ |
51470298 |
4665 | if (!s->first_kex) { |
32874aea |
4666 | crReturn(0); |
0db56f73 |
4667 | } |
51470298 |
4668 | s->first_kex = 0; |
0db56f73 |
4669 | |
4670 | /* |
7cca0d81 |
4671 | * Now we're encrypting. Begin returning 1 to the protocol main |
4672 | * function so that other things can run on top of the |
4673 | * transport. If we ever see a KEXINIT, we must go back to the |
4674 | * start. |
033b4cef |
4675 | */ |
51470298 |
4676 | while (!(ispkt && ssh->pktin.type == SSH2_MSG_KEXINIT)) { |
32874aea |
4677 | crReturn(1); |
e96adf72 |
4678 | } |
5e0d7cb8 |
4679 | logevent("Server initiated key re-exchange"); |
7cca0d81 |
4680 | goto begin_key_exchange; |
e5574168 |
4681 | |
4682 | crFinish(1); |
4683 | } |
4684 | |
7cca0d81 |
4685 | /* |
783415f8 |
4686 | * Add data to an SSH2 channel output buffer. |
4687 | */ |
32874aea |
4688 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, |
4689 | int len) |
4690 | { |
5471d09a |
4691 | bufchain_add(&c->v.v2.outbuffer, buf, len); |
783415f8 |
4692 | } |
4693 | |
4694 | /* |
4695 | * Attempt to send data on an SSH2 channel. |
4696 | */ |
5471d09a |
4697 | static int ssh2_try_send(struct ssh_channel *c) |
32874aea |
4698 | { |
51470298 |
4699 | Ssh ssh = c->ssh; |
4700 | |
5471d09a |
4701 | while (c->v.v2.remwindow > 0 && bufchain_size(&c->v.v2.outbuffer) > 0) { |
4702 | int len; |
4703 | void *data; |
4704 | bufchain_prefix(&c->v.v2.outbuffer, &data, &len); |
4705 | if ((unsigned)len > c->v.v2.remwindow) |
4706 | len = c->v.v2.remwindow; |
4707 | if ((unsigned)len > c->v.v2.remmaxpkt) |
4708 | len = c->v.v2.remmaxpkt; |
51470298 |
4709 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_DATA); |
4710 | ssh2_pkt_adduint32(ssh, c->remoteid); |
9a10ecf4 |
4711 | dont_log_data(ssh, PKTLOG_OMIT); |
51470298 |
4712 | ssh2_pkt_addstring_start(ssh); |
4713 | ssh2_pkt_addstring_data(ssh, data, len); |
9a10ecf4 |
4714 | end_log_omission(ssh); |
51470298 |
4715 | ssh2_pkt_send(ssh); |
5471d09a |
4716 | bufchain_consume(&c->v.v2.outbuffer, len); |
4717 | c->v.v2.remwindow -= len; |
4718 | } |
4719 | |
4720 | /* |
4721 | * After having sent as much data as we can, return the amount |
4722 | * still buffered. |
4723 | */ |
4724 | return bufchain_size(&c->v.v2.outbuffer); |
4725 | } |
4726 | |
4727 | /* |
4728 | * Potentially enlarge the window on an SSH2 channel. |
4729 | */ |
4730 | static void ssh2_set_window(struct ssh_channel *c, unsigned newwin) |
4731 | { |
51470298 |
4732 | Ssh ssh = c->ssh; |
4733 | |
6b69f42e |
4734 | /* |
4735 | * Never send WINDOW_ADJUST for a channel that the remote side |
4736 | * already thinks it's closed; there's no point, since it won't |
4737 | * be sending any more data anyway. |
4738 | */ |
4739 | if (c->closes != 0) |
4740 | return; |
4741 | |
5471d09a |
4742 | if (newwin > c->v.v2.locwindow) { |
51470298 |
4743 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
4744 | ssh2_pkt_adduint32(ssh, c->remoteid); |
4745 | ssh2_pkt_adduint32(ssh, newwin - c->v.v2.locwindow); |
4746 | ssh2_pkt_send(ssh); |
5471d09a |
4747 | c->v.v2.locwindow = newwin; |
783415f8 |
4748 | } |
4749 | } |
4750 | |
4751 | /* |
7cca0d81 |
4752 | * Handle the SSH2 userauth and connection layers. |
4753 | */ |
51470298 |
4754 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
7cca0d81 |
4755 | { |
51470298 |
4756 | struct do_ssh2_authconn_state { |
4757 | enum { |
4758 | AUTH_INVALID, AUTH_PUBLICKEY_AGENT, AUTH_PUBLICKEY_FILE, |
4759 | AUTH_PASSWORD, |
4760 | AUTH_KEYBOARD_INTERACTIVE |
4761 | } method; |
4762 | enum { |
4763 | AUTH_TYPE_NONE, |
4764 | AUTH_TYPE_PUBLICKEY, |
4765 | AUTH_TYPE_PUBLICKEY_OFFER_LOUD, |
4766 | AUTH_TYPE_PUBLICKEY_OFFER_QUIET, |
4767 | AUTH_TYPE_PASSWORD, |
4768 | AUTH_TYPE_KEYBOARD_INTERACTIVE, |
4769 | AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET |
4770 | } type; |
4771 | int gotit, need_pw, can_pubkey, can_passwd, can_keyb_inter; |
4772 | int tried_pubkey_config, tried_agent, tried_keyb_inter; |
4773 | int kbd_inter_running; |
4774 | int we_are_in; |
4775 | int num_prompts, curr_prompt, echo; |
4776 | char username[100]; |
4777 | int got_username; |
85fdbe25 |
4778 | char pwprompt[512]; |
51470298 |
4779 | char password[100]; |
4780 | void *publickey_blob; |
4781 | int publickey_bloblen; |
4782 | unsigned char request[5], *response, *p; |
4783 | int responselen; |
4784 | int keyi, nkeys; |
4785 | int authed; |
4786 | char *pkblob, *alg, *commentp; |
4787 | int pklen, alglen, commentlen; |
4788 | int siglen, retlen, len; |
4789 | char *q, *agentreq, *ret; |
4790 | int try_send; |
4791 | }; |
4792 | crState(do_ssh2_authconn_state); |
4793 | |
4794 | crBegin(ssh->do_ssh2_authconn_crstate); |
e5574168 |
4795 | |
7cca0d81 |
4796 | /* |
4797 | * Request userauth protocol, and await a response to it. |
4798 | */ |
51470298 |
4799 | ssh2_pkt_init(ssh, SSH2_MSG_SERVICE_REQUEST); |
4800 | ssh2_pkt_addstring(ssh, "ssh-userauth"); |
4801 | ssh2_pkt_send(ssh); |
7cca0d81 |
4802 | crWaitUntilV(ispkt); |
51470298 |
4803 | if (ssh->pktin.type != SSH2_MSG_SERVICE_ACCEPT) { |
6b5cf8b4 |
4804 | bombout(("Server refused user authentication protocol")); |
7ffdbc1a |
4805 | crStopV; |
8d5de777 |
4806 | } |
7cca0d81 |
4807 | |
4808 | /* |
1408a877 |
4809 | * We repeat this whole loop, including the username prompt, |
4810 | * until we manage a successful authentication. If the user |
51470298 |
4811 | * types the wrong _password_, they can be sent back to the |
4812 | * beginning to try another username, if this is configured on. |
4813 | * (If they specify a username in the config, they are never |
4814 | * asked, even if they do give a wrong password.) |
1408a877 |
4815 | * |
4816 | * I think this best serves the needs of |
4817 | * |
4818 | * - the people who have no configuration, no keys, and just |
4819 | * want to try repeated (username,password) pairs until they |
4820 | * type both correctly |
4821 | * |
4822 | * - people who have keys and configuration but occasionally |
4823 | * need to fall back to passwords |
4824 | * |
4825 | * - people with a key held in Pageant, who might not have |
4826 | * logged in to a particular machine before; so they want to |
4827 | * type a username, and then _either_ their key will be |
4828 | * accepted, _or_ they will type a password. If they mistype |
4829 | * the username they will want to be able to get back and |
4830 | * retype it! |
7cca0d81 |
4831 | */ |
51470298 |
4832 | s->username[0] = '\0'; |
4833 | s->got_username = FALSE; |
1408a877 |
4834 | do { |
1408a877 |
4835 | /* |
4836 | * Get a username. |
4837 | */ |
86916870 |
4838 | if (s->got_username && !ssh->cfg.change_username) { |
5bb641e1 |
4839 | /* |
4840 | * We got a username last time round this loop, and |
4841 | * with change_username turned off we don't try to get |
4842 | * it again. |
4843 | */ |
86916870 |
4844 | } else if ((flags & FLAG_INTERACTIVE) && !*ssh->cfg.username) { |
c0a81592 |
4845 | if (ssh_get_line && !ssh_getline_pw_only) { |
32874aea |
4846 | if (!ssh_get_line("login as: ", |
51470298 |
4847 | s->username, sizeof(s->username), FALSE)) { |
32874aea |
4848 | /* |
4849 | * get_line failed to get a username. |
4850 | * Terminate. |
4851 | */ |
4852 | logevent("No username provided. Abandoning session."); |
3bb2f322 |
4853 | ssh_closing((Plug)ssh, NULL, 0, 0); |
7ffdbc1a |
4854 | crStopV; |
32874aea |
4855 | } |
4856 | } else { |
51470298 |
4857 | int ret; /* need not be saved across crReturn */ |
4858 | c_write_str(ssh, "login as: "); |
4859 | ssh->send_ok = 1; |
4860 | setup_userpass_input(ssh, s->username, sizeof(s->username), 1); |
0405e71f |
4861 | do { |
32874aea |
4862 | crWaitUntilV(!ispkt); |
51470298 |
4863 | ret = process_userpass_input(ssh, in, inlen); |
0405e71f |
4864 | } while (ret == 0); |
4865 | if (ret < 0) |
4866 | cleanup_exit(0); |
4ca0c9d1 |
4867 | c_write_str(ssh, "\r\n"); |
32874aea |
4868 | } |
51470298 |
4869 | s->username[strcspn(s->username, "\n\r")] = '\0'; |
7cca0d81 |
4870 | } else { |
57356d63 |
4871 | char *stuff; |
86916870 |
4872 | strncpy(s->username, ssh->cfg.username, sizeof(s->username)); |
51470298 |
4873 | s->username[sizeof(s->username)-1] = '\0'; |
65a22376 |
4874 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
57356d63 |
4875 | stuff = dupprintf("Using username \"%s\".\r\n", s->username); |
51470298 |
4876 | c_write_str(ssh, stuff); |
57356d63 |
4877 | sfree(stuff); |
7cca0d81 |
4878 | } |
4879 | } |
51470298 |
4880 | s->got_username = TRUE; |
7cca0d81 |
4881 | |
65a22376 |
4882 | /* |
1408a877 |
4883 | * Send an authentication request using method "none": (a) |
4884 | * just in case it succeeds, and (b) so that we know what |
4885 | * authentication methods we can usefully try next. |
65a22376 |
4886 | */ |
51470298 |
4887 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
4888 | |
4889 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4890 | ssh2_pkt_addstring(ssh, s->username); |
4891 | ssh2_pkt_addstring(ssh, "ssh-connection");/* service requested */ |
4892 | ssh2_pkt_addstring(ssh, "none"); /* method */ |
4893 | ssh2_pkt_send(ssh); |
4894 | s->type = AUTH_TYPE_NONE; |
4895 | s->gotit = FALSE; |
4896 | s->we_are_in = FALSE; |
4897 | |
4898 | s->tried_pubkey_config = FALSE; |
4899 | s->tried_agent = FALSE; |
4900 | s->tried_keyb_inter = FALSE; |
4901 | s->kbd_inter_running = FALSE; |
86916870 |
4902 | /* Load the pub half of ssh->cfg.keyfile so we notice if it's in Pageant */ |
9a30e26b |
4903 | if (!filename_is_null(ssh->cfg.keyfile)) { |
231ee168 |
4904 | int keytype; |
6c6d9ed9 |
4905 | logeventf(ssh, "Reading private key file \"%.150s\"", |
4906 | filename_to_str(&ssh->cfg.keyfile)); |
9a30e26b |
4907 | keytype = key_type(&ssh->cfg.keyfile); |
51470298 |
4908 | if (keytype == SSH_KEYTYPE_SSH2) { |
4909 | s->publickey_blob = |
9a30e26b |
4910 | ssh2_userkey_loadpub(&ssh->cfg.keyfile, NULL, |
222d54dc |
4911 | &s->publickey_bloblen, NULL); |
51470298 |
4912 | } else { |
57356d63 |
4913 | char *msgbuf; |
c2ad7d0d |
4914 | logeventf(ssh, "Unable to use this key file (%s)", |
a8327734 |
4915 | key_type_to_str(keytype)); |
57356d63 |
4916 | msgbuf = dupprintf("Unable to use key file \"%.150s\"" |
6c6d9ed9 |
4917 | " (%s)\r\n", |
4918 | filename_to_str(&ssh->cfg.keyfile), |
57356d63 |
4919 | key_type_to_str(keytype)); |
51470298 |
4920 | c_write_str(ssh, msgbuf); |
57356d63 |
4921 | sfree(msgbuf); |
51470298 |
4922 | s->publickey_blob = NULL; |
231ee168 |
4923 | } |
396778f1 |
4924 | } else |
51470298 |
4925 | s->publickey_blob = NULL; |
65a22376 |
4926 | |
1408a877 |
4927 | while (1) { |
4928 | /* |
4929 | * Wait for the result of the last authentication request. |
4930 | */ |
51470298 |
4931 | if (!s->gotit) |
1408a877 |
4932 | crWaitUntilV(ispkt); |
51470298 |
4933 | while (ssh->pktin.type == SSH2_MSG_USERAUTH_BANNER) { |
32874aea |
4934 | char *banner; |
4935 | int size; |
4936 | /* |
4937 | * Don't show the banner if we're operating in |
4938 | * non-verbose non-interactive mode. (It's probably |
4939 | * a script, which means nobody will read the |
4940 | * banner _anyway_, and moreover the printing of |
4941 | * the banner will screw up processing on the |
4942 | * output of (say) plink.) |
4943 | */ |
4944 | if (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE)) { |
0016d70b |
4945 | ssh_pkt_getstring(ssh, &banner, &size); |
32874aea |
4946 | if (banner) |
51470298 |
4947 | c_write_untrusted(ssh, banner, size); |
32874aea |
4948 | } |
1408a877 |
4949 | crWaitUntilV(ispkt); |
4950 | } |
51470298 |
4951 | if (ssh->pktin.type == SSH2_MSG_USERAUTH_SUCCESS) { |
1408a877 |
4952 | logevent("Access granted"); |
51470298 |
4953 | s->we_are_in = TRUE; |
1408a877 |
4954 | break; |
4955 | } |
65a22376 |
4956 | |
51470298 |
4957 | if (s->kbd_inter_running && |
4958 | ssh->pktin.type == SSH2_MSG_USERAUTH_INFO_REQUEST) { |
b3186d64 |
4959 | /* |
45068b27 |
4960 | * This is either a further set-of-prompts packet |
4961 | * in keyboard-interactive authentication, or it's |
4962 | * the same one and we came back here with `gotit' |
4963 | * set. In the former case, we must reset the |
4964 | * curr_prompt variable. |
b3186d64 |
4965 | */ |
51470298 |
4966 | if (!s->gotit) |
4967 | s->curr_prompt = 0; |
4968 | } else if (ssh->pktin.type != SSH2_MSG_USERAUTH_FAILURE) { |
6b5cf8b4 |
4969 | bombout(("Strange packet received during authentication: type %d", |
51470298 |
4970 | ssh->pktin.type)); |
7ffdbc1a |
4971 | crStopV; |
65a22376 |
4972 | } |
4973 | |
51470298 |
4974 | s->gotit = FALSE; |
65a22376 |
4975 | |
1408a877 |
4976 | /* |
4977 | * OK, we're now sitting on a USERAUTH_FAILURE message, so |
4978 | * we can look at the string in it and know what we can |
4979 | * helpfully try next. |
4980 | */ |
51470298 |
4981 | if (ssh->pktin.type == SSH2_MSG_USERAUTH_FAILURE) { |
1408a877 |
4982 | char *methods; |
4983 | int methlen; |
0016d70b |
4984 | ssh_pkt_getstring(ssh, &methods, &methlen); |
51470298 |
4985 | s->kbd_inter_running = FALSE; |
4986 | if (!ssh2_pkt_getbool(ssh)) { |
1408a877 |
4987 | /* |
4988 | * We have received an unequivocal Access |
4989 | * Denied. This can translate to a variety of |
4990 | * messages: |
4991 | * |
4992 | * - if we'd just tried "none" authentication, |
4993 | * it's not worth printing anything at all |
4994 | * |
4995 | * - if we'd just tried a public key _offer_, |
4996 | * the message should be "Server refused our |
4997 | * key" (or no message at all if the key |
4998 | * came from Pageant) |
4999 | * |
5000 | * - if we'd just tried anything else, the |
5001 | * message really should be "Access denied". |
5002 | * |
5003 | * Additionally, if we'd just tried password |
5004 | * authentication, we should break out of this |
5005 | * whole loop so as to go back to the username |
5006 | * prompt. |
5007 | */ |
51470298 |
5008 | if (s->type == AUTH_TYPE_NONE) { |
1408a877 |
5009 | /* do nothing */ |
51470298 |
5010 | } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD || |
5011 | s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) { |
5012 | if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD) |
5013 | c_write_str(ssh, "Server refused our key\r\n"); |
1408a877 |
5014 | logevent("Server refused public key"); |
51470298 |
5015 | } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) { |
4bdf7c46 |
5016 | /* server declined keyboard-interactive; ignore */ |
1408a877 |
5017 | } else { |
51470298 |
5018 | c_write_str(ssh, "Access denied\r\n"); |
1408a877 |
5019 | logevent("Access denied"); |
51470298 |
5020 | if (s->type == AUTH_TYPE_PASSWORD) { |
5021 | s->we_are_in = FALSE; |
1408a877 |
5022 | break; |
5023 | } |
5024 | } |
5025 | } else { |
51470298 |
5026 | c_write_str(ssh, "Further authentication required\r\n"); |
1408a877 |
5027 | logevent("Further authentication required"); |
5028 | } |
65a22376 |
5029 | |
51470298 |
5030 | s->can_pubkey = |
32874aea |
5031 | in_commasep_string("publickey", methods, methlen); |
51470298 |
5032 | s->can_passwd = |
32874aea |
5033 | in_commasep_string("password", methods, methlen); |
86916870 |
5034 | s->can_keyb_inter = ssh->cfg.try_ki_auth && |
761187b6 |
5035 | in_commasep_string("keyboard-interactive", methods, methlen); |
1408a877 |
5036 | } |
65a22376 |
5037 | |
51470298 |
5038 | s->method = 0; |
5039 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
954a9540 |
5040 | s->need_pw = FALSE; |
65a22376 |
5041 | |
0405e71f |
5042 | /* |
5043 | * Most password/passphrase prompts will be |
5044 | * non-echoing, so we set this to 0 by default. |
5045 | * Exception is that some keyboard-interactive prompts |
5046 | * can be echoing, in which case we'll set this to 1. |
5047 | */ |
51470298 |
5048 | s->echo = 0; |
0405e71f |
5049 | |
51470298 |
5050 | if (!s->method && s->can_pubkey && |
5051 | agent_exists() && !s->tried_agent) { |
1983e559 |
5052 | /* |
5053 | * Attempt public-key authentication using Pageant. |
5054 | */ |
1983e559 |
5055 | void *r; |
51470298 |
5056 | s->authed = FALSE; |
1983e559 |
5057 | |
51470298 |
5058 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
5059 | ssh->pkt_ctx |= SSH2_PKTCTX_PUBLICKEY; |
00db133f |
5060 | |
51470298 |
5061 | s->tried_agent = TRUE; |
1983e559 |
5062 | |
5063 | logevent("Pageant is running. Requesting keys."); |
5064 | |
5065 | /* Request the keys held by the agent. */ |
51470298 |
5066 | PUT_32BIT(s->request, 1); |
5067 | s->request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
839f10db |
5068 | if (!agent_query(s->request, 5, &r, &s->responselen, |
5069 | ssh_agent_callback, ssh)) { |
5070 | do { |
5071 | crReturnV; |
5072 | if (ispkt) { |
5073 | bombout(("Unexpected data from server while" |
5074 | " waiting for agent response")); |
5075 | crStopV; |
5076 | } |
5077 | } while (ispkt || inlen > 0); |
5078 | r = ssh->agent_response; |
5079 | s->responselen = ssh->agent_response_len; |
5080 | } |
51470298 |
5081 | s->response = (unsigned char *) r; |
5082 | if (s->response && s->responselen >= 5 && |
5083 | s->response[4] == SSH2_AGENT_IDENTITIES_ANSWER) { |
5084 | s->p = s->response + 5; |
5085 | s->nkeys = GET_32BIT(s->p); |
5086 | s->p += 4; |
32874aea |
5087 | { |
5088 | char buf[64]; |
51470298 |
5089 | sprintf(buf, "Pageant has %d SSH2 keys", s->nkeys); |
32874aea |
5090 | logevent(buf); |
5091 | } |
51470298 |
5092 | for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) { |
2d466ffd |
5093 | void *vret; |
1983e559 |
5094 | |
32874aea |
5095 | { |
5096 | char buf[64]; |
51470298 |
5097 | sprintf(buf, "Trying Pageant key #%d", s->keyi); |
32874aea |
5098 | logevent(buf); |
5099 | } |
51470298 |
5100 | s->pklen = GET_32BIT(s->p); |
5101 | s->p += 4; |
5102 | if (s->publickey_blob && |
5103 | s->pklen == s->publickey_bloblen && |
5104 | !memcmp(s->p, s->publickey_blob, |
5105 | s->publickey_bloblen)) { |
396778f1 |
5106 | logevent("This key matches configured key file"); |
51470298 |
5107 | s->tried_pubkey_config = 1; |
396778f1 |
5108 | } |
d8baa528 |
5109 | s->pkblob = (char *)s->p; |
51470298 |
5110 | s->p += s->pklen; |
5111 | s->alglen = GET_32BIT(s->pkblob); |
5112 | s->alg = s->pkblob + 4; |
5113 | s->commentlen = GET_32BIT(s->p); |
5114 | s->p += 4; |
d8baa528 |
5115 | s->commentp = (char *)s->p; |
51470298 |
5116 | s->p += s->commentlen; |
5117 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
5118 | ssh2_pkt_addstring(ssh, s->username); |
5119 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
5120 | ssh2_pkt_addstring(ssh, "publickey"); /* method */ |
5121 | ssh2_pkt_addbool(ssh, FALSE); /* no signature included */ |
5122 | ssh2_pkt_addstring_start(ssh); |
5123 | ssh2_pkt_addstring_data(ssh, s->alg, s->alglen); |
5124 | ssh2_pkt_addstring_start(ssh); |
5125 | ssh2_pkt_addstring_data(ssh, s->pkblob, s->pklen); |
5126 | ssh2_pkt_send(ssh); |
1983e559 |
5127 | |
5128 | crWaitUntilV(ispkt); |
51470298 |
5129 | if (ssh->pktin.type != SSH2_MSG_USERAUTH_PK_OK) { |
1983e559 |
5130 | logevent("Key refused"); |
5131 | continue; |
5132 | } |
5133 | |
32874aea |
5134 | if (flags & FLAG_VERBOSE) { |
51470298 |
5135 | c_write_str(ssh, "Authenticating with " |
5136 | "public key \""); |
5137 | c_write(ssh, s->commentp, s->commentlen); |
5138 | c_write_str(ssh, "\" from agent\r\n"); |
32874aea |
5139 | } |
1983e559 |
5140 | |
5141 | /* |
5142 | * Server is willing to accept the key. |
5143 | * Construct a SIGN_REQUEST. |
5144 | */ |
51470298 |
5145 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
5146 | ssh2_pkt_addstring(ssh, s->username); |
5147 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
5148 | ssh2_pkt_addstring(ssh, "publickey"); /* method */ |
5149 | ssh2_pkt_addbool(ssh, TRUE); |
5150 | ssh2_pkt_addstring_start(ssh); |
5151 | ssh2_pkt_addstring_data(ssh, s->alg, s->alglen); |
5152 | ssh2_pkt_addstring_start(ssh); |
5153 | ssh2_pkt_addstring_data(ssh, s->pkblob, s->pklen); |
5154 | |
5155 | s->siglen = ssh->pktout.length - 5 + 4 + 20; |
dda87a28 |
5156 | if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID) |
5157 | s->siglen -= 4; |
51470298 |
5158 | s->len = 1; /* message type */ |
5159 | s->len += 4 + s->pklen; /* key blob */ |
5160 | s->len += 4 + s->siglen; /* data to sign */ |
5161 | s->len += 4; /* flags */ |
3d88e64d |
5162 | s->agentreq = snewn(4 + s->len, char); |
51470298 |
5163 | PUT_32BIT(s->agentreq, s->len); |
5164 | s->q = s->agentreq + 4; |
5165 | *s->q++ = SSH2_AGENTC_SIGN_REQUEST; |
5166 | PUT_32BIT(s->q, s->pklen); |
5167 | s->q += 4; |
5168 | memcpy(s->q, s->pkblob, s->pklen); |
5169 | s->q += s->pklen; |
5170 | PUT_32BIT(s->q, s->siglen); |
5171 | s->q += 4; |
1983e559 |
5172 | /* Now the data to be signed... */ |
dda87a28 |
5173 | if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) { |
5174 | PUT_32BIT(s->q, 20); |
5175 | s->q += 4; |
5176 | } |
51470298 |
5177 | memcpy(s->q, ssh->v2_session_id, 20); |
5178 | s->q += 20; |
5179 | memcpy(s->q, ssh->pktout.data + 5, |
5180 | ssh->pktout.length - 5); |
5181 | s->q += ssh->pktout.length - 5; |
1983e559 |
5182 | /* And finally the (zero) flags word. */ |
51470298 |
5183 | PUT_32BIT(s->q, 0); |
839f10db |
5184 | if (!agent_query(s->agentreq, s->len + 4, |
5185 | &vret, &s->retlen, |
5186 | ssh_agent_callback, ssh)) { |
5187 | do { |
5188 | crReturnV; |
5189 | if (ispkt) { |
5190 | bombout(("Unexpected data from server" |
5191 | " while waiting for agent" |
5192 | " response")); |
5193 | crStopV; |
5194 | } |
5195 | } while (ispkt || inlen > 0); |
5196 | vret = ssh->agent_response; |
5197 | s->retlen = ssh->agent_response_len; |
5198 | } |
51470298 |
5199 | s->ret = vret; |
5200 | sfree(s->agentreq); |
5201 | if (s->ret) { |
5202 | if (s->ret[4] == SSH2_AGENT_SIGN_RESPONSE) { |
1983e559 |
5203 | logevent("Sending Pageant's response"); |
51470298 |
5204 | ssh2_add_sigblob(ssh, s->pkblob, s->pklen, |
5205 | s->ret + 9, |
5206 | GET_32BIT(s->ret + 5)); |
5207 | ssh2_pkt_send(ssh); |
5208 | s->authed = TRUE; |
1983e559 |
5209 | break; |
5210 | } else { |
32874aea |
5211 | logevent |
5212 | ("Pageant failed to answer challenge"); |
51470298 |
5213 | sfree(s->ret); |
1983e559 |
5214 | } |
5215 | } |
5216 | } |
51470298 |
5217 | if (s->authed) |
1983e559 |
5218 | continue; |
5219 | } |
29b1d0b3 |
5220 | sfree(s->response); |
1983e559 |
5221 | } |
5222 | |
51470298 |
5223 | if (!s->method && s->can_pubkey && s->publickey_blob |
5224 | && !s->tried_pubkey_config) { |
1408a877 |
5225 | unsigned char *pub_blob; |
5226 | char *algorithm, *comment; |
5227 | int pub_blob_len; |
65a22376 |
5228 | |
51470298 |
5229 | s->tried_pubkey_config = TRUE; |
65a22376 |
5230 | |
51470298 |
5231 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
5232 | ssh->pkt_ctx |= SSH2_PKTCTX_PUBLICKEY; |
00db133f |
5233 | |
65a22376 |
5234 | /* |
1408a877 |
5235 | * Try the public key supplied in the configuration. |
5236 | * |
5237 | * First, offer the public blob to see if the server is |
5238 | * willing to accept it. |
65a22376 |
5239 | */ |
d8baa528 |
5240 | pub_blob = |
9a30e26b |
5241 | (unsigned char *)ssh2_userkey_loadpub(&ssh->cfg.keyfile, |
d8baa528 |
5242 | &algorithm, |
222d54dc |
5243 | &pub_blob_len, |
5244 | NULL); |
1408a877 |
5245 | if (pub_blob) { |
51470298 |
5246 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
5247 | ssh2_pkt_addstring(ssh, s->username); |
5248 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
5249 | ssh2_pkt_addstring(ssh, "publickey"); /* method */ |
5250 | ssh2_pkt_addbool(ssh, FALSE); /* no signature included */ |
5251 | ssh2_pkt_addstring(ssh, algorithm); |
5252 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
5253 | ssh2_pkt_addstring_data(ssh, (char *)pub_blob, |
5254 | pub_blob_len); |
51470298 |
5255 | ssh2_pkt_send(ssh); |
32874aea |
5256 | logevent("Offered public key"); /* FIXME */ |
1408a877 |
5257 | |
5258 | crWaitUntilV(ispkt); |
51470298 |
5259 | if (ssh->pktin.type != SSH2_MSG_USERAUTH_PK_OK) { |
5260 | s->gotit = TRUE; |
5261 | s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD; |
32874aea |
5262 | continue; /* key refused; give up on it */ |
1408a877 |
5263 | } |
65a22376 |
5264 | |
1408a877 |
5265 | logevent("Offer of public key accepted"); |
65a22376 |
5266 | /* |
1408a877 |
5267 | * Actually attempt a serious authentication using |
5268 | * the key. |
65a22376 |
5269 | */ |
9a30e26b |
5270 | if (ssh2_userkey_encrypted(&ssh->cfg.keyfile, &comment)) { |
51470298 |
5271 | sprintf(s->pwprompt, |
32874aea |
5272 | "Passphrase for key \"%.100s\": ", |
5273 | comment); |
51470298 |
5274 | s->need_pw = TRUE; |
1408a877 |
5275 | } else { |
51470298 |
5276 | s->need_pw = FALSE; |
1408a877 |
5277 | } |
4e520718 |
5278 | if (flags & FLAG_VERBOSE) { |
5279 | c_write_str(ssh, "Authenticating with public key \""); |
5280 | c_write_str(ssh, comment); |
5281 | c_write_str(ssh, "\"\r\n"); |
5282 | } |
51470298 |
5283 | s->method = AUTH_PUBLICKEY_FILE; |
65a22376 |
5284 | } |
1408a877 |
5285 | } |
5286 | |
51470298 |
5287 | if (!s->method && s->can_keyb_inter && !s->tried_keyb_inter) { |
5288 | s->method = AUTH_KEYBOARD_INTERACTIVE; |
5289 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
5290 | s->tried_keyb_inter = TRUE; |
af659722 |
5291 | |
51470298 |
5292 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
5293 | ssh->pkt_ctx |= SSH2_PKTCTX_KBDINTER; |
00db133f |
5294 | |
51470298 |
5295 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
5296 | ssh2_pkt_addstring(ssh, s->username); |
5297 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
5298 | ssh2_pkt_addstring(ssh, "keyboard-interactive"); /* method */ |
5299 | ssh2_pkt_addstring(ssh, ""); /* lang */ |
5300 | ssh2_pkt_addstring(ssh, ""); |
5301 | ssh2_pkt_send(ssh); |
af659722 |
5302 | |
5303 | crWaitUntilV(ispkt); |
51470298 |
5304 | if (ssh->pktin.type != SSH2_MSG_USERAUTH_INFO_REQUEST) { |
5305 | if (ssh->pktin.type == SSH2_MSG_USERAUTH_FAILURE) |
5306 | s->gotit = TRUE; |
af659722 |
5307 | logevent("Keyboard-interactive authentication refused"); |
51470298 |
5308 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET; |
af659722 |
5309 | continue; |
5310 | } |
5311 | |
51470298 |
5312 | s->kbd_inter_running = TRUE; |
5313 | s->curr_prompt = 0; |
af659722 |
5314 | } |
5315 | |
51470298 |
5316 | if (s->kbd_inter_running) { |
5317 | s->method = AUTH_KEYBOARD_INTERACTIVE; |
5318 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
5319 | s->tried_keyb_inter = TRUE; |
af659722 |
5320 | |
51470298 |
5321 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
5322 | ssh->pkt_ctx |= SSH2_PKTCTX_KBDINTER; |
00db133f |
5323 | |
51470298 |
5324 | if (s->curr_prompt == 0) { |
45068b27 |
5325 | /* |
5326 | * We've got a fresh USERAUTH_INFO_REQUEST. |
5327 | * Display header data, and start going through |
5328 | * the prompts. |
5329 | */ |
5330 | char *name, *inst, *lang; |
5331 | int name_len, inst_len, lang_len; |
5332 | |
0016d70b |
5333 | ssh_pkt_getstring(ssh, &name, &name_len); |
5334 | ssh_pkt_getstring(ssh, &inst, &inst_len); |
5335 | ssh_pkt_getstring(ssh, &lang, &lang_len); |
45068b27 |
5336 | if (name_len > 0) { |
51470298 |
5337 | c_write_untrusted(ssh, name, name_len); |
5338 | c_write_str(ssh, "\r\n"); |
45068b27 |
5339 | } |
5340 | if (inst_len > 0) { |
51470298 |
5341 | c_write_untrusted(ssh, inst, inst_len); |
5342 | c_write_str(ssh, "\r\n"); |
45068b27 |
5343 | } |
0016d70b |
5344 | s->num_prompts = ssh_pkt_getuint32(ssh); |
45068b27 |
5345 | } |
af659722 |
5346 | |
45068b27 |
5347 | /* |
5348 | * If there are prompts remaining in the packet, |
5349 | * display one and get a response. |
5350 | */ |
51470298 |
5351 | if (s->curr_prompt < s->num_prompts) { |
45068b27 |
5352 | char *prompt; |
5353 | int prompt_len; |
af659722 |
5354 | |
0016d70b |
5355 | ssh_pkt_getstring(ssh, &prompt, &prompt_len); |
45068b27 |
5356 | if (prompt_len > 0) { |
85fdbe25 |
5357 | static const char trunc[] = "<prompt truncated>: "; |
5358 | static const int prlen = sizeof(s->pwprompt) - |
5359 | lenof(trunc); |
5360 | if (prompt_len > prlen) { |
5361 | memcpy(s->pwprompt, prompt, prlen); |
5362 | strcpy(s->pwprompt + prlen, trunc); |
5363 | } else { |
5364 | memcpy(s->pwprompt, prompt, prompt_len); |
5365 | s->pwprompt[prompt_len] = '\0'; |
5366 | } |
45068b27 |
5367 | } else { |
51470298 |
5368 | strcpy(s->pwprompt, |
45068b27 |
5369 | "<server failed to send prompt>: "); |
5370 | } |
51470298 |
5371 | s->echo = ssh2_pkt_getbool(ssh); |
5372 | s->need_pw = TRUE; |
45068b27 |
5373 | } else |
51470298 |
5374 | s->need_pw = FALSE; |
af659722 |
5375 | } |
5376 | |
51470298 |
5377 | if (!s->method && s->can_passwd) { |
5378 | s->method = AUTH_PASSWORD; |
5379 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
5380 | ssh->pkt_ctx |= SSH2_PKTCTX_PASSWORD; |
5381 | sprintf(s->pwprompt, "%.90s@%.90s's password: ", s->username, |
5382 | ssh->savedhost); |
5383 | s->need_pw = TRUE; |
1408a877 |
5384 | } |
5385 | |
51470298 |
5386 | if (s->need_pw) { |
fa17a66e |
5387 | if (ssh_get_line) { |
51470298 |
5388 | if (!ssh_get_line(s->pwprompt, s->password, |
5389 | sizeof(s->password), TRUE)) { |
1408a877 |
5390 | /* |
fa17a66e |
5391 | * get_line failed to get a password (for |
5392 | * example because one was supplied on the |
5393 | * command line which has already failed to |
5394 | * work). Terminate. |
1408a877 |
5395 | */ |
51470298 |
5396 | ssh2_pkt_init(ssh, SSH2_MSG_DISCONNECT); |
5397 | ssh2_pkt_adduint32(ssh,SSH2_DISCONNECT_BY_APPLICATION); |
5398 | ssh2_pkt_addstring(ssh, "No more passwords available" |
5399 | " to try"); |
5400 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
5401 | ssh2_pkt_send(ssh); |
247308b5 |
5402 | logevent("Unable to authenticate"); |
a8327734 |
5403 | connection_fatal(ssh->frontend, |
5404 | "Unable to authenticate"); |
3bb2f322 |
5405 | ssh_closing((Plug)ssh, NULL, 0, 0); |
7ffdbc1a |
5406 | crStopV; |
1408a877 |
5407 | } |
5408 | } else { |
51470298 |
5409 | int ret; /* need not be saved across crReturn */ |
5410 | c_write_untrusted(ssh, s->pwprompt, strlen(s->pwprompt)); |
5411 | ssh->send_ok = 1; |
1408a877 |
5412 | |
51470298 |
5413 | setup_userpass_input(ssh, s->password, |
5414 | sizeof(s->password), s->echo); |
0405e71f |
5415 | do { |
1408a877 |
5416 | crWaitUntilV(!ispkt); |
51470298 |
5417 | ret = process_userpass_input(ssh, in, inlen); |
0405e71f |
5418 | } while (ret == 0); |
5419 | if (ret < 0) |
5420 | cleanup_exit(0); |
51470298 |
5421 | c_write_str(ssh, "\r\n"); |
65a22376 |
5422 | } |
65a22376 |
5423 | } |
65a22376 |
5424 | |
51470298 |
5425 | if (s->method == AUTH_PUBLICKEY_FILE) { |
1408a877 |
5426 | /* |
5427 | * We have our passphrase. Now try the actual authentication. |
5428 | */ |
5429 | struct ssh2_userkey *key; |
222d54dc |
5430 | const char *error = NULL; |
65a22376 |
5431 | |
222d54dc |
5432 | key = ssh2_load_userkey(&ssh->cfg.keyfile, s->password, |
5433 | &error); |
1408a877 |
5434 | if (key == SSH2_WRONG_PASSPHRASE || key == NULL) { |
5435 | if (key == SSH2_WRONG_PASSPHRASE) { |
51470298 |
5436 | c_write_str(ssh, "Wrong passphrase\r\n"); |
5437 | s->tried_pubkey_config = FALSE; |
1408a877 |
5438 | } else { |
222d54dc |
5439 | c_write_str(ssh, "Unable to load private key ("); |
5440 | c_write_str(ssh, error); |
5441 | c_write_str(ssh, ")\r\n"); |
51470298 |
5442 | s->tried_pubkey_config = TRUE; |
1408a877 |
5443 | } |
5444 | /* Send a spurious AUTH_NONE to return to the top. */ |
51470298 |
5445 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
5446 | ssh2_pkt_addstring(ssh, s->username); |
5447 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
5448 | ssh2_pkt_addstring(ssh, "none"); /* method */ |
5449 | ssh2_pkt_send(ssh); |
5450 | s->type = AUTH_TYPE_NONE; |
1408a877 |
5451 | } else { |
1dd353b5 |
5452 | unsigned char *pkblob, *sigblob, *sigdata; |
5453 | int pkblob_len, sigblob_len, sigdata_len; |
dda87a28 |
5454 | int p; |
65a22376 |
5455 | |
1408a877 |
5456 | /* |
5457 | * We have loaded the private key and the server |
5458 | * has announced that it's willing to accept it. |
5459 | * Hallelujah. Generate a signature and send it. |
5460 | */ |
51470298 |
5461 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
5462 | ssh2_pkt_addstring(ssh, s->username); |
5463 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
5464 | ssh2_pkt_addstring(ssh, "publickey"); /* method */ |
5465 | ssh2_pkt_addbool(ssh, TRUE); |
5466 | ssh2_pkt_addstring(ssh, key->alg->name); |
1dd353b5 |
5467 | pkblob = key->alg->public_blob(key->data, &pkblob_len); |
51470298 |
5468 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
5469 | ssh2_pkt_addstring_data(ssh, (char *)pkblob, pkblob_len); |
1408a877 |
5470 | |
5471 | /* |
5472 | * The data to be signed is: |
5473 | * |
5474 | * string session-id |
5475 | * |
5476 | * followed by everything so far placed in the |
5477 | * outgoing packet. |
5478 | */ |
51470298 |
5479 | sigdata_len = ssh->pktout.length - 5 + 4 + 20; |
dda87a28 |
5480 | if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID) |
5481 | sigdata_len -= 4; |
92d60585 |
5482 | sigdata = snewn(sigdata_len, unsigned char); |
dda87a28 |
5483 | p = 0; |
5484 | if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) { |
5485 | PUT_32BIT(sigdata+p, 20); |
5486 | p += 4; |
5487 | } |
5488 | memcpy(sigdata+p, ssh->v2_session_id, 20); p += 20; |
5489 | memcpy(sigdata+p, ssh->pktout.data + 5, |
51470298 |
5490 | ssh->pktout.length - 5); |
dda87a28 |
5491 | p += ssh->pktout.length - 5; |
5492 | assert(p == sigdata_len); |
d8baa528 |
5493 | sigblob = key->alg->sign(key->data, (char *)sigdata, |
1dd353b5 |
5494 | sigdata_len, &sigblob_len); |
51470298 |
5495 | ssh2_add_sigblob(ssh, pkblob, pkblob_len, |
1dd353b5 |
5496 | sigblob, sigblob_len); |
5497 | sfree(pkblob); |
5498 | sfree(sigblob); |
1408a877 |
5499 | sfree(sigdata); |
5500 | |
51470298 |
5501 | ssh2_pkt_send(ssh); |
5502 | s->type = AUTH_TYPE_PUBLICKEY; |
75374b2f |
5503 | key->alg->freekey(key->data); |
1408a877 |
5504 | } |
51470298 |
5505 | } else if (s->method == AUTH_PASSWORD) { |
65a22376 |
5506 | /* |
1408a877 |
5507 | * We send the password packet lumped tightly together with |
5508 | * an SSH_MSG_IGNORE packet. The IGNORE packet contains a |
5509 | * string long enough to make the total length of the two |
5510 | * packets constant. This should ensure that a passive |
5511 | * listener doing traffic analyis can't work out the length |
5512 | * of the password. |
5513 | * |
5514 | * For this to work, we need an assumption about the |
5515 | * maximum length of the password packet. I think 256 is |
5516 | * pretty conservative. Anyone using a password longer than |
5517 | * that probably doesn't have much to worry about from |
5518 | * people who find out how long their password is! |
65a22376 |
5519 | */ |
51470298 |
5520 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
5521 | ssh2_pkt_addstring(ssh, s->username); |
5522 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
5523 | ssh2_pkt_addstring(ssh, "password"); |
5524 | ssh2_pkt_addbool(ssh, FALSE); |
9a10ecf4 |
5525 | dont_log_password(ssh, PKTLOG_BLANK); |
51470298 |
5526 | ssh2_pkt_addstring(ssh, s->password); |
1409c1b3 |
5527 | memset(s->password, 0, sizeof(s->password)); |
9a10ecf4 |
5528 | end_log_omission(ssh); |
51470298 |
5529 | ssh2_pkt_defer(ssh); |
65a22376 |
5530 | /* |
1408a877 |
5531 | * We'll include a string that's an exact multiple of the |
5532 | * cipher block size. If the cipher is NULL for some |
5533 | * reason, we don't do this trick at all because we gain |
5534 | * nothing by it. |
65a22376 |
5535 | */ |
51470298 |
5536 | if (ssh->cscipher) { |
32874aea |
5537 | int stringlen, i; |
5538 | |
51470298 |
5539 | stringlen = (256 - ssh->deferred_len); |
5540 | stringlen += ssh->cscipher->blksize - 1; |
5541 | stringlen -= (stringlen % ssh->cscipher->blksize); |
5542 | if (ssh->cscomp) { |
32874aea |
5543 | /* |
5544 | * Temporarily disable actual compression, |
5545 | * so we can guarantee to get this string |
5546 | * exactly the length we want it. The |
5547 | * compression-disabling routine should |
5548 | * return an integer indicating how many |
5549 | * bytes we should adjust our string length |
5550 | * by. |
5551 | */ |
5366aed8 |
5552 | stringlen -= |
5553 | ssh->cscomp->disable_compression(ssh->cs_comp_ctx); |
32874aea |
5554 | } |
51470298 |
5555 | ssh2_pkt_init(ssh, SSH2_MSG_IGNORE); |
5556 | ssh2_pkt_addstring_start(ssh); |
6e9e9520 |
5557 | for (i = 0; i < stringlen; i++) { |
32874aea |
5558 | char c = (char) random_byte(); |
51470298 |
5559 | ssh2_pkt_addstring_data(ssh, &c, 1); |
65a22376 |
5560 | } |
51470298 |
5561 | ssh2_pkt_defer(ssh); |
65a22376 |
5562 | } |
51470298 |
5563 | ssh_pkt_defersend(ssh); |
0d43337a |
5564 | logevent("Sent password"); |
51470298 |
5565 | s->type = AUTH_TYPE_PASSWORD; |
5566 | } else if (s->method == AUTH_KEYBOARD_INTERACTIVE) { |
5567 | if (s->curr_prompt == 0) { |
5568 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE); |
5569 | ssh2_pkt_adduint32(ssh, s->num_prompts); |
45068b27 |
5570 | } |
51470298 |
5571 | if (s->need_pw) { /* only add pw if we just got one! */ |
9a10ecf4 |
5572 | dont_log_password(ssh, PKTLOG_BLANK); |
51470298 |
5573 | ssh2_pkt_addstring(ssh, s->password); |
5574 | memset(s->password, 0, sizeof(s->password)); |
9a10ecf4 |
5575 | end_log_omission(ssh); |
51470298 |
5576 | s->curr_prompt++; |
45068b27 |
5577 | } |
51470298 |
5578 | if (s->curr_prompt >= s->num_prompts) { |
5579 | ssh2_pkt_send(ssh); |
45068b27 |
5580 | } else { |
5581 | /* |
5582 | * If there are prompts remaining, we set |
5583 | * `gotit' so that we won't attempt to get |
5584 | * another packet. Then we go back round the |
5585 | * loop and will end up retrieving another |
5586 | * prompt out of the existing packet. Funky or |
5587 | * what? |
5588 | */ |
51470298 |
5589 | s->gotit = TRUE; |
45068b27 |
5590 | } |
51470298 |
5591 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
1408a877 |
5592 | } else { |
51470298 |
5593 | c_write_str(ssh, "No supported authentication methods" |
5594 | " left to try!\r\n"); |
5595 | logevent("No supported authentications offered." |
5596 | " Disconnecting"); |
5597 | ssh2_pkt_init(ssh, SSH2_MSG_DISCONNECT); |
5598 | ssh2_pkt_adduint32(ssh, SSH2_DISCONNECT_BY_APPLICATION); |
5599 | ssh2_pkt_addstring(ssh, "No supported authentication" |
5600 | " methods available"); |
5601 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
5602 | ssh2_pkt_send(ssh); |
3bb2f322 |
5603 | ssh_closing((Plug)ssh, NULL, 0, 0); |
7ffdbc1a |
5604 | crStopV; |
65a22376 |
5605 | } |
65a22376 |
5606 | } |
51470298 |
5607 | } while (!s->we_are_in); |
7cca0d81 |
5608 | |
5609 | /* |
5610 | * Now we're authenticated for the connection protocol. The |
5611 | * connection protocol will automatically have started at this |
5612 | * point; there's no need to send SERVICE_REQUEST. |
5613 | */ |
5614 | |
0ed48730 |
5615 | ssh->channels = newtree234(ssh_channelcmp); |
5616 | |
7cca0d81 |
5617 | /* |
0ed48730 |
5618 | * Create the main session channel. |
7cca0d81 |
5619 | */ |
0ed48730 |
5620 | if (!ssh->cfg.ssh_no_shell) { |
5621 | ssh->mainchan = snew(struct ssh_channel); |
5622 | ssh->mainchan->ssh = ssh; |
5623 | ssh->mainchan->localid = alloc_channel_id(ssh); |
5624 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN); |
5625 | ssh2_pkt_addstring(ssh, "session"); |
5626 | ssh2_pkt_adduint32(ssh, ssh->mainchan->localid); |
5627 | ssh->mainchan->v.v2.locwindow = OUR_V2_WINSIZE; |
5628 | ssh2_pkt_adduint32(ssh, ssh->mainchan->v.v2.locwindow);/* our window size */ |
5629 | ssh2_pkt_adduint32(ssh, 0x4000UL); /* our max pkt size */ |
5630 | ssh2_pkt_send(ssh); |
5631 | crWaitUntilV(ispkt); |
5632 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
5633 | bombout(("Server refused to open a session")); |
5634 | crStopV; |
5635 | /* FIXME: error data comes back in FAILURE packet */ |
5636 | } |
5637 | if (ssh_pkt_getuint32(ssh) != ssh->mainchan->localid) { |
5638 | bombout(("Server's channel confirmation cited wrong channel")); |
5639 | crStopV; |
5640 | } |
5641 | ssh->mainchan->remoteid = ssh_pkt_getuint32(ssh); |
5642 | ssh->mainchan->type = CHAN_MAINSESSION; |
5643 | ssh->mainchan->closes = 0; |
5644 | ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(ssh); |
5645 | ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(ssh); |
5646 | bufchain_init(&ssh->mainchan->v.v2.outbuffer); |
5647 | add234(ssh->channels, ssh->mainchan); |
5648 | logevent("Opened channel for session"); |
5649 | } else |
5650 | ssh->mainchan = NULL; |
7cca0d81 |
5651 | |
5652 | /* |
783415f8 |
5653 | * Potentially enable X11 forwarding. |
5654 | */ |
0ed48730 |
5655 | if (ssh->mainchan && ssh->cfg.x11_forward) { |
32874aea |
5656 | char proto[20], data[64]; |
5657 | logevent("Requesting X11 forwarding"); |
302121de |
5658 | ssh->x11auth = x11_invent_auth(proto, sizeof(proto), |
86916870 |
5659 | data, sizeof(data), ssh->cfg.x11_auth); |
5660 | x11_get_real_auth(ssh->x11auth, ssh->cfg.x11_display); |
51470298 |
5661 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
5662 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
5663 | ssh2_pkt_addstring(ssh, "x11-req"); |
5664 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5665 | ssh2_pkt_addbool(ssh, 0); /* many connections */ |
5666 | ssh2_pkt_addstring(ssh, proto); |
5667 | ssh2_pkt_addstring(ssh, data); |
86916870 |
5668 | ssh2_pkt_adduint32(ssh, x11_get_screen_number(ssh->cfg.x11_display)); |
51470298 |
5669 | ssh2_pkt_send(ssh); |
32874aea |
5670 | |
5671 | do { |
5672 | crWaitUntilV(ispkt); |
51470298 |
5673 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
0016d70b |
5674 | unsigned i = ssh_pkt_getuint32(ssh); |
32874aea |
5675 | struct ssh_channel *c; |
51470298 |
5676 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
5677 | if (!c) |
5678 | continue; /* nonexistent channel */ |
0016d70b |
5679 | c->v.v2.remwindow += ssh_pkt_getuint32(ssh); |
32874aea |
5680 | } |
51470298 |
5681 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
32874aea |
5682 | |
51470298 |
5683 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
5684 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
6b5cf8b4 |
5685 | bombout(("Unexpected response to X11 forwarding request:" |
51470298 |
5686 | " packet type %d", ssh->pktin.type)); |
7ffdbc1a |
5687 | crStopV; |
32874aea |
5688 | } |
5689 | logevent("X11 forwarding refused"); |
5690 | } else { |
5691 | logevent("X11 forwarding enabled"); |
51470298 |
5692 | ssh->X11_fwd_enabled = TRUE; |
32874aea |
5693 | } |
783415f8 |
5694 | } |
5695 | |
5696 | /* |
bc240b21 |
5697 | * Enable port forwardings. |
5698 | */ |
5699 | { |
bc240b21 |
5700 | char type; |
5701 | int n; |
a4fc0d74 |
5702 | int sport,dport,sserv,dserv; |
6ee9b735 |
5703 | char sports[256], dports[256], saddr[256], host[256]; |
bc240b21 |
5704 | |
51470298 |
5705 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh2); |
bc240b21 |
5706 | /* Add port forwardings. */ |
86916870 |
5707 | ssh->portfwd_strptr = ssh->cfg.portfwd; |
51470298 |
5708 | while (*ssh->portfwd_strptr) { |
5709 | type = *ssh->portfwd_strptr++; |
6ee9b735 |
5710 | saddr[0] = '\0'; |
bc240b21 |
5711 | n = 0; |
6ee9b735 |
5712 | while (*ssh->portfwd_strptr && *ssh->portfwd_strptr != '\t') { |
5713 | if (*ssh->portfwd_strptr == ':') { |
5714 | /* |
5715 | * We've seen a colon in the middle of the |
5716 | * source port number. This means that |
5717 | * everything we've seen until now is the |
5718 | * source _address_, so we'll move it into |
5719 | * saddr and start sports from the beginning |
5720 | * again. |
5721 | */ |
5722 | ssh->portfwd_strptr++; |
5723 | sports[n] = '\0'; |
5724 | strcpy(saddr, sports); |
5725 | n = 0; |
5726 | } |
5727 | if (n < 255) sports[n++] = *ssh->portfwd_strptr++; |
5728 | } |
bc240b21 |
5729 | sports[n] = 0; |
820ebe3b |
5730 | if (type != 'D') { |
5731 | if (*ssh->portfwd_strptr == '\t') |
5732 | ssh->portfwd_strptr++; |
5733 | n = 0; |
5734 | while (*ssh->portfwd_strptr && *ssh->portfwd_strptr != ':') { |
5735 | if (n < 255) host[n++] = *ssh->portfwd_strptr++; |
5736 | } |
5737 | host[n] = 0; |
5738 | if (*ssh->portfwd_strptr == ':') |
5739 | ssh->portfwd_strptr++; |
5740 | n = 0; |
5741 | while (*ssh->portfwd_strptr) { |
5742 | if (n < 255) dports[n++] = *ssh->portfwd_strptr++; |
5743 | } |
5744 | dports[n] = 0; |
51470298 |
5745 | ssh->portfwd_strptr++; |
820ebe3b |
5746 | dport = atoi(dports); |
5747 | dserv = 0; |
5748 | if (dport == 0) { |
5749 | dserv = 1; |
5750 | dport = net_service_lookup(dports); |
5751 | if (!dport) { |
5752 | logeventf(ssh, "Service lookup failed for destination" |
5753 | " port \"%s\"", dports); |
5754 | } |
a4fc0d74 |
5755 | } |
820ebe3b |
5756 | } else { |
5757 | while (*ssh->portfwd_strptr) ssh->portfwd_strptr++; |
7ffdbc1a |
5758 | dport = dserv = -1; |
7d5b0f4d |
5759 | ssh->portfwd_strptr++; /* eat the NUL and move to next one */ |
a4fc0d74 |
5760 | } |
bc240b21 |
5761 | sport = atoi(sports); |
a4fc0d74 |
5762 | sserv = 0; |
5763 | if (sport == 0) { |
5764 | sserv = 1; |
68a49acb |
5765 | sport = net_service_lookup(sports); |
5766 | if (!sport) { |
57356d63 |
5767 | logeventf(ssh, "Service lookup failed for source" |
5768 | " port \"%s\"", sports); |
a4fc0d74 |
5769 | } |
5770 | } |
bc240b21 |
5771 | if (sport && dport) { |
94ab1ddf |
5772 | /* Set up a description of the source port. */ |
5773 | char *sportdesc = dupprintf("%.*s%.*s%.*s%.*s%d%.*s", |
5774 | (int)(*saddr?strlen(saddr):0), *saddr?saddr:NULL, |
5775 | (int)(*saddr?1:0), ":", |
5776 | (int)(sserv ? strlen(sports) : 0), sports, |
5777 | sserv, "(", sport, sserv, ")"); |
bc240b21 |
5778 | if (type == 'L') { |
94ab1ddf |
5779 | /* Verbose description of the destination port */ |
5780 | char *dportdesc = dupprintf("%s:%.*s%.*s%d%.*s", |
5781 | host, |
5782 | (int)(dserv ? strlen(dports) : 0), dports, |
5783 | dserv, "(", dport, dserv, ")"); |
5784 | const char *err = pfd_addforward(host, dport, |
5785 | *saddr ? saddr : NULL, |
5786 | sport, ssh, &ssh->cfg); |
5787 | if (err) { |
5788 | logeventf(ssh, "Local port %s forward to %s" |
5789 | " failed: %s", sportdesc, dportdesc, err); |
5790 | } else { |
5791 | logeventf(ssh, "Local port %s forwarding to %s", |
5792 | sportdesc, dportdesc); |
5793 | } |
5794 | sfree(dportdesc); |
820ebe3b |
5795 | } else if (type == 'D') { |
94ab1ddf |
5796 | const char *err = pfd_addforward(NULL, -1, |
5797 | *saddr ? saddr : NULL, |
5798 | sport, ssh, &ssh->cfg); |
5799 | if (err) { |
5800 | logeventf(ssh, "Local port %s SOCKS dynamic forward" |
5801 | " setup failed: %s", sportdesc, err); |
5802 | } else { |
5803 | logeventf(ssh, "Local port %s doing SOCKS" |
5804 | " dynamic forwarding", sportdesc); |
5805 | } |
bc240b21 |
5806 | } else { |
5807 | struct ssh_rportfwd *pf; |
3d88e64d |
5808 | pf = snew(struct ssh_rportfwd); |
bc240b21 |
5809 | strcpy(pf->dhost, host); |
5810 | pf->dport = dport; |
5811 | pf->sport = sport; |
51470298 |
5812 | if (add234(ssh->rportfwds, pf) != pf) { |
57356d63 |
5813 | logeventf(ssh, "Duplicate remote port forwarding" |
5814 | " to %s:%d", host, dport); |
bc240b21 |
5815 | sfree(pf); |
5816 | } else { |
94ab1ddf |
5817 | logeventf(ssh, "Requesting remote port %s" |
57356d63 |
5818 | " forward to %s:%.*s%.*s%d%.*s", |
94ab1ddf |
5819 | sportdesc, |
57356d63 |
5820 | host, |
5821 | (int)(dserv ? strlen(dports) : 0), dports, |
5822 | dserv, "(", dport, dserv, ")"); |
51470298 |
5823 | ssh2_pkt_init(ssh, SSH2_MSG_GLOBAL_REQUEST); |
5824 | ssh2_pkt_addstring(ssh, "tcpip-forward"); |
5825 | ssh2_pkt_addbool(ssh, 1);/* want reply */ |
6ee9b735 |
5826 | if (*saddr) |
5827 | ssh2_pkt_addstring(ssh, saddr); |
86916870 |
5828 | if (ssh->cfg.rport_acceptall) |
51470298 |
5829 | ssh2_pkt_addstring(ssh, "0.0.0.0"); |
beefa433 |
5830 | else |
51470298 |
5831 | ssh2_pkt_addstring(ssh, "127.0.0.1"); |
5832 | ssh2_pkt_adduint32(ssh, sport); |
5833 | ssh2_pkt_send(ssh); |
bc240b21 |
5834 | |
5835 | do { |
5836 | crWaitUntilV(ispkt); |
51470298 |
5837 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
0016d70b |
5838 | unsigned i = ssh_pkt_getuint32(ssh); |
bc240b21 |
5839 | struct ssh_channel *c; |
51470298 |
5840 | c = find234(ssh->channels, &i, ssh_channelfind); |
bc240b21 |
5841 | if (!c) |
5842 | continue;/* nonexistent channel */ |
0016d70b |
5843 | c->v.v2.remwindow += ssh_pkt_getuint32(ssh); |
bc240b21 |
5844 | } |
51470298 |
5845 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
bc240b21 |
5846 | |
51470298 |
5847 | if (ssh->pktin.type != SSH2_MSG_REQUEST_SUCCESS) { |
5848 | if (ssh->pktin.type != SSH2_MSG_REQUEST_FAILURE) { |
6b5cf8b4 |
5849 | bombout(("Unexpected response to port " |
c9886e66 |
5850 | "forwarding request: packet type %d", |
51470298 |
5851 | ssh->pktin.type)); |
7ffdbc1a |
5852 | crStopV; |
bc240b21 |
5853 | } |
5854 | logevent("Server refused this port forwarding"); |
5855 | } else { |
5856 | logevent("Remote port forwarding enabled"); |
5857 | } |
5858 | } |
5859 | } |
94ab1ddf |
5860 | sfree(sportdesc); |
bc240b21 |
5861 | } |
5862 | } |
5863 | } |
5864 | |
5865 | /* |
36c2a3e9 |
5866 | * Potentially enable agent forwarding. |
5867 | */ |
0ed48730 |
5868 | if (ssh->mainchan && ssh->cfg.agentfwd && agent_exists()) { |
32874aea |
5869 | logevent("Requesting OpenSSH-style agent forwarding"); |
51470298 |
5870 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
5871 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
5872 | ssh2_pkt_addstring(ssh, "auth-agent-req@openssh.com"); |
5873 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5874 | ssh2_pkt_send(ssh); |
32874aea |
5875 | |
5876 | do { |
5877 | crWaitUntilV(ispkt); |
51470298 |
5878 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
0016d70b |
5879 | unsigned i = ssh_pkt_getuint32(ssh); |
32874aea |
5880 | struct ssh_channel *c; |
51470298 |
5881 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
5882 | if (!c) |
5883 | continue; /* nonexistent channel */ |
0016d70b |
5884 | c->v.v2.remwindow += ssh_pkt_getuint32(ssh); |
32874aea |
5885 | } |
51470298 |
5886 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
32874aea |
5887 | |
51470298 |
5888 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
5889 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
6b5cf8b4 |
5890 | bombout(("Unexpected response to agent forwarding request:" |
51470298 |
5891 | " packet type %d", ssh->pktin.type)); |
7ffdbc1a |
5892 | crStopV; |
32874aea |
5893 | } |
5894 | logevent("Agent forwarding refused"); |
5895 | } else { |
5896 | logevent("Agent forwarding enabled"); |
51470298 |
5897 | ssh->agentfwd_enabled = TRUE; |
32874aea |
5898 | } |
36c2a3e9 |
5899 | } |
5900 | |
5901 | /* |
7cca0d81 |
5902 | * Now allocate a pty for the session. |
5903 | */ |
0ed48730 |
5904 | if (ssh->mainchan && !ssh->cfg.nopty) { |
a5dd8467 |
5905 | /* Unpick the terminal-speed string. */ |
5906 | /* XXX perhaps we should allow no speeds to be sent. */ |
db219738 |
5907 | ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */ |
5908 | sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed); |
a5dd8467 |
5909 | /* Build the pty request. */ |
51470298 |
5910 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
5911 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); /* recipient channel */ |
5912 | ssh2_pkt_addstring(ssh, "pty-req"); |
5913 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
86916870 |
5914 | ssh2_pkt_addstring(ssh, ssh->cfg.termtype); |
51470298 |
5915 | ssh2_pkt_adduint32(ssh, ssh->term_width); |
5916 | ssh2_pkt_adduint32(ssh, ssh->term_height); |
5917 | ssh2_pkt_adduint32(ssh, 0); /* pixel width */ |
5918 | ssh2_pkt_adduint32(ssh, 0); /* pixel height */ |
5919 | ssh2_pkt_addstring_start(ssh); |
a5dd8467 |
5920 | ssh2_pkt_addbyte(ssh, 128); /* TTY_OP_ISPEED */ |
db219738 |
5921 | ssh2_pkt_adduint32(ssh, ssh->ispeed); |
a5dd8467 |
5922 | ssh2_pkt_addbyte(ssh, 129); /* TTY_OP_OSPEED */ |
db219738 |
5923 | ssh2_pkt_adduint32(ssh, ssh->ospeed); |
a5dd8467 |
5924 | ssh2_pkt_addstring_data(ssh, "\0", 1); /* TTY_OP_END */ |
51470298 |
5925 | ssh2_pkt_send(ssh); |
5926 | ssh->state = SSH_STATE_INTERMED; |
32874aea |
5927 | |
5928 | do { |
5929 | crWaitUntilV(ispkt); |
51470298 |
5930 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
0016d70b |
5931 | unsigned i = ssh_pkt_getuint32(ssh); |
32874aea |
5932 | struct ssh_channel *c; |
51470298 |
5933 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
5934 | if (!c) |
5935 | continue; /* nonexistent channel */ |
0016d70b |
5936 | c->v.v2.remwindow += ssh_pkt_getuint32(ssh); |
32874aea |
5937 | } |
51470298 |
5938 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
32874aea |
5939 | |
51470298 |
5940 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
5941 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
6b5cf8b4 |
5942 | bombout(("Unexpected response to pty request:" |
51470298 |
5943 | " packet type %d", ssh->pktin.type)); |
7ffdbc1a |
5944 | crStopV; |
32874aea |
5945 | } |
51470298 |
5946 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
5947 | ssh->editing = ssh->echoing = 1; |
32874aea |
5948 | } else { |
a5dd8467 |
5949 | logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)", |
db219738 |
5950 | ssh->ospeed, ssh->ispeed); |
32874aea |
5951 | } |
0965bee0 |
5952 | } else { |
51470298 |
5953 | ssh->editing = ssh->echoing = 1; |
7cca0d81 |
5954 | } |
5955 | |
5956 | /* |
fd5e5847 |
5957 | * Start a shell or a remote command. We may have to attempt |
5958 | * this twice if the config data has provided a second choice |
5959 | * of command. |
7cca0d81 |
5960 | */ |
0ed48730 |
5961 | if (ssh->mainchan) while (1) { |
fd5e5847 |
5962 | int subsys; |
5963 | char *cmd; |
5964 | |
51470298 |
5965 | if (ssh->fallback_cmd) { |
86916870 |
5966 | subsys = ssh->cfg.ssh_subsys2; |
5967 | cmd = ssh->cfg.remote_cmd_ptr2; |
fd5e5847 |
5968 | } else { |
86916870 |
5969 | subsys = ssh->cfg.ssh_subsys; |
5970 | cmd = ssh->cfg.remote_cmd_ptr; |
fd5e5847 |
5971 | } |
5972 | |
51470298 |
5973 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
5974 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); /* recipient channel */ |
fd5e5847 |
5975 | if (subsys) { |
51470298 |
5976 | ssh2_pkt_addstring(ssh, "subsystem"); |
5977 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5978 | ssh2_pkt_addstring(ssh, cmd); |
fd5e5847 |
5979 | } else if (*cmd) { |
51470298 |
5980 | ssh2_pkt_addstring(ssh, "exec"); |
5981 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5982 | ssh2_pkt_addstring(ssh, cmd); |
fd5e5847 |
5983 | } else { |
51470298 |
5984 | ssh2_pkt_addstring(ssh, "shell"); |
5985 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
32874aea |
5986 | } |
51470298 |
5987 | ssh2_pkt_send(ssh); |
fd5e5847 |
5988 | do { |
5989 | crWaitUntilV(ispkt); |
51470298 |
5990 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
0016d70b |
5991 | unsigned i = ssh_pkt_getuint32(ssh); |
fd5e5847 |
5992 | struct ssh_channel *c; |
51470298 |
5993 | c = find234(ssh->channels, &i, ssh_channelfind); |
fd5e5847 |
5994 | if (!c) |
5995 | continue; /* nonexistent channel */ |
0016d70b |
5996 | c->v.v2.remwindow += ssh_pkt_getuint32(ssh); |
fd5e5847 |
5997 | } |
51470298 |
5998 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
5999 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
6000 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
6b5cf8b4 |
6001 | bombout(("Unexpected response to shell/command request:" |
51470298 |
6002 | " packet type %d", ssh->pktin.type)); |
7ffdbc1a |
6003 | crStopV; |
fd5e5847 |
6004 | } |
6005 | /* |
6006 | * We failed to start the command. If this is the |
6007 | * fallback command, we really are finished; if it's |
6008 | * not, and if the fallback command exists, try falling |
6009 | * back to it before complaining. |
6010 | */ |
86916870 |
6011 | if (!ssh->fallback_cmd && ssh->cfg.remote_cmd_ptr2 != NULL) { |
fd5e5847 |
6012 | logevent("Primary command failed; attempting fallback"); |
51470298 |
6013 | ssh->fallback_cmd = TRUE; |
fd5e5847 |
6014 | continue; |
6015 | } |
6b5cf8b4 |
6016 | bombout(("Server refused to start a shell/command")); |
7ffdbc1a |
6017 | crStopV; |
fd5e5847 |
6018 | } else { |
6019 | logevent("Started a shell/command"); |
32874aea |
6020 | } |
fd5e5847 |
6021 | break; |
7cca0d81 |
6022 | } |
6023 | |
51470298 |
6024 | ssh->state = SSH_STATE_SESSION; |
6025 | if (ssh->size_needed) |
6026 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
6027 | if (ssh->eof_needed) |
6028 | ssh_special(ssh, TS_EOF); |
6e48c3fe |
6029 | |
7cca0d81 |
6030 | /* |
6031 | * Transfer data! |
6032 | */ |
b9d7bcad |
6033 | if (ssh->ldisc) |
6034 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
0ed48730 |
6035 | if (ssh->mainchan) |
6036 | ssh->send_ok = 1; |
7cca0d81 |
6037 | while (1) { |
e5574168 |
6038 | crReturnV; |
51470298 |
6039 | s->try_send = FALSE; |
7cca0d81 |
6040 | if (ispkt) { |
51470298 |
6041 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_DATA || |
6042 | ssh->pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
32874aea |
6043 | char *data; |
6044 | int length; |
0016d70b |
6045 | unsigned i = ssh_pkt_getuint32(ssh); |
32874aea |
6046 | struct ssh_channel *c; |
51470298 |
6047 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
6048 | if (!c) |
6049 | continue; /* nonexistent channel */ |
51470298 |
6050 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA && |
0016d70b |
6051 | ssh_pkt_getuint32(ssh) != SSH2_EXTENDED_DATA_STDERR) |
32874aea |
6052 | continue; /* extended but not stderr */ |
0016d70b |
6053 | ssh_pkt_getstring(ssh, &data, &length); |
32874aea |
6054 | if (data) { |
7ffdbc1a |
6055 | int bufsize = 0; |
5471d09a |
6056 | c->v.v2.locwindow -= length; |
32874aea |
6057 | switch (c->type) { |
6058 | case CHAN_MAINSESSION: |
5471d09a |
6059 | bufsize = |
51470298 |
6060 | from_backend(ssh->frontend, ssh->pktin.type == |
5471d09a |
6061 | SSH2_MSG_CHANNEL_EXTENDED_DATA, |
6062 | data, length); |
32874aea |
6063 | break; |
6064 | case CHAN_X11: |
5471d09a |
6065 | bufsize = x11_send(c->u.x11.s, data, length); |
32874aea |
6066 | break; |
d74d141c |
6067 | case CHAN_SOCKDATA: |
5471d09a |
6068 | bufsize = pfd_send(c->u.pfd.s, data, length); |
bc240b21 |
6069 | break; |
36c2a3e9 |
6070 | case CHAN_AGENT: |
32874aea |
6071 | while (length > 0) { |
6072 | if (c->u.a.lensofar < 4) { |
6073 | int l = min(4 - c->u.a.lensofar, length); |
6074 | memcpy(c->u.a.msglen + c->u.a.lensofar, |
6075 | data, l); |
6076 | data += l; |
6077 | length -= l; |
6078 | c->u.a.lensofar += l; |
6079 | } |
6080 | if (c->u.a.lensofar == 4) { |
6081 | c->u.a.totallen = |
6082 | 4 + GET_32BIT(c->u.a.msglen); |
3d88e64d |
6083 | c->u.a.message = snewn(c->u.a.totallen, |
6084 | unsigned char); |
32874aea |
6085 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
6086 | } |
6087 | if (c->u.a.lensofar >= 4 && length > 0) { |
6088 | int l = |
6089 | min(c->u.a.totallen - c->u.a.lensofar, |
6090 | length); |
6091 | memcpy(c->u.a.message + c->u.a.lensofar, |
6092 | data, l); |
6093 | data += l; |
6094 | length -= l; |
6095 | c->u.a.lensofar += l; |
6096 | } |
6097 | if (c->u.a.lensofar == c->u.a.totallen) { |
839f10db |
6098 | void *reply; |
32874aea |
6099 | int replylen; |
839f10db |
6100 | if (agent_query(c->u.a.message, |
6101 | c->u.a.totallen, |
6102 | &reply, &replylen, |
6103 | ssh_agentf_callback, c)) |
6104 | ssh_agentf_callback(c, reply, replylen); |
32874aea |
6105 | sfree(c->u.a.message); |
6106 | c->u.a.lensofar = 0; |
6107 | } |
6108 | } |
5471d09a |
6109 | bufsize = 0; |
32874aea |
6110 | break; |
6111 | } |
6112 | /* |
5471d09a |
6113 | * If we are not buffering too much data, |
6114 | * enlarge the window again at the remote side. |
32874aea |
6115 | */ |
5471d09a |
6116 | if (bufsize < OUR_V2_WINSIZE) |
6117 | ssh2_set_window(c, OUR_V2_WINSIZE - bufsize); |
32874aea |
6118 | } |
51470298 |
6119 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_EOF) { |
0016d70b |
6120 | unsigned i = ssh_pkt_getuint32(ssh); |
32874aea |
6121 | struct ssh_channel *c; |
6122 | |
51470298 |
6123 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
6124 | if (!c) |
6125 | continue; /* nonexistent channel */ |
6126 | |
6127 | if (c->type == CHAN_X11) { |
6128 | /* |
6129 | * Remote EOF on an X11 channel means we should |
6130 | * wrap up and close the channel ourselves. |
6131 | */ |
6132 | x11_close(c->u.x11.s); |
6133 | sshfwd_close(c); |
6134 | } else if (c->type == CHAN_AGENT) { |
36c2a3e9 |
6135 | sshfwd_close(c); |
d74d141c |
6136 | } else if (c->type == CHAN_SOCKDATA) { |
bc240b21 |
6137 | pfd_close(c->u.pfd.s); |
6138 | sshfwd_close(c); |
36c2a3e9 |
6139 | } |
51470298 |
6140 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_CLOSE) { |
0016d70b |
6141 | unsigned i = ssh_pkt_getuint32(ssh); |
32874aea |
6142 | struct ssh_channel *c; |
6143 | |
51470298 |
6144 | c = find234(ssh->channels, &i, ssh_channelfind); |
d0d844c1 |
6145 | if (!c || ((int)c->remoteid) == -1) { |
6b5cf8b4 |
6146 | bombout(("Received CHANNEL_CLOSE for %s channel %d\n", |
d0d844c1 |
6147 | c ? "half-open" : "nonexistent", i)); |
7ffdbc1a |
6148 | crStopV; |
d0d844c1 |
6149 | } |
32874aea |
6150 | /* Do pre-close processing on the channel. */ |
6151 | switch (c->type) { |
6152 | case CHAN_MAINSESSION: |
6153 | break; /* nothing to see here, move along */ |
6154 | case CHAN_X11: |
92f157bd |
6155 | if (c->u.x11.s != NULL) |
6156 | x11_close(c->u.x11.s); |
6157 | sshfwd_close(c); |
32874aea |
6158 | break; |
6159 | case CHAN_AGENT: |
92f157bd |
6160 | sshfwd_close(c); |
32874aea |
6161 | break; |
d74d141c |
6162 | case CHAN_SOCKDATA: |
92f157bd |
6163 | if (c->u.pfd.s != NULL) |
6164 | pfd_close(c->u.pfd.s); |
6165 | sshfwd_close(c); |
bc240b21 |
6166 | break; |
32874aea |
6167 | } |
92f157bd |
6168 | if (c->closes == 0) { |
51470298 |
6169 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_CLOSE); |
6170 | ssh2_pkt_adduint32(ssh, c->remoteid); |
6171 | ssh2_pkt_send(ssh); |
92f157bd |
6172 | } |
51470298 |
6173 | del234(ssh->channels, c); |
5471d09a |
6174 | bufchain_clear(&c->v.v2.outbuffer); |
32874aea |
6175 | sfree(c); |
6176 | |
6177 | /* |
6178 | * See if that was the last channel left open. |
0ed48730 |
6179 | * (This is only our termination condition if we're |
6180 | * not running in -N mode.) |
32874aea |
6181 | */ |
0ed48730 |
6182 | if (!ssh->cfg.ssh_no_shell && count234(ssh->channels) == 0) { |
3bb2f322 |
6183 | logevent("All channels closed. Disconnecting"); |
ca2914d1 |
6184 | #if 0 |
6185 | /* |
6186 | * We used to send SSH_MSG_DISCONNECT here, |
6187 | * because I'd believed that _every_ conforming |
6188 | * SSH2 connection had to end with a disconnect |
6189 | * being sent by at least one side; apparently |
6190 | * I was wrong and it's perfectly OK to |
6191 | * unceremoniously slam the connection shut |
6192 | * when you're done, and indeed OpenSSH feels |
6193 | * this is more polite than sending a |
6194 | * DISCONNECT. So now we don't. |
6195 | */ |
51470298 |
6196 | ssh2_pkt_init(ssh, SSH2_MSG_DISCONNECT); |
6197 | ssh2_pkt_adduint32(ssh, SSH2_DISCONNECT_BY_APPLICATION); |
6198 | ssh2_pkt_addstring(ssh, "All open channels closed"); |
6199 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
6200 | ssh2_pkt_send(ssh); |
ca2914d1 |
6201 | #endif |
3bb2f322 |
6202 | ssh_closing((Plug)ssh, NULL, 0, 0); |
7ffdbc1a |
6203 | crStopV; |
32874aea |
6204 | } |
6205 | continue; /* remote sends close; ignore (FIXME) */ |
51470298 |
6206 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
0016d70b |
6207 | unsigned i = ssh_pkt_getuint32(ssh); |
32874aea |
6208 | struct ssh_channel *c; |
51470298 |
6209 | c = find234(ssh->channels, &i, ssh_channelfind); |
a6253970 |
6210 | if (!c || c->closes) |
6211 | continue; /* nonexistent or closing channel */ |
0016d70b |
6212 | c->v.v2.remwindow += ssh_pkt_getuint32(ssh); |
51470298 |
6213 | s->try_send = TRUE; |
6214 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
0016d70b |
6215 | unsigned i = ssh_pkt_getuint32(ssh); |
bc240b21 |
6216 | struct ssh_channel *c; |
51470298 |
6217 | c = find234(ssh->channels, &i, ssh_channelfind); |
bc240b21 |
6218 | if (!c) |
6219 | continue; /* nonexistent channel */ |
6220 | if (c->type != CHAN_SOCKDATA_DORMANT) |
6221 | continue; /* dunno why they're confirming this */ |
0016d70b |
6222 | c->remoteid = ssh_pkt_getuint32(ssh); |
bc240b21 |
6223 | c->type = CHAN_SOCKDATA; |
0016d70b |
6224 | c->v.v2.remwindow = ssh_pkt_getuint32(ssh); |
6225 | c->v.v2.remmaxpkt = ssh_pkt_getuint32(ssh); |
4ed34d25 |
6226 | if (c->u.pfd.s) |
6227 | pfd_confirm(c->u.pfd.s); |
6228 | if (c->closes) { |
6229 | /* |
6230 | * We have a pending close on this channel, |
6231 | * which we decided on before the server acked |
6232 | * the channel open. So now we know the |
6233 | * remoteid, we can close it again. |
6234 | */ |
51470298 |
6235 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_CLOSE); |
6236 | ssh2_pkt_adduint32(ssh, c->remoteid); |
6237 | ssh2_pkt_send(ssh); |
4ed34d25 |
6238 | } |
51470298 |
6239 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_OPEN_FAILURE) { |
e595953d |
6240 | static const char *const reasons[] = { |
6241 | "<unknown reason code>", |
6242 | "Administratively prohibited", |
6243 | "Connect failed", |
6244 | "Unknown channel type", |
6245 | "Resource shortage", |
6246 | }; |
0016d70b |
6247 | unsigned i = ssh_pkt_getuint32(ssh); |
e595953d |
6248 | unsigned reason_code; |
6249 | char *reason_string; |
6250 | int reason_length; |
6251 | char *message; |
724cface |
6252 | struct ssh_channel *c; |
51470298 |
6253 | c = find234(ssh->channels, &i, ssh_channelfind); |
724cface |
6254 | if (!c) |
6255 | continue; /* nonexistent channel */ |
6256 | if (c->type != CHAN_SOCKDATA_DORMANT) |
6257 | continue; /* dunno why they're failing this */ |
6258 | |
e595953d |
6259 | reason_code = ssh_pkt_getuint32(ssh); |
6260 | if (reason_code >= lenof(reasons)) |
6261 | reason_code = 0; /* ensure reasons[reason_code] in range */ |
6262 | ssh_pkt_getstring(ssh, &reason_string, &reason_length); |
6263 | message = dupprintf("Forwarded connection refused by" |
6264 | " server: %s [%.*s]", reasons[reason_code], |
6265 | reason_length, reason_string); |
6266 | logevent(message); |
6267 | sfree(message); |
724cface |
6268 | |
6269 | pfd_close(c->u.pfd.s); |
6270 | |
51470298 |
6271 | del234(ssh->channels, c); |
724cface |
6272 | sfree(c); |
51470298 |
6273 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_REQUEST) { |
ebb0a590 |
6274 | unsigned localid; |
6275 | char *type; |
6276 | int typelen, want_reply; |
6277 | struct ssh_channel *c; |
6278 | |
0016d70b |
6279 | localid = ssh_pkt_getuint32(ssh); |
6280 | ssh_pkt_getstring(ssh, &type, &typelen); |
51470298 |
6281 | want_reply = ssh2_pkt_getbool(ssh); |
ebb0a590 |
6282 | |
6283 | /* |
6284 | * First, check that the channel exists. Otherwise, |
6285 | * we can instantly disconnect with a rude message. |
6286 | */ |
51470298 |
6287 | c = find234(ssh->channels, &localid, ssh_channelfind); |
ebb0a590 |
6288 | if (!c) { |
6289 | char buf[80]; |
6290 | sprintf(buf, "Received channel request for nonexistent" |
6291 | " channel %d", localid); |
6292 | logevent(buf); |
51470298 |
6293 | ssh2_pkt_init(ssh, SSH2_MSG_DISCONNECT); |
6294 | ssh2_pkt_adduint32(ssh, SSH2_DISCONNECT_BY_APPLICATION); |
6295 | ssh2_pkt_addstring(ssh, buf); |
6296 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
6297 | ssh2_pkt_send(ssh); |
6b5cf8b4 |
6298 | connection_fatal(ssh->frontend, "%s", buf); |
3bb2f322 |
6299 | ssh_closing((Plug)ssh, NULL, 0, 0); |
7ffdbc1a |
6300 | crStopV; |
ebb0a590 |
6301 | } |
6302 | |
6303 | /* |
d8d6c7e5 |
6304 | * Having got the channel number, we now look at |
6305 | * the request type string to see if it's something |
6306 | * we recognise. |
ebb0a590 |
6307 | */ |
d8d6c7e5 |
6308 | if (typelen == 11 && !memcmp(type, "exit-status", 11) && |
51470298 |
6309 | c == ssh->mainchan) { |
d8d6c7e5 |
6310 | /* We recognise "exit-status" on the primary channel. */ |
6311 | char buf[100]; |
0016d70b |
6312 | ssh->exitcode = ssh_pkt_getuint32(ssh); |
d8d6c7e5 |
6313 | sprintf(buf, "Server sent command exit status %d", |
51470298 |
6314 | ssh->exitcode); |
d8d6c7e5 |
6315 | logevent(buf); |
6316 | if (want_reply) { |
51470298 |
6317 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_SUCCESS); |
6318 | ssh2_pkt_adduint32(ssh, c->remoteid); |
6319 | ssh2_pkt_send(ssh); |
d8d6c7e5 |
6320 | } |
6321 | } else { |
6322 | /* |
6323 | * This is a channel request we don't know |
6324 | * about, so we now either ignore the request |
6325 | * or respond with CHANNEL_FAILURE, depending |
6326 | * on want_reply. |
6327 | */ |
6328 | if (want_reply) { |
51470298 |
6329 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_FAILURE); |
6330 | ssh2_pkt_adduint32(ssh, c->remoteid); |
6331 | ssh2_pkt_send(ssh); |
d8d6c7e5 |
6332 | } |
ebb0a590 |
6333 | } |
51470298 |
6334 | } else if (ssh->pktin.type == SSH2_MSG_GLOBAL_REQUEST) { |
697d4856 |
6335 | char *type; |
6336 | int typelen, want_reply; |
6337 | |
0016d70b |
6338 | ssh_pkt_getstring(ssh, &type, &typelen); |
51470298 |
6339 | want_reply = ssh2_pkt_getbool(ssh); |
697d4856 |
6340 | |
6341 | /* |
6342 | * We currently don't support any global requests |
6343 | * at all, so we either ignore the request or |
6344 | * respond with REQUEST_FAILURE, depending on |
6345 | * want_reply. |
6346 | */ |
6347 | if (want_reply) { |
51470298 |
6348 | ssh2_pkt_init(ssh, SSH2_MSG_REQUEST_FAILURE); |
6349 | ssh2_pkt_send(ssh); |
697d4856 |
6350 | } |
51470298 |
6351 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_OPEN) { |
32874aea |
6352 | char *type; |
6353 | int typelen; |
b3ebaa28 |
6354 | char *peeraddr; |
6355 | int peeraddrlen; |
2e5b14c0 |
6356 | int peerport; |
32874aea |
6357 | char *error = NULL; |
6358 | struct ssh_channel *c; |
bc240b21 |
6359 | unsigned remid, winsize, pktsize; |
0016d70b |
6360 | ssh_pkt_getstring(ssh, &type, &typelen); |
3d88e64d |
6361 | c = snew(struct ssh_channel); |
51470298 |
6362 | c->ssh = ssh; |
32874aea |
6363 | |
0016d70b |
6364 | remid = ssh_pkt_getuint32(ssh); |
6365 | winsize = ssh_pkt_getuint32(ssh); |
6366 | pktsize = ssh_pkt_getuint32(ssh); |
bc240b21 |
6367 | |
32874aea |
6368 | if (typelen == 3 && !memcmp(type, "x11", 3)) { |
df85f78b |
6369 | char *addrstr; |
6370 | |
0016d70b |
6371 | ssh_pkt_getstring(ssh, &peeraddr, &peeraddrlen); |
df85f78b |
6372 | addrstr = snewn(peeraddrlen+1, char); |
b3ebaa28 |
6373 | memcpy(addrstr, peeraddr, peeraddrlen); |
5a9bf764 |
6374 | addrstr[peeraddrlen] = '\0'; |
0016d70b |
6375 | peerport = ssh_pkt_getuint32(ssh); |
b3ebaa28 |
6376 | |
5a9bf764 |
6377 | logeventf(ssh, "Received X11 connect request from %s:%d", |
6378 | addrstr, peerport); |
6379 | |
51470298 |
6380 | if (!ssh->X11_fwd_enabled) |
32874aea |
6381 | error = "X11 forwarding is not enabled"; |
86916870 |
6382 | else if (x11_init(&c->u.x11.s, ssh->cfg.x11_display, c, |
2e5b14c0 |
6383 | ssh->x11auth, addrstr, peerport, |
e8fa8f62 |
6384 | &ssh->cfg) != NULL) { |
32874aea |
6385 | error = "Unable to open an X11 connection"; |
6386 | } else { |
5a9bf764 |
6387 | logevent("Opening X11 forward connection succeeded"); |
32874aea |
6388 | c->type = CHAN_X11; |
6389 | } |
b3ebaa28 |
6390 | |
6391 | sfree(addrstr); |
bc240b21 |
6392 | } else if (typelen == 15 && |
6393 | !memcmp(type, "forwarded-tcpip", 15)) { |
6394 | struct ssh_rportfwd pf, *realpf; |
6395 | char *dummy; |
6396 | int dummylen; |
0016d70b |
6397 | ssh_pkt_getstring(ssh, &dummy, &dummylen);/* skip address */ |
6398 | pf.sport = ssh_pkt_getuint32(ssh); |
6399 | ssh_pkt_getstring(ssh, &peeraddr, &peeraddrlen); |
6400 | peerport = ssh_pkt_getuint32(ssh); |
51470298 |
6401 | realpf = find234(ssh->rportfwds, &pf, NULL); |
5a9bf764 |
6402 | logeventf(ssh, "Received remote port %d open request " |
6403 | "from %s:%d", pf.sport, peeraddr, peerport); |
bc240b21 |
6404 | if (realpf == NULL) { |
6405 | error = "Remote port is not recognised"; |
6406 | } else { |
cbe2d68f |
6407 | const char *e = pfd_newconnect(&c->u.pfd.s, |
6408 | realpf->dhost, |
6409 | realpf->dport, c, |
6410 | &ssh->cfg); |
5a9bf764 |
6411 | logeventf(ssh, "Attempting to forward remote port to " |
6412 | "%s:%d", realpf->dhost, realpf->dport); |
bc240b21 |
6413 | if (e != NULL) { |
57356d63 |
6414 | logeventf(ssh, "Port open failed: %s", e); |
bc240b21 |
6415 | error = "Port open failed"; |
6416 | } else { |
6417 | logevent("Forwarded port opened successfully"); |
6418 | c->type = CHAN_SOCKDATA; |
6419 | } |
6420 | } |
32874aea |
6421 | } else if (typelen == 22 && |
36c2a3e9 |
6422 | !memcmp(type, "auth-agent@openssh.com", 3)) { |
51470298 |
6423 | if (!ssh->agentfwd_enabled) |
32874aea |
6424 | error = "Agent forwarding is not enabled"; |
36c2a3e9 |
6425 | else { |
32874aea |
6426 | c->type = CHAN_AGENT; /* identify channel type */ |
36c2a3e9 |
6427 | c->u.a.lensofar = 0; |
32874aea |
6428 | } |
6429 | } else { |
6430 | error = "Unsupported channel type requested"; |
6431 | } |
6432 | |
bc240b21 |
6433 | c->remoteid = remid; |
32874aea |
6434 | if (error) { |
51470298 |
6435 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE); |
6436 | ssh2_pkt_adduint32(ssh, c->remoteid); |
6437 | ssh2_pkt_adduint32(ssh, SSH2_OPEN_CONNECT_FAILED); |
6438 | ssh2_pkt_addstring(ssh, error); |
6439 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
6440 | ssh2_pkt_send(ssh); |
5a9bf764 |
6441 | logeventf(ssh, "Rejected channel open: %s", error); |
32874aea |
6442 | sfree(c); |
6443 | } else { |
51470298 |
6444 | c->localid = alloc_channel_id(ssh); |
32874aea |
6445 | c->closes = 0; |
5471d09a |
6446 | c->v.v2.locwindow = OUR_V2_WINSIZE; |
6447 | c->v.v2.remwindow = winsize; |
6448 | c->v.v2.remmaxpkt = pktsize; |
6449 | bufchain_init(&c->v.v2.outbuffer); |
51470298 |
6450 | add234(ssh->channels, c); |
6451 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
6452 | ssh2_pkt_adduint32(ssh, c->remoteid); |
6453 | ssh2_pkt_adduint32(ssh, c->localid); |
6454 | ssh2_pkt_adduint32(ssh, c->v.v2.locwindow); |
6455 | ssh2_pkt_adduint32(ssh, 0x4000UL); /* our max pkt size */ |
6456 | ssh2_pkt_send(ssh); |
32874aea |
6457 | } |
7cca0d81 |
6458 | } else { |
6b5cf8b4 |
6459 | bombout(("Strange packet received: type %d", ssh->pktin.type)); |
7ffdbc1a |
6460 | crStopV; |
7cca0d81 |
6461 | } |
0ed48730 |
6462 | } else if (ssh->mainchan) { |
32874aea |
6463 | /* |
6464 | * We have spare data. Add it to the channel buffer. |
6465 | */ |
d8baa528 |
6466 | ssh2_add_channel_data(ssh->mainchan, (char *)in, inlen); |
51470298 |
6467 | s->try_send = TRUE; |
32874aea |
6468 | } |
51470298 |
6469 | if (s->try_send) { |
32874aea |
6470 | int i; |
6471 | struct ssh_channel *c; |
6472 | /* |
6473 | * Try to send data on all channels if we can. |
6474 | */ |
51470298 |
6475 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
a6253970 |
6476 | int bufsize; |
6477 | if (c->closes) |
6478 | continue; /* don't send on closing channels */ |
6479 | bufsize = ssh2_try_send(c); |
5471d09a |
6480 | if (bufsize == 0) { |
6481 | switch (c->type) { |
6482 | case CHAN_MAINSESSION: |
6483 | /* stdin need not receive an unthrottle |
6484 | * notification since it will be polled */ |
6485 | break; |
6486 | case CHAN_X11: |
6487 | x11_unthrottle(c->u.x11.s); |
6488 | break; |
6489 | case CHAN_AGENT: |
6490 | /* agent sockets are request/response and need no |
6491 | * buffer management */ |
6492 | break; |
6493 | case CHAN_SOCKDATA: |
6494 | pfd_unthrottle(c->u.pfd.s); |
6495 | break; |
6496 | } |
6497 | } |
6498 | } |
7cca0d81 |
6499 | } |
e5574168 |
6500 | } |
6501 | |
6502 | crFinishV; |
6503 | } |
6504 | |
6505 | /* |
7cca0d81 |
6506 | * Handle the top-level SSH2 protocol. |
6507 | */ |
51470298 |
6508 | static void ssh2_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
7cca0d81 |
6509 | { |
51470298 |
6510 | if (do_ssh2_transport(ssh, in, inlen, ispkt) == 0) |
32874aea |
6511 | return; |
51470298 |
6512 | do_ssh2_authconn(ssh, in, inlen, ispkt); |
7cca0d81 |
6513 | } |
6514 | |
6515 | /* |
8df7a775 |
6516 | * Called to set up the connection. |
374330e2 |
6517 | * |
6518 | * Returns an error message, or NULL on success. |
374330e2 |
6519 | */ |
cbe2d68f |
6520 | static const char *ssh_init(void *frontend_handle, void **backend_handle, |
6521 | Config *cfg, |
79bf227b |
6522 | char *host, int port, char **realhost, int nodelay, |
6523 | int keepalive) |
32874aea |
6524 | { |
cbe2d68f |
6525 | const char *p; |
51470298 |
6526 | Ssh ssh; |
6527 | |
3d88e64d |
6528 | ssh = snew(struct ssh_tag); |
86916870 |
6529 | ssh->cfg = *cfg; /* STRUCTURE COPY */ |
125105d1 |
6530 | ssh->version = 0; /* when not ready yet */ |
51470298 |
6531 | ssh->s = NULL; |
6532 | ssh->cipher = NULL; |
371e569c |
6533 | ssh->v1_cipher_ctx = NULL; |
0183b242 |
6534 | ssh->crcda_ctx = NULL; |
51470298 |
6535 | ssh->cscipher = NULL; |
371e569c |
6536 | ssh->cs_cipher_ctx = NULL; |
51470298 |
6537 | ssh->sccipher = NULL; |
371e569c |
6538 | ssh->sc_cipher_ctx = NULL; |
51470298 |
6539 | ssh->csmac = NULL; |
a8327734 |
6540 | ssh->cs_mac_ctx = NULL; |
51470298 |
6541 | ssh->scmac = NULL; |
e0e1a00d |
6542 | ssh->sc_mac_ctx = NULL; |
51470298 |
6543 | ssh->cscomp = NULL; |
5366aed8 |
6544 | ssh->cs_comp_ctx = NULL; |
51470298 |
6545 | ssh->sccomp = NULL; |
5366aed8 |
6546 | ssh->sc_comp_ctx = NULL; |
51470298 |
6547 | ssh->kex = NULL; |
389aa499 |
6548 | ssh->kex_ctx = NULL; |
51470298 |
6549 | ssh->hostkey = NULL; |
6550 | ssh->exitcode = -1; |
6551 | ssh->state = SSH_STATE_PREPACKET; |
6552 | ssh->size_needed = FALSE; |
6553 | ssh->eof_needed = FALSE; |
b9d7bcad |
6554 | ssh->ldisc = NULL; |
a8327734 |
6555 | ssh->logctx = NULL; |
51470298 |
6556 | { |
6557 | static const struct Packet empty = { 0, 0, NULL, NULL, 0 }; |
6558 | ssh->pktin = ssh->pktout = empty; |
6559 | } |
6560 | ssh->deferred_send_data = NULL; |
6561 | ssh->deferred_len = 0; |
6562 | ssh->deferred_size = 0; |
9a10ecf4 |
6563 | ssh->pktout_logmode = PKTLOG_EMIT; |
6564 | ssh->pktout_nblanks = 0; |
6565 | ssh->pktout_blanks = NULL; |
51470298 |
6566 | ssh->fallback_cmd = 0; |
6567 | ssh->pkt_ctx = 0; |
302121de |
6568 | ssh->x11auth = NULL; |
be738459 |
6569 | ssh->v1_compressing = FALSE; |
51470298 |
6570 | ssh->v2_outgoing_sequence = 0; |
6571 | ssh->ssh1_rdpkt_crstate = 0; |
6572 | ssh->ssh2_rdpkt_crstate = 0; |
6573 | ssh->do_ssh_init_crstate = 0; |
6574 | ssh->ssh_gotdata_crstate = 0; |
6575 | ssh->ssh1_protocol_crstate = 0; |
6576 | ssh->do_ssh1_login_crstate = 0; |
6577 | ssh->do_ssh2_transport_crstate = 0; |
6578 | ssh->do_ssh2_authconn_crstate = 0; |
6579 | ssh->do_ssh_init_state = NULL; |
6580 | ssh->do_ssh1_login_state = NULL; |
6581 | ssh->do_ssh2_transport_state = NULL; |
6582 | ssh->do_ssh2_authconn_state = NULL; |
6571dbfd |
6583 | ssh->mainchan = NULL; |
968d2d92 |
6584 | ssh->throttled_all = 0; |
6585 | ssh->v1_stdout_throttling = 0; |
51470298 |
6586 | |
6587 | *backend_handle = ssh; |
32874aea |
6588 | |
8f203108 |
6589 | #ifdef MSCRYPTOAPI |
32874aea |
6590 | if (crypto_startup() == 0) |
8f203108 |
6591 | return "Microsoft high encryption pack not installed!"; |
6592 | #endif |
374330e2 |
6593 | |
51470298 |
6594 | ssh->frontend = frontend_handle; |
86916870 |
6595 | ssh->term_width = ssh->cfg.width; |
6596 | ssh->term_height = ssh->cfg.height; |
887035a5 |
6597 | |
fabd1805 |
6598 | ssh->channels = NULL; |
6599 | ssh->rportfwds = NULL; |
6600 | |
51470298 |
6601 | ssh->send_ok = 0; |
6602 | ssh->editing = 0; |
6603 | ssh->echoing = 0; |
6604 | ssh->v1_throttle_count = 0; |
6605 | ssh->overall_bufsize = 0; |
6606 | ssh->fallback_cmd = 0; |
8df7a775 |
6607 | |
3648d4c5 |
6608 | ssh->protocol = NULL; |
6609 | |
79bf227b |
6610 | p = connect_to_host(ssh, host, port, realhost, nodelay, keepalive); |
fb09bf1c |
6611 | if (p != NULL) |
6612 | return p; |
374330e2 |
6613 | |
374330e2 |
6614 | return NULL; |
6615 | } |
6616 | |
fabd1805 |
6617 | static void ssh_free(void *handle) |
6618 | { |
6619 | Ssh ssh = (Ssh) handle; |
6620 | struct ssh_channel *c; |
6621 | struct ssh_rportfwd *pf; |
6622 | |
6623 | if (ssh->v1_cipher_ctx) |
6624 | ssh->cipher->free_context(ssh->v1_cipher_ctx); |
6625 | if (ssh->cs_cipher_ctx) |
6626 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
6627 | if (ssh->sc_cipher_ctx) |
6628 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
6629 | if (ssh->cs_mac_ctx) |
6630 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
6631 | if (ssh->sc_mac_ctx) |
6632 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
29b1d0b3 |
6633 | if (ssh->cs_comp_ctx) { |
6634 | if (ssh->cscomp) |
6635 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
6636 | else |
6637 | zlib_compress_cleanup(ssh->cs_comp_ctx); |
6638 | } |
6639 | if (ssh->sc_comp_ctx) { |
6640 | if (ssh->sccomp) |
6641 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
6642 | else |
6643 | zlib_decompress_cleanup(ssh->sc_comp_ctx); |
6644 | } |
fabd1805 |
6645 | if (ssh->kex_ctx) |
6646 | dh_cleanup(ssh->kex_ctx); |
6647 | sfree(ssh->savedhost); |
6648 | |
6649 | if (ssh->channels) { |
6650 | while ((c = delpos234(ssh->channels, 0)) != NULL) { |
6651 | switch (c->type) { |
6652 | case CHAN_X11: |
6653 | if (c->u.x11.s != NULL) |
6654 | x11_close(c->u.x11.s); |
6655 | break; |
6656 | case CHAN_SOCKDATA: |
6657 | if (c->u.pfd.s != NULL) |
6658 | pfd_close(c->u.pfd.s); |
6659 | break; |
6660 | } |
6661 | sfree(c); |
6662 | } |
6663 | freetree234(ssh->channels); |
6664 | } |
6665 | |
6666 | if (ssh->rportfwds) { |
6667 | while ((pf = delpos234(ssh->rportfwds, 0)) != NULL) |
6668 | sfree(pf); |
6669 | freetree234(ssh->rportfwds); |
6670 | } |
6671 | sfree(ssh->deferred_send_data); |
6672 | if (ssh->x11auth) |
6673 | x11_free_auth(ssh->x11auth); |
6674 | sfree(ssh->do_ssh_init_state); |
6675 | sfree(ssh->do_ssh1_login_state); |
6676 | sfree(ssh->do_ssh2_transport_state); |
6677 | sfree(ssh->do_ssh2_authconn_state); |
679539d7 |
6678 | if (ssh->pktout.data) { |
6679 | sfree(ssh->pktout.data); |
6680 | ssh->pktout.data = NULL; |
6681 | } |
6682 | if (ssh->pktin.data) { |
6683 | sfree(ssh->pktin.data); |
6684 | ssh->pktin.data = NULL; |
6685 | } |
6686 | if (ssh->crcda_ctx) { |
6687 | crcda_free_context(ssh->crcda_ctx); |
6688 | ssh->crcda_ctx = NULL; |
6689 | } |
fabd1805 |
6690 | if (ssh->s) |
36f94d1f |
6691 | ssh_do_close(ssh); |
fabd1805 |
6692 | sfree(ssh); |
6693 | } |
6694 | |
374330e2 |
6695 | /* |
86916870 |
6696 | * Reconfigure the SSH backend. |
6697 | * |
6698 | * Currently, this function does nothing very useful. In future, |
6699 | * however, we could do some handy things with it. For example, we |
6700 | * could make the port forwarding configurer active in the Change |
6701 | * Settings box, and this routine could close down existing |
6702 | * forwardings and open up new ones in response to changes. |
6703 | */ |
6704 | static void ssh_reconfig(void *handle, Config *cfg) |
6705 | { |
6706 | Ssh ssh = (Ssh) handle; |
6707 | ssh->cfg = *cfg; /* STRUCTURE COPY */ |
6708 | } |
6709 | |
6710 | /* |
374330e2 |
6711 | * Called to send data down the Telnet connection. |
6712 | */ |
51470298 |
6713 | static int ssh_send(void *handle, char *buf, int len) |
32874aea |
6714 | { |
51470298 |
6715 | Ssh ssh = (Ssh) handle; |
6716 | |
6717 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
5471d09a |
6718 | return 0; |
374330e2 |
6719 | |
d8baa528 |
6720 | ssh->protocol(ssh, (unsigned char *)buf, len, 0); |
5471d09a |
6721 | |
51470298 |
6722 | return ssh_sendbuffer(ssh); |
5471d09a |
6723 | } |
6724 | |
6725 | /* |
6726 | * Called to query the current amount of buffered stdin data. |
6727 | */ |
51470298 |
6728 | static int ssh_sendbuffer(void *handle) |
5471d09a |
6729 | { |
51470298 |
6730 | Ssh ssh = (Ssh) handle; |
5471d09a |
6731 | int override_value; |
6732 | |
51470298 |
6733 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
5471d09a |
6734 | return 0; |
6735 | |
6736 | /* |
6737 | * If the SSH socket itself has backed up, add the total backup |
6738 | * size on that to any individual buffer on the stdin channel. |
6739 | */ |
6740 | override_value = 0; |
51470298 |
6741 | if (ssh->throttled_all) |
6742 | override_value = ssh->overall_bufsize; |
5471d09a |
6743 | |
51470298 |
6744 | if (ssh->version == 1) { |
5471d09a |
6745 | return override_value; |
51470298 |
6746 | } else if (ssh->version == 2) { |
6747 | if (!ssh->mainchan || ssh->mainchan->closes > 0) |
5471d09a |
6748 | return override_value; |
6749 | else |
51470298 |
6750 | return (override_value + |
6751 | bufchain_size(&ssh->mainchan->v.v2.outbuffer)); |
5471d09a |
6752 | } |
6753 | |
6754 | return 0; |
374330e2 |
6755 | } |
6756 | |
6757 | /* |
6e48c3fe |
6758 | * Called to set the size of the window from SSH's POV. |
374330e2 |
6759 | */ |
51470298 |
6760 | static void ssh_size(void *handle, int width, int height) |
32874aea |
6761 | { |
51470298 |
6762 | Ssh ssh = (Ssh) handle; |
6763 | |
6764 | ssh->term_width = width; |
6765 | ssh->term_height = height; |
f278d6f8 |
6766 | |
51470298 |
6767 | switch (ssh->state) { |
374330e2 |
6768 | case SSH_STATE_BEFORE_SIZE: |
3687d221 |
6769 | case SSH_STATE_PREPACKET: |
21248260 |
6770 | case SSH_STATE_CLOSED: |
374330e2 |
6771 | break; /* do nothing */ |
6772 | case SSH_STATE_INTERMED: |
51470298 |
6773 | ssh->size_needed = TRUE; /* buffer for later */ |
374330e2 |
6774 | break; |
6775 | case SSH_STATE_SESSION: |
86916870 |
6776 | if (!ssh->cfg.nopty) { |
51470298 |
6777 | if (ssh->version == 1) { |
6778 | send_packet(ssh, SSH1_CMSG_WINDOW_SIZE, |
6779 | PKT_INT, ssh->term_height, |
6780 | PKT_INT, ssh->term_width, |
32874aea |
6781 | PKT_INT, 0, PKT_INT, 0, PKT_END); |
0ed48730 |
6782 | } else if (ssh->mainchan) { |
51470298 |
6783 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
6784 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
6785 | ssh2_pkt_addstring(ssh, "window-change"); |
6786 | ssh2_pkt_addbool(ssh, 0); |
6787 | ssh2_pkt_adduint32(ssh, ssh->term_width); |
6788 | ssh2_pkt_adduint32(ssh, ssh->term_height); |
6789 | ssh2_pkt_adduint32(ssh, 0); |
6790 | ssh2_pkt_adduint32(ssh, 0); |
6791 | ssh2_pkt_send(ssh); |
32874aea |
6792 | } |
6793 | } |
6794 | break; |
374330e2 |
6795 | } |
6796 | } |
6797 | |
6798 | /* |
125105d1 |
6799 | * Return a list of the special codes that make sense in this |
6800 | * protocol. |
6801 | */ |
6802 | static const struct telnet_special *ssh_get_specials(void *handle) |
6803 | { |
6804 | Ssh ssh = (Ssh) handle; |
6805 | |
6806 | if (ssh->version == 1) { |
6807 | static const struct telnet_special ssh1_specials[] = { |
6808 | {"IGNORE message", TS_NOP}, |
6809 | {NULL, 0} |
6810 | }; |
6811 | return ssh1_specials; |
6812 | } else if (ssh->version == 2) { |
6813 | static const struct telnet_special ssh2_specials[] = { |
6814 | {"Break", TS_BRK}, |
6815 | {"IGNORE message", TS_NOP}, |
6816 | {NULL, 0} |
6817 | }; |
6818 | return ssh2_specials; |
6819 | } else |
6820 | return NULL; |
6821 | } |
6822 | |
6823 | /* |
6abbf9e3 |
6824 | * Send Telnet special codes. TS_EOF is useful for `plink', so you |
6825 | * can send an EOF and collect resulting output (e.g. `plink |
6826 | * hostname sort'). |
374330e2 |
6827 | */ |
51470298 |
6828 | static void ssh_special(void *handle, Telnet_Special code) |
32874aea |
6829 | { |
51470298 |
6830 | Ssh ssh = (Ssh) handle; |
6831 | |
6abbf9e3 |
6832 | if (code == TS_EOF) { |
51470298 |
6833 | if (ssh->state != SSH_STATE_SESSION) { |
32874aea |
6834 | /* |
6835 | * Buffer the EOF in case we are pre-SESSION, so we can |
6836 | * send it as soon as we reach SESSION. |
6837 | */ |
6838 | if (code == TS_EOF) |
51470298 |
6839 | ssh->eof_needed = TRUE; |
32874aea |
6840 | return; |
6841 | } |
51470298 |
6842 | if (ssh->version == 1) { |
6843 | send_packet(ssh, SSH1_CMSG_EOF, PKT_END); |
0ed48730 |
6844 | } else if (ssh->mainchan) { |
51470298 |
6845 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_EOF); |
6846 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
6847 | ssh2_pkt_send(ssh); |
32874aea |
6848 | } |
6849 | logevent("Sent EOF message"); |
125105d1 |
6850 | } else if (code == TS_PING || code == TS_NOP) { |
51470298 |
6851 | if (ssh->state == SSH_STATE_CLOSED |
6852 | || ssh->state == SSH_STATE_PREPACKET) return; |
6853 | if (ssh->version == 1) { |
6854 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) |
6855 | send_packet(ssh, SSH1_MSG_IGNORE, PKT_STR, "", PKT_END); |
32874aea |
6856 | } else { |
51470298 |
6857 | ssh2_pkt_init(ssh, SSH2_MSG_IGNORE); |
6858 | ssh2_pkt_addstring_start(ssh); |
6859 | ssh2_pkt_send(ssh); |
32874aea |
6860 | } |
125105d1 |
6861 | } else if (code == TS_BRK) { |
6862 | if (ssh->state == SSH_STATE_CLOSED |
6863 | || ssh->state == SSH_STATE_PREPACKET) return; |
6864 | if (ssh->version == 1) { |
6865 | logevent("Unable to send BREAK signal in SSH1"); |
0ed48730 |
6866 | } else if (ssh->mainchan) { |
125105d1 |
6867 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
6868 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
6869 | ssh2_pkt_addstring(ssh, "break"); |
6870 | ssh2_pkt_addbool(ssh, 0); |
6871 | ssh2_pkt_adduint32(ssh, 0); /* default break length */ |
6872 | ssh2_pkt_send(ssh); |
6873 | } |
6abbf9e3 |
6874 | } else { |
32874aea |
6875 | /* do nothing */ |
6abbf9e3 |
6876 | } |
374330e2 |
6877 | } |
6878 | |
51470298 |
6879 | void *new_sock_channel(void *handle, Socket s) |
d74d141c |
6880 | { |
51470298 |
6881 | Ssh ssh = (Ssh) handle; |
d74d141c |
6882 | struct ssh_channel *c; |
3d88e64d |
6883 | c = snew(struct ssh_channel); |
51470298 |
6884 | c->ssh = ssh; |
d74d141c |
6885 | |
6886 | if (c) { |
bc240b21 |
6887 | c->remoteid = -1; /* to be set when open confirmed */ |
51470298 |
6888 | c->localid = alloc_channel_id(ssh); |
d74d141c |
6889 | c->closes = 0; |
bc240b21 |
6890 | c->type = CHAN_SOCKDATA_DORMANT;/* identify channel type */ |
d74d141c |
6891 | c->u.pfd.s = s; |
013dd8c0 |
6892 | bufchain_init(&c->v.v2.outbuffer); |
51470298 |
6893 | add234(ssh->channels, c); |
d74d141c |
6894 | } |
6895 | return c; |
6896 | } |
6897 | |
5471d09a |
6898 | /* |
6899 | * This is called when stdout/stderr (the entity to which |
6900 | * from_backend sends data) manages to clear some backlog. |
6901 | */ |
ae9ae89f |
6902 | static void ssh_unthrottle(void *handle, int bufsize) |
5471d09a |
6903 | { |
51470298 |
6904 | Ssh ssh = (Ssh) handle; |
6905 | if (ssh->version == 1) { |
6906 | if (ssh->v1_stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) { |
6907 | ssh->v1_stdout_throttling = 0; |
6908 | ssh1_throttle(ssh, -1); |
5471d09a |
6909 | } |
6910 | } else { |
51470298 |
6911 | if (ssh->mainchan && ssh->mainchan->closes == 0) |
6912 | ssh2_set_window(ssh->mainchan, OUR_V2_WINSIZE - bufsize); |
5471d09a |
6913 | } |
6914 | } |
6915 | |
6b78788a |
6916 | void ssh_send_port_open(void *channel, char *hostname, int port, char *org) |
d74d141c |
6917 | { |
6918 | struct ssh_channel *c = (struct ssh_channel *)channel; |
6b78788a |
6919 | Ssh ssh = c->ssh; |
d74d141c |
6920 | |
57356d63 |
6921 | logeventf(ssh, "Opening forwarded connection to %s:%d", hostname, port); |
d74d141c |
6922 | |
51470298 |
6923 | if (ssh->version == 1) { |
6924 | send_packet(ssh, SSH1_MSG_PORT_OPEN, |
bc240b21 |
6925 | PKT_INT, c->localid, |
6926 | PKT_STR, hostname, |
6927 | PKT_INT, port, |
6928 | //PKT_STR, <org:orgport>, |
6929 | PKT_END); |
6930 | } else { |
51470298 |
6931 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN); |
6932 | ssh2_pkt_addstring(ssh, "direct-tcpip"); |
6933 | ssh2_pkt_adduint32(ssh, c->localid); |
5471d09a |
6934 | c->v.v2.locwindow = OUR_V2_WINSIZE; |
51470298 |
6935 | ssh2_pkt_adduint32(ssh, c->v.v2.locwindow);/* our window size */ |
6936 | ssh2_pkt_adduint32(ssh, 0x4000UL); /* our max pkt size */ |
6937 | ssh2_pkt_addstring(ssh, hostname); |
6938 | ssh2_pkt_adduint32(ssh, port); |
bc240b21 |
6939 | /* |
6940 | * We make up values for the originator data; partly it's |
6941 | * too much hassle to keep track, and partly I'm not |
6942 | * convinced the server should be told details like that |
6943 | * about my local network configuration. |
6944 | */ |
51470298 |
6945 | ssh2_pkt_addstring(ssh, "client-side-connection"); |
6946 | ssh2_pkt_adduint32(ssh, 0); |
6947 | ssh2_pkt_send(ssh); |
bc240b21 |
6948 | } |
d74d141c |
6949 | } |
6950 | |
6951 | |
51470298 |
6952 | static Socket ssh_socket(void *handle) |
32874aea |
6953 | { |
51470298 |
6954 | Ssh ssh = (Ssh) handle; |
6955 | return ssh->s; |
32874aea |
6956 | } |
8ccc75b0 |
6957 | |
51470298 |
6958 | static int ssh_sendok(void *handle) |
32874aea |
6959 | { |
51470298 |
6960 | Ssh ssh = (Ssh) handle; |
6961 | return ssh->send_ok; |
32874aea |
6962 | } |
fb09bf1c |
6963 | |
51470298 |
6964 | static int ssh_ldisc(void *handle, int option) |
32874aea |
6965 | { |
51470298 |
6966 | Ssh ssh = (Ssh) handle; |
32874aea |
6967 | if (option == LD_ECHO) |
51470298 |
6968 | return ssh->echoing; |
32874aea |
6969 | if (option == LD_EDIT) |
51470298 |
6970 | return ssh->editing; |
0965bee0 |
6971 | return FALSE; |
6972 | } |
6973 | |
b9d7bcad |
6974 | static void ssh_provide_ldisc(void *handle, void *ldisc) |
6975 | { |
6976 | Ssh ssh = (Ssh) handle; |
6977 | ssh->ldisc = ldisc; |
6978 | } |
6979 | |
a8327734 |
6980 | static void ssh_provide_logctx(void *handle, void *logctx) |
6981 | { |
6982 | Ssh ssh = (Ssh) handle; |
6983 | ssh->logctx = logctx; |
6984 | } |
6985 | |
51470298 |
6986 | static int ssh_return_exitcode(void *handle) |
6987 | { |
6988 | Ssh ssh = (Ssh) handle; |
3bb2f322 |
6989 | if (ssh->s != NULL) |
6990 | return -1; |
6991 | else |
6992 | return (ssh->exitcode >= 0 ? ssh->exitcode : 0); |
51470298 |
6993 | } |
6994 | |
6995 | /* |
6996 | * Gross hack: pscp will try to start SFTP but fall back to scp1 if |
6997 | * that fails. This variable is the means by which scp.c can reach |
6998 | * into the SSH code and find out which one it got. |
6999 | */ |
7000 | extern int ssh_fallback_cmd(void *handle) |
d8d6c7e5 |
7001 | { |
51470298 |
7002 | Ssh ssh = (Ssh) handle; |
7003 | return ssh->fallback_cmd; |
d8d6c7e5 |
7004 | } |
7005 | |
374330e2 |
7006 | Backend ssh_backend = { |
7007 | ssh_init, |
fabd1805 |
7008 | ssh_free, |
86916870 |
7009 | ssh_reconfig, |
374330e2 |
7010 | ssh_send, |
5471d09a |
7011 | ssh_sendbuffer, |
374330e2 |
7012 | ssh_size, |
4017be6d |
7013 | ssh_special, |
125105d1 |
7014 | ssh_get_specials, |
8ccc75b0 |
7015 | ssh_socket, |
d8d6c7e5 |
7016 | ssh_return_exitcode, |
97db3be4 |
7017 | ssh_sendok, |
0965bee0 |
7018 | ssh_ldisc, |
b9d7bcad |
7019 | ssh_provide_ldisc, |
a8327734 |
7020 | ssh_provide_logctx, |
5471d09a |
7021 | ssh_unthrottle, |
97db3be4 |
7022 | 22 |
bc240b21 |
7023 | }; |