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" |
bf84a03a |
15 | #ifndef NO_GSSAPI |
b3d375b2 |
16 | #include "sshgssc.h" |
42af6a67 |
17 | #include "sshgss.h" |
bf84a03a |
18 | #endif |
374330e2 |
19 | |
20 | #ifndef FALSE |
21 | #define FALSE 0 |
22 | #endif |
23 | #ifndef TRUE |
24 | #define TRUE 1 |
25 | #endif |
26 | |
32874aea |
27 | #define SSH1_MSG_DISCONNECT 1 /* 0x1 */ |
28 | #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */ |
29 | #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */ |
30 | #define SSH1_CMSG_USER 4 /* 0x4 */ |
31 | #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */ |
32 | #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */ |
33 | #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */ |
34 | #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */ |
35 | #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */ |
36 | #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */ |
37 | #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */ |
38 | #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */ |
39 | #define SSH1_SMSG_SUCCESS 14 /* 0xe */ |
40 | #define SSH1_SMSG_FAILURE 15 /* 0xf */ |
41 | #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */ |
42 | #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */ |
43 | #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */ |
44 | #define SSH1_CMSG_EOF 19 /* 0x13 */ |
45 | #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */ |
46 | #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */ |
47 | #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */ |
48 | #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */ |
49 | #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */ |
50 | #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */ |
51 | #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */ |
52 | #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */ |
53 | #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */ |
54 | #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */ |
55 | #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */ |
56 | #define SSH1_MSG_IGNORE 32 /* 0x20 */ |
57 | #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */ |
58 | #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */ |
59 | #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */ |
60 | #define SSH1_MSG_DEBUG 36 /* 0x24 */ |
61 | #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */ |
62 | #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */ |
63 | #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */ |
64 | #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */ |
65 | #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */ |
66 | #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */ |
67 | #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */ |
68 | |
8f9e3231 |
69 | #define SSH1_AUTH_RHOSTS 1 /* 0x1 */ |
70 | #define SSH1_AUTH_RSA 2 /* 0x2 */ |
71 | #define SSH1_AUTH_PASSWORD 3 /* 0x3 */ |
72 | #define SSH1_AUTH_RHOSTS_RSA 4 /* 0x4 */ |
32874aea |
73 | #define SSH1_AUTH_TIS 5 /* 0x5 */ |
74 | #define SSH1_AUTH_CCARD 16 /* 0x10 */ |
75 | |
76 | #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */ |
b96dc54c |
77 | /* Mask for protoflags we will echo back to server if seen */ |
32874aea |
78 | #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */ |
79 | |
80 | #define SSH2_MSG_DISCONNECT 1 /* 0x1 */ |
81 | #define SSH2_MSG_IGNORE 2 /* 0x2 */ |
82 | #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */ |
83 | #define SSH2_MSG_DEBUG 4 /* 0x4 */ |
84 | #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */ |
85 | #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */ |
86 | #define SSH2_MSG_KEXINIT 20 /* 0x14 */ |
87 | #define SSH2_MSG_NEWKEYS 21 /* 0x15 */ |
88 | #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */ |
89 | #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */ |
90 | #define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */ |
91 | #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */ |
92 | #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */ |
93 | #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */ |
fae1a71b |
94 | #define SSH2_MSG_KEXRSA_PUBKEY 30 /* 0x1e */ |
95 | #define SSH2_MSG_KEXRSA_SECRET 31 /* 0x1f */ |
96 | #define SSH2_MSG_KEXRSA_DONE 32 /* 0x20 */ |
32874aea |
97 | #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */ |
98 | #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */ |
99 | #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */ |
100 | #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */ |
101 | #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */ |
102 | #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */ |
761187b6 |
103 | #define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */ |
104 | #define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */ |
32874aea |
105 | #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */ |
106 | #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */ |
107 | #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */ |
108 | #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */ |
109 | #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */ |
110 | #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */ |
111 | #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */ |
112 | #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */ |
113 | #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */ |
114 | #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */ |
115 | #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */ |
116 | #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */ |
117 | #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */ |
118 | #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */ |
42af6a67 |
119 | #define SSH2_MSG_USERAUTH_GSSAPI_RESPONSE 60 |
120 | #define SSH2_MSG_USERAUTH_GSSAPI_TOKEN 61 |
121 | #define SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE 63 |
122 | #define SSH2_MSG_USERAUTH_GSSAPI_ERROR 64 |
123 | #define SSH2_MSG_USERAUTH_GSSAPI_ERRTOK 65 |
124 | #define SSH2_MSG_USERAUTH_GSSAPI_MIC 66 |
32874aea |
125 | |
00db133f |
126 | /* |
127 | * Packet type contexts, so that ssh2_pkt_type can correctly decode |
128 | * the ambiguous type numbers back into the correct type strings. |
129 | */ |
acab36bc |
130 | typedef enum { |
131 | SSH2_PKTCTX_NOKEX, |
132 | SSH2_PKTCTX_DHGROUP, |
133 | SSH2_PKTCTX_DHGEX, |
134 | SSH2_PKTCTX_RSAKEX |
135 | } Pkt_KCtx; |
136 | typedef enum { |
137 | SSH2_PKTCTX_NOAUTH, |
138 | SSH2_PKTCTX_PUBLICKEY, |
139 | SSH2_PKTCTX_PASSWORD, |
42af6a67 |
140 | SSH2_PKTCTX_GSSAPI, |
acab36bc |
141 | SSH2_PKTCTX_KBDINTER |
142 | } Pkt_ACtx; |
00db133f |
143 | |
32874aea |
144 | #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */ |
145 | #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */ |
146 | #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */ |
147 | #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */ |
148 | #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */ |
149 | #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */ |
150 | #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */ |
151 | #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */ |
152 | #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */ |
153 | #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */ |
154 | #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */ |
155 | #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */ |
156 | #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */ |
157 | #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */ |
158 | #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */ |
38c4a8da |
159 | |
160 | static const char *const ssh2_disconnect_reasons[] = { |
161 | NULL, |
afb4d0dc |
162 | "host not allowed to connect", |
163 | "protocol error", |
164 | "key exchange failed", |
165 | "host authentication failed", |
1da38d29 |
166 | "MAC error", |
afb4d0dc |
167 | "compression error", |
168 | "service not available", |
169 | "protocol version not supported", |
170 | "host key not verifiable", |
171 | "connection lost", |
172 | "by application", |
173 | "too many connections", |
174 | "auth cancelled by user", |
175 | "no more auth methods available", |
176 | "illegal user name", |
38c4a8da |
177 | }; |
9005f3ba |
178 | |
32874aea |
179 | #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */ |
180 | #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */ |
181 | #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */ |
182 | #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */ |
d211621f |
183 | |
32874aea |
184 | #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */ |
fb09bf1c |
185 | |
7d503c31 |
186 | /* |
187 | * Various remote-bug flags. |
188 | */ |
189 | #define BUG_CHOKES_ON_SSH1_IGNORE 1 |
190 | #define BUG_SSH2_HMAC 2 |
bd358db1 |
191 | #define BUG_NEEDS_SSH1_PLAIN_PASSWORD 4 |
0df73905 |
192 | #define BUG_CHOKES_ON_RSA 8 |
1dd353b5 |
193 | #define BUG_SSH2_RSA_PADDING 16 |
088bde77 |
194 | #define BUG_SSH2_DERIVEKEY 32 |
f382c87d |
195 | #define BUG_SSH2_REKEY 64 |
dda87a28 |
196 | #define BUG_SSH2_PK_SESSIONID 128 |
c9739dba |
197 | #define BUG_SSH2_MAXPKT 256 |
cf6ddb95 |
198 | #define BUG_CHOKES_ON_SSH2_IGNORE 512 |
2e0aae4f |
199 | #define BUG_CHOKES_ON_WINADJ 1024 |
bd358db1 |
200 | |
c6ccd5c2 |
201 | /* |
202 | * Codes for terminal modes. |
203 | * Most of these are the same in SSH-1 and SSH-2. |
7646c432 |
204 | * This list is derived from RFC 4254 and |
c6ccd5c2 |
205 | * SSH-1 RFC-1.2.31. |
206 | */ |
207 | static const struct { |
208 | const char* const mode; |
209 | int opcode; |
210 | enum { TTY_OP_CHAR, TTY_OP_BOOL } type; |
211 | } ssh_ttymodes[] = { |
212 | /* "V" prefix discarded for special characters relative to SSH specs */ |
213 | { "INTR", 1, TTY_OP_CHAR }, |
214 | { "QUIT", 2, TTY_OP_CHAR }, |
215 | { "ERASE", 3, TTY_OP_CHAR }, |
216 | { "KILL", 4, TTY_OP_CHAR }, |
217 | { "EOF", 5, TTY_OP_CHAR }, |
218 | { "EOL", 6, TTY_OP_CHAR }, |
219 | { "EOL2", 7, TTY_OP_CHAR }, |
220 | { "START", 8, TTY_OP_CHAR }, |
221 | { "STOP", 9, TTY_OP_CHAR }, |
222 | { "SUSP", 10, TTY_OP_CHAR }, |
223 | { "DSUSP", 11, TTY_OP_CHAR }, |
224 | { "REPRINT", 12, TTY_OP_CHAR }, |
225 | { "WERASE", 13, TTY_OP_CHAR }, |
226 | { "LNEXT", 14, TTY_OP_CHAR }, |
227 | { "FLUSH", 15, TTY_OP_CHAR }, |
228 | { "SWTCH", 16, TTY_OP_CHAR }, |
229 | { "STATUS", 17, TTY_OP_CHAR }, |
230 | { "DISCARD", 18, TTY_OP_CHAR }, |
231 | { "IGNPAR", 30, TTY_OP_BOOL }, |
232 | { "PARMRK", 31, TTY_OP_BOOL }, |
233 | { "INPCK", 32, TTY_OP_BOOL }, |
234 | { "ISTRIP", 33, TTY_OP_BOOL }, |
235 | { "INLCR", 34, TTY_OP_BOOL }, |
236 | { "IGNCR", 35, TTY_OP_BOOL }, |
237 | { "ICRNL", 36, TTY_OP_BOOL }, |
238 | { "IUCLC", 37, TTY_OP_BOOL }, |
239 | { "IXON", 38, TTY_OP_BOOL }, |
240 | { "IXANY", 39, TTY_OP_BOOL }, |
241 | { "IXOFF", 40, TTY_OP_BOOL }, |
242 | { "IMAXBEL", 41, TTY_OP_BOOL }, |
243 | { "ISIG", 50, TTY_OP_BOOL }, |
244 | { "ICANON", 51, TTY_OP_BOOL }, |
245 | { "XCASE", 52, TTY_OP_BOOL }, |
246 | { "ECHO", 53, TTY_OP_BOOL }, |
247 | { "ECHOE", 54, TTY_OP_BOOL }, |
248 | { "ECHOK", 55, TTY_OP_BOOL }, |
249 | { "ECHONL", 56, TTY_OP_BOOL }, |
250 | { "NOFLSH", 57, TTY_OP_BOOL }, |
251 | { "TOSTOP", 58, TTY_OP_BOOL }, |
252 | { "IEXTEN", 59, TTY_OP_BOOL }, |
253 | { "ECHOCTL", 60, TTY_OP_BOOL }, |
254 | { "ECHOKE", 61, TTY_OP_BOOL }, |
255 | { "PENDIN", 62, TTY_OP_BOOL }, /* XXX is this a real mode? */ |
256 | { "OPOST", 70, TTY_OP_BOOL }, |
257 | { "OLCUC", 71, TTY_OP_BOOL }, |
258 | { "ONLCR", 72, TTY_OP_BOOL }, |
259 | { "OCRNL", 73, TTY_OP_BOOL }, |
260 | { "ONOCR", 74, TTY_OP_BOOL }, |
261 | { "ONLRET", 75, TTY_OP_BOOL }, |
262 | { "CS7", 90, TTY_OP_BOOL }, |
263 | { "CS8", 91, TTY_OP_BOOL }, |
264 | { "PARENB", 92, TTY_OP_BOOL }, |
265 | { "PARODD", 93, TTY_OP_BOOL } |
266 | }; |
267 | |
268 | /* Miscellaneous other tty-related constants. */ |
269 | #define SSH_TTY_OP_END 0 |
270 | /* The opcodes for ISPEED/OSPEED differ between SSH-1 and SSH-2. */ |
271 | #define SSH1_TTY_OP_ISPEED 192 |
272 | #define SSH1_TTY_OP_OSPEED 193 |
273 | #define SSH2_TTY_OP_ISPEED 128 |
274 | #define SSH2_TTY_OP_OSPEED 129 |
275 | |
276 | /* Helper functions for parsing tty-related config. */ |
277 | static unsigned int ssh_tty_parse_specchar(char *s) |
278 | { |
279 | unsigned int ret; |
280 | if (*s) { |
281 | char *next = NULL; |
282 | ret = ctrlparse(s, &next); |
283 | if (!next) ret = s[0]; |
284 | } else { |
285 | ret = 255; /* special value meaning "don't set" */ |
286 | } |
287 | return ret; |
288 | } |
289 | static unsigned int ssh_tty_parse_boolean(char *s) |
290 | { |
291 | if (stricmp(s, "yes") == 0 || |
292 | stricmp(s, "on") == 0 || |
293 | stricmp(s, "true") == 0 || |
294 | stricmp(s, "+") == 0) |
295 | return 1; /* true */ |
296 | else if (stricmp(s, "no") == 0 || |
297 | stricmp(s, "off") == 0 || |
298 | stricmp(s, "false") == 0 || |
299 | stricmp(s, "-") == 0) |
300 | return 0; /* false */ |
301 | else |
302 | return (atoi(s) != 0); |
303 | } |
304 | |
00db133f |
305 | #define translate(x) if (type == x) return #x |
acab36bc |
306 | #define translatek(x,ctx) if (type == x && (pkt_kctx == ctx)) return #x |
307 | #define translatea(x,ctx) if (type == x && (pkt_actx == ctx)) return #x |
ae9ae89f |
308 | static char *ssh1_pkt_type(int type) |
00db133f |
309 | { |
310 | translate(SSH1_MSG_DISCONNECT); |
311 | translate(SSH1_SMSG_PUBLIC_KEY); |
312 | translate(SSH1_CMSG_SESSION_KEY); |
313 | translate(SSH1_CMSG_USER); |
314 | translate(SSH1_CMSG_AUTH_RSA); |
315 | translate(SSH1_SMSG_AUTH_RSA_CHALLENGE); |
316 | translate(SSH1_CMSG_AUTH_RSA_RESPONSE); |
317 | translate(SSH1_CMSG_AUTH_PASSWORD); |
318 | translate(SSH1_CMSG_REQUEST_PTY); |
319 | translate(SSH1_CMSG_WINDOW_SIZE); |
320 | translate(SSH1_CMSG_EXEC_SHELL); |
321 | translate(SSH1_CMSG_EXEC_CMD); |
322 | translate(SSH1_SMSG_SUCCESS); |
323 | translate(SSH1_SMSG_FAILURE); |
324 | translate(SSH1_CMSG_STDIN_DATA); |
325 | translate(SSH1_SMSG_STDOUT_DATA); |
326 | translate(SSH1_SMSG_STDERR_DATA); |
327 | translate(SSH1_CMSG_EOF); |
328 | translate(SSH1_SMSG_EXIT_STATUS); |
329 | translate(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION); |
330 | translate(SSH1_MSG_CHANNEL_OPEN_FAILURE); |
331 | translate(SSH1_MSG_CHANNEL_DATA); |
332 | translate(SSH1_MSG_CHANNEL_CLOSE); |
333 | translate(SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION); |
334 | translate(SSH1_SMSG_X11_OPEN); |
335 | translate(SSH1_CMSG_PORT_FORWARD_REQUEST); |
336 | translate(SSH1_MSG_PORT_OPEN); |
337 | translate(SSH1_CMSG_AGENT_REQUEST_FORWARDING); |
338 | translate(SSH1_SMSG_AGENT_OPEN); |
339 | translate(SSH1_MSG_IGNORE); |
340 | translate(SSH1_CMSG_EXIT_CONFIRMATION); |
341 | translate(SSH1_CMSG_X11_REQUEST_FORWARDING); |
342 | translate(SSH1_CMSG_AUTH_RHOSTS_RSA); |
343 | translate(SSH1_MSG_DEBUG); |
344 | translate(SSH1_CMSG_REQUEST_COMPRESSION); |
345 | translate(SSH1_CMSG_AUTH_TIS); |
346 | translate(SSH1_SMSG_AUTH_TIS_CHALLENGE); |
347 | translate(SSH1_CMSG_AUTH_TIS_RESPONSE); |
348 | translate(SSH1_CMSG_AUTH_CCARD); |
349 | translate(SSH1_SMSG_AUTH_CCARD_CHALLENGE); |
350 | translate(SSH1_CMSG_AUTH_CCARD_RESPONSE); |
351 | return "unknown"; |
352 | } |
acab36bc |
353 | static char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type) |
00db133f |
354 | { |
42af6a67 |
355 | translatea(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,SSH2_PKTCTX_GSSAPI); |
356 | translatea(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,SSH2_PKTCTX_GSSAPI); |
357 | translatea(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,SSH2_PKTCTX_GSSAPI); |
358 | translatea(SSH2_MSG_USERAUTH_GSSAPI_ERROR,SSH2_PKTCTX_GSSAPI); |
359 | translatea(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,SSH2_PKTCTX_GSSAPI); |
360 | translatea(SSH2_MSG_USERAUTH_GSSAPI_MIC, SSH2_PKTCTX_GSSAPI); |
00db133f |
361 | translate(SSH2_MSG_DISCONNECT); |
362 | translate(SSH2_MSG_IGNORE); |
363 | translate(SSH2_MSG_UNIMPLEMENTED); |
364 | translate(SSH2_MSG_DEBUG); |
365 | translate(SSH2_MSG_SERVICE_REQUEST); |
366 | translate(SSH2_MSG_SERVICE_ACCEPT); |
367 | translate(SSH2_MSG_KEXINIT); |
368 | translate(SSH2_MSG_NEWKEYS); |
acab36bc |
369 | translatek(SSH2_MSG_KEXDH_INIT, SSH2_PKTCTX_DHGROUP); |
370 | translatek(SSH2_MSG_KEXDH_REPLY, SSH2_PKTCTX_DHGROUP); |
371 | translatek(SSH2_MSG_KEX_DH_GEX_REQUEST, SSH2_PKTCTX_DHGEX); |
372 | translatek(SSH2_MSG_KEX_DH_GEX_GROUP, SSH2_PKTCTX_DHGEX); |
373 | translatek(SSH2_MSG_KEX_DH_GEX_INIT, SSH2_PKTCTX_DHGEX); |
374 | translatek(SSH2_MSG_KEX_DH_GEX_REPLY, SSH2_PKTCTX_DHGEX); |
375 | translatek(SSH2_MSG_KEXRSA_PUBKEY, SSH2_PKTCTX_RSAKEX); |
376 | translatek(SSH2_MSG_KEXRSA_SECRET, SSH2_PKTCTX_RSAKEX); |
377 | translatek(SSH2_MSG_KEXRSA_DONE, SSH2_PKTCTX_RSAKEX); |
00db133f |
378 | translate(SSH2_MSG_USERAUTH_REQUEST); |
379 | translate(SSH2_MSG_USERAUTH_FAILURE); |
380 | translate(SSH2_MSG_USERAUTH_SUCCESS); |
381 | translate(SSH2_MSG_USERAUTH_BANNER); |
acab36bc |
382 | translatea(SSH2_MSG_USERAUTH_PK_OK, SSH2_PKTCTX_PUBLICKEY); |
383 | translatea(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, SSH2_PKTCTX_PASSWORD); |
384 | translatea(SSH2_MSG_USERAUTH_INFO_REQUEST, SSH2_PKTCTX_KBDINTER); |
385 | translatea(SSH2_MSG_USERAUTH_INFO_RESPONSE, SSH2_PKTCTX_KBDINTER); |
00db133f |
386 | translate(SSH2_MSG_GLOBAL_REQUEST); |
387 | translate(SSH2_MSG_REQUEST_SUCCESS); |
388 | translate(SSH2_MSG_REQUEST_FAILURE); |
389 | translate(SSH2_MSG_CHANNEL_OPEN); |
390 | translate(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
391 | translate(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
392 | translate(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
393 | translate(SSH2_MSG_CHANNEL_DATA); |
394 | translate(SSH2_MSG_CHANNEL_EXTENDED_DATA); |
395 | translate(SSH2_MSG_CHANNEL_EOF); |
396 | translate(SSH2_MSG_CHANNEL_CLOSE); |
397 | translate(SSH2_MSG_CHANNEL_REQUEST); |
398 | translate(SSH2_MSG_CHANNEL_SUCCESS); |
399 | translate(SSH2_MSG_CHANNEL_FAILURE); |
400 | return "unknown"; |
401 | } |
402 | #undef translate |
403 | #undef translatec |
7d503c31 |
404 | |
9a10ecf4 |
405 | /* Enumeration values for fields in SSH-1 packets */ |
406 | enum { |
407 | PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM, |
408 | /* These values are for communicating relevant semantics of |
409 | * fields to the packet logging code. */ |
410 | PKTT_OTHER, PKTT_PASSWORD, PKTT_DATA |
411 | }; |
972a41c8 |
412 | |
acddebd9 |
413 | /* |
414 | * Coroutine mechanics for the sillier bits of the code. If these |
415 | * macros look impenetrable to you, you might find it helpful to |
416 | * read |
417 | * |
418 | * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html |
419 | * |
420 | * which explains the theory behind these macros. |
b624d1e6 |
421 | * |
422 | * In particular, if you are getting `case expression not constant' |
423 | * errors when building with MS Visual Studio, this is because MS's |
424 | * Edit and Continue debugging feature causes their compiler to |
425 | * violate ANSI C. To disable Edit and Continue debugging: |
426 | * |
427 | * - right-click ssh.c in the FileView |
428 | * - click Settings |
429 | * - select the C/C++ tab and the General category |
430 | * - under `Debug info:', select anything _other_ than `Program |
431 | * Database for Edit and Continue'. |
acddebd9 |
432 | */ |
51470298 |
433 | #define crBegin(v) { int *crLine = &v; switch(v) { case 0:; |
30919997 |
434 | #define crBeginState crBegin(s->crLine) |
435 | #define crStateP(t, v) \ |
436 | struct t *s; \ |
437 | if (!(v)) { s = (v) = snew(struct t); s->crLine = 0; } \ |
438 | s = (v); |
439 | #define crState(t) crStateP(t, ssh->t) |
51470298 |
440 | #define crFinish(z) } *crLine = 0; return (z); } |
441 | #define crFinishV } *crLine = 0; return; } |
0d201bfb |
442 | #define crFinishFree(z) } sfree(s); return (z); } |
443 | #define crFinishFreeV } sfree(s); return; } |
374330e2 |
444 | #define crReturn(z) \ |
445 | do {\ |
51470298 |
446 | *crLine =__LINE__; return (z); case __LINE__:;\ |
374330e2 |
447 | } while (0) |
448 | #define crReturnV \ |
449 | do {\ |
51470298 |
450 | *crLine=__LINE__; return; case __LINE__:;\ |
374330e2 |
451 | } while (0) |
51470298 |
452 | #define crStop(z) do{ *crLine = 0; return (z); }while(0) |
453 | #define crStopV do{ *crLine = 0; return; }while(0) |
fb09bf1c |
454 | #define crWaitUntil(c) do { crReturn(0); } while (!(c)) |
7cca0d81 |
455 | #define crWaitUntilV(c) do { crReturnV; } while (!(c)) |
374330e2 |
456 | |
51470298 |
457 | typedef struct ssh_tag *Ssh; |
ff3187f6 |
458 | struct Packet; |
459 | |
dacd8872 |
460 | static struct Packet *ssh1_pkt_init(int pkt_type); |
ff3187f6 |
461 | static struct Packet *ssh2_pkt_init(int pkt_type); |
dacd8872 |
462 | static void ssh_pkt_ensure(struct Packet *, int length); |
463 | static void ssh_pkt_adddata(struct Packet *, void *data, int len); |
464 | static void ssh_pkt_addbyte(struct Packet *, unsigned char value); |
ff3187f6 |
465 | static void ssh2_pkt_addbool(struct Packet *, unsigned char value); |
dacd8872 |
466 | static void ssh_pkt_adduint32(struct Packet *, unsigned long value); |
467 | static void ssh_pkt_addstring_start(struct Packet *); |
468 | static void ssh_pkt_addstring_str(struct Packet *, char *data); |
469 | static void ssh_pkt_addstring_data(struct Packet *, char *data, int len); |
470 | static void ssh_pkt_addstring(struct Packet *, char *data); |
d8baa528 |
471 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len); |
dacd8872 |
472 | static void ssh1_pkt_addmp(struct Packet *, Bignum b); |
ff3187f6 |
473 | static void ssh2_pkt_addmp(struct Packet *, Bignum b); |
474 | static int ssh2_pkt_construct(Ssh, struct Packet *); |
475 | static void ssh2_pkt_send(Ssh, struct Packet *); |
590f6a5f |
476 | static void ssh2_pkt_send_noqueue(Ssh, struct Packet *); |
ff3187f6 |
477 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, |
478 | struct Packet *pktin); |
479 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, |
480 | struct Packet *pktin); |
997097db |
481 | static void ssh2_channel_check_close(struct ssh_channel *c); |
bc06669b |
482 | static void ssh_channel_destroy(struct ssh_channel *c); |
3d63ca2e |
483 | |
5471d09a |
484 | /* |
485 | * Buffer management constants. There are several of these for |
486 | * various different purposes: |
487 | * |
488 | * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up |
489 | * on a local data stream before we throttle the whole SSH |
2e85c969 |
490 | * connection (in SSH-1 only). Throttling the whole connection is |
5471d09a |
491 | * pretty drastic so we set this high in the hope it won't |
492 | * happen very often. |
493 | * |
494 | * - SSH_MAX_BACKLOG is the amount of backlog that must build up |
495 | * on the SSH connection itself before we defensively throttle |
496 | * _all_ local data streams. This is pretty drastic too (though |
2e85c969 |
497 | * thankfully unlikely in SSH-2 since the window mechanism should |
5471d09a |
498 | * ensure that the server never has any need to throttle its end |
499 | * of the connection), so we set this high as well. |
500 | * |
2e85c969 |
501 | * - OUR_V2_WINSIZE is the maximum window size we present on SSH-2 |
5471d09a |
502 | * channels. |
9b53e9b8 |
503 | * |
504 | * - OUR_V2_BIGWIN is the window size we advertise for the only |
3361b80a |
505 | * channel in a simple connection. It must be <= INT_MAX. |
9916cc1e |
506 | * |
507 | * - OUR_V2_MAXPKT is the official "maximum packet size" we send |
508 | * to the remote side. This actually has nothing to do with the |
509 | * size of the _packet_, but is instead a limit on the amount |
510 | * of data we're willing to receive in a single SSH2 channel |
511 | * data message. |
512 | * |
513 | * - OUR_V2_PACKETLIMIT is actually the maximum size of SSH |
514 | * _packet_ we're prepared to cope with. It must be a multiple |
515 | * of the cipher block size, and must be at least 35000. |
5471d09a |
516 | */ |
517 | |
518 | #define SSH1_BUFFER_LIMIT 32768 |
519 | #define SSH_MAX_BACKLOG 32768 |
520 | #define OUR_V2_WINSIZE 16384 |
3361b80a |
521 | #define OUR_V2_BIGWIN 0x7fffffff |
954d5c5a |
522 | #define OUR_V2_MAXPKT 0x4000UL |
9916cc1e |
523 | #define OUR_V2_PACKETLIMIT 0x9000UL |
d74d141c |
524 | |
85cc02bb |
525 | const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss }; |
e5574168 |
526 | |
8b2715b2 |
527 | const static struct ssh_mac *macs[] = { |
23159902 |
528 | &ssh_hmac_sha256, &ssh_hmac_sha1, &ssh_hmac_sha1_96, &ssh_hmac_md5 |
32874aea |
529 | }; |
8b2715b2 |
530 | const static struct ssh_mac *buggymacs[] = { |
6668a75e |
531 | &ssh_hmac_sha1_buggy, &ssh_hmac_sha1_96_buggy, &ssh_hmac_md5 |
32874aea |
532 | }; |
e5574168 |
533 | |
5366aed8 |
534 | static void *ssh_comp_none_init(void) |
535 | { |
536 | return NULL; |
537 | } |
538 | static void ssh_comp_none_cleanup(void *handle) |
32874aea |
539 | { |
540 | } |
5366aed8 |
541 | static int ssh_comp_none_block(void *handle, unsigned char *block, int len, |
32874aea |
542 | unsigned char **outblock, int *outlen) |
543 | { |
544 | return 0; |
545 | } |
5366aed8 |
546 | static int ssh_comp_none_disable(void *handle) |
32874aea |
547 | { |
4ba9b64b |
548 | return 0; |
549 | } |
57476f6b |
550 | const static struct ssh_compress ssh_comp_none = { |
ab18ccff |
551 | "none", NULL, |
5366aed8 |
552 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
553 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
554 | ssh_comp_none_disable, NULL |
e5574168 |
555 | }; |
4ba9b64b |
556 | extern const struct ssh_compress ssh_zlib; |
557 | const static struct ssh_compress *compressions[] = { |
32874aea |
558 | &ssh_zlib, &ssh_comp_none |
559 | }; |
374330e2 |
560 | |
32874aea |
561 | enum { /* channel types */ |
783415f8 |
562 | CHAN_MAINSESSION, |
563 | CHAN_X11, |
564 | CHAN_AGENT, |
bc240b21 |
565 | CHAN_SOCKDATA, |
c6b41791 |
566 | CHAN_SOCKDATA_DORMANT, /* one the remote hasn't confirmed */ |
567 | /* |
568 | * CHAN_ZOMBIE is used to indicate a channel for which we've |
569 | * already destroyed the local data source: for instance, if a |
570 | * forwarded port experiences a socket error on the local side, we |
571 | * immediately destroy its local socket and turn the SSH channel |
572 | * into CHAN_ZOMBIE. |
573 | */ |
574 | CHAN_ZOMBIE |
783415f8 |
575 | }; |
576 | |
39d830e4 |
577 | typedef void (*handler_fn_t)(Ssh ssh, struct Packet *pktin); |
578 | typedef void (*chandler_fn_t)(Ssh ssh, struct Packet *pktin, void *ctx); |
579 | typedef void (*cchandler_fn_t)(struct ssh_channel *, struct Packet *, void *); |
580 | |
581 | /* |
582 | * Each channel has a queue of outstanding CHANNEL_REQUESTS and their |
583 | * handlers. |
584 | */ |
585 | struct outstanding_channel_request { |
586 | cchandler_fn_t handler; |
587 | void *ctx; |
588 | struct outstanding_channel_request *next; |
589 | }; |
590 | |
dacbd0e8 |
591 | /* |
592 | * 2-3-4 tree storing channels. |
593 | */ |
594 | struct ssh_channel { |
51470298 |
595 | Ssh ssh; /* pointer back to main context */ |
d211621f |
596 | unsigned remoteid, localid; |
dacbd0e8 |
597 | int type; |
64d6ff88 |
598 | /* True if we opened this channel but server hasn't confirmed. */ |
599 | int halfopen; |
0357890f |
600 | /* |
2e85c969 |
601 | * In SSH-1, this value contains four bits: |
0357890f |
602 | * |
603 | * 1 We have sent SSH1_MSG_CHANNEL_CLOSE. |
604 | * 2 We have sent SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
605 | * 4 We have received SSH1_MSG_CHANNEL_CLOSE. |
606 | * 8 We have received SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
607 | * |
608 | * A channel is completely finished with when all four bits are set. |
bc06669b |
609 | * |
610 | * In SSH-2, the four bits mean: |
611 | * |
612 | * 1 We have sent SSH2_MSG_CHANNEL_EOF. |
613 | * 2 We have sent SSH2_MSG_CHANNEL_CLOSE. |
614 | * 4 We have received SSH2_MSG_CHANNEL_EOF. |
615 | * 8 We have received SSH2_MSG_CHANNEL_CLOSE. |
616 | * |
617 | * A channel is completely finished with when we have both sent |
618 | * and received CLOSE. |
619 | * |
620 | * The symbolic constants below use the SSH-2 terminology, which |
621 | * is a bit confusing in SSH-1, but we have to use _something_. |
0357890f |
622 | */ |
bc06669b |
623 | #define CLOSES_SENT_EOF 1 |
624 | #define CLOSES_SENT_CLOSE 2 |
625 | #define CLOSES_RCVD_EOF 4 |
626 | #define CLOSES_RCVD_CLOSE 8 |
dacbd0e8 |
627 | int closes; |
78280721 |
628 | |
629 | /* |
bc06669b |
630 | * This flag indicates that an EOF is pending on the outgoing side |
631 | * of the channel: that is, wherever we're getting the data for |
632 | * this channel has sent us some data followed by EOF. We can't |
633 | * actually send the EOF until we've finished sending the data, so |
634 | * we set this flag instead to remind us to do so once our buffer |
635 | * is clear. |
78280721 |
636 | */ |
bc06669b |
637 | int pending_eof; |
78280721 |
638 | |
ff68564b |
639 | /* |
640 | * True if this channel is causing the underlying connection to be |
641 | * throttled. |
642 | */ |
643 | int throttling_conn; |
5471d09a |
644 | union { |
5471d09a |
645 | struct ssh2_data_channel { |
646 | bufchain outbuffer; |
647 | unsigned remwindow, remmaxpkt; |
467e064d |
648 | /* locwindow is signed so we can cope with excess data. */ |
9b53e9b8 |
649 | int locwindow, locmaxwin; |
0e443c99 |
650 | /* |
651 | * remlocwin is the amount of local window that we think |
652 | * the remote end had available to it after it sent the |
653 | * last data packet or window adjust ack. |
654 | */ |
655 | int remlocwin; |
656 | /* |
39d830e4 |
657 | * These store the list of channel requests that haven't |
0e443c99 |
658 | * been acked. |
659 | */ |
39d830e4 |
660 | struct outstanding_channel_request *chanreq_head, *chanreq_tail; |
0e443c99 |
661 | enum { THROTTLED, UNTHROTTLING, UNTHROTTLED } throttle_state; |
5471d09a |
662 | } v2; |
663 | } v; |
dacbd0e8 |
664 | union { |
32874aea |
665 | struct ssh_agent_channel { |
666 | unsigned char *message; |
667 | unsigned char msglen[4]; |
a03c9f9c |
668 | unsigned lensofar, totallen; |
be3f980d |
669 | int outstanding_requests; |
32874aea |
670 | } a; |
671 | struct ssh_x11_channel { |
672 | Socket s; |
673 | } x11; |
d74d141c |
674 | struct ssh_pfd_channel { |
675 | Socket s; |
676 | } pfd; |
dacbd0e8 |
677 | } u; |
678 | }; |
57476f6b |
679 | |
d74d141c |
680 | /* |
2e85c969 |
681 | * 2-3-4 tree storing remote->local port forwardings. SSH-1 and SSH-2 |
682 | * use this structure in different ways, reflecting SSH-2's |
bc240b21 |
683 | * altogether saner approach to port forwarding. |
684 | * |
2e85c969 |
685 | * In SSH-1, you arrange a remote forwarding by sending the server |
bc240b21 |
686 | * the remote port number, and the local destination host:port. |
687 | * When a connection comes in, the server sends you back that |
688 | * host:port pair, and you connect to it. This is a ready-made |
689 | * security hole if you're not on the ball: a malicious server |
690 | * could send you back _any_ host:port pair, so if you trustingly |
691 | * connect to the address it gives you then you've just opened the |
692 | * entire inside of your corporate network just by connecting |
693 | * through it to a dodgy SSH server. Hence, we must store a list of |
694 | * host:port pairs we _are_ trying to forward to, and reject a |
695 | * connection request from the server if it's not in the list. |
696 | * |
2e85c969 |
697 | * In SSH-2, each side of the connection minds its own business and |
bc240b21 |
698 | * doesn't send unnecessary information to the other. You arrange a |
699 | * remote forwarding by sending the server just the remote port |
700 | * number. When a connection comes in, the server tells you which |
701 | * of its ports was connected to; and _you_ have to remember what |
702 | * local host:port pair went with that port number. |
703 | * |
2e85c969 |
704 | * Hence, in SSH-1 this structure is indexed by destination |
705 | * host:port pair, whereas in SSH-2 it is indexed by source port. |
d74d141c |
706 | */ |
fda2feb1 |
707 | struct ssh_portfwd; /* forward declaration */ |
708 | |
d74d141c |
709 | struct ssh_rportfwd { |
bc240b21 |
710 | unsigned sport, dport; |
711 | char dhost[256]; |
06fadff5 |
712 | char *sportdesc; |
fda2feb1 |
713 | struct ssh_portfwd *pfrec; |
d74d141c |
714 | }; |
fda2feb1 |
715 | #define free_rportfwd(pf) ( \ |
716 | ((pf) ? (sfree((pf)->sportdesc)) : (void)0 ), sfree(pf) ) |
717 | |
718 | /* |
719 | * Separately to the rportfwd tree (which is for looking up port |
720 | * open requests from the server), a tree of _these_ structures is |
721 | * used to keep track of all the currently open port forwardings, |
722 | * so that we can reconfigure in mid-session if the user requests |
723 | * it. |
724 | */ |
725 | struct ssh_portfwd { |
84328ddb |
726 | enum { DESTROY, KEEP, CREATE } status; |
fda2feb1 |
727 | int type; |
728 | unsigned sport, dport; |
729 | char *saddr, *daddr; |
3fe92132 |
730 | char *sserv, *dserv; |
fda2feb1 |
731 | struct ssh_rportfwd *remote; |
05581745 |
732 | int addressfamily; |
fda2feb1 |
733 | void *local; |
734 | }; |
735 | #define free_portfwd(pf) ( \ |
3fe92132 |
736 | ((pf) ? (sfree((pf)->saddr), sfree((pf)->daddr), \ |
737 | sfree((pf)->sserv), sfree((pf)->dserv)) : (void)0 ), sfree(pf) ) |
d74d141c |
738 | |
57476f6b |
739 | struct Packet { |
dacd8872 |
740 | long length; /* length of `data' actually used */ |
741 | long forcepad; /* SSH-2: force padding to at least this length */ |
742 | int type; /* only used for incoming packets */ |
743 | unsigned long sequence; /* SSH-2 incoming sequence number */ |
744 | unsigned char *data; /* allocated storage */ |
745 | unsigned char *body; /* offset of payload within `data' */ |
746 | long savedpos; /* temporary index into `data' (for strings) */ |
747 | long maxlen; /* amount of storage allocated for `data' */ |
748 | long encrypted_len; /* for SSH-2 total-size counting */ |
ff3187f6 |
749 | |
750 | /* |
751 | * State associated with packet logging |
752 | */ |
753 | int logmode; |
754 | int nblanks; |
755 | struct logblank_t *blanks; |
57476f6b |
756 | }; |
757 | |
1c1a7262 |
758 | static void ssh1_protocol(Ssh ssh, void *vin, int inlen, |
ff3187f6 |
759 | struct Packet *pktin); |
1c1a7262 |
760 | static void ssh2_protocol(Ssh ssh, void *vin, int inlen, |
ff3187f6 |
761 | struct Packet *pktin); |
b09eaa88 |
762 | static void ssh1_protocol_setup(Ssh ssh); |
763 | static void ssh2_protocol_setup(Ssh ssh); |
51470298 |
764 | static void ssh_size(void *handle, int width, int height); |
765 | static void ssh_special(void *handle, Telnet_Special); |
5471d09a |
766 | static int ssh2_try_send(struct ssh_channel *c); |
51470298 |
767 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len); |
768 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize); |
9fca3f8c |
769 | static void ssh2_set_window(struct ssh_channel *c, int newwin); |
51470298 |
770 | static int ssh_sendbuffer(void *handle); |
ac934965 |
771 | static int ssh_do_close(Ssh ssh, int notify_exit); |
ff3187f6 |
772 | static unsigned long ssh_pkt_getuint32(struct Packet *pkt); |
773 | static int ssh2_pkt_getbool(struct Packet *pkt); |
774 | static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length); |
d719927e |
775 | static void ssh2_timer(void *ctx, unsigned long now); |
0b106f61 |
776 | static void do_ssh2_transport(Ssh ssh, void *vin, int inlen, |
777 | struct Packet *pktin); |
39d830e4 |
778 | static void ssh2_msg_unexpected(Ssh ssh, struct Packet *pktin); |
57476f6b |
779 | |
51470298 |
780 | struct rdpkt1_state_tag { |
57476f6b |
781 | long len, pad, biglen, to_read; |
782 | unsigned long realcrc, gotcrc; |
783 | unsigned char *p; |
784 | int i; |
785 | int chunk; |
ff3187f6 |
786 | struct Packet *pktin; |
51470298 |
787 | }; |
57476f6b |
788 | |
51470298 |
789 | struct rdpkt2_state_tag { |
960e736a |
790 | long len, pad, payload, packetlen, maclen; |
791 | int i; |
792 | int cipherblk; |
793 | unsigned long incoming_sequence; |
ff3187f6 |
794 | struct Packet *pktin; |
51470298 |
795 | }; |
796 | |
06fadff5 |
797 | struct queued_handler; |
798 | struct queued_handler { |
799 | int msg1, msg2; |
800 | chandler_fn_t handler; |
801 | void *ctx; |
802 | struct queued_handler *next; |
803 | }; |
b09eaa88 |
804 | |
51470298 |
805 | struct ssh_tag { |
806 | const struct plug_function_table *fn; |
807 | /* the above field _must_ be first in the structure */ |
808 | |
4320baf7 |
809 | char *v_c, *v_s; |
b672f405 |
810 | void *exhash; |
51470298 |
811 | |
812 | Socket s; |
813 | |
b9d7bcad |
814 | void *ldisc; |
a8327734 |
815 | void *logctx; |
b9d7bcad |
816 | |
51470298 |
817 | unsigned char session_key[32]; |
818 | int v1_compressing; |
819 | int v1_remote_protoflags; |
820 | int v1_local_protoflags; |
821 | int agentfwd_enabled; |
822 | int X11_fwd_enabled; |
823 | int remote_bugs; |
824 | const struct ssh_cipher *cipher; |
371e569c |
825 | void *v1_cipher_ctx; |
0183b242 |
826 | void *crcda_ctx; |
51470298 |
827 | const struct ssh2_cipher *cscipher, *sccipher; |
371e569c |
828 | void *cs_cipher_ctx, *sc_cipher_ctx; |
51470298 |
829 | const struct ssh_mac *csmac, *scmac; |
e0e1a00d |
830 | void *cs_mac_ctx, *sc_mac_ctx; |
51470298 |
831 | const struct ssh_compress *cscomp, *sccomp; |
5366aed8 |
832 | void *cs_comp_ctx, *sc_comp_ctx; |
51470298 |
833 | const struct ssh_kex *kex; |
834 | const struct ssh_signkey *hostkey; |
754c0df9 |
835 | unsigned char v2_session_id[SSH2_KEX_MAX_HASH_LEN]; |
b672f405 |
836 | int v2_session_id_len; |
27cd7fc2 |
837 | void *kex_ctx; |
51470298 |
838 | |
839 | char *savedhost; |
840 | int savedport; |
841 | int send_ok; |
842 | int echoing, editing; |
843 | |
844 | void *frontend; |
845 | |
db219738 |
846 | int ospeed, ispeed; /* temporaries */ |
51470298 |
847 | int term_width, term_height; |
848 | |
849 | tree234 *channels; /* indexed by local id */ |
850 | struct ssh_channel *mainchan; /* primary session channel */ |
feb02b4e |
851 | int ncmode; /* is primary channel direct-tcpip? */ |
51470298 |
852 | int exitcode; |
ac934965 |
853 | int close_expected; |
9e296bfa |
854 | int clean_exit; |
51470298 |
855 | |
fda2feb1 |
856 | tree234 *rportfwds, *portfwds; |
51470298 |
857 | |
858 | enum { |
859 | SSH_STATE_PREPACKET, |
860 | SSH_STATE_BEFORE_SIZE, |
861 | SSH_STATE_INTERMED, |
862 | SSH_STATE_SESSION, |
863 | SSH_STATE_CLOSED |
864 | } state; |
865 | |
866 | int size_needed, eof_needed; |
bc06669b |
867 | int sent_console_eof; |
4a202fc6 |
868 | int got_pty; /* affects EOF behaviour on main channel */ |
51470298 |
869 | |
590f6a5f |
870 | struct Packet **queue; |
871 | int queuelen, queuesize; |
872 | int queueing; |
51470298 |
873 | unsigned char *deferred_send_data; |
874 | int deferred_len, deferred_size; |
875 | |
876 | /* |
877 | * Gross hack: pscp will try to start SFTP but fall back to |
878 | * scp1 if that fails. This variable is the means by which |
879 | * scp.c can reach into the SSH code and find out which one it |
880 | * got. |
881 | */ |
882 | int fallback_cmd; |
883 | |
6bbce591 |
884 | bufchain banner; /* accumulates banners during do_ssh2_authconn */ |
51470298 |
885 | |
acab36bc |
886 | Pkt_KCtx pkt_kctx; |
887 | Pkt_ACtx pkt_actx; |
51470298 |
888 | |
8def70c3 |
889 | struct X11Display *x11disp; |
302121de |
890 | |
51470298 |
891 | int version; |
ff68564b |
892 | int conn_throttle_count; |
51470298 |
893 | int overall_bufsize; |
894 | int throttled_all; |
895 | int v1_stdout_throttling; |
a8756193 |
896 | unsigned long v2_outgoing_sequence; |
51470298 |
897 | |
898 | int ssh1_rdpkt_crstate; |
899 | int ssh2_rdpkt_crstate; |
51470298 |
900 | int ssh_gotdata_crstate; |
b09eaa88 |
901 | int do_ssh1_connection_crstate; |
51470298 |
902 | |
903 | void *do_ssh_init_state; |
904 | void *do_ssh1_login_state; |
905 | void *do_ssh2_transport_state; |
906 | void *do_ssh2_authconn_state; |
907 | |
908 | struct rdpkt1_state_tag rdpkt1_state; |
909 | struct rdpkt2_state_tag rdpkt2_state; |
910 | |
2e85c969 |
911 | /* SSH-1 and SSH-2 use this for different things, but both use it */ |
b09eaa88 |
912 | int protocol_initial_phase_done; |
913 | |
1c1a7262 |
914 | void (*protocol) (Ssh ssh, void *vin, int inlen, |
ff3187f6 |
915 | struct Packet *pkt); |
916 | struct Packet *(*s_rdpkt) (Ssh ssh, unsigned char **data, int *datalen); |
86916870 |
917 | |
918 | /* |
4a693cfc |
919 | * We maintain our own copy of a Conf structure here. That way, |
920 | * when we're passed a new one for reconfiguration, we can check |
921 | * the differences and potentially reconfigure port forwardings |
922 | * etc in mid-session. |
86916870 |
923 | */ |
4a693cfc |
924 | Conf *conf; |
925 | |
926 | /* |
927 | * Values cached out of conf so as to avoid the tree234 lookup |
928 | * cost every time they're used. |
929 | */ |
930 | int logomitdata; |
931 | |
932 | /* |
933 | * Dynamically allocated username string created during SSH |
934 | * login. Stored in here rather than in the coroutine state so |
935 | * that it'll be reliably freed if we shut down the SSH session |
936 | * at some unexpected moment. |
937 | */ |
938 | char *username; |
839f10db |
939 | |
940 | /* |
3d9449a1 |
941 | * Used to transfer data back from async callbacks. |
839f10db |
942 | */ |
943 | void *agent_response; |
944 | int agent_response_len; |
3d9449a1 |
945 | int user_response; |
946 | |
947 | /* |
948 | * The SSH connection can be set as `frozen', meaning we are |
949 | * not currently accepting incoming data from the network. This |
950 | * is slightly more serious than setting the _socket_ as |
951 | * frozen, because we may already have had data passed to us |
952 | * from the network which we need to delay processing until |
953 | * after the freeze is lifted, so we also need a bufchain to |
954 | * store that data. |
955 | */ |
956 | int frozen; |
957 | bufchain queued_incoming_data; |
b09eaa88 |
958 | |
959 | /* |
960 | * Dispatch table for packet types that we may have to deal |
961 | * with at any time. |
962 | */ |
963 | handler_fn_t packet_dispatch[256]; |
39934deb |
964 | |
965 | /* |
06fadff5 |
966 | * Queues of one-off handler functions for success/failure |
967 | * indications from a request. |
968 | */ |
969 | struct queued_handler *qhead, *qtail; |
bc074147 |
970 | handler_fn_t q_saved_handler1, q_saved_handler2; |
06fadff5 |
971 | |
972 | /* |
39934deb |
973 | * This module deals with sending keepalives. |
974 | */ |
975 | Pinger pinger; |
9442dd57 |
976 | |
977 | /* |
978 | * Track incoming and outgoing data sizes and time, for |
979 | * size-based rekeys. |
980 | */ |
981 | unsigned long incoming_data_size, outgoing_data_size, deferred_data_size; |
d57f70af |
982 | unsigned long max_data_size; |
9442dd57 |
983 | int kex_in_progress; |
d719927e |
984 | unsigned long next_rekey, last_rekey; |
e13bba36 |
985 | char *deferred_rekey_reason; /* points to STATIC string; don't free */ |
42af6a67 |
986 | |
987 | /* |
988 | * Fully qualified host name, which we need if doing GSSAPI. |
989 | */ |
990 | char *fullhostname; |
1e00c92b |
991 | |
992 | #ifndef NO_GSSAPI |
993 | /* |
994 | * GSSAPI libraries for this session. |
995 | */ |
996 | struct ssh_gss_liblist *gsslibs; |
997 | #endif |
51470298 |
998 | }; |
960e736a |
999 | |
382908ad |
1000 | #define logevent(s) logevent(ssh->frontend, s) |
a8327734 |
1001 | |
1002 | /* logevent, only printf-formatted. */ |
cbe2d68f |
1003 | static void logeventf(Ssh ssh, const char *fmt, ...) |
a8327734 |
1004 | { |
1005 | va_list ap; |
57356d63 |
1006 | char *buf; |
a8327734 |
1007 | |
1008 | va_start(ap, fmt); |
57356d63 |
1009 | buf = dupvprintf(fmt, ap); |
a8327734 |
1010 | va_end(ap); |
57356d63 |
1011 | logevent(buf); |
57356d63 |
1012 | sfree(buf); |
a8327734 |
1013 | } |
1014 | |
3976feca |
1015 | static void bomb_out(Ssh ssh, char *text) |
1016 | { |
1017 | ssh_do_close(ssh, FALSE); |
1018 | logevent(text); |
1019 | connection_fatal(ssh->frontend, "%s", text); |
1020 | sfree(text); |
1021 | } |
1022 | |
1023 | #define bombout(msg) bomb_out(ssh, dupprintf msg) |
a8327734 |
1024 | |
9a10ecf4 |
1025 | /* Functions to leave bits out of the SSH packet log file. */ |
1026 | |
ff3187f6 |
1027 | static void dont_log_password(Ssh ssh, struct Packet *pkt, int blanktype) |
9a10ecf4 |
1028 | { |
4a693cfc |
1029 | if (conf_get_int(ssh->conf, CONF_logomitpass)) |
ff3187f6 |
1030 | pkt->logmode = blanktype; |
9a10ecf4 |
1031 | } |
1032 | |
ff3187f6 |
1033 | static void dont_log_data(Ssh ssh, struct Packet *pkt, int blanktype) |
9a10ecf4 |
1034 | { |
4a693cfc |
1035 | if (ssh->logomitdata) |
ff3187f6 |
1036 | pkt->logmode = blanktype; |
9a10ecf4 |
1037 | } |
1038 | |
ff3187f6 |
1039 | static void end_log_omission(Ssh ssh, struct Packet *pkt) |
9a10ecf4 |
1040 | { |
ff3187f6 |
1041 | pkt->logmode = PKTLOG_EMIT; |
9a10ecf4 |
1042 | } |
1043 | |
4a693cfc |
1044 | /* Helper function for common bits of parsing ttymodes. */ |
1045 | static void parse_ttymodes(Ssh ssh, |
c6ccd5c2 |
1046 | void (*do_mode)(void *data, char *mode, char *val), |
1047 | void *data) |
1048 | { |
4a693cfc |
1049 | char *key, *val; |
1050 | |
1051 | for (val = conf_get_str_strs(ssh->conf, CONF_ttymodes, NULL, &key); |
1052 | val != NULL; |
1053 | val = conf_get_str_strs(ssh->conf, CONF_ttymodes, key, &key)) { |
1054 | /* |
1055 | * val[0] is either 'V', indicating that an explicit value |
1056 | * follows it, or 'A' indicating that we should pass the |
1057 | * value through from the local environment via get_ttymode. |
1058 | */ |
ab7a09e5 |
1059 | if (val[0] == 'A') { |
4a693cfc |
1060 | val = get_ttymode(ssh->frontend, key); |
ab7a09e5 |
1061 | if (val) { |
1062 | do_mode(data, key, val); |
1063 | sfree(val); |
1064 | } |
1065 | } else |
1066 | do_mode(data, key, val + 1); /* skip the 'V' */ |
c6ccd5c2 |
1067 | } |
1068 | } |
1069 | |
32874aea |
1070 | static int ssh_channelcmp(void *av, void *bv) |
1071 | { |
1072 | struct ssh_channel *a = (struct ssh_channel *) av; |
1073 | struct ssh_channel *b = (struct ssh_channel *) bv; |
1074 | if (a->localid < b->localid) |
1075 | return -1; |
1076 | if (a->localid > b->localid) |
1077 | return +1; |
dacbd0e8 |
1078 | return 0; |
1079 | } |
32874aea |
1080 | static int ssh_channelfind(void *av, void *bv) |
1081 | { |
1082 | unsigned *a = (unsigned *) av; |
1083 | struct ssh_channel *b = (struct ssh_channel *) bv; |
1084 | if (*a < b->localid) |
1085 | return -1; |
1086 | if (*a > b->localid) |
1087 | return +1; |
dacbd0e8 |
1088 | return 0; |
1089 | } |
1090 | |
bc240b21 |
1091 | static int ssh_rportcmp_ssh1(void *av, void *bv) |
d74d141c |
1092 | { |
1093 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
1094 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
1095 | int i; |
bc240b21 |
1096 | if ( (i = strcmp(a->dhost, b->dhost)) != 0) |
d74d141c |
1097 | return i < 0 ? -1 : +1; |
bc240b21 |
1098 | if (a->dport > b->dport) |
1099 | return +1; |
1100 | if (a->dport < b->dport) |
1101 | return -1; |
1102 | return 0; |
1103 | } |
1104 | |
1105 | static int ssh_rportcmp_ssh2(void *av, void *bv) |
1106 | { |
1107 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
1108 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
cdcbdf3b |
1109 | |
bc240b21 |
1110 | if (a->sport > b->sport) |
d74d141c |
1111 | return +1; |
bc240b21 |
1112 | if (a->sport < b->sport) |
1113 | return -1; |
d74d141c |
1114 | return 0; |
1115 | } |
1116 | |
fda2feb1 |
1117 | /* |
1118 | * Special form of strcmp which can cope with NULL inputs. NULL is |
1119 | * defined to sort before even the empty string. |
1120 | */ |
1121 | static int nullstrcmp(const char *a, const char *b) |
1122 | { |
1123 | if (a == NULL && b == NULL) |
1124 | return 0; |
1125 | if (a == NULL) |
1126 | return -1; |
1127 | if (b == NULL) |
1128 | return +1; |
1129 | return strcmp(a, b); |
1130 | } |
1131 | |
1132 | static int ssh_portcmp(void *av, void *bv) |
1133 | { |
1134 | struct ssh_portfwd *a = (struct ssh_portfwd *) av; |
1135 | struct ssh_portfwd *b = (struct ssh_portfwd *) bv; |
1136 | int i; |
1137 | if (a->type > b->type) |
1138 | return +1; |
1139 | if (a->type < b->type) |
1140 | return -1; |
84328ddb |
1141 | if (a->addressfamily > b->addressfamily) |
1142 | return +1; |
1143 | if (a->addressfamily < b->addressfamily) |
1144 | return -1; |
fda2feb1 |
1145 | if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0) |
1146 | return i < 0 ? -1 : +1; |
1147 | if (a->sport > b->sport) |
1148 | return +1; |
1149 | if (a->sport < b->sport) |
1150 | return -1; |
1151 | if (a->type != 'D') { |
1152 | if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0) |
1153 | return i < 0 ? -1 : +1; |
1154 | if (a->dport > b->dport) |
1155 | return +1; |
1156 | if (a->dport < b->dport) |
1157 | return -1; |
1158 | } |
1159 | return 0; |
1160 | } |
1161 | |
51470298 |
1162 | static int alloc_channel_id(Ssh ssh) |
32874aea |
1163 | { |
260f3dec |
1164 | const unsigned CHANNEL_NUMBER_OFFSET = 256; |
1165 | unsigned low, high, mid; |
d2371c81 |
1166 | int tsize; |
1167 | struct ssh_channel *c; |
1168 | |
1169 | /* |
1170 | * First-fit allocation of channel numbers: always pick the |
1171 | * lowest unused one. To do this, binary-search using the |
1172 | * counted B-tree to find the largest channel ID which is in a |
1173 | * contiguous sequence from the beginning. (Precisely |
1174 | * everything in that sequence must have ID equal to its tree |
1175 | * index plus CHANNEL_NUMBER_OFFSET.) |
1176 | */ |
51470298 |
1177 | tsize = count234(ssh->channels); |
d2371c81 |
1178 | |
32874aea |
1179 | low = -1; |
1180 | high = tsize; |
d2371c81 |
1181 | while (high - low > 1) { |
1182 | mid = (high + low) / 2; |
51470298 |
1183 | c = index234(ssh->channels, mid); |
d2371c81 |
1184 | if (c->localid == mid + CHANNEL_NUMBER_OFFSET) |
1185 | low = mid; /* this one is fine */ |
1186 | else |
1187 | high = mid; /* this one is past it */ |
1188 | } |
1189 | /* |
1190 | * Now low points to either -1, or the tree index of the |
1191 | * largest ID in the initial sequence. |
1192 | */ |
1193 | { |
1194 | unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET; |
51470298 |
1195 | assert(NULL == find234(ssh->channels, &i, ssh_channelfind)); |
d2371c81 |
1196 | } |
1197 | return low + 1 + CHANNEL_NUMBER_OFFSET; |
1198 | } |
1199 | |
edd0cb8a |
1200 | static void c_write_stderr(int trusted, const char *buf, int len) |
1201 | { |
1202 | int i; |
1203 | for (i = 0; i < len; i++) |
667deca9 |
1204 | if (buf[i] != '\r' && (trusted || buf[i] == '\n' || (buf[i] & 0x60))) |
edd0cb8a |
1205 | fputc(buf[i], stderr); |
1206 | } |
1207 | |
9fab77dc |
1208 | static void c_write(Ssh ssh, const char *buf, int len) |
32874aea |
1209 | { |
edd0cb8a |
1210 | if (flags & FLAG_STDERR) |
1211 | c_write_stderr(1, buf, len); |
1212 | else |
1213 | from_backend(ssh->frontend, 1, buf, len); |
3bdaf79d |
1214 | } |
1215 | |
9fab77dc |
1216 | static void c_write_untrusted(Ssh ssh, const char *buf, int len) |
32874aea |
1217 | { |
edd0cb8a |
1218 | if (flags & FLAG_STDERR) |
1219 | c_write_stderr(0, buf, len); |
1220 | else |
1221 | from_backend_untrusted(ssh->frontend, buf, len); |
a209e957 |
1222 | } |
1223 | |
9fab77dc |
1224 | static void c_write_str(Ssh ssh, const char *buf) |
32874aea |
1225 | { |
51470298 |
1226 | c_write(ssh, buf, strlen(buf)); |
1408a877 |
1227 | } |
1228 | |
ff3187f6 |
1229 | static void ssh_free_packet(struct Packet *pkt) |
1230 | { |
1231 | sfree(pkt->data); |
1232 | sfree(pkt); |
1233 | } |
1234 | static struct Packet *ssh_new_packet(void) |
1235 | { |
1236 | struct Packet *pkt = snew(struct Packet); |
1237 | |
dacd8872 |
1238 | pkt->body = pkt->data = NULL; |
ff3187f6 |
1239 | pkt->maxlen = 0; |
1240 | pkt->logmode = PKTLOG_EMIT; |
1241 | pkt->nblanks = 0; |
1242 | pkt->blanks = NULL; |
1243 | |
1244 | return pkt; |
1245 | } |
1246 | |
fb09bf1c |
1247 | /* |
1248 | * Collect incoming data in the incoming packet buffer. |
e5574168 |
1249 | * Decipher and verify the packet when it is completely read. |
1250 | * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets. |
fb09bf1c |
1251 | * Update the *data and *datalen variables. |
ff3187f6 |
1252 | * Return a Packet structure when a packet is completed. |
fb09bf1c |
1253 | */ |
ff3187f6 |
1254 | static struct Packet *ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
fb09bf1c |
1255 | { |
51470298 |
1256 | struct rdpkt1_state_tag *st = &ssh->rdpkt1_state; |
374330e2 |
1257 | |
51470298 |
1258 | crBegin(ssh->ssh1_rdpkt_crstate); |
374330e2 |
1259 | |
ff3187f6 |
1260 | st->pktin = ssh_new_packet(); |
1261 | |
1262 | st->pktin->type = 0; |
1263 | st->pktin->length = 0; |
374330e2 |
1264 | |
57476f6b |
1265 | for (st->i = st->len = 0; st->i < 4; st->i++) { |
fb09bf1c |
1266 | while ((*datalen) == 0) |
ff3187f6 |
1267 | crReturn(NULL); |
57476f6b |
1268 | st->len = (st->len << 8) + **data; |
fb09bf1c |
1269 | (*data)++, (*datalen)--; |
1270 | } |
374330e2 |
1271 | |
57476f6b |
1272 | st->pad = 8 - (st->len % 8); |
1273 | st->biglen = st->len + st->pad; |
ff3187f6 |
1274 | st->pktin->length = st->len - 5; |
fb09bf1c |
1275 | |
ae0500e5 |
1276 | if (st->biglen < 0) { |
1277 | bombout(("Extremely large packet length from server suggests" |
1278 | " data stream corruption")); |
ff3187f6 |
1279 | ssh_free_packet(st->pktin); |
1280 | crStop(NULL); |
ae0500e5 |
1281 | } |
1282 | |
ff3187f6 |
1283 | st->pktin->maxlen = st->biglen; |
1284 | st->pktin->data = snewn(st->biglen + APIEXTRA, unsigned char); |
374330e2 |
1285 | |
57476f6b |
1286 | st->to_read = st->biglen; |
ff3187f6 |
1287 | st->p = st->pktin->data; |
57476f6b |
1288 | while (st->to_read > 0) { |
32874aea |
1289 | st->chunk = st->to_read; |
fb09bf1c |
1290 | while ((*datalen) == 0) |
ff3187f6 |
1291 | crReturn(NULL); |
57476f6b |
1292 | if (st->chunk > (*datalen)) |
1293 | st->chunk = (*datalen); |
1294 | memcpy(st->p, *data, st->chunk); |
1295 | *data += st->chunk; |
1296 | *datalen -= st->chunk; |
1297 | st->p += st->chunk; |
1298 | st->to_read -= st->chunk; |
fb09bf1c |
1299 | } |
374330e2 |
1300 | |
ff3187f6 |
1301 | if (ssh->cipher && detect_attack(ssh->crcda_ctx, st->pktin->data, |
0183b242 |
1302 | st->biglen, NULL)) { |
6b5cf8b4 |
1303 | bombout(("Network attack (CRC compensation) detected!")); |
ff3187f6 |
1304 | ssh_free_packet(st->pktin); |
1305 | crStop(NULL); |
9a3a93a5 |
1306 | } |
1307 | |
51470298 |
1308 | if (ssh->cipher) |
ff3187f6 |
1309 | ssh->cipher->decrypt(ssh->v1_cipher_ctx, st->pktin->data, st->biglen); |
374330e2 |
1310 | |
ff3187f6 |
1311 | st->realcrc = crc32_compute(st->pktin->data, st->biglen - 4); |
1312 | st->gotcrc = GET_32BIT(st->pktin->data + st->biglen - 4); |
57476f6b |
1313 | if (st->gotcrc != st->realcrc) { |
6b5cf8b4 |
1314 | bombout(("Incorrect CRC received on packet")); |
ff3187f6 |
1315 | ssh_free_packet(st->pktin); |
1316 | crStop(NULL); |
fb09bf1c |
1317 | } |
572f871e |
1318 | |
ff3187f6 |
1319 | st->pktin->body = st->pktin->data + st->pad + 1; |
1320 | st->pktin->savedpos = 0; |
4ba9b64b |
1321 | |
51470298 |
1322 | if (ssh->v1_compressing) { |
4ba9b64b |
1323 | unsigned char *decompblk; |
1324 | int decomplen; |
36b8d9bb |
1325 | if (!zlib_decompress_block(ssh->sc_comp_ctx, |
ff3187f6 |
1326 | st->pktin->body - 1, st->pktin->length + 1, |
36b8d9bb |
1327 | &decompblk, &decomplen)) { |
1328 | bombout(("Zlib decompression encountered invalid data")); |
ff3187f6 |
1329 | ssh_free_packet(st->pktin); |
1330 | crStop(NULL); |
36b8d9bb |
1331 | } |
4ba9b64b |
1332 | |
ff3187f6 |
1333 | if (st->pktin->maxlen < st->pad + decomplen) { |
1334 | st->pktin->maxlen = st->pad + decomplen; |
1335 | st->pktin->data = sresize(st->pktin->data, |
1336 | st->pktin->maxlen + APIEXTRA, |
3d88e64d |
1337 | unsigned char); |
ff3187f6 |
1338 | st->pktin->body = st->pktin->data + st->pad + 1; |
4ba9b64b |
1339 | } |
1340 | |
ff3187f6 |
1341 | memcpy(st->pktin->body - 1, decompblk, decomplen); |
dcbde236 |
1342 | sfree(decompblk); |
ff3187f6 |
1343 | st->pktin->length = decomplen - 1; |
4ba9b64b |
1344 | } |
1345 | |
ff3187f6 |
1346 | st->pktin->type = st->pktin->body[-1]; |
00db133f |
1347 | |
9a10ecf4 |
1348 | /* |
1349 | * Log incoming packet, possibly omitting sensitive fields. |
1350 | */ |
1351 | if (ssh->logctx) { |
1352 | int nblanks = 0; |
1353 | struct logblank_t blank; |
4a693cfc |
1354 | if (ssh->logomitdata) { |
9a10ecf4 |
1355 | int do_blank = FALSE, blank_prefix = 0; |
1356 | /* "Session data" packets - omit the data field */ |
ff3187f6 |
1357 | if ((st->pktin->type == SSH1_SMSG_STDOUT_DATA) || |
1358 | (st->pktin->type == SSH1_SMSG_STDERR_DATA)) { |
9a10ecf4 |
1359 | do_blank = TRUE; blank_prefix = 4; |
a2724fba |
1360 | } else if (st->pktin->type == SSH1_MSG_CHANNEL_DATA) { |
1361 | do_blank = TRUE; blank_prefix = 8; |
9a10ecf4 |
1362 | } |
1363 | if (do_blank) { |
1364 | blank.offset = blank_prefix; |
ff3187f6 |
1365 | blank.len = st->pktin->length; |
9a10ecf4 |
1366 | blank.type = PKTLOG_OMIT; |
1367 | nblanks = 1; |
1368 | } |
1369 | } |
a8327734 |
1370 | log_packet(ssh->logctx, |
ff3187f6 |
1371 | PKT_INCOMING, st->pktin->type, |
1372 | ssh1_pkt_type(st->pktin->type), |
1373 | st->pktin->body, st->pktin->length, |
0d13bfcd |
1374 | nblanks, &blank, NULL); |
9a10ecf4 |
1375 | } |
00db133f |
1376 | |
ff3187f6 |
1377 | crFinish(st->pktin); |
fb09bf1c |
1378 | } |
1379 | |
ff3187f6 |
1380 | static struct Packet *ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
e5574168 |
1381 | { |
51470298 |
1382 | struct rdpkt2_state_tag *st = &ssh->rdpkt2_state; |
e5574168 |
1383 | |
51470298 |
1384 | crBegin(ssh->ssh2_rdpkt_crstate); |
e5574168 |
1385 | |
ff3187f6 |
1386 | st->pktin = ssh_new_packet(); |
1387 | |
1388 | st->pktin->type = 0; |
1389 | st->pktin->length = 0; |
51470298 |
1390 | if (ssh->sccipher) |
1391 | st->cipherblk = ssh->sccipher->blksize; |
e5574168 |
1392 | else |
32874aea |
1393 | st->cipherblk = 8; |
960e736a |
1394 | if (st->cipherblk < 8) |
32874aea |
1395 | st->cipherblk = 8; |
9916cc1e |
1396 | st->maclen = ssh->scmac ? ssh->scmac->len : 0; |
960e736a |
1397 | |
9916cc1e |
1398 | if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_IS_CBC) && |
1399 | ssh->scmac) { |
1400 | /* |
1401 | * When dealing with a CBC-mode cipher, we want to avoid the |
1402 | * possibility of an attacker's tweaking the ciphertext stream |
1403 | * so as to cause us to feed the same block to the block |
1404 | * cipher more than once and thus leak information |
1405 | * (VU#958563). The way we do this is not to take any |
1406 | * decisions on the basis of anything we've decrypted until |
1407 | * we've verified it with a MAC. That includes the packet |
1408 | * length, so we just read data and check the MAC repeatedly, |
1409 | * and when the MAC passes, see if the length we've got is |
1410 | * plausible. |
1411 | */ |
1412 | |
1413 | /* May as well allocate the whole lot now. */ |
1414 | st->pktin->data = snewn(OUR_V2_PACKETLIMIT + st->maclen + APIEXTRA, |
1415 | unsigned char); |
1416 | |
1417 | /* Read an amount corresponding to the MAC. */ |
1418 | for (st->i = 0; st->i < st->maclen; st->i++) { |
1419 | while ((*datalen) == 0) |
1420 | crReturn(NULL); |
1421 | st->pktin->data[st->i] = *(*data)++; |
1422 | (*datalen)--; |
1423 | } |
e5574168 |
1424 | |
9916cc1e |
1425 | st->packetlen = 0; |
1426 | { |
1427 | unsigned char seq[4]; |
1428 | ssh->scmac->start(ssh->sc_mac_ctx); |
1429 | PUT_32BIT(seq, st->incoming_sequence); |
1430 | ssh->scmac->bytes(ssh->sc_mac_ctx, seq, 4); |
1431 | } |
4252c9cc |
1432 | |
9916cc1e |
1433 | for (;;) { /* Once around this loop per cipher block. */ |
1434 | /* Read another cipher-block's worth, and tack it onto the end. */ |
1435 | for (st->i = 0; st->i < st->cipherblk; st->i++) { |
1436 | while ((*datalen) == 0) |
1437 | crReturn(NULL); |
1438 | st->pktin->data[st->packetlen+st->maclen+st->i] = *(*data)++; |
1439 | (*datalen)--; |
1440 | } |
1441 | /* Decrypt one more block (a little further back in the stream). */ |
1442 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
1443 | st->pktin->data + st->packetlen, |
1444 | st->cipherblk); |
1445 | /* Feed that block to the MAC. */ |
1446 | ssh->scmac->bytes(ssh->sc_mac_ctx, |
1447 | st->pktin->data + st->packetlen, st->cipherblk); |
1448 | st->packetlen += st->cipherblk; |
1449 | /* See if that gives us a valid packet. */ |
1450 | if (ssh->scmac->verresult(ssh->sc_mac_ctx, |
1451 | st->pktin->data + st->packetlen) && |
b1650067 |
1452 | ((st->len = toint(GET_32BIT(st->pktin->data))) == |
1453 | st->packetlen-4)) |
9916cc1e |
1454 | break; |
1455 | if (st->packetlen >= OUR_V2_PACKETLIMIT) { |
1456 | bombout(("No valid incoming packet found")); |
1457 | ssh_free_packet(st->pktin); |
1458 | crStop(NULL); |
1459 | } |
1460 | } |
1461 | st->pktin->maxlen = st->packetlen + st->maclen; |
1462 | st->pktin->data = sresize(st->pktin->data, |
1463 | st->pktin->maxlen + APIEXTRA, |
1464 | unsigned char); |
1465 | } else { |
1466 | st->pktin->data = snewn(st->cipherblk + APIEXTRA, unsigned char); |
1467 | |
1468 | /* |
1469 | * Acquire and decrypt the first block of the packet. This will |
1470 | * contain the length and padding details. |
1471 | */ |
1472 | for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { |
1473 | while ((*datalen) == 0) |
1474 | crReturn(NULL); |
1475 | st->pktin->data[st->i] = *(*data)++; |
1476 | (*datalen)--; |
1477 | } |
e5574168 |
1478 | |
9916cc1e |
1479 | if (ssh->sccipher) |
1480 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
1481 | st->pktin->data, st->cipherblk); |
1482 | |
1483 | /* |
1484 | * Now get the length figure. |
1485 | */ |
b1650067 |
1486 | st->len = toint(GET_32BIT(st->pktin->data)); |
9916cc1e |
1487 | |
1488 | /* |
1489 | * _Completely_ silly lengths should be stomped on before they |
1490 | * do us any more damage. |
1491 | */ |
1492 | if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT || |
1493 | (st->len + 4) % st->cipherblk != 0) { |
1494 | bombout(("Incoming packet was garbled on decryption")); |
1495 | ssh_free_packet(st->pktin); |
1496 | crStop(NULL); |
1497 | } |
e5574168 |
1498 | |
9916cc1e |
1499 | /* |
1500 | * So now we can work out the total packet length. |
1501 | */ |
1502 | st->packetlen = st->len + 4; |
1503 | |
1504 | /* |
1505 | * Allocate memory for the rest of the packet. |
1506 | */ |
1507 | st->pktin->maxlen = st->packetlen + st->maclen; |
1508 | st->pktin->data = sresize(st->pktin->data, |
1509 | st->pktin->maxlen + APIEXTRA, |
1510 | unsigned char); |
1511 | |
1512 | /* |
1513 | * Read and decrypt the remainder of the packet. |
1514 | */ |
1515 | for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; |
1516 | st->i++) { |
1517 | while ((*datalen) == 0) |
1518 | crReturn(NULL); |
1519 | st->pktin->data[st->i] = *(*data)++; |
1520 | (*datalen)--; |
1521 | } |
1522 | /* Decrypt everything _except_ the MAC. */ |
1523 | if (ssh->sccipher) |
1524 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
1525 | st->pktin->data + st->cipherblk, |
1526 | st->packetlen - st->cipherblk); |
1527 | |
1528 | /* |
1529 | * Check the MAC. |
1530 | */ |
1531 | if (ssh->scmac |
1532 | && !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data, |
1533 | st->len + 4, st->incoming_sequence)) { |
1534 | bombout(("Incorrect MAC received on packet")); |
1535 | ssh_free_packet(st->pktin); |
1536 | crStop(NULL); |
1537 | } |
1538 | } |
1539 | /* Get and sanity-check the amount of random padding. */ |
1540 | st->pad = st->pktin->data[4]; |
1541 | if (st->pad < 4 || st->len - st->pad < 1) { |
1542 | bombout(("Invalid padding length on received packet")); |
ff3187f6 |
1543 | ssh_free_packet(st->pktin); |
1544 | crStop(NULL); |
717dc483 |
1545 | } |
717dc483 |
1546 | /* |
e5574168 |
1547 | * This enables us to deduce the payload length. |
1548 | */ |
960e736a |
1549 | st->payload = st->len - st->pad - 1; |
e5574168 |
1550 | |
ff3187f6 |
1551 | st->pktin->length = st->payload + 5; |
9442dd57 |
1552 | st->pktin->encrypted_len = st->packetlen; |
1553 | |
b09eaa88 |
1554 | st->pktin->sequence = st->incoming_sequence++; |
e5574168 |
1555 | |
4ba9b64b |
1556 | /* |
1557 | * Decompress packet payload. |
1558 | */ |
1559 | { |
1560 | unsigned char *newpayload; |
1561 | int newlen; |
51470298 |
1562 | if (ssh->sccomp && |
5366aed8 |
1563 | ssh->sccomp->decompress(ssh->sc_comp_ctx, |
ff3187f6 |
1564 | st->pktin->data + 5, st->pktin->length - 5, |
51470298 |
1565 | &newpayload, &newlen)) { |
ff3187f6 |
1566 | if (st->pktin->maxlen < newlen + 5) { |
1567 | st->pktin->maxlen = newlen + 5; |
1568 | st->pktin->data = sresize(st->pktin->data, |
1569 | st->pktin->maxlen + APIEXTRA, |
3d88e64d |
1570 | unsigned char); |
4ba9b64b |
1571 | } |
ff3187f6 |
1572 | st->pktin->length = 5 + newlen; |
1573 | memcpy(st->pktin->data + 5, newpayload, newlen); |
dcbde236 |
1574 | sfree(newpayload); |
4ba9b64b |
1575 | } |
1576 | } |
1577 | |
ff3187f6 |
1578 | st->pktin->savedpos = 6; |
1579 | st->pktin->body = st->pktin->data; |
1580 | st->pktin->type = st->pktin->data[5]; |
e5574168 |
1581 | |
9a10ecf4 |
1582 | /* |
1583 | * Log incoming packet, possibly omitting sensitive fields. |
1584 | */ |
1585 | if (ssh->logctx) { |
1586 | int nblanks = 0; |
1587 | struct logblank_t blank; |
4a693cfc |
1588 | if (ssh->logomitdata) { |
9a10ecf4 |
1589 | int do_blank = FALSE, blank_prefix = 0; |
1590 | /* "Session data" packets - omit the data field */ |
ff3187f6 |
1591 | if (st->pktin->type == SSH2_MSG_CHANNEL_DATA) { |
9a10ecf4 |
1592 | do_blank = TRUE; blank_prefix = 8; |
a2724fba |
1593 | } else if (st->pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
1594 | do_blank = TRUE; blank_prefix = 12; |
9a10ecf4 |
1595 | } |
1596 | if (do_blank) { |
1597 | blank.offset = blank_prefix; |
ff3187f6 |
1598 | blank.len = (st->pktin->length-6) - blank_prefix; |
9a10ecf4 |
1599 | blank.type = PKTLOG_OMIT; |
1600 | nblanks = 1; |
1601 | } |
1602 | } |
ff3187f6 |
1603 | log_packet(ssh->logctx, PKT_INCOMING, st->pktin->type, |
acab36bc |
1604 | ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, |
1605 | st->pktin->type), |
ff3187f6 |
1606 | st->pktin->data+6, st->pktin->length-6, |
0d13bfcd |
1607 | nblanks, &blank, &st->pktin->sequence); |
9a10ecf4 |
1608 | } |
00db133f |
1609 | |
ff3187f6 |
1610 | crFinish(st->pktin); |
e5574168 |
1611 | } |
1612 | |
dacd8872 |
1613 | static int s_wrpkt_prepare(Ssh ssh, struct Packet *pkt, int *offset_p) |
32874aea |
1614 | { |
dacd8872 |
1615 | int pad, biglen, i, pktoffs; |
374330e2 |
1616 | unsigned long crc; |
fd7a4aad |
1617 | #ifdef __SC__ |
1618 | /* |
1619 | * XXX various versions of SC (including 8.8.4) screw up the |
1620 | * register allocation in this function and use the same register |
1621 | * (D6) for len and as a temporary, with predictable results. The |
1622 | * following sledgehammer prevents this. |
1623 | */ |
1624 | volatile |
1625 | #endif |
1626 | int len; |
374330e2 |
1627 | |
a8327734 |
1628 | if (ssh->logctx) |
dacd8872 |
1629 | log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[12], |
1630 | ssh1_pkt_type(pkt->data[12]), |
1631 | pkt->body, pkt->length - (pkt->body - pkt->data), |
0d13bfcd |
1632 | pkt->nblanks, pkt->blanks, NULL); |
ff3187f6 |
1633 | sfree(pkt->blanks); pkt->blanks = NULL; |
1634 | pkt->nblanks = 0; |
00db133f |
1635 | |
51470298 |
1636 | if (ssh->v1_compressing) { |
4ba9b64b |
1637 | unsigned char *compblk; |
1638 | int complen; |
5366aed8 |
1639 | zlib_compress_block(ssh->cs_comp_ctx, |
dacd8872 |
1640 | pkt->data + 12, pkt->length - 12, |
4ba9b64b |
1641 | &compblk, &complen); |
48406e6b |
1642 | ssh_pkt_ensure(pkt, complen + 2); /* just in case it's got bigger */ |
dacd8872 |
1643 | memcpy(pkt->data + 12, compblk, complen); |
dcbde236 |
1644 | sfree(compblk); |
dacd8872 |
1645 | pkt->length = complen + 12; |
4ba9b64b |
1646 | } |
1647 | |
dacd8872 |
1648 | ssh_pkt_ensure(pkt, pkt->length + 4); /* space for CRC */ |
1649 | pkt->length += 4; |
1650 | len = pkt->length - 4 - 8; /* len(type+data+CRC) */ |
32874aea |
1651 | pad = 8 - (len % 8); |
dacd8872 |
1652 | pktoffs = 8 - pad; |
1653 | biglen = len + pad; /* len(padding+type+data+CRC) */ |
374330e2 |
1654 | |
dacd8872 |
1655 | for (i = pktoffs; i < 4+8; i++) |
1656 | pkt->data[i] = random_byte(); |
1657 | crc = crc32_compute(pkt->data + pktoffs + 4, biglen - 4); /* all ex len */ |
1658 | PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc); |
1659 | PUT_32BIT(pkt->data + pktoffs, len); |
374330e2 |
1660 | |
51470298 |
1661 | if (ssh->cipher) |
dacd8872 |
1662 | ssh->cipher->encrypt(ssh->v1_cipher_ctx, |
1663 | pkt->data + pktoffs + 4, biglen); |
374330e2 |
1664 | |
dacd8872 |
1665 | if (offset_p) *offset_p = pktoffs; |
1666 | return biglen + 4; /* len(length+padding+type+data+CRC) */ |
39065bed |
1667 | } |
1668 | |
bf8a49a1 |
1669 | static int s_write(Ssh ssh, void *data, int len) |
1670 | { |
ff05dfef |
1671 | if (ssh->logctx) |
0d13bfcd |
1672 | log_packet(ssh->logctx, PKT_OUTGOING, -1, NULL, data, len, |
1673 | 0, NULL, NULL); |
bf8a49a1 |
1674 | return sk_write(ssh->s, (char *)data, len); |
1675 | } |
1676 | |
ff3187f6 |
1677 | static void s_wrpkt(Ssh ssh, struct Packet *pkt) |
32874aea |
1678 | { |
dacd8872 |
1679 | int len, backlog, offset; |
1680 | len = s_wrpkt_prepare(ssh, pkt, &offset); |
bf8a49a1 |
1681 | backlog = s_write(ssh, pkt->data + offset, len); |
5471d09a |
1682 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1683 | ssh_throttle_all(ssh, 1, backlog); |
dacd8872 |
1684 | ssh_free_packet(pkt); |
39065bed |
1685 | } |
1686 | |
ff3187f6 |
1687 | static void s_wrpkt_defer(Ssh ssh, struct Packet *pkt) |
32874aea |
1688 | { |
dacd8872 |
1689 | int len, offset; |
1690 | len = s_wrpkt_prepare(ssh, pkt, &offset); |
51470298 |
1691 | if (ssh->deferred_len + len > ssh->deferred_size) { |
1692 | ssh->deferred_size = ssh->deferred_len + len + 128; |
3d88e64d |
1693 | ssh->deferred_send_data = sresize(ssh->deferred_send_data, |
1694 | ssh->deferred_size, |
1695 | unsigned char); |
39065bed |
1696 | } |
dacd8872 |
1697 | memcpy(ssh->deferred_send_data + ssh->deferred_len, |
1698 | pkt->data + offset, len); |
51470298 |
1699 | ssh->deferred_len += len; |
dacd8872 |
1700 | ssh_free_packet(pkt); |
374330e2 |
1701 | } |
1702 | |
fb09bf1c |
1703 | /* |
dacd8872 |
1704 | * Construct a SSH-1 packet with the specified contents. |
1705 | * (This all-at-once interface used to be the only one, but now SSH-1 |
1706 | * packets can also be constructed incrementally.) |
fb09bf1c |
1707 | */ |
dacd8872 |
1708 | static struct Packet *construct_packet(Ssh ssh, int pkttype, va_list ap) |
fb09bf1c |
1709 | { |
dacd8872 |
1710 | int argtype; |
7cca0d81 |
1711 | Bignum bn; |
ff3187f6 |
1712 | struct Packet *pkt; |
fb09bf1c |
1713 | |
dacd8872 |
1714 | pkt = ssh1_pkt_init(pkttype); |
fb09bf1c |
1715 | |
dacd8872 |
1716 | while ((argtype = va_arg(ap, int)) != PKT_END) { |
1717 | unsigned char *argp, argchar; |
8a9977e5 |
1718 | char *sargp; |
dacd8872 |
1719 | unsigned long argint; |
1720 | int arglen; |
fb09bf1c |
1721 | switch (argtype) { |
9a10ecf4 |
1722 | /* Actual fields in the packet */ |
fb09bf1c |
1723 | case PKT_INT: |
dacd8872 |
1724 | argint = va_arg(ap, int); |
1725 | ssh_pkt_adduint32(pkt, argint); |
fb09bf1c |
1726 | break; |
1727 | case PKT_CHAR: |
dacd8872 |
1728 | argchar = (unsigned char) va_arg(ap, int); |
1729 | ssh_pkt_addbyte(pkt, argchar); |
fb09bf1c |
1730 | break; |
1731 | case PKT_DATA: |
dacd8872 |
1732 | argp = va_arg(ap, unsigned char *); |
1733 | arglen = va_arg(ap, int); |
1734 | ssh_pkt_adddata(pkt, argp, arglen); |
fb09bf1c |
1735 | break; |
1736 | case PKT_STR: |
8a9977e5 |
1737 | sargp = va_arg(ap, char *); |
1738 | ssh_pkt_addstring(pkt, sargp); |
fb09bf1c |
1739 | break; |
7cca0d81 |
1740 | case PKT_BIGNUM: |
dacd8872 |
1741 | bn = va_arg(ap, Bignum); |
1742 | ssh1_pkt_addmp(pkt, bn); |
9a10ecf4 |
1743 | break; |
1744 | /* Tokens for modifications to packet logging */ |
1745 | case PKTT_PASSWORD: |
ff3187f6 |
1746 | dont_log_password(ssh, pkt, PKTLOG_BLANK); |
9a10ecf4 |
1747 | break; |
1748 | case PKTT_DATA: |
ff3187f6 |
1749 | dont_log_data(ssh, pkt, PKTLOG_OMIT); |
7cca0d81 |
1750 | break; |
9a10ecf4 |
1751 | case PKTT_OTHER: |
ff3187f6 |
1752 | end_log_omission(ssh, pkt); |
9a10ecf4 |
1753 | break; |
1754 | } |
fb09bf1c |
1755 | } |
ff3187f6 |
1756 | |
1757 | return pkt; |
39065bed |
1758 | } |
fb09bf1c |
1759 | |
51470298 |
1760 | static void send_packet(Ssh ssh, int pkttype, ...) |
32874aea |
1761 | { |
ff3187f6 |
1762 | struct Packet *pkt; |
dacd8872 |
1763 | va_list ap; |
1764 | va_start(ap, pkttype); |
1765 | pkt = construct_packet(ssh, pkttype, ap); |
1766 | va_end(ap); |
ff3187f6 |
1767 | s_wrpkt(ssh, pkt); |
fb09bf1c |
1768 | } |
1769 | |
51470298 |
1770 | static void defer_packet(Ssh ssh, int pkttype, ...) |
32874aea |
1771 | { |
ff3187f6 |
1772 | struct Packet *pkt; |
dacd8872 |
1773 | va_list ap; |
1774 | va_start(ap, pkttype); |
1775 | pkt = construct_packet(ssh, pkttype, ap); |
1776 | va_end(ap); |
ff3187f6 |
1777 | s_wrpkt_defer(ssh, pkt); |
39065bed |
1778 | } |
1779 | |
32874aea |
1780 | static int ssh_versioncmp(char *a, char *b) |
1781 | { |
9697bfd2 |
1782 | char *ae, *be; |
1783 | unsigned long av, bv; |
1784 | |
43aa02a7 |
1785 | av = strtoul(a, &ae, 10); |
1786 | bv = strtoul(b, &be, 10); |
32874aea |
1787 | if (av != bv) |
1788 | return (av < bv ? -1 : +1); |
1789 | if (*ae == '.') |
1790 | ae++; |
1791 | if (*be == '.') |
1792 | be++; |
43aa02a7 |
1793 | av = strtoul(ae, &ae, 10); |
1794 | bv = strtoul(be, &be, 10); |
32874aea |
1795 | if (av != bv) |
1796 | return (av < bv ? -1 : +1); |
9697bfd2 |
1797 | return 0; |
1798 | } |
1799 | |
e5574168 |
1800 | /* |
a92dd380 |
1801 | * Utility routines for putting an SSH-protocol `string' and |
b672f405 |
1802 | * `uint32' into a hash state. |
e5574168 |
1803 | */ |
b672f405 |
1804 | static void hash_string(const struct ssh_hash *h, void *s, void *str, int len) |
32874aea |
1805 | { |
e5574168 |
1806 | unsigned char lenblk[4]; |
e5574168 |
1807 | PUT_32BIT(lenblk, len); |
b672f405 |
1808 | h->bytes(s, lenblk, 4); |
1809 | h->bytes(s, str, len); |
e5574168 |
1810 | } |
1811 | |
b672f405 |
1812 | static void hash_uint32(const struct ssh_hash *h, void *s, unsigned i) |
32874aea |
1813 | { |
a92dd380 |
1814 | unsigned char intblk[4]; |
1815 | PUT_32BIT(intblk, i); |
b672f405 |
1816 | h->bytes(s, intblk, 4); |
a92dd380 |
1817 | } |
1818 | |
7cca0d81 |
1819 | /* |
dacd8872 |
1820 | * Packet construction functions. Mostly shared between SSH-1 and SSH-2. |
7cca0d81 |
1821 | */ |
dacd8872 |
1822 | static void ssh_pkt_ensure(struct Packet *pkt, int length) |
32874aea |
1823 | { |
ff3187f6 |
1824 | if (pkt->maxlen < length) { |
dacd8872 |
1825 | unsigned char *body = pkt->body; |
3d71de0b |
1826 | int offset = body ? body - pkt->data : 0; |
ff3187f6 |
1827 | pkt->maxlen = length + 256; |
1828 | pkt->data = sresize(pkt->data, pkt->maxlen + APIEXTRA, unsigned char); |
dacd8872 |
1829 | if (body) pkt->body = pkt->data + offset; |
7cca0d81 |
1830 | } |
783415f8 |
1831 | } |
dacd8872 |
1832 | static void ssh_pkt_adddata(struct Packet *pkt, void *data, int len) |
32874aea |
1833 | { |
ff3187f6 |
1834 | if (pkt->logmode != PKTLOG_EMIT) { |
1835 | pkt->nblanks++; |
1836 | pkt->blanks = sresize(pkt->blanks, pkt->nblanks, struct logblank_t); |
dacd8872 |
1837 | assert(pkt->body); |
1838 | pkt->blanks[pkt->nblanks-1].offset = pkt->length - |
1839 | (pkt->body - pkt->data); |
ff3187f6 |
1840 | pkt->blanks[pkt->nblanks-1].len = len; |
1841 | pkt->blanks[pkt->nblanks-1].type = pkt->logmode; |
1842 | } |
1843 | pkt->length += len; |
dacd8872 |
1844 | ssh_pkt_ensure(pkt, pkt->length); |
ff3187f6 |
1845 | memcpy(pkt->data + pkt->length - len, data, len); |
7cca0d81 |
1846 | } |
dacd8872 |
1847 | static void ssh_pkt_addbyte(struct Packet *pkt, unsigned char byte) |
32874aea |
1848 | { |
dacd8872 |
1849 | ssh_pkt_adddata(pkt, &byte, 1); |
7cca0d81 |
1850 | } |
ff3187f6 |
1851 | static void ssh2_pkt_addbool(struct Packet *pkt, unsigned char value) |
32874aea |
1852 | { |
dacd8872 |
1853 | ssh_pkt_adddata(pkt, &value, 1); |
7cca0d81 |
1854 | } |
dacd8872 |
1855 | static void ssh_pkt_adduint32(struct Packet *pkt, unsigned long value) |
32874aea |
1856 | { |
7cca0d81 |
1857 | unsigned char x[4]; |
1858 | PUT_32BIT(x, value); |
dacd8872 |
1859 | ssh_pkt_adddata(pkt, x, 4); |
7cca0d81 |
1860 | } |
dacd8872 |
1861 | static void ssh_pkt_addstring_start(struct Packet *pkt) |
32874aea |
1862 | { |
dacd8872 |
1863 | ssh_pkt_adduint32(pkt, 0); |
ff3187f6 |
1864 | pkt->savedpos = pkt->length; |
7cca0d81 |
1865 | } |
dacd8872 |
1866 | static void ssh_pkt_addstring_str(struct Packet *pkt, char *data) |
32874aea |
1867 | { |
dacd8872 |
1868 | ssh_pkt_adddata(pkt, data, strlen(data)); |
ff3187f6 |
1869 | PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos); |
7cca0d81 |
1870 | } |
dacd8872 |
1871 | static void ssh_pkt_addstring_data(struct Packet *pkt, char *data, int len) |
32874aea |
1872 | { |
dacd8872 |
1873 | ssh_pkt_adddata(pkt, data, len); |
ff3187f6 |
1874 | PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos); |
7cca0d81 |
1875 | } |
dacd8872 |
1876 | static void ssh_pkt_addstring(struct Packet *pkt, char *data) |
1877 | { |
1878 | ssh_pkt_addstring_start(pkt); |
1879 | ssh_pkt_addstring_str(pkt, data); |
1880 | } |
1881 | static void ssh1_pkt_addmp(struct Packet *pkt, Bignum b) |
32874aea |
1882 | { |
dacd8872 |
1883 | int len = ssh1_bignum_length(b); |
8a9977e5 |
1884 | unsigned char *data = snewn(len, unsigned char); |
dacd8872 |
1885 | (void) ssh1_write_bignum(data, b); |
1886 | ssh_pkt_adddata(pkt, data, len); |
1887 | sfree(data); |
7cca0d81 |
1888 | } |
d8baa528 |
1889 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len) |
32874aea |
1890 | { |
7cca0d81 |
1891 | unsigned char *p; |
32874aea |
1892 | int i, n = (bignum_bitcount(b) + 7) / 8; |
3d88e64d |
1893 | p = snewn(n + 1, unsigned char); |
7cca0d81 |
1894 | p[0] = 0; |
3709bfe9 |
1895 | for (i = 1; i <= n; i++) |
32874aea |
1896 | p[i] = bignum_byte(b, n - i); |
7cca0d81 |
1897 | i = 0; |
32874aea |
1898 | while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0) |
1899 | i++; |
1900 | memmove(p, p + i, n + 1 - i); |
1901 | *len = n + 1 - i; |
7cca0d81 |
1902 | return p; |
1903 | } |
ff3187f6 |
1904 | static void ssh2_pkt_addmp(struct Packet *pkt, Bignum b) |
32874aea |
1905 | { |
7cca0d81 |
1906 | unsigned char *p; |
1907 | int len; |
1908 | p = ssh2_mpint_fmt(b, &len); |
dacd8872 |
1909 | ssh_pkt_addstring_start(pkt); |
1910 | ssh_pkt_addstring_data(pkt, (char *)p, len); |
dcbde236 |
1911 | sfree(p); |
7cca0d81 |
1912 | } |
b185170a |
1913 | |
dacd8872 |
1914 | static struct Packet *ssh1_pkt_init(int pkt_type) |
1915 | { |
1916 | struct Packet *pkt = ssh_new_packet(); |
1917 | pkt->length = 4 + 8; /* space for length + max padding */ |
1918 | ssh_pkt_addbyte(pkt, pkt_type); |
1919 | pkt->body = pkt->data + pkt->length; |
1920 | return pkt; |
1921 | } |
1922 | |
1923 | /* For legacy code (SSH-1 and -2 packet construction used to be separate) */ |
1924 | #define ssh2_pkt_ensure(pkt, length) ssh_pkt_ensure(pkt, length) |
1925 | #define ssh2_pkt_adddata(pkt, data, len) ssh_pkt_adddata(pkt, data, len) |
1926 | #define ssh2_pkt_addbyte(pkt, byte) ssh_pkt_addbyte(pkt, byte) |
1927 | #define ssh2_pkt_adduint32(pkt, value) ssh_pkt_adduint32(pkt, value) |
1928 | #define ssh2_pkt_addstring_start(pkt) ssh_pkt_addstring_start(pkt) |
1929 | #define ssh2_pkt_addstring_str(pkt, data) ssh_pkt_addstring_str(pkt, data) |
1930 | #define ssh2_pkt_addstring_data(pkt, data, len) ssh_pkt_addstring_data(pkt, data, len) |
1931 | #define ssh2_pkt_addstring(pkt, data) ssh_pkt_addstring(pkt, data) |
1932 | |
1933 | static struct Packet *ssh2_pkt_init(int pkt_type) |
1934 | { |
1935 | struct Packet *pkt = ssh_new_packet(); |
3d71de0b |
1936 | pkt->length = 5; /* space for packet length + padding length */ |
dacd8872 |
1937 | pkt->forcepad = 0; |
1938 | ssh_pkt_addbyte(pkt, (unsigned char) pkt_type); |
3d71de0b |
1939 | pkt->body = pkt->data + pkt->length; /* after packet type */ |
dacd8872 |
1940 | return pkt; |
1941 | } |
1942 | |
b185170a |
1943 | /* |
2e85c969 |
1944 | * Construct an SSH-2 final-form packet: compress it, encrypt it, |
b185170a |
1945 | * put the MAC on it. Final packet, ready to be sent, is stored in |
ff3187f6 |
1946 | * pkt->data. Total length is returned. |
b185170a |
1947 | */ |
ff3187f6 |
1948 | static int ssh2_pkt_construct(Ssh ssh, struct Packet *pkt) |
32874aea |
1949 | { |
7cca0d81 |
1950 | int cipherblk, maclen, padding, i; |
7cca0d81 |
1951 | |
a8327734 |
1952 | if (ssh->logctx) |
ff3187f6 |
1953 | log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[5], |
acab36bc |
1954 | ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->data[5]), |
dacd8872 |
1955 | pkt->body, pkt->length - (pkt->body - pkt->data), |
0d13bfcd |
1956 | pkt->nblanks, pkt->blanks, &ssh->v2_outgoing_sequence); |
ff3187f6 |
1957 | sfree(pkt->blanks); pkt->blanks = NULL; |
1958 | pkt->nblanks = 0; |
00db133f |
1959 | |
7cca0d81 |
1960 | /* |
4ba9b64b |
1961 | * Compress packet payload. |
1962 | */ |
4ba9b64b |
1963 | { |
1964 | unsigned char *newpayload; |
1965 | int newlen; |
51470298 |
1966 | if (ssh->cscomp && |
ff3187f6 |
1967 | ssh->cscomp->compress(ssh->cs_comp_ctx, pkt->data + 5, |
1968 | pkt->length - 5, |
51470298 |
1969 | &newpayload, &newlen)) { |
ff3187f6 |
1970 | pkt->length = 5; |
1971 | ssh2_pkt_adddata(pkt, newpayload, newlen); |
dcbde236 |
1972 | sfree(newpayload); |
4ba9b64b |
1973 | } |
1974 | } |
1975 | |
1976 | /* |
7cca0d81 |
1977 | * Add padding. At least four bytes, and must also bring total |
1978 | * length (minus MAC) up to a multiple of the block size. |
95d2d262 |
1979 | * If pkt->forcepad is set, make sure the packet is at least that size |
1980 | * after padding. |
7cca0d81 |
1981 | */ |
51470298 |
1982 | cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */ |
32874aea |
1983 | cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ |
7cca0d81 |
1984 | padding = 4; |
95d2d262 |
1985 | if (pkt->length + padding < pkt->forcepad) |
1986 | padding = pkt->forcepad - pkt->length; |
32874aea |
1987 | padding += |
ff3187f6 |
1988 | (cipherblk - (pkt->length + padding) % cipherblk) % cipherblk; |
95d2d262 |
1989 | assert(padding <= 255); |
51470298 |
1990 | maclen = ssh->csmac ? ssh->csmac->len : 0; |
ff3187f6 |
1991 | ssh2_pkt_ensure(pkt, pkt->length + padding + maclen); |
1992 | pkt->data[4] = padding; |
7cca0d81 |
1993 | for (i = 0; i < padding; i++) |
ff3187f6 |
1994 | pkt->data[pkt->length + i] = random_byte(); |
1995 | PUT_32BIT(pkt->data, pkt->length + padding - 4); |
51470298 |
1996 | if (ssh->csmac) |
ff3187f6 |
1997 | ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data, |
1998 | pkt->length + padding, |
51470298 |
1999 | ssh->v2_outgoing_sequence); |
2000 | ssh->v2_outgoing_sequence++; /* whether or not we MACed */ |
2001 | |
2002 | if (ssh->cscipher) |
371e569c |
2003 | ssh->cscipher->encrypt(ssh->cs_cipher_ctx, |
ff3187f6 |
2004 | pkt->data, pkt->length + padding); |
51470298 |
2005 | |
9442dd57 |
2006 | pkt->encrypted_len = pkt->length + padding; |
2007 | |
ff3187f6 |
2008 | /* Ready-to-send packet starts at pkt->data. We return length. */ |
2009 | return pkt->length + padding + maclen; |
b185170a |
2010 | } |
2011 | |
2012 | /* |
590f6a5f |
2013 | * Routines called from the main SSH code to send packets. There |
2014 | * are quite a few of these, because we have two separate |
2015 | * mechanisms for delaying the sending of packets: |
2016 | * |
2017 | * - In order to send an IGNORE message and a password message in |
2018 | * a single fixed-length blob, we require the ability to |
2019 | * concatenate the encrypted forms of those two packets _into_ a |
2020 | * single blob and then pass it to our <network.h> transport |
2021 | * layer in one go. Hence, there's a deferment mechanism which |
2022 | * works after packet encryption. |
2023 | * |
2024 | * - In order to avoid sending any connection-layer messages |
2025 | * during repeat key exchange, we have to queue up any such |
2026 | * outgoing messages _before_ they are encrypted (and in |
2027 | * particular before they're allocated sequence numbers), and |
2028 | * then send them once we've finished. |
2029 | * |
2030 | * I call these mechanisms `defer' and `queue' respectively, so as |
2031 | * to distinguish them reasonably easily. |
2032 | * |
2033 | * The functions send_noqueue() and defer_noqueue() free the packet |
2034 | * structure they are passed. Every outgoing packet goes through |
2035 | * precisely one of these functions in its life; packets passed to |
2036 | * ssh2_pkt_send() or ssh2_pkt_defer() either go straight to one of |
2037 | * these or get queued, and then when the queue is later emptied |
2038 | * the packets are all passed to defer_noqueue(). |
97ab28d7 |
2039 | * |
2040 | * When using a CBC-mode cipher, it's necessary to ensure that an |
2041 | * attacker can't provide data to be encrypted using an IV that they |
2042 | * know. We ensure this by prefixing each packet that might contain |
2043 | * user data with an SSH_MSG_IGNORE. This is done using the deferral |
2044 | * mechanism, so in this case send_noqueue() ends up redirecting to |
2045 | * defer_noqueue(). If you don't like this inefficiency, don't use |
2046 | * CBC. |
b185170a |
2047 | */ |
590f6a5f |
2048 | |
97ab28d7 |
2049 | static void ssh2_pkt_defer_noqueue(Ssh, struct Packet *, int); |
2050 | static void ssh_pkt_defersend(Ssh); |
2051 | |
590f6a5f |
2052 | /* |
2e85c969 |
2053 | * Send an SSH-2 packet immediately, without queuing or deferring. |
590f6a5f |
2054 | */ |
2055 | static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt) |
32874aea |
2056 | { |
5471d09a |
2057 | int len; |
2058 | int backlog; |
97ab28d7 |
2059 | if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC)) { |
2060 | /* We need to send two packets, so use the deferral mechanism. */ |
2061 | ssh2_pkt_defer_noqueue(ssh, pkt, FALSE); |
2062 | ssh_pkt_defersend(ssh); |
2063 | return; |
2064 | } |
ff3187f6 |
2065 | len = ssh2_pkt_construct(ssh, pkt); |
bf8a49a1 |
2066 | backlog = s_write(ssh, pkt->data, len); |
5471d09a |
2067 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
2068 | ssh_throttle_all(ssh, 1, backlog); |
9442dd57 |
2069 | |
2070 | ssh->outgoing_data_size += pkt->encrypted_len; |
2071 | if (!ssh->kex_in_progress && |
d57f70af |
2072 | ssh->max_data_size != 0 && |
2073 | ssh->outgoing_data_size > ssh->max_data_size) |
f382c87d |
2074 | do_ssh2_transport(ssh, "too much data sent", -1, NULL); |
9442dd57 |
2075 | |
ff3187f6 |
2076 | ssh_free_packet(pkt); |
b185170a |
2077 | } |
2078 | |
2079 | /* |
2e85c969 |
2080 | * Defer an SSH-2 packet. |
b185170a |
2081 | */ |
97ab28d7 |
2082 | static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt, int noignore) |
32874aea |
2083 | { |
97ab28d7 |
2084 | int len; |
2085 | if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC) && |
cf6ddb95 |
2086 | ssh->deferred_len == 0 && !noignore && |
2087 | !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) { |
97ab28d7 |
2088 | /* |
2089 | * Interpose an SSH_MSG_IGNORE to ensure that user data don't |
2090 | * get encrypted with a known IV. |
2091 | */ |
2092 | struct Packet *ipkt = ssh2_pkt_init(SSH2_MSG_IGNORE); |
27311cc7 |
2093 | ssh2_pkt_addstring_start(ipkt); |
97ab28d7 |
2094 | ssh2_pkt_defer_noqueue(ssh, ipkt, TRUE); |
2095 | } |
2096 | len = ssh2_pkt_construct(ssh, pkt); |
51470298 |
2097 | if (ssh->deferred_len + len > ssh->deferred_size) { |
2098 | ssh->deferred_size = ssh->deferred_len + len + 128; |
3d88e64d |
2099 | ssh->deferred_send_data = sresize(ssh->deferred_send_data, |
2100 | ssh->deferred_size, |
2101 | unsigned char); |
b185170a |
2102 | } |
ff3187f6 |
2103 | memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->data, len); |
51470298 |
2104 | ssh->deferred_len += len; |
9442dd57 |
2105 | ssh->deferred_data_size += pkt->encrypted_len; |
ff3187f6 |
2106 | ssh_free_packet(pkt); |
b185170a |
2107 | } |
2108 | |
2109 | /* |
2e85c969 |
2110 | * Queue an SSH-2 packet. |
590f6a5f |
2111 | */ |
2112 | static void ssh2_pkt_queue(Ssh ssh, struct Packet *pkt) |
2113 | { |
2114 | assert(ssh->queueing); |
2115 | |
2116 | if (ssh->queuelen >= ssh->queuesize) { |
2117 | ssh->queuesize = ssh->queuelen + 32; |
2118 | ssh->queue = sresize(ssh->queue, ssh->queuesize, struct Packet *); |
2119 | } |
2120 | |
2121 | ssh->queue[ssh->queuelen++] = pkt; |
2122 | } |
2123 | |
2124 | /* |
2125 | * Either queue or send a packet, depending on whether queueing is |
2126 | * set. |
2127 | */ |
2128 | static void ssh2_pkt_send(Ssh ssh, struct Packet *pkt) |
2129 | { |
2130 | if (ssh->queueing) |
2131 | ssh2_pkt_queue(ssh, pkt); |
2132 | else |
2133 | ssh2_pkt_send_noqueue(ssh, pkt); |
2134 | } |
2135 | |
2136 | /* |
2137 | * Either queue or defer a packet, depending on whether queueing is |
2138 | * set. |
2139 | */ |
2140 | static void ssh2_pkt_defer(Ssh ssh, struct Packet *pkt) |
2141 | { |
2142 | if (ssh->queueing) |
2143 | ssh2_pkt_queue(ssh, pkt); |
2144 | else |
97ab28d7 |
2145 | ssh2_pkt_defer_noqueue(ssh, pkt, FALSE); |
590f6a5f |
2146 | } |
2147 | |
2148 | /* |
b185170a |
2149 | * Send the whole deferred data block constructed by |
2e85c969 |
2150 | * ssh2_pkt_defer() or SSH-1's defer_packet(). |
590f6a5f |
2151 | * |
2152 | * The expected use of the defer mechanism is that you call |
2153 | * ssh2_pkt_defer() a few times, then call ssh_pkt_defersend(). If |
2154 | * not currently queueing, this simply sets up deferred_send_data |
2155 | * and then sends it. If we _are_ currently queueing, the calls to |
2156 | * ssh2_pkt_defer() put the deferred packets on to the queue |
2157 | * instead, and therefore ssh_pkt_defersend() has no deferred data |
2158 | * to send. Hence, there's no need to make it conditional on |
2159 | * ssh->queueing. |
b185170a |
2160 | */ |
51470298 |
2161 | static void ssh_pkt_defersend(Ssh ssh) |
32874aea |
2162 | { |
5471d09a |
2163 | int backlog; |
bf8a49a1 |
2164 | backlog = s_write(ssh, ssh->deferred_send_data, ssh->deferred_len); |
51470298 |
2165 | ssh->deferred_len = ssh->deferred_size = 0; |
2166 | sfree(ssh->deferred_send_data); |
2167 | ssh->deferred_send_data = NULL; |
5471d09a |
2168 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
2169 | ssh_throttle_all(ssh, 1, backlog); |
9442dd57 |
2170 | |
2171 | ssh->outgoing_data_size += ssh->deferred_data_size; |
2172 | if (!ssh->kex_in_progress && |
d57f70af |
2173 | ssh->max_data_size != 0 && |
2174 | ssh->outgoing_data_size > ssh->max_data_size) |
f382c87d |
2175 | do_ssh2_transport(ssh, "too much data sent", -1, NULL); |
9442dd57 |
2176 | ssh->deferred_data_size = 0; |
7cca0d81 |
2177 | } |
2178 | |
590f6a5f |
2179 | /* |
18524056 |
2180 | * Send a packet whose length needs to be disguised (typically |
2181 | * passwords or keyboard-interactive responses). |
2182 | */ |
2183 | static void ssh2_pkt_send_with_padding(Ssh ssh, struct Packet *pkt, |
2184 | int padsize) |
2185 | { |
2186 | #if 0 |
2187 | if (0) { |
2188 | /* |
2189 | * The simplest way to do this is to adjust the |
2190 | * variable-length padding field in the outgoing packet. |
2191 | * |
2192 | * Currently compiled out, because some Cisco SSH servers |
2193 | * don't like excessively padded packets (bah, why's it |
2194 | * always Cisco?) |
2195 | */ |
2196 | pkt->forcepad = padsize; |
2197 | ssh2_pkt_send(ssh, pkt); |
2198 | } else |
2199 | #endif |
2200 | { |
2201 | /* |
2202 | * If we can't do that, however, an alternative approach is |
2203 | * to use the pkt_defer mechanism to bundle the packet |
2204 | * tightly together with an SSH_MSG_IGNORE such that their |
2205 | * combined length is a constant. So first we construct the |
2206 | * final form of this packet and defer its sending. |
2207 | */ |
2208 | ssh2_pkt_defer(ssh, pkt); |
2209 | |
2210 | /* |
2211 | * Now construct an SSH_MSG_IGNORE which includes a string |
2212 | * that's an exact multiple of the cipher block size. (If |
2213 | * the cipher is NULL so that the block size is |
2214 | * unavailable, we don't do this trick at all, because we |
2215 | * gain nothing by it.) |
2216 | */ |
cf6ddb95 |
2217 | if (ssh->cscipher && |
2218 | !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) { |
18524056 |
2219 | int stringlen, i; |
2220 | |
2221 | stringlen = (256 - ssh->deferred_len); |
2222 | stringlen += ssh->cscipher->blksize - 1; |
2223 | stringlen -= (stringlen % ssh->cscipher->blksize); |
2224 | if (ssh->cscomp) { |
2225 | /* |
2226 | * Temporarily disable actual compression, so we |
2227 | * can guarantee to get this string exactly the |
2228 | * length we want it. The compression-disabling |
2229 | * routine should return an integer indicating how |
2230 | * many bytes we should adjust our string length |
2231 | * by. |
2232 | */ |
2233 | stringlen -= |
2234 | ssh->cscomp->disable_compression(ssh->cs_comp_ctx); |
2235 | } |
2236 | pkt = ssh2_pkt_init(SSH2_MSG_IGNORE); |
2237 | ssh2_pkt_addstring_start(pkt); |
2238 | for (i = 0; i < stringlen; i++) { |
2239 | char c = (char) random_byte(); |
2240 | ssh2_pkt_addstring_data(pkt, &c, 1); |
2241 | } |
2242 | ssh2_pkt_defer(ssh, pkt); |
2243 | } |
2244 | ssh_pkt_defersend(ssh); |
2245 | } |
2246 | } |
2247 | |
2248 | /* |
2e85c969 |
2249 | * Send all queued SSH-2 packets. We send them by means of |
590f6a5f |
2250 | * ssh2_pkt_defer_noqueue(), in case they included a pair of |
2251 | * packets that needed to be lumped together. |
2252 | */ |
2253 | static void ssh2_pkt_queuesend(Ssh ssh) |
2254 | { |
2255 | int i; |
2256 | |
2257 | assert(!ssh->queueing); |
2258 | |
2259 | for (i = 0; i < ssh->queuelen; i++) |
97ab28d7 |
2260 | ssh2_pkt_defer_noqueue(ssh, ssh->queue[i], FALSE); |
590f6a5f |
2261 | ssh->queuelen = 0; |
2262 | |
2263 | ssh_pkt_defersend(ssh); |
2264 | } |
2265 | |
7cca0d81 |
2266 | #if 0 |
32874aea |
2267 | void bndebug(char *string, Bignum b) |
2268 | { |
7cca0d81 |
2269 | unsigned char *p; |
2270 | int i, len; |
2271 | p = ssh2_mpint_fmt(b, &len); |
2272 | debug(("%s", string)); |
2273 | for (i = 0; i < len; i++) |
32874aea |
2274 | debug((" %02x", p[i])); |
765c4200 |
2275 | debug(("\n")); |
dcbde236 |
2276 | sfree(p); |
7cca0d81 |
2277 | } |
2278 | #endif |
2279 | |
b672f405 |
2280 | static void hash_mpint(const struct ssh_hash *h, void *s, Bignum b) |
32874aea |
2281 | { |
7cca0d81 |
2282 | unsigned char *p; |
2283 | int len; |
2284 | p = ssh2_mpint_fmt(b, &len); |
b672f405 |
2285 | hash_string(h, s, p, len); |
dcbde236 |
2286 | sfree(p); |
7cca0d81 |
2287 | } |
2288 | |
2289 | /* |
2e85c969 |
2290 | * Packet decode functions for both SSH-1 and SSH-2. |
7cca0d81 |
2291 | */ |
ff3187f6 |
2292 | static unsigned long ssh_pkt_getuint32(struct Packet *pkt) |
32874aea |
2293 | { |
7cca0d81 |
2294 | unsigned long value; |
ff3187f6 |
2295 | if (pkt->length - pkt->savedpos < 4) |
32874aea |
2296 | return 0; /* arrgh, no way to decline (FIXME?) */ |
ff3187f6 |
2297 | value = GET_32BIT(pkt->body + pkt->savedpos); |
2298 | pkt->savedpos += 4; |
7cca0d81 |
2299 | return value; |
2300 | } |
ff3187f6 |
2301 | static int ssh2_pkt_getbool(struct Packet *pkt) |
32874aea |
2302 | { |
65a22376 |
2303 | unsigned long value; |
ff3187f6 |
2304 | if (pkt->length - pkt->savedpos < 1) |
32874aea |
2305 | return 0; /* arrgh, no way to decline (FIXME?) */ |
ff3187f6 |
2306 | value = pkt->body[pkt->savedpos] != 0; |
2307 | pkt->savedpos++; |
65a22376 |
2308 | return value; |
2309 | } |
ff3187f6 |
2310 | static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length) |
32874aea |
2311 | { |
57356d63 |
2312 | int len; |
7cca0d81 |
2313 | *p = NULL; |
45068b27 |
2314 | *length = 0; |
ff3187f6 |
2315 | if (pkt->length - pkt->savedpos < 4) |
32874aea |
2316 | return; |
b1650067 |
2317 | len = toint(GET_32BIT(pkt->body + pkt->savedpos)); |
57356d63 |
2318 | if (len < 0) |
2319 | return; |
2320 | *length = len; |
ff3187f6 |
2321 | pkt->savedpos += 4; |
2322 | if (pkt->length - pkt->savedpos < *length) |
32874aea |
2323 | return; |
ff3187f6 |
2324 | *p = (char *)(pkt->body + pkt->savedpos); |
2325 | pkt->savedpos += *length; |
7cca0d81 |
2326 | } |
ff3187f6 |
2327 | static void *ssh_pkt_getdata(struct Packet *pkt, int length) |
0016d70b |
2328 | { |
ff3187f6 |
2329 | if (pkt->length - pkt->savedpos < length) |
0016d70b |
2330 | return NULL; |
ff3187f6 |
2331 | pkt->savedpos += length; |
2332 | return pkt->body + (pkt->savedpos - length); |
0016d70b |
2333 | } |
ff3187f6 |
2334 | static int ssh1_pkt_getrsakey(struct Packet *pkt, struct RSAKey *key, |
0016d70b |
2335 | unsigned char **keystr) |
2336 | { |
2337 | int j; |
2338 | |
ff3187f6 |
2339 | j = makekey(pkt->body + pkt->savedpos, |
2340 | pkt->length - pkt->savedpos, |
0016d70b |
2341 | key, keystr, 0); |
2342 | |
2343 | if (j < 0) |
2344 | return FALSE; |
2345 | |
ff3187f6 |
2346 | pkt->savedpos += j; |
2347 | assert(pkt->savedpos < pkt->length); |
0016d70b |
2348 | |
2349 | return TRUE; |
2350 | } |
ff3187f6 |
2351 | static Bignum ssh1_pkt_getmp(struct Packet *pkt) |
0016d70b |
2352 | { |
2353 | int j; |
2354 | Bignum b; |
2355 | |
ff3187f6 |
2356 | j = ssh1_read_bignum(pkt->body + pkt->savedpos, |
2357 | pkt->length - pkt->savedpos, &b); |
0016d70b |
2358 | |
2359 | if (j < 0) |
2360 | return NULL; |
2361 | |
ff3187f6 |
2362 | pkt->savedpos += j; |
0016d70b |
2363 | return b; |
2364 | } |
ff3187f6 |
2365 | static Bignum ssh2_pkt_getmp(struct Packet *pkt) |
32874aea |
2366 | { |
7cca0d81 |
2367 | char *p; |
3709bfe9 |
2368 | int length; |
7cca0d81 |
2369 | Bignum b; |
2370 | |
ff3187f6 |
2371 | ssh_pkt_getstring(pkt, &p, &length); |
7cca0d81 |
2372 | if (!p) |
32874aea |
2373 | return NULL; |
ff3187f6 |
2374 | if (p[0] & 0x80) |
32874aea |
2375 | return NULL; |
d8baa528 |
2376 | b = bignum_from_bytes((unsigned char *)p, length); |
7cca0d81 |
2377 | return b; |
2378 | } |
2379 | |
7d503c31 |
2380 | /* |
2e85c969 |
2381 | * Helper function to add an SSH-2 signature blob to a packet. |
1dd353b5 |
2382 | * Expects to be shown the public key blob as well as the signature |
2383 | * blob. Normally works just like ssh2_pkt_addstring, but will |
2384 | * fiddle with the signature packet if necessary for |
2385 | * BUG_SSH2_RSA_PADDING. |
2386 | */ |
ff3187f6 |
2387 | static void ssh2_add_sigblob(Ssh ssh, struct Packet *pkt, |
2388 | void *pkblob_v, int pkblob_len, |
1dd353b5 |
2389 | void *sigblob_v, int sigblob_len) |
2390 | { |
2391 | unsigned char *pkblob = (unsigned char *)pkblob_v; |
2392 | unsigned char *sigblob = (unsigned char *)sigblob_v; |
2393 | |
2394 | /* dmemdump(pkblob, pkblob_len); */ |
2395 | /* dmemdump(sigblob, sigblob_len); */ |
2396 | |
2397 | /* |
2398 | * See if this is in fact an ssh-rsa signature and a buggy |
2399 | * server; otherwise we can just do this the easy way. |
2400 | */ |
b1650067 |
2401 | if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) && pkblob_len > 4+7+4 && |
1dd353b5 |
2402 | (GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) { |
2403 | int pos, len, siglen; |
2404 | |
2405 | /* |
2406 | * Find the byte length of the modulus. |
2407 | */ |
2408 | |
2409 | pos = 4+7; /* skip over "ssh-rsa" */ |
b1650067 |
2410 | len = toint(GET_32BIT(pkblob+pos)); /* get length of exponent */ |
2411 | if (len < 0 || len > pkblob_len - pos - 4) |
2412 | goto give_up; |
2413 | pos += 4 + len; /* skip over exponent */ |
2414 | if (pkblob_len - pos < 4) |
2415 | goto give_up; |
2416 | len = toint(GET_32BIT(pkblob+pos)); /* find length of modulus */ |
2417 | if (len < 0 || len > pkblob_len - pos - 4) |
2418 | goto give_up; |
1dd353b5 |
2419 | pos += 4; /* find modulus itself */ |
2420 | while (len > 0 && pkblob[pos] == 0) |
2421 | len--, pos++; |
2422 | /* debug(("modulus length is %d\n", len)); */ |
2423 | |
2424 | /* |
2425 | * Now find the signature integer. |
2426 | */ |
2427 | pos = 4+7; /* skip over "ssh-rsa" */ |
b1650067 |
2428 | if (sigblob_len < pos+4) |
2429 | goto give_up; |
2430 | siglen = toint(GET_32BIT(sigblob+pos)); |
2431 | if (siglen != sigblob_len - pos - 4) |
2432 | goto give_up; |
1dd353b5 |
2433 | /* debug(("signature length is %d\n", siglen)); */ |
2434 | |
2435 | if (len != siglen) { |
2436 | unsigned char newlen[4]; |
ff3187f6 |
2437 | ssh2_pkt_addstring_start(pkt); |
2438 | ssh2_pkt_addstring_data(pkt, (char *)sigblob, pos); |
1dd353b5 |
2439 | /* dmemdump(sigblob, pos); */ |
2440 | pos += 4; /* point to start of actual sig */ |
2441 | PUT_32BIT(newlen, len); |
ff3187f6 |
2442 | ssh2_pkt_addstring_data(pkt, (char *)newlen, 4); |
1dd353b5 |
2443 | /* dmemdump(newlen, 4); */ |
2444 | newlen[0] = 0; |
2445 | while (len-- > siglen) { |
ff3187f6 |
2446 | ssh2_pkt_addstring_data(pkt, (char *)newlen, 1); |
1dd353b5 |
2447 | /* dmemdump(newlen, 1); */ |
2448 | } |
ff3187f6 |
2449 | ssh2_pkt_addstring_data(pkt, (char *)(sigblob+pos), siglen); |
1dd353b5 |
2450 | /* dmemdump(sigblob+pos, siglen); */ |
2451 | return; |
2452 | } |
2453 | |
b1650067 |
2454 | /* Otherwise fall through and do it the easy way. We also come |
2455 | * here as a fallback if we discover above that the key blob |
2456 | * is misformatted in some way. */ |
2457 | give_up:; |
1dd353b5 |
2458 | } |
2459 | |
ff3187f6 |
2460 | ssh2_pkt_addstring_start(pkt); |
2461 | ssh2_pkt_addstring_data(pkt, (char *)sigblob, sigblob_len); |
1dd353b5 |
2462 | } |
2463 | |
2464 | /* |
7d503c31 |
2465 | * Examine the remote side's version string and compare it against |
2466 | * a list of known buggy implementations. |
2467 | */ |
51470298 |
2468 | static void ssh_detect_bugs(Ssh ssh, char *vstring) |
32874aea |
2469 | { |
2470 | char *imp; /* pointer to implementation part */ |
7d503c31 |
2471 | imp = vstring; |
2472 | imp += strcspn(imp, "-"); |
bd358db1 |
2473 | if (*imp) imp++; |
7d503c31 |
2474 | imp += strcspn(imp, "-"); |
bd358db1 |
2475 | if (*imp) imp++; |
7d503c31 |
2476 | |
51470298 |
2477 | ssh->remote_bugs = 0; |
7d503c31 |
2478 | |
bf982899 |
2479 | /* |
2480 | * General notes on server version strings: |
2481 | * - Not all servers reporting "Cisco-1.25" have all the bugs listed |
2482 | * here -- in particular, we've heard of one that's perfectly happy |
2483 | * with SSH1_MSG_IGNOREs -- but this string never seems to change, |
2484 | * so we can't distinguish them. |
2485 | */ |
4a693cfc |
2486 | if (conf_get_int(ssh->conf, CONF_sshbug_ignore1) == FORCE_ON || |
2487 | (conf_get_int(ssh->conf, CONF_sshbug_ignore1) == AUTO && |
2c9c6388 |
2488 | (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") || |
2489 | !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") || |
46ac09aa |
2490 | !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25") || |
bd0b4caf |
2491 | !strcmp(imp, "OSU_1.4alpha3") || !strcmp(imp, "OSU_1.5alpha4")))) { |
32874aea |
2492 | /* |
2493 | * These versions don't support SSH1_MSG_IGNORE, so we have |
2494 | * to use a different defence against password length |
2495 | * sniffing. |
2496 | */ |
51470298 |
2497 | ssh->remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE; |
2e85c969 |
2498 | logevent("We believe remote version has SSH-1 ignore bug"); |
7d503c31 |
2499 | } |
2500 | |
4a693cfc |
2501 | if (conf_get_int(ssh->conf, CONF_sshbug_plainpw1) == FORCE_ON || |
2502 | (conf_get_int(ssh->conf, CONF_sshbug_plainpw1) == AUTO && |
46ac09aa |
2503 | (!strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3")))) { |
bd358db1 |
2504 | /* |
2505 | * These versions need a plain password sent; they can't |
2506 | * handle having a null and a random length of data after |
2507 | * the password. |
2508 | */ |
51470298 |
2509 | ssh->remote_bugs |= BUG_NEEDS_SSH1_PLAIN_PASSWORD; |
2e85c969 |
2510 | logevent("We believe remote version needs a plain SSH-1 password"); |
bd358db1 |
2511 | } |
2512 | |
4a693cfc |
2513 | if (conf_get_int(ssh->conf, CONF_sshbug_rsa1) == FORCE_ON || |
2514 | (conf_get_int(ssh->conf, CONF_sshbug_rsa1) == AUTO && |
2c9c6388 |
2515 | (!strcmp(imp, "Cisco-1.25")))) { |
0df73905 |
2516 | /* |
2517 | * These versions apparently have no clue whatever about |
2518 | * RSA authentication and will panic and die if they see |
2519 | * an AUTH_RSA message. |
2520 | */ |
51470298 |
2521 | ssh->remote_bugs |= BUG_CHOKES_ON_RSA; |
19f47a7d |
2522 | logevent("We believe remote version can't handle SSH-1 RSA authentication"); |
0df73905 |
2523 | } |
2524 | |
4a693cfc |
2525 | if (conf_get_int(ssh->conf, CONF_sshbug_hmac2) == FORCE_ON || |
2526 | (conf_get_int(ssh->conf, CONF_sshbug_hmac2) == AUTO && |
b9f387af |
2527 | !wc_match("* VShell", imp) && |
831301f6 |
2528 | (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) || |
2529 | wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) || |
2530 | wc_match("2.1 *", imp)))) { |
32874aea |
2531 | /* |
2532 | * These versions have the HMAC bug. |
2533 | */ |
51470298 |
2534 | ssh->remote_bugs |= BUG_SSH2_HMAC; |
2e85c969 |
2535 | logevent("We believe remote version has SSH-2 HMAC bug"); |
7d503c31 |
2536 | } |
1dd353b5 |
2537 | |
4a693cfc |
2538 | if (conf_get_int(ssh->conf, CONF_sshbug_derivekey2) == FORCE_ON || |
2539 | (conf_get_int(ssh->conf, CONF_sshbug_derivekey2) == AUTO && |
b9f387af |
2540 | !wc_match("* VShell", imp) && |
2856a1b9 |
2541 | (wc_match("2.0.0*", imp) || wc_match("2.0.10*", imp) ))) { |
088bde77 |
2542 | /* |
2543 | * These versions have the key-derivation bug (failing to |
2544 | * include the literal shared secret in the hashes that |
2545 | * generate the keys). |
2546 | */ |
51470298 |
2547 | ssh->remote_bugs |= BUG_SSH2_DERIVEKEY; |
2e85c969 |
2548 | logevent("We believe remote version has SSH-2 key-derivation bug"); |
088bde77 |
2549 | } |
2550 | |
4a693cfc |
2551 | if (conf_get_int(ssh->conf, CONF_sshbug_rsapad2) == FORCE_ON || |
2552 | (conf_get_int(ssh->conf, CONF_sshbug_rsapad2) == AUTO && |
831301f6 |
2553 | (wc_match("OpenSSH_2.[5-9]*", imp) || |
2554 | wc_match("OpenSSH_3.[0-2]*", imp)))) { |
1dd353b5 |
2555 | /* |
2e85c969 |
2556 | * These versions have the SSH-2 RSA padding bug. |
1dd353b5 |
2557 | */ |
51470298 |
2558 | ssh->remote_bugs |= BUG_SSH2_RSA_PADDING; |
2e85c969 |
2559 | logevent("We believe remote version has SSH-2 RSA padding bug"); |
1dd353b5 |
2560 | } |
8e975795 |
2561 | |
4a693cfc |
2562 | if (conf_get_int(ssh->conf, CONF_sshbug_pksessid2) == FORCE_ON || |
2563 | (conf_get_int(ssh->conf, CONF_sshbug_pksessid2) == AUTO && |
dda87a28 |
2564 | wc_match("OpenSSH_2.[0-2]*", imp))) { |
2565 | /* |
2e85c969 |
2566 | * These versions have the SSH-2 session-ID bug in |
dda87a28 |
2567 | * public-key authentication. |
2568 | */ |
2569 | ssh->remote_bugs |= BUG_SSH2_PK_SESSIONID; |
2e85c969 |
2570 | logevent("We believe remote version has SSH-2 public-key-session-ID bug"); |
dda87a28 |
2571 | } |
f382c87d |
2572 | |
4a693cfc |
2573 | if (conf_get_int(ssh->conf, CONF_sshbug_rekey2) == FORCE_ON || |
2574 | (conf_get_int(ssh->conf, CONF_sshbug_rekey2) == AUTO && |
39b8712f |
2575 | (wc_match("DigiSSH_2.0", imp) || |
2576 | wc_match("OpenSSH_2.[0-4]*", imp) || |
e12d95a5 |
2577 | wc_match("OpenSSH_2.5.[0-3]*", imp) || |
2578 | wc_match("Sun_SSH_1.0", imp) || |
f4275b53 |
2579 | wc_match("Sun_SSH_1.0.1", imp) || |
96bd26de |
2580 | /* All versions <= 1.2.6 (they changed their format in 1.2.7) */ |
2581 | wc_match("WeOnlyDo-*", imp)))) { |
f382c87d |
2582 | /* |
2e85c969 |
2583 | * These versions have the SSH-2 rekey bug. |
f382c87d |
2584 | */ |
2585 | ssh->remote_bugs |= BUG_SSH2_REKEY; |
2e85c969 |
2586 | logevent("We believe remote version has SSH-2 rekey bug"); |
f382c87d |
2587 | } |
c9739dba |
2588 | |
4a693cfc |
2589 | if (conf_get_int(ssh->conf, CONF_sshbug_maxpkt2) == FORCE_ON || |
2590 | (conf_get_int(ssh->conf, CONF_sshbug_maxpkt2) == AUTO && |
5ca836f2 |
2591 | (wc_match("1.36_sshlib GlobalSCAPE", imp) || |
2592 | wc_match("1.36 sshlib: GlobalScape", imp)))) { |
c9739dba |
2593 | /* |
2594 | * This version ignores our makpkt and needs to be throttled. |
2595 | */ |
2596 | ssh->remote_bugs |= BUG_SSH2_MAXPKT; |
2597 | logevent("We believe remote version ignores SSH-2 maximum packet size"); |
2598 | } |
cf6ddb95 |
2599 | |
4a693cfc |
2600 | if (conf_get_int(ssh->conf, CONF_sshbug_ignore2) == FORCE_ON) { |
cf6ddb95 |
2601 | /* |
2602 | * Servers that don't support SSH2_MSG_IGNORE. Currently, |
2603 | * none detected automatically. |
2604 | */ |
2605 | ssh->remote_bugs |= BUG_CHOKES_ON_SSH2_IGNORE; |
2606 | logevent("We believe remote version has SSH-2 ignore bug"); |
2607 | } |
2e0aae4f |
2608 | |
2609 | if (conf_get_int(ssh->conf, CONF_sshbug_winadj) == FORCE_ON) { |
2610 | /* |
2611 | * Servers that don't support our winadj request for one |
2612 | * reason or another. Currently, none detected automatically. |
2613 | */ |
2614 | ssh->remote_bugs |= BUG_CHOKES_ON_WINADJ; |
2615 | logevent("We believe remote version has winadj bug"); |
2616 | } |
7d503c31 |
2617 | } |
2618 | |
d38d6a1f |
2619 | /* |
2620 | * The `software version' part of an SSH version string is required |
2621 | * to contain no spaces or minus signs. |
2622 | */ |
2623 | static void ssh_fix_verstring(char *str) |
2624 | { |
2625 | /* Eat "SSH-<protoversion>-". */ |
2626 | assert(*str == 'S'); str++; |
2627 | assert(*str == 'S'); str++; |
2628 | assert(*str == 'H'); str++; |
2629 | assert(*str == '-'); str++; |
2630 | while (*str && *str != '-') str++; |
2631 | assert(*str == '-'); str++; |
2632 | |
2633 | /* Convert minus signs and spaces in the remaining string into |
2634 | * underscores. */ |
2635 | while (*str) { |
2636 | if (*str == '-' || *str == ' ') |
2637 | *str = '_'; |
2638 | str++; |
2639 | } |
2640 | } |
2641 | |
1fcd0d98 |
2642 | /* |
2643 | * Send an appropriate SSH version string. |
2644 | */ |
2645 | static void ssh_send_verstring(Ssh ssh, char *svers) |
2646 | { |
2647 | char *verstring; |
2648 | |
2649 | if (ssh->version == 2) { |
2650 | /* |
2651 | * Construct a v2 version string. |
2652 | */ |
2653 | verstring = dupprintf("SSH-2.0-%s\015\012", sshver); |
2654 | } else { |
2655 | /* |
2656 | * Construct a v1 version string. |
2657 | */ |
2658 | verstring = dupprintf("SSH-%s-%s\012", |
2659 | (ssh_versioncmp(svers, "1.5") <= 0 ? |
2660 | svers : "1.5"), |
2661 | sshver); |
2662 | } |
2663 | |
2664 | ssh_fix_verstring(verstring); |
2665 | |
2666 | if (ssh->version == 2) { |
2667 | size_t len; |
2668 | /* |
2669 | * Record our version string. |
2670 | */ |
2671 | len = strcspn(verstring, "\015\012"); |
2672 | ssh->v_c = snewn(len + 1, char); |
2673 | memcpy(ssh->v_c, verstring, len); |
2674 | ssh->v_c[len] = 0; |
2675 | } |
2676 | |
2677 | logeventf(ssh, "We claim version: %.*s", |
2678 | strcspn(verstring, "\015\012"), verstring); |
2679 | s_write(ssh, verstring, strlen(verstring)); |
2680 | sfree(verstring); |
2681 | } |
2682 | |
51470298 |
2683 | static int do_ssh_init(Ssh ssh, unsigned char c) |
32874aea |
2684 | { |
51470298 |
2685 | struct do_ssh_init_state { |
30919997 |
2686 | int crLine; |
51470298 |
2687 | int vslen; |
2688 | char version[10]; |
2689 | char *vstring; |
2690 | int vstrsize; |
2691 | int i; |
2692 | int proto1, proto2; |
2693 | }; |
2694 | crState(do_ssh_init_state); |
30919997 |
2695 | |
2696 | crBeginState; |
8df7a775 |
2697 | |
8c1835c0 |
2698 | /* Search for a line beginning with the string "SSH-" in the input. */ |
2699 | for (;;) { |
2700 | if (c != 'S') goto no; |
2701 | crReturn(1); |
2702 | if (c != 'S') goto no; |
2703 | crReturn(1); |
2704 | if (c != 'H') goto no; |
2705 | crReturn(1); |
2706 | if (c != '-') goto no; |
2707 | break; |
2708 | no: |
2709 | while (c != '\012') |
2710 | crReturn(1); |
2711 | crReturn(1); |
374330e2 |
2712 | } |
8df7a775 |
2713 | |
51470298 |
2714 | s->vstrsize = 16; |
3d88e64d |
2715 | s->vstring = snewn(s->vstrsize, char); |
51470298 |
2716 | strcpy(s->vstring, "SSH-"); |
2717 | s->vslen = 4; |
2718 | s->i = 0; |
374330e2 |
2719 | while (1) { |
8df7a775 |
2720 | crReturn(1); /* get another char */ |
51470298 |
2721 | if (s->vslen >= s->vstrsize - 1) { |
2722 | s->vstrsize += 16; |
3d88e64d |
2723 | s->vstring = sresize(s->vstring, s->vstrsize, char); |
32874aea |
2724 | } |
51470298 |
2725 | s->vstring[s->vslen++] = c; |
2726 | if (s->i >= 0) { |
374330e2 |
2727 | if (c == '-') { |
51470298 |
2728 | s->version[s->i] = '\0'; |
2729 | s->i = -1; |
2730 | } else if (s->i < sizeof(s->version) - 1) |
2731 | s->version[s->i++] = c; |
c4ffc4d0 |
2732 | } else if (c == '\012') |
374330e2 |
2733 | break; |
2734 | } |
2735 | |
51470298 |
2736 | ssh->agentfwd_enabled = FALSE; |
2737 | ssh->rdpkt2_state.incoming_sequence = 0; |
960e736a |
2738 | |
51470298 |
2739 | s->vstring[s->vslen] = 0; |
a7d4653a |
2740 | s->vstring[strcspn(s->vstring, "\015\012")] = '\0';/* remove EOL chars */ |
fb983202 |
2741 | logeventf(ssh, "Server version: %s", s->vstring); |
51470298 |
2742 | ssh_detect_bugs(ssh, s->vstring); |
c5e9c988 |
2743 | |
adf799dd |
2744 | /* |
38d228a2 |
2745 | * Decide which SSH protocol version to support. |
adf799dd |
2746 | */ |
38d228a2 |
2747 | |
2748 | /* Anything strictly below "2.0" means protocol 1 is supported. */ |
51470298 |
2749 | s->proto1 = ssh_versioncmp(s->version, "2.0") < 0; |
38d228a2 |
2750 | /* Anything greater or equal to "1.99" means protocol 2 is supported. */ |
51470298 |
2751 | s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0; |
38d228a2 |
2752 | |
4a693cfc |
2753 | if (conf_get_int(ssh->conf, CONF_sshprot) == 0 && !s->proto1) { |
6b5cf8b4 |
2754 | bombout(("SSH protocol version 1 required by user but not provided by server")); |
7ffdbc1a |
2755 | crStop(0); |
38d228a2 |
2756 | } |
4a693cfc |
2757 | if (conf_get_int(ssh->conf, CONF_sshprot) == 3 && !s->proto2) { |
6b5cf8b4 |
2758 | bombout(("SSH protocol version 2 required by user but not provided by server")); |
7ffdbc1a |
2759 | crStop(0); |
38d228a2 |
2760 | } |
2761 | |
4a693cfc |
2762 | if (s->proto2 && (conf_get_int(ssh->conf, CONF_sshprot) >= 2 || !s->proto1)) |
1fcd0d98 |
2763 | ssh->version = 2; |
2764 | else |
2765 | ssh->version = 1; |
d38d6a1f |
2766 | |
2767 | logeventf(ssh, "Using SSH protocol version %d", ssh->version); |
2768 | |
1fcd0d98 |
2769 | /* Send the version string, if we haven't already */ |
4a693cfc |
2770 | if (conf_get_int(ssh->conf, CONF_sshprot) != 3) |
1fcd0d98 |
2771 | ssh_send_verstring(ssh, s->version); |
2772 | |
2773 | if (ssh->version == 2) { |
2774 | size_t len; |
2775 | /* |
2776 | * Record their version string. |
2777 | */ |
2778 | len = strcspn(s->vstring, "\015\012"); |
2779 | ssh->v_s = snewn(len + 1, char); |
2780 | memcpy(ssh->v_s, s->vstring, len); |
2781 | ssh->v_s[len] = 0; |
2782 | |
2783 | /* |
2784 | * Initialise SSH-2 protocol. |
2785 | */ |
2786 | ssh->protocol = ssh2_protocol; |
2787 | ssh2_protocol_setup(ssh); |
2788 | ssh->s_rdpkt = ssh2_rdpkt; |
2789 | } else { |
2790 | /* |
2791 | * Initialise SSH-1 protocol. |
2792 | */ |
2793 | ssh->protocol = ssh1_protocol; |
2794 | ssh1_protocol_setup(ssh); |
2795 | ssh->s_rdpkt = ssh1_rdpkt; |
2796 | } |
2797 | if (ssh->version == 2) |
2798 | do_ssh2_transport(ssh, NULL, -1, NULL); |
2799 | |
125105d1 |
2800 | update_specials_menu(ssh->frontend); |
51470298 |
2801 | ssh->state = SSH_STATE_BEFORE_SIZE; |
4a693cfc |
2802 | ssh->pinger = pinger_new(ssh->conf, &ssh_backend, ssh); |
8df7a775 |
2803 | |
51470298 |
2804 | sfree(s->vstring); |
50526e47 |
2805 | |
8df7a775 |
2806 | crFinish(0); |
2807 | } |
2808 | |
3d9449a1 |
2809 | static void ssh_process_incoming_data(Ssh ssh, |
2810 | unsigned char **data, int *datalen) |
2811 | { |
bf8a49a1 |
2812 | struct Packet *pktin; |
2813 | |
2814 | pktin = ssh->s_rdpkt(ssh, data, datalen); |
3d9449a1 |
2815 | if (pktin) { |
2816 | ssh->protocol(ssh, NULL, 0, pktin); |
2817 | ssh_free_packet(pktin); |
2818 | } |
2819 | } |
2820 | |
2821 | static void ssh_queue_incoming_data(Ssh ssh, |
2822 | unsigned char **data, int *datalen) |
2823 | { |
2824 | bufchain_add(&ssh->queued_incoming_data, *data, *datalen); |
2825 | *data += *datalen; |
2826 | *datalen = 0; |
2827 | } |
2828 | |
2829 | static void ssh_process_queued_incoming_data(Ssh ssh) |
2830 | { |
2831 | void *vdata; |
2832 | unsigned char *data; |
2833 | int len, origlen; |
2834 | |
2835 | while (!ssh->frozen && bufchain_size(&ssh->queued_incoming_data)) { |
2836 | bufchain_prefix(&ssh->queued_incoming_data, &vdata, &len); |
2837 | data = vdata; |
2838 | origlen = len; |
2839 | |
2840 | while (!ssh->frozen && len > 0) |
2841 | ssh_process_incoming_data(ssh, &data, &len); |
2842 | |
2843 | if (origlen > len) |
2844 | bufchain_consume(&ssh->queued_incoming_data, origlen - len); |
2845 | } |
2846 | } |
2847 | |
2848 | static void ssh_set_frozen(Ssh ssh, int frozen) |
2849 | { |
a5a6f839 |
2850 | if (ssh->s) |
2851 | sk_set_frozen(ssh->s, frozen); |
3d9449a1 |
2852 | ssh->frozen = frozen; |
2853 | } |
2854 | |
51470298 |
2855 | static void ssh_gotdata(Ssh ssh, unsigned char *data, int datalen) |
8df7a775 |
2856 | { |
bf8a49a1 |
2857 | /* Log raw data, if we're in that mode. */ |
ff05dfef |
2858 | if (ssh->logctx) |
2859 | log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, datalen, |
0d13bfcd |
2860 | 0, NULL, NULL); |
bf8a49a1 |
2861 | |
51470298 |
2862 | crBegin(ssh->ssh_gotdata_crstate); |
8df7a775 |
2863 | |
2864 | /* |
2865 | * To begin with, feed the characters one by one to the |
2866 | * protocol initialisation / selection function do_ssh_init(). |
2867 | * When that returns 0, we're done with the initial greeting |
2868 | * exchange and can move on to packet discipline. |
2869 | */ |
2870 | while (1) { |
51470298 |
2871 | int ret; /* need not be kept across crReturn */ |
8df7a775 |
2872 | if (datalen == 0) |
2873 | crReturnV; /* more data please */ |
51470298 |
2874 | ret = do_ssh_init(ssh, *data); |
32874aea |
2875 | data++; |
2876 | datalen--; |
8df7a775 |
2877 | if (ret == 0) |
2878 | break; |
2879 | } |
2880 | |
2881 | /* |
2882 | * We emerge from that loop when the initial negotiation is |
2883 | * over and we have selected an s_rdpkt function. Now pass |
2884 | * everything to s_rdpkt, and then pass the resulting packets |
2885 | * to the proper protocol handler. |
2886 | */ |
3d9449a1 |
2887 | |
8df7a775 |
2888 | while (1) { |
e375106c |
2889 | while (bufchain_size(&ssh->queued_incoming_data) > 0 || datalen > 0) { |
2890 | if (ssh->frozen) { |
3d9449a1 |
2891 | ssh_queue_incoming_data(ssh, &data, &datalen); |
e375106c |
2892 | /* This uses up all data and cannot cause anything interesting |
2893 | * to happen; indeed, for anything to happen at all, we must |
2894 | * return, so break out. */ |
2895 | break; |
2896 | } else if (bufchain_size(&ssh->queued_incoming_data) > 0) { |
2897 | /* This uses up some or all data, and may freeze the |
2898 | * session. */ |
2899 | ssh_process_queued_incoming_data(ssh); |
2900 | } else { |
2901 | /* This uses up some or all data, and may freeze the |
2902 | * session. */ |
2903 | ssh_process_incoming_data(ssh, &data, &datalen); |
2904 | } |
2905 | /* FIXME this is probably EBW. */ |
ff3187f6 |
2906 | if (ssh->state == SSH_STATE_CLOSED) |
2907 | return; |
8df7a775 |
2908 | } |
e375106c |
2909 | /* We're out of data. Go and get some more. */ |
8df7a775 |
2910 | crReturnV; |
2911 | } |
2912 | crFinishV; |
2913 | } |
2914 | |
ac934965 |
2915 | static int ssh_do_close(Ssh ssh, int notify_exit) |
32874aea |
2916 | { |
80ffa58b |
2917 | int ret = 0; |
36f94d1f |
2918 | struct ssh_channel *c; |
2919 | |
51470298 |
2920 | ssh->state = SSH_STATE_CLOSED; |
ecbb0000 |
2921 | expire_timer_context(ssh); |
51470298 |
2922 | if (ssh->s) { |
2923 | sk_close(ssh->s); |
2924 | ssh->s = NULL; |
ac934965 |
2925 | if (notify_exit) |
2926 | notify_remote_exit(ssh->frontend); |
2927 | else |
2928 | ret = 1; |
f3ab576e |
2929 | } |
36f94d1f |
2930 | /* |
80ffa58b |
2931 | * Now we must shut down any port- and X-forwarded channels going |
36f94d1f |
2932 | * through this connection. |
2933 | */ |
74a98066 |
2934 | if (ssh->channels) { |
80ffa58b |
2935 | while (NULL != (c = index234(ssh->channels, 0))) { |
74a98066 |
2936 | switch (c->type) { |
2937 | case CHAN_X11: |
2938 | x11_close(c->u.x11.s); |
2939 | break; |
2940 | case CHAN_SOCKDATA: |
409eca86 |
2941 | case CHAN_SOCKDATA_DORMANT: |
74a98066 |
2942 | pfd_close(c->u.pfd.s); |
2943 | break; |
2944 | } |
80ffa58b |
2945 | del234(ssh->channels, c); /* moving next one to index 0 */ |
74a98066 |
2946 | if (ssh->version == 2) |
2947 | bufchain_clear(&c->v.v2.outbuffer); |
2948 | sfree(c); |
36f94d1f |
2949 | } |
36f94d1f |
2950 | } |
f8c9f9df |
2951 | /* |
2952 | * Go through port-forwardings, and close any associated |
2953 | * listening sockets. |
2954 | */ |
2955 | if (ssh->portfwds) { |
2956 | struct ssh_portfwd *pf; |
2957 | while (NULL != (pf = index234(ssh->portfwds, 0))) { |
2958 | /* Dispose of any listening socket. */ |
2959 | if (pf->local) |
2960 | pfd_terminate(pf->local); |
2961 | del234(ssh->portfwds, pf); /* moving next one to index 0 */ |
2962 | free_portfwd(pf); |
2963 | } |
b72bdf11 |
2964 | freetree234(ssh->portfwds); |
2965 | ssh->portfwds = NULL; |
f8c9f9df |
2966 | } |
ac934965 |
2967 | |
2968 | return ret; |
36f94d1f |
2969 | } |
2970 | |
7555d6a5 |
2971 | static void ssh_log(Plug plug, int type, SockAddr addr, int port, |
2972 | const char *error_msg, int error_code) |
2973 | { |
2974 | Ssh ssh = (Ssh) plug; |
2975 | char addrbuf[256], *msg; |
2976 | |
2977 | sk_getaddr(addr, addrbuf, lenof(addrbuf)); |
2978 | |
2979 | if (type == 0) |
2980 | msg = dupprintf("Connecting to %s port %d", addrbuf, port); |
2981 | else |
2982 | msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); |
2983 | |
2984 | logevent(msg); |
fb983202 |
2985 | sfree(msg); |
7555d6a5 |
2986 | } |
2987 | |
cbe2d68f |
2988 | static int ssh_closing(Plug plug, const char *error_msg, int error_code, |
36f94d1f |
2989 | int calling_back) |
2990 | { |
2991 | Ssh ssh = (Ssh) plug; |
ac934965 |
2992 | int need_notify = ssh_do_close(ssh, FALSE); |
2993 | |
9e296bfa |
2994 | if (!error_msg) { |
2995 | if (!ssh->close_expected) |
2996 | error_msg = "Server unexpectedly closed network connection"; |
2997 | else |
2998 | error_msg = "Server closed network connection"; |
ac934965 |
2999 | } |
3000 | |
d051d1b4 |
3001 | if (ssh->close_expected && ssh->clean_exit && ssh->exitcode < 0) |
3002 | ssh->exitcode = 0; |
3003 | |
2425184b |
3004 | if (need_notify) |
3005 | notify_remote_exit(ssh->frontend); |
3006 | |
9e296bfa |
3007 | if (error_msg) |
247308b5 |
3008 | logevent(error_msg); |
9e296bfa |
3009 | if (!ssh->close_expected || !ssh->clean_exit) |
971bcc0a |
3010 | connection_fatal(ssh->frontend, "%s", error_msg); |
7e78000d |
3011 | return 0; |
3012 | } |
3013 | |
32874aea |
3014 | static int ssh_receive(Plug plug, int urgent, char *data, int len) |
3015 | { |
51470298 |
3016 | Ssh ssh = (Ssh) plug; |
d8baa528 |
3017 | ssh_gotdata(ssh, (unsigned char *)data, len); |
51470298 |
3018 | if (ssh->state == SSH_STATE_CLOSED) { |
ac934965 |
3019 | ssh_do_close(ssh, TRUE); |
32874aea |
3020 | return 0; |
3257deae |
3021 | } |
fef97f43 |
3022 | return 1; |
374330e2 |
3023 | } |
3024 | |
5471d09a |
3025 | static void ssh_sent(Plug plug, int bufsize) |
3026 | { |
51470298 |
3027 | Ssh ssh = (Ssh) plug; |
5471d09a |
3028 | /* |
3029 | * If the send backlog on the SSH socket itself clears, we |
3030 | * should unthrottle the whole world if it was throttled. |
3031 | */ |
3032 | if (bufsize < SSH_MAX_BACKLOG) |
51470298 |
3033 | ssh_throttle_all(ssh, 0, bufsize); |
5471d09a |
3034 | } |
3035 | |
fb09bf1c |
3036 | /* |
8df7a775 |
3037 | * Connect to specified host and port. |
3038 | * Returns an error message, or NULL on success. |
6e1ebb76 |
3039 | * Also places the canonical host name into `realhost'. It must be |
3040 | * freed by the caller. |
8df7a775 |
3041 | */ |
cbe2d68f |
3042 | static const char *connect_to_host(Ssh ssh, char *host, int port, |
79bf227b |
3043 | char **realhost, int nodelay, int keepalive) |
8df7a775 |
3044 | { |
51470298 |
3045 | static const struct plug_function_table fn_table = { |
7555d6a5 |
3046 | ssh_log, |
7e78000d |
3047 | ssh_closing, |
5471d09a |
3048 | ssh_receive, |
3049 | ssh_sent, |
3050 | NULL |
51470298 |
3051 | }; |
7e78000d |
3052 | |
8df7a775 |
3053 | SockAddr addr; |
cbe2d68f |
3054 | const char *err; |
4a693cfc |
3055 | char *loghost; |
3056 | int addressfamily, sshprot; |
3057 | |
3058 | loghost = conf_get_str(ssh->conf, CONF_loghost); |
3059 | if (*loghost) { |
881da168 |
3060 | char *colon; |
8df7a775 |
3061 | |
4a693cfc |
3062 | ssh->savedhost = dupstr(loghost); |
881da168 |
3063 | ssh->savedport = 22; /* default ssh port */ |
3064 | |
3065 | /* |
3066 | * A colon suffix on savedhost also lets us affect |
3067 | * savedport. |
3068 | * |
3069 | * (FIXME: do something about IPv6 address literals here.) |
3070 | */ |
3071 | colon = strrchr(ssh->savedhost, ':'); |
3072 | if (colon) { |
3073 | *colon++ = '\0'; |
3074 | if (*colon) |
3075 | ssh->savedport = atoi(colon); |
3076 | } |
3077 | } else { |
3078 | ssh->savedhost = dupstr(host); |
3079 | if (port < 0) |
3080 | port = 22; /* default ssh port */ |
3081 | ssh->savedport = port; |
3082 | } |
8df7a775 |
3083 | |
3084 | /* |
3085 | * Try to find host. |
3086 | */ |
4a693cfc |
3087 | addressfamily = conf_get_int(ssh->conf, CONF_addressfamily); |
05581745 |
3088 | logeventf(ssh, "Looking up host \"%s\"%s", host, |
4a693cfc |
3089 | (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : |
3090 | (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); |
3091 | addr = name_lookup(host, port, realhost, ssh->conf, addressfamily); |
170c1e6e |
3092 | if ((err = sk_addr_error(addr)) != NULL) { |
3093 | sk_addr_free(addr); |
8df7a775 |
3094 | return err; |
170c1e6e |
3095 | } |
42af6a67 |
3096 | ssh->fullhostname = dupstr(*realhost); /* save in case of GSSAPI */ |
8df7a775 |
3097 | |
8df7a775 |
3098 | /* |
3099 | * Open socket. |
3100 | */ |
51470298 |
3101 | ssh->fn = &fn_table; |
e8fa8f62 |
3102 | ssh->s = new_connection(addr, *realhost, port, |
4a693cfc |
3103 | 0, 1, nodelay, keepalive, (Plug) ssh, ssh->conf); |
70e5d0fd |
3104 | if ((err = sk_socket_error(ssh->s)) != NULL) { |
51470298 |
3105 | ssh->s = NULL; |
39934deb |
3106 | notify_remote_exit(ssh->frontend); |
8df7a775 |
3107 | return err; |
67c4ba2e |
3108 | } |
8df7a775 |
3109 | |
ff05dfef |
3110 | /* |
3111 | * If the SSH version number's fixed, set it now, and if it's SSH-2, |
3112 | * send the version string too. |
3113 | */ |
4a693cfc |
3114 | sshprot = conf_get_int(ssh->conf, CONF_sshprot); |
3115 | if (sshprot == 0) |
ff05dfef |
3116 | ssh->version = 1; |
4a693cfc |
3117 | if (sshprot == 3) { |
ff05dfef |
3118 | ssh->version = 2; |
3119 | ssh_send_verstring(ssh, NULL); |
3120 | } |
3121 | |
881da168 |
3122 | /* |
3123 | * loghost, if configured, overrides realhost. |
3124 | */ |
4a693cfc |
3125 | if (*loghost) { |
881da168 |
3126 | sfree(*realhost); |
4a693cfc |
3127 | *realhost = dupstr(loghost); |
881da168 |
3128 | } |
3129 | |
8df7a775 |
3130 | return NULL; |
3131 | } |
3132 | |
3133 | /* |
5471d09a |
3134 | * Throttle or unthrottle the SSH connection. |
3135 | */ |
ff68564b |
3136 | static void ssh_throttle_conn(Ssh ssh, int adjust) |
5471d09a |
3137 | { |
ff68564b |
3138 | int old_count = ssh->conn_throttle_count; |
3139 | ssh->conn_throttle_count += adjust; |
3140 | assert(ssh->conn_throttle_count >= 0); |
3141 | if (ssh->conn_throttle_count && !old_count) { |
3d9449a1 |
3142 | ssh_set_frozen(ssh, 1); |
ff68564b |
3143 | } else if (!ssh->conn_throttle_count && old_count) { |
3d9449a1 |
3144 | ssh_set_frozen(ssh, 0); |
5471d09a |
3145 | } |
3146 | } |
3147 | |
3148 | /* |
3149 | * Throttle or unthrottle _all_ local data streams (for when sends |
3150 | * on the SSH connection itself back up). |
3151 | */ |
51470298 |
3152 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize) |
5471d09a |
3153 | { |
3154 | int i; |
3155 | struct ssh_channel *c; |
3156 | |
51470298 |
3157 | if (enable == ssh->throttled_all) |
5471d09a |
3158 | return; |
51470298 |
3159 | ssh->throttled_all = enable; |
3160 | ssh->overall_bufsize = bufsize; |
3161 | if (!ssh->channels) |
5471d09a |
3162 | return; |
51470298 |
3163 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
5471d09a |
3164 | switch (c->type) { |
3165 | case CHAN_MAINSESSION: |
3166 | /* |
3167 | * This is treated separately, outside the switch. |
3168 | */ |
3169 | break; |
3170 | case CHAN_X11: |
3171 | x11_override_throttle(c->u.x11.s, enable); |
3172 | break; |
3173 | case CHAN_AGENT: |
3174 | /* Agent channels require no buffer management. */ |
3175 | break; |
3176 | case CHAN_SOCKDATA: |
36f94d1f |
3177 | pfd_override_throttle(c->u.pfd.s, enable); |
5471d09a |
3178 | break; |
3179 | } |
3180 | } |
3181 | } |
3182 | |
f11d78f2 |
3183 | static void ssh_agent_callback(void *sshv, void *reply, int replylen) |
839f10db |
3184 | { |
3185 | Ssh ssh = (Ssh) sshv; |
3186 | |
3187 | ssh->agent_response = reply; |
3188 | ssh->agent_response_len = replylen; |
3189 | |
3190 | if (ssh->version == 1) |
ff3187f6 |
3191 | do_ssh1_login(ssh, NULL, -1, NULL); |
839f10db |
3192 | else |
ff3187f6 |
3193 | do_ssh2_authconn(ssh, NULL, -1, NULL); |
839f10db |
3194 | } |
3195 | |
3d9449a1 |
3196 | static void ssh_dialog_callback(void *sshv, int ret) |
3197 | { |
3198 | Ssh ssh = (Ssh) sshv; |
3199 | |
3200 | ssh->user_response = ret; |
3201 | |
3202 | if (ssh->version == 1) |
3203 | do_ssh1_login(ssh, NULL, -1, NULL); |
3204 | else |
3205 | do_ssh2_transport(ssh, NULL, -1, NULL); |
3206 | |
3207 | /* |
3208 | * This may have unfrozen the SSH connection, so do a |
3209 | * queued-data run. |
3210 | */ |
3211 | ssh_process_queued_incoming_data(ssh); |
3212 | } |
3213 | |
f11d78f2 |
3214 | static void ssh_agentf_callback(void *cv, void *reply, int replylen) |
839f10db |
3215 | { |
3216 | struct ssh_channel *c = (struct ssh_channel *)cv; |
3217 | Ssh ssh = c->ssh; |
3218 | void *sentreply = reply; |
3219 | |
be3f980d |
3220 | c->u.a.outstanding_requests--; |
839f10db |
3221 | if (!sentreply) { |
3222 | /* Fake SSH_AGENT_FAILURE. */ |
3223 | sentreply = "\0\0\0\1\5"; |
3224 | replylen = 5; |
3225 | } |
3226 | if (ssh->version == 2) { |
3227 | ssh2_add_channel_data(c, sentreply, replylen); |
3228 | ssh2_try_send(c); |
3229 | } else { |
3230 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
3231 | PKT_INT, c->remoteid, |
3232 | PKT_INT, replylen, |
a2724fba |
3233 | PKTT_DATA, |
839f10db |
3234 | PKT_DATA, sentreply, replylen, |
9a10ecf4 |
3235 | PKTT_OTHER, |
839f10db |
3236 | PKT_END); |
3237 | } |
3238 | if (reply) |
3239 | sfree(reply); |
be3f980d |
3240 | /* |
3241 | * If we've already seen an incoming EOF but haven't sent an |
3242 | * outgoing one, this may be the moment to send it. |
3243 | */ |
3244 | if (c->u.a.outstanding_requests == 0 && (c->closes & CLOSES_RCVD_EOF)) |
3245 | sshfwd_write_eof(c); |
839f10db |
3246 | } |
3247 | |
0405e71f |
3248 | /* |
9e296bfa |
3249 | * Client-initiated disconnection. Send a DISCONNECT if `wire_reason' |
3250 | * non-NULL, otherwise just close the connection. `client_reason' == NULL |
3251 | * => log `wire_reason'. |
3252 | */ |
3253 | static void ssh_disconnect(Ssh ssh, char *client_reason, char *wire_reason, |
3254 | int code, int clean_exit) |
3255 | { |
3256 | char *error; |
3257 | if (!client_reason) |
3258 | client_reason = wire_reason; |
3259 | if (client_reason) |
3260 | error = dupprintf("Disconnected: %s", client_reason); |
3261 | else |
3262 | error = dupstr("Disconnected"); |
3263 | if (wire_reason) { |
3264 | if (ssh->version == 1) { |
3265 | send_packet(ssh, SSH1_MSG_DISCONNECT, PKT_STR, wire_reason, |
3266 | PKT_END); |
3267 | } else if (ssh->version == 2) { |
3268 | struct Packet *pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
3269 | ssh2_pkt_adduint32(pktout, code); |
3270 | ssh2_pkt_addstring(pktout, wire_reason); |
3271 | ssh2_pkt_addstring(pktout, "en"); /* language tag */ |
3272 | ssh2_pkt_send_noqueue(ssh, pktout); |
3273 | } |
3274 | } |
3275 | ssh->close_expected = TRUE; |
3276 | ssh->clean_exit = clean_exit; |
3277 | ssh_closing((Plug)ssh, error, 0, 0); |
3278 | sfree(error); |
3279 | } |
3280 | |
3281 | /* |
fb09bf1c |
3282 | * Handle the key exchange and user authentication phases. |
3283 | */ |
ff3187f6 |
3284 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, |
3285 | struct Packet *pktin) |
fb09bf1c |
3286 | { |
0016d70b |
3287 | int i, j, ret; |
3288 | unsigned char cookie[8], *ptr; |
374330e2 |
3289 | struct MD5Context md5c; |
51470298 |
3290 | struct do_ssh1_login_state { |
30919997 |
3291 | int crLine; |
51470298 |
3292 | int len; |
3293 | unsigned char *rsabuf, *keystr1, *keystr2; |
3294 | unsigned long supported_ciphers_mask, supported_auths_mask; |
3295 | int tried_publickey, tried_agent; |
3296 | int tis_auth_refused, ccard_auth_refused; |
3297 | unsigned char session_id[16]; |
3298 | int cipher_type; |
51470298 |
3299 | void *publickey_blob; |
3300 | int publickey_bloblen; |
edd0cb8a |
3301 | char *publickey_comment; |
3302 | int publickey_encrypted; |
3303 | prompts_t *cur_prompt; |
51470298 |
3304 | char c; |
3305 | int pwpkt_type; |
3306 | unsigned char request[5], *response, *p; |
3307 | int responselen; |
3308 | int keyi, nkeys; |
3309 | int authed; |
3310 | struct RSAKey key; |
3311 | Bignum challenge; |
3312 | char *commentp; |
3313 | int commentlen; |
3d9449a1 |
3314 | int dlgret; |
4a693cfc |
3315 | Filename *keyfile; |
aacc46fa |
3316 | struct RSAKey servkey, hostkey; |
51470298 |
3317 | }; |
3318 | crState(do_ssh1_login_state); |
3319 | |
30919997 |
3320 | crBeginState; |
374330e2 |
3321 | |
ff3187f6 |
3322 | if (!pktin) |
3323 | crWaitUntil(pktin); |
374330e2 |
3324 | |
ff3187f6 |
3325 | if (pktin->type != SSH1_SMSG_PUBLIC_KEY) { |
6b5cf8b4 |
3326 | bombout(("Public key packet not received")); |
7ffdbc1a |
3327 | crStop(0); |
8d5de777 |
3328 | } |
374330e2 |
3329 | |
c5e9c988 |
3330 | logevent("Received public keys"); |
374330e2 |
3331 | |
ff3187f6 |
3332 | ptr = ssh_pkt_getdata(pktin, 8); |
0016d70b |
3333 | if (!ptr) { |
2e85c969 |
3334 | bombout(("SSH-1 public key packet stopped before random cookie")); |
0016d70b |
3335 | crStop(0); |
3336 | } |
3337 | memcpy(cookie, ptr, 8); |
374330e2 |
3338 | |
aacc46fa |
3339 | if (!ssh1_pkt_getrsakey(pktin, &s->servkey, &s->keystr1) || |
3340 | !ssh1_pkt_getrsakey(pktin, &s->hostkey, &s->keystr2)) { |
2e85c969 |
3341 | bombout(("Failed to read SSH-1 public keys from public key packet")); |
0016d70b |
3342 | crStop(0); |
3343 | } |
374330e2 |
3344 | |
c5e9c988 |
3345 | /* |
1c2a93c4 |
3346 | * Log the host key fingerprint. |
c5e9c988 |
3347 | */ |
c5e9c988 |
3348 | { |
3349 | char logmsg[80]; |
1c2a93c4 |
3350 | logevent("Host key fingerprint is:"); |
c5e9c988 |
3351 | strcpy(logmsg, " "); |
aacc46fa |
3352 | s->hostkey.comment = NULL; |
32874aea |
3353 | rsa_fingerprint(logmsg + strlen(logmsg), |
aacc46fa |
3354 | sizeof(logmsg) - strlen(logmsg), &s->hostkey); |
c5e9c988 |
3355 | logevent(logmsg); |
3356 | } |
3357 | |
ff3187f6 |
3358 | ssh->v1_remote_protoflags = ssh_pkt_getuint32(pktin); |
3359 | s->supported_ciphers_mask = ssh_pkt_getuint32(pktin); |
3360 | s->supported_auths_mask = ssh_pkt_getuint32(pktin); |
d5b2c841 |
3361 | if ((ssh->remote_bugs & BUG_CHOKES_ON_RSA)) |
3362 | s->supported_auths_mask &= ~(1 << SSH1_AUTH_RSA); |
bea1ef5f |
3363 | |
51470298 |
3364 | ssh->v1_local_protoflags = |
3365 | ssh->v1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED; |
3366 | ssh->v1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER; |
b96dc54c |
3367 | |
c5e9c988 |
3368 | MD5Init(&md5c); |
aacc46fa |
3369 | MD5Update(&md5c, s->keystr2, s->hostkey.bytes); |
3370 | MD5Update(&md5c, s->keystr1, s->servkey.bytes); |
0016d70b |
3371 | MD5Update(&md5c, cookie, 8); |
51470298 |
3372 | MD5Final(s->session_id, &md5c); |
374330e2 |
3373 | |
32874aea |
3374 | for (i = 0; i < 32; i++) |
51470298 |
3375 | ssh->session_key[i] = random_byte(); |
374330e2 |
3376 | |
0016d70b |
3377 | /* |
3378 | * Verify that the `bits' and `bytes' parameters match. |
3379 | */ |
aacc46fa |
3380 | if (s->hostkey.bits > s->hostkey.bytes * 8 || |
3381 | s->servkey.bits > s->servkey.bytes * 8) { |
2e85c969 |
3382 | bombout(("SSH-1 public keys were badly formatted")); |
0016d70b |
3383 | crStop(0); |
3384 | } |
3385 | |
aacc46fa |
3386 | s->len = (s->hostkey.bytes > s->servkey.bytes ? |
3387 | s->hostkey.bytes : s->servkey.bytes); |
374330e2 |
3388 | |
3d88e64d |
3389 | s->rsabuf = snewn(s->len, unsigned char); |
374330e2 |
3390 | |
89ee5268 |
3391 | /* |
3392 | * Verify the host key. |
3393 | */ |
3394 | { |
32874aea |
3395 | /* |
3396 | * First format the key into a string. |
3397 | */ |
aacc46fa |
3398 | int len = rsastr_len(&s->hostkey); |
32874aea |
3399 | char fingerprint[100]; |
3d88e64d |
3400 | char *keystr = snewn(len, char); |
aacc46fa |
3401 | rsastr_fmt(keystr, &s->hostkey); |
3402 | rsa_fingerprint(fingerprint, sizeof(fingerprint), &s->hostkey); |
3d9449a1 |
3403 | |
3404 | ssh_set_frozen(ssh, 1); |
3405 | s->dlgret = verify_ssh_host_key(ssh->frontend, |
3406 | ssh->savedhost, ssh->savedport, |
3407 | "rsa", keystr, fingerprint, |
3408 | ssh_dialog_callback, ssh); |
32874aea |
3409 | sfree(keystr); |
3d9449a1 |
3410 | if (s->dlgret < 0) { |
3411 | do { |
3412 | crReturn(0); |
3413 | if (pktin) { |
3414 | bombout(("Unexpected data from server while waiting" |
3415 | " for user host key response")); |
3416 | crStop(0); |
3417 | } |
3418 | } while (pktin || inlen > 0); |
3419 | s->dlgret = ssh->user_response; |
3420 | } |
3421 | ssh_set_frozen(ssh, 0); |
3422 | |
3423 | if (s->dlgret == 0) { |
9e296bfa |
3424 | ssh_disconnect(ssh, "User aborted at host key verification", |
3425 | NULL, 0, TRUE); |
2b3f6c19 |
3426 | crStop(0); |
3d9449a1 |
3427 | } |
32874aea |
3428 | } |
3429 | |
3430 | for (i = 0; i < 32; i++) { |
51470298 |
3431 | s->rsabuf[i] = ssh->session_key[i]; |
374330e2 |
3432 | if (i < 16) |
51470298 |
3433 | s->rsabuf[i] ^= s->session_id[i]; |
374330e2 |
3434 | } |
3435 | |
aacc46fa |
3436 | if (s->hostkey.bytes > s->servkey.bytes) { |
3437 | ret = rsaencrypt(s->rsabuf, 32, &s->servkey); |
0016d70b |
3438 | if (ret) |
aacc46fa |
3439 | ret = rsaencrypt(s->rsabuf, s->servkey.bytes, &s->hostkey); |
374330e2 |
3440 | } else { |
aacc46fa |
3441 | ret = rsaencrypt(s->rsabuf, 32, &s->hostkey); |
0016d70b |
3442 | if (ret) |
aacc46fa |
3443 | ret = rsaencrypt(s->rsabuf, s->hostkey.bytes, &s->servkey); |
0016d70b |
3444 | } |
3445 | if (!ret) { |
2e85c969 |
3446 | bombout(("SSH-1 public key encryptions failed due to bad formatting")); |
0016d70b |
3447 | crStop(0); |
374330e2 |
3448 | } |
3449 | |
c5e9c988 |
3450 | logevent("Encrypted session key"); |
3451 | |
ca20bfcf |
3452 | { |
3453 | int cipher_chosen = 0, warn = 0; |
3454 | char *cipher_string = NULL; |
51470298 |
3455 | int i; |
ca20bfcf |
3456 | for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) { |
4a693cfc |
3457 | int next_cipher = conf_get_int_int(ssh->conf, |
3458 | CONF_ssh_cipherlist, i); |
ca20bfcf |
3459 | if (next_cipher == CIPHER_WARN) { |
3460 | /* If/when we choose a cipher, warn about it */ |
3461 | warn = 1; |
3462 | } else if (next_cipher == CIPHER_AES) { |
3463 | /* XXX Probably don't need to mention this. */ |
2e85c969 |
3464 | logevent("AES not supported in SSH-1, skipping"); |
ca20bfcf |
3465 | } else { |
3466 | switch (next_cipher) { |
51470298 |
3467 | case CIPHER_3DES: s->cipher_type = SSH_CIPHER_3DES; |
ca20bfcf |
3468 | cipher_string = "3DES"; break; |
51470298 |
3469 | case CIPHER_BLOWFISH: s->cipher_type = SSH_CIPHER_BLOWFISH; |
ca20bfcf |
3470 | cipher_string = "Blowfish"; break; |
51470298 |
3471 | case CIPHER_DES: s->cipher_type = SSH_CIPHER_DES; |
ca20bfcf |
3472 | cipher_string = "single-DES"; break; |
3473 | } |
51470298 |
3474 | if (s->supported_ciphers_mask & (1 << s->cipher_type)) |
ca20bfcf |
3475 | cipher_chosen = 1; |
3476 | } |
3477 | } |
3478 | if (!cipher_chosen) { |
51470298 |
3479 | if ((s->supported_ciphers_mask & (1 << SSH_CIPHER_3DES)) == 0) |
2e85c969 |
3480 | bombout(("Server violates SSH-1 protocol by not " |
ca20bfcf |
3481 | "supporting 3DES encryption")); |
3482 | else |
3483 | /* shouldn't happen */ |
6b5cf8b4 |
3484 | bombout(("No supported ciphers found")); |
7ffdbc1a |
3485 | crStop(0); |
a99a05c0 |
3486 | } |
ca20bfcf |
3487 | |
3488 | /* Warn about chosen cipher if necessary. */ |
bb348ab1 |
3489 | if (warn) { |
3d9449a1 |
3490 | ssh_set_frozen(ssh, 1); |
3491 | s->dlgret = askalg(ssh->frontend, "cipher", cipher_string, |
3492 | ssh_dialog_callback, ssh); |
3493 | if (s->dlgret < 0) { |
3494 | do { |
3495 | crReturn(0); |
3496 | if (pktin) { |
3497 | bombout(("Unexpected data from server while waiting" |
3498 | " for user response")); |
3499 | crStop(0); |
3500 | } |
3501 | } while (pktin || inlen > 0); |
3502 | s->dlgret = ssh->user_response; |
3503 | } |
3504 | ssh_set_frozen(ssh, 0); |
3505 | if (s->dlgret == 0) { |
9e296bfa |
3506 | ssh_disconnect(ssh, "User aborted at cipher warning", NULL, |
3507 | 0, TRUE); |
96ccde8b |
3508 | crStop(0); |
3d9449a1 |
3509 | } |
bb348ab1 |
3510 | } |
bea1ef5f |
3511 | } |
ca20bfcf |
3512 | |
51470298 |
3513 | switch (s->cipher_type) { |
32874aea |
3514 | case SSH_CIPHER_3DES: |
3515 | logevent("Using 3DES encryption"); |
3516 | break; |
3517 | case SSH_CIPHER_DES: |
3518 | logevent("Using single-DES encryption"); |
3519 | break; |
3520 | case SSH_CIPHER_BLOWFISH: |
3521 | logevent("Using Blowfish encryption"); |
3522 | break; |
c5e9c988 |
3523 | } |
bea1ef5f |
3524 | |
51470298 |
3525 | send_packet(ssh, SSH1_CMSG_SESSION_KEY, |
3526 | PKT_CHAR, s->cipher_type, |
32874aea |
3527 | PKT_DATA, cookie, 8, |
51470298 |
3528 | PKT_CHAR, (s->len * 8) >> 8, PKT_CHAR, (s->len * 8) & 0xFF, |
3529 | PKT_DATA, s->rsabuf, s->len, |
3530 | PKT_INT, ssh->v1_local_protoflags, PKT_END); |
fb09bf1c |
3531 | |
c5e9c988 |
3532 | logevent("Trying to enable encryption..."); |
374330e2 |
3533 | |
51470298 |
3534 | sfree(s->rsabuf); |
374330e2 |
3535 | |
51470298 |
3536 | ssh->cipher = (s->cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 : |
3537 | s->cipher_type == SSH_CIPHER_DES ? &ssh_des : |
3538 | &ssh_3des); |
371e569c |
3539 | ssh->v1_cipher_ctx = ssh->cipher->make_context(); |
3540 | ssh->cipher->sesskey(ssh->v1_cipher_ctx, ssh->session_key); |
57356d63 |
3541 | logeventf(ssh, "Initialised %s encryption", ssh->cipher->text_name); |
374330e2 |
3542 | |
0183b242 |
3543 | ssh->crcda_ctx = crcda_make_context(); |
3544 | logevent("Installing CRC compensation attack detector"); |
3545 | |
aacc46fa |
3546 | if (s->servkey.modulus) { |
3547 | sfree(s->servkey.modulus); |
3548 | s->servkey.modulus = NULL; |
679539d7 |
3549 | } |
aacc46fa |
3550 | if (s->servkey.exponent) { |
3551 | sfree(s->servkey.exponent); |
3552 | s->servkey.exponent = NULL; |
679539d7 |
3553 | } |
aacc46fa |
3554 | if (s->hostkey.modulus) { |
3555 | sfree(s->hostkey.modulus); |
3556 | s->hostkey.modulus = NULL; |
679539d7 |
3557 | } |
aacc46fa |
3558 | if (s->hostkey.exponent) { |
3559 | sfree(s->hostkey.exponent); |
3560 | s->hostkey.exponent = NULL; |
679539d7 |
3561 | } |
ff3187f6 |
3562 | crWaitUntil(pktin); |
374330e2 |
3563 | |
ff3187f6 |
3564 | if (pktin->type != SSH1_SMSG_SUCCESS) { |
6b5cf8b4 |
3565 | bombout(("Encryption not successfully enabled")); |
7ffdbc1a |
3566 | crStop(0); |
8d5de777 |
3567 | } |
374330e2 |
3568 | |
c5e9c988 |
3569 | logevent("Successfully started encryption"); |
3570 | |
edd0cb8a |
3571 | fflush(stdout); /* FIXME eh? */ |
374330e2 |
3572 | { |
4a693cfc |
3573 | if ((ssh->username = get_remote_username(ssh->conf)) == NULL) { |
edd0cb8a |
3574 | int ret; /* need not be kept over crReturn */ |
3575 | s->cur_prompt = new_prompts(ssh->frontend); |
3576 | s->cur_prompt->to_server = TRUE; |
3577 | s->cur_prompt->name = dupstr("SSH login name"); |
b61f81bc |
3578 | add_prompt(s->cur_prompt, dupstr("login as: "), TRUE); |
edd0cb8a |
3579 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
3580 | while (ret < 0) { |
51470298 |
3581 | ssh->send_ok = 1; |
edd0cb8a |
3582 | crWaitUntil(!pktin); |
3583 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
3584 | ssh->send_ok = 0; |
3585 | } |
3586 | if (!ret) { |
3587 | /* |
3588 | * Failed to get a username. Terminate. |
3589 | */ |
3590 | free_prompts(s->cur_prompt); |
3591 | ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE); |
3592 | crStop(0); |
32874aea |
3593 | } |
4a693cfc |
3594 | ssh->username = dupstr(s->cur_prompt->prompts[0]->result); |
edd0cb8a |
3595 | free_prompts(s->cur_prompt); |
374330e2 |
3596 | } |
fb09bf1c |
3597 | |
4a693cfc |
3598 | send_packet(ssh, SSH1_CMSG_USER, PKT_STR, ssh->username, PKT_END); |
c5e9c988 |
3599 | { |
4a693cfc |
3600 | char *userlog = dupprintf("Sent username \"%s\"", ssh->username); |
c5e9c988 |
3601 | logevent(userlog); |
32874aea |
3602 | if (flags & FLAG_INTERACTIVE && |
3603 | (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) { |
51470298 |
3604 | c_write_str(ssh, userlog); |
edd0cb8a |
3605 | c_write_str(ssh, "\r\n"); |
3c8e959b |
3606 | } |
edd0cb8a |
3607 | sfree(userlog); |
c5e9c988 |
3608 | } |
374330e2 |
3609 | } |
3610 | |
ff3187f6 |
3611 | crWaitUntil(pktin); |
374330e2 |
3612 | |
d5b2c841 |
3613 | if ((s->supported_auths_mask & (1 << SSH1_AUTH_RSA)) == 0) { |
0df73905 |
3614 | /* We must not attempt PK auth. Pretend we've already tried it. */ |
51470298 |
3615 | s->tried_publickey = s->tried_agent = 1; |
0df73905 |
3616 | } else { |
51470298 |
3617 | s->tried_publickey = s->tried_agent = 0; |
0df73905 |
3618 | } |
51470298 |
3619 | s->tis_auth_refused = s->ccard_auth_refused = 0; |
edd0cb8a |
3620 | /* |
3621 | * Load the public half of any configured keyfile for later use. |
3622 | */ |
4a693cfc |
3623 | s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile); |
962468d4 |
3624 | if (!filename_is_null(s->keyfile)) { |
edd0cb8a |
3625 | int keytype; |
3626 | logeventf(ssh, "Reading private key file \"%.150s\"", |
4a693cfc |
3627 | filename_to_str(s->keyfile)); |
3628 | keytype = key_type(s->keyfile); |
edd0cb8a |
3629 | if (keytype == SSH_KEYTYPE_SSH1) { |
3630 | const char *error; |
4a693cfc |
3631 | if (rsakey_pubblob(s->keyfile, |
edd0cb8a |
3632 | &s->publickey_blob, &s->publickey_bloblen, |
3633 | &s->publickey_comment, &error)) { |
4a693cfc |
3634 | s->publickey_encrypted = rsakey_encrypted(s->keyfile, |
edd0cb8a |
3635 | NULL); |
3636 | } else { |
3637 | char *msgbuf; |
3638 | logeventf(ssh, "Unable to load private key (%s)", error); |
3639 | msgbuf = dupprintf("Unable to load private key file " |
3640 | "\"%.150s\" (%s)\r\n", |
4a693cfc |
3641 | filename_to_str(s->keyfile), |
edd0cb8a |
3642 | error); |
3643 | c_write_str(ssh, msgbuf); |
3644 | sfree(msgbuf); |
3645 | s->publickey_blob = NULL; |
3646 | } |
3647 | } else { |
3648 | char *msgbuf; |
3649 | logeventf(ssh, "Unable to use this key file (%s)", |
3650 | key_type_to_str(keytype)); |
3651 | msgbuf = dupprintf("Unable to use key file \"%.150s\"" |
3652 | " (%s)\r\n", |
4a693cfc |
3653 | filename_to_str(s->keyfile), |
edd0cb8a |
3654 | key_type_to_str(keytype)); |
3655 | c_write_str(ssh, msgbuf); |
3656 | sfree(msgbuf); |
51470298 |
3657 | s->publickey_blob = NULL; |
edd0cb8a |
3658 | } |
396778f1 |
3659 | } else |
51470298 |
3660 | s->publickey_blob = NULL; |
7cca0d81 |
3661 | |
ff3187f6 |
3662 | while (pktin->type == SSH1_SMSG_FAILURE) { |
51470298 |
3663 | s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD; |
614a20a0 |
3664 | |
4a693cfc |
3665 | if (conf_get_int(ssh->conf, CONF_tryagent) && agent_exists() && !s->tried_agent) { |
32874aea |
3666 | /* |
3667 | * Attempt RSA authentication using Pageant. |
3668 | */ |
32874aea |
3669 | void *r; |
3670 | |
51470298 |
3671 | s->authed = FALSE; |
3672 | s->tried_agent = 1; |
32874aea |
3673 | logevent("Pageant is running. Requesting keys."); |
3674 | |
3675 | /* Request the keys held by the agent. */ |
51470298 |
3676 | PUT_32BIT(s->request, 1); |
3677 | s->request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
839f10db |
3678 | if (!agent_query(s->request, 5, &r, &s->responselen, |
3679 | ssh_agent_callback, ssh)) { |
3680 | do { |
3681 | crReturn(0); |
ff3187f6 |
3682 | if (pktin) { |
839f10db |
3683 | bombout(("Unexpected data from server while waiting" |
3684 | " for agent response")); |
3685 | crStop(0); |
3686 | } |
ff3187f6 |
3687 | } while (pktin || inlen > 0); |
839f10db |
3688 | r = ssh->agent_response; |
3689 | s->responselen = ssh->agent_response_len; |
3690 | } |
51470298 |
3691 | s->response = (unsigned char *) r; |
3692 | if (s->response && s->responselen >= 5 && |
3693 | s->response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) { |
3694 | s->p = s->response + 5; |
b1650067 |
3695 | s->nkeys = toint(GET_32BIT(s->p)); |
3696 | if (s->nkeys < 0) { |
3697 | logeventf(ssh, "Pageant reported negative key count %d", |
3698 | s->nkeys); |
3699 | s->nkeys = 0; |
3700 | } |
51470298 |
3701 | s->p += 4; |
2e85c969 |
3702 | logeventf(ssh, "Pageant has %d SSH-1 keys", s->nkeys); |
51470298 |
3703 | for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) { |
94cd7c3a |
3704 | unsigned char *pkblob = s->p; |
51470298 |
3705 | s->p += 4; |
0016d70b |
3706 | { |
3707 | int n, ok = FALSE; |
3708 | do { /* do while (0) to make breaking easy */ |
3709 | n = ssh1_read_bignum |
b1650067 |
3710 | (s->p, toint(s->responselen-(s->p-s->response)), |
0016d70b |
3711 | &s->key.exponent); |
3712 | if (n < 0) |
3713 | break; |
3714 | s->p += n; |
3715 | n = ssh1_read_bignum |
b1650067 |
3716 | (s->p, toint(s->responselen-(s->p-s->response)), |
0016d70b |
3717 | &s->key.modulus); |
3718 | if (n < 0) |
b1650067 |
3719 | break; |
0016d70b |
3720 | s->p += n; |
3721 | if (s->responselen - (s->p-s->response) < 4) |
3722 | break; |
b1650067 |
3723 | s->commentlen = toint(GET_32BIT(s->p)); |
0016d70b |
3724 | s->p += 4; |
b1650067 |
3725 | if (s->commentlen < 0 || |
3726 | toint(s->responselen - (s->p-s->response)) < |
0016d70b |
3727 | s->commentlen) |
3728 | break; |
3729 | s->commentp = (char *)s->p; |
3730 | s->p += s->commentlen; |
3731 | ok = TRUE; |
3732 | } while (0); |
3733 | if (!ok) { |
3734 | logevent("Pageant key list packet was truncated"); |
3735 | break; |
3736 | } |
3737 | } |
94cd7c3a |
3738 | if (s->publickey_blob) { |
3739 | if (!memcmp(pkblob, s->publickey_blob, |
3740 | s->publickey_bloblen)) { |
3741 | logeventf(ssh, "Pageant key #%d matches " |
3742 | "configured key file", s->keyi); |
3743 | s->tried_publickey = 1; |
3744 | } else |
3745 | /* Skip non-configured key */ |
3746 | continue; |
3747 | } |
3748 | logeventf(ssh, "Trying Pageant key #%d", s->keyi); |
51470298 |
3749 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
3750 | PKT_BIGNUM, s->key.modulus, PKT_END); |
ff3187f6 |
3751 | crWaitUntil(pktin); |
3752 | if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
32874aea |
3753 | logevent("Key refused"); |
3754 | continue; |
3755 | } |
3756 | logevent("Received RSA challenge"); |
ff3187f6 |
3757 | if ((s->challenge = ssh1_pkt_getmp(pktin)) == NULL) { |
0016d70b |
3758 | bombout(("Server's RSA challenge was badly formatted")); |
3759 | crStop(0); |
3760 | } |
3761 | |
32874aea |
3762 | { |
3763 | char *agentreq, *q, *ret; |
2d466ffd |
3764 | void *vret; |
32874aea |
3765 | int len, retlen; |
3766 | len = 1 + 4; /* message type, bit count */ |
51470298 |
3767 | len += ssh1_bignum_length(s->key.exponent); |
3768 | len += ssh1_bignum_length(s->key.modulus); |
3769 | len += ssh1_bignum_length(s->challenge); |
32874aea |
3770 | len += 16; /* session id */ |
3771 | len += 4; /* response format */ |
3d88e64d |
3772 | agentreq = snewn(4 + len, char); |
32874aea |
3773 | PUT_32BIT(agentreq, len); |
3774 | q = agentreq + 4; |
3775 | *q++ = SSH1_AGENTC_RSA_CHALLENGE; |
51470298 |
3776 | PUT_32BIT(q, bignum_bitcount(s->key.modulus)); |
32874aea |
3777 | q += 4; |
51470298 |
3778 | q += ssh1_write_bignum(q, s->key.exponent); |
3779 | q += ssh1_write_bignum(q, s->key.modulus); |
3780 | q += ssh1_write_bignum(q, s->challenge); |
3781 | memcpy(q, s->session_id, 16); |
32874aea |
3782 | q += 16; |
3783 | PUT_32BIT(q, 1); /* response format */ |
839f10db |
3784 | if (!agent_query(agentreq, len + 4, &vret, &retlen, |
3785 | ssh_agent_callback, ssh)) { |
3786 | sfree(agentreq); |
3787 | do { |
3788 | crReturn(0); |
ff3187f6 |
3789 | if (pktin) { |
839f10db |
3790 | bombout(("Unexpected data from server" |
3791 | " while waiting for agent" |
3792 | " response")); |
3793 | crStop(0); |
3794 | } |
ff3187f6 |
3795 | } while (pktin || inlen > 0); |
839f10db |
3796 | vret = ssh->agent_response; |
3797 | retlen = ssh->agent_response_len; |
3798 | } else |
3799 | sfree(agentreq); |
2d466ffd |
3800 | ret = vret; |
32874aea |
3801 | if (ret) { |
3802 | if (ret[4] == SSH1_AGENT_RSA_RESPONSE) { |
3803 | logevent("Sending Pageant's response"); |
51470298 |
3804 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
32874aea |
3805 | PKT_DATA, ret + 5, 16, |
3806 | PKT_END); |
3807 | sfree(ret); |
ff3187f6 |
3808 | crWaitUntil(pktin); |
3809 | if (pktin->type == SSH1_SMSG_SUCCESS) { |
32874aea |
3810 | logevent |
3811 | ("Pageant's response accepted"); |
3812 | if (flags & FLAG_VERBOSE) { |
51470298 |
3813 | c_write_str(ssh, "Authenticated using" |
3814 | " RSA key \""); |
3815 | c_write(ssh, s->commentp, |
3816 | s->commentlen); |
3817 | c_write_str(ssh, "\" from agent\r\n"); |
32874aea |
3818 | } |
51470298 |
3819 | s->authed = TRUE; |
32874aea |
3820 | } else |
3821 | logevent |
3822 | ("Pageant's response not accepted"); |
3823 | } else { |
3824 | logevent |
3825 | ("Pageant failed to answer challenge"); |
3826 | sfree(ret); |
3827 | } |
3828 | } else { |
3829 | logevent("No reply received from Pageant"); |
3830 | } |
3831 | } |
51470298 |
3832 | freebn(s->key.exponent); |
3833 | freebn(s->key.modulus); |
3834 | freebn(s->challenge); |
3835 | if (s->authed) |
32874aea |
3836 | break; |
3837 | } |
29b1d0b3 |
3838 | sfree(s->response); |
94cd7c3a |
3839 | if (s->publickey_blob && !s->tried_publickey) |
3840 | logevent("Configured key file not in Pageant"); |
5ff99b8e |
3841 | } else { |
3842 | logevent("Failed to get reply from Pageant"); |
3843 | } |
51470298 |
3844 | if (s->authed) |
32874aea |
3845 | break; |
3846 | } |
edd0cb8a |
3847 | if (s->publickey_blob && !s->tried_publickey) { |
3848 | /* |
3849 | * Try public key authentication with the specified |
3850 | * key file. |
3851 | */ |
3852 | int got_passphrase; /* need not be kept over crReturn */ |
3853 | if (flags & FLAG_VERBOSE) |
3854 | c_write_str(ssh, "Trying public key authentication.\r\n"); |
4a693cfc |
3855 | s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile); |
edd0cb8a |
3856 | logeventf(ssh, "Trying public key \"%s\"", |
4a693cfc |
3857 | filename_to_str(s->keyfile)); |
edd0cb8a |
3858 | s->tried_publickey = 1; |
3859 | got_passphrase = FALSE; |
3860 | while (!got_passphrase) { |
3861 | /* |
3862 | * Get a passphrase, if necessary. |
3863 | */ |
3864 | char *passphrase = NULL; /* only written after crReturn */ |
3865 | const char *error; |
3866 | if (!s->publickey_encrypted) { |
3867 | if (flags & FLAG_VERBOSE) |
3868 | c_write_str(ssh, "No passphrase required.\r\n"); |
3869 | passphrase = NULL; |
3870 | } else { |
3871 | int ret; /* need not be kept over crReturn */ |
3872 | s->cur_prompt = new_prompts(ssh->frontend); |
3873 | s->cur_prompt->to_server = FALSE; |
3874 | s->cur_prompt->name = dupstr("SSH key passphrase"); |
3875 | add_prompt(s->cur_prompt, |
3876 | dupprintf("Passphrase for key \"%.100s\": ", |
b61f81bc |
3877 | s->publickey_comment), FALSE); |
edd0cb8a |
3878 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
3879 | while (ret < 0) { |
3880 | ssh->send_ok = 1; |
3881 | crWaitUntil(!pktin); |
3882 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
3883 | ssh->send_ok = 0; |
3884 | } |
3885 | if (!ret) { |
3886 | /* Failed to get a passphrase. Terminate. */ |
3887 | free_prompts(s->cur_prompt); |
3888 | ssh_disconnect(ssh, NULL, "Unable to authenticate", |
3889 | 0, TRUE); |
3890 | crStop(0); |
3891 | } |
3892 | passphrase = dupstr(s->cur_prompt->prompts[0]->result); |
3893 | free_prompts(s->cur_prompt); |
3894 | } |
3895 | /* |
3896 | * Try decrypting key with passphrase. |
3897 | */ |
4a693cfc |
3898 | s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile); |
3899 | ret = loadrsakey(s->keyfile, &s->key, passphrase, |
edd0cb8a |
3900 | &error); |
3901 | if (passphrase) { |
dfb88efd |
3902 | smemclr(passphrase, strlen(passphrase)); |
edd0cb8a |
3903 | sfree(passphrase); |
3904 | } |
3905 | if (ret == 1) { |
3906 | /* Correct passphrase. */ |
3907 | got_passphrase = TRUE; |
3908 | } else if (ret == 0) { |
3909 | c_write_str(ssh, "Couldn't load private key from "); |
4a693cfc |
3910 | c_write_str(ssh, filename_to_str(s->keyfile)); |
edd0cb8a |
3911 | c_write_str(ssh, " ("); |
3912 | c_write_str(ssh, error); |
3913 | c_write_str(ssh, ").\r\n"); |
3914 | got_passphrase = FALSE; |
3915 | break; /* go and try something else */ |
3916 | } else if (ret == -1) { |
3917 | c_write_str(ssh, "Wrong passphrase.\r\n"); /* FIXME */ |
edd0cb8a |
3918 | got_passphrase = FALSE; |
3919 | /* and try again */ |
43536490 |
3920 | } else { |
3921 | assert(0 && "unexpected return from loadrsakey()"); |
edc23a8a |
3922 | got_passphrase = FALSE; /* placate optimisers */ |
edd0cb8a |
3923 | } |
3924 | } |
3925 | |
3926 | if (got_passphrase) { |
3927 | |
3928 | /* |
3929 | * Send a public key attempt. |
3930 | */ |
3931 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
3932 | PKT_BIGNUM, s->key.modulus, PKT_END); |
3933 | |
3934 | crWaitUntil(pktin); |
3935 | if (pktin->type == SSH1_SMSG_FAILURE) { |
3936 | c_write_str(ssh, "Server refused our public key.\r\n"); |
7b575324 |
3937 | continue; /* go and try something else */ |
edd0cb8a |
3938 | } |
3939 | if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
3940 | bombout(("Bizarre response to offer of public key")); |
3941 | crStop(0); |
3942 | } |
3943 | |
3944 | { |
3945 | int i; |
3946 | unsigned char buffer[32]; |
3947 | Bignum challenge, response; |
3948 | |
3949 | if ((challenge = ssh1_pkt_getmp(pktin)) == NULL) { |
3950 | bombout(("Server's RSA challenge was badly formatted")); |
3951 | crStop(0); |
3952 | } |
3953 | response = rsadecrypt(challenge, &s->key); |
3954 | freebn(s->key.private_exponent);/* burn the evidence */ |
3955 | |
3956 | for (i = 0; i < 32; i++) { |
3957 | buffer[i] = bignum_byte(response, 31 - i); |
3958 | } |
3959 | |
3960 | MD5Init(&md5c); |
3961 | MD5Update(&md5c, buffer, 32); |
3962 | MD5Update(&md5c, s->session_id, 16); |
3963 | MD5Final(buffer, &md5c); |
3964 | |
3965 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
3966 | PKT_DATA, buffer, 16, PKT_END); |
3967 | |
3968 | freebn(challenge); |
3969 | freebn(response); |
3970 | } |
3971 | |
3972 | crWaitUntil(pktin); |
3973 | if (pktin->type == SSH1_SMSG_FAILURE) { |
3974 | if (flags & FLAG_VERBOSE) |
3975 | c_write_str(ssh, "Failed to authenticate with" |
3976 | " our public key.\r\n"); |
7b575324 |
3977 | continue; /* go and try something else */ |
edd0cb8a |
3978 | } else if (pktin->type != SSH1_SMSG_SUCCESS) { |
3979 | bombout(("Bizarre response to RSA authentication response")); |
3980 | crStop(0); |
3981 | } |
3982 | |
3983 | break; /* we're through! */ |
3984 | } |
3985 | |
3986 | } |
3987 | |
3988 | /* |
3989 | * Otherwise, try various forms of password-like authentication. |
3990 | */ |
3991 | s->cur_prompt = new_prompts(ssh->frontend); |
32874aea |
3992 | |
4a693cfc |
3993 | if (conf_get_int(ssh->conf, CONF_try_tis_auth) && |
51470298 |
3994 | (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) && |
3995 | !s->tis_auth_refused) { |
3996 | s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE; |
32874aea |
3997 | logevent("Requested TIS authentication"); |
51470298 |
3998 | send_packet(ssh, SSH1_CMSG_AUTH_TIS, PKT_END); |
ff3187f6 |
3999 | crWaitUntil(pktin); |
4000 | if (pktin->type != SSH1_SMSG_AUTH_TIS_CHALLENGE) { |
32874aea |
4001 | logevent("TIS authentication declined"); |
4002 | if (flags & FLAG_INTERACTIVE) |
51470298 |
4003 | c_write_str(ssh, "TIS authentication refused.\r\n"); |
4004 | s->tis_auth_refused = 1; |
614a20a0 |
4005 | continue; |
32874aea |
4006 | } else { |
0016d70b |
4007 | char *challenge; |
4008 | int challengelen; |
edd0cb8a |
4009 | char *instr_suf, *prompt; |
0016d70b |
4010 | |
ff3187f6 |
4011 | ssh_pkt_getstring(pktin, &challenge, &challengelen); |
0016d70b |
4012 | if (!challenge) { |
4013 | bombout(("TIS challenge packet was badly formed")); |
4014 | crStop(0); |
4015 | } |
32874aea |
4016 | logevent("Received TIS challenge"); |
edd0cb8a |
4017 | s->cur_prompt->to_server = TRUE; |
4018 | s->cur_prompt->name = dupstr("SSH TIS authentication"); |
614a20a0 |
4019 | /* Prompt heuristic comes from OpenSSH */ |
edd0cb8a |
4020 | if (memchr(challenge, '\n', challengelen)) { |
4021 | instr_suf = dupstr(""); |
4022 | prompt = dupprintf("%.*s", challengelen, challenge); |
4023 | } else { |
4024 | instr_suf = dupprintf("%.*s", challengelen, challenge); |
4025 | prompt = dupstr("Response: "); |
4026 | } |
4027 | s->cur_prompt->instruction = |
4028 | dupprintf("Using TIS authentication.%s%s", |
4029 | (*instr_suf) ? "\n" : "", |
4030 | instr_suf); |
4031 | s->cur_prompt->instr_reqd = TRUE; |
b61f81bc |
4032 | add_prompt(s->cur_prompt, prompt, FALSE); |
edd0cb8a |
4033 | sfree(instr_suf); |
32874aea |
4034 | } |
4035 | } |
4a693cfc |
4036 | if (conf_get_int(ssh->conf, CONF_try_tis_auth) && |
51470298 |
4037 | (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) && |
4038 | !s->ccard_auth_refused) { |
4039 | s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE; |
32874aea |
4040 | logevent("Requested CryptoCard authentication"); |
51470298 |
4041 | send_packet(ssh, SSH1_CMSG_AUTH_CCARD, PKT_END); |
ff3187f6 |
4042 | crWaitUntil(pktin); |
4043 | if (pktin->type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
32874aea |
4044 | logevent("CryptoCard authentication declined"); |
51470298 |
4045 | c_write_str(ssh, "CryptoCard authentication refused.\r\n"); |
4046 | s->ccard_auth_refused = 1; |
614a20a0 |
4047 | continue; |
32874aea |
4048 | } else { |
0016d70b |
4049 | char *challenge; |
4050 | int challengelen; |
edd0cb8a |
4051 | char *instr_suf, *prompt; |
0016d70b |
4052 | |
ff3187f6 |
4053 | ssh_pkt_getstring(pktin, &challenge, &challengelen); |
0016d70b |
4054 | if (!challenge) { |
4055 | bombout(("CryptoCard challenge packet was badly formed")); |
4056 | crStop(0); |
4057 | } |
32874aea |
4058 | logevent("Received CryptoCard challenge"); |
edd0cb8a |
4059 | s->cur_prompt->to_server = TRUE; |
4060 | s->cur_prompt->name = dupstr("SSH CryptoCard authentication"); |
4061 | s->cur_prompt->name_reqd = FALSE; |
4062 | /* Prompt heuristic comes from OpenSSH */ |
4063 | if (memchr(challenge, '\n', challengelen)) { |
4064 | instr_suf = dupstr(""); |
4065 | prompt = dupprintf("%.*s", challengelen, challenge); |
4066 | } else { |
4067 | instr_suf = dupprintf("%.*s", challengelen, challenge); |
4068 | prompt = dupstr("Response: "); |
4069 | } |
4070 | s->cur_prompt->instruction = |
4071 | dupprintf("Using CryptoCard authentication.%s%s", |
4072 | (*instr_suf) ? "\n" : "", |
4073 | instr_suf); |
4074 | s->cur_prompt->instr_reqd = TRUE; |
b61f81bc |
4075 | add_prompt(s->cur_prompt, prompt, FALSE); |
edd0cb8a |
4076 | sfree(instr_suf); |
32874aea |
4077 | } |
4078 | } |
51470298 |
4079 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
8f9e3231 |
4080 | if ((s->supported_auths_mask & (1 << SSH1_AUTH_PASSWORD)) == 0) { |
4081 | bombout(("No supported authentication methods available")); |
4082 | crStop(0); |
4083 | } |
edd0cb8a |
4084 | s->cur_prompt->to_server = TRUE; |
4085 | s->cur_prompt->name = dupstr("SSH password"); |
4a693cfc |
4086 | add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ", |
4087 | ssh->username, ssh->savedhost), |
b61f81bc |
4088 | FALSE); |
32874aea |
4089 | } |
a52f067e |
4090 | |
614a20a0 |
4091 | /* |
4092 | * Show password prompt, having first obtained it via a TIS |
4093 | * or CryptoCard exchange if we're doing TIS or CryptoCard |
4094 | * authentication. |
4095 | */ |
edd0cb8a |
4096 | { |
4097 | int ret; /* need not be kept over crReturn */ |
4098 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
4099 | while (ret < 0) { |
4100 | ssh->send_ok = 1; |
4101 | crWaitUntil(!pktin); |
4102 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
4103 | ssh->send_ok = 0; |
4104 | } |
4105 | if (!ret) { |
32874aea |
4106 | /* |
edd0cb8a |
4107 | * Failed to get a password (for example |
32874aea |
4108 | * because one was supplied on the command line |
4109 | * which has already failed to work). Terminate. |
4110 | */ |
edd0cb8a |
4111 | free_prompts(s->cur_prompt); |
4112 | ssh_disconnect(ssh, NULL, "Unable to authenticate", 0, TRUE); |
4113 | crStop(0); |
32874aea |
4114 | } |
32874aea |
4115 | } |
4116 | |
edd0cb8a |
4117 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
32874aea |
4118 | /* |
edd0cb8a |
4119 | * Defence against traffic analysis: we send a |
4120 | * whole bunch of packets containing strings of |
4121 | * different lengths. One of these strings is the |
4122 | * password, in a SSH1_CMSG_AUTH_PASSWORD packet. |
4123 | * The others are all random data in |
4124 | * SSH1_MSG_IGNORE packets. This way a passive |
4125 | * listener can't tell which is the password, and |
4126 | * hence can't deduce the password length. |
4127 | * |
4128 | * Anybody with a password length greater than 16 |
4129 | * bytes is going to have enough entropy in their |
4130 | * password that a listener won't find it _that_ |
4131 | * much help to know how long it is. So what we'll |
4132 | * do is: |
4133 | * |
4134 | * - if password length < 16, we send 15 packets |
4135 | * containing string lengths 1 through 15 |
4136 | * |
4137 | * - otherwise, we let N be the nearest multiple |
4138 | * of 8 below the password length, and send 8 |
4139 | * packets containing string lengths N through |
4140 | * N+7. This won't obscure the order of |
4141 | * magnitude of the password length, but it will |
4142 | * introduce a bit of extra uncertainty. |
4143 | * |
bf982899 |
4144 | * A few servers can't deal with SSH1_MSG_IGNORE, at |
4145 | * least in this context. For these servers, we need |
4146 | * an alternative defence. We make use of the fact |
4147 | * that the password is interpreted as a C string: |
4148 | * so we can append a NUL, then some random data. |
edd0cb8a |
4149 | * |
bf982899 |
4150 | * A few servers can deal with neither SSH1_MSG_IGNORE |
4151 | * here _nor_ a padded password string. |
4152 | * For these servers we are left with no defences |
edd0cb8a |
4153 | * against password length sniffing. |
32874aea |
4154 | */ |
bf982899 |
4155 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE) && |
4156 | !(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) { |
32874aea |
4157 | /* |
edd0cb8a |
4158 | * The server can deal with SSH1_MSG_IGNORE, so |
4159 | * we can use the primary defence. |
32874aea |
4160 | */ |
edd0cb8a |
4161 | int bottom, top, pwlen, i; |
4162 | char *randomstr; |
32874aea |
4163 | |
edd0cb8a |
4164 | pwlen = strlen(s->cur_prompt->prompts[0]->result); |
4165 | if (pwlen < 16) { |
4166 | bottom = 0; /* zero length passwords are OK! :-) */ |
4167 | top = 15; |
4168 | } else { |
4169 | bottom = pwlen & ~7; |
4170 | top = bottom + 7; |
4171 | } |
32874aea |
4172 | |
edd0cb8a |
4173 | assert(pwlen >= bottom && pwlen <= top); |
32874aea |
4174 | |
edd0cb8a |
4175 | randomstr = snewn(top + 1, char); |
32874aea |
4176 | |
edd0cb8a |
4177 | for (i = bottom; i <= top; i++) { |
4178 | if (i == pwlen) { |
4179 | defer_packet(ssh, s->pwpkt_type, |
4180 | PKTT_PASSWORD, PKT_STR, |
4181 | s->cur_prompt->prompts[0]->result, |
4182 | PKTT_OTHER, PKT_END); |
4183 | } else { |
4184 | for (j = 0; j < i; j++) { |
4185 | do { |
4186 | randomstr[j] = random_byte(); |
4187 | } while (randomstr[j] == '\0'); |
32874aea |
4188 | } |
edd0cb8a |
4189 | randomstr[i] = '\0'; |
4190 | defer_packet(ssh, SSH1_MSG_IGNORE, |
4191 | PKT_STR, randomstr, PKT_END); |
32874aea |
4192 | } |
edd0cb8a |
4193 | } |
4194 | logevent("Sending password with camouflage packets"); |
4195 | ssh_pkt_defersend(ssh); |
4196 | sfree(randomstr); |
4197 | } |
4198 | else if (!(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) { |
4199 | /* |
4200 | * The server can't deal with SSH1_MSG_IGNORE |
4201 | * but can deal with padded passwords, so we |
4202 | * can use the secondary defence. |
4203 | */ |
4204 | char string[64]; |
4205 | char *ss; |
4206 | int len; |
4207 | |
4208 | len = strlen(s->cur_prompt->prompts[0]->result); |
4209 | if (len < sizeof(string)) { |
4210 | ss = string; |
4211 | strcpy(string, s->cur_prompt->prompts[0]->result); |
4212 | len++; /* cover the zero byte */ |
4213 | while (len < sizeof(string)) { |
4214 | string[len++] = (char) random_byte(); |
bd358db1 |
4215 | } |
bd358db1 |
4216 | } else { |
edd0cb8a |
4217 | ss = s->cur_prompt->prompts[0]->result; |
32874aea |
4218 | } |
edd0cb8a |
4219 | logevent("Sending length-padded password"); |
9a10ecf4 |
4220 | send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD, |
edd0cb8a |
4221 | PKT_INT, len, PKT_DATA, ss, len, |
4222 | PKTT_OTHER, PKT_END); |
4223 | } else { |
4224 | /* |
bf982899 |
4225 | * The server is believed unable to cope with |
4226 | * any of our password camouflage methods. |
edd0cb8a |
4227 | */ |
4228 | int len; |
4229 | len = strlen(s->cur_prompt->prompts[0]->result); |
4230 | logevent("Sending unpadded password"); |
4231 | send_packet(ssh, s->pwpkt_type, |
4232 | PKTT_PASSWORD, PKT_INT, len, |
4233 | PKT_DATA, s->cur_prompt->prompts[0]->result, len, |
4234 | PKTT_OTHER, PKT_END); |
32874aea |
4235 | } |
edd0cb8a |
4236 | } else { |
4237 | send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD, |
4238 | PKT_STR, s->cur_prompt->prompts[0]->result, |
4239 | PKTT_OTHER, PKT_END); |
32874aea |
4240 | } |
c5e9c988 |
4241 | logevent("Sent password"); |
edd0cb8a |
4242 | free_prompts(s->cur_prompt); |
ff3187f6 |
4243 | crWaitUntil(pktin); |
4244 | if (pktin->type == SSH1_SMSG_FAILURE) { |
32874aea |
4245 | if (flags & FLAG_VERBOSE) |
51470298 |
4246 | c_write_str(ssh, "Access denied\r\n"); |
c5e9c988 |
4247 | logevent("Authentication refused"); |
ff3187f6 |
4248 | } else if (pktin->type != SSH1_SMSG_SUCCESS) { |
4249 | bombout(("Strange packet received, type %d", pktin->type)); |
7ffdbc1a |
4250 | crStop(0); |
374330e2 |
4251 | } |
4252 | } |
4253 | |
edd0cb8a |
4254 | /* Clear up */ |
4255 | if (s->publickey_blob) { |
4256 | sfree(s->publickey_blob); |
4257 | sfree(s->publickey_comment); |
4258 | } |
4259 | |
c5e9c988 |
4260 | logevent("Authentication successful"); |
4261 | |
fb09bf1c |
4262 | crFinish(1); |
4263 | } |
4264 | |
bc06669b |
4265 | static void ssh_channel_try_eof(struct ssh_channel *c) |
4266 | { |
4267 | Ssh ssh = c->ssh; |
4268 | assert(c->pending_eof); /* precondition for calling us */ |
4269 | if (c->halfopen) |
4270 | return; /* can't close: not even opened yet */ |
4271 | if (ssh->version == 2 && bufchain_size(&c->v.v2.outbuffer) > 0) |
4272 | return; /* can't send EOF: pending outgoing data */ |
4273 | |
3620f307 |
4274 | c->pending_eof = FALSE; /* we're about to send it */ |
bc06669b |
4275 | if (ssh->version == 1) { |
4276 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, |
4277 | PKT_END); |
4278 | c->closes |= CLOSES_SENT_EOF; |
4279 | } else { |
4280 | struct Packet *pktout; |
4281 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF); |
4282 | ssh2_pkt_adduint32(pktout, c->remoteid); |
4283 | ssh2_pkt_send(ssh, pktout); |
4284 | c->closes |= CLOSES_SENT_EOF; |
3620f307 |
4285 | ssh2_channel_check_close(c); |
bc06669b |
4286 | } |
bc06669b |
4287 | } |
4288 | |
4289 | void sshfwd_write_eof(struct ssh_channel *c) |
32874aea |
4290 | { |
51470298 |
4291 | Ssh ssh = c->ssh; |
4292 | |
1ef619ae |
4293 | if (ssh->state == SSH_STATE_CLOSED) |
36f94d1f |
4294 | return; |
36f94d1f |
4295 | |
bc06669b |
4296 | if (c->closes & CLOSES_SENT_EOF) |
4297 | return; |
78280721 |
4298 | |
bc06669b |
4299 | c->pending_eof = TRUE; |
4300 | ssh_channel_try_eof(c); |
32874aea |
4301 | } |
4302 | |
c6b41791 |
4303 | void sshfwd_unclean_close(struct ssh_channel *c) |
4304 | { |
4305 | Ssh ssh = c->ssh; |
c6b41791 |
4306 | |
4307 | if (ssh->state == SSH_STATE_CLOSED) |
4308 | return; |
4309 | |
c6b41791 |
4310 | switch (c->type) { |
4311 | case CHAN_X11: |
4312 | x11_close(c->u.x11.s); |
04120005 |
4313 | logevent("Forwarded X11 connection terminated due to local error"); |
c6b41791 |
4314 | break; |
4315 | case CHAN_SOCKDATA: |
4316 | case CHAN_SOCKDATA_DORMANT: |
4317 | pfd_close(c->u.pfd.s); |
04120005 |
4318 | logevent("Forwarded port closed due to local error"); |
c6b41791 |
4319 | break; |
4320 | } |
4321 | c->type = CHAN_ZOMBIE; |
96210393 |
4322 | |
c6b41791 |
4323 | ssh2_channel_check_close(c); |
4324 | } |
4325 | |
5471d09a |
4326 | int sshfwd_write(struct ssh_channel *c, char *buf, int len) |
32874aea |
4327 | { |
51470298 |
4328 | Ssh ssh = c->ssh; |
4329 | |
1ef619ae |
4330 | if (ssh->state == SSH_STATE_CLOSED) |
36f94d1f |
4331 | return 0; |
36f94d1f |
4332 | |
51470298 |
4333 | if (ssh->version == 1) { |
4334 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
32874aea |
4335 | PKT_INT, c->remoteid, |
a2724fba |
4336 | PKT_INT, len, PKTT_DATA, PKT_DATA, buf, len, |
9a10ecf4 |
4337 | PKTT_OTHER, PKT_END); |
5471d09a |
4338 | /* |
2e85c969 |
4339 | * In SSH-1 we can return 0 here - implying that forwarded |
5471d09a |
4340 | * connections are never individually throttled - because |
4341 | * the only circumstance that can cause throttling will be |
4342 | * the whole SSH connection backing up, in which case |
4343 | * _everything_ will be throttled as a whole. |
4344 | */ |
4345 | return 0; |
783415f8 |
4346 | } else { |
32874aea |
4347 | ssh2_add_channel_data(c, buf, len); |
5471d09a |
4348 | return ssh2_try_send(c); |
4349 | } |
4350 | } |
4351 | |
4352 | void sshfwd_unthrottle(struct ssh_channel *c, int bufsize) |
4353 | { |
51470298 |
4354 | Ssh ssh = c->ssh; |
ff68564b |
4355 | int buflimit; |
51470298 |
4356 | |
1ef619ae |
4357 | if (ssh->state == SSH_STATE_CLOSED) |
36f94d1f |
4358 | return; |
36f94d1f |
4359 | |
51470298 |
4360 | if (ssh->version == 1) { |
ff68564b |
4361 | buflimit = SSH1_BUFFER_LIMIT; |
5471d09a |
4362 | } else { |
ff68564b |
4363 | buflimit = c->v.v2.locmaxwin; |
4364 | ssh2_set_window(c, bufsize < buflimit ? buflimit - bufsize : 0); |
4365 | } |
4366 | if (c->throttling_conn && bufsize <= buflimit) { |
4367 | c->throttling_conn = 0; |
4368 | ssh_throttle_conn(ssh, -1); |
783415f8 |
4369 | } |
9c964e85 |
4370 | } |
4371 | |
06fadff5 |
4372 | static void ssh_queueing_handler(Ssh ssh, struct Packet *pktin) |
4373 | { |
4374 | struct queued_handler *qh = ssh->qhead; |
4375 | |
4376 | assert(qh != NULL); |
4377 | |
4378 | assert(pktin->type == qh->msg1 || pktin->type == qh->msg2); |
4379 | |
4380 | if (qh->msg1 > 0) { |
4381 | assert(ssh->packet_dispatch[qh->msg1] == ssh_queueing_handler); |
bc074147 |
4382 | ssh->packet_dispatch[qh->msg1] = ssh->q_saved_handler1; |
06fadff5 |
4383 | } |
4384 | if (qh->msg2 > 0) { |
4385 | assert(ssh->packet_dispatch[qh->msg2] == ssh_queueing_handler); |
bc074147 |
4386 | ssh->packet_dispatch[qh->msg2] = ssh->q_saved_handler2; |
06fadff5 |
4387 | } |
4388 | |
4389 | if (qh->next) { |
4390 | ssh->qhead = qh->next; |
4391 | |
4392 | if (ssh->qhead->msg1 > 0) { |
bc074147 |
4393 | ssh->q_saved_handler1 = ssh->packet_dispatch[ssh->qhead->msg1]; |
06fadff5 |
4394 | ssh->packet_dispatch[ssh->qhead->msg1] = ssh_queueing_handler; |
4395 | } |
4396 | if (ssh->qhead->msg2 > 0) { |
bc074147 |
4397 | ssh->q_saved_handler2 = ssh->packet_dispatch[ssh->qhead->msg2]; |
06fadff5 |
4398 | ssh->packet_dispatch[ssh->qhead->msg2] = ssh_queueing_handler; |
4399 | } |
4400 | } else { |
4401 | ssh->qhead = ssh->qtail = NULL; |
06fadff5 |
4402 | } |
4403 | |
4404 | qh->handler(ssh, pktin, qh->ctx); |
4405 | |
4406 | sfree(qh); |
4407 | } |
4408 | |
4409 | static void ssh_queue_handler(Ssh ssh, int msg1, int msg2, |
4410 | chandler_fn_t handler, void *ctx) |
4411 | { |
4412 | struct queued_handler *qh; |
4413 | |
4414 | qh = snew(struct queued_handler); |
4415 | qh->msg1 = msg1; |
4416 | qh->msg2 = msg2; |
4417 | qh->handler = handler; |
4418 | qh->ctx = ctx; |
4419 | qh->next = NULL; |
4420 | |
4421 | if (ssh->qtail == NULL) { |
4422 | ssh->qhead = qh; |
4423 | |
4424 | if (qh->msg1 > 0) { |
bc074147 |
4425 | ssh->q_saved_handler1 = ssh->packet_dispatch[ssh->qhead->msg1]; |
06fadff5 |
4426 | ssh->packet_dispatch[qh->msg1] = ssh_queueing_handler; |
4427 | } |
4428 | if (qh->msg2 > 0) { |
bc074147 |
4429 | ssh->q_saved_handler2 = ssh->packet_dispatch[ssh->qhead->msg2]; |
06fadff5 |
4430 | ssh->packet_dispatch[qh->msg2] = ssh_queueing_handler; |
4431 | } |
4432 | } else { |
4433 | ssh->qtail->next = qh; |
4434 | } |
4435 | ssh->qtail = qh; |
4436 | } |
4437 | |
4438 | static void ssh_rportfwd_succfail(Ssh ssh, struct Packet *pktin, void *ctx) |
4439 | { |
4440 | struct ssh_rportfwd *rpf, *pf = (struct ssh_rportfwd *)ctx; |
4441 | |
4442 | if (pktin->type == (ssh->version == 1 ? SSH1_SMSG_SUCCESS : |
4443 | SSH2_MSG_REQUEST_SUCCESS)) { |
4444 | logeventf(ssh, "Remote port forwarding from %s enabled", |
4445 | pf->sportdesc); |
4446 | } else { |
4447 | logeventf(ssh, "Remote port forwarding from %s refused", |
4448 | pf->sportdesc); |
4449 | |
4450 | rpf = del234(ssh->rportfwds, pf); |
4451 | assert(rpf == pf); |
8aea57fd |
4452 | pf->pfrec->remote = NULL; |
fda2feb1 |
4453 | free_rportfwd(pf); |
06fadff5 |
4454 | } |
4455 | } |
4456 | |
4a693cfc |
4457 | static void ssh_setup_portfwd(Ssh ssh, Conf *conf) |
06fadff5 |
4458 | { |
84328ddb |
4459 | struct ssh_portfwd *epf; |
4460 | int i; |
4a693cfc |
4461 | char *key, *val; |
06fadff5 |
4462 | |
fda2feb1 |
4463 | if (!ssh->portfwds) { |
4464 | ssh->portfwds = newtree234(ssh_portcmp); |
4465 | } else { |
4466 | /* |
4467 | * Go through the existing port forwardings and tag them |
84328ddb |
4468 | * with status==DESTROY. Any that we want to keep will be |
4469 | * re-enabled (status==KEEP) as we go through the |
4470 | * configuration and find out which bits are the same as |
4471 | * they were before. |
fda2feb1 |
4472 | */ |
4473 | struct ssh_portfwd *epf; |
4474 | int i; |
4475 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
84328ddb |
4476 | epf->status = DESTROY; |
fda2feb1 |
4477 | } |
4478 | |
4a693cfc |
4479 | for (val = conf_get_str_strs(conf, CONF_portfwd, NULL, &key); |
4480 | val != NULL; |
4481 | val = conf_get_str_strs(conf, CONF_portfwd, key, &key)) { |
4482 | char *kp, *kp2, *vp, *vp2; |
84328ddb |
4483 | char address_family, type; |
4484 | int sport,dport,sserv,dserv; |
4a693cfc |
4485 | char *sports, *dports, *saddr, *host; |
4486 | |
4487 | kp = key; |
84328ddb |
4488 | |
05581745 |
4489 | address_family = 'A'; |
4490 | type = 'L'; |
4a693cfc |
4491 | if (*kp == 'A' || *kp == '4' || *kp == '6') |
4492 | address_family = *kp++; |
4493 | if (*kp == 'L' || *kp == 'R') |
4494 | type = *kp++; |
4495 | |
4496 | if ((kp2 = strchr(kp, ':')) != NULL) { |
4497 | /* |
4498 | * There's a colon in the middle of the source port |
4499 | * string, which means that the part before it is |
4500 | * actually a source address. |
4501 | */ |
4502 | saddr = dupprintf("%.*s", (int)(kp2 - kp), kp); |
4503 | sports = kp2+1; |
06fadff5 |
4504 | } else { |
4a693cfc |
4505 | saddr = NULL; |
4506 | sports = kp; |
06fadff5 |
4507 | } |
4508 | sport = atoi(sports); |
4509 | sserv = 0; |
4510 | if (sport == 0) { |
4511 | sserv = 1; |
4512 | sport = net_service_lookup(sports); |
4513 | if (!sport) { |
4514 | logeventf(ssh, "Service lookup failed for source" |
4515 | " port \"%s\"", sports); |
4516 | } |
4517 | } |
4a693cfc |
4518 | |
4519 | if (type == 'L' && !strcmp(val, "D")) { |
4520 | /* dynamic forwarding */ |
4521 | host = NULL; |
4522 | dports = NULL; |
4523 | dport = -1; |
4524 | dserv = 0; |
4525 | type = 'D'; |
4526 | } else { |
4527 | /* ordinary forwarding */ |
4528 | vp = val; |
4529 | vp2 = vp + strcspn(vp, ":"); |
4530 | host = dupprintf("%.*s", (int)(vp2 - vp), vp); |
4531 | if (vp2) |
4532 | vp2++; |
4533 | dports = vp2; |
4534 | dport = atoi(dports); |
4535 | dserv = 0; |
4536 | if (dport == 0) { |
4537 | dserv = 1; |
4538 | dport = net_service_lookup(dports); |
4539 | if (!dport) { |
4540 | logeventf(ssh, "Service lookup failed for destination" |
4541 | " port \"%s\"", dports); |
4542 | } |
4543 | } |
4544 | } |
4545 | |
06fadff5 |
4546 | if (sport && dport) { |
4547 | /* Set up a description of the source port. */ |
fda2feb1 |
4548 | struct ssh_portfwd *pfrec, *epfrec; |
fda2feb1 |
4549 | |
4550 | pfrec = snew(struct ssh_portfwd); |
4551 | pfrec->type = type; |
4a693cfc |
4552 | pfrec->saddr = saddr; |
3fe92132 |
4553 | pfrec->sserv = sserv ? dupstr(sports) : NULL; |
fda2feb1 |
4554 | pfrec->sport = sport; |
4a693cfc |
4555 | pfrec->daddr = host; |
3fe92132 |
4556 | pfrec->dserv = dserv ? dupstr(dports) : NULL; |
fda2feb1 |
4557 | pfrec->dport = dport; |
4558 | pfrec->local = NULL; |
4559 | pfrec->remote = NULL; |
05581745 |
4560 | pfrec->addressfamily = (address_family == '4' ? ADDRTYPE_IPV4 : |
4561 | address_family == '6' ? ADDRTYPE_IPV6 : |
4562 | ADDRTYPE_UNSPEC); |
fda2feb1 |
4563 | |
4564 | epfrec = add234(ssh->portfwds, pfrec); |
4565 | if (epfrec != pfrec) { |
c00c1931 |
4566 | if (epfrec->status == DESTROY) { |
4567 | /* |
4568 | * We already have a port forwarding up and running |
4569 | * with precisely these parameters. Hence, no need |
4570 | * to do anything; simply re-tag the existing one |
4571 | * as KEEP. |
4572 | */ |
4573 | epfrec->status = KEEP; |
4574 | } |
fda2feb1 |
4575 | /* |
c00c1931 |
4576 | * Anything else indicates that there was a duplicate |
4577 | * in our input, which we'll silently ignore. |
fda2feb1 |
4578 | */ |
fda2feb1 |
4579 | free_portfwd(pfrec); |
84328ddb |
4580 | } else { |
4581 | pfrec->status = CREATE; |
4582 | } |
4a693cfc |
4583 | } else { |
4584 | sfree(saddr); |
4585 | sfree(host); |
84328ddb |
4586 | } |
4587 | } |
4588 | |
4589 | /* |
4590 | * Now go through and destroy any port forwardings which were |
4591 | * not re-enabled. |
4592 | */ |
4593 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
4594 | if (epf->status == DESTROY) { |
4595 | char *message; |
4596 | |
4597 | message = dupprintf("%s port forwarding from %s%s%d", |
4598 | epf->type == 'L' ? "local" : |
4599 | epf->type == 'R' ? "remote" : "dynamic", |
4600 | epf->saddr ? epf->saddr : "", |
4601 | epf->saddr ? ":" : "", |
4602 | epf->sport); |
4603 | |
4604 | if (epf->type != 'D') { |
4605 | char *msg2 = dupprintf("%s to %s:%d", message, |
4606 | epf->daddr, epf->dport); |
4607 | sfree(message); |
4608 | message = msg2; |
4609 | } |
4610 | |
4611 | logeventf(ssh, "Cancelling %s", message); |
4612 | sfree(message); |
4613 | |
8aea57fd |
4614 | /* epf->remote or epf->local may be NULL if setting up a |
4615 | * forwarding failed. */ |
84328ddb |
4616 | if (epf->remote) { |
4617 | struct ssh_rportfwd *rpf = epf->remote; |
4618 | struct Packet *pktout; |
4619 | |
4620 | /* |
4621 | * Cancel the port forwarding at the server |
4622 | * end. |
4623 | */ |
4624 | if (ssh->version == 1) { |
4625 | /* |
4626 | * We cannot cancel listening ports on the |
2e85c969 |
4627 | * server side in SSH-1! There's no message |
84328ddb |
4628 | * to support it. Instead, we simply remove |
4629 | * the rportfwd record from the local end |
4630 | * so that any connections the server tries |
4631 | * to make on it are rejected. |
4632 | */ |
4633 | } else { |
4634 | pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST); |
4635 | ssh2_pkt_addstring(pktout, "cancel-tcpip-forward"); |
4636 | ssh2_pkt_addbool(pktout, 0);/* _don't_ want reply */ |
4637 | if (epf->saddr) { |
4638 | ssh2_pkt_addstring(pktout, epf->saddr); |
4a693cfc |
4639 | } else if (conf_get_int(conf, CONF_rport_acceptall)) { |
4640 | /* XXX: rport_acceptall may not represent |
5188540b |
4641 | * what was used to open the original connection, |
4642 | * since it's reconfigurable. */ |
b1b5c358 |
4643 | ssh2_pkt_addstring(pktout, ""); |
84328ddb |
4644 | } else { |
b1b5c358 |
4645 | ssh2_pkt_addstring(pktout, "localhost"); |
84328ddb |
4646 | } |
4647 | ssh2_pkt_adduint32(pktout, epf->sport); |
4648 | ssh2_pkt_send(ssh, pktout); |
4649 | } |
4650 | |
4651 | del234(ssh->rportfwds, rpf); |
4652 | free_rportfwd(rpf); |
4653 | } else if (epf->local) { |
4654 | pfd_terminate(epf->local); |
4655 | } |
4656 | |
4657 | delpos234(ssh->portfwds, i); |
4658 | free_portfwd(epf); |
4659 | i--; /* so we don't skip one in the list */ |
4660 | } |
4661 | |
4662 | /* |
4663 | * And finally, set up any new port forwardings (status==CREATE). |
4664 | */ |
4665 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
4666 | if (epf->status == CREATE) { |
4667 | char *sportdesc, *dportdesc; |
3fe92132 |
4668 | sportdesc = dupprintf("%s%s%s%s%d%s", |
84328ddb |
4669 | epf->saddr ? epf->saddr : "", |
4670 | epf->saddr ? ":" : "", |
3fe92132 |
4671 | epf->sserv ? epf->sserv : "", |
4672 | epf->sserv ? "(" : "", |
4673 | epf->sport, |
4674 | epf->sserv ? ")" : ""); |
84328ddb |
4675 | if (epf->type == 'D') { |
4676 | dportdesc = NULL; |
4677 | } else { |
3fe92132 |
4678 | dportdesc = dupprintf("%s:%s%s%d%s", |
4679 | epf->daddr, |
4680 | epf->dserv ? epf->dserv : "", |
4681 | epf->dserv ? "(" : "", |
4682 | epf->dport, |
4683 | epf->dserv ? ")" : ""); |
84328ddb |
4684 | } |
05581745 |
4685 | |
84328ddb |
4686 | if (epf->type == 'L') { |
4687 | const char *err = pfd_addforward(epf->daddr, epf->dport, |
4688 | epf->saddr, epf->sport, |
4a693cfc |
4689 | ssh, conf, |
84328ddb |
4690 | &epf->local, |
4691 | epf->addressfamily); |
4692 | |
4693 | logeventf(ssh, "Local %sport %s forwarding to %s%s%s", |
4694 | epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " : |
4695 | epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "", |
4696 | sportdesc, dportdesc, |
4697 | err ? " failed: " : "", err ? err : ""); |
4698 | } else if (epf->type == 'D') { |
06fadff5 |
4699 | const char *err = pfd_addforward(NULL, -1, |
84328ddb |
4700 | epf->saddr, epf->sport, |
4a693cfc |
4701 | ssh, conf, |
84328ddb |
4702 | &epf->local, |
4703 | epf->addressfamily); |
4704 | |
4705 | logeventf(ssh, "Local %sport %s SOCKS dynamic forwarding%s%s", |
4706 | epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " : |
4707 | epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "", |
4708 | sportdesc, |
4709 | err ? " failed: " : "", err ? err : ""); |
06fadff5 |
4710 | } else { |
4711 | struct ssh_rportfwd *pf; |
4712 | |
4713 | /* |
4714 | * Ensure the remote port forwardings tree exists. |
4715 | */ |
4716 | if (!ssh->rportfwds) { |
4717 | if (ssh->version == 1) |
4718 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh1); |
4719 | else |
4720 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh2); |
4721 | } |
4722 | |
4723 | pf = snew(struct ssh_rportfwd); |
84328ddb |
4724 | strncpy(pf->dhost, epf->daddr, lenof(pf->dhost)-1); |
4725 | pf->dhost[lenof(pf->dhost)-1] = '\0'; |
4726 | pf->dport = epf->dport; |
4727 | pf->sport = epf->sport; |
06fadff5 |
4728 | if (add234(ssh->rportfwds, pf) != pf) { |
4729 | logeventf(ssh, "Duplicate remote port forwarding to %s:%d", |
84328ddb |
4730 | epf->daddr, epf->dport); |
06fadff5 |
4731 | sfree(pf); |
4732 | } else { |
4733 | logeventf(ssh, "Requesting remote port %s" |
84328ddb |
4734 | " forward to %s", sportdesc, dportdesc); |
06fadff5 |
4735 | |
4736 | pf->sportdesc = sportdesc; |
4737 | sportdesc = NULL; |
84328ddb |
4738 | epf->remote = pf; |
4739 | pf->pfrec = epf; |
06fadff5 |
4740 | |
4741 | if (ssh->version == 1) { |
4742 | send_packet(ssh, SSH1_CMSG_PORT_FORWARD_REQUEST, |
84328ddb |
4743 | PKT_INT, epf->sport, |
4744 | PKT_STR, epf->daddr, |
4745 | PKT_INT, epf->dport, |
06fadff5 |
4746 | PKT_END); |
4747 | ssh_queue_handler(ssh, SSH1_SMSG_SUCCESS, |
4748 | SSH1_SMSG_FAILURE, |
4749 | ssh_rportfwd_succfail, pf); |
4750 | } else { |
4751 | struct Packet *pktout; |
4752 | pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST); |
4753 | ssh2_pkt_addstring(pktout, "tcpip-forward"); |
4754 | ssh2_pkt_addbool(pktout, 1);/* want reply */ |
84328ddb |
4755 | if (epf->saddr) { |
4756 | ssh2_pkt_addstring(pktout, epf->saddr); |
4a693cfc |
4757 | } else if (conf_get_int(conf, CONF_rport_acceptall)) { |
b1b5c358 |
4758 | ssh2_pkt_addstring(pktout, ""); |
06fadff5 |
4759 | } else { |
b1b5c358 |
4760 | ssh2_pkt_addstring(pktout, "localhost"); |
06fadff5 |
4761 | } |
84328ddb |
4762 | ssh2_pkt_adduint32(pktout, epf->sport); |
06fadff5 |
4763 | ssh2_pkt_send(ssh, pktout); |
4764 | |
4765 | ssh_queue_handler(ssh, SSH2_MSG_REQUEST_SUCCESS, |
4766 | SSH2_MSG_REQUEST_FAILURE, |
4767 | ssh_rportfwd_succfail, pf); |
4768 | } |
4769 | } |
4770 | } |
4771 | sfree(sportdesc); |
84328ddb |
4772 | sfree(dportdesc); |
06fadff5 |
4773 | } |
06fadff5 |
4774 | } |
4775 | |
51df0ab5 |
4776 | static void ssh1_smsg_stdout_stderr_data(Ssh ssh, struct Packet *pktin) |
4777 | { |
4778 | char *string; |
4779 | int stringlen, bufsize; |
4780 | |
4781 | ssh_pkt_getstring(pktin, &string, &stringlen); |
4782 | if (string == NULL) { |
4783 | bombout(("Incoming terminal data packet was badly formed")); |
4784 | return; |
4785 | } |
4786 | |
4787 | bufsize = from_backend(ssh->frontend, pktin->type == SSH1_SMSG_STDERR_DATA, |
4788 | string, stringlen); |
4789 | if (!ssh->v1_stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) { |
4790 | ssh->v1_stdout_throttling = 1; |
ff68564b |
4791 | ssh_throttle_conn(ssh, +1); |
51df0ab5 |
4792 | } |
4793 | } |
4794 | |
4795 | static void ssh1_smsg_x11_open(Ssh ssh, struct Packet *pktin) |
4796 | { |
4797 | /* Remote side is trying to open a channel to talk to our |
4798 | * X-Server. Give them back a local channel number. */ |
4799 | struct ssh_channel *c; |
4800 | int remoteid = ssh_pkt_getuint32(pktin); |
4801 | |
4802 | logevent("Received X11 connect request"); |
4803 | /* Refuse if X11 forwarding is disabled. */ |
4804 | if (!ssh->X11_fwd_enabled) { |
4805 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4806 | PKT_INT, remoteid, PKT_END); |
4807 | logevent("Rejected X11 connect request"); |
4808 | } else { |
4809 | c = snew(struct ssh_channel); |
4810 | c->ssh = ssh; |
4811 | |
8def70c3 |
4812 | if (x11_init(&c->u.x11.s, ssh->x11disp, c, |
4a693cfc |
4813 | NULL, -1, ssh->conf) != NULL) { |
51df0ab5 |
4814 | logevent("Opening X11 forward connection failed"); |
4815 | sfree(c); |
4816 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4817 | PKT_INT, remoteid, PKT_END); |
4818 | } else { |
4819 | logevent |
4820 | ("Opening X11 forward connection succeeded"); |
4821 | c->remoteid = remoteid; |
64d6ff88 |
4822 | c->halfopen = FALSE; |
51df0ab5 |
4823 | c->localid = alloc_channel_id(ssh); |
4824 | c->closes = 0; |
bc06669b |
4825 | c->pending_eof = FALSE; |
ff68564b |
4826 | c->throttling_conn = 0; |
51df0ab5 |
4827 | c->type = CHAN_X11; /* identify channel type */ |
4828 | add234(ssh->channels, c); |
4829 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
4830 | PKT_INT, c->remoteid, PKT_INT, |
4831 | c->localid, PKT_END); |
4832 | logevent("Opened X11 forward channel"); |
4833 | } |
4834 | } |
4835 | } |
4836 | |
4837 | static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin) |
4838 | { |
4839 | /* Remote side is trying to open a channel to talk to our |
4840 | * agent. Give them back a local channel number. */ |
4841 | struct ssh_channel *c; |
4842 | int remoteid = ssh_pkt_getuint32(pktin); |
4843 | |
4844 | /* Refuse if agent forwarding is disabled. */ |
4845 | if (!ssh->agentfwd_enabled) { |
4846 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4847 | PKT_INT, remoteid, PKT_END); |
4848 | } else { |
4849 | c = snew(struct ssh_channel); |
4850 | c->ssh = ssh; |
4851 | c->remoteid = remoteid; |
64d6ff88 |
4852 | c->halfopen = FALSE; |
51df0ab5 |
4853 | c->localid = alloc_channel_id(ssh); |
4854 | c->closes = 0; |
bc06669b |
4855 | c->pending_eof = FALSE; |
ff68564b |
4856 | c->throttling_conn = 0; |
51df0ab5 |
4857 | c->type = CHAN_AGENT; /* identify channel type */ |
4858 | c->u.a.lensofar = 0; |
bc06669b |
4859 | c->u.a.message = NULL; |
be3f980d |
4860 | c->u.a.outstanding_requests = 0; |
51df0ab5 |
4861 | add234(ssh->channels, c); |
4862 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
4863 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
4864 | PKT_END); |
4865 | } |
4866 | } |
4867 | |
4868 | static void ssh1_msg_port_open(Ssh ssh, struct Packet *pktin) |
4869 | { |
4870 | /* Remote side is trying to open a channel to talk to a |
4871 | * forwarded port. Give them back a local channel number. */ |
05581745 |
4872 | struct ssh_rportfwd pf, *pfp; |
51df0ab5 |
4873 | int remoteid; |
4874 | int hostsize, port; |
fb983202 |
4875 | char *host; |
51df0ab5 |
4876 | const char *e; |
51df0ab5 |
4877 | |
4878 | remoteid = ssh_pkt_getuint32(pktin); |
4879 | ssh_pkt_getstring(pktin, &host, &hostsize); |
4880 | port = ssh_pkt_getuint32(pktin); |
4881 | |
4882 | if (hostsize >= lenof(pf.dhost)) |
4883 | hostsize = lenof(pf.dhost)-1; |
4884 | memcpy(pf.dhost, host, hostsize); |
4885 | pf.dhost[hostsize] = '\0'; |
4886 | pf.dport = port; |
05581745 |
4887 | pfp = find234(ssh->rportfwds, &pf, NULL); |
51df0ab5 |
4888 | |
05581745 |
4889 | if (pfp == NULL) { |
fb983202 |
4890 | logeventf(ssh, "Rejected remote port open request for %s:%d", |
4891 | pf.dhost, port); |
51df0ab5 |
4892 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4893 | PKT_INT, remoteid, PKT_END); |
4894 | } else { |
038ec85e |
4895 | struct ssh_channel *c = snew(struct ssh_channel); |
4896 | c->ssh = ssh; |
4897 | |
fb983202 |
4898 | logeventf(ssh, "Received remote port open request for %s:%d", |
4899 | pf.dhost, port); |
51df0ab5 |
4900 | e = pfd_newconnect(&c->u.pfd.s, pf.dhost, port, |
4a693cfc |
4901 | c, ssh->conf, pfp->pfrec->addressfamily); |
51df0ab5 |
4902 | if (e != NULL) { |
fb983202 |
4903 | logeventf(ssh, "Port open failed: %s", e); |
51df0ab5 |
4904 | sfree(c); |
4905 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
4906 | PKT_INT, remoteid, PKT_END); |
4907 | } else { |
4908 | c->remoteid = remoteid; |
64d6ff88 |
4909 | c->halfopen = FALSE; |
51df0ab5 |
4910 | c->localid = alloc_channel_id(ssh); |
4911 | c->closes = 0; |
bc06669b |
4912 | c->pending_eof = FALSE; |
ff68564b |
4913 | c->throttling_conn = 0; |
51df0ab5 |
4914 | c->type = CHAN_SOCKDATA; /* identify channel type */ |
4915 | add234(ssh->channels, c); |
4916 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
4917 | PKT_INT, c->remoteid, PKT_INT, |
4918 | c->localid, PKT_END); |
4919 | logevent("Forwarded port opened successfully"); |
4920 | } |
4921 | } |
4922 | } |
4923 | |
4924 | static void ssh1_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin) |
4925 | { |
4926 | unsigned int remoteid = ssh_pkt_getuint32(pktin); |
4927 | unsigned int localid = ssh_pkt_getuint32(pktin); |
4928 | struct ssh_channel *c; |
4929 | |
4930 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
4931 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
4932 | c->remoteid = localid; |
64d6ff88 |
4933 | c->halfopen = FALSE; |
51df0ab5 |
4934 | c->type = CHAN_SOCKDATA; |
ff68564b |
4935 | c->throttling_conn = 0; |
51df0ab5 |
4936 | pfd_confirm(c->u.pfd.s); |
4937 | } |
4938 | |
bc06669b |
4939 | if (c && c->pending_eof) { |
51df0ab5 |
4940 | /* |
4941 | * We have a pending close on this channel, |
4942 | * which we decided on before the server acked |
4943 | * the channel open. So now we know the |
4944 | * remoteid, we can close it again. |
4945 | */ |
bc06669b |
4946 | ssh_channel_try_eof(c); |
51df0ab5 |
4947 | } |
4948 | } |
4949 | |
4950 | static void ssh1_msg_channel_open_failure(Ssh ssh, struct Packet *pktin) |
4951 | { |
4952 | unsigned int remoteid = ssh_pkt_getuint32(pktin); |
4953 | struct ssh_channel *c; |
4954 | |
4955 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
4956 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
4957 | logevent("Forwarded connection refused by server"); |
4958 | pfd_close(c->u.pfd.s); |
4959 | del234(ssh->channels, c); |
4960 | sfree(c); |
4961 | } |
4962 | } |
4963 | |
4964 | static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin) |
4965 | { |
4966 | /* Remote side closes a channel. */ |
4967 | unsigned i = ssh_pkt_getuint32(pktin); |
4968 | struct ssh_channel *c; |
4969 | c = find234(ssh->channels, &i, ssh_channelfind); |
64d6ff88 |
4970 | if (c && !c->halfopen) { |
51df0ab5 |
4971 | |
bc06669b |
4972 | if (pktin->type == SSH1_MSG_CHANNEL_CLOSE && |
4973 | !(c->closes & CLOSES_RCVD_EOF)) { |
4974 | /* |
4975 | * Received CHANNEL_CLOSE, which we translate into |
4976 | * outgoing EOF. |
4977 | */ |
4978 | int send_close = FALSE; |
4979 | |
4980 | c->closes |= CLOSES_RCVD_EOF; |
4981 | |
4982 | switch (c->type) { |
4983 | case CHAN_X11: |
4984 | if (c->u.x11.s) |
4985 | x11_send_eof(c->u.x11.s); |
4986 | else |
4987 | send_close = TRUE; |
9fb91415 |
4988 | break; |
bc06669b |
4989 | case CHAN_SOCKDATA: |
4990 | if (c->u.pfd.s) |
9fb91415 |
4991 | pfd_send_eof(c->u.pfd.s); |
bc06669b |
4992 | else |
4993 | send_close = TRUE; |
9fb91415 |
4994 | break; |
bc06669b |
4995 | case CHAN_AGENT: |
4996 | send_close = TRUE; |
9fb91415 |
4997 | break; |
bc06669b |
4998 | } |
51df0ab5 |
4999 | |
bc06669b |
5000 | if (send_close && !(c->closes & CLOSES_SENT_EOF)) { |
5001 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, |
5002 | PKT_END); |
5003 | c->closes |= CLOSES_SENT_EOF; |
5004 | } |
5005 | } |
5006 | |
5007 | if (pktin->type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION && |
5008 | !(c->closes & CLOSES_RCVD_CLOSE)) { |
5009 | |
5010 | if (!(c->closes & CLOSES_SENT_EOF)) { |
5011 | bombout(("Received CHANNEL_CLOSE_CONFIRMATION for channel %d" |
5012 | " for which we never sent CHANNEL_CLOSE\n", i)); |
5013 | } |
5014 | |
5015 | c->closes |= CLOSES_RCVD_CLOSE; |
5016 | } |
5017 | |
5018 | if (!((CLOSES_SENT_EOF | CLOSES_RCVD_EOF) & ~c->closes) && |
5019 | !(c->closes & CLOSES_SENT_CLOSE)) { |
5020 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION, |
5021 | PKT_INT, c->remoteid, PKT_END); |
5022 | c->closes |= CLOSES_SENT_CLOSE; |
5023 | } |
5024 | |
5025 | if (!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes)) |
5026 | ssh_channel_destroy(c); |
51df0ab5 |
5027 | } else { |
5028 | bombout(("Received CHANNEL_CLOSE%s for %s channel %d\n", |
5029 | pktin->type == SSH1_MSG_CHANNEL_CLOSE ? "" : |
5030 | "_CONFIRMATION", c ? "half-open" : "nonexistent", |
5031 | i)); |
5032 | } |
5033 | } |
5034 | |
5035 | static void ssh1_msg_channel_data(Ssh ssh, struct Packet *pktin) |
5036 | { |
5037 | /* Data sent down one of our channels. */ |
5038 | int i = ssh_pkt_getuint32(pktin); |
5039 | char *p; |
6d44acc9 |
5040 | int len; |
51df0ab5 |
5041 | struct ssh_channel *c; |
5042 | |
5043 | ssh_pkt_getstring(pktin, &p, &len); |
5044 | |
5045 | c = find234(ssh->channels, &i, ssh_channelfind); |
5046 | if (c) { |
5047 | int bufsize = 0; |
5048 | switch (c->type) { |
5049 | case CHAN_X11: |
5050 | bufsize = x11_send(c->u.x11.s, p, len); |
5051 | break; |
5052 | case CHAN_SOCKDATA: |
5053 | bufsize = pfd_send(c->u.pfd.s, p, len); |
5054 | break; |
5055 | case CHAN_AGENT: |
5056 | /* Data for an agent message. Buffer it. */ |
5057 | while (len > 0) { |
5058 | if (c->u.a.lensofar < 4) { |
62ddb51e |
5059 | unsigned int l = min(4 - c->u.a.lensofar, (unsigned)len); |
51df0ab5 |
5060 | memcpy(c->u.a.msglen + c->u.a.lensofar, p, |
5061 | l); |
5062 | p += l; |
5063 | len -= l; |
5064 | c->u.a.lensofar += l; |
5065 | } |
5066 | if (c->u.a.lensofar == 4) { |
5067 | c->u.a.totallen = |
5068 | 4 + GET_32BIT(c->u.a.msglen); |
5069 | c->u.a.message = snewn(c->u.a.totallen, |
5070 | unsigned char); |
5071 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
5072 | } |
5073 | if (c->u.a.lensofar >= 4 && len > 0) { |
aa63ab7e |
5074 | unsigned int l = |
51df0ab5 |
5075 | min(c->u.a.totallen - c->u.a.lensofar, |
62ddb51e |
5076 | (unsigned)len); |
51df0ab5 |
5077 | memcpy(c->u.a.message + c->u.a.lensofar, p, |
5078 | l); |
5079 | p += l; |
5080 | len -= l; |
5081 | c->u.a.lensofar += l; |
5082 | } |
5083 | if (c->u.a.lensofar == c->u.a.totallen) { |
5084 | void *reply; |
5085 | int replylen; |
be3f980d |
5086 | c->u.a.outstanding_requests++; |
51df0ab5 |
5087 | if (agent_query(c->u.a.message, |
5088 | c->u.a.totallen, |
5089 | &reply, &replylen, |
5090 | ssh_agentf_callback, c)) |
5091 | ssh_agentf_callback(c, reply, replylen); |
5092 | sfree(c->u.a.message); |
5093 | c->u.a.lensofar = 0; |
5094 | } |
5095 | } |
5096 | bufsize = 0; /* agent channels never back up */ |
5097 | break; |
5098 | } |
ff68564b |
5099 | if (!c->throttling_conn && bufsize > SSH1_BUFFER_LIMIT) { |
5100 | c->throttling_conn = 1; |
5101 | ssh_throttle_conn(ssh, +1); |
51df0ab5 |
5102 | } |
5103 | } |
5104 | } |
5105 | |
5106 | static void ssh1_smsg_exit_status(Ssh ssh, struct Packet *pktin) |
5107 | { |
51df0ab5 |
5108 | ssh->exitcode = ssh_pkt_getuint32(pktin); |
fb983202 |
5109 | logeventf(ssh, "Server sent command exit status %d", ssh->exitcode); |
51df0ab5 |
5110 | send_packet(ssh, SSH1_CMSG_EXIT_CONFIRMATION, PKT_END); |
5111 | /* |
5112 | * In case `helpful' firewalls or proxies tack |
5113 | * extra human-readable text on the end of the |
5114 | * session which we might mistake for another |
5115 | * encrypted packet, we close the session once |
5116 | * we've sent EXIT_CONFIRMATION. |
5117 | */ |
9e296bfa |
5118 | ssh_disconnect(ssh, NULL, NULL, 0, TRUE); |
51df0ab5 |
5119 | } |
5120 | |
c6ccd5c2 |
5121 | /* Helper function to deal with sending tty modes for REQUEST_PTY */ |
5122 | static void ssh1_send_ttymode(void *data, char *mode, char *val) |
5123 | { |
5124 | struct Packet *pktout = (struct Packet *)data; |
5125 | int i = 0; |
5126 | unsigned int arg = 0; |
5127 | while (strcmp(mode, ssh_ttymodes[i].mode) != 0) i++; |
5128 | if (i == lenof(ssh_ttymodes)) return; |
5129 | switch (ssh_ttymodes[i].type) { |
5130 | case TTY_OP_CHAR: |
5131 | arg = ssh_tty_parse_specchar(val); |
5132 | break; |
5133 | case TTY_OP_BOOL: |
5134 | arg = ssh_tty_parse_boolean(val); |
5135 | break; |
5136 | } |
5137 | ssh2_pkt_addbyte(pktout, ssh_ttymodes[i].opcode); |
5138 | ssh2_pkt_addbyte(pktout, arg); |
5139 | } |
5140 | |
5141 | |
b09eaa88 |
5142 | static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen, |
5143 | struct Packet *pktin) |
32874aea |
5144 | { |
b09eaa88 |
5145 | crBegin(ssh->do_ssh1_connection_crstate); |
fb09bf1c |
5146 | |
51df0ab5 |
5147 | ssh->packet_dispatch[SSH1_SMSG_STDOUT_DATA] = |
5148 | ssh->packet_dispatch[SSH1_SMSG_STDERR_DATA] = |
5149 | ssh1_smsg_stdout_stderr_data; |
5150 | |
5151 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_CONFIRMATION] = |
5152 | ssh1_msg_channel_open_confirmation; |
5153 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_FAILURE] = |
5154 | ssh1_msg_channel_open_failure; |
5155 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE] = |
5156 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION] = |
5157 | ssh1_msg_channel_close; |
5158 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_DATA] = ssh1_msg_channel_data; |
5159 | ssh->packet_dispatch[SSH1_SMSG_EXIT_STATUS] = ssh1_smsg_exit_status; |
5160 | |
4a693cfc |
5161 | if (conf_get_int(ssh->conf, CONF_agentfwd) && agent_exists()) { |
32874aea |
5162 | logevent("Requesting agent forwarding"); |
51470298 |
5163 | send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END); |
32874aea |
5164 | do { |
5165 | crReturnV; |
ff3187f6 |
5166 | } while (!pktin); |
5167 | if (pktin->type != SSH1_SMSG_SUCCESS |
5168 | && pktin->type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
5169 | bombout(("Protocol confusion")); |
7ffdbc1a |
5170 | crStopV; |
ff3187f6 |
5171 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
32874aea |
5172 | logevent("Agent forwarding refused"); |
5173 | } else { |
5174 | logevent("Agent forwarding enabled"); |
51470298 |
5175 | ssh->agentfwd_enabled = TRUE; |
51df0ab5 |
5176 | ssh->packet_dispatch[SSH1_SMSG_AGENT_OPEN] = ssh1_smsg_agent_open; |
db7d555c |
5177 | } |
dacbd0e8 |
5178 | } |
5179 | |
4a693cfc |
5180 | if (conf_get_int(ssh->conf, CONF_x11_forward) && |
5181 | (ssh->x11disp = x11_setup_display(conf_get_str(ssh->conf, CONF_x11_display), |
5182 | conf_get_int(ssh->conf, CONF_x11_auth), ssh->conf))) { |
32874aea |
5183 | logevent("Requesting X11 forwarding"); |
f68353be |
5184 | /* |
5185 | * Note that while we blank the X authentication data here, we don't |
5186 | * take any special action to blank the start of an X11 channel, |
5187 | * so using MIT-MAGIC-COOKIE-1 and actually opening an X connection |
5188 | * without having session blanking enabled is likely to leak your |
5189 | * cookie into the log. |
5190 | */ |
51470298 |
5191 | if (ssh->v1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) { |
5192 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
8def70c3 |
5193 | PKT_STR, ssh->x11disp->remoteauthprotoname, |
5194 | PKTT_PASSWORD, |
5195 | PKT_STR, ssh->x11disp->remoteauthdatastring, |
5196 | PKTT_OTHER, |
5197 | PKT_INT, ssh->x11disp->screennum, |
421d6835 |
5198 | PKT_END); |
32874aea |
5199 | } else { |
51470298 |
5200 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
8def70c3 |
5201 | PKT_STR, ssh->x11disp->remoteauthprotoname, |
5202 | PKTT_PASSWORD, |
5203 | PKT_STR, ssh->x11disp->remoteauthdatastring, |
5204 | PKTT_OTHER, |
5205 | PKT_END); |
32874aea |
5206 | } |
5207 | do { |
5208 | crReturnV; |
ff3187f6 |
5209 | } while (!pktin); |
5210 | if (pktin->type != SSH1_SMSG_SUCCESS |
5211 | && pktin->type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
5212 | bombout(("Protocol confusion")); |
7ffdbc1a |
5213 | crStopV; |
ff3187f6 |
5214 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
32874aea |
5215 | logevent("X11 forwarding refused"); |
5216 | } else { |
5217 | logevent("X11 forwarding enabled"); |
51470298 |
5218 | ssh->X11_fwd_enabled = TRUE; |
51df0ab5 |
5219 | ssh->packet_dispatch[SSH1_SMSG_X11_OPEN] = ssh1_smsg_x11_open; |
9c964e85 |
5220 | } |
5221 | } |
5222 | |
4a693cfc |
5223 | ssh_setup_portfwd(ssh, ssh->conf); |
06fadff5 |
5224 | ssh->packet_dispatch[SSH1_MSG_PORT_OPEN] = ssh1_msg_port_open; |
d74d141c |
5225 | |
4a693cfc |
5226 | if (!conf_get_int(ssh->conf, CONF_nopty)) { |
c6ccd5c2 |
5227 | struct Packet *pkt; |
a5dd8467 |
5228 | /* Unpick the terminal-speed string. */ |
5229 | /* XXX perhaps we should allow no speeds to be sent. */ |
db219738 |
5230 | ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */ |
4a693cfc |
5231 | sscanf(conf_get_str(ssh->conf, CONF_termspeed), "%d,%d", &ssh->ospeed, &ssh->ispeed); |
a5dd8467 |
5232 | /* Send the pty request. */ |
c6ccd5c2 |
5233 | pkt = ssh1_pkt_init(SSH1_CMSG_REQUEST_PTY); |
4a693cfc |
5234 | ssh_pkt_addstring(pkt, conf_get_str(ssh->conf, CONF_termtype)); |
c6ccd5c2 |
5235 | ssh_pkt_adduint32(pkt, ssh->term_height); |
5236 | ssh_pkt_adduint32(pkt, ssh->term_width); |
5237 | ssh_pkt_adduint32(pkt, 0); /* width in pixels */ |
5238 | ssh_pkt_adduint32(pkt, 0); /* height in pixels */ |
4a693cfc |
5239 | parse_ttymodes(ssh, ssh1_send_ttymode, (void *)pkt); |
c6ccd5c2 |
5240 | ssh_pkt_addbyte(pkt, SSH1_TTY_OP_ISPEED); |
5241 | ssh_pkt_adduint32(pkt, ssh->ispeed); |
5242 | ssh_pkt_addbyte(pkt, SSH1_TTY_OP_OSPEED); |
5243 | ssh_pkt_adduint32(pkt, ssh->ospeed); |
5244 | ssh_pkt_addbyte(pkt, SSH_TTY_OP_END); |
5245 | s_wrpkt(ssh, pkt); |
51470298 |
5246 | ssh->state = SSH_STATE_INTERMED; |
32874aea |
5247 | do { |
5248 | crReturnV; |
ff3187f6 |
5249 | } while (!pktin); |
5250 | if (pktin->type != SSH1_SMSG_SUCCESS |
5251 | && pktin->type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
5252 | bombout(("Protocol confusion")); |
7ffdbc1a |
5253 | crStopV; |
ff3187f6 |
5254 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
51470298 |
5255 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
5256 | ssh->editing = ssh->echoing = 1; |
4a202fc6 |
5257 | } else { |
5258 | logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)", |
5259 | ssh->ospeed, ssh->ispeed); |
5260 | ssh->got_pty = TRUE; |
5261 | } |
0965bee0 |
5262 | } else { |
51470298 |
5263 | ssh->editing = ssh->echoing = 1; |
374330e2 |
5264 | } |
5265 | |
4a693cfc |
5266 | if (conf_get_int(ssh->conf, CONF_compression)) { |
51470298 |
5267 | send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END); |
32874aea |
5268 | do { |
5269 | crReturnV; |
ff3187f6 |
5270 | } while (!pktin); |
5271 | if (pktin->type != SSH1_SMSG_SUCCESS |
5272 | && pktin->type != SSH1_SMSG_FAILURE) { |
6b5cf8b4 |
5273 | bombout(("Protocol confusion")); |
7ffdbc1a |
5274 | crStopV; |
ff3187f6 |
5275 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
51470298 |
5276 | c_write_str(ssh, "Server refused to compress\r\n"); |
32874aea |
5277 | } |
4ba9b64b |
5278 | logevent("Started compression"); |
51470298 |
5279 | ssh->v1_compressing = TRUE; |
5366aed8 |
5280 | ssh->cs_comp_ctx = zlib_compress_init(); |
5281 | logevent("Initialised zlib (RFC1950) compression"); |
5282 | ssh->sc_comp_ctx = zlib_decompress_init(); |
5283 | logevent("Initialised zlib (RFC1950) decompression"); |
4ba9b64b |
5284 | } |
5285 | |
fd5e5847 |
5286 | /* |
5287 | * Start the shell or command. |
5288 | * |
2e85c969 |
5289 | * Special case: if the first-choice command is an SSH-2 |
fd5e5847 |
5290 | * subsystem (hence not usable here) and the second choice |
5291 | * exists, we fall straight back to that. |
5292 | */ |
5293 | { |
4a693cfc |
5294 | char *cmd = conf_get_str(ssh->conf, CONF_remote_cmd); |
fd5e5847 |
5295 | |
4a693cfc |
5296 | if (conf_get_int(ssh->conf, CONF_ssh_subsys) && |
5297 | conf_get_str(ssh->conf, CONF_remote_cmd2)) { |
5298 | cmd = conf_get_str(ssh->conf, CONF_remote_cmd2); |
51470298 |
5299 | ssh->fallback_cmd = TRUE; |
fd5e5847 |
5300 | } |
5301 | if (*cmd) |
51470298 |
5302 | send_packet(ssh, SSH1_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END); |
fd5e5847 |
5303 | else |
51470298 |
5304 | send_packet(ssh, SSH1_CMSG_EXEC_SHELL, PKT_END); |
fd5e5847 |
5305 | logevent("Started session"); |
5306 | } |
374330e2 |
5307 | |
51470298 |
5308 | ssh->state = SSH_STATE_SESSION; |
5309 | if (ssh->size_needed) |
5310 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
5311 | if (ssh->eof_needed) |
5312 | ssh_special(ssh, TS_EOF); |
374330e2 |
5313 | |
b9d7bcad |
5314 | if (ssh->ldisc) |
5315 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
51470298 |
5316 | ssh->send_ok = 1; |
5317 | ssh->channels = newtree234(ssh_channelcmp); |
374330e2 |
5318 | while (1) { |
d74d141c |
5319 | |
51df0ab5 |
5320 | /* |
5321 | * By this point, most incoming packets are already being |
5322 | * handled by the dispatch table, and we need only pay |
5323 | * attention to the unusual ones. |
5324 | */ |
0357890f |
5325 | |
51df0ab5 |
5326 | crReturnV; |
5327 | if (pktin) { |
5328 | if (pktin->type == SSH1_SMSG_SUCCESS) { |
972a41c8 |
5329 | /* may be from EXEC_SHELL on some servers */ |
ff3187f6 |
5330 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
972a41c8 |
5331 | /* may be from EXEC_SHELL on some servers |
374330e2 |
5332 | * if no pty is available or in other odd cases. Ignore */ |
374330e2 |
5333 | } else { |
ff3187f6 |
5334 | bombout(("Strange packet received: type %d", pktin->type)); |
7ffdbc1a |
5335 | crStopV; |
374330e2 |
5336 | } |
5337 | } else { |
8df7a775 |
5338 | while (inlen > 0) { |
5339 | int len = min(inlen, 512); |
a2724fba |
5340 | send_packet(ssh, SSH1_CMSG_STDIN_DATA, |
5341 | PKT_INT, len, PKTT_DATA, PKT_DATA, in, len, |
9a10ecf4 |
5342 | PKTT_OTHER, PKT_END); |
8df7a775 |
5343 | in += len; |
5344 | inlen -= len; |
5345 | } |
374330e2 |
5346 | } |
5347 | } |
5348 | |
5349 | crFinishV; |
5350 | } |
5351 | |
5352 | /* |
2e85c969 |
5353 | * Handle the top-level SSH-2 protocol. |
b09eaa88 |
5354 | */ |
5355 | static void ssh1_msg_debug(Ssh ssh, struct Packet *pktin) |
5356 | { |
fb983202 |
5357 | char *msg; |
b09eaa88 |
5358 | int msglen; |
5359 | |
5360 | ssh_pkt_getstring(pktin, &msg, &msglen); |
fb983202 |
5361 | logeventf(ssh, "Remote debug message: %.*s", msglen, msg); |
b09eaa88 |
5362 | } |
5363 | |
5364 | static void ssh1_msg_disconnect(Ssh ssh, struct Packet *pktin) |
5365 | { |
5366 | /* log reason code in disconnect message */ |
5367 | char *msg; |
5368 | int msglen; |
5369 | |
5370 | ssh_pkt_getstring(pktin, &msg, &msglen); |
5371 | bombout(("Server sent disconnect message:\n\"%.*s\"", msglen, msg)); |
5372 | } |
5373 | |
409bfa77 |
5374 | static void ssh_msg_ignore(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
5375 | { |
5376 | /* Do nothing, because we're ignoring it! Duhh. */ |
5377 | } |
5378 | |
5379 | static void ssh1_protocol_setup(Ssh ssh) |
5380 | { |
5381 | int i; |
5382 | |
5383 | /* |
5384 | * Most messages are handled by the coroutines. |
5385 | */ |
5386 | for (i = 0; i < 256; i++) |
5387 | ssh->packet_dispatch[i] = NULL; |
5388 | |
5389 | /* |
5390 | * These special message types we install handlers for. |
5391 | */ |
5392 | ssh->packet_dispatch[SSH1_MSG_DISCONNECT] = ssh1_msg_disconnect; |
5393 | ssh->packet_dispatch[SSH1_MSG_IGNORE] = ssh_msg_ignore; |
5394 | ssh->packet_dispatch[SSH1_MSG_DEBUG] = ssh1_msg_debug; |
5395 | } |
5396 | |
1c1a7262 |
5397 | static void ssh1_protocol(Ssh ssh, void *vin, int inlen, |
b09eaa88 |
5398 | struct Packet *pktin) |
5399 | { |
1c1a7262 |
5400 | unsigned char *in=(unsigned char*)vin; |
b09eaa88 |
5401 | if (ssh->state == SSH_STATE_CLOSED) |
5402 | return; |
5403 | |
5404 | if (pktin && ssh->packet_dispatch[pktin->type]) { |
5405 | ssh->packet_dispatch[pktin->type](ssh, pktin); |
5406 | return; |
5407 | } |
5408 | |
5409 | if (!ssh->protocol_initial_phase_done) { |
5410 | if (do_ssh1_login(ssh, in, inlen, pktin)) |
5411 | ssh->protocol_initial_phase_done = TRUE; |
5412 | else |
5413 | return; |
5414 | } |
5415 | |
5416 | do_ssh1_connection(ssh, in, inlen, pktin); |
5417 | } |
5418 | |
5419 | /* |
e5574168 |
5420 | * Utility routine for decoding comma-separated strings in KEXINIT. |
5421 | */ |
32874aea |
5422 | static int in_commasep_string(char *needle, char *haystack, int haylen) |
5423 | { |
57356d63 |
5424 | int needlen; |
5425 | if (!needle || !haystack) /* protect against null pointers */ |
5426 | return 0; |
5427 | needlen = strlen(needle); |
e5574168 |
5428 | while (1) { |
32874aea |
5429 | /* |
5430 | * Is it at the start of the string? |
5431 | */ |
5432 | if (haylen >= needlen && /* haystack is long enough */ |
5433 | !memcmp(needle, haystack, needlen) && /* initial match */ |
5434 | (haylen == needlen || haystack[needlen] == ',') |
5435 | /* either , or EOS follows */ |
5436 | ) |
5437 | return 1; |
5438 | /* |
5439 | * If not, search for the next comma and resume after that. |
5440 | * If no comma found, terminate. |
5441 | */ |
5442 | while (haylen > 0 && *haystack != ',') |
5443 | haylen--, haystack++; |
5444 | if (haylen == 0) |
5445 | return 0; |
5446 | haylen--, haystack++; /* skip over comma itself */ |
e5574168 |
5447 | } |
5448 | } |
5449 | |
5450 | /* |
b59743d5 |
5451 | * Similar routine for checking whether we have the first string in a list. |
5452 | */ |
5453 | static int first_in_commasep_string(char *needle, char *haystack, int haylen) |
5454 | { |
5455 | int needlen; |
5456 | if (!needle || !haystack) /* protect against null pointers */ |
5457 | return 0; |
5458 | needlen = strlen(needle); |
5459 | /* |
5460 | * Is it at the start of the string? |
5461 | */ |
5462 | if (haylen >= needlen && /* haystack is long enough */ |
5463 | !memcmp(needle, haystack, needlen) && /* initial match */ |
5464 | (haylen == needlen || haystack[needlen] == ',') |
5465 | /* either , or EOS follows */ |
5466 | ) |
5467 | return 1; |
5468 | return 0; |
5469 | } |
5470 | |
5471 | |
5472 | /* |
2e85c969 |
5473 | * SSH-2 key creation method. |
754c0df9 |
5474 | * (Currently assumes 2 lots of any hash are sufficient to generate |
5475 | * keys/IVs for any cipher/MAC. SSH2_MKKEY_ITERS documents this assumption.) |
d39f364a |
5476 | */ |
754c0df9 |
5477 | #define SSH2_MKKEY_ITERS (2) |
b672f405 |
5478 | static void ssh2_mkkey(Ssh ssh, Bignum K, unsigned char *H, char chr, |
d8baa528 |
5479 | unsigned char *keyspace) |
32874aea |
5480 | { |
b672f405 |
5481 | const struct ssh_hash *h = ssh->kex->hash; |
5482 | void *s; |
5483 | /* First hlen bytes. */ |
5484 | s = h->init(); |
51470298 |
5485 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
b672f405 |
5486 | hash_mpint(h, s, K); |
5487 | h->bytes(s, H, h->hlen); |
5488 | h->bytes(s, &chr, 1); |
5489 | h->bytes(s, ssh->v2_session_id, ssh->v2_session_id_len); |
5490 | h->final(s, keyspace); |
5491 | /* Next hlen bytes. */ |
5492 | s = h->init(); |
51470298 |
5493 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
b672f405 |
5494 | hash_mpint(h, s, K); |
5495 | h->bytes(s, H, h->hlen); |
5496 | h->bytes(s, keyspace, h->hlen); |
5497 | h->final(s, keyspace + h->hlen); |
d39f364a |
5498 | } |
5499 | |
5500 | /* |
2e85c969 |
5501 | * Handle the SSH-2 transport layer. |
e5574168 |
5502 | */ |
0b106f61 |
5503 | static void do_ssh2_transport(Ssh ssh, void *vin, int inlen, |
ff3187f6 |
5504 | struct Packet *pktin) |
e5574168 |
5505 | { |
1c1a7262 |
5506 | unsigned char *in = (unsigned char *)vin; |
51470298 |
5507 | struct do_ssh2_transport_state { |
30919997 |
5508 | int crLine; |
4763c1c2 |
5509 | int nbits, pbits, warn_kex, warn_cscipher, warn_sccipher; |
51470298 |
5510 | Bignum p, g, e, f, K; |
3dc9a6a7 |
5511 | void *our_kexinit; |
5512 | int our_kexinitlen; |
51470298 |
5513 | int kex_init_value, kex_reply_value; |
5514 | const struct ssh_mac **maclist; |
5515 | int nmacs; |
5516 | const struct ssh2_cipher *cscipher_tobe; |
5517 | const struct ssh2_cipher *sccipher_tobe; |
5518 | const struct ssh_mac *csmac_tobe; |
5519 | const struct ssh_mac *scmac_tobe; |
5520 | const struct ssh_compress *cscomp_tobe; |
5521 | const struct ssh_compress *sccomp_tobe; |
fae1a71b |
5522 | char *hostkeydata, *sigdata, *rsakeydata, *keystr, *fingerprint; |
5523 | int hostkeylen, siglen, rsakeylen; |
51470298 |
5524 | void *hkey; /* actual host key */ |
fae1a71b |
5525 | void *rsakey; /* for RSA kex */ |
754c0df9 |
5526 | unsigned char exchange_hash[SSH2_KEX_MAX_HASH_LEN]; |
83e7d008 |
5527 | int n_preferred_kex; |
34557659 |
5528 | const struct ssh_kexes *preferred_kex[KEX_MAX]; |
51470298 |
5529 | int n_preferred_ciphers; |
5530 | const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX]; |
5531 | const struct ssh_compress *preferred_comp; |
ab18ccff |
5532 | int userauth_succeeded; /* for delayed compression */ |
5533 | int pending_compression; |
e13bba36 |
5534 | int got_session_id, activated_authconn; |
ff3187f6 |
5535 | struct Packet *pktout; |
3d9449a1 |
5536 | int dlgret; |
5537 | int guessok; |
4763c1c2 |
5538 | int ignorepkt; |
51470298 |
5539 | }; |
5540 | crState(do_ssh2_transport_state); |
5541 | |
30919997 |
5542 | crBeginState; |
51470298 |
5543 | |
5544 | s->cscipher_tobe = s->sccipher_tobe = NULL; |
5545 | s->csmac_tobe = s->scmac_tobe = NULL; |
5546 | s->cscomp_tobe = s->sccomp_tobe = NULL; |
5547 | |
e13bba36 |
5548 | s->got_session_id = s->activated_authconn = FALSE; |
ab18ccff |
5549 | s->userauth_succeeded = FALSE; |
5550 | s->pending_compression = FALSE; |
e5574168 |
5551 | |
e13bba36 |
5552 | /* |
5553 | * Be prepared to work around the buggy MAC problem. |
5554 | */ |
5555 | if (ssh->remote_bugs & BUG_SSH2_HMAC) |
5556 | s->maclist = buggymacs, s->nmacs = lenof(buggymacs); |
5557 | else |
5558 | s->maclist = macs, s->nmacs = lenof(macs); |
5559 | |
5560 | begin_key_exchange: |
acab36bc |
5561 | ssh->pkt_kctx = SSH2_PKTCTX_NOKEX; |
51470298 |
5562 | { |
cc18f256 |
5563 | int i, j, k, commalist_started; |
e13bba36 |
5564 | |
51470298 |
5565 | /* |
83e7d008 |
5566 | * Set up the preferred key exchange. (NULL => warn below here) |
5567 | */ |
5568 | s->n_preferred_kex = 0; |
5569 | for (i = 0; i < KEX_MAX; i++) { |
4a693cfc |
5570 | switch (conf_get_int_int(ssh->conf, CONF_ssh_kexlist, i)) { |
83e7d008 |
5571 | case KEX_DHGEX: |
5572 | s->preferred_kex[s->n_preferred_kex++] = |
5573 | &ssh_diffiehellman_gex; |
5574 | break; |
5575 | case KEX_DHGROUP14: |
5576 | s->preferred_kex[s->n_preferred_kex++] = |
5577 | &ssh_diffiehellman_group14; |
5578 | break; |
5579 | case KEX_DHGROUP1: |
5580 | s->preferred_kex[s->n_preferred_kex++] = |
5581 | &ssh_diffiehellman_group1; |
5582 | break; |
fae1a71b |
5583 | case KEX_RSA: |
5584 | s->preferred_kex[s->n_preferred_kex++] = |
5585 | &ssh_rsa_kex; |
5586 | break; |
754c0df9 |
5587 | case KEX_WARN: |
83e7d008 |
5588 | /* Flag for later. Don't bother if it's the last in |
5589 | * the list. */ |
5590 | if (i < KEX_MAX - 1) { |
5591 | s->preferred_kex[s->n_preferred_kex++] = NULL; |
5592 | } |
5593 | break; |
5594 | } |
5595 | } |
83e7d008 |
5596 | |
83e7d008 |
5597 | /* |
51470298 |
5598 | * Set up the preferred ciphers. (NULL => warn below here) |
5599 | */ |
5600 | s->n_preferred_ciphers = 0; |
5601 | for (i = 0; i < CIPHER_MAX; i++) { |
4a693cfc |
5602 | switch (conf_get_int_int(ssh->conf, CONF_ssh_cipherlist, i)) { |
51470298 |
5603 | case CIPHER_BLOWFISH: |
5604 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_blowfish; |
5605 | break; |
5606 | case CIPHER_DES: |
4a693cfc |
5607 | if (conf_get_int(ssh->conf, CONF_ssh2_des_cbc)) { |
51470298 |
5608 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_des; |
5609 | } |
5610 | break; |
5611 | case CIPHER_3DES: |
5612 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_3des; |
5613 | break; |
5614 | case CIPHER_AES: |
5615 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_aes; |
5616 | break; |
a2add208 |
5617 | case CIPHER_ARCFOUR: |
5618 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_arcfour; |
5619 | break; |
51470298 |
5620 | case CIPHER_WARN: |
5621 | /* Flag for later. Don't bother if it's the last in |
5622 | * the list. */ |
5623 | if (i < CIPHER_MAX - 1) { |
5624 | s->preferred_ciphers[s->n_preferred_ciphers++] = NULL; |
5625 | } |
5626 | break; |
ca20bfcf |
5627 | } |
ca20bfcf |
5628 | } |
7591b9ff |
5629 | |
e13bba36 |
5630 | /* |
5631 | * Set up preferred compression. |
5632 | */ |
4a693cfc |
5633 | if (conf_get_int(ssh->conf, CONF_compression)) |
e13bba36 |
5634 | s->preferred_comp = &ssh_zlib; |
5635 | else |
5636 | s->preferred_comp = &ssh_comp_none; |
51470298 |
5637 | |
5638 | /* |
590f6a5f |
5639 | * Enable queueing of outgoing auth- or connection-layer |
5640 | * packets while we are in the middle of a key exchange. |
5641 | */ |
5642 | ssh->queueing = TRUE; |
5643 | |
5644 | /* |
9442dd57 |
5645 | * Flag that KEX is in progress. |
5646 | */ |
5647 | ssh->kex_in_progress = TRUE; |
5648 | |
5649 | /* |
51470298 |
5650 | * Construct and send our key exchange packet. |
5651 | */ |
ff3187f6 |
5652 | s->pktout = ssh2_pkt_init(SSH2_MSG_KEXINIT); |
51470298 |
5653 | for (i = 0; i < 16; i++) |
ff3187f6 |
5654 | ssh2_pkt_addbyte(s->pktout, (unsigned char) random_byte()); |
51470298 |
5655 | /* List key exchange algorithms. */ |
ff3187f6 |
5656 | ssh2_pkt_addstring_start(s->pktout); |
83e7d008 |
5657 | commalist_started = 0; |
5658 | for (i = 0; i < s->n_preferred_kex; i++) { |
34557659 |
5659 | const struct ssh_kexes *k = s->preferred_kex[i]; |
83e7d008 |
5660 | if (!k) continue; /* warning flag */ |
34557659 |
5661 | for (j = 0; j < k->nkexes; j++) { |
5662 | if (commalist_started) |
5663 | ssh2_pkt_addstring_str(s->pktout, ","); |
5664 | ssh2_pkt_addstring_str(s->pktout, k->list[j]->name); |
5665 | commalist_started = 1; |
5666 | } |
32874aea |
5667 | } |
51470298 |
5668 | /* List server host key algorithms. */ |
ff3187f6 |
5669 | ssh2_pkt_addstring_start(s->pktout); |
51470298 |
5670 | for (i = 0; i < lenof(hostkey_algs); i++) { |
ff3187f6 |
5671 | ssh2_pkt_addstring_str(s->pktout, hostkey_algs[i]->name); |
51470298 |
5672 | if (i < lenof(hostkey_algs) - 1) |
ff3187f6 |
5673 | ssh2_pkt_addstring_str(s->pktout, ","); |
32874aea |
5674 | } |
cc18f256 |
5675 | /* List encryption algorithms (client->server then server->client). */ |
5676 | for (k = 0; k < 2; k++) { |
5677 | ssh2_pkt_addstring_start(s->pktout); |
5678 | commalist_started = 0; |
5679 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
5680 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
5681 | if (!c) continue; /* warning flag */ |
5682 | for (j = 0; j < c->nciphers; j++) { |
5683 | if (commalist_started) |
5684 | ssh2_pkt_addstring_str(s->pktout, ","); |
5685 | ssh2_pkt_addstring_str(s->pktout, c->list[j]->name); |
5686 | commalist_started = 1; |
5687 | } |
51470298 |
5688 | } |
5689 | } |
cc18f256 |
5690 | /* List MAC algorithms (client->server then server->client). */ |
5691 | for (j = 0; j < 2; j++) { |
5692 | ssh2_pkt_addstring_start(s->pktout); |
5693 | for (i = 0; i < s->nmacs; i++) { |
5694 | ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name); |
5695 | if (i < s->nmacs - 1) |
ff3187f6 |
5696 | ssh2_pkt_addstring_str(s->pktout, ","); |
51470298 |
5697 | } |
5698 | } |
ab18ccff |
5699 | /* List client->server compression algorithms, |
5700 | * then server->client compression algorithms. (We use the |
5701 | * same set twice.) */ |
5702 | for (j = 0; j < 2; j++) { |
5703 | ssh2_pkt_addstring_start(s->pktout); |
5704 | assert(lenof(compressions) > 1); |
5705 | /* Prefer non-delayed versions */ |
5706 | ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name); |
5707 | /* We don't even list delayed versions of algorithms until |
5708 | * they're allowed to be used, to avoid a race. See the end of |
5709 | * this function. */ |
5710 | if (s->userauth_succeeded && s->preferred_comp->delayed_name) { |
ff3187f6 |
5711 | ssh2_pkt_addstring_str(s->pktout, ","); |
ab18ccff |
5712 | ssh2_pkt_addstring_str(s->pktout, |
5713 | s->preferred_comp->delayed_name); |
f6e0abe2 |
5714 | } |
ab18ccff |
5715 | for (i = 0; i < lenof(compressions); i++) { |
5716 | const struct ssh_compress *c = compressions[i]; |
5717 | if (c != s->preferred_comp) { |
5718 | ssh2_pkt_addstring_str(s->pktout, ","); |
5719 | ssh2_pkt_addstring_str(s->pktout, c->name); |
5720 | if (s->userauth_succeeded && c->delayed_name) { |
5721 | ssh2_pkt_addstring_str(s->pktout, ","); |
5722 | ssh2_pkt_addstring_str(s->pktout, c->delayed_name); |
5723 | } |
5724 | } |
f6e0abe2 |
5725 | } |
51470298 |
5726 | } |
5727 | /* List client->server languages. Empty list. */ |
ff3187f6 |
5728 | ssh2_pkt_addstring_start(s->pktout); |
51470298 |
5729 | /* List server->client languages. Empty list. */ |
ff3187f6 |
5730 | ssh2_pkt_addstring_start(s->pktout); |
51470298 |
5731 | /* First KEX packet does _not_ follow, because we're not that brave. */ |
ff3187f6 |
5732 | ssh2_pkt_addbool(s->pktout, FALSE); |
51470298 |
5733 | /* Reserved. */ |
ff3187f6 |
5734 | ssh2_pkt_adduint32(s->pktout, 0); |
e5574168 |
5735 | } |
0db56f73 |
5736 | |
3dc9a6a7 |
5737 | s->our_kexinitlen = s->pktout->length - 5; |
5738 | s->our_kexinit = snewn(s->our_kexinitlen, unsigned char); |
5739 | memcpy(s->our_kexinit, s->pktout->data + 5, s->our_kexinitlen); |
0db56f73 |
5740 | |
590f6a5f |
5741 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
e5574168 |
5742 | |
ff3187f6 |
5743 | if (!pktin) |
0b106f61 |
5744 | crWaitUntilV(pktin); |
e5574168 |
5745 | |
5746 | /* |
5747 | * Now examine the other side's KEXINIT to see what we're up |
5748 | * to. |
5749 | */ |
51470298 |
5750 | { |
4763c1c2 |
5751 | char *str, *preferred; |
3d9449a1 |
5752 | int i, j, len; |
51470298 |
5753 | |
ff3187f6 |
5754 | if (pktin->type != SSH2_MSG_KEXINIT) { |
6b5cf8b4 |
5755 | bombout(("expected key exchange packet from server")); |
0b106f61 |
5756 | crStopV; |
32874aea |
5757 | } |
51470298 |
5758 | ssh->kex = NULL; |
5759 | ssh->hostkey = NULL; |
5760 | s->cscipher_tobe = NULL; |
5761 | s->sccipher_tobe = NULL; |
5762 | s->csmac_tobe = NULL; |
5763 | s->scmac_tobe = NULL; |
5764 | s->cscomp_tobe = NULL; |
5765 | s->sccomp_tobe = NULL; |
4763c1c2 |
5766 | s->warn_kex = s->warn_cscipher = s->warn_sccipher = FALSE; |
5767 | |
ff3187f6 |
5768 | pktin->savedpos += 16; /* skip garbage cookie */ |
5769 | ssh_pkt_getstring(pktin, &str, &len); /* key exchange algorithms */ |
4763c1c2 |
5770 | |
5771 | preferred = NULL; |
83e7d008 |
5772 | for (i = 0; i < s->n_preferred_kex; i++) { |
34557659 |
5773 | const struct ssh_kexes *k = s->preferred_kex[i]; |
83e7d008 |
5774 | if (!k) { |
4763c1c2 |
5775 | s->warn_kex = TRUE; |
5776 | } else { |
34557659 |
5777 | for (j = 0; j < k->nkexes; j++) { |
5778 | if (!preferred) preferred = k->list[j]->name; |
5779 | if (in_commasep_string(k->list[j]->name, str, len)) { |
5780 | ssh->kex = k->list[j]; |
5781 | break; |
5782 | } |
5783 | } |
83e7d008 |
5784 | } |
4763c1c2 |
5785 | if (ssh->kex) |
51470298 |
5786 | break; |
32874aea |
5787 | } |
83e7d008 |
5788 | if (!ssh->kex) { |
5789 | bombout(("Couldn't agree a key exchange algorithm (available: %s)", |
5790 | str ? str : "(null)")); |
0b106f61 |
5791 | crStopV; |
83e7d008 |
5792 | } |
b59743d5 |
5793 | /* |
5794 | * Note that the server's guess is considered wrong if it doesn't match |
5795 | * the first algorithm in our list, even if it's still the algorithm |
5796 | * we end up using. |
5797 | */ |
4763c1c2 |
5798 | s->guessok = first_in_commasep_string(preferred, str, len); |
ff3187f6 |
5799 | ssh_pkt_getstring(pktin, &str, &len); /* host key algorithms */ |
51470298 |
5800 | for (i = 0; i < lenof(hostkey_algs); i++) { |
5801 | if (in_commasep_string(hostkey_algs[i]->name, str, len)) { |
5802 | ssh->hostkey = hostkey_algs[i]; |
5803 | break; |
5804 | } |
5805 | } |
fc40b431 |
5806 | if (!ssh->hostkey) { |
5807 | bombout(("Couldn't agree a host key algorithm (available: %s)", |
5808 | str ? str : "(null)")); |
0b106f61 |
5809 | crStopV; |
fc40b431 |
5810 | } |
5811 | |
3d9449a1 |
5812 | s->guessok = s->guessok && |
b59743d5 |
5813 | first_in_commasep_string(hostkey_algs[0]->name, str, len); |
ff3187f6 |
5814 | ssh_pkt_getstring(pktin, &str, &len); /* client->server cipher */ |
51470298 |
5815 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
5816 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
5817 | if (!c) { |
4763c1c2 |
5818 | s->warn_cscipher = TRUE; |
51470298 |
5819 | } else { |
5820 | for (j = 0; j < c->nciphers; j++) { |
5821 | if (in_commasep_string(c->list[j]->name, str, len)) { |
5822 | s->cscipher_tobe = c->list[j]; |
5823 | break; |
5824 | } |
ca20bfcf |
5825 | } |
32874aea |
5826 | } |
4763c1c2 |
5827 | if (s->cscipher_tobe) |
51470298 |
5828 | break; |
32874aea |
5829 | } |
51470298 |
5830 | if (!s->cscipher_tobe) { |
6b5cf8b4 |
5831 | bombout(("Couldn't agree a client-to-server cipher (available: %s)", |
57356d63 |
5832 | str ? str : "(null)")); |
0b106f61 |
5833 | crStopV; |
ca20bfcf |
5834 | } |
0ef8f407 |
5835 | |
ff3187f6 |
5836 | ssh_pkt_getstring(pktin, &str, &len); /* server->client cipher */ |
51470298 |
5837 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
5838 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
5839 | if (!c) { |
4763c1c2 |
5840 | s->warn_sccipher = TRUE; |
51470298 |
5841 | } else { |
5842 | for (j = 0; j < c->nciphers; j++) { |
5843 | if (in_commasep_string(c->list[j]->name, str, len)) { |
5844 | s->sccipher_tobe = c->list[j]; |
5845 | break; |
5846 | } |
ca20bfcf |
5847 | } |
32874aea |
5848 | } |
4763c1c2 |
5849 | if (s->sccipher_tobe) |
51470298 |
5850 | break; |
32874aea |
5851 | } |
51470298 |
5852 | if (!s->sccipher_tobe) { |
6b5cf8b4 |
5853 | bombout(("Couldn't agree a server-to-client cipher (available: %s)", |
57356d63 |
5854 | str ? str : "(null)")); |
0b106f61 |
5855 | crStopV; |
ca20bfcf |
5856 | } |
0ef8f407 |
5857 | |
ff3187f6 |
5858 | ssh_pkt_getstring(pktin, &str, &len); /* client->server mac */ |
51470298 |
5859 | for (i = 0; i < s->nmacs; i++) { |
5860 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
5861 | s->csmac_tobe = s->maclist[i]; |
5862 | break; |
5863 | } |
32874aea |
5864 | } |
ff3187f6 |
5865 | ssh_pkt_getstring(pktin, &str, &len); /* server->client mac */ |
51470298 |
5866 | for (i = 0; i < s->nmacs; i++) { |
5867 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
5868 | s->scmac_tobe = s->maclist[i]; |
5869 | break; |
5870 | } |
32874aea |
5871 | } |
ff3187f6 |
5872 | ssh_pkt_getstring(pktin, &str, &len); /* client->server compression */ |
51470298 |
5873 | for (i = 0; i < lenof(compressions) + 1; i++) { |
5874 | const struct ssh_compress *c = |
5875 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
5876 | if (in_commasep_string(c->name, str, len)) { |
5877 | s->cscomp_tobe = c; |
5878 | break; |
ab18ccff |
5879 | } else if (in_commasep_string(c->delayed_name, str, len)) { |
5880 | if (s->userauth_succeeded) { |
5881 | s->cscomp_tobe = c; |
5882 | break; |
5883 | } else { |
5884 | s->pending_compression = TRUE; /* try this later */ |
5885 | } |
51470298 |
5886 | } |
32874aea |
5887 | } |
ff3187f6 |
5888 | ssh_pkt_getstring(pktin, &str, &len); /* server->client compression */ |
51470298 |
5889 | for (i = 0; i < lenof(compressions) + 1; i++) { |
5890 | const struct ssh_compress *c = |
5891 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
5892 | if (in_commasep_string(c->name, str, len)) { |
5893 | s->sccomp_tobe = c; |
5894 | break; |
ab18ccff |
5895 | } else if (in_commasep_string(c->delayed_name, str, len)) { |
5896 | if (s->userauth_succeeded) { |
5897 | s->sccomp_tobe = c; |
5898 | break; |
5899 | } else { |
5900 | s->pending_compression = TRUE; /* try this later */ |
5901 | } |
51470298 |
5902 | } |
32874aea |
5903 | } |
ab18ccff |
5904 | if (s->pending_compression) { |
5905 | logevent("Server supports delayed compression; " |
5906 | "will try this later"); |
5907 | } |
b59743d5 |
5908 | ssh_pkt_getstring(pktin, &str, &len); /* client->server language */ |
5909 | ssh_pkt_getstring(pktin, &str, &len); /* server->client language */ |
4763c1c2 |
5910 | s->ignorepkt = ssh2_pkt_getbool(pktin) && !s->guessok; |
5911 | |
42fee8d6 |
5912 | ssh->exhash = ssh->kex->hash->init(); |
5913 | hash_string(ssh->kex->hash, ssh->exhash, ssh->v_c, strlen(ssh->v_c)); |
5914 | hash_string(ssh->kex->hash, ssh->exhash, ssh->v_s, strlen(ssh->v_s)); |
5915 | hash_string(ssh->kex->hash, ssh->exhash, |
5916 | s->our_kexinit, s->our_kexinitlen); |
5917 | sfree(s->our_kexinit); |
5918 | if (pktin->length > 5) |
5919 | hash_string(ssh->kex->hash, ssh->exhash, |
5920 | pktin->data + 5, pktin->length - 5); |
5921 | |
4763c1c2 |
5922 | if (s->warn_kex) { |
5923 | ssh_set_frozen(ssh, 1); |
5924 | s->dlgret = askalg(ssh->frontend, "key-exchange algorithm", |
5925 | ssh->kex->name, |
5926 | ssh_dialog_callback, ssh); |
5927 | if (s->dlgret < 0) { |
5928 | do { |
0b106f61 |
5929 | crReturnV; |
4763c1c2 |
5930 | if (pktin) { |
5931 | bombout(("Unexpected data from server while" |
5932 | " waiting for user response")); |
0b106f61 |
5933 | crStopV; |
4763c1c2 |
5934 | } |
5935 | } while (pktin || inlen > 0); |
5936 | s->dlgret = ssh->user_response; |
5937 | } |
5938 | ssh_set_frozen(ssh, 0); |
5939 | if (s->dlgret == 0) { |
9e296bfa |
5940 | ssh_disconnect(ssh, "User aborted at kex warning", NULL, |
5941 | 0, TRUE); |
0b106f61 |
5942 | crStopV; |
4763c1c2 |
5943 | } |
5944 | } |
5945 | |
5946 | if (s->warn_cscipher) { |
5947 | ssh_set_frozen(ssh, 1); |
5948 | s->dlgret = askalg(ssh->frontend, |
5949 | "client-to-server cipher", |
5950 | s->cscipher_tobe->name, |
5951 | ssh_dialog_callback, ssh); |
5952 | if (s->dlgret < 0) { |
5953 | do { |
0b106f61 |
5954 | crReturnV; |
4763c1c2 |
5955 | if (pktin) { |
5956 | bombout(("Unexpected data from server while" |
5957 | " waiting for user response")); |
0b106f61 |
5958 | crStopV; |
4763c1c2 |
5959 | } |
5960 | } while (pktin || inlen > 0); |
5961 | s->dlgret = ssh->user_response; |
5962 | } |
5963 | ssh_set_frozen(ssh, 0); |
5964 | if (s->dlgret == 0) { |
9e296bfa |
5965 | ssh_disconnect(ssh, "User aborted at cipher warning", NULL, |
5966 | 0, TRUE); |
0b106f61 |
5967 | crStopV; |
4763c1c2 |
5968 | } |
5969 | } |
5970 | |
5971 | if (s->warn_sccipher) { |
5972 | ssh_set_frozen(ssh, 1); |
5973 | s->dlgret = askalg(ssh->frontend, |
5974 | "server-to-client cipher", |
5975 | s->sccipher_tobe->name, |
5976 | ssh_dialog_callback, ssh); |
5977 | if (s->dlgret < 0) { |
5978 | do { |
0b106f61 |
5979 | crReturnV; |
4763c1c2 |
5980 | if (pktin) { |
5981 | bombout(("Unexpected data from server while" |
5982 | " waiting for user response")); |
0b106f61 |
5983 | crStopV; |
4763c1c2 |
5984 | } |
5985 | } while (pktin || inlen > 0); |
5986 | s->dlgret = ssh->user_response; |
5987 | } |
5988 | ssh_set_frozen(ssh, 0); |
5989 | if (s->dlgret == 0) { |
9e296bfa |
5990 | ssh_disconnect(ssh, "User aborted at cipher warning", NULL, |
5991 | 0, TRUE); |
0b106f61 |
5992 | crStopV; |
4763c1c2 |
5993 | } |
5994 | } |
5995 | |
5996 | if (s->ignorepkt) /* first_kex_packet_follows */ |
0b106f61 |
5997 | crWaitUntilV(pktin); /* Ignore packet */ |
e5574168 |
5998 | } |
e5574168 |
5999 | |
fae1a71b |
6000 | if (ssh->kex->main_type == KEXTYPE_DH) { |
30774ba3 |
6001 | /* |
6002 | * Work out the number of bits of key we will need from the |
6003 | * key exchange. We start with the maximum key length of |
6004 | * either cipher... |
6005 | */ |
6006 | { |
6007 | int csbits, scbits; |
6008 | |
6009 | csbits = s->cscipher_tobe->keylen; |
6010 | scbits = s->sccipher_tobe->keylen; |
6011 | s->nbits = (csbits > scbits ? csbits : scbits); |
6012 | } |
6013 | /* The keys only have hlen-bit entropy, since they're based on |
6014 | * a hash. So cap the key size at hlen bits. */ |
6015 | if (s->nbits > ssh->kex->hash->hlen * 8) |
6016 | s->nbits = ssh->kex->hash->hlen * 8; |
6017 | |
6018 | /* |
6019 | * If we're doing Diffie-Hellman group exchange, start by |
6020 | * requesting a group. |
6021 | */ |
6022 | if (!ssh->kex->pdata) { |
6023 | logevent("Doing Diffie-Hellman group exchange"); |
acab36bc |
6024 | ssh->pkt_kctx = SSH2_PKTCTX_DHGEX; |
30774ba3 |
6025 | /* |
6026 | * Work out how big a DH group we will need to allow that |
6027 | * much data. |
6028 | */ |
6029 | s->pbits = 512 << ((s->nbits - 1) / 64); |
6030 | s->pktout = ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST); |
6031 | ssh2_pkt_adduint32(s->pktout, s->pbits); |
6032 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
6033 | |
0b106f61 |
6034 | crWaitUntilV(pktin); |
30774ba3 |
6035 | if (pktin->type != SSH2_MSG_KEX_DH_GEX_GROUP) { |
6036 | bombout(("expected key exchange group packet from server")); |
0b106f61 |
6037 | crStopV; |
30774ba3 |
6038 | } |
6039 | s->p = ssh2_pkt_getmp(pktin); |
6040 | s->g = ssh2_pkt_getmp(pktin); |
6041 | if (!s->p || !s->g) { |
6042 | bombout(("unable to read mp-ints from incoming group packet")); |
0b106f61 |
6043 | crStopV; |
30774ba3 |
6044 | } |
6045 | ssh->kex_ctx = dh_setup_gex(s->p, s->g); |
6046 | s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT; |
6047 | s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY; |
6048 | } else { |
acab36bc |
6049 | ssh->pkt_kctx = SSH2_PKTCTX_DHGROUP; |
30774ba3 |
6050 | ssh->kex_ctx = dh_setup_group(ssh->kex); |
6051 | s->kex_init_value = SSH2_MSG_KEXDH_INIT; |
6052 | s->kex_reply_value = SSH2_MSG_KEXDH_REPLY; |
6053 | logeventf(ssh, "Using Diffie-Hellman with standard group \"%s\"", |
6054 | ssh->kex->groupname); |
6055 | } |
e5574168 |
6056 | |
30774ba3 |
6057 | logeventf(ssh, "Doing Diffie-Hellman key exchange with hash %s", |
6058 | ssh->kex->hash->text_name); |
6059 | /* |
6060 | * Now generate and send e for Diffie-Hellman. |
6061 | */ |
6062 | set_busy_status(ssh->frontend, BUSY_CPU); /* this can take a while */ |
6063 | s->e = dh_create_e(ssh->kex_ctx, s->nbits * 2); |
6064 | s->pktout = ssh2_pkt_init(s->kex_init_value); |
6065 | ssh2_pkt_addmp(s->pktout, s->e); |
6066 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
6067 | |
6068 | set_busy_status(ssh->frontend, BUSY_WAITING); /* wait for server */ |
0b106f61 |
6069 | crWaitUntilV(pktin); |
30774ba3 |
6070 | if (pktin->type != s->kex_reply_value) { |
6071 | bombout(("expected key exchange reply packet from server")); |
0b106f61 |
6072 | crStopV; |
30774ba3 |
6073 | } |
6074 | set_busy_status(ssh->frontend, BUSY_CPU); /* cogitate */ |
6075 | ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen); |
6076 | s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen); |
6077 | s->f = ssh2_pkt_getmp(pktin); |
6078 | if (!s->f) { |
6079 | bombout(("unable to parse key exchange reply packet")); |
0b106f61 |
6080 | crStopV; |
30774ba3 |
6081 | } |
6082 | ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen); |
e5574168 |
6083 | |
30774ba3 |
6084 | s->K = dh_find_K(ssh->kex_ctx, s->f); |
e5574168 |
6085 | |
30774ba3 |
6086 | /* We assume everything from now on will be quick, and it might |
6087 | * involve user interaction. */ |
6088 | set_busy_status(ssh->frontend, BUSY_NOT); |
755e0524 |
6089 | |
30774ba3 |
6090 | hash_string(ssh->kex->hash, ssh->exhash, s->hostkeydata, s->hostkeylen); |
6091 | if (!ssh->kex->pdata) { |
6092 | hash_uint32(ssh->kex->hash, ssh->exhash, s->pbits); |
6093 | hash_mpint(ssh->kex->hash, ssh->exhash, s->p); |
6094 | hash_mpint(ssh->kex->hash, ssh->exhash, s->g); |
6095 | } |
6096 | hash_mpint(ssh->kex->hash, ssh->exhash, s->e); |
6097 | hash_mpint(ssh->kex->hash, ssh->exhash, s->f); |
6098 | |
6099 | dh_cleanup(ssh->kex_ctx); |
6100 | freebn(s->f); |
6101 | if (!ssh->kex->pdata) { |
6102 | freebn(s->g); |
6103 | freebn(s->p); |
6104 | } |
fae1a71b |
6105 | } else { |
6106 | logeventf(ssh, "Doing RSA key exchange with hash %s", |
6107 | ssh->kex->hash->text_name); |
acab36bc |
6108 | ssh->pkt_kctx = SSH2_PKTCTX_RSAKEX; |
fae1a71b |
6109 | /* |
6110 | * RSA key exchange. First expect a KEXRSA_PUBKEY packet |
6111 | * from the server. |
6112 | */ |
0b106f61 |
6113 | crWaitUntilV(pktin); |
fae1a71b |
6114 | if (pktin->type != SSH2_MSG_KEXRSA_PUBKEY) { |
6115 | bombout(("expected RSA public key packet from server")); |
0b106f61 |
6116 | crStopV; |
fae1a71b |
6117 | } |
6118 | |
6119 | ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen); |
6120 | hash_string(ssh->kex->hash, ssh->exhash, |
6121 | s->hostkeydata, s->hostkeylen); |
6122 | s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen); |
6123 | |
6124 | { |
6125 | char *keydata; |
6126 | ssh_pkt_getstring(pktin, &keydata, &s->rsakeylen); |
6127 | s->rsakeydata = snewn(s->rsakeylen, char); |
6128 | memcpy(s->rsakeydata, keydata, s->rsakeylen); |
6129 | } |
6130 | |
6131 | s->rsakey = ssh_rsakex_newkey(s->rsakeydata, s->rsakeylen); |
6132 | if (!s->rsakey) { |
6133 | sfree(s->rsakeydata); |
6134 | bombout(("unable to parse RSA public key from server")); |
0b106f61 |
6135 | crStopV; |
fae1a71b |
6136 | } |
6137 | |
6138 | hash_string(ssh->kex->hash, ssh->exhash, s->rsakeydata, s->rsakeylen); |
6139 | |
6140 | /* |
6141 | * Next, set up a shared secret K, of precisely KLEN - |
6142 | * 2*HLEN - 49 bits, where KLEN is the bit length of the |
6143 | * RSA key modulus and HLEN is the bit length of the hash |
6144 | * we're using. |
6145 | */ |
6146 | { |
6147 | int klen = ssh_rsakex_klen(s->rsakey); |
6148 | int nbits = klen - (2*ssh->kex->hash->hlen*8 + 49); |
6149 | int i, byte = 0; |
6150 | unsigned char *kstr1, *kstr2, *outstr; |
6151 | int kstr1len, kstr2len, outstrlen; |
6152 | |
6153 | s->K = bn_power_2(nbits - 1); |
6154 | |
6155 | for (i = 0; i < nbits; i++) { |
6156 | if ((i & 7) == 0) { |
6157 | byte = random_byte(); |
6158 | } |
6159 | bignum_set_bit(s->K, i, (byte >> (i & 7)) & 1); |
6160 | } |
6161 | |
6162 | /* |
6163 | * Encode this as an mpint. |
6164 | */ |
6165 | kstr1 = ssh2_mpint_fmt(s->K, &kstr1len); |
6166 | kstr2 = snewn(kstr2len = 4 + kstr1len, unsigned char); |
6167 | PUT_32BIT(kstr2, kstr1len); |
6168 | memcpy(kstr2 + 4, kstr1, kstr1len); |
6169 | |
6170 | /* |
6171 | * Encrypt it with the given RSA key. |
6172 | */ |
6173 | outstrlen = (klen + 7) / 8; |
6174 | outstr = snewn(outstrlen, unsigned char); |
6175 | ssh_rsakex_encrypt(ssh->kex->hash, kstr2, kstr2len, |
6176 | outstr, outstrlen, s->rsakey); |
6177 | |
6178 | /* |
6179 | * And send it off in a return packet. |
6180 | */ |
6181 | s->pktout = ssh2_pkt_init(SSH2_MSG_KEXRSA_SECRET); |
6182 | ssh2_pkt_addstring_start(s->pktout); |
7108a872 |
6183 | ssh2_pkt_addstring_data(s->pktout, (char *)outstr, outstrlen); |
fae1a71b |
6184 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
6185 | |
6186 | hash_string(ssh->kex->hash, ssh->exhash, outstr, outstrlen); |
6187 | |
6188 | sfree(kstr2); |
6189 | sfree(kstr1); |
6190 | sfree(outstr); |
6191 | } |
6192 | |
6193 | ssh_rsakex_freekey(s->rsakey); |
6194 | |
0b106f61 |
6195 | crWaitUntilV(pktin); |
fae1a71b |
6196 | if (pktin->type != SSH2_MSG_KEXRSA_DONE) { |
6197 | sfree(s->rsakeydata); |
6198 | bombout(("expected signature packet from server")); |
0b106f61 |
6199 | crStopV; |
fae1a71b |
6200 | } |
6201 | |
6202 | ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen); |
6203 | |
6204 | sfree(s->rsakeydata); |
6205 | } |
6206 | |
b672f405 |
6207 | hash_mpint(ssh->kex->hash, ssh->exhash, s->K); |
6208 | assert(ssh->kex->hash->hlen <= sizeof(s->exchange_hash)); |
6209 | ssh->kex->hash->final(ssh->exhash, s->exchange_hash); |
e5574168 |
6210 | |
fabd1805 |
6211 | ssh->kex_ctx = NULL; |
3709bfe9 |
6212 | |
7cca0d81 |
6213 | #if 0 |
765c4200 |
6214 | debug(("Exchange hash is:\n")); |
b672f405 |
6215 | dmemdump(s->exchange_hash, ssh->kex->hash->hlen); |
7cca0d81 |
6216 | #endif |
6217 | |
51470298 |
6218 | if (!s->hkey || |
6219 | !ssh->hostkey->verifysig(s->hkey, s->sigdata, s->siglen, |
b672f405 |
6220 | (char *)s->exchange_hash, |
6221 | ssh->kex->hash->hlen)) { |
6b5cf8b4 |
6222 | bombout(("Server's host key did not match the signature supplied")); |
0b106f61 |
6223 | crStopV; |
8d5de777 |
6224 | } |
e5574168 |
6225 | |
6226 | /* |
7cca0d81 |
6227 | * Authenticate remote host: verify host key. (We've already |
6228 | * checked the signature of the exchange hash.) |
e5574168 |
6229 | */ |
51470298 |
6230 | s->keystr = ssh->hostkey->fmtkey(s->hkey); |
6231 | s->fingerprint = ssh->hostkey->fingerprint(s->hkey); |
3d9449a1 |
6232 | ssh_set_frozen(ssh, 1); |
6233 | s->dlgret = verify_ssh_host_key(ssh->frontend, |
6234 | ssh->savedhost, ssh->savedport, |
6235 | ssh->hostkey->keytype, s->keystr, |
6236 | s->fingerprint, |
6237 | ssh_dialog_callback, ssh); |
6238 | if (s->dlgret < 0) { |
6239 | do { |
0b106f61 |
6240 | crReturnV; |
3d9449a1 |
6241 | if (pktin) { |
6242 | bombout(("Unexpected data from server while waiting" |
6243 | " for user host key response")); |
0b106f61 |
6244 | crStopV; |
3d9449a1 |
6245 | } |
6246 | } while (pktin || inlen > 0); |
6247 | s->dlgret = ssh->user_response; |
6248 | } |
6249 | ssh_set_frozen(ssh, 0); |
6250 | if (s->dlgret == 0) { |
9e296bfa |
6251 | ssh_disconnect(ssh, "User aborted at host key verification", NULL, |
6252 | 0, TRUE); |
0b106f61 |
6253 | crStopV; |
3d9449a1 |
6254 | } |
e13bba36 |
6255 | if (!s->got_session_id) { /* don't bother logging this in rekeys */ |
5e0d7cb8 |
6256 | logevent("Host key fingerprint is:"); |
51470298 |
6257 | logevent(s->fingerprint); |
5e0d7cb8 |
6258 | } |
51470298 |
6259 | sfree(s->fingerprint); |
6260 | sfree(s->keystr); |
6261 | ssh->hostkey->freekey(s->hkey); |
d39f364a |
6262 | |
6263 | /* |
9442dd57 |
6264 | * The exchange hash from the very first key exchange is also |
6265 | * the session id, used in session key construction and |
6266 | * authentication. |
6267 | */ |
e13bba36 |
6268 | if (!s->got_session_id) { |
b672f405 |
6269 | assert(sizeof(s->exchange_hash) <= sizeof(ssh->v2_session_id)); |
9442dd57 |
6270 | memcpy(ssh->v2_session_id, s->exchange_hash, |
6271 | sizeof(s->exchange_hash)); |
b672f405 |
6272 | ssh->v2_session_id_len = ssh->kex->hash->hlen; |
6052bb76 |
6273 | assert(ssh->v2_session_id_len <= sizeof(ssh->v2_session_id)); |
e13bba36 |
6274 | s->got_session_id = TRUE; |
6275 | } |
9442dd57 |
6276 | |
6277 | /* |
7cca0d81 |
6278 | * Send SSH2_MSG_NEWKEYS. |
d39f364a |
6279 | */ |
ff3187f6 |
6280 | s->pktout = ssh2_pkt_init(SSH2_MSG_NEWKEYS); |
590f6a5f |
6281 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
9442dd57 |
6282 | ssh->outgoing_data_size = 0; /* start counting from here */ |
6283 | |
6284 | /* |
6285 | * We've sent client NEWKEYS, so create and initialise |
c64fe7d4 |
6286 | * client-to-server session keys. |
9442dd57 |
6287 | */ |
6288 | if (ssh->cs_cipher_ctx) |
6289 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
6290 | ssh->cscipher = s->cscipher_tobe; |
6291 | ssh->cs_cipher_ctx = ssh->cscipher->make_context(); |
6292 | |
6293 | if (ssh->cs_mac_ctx) |
6294 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
6295 | ssh->csmac = s->csmac_tobe; |
6296 | ssh->cs_mac_ctx = ssh->csmac->make_context(); |
6297 | |
6298 | if (ssh->cs_comp_ctx) |
6299 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
6300 | ssh->cscomp = s->cscomp_tobe; |
6301 | ssh->cs_comp_ctx = ssh->cscomp->compress_init(); |
6302 | |
6303 | /* |
6304 | * Set IVs on client-to-server keys. Here we use the exchange |
6305 | * hash from the _first_ key exchange. |
6306 | */ |
6307 | { |
754c0df9 |
6308 | unsigned char keyspace[SSH2_KEX_MAX_HASH_LEN * SSH2_MKKEY_ITERS]; |
6309 | assert(sizeof(keyspace) >= ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
b672f405 |
6310 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'C',keyspace); |
754c0df9 |
6311 | assert((ssh->cscipher->keylen+7) / 8 <= |
6312 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
9442dd57 |
6313 | ssh->cscipher->setkey(ssh->cs_cipher_ctx, keyspace); |
b672f405 |
6314 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'A',keyspace); |
754c0df9 |
6315 | assert(ssh->cscipher->blksize <= |
6316 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
9442dd57 |
6317 | ssh->cscipher->setiv(ssh->cs_cipher_ctx, keyspace); |
b672f405 |
6318 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'E',keyspace); |
754c0df9 |
6319 | assert(ssh->csmac->len <= |
6320 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
9442dd57 |
6321 | ssh->csmac->setkey(ssh->cs_mac_ctx, keyspace); |
dfb88efd |
6322 | smemclr(keyspace, sizeof(keyspace)); |
9442dd57 |
6323 | } |
6324 | |
6325 | logeventf(ssh, "Initialised %.200s client->server encryption", |
6326 | ssh->cscipher->text_name); |
6327 | logeventf(ssh, "Initialised %.200s client->server MAC algorithm", |
6328 | ssh->csmac->text_name); |
6329 | if (ssh->cscomp->text_name) |
6330 | logeventf(ssh, "Initialised %s compression", |
6331 | ssh->cscomp->text_name); |
590f6a5f |
6332 | |
6333 | /* |
6334 | * Now our end of the key exchange is complete, we can send all |
6335 | * our queued higher-layer packets. |
6336 | */ |
6337 | ssh->queueing = FALSE; |
6338 | ssh2_pkt_queuesend(ssh); |
d39f364a |
6339 | |
6340 | /* |
8406eaf9 |
6341 | * Expect SSH2_MSG_NEWKEYS from server. |
6342 | */ |
0b106f61 |
6343 | crWaitUntilV(pktin); |
ff3187f6 |
6344 | if (pktin->type != SSH2_MSG_NEWKEYS) { |
6b5cf8b4 |
6345 | bombout(("expected new-keys packet from server")); |
0b106f61 |
6346 | crStopV; |
8406eaf9 |
6347 | } |
9442dd57 |
6348 | ssh->incoming_data_size = 0; /* start counting from here */ |
8406eaf9 |
6349 | |
6350 | /* |
9442dd57 |
6351 | * We've seen server NEWKEYS, so create and initialise |
6352 | * server-to-client session keys. |
d39f364a |
6353 | */ |
371e569c |
6354 | if (ssh->sc_cipher_ctx) |
6355 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
51470298 |
6356 | ssh->sccipher = s->sccipher_tobe; |
371e569c |
6357 | ssh->sc_cipher_ctx = ssh->sccipher->make_context(); |
e0e1a00d |
6358 | |
e0e1a00d |
6359 | if (ssh->sc_mac_ctx) |
6360 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
51470298 |
6361 | ssh->scmac = s->scmac_tobe; |
e0e1a00d |
6362 | ssh->sc_mac_ctx = ssh->scmac->make_context(); |
6363 | |
5366aed8 |
6364 | if (ssh->sc_comp_ctx) |
6365 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
51470298 |
6366 | ssh->sccomp = s->sccomp_tobe; |
5366aed8 |
6367 | ssh->sc_comp_ctx = ssh->sccomp->decompress_init(); |
6368 | |
d39f364a |
6369 | /* |
9442dd57 |
6370 | * Set IVs on server-to-client keys. Here we use the exchange |
6371 | * hash from the _first_ key exchange. |
d39f364a |
6372 | */ |
51470298 |
6373 | { |
754c0df9 |
6374 | unsigned char keyspace[SSH2_KEX_MAX_HASH_LEN * SSH2_MKKEY_ITERS]; |
6375 | assert(sizeof(keyspace) >= ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
b672f405 |
6376 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'D',keyspace); |
754c0df9 |
6377 | assert((ssh->sccipher->keylen+7) / 8 <= |
6378 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
371e569c |
6379 | ssh->sccipher->setkey(ssh->sc_cipher_ctx, keyspace); |
b672f405 |
6380 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'B',keyspace); |
754c0df9 |
6381 | assert(ssh->sccipher->blksize <= |
6382 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
371e569c |
6383 | ssh->sccipher->setiv(ssh->sc_cipher_ctx, keyspace); |
b672f405 |
6384 | ssh2_mkkey(ssh,s->K,s->exchange_hash,'F',keyspace); |
754c0df9 |
6385 | assert(ssh->scmac->len <= |
6386 | ssh->kex->hash->hlen * SSH2_MKKEY_ITERS); |
e0e1a00d |
6387 | ssh->scmac->setkey(ssh->sc_mac_ctx, keyspace); |
dfb88efd |
6388 | smemclr(keyspace, sizeof(keyspace)); |
51470298 |
6389 | } |
57356d63 |
6390 | logeventf(ssh, "Initialised %.200s server->client encryption", |
6391 | ssh->sccipher->text_name); |
6c135243 |
6392 | logeventf(ssh, "Initialised %.200s server->client MAC algorithm", |
6393 | ssh->scmac->text_name); |
57356d63 |
6394 | if (ssh->sccomp->text_name) |
6395 | logeventf(ssh, "Initialised %s decompression", |
6396 | ssh->sccomp->text_name); |
9442dd57 |
6397 | |
6398 | /* |
fae1a71b |
6399 | * Free shared secret. |
9442dd57 |
6400 | */ |
679539d7 |
6401 | freebn(s->K); |
d39f364a |
6402 | |
033b4cef |
6403 | /* |
e13bba36 |
6404 | * Key exchange is over. Loop straight back round if we have a |
6405 | * deferred rekey reason. |
6406 | */ |
6407 | if (ssh->deferred_rekey_reason) { |
6408 | logevent(ssh->deferred_rekey_reason); |
6409 | pktin = NULL; |
6410 | ssh->deferred_rekey_reason = NULL; |
6411 | goto begin_key_exchange; |
6412 | } |
6413 | |
6414 | /* |
6415 | * Otherwise, schedule a timer for our next rekey. |
9442dd57 |
6416 | */ |
6417 | ssh->kex_in_progress = FALSE; |
e6c1536e |
6418 | ssh->last_rekey = GETTICKCOUNT(); |
4a693cfc |
6419 | if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0) |
6420 | ssh->next_rekey = schedule_timer(conf_get_int(ssh->conf, CONF_ssh_rekey_time)*60*TICKSPERSEC, |
d57f70af |
6421 | ssh2_timer, ssh); |
e13bba36 |
6422 | |
9442dd57 |
6423 | /* |
7cca0d81 |
6424 | * Now we're encrypting. Begin returning 1 to the protocol main |
6425 | * function so that other things can run on top of the |
6426 | * transport. If we ever see a KEXINIT, we must go back to the |
6427 | * start. |
9442dd57 |
6428 | * |
6429 | * We _also_ go back to the start if we see pktin==NULL and |
ab18ccff |
6430 | * inlen negative, because this is a special signal meaning |
9442dd57 |
6431 | * `initiate client-driven rekey', and `in' contains a message |
6432 | * giving the reason for the rekey. |
ab18ccff |
6433 | * |
6434 | * inlen==-1 means always initiate a rekey; |
6435 | * inlen==-2 means that userauth has completed successfully and |
6436 | * we should consider rekeying (for delayed compression). |
033b4cef |
6437 | */ |
9442dd57 |
6438 | while (!((pktin && pktin->type == SSH2_MSG_KEXINIT) || |
ab18ccff |
6439 | (!pktin && inlen < 0))) { |
f382c87d |
6440 | wait_for_rekey: |
bc074147 |
6441 | if (!ssh->protocol_initial_phase_done) { |
6442 | ssh->protocol_initial_phase_done = TRUE; |
6443 | /* |
6444 | * Allow authconn to initialise itself. |
6445 | */ |
6446 | do_ssh2_authconn(ssh, NULL, 0, NULL); |
6447 | } |
0b106f61 |
6448 | crReturnV; |
e96adf72 |
6449 | } |
9442dd57 |
6450 | if (pktin) { |
6451 | logevent("Server initiated key re-exchange"); |
6452 | } else { |
ab18ccff |
6453 | if (inlen == -2) { |
6454 | /* |
6455 | * authconn has seen a USERAUTH_SUCCEEDED. Time to enable |
6456 | * delayed compression, if it's available. |
6457 | * |
6458 | * draft-miller-secsh-compression-delayed-00 says that you |
6459 | * negotiate delayed compression in the first key exchange, and |
6460 | * both sides start compressing when the server has sent |
6461 | * USERAUTH_SUCCESS. This has a race condition -- the server |
6462 | * can't know when the client has seen it, and thus which incoming |
6463 | * packets it should treat as compressed. |
6464 | * |
6465 | * Instead, we do the initial key exchange without offering the |
6466 | * delayed methods, but note if the server offers them; when we |
6467 | * get here, if a delayed method was available that was higher |
6468 | * on our list than what we got, we initiate a rekey in which we |
6469 | * _do_ list the delayed methods (and hopefully get it as a |
6470 | * result). Subsequent rekeys will do the same. |
6471 | */ |
6472 | assert(!s->userauth_succeeded); /* should only happen once */ |
6473 | s->userauth_succeeded = TRUE; |
6474 | if (!s->pending_compression) |
6475 | /* Can't see any point rekeying. */ |
6476 | goto wait_for_rekey; /* this is utterly horrid */ |
6477 | /* else fall through to rekey... */ |
6478 | s->pending_compression = FALSE; |
6479 | } |
f382c87d |
6480 | /* |
ab18ccff |
6481 | * Now we've decided to rekey. |
6482 | * |
f382c87d |
6483 | * Special case: if the server bug is set that doesn't |
6484 | * allow rekeying, we give a different log message and |
6485 | * continue waiting. (If such a server _initiates_ a rekey, |
6486 | * we process it anyway!) |
6487 | */ |
6488 | if ((ssh->remote_bugs & BUG_SSH2_REKEY)) { |
6489 | logeventf(ssh, "Server bug prevents key re-exchange (%s)", |
6490 | (char *)in); |
6491 | /* Reset the counters, so that at least this message doesn't |
6492 | * hit the event log _too_ often. */ |
6493 | ssh->outgoing_data_size = 0; |
6494 | ssh->incoming_data_size = 0; |
4a693cfc |
6495 | if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0) { |
f382c87d |
6496 | ssh->next_rekey = |
4a693cfc |
6497 | schedule_timer(conf_get_int(ssh->conf, CONF_ssh_rekey_time)*60*TICKSPERSEC, |
f382c87d |
6498 | ssh2_timer, ssh); |
6499 | } |
ab18ccff |
6500 | goto wait_for_rekey; /* this is still utterly horrid */ |
f382c87d |
6501 | } else { |
6502 | logeventf(ssh, "Initiating key re-exchange (%s)", (char *)in); |
f382c87d |
6503 | } |
9442dd57 |
6504 | } |
7cca0d81 |
6505 | goto begin_key_exchange; |
e5574168 |
6506 | |
0b106f61 |
6507 | crFinishV; |
e5574168 |
6508 | } |
6509 | |
7cca0d81 |
6510 | /* |
2e85c969 |
6511 | * Add data to an SSH-2 channel output buffer. |
783415f8 |
6512 | */ |
32874aea |
6513 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, |
6514 | int len) |
6515 | { |
5471d09a |
6516 | bufchain_add(&c->v.v2.outbuffer, buf, len); |
783415f8 |
6517 | } |
6518 | |
6519 | /* |
2e85c969 |
6520 | * Attempt to send data on an SSH-2 channel. |
783415f8 |
6521 | */ |
5471d09a |
6522 | static int ssh2_try_send(struct ssh_channel *c) |
32874aea |
6523 | { |
51470298 |
6524 | Ssh ssh = c->ssh; |
ff3187f6 |
6525 | struct Packet *pktout; |
bc06669b |
6526 | int ret; |
51470298 |
6527 | |
5471d09a |
6528 | while (c->v.v2.remwindow > 0 && bufchain_size(&c->v.v2.outbuffer) > 0) { |
6529 | int len; |
6530 | void *data; |
6531 | bufchain_prefix(&c->v.v2.outbuffer, &data, &len); |
6532 | if ((unsigned)len > c->v.v2.remwindow) |
6533 | len = c->v.v2.remwindow; |
6534 | if ((unsigned)len > c->v.v2.remmaxpkt) |
6535 | len = c->v.v2.remmaxpkt; |
ff3187f6 |
6536 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA); |
6537 | ssh2_pkt_adduint32(pktout, c->remoteid); |
ff3187f6 |
6538 | ssh2_pkt_addstring_start(pktout); |
a2724fba |
6539 | dont_log_data(ssh, pktout, PKTLOG_OMIT); |
ff3187f6 |
6540 | ssh2_pkt_addstring_data(pktout, data, len); |
6541 | end_log_omission(ssh, pktout); |
6542 | ssh2_pkt_send(ssh, pktout); |
5471d09a |
6543 | bufchain_consume(&c->v.v2.outbuffer, len); |
6544 | c->v.v2.remwindow -= len; |
6545 | } |
6546 | |
6547 | /* |
6548 | * After having sent as much data as we can, return the amount |
6549 | * still buffered. |
6550 | */ |
bc06669b |
6551 | ret = bufchain_size(&c->v.v2.outbuffer); |
6552 | |
6553 | /* |
6554 | * And if there's no data pending but we need to send an EOF, send |
6555 | * it. |
6556 | */ |
6557 | if (!ret && c->pending_eof) |
6558 | ssh_channel_try_eof(c); |
6559 | |
6560 | return ret; |
5471d09a |
6561 | } |
6562 | |
78280721 |
6563 | static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c) |
1bfc7e93 |
6564 | { |
6565 | int bufsize; |
bc06669b |
6566 | if (c->closes & CLOSES_SENT_EOF) |
6567 | return; /* don't send on channels we've EOFed */ |
1bfc7e93 |
6568 | bufsize = ssh2_try_send(c); |
6569 | if (bufsize == 0) { |
6570 | switch (c->type) { |
6571 | case CHAN_MAINSESSION: |
6572 | /* stdin need not receive an unthrottle |
6573 | * notification since it will be polled */ |
6574 | break; |
6575 | case CHAN_X11: |
6576 | x11_unthrottle(c->u.x11.s); |
6577 | break; |
6578 | case CHAN_AGENT: |
6579 | /* agent sockets are request/response and need no |
6580 | * buffer management */ |
6581 | break; |
6582 | case CHAN_SOCKDATA: |
6583 | pfd_unthrottle(c->u.pfd.s); |
6584 | break; |
6585 | } |
6586 | } |
6587 | } |
6588 | |
5471d09a |
6589 | /* |
94bdfe40 |
6590 | * Set up most of a new ssh_channel for SSH-2. |
6591 | */ |
6592 | static void ssh2_channel_init(struct ssh_channel *c) |
6593 | { |
6594 | Ssh ssh = c->ssh; |
6595 | c->localid = alloc_channel_id(ssh); |
6596 | c->closes = 0; |
bc06669b |
6597 | c->pending_eof = FALSE; |
94bdfe40 |
6598 | c->throttling_conn = FALSE; |
6599 | c->v.v2.locwindow = c->v.v2.locmaxwin = c->v.v2.remlocwin = |
4a693cfc |
6600 | conf_get_int(ssh->conf, CONF_ssh_simple) ? OUR_V2_BIGWIN : OUR_V2_WINSIZE; |
39d830e4 |
6601 | c->v.v2.chanreq_head = NULL; |
94bdfe40 |
6602 | c->v.v2.throttle_state = UNTHROTTLED; |
6603 | bufchain_init(&c->v.v2.outbuffer); |
6604 | } |
6605 | |
6606 | /* |
4a62c66d |
6607 | * Construct the common parts of a CHANNEL_OPEN. |
6608 | */ |
6609 | static struct Packet *ssh2_chanopen_init(struct ssh_channel *c, char *type) |
6610 | { |
6611 | struct Packet *pktout; |
6612 | |
6613 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
6614 | ssh2_pkt_addstring(pktout, type); |
6615 | ssh2_pkt_adduint32(pktout, c->localid); |
6616 | ssh2_pkt_adduint32(pktout, c->v.v2.locwindow);/* our window size */ |
6617 | ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
6618 | return pktout; |
6619 | } |
6620 | |
6621 | /* |
39d830e4 |
6622 | * CHANNEL_FAILURE doesn't come with any indication of what message |
6623 | * caused it, so we have to keep track of the outstanding |
6624 | * CHANNEL_REQUESTs ourselves. |
6625 | */ |
6626 | static void ssh2_queue_chanreq_handler(struct ssh_channel *c, |
6627 | cchandler_fn_t handler, void *ctx) |
6628 | { |
6629 | struct outstanding_channel_request *ocr = |
6630 | snew(struct outstanding_channel_request); |
6631 | |
3620f307 |
6632 | assert(!(c->closes & (CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE))); |
39d830e4 |
6633 | ocr->handler = handler; |
6634 | ocr->ctx = ctx; |
6635 | ocr->next = NULL; |
6636 | if (!c->v.v2.chanreq_head) |
6637 | c->v.v2.chanreq_head = ocr; |
6638 | else |
6639 | c->v.v2.chanreq_tail->next = ocr; |
6640 | c->v.v2.chanreq_tail = ocr; |
6641 | } |
6642 | |
6643 | /* |
134f5ece |
6644 | * Construct the common parts of a CHANNEL_REQUEST. If handler is not |
6645 | * NULL then a reply will be requested and the handler will be called |
6646 | * when it arrives. The returned packet is ready to have any |
6647 | * request-specific data added and be sent. Note that if a handler is |
6648 | * provided, it's essential that the request actually be sent. |
3620f307 |
6649 | * |
6650 | * The handler will usually be passed the response packet in pktin. |
6651 | * If pktin is NULL, this means that no reply will ever be forthcoming |
6652 | * (e.g. because the entire connection is being destroyed) and the |
6653 | * handler should free any storage it's holding. |
134f5ece |
6654 | */ |
6655 | static struct Packet *ssh2_chanreq_init(struct ssh_channel *c, char *type, |
6656 | cchandler_fn_t handler, void *ctx) |
6657 | { |
6658 | struct Packet *pktout; |
6659 | |
3620f307 |
6660 | assert(!(c->closes & (CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE))); |
134f5ece |
6661 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
6662 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6663 | ssh2_pkt_addstring(pktout, type); |
6664 | ssh2_pkt_addbool(pktout, handler != NULL); |
6665 | if (handler != NULL) |
6666 | ssh2_queue_chanreq_handler(c, handler, ctx); |
6667 | return pktout; |
6668 | } |
6669 | |
6670 | /* |
2e85c969 |
6671 | * Potentially enlarge the window on an SSH-2 channel. |
5471d09a |
6672 | */ |
39d830e4 |
6673 | static void ssh2_handle_winadj_response(struct ssh_channel *, struct Packet *, |
6674 | void *); |
9fca3f8c |
6675 | static void ssh2_set_window(struct ssh_channel *c, int newwin) |
5471d09a |
6676 | { |
51470298 |
6677 | Ssh ssh = c->ssh; |
6678 | |
6b69f42e |
6679 | /* |
bc06669b |
6680 | * Never send WINDOW_ADJUST for a channel that the remote side has |
6681 | * already sent EOF on; there's no point, since it won't be |
beff003a |
6682 | * sending any more data anyway. Ditto if _we've_ already sent |
6683 | * CLOSE. |
6b69f42e |
6684 | */ |
beff003a |
6685 | if (c->closes & (CLOSES_RCVD_EOF | CLOSES_SENT_CLOSE)) |
6b69f42e |
6686 | return; |
6687 | |
d252310a |
6688 | /* |
c9739dba |
6689 | * If the remote end has a habit of ignoring maxpkt, limit the |
6690 | * window so that it has no choice (assuming it doesn't ignore the |
6691 | * window as well). |
6692 | */ |
6693 | if ((ssh->remote_bugs & BUG_SSH2_MAXPKT) && newwin > OUR_V2_MAXPKT) |
6694 | newwin = OUR_V2_MAXPKT; |
c9739dba |
6695 | |
6696 | /* |
d252310a |
6697 | * Only send a WINDOW_ADJUST if there's significantly more window |
6698 | * available than the other end thinks there is. This saves us |
6699 | * sending a WINDOW_ADJUST for every character in a shell session. |
6700 | * |
6701 | * "Significant" is arbitrarily defined as half the window size. |
6702 | */ |
3361b80a |
6703 | if (newwin / 2 >= c->v.v2.locwindow) { |
ff3187f6 |
6704 | struct Packet *pktout; |
39d830e4 |
6705 | unsigned *up; |
ff3187f6 |
6706 | |
0e443c99 |
6707 | /* |
6708 | * In order to keep track of how much window the client |
6709 | * actually has available, we'd like it to acknowledge each |
6710 | * WINDOW_ADJUST. We can't do that directly, so we accompany |
6711 | * it with a CHANNEL_REQUEST that has to be acknowledged. |
6712 | * |
6713 | * This is only necessary if we're opening the window wide. |
6714 | * If we're not, then throughput is being constrained by |
6715 | * something other than the maximum window size anyway. |
0e443c99 |
6716 | */ |
6717 | if (newwin == c->v.v2.locmaxwin && |
2e0aae4f |
6718 | !(ssh->remote_bugs & BUG_CHOKES_ON_WINADJ)) { |
39d830e4 |
6719 | up = snew(unsigned); |
6720 | *up = newwin - c->v.v2.locwindow; |
134f5ece |
6721 | pktout = ssh2_chanreq_init(c, "winadj@putty.projects.tartarus.org", |
6722 | ssh2_handle_winadj_response, up); |
6723 | ssh2_pkt_send(ssh, pktout); |
6724 | |
0e443c99 |
6725 | if (c->v.v2.throttle_state != UNTHROTTLED) |
6726 | c->v.v2.throttle_state = UNTHROTTLING; |
6727 | } else { |
6728 | /* Pretend the WINDOW_ADJUST was acked immediately. */ |
6729 | c->v.v2.remlocwin = newwin; |
6730 | c->v.v2.throttle_state = THROTTLED; |
6731 | } |
ff3187f6 |
6732 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
6733 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6734 | ssh2_pkt_adduint32(pktout, newwin - c->v.v2.locwindow); |
6735 | ssh2_pkt_send(ssh, pktout); |
5471d09a |
6736 | c->v.v2.locwindow = newwin; |
783415f8 |
6737 | } |
6738 | } |
6739 | |
e223b7c7 |
6740 | /* |
6741 | * Find the channel associated with a message. If there's no channel, |
6742 | * or it's not properly open, make a noise about it and return NULL. |
6743 | */ |
6744 | static struct ssh_channel *ssh2_channel_msg(Ssh ssh, struct Packet *pktin) |
6745 | { |
6746 | unsigned localid = ssh_pkt_getuint32(pktin); |
6747 | struct ssh_channel *c; |
6748 | |
6749 | c = find234(ssh->channels, &localid, ssh_channelfind); |
6750 | if (!c || |
6751 | (c->halfopen && pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION && |
6752 | pktin->type != SSH2_MSG_CHANNEL_OPEN_FAILURE)) { |
6753 | char *buf = dupprintf("Received %s for %s channel %u", |
6754 | ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, |
6755 | pktin->type), |
6756 | c ? "half-open" : "nonexistent", localid); |
6757 | ssh_disconnect(ssh, NULL, buf, SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE); |
6758 | sfree(buf); |
6759 | return NULL; |
6760 | } |
6761 | return c; |
6762 | } |
6763 | |
39d830e4 |
6764 | static void ssh2_handle_winadj_response(struct ssh_channel *c, |
6765 | struct Packet *pktin, void *ctx) |
e030765f |
6766 | { |
39d830e4 |
6767 | unsigned *sizep = ctx; |
6768 | |
6769 | /* |
6770 | * Winadj responses should always be failures. However, at least |
6771 | * one server ("boks_sshd") is known to return SUCCESS for channel |
6772 | * requests it's never heard of, such as "winadj@putty". Raised |
6773 | * with foxt.com as bug 090916-090424, but for the sake of a quiet |
6774 | * life, we don't worry about what kind of response we got. |
6775 | */ |
6776 | |
6777 | c->v.v2.remlocwin += *sizep; |
6778 | sfree(sizep); |
e030765f |
6779 | /* |
6780 | * winadj messages are only sent when the window is fully open, so |
6781 | * if we get an ack of one, we know any pending unthrottle is |
6782 | * complete. |
6783 | */ |
6784 | if (c->v.v2.throttle_state == UNTHROTTLING) |
6785 | c->v.v2.throttle_state = UNTHROTTLED; |
e030765f |
6786 | } |
6787 | |
39d830e4 |
6788 | static void ssh2_msg_channel_response(Ssh ssh, struct Packet *pktin) |
837d6ba6 |
6789 | { |
39d830e4 |
6790 | struct ssh_channel *c = ssh2_channel_msg(ssh, pktin); |
6791 | struct outstanding_channel_request *ocr; |
837d6ba6 |
6792 | |
39d830e4 |
6793 | if (!c) return; |
6794 | ocr = c->v.v2.chanreq_head; |
6795 | if (!ocr) { |
6796 | ssh2_msg_unexpected(ssh, pktin); |
837d6ba6 |
6797 | return; |
39d830e4 |
6798 | } |
6799 | ocr->handler(c, pktin, ocr->ctx); |
6800 | c->v.v2.chanreq_head = ocr->next; |
6801 | sfree(ocr); |
0e443c99 |
6802 | /* |
39d830e4 |
6803 | * We may now initiate channel-closing procedures, if that |
6804 | * CHANNEL_REQUEST was the last thing outstanding before we send |
6805 | * CHANNEL_CLOSE. |
0e443c99 |
6806 | */ |
39d830e4 |
6807 | ssh2_channel_check_close(c); |
0e443c99 |
6808 | } |
6809 | |
51df0ab5 |
6810 | static void ssh2_msg_channel_window_adjust(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
6811 | { |
b09eaa88 |
6812 | struct ssh_channel *c; |
e223b7c7 |
6813 | c = ssh2_channel_msg(ssh, pktin); |
6814 | if (!c) |
6815 | return; |
bc06669b |
6816 | if (!(c->closes & CLOSES_SENT_EOF)) { |
b09eaa88 |
6817 | c->v.v2.remwindow += ssh_pkt_getuint32(pktin); |
78280721 |
6818 | ssh2_try_send_and_unthrottle(ssh, c); |
1bfc7e93 |
6819 | } |
b09eaa88 |
6820 | } |
6821 | |
51df0ab5 |
6822 | static void ssh2_msg_channel_data(Ssh ssh, struct Packet *pktin) |
6823 | { |
6824 | char *data; |
6d44acc9 |
6825 | int length; |
51df0ab5 |
6826 | struct ssh_channel *c; |
e223b7c7 |
6827 | c = ssh2_channel_msg(ssh, pktin); |
51df0ab5 |
6828 | if (!c) |
e223b7c7 |
6829 | return; |
51df0ab5 |
6830 | if (pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA && |
6831 | ssh_pkt_getuint32(pktin) != SSH2_EXTENDED_DATA_STDERR) |
6832 | return; /* extended but not stderr */ |
6833 | ssh_pkt_getstring(pktin, &data, &length); |
6834 | if (data) { |
6835 | int bufsize = 0; |
6836 | c->v.v2.locwindow -= length; |
0e443c99 |
6837 | c->v.v2.remlocwin -= length; |
51df0ab5 |
6838 | switch (c->type) { |
6839 | case CHAN_MAINSESSION: |
6840 | bufsize = |
6841 | from_backend(ssh->frontend, pktin->type == |
6842 | SSH2_MSG_CHANNEL_EXTENDED_DATA, |
6843 | data, length); |
6844 | break; |
6845 | case CHAN_X11: |
6846 | bufsize = x11_send(c->u.x11.s, data, length); |
6847 | break; |
6848 | case CHAN_SOCKDATA: |
6849 | bufsize = pfd_send(c->u.pfd.s, data, length); |
6850 | break; |
6851 | case CHAN_AGENT: |
6852 | while (length > 0) { |
6853 | if (c->u.a.lensofar < 4) { |
62ddb51e |
6854 | unsigned int l = min(4 - c->u.a.lensofar, |
6855 | (unsigned)length); |
51df0ab5 |
6856 | memcpy(c->u.a.msglen + c->u.a.lensofar, |
6857 | data, l); |
6858 | data += l; |
6859 | length -= l; |
6860 | c->u.a.lensofar += l; |
6861 | } |
6862 | if (c->u.a.lensofar == 4) { |
6863 | c->u.a.totallen = |
6864 | 4 + GET_32BIT(c->u.a.msglen); |
6865 | c->u.a.message = snewn(c->u.a.totallen, |
6866 | unsigned char); |
6867 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
6868 | } |
6869 | if (c->u.a.lensofar >= 4 && length > 0) { |
aa63ab7e |
6870 | unsigned int l = |
51df0ab5 |
6871 | min(c->u.a.totallen - c->u.a.lensofar, |
62ddb51e |
6872 | (unsigned)length); |
51df0ab5 |
6873 | memcpy(c->u.a.message + c->u.a.lensofar, |
6874 | data, l); |
6875 | data += l; |
6876 | length -= l; |
6877 | c->u.a.lensofar += l; |
6878 | } |
6879 | if (c->u.a.lensofar == c->u.a.totallen) { |
6880 | void *reply; |
6881 | int replylen; |
be3f980d |
6882 | c->u.a.outstanding_requests++; |
51df0ab5 |
6883 | if (agent_query(c->u.a.message, |
6884 | c->u.a.totallen, |
6885 | &reply, &replylen, |
6886 | ssh_agentf_callback, c)) |
6887 | ssh_agentf_callback(c, reply, replylen); |
6888 | sfree(c->u.a.message); |
bc06669b |
6889 | c->u.a.message = NULL; |
51df0ab5 |
6890 | c->u.a.lensofar = 0; |
6891 | } |
6892 | } |
6893 | bufsize = 0; |
6894 | break; |
6895 | } |
6896 | /* |
0e443c99 |
6897 | * If it looks like the remote end hit the end of its window, |
6898 | * and we didn't want it to do that, think about using a |
6899 | * larger window. |
6900 | */ |
6901 | if (c->v.v2.remlocwin <= 0 && c->v.v2.throttle_state == UNTHROTTLED && |
6902 | c->v.v2.locmaxwin < 0x40000000) |
6903 | c->v.v2.locmaxwin += OUR_V2_WINSIZE; |
6904 | /* |
51df0ab5 |
6905 | * If we are not buffering too much data, |
6906 | * enlarge the window again at the remote side. |
467e064d |
6907 | * If we are buffering too much, we may still |
6908 | * need to adjust the window if the server's |
6909 | * sent excess data. |
51df0ab5 |
6910 | */ |
9b53e9b8 |
6911 | ssh2_set_window(c, bufsize < c->v.v2.locmaxwin ? |
6912 | c->v.v2.locmaxwin - bufsize : 0); |
ff68564b |
6913 | /* |
6914 | * If we're either buffering way too much data, or if we're |
6915 | * buffering anything at all and we're in "simple" mode, |
6916 | * throttle the whole channel. |
6917 | */ |
6918 | if ((bufsize > c->v.v2.locmaxwin || |
4a693cfc |
6919 | (conf_get_int(ssh->conf, CONF_ssh_simple) && bufsize > 0)) && |
ff68564b |
6920 | !c->throttling_conn) { |
6921 | c->throttling_conn = 1; |
6922 | ssh_throttle_conn(ssh, +1); |
6923 | } |
51df0ab5 |
6924 | } |
6925 | } |
6926 | |
bc06669b |
6927 | static void ssh_channel_destroy(struct ssh_channel *c) |
51df0ab5 |
6928 | { |
bc06669b |
6929 | Ssh ssh = c->ssh; |
51df0ab5 |
6930 | |
bc06669b |
6931 | switch (c->type) { |
6932 | case CHAN_MAINSESSION: |
6933 | ssh->mainchan = NULL; |
6934 | update_specials_menu(ssh->frontend); |
6935 | break; |
6936 | case CHAN_X11: |
6937 | if (c->u.x11.s != NULL) |
6938 | x11_close(c->u.x11.s); |
6939 | logevent("Forwarded X11 connection terminated"); |
6940 | break; |
6941 | case CHAN_AGENT: |
6942 | sfree(c->u.a.message); |
6943 | break; |
6944 | case CHAN_SOCKDATA: |
6945 | if (c->u.pfd.s != NULL) |
6946 | pfd_close(c->u.pfd.s); |
6947 | logevent("Forwarded port closed"); |
6948 | break; |
6949 | } |
6950 | |
6951 | del234(ssh->channels, c); |
3620f307 |
6952 | if (ssh->version == 2) { |
bc06669b |
6953 | bufchain_clear(&c->v.v2.outbuffer); |
3620f307 |
6954 | assert(c->v.v2.chanreq_head == NULL); |
6955 | } |
bc06669b |
6956 | sfree(c); |
6957 | |
6958 | /* |
6959 | * See if that was the last channel left open. |
6960 | * (This is only our termination condition if we're |
6961 | * not running in -N mode.) |
6962 | */ |
6963 | if (ssh->version == 2 && |
6964 | !conf_get_int(ssh->conf, CONF_ssh_no_shell) && |
6965 | count234(ssh->channels) == 0) { |
6966 | /* |
6967 | * We used to send SSH_MSG_DISCONNECT here, |
6968 | * because I'd believed that _every_ conforming |
6969 | * SSH-2 connection had to end with a disconnect |
6970 | * being sent by at least one side; apparently |
6971 | * I was wrong and it's perfectly OK to |
6972 | * unceremoniously slam the connection shut |
6973 | * when you're done, and indeed OpenSSH feels |
6974 | * this is more polite than sending a |
6975 | * DISCONNECT. So now we don't. |
6976 | */ |
6977 | ssh_disconnect(ssh, "All channels closed", NULL, 0, TRUE); |
6978 | } |
6979 | } |
6980 | |
6981 | static void ssh2_channel_check_close(struct ssh_channel *c) |
6982 | { |
6983 | Ssh ssh = c->ssh; |
6984 | struct Packet *pktout; |
6985 | |
04120005 |
6986 | if ((!((CLOSES_SENT_EOF | CLOSES_RCVD_EOF) & ~c->closes) || |
6987 | c->type == CHAN_ZOMBIE) && |
6988 | !c->v.v2.chanreq_head && |
6989 | !(c->closes & CLOSES_SENT_CLOSE)) { |
bc06669b |
6990 | /* |
04120005 |
6991 | * We have both sent and received EOF (or the channel is a |
6992 | * zombie), and we have no outstanding channel requests, which |
6993 | * means the channel is in final wind-up. But we haven't sent |
6994 | * CLOSE, so let's do so now. |
bc06669b |
6995 | */ |
6996 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
6997 | ssh2_pkt_adduint32(pktout, c->remoteid); |
6998 | ssh2_pkt_send(ssh, pktout); |
04120005 |
6999 | c->closes |= CLOSES_SENT_EOF | CLOSES_SENT_CLOSE; |
bc06669b |
7000 | } |
7001 | |
7002 | if (!((CLOSES_SENT_CLOSE | CLOSES_RCVD_CLOSE) & ~c->closes)) { |
3620f307 |
7003 | assert(c->v.v2.chanreq_head == NULL); |
bc06669b |
7004 | /* |
7005 | * We have both sent and received CLOSE, which means we're |
7006 | * completely done with the channel. |
7007 | */ |
7008 | ssh_channel_destroy(c); |
7009 | } |
7010 | } |
7011 | |
7012 | static void ssh2_channel_got_eof(struct ssh_channel *c) |
7013 | { |
7014 | if (c->closes & CLOSES_RCVD_EOF) |
7015 | return; /* already seen EOF */ |
7016 | c->closes |= CLOSES_RCVD_EOF; |
51df0ab5 |
7017 | |
7018 | if (c->type == CHAN_X11) { |
bc06669b |
7019 | x11_send_eof(c->u.x11.s); |
51df0ab5 |
7020 | } else if (c->type == CHAN_AGENT) { |
be3f980d |
7021 | if (c->u.a.outstanding_requests == 0) { |
7022 | /* Manufacture an outgoing EOF in response to the incoming one. */ |
7023 | sshfwd_write_eof(c); |
7024 | } |
51df0ab5 |
7025 | } else if (c->type == CHAN_SOCKDATA) { |
bc06669b |
7026 | pfd_send_eof(c->u.pfd.s); |
7027 | } else if (c->type == CHAN_MAINSESSION) { |
7028 | Ssh ssh = c->ssh; |
7029 | |
4a202fc6 |
7030 | if (!ssh->sent_console_eof && |
7031 | (from_backend_eof(ssh->frontend) || ssh->got_pty)) { |
bc06669b |
7032 | /* |
4a202fc6 |
7033 | * Either from_backend_eof told us that the front end |
7034 | * wants us to close the outgoing side of the connection |
7035 | * as soon as we see EOF from the far end, or else we've |
7036 | * unilaterally decided to do that because we've allocated |
7037 | * a remote pty and hence EOF isn't a particularly |
7038 | * meaningful concept. |
bc06669b |
7039 | */ |
7040 | sshfwd_write_eof(c); |
7041 | } |
7042 | ssh->sent_console_eof = TRUE; |
51df0ab5 |
7043 | } |
bc06669b |
7044 | |
7045 | ssh2_channel_check_close(c); |
7046 | } |
7047 | |
7048 | static void ssh2_msg_channel_eof(Ssh ssh, struct Packet *pktin) |
7049 | { |
7050 | struct ssh_channel *c; |
7051 | |
7052 | c = ssh2_channel_msg(ssh, pktin); |
7053 | if (!c) |
7054 | return; |
7055 | ssh2_channel_got_eof(c); |
51df0ab5 |
7056 | } |
7057 | |
7058 | static void ssh2_msg_channel_close(Ssh ssh, struct Packet *pktin) |
7059 | { |
51df0ab5 |
7060 | struct ssh_channel *c; |
51df0ab5 |
7061 | |
e223b7c7 |
7062 | c = ssh2_channel_msg(ssh, pktin); |
7063 | if (!c) |
51df0ab5 |
7064 | return; |
51df0ab5 |
7065 | |
7066 | /* |
bc06669b |
7067 | * When we receive CLOSE on a channel, we assume it comes with an |
7068 | * implied EOF if we haven't seen EOF yet. |
51df0ab5 |
7069 | */ |
bc06669b |
7070 | ssh2_channel_got_eof(c); |
7071 | |
7072 | /* |
3effbcf2 |
7073 | * And we also send an outgoing EOF, if we haven't already, on the |
7074 | * assumption that CLOSE is a pretty forceful announcement that |
7075 | * the remote side is doing away with the entire channel. (If it |
7076 | * had wanted to send us EOF and continue receiving data from us, |
7077 | * it would have just sent CHANNEL_EOF.) |
3effbcf2 |
7078 | */ |
a699eb41 |
7079 | if (!(c->closes & CLOSES_SENT_EOF)) { |
7080 | /* |
7081 | * Make sure we don't read any more from whatever our local |
7082 | * data source is for this channel. |
7083 | */ |
7084 | switch (c->type) { |
7085 | case CHAN_MAINSESSION: |
7086 | ssh->send_ok = 0; /* stop trying to read from stdin */ |
7087 | break; |
7088 | case CHAN_X11: |
7089 | x11_override_throttle(c->u.x11.s, 1); |
7090 | break; |
7091 | case CHAN_SOCKDATA: |
7092 | pfd_override_throttle(c->u.pfd.s, 1); |
7093 | break; |
7094 | } |
7095 | |
7096 | /* |
7097 | * Send outgoing EOF. |
7098 | */ |
7539afa2 |
7099 | sshfwd_write_eof(c); |
3effbcf2 |
7100 | } |
7101 | |
7102 | /* |
bc06669b |
7103 | * Now process the actual close. |
7104 | */ |
7105 | if (!(c->closes & CLOSES_RCVD_CLOSE)) { |
7106 | c->closes |= CLOSES_RCVD_CLOSE; |
7107 | ssh2_channel_check_close(c); |
51df0ab5 |
7108 | } |
7109 | } |
7110 | |
7111 | static void ssh2_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin) |
7112 | { |
51df0ab5 |
7113 | struct ssh_channel *c; |
51df0ab5 |
7114 | |
e223b7c7 |
7115 | c = ssh2_channel_msg(ssh, pktin); |
51df0ab5 |
7116 | if (!c) |
e223b7c7 |
7117 | return; |
51df0ab5 |
7118 | if (c->type != CHAN_SOCKDATA_DORMANT) |
7119 | return; /* dunno why they're confirming this */ |
7120 | c->remoteid = ssh_pkt_getuint32(pktin); |
64d6ff88 |
7121 | c->halfopen = FALSE; |
51df0ab5 |
7122 | c->type = CHAN_SOCKDATA; |
7123 | c->v.v2.remwindow = ssh_pkt_getuint32(pktin); |
7124 | c->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin); |
7125 | if (c->u.pfd.s) |
7126 | pfd_confirm(c->u.pfd.s); |
bc06669b |
7127 | if (c->pending_eof) |
7128 | ssh_channel_try_eof(c); |
51df0ab5 |
7129 | } |
7130 | |
7131 | static void ssh2_msg_channel_open_failure(Ssh ssh, struct Packet *pktin) |
7132 | { |
7133 | static const char *const reasons[] = { |
7134 | "<unknown reason code>", |
7135 | "Administratively prohibited", |
7136 | "Connect failed", |
7137 | "Unknown channel type", |
7138 | "Resource shortage", |
7139 | }; |
51df0ab5 |
7140 | unsigned reason_code; |
7141 | char *reason_string; |
7142 | int reason_length; |
51df0ab5 |
7143 | struct ssh_channel *c; |
e223b7c7 |
7144 | c = ssh2_channel_msg(ssh, pktin); |
51df0ab5 |
7145 | if (!c) |
e223b7c7 |
7146 | return; |
51df0ab5 |
7147 | if (c->type != CHAN_SOCKDATA_DORMANT) |
7148 | return; /* dunno why they're failing this */ |
7149 | |
7150 | reason_code = ssh_pkt_getuint32(pktin); |
7151 | if (reason_code >= lenof(reasons)) |
7152 | reason_code = 0; /* ensure reasons[reason_code] in range */ |
7153 | ssh_pkt_getstring(pktin, &reason_string, &reason_length); |
fb983202 |
7154 | logeventf(ssh, "Forwarded connection refused by server: %s [%.*s]", |
7155 | reasons[reason_code], reason_length, reason_string); |
51df0ab5 |
7156 | |
7157 | pfd_close(c->u.pfd.s); |
7158 | |
7159 | del234(ssh->channels, c); |
7160 | sfree(c); |
7161 | } |
7162 | |
7163 | static void ssh2_msg_channel_request(Ssh ssh, struct Packet *pktin) |
7164 | { |
51df0ab5 |
7165 | char *type; |
7166 | int typelen, want_reply; |
7167 | int reply = SSH2_MSG_CHANNEL_FAILURE; /* default */ |
7168 | struct ssh_channel *c; |
7169 | struct Packet *pktout; |
7170 | |
e223b7c7 |
7171 | c = ssh2_channel_msg(ssh, pktin); |
7172 | if (!c) |
7173 | return; |
51df0ab5 |
7174 | ssh_pkt_getstring(pktin, &type, &typelen); |
7175 | want_reply = ssh2_pkt_getbool(pktin); |
7176 | |
7177 | /* |
51df0ab5 |
7178 | * Having got the channel number, we now look at |
7179 | * the request type string to see if it's something |
7180 | * we recognise. |
7181 | */ |
7182 | if (c == ssh->mainchan) { |
7183 | /* |
7184 | * We recognise "exit-status" and "exit-signal" on |
7185 | * the primary channel. |
7186 | */ |
7187 | if (typelen == 11 && |
7188 | !memcmp(type, "exit-status", 11)) { |
7189 | |
7190 | ssh->exitcode = ssh_pkt_getuint32(pktin); |
7191 | logeventf(ssh, "Server sent command exit status %d", |
7192 | ssh->exitcode); |
7193 | reply = SSH2_MSG_CHANNEL_SUCCESS; |
7194 | |
7195 | } else if (typelen == 11 && |
7196 | !memcmp(type, "exit-signal", 11)) { |
7197 | |
7198 | int is_plausible = TRUE, is_int = FALSE; |
7199 | char *fmt_sig = "", *fmt_msg = ""; |
7200 | char *msg; |
7201 | int msglen = 0, core = FALSE; |
7202 | /* ICK: older versions of OpenSSH (e.g. 3.4p1) |
7203 | * provide an `int' for the signal, despite its |
bccbeb71 |
7204 | * having been a `string' in the drafts of RFC 4254 since at |
51df0ab5 |
7205 | * least 2001. (Fixed in session.c 1.147.) Try to |
7206 | * infer which we can safely parse it as. */ |
7207 | { |
7208 | unsigned char *p = pktin->body + |
7209 | pktin->savedpos; |
7210 | long len = pktin->length - pktin->savedpos; |
7211 | unsigned long num = GET_32BIT(p); /* what is it? */ |
7212 | /* If it's 0, it hardly matters; assume string */ |
7213 | if (num == 0) { |
7214 | is_int = FALSE; |
7215 | } else { |
7216 | int maybe_int = FALSE, maybe_str = FALSE; |
b1650067 |
7217 | #define CHECK_HYPOTHESIS(offset, result) \ |
7218 | do \ |
7219 | { \ |
7220 | int q = toint(offset); \ |
7221 | if (q >= 0 && q+4 <= len) { \ |
7222 | q = toint(q + 4 + GET_32BIT(p+q)); \ |
7223 | if (q >= 0 && q+4 <= len && \ |
7224 | ((q = toint(q + 4 + GET_32BIT(p+q))) != 0) && \ |
7225 | q == len) \ |
7226 | result = TRUE; \ |
7227 | } \ |
7228 | } while(0) |
51df0ab5 |
7229 | CHECK_HYPOTHESIS(4+1, maybe_int); |
7230 | CHECK_HYPOTHESIS(4+num+1, maybe_str); |
7231 | #undef CHECK_HYPOTHESIS |
7232 | if (maybe_int && !maybe_str) |
7233 | is_int = TRUE; |
7234 | else if (!maybe_int && maybe_str) |
7235 | is_int = FALSE; |
7236 | else |
7237 | /* Crikey. Either or neither. Panic. */ |
7238 | is_plausible = FALSE; |
7239 | } |
7240 | } |
d051d1b4 |
7241 | ssh->exitcode = 128; /* means `unknown signal' */ |
51df0ab5 |
7242 | if (is_plausible) { |
7243 | if (is_int) { |
7244 | /* Old non-standard OpenSSH. */ |
7245 | int signum = ssh_pkt_getuint32(pktin); |
7246 | fmt_sig = dupprintf(" %d", signum); |
d051d1b4 |
7247 | ssh->exitcode = 128 + signum; |
51df0ab5 |
7248 | } else { |
bccbeb71 |
7249 | /* As per RFC 4254. */ |
51df0ab5 |
7250 | char *sig; |
7251 | int siglen; |
7252 | ssh_pkt_getstring(pktin, &sig, &siglen); |
7253 | /* Signal name isn't supposed to be blank, but |
7254 | * let's cope gracefully if it is. */ |
7255 | if (siglen) { |
7256 | fmt_sig = dupprintf(" \"%.*s\"", |
7257 | siglen, sig); |
7258 | } |
d051d1b4 |
7259 | |
7260 | /* |
7261 | * Really hideous method of translating the |
7262 | * signal description back into a locally |
7263 | * meaningful number. |
7264 | */ |
7265 | |
7266 | if (0) |
7267 | ; |
b707973a |
7268 | #define TRANSLATE_SIGNAL(s) \ |
7269 | else if (siglen == lenof(#s)-1 && !memcmp(sig, #s, siglen)) \ |
7270 | ssh->exitcode = 128 + SIG ## s |
d051d1b4 |
7271 | #ifdef SIGABRT |
b707973a |
7272 | TRANSLATE_SIGNAL(ABRT); |
d051d1b4 |
7273 | #endif |
7274 | #ifdef SIGALRM |
b707973a |
7275 | TRANSLATE_SIGNAL(ALRM); |
d051d1b4 |
7276 | #endif |
7277 | #ifdef SIGFPE |
b707973a |
7278 | TRANSLATE_SIGNAL(FPE); |
d051d1b4 |
7279 | #endif |
7280 | #ifdef SIGHUP |
b707973a |
7281 | TRANSLATE_SIGNAL(HUP); |
d051d1b4 |
7282 | #endif |
7283 | #ifdef SIGILL |
b707973a |
7284 | TRANSLATE_SIGNAL(ILL); |
d051d1b4 |
7285 | #endif |
7286 | #ifdef SIGINT |
b707973a |
7287 | TRANSLATE_SIGNAL(INT); |
d051d1b4 |
7288 | #endif |
7289 | #ifdef SIGKILL |
b707973a |
7290 | TRANSLATE_SIGNAL(KILL); |
d051d1b4 |
7291 | #endif |
7292 | #ifdef SIGPIPE |
b707973a |
7293 | TRANSLATE_SIGNAL(PIPE); |
d051d1b4 |
7294 | #endif |
7295 | #ifdef SIGQUIT |
b707973a |
7296 | TRANSLATE_SIGNAL(QUIT); |
d051d1b4 |
7297 | #endif |
7298 | #ifdef SIGSEGV |
b707973a |
7299 | TRANSLATE_SIGNAL(SEGV); |
d051d1b4 |
7300 | #endif |
7301 | #ifdef SIGTERM |
b707973a |
7302 | TRANSLATE_SIGNAL(TERM); |
d051d1b4 |
7303 | #endif |
7304 | #ifdef SIGUSR1 |
b707973a |
7305 | TRANSLATE_SIGNAL(USR1); |
d051d1b4 |
7306 | #endif |
7307 | #ifdef SIGUSR2 |
b707973a |
7308 | TRANSLATE_SIGNAL(USR2); |
d051d1b4 |
7309 | #endif |
b707973a |
7310 | #undef TRANSLATE_SIGNAL |
d051d1b4 |
7311 | else |
7312 | ssh->exitcode = 128; |
51df0ab5 |
7313 | } |
7314 | core = ssh2_pkt_getbool(pktin); |
7315 | ssh_pkt_getstring(pktin, &msg, &msglen); |
7316 | if (msglen) { |
7317 | fmt_msg = dupprintf(" (\"%.*s\")", msglen, msg); |
7318 | } |
7319 | /* ignore lang tag */ |
7320 | } /* else don't attempt to parse */ |
7321 | logeventf(ssh, "Server exited on signal%s%s%s", |
7322 | fmt_sig, core ? " (core dumped)" : "", |
7323 | fmt_msg); |
7324 | if (*fmt_sig) sfree(fmt_sig); |
7325 | if (*fmt_msg) sfree(fmt_msg); |
7326 | reply = SSH2_MSG_CHANNEL_SUCCESS; |
7327 | |
7328 | } |
7329 | } else { |
7330 | /* |
7331 | * This is a channel request we don't know |
7332 | * about, so we now either ignore the request |
7333 | * or respond with CHANNEL_FAILURE, depending |
7334 | * on want_reply. |
7335 | */ |
7336 | reply = SSH2_MSG_CHANNEL_FAILURE; |
7337 | } |
7338 | if (want_reply) { |
7339 | pktout = ssh2_pkt_init(reply); |
7340 | ssh2_pkt_adduint32(pktout, c->remoteid); |
7341 | ssh2_pkt_send(ssh, pktout); |
7342 | } |
7343 | } |
7344 | |
7345 | static void ssh2_msg_global_request(Ssh ssh, struct Packet *pktin) |
7346 | { |
7347 | char *type; |
7348 | int typelen, want_reply; |
7349 | struct Packet *pktout; |
7350 | |
7351 | ssh_pkt_getstring(pktin, &type, &typelen); |
7352 | want_reply = ssh2_pkt_getbool(pktin); |
7353 | |
7354 | /* |
7355 | * We currently don't support any global requests |
7356 | * at all, so we either ignore the request or |
7357 | * respond with REQUEST_FAILURE, depending on |
7358 | * want_reply. |
7359 | */ |
7360 | if (want_reply) { |
7361 | pktout = ssh2_pkt_init(SSH2_MSG_REQUEST_FAILURE); |
7362 | ssh2_pkt_send(ssh, pktout); |
7363 | } |
7364 | } |
7365 | |
7366 | static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin) |
7367 | { |
7368 | char *type; |
7369 | int typelen; |
7370 | char *peeraddr; |
7371 | int peeraddrlen; |
7372 | int peerport; |
7373 | char *error = NULL; |
7374 | struct ssh_channel *c; |
7375 | unsigned remid, winsize, pktsize; |
7376 | struct Packet *pktout; |
7377 | |
7378 | ssh_pkt_getstring(pktin, &type, &typelen); |
7379 | c = snew(struct ssh_channel); |
7380 | c->ssh = ssh; |
7381 | |
7382 | remid = ssh_pkt_getuint32(pktin); |
7383 | winsize = ssh_pkt_getuint32(pktin); |
7384 | pktsize = ssh_pkt_getuint32(pktin); |
7385 | |
7386 | if (typelen == 3 && !memcmp(type, "x11", 3)) { |
7387 | char *addrstr; |
5907ebda |
7388 | const char *x11err; |
51df0ab5 |
7389 | |
7390 | ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen); |
7391 | addrstr = snewn(peeraddrlen+1, char); |
7392 | memcpy(addrstr, peeraddr, peeraddrlen); |
7393 | addrstr[peeraddrlen] = '\0'; |
7394 | peerport = ssh_pkt_getuint32(pktin); |
7395 | |
7396 | logeventf(ssh, "Received X11 connect request from %s:%d", |
7397 | addrstr, peerport); |
7398 | |
7399 | if (!ssh->X11_fwd_enabled) |
7400 | error = "X11 forwarding is not enabled"; |
5907ebda |
7401 | else if ((x11err = x11_init(&c->u.x11.s, ssh->x11disp, c, |
4a693cfc |
7402 | addrstr, peerport, ssh->conf)) != NULL) { |
5907ebda |
7403 | logeventf(ssh, "Local X11 connection failed: %s", x11err); |
51df0ab5 |
7404 | error = "Unable to open an X11 connection"; |
7405 | } else { |
7406 | logevent("Opening X11 forward connection succeeded"); |
7407 | c->type = CHAN_X11; |
7408 | } |
7409 | |
7410 | sfree(addrstr); |
7411 | } else if (typelen == 15 && |
7412 | !memcmp(type, "forwarded-tcpip", 15)) { |
7413 | struct ssh_rportfwd pf, *realpf; |
7414 | char *dummy; |
7415 | int dummylen; |
7416 | ssh_pkt_getstring(pktin, &dummy, &dummylen);/* skip address */ |
7417 | pf.sport = ssh_pkt_getuint32(pktin); |
7418 | ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen); |
7419 | peerport = ssh_pkt_getuint32(pktin); |
7420 | realpf = find234(ssh->rportfwds, &pf, NULL); |
7421 | logeventf(ssh, "Received remote port %d open request " |
7422 | "from %s:%d", pf.sport, peeraddr, peerport); |
7423 | if (realpf == NULL) { |
7424 | error = "Remote port is not recognised"; |
7425 | } else { |
7426 | const char *e = pfd_newconnect(&c->u.pfd.s, |
7427 | realpf->dhost, |
7428 | realpf->dport, c, |
4a693cfc |
7429 | ssh->conf, |
05581745 |
7430 | realpf->pfrec->addressfamily); |
51df0ab5 |
7431 | logeventf(ssh, "Attempting to forward remote port to " |
7432 | "%s:%d", realpf->dhost, realpf->dport); |
7433 | if (e != NULL) { |
7434 | logeventf(ssh, "Port open failed: %s", e); |
7435 | error = "Port open failed"; |
7436 | } else { |
7437 | logevent("Forwarded port opened successfully"); |
7438 | c->type = CHAN_SOCKDATA; |
7439 | } |
7440 | } |
7441 | } else if (typelen == 22 && |
c49cf994 |
7442 | !memcmp(type, "auth-agent@openssh.com", 22)) { |
51df0ab5 |
7443 | if (!ssh->agentfwd_enabled) |
7444 | error = "Agent forwarding is not enabled"; |
7445 | else { |
7446 | c->type = CHAN_AGENT; /* identify channel type */ |
7447 | c->u.a.lensofar = 0; |
be3f980d |
7448 | c->u.a.outstanding_requests = 0; |
51df0ab5 |
7449 | } |
7450 | } else { |
7451 | error = "Unsupported channel type requested"; |
7452 | } |
7453 | |
7454 | c->remoteid = remid; |
64d6ff88 |
7455 | c->halfopen = FALSE; |
51df0ab5 |
7456 | if (error) { |
7457 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
7458 | ssh2_pkt_adduint32(pktout, c->remoteid); |
7459 | ssh2_pkt_adduint32(pktout, SSH2_OPEN_CONNECT_FAILED); |
7460 | ssh2_pkt_addstring(pktout, error); |
7461 | ssh2_pkt_addstring(pktout, "en"); /* language tag */ |
7462 | ssh2_pkt_send(ssh, pktout); |
7463 | logeventf(ssh, "Rejected channel open: %s", error); |
7464 | sfree(c); |
7465 | } else { |
94bdfe40 |
7466 | ssh2_channel_init(c); |
51df0ab5 |
7467 | c->v.v2.remwindow = winsize; |
7468 | c->v.v2.remmaxpkt = pktsize; |
51df0ab5 |
7469 | add234(ssh->channels, c); |
7470 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
7471 | ssh2_pkt_adduint32(pktout, c->remoteid); |
7472 | ssh2_pkt_adduint32(pktout, c->localid); |
7473 | ssh2_pkt_adduint32(pktout, c->v.v2.locwindow); |
954d5c5a |
7474 | ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
51df0ab5 |
7475 | ssh2_pkt_send(ssh, pktout); |
7476 | } |
7477 | } |
7478 | |
6bbce591 |
7479 | /* |
adb6167a |
7480 | * Buffer banner messages for later display at some convenient point, |
7481 | * if we're going to display them. |
6bbce591 |
7482 | */ |
7483 | static void ssh2_msg_userauth_banner(Ssh ssh, struct Packet *pktin) |
7484 | { |
7485 | /* Arbitrary limit to prevent unbounded inflation of buffer */ |
4a693cfc |
7486 | if (conf_get_int(ssh->conf, CONF_ssh_show_banner) && |
adb6167a |
7487 | bufchain_size(&ssh->banner) <= 131072) { |
6bbce591 |
7488 | char *banner = NULL; |
7489 | int size = 0; |
7490 | ssh_pkt_getstring(pktin, &banner, &size); |
7491 | if (banner) |
7492 | bufchain_add(&ssh->banner, banner, size); |
7493 | } |
7494 | } |
7495 | |
c6ccd5c2 |
7496 | /* Helper function to deal with sending tty modes for "pty-req" */ |
7497 | static void ssh2_send_ttymode(void *data, char *mode, char *val) |
7498 | { |
7499 | struct Packet *pktout = (struct Packet *)data; |
7500 | int i = 0; |
7501 | unsigned int arg = 0; |
7502 | while (strcmp(mode, ssh_ttymodes[i].mode) != 0) i++; |
7503 | if (i == lenof(ssh_ttymodes)) return; |
7504 | switch (ssh_ttymodes[i].type) { |
7505 | case TTY_OP_CHAR: |
7506 | arg = ssh_tty_parse_specchar(val); |
7507 | break; |
7508 | case TTY_OP_BOOL: |
7509 | arg = ssh_tty_parse_boolean(val); |
7510 | break; |
7511 | } |
7512 | ssh2_pkt_addbyte(pktout, ssh_ttymodes[i].opcode); |
7513 | ssh2_pkt_adduint32(pktout, arg); |
7514 | } |
7515 | |
67a9ec92 |
7516 | static void ssh2_setup_x11(struct ssh_channel *c, struct Packet *pktin, |
7517 | void *ctx) |
ebbc254d |
7518 | { |
67a9ec92 |
7519 | struct ssh2_setup_x11_state { |
ebbc254d |
7520 | int crLine; |
7521 | }; |
7522 | Ssh ssh = c->ssh; |
7523 | struct Packet *pktout; |
67a9ec92 |
7524 | crStateP(ssh2_setup_x11_state, ctx); |
ebbc254d |
7525 | |
7526 | crBeginState; |
7527 | |
67a9ec92 |
7528 | logevent("Requesting X11 forwarding"); |
7529 | pktout = ssh2_chanreq_init(ssh->mainchan, "x11-req", |
7530 | ssh2_setup_x11, s); |
7531 | ssh2_pkt_addbool(pktout, 0); /* many connections */ |
7532 | ssh2_pkt_addstring(pktout, ssh->x11disp->remoteauthprotoname); |
ebbc254d |
7533 | /* |
67a9ec92 |
7534 | * Note that while we blank the X authentication data here, we don't |
7535 | * take any special action to blank the start of an X11 channel, |
7536 | * so using MIT-MAGIC-COOKIE-1 and actually opening an X connection |
7537 | * without having session blanking enabled is likely to leak your |
7538 | * cookie into the log. |
ebbc254d |
7539 | */ |
67a9ec92 |
7540 | dont_log_password(ssh, pktout, PKTLOG_BLANK); |
7541 | ssh2_pkt_addstring(pktout, ssh->x11disp->remoteauthdatastring); |
7542 | end_log_omission(ssh, pktout); |
7543 | ssh2_pkt_adduint32(pktout, ssh->x11disp->screennum); |
7544 | ssh2_pkt_send(ssh, pktout); |
ebbc254d |
7545 | |
1bc8ef67 |
7546 | /* Wait to be called back with either a response packet, or NULL |
7547 | * meaning clean up and free our data */ |
7548 | crReturnV; |
ebbc254d |
7549 | |
67a9ec92 |
7550 | if (pktin) { |
7551 | if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) { |
7552 | logevent("X11 forwarding enabled"); |
7553 | ssh->X11_fwd_enabled = TRUE; |
7554 | } else |
7555 | logevent("X11 forwarding refused"); |
ebbc254d |
7556 | } |
67a9ec92 |
7557 | |
0d201bfb |
7558 | crFinishFreeV; |
ebbc254d |
7559 | } |
7560 | |
67a9ec92 |
7561 | static void ssh2_setup_agent(struct ssh_channel *c, struct Packet *pktin, |
ebbc254d |
7562 | void *ctx) |
7563 | { |
67a9ec92 |
7564 | struct ssh2_setup_agent_state { |
ebbc254d |
7565 | int crLine; |
7566 | }; |
7567 | Ssh ssh = c->ssh; |
7568 | struct Packet *pktout; |
67a9ec92 |
7569 | crStateP(ssh2_setup_agent_state, ctx); |
ebbc254d |
7570 | |
7571 | crBeginState; |
7572 | |
67a9ec92 |
7573 | logevent("Requesting OpenSSH-style agent forwarding"); |
7574 | pktout = ssh2_chanreq_init(ssh->mainchan, "auth-agent-req@openssh.com", |
7575 | ssh2_setup_agent, s); |
7576 | ssh2_pkt_send(ssh, pktout); |
ebbc254d |
7577 | |
1bc8ef67 |
7578 | /* Wait to be called back with either a response packet, or NULL |
7579 | * meaning clean up and free our data */ |
7580 | crReturnV; |
ebbc254d |
7581 | |
67a9ec92 |
7582 | if (pktin) { |
7583 | if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) { |
7584 | logevent("Agent forwarding enabled"); |
7585 | ssh->agentfwd_enabled = TRUE; |
7586 | } else |
7587 | logevent("Agent forwarding refused"); |
ebbc254d |
7588 | } |
67a9ec92 |
7589 | |
0d201bfb |
7590 | crFinishFreeV; |
ebbc254d |
7591 | } |
7592 | |
67a9ec92 |
7593 | static void ssh2_setup_pty(struct ssh_channel *c, struct Packet *pktin, |
ebbc254d |
7594 | void *ctx) |
7595 | { |
67a9ec92 |
7596 | struct ssh2_setup_pty_state { |
ebbc254d |
7597 | int crLine; |
7598 | }; |
7599 | Ssh ssh = c->ssh; |
7600 | struct Packet *pktout; |
67a9ec92 |
7601 | crStateP(ssh2_setup_pty_state, ctx); |
ebbc254d |
7602 | |
7603 | crBeginState; |
7604 | |
67a9ec92 |
7605 | /* Unpick the terminal-speed string. */ |
7606 | /* XXX perhaps we should allow no speeds to be sent. */ |
7607 | ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */ |
7608 | sscanf(conf_get_str(ssh->conf, CONF_termspeed), "%d,%d", &ssh->ospeed, &ssh->ispeed); |
7609 | /* Build the pty request. */ |
7610 | pktout = ssh2_chanreq_init(ssh->mainchan, "pty-req", |
7611 | ssh2_setup_pty, s); |
7612 | ssh2_pkt_addstring(pktout, conf_get_str(ssh->conf, CONF_termtype)); |
7613 | ssh2_pkt_adduint32(pktout, ssh->term_width); |
7614 | ssh2_pkt_adduint32(pktout, ssh->term_height); |
7615 | ssh2_pkt_adduint32(pktout, 0); /* pixel width */ |
7616 | ssh2_pkt_adduint32(pktout, 0); /* pixel height */ |
7617 | ssh2_pkt_addstring_start(pktout); |
7618 | parse_ttymodes(ssh, ssh2_send_ttymode, (void *)pktout); |
7619 | ssh2_pkt_addbyte(pktout, SSH2_TTY_OP_ISPEED); |
7620 | ssh2_pkt_adduint32(pktout, ssh->ispeed); |
7621 | ssh2_pkt_addbyte(pktout, SSH2_TTY_OP_OSPEED); |
7622 | ssh2_pkt_adduint32(pktout, ssh->ospeed); |
7623 | ssh2_pkt_addstring_data(pktout, "\0", 1); /* TTY_OP_END */ |
7624 | ssh2_pkt_send(ssh, pktout); |
7625 | ssh->state = SSH_STATE_INTERMED; |
ebbc254d |
7626 | |
1bc8ef67 |
7627 | /* Wait to be called back with either a response packet, or NULL |
7628 | * meaning clean up and free our data */ |
7629 | crReturnV; |
ebbc254d |
7630 | |
67a9ec92 |
7631 | if (pktin) { |
7632 | if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) { |
7633 | logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)", |
7634 | ssh->ospeed, ssh->ispeed); |
7635 | ssh->got_pty = TRUE; |
7636 | } else { |
7637 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
7638 | ssh->editing = ssh->echoing = 1; |
7639 | } |
ebbc254d |
7640 | } |
67a9ec92 |
7641 | |
0d201bfb |
7642 | crFinishFreeV; |
ebbc254d |
7643 | } |
7644 | |
7645 | static void ssh2_setup_env(struct ssh_channel *c, struct Packet *pktin, |
7646 | void *ctx) |
7647 | { |
7648 | struct ssh2_setup_env_state { |
7649 | int crLine; |
7650 | int num_env, env_left, env_ok; |
7651 | }; |
7652 | Ssh ssh = c->ssh; |
7653 | struct Packet *pktout; |
7654 | crStateP(ssh2_setup_env_state, ctx); |
7655 | |
7656 | crBeginState; |
7657 | |
7658 | /* |
7659 | * Send environment variables. |
7660 | * |
7661 | * Simplest thing here is to send all the requests at once, and |
7662 | * then wait for a whole bunch of successes or failures. |
7663 | */ |
7664 | s->num_env = 0; |
67a9ec92 |
7665 | { |
ebbc254d |
7666 | char *key, *val; |
7667 | |
7668 | for (val = conf_get_str_strs(ssh->conf, CONF_environmt, NULL, &key); |
7669 | val != NULL; |
7670 | val = conf_get_str_strs(ssh->conf, CONF_environmt, key, &key)) { |
134f5ece |
7671 | pktout = ssh2_chanreq_init(ssh->mainchan, "env", ssh2_setup_env, s); |
ebbc254d |
7672 | ssh2_pkt_addstring(pktout, key); |
7673 | ssh2_pkt_addstring(pktout, val); |
7674 | ssh2_pkt_send(ssh, pktout); |
ebbc254d |
7675 | |
7676 | s->num_env++; |
7677 | } |
7678 | if (s->num_env) |
7679 | logeventf(ssh, "Sent %d environment variables", s->num_env); |
7680 | } |
7681 | |
7682 | if (s->num_env) { |
7683 | s->env_ok = 0; |
7684 | s->env_left = s->num_env; |
7685 | |
7686 | while (s->env_left > 0) { |
1bc8ef67 |
7687 | /* Wait to be called back with either a response packet, |
7688 | * or NULL meaning clean up and free our data */ |
7689 | crReturnV; |
3620f307 |
7690 | if (!pktin) goto out; |
c330b1cc |
7691 | if (pktin->type == SSH2_MSG_CHANNEL_SUCCESS) |
ebbc254d |
7692 | s->env_ok++; |
ebbc254d |
7693 | s->env_left--; |
7694 | } |
7695 | |
7696 | if (s->env_ok == s->num_env) { |
7697 | logevent("All environment variables successfully set"); |
7698 | } else if (s->env_ok == 0) { |
7699 | logevent("All environment variables refused"); |
7700 | c_write_str(ssh, "Server refused to set environment variables\r\n"); |
7701 | } else { |
7702 | logeventf(ssh, "%d environment variables refused", |
7703 | s->num_env - s->env_ok); |
7704 | c_write_str(ssh, "Server refused to set all environment variables\r\n"); |
7705 | } |
7706 | } |
5ca12719 |
7707 | out:; |
0d201bfb |
7708 | crFinishFreeV; |
ebbc254d |
7709 | } |
7710 | |
783415f8 |
7711 | /* |
2e85c969 |
7712 | * Handle the SSH-2 userauth and connection layers. |
7cca0d81 |
7713 | */ |
bc074147 |
7714 | static void ssh2_msg_authconn(Ssh ssh, struct Packet *pktin) |
7715 | { |
7716 | do_ssh2_authconn(ssh, NULL, 0, pktin); |
7717 | } |
7718 | |
ebbc254d |
7719 | static void ssh2_response_authconn(struct ssh_channel *c, struct Packet *pktin, |
7720 | void *ctx) |
7721 | { |
7722 | do_ssh2_authconn(c->ssh, NULL, 0, pktin); |
7723 | } |
7724 | |
ff3187f6 |
7725 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, |
7726 | struct Packet *pktin) |
7cca0d81 |
7727 | { |
51470298 |
7728 | struct do_ssh2_authconn_state { |
30919997 |
7729 | int crLine; |
51470298 |
7730 | enum { |
51470298 |
7731 | AUTH_TYPE_NONE, |
7732 | AUTH_TYPE_PUBLICKEY, |
7733 | AUTH_TYPE_PUBLICKEY_OFFER_LOUD, |
7734 | AUTH_TYPE_PUBLICKEY_OFFER_QUIET, |
7735 | AUTH_TYPE_PASSWORD, |
8221906d |
7736 | AUTH_TYPE_GSSAPI, /* always QUIET */ |
51470298 |
7737 | AUTH_TYPE_KEYBOARD_INTERACTIVE, |
7738 | AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET |
7739 | } type; |
a1a1fae4 |
7740 | int done_service_req; |
51470298 |
7741 | int gotit, need_pw, can_pubkey, can_passwd, can_keyb_inter; |
94cd7c3a |
7742 | int tried_pubkey_config, done_agent; |
bf84a03a |
7743 | #ifndef NO_GSSAPI |
42af6a67 |
7744 | int can_gssapi; |
7745 | int tried_gssapi; |
bf84a03a |
7746 | #endif |
edd0cb8a |
7747 | int kbd_inter_refused; |
ab18ccff |
7748 | int we_are_in, userauth_success; |
edd0cb8a |
7749 | prompts_t *cur_prompt; |
7750 | int num_prompts; |
4a693cfc |
7751 | char *username; |
e955d348 |
7752 | char *password; |
51470298 |
7753 | int got_username; |
51470298 |
7754 | void *publickey_blob; |
7755 | int publickey_bloblen; |
edd0cb8a |
7756 | int publickey_encrypted; |
7757 | char *publickey_algorithm; |
7758 | char *publickey_comment; |
94cd7c3a |
7759 | unsigned char agent_request[5], *agent_response, *agentp; |
7760 | int agent_responselen; |
7761 | unsigned char *pkblob_in_agent; |
51470298 |
7762 | int keyi, nkeys; |
51470298 |
7763 | char *pkblob, *alg, *commentp; |
7764 | int pklen, alglen, commentlen; |
7765 | int siglen, retlen, len; |
7766 | char *q, *agentreq, *ret; |
7767 | int try_send; |
ff3187f6 |
7768 | struct Packet *pktout; |
4a693cfc |
7769 | Filename *keyfile; |
bf84a03a |
7770 | #ifndef NO_GSSAPI |
b3d375b2 |
7771 | struct ssh_gss_library *gsslib; |
42af6a67 |
7772 | Ssh_gss_ctx gss_ctx; |
7773 | Ssh_gss_buf gss_buf; |
7774 | Ssh_gss_buf gss_rcvtok, gss_sndtok; |
7775 | Ssh_gss_name gss_srv_name; |
7776 | Ssh_gss_stat gss_stat; |
bf84a03a |
7777 | #endif |
51470298 |
7778 | }; |
7779 | crState(do_ssh2_authconn_state); |
7780 | |
30919997 |
7781 | crBeginState; |
e5574168 |
7782 | |
bc074147 |
7783 | /* Register as a handler for all the messages this coroutine handles. */ |
7784 | ssh->packet_dispatch[SSH2_MSG_SERVICE_ACCEPT] = ssh2_msg_authconn; |
7785 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_REQUEST] = ssh2_msg_authconn; |
7786 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_FAILURE] = ssh2_msg_authconn; |
7787 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_SUCCESS] = ssh2_msg_authconn; |
7788 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = ssh2_msg_authconn; |
7789 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_PK_OK] = ssh2_msg_authconn; |
7790 | /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ] = ssh2_msg_authconn; duplicate case value */ |
7791 | /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_REQUEST] = ssh2_msg_authconn; duplicate case value */ |
7792 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_RESPONSE] = ssh2_msg_authconn; |
7793 | ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = ssh2_msg_authconn; |
7794 | ssh->packet_dispatch[SSH2_MSG_REQUEST_SUCCESS] = ssh2_msg_authconn; |
7795 | ssh->packet_dispatch[SSH2_MSG_REQUEST_FAILURE] = ssh2_msg_authconn; |
7796 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = ssh2_msg_authconn; |
7797 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = ssh2_msg_authconn; |
7798 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = ssh2_msg_authconn; |
7799 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = ssh2_msg_authconn; |
7800 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = ssh2_msg_authconn; |
7801 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = ssh2_msg_authconn; |
7802 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_authconn; |
7803 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_authconn; |
bc074147 |
7804 | |
a1a1fae4 |
7805 | s->done_service_req = FALSE; |
ab18ccff |
7806 | s->we_are_in = s->userauth_success = FALSE; |
bf84a03a |
7807 | #ifndef NO_GSSAPI |
42af6a67 |
7808 | s->tried_gssapi = FALSE; |
bf84a03a |
7809 | #endif |
42af6a67 |
7810 | |
4a693cfc |
7811 | if (!conf_get_int(ssh->conf, CONF_ssh_no_userauth)) { |
a1a1fae4 |
7812 | /* |
7813 | * Request userauth protocol, and await a response to it. |
7814 | */ |
7815 | s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST); |
7816 | ssh2_pkt_addstring(s->pktout, "ssh-userauth"); |
7817 | ssh2_pkt_send(ssh, s->pktout); |
7818 | crWaitUntilV(pktin); |
7819 | if (pktin->type == SSH2_MSG_SERVICE_ACCEPT) |
7820 | s->done_service_req = TRUE; |
7821 | } |
7822 | if (!s->done_service_req) { |
7823 | /* |
7824 | * Request connection protocol directly, without authentication. |
7825 | */ |
7826 | s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST); |
7827 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
7828 | ssh2_pkt_send(ssh, s->pktout); |
7829 | crWaitUntilV(pktin); |
7830 | if (pktin->type == SSH2_MSG_SERVICE_ACCEPT) { |
7831 | s->we_are_in = TRUE; /* no auth required */ |
7832 | } else { |
7833 | bombout(("Server refused service request")); |
7834 | crStopV; |
7835 | } |
8d5de777 |
7836 | } |
7cca0d81 |
7837 | |
94cd7c3a |
7838 | /* Arrange to be able to deal with any BANNERs that come in. |
7839 | * (We do this now as packets may come in during the next bit.) */ |
7840 | bufchain_init(&ssh->banner); |
7841 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = |
7842 | ssh2_msg_userauth_banner; |
7843 | |
7cca0d81 |
7844 | /* |
edd0cb8a |
7845 | * Misc one-time setup for authentication. |
7846 | */ |
7847 | s->publickey_blob = NULL; |
7848 | if (!s->we_are_in) { |
7849 | |
7850 | /* |
7851 | * Load the public half of any configured public key file |
7852 | * for later use. |
7853 | */ |
4a693cfc |
7854 | s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile); |
962468d4 |
7855 | if (!filename_is_null(s->keyfile)) { |
edd0cb8a |
7856 | int keytype; |
7857 | logeventf(ssh, "Reading private key file \"%.150s\"", |
4a693cfc |
7858 | filename_to_str(s->keyfile)); |
7859 | keytype = key_type(s->keyfile); |
edd0cb8a |
7860 | if (keytype == SSH_KEYTYPE_SSH2) { |
7861 | const char *error; |
7862 | s->publickey_blob = |
4a693cfc |
7863 | ssh2_userkey_loadpub(s->keyfile, |
edd0cb8a |
7864 | &s->publickey_algorithm, |
7865 | &s->publickey_bloblen, |
7866 | &s->publickey_comment, &error); |
7867 | if (s->publickey_blob) { |
7868 | s->publickey_encrypted = |
4a693cfc |
7869 | ssh2_userkey_encrypted(s->keyfile, NULL); |
edd0cb8a |
7870 | } else { |
7871 | char *msgbuf; |
7872 | logeventf(ssh, "Unable to load private key (%s)", |
7873 | error); |
7874 | msgbuf = dupprintf("Unable to load private key file " |
7875 | "\"%.150s\" (%s)\r\n", |
4a693cfc |
7876 | filename_to_str(s->keyfile), |
edd0cb8a |
7877 | error); |
7878 | c_write_str(ssh, msgbuf); |
7879 | sfree(msgbuf); |
7880 | } |
7881 | } else { |
7882 | char *msgbuf; |
7883 | logeventf(ssh, "Unable to use this key file (%s)", |
7884 | key_type_to_str(keytype)); |
7885 | msgbuf = dupprintf("Unable to use key file \"%.150s\"" |
7886 | " (%s)\r\n", |
4a693cfc |
7887 | filename_to_str(s->keyfile), |
edd0cb8a |
7888 | key_type_to_str(keytype)); |
7889 | c_write_str(ssh, msgbuf); |
7890 | sfree(msgbuf); |
7891 | s->publickey_blob = NULL; |
7892 | } |
7893 | } |
7894 | |
94cd7c3a |
7895 | /* |
7896 | * Find out about any keys Pageant has (but if there's a |
7897 | * public key configured, filter out all others). |
7898 | */ |
7899 | s->nkeys = 0; |
7900 | s->agent_response = NULL; |
7901 | s->pkblob_in_agent = NULL; |
4a693cfc |
7902 | if (conf_get_int(ssh->conf, CONF_tryagent) && agent_exists()) { |
94cd7c3a |
7903 | |
7904 | void *r; |
7905 | |
7906 | logevent("Pageant is running. Requesting keys."); |
7907 | |
7908 | /* Request the keys held by the agent. */ |
7909 | PUT_32BIT(s->agent_request, 1); |
7910 | s->agent_request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
7911 | if (!agent_query(s->agent_request, 5, &r, &s->agent_responselen, |
7912 | ssh_agent_callback, ssh)) { |
7913 | do { |
7914 | crReturnV; |
7915 | if (pktin) { |
7916 | bombout(("Unexpected data from server while" |
7917 | " waiting for agent response")); |
7918 | crStopV; |
7919 | } |
7920 | } while (pktin || inlen > 0); |
7921 | r = ssh->agent_response; |
7922 | s->agent_responselen = ssh->agent_response_len; |
7923 | } |
7924 | s->agent_response = (unsigned char *) r; |
7925 | if (s->agent_response && s->agent_responselen >= 5 && |
7926 | s->agent_response[4] == SSH2_AGENT_IDENTITIES_ANSWER) { |
7927 | int keyi; |
7928 | unsigned char *p; |
7929 | p = s->agent_response + 5; |
b1650067 |
7930 | s->nkeys = toint(GET_32BIT(p)); |
7931 | |
7932 | /* |
7933 | * Vet the Pageant response to ensure that the key |
7934 | * count and blob lengths make sense. |
7935 | */ |
7936 | if (s->nkeys < 0) { |
7937 | logeventf(ssh, "Pageant response contained a negative" |
7938 | " key count %d", s->nkeys); |
7939 | s->nkeys = 0; |
7940 | goto done_agent_query; |
7941 | } else { |
7942 | unsigned char *q = p + 4; |
7943 | int lenleft = s->agent_responselen - 5 - 4; |
7944 | |
7945 | for (keyi = 0; keyi < s->nkeys; keyi++) { |
7946 | int bloblen, commentlen; |
7947 | if (lenleft < 4) { |
7948 | logeventf(ssh, "Pageant response was truncated"); |
7949 | s->nkeys = 0; |
7950 | goto done_agent_query; |
7951 | } |
7952 | bloblen = toint(GET_32BIT(q)); |
7953 | if (bloblen < 0 || bloblen > lenleft) { |
7954 | logeventf(ssh, "Pageant response was truncated"); |
7955 | s->nkeys = 0; |
7956 | goto done_agent_query; |
7957 | } |
7958 | lenleft -= 4 + bloblen; |
7959 | q += 4 + bloblen; |
7960 | commentlen = toint(GET_32BIT(q)); |
7961 | if (commentlen < 0 || commentlen > lenleft) { |
7962 | logeventf(ssh, "Pageant response was truncated"); |
7963 | s->nkeys = 0; |
7964 | goto done_agent_query; |
7965 | } |
7966 | lenleft -= 4 + commentlen; |
7967 | q += 4 + commentlen; |
7968 | } |
7969 | } |
7970 | |
94cd7c3a |
7971 | p += 4; |
7972 | logeventf(ssh, "Pageant has %d SSH-2 keys", s->nkeys); |
7973 | if (s->publickey_blob) { |
7974 | /* See if configured key is in agent. */ |
7975 | for (keyi = 0; keyi < s->nkeys; keyi++) { |
b1650067 |
7976 | s->pklen = toint(GET_32BIT(p)); |
94cd7c3a |
7977 | if (s->pklen == s->publickey_bloblen && |
7978 | !memcmp(p+4, s->publickey_blob, |
7979 | s->publickey_bloblen)) { |
7980 | logeventf(ssh, "Pageant key #%d matches " |
7981 | "configured key file", keyi); |
7982 | s->keyi = keyi; |
7983 | s->pkblob_in_agent = p; |
7984 | break; |
7985 | } |
7986 | p += 4 + s->pklen; |
b1650067 |
7987 | p += toint(GET_32BIT(p)) + 4; /* comment */ |
94cd7c3a |
7988 | } |
7989 | if (!s->pkblob_in_agent) { |
7990 | logevent("Configured key file not in Pageant"); |
7991 | s->nkeys = 0; |
7992 | } |
7993 | } |
5ff99b8e |
7994 | } else { |
7995 | logevent("Failed to get reply from Pageant"); |
94cd7c3a |
7996 | } |
b1650067 |
7997 | done_agent_query:; |
94cd7c3a |
7998 | } |
7999 | |
edd0cb8a |
8000 | } |
8001 | |
8002 | /* |
1408a877 |
8003 | * We repeat this whole loop, including the username prompt, |
8004 | * until we manage a successful authentication. If the user |
51470298 |
8005 | * types the wrong _password_, they can be sent back to the |
8006 | * beginning to try another username, if this is configured on. |
8007 | * (If they specify a username in the config, they are never |
8008 | * asked, even if they do give a wrong password.) |
1408a877 |
8009 | * |
8010 | * I think this best serves the needs of |
8011 | * |
8012 | * - the people who have no configuration, no keys, and just |
8013 | * want to try repeated (username,password) pairs until they |
8014 | * type both correctly |
8015 | * |
8016 | * - people who have keys and configuration but occasionally |
8017 | * need to fall back to passwords |
8018 | * |
8019 | * - people with a key held in Pageant, who might not have |
8020 | * logged in to a particular machine before; so they want to |
8021 | * type a username, and then _either_ their key will be |
8022 | * accepted, _or_ they will type a password. If they mistype |
8023 | * the username they will want to be able to get back and |
8024 | * retype it! |
7cca0d81 |
8025 | */ |
51470298 |
8026 | s->got_username = FALSE; |
a1a1fae4 |
8027 | while (!s->we_are_in) { |
1408a877 |
8028 | /* |
8029 | * Get a username. |
8030 | */ |
4a693cfc |
8031 | if (s->got_username && !conf_get_int(ssh->conf, CONF_change_username)) { |
5bb641e1 |
8032 | /* |
8033 | * We got a username last time round this loop, and |
8034 | * with change_username turned off we don't try to get |
8035 | * it again. |
8036 | */ |
4a693cfc |
8037 | } else if ((ssh->username = get_remote_username(ssh->conf)) == NULL) { |
edd0cb8a |
8038 | int ret; /* need not be kept over crReturn */ |
8039 | s->cur_prompt = new_prompts(ssh->frontend); |
8040 | s->cur_prompt->to_server = TRUE; |
8041 | s->cur_prompt->name = dupstr("SSH login name"); |
b61f81bc |
8042 | add_prompt(s->cur_prompt, dupstr("login as: "), TRUE); |
edd0cb8a |
8043 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
8044 | while (ret < 0) { |
51470298 |
8045 | ssh->send_ok = 1; |
edd0cb8a |
8046 | crWaitUntilV(!pktin); |
8047 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
8048 | ssh->send_ok = 0; |
32874aea |
8049 | } |
edd0cb8a |
8050 | if (!ret) { |
8051 | /* |
8052 | * get_userpass_input() failed to get a username. |
8053 | * Terminate. |
8054 | */ |
8055 | free_prompts(s->cur_prompt); |
8056 | ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE); |
8057 | crStopV; |
8058 | } |
4a693cfc |
8059 | ssh->username = dupstr(s->cur_prompt->prompts[0]->result); |
edd0cb8a |
8060 | free_prompts(s->cur_prompt); |
7cca0d81 |
8061 | } else { |
57356d63 |
8062 | char *stuff; |
65a22376 |
8063 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
4a693cfc |
8064 | stuff = dupprintf("Using username \"%s\".\r\n", ssh->username); |
51470298 |
8065 | c_write_str(ssh, stuff); |
57356d63 |
8066 | sfree(stuff); |
7cca0d81 |
8067 | } |
8068 | } |
51470298 |
8069 | s->got_username = TRUE; |
7cca0d81 |
8070 | |
65a22376 |
8071 | /* |
1408a877 |
8072 | * Send an authentication request using method "none": (a) |
8073 | * just in case it succeeds, and (b) so that we know what |
8074 | * authentication methods we can usefully try next. |
65a22376 |
8075 | */ |
acab36bc |
8076 | ssh->pkt_actx = SSH2_PKTCTX_NOAUTH; |
51470298 |
8077 | |
ff3187f6 |
8078 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8079 | ssh2_pkt_addstring(s->pktout, ssh->username); |
ff3187f6 |
8080 | ssh2_pkt_addstring(s->pktout, "ssh-connection");/* service requested */ |
8081 | ssh2_pkt_addstring(s->pktout, "none"); /* method */ |
8082 | ssh2_pkt_send(ssh, s->pktout); |
51470298 |
8083 | s->type = AUTH_TYPE_NONE; |
8084 | s->gotit = FALSE; |
8085 | s->we_are_in = FALSE; |
8086 | |
8087 | s->tried_pubkey_config = FALSE; |
6ac3a551 |
8088 | s->kbd_inter_refused = FALSE; |
65a22376 |
8089 | |
94cd7c3a |
8090 | /* Reset agent request state. */ |
8091 | s->done_agent = FALSE; |
8092 | if (s->agent_response) { |
8093 | if (s->pkblob_in_agent) { |
8094 | s->agentp = s->pkblob_in_agent; |
8095 | } else { |
8096 | s->agentp = s->agent_response + 5 + 4; |
8097 | s->keyi = 0; |
8098 | } |
8099 | } |
8100 | |
1408a877 |
8101 | while (1) { |
854caca5 |
8102 | char *methods = NULL; |
8103 | int methlen = 0; |
8104 | |
1408a877 |
8105 | /* |
8106 | * Wait for the result of the last authentication request. |
8107 | */ |
51470298 |
8108 | if (!s->gotit) |
ff3187f6 |
8109 | crWaitUntilV(pktin); |
6bbce591 |
8110 | /* |
8111 | * Now is a convenient point to spew any banner material |
8112 | * that we've accumulated. (This should ensure that when |
8113 | * we exit the auth loop, we haven't any left to deal |
8114 | * with.) |
8115 | */ |
8116 | { |
8117 | int size = bufchain_size(&ssh->banner); |
32874aea |
8118 | /* |
8119 | * Don't show the banner if we're operating in |
8120 | * non-verbose non-interactive mode. (It's probably |
8121 | * a script, which means nobody will read the |
8122 | * banner _anyway_, and moreover the printing of |
8123 | * the banner will screw up processing on the |
8124 | * output of (say) plink.) |
8125 | */ |
6bbce591 |
8126 | if (size && (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE))) { |
8127 | char *banner = snewn(size, char); |
8128 | bufchain_fetch(&ssh->banner, banner, size); |
8129 | c_write_untrusted(ssh, banner, size); |
8130 | sfree(banner); |
32874aea |
8131 | } |
6bbce591 |
8132 | bufchain_clear(&ssh->banner); |
1408a877 |
8133 | } |
ff3187f6 |
8134 | if (pktin->type == SSH2_MSG_USERAUTH_SUCCESS) { |
1408a877 |
8135 | logevent("Access granted"); |
ab18ccff |
8136 | s->we_are_in = s->userauth_success = TRUE; |
1408a877 |
8137 | break; |
8138 | } |
65a22376 |
8139 | |
42af6a67 |
8140 | if (pktin->type != SSH2_MSG_USERAUTH_FAILURE && s->type != AUTH_TYPE_GSSAPI) { |
e955d348 |
8141 | bombout(("Strange packet received during authentication: " |
8142 | "type %d", pktin->type)); |
7ffdbc1a |
8143 | crStopV; |
65a22376 |
8144 | } |
8145 | |
51470298 |
8146 | s->gotit = FALSE; |
65a22376 |
8147 | |
1408a877 |
8148 | /* |
8149 | * OK, we're now sitting on a USERAUTH_FAILURE message, so |
8150 | * we can look at the string in it and know what we can |
8151 | * helpfully try next. |
8152 | */ |
ff3187f6 |
8153 | if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) { |
ff3187f6 |
8154 | ssh_pkt_getstring(pktin, &methods, &methlen); |
ff3187f6 |
8155 | if (!ssh2_pkt_getbool(pktin)) { |
1408a877 |
8156 | /* |
8157 | * We have received an unequivocal Access |
8158 | * Denied. This can translate to a variety of |
8221906d |
8159 | * messages, or no message at all. |
8160 | * |
8161 | * For forms of authentication which are attempted |
8162 | * implicitly, by which I mean without printing |
8163 | * anything in the window indicating that we're |
8164 | * trying them, we should never print 'Access |
8165 | * denied'. |
8166 | * |
8167 | * If we do print a message saying that we're |
8168 | * attempting some kind of authentication, it's OK |
8169 | * to print a followup message saying it failed - |
8170 | * but the message may sometimes be more specific |
8171 | * than simply 'Access denied'. |
8172 | * |
1408a877 |
8173 | * Additionally, if we'd just tried password |
8174 | * authentication, we should break out of this |
8175 | * whole loop so as to go back to the username |
91f57d1f |
8176 | * prompt (iff we're configured to allow |
8177 | * username change attempts). |
1408a877 |
8178 | */ |
51470298 |
8179 | if (s->type == AUTH_TYPE_NONE) { |
1408a877 |
8180 | /* do nothing */ |
51470298 |
8181 | } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD || |
8182 | s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) { |
8183 | if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD) |
8184 | c_write_str(ssh, "Server refused our key\r\n"); |
8221906d |
8185 | logevent("Server refused our key"); |
8186 | } else if (s->type == AUTH_TYPE_PUBLICKEY) { |
8187 | /* This _shouldn't_ happen except by a |
8188 | * protocol bug causing client and server to |
8189 | * disagree on what is a correct signature. */ |
8190 | c_write_str(ssh, "Server refused public-key signature" |
8191 | " despite accepting key!\r\n"); |
8192 | logevent("Server refused public-key signature" |
8193 | " despite accepting key!"); |
51470298 |
8194 | } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) { |
8221906d |
8195 | /* quiet, so no c_write */ |
8196 | logevent("Server refused keyboard-interactive authentication"); |
8197 | } else if (s->type==AUTH_TYPE_GSSAPI) { |
8198 | /* always quiet, so no c_write */ |
2af4fe8f |
8199 | /* also, the code down in the GSSAPI block has |
8200 | * already logged this in the Event Log */ |
8221906d |
8201 | } else if (s->type == AUTH_TYPE_KEYBOARD_INTERACTIVE) { |
8202 | logevent("Keyboard-interactive authentication failed"); |
8203 | c_write_str(ssh, "Access denied\r\n"); |
8204 | } else { |
8205 | assert(s->type == AUTH_TYPE_PASSWORD); |
8206 | logevent("Password authentication failed"); |
51470298 |
8207 | c_write_str(ssh, "Access denied\r\n"); |
8221906d |
8208 | |
8209 | if (conf_get_int(ssh->conf, CONF_change_username)) { |
6c9dce7c |
8210 | /* XXX perhaps we should allow |
8211 | * keyboard-interactive to do this too? */ |
51470298 |
8212 | s->we_are_in = FALSE; |
1408a877 |
8213 | break; |
8214 | } |
8215 | } |
8216 | } else { |
51470298 |
8217 | c_write_str(ssh, "Further authentication required\r\n"); |
1408a877 |
8218 | logevent("Further authentication required"); |
8219 | } |
65a22376 |
8220 | |
51470298 |
8221 | s->can_pubkey = |
32874aea |
8222 | in_commasep_string("publickey", methods, methlen); |
51470298 |
8223 | s->can_passwd = |
32874aea |
8224 | in_commasep_string("password", methods, methlen); |
4a693cfc |
8225 | s->can_keyb_inter = conf_get_int(ssh->conf, CONF_try_ki_auth) && |
761187b6 |
8226 | in_commasep_string("keyboard-interactive", methods, methlen); |
1e00c92b |
8227 | #ifndef NO_GSSAPI |
8228 | if (!ssh->gsslibs) |
4a693cfc |
8229 | ssh->gsslibs = ssh_gss_setup(ssh->conf); |
8230 | s->can_gssapi = conf_get_int(ssh->conf, CONF_try_gssapi_auth) && |
b3d375b2 |
8231 | in_commasep_string("gssapi-with-mic", methods, methlen) && |
1e00c92b |
8232 | ssh->gsslibs->nlibraries > 0; |
42af6a67 |
8233 | #endif |
1408a877 |
8234 | } |
65a22376 |
8235 | |
acab36bc |
8236 | ssh->pkt_actx = SSH2_PKTCTX_NOAUTH; |
65a22376 |
8237 | |
94cd7c3a |
8238 | if (s->can_pubkey && !s->done_agent && s->nkeys) { |
0405e71f |
8239 | |
1983e559 |
8240 | /* |
94cd7c3a |
8241 | * Attempt public-key authentication using a key from Pageant. |
1983e559 |
8242 | */ |
1983e559 |
8243 | |
acab36bc |
8244 | ssh->pkt_actx = SSH2_PKTCTX_PUBLICKEY; |
00db133f |
8245 | |
94cd7c3a |
8246 | logeventf(ssh, "Trying Pageant key #%d", s->keyi); |
8247 | |
8248 | /* Unpack key from agent response */ |
b1650067 |
8249 | s->pklen = toint(GET_32BIT(s->agentp)); |
94cd7c3a |
8250 | s->agentp += 4; |
8251 | s->pkblob = (char *)s->agentp; |
8252 | s->agentp += s->pklen; |
b1650067 |
8253 | s->alglen = toint(GET_32BIT(s->pkblob)); |
94cd7c3a |
8254 | s->alg = s->pkblob + 4; |
b1650067 |
8255 | s->commentlen = toint(GET_32BIT(s->agentp)); |
94cd7c3a |
8256 | s->agentp += 4; |
8257 | s->commentp = (char *)s->agentp; |
8258 | s->agentp += s->commentlen; |
8259 | /* s->agentp now points at next key, if any */ |
8260 | |
8261 | /* See if server will accept it */ |
8262 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8263 | ssh2_pkt_addstring(s->pktout, ssh->username); |
94cd7c3a |
8264 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
8265 | /* service requested */ |
8266 | ssh2_pkt_addstring(s->pktout, "publickey"); |
8267 | /* method */ |
8268 | ssh2_pkt_addbool(s->pktout, FALSE); /* no signature included */ |
8269 | ssh2_pkt_addstring_start(s->pktout); |
8270 | ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen); |
8271 | ssh2_pkt_addstring_start(s->pktout); |
8272 | ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen); |
8273 | ssh2_pkt_send(ssh, s->pktout); |
8274 | s->type = AUTH_TYPE_PUBLICKEY_OFFER_QUIET; |
1983e559 |
8275 | |
94cd7c3a |
8276 | crWaitUntilV(pktin); |
8277 | if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) { |
1983e559 |
8278 | |
94cd7c3a |
8279 | /* Offer of key refused. */ |
8280 | s->gotit = TRUE; |
1983e559 |
8281 | |
94cd7c3a |
8282 | } else { |
8283 | |
8284 | void *vret; |
1983e559 |
8285 | |
94cd7c3a |
8286 | if (flags & FLAG_VERBOSE) { |
8287 | c_write_str(ssh, "Authenticating with " |
8288 | "public key \""); |
8289 | c_write(ssh, s->commentp, s->commentlen); |
8290 | c_write_str(ssh, "\" from agent\r\n"); |
8291 | } |
1983e559 |
8292 | |
94cd7c3a |
8293 | /* |
8294 | * Server is willing to accept the key. |
8295 | * Construct a SIGN_REQUEST. |
8296 | */ |
8297 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8298 | ssh2_pkt_addstring(s->pktout, ssh->username); |
94cd7c3a |
8299 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
8300 | /* service requested */ |
8301 | ssh2_pkt_addstring(s->pktout, "publickey"); |
8302 | /* method */ |
8303 | ssh2_pkt_addbool(s->pktout, TRUE); /* signature included */ |
8304 | ssh2_pkt_addstring_start(s->pktout); |
8305 | ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen); |
8306 | ssh2_pkt_addstring_start(s->pktout); |
8307 | ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen); |
8308 | |
8309 | /* Ask agent for signature. */ |
8310 | s->siglen = s->pktout->length - 5 + 4 + |
8311 | ssh->v2_session_id_len; |
8312 | if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID) |
8313 | s->siglen -= 4; |
8314 | s->len = 1; /* message type */ |
8315 | s->len += 4 + s->pklen; /* key blob */ |
8316 | s->len += 4 + s->siglen; /* data to sign */ |
8317 | s->len += 4; /* flags */ |
8318 | s->agentreq = snewn(4 + s->len, char); |
8319 | PUT_32BIT(s->agentreq, s->len); |
8320 | s->q = s->agentreq + 4; |
8321 | *s->q++ = SSH2_AGENTC_SIGN_REQUEST; |
8322 | PUT_32BIT(s->q, s->pklen); |
8323 | s->q += 4; |
8324 | memcpy(s->q, s->pkblob, s->pklen); |
8325 | s->q += s->pklen; |
8326 | PUT_32BIT(s->q, s->siglen); |
8327 | s->q += 4; |
8328 | /* Now the data to be signed... */ |
8329 | if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) { |
8330 | PUT_32BIT(s->q, ssh->v2_session_id_len); |
51470298 |
8331 | s->q += 4; |
94cd7c3a |
8332 | } |
8333 | memcpy(s->q, ssh->v2_session_id, |
8334 | ssh->v2_session_id_len); |
8335 | s->q += ssh->v2_session_id_len; |
8336 | memcpy(s->q, s->pktout->data + 5, |
8337 | s->pktout->length - 5); |
8338 | s->q += s->pktout->length - 5; |
8339 | /* And finally the (zero) flags word. */ |
8340 | PUT_32BIT(s->q, 0); |
8341 | if (!agent_query(s->agentreq, s->len + 4, |
8342 | &vret, &s->retlen, |
8343 | ssh_agent_callback, ssh)) { |
8344 | do { |
8345 | crReturnV; |
8346 | if (pktin) { |
8347 | bombout(("Unexpected data from server" |
8348 | " while waiting for agent" |
8349 | " response")); |
8350 | crStopV; |
1983e559 |
8351 | } |
94cd7c3a |
8352 | } while (pktin || inlen > 0); |
8353 | vret = ssh->agent_response; |
8354 | s->retlen = ssh->agent_response_len; |
8355 | } |
8356 | s->ret = vret; |
8357 | sfree(s->agentreq); |
8358 | if (s->ret) { |
b1650067 |
8359 | if (s->retlen >= 9 && |
8360 | s->ret[4] == SSH2_AGENT_SIGN_RESPONSE && |
8361 | GET_32BIT(s->ret + 5) <= (unsigned)(s->retlen-9)) { |
94cd7c3a |
8362 | logevent("Sending Pageant's response"); |
8363 | ssh2_add_sigblob(ssh, s->pktout, |
8364 | s->pkblob, s->pklen, |
8365 | s->ret + 9, |
8366 | GET_32BIT(s->ret + 5)); |
8367 | ssh2_pkt_send(ssh, s->pktout); |
8368 | s->type = AUTH_TYPE_PUBLICKEY; |
8369 | } else { |
8370 | /* FIXME: less drastic response */ |
8371 | bombout(("Pageant failed to answer challenge")); |
8372 | crStopV; |
1983e559 |
8373 | } |
8374 | } |
1983e559 |
8375 | } |
94cd7c3a |
8376 | |
8377 | /* Do we have any keys left to try? */ |
8378 | if (s->pkblob_in_agent) { |
8379 | s->done_agent = TRUE; |
8380 | s->tried_pubkey_config = TRUE; |
8381 | } else { |
8382 | s->keyi++; |
8383 | if (s->keyi >= s->nkeys) |
8384 | s->done_agent = TRUE; |
8385 | } |
1983e559 |
8386 | |
edd0cb8a |
8387 | } else if (s->can_pubkey && s->publickey_blob && |
8388 | !s->tried_pubkey_config) { |
65a22376 |
8389 | |
edd0cb8a |
8390 | struct ssh2_userkey *key; /* not live over crReturn */ |
8391 | char *passphrase; /* not live over crReturn */ |
65a22376 |
8392 | |
acab36bc |
8393 | ssh->pkt_actx = SSH2_PKTCTX_PUBLICKEY; |
00db133f |
8394 | |
edd0cb8a |
8395 | s->tried_pubkey_config = TRUE; |
8396 | |
65a22376 |
8397 | /* |
1408a877 |
8398 | * Try the public key supplied in the configuration. |
8399 | * |
8400 | * First, offer the public blob to see if the server is |
8401 | * willing to accept it. |
65a22376 |
8402 | */ |
ff3187f6 |
8403 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8404 | ssh2_pkt_addstring(s->pktout, ssh->username); |
edd0cb8a |
8405 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
8406 | /* service requested */ |
8407 | ssh2_pkt_addstring(s->pktout, "publickey"); /* method */ |
8408 | ssh2_pkt_addbool(s->pktout, FALSE); |
8409 | /* no signature included */ |
8410 | ssh2_pkt_addstring(s->pktout, s->publickey_algorithm); |
8411 | ssh2_pkt_addstring_start(s->pktout); |
8412 | ssh2_pkt_addstring_data(s->pktout, |
8413 | (char *)s->publickey_blob, |
8414 | s->publickey_bloblen); |
ff3187f6 |
8415 | ssh2_pkt_send(ssh, s->pktout); |
edd0cb8a |
8416 | logevent("Offered public key"); |
ff3187f6 |
8417 | |
8418 | crWaitUntilV(pktin); |
edd0cb8a |
8419 | if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) { |
8420 | /* Key refused. Give up. */ |
8421 | s->gotit = TRUE; /* reconsider message next loop */ |
8422 | s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD; |
8423 | continue; /* process this new message */ |
45068b27 |
8424 | } |
edd0cb8a |
8425 | logevent("Offer of public key accepted"); |
af659722 |
8426 | |
45068b27 |
8427 | /* |
edd0cb8a |
8428 | * Actually attempt a serious authentication using |
8429 | * the key. |
45068b27 |
8430 | */ |
edd0cb8a |
8431 | if (flags & FLAG_VERBOSE) { |
8432 | c_write_str(ssh, "Authenticating with public key \""); |
8433 | c_write_str(ssh, s->publickey_comment); |
8434 | c_write_str(ssh, "\"\r\n"); |
8435 | } |
8436 | key = NULL; |
8437 | while (!key) { |
8438 | const char *error; /* not live over crReturn */ |
8439 | if (s->publickey_encrypted) { |
8440 | /* |
8441 | * Get a passphrase from the user. |
8442 | */ |
8443 | int ret; /* need not be kept over crReturn */ |
8444 | s->cur_prompt = new_prompts(ssh->frontend); |
8445 | s->cur_prompt->to_server = FALSE; |
8446 | s->cur_prompt->name = dupstr("SSH key passphrase"); |
8447 | add_prompt(s->cur_prompt, |
8448 | dupprintf("Passphrase for key \"%.100s\": ", |
8449 | s->publickey_comment), |
b61f81bc |
8450 | FALSE); |
edd0cb8a |
8451 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
8452 | while (ret < 0) { |
8453 | ssh->send_ok = 1; |
8454 | crWaitUntilV(!pktin); |
8455 | ret = get_userpass_input(s->cur_prompt, |
8456 | in, inlen); |
8457 | ssh->send_ok = 0; |
8458 | } |
8459 | if (!ret) { |
8460 | /* Failed to get a passphrase. Terminate. */ |
8461 | free_prompts(s->cur_prompt); |
8462 | ssh_disconnect(ssh, NULL, |
8463 | "Unable to authenticate", |
8464 | SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER, |
8465 | TRUE); |
8466 | crStopV; |
85fdbe25 |
8467 | } |
edd0cb8a |
8468 | passphrase = |
8469 | dupstr(s->cur_prompt->prompts[0]->result); |
8470 | free_prompts(s->cur_prompt); |
45068b27 |
8471 | } else { |
edd0cb8a |
8472 | passphrase = NULL; /* no passphrase needed */ |
45068b27 |
8473 | } |
1408a877 |
8474 | |
edd0cb8a |
8475 | /* |
8476 | * Try decrypting the key. |
8477 | */ |
4a693cfc |
8478 | s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile); |
8479 | key = ssh2_load_userkey(s->keyfile, passphrase, &error); |
edd0cb8a |
8480 | if (passphrase) { |
8481 | /* burn the evidence */ |
dfb88efd |
8482 | smemclr(passphrase, strlen(passphrase)); |
edd0cb8a |
8483 | sfree(passphrase); |
8484 | } |
8485 | if (key == SSH2_WRONG_PASSPHRASE || key == NULL) { |
8486 | if (passphrase && |
8487 | (key == SSH2_WRONG_PASSPHRASE)) { |
8488 | c_write_str(ssh, "Wrong passphrase\r\n"); |
8489 | key = NULL; |
8490 | /* and loop again */ |
8491 | } else { |
8492 | c_write_str(ssh, "Unable to load private key ("); |
8493 | c_write_str(ssh, error); |
8494 | c_write_str(ssh, ")\r\n"); |
8495 | key = NULL; |
8496 | break; /* try something else */ |
8497 | } |
1408a877 |
8498 | } |
65a22376 |
8499 | } |
65a22376 |
8500 | |
edd0cb8a |
8501 | if (key) { |
1dd353b5 |
8502 | unsigned char *pkblob, *sigblob, *sigdata; |
8503 | int pkblob_len, sigblob_len, sigdata_len; |
edd0cb8a |
8504 | int p; |
65a22376 |
8505 | |
1408a877 |
8506 | /* |
8507 | * We have loaded the private key and the server |
8508 | * has announced that it's willing to accept it. |
8509 | * Hallelujah. Generate a signature and send it. |
8510 | */ |
ff3187f6 |
8511 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8512 | ssh2_pkt_addstring(s->pktout, ssh->username); |
edd0cb8a |
8513 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
8514 | /* service requested */ |
8515 | ssh2_pkt_addstring(s->pktout, "publickey"); |
8516 | /* method */ |
ff3187f6 |
8517 | ssh2_pkt_addbool(s->pktout, TRUE); |
edd0cb8a |
8518 | /* signature follows */ |
ff3187f6 |
8519 | ssh2_pkt_addstring(s->pktout, key->alg->name); |
edd0cb8a |
8520 | pkblob = key->alg->public_blob(key->data, |
8521 | &pkblob_len); |
ff3187f6 |
8522 | ssh2_pkt_addstring_start(s->pktout); |
edd0cb8a |
8523 | ssh2_pkt_addstring_data(s->pktout, (char *)pkblob, |
8524 | pkblob_len); |
1408a877 |
8525 | |
8526 | /* |
8527 | * The data to be signed is: |
8528 | * |
8529 | * string session-id |
8530 | * |
8531 | * followed by everything so far placed in the |
8532 | * outgoing packet. |
8533 | */ |
b672f405 |
8534 | sigdata_len = s->pktout->length - 5 + 4 + |
8535 | ssh->v2_session_id_len; |
edd0cb8a |
8536 | if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID) |
8537 | sigdata_len -= 4; |
92d60585 |
8538 | sigdata = snewn(sigdata_len, unsigned char); |
edd0cb8a |
8539 | p = 0; |
8540 | if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) { |
8541 | PUT_32BIT(sigdata+p, ssh->v2_session_id_len); |
8542 | p += 4; |
8543 | } |
b672f405 |
8544 | memcpy(sigdata+p, ssh->v2_session_id, |
8545 | ssh->v2_session_id_len); |
8546 | p += ssh->v2_session_id_len; |
ff3187f6 |
8547 | memcpy(sigdata+p, s->pktout->data + 5, |
8548 | s->pktout->length - 5); |
edd0cb8a |
8549 | p += s->pktout->length - 5; |
8550 | assert(p == sigdata_len); |
d8baa528 |
8551 | sigblob = key->alg->sign(key->data, (char *)sigdata, |
1dd353b5 |
8552 | sigdata_len, &sigblob_len); |
ff3187f6 |
8553 | ssh2_add_sigblob(ssh, s->pktout, pkblob, pkblob_len, |
1dd353b5 |
8554 | sigblob, sigblob_len); |
8555 | sfree(pkblob); |
8556 | sfree(sigblob); |
1408a877 |
8557 | sfree(sigdata); |
8558 | |
ff3187f6 |
8559 | ssh2_pkt_send(ssh, s->pktout); |
8221906d |
8560 | logevent("Sent public key signature"); |
51470298 |
8561 | s->type = AUTH_TYPE_PUBLICKEY; |
75374b2f |
8562 | key->alg->freekey(key->data); |
1408a877 |
8563 | } |
edd0cb8a |
8564 | |
42af6a67 |
8565 | #ifndef NO_GSSAPI |
8566 | } else if (s->can_gssapi && !s->tried_gssapi) { |
8567 | |
8568 | /* GSSAPI Authentication */ |
8569 | |
86557057 |
8570 | int micoffset, len; |
8571 | char *data; |
42af6a67 |
8572 | Ssh_gss_buf mic; |
8573 | s->type = AUTH_TYPE_GSSAPI; |
8574 | s->tried_gssapi = TRUE; |
8575 | s->gotit = TRUE; |
8576 | ssh->pkt_actx = SSH2_PKTCTX_GSSAPI; |
8577 | |
b3d375b2 |
8578 | /* |
8579 | * Pick the highest GSS library on the preference |
8580 | * list. |
8581 | */ |
8582 | { |
8583 | int i, j; |
8584 | s->gsslib = NULL; |
8585 | for (i = 0; i < ngsslibs; i++) { |
4a693cfc |
8586 | int want_id = conf_get_int_int(ssh->conf, |
8587 | CONF_ssh_gsslist, i); |
1e00c92b |
8588 | for (j = 0; j < ssh->gsslibs->nlibraries; j++) |
8589 | if (ssh->gsslibs->libraries[j].id == want_id) { |
8590 | s->gsslib = &ssh->gsslibs->libraries[j]; |
b3d375b2 |
8591 | goto got_gsslib; /* double break */ |
8592 | } |
8593 | } |
8594 | got_gsslib: |
8595 | /* |
8596 | * We always expect to have found something in |
8597 | * the above loop: we only came here if there |
8598 | * was at least one viable GSS library, and the |
8599 | * preference list should always mention |
8600 | * everything and only change the order. |
8601 | */ |
8602 | assert(s->gsslib); |
8603 | } |
8604 | |
8605 | if (s->gsslib->gsslogmsg) |
8606 | logevent(s->gsslib->gsslogmsg); |
8607 | |
42af6a67 |
8608 | /* Sending USERAUTH_REQUEST with "gssapi-with-mic" method */ |
8609 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8610 | ssh2_pkt_addstring(s->pktout, ssh->username); |
42af6a67 |
8611 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
8612 | ssh2_pkt_addstring(s->pktout, "gssapi-with-mic"); |
8221906d |
8613 | logevent("Attempting GSSAPI authentication"); |
42af6a67 |
8614 | |
8615 | /* add mechanism info */ |
b3d375b2 |
8616 | s->gsslib->indicate_mech(s->gsslib, &s->gss_buf); |
42af6a67 |
8617 | |
8618 | /* number of GSSAPI mechanisms */ |
8619 | ssh2_pkt_adduint32(s->pktout,1); |
8620 | |
8621 | /* length of OID + 2 */ |
86557057 |
8622 | ssh2_pkt_adduint32(s->pktout, s->gss_buf.length + 2); |
42af6a67 |
8623 | ssh2_pkt_addbyte(s->pktout, SSH2_GSS_OIDTYPE); |
8624 | |
8625 | /* length of OID */ |
86557057 |
8626 | ssh2_pkt_addbyte(s->pktout, (unsigned char) s->gss_buf.length); |
42af6a67 |
8627 | |
86557057 |
8628 | ssh_pkt_adddata(s->pktout, s->gss_buf.value, |
8629 | s->gss_buf.length); |
42af6a67 |
8630 | ssh2_pkt_send(ssh, s->pktout); |
8631 | crWaitUntilV(pktin); |
8632 | if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_RESPONSE) { |
8633 | logevent("GSSAPI authentication request refused"); |
8634 | continue; |
8635 | } |
8636 | |
8637 | /* check returned packet ... */ |
8638 | |
86557057 |
8639 | ssh_pkt_getstring(pktin, &data, &len); |
8640 | s->gss_rcvtok.value = data; |
8641 | s->gss_rcvtok.length = len; |
8642 | if (s->gss_rcvtok.length != s->gss_buf.length + 2 || |
8643 | ((char *)s->gss_rcvtok.value)[0] != SSH2_GSS_OIDTYPE || |
8644 | ((char *)s->gss_rcvtok.value)[1] != s->gss_buf.length || |
8645 | memcmp((char *)s->gss_rcvtok.value + 2, |
8646 | s->gss_buf.value,s->gss_buf.length) ) { |
42af6a67 |
8647 | logevent("GSSAPI authentication - wrong response from server"); |
8648 | continue; |
8649 | } |
8650 | |
8651 | /* now start running */ |
b3d375b2 |
8652 | s->gss_stat = s->gsslib->import_name(s->gsslib, |
8653 | ssh->fullhostname, |
8654 | &s->gss_srv_name); |
42af6a67 |
8655 | if (s->gss_stat != SSH_GSS_OK) { |
8656 | if (s->gss_stat == SSH_GSS_BAD_HOST_NAME) |
8657 | logevent("GSSAPI import name failed - Bad service name"); |
8658 | else |
8659 | logevent("GSSAPI import name failed"); |
8660 | continue; |
8661 | } |
8662 | |
8663 | /* fetch TGT into GSS engine */ |
b3d375b2 |
8664 | s->gss_stat = s->gsslib->acquire_cred(s->gsslib, &s->gss_ctx); |
42af6a67 |
8665 | |
8666 | if (s->gss_stat != SSH_GSS_OK) { |
8667 | logevent("GSSAPI authentication failed to get credentials"); |
b3d375b2 |
8668 | s->gsslib->release_name(s->gsslib, &s->gss_srv_name); |
42af6a67 |
8669 | continue; |
8670 | } |
8671 | |
8672 | /* initial tokens are empty */ |
86557057 |
8673 | SSH_GSS_CLEAR_BUF(&s->gss_rcvtok); |
99a3b3c9 |
8674 | SSH_GSS_CLEAR_BUF(&s->gss_sndtok); |
42af6a67 |
8675 | |
8676 | /* now enter the loop */ |
8677 | do { |
b3d375b2 |
8678 | s->gss_stat = s->gsslib->init_sec_context |
8679 | (s->gsslib, |
8680 | &s->gss_ctx, |
8681 | s->gss_srv_name, |
4a693cfc |
8682 | conf_get_int(ssh->conf, CONF_gssapifwd), |
b3d375b2 |
8683 | &s->gss_rcvtok, |
8684 | &s->gss_sndtok); |
42af6a67 |
8685 | |
8686 | if (s->gss_stat!=SSH_GSS_S_COMPLETE && |
8687 | s->gss_stat!=SSH_GSS_S_CONTINUE_NEEDED) { |
8688 | logevent("GSSAPI authentication initialisation failed"); |
8689 | |
b3d375b2 |
8690 | if (s->gsslib->display_status(s->gsslib, s->gss_ctx, |
8691 | &s->gss_buf) == SSH_GSS_OK) { |
86557057 |
8692 | logevent(s->gss_buf.value); |
8693 | sfree(s->gss_buf.value); |
42af6a67 |
8694 | } |
8695 | |
8696 | break; |
8697 | } |
8698 | logevent("GSSAPI authentication initialised"); |
8699 | |
8700 | /* Client and server now exchange tokens until GSSAPI |
8701 | * no longer says CONTINUE_NEEDED */ |
8702 | |
86557057 |
8703 | if (s->gss_sndtok.length != 0) { |
42af6a67 |
8704 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); |
8705 | ssh_pkt_addstring_start(s->pktout); |
86557057 |
8706 | ssh_pkt_addstring_data(s->pktout,s->gss_sndtok.value,s->gss_sndtok.length); |
42af6a67 |
8707 | ssh2_pkt_send(ssh, s->pktout); |
b3d375b2 |
8708 | s->gsslib->free_tok(s->gsslib, &s->gss_sndtok); |
42af6a67 |
8709 | } |
8710 | |
8711 | if (s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED) { |
8712 | crWaitUntilV(pktin); |
8713 | if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_TOKEN) { |
8714 | logevent("GSSAPI authentication - bad server response"); |
8715 | s->gss_stat = SSH_GSS_FAILURE; |
8716 | break; |
8717 | } |
86557057 |
8718 | ssh_pkt_getstring(pktin, &data, &len); |
8719 | s->gss_rcvtok.value = data; |
8720 | s->gss_rcvtok.length = len; |
42af6a67 |
8721 | } |
8722 | } while (s-> gss_stat == SSH_GSS_S_CONTINUE_NEEDED); |
8723 | |
8724 | if (s->gss_stat != SSH_GSS_OK) { |
b3d375b2 |
8725 | s->gsslib->release_name(s->gsslib, &s->gss_srv_name); |
8726 | s->gsslib->release_cred(s->gsslib, &s->gss_ctx); |
42af6a67 |
8727 | continue; |
8728 | } |
8729 | logevent("GSSAPI authentication loop finished OK"); |
8730 | |
8731 | /* Now send the MIC */ |
8732 | |
8733 | s->pktout = ssh2_pkt_init(0); |
8734 | micoffset = s->pktout->length; |
8735 | ssh_pkt_addstring_start(s->pktout); |
8736 | ssh_pkt_addstring_data(s->pktout, (char *)ssh->v2_session_id, ssh->v2_session_id_len); |
8737 | ssh_pkt_addbyte(s->pktout, SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8738 | ssh_pkt_addstring(s->pktout, ssh->username); |
42af6a67 |
8739 | ssh_pkt_addstring(s->pktout, "ssh-connection"); |
8740 | ssh_pkt_addstring(s->pktout, "gssapi-with-mic"); |
8741 | |
86557057 |
8742 | s->gss_buf.value = (char *)s->pktout->data + micoffset; |
8743 | s->gss_buf.length = s->pktout->length - micoffset; |
42af6a67 |
8744 | |
b3d375b2 |
8745 | s->gsslib->get_mic(s->gsslib, s->gss_ctx, &s->gss_buf, &mic); |
42af6a67 |
8746 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_GSSAPI_MIC); |
8747 | ssh_pkt_addstring_start(s->pktout); |
86557057 |
8748 | ssh_pkt_addstring_data(s->pktout, mic.value, mic.length); |
42af6a67 |
8749 | ssh2_pkt_send(ssh, s->pktout); |
b3d375b2 |
8750 | s->gsslib->free_mic(s->gsslib, &mic); |
42af6a67 |
8751 | |
8752 | s->gotit = FALSE; |
8753 | |
b3d375b2 |
8754 | s->gsslib->release_name(s->gsslib, &s->gss_srv_name); |
8755 | s->gsslib->release_cred(s->gsslib, &s->gss_ctx); |
42af6a67 |
8756 | continue; |
8757 | #endif |
edd0cb8a |
8758 | } else if (s->can_keyb_inter && !s->kbd_inter_refused) { |
8759 | |
8760 | /* |
8761 | * Keyboard-interactive authentication. |
8762 | */ |
edd0cb8a |
8763 | |
edd0cb8a |
8764 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
8765 | |
acab36bc |
8766 | ssh->pkt_actx = SSH2_PKTCTX_KBDINTER; |
edd0cb8a |
8767 | |
8768 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8769 | ssh2_pkt_addstring(s->pktout, ssh->username); |
edd0cb8a |
8770 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
8771 | /* service requested */ |
8772 | ssh2_pkt_addstring(s->pktout, "keyboard-interactive"); |
8773 | /* method */ |
8774 | ssh2_pkt_addstring(s->pktout, ""); /* lang */ |
8775 | ssh2_pkt_addstring(s->pktout, ""); /* submethods */ |
8776 | ssh2_pkt_send(ssh, s->pktout); |
8221906d |
8777 | |
8778 | logevent("Attempting keyboard-interactive authentication"); |
edd0cb8a |
8779 | |
8780 | crWaitUntilV(pktin); |
8781 | if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) { |
8782 | /* Server is not willing to do keyboard-interactive |
b8e3173d |
8783 | * at all (or, bizarrely but legally, accepts the |
8784 | * user without actually issuing any prompts). |
8785 | * Give up on it entirely. */ |
edd0cb8a |
8786 | s->gotit = TRUE; |
edd0cb8a |
8787 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET; |
8788 | s->kbd_inter_refused = TRUE; /* don't try it again */ |
8789 | continue; |
8790 | } |
8791 | |
8792 | /* |
b8e3173d |
8793 | * Loop while the server continues to send INFO_REQUESTs. |
edd0cb8a |
8794 | */ |
b8e3173d |
8795 | while (pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) { |
edd0cb8a |
8796 | |
b8e3173d |
8797 | char *name, *inst, *lang; |
8798 | int name_len, inst_len, lang_len; |
8799 | int i; |
8800 | |
8801 | /* |
8802 | * We've got a fresh USERAUTH_INFO_REQUEST. |
8803 | * Get the preamble and start building a prompt. |
8804 | */ |
8805 | ssh_pkt_getstring(pktin, &name, &name_len); |
8806 | ssh_pkt_getstring(pktin, &inst, &inst_len); |
8807 | ssh_pkt_getstring(pktin, &lang, &lang_len); |
8808 | s->cur_prompt = new_prompts(ssh->frontend); |
8809 | s->cur_prompt->to_server = TRUE; |
edd0cb8a |
8810 | |
b8e3173d |
8811 | /* |
9ea10e78 |
8812 | * Get any prompt(s) from the packet. |
b8e3173d |
8813 | */ |
8814 | s->num_prompts = ssh_pkt_getuint32(pktin); |
8815 | for (i = 0; i < s->num_prompts; i++) { |
8816 | char *prompt; |
8817 | int prompt_len; |
8818 | int echo; |
8819 | static char noprompt[] = |
8820 | "<server failed to send prompt>: "; |
8821 | |
8822 | ssh_pkt_getstring(pktin, &prompt, &prompt_len); |
8823 | echo = ssh2_pkt_getbool(pktin); |
8824 | if (!prompt_len) { |
8825 | prompt = noprompt; |
8826 | prompt_len = lenof(noprompt)-1; |
8827 | } |
8828 | add_prompt(s->cur_prompt, |
8829 | dupprintf("%.*s", prompt_len, prompt), |
b61f81bc |
8830 | echo); |
edd0cb8a |
8831 | } |
b8e3173d |
8832 | |
9874c9d7 |
8833 | if (name_len) { |
8834 | /* FIXME: better prefix to distinguish from |
8835 | * local prompts? */ |
8836 | s->cur_prompt->name = |
8837 | dupprintf("SSH server: %.*s", name_len, name); |
8838 | s->cur_prompt->name_reqd = TRUE; |
8839 | } else { |
8840 | s->cur_prompt->name = |
8841 | dupstr("SSH server authentication"); |
8842 | s->cur_prompt->name_reqd = FALSE; |
8843 | } |
8844 | /* We add a prefix to try to make it clear that a prompt |
8845 | * has come from the server. |
8846 | * FIXME: ugly to print "Using..." in prompt _every_ |
8847 | * time round. Can this be done more subtly? */ |
8848 | /* Special case: for reasons best known to themselves, |
8849 | * some servers send k-i requests with no prompts and |
8850 | * nothing to display. Keep quiet in this case. */ |
8851 | if (s->num_prompts || name_len || inst_len) { |
8852 | s->cur_prompt->instruction = |
8853 | dupprintf("Using keyboard-interactive authentication.%s%.*s", |
8854 | inst_len ? "\n" : "", inst_len, inst); |
8855 | s->cur_prompt->instr_reqd = TRUE; |
8856 | } else { |
8857 | s->cur_prompt->instr_reqd = FALSE; |
8858 | } |
8859 | |
b8e3173d |
8860 | /* |
9ea10e78 |
8861 | * Display any instructions, and get the user's |
8862 | * response(s). |
b8e3173d |
8863 | */ |
9ea10e78 |
8864 | { |
b8e3173d |
8865 | int ret; /* not live over crReturn */ |
8866 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
8867 | while (ret < 0) { |
8868 | ssh->send_ok = 1; |
8869 | crWaitUntilV(!pktin); |
8870 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
8871 | ssh->send_ok = 0; |
8872 | } |
8873 | if (!ret) { |
8874 | /* |
8875 | * Failed to get responses. Terminate. |
8876 | */ |
8877 | free_prompts(s->cur_prompt); |
8878 | ssh_disconnect(ssh, NULL, "Unable to authenticate", |
8879 | SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER, |
8880 | TRUE); |
8881 | crStopV; |
8882 | } |
edd0cb8a |
8883 | } |
b8e3173d |
8884 | |
8885 | /* |
9ea10e78 |
8886 | * Send the response(s) to the server. |
b8e3173d |
8887 | */ |
8888 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
b8e3173d |
8889 | ssh2_pkt_adduint32(s->pktout, s->num_prompts); |
8890 | for (i=0; i < s->num_prompts; i++) { |
8891 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
8892 | ssh2_pkt_addstring(s->pktout, |
8893 | s->cur_prompt->prompts[i]->result); |
8894 | end_log_omission(ssh, s->pktout); |
8895 | } |
18524056 |
8896 | ssh2_pkt_send_with_padding(ssh, s->pktout, 256); |
b8e3173d |
8897 | |
c69784c0 |
8898 | /* |
8899 | * Free the prompts structure from this iteration. |
8900 | * If there's another, a new one will be allocated |
8901 | * when we return to the top of this while loop. |
8902 | */ |
8903 | free_prompts(s->cur_prompt); |
8904 | |
b8e3173d |
8905 | /* |
8906 | * Get the next packet in case it's another |
8907 | * INFO_REQUEST. |
8908 | */ |
8909 | crWaitUntilV(pktin); |
8910 | |
edd0cb8a |
8911 | } |
8912 | |
8913 | /* |
b8e3173d |
8914 | * We should have SUCCESS or FAILURE now. |
edd0cb8a |
8915 | */ |
b8e3173d |
8916 | s->gotit = TRUE; |
edd0cb8a |
8917 | |
8918 | } else if (s->can_passwd) { |
8919 | |
8920 | /* |
8921 | * Plain old password authentication. |
8922 | */ |
8923 | int ret; /* not live over crReturn */ |
e955d348 |
8924 | int changereq_first_time; /* not live over crReturn */ |
edd0cb8a |
8925 | |
acab36bc |
8926 | ssh->pkt_actx = SSH2_PKTCTX_PASSWORD; |
edd0cb8a |
8927 | |
8928 | s->cur_prompt = new_prompts(ssh->frontend); |
8929 | s->cur_prompt->to_server = TRUE; |
8930 | s->cur_prompt->name = dupstr("SSH password"); |
4a693cfc |
8931 | add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ", |
8932 | ssh->username, |
edd0cb8a |
8933 | ssh->savedhost), |
b61f81bc |
8934 | FALSE); |
edd0cb8a |
8935 | |
8936 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
8937 | while (ret < 0) { |
8938 | ssh->send_ok = 1; |
8939 | crWaitUntilV(!pktin); |
8940 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
8941 | ssh->send_ok = 0; |
8942 | } |
8943 | if (!ret) { |
8944 | /* |
8945 | * Failed to get responses. Terminate. |
8946 | */ |
8947 | free_prompts(s->cur_prompt); |
8948 | ssh_disconnect(ssh, NULL, "Unable to authenticate", |
8949 | SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER, |
8950 | TRUE); |
8951 | crStopV; |
8952 | } |
e955d348 |
8953 | /* |
8954 | * Squirrel away the password. (We may need it later if |
8955 | * asked to change it.) |
8956 | */ |
8957 | s->password = dupstr(s->cur_prompt->prompts[0]->result); |
8958 | free_prompts(s->cur_prompt); |
edd0cb8a |
8959 | |
8960 | /* |
8961 | * Send the password packet. |
8962 | * |
95d2d262 |
8963 | * We pad out the password packet to 256 bytes to make |
8964 | * it harder for an attacker to find the length of the |
8965 | * user's password. |
1408a877 |
8966 | * |
95d2d262 |
8967 | * Anyone using a password longer than 256 bytes |
8968 | * probably doesn't have much to worry about from |
1408a877 |
8969 | * people who find out how long their password is! |
65a22376 |
8970 | */ |
ff3187f6 |
8971 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
8972 | ssh2_pkt_addstring(s->pktout, ssh->username); |
edd0cb8a |
8973 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
8974 | /* service requested */ |
ff3187f6 |
8975 | ssh2_pkt_addstring(s->pktout, "password"); |
8976 | ssh2_pkt_addbool(s->pktout, FALSE); |
8977 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
e955d348 |
8978 | ssh2_pkt_addstring(s->pktout, s->password); |
ff3187f6 |
8979 | end_log_omission(ssh, s->pktout); |
18524056 |
8980 | ssh2_pkt_send_with_padding(ssh, s->pktout, 256); |
0d43337a |
8981 | logevent("Sent password"); |
28f4d4f0 |
8982 | s->type = AUTH_TYPE_PASSWORD; |
edd0cb8a |
8983 | |
e955d348 |
8984 | /* |
8985 | * Wait for next packet, in case it's a password change |
8986 | * request. |
8987 | */ |
8988 | crWaitUntilV(pktin); |
8989 | changereq_first_time = TRUE; |
8990 | |
8991 | while (pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) { |
8992 | |
8993 | /* |
8994 | * We're being asked for a new password |
8995 | * (perhaps not for the first time). |
8996 | * Loop until the server accepts it. |
8997 | */ |
8998 | |
8999 | int got_new = FALSE; /* not live over crReturn */ |
9000 | char *prompt; /* not live over crReturn */ |
9001 | int prompt_len; /* not live over crReturn */ |
9002 | |
9003 | { |
9004 | char *msg; |
9005 | if (changereq_first_time) |
9006 | msg = "Server requested password change"; |
9007 | else |
9008 | msg = "Server rejected new password"; |
9009 | logevent(msg); |
9010 | c_write_str(ssh, msg); |
9011 | c_write_str(ssh, "\r\n"); |
9012 | } |
9013 | |
9014 | ssh_pkt_getstring(pktin, &prompt, &prompt_len); |
9015 | |
9016 | s->cur_prompt = new_prompts(ssh->frontend); |
9017 | s->cur_prompt->to_server = TRUE; |
9018 | s->cur_prompt->name = dupstr("New SSH password"); |
9019 | s->cur_prompt->instruction = |
9020 | dupprintf("%.*s", prompt_len, prompt); |
9021 | s->cur_prompt->instr_reqd = TRUE; |
d9add7bc |
9022 | /* |
9023 | * There's no explicit requirement in the protocol |
9024 | * for the "old" passwords in the original and |
9025 | * password-change messages to be the same, and |
9026 | * apparently some Cisco kit supports password change |
9027 | * by the user entering a blank password originally |
9028 | * and the real password subsequently, so, |
9029 | * reluctantly, we prompt for the old password again. |
9030 | * |
9031 | * (On the other hand, some servers don't even bother |
9032 | * to check this field.) |
9033 | */ |
9034 | add_prompt(s->cur_prompt, |
9035 | dupstr("Current password (blank for previously entered password): "), |
b61f81bc |
9036 | FALSE); |
e955d348 |
9037 | add_prompt(s->cur_prompt, dupstr("Enter new password: "), |
b61f81bc |
9038 | FALSE); |
e955d348 |
9039 | add_prompt(s->cur_prompt, dupstr("Confirm new password: "), |
b61f81bc |
9040 | FALSE); |
e955d348 |
9041 | |
9042 | /* |
9043 | * Loop until the user manages to enter the same |
9044 | * password twice. |
9045 | */ |
9046 | while (!got_new) { |
9047 | |
9048 | ret = get_userpass_input(s->cur_prompt, NULL, 0); |
9049 | while (ret < 0) { |
9050 | ssh->send_ok = 1; |
9051 | crWaitUntilV(!pktin); |
9052 | ret = get_userpass_input(s->cur_prompt, in, inlen); |
9053 | ssh->send_ok = 0; |
9054 | } |
9055 | if (!ret) { |
9056 | /* |
9057 | * Failed to get responses. Terminate. |
9058 | */ |
9059 | /* burn the evidence */ |
9060 | free_prompts(s->cur_prompt); |
dfb88efd |
9061 | smemclr(s->password, strlen(s->password)); |
e955d348 |
9062 | sfree(s->password); |
9063 | ssh_disconnect(ssh, NULL, "Unable to authenticate", |
9064 | SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER, |
9065 | TRUE); |
9066 | crStopV; |
9067 | } |
9068 | |
9069 | /* |
d9add7bc |
9070 | * If the user specified a new original password |
9071 | * (IYSWIM), overwrite any previously specified |
9072 | * one. |
9073 | * (A side effect is that the user doesn't have to |
9074 | * re-enter it if they louse up the new password.) |
9075 | */ |
9076 | if (s->cur_prompt->prompts[0]->result[0]) { |
dfb88efd |
9077 | smemclr(s->password, strlen(s->password)); |
d9add7bc |
9078 | /* burn the evidence */ |
9079 | sfree(s->password); |
9080 | s->password = |
9081 | dupstr(s->cur_prompt->prompts[0]->result); |
9082 | } |
9083 | |
9084 | /* |
9085 | * Check the two new passwords match. |
e955d348 |
9086 | */ |
d9add7bc |
9087 | got_new = (strcmp(s->cur_prompt->prompts[1]->result, |
9088 | s->cur_prompt->prompts[2]->result) |
e955d348 |
9089 | == 0); |
9090 | if (!got_new) |
9091 | /* They don't. Silly user. */ |
9092 | c_write_str(ssh, "Passwords do not match\r\n"); |
9093 | |
9094 | } |
9095 | |
9096 | /* |
9097 | * Send the new password (along with the old one). |
9098 | * (see above for padding rationale) |
9099 | */ |
9100 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
4a693cfc |
9101 | ssh2_pkt_addstring(s->pktout, ssh->username); |
e955d348 |
9102 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); |
9103 | /* service requested */ |
9104 | ssh2_pkt_addstring(s->pktout, "password"); |
9105 | ssh2_pkt_addbool(s->pktout, TRUE); |
9106 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
9107 | ssh2_pkt_addstring(s->pktout, s->password); |
9108 | ssh2_pkt_addstring(s->pktout, |
d9add7bc |
9109 | s->cur_prompt->prompts[1]->result); |
e955d348 |
9110 | free_prompts(s->cur_prompt); |
9111 | end_log_omission(ssh, s->pktout); |
18524056 |
9112 | ssh2_pkt_send_with_padding(ssh, s->pktout, 256); |
e955d348 |
9113 | logevent("Sent new password"); |
9114 | |
9115 | /* |
9116 | * Now see what the server has to say about it. |
9117 | * (If it's CHANGEREQ again, it's not happy with the |
9118 | * new password.) |
9119 | */ |
9120 | crWaitUntilV(pktin); |
9121 | changereq_first_time = FALSE; |
9122 | |
9123 | } |
9124 | |
9125 | /* |
9126 | * We need to reexamine the current pktin at the top |
9127 | * of the loop. Either: |
9128 | * - we weren't asked to change password at all, in |
9129 | * which case it's a SUCCESS or FAILURE with the |
9130 | * usual meaning |
9131 | * - we sent a new password, and the server was |
9132 | * either OK with it (SUCCESS or FAILURE w/partial |
9133 | * success) or unhappy with the _old_ password |
9134 | * (FAILURE w/o partial success) |
9135 | * In any of these cases, we go back to the top of |
9136 | * the loop and start again. |
9137 | */ |
9138 | s->gotit = TRUE; |
9139 | |
9140 | /* |
9141 | * We don't need the old password any more, in any |
9142 | * case. Burn the evidence. |
9143 | */ |
dfb88efd |
9144 | smemclr(s->password, strlen(s->password)); |
e955d348 |
9145 | sfree(s->password); |
9146 | |
1408a877 |
9147 | } else { |
854caca5 |
9148 | char *str = dupprintf("No supported authentication methods available" |
9149 | " (server sent: %.*s)", |
9150 | methlen, methods); |
edd0cb8a |
9151 | |
854caca5 |
9152 | ssh_disconnect(ssh, str, |
9e296bfa |
9153 | "No supported authentication methods available", |
9154 | SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, |
9155 | FALSE); |
854caca5 |
9156 | sfree(str); |
9157 | |
7ffdbc1a |
9158 | crStopV; |
edd0cb8a |
9159 | |
65a22376 |
9160 | } |
edd0cb8a |
9161 | |
65a22376 |
9162 | } |
a1a1fae4 |
9163 | } |
6bbce591 |
9164 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = NULL; |
7cca0d81 |
9165 | |
edd0cb8a |
9166 | /* Clear up various bits and pieces from authentication. */ |
9167 | if (s->publickey_blob) { |
9168 | sfree(s->publickey_blob); |
9169 | sfree(s->publickey_comment); |
9170 | } |
94cd7c3a |
9171 | if (s->agent_response) |
9172 | sfree(s->agent_response); |
edd0cb8a |
9173 | |
ab18ccff |
9174 | if (s->userauth_success) { |
9175 | /* |
9176 | * We've just received USERAUTH_SUCCESS, and we haven't sent any |
9177 | * packets since. Signal the transport layer to consider enacting |
9178 | * delayed compression. |
9179 | * |
9180 | * (Relying on we_are_in is not sufficient, as |
9181 | * draft-miller-secsh-compression-delayed is quite clear that it |
9182 | * triggers on USERAUTH_SUCCESS specifically, and we_are_in can |
9183 | * become set for other reasons.) |
9184 | */ |
9185 | do_ssh2_transport(ssh, "enabling delayed compression", -2, NULL); |
9186 | } |
9187 | |
7cca0d81 |
9188 | /* |
a1a1fae4 |
9189 | * Now the connection protocol has started, one way or another. |
7cca0d81 |
9190 | */ |
9191 | |
0ed48730 |
9192 | ssh->channels = newtree234(ssh_channelcmp); |
9193 | |
7cca0d81 |
9194 | /* |
b09eaa88 |
9195 | * Set up handlers for some connection protocol messages, so we |
9196 | * don't have to handle them repeatedly in this coroutine. |
9197 | */ |
9198 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = |
9199 | ssh2_msg_channel_window_adjust; |
51df0ab5 |
9200 | ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = |
9201 | ssh2_msg_global_request; |
b09eaa88 |
9202 | |
9203 | /* |
0ed48730 |
9204 | * Create the main session channel. |
7cca0d81 |
9205 | */ |
4a693cfc |
9206 | if (conf_get_int(ssh->conf, CONF_ssh_no_shell)) { |
feb02b4e |
9207 | ssh->mainchan = NULL; |
4e63d9b2 |
9208 | } else { |
feb02b4e |
9209 | ssh->mainchan = snew(struct ssh_channel); |
9210 | ssh->mainchan->ssh = ssh; |
94bdfe40 |
9211 | ssh2_channel_init(ssh->mainchan); |
feb02b4e |
9212 | |
4e63d9b2 |
9213 | if (*conf_get_str(ssh->conf, CONF_ssh_nc_host)) { |
9214 | /* |
9215 | * Just start a direct-tcpip channel and use it as the main |
9216 | * channel. |
9217 | */ |
9218 | ssh_send_port_open(ssh->mainchan, |
9219 | conf_get_str(ssh->conf, CONF_ssh_nc_host), |
9220 | conf_get_int(ssh->conf, CONF_ssh_nc_port), |
9221 | "main channel"); |
9222 | ssh->ncmode = TRUE; |
9223 | } else { |
9224 | s->pktout = ssh2_chanopen_init(ssh->mainchan, "session"); |
9225 | logevent("Opening session as main channel"); |
9226 | ssh2_pkt_send(ssh, s->pktout); |
9227 | ssh->ncmode = FALSE; |
feb02b4e |
9228 | } |
ff3187f6 |
9229 | crWaitUntilV(pktin); |
9230 | if (pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
4e63d9b2 |
9231 | bombout(("Server refused to open channel")); |
0ed48730 |
9232 | crStopV; |
9233 | /* FIXME: error data comes back in FAILURE packet */ |
9234 | } |
ff3187f6 |
9235 | if (ssh_pkt_getuint32(pktin) != ssh->mainchan->localid) { |
0ed48730 |
9236 | bombout(("Server's channel confirmation cited wrong channel")); |
9237 | crStopV; |
9238 | } |
ff3187f6 |
9239 | ssh->mainchan->remoteid = ssh_pkt_getuint32(pktin); |
64d6ff88 |
9240 | ssh->mainchan->halfopen = FALSE; |
0ed48730 |
9241 | ssh->mainchan->type = CHAN_MAINSESSION; |
ff3187f6 |
9242 | ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(pktin); |
9243 | ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin); |
0ed48730 |
9244 | add234(ssh->channels, ssh->mainchan); |
62638676 |
9245 | update_specials_menu(ssh->frontend); |
4e63d9b2 |
9246 | logevent("Opened main channel"); |
feb02b4e |
9247 | } |
7cca0d81 |
9248 | |
9249 | /* |
51df0ab5 |
9250 | * Now we have a channel, make dispatch table entries for |
9251 | * general channel-based messages. |
9252 | */ |
9253 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = |
9254 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = |
9255 | ssh2_msg_channel_data; |
9256 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_channel_eof; |
9257 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_channel_close; |
9258 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = |
9259 | ssh2_msg_channel_open_confirmation; |
9260 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = |
9261 | ssh2_msg_channel_open_failure; |
9262 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] = |
9263 | ssh2_msg_channel_request; |
9264 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = |
9265 | ssh2_msg_channel_open; |
ebbc254d |
9266 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = ssh2_msg_channel_response; |
9267 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = ssh2_msg_channel_response; |
9268 | |
51df0ab5 |
9269 | |
4a693cfc |
9270 | if (ssh->mainchan && conf_get_int(ssh->conf, CONF_ssh_simple)) { |
3361b80a |
9271 | /* |
9272 | * This message indicates to the server that we promise |
9273 | * not to try to run any other channel in parallel with |
9274 | * this one, so it's safe for it to advertise a very large |
9275 | * window and leave the flow control to TCP. |
9276 | */ |
134f5ece |
9277 | s->pktout = ssh2_chanreq_init(ssh->mainchan, |
9278 | "simple@putty.projects.tartarus.org", |
9279 | NULL, NULL); |
3361b80a |
9280 | ssh2_pkt_send(ssh, s->pktout); |
9281 | } |
9282 | |
51df0ab5 |
9283 | /* |
c99deca4 |
9284 | * Enable port forwardings. |
9285 | */ |
9286 | ssh_setup_portfwd(ssh, ssh->conf); |
9287 | |
e03299cb |
9288 | if (ssh->mainchan && !ssh->ncmode) { |
9289 | /* |
9290 | * Send the CHANNEL_REQUESTS for the main session channel. |
9291 | * Each one is handled by its own little asynchronous |
9292 | * co-routine. |
9293 | */ |
c99deca4 |
9294 | |
e03299cb |
9295 | /* Potentially enable X11 forwarding. */ |
9296 | if (conf_get_int(ssh->conf, CONF_x11_forward) && |
9297 | (ssh->x11disp = |
9298 | x11_setup_display(conf_get_str(ssh->conf, CONF_x11_display), |
9299 | conf_get_int(ssh->conf, CONF_x11_auth), |
9300 | ssh->conf))) |
9301 | ssh2_setup_x11(ssh->mainchan, NULL, NULL); |
bc240b21 |
9302 | |
e03299cb |
9303 | /* Potentially enable agent forwarding. */ |
9304 | if (conf_get_int(ssh->conf, CONF_agentfwd) && agent_exists()) |
9305 | ssh2_setup_agent(ssh->mainchan, NULL, NULL); |
36c2a3e9 |
9306 | |
e03299cb |
9307 | /* Now allocate a pty for the session. */ |
9308 | if (!conf_get_int(ssh->conf, CONF_nopty)) |
9309 | ssh2_setup_pty(ssh->mainchan, NULL, NULL); |
7cca0d81 |
9310 | |
e03299cb |
9311 | /* Send environment variables. */ |
9312 | ssh2_setup_env(ssh->mainchan, NULL, NULL); |
73feed4f |
9313 | |
e03299cb |
9314 | /* |
9315 | * Start a shell or a remote command. We may have to attempt |
9316 | * this twice if the config data has provided a second choice |
9317 | * of command. |
9318 | */ |
9319 | while (1) { |
9320 | int subsys; |
9321 | char *cmd; |
fd5e5847 |
9322 | |
e03299cb |
9323 | if (ssh->fallback_cmd) { |
9324 | subsys = conf_get_int(ssh->conf, CONF_ssh_subsys2); |
9325 | cmd = conf_get_str(ssh->conf, CONF_remote_cmd2); |
9326 | } else { |
9327 | subsys = conf_get_int(ssh->conf, CONF_ssh_subsys); |
9328 | cmd = conf_get_str(ssh->conf, CONF_remote_cmd); |
9329 | } |
fd5e5847 |
9330 | |
e03299cb |
9331 | if (subsys) { |
9332 | s->pktout = ssh2_chanreq_init(ssh->mainchan, "subsystem", |
9333 | ssh2_response_authconn, NULL); |
9334 | ssh2_pkt_addstring(s->pktout, cmd); |
9335 | } else if (*cmd) { |
9336 | s->pktout = ssh2_chanreq_init(ssh->mainchan, "exec", |
9337 | ssh2_response_authconn, NULL); |
9338 | ssh2_pkt_addstring(s->pktout, cmd); |
9339 | } else { |
9340 | s->pktout = ssh2_chanreq_init(ssh->mainchan, "shell", |
9341 | ssh2_response_authconn, NULL); |
9342 | } |
9343 | ssh2_pkt_send(ssh, s->pktout); |
b09eaa88 |
9344 | |
e03299cb |
9345 | crWaitUntilV(pktin); |
b09eaa88 |
9346 | |
e03299cb |
9347 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
9348 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
9349 | bombout(("Unexpected response to shell/command request:" |
9350 | " packet type %d", pktin->type)); |
9351 | crStopV; |
9352 | } |
9353 | /* |
9354 | * We failed to start the command. If this is the |
9355 | * fallback command, we really are finished; if it's |
9356 | * not, and if the fallback command exists, try falling |
9357 | * back to it before complaining. |
9358 | */ |
9359 | if (!ssh->fallback_cmd && |
9360 | *conf_get_str(ssh->conf, CONF_remote_cmd2)) { |
9361 | logevent("Primary command failed; attempting fallback"); |
9362 | ssh->fallback_cmd = TRUE; |
9363 | continue; |
9364 | } |
9365 | bombout(("Server refused to start a shell/command")); |
7ffdbc1a |
9366 | crStopV; |
e03299cb |
9367 | } else { |
9368 | logevent("Started a shell/command"); |
fd5e5847 |
9369 | } |
e03299cb |
9370 | break; |
32874aea |
9371 | } |
e03299cb |
9372 | } else { |
9373 | ssh->editing = ssh->echoing = TRUE; |
7cca0d81 |
9374 | } |
9375 | |
51470298 |
9376 | ssh->state = SSH_STATE_SESSION; |
9377 | if (ssh->size_needed) |
9378 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
9379 | if (ssh->eof_needed) |
9380 | ssh_special(ssh, TS_EOF); |
6e48c3fe |
9381 | |
7cca0d81 |
9382 | /* |
9383 | * Transfer data! |
9384 | */ |
b9d7bcad |
9385 | if (ssh->ldisc) |
9386 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
0ed48730 |
9387 | if (ssh->mainchan) |
9388 | ssh->send_ok = 1; |
7cca0d81 |
9389 | while (1) { |
e5574168 |
9390 | crReturnV; |
51470298 |
9391 | s->try_send = FALSE; |
ff3187f6 |
9392 | if (pktin) { |
2b7540a7 |
9393 | |
51df0ab5 |
9394 | /* |
9395 | * _All_ the connection-layer packets we expect to |
9396 | * receive are now handled by the dispatch table. |
9397 | * Anything that reaches here must be bogus. |
9398 | */ |
32874aea |
9399 | |
51df0ab5 |
9400 | bombout(("Strange packet received: type %d", pktin->type)); |
9401 | crStopV; |
0ed48730 |
9402 | } else if (ssh->mainchan) { |
32874aea |
9403 | /* |
9404 | * We have spare data. Add it to the channel buffer. |
9405 | */ |
d8baa528 |
9406 | ssh2_add_channel_data(ssh->mainchan, (char *)in, inlen); |
51470298 |
9407 | s->try_send = TRUE; |
32874aea |
9408 | } |
51470298 |
9409 | if (s->try_send) { |
32874aea |
9410 | int i; |
9411 | struct ssh_channel *c; |
9412 | /* |
9413 | * Try to send data on all channels if we can. |
9414 | */ |
1bfc7e93 |
9415 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) |
78280721 |
9416 | ssh2_try_send_and_unthrottle(ssh, c); |
7cca0d81 |
9417 | } |
e5574168 |
9418 | } |
9419 | |
9420 | crFinishV; |
9421 | } |
9422 | |
9423 | /* |
2e85c969 |
9424 | * Handlers for SSH-2 messages that might arrive at any moment. |
b09eaa88 |
9425 | */ |
409bfa77 |
9426 | static void ssh2_msg_disconnect(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
9427 | { |
9428 | /* log reason code in disconnect message */ |
9429 | char *buf, *msg; |
60860bc3 |
9430 | int reason, msglen; |
b09eaa88 |
9431 | |
9432 | reason = ssh_pkt_getuint32(pktin); |
9433 | ssh_pkt_getstring(pktin, &msg, &msglen); |
9434 | |
9435 | if (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) { |
9436 | buf = dupprintf("Received disconnect message (%s)", |
9437 | ssh2_disconnect_reasons[reason]); |
9438 | } else { |
9439 | buf = dupprintf("Received disconnect message (unknown" |
9440 | " type %d)", reason); |
9441 | } |
9442 | logevent(buf); |
9443 | sfree(buf); |
60860bc3 |
9444 | buf = dupprintf("Disconnection message text: %.*s", |
9445 | msglen, msg); |
b09eaa88 |
9446 | logevent(buf); |
60860bc3 |
9447 | bombout(("Server sent disconnect message\ntype %d (%s):\n\"%.*s\"", |
b09eaa88 |
9448 | reason, |
9449 | (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) ? |
9450 | ssh2_disconnect_reasons[reason] : "unknown", |
60860bc3 |
9451 | msglen, msg)); |
b09eaa88 |
9452 | sfree(buf); |
9453 | } |
9454 | |
409bfa77 |
9455 | static void ssh2_msg_debug(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
9456 | { |
9457 | /* log the debug message */ |
fb983202 |
9458 | char *msg; |
b09eaa88 |
9459 | int msglen; |
b09eaa88 |
9460 | |
3ab79841 |
9461 | /* XXX maybe we should actually take notice of the return value */ |
9462 | ssh2_pkt_getbool(pktin); |
b09eaa88 |
9463 | ssh_pkt_getstring(pktin, &msg, &msglen); |
9464 | |
fb983202 |
9465 | logeventf(ssh, "Remote debug message: %.*s", msglen, msg); |
b09eaa88 |
9466 | } |
9467 | |
bc074147 |
9468 | static void ssh2_msg_transport(Ssh ssh, struct Packet *pktin) |
9469 | { |
9470 | do_ssh2_transport(ssh, NULL, 0, pktin); |
9471 | } |
9472 | |
9473 | /* |
9474 | * Called if we receive a packet that isn't allowed by the protocol. |
9475 | * This only applies to packets whose meaning PuTTY understands. |
9476 | * Entirely unknown packets are handled below. |
9477 | */ |
9478 | static void ssh2_msg_unexpected(Ssh ssh, struct Packet *pktin) |
9479 | { |
9480 | char *buf = dupprintf("Server protocol violation: unexpected %s packet", |
9481 | ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, |
9482 | pktin->type)); |
9483 | ssh_disconnect(ssh, NULL, buf, SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE); |
1418b055 |
9484 | sfree(buf); |
bc074147 |
9485 | } |
9486 | |
409bfa77 |
9487 | static void ssh2_msg_something_unimplemented(Ssh ssh, struct Packet *pktin) |
b09eaa88 |
9488 | { |
9489 | struct Packet *pktout; |
9490 | pktout = ssh2_pkt_init(SSH2_MSG_UNIMPLEMENTED); |
9491 | ssh2_pkt_adduint32(pktout, pktin->sequence); |
9492 | /* |
9493 | * UNIMPLEMENTED messages MUST appear in the same order as the |
9494 | * messages they respond to. Hence, never queue them. |
9495 | */ |
9496 | ssh2_pkt_send_noqueue(ssh, pktout); |
9497 | } |
9498 | |
9499 | /* |
2e85c969 |
9500 | * Handle the top-level SSH-2 protocol. |
7cca0d81 |
9501 | */ |
b09eaa88 |
9502 | static void ssh2_protocol_setup(Ssh ssh) |
9503 | { |
9504 | int i; |
9505 | |
9506 | /* |
9507 | * Most messages cause SSH2_MSG_UNIMPLEMENTED. |
9508 | */ |
9509 | for (i = 0; i < 256; i++) |
9510 | ssh->packet_dispatch[i] = ssh2_msg_something_unimplemented; |
9511 | |
9512 | /* |
bc074147 |
9513 | * Initially, we only accept transport messages (and a few generic |
9514 | * ones). do_ssh2_authconn will add more when it starts. |
9515 | * Messages that are understood but not currently acceptable go to |
9516 | * ssh2_msg_unexpected. |
b09eaa88 |
9517 | */ |
bc074147 |
9518 | ssh->packet_dispatch[SSH2_MSG_UNIMPLEMENTED] = ssh2_msg_unexpected; |
9519 | ssh->packet_dispatch[SSH2_MSG_SERVICE_REQUEST] = ssh2_msg_unexpected; |
9520 | ssh->packet_dispatch[SSH2_MSG_SERVICE_ACCEPT] = ssh2_msg_unexpected; |
9521 | ssh->packet_dispatch[SSH2_MSG_KEXINIT] = ssh2_msg_transport; |
9522 | ssh->packet_dispatch[SSH2_MSG_NEWKEYS] = ssh2_msg_transport; |
9523 | ssh->packet_dispatch[SSH2_MSG_KEXDH_INIT] = ssh2_msg_transport; |
9524 | ssh->packet_dispatch[SSH2_MSG_KEXDH_REPLY] = ssh2_msg_transport; |
9525 | /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REQUEST] = ssh2_msg_transport; duplicate case value */ |
9526 | /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_GROUP] = ssh2_msg_transport; duplicate case value */ |
9527 | ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_INIT] = ssh2_msg_transport; |
9528 | ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REPLY] = ssh2_msg_transport; |
9529 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_REQUEST] = ssh2_msg_unexpected; |
9530 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_FAILURE] = ssh2_msg_unexpected; |
9531 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_SUCCESS] = ssh2_msg_unexpected; |
9532 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = ssh2_msg_unexpected; |
9533 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_PK_OK] = ssh2_msg_unexpected; |
9534 | /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ] = ssh2_msg_unexpected; duplicate case value */ |
9535 | /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_REQUEST] = ssh2_msg_unexpected; duplicate case value */ |
9536 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_RESPONSE] = ssh2_msg_unexpected; |
9537 | ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = ssh2_msg_unexpected; |
9538 | ssh->packet_dispatch[SSH2_MSG_REQUEST_SUCCESS] = ssh2_msg_unexpected; |
9539 | ssh->packet_dispatch[SSH2_MSG_REQUEST_FAILURE] = ssh2_msg_unexpected; |
9540 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = ssh2_msg_unexpected; |
9541 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = ssh2_msg_unexpected; |
9542 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = ssh2_msg_unexpected; |
9543 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = ssh2_msg_unexpected; |
9544 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = ssh2_msg_unexpected; |
9545 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = ssh2_msg_unexpected; |
9546 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_unexpected; |
9547 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_unexpected; |
9548 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] = ssh2_msg_unexpected; |
9549 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = ssh2_msg_unexpected; |
9550 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = ssh2_msg_unexpected; |
b09eaa88 |
9551 | |
9552 | /* |
bc074147 |
9553 | * These messages have a special handler from the start. |
b09eaa88 |
9554 | */ |
9555 | ssh->packet_dispatch[SSH2_MSG_DISCONNECT] = ssh2_msg_disconnect; |
2e85c969 |
9556 | ssh->packet_dispatch[SSH2_MSG_IGNORE] = ssh_msg_ignore; /* shared with SSH-1 */ |
b09eaa88 |
9557 | ssh->packet_dispatch[SSH2_MSG_DEBUG] = ssh2_msg_debug; |
9558 | } |
9559 | |
d719927e |
9560 | static void ssh2_timer(void *ctx, unsigned long now) |
9442dd57 |
9561 | { |
9562 | Ssh ssh = (Ssh)ctx; |
9563 | |
ecbb0000 |
9564 | if (ssh->state == SSH_STATE_CLOSED) |
9565 | return; |
9566 | |
4a693cfc |
9567 | if (!ssh->kex_in_progress && conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0 && |
d719927e |
9568 | now == ssh->next_rekey) { |
f382c87d |
9569 | do_ssh2_transport(ssh, "timeout", -1, NULL); |
9442dd57 |
9570 | } |
9571 | } |
9572 | |
1c1a7262 |
9573 | static void ssh2_protocol(Ssh ssh, void *vin, int inlen, |
ff3187f6 |
9574 | struct Packet *pktin) |
7cca0d81 |
9575 | { |
1c1a7262 |
9576 | unsigned char *in = (unsigned char *)vin; |
b09eaa88 |
9577 | if (ssh->state == SSH_STATE_CLOSED) |
9578 | return; |
9579 | |
9442dd57 |
9580 | if (pktin) { |
9581 | ssh->incoming_data_size += pktin->encrypted_len; |
9582 | if (!ssh->kex_in_progress && |
d57f70af |
9583 | ssh->max_data_size != 0 && |
9584 | ssh->incoming_data_size > ssh->max_data_size) |
f382c87d |
9585 | do_ssh2_transport(ssh, "too much data received", -1, NULL); |
9442dd57 |
9586 | } |
9587 | |
bc074147 |
9588 | if (pktin) |
b09eaa88 |
9589 | ssh->packet_dispatch[pktin->type](ssh, pktin); |
bc074147 |
9590 | else if (!ssh->protocol_initial_phase_done) |
9591 | do_ssh2_transport(ssh, in, inlen, pktin); |
9592 | else |
b09eaa88 |
9593 | do_ssh2_authconn(ssh, in, inlen, pktin); |
7cca0d81 |
9594 | } |
9595 | |
4a693cfc |
9596 | static void ssh_cache_conf_values(Ssh ssh) |
9597 | { |
9598 | ssh->logomitdata = conf_get_int(ssh->conf, CONF_logomitdata); |
9599 | } |
9600 | |
7cca0d81 |
9601 | /* |
8df7a775 |
9602 | * Called to set up the connection. |
374330e2 |
9603 | * |
9604 | * Returns an error message, or NULL on success. |
374330e2 |
9605 | */ |
cbe2d68f |
9606 | static const char *ssh_init(void *frontend_handle, void **backend_handle, |
4a693cfc |
9607 | Conf *conf, char *host, int port, char **realhost, |
9608 | int nodelay, int keepalive) |
32874aea |
9609 | { |
cbe2d68f |
9610 | const char *p; |
51470298 |
9611 | Ssh ssh; |
9612 | |
3d88e64d |
9613 | ssh = snew(struct ssh_tag); |
4a693cfc |
9614 | ssh->conf = conf_copy(conf); |
9615 | ssh_cache_conf_values(ssh); |
125105d1 |
9616 | ssh->version = 0; /* when not ready yet */ |
51470298 |
9617 | ssh->s = NULL; |
9618 | ssh->cipher = NULL; |
371e569c |
9619 | ssh->v1_cipher_ctx = NULL; |
0183b242 |
9620 | ssh->crcda_ctx = NULL; |
51470298 |
9621 | ssh->cscipher = NULL; |
371e569c |
9622 | ssh->cs_cipher_ctx = NULL; |
51470298 |
9623 | ssh->sccipher = NULL; |
371e569c |
9624 | ssh->sc_cipher_ctx = NULL; |
51470298 |
9625 | ssh->csmac = NULL; |
a8327734 |
9626 | ssh->cs_mac_ctx = NULL; |
51470298 |
9627 | ssh->scmac = NULL; |
e0e1a00d |
9628 | ssh->sc_mac_ctx = NULL; |
51470298 |
9629 | ssh->cscomp = NULL; |
5366aed8 |
9630 | ssh->cs_comp_ctx = NULL; |
51470298 |
9631 | ssh->sccomp = NULL; |
5366aed8 |
9632 | ssh->sc_comp_ctx = NULL; |
51470298 |
9633 | ssh->kex = NULL; |
389aa499 |
9634 | ssh->kex_ctx = NULL; |
51470298 |
9635 | ssh->hostkey = NULL; |
9636 | ssh->exitcode = -1; |
ac934965 |
9637 | ssh->close_expected = FALSE; |
9e296bfa |
9638 | ssh->clean_exit = FALSE; |
51470298 |
9639 | ssh->state = SSH_STATE_PREPACKET; |
9640 | ssh->size_needed = FALSE; |
9641 | ssh->eof_needed = FALSE; |
b9d7bcad |
9642 | ssh->ldisc = NULL; |
a8327734 |
9643 | ssh->logctx = NULL; |
51470298 |
9644 | ssh->deferred_send_data = NULL; |
9645 | ssh->deferred_len = 0; |
9646 | ssh->deferred_size = 0; |
9647 | ssh->fallback_cmd = 0; |
acab36bc |
9648 | ssh->pkt_kctx = SSH2_PKTCTX_NOKEX; |
9649 | ssh->pkt_actx = SSH2_PKTCTX_NOAUTH; |
8def70c3 |
9650 | ssh->x11disp = NULL; |
be738459 |
9651 | ssh->v1_compressing = FALSE; |
51470298 |
9652 | ssh->v2_outgoing_sequence = 0; |
9653 | ssh->ssh1_rdpkt_crstate = 0; |
9654 | ssh->ssh2_rdpkt_crstate = 0; |
51470298 |
9655 | ssh->ssh_gotdata_crstate = 0; |
b09eaa88 |
9656 | ssh->do_ssh1_connection_crstate = 0; |
51470298 |
9657 | ssh->do_ssh_init_state = NULL; |
9658 | ssh->do_ssh1_login_state = NULL; |
9659 | ssh->do_ssh2_transport_state = NULL; |
9660 | ssh->do_ssh2_authconn_state = NULL; |
4320baf7 |
9661 | ssh->v_c = NULL; |
9662 | ssh->v_s = NULL; |
6571dbfd |
9663 | ssh->mainchan = NULL; |
968d2d92 |
9664 | ssh->throttled_all = 0; |
9665 | ssh->v1_stdout_throttling = 0; |
590f6a5f |
9666 | ssh->queue = NULL; |
9667 | ssh->queuelen = ssh->queuesize = 0; |
9668 | ssh->queueing = FALSE; |
06fadff5 |
9669 | ssh->qhead = ssh->qtail = NULL; |
e13bba36 |
9670 | ssh->deferred_rekey_reason = NULL; |
3d9449a1 |
9671 | bufchain_init(&ssh->queued_incoming_data); |
9672 | ssh->frozen = FALSE; |
4a693cfc |
9673 | ssh->username = NULL; |
bc06669b |
9674 | ssh->sent_console_eof = FALSE; |
4a202fc6 |
9675 | ssh->got_pty = FALSE; |
51470298 |
9676 | |
9677 | *backend_handle = ssh; |
32874aea |
9678 | |
8f203108 |
9679 | #ifdef MSCRYPTOAPI |
32874aea |
9680 | if (crypto_startup() == 0) |
8f203108 |
9681 | return "Microsoft high encryption pack not installed!"; |
9682 | #endif |
374330e2 |
9683 | |
51470298 |
9684 | ssh->frontend = frontend_handle; |
4a693cfc |
9685 | ssh->term_width = conf_get_int(ssh->conf, CONF_width); |
9686 | ssh->term_height = conf_get_int(ssh->conf, CONF_height); |
887035a5 |
9687 | |
fabd1805 |
9688 | ssh->channels = NULL; |
9689 | ssh->rportfwds = NULL; |
f47a5ffb |
9690 | ssh->portfwds = NULL; |
fabd1805 |
9691 | |
51470298 |
9692 | ssh->send_ok = 0; |
9693 | ssh->editing = 0; |
9694 | ssh->echoing = 0; |
ff68564b |
9695 | ssh->conn_throttle_count = 0; |
51470298 |
9696 | ssh->overall_bufsize = 0; |
9697 | ssh->fallback_cmd = 0; |
8df7a775 |
9698 | |
3648d4c5 |
9699 | ssh->protocol = NULL; |
9700 | |
b09eaa88 |
9701 | ssh->protocol_initial_phase_done = FALSE; |
9702 | |
39934deb |
9703 | ssh->pinger = NULL; |
9704 | |
9442dd57 |
9705 | ssh->incoming_data_size = ssh->outgoing_data_size = |
9706 | ssh->deferred_data_size = 0L; |
4a693cfc |
9707 | ssh->max_data_size = parse_blocksize(conf_get_str(ssh->conf, |
9708 | CONF_ssh_rekey_data)); |
9442dd57 |
9709 | ssh->kex_in_progress = FALSE; |
9710 | |
1e00c92b |
9711 | #ifndef NO_GSSAPI |
9712 | ssh->gsslibs = NULL; |
9713 | #endif |
9714 | |
79bf227b |
9715 | p = connect_to_host(ssh, host, port, realhost, nodelay, keepalive); |
fb09bf1c |
9716 | if (p != NULL) |
9717 | return p; |
374330e2 |
9718 | |
5d17ccfc |
9719 | random_ref(); |
9720 | |
374330e2 |
9721 | return NULL; |
9722 | } |
9723 | |
fabd1805 |
9724 | static void ssh_free(void *handle) |
9725 | { |
9726 | Ssh ssh = (Ssh) handle; |
9727 | struct ssh_channel *c; |
9728 | struct ssh_rportfwd *pf; |
9729 | |
9730 | if (ssh->v1_cipher_ctx) |
9731 | ssh->cipher->free_context(ssh->v1_cipher_ctx); |
9732 | if (ssh->cs_cipher_ctx) |
9733 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
9734 | if (ssh->sc_cipher_ctx) |
9735 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
9736 | if (ssh->cs_mac_ctx) |
9737 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
9738 | if (ssh->sc_mac_ctx) |
9739 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
29b1d0b3 |
9740 | if (ssh->cs_comp_ctx) { |
9741 | if (ssh->cscomp) |
9742 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
9743 | else |
9744 | zlib_compress_cleanup(ssh->cs_comp_ctx); |
9745 | } |
9746 | if (ssh->sc_comp_ctx) { |
9747 | if (ssh->sccomp) |
9748 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
9749 | else |
9750 | zlib_decompress_cleanup(ssh->sc_comp_ctx); |
9751 | } |
fabd1805 |
9752 | if (ssh->kex_ctx) |
9753 | dh_cleanup(ssh->kex_ctx); |
9754 | sfree(ssh->savedhost); |
9755 | |
590f6a5f |
9756 | while (ssh->queuelen-- > 0) |
9757 | ssh_free_packet(ssh->queue[ssh->queuelen]); |
9758 | sfree(ssh->queue); |
9759 | |
06fadff5 |
9760 | while (ssh->qhead) { |
9761 | struct queued_handler *qh = ssh->qhead; |
9762 | ssh->qhead = qh->next; |
8a9ec857 |
9763 | sfree(qh); |
06fadff5 |
9764 | } |
9765 | ssh->qhead = ssh->qtail = NULL; |
9766 | |
fabd1805 |
9767 | if (ssh->channels) { |
9768 | while ((c = delpos234(ssh->channels, 0)) != NULL) { |
9769 | switch (c->type) { |
9770 | case CHAN_X11: |
9771 | if (c->u.x11.s != NULL) |
9772 | x11_close(c->u.x11.s); |
9773 | break; |
9774 | case CHAN_SOCKDATA: |
409eca86 |
9775 | case CHAN_SOCKDATA_DORMANT: |
fabd1805 |
9776 | if (c->u.pfd.s != NULL) |
9777 | pfd_close(c->u.pfd.s); |
9778 | break; |
9779 | } |
3620f307 |
9780 | if (ssh->version == 2) { |
9781 | struct outstanding_channel_request *ocr, *nocr; |
9782 | ocr = c->v.v2.chanreq_head; |
9783 | while (ocr) { |
9784 | ocr->handler(c, NULL, ocr->ctx); |
9785 | nocr = ocr->next; |
9786 | sfree(ocr); |
9787 | ocr = nocr; |
9788 | } |
9789 | bufchain_clear(&c->v.v2.outbuffer); |
9790 | } |
fabd1805 |
9791 | sfree(c); |
9792 | } |
9793 | freetree234(ssh->channels); |
cdb52705 |
9794 | ssh->channels = NULL; |
fabd1805 |
9795 | } |
9796 | |
9797 | if (ssh->rportfwds) { |
9798 | while ((pf = delpos234(ssh->rportfwds, 0)) != NULL) |
8aea57fd |
9799 | free_rportfwd(pf); |
fabd1805 |
9800 | freetree234(ssh->rportfwds); |
cdb52705 |
9801 | ssh->rportfwds = NULL; |
fabd1805 |
9802 | } |
9803 | sfree(ssh->deferred_send_data); |
8def70c3 |
9804 | if (ssh->x11disp) |
9805 | x11_free_display(ssh->x11disp); |
fabd1805 |
9806 | sfree(ssh->do_ssh_init_state); |
9807 | sfree(ssh->do_ssh1_login_state); |
9808 | sfree(ssh->do_ssh2_transport_state); |
9809 | sfree(ssh->do_ssh2_authconn_state); |
4320baf7 |
9810 | sfree(ssh->v_c); |
9811 | sfree(ssh->v_s); |
42af6a67 |
9812 | sfree(ssh->fullhostname); |
679539d7 |
9813 | if (ssh->crcda_ctx) { |
9814 | crcda_free_context(ssh->crcda_ctx); |
9815 | ssh->crcda_ctx = NULL; |
9816 | } |
fabd1805 |
9817 | if (ssh->s) |
ac934965 |
9818 | ssh_do_close(ssh, TRUE); |
9442dd57 |
9819 | expire_timer_context(ssh); |
39934deb |
9820 | if (ssh->pinger) |
9821 | pinger_free(ssh->pinger); |
3d9449a1 |
9822 | bufchain_clear(&ssh->queued_incoming_data); |
4a693cfc |
9823 | sfree(ssh->username); |
9824 | conf_free(ssh->conf); |
1e00c92b |
9825 | #ifndef NO_GSSAPI |
9826 | if (ssh->gsslibs) |
9827 | ssh_gss_cleanup(ssh->gsslibs); |
9828 | #endif |
ee50e8b6 |
9829 | sfree(ssh); |
5d17ccfc |
9830 | |
9831 | random_unref(); |
fabd1805 |
9832 | } |
9833 | |
374330e2 |
9834 | /* |
86916870 |
9835 | * Reconfigure the SSH backend. |
86916870 |
9836 | */ |
4a693cfc |
9837 | static void ssh_reconfig(void *handle, Conf *conf) |
86916870 |
9838 | { |
9839 | Ssh ssh = (Ssh) handle; |
e13bba36 |
9840 | char *rekeying = NULL, rekey_mandatory = FALSE; |
e6c1536e |
9841 | unsigned long old_max_data_size; |
4a693cfc |
9842 | int i, rekey_time; |
e6c1536e |
9843 | |
4a693cfc |
9844 | pinger_reconfig(ssh->pinger, ssh->conf, conf); |
3b6606c7 |
9845 | if (ssh->portfwds) |
4a693cfc |
9846 | ssh_setup_portfwd(ssh, conf); |
e6c1536e |
9847 | |
4a693cfc |
9848 | rekey_time = conf_get_int(conf, CONF_ssh_rekey_time); |
9849 | if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != rekey_time && |
9850 | rekey_time != 0) { |
d719927e |
9851 | unsigned long new_next = ssh->last_rekey + rekey_time*60*TICKSPERSEC; |
9852 | unsigned long now = GETTICKCOUNT(); |
e6c1536e |
9853 | |
d719927e |
9854 | if (now - ssh->last_rekey > rekey_time*60*TICKSPERSEC) { |
f382c87d |
9855 | rekeying = "timeout shortened"; |
e6c1536e |
9856 | } else { |
9857 | ssh->next_rekey = schedule_timer(new_next - now, ssh2_timer, ssh); |
9858 | } |
9859 | } |
9860 | |
9861 | old_max_data_size = ssh->max_data_size; |
4a693cfc |
9862 | ssh->max_data_size = parse_blocksize(conf_get_str(ssh->conf, |
9863 | CONF_ssh_rekey_data)); |
e6c1536e |
9864 | if (old_max_data_size != ssh->max_data_size && |
9865 | ssh->max_data_size != 0) { |
9866 | if (ssh->outgoing_data_size > ssh->max_data_size || |
9867 | ssh->incoming_data_size > ssh->max_data_size) |
f382c87d |
9868 | rekeying = "data limit lowered"; |
e6c1536e |
9869 | } |
9870 | |
4a693cfc |
9871 | if (conf_get_int(ssh->conf, CONF_compression) != |
9872 | conf_get_int(conf, CONF_compression)) { |
f382c87d |
9873 | rekeying = "compression setting changed"; |
e13bba36 |
9874 | rekey_mandatory = TRUE; |
9875 | } |
9876 | |
4a693cfc |
9877 | for (i = 0; i < CIPHER_MAX; i++) |
9878 | if (conf_get_int_int(ssh->conf, CONF_ssh_cipherlist, i) != |
9879 | conf_get_int_int(conf, CONF_ssh_cipherlist, i)) { |
9880 | rekeying = "cipher settings changed"; |
9881 | rekey_mandatory = TRUE; |
9882 | } |
9883 | if (conf_get_int(ssh->conf, CONF_ssh2_des_cbc) != |
9884 | conf_get_int(conf, CONF_ssh2_des_cbc)) { |
f382c87d |
9885 | rekeying = "cipher settings changed"; |
e13bba36 |
9886 | rekey_mandatory = TRUE; |
e6c1536e |
9887 | } |
9888 | |
4a693cfc |
9889 | conf_free(ssh->conf); |
9890 | ssh->conf = conf_copy(conf); |
9891 | ssh_cache_conf_values(ssh); |
e13bba36 |
9892 | |
9893 | if (rekeying) { |
9894 | if (!ssh->kex_in_progress) { |
9895 | do_ssh2_transport(ssh, rekeying, -1, NULL); |
9896 | } else if (rekey_mandatory) { |
9897 | ssh->deferred_rekey_reason = rekeying; |
9898 | } |
9899 | } |
86916870 |
9900 | } |
9901 | |
9902 | /* |
86dc445a |
9903 | * Called to send data down the SSH connection. |
374330e2 |
9904 | */ |
51470298 |
9905 | static int ssh_send(void *handle, char *buf, int len) |
32874aea |
9906 | { |
51470298 |
9907 | Ssh ssh = (Ssh) handle; |
9908 | |
9909 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
5471d09a |
9910 | return 0; |
374330e2 |
9911 | |
d8baa528 |
9912 | ssh->protocol(ssh, (unsigned char *)buf, len, 0); |
5471d09a |
9913 | |
51470298 |
9914 | return ssh_sendbuffer(ssh); |
5471d09a |
9915 | } |
9916 | |
9917 | /* |
9918 | * Called to query the current amount of buffered stdin data. |
9919 | */ |
51470298 |
9920 | static int ssh_sendbuffer(void *handle) |
5471d09a |
9921 | { |
51470298 |
9922 | Ssh ssh = (Ssh) handle; |
5471d09a |
9923 | int override_value; |
9924 | |
51470298 |
9925 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
5471d09a |
9926 | return 0; |
9927 | |
9928 | /* |
9929 | * If the SSH socket itself has backed up, add the total backup |
9930 | * size on that to any individual buffer on the stdin channel. |
9931 | */ |
9932 | override_value = 0; |
51470298 |
9933 | if (ssh->throttled_all) |
9934 | override_value = ssh->overall_bufsize; |
5471d09a |
9935 | |
51470298 |
9936 | if (ssh->version == 1) { |
5471d09a |
9937 | return override_value; |
51470298 |
9938 | } else if (ssh->version == 2) { |
bc06669b |
9939 | if (!ssh->mainchan) |
5471d09a |
9940 | return override_value; |
9941 | else |
51470298 |
9942 | return (override_value + |
9943 | bufchain_size(&ssh->mainchan->v.v2.outbuffer)); |
5471d09a |
9944 | } |
9945 | |
9946 | return 0; |
374330e2 |
9947 | } |
9948 | |
9949 | /* |
6e48c3fe |
9950 | * Called to set the size of the window from SSH's POV. |
374330e2 |
9951 | */ |
51470298 |
9952 | static void ssh_size(void *handle, int width, int height) |
32874aea |
9953 | { |
51470298 |
9954 | Ssh ssh = (Ssh) handle; |
ff3187f6 |
9955 | struct Packet *pktout; |
51470298 |
9956 | |
9957 | ssh->term_width = width; |
9958 | ssh->term_height = height; |
f278d6f8 |
9959 | |
51470298 |
9960 | switch (ssh->state) { |
374330e2 |
9961 | case SSH_STATE_BEFORE_SIZE: |
3687d221 |
9962 | case SSH_STATE_PREPACKET: |
21248260 |
9963 | case SSH_STATE_CLOSED: |
374330e2 |
9964 | break; /* do nothing */ |
9965 | case SSH_STATE_INTERMED: |
51470298 |
9966 | ssh->size_needed = TRUE; /* buffer for later */ |
374330e2 |
9967 | break; |
9968 | case SSH_STATE_SESSION: |
4a693cfc |
9969 | if (!conf_get_int(ssh->conf, CONF_nopty)) { |
51470298 |
9970 | if (ssh->version == 1) { |
9971 | send_packet(ssh, SSH1_CMSG_WINDOW_SIZE, |
9972 | PKT_INT, ssh->term_height, |
9973 | PKT_INT, ssh->term_width, |
32874aea |
9974 | PKT_INT, 0, PKT_INT, 0, PKT_END); |
0ed48730 |
9975 | } else if (ssh->mainchan) { |
134f5ece |
9976 | pktout = ssh2_chanreq_init(ssh->mainchan, "window-change", |
9977 | NULL, NULL); |
ff3187f6 |
9978 | ssh2_pkt_adduint32(pktout, ssh->term_width); |
9979 | ssh2_pkt_adduint32(pktout, ssh->term_height); |
9980 | ssh2_pkt_adduint32(pktout, 0); |
9981 | ssh2_pkt_adduint32(pktout, 0); |
9982 | ssh2_pkt_send(ssh, pktout); |
32874aea |
9983 | } |
9984 | } |
9985 | break; |
374330e2 |
9986 | } |
9987 | } |
9988 | |
9989 | /* |
125105d1 |
9990 | * Return a list of the special codes that make sense in this |
9991 | * protocol. |
9992 | */ |
9993 | static const struct telnet_special *ssh_get_specials(void *handle) |
9994 | { |
786ba75e |
9995 | static const struct telnet_special ssh1_ignore_special[] = { |
9996 | {"IGNORE message", TS_NOP} |
9997 | }; |
cf6ddb95 |
9998 | static const struct telnet_special ssh2_ignore_special[] = { |
62638676 |
9999 | {"IGNORE message", TS_NOP}, |
cf6ddb95 |
10000 | }; |
10001 | static const struct telnet_special ssh2_rekey_special[] = { |
9442dd57 |
10002 | {"Repeat key exchange", TS_REKEY}, |
62638676 |
10003 | }; |
10004 | static const struct telnet_special ssh2_session_specials[] = { |
6f2d0cde |
10005 | {NULL, TS_SEP}, |
10006 | {"Break", TS_BRK}, |
bccbeb71 |
10007 | /* These are the signal names defined by RFC 4254. |
6f2d0cde |
10008 | * They include all the ISO C signals, but are a subset of the POSIX |
10009 | * required signals. */ |
10010 | {"SIGINT (Interrupt)", TS_SIGINT}, |
10011 | {"SIGTERM (Terminate)", TS_SIGTERM}, |
10012 | {"SIGKILL (Kill)", TS_SIGKILL}, |
10013 | {"SIGQUIT (Quit)", TS_SIGQUIT}, |
10014 | {"SIGHUP (Hangup)", TS_SIGHUP}, |
10015 | {"More signals", TS_SUBMENU}, |
10016 | {"SIGABRT", TS_SIGABRT}, {"SIGALRM", TS_SIGALRM}, |
10017 | {"SIGFPE", TS_SIGFPE}, {"SIGILL", TS_SIGILL}, |
10018 | {"SIGPIPE", TS_SIGPIPE}, {"SIGSEGV", TS_SIGSEGV}, |
10019 | {"SIGUSR1", TS_SIGUSR1}, {"SIGUSR2", TS_SIGUSR2}, |
10020 | {NULL, TS_EXITMENU} |
62638676 |
10021 | }; |
10022 | static const struct telnet_special specials_end[] = { |
6f2d0cde |
10023 | {NULL, TS_EXITMENU} |
62638676 |
10024 | }; |
786ba75e |
10025 | /* XXX review this length for any changes: */ |
cf6ddb95 |
10026 | static struct telnet_special ssh_specials[lenof(ssh2_ignore_special) + |
10027 | lenof(ssh2_rekey_special) + |
62638676 |
10028 | lenof(ssh2_session_specials) + |
10029 | lenof(specials_end)]; |
125105d1 |
10030 | Ssh ssh = (Ssh) handle; |
62638676 |
10031 | int i = 0; |
10032 | #define ADD_SPECIALS(name) \ |
10033 | do { \ |
10034 | assert((i + lenof(name)) <= lenof(ssh_specials)); \ |
10035 | memcpy(&ssh_specials[i], name, sizeof name); \ |
10036 | i += lenof(name); \ |
10037 | } while(0) |
125105d1 |
10038 | |
10039 | if (ssh->version == 1) { |
62638676 |
10040 | /* Don't bother offering IGNORE if we've decided the remote |
10041 | * won't cope with it, since we wouldn't bother sending it if |
10042 | * asked anyway. */ |
10043 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) |
786ba75e |
10044 | ADD_SPECIALS(ssh1_ignore_special); |
125105d1 |
10045 | } else if (ssh->version == 2) { |
cf6ddb95 |
10046 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) |
10047 | ADD_SPECIALS(ssh2_ignore_special); |
10048 | if (!(ssh->remote_bugs & BUG_SSH2_REKEY)) |
10049 | ADD_SPECIALS(ssh2_rekey_special); |
62638676 |
10050 | if (ssh->mainchan) |
10051 | ADD_SPECIALS(ssh2_session_specials); |
10052 | } /* else we're not ready yet */ |
10053 | |
10054 | if (i) { |
10055 | ADD_SPECIALS(specials_end); |
10056 | return ssh_specials; |
10057 | } else { |
125105d1 |
10058 | return NULL; |
62638676 |
10059 | } |
10060 | #undef ADD_SPECIALS |
125105d1 |
10061 | } |
10062 | |
10063 | /* |
86dc445a |
10064 | * Send special codes. TS_EOF is useful for `plink', so you |
6abbf9e3 |
10065 | * can send an EOF and collect resulting output (e.g. `plink |
10066 | * hostname sort'). |
374330e2 |
10067 | */ |
51470298 |
10068 | static void ssh_special(void *handle, Telnet_Special code) |
32874aea |
10069 | { |
51470298 |
10070 | Ssh ssh = (Ssh) handle; |
ff3187f6 |
10071 | struct Packet *pktout; |
51470298 |
10072 | |
6abbf9e3 |
10073 | if (code == TS_EOF) { |
51470298 |
10074 | if (ssh->state != SSH_STATE_SESSION) { |
32874aea |
10075 | /* |
10076 | * Buffer the EOF in case we are pre-SESSION, so we can |
10077 | * send it as soon as we reach SESSION. |
10078 | */ |
10079 | if (code == TS_EOF) |
51470298 |
10080 | ssh->eof_needed = TRUE; |
32874aea |
10081 | return; |
10082 | } |
51470298 |
10083 | if (ssh->version == 1) { |
10084 | send_packet(ssh, SSH1_CMSG_EOF, PKT_END); |
0ed48730 |
10085 | } else if (ssh->mainchan) { |
bc06669b |
10086 | sshfwd_write_eof(ssh->mainchan); |
00a0b113 |
10087 | ssh->send_ok = 0; /* now stop trying to read from stdin */ |
32874aea |
10088 | } |
10089 | logevent("Sent EOF message"); |
125105d1 |
10090 | } else if (code == TS_PING || code == TS_NOP) { |
51470298 |
10091 | if (ssh->state == SSH_STATE_CLOSED |
10092 | || ssh->state == SSH_STATE_PREPACKET) return; |
10093 | if (ssh->version == 1) { |
10094 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) |
10095 | send_packet(ssh, SSH1_MSG_IGNORE, PKT_STR, "", PKT_END); |
32874aea |
10096 | } else { |
cf6ddb95 |
10097 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) { |
10098 | pktout = ssh2_pkt_init(SSH2_MSG_IGNORE); |
10099 | ssh2_pkt_addstring_start(pktout); |
10100 | ssh2_pkt_send_noqueue(ssh, pktout); |
10101 | } |
32874aea |
10102 | } |
9442dd57 |
10103 | } else if (code == TS_REKEY) { |
10104 | if (!ssh->kex_in_progress && ssh->version == 2) { |
f382c87d |
10105 | do_ssh2_transport(ssh, "at user request", -1, NULL); |
9442dd57 |
10106 | } |
125105d1 |
10107 | } else if (code == TS_BRK) { |
10108 | if (ssh->state == SSH_STATE_CLOSED |
10109 | || ssh->state == SSH_STATE_PREPACKET) return; |
10110 | if (ssh->version == 1) { |
2e85c969 |
10111 | logevent("Unable to send BREAK signal in SSH-1"); |
0ed48730 |
10112 | } else if (ssh->mainchan) { |
134f5ece |
10113 | pktout = ssh2_chanreq_init(ssh->mainchan, "break", NULL, NULL); |
ff3187f6 |
10114 | ssh2_pkt_adduint32(pktout, 0); /* default break length */ |
10115 | ssh2_pkt_send(ssh, pktout); |
125105d1 |
10116 | } |
6abbf9e3 |
10117 | } else { |
6f2d0cde |
10118 | /* Is is a POSIX signal? */ |
10119 | char *signame = NULL; |
10120 | if (code == TS_SIGABRT) signame = "ABRT"; |
10121 | if (code == TS_SIGALRM) signame = "ALRM"; |
10122 | if (code == TS_SIGFPE) signame = "FPE"; |
10123 | if (code == TS_SIGHUP) signame = "HUP"; |
10124 | if (code == TS_SIGILL) signame = "ILL"; |
10125 | if (code == TS_SIGINT) signame = "INT"; |
10126 | if (code == TS_SIGKILL) signame = "KILL"; |
10127 | if (code == TS_SIGPIPE) signame = "PIPE"; |
10128 | if (code == TS_SIGQUIT) signame = "QUIT"; |
10129 | if (code == TS_SIGSEGV) signame = "SEGV"; |
10130 | if (code == TS_SIGTERM) signame = "TERM"; |
10131 | if (code == TS_SIGUSR1) signame = "USR1"; |
10132 | if (code == TS_SIGUSR2) signame = "USR2"; |
10133 | /* The SSH-2 protocol does in principle support arbitrary named |
10134 | * signals, including signame@domain, but we don't support those. */ |
10135 | if (signame) { |
10136 | /* It's a signal. */ |
10137 | if (ssh->version == 2 && ssh->mainchan) { |
134f5ece |
10138 | pktout = ssh2_chanreq_init(ssh->mainchan, "signal", NULL, NULL); |
ff3187f6 |
10139 | ssh2_pkt_addstring(pktout, signame); |
10140 | ssh2_pkt_send(ssh, pktout); |
6f2d0cde |
10141 | logeventf(ssh, "Sent signal SIG%s", signame); |
10142 | } |
10143 | } else { |
10144 | /* Never heard of it. Do nothing */ |
10145 | } |
6abbf9e3 |
10146 | } |
374330e2 |
10147 | } |
10148 | |
51470298 |
10149 | void *new_sock_channel(void *handle, Socket s) |
d74d141c |
10150 | { |
51470298 |
10151 | Ssh ssh = (Ssh) handle; |
d74d141c |
10152 | struct ssh_channel *c; |
3d88e64d |
10153 | c = snew(struct ssh_channel); |
d74d141c |
10154 | |
70c0cbe1 |
10155 | c->ssh = ssh; |
10156 | ssh2_channel_init(c); |
10157 | c->halfopen = TRUE; |
10158 | c->type = CHAN_SOCKDATA_DORMANT;/* identify channel type */ |
10159 | c->u.pfd.s = s; |
10160 | add234(ssh->channels, c); |
d74d141c |
10161 | return c; |
10162 | } |
10163 | |
5471d09a |
10164 | /* |
10165 | * This is called when stdout/stderr (the entity to which |
10166 | * from_backend sends data) manages to clear some backlog. |
10167 | */ |
ae9ae89f |
10168 | static void ssh_unthrottle(void *handle, int bufsize) |
5471d09a |
10169 | { |
51470298 |
10170 | Ssh ssh = (Ssh) handle; |
ff68564b |
10171 | int buflimit; |
10172 | |
51470298 |
10173 | if (ssh->version == 1) { |
10174 | if (ssh->v1_stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) { |
10175 | ssh->v1_stdout_throttling = 0; |
ff68564b |
10176 | ssh_throttle_conn(ssh, -1); |
5471d09a |
10177 | } |
10178 | } else { |
ff68564b |
10179 | if (ssh->mainchan) { |
ab87bf1d |
10180 | ssh2_set_window(ssh->mainchan, |
ff68564b |
10181 | bufsize < ssh->mainchan->v.v2.locmaxwin ? |
10182 | ssh->mainchan->v.v2.locmaxwin - bufsize : 0); |
4a693cfc |
10183 | if (conf_get_int(ssh->conf, CONF_ssh_simple)) |
ff68564b |
10184 | buflimit = 0; |
10185 | else |
10186 | buflimit = ssh->mainchan->v.v2.locmaxwin; |
10187 | if (ssh->mainchan->throttling_conn && bufsize <= buflimit) { |
10188 | ssh->mainchan->throttling_conn = 0; |
10189 | ssh_throttle_conn(ssh, -1); |
10190 | } |
10191 | } |
5471d09a |
10192 | } |
78e71de1 |
10193 | |
10194 | /* |
10195 | * Now process any SSH connection data that was stashed in our |
10196 | * queue while we were frozen. |
10197 | */ |
10198 | ssh_process_queued_incoming_data(ssh); |
5471d09a |
10199 | } |
10200 | |
6b78788a |
10201 | void ssh_send_port_open(void *channel, char *hostname, int port, char *org) |
d74d141c |
10202 | { |
10203 | struct ssh_channel *c = (struct ssh_channel *)channel; |
6b78788a |
10204 | Ssh ssh = c->ssh; |
ff3187f6 |
10205 | struct Packet *pktout; |
d74d141c |
10206 | |
4e63d9b2 |
10207 | logeventf(ssh, "Opening connection to %s:%d for %s", hostname, port, org); |
d74d141c |
10208 | |
51470298 |
10209 | if (ssh->version == 1) { |
10210 | send_packet(ssh, SSH1_MSG_PORT_OPEN, |
bc240b21 |
10211 | PKT_INT, c->localid, |
10212 | PKT_STR, hostname, |
10213 | PKT_INT, port, |
31fb1866 |
10214 | /* PKT_STR, <org:orgport>, */ |
bc240b21 |
10215 | PKT_END); |
10216 | } else { |
4a62c66d |
10217 | pktout = ssh2_chanopen_init(c, "direct-tcpip"); |
ff3187f6 |
10218 | ssh2_pkt_addstring(pktout, hostname); |
10219 | ssh2_pkt_adduint32(pktout, port); |
bc240b21 |
10220 | /* |
10221 | * We make up values for the originator data; partly it's |
10222 | * too much hassle to keep track, and partly I'm not |
10223 | * convinced the server should be told details like that |
10224 | * about my local network configuration. |
1f182589 |
10225 | * The "originator IP address" is syntactically a numeric |
10226 | * IP address, and some servers (e.g., Tectia) get upset |
10227 | * if it doesn't match this syntax. |
bc240b21 |
10228 | */ |
1f182589 |
10229 | ssh2_pkt_addstring(pktout, "0.0.0.0"); |
ff3187f6 |
10230 | ssh2_pkt_adduint32(pktout, 0); |
10231 | ssh2_pkt_send(ssh, pktout); |
bc240b21 |
10232 | } |
d74d141c |
10233 | } |
10234 | |
6226c939 |
10235 | static int ssh_connected(void *handle) |
32874aea |
10236 | { |
51470298 |
10237 | Ssh ssh = (Ssh) handle; |
6226c939 |
10238 | return ssh->s != NULL; |
32874aea |
10239 | } |
8ccc75b0 |
10240 | |
51470298 |
10241 | static int ssh_sendok(void *handle) |
32874aea |
10242 | { |
51470298 |
10243 | Ssh ssh = (Ssh) handle; |
10244 | return ssh->send_ok; |
32874aea |
10245 | } |
fb09bf1c |
10246 | |
51470298 |
10247 | static int ssh_ldisc(void *handle, int option) |
32874aea |
10248 | { |
51470298 |
10249 | Ssh ssh = (Ssh) handle; |
32874aea |
10250 | if (option == LD_ECHO) |
51470298 |
10251 | return ssh->echoing; |
32874aea |
10252 | if (option == LD_EDIT) |
51470298 |
10253 | return ssh->editing; |
0965bee0 |
10254 | return FALSE; |
10255 | } |
10256 | |
b9d7bcad |
10257 | static void ssh_provide_ldisc(void *handle, void *ldisc) |
10258 | { |
10259 | Ssh ssh = (Ssh) handle; |
10260 | ssh->ldisc = ldisc; |
10261 | } |
10262 | |
a8327734 |
10263 | static void ssh_provide_logctx(void *handle, void *logctx) |
10264 | { |
10265 | Ssh ssh = (Ssh) handle; |
10266 | ssh->logctx = logctx; |
10267 | } |
10268 | |
51470298 |
10269 | static int ssh_return_exitcode(void *handle) |
10270 | { |
10271 | Ssh ssh = (Ssh) handle; |
3bb2f322 |
10272 | if (ssh->s != NULL) |
10273 | return -1; |
10274 | else |
3c112d53 |
10275 | return (ssh->exitcode >= 0 ? ssh->exitcode : INT_MAX); |
51470298 |
10276 | } |
10277 | |
10278 | /* |
f89c3294 |
10279 | * cfg_info for SSH is the currently running version of the |
10280 | * protocol. (1 for 1; 2 for 2; 0 for not-decided-yet.) |
10281 | */ |
10282 | static int ssh_cfg_info(void *handle) |
10283 | { |
10284 | Ssh ssh = (Ssh) handle; |
10285 | return ssh->version; |
10286 | } |
10287 | |
10288 | /* |
51470298 |
10289 | * Gross hack: pscp will try to start SFTP but fall back to scp1 if |
10290 | * that fails. This variable is the means by which scp.c can reach |
10291 | * into the SSH code and find out which one it got. |
10292 | */ |
10293 | extern int ssh_fallback_cmd(void *handle) |
d8d6c7e5 |
10294 | { |
51470298 |
10295 | Ssh ssh = (Ssh) handle; |
10296 | return ssh->fallback_cmd; |
d8d6c7e5 |
10297 | } |
10298 | |
374330e2 |
10299 | Backend ssh_backend = { |
10300 | ssh_init, |
fabd1805 |
10301 | ssh_free, |
86916870 |
10302 | ssh_reconfig, |
374330e2 |
10303 | ssh_send, |
5471d09a |
10304 | ssh_sendbuffer, |
374330e2 |
10305 | ssh_size, |
4017be6d |
10306 | ssh_special, |
125105d1 |
10307 | ssh_get_specials, |
6226c939 |
10308 | ssh_connected, |
d8d6c7e5 |
10309 | ssh_return_exitcode, |
97db3be4 |
10310 | ssh_sendok, |
0965bee0 |
10311 | ssh_ldisc, |
b9d7bcad |
10312 | ssh_provide_ldisc, |
a8327734 |
10313 | ssh_provide_logctx, |
5471d09a |
10314 | ssh_unthrottle, |
f89c3294 |
10315 | ssh_cfg_info, |
9e164d82 |
10316 | "ssh", |
10317 | PROT_SSH, |
97db3be4 |
10318 | 22 |
bc240b21 |
10319 | }; |