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 int c
= (unsigned char) *buf
;
80 if (new_head
!= inbuf_reap
) {
81 inbuf
[inbuf_head
] = *buf
++;
82 inbuf_head
= new_head
;
96 static struct Packet pktin
= { 0, 0, 0, NULL
, 0 };
97 static struct Packet pktout
= { 0, 0, 0, NULL
, 0 };
99 static void ssh_protocol(unsigned char *in
, int inlen
, int ispkt
);
100 static void ssh_size(void);
102 static void ssh_gotdata(unsigned char *data
, int datalen
) {
103 static long len
, biglen
, to_read
;
104 static unsigned char c
, *p
;
106 static char padding
[8];
107 static unsigned char word
[4];
111 for (i
= len
= 0; i
< 4; i
++) {
114 len
= (len
<< 8) + *data
;
119 if (len
== 0x52656d6f) { /* "Remo"te server has closed ... */
120 len
= 0x300; /* big enough to carry to end */
128 len
-= 5; /* type and CRC */
131 if (pktin
.maxlen
< biglen
) {
132 pktin
.maxlen
= biglen
;
133 pktin
.data
= (pktin
.data
== NULL ?
malloc(biglen
) :
134 realloc(pktin
.data
, biglen
));
136 fatalbox("Out of memory");
139 p
= pktin
.data
, to_read
= biglen
;
140 while (to_read
> 0) {
147 memcpy(p
, data
, chunk
);
155 cipher
->decrypt(pktin
.data
, biglen
);
157 pktin
.type
= pktin
.data
[pad
];
158 pktin
.body
= pktin
.data
+pad
+1;
160 if (pktin
.type
== 36) { /* SSH_MSG_DEBUG */
163 ssh_protocol(NULL
, 0, 1);
168 static void s_wrpkt_start(int type
, int len
) {
171 len
+= 5; /* type and CRC */
175 pktout
.length
= len
-5;
176 if (pktout
.maxlen
< biglen
) {
177 pktout
.maxlen
= biglen
;
178 pktout
.data
= (pktout
.data
== NULL ?
malloc(biglen
+4) :
179 realloc(pktout
.data
, biglen
+4));
181 fatalbox("Out of memory");
185 pktout
.body
= pktout
.data
+4+pad
+1;
188 static void s_wrpkt(void) {
189 int pad
, len
, biglen
, i
;
192 len
= pktout
.length
+ 5; /* type and CRC */
196 pktout
.body
[-1] = pktout
.type
;
197 for (i
=0; i
<pad
; i
++)
198 pktout
.data
[i
+4] = random_byte();
199 crc
= crc32(pktout
.data
+4, biglen
-4);
201 pktout
.data
[biglen
+0] = (unsigned char) ((crc
>> 24) & 0xFF);
202 pktout
.data
[biglen
+1] = (unsigned char) ((crc
>> 16) & 0xFF);
203 pktout
.data
[biglen
+2] = (unsigned char) ((crc
>> 8) & 0xFF);
204 pktout
.data
[biglen
+3] = (unsigned char) (crc
& 0xFF);
206 pktout
.data
[0] = (len
>> 24) & 0xFF;
207 pktout
.data
[1] = (len
>> 16) & 0xFF;
208 pktout
.data
[2] = (len
>> 8) & 0xFF;
209 pktout
.data
[3] = len
& 0xFF;
212 cipher
->encrypt(pktout
.data
+4, biglen
);
214 s_write(pktout
.data
, biglen
+4);
217 static int do_ssh_init(void) {
225 while (s_read(&c
, 1) == 1) {
226 if (c
== 'S' && i
< 2) i
++;
227 else if (c
== 'S' && i
== 2) i
= 2;
228 else if (c
== 'H' && i
== 2) break;
232 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
233 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
234 if (s_read(&c
,1) != 1 || c
!= 'H') return 0;
236 if (s_read(&c
,1) != 1 || c
!= '-') return 0;
239 if (s_read(&c
,1) != 1)
245 } else if (i
< sizeof(version
)-1)
252 sprintf(vstring
, "SSH-%s-7.7.7\n",
253 (strcmp(version
, "1.5") <= 0 ? version
: "1.5"));
254 s_write(vstring
, strlen(vstring
));
258 static void ssh_protocol(unsigned char *in
, int inlen
, int ispkt
) {
260 unsigned char session_id
[16];
261 unsigned char *rsabuf
, *keystr1
, *keystr2
;
262 unsigned char cookie
[8];
263 struct RSAKey servkey
, hostkey
;
264 struct MD5Context md5c
;
265 unsigned long supported_ciphers_mask
;
268 extern struct ssh_cipher ssh_3des
;
269 extern struct ssh_cipher ssh_blowfish
;
279 fatalbox("Public key packet not received");
281 memcpy(cookie
, pktin
.body
, 8);
285 i
= makekey(pktin
.body
+8, &servkey
, &keystr1
);
287 j
= makekey(pktin
.body
+8+i
, &hostkey
, &keystr2
);
289 supported_ciphers_mask
= (pktin
.body
[12+i
+j
] << 24) |
290 (pktin
.body
[13+i
+j
] << 16) |
291 (pktin
.body
[14+i
+j
] << 8) |
292 (pktin
.body
[15+i
+j
]);
294 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
295 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
296 MD5Update(&md5c
, pktin
.body
, 8);
298 MD5Final(session_id
, &md5c
);
301 session_key
[i
] = random_byte();
303 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
305 rsabuf
= malloc(len
);
307 fatalbox("Out of memory");
309 verify_ssh_host_key(savedhost
, &hostkey
);
311 for (i
=0; i
<32; i
++) {
312 rsabuf
[i
] = session_key
[i
];
314 rsabuf
[i
] ^= session_id
[i
];
317 if (hostkey
.bytes
> servkey
.bytes
) {
318 rsaencrypt(rsabuf
, 32, &servkey
);
319 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
321 rsaencrypt(rsabuf
, 32, &hostkey
);
322 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
325 cipher_type
= cfg
.cipher
== CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH
:
327 if ((supported_ciphers_mask
& (1 << cipher_type
)) == 0) {
328 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
329 cipher_type
= SSH_CIPHER_3DES
;
332 s_wrpkt_start(3, len
+15);
333 pktout
.body
[0] = cipher_type
;
334 memcpy(pktout
.body
+1, cookie
, 8);
335 pktout
.body
[9] = (len
*8) >> 8;
336 pktout
.body
[10] = (len
*8) & 0xFF;
337 memcpy(pktout
.body
+11, rsabuf
, len
);
338 pktout
.body
[len
+11] = pktout
.body
[len
+12] = 0; /* protocol flags */
339 pktout
.body
[len
+13] = pktout
.body
[len
+14] = 0;
344 cipher
= cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish
:
346 cipher
->sesskey(session_key
);
348 do { crReturnV
; } while (!ispkt
);
350 if (pktin
.type
!= 14)
351 fatalbox("Encryption not successfully enabled");
355 static char username
[100];
358 if (!*cfg
.username
) {
359 c_write("login as: ", 10);
361 do { crReturnV
; } while (ispkt
);
362 while (inlen
--) switch (c
= *in
++) {
384 if (c
>= ' ' && c
<= '~' && pos
< 40) {
392 username
[strcspn(username
, "\n\r")] = '\0';
395 strncpy(username
, cfg
.username
, 99);
397 sprintf(stuff
, "Sent username \"%s\".\r\n", username
);
398 c_write(stuff
, strlen(stuff
));
400 s_wrpkt_start(4, 4+strlen(username
));
401 pktout
.body
[0] = pktout
.body
[1] = pktout
.body
[2] = 0;
402 pktout
.body
[3] = strlen(username
);
403 memcpy(pktout
.body
+4, username
, strlen(username
));
407 do { crReturnV
; } while (!ispkt
);
409 while (pktin
.type
== 15) {
410 static char password
[100];
413 c_write("password: ", 10);
416 do { crReturnV
; } while (ispkt
);
417 while (inlen
--) switch (c
= *in
++) {
434 if (c
>= ' ' && c
<= '~' && pos
< 40)
440 s_wrpkt_start(9, 4+strlen(password
));
441 pktout
.body
[0] = pktout
.body
[1] = pktout
.body
[2] = 0;
442 pktout
.body
[3] = strlen(password
);
443 memcpy(pktout
.body
+4, password
, strlen(password
));
445 memset(password
, 0, strlen(password
));
446 do { crReturnV
; } while (!ispkt
);
447 if (pktin
.type
== 15) {
448 c_write("Access denied\r\n", 15);
449 } else if (pktin
.type
!= 14) {
450 fatalbox("Strange packet received, type %d", pktin
.type
);
455 i
= strlen(cfg
.termtype
);
456 s_wrpkt_start(10, i
+5*4+1);
457 pktout
.body
[0] = (i
>> 24) & 0xFF;
458 pktout
.body
[1] = (i
>> 16) & 0xFF;
459 pktout
.body
[2] = (i
>> 8) & 0xFF;
460 pktout
.body
[3] = i
& 0xFF;
461 memcpy(pktout
.body
+4, cfg
.termtype
, i
);
463 pktout
.body
[i
++] = (rows
>> 24) & 0xFF;
464 pktout
.body
[i
++] = (rows
>> 16) & 0xFF;
465 pktout
.body
[i
++] = (rows
>> 8) & 0xFF;
466 pktout
.body
[i
++] = rows
& 0xFF;
467 pktout
.body
[i
++] = (cols
>> 24) & 0xFF;
468 pktout
.body
[i
++] = (cols
>> 16) & 0xFF;
469 pktout
.body
[i
++] = (cols
>> 8) & 0xFF;
470 pktout
.body
[i
++] = cols
& 0xFF;
471 memset(pktout
.body
+i
, 0, 9); /* 0 pixwidth, 0 pixheight, 0.b endofopt */
473 ssh_state
= SSH_STATE_INTERMED
;
474 do { crReturnV
; } while (!ispkt
);
475 if (pktin
.type
!= 14 && pktin
.type
!= 15) {
476 fatalbox("Protocol confusion");
477 } else if (pktin
.type
== 15) {
478 c_write("Server refused to allocate pty\r\n", 32);
482 s_wrpkt_start(12, 0);
485 ssh_state
= SSH_STATE_SESSION
;
492 if (pktin
.type
== 17 || pktin
.type
== 18) {
494 for (i
= 0; i
< 4; i
++)
495 len
= (len
<< 8) + pktin
.body
[i
];
496 c_write(pktin
.body
+4, len
);
497 } else if (pktin
.type
== 1) {
498 /* SSH_MSG_DISCONNECT */
499 ssh_state
= SSH_STATE_CLOSED
;
500 } else if (pktin
.type
== 14) {
501 /* SSH_MSG_SUCCESS: may be from EXEC_SHELL on some servers */
502 } else if (pktin
.type
== 15) {
503 /* SSH_MSG_FAILURE: may be from EXEC_SHELL on some servers
504 * if no pty is available or in other odd cases. Ignore */
505 } else if (pktin
.type
== 20) {
507 s_wrpkt_start(33, 0);
510 fatalbox("Strange packet received: type %d", pktin
.type
);
513 s_wrpkt_start(16, 4+inlen
);
514 pktout
.body
[0] = (inlen
>> 24) & 0xFF;
515 pktout
.body
[1] = (inlen
>> 16) & 0xFF;
516 pktout
.body
[2] = (inlen
>> 8) & 0xFF;
517 pktout
.body
[3] = inlen
& 0xFF;
518 memcpy(pktout
.body
+4, in
, inlen
);
527 * Called to set up the connection. Will arrange for WM_NETEVENT
528 * messages to be passed to the specified window, whose window
529 * procedure should then call telnet_msg().
531 * Returns an error message, or NULL on success.
533 * Also places the canonical host name into `realhost'.
535 static char *ssh_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
544 savedhost
= malloc(1+strlen(host
));
546 fatalbox("Out of memory");
547 strcpy(savedhost
, host
);
559 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
560 if ( (h
= gethostbyname(host
)) == NULL
)
561 switch (WSAGetLastError()) {
562 case WSAENETDOWN
: return "Network is down";
563 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
564 return "Host does not exist";
565 case WSATRY_AGAIN
: return "Host not found";
566 default: return "gethostbyname: unknown error";
568 memcpy (&a
, h
->h_addr
, sizeof(a
));
569 *realhost
= h
->h_name
;
578 port
= 22; /* default ssh port */
583 s
= socket(AF_INET
, SOCK_STREAM
, 0);
584 if (s
== INVALID_SOCKET
)
585 switch (WSAGetLastError()) {
586 case WSAENETDOWN
: return "Network is down";
587 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
588 default: return "socket(): unknown error";
592 * Bind to local address.
594 addr
.sin_family
= AF_INET
;
595 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
596 addr
.sin_port
= htons(0);
597 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
598 switch (WSAGetLastError()) {
599 case WSAENETDOWN
: return "Network is down";
600 default: return "bind(): unknown error";
604 * Connect to remote address.
606 addr
.sin_addr
.s_addr
= htonl(a
);
607 addr
.sin_port
= htons((short)port
);
608 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
609 switch (WSAGetLastError()) {
610 case WSAENETDOWN
: return "Network is down";
611 case WSAECONNREFUSED
: return "Connection refused";
612 case WSAENETUNREACH
: return "Network is unreachable";
613 case WSAEHOSTUNREACH
: return "No route to host";
614 default: return "connect(): unknown error";
618 send(s
, "connect ", 8, 0);
619 send(s
, FWhost
, strlen(FWhost
), 0);
622 sprintf(buf
, " %d\n", FWport
);
623 send (s
, buf
, strlen(buf
), 0);
628 return "Protocol initialisation error";
630 if (WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
631 switch (WSAGetLastError()) {
632 case WSAENETDOWN
: return "Network is down";
633 default: return "WSAAsyncSelect(): unknown error";
640 * Process a WM_NETEVENT message. Will return 0 if the connection
641 * has closed, or <0 for a socket error.
643 static int ssh_msg (WPARAM wParam
, LPARAM lParam
) {
647 if (s
== INVALID_SOCKET
) /* how the hell did we get here?! */
650 if (WSAGETSELECTERROR(lParam
) != 0)
651 return -WSAGETSELECTERROR(lParam
);
653 switch (WSAGETSELECTEVENT(lParam
)) {
655 ret
= recv(s
, buf
, sizeof(buf
), 0);
656 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
658 if (ret
< 0) /* any _other_ error */
659 return -10000-WSAGetLastError();
662 return 0; /* can't happen, in theory */
664 ssh_gotdata (buf
, ret
);
668 ssh_state
= SSH_STATE_CLOSED
;
671 return 1; /* shouldn't happen, but WTF */
675 * Called to send data down the Telnet connection.
677 static void ssh_send (char *buf
, int len
) {
678 if (s
== INVALID_SOCKET
)
681 ssh_protocol(buf
, len
, 0);
685 * Called to set the size of the window from Telnet's POV.
687 static void ssh_size(void) {
689 case SSH_STATE_BEFORE_SIZE
:
690 case SSH_STATE_CLOSED
:
691 break; /* do nothing */
692 case SSH_STATE_INTERMED
:
693 size_needed
= TRUE
; /* buffer for later */
695 case SSH_STATE_SESSION
:
697 s_wrpkt_start(11, 16);
698 pktout
.body
[0] = (rows
>> 24) & 0xFF;
699 pktout
.body
[1] = (rows
>> 16) & 0xFF;
700 pktout
.body
[2] = (rows
>> 8) & 0xFF;
701 pktout
.body
[3] = rows
& 0xFF;
702 pktout
.body
[4] = (cols
>> 24) & 0xFF;
703 pktout
.body
[5] = (cols
>> 16) & 0xFF;
704 pktout
.body
[6] = (cols
>> 8) & 0xFF;
705 pktout
.body
[7] = cols
& 0xFF;
706 memset(pktout
.body
+8, 0, 8);
713 * (Send Telnet special codes)
715 static void ssh_special (Telnet_Special code
) {
719 Backend ssh_backend
= {