374330e2 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <winsock.h> |
4 | |
5 | #include "putty.h" |
6 | |
7 | #ifndef FALSE |
8 | #define FALSE 0 |
9 | #endif |
10 | #ifndef TRUE |
11 | #define TRUE 1 |
12 | #endif |
13 | |
14 | #include "ssh.h" |
15 | |
972a41c8 |
16 | #define SSH_MSG_DISCONNECT 1 |
17 | #define SSH_SMSG_PUBLIC_KEY 2 |
18 | #define SSH_CMSG_SESSION_KEY 3 |
19 | #define SSH_CMSG_USER 4 |
20 | #define SSH_CMSG_AUTH_PASSWORD 9 |
21 | #define SSH_CMSG_REQUEST_PTY 10 |
22 | #define SSH_CMSG_EXEC_SHELL 12 |
23 | #define SSH_CMSG_STDIN_DATA 16 |
24 | #define SSH_SMSG_STDOUT_DATA 17 |
25 | #define SSH_SMSG_STDERR_DATA 18 |
26 | #define SSH_SMSG_SUCCESS 14 |
27 | #define SSH_SMSG_FAILURE 15 |
28 | #define SSH_SMSG_EXITSTATUS 20 |
29 | #define SSH_MSG_IGNORE 32 |
30 | #define SSH_CMSG_EXIT_CONFIRMATION 33 |
31 | #define SSH_MSG_DEBUG 36 |
ccbfb941 |
32 | #define SSH_CMSG_AUTH_TIS 39 |
33 | #define SSH_SMSG_AUTH_TIS_CHALLENGE 40 |
34 | #define SSH_CMSG_AUTH_TIS_RESPONSE 41 |
35 | |
36 | #define SSH_AUTH_TIS 5 |
972a41c8 |
37 | |
374330e2 |
38 | /* Coroutine mechanics for the sillier bits of the code */ |
39 | #define crBegin1 static int crLine = 0; |
40 | #define crBegin2 switch(crLine) { case 0:; |
41 | #define crBegin crBegin1; crBegin2; |
42 | #define crFinish(z) } crLine = 0; return (z) |
43 | #define crFinishV } crLine = 0; return |
44 | #define crReturn(z) \ |
45 | do {\ |
46 | crLine=__LINE__; return (z); case __LINE__:;\ |
47 | } while (0) |
48 | #define crReturnV \ |
49 | do {\ |
50 | crLine=__LINE__; return; case __LINE__:;\ |
51 | } while (0) |
52 | #define crStop(z) do{ crLine = 0; return (z); }while(0) |
53 | #define crStopV do{ crLine = 0; return; }while(0) |
54 | |
55 | #ifndef FALSE |
56 | #define FALSE 0 |
57 | #endif |
58 | #ifndef TRUE |
59 | #define TRUE 1 |
60 | #endif |
61 | |
62 | static SOCKET s = INVALID_SOCKET; |
63 | |
64 | static unsigned char session_key[32]; |
65 | static struct ssh_cipher *cipher = NULL; |
66 | |
67 | static char *savedhost; |
68 | |
69 | static enum { |
70 | SSH_STATE_BEFORE_SIZE, |
71 | SSH_STATE_INTERMED, |
21248260 |
72 | SSH_STATE_SESSION, |
73 | SSH_STATE_CLOSED |
374330e2 |
74 | } ssh_state = SSH_STATE_BEFORE_SIZE; |
75 | |
76 | static int size_needed = FALSE; |
77 | |
78 | static void s_write (char *buf, int len) { |
79 | while (len > 0) { |
80 | int i = send (s, buf, len, 0); |
81 | if (i > 0) |
82 | len -= i, buf += i; |
83 | } |
84 | } |
85 | |
86 | static int s_read (char *buf, int len) { |
87 | int ret = 0; |
88 | while (len > 0) { |
89 | int i = recv (s, buf, len, 0); |
90 | if (i > 0) |
91 | len -= i, buf += i, ret += i; |
92 | else |
93 | return i; |
94 | } |
95 | return ret; |
96 | } |
97 | |
98 | static void c_write (char *buf, int len) { |
99 | while (len--) { |
100 | int new_head = (inbuf_head + 1) & INBUF_MASK; |
374330e2 |
101 | if (new_head != inbuf_reap) { |
102 | inbuf[inbuf_head] = *buf++; |
103 | inbuf_head = new_head; |
104 | } |
105 | } |
106 | } |
107 | |
108 | struct Packet { |
109 | long length; |
110 | int type; |
111 | unsigned long crc; |
112 | unsigned char *data; |
113 | unsigned char *body; |
114 | long maxlen; |
115 | }; |
116 | |
117 | static struct Packet pktin = { 0, 0, 0, NULL, 0 }; |
118 | static struct Packet pktout = { 0, 0, 0, NULL, 0 }; |
119 | |
120 | static void ssh_protocol(unsigned char *in, int inlen, int ispkt); |
121 | static void ssh_size(void); |
122 | |
123 | static void ssh_gotdata(unsigned char *data, int datalen) { |
124 | static long len, biglen, to_read; |
f67b4e85 |
125 | static unsigned char *p; |
374330e2 |
126 | static int i, pad; |
374330e2 |
127 | |
128 | crBegin; |
129 | while (1) { |
130 | for (i = len = 0; i < 4; i++) { |
131 | while (datalen == 0) |
132 | crReturnV; |
133 | len = (len << 8) + *data; |
134 | data++, datalen--; |
135 | } |
136 | |
37508af4 |
137 | #ifdef FWHACK |
138 | if (len == 0x52656d6f) { /* "Remo"te server has closed ... */ |
139 | len = 0x300; /* big enough to carry to end */ |
140 | } |
141 | #endif |
142 | |
374330e2 |
143 | pad = 8 - (len%8); |
144 | |
145 | biglen = len + pad; |
146 | |
147 | len -= 5; /* type and CRC */ |
148 | |
149 | pktin.length = len; |
150 | if (pktin.maxlen < biglen) { |
151 | pktin.maxlen = biglen; |
152 | pktin.data = (pktin.data == NULL ? malloc(biglen) : |
153 | realloc(pktin.data, biglen)); |
154 | if (!pktin.data) |
155 | fatalbox("Out of memory"); |
156 | } |
157 | |
158 | p = pktin.data, to_read = biglen; |
159 | while (to_read > 0) { |
160 | static int chunk; |
161 | chunk = to_read; |
162 | while (datalen == 0) |
163 | crReturnV; |
164 | if (chunk > datalen) |
165 | chunk = datalen; |
166 | memcpy(p, data, chunk); |
167 | data += chunk; |
168 | datalen -= chunk; |
169 | p += chunk; |
170 | to_read -= chunk; |
171 | } |
172 | |
173 | if (cipher) |
174 | cipher->decrypt(pktin.data, biglen); |
175 | |
176 | pktin.type = pktin.data[pad]; |
177 | pktin.body = pktin.data+pad+1; |
178 | |
972a41c8 |
179 | if (pktin.type == SSH_MSG_DEBUG) { |
374330e2 |
180 | /* FIXME: log it */ |
972a41c8 |
181 | } else if (pktin.type == SSH_MSG_IGNORE) { |
96b9dc0a |
182 | /* do nothing */; |
374330e2 |
183 | } else |
184 | ssh_protocol(NULL, 0, 1); |
185 | } |
186 | crFinishV; |
187 | } |
188 | |
189 | static void s_wrpkt_start(int type, int len) { |
190 | int pad, biglen; |
191 | |
192 | len += 5; /* type and CRC */ |
193 | pad = 8 - (len%8); |
194 | biglen = len + pad; |
195 | |
196 | pktout.length = len-5; |
197 | if (pktout.maxlen < biglen) { |
198 | pktout.maxlen = biglen; |
c1f5f956 |
199 | pktout.data = (pktout.data == NULL ? malloc(biglen+4) : |
200 | realloc(pktout.data, biglen+4)); |
374330e2 |
201 | if (!pktout.data) |
202 | fatalbox("Out of memory"); |
203 | } |
204 | |
205 | pktout.type = type; |
206 | pktout.body = pktout.data+4+pad+1; |
207 | } |
208 | |
209 | static void s_wrpkt(void) { |
210 | int pad, len, biglen, i; |
211 | unsigned long crc; |
212 | |
213 | len = pktout.length + 5; /* type and CRC */ |
214 | pad = 8 - (len%8); |
215 | biglen = len + pad; |
216 | |
217 | pktout.body[-1] = pktout.type; |
218 | for (i=0; i<pad; i++) |
219 | pktout.data[i+4] = random_byte(); |
220 | crc = crc32(pktout.data+4, biglen-4); |
221 | |
222 | pktout.data[biglen+0] = (unsigned char) ((crc >> 24) & 0xFF); |
223 | pktout.data[biglen+1] = (unsigned char) ((crc >> 16) & 0xFF); |
224 | pktout.data[biglen+2] = (unsigned char) ((crc >> 8) & 0xFF); |
225 | pktout.data[biglen+3] = (unsigned char) (crc & 0xFF); |
226 | |
227 | pktout.data[0] = (len >> 24) & 0xFF; |
228 | pktout.data[1] = (len >> 16) & 0xFF; |
229 | pktout.data[2] = (len >> 8) & 0xFF; |
230 | pktout.data[3] = len & 0xFF; |
231 | |
232 | if (cipher) |
233 | cipher->encrypt(pktout.data+4, biglen); |
234 | |
235 | s_write(pktout.data, biglen+4); |
236 | } |
237 | |
9697bfd2 |
238 | static int ssh_versioncmp(char *a, char *b) { |
239 | char *ae, *be; |
240 | unsigned long av, bv; |
241 | |
43aa02a7 |
242 | av = strtoul(a, &ae, 10); |
243 | bv = strtoul(b, &be, 10); |
9697bfd2 |
244 | if (av != bv) return (av < bv ? -1 : +1); |
245 | if (*ae == '.') ae++; |
246 | if (*be == '.') be++; |
43aa02a7 |
247 | av = strtoul(ae, &ae, 10); |
248 | bv = strtoul(be, &be, 10); |
9697bfd2 |
249 | if (av != bv) return (av < bv ? -1 : +1); |
250 | return 0; |
251 | } |
252 | |
374330e2 |
253 | static int do_ssh_init(void) { |
c5e9c988 |
254 | char c, *vsp; |
374330e2 |
255 | char version[10]; |
c5e9c988 |
256 | char vstring[80]; |
257 | char vlog[sizeof(vstring)+20]; |
374330e2 |
258 | int i; |
259 | |
260 | #ifdef FWHACK |
261 | i = 0; |
262 | while (s_read(&c, 1) == 1) { |
263 | if (c == 'S' && i < 2) i++; |
264 | else if (c == 'S' && i == 2) i = 2; |
265 | else if (c == 'H' && i == 2) break; |
266 | else i = 0; |
267 | } |
268 | #else |
269 | if (s_read(&c,1) != 1 || c != 'S') return 0; |
270 | if (s_read(&c,1) != 1 || c != 'S') return 0; |
271 | if (s_read(&c,1) != 1 || c != 'H') return 0; |
272 | #endif |
c5e9c988 |
273 | strcpy(vstring, "SSH-"); |
274 | vsp = vstring+4; |
374330e2 |
275 | if (s_read(&c,1) != 1 || c != '-') return 0; |
276 | i = 0; |
277 | while (1) { |
278 | if (s_read(&c,1) != 1) |
279 | return 0; |
c5e9c988 |
280 | if (vsp < vstring+sizeof(vstring)-1) |
281 | *vsp++ = c; |
374330e2 |
282 | if (i >= 0) { |
283 | if (c == '-') { |
284 | version[i] = '\0'; |
285 | i = -1; |
286 | } else if (i < sizeof(version)-1) |
287 | version[i++] = c; |
288 | } |
289 | else if (c == '\n') |
290 | break; |
291 | } |
292 | |
c5e9c988 |
293 | *vsp = 0; |
294 | sprintf(vlog, "Server version: %s", vstring); |
295 | vlog[strcspn(vlog, "\r\n")] = '\0'; |
296 | logevent(vlog); |
297 | |
9697bfd2 |
298 | sprintf(vstring, "SSH-%s-PuTTY\n", |
299 | (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5")); |
c5e9c988 |
300 | sprintf(vlog, "We claim version: %s", vstring); |
301 | vlog[strcspn(vlog, "\r\n")] = '\0'; |
302 | logevent(vlog); |
374330e2 |
303 | s_write(vstring, strlen(vstring)); |
fef97f43 |
304 | return 1; |
374330e2 |
305 | } |
306 | |
307 | static void ssh_protocol(unsigned char *in, int inlen, int ispkt) { |
308 | int i, j, len; |
309 | unsigned char session_id[16]; |
310 | unsigned char *rsabuf, *keystr1, *keystr2; |
311 | unsigned char cookie[8]; |
312 | struct RSAKey servkey, hostkey; |
313 | struct MD5Context md5c; |
ccbfb941 |
314 | static unsigned long supported_ciphers_mask, supported_auths_mask; |
bea1ef5f |
315 | int cipher_type; |
374330e2 |
316 | |
317 | extern struct ssh_cipher ssh_3des; |
9697bfd2 |
318 | extern struct ssh_cipher ssh_des; |
bea1ef5f |
319 | extern struct ssh_cipher ssh_blowfish; |
374330e2 |
320 | |
321 | crBegin; |
322 | |
323 | random_init(); |
324 | |
325 | while (!ispkt) |
326 | crReturnV; |
327 | |
972a41c8 |
328 | if (pktin.type != SSH_SMSG_PUBLIC_KEY) |
374330e2 |
329 | fatalbox("Public key packet not received"); |
330 | |
c5e9c988 |
331 | logevent("Received public keys"); |
374330e2 |
332 | |
c5e9c988 |
333 | memcpy(cookie, pktin.body, 8); |
374330e2 |
334 | |
335 | i = makekey(pktin.body+8, &servkey, &keystr1); |
336 | |
337 | j = makekey(pktin.body+8+i, &hostkey, &keystr2); |
338 | |
c5e9c988 |
339 | /* |
340 | * Hash the host key and print the hash in the log box. Just as |
341 | * a last resort in case the registry's host key checking is |
342 | * compromised, we'll allow the user some ability to verify |
343 | * host keys by eye. |
344 | */ |
345 | MD5Init(&md5c); |
346 | MD5Update(&md5c, keystr2, hostkey.bytes); |
347 | MD5Final(session_id, &md5c); |
348 | { |
349 | char logmsg[80]; |
350 | int i; |
351 | logevent("Host key MD5 is:"); |
352 | strcpy(logmsg, " "); |
353 | for (i = 0; i < 16; i++) |
354 | sprintf(logmsg+strlen(logmsg), "%02x", session_id[i]); |
355 | logevent(logmsg); |
356 | } |
357 | |
ccbfb941 |
358 | supported_ciphers_mask = ((pktin.body[12+i+j] << 24) | |
359 | (pktin.body[13+i+j] << 16) | |
360 | (pktin.body[14+i+j] << 8) | |
361 | (pktin.body[15+i+j])); |
362 | |
363 | supported_auths_mask = ((pktin.body[16+i+j] << 24) | |
364 | (pktin.body[17+i+j] << 16) | |
365 | (pktin.body[18+i+j] << 8) | |
366 | (pktin.body[19+i+j])); |
bea1ef5f |
367 | |
c5e9c988 |
368 | MD5Init(&md5c); |
369 | |
374330e2 |
370 | MD5Update(&md5c, keystr2, hostkey.bytes); |
371 | MD5Update(&md5c, keystr1, servkey.bytes); |
372 | MD5Update(&md5c, pktin.body, 8); |
373 | |
374 | MD5Final(session_id, &md5c); |
375 | |
376 | for (i=0; i<32; i++) |
377 | session_key[i] = random_byte(); |
378 | |
379 | len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); |
380 | |
381 | rsabuf = malloc(len); |
382 | if (!rsabuf) |
383 | fatalbox("Out of memory"); |
384 | |
385 | verify_ssh_host_key(savedhost, &hostkey); |
386 | |
387 | for (i=0; i<32; i++) { |
388 | rsabuf[i] = session_key[i]; |
389 | if (i < 16) |
390 | rsabuf[i] ^= session_id[i]; |
391 | } |
392 | |
393 | if (hostkey.bytes > servkey.bytes) { |
394 | rsaencrypt(rsabuf, 32, &servkey); |
395 | rsaencrypt(rsabuf, servkey.bytes, &hostkey); |
396 | } else { |
397 | rsaencrypt(rsabuf, 32, &hostkey); |
398 | rsaencrypt(rsabuf, hostkey.bytes, &servkey); |
399 | } |
400 | |
c5e9c988 |
401 | logevent("Encrypted session key"); |
402 | |
bea1ef5f |
403 | cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH : |
9697bfd2 |
404 | cfg.cipher == CIPHER_DES ? SSH_CIPHER_DES : |
bea1ef5f |
405 | SSH_CIPHER_3DES; |
406 | if ((supported_ciphers_mask & (1 << cipher_type)) == 0) { |
407 | c_write("Selected cipher not supported, falling back to 3DES\r\n", 53); |
408 | cipher_type = SSH_CIPHER_3DES; |
409 | } |
c5e9c988 |
410 | switch (cipher_type) { |
411 | case SSH_CIPHER_3DES: logevent("Using 3DES encryption"); break; |
412 | case SSH_CIPHER_DES: logevent("Using single-DES encryption"); break; |
413 | case SSH_CIPHER_BLOWFISH: logevent("Using Blowfish encryption"); break; |
414 | } |
bea1ef5f |
415 | |
972a41c8 |
416 | s_wrpkt_start(SSH_CMSG_SESSION_KEY, len+15); |
bea1ef5f |
417 | pktout.body[0] = cipher_type; |
374330e2 |
418 | memcpy(pktout.body+1, cookie, 8); |
419 | pktout.body[9] = (len*8) >> 8; |
420 | pktout.body[10] = (len*8) & 0xFF; |
421 | memcpy(pktout.body+11, rsabuf, len); |
422 | pktout.body[len+11] = pktout.body[len+12] = 0; /* protocol flags */ |
423 | pktout.body[len+13] = pktout.body[len+14] = 0; |
424 | s_wrpkt(); |
c5e9c988 |
425 | logevent("Trying to enable encryption..."); |
374330e2 |
426 | |
427 | free(rsabuf); |
428 | |
bea1ef5f |
429 | cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish : |
9697bfd2 |
430 | cipher_type == SSH_CIPHER_DES ? &ssh_des : |
bea1ef5f |
431 | &ssh_3des; |
374330e2 |
432 | cipher->sesskey(session_key); |
433 | |
434 | do { crReturnV; } while (!ispkt); |
435 | |
972a41c8 |
436 | if (pktin.type != SSH_SMSG_SUCCESS) |
374330e2 |
437 | fatalbox("Encryption not successfully enabled"); |
438 | |
c5e9c988 |
439 | logevent("Successfully started encryption"); |
440 | |
374330e2 |
441 | fflush(stdout); |
442 | { |
443 | static char username[100]; |
444 | static int pos = 0; |
445 | static char c; |
446 | if (!*cfg.username) { |
447 | c_write("login as: ", 10); |
448 | while (pos >= 0) { |
449 | do { crReturnV; } while (ispkt); |
450 | while (inlen--) switch (c = *in++) { |
451 | case 10: case 13: |
452 | username[pos] = 0; |
453 | pos = -1; |
454 | break; |
455 | case 8: case 127: |
456 | if (pos > 0) { |
457 | c_write("\b \b", 3); |
458 | pos--; |
459 | } |
460 | break; |
461 | case 21: case 27: |
462 | while (pos > 0) { |
463 | c_write("\b \b", 3); |
464 | pos--; |
465 | } |
466 | break; |
467 | case 3: case 4: |
468 | random_save_seed(); |
469 | exit(0); |
470 | break; |
471 | default: |
472 | if (c >= ' ' && c <= '~' && pos < 40) { |
473 | username[pos++] = c; |
474 | c_write(&c, 1); |
475 | } |
476 | break; |
477 | } |
478 | } |
479 | c_write("\r\n", 2); |
480 | username[strcspn(username, "\n\r")] = '\0'; |
481 | } else { |
482 | char stuff[200]; |
483 | strncpy(username, cfg.username, 99); |
484 | username[99] = '\0'; |
485 | sprintf(stuff, "Sent username \"%s\".\r\n", username); |
486 | c_write(stuff, strlen(stuff)); |
487 | } |
972a41c8 |
488 | s_wrpkt_start(SSH_CMSG_USER, 4+strlen(username)); |
c5e9c988 |
489 | { |
490 | char userlog[20+sizeof(username)]; |
491 | sprintf(userlog, "Sent username \"%s\"", username); |
492 | logevent(userlog); |
493 | } |
374330e2 |
494 | pktout.body[0] = pktout.body[1] = pktout.body[2] = 0; |
495 | pktout.body[3] = strlen(username); |
496 | memcpy(pktout.body+4, username, strlen(username)); |
497 | s_wrpkt(); |
498 | } |
499 | |
500 | do { crReturnV; } while (!ispkt); |
501 | |
972a41c8 |
502 | while (pktin.type == SSH_SMSG_FAILURE) { |
374330e2 |
503 | static char password[100]; |
504 | static int pos; |
505 | static char c; |
ccbfb941 |
506 | static int pwpkt_type; |
507 | |
508 | /* |
509 | * Show password prompt, having first obtained it via a TIS |
510 | * exchange if we're doing TIS authentication. |
511 | */ |
512 | pwpkt_type = SSH_CMSG_AUTH_PASSWORD; |
513 | if (pktin.type == SSH_SMSG_FAILURE && |
514 | cfg.try_tis_auth && |
515 | (supported_auths_mask & (1<<SSH_AUTH_TIS))) { |
516 | pwpkt_type = SSH_CMSG_AUTH_TIS_RESPONSE; |
c5e9c988 |
517 | logevent("Requested TIS authentication"); |
ccbfb941 |
518 | s_wrpkt_start(SSH_CMSG_AUTH_TIS, 0); |
519 | s_wrpkt(); |
520 | do { crReturnV; } while (!ispkt); |
521 | if (pktin.type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
c5e9c988 |
522 | logevent("TIS authentication declined"); |
ccbfb941 |
523 | c_write("TIS authentication refused.\r\n", 29); |
524 | } else { |
525 | int challengelen = ((pktin.body[0] << 24) | |
526 | (pktin.body[1] << 16) | |
527 | (pktin.body[2] << 8) | |
528 | (pktin.body[3])); |
c5e9c988 |
529 | logevent("Received TIS challenge"); |
ccbfb941 |
530 | c_write(pktin.body+4, challengelen); |
531 | } |
532 | } |
533 | if (pwpkt_type == SSH_CMSG_AUTH_PASSWORD) |
534 | c_write("password: ", 10); |
535 | |
374330e2 |
536 | pos = 0; |
537 | while (pos >= 0) { |
538 | do { crReturnV; } while (ispkt); |
539 | while (inlen--) switch (c = *in++) { |
540 | case 10: case 13: |
541 | password[pos] = 0; |
542 | pos = -1; |
543 | break; |
544 | case 8: case 127: |
545 | if (pos > 0) |
546 | pos--; |
547 | break; |
548 | case 21: case 27: |
549 | pos = 0; |
550 | break; |
551 | case 3: case 4: |
552 | random_save_seed(); |
553 | exit(0); |
554 | break; |
555 | default: |
556 | if (c >= ' ' && c <= '~' && pos < 40) |
557 | password[pos++] = c; |
558 | break; |
559 | } |
560 | } |
561 | c_write("\r\n", 2); |
ccbfb941 |
562 | s_wrpkt_start(pwpkt_type, 4+strlen(password)); |
374330e2 |
563 | pktout.body[0] = pktout.body[1] = pktout.body[2] = 0; |
564 | pktout.body[3] = strlen(password); |
565 | memcpy(pktout.body+4, password, strlen(password)); |
566 | s_wrpkt(); |
c5e9c988 |
567 | logevent("Sent password"); |
374330e2 |
568 | memset(password, 0, strlen(password)); |
569 | do { crReturnV; } while (!ispkt); |
570 | if (pktin.type == 15) { |
571 | c_write("Access denied\r\n", 15); |
c5e9c988 |
572 | logevent("Authentication refused"); |
374330e2 |
573 | } else if (pktin.type != 14) { |
574 | fatalbox("Strange packet received, type %d", pktin.type); |
575 | } |
576 | } |
577 | |
c5e9c988 |
578 | logevent("Authentication successful"); |
579 | |
fef97f43 |
580 | if (!cfg.nopty) { |
581 | i = strlen(cfg.termtype); |
972a41c8 |
582 | s_wrpkt_start(SSH_CMSG_REQUEST_PTY, i+5*4+1); |
fef97f43 |
583 | pktout.body[0] = (i >> 24) & 0xFF; |
584 | pktout.body[1] = (i >> 16) & 0xFF; |
585 | pktout.body[2] = (i >> 8) & 0xFF; |
586 | pktout.body[3] = i & 0xFF; |
587 | memcpy(pktout.body+4, cfg.termtype, i); |
588 | i += 4; |
589 | pktout.body[i++] = (rows >> 24) & 0xFF; |
590 | pktout.body[i++] = (rows >> 16) & 0xFF; |
591 | pktout.body[i++] = (rows >> 8) & 0xFF; |
592 | pktout.body[i++] = rows & 0xFF; |
593 | pktout.body[i++] = (cols >> 24) & 0xFF; |
594 | pktout.body[i++] = (cols >> 16) & 0xFF; |
595 | pktout.body[i++] = (cols >> 8) & 0xFF; |
596 | pktout.body[i++] = cols & 0xFF; |
597 | memset(pktout.body+i, 0, 9); /* 0 pixwidth, 0 pixheight, 0.b endofopt */ |
598 | s_wrpkt(); |
599 | ssh_state = SSH_STATE_INTERMED; |
600 | do { crReturnV; } while (!ispkt); |
ccbfb941 |
601 | if (pktin.type != SSH_SMSG_SUCCESS && pktin.type != SSH_SMSG_FAILURE) { |
fef97f43 |
602 | fatalbox("Protocol confusion"); |
ccbfb941 |
603 | } else if (pktin.type == SSH_SMSG_FAILURE) { |
fef97f43 |
604 | c_write("Server refused to allocate pty\r\n", 32); |
605 | } |
c5e9c988 |
606 | logevent("Allocated pty"); |
374330e2 |
607 | } |
608 | |
972a41c8 |
609 | s_wrpkt_start(SSH_CMSG_EXEC_SHELL, 0); |
374330e2 |
610 | s_wrpkt(); |
c5e9c988 |
611 | logevent("Started session"); |
374330e2 |
612 | |
613 | ssh_state = SSH_STATE_SESSION; |
614 | if (size_needed) |
615 | ssh_size(); |
616 | |
617 | while (1) { |
618 | crReturnV; |
619 | if (ispkt) { |
972a41c8 |
620 | if (pktin.type == SSH_SMSG_STDOUT_DATA || |
621 | pktin.type == SSH_SMSG_STDERR_DATA) { |
374330e2 |
622 | long len = 0; |
623 | for (i = 0; i < 4; i++) |
624 | len = (len << 8) + pktin.body[i]; |
625 | c_write(pktin.body+4, len); |
972a41c8 |
626 | } else if (pktin.type == SSH_MSG_DISCONNECT) { |
21248260 |
627 | ssh_state = SSH_STATE_CLOSED; |
c5e9c988 |
628 | logevent("Received disconnect request"); |
ccbfb941 |
629 | } else if (pktin.type == SSH_SMSG_SUCCESS) { |
972a41c8 |
630 | /* may be from EXEC_SHELL on some servers */ |
ccbfb941 |
631 | } else if (pktin.type == SSH_SMSG_FAILURE) { |
972a41c8 |
632 | /* may be from EXEC_SHELL on some servers |
374330e2 |
633 | * if no pty is available or in other odd cases. Ignore */ |
972a41c8 |
634 | } else if (pktin.type == SSH_SMSG_EXITSTATUS) { |
635 | s_wrpkt_start(SSH_CMSG_EXIT_CONFIRMATION, 0); |
374330e2 |
636 | s_wrpkt(); |
637 | } else { |
638 | fatalbox("Strange packet received: type %d", pktin.type); |
639 | } |
640 | } else { |
972a41c8 |
641 | s_wrpkt_start(SSH_CMSG_STDIN_DATA, 4+inlen); |
374330e2 |
642 | pktout.body[0] = (inlen >> 24) & 0xFF; |
643 | pktout.body[1] = (inlen >> 16) & 0xFF; |
644 | pktout.body[2] = (inlen >> 8) & 0xFF; |
645 | pktout.body[3] = inlen & 0xFF; |
646 | memcpy(pktout.body+4, in, inlen); |
647 | s_wrpkt(); |
648 | } |
649 | } |
650 | |
651 | crFinishV; |
652 | } |
653 | |
654 | /* |
655 | * Called to set up the connection. Will arrange for WM_NETEVENT |
656 | * messages to be passed to the specified window, whose window |
657 | * procedure should then call telnet_msg(). |
658 | * |
659 | * Returns an error message, or NULL on success. |
660 | * |
661 | * Also places the canonical host name into `realhost'. |
662 | */ |
663 | static char *ssh_init (HWND hwnd, char *host, int port, char **realhost) { |
664 | SOCKADDR_IN addr; |
665 | struct hostent *h; |
666 | unsigned long a; |
667 | #ifdef FWHACK |
668 | char *FWhost; |
669 | int FWport; |
670 | #endif |
671 | |
672 | savedhost = malloc(1+strlen(host)); |
673 | if (!savedhost) |
674 | fatalbox("Out of memory"); |
675 | strcpy(savedhost, host); |
676 | |
677 | #ifdef FWHACK |
678 | FWhost = host; |
679 | FWport = port; |
680 | host = FWSTR; |
681 | port = 23; |
682 | #endif |
683 | |
684 | /* |
685 | * Try to find host. |
686 | */ |
687 | if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) { |
688 | if ( (h = gethostbyname(host)) == NULL) |
689 | switch (WSAGetLastError()) { |
690 | case WSAENETDOWN: return "Network is down"; |
691 | case WSAHOST_NOT_FOUND: case WSANO_DATA: |
692 | return "Host does not exist"; |
693 | case WSATRY_AGAIN: return "Host not found"; |
694 | default: return "gethostbyname: unknown error"; |
695 | } |
696 | memcpy (&a, h->h_addr, sizeof(a)); |
697 | *realhost = h->h_name; |
698 | } else |
699 | *realhost = host; |
700 | #ifdef FWHACK |
701 | *realhost = FWhost; |
702 | #endif |
703 | a = ntohl(a); |
704 | |
705 | if (port < 0) |
706 | port = 22; /* default ssh port */ |
707 | |
708 | /* |
709 | * Open socket. |
710 | */ |
711 | s = socket(AF_INET, SOCK_STREAM, 0); |
712 | if (s == INVALID_SOCKET) |
713 | switch (WSAGetLastError()) { |
714 | case WSAENETDOWN: return "Network is down"; |
715 | case WSAEAFNOSUPPORT: return "TCP/IP support not present"; |
716 | default: return "socket(): unknown error"; |
717 | } |
718 | |
719 | /* |
720 | * Bind to local address. |
721 | */ |
722 | addr.sin_family = AF_INET; |
723 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
724 | addr.sin_port = htons(0); |
725 | if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
726 | switch (WSAGetLastError()) { |
727 | case WSAENETDOWN: return "Network is down"; |
728 | default: return "bind(): unknown error"; |
729 | } |
730 | |
731 | /* |
732 | * Connect to remote address. |
733 | */ |
734 | addr.sin_addr.s_addr = htonl(a); |
735 | addr.sin_port = htons((short)port); |
736 | if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
737 | switch (WSAGetLastError()) { |
738 | case WSAENETDOWN: return "Network is down"; |
739 | case WSAECONNREFUSED: return "Connection refused"; |
740 | case WSAENETUNREACH: return "Network is unreachable"; |
741 | case WSAEHOSTUNREACH: return "No route to host"; |
742 | default: return "connect(): unknown error"; |
743 | } |
744 | |
745 | #ifdef FWHACK |
746 | send(s, "connect ", 8, 0); |
747 | send(s, FWhost, strlen(FWhost), 0); |
748 | { |
749 | char buf[20]; |
750 | sprintf(buf, " %d\n", FWport); |
751 | send (s, buf, strlen(buf), 0); |
752 | } |
753 | #endif |
754 | |
755 | if (!do_ssh_init()) |
756 | return "Protocol initialisation error"; |
757 | |
758 | if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | FD_CLOSE) == SOCKET_ERROR) |
759 | switch (WSAGetLastError()) { |
760 | case WSAENETDOWN: return "Network is down"; |
761 | default: return "WSAAsyncSelect(): unknown error"; |
762 | } |
763 | |
764 | return NULL; |
765 | } |
766 | |
767 | /* |
768 | * Process a WM_NETEVENT message. Will return 0 if the connection |
769 | * has closed, or <0 for a socket error. |
770 | */ |
771 | static int ssh_msg (WPARAM wParam, LPARAM lParam) { |
772 | int ret; |
773 | char buf[256]; |
774 | |
8ce72d2c |
775 | /* |
776 | * Because reading less than the whole of the available pending |
777 | * data can generate an FD_READ event, we need to allow for the |
778 | * possibility that FD_READ may arrive with FD_CLOSE already in |
779 | * the queue; so it's possible that we can get here even with s |
780 | * invalid. If so, we return 1 and don't worry about it. |
781 | */ |
782 | if (s == INVALID_SOCKET) |
783 | return 1; |
374330e2 |
784 | |
785 | if (WSAGETSELECTERROR(lParam) != 0) |
786 | return -WSAGETSELECTERROR(lParam); |
787 | |
788 | switch (WSAGETSELECTEVENT(lParam)) { |
789 | case FD_READ: |
8ce72d2c |
790 | case FD_CLOSE: |
374330e2 |
791 | ret = recv(s, buf, sizeof(buf), 0); |
792 | if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK) |
793 | return 1; |
794 | if (ret < 0) /* any _other_ error */ |
795 | return -10000-WSAGetLastError(); |
796 | if (ret == 0) { |
797 | s = INVALID_SOCKET; |
8ce72d2c |
798 | return 0; |
374330e2 |
799 | } |
800 | ssh_gotdata (buf, ret); |
801 | return 1; |
374330e2 |
802 | } |
803 | return 1; /* shouldn't happen, but WTF */ |
804 | } |
805 | |
806 | /* |
807 | * Called to send data down the Telnet connection. |
808 | */ |
809 | static void ssh_send (char *buf, int len) { |
810 | if (s == INVALID_SOCKET) |
811 | return; |
812 | |
813 | ssh_protocol(buf, len, 0); |
814 | } |
815 | |
816 | /* |
817 | * Called to set the size of the window from Telnet's POV. |
818 | */ |
819 | static void ssh_size(void) { |
820 | switch (ssh_state) { |
821 | case SSH_STATE_BEFORE_SIZE: |
21248260 |
822 | case SSH_STATE_CLOSED: |
374330e2 |
823 | break; /* do nothing */ |
824 | case SSH_STATE_INTERMED: |
825 | size_needed = TRUE; /* buffer for later */ |
826 | break; |
827 | case SSH_STATE_SESSION: |
fef97f43 |
828 | if (!cfg.nopty) { |
829 | s_wrpkt_start(11, 16); |
830 | pktout.body[0] = (rows >> 24) & 0xFF; |
831 | pktout.body[1] = (rows >> 16) & 0xFF; |
832 | pktout.body[2] = (rows >> 8) & 0xFF; |
833 | pktout.body[3] = rows & 0xFF; |
834 | pktout.body[4] = (cols >> 24) & 0xFF; |
835 | pktout.body[5] = (cols >> 16) & 0xFF; |
836 | pktout.body[6] = (cols >> 8) & 0xFF; |
837 | pktout.body[7] = cols & 0xFF; |
838 | memset(pktout.body+8, 0, 8); |
839 | s_wrpkt(); |
840 | } |
374330e2 |
841 | } |
842 | } |
843 | |
844 | /* |
845 | * (Send Telnet special codes) |
846 | */ |
847 | static void ssh_special (Telnet_Special code) { |
848 | /* do nothing */ |
849 | } |
850 | |
851 | Backend ssh_backend = { |
852 | ssh_init, |
853 | ssh_msg, |
854 | ssh_send, |
855 | ssh_size, |
856 | ssh_special |
857 | }; |