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
;
266 extern struct ssh_cipher ssh_3des
;
276 fatalbox("Public key packet not received");
278 memcpy(cookie
, pktin
.body
, 8);
282 i
= makekey(pktin
.body
+8, &servkey
, &keystr1
);
284 j
= makekey(pktin
.body
+8+i
, &hostkey
, &keystr2
);
286 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
287 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
288 MD5Update(&md5c
, pktin
.body
, 8);
290 MD5Final(session_id
, &md5c
);
293 session_key
[i
] = random_byte();
295 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
297 rsabuf
= malloc(len
);
299 fatalbox("Out of memory");
301 verify_ssh_host_key(savedhost
, &hostkey
);
303 for (i
=0; i
<32; i
++) {
304 rsabuf
[i
] = session_key
[i
];
306 rsabuf
[i
] ^= session_id
[i
];
309 if (hostkey
.bytes
> servkey
.bytes
) {
310 rsaencrypt(rsabuf
, 32, &servkey
);
311 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
313 rsaencrypt(rsabuf
, 32, &hostkey
);
314 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
317 s_wrpkt_start(3, len
+15);
318 pktout
.body
[0] = 3; /* SSH_CIPHER_3DES */
319 memcpy(pktout
.body
+1, cookie
, 8);
320 pktout
.body
[9] = (len
*8) >> 8;
321 pktout
.body
[10] = (len
*8) & 0xFF;
322 memcpy(pktout
.body
+11, rsabuf
, len
);
323 pktout
.body
[len
+11] = pktout
.body
[len
+12] = 0; /* protocol flags */
324 pktout
.body
[len
+13] = pktout
.body
[len
+14] = 0;
330 cipher
->sesskey(session_key
);
332 do { crReturnV
; } while (!ispkt
);
334 if (pktin
.type
!= 14)
335 fatalbox("Encryption not successfully enabled");
339 static char username
[100];
342 if (!*cfg
.username
) {
343 c_write("login as: ", 10);
345 do { crReturnV
; } while (ispkt
);
346 while (inlen
--) switch (c
= *in
++) {
368 if (c
>= ' ' && c
<= '~' && pos
< 40) {
376 username
[strcspn(username
, "\n\r")] = '\0';
379 strncpy(username
, cfg
.username
, 99);
381 sprintf(stuff
, "Sent username \"%s\".\r\n", username
);
382 c_write(stuff
, strlen(stuff
));
384 s_wrpkt_start(4, 4+strlen(username
));
385 pktout
.body
[0] = pktout
.body
[1] = pktout
.body
[2] = 0;
386 pktout
.body
[3] = strlen(username
);
387 memcpy(pktout
.body
+4, username
, strlen(username
));
391 do { crReturnV
; } while (!ispkt
);
393 while (pktin
.type
== 15) {
394 static char password
[100];
397 c_write("password: ", 10);
400 do { crReturnV
; } while (ispkt
);
401 while (inlen
--) switch (c
= *in
++) {
418 if (c
>= ' ' && c
<= '~' && pos
< 40)
424 s_wrpkt_start(9, 4+strlen(password
));
425 pktout
.body
[0] = pktout
.body
[1] = pktout
.body
[2] = 0;
426 pktout
.body
[3] = strlen(password
);
427 memcpy(pktout
.body
+4, password
, strlen(password
));
429 memset(password
, 0, strlen(password
));
430 do { crReturnV
; } while (!ispkt
);
431 if (pktin
.type
== 15) {
432 c_write("Access denied\r\n", 15);
433 } else if (pktin
.type
!= 14) {
434 fatalbox("Strange packet received, type %d", pktin
.type
);
439 i
= strlen(cfg
.termtype
);
440 s_wrpkt_start(10, i
+5*4+1);
441 pktout
.body
[0] = (i
>> 24) & 0xFF;
442 pktout
.body
[1] = (i
>> 16) & 0xFF;
443 pktout
.body
[2] = (i
>> 8) & 0xFF;
444 pktout
.body
[3] = i
& 0xFF;
445 memcpy(pktout
.body
+4, cfg
.termtype
, i
);
447 pktout
.body
[i
++] = (rows
>> 24) & 0xFF;
448 pktout
.body
[i
++] = (rows
>> 16) & 0xFF;
449 pktout
.body
[i
++] = (rows
>> 8) & 0xFF;
450 pktout
.body
[i
++] = rows
& 0xFF;
451 pktout
.body
[i
++] = (cols
>> 24) & 0xFF;
452 pktout
.body
[i
++] = (cols
>> 16) & 0xFF;
453 pktout
.body
[i
++] = (cols
>> 8) & 0xFF;
454 pktout
.body
[i
++] = cols
& 0xFF;
455 memset(pktout
.body
+i
, 0, 9); /* 0 pixwidth, 0 pixheight, 0.b endofopt */
457 ssh_state
= SSH_STATE_INTERMED
;
458 do { crReturnV
; } while (!ispkt
);
459 if (pktin
.type
!= 14 && pktin
.type
!= 15) {
460 fatalbox("Protocol confusion");
461 } else if (pktin
.type
== 15) {
462 c_write("Server refused to allocate pty\r\n", 32);
466 s_wrpkt_start(12, 0);
469 ssh_state
= SSH_STATE_SESSION
;
476 if (pktin
.type
== 17 || pktin
.type
== 18) {
478 for (i
= 0; i
< 4; i
++)
479 len
= (len
<< 8) + pktin
.body
[i
];
480 c_write(pktin
.body
+4, len
);
481 } else if (pktin
.type
== 1) {
482 /* SSH_MSG_DISCONNECT */
483 ssh_state
= SSH_STATE_CLOSED
;
484 } else if (pktin
.type
== 14) {
485 /* SSH_MSG_SUCCESS: may be from EXEC_SHELL on some servers */
486 } else if (pktin
.type
== 15) {
487 /* SSH_MSG_FAILURE: may be from EXEC_SHELL on some servers
488 * if no pty is available or in other odd cases. Ignore */
489 } else if (pktin
.type
== 20) {
491 s_wrpkt_start(33, 0);
494 fatalbox("Strange packet received: type %d", pktin
.type
);
497 s_wrpkt_start(16, 4+inlen
);
498 pktout
.body
[0] = (inlen
>> 24) & 0xFF;
499 pktout
.body
[1] = (inlen
>> 16) & 0xFF;
500 pktout
.body
[2] = (inlen
>> 8) & 0xFF;
501 pktout
.body
[3] = inlen
& 0xFF;
502 memcpy(pktout
.body
+4, in
, inlen
);
511 * Called to set up the connection. Will arrange for WM_NETEVENT
512 * messages to be passed to the specified window, whose window
513 * procedure should then call telnet_msg().
515 * Returns an error message, or NULL on success.
517 * Also places the canonical host name into `realhost'.
519 static char *ssh_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
528 savedhost
= malloc(1+strlen(host
));
530 fatalbox("Out of memory");
531 strcpy(savedhost
, host
);
543 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
544 if ( (h
= gethostbyname(host
)) == NULL
)
545 switch (WSAGetLastError()) {
546 case WSAENETDOWN
: return "Network is down";
547 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
548 return "Host does not exist";
549 case WSATRY_AGAIN
: return "Host not found";
550 default: return "gethostbyname: unknown error";
552 memcpy (&a
, h
->h_addr
, sizeof(a
));
553 *realhost
= h
->h_name
;
562 port
= 22; /* default ssh port */
567 s
= socket(AF_INET
, SOCK_STREAM
, 0);
568 if (s
== INVALID_SOCKET
)
569 switch (WSAGetLastError()) {
570 case WSAENETDOWN
: return "Network is down";
571 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
572 default: return "socket(): unknown error";
576 * Bind to local address.
578 addr
.sin_family
= AF_INET
;
579 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
580 addr
.sin_port
= htons(0);
581 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
582 switch (WSAGetLastError()) {
583 case WSAENETDOWN
: return "Network is down";
584 default: return "bind(): unknown error";
588 * Connect to remote address.
590 addr
.sin_addr
.s_addr
= htonl(a
);
591 addr
.sin_port
= htons((short)port
);
592 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
593 switch (WSAGetLastError()) {
594 case WSAENETDOWN
: return "Network is down";
595 case WSAECONNREFUSED
: return "Connection refused";
596 case WSAENETUNREACH
: return "Network is unreachable";
597 case WSAEHOSTUNREACH
: return "No route to host";
598 default: return "connect(): unknown error";
602 send(s
, "connect ", 8, 0);
603 send(s
, FWhost
, strlen(FWhost
), 0);
606 sprintf(buf
, " %d\n", FWport
);
607 send (s
, buf
, strlen(buf
), 0);
612 return "Protocol initialisation error";
614 if (WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
615 switch (WSAGetLastError()) {
616 case WSAENETDOWN
: return "Network is down";
617 default: return "WSAAsyncSelect(): unknown error";
624 * Process a WM_NETEVENT message. Will return 0 if the connection
625 * has closed, or <0 for a socket error.
627 static int ssh_msg (WPARAM wParam
, LPARAM lParam
) {
631 if (s
== INVALID_SOCKET
) /* how the hell did we get here?! */
634 if (WSAGETSELECTERROR(lParam
) != 0)
635 return -WSAGETSELECTERROR(lParam
);
637 switch (WSAGETSELECTEVENT(lParam
)) {
639 ret
= recv(s
, buf
, sizeof(buf
), 0);
640 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
642 if (ret
< 0) /* any _other_ error */
643 return -10000-WSAGetLastError();
646 return 0; /* can't happen, in theory */
648 ssh_gotdata (buf
, ret
);
652 ssh_state
= SSH_STATE_CLOSED
;
655 return 1; /* shouldn't happen, but WTF */
659 * Called to send data down the Telnet connection.
661 static void ssh_send (char *buf
, int len
) {
662 if (s
== INVALID_SOCKET
)
665 ssh_protocol(buf
, len
, 0);
669 * Called to set the size of the window from Telnet's POV.
671 static void ssh_size(void) {
673 case SSH_STATE_BEFORE_SIZE
:
674 case SSH_STATE_CLOSED
:
675 break; /* do nothing */
676 case SSH_STATE_INTERMED
:
677 size_needed
= TRUE
; /* buffer for later */
679 case SSH_STATE_SESSION
:
681 s_wrpkt_start(11, 16);
682 pktout
.body
[0] = (rows
>> 24) & 0xFF;
683 pktout
.body
[1] = (rows
>> 16) & 0xFF;
684 pktout
.body
[2] = (rows
>> 8) & 0xFF;
685 pktout
.body
[3] = rows
& 0xFF;
686 pktout
.body
[4] = (cols
>> 24) & 0xFF;
687 pktout
.body
[5] = (cols
>> 16) & 0xFF;
688 pktout
.body
[6] = (cols
>> 8) & 0xFF;
689 pktout
.body
[7] = cols
& 0xFF;
690 memset(pktout
.body
+8, 0, 8);
697 * (Send Telnet special codes)
699 static void ssh_special (Telnet_Special code
) {
703 Backend ssh_backend
= {