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
24 crLine=__LINE__; return (z); case __LINE__:;\
28 crLine=__LINE__; return; case __LINE__:;\
30 #define crStop(z) do{ crLine = 0; return (z); }while(0)
31 #define crStopV do{ crLine = 0; return; }while(0)
40 static SOCKET s = INVALID_SOCKET;
42 static unsigned char session_key[32];
43 static struct ssh_cipher *cipher = NULL;
45 static char *savedhost;
48 SSH_STATE_BEFORE_SIZE,
52 } ssh_state = SSH_STATE_BEFORE_SIZE;
54 static int size_needed = FALSE;
56 static void s_write (char *buf, int len) {
58 int i = send (s, buf, len, 0);
64 static int s_read (char *buf, int len) {
67 int i = recv (s, buf, len, 0);
69 len -= i, buf += i, ret += i;
76 static void c_write (char *buf, int len) {
78 int new_head = (inbuf_head + 1) & INBUF_MASK;
79 if (new_head != inbuf_reap) {
80 inbuf[inbuf_head] = *buf++;
81 inbuf_head = new_head;
95 static struct Packet pktin = { 0, 0, 0, NULL, 0 };
96 static struct Packet pktout = { 0, 0, 0, NULL, 0 };
98 static void ssh_protocol(unsigned char *in, int inlen, int ispkt);
99 static void ssh_size(void);
101 static void ssh_gotdata(unsigned char *data, int datalen) {
102 static long len, biglen, to_read;
103 static unsigned char *p;
108 for (i = len = 0; i < 4; i++) {
111 len = (len << 8) + *data;
116 if (len == 0x52656d6f) { /* "Remo"te server has closed ... */
117 len = 0x300; /* big enough to carry to end */
125 len -= 5; /* type and CRC */
128 if (pktin.maxlen < biglen) {
129 pktin.maxlen = biglen;
130 pktin.data = (pktin.data == NULL ? malloc(biglen) :
131 realloc(pktin.data, biglen));
133 fatalbox("Out of memory");
136 p = pktin.data, to_read = biglen;
137 while (to_read > 0) {
144 memcpy(p, data, chunk);
152 cipher->decrypt(pktin.data, biglen);
154 pktin.type = pktin.data[pad];
155 pktin.body = pktin.data+pad+1;
157 if (pktin.type == 36) { /* SSH_MSG_DEBUG */
159 } else if (pktin.type == 32) { /* SSH_MSG_IGNORE */
162 ssh_protocol(NULL, 0, 1);
167 static void s_wrpkt_start(int type, int len) {
170 len += 5; /* type and CRC */
174 pktout.length = len-5;
175 if (pktout.maxlen < biglen) {
176 pktout.maxlen = biglen;
177 pktout.data = (pktout.data == NULL ? malloc(biglen+4) :
178 realloc(pktout.data, biglen+4));
180 fatalbox("Out of memory");
184 pktout.body = pktout.data+4+pad+1;
187 static void s_wrpkt(void) {
188 int pad, len, biglen, i;
191 len = pktout.length + 5; /* type and CRC */
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);
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);
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;
211 cipher->encrypt(pktout.data+4, biglen);
213 s_write(pktout.data, biglen+4);
216 static int ssh_versioncmp(char *a, char *b) {
218 unsigned long av, bv;
220 av = strtoul(a, &ae, 10);
221 bv = strtoul(b, &be, 10);
222 if (av != bv) return (av < bv ? -1 : +1);
223 if (*ae == '.') ae++;
224 if (*be == '.') be++;
225 av = strtoul(ae, &ae, 10);
226 bv = strtoul(be, &be, 10);
227 if (av != bv) return (av < bv ? -1 : +1);
231 static int do_ssh_init(void) {
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;
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;
250 if (s_read(&c,1) != 1 || c != '-') return 0;
253 if (s_read(&c,1) != 1)
259 } else if (i < sizeof(version)-1)
266 sprintf(vstring, "SSH-%s-PuTTY\n",
267 (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5"));
268 s_write(vstring, strlen(vstring));
272 static void ssh_protocol(unsigned char *in, int inlen, int ispkt) {
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;
279 unsigned long supported_ciphers_mask;
282 extern struct ssh_cipher ssh_3des;
283 extern struct ssh_cipher ssh_des;
284 extern struct ssh_cipher ssh_blowfish;
294 fatalbox("Public key packet not received");
296 memcpy(cookie, pktin.body, 8);
300 i = makekey(pktin.body+8, &servkey, &keystr1);
302 j = makekey(pktin.body+8+i, &hostkey, &keystr2);
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]);
309 MD5Update(&md5c, keystr2, hostkey.bytes);
310 MD5Update(&md5c, keystr1, servkey.bytes);
311 MD5Update(&md5c, pktin.body, 8);
313 MD5Final(session_id, &md5c);
316 session_key[i] = random_byte();
318 len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
320 rsabuf = malloc(len);
322 fatalbox("Out of memory");
324 verify_ssh_host_key(savedhost, &hostkey);
326 for (i=0; i<32; i++) {
327 rsabuf[i] = session_key[i];
329 rsabuf[i] ^= session_id[i];
332 if (hostkey.bytes > servkey.bytes) {
333 rsaencrypt(rsabuf, 32, &servkey);
334 rsaencrypt(rsabuf, servkey.bytes, &hostkey);
336 rsaencrypt(rsabuf, 32, &hostkey);
337 rsaencrypt(rsabuf, hostkey.bytes, &servkey);
340 cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH :
341 cfg.cipher == CIPHER_DES ? SSH_CIPHER_DES :
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;
348 s_wrpkt_start(3, len+15);
349 pktout.body[0] = cipher_type;
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;
360 cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish :
361 cipher_type == SSH_CIPHER_DES ? &ssh_des :
363 cipher->sesskey(session_key);
365 do { crReturnV; } while (!ispkt);
367 if (pktin.type != 14)
368 fatalbox("Encryption not successfully enabled");
372 static char username[100];
375 if (!*cfg.username) {
376 c_write("login as: ", 10);
378 do { crReturnV; } while (ispkt);
379 while (inlen--) switch (c = *in++) {
401 if (c >= ' ' && c <= '~' && pos < 40) {
409 username[strcspn(username, "\n\r")] = '\0';
412 strncpy(username, cfg.username, 99);
414 sprintf(stuff, "Sent username \"%s\".\r\n", username);
415 c_write(stuff, strlen(stuff));
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));
424 do { crReturnV; } while (!ispkt);
426 while (pktin.type == 15) {
427 static char password[100];
430 c_write("password: ", 10);
433 do { crReturnV; } while (ispkt);
434 while (inlen--) switch (c = *in++) {
451 if (c >= ' ' && c <= '~' && pos < 40)
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));
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);
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);
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 */
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);
499 s_wrpkt_start(12, 0);
502 ssh_state = SSH_STATE_SESSION;
509 if (pktin.type == 17 || pktin.type == 18) {
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) {
515 /* SSH_MSG_DISCONNECT */
516 ssh_state = SSH_STATE_CLOSED;
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) {
524 s_wrpkt_start(33, 0);
527 fatalbox("Strange packet received: type %d", pktin.type);
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);
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().
548 * Returns an error message, or NULL on success.
550 * Also places the canonical host name into `realhost'.
552 static char *ssh_init (HWND hwnd, char *host, int port, char **realhost) {
561 savedhost = malloc(1+strlen(host));
563 fatalbox("Out of memory");
564 strcpy(savedhost, host);
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";
585 memcpy (&a, h->h_addr, sizeof(a));
586 *realhost = h->h_name;
595 port = 22; /* default ssh port */
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";
609 * Bind to local address.
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";
621 * Connect to remote address.
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";
635 send(s, "connect ", 8, 0);
636 send(s, FWhost, strlen(FWhost), 0);
639 sprintf(buf, " %d\n", FWport);
640 send (s, buf, strlen(buf), 0);
645 return "Protocol initialisation error";
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";
657 * Process a WM_NETEVENT message. Will return 0 if the connection
658 * has closed, or <0 for a socket error.
660 static int ssh_msg (WPARAM wParam, LPARAM lParam) {
664 if (s == INVALID_SOCKET) /* how the hell did we get here?! */
667 if (WSAGETSELECTERROR(lParam) != 0)
668 return -WSAGETSELECTERROR(lParam);
670 switch (WSAGETSELECTEVENT(lParam)) {
672 ret = recv(s, buf, sizeof(buf), 0);
673 if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
675 if (ret < 0) /* any _other_ error */
676 return -10000-WSAGetLastError();
679 return 0; /* can't happen, in theory */
681 ssh_gotdata (buf, ret);
685 ssh_state = SSH_STATE_CLOSED;
688 return 1; /* shouldn't happen, but WTF */
692 * Called to send data down the Telnet connection.
694 static void ssh_send (char *buf, int len) {
695 if (s == INVALID_SOCKET)
698 ssh_protocol(buf, len, 0);
702 * Called to set the size of the window from Telnet's POV.
704 static void ssh_size(void) {
706 case SSH_STATE_BEFORE_SIZE:
707 case SSH_STATE_CLOSED:
708 break; /* do nothing */
709 case SSH_STATE_INTERMED:
710 size_needed = TRUE; /* buffer for later */
712 case SSH_STATE_SESSION:
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);
730 * (Send Telnet special codes)
732 static void ssh_special (Telnet_Special code) {
736 Backend ssh_backend = {