18 #define logevent(s) { logevent(s); \
19 if (IS_SCP && (scp_flags & SCP_VERBOSE) != 0) \
20 fprintf(stderr, "%s\n", s); }
22 #define SSH_MSG_DISCONNECT 1
23 #define SSH_SMSG_PUBLIC_KEY 2
24 #define SSH_CMSG_SESSION_KEY 3
25 #define SSH_CMSG_USER 4
26 #define SSH_CMSG_AUTH_PASSWORD 9
27 #define SSH_CMSG_REQUEST_PTY 10
28 #define SSH_CMSG_WINDOW_SIZE 11
29 #define SSH_CMSG_EXEC_SHELL 12
30 #define SSH_CMSG_EXEC_CMD 13
31 #define SSH_SMSG_SUCCESS 14
32 #define SSH_SMSG_FAILURE 15
33 #define SSH_CMSG_STDIN_DATA 16
34 #define SSH_SMSG_STDOUT_DATA 17
35 #define SSH_SMSG_STDERR_DATA 18
36 #define SSH_CMSG_EOF 19
37 #define SSH_SMSG_EXIT_STATUS 20
38 #define SSH_CMSG_EXIT_CONFIRMATION 33
39 #define SSH_MSG_IGNORE 32
40 #define SSH_MSG_DEBUG 36
41 #define SSH_CMSG_AUTH_TIS 39
42 #define SSH_SMSG_AUTH_TIS_CHALLENGE 40
43 #define SSH_CMSG_AUTH_TIS_RESPONSE 41
45 #define SSH_AUTH_TIS 5
47 #define GET_32BIT(cp) \
48 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
49 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
50 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
51 ((unsigned long)(unsigned char)(cp)[3]))
53 #define PUT_32BIT(cp, value) { \
54 (cp)[0] = (unsigned char)((value) >> 24); \
55 (cp)[1] = (unsigned char)((value) >> 16); \
56 (cp)[2] = (unsigned char)((value) >> 8); \
57 (cp)[3] = (unsigned char)(value); }
59 enum { PKT_END
, PKT_INT
, PKT_CHAR
, PKT_DATA
, PKT_STR
};
61 /* Coroutine mechanics for the sillier bits of the code */
62 #define crBegin1 static int crLine = 0;
63 #define crBegin2 switch(crLine) { case 0:;
64 #define crBegin crBegin1; crBegin2;
65 #define crFinish(z) } crLine = 0; return (z)
66 #define crFinishV } crLine = 0; return
69 crLine=__LINE__; return (z); case __LINE__:;\
73 crLine=__LINE__; return; case __LINE__:;\
75 #define crStop(z) do{ crLine = 0; return (z); }while(0)
76 #define crStopV do{ crLine = 0; return; }while(0)
77 #define crWaitUntil(c) do { crReturn(0); } while (!(c))
79 static SOCKET s
= INVALID_SOCKET
;
81 static unsigned char session_key
[32];
82 static struct ssh_cipher
*cipher
= NULL
;
84 int (*ssh_get_password
)(const char *prompt
, char *str
, int maxlen
) = NULL
;
86 static char *savedhost
;
89 SSH_STATE_BEFORE_SIZE
,
93 } ssh_state
= SSH_STATE_BEFORE_SIZE
;
95 static int size_needed
= FALSE
;
97 static void s_write (char *buf
, int len
) {
99 int i
= send (s
, buf
, len
, 0);
103 fatalbox("Lost connection while sending");
110 static int s_read (char *buf
, int len
) {
113 int i
= recv (s
, buf
, len
, 0);
117 len
-= i
, buf
+= i
, ret
+= i
;
124 static void c_write (char *buf
, int len
) {
126 if (len
> 0 && buf
[len
-1] == '\n') len
--;
127 if (len
> 0 && buf
[len
-1] == '\r') len
--;
128 if (len
> 0) { fwrite(buf
, len
, 1, stderr
); fputc('\n', stderr
); }
143 static struct Packet pktin
= { 0, 0, NULL
, NULL
, 0 };
144 static struct Packet pktout
= { 0, 0, NULL
, NULL
, 0 };
146 static void ssh_protocol(unsigned char *in
, int inlen
, int ispkt
);
147 static void ssh_size(void);
151 * Collect incoming data in the incoming packet buffer.
152 * Decihper and verify the packet when it is completely read.
153 * Drop SSH_MSG_DEBUG and SSH_MSG_IGNORE packets.
154 * Update the *data and *datalen variables.
155 * Return the additional nr of bytes needed, or 0 when
156 * a complete packet is available.
158 static int s_rdpkt(unsigned char **data
, int *datalen
)
160 static long len
, pad
, biglen
, to_read
;
161 static unsigned long realcrc
, gotcrc
;
162 static unsigned char *p
;
172 for (i
= len
= 0; i
< 4; i
++) {
173 while ((*datalen
) == 0)
175 len
= (len
<< 8) + **data
;
176 (*data
)++, (*datalen
)--;
180 if (len
== 0x52656d6f) { /* "Remo"te server has closed ... */
181 len
= 0x300; /* big enough to carry to end */
187 pktin
.length
= len
- 5;
189 if (pktin
.maxlen
< biglen
) {
190 pktin
.maxlen
= biglen
;
192 /* Allocate enough buffer space for extra block
193 * for MS CryptEncrypt() */
194 pktin
.data
= (pktin
.data
== NULL ?
malloc(biglen
+8) :
195 realloc(pktin
.data
, biglen
+8));
197 pktin
.data
= (pktin
.data
== NULL ?
malloc(biglen
) :
198 realloc(pktin
.data
, biglen
));
201 fatalbox("Out of memory");
206 while (to_read
> 0) {
209 while ((*datalen
) == 0)
211 if (chunk
> (*datalen
))
213 memcpy(p
, *data
, chunk
);
221 cipher
->decrypt(pktin
.data
, biglen
);
223 pktin
.type
= pktin
.data
[pad
];
224 pktin
.body
= pktin
.data
+ pad
+ 1;
226 realcrc
= crc32(pktin
.data
, biglen
-4);
227 gotcrc
= GET_32BIT(pktin
.data
+biglen
-4);
228 if (gotcrc
!= realcrc
) {
229 fatalbox("Incorrect CRC received on packet");
232 if (pktin
.type
== SSH_SMSG_STDOUT_DATA
||
233 pktin
.type
== SSH_SMSG_STDERR_DATA
||
234 pktin
.type
== SSH_MSG_DEBUG
||
235 pktin
.type
== SSH_SMSG_AUTH_TIS_CHALLENGE
) {
236 long strlen
= GET_32BIT(pktin
.body
);
237 if (strlen
+ 4 != pktin
.length
)
238 fatalbox("Received data packet with bogus string length");
241 if (pktin
.type
== SSH_MSG_DEBUG
) {
242 /* log debug message */
244 int strlen
= GET_32BIT(pktin
.body
);
245 strcpy(buf
, "Remote: ");
246 if (strlen
> 70) strlen
= 70;
247 memcpy(buf
+8, pktin
.body
+4, strlen
);
248 buf
[8+strlen
] = '\0';
251 } else if (pktin
.type
== SSH_MSG_IGNORE
) {
259 static void ssh_gotdata(unsigned char *data
, int datalen
)
261 while (datalen
> 0) {
262 if ( s_rdpkt(&data
, &datalen
) == 0 ) {
263 ssh_protocol(NULL
, 0, 1);
264 if (ssh_state
== SSH_STATE_CLOSED
) {
272 static void s_wrpkt_start(int type
, int len
) {
275 len
+= 5; /* type and CRC */
279 pktout
.length
= len
-5;
280 if (pktout
.maxlen
< biglen
) {
281 pktout
.maxlen
= biglen
;
283 /* Allocate enough buffer space for extra block
284 * for MS CryptEncrypt() */
285 pktout
.data
= (pktout
.data
== NULL ?
malloc(biglen
+12) :
286 realloc(pktout
.data
, biglen
+12));
288 pktout
.data
= (pktout
.data
== NULL ?
malloc(biglen
+4) :
289 realloc(pktout
.data
, biglen
+4));
292 fatalbox("Out of memory");
296 pktout
.body
= pktout
.data
+4+pad
+1;
299 static void s_wrpkt(void) {
300 int pad
, len
, biglen
, i
;
303 len
= pktout
.length
+ 5; /* type and CRC */
307 pktout
.body
[-1] = pktout
.type
;
308 for (i
=0; i
<pad
; i
++)
309 pktout
.data
[i
+4] = random_byte();
310 crc
= crc32(pktout
.data
+4, biglen
-4);
311 PUT_32BIT(pktout
.data
+biglen
, crc
);
312 PUT_32BIT(pktout
.data
, len
);
315 cipher
->encrypt(pktout
.data
+4, biglen
);
317 s_write(pktout
.data
, biglen
+4);
321 * Construct a packet with the specified contents and
322 * send it to the server.
324 static void send_packet(int pkttype
, ...)
327 unsigned char *p
, *argp
, argchar
;
328 unsigned long argint
;
329 int pktlen
, argtype
, arglen
;
332 va_start(args
, pkttype
);
333 while ((argtype
= va_arg(args
, int)) != PKT_END
) {
336 (void) va_arg(args
, int);
340 (void) va_arg(args
, char);
344 (void) va_arg(args
, unsigned char *);
345 arglen
= va_arg(args
, int);
349 argp
= va_arg(args
, unsigned char *);
350 arglen
= strlen(argp
);
351 pktlen
+= 4 + arglen
;
359 s_wrpkt_start(pkttype
, pktlen
);
362 va_start(args
, pkttype
);
363 while ((argtype
= va_arg(args
, int)) != PKT_END
) {
366 argint
= va_arg(args
, int);
367 PUT_32BIT(p
, argint
);
371 argchar
= va_arg(args
, unsigned char);
376 argp
= va_arg(args
, unsigned char *);
377 arglen
= va_arg(args
, int);
378 memcpy(p
, argp
, arglen
);
382 argp
= va_arg(args
, unsigned char *);
383 arglen
= strlen(argp
);
384 PUT_32BIT(p
, arglen
);
385 memcpy(p
+ 4, argp
, arglen
);
397 * Connect to specified host and port.
398 * Returns an error message, or NULL on success.
399 * Also places the canonical host name into `realhost'.
401 static char *connect_to_host(char *host
, int port
, char **realhost
)
411 savedhost
= malloc(1+strlen(host
));
413 fatalbox("Out of memory");
414 strcpy(savedhost
, host
);
417 port
= 22; /* default ssh port */
429 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
430 if ( (h
= gethostbyname(host
)) == NULL
)
431 switch (WSAGetLastError()) {
432 case WSAENETDOWN
: return "Network is down";
433 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
434 return "Host does not exist";
435 case WSATRY_AGAIN
: return "Host not found";
436 default: return "gethostbyname: unknown error";
438 memcpy (&a
, h
->h_addr
, sizeof(a
));
439 *realhost
= h
->h_name
;
450 s
= socket(AF_INET
, SOCK_STREAM
, 0);
451 if (s
== INVALID_SOCKET
)
452 switch (WSAGetLastError()) {
453 case WSAENETDOWN
: return "Network is down";
454 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
455 default: return "socket(): unknown error";
459 * Bind to local address.
461 addr
.sin_family
= AF_INET
;
462 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
463 addr
.sin_port
= htons(0);
464 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
465 switch (WSAGetLastError()) {
466 case WSAENETDOWN
: return "Network is down";
467 default: return "bind(): unknown error";
471 * Connect to remote address.
473 addr
.sin_addr
.s_addr
= htonl(a
);
474 addr
.sin_port
= htons((short)port
);
475 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
476 switch (WSAGetLastError()) {
477 case WSAENETDOWN
: return "Network is down";
478 case WSAECONNREFUSED
: return "Connection refused";
479 case WSAENETUNREACH
: return "Network is unreachable";
480 case WSAEHOSTUNREACH
: return "No route to host";
481 default: return "connect(): unknown error";
485 send(s
, "connect ", 8, 0);
486 send(s
, FWhost
, strlen(FWhost
), 0);
489 sprintf(buf
, " %d\n", FWport
);
490 send (s
, buf
, strlen(buf
), 0);
497 static int ssh_versioncmp(char *a
, char *b
) {
499 unsigned long av
, bv
;
501 av
= strtoul(a
, &ae
, 10);
502 bv
= strtoul(b
, &be
, 10);
503 if (av
!= bv
) return (av
< bv ?
-1 : +1);
504 if (*ae
== '.') ae
++;
505 if (*be
== '.') be
++;
506 av
= strtoul(ae
, &ae
, 10);
507 bv
= strtoul(be
, &be
, 10);
508 if (av
!= bv
) return (av
< bv ?
-1 : +1);
512 static int do_ssh_init(void) {
516 char vlog
[sizeof(vstring
)+20];
521 while (s_read(&c
, 1) == 1) {
522 if (c
== 'S' && i
< 2) i
++;
523 else if (c
== 'S' && i
== 2) i
= 2;
524 else if (c
== 'H' && i
== 2) break;
528 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
529 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
530 if (s_read(&c
,1) != 1 || c
!= 'H') return 0;
532 strcpy(vstring
, "SSH-");
534 if (s_read(&c
,1) != 1 || c
!= '-') return 0;
537 if (s_read(&c
,1) != 1)
539 if (vsp
< vstring
+sizeof(vstring
)-1)
545 } else if (i
< sizeof(version
)-1)
553 sprintf(vlog
, "Server version: %s", vstring
);
554 vlog
[strcspn(vlog
, "\r\n")] = '\0';
557 sprintf(vstring
, "SSH-%s-PuTTY\n",
558 (ssh_versioncmp(version
, "1.5") <= 0 ? version
: "1.5"));
559 sprintf(vlog
, "We claim version: %s", vstring
);
560 vlog
[strcspn(vlog
, "\r\n")] = '\0';
562 s_write(vstring
, strlen(vstring
));
567 * Handle the key exchange and user authentication phases.
569 static int do_ssh_login(unsigned char *in
, int inlen
, int ispkt
)
572 unsigned char session_id
[16];
573 unsigned char *rsabuf
, *keystr1
, *keystr2
;
574 unsigned char cookie
[8];
575 struct RSAKey servkey
, hostkey
;
576 struct MD5Context md5c
;
577 static unsigned long supported_ciphers_mask
, supported_auths_mask
;
580 extern struct ssh_cipher ssh_3des
;
581 extern struct ssh_cipher ssh_des
;
582 extern struct ssh_cipher ssh_blowfish
;
586 if (!ispkt
) crWaitUntil(ispkt
);
588 if (pktin
.type
!= SSH_SMSG_PUBLIC_KEY
)
589 fatalbox("Public key packet not received");
591 logevent("Received public keys");
593 memcpy(cookie
, pktin
.body
, 8);
595 i
= makekey(pktin
.body
+8, &servkey
, &keystr1
);
596 j
= makekey(pktin
.body
+8+i
, &hostkey
, &keystr2
);
599 * Hash the host key and print the hash in the log box. Just as
600 * a last resort in case the registry's host key checking is
601 * compromised, we'll allow the user some ability to verify
605 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
606 MD5Final(session_id
, &md5c
);
610 logevent("Host key MD5 is:");
612 for (i
= 0; i
< 16; i
++)
613 sprintf(logmsg
+strlen(logmsg
), "%02x", session_id
[i
]);
617 supported_ciphers_mask
= GET_32BIT(pktin
.body
+12+i
+j
);
618 supported_auths_mask
= GET_32BIT(pktin
.body
+16+i
+j
);
621 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
622 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
623 MD5Update(&md5c
, pktin
.body
, 8);
624 MD5Final(session_id
, &md5c
);
627 session_key
[i
] = random_byte();
629 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
631 rsabuf
= malloc(len
);
633 fatalbox("Out of memory");
636 * Verify the host key.
640 * First format the key into a string.
642 int len
= rsastr_len(&hostkey
);
643 char *keystr
= malloc(len
);
645 fatalbox("Out of memory");
646 rsastr_fmt(keystr
, &hostkey
);
647 verify_ssh_host_key(savedhost
, keystr
);
651 for (i
=0; i
<32; i
++) {
652 rsabuf
[i
] = session_key
[i
];
654 rsabuf
[i
] ^= session_id
[i
];
657 if (hostkey
.bytes
> servkey
.bytes
) {
658 rsaencrypt(rsabuf
, 32, &servkey
);
659 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
661 rsaencrypt(rsabuf
, 32, &hostkey
);
662 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
665 logevent("Encrypted session key");
667 cipher_type
= cfg
.cipher
== CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH
:
668 cfg
.cipher
== CIPHER_DES ? SSH_CIPHER_DES
:
670 if ((supported_ciphers_mask
& (1 << cipher_type
)) == 0) {
671 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
672 cipher_type
= SSH_CIPHER_3DES
;
674 switch (cipher_type
) {
675 case SSH_CIPHER_3DES
: logevent("Using 3DES encryption"); break;
676 case SSH_CIPHER_DES
: logevent("Using single-DES encryption"); break;
677 case SSH_CIPHER_BLOWFISH
: logevent("Using Blowfish encryption"); break;
680 send_packet(SSH_CMSG_SESSION_KEY
,
681 PKT_CHAR
, cipher_type
,
683 PKT_CHAR
, (len
*8) >> 8, PKT_CHAR
, (len
*8) & 0xFF,
684 PKT_DATA
, rsabuf
, len
,
688 logevent("Trying to enable encryption...");
692 cipher
= cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish
:
693 cipher_type
== SSH_CIPHER_DES ?
&ssh_des
:
695 cipher
->sesskey(session_key
);
699 if (pktin
.type
!= SSH_SMSG_SUCCESS
)
700 fatalbox("Encryption not successfully enabled");
702 logevent("Successfully started encryption");
706 static char username
[100];
709 if (!IS_SCP
&& !*cfg
.username
) {
710 c_write("login as: ", 10);
713 while (inlen
--) switch (c
= *in
++) {
735 if (((c
>= ' ' && c
<= '~') ||
736 ((unsigned char)c
>= 160)) && pos
< 40) {
744 username
[strcspn(username
, "\n\r")] = '\0';
747 strncpy(username
, cfg
.username
, 99);
750 sprintf(stuff
, "Sent username \"%s\".\r\n", username
);
751 c_write(stuff
, strlen(stuff
));
755 send_packet(SSH_CMSG_USER
, PKT_STR
, username
, PKT_END
);
757 char userlog
[20+sizeof(username
)];
758 sprintf(userlog
, "Sent username \"%s\"", username
);
765 while (pktin
.type
== SSH_SMSG_FAILURE
) {
766 static char password
[100];
769 static int pwpkt_type
;
772 * Show password prompt, having first obtained it via a TIS
773 * exchange if we're doing TIS authentication.
775 pwpkt_type
= SSH_CMSG_AUTH_PASSWORD
;
779 sprintf(prompt
, "%s@%s's password: ", cfg
.username
, savedhost
);
780 if (!ssh_get_password(prompt
, password
, sizeof(password
))) {
782 * get_password failed to get a password (for
783 * example because one was supplied on the command
784 * line which has already failed to work).
787 logevent("No more passwords to try");
788 ssh_state
= SSH_STATE_CLOSED
;
793 if (pktin
.type
== SSH_SMSG_FAILURE
&&
795 (supported_auths_mask
& (1<<SSH_AUTH_TIS
))) {
796 pwpkt_type
= SSH_CMSG_AUTH_TIS_RESPONSE
;
797 logevent("Requested TIS authentication");
798 send_packet(SSH_CMSG_AUTH_TIS
, PKT_END
);
800 if (pktin
.type
!= SSH_SMSG_AUTH_TIS_CHALLENGE
) {
801 logevent("TIS authentication declined");
802 c_write("TIS authentication refused.\r\n", 29);
804 int challengelen
= ((pktin
.body
[0] << 24) |
805 (pktin
.body
[1] << 16) |
806 (pktin
.body
[2] << 8) |
808 logevent("Received TIS challenge");
809 c_write(pktin
.body
+4, challengelen
);
812 if (pwpkt_type
== SSH_CMSG_AUTH_PASSWORD
)
813 c_write("password: ", 10);
818 while (inlen
--) switch (c
= *in
++) {
835 if (((c
>= ' ' && c
<= '~') ||
836 ((unsigned char)c
>= 160)) && pos
< 40)
845 send_packet(pwpkt_type
, PKT_STR
, password
, PKT_END
);
846 logevent("Sent password");
847 memset(password
, 0, strlen(password
));
849 if (pktin
.type
== SSH_SMSG_FAILURE
) {
850 c_write("Access denied\r\n", 15);
851 logevent("Authentication refused");
852 } else if (pktin
.type
== SSH_MSG_DISCONNECT
) {
853 logevent("Received disconnect request");
854 ssh_state
= SSH_STATE_CLOSED
;
856 } else if (pktin
.type
!= SSH_SMSG_SUCCESS
) {
857 fatalbox("Strange packet received, type %d", pktin
.type
);
861 logevent("Authentication successful");
866 static void ssh_protocol(unsigned char *in
, int inlen
, int ispkt
) {
871 while (!do_ssh_login(in
, inlen
, ispkt
)) {
874 if (ssh_state
== SSH_STATE_CLOSED
)
878 send_packet(SSH_CMSG_REQUEST_PTY
,
879 PKT_STR
, cfg
.termtype
,
880 PKT_INT
, rows
, PKT_INT
, cols
,
881 PKT_INT
, 0, PKT_INT
, 0,
884 ssh_state
= SSH_STATE_INTERMED
;
885 do { crReturnV
; } while (!ispkt
);
886 if (pktin
.type
!= SSH_SMSG_SUCCESS
&& pktin
.type
!= SSH_SMSG_FAILURE
) {
887 fatalbox("Protocol confusion");
888 } else if (pktin
.type
== SSH_SMSG_FAILURE
) {
889 c_write("Server refused to allocate pty\r\n", 32);
891 logevent("Allocated pty");
894 send_packet(SSH_CMSG_EXEC_SHELL
, PKT_END
);
895 logevent("Started session");
897 ssh_state
= SSH_STATE_SESSION
;
904 if (pktin
.type
== SSH_SMSG_STDOUT_DATA
||
905 pktin
.type
== SSH_SMSG_STDERR_DATA
) {
906 long len
= GET_32BIT(pktin
.body
);
907 c_write(pktin
.body
+4, len
);
908 } else if (pktin
.type
== SSH_MSG_DISCONNECT
) {
909 ssh_state
= SSH_STATE_CLOSED
;
910 logevent("Received disconnect request");
911 } else if (pktin
.type
== SSH_SMSG_SUCCESS
) {
912 /* may be from EXEC_SHELL on some servers */
913 } else if (pktin
.type
== SSH_SMSG_FAILURE
) {
914 /* may be from EXEC_SHELL on some servers
915 * if no pty is available or in other odd cases. Ignore */
916 } else if (pktin
.type
== SSH_SMSG_EXIT_STATUS
) {
917 send_packet(SSH_CMSG_EXIT_CONFIRMATION
, PKT_END
);
919 fatalbox("Strange packet received: type %d", pktin
.type
);
922 send_packet(SSH_CMSG_STDIN_DATA
,
923 PKT_INT
, inlen
, PKT_DATA
, in
, inlen
, PKT_END
);
931 * Called to set up the connection. Will arrange for WM_NETEVENT
932 * messages to be passed to the specified window, whose window
933 * procedure should then call telnet_msg().
935 * Returns an error message, or NULL on success.
937 static char *ssh_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
941 if(crypto_startup() == 0)
942 return "Microsoft high encryption pack not installed!";
945 p
= connect_to_host(host
, port
, realhost
);
950 return "Protocol initialisation error";
952 if (WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
953 switch (WSAGetLastError()) {
954 case WSAENETDOWN
: return "Network is down";
955 default: return "WSAAsyncSelect(): unknown error";
962 * Process a WM_NETEVENT message. Will return 0 if the connection
963 * has closed, or <0 for a socket error.
965 static int ssh_msg (WPARAM wParam
, LPARAM lParam
) {
970 * Because reading less than the whole of the available pending
971 * data can generate an FD_READ event, we need to allow for the
972 * possibility that FD_READ may arrive with FD_CLOSE already in
973 * the queue; so it's possible that we can get here even with s
974 * invalid. If so, we return 1 and don't worry about it.
976 if (s
== INVALID_SOCKET
)
979 if (WSAGETSELECTERROR(lParam
) != 0)
980 return -WSAGETSELECTERROR(lParam
);
982 switch (WSAGETSELECTEVENT(lParam
)) {
985 ret
= recv(s
, buf
, sizeof(buf
), 0);
986 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
988 if (ret
< 0) /* any _other_ error */
989 return -10000-WSAGetLastError();
994 ssh_gotdata (buf
, ret
);
995 if (ssh_state
== SSH_STATE_CLOSED
) {
1002 return 1; /* shouldn't happen, but WTF */
1006 * Called to send data down the Telnet connection.
1008 static void ssh_send (char *buf
, int len
) {
1009 if (s
== INVALID_SOCKET
)
1012 ssh_protocol(buf
, len
, 0);
1016 * Called to set the size of the window from Telnet's POV.
1018 static void ssh_size(void) {
1019 switch (ssh_state
) {
1020 case SSH_STATE_BEFORE_SIZE
:
1021 case SSH_STATE_CLOSED
:
1022 break; /* do nothing */
1023 case SSH_STATE_INTERMED
:
1024 size_needed
= TRUE
; /* buffer for later */
1026 case SSH_STATE_SESSION
:
1028 send_packet(SSH_CMSG_WINDOW_SIZE
,
1029 PKT_INT
, rows
, PKT_INT
, cols
,
1030 PKT_INT
, 0, PKT_INT
, 0, PKT_END
);
1036 * (Send Telnet special codes)
1038 static void ssh_special (Telnet_Special code
) {
1044 * Read and decrypt one incoming SSH packet.
1045 * (only used by pSCP)
1047 static void get_packet(void)
1049 unsigned char buf
[4096], *p
;
1058 while ((to_read
= s_rdpkt(&p
, &len
)) > 0) {
1059 if (to_read
> sizeof(buf
)) to_read
= sizeof(buf
);
1060 len
= s_read(buf
, to_read
);
1061 if (len
!= to_read
) {
1073 * Receive a block of data over the SSH link. Block until
1074 * all data is available. Return nr of bytes read (0 if lost connection).
1075 * (only used by pSCP)
1077 int ssh_scp_recv(unsigned char *buf
, int len
)
1079 static int pending_input_len
= 0;
1080 static unsigned char *pending_input_ptr
;
1085 if (pending_input_len
>= to_read
) {
1086 memcpy(buf
, pending_input_ptr
, to_read
);
1087 pending_input_ptr
+= to_read
;
1088 pending_input_len
-= to_read
;
1092 if (pending_input_len
> 0) {
1093 memcpy(buf
, pending_input_ptr
, pending_input_len
);
1094 buf
+= pending_input_len
;
1095 to_read
-= pending_input_len
;
1096 pending_input_len
= 0;
1099 if (s
== INVALID_SOCKET
)
1101 while (to_read
> 0) {
1103 if (s
== INVALID_SOCKET
)
1105 if (pktin
.type
== SSH_SMSG_STDOUT_DATA
) {
1106 int plen
= GET_32BIT(pktin
.body
);
1107 if (plen
<= to_read
) {
1108 memcpy(buf
, pktin
.body
+ 4, plen
);
1112 memcpy(buf
, pktin
.body
+ 4, to_read
);
1113 pending_input_len
= plen
- to_read
;
1114 pending_input_ptr
= pktin
.body
+ 4 + to_read
;
1117 } else if (pktin
.type
== SSH_SMSG_STDERR_DATA
) {
1118 int plen
= GET_32BIT(pktin
.body
);
1119 fwrite(pktin
.body
+ 4, plen
, 1, stderr
);
1120 } else if (pktin
.type
== SSH_MSG_DISCONNECT
) {
1121 logevent("Received disconnect request");
1122 } else if (pktin
.type
== SSH_SMSG_SUCCESS
||
1123 pktin
.type
== SSH_SMSG_FAILURE
) {
1125 } else if (pktin
.type
== SSH_SMSG_EXIT_STATUS
) {
1127 sprintf(logbuf
, "Remote exit status: %d", GET_32BIT(pktin
.body
));
1129 send_packet(SSH_CMSG_EXIT_CONFIRMATION
, PKT_END
);
1130 logevent("Closing connection");
1134 fatalbox("Strange packet received: type %d", pktin
.type
);
1142 * Send a block of data over the SSH link.
1143 * Block until all data is sent.
1144 * (only used by pSCP)
1146 void ssh_scp_send(unsigned char *buf
, int len
)
1149 if (s
== INVALID_SOCKET
)
1151 send_packet(SSH_CMSG_STDIN_DATA
,
1152 PKT_INT
, len
, PKT_DATA
, buf
, len
, PKT_END
);
1156 * Send an EOF notification to the server.
1157 * (only used by pSCP)
1159 void ssh_scp_send_eof(void)
1162 if (s
== INVALID_SOCKET
)
1164 send_packet(SSH_CMSG_EOF
, PKT_END
);
1168 * Set up the connection, login on the remote host and
1169 * start execution of a command.
1170 * Returns an error message, or NULL on success.
1171 * (only used by pSCP)
1173 char *ssh_scp_init(char *host
, int port
, char *cmd
, char **realhost
)
1180 if (crypto_startup() == 0)
1181 return "Microsoft high encryption pack not installed!";
1184 p
= connect_to_host(host
, port
, realhost
);
1191 return "Protocol initialisation error";
1193 /* Exchange keys and login */
1196 if (s
== INVALID_SOCKET
)
1197 return "Connection closed by remote host";
1198 } while (!do_ssh_login(NULL
, 0, 1));
1200 if (ssh_state
== SSH_STATE_CLOSED
) {
1203 return "Session initialisation error";
1206 /* Execute command */
1207 sprintf(buf
, "Sending command: %.100s", cmd
);
1209 send_packet(SSH_CMSG_EXEC_CMD
, PKT_STR
, cmd
, PKT_END
);
1215 Backend ssh_backend
= {