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