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 */
160 ssh_protocol(NULL
, 0, 1);
165 static void s_wrpkt_start(int type
, int len
) {
168 len
+= 5; /* type and CRC */
172 pktout
.length
= len
-5;
173 if (pktout
.maxlen
< biglen
) {
174 pktout
.maxlen
= biglen
;
175 pktout
.data
= (pktout
.data
== NULL ?
malloc(biglen
+4) :
176 realloc(pktout
.data
, biglen
+4));
178 fatalbox("Out of memory");
182 pktout
.body
= pktout
.data
+4+pad
+1;
185 static void s_wrpkt(void) {
186 int pad
, len
, biglen
, i
;
189 len
= pktout
.length
+ 5; /* type and CRC */
193 pktout
.body
[-1] = pktout
.type
;
194 for (i
=0; i
<pad
; i
++)
195 pktout
.data
[i
+4] = random_byte();
196 crc
= crc32(pktout
.data
+4, biglen
-4);
198 pktout
.data
[biglen
+0] = (unsigned char) ((crc
>> 24) & 0xFF);
199 pktout
.data
[biglen
+1] = (unsigned char) ((crc
>> 16) & 0xFF);
200 pktout
.data
[biglen
+2] = (unsigned char) ((crc
>> 8) & 0xFF);
201 pktout
.data
[biglen
+3] = (unsigned char) (crc
& 0xFF);
203 pktout
.data
[0] = (len
>> 24) & 0xFF;
204 pktout
.data
[1] = (len
>> 16) & 0xFF;
205 pktout
.data
[2] = (len
>> 8) & 0xFF;
206 pktout
.data
[3] = len
& 0xFF;
209 cipher
->encrypt(pktout
.data
+4, biglen
);
211 s_write(pktout
.data
, biglen
+4);
214 static int do_ssh_init(void) {
222 while (s_read(&c
, 1) == 1) {
223 if (c
== 'S' && i
< 2) i
++;
224 else if (c
== 'S' && i
== 2) i
= 2;
225 else if (c
== 'H' && i
== 2) break;
229 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
230 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
231 if (s_read(&c
,1) != 1 || c
!= 'H') return 0;
233 if (s_read(&c
,1) != 1 || c
!= '-') return 0;
236 if (s_read(&c
,1) != 1)
242 } else if (i
< sizeof(version
)-1)
249 sprintf(vstring
, "SSH-%s-7.7.7\n",
250 (strcmp(version
, "1.5") <= 0 ? version
: "1.5"));
251 s_write(vstring
, strlen(vstring
));
255 static void ssh_protocol(unsigned char *in
, int inlen
, int ispkt
) {
257 unsigned char session_id
[16];
258 unsigned char *rsabuf
, *keystr1
, *keystr2
;
259 unsigned char cookie
[8];
260 struct RSAKey servkey
, hostkey
;
261 struct MD5Context md5c
;
262 unsigned long supported_ciphers_mask
;
265 extern struct ssh_cipher ssh_3des
;
266 extern struct ssh_cipher ssh_blowfish
;
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 supported_ciphers_mask
= (pktin
.body
[12+i
+j
] << 24) |
287 (pktin
.body
[13+i
+j
] << 16) |
288 (pktin
.body
[14+i
+j
] << 8) |
289 (pktin
.body
[15+i
+j
]);
291 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
292 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
293 MD5Update(&md5c
, pktin
.body
, 8);
295 MD5Final(session_id
, &md5c
);
298 session_key
[i
] = random_byte();
300 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
302 rsabuf
= malloc(len
);
304 fatalbox("Out of memory");
306 verify_ssh_host_key(savedhost
, &hostkey
);
308 for (i
=0; i
<32; i
++) {
309 rsabuf
[i
] = session_key
[i
];
311 rsabuf
[i
] ^= session_id
[i
];
314 if (hostkey
.bytes
> servkey
.bytes
) {
315 rsaencrypt(rsabuf
, 32, &servkey
);
316 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
318 rsaencrypt(rsabuf
, 32, &hostkey
);
319 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
322 cipher_type
= cfg
.cipher
== CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH
:
324 if ((supported_ciphers_mask
& (1 << cipher_type
)) == 0) {
325 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
326 cipher_type
= SSH_CIPHER_3DES
;
329 s_wrpkt_start(3, len
+15);
330 pktout
.body
[0] = cipher_type
;
331 memcpy(pktout
.body
+1, cookie
, 8);
332 pktout
.body
[9] = (len
*8) >> 8;
333 pktout
.body
[10] = (len
*8) & 0xFF;
334 memcpy(pktout
.body
+11, rsabuf
, len
);
335 pktout
.body
[len
+11] = pktout
.body
[len
+12] = 0; /* protocol flags */
336 pktout
.body
[len
+13] = pktout
.body
[len
+14] = 0;
341 cipher
= cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish
:
343 cipher
->sesskey(session_key
);
345 do { crReturnV
; } while (!ispkt
);
347 if (pktin
.type
!= 14)
348 fatalbox("Encryption not successfully enabled");
352 static char username
[100];
355 if (!*cfg
.username
) {
356 c_write("login as: ", 10);
358 do { crReturnV
; } while (ispkt
);
359 while (inlen
--) switch (c
= *in
++) {
381 if (c
>= ' ' && c
<= '~' && pos
< 40) {
389 username
[strcspn(username
, "\n\r")] = '\0';
392 strncpy(username
, cfg
.username
, 99);
394 sprintf(stuff
, "Sent username \"%s\".\r\n", username
);
395 c_write(stuff
, strlen(stuff
));
397 s_wrpkt_start(4, 4+strlen(username
));
398 pktout
.body
[0] = pktout
.body
[1] = pktout
.body
[2] = 0;
399 pktout
.body
[3] = strlen(username
);
400 memcpy(pktout
.body
+4, username
, strlen(username
));
404 do { crReturnV
; } while (!ispkt
);
406 while (pktin
.type
== 15) {
407 static char password
[100];
410 c_write("password: ", 10);
413 do { crReturnV
; } while (ispkt
);
414 while (inlen
--) switch (c
= *in
++) {
431 if (c
>= ' ' && c
<= '~' && pos
< 40)
437 s_wrpkt_start(9, 4+strlen(password
));
438 pktout
.body
[0] = pktout
.body
[1] = pktout
.body
[2] = 0;
439 pktout
.body
[3] = strlen(password
);
440 memcpy(pktout
.body
+4, password
, strlen(password
));
442 memset(password
, 0, strlen(password
));
443 do { crReturnV
; } while (!ispkt
);
444 if (pktin
.type
== 15) {
445 c_write("Access denied\r\n", 15);
446 } else if (pktin
.type
!= 14) {
447 fatalbox("Strange packet received, type %d", pktin
.type
);
452 i
= strlen(cfg
.termtype
);
453 s_wrpkt_start(10, i
+5*4+1);
454 pktout
.body
[0] = (i
>> 24) & 0xFF;
455 pktout
.body
[1] = (i
>> 16) & 0xFF;
456 pktout
.body
[2] = (i
>> 8) & 0xFF;
457 pktout
.body
[3] = i
& 0xFF;
458 memcpy(pktout
.body
+4, cfg
.termtype
, i
);
460 pktout
.body
[i
++] = (rows
>> 24) & 0xFF;
461 pktout
.body
[i
++] = (rows
>> 16) & 0xFF;
462 pktout
.body
[i
++] = (rows
>> 8) & 0xFF;
463 pktout
.body
[i
++] = rows
& 0xFF;
464 pktout
.body
[i
++] = (cols
>> 24) & 0xFF;
465 pktout
.body
[i
++] = (cols
>> 16) & 0xFF;
466 pktout
.body
[i
++] = (cols
>> 8) & 0xFF;
467 pktout
.body
[i
++] = cols
& 0xFF;
468 memset(pktout
.body
+i
, 0, 9); /* 0 pixwidth, 0 pixheight, 0.b endofopt */
470 ssh_state
= SSH_STATE_INTERMED
;
471 do { crReturnV
; } while (!ispkt
);
472 if (pktin
.type
!= 14 && pktin
.type
!= 15) {
473 fatalbox("Protocol confusion");
474 } else if (pktin
.type
== 15) {
475 c_write("Server refused to allocate pty\r\n", 32);
479 s_wrpkt_start(12, 0);
482 ssh_state
= SSH_STATE_SESSION
;
489 if (pktin
.type
== 17 || pktin
.type
== 18) {
491 for (i
= 0; i
< 4; i
++)
492 len
= (len
<< 8) + pktin
.body
[i
];
493 c_write(pktin
.body
+4, len
);
494 } else if (pktin
.type
== 1) {
495 /* SSH_MSG_DISCONNECT */
496 ssh_state
= SSH_STATE_CLOSED
;
497 } else if (pktin
.type
== 14) {
498 /* SSH_MSG_SUCCESS: may be from EXEC_SHELL on some servers */
499 } else if (pktin
.type
== 15) {
500 /* SSH_MSG_FAILURE: may be from EXEC_SHELL on some servers
501 * if no pty is available or in other odd cases. Ignore */
502 } else if (pktin
.type
== 20) {
504 s_wrpkt_start(33, 0);
507 fatalbox("Strange packet received: type %d", pktin
.type
);
510 s_wrpkt_start(16, 4+inlen
);
511 pktout
.body
[0] = (inlen
>> 24) & 0xFF;
512 pktout
.body
[1] = (inlen
>> 16) & 0xFF;
513 pktout
.body
[2] = (inlen
>> 8) & 0xFF;
514 pktout
.body
[3] = inlen
& 0xFF;
515 memcpy(pktout
.body
+4, in
, inlen
);
524 * Called to set up the connection. Will arrange for WM_NETEVENT
525 * messages to be passed to the specified window, whose window
526 * procedure should then call telnet_msg().
528 * Returns an error message, or NULL on success.
530 * Also places the canonical host name into `realhost'.
532 static char *ssh_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
541 savedhost
= malloc(1+strlen(host
));
543 fatalbox("Out of memory");
544 strcpy(savedhost
, host
);
556 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
557 if ( (h
= gethostbyname(host
)) == NULL
)
558 switch (WSAGetLastError()) {
559 case WSAENETDOWN
: return "Network is down";
560 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
561 return "Host does not exist";
562 case WSATRY_AGAIN
: return "Host not found";
563 default: return "gethostbyname: unknown error";
565 memcpy (&a
, h
->h_addr
, sizeof(a
));
566 *realhost
= h
->h_name
;
575 port
= 22; /* default ssh port */
580 s
= socket(AF_INET
, SOCK_STREAM
, 0);
581 if (s
== INVALID_SOCKET
)
582 switch (WSAGetLastError()) {
583 case WSAENETDOWN
: return "Network is down";
584 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
585 default: return "socket(): unknown error";
589 * Bind to local address.
591 addr
.sin_family
= AF_INET
;
592 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
593 addr
.sin_port
= htons(0);
594 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
595 switch (WSAGetLastError()) {
596 case WSAENETDOWN
: return "Network is down";
597 default: return "bind(): unknown error";
601 * Connect to remote address.
603 addr
.sin_addr
.s_addr
= htonl(a
);
604 addr
.sin_port
= htons((short)port
);
605 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
606 switch (WSAGetLastError()) {
607 case WSAENETDOWN
: return "Network is down";
608 case WSAECONNREFUSED
: return "Connection refused";
609 case WSAENETUNREACH
: return "Network is unreachable";
610 case WSAEHOSTUNREACH
: return "No route to host";
611 default: return "connect(): unknown error";
615 send(s
, "connect ", 8, 0);
616 send(s
, FWhost
, strlen(FWhost
), 0);
619 sprintf(buf
, " %d\n", FWport
);
620 send (s
, buf
, strlen(buf
), 0);
625 return "Protocol initialisation error";
627 if (WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
628 switch (WSAGetLastError()) {
629 case WSAENETDOWN
: return "Network is down";
630 default: return "WSAAsyncSelect(): unknown error";
637 * Process a WM_NETEVENT message. Will return 0 if the connection
638 * has closed, or <0 for a socket error.
640 static int ssh_msg (WPARAM wParam
, LPARAM lParam
) {
644 if (s
== INVALID_SOCKET
) /* how the hell did we get here?! */
647 if (WSAGETSELECTERROR(lParam
) != 0)
648 return -WSAGETSELECTERROR(lParam
);
650 switch (WSAGETSELECTEVENT(lParam
)) {
652 ret
= recv(s
, buf
, sizeof(buf
), 0);
653 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
655 if (ret
< 0) /* any _other_ error */
656 return -10000-WSAGetLastError();
659 return 0; /* can't happen, in theory */
661 ssh_gotdata (buf
, ret
);
665 ssh_state
= SSH_STATE_CLOSED
;
668 return 1; /* shouldn't happen, but WTF */
672 * Called to send data down the Telnet connection.
674 static void ssh_send (char *buf
, int len
) {
675 if (s
== INVALID_SOCKET
)
678 ssh_protocol(buf
, len
, 0);
682 * Called to set the size of the window from Telnet's POV.
684 static void ssh_size(void) {
686 case SSH_STATE_BEFORE_SIZE
:
687 case SSH_STATE_CLOSED
:
688 break; /* do nothing */
689 case SSH_STATE_INTERMED
:
690 size_needed
= TRUE
; /* buffer for later */
692 case SSH_STATE_SESSION
:
694 s_wrpkt_start(11, 16);
695 pktout
.body
[0] = (rows
>> 24) & 0xFF;
696 pktout
.body
[1] = (rows
>> 16) & 0xFF;
697 pktout
.body
[2] = (rows
>> 8) & 0xFF;
698 pktout
.body
[3] = rows
& 0xFF;
699 pktout
.body
[4] = (cols
>> 24) & 0xFF;
700 pktout
.body
[5] = (cols
>> 16) & 0xFF;
701 pktout
.body
[6] = (cols
>> 8) & 0xFF;
702 pktout
.body
[7] = cols
& 0xFF;
703 memset(pktout
.body
+8, 0, 8);
710 * (Send Telnet special codes)
712 static void ssh_special (Telnet_Special code
) {
716 Backend ssh_backend
= {