eaf1e20a |
1 | /* |
2 | * SSH backend. |
3 | */ |
4 | |
374330e2 |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
fb09bf1c |
7 | #include <stdarg.h> |
8 | #include <assert.h> |
3c112d53 |
9 | #include <limits.h> |
d051d1b4 |
10 | #include <signal.h> |
374330e2 |
11 | |
12 | #include "putty.h" |
dacbd0e8 |
13 | #include "tree234.h" |
fb09bf1c |
14 | #include "ssh.h" |
374330e2 |
15 | |
16 | #ifndef FALSE |
17 | #define FALSE 0 |
18 | #endif |
19 | #ifndef TRUE |
20 | #define TRUE 1 |
21 | #endif |
22 | |
32874aea |
23 | #define SSH1_MSG_DISCONNECT 1 /* 0x1 */ |
24 | #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */ |
25 | #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */ |
26 | #define SSH1_CMSG_USER 4 /* 0x4 */ |
27 | #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */ |
28 | #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */ |
29 | #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */ |
30 | #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */ |
31 | #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */ |
32 | #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */ |
33 | #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */ |
34 | #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */ |
35 | #define SSH1_SMSG_SUCCESS 14 /* 0xe */ |
36 | #define SSH1_SMSG_FAILURE 15 /* 0xf */ |
37 | #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */ |
38 | #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */ |
39 | #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */ |
40 | #define SSH1_CMSG_EOF 19 /* 0x13 */ |
41 | #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */ |
42 | #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */ |
43 | #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */ |
44 | #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */ |
45 | #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */ |
46 | #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */ |
47 | #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */ |
48 | #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */ |
49 | #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */ |
50 | #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */ |
51 | #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */ |
52 | #define SSH1_MSG_IGNORE 32 /* 0x20 */ |
53 | #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */ |
54 | #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */ |
55 | #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */ |
56 | #define SSH1_MSG_DEBUG 36 /* 0x24 */ |
57 | #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */ |
58 | #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */ |
59 | #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */ |
60 | #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */ |
61 | #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */ |
62 | #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */ |
63 | #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */ |
64 | |
8f9e3231 |
65 | #define SSH1_AUTH_RHOSTS 1 /* 0x1 */ |
66 | #define SSH1_AUTH_RSA 2 /* 0x2 */ |
67 | #define SSH1_AUTH_PASSWORD 3 /* 0x3 */ |
68 | #define SSH1_AUTH_RHOSTS_RSA 4 /* 0x4 */ |
32874aea |
69 | #define SSH1_AUTH_TIS 5 /* 0x5 */ |
70 | #define SSH1_AUTH_CCARD 16 /* 0x10 */ |
71 | |
72 | #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */ |
b96dc54c |
73 | /* Mask for protoflags we will echo back to server if seen */ |
32874aea |
74 | #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */ |
75 | |
76 | #define SSH2_MSG_DISCONNECT 1 /* 0x1 */ |
77 | #define SSH2_MSG_IGNORE 2 /* 0x2 */ |
78 | #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */ |
79 | #define SSH2_MSG_DEBUG 4 /* 0x4 */ |
80 | #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */ |
81 | #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */ |
82 | #define SSH2_MSG_KEXINIT 20 /* 0x14 */ |
83 | #define SSH2_MSG_NEWKEYS 21 /* 0x15 */ |
84 | #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */ |
85 | #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */ |
86 | #define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */ |
87 | #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */ |
88 | #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */ |
89 | #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */ |
fae1a71b |
90 | #define SSH2_MSG_KEXRSA_PUBKEY 30 /* 0x1e */ |
91 | #define SSH2_MSG_KEXRSA_SECRET 31 /* 0x1f */ |
92 | #define SSH2_MSG_KEXRSA_DONE 32 /* 0x20 */ |
32874aea |
93 | #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */ |
94 | #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */ |
95 | #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */ |
96 | #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */ |
97 | #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */ |
98 | #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */ |
761187b6 |
99 | #define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */ |
100 | #define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */ |
32874aea |
101 | #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */ |
102 | #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */ |
103 | #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */ |
104 | #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */ |
105 | #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */ |
106 | #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */ |
107 | #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */ |
108 | #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */ |
109 | #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */ |
110 | #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */ |
111 | #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */ |
112 | #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */ |
113 | #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */ |
114 | #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */ |
115 | |
00db133f |
116 | /* |
117 | * Packet type contexts, so that ssh2_pkt_type can correctly decode |
118 | * the ambiguous type numbers back into the correct type strings. |
119 | */ |
acab36bc |
120 | typedef enum { |
121 | SSH2_PKTCTX_NOKEX, |
122 | SSH2_PKTCTX_DHGROUP, |
123 | SSH2_PKTCTX_DHGEX, |
124 | SSH2_PKTCTX_RSAKEX |
125 | } Pkt_KCtx; |
126 | typedef enum { |
127 | SSH2_PKTCTX_NOAUTH, |
128 | SSH2_PKTCTX_PUBLICKEY, |
129 | SSH2_PKTCTX_PASSWORD, |
130 | SSH2_PKTCTX_KBDINTER |
131 | } Pkt_ACtx; |
00db133f |
132 | |
32874aea |
133 | #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */ |
134 | #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */ |
135 | #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */ |
136 | #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */ |
137 | #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */ |
138 | #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */ |
139 | #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */ |
140 | #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */ |
141 | #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */ |
142 | #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */ |
143 | #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */ |
144 | #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */ |
145 | #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */ |
146 | #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */ |
147 | #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */ |
38c4a8da |
148 | |
149 | static const char *const ssh2_disconnect_reasons[] = { |
150 | NULL, |
afb4d0dc |
151 | "host not allowed to connect", |
152 | "protocol error", |
153 | "key exchange failed", |
154 | "host authentication failed", |
1da38d29 |
155 | "MAC error", |
afb4d0dc |
156 | "compression error", |
157 | "service not available", |
158 | "protocol version not supported", |
159 | "host key not verifiable", |
160 | "connection lost", |
161 | "by application", |
162 | "too many connections", |
163 | "auth cancelled by user", |
164 | "no more auth methods available", |
165 | "illegal user name", |
38c4a8da |
166 | }; |
9005f3ba |
167 | |
32874aea |
168 | #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */ |
169 | #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */ |
170 | #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */ |
171 | #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */ |
d211621f |
172 | |
32874aea |
173 | #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */ |
fb09bf1c |
174 | |
7d503c31 |
175 | /* |
176 | * Various remote-bug flags. |
177 | */ |
178 | #define BUG_CHOKES_ON_SSH1_IGNORE 1 |
179 | #define BUG_SSH2_HMAC 2 |
bd358db1 |
180 | #define BUG_NEEDS_SSH1_PLAIN_PASSWORD 4 |
0df73905 |
181 | #define BUG_CHOKES_ON_RSA 8 |
1dd353b5 |
182 | #define BUG_SSH2_RSA_PADDING 16 |
088bde77 |
183 | #define BUG_SSH2_DERIVEKEY 32 |
f382c87d |
184 | #define BUG_SSH2_REKEY 64 |
dda87a28 |
185 | #define BUG_SSH2_PK_SESSIONID 128 |
bd358db1 |
186 | |
c6ccd5c2 |
187 | /* |
188 | * Codes for terminal modes. |
189 | * Most of these are the same in SSH-1 and SSH-2. |
7646c432 |
190 | * This list is derived from RFC 4254 and |
c6ccd5c2 |
191 | * SSH-1 RFC-1.2.31. |
192 | */ |
193 | static const struct { |
194 | const char* const mode; |
195 | int opcode; |
196 | enum { TTY_OP_CHAR, TTY_OP_BOOL } type; |
197 | } ssh_ttymodes[] = { |
198 | /* "V" prefix discarded for special characters relative to SSH specs */ |
199 | { "INTR", 1, TTY_OP_CHAR }, |
200 | { "QUIT", 2, TTY_OP_CHAR }, |
201 | { "ERASE", 3, TTY_OP_CHAR }, |
202 | { "KILL", 4, TTY_OP_CHAR }, |
203 | { "EOF", 5, TTY_OP_CHAR }, |
204 | { "EOL", 6, TTY_OP_CHAR }, |
205 | { "EOL2", 7, TTY_OP_CHAR }, |
206 | { "START", 8, TTY_OP_CHAR }, |
207 | { "STOP", 9, TTY_OP_CHAR }, |
208 | { "SUSP", 10, TTY_OP_CHAR }, |
209 | { "DSUSP", 11, TTY_OP_CHAR }, |
210 | { "REPRINT", 12, TTY_OP_CHAR }, |
211 | { "WERASE", 13, TTY_OP_CHAR }, |
212 | { "LNEXT", 14, TTY_OP_CHAR }, |
213 | { "FLUSH", 15, TTY_OP_CHAR }, |
214 | { "SWTCH", 16, TTY_OP_CHAR }, |
215 | { "STATUS", 17, TTY_OP_CHAR }, |
216 | { "DISCARD", 18, TTY_OP_CHAR }, |
217 | { "IGNPAR", 30, TTY_OP_BOOL }, |
218 | { "PARMRK", 31, TTY_OP_BOOL }, |
219 | { "INPCK", 32, TTY_OP_BOOL }, |
220 | { "ISTRIP", 33, TTY_OP_BOOL }, |
221 | { "INLCR", 34, TTY_OP_BOOL }, |
222 | { "IGNCR", 35, TTY_OP_BOOL }, |
223 | { "ICRNL", 36, TTY_OP_BOOL }, |
224 | { "IUCLC", 37, TTY_OP_BOOL }, |
225 | { "IXON", 38, TTY_OP_BOOL }, |
226 | { "IXANY", 39, TTY_OP_BOOL }, |
227 | { "IXOFF", 40, TTY_OP_BOOL }, |
228 | { "IMAXBEL", 41, TTY_OP_BOOL }, |
229 | { "ISIG", 50, TTY_OP_BOOL }, |
230 | { "ICANON", 51, TTY_OP_BOOL }, |
231 | { "XCASE", 52, TTY_OP_BOOL }, |
232 | { "ECHO", 53, TTY_OP_BOOL }, |
233 | { "ECHOE", 54, TTY_OP_BOOL }, |
234 | { "ECHOK", 55, TTY_OP_BOOL }, |
235 | { "ECHONL", 56, TTY_OP_BOOL }, |
236 | { "NOFLSH", 57, TTY_OP_BOOL }, |
237 | { "TOSTOP", 58, TTY_OP_BOOL }, |
238 | { "IEXTEN", 59, TTY_OP_BOOL }, |
239 | { "ECHOCTL", 60, TTY_OP_BOOL }, |
240 | { "ECHOKE", 61, TTY_OP_BOOL }, |
241 | { "PENDIN", 62, TTY_OP_BOOL }, /* XXX is this a real mode? */ |
242 | { "OPOST", 70, TTY_OP_BOOL }, |
243 | { "OLCUC", 71, TTY_OP_BOOL }, |
244 | { "ONLCR", 72, TTY_OP_BOOL }, |
245 | { "OCRNL", 73, TTY_OP_BOOL }, |
246 | { "ONOCR", 74, TTY_OP_BOOL }, |
247 | { "ONLRET", 75, TTY_OP_BOOL }, |
248 | { "CS7", 90, TTY_OP_BOOL }, |
249 | { "CS8", 91, TTY_OP_BOOL }, |
250 | { "PARENB", 92, TTY_OP_BOOL }, |
251 | { "PARODD", 93, TTY_OP_BOOL } |
252 | }; |
253 | |
254 | /* Miscellaneous other tty-related constants. */ |
255 | #define SSH_TTY_OP_END 0 |
256 | /* The opcodes for ISPEED/OSPEED differ between SSH-1 and SSH-2. */ |
257 | #define SSH1_TTY_OP_ISPEED 192 |
258 | #define SSH1_TTY_OP_OSPEED 193 |
259 | #define SSH2_TTY_OP_ISPEED 128 |
260 | #define SSH2_TTY_OP_OSPEED 129 |
261 | |
262 | /* Helper functions for parsing tty-related config. */ |
263 | static unsigned int ssh_tty_parse_specchar(char *s) |
264 | { |
265 | unsigned int ret; |
266 | if (*s) { |
267 | char *next = NULL; |
268 | ret = ctrlparse(s, &next); |
269 | if (!next) ret = s[0]; |
270 | } else { |
271 | ret = 255; /* special value meaning "don't set" */ |
272 | } |
273 | return ret; |
274 | } |
275 | static unsigned int ssh_tty_parse_boolean(char *s) |
276 | { |
277 | if (stricmp(s, "yes") == 0 || |
278 | stricmp(s, "on") == 0 || |
279 | stricmp(s, "true") == 0 || |
280 | stricmp(s, "+") == 0) |
281 | return 1; /* true */ |
282 | else if (stricmp(s, "no") == 0 || |
283 | stricmp(s, "off") == 0 || |
284 | stricmp(s, "false") == 0 || |
285 | stricmp(s, "-") == 0) |
286 | return 0; /* false */ |
287 | else |
288 | return (atoi(s) != 0); |
289 | } |
290 | |
00db133f |
291 | #define translate(x) if (type == x) return #x |
acab36bc |
292 | #define translatek(x,ctx) if (type == x && (pkt_kctx == ctx)) return #x |
293 | #define translatea(x,ctx) if (type == x && (pkt_actx == ctx)) return #x |
ae9ae89f |
294 | static char *ssh1_pkt_type(int type) |
00db133f |
295 | { |
296 | translate(SSH1_MSG_DISCONNECT); |
297 | translate(SSH1_SMSG_PUBLIC_KEY); |
298 | translate(SSH1_CMSG_SESSION_KEY); |
299 | translate(SSH1_CMSG_USER); |
300 | translate(SSH1_CMSG_AUTH_RSA); |
301 | translate(SSH1_SMSG_AUTH_RSA_CHALLENGE); |
302 | translate(SSH1_CMSG_AUTH_RSA_RESPONSE); |
303 | translate(SSH1_CMSG_AUTH_PASSWORD); |
304 | translate(SSH1_CMSG_REQUEST_PTY); |
305 | translate(SSH1_CMSG_WINDOW_SIZE); |
306 | translate(SSH1_CMSG_EXEC_SHELL); |
307 | translate(SSH1_CMSG_EXEC_CMD); |
308 | translate(SSH1_SMSG_SUCCESS); |
309 | translate(SSH1_SMSG_FAILURE); |
310 | translate(SSH1_CMSG_STDIN_DATA); |
311 | translate(SSH1_SMSG_STDOUT_DATA); |
312 | translate(SSH1_SMSG_STDERR_DATA); |
313 | translate(SSH1_CMSG_EOF); |
314 | translate(SSH1_SMSG_EXIT_STATUS); |
315 | translate(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION); |
316 | translate(SSH1_MSG_CHANNEL_OPEN_FAILURE); |
317 | translate(SSH1_MSG_CHANNEL_DATA); |
318 | translate(SSH1_MSG_CHANNEL_CLOSE); |
319 | translate(SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION); |
320 | translate(SSH1_SMSG_X11_OPEN); |
321 | translate(SSH1_CMSG_PORT_FORWARD_REQUEST); |
322 | translate(SSH1_MSG_PORT_OPEN); |
323 | translate(SSH1_CMSG_AGENT_REQUEST_FORWARDING); |
324 | translate(SSH1_SMSG_AGENT_OPEN); |
325 | translate(SSH1_MSG_IGNORE); |
326 | translate(SSH1_CMSG_EXIT_CONFIRMATION); |
327 | translate(SSH1_CMSG_X11_REQUEST_FORWARDING); |
328 | translate(SSH1_CMSG_AUTH_RHOSTS_RSA); |
329 | translate(SSH1_MSG_DEBUG); |
330 | translate(SSH1_CMSG_REQUEST_COMPRESSION); |
331 | translate(SSH1_CMSG_AUTH_TIS); |
332 | translate(SSH1_SMSG_AUTH_TIS_CHALLENGE); |
333 | translate(SSH1_CMSG_AUTH_TIS_RESPONSE); |
334 | translate(SSH1_CMSG_AUTH_CCARD); |
335 | translate(SSH1_SMSG_AUTH_CCARD_CHALLENGE); |
336 | translate(SSH1_CMSG_AUTH_CCARD_RESPONSE); |
337 | return "unknown"; |
338 | } |
acab36bc |
339 | static char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type) |
00db133f |
340 | { |
341 | translate(SSH2_MSG_DISCONNECT); |
342 | translate(SSH2_MSG_IGNORE); |
343 | translate(SSH2_MSG_UNIMPLEMENTED); |
344 | translate(SSH2_MSG_DEBUG); |
345 | translate(SSH2_MSG_SERVICE_REQUEST); |
346 | translate(SSH2_MSG_SERVICE_ACCEPT); |
347 | translate(SSH2_MSG_KEXINIT); |
348 | translate(SSH2_MSG_NEWKEYS); |
acab36bc |
349 | translatek(SSH2_MSG_KEXDH_INIT, SSH2_PKTCTX_DHGROUP); |
350 | translatek(SSH2_MSG_KEXDH_REPLY, SSH2_PKTCTX_DHGROUP); |
351 | translatek(SSH2_MSG_KEX_DH_GEX_REQUEST, SSH2_PKTCTX_DHGEX); |
352 | translatek(SSH2_MSG_KEX_DH_GEX_GROUP, SSH2_PKTCTX_DHGEX); |
353 | translatek(SSH2_MSG_KEX_DH_GEX_INIT, SSH2_PKTCTX_DHGEX); |
354 | translatek(SSH2_MSG_KEX_DH_GEX_REPLY, SSH2_PKTCTX_DHGEX); |
355 | translatek(SSH2_MSG_KEXRSA_PUBKEY, SSH2_PKTCTX_RSAKEX); |
356 | translatek(SSH2_MSG_KEXRSA_SECRET, SSH2_PKTCTX_RSAKEX); |
357 | translatek(SSH2_MSG_KEXRSA_DONE, SSH2_PKTCTX_RSAKEX); |
00db133f |
358 | translate(SSH2_MSG_USERAUTH_REQUEST); |
359 | translate(SSH2_MSG_USERAUTH_FAILURE); |
360 | translate(SSH2_MSG_USERAUTH_SUCCESS); |
361 | translate(SSH2_MSG_USERAUTH_BANNER); |
acab36bc |
362 | translatea(SSH2_MSG_USERAUTH_PK_OK, SSH2_PKTCTX_PUBLICKEY); |
363 | translatea(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, SSH2_PKTCTX_PASSWORD); |
364 | translatea(SSH2_MSG_USERAUTH_INFO_REQUEST, SSH2_PKTCTX_KBDINTER); |
365 | translatea(SSH2_MSG_USERAUTH_INFO_RESPONSE, SSH2_PKTCTX_KBDINTER); |
00db133f |
366 | translate(SSH2_MSG_GLOBAL_REQUEST); |
367 | translate(SSH2_MSG_REQUEST_SUCCESS); |
368 | translate(SSH2_MSG_REQUEST_FAILURE); |
369 | translate(SSH2_MSG_CHANNEL_OPEN); |
370 | translate(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
371 | translate(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
372 | translate(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
373 | translate(SSH2_MSG_CHANNEL_DATA); |
374 | translate(SSH2_MSG_CHANNEL_EXTENDED_DATA); |
375 | translate(SSH2_MSG_CHANNEL_EOF); |
376 | translate(SSH2_MSG_CHANNEL_CLOSE); |
377 | translate(SSH2_MSG_CHANNEL_REQUEST); |
378 | translate(SSH2_MSG_CHANNEL_SUCCESS); |
379 | translate(SSH2_MSG_CHANNEL_FAILURE); |
380 | return "unknown"; |
381 | } |
382 | #undef translate |
383 | #undef translatec |
7d503c31 |
384 | |
9a10ecf4 |
385 | /* Enumeration values for fields in SSH-1 packets */ |
386 | enum { |
387 | PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM, |
388 | /* These values are for communicating relevant semantics of |
389 | * fields to the packet logging code. */ |
390 | PKTT_OTHER, PKTT_PASSWORD, PKTT_DATA |
391 | }; |
972a41c8 |
392 | |
acddebd9 |
393 | /* |
394 | * Coroutine mechanics for the sillier bits of the code. If these |
395 | * macros look impenetrable to you, you might find it helpful to |
396 | * read |
397 | * |
398 | * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html |
399 | * |
400 | * which explains the theory behind these macros. |
b624d1e6 |
401 | * |
402 | * In particular, if you are getting `case expression not constant' |
403 | * errors when building with MS Visual Studio, this is because MS's |
404 | * Edit and Continue debugging feature causes their compiler to |
405 | * violate ANSI C. To disable Edit and Continue debugging: |
406 | * |
407 | * - right-click ssh.c in the FileView |
408 | * - click Settings |
409 | * - select the C/C++ tab and the General category |
410 | * - under `Debug info:', select anything _other_ than `Program |
411 | * Database for Edit and Continue'. |
acddebd9 |
412 | */ |
51470298 |
413 | #define crBegin(v) { int *crLine = &v; switch(v) { case 0:; |
414 | #define crState(t) \ |
415 | struct t *s; \ |
3d88e64d |
416 | if (!ssh->t) ssh->t = snew(struct t); \ |
51470298 |
417 | s = ssh->t; |
418 | #define crFinish(z) } *crLine = 0; return (z); } |
419 | #define crFinishV } *crLine = 0; return; } |
374330e2 |
420 | #define crReturn(z) \ |
421 | do {\ |
51470298 |
422 | *crLine =__LINE__; return (z); case __LINE__:;\ |
374330e2 |
423 | } while (0) |
424 | #define crReturnV \ |
425 | do {\ |
51470298 |
426 | *crLine=__LINE__; return; case __LINE__:;\ |
374330e2 |
427 | } while (0) |
51470298 |
428 | #define crStop(z) do{ *crLine = 0; return (z); }while(0) |
429 | #define crStopV do{ *crLine = 0; return; }while(0) |
fb09bf1c |
430 | #define crWaitUntil(c) do { crReturn(0); } while (!(c)) |
7cca0d81 |
431 | #define crWaitUntilV(c) do { crReturnV; } while (!(c)) |
374330e2 |
432 | |
51470298 |
433 | typedef struct ssh_tag *Ssh; |
ff3187f6 |
434 | struct Packet; |
435 | |
dacd8872 |
436 | static struct Packet *ssh1_pkt_init(int pkt_type); |
ff3187f6 |
437 | static struct Packet *ssh2_pkt_init(int pkt_type); |
dacd8872 |
438 | static void ssh_pkt_ensure(struct Packet *, int length); |
439 | static void ssh_pkt_adddata(struct Packet *, void *data, int len); |
440 | static void ssh_pkt_addbyte(struct Packet *, unsigned char value); |
ff3187f6 |
441 | static void ssh2_pkt_addbool(struct Packet *, unsigned char value); |
dacd8872 |
442 | static void ssh_pkt_adduint32(struct Packet *, unsigned long value); |
443 | static void ssh_pkt_addstring_start(struct Packet *); |
444 | static void ssh_pkt_addstring_str(struct Packet *, char *data); |
445 | static void ssh_pkt_addstring_data(struct Packet *, char *data, int len); |
446 | static void ssh_pkt_addstring(struct Packet *, char *data); |
d8baa528 |
447 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len); |
dacd8872 |
448 | static void ssh1_pkt_addmp(struct Packet *, Bignum b); |
ff3187f6 |
449 | static void ssh2_pkt_addmp(struct Packet *, Bignum b); |
450 | static int ssh2_pkt_construct(Ssh, struct Packet *); |
451 | static void ssh2_pkt_send(Ssh, struct Packet *); |
590f6a5f |
452 | static void ssh2_pkt_send_noqueue(Ssh, struct Packet *); |
ff3187f6 |
453 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, |
454 | struct Packet *pktin); |
455 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, |
456 | struct Packet *pktin); |
3d63ca2e |
457 | |
5471d09a |
458 | /* |
459 | * Buffer management constants. There are several of these for |
460 | * various different purposes: |
461 | * |
462 | * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up |
463 | * on a local data stream before we throttle the whole SSH |
2e85c969 |
464 | * connection (in SSH-1 only). Throttling the whole connection is |
5471d09a |
465 | * pretty drastic so we set this high in the hope it won't |
466 | * happen very often. |
467 | * |
468 | * - SSH_MAX_BACKLOG is the amount of backlog that must build up |
469 | * on the SSH connection itself before we defensively throttle |
470 | * _all_ local data streams. This is pretty drastic too (though |
2e85c969 |
471 | * thankfully unlikely in SSH-2 since the window mechanism should |
5471d09a |
472 | * ensure that the server never has any need to throttle its end |
473 | * of the connection), so we set this high as well. |
474 | * |
2e85c969 |
475 | * - OUR_V2_WINSIZE is the maximum window size we present on SSH-2 |
5471d09a |
476 | * channels. |
9b53e9b8 |
477 | * |
478 | * - OUR_V2_BIGWIN is the window size we advertise for the only |
3361b80a |
479 | * channel in a simple connection. It must be <= INT_MAX. |
5471d09a |
480 | */ |
481 | |
482 | #define SSH1_BUFFER_LIMIT 32768 |
483 | #define SSH_MAX_BACKLOG 32768 |
484 | #define OUR_V2_WINSIZE 16384 |
3361b80a |
485 | #define OUR_V2_BIGWIN 0x7fffffff |
954d5c5a |
486 | #define OUR_V2_MAXPKT 0x4000UL |
d74d141c |
487 | |
edd0cb8a |
488 | /* Maximum length of passwords/passphrases (arbitrary) */ |
489 | #define SSH_MAX_PASSWORD_LEN 100 |
490 | |
85cc02bb |
491 | const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss }; |
e5574168 |
492 | |
8b2715b2 |
493 | const static struct ssh_mac *macs[] = { |
6668a75e |
494 | &ssh_hmac_sha1, &ssh_hmac_sha1_96, &ssh_hmac_md5 |
32874aea |
495 | }; |
8b2715b2 |
496 | const static struct ssh_mac *buggymacs[] = { |
6668a75e |
497 | &ssh_hmac_sha1_buggy, &ssh_hmac_sha1_96_buggy, &ssh_hmac_md5 |
32874aea |
498 | }; |
e5574168 |
499 | |
5366aed8 |
500 | static void *ssh_comp_none_init(void) |
501 | { |
502 | return NULL; |
503 | } |
504 | static void ssh_comp_none_cleanup(void *handle) |
32874aea |
505 | { |
506 | } |
5366aed8 |
507 | static int ssh_comp_none_block(void *handle, unsigned char *block, int len, |
32874aea |
508 | unsigned char **outblock, int *outlen) |
509 | { |
510 | return 0; |
511 | } |
5366aed8 |
512 | static int ssh_comp_none_disable(void *handle) |
32874aea |
513 | { |
4ba9b64b |
514 | return 0; |
515 | } |
57476f6b |
516 | const static struct ssh_compress ssh_comp_none = { |
4ba9b64b |
517 | "none", |
5366aed8 |
518 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
519 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
520 | ssh_comp_none_disable, NULL |
e5574168 |
521 | }; |
4ba9b64b |
522 | extern const struct ssh_compress ssh_zlib; |
523 | const static struct ssh_compress *compressions[] = { |
32874aea |
524 | &ssh_zlib, &ssh_comp_none |
525 | }; |
374330e2 |
526 | |
32874aea |
527 | enum { /* channel types */ |
783415f8 |
528 | CHAN_MAINSESSION, |
529 | CHAN_X11, |
530 | CHAN_AGENT, |
bc240b21 |
531 | CHAN_SOCKDATA, |
532 | CHAN_SOCKDATA_DORMANT /* one the remote hasn't confirmed */ |
783415f8 |
533 | }; |
534 | |
dacbd0e8 |
535 | /* |
0e443c99 |
536 | * little structure to keep track of outstanding WINDOW_ADJUSTs |
537 | */ |
538 | struct winadj { |
539 | struct winadj *next; |
540 | unsigned size; |
541 | }; |
542 | |
543 | /* |
dacbd0e8 |
544 | * 2-3-4 tree storing channels. |
545 | */ |
546 | struct ssh_channel { |
51470298 |
547 | Ssh ssh; /* pointer back to main context */ |
d211621f |
548 | unsigned remoteid, localid; |
dacbd0e8 |
549 | int type; |
64d6ff88 |
550 | /* True if we opened this channel but server hasn't confirmed. */ |
551 | int halfopen; |
0357890f |
552 | /* |
2e85c969 |
553 | * In SSH-1, this value contains four bits: |
0357890f |
554 | * |
555 | * 1 We have sent SSH1_MSG_CHANNEL_CLOSE. |
556 | * 2 We have sent SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
557 | * 4 We have received SSH1_MSG_CHANNEL_CLOSE. |
558 | * 8 We have received SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
559 | * |
560 | * A channel is completely finished with when all four bits are set. |
561 | */ |
dacbd0e8 |
562 | int closes; |
5471d09a |
563 | union { |
564 | struct ssh1_data_channel { |
565 | int throttling; |
566 | } v1; |
567 | struct ssh2_data_channel { |
568 | bufchain outbuffer; |
569 | unsigned remwindow, remmaxpkt; |
467e064d |
570 | /* locwindow is signed so we can cope with excess data. */ |
9b53e9b8 |
571 | int locwindow, locmaxwin; |
0e443c99 |
572 | /* |
573 | * remlocwin is the amount of local window that we think |
574 | * the remote end had available to it after it sent the |
575 | * last data packet or window adjust ack. |
576 | */ |
577 | int remlocwin; |
578 | /* |
579 | * These store the list of window adjusts that haven't |
580 | * been acked. |
581 | */ |
582 | struct winadj *winadj_head, *winadj_tail; |
583 | enum { THROTTLED, UNTHROTTLING, UNTHROTTLED } throttle_state; |
5471d09a |
584 | } v2; |
585 | } v; |
dacbd0e8 |
586 | union { |
32874aea |
587 | struct ssh_agent_channel { |
588 | unsigned char *message; |
589 | unsigned char msglen[4]; |
a03c9f9c |
590 | unsigned lensofar, totallen; |
32874aea |
591 | } a; |
592 | struct ssh_x11_channel { |
593 | Socket s; |
594 | } x11; |
d74d141c |
595 | struct ssh_pfd_channel { |
596 | Socket s; |
597 | } pfd; |
dacbd0e8 |
598 | } u; |
599 | }; |
57476f6b |
600 | |
d74d141c |
601 | /* |
2e85c969 |
602 | * 2-3-4 tree storing remote->local port forwardings. SSH-1 and SSH-2 |
603 | * use this structure in different ways, reflecting SSH-2's |
bc240b21 |
604 | * altogether saner approach to port forwarding. |
605 | * |
2e85c969 |
606 | * In SSH-1, you arrange a remote forwarding by sending the server |
bc240b21 |
607 | * the remote port number, and the local destination host:port. |
608 | * When a connection comes in, the server sends you back that |
609 | * host:port pair, and you connect to it. This is a ready-made |
610 | * security hole if you're not on the ball: a malicious server |
611 | * could send you back _any_ host:port pair, so if you trustingly |
612 | * connect to the address it gives you then you've just opened the |
613 | * entire inside of your corporate network just by connecting |
614 | * through it to a dodgy SSH server. Hence, we must store a list of |
615 | * host:port pairs we _are_ trying to forward to, and reject a |
616 | * connection request from the server if it's not in the list. |
617 | * |
2e85c969 |
618 | * In SSH-2, each side of the connection minds its own business and |
bc240b21 |
619 | * doesn't send unnecessary information to the other. You arrange a |
620 | * remote forwarding by sending the server just the remote port |
621 | * number. When a connection comes in, the server tells you which |
622 | * of its ports was connected to; and _you_ have to remember what |
623 | * local host:port pair went with that port number. |
624 | * |
2e85c969 |
625 | * Hence, in SSH-1 this structure is indexed by destination |
626 | * host:port pair, whereas in SSH-2 it is indexed by source port. |
d74d141c |
627 | */ |
fda2feb1 |
628 | struct ssh_portfwd; /* forward declaration */ |
629 | |
d74d141c |
630 | struct ssh_rportfwd { |
bc240b21 |
631 | unsigned sport, dport; |
632 | char dhost[256]; |
06fadff5 |
633 | char *sportdesc; |
fda2feb1 |
634 | struct ssh_portfwd *pfrec; |
d74d141c |
635 | }; |
fda2feb1 |
636 | #define free_rportfwd(pf) ( \ |
637 | ((pf) ? (sfree((pf)->sportdesc)) : (void)0 ), sfree(pf) ) |
638 | |
639 | /* |
640 | * Separately to the rportfwd tree (which is for looking up port |
641 | * open requests from the server), a tree of _these_ structures is |
642 | * used to keep track of all the currently open port forwardings, |
643 | * so that we can reconfigure in mid-session if the user requests |
644 | * it. |
645 | */ |
646 | struct ssh_portfwd { |
84328ddb |
647 | enum { DESTROY, KEEP, CREATE } status; |
fda2feb1 |
648 | int type; |
649 | unsigned sport, dport; |
650 | char *saddr, *daddr; |
3fe92132 |
651 | char *sserv, *dserv; |
fda2feb1 |
652 | struct ssh_rportfwd *remote; |
05581745 |
653 | int addressfamily; |
fda2feb1 |
654 | void *local; |
655 | }; |
656 | #define free_portfwd(pf) ( \ |
3fe92132 |
657 | ((pf) ? (sfree((pf)->saddr), sfree((pf)->daddr), \ |
658 | sfree((pf)->sserv), sfree((pf)->dserv)) : (void)0 ), sfree(pf) ) |
d74d141c |
659 | |
57476f6b |
660 | struct Packet { |
dacd8872 |
661 | long length; /* length of `data' actually used */ |
662 | long forcepad; /* SSH-2: force padding to at least this length */ |
663 | int type; /* only used for incoming packets */ |
664 | unsigned long sequence; /* SSH-2 incoming sequence number */ |
665 | unsigned char *data; /* allocated storage */ |
666 | unsigned char *body; /* offset of payload within `data' */ |
667 | long savedpos; /* temporary index into `data' (for strings) */ |
668 | long maxlen; /* amount of storage allocated for `data' */ |
669 | long encrypted_len; /* for SSH-2 total-size counting */ |
ff3187f6 |
670 | |
671 | /* |
672 | * State associated with packet logging |
673 | */ |
674 | int logmode; |
675 | int nblanks; |
676 | struct logblank_t *blanks; |
57476f6b |
677 | }; |
678 | |
1c1a7262 |
679 | static void ssh1_protocol(Ssh ssh, void *vin, int inlen, |
ff3187f6 |
680 | struct Packet *pktin); |
1c1a7262 |
681 | static void ssh2_protocol(Ssh ssh, void *vin, int inlen, |
ff3187f6 |
682 | struct Packet *pktin); |
b09eaa88 |
683 | static void ssh1_protocol_setup(Ssh ssh); |
684 | static void ssh2_protocol_setup(Ssh ssh); |
51470298 |
685 | static void ssh_size(void *handle, int width, int height); |
686 | static void ssh_special(void *handle, Telnet_Special); |
5471d09a |
687 | static int ssh2_try_send(struct ssh_channel *c); |
51470298 |
688 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len); |
689 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize); |
9fca3f8c |
690 | static void ssh2_set_window(struct ssh_channel *c, int newwin); |
51470298 |
691 | static int ssh_sendbuffer(void *handle); |
ac934965 |
692 | static int ssh_do_close(Ssh ssh, int notify_exit); |
ff3187f6 |
693 | static unsigned long ssh_pkt_getuint32(struct Packet *pkt); |
694 | static int ssh2_pkt_getbool(struct Packet *pkt); |
695 | static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length); |
9442dd57 |
696 | static void ssh2_timer(void *ctx, long now); |
1c1a7262 |
697 | static int do_ssh2_transport(Ssh ssh, void *vin, int inlen, |
9442dd57 |
698 | struct Packet *pktin); |
57476f6b |
699 | |
51470298 |
700 | struct rdpkt1_state_tag { |
57476f6b |
701 | long len, pad, biglen, to_read; |
702 | unsigned long realcrc, gotcrc; |
703 | unsigned char *p; |
704 | int i; |
705 | int chunk; |
ff3187f6 |
706 | struct Packet *pktin; |
51470298 |
707 | }; |
57476f6b |
708 | |
51470298 |
709 | struct rdpkt2_state_tag { |
960e736a |
710 | long len, pad, payload, packetlen, maclen; |
711 | int i; |
712 | int cipherblk; |
713 | unsigned long incoming_sequence; |
ff3187f6 |
714 | struct Packet *pktin; |
51470298 |
715 | }; |
716 | |
b09eaa88 |
717 | typedef void (*handler_fn_t)(Ssh ssh, struct Packet *pktin); |
06fadff5 |
718 | typedef void (*chandler_fn_t)(Ssh ssh, struct Packet *pktin, void *ctx); |
719 | |
720 | struct queued_handler; |
721 | struct queued_handler { |
722 | int msg1, msg2; |
723 | chandler_fn_t handler; |
724 | void *ctx; |
725 | struct queued_handler *next; |
726 | }; |
b09eaa88 |
727 | |
51470298 |
728 | struct ssh_tag { |
729 | const struct plug_function_table *fn; |
730 | /* the above field _must_ be first in the structure */ |
731 | |
4320baf7 |
732 | char *v_c, *v_s; |
b672f405 |
733 | void *exhash; |
51470298 |
734 | |
735 | Socket s; |
736 | |
b9d7bcad |
737 | void *ldisc; |
a8327734 |
738 | void *logctx; |
b9d7bcad |
739 | |
51470298 |
740 | unsigned char session_key[32]; |
741 | int v1_compressing; |
742 | int v1_remote_protoflags; |
743 | int v1_local_protoflags; |
744 | int agentfwd_enabled; |
745 | int X11_fwd_enabled; |
746 | int remote_bugs; |
747 | const struct ssh_cipher *cipher; |
371e569c |
748 | void *v1_cipher_ctx; |
0183b242 |
749 | void *crcda_ctx; |
51470298 |
750 | const struct ssh2_cipher *cscipher, *sccipher; |
371e569c |
751 | void *cs_cipher_ctx, *sc_cipher_ctx; |
51470298 |
752 | const struct ssh_mac *csmac, *scmac; |
e0e1a00d |
753 | void *cs_mac_ctx, *sc_mac_ctx; |
51470298 |
754 | const struct ssh_compress *cscomp, *sccomp; |
5366aed8 |
755 | void *cs_comp_ctx, *sc_comp_ctx; |
51470298 |
756 | const struct ssh_kex *kex; |
757 | const struct ssh_signkey *hostkey; |
754c0df9 |
758 | unsigned char v2_session_id[SSH2_KEX_MAX_HASH_LEN]; |
b672f405 |
759 | int v2_session_id_len; |
27cd7fc2 |
760 | void *kex_ctx; |
51470298 |
761 | |
762 | char *savedhost; |
763 | int savedport; |
764 | int send_ok; |
765 | int echoing, editing; |
766 | |
767 | void *frontend; |
768 | |
db219738 |
769 | int ospeed, ispeed; /* temporaries */ |
51470298 |
770 | int term_width, term_height; |
771 | |
772 | tree234 *channels; /* indexed by local id */ |
773 | struct ssh_channel *mainchan; /* primary session channel */ |
feb02b4e |
774 | int ncmode; /* is primary channel direct-tcpip? */ |
51470298 |
775 | int exitcode; |
ac934965 |
776 | int close_expected; |
9e296bfa |
777 | int clean_exit; |
51470298 |
778 | |
fda2feb1 |
779 | tree234 *rportfwds, *portfwds; |
51470298 |
780 | |
781 | enum { |
782 | SSH_STATE_PREPACKET, |
783 | SSH_STATE_BEFORE_SIZE, |
784 | SSH_STATE_INTERMED, |
785 | SSH_STATE_SESSION, |
786 | SSH_STATE_CLOSED |
787 | } state; |
788 | |
789 | int size_needed, eof_needed; |
790 | |
590f6a5f |
791 | struct Packet **queue; |
792 | int queuelen, queuesize; |
793 | int queueing; |
51470298 |
794 | unsigned char *deferred_send_data; |
795 | int deferred_len, deferred_size; |
796 | |
797 | /* |
798 | * Gross hack: pscp will try to start SFTP but fall back to |
799 | * scp1 if that fails. This variable is the means by which |
800 | * scp.c can reach into the SSH code and find out which one it |
801 | * got. |
802 | */ |
803 | int fallback_cmd; |
804 | |
6bbce591 |
805 | bufchain banner; /* accumulates banners during do_ssh2_authconn */ |
51470298 |
806 | |
acab36bc |
807 | Pkt_KCtx pkt_kctx; |
808 | Pkt_ACtx pkt_actx; |
51470298 |
809 | |
302121de |
810 | void *x11auth; |
811 | |
51470298 |
812 | int version; |
813 | int v1_throttle_count; |
814 | int overall_bufsize; |
815 | int throttled_all; |
816 | int v1_stdout_throttling; |
a8756193 |
817 | unsigned long v2_outgoing_sequence; |
51470298 |
818 | |
819 | int ssh1_rdpkt_crstate; |
820 | int ssh2_rdpkt_crstate; |
821 | int do_ssh_init_crstate; |
822 | int ssh_gotdata_crstate; |
51470298 |
823 | int do_ssh1_login_crstate; |
b09eaa88 |
824 | int do_ssh1_connection_crstate; |
51470298 |
825 | int do_ssh2_transport_crstate; |
826 | int do_ssh2_authconn_crstate; |
827 | |
828 | void *do_ssh_init_state; |
829 | void *do_ssh1_login_state; |
830 | void *do_ssh2_transport_state; |
831 | void *do_ssh2_authconn_state; |
832 | |
833 | struct rdpkt1_state_tag rdpkt1_state; |
834 | struct rdpkt2_state_tag rdpkt2_state; |
835 | |
2e85c969 |
836 | /* SSH-1 and SSH-2 use this for different things, but both use it */ |
b09eaa88 |
837 | int protocol_initial_phase_done; |
838 | |
1c1a7262 |
839 | void (*protocol) (Ssh ssh, void *vin, int inlen, |
ff3187f6 |
840 | struct Packet *pkt); |
841 | struct Packet *(*s_rdpkt) (Ssh ssh, unsigned char **data, int *datalen); |
86916870 |
842 | |
843 | /* |
844 | * We maintain a full _copy_ of a Config structure here, not |
845 | * merely a pointer to it. That way, when we're passed a new |
846 | * one for reconfiguration, we can check the differences and |
847 | * potentially reconfigure port forwardings etc in mid-session. |
848 | */ |
849 | Config cfg; |
839f10db |
850 | |
851 | /* |
3d9449a1 |
852 | * Used to transfer data back from async callbacks. |
839f10db |
853 | */ |
854 | void *agent_response; |
855 | int agent_response_len; |
3d9449a1 |
856 | int user_response; |
857 | |
858 | /* |
859 | * The SSH connection can be set as `frozen', meaning we are |
860 | * not currently accepting incoming data from the network. This |
861 | * is slightly more serious than setting the _socket_ as |
862 | * frozen, because we may already have had data passed to us |
863 | * from the network which we need to delay processing until |
864 | * after the freeze is lifted, so we also need a bufchain to |
865 | * store that data. |
866 | */ |
867 | int frozen; |
868 | bufchain queued_incoming_data; |
b09eaa88 |
869 | |
870 | /* |
871 | * Dispatch table for packet types that we may have to deal |
872 | * with at any time. |
873 | */ |
874 | handler_fn_t packet_dispatch[256]; |
39934deb |
875 | |
876 | /* |
06fadff5 |
877 | * Queues of one-off handler functions for success/failure |
878 | * indications from a request. |
879 | */ |
880 | struct queued_handler *qhead, *qtail; |
881 | |
882 | /* |
39934deb |
883 | * This module deals with sending keepalives. |
884 | */ |
885 | Pinger pinger; |
9442dd57 |
886 | |
887 | /* |
888 | * Track incoming and outgoing data sizes and time, for |
889 | * size-based rekeys. |
890 | */ |
891 | unsigned long incoming_data_size, outgoing_data_size, deferred_data_size; |
d57f70af |
892 | unsigned long max_data_size; |
9442dd57 |
893 | int kex_in_progress; |
e6c1536e |
894 | long next_rekey, last_rekey; |
e13bba36 |
895 | char *deferred_rekey_reason; /* points to STATIC string; don't free */ |
51470298 |
896 | }; |
960e736a |
897 | |
382908ad |
898 | #define logevent(s) logevent(ssh->frontend, s) |
a8327734 |
899 | |
900 | /* logevent, only printf-formatted. */ |
cbe2d68f |
901 | static void logeventf(Ssh ssh, const char *fmt, ...) |
a8327734 |
902 | { |
903 | va_list ap; |
57356d63 |
904 | char *buf; |
a8327734 |
905 | |
906 | va_start(ap, fmt); |
57356d63 |
907 | buf = dupvprintf(fmt, ap); |
a8327734 |
908 | va_end(ap); |
57356d63 |
909 | logevent(buf); |
57356d63 |
910 | sfree(buf); |
a8327734 |
911 | } |
912 | |
6b5cf8b4 |
913 | #define bombout(msg) \ |
914 | do { \ |
915 | char *text = dupprintf msg; \ |
ac934965 |
916 | ssh_do_close(ssh, FALSE); \ |
6b5cf8b4 |
917 | logevent(text); \ |
918 | connection_fatal(ssh->frontend, "%s", text); \ |
919 | sfree(text); \ |
920 | } while (0) |
a8327734 |
921 | |
9a10ecf4 |
922 | /* Functions to leave bits out of the SSH packet log file. */ |
923 | |
ff3187f6 |
924 | static void dont_log_password(Ssh ssh, struct Packet *pkt, int blanktype) |
9a10ecf4 |
925 | { |
926 | if (ssh->cfg.logomitpass) |
ff3187f6 |
927 | pkt->logmode = blanktype; |
9a10ecf4 |
928 | } |
929 | |
ff3187f6 |
930 | static void dont_log_data(Ssh ssh, struct Packet *pkt, int blanktype) |
9a10ecf4 |
931 | { |
932 | if (ssh->cfg.logomitdata) |
ff3187f6 |
933 | pkt->logmode = blanktype; |
9a10ecf4 |
934 | } |
935 | |
ff3187f6 |
936 | static void end_log_omission(Ssh ssh, struct Packet *pkt) |
9a10ecf4 |
937 | { |
ff3187f6 |
938 | pkt->logmode = PKTLOG_EMIT; |
9a10ecf4 |
939 | } |
940 | |
c6ccd5c2 |
941 | /* Helper function for common bits of parsing cfg.ttymodes. */ |
942 | static void parse_ttymodes(Ssh ssh, char *modes, |
943 | void (*do_mode)(void *data, char *mode, char *val), |
944 | void *data) |
945 | { |
946 | while (*modes) { |
947 | char *t = strchr(modes, '\t'); |
948 | char *m = snewn(t-modes+1, char); |
949 | char *val; |
950 | strncpy(m, modes, t-modes); |
951 | m[t-modes] = '\0'; |
952 | if (*(t+1) == 'A') |
953 | val = get_ttymode(ssh->frontend, m); |
954 | else |
955 | val = dupstr(t+2); |
956 | if (val) |
957 | do_mode(data, m, val); |
958 | sfree(m); |
959 | sfree(val); |
960 | modes += strlen(modes) + 1; |
961 | } |
962 | } |
963 | |
32874aea |
964 | static int ssh_channelcmp(void *av, void *bv) |
965 | { |
966 | struct ssh_channel *a = (struct ssh_channel *) av; |
967 | struct ssh_channel *b = (struct ssh_channel *) bv; |
968 | if (a->localid < b->localid) |
969 | return -1; |
970 | if (a->localid > b->localid) |
971 | return +1; |
dacbd0e8 |
972 | return 0; |
973 | } |
32874aea |
974 | static int ssh_channelfind(void *av, void *bv) |
975 | { |
976 | unsigned *a = (unsigned *) av; |
977 | struct ssh_channel *b = (struct ssh_channel *) bv; |
978 | if (*a < b->localid) |
979 | return -1; |
980 | if (*a > b->localid) |
981 | return +1; |
dacbd0e8 |
982 | return 0; |
983 | } |
984 | |
bc240b21 |
985 | static int ssh_rportcmp_ssh1(void *av, void *bv) |
d74d141c |
986 | { |
987 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
988 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
989 | int i; |
bc240b21 |
990 | if ( (i = strcmp(a->dhost, b->dhost)) != 0) |
d74d141c |
991 | return i < 0 ? -1 : +1; |
bc240b21 |
992 | if (a->dport > b->dport) |
993 | return +1; |
994 | if (a->dport < b->dport) |
995 | return -1; |
996 | return 0; |
997 | } |
998 | |
999 | static int ssh_rportcmp_ssh2(void *av, void *bv) |
1000 | { |
1001 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
1002 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
cdcbdf3b |
1003 | |
bc240b21 |
1004 | if (a->sport > b->sport) |
d74d141c |
1005 | return +1; |
bc240b21 |
1006 | if (a->sport < b->sport) |
1007 | return -1; |
d74d141c |
1008 | return 0; |
1009 | } |
1010 | |
fda2feb1 |
1011 | /* |
1012 | * Special form of strcmp which can cope with NULL inputs. NULL is |
1013 | * defined to sort before even the empty string. |
1014 | */ |
1015 | static int nullstrcmp(const char *a, const char *b) |
1016 | { |
1017 | if (a == NULL && b == NULL) |
1018 | return 0; |
1019 | if (a == NULL) |
1020 | return -1; |
1021 | if (b == NULL) |
1022 | return +1; |
1023 | return strcmp(a, b); |
1024 | } |
1025 | |
1026 | static int ssh_portcmp(void *av, void *bv) |
1027 | { |
1028 | struct ssh_portfwd *a = (struct ssh_portfwd *) av; |
1029 | struct ssh_portfwd *b = (struct ssh_portfwd *) bv; |
1030 | int i; |
1031 | if (a->type > b->type) |
1032 | return +1; |
1033 | if (a->type < b->type) |
1034 | return -1; |
84328ddb |
1035 | if (a->addressfamily > b->addressfamily) |
1036 | return +1; |
1037 | if (a->addressfamily < b->addressfamily) |
1038 | return -1; |
fda2feb1 |
1039 | if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0) |
1040 | return i < 0 ? -1 : +1; |
1041 | if (a->sport > b->sport) |
1042 | return +1; |
1043 | if (a->sport < b->sport) |
1044 | return -1; |
1045 | if (a->type != 'D') { |
1046 | if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0) |
1047 | return i < 0 ? -1 : +1; |
1048 | if (a->dport > b->dport) |
1049 | return +1; |
1050 | if (a->dport < b->dport) |
1051 | return -1; |
1052 | } |
1053 | return 0; |
1054 | } |
1055 | |
51470298 |
1056 | static int alloc_channel_id(Ssh ssh) |
32874aea |
1057 | { |
260f3dec |
1058 | const unsigned CHANNEL_NUMBER_OFFSET = 256; |
1059 | unsigned low, high, mid; |
d2371c81 |
1060 | int tsize; |
1061 | struct ssh_channel *c; |
1062 | |
1063 | /* |
1064 | * First-fit allocation of channel numbers: always pick the |
1065 | * lowest unused one. To do this, binary-search using the |
1066 | * counted B-tree to find the largest channel ID which is in a |
1067 | * contiguous sequence from the beginning. (Precisely |
1068 | * everything in that sequence must have ID equal to its tree |
1069 | * index plus CHANNEL_NUMBER_OFFSET.) |
1070 | */ |
51470298 |
1071 | tsize = count234(ssh->channels); |
d2371c81 |
1072 | |
32874aea |
1073 | low = -1; |
1074 | high = tsize; |
d2371c81 |
1075 | while (high - low > 1) { |
1076 | mid = (high + low) / 2; |
51470298 |
1077 | c = index234(ssh->channels, mid); |
d2371c81 |
1078 | if (c->localid == mid + CHANNEL_NUMBER_OFFSET) |
1079 | low = mid; /* this one is fine */ |
1080 | else |
1081 | high = mid; /* this one is past it */ |
1082 | } |
1083 | /* |
1084 | * Now low points to either -1, or the tree index of the |
1085 | * largest ID in the initial sequence. |
1086 | */ |
1087 | { |
1088 | unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET; |
51470298 |
1089 | assert(NULL == find234(ssh->channels, &i, ssh_channelfind)); |
d2371c81 |
1090 | } |
1091 | return low + 1 + CHANNEL_NUMBER_OFFSET; |
1092 | } |
1093 | |
edd0cb8a |
1094 | static void c_write_stderr(int trusted, const char *buf, int len) |
1095 | { |
1096 | int i; |
1097 | for (i = 0; i < len; i++) |
667deca9 |
1098 | if (buf[i] != '\r' && (trusted || buf[i] == '\n' || (buf[i] & 0x60))) |
edd0cb8a |
1099 | fputc(buf[i], stderr); |
1100 | } |
1101 | |
9fab77dc |
1102 | static void c_write(Ssh ssh, const char *buf, int len) |
32874aea |
1103 | { |
edd0cb8a |
1104 | if (flags & FLAG_STDERR) |
1105 | c_write_stderr(1, buf, len); |
1106 | else |
1107 | from_backend(ssh->frontend, 1, buf, len); |
3bdaf79d |
1108 | } |
1109 | |
9fab77dc |
1110 | static void c_write_untrusted(Ssh ssh, const char *buf, int len) |
32874aea |
1111 | { |
edd0cb8a |
1112 | if (flags & FLAG_STDERR) |
1113 | c_write_stderr(0, buf, len); |
1114 | else |
1115 | from_backend_untrusted(ssh->frontend, buf, len); |
a209e957 |
1116 | } |
1117 | |
9fab77dc |
1118 | static void c_write_str(Ssh ssh, const char *buf) |
32874aea |
1119 | { |
51470298 |
1120 | c_write(ssh, buf, strlen(buf)); |
1408a877 |
1121 | } |
1122 | |
ff3187f6 |
1123 | static void ssh_free_packet(struct Packet *pkt) |
1124 | { |
1125 | sfree(pkt->data); |
1126 | sfree(pkt); |
1127 | } |
1128 | static struct Packet *ssh_new_packet(void) |
1129 | { |
1130 | struct Packet *pkt = snew(struct Packet); |
1131 | |
dacd8872 |
1132 | pkt->body = pkt->data = NULL; |
ff3187f6 |
1133 | pkt->maxlen = 0; |
1134 | pkt->logmode = PKTLOG_EMIT; |
1135 | pkt->nblanks = 0; |
1136 | pkt->blanks = NULL; |
1137 | |
1138 | return pkt; |
1139 | } |
1140 | |
fb09bf1c |
1141 | /* |
1142 | * Collect incoming data in the incoming packet buffer. |
e5574168 |
1143 | * Decipher and verify the packet when it is completely read. |
1144 | * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets. |
fb09bf1c |
1145 | * Update the *data and *datalen variables. |
ff3187f6 |
1146 | * Return a Packet structure when a packet is completed. |
fb09bf1c |
1147 | */ |
ff3187f6 |
1148 | static struct Packet *ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
fb09bf1c |
1149 | { |
51470298 |
1150 | struct rdpkt1_state_tag *st = &ssh->rdpkt1_state; |
374330e2 |
1151 | |
51470298 |
1152 | crBegin(ssh->ssh1_rdpkt_crstate); |
374330e2 |
1153 | |
ff3187f6 |
1154 | st->pktin = ssh_new_packet(); |
1155 | |
1156 | st->pktin->type = 0; |
1157 | st->pktin->length = 0; |
374330e2 |
1158 | |
57476f6b |
1159 | for (st->i = st->len = 0; st->i < 4; st->i++) { |
fb09bf1c |
1160 | while ((*datalen) == 0) |
ff3187f6 |
1161 | crReturn(NULL); |
57476f6b |
1162 | st->len = (st->len << 8) + **data; |
fb09bf1c |
1163 | (*data)++, (*datalen)--; |
1164 | } |
374330e2 |
1165 | |
57476f6b |
1166 | st->pad = 8 - (st->len % 8); |
1167 | st->biglen = st->len + st->pad; |
ff3187f6 |
1168 | st->pktin->length = st->len - 5; |
fb09bf1c |
1169 | |
ae0500e5 |
1170 | if (st->biglen < 0) { |
1171 | bombout(("Extremely large packet length from server suggests" |
1172 | " data stream corruption")); |
ff3187f6 |
1173 | ssh_free_packet(st->pktin); |
1174 | crStop(NULL); |
ae0500e5 |
1175 | } |
1176 | |
ff3187f6 |
1177 | st->pktin->maxlen = st->biglen; |
1178 | st->pktin->data = snewn(st->biglen + APIEXTRA, unsigned char); |
374330e2 |
1179 | |
57476f6b |
1180 | st->to_read = st->biglen; |
ff3187f6 |
1181 | st->p = st->pktin->data; |
57476f6b |
1182 | while (st->to_read > 0) { |
32874aea |
1183 | st->chunk = st->to_read; |
fb09bf1c |
1184 | while ((*datalen) == 0) |
ff3187f6 |
1185 | crReturn(NULL); |
57476f6b |
1186 | if (st->chunk > (*datalen)) |
1187 | st->chunk = (*datalen); |
1188 | memcpy(st->p, *data, st->chunk); |
1189 | *data += st->chunk; |
1190 | *datalen -= st->chunk; |
1191 | st->p += st->chunk; |
1192 | st->to_read -= st->chunk; |
fb09bf1c |
1193 | } |
374330e2 |
1194 | |
ff3187f6 |
1195 | if (ssh->cipher && detect_attack(ssh->crcda_ctx, st->pktin->data, |
0183b242 |
1196 | st->biglen, NULL)) { |
6b5cf8b4 |
1197 | bombout(("Network attack (CRC compensation) detected!")); |
ff3187f6 |
1198 | ssh_free_packet(st->pktin); |
1199 | crStop(NULL); |
9a3a93a5 |
1200 | } |
1201 | |
51470298 |
1202 | if (ssh->cipher) |
ff3187f6 |
1203 | ssh->cipher->decrypt(ssh->v1_cipher_ctx, st->pktin->data, st->biglen); |
374330e2 |
1204 | |
ff3187f6 |
1205 | st->realcrc = crc32_compute(st->pktin->data, st->biglen - 4); |
1206 | st->gotcrc = GET_32BIT(st->pktin->data + st->biglen - 4); |
57476f6b |
1207 | if (st->gotcrc != st->realcrc) { |
6b5cf8b4 |
1208 | bombout(("Incorrect CRC received on packet")); |
ff3187f6 |
1209 | ssh_free_packet(st->pktin); |
1210 | crStop(NULL); |
fb09bf1c |
1211 | } |
572f871e |
1212 | |
ff3187f6 |
1213 | st->pktin->body = st->pktin->data + st->pad + 1; |
1214 | st->pktin->savedpos = 0; |
4ba9b64b |
1215 | |
51470298 |
1216 | if (ssh->v1_compressing) { |
4ba9b64b |
1217 | unsigned char *decompblk; |
1218 | int decomplen; |
36b8d9bb |
1219 | if (!zlib_decompress_block(ssh->sc_comp_ctx, |
ff3187f6 |
1220 | st->pktin->body - 1, st->pktin->length + 1, |
36b8d9bb |
1221 | &decompblk, &decomplen)) { |
1222 | bombout(("Zlib decompression encountered invalid data")); |
ff3187f6 |
1223 | ssh_free_packet(st->pktin); |
1224 | crStop(NULL); |
36b8d9bb |
1225 | } |
4ba9b64b |
1226 | |
ff3187f6 |
1227 | if (st->pktin->maxlen < st->pad + decomplen) { |
1228 | st->pktin->maxlen = st->pad + decomplen; |
1229 | st->pktin->data = sresize(st->pktin->data, |
1230 | st->pktin->maxlen + APIEXTRA, |
3d88e64d |
1231 | unsigned char); |
ff3187f6 |
1232 | st->pktin->body = st->pktin->data + st->pad + 1; |
4ba9b64b |
1233 | } |
1234 | |
ff3187f6 |
1235 | memcpy(st->pktin->body - 1, decompblk, decomplen); |
dcbde236 |
1236 | sfree(decompblk); |
ff3187f6 |
1237 | st->pktin->length = decomplen - 1; |
4ba9b64b |
1238 | } |
1239 | |
ff3187f6 |
1240 | st->pktin->type = st->pktin->body[-1]; |
00db133f |
1241 | |
9a10ecf4 |
1242 | /* |
1243 | * Log incoming packet, possibly omitting sensitive fields. |
1244 | */ |
1245 | if (ssh->logctx) { |
1246 | int nblanks = 0; |
1247 | struct logblank_t blank; |
1248 | if (ssh->cfg.logomitdata) { |
1249 | int do_blank = FALSE, blank_prefix = 0; |
1250 | /* "Session data" packets - omit the data field */ |
ff3187f6 |
1251 | if ((st->pktin->type == SSH1_SMSG_STDOUT_DATA) || |
1252 | (st->pktin->type == SSH1_SMSG_STDERR_DATA)) { |
9a10ecf4 |
1253 | do_blank = TRUE; blank_prefix = 4; |
a2724fba |
1254 | } else if (st->pktin->type == SSH1_MSG_CHANNEL_DATA) { |
1255 | do_blank = TRUE; blank_prefix = 8; |
9a10ecf4 |
1256 | } |
1257 | if (do_blank) { |
1258 | blank.offset = blank_prefix; |
ff3187f6 |
1259 | blank.len = st->pktin->length; |
9a10ecf4 |
1260 | blank.type = PKTLOG_OMIT; |
1261 | nblanks = 1; |
1262 | } |
1263 | } |
a8327734 |
1264 | log_packet(ssh->logctx, |
ff3187f6 |
1265 | PKT_INCOMING, st->pktin->type, |
1266 | ssh1_pkt_type(st->pktin->type), |
1267 | st->pktin->body, st->pktin->length, |
9a10ecf4 |
1268 | nblanks, &blank); |
1269 | } |
00db133f |
1270 | |
ff3187f6 |
1271 | crFinish(st->pktin); |
fb09bf1c |
1272 | } |
1273 | |
ff3187f6 |
1274 | static struct Packet *ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
e5574168 |
1275 | { |
51470298 |
1276 | struct rdpkt2_state_tag *st = &ssh->rdpkt2_state; |
e5574168 |
1277 | |
51470298 |
1278 | crBegin(ssh->ssh2_rdpkt_crstate); |
e5574168 |
1279 | |
ff3187f6 |
1280 | st->pktin = ssh_new_packet(); |
1281 | |
1282 | st->pktin->type = 0; |
1283 | st->pktin->length = 0; |
51470298 |
1284 | if (ssh->sccipher) |
1285 | st->cipherblk = ssh->sccipher->blksize; |
e5574168 |
1286 | else |
32874aea |
1287 | st->cipherblk = 8; |
960e736a |
1288 | if (st->cipherblk < 8) |
32874aea |
1289 | st->cipherblk = 8; |
960e736a |
1290 | |
ff3187f6 |
1291 | st->pktin->data = snewn(st->cipherblk + APIEXTRA, unsigned char); |
e5574168 |
1292 | |
1293 | /* |
1294 | * Acquire and decrypt the first block of the packet. This will |
1295 | * contain the length and padding details. |
1296 | */ |
32874aea |
1297 | for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { |
e5574168 |
1298 | while ((*datalen) == 0) |
ff3187f6 |
1299 | crReturn(NULL); |
1300 | st->pktin->data[st->i] = *(*data)++; |
32874aea |
1301 | (*datalen)--; |
e5574168 |
1302 | } |
4252c9cc |
1303 | |
51470298 |
1304 | if (ssh->sccipher) |
371e569c |
1305 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
ff3187f6 |
1306 | st->pktin->data, st->cipherblk); |
e5574168 |
1307 | |
1308 | /* |
1309 | * Now get the length and padding figures. |
1310 | */ |
ff3187f6 |
1311 | st->len = GET_32BIT(st->pktin->data); |
1312 | st->pad = st->pktin->data[4]; |
e5574168 |
1313 | |
1314 | /* |
717dc483 |
1315 | * _Completely_ silly lengths should be stomped on before they |
1316 | * do us any more damage. |
1317 | */ |
cba1e4b5 |
1318 | if (st->len < 0 || st->len > 35000 || st->pad < 4 || |
1319 | st->len - st->pad < 1 || (st->len + 4) % st->cipherblk != 0) { |
6b5cf8b4 |
1320 | bombout(("Incoming packet was garbled on decryption")); |
ff3187f6 |
1321 | ssh_free_packet(st->pktin); |
1322 | crStop(NULL); |
717dc483 |
1323 | } |
1324 | |
1325 | /* |
e5574168 |
1326 | * This enables us to deduce the payload length. |
1327 | */ |
960e736a |
1328 | st->payload = st->len - st->pad - 1; |
e5574168 |
1329 | |
ff3187f6 |
1330 | st->pktin->length = st->payload + 5; |
e5574168 |
1331 | |
1332 | /* |
1333 | * So now we can work out the total packet length. |
1334 | */ |
960e736a |
1335 | st->packetlen = st->len + 4; |
51470298 |
1336 | st->maclen = ssh->scmac ? ssh->scmac->len : 0; |
e5574168 |
1337 | |
1338 | /* |
ff3187f6 |
1339 | * Allocate memory for the rest of the packet. |
e5574168 |
1340 | */ |
ff3187f6 |
1341 | st->pktin->maxlen = st->packetlen + st->maclen; |
1342 | st->pktin->data = sresize(st->pktin->data, |
1343 | st->pktin->maxlen + APIEXTRA, |
1344 | unsigned char); |
e5574168 |
1345 | |
1346 | /* |
1347 | * Read and decrypt the remainder of the packet. |
1348 | */ |
32874aea |
1349 | for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; |
1350 | st->i++) { |
e5574168 |
1351 | while ((*datalen) == 0) |
ff3187f6 |
1352 | crReturn(NULL); |
1353 | st->pktin->data[st->i] = *(*data)++; |
32874aea |
1354 | (*datalen)--; |
e5574168 |
1355 | } |
1356 | /* Decrypt everything _except_ the MAC. */ |
51470298 |
1357 | if (ssh->sccipher) |
371e569c |
1358 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
ff3187f6 |
1359 | st->pktin->data + st->cipherblk, |
51470298 |
1360 | st->packetlen - st->cipherblk); |
e5574168 |
1361 | |
9442dd57 |
1362 | st->pktin->encrypted_len = st->packetlen; |
1363 | |
e5574168 |
1364 | /* |
1365 | * Check the MAC. |
1366 | */ |
51470298 |
1367 | if (ssh->scmac |
ff3187f6 |
1368 | && !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data, st->len + 4, |
51470298 |
1369 | st->incoming_sequence)) { |
6b5cf8b4 |
1370 | bombout(("Incorrect MAC received on packet")); |
ff3187f6 |
1371 | ssh_free_packet(st->pktin); |
1372 | crStop(NULL); |
8d5de777 |
1373 | } |
b09eaa88 |
1374 | |
1375 | st->pktin->sequence = st->incoming_sequence++; |
e5574168 |
1376 | |
4ba9b64b |
1377 | /* |
1378 | * Decompress packet payload. |
1379 | */ |
1380 | { |
1381 | unsigned char *newpayload; |
1382 | int newlen; |
51470298 |
1383 | if (ssh->sccomp && |
5366aed8 |
1384 | ssh->sccomp->decompress(ssh->sc_comp_ctx, |
ff3187f6 |
1385 | st->pktin->data + 5, st->pktin->length - 5, |
51470298 |
1386 | &newpayload, &newlen)) { |
ff3187f6 |
1387 | if (st->pktin->maxlen < newlen + 5) { |
1388 | st->pktin->maxlen = newlen + 5; |
1389 | st->pktin->data = sresize(st->pktin->data, |
1390 | st->pktin->maxlen + APIEXTRA, |
3d88e64d |
1391 | unsigned char); |
4ba9b64b |
1392 | } |
ff3187f6 |
1393 | st->pktin->length = 5 + newlen; |
1394 | memcpy(st->pktin->data + 5, newpayload, newlen); |
dcbde236 |
1395 | sfree(newpayload); |
4ba9b64b |
1396 | } |
1397 | } |
1398 | |
ff3187f6 |
1399 | st->pktin->savedpos = 6; |
1400 | st->pktin->body = st->pktin->data; |
1401 | st->pktin->type = st->pktin->data[5]; |
e5574168 |
1402 | |
9a10ecf4 |
1403 | /* |
1404 | * Log incoming packet, possibly omitting sensitive fields. |
1405 | */ |
1406 | if (ssh->logctx) { |
1407 | int nblanks = 0; |
1408 | struct logblank_t blank; |
1409 | if (ssh->cfg.logomitdata) { |
1410 | int do_blank = FALSE, blank_prefix = 0; |
1411 | /* "Session data" packets - omit the data field */ |
ff3187f6 |
1412 | if (st->pktin->type == SSH2_MSG_CHANNEL_DATA) { |
9a10ecf4 |
1413 | do_blank = TRUE; blank_prefix = 8; |
a2724fba |
1414 | } else if (st->pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
1415 | do_blank = TRUE; blank_prefix = 12; |
9a10ecf4 |
1416 | } |
1417 | if (do_blank) { |
1418 | blank.offset = blank_prefix; |
ff3187f6 |
1419 | blank.len = (st->pktin->length-6) - blank_prefix; |
9a10ecf4 |
1420 | blank.type = PKTLOG_OMIT; |
1421 | nblanks = 1; |
1422 | } |
1423 | } |
ff3187f6 |
1424 | log_packet(ssh->logctx, PKT_INCOMING, st->pktin->type, |
acab36bc |
1425 | ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, |
1426 | st->pktin->type), |
ff3187f6 |
1427 | st->pktin->data+6, st->pktin->length-6, |
9a10ecf4 |
1428 | nblanks, &blank); |
1429 | } |
00db133f |
1430 | |
ff3187f6 |
1431 | crFinish(st->pktin); |
e5574168 |
1432 | } |
1433 | |
dacd8872 |
1434 | static int s_wrpkt_prepare(Ssh ssh, struct Packet *pkt, int *offset_p) |
32874aea |
1435 | { |
dacd8872 |
1436 | int pad, biglen, i, pktoffs; |
374330e2 |
1437 | unsigned long crc; |
fd7a4aad |
1438 | #ifdef __SC__ |
1439 | /* |
1440 | * XXX various versions of SC (including 8.8.4) screw up the |
1441 | * register allocation in this function and use the same register |
1442 | * (D6) for len and as a temporary, with predictable results. The |
1443 | * following sledgehammer prevents this. |
1444 | */ |
1445 | volatile |
1446 | #endif |
1447 | int len; |
374330e2 |
1448 | |
a8327734 |
1449 | if (ssh->logctx) |
dacd8872 |
1450 | log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[12], |
1451 | ssh1_pkt_type(pkt->data[12]), |
1452 | pkt->body, pkt->length - (pkt->body - pkt->data), |
ff3187f6 |
1453 | pkt->nblanks, pkt->blanks); |
1454 | sfree(pkt->blanks); pkt->blanks = NULL; |
1455 | pkt->nblanks = 0; |
00db133f |
1456 | |
51470298 |
1457 | if (ssh->v1_compressing) { |
4ba9b64b |
1458 | unsigned char *compblk; |
1459 | int complen; |
5366aed8 |
1460 | zlib_compress_block(ssh->cs_comp_ctx, |
dacd8872 |
1461 | pkt->data + 12, pkt->length - 12, |
4ba9b64b |
1462 | &compblk, &complen); |
48406e6b |
1463 | ssh_pkt_ensure(pkt, complen + 2); /* just in case it's got bigger */ |
dacd8872 |
1464 | memcpy(pkt->data + 12, compblk, complen); |
dcbde236 |
1465 | sfree(compblk); |
dacd8872 |
1466 | pkt->length = complen + 12; |
4ba9b64b |
1467 | } |
1468 | |
dacd8872 |
1469 | ssh_pkt_ensure(pkt, pkt->length + 4); /* space for CRC */ |
1470 | pkt->length += 4; |
1471 | len = pkt->length - 4 - 8; /* len(type+data+CRC) */ |
32874aea |
1472 | pad = 8 - (len % 8); |
dacd8872 |
1473 | pktoffs = 8 - pad; |
1474 | biglen = len + pad; /* len(padding+type+data+CRC) */ |
374330e2 |
1475 | |
dacd8872 |
1476 | for (i = pktoffs; i < 4+8; i++) |
1477 | pkt->data[i] = random_byte(); |
1478 | crc = crc32_compute(pkt->data + pktoffs + 4, biglen - 4); /* all ex len */ |
1479 | PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc); |
1480 | PUT_32BIT(pkt->data + pktoffs, len); |
374330e2 |
1481 | |
51470298 |
1482 | if (ssh->cipher) |
dacd8872 |
1483 | ssh->cipher->encrypt(ssh->v1_cipher_ctx, |
1484 | pkt->data + pktoffs + 4, biglen); |
374330e2 |
1485 | |
dacd8872 |
1486 | if (offset_p) *offset_p = pktoffs; |
1487 | return biglen + 4; /* len(length+padding+type+data+CRC) */ |
39065bed |
1488 | } |
1489 | |
bf8a49a1 |
1490 | static int s_write(Ssh ssh, void *data, int len) |
1491 | { |
ff05dfef |
1492 | if (ssh->logctx) |
1493 | log_packet(ssh->logctx, PKT_OUTGOING, -1, NULL, data, len, 0, NULL); |
bf8a49a1 |
1494 | return sk_write(ssh->s, (char *)data, len); |
1495 | } |
1496 | |
ff3187f6 |
1497 | static void s_wrpkt(Ssh ssh, struct Packet *pkt) |
32874aea |
1498 | { |
dacd8872 |
1499 | int len, backlog, offset; |
1500 | len = s_wrpkt_prepare(ssh, pkt, &offset); |
bf8a49a1 |
1501 | backlog = s_write(ssh, pkt->data + offset, len); |
5471d09a |
1502 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1503 | ssh_throttle_all(ssh, 1, backlog); |
dacd8872 |
1504 | ssh_free_packet(pkt); |
39065bed |
1505 | } |
1506 | |
ff3187f6 |
1507 | static void s_wrpkt_defer(Ssh ssh, struct Packet *pkt) |
32874aea |
1508 | { |
dacd8872 |
1509 | int len, offset; |
1510 | len = s_wrpkt_prepare(ssh, pkt, &offset); |
51470298 |
1511 | if (ssh->deferred_len + len > ssh->deferred_size) { |
1512 | ssh->deferred_size = ssh->deferred_len + len + 128; |
3d88e64d |
1513 | ssh->deferred_send_data = sresize(ssh->deferred_send_data, |
1514 | ssh->deferred_size, |
1515 | unsigned char); |
39065bed |
1516 | } |
dacd8872 |
1517 | memcpy(ssh->deferred_send_data + ssh->deferred_len, |
1518 | pkt->data + offset, len); |
51470298 |
1519 | ssh->deferred_len += len; |
dacd8872 |
1520 | ssh_free_packet(pkt); |
374330e2 |
1521 | } |
1522 | |
fb09bf1c |
1523 | /* |
dacd8872 |
1524 | * Construct a SSH-1 packet with the specified contents. |
1525 | * (This all-at-once interface used to be the only one, but now SSH-1 |
1526 | * packets can also be constructed incrementally.) |
fb09bf1c |
1527 | */ |
dacd8872 |
1528 | static struct Packet *construct_packet(Ssh ssh, int pkttype, va_list ap) |
fb09bf1c |
1529 | { |
dacd8872 |
1530 | int argtype; |
7cca0d81 |
1531 | Bignum bn; |
ff3187f6 |
1532 | struct Packet *pkt; |
fb09bf1c |
1533 | |
dacd8872 |
1534 | pkt = ssh1_pkt_init(pkttype); |
fb09bf1c |
1535 | |
dacd8872 |
1536 | while ((argtype = va_arg(ap, int)) != PKT_END) { |
1537 | unsigned char *argp, argchar; |
8a9977e5 |
1538 | char *sargp; |
dacd8872 |
1539 | unsigned long argint; |
1540 | int arglen; |
fb09bf1c |
1541 | switch (argtype) { |
9a10ecf4 |
1542 | /* Actual fields in the packet */ |
fb09bf1c |
1543 | case PKT_INT: |
dacd8872 |
1544 | argint = va_arg(ap, int); |
1545 | ssh_pkt_adduint32(pkt, argint); |
fb09bf1c |
1546 | break; |
1547 | case PKT_CHAR: |
dacd8872 |
1548 | argchar = (unsigned char) va_arg(ap, int); |
1549 | ssh_pkt_addbyte(pkt, argchar); |
fb09bf1c |
1550 | break; |
1551 | case PKT_DATA: |
dacd8872 |
1552 | argp = va_arg(ap, unsigned char *); |
1553 | arglen = va_arg(ap, int); |
1554 | ssh_pkt_adddata(pkt, argp, arglen); |
fb09bf1c |
1555 | break; |
1556 | case PKT_STR: |
8a9977e5 |
1557 | sargp = va_arg(ap, char *); |
1558 | ssh_pkt_addstring(pkt, sargp); |
fb09bf1c |
1559 | break; |
7cca0d81 |
1560 | case PKT_BIGNUM: |
dacd8872 |
1561 | bn = va_arg(ap, Bignum); |
1562 | ssh1_pkt_addmp(pkt, bn); |
9a10ecf4 |
1563 | break; |
1564 | /* Tokens for modifications to packet logging */ |
1565 | case PKTT_PASSWORD: |
ff3187f6 |
1566 | dont_log_password(ssh, pkt, PKTLOG_BLANK); |
9a10ecf4 |
1567 | break; |
1568 | case PKTT_DATA: |
ff3187f6 |
1569 | dont_log_data(ssh, pkt, PKTLOG_OMIT); |
7cca0d81 |
1570 | break; |
9a10ecf4 |
1571 | case PKTT_OTHER: |
ff3187f6 |
1572 | end_log_omission(ssh, pkt); |
9a10ecf4 |
1573 | break; |
1574 | } |
fb09bf1c |
1575 | } |
ff3187f6 |
1576 | |
1577 | return pkt; |
39065bed |
1578 | } |
fb09bf1c |
1579 | |
51470298 |
1580 | static void send_packet(Ssh ssh, int pkttype, ...) |
32874aea |
1581 | { |
ff3187f6 |
1582 | struct Packet *pkt; |
dacd8872 |
1583 | va_list ap; |
1584 | va_start(ap, pkttype); |
1585 | pkt = construct_packet(ssh, pkttype, ap); |
1586 | va_end(ap); |
ff3187f6 |
1587 | s_wrpkt(ssh, pkt); |
fb09bf1c |
1588 | } |
1589 | |
51470298 |
1590 | static void defer_packet(Ssh ssh, int pkttype, ...) |
32874aea |
1591 | { |
ff3187f6 |
1592 | struct Packet *pkt; |
dacd8872 |
1593 | va_list ap; |
1594 | va_start(ap, pkttype); |
1595 | pkt = construct_packet(ssh, pkttype, ap); |
1596 | va_end(ap); |
ff3187f6 |
1597 | s_wrpkt_defer(ssh, pkt); |
39065bed |
1598 | } |
1599 | |
32874aea |
1600 | static int ssh_versioncmp(char *a, char *b) |
1601 | { |
9697bfd2 |
1602 | char *ae, *be; |
1603 | unsigned long av, bv; |
1604 | |
43aa02a7 |
1605 | av = strtoul(a, &ae, 10); |
1606 | bv = strtoul(b, &be, 10); |
32874aea |
1607 | if (av != bv) |
1608 | return (av < bv ? -1 : +1); |
1609 | if (*ae == '.') |
1610 | ae++; |
1611 | if (*be == '.') |
1612 | be++; |
43aa02a7 |
1613 | av = strtoul(ae, &ae, 10); |
1614 | bv = strtoul(be, &be, 10); |
32874aea |
1615 | if (av != bv) |
1616 | return (av < bv ? -1 : +1); |
9697bfd2 |
1617 | return 0; |
1618 | } |
1619 | |
e5574168 |
1620 | /* |
a92dd380 |
1621 | * Utility routines for putting an SSH-protocol `string' and |
b672f405 |
1622 | * `uint32' into a hash state. |
e5574168 |
1623 | */ |
b672f405 |
1624 | static void hash_string(const struct ssh_hash *h, void *s, void *str, int len) |
32874aea |
1625 | { |
e5574168 |
1626 | unsigned char lenblk[4]; |
e5574168 |
1627 | PUT_32BIT(lenblk, len); |
b672f405 |
1628 | h->bytes(s, lenblk, 4); |
1629 | h->bytes(s, str, len); |
e5574168 |
1630 | } |
1631 | |
b672f405 |
1632 | static void hash_uint32(const struct ssh_hash *h, void *s, unsigned i) |
32874aea |
1633 | { |
a92dd380 |
1634 | unsigned char intblk[4]; |
1635 | PUT_32BIT(intblk, i); |
b672f405 |
1636 | h->bytes(s, intblk, 4); |
a92dd380 |
1637 | } |
1638 | |
7cca0d81 |
1639 | /* |
dacd8872 |
1640 | * Packet construction functions. Mostly shared between SSH-1 and SSH-2. |
7cca0d81 |
1641 | */ |
dacd8872 |
1642 | static void ssh_pkt_ensure(struct Packet *pkt, int length) |
32874aea |
1643 | { |
ff3187f6 |
1644 | if (pkt->maxlen < length) { |
dacd8872 |
1645 | unsigned char *body = pkt->body; |
3d71de0b |
1646 | int offset = body ? body - pkt->data : 0; |
ff3187f6 |
1647 | pkt->maxlen = length + 256; |
1648 | pkt->data = sresize(pkt->data, pkt->maxlen + APIEXTRA, unsigned char); |
dacd8872 |
1649 | if (body) pkt->body = pkt->data + offset; |
7cca0d81 |
1650 | } |
783415f8 |
1651 | } |
dacd8872 |
1652 | static void ssh_pkt_adddata(struct Packet *pkt, void *data, int len) |
32874aea |
1653 | { |
ff3187f6 |
1654 | if (pkt->logmode != PKTLOG_EMIT) { |
1655 | pkt->nblanks++; |
1656 | pkt->blanks = sresize(pkt->blanks, pkt->nblanks, struct logblank_t); |
dacd8872 |
1657 | assert(pkt->body); |
1658 | pkt->blanks[pkt->nblanks-1].offset = pkt->length - |
1659 | (pkt->body - pkt->data); |
ff3187f6 |
1660 | pkt->blanks[pkt->nblanks-1].len = len; |
1661 | pkt->blanks[pkt->nblanks-1].type = pkt->logmode; |
1662 | } |
1663 | pkt->length += len; |
dacd8872 |
1664 | ssh_pkt_ensure(pkt, pkt->length); |
ff3187f6 |
1665 | memcpy(pkt->data + pkt->length - len, data, len); |
7cca0d81 |
1666 | } |
dacd8872 |
1667 | static void ssh_pkt_addbyte(struct Packet *pkt, unsigned char byte) |
32874aea |
1668 | { |
dacd8872 |
1669 | ssh_pkt_adddata(pkt, &byte, 1); |
7cca0d81 |
1670 | } |
ff3187f6 |
1671 | static void ssh2_pkt_addbool(struct Packet *pkt, unsigned char value) |
32874aea |
1672 | { |
dacd8872 |
1673 | ssh_pkt_adddata(pkt, &value, 1); |
7cca0d81 |
1674 | } |
dacd8872 |
1675 | static void ssh_pkt_adduint32(struct Packet *pkt, unsigned long value) |
32874aea |
1676 | { |
7cca0d81 |
1677 | unsigned char x[4]; |
1678 | PUT_32BIT(x, value); |
dacd8872 |
1679 | ssh_pkt_adddata(pkt, x, 4); |
7cca0d81 |
1680 | } |
dacd8872 |
1681 | static void ssh_pkt_addstring_start(struct Packet *pkt) |
32874aea |
1682 | { |
dacd8872 |
1683 | ssh_pkt_adduint32(pkt, 0); |
ff3187f6 |
1684 | pkt->savedpos = pkt->length; |
7cca0d81 |
1685 | } |
dacd8872 |
1686 | static void ssh_pkt_addstring_str(struct Packet *pkt, char *data) |
32874aea |
1687 | { |
dacd8872 |
1688 | ssh_pkt_adddata(pkt, data, strlen(data)); |
ff3187f6 |
1689 | PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos); |
7cca0d81 |
1690 | } |
dacd8872 |
1691 | static void ssh_pkt_addstring_data(struct Packet *pkt, char *data, int len) |
32874aea |
1692 | { |
dacd8872 |
1693 | ssh_pkt_adddata(pkt, data, len); |
ff3187f6 |
1694 | PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos); |
7cca0d81 |
1695 | } |
dacd8872 |
1696 | static void ssh_pkt_addstring(struct Packet *pkt, char *data) |
1697 | { |
1698 | ssh_pkt_addstring_start(pkt); |
1699 | ssh_pkt_addstring_str(pkt, data); |
1700 | } |
1701 | static void ssh1_pkt_addmp(struct Packet *pkt, Bignum b) |
32874aea |
1702 | { |
dacd8872 |
1703 | int len = ssh1_bignum_length(b); |
8a9977e5 |
1704 | unsigned char *data = snewn(len, unsigned char); |
dacd8872 |
1705 | (void) ssh1_write_bignum(data, b); |
1706 | ssh_pkt_adddata(pkt, data, len); |
1707 | sfree(data); |
7cca0d81 |
1708 | } |
d8baa528 |
1709 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len) |
32874aea |
1710 | { |
7cca0d81 |
1711 | unsigned char *p; |
32874aea |
1712 | int i, n = (bignum_bitcount(b) + 7) / 8; |
3d88e64d |
1713 | p = snewn(n + 1, unsigned char); |
7cca0d81 |
1714 | p[0] = 0; |
3709bfe9 |
1715 | for (i = 1; i <= n; i++) |
32874aea |
1716 | p[i] = bignum_byte(b, n - i); |
7cca0d81 |
1717 | i = 0; |
32874aea |
1718 | while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0) |
1719 | i++; |
1720 | memmove(p, p + i, n + 1 - i); |
1721 | *len = n + 1 - i; |
7cca0d81 |
1722 | return p; |
1723 | } |
ff3187f6 |
1724 | static void ssh2_pkt_addmp(struct Packet *pkt, Bignum b) |
32874aea |
1725 | { |
7cca0d81 |
1726 | unsigned char *p; |
1727 | int len; |
1728 | p = ssh2_mpint_fmt(b, &len); |
dacd8872 |
1729 | ssh_pkt_addstring_start(pkt); |
1730 | ssh_pkt_addstring_data(pkt, (char *)p, len); |
dcbde236 |
1731 | sfree(p); |
7cca0d81 |
1732 | } |
b185170a |
1733 | |
dacd8872 |
1734 | static struct Packet *ssh1_pkt_init(int pkt_type) |
1735 | { |
1736 | struct Packet *pkt = ssh_new_packet(); |
1737 | pkt->length = 4 + 8; /* space for length + max padding */ |
1738 | ssh_pkt_addbyte(pkt, pkt_type); |
1739 | pkt->body = pkt->data + pkt->length; |
1740 | return pkt; |
1741 | } |
1742 | |
1743 | /* For legacy code (SSH-1 and -2 packet construction used to be separate) */ |
1744 | #define ssh2_pkt_ensure(pkt, length) ssh_pkt_ensure(pkt, length) |
1745 | #define ssh2_pkt_adddata(pkt, data, len) ssh_pkt_adddata(pkt, data, len) |
1746 | #define ssh2_pkt_addbyte(pkt, byte) ssh_pkt_addbyte(pkt, byte) |
1747 | #define ssh2_pkt_adduint32(pkt, value) ssh_pkt_adduint32(pkt, value) |
1748 | #define ssh2_pkt_addstring_start(pkt) ssh_pkt_addstring_start(pkt) |
1749 | #define ssh2_pkt_addstring_str(pkt, data) ssh_pkt_addstring_str(pkt, data) |
1750 | #define ssh2_pkt_addstring_data(pkt, data, len) ssh_pkt_addstring_data(pkt, data, len) |
1751 | #define ssh2_pkt_addstring(pkt, data) ssh_pkt_addstring(pkt, data) |
1752 | |
1753 | static struct Packet *ssh2_pkt_init(int pkt_type) |
1754 | { |
1755 | struct Packet *pkt = ssh_new_packet(); |
3d71de0b |
1756 | pkt->length = 5; /* space for packet length + padding length */ |
dacd8872 |
1757 | pkt->forcepad = 0; |
1758 | ssh_pkt_addbyte(pkt, (unsigned char) pkt_type); |
3d71de0b |
1759 | pkt->body = pkt->data + pkt->length; /* after packet type */ |
dacd8872 |
1760 | return pkt; |
1761 | } |
1762 | |
b185170a |
1763 | /* |
2e85c969 |
1764 | * Construct an SSH-2 final-form packet: compress it, encrypt it, |
b185170a |
1765 | * put the MAC on it. Final packet, ready to be sent, is stored in |
ff3187f6 |
1766 | * pkt->data. Total length is returned. |
b185170a |
1767 | */ |
ff3187f6 |
1768 | static int ssh2_pkt_construct(Ssh ssh, struct Packet *pkt) |
32874aea |
1769 | { |
7cca0d81 |
1770 | int cipherblk, maclen, padding, i; |
7cca0d81 |
1771 | |
a8327734 |
1772 | if (ssh->logctx) |
ff3187f6 |
1773 | log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[5], |
acab36bc |
1774 | ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->data[5]), |
dacd8872 |
1775 | pkt->body, pkt->length - (pkt->body - pkt->data), |
ff3187f6 |
1776 | pkt->nblanks, pkt->blanks); |
1777 | sfree(pkt->blanks); pkt->blanks = NULL; |
1778 | pkt->nblanks = 0; |
00db133f |
1779 | |
7cca0d81 |
1780 | /* |
4ba9b64b |
1781 | * Compress packet payload. |
1782 | */ |
4ba9b64b |
1783 | { |
1784 | unsigned char *newpayload; |
1785 | int newlen; |
51470298 |
1786 | if (ssh->cscomp && |
ff3187f6 |
1787 | ssh->cscomp->compress(ssh->cs_comp_ctx, pkt->data + 5, |
1788 | pkt->length - 5, |
51470298 |
1789 | &newpayload, &newlen)) { |
ff3187f6 |
1790 | pkt->length = 5; |
1791 | ssh2_pkt_adddata(pkt, newpayload, newlen); |
dcbde236 |
1792 | sfree(newpayload); |
4ba9b64b |
1793 | } |
1794 | } |
1795 | |
1796 | /* |
7cca0d81 |
1797 | * Add padding. At least four bytes, and must also bring total |
1798 | * length (minus MAC) up to a multiple of the block size. |
95d2d262 |
1799 | * If pkt->forcepad is set, make sure the packet is at least that size |
1800 | * after padding. |
7cca0d81 |
1801 | */ |
51470298 |
1802 | cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */ |
32874aea |
1803 | cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ |
7cca0d81 |
1804 | padding = 4; |
95d2d262 |
1805 | if (pkt->length + padding < pkt->forcepad) |
1806 | padding = pkt->forcepad - pkt->length; |
32874aea |
1807 | padding += |
ff3187f6 |
1808 | (cipherblk - (pkt->length + padding) % cipherblk) % cipherblk; |
95d2d262 |
1809 | assert(padding <= 255); |
51470298 |
1810 | maclen = ssh->csmac ? ssh->csmac->len : 0; |
ff3187f6 |
1811 | ssh2_pkt_ensure(pkt, pkt->length + padding + maclen); |
1812 | pkt->data[4] = padding; |
7cca0d81 |
1813 | for (i = 0; i < padding; i++) |
ff3187f6 |
1814 | pkt->data[pkt->length + i] = random_byte(); |
1815 | PUT_32BIT(pkt->data, pkt->length + padding - 4); |
51470298 |
1816 | if (ssh->csmac) |
ff3187f6 |
1817 | ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data, |
1818 | pkt->length + padding, |
51470298 |
1819 | ssh->v2_outgoing_sequence); |
1820 | ssh->v2_outgoing_sequence++; /* whether or not we MACed */ |
1821 | |
1822 | if (ssh->cscipher) |
371e569c |
1823 | ssh->cscipher->encrypt(ssh->cs_cipher_ctx, |
ff3187f6 |
1824 | pkt->data, pkt->length + padding); |
51470298 |
1825 | |
9442dd57 |
1826 | pkt->encrypted_len = pkt->length + padding; |
1827 | |
ff3187f6 |
1828 | /* Ready-to-send packet starts at pkt->data. We return length. */ |
1829 | return pkt->length + padding + maclen; |
b185170a |
1830 | } |
1831 | |
1832 | /* |
590f6a5f |
1833 | * Routines called from the main SSH code to send packets. There |
1834 | * are quite a few of these, because we have two separate |
1835 | * mechanisms for delaying the sending of packets: |
1836 | * |
1837 | * - In order to send an IGNORE message and a password message in |
1838 | * a single fixed-length blob, we require the ability to |
1839 | * concatenate the encrypted forms of those two packets _into_ a |
1840 | * single blob and then pass it to our <network.h> transport |
1841 | * layer in one go. Hence, there's a deferment mechanism which |
1842 | * works after packet encryption. |
1843 | * |
1844 | * - In order to avoid sending any connection-layer messages |
1845 | * during repeat key exchange, we have to queue up any such |
1846 | * outgoing messages _before_ they are encrypted (and in |
1847 | * particular before they're allocated sequence numbers), and |
1848 | * then send them once we've finished. |
1849 | * |
1850 | * I call these mechanisms `defer' and `queue' respectively, so as |
1851 | * to distinguish them reasonably easily. |
1852 | * |
1853 | * The functions send_noqueue() and defer_noqueue() free the packet |
1854 | * structure they are passed. Every outgoing packet goes through |
1855 | * precisely one of these functions in its life; packets passed to |
1856 | * ssh2_pkt_send() or ssh2_pkt_defer() either go straight to one of |
1857 | * these or get queued, and then when the queue is later emptied |
1858 | * the packets are all passed to defer_noqueue(). |
97ab28d7 |
1859 | * |
1860 | * When using a CBC-mode cipher, it's necessary to ensure that an |
1861 | * attacker can't provide data to be encrypted using an IV that they |
1862 | * know. We ensure this by prefixing each packet that might contain |
1863 | * user data with an SSH_MSG_IGNORE. This is done using the deferral |
1864 | * mechanism, so in this case send_noqueue() ends up redirecting to |
1865 | * defer_noqueue(). If you don't like this inefficiency, don't use |
1866 | * CBC. |
b185170a |
1867 | */ |
590f6a5f |
1868 | |
97ab28d7 |
1869 | static void ssh2_pkt_defer_noqueue(Ssh, struct Packet *, int); |
1870 | static void ssh_pkt_defersend(Ssh); |
1871 | |
590f6a5f |
1872 | /* |
2e85c969 |
1873 | * Send an SSH-2 packet immediately, without queuing or deferring. |
590f6a5f |
1874 | */ |
1875 | static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt) |
32874aea |
1876 | { |
5471d09a |
1877 | int len; |
1878 | int backlog; |
97ab28d7 |
1879 | if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC)) { |
1880 | /* We need to send two packets, so use the deferral mechanism. */ |
1881 | ssh2_pkt_defer_noqueue(ssh, pkt, FALSE); |
1882 | ssh_pkt_defersend(ssh); |
1883 | return; |
1884 | } |
ff3187f6 |
1885 | len = ssh2_pkt_construct(ssh, pkt); |
bf8a49a1 |
1886 | backlog = s_write(ssh, pkt->data, len); |
5471d09a |
1887 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1888 | ssh_throttle_all(ssh, 1, backlog); |
9442dd57 |
1889 | |
1890 | ssh->outgoing_data_size += pkt->encrypted_len; |
1891 | if (!ssh->kex_in_progress && |
d57f70af |
1892 | ssh->max_data_size != 0 && |
1893 | ssh->outgoing_data_size > ssh->max_data_size) |
f382c87d |
1894 | do_ssh2_transport(ssh, "too much data sent", -1, NULL); |
9442dd57 |
1895 | |
ff3187f6 |
1896 | ssh_free_packet(pkt); |
b185170a |
1897 | } |
1898 | |
1899 | /* |
2e85c969 |
1900 | * Defer an SSH-2 packet. |
b185170a |
1901 | */ |
97ab28d7 |
1902 | static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt, int noignore) |
32874aea |
1903 | { |
97ab28d7 |
1904 | int len; |
1905 | if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC) && |
1906 | ssh->deferred_len == 0 && !noignore) { |
1907 | /* |
1908 | * Interpose an SSH_MSG_IGNORE to ensure that user data don't |
1909 | * get encrypted with a known IV. |
1910 | */ |
1911 | struct Packet *ipkt = ssh2_pkt_init(SSH2_MSG_IGNORE); |
27311cc7 |
1912 | ssh2_pkt_addstring_start(ipkt); |
97ab28d7 |
1913 | ssh2_pkt_defer_noqueue(ssh, ipkt, TRUE); |
1914 | } |
1915 | len = ssh2_pkt_construct(ssh, pkt); |
51470298 |
1916 | if (ssh->deferred_len + len > ssh->deferred_size) { |
1917 | ssh->deferred_size = ssh->deferred_len + len + 128; |
3d88e64d |
1918 | ssh->deferred_send_data = sresize(ssh->deferred_send_data, |
1919 | ssh->deferred_size, |
1920 | unsigned char); |
b185170a |
1921 | } |
ff3187f6 |
1922 | memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->data, len); |
51470298 |
1923 | ssh->deferred_len += len; |
9442dd57 |
1924 | ssh->deferred_data_size += pkt->encrypted_len; |
ff3187f6 |
1925 | ssh_free_packet(pkt); |
b185170a |
1926 | } |
1927 | |
1928 | /* |
2e85c969 |
1929 | * Queue an SSH-2 packet. |
590f6a5f |
1930 | */ |
1931 | static void ssh2_pkt_queue(Ssh ssh, struct Packet *pkt) |
1932 | { |
1933 | assert(ssh->queueing); |
1934 | |
1935 | if (ssh->queuelen >= ssh->queuesize) { |
1936 | ssh->queuesize = ssh->queuelen + 32; |
1937 | ssh->queue = sresize(ssh->queue, ssh->queuesize, struct Packet *); |
1938 | } |
1939 | |
1940 | ssh->queue[ssh->queuelen++] = pkt; |
1941 | } |
1942 | |
1943 | /* |
1944 | * Either queue or send a packet, depending on whether queueing is |
1945 | * set. |
1946 | */ |
1947 | static void ssh2_pkt_send(Ssh ssh, struct Packet *pkt) |
1948 | { |
1949 | if (ssh->queueing) |
1950 | ssh2_pkt_queue(ssh, pkt); |
1951 | else |
1952 | ssh2_pkt_send_noqueue(ssh, pkt); |
1953 | } |
1954 | |
1955 | /* |
1956 | * Either queue or defer a packet, depending on whether queueing is |
1957 | * set. |
1958 | */ |
1959 | static void ssh2_pkt_defer(Ssh ssh, struct Packet *pkt) |
1960 | { |
1961 | if (ssh->queueing) |
1962 | ssh2_pkt_queue(ssh, pkt); |
1963 | else |
97ab28d7 |
1964 | ssh2_pkt_defer_noqueue(ssh, pkt, FALSE); |
590f6a5f |
1965 | } |
1966 | |
1967 | /* |
b185170a |
1968 | * Send the whole deferred data block constructed by |
2e85c969 |
1969 | * ssh2_pkt_defer() or SSH-1's defer_packet(). |
590f6a5f |
1970 | * |
1971 | * The expected use of the defer mechanism is that you call |
1972 | * ssh2_pkt_defer() a few times, then call ssh_pkt_defersend(). If |
1973 | * not currently queueing, this simply sets up deferred_send_data |
1974 | * and then sends it. If we _are_ currently queueing, the calls to |
1975 | * ssh2_pkt_defer() put the deferred packets on to the queue |
1976 | * instead, and therefore ssh_pkt_defersend() has no deferred data |
1977 | * to send. Hence, there's no need to make it conditional on |
1978 | * ssh->queueing. |
b185170a |
1979 | */ |
51470298 |
1980 | static void ssh_pkt_defersend(Ssh ssh) |
32874aea |
1981 | { |
5471d09a |
1982 | int backlog; |
bf8a49a1 |
1983 | backlog = s_write(ssh, ssh->deferred_send_data, ssh->deferred_len); |
51470298 |
1984 | ssh->deferred_len = ssh->deferred_size = 0; |
1985 | sfree(ssh->deferred_send_data); |
1986 | ssh->deferred_send_data = NULL; |
5471d09a |
1987 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1988 | ssh_throttle_all(ssh, 1, backlog); |
9442dd57 |
1989 | |
1990 | ssh->outgoing_data_size += ssh->deferred_data_size; |
1991 | if (!ssh->kex_in_progress && |
d57f70af |
1992 | ssh->max_data_size != 0 && |
1993 | ssh->outgoing_data_size > ssh->max_data_size) |
f382c87d |
1994 | do_ssh2_transport(ssh, "too much data sent", -1, NULL); |
9442dd57 |
1995 | ssh->deferred_data_size = 0; |
7cca0d81 |
1996 | } |
1997 | |
590f6a5f |
1998 | /* |
18524056 |
1999 | * Send a packet whose length needs to be disguised (typically |
2000 | * passwords or keyboard-interactive responses). |
2001 | */ |
2002 | static void ssh2_pkt_send_with_padding(Ssh ssh, struct Packet *pkt, |
2003 | int padsize) |
2004 | { |
2005 | #if 0 |
2006 | if (0) { |
2007 | /* |
2008 | * The simplest way to do this is to adjust the |
2009 | * variable-length padding field in the outgoing packet. |
2010 | * |
2011 | * Currently compiled out, because some Cisco SSH servers |
2012 | * don't like excessively padded packets (bah, why's it |
2013 | * always Cisco?) |
2014 | */ |
2015 | pkt->forcepad = padsize; |
2016 | ssh2_pkt_send(ssh, pkt); |
2017 | } else |
2018 | #endif |
2019 | { |
2020 | /* |
2021 | * If we can't do that, however, an alternative approach is |
2022 | * to use the pkt_defer mechanism to bundle the packet |
2023 | * tightly together with an SSH_MSG_IGNORE such that their |
2024 | * combined length is a constant. So first we construct the |
2025 | * final form of this packet and defer its sending. |
2026 | */ |
2027 | ssh2_pkt_defer(ssh, pkt); |
2028 | |
2029 | /* |
2030 | * Now construct an SSH_MSG_IGNORE which includes a string |
2031 | * that's an exact multiple of the cipher block size. (If |
2032 | * the cipher is NULL so that the block size is |
2033 | * unavailable, we don't do this trick at all, because we |
2034 | * gain nothing by it.) |
2035 | */ |
2036 | if (ssh->cscipher) { |
2037 | int stringlen, i; |
2038 | |
2039 | stringlen = (256 - ssh->deferred_len); |
2040 | stringlen += ssh->cscipher->blksize - 1; |
2041 | stringlen -= (stringlen % ssh->cscipher->blksize); |
2042 | if (ssh->cscomp) { |
2043 | /* |
2044 | * Temporarily disable actual compression, so we |
2045 | * can guarantee to get this string exactly the |
2046 | * length we want it. The compression-disabling |
2047 | * routine should return an integer indicating how |
2048 | * many bytes we should adjust our string length |
2049 | * by. |
2050 | */ |
2051 | stringlen -= |
2052 | ssh->cscomp->disable_compression(ssh->cs_comp_ctx); |
2053 | } |
2054 | pkt = ssh2_pkt_init(SSH2_MSG_IGNORE); |
2055 | ssh2_pkt_addstring_start(pkt); |
2056 | for (i = 0; i < stringlen; i++) { |
2057 | char c = (char) random_byte(); |
2058 | ssh2_pkt_addstring_data(pkt, &c, 1); |
2059 | } |
2060 | ssh2_pkt_defer(ssh, pkt); |
2061 | } |
2062 | ssh_pkt_defersend(ssh); |
2063 | } |
2064 | } |
2065 | |
2066 | /* |
2e85c969 |
2067 | * Send all queued SSH-2 packets. We send them by means of |
590f6a5f |
2068 | * ssh2_pkt_defer_noqueue(), in case they included a pair of |
2069 | * packets that needed to be lumped together. |
2070 | */ |
2071 | static void ssh2_pkt_queuesend(Ssh ssh) |
2072 | { |
2073 | int i; |
2074 | |
2075 | assert(!ssh->queueing); |
2076 | |
2077 | for (i = 0; i < ssh->queuelen; i++) |
97ab28d7 |
2078 | ssh2_pkt_defer_noqueue(ssh, ssh->queue[i], FALSE); |
590f6a5f |
2079 | ssh->queuelen = 0; |
2080 | |
2081 | ssh_pkt_defersend(ssh); |
2082 | } |
2083 | |
7cca0d81 |
2084 | #if 0 |
32874aea |
2085 | void bndebug(char *string, Bignum b) |
2086 | { |
7cca0d81 |
2087 | unsigned char *p; |
2088 | int i, len; |
2089 | p = ssh2_mpint_fmt(b, &len); |
2090 | debug(("%s", string)); |
2091 | for (i = 0; i < len; i++) |
32874aea |
2092 | debug((" %02x", p[i])); |
765c4200 |
2093 | debug(("\n")); |
dcbde236 |
2094 | sfree(p); |
7cca0d81 |
2095 | } |
2096 | #endif |
2097 | |
b672f405 |
2098 | static void hash_mpint(const struct ssh_hash *h, void *s, Bignum b) |
32874aea |
2099 | { |
7cca0d81 |
2100 | unsigned char *p; |
2101 | int len; |
2102 | p = ssh2_mpint_fmt(b, &len); |
b672f405 |
2103 | hash_string(h, s, p, len); |
dcbde236 |
2104 | sfree(p); |
7cca0d81 |
2105 | } |
2106 | |
2107 | /* |
2e85c969 |
2108 | * Packet decode functions for both SSH-1 and SSH-2. |
7cca0d81 |
2109 | */ |
ff3187f6 |
2110 | static unsigned long ssh_pkt_getuint32(struct Packet *pkt) |
32874aea |
2111 | { |
7cca0d81 |
2112 | unsigned long value; |
ff3187f6 |
2113 | if (pkt->length - pkt->savedpos < 4) |
32874aea |
2114 | return 0; /* arrgh, no way to decline (FIXME?) */ |
ff3187f6 |
2115 | value = GET_32BIT(pkt->body + pkt->savedpos); |
2116 | pkt->savedpos += 4; |
7cca0d81 |
2117 | return value; |
2118 | } |
ff3187f6 |
2119 | static int ssh2_pkt_getbool(struct Packet *pkt) |
32874aea |
2120 | { |
65a22376 |
2121 | unsigned long value; |
ff3187f6 |
2122 | if (pkt->length - pkt->savedpos < 1) |
32874aea |
2123 | return 0; /* arrgh, no way to decline (FIXME?) */ |
ff3187f6 |
2124 | value = pkt->body[pkt->savedpos] != 0; |
2125 | pkt->savedpos++; |
65a22376 |
2126 | return value; |
2127 | } |
ff3187f6 |
2128 | static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length) |
32874aea |
2129 | { |
57356d63 |
2130 | int len; |
7cca0d81 |
2131 | *p = NULL; |
45068b27 |
2132 | *length = 0; |
ff3187f6 |
2133 | if (pkt->length - pkt->savedpos < 4) |
32874aea |
2134 | return; |
ff3187f6 |
2135 | len = GET_32BIT(pkt->body + pkt->savedpos); |
57356d63 |
2136 | if (len < 0) |
2137 | return; |
2138 | *length = len; |
ff3187f6 |
2139 | pkt->savedpos += 4; |
2140 | if (pkt->length - pkt->savedpos < *length) |
32874aea |
2141 | return; |
ff3187f6 |
2142 | *p = (char *)(pkt->body + pkt->savedpos); |
2143 | pkt->savedpos += *length; |
7cca0d81 |
2144 | } |
ff3187f6 |
2145 | static void *ssh_pkt_getdata(struct Packet *pkt, int length) |
0016d70b |
2146 | { |
ff3187f6 |
2147 | if (pkt->length - pkt->savedpos < length) |
0016d70b |
2148 | return NULL; |
ff3187f6 |
2149 | pkt->savedpos += length; |
2150 | return pkt->body + (pkt->savedpos - length); |
0016d70b |
2151 | } |
ff3187f6 |
2152 | static int ssh1_pkt_getrsakey(struct Packet *pkt, struct RSAKey *key, |
0016d70b |
2153 | unsigned char **keystr) |
2154 | { |
2155 | int j; |
2156 | |
ff3187f6 |
2157 | j = makekey(pkt->body + pkt->savedpos, |
2158 | pkt->length - pkt->savedpos, |
0016d70b |
2159 | key, keystr, 0); |
2160 | |
2161 | if (j < 0) |
2162 | return FALSE; |
2163 | |
ff3187f6 |
2164 | pkt->savedpos += j; |
2165 | assert(pkt->savedpos < pkt->length); |
0016d70b |
2166 | |
2167 | return TRUE; |
2168 | } |
ff3187f6 |
2169 | static Bignum ssh1_pkt_getmp(struct Packet *pkt) |
0016d70b |
2170 | { |
2171 | int j; |
2172 | Bignum b; |
2173 | |
ff3187f6 |
2174 | j = ssh1_read_bignum(pkt->body + pkt->savedpos, |
2175 | pkt->length - pkt->savedpos, &b); |
0016d70b |
2176 | |
2177 | if (j < 0) |
2178 | return NULL; |
2179 | |
ff3187f6 |
2180 | pkt->savedpos += j; |
0016d70b |
2181 | return b; |
2182 | } |
ff3187f6 |
2183 | static Bignum ssh2_pkt_getmp(struct Packet *pkt) |
32874aea |
2184 | { |
7cca0d81 |
2185 | char *p; |
3709bfe9 |
2186 | int length; |
7cca0d81 |
2187 | Bignum b; |
2188 | |
ff3187f6 |
2189 | ssh_pkt_getstring(pkt, &p, &length); |
7cca0d81 |
2190 | if (!p) |
32874aea |
2191 | return NULL; |
ff3187f6 |
2192 | if (p[0] & 0x80) |
32874aea |
2193 | return NULL; |
d8baa528 |
2194 | b = bignum_from_bytes((unsigned char *)p, length); |
7cca0d81 |
2195 | return b; |
2196 | } |
2197 | |
7d503c31 |
2198 | /* |
2e85c969 |
2199 | * Helper function to add an SSH-2 signature blob to a packet. |
1dd353b5 |
2200 | * Expects to be shown the public key blob as well as the signature |
2201 | * blob. Normally works just like ssh2_pkt_addstring, but will |
2202 | * fiddle with the signature packet if necessary for |
2203 | * BUG_SSH2_RSA_PADDING. |
2204 | */ |
ff3187f6 |
2205 | static void ssh2_add_sigblob(Ssh ssh, struct Packet *pkt, |
2206 | void *pkblob_v, int pkblob_len, |
1dd353b5 |
2207 | void *sigblob_v, int sigblob_len) |
2208 | { |
2209 | unsigned char *pkblob = (unsigned char *)pkblob_v; |
2210 | unsigned char *sigblob = (unsigned char *)sigblob_v; |
2211 | |
2212 | /* dmemdump(pkblob, pkblob_len); */ |
2213 | /* dmemdump(sigblob, sigblob_len); */ |
2214 | |
2215 | /* |
2216 | * See if this is in fact an ssh-rsa signature and a buggy |
2217 | * server; otherwise we can just do this the easy way. |
2218 | */ |
51470298 |
2219 | if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) && |
1dd353b5 |
2220 | (GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) { |
2221 | int pos, len, siglen; |
2222 | |
2223 | /* |
2224 | * Find the byte length of the modulus. |
2225 | */ |
2226 | |
2227 | pos = 4+7; /* skip over "ssh-rsa" */ |
2228 | pos += 4 + GET_32BIT(pkblob+pos); /* skip over exponent */ |
2229 | len = GET_32BIT(pkblob+pos); /* find length of modulus */ |
2230 | pos += 4; /* find modulus itself */ |
2231 | while (len > 0 && pkblob[pos] == 0) |
2232 | len--, pos++; |
2233 | /* debug(("modulus length is %d\n", len)); */ |
2234 | |
2235 | /* |
2236 | * Now find the signature integer. |
2237 | */ |
2238 | pos = 4+7; /* skip over "ssh-rsa" */ |
2239 | siglen = GET_32BIT(sigblob+pos); |
2240 | /* debug(("signature length is %d\n", siglen)); */ |
2241 | |
2242 | if (len != siglen) { |
2243 | unsigned char newlen[4]; |
ff3187f6 |
2244 | ssh2_pkt_addstring_start(pkt); |
2245 | ssh2_pkt_addstring_data(pkt, (char *)sigblob, pos); |
1dd353b5 |
2246 | /* dmemdump(sigblob, pos); */ |
2247 | pos += 4; /* point to start of actual sig */ |
2248 | PUT_32BIT(newlen, len); |
ff3187f6 |
2249 | ssh2_pkt_addstring_data(pkt, (char *)newlen, 4); |
1dd353b5 |
2250 | /* dmemdump(newlen, 4); */ |
2251 | newlen[0] = 0; |
2252 | while (len-- > siglen) { |
ff3187f6 |
2253 | ssh2_pkt_addstring_data(pkt, (char *)newlen, 1); |
1dd353b5 |
2254 | /* dmemdump(newlen, 1); */ |
2255 | } |
ff3187f6 |
2256 | ssh2_pkt_addstring_data(pkt, (char *)(sigblob+pos), siglen); |
1dd353b5 |
2257 | /* dmemdump(sigblob+pos, siglen); */ |
2258 | return; |
2259 | } |
2260 | |
2261 | /* Otherwise fall through and do it the easy way. */ |
2262 | } |
2263 | |
ff3187f6 |
2264 | ssh2_pkt_addstring_start(pkt); |
2265 | ssh2_pkt_addstring_data(pkt, (char *)sigblob, sigblob_len); |
1dd353b5 |
2266 | } |
2267 | |
2268 | /* |
7d503c31 |
2269 | * Examine the remote side's version string and compare it against |
2270 | * a list of known buggy implementations. |
2271 | */ |
51470298 |
2272 | static void ssh_detect_bugs(Ssh ssh, char *vstring) |
32874aea |
2273 | { |
2274 | char *imp; /* pointer to implementation part */ |
7d503c31 |
2275 | imp = vstring; |
2276 | imp += strcspn(imp, "-"); |
bd358db1 |
2277 | if (*imp) imp++; |
7d503c31 |
2278 | imp += strcspn(imp, "-"); |
bd358db1 |
2279 | if (*imp) imp++; |
7d503c31 |
2280 | |
51470298 |
2281 | ssh->remote_bugs = 0; |
7d503c31 |
2282 | |
bf982899 |
2283 | /* |
2284 | * General notes on server version strings: |
2285 | * - Not all servers reporting "Cisco-1.25" have all the bugs listed |
2286 | * here -- in particular, we've heard of one that's perfectly happy |
2287 | * with SSH1_MSG_IGNOREs -- but this string never seems to change, |
2288 | * so we can't distinguish them. |
2289 | */ |
5ecd7ad0 |
2290 | if (ssh->cfg.sshbug_ignore1 == FORCE_ON || |
2291 | (ssh->cfg.sshbug_ignore1 == AUTO && |
2c9c6388 |
2292 | (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") || |
2293 | !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") || |
46ac09aa |
2294 | !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25") || |
bd0b4caf |
2295 | !strcmp(imp, "OSU_1.4alpha3") || !strcmp(imp, "OSU_1.5alpha4")))) { |
32874aea |
2296 | /* |
2297 | * These versions don't support SSH1_MSG_IGNORE, so we have |
2298 | * to use a different defence against password length |
2299 | * sniffing. |
2300 | */ |
51470298 |
2301 | ssh->remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE; |
2e85c969 |
2302 | logevent("We believe remote version has SSH-1 ignore bug"); |
7d503c31 |
2303 | } |
2304 | |
5ecd7ad0 |
2305 | if (ssh->cfg.sshbug_plainpw1 == FORCE_ON || |
2306 | (ssh->cfg.sshbug_plainpw1 == AUTO && |
46ac09aa |
2307 | (!strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3")))) { |
bd358db1 |
2308 | /* |
2309 | * These versions need a plain password sent; they can't |
2310 | * handle having a null and a random length of data after |
2311 | * the password. |
2312 | */ |
51470298 |
2313 | ssh->remote_bugs |= BUG_NEEDS_SSH1_PLAIN_PASSWORD; |
2e85c969 |
2314 | logevent("We believe remote version needs a plain SSH-1 password"); |
bd358db1 |
2315 | } |
2316 | |
5ecd7ad0 |
2317 | if (ssh->cfg.sshbug_rsa1 == FORCE_ON || |
2318 | (ssh->cfg.sshbug_rsa1 == AUTO && |
2c9c6388 |
2319 | (!strcmp(imp, "Cisco-1.25")))) { |
0df73905 |
2320 | /* |
2321 | * These versions apparently have no clue whatever about |
2322 | * RSA authentication and will panic and die if they see |
2323 | * an AUTH_RSA message. |
2324 | */ |
51470298 |
2325 | ssh->remote_bugs |= BUG_CHOKES_ON_RSA; |
19f47a7d |
2326 | logevent("We believe remote version can't handle SSH-1 RSA authentication"); |
0df73905 |
2327 | } |
2328 | |
5ecd7ad0 |
2329 | if (ssh->cfg.sshbug_hmac2 == FORCE_ON || |
2330 | (ssh->cfg.sshbug_hmac2 == AUTO && |
b9f387af |
2331 | !wc_match("* VShell", imp) && |
831301f6 |
2332 | (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) || |
2333 | wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) || |
2334 | wc_match("2.1 *", imp)))) { |
32874aea |
2335 | /* |
2336 | * These versions have the HMAC bug. |
2337 | */ |
51470298 |
2338 | ssh->remote_bugs |= BUG_SSH2_HMAC; |
2e85c969 |
2339 | logevent("We believe remote version has SSH-2 HMAC bug"); |
7d503c31 |
2340 | } |
1dd353b5 |
2341 | |
5ecd7ad0 |
2342 | if (ssh->cfg.sshbug_derivekey2 == FORCE_ON || |
2343 | (ssh->cfg.sshbug_derivekey2 == AUTO && |
b9f387af |
2344 | !wc_match("* VShell", imp) && |
2856a1b9 |
2345 | (wc_match("2.0.0*", imp) || wc_match("2.0.10*", imp) ))) { |
088bde77 |
2346 | /* |
2347 | * These versions have the key-derivation bug (failing to |
2348 | * include the literal shared secret in the hashes that |
2349 | * generate the keys). |
2350 | */ |
51470298 |
2351 | ssh->remote_bugs |= BUG_SSH2_DERIVEKEY; |
2e85c969 |
2352 | logevent("We believe remote version has SSH-2 key-derivation bug"); |
088bde77 |
2353 | } |
2354 | |
5ecd7ad0 |
2355 | if (ssh->cfg.sshbug_rsapad2 == FORCE_ON || |
2356 | (ssh->cfg.sshbug_rsapad2 == AUTO && |
831301f6 |
2357 | (wc_match("OpenSSH_2.[5-9]*", imp) || |
2358 | wc_match("OpenSSH_3.[0-2]*", imp)))) { |
1dd353b5 |
2359 | /* |
2e85c969 |
2360 | * These versions have the SSH-2 RSA padding bug. |
1dd353b5 |
2361 | */ |
51470298 |
2362 | ssh->remote_bugs |= BUG_SSH2_RSA_PADDING; |
2e85c969 |
2363 | logevent("We believe remote version has SSH-2 RSA padding bug"); |
1dd353b5 |
2364 | } |
8e975795 |
2365 | |
dda87a28 |
2366 | if (ssh->cfg.sshbug_pksessid2 == FORCE_ON || |
2367 | (ssh->cfg.sshbug_pksessid2 == AUTO && |
2368 | wc_match("OpenSSH_2.[0-2]*", imp))) { |
2369 | /* |
2e85c969 |
2370 | * These versions have the SSH-2 session-ID bug in |
dda87a28 |
2371 | * public-key authentication. |
2372 | */ |
2373 | ssh->remote_bugs |= BUG_SSH2_PK_SESSIONID; |
2e85c969 |
2374 | logevent("We believe remote version has SSH-2 public-key-session-ID bug"); |
dda87a28 |
2375 | } |
f382c87d |
2376 | |
2377 | if (ssh->cfg.sshbug_rekey2 == FORCE_ON || |
2378 | (ssh->cfg.sshbug_rekey2 == AUTO && |
39b8712f |
2379 | (wc_match("DigiSSH_2.0", imp) || |
2380 | wc_match("OpenSSH_2.[0-4]*", imp) || |
e12d95a5 |
2381 | wc_match("OpenSSH_2.5.[0-3]*", imp) || |
2382 | wc_match("Sun_SSH_1.0", imp) || |
f4275b53 |
2383 | wc_match("Sun_SSH_1.0.1", imp) || |
96bd26de |
2384 | /* All versions <= 1.2.6 (they changed their format in 1.2.7) */ |
2385 | wc_match("WeOnlyDo-*", imp)))) { |
f382c87d |
2386 | /* |
2e85c969 |
2387 | * These versions have the SSH-2 rekey bug. |
f382c87d |
2388 | */ |
2389 | ssh->remote_bugs |= BUG_SSH2_REKEY; |
2e85c969 |
2390 | logevent("We believe remote version has SSH-2 rekey bug"); |
f382c87d |
2391 | } |
7d503c31 |
2392 | } |
2393 | |
d38d6a1f |
2394 | /* |
2395 | * The `software version' part of an SSH version string is required |
2396 | * to contain no spaces or minus signs. |
2397 | */ |
2398 | static void ssh_fix_verstring(char *str) |
2399 | { |
2400 | /* Eat "SSH-<protoversion>-". */ |
2401 | assert(*str == 'S'); str++; |
2402 | assert(*str == 'S'); str++; |
2403 | assert(*str == 'H'); str++; |
2404 | assert(*str == '-'); str++; |
2405 | while (*str && *str != '-') str++; |
2406 | assert(*str == '-'); str++; |
2407 | |
2408 | /* Convert minus signs and spaces in the remaining string into |
2409 | * underscores. */ |
2410 | while (*str) { |
2411 | if (*str == '-' || *str == ' ') |
2412 | *str = '_'; |
2413 | str++; |
2414 | } |
2415 | } |
2416 | |
1fcd0d98 |
2417 | /* |
2418 | * Send an appropriate SSH version string. |
2419 | */ |
2420 | static void ssh_send_verstring(Ssh ssh, char *svers) |
2421 | { |
2422 | char *verstring; |
2423 | |
2424 | if (ssh->version == 2) { |
2425 | /* |
2426 | * Construct a v2 version string. |
2427 | */ |
2428 | verstring = dupprintf("SSH-2.0-%s\015\012", sshver); |
2429 | } else { |
2430 | /* |
2431 | * Construct a v1 version string. |
2432 | */ |
2433 | verstring = dupprintf("SSH-%s-%s\012", |
2434 | (ssh_versioncmp(svers, "1.5") <= 0 ? |
2435 | svers : "1.5"), |
2436 | sshver); |
2437 | } |
2438 | |
2439 | ssh_fix_verstring(verstring); |
2440 | |
2441 | if (ssh->version == 2) { |
2442 | size_t len; |
2443 | /* |
2444 | * Record our version string. |
2445 | */ |
2446 | len = strcspn(verstring, "\015\012"); |
2447 | ssh->v_c = snewn(len + 1, char); |
2448 | memcpy(ssh->v_c, verstring, len); |
2449 | ssh->v_c[len] = 0; |
2450 | } |
2451 | |
2452 | logeventf(ssh, "We claim version: %.*s", |
2453 | strcspn(verstring, "\015\012"), verstring); |
2454 | s_write(ssh, verstring, strlen(verstring)); |
2455 | sfree(verstring); |
2456 | } |
2457 | |
51470298 |
2458 | static int do_ssh_init(Ssh ssh, unsigned char c) |
32874aea |
2459 | { |
51470298 |
2460 | struct do_ssh_init_state { |
2461 | int vslen; |
2462 | char version[10]; |
2463 | char *vstring; |
2464 | int vstrsize; |
2465 | int i; |
2466 | int proto1, proto2; |
2467 | }; |
2468 | crState(do_ssh_init_state); |
374330e2 |
2469 | |
51470298 |
2470 | crBegin(ssh->do_ssh_init_crstate); |
8df7a775 |
2471 | |
8c1835c0 |
2472 | /* Search for a line beginning with the string "SSH-" in the input. */ |
2473 | for (;;) { |
2474 | if (c != 'S') goto no; |
2475 | crReturn(1); |
2476 | if (c != 'S') goto no; |
2477 | crReturn(1); |
2478 | if (c != 'H') goto no; |
2479 | crReturn(1); |
2480 | if (c != '-') goto no; |
2481 | break; |
2482 | no: |
2483 | while (c != '\012') |
2484 | crReturn(1); |
2485 | crReturn(1); |
374330e2 |
2486 | } |
8df7a775 |
2487 | |
51470298 |
2488 | s->vstrsize = 16; |
3d88e64d |
2489 | s->vstring = snewn(s->vstrsize, char); |
51470298 |
2490 | strcpy(s->vstring, "SSH-"); |
2491 | s->vslen = 4; |
2492 | s->i = 0; |
374330e2 |
2493 | while (1) { |
8df7a775 |
2494 | crReturn(1); /* get another char */ |
51470298 |
2495 | if (s->vslen >= s->vstrsize - 1) { |
2496 | s->vstrsize += 16; |
3d88e64d |
2497 | s->vstring = sresize(s->vstring, s->vstrsize, char); |
32874aea |
2498 | } |
51470298 |
2499 | s->vstring[s->vslen++] = c; |
2500 | if (s->i >= 0) { |
374330e2 |
2501 | if (c == '-') { |
51470298 |
2502 | s->version[s->i] = '\0'; |
2503 | s->i = -1; |
2504 | } else if (s->i < sizeof(s->version) - 1) |
2505 | s->version[s->i++] = c; |
c4ffc4d0 |
2506 | } else if (c == '\012') |
374330e2 |
2507 | break; |
2508 | } |
2509 | |
51470298 |
2510 | ssh->agentfwd_enabled = FALSE; |
2511 | ssh->rdpkt2_state.incoming_sequence = 0; |
960e736a |
2512 | |
51470298 |
2513 | s->vstring[s->vslen] = 0; |
a7d4653a |
2514 | s->vstring[strcspn(s->vstring, "\015\012")] = '\0';/* remove EOL chars */ |
fb983202 |
2515 | logeventf(ssh, "Server version: %s", s->vstring); |
51470298 |
2516 | ssh_detect_bugs(ssh, s->vstring); |
c5e9c988 |
2517 | |
adf799dd |
2518 | /* |
38d228a2 |
2519 | * Decide which SSH protocol version to support. |
adf799dd |
2520 | */ |
38d228a2 |
2521 | |
2522 | /* Anything strictly below "2.0" means protocol 1 is supported. */ |
51470298 |
2523 | s->proto1 = ssh_versioncmp(s->version, "2.0") < 0; |
38d228a2 |
2524 | /* Anything greater or equal to "1.99" means protocol 2 is supported. */ |
51470298 |
2525 | s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0; |
38d228a2 |
2526 | |
86916870 |
2527 | if (ssh->cfg.sshprot == 0 && !s->proto1) { |
6b5cf8b4 |
2528 | bombout(("SSH protocol version 1 required by user but not provided by server")); |
7ffdbc1a |
2529 | crStop(0); |
38d228a2 |
2530 | } |
86916870 |
2531 | if (ssh->cfg.sshprot == 3 && !s->proto2) { |
6b5cf8b4 |
2532 | bombout(("SSH protocol version 2 required by user but not provided by server")); |
7ffdbc1a |
2533 | crStop(0); |
38d228a2 |
2534 | } |
2535 | |
1fcd0d98 |
2536 | if (s->proto2 && (ssh->cfg.sshprot >= 2 || !s->proto1)) |
2537 | ssh->version = 2; |
2538 | else |
2539 | ssh->version = 1; |
d38d6a1f |
2540 | |
2541 | logeventf(ssh, "Using SSH protocol version %d", ssh->version); |
2542 | |
1fcd0d98 |
2543 | /* Send the version string, if we haven't already */ |
2544 | if (ssh->cfg.sshprot != 3) |
2545 | ssh_send_verstring(ssh, s->version); |
2546 | |
2547 | if (ssh->version == 2) { |
2548 | size_t len; |
2549 | /* |
2550 | * Record their version string. |
2551 | */ |
2552 | len = strcspn(s->vstring, "\015\012"); |
2553 | ssh->v_s = snewn(len + 1, char); |
2554 | memcpy(ssh->v_s, s->vstring, len); |
2555 | ssh->v_s[len] = 0; |
2556 | |
2557 | /* |
2558 | * Initialise SSH-2 protocol. |
2559 | */ |
2560 | ssh->protocol = ssh2_protocol; |
2561 | ssh2_protocol_setup(ssh); |
2562 | ssh->s_rdpkt = ssh2_rdpkt; |
2563 | } else { |
2564 | /* |
2565 | * Initialise SSH-1 protocol. |
2566 | */ |
2567 | ssh->protocol = ssh1_protocol; |
2568 | ssh1_protocol_setup(ssh); |
2569 | ssh->s_rdpkt = ssh1_rdpkt; |
2570 | } |
2571 | if (ssh->version == 2) |
2572 | do_ssh2_transport(ssh, NULL, -1, NULL); |
2573 | |
125105d1 |
2574 | update_specials_menu(ssh->frontend); |
51470298 |
2575 | ssh->state = SSH_STATE_BEFORE_SIZE; |
39934deb |
2576 | ssh->pinger = pinger_new(&ssh->cfg, &ssh_backend, ssh); |
8df7a775 |
2577 | |
51470298 |
2578 | sfree(s->vstring); |
50526e47 |
2579 | |
8df7a775 |
2580 | crFinish(0); |
2581 | } |
2582 | |
3d9449a1 |
2583 | static void ssh_process_incoming_data(Ssh ssh, |
2584 | unsigned char **data, int *datalen) |
2585 | { |
bf8a49a1 |
2586 | struct Packet *pktin; |
2587 | |
2588 | pktin = ssh->s_rdpkt(ssh, data, datalen); |
3d9449a1 |
2589 | if (pktin) { |
2590 | ssh->protocol(ssh, NULL, 0, pktin); |
2591 | ssh_free_packet(pktin); |
2592 | } |
2593 | } |
2594 | |
2595 | static void ssh_queue_incoming_data(Ssh ssh, |
2596 | unsigned char **data, int *datalen) |
2597 | { |
2598 | bufchain_add(&ssh->queued_incoming_data, *data, *datalen); |
2599 | *data += *datalen; |
2600 | *datalen = 0; |
2601 | } |
2602 | |
2603 | static void ssh_process_queued_incoming_data(Ssh ssh) |
2604 | { |
2605 | void *vdata; |
2606 | unsigned char *data; |
2607 | int len, origlen; |
2608 | |
2609 | while (!ssh->frozen && bufchain_size(&ssh->queued_incoming_data)) { |
2610 | bufchain_prefix(&ssh->queued_incoming_data, &vdata, &len); |
2611 | data = vdata; |
2612 | origlen = len; |
2613 | |
2614 | while (!ssh->frozen && len > 0) |
2615 | ssh_process_incoming_data(ssh, &data, &len); |
2616 | |
2617 | if (origlen > len) |
2618 | bufchain_consume(&ssh->queued_incoming_data, origlen - len); |
2619 | } |
2620 | } |
2621 | |
2622 | static void ssh_set_frozen(Ssh ssh, int frozen) |
2623 | { |
a5a6f839 |
2624 | if (ssh->s) |
2625 | sk_set_frozen(ssh->s, frozen); |
3d9449a1 |
2626 | ssh->frozen = frozen; |
2627 | } |
2628 | |
51470298 |
2629 | static void ssh_gotdata(Ssh ssh, unsigned char *data, int datalen) |
8df7a775 |
2630 | { |
bf8a49a1 |
2631 | /* Log raw data, if we're in that mode. */ |
ff05dfef |
2632 | if (ssh->logctx) |
2633 | log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, datalen, |
2634 | 0, NULL); |
bf8a49a1 |
2635 | |
51470298 |
2636 | crBegin(ssh->ssh_gotdata_crstate); |
8df7a775 |
2637 | |
2638 | /* |
2639 | * To begin with, feed the characters one by one to the |
2640 | * protocol initialisation / selection function do_ssh_init(). |
2641 | * When that returns 0, we're done with the initial greeting |
2642 | * exchange and can move on to packet discipline. |
2643 | */ |
2644 | while (1) { |
51470298 |
2645 | int ret; /* need not be kept across crReturn */ |
8df7a775 |
2646 | if (datalen == 0) |
2647 | crReturnV; /* more data please */ |
51470298 |
2648 | ret = do_ssh_init(ssh, *data); |
32874aea |
2649 | data++; |
2650 | datalen--; |
8df7a775 |
2651 | if (ret == 0) |
2652 | break; |
2653 | } |
2654 | |
2655 | /* |
2656 | * We emerge from that loop when the initial negotiation is |
2657 | * over and we have selected an s_rdpkt function. Now pass |
2658 | * everything to s_rdpkt, and then pass the resulting packets |
2659 | * to the proper protocol handler. |
2660 | */ |
3d9449a1 |
2661 | |
8df7a775 |
2662 | while (1) { |
e375106c |
2663 | while (bufchain_size(&ssh->queued_incoming_data) > 0 || datalen > 0) { |
2664 | if (ssh->frozen) { |
3d9449a1 |
2665 | ssh_queue_incoming_data(ssh, &data, &datalen); |
e375106c |
2666 | /* This uses up all data and cannot cause anything interesting |
2667 | * to happen; indeed, for anything to happen at all, we must |
2668 | * return, so break out. */ |
2669 | break; |
2670 | } else if (bufchain_size(&ssh->queued_incoming_data) > 0) { |
2671 | /* This uses up some or all data, and may freeze the |
2672 | * session. */ |
2673 | ssh_process_queued_incoming_data(ssh); |
2674 | } else { |
2675 | /* This uses up some or all data, and may freeze the |
2676 | * session. */ |
2677 | ssh_process_incoming_data(ssh, &data, &datalen); |
2678 | } |
2679 | /* FIXME this is probably EBW. */ |
ff3187f6 |
2680 | if (ssh->state == SSH_STATE_CLOSED) |
2681 | return; |
8df7a775 |
2682 | } |
e375106c |
2683 | /* We're out of data. Go and get some more. */ |
8df7a775 |
2684 | crReturnV; |
2685 | } |
2686 | crFinishV; |
2687 | } |
2688 | |
ac934965 |
2689 | static int ssh_do_close(Ssh ssh, int notify_exit) |
32874aea |
2690 | { |
80ffa58b |
2691 | int ret = 0; |
36f94d1f |
2692 | struct ssh_channel *c; |
2693 | |
51470298 |
2694 | ssh->state = SSH_STATE_CLOSED; |
ecbb0000 |
2695 | expire_timer_context(ssh); |
51470298 |
2696 | if (ssh->s) { |
2697 | sk_close(ssh->s); |
2698 | ssh->s = NULL; |
ac934965 |
2699 | if (notify_exit) |
2700 | notify_remote_exit(ssh->frontend); |
2701 | else |
2702 | ret = 1; |
f3ab576e |
2703 | } |
36f94d1f |
2704 | /* |
80ffa58b |
2705 | * Now we must shut down any port- and X-forwarded channels going |
36f94d1f |
2706 | * through this connection. |
2707 | */ |
74a98066 |
2708 | if (ssh->channels) { |
80ffa58b |
2709 | while (NULL != (c = index234(ssh->channels, 0))) { |
74a98066 |
2710 | switch (c->type) { |
2711 | case CHAN_X11: |
2712 | x11_close(c->u.x11.s); |
2713 | break; |
2714 | case CHAN_SOCKDATA: |
2715 | pfd_close(c->u.pfd.s); |
2716 | break; |
2717 | } |
80ffa58b |
2718 | del234(ssh->channels, c); /* moving next one to index 0 */ |
74a98066 |
2719 | if (ssh->version == 2) |
2720 | bufchain_clear(&c->v.v2.outbuffer); |
2721 | sfree(c); |
36f94d1f |
2722 | } |
36f94d1f |
2723 | } |
f8c9f9df |
2724 | /* |
2725 | * Go through port-forwardings, and close any associated |
2726 | * listening sockets. |
2727 | */ |
2728 | if (ssh->portfwds) { |
2729 | struct ssh_portfwd *pf; |
2730 | while (NULL != (pf = index234(ssh->portfwds, 0))) { |
2731 | /* Dispose of any listening socket. */ |
2732 | if (pf->local) |
2733 | pfd_terminate(pf->local); |
2734 | del234(ssh->portfwds, pf); /* moving next one to index 0 */ |
2735 | free_portfwd(pf); |
2736 | } |
2737 | } |
ac934965 |
2738 | |
2739 | return ret; |
36f94d1f |
2740 | } |
2741 | |
7555d6a5 |
2742 | static void ssh_log(Plug plug, int type, SockAddr addr, int port, |
2743 | const char *error_msg, int error_code) |
2744 | { |
2745 | Ssh ssh = (Ssh) plug; |
2746 | char addrbuf[256], *msg; |
2747 | |
2748 | sk_getaddr(addr, addrbuf, lenof(addrbuf)); |
2749 | |
2750 | if (type == 0) |
2751 | msg = dupprintf("Connecting to %s port %d", addrbuf, port); |
2752 | else |
2753 | msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); |
2754 | |
2755 | logevent(msg); |
fb983202 |
2756 | sfree(msg); |
7555d6a5 |
2757 | } |
2758 | |
cbe2d68f |
2759 | static int ssh_closing(Plug plug, const char *error_msg, int error_code, |
36f94d1f |
2760 | int calling_back) |
2761 | { |
2762 | Ssh ssh = (Ssh) plug; |
ac934965 |
2763 | int need_notify = ssh_do_close(ssh, FALSE); |
2764 | |
9e296bfa |
2765 | if (!error_msg) { |
2766 | if (!ssh->close_expected) |
2767 | error_msg = "Server unexpectedly closed network connection"; |
2768 | else |
2769 | error_msg = "Server closed network connection"; |
ac934965 |
2770 | } |
2771 | |
d051d1b4 |
2772 | if (ssh->close_expected && ssh->clean_exit && ssh->exitcode < 0) |
2773 | ssh->exitcode = 0; |
2774 | |
2425184b |
2775 | if (need_notify) |
2776 | notify_remote_exit(ssh->frontend); |
2777 | |
9e296bfa |
2778 | if (error_msg) |
247308b5 |
2779 | logevent(error_msg); |
9e296bfa |
2780 | if (!ssh->close_expected || !ssh->clean_exit) |
971bcc0a |
2781 | connection_fatal(ssh->frontend, "%s", error_msg); |
7e78000d |
2782 | return 0; |
2783 | } |
2784 | |
32874aea |
2785 | static int ssh_receive(Plug plug, int urgent, char *data, int len) |
2786 | { |
51470298 |
2787 | Ssh ssh = (Ssh) plug; |
d8baa528 |
2788 | ssh_gotdata(ssh, (unsigned char *)data, len); |
51470298 |
2789 | if (ssh->state == SSH_STATE_CLOSED) { |
ac934965 |
2790 | ssh_do_close(ssh, TRUE); |
32874aea |
2791 | return 0; |
3257deae |
2792 | } |
fef97f43 |
2793 | return 1; |
374330e2 |
2794 | } |
2795 | |
5471d09a |
2796 | static void ssh_sent(Plug plug, int bufsize) |
2797 | { |
51470298 |
2798 | Ssh ssh = (Ssh) plug; |
5471d09a |
2799 | /* |
2800 | * If the send backlog on the SSH socket itself clears, we |
2801 | * should unthrottle the whole world if it was throttled. |
2802 | */ |
2803 | if (bufsize < SSH_MAX_BACKLOG) |
51470298 |
2804 | ssh_throttle_all(ssh, 0, bufsize); |
5471d09a |
2805 | } |
2806 | |
fb09bf1c |
2807 | /* |
8df7a775 |
2808 | * Connect to specified host and port. |
2809 | * Returns an error message, or NULL on success. |
6e1ebb76 |
2810 | * Also places the canonical host name into `realhost'. It must be |
2811 | * freed by the caller. |
8df7a775 |
2812 | */ |
cbe2d68f |
2813 | static const char *connect_to_host(Ssh ssh, char *host, int port, |
79bf227b |
2814 | char **realhost, int nodelay, int keepalive) |
8df7a775 |
2815 | { |
51470298 |
2816 | static const struct plug_function_table fn_table = { |
7555d6a5 |
2817 | ssh_log, |
7e78000d |
2818 | ssh_closing, |
5471d09a |
2819 | ssh_receive, |
2820 | ssh_sent, |
2821 | NULL |
51470298 |
2822 | }; |
7e78000d |
2823 | |
8df7a775 |
2824 | SockAddr addr; |
cbe2d68f |
2825 | const char *err; |
8df7a775 |
2826 | |
3d88e64d |
2827 | ssh->savedhost = snewn(1 + strlen(host), char); |
51470298 |
2828 | strcpy(ssh->savedhost, host); |
8df7a775 |
2829 | |
2830 | if (port < 0) |
2831 | port = 22; /* default ssh port */ |
51470298 |
2832 | ssh->savedport = port; |
8df7a775 |
2833 | |
2834 | /* |
2835 | * Try to find host. |
2836 | */ |
05581745 |
2837 | logeventf(ssh, "Looking up host \"%s\"%s", host, |
2838 | (ssh->cfg.addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : |
2839 | (ssh->cfg.addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); |
2840 | addr = name_lookup(host, port, realhost, &ssh->cfg, |
2841 | ssh->cfg.addressfamily); |
170c1e6e |
2842 | if ((err = sk_addr_error(addr)) != NULL) { |
2843 | sk_addr_free(addr); |
8df7a775 |
2844 | return err; |
170c1e6e |
2845 | } |
8df7a775 |
2846 | |
8df7a775 |
2847 | /* |
2848 | * Open socket. |
2849 | */ |
51470298 |
2850 | ssh->fn = &fn_table; |
e8fa8f62 |
2851 | ssh->s = new_connection(addr, *realhost, port, |
79bf227b |
2852 | 0, 1, nodelay, keepalive, (Plug) ssh, &ssh->cfg); |
70e5d0fd |
2853 | if ((err = sk_socket_error(ssh->s)) != NULL) { |
51470298 |
2854 | ssh->s = NULL; |
39934deb |
2855 | notify_remote_exit(ssh->frontend); |
8df7a775 |
2856 | return err; |
67c4ba2e |
2857 | } |
8df7a775 |
2858 | |
ff05dfef |
2859 | /* |
2860 | * If the SSH version number's fixed, set it now, and if it's SSH-2, |
2861 | * send the version string too. |
2862 | */ |
2863 | if (ssh->cfg.sshprot == 0) |
2864 | ssh->version = 1; |
2865 | if (ssh->cfg.sshprot == 3) { |
2866 | ssh->version = 2; |
2867 | ssh_send_verstring(ssh, NULL); |
2868 | } |
2869 | |
8df7a775 |
2870 | return NULL; |
2871 | } |
2872 | |
2873 | /* |
5471d09a |
2874 | * Throttle or unthrottle the SSH connection. |
2875 | */ |
51470298 |
2876 | static void ssh1_throttle(Ssh ssh, int adjust) |
5471d09a |
2877 | { |
51470298 |
2878 | int old_count = ssh->v1_throttle_count; |
2879 | ssh->v1_throttle_count += adjust; |
2880 | assert(ssh->v1_throttle_count >= 0); |
2881 | if (ssh->v1_throttle_count && !old_count) { |
3d9449a1 |
2882 | ssh_set_frozen(ssh, 1); |
51470298 |
2883 | } else if (!ssh->v1_throttle_count && old_count) { |
3d9449a1 |
2884 | ssh_set_frozen(ssh, 0); |
5471d09a |
2885 | } |
2886 | } |
2887 | |
2888 | /* |
2889 | * Throttle or unthrottle _all_ local data streams (for when sends |
2890 | * on the SSH connection itself back up). |
2891 | */ |
51470298 |
2892 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize) |
5471d09a |
2893 | { |
2894 | int i; |
2895 | struct ssh_channel *c; |
2896 | |
51470298 |
2897 | if (enable == ssh->throttled_all) |
5471d09a |
2898 | return; |
51470298 |
2899 | ssh->throttled_all = enable; |
2900 | ssh->overall_bufsize = bufsize; |
2901 | if (!ssh->channels) |
5471d09a |
2902 | return; |
51470298 |
2903 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
5471d09a |
2904 | switch (c->type) { |
2905 | case CHAN_MAINSESSION: |
2906 | /* |
2907 | * This is treated separately, outside the switch. |
2908 | */ |
2909 | break; |
2910 | case CHAN_X11: |
2911 | x11_override_throttle(c->u.x11.s, enable); |
2912 | break; |
2913 | case CHAN_AGENT: |
2914 | /* Agent channels require no buffer management. */ |
2915 | break; |
2916 | case CHAN_SOCKDATA: |
36f94d1f |
2917 | pfd_override_throttle(c->u.pfd.s, enable); |
5471d09a |
2918 | break; |
2919 | } |
2920 | } |
2921 | } |
2922 | |
f11d78f2 |
2923 | static void ssh_agent_callback(void *sshv, void *reply, int replylen) |
839f10db |
2924 | { |
2925 | Ssh ssh = (Ssh) sshv; |
2926 | |
2927 | ssh->agent_response = reply; |
2928 | ssh->agent_response_len = replylen; |
2929 | |
2930 | if (ssh->version == 1) |
ff3187f6 |
2931 | do_ssh1_login(ssh, NULL, -1, NULL); |
839f10db |
2932 | else |
ff3187f6 |
2933 | do_ssh2_authconn(ssh, NULL, -1, NULL); |
839f10db |
2934 | } |
2935 | |
3d9449a1 |
2936 | static void ssh_dialog_callback(void *sshv, int ret) |
2937 | { |
2938 | Ssh ssh = (Ssh) sshv; |
2939 | |
2940 | ssh->user_response = ret; |
2941 | |
2942 | if (ssh->version == 1) |
2943 | do_ssh1_login(ssh, NULL, -1, NULL); |
2944 | else |
2945 | do_ssh2_transport(ssh, NULL, -1, NULL); |
2946 | |
2947 | /* |
2948 | * This may have unfrozen the SSH connection, so do a |
2949 | * queued-data run. |
2950 | */ |
2951 | ssh_process_queued_incoming_data(ssh); |
2952 | } |
2953 | |
f11d78f2 |
2954 | static void ssh_agentf_callback(void *cv, void *reply, int replylen) |
839f10db |
2955 | { |
2956 | struct ssh_channel *c = (struct ssh_channel *)cv; |
2957 | Ssh ssh = c->ssh; |
2958 | void *sentreply = reply; |
2959 | |
2960 | if (!sentreply) { |
2961 | /* Fake SSH_AGENT_FAILURE. */ |
2962 | sentreply = "\0\0\0\1\5"; |
2963 | replylen = 5; |
2964 | } |
2965 | if (ssh->version == 2) { |
2966 | ssh2_add_channel_data(c, sentreply, replylen); |
2967 | ssh2_try_send(c); |
2968 | } else { |
2969 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
2970 | PKT_INT, c->remoteid, |
2971 | PKT_INT, replylen, |
a2724fba |
2972 | PKTT_DATA, |
839f10db |
2973 | PKT_DATA, sentreply, replylen, |
9a10ecf4 |
2974 | PKTT_OTHER, |
839f10db |
2975 | PKT_END); |
2976 | } |
2977 | if (reply) |
2978 | sfree(reply); |
2979 | } |
2980 | |
0405e71f |
2981 | /* |
9e296bfa |
2982 | * Client-initiated disconnection. Send a DISCONNECT if `wire_reason' |
2983 | * non-NULL, otherwise just close the connection. `client_reason' == NULL |
2984 | * => log `wire_reason'. |
2985 | */ |
2986 | static void ssh_disconnect(Ssh ssh, char *client_reason, char *wire_reason, |
2987 | int code, int clean_exit) |
2988 | { |
2989 | char *error; |
2990 | if (!client_reason) |
2991 | client_reason = wire_reason; |
2992 | if (client_reason) |
2993 | error = dupprintf("Disconnected: %s", client_reason); |
2994 | else |
2995 | error = dupstr("Disconnected"); |
2996 | if (wire_reason) { |
2997 | if (ssh->version == 1) { |
2998 | send_packet(ssh, SSH1_MSG_DISCONNECT, PKT_STR, wire_reason, |
2999 | PKT_END); |
3000 | } else if (ssh->version == 2) { |
3001 | struct Packet *pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
3002 | ssh2_pkt_adduint32(pktout, code); |
3003 | ssh2_pkt_addstring(pktout, wire_reason); |
3004 | ssh2_pkt_addstring(pktout, "en"); /* language tag */ |
3005 | ssh2_pkt_send_noqueue(ssh, pktout); |
3006 | } |
3007 | } |
3008 | ssh->close_expected = TRUE; |
3009 | ssh->clean_exit = clean_exit; |
3010 | ssh_closing((Plug)ssh, error, 0, 0); |
3011 | sfree(error); |
3012 | } |
3013 | |
3014 | /* |
fb09bf1c |
3015 | * Handle the key exchange and user authentication phases. |
3016 | */ |
ff3187f6 |
3017 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, |
3018 | struct Packet *pktin) |
fb09bf1c |
3019 | { |
0016d70b |
3020 | int i, j, ret; |
3021 | unsigned char cookie[8], *ptr; |
374330e2 |
3022 | struct RSAKey servkey, hostkey; |
3023 | struct MD5Context md5c; |
51470298 |
3024 | struct do_ssh1_login_state { |
3025 | int len; |
3026 | unsigned char *rsabuf, *keystr1, *keystr2; |
3027 | unsigned long supported_ciphers_mask, supported_auths_mask; |
3028 | int tried_publickey, tried_agent; |
3029 | int tis_auth_refused, ccard_auth_refused; |
3030 | unsigned char session_id[16]; |
3031 | int cipher_type; |
3032 | char username[100]; |
3033 | void *publickey_blob; |
3034 | int publickey_bloblen; |
edd0cb8a |
3035 | char *publickey_comment; |
3036 | int publickey_encrypted; |
3037 | prompts_t *cur_prompt; |
51470298 |
3038 | char c; |
3039 | int pwpkt_type; |
3040 | unsigned char request[5], *response, *p; |
3041 | int responselen; |
3042 | int keyi, nkeys; |
3043 | int authed; |
3044 | struct RSAKey key; |
3045 | Bignum challenge; |
3046 | char *commentp; |
3047 | int commentlen; |
3d9449a1 |
3048 | int dlgret; |
51470298 |
3049 | }; |
3050 | crState(do_ssh1_login_state); |
3051 | |
3052 | crBegin(ssh->do_ssh1_login_crstate); |
374330e2 |
3053 | |
ff3187f6 |
3054 | if (!pktin) |
3055 | crWaitUntil(pktin); |
374330e2 |
3056 | |
ff3187f6 |
3057 | if (pktin->type != SSH1_SMSG_PUBLIC_KEY) { |
6b5cf8b4 |
3058 | bombout(("Public key packet not received")); |
7ffdbc1a |
3059 | crStop(0); |
8d5de777 |
3060 | } |
374330e2 |
3061 | |
c5e9c988 |
3062 | logevent("Received public keys"); |
374330e2 |
3063 | |
ff3187f6 |
3064 | ptr = ssh_pkt_getdata(pktin, 8); |
0016d70b |
3065 | if (!ptr) { |
2e85c969 |
3066 | bombout(("SSH-1 public key packet stopped before random cookie")); |
0016d70b |
3067 | crStop(0); |
3068 | } |
3069 | memcpy(cookie, ptr, 8); |
374330e2 |
3070 | |
ff3187f6 |
3071 | if (!ssh1_pkt_getrsakey(pktin, &servkey, &s->keystr1) || |
3072 | !ssh1_pkt_getrsakey(pktin, &hostkey, &s->keystr2)) { |
2e85c969 |
3073 | bombout(("Failed to read SSH-1 public keys from public key packet")); |
0016d70b |
3074 | crStop(0); |
3075 | } |
374330e2 |
3076 | |
c5e9c988 |
3077 | /* |
1c2a93c4 |
3078 | * Log the host key fingerprint. |
c5e9c988 |
3079 | */ |
c5e9c988 |
3080 | { |
3081 | char logmsg[80]; |
1c2a93c4 |
3082 | logevent("Host key fingerprint is:"); |
c5e9c988 |
3083 | strcpy(logmsg, " "); |
32874aea |
3084 | hostkey.comment = NULL; |
3085 | rsa_fingerprint(logmsg + strlen(logmsg), |
3086 | sizeof(logmsg) - strlen(logmsg), &hostkey); |
c5e9c988 |
3087 | logevent(logmsg); |
3088 | } |
3089 | |
ff3187f6 |
3090 | ssh->v1_remote_protoflags = ssh_pkt_getuint32(pktin); |
3091 | s->supported_ciphers_mask = ssh_pkt_getuint32(pktin); |
3092 | s->supported_auths_mask = ssh_pkt_getuint32(pktin); |
d5b2c841 |
3093 | if ((ssh->remote_bugs & BUG_CHOKES_ON_RSA)) |
3094 | s->supported_auths_mask &= ~(1 << SSH1_AUTH_RSA); |
bea1ef5f |
3095 | |
51470298 |
3096 | ssh->v1_local_protoflags = |
3097 | ssh->v1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED; |
3098 | ssh->v1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER; |
b96dc54c |
3099 | |
c5e9c988 |
3100 | MD5Init(&md5c); |
51470298 |
3101 | MD5Update(&md5c, s->keystr2, hostkey.bytes); |
3102 | MD5Update(&md5c, s->keystr1, servkey.bytes); |
0016d70b |
3103 | MD5Update(&md5c, cookie, 8); |
51470298 |
3104 | MD5Final(s->session_id, &md5c); |
374330e2 |
3105 | |
32874aea |
3106 | for (i = 0; i < 32; i++) |
51470298 |
3107 | ssh->session_key[i] = random_byte(); |
374330e2 |
3108 | |
0016d70b |
3109 | /* |
3110 | * Verify that the `bits' and `bytes' parameters match. |
3111 | */ |
3112 | if (hostkey.bits > hostkey.bytes * 8 || |
3113 | servkey.bits > servkey.bytes * 8) { |
2e85c969 |
3114 | bombout(("SSH-1 public keys were badly formatted")); |
0016d70b |
3115 | crStop(0); |
3116 | } |
3117 | |
51470298 |
3118 | s->len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); |
374330e2 |
3119 | |
3d88e64d |
3120 | s->rsabuf = snewn(s->len, unsigned char); |
374330e2 |
3121 | |
89ee5268 |
3122 | /* |
3123 | * Verify the host key. |
3124 | */ |
3125 | { |
32874aea |
3126 | /* |
3127 | * First format the key into a string. |
3128 | */ |
3129 | int len = rsastr_len(&hostkey); |
3130 | char fingerprint[100]; |
3d88e64d |
3131 | char *keystr = snewn(len, char); |
32874aea |
3132 | rsastr_fmt(keystr, &hostkey); |
3133 | rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey); |
3d9449a1 |
3134 | |
3135 | ssh_set_frozen(ssh, 1); |
3136 | s->dlgret = verify_ssh_host_key(ssh->frontend, |
3137 | ssh->savedhost, ssh->savedport, |
3138 | "rsa", keystr, fingerprint, |
3139 | ssh_dialog_callback, ssh); |
32874aea |
3140 | sfree(keystr); |
3d9449a1 |
3141 | if (s->dlgret < 0) { |
3142 | do { |
3143 | crReturn(0); |
3144 | if (pktin) { |
3145 | bombout(("Unexpected data from server while waiting" |
3146 | " for user host key response")); |
3147 | crStop(0); |
3148 | } |
3149 | } while (pktin || inlen > 0); |
3150 | s->dlgret = ssh->user_response; |
3151 | } |
3152 | ssh_set_frozen(ssh, 0); |
3153 | |
3154 | if (s->dlgret == 0) { |
9e296bfa |
3155 | ssh_disconnect(ssh, "User aborted at host key verification", |
3156 | NULL, 0, TRUE); |
2b3f6c19 |
3157 | crStop(0); |
3d9449a1 |
3158 | } |
32874aea |
3159 | } |
3160 | |
3161 | for (i = 0; i < 32; i++) { |
51470298 |
3162 | s->rsabuf[i] = ssh->session_key[i]; |
374330e2 |
3163 | if (i < 16) |
51470298 |
3164 | s->rsabuf[i] ^= s->session_id[i]; |
374330e2 |
3165 | } |
3166 | |
3167 | if (hostkey.bytes > servkey.bytes) { |
0016d70b |
3168 | ret = rsaencrypt(s->rsabuf, 32, &servkey); |
3169 | if (ret) |
3170 | ret = rsaencrypt(s->rsabuf, servkey.bytes, &hostkey); |
374330e2 |
3171 | } else { |
0016d70b |
3172 | ret = rsaencrypt(s->rsabuf, 32, &hostkey); |
3173 | if (ret) |
3174 | ret = rsaencrypt(s->rsabuf, hostkey.bytes, &servkey); |
3175 | } |
3176 | if (!ret) { |
2e85c969 |
3177 | bombout(("SSH-1 public key encryptions failed due to bad formatting")); |
0016d70b |
3178 | crStop(0); |
374330e2 |
3179 | } |
3180 | |
c5e9c988 |
3181 | logevent("Encrypted session key"); |
3182 | |
ca20bfcf |
3183 | { |
3184 | int cipher_chosen = 0, warn = 0; |
3185 | char *cipher_string = NULL; |
51470298 |
3186 | int i; |
ca20bfcf |
3187 | for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) { |
86916870 |
3188 | int next_cipher = ssh->cfg.ssh_cipherlist[i]; |
ca20bfcf |
3189 | if (next_cipher == CIPHER_WARN) { |
3190 | /* If/when we choose a cipher, warn about it */ |
3191 | warn = 1; |
3192 | } else if (next_cipher == CIPHER_AES) { |
3193 | /* XXX Probably don't need to mention this. */ |
2e85c969 |
3194 | logevent("AES not supported in SSH-1, skipping"); |
ca20bfcf |
3195 | } else { |
3196 | switch (next_cipher) { |
51470298 |
3197 | case CIPHER_3DES: s->cipher_type = SSH_CIPHER_3DES; |
ca20bfcf |
3198 | cipher_string = "3DES"; break; |
51470298 |
3199 | case CIPHER_BLOWFISH: s->cipher_type = SSH_CIPHER_BLOWFISH; |
ca20bfcf |
3200 | cipher_string = "Blowfish"; break; |
51470298 |
3201 | case CIPHER_DES: s->cipher_type = SSH_CIPHER_DES; |
ca20bfcf |
3202 | cipher_string = "single-DES"; break; |
3203 | } |
51470298 |
3204 | if (s->supported_ciphers_mask & (1 << s->cipher_type)) |
ca20bfcf |
3205 | cipher_chosen = 1; |
3206 | } |
3207 | } |
3208 | if (!cipher_chosen) { |
51470298 |
3209 | if ((s->supported_ciphers_mask & (1 << SSH_CIPHER_3DES)) == 0) |
2e85c969 |
3210 | bombout(("Server violates SSH-1 protocol by not " |
ca20bfcf |
3211 | "supporting 3DES encryption")); |
3212 | else |
3213 | /* shouldn't happen */ |
6b5cf8b4 |
3214 | bombout(("No supported ciphers found")); |
7ffdbc1a |
3215 | crStop(0); |
a99a05c0 |
3216 | } |
ca20bfcf |
3217 | |
3218 | /* Warn about chosen cipher if necessary. */ |
bb348ab1 |
3219 | if (warn) { |
3d9449a1 |
3220 | ssh_set_frozen(ssh, 1); |
3221 | s->dlgret = askalg(ssh->frontend, "cipher", cipher_string, |
3222 | ssh_dialog_callback, ssh); |
3223 | if (s->dlgret < 0) { |
3224 | do { |
3225 | crReturn(0); |
3226 | if (pktin) { |
3227 | bombout(("Unexpected data from server while waiting" |
3228 | " for user response")); |
3229 | crStop(0); |
3230 | } |
3231 | } while (pktin || inlen > 0); |
3232 | s->dlgret = ssh->user_response; |
3233 | } |
3234 | ssh_set_frozen(ssh, 0); |
3235 | if (s->dlgret == 0) { |
9e296bfa |
3236 | ssh_disconnect(ssh, "User aborted at cipher warning", NULL, |
3237 | 0, TRUE); |
96ccde8b |
3238 | crStop(0); |
3d9449a1 |
3239 | } |
bb348ab1 |
3240 | } |
bea1ef5f |
3241 | } |
ca20bfcf |
3242 | |
51470298 |
3243 | switch (s->cipher_type) { |
32874aea |
3244 | case SSH_CIPHER_3DES: |
3245 | logevent("Using 3DES encryption"); |
3246 | break; |
3247 | case SSH_CIPHER_DES: |
3248 | logevent("Using single-DES encryption"); |
3249 | break; |
3250 | case SSH_CIPHER_BLOWFISH: |
3251 | logevent("Using Blowfish encryption"); |
3252 | break; |
c5e9c988 |
3253 | } |
bea1ef5f |
3254 | |
51470298 |
3255 | send_packet(ssh, SSH1_CMSG_SESSION_KEY, |
3256 | PKT_CHAR, s->cipher_type, |
32874aea |
3257 | PKT_DATA, cookie, 8, |
51470298 |
3258 | PKT_CHAR, (s->len * 8) >> 8, PKT_CHAR, (s->len * 8) & 0xFF, |
3259 | PKT_DATA, s->rsabuf, s->len, |
3260 | PKT_INT, ssh->v1_local_protoflags, PKT_END); |
fb09bf1c |
3261 | |
c5e9c988 |
3262 | logevent("Trying to enable encryption..."); |
374330e2 |
3263 | |
51470298 |
3264 | sfree(s->rsabuf); |
374330e2 |
3265 | |
51470298 |
3266 | ssh->cipher = (s->cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 : |
3267 | s->cipher_type == SSH_CIPHER_DES ? &ssh_des : |
3268 | &ssh_3des); |
371e569c |
3269 | ssh->v1_cipher_ctx = ssh->cipher->make_context(); |
3270 | ssh->cipher->sesskey(ssh->v1_cipher_ctx, ssh->session_key); |
57356d63 |
3271 | logeventf(ssh, "Initialised %s encryption", ssh->cipher->text_name); |
374330e2 |
3272 | |
0183b242 |
3273 | ssh->crcda_ctx = crcda_make_context(); |
3274 | logevent("Installing CRC compensation attack detector"); |
3275 | |
679539d7 |
3276 | if (servkey.modulus) { |
3277 | sfree(servkey.modulus); |
3278 | servkey.modulus = NULL; |
3279 | } |
3280 | if (servkey.exponent) { |
3281 | sfree(servkey.exponent); |
3282 | servkey.exponent = NULL; |
3283 | } |
3284 | if (hostkey.modulus) { |
3285 | sfree(hostkey.modulus); |
3286 | hostkey.modulus = NULL; |
3287 | } |
3288 | if (hostkey.exponent) { |
3289 | sfree(hostkey.exponent); |
3290 | hostkey.exponent = NULL; |
3291 | } |
ff3187f6 |
3292 | crWaitUntil(pktin); |
374330e2 |
3293 | |
ff3187f6 |
3294 | if (pktin->type != SSH1_SMSG_SUCCESS) { |
6b5cf8b4 |
3295 | bombout(("Encryption not successfully enabled")); |
7ffdbc1a |
3296 | crStop(0); |
8d5de777 |
3297 | } |
374330e2 |
3298 | |
c5e9c988 |
3299 | logevent("Successfully started encryption"); |
3300 | |
edd0cb8a |
3301 | fflush(stdout); /* FIXME eh? */ |
374330e2 |
3302 | { |
aa09f7d0 |
3303 | if (!*ssh->cfg.username) { |
edd0cb8a |
3304 | int ret; /* need not be kept over crReturn */ |
3305 | s->cur_prompt = new_prompts(ssh->frontend); |
3306 | s->cur_prompt->to_server = TRUE; |
3307 | s->cur_prompt->name = dupstr("SSH login name"); |
3308 | add_prompt(s->cur_prompt, dupstr("login as: "), TRUE, |
3309 | lenof(s->username)); |
3310 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
3311 | while (ret < 0) { |
51470298 |
3312 | ssh->send_ok = 1; |
edd0cb8a |
3313 | crWaitUntil(!pktin); |
3314 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
3315 | ssh->send_ok = 0; |
3316 | } |
3317 | if (!ret) { |
3318 | /* |
3319 | * Failed to get a username. Terminate. |
3320 | */ |
3321 | free_prompts(s->cur_prompt); |
3322 | ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE); |
3323 | crStop(0); |
32874aea |
3324 | } |
edd0cb8a |
3325 | memcpy(s->username, s->cur_prompt->prompts[0]->result, |
3326 | lenof(s->username)); |
3327 | free_prompts(s->cur_prompt); |
32874aea |
3328 | } else { |
86916870 |
3329 | strncpy(s->username, ssh->cfg.username, sizeof(s->username)); |
51470298 |
3330 | s->username[sizeof(s->username)-1] = '\0'; |
374330e2 |
3331 | } |
fb09bf1c |
3332 | |
51470298 |
3333 | send_packet(ssh, SSH1_CMSG_USER, PKT_STR, s->username, PKT_END); |
c5e9c988 |
3334 | { |
edd0cb8a |
3335 | char *userlog = dupprintf("Sent username \"%s\"", s->username); |
c5e9c988 |
3336 | logevent(userlog); |
32874aea |
3337 | if (flags & FLAG_INTERACTIVE && |
3338 | (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) { |
51470298 |
3339 | c_write_str(ssh, userlog); |
edd0cb8a |
3340 | c_write_str(ssh, "\r\n"); |
3c8e959b |
3341 | } |
edd0cb8a |
3342 | sfree(userlog); |
c5e9c988 |
3343 | } |
374330e2 |
3344 | } |
3345 | |
ff3187f6 |
3346 | crWaitUntil(pktin); |
374330e2 |
3347 | |
d5b2c841 |
3348 | if ((s->supported_auths_mask & (1 << SSH1_AUTH_RSA)) == 0) { |
0df73905 |
3349 | /* We must not attempt PK auth. Pretend we've already tried it. */ |
51470298 |
3350 | s->tried_publickey = s->tried_agent = 1; |
0df73905 |
3351 | } else { |
51470298 |
3352 | s->tried_publickey = s->tried_agent = 0; |
0df73905 |
3353 | } |
51470298 |
3354 | s->tis_auth_refused = s->ccard_auth_refused = 0; |
edd0cb8a |
3355 | /* |
3356 | * Load the public half of any configured keyfile for later use. |
3357 | */ |
9a30e26b |
3358 | if (!filename_is_null(ssh->cfg.keyfile)) { |
edd0cb8a |
3359 | int keytype; |
3360 | logeventf(ssh, "Reading private key file \"%.150s\"", |
3361 | filename_to_str(&ssh->cfg.keyfile)); |
3362 | keytype = key_type(&ssh->cfg.keyfile); |
3363 | if (keytype == SSH_KEYTYPE_SSH1) { |
3364 | const char *error; |
3365 | if (rsakey_pubblob(&ssh->cfg.keyfile, |
3366 | &s->publickey_blob, &s->publickey_bloblen, |
3367 | &s->publickey_comment, &error)) { |
3368 | s->publickey_encrypted = rsakey_encrypted(&ssh->cfg.keyfile, |
3369 | NULL); |
3370 | } else { |
3371 | char *msgbuf; |
3372 | logeventf(ssh, "Unable to load private key (%s)", error); |
3373 | msgbuf = dupprintf("Unable to load private key file " |
3374 | "\"%.150s\" (%s)\r\n", |
3375 | filename_to_str(&ssh->cfg.keyfile), |
3376 | error); |
3377 | c_write_str(ssh, msgbuf); |
3378 | sfree(msgbuf); |
3379 | s->publickey_blob = NULL; |
3380 | } |
3381 | } else { |
3382 | char *msgbuf; |
3383 | logeventf(ssh, "Unable to use this key file (%s)", |
3384 | key_type_to_str(keytype)); |
3385 | msgbuf = dupprintf("Unable to use key file \"%.150s\"" |
3386 | " (%s)\r\n", |
3387 | filename_to_str(&ssh->cfg.keyfile), |
3388 | key_type_to_str(keytype)); |
3389 | c_write_str(ssh, msgbuf); |
3390 | sfree(msgbuf); |
51470298 |
3391 | s->publickey_blob = NULL; |
edd0cb8a |
3392 | } |
396778f1 |
3393 | } else |
51470298 |
3394 | s->publickey_blob = NULL; |
7cca0d81 |
3395 | |
ff3187f6 |
3396 | while (pktin->type == SSH1_SMSG_FAILURE) { |
51470298 |
3397 | s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD; |
614a20a0 |
3398 | |
973612f5 |
3399 | if (ssh->cfg.tryagent && agent_exists() && !s->tried_agent) { |
32874aea |
3400 | /* |
3401 | * Attempt RSA authentication using Pageant. |
3402 | */ |
32874aea |
3403 | void *r; |
3404 | |
51470298 |
3405 | s->authed = FALSE; |
3406 | s->tried_agent = 1; |
32874aea |
3407 | logevent("Pageant is running. Requesting keys."); |
3408 | |
3409 | /* Request the keys held by the agent. */ |
51470298 |
3410 | PUT_32BIT(s->request, 1); |
3411 | s->request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
839f10db |
3412 | if (!agent_query(s->request, 5, &r, &s->responselen, |
3413 | ssh_agent_callback, ssh)) { |
3414 | do { |
3415 | crReturn(0); |
ff3187f6 |
3416 | if (pktin) { |
839f10db |
3417 | bombout(("Unexpected data from server while waiting" |
3418 | " for agent response")); |
3419 | crStop(0); |
3420 | } |
ff3187f6 |
3421 | } while (pktin || inlen > 0); |
839f10db |
3422 | r = ssh->agent_response; |
3423 | s->responselen = ssh->agent_response_len; |
3424 | } |
51470298 |
3425 | s->response = (unsigned char *) r; |
3426 | if (s->response && s->responselen >= 5 && |
3427 | s->response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) { |
3428 | s->p = s->response + 5; |
3429 | s->nkeys = GET_32BIT(s->p); |
3430 | s->p += 4; |
2e85c969 |
3431 | logeventf(ssh, "Pageant has %d SSH-1 keys", s->nkeys); |
51470298 |
3432 | for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) { |
94cd7c3a |
3433 | unsigned char *pkblob = s->p; |
51470298 |
3434 | s->p += 4; |
0016d70b |
3435 | { |
3436 | int n, ok = FALSE; |
3437 | do { /* do while (0) to make breaking easy */ |
3438 | n = ssh1_read_bignum |
3439 | (s->p, s->responselen-(s->p-s->response), |
3440 | &s->key.exponent); |
3441 | if (n < 0) |
3442 | break; |
3443 | s->p += n; |
3444 | n = ssh1_read_bignum |
3445 | (s->p, s->responselen-(s->p-s->response), |
3446 | &s->key.modulus); |
3447 | if (n < 0) |
3448 | break; |
3449 | s->p += n; |
3450 | if (s->responselen - (s->p-s->response) < 4) |
3451 | break; |
3452 | s->commentlen = GET_32BIT(s->p); |
3453 | s->p += 4; |
3454 | if (s->responselen - (s->p-s->response) < |
3455 | s->commentlen) |
3456 | break; |
3457 | s->commentp = (char *)s->p; |
3458 | s->p += s->commentlen; |
3459 | ok = TRUE; |
3460 | } while (0); |
3461 | if (!ok) { |
3462 | logevent("Pageant key list packet was truncated"); |
3463 | break; |
3464 | } |
3465 | } |
94cd7c3a |
3466 | if (s->publickey_blob) { |
3467 | if (!memcmp(pkblob, s->publickey_blob, |
3468 | s->publickey_bloblen)) { |
3469 | logeventf(ssh, "Pageant key #%d matches " |
3470 | "configured key file", s->keyi); |
3471 | s->tried_publickey = 1; |
3472 | } else |
3473 | /* Skip non-configured key */ |
3474 | continue; |
3475 | } |
3476 | logeventf(ssh, "Trying Pageant key #%d", s->keyi); |
51470298 |
3477 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
3478 | PKT_BIGNUM, s->key.modulus, PKT_END); |
ff3187f6 |
3479 | crWaitUntil(pktin); |
3480 | if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
32874aea |
3481 | logevent("Key refused"); |
3482 | continue; |
3483 | } |
3484 | logevent("Received RSA challenge"); |
ff3187f6 |
3485 | if ((s->challenge = ssh1_pkt_getmp(pktin)) == NULL) { |
0016d70b |
3486 | bombout(("Server's RSA challenge was badly formatted")); |
3487 | crStop(0); |
3488 | } |
3489 | |
32874aea |
3490 | { |
3491 | char *agentreq, *q, *ret; |
2d466ffd |
3492 | void *vret; |
32874aea |
3493 | int len, retlen; |
3494 | len = 1 + 4; /* message type, bit count */ |
51470298 |
3495 | len += ssh1_bignum_length(s->key.exponent); |
3496 | len += ssh1_bignum_length(s->key.modulus); |
3497 | len += ssh1_bignum_length(s->challenge); |
32874aea |
3498 | len += 16; /* session id */ |
3499 | len += 4; /* response format */ |
3d88e64d |
3500 | agentreq = snewn(4 + len, char); |
32874aea |
3501 | PUT_32BIT(agentreq, len); |
3502 | q = agentreq + 4; |
3503 | *q++ = SSH1_AGENTC_RSA_CHALLENGE; |
51470298 |
3504 | PUT_32BIT(q, bignum_bitcount(s->key.modulus)); |
32874aea |
3505 | q += 4; |
51470298 |
3506 | q += ssh1_write_bignum(q, s->key.exponent); |
3507 | q += ssh1_write_bignum(q, s->key.modulus); |
3508 | q += ssh1_write_bignum(q, s->challenge); |
3509 | memcpy(q, s->session_id, 16); |
32874aea |
3510 | q += 16; |
3511 | PUT_32BIT(q, 1); /* response format */ |
839f10db |
3512 | if (!agent_query(agentreq, len + 4, &vret, &retlen, |
3513 | ssh_agent_callback, ssh)) { |
3514 | sfree(agentreq); |
3515 | do { |
3516 | crReturn(0); |
ff3187f6 |
3517 | if (pktin) { |
839f10db |
3518 | bombout(("Unexpected data from server" |
3519 | " while waiting for agent" |
3520 | " response")); |
3521 | crStop(0); |
3522 | } |
ff3187f6 |
3523 | } while (pktin || inlen > 0); |
839f10db |
3524 | vret = ssh->agent_response; |
3525 | retlen = ssh->agent_response_len; |
3526 | } else |
3527 | sfree(agentreq); |
2d466ffd |
3528 | ret = vret; |
32874aea |
3529 | if (ret) { |
3530 | if (ret[4] == SSH1_AGENT_RSA_RESPONSE) { |
3531 | logevent("Sending Pageant's response"); |
51470298 |
3532 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
32874aea |
3533 | PKT_DATA, ret + 5, 16, |
3534 | PKT_END); |
3535 | sfree(ret); |
ff3187f6 |
3536 | crWaitUntil(pktin); |
3537 | if (pktin->type == SSH1_SMSG_SUCCESS) { |
32874aea |
3538 | logevent |
3539 | ("Pageant's response accepted"); |
3540 | if (flags & FLAG_VERBOSE) { |
51470298 |
3541 | c_write_str(ssh, "Authenticated using" |
3542 | " RSA key \""); |
3543 | c_write(ssh, s->commentp, |
3544 | s->commentlen); |
3545 | c_write_str(ssh, "\" from agent\r\n"); |
32874aea |
3546 | } |
51470298 |
3547 | s->authed = TRUE; |
32874aea |
3548 | } else |
3549 | logevent |
3550 | ("Pageant's response not accepted"); |
3551 | } else { |
3552 | logevent |
3553 | ("Pageant failed to answer challenge"); |
3554 | sfree(ret); |
3555 | } |
3556 | } else { |
3557 | logevent("No reply received from Pageant"); |
3558 | } |
3559 | } |
51470298 |
3560 | freebn(s->key.exponent); |
3561 | freebn(s->key.modulus); |
3562 | freebn(s->challenge); |
3563 | if (s->authed) |
32874aea |
3564 | break; |
3565 | } |
29b1d0b3 |
3566 | sfree(s->response); |
94cd7c3a |
3567 | if (s->publickey_blob && !s->tried_publickey) |
3568 | logevent("Configured key file not in Pageant"); |
32874aea |
3569 | } |
51470298 |
3570 | if (s->authed) |
32874aea |
3571 | break; |
3572 | } |
edd0cb8a |
3573 | if (s->publickey_blob && !s->tried_publickey) { |
3574 | /* |
3575 | * Try public key authentication with the specified |
3576 | * key file. |
3577 | */ |
3578 | int got_passphrase; /* need not be kept over crReturn */ |
3579 | if (flags & FLAG_VERBOSE) |
3580 | c_write_str(ssh, "Trying public key authentication.\r\n"); |
3581 | logeventf(ssh, "Trying public key \"%s\"", |
3582 | filename_to_str(&ssh->cfg.keyfile)); |
3583 | s->tried_publickey = 1; |
3584 | got_passphrase = FALSE; |
3585 | while (!got_passphrase) { |
3586 | /* |
3587 | * Get a passphrase, if necessary. |
3588 | */ |
3589 | char *passphrase = NULL; /* only written after crReturn */ |
3590 | const char *error; |
3591 | if (!s->publickey_encrypted) { |
3592 | if (flags & FLAG_VERBOSE) |
3593 | c_write_str(ssh, "No passphrase required.\r\n"); |
3594 | passphrase = NULL; |
3595 | } else { |
3596 | int ret; /* need not be kept over crReturn */ |
3597 | s->cur_prompt = new_prompts(ssh->frontend); |
3598 | s->cur_prompt->to_server = FALSE; |
3599 | s->cur_prompt->name = dupstr("SSH key passphrase"); |
3600 | add_prompt(s->cur_prompt, |
3601 | dupprintf("Passphrase for key \"%.100s\": ", |
3602 | s->publickey_comment), |
3603 | FALSE, SSH_MAX_PASSWORD_LEN); |
3604 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
3605 | while (ret < 0) { |
3606 | ssh->send_ok = 1; |
3607 | crWaitUntil(!pktin); |
3608 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
3609 | ssh->send_ok = 0; |
3610 | } |
3611 | if (!ret) { |
3612 | /* Failed to get a passphrase. Terminate. */ |
3613 | free_prompts(s->cur_prompt); |
3614 | ssh_disconnect(ssh, NULL, "Unable to authenticate", |
3615 | 0, TRUE); |
3616 | crStop(0); |
3617 | } |
3618 | passphrase = dupstr(s->cur_prompt->prompts[0]->result); |
3619 | free_prompts(s->cur_prompt); |
3620 | } |
3621 | /* |
3622 | * Try decrypting key with passphrase. |
3623 | */ |
3624 | ret = loadrsakey(&ssh->cfg.keyfile, &s->key, passphrase, |
3625 | &error); |
3626 | if (passphrase) { |
3627 | memset(passphrase, 0, strlen(passphrase)); |
3628 | sfree(passphrase); |
3629 | } |
3630 | if (ret == 1) { |
3631 | /* Correct passphrase. */ |
3632 | got_passphrase = TRUE; |
3633 | } else if (ret == 0) { |
3634 | c_write_str(ssh, "Couldn't load private key from "); |
3635 | c_write_str(ssh, filename_to_str(&ssh->cfg.keyfile)); |
3636 | c_write_str(ssh, " ("); |
3637 | c_write_str(ssh, error); |
3638 | c_write_str(ssh, ").\r\n"); |
3639 | got_passphrase = FALSE; |
3640 | break; /* go and try something else */ |
3641 | } else if (ret == -1) { |
3642 | c_write_str(ssh, "Wrong passphrase.\r\n"); /* FIXME */ |
edd0cb8a |
3643 | got_passphrase = FALSE; |
3644 | /* and try again */ |
43536490 |
3645 | } else { |
3646 | assert(0 && "unexpected return from loadrsakey()"); |
edc23a8a |
3647 | got_passphrase = FALSE; /* placate optimisers */ |
edd0cb8a |
3648 | } |
3649 | } |
3650 | |
3651 | if (got_passphrase) { |
3652 | |
3653 | /* |
3654 | * Send a public key attempt. |
3655 | */ |
3656 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
3657 | PKT_BIGNUM, s->key.modulus, PKT_END); |
3658 | |
3659 | crWaitUntil(pktin); |
3660 | if (pktin->type == SSH1_SMSG_FAILURE) { |
3661 | c_write_str(ssh, "Server refused our public key.\r\n"); |
7b575324 |
3662 | continue; /* go and try something else */ |
edd0cb8a |
3663 | } |
3664 | if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
3665 | bombout(("Bizarre response to offer of public key")); |
3666 | crStop(0); |
3667 | } |
3668 | |
3669 | { |
3670 | int i; |
3671 | unsigned char buffer[32]; |
3672 | Bignum challenge, response; |
3673 | |
3674 | if ((challenge = ssh1_pkt_getmp(pktin)) == NULL) { |
3675 | bombout(("Server's RSA challenge was badly formatted")); |
3676 | crStop(0); |
3677 | } |
3678 | response = rsadecrypt(challenge, &s->key); |
3679 | freebn(s->key.private_exponent);/* burn the evidence */ |
3680 | |
3681 | for (i = 0; i < 32; i++) { |
3682 | buffer[i] = bignum_byte(response, 31 - i); |
3683 | } |
3684 | |
3685 | MD5Init(&md5c); |
3686 | MD5Update(&md5c, buffer, 32); |
3687 | MD5Update(&md5c, s->session_id, 16); |
3688 | MD5Final(buffer, &md5c); |
3689 | |
3690 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
3691 | PKT_DATA, buffer, 16, PKT_END); |
3692 | |
3693 | freebn(challenge); |
3694 | freebn(response); |
3695 | } |
3696 | |
3697 | crWaitUntil(pktin); |
3698 | if (pktin->type == SSH1_SMSG_FAILURE) { |
3699 | if (flags & FLAG_VERBOSE) |
3700 | c_write_str(ssh, "Failed to authenticate with" |
3701 | " our public key.\r\n"); |
7b575324 |
3702 | continue; /* go and try something else */ |
edd0cb8a |
3703 | } else if (pktin->type != SSH1_SMSG_SUCCESS) { |
3704 | bombout(("Bizarre response to RSA authentication response")); |
3705 | crStop(0); |
3706 | } |
3707 | |
3708 | break; /* we're through! */ |
3709 | } |
3710 | |
3711 | } |
3712 | |
3713 | /* |
3714 | * Otherwise, try various forms of password-like authentication. |
3715 | */ |
3716 | s->cur_prompt = new_prompts(ssh->frontend); |
32874aea |
3717 | |
86916870 |
3718 | if (ssh->cfg.try_tis_auth && |
51470298 |
3719 | (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) && |
3720 | !s->tis_auth_refused) { |
3721 | s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE; |
32874aea |
3722 | logevent("Requested TIS authentication"); |
51470298 |
3723 | send_packet(ssh, SSH1_CMSG_AUTH_TIS, PKT_END); |
ff3187f6 |
3724 | crWaitUntil(pktin); |
3725 | if (pktin->type != SSH1_SMSG_AUTH_TIS_CHALLENGE) { |
32874aea |
3726 | logevent("TIS authentication declined"); |
3727 | if (flags & FLAG_INTERACTIVE) |
51470298 |
3728 | c_write_str(ssh, "TIS authentication refused.\r\n"); |
3729 | s->tis_auth_refused = 1; |
614a20a0 |
3730 | continue; |
32874aea |
3731 | } else { |
0016d70b |
3732 | char *challenge; |
3733 | int challengelen; |
edd0cb8a |
3734 | char *instr_suf, *prompt; |
0016d70b |
3735 | |
ff3187f6 |
3736 | ssh_pkt_getstring(pktin, &challenge, &challengelen); |
0016d70b |
3737 | if (!challenge) { |
3738 | bombout(("TIS challenge packet was badly formed")); |
3739 | crStop(0); |
3740 | } |
32874aea |
3741 | logevent("Received TIS challenge"); |
edd0cb8a |
3742 | s->cur_prompt->to_server = TRUE; |
3743 | s->cur_prompt->name = dupstr("SSH TIS authentication"); |
614a20a0 |
3744 | /* Prompt heuristic comes from OpenSSH */ |
edd0cb8a |
3745 | if (memchr(challenge, '\n', challengelen)) { |
3746 | instr_suf = dupstr(""); |
3747 | prompt = dupprintf("%.*s", challengelen, challenge); |
3748 | } else { |
3749 | instr_suf = dupprintf("%.*s", challengelen, challenge); |
3750 | prompt = dupstr("Response: "); |
3751 | } |
3752 | s->cur_prompt->instruction = |
3753 | dupprintf("Using TIS authentication.%s%s", |
3754 | (*instr_suf) ? "\n" : "", |
3755 | instr_suf); |
3756 | s->cur_prompt->instr_reqd = TRUE; |
3757 | add_prompt(s->cur_prompt, prompt, FALSE, SSH_MAX_PASSWORD_LEN); |
3758 | sfree(instr_suf); |
32874aea |
3759 | } |
3760 | } |
86916870 |
3761 | if (ssh->cfg.try_tis_auth && |
51470298 |
3762 | (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) && |
3763 | !s->ccard_auth_refused) { |
3764 | s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE; |
32874aea |
3765 | logevent("Requested CryptoCard authentication"); |
51470298 |
3766 | send_packet(ssh, SSH1_CMSG_AUTH_CCARD, PKT_END); |
ff3187f6 |
3767 | crWaitUntil(pktin); |
3768 | if (pktin->type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
32874aea |
3769 | logevent("CryptoCard authentication declined"); |
51470298 |
3770 | c_write_str(ssh, "CryptoCard authentication refused.\r\n"); |
3771 | s->ccard_auth_refused = 1; |
614a20a0 |
3772 | continue; |
32874aea |
3773 | } else { |
0016d70b |
3774 | char *challenge; |
3775 | int challengelen; |
edd0cb8a |
3776 | char *instr_suf, *prompt; |
0016d70b |
3777 | |
ff3187f6 |
3778 | ssh_pkt_getstring(pktin, &challenge, &challengelen); |
0016d70b |
3779 | if (!challenge) { |
3780 | bombout(("CryptoCard challenge packet was badly formed")); |
3781 | crStop(0); |
3782 | } |
32874aea |
3783 | logevent("Received CryptoCard challenge"); |
edd0cb8a |
3784 | s->cur_prompt->to_server = TRUE; |
3785 | s->cur_prompt->name = dupstr("SSH CryptoCard authentication"); |
3786 | s->cur_prompt->name_reqd = FALSE; |
3787 | /* Prompt heuristic comes from OpenSSH */ |
3788 | if (memchr(challenge, '\n', challengelen)) { |
3789 | instr_suf = dupstr(""); |
3790 | prompt = dupprintf("%.*s", challengelen, challenge); |
3791 | } else { |
3792 | instr_suf = dupprintf("%.*s", challengelen, challenge); |
3793 | prompt = dupstr("Response: "); |
3794 | } |
3795 | s->cur_prompt->instruction = |
3796 | dupprintf("Using CryptoCard authentication.%s%s", |
3797 | (*instr_suf) ? "\n" : "", |
3798 | instr_suf); |
3799 | s->cur_prompt->instr_reqd = TRUE; |
3800 | add_prompt(s->cur_prompt, prompt, FALSE, SSH_MAX_PASSWORD_LEN); |
3801 | sfree(instr_suf); |
32874aea |
3802 | } |
3803 | } |
51470298 |
3804 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
8f9e3231 |
3805 | if ((s->supported_auths_mask & (1 << SSH1_AUTH_PASSWORD)) == 0) { |
3806 | bombout(("No supported authentication methods available")); |
3807 | crStop(0); |
3808 | } |
edd0cb8a |
3809 | s->cur_prompt->to_server = TRUE; |
3810 | s->cur_prompt->name = dupstr("SSH password"); |
3811 | add_prompt(s->cur_prompt, dupprintf("%.90s@%.90s's password: ", |
3812 | s->username, ssh->savedhost), |
3813 | FALSE, SSH_MAX_PASSWORD_LEN); |
32874aea |
3814 | } |
a52f067e |
3815 | |
614a20a0 |
3816 | /* |
3817 | * Show password prompt, having first obtained it via a TIS |
3818 | * or CryptoCard exchange if we're doing TIS or CryptoCard |
3819 | * authentication. |
3820 | */ |
edd0cb8a |
3821 | { |
3822 | int ret; /* need not be kept over crReturn */ |
3823 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
3824 | while (ret < 0) { |
3825 | ssh->send_ok = 1; |
3826 | crWaitUntil(!pktin); |
3827 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
3828 | ssh->send_ok = 0; |
3829 | } |
3830 | if (!ret) { |
32874aea |
3831 | /* |
edd0cb8a |
3832 | * Failed to get a password (for example |
32874aea |
3833 | * because one was supplied on the command line |
3834 | * which has already failed to work). Terminate. |
3835 | */ |
edd0cb8a |
3836 | free_prompts(s->cur_prompt); |
3837 | ssh_disconnect(ssh, NULL, "Unable to authenticate", 0, TRUE); |
3838 | crStop(0); |
32874aea |
3839 | } |
32874aea |
3840 | } |
3841 | |
edd0cb8a |
3842 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
32874aea |
3843 | /* |
edd0cb8a |
3844 | * Defence against traffic analysis: we send a |
3845 | * whole bunch of packets containing strings of |
3846 | * different lengths. One of these strings is the |
3847 | * password, in a SSH1_CMSG_AUTH_PASSWORD packet. |
3848 | * The others are all random data in |
3849 | * SSH1_MSG_IGNORE packets. This way a passive |
3850 | * listener can't tell which is the password, and |
3851 | * hence can't deduce the password length. |
3852 | * |
3853 | * Anybody with a password length greater than 16 |
3854 | * bytes is going to have enough entropy in their |
3855 | * password that a listener won't find it _that_ |
3856 | * much help to know how long it is. So what we'll |
3857 | * do is: |
3858 | * |
3859 | * - if password length < 16, we send 15 packets |
3860 | * containing string lengths 1 through 15 |
3861 | * |
3862 | * - otherwise, we let N be the nearest multiple |
3863 | * of 8 below the password length, and send 8 |
3864 | * packets containing string lengths N through |
3865 | * N+7. This won't obscure the order of |
3866 | * magnitude of the password length, but it will |
3867 | * introduce a bit of extra uncertainty. |
3868 | * |
bf982899 |
3869 | * A few servers can't deal with SSH1_MSG_IGNORE, at |
3870 | * least in this context. For these servers, we need |
3871 | * an alternative defence. We make use of the fact |
3872 | * that the password is interpreted as a C string: |
3873 | * so we can append a NUL, then some random data. |
edd0cb8a |
3874 | * |
bf982899 |
3875 | * A few servers can deal with neither SSH1_MSG_IGNORE |
3876 | * here _nor_ a padded password string. |
3877 | * For these servers we are left with no defences |
edd0cb8a |
3878 | * against password length sniffing. |
32874aea |
3879 | */ |
bf982899 |
3880 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE) && |
3881 | !(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) { |
32874aea |
3882 | /* |
edd0cb8a |
3883 | * The server can deal with SSH1_MSG_IGNORE, so |
3884 | * we can use the primary defence. |
32874aea |
3885 | */ |
edd0cb8a |
3886 | int bottom, top, pwlen, i; |
3887 | char *randomstr; |
32874aea |
3888 | |
edd0cb8a |
3889 | pwlen = strlen(s->cur_prompt->prompts[0]->result); |
3890 | if (pwlen < 16) { |
3891 | bottom = 0; /* zero length passwords are OK! :-) */ |
3892 | top = 15; |
3893 | } else { |
3894 | bottom = pwlen & ~7; |
3895 | top = bottom + 7; |
3896 | } |
32874aea |
3897 | |
edd0cb8a |
3898 | assert(pwlen >= bottom && pwlen <= top); |
32874aea |
3899 | |
edd0cb8a |
3900 | randomstr = snewn(top + 1, char); |
32874aea |
3901 | |
edd0cb8a |
3902 | for (i = bottom; i <= top; i++) { |
3903 | if (i == pwlen) { |
3904 | defer_packet(ssh, s->pwpkt_type, |
3905 | PKTT_PASSWORD, PKT_STR, |
3906 | s->cur_prompt->prompts[0]->result, |
3907 | PKTT_OTHER, PKT_END); |
3908 | } else { |
3909 | for (j = 0; j < i; j++) { |
3910 | do { |
3911 | randomstr[j] = random_byte(); |
3912 | } while (randomstr[j] == '\0'); |
32874aea |
3913 | } |
edd0cb8a |
3914 | randomstr[i] = '\0'; |
3915 | defer_packet(ssh, SSH1_MSG_IGNORE, |
3916 | PKT_STR, randomstr, PKT_END); |
32874aea |
3917 | } |
edd0cb8a |
3918 | } |
3919 | logevent("Sending password with camouflage packets"); |
3920 | ssh_pkt_defersend(ssh); |
3921 | sfree(randomstr); |
3922 | } |
3923 | else if (!(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) { |
3924 | /* |
3925 | * The server can't deal with SSH1_MSG_IGNORE |
3926 | * but can deal with padded passwords, so we |
3927 | * can use the secondary defence. |
3928 | */ |
3929 | char string[64]; |
3930 | char *ss; |
3931 | int len; |
3932 | |
3933 | len = strlen(s->cur_prompt->prompts[0]->result); |
3934 | if (len < sizeof(string)) { |
3935 | ss = string; |
3936 | strcpy(string, s->cur_prompt->prompts[0]->result); |
3937 | len++; /* cover the zero byte */ |
3938 | while (len < sizeof(string)) { |
3939 | string[len++] = (char) random_byte(); |
bd358db1 |
3940 | } |
bd358db1 |
3941 | } else { |
edd0cb8a |
3942 | ss = s->cur_prompt->prompts[0]->result; |
32874aea |
3943 | } |
edd0cb8a |
3944 | logevent("Sending length-padded password"); |
9a10ecf4 |
3945 | send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD, |
edd0cb8a |
3946 | PKT_INT, len, PKT_DATA, ss, len, |
3947 | PKTT_OTHER, PKT_END); |
3948 | } else { |
3949 | /* |
bf982899 |
3950 | * The server is believed unable to cope with |
3951 | * any of our password camouflage methods. |
edd0cb8a |
3952 | */ |
3953 | int len; |
3954 | len = strlen(s->cur_prompt->prompts[0]->result); |
3955 | logevent("Sending unpadded password"); |
3956 | send_packet(ssh, s->pwpkt_type, |
3957 | PKTT_PASSWORD, PKT_INT, len, |
3958 | PKT_DATA, s->cur_prompt->prompts[0]->result, len, |
3959 | PKTT_OTHER, PKT_END); |
32874aea |
3960 | } |
edd0cb8a |
3961 | } else { |
3962 | send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD, |
3963 | PKT_STR, s->cur_prompt->prompts[0]->result, |
3964 | PKTT_OTHER, PKT_END); |
32874aea |
3965 | } |
c5e9c988 |
3966 | logevent("Sent password"); |
edd0cb8a |
3967 | free_prompts(s->cur_prompt); |
ff3187f6 |
3968 | crWaitUntil(pktin); |
3969 | if (pktin->type == SSH1_SMSG_FAILURE) { |
32874aea |
3970 | if (flags & FLAG_VERBOSE) |
51470298 |
3971 | c_write_str(ssh, "Access denied\r\n"); |
c5e9c988 |
3972 | logevent("Authentication refused"); |
ff3187f6 |
3973 | } else if (pktin->type != SSH1_SMSG_SUCCESS) { |
3974 | bombout(("Strange packet received, type %d", pktin->type)); |
7ffdbc1a |
3975 | crStop(0); |
374330e2 |
3976 | } |
3977 | } |
3978 | |
edd0cb8a |
3979 | /* Clear up */ |
3980 | if (s->publickey_blob) { |
3981 | sfree(s->publickey_blob); |
3982 | sfree(s->publickey_comment); |
3983 | } |
3984 | |
c5e9c988 |
3985 | logevent("Authentication successful"); |
3986 | |
fb09bf1c |
3987 | crFinish(1); |
3988 | } |
3989 | |
32874aea |
3990 | void sshfwd_close(struct ssh_channel *c) |
3991 | { |
51470298 |
3992 | Ssh ssh = c->ssh; |
3993 | |
1ef619ae |
3994 | if (ssh->state == SSH_STATE_CLOSED) |
36f94d1f |
3995 | return; |
36f94d1f |
3996 | |
80e48603 |
3997 | if (c && !c->closes) { |
4ed34d25 |
3998 | /* |
64d6ff88 |
3999 | * If halfopen is true, we have sent |
4ed34d25 |
4000 | * CHANNEL_OPEN for this channel, but it hasn't even been |
4001 | * acknowledged by the server. So we must set a close flag |
4002 | * on it now, and then when the server acks the channel |
4003 | * open, we can close it then. |
4004 | */ |
64d6ff88 |
4005 | if (!c->halfopen) { |
51470298 |
4006 | if (ssh->version == 1) { |
4007 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, |
4ed34d25 |
4008 | PKT_END); |
4009 | } else { |
ff3187f6 |
4010 | struct Packet *pktout; |
4011 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
4012 | ssh2_pkt_adduint32(pktout, c->remoteid); |
4013 | ssh2_pkt_send(ssh, pktout); |
4ed34d25 |
4014 | } |
32874aea |
4015 | } |
0357890f |
4016 | c->closes = 1; /* sent MSG_CLOSE */ |
32874aea |
4017 | if (c->type == CHAN_X11) { |
4018 | c->u.x11.s = NULL; |
d74d141c |
4019 | logevent("Forwarded X11 connection terminated"); |
4ed34d25 |
4020 | } else if (c->type == CHAN_SOCKDATA || |
4021 | c->type == CHAN_SOCKDATA_DORMANT) { |
d74d141c |
4022 | c->u.pfd.s = NULL; |
4023 | logevent("Forwarded port closed"); |
32874aea |
4024 | } |
4025 | } |
4026 | } |
4027 | |
5471d09a |
4028 | int sshfwd_write(struct ssh_channel *c, char *buf, int len) |
32874aea |
4029 | { |
51470298 |
4030 | Ssh ssh = c->ssh; |
4031 | |
1ef619ae |
4032 | if (ssh->state == SSH_STATE_CLOSED) |
36f94d1f |
4033 | return 0; |
36f94d1f |
4034 | |
51470298 |
4035 | if (ssh->version == 1) { |
4036 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
32874aea |
4037 | PKT_INT, c->remoteid, |
a2724fba |
4038 | PKT_INT, len, PKTT_DATA, PKT_DATA, buf, len, |
9a10ecf4 |
4039 | PKTT_OTHER, PKT_END); |
5471d09a |
4040 | /* |
2e85c969 |
4041 | * In SSH-1 we can return 0 here - implying that forwarded |
5471d09a |
4042 | * connections are never individually throttled - because |
4043 | * the only circumstance that can cause throttling will be |
4044 | * the whole SSH connection backing up, in which case |
4045 | * _everything_ will be throttled as a whole. |
4046 | */ |
4047 | return 0; |
783415f8 |
4048 | } else { |
32874aea |
4049 | ssh2_add_channel_data(c, buf, len); |
5471d09a |
4050 | return ssh2_try_send(c); |
4051 | } |
4052 | } |
4053 | |
4054 | void sshfwd_unthrottle(struct ssh_channel *c, int bufsize) |
4055 | { |
51470298 |
4056 | Ssh ssh = c->ssh; |
4057 | |
1ef619ae |
4058 | if (ssh->state == SSH_STATE_CLOSED) |
36f94d1f |
4059 | return; |
36f94d1f |
4060 | |
51470298 |
4061 | if (ssh->version == 1) { |
5471d09a |
4062 | if (c->v.v1.throttling && bufsize < SSH1_BUFFER_LIMIT) { |
4063 | c->v.v1.throttling = 0; |
51470298 |
4064 | ssh1_throttle(ssh, -1); |
5471d09a |
4065 | } |
4066 | } else { |
9b53e9b8 |
4067 | ssh2_set_window(c, c->v.v2.locmaxwin - bufsize); |
783415f8 |
4068 | } |
9c964e85 |
4069 | } |
4070 | |
06fadff5 |
4071 | static void ssh_queueing_handler(Ssh ssh, struct Packet *pktin) |
4072 | { |
4073 | struct queued_handler *qh = ssh->qhead; |
4074 | |
4075 | assert(qh != NULL); |
4076 | |
4077 | assert(pktin->type == qh->msg1 || pktin->type == qh->msg2); |
4078 | |
4079 | if (qh->msg1 > 0) { |
4080 | assert(ssh->packet_dispatch[qh->msg1] == ssh_queueing_handler); |
4081 | ssh->packet_dispatch[qh->msg1] = NULL; |
4082 | } |
4083 | if (qh->msg2 > 0) { |
4084 | assert(ssh->packet_dispatch[qh->msg2] == ssh_queueing_handler); |
4085 | ssh->packet_dispatch[qh->msg2] = NULL; |
4086 | } |
4087 | |
4088 | if (qh->next) { |
4089 | ssh->qhead = qh->next; |
4090 | |
4091 | if (ssh->qhead->msg1 > 0) { |
4092 | assert(ssh->packet_dispatch[ssh->qhead->msg1] == NULL); |
4093 | ssh->packet_dispatch[ssh->qhead->msg1] = ssh_queueing_handler; |
4094 | } |
4095 | if (ssh->qhead->msg2 > 0) { |
4096 | assert(ssh->packet_dispatch[ssh->qhead->msg2] == NULL); |
4097 | ssh->packet_dispatch[ssh->qhead->msg2] = ssh_queueing_handler; |
4098 | } |
4099 | } else { |
4100 | ssh->qhead = ssh->qtail = NULL; |
4101 | ssh->packet_dispatch[pktin->type] = NULL; |
4102 | } |
4103 | |
4104 | qh->handler(ssh, pktin, qh->ctx); |
4105 | |
4106 | sfree(qh); |
4107 | } |
4108 | |
4109 | static void ssh_queue_handler(Ssh ssh, int msg1, int msg2, |
4110 | chandler_fn_t handler, void *ctx) |
4111 | { |
4112 | struct queued_handler *qh; |
4113 | |
4114 | qh = snew(struct queued_handler); |
4115 | qh->msg1 = msg1; |
4116 | qh->msg2 = msg2; |
4117 | qh->handler = handler; |
4118 | qh->ctx = ctx; |
4119 | qh->next = NULL; |
4120 | |
4121 | if (ssh->qtail == NULL) { |
4122 | ssh->qhead = qh; |
4123 | |
4124 | if (qh->msg1 > 0) { |
4125 | assert(ssh->packet_dispatch[qh->msg1] == NULL); |
4126 | ssh->packet_dispatch[qh->msg1] = ssh_queueing_handler; |
4127 | } |
4128 | if (qh->msg2 > 0) { |
4129 | assert(ssh->packet_dispatch[qh->msg2] == NULL); |
4130 | ssh->packet_dispatch[qh->msg2] = ssh_queueing_handler; |
4131 | } |
4132 | } else { |
4133 | ssh->qtail->next = qh; |
4134 | } |
4135 | ssh->qtail = qh; |
4136 | } |
4137 | |
4138 | static void ssh_rportfwd_succfail(Ssh ssh, struct Packet *pktin, void *ctx) |
4139 | { |
4140 | struct ssh_rportfwd *rpf, *pf = (struct ssh_rportfwd *)ctx; |
4141 | |
4142 | if (pktin->type == (ssh->version == 1 ? SSH1_SMSG_SUCCESS : |
4143 | SSH2_MSG_REQUEST_SUCCESS)) { |
4144 | logeventf(ssh, "Remote port forwarding from %s enabled", |
4145 | pf->sportdesc); |
4146 | } else { |
4147 | logeventf(ssh, "Remote port forwarding from %s refused", |
4148 | pf->sportdesc); |
4149 | |
4150 | rpf = del234(ssh->rportfwds, pf); |
4151 | assert(rpf == pf); |
fda2feb1 |
4152 | free_rportfwd(pf); |
06fadff5 |
4153 | } |
4154 | } |
4155 | |
4156 | static void ssh_setup_portfwd(Ssh ssh, const Config *cfg) |
4157 | { |
84328ddb |
4158 | const char *portfwd_strptr = cfg->portfwd; |
4159 | struct ssh_portfwd *epf; |
4160 | int i; |
06fadff5 |
4161 | |
fda2feb1 |
4162 | if (!ssh->portfwds) { |
4163 | ssh->portfwds = newtree234(ssh_portcmp); |
4164 | } else { |
4165 | /* |
4166 | * Go through the existing port forwardings and tag them |
84328ddb |
4167 | * with status==DESTROY. Any that we want to keep will be |
4168 | * re-enabled (status==KEEP) as we go through the |
4169 | * configuration and find out which bits are the same as |
4170 | * they were before. |
fda2feb1 |
4171 | */ |
4172 | struct ssh_portfwd *epf; |
4173 | int i; |
4174 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
84328ddb |
4175 | epf->status = DESTROY; |
fda2feb1 |
4176 | } |
4177 | |
06fadff5 |
4178 | while (*portfwd_strptr) { |
84328ddb |
4179 | char address_family, type; |
4180 | int sport,dport,sserv,dserv; |
4181 | char sports[256], dports[256], saddr[256], host[256]; |
4182 | int n; |
4183 | |
05581745 |
4184 | address_family = 'A'; |
4185 | type = 'L'; |
84328ddb |
4186 | if (*portfwd_strptr == 'A' || |
4187 | *portfwd_strptr == '4' || |
4188 | *portfwd_strptr == '6') |
4189 | address_family = *portfwd_strptr++; |
4190 | if (*portfwd_strptr == 'L' || |
4191 | *portfwd_strptr == 'R' || |
4192 | *portfwd_strptr == 'D') |
4193 | type = *portfwd_strptr++; |
05581745 |
4194 | |
06fadff5 |
4195 | saddr[0] = '\0'; |
05581745 |
4196 | |
06fadff5 |
4197 | n = 0; |
4198 | while (*portfwd_strptr && *portfwd_strptr != '\t') { |
4199 | if (*portfwd_strptr == ':') { |
4200 | /* |
4201 | * We've seen a colon in the middle of the |
4202 | * source port number. This means that |
4203 | * everything we've seen until now is the |
4204 | * source _address_, so we'll move it into |
4205 | * saddr and start sports from the beginning |
4206 | * again. |
4207 | */ |
4208 | portfwd_strptr++; |
4209 | sports[n] = '\0'; |
4210 | if (ssh->version == 1 && type == 'R') { |
2e85c969 |
4211 | logeventf(ssh, "SSH-1 cannot handle remote source address " |
06fadff5 |
4212 | "spec \"%s\"; ignoring", sports); |
4213 | } else |
4214 | strcpy(saddr, sports); |
4215 | n = 0; |
4216 | } |
019164b0 |
4217 | if (n < lenof(sports)-1) sports[n++] = *portfwd_strptr++; |
06fadff5 |
4218 | } |
4219 | sports[n] = 0; |
4220 | if (type != 'D') { |
4221 | if (*portfwd_strptr == '\t') |
4222 | portfwd_strptr++; |
4223 | n = 0; |
4224 | while (*portfwd_strptr && *portfwd_strptr != ':') { |
019164b0 |
4225 | if (n < lenof(host)-1) host[n++] = *portfwd_strptr++; |
06fadff5 |
4226 | } |
4227 | host[n] = 0; |
4228 | if (*portfwd_strptr == ':') |
4229 | portfwd_strptr++; |
4230 | n = 0; |
4231 | while (*portfwd_strptr) { |
019164b0 |
4232 | if (n < lenof(dports)-1) dports[n++] = *portfwd_strptr++; |
06fadff5 |
4233 | } |
4234 | dports[n] = 0; |
4235 | portfwd_strptr++; |
4236 | dport = atoi(dports); |
4237 | dserv = 0; |
4238 | if (dport == 0) { |
4239 | dserv = 1; |
4240 | dport = net_service_lookup(dports); |
4241 | if (!dport) { |
4242 | logeventf(ssh, "Service lookup failed for destination" |
4243 | " port \"%s\"", dports); |
4244 | } |
4245 | } |
4246 | } else { |
4247 | while (*portfwd_strptr) portfwd_strptr++; |
a9e72926 |
4248 | host[0] = 0; |
4249 | dports[0] = 0; |
06fadff5 |
4250 | dport = dserv = -1; |
4251 | portfwd_strptr++; /* eat the NUL and move to next one */ |
4252 | } |
4253 | sport = atoi(sports); |
4254 | sserv = 0; |
4255 | if (sport == 0) { |
4256 | sserv = 1; |
4257 | sport = net_service_lookup(sports); |
4258 | if (!sport) { |
4259 | logeventf(ssh, "Service lookup failed for source" |
4260 | " port \"%s\"", sports); |
4261 | } |
4262 | } |
4263 | if (sport && dport) { |
4264 | /* Set up a description of the source port. */ |
fda2feb1 |
4265 | struct ssh_portfwd *pfrec, *epfrec; |
fda2feb1 |
4266 | |
4267 | pfrec = snew(struct ssh_portfwd); |
4268 | pfrec->type = type; |
4269 | pfrec->saddr = *saddr ? dupstr(saddr) : NULL; |
3fe92132 |
4270 | pfrec->sserv = sserv ? dupstr(sports) : NULL; |
fda2feb1 |
4271 | pfrec->sport = sport; |
84328ddb |
4272 | pfrec->daddr = *host ? dupstr(host) : NULL; |
3fe92132 |
4273 | pfrec->dserv = dserv ? dupstr(dports) : NULL; |
fda2feb1 |
4274 | pfrec->dport = dport; |
4275 | pfrec->local = NULL; |
4276 | pfrec->remote = NULL; |
05581745 |
4277 | pfrec->addressfamily = (address_family == '4' ? ADDRTYPE_IPV4 : |
4278 | address_family == '6' ? ADDRTYPE_IPV6 : |
4279 | ADDRTYPE_UNSPEC); |
fda2feb1 |
4280 | |
4281 | epfrec = add234(ssh->portfwds, pfrec); |
4282 | if (epfrec != pfrec) { |
4283 | /* |
4284 | * We already have a port forwarding with precisely |
4285 | * these parameters. Hence, no need to do anything; |
84328ddb |
4286 | * simply tag the existing one as KEEP. |
fda2feb1 |
4287 | */ |
84328ddb |
4288 | epfrec->status = KEEP; |
fda2feb1 |
4289 | free_portfwd(pfrec); |
84328ddb |
4290 | } else { |
4291 | pfrec->status = CREATE; |
4292 | } |
4293 | } |
4294 | } |
4295 | |
4296 | /* |
4297 | * Now go through and destroy any port forwardings which were |
4298 | * not re-enabled. |
4299 | */ |
4300 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
4301 | if (epf->status == DESTROY) { |
4302 | char *message; |
4303 | |
4304 | message = dupprintf("%s port forwarding from %s%s%d", |
4305 | epf->type == 'L' ? "local" : |
4306 | epf->type == 'R' ? "remote" : "dynamic", |
4307 | epf->saddr ? epf->saddr : "", |
4308 | epf->saddr ? ":" : "", |
4309 | epf->sport); |
4310 | |
4311 | if (epf->type != 'D') { |
4312 | char *msg2 = dupprintf("%s to %s:%d", message, |
4313 | epf->daddr, epf->dport); |
4314 | sfree(message); |
4315 | message = msg2; |
4316 | } |
4317 | |
4318 | logeventf(ssh, "Cancelling %s", message); |
4319 | sfree(message); |
4320 | |
4321 | if (epf->remote) { |
4322 | struct ssh_rportfwd *rpf = epf->remote; |
4323 | struct Packet *pktout; |
4324 | |
4325 | /* |
4326 | * Cancel the port forwarding at the server |
4327 | * end. |
4328 | */ |
4329 | if (ssh->version == 1) { |
4330 | /* |
4331 | * We cannot cancel listening ports on the |
2e85c969 |
4332 | * server side in SSH-1! There's no message |
84328ddb |
4333 | * to support it. Instead, we simply remove |
4334 | * the rportfwd record from the local end |
4335 | * so that any connections the server tries |
4336 | * to make on it are rejected. |
4337 | */ |
4338 | } else { |
4339 | pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST); |
4340 | ssh2_pkt_addstring(pktout, "cancel-tcpip-forward"); |
4341 | ssh2_pkt_addbool(pktout, 0);/* _don't_ want reply */ |
4342 | if (epf->saddr) { |
4343 | ssh2_pkt_addstring(pktout, epf->saddr); |
4344 | } else if (ssh->cfg.rport_acceptall) { |
5188540b |
4345 | /* XXX: ssh->cfg.rport_acceptall may not represent |
4346 | * what was used to open the original connection, |
4347 | * since it's reconfigurable. */ |
84328ddb |
4348 | ssh2_pkt_addstring(pktout, "0.0.0.0"); |
4349 | } else { |
4350 | ssh2_pkt_addstring(pktout, "127.0.0.1"); |
4351 | } |
4352 | ssh2_pkt_adduint32(pktout, epf->sport); |
4353 | ssh2_pkt_send(ssh, pktout); |
4354 | } |
4355 | |
4356 | del234(ssh->rportfwds, rpf); |
4357 | free_rportfwd(rpf); |
4358 | } else if (epf->local) { |
4359 | pfd_terminate(epf->local); |
4360 | } |
4361 | |
4362 | delpos234(ssh->portfwds, i); |
4363 | free_portfwd(epf); |
4364 | i--; /* so we don't skip one in the list */ |
4365 | } |
4366 | |
4367 | /* |
4368 | * And finally, set up any new port forwardings (status==CREATE). |
4369 | */ |
4370 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
4371 | if (epf->status == CREATE) { |
4372 | char *sportdesc, *dportdesc; |
3fe92132 |
4373 | sportdesc = dupprintf("%s%s%s%s%d%s", |
84328ddb |
4374 | epf->saddr ? epf->saddr : "", |
4375 | epf->saddr ? ":" : "", |
3fe92132 |
4376 | epf->sserv ? epf->sserv : "", |
4377 | epf->sserv ? "(" : "", |
4378 | epf->sport, |
4379 | epf->sserv ? ")" : ""); |
84328ddb |
4380 | if (epf->type == 'D') { |
4381 | dportdesc = NULL; |
4382 | } else { |
3fe92132 |
4383 | dportdesc = dupprintf("%s:%s%s%d%s", |
4384 | epf->daddr, |
4385 | epf->dserv ? epf->dserv : "", |
4386 | epf->dserv ? "(" : "", |
4387 | epf->dport, |
4388 | epf->dserv ? ")" : ""); |
84328ddb |
4389 | } |
05581745 |
4390 | |
84328ddb |
4391 | if (epf->type == 'L') { |
4392 | const char *err = pfd_addforward(epf->daddr, epf->dport, |
4393 | epf->saddr, epf->sport, |
5188540b |
4394 | ssh, cfg, |
84328ddb |
4395 | &epf->local, |
4396 | epf->addressfamily); |
4397 | |
4398 | logeventf(ssh, "Local %sport %s forwarding to %s%s%s", |
4399 | epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " : |
4400 | epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "", |
4401 | sportdesc, dportdesc, |
4402 | err ? " failed: " : "", err ? err : ""); |
4403 | } else if (epf->type == 'D') { |
06fadff5 |
4404 | const char *err = pfd_addforward(NULL, -1, |
84328ddb |
4405 | epf->saddr, epf->sport, |
5188540b |
4406 | ssh, cfg, |
84328ddb |
4407 | &epf->local, |
4408 | epf->addressfamily); |
4409 | |
4410 | logeventf(ssh, "Local %sport %s SOCKS dynamic forwarding%s%s", |
4411 | epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " : |
4412 | epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "", |
4413 | sportdesc, |
4414 | err ? " failed: " : "", err ? err : ""); |
06fadff5 |
4415 | } else { |
4416 | struct ssh_rportfwd *pf; |
4417 | |
4418 | /* |
4419 | * Ensure the remote port forwardings tree exists. |
4420 | */ |
4421 | if (!ssh->rportfwds) { |
4422 | if (ssh->version == 1) |
4423 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh1); |
4424 | else |
4425 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh2); |
4426 | } |
4427 | |
4428 | pf = snew(struct ssh_rportfwd); |
84328ddb |
4429 | strncpy(pf->dhost, epf->daddr, lenof(pf->dhost)-1); |
4430 | pf->dhost[lenof(pf->dhost)-1] = '\0'; |
4431 | pf->dport = epf->dport; |
4432 | pf->sport = epf->sport; |
06fadff5 |
4433 | if (add234(ssh->rportfwds, pf) != pf) { |
4434 | logeventf(ssh, "Duplicate remote port forwarding to %s:%d", |
84328ddb |
4435 | epf->daddr, epf->dport); |
06fadff5 |
4436 | sfree(pf); |
4437 | } else { |
4438 | logeventf(ssh, "Requesting remote port %s" |
84328ddb |
4439 | " forward to %s", sportdesc, dportdesc); |
06fadff5 |
4440 | |
4441 | pf->sportdesc = sportdesc; |
4442 | sportdesc = NULL; |
84328ddb |
4443 | epf->remote = pf; |
4444 | pf->pfrec = epf; |
06fadff5 |
4445 | |
4446 | if (ssh->version == 1) { |
4447 | send_packet(ssh, SSH1_CMSG_PORT_FORWARD_REQUEST, |
84328ddb |
4448 | PKT_INT, epf->sport, |
4449 | PKT_STR, epf->daddr, |
4450 | PKT_INT, epf->dport, |
06fadff5 |
4451 | PKT_END); |
4452 | ssh_queue_handler(ssh, SSH1_SMSG_SUCCESS, |
4453 | SSH1_SMSG_FAILURE, |
4454 | ssh_rportfwd_succfail, pf); |
4455 | } else { |
4456 | struct Packet *pktout; |
4457 | pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST); |
4458 | ssh2_pkt_addstring(pktout, "tcpip-forward"); |
4459 | ssh2_pkt_addbool(pktout, 1);/* want reply */ |
84328ddb |
4460 | if (epf->saddr) { |
4461 | ssh2_pkt_addstring(pktout, epf->saddr); |
5188540b |
4462 | } else if (cfg->rport_acceptall) { |
06fadff5 |
4463 | ssh2_pkt_addstring(pktout, "0.0.0.0"); |
4464 | } else { |
4465 | ssh2_pkt_addstring(pktout, "127.0.0.1"); |
4466 | } |
84328ddb |
4467 | ssh2_pkt_adduint32(pktout, epf->sport); |
06fadff5 |
4468 | ssh2_pkt_send(ssh, pktout); |
4469 | |
4470 | ssh_queue_handler(ssh, SSH2_MSG_REQUEST_SUCCESS, |
4471 | SSH2_MSG_REQUEST_FAILURE, |
4472 | ssh_rportfwd_succfail, pf); |
4473 | } |
4474 | } |
4475 | } |
4476 | sfree(sportdesc); |
84328ddb |
4477 | sfree(dportdesc); |
06fadff5 |
4478 | } |
06fadff5 |
4479 | } |
4480 | |
51df0ab5 |
4481 | static void ssh1_smsg_stdout_stderr_data(Ssh ssh, struct Packet *pktin) |
4482 | { |
4483 | char *string; |
4484 | int stringlen, bufsize; |
4485 | |
4486 | ssh_pkt_getstring(pktin, &string, &stringlen); |
4487 | if (string == NULL) { |
4488 | bombout(("Incoming terminal data packet was badly formed")); |
4489 | return; |
4490 | } |
4491 | |
4492 | bufsize = from_backend(ssh->frontend, pktin->type == SSH1_SMSG_STDERR_DATA, |
4493 | string, stringlen); |
4494 | if (!ssh->v1_stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) { |
4495 | ssh->v1_stdout_throttling = 1; |
4496 | ssh1_throttle(ssh, +1); |
4497 | } |
4498 | } |
4499 | |
4500 | static void ssh1_smsg_x11_open(Ssh ssh, struct Packet *pktin) |
4501 | { |
4502 | /* Remote side is trying to open a channel to talk to our |
4503 | * X-Server. Give them back a local channel number. */ |
4504 | struct ssh_channel *c; |
4505 | int remoteid = ssh_pkt_getuint32(pktin); |
4506 | |
4507 | logevent("Received X11 connect request"); |
4508 | /* Refuse if X11 forwarding is disabled. */ |
4509 | if (!ssh->X11_fwd_enabled) { |
4510 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4511 | PKT_INT, remoteid, PKT_END); |
4512 | logevent("Rejected X11 connect request"); |
4513 | } else { |
4514 | c = snew(struct ssh_channel); |
4515 | c->ssh = ssh; |
4516 | |
4517 | if (x11_init(&c->u.x11.s, ssh->cfg.x11_display, c, |
4518 | ssh->x11auth, NULL, -1, &ssh->cfg) != NULL) { |
4519 | logevent("Opening X11 forward connection failed"); |
4520 | sfree(c); |
4521 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4522 | PKT_INT, remoteid, PKT_END); |
4523 | } else { |
4524 | logevent |
4525 | ("Opening X11 forward connection succeeded"); |
4526 | c->remoteid = remoteid; |
64d6ff88 |
4527 | c->halfopen = FALSE; |
51df0ab5 |
4528 | c->localid = alloc_channel_id(ssh); |
4529 | c->closes = 0; |
4530 | c->v.v1.throttling = 0; |
4531 | c->type = CHAN_X11; /* identify channel type */ |
4532 | add234(ssh->channels, c); |
4533 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
4534 | PKT_INT, c->remoteid, PKT_INT, |
4535 | c->localid, PKT_END); |
4536 | logevent("Opened X11 forward channel"); |
4537 | } |
4538 | } |
4539 | } |
4540 | |
4541 | static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin) |
4542 | { |
4543 | /* Remote side is trying to open a channel to talk to our |
4544 | * agent. Give them back a local channel number. */ |
4545 | struct ssh_channel *c; |
4546 | int remoteid = ssh_pkt_getuint32(pktin); |
4547 | |
4548 | /* Refuse if agent forwarding is disabled. */ |
4549 | if (!ssh->agentfwd_enabled) { |
4550 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4551 | PKT_INT, remoteid, PKT_END); |
4552 | } else { |
4553 | c = snew(struct ssh_channel); |
4554 | c->ssh = ssh; |
4555 | c->remoteid = remoteid; |
64d6ff88 |
4556 | c->halfopen = FALSE; |
51df0ab5 |
4557 | c->localid = alloc_channel_id(ssh); |
4558 | c->closes = 0; |
4559 | c->v.v1.throttling = 0; |
4560 | c->type = CHAN_AGENT; /* identify channel type */ |
4561 | c->u.a.lensofar = 0; |
4562 | add234(ssh->channels, c); |
4563 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
4564 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
4565 | PKT_END); |
4566 | } |
4567 | } |
4568 | |
4569 | static void ssh1_msg_port_open(Ssh ssh, struct Packet *pktin) |
4570 | { |
4571 | /* Remote side is trying to open a channel to talk to a |
4572 | * forwarded port. Give them back a local channel number. */ |
4573 | struct ssh_channel *c; |
05581745 |
4574 | struct ssh_rportfwd pf, *pfp; |
51df0ab5 |
4575 | int remoteid; |
4576 | int hostsize, port; |
fb983202 |
4577 | char *host; |
51df0ab5 |
4578 | const char *e; |
4579 | c = snew(struct ssh_channel); |
4580 | c->ssh = ssh; |
4581 | |
4582 | remoteid = ssh_pkt_getuint32(pktin); |
4583 | ssh_pkt_getstring(pktin, &host, &hostsize); |
4584 | port = ssh_pkt_getuint32(pktin); |
4585 | |
4586 | if (hostsize >= lenof(pf.dhost)) |
4587 | hostsize = lenof(pf.dhost)-1; |
4588 | memcpy(pf.dhost, host, hostsize); |
4589 | pf.dhost[hostsize] = '\0'; |
4590 | pf.dport = port; |
05581745 |
4591 | pfp = find234(ssh->rportfwds, &pf, NULL); |
51df0ab5 |
4592 | |
05581745 |
4593 | if (pfp == NULL) { |
fb983202 |
4594 | logeventf(ssh, "Rejected remote port open request for %s:%d", |
4595 | pf.dhost, port); |
51df0ab5 |
4596 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4597 | PKT_INT, remoteid, PKT_END); |
4598 | } else { |
fb983202 |
4599 | logeventf(ssh, "Received remote port open request for %s:%d", |
4600 | pf.dhost, port); |
51df0ab5 |
4601 | e = pfd_newconnect(&c->u.pfd.s, pf.dhost, port, |
05581745 |
4602 | c, &ssh->cfg, pfp->pfrec->addressfamily); |
51df0ab5 |
4603 | if (e != NULL) { |
fb983202 |
4604 | logeventf(ssh, "Port open failed: %s", e); |
51df0ab5 |
4605 | sfree(c); |
4606 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4607 | PKT_INT, remoteid, PKT_END); |
4608 | } else { |
4609 | c->remoteid = remoteid; |
64d6ff88 |
4610 | c->halfopen = FALSE; |
51df0ab5 |
4611 | c->localid = alloc_channel_id(ssh); |
4612 | c->closes = 0; |
4613 | c->v.v1.throttling = 0; |
4614 | c->type = CHAN_SOCKDATA; /* identify channel type */ |
4615 | add234(ssh->channels, c); |
4616 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
4617 | PKT_INT, c->remoteid, PKT_INT, |
4618 | c->localid, PKT_END); |
4619 | logevent("Forwarded port opened successfully"); |
4620 | } |
4621 | } |
4622 | } |
4623 | |
4624 | static void ssh1_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin) |
4625 | { |
4626 | unsigned int remoteid = ssh_pkt_getuint32(pktin); |
4627 | unsigned int localid = ssh_pkt_getuint32(pktin); |
4628 | struct ssh_channel *c; |
4629 | |
4630 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
4631 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
4632 | c->remoteid = localid; |
64d6ff88 |
4633 | c->halfopen = FALSE; |
51df0ab5 |
4634 | c->type = CHAN_SOCKDATA; |
4635 | c->v.v1.throttling = 0; |
4636 | pfd_confirm(c->u.pfd.s); |
4637 | } |
4638 | |
4639 | if (c && c->closes) { |
4640 | /* |
4641 | * We have a pending close on this channel, |
4642 | * which we decided on before the server acked |
4643 | * the channel open. So now we know the |
4644 | * remoteid, we can close it again. |
4645 | */ |
4646 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, |
4647 | PKT_INT, c->remoteid, PKT_END); |
4648 | } |
4649 | } |
4650 | |
4651 | static void ssh1_msg_channel_open_failure(Ssh ssh, struct Packet *pktin) |
4652 | { |
4653 | unsigned int remoteid = ssh_pkt_getuint32(pktin); |
4654 | struct ssh_channel *c; |
4655 | |
4656 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
4657 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
4658 | logevent("Forwarded connection refused by server"); |
4659 | pfd_close(c->u.pfd.s); |
4660 | del234(ssh->channels, c); |
4661 | sfree(c); |
4662 | } |
4663 | } |
4664 | |
4665 | static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin) |
4666 | { |
4667 | /* Remote side closes a channel. */ |
4668 | unsigned i = ssh_pkt_getuint32(pktin); |
4669 | struct ssh_channel *c; |
4670 | c = find234(ssh->channels, &i, ssh_channelfind); |
64d6ff88 |
4671 | if (c && !c->halfopen) { |
51df0ab5 |
4672 | int closetype; |
4673 | closetype = |
4674 | (pktin->type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2); |
4675 | |
4676 | if ((c->closes == 0) && (c->type == CHAN_X11)) { |
4677 | logevent("Forwarded X11 connection terminated"); |
4678 | assert(c->u.x11.s != NULL); |
4679 | x11_close(c->u.x11.s); |
4680 | c->u.x11.s = NULL; |
4681 | } |
4682 | if ((c->closes == 0) && (c->type == CHAN_SOCKDATA)) { |
4683 | logevent("Forwarded port closed"); |
4684 | assert(c->u.pfd.s != NULL); |
4685 | pfd_close(c->u.pfd.s); |
4686 | c->u.pfd.s = NULL; |
4687 | } |
4688 | |
4689 | c->closes |= (closetype << 2); /* seen this message */ |
4690 | if (!(c->closes & closetype)) { |
4691 | send_packet(ssh, pktin->type, PKT_INT, c->remoteid, |
4692 | PKT_END); |
4693 | c->closes |= closetype; /* sent it too */ |
4694 | } |
4695 | |
4696 | if (c->closes == 15) { |
4697 | del234(ssh->channels, c); |
4698 | sfree(c); |
4699 | } |
4700 | } else { |
4701 | bombout(("Received CHANNEL_CLOSE%s for %s channel %d\n", |
4702 | pktin->type == SSH1_MSG_CHANNEL_CLOSE ? "" : |
4703 | "_CONFIRMATION", c ? "half-open" : "nonexistent", |
4704 | i)); |
4705 | } |
4706 | } |
4707 | |
4708 | static void ssh1_msg_channel_data(Ssh ssh, struct Packet *pktin) |
4709 | { |
4710 | /* Data sent down one of our channels. */ |
4711 | int i = ssh_pkt_getuint32(pktin); |
4712 | char *p; |
6d44acc9 |
4713 | int len; |
51df0ab5 |
4714 | struct ssh_channel *c; |
4715 | |
4716 | ssh_pkt_getstring(pktin, &p, &len); |
4717 | |
4718 | c = find234(ssh->channels, &i, ssh_channelfind); |
4719 | if (c) { |
4720 | int bufsize = 0; |
4721 | switch (c->type) { |
4722 | case CHAN_X11: |
4723 | bufsize = x11_send(c->u.x11.s, p, len); |
4724 | break; |
4725 | case CHAN_SOCKDATA: |
4726 | bufsize = pfd_send(c->u.pfd.s, p, len); |
4727 | break; |
4728 | case CHAN_AGENT: |
4729 | /* Data for an agent message. Buffer it. */ |
4730 | while (len > 0) { |
4731 | if (c->u.a.lensofar < 4) { |
62ddb51e |
4732 | unsigned int l = min(4 - c->u.a.lensofar, (unsigned)len); |
51df0ab5 |
4733 | memcpy(c->u.a.msglen + c->u.a.lensofar, p, |
4734 | l); |
4735 | p += l; |
4736 | len -= l; |
4737 | c->u.a.lensofar += l; |
4738 | } |
4739 | if (c->u.a.lensofar == 4) { |
4740 | c->u.a.totallen = |
4741 | 4 + GET_32BIT(c->u.a.msglen); |
4742 | c->u.a.message = snewn(c->u.a.totallen, |
4743 | unsigned char); |
4744 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
4745 | } |
4746 | if (c->u.a.lensofar >= 4 && len > 0) { |
aa63ab7e |
4747 | unsigned int l = |
51df0ab5 |
4748 | min(c->u.a.totallen - c->u.a.lensofar, |
62ddb51e |
4749 | (unsigned)len); |
51df0ab5 |
4750 | memcpy(c->u.a.message + c->u.a.lensofar, p, |
4751 | l); |
4752 | p += l; |
4753 | len -= l; |
4754 | c->u.a.lensofar += l; |
4755 | } |
4756 | if (c->u.a.lensofar == c->u.a.totallen) { |
4757 | void *reply; |
4758 | int replylen; |
4759 | if (agent_query(c->u.a.message, |
4760 | c->u.a.totallen, |
4761 | &reply, &replylen, |
4762 | ssh_agentf_callback, c)) |
4763 | ssh_agentf_callback(c, reply, replylen); |
4764 | sfree(c->u.a.message); |
4765 | c->u.a.lensofar = 0; |
4766 | } |
4767 | } |
4768 | bufsize = 0; /* agent channels never back up */ |
4769 | break; |
4770 | } |
4771 | if (!c->v.v1.throttling && bufsize > SSH1_BUFFER_LIMIT) { |
4772 | c->v.v1.throttling = 1; |
4773 | ssh1_throttle(ssh, +1); |
4774 | } |
4775 | } |
4776 | } |
4777 | |
4778 | static void ssh1_smsg_exit_status(Ssh ssh, struct Packet *pktin) |
4779 | { |
51df0ab5 |
4780 | ssh->exitcode = ssh_pkt_getuint32(pktin); |
fb983202 |
4781 | logeventf(ssh, "Server sent command exit status %d", ssh->exitcode); |
51df0ab5 |
4782 | send_packet(ssh, SSH1_CMSG_EXIT_CONFIRMATION, PKT_END); |
4783 | /* |
4784 | * In case `helpful' firewalls or proxies tack |
4785 | * extra human-readable text on the end of the |
4786 | * session which we might mistake for another |
4787 | * encrypted packet, we close the session once |
4788 | * we've sent EXIT_CONFIRMATION. |
4789 | */ |
9e296bfa |
4790 | ssh_disconnect(ssh, NULL, NULL, 0, TRUE); |
51df0ab5 |
4791 | } |
4792 | |
c6ccd5c2 |
4793 | /* Helper function to deal with sending tty modes for REQUEST_PTY */ |
4794 | static void ssh1_send_ttymode(void *data, char *mode, char *val) |
4795 | { |
4796 | struct Packet *pktout = (struct Packet *)data; |
4797 | int i = 0; |
4798 | unsigned int arg = 0; |
4799 | while (strcmp(mode, ssh_ttymodes[i].mode) != 0) i++; |
4800 | if (i == lenof(ssh_ttymodes)) return; |
4801 | switch (ssh_ttymodes[i].type) { |
4802 | case TTY_OP_CHAR: |
4803 | arg = ssh_tty_parse_specchar(val); |
4804 | break; |
4805 | case TTY_OP_BOOL: |
4806 | arg = ssh_tty_parse_boolean(val); |
4807 | break; |
4808 | } |
4809 | ssh2_pkt_addbyte(pktout, ssh_ttymodes[i].opcode); |
4810 | ssh2_pkt_addbyte(pktout, arg); |
4811 | } |
4812 | |
4813 | |
b09eaa88 |
4814 | static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen, |
4815 | struct Packet *pktin) |
32874aea |
4816 | { |
b09eaa88 |
4817 | crBegin(ssh->do_ssh1_connection_crstate); |
fb09bf1c |
4818 | |
51df0ab5 |
4819 | ssh->packet_dispatch[SSH1_SMSG_STDOUT_DATA] = |
4820 | ssh->packet_dispatch[SSH1_SMSG_STDERR_DATA] = |
4821 | ssh1_smsg_stdout_stderr_data; |
4822 | |
4823 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_CONFIRMATION] = |
4824 | ssh1_msg_channel_open_confirmation; |
4825 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_FAILURE] = |
4826 | ssh1_msg_channel_open_failure; |
4827 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE] = |
4828 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION] = |
4829 | ssh1_msg_channel_close; |
4830 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_DATA] = ssh1_msg_channel_data; |
4831 | ssh->packet_dispatch[SSH1_SMSG_EXIT_STATUS] = ssh1_smsg_exit_status; |
4832 | |
86916870 |
4833 | if (ssh->cfg.agentfwd && agent_exists()) { |
32874aea |
4834 | logevent("Requesting agent forwarding"); |
51470298 |
4835 | send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END); |
32874aea |
4836 | do { |
4837 | crReturnV; |
ff3187f6 |
4838 | } while (!pktin); |
4839 | if (pktin->type != SSH1_SMSG_SUCCESS |
4840 | && pktin->type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
4841 | bombout(("Protocol confusion")); |
7ffdbc1a |
4842 | crStopV; |
ff3187f6 |
4843 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
32874aea |
4844 | logevent("Agent forwarding refused"); |
4845 | } else { |
4846 | logevent("Agent forwarding enabled"); |
51470298 |
4847 | ssh->agentfwd_enabled = TRUE; |
51df0ab5 |
4848 | ssh->packet_dispatch[SSH1_SMSG_AGENT_OPEN] = ssh1_smsg_agent_open; |
db7d555c |
4849 | } |
dacbd0e8 |
4850 | } |
4851 | |
86916870 |
4852 | if (ssh->cfg.x11_forward) { |
32874aea |
4853 | char proto[20], data[64]; |
4854 | logevent("Requesting X11 forwarding"); |
302121de |
4855 | ssh->x11auth = x11_invent_auth(proto, sizeof(proto), |
86916870 |
4856 | data, sizeof(data), ssh->cfg.x11_auth); |
4857 | x11_get_real_auth(ssh->x11auth, ssh->cfg.x11_display); |
f68353be |
4858 | /* |
4859 | * Note that while we blank the X authentication data here, we don't |
4860 | * take any special action to blank the start of an X11 channel, |
4861 | * so using MIT-MAGIC-COOKIE-1 and actually opening an X connection |
4862 | * without having session blanking enabled is likely to leak your |
4863 | * cookie into the log. |
4864 | */ |
51470298 |
4865 | if (ssh->v1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) { |
4866 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
f68353be |
4867 | PKT_STR, proto, |
4868 | PKTT_PASSWORD, PKT_STR, data, PKTT_OTHER, |
86916870 |
4869 | PKT_INT, x11_get_screen_number(ssh->cfg.x11_display), |
421d6835 |
4870 | PKT_END); |
32874aea |
4871 | } else { |
51470298 |
4872 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
f68353be |
4873 | PKT_STR, proto, |
4874 | PKTT_PASSWORD, PKT_STR, data, PKTT_OTHER, PKT_END); |
32874aea |
4875 | } |
4876 | do { |
4877 | crReturnV; |
ff3187f6 |
4878 | } while (!pktin); |
4879 | if (pktin->type != SSH1_SMSG_SUCCESS |
4880 | && pktin->type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
4881 | bombout(("Protocol confusion")); |
7ffdbc1a |
4882 | crStopV; |
ff3187f6 |
4883 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
32874aea |
4884 | logevent("X11 forwarding refused"); |
4885 | } else { |
4886 | logevent("X11 forwarding enabled"); |
51470298 |
4887 | ssh->X11_fwd_enabled = TRUE; |
51df0ab5 |
4888 | ssh->packet_dispatch[SSH1_SMSG_X11_OPEN] = ssh1_smsg_x11_open; |
9c964e85 |
4889 | } |
4890 | } |
4891 | |
06fadff5 |
4892 | ssh_setup_portfwd(ssh, &ssh->cfg); |
4893 | ssh->packet_dispatch[SSH1_MSG_PORT_OPEN] = ssh1_msg_port_open; |
d74d141c |
4894 | |
86916870 |
4895 | if (!ssh->cfg.nopty) { |
c6ccd5c2 |
4896 | struct Packet *pkt; |
a5dd8467 |
4897 | /* Unpick the terminal-speed string. */ |
4898 | /* XXX perhaps we should allow no speeds to be sent. */ |
db219738 |
4899 | ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */ |
4900 | sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed); |
a5dd8467 |
4901 | /* Send the pty request. */ |
c6ccd5c2 |
4902 | pkt = ssh1_pkt_init(SSH1_CMSG_REQUEST_PTY); |
4903 | ssh_pkt_addstring(pkt, ssh->cfg.termtype); |
4904 | ssh_pkt_adduint32(pkt, ssh->term_height); |
4905 | ssh_pkt_adduint32(pkt, ssh->term_width); |
4906 | ssh_pkt_adduint32(pkt, 0); /* width in pixels */ |
4907 | ssh_pkt_adduint32(pkt, 0); /* height in pixels */ |
4908 | parse_ttymodes(ssh, ssh->cfg.ttymodes, |
4909 | ssh1_send_ttymode, (void *)pkt); |
4910 | ssh_pkt_addbyte(pkt, SSH1_TTY_OP_ISPEED); |
4911 | ssh_pkt_adduint32(pkt, ssh->ispeed); |
4912 | ssh_pkt_addbyte(pkt, SSH1_TTY_OP_OSPEED); |
4913 | ssh_pkt_adduint32(pkt, ssh->ospeed); |
4914 | ssh_pkt_addbyte(pkt, SSH_TTY_OP_END); |
4915 | s_wrpkt(ssh, pkt); |
51470298 |
4916 | ssh->state = SSH_STATE_INTERMED; |
32874aea |
4917 | do { |
4918 | crReturnV; |
ff3187f6 |
4919 | } while (!pktin); |
4920 | if (pktin->type != SSH1_SMSG_SUCCESS |
4921 | && pktin->type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
4922 | bombout(("Protocol confusion")); |
7ffdbc1a |
4923 | crStopV; |
ff3187f6 |
4924 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
51470298 |
4925 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
4926 | ssh->editing = ssh->echoing = 1; |
32874aea |
4927 | } |
a5dd8467 |
4928 | logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)", |
db219738 |
4929 | ssh->ospeed, ssh->ispeed); |
0965bee0 |
4930 | } else { |
51470298 |
4931 | ssh->editing = ssh->echoing = 1; |
374330e2 |
4932 | } |
4933 | |
86916870 |
4934 | if (ssh->cfg.compression) { |
51470298 |
4935 | send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END); |
32874aea |
4936 | do { |
4937 | crReturnV; |
ff3187f6 |
4938 | } while (!pktin); |
4939 | if (pktin->type != SSH1_SMSG_SUCCESS |
4940 | && pktin->type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
4941 | bombout(("Protocol confusion")); |
7ffdbc1a |
4942 | crStopV; |
ff3187f6 |
4943 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
51470298 |
4944 | c_write_str(ssh, "Server refused to compress\r\n"); |
32874aea |
4945 | } |
4ba9b64b |
4946 | logevent("Started compression"); |
51470298 |
4947 | ssh->v1_compressing = TRUE; |
5366aed8 |
4948 | ssh->cs_comp_ctx = zlib_compress_init(); |
4949 | logevent("Initialised zlib (RFC1950) compression"); |
4950 | ssh->sc_comp_ctx = zlib_decompress_init(); |
4951 | logevent("Initialised zlib (RFC1950) decompression"); |
4ba9b64b |
4952 | } |
4953 | |
fd5e5847 |
4954 | /* |
4955 | * Start the shell or command. |
4956 | * |
2e85c969 |
4957 | * Special case: if the first-choice command is an SSH-2 |
fd5e5847 |
4958 | * subsystem (hence not usable here) and the second choice |
4959 | * exists, we fall straight back to that. |
4960 | */ |
4961 | { |
86916870 |
4962 | char *cmd = ssh->cfg.remote_cmd_ptr; |
04c52f10 |
4963 | |
4964 | if (!cmd) cmd = ssh->cfg.remote_cmd; |
fd5e5847 |
4965 | |
86916870 |
4966 | if (ssh->cfg.ssh_subsys && ssh->cfg.remote_cmd_ptr2) { |
4967 | cmd = ssh->cfg.remote_cmd_ptr2; |
51470298 |
4968 | ssh->fallback_cmd = TRUE; |
fd5e5847 |
4969 | } |
4970 | if (*cmd) |
51470298 |
4971 | send_packet(ssh, SSH1_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END); |
fd5e5847 |
4972 | else |
51470298 |
4973 | send_packet(ssh, SSH1_CMSG_EXEC_SHELL, PKT_END); |
fd5e5847 |
4974 | logevent("Started session"); |
4975 | } |
374330e2 |
4976 | |
51470298 |
4977 | ssh->state = SSH_STATE_SESSION; |
4978 | if (ssh->size_needed) |
4979 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
4980 | if (ssh->eof_needed) |
4981 | ssh_special(ssh, TS_EOF); |
374330e2 |
4982 | |
b9d7bcad |
4983 | if (ssh->ldisc) |
4984 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
51470298 |
4985 | ssh->send_ok = 1; |
4986 | ssh->channels = newtree234(ssh_channelcmp); |
374330e2 |
4987 | while (1) { |
d74d141c |
4988 | |
51df0ab5 |
4989 | /* |
4990 | * By this point, most incoming packets are already being |
4991 | * handled by the dispatch table, and we need only pay |
4992 | * attention to the unusual ones. |
4993 | */ |
0357890f |
4994 | |
51df0ab5 |
4995 | crReturnV; |
4996 | if (pktin) { |
4997 | if (pktin->type == SSH1_SMSG_SUCCESS) { |
972a41c8 |
4998 | /* may be from EXEC_SHELL on some servers */ |
ff3187f6 |
4999 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
972a41c8 |
5000 | /* may be from EXEC_SHELL on some servers |
374330e2 |
5001 | * if no pty is available or in other odd cases. Ignore */ |
374330e2 |
5002 | } else { |
ff3187f6 |
5003 | bombout(("Strange packet received: type %d", pktin->type)); |
7ffdbc1a |
5004 | crStopV; |
374330e2 |
5005 | } |
5006 | } else { |
8df7a775 |
5007 | while (inlen > 0) { |
5008 | int len = min(inlen, 512); |
a2724fba |
5009 | send_packet(ssh, SSH1_CMSG_STDIN_DATA, |
5010 | PKT_INT, len, PKTT_DATA, PKT_DATA, in, len, |
9a10ecf4 |
5011 | PKTT_OTHER, PKT_END); |
8df7a775 |
5012 | in += len; |
5013 | inlen -= len; |
5014 | } |
374330e2 |
5015 | } |
5016 | } |
5017 | |
5018 | crFinishV; |
5019 | } |
5020 | |
5021 | /* |
2e85c969 |
5022 | * Handle the top-level SSH-2 protocol. |
b09eaa88 |
5023 | */ |
5024 | static void ssh1_msg_debug(Ssh ssh, struct Packet *pktin) |
5025 | { |
fb983202 |
5026 | char *msg; |
b09eaa88 |
5027 | int msglen; |
5028 | |
5029 | ssh_pkt_getstring(pktin, &msg, &msglen); |
fb983202 |
5030 | logeventf(ssh, "Remote debug message: %.*s", msglen, msg); |
b09eaa88 |
5031 | } |
5032 | |
5033 | static void ssh1_msg_disconnect(Ssh ssh, struct Packet *pktin) |
5034 | { |
5035 | /* log reason code in disconnect message */ |
5036 | char *msg; |
5037 | int msglen; |
5038 | |
5039 | ssh_pkt_getstring(pktin, &msg, &msglen); |
5040 | bombout(("Server sent disconnect message:\n\"%.*s\"", msglen, msg)); |
5041 | } |
5042 | |
409bfa77 |
5043 | static void ssh_msg_ignore(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
5044 | { |
5045 | /* Do nothing, because we're ignoring it! Duhh. */ |
5046 | } |
5047 | |
5048 | static void ssh1_protocol_setup(Ssh ssh) |
5049 | { |
5050 | int i; |
5051 | |
5052 | /* |
5053 | * Most messages are handled by the coroutines. |
5054 | */ |
5055 | for (i = 0; i < 256; i++) |
5056 | ssh->packet_dispatch[i] = NULL; |
5057 | |
5058 | /* |
5059 | * These special message types we install handlers for. |
5060 | */ |
5061 | ssh->packet_dispatch[SSH1_MSG_DISCONNECT] = ssh1_msg_disconnect; |
5062 | ssh->packet_dispatch[SSH1_MSG_IGNORE] = ssh_msg_ignore; |
5063 | ssh->packet_dispatch[SSH1_MSG_DEBUG] = ssh1_msg_debug; |
5064 | } |
5065 | |
1c1a7262 |
5066 | static void ssh1_protocol(Ssh ssh, void *vin, int inlen, |
b09eaa88 |
5067 | struct Packet *pktin) |
5068 | { |
1c1a7262 |
5069 | unsigned char *in=(unsigned char*)vin; |
b09eaa88 |
5070 | if (ssh->state == SSH_STATE_CLOSED) |
5071 | return; |
5072 | |
5073 | if (pktin && ssh->packet_dispatch[pktin->type]) { |
5074 | ssh->packet_dispatch[pktin->type](ssh, pktin); |
5075 | return; |
5076 | } |
5077 | |
5078 | if (!ssh->protocol_initial_phase_done) { |
5079 | if (do_ssh1_login(ssh, in, inlen, pktin)) |
5080 | ssh->protocol_initial_phase_done = TRUE; |
5081 | else |
5082 | return; |
5083 | } |
5084 | |
5085 | do_ssh1_connection(ssh, in, inlen, pktin); |
5086 | } |
5087 | |
5088 | /* |
e5574168 |
5089 | * Utility routine for decoding comma-separated strings in KEXINIT. |
5090 | */ |
32874aea |
5091 | static int in_commasep_string(char *needle, char *haystack, int haylen) |
5092 | { |
57356d63 |
5093 | int needlen; |
5094 | if (!needle || !haystack) /* protect against null pointers */ |
5095 | return 0; |
5096 | needlen = strlen(needle); |
e5574168 |
5097 | while (1) { |
32874aea |
5098 | /* |
5099 | * Is it at the start of the string? |
5100 | */ |
5101 | if (haylen >= needlen && /* haystack is long enough */ |
5102 | !memcmp(needle, haystack, needlen) && /* initial match */ |
5103 | (haylen == needlen || haystack[needlen] == ',') |
5104 | /* either , or EOS follows */ |
5105 | ) |
5106 | return 1; |
5107 | /* |
5108 | * If not, search for the next comma and resume after that. |
5109 | * If no comma found, terminate. |
5110 | */ |
5111 | while (haylen > 0 && *haystack != ',') |
5112 | haylen--, haystack++; |
5113 | if (haylen == 0) |
5114 | return 0; |
5115 | haylen--, haystack++; /* skip over comma itself */ |
e5574168 |
5116 | } |
5117 | } |
5118 | |
5119 | /* |
b59743d5 |
5120 | * Similar routine for checking whether we have the first string in a list. |
5121 | */ |
5122 | static int first_in_commasep_string(char *needle, char *haystack, int haylen) |
5123 | { |
5124 | int needlen; |
5125 | if (!needle || !haystack) /* protect against null pointers */ |
5126 | return 0; |
5127 | needlen = strlen(needle); |
5128 | /* |
5129 | * Is it at the start of the string? |
5130 | */ |
5131 | if (haylen >= needlen && /* haystack is long enough */ |
5132 | !memcmp(needle, haystack, needlen) && /* initial match */ |
5133 | (haylen == needlen || haystack[needlen] == ',') |
5134 | /* either , or EOS follows */ |
5135 | ) |
5136 | return 1; |
5137 | return 0; |
5138 | } |
5139 | |
5140 | |
5141 | /* |
2e85c969 |
5142 | * SSH-2 key creation method. |
754c0df9 |
5143 | * (Currently assumes 2 lots of any hash are sufficient to generate |
5144 | * keys/IVs for any cipher/MAC. SSH2_MKKEY_ITERS documents this assumption.) |
d39f364a |
5145 | */ |
754c0df9 |
5146 | #define SSH2_MKKEY_ITERS (2) |
b672f405 |
5147 | static void ssh2_mkkey(Ssh ssh, Bignum K, unsigned char *H, char chr, |
d8baa528 |
5148 | unsigned char *keyspace) |
32874aea |
5149 | { |
b672f405 |
5150 | const struct ssh_hash *h = ssh->kex->hash; |
5151 | void *s; |
5152 | /* First hlen bytes. */ |
5153 | s = h->init(); |
51470298 |
5154 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
b672f405 |
5155 | hash_mpint(h, s, K); |
5156 | h->bytes(s, H, h->hlen); |
5157 | h->bytes(s, &chr, 1); |
5158 | h->bytes(s, ssh->v2_session_id, ssh->v2_session_id_len); |
5159 | h->final(s, keyspace); |
5160 | /* Next hlen bytes. */ |
5161 | s = h->init(); |
51470298 |
5162 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
b672f405 |
5163 | hash_mpint(h, s, K); |
5164 | h->bytes(s, H, h->hlen); |
5165 | h->bytes(s, keyspace, h->hlen); |
5166 | h->final(s, keyspace + h->hlen); |
d39f364a |
5167 | } |
5168 | |
5169 | /* |
2e85c969 |
5170 | * Handle the SSH-2 transport layer. |
e5574168 |
5171 | */ |
1c1a7262 |
5172 | static int do_ssh2_transport(Ssh ssh, void *vin, int inlen, |
ff3187f6 |
5173 | struct Packet *pktin) |
e5574168 |
5174 | { |
1c1a7262 |
5175 | unsigned char *in = (unsigned char *)vin; |
51470298 |
5176 | struct do_ssh2_transport_state { |
4763c1c2 |
5177 | int nbits, pbits, warn_kex, warn_cscipher, warn_sccipher; |
51470298 |
5178 | Bignum p, g, e, f, K; |
3dc9a6a7 |
5179 | void *our_kexinit; |
5180 | int our_kexinitlen; |
51470298 |
5181 | int kex_init_value, kex_reply_value; |
5182 | const struct ssh_mac **maclist; |
5183 | int nmacs; |
5184 | const struct ssh2_cipher *cscipher_tobe; |
5185 | const struct ssh2_cipher *sccipher_tobe; |
5186 | const struct ssh_mac *csmac_tobe; |
5187 | const struct ssh_mac *scmac_tobe; |
5188 | const struct ssh_compress *cscomp_tobe; |
5189 | const struct ssh_compress *sccomp_tobe; |
fae1a71b |
5190 | char *hostkeydata, *sigdata, *rsakeydata, *keystr, *fingerprint; |
5191 | int hostkeylen, siglen, rsakeylen; |
51470298 |
5192 | void *hkey; /* actual host key */ |
fae1a71b |
5193 | void *rsakey; /* for RSA kex */ |
754c0df9 |
5194 | unsigned char exchange_hash[SSH2_KEX_MAX_HASH_LEN]; |
83e7d008 |
5195 | int n_preferred_kex; |
34557659 |
5196 | const struct ssh_kexes *preferred_kex[KEX_MAX]; |
51470298 |
5197 | int n_preferred_ciphers; |
5198 | const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX]; |
5199 | const struct ssh_compress *preferred_comp; |
e13bba36 |
5200 | int got_session_id, activated_authconn; |
ff3187f6 |
5201 | struct Packet *pktout; |
3d9449a1 |
5202 | int dlgret; |
5203 | int guessok; |
4763c1c2 |
5204 | int ignorepkt; |
51470298 |
5205 | }; |
5206 | crState(do_ssh2_transport_state); |
5207 | |
5208 | crBegin(ssh->do_ssh2_transport_crstate); |
5209 | |
5210 | s->cscipher_tobe = s->sccipher_tobe = NULL; |
5211 | s->csmac_tobe = s->scmac_tobe = NULL; |
5212 | s->cscomp_tobe = s->sccomp_tobe = NULL; |
5213 | |
e13bba36 |
5214 | s->got_session_id = s->activated_authconn = FALSE; |
e5574168 |
5215 | |
e13bba36 |
5216 | /* |
5217 | * Be prepared to work around the buggy MAC problem. |
5218 | */ |
5219 | if (ssh->remote_bugs & BUG_SSH2_HMAC) |
5220 | s->maclist = buggymacs, s->nmacs = lenof(buggymacs); |
5221 | else |
5222 | s->maclist = macs, s->nmacs = lenof(macs); |
5223 | |
5224 | begin_key_exchange: |
acab36bc |
5225 | ssh->pkt_kctx = SSH2_PKTCTX_NOKEX; |
51470298 |
5226 | { |
e13bba36 |
5227 | int i, j, commalist_started; |
5228 | |
51470298 |
5229 | /* |
83e7d008 |
5230 | * Set up the preferred key exchange. (NULL => warn below here) |
5231 | */ |
5232 | s->n_preferred_kex = 0; |
5233 | for (i = 0; i < KEX_MAX; i++) { |
5234 | switch (ssh->cfg.ssh_kexlist[i]) { |
5235 | case KEX_DHGEX: |
5236 | s->preferred_kex[s->n_preferred_kex++] = |
5237 | &ssh_diffiehellman_gex; |
5238 | break; |
5239 | case KEX_DHGROUP14: |
5240 | s->preferred_kex[s->n_preferred_kex++] = |
5241 | &ssh_diffiehellman_group14; |
5242 | break; |
5243 | case KEX_DHGROUP1: |
5244 | s->preferred_kex[s->n_preferred_kex++] = |
5245 | &ssh_diffiehellman_group1; |
5246 | break; |
fae1a71b |
5247 | case KEX_RSA: |
5248 | s->preferred_kex[s->n_preferred_kex++] = |
5249 | &ssh_rsa_kex; |
5250 | break; |
754c0df9 |
5251 | case KEX_WARN: |
83e7d008 |
5252 | /* Flag for later. Don't bother if it's the last in |
5253 | * the list. */ |
5254 | if (i < KEX_MAX - 1) { |
5255 | s->preferred_kex[s->n_preferred_kex++] = NULL; |
5256 | } |
5257 | break; |
5258 | } |
5259 | } |
83e7d008 |
5260 | |
83e7d008 |
5261 | /* |
51470298 |
5262 | * Set up the preferred ciphers. (NULL => warn below here) |
5263 | */ |
5264 | s->n_preferred_ciphers = 0; |
5265 | for (i = 0; i < CIPHER_MAX; i++) { |
86916870 |
5266 | switch (ssh->cfg.ssh_cipherlist[i]) { |
51470298 |
5267 | case CIPHER_BLOWFISH: |
5268 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_blowfish; |
5269 | break; |
5270 | case CIPHER_DES: |
86916870 |
5271 | if (ssh->cfg.ssh2_des_cbc) { |
51470298 |
5272 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_des; |
5273 | } |
5274 | break; |
5275 | case CIPHER_3DES: |
5276 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_3des; |
5277 | break; |
5278 | case CIPHER_AES: |
5279 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_aes; |
5280 | break; |
a2add208 |
5281 | case CIPHER_ARCFOUR: |
5282 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_arcfour; |
5283 | break; |
51470298 |
5284 | case CIPHER_WARN: |
5285 | /* Flag for later. Don't bother if it's the last in |
5286 | * the list. */ |
5287 | if (i < CIPHER_MAX - 1) { |
5288 | s->preferred_ciphers[s->n_preferred_ciphers++] = NULL; |
5289 | } |
5290 | break; |
ca20bfcf |
5291 | } |
ca20bfcf |
5292 | } |
7591b9ff |
5293 | |
e13bba36 |
5294 | /* |
5295 | * Set up preferred compression. |
5296 | */ |
5297 | if (ssh->cfg.compression) |
5298 | s->preferred_comp = &ssh_zlib; |
5299 | else |
5300 | s->preferred_comp = &ssh_comp_none; |
51470298 |
5301 | |
5302 | /* |
590f6a5f |
5303 | * Enable queueing of outgoing auth- or connection-layer |
5304 | * packets while we are in the middle of a key exchange. |
5305 | */ |
5306 | ssh->queueing = TRUE; |
5307 | |
5308 | /* |
9442dd57 |
5309 | * Flag that KEX is in progress. |
5310 | */ |
5311 | ssh->kex_in_progress = TRUE; |
5312 | |
5313 | /* |
51470298 |
5314 | * Construct and send our key exchange packet. |
5315 | */ |
ff3187f6 |
5316 | s->pktout = ssh2_pkt_init(SSH2_MSG_KEXINIT); |
51470298 |
5317 | for (i = 0; i < 16; i++) |
ff3187f6 |
5318 | ssh2_pkt_addbyte(s->pktout, (unsigned char) random_byte()); |
51470298 |
5319 | /* List key exchange algorithms. */ |
ff3187f6 |
5320 | ssh2_pkt_addstring_start(s->pktout); |
83e7d008 |
5321 | commalist_started = 0; |
5322 | for (i = 0; i < s->n_preferred_kex; i++) { |
34557659 |
5323 | const struct ssh_kexes *k = s->preferred_kex[i]; |
83e7d008 |
5324 | if (!k) continue; /* warning flag */ |
34557659 |
5325 | for (j = 0; j < k->nkexes; j++) { |
5326 | if (commalist_started) |
5327 | ssh2_pkt_addstring_str(s->pktout, ","); |
5328 | ssh2_pkt_addstring_str(s->pktout, k->list[j]->name); |
5329 | commalist_started = 1; |
5330 | } |
32874aea |
5331 | } |
51470298 |
5332 | /* List server host key algorithms. */ |
ff3187f6 |
5333 | ssh2_pkt_addstring_start(s->pktout); |
51470298 |
5334 | for (i = 0; i < lenof(hostkey_algs); i++) { |
ff3187f6 |
5335 | ssh2_pkt_addstring_str(s->pktout, hostkey_algs[i]->name); |
51470298 |
5336 | if (i < lenof(hostkey_algs) - 1) |
ff3187f6 |
5337 | ssh2_pkt_addstring_str(s->pktout, ","); |
32874aea |
5338 | } |
51470298 |
5339 | /* List client->server encryption algorithms. */ |
ff3187f6 |
5340 | ssh2_pkt_addstring_start(s->pktout); |
83e7d008 |
5341 | commalist_started = 0; |
51470298 |
5342 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
5343 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
5344 | if (!c) continue; /* warning flag */ |
5345 | for (j = 0; j < c->nciphers; j++) { |
83e7d008 |
5346 | if (commalist_started) |
ff3187f6 |
5347 | ssh2_pkt_addstring_str(s->pktout, ","); |
5348 | ssh2_pkt_addstring_str(s->pktout, c->list[j]->name); |
83e7d008 |
5349 | commalist_started = 1; |
51470298 |
5350 | } |
5351 | } |
5352 | /* List server->client encryption algorithms. */ |
ff3187f6 |
5353 | ssh2_pkt_addstring_start(s->pktout); |
83e7d008 |
5354 | commalist_started = 0; |
51470298 |
5355 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
5356 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
5357 | if (!c) continue; /* warning flag */ |
5358 | for (j = 0; j < c->nciphers; j++) { |
83e7d008 |
5359 | if (commalist_started) |
ff3187f6 |
5360 | ssh2_pkt_addstring_str(s->pktout, ","); |
5361 | ssh2_pkt_addstring_str(s->pktout, c->list[j]->name); |
83e7d008 |
5362 | commalist_started = 1; |
51470298 |
5363 | } |
5364 | } |
5365 | /* List client->server MAC algorithms. */ |
ff3187f6 |
5366 | ssh2_pkt_addstring_start(s->pktout); |
51470298 |
5367 | for (i = 0; i < s->nmacs; i++) { |
ff3187f6 |
5368 | ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name); |
51470298 |
5369 | if (i < s->nmacs - 1) |
ff3187f6 |
5370 | ssh2_pkt_addstring_str(s->pktout, ","); |
51470298 |
5371 | } |
5372 | /* List server->client MAC algorithms. */ |
ff3187f6 |
5373 | ssh2_pkt_addstring_start(s->pktout); |
51470298 |
5374 | for (i = 0; i < s->nmacs; i++) { |
ff3187f6 |
5375 | ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name); |
51470298 |
5376 | if (i < s->nmacs - 1) |
ff3187f6 |
5377 | ssh2_pkt_addstring_str(s->pktout, ","); |
51470298 |
5378 | } |
5379 | /* List client->server compression algorithms. */ |
ff3187f6 |
5380 | ssh2_pkt_addstring_start(s->pktout); |
f6e0abe2 |
5381 | assert(lenof(compressions) > 1); |
ff3187f6 |
5382 | ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name); |
f6e0abe2 |
5383 | for (i = 0; i < lenof(compressions); i++) { |
5384 | const struct ssh_compress *c = compressions[i]; |
5385 | if (c != s->preferred_comp) { |
ff3187f6 |
5386 | ssh2_pkt_addstring_str(s->pktout, ","); |
5387 | ssh2_pkt_addstring_str(s->pktout, c->name); |
f6e0abe2 |
5388 | } |
51470298 |
5389 | } |
5390 | /* List server->client compression algorithms. */ |
ff3187f6 |
5391 | ssh2_pkt_addstring_start(s->pktout); |
f6e0abe2 |
5392 | assert(lenof(compressions) > 1); |
ff3187f6 |
5393 | ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name); |
f6e0abe2 |
5394 | for (i = 0; i < lenof(compressions); i++) { |
5395 | const struct ssh_compress *c = compressions[i]; |
5396 | if (c != s->preferred_comp) { |
ff3187f6 |
5397 | ssh2_pkt_addstring_str(s->pktout, ","); |
5398 | ssh2_pkt_addstring_str(s->pktout, c->name); |
f6e0abe2 |
5399 | } |
51470298 |
5400 | } |
5401 | /* List client->server languages. Empty list. */ |
ff3187f6 |
5402 | ssh2_pkt_addstring_start(s->pktout); |
51470298 |
5403 | /* List server->client languages. Empty list. */ |
ff3187f6 |
5404 | ssh2_pkt_addstring_start(s->pktout); |
51470298 |
5405 | /* First KEX packet does _not_ follow, because we're not that brave. */ |
ff3187f6 |
5406 | ssh2_pkt_addbool(s->pktout, FALSE); |
51470298 |
5407 | /* Reserved. */ |
ff3187f6 |
5408 | ssh2_pkt_adduint32(s->pktout, 0); |
e5574168 |
5409 | } |
0db56f73 |
5410 | |
3dc9a6a7 |
5411 | s->our_kexinitlen = s->pktout->length - 5; |
5412 | s->our_kexinit = snewn(s->our_kexinitlen, unsigned char); |
5413 | memcpy(s->our_kexinit, s->pktout->data + 5, s->our_kexinitlen); |
0db56f73 |
5414 | |
590f6a5f |
5415 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
e5574168 |
5416 | |
ff3187f6 |
5417 | if (!pktin) |
5418 | crWaitUntil(pktin); |
e5574168 |
5419 | |
5420 | /* |
5421 | * Now examine the other side's KEXINIT to see what we're up |
5422 | * to. |
5423 | */ |
51470298 |
5424 | { |
4763c1c2 |
5425 | char *str, *preferred; |
3d9449a1 |
5426 | int i, j, len; |
51470298 |
5427 | |
ff3187f6 |
5428 | if (pktin->type != SSH2_MSG_KEXINIT) { |
6b5cf8b4 |
5429 | bombout(("expected key exchange packet from server")); |
7ffdbc1a |
5430 | crStop(0); |
32874aea |
5431 | } |
51470298 |
5432 | ssh->kex = NULL; |
5433 | ssh->hostkey = NULL; |
5434 | s->cscipher_tobe = NULL; |
5435 | s->sccipher_tobe = NULL; |
5436 | s->csmac_tobe = NULL; |
5437 | s->scmac_tobe = NULL; |
5438 | s->cscomp_tobe = NULL; |
5439 | s->sccomp_tobe = NULL; |
4763c1c2 |
5440 | s->warn_kex = s->warn_cscipher = s->warn_sccipher = FALSE; |
5441 | |
ff3187f6 |
5442 | pktin->savedpos += 16; /* skip garbage cookie */ |
5443 | ssh_pkt_getstring(pktin, &str, &len); /* key exchange algorithms */ |
4763c1c2 |
5444 | |
5445 | preferred = NULL; |
83e7d008 |
5446 | for (i = 0; i < s->n_preferred_kex; i++) { |
34557659 |
5447 | const struct ssh_kexes *k = s->preferred_kex[i]; |
83e7d008 |
5448 | if (!k) { |
4763c1c2 |
5449 | s->warn_kex = TRUE; |
5450 | } else { |
34557659 |
5451 | for (j = 0; j < k->nkexes; j++) { |
5452 | if (!preferred) preferred = k->list[j]->name; |
5453 | if (in_commasep_string(k->list[j]->name, str, len)) { |
5454 | ssh->kex = k->list[j]; |
5455 | break; |
5456 | } |
5457 | } |
83e7d008 |
5458 | } |
4763c1c2 |
5459 | if (ssh->kex) |
51470298 |
5460 | break; |
32874aea |
5461 | } |
83e7d008 |
5462 | if (!ssh->kex) { |
5463 | bombout(("Couldn't agree a key exchange algorithm (available: %s)", |
5464 | str ? str : "(null)")); |
5465 | crStop(0); |
5466 | } |
b59743d5 |
5467 | /* |
5468 | * Note that the server's guess is considered wrong if it doesn't match |
5469 | * the first algorithm in our list, even if it's still the algorithm |
5470 | * we end up using. |
5471 | */ |
4763c1c2 |
5472 | s->guessok = first_in_commasep_string(preferred, str, len); |
ff3187f6 |
5473 | ssh_pkt_getstring(pktin, &str, &len); /* host key algorithms */ |
51470298 |
5474 | for (i = 0; i < lenof(hostkey_algs); i++) { |
5475 | if (in_commasep_string(hostkey_algs[i]->name, str, len)) { |
5476 | ssh->hostkey = hostkey_algs[i]; |
5477 | break; |
5478 | } |
5479 | } |
3d9449a1 |
5480 | s->guessok = s->guessok && |
b59743d5 |
5481 | first_in_commasep_string(hostkey_algs[0]->name, str, len); |
ff3187f6 |
5482 | ssh_pkt_getstring(pktin, &str, &len); /* client->server cipher */ |
51470298 |
5483 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
5484 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
5485 | if (!c) { |
4763c1c2 |
5486 | s->warn_cscipher = TRUE; |
51470298 |
5487 | } else { |
5488 | for (j = 0; j < c->nciphers; j++) { |
5489 | if (in_commasep_string(c->list[j]->name, str, len)) { |
5490 | s->cscipher_tobe = c->list[j]; |
5491 | break; |
5492 | } |
ca20bfcf |
5493 | } |
32874aea |
5494 | } |
4763c1c2 |
5495 | if (s->cscipher_tobe) |
51470298 |
5496 | break; |
32874aea |
5497 | } |
51470298 |
5498 | if (!s->cscipher_tobe) { |
6b5cf8b4 |
5499 | bombout(("Couldn't agree a client-to-server cipher (available: %s)", |
57356d63 |
5500 | str ? str : "(null)")); |
7ffdbc1a |
5501 | crStop(0); |
ca20bfcf |
5502 | } |
0ef8f407 |
5503 | |
ff3187f6 |
5504 | ssh_pkt_getstring(pktin, &str, &len); /* server->client cipher */ |
51470298 |
5505 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
5506 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
5507 | if (!c) { |
4763c1c2 |
5508 | s->warn_sccipher = TRUE; |
51470298 |
5509 | } else { |
5510 | for (j = 0; j < c->nciphers; j++) { |
5511 | if (in_commasep_string(c->list[j]->name, str, len)) { |
5512 | s->sccipher_tobe = c->list[j]; |
5513 | break; |
5514 | } |
ca20bfcf |
5515 | } |
32874aea |
5516 | } |
4763c1c2 |
5517 | if (s->sccipher_tobe) |
51470298 |
5518 | break; |
32874aea |
5519 | } |
51470298 |
5520 | if (!s->sccipher_tobe) { |
6b5cf8b4 |
5521 | bombout(("Couldn't agree a server-to-client cipher (available: %s)", |
57356d63 |
5522 | str ? str : "(null)")); |
7ffdbc1a |
5523 | crStop(0); |
ca20bfcf |
5524 | } |
0ef8f407 |
5525 | |
ff3187f6 |
5526 | ssh_pkt_getstring(pktin, &str, &len); /* client->server mac */ |
51470298 |
5527 | for (i = 0; i < s->nmacs; i++) { |
5528 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
5529 | s->csmac_tobe = s->maclist[i]; |
5530 | break; |
5531 | } |
32874aea |
5532 | } |
ff3187f6 |
5533 | ssh_pkt_getstring(pktin, &str, &len); /* server->client mac */ |
51470298 |
5534 | for (i = 0; i < s->nmacs; i++) { |
5535 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
5536 | s->scmac_tobe = s->maclist[i]; |
5537 | break; |
5538 | } |
32874aea |
5539 | } |
ff3187f6 |
5540 | ssh_pkt_getstring(pktin, &str, &len); /* client->server compression */ |
51470298 |
5541 | for (i = 0; i < lenof(compressions) + 1; i++) { |
5542 | const struct ssh_compress *c = |
5543 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
5544 | if (in_commasep_string(c->name, str, len)) { |
5545 | s->cscomp_tobe = c; |
5546 | break; |
5547 | } |
32874aea |
5548 | } |
ff3187f6 |
5549 | ssh_pkt_getstring(pktin, &str, &len); /* server->client compression */ |
51470298 |
5550 | for (i = 0; i < lenof(compressions) + 1; i++) { |
5551 | const struct ssh_compress *c = |
5552 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
5553 | if (in_commasep_string(c->name, str, len)) { |
5554 | s->sccomp_tobe = c; |
5555 | break; |
5556 | } |
32874aea |
5557 | } |
b59743d5 |
5558 | ssh_pkt_getstring(pktin, &str, &len); /* client->server language */ |
5559 | ssh_pkt_getstring(pktin, &str, &len); /* server->client language */ |
4763c1c2 |
5560 | s->ignorepkt = ssh2_pkt_getbool(pktin) && !s->guessok; |
5561 | |
5562 | if (s->warn_kex) { |
5563 | ssh_set_frozen(ssh, 1); |
5564 | s->dlgret = askalg(ssh->frontend, "key-exchange algorithm", |
5565 | ssh->kex->name, |
5566 | ssh_dialog_callback, ssh); |
5567 | if (s->dlgret < 0) { |
5568 | do { |
5569 | crReturn(0); |
5570 | if (pktin) { |
5571 | bombout(("Unexpected data from server while" |
5572 | " waiting for user response")); |
5573 | crStop(0); |
5574 | } |
5575 | } while (pktin || inlen > 0); |
5576 | s->dlgret = ssh->user_response; |
5577 | } |
5578 | ssh_set_frozen(ssh, 0); |
5579 | if (s->dlgret == 0) { |
9e296bfa |
5580 | ssh_disconnect(ssh, "User aborted at kex warning", NULL, |
5581 | 0, TRUE); |
a5a6f839 |
5582 | crStop(0); |
4763c1c2 |
5583 | } |
5584 | } |
5585 | |
5586 | if (s->warn_cscipher) { |
5587 | ssh_set_frozen(ssh, 1); |
5588 | s->dlgret = askalg(ssh->frontend, |
5589 | "client-to-server cipher", |
5590 | s->cscipher_tobe->name, |
5591 | ssh_dialog_callback, ssh); |
5592 | if (s->dlgret < 0) { |
5593 | do { |
5594 | crReturn(0); |
5595 | if (pktin) { |
5596 | bombout(("Unexpected data from server while" |
5597 | " waiting for user response")); |
5598 | crStop(0); |
5599 | } |
5600 | } while (pktin || inlen > 0); |
5601 | s->dlgret = ssh->user_response; |
5602 | } |
5603 | ssh_set_frozen(ssh, 0); |
5604 | if (s->dlgret == 0) { |
9e296bfa |
5605 | ssh_disconnect(ssh, "User aborted at cipher warning", NULL, |
5606 | 0, TRUE); |
a5a6f839 |
5607 | crStop(0); |
4763c1c2 |
5608 | } |
5609 | } |
5610 | |
5611 | if (s->warn_sccipher) { |
5612 | ssh_set_frozen(ssh, 1); |
5613 | s->dlgret = askalg(ssh->frontend, |
5614 | "server-to-client cipher", |
5615 | s->sccipher_tobe->name, |
5616 | ssh_dialog_callback, ssh); |
5617 | if (s->dlgret < 0) { |
5618 | do { |
5619 | crReturn(0); |
5620 | if (pktin) { |
5621 | bombout(("Unexpected data from server while" |
5622 | " waiting for user response")); |
5623 | crStop(0); |
5624 | } |
5625 | } while (pktin || inlen > 0); |
5626 | s->dlgret = ssh->user_response; |
5627 | } |
5628 | ssh_set_frozen(ssh, 0); |
5629 | if (s->dlgret == 0) { |
9e296bfa |
5630 | ssh_disconnect(ssh, "User aborted at cipher warning", NULL, |
5631 | 0, TRUE); |
a5a6f839 |
5632 | crStop(0); |
4763c1c2 |
5633 | } |
5634 | } |
5635 | |
b672f405 |
5636 | ssh->exhash = ssh->kex->hash->init(); |
5637 | hash_string(ssh->kex->hash, ssh->exhash, ssh->v_c, strlen(ssh->v_c)); |
5638 | hash_string(ssh->kex->hash, ssh->exhash, ssh->v_s, strlen(ssh->v_s)); |
5639 | hash_string(ssh->kex->hash, ssh->exhash, |
5640 | s->our_kexinit, s->our_kexinitlen); |
3dc9a6a7 |
5641 | sfree(s->our_kexinit); |
5642 | if (pktin->length > 5) |
b672f405 |
5643 | hash_string(ssh->kex->hash, ssh->exhash, |
5644 | pktin->data + 5, pktin->length - 5); |
3dc9a6a7 |
5645 | |
4763c1c2 |
5646 | if (s->ignorepkt) /* first_kex_packet_follows */ |
b59743d5 |
5647 | crWaitUntil(pktin); /* Ignore packet */ |
e5574168 |
5648 | } |
e5574168 |
5649 | |
fae1a71b |
5650 | if (ssh->kex->main_type == KEXTYPE_DH) { |
30774ba3 |
5651 | /* |
5652 | * Work out the number of bits of key we will need from the |
5653 | * key exchange. We start with the maximum key length of |
5654 | * either cipher... |
5655 | */ |
5656 | { |
5657 | int csbits, scbits; |
5658 | |
5659 | csbits = s->cscipher_tobe->keylen; |
5660 | scbits = s->sccipher_tobe->keylen; |
5661 | s->nbits = (csbits > scbits ? csbits : scbits); |
5662 | } |
5663 | /* The keys only have hlen-bit entropy, since they're based on |
5664 | * a hash. So cap the key size at hlen bits. */ |
5665 | if (s->nbits > ssh->kex->hash->hlen * 8) |
5666 | s->nbits = ssh->kex->hash->hlen * 8; |
5667 | |
5668 | /* |
5669 | * If we're doing Diffie-Hellman group exchange, start by |
5670 | * requesting a group. |
5671 | */ |
5672 | if (!ssh->kex->pdata) { |
5673 | logevent("Doing Diffie-Hellman group exchange"); |
acab36bc |
5674 | ssh->pkt_kctx = SSH2_PKTCTX_DHGEX; |
30774ba3 |
5675 | /* |
5676 | * Work out how big a DH group we will need to allow that |
5677 | * much data. |
5678 | */ |
5679 | s->pbits = 512 << ((s->nbits - 1) / 64); |
5680 | s->pktout = ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST); |
5681 | ssh2_pkt_adduint32(s->pktout, s->pbits); |
5682 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
5683 | |
5684 | crWaitUntil(pktin); |
5685 | if (pktin->type != SSH2_MSG_KEX_DH_GEX_GROUP) { |
5686 | bombout(("expected key exchange group packet from server")); |
5687 | crStop(0); |
5688 | } |
5689 | s->p = ssh2_pkt_getmp(pktin); |
5690 | s->g = ssh2_pkt_getmp(pktin); |
5691 | if (!s->p || !s->g) { |
5692 | bombout(("unable to read mp-ints from incoming group packet")); |
5693 | crStop(0); |
5694 | } |
5695 | ssh->kex_ctx = dh_setup_gex(s->p, s->g); |
5696 | s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT; |
5697 | s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY; |
5698 | } else { |
acab36bc |
5699 | ssh->pkt_kctx = SSH2_PKTCTX_DHGROUP; |
30774ba3 |
5700 | ssh->kex_ctx = dh_setup_group(ssh->kex); |
5701 | s->kex_init_value = SSH2_MSG_KEXDH_INIT; |
5702 | s->kex_reply_value = SSH2_MSG_KEXDH_REPLY; |
5703 | logeventf(ssh, "Using Diffie-Hellman with standard group \"%s\"", |
5704 | ssh->kex->groupname); |
5705 | } |
e5574168 |
5706 | |
30774ba3 |
5707 | logeventf(ssh, "Doing Diffie-Hellman key exchange with hash %s", |
5708 | ssh->kex->hash->text_name); |
5709 | /* |
5710 | * Now generate and send e for Diffie-Hellman. |
5711 | */ |
5712 | set_busy_status(ssh->frontend, BUSY_CPU); /* this can take a while */ |
5713 | s->e = dh_create_e(ssh->kex_ctx, s->nbits * 2); |
5714 | s->pktout = ssh2_pkt_init(s->kex_init_value); |
5715 | ssh2_pkt_addmp(s->pktout, s->e); |
5716 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
5717 | |
5718 | set_busy_status(ssh->frontend, BUSY_WAITING); /* wait for server */ |
5719 | crWaitUntil(pktin); |
5720 | if (pktin->type != s->kex_reply_value) { |
5721 | bombout(("expected key exchange reply packet from server")); |
5722 | crStop(0); |
5723 | } |
5724 | set_busy_status(ssh->frontend, BUSY_CPU); /* cogitate */ |
5725 | ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen); |
5726 | s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen); |
5727 | s->f = ssh2_pkt_getmp(pktin); |
5728 | if (!s->f) { |
5729 | bombout(("unable to parse key exchange reply packet")); |
5730 | crStop(0); |
5731 | } |
5732 | ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen); |
e5574168 |
5733 | |
30774ba3 |
5734 | s->K = dh_find_K(ssh->kex_ctx, s->f); |
e5574168 |
5735 | |
30774ba3 |
5736 | /* We assume everything from now on will be quick, and it might |
5737 | * involve user interaction. */ |
5738 | set_busy_status(ssh->frontend, BUSY_NOT); |
755e0524 |
5739 | |
30774ba3 |
5740 | hash_string(ssh->kex->hash, ssh->exhash, s->hostkeydata, s->hostkeylen); |
5741 | if (!ssh->kex->pdata) { |
5742 | hash_uint32(ssh->kex->hash, ssh->exhash, s->pbits); |
5743 | hash_mpint(ssh->kex->hash, ssh->exhash, s->p); |
5744 | hash_mpint(ssh->kex->hash, ssh->exhash, s->g); |
5745 | } |
5746 | hash_mpint(ssh->kex->hash, ssh->exhash, s->e); |
5747 | hash_mpint(ssh->kex->hash, ssh->exhash, s->f); |
5748 | |
5749 | dh_cleanup(ssh->kex_ctx); |
5750 | freebn(s->f); |
5751 | if (!ssh->kex->pdata) { |
5752 | freebn(s->g); |
5753 | freebn(s->p); |
5754 | } |
fae1a71b |
5755 | } else { |
5756 | logeventf(ssh, "Doing RSA key exchange with hash %s", |
5757 | ssh->kex->hash->text_name); |
acab36bc |
5758 | ssh->pkt_kctx = SSH2_PKTCTX_RSAKEX; |
fae1a71b |
5759 | /* |
5760 | * RSA key exchange. First expect a KEXRSA_PUBKEY packet |
5761 | * from the server. |
5762 | */ |
5763 | crWaitUntil(pktin); |
5764 | if (pktin->type != SSH2_MSG_KEXRSA_PUBKEY) { |
5765 | bombout(("expected RSA public key packet from server")); |
5766 | crStop(0); |
5767 | } |
5768 | |
5769 | ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen); |
5770 | hash_string(ssh->kex->hash, ssh->exhash, |
5771 | s->hostkeydata, s->hostkeylen); |
5772 | s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen); |
5773 | |
5774 | { |
5775 | char *keydata; |
5776 | ssh_pkt_getstring(pktin, &keydata, &s->rsakeylen); |
5777 | s->rsakeydata = snewn(s->rsakeylen, char); |
5778 | memcpy(s->rsakeydata, keydata, s->rsakeylen); |
5779 | } |
5780 | |
5781 | s->rsakey = ssh_rsakex_newkey(s->rsakeydata, s->rsakeylen); |
5782 | if (!s->rsakey) { |
5783 | sfree(s->rsakeydata); |
5784 | bombout(("unable to parse RSA public key from server")); |
5785 | crStop(0); |
5786 | } |
5787 | |
5788 | hash_string(ssh->kex->hash, ssh->exhash, s->rsakeydata, s->rsakeylen); |
5789 | |
5790 | /* |
5791 | * Next, set up a shared secret K, of precisely KLEN - |
5792 | * 2*HLEN - 49 bits, where KLEN is the bit length of the |
5793 | * RSA key modulus and HLEN is the bit length of the hash |
5794 | * we're using. |
5795 | */ |
5796 | { |
5797 | int klen = ssh_rsakex_klen(s->rsakey); |
5798 | int nbits = klen - (2*ssh->kex->hash->hlen*8 + 49); |
5799 | int i, byte = 0; |
5800 | unsigned char *kstr1, *kstr2, *outstr; |
5801 | int kstr1len, kstr2len, outstrlen; |
5802 | |
5803 | s->K = bn_power_2(nbits - 1); |
5804 | |
5805 | for (i = 0; i < nbits; i++) { |
5806 | if ((i & 7) == 0) { |
5807 | byte = random_byte(); |
5808 | } |
5809 | bignum_set_bit(s->K, i, (byte >> (i & 7)) & 1); |
5810 | } |
5811 | |
5812 | /* |
5813 | * Encode this as an mpint. |
5814 | */ |
5815 | kstr1 = ssh2_mpint_fmt(s->K, &kstr1len); |
5816 | kstr2 = snewn(kstr2len = 4 + kstr1len, unsigned char); |
5817 | PUT_32BIT(kstr2, kstr1len); |
5818 | memcpy(kstr2 + 4, kstr1, kstr1len); |
5819 | |
5820 | /* |
5821 | * Encrypt it with the given RSA key. |
5822 | */ |
5823 | outstrlen = (klen + 7) / 8; |
5824 | outstr = snewn(outstrlen, unsigned char); |
5825 | ssh_rsakex_encrypt(ssh->kex->hash, kstr2, kstr2len, |
5826 | outstr, outstrlen, s->rsakey); |
5827 | |
5828 | /* |
5829 | * And send it off in a return packet. |
5830 | */ |
5831 | s->pktout = ssh2_pkt_init(SSH2_MSG_KEXRSA_SECRET); |
5832 | ssh2_pkt_addstring_start(s->pktout); |
7108a872 |
5833 | ssh2_pkt_addstring_data(s->pktout, (char *)outstr, outstrlen); |
fae1a71b |
5834 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
5835 | |
5836 | hash_string(ssh->kex->hash, ssh->exhash, outstr, outstrlen); |
5837 | |
5838 | sfree(kstr2); |
5839 | sfree(kstr1); |
5840 | sfree(outstr); |
5841 | } |
5842 | |
5843 | ssh_rsakex_freekey(s->rsakey); |
5844 | |
5845 | crWaitUntil(pktin); |
5846 | if (pktin->type != SSH2_MSG_KEXRSA_DONE) { |
5847 | sfree(s->rsakeydata); |
5848 | bombout(("expected signature packet from server")); |
5849 | crStop(0); |
5850 | } |
5851 | |
5852 | ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen); |
5853 | |
5854 | sfree(s->rsakeydata); |
5855 | } |
5856 | |
b672f405 |
5857 | hash_mpint(ssh->kex->hash, ssh->exhash, s->K); |
5858 | assert(ssh->kex->hash->hlen <= sizeof(s->exchange_hash)); |
5859 | ssh->kex->hash->final(ssh->exhash, s->exchange_hash); |
e5574168 |
5860 | |
fabd1805 |
5861 | ssh->kex_ctx = NULL; |
3709bfe9 |
5862 | |
7cca0d81 |
5863 | #if 0 |
765c4200 |
5864 | debug(("Exchange hash is:\n")); |
b672f405 |
5865 | dmemdump(s->exchange_hash, ssh->kex->hash->hlen); |
7cca0d81 |
5866 | #endif |
5867 | |
51470298 |
5868 | if (!s->hkey || |
5869 | !ssh->hostkey->verifysig(s->hkey, s->sigdata, s->siglen, |
b672f405 |
5870 | (char *)s->exchange_hash, |
5871 | ssh->kex->hash->hlen)) { |
6b5cf8b4 |
5872 | bombout(("Server's host key did not match the signature supplied")); |
7ffdbc1a |
5873 | crStop(0); |
8d5de777 |
5874 | } |
e5574168 |
5875 | |
5876 | /* |
7cca0d81 |
5877 | * Authenticate remote host: verify host key. (We've already |
5878 | * checked the signature of the exchange hash.) |
e5574168 |
5879 | */ |
51470298 |
5880 | s->keystr = ssh->hostkey->fmtkey(s->hkey); |
5881 | s->fingerprint = ssh->hostkey->fingerprint(s->hkey); |
3d9449a1 |
5882 | ssh_set_frozen(ssh, 1); |
5883 | s->dlgret = verify_ssh_host_key(ssh->frontend, |
5884 | ssh->savedhost, ssh->savedport, |
5885 | ssh->hostkey->keytype, s->keystr, |
5886 | s->fingerprint, |
5887 | ssh_dialog_callback, ssh); |
5888 | if (s->dlgret < 0) { |
5889 | do { |
5890 | crReturn(0); |
5891 | if (pktin) { |
5892 | bombout(("Unexpected data from server while waiting" |
5893 | " for user host key response")); |
5894 | crStop(0); |
5895 | } |
5896 | } while (pktin || inlen > 0); |
5897 | s->dlgret = ssh->user_response; |
5898 | } |
5899 | ssh_set_frozen(ssh, 0); |
5900 | if (s->dlgret == 0) { |
9e296bfa |
5901 | ssh_disconnect(ssh, "User aborted at host key verification", NULL, |
5902 | 0, TRUE); |
3d9449a1 |
5903 | crStop(0); |
5904 | } |
e13bba36 |
5905 | if (!s->got_session_id) { /* don't bother logging this in rekeys */ |
5e0d7cb8 |
5906 | logevent("Host key fingerprint is:"); |
51470298 |
5907 | logevent(s->fingerprint); |
5e0d7cb8 |
5908 | } |
51470298 |
5909 | sfree(s->fingerprint); |
5910 | sfree(s->keystr); |
5911 | ssh->hostkey->freekey(s->hkey); |
d39f364a |
5912 | |
5913 | /* |
9442dd57 |
5914 | * The exchange hash from the very first key exchange is also |
5915 | * the session id, used in session key construction and |
5916 | * authentication. |
5917 | */ |
e13bba36 |
5918 | if (!s->got_session_id) { |
b672f405 |
5919 | assert(sizeof(s->exchange_hash) <= sizeof(ssh->v2_session_id)); |
9442dd57 |
5920 | memcpy(ssh->v2_session_id, s->exchange_hash, |
5921 | sizeof(s->exchange_hash)); |
b672f405 |
5922 | ssh->v2_session_id_len = ssh->kex->hash->hlen; |
6052bb76 |
5923 | assert(ssh->v2_session_id_len <= sizeof(ssh->v2_session_id)); |
e13bba36 |
5924 | s->got_session_id = TRUE; |
5925 | } |
9442dd57 |
5926 | |
5927 | /* |
7cca0d81 |
5928 | * Send SSH2_MSG_NEWKEYS. |
d39f364a |
5929 | */ |
ff3187f6 |
5930 | s->pktout = ssh2_pkt_init(SSH2_MSG_NEWKEYS); |
590f6a5f |
5931 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
9442dd57 |
5932 | ssh->outgoing_data_size = 0; /* start counting from here */ |
5933 | |
5934 | /* |
5935 | * We've sent client NEWKEYS, so create and initialise |
c64fe7d4 |
5936 | * client-to-server session keys. |
9442dd57 |
5937 | */ |
5938 | if (ssh->cs_cipher_ctx) |
5939 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
5940 | ssh->cscipher = s->cscipher_tobe; |
5941 | ssh->cs_cipher_ctx = ssh->cscipher->make_context(); |
5942 | |
5943 | if (ssh->cs_mac_ctx) |
5944 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
5945 | ssh->csmac = s->csmac_tobe; |
5946 | ssh->cs_mac_ctx = ssh->csmac->make_context(); |
5947 | |
5948 | if (ssh->cs_comp_ctx) |
5949 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
5950 | ssh->cscomp = s->cscomp_tobe; |
5951 | ssh->cs_comp_ctx = ssh->cscomp->compress_init(); |
5952 | |
5953 | /* |
5954 | * Set IVs on client-to-server keys. Here we use the exchange |
5955 | * hash from the _first_ key exchange. |
5956 | */ |
5957 | { |
754c0df9 |
5958 | unsigned char keyspace[SSH2_KEX_MAX_HASH_LEN * SSH2_MKKEY_ITERS]; |
5959 | assert(sizeof(keyspace) >= ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
b672f405 |
5960 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'C',keyspace); |
754c0df9 |
5961 | assert((ssh->cscipher->keylen+7) / 8 <= |
5962 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
9442dd57 |
5963 | ssh->cscipher->setkey(ssh->cs_cipher_ctx, keyspace); |
b672f405 |
5964 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'A',keyspace); |
754c0df9 |
5965 | assert(ssh->cscipher->blksize <= |
5966 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
9442dd57 |
5967 | ssh->cscipher->setiv(ssh->cs_cipher_ctx, keyspace); |
b672f405 |
5968 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'E',keyspace); |
754c0df9 |
5969 | assert(ssh->csmac->len <= |
5970 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
9442dd57 |
5971 | ssh->csmac->setkey(ssh->cs_mac_ctx, keyspace); |
754c0df9 |
5972 | memset(keyspace, 0, sizeof(keyspace)); |
9442dd57 |
5973 | } |
5974 | |
5975 | logeventf(ssh, "Initialised %.200s client->server encryption", |
5976 | ssh->cscipher->text_name); |
5977 | logeventf(ssh, "Initialised %.200s client->server MAC algorithm", |
5978 | ssh->csmac->text_name); |
5979 | if (ssh->cscomp->text_name) |
5980 | logeventf(ssh, "Initialised %s compression", |
5981 | ssh->cscomp->text_name); |
590f6a5f |
5982 | |
5983 | /* |
5984 | * Now our end of the key exchange is complete, we can send all |
5985 | * our queued higher-layer packets. |
5986 | */ |
5987 | ssh->queueing = FALSE; |
5988 | ssh2_pkt_queuesend(ssh); |
d39f364a |
5989 | |
5990 | /* |
8406eaf9 |
5991 | * Expect SSH2_MSG_NEWKEYS from server. |
5992 | */ |
ff3187f6 |
5993 | crWaitUntil(pktin); |
5994 | if (pktin->type != SSH2_MSG_NEWKEYS) { |
6b5cf8b4 |
5995 | bombout(("expected new-keys packet from server")); |
7ffdbc1a |
5996 | crStop(0); |
8406eaf9 |
5997 | } |
9442dd57 |
5998 | ssh->incoming_data_size = 0; /* start counting from here */ |
8406eaf9 |
5999 | |
6000 | /* |
9442dd57 |
6001 | * We've seen server NEWKEYS, so create and initialise |
6002 | * server-to-client session keys. |
d39f364a |
6003 | */ |
371e569c |
6004 | if (ssh->sc_cipher_ctx) |
6005 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
51470298 |
6006 | ssh->sccipher = s->sccipher_tobe; |
371e569c |
6007 | ssh->sc_cipher_ctx = ssh->sccipher->make_context(); |
e0e1a00d |
6008 | |
e0e1a00d |
6009 | if (ssh->sc_mac_ctx) |
6010 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
51470298 |
6011 | ssh->scmac = s->scmac_tobe; |
e0e1a00d |
6012 | ssh->sc_mac_ctx = ssh->scmac->make_context(); |
6013 | |
5366aed8 |
6014 | if (ssh->sc_comp_ctx) |
6015 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
51470298 |
6016 | ssh->sccomp = s->sccomp_tobe; |
5366aed8 |
6017 | ssh->sc_comp_ctx = ssh->sccomp->decompress_init(); |
6018 | |
d39f364a |
6019 | /* |
9442dd57 |
6020 | * Set IVs on server-to-client keys. Here we use the exchange |
6021 | * hash from the _first_ key exchange. |
d39f364a |
6022 | */ |
51470298 |
6023 | { |
754c0df9 |
6024 | unsigned char keyspace[SSH2_KEX_MAX_HASH_LEN * SSH2_MKKEY_ITERS]; |
6025 | assert(sizeof(keyspace) >= ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
b672f405 |
6026 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'D',keyspace); |
754c0df9 |
6027 | assert((ssh->sccipher->keylen+7) / 8 <= |
6028 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
371e569c |
6029 | ssh->sccipher->setkey(ssh->sc_cipher_ctx, keyspace); |
b672f405 |
6030 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'B',keyspace); |
754c0df9 |
6031 | assert(ssh->sccipher->blksize <= |
6032 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
371e569c |
6033 | ssh->sccipher->setiv(ssh->sc_cipher_ctx, keyspace); |
b672f405 |
6034 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'F',keyspace); |
754c0df9 |
6035 | assert(ssh->scmac->len <= |
6036 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
e0e1a00d |
6037 | ssh->scmac->setkey(ssh->sc_mac_ctx, keyspace); |
754c0df9 |
6038 | memset(keyspace, 0, sizeof(keyspace)); |
51470298 |
6039 | } |
57356d63 |
6040 | logeventf(ssh, "Initialised %.200s server->client encryption", |
6041 | ssh->sccipher->text_name); |
6c135243 |
6042 | logeventf(ssh, "Initialised %.200s server->client MAC algorithm", |
6043 | ssh->scmac->text_name); |
57356d63 |
6044 | if (ssh->sccomp->text_name) |
6045 | logeventf(ssh, "Initialised %s decompression", |
6046 | ssh->sccomp->text_name); |
9442dd57 |
6047 | |
6048 | /* |
fae1a71b |
6049 | * Free shared secret. |
9442dd57 |
6050 | */ |
679539d7 |
6051 | freebn(s->K); |
d39f364a |
6052 | |
033b4cef |
6053 | /* |
e13bba36 |
6054 | * Key exchange is over. Loop straight back round if we have a |
6055 | * deferred rekey reason. |
6056 | */ |
6057 | if (ssh->deferred_rekey_reason) { |
6058 | logevent(ssh->deferred_rekey_reason); |
6059 | pktin = NULL; |
6060 | ssh->deferred_rekey_reason = NULL; |
6061 | goto begin_key_exchange; |
6062 | } |
6063 | |
6064 | /* |
6065 | * Otherwise, schedule a timer for our next rekey. |
9442dd57 |
6066 | */ |
6067 | ssh->kex_in_progress = FALSE; |
e6c1536e |
6068 | ssh->last_rekey = GETTICKCOUNT(); |
d57f70af |
6069 | if (ssh->cfg.ssh_rekey_time != 0) |
6070 | ssh->next_rekey = schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC, |
6071 | ssh2_timer, ssh); |
e13bba36 |
6072 | |
9442dd57 |
6073 | /* |
0db56f73 |
6074 | * If this is the first key exchange phase, we must pass the |
6075 | * SSH2_MSG_NEWKEYS packet to the next layer, not because it |
6076 | * wants to see it but because it will need time to initialise |
6077 | * itself before it sees an actual packet. In subsequent key |
6078 | * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because |
6079 | * it would only confuse the layer above. |
6080 | */ |
e13bba36 |
6081 | if (s->activated_authconn) { |
0e3607ed |
6082 | crReturn(0); |
0db56f73 |
6083 | } |
e13bba36 |
6084 | s->activated_authconn = TRUE; |
0db56f73 |
6085 | |
6086 | /* |
7cca0d81 |
6087 | * Now we're encrypting. Begin returning 1 to the protocol main |
6088 | * function so that other things can run on top of the |
6089 | * transport. If we ever see a KEXINIT, we must go back to the |
6090 | * start. |
9442dd57 |
6091 | * |
6092 | * We _also_ go back to the start if we see pktin==NULL and |
6093 | * inlen==-1, because this is a special signal meaning |
6094 | * `initiate client-driven rekey', and `in' contains a message |
6095 | * giving the reason for the rekey. |
033b4cef |
6096 | */ |
9442dd57 |
6097 | while (!((pktin && pktin->type == SSH2_MSG_KEXINIT) || |
6098 | (!pktin && inlen == -1))) { |
f382c87d |
6099 | wait_for_rekey: |
32874aea |
6100 | crReturn(1); |
e96adf72 |
6101 | } |
9442dd57 |
6102 | if (pktin) { |
6103 | logevent("Server initiated key re-exchange"); |
6104 | } else { |
f382c87d |
6105 | /* |
6106 | * Special case: if the server bug is set that doesn't |
6107 | * allow rekeying, we give a different log message and |
6108 | * continue waiting. (If such a server _initiates_ a rekey, |
6109 | * we process it anyway!) |
6110 | */ |
6111 | if ((ssh->remote_bugs & BUG_SSH2_REKEY)) { |
6112 | logeventf(ssh, "Server bug prevents key re-exchange (%s)", |
6113 | (char *)in); |
6114 | /* Reset the counters, so that at least this message doesn't |
6115 | * hit the event log _too_ often. */ |
6116 | ssh->outgoing_data_size = 0; |
6117 | ssh->incoming_data_size = 0; |
6118 | if (ssh->cfg.ssh_rekey_time != 0) { |
6119 | ssh->next_rekey = |
6120 | schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC, |
6121 | ssh2_timer, ssh); |
6122 | } |
6123 | goto wait_for_rekey; /* this is utterly horrid */ |
6124 | } else { |
6125 | logeventf(ssh, "Initiating key re-exchange (%s)", (char *)in); |
f382c87d |
6126 | } |
9442dd57 |
6127 | } |
7cca0d81 |
6128 | goto begin_key_exchange; |
e5574168 |
6129 | |
6130 | crFinish(1); |
6131 | } |
6132 | |
7cca0d81 |
6133 | /* |
2e85c969 |
6134 | * Add data to an SSH-2 channel output buffer. |
783415f8 |
6135 | */ |
32874aea |
6136 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, |
6137 | int len) |
6138 | { |
5471d09a |
6139 | bufchain_add(&c->v.v2.outbuffer, buf, len); |
783415f8 |
6140 | } |
6141 | |
6142 | /* |
2e85c969 |
6143 | * Attempt to send data on an SSH-2 channel. |
783415f8 |
6144 | */ |
5471d09a |
6145 | static int ssh2_try_send(struct ssh_channel *c) |
32874aea |
6146 | { |
51470298 |
6147 | Ssh ssh = c->ssh; |
ff3187f6 |
6148 | struct Packet *pktout; |
51470298 |
6149 | |
5471d09a |
6150 | while (c->v.v2.remwindow > 0 && bufchain_size(&c->v.v2.outbuffer) > 0) { |
6151 | int len; |
6152 | void *data; |
6153 | bufchain_prefix(&c->v.v2.outbuffer, &data, &len); |
6154 | if ((unsigned)len > c->v.v2.remwindow) |
6155 | len = c->v.v2.remwindow; |
6156 | if ((unsigned)len > c->v.v2.remmaxpkt) |
6157 | len = c->v.v2.remmaxpkt; |
ff3187f6 |
6158 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA); |
6159 | ssh2_pkt_adduint32(pktout, c->remoteid); |
ff3187f6 |
6160 | ssh2_pkt_addstring_start(pktout); |
a2724fba |
6161 | dont_log_data(ssh, pktout, PKTLOG_OMIT); |
ff3187f6 |
6162 | ssh2_pkt_addstring_data(pktout, data, len); |
6163 | end_log_omission(ssh, pktout); |
6164 | ssh2_pkt_send(ssh, pktout); |
5471d09a |
6165 | bufchain_consume(&c->v.v2.outbuffer, len); |
6166 | c->v.v2.remwindow -= len; |
6167 | } |
6168 | |
6169 | /* |
6170 | * After having sent as much data as we can, return the amount |
6171 | * still buffered. |
6172 | */ |
6173 | return bufchain_size(&c->v.v2.outbuffer); |
6174 | } |
6175 | |
1bfc7e93 |
6176 | static void ssh2_try_send_and_unthrottle(struct ssh_channel *c) |
6177 | { |
6178 | int bufsize; |
6179 | if (c->closes) |
6180 | return; /* don't send on closing channels */ |
6181 | bufsize = ssh2_try_send(c); |
6182 | if (bufsize == 0) { |
6183 | switch (c->type) { |
6184 | case CHAN_MAINSESSION: |
6185 | /* stdin need not receive an unthrottle |
6186 | * notification since it will be polled */ |
6187 | break; |
6188 | case CHAN_X11: |
6189 | x11_unthrottle(c->u.x11.s); |
6190 | break; |
6191 | case CHAN_AGENT: |
6192 | /* agent sockets are request/response and need no |
6193 | * buffer management */ |
6194 | break; |
6195 | case CHAN_SOCKDATA: |
6196 | pfd_unthrottle(c->u.pfd.s); |
6197 | break; |
6198 | } |
6199 | } |
6200 | } |
6201 | |
5471d09a |
6202 | /* |
2e85c969 |
6203 | * Potentially enlarge the window on an SSH-2 channel. |
5471d09a |
6204 | */ |
9fca3f8c |
6205 | static void ssh2_set_window(struct ssh_channel *c, int newwin) |
5471d09a |
6206 | { |
51470298 |
6207 | Ssh ssh = c->ssh; |
6208 | |
6b69f42e |
6209 | /* |
6210 | * Never send WINDOW_ADJUST for a channel that the remote side |
6211 | * already thinks it's closed; there's no point, since it won't |
6212 | * be sending any more data anyway. |
6213 | */ |
6214 | if (c->closes != 0) |
6215 | return; |
6216 | |
d252310a |
6217 | /* |
6218 | * Only send a WINDOW_ADJUST if there's significantly more window |
6219 | * available than the other end thinks there is. This saves us |
6220 | * sending a WINDOW_ADJUST for every character in a shell session. |
6221 | * |
6222 | * "Significant" is arbitrarily defined as half the window size. |
6223 | */ |
3361b80a |
6224 | if (newwin / 2 >= c->v.v2.locwindow) { |
ff3187f6 |
6225 | struct Packet *pktout; |
0e443c99 |
6226 | struct winadj *wa; |
ff3187f6 |
6227 | |
0e443c99 |
6228 | /* |
6229 | * In order to keep track of how much window the client |
6230 | * actually has available, we'd like it to acknowledge each |
6231 | * WINDOW_ADJUST. We can't do that directly, so we accompany |
6232 | * it with a CHANNEL_REQUEST that has to be acknowledged. |
6233 | * |
6234 | * This is only necessary if we're opening the window wide. |
6235 | * If we're not, then throughput is being constrained by |
6236 | * something other than the maximum window size anyway. |
6237 | * |
6238 | * We also only send this if the main channel has finished its |
6239 | * initial CHANNEL_REQUESTs and installed the default |
6240 | * CHANNEL_FAILURE handler, so as not to risk giving it |
6241 | * unexpected CHANNEL_FAILUREs. |
6242 | */ |
6243 | if (newwin == c->v.v2.locmaxwin && |
6244 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE]) { |
6245 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
6246 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6247 | ssh2_pkt_addstring(pktout, "winadj@putty.projects.tartarus.org"); |
6248 | ssh2_pkt_addbool(pktout, TRUE); |
6249 | ssh2_pkt_send(ssh, pktout); |
6250 | |
6251 | /* |
6252 | * CHANNEL_FAILURE doesn't come with any indication of |
6253 | * what message caused it, so we have to keep track of the |
6254 | * outstanding CHANNEL_REQUESTs ourselves. |
6255 | */ |
6256 | wa = snew(struct winadj); |
6257 | wa->size = newwin - c->v.v2.locwindow; |
6258 | wa->next = NULL; |
6259 | if (!c->v.v2.winadj_head) |
6260 | c->v.v2.winadj_head = wa; |
6261 | else |
6262 | c->v.v2.winadj_tail->next = wa; |
6263 | c->v.v2.winadj_tail = wa; |
6264 | if (c->v.v2.throttle_state != UNTHROTTLED) |
6265 | c->v.v2.throttle_state = UNTHROTTLING; |
6266 | } else { |
6267 | /* Pretend the WINDOW_ADJUST was acked immediately. */ |
6268 | c->v.v2.remlocwin = newwin; |
6269 | c->v.v2.throttle_state = THROTTLED; |
6270 | } |
ff3187f6 |
6271 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
6272 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6273 | ssh2_pkt_adduint32(pktout, newwin - c->v.v2.locwindow); |
6274 | ssh2_pkt_send(ssh, pktout); |
5471d09a |
6275 | c->v.v2.locwindow = newwin; |
783415f8 |
6276 | } |
6277 | } |
6278 | |
0e443c99 |
6279 | static void ssh2_msg_channel_failure(Ssh ssh, struct Packet *pktin) |
6280 | { |
6281 | /* |
6282 | * The only time this should get called is for "winadj@putty" |
6283 | * messages sent above. All other channel requests are either |
6284 | * sent with want_reply false or are sent before this handler gets |
6285 | * installed. |
6286 | */ |
6287 | unsigned i = ssh_pkt_getuint32(pktin); |
6288 | struct ssh_channel *c; |
6289 | struct winadj *wa; |
6290 | |
6291 | c = find234(ssh->channels, &i, ssh_channelfind); |
6292 | if (!c) |
6293 | return; /* nonexistent channel */ |
6294 | wa = c->v.v2.winadj_head; |
6295 | if (!wa) |
6296 | logevent("excess SSH_MSG_CHANNEL_FAILURE"); |
6297 | else { |
6298 | c->v.v2.winadj_head = wa->next; |
6299 | c->v.v2.remlocwin += wa->size; |
6300 | sfree(wa); |
6301 | /* |
6302 | * winadj messages are only sent when the window is fully open, |
6303 | * so if we get an ack of one, we know any pending unthrottle |
6304 | * is complete. |
6305 | */ |
6306 | if (c->v.v2.throttle_state == UNTHROTTLING) |
6307 | c->v.v2.throttle_state = UNTHROTTLED; |
6308 | } |
6309 | } |
6310 | |
51df0ab5 |
6311 | static void ssh2_msg_channel_window_adjust(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
6312 | { |
6313 | unsigned i = ssh_pkt_getuint32(pktin); |
6314 | struct ssh_channel *c; |
6315 | c = find234(ssh->channels, &i, ssh_channelfind); |
1bfc7e93 |
6316 | if (c && !c->closes) { |
b09eaa88 |
6317 | c->v.v2.remwindow += ssh_pkt_getuint32(pktin); |
1bfc7e93 |
6318 | ssh2_try_send_and_unthrottle(c); |
6319 | } |
b09eaa88 |
6320 | } |
6321 | |
51df0ab5 |
6322 | static void ssh2_msg_channel_data(Ssh ssh, struct Packet *pktin) |
6323 | { |
6324 | char *data; |
6d44acc9 |
6325 | int length; |
51df0ab5 |
6326 | unsigned i = ssh_pkt_getuint32(pktin); |
6327 | struct ssh_channel *c; |
6328 | c = find234(ssh->channels, &i, ssh_channelfind); |
6329 | if (!c) |
6330 | return; /* nonexistent channel */ |
6331 | if (pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA && |
6332 | ssh_pkt_getuint32(pktin) != SSH2_EXTENDED_DATA_STDERR) |
6333 | return; /* extended but not stderr */ |
6334 | ssh_pkt_getstring(pktin, &data, &length); |
6335 | if (data) { |
6336 | int bufsize = 0; |
6337 | c->v.v2.locwindow -= length; |
0e443c99 |
6338 | c->v.v2.remlocwin -= length; |
51df0ab5 |
6339 | switch (c->type) { |
6340 | case CHAN_MAINSESSION: |
6341 | bufsize = |
6342 | from_backend(ssh->frontend, pktin->type == |
6343 | SSH2_MSG_CHANNEL_EXTENDED_DATA, |
6344 | data, length); |
6345 | break; |
6346 | case CHAN_X11: |
6347 | bufsize = x11_send(c->u.x11.s, data, length); |
6348 | break; |
6349 | case CHAN_SOCKDATA: |
6350 | bufsize = pfd_send(c->u.pfd.s, data, length); |
6351 | break; |
6352 | case CHAN_AGENT: |
6353 | while (length > 0) { |
6354 | if (c->u.a.lensofar < 4) { |
62ddb51e |
6355 | unsigned int l = min(4 - c->u.a.lensofar, |
6356 | (unsigned)length); |
51df0ab5 |
6357 | memcpy(c->u.a.msglen + c->u.a.lensofar, |
6358 | data, l); |
6359 | data += l; |
6360 | length -= l; |
6361 | c->u.a.lensofar += l; |
6362 | } |
6363 | if (c->u.a.lensofar == 4) { |
6364 | c->u.a.totallen = |
6365 | 4 + GET_32BIT(c->u.a.msglen); |
6366 | c->u.a.message = snewn(c->u.a.totallen, |
6367 | unsigned char); |
6368 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
6369 | } |
6370 | if (c->u.a.lensofar >= 4 && length > 0) { |
aa63ab7e |
6371 | unsigned int l = |
51df0ab5 |
6372 | min(c->u.a.totallen - c->u.a.lensofar, |
62ddb51e |
6373 | (unsigned)length); |
51df0ab5 |
6374 | memcpy(c->u.a.message + c->u.a.lensofar, |
6375 | data, l); |
6376 | data += l; |
6377 | length -= l; |
6378 | c->u.a.lensofar += l; |
6379 | } |
6380 | if (c->u.a.lensofar == c->u.a.totallen) { |
6381 | void *reply; |
6382 | int replylen; |
6383 | if (agent_query(c->u.a.message, |
6384 | c->u.a.totallen, |
6385 | &reply, &replylen, |
6386 | ssh_agentf_callback, c)) |
6387 | ssh_agentf_callback(c, reply, replylen); |
6388 | sfree(c->u.a.message); |
6389 | c->u.a.lensofar = 0; |
6390 | } |
6391 | } |
6392 | bufsize = 0; |
6393 | break; |
6394 | } |
6395 | /* |
0e443c99 |
6396 | * If it looks like the remote end hit the end of its window, |
6397 | * and we didn't want it to do that, think about using a |
6398 | * larger window. |
6399 | */ |
6400 | if (c->v.v2.remlocwin <= 0 && c->v.v2.throttle_state == UNTHROTTLED && |
6401 | c->v.v2.locmaxwin < 0x40000000) |
6402 | c->v.v2.locmaxwin += OUR_V2_WINSIZE; |
6403 | /* |
51df0ab5 |
6404 | * If we are not buffering too much data, |
6405 | * enlarge the window again at the remote side. |
467e064d |
6406 | * If we are buffering too much, we may still |
6407 | * need to adjust the window if the server's |
6408 | * sent excess data. |
51df0ab5 |
6409 | */ |
9b53e9b8 |
6410 | ssh2_set_window(c, bufsize < c->v.v2.locmaxwin ? |
6411 | c->v.v2.locmaxwin - bufsize : 0); |
51df0ab5 |
6412 | } |
6413 | } |
6414 | |
6415 | static void ssh2_msg_channel_eof(Ssh ssh, struct Packet *pktin) |
6416 | { |
6417 | unsigned i = ssh_pkt_getuint32(pktin); |
6418 | struct ssh_channel *c; |
6419 | |
6420 | c = find234(ssh->channels, &i, ssh_channelfind); |
6421 | if (!c) |
6422 | return; /* nonexistent channel */ |
6423 | |
6424 | if (c->type == CHAN_X11) { |
6425 | /* |
6426 | * Remote EOF on an X11 channel means we should |
6427 | * wrap up and close the channel ourselves. |
6428 | */ |
6429 | x11_close(c->u.x11.s); |
6430 | sshfwd_close(c); |
6431 | } else if (c->type == CHAN_AGENT) { |
6432 | sshfwd_close(c); |
6433 | } else if (c->type == CHAN_SOCKDATA) { |
6434 | pfd_close(c->u.pfd.s); |
6435 | sshfwd_close(c); |
6436 | } |
6437 | } |
6438 | |
6439 | static void ssh2_msg_channel_close(Ssh ssh, struct Packet *pktin) |
6440 | { |
6441 | unsigned i = ssh_pkt_getuint32(pktin); |
6442 | struct ssh_channel *c; |
6443 | struct Packet *pktout; |
6444 | |
6445 | c = find234(ssh->channels, &i, ssh_channelfind); |
64d6ff88 |
6446 | if (!c || c->halfopen) { |
51df0ab5 |
6447 | bombout(("Received CHANNEL_CLOSE for %s channel %d\n", |
6448 | c ? "half-open" : "nonexistent", i)); |
6449 | return; |
6450 | } |
6451 | /* Do pre-close processing on the channel. */ |
6452 | switch (c->type) { |
6453 | case CHAN_MAINSESSION: |
6454 | ssh->mainchan = NULL; |
6455 | update_specials_menu(ssh->frontend); |
6456 | break; |
6457 | case CHAN_X11: |
6458 | if (c->u.x11.s != NULL) |
6459 | x11_close(c->u.x11.s); |
6460 | sshfwd_close(c); |
6461 | break; |
6462 | case CHAN_AGENT: |
6463 | sshfwd_close(c); |
6464 | break; |
6465 | case CHAN_SOCKDATA: |
6466 | if (c->u.pfd.s != NULL) |
6467 | pfd_close(c->u.pfd.s); |
6468 | sshfwd_close(c); |
6469 | break; |
6470 | } |
6471 | if (c->closes == 0) { |
6472 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
6473 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6474 | ssh2_pkt_send(ssh, pktout); |
6475 | } |
6476 | del234(ssh->channels, c); |
6477 | bufchain_clear(&c->v.v2.outbuffer); |
6478 | sfree(c); |
6479 | |
6480 | /* |
6481 | * See if that was the last channel left open. |
6482 | * (This is only our termination condition if we're |
6483 | * not running in -N mode.) |
6484 | */ |
6485 | if (!ssh->cfg.ssh_no_shell && count234(ssh->channels) == 0) { |
51df0ab5 |
6486 | /* |
6487 | * We used to send SSH_MSG_DISCONNECT here, |
6488 | * because I'd believed that _every_ conforming |
2e85c969 |
6489 | * SSH-2 connection had to end with a disconnect |
51df0ab5 |
6490 | * being sent by at least one side; apparently |
6491 | * I was wrong and it's perfectly OK to |
6492 | * unceremoniously slam the connection shut |
6493 | * when you're done, and indeed OpenSSH feels |
6494 | * this is more polite than sending a |
6495 | * DISCONNECT. So now we don't. |
6496 | */ |
9e296bfa |
6497 | ssh_disconnect(ssh, "All channels closed", NULL, 0, TRUE); |
51df0ab5 |
6498 | } |
6499 | } |
6500 | |
6501 | static void ssh2_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin) |
6502 | { |
6503 | unsigned i = ssh_pkt_getuint32(pktin); |
6504 | struct ssh_channel *c; |
6505 | struct Packet *pktout; |
6506 | |
6507 | c = find234(ssh->channels, &i, ssh_channelfind); |
6508 | if (!c) |
6509 | return; /* nonexistent channel */ |
6510 | if (c->type != CHAN_SOCKDATA_DORMANT) |
6511 | return; /* dunno why they're confirming this */ |
6512 | c->remoteid = ssh_pkt_getuint32(pktin); |
64d6ff88 |
6513 | c->halfopen = FALSE; |
51df0ab5 |
6514 | c->type = CHAN_SOCKDATA; |
6515 | c->v.v2.remwindow = ssh_pkt_getuint32(pktin); |
6516 | c->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin); |
6517 | if (c->u.pfd.s) |
6518 | pfd_confirm(c->u.pfd.s); |
6519 | if (c->closes) { |
6520 | /* |
6521 | * We have a pending close on this channel, |
6522 | * which we decided on before the server acked |
6523 | * the channel open. So now we know the |
6524 | * remoteid, we can close it again. |
6525 | */ |
6526 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
6527 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6528 | ssh2_pkt_send(ssh, pktout); |
6529 | } |
6530 | } |
6531 | |
6532 | static void ssh2_msg_channel_open_failure(Ssh ssh, struct Packet *pktin) |
6533 | { |
6534 | static const char *const reasons[] = { |
6535 | "<unknown reason code>", |
6536 | "Administratively prohibited", |
6537 | "Connect failed", |
6538 | "Unknown channel type", |
6539 | "Resource shortage", |
6540 | }; |
6541 | unsigned i = ssh_pkt_getuint32(pktin); |
6542 | unsigned reason_code; |
6543 | char *reason_string; |
6544 | int reason_length; |
51df0ab5 |
6545 | struct ssh_channel *c; |
6546 | c = find234(ssh->channels, &i, ssh_channelfind); |
6547 | if (!c) |
6548 | return; /* nonexistent channel */ |
6549 | if (c->type != CHAN_SOCKDATA_DORMANT) |
6550 | return; /* dunno why they're failing this */ |
6551 | |
6552 | reason_code = ssh_pkt_getuint32(pktin); |
6553 | if (reason_code >= lenof(reasons)) |
6554 | reason_code = 0; /* ensure reasons[reason_code] in range */ |
6555 | ssh_pkt_getstring(pktin, &reason_string, &reason_length); |
fb983202 |
6556 | logeventf(ssh, "Forwarded connection refused by server: %s [%.*s]", |
6557 | reasons[reason_code], reason_length, reason_string); |
51df0ab5 |
6558 | |
6559 | pfd_close(c->u.pfd.s); |
6560 | |
6561 | del234(ssh->channels, c); |
6562 | sfree(c); |
6563 | } |
6564 | |
6565 | static void ssh2_msg_channel_request(Ssh ssh, struct Packet *pktin) |
6566 | { |
6567 | unsigned localid; |
6568 | char *type; |
6569 | int typelen, want_reply; |
6570 | int reply = SSH2_MSG_CHANNEL_FAILURE; /* default */ |
6571 | struct ssh_channel *c; |
6572 | struct Packet *pktout; |
6573 | |
6574 | localid = ssh_pkt_getuint32(pktin); |
6575 | ssh_pkt_getstring(pktin, &type, &typelen); |
6576 | want_reply = ssh2_pkt_getbool(pktin); |
6577 | |
6578 | /* |
6579 | * First, check that the channel exists. Otherwise, |
6580 | * we can instantly disconnect with a rude message. |
6581 | */ |
6582 | c = find234(ssh->channels, &localid, ssh_channelfind); |
6583 | if (!c) { |
9e296bfa |
6584 | char *buf = dupprintf("Received channel request for nonexistent" |
6585 | " channel %d", localid); |
6586 | ssh_disconnect(ssh, NULL, buf, SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE); |
6587 | sfree(buf); |
51df0ab5 |
6588 | return; |
6589 | } |
6590 | |
6591 | /* |
6592 | * Having got the channel number, we now look at |
6593 | * the request type string to see if it's something |
6594 | * we recognise. |
6595 | */ |
6596 | if (c == ssh->mainchan) { |
6597 | /* |
6598 | * We recognise "exit-status" and "exit-signal" on |
6599 | * the primary channel. |
6600 | */ |
6601 | if (typelen == 11 && |
6602 | !memcmp(type, "exit-status", 11)) { |
6603 | |
6604 | ssh->exitcode = ssh_pkt_getuint32(pktin); |
6605 | logeventf(ssh, "Server sent command exit status %d", |
6606 | ssh->exitcode); |
6607 | reply = SSH2_MSG_CHANNEL_SUCCESS; |
6608 | |
6609 | } else if (typelen == 11 && |
6610 | !memcmp(type, "exit-signal", 11)) { |
6611 | |
6612 | int is_plausible = TRUE, is_int = FALSE; |
6613 | char *fmt_sig = "", *fmt_msg = ""; |
6614 | char *msg; |
6615 | int msglen = 0, core = FALSE; |
6616 | /* ICK: older versions of OpenSSH (e.g. 3.4p1) |
6617 | * provide an `int' for the signal, despite its |
6618 | * having been a `string' in the drafts since at |
6619 | * least 2001. (Fixed in session.c 1.147.) Try to |
6620 | * infer which we can safely parse it as. */ |
6621 | { |
6622 | unsigned char *p = pktin->body + |
6623 | pktin->savedpos; |
6624 | long len = pktin->length - pktin->savedpos; |
6625 | unsigned long num = GET_32BIT(p); /* what is it? */ |
6626 | /* If it's 0, it hardly matters; assume string */ |
6627 | if (num == 0) { |
6628 | is_int = FALSE; |
6629 | } else { |
6630 | int maybe_int = FALSE, maybe_str = FALSE; |
6631 | #define CHECK_HYPOTHESIS(offset, result) \ |
6632 | do { \ |
6633 | long q = offset; \ |
6634 | if (q >= 0 && q+4 <= len) { \ |
6635 | q = q + 4 + GET_32BIT(p+q); \ |
6636 | if (q >= 0 && q+4 <= len && \ |
c849ff23 |
6637 | ((q = q + 4 + GET_32BIT(p+q))!= 0) && q == len) \ |
51df0ab5 |
6638 | result = TRUE; \ |
6639 | } \ |
6640 | } while(0) |
6641 | CHECK_HYPOTHESIS(4+1, maybe_int); |
6642 | CHECK_HYPOTHESIS(4+num+1, maybe_str); |
6643 | #undef CHECK_HYPOTHESIS |
6644 | if (maybe_int && !maybe_str) |
6645 | is_int = TRUE; |
6646 | else if (!maybe_int && maybe_str) |
6647 | is_int = FALSE; |
6648 | else |
6649 | /* Crikey. Either or neither. Panic. */ |
6650 | is_plausible = FALSE; |
6651 | } |
6652 | } |
d051d1b4 |
6653 | ssh->exitcode = 128; /* means `unknown signal' */ |
51df0ab5 |
6654 | if (is_plausible) { |
6655 | if (is_int) { |
6656 | /* Old non-standard OpenSSH. */ |
6657 | int signum = ssh_pkt_getuint32(pktin); |
6658 | fmt_sig = dupprintf(" %d", signum); |
d051d1b4 |
6659 | ssh->exitcode = 128 + signum; |
51df0ab5 |
6660 | } else { |
6661 | /* As per the drafts. */ |
6662 | char *sig; |
6663 | int siglen; |
6664 | ssh_pkt_getstring(pktin, &sig, &siglen); |
6665 | /* Signal name isn't supposed to be blank, but |
6666 | * let's cope gracefully if it is. */ |
6667 | if (siglen) { |
6668 | fmt_sig = dupprintf(" \"%.*s\"", |
6669 | siglen, sig); |
6670 | } |
d051d1b4 |
6671 | |
6672 | /* |
6673 | * Really hideous method of translating the |
6674 | * signal description back into a locally |
6675 | * meaningful number. |
6676 | */ |
6677 | |
6678 | if (0) |
6679 | ; |
b707973a |
6680 | #define TRANSLATE_SIGNAL(s) \ |
6681 | else if (siglen == lenof(#s)-1 && !memcmp(sig, #s, siglen)) \ |
6682 | ssh->exitcode = 128 + SIG ## s |
d051d1b4 |
6683 | #ifdef SIGABRT |
b707973a |
6684 | TRANSLATE_SIGNAL(ABRT); |
d051d1b4 |
6685 | #endif |
6686 | #ifdef SIGALRM |
b707973a |
6687 | TRANSLATE_SIGNAL(ALRM); |
d051d1b4 |
6688 | #endif |
6689 | #ifdef SIGFPE |
b707973a |
6690 | TRANSLATE_SIGNAL(FPE); |
d051d1b4 |
6691 | #endif |
6692 | #ifdef SIGHUP |
b707973a |
6693 | TRANSLATE_SIGNAL(HUP); |
d051d1b4 |
6694 | #endif |
6695 | #ifdef SIGILL |
b707973a |
6696 | TRANSLATE_SIGNAL(ILL); |
d051d1b4 |
6697 | #endif |
6698 | #ifdef SIGINT |
b707973a |
6699 | TRANSLATE_SIGNAL(INT); |
d051d1b4 |
6700 | #endif |
6701 | #ifdef SIGKILL |
b707973a |
6702 | TRANSLATE_SIGNAL(KILL); |
d051d1b4 |
6703 | #endif |
6704 | #ifdef SIGPIPE |
b707973a |
6705 | TRANSLATE_SIGNAL(PIPE); |
d051d1b4 |
6706 | #endif |
6707 | #ifdef SIGQUIT |
b707973a |
6708 | TRANSLATE_SIGNAL(QUIT); |
d051d1b4 |
6709 | #endif |
6710 | #ifdef SIGSEGV |
b707973a |
6711 | TRANSLATE_SIGNAL(SEGV); |
d051d1b4 |
6712 | #endif |
6713 | #ifdef SIGTERM |
b707973a |
6714 | TRANSLATE_SIGNAL(TERM); |
d051d1b4 |
6715 | #endif |
6716 | #ifdef SIGUSR1 |
b707973a |
6717 | TRANSLATE_SIGNAL(USR1); |
d051d1b4 |
6718 | #endif |
6719 | #ifdef SIGUSR2 |
b707973a |
6720 | TRANSLATE_SIGNAL(USR2); |
d051d1b4 |
6721 | #endif |
b707973a |
6722 | #undef TRANSLATE_SIGNAL |
d051d1b4 |
6723 | else |
6724 | ssh->exitcode = 128; |
51df0ab5 |
6725 | } |
6726 | core = ssh2_pkt_getbool(pktin); |
6727 | ssh_pkt_getstring(pktin, &msg, &msglen); |
6728 | if (msglen) { |
6729 | fmt_msg = dupprintf(" (\"%.*s\")", msglen, msg); |
6730 | } |
6731 | /* ignore lang tag */ |
6732 | } /* else don't attempt to parse */ |
6733 | logeventf(ssh, "Server exited on signal%s%s%s", |
6734 | fmt_sig, core ? " (core dumped)" : "", |
6735 | fmt_msg); |
6736 | if (*fmt_sig) sfree(fmt_sig); |
6737 | if (*fmt_msg) sfree(fmt_msg); |
6738 | reply = SSH2_MSG_CHANNEL_SUCCESS; |
6739 | |
6740 | } |
6741 | } else { |
6742 | /* |
6743 | * This is a channel request we don't know |
6744 | * about, so we now either ignore the request |
6745 | * or respond with CHANNEL_FAILURE, depending |
6746 | * on want_reply. |
6747 | */ |
6748 | reply = SSH2_MSG_CHANNEL_FAILURE; |
6749 | } |
6750 | if (want_reply) { |
6751 | pktout = ssh2_pkt_init(reply); |
6752 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6753 | ssh2_pkt_send(ssh, pktout); |
6754 | } |
6755 | } |
6756 | |
6757 | static void ssh2_msg_global_request(Ssh ssh, struct Packet *pktin) |
6758 | { |
6759 | char *type; |
6760 | int typelen, want_reply; |
6761 | struct Packet *pktout; |
6762 | |
6763 | ssh_pkt_getstring(pktin, &type, &typelen); |
6764 | want_reply = ssh2_pkt_getbool(pktin); |
6765 | |
6766 | /* |
6767 | * We currently don't support any global requests |
6768 | * at all, so we either ignore the request or |
6769 | * respond with REQUEST_FAILURE, depending on |
6770 | * want_reply. |
6771 | */ |
6772 | if (want_reply) { |
6773 | pktout = ssh2_pkt_init(SSH2_MSG_REQUEST_FAILURE); |
6774 | ssh2_pkt_send(ssh, pktout); |
6775 | } |
6776 | } |
6777 | |
6778 | static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin) |
6779 | { |
6780 | char *type; |
6781 | int typelen; |
6782 | char *peeraddr; |
6783 | int peeraddrlen; |
6784 | int peerport; |
6785 | char *error = NULL; |
6786 | struct ssh_channel *c; |
6787 | unsigned remid, winsize, pktsize; |
6788 | struct Packet *pktout; |
6789 | |
6790 | ssh_pkt_getstring(pktin, &type, &typelen); |
6791 | c = snew(struct ssh_channel); |
6792 | c->ssh = ssh; |
6793 | |
6794 | remid = ssh_pkt_getuint32(pktin); |
6795 | winsize = ssh_pkt_getuint32(pktin); |
6796 | pktsize = ssh_pkt_getuint32(pktin); |
6797 | |
6798 | if (typelen == 3 && !memcmp(type, "x11", 3)) { |
6799 | char *addrstr; |
6800 | |
6801 | ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen); |
6802 | addrstr = snewn(peeraddrlen+1, char); |
6803 | memcpy(addrstr, peeraddr, peeraddrlen); |
6804 | addrstr[peeraddrlen] = '\0'; |
6805 | peerport = ssh_pkt_getuint32(pktin); |
6806 | |
6807 | logeventf(ssh, "Received X11 connect request from %s:%d", |
6808 | addrstr, peerport); |
6809 | |
6810 | if (!ssh->X11_fwd_enabled) |
6811 | error = "X11 forwarding is not enabled"; |
6812 | else if (x11_init(&c->u.x11.s, ssh->cfg.x11_display, c, |
6813 | ssh->x11auth, addrstr, peerport, |
6814 | &ssh->cfg) != NULL) { |
6815 | error = "Unable to open an X11 connection"; |
6816 | } else { |
6817 | logevent("Opening X11 forward connection succeeded"); |
6818 | c->type = CHAN_X11; |
6819 | } |
6820 | |
6821 | sfree(addrstr); |
6822 | } else if (typelen == 15 && |
6823 | !memcmp(type, "forwarded-tcpip", 15)) { |
6824 | struct ssh_rportfwd pf, *realpf; |
6825 | char *dummy; |
6826 | int dummylen; |
6827 | ssh_pkt_getstring(pktin, &dummy, &dummylen);/* skip address */ |
6828 | pf.sport = ssh_pkt_getuint32(pktin); |
6829 | ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen); |
6830 | peerport = ssh_pkt_getuint32(pktin); |
6831 | realpf = find234(ssh->rportfwds, &pf, NULL); |
6832 | logeventf(ssh, "Received remote port %d open request " |
6833 | "from %s:%d", pf.sport, peeraddr, peerport); |
6834 | if (realpf == NULL) { |
6835 | error = "Remote port is not recognised"; |
6836 | } else { |
6837 | const char *e = pfd_newconnect(&c->u.pfd.s, |
6838 | realpf->dhost, |
6839 | realpf->dport, c, |
05581745 |
6840 | &ssh->cfg, |
6841 | realpf->pfrec->addressfamily); |
51df0ab5 |
6842 | logeventf(ssh, "Attempting to forward remote port to " |
6843 | "%s:%d", realpf->dhost, realpf->dport); |
6844 | if (e != NULL) { |
6845 | logeventf(ssh, "Port open failed: %s", e); |
6846 | error = "Port open failed"; |
6847 | } else { |
6848 | logevent("Forwarded port opened successfully"); |
6849 | c->type = CHAN_SOCKDATA; |
6850 | } |
6851 | } |
6852 | } else if (typelen == 22 && |
c49cf994 |
6853 | !memcmp(type, "auth-agent@openssh.com", 22)) { |
51df0ab5 |
6854 | if (!ssh->agentfwd_enabled) |
6855 | error = "Agent forwarding is not enabled"; |
6856 | else { |
6857 | c->type = CHAN_AGENT; /* identify channel type */ |
6858 | c->u.a.lensofar = 0; |
6859 | } |
6860 | } else { |
6861 | error = "Unsupported channel type requested"; |
6862 | } |
6863 | |
6864 | c->remoteid = remid; |
64d6ff88 |
6865 | c->halfopen = FALSE; |
51df0ab5 |
6866 | if (error) { |
6867 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
6868 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6869 | ssh2_pkt_adduint32(pktout, SSH2_OPEN_CONNECT_FAILED); |
6870 | ssh2_pkt_addstring(pktout, error); |
6871 | ssh2_pkt_addstring(pktout, "en"); /* language tag */ |
6872 | ssh2_pkt_send(ssh, pktout); |
6873 | logeventf(ssh, "Rejected channel open: %s", error); |
6874 | sfree(c); |
6875 | } else { |
6876 | c->localid = alloc_channel_id(ssh); |
6877 | c->closes = 0; |
9b53e9b8 |
6878 | c->v.v2.locwindow = c->v.v2.locmaxwin = OUR_V2_WINSIZE; |
51df0ab5 |
6879 | c->v.v2.remwindow = winsize; |
6880 | c->v.v2.remmaxpkt = pktsize; |
0e443c99 |
6881 | c->v.v2.remlocwin = OUR_V2_WINSIZE; |
6882 | c->v.v2.winadj_head = c->v.v2.winadj_tail = NULL; |
6883 | c->v.v2.throttle_state = UNTHROTTLED; |
51df0ab5 |
6884 | bufchain_init(&c->v.v2.outbuffer); |
6885 | add234(ssh->channels, c); |
6886 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
6887 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6888 | ssh2_pkt_adduint32(pktout, c->localid); |
6889 | ssh2_pkt_adduint32(pktout, c->v.v2.locwindow); |
954d5c5a |
6890 | ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
51df0ab5 |
6891 | ssh2_pkt_send(ssh, pktout); |
6892 | } |
6893 | } |
6894 | |
6bbce591 |
6895 | /* |
6896 | * Buffer banner messages for later display at some convenient point. |
6897 | */ |
6898 | static void ssh2_msg_userauth_banner(Ssh ssh, struct Packet *pktin) |
6899 | { |
6900 | /* Arbitrary limit to prevent unbounded inflation of buffer */ |
6901 | if (bufchain_size(&ssh->banner) <= 131072) { |
6902 | char *banner = NULL; |
6903 | int size = 0; |
6904 | ssh_pkt_getstring(pktin, &banner, &size); |
6905 | if (banner) |
6906 | bufchain_add(&ssh->banner, banner, size); |
6907 | } |
6908 | } |
6909 | |
c6ccd5c2 |
6910 | /* Helper function to deal with sending tty modes for "pty-req" */ |
6911 | static void ssh2_send_ttymode(void *data, char *mode, char *val) |
6912 | { |
6913 | struct Packet *pktout = (struct Packet *)data; |
6914 | int i = 0; |
6915 | unsigned int arg = 0; |
6916 | while (strcmp(mode, ssh_ttymodes[i].mode) != 0) i++; |
6917 | if (i == lenof(ssh_ttymodes)) return; |
6918 | switch (ssh_ttymodes[i].type) { |
6919 | case TTY_OP_CHAR: |
6920 | arg = ssh_tty_parse_specchar(val); |
6921 | break; |
6922 | case TTY_OP_BOOL: |
6923 | arg = ssh_tty_parse_boolean(val); |
6924 | break; |
6925 | } |
6926 | ssh2_pkt_addbyte(pktout, ssh_ttymodes[i].opcode); |
6927 | ssh2_pkt_adduint32(pktout, arg); |
6928 | } |
6929 | |
783415f8 |
6930 | /* |
2e85c969 |
6931 | * Handle the SSH-2 userauth and connection layers. |
7cca0d81 |
6932 | */ |
ff3187f6 |
6933 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, |
6934 | struct Packet *pktin) |
7cca0d81 |
6935 | { |
51470298 |
6936 | struct do_ssh2_authconn_state { |
6937 | enum { |
51470298 |
6938 | AUTH_TYPE_NONE, |
6939 | AUTH_TYPE_PUBLICKEY, |
6940 | AUTH_TYPE_PUBLICKEY_OFFER_LOUD, |
6941 | AUTH_TYPE_PUBLICKEY_OFFER_QUIET, |
6942 | AUTH_TYPE_PASSWORD, |
6943 | AUTH_TYPE_KEYBOARD_INTERACTIVE, |
6944 | AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET |
6945 | } type; |
a1a1fae4 |
6946 | int done_service_req; |
51470298 |
6947 | int gotit, need_pw, can_pubkey, can_passwd, can_keyb_inter; |
94cd7c3a |
6948 | int tried_pubkey_config, done_agent; |
edd0cb8a |
6949 | int kbd_inter_refused; |
51470298 |
6950 | int we_are_in; |
edd0cb8a |
6951 | prompts_t *cur_prompt; |
6952 | int num_prompts; |
51470298 |
6953 | char username[100]; |
e955d348 |
6954 | char *password; |
51470298 |
6955 | int got_username; |
51470298 |
6956 | void *publickey_blob; |
6957 | int publickey_bloblen; |
edd0cb8a |
6958 | int publickey_encrypted; |
6959 | char *publickey_algorithm; |
6960 | char *publickey_comment; |
94cd7c3a |
6961 | unsigned char agent_request[5], *agent_response, *agentp; |
6962 | int agent_responselen; |
6963 | unsigned char *pkblob_in_agent; |
51470298 |
6964 | int keyi, nkeys; |
51470298 |
6965 | char *pkblob, *alg, *commentp; |
6966 | int pklen, alglen, commentlen; |
6967 | int siglen, retlen, len; |
6968 | char *q, *agentreq, *ret; |
6969 | int try_send; |
73feed4f |
6970 | int num_env, env_left, env_ok; |
ff3187f6 |
6971 | struct Packet *pktout; |
51470298 |
6972 | }; |
6973 | crState(do_ssh2_authconn_state); |
6974 | |
6975 | crBegin(ssh->do_ssh2_authconn_crstate); |
e5574168 |
6976 | |
a1a1fae4 |
6977 | s->done_service_req = FALSE; |
6978 | s->we_are_in = FALSE; |
6979 | if (!ssh->cfg.ssh_no_userauth) { |
6980 | /* |
6981 | * Request userauth protocol, and await a response to it. |
6982 | */ |
6983 | s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST); |
6984 | ssh2_pkt_addstring(s->pktout, "ssh-userauth"); |
6985 | ssh2_pkt_send(ssh, s->pktout); |
6986 | crWaitUntilV(pktin); |
6987 | if (pktin->type == SSH2_MSG_SERVICE_ACCEPT) |
6988 | s->done_service_req = TRUE; |
6989 | } |
6990 | if (!s->done_service_req) { |
6991 | /* |
6992 | * Request connection protocol directly, without authentication. |
6993 | */ |
6994 | s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST); |
6995 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
6996 | ssh2_pkt_send(ssh, s->pktout); |
6997 | crWaitUntilV(pktin); |
6998 | if (pktin->type == SSH2_MSG_SERVICE_ACCEPT) { |
6999 | s->we_are_in = TRUE; /* no auth required */ |
7000 | } else { |
7001 | bombout(("Server refused service request")); |
7002 | crStopV; |
7003 | } |
8d5de777 |
7004 | } |
7cca0d81 |
7005 | |
94cd7c3a |
7006 | /* Arrange to be able to deal with any BANNERs that come in. |
7007 | * (We do this now as packets may come in during the next bit.) */ |
7008 | bufchain_init(&ssh->banner); |
7009 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = |
7010 | ssh2_msg_userauth_banner; |
7011 | |
7cca0d81 |
7012 | /* |
edd0cb8a |
7013 | * Misc one-time setup for authentication. |
7014 | */ |
7015 | s->publickey_blob = NULL; |
7016 | if (!s->we_are_in) { |
7017 | |
7018 | /* |
7019 | * Load the public half of any configured public key file |
7020 | * for later use. |
7021 | */ |
7022 | if (!filename_is_null(ssh->cfg.keyfile)) { |
7023 | int keytype; |
7024 | logeventf(ssh, "Reading private key file \"%.150s\"", |
7025 | filename_to_str(&ssh->cfg.keyfile)); |
7026 | keytype = key_type(&ssh->cfg.keyfile); |
7027 | if (keytype == SSH_KEYTYPE_SSH2) { |
7028 | const char *error; |
7029 | s->publickey_blob = |
7030 | ssh2_userkey_loadpub(&ssh->cfg.keyfile, |
7031 | &s->publickey_algorithm, |
7032 | &s->publickey_bloblen, |
7033 | &s->publickey_comment, &error); |
7034 | if (s->publickey_blob) { |
7035 | s->publickey_encrypted = |
7036 | ssh2_userkey_encrypted(&ssh->cfg.keyfile, NULL); |
7037 | } else { |
7038 | char *msgbuf; |
7039 | logeventf(ssh, "Unable to load private key (%s)", |
7040 | error); |
7041 | msgbuf = dupprintf("Unable to load private key file " |
7042 | "\"%.150s\" (%s)\r\n", |
7043 | filename_to_str(&ssh->cfg.keyfile), |
7044 | error); |
7045 | c_write_str(ssh, msgbuf); |
7046 | sfree(msgbuf); |
7047 | } |
7048 | } else { |
7049 | char *msgbuf; |
7050 | logeventf(ssh, "Unable to use this key file (%s)", |
7051 | key_type_to_str(keytype)); |
7052 | msgbuf = dupprintf("Unable to use key file \"%.150s\"" |
7053 | " (%s)\r\n", |
7054 | filename_to_str(&ssh->cfg.keyfile), |
7055 | key_type_to_str(keytype)); |
7056 | c_write_str(ssh, msgbuf); |
7057 | sfree(msgbuf); |
7058 | s->publickey_blob = NULL; |
7059 | } |
7060 | } |
7061 | |
94cd7c3a |
7062 | /* |
7063 | * Find out about any keys Pageant has (but if there's a |
7064 | * public key configured, filter out all others). |
7065 | */ |
7066 | s->nkeys = 0; |
7067 | s->agent_response = NULL; |
7068 | s->pkblob_in_agent = NULL; |
116f229d |
7069 | if (ssh->cfg.tryagent && agent_exists()) { |
94cd7c3a |
7070 | |
7071 | void *r; |
7072 | |
7073 | logevent("Pageant is running. Requesting keys."); |
7074 | |
7075 | /* Request the keys held by the agent. */ |
7076 | PUT_32BIT(s->agent_request, 1); |
7077 | s->agent_request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
7078 | if (!agent_query(s->agent_request, 5, &r, &s->agent_responselen, |
7079 | ssh_agent_callback, ssh)) { |
7080 | do { |
7081 | crReturnV; |
7082 | if (pktin) { |
7083 | bombout(("Unexpected data from server while" |
7084 | " waiting for agent response")); |
7085 | crStopV; |
7086 | } |
7087 | } while (pktin || inlen > 0); |
7088 | r = ssh->agent_response; |
7089 | s->agent_responselen = ssh->agent_response_len; |
7090 | } |
7091 | s->agent_response = (unsigned char *) r; |
7092 | if (s->agent_response && s->agent_responselen >= 5 && |
7093 | s->agent_response[4] == SSH2_AGENT_IDENTITIES_ANSWER) { |
7094 | int keyi; |
7095 | unsigned char *p; |
7096 | p = s->agent_response + 5; |
7097 | s->nkeys = GET_32BIT(p); |
7098 | p += 4; |
7099 | logeventf(ssh, "Pageant has %d SSH-2 keys", s->nkeys); |
7100 | if (s->publickey_blob) { |
7101 | /* See if configured key is in agent. */ |
7102 | for (keyi = 0; keyi < s->nkeys; keyi++) { |
7103 | s->pklen = GET_32BIT(p); |
7104 | if (s->pklen == s->publickey_bloblen && |
7105 | !memcmp(p+4, s->publickey_blob, |
7106 | s->publickey_bloblen)) { |
7107 | logeventf(ssh, "Pageant key #%d matches " |
7108 | "configured key file", keyi); |
7109 | s->keyi = keyi; |
7110 | s->pkblob_in_agent = p; |
7111 | break; |
7112 | } |
7113 | p += 4 + s->pklen; |
7114 | p += GET_32BIT(p) + 4; /* comment */ |
7115 | } |
7116 | if (!s->pkblob_in_agent) { |
7117 | logevent("Configured key file not in Pageant"); |
7118 | s->nkeys = 0; |
7119 | } |
7120 | } |
7121 | } |
7122 | } |
7123 | |
edd0cb8a |
7124 | } |
7125 | |
7126 | /* |
1408a877 |
7127 | * We repeat this whole loop, including the username prompt, |
7128 | * until we manage a successful authentication. If the user |
51470298 |
7129 | * types the wrong _password_, they can be sent back to the |
7130 | * beginning to try another username, if this is configured on. |
7131 | * (If they specify a username in the config, they are never |
7132 | * asked, even if they do give a wrong password.) |
1408a877 |
7133 | * |
7134 | * I think this best serves the needs of |
7135 | * |
7136 | * - the people who have no configuration, no keys, and just |
7137 | * want to try repeated (username,password) pairs until they |
7138 | * type both correctly |
7139 | * |
7140 | * - people who have keys and configuration but occasionally |
7141 | * need to fall back to passwords |
7142 | * |
7143 | * - people with a key held in Pageant, who might not have |
7144 | * logged in to a particular machine before; so they want to |
7145 | * type a username, and then _either_ their key will be |
7146 | * accepted, _or_ they will type a password. If they mistype |
7147 | * the username they will want to be able to get back and |
7148 | * retype it! |
7cca0d81 |
7149 | */ |
51470298 |
7150 | s->username[0] = '\0'; |
7151 | s->got_username = FALSE; |
a1a1fae4 |
7152 | while (!s->we_are_in) { |
1408a877 |
7153 | /* |
7154 | * Get a username. |
7155 | */ |
86916870 |
7156 | if (s->got_username && !ssh->cfg.change_username) { |
5bb641e1 |
7157 | /* |
7158 | * We got a username last time round this loop, and |
7159 | * with change_username turned off we don't try to get |
7160 | * it again. |
7161 | */ |
aa09f7d0 |
7162 | } else if (!*ssh->cfg.username) { |
edd0cb8a |
7163 | int ret; /* need not be kept over crReturn */ |
7164 | s->cur_prompt = new_prompts(ssh->frontend); |
7165 | s->cur_prompt->to_server = TRUE; |
7166 | s->cur_prompt->name = dupstr("SSH login name"); |
7167 | add_prompt(s->cur_prompt, dupstr("login as: "), TRUE, |
7168 | lenof(s->username)); |
7169 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
7170 | while (ret < 0) { |
51470298 |
7171 | ssh->send_ok = 1; |
edd0cb8a |
7172 | crWaitUntilV(!pktin); |
7173 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
7174 | ssh->send_ok = 0; |
32874aea |
7175 | } |
edd0cb8a |
7176 | if (!ret) { |
7177 | /* |
7178 | * get_userpass_input() failed to get a username. |
7179 | * Terminate. |
7180 | */ |
7181 | free_prompts(s->cur_prompt); |
7182 | ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE); |
7183 | crStopV; |
7184 | } |
7185 | memcpy(s->username, s->cur_prompt->prompts[0]->result, |
7186 | lenof(s->username)); |
7187 | free_prompts(s->cur_prompt); |
7cca0d81 |
7188 | } else { |
57356d63 |
7189 | char *stuff; |
86916870 |
7190 | strncpy(s->username, ssh->cfg.username, sizeof(s->username)); |
51470298 |
7191 | s->username[sizeof(s->username)-1] = '\0'; |
65a22376 |
7192 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
57356d63 |
7193 | stuff = dupprintf("Using username \"%s\".\r\n", s->username); |
51470298 |
7194 | c_write_str(ssh, stuff); |
57356d63 |
7195 | sfree(stuff); |
7cca0d81 |
7196 | } |
7197 | } |
51470298 |
7198 | s->got_username = TRUE; |
7cca0d81 |
7199 | |
65a22376 |
7200 | /* |
1408a877 |
7201 | * Send an authentication request using method "none": (a) |
7202 | * just in case it succeeds, and (b) so that we know what |
7203 | * authentication methods we can usefully try next. |
65a22376 |
7204 | */ |
acab36bc |
7205 | ssh->pkt_actx = SSH2_PKTCTX_NOAUTH; |
51470298 |
7206 | |
ff3187f6 |
7207 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
7208 | ssh2_pkt_addstring(s->pktout, s->username); |
7209 | ssh2_pkt_addstring(s->pktout, "ssh-connection");/* service requested */ |
7210 | ssh2_pkt_addstring(s->pktout, "none"); /* method */ |
7211 | ssh2_pkt_send(ssh, s->pktout); |
51470298 |
7212 | s->type = AUTH_TYPE_NONE; |
7213 | s->gotit = FALSE; |
7214 | s->we_are_in = FALSE; |
7215 | |
7216 | s->tried_pubkey_config = FALSE; |
6ac3a551 |
7217 | s->kbd_inter_refused = FALSE; |
65a22376 |
7218 | |
94cd7c3a |
7219 | /* Reset agent request state. */ |
7220 | s->done_agent = FALSE; |
7221 | if (s->agent_response) { |
7222 | if (s->pkblob_in_agent) { |
7223 | s->agentp = s->pkblob_in_agent; |
7224 | } else { |
7225 | s->agentp = s->agent_response + 5 + 4; |
7226 | s->keyi = 0; |
7227 | } |
7228 | } |
7229 | |
1408a877 |
7230 | while (1) { |
7231 | /* |
7232 | * Wait for the result of the last authentication request. |
7233 | */ |
51470298 |
7234 | if (!s->gotit) |
ff3187f6 |
7235 | crWaitUntilV(pktin); |
6bbce591 |
7236 | /* |
7237 | * Now is a convenient point to spew any banner material |
7238 | * that we've accumulated. (This should ensure that when |
7239 | * we exit the auth loop, we haven't any left to deal |
7240 | * with.) |
7241 | */ |
7242 | { |
7243 | int size = bufchain_size(&ssh->banner); |
32874aea |
7244 | /* |
7245 | * Don't show the banner if we're operating in |
7246 | * non-verbose non-interactive mode. (It's probably |
7247 | * a script, which means nobody will read the |
7248 | * banner _anyway_, and moreover the printing of |
7249 | * the banner will screw up processing on the |
7250 | * output of (say) plink.) |
7251 | */ |
6bbce591 |
7252 | if (size && (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE))) { |
7253 | char *banner = snewn(size, char); |
7254 | bufchain_fetch(&ssh->banner, banner, size); |
7255 | c_write_untrusted(ssh, banner, size); |
7256 | sfree(banner); |
32874aea |
7257 | } |
6bbce591 |
7258 | bufchain_clear(&ssh->banner); |
1408a877 |
7259 | } |
ff3187f6 |
7260 | if (pktin->type == SSH2_MSG_USERAUTH_SUCCESS) { |
1408a877 |
7261 | logevent("Access granted"); |
51470298 |
7262 | s->we_are_in = TRUE; |
1408a877 |
7263 | break; |
7264 | } |
65a22376 |
7265 | |
e955d348 |
7266 | if (pktin->type != SSH2_MSG_USERAUTH_FAILURE) { |
7267 | bombout(("Strange packet received during authentication: " |
7268 | "type %d", pktin->type)); |
7ffdbc1a |
7269 | crStopV; |
65a22376 |
7270 | } |
7271 | |
51470298 |
7272 | s->gotit = FALSE; |
65a22376 |
7273 | |
1408a877 |
7274 | /* |
7275 | * OK, we're now sitting on a USERAUTH_FAILURE message, so |
7276 | * we can look at the string in it and know what we can |
7277 | * helpfully try next. |
7278 | */ |
ff3187f6 |
7279 | if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) { |
1408a877 |
7280 | char *methods; |
7281 | int methlen; |
ff3187f6 |
7282 | ssh_pkt_getstring(pktin, &methods, &methlen); |
ff3187f6 |
7283 | if (!ssh2_pkt_getbool(pktin)) { |
1408a877 |
7284 | /* |
7285 | * We have received an unequivocal Access |
7286 | * Denied. This can translate to a variety of |
7287 | * messages: |
7288 | * |
7289 | * - if we'd just tried "none" authentication, |
7290 | * it's not worth printing anything at all |
7291 | * |
7292 | * - if we'd just tried a public key _offer_, |
7293 | * the message should be "Server refused our |
7294 | * key" (or no message at all if the key |
7295 | * came from Pageant) |
7296 | * |
7297 | * - if we'd just tried anything else, the |
7298 | * message really should be "Access denied". |
7299 | * |
7300 | * Additionally, if we'd just tried password |
7301 | * authentication, we should break out of this |
7302 | * whole loop so as to go back to the username |
91f57d1f |
7303 | * prompt (iff we're configured to allow |
7304 | * username change attempts). |
1408a877 |
7305 | */ |
51470298 |
7306 | if (s->type == AUTH_TYPE_NONE) { |
1408a877 |
7307 | /* do nothing */ |
51470298 |
7308 | } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD || |
7309 | s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) { |
7310 | if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD) |
7311 | c_write_str(ssh, "Server refused our key\r\n"); |
1408a877 |
7312 | logevent("Server refused public key"); |
51470298 |
7313 | } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) { |
4bdf7c46 |
7314 | /* server declined keyboard-interactive; ignore */ |
1408a877 |
7315 | } else { |
51470298 |
7316 | c_write_str(ssh, "Access denied\r\n"); |
1408a877 |
7317 | logevent("Access denied"); |
91f57d1f |
7318 | if (s->type == AUTH_TYPE_PASSWORD && |
7319 | ssh->cfg.change_username) { |
6c9dce7c |
7320 | /* XXX perhaps we should allow |
7321 | * keyboard-interactive to do this too? */ |
51470298 |
7322 | s->we_are_in = FALSE; |
1408a877 |
7323 | break; |
7324 | } |
7325 | } |
7326 | } else { |
51470298 |
7327 | c_write_str(ssh, "Further authentication required\r\n"); |
1408a877 |
7328 | logevent("Further authentication required"); |
7329 | } |
65a22376 |
7330 | |
51470298 |
7331 | s->can_pubkey = |
32874aea |
7332 | in_commasep_string("publickey", methods, methlen); |
51470298 |
7333 | s->can_passwd = |
32874aea |
7334 | in_commasep_string("password", methods, methlen); |
86916870 |
7335 | s->can_keyb_inter = ssh->cfg.try_ki_auth && |
761187b6 |
7336 | in_commasep_string("keyboard-interactive", methods, methlen); |
1408a877 |
7337 | } |
65a22376 |
7338 | |
acab36bc |
7339 | ssh->pkt_actx = SSH2_PKTCTX_NOAUTH; |
65a22376 |
7340 | |
94cd7c3a |
7341 | if (s->can_pubkey && !s->done_agent && s->nkeys) { |
0405e71f |
7342 | |
1983e559 |
7343 | /* |
94cd7c3a |
7344 | * Attempt public-key authentication using a key from Pageant. |
1983e559 |
7345 | */ |
1983e559 |
7346 | |
acab36bc |
7347 | ssh->pkt_actx = SSH2_PKTCTX_PUBLICKEY; |
00db133f |
7348 | |
94cd7c3a |
7349 | logeventf(ssh, "Trying Pageant key #%d", s->keyi); |
7350 | |
7351 | /* Unpack key from agent response */ |
7352 | s->pklen = GET_32BIT(s->agentp); |
7353 | s->agentp += 4; |
7354 | s->pkblob = (char *)s->agentp; |
7355 | s->agentp += s->pklen; |
7356 | s->alglen = GET_32BIT(s->pkblob); |
7357 | s->alg = s->pkblob + 4; |
7358 | s->commentlen = GET_32BIT(s->agentp); |
7359 | s->agentp += 4; |
7360 | s->commentp = (char *)s->agentp; |
7361 | s->agentp += s->commentlen; |
7362 | /* s->agentp now points at next key, if any */ |
7363 | |
7364 | /* See if server will accept it */ |
7365 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
7366 | ssh2_pkt_addstring(s->pktout, s->username); |
7367 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
7368 | /* service requested */ |
7369 | ssh2_pkt_addstring(s->pktout, "publickey"); |
7370 | /* method */ |
7371 | ssh2_pkt_addbool(s->pktout, FALSE); /* no signature included */ |
7372 | ssh2_pkt_addstring_start(s->pktout); |
7373 | ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen); |
7374 | ssh2_pkt_addstring_start(s->pktout); |
7375 | ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen); |
7376 | ssh2_pkt_send(ssh, s->pktout); |
7377 | s->type = AUTH_TYPE_PUBLICKEY_OFFER_QUIET; |
1983e559 |
7378 | |
94cd7c3a |
7379 | crWaitUntilV(pktin); |
7380 | if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) { |
1983e559 |
7381 | |
94cd7c3a |
7382 | /* Offer of key refused. */ |
7383 | s->gotit = TRUE; |
1983e559 |
7384 | |
94cd7c3a |
7385 | } else { |
7386 | |
7387 | void *vret; |
1983e559 |
7388 | |
94cd7c3a |
7389 | if (flags & FLAG_VERBOSE) { |
7390 | c_write_str(ssh, "Authenticating with " |
7391 | "public key \""); |
7392 | c_write(ssh, s->commentp, s->commentlen); |
7393 | c_write_str(ssh, "\" from agent\r\n"); |
7394 | } |
1983e559 |
7395 | |
94cd7c3a |
7396 | /* |
7397 | * Server is willing to accept the key. |
7398 | * Construct a SIGN_REQUEST. |
7399 | */ |
7400 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
7401 | ssh2_pkt_addstring(s->pktout, s->username); |
7402 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
7403 | /* service requested */ |
7404 | ssh2_pkt_addstring(s->pktout, "publickey"); |
7405 | /* method */ |
7406 | ssh2_pkt_addbool(s->pktout, TRUE); /* signature included */ |
7407 | ssh2_pkt_addstring_start(s->pktout); |
7408 | ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen); |
7409 | ssh2_pkt_addstring_start(s->pktout); |
7410 | ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen); |
7411 | |
7412 | /* Ask agent for signature. */ |
7413 | s->siglen = s->pktout->length - 5 + 4 + |
7414 | ssh->v2_session_id_len; |
7415 | if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID) |
7416 | s->siglen -= 4; |
7417 | s->len = 1; /* message type */ |
7418 | s->len += 4 + s->pklen; /* key blob */ |
7419 | s->len += 4 + s->siglen; /* data to sign */ |
7420 | s->len += 4; /* flags */ |
7421 | s->agentreq = snewn(4 + s->len, char); |
7422 | PUT_32BIT(s->agentreq, s->len); |
7423 | s->q = s->agentreq + 4; |
7424 | *s->q++ = SSH2_AGENTC_SIGN_REQUEST; |
7425 | PUT_32BIT(s->q, s->pklen); |
7426 | s->q += 4; |
7427 | memcpy(s->q, s->pkblob, s->pklen); |
7428 | s->q += s->pklen; |
7429 | PUT_32BIT(s->q, s->siglen); |
7430 | s->q += 4; |
7431 | /* Now the data to be signed... */ |
7432 | if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) { |
7433 | PUT_32BIT(s->q, ssh->v2_session_id_len); |
51470298 |
7434 | s->q += 4; |
94cd7c3a |
7435 | } |
7436 | memcpy(s->q, ssh->v2_session_id, |
7437 | ssh->v2_session_id_len); |
7438 | s->q += ssh->v2_session_id_len; |
7439 | memcpy(s->q, s->pktout->data + 5, |
7440 | s->pktout->length - 5); |
7441 | s->q += s->pktout->length - 5; |
7442 | /* And finally the (zero) flags word. */ |
7443 | PUT_32BIT(s->q, 0); |
7444 | if (!agent_query(s->agentreq, s->len + 4, |
7445 | &vret, &s->retlen, |
7446 | ssh_agent_callback, ssh)) { |
7447 | do { |
7448 | crReturnV; |
7449 | if (pktin) { |
7450 | bombout(("Unexpected data from server" |
7451 | " while waiting for agent" |
7452 | " response")); |
7453 | crStopV; |
1983e559 |
7454 | } |
94cd7c3a |
7455 | } while (pktin || inlen > 0); |
7456 | vret = ssh->agent_response; |
7457 | s->retlen = ssh->agent_response_len; |
7458 | } |
7459 | s->ret = vret; |
7460 | sfree(s->agentreq); |
7461 | if (s->ret) { |
7462 | if (s->ret[4] == SSH2_AGENT_SIGN_RESPONSE) { |
7463 | logevent("Sending Pageant's response"); |
7464 | ssh2_add_sigblob(ssh, s->pktout, |
7465 | s->pkblob, s->pklen, |
7466 | s->ret + 9, |
7467 | GET_32BIT(s->ret + 5)); |
7468 | ssh2_pkt_send(ssh, s->pktout); |
7469 | s->type = AUTH_TYPE_PUBLICKEY; |
7470 | } else { |
7471 | /* FIXME: less drastic response */ |
7472 | bombout(("Pageant failed to answer challenge")); |
7473 | crStopV; |
1983e559 |
7474 | } |
7475 | } |
1983e559 |
7476 | } |
94cd7c3a |
7477 | |
7478 | /* Do we have any keys left to try? */ |
7479 | if (s->pkblob_in_agent) { |
7480 | s->done_agent = TRUE; |
7481 | s->tried_pubkey_config = TRUE; |
7482 | } else { |
7483 | s->keyi++; |
7484 | if (s->keyi >= s->nkeys) |
7485 | s->done_agent = TRUE; |
7486 | } |
1983e559 |
7487 | |
edd0cb8a |
7488 | } else if (s->can_pubkey && s->publickey_blob && |
7489 | !s->tried_pubkey_config) { |
65a22376 |
7490 | |
edd0cb8a |
7491 | struct ssh2_userkey *key; /* not live over crReturn */ |
7492 | char *passphrase; /* not live over crReturn */ |
65a22376 |
7493 | |
acab36bc |
7494 | ssh->pkt_actx = SSH2_PKTCTX_PUBLICKEY; |
00db133f |
7495 | |
edd0cb8a |
7496 | s->tried_pubkey_config = TRUE; |
7497 | |
65a22376 |
7498 | /* |
1408a877 |
7499 | * Try the public key supplied in the configuration. |
7500 | * |
7501 | * First, offer the public blob to see if the server is |
7502 | * willing to accept it. |
65a22376 |
7503 | */ |
ff3187f6 |
7504 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
7505 | ssh2_pkt_addstring(s->pktout, s->username); |
edd0cb8a |
7506 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
7507 | /* service requested */ |
7508 | ssh2_pkt_addstring(s->pktout, "publickey"); /* method */ |
7509 | ssh2_pkt_addbool(s->pktout, FALSE); |
7510 | /* no signature included */ |
7511 | ssh2_pkt_addstring(s->pktout, s->publickey_algorithm); |
7512 | ssh2_pkt_addstring_start(s->pktout); |
7513 | ssh2_pkt_addstring_data(s->pktout, |
7514 | (char *)s->publickey_blob, |
7515 | s->publickey_bloblen); |
ff3187f6 |
7516 | ssh2_pkt_send(ssh, s->pktout); |
edd0cb8a |
7517 | logevent("Offered public key"); |
ff3187f6 |
7518 | |
7519 | crWaitUntilV(pktin); |
edd0cb8a |
7520 | if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) { |
7521 | /* Key refused. Give up. */ |
7522 | s->gotit = TRUE; /* reconsider message next loop */ |
7523 | s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD; |
7524 | continue; /* process this new message */ |
45068b27 |
7525 | } |
edd0cb8a |
7526 | logevent("Offer of public key accepted"); |
af659722 |
7527 | |
45068b27 |
7528 | /* |
edd0cb8a |
7529 | * Actually attempt a serious authentication using |
7530 | * the key. |
45068b27 |
7531 | */ |
edd0cb8a |
7532 | if (flags & FLAG_VERBOSE) { |
7533 | c_write_str(ssh, "Authenticating with public key \""); |
7534 | c_write_str(ssh, s->publickey_comment); |
7535 | c_write_str(ssh, "\"\r\n"); |
7536 | } |
7537 | key = NULL; |
7538 | while (!key) { |
7539 | const char *error; /* not live over crReturn */ |
7540 | if (s->publickey_encrypted) { |
7541 | /* |
7542 | * Get a passphrase from the user. |
7543 | */ |
7544 | int ret; /* need not be kept over crReturn */ |
7545 | s->cur_prompt = new_prompts(ssh->frontend); |
7546 | s->cur_prompt->to_server = FALSE; |
7547 | s->cur_prompt->name = dupstr("SSH key passphrase"); |
7548 | add_prompt(s->cur_prompt, |
7549 | dupprintf("Passphrase for key \"%.100s\": ", |
7550 | s->publickey_comment), |
7551 | FALSE, SSH_MAX_PASSWORD_LEN); |
7552 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
7553 | while (ret < 0) { |
7554 | ssh->send_ok = 1; |
7555 | crWaitUntilV(!pktin); |
7556 | ret = get_userpass_input(s->cur_prompt, |
7557 | in, inlen); |
7558 | ssh->send_ok = 0; |
7559 | } |
7560 | if (!ret) { |
7561 | /* Failed to get a passphrase. Terminate. */ |
7562 | free_prompts(s->cur_prompt); |
7563 | ssh_disconnect(ssh, NULL, |
7564 | "Unable to authenticate", |
7565 | SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER, |
7566 | TRUE); |
7567 | crStopV; |
85fdbe25 |
7568 | } |
edd0cb8a |
7569 | passphrase = |
7570 | dupstr(s->cur_prompt->prompts[0]->result); |
7571 | free_prompts(s->cur_prompt); |
45068b27 |
7572 | } else { |
edd0cb8a |
7573 | passphrase = NULL; /* no passphrase needed */ |
45068b27 |
7574 | } |
1408a877 |
7575 | |
edd0cb8a |
7576 | /* |
7577 | * Try decrypting the key. |
7578 | */ |
7579 | key = ssh2_load_userkey(&ssh->cfg.keyfile, passphrase, |
7580 | &error); |
7581 | if (passphrase) { |
7582 | /* burn the evidence */ |
7583 | memset(passphrase, 0, strlen(passphrase)); |
7584 | sfree(passphrase); |
7585 | } |
7586 | if (key == SSH2_WRONG_PASSPHRASE || key == NULL) { |
7587 | if (passphrase && |
7588 | (key == SSH2_WRONG_PASSPHRASE)) { |
7589 | c_write_str(ssh, "Wrong passphrase\r\n"); |
7590 | key = NULL; |
7591 | /* and loop again */ |
7592 | } else { |
7593 | c_write_str(ssh, "Unable to load private key ("); |
7594 | c_write_str(ssh, error); |
7595 | c_write_str(ssh, ")\r\n"); |
7596 | key = NULL; |
7597 | break; /* try something else */ |
7598 | } |
1408a877 |
7599 | } |
65a22376 |
7600 | } |
65a22376 |
7601 | |
edd0cb8a |
7602 | if (key) { |
1dd353b5 |
7603 | unsigned char *pkblob, *sigblob, *sigdata; |
7604 | int pkblob_len, sigblob_len, sigdata_len; |
edd0cb8a |
7605 | int p; |
65a22376 |
7606 | |
1408a877 |
7607 | /* |
7608 | * We have loaded the private key and the server |
7609 | * has announced that it's willing to accept it. |
7610 | * Hallelujah. Generate a signature and send it. |
7611 | */ |
ff3187f6 |
7612 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
7613 | ssh2_pkt_addstring(s->pktout, s->username); |
edd0cb8a |
7614 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
7615 | /* service requested */ |
7616 | ssh2_pkt_addstring(s->pktout, "publickey"); |
7617 | /* method */ |
ff3187f6 |
7618 | ssh2_pkt_addbool(s->pktout, TRUE); |
edd0cb8a |
7619 | /* signature follows */ |
ff3187f6 |
7620 | ssh2_pkt_addstring(s->pktout, key->alg->name); |
edd0cb8a |
7621 | pkblob = key->alg->public_blob(key->data, |
7622 | &pkblob_len); |
ff3187f6 |
7623 | ssh2_pkt_addstring_start(s->pktout); |
edd0cb8a |
7624 | ssh2_pkt_addstring_data(s->pktout, (char *)pkblob, |
7625 | pkblob_len); |
1408a877 |
7626 | |
7627 | /* |
7628 | * The data to be signed is: |
7629 | * |
7630 | * string session-id |
7631 | * |
7632 | * followed by everything so far placed in the |
7633 | * outgoing packet. |
7634 | */ |
b672f405 |
7635 | sigdata_len = s->pktout->length - 5 + 4 + |
7636 | ssh->v2_session_id_len; |
edd0cb8a |
7637 | if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID) |
7638 | sigdata_len -= 4; |
92d60585 |
7639 | sigdata = snewn(sigdata_len, unsigned char); |
edd0cb8a |
7640 | p = 0; |
7641 | if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) { |
7642 | PUT_32BIT(sigdata+p, ssh->v2_session_id_len); |
7643 | p += 4; |
7644 | } |
b672f405 |
7645 | memcpy(sigdata+p, ssh->v2_session_id, |
7646 | ssh->v2_session_id_len); |
7647 | p += ssh->v2_session_id_len; |
ff3187f6 |
7648 | memcpy(sigdata+p, s->pktout->data + 5, |
7649 | s->pktout->length - 5); |
edd0cb8a |
7650 | p += s->pktout->length - 5; |
7651 | assert(p == sigdata_len); |
d8baa528 |
7652 | sigblob = key->alg->sign(key->data, (char *)sigdata, |
1dd353b5 |
7653 | sigdata_len, &sigblob_len); |
ff3187f6 |
7654 | ssh2_add_sigblob(ssh, s->pktout, pkblob, pkblob_len, |
1dd353b5 |
7655 | sigblob, sigblob_len); |
7656 | sfree(pkblob); |
7657 | sfree(sigblob); |
1408a877 |
7658 | sfree(sigdata); |
7659 | |
ff3187f6 |
7660 | ssh2_pkt_send(ssh, s->pktout); |
51470298 |
7661 | s->type = AUTH_TYPE_PUBLICKEY; |
75374b2f |
7662 | key->alg->freekey(key->data); |
1408a877 |
7663 | } |
edd0cb8a |
7664 | |
7665 | } else if (s->can_keyb_inter && !s->kbd_inter_refused) { |
7666 | |
7667 | /* |
7668 | * Keyboard-interactive authentication. |
7669 | */ |
edd0cb8a |
7670 | |
edd0cb8a |
7671 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
7672 | |
acab36bc |
7673 | ssh->pkt_actx = SSH2_PKTCTX_KBDINTER; |
edd0cb8a |
7674 | |
7675 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
7676 | ssh2_pkt_addstring(s->pktout, s->username); |
7677 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
7678 | /* service requested */ |
7679 | ssh2_pkt_addstring(s->pktout, "keyboard-interactive"); |
7680 | /* method */ |
7681 | ssh2_pkt_addstring(s->pktout, ""); /* lang */ |
7682 | ssh2_pkt_addstring(s->pktout, ""); /* submethods */ |
7683 | ssh2_pkt_send(ssh, s->pktout); |
7684 | |
7685 | crWaitUntilV(pktin); |
7686 | if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) { |
7687 | /* Server is not willing to do keyboard-interactive |
b8e3173d |
7688 | * at all (or, bizarrely but legally, accepts the |
7689 | * user without actually issuing any prompts). |
7690 | * Give up on it entirely. */ |
edd0cb8a |
7691 | s->gotit = TRUE; |
7692 | if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) |
7693 | logevent("Keyboard-interactive authentication refused"); |
7694 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET; |
7695 | s->kbd_inter_refused = TRUE; /* don't try it again */ |
7696 | continue; |
7697 | } |
7698 | |
7699 | /* |
b8e3173d |
7700 | * Loop while the server continues to send INFO_REQUESTs. |
edd0cb8a |
7701 | */ |
b8e3173d |
7702 | while (pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) { |
edd0cb8a |
7703 | |
b8e3173d |
7704 | char *name, *inst, *lang; |
7705 | int name_len, inst_len, lang_len; |
7706 | int i; |
7707 | |
7708 | /* |
7709 | * We've got a fresh USERAUTH_INFO_REQUEST. |
7710 | * Get the preamble and start building a prompt. |
7711 | */ |
7712 | ssh_pkt_getstring(pktin, &name, &name_len); |
7713 | ssh_pkt_getstring(pktin, &inst, &inst_len); |
7714 | ssh_pkt_getstring(pktin, &lang, &lang_len); |
7715 | s->cur_prompt = new_prompts(ssh->frontend); |
7716 | s->cur_prompt->to_server = TRUE; |
7717 | if (name_len) { |
7718 | /* FIXME: better prefix to distinguish from |
7719 | * local prompts? */ |
7720 | s->cur_prompt->name = |
7721 | dupprintf("SSH server: %.*s", name_len, name); |
7722 | s->cur_prompt->name_reqd = TRUE; |
7723 | } else { |
7724 | s->cur_prompt->name = |
7725 | dupstr("SSH server authentication"); |
7726 | s->cur_prompt->name_reqd = FALSE; |
edd0cb8a |
7727 | } |
b8e3173d |
7728 | /* FIXME: ugly to print "Using..." in prompt _every_ |
7729 | * time round. Can this be done more subtly? */ |
7730 | s->cur_prompt->instruction = |
7731 | dupprintf("Using keyboard-interactive authentication.%s%.*s", |
7732 | inst_len ? "\n" : "", inst_len, inst); |
7733 | s->cur_prompt->instr_reqd = TRUE; |
edd0cb8a |
7734 | |
b8e3173d |
7735 | /* |
7736 | * Get the prompts from the packet. |
7737 | */ |
7738 | s->num_prompts = ssh_pkt_getuint32(pktin); |
7739 | for (i = 0; i < s->num_prompts; i++) { |
7740 | char *prompt; |
7741 | int prompt_len; |
7742 | int echo; |
7743 | static char noprompt[] = |
7744 | "<server failed to send prompt>: "; |
7745 | |
7746 | ssh_pkt_getstring(pktin, &prompt, &prompt_len); |
7747 | echo = ssh2_pkt_getbool(pktin); |
7748 | if (!prompt_len) { |
7749 | prompt = noprompt; |
7750 | prompt_len = lenof(noprompt)-1; |
7751 | } |
7752 | add_prompt(s->cur_prompt, |
7753 | dupprintf("%.*s", prompt_len, prompt), |
7754 | echo, SSH_MAX_PASSWORD_LEN); |
edd0cb8a |
7755 | } |
b8e3173d |
7756 | |
7757 | /* |
7758 | * Get the user's responses. |
7759 | */ |
7760 | if (s->num_prompts) { |
7761 | int ret; /* not live over crReturn */ |
7762 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
7763 | while (ret < 0) { |
7764 | ssh->send_ok = 1; |
7765 | crWaitUntilV(!pktin); |
7766 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
7767 | ssh->send_ok = 0; |
7768 | } |
7769 | if (!ret) { |
7770 | /* |
7771 | * Failed to get responses. Terminate. |
7772 | */ |
7773 | free_prompts(s->cur_prompt); |
7774 | ssh_disconnect(ssh, NULL, "Unable to authenticate", |
7775 | SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER, |
7776 | TRUE); |
7777 | crStopV; |
7778 | } |
edd0cb8a |
7779 | } |
b8e3173d |
7780 | |
7781 | /* |
7782 | * Send the responses to the server. |
7783 | */ |
7784 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
b8e3173d |
7785 | ssh2_pkt_adduint32(s->pktout, s->num_prompts); |
7786 | for (i=0; i < s->num_prompts; i++) { |
7787 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
7788 | ssh2_pkt_addstring(s->pktout, |
7789 | s->cur_prompt->prompts[i]->result); |
7790 | end_log_omission(ssh, s->pktout); |
7791 | } |
18524056 |
7792 | ssh2_pkt_send_with_padding(ssh, s->pktout, 256); |
b8e3173d |
7793 | |
7794 | /* |
7795 | * Get the next packet in case it's another |
7796 | * INFO_REQUEST. |
7797 | */ |
7798 | crWaitUntilV(pktin); |
7799 | |
edd0cb8a |
7800 | } |
7801 | |
7802 | /* |
b8e3173d |
7803 | * We should have SUCCESS or FAILURE now. |
edd0cb8a |
7804 | */ |
b8e3173d |
7805 | s->gotit = TRUE; |
edd0cb8a |
7806 | |
7807 | } else if (s->can_passwd) { |
7808 | |
7809 | /* |
7810 | * Plain old password authentication. |
7811 | */ |
7812 | int ret; /* not live over crReturn */ |
e955d348 |
7813 | int changereq_first_time; /* not live over crReturn */ |
edd0cb8a |
7814 | |
acab36bc |
7815 | ssh->pkt_actx = SSH2_PKTCTX_PASSWORD; |
edd0cb8a |
7816 | |
7817 | s->cur_prompt = new_prompts(ssh->frontend); |
7818 | s->cur_prompt->to_server = TRUE; |
7819 | s->cur_prompt->name = dupstr("SSH password"); |
7820 | add_prompt(s->cur_prompt, dupprintf("%.90s@%.90s's password: ", |
7821 | s->username, |
7822 | ssh->savedhost), |
7823 | FALSE, SSH_MAX_PASSWORD_LEN); |
7824 | |
7825 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
7826 | while (ret < 0) { |
7827 | ssh->send_ok = 1; |
7828 | crWaitUntilV(!pktin); |
7829 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
7830 | ssh->send_ok = 0; |
7831 | } |
7832 | if (!ret) { |
7833 | /* |
7834 | * Failed to get responses. Terminate. |
7835 | */ |
7836 | free_prompts(s->cur_prompt); |
7837 | ssh_disconnect(ssh, NULL, "Unable to authenticate", |
7838 | SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER, |
7839 | TRUE); |
7840 | crStopV; |
7841 | } |
e955d348 |
7842 | /* |
7843 | * Squirrel away the password. (We may need it later if |
7844 | * asked to change it.) |
7845 | */ |
7846 | s->password = dupstr(s->cur_prompt->prompts[0]->result); |
7847 | free_prompts(s->cur_prompt); |
edd0cb8a |
7848 | |
7849 | /* |
7850 | * Send the password packet. |
7851 | * |
95d2d262 |
7852 | * We pad out the password packet to 256 bytes to make |
7853 | * it harder for an attacker to find the length of the |
7854 | * user's password. |
1408a877 |
7855 | * |
95d2d262 |
7856 | * Anyone using a password longer than 256 bytes |
7857 | * probably doesn't have much to worry about from |
1408a877 |
7858 | * people who find out how long their password is! |
65a22376 |
7859 | */ |
ff3187f6 |
7860 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
7861 | ssh2_pkt_addstring(s->pktout, s->username); |
edd0cb8a |
7862 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
7863 | /* service requested */ |
ff3187f6 |
7864 | ssh2_pkt_addstring(s->pktout, "password"); |
7865 | ssh2_pkt_addbool(s->pktout, FALSE); |
7866 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
e955d348 |
7867 | ssh2_pkt_addstring(s->pktout, s->password); |
ff3187f6 |
7868 | end_log_omission(ssh, s->pktout); |
18524056 |
7869 | ssh2_pkt_send_with_padding(ssh, s->pktout, 256); |
0d43337a |
7870 | logevent("Sent password"); |
28f4d4f0 |
7871 | s->type = AUTH_TYPE_PASSWORD; |
edd0cb8a |
7872 | |
e955d348 |
7873 | /* |
7874 | * Wait for next packet, in case it's a password change |
7875 | * request. |
7876 | */ |
7877 | crWaitUntilV(pktin); |
7878 | changereq_first_time = TRUE; |
7879 | |
7880 | while (pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) { |
7881 | |
7882 | /* |
7883 | * We're being asked for a new password |
7884 | * (perhaps not for the first time). |
7885 | * Loop until the server accepts it. |
7886 | */ |
7887 | |
7888 | int got_new = FALSE; /* not live over crReturn */ |
7889 | char *prompt; /* not live over crReturn */ |
7890 | int prompt_len; /* not live over crReturn */ |
7891 | |
7892 | { |
7893 | char *msg; |
7894 | if (changereq_first_time) |
7895 | msg = "Server requested password change"; |
7896 | else |
7897 | msg = "Server rejected new password"; |
7898 | logevent(msg); |
7899 | c_write_str(ssh, msg); |
7900 | c_write_str(ssh, "\r\n"); |
7901 | } |
7902 | |
7903 | ssh_pkt_getstring(pktin, &prompt, &prompt_len); |
7904 | |
7905 | s->cur_prompt = new_prompts(ssh->frontend); |
7906 | s->cur_prompt->to_server = TRUE; |
7907 | s->cur_prompt->name = dupstr("New SSH password"); |
7908 | s->cur_prompt->instruction = |
7909 | dupprintf("%.*s", prompt_len, prompt); |
7910 | s->cur_prompt->instr_reqd = TRUE; |
d9add7bc |
7911 | /* |
7912 | * There's no explicit requirement in the protocol |
7913 | * for the "old" passwords in the original and |
7914 | * password-change messages to be the same, and |
7915 | * apparently some Cisco kit supports password change |
7916 | * by the user entering a blank password originally |
7917 | * and the real password subsequently, so, |
7918 | * reluctantly, we prompt for the old password again. |
7919 | * |
7920 | * (On the other hand, some servers don't even bother |
7921 | * to check this field.) |
7922 | */ |
7923 | add_prompt(s->cur_prompt, |
7924 | dupstr("Current password (blank for previously entered password): "), |
7925 | FALSE, SSH_MAX_PASSWORD_LEN); |
e955d348 |
7926 | add_prompt(s->cur_prompt, dupstr("Enter new password: "), |
7927 | FALSE, SSH_MAX_PASSWORD_LEN); |
7928 | add_prompt(s->cur_prompt, dupstr("Confirm new password: "), |
7929 | FALSE, SSH_MAX_PASSWORD_LEN); |
7930 | |
7931 | /* |
7932 | * Loop until the user manages to enter the same |
7933 | * password twice. |
7934 | */ |
7935 | while (!got_new) { |
7936 | |
7937 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
7938 | while (ret < 0) { |
7939 | ssh->send_ok = 1; |
7940 | crWaitUntilV(!pktin); |
7941 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
7942 | ssh->send_ok = 0; |
7943 | } |
7944 | if (!ret) { |
7945 | /* |
7946 | * Failed to get responses. Terminate. |
7947 | */ |
7948 | /* burn the evidence */ |
7949 | free_prompts(s->cur_prompt); |
7950 | memset(s->password, 0, strlen(s->password)); |
7951 | sfree(s->password); |
7952 | ssh_disconnect(ssh, NULL, "Unable to authenticate", |
7953 | SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER, |
7954 | TRUE); |
7955 | crStopV; |
7956 | } |
7957 | |
7958 | /* |
d9add7bc |
7959 | * If the user specified a new original password |
7960 | * (IYSWIM), overwrite any previously specified |
7961 | * one. |
7962 | * (A side effect is that the user doesn't have to |
7963 | * re-enter it if they louse up the new password.) |
7964 | */ |
7965 | if (s->cur_prompt->prompts[0]->result[0]) { |
7966 | memset(s->password, 0, strlen(s->password)); |
7967 | /* burn the evidence */ |
7968 | sfree(s->password); |
7969 | s->password = |
7970 | dupstr(s->cur_prompt->prompts[0]->result); |
7971 | } |
7972 | |
7973 | /* |
7974 | * Check the two new passwords match. |
e955d348 |
7975 | */ |
d9add7bc |
7976 | got_new = (strcmp(s->cur_prompt->prompts[1]->result, |
7977 | s->cur_prompt->prompts[2]->result) |
e955d348 |
7978 | == 0); |
7979 | if (!got_new) |
7980 | /* They don't. Silly user. */ |
7981 | c_write_str(ssh, "Passwords do not match\r\n"); |
7982 | |
7983 | } |
7984 | |
7985 | /* |
7986 | * Send the new password (along with the old one). |
7987 | * (see above for padding rationale) |
7988 | */ |
7989 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
e955d348 |
7990 | ssh2_pkt_addstring(s->pktout, s->username); |
7991 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
7992 | /* service requested */ |
7993 | ssh2_pkt_addstring(s->pktout, "password"); |
7994 | ssh2_pkt_addbool(s->pktout, TRUE); |
7995 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
7996 | ssh2_pkt_addstring(s->pktout, s->password); |
7997 | ssh2_pkt_addstring(s->pktout, |
d9add7bc |
7998 | s->cur_prompt->prompts[1]->result); |
e955d348 |
7999 | free_prompts(s->cur_prompt); |
8000 | end_log_omission(ssh, s->pktout); |
18524056 |
8001 | ssh2_pkt_send_with_padding(ssh, s->pktout, 256); |
e955d348 |
8002 | logevent("Sent new password"); |
8003 | |
8004 | /* |
8005 | * Now see what the server has to say about it. |
8006 | * (If it's CHANGEREQ again, it's not happy with the |
8007 | * new password.) |
8008 | */ |
8009 | crWaitUntilV(pktin); |
8010 | changereq_first_time = FALSE; |
8011 | |
8012 | } |
8013 | |
8014 | /* |
8015 | * We need to reexamine the current pktin at the top |
8016 | * of the loop. Either: |
8017 | * - we weren't asked to change password at all, in |
8018 | * which case it's a SUCCESS or FAILURE with the |
8019 | * usual meaning |
8020 | * - we sent a new password, and the server was |
8021 | * either OK with it (SUCCESS or FAILURE w/partial |
8022 | * success) or unhappy with the _old_ password |
8023 | * (FAILURE w/o partial success) |
8024 | * In any of these cases, we go back to the top of |
8025 | * the loop and start again. |
8026 | */ |
8027 | s->gotit = TRUE; |
8028 | |
8029 | /* |
8030 | * We don't need the old password any more, in any |
8031 | * case. Burn the evidence. |
8032 | */ |
8033 | memset(s->password, 0, strlen(s->password)); |
8034 | sfree(s->password); |
8035 | |
1408a877 |
8036 | } else { |
edd0cb8a |
8037 | |
9e296bfa |
8038 | ssh_disconnect(ssh, NULL, |
8039 | "No supported authentication methods available", |
8040 | SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, |
8041 | FALSE); |
7ffdbc1a |
8042 | crStopV; |
edd0cb8a |
8043 | |
65a22376 |
8044 | } |
edd0cb8a |
8045 | |
65a22376 |
8046 | } |
a1a1fae4 |
8047 | } |
6bbce591 |
8048 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = NULL; |
7cca0d81 |
8049 | |
edd0cb8a |
8050 | /* Clear up various bits and pieces from authentication. */ |
8051 | if (s->publickey_blob) { |
8052 | sfree(s->publickey_blob); |
8053 | sfree(s->publickey_comment); |
8054 | } |
94cd7c3a |
8055 | if (s->agent_response) |
8056 | sfree(s->agent_response); |
edd0cb8a |
8057 | |
7cca0d81 |
8058 | /* |
a1a1fae4 |
8059 | * Now the connection protocol has started, one way or another. |
7cca0d81 |
8060 | */ |
8061 | |
0ed48730 |
8062 | ssh->channels = newtree234(ssh_channelcmp); |
8063 | |
7cca0d81 |
8064 | /* |
b09eaa88 |
8065 | * Set up handlers for some connection protocol messages, so we |
8066 | * don't have to handle them repeatedly in this coroutine. |
8067 | */ |
8068 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = |
8069 | ssh2_msg_channel_window_adjust; |
51df0ab5 |
8070 | ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = |
8071 | ssh2_msg_global_request; |
b09eaa88 |
8072 | |
8073 | /* |
0ed48730 |
8074 | * Create the main session channel. |
7cca0d81 |
8075 | */ |
feb02b4e |
8076 | if (ssh->cfg.ssh_no_shell) { |
8077 | ssh->mainchan = NULL; |
8078 | } else if (*ssh->cfg.ssh_nc_host) { |
8079 | /* |
8080 | * Just start a direct-tcpip channel and use it as the main |
8081 | * channel. |
8082 | */ |
8083 | ssh->mainchan = snew(struct ssh_channel); |
8084 | ssh->mainchan->ssh = ssh; |
8085 | ssh->mainchan->localid = alloc_channel_id(ssh); |
23828b7e |
8086 | logeventf(ssh, |
8087 | "Opening direct-tcpip channel to %s:%d in place of session", |
8088 | ssh->cfg.ssh_nc_host, ssh->cfg.ssh_nc_port); |
feb02b4e |
8089 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
8090 | ssh2_pkt_addstring(s->pktout, "direct-tcpip"); |
8091 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->localid); |
9b53e9b8 |
8092 | ssh->mainchan->v.v2.locwindow = ssh->mainchan->v.v2.locmaxwin = |
0e443c99 |
8093 | ssh->mainchan->v.v2.remlocwin = |
9b53e9b8 |
8094 | ssh->cfg.ssh_simple ? OUR_V2_BIGWIN : OUR_V2_WINSIZE; |
0e443c99 |
8095 | ssh->mainchan->v.v2.winadj_head = NULL; |
8096 | ssh->mainchan->v.v2.winadj_tail = NULL; |
8097 | ssh->mainchan->v.v2.throttle_state = UNTHROTTLED; |
feb02b4e |
8098 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->v.v2.locwindow);/* our window size */ |
8099 | ssh2_pkt_adduint32(s->pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
8100 | ssh2_pkt_addstring(s->pktout, ssh->cfg.ssh_nc_host); |
8101 | ssh2_pkt_adduint32(s->pktout, ssh->cfg.ssh_nc_port); |
8102 | /* |
23828b7e |
8103 | * There's nothing meaningful to put in the originator |
8104 | * fields, but some servers insist on syntactically correct |
8105 | * information. |
feb02b4e |
8106 | */ |
8107 | ssh2_pkt_addstring(s->pktout, "0.0.0.0"); |
8108 | ssh2_pkt_adduint32(s->pktout, 0); |
8109 | ssh2_pkt_send(ssh, s->pktout); |
8110 | |
8111 | crWaitUntilV(pktin); |
8112 | if (pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
8113 | bombout(("Server refused to open a direct-tcpip channel")); |
8114 | crStopV; |
8115 | /* FIXME: error data comes back in FAILURE packet */ |
8116 | } |
8117 | if (ssh_pkt_getuint32(pktin) != ssh->mainchan->localid) { |
8118 | bombout(("Server's channel confirmation cited wrong channel")); |
8119 | crStopV; |
8120 | } |
8121 | ssh->mainchan->remoteid = ssh_pkt_getuint32(pktin); |
8122 | ssh->mainchan->halfopen = FALSE; |
8123 | ssh->mainchan->type = CHAN_MAINSESSION; |
8124 | ssh->mainchan->closes = 0; |
8125 | ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(pktin); |
8126 | ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin); |
8127 | bufchain_init(&ssh->mainchan->v.v2.outbuffer); |
8128 | add234(ssh->channels, ssh->mainchan); |
8129 | update_specials_menu(ssh->frontend); |
23828b7e |
8130 | logevent("Opened direct-tcpip channel"); |
feb02b4e |
8131 | ssh->ncmode = TRUE; |
8132 | } else { |
0ed48730 |
8133 | ssh->mainchan = snew(struct ssh_channel); |
8134 | ssh->mainchan->ssh = ssh; |
8135 | ssh->mainchan->localid = alloc_channel_id(ssh); |
ff3187f6 |
8136 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
8137 | ssh2_pkt_addstring(s->pktout, "session"); |
8138 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->localid); |
9b53e9b8 |
8139 | ssh->mainchan->v.v2.locwindow = ssh->mainchan->v.v2.locmaxwin = |
0e443c99 |
8140 | ssh->mainchan->v.v2.remlocwin = |
9b53e9b8 |
8141 | ssh->cfg.ssh_simple ? OUR_V2_BIGWIN : OUR_V2_WINSIZE; |
0e443c99 |
8142 | ssh->mainchan->v.v2.winadj_head = NULL; |
8143 | ssh->mainchan->v.v2.winadj_tail = NULL; |
8144 | ssh->mainchan->v.v2.throttle_state = UNTHROTTLED; |
ff3187f6 |
8145 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->v.v2.locwindow);/* our window size */ |
954d5c5a |
8146 | ssh2_pkt_adduint32(s->pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
ff3187f6 |
8147 | ssh2_pkt_send(ssh, s->pktout); |
8148 | crWaitUntilV(pktin); |
8149 | if (pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
0ed48730 |
8150 | bombout(("Server refused to open a session")); |
8151 | crStopV; |
8152 | /* FIXME: error data comes back in FAILURE packet */ |
8153 | } |
ff3187f6 |
8154 | if (ssh_pkt_getuint32(pktin) != ssh->mainchan->localid) { |
0ed48730 |
8155 | bombout(("Server's channel confirmation cited wrong channel")); |
8156 | crStopV; |
8157 | } |
ff3187f6 |
8158 | ssh->mainchan->remoteid = ssh_pkt_getuint32(pktin); |
64d6ff88 |
8159 | ssh->mainchan->halfopen = FALSE; |
0ed48730 |
8160 | ssh->mainchan->type = CHAN_MAINSESSION; |
8161 | ssh->mainchan->closes = 0; |
ff3187f6 |
8162 | ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(pktin); |
8163 | ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin); |
0ed48730 |
8164 | bufchain_init(&ssh->mainchan->v.v2.outbuffer); |
8165 | add234(ssh->channels, ssh->mainchan); |
62638676 |
8166 | update_specials_menu(ssh->frontend); |
0ed48730 |
8167 | logevent("Opened channel for session"); |
feb02b4e |
8168 | ssh->ncmode = FALSE; |
8169 | } |
7cca0d81 |
8170 | |
8171 | /* |
51df0ab5 |
8172 | * Now we have a channel, make dispatch table entries for |
8173 | * general channel-based messages. |
8174 | */ |
8175 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = |
8176 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = |
8177 | ssh2_msg_channel_data; |
8178 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_channel_eof; |
8179 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_channel_close; |
8180 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = |
8181 | ssh2_msg_channel_open_confirmation; |
8182 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = |
8183 | ssh2_msg_channel_open_failure; |
8184 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] = |
8185 | ssh2_msg_channel_request; |
8186 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = |
8187 | ssh2_msg_channel_open; |
8188 | |
3361b80a |
8189 | if (ssh->cfg.ssh_simple) { |
8190 | /* |
8191 | * This message indicates to the server that we promise |
8192 | * not to try to run any other channel in parallel with |
8193 | * this one, so it's safe for it to advertise a very large |
8194 | * window and leave the flow control to TCP. |
8195 | */ |
8196 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
8197 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); |
8198 | ssh2_pkt_addstring(s->pktout, "simple@putty.projects.tartarus.org"); |
8199 | ssh2_pkt_addbool(s->pktout, 0); /* no reply */ |
8200 | ssh2_pkt_send(ssh, s->pktout); |
8201 | } |
8202 | |
51df0ab5 |
8203 | /* |
783415f8 |
8204 | * Potentially enable X11 forwarding. |
8205 | */ |
feb02b4e |
8206 | if (ssh->mainchan && !ssh->ncmode && ssh->cfg.x11_forward) { |
32874aea |
8207 | char proto[20], data[64]; |
8208 | logevent("Requesting X11 forwarding"); |
302121de |
8209 | ssh->x11auth = x11_invent_auth(proto, sizeof(proto), |
86916870 |
8210 | data, sizeof(data), ssh->cfg.x11_auth); |
8211 | x11_get_real_auth(ssh->x11auth, ssh->cfg.x11_display); |
ff3187f6 |
8212 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
8213 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); |
8214 | ssh2_pkt_addstring(s->pktout, "x11-req"); |
8215 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
8216 | ssh2_pkt_addbool(s->pktout, 0); /* many connections */ |
8217 | ssh2_pkt_addstring(s->pktout, proto); |
f68353be |
8218 | /* |
8219 | * Note that while we blank the X authentication data here, we don't |
8220 | * take any special action to blank the start of an X11 channel, |
8221 | * so using MIT-MAGIC-COOKIE-1 and actually opening an X connection |
8222 | * without having session blanking enabled is likely to leak your |
8223 | * cookie into the log. |
8224 | */ |
178c3872 |
8225 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
ff3187f6 |
8226 | ssh2_pkt_addstring(s->pktout, data); |
178c3872 |
8227 | end_log_omission(ssh, s->pktout); |
ff3187f6 |
8228 | ssh2_pkt_adduint32(s->pktout, x11_get_screen_number(ssh->cfg.x11_display)); |
8229 | ssh2_pkt_send(ssh, s->pktout); |
32874aea |
8230 | |
b09eaa88 |
8231 | crWaitUntilV(pktin); |
32874aea |
8232 | |
ff3187f6 |
8233 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
8234 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
6b5cf8b4 |
8235 | bombout(("Unexpected response to X11 forwarding request:" |
ff3187f6 |
8236 | " packet type %d", pktin->type)); |
7ffdbc1a |
8237 | crStopV; |
32874aea |
8238 | } |
8239 | logevent("X11 forwarding refused"); |
8240 | } else { |
8241 | logevent("X11 forwarding enabled"); |
51470298 |
8242 | ssh->X11_fwd_enabled = TRUE; |
32874aea |
8243 | } |
783415f8 |
8244 | } |
8245 | |
8246 | /* |
bc240b21 |
8247 | * Enable port forwardings. |
8248 | */ |
06fadff5 |
8249 | ssh_setup_portfwd(ssh, &ssh->cfg); |
bc240b21 |
8250 | |
8251 | /* |
36c2a3e9 |
8252 | * Potentially enable agent forwarding. |
8253 | */ |
feb02b4e |
8254 | if (ssh->mainchan && !ssh->ncmode && ssh->cfg.agentfwd && agent_exists()) { |
32874aea |
8255 | logevent("Requesting OpenSSH-style agent forwarding"); |
ff3187f6 |
8256 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
8257 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); |
8258 | ssh2_pkt_addstring(s->pktout, "auth-agent-req@openssh.com"); |
8259 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
8260 | ssh2_pkt_send(ssh, s->pktout); |
32874aea |
8261 | |
b09eaa88 |
8262 | crWaitUntilV(pktin); |
32874aea |
8263 | |
ff3187f6 |
8264 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
8265 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
6b5cf8b4 |
8266 | bombout(("Unexpected response to agent forwarding request:" |
ff3187f6 |
8267 | " packet type %d", pktin->type)); |
7ffdbc1a |
8268 | crStopV; |
32874aea |
8269 | } |
8270 | logevent("Agent forwarding refused"); |
8271 | } else { |
8272 | logevent("Agent forwarding enabled"); |
51470298 |
8273 | ssh->agentfwd_enabled = TRUE; |
32874aea |
8274 | } |
36c2a3e9 |
8275 | } |
8276 | |
8277 | /* |
7cca0d81 |
8278 | * Now allocate a pty for the session. |
8279 | */ |
feb02b4e |
8280 | if (ssh->mainchan && !ssh->ncmode && !ssh->cfg.nopty) { |
a5dd8467 |
8281 | /* Unpick the terminal-speed string. */ |
8282 | /* XXX perhaps we should allow no speeds to be sent. */ |
db219738 |
8283 | ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */ |
8284 | sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed); |
a5dd8467 |
8285 | /* Build the pty request. */ |
ff3187f6 |
8286 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
8287 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); /* recipient channel */ |
8288 | ssh2_pkt_addstring(s->pktout, "pty-req"); |
8289 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
8290 | ssh2_pkt_addstring(s->pktout, ssh->cfg.termtype); |
8291 | ssh2_pkt_adduint32(s->pktout, ssh->term_width); |
8292 | ssh2_pkt_adduint32(s->pktout, ssh->term_height); |
8293 | ssh2_pkt_adduint32(s->pktout, 0); /* pixel width */ |
8294 | ssh2_pkt_adduint32(s->pktout, 0); /* pixel height */ |
8295 | ssh2_pkt_addstring_start(s->pktout); |
c6ccd5c2 |
8296 | parse_ttymodes(ssh, ssh->cfg.ttymodes, |
8297 | ssh2_send_ttymode, (void *)s->pktout); |
8298 | ssh2_pkt_addbyte(s->pktout, SSH2_TTY_OP_ISPEED); |
ff3187f6 |
8299 | ssh2_pkt_adduint32(s->pktout, ssh->ispeed); |
c6ccd5c2 |
8300 | ssh2_pkt_addbyte(s->pktout, SSH2_TTY_OP_OSPEED); |
ff3187f6 |
8301 | ssh2_pkt_adduint32(s->pktout, ssh->ospeed); |
8302 | ssh2_pkt_addstring_data(s->pktout, "\0", 1); /* TTY_OP_END */ |
8303 | ssh2_pkt_send(ssh, s->pktout); |
51470298 |
8304 | ssh->state = SSH_STATE_INTERMED; |
32874aea |
8305 | |
b09eaa88 |
8306 | crWaitUntilV(pktin); |
32874aea |
8307 | |
ff3187f6 |
8308 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
8309 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
6b5cf8b4 |
8310 | bombout(("Unexpected response to pty request:" |
ff3187f6 |
8311 | " packet type %d", pktin->type)); |
7ffdbc1a |
8312 | crStopV; |
32874aea |
8313 | } |
51470298 |
8314 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
8315 | ssh->editing = ssh->echoing = 1; |
32874aea |
8316 | } else { |
a5dd8467 |
8317 | logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)", |
db219738 |
8318 | ssh->ospeed, ssh->ispeed); |
32874aea |
8319 | } |
0965bee0 |
8320 | } else { |
51470298 |
8321 | ssh->editing = ssh->echoing = 1; |
7cca0d81 |
8322 | } |
8323 | |
8324 | /* |
73feed4f |
8325 | * Send environment variables. |
8326 | * |
8327 | * Simplest thing here is to send all the requests at once, and |
8328 | * then wait for a whole bunch of successes or failures. |
8329 | */ |
feb02b4e |
8330 | if (ssh->mainchan && !ssh->ncmode && *ssh->cfg.environmt) { |
73feed4f |
8331 | char *e = ssh->cfg.environmt; |
8332 | char *var, *varend, *val; |
8333 | |
8334 | s->num_env = 0; |
8335 | |
8336 | while (*e) { |
8337 | var = e; |
8338 | while (*e && *e != '\t') e++; |
8339 | varend = e; |
8340 | if (*e == '\t') e++; |
8341 | val = e; |
8342 | while (*e) e++; |
8343 | e++; |
8344 | |
ff3187f6 |
8345 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
8346 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); |
8347 | ssh2_pkt_addstring(s->pktout, "env"); |
8348 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
8349 | ssh2_pkt_addstring_start(s->pktout); |
8350 | ssh2_pkt_addstring_data(s->pktout, var, varend-var); |
8351 | ssh2_pkt_addstring(s->pktout, val); |
8352 | ssh2_pkt_send(ssh, s->pktout); |
73feed4f |
8353 | |
8354 | s->num_env++; |
8355 | } |
8356 | |
8357 | logeventf(ssh, "Sent %d environment variables", s->num_env); |
8358 | |
8359 | s->env_ok = 0; |
8360 | s->env_left = s->num_env; |
8361 | |
8362 | while (s->env_left > 0) { |
b09eaa88 |
8363 | crWaitUntilV(pktin); |
73feed4f |
8364 | |
ff3187f6 |
8365 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
8366 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
73feed4f |
8367 | bombout(("Unexpected response to environment request:" |
ff3187f6 |
8368 | " packet type %d", pktin->type)); |
73feed4f |
8369 | crStopV; |
8370 | } |
8371 | } else { |
8372 | s->env_ok++; |
8373 | } |
8374 | |
8375 | s->env_left--; |
8376 | } |
8377 | |
8378 | if (s->env_ok == s->num_env) { |
8379 | logevent("All environment variables successfully set"); |
8380 | } else if (s->env_ok == 0) { |
8381 | logevent("All environment variables refused"); |
8382 | c_write_str(ssh, "Server refused to set environment variables\r\n"); |
8383 | } else { |
8384 | logeventf(ssh, "%d environment variables refused", |
8385 | s->num_env - s->env_ok); |
8386 | c_write_str(ssh, "Server refused to set all environment variables\r\n"); |
8387 | } |
8388 | } |
8389 | |
8390 | /* |
fd5e5847 |
8391 | * Start a shell or a remote command. We may have to attempt |
8392 | * this twice if the config data has provided a second choice |
8393 | * of command. |
7cca0d81 |
8394 | */ |
feb02b4e |
8395 | if (ssh->mainchan && !ssh->ncmode) while (1) { |
fd5e5847 |
8396 | int subsys; |
8397 | char *cmd; |
8398 | |
51470298 |
8399 | if (ssh->fallback_cmd) { |
86916870 |
8400 | subsys = ssh->cfg.ssh_subsys2; |
8401 | cmd = ssh->cfg.remote_cmd_ptr2; |
fd5e5847 |
8402 | } else { |
86916870 |
8403 | subsys = ssh->cfg.ssh_subsys; |
8404 | cmd = ssh->cfg.remote_cmd_ptr; |
04c52f10 |
8405 | if (!cmd) cmd = ssh->cfg.remote_cmd; |
fd5e5847 |
8406 | } |
8407 | |
ff3187f6 |
8408 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
8409 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); /* recipient channel */ |
fd5e5847 |
8410 | if (subsys) { |
ff3187f6 |
8411 | ssh2_pkt_addstring(s->pktout, "subsystem"); |
8412 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
8413 | ssh2_pkt_addstring(s->pktout, cmd); |
fd5e5847 |
8414 | } else if (*cmd) { |
ff3187f6 |
8415 | ssh2_pkt_addstring(s->pktout, "exec"); |
8416 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
8417 | ssh2_pkt_addstring(s->pktout, cmd); |
fd5e5847 |
8418 | } else { |
ff3187f6 |
8419 | ssh2_pkt_addstring(s->pktout, "shell"); |
8420 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
32874aea |
8421 | } |
ff3187f6 |
8422 | ssh2_pkt_send(ssh, s->pktout); |
b09eaa88 |
8423 | |
8424 | crWaitUntilV(pktin); |
8425 | |
ff3187f6 |
8426 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
8427 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
6b5cf8b4 |
8428 | bombout(("Unexpected response to shell/command request:" |
ff3187f6 |
8429 | " packet type %d", pktin->type)); |
7ffdbc1a |
8430 | crStopV; |
fd5e5847 |
8431 | } |
8432 | /* |
8433 | * We failed to start the command. If this is the |
8434 | * fallback command, we really are finished; if it's |
8435 | * not, and if the fallback command exists, try falling |
8436 | * back to it before complaining. |
8437 | */ |
86916870 |
8438 | if (!ssh->fallback_cmd && ssh->cfg.remote_cmd_ptr2 != NULL) { |
fd5e5847 |
8439 | logevent("Primary command failed; attempting fallback"); |
51470298 |
8440 | ssh->fallback_cmd = TRUE; |
fd5e5847 |
8441 | continue; |
8442 | } |
6b5cf8b4 |
8443 | bombout(("Server refused to start a shell/command")); |
7ffdbc1a |
8444 | crStopV; |
fd5e5847 |
8445 | } else { |
8446 | logevent("Started a shell/command"); |
32874aea |
8447 | } |
fd5e5847 |
8448 | break; |
7cca0d81 |
8449 | } |
8450 | |
51470298 |
8451 | ssh->state = SSH_STATE_SESSION; |
8452 | if (ssh->size_needed) |
8453 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
8454 | if (ssh->eof_needed) |
8455 | ssh_special(ssh, TS_EOF); |
6e48c3fe |
8456 | |
7cca0d81 |
8457 | /* |
0e443c99 |
8458 | * All the initial channel requests are done, so install the default |
8459 | * failure handler. |
8460 | */ |
8461 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = ssh2_msg_channel_failure; |
8462 | |
8463 | /* |
7cca0d81 |
8464 | * Transfer data! |
8465 | */ |
b9d7bcad |
8466 | if (ssh->ldisc) |
8467 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
0ed48730 |
8468 | if (ssh->mainchan) |
8469 | ssh->send_ok = 1; |
7cca0d81 |
8470 | while (1) { |
e5574168 |
8471 | crReturnV; |
51470298 |
8472 | s->try_send = FALSE; |
ff3187f6 |
8473 | if (pktin) { |
2b7540a7 |
8474 | |
51df0ab5 |
8475 | /* |
8476 | * _All_ the connection-layer packets we expect to |
8477 | * receive are now handled by the dispatch table. |
8478 | * Anything that reaches here must be bogus. |
8479 | */ |
32874aea |
8480 | |
51df0ab5 |
8481 | bombout(("Strange packet received: type %d", pktin->type)); |
8482 | crStopV; |
0ed48730 |
8483 | } else if (ssh->mainchan) { |
32874aea |
8484 | /* |
8485 | * We have spare data. Add it to the channel buffer. |
8486 | */ |
d8baa528 |
8487 | ssh2_add_channel_data(ssh->mainchan, (char *)in, inlen); |
51470298 |
8488 | s->try_send = TRUE; |
32874aea |
8489 | } |
51470298 |
8490 | if (s->try_send) { |
32874aea |
8491 | int i; |
8492 | struct ssh_channel *c; |
8493 | /* |
8494 | * Try to send data on all channels if we can. |
8495 | */ |
1bfc7e93 |
8496 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) |
8497 | ssh2_try_send_and_unthrottle(c); |
7cca0d81 |
8498 | } |
e5574168 |
8499 | } |
8500 | |
8501 | crFinishV; |
8502 | } |
8503 | |
8504 | /* |
2e85c969 |
8505 | * Handlers for SSH-2 messages that might arrive at any moment. |
b09eaa88 |
8506 | */ |
409bfa77 |
8507 | static void ssh2_msg_disconnect(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
8508 | { |
8509 | /* log reason code in disconnect message */ |
8510 | char *buf, *msg; |
8511 | int nowlen, reason, msglen; |
8512 | |
8513 | reason = ssh_pkt_getuint32(pktin); |
8514 | ssh_pkt_getstring(pktin, &msg, &msglen); |
8515 | |
8516 | if (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) { |
8517 | buf = dupprintf("Received disconnect message (%s)", |
8518 | ssh2_disconnect_reasons[reason]); |
8519 | } else { |
8520 | buf = dupprintf("Received disconnect message (unknown" |
8521 | " type %d)", reason); |
8522 | } |
8523 | logevent(buf); |
8524 | sfree(buf); |
8525 | buf = dupprintf("Disconnection message text: %n%.*s", |
8526 | &nowlen, msglen, msg); |
8527 | logevent(buf); |
8528 | bombout(("Server sent disconnect message\ntype %d (%s):\n\"%s\"", |
8529 | reason, |
8530 | (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) ? |
8531 | ssh2_disconnect_reasons[reason] : "unknown", |
8532 | buf+nowlen)); |
8533 | sfree(buf); |
8534 | } |
8535 | |
409bfa77 |
8536 | static void ssh2_msg_debug(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
8537 | { |
8538 | /* log the debug message */ |
fb983202 |
8539 | char *msg; |
b09eaa88 |
8540 | int msglen; |
8541 | int always_display; |
8542 | |
8543 | /* XXX maybe we should actually take notice of this */ |
8544 | always_display = ssh2_pkt_getbool(pktin); |
8545 | ssh_pkt_getstring(pktin, &msg, &msglen); |
8546 | |
fb983202 |
8547 | logeventf(ssh, "Remote debug message: %.*s", msglen, msg); |
b09eaa88 |
8548 | } |
8549 | |
409bfa77 |
8550 | static void ssh2_msg_something_unimplemented(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
8551 | { |
8552 | struct Packet *pktout; |
8553 | pktout = ssh2_pkt_init(SSH2_MSG_UNIMPLEMENTED); |
8554 | ssh2_pkt_adduint32(pktout, pktin->sequence); |
8555 | /* |
8556 | * UNIMPLEMENTED messages MUST appear in the same order as the |
8557 | * messages they respond to. Hence, never queue them. |
8558 | */ |
8559 | ssh2_pkt_send_noqueue(ssh, pktout); |
8560 | } |
8561 | |
8562 | /* |
2e85c969 |
8563 | * Handle the top-level SSH-2 protocol. |
7cca0d81 |
8564 | */ |
b09eaa88 |
8565 | static void ssh2_protocol_setup(Ssh ssh) |
8566 | { |
8567 | int i; |
8568 | |
8569 | /* |
8570 | * Most messages cause SSH2_MSG_UNIMPLEMENTED. |
8571 | */ |
8572 | for (i = 0; i < 256; i++) |
8573 | ssh->packet_dispatch[i] = ssh2_msg_something_unimplemented; |
8574 | |
8575 | /* |
8576 | * Any message we actually understand, we set to NULL so that |
8577 | * the coroutines will get it. |
8578 | */ |
8579 | ssh->packet_dispatch[SSH2_MSG_UNIMPLEMENTED] = NULL; |
8580 | ssh->packet_dispatch[SSH2_MSG_SERVICE_REQUEST] = NULL; |
8581 | ssh->packet_dispatch[SSH2_MSG_SERVICE_ACCEPT] = NULL; |
8582 | ssh->packet_dispatch[SSH2_MSG_KEXINIT] = NULL; |
8583 | ssh->packet_dispatch[SSH2_MSG_NEWKEYS] = NULL; |
8584 | ssh->packet_dispatch[SSH2_MSG_KEXDH_INIT] = NULL; |
8585 | ssh->packet_dispatch[SSH2_MSG_KEXDH_REPLY] = NULL; |
8586 | /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REQUEST] = NULL; duplicate case value */ |
8587 | /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_GROUP] = NULL; duplicate case value */ |
8588 | ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_INIT] = NULL; |
8589 | ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REPLY] = NULL; |
8590 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_REQUEST] = NULL; |
8591 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_FAILURE] = NULL; |
8592 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_SUCCESS] = NULL; |
8593 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = NULL; |
8594 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_PK_OK] = NULL; |
8595 | /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ] = NULL; duplicate case value */ |
8596 | /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_REQUEST] = NULL; duplicate case value */ |
8597 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_RESPONSE] = NULL; |
8598 | ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = NULL; |
8599 | ssh->packet_dispatch[SSH2_MSG_REQUEST_SUCCESS] = NULL; |
8600 | ssh->packet_dispatch[SSH2_MSG_REQUEST_FAILURE] = NULL; |
8601 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = NULL; |
8602 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = NULL; |
8603 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = NULL; |
8604 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = NULL; |
8605 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = NULL; |
8606 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = NULL; |
8607 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = NULL; |
8608 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = NULL; |
8609 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] = NULL; |
8610 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = NULL; |
8611 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = NULL; |
8612 | |
8613 | /* |
8614 | * These special message types we install handlers for. |
8615 | */ |
8616 | ssh->packet_dispatch[SSH2_MSG_DISCONNECT] = ssh2_msg_disconnect; |
2e85c969 |
8617 | ssh->packet_dispatch[SSH2_MSG_IGNORE] = ssh_msg_ignore; /* shared with SSH-1 */ |
b09eaa88 |
8618 | ssh->packet_dispatch[SSH2_MSG_DEBUG] = ssh2_msg_debug; |
8619 | } |
8620 | |
9442dd57 |
8621 | static void ssh2_timer(void *ctx, long now) |
8622 | { |
8623 | Ssh ssh = (Ssh)ctx; |
8624 | |
ecbb0000 |
8625 | if (ssh->state == SSH_STATE_CLOSED) |
8626 | return; |
8627 | |
e6c1536e |
8628 | if (!ssh->kex_in_progress && ssh->cfg.ssh_rekey_time != 0 && |
9442dd57 |
8629 | now - ssh->next_rekey >= 0) { |
f382c87d |
8630 | do_ssh2_transport(ssh, "timeout", -1, NULL); |
9442dd57 |
8631 | } |
8632 | } |
8633 | |
1c1a7262 |
8634 | static void ssh2_protocol(Ssh ssh, void *vin, int inlen, |
ff3187f6 |
8635 | struct Packet *pktin) |
7cca0d81 |
8636 | { |
1c1a7262 |
8637 | unsigned char *in = (unsigned char *)vin; |
b09eaa88 |
8638 | if (ssh->state == SSH_STATE_CLOSED) |
8639 | return; |
8640 | |
9442dd57 |
8641 | if (pktin) { |
8642 | ssh->incoming_data_size += pktin->encrypted_len; |
8643 | if (!ssh->kex_in_progress && |
d57f70af |
8644 | ssh->max_data_size != 0 && |
8645 | ssh->incoming_data_size > ssh->max_data_size) |
f382c87d |
8646 | do_ssh2_transport(ssh, "too much data received", -1, NULL); |
9442dd57 |
8647 | } |
8648 | |
b09eaa88 |
8649 | if (pktin && ssh->packet_dispatch[pktin->type]) { |
8650 | ssh->packet_dispatch[pktin->type](ssh, pktin); |
32874aea |
8651 | return; |
b09eaa88 |
8652 | } |
8653 | |
8654 | if (!ssh->protocol_initial_phase_done || |
8655 | (pktin && pktin->type >= 20 && pktin->type < 50)) { |
8656 | if (do_ssh2_transport(ssh, in, inlen, pktin) && |
8657 | !ssh->protocol_initial_phase_done) { |
8658 | ssh->protocol_initial_phase_done = TRUE; |
8659 | /* |
8660 | * Allow authconn to initialise itself. |
8661 | */ |
8662 | do_ssh2_authconn(ssh, NULL, 0, NULL); |
8663 | } |
8664 | } else { |
8665 | do_ssh2_authconn(ssh, in, inlen, pktin); |
8666 | } |
7cca0d81 |
8667 | } |
8668 | |
8669 | /* |
8df7a775 |
8670 | * Called to set up the connection. |
374330e2 |
8671 | * |
8672 | * Returns an error message, or NULL on success. |
374330e2 |
8673 | */ |
cbe2d68f |
8674 | static const char *ssh_init(void *frontend_handle, void **backend_handle, |
8675 | Config *cfg, |
79bf227b |
8676 | char *host, int port, char **realhost, int nodelay, |
8677 | int keepalive) |
32874aea |
8678 | { |
cbe2d68f |
8679 | const char *p; |
51470298 |
8680 | Ssh ssh; |
8681 | |
3d88e64d |
8682 | ssh = snew(struct ssh_tag); |
86916870 |
8683 | ssh->cfg = *cfg; /* STRUCTURE COPY */ |
125105d1 |
8684 | ssh->version = 0; /* when not ready yet */ |
51470298 |
8685 | ssh->s = NULL; |
8686 | ssh->cipher = NULL; |
371e569c |
8687 | ssh->v1_cipher_ctx = NULL; |
0183b242 |
8688 | ssh->crcda_ctx = NULL; |
51470298 |
8689 | ssh->cscipher = NULL; |
371e569c |
8690 | ssh->cs_cipher_ctx = NULL; |
51470298 |
8691 | ssh->sccipher = NULL; |
371e569c |
8692 | ssh->sc_cipher_ctx = NULL; |
51470298 |
8693 | ssh->csmac = NULL; |
a8327734 |
8694 | ssh->cs_mac_ctx = NULL; |
51470298 |
8695 | ssh->scmac = NULL; |
e0e1a00d |
8696 | ssh->sc_mac_ctx = NULL; |
51470298 |
8697 | ssh->cscomp = NULL; |
5366aed8 |
8698 | ssh->cs_comp_ctx = NULL; |
51470298 |
8699 | ssh->sccomp = NULL; |
5366aed8 |
8700 | ssh->sc_comp_ctx = NULL; |
51470298 |
8701 | ssh->kex = NULL; |
389aa499 |
8702 | ssh->kex_ctx = NULL; |
51470298 |
8703 | ssh->hostkey = NULL; |
8704 | ssh->exitcode = -1; |
ac934965 |
8705 | ssh->close_expected = FALSE; |
9e296bfa |
8706 | ssh->clean_exit = FALSE; |
51470298 |
8707 | ssh->state = SSH_STATE_PREPACKET; |
8708 | ssh->size_needed = FALSE; |
8709 | ssh->eof_needed = FALSE; |
b9d7bcad |
8710 | ssh->ldisc = NULL; |
a8327734 |
8711 | ssh->logctx = NULL; |
51470298 |
8712 | ssh->deferred_send_data = NULL; |
8713 | ssh->deferred_len = 0; |
8714 | ssh->deferred_size = 0; |
8715 | ssh->fallback_cmd = 0; |
acab36bc |
8716 | ssh->pkt_kctx = SSH2_PKTCTX_NOKEX; |
8717 | ssh->pkt_actx = SSH2_PKTCTX_NOAUTH; |
302121de |
8718 | ssh->x11auth = NULL; |
be738459 |
8719 | ssh->v1_compressing = FALSE; |
51470298 |
8720 | ssh->v2_outgoing_sequence = 0; |
8721 | ssh->ssh1_rdpkt_crstate = 0; |
8722 | ssh->ssh2_rdpkt_crstate = 0; |
8723 | ssh->do_ssh_init_crstate = 0; |
8724 | ssh->ssh_gotdata_crstate = 0; |
b09eaa88 |
8725 | ssh->do_ssh1_connection_crstate = 0; |
51470298 |
8726 | ssh->do_ssh1_login_crstate = 0; |
8727 | ssh->do_ssh2_transport_crstate = 0; |
8728 | ssh->do_ssh2_authconn_crstate = 0; |
8729 | ssh->do_ssh_init_state = NULL; |
8730 | ssh->do_ssh1_login_state = NULL; |
8731 | ssh->do_ssh2_transport_state = NULL; |
8732 | ssh->do_ssh2_authconn_state = NULL; |
4320baf7 |
8733 | ssh->v_c = NULL; |
8734 | ssh->v_s = NULL; |
6571dbfd |
8735 | ssh->mainchan = NULL; |
968d2d92 |
8736 | ssh->throttled_all = 0; |
8737 | ssh->v1_stdout_throttling = 0; |
590f6a5f |
8738 | ssh->queue = NULL; |
8739 | ssh->queuelen = ssh->queuesize = 0; |
8740 | ssh->queueing = FALSE; |
06fadff5 |
8741 | ssh->qhead = ssh->qtail = NULL; |
e13bba36 |
8742 | ssh->deferred_rekey_reason = NULL; |
3d9449a1 |
8743 | bufchain_init(&ssh->queued_incoming_data); |
8744 | ssh->frozen = FALSE; |
51470298 |
8745 | |
8746 | *backend_handle = ssh; |
32874aea |
8747 | |
8f203108 |
8748 | #ifdef MSCRYPTOAPI |
32874aea |
8749 | if (crypto_startup() == 0) |
8f203108 |
8750 | return "Microsoft high encryption pack not installed!"; |
8751 | #endif |
374330e2 |
8752 | |
51470298 |
8753 | ssh->frontend = frontend_handle; |
86916870 |
8754 | ssh->term_width = ssh->cfg.width; |
8755 | ssh->term_height = ssh->cfg.height; |
887035a5 |
8756 | |
fabd1805 |
8757 | ssh->channels = NULL; |
8758 | ssh->rportfwds = NULL; |
f47a5ffb |
8759 | ssh->portfwds = NULL; |
fabd1805 |
8760 | |
51470298 |
8761 | ssh->send_ok = 0; |
8762 | ssh->editing = 0; |
8763 | ssh->echoing = 0; |
8764 | ssh->v1_throttle_count = 0; |
8765 | ssh->overall_bufsize = 0; |
8766 | ssh->fallback_cmd = 0; |
8df7a775 |
8767 | |
3648d4c5 |
8768 | ssh->protocol = NULL; |
8769 | |
b09eaa88 |
8770 | ssh->protocol_initial_phase_done = FALSE; |
8771 | |
39934deb |
8772 | ssh->pinger = NULL; |
8773 | |
9442dd57 |
8774 | ssh->incoming_data_size = ssh->outgoing_data_size = |
8775 | ssh->deferred_data_size = 0L; |
d57f70af |
8776 | ssh->max_data_size = parse_blocksize(ssh->cfg.ssh_rekey_data); |
9442dd57 |
8777 | ssh->kex_in_progress = FALSE; |
8778 | |
79bf227b |
8779 | p = connect_to_host(ssh, host, port, realhost, nodelay, keepalive); |
fb09bf1c |
8780 | if (p != NULL) |
8781 | return p; |
374330e2 |
8782 | |
5d17ccfc |
8783 | random_ref(); |
8784 | |
374330e2 |
8785 | return NULL; |
8786 | } |
8787 | |
fabd1805 |
8788 | static void ssh_free(void *handle) |
8789 | { |
8790 | Ssh ssh = (Ssh) handle; |
8791 | struct ssh_channel *c; |
8792 | struct ssh_rportfwd *pf; |
8793 | |
8794 | if (ssh->v1_cipher_ctx) |
8795 | ssh->cipher->free_context(ssh->v1_cipher_ctx); |
8796 | if (ssh->cs_cipher_ctx) |
8797 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
8798 | if (ssh->sc_cipher_ctx) |
8799 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
8800 | if (ssh->cs_mac_ctx) |
8801 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
8802 | if (ssh->sc_mac_ctx) |
8803 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
29b1d0b3 |
8804 | if (ssh->cs_comp_ctx) { |
8805 | if (ssh->cscomp) |
8806 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
8807 | else |
8808 | zlib_compress_cleanup(ssh->cs_comp_ctx); |
8809 | } |
8810 | if (ssh->sc_comp_ctx) { |
8811 | if (ssh->sccomp) |
8812 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
8813 | else |
8814 | zlib_decompress_cleanup(ssh->sc_comp_ctx); |
8815 | } |
fabd1805 |
8816 | if (ssh->kex_ctx) |
8817 | dh_cleanup(ssh->kex_ctx); |
8818 | sfree(ssh->savedhost); |
8819 | |
590f6a5f |
8820 | while (ssh->queuelen-- > 0) |
8821 | ssh_free_packet(ssh->queue[ssh->queuelen]); |
8822 | sfree(ssh->queue); |
8823 | |
06fadff5 |
8824 | while (ssh->qhead) { |
8825 | struct queued_handler *qh = ssh->qhead; |
8826 | ssh->qhead = qh->next; |
8827 | sfree(ssh->qhead); |
8828 | } |
8829 | ssh->qhead = ssh->qtail = NULL; |
8830 | |
fabd1805 |
8831 | if (ssh->channels) { |
8832 | while ((c = delpos234(ssh->channels, 0)) != NULL) { |
8833 | switch (c->type) { |
8834 | case CHAN_X11: |
8835 | if (c->u.x11.s != NULL) |
8836 | x11_close(c->u.x11.s); |
8837 | break; |
8838 | case CHAN_SOCKDATA: |
8839 | if (c->u.pfd.s != NULL) |
8840 | pfd_close(c->u.pfd.s); |
8841 | break; |
8842 | } |
8843 | sfree(c); |
8844 | } |
8845 | freetree234(ssh->channels); |
cdb52705 |
8846 | ssh->channels = NULL; |
fabd1805 |
8847 | } |
8848 | |
8849 | if (ssh->rportfwds) { |
8850 | while ((pf = delpos234(ssh->rportfwds, 0)) != NULL) |
8851 | sfree(pf); |
8852 | freetree234(ssh->rportfwds); |
cdb52705 |
8853 | ssh->rportfwds = NULL; |
fabd1805 |
8854 | } |
8855 | sfree(ssh->deferred_send_data); |
8856 | if (ssh->x11auth) |
8857 | x11_free_auth(ssh->x11auth); |
8858 | sfree(ssh->do_ssh_init_state); |
8859 | sfree(ssh->do_ssh1_login_state); |
8860 | sfree(ssh->do_ssh2_transport_state); |
8861 | sfree(ssh->do_ssh2_authconn_state); |
4320baf7 |
8862 | sfree(ssh->v_c); |
8863 | sfree(ssh->v_s); |
679539d7 |
8864 | if (ssh->crcda_ctx) { |
8865 | crcda_free_context(ssh->crcda_ctx); |
8866 | ssh->crcda_ctx = NULL; |
8867 | } |
fabd1805 |
8868 | if (ssh->s) |
ac934965 |
8869 | ssh_do_close(ssh, TRUE); |
9442dd57 |
8870 | expire_timer_context(ssh); |
39934deb |
8871 | if (ssh->pinger) |
8872 | pinger_free(ssh->pinger); |
3d9449a1 |
8873 | bufchain_clear(&ssh->queued_incoming_data); |
ee50e8b6 |
8874 | sfree(ssh); |
5d17ccfc |
8875 | |
8876 | random_unref(); |
fabd1805 |
8877 | } |
8878 | |
374330e2 |
8879 | /* |
86916870 |
8880 | * Reconfigure the SSH backend. |
86916870 |
8881 | */ |
8882 | static void ssh_reconfig(void *handle, Config *cfg) |
8883 | { |
8884 | Ssh ssh = (Ssh) handle; |
e13bba36 |
8885 | char *rekeying = NULL, rekey_mandatory = FALSE; |
e6c1536e |
8886 | unsigned long old_max_data_size; |
8887 | |
39934deb |
8888 | pinger_reconfig(ssh->pinger, &ssh->cfg, cfg); |
3b6606c7 |
8889 | if (ssh->portfwds) |
8890 | ssh_setup_portfwd(ssh, cfg); |
e6c1536e |
8891 | |
8892 | if (ssh->cfg.ssh_rekey_time != cfg->ssh_rekey_time && |
8893 | cfg->ssh_rekey_time != 0) { |
8894 | long new_next = ssh->last_rekey + cfg->ssh_rekey_time*60*TICKSPERSEC; |
8895 | long now = GETTICKCOUNT(); |
8896 | |
8897 | if (new_next - now < 0) { |
f382c87d |
8898 | rekeying = "timeout shortened"; |
e6c1536e |
8899 | } else { |
8900 | ssh->next_rekey = schedule_timer(new_next - now, ssh2_timer, ssh); |
8901 | } |
8902 | } |
8903 | |
8904 | old_max_data_size = ssh->max_data_size; |
8905 | ssh->max_data_size = parse_blocksize(cfg->ssh_rekey_data); |
8906 | if (old_max_data_size != ssh->max_data_size && |
8907 | ssh->max_data_size != 0) { |
8908 | if (ssh->outgoing_data_size > ssh->max_data_size || |
8909 | ssh->incoming_data_size > ssh->max_data_size) |
f382c87d |
8910 | rekeying = "data limit lowered"; |
e6c1536e |
8911 | } |
8912 | |
e13bba36 |
8913 | if (ssh->cfg.compression != cfg->compression) { |
f382c87d |
8914 | rekeying = "compression setting changed"; |
e13bba36 |
8915 | rekey_mandatory = TRUE; |
8916 | } |
8917 | |
8918 | if (ssh->cfg.ssh2_des_cbc != cfg->ssh2_des_cbc || |
8919 | memcmp(ssh->cfg.ssh_cipherlist, cfg->ssh_cipherlist, |
8920 | sizeof(ssh->cfg.ssh_cipherlist))) { |
f382c87d |
8921 | rekeying = "cipher settings changed"; |
e13bba36 |
8922 | rekey_mandatory = TRUE; |
e6c1536e |
8923 | } |
8924 | |
86916870 |
8925 | ssh->cfg = *cfg; /* STRUCTURE COPY */ |
e13bba36 |
8926 | |
8927 | if (rekeying) { |
8928 | if (!ssh->kex_in_progress) { |
8929 | do_ssh2_transport(ssh, rekeying, -1, NULL); |
8930 | } else if (rekey_mandatory) { |
8931 | ssh->deferred_rekey_reason = rekeying; |
8932 | } |
8933 | } |
86916870 |
8934 | } |
8935 | |
8936 | /* |
86dc445a |
8937 | * Called to send data down the SSH connection. |
374330e2 |
8938 | */ |
51470298 |
8939 | static int ssh_send(void *handle, char *buf, int len) |
32874aea |
8940 | { |
51470298 |
8941 | Ssh ssh = (Ssh) handle; |
8942 | |
8943 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
5471d09a |
8944 | return 0; |
374330e2 |
8945 | |
d8baa528 |
8946 | ssh->protocol(ssh, (unsigned char *)buf, len, 0); |
5471d09a |
8947 | |
51470298 |
8948 | return ssh_sendbuffer(ssh); |
5471d09a |
8949 | } |
8950 | |
8951 | /* |
8952 | * Called to query the current amount of buffered stdin data. |
8953 | */ |
51470298 |
8954 | static int ssh_sendbuffer(void *handle) |
5471d09a |
8955 | { |
51470298 |
8956 | Ssh ssh = (Ssh) handle; |
5471d09a |
8957 | int override_value; |
8958 | |
51470298 |
8959 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
5471d09a |
8960 | return 0; |
8961 | |
8962 | /* |
8963 | * If the SSH socket itself has backed up, add the total backup |
8964 | * size on that to any individual buffer on the stdin channel. |
8965 | */ |
8966 | override_value = 0; |
51470298 |
8967 | if (ssh->throttled_all) |
8968 | override_value = ssh->overall_bufsize; |
5471d09a |
8969 | |
51470298 |
8970 | if (ssh->version == 1) { |
5471d09a |
8971 | return override_value; |
51470298 |
8972 | } else if (ssh->version == 2) { |
8973 | if (!ssh->mainchan || ssh->mainchan->closes > 0) |
5471d09a |
8974 | return override_value; |
8975 | else |
51470298 |
8976 | return (override_value + |
8977 | bufchain_size(&ssh->mainchan->v.v2.outbuffer)); |
5471d09a |
8978 | } |
8979 | |
8980 | return 0; |
374330e2 |
8981 | } |
8982 | |
8983 | /* |
6e48c3fe |
8984 | * Called to set the size of the window from SSH's POV. |
374330e2 |
8985 | */ |
51470298 |
8986 | static void ssh_size(void *handle, int width, int height) |
32874aea |
8987 | { |
51470298 |
8988 | Ssh ssh = (Ssh) handle; |
ff3187f6 |
8989 | struct Packet *pktout; |
51470298 |
8990 | |
8991 | ssh->term_width = width; |
8992 | ssh->term_height = height; |
f278d6f8 |
8993 | |
51470298 |
8994 | switch (ssh->state) { |
374330e2 |
8995 | case SSH_STATE_BEFORE_SIZE: |
3687d221 |
8996 | case SSH_STATE_PREPACKET: |
21248260 |
8997 | case SSH_STATE_CLOSED: |
374330e2 |
8998 | break; /* do nothing */ |
8999 | case SSH_STATE_INTERMED: |
51470298 |
9000 | ssh->size_needed = TRUE; /* buffer for later */ |
374330e2 |
9001 | break; |
9002 | case SSH_STATE_SESSION: |
86916870 |
9003 | if (!ssh->cfg.nopty) { |
51470298 |
9004 | if (ssh->version == 1) { |
9005 | send_packet(ssh, SSH1_CMSG_WINDOW_SIZE, |
9006 | PKT_INT, ssh->term_height, |
9007 | PKT_INT, ssh->term_width, |
32874aea |
9008 | PKT_INT, 0, PKT_INT, 0, PKT_END); |
0ed48730 |
9009 | } else if (ssh->mainchan) { |
ff3187f6 |
9010 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
9011 | ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid); |
9012 | ssh2_pkt_addstring(pktout, "window-change"); |
9013 | ssh2_pkt_addbool(pktout, 0); |
9014 | ssh2_pkt_adduint32(pktout, ssh->term_width); |
9015 | ssh2_pkt_adduint32(pktout, ssh->term_height); |
9016 | ssh2_pkt_adduint32(pktout, 0); |
9017 | ssh2_pkt_adduint32(pktout, 0); |
9018 | ssh2_pkt_send(ssh, pktout); |
32874aea |
9019 | } |
9020 | } |
9021 | break; |
374330e2 |
9022 | } |
9023 | } |
9024 | |
9025 | /* |
125105d1 |
9026 | * Return a list of the special codes that make sense in this |
9027 | * protocol. |
9028 | */ |
9029 | static const struct telnet_special *ssh_get_specials(void *handle) |
9030 | { |
786ba75e |
9031 | static const struct telnet_special ssh1_ignore_special[] = { |
9032 | {"IGNORE message", TS_NOP} |
9033 | }; |
9034 | static const struct telnet_special ssh2_transport_specials[] = { |
62638676 |
9035 | {"IGNORE message", TS_NOP}, |
9442dd57 |
9036 | {"Repeat key exchange", TS_REKEY}, |
62638676 |
9037 | }; |
9038 | static const struct telnet_special ssh2_session_specials[] = { |
6f2d0cde |
9039 | {NULL, TS_SEP}, |
9040 | {"Break", TS_BRK}, |
786ba75e |
9041 | /* These are the signal names defined by draft-ietf-secsh-connect-23. |
6f2d0cde |
9042 | * They include all the ISO C signals, but are a subset of the POSIX |
9043 | * required signals. */ |
9044 | {"SIGINT (Interrupt)", TS_SIGINT}, |
9045 | {"SIGTERM (Terminate)", TS_SIGTERM}, |
9046 | {"SIGKILL (Kill)", TS_SIGKILL}, |
9047 | {"SIGQUIT (Quit)", TS_SIGQUIT}, |
9048 | {"SIGHUP (Hangup)", TS_SIGHUP}, |
9049 | {"More signals", TS_SUBMENU}, |
9050 | {"SIGABRT", TS_SIGABRT}, {"SIGALRM", TS_SIGALRM}, |
9051 | {"SIGFPE", TS_SIGFPE}, {"SIGILL", TS_SIGILL}, |
9052 | {"SIGPIPE", TS_SIGPIPE}, {"SIGSEGV", TS_SIGSEGV}, |
9053 | {"SIGUSR1", TS_SIGUSR1}, {"SIGUSR2", TS_SIGUSR2}, |
9054 | {NULL, TS_EXITMENU} |
62638676 |
9055 | }; |
9056 | static const struct telnet_special specials_end[] = { |
6f2d0cde |
9057 | {NULL, TS_EXITMENU} |
62638676 |
9058 | }; |
786ba75e |
9059 | /* XXX review this length for any changes: */ |
9060 | static struct telnet_special ssh_specials[lenof(ssh2_transport_specials) + |
62638676 |
9061 | lenof(ssh2_session_specials) + |
9062 | lenof(specials_end)]; |
125105d1 |
9063 | Ssh ssh = (Ssh) handle; |
62638676 |
9064 | int i = 0; |
9065 | #define ADD_SPECIALS(name) \ |
9066 | do { \ |
9067 | assert((i + lenof(name)) <= lenof(ssh_specials)); \ |
9068 | memcpy(&ssh_specials[i], name, sizeof name); \ |
9069 | i += lenof(name); \ |
9070 | } while(0) |
125105d1 |
9071 | |
9072 | if (ssh->version == 1) { |
62638676 |
9073 | /* Don't bother offering IGNORE if we've decided the remote |
9074 | * won't cope with it, since we wouldn't bother sending it if |
9075 | * asked anyway. */ |
9076 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) |
786ba75e |
9077 | ADD_SPECIALS(ssh1_ignore_special); |
125105d1 |
9078 | } else if (ssh->version == 2) { |
786ba75e |
9079 | ADD_SPECIALS(ssh2_transport_specials); |
62638676 |
9080 | if (ssh->mainchan) |
9081 | ADD_SPECIALS(ssh2_session_specials); |
9082 | } /* else we're not ready yet */ |
9083 | |
9084 | if (i) { |
9085 | ADD_SPECIALS(specials_end); |
9086 | return ssh_specials; |
9087 | } else { |
125105d1 |
9088 | return NULL; |
62638676 |
9089 | } |
9090 | #undef ADD_SPECIALS |
125105d1 |
9091 | } |
9092 | |
9093 | /* |
86dc445a |
9094 | * Send special codes. TS_EOF is useful for `plink', so you |
6abbf9e3 |
9095 | * can send an EOF and collect resulting output (e.g. `plink |
9096 | * hostname sort'). |
374330e2 |
9097 | */ |
51470298 |
9098 | static void ssh_special(void *handle, Telnet_Special code) |
32874aea |
9099 | { |
51470298 |
9100 | Ssh ssh = (Ssh) handle; |
ff3187f6 |
9101 | struct Packet *pktout; |
51470298 |
9102 | |
6abbf9e3 |
9103 | if (code == TS_EOF) { |
51470298 |
9104 | if (ssh->state != SSH_STATE_SESSION) { |
32874aea |
9105 | /* |
9106 | * Buffer the EOF in case we are pre-SESSION, so we can |
9107 | * send it as soon as we reach SESSION. |
9108 | */ |
9109 | if (code == TS_EOF) |
51470298 |
9110 | ssh->eof_needed = TRUE; |
32874aea |
9111 | return; |
9112 | } |
51470298 |
9113 | if (ssh->version == 1) { |
9114 | send_packet(ssh, SSH1_CMSG_EOF, PKT_END); |
0ed48730 |
9115 | } else if (ssh->mainchan) { |
ff3187f6 |
9116 | struct Packet *pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF); |
9117 | ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid); |
9118 | ssh2_pkt_send(ssh, pktout); |
00a0b113 |
9119 | ssh->send_ok = 0; /* now stop trying to read from stdin */ |
32874aea |
9120 | } |
9121 | logevent("Sent EOF message"); |
125105d1 |
9122 | } else if (code == TS_PING || code == TS_NOP) { |
51470298 |
9123 | if (ssh->state == SSH_STATE_CLOSED |
9124 | || ssh->state == SSH_STATE_PREPACKET) return; |
9125 | if (ssh->version == 1) { |
9126 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) |
9127 | send_packet(ssh, SSH1_MSG_IGNORE, PKT_STR, "", PKT_END); |
32874aea |
9128 | } else { |
ff3187f6 |
9129 | pktout = ssh2_pkt_init(SSH2_MSG_IGNORE); |
9130 | ssh2_pkt_addstring_start(pktout); |
590f6a5f |
9131 | ssh2_pkt_send_noqueue(ssh, pktout); |
32874aea |
9132 | } |
9442dd57 |
9133 | } else if (code == TS_REKEY) { |
9134 | if (!ssh->kex_in_progress && ssh->version == 2) { |
f382c87d |
9135 | do_ssh2_transport(ssh, "at user request", -1, NULL); |
9442dd57 |
9136 | } |
125105d1 |
9137 | } else if (code == TS_BRK) { |
9138 | if (ssh->state == SSH_STATE_CLOSED |
9139 | || ssh->state == SSH_STATE_PREPACKET) return; |
9140 | if (ssh->version == 1) { |
2e85c969 |
9141 | logevent("Unable to send BREAK signal in SSH-1"); |
0ed48730 |
9142 | } else if (ssh->mainchan) { |
ff3187f6 |
9143 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
9144 | ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid); |
9145 | ssh2_pkt_addstring(pktout, "break"); |
9146 | ssh2_pkt_addbool(pktout, 0); |
9147 | ssh2_pkt_adduint32(pktout, 0); /* default break length */ |
9148 | ssh2_pkt_send(ssh, pktout); |
125105d1 |
9149 | } |
6abbf9e3 |
9150 | } else { |
6f2d0cde |
9151 | /* Is is a POSIX signal? */ |
9152 | char *signame = NULL; |
9153 | if (code == TS_SIGABRT) signame = "ABRT"; |
9154 | if (code == TS_SIGALRM) signame = "ALRM"; |
9155 | if (code == TS_SIGFPE) signame = "FPE"; |
9156 | if (code == TS_SIGHUP) signame = "HUP"; |
9157 | if (code == TS_SIGILL) signame = "ILL"; |
9158 | if (code == TS_SIGINT) signame = "INT"; |
9159 | if (code == TS_SIGKILL) signame = "KILL"; |
9160 | if (code == TS_SIGPIPE) signame = "PIPE"; |
9161 | if (code == TS_SIGQUIT) signame = "QUIT"; |
9162 | if (code == TS_SIGSEGV) signame = "SEGV"; |
9163 | if (code == TS_SIGTERM) signame = "TERM"; |
9164 | if (code == TS_SIGUSR1) signame = "USR1"; |
9165 | if (code == TS_SIGUSR2) signame = "USR2"; |
9166 | /* The SSH-2 protocol does in principle support arbitrary named |
9167 | * signals, including signame@domain, but we don't support those. */ |
9168 | if (signame) { |
9169 | /* It's a signal. */ |
9170 | if (ssh->version == 2 && ssh->mainchan) { |
ff3187f6 |
9171 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
9172 | ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid); |
9173 | ssh2_pkt_addstring(pktout, "signal"); |
9174 | ssh2_pkt_addbool(pktout, 0); |
9175 | ssh2_pkt_addstring(pktout, signame); |
9176 | ssh2_pkt_send(ssh, pktout); |
6f2d0cde |
9177 | logeventf(ssh, "Sent signal SIG%s", signame); |
9178 | } |
9179 | } else { |
9180 | /* Never heard of it. Do nothing */ |
9181 | } |
6abbf9e3 |
9182 | } |
374330e2 |
9183 | } |
9184 | |
51470298 |
9185 | void *new_sock_channel(void *handle, Socket s) |
d74d141c |
9186 | { |
51470298 |
9187 | Ssh ssh = (Ssh) handle; |
d74d141c |
9188 | struct ssh_channel *c; |
3d88e64d |
9189 | c = snew(struct ssh_channel); |
51470298 |
9190 | c->ssh = ssh; |
d74d141c |
9191 | |
9192 | if (c) { |
64d6ff88 |
9193 | c->halfopen = TRUE; |
51470298 |
9194 | c->localid = alloc_channel_id(ssh); |
d74d141c |
9195 | c->closes = 0; |
bc240b21 |
9196 | c->type = CHAN_SOCKDATA_DORMANT;/* identify channel type */ |
d74d141c |
9197 | c->u.pfd.s = s; |
013dd8c0 |
9198 | bufchain_init(&c->v.v2.outbuffer); |
51470298 |
9199 | add234(ssh->channels, c); |
d74d141c |
9200 | } |
9201 | return c; |
9202 | } |
9203 | |
5471d09a |
9204 | /* |
9205 | * This is called when stdout/stderr (the entity to which |
9206 | * from_backend sends data) manages to clear some backlog. |
9207 | */ |
ae9ae89f |
9208 | static void ssh_unthrottle(void *handle, int bufsize) |
5471d09a |
9209 | { |
51470298 |
9210 | Ssh ssh = (Ssh) handle; |
9211 | if (ssh->version == 1) { |
9212 | if (ssh->v1_stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) { |
9213 | ssh->v1_stdout_throttling = 0; |
9214 | ssh1_throttle(ssh, -1); |
5471d09a |
9215 | } |
9216 | } else { |
ab87bf1d |
9217 | if (ssh->mainchan) |
9218 | ssh2_set_window(ssh->mainchan, |
9219 | ssh->mainchan->v.v2.locmaxwin - bufsize); |
5471d09a |
9220 | } |
9221 | } |
9222 | |
6b78788a |
9223 | void ssh_send_port_open(void *channel, char *hostname, int port, char *org) |
d74d141c |
9224 | { |
9225 | struct ssh_channel *c = (struct ssh_channel *)channel; |
6b78788a |
9226 | Ssh ssh = c->ssh; |
ff3187f6 |
9227 | struct Packet *pktout; |
d74d141c |
9228 | |
57356d63 |
9229 | logeventf(ssh, "Opening forwarded connection to %s:%d", hostname, port); |
d74d141c |
9230 | |
51470298 |
9231 | if (ssh->version == 1) { |
9232 | send_packet(ssh, SSH1_MSG_PORT_OPEN, |
bc240b21 |
9233 | PKT_INT, c->localid, |
9234 | PKT_STR, hostname, |
9235 | PKT_INT, port, |
31fb1866 |
9236 | /* PKT_STR, <org:orgport>, */ |
bc240b21 |
9237 | PKT_END); |
9238 | } else { |
ff3187f6 |
9239 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
9240 | ssh2_pkt_addstring(pktout, "direct-tcpip"); |
9241 | ssh2_pkt_adduint32(pktout, c->localid); |
9b53e9b8 |
9242 | c->v.v2.locwindow = c->v.v2.locmaxwin = OUR_V2_WINSIZE; |
0e443c99 |
9243 | c->v.v2.remlocwin = OUR_V2_WINSIZE; |
9244 | c->v.v2.winadj_head = c->v.v2.winadj_head = NULL; |
9245 | c->v.v2.throttle_state = UNTHROTTLED; |
ff3187f6 |
9246 | ssh2_pkt_adduint32(pktout, c->v.v2.locwindow);/* our window size */ |
954d5c5a |
9247 | ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
ff3187f6 |
9248 | ssh2_pkt_addstring(pktout, hostname); |
9249 | ssh2_pkt_adduint32(pktout, port); |
bc240b21 |
9250 | /* |
9251 | * We make up values for the originator data; partly it's |
9252 | * too much hassle to keep track, and partly I'm not |
9253 | * convinced the server should be told details like that |
9254 | * about my local network configuration. |
1f182589 |
9255 | * The "originator IP address" is syntactically a numeric |
9256 | * IP address, and some servers (e.g., Tectia) get upset |
9257 | * if it doesn't match this syntax. |
bc240b21 |
9258 | */ |
1f182589 |
9259 | ssh2_pkt_addstring(pktout, "0.0.0.0"); |
ff3187f6 |
9260 | ssh2_pkt_adduint32(pktout, 0); |
9261 | ssh2_pkt_send(ssh, pktout); |
bc240b21 |
9262 | } |
d74d141c |
9263 | } |
9264 | |
6226c939 |
9265 | static int ssh_connected(void *handle) |
32874aea |
9266 | { |
51470298 |
9267 | Ssh ssh = (Ssh) handle; |
6226c939 |
9268 | return ssh->s != NULL; |
32874aea |
9269 | } |
8ccc75b0 |
9270 | |
51470298 |
9271 | static int ssh_sendok(void *handle) |
32874aea |
9272 | { |
51470298 |
9273 | Ssh ssh = (Ssh) handle; |
9274 | return ssh->send_ok; |
32874aea |
9275 | } |
fb09bf1c |
9276 | |
51470298 |
9277 | static int ssh_ldisc(void *handle, int option) |
32874aea |
9278 | { |
51470298 |
9279 | Ssh ssh = (Ssh) handle; |
32874aea |
9280 | if (option == LD_ECHO) |
51470298 |
9281 | return ssh->echoing; |
32874aea |
9282 | if (option == LD_EDIT) |
51470298 |
9283 | return ssh->editing; |
0965bee0 |
9284 | return FALSE; |
9285 | } |
9286 | |
b9d7bcad |
9287 | static void ssh_provide_ldisc(void *handle, void *ldisc) |
9288 | { |
9289 | Ssh ssh = (Ssh) handle; |
9290 | ssh->ldisc = ldisc; |
9291 | } |
9292 | |
a8327734 |
9293 | static void ssh_provide_logctx(void *handle, void *logctx) |
9294 | { |
9295 | Ssh ssh = (Ssh) handle; |
9296 | ssh->logctx = logctx; |
9297 | } |
9298 | |
51470298 |
9299 | static int ssh_return_exitcode(void *handle) |
9300 | { |
9301 | Ssh ssh = (Ssh) handle; |
3bb2f322 |
9302 | if (ssh->s != NULL) |
9303 | return -1; |
9304 | else |
3c112d53 |
9305 | return (ssh->exitcode >= 0 ? ssh->exitcode : INT_MAX); |
51470298 |
9306 | } |
9307 | |
9308 | /* |
f89c3294 |
9309 | * cfg_info for SSH is the currently running version of the |
9310 | * protocol. (1 for 1; 2 for 2; 0 for not-decided-yet.) |
9311 | */ |
9312 | static int ssh_cfg_info(void *handle) |
9313 | { |
9314 | Ssh ssh = (Ssh) handle; |
9315 | return ssh->version; |
9316 | } |
9317 | |
9318 | /* |
51470298 |
9319 | * Gross hack: pscp will try to start SFTP but fall back to scp1 if |
9320 | * that fails. This variable is the means by which scp.c can reach |
9321 | * into the SSH code and find out which one it got. |
9322 | */ |
9323 | extern int ssh_fallback_cmd(void *handle) |
d8d6c7e5 |
9324 | { |
51470298 |
9325 | Ssh ssh = (Ssh) handle; |
9326 | return ssh->fallback_cmd; |
d8d6c7e5 |
9327 | } |
9328 | |
374330e2 |
9329 | Backend ssh_backend = { |
9330 | ssh_init, |
fabd1805 |
9331 | ssh_free, |
86916870 |
9332 | ssh_reconfig, |
374330e2 |
9333 | ssh_send, |
5471d09a |
9334 | ssh_sendbuffer, |
374330e2 |
9335 | ssh_size, |
4017be6d |
9336 | ssh_special, |
125105d1 |
9337 | ssh_get_specials, |
6226c939 |
9338 | ssh_connected, |
d8d6c7e5 |
9339 | ssh_return_exitcode, |
97db3be4 |
9340 | ssh_sendok, |
0965bee0 |
9341 | ssh_ldisc, |
b9d7bcad |
9342 | ssh_provide_ldisc, |
a8327734 |
9343 | ssh_provide_logctx, |
5471d09a |
9344 | ssh_unthrottle, |
f89c3294 |
9345 | ssh_cfg_info, |
9e164d82 |
9346 | "ssh", |
9347 | PROT_SSH, |
97db3be4 |
9348 | 22 |
bc240b21 |
9349 | }; |