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