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