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 do_ssh_init(void) {
224 while (s_read(&c
, 1) == 1) {
225 if (c
== 'S' && i
< 2) i
++;
226 else if (c
== 'S' && i
== 2) i
= 2;
227 else if (c
== 'H' && i
== 2) break;
231 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
232 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
233 if (s_read(&c
,1) != 1 || c
!= 'H') return 0;
235 if (s_read(&c
,1) != 1 || c
!= '-') return 0;
238 if (s_read(&c
,1) != 1)
244 } else if (i
< sizeof(version
)-1)
251 sprintf(vstring
, "SSH-%s-7.7.7\n",
252 (strcmp(version
, "1.5") <= 0 ? version
: "1.5"));
253 s_write(vstring
, strlen(vstring
));
257 static void ssh_protocol(unsigned char *in
, int inlen
, int ispkt
) {
259 unsigned char session_id
[16];
260 unsigned char *rsabuf
, *keystr1
, *keystr2
;
261 unsigned char cookie
[8];
262 struct RSAKey servkey
, hostkey
;
263 struct MD5Context md5c
;
264 unsigned long supported_ciphers_mask
;
267 extern struct ssh_cipher ssh_3des
;
268 extern struct ssh_cipher ssh_blowfish
;
278 fatalbox("Public key packet not received");
280 memcpy(cookie
, pktin
.body
, 8);
284 i
= makekey(pktin
.body
+8, &servkey
, &keystr1
);
286 j
= makekey(pktin
.body
+8+i
, &hostkey
, &keystr2
);
288 supported_ciphers_mask
= (pktin
.body
[12+i
+j
] << 24) |
289 (pktin
.body
[13+i
+j
] << 16) |
290 (pktin
.body
[14+i
+j
] << 8) |
291 (pktin
.body
[15+i
+j
]);
293 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
294 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
295 MD5Update(&md5c
, pktin
.body
, 8);
297 MD5Final(session_id
, &md5c
);
300 session_key
[i
] = random_byte();
302 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
304 rsabuf
= malloc(len
);
306 fatalbox("Out of memory");
308 verify_ssh_host_key(savedhost
, &hostkey
);
310 for (i
=0; i
<32; i
++) {
311 rsabuf
[i
] = session_key
[i
];
313 rsabuf
[i
] ^= session_id
[i
];
316 if (hostkey
.bytes
> servkey
.bytes
) {
317 rsaencrypt(rsabuf
, 32, &servkey
);
318 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
320 rsaencrypt(rsabuf
, 32, &hostkey
);
321 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
324 cipher_type
= cfg
.cipher
== CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH
:
326 if ((supported_ciphers_mask
& (1 << cipher_type
)) == 0) {
327 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
328 cipher_type
= SSH_CIPHER_3DES
;
331 s_wrpkt_start(3, len
+15);
332 pktout
.body
[0] = cipher_type
;
333 memcpy(pktout
.body
+1, cookie
, 8);
334 pktout
.body
[9] = (len
*8) >> 8;
335 pktout
.body
[10] = (len
*8) & 0xFF;
336 memcpy(pktout
.body
+11, rsabuf
, len
);
337 pktout
.body
[len
+11] = pktout
.body
[len
+12] = 0; /* protocol flags */
338 pktout
.body
[len
+13] = pktout
.body
[len
+14] = 0;
343 cipher
= cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish
:
345 cipher
->sesskey(session_key
);
347 do { crReturnV
; } while (!ispkt
);
349 if (pktin
.type
!= 14)
350 fatalbox("Encryption not successfully enabled");
354 static char username
[100];
357 if (!*cfg
.username
) {
358 c_write("login as: ", 10);
360 do { crReturnV
; } while (ispkt
);
361 while (inlen
--) switch (c
= *in
++) {
383 if (c
>= ' ' && c
<= '~' && pos
< 40) {
391 username
[strcspn(username
, "\n\r")] = '\0';
394 strncpy(username
, cfg
.username
, 99);
396 sprintf(stuff
, "Sent username \"%s\".\r\n", username
);
397 c_write(stuff
, strlen(stuff
));
399 s_wrpkt_start(4, 4+strlen(username
));
400 pktout
.body
[0] = pktout
.body
[1] = pktout
.body
[2] = 0;
401 pktout
.body
[3] = strlen(username
);
402 memcpy(pktout
.body
+4, username
, strlen(username
));
406 do { crReturnV
; } while (!ispkt
);
408 while (pktin
.type
== 15) {
409 static char password
[100];
412 c_write("password: ", 10);
415 do { crReturnV
; } while (ispkt
);
416 while (inlen
--) switch (c
= *in
++) {
433 if (c
>= ' ' && c
<= '~' && pos
< 40)
439 s_wrpkt_start(9, 4+strlen(password
));
440 pktout
.body
[0] = pktout
.body
[1] = pktout
.body
[2] = 0;
441 pktout
.body
[3] = strlen(password
);
442 memcpy(pktout
.body
+4, password
, strlen(password
));
444 memset(password
, 0, strlen(password
));
445 do { crReturnV
; } while (!ispkt
);
446 if (pktin
.type
== 15) {
447 c_write("Access denied\r\n", 15);
448 } else if (pktin
.type
!= 14) {
449 fatalbox("Strange packet received, type %d", pktin
.type
);
454 i
= strlen(cfg
.termtype
);
455 s_wrpkt_start(10, i
+5*4+1);
456 pktout
.body
[0] = (i
>> 24) & 0xFF;
457 pktout
.body
[1] = (i
>> 16) & 0xFF;
458 pktout
.body
[2] = (i
>> 8) & 0xFF;
459 pktout
.body
[3] = i
& 0xFF;
460 memcpy(pktout
.body
+4, cfg
.termtype
, i
);
462 pktout
.body
[i
++] = (rows
>> 24) & 0xFF;
463 pktout
.body
[i
++] = (rows
>> 16) & 0xFF;
464 pktout
.body
[i
++] = (rows
>> 8) & 0xFF;
465 pktout
.body
[i
++] = rows
& 0xFF;
466 pktout
.body
[i
++] = (cols
>> 24) & 0xFF;
467 pktout
.body
[i
++] = (cols
>> 16) & 0xFF;
468 pktout
.body
[i
++] = (cols
>> 8) & 0xFF;
469 pktout
.body
[i
++] = cols
& 0xFF;
470 memset(pktout
.body
+i
, 0, 9); /* 0 pixwidth, 0 pixheight, 0.b endofopt */
472 ssh_state
= SSH_STATE_INTERMED
;
473 do { crReturnV
; } while (!ispkt
);
474 if (pktin
.type
!= 14 && pktin
.type
!= 15) {
475 fatalbox("Protocol confusion");
476 } else if (pktin
.type
== 15) {
477 c_write("Server refused to allocate pty\r\n", 32);
481 s_wrpkt_start(12, 0);
484 ssh_state
= SSH_STATE_SESSION
;
491 if (pktin
.type
== 17 || pktin
.type
== 18) {
493 for (i
= 0; i
< 4; i
++)
494 len
= (len
<< 8) + pktin
.body
[i
];
495 c_write(pktin
.body
+4, len
);
496 } else if (pktin
.type
== 1) {
497 /* SSH_MSG_DISCONNECT */
498 ssh_state
= SSH_STATE_CLOSED
;
499 } else if (pktin
.type
== 14) {
500 /* SSH_MSG_SUCCESS: may be from EXEC_SHELL on some servers */
501 } else if (pktin
.type
== 15) {
502 /* SSH_MSG_FAILURE: may be from EXEC_SHELL on some servers
503 * if no pty is available or in other odd cases. Ignore */
504 } else if (pktin
.type
== 20) {
506 s_wrpkt_start(33, 0);
509 fatalbox("Strange packet received: type %d", pktin
.type
);
512 s_wrpkt_start(16, 4+inlen
);
513 pktout
.body
[0] = (inlen
>> 24) & 0xFF;
514 pktout
.body
[1] = (inlen
>> 16) & 0xFF;
515 pktout
.body
[2] = (inlen
>> 8) & 0xFF;
516 pktout
.body
[3] = inlen
& 0xFF;
517 memcpy(pktout
.body
+4, in
, inlen
);
526 * Called to set up the connection. Will arrange for WM_NETEVENT
527 * messages to be passed to the specified window, whose window
528 * procedure should then call telnet_msg().
530 * Returns an error message, or NULL on success.
532 * Also places the canonical host name into `realhost'.
534 static char *ssh_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
543 savedhost
= malloc(1+strlen(host
));
545 fatalbox("Out of memory");
546 strcpy(savedhost
, host
);
558 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
559 if ( (h
= gethostbyname(host
)) == NULL
)
560 switch (WSAGetLastError()) {
561 case WSAENETDOWN
: return "Network is down";
562 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
563 return "Host does not exist";
564 case WSATRY_AGAIN
: return "Host not found";
565 default: return "gethostbyname: unknown error";
567 memcpy (&a
, h
->h_addr
, sizeof(a
));
568 *realhost
= h
->h_name
;
577 port
= 22; /* default ssh port */
582 s
= socket(AF_INET
, SOCK_STREAM
, 0);
583 if (s
== INVALID_SOCKET
)
584 switch (WSAGetLastError()) {
585 case WSAENETDOWN
: return "Network is down";
586 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
587 default: return "socket(): unknown error";
591 * Bind to local address.
593 addr
.sin_family
= AF_INET
;
594 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
595 addr
.sin_port
= htons(0);
596 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
597 switch (WSAGetLastError()) {
598 case WSAENETDOWN
: return "Network is down";
599 default: return "bind(): unknown error";
603 * Connect to remote address.
605 addr
.sin_addr
.s_addr
= htonl(a
);
606 addr
.sin_port
= htons((short)port
);
607 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
608 switch (WSAGetLastError()) {
609 case WSAENETDOWN
: return "Network is down";
610 case WSAECONNREFUSED
: return "Connection refused";
611 case WSAENETUNREACH
: return "Network is unreachable";
612 case WSAEHOSTUNREACH
: return "No route to host";
613 default: return "connect(): unknown error";
617 send(s
, "connect ", 8, 0);
618 send(s
, FWhost
, strlen(FWhost
), 0);
621 sprintf(buf
, " %d\n", FWport
);
622 send (s
, buf
, strlen(buf
), 0);
627 return "Protocol initialisation error";
629 if (WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
630 switch (WSAGetLastError()) {
631 case WSAENETDOWN
: return "Network is down";
632 default: return "WSAAsyncSelect(): unknown error";
639 * Process a WM_NETEVENT message. Will return 0 if the connection
640 * has closed, or <0 for a socket error.
642 static int ssh_msg (WPARAM wParam
, LPARAM lParam
) {
646 if (s
== INVALID_SOCKET
) /* how the hell did we get here?! */
649 if (WSAGETSELECTERROR(lParam
) != 0)
650 return -WSAGETSELECTERROR(lParam
);
652 switch (WSAGETSELECTEVENT(lParam
)) {
654 ret
= recv(s
, buf
, sizeof(buf
), 0);
655 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
657 if (ret
< 0) /* any _other_ error */
658 return -10000-WSAGetLastError();
661 return 0; /* can't happen, in theory */
663 ssh_gotdata (buf
, ret
);
667 ssh_state
= SSH_STATE_CLOSED
;
670 return 1; /* shouldn't happen, but WTF */
674 * Called to send data down the Telnet connection.
676 static void ssh_send (char *buf
, int len
) {
677 if (s
== INVALID_SOCKET
)
680 ssh_protocol(buf
, len
, 0);
684 * Called to set the size of the window from Telnet's POV.
686 static void ssh_size(void) {
688 case SSH_STATE_BEFORE_SIZE
:
689 case SSH_STATE_CLOSED
:
690 break; /* do nothing */
691 case SSH_STATE_INTERMED
:
692 size_needed
= TRUE
; /* buffer for later */
694 case SSH_STATE_SESSION
:
696 s_wrpkt_start(11, 16);
697 pktout
.body
[0] = (rows
>> 24) & 0xFF;
698 pktout
.body
[1] = (rows
>> 16) & 0xFF;
699 pktout
.body
[2] = (rows
>> 8) & 0xFF;
700 pktout
.body
[3] = rows
& 0xFF;
701 pktout
.body
[4] = (cols
>> 24) & 0xFF;
702 pktout
.body
[5] = (cols
>> 16) & 0xFF;
703 pktout
.body
[6] = (cols
>> 8) & 0xFF;
704 pktout
.body
[7] = cols
& 0xFF;
705 memset(pktout
.body
+8, 0, 8);
712 * (Send Telnet special codes)
714 static void ssh_special (Telnet_Special code
) {
718 Backend ssh_backend
= {