18 #define logevent(s) { logevent(s); \
19 if (IS_SCP && (scp_flags & SCP_VERBOSE) != 0) \
20 fprintf(stderr, "%s\n", s); }
22 #define SSH1_MSG_DISCONNECT 1
23 #define SSH1_SMSG_PUBLIC_KEY 2
24 #define SSH1_CMSG_SESSION_KEY 3
25 #define SSH1_CMSG_USER 4
26 #define SSH1_CMSG_AUTH_PASSWORD 9
27 #define SSH1_CMSG_REQUEST_PTY 10
28 #define SSH1_CMSG_WINDOW_SIZE 11
29 #define SSH1_CMSG_EXEC_SHELL 12
30 #define SSH1_CMSG_EXEC_CMD 13
31 #define SSH1_SMSG_SUCCESS 14
32 #define SSH1_SMSG_FAILURE 15
33 #define SSH1_CMSG_STDIN_DATA 16
34 #define SSH1_SMSG_STDOUT_DATA 17
35 #define SSH1_SMSG_STDERR_DATA 18
36 #define SSH1_CMSG_EOF 19
37 #define SSH1_SMSG_EXIT_STATUS 20
38 #define SSH1_CMSG_EXIT_CONFIRMATION 33
39 #define SSH1_MSG_IGNORE 32
40 #define SSH1_MSG_DEBUG 36
41 #define SSH1_CMSG_AUTH_TIS 39
42 #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40
43 #define SSH1_CMSG_AUTH_TIS_RESPONSE 41
45 #define SSH1_AUTH_TIS 5
47 #define SSH2_MSG_DISCONNECT 1
48 #define SSH2_MSG_IGNORE 2
49 #define SSH2_MSG_UNIMPLEMENTED 3
50 #define SSH2_MSG_DEBUG 4
51 #define SSH2_MSG_SERVICE_REQUEST 5
52 #define SSH2_MSG_SERVICE_ACCEPT 6
53 #define SSH2_MSG_KEXINIT 20
54 #define SSH2_MSG_NEWKEYS 21
55 #define SSH2_MSG_KEXDH_INIT 30
56 #define SSH2_MSG_KEXDH_REPLY 31
58 #define GET_32BIT(cp) \
59 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
60 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
61 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
62 ((unsigned long)(unsigned char)(cp)[3]))
64 #define PUT_32BIT(cp, value) { \
65 (cp)[0] = (unsigned char)((value) >> 24); \
66 (cp)[1] = (unsigned char)((value) >> 16); \
67 (cp)[2] = (unsigned char)((value) >> 8); \
68 (cp)[3] = (unsigned char)(value); }
70 enum { PKT_END
, PKT_INT
, PKT_CHAR
, PKT_DATA
, PKT_STR
};
72 /* Coroutine mechanics for the sillier bits of the code */
73 #define crBegin1 static int crLine = 0;
74 #define crBegin2 switch(crLine) { case 0:;
75 #define crBegin crBegin1; crBegin2;
76 #define crFinish(z) } crLine = 0; return (z)
77 #define crFinishV } crLine = 0; return
80 crLine=__LINE__; return (z); case __LINE__:;\
84 crLine=__LINE__; return; case __LINE__:;\
86 #define crStop(z) do{ crLine = 0; return (z); }while(0)
87 #define crStopV do{ crLine = 0; return; }while(0)
88 #define crWaitUntil(c) do { crReturn(0); } while (!(c))
90 extern struct ssh_cipher ssh_3des
;
91 extern struct ssh_cipher ssh_des
;
92 extern struct ssh_cipher ssh_blowfish
;
94 /* for ssh 2; we miss out single-DES because it isn't supported */
95 struct ssh_cipher
*ciphers
[] = { &ssh_3des
, &ssh_blowfish
};
97 extern struct ssh_kex ssh_diffiehellman
;
98 struct ssh_kex
*kex_algs
[] = { &ssh_diffiehellman
};
100 extern struct ssh_hostkey ssh_dss
;
101 struct ssh_hostkey
*hostkey_algs
[] = { &ssh_dss
};
103 extern struct ssh_mac ssh_sha1
;
107 static void nullmac_sesskey(unsigned char *key
, int len
) { }
108 static void nullmac_generate(unsigned char *blk
, int len
, unsigned long seq
) { }
109 static int nullmac_verify(unsigned char *blk
, int len
, unsigned long seq
) { return 1; }
110 struct ssh_mac ssh_mac_none
= {
111 nullmac_sesskey
, nullmac_generate
, nullmac_verify
, "none", 0
113 struct ssh_mac
*macs
[] = { &ssh_sha1
, &ssh_mac_none
};
115 struct ssh_compress ssh_comp_none
= {
118 struct ssh_compress
*compressions
[] = { &ssh_comp_none
};
120 static SOCKET s
= INVALID_SOCKET
;
122 static unsigned char session_key
[32];
123 static struct ssh_cipher
*cipher
= NULL
;
124 static struct ssh_cipher
*cscipher
= NULL
;
125 static struct ssh_cipher
*sccipher
= NULL
;
126 static struct ssh_mac
*csmac
= NULL
;
127 static struct ssh_mac
*scmac
= NULL
;
128 static struct ssh_compress
*cscomp
= NULL
;
129 static struct ssh_compress
*sccomp
= NULL
;
130 static struct ssh_kex
*kex
= NULL
;
131 static struct ssh_hostkey
*hostkey
= NULL
;
133 int (*ssh_get_password
)(const char *prompt
, char *str
, int maxlen
) = NULL
;
135 static char *savedhost
;
138 SSH_STATE_BEFORE_SIZE
,
142 } ssh_state
= SSH_STATE_BEFORE_SIZE
;
144 static int size_needed
= FALSE
;
146 static void s_write (char *buf
, int len
) {
148 int i
= send (s
, buf
, len
, 0);
152 fatalbox("Lost connection while sending");
159 static int s_read (char *buf
, int len
) {
162 int i
= recv (s
, buf
, len
, 0);
166 len
-= i
, buf
+= i
, ret
+= i
;
173 static void c_write (char *buf
, int len
) {
175 if (len
> 0 && buf
[len
-1] == '\n') len
--;
176 if (len
> 0 && buf
[len
-1] == '\r') len
--;
177 if (len
> 0) { fwrite(buf
, len
, 1, stderr
); fputc('\n', stderr
); }
193 static struct Packet pktin
= { 0, 0, NULL
, NULL
, 0 };
194 static struct Packet pktout
= { 0, 0, NULL
, NULL
, 0 };
196 static void (*ssh_protocol
)(unsigned char *in
, int inlen
, int ispkt
);
197 static void ssh1_protocol(unsigned char *in
, int inlen
, int ispkt
);
198 static void ssh2_protocol(unsigned char *in
, int inlen
, int ispkt
);
199 static void ssh_size(void);
201 static int (*s_rdpkt
)(unsigned char **data
, int *datalen
);
204 * Collect incoming data in the incoming packet buffer.
205 * Decipher and verify the packet when it is completely read.
206 * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets.
207 * Update the *data and *datalen variables.
208 * Return the additional nr of bytes needed, or 0 when
209 * a complete packet is available.
211 static int ssh1_rdpkt(unsigned char **data
, int *datalen
)
213 static long len
, pad
, biglen
, to_read
;
214 static unsigned long realcrc
, gotcrc
;
215 static unsigned char *p
;
225 for (i
= len
= 0; i
< 4; i
++) {
226 while ((*datalen
) == 0)
228 len
= (len
<< 8) + **data
;
229 (*data
)++, (*datalen
)--;
233 if (len
== 0x52656d6f) { /* "Remo"te server has closed ... */
234 len
= 0x300; /* big enough to carry to end */
240 pktin
.length
= len
- 5;
242 if (pktin
.maxlen
< biglen
) {
243 pktin
.maxlen
= biglen
;
244 pktin
.data
= (pktin
.data
== NULL ?
malloc(biglen
+APIEXTRA
) :
245 realloc(pktin
.data
, biglen
+APIEXTRA
));
247 fatalbox("Out of memory");
252 while (to_read
> 0) {
255 while ((*datalen
) == 0)
257 if (chunk
> (*datalen
))
259 memcpy(p
, *data
, chunk
);
267 cipher
->decrypt(pktin
.data
, biglen
);
269 pktin
.type
= pktin
.data
[pad
];
270 pktin
.body
= pktin
.data
+ pad
+ 1;
272 realcrc
= crc32(pktin
.data
, biglen
-4);
273 gotcrc
= GET_32BIT(pktin
.data
+biglen
-4);
274 if (gotcrc
!= realcrc
) {
275 fatalbox("Incorrect CRC received on packet");
278 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
||
279 pktin
.type
== SSH1_SMSG_STDERR_DATA
||
280 pktin
.type
== SSH1_MSG_DEBUG
||
281 pktin
.type
== SSH1_SMSG_AUTH_TIS_CHALLENGE
) {
282 long strlen
= GET_32BIT(pktin
.body
);
283 if (strlen
+ 4 != pktin
.length
)
284 fatalbox("Received data packet with bogus string length");
287 if (pktin
.type
== SSH1_MSG_DEBUG
) {
288 /* log debug message */
290 int strlen
= GET_32BIT(pktin
.body
);
291 strcpy(buf
, "Remote: ");
292 if (strlen
> 70) strlen
= 70;
293 memcpy(buf
+8, pktin
.body
+4, strlen
);
294 buf
[8+strlen
] = '\0';
297 } else if (pktin
.type
== SSH1_MSG_IGNORE
) {
305 static int ssh2_rdpkt(unsigned char **data
, int *datalen
)
307 static long len
, pad
, payload
, packetlen
, maclen
;
309 static int cipherblk
;
310 static unsigned long incoming_sequence
= 0;
320 cipherblk
= cipher
->blksize
;
326 if (pktin
.maxlen
< cipherblk
) {
327 pktin
.maxlen
= cipherblk
;
328 pktin
.data
= (pktin
.data
== NULL ?
malloc(cipherblk
+APIEXTRA
) :
329 realloc(pktin
.data
, cipherblk
+APIEXTRA
));
331 fatalbox("Out of memory");
335 * Acquire and decrypt the first block of the packet. This will
336 * contain the length and padding details.
338 for (i
= len
= 0; i
< cipherblk
; i
++) {
339 while ((*datalen
) == 0)
340 crReturn(cipherblk
-i
);
341 pktin
.data
[i
] = *(*data
)++;
345 if (!memcmp(pktin
.data
, "Remo", 4)) {/* "Remo"te server has closed ... */
350 sccipher
->decrypt(pktin
.data
, cipherblk
);
353 * Now get the length and padding figures.
355 len
= GET_32BIT(pktin
.data
);
359 * This enables us to deduce the payload length.
361 payload
= len
- pad
- 1;
363 pktin
.length
= payload
+ 5;
366 * So now we can work out the total packet length.
369 maclen
= scmac ? scmac
->len
: 0;
372 * Adjust memory allocation if packet is too big.
374 if (pktin
.maxlen
< packetlen
) {
375 pktin
.maxlen
= packetlen
;
376 pktin
.data
= (pktin
.data
== NULL ?
malloc(packetlen
+APIEXTRA
) :
377 realloc(pktin
.data
, packetlen
+APIEXTRA
));
379 fatalbox("Out of memory");
383 * Read and decrypt the remainder of the packet.
385 for (i
= cipherblk
; i
< packetlen
+ maclen
; i
++) {
386 while ((*datalen
) == 0)
387 crReturn(packetlen
+ maclen
- i
);
388 pktin
.data
[i
] = *(*data
)++;
391 /* Decrypt everything _except_ the MAC. */
393 sccipher
->decrypt(pktin
.data
+ cipherblk
, packetlen
- cipherblk
);
398 if (scmac
&& !scmac
->verify(pktin
.data
, incoming_sequence
++, len
+4))
399 fatalbox("Incorrect MAC received on packet");
402 pktin
.type
= pktin
.data
[5];
404 debug(("Got packet len=%d pad=%d\r\n", len
, pad
));
405 for (i
= 0; i
< payload
; i
++)
406 debug((" %02x", (unsigned char)pktin
.data
[i
]));
411 * FIXME: handle IGNORE and DEBUG messages.
417 static void ssh_gotdata(unsigned char *data
, int datalen
)
419 while (datalen
> 0) {
420 if ( s_rdpkt(&data
, &datalen
) == 0 ) {
421 ssh_protocol(NULL
, 0, 1);
422 if (ssh_state
== SSH_STATE_CLOSED
) {
430 static void s_wrpkt_start(int type
, int len
) {
433 len
+= 5; /* type and CRC */
437 pktout
.length
= len
-5;
438 if (pktout
.maxlen
< biglen
) {
439 pktout
.maxlen
= biglen
;
441 /* Allocate enough buffer space for extra block
442 * for MS CryptEncrypt() */
443 pktout
.data
= (pktout
.data
== NULL ?
malloc(biglen
+12) :
444 realloc(pktout
.data
, biglen
+12));
446 pktout
.data
= (pktout
.data
== NULL ?
malloc(biglen
+4) :
447 realloc(pktout
.data
, biglen
+4));
450 fatalbox("Out of memory");
454 pktout
.body
= pktout
.data
+4+pad
+1;
457 static void s_wrpkt(void) {
458 int pad
, len
, biglen
, i
;
461 len
= pktout
.length
+ 5; /* type and CRC */
465 pktout
.body
[-1] = pktout
.type
;
466 for (i
=0; i
<pad
; i
++)
467 pktout
.data
[i
+4] = random_byte();
468 crc
= crc32(pktout
.data
+4, biglen
-4);
469 PUT_32BIT(pktout
.data
+biglen
, crc
);
470 PUT_32BIT(pktout
.data
, len
);
473 cipher
->encrypt(pktout
.data
+4, biglen
);
475 s_write(pktout
.data
, biglen
+4);
479 * Construct a packet with the specified contents and
480 * send it to the server.
482 static void send_packet(int pkttype
, ...)
485 unsigned char *p
, *argp
, argchar
;
486 unsigned long argint
;
487 int pktlen
, argtype
, arglen
;
490 va_start(args
, pkttype
);
491 while ((argtype
= va_arg(args
, int)) != PKT_END
) {
494 (void) va_arg(args
, int);
498 (void) va_arg(args
, char);
502 (void) va_arg(args
, unsigned char *);
503 arglen
= va_arg(args
, int);
507 argp
= va_arg(args
, unsigned char *);
508 arglen
= strlen(argp
);
509 pktlen
+= 4 + arglen
;
517 s_wrpkt_start(pkttype
, pktlen
);
520 va_start(args
, pkttype
);
521 while ((argtype
= va_arg(args
, int)) != PKT_END
) {
524 argint
= va_arg(args
, int);
525 PUT_32BIT(p
, argint
);
529 argchar
= va_arg(args
, unsigned char);
534 argp
= va_arg(args
, unsigned char *);
535 arglen
= va_arg(args
, int);
536 memcpy(p
, argp
, arglen
);
540 argp
= va_arg(args
, unsigned char *);
541 arglen
= strlen(argp
);
542 PUT_32BIT(p
, arglen
);
543 memcpy(p
+ 4, argp
, arglen
);
555 * Connect to specified host and port.
556 * Returns an error message, or NULL on success.
557 * Also places the canonical host name into `realhost'.
559 static char *connect_to_host(char *host
, int port
, char **realhost
)
569 savedhost
= malloc(1+strlen(host
));
571 fatalbox("Out of memory");
572 strcpy(savedhost
, host
);
575 port
= 22; /* default ssh port */
587 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
588 if ( (h
= gethostbyname(host
)) == NULL
)
589 switch (WSAGetLastError()) {
590 case WSAENETDOWN
: return "Network is down";
591 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
592 return "Host does not exist";
593 case WSATRY_AGAIN
: return "Host not found";
594 default: return "gethostbyname: unknown error";
596 memcpy (&a
, h
->h_addr
, sizeof(a
));
597 *realhost
= h
->h_name
;
608 s
= socket(AF_INET
, SOCK_STREAM
, 0);
609 if (s
== INVALID_SOCKET
)
610 switch (WSAGetLastError()) {
611 case WSAENETDOWN
: return "Network is down";
612 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
613 default: return "socket(): unknown error";
617 * Bind to local address.
619 addr
.sin_family
= AF_INET
;
620 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
621 addr
.sin_port
= htons(0);
622 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
623 switch (WSAGetLastError()) {
624 case WSAENETDOWN
: return "Network is down";
625 default: return "bind(): unknown error";
629 * Connect to remote address.
631 addr
.sin_addr
.s_addr
= htonl(a
);
632 addr
.sin_port
= htons((short)port
);
633 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
634 switch (WSAGetLastError()) {
635 case WSAENETDOWN
: return "Network is down";
636 case WSAECONNREFUSED
: return "Connection refused";
637 case WSAENETUNREACH
: return "Network is unreachable";
638 case WSAEHOSTUNREACH
: return "No route to host";
639 default: return "connect(): unknown error";
643 send(s
, "connect ", 8, 0);
644 send(s
, FWhost
, strlen(FWhost
), 0);
647 sprintf(buf
, " %d\n", FWport
);
648 send (s
, buf
, strlen(buf
), 0);
655 static int ssh_versioncmp(char *a
, char *b
) {
657 unsigned long av
, bv
;
659 av
= strtoul(a
, &ae
, 10);
660 bv
= strtoul(b
, &be
, 10);
661 if (av
!= bv
) return (av
< bv ?
-1 : +1);
662 if (*ae
== '.') ae
++;
663 if (*be
== '.') be
++;
664 av
= strtoul(ae
, &ae
, 10);
665 bv
= strtoul(be
, &be
, 10);
666 if (av
!= bv
) return (av
< bv ?
-1 : +1);
672 * Utility routine for putting an SSH-protocol `string' into a SHA
676 void sha_string(SHA_State
*s
, void *str
, int len
) {
677 unsigned char lenblk
[4];
679 PUT_32BIT(lenblk
, len
);
680 if (!fp
) fp
= fopen("h:\\statham\\windows\\putty\\data","wb");
681 fwrite(lenblk
, 4, 1, fp
);
682 SHA_Bytes(s
, lenblk
, 4);
683 fwrite(str
, len
, 1, fp
);
685 SHA_Bytes(s
, str
, len
);
688 static int do_ssh_init(void) {
692 char vlog
[sizeof(vstring
)+20];
697 while (s_read(&c
, 1) == 1) {
698 if (c
== 'S' && i
< 2) i
++;
699 else if (c
== 'S' && i
== 2) i
= 2;
700 else if (c
== 'H' && i
== 2) break;
704 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
705 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
706 if (s_read(&c
,1) != 1 || c
!= 'H') return 0;
708 strcpy(vstring
, "SSH-");
710 if (s_read(&c
,1) != 1 || c
!= '-') return 0;
713 if (s_read(&c
,1) != 1)
715 if (vsp
< vstring
+sizeof(vstring
)-1)
721 } else if (i
< sizeof(version
)-1)
729 sprintf(vlog
, "Server version: %s", vstring
);
730 vlog
[strcspn(vlog
, "\r\n")] = '\0';
733 if (ssh_versioncmp(version
, "1.99") >= 0) {
735 * This is a v2 server. Begin v2 protocol.
737 char *verstring
= "SSH-2.0-PuTTY";
740 * Hash our version string and their version string.
742 sha_string(&exhash
, verstring
, strlen(verstring
));
743 sha_string(&exhash
, vstring
, strcspn(vstring
, "\r\n"));
744 sprintf(vstring
, "%s\n", verstring
);
745 sprintf(vlog
, "We claim version: %s", verstring
);
747 logevent("Using SSH protocol version 2");
748 s_write(vstring
, strlen(vstring
));
749 ssh_protocol
= ssh2_protocol
;
750 s_rdpkt
= ssh2_rdpkt
;
753 * This is a v1 server. Begin v1 protocol.
755 sprintf(vstring
, "SSH-%s-PuTTY\n",
756 (ssh_versioncmp(version
, "1.5") <= 0 ? version
: "1.5"));
757 sprintf(vlog
, "We claim version: %s", vstring
);
758 vlog
[strcspn(vlog
, "\r\n")] = '\0';
760 logevent("Using SSH protocol version 1");
761 s_write(vstring
, strlen(vstring
));
762 ssh_protocol
= ssh1_protocol
;
763 s_rdpkt
= ssh1_rdpkt
;
769 * Handle the key exchange and user authentication phases.
771 static int do_ssh1_login(unsigned char *in
, int inlen
, int ispkt
)
774 unsigned char session_id
[16];
775 unsigned char *rsabuf
, *keystr1
, *keystr2
;
776 unsigned char cookie
[8];
777 struct RSAKey servkey
, hostkey
;
778 struct MD5Context md5c
;
779 static unsigned long supported_ciphers_mask
, supported_auths_mask
;
784 if (!ispkt
) crWaitUntil(ispkt
);
786 if (pktin
.type
!= SSH1_SMSG_PUBLIC_KEY
)
787 fatalbox("Public key packet not received");
789 logevent("Received public keys");
791 memcpy(cookie
, pktin
.body
, 8);
793 i
= makekey(pktin
.body
+8, &servkey
, &keystr1
);
794 j
= makekey(pktin
.body
+8+i
, &hostkey
, &keystr2
);
797 * Hash the host key and print the hash in the log box. Just as
798 * a last resort in case the registry's host key checking is
799 * compromised, we'll allow the user some ability to verify
803 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
804 MD5Final(session_id
, &md5c
);
808 logevent("Host key MD5 is:");
810 for (i
= 0; i
< 16; i
++)
811 sprintf(logmsg
+strlen(logmsg
), "%02x", session_id
[i
]);
815 supported_ciphers_mask
= GET_32BIT(pktin
.body
+12+i
+j
);
816 supported_auths_mask
= GET_32BIT(pktin
.body
+16+i
+j
);
819 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
820 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
821 MD5Update(&md5c
, pktin
.body
, 8);
822 MD5Final(session_id
, &md5c
);
825 session_key
[i
] = random_byte();
827 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
829 rsabuf
= malloc(len
);
831 fatalbox("Out of memory");
834 * Verify the host key.
838 * First format the key into a string.
840 int len
= rsastr_len(&hostkey
);
841 char *keystr
= malloc(len
);
843 fatalbox("Out of memory");
844 rsastr_fmt(keystr
, &hostkey
);
845 verify_ssh_host_key(savedhost
, keystr
);
849 for (i
=0; i
<32; i
++) {
850 rsabuf
[i
] = session_key
[i
];
852 rsabuf
[i
] ^= session_id
[i
];
855 if (hostkey
.bytes
> servkey
.bytes
) {
856 rsaencrypt(rsabuf
, 32, &servkey
);
857 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
859 rsaencrypt(rsabuf
, 32, &hostkey
);
860 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
863 logevent("Encrypted session key");
865 cipher_type
= cfg
.cipher
== CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH
:
866 cfg
.cipher
== CIPHER_DES ? SSH_CIPHER_DES
:
868 if ((supported_ciphers_mask
& (1 << cipher_type
)) == 0) {
869 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
870 cipher_type
= SSH_CIPHER_3DES
;
872 switch (cipher_type
) {
873 case SSH_CIPHER_3DES
: logevent("Using 3DES encryption"); break;
874 case SSH_CIPHER_DES
: logevent("Using single-DES encryption"); break;
875 case SSH_CIPHER_BLOWFISH
: logevent("Using Blowfish encryption"); break;
878 send_packet(SSH1_CMSG_SESSION_KEY
,
879 PKT_CHAR
, cipher_type
,
881 PKT_CHAR
, (len
*8) >> 8, PKT_CHAR
, (len
*8) & 0xFF,
882 PKT_DATA
, rsabuf
, len
,
886 logevent("Trying to enable encryption...");
890 cipher
= cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish
:
891 cipher_type
== SSH_CIPHER_DES ?
&ssh_des
:
893 cipher
->sesskey(session_key
);
897 if (pktin
.type
!= SSH1_SMSG_SUCCESS
)
898 fatalbox("Encryption not successfully enabled");
900 logevent("Successfully started encryption");
904 static char username
[100];
907 if (!IS_SCP
&& !*cfg
.username
) {
908 c_write("login as: ", 10);
911 while (inlen
--) switch (c
= *in
++) {
933 if (((c
>= ' ' && c
<= '~') ||
934 ((unsigned char)c
>= 160)) && pos
< 40) {
942 username
[strcspn(username
, "\n\r")] = '\0';
945 strncpy(username
, cfg
.username
, 99);
948 sprintf(stuff
, "Sent username \"%s\".\r\n", username
);
949 c_write(stuff
, strlen(stuff
));
953 send_packet(SSH1_CMSG_USER
, PKT_STR
, username
, PKT_END
);
955 char userlog
[20+sizeof(username
)];
956 sprintf(userlog
, "Sent username \"%s\"", username
);
963 while (pktin
.type
== SSH1_SMSG_FAILURE
) {
964 static char password
[100];
967 static int pwpkt_type
;
970 * Show password prompt, having first obtained it via a TIS
971 * exchange if we're doing TIS authentication.
973 pwpkt_type
= SSH1_CMSG_AUTH_PASSWORD
;
977 sprintf(prompt
, "%s@%s's password: ", cfg
.username
, savedhost
);
978 if (!ssh_get_password(prompt
, password
, sizeof(password
))) {
980 * get_password failed to get a password (for
981 * example because one was supplied on the command
982 * line which has already failed to work).
985 logevent("No more passwords to try");
986 ssh_state
= SSH_STATE_CLOSED
;
991 if (pktin
.type
== SSH1_SMSG_FAILURE
&&
993 (supported_auths_mask
& (1<<SSH1_AUTH_TIS
))) {
994 pwpkt_type
= SSH1_CMSG_AUTH_TIS_RESPONSE
;
995 logevent("Requested TIS authentication");
996 send_packet(SSH1_CMSG_AUTH_TIS
, PKT_END
);
998 if (pktin
.type
!= SSH1_SMSG_AUTH_TIS_CHALLENGE
) {
999 logevent("TIS authentication declined");
1000 c_write("TIS authentication refused.\r\n", 29);
1002 int challengelen
= ((pktin
.body
[0] << 24) |
1003 (pktin
.body
[1] << 16) |
1004 (pktin
.body
[2] << 8) |
1006 logevent("Received TIS challenge");
1007 c_write(pktin
.body
+4, challengelen
);
1010 if (pwpkt_type
== SSH1_CMSG_AUTH_PASSWORD
)
1011 c_write("password: ", 10);
1015 crWaitUntil(!ispkt
);
1016 while (inlen
--) switch (c
= *in
++) {
1033 if (((c
>= ' ' && c
<= '~') ||
1034 ((unsigned char)c
>= 160)) && pos
< 40)
1035 password
[pos
++] = c
;
1043 send_packet(pwpkt_type
, PKT_STR
, password
, PKT_END
);
1044 logevent("Sent password");
1045 memset(password
, 0, strlen(password
));
1047 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1048 c_write("Access denied\r\n", 15);
1049 logevent("Authentication refused");
1050 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1051 logevent("Received disconnect request");
1052 ssh_state
= SSH_STATE_CLOSED
;
1054 } else if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1055 fatalbox("Strange packet received, type %d", pktin
.type
);
1059 logevent("Authentication successful");
1064 static void ssh1_protocol(unsigned char *in
, int inlen
, int ispkt
) {
1069 while (!do_ssh1_login(in
, inlen
, ispkt
)) {
1072 if (ssh_state
== SSH_STATE_CLOSED
)
1076 send_packet(SSH1_CMSG_REQUEST_PTY
,
1077 PKT_STR
, cfg
.termtype
,
1078 PKT_INT
, rows
, PKT_INT
, cols
,
1079 PKT_INT
, 0, PKT_INT
, 0,
1082 ssh_state
= SSH_STATE_INTERMED
;
1083 do { crReturnV
; } while (!ispkt
);
1084 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1085 fatalbox("Protocol confusion");
1086 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1087 c_write("Server refused to allocate pty\r\n", 32);
1089 logevent("Allocated pty");
1092 send_packet(SSH1_CMSG_EXEC_SHELL
, PKT_END
);
1093 logevent("Started session");
1095 ssh_state
= SSH_STATE_SESSION
;
1102 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
||
1103 pktin
.type
== SSH1_SMSG_STDERR_DATA
) {
1104 long len
= GET_32BIT(pktin
.body
);
1105 c_write(pktin
.body
+4, len
);
1106 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1107 ssh_state
= SSH_STATE_CLOSED
;
1108 logevent("Received disconnect request");
1109 } else if (pktin
.type
== SSH1_SMSG_SUCCESS
) {
1110 /* may be from EXEC_SHELL on some servers */
1111 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1112 /* may be from EXEC_SHELL on some servers
1113 * if no pty is available or in other odd cases. Ignore */
1114 } else if (pktin
.type
== SSH1_SMSG_EXIT_STATUS
) {
1115 send_packet(SSH1_CMSG_EXIT_CONFIRMATION
, PKT_END
);
1117 fatalbox("Strange packet received: type %d", pktin
.type
);
1120 send_packet(SSH1_CMSG_STDIN_DATA
,
1121 PKT_INT
, inlen
, PKT_DATA
, in
, inlen
, PKT_END
);
1129 * SSH2 packet construction functions.
1131 void ssh2_pkt_adddata(void *data
, int len
) {
1132 pktout
.length
+= len
;
1133 if (pktout
.maxlen
< pktout
.length
) {
1134 pktout
.maxlen
= pktout
.length
+ 256;
1135 pktout
.data
= (pktout
.data
== NULL ?
malloc(pktout
.maxlen
+APIEXTRA
) :
1136 realloc(pktout
.data
, pktout
.maxlen
+APIEXTRA
));
1138 fatalbox("Out of memory");
1140 memcpy(pktout
.data
+pktout
.length
-len
, data
, len
);
1142 void ssh2_pkt_addbyte(unsigned char byte
) {
1143 ssh2_pkt_adddata(&byte
, 1);
1145 void ssh2_pkt_init(int pkt_type
) {
1147 ssh2_pkt_addbyte((unsigned char)pkt_type
);
1149 void ssh2_pkt_addbool(unsigned char value
) {
1150 ssh2_pkt_adddata(&value
, 1);
1152 void ssh2_pkt_adduint32(unsigned long value
) {
1154 PUT_32BIT(x
, value
);
1155 ssh2_pkt_adddata(x
, 4);
1157 void ssh2_pkt_addstring_start(void) {
1158 ssh2_pkt_adduint32(0);
1159 pktout
.savedpos
= pktout
.length
;
1161 void ssh2_pkt_addstring_str(char *data
) {
1162 ssh2_pkt_adddata(data
, strlen(data
));
1163 PUT_32BIT(pktout
.data
+ pktout
.savedpos
- 4,
1164 pktout
.length
- pktout
.savedpos
);
1166 void ssh2_pkt_addstring_data(char *data
, int len
) {
1167 ssh2_pkt_adddata(data
, len
);
1168 PUT_32BIT(pktout
.data
+ pktout
.savedpos
- 4,
1169 pktout
.length
- pktout
.savedpos
);
1171 void ssh2_pkt_addstring(char *data
) {
1172 ssh2_pkt_addstring_start();
1173 ssh2_pkt_addstring_str(data
);
1175 char *ssh2_mpint_fmt(Bignum b
, int *len
) {
1178 p
= malloc(n
* 2 + 1);
1180 fatalbox("out of memory");
1182 for (i
= 0; i
< n
; i
++) {
1183 p
[i
*2+1] = (b
[n
-i
] >> 8) & 0xFF;
1184 p
[i
*2+2] = (b
[n
-i
] ) & 0xFF;
1187 while (p
[i
] == 0 && (p
[i
+1] & 0x80) == 0)
1189 memmove(p
, p
+i
, n
*2+1-i
);
1193 void ssh2_pkt_addmp(Bignum b
) {
1196 p
= ssh2_mpint_fmt(b
, &len
);
1197 ssh2_pkt_addstring_start();
1198 ssh2_pkt_addstring_data(p
, len
);
1201 void ssh2_pkt_send(void) {
1202 int cipherblk
, maclen
, padding
, i
;
1203 unsigned long outgoing_sequence
= 0;
1206 * Add padding. At least four bytes, and must also bring total
1207 * length (minus MAC) up to a multiple of the block size.
1209 cipherblk
= cipher ? cipher
->blksize
: 8; /* block size */
1210 cipherblk
= cipherblk
< 8 ?
8 : cipherblk
; /* or 8 if blksize < 8 */
1212 padding
+= (cipherblk
- (pktout
.length
+ padding
) % cipherblk
) % cipherblk
;
1213 pktout
.data
[4] = padding
;
1214 for (i
= 0; i
< padding
; i
++)
1215 pktout
.data
[pktout
.length
+ i
] = random_byte();
1216 PUT_32BIT(pktout
.data
, pktout
.length
+ padding
- 4);
1218 csmac
->generate(pktout
.data
, outgoing_sequence
++,
1219 pktout
.length
+ padding
);
1221 cscipher
->encrypt(pktout
.data
, pktout
.length
+ padding
);
1222 maclen
= csmac ? csmac
->len
: 0;
1224 debug(("Sending packet len=%d\r\n", pktout
.length
+padding
+maclen
));
1225 for (i
= 0; i
< pktout
.length
+padding
+maclen
; i
++)
1226 debug((" %02x", (unsigned char)pktout
.data
[i
]));
1229 s_write(pktout
.data
, pktout
.length
+ padding
+ maclen
);
1232 void sha_mpint(SHA_State
*s
, Bignum b
) {
1235 p
= ssh2_mpint_fmt(b
, &len
);
1236 sha_string(s
, p
, len
);
1241 * SSH2 packet decode functions.
1243 void ssh2_pkt_getstring(char **p
, int *length
) {
1245 if (pktin
.length
- pktin
.savedpos
< 4)
1247 *length
= GET_32BIT(pktin
.data
+pktin
.savedpos
);
1248 pktin
.savedpos
+= 4;
1249 if (pktin
.length
- pktin
.savedpos
< *length
)
1251 *p
= pktin
.data
+pktin
.savedpos
;
1252 pktin
.savedpos
+= *length
;
1254 Bignum
ssh2_pkt_getmp(void) {
1259 ssh2_pkt_getstring(&p
, &length
);
1263 fatalbox("internal error: Can't handle negative mpints");
1264 b
= newbn((length
+1)/2);
1265 for (i
= 0; i
< length
; i
++) {
1268 b
[j
/2+1] |= ((unsigned char)p
[i
]) << 8;
1270 b
[j
/2+1] |= ((unsigned char)p
[i
]);
1275 void bndebug(char *string
, Bignum b
) {
1278 p
= ssh2_mpint_fmt(b
, &len
);
1279 debug(("%s", string
));
1280 for (i
= 0; i
< len
; i
++)
1281 debug((" %02x", p
[i
]));
1287 * Utility routine for decoding comma-separated strings in KEXINIT.
1289 int in_commasep_string(char *needle
, char *haystack
, int haylen
) {
1290 int needlen
= strlen(needle
);
1293 * Is it at the start of the string?
1295 if (haylen
>= needlen
&& /* haystack is long enough */
1296 !memcmp(needle
, haystack
, needlen
) && /* initial match */
1297 (haylen
== needlen
|| haystack
[needlen
] == ',')
1298 /* either , or EOS follows */
1302 * If not, search for the next comma and resume after that.
1303 * If no comma found, terminate.
1305 while (haylen
> 0 && *haystack
!= ',')
1306 haylen
--, haystack
++;
1309 haylen
--, haystack
++; /* skip over comma itself */
1314 * Handle the SSH2 key exchange phase.
1316 static int do_ssh2_kex(unsigned char *in
, int inlen
, int ispkt
)
1320 static Bignum e
, f
, K
;
1321 static struct ssh_cipher
*cscipher_tobe
= NULL
;
1322 static struct ssh_cipher
*sccipher_tobe
= NULL
;
1323 static struct ssh_mac
*csmac_tobe
= NULL
;
1324 static struct ssh_mac
*scmac_tobe
= NULL
;
1325 static struct ssh_compress
*cscomp_tobe
= NULL
;
1326 static struct ssh_compress
*sccomp_tobe
= NULL
;
1327 static char *hostkeydata
, *sigdata
;
1328 static int hostkeylen
, siglen
;
1329 static unsigned char exchange_hash
[20];
1334 * Construct and send our key exchange packet.
1336 ssh2_pkt_init(SSH2_MSG_KEXINIT
);
1337 for (i
= 0; i
< 16; i
++)
1338 ssh2_pkt_addbyte((unsigned char)random_byte());
1339 /* List key exchange algorithms. */
1340 ssh2_pkt_addstring_start();
1341 for (i
= 0; i
< lenof(kex_algs
); i
++) {
1342 ssh2_pkt_addstring_str(kex_algs
[i
]->name
);
1343 if (i
< lenof(kex_algs
)-1)
1344 ssh2_pkt_addstring_str(",");
1346 /* List server host key algorithms. */
1347 ssh2_pkt_addstring_start();
1348 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
1349 ssh2_pkt_addstring_str(hostkey_algs
[i
]->name
);
1350 if (i
< lenof(hostkey_algs
)-1)
1351 ssh2_pkt_addstring_str(",");
1353 /* List client->server encryption algorithms. */
1354 ssh2_pkt_addstring_start();
1355 for (i
= 0; i
< lenof(ciphers
); i
++) {
1356 ssh2_pkt_addstring_str(ciphers
[i
]->name
);
1357 if (i
< lenof(ciphers
)-1)
1358 ssh2_pkt_addstring_str(",");
1360 /* List server->client encryption algorithms. */
1361 ssh2_pkt_addstring_start();
1362 for (i
= 0; i
< lenof(ciphers
); i
++) {
1363 ssh2_pkt_addstring_str(ciphers
[i
]->name
);
1364 if (i
< lenof(ciphers
)-1)
1365 ssh2_pkt_addstring_str(",");
1367 /* List client->server MAC algorithms. */
1368 ssh2_pkt_addstring_start();
1369 for (i
= 0; i
< lenof(macs
); i
++) {
1370 ssh2_pkt_addstring_str(macs
[i
]->name
);
1371 if (i
< lenof(macs
)-1)
1372 ssh2_pkt_addstring_str(",");
1374 /* List server->client MAC algorithms. */
1375 ssh2_pkt_addstring_start();
1376 for (i
= 0; i
< lenof(macs
); i
++) {
1377 ssh2_pkt_addstring_str(macs
[i
]->name
);
1378 if (i
< lenof(macs
)-1)
1379 ssh2_pkt_addstring_str(",");
1381 /* List client->server compression algorithms. */
1382 ssh2_pkt_addstring_start();
1383 for (i
= 0; i
< lenof(compressions
); i
++) {
1384 ssh2_pkt_addstring_str(compressions
[i
]->name
);
1385 if (i
< lenof(compressions
)-1)
1386 ssh2_pkt_addstring_str(",");
1388 /* List server->client compression algorithms. */
1389 ssh2_pkt_addstring_start();
1390 for (i
= 0; i
< lenof(compressions
); i
++) {
1391 ssh2_pkt_addstring_str(compressions
[i
]->name
);
1392 if (i
< lenof(compressions
)-1)
1393 ssh2_pkt_addstring_str(",");
1395 /* List client->server languages. Empty list. */
1396 ssh2_pkt_addstring_start();
1397 /* List server->client languages. Empty list. */
1398 ssh2_pkt_addstring_start();
1399 /* First KEX packet does _not_ follow, because we're not that brave. */
1400 ssh2_pkt_addbool(FALSE
);
1402 ssh2_pkt_adduint32(0);
1403 sha_string(&exhash
, pktout
.data
+5, pktout
.length
-5);
1406 if (!ispkt
) crWaitUntil(ispkt
);
1407 sha_string(&exhash
, pktin
.data
+5, pktin
.length
-5);
1410 * Now examine the other side's KEXINIT to see what we're up
1413 if (pktin
.type
!= SSH2_MSG_KEXINIT
)
1414 fatalbox("expected key exchange packet from server");
1415 kex
= NULL
; hostkey
= NULL
; cscipher_tobe
= NULL
; sccipher_tobe
= NULL
;
1416 csmac_tobe
= NULL
; scmac_tobe
= NULL
; cscomp_tobe
= NULL
; sccomp_tobe
= NULL
;
1417 pktin
.savedpos
+= 16; /* skip garbage cookie */
1418 ssh2_pkt_getstring(&str
, &len
); /* key exchange algorithms */
1419 for (i
= 0; i
< lenof(kex_algs
); i
++) {
1420 if (in_commasep_string(kex_algs
[i
]->name
, str
, len
)) {
1425 ssh2_pkt_getstring(&str
, &len
); /* host key algorithms */
1426 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
1427 if (in_commasep_string(hostkey_algs
[i
]->name
, str
, len
)) {
1428 hostkey
= hostkey_algs
[i
];
1432 ssh2_pkt_getstring(&str
, &len
); /* client->server cipher */
1433 for (i
= 0; i
< lenof(ciphers
); i
++) {
1434 if (in_commasep_string(ciphers
[i
]->name
, str
, len
)) {
1435 cscipher_tobe
= ciphers
[i
];
1439 ssh2_pkt_getstring(&str
, &len
); /* server->client cipher */
1440 for (i
= 0; i
< lenof(ciphers
); i
++) {
1441 if (in_commasep_string(ciphers
[i
]->name
, str
, len
)) {
1442 sccipher_tobe
= ciphers
[i
];
1446 ssh2_pkt_getstring(&str
, &len
); /* client->server mac */
1447 for (i
= 0; i
< lenof(macs
); i
++) {
1448 if (in_commasep_string(macs
[i
]->name
, str
, len
)) {
1449 csmac_tobe
= macs
[i
];
1453 ssh2_pkt_getstring(&str
, &len
); /* server->client mac */
1454 for (i
= 0; i
< lenof(macs
); i
++) {
1455 if (in_commasep_string(macs
[i
]->name
, str
, len
)) {
1456 scmac_tobe
= macs
[i
];
1460 ssh2_pkt_getstring(&str
, &len
); /* client->server compression */
1461 for (i
= 0; i
< lenof(compressions
); i
++) {
1462 if (in_commasep_string(compressions
[i
]->name
, str
, len
)) {
1463 cscomp_tobe
= compressions
[i
];
1467 ssh2_pkt_getstring(&str
, &len
); /* server->client compression */
1468 for (i
= 0; i
< lenof(compressions
); i
++) {
1469 if (in_commasep_string(compressions
[i
]->name
, str
, len
)) {
1470 sccomp_tobe
= compressions
[i
];
1474 debug(("key exchange is %s\r\n", kex ? kex
->name
: NULL
));
1475 debug(("host key alg is %s\r\n", hostkey ? hostkey
->name
: NULL
));
1476 debug(("cscipher alg is %s\r\n", cscipher_tobe ? cscipher_tobe
->name
: NULL
));
1477 debug(("sccipher alg is %s\r\n", sccipher_tobe ? sccipher_tobe
->name
: NULL
));
1478 debug(("csmac alg is %s\r\n", csmac_tobe ? csmac_tobe
->name
: NULL
));
1479 debug(("scmac alg is %s\r\n", scmac_tobe ? scmac_tobe
->name
: NULL
));
1480 debug(("cscomp alg is %s\r\n", cscomp_tobe ? cscomp_tobe
->name
: NULL
));
1481 debug(("sccomp alg is %s\r\n", sccomp_tobe ? sccomp_tobe
->name
: NULL
));
1484 * Currently we only support Diffie-Hellman and DSS, so let's
1485 * bomb out if those aren't selected.
1487 if (kex
!= &ssh_diffiehellman
|| hostkey
!= &ssh_dss
)
1488 fatalbox("internal fault: chaos in SSH 2 transport layer");
1491 * Now we begin the fun. Generate and send e for Diffie-Hellman.
1495 ssh2_pkt_init(SSH2_MSG_KEXDH_INIT
);
1500 if (pktin
.type
!= SSH2_MSG_KEXDH_REPLY
)
1501 fatalbox("expected key exchange packet from server");
1502 ssh2_pkt_getstring(&hostkeydata
, &hostkeylen
);
1503 f
= ssh2_pkt_getmp();
1505 ssh2_pkt_getstring(&sigdata
, &siglen
);
1510 sha_string(&exhash
, hostkeydata
, hostkeylen
);
1511 sha_mpint(&exhash
, e
);
1512 sha_mpint(&exhash
, f
);
1513 sha_mpint(&exhash
, K
);
1514 SHA_Final(&exhash
, exchange_hash
);
1516 debug(("Exchange hash is:\r\n"));
1517 for (i
= 0; i
< 20; i
++)
1518 debug((" %02x", exchange_hash
[i
]));
1522 * FIXME: verify hostkeydata and sigdata.
1530 static void ssh2_protocol(unsigned char *in
, int inlen
, int ispkt
) {
1535 while (!do_ssh2_kex(in
, inlen
, ispkt
)) {
1543 * Called to set up the connection. Will arrange for WM_NETEVENT
1544 * messages to be passed to the specified window, whose window
1545 * procedure should then call telnet_msg().
1547 * Returns an error message, or NULL on success.
1549 static char *ssh_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
1553 if(crypto_startup() == 0)
1554 return "Microsoft high encryption pack not installed!";
1557 p
= connect_to_host(host
, port
, realhost
);
1562 return "Protocol initialisation error";
1564 if (WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1565 switch (WSAGetLastError()) {
1566 case WSAENETDOWN
: return "Network is down";
1567 default: return "WSAAsyncSelect(): unknown error";
1574 * Process a WM_NETEVENT message. Will return 0 if the connection
1575 * has closed, or <0 for a socket error.
1577 static int ssh_msg (WPARAM wParam
, LPARAM lParam
) {
1582 * Because reading less than the whole of the available pending
1583 * data can generate an FD_READ event, we need to allow for the
1584 * possibility that FD_READ may arrive with FD_CLOSE already in
1585 * the queue; so it's possible that we can get here even with s
1586 * invalid. If so, we return 1 and don't worry about it.
1588 if (s
== INVALID_SOCKET
)
1591 if (WSAGETSELECTERROR(lParam
) != 0)
1592 return -WSAGETSELECTERROR(lParam
);
1594 switch (WSAGETSELECTEVENT(lParam
)) {
1597 ret
= recv(s
, buf
, sizeof(buf
), 0);
1598 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
1600 if (ret
< 0) /* any _other_ error */
1601 return -10000-WSAGetLastError();
1606 ssh_gotdata (buf
, ret
);
1607 if (ssh_state
== SSH_STATE_CLOSED
) {
1614 return 1; /* shouldn't happen, but WTF */
1618 * Called to send data down the Telnet connection.
1620 static void ssh_send (char *buf
, int len
) {
1621 if (s
== INVALID_SOCKET
)
1624 ssh_protocol(buf
, len
, 0);
1628 * Called to set the size of the window from Telnet's POV.
1630 static void ssh_size(void) {
1631 switch (ssh_state
) {
1632 case SSH_STATE_BEFORE_SIZE
:
1633 case SSH_STATE_CLOSED
:
1634 break; /* do nothing */
1635 case SSH_STATE_INTERMED
:
1636 size_needed
= TRUE
; /* buffer for later */
1638 case SSH_STATE_SESSION
:
1640 send_packet(SSH1_CMSG_WINDOW_SIZE
,
1641 PKT_INT
, rows
, PKT_INT
, cols
,
1642 PKT_INT
, 0, PKT_INT
, 0, PKT_END
);
1648 * (Send Telnet special codes)
1650 static void ssh_special (Telnet_Special code
) {
1656 * Read and decrypt one incoming SSH packet.
1657 * (only used by pSCP)
1659 static void get_packet(void)
1661 unsigned char buf
[4096], *p
;
1670 while ((to_read
= s_rdpkt(&p
, &len
)) > 0) {
1671 if (to_read
> sizeof(buf
)) to_read
= sizeof(buf
);
1672 len
= s_read(buf
, to_read
);
1673 if (len
!= to_read
) {
1685 * Receive a block of data over the SSH link. Block until
1686 * all data is available. Return nr of bytes read (0 if lost connection).
1687 * (only used by pSCP)
1689 int ssh_scp_recv(unsigned char *buf
, int len
)
1691 static int pending_input_len
= 0;
1692 static unsigned char *pending_input_ptr
;
1697 if (pending_input_len
>= to_read
) {
1698 memcpy(buf
, pending_input_ptr
, to_read
);
1699 pending_input_ptr
+= to_read
;
1700 pending_input_len
-= to_read
;
1704 if (pending_input_len
> 0) {
1705 memcpy(buf
, pending_input_ptr
, pending_input_len
);
1706 buf
+= pending_input_len
;
1707 to_read
-= pending_input_len
;
1708 pending_input_len
= 0;
1711 if (s
== INVALID_SOCKET
)
1713 while (to_read
> 0) {
1715 if (s
== INVALID_SOCKET
)
1717 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
) {
1718 int plen
= GET_32BIT(pktin
.body
);
1719 if (plen
<= to_read
) {
1720 memcpy(buf
, pktin
.body
+ 4, plen
);
1724 memcpy(buf
, pktin
.body
+ 4, to_read
);
1725 pending_input_len
= plen
- to_read
;
1726 pending_input_ptr
= pktin
.body
+ 4 + to_read
;
1729 } else if (pktin
.type
== SSH1_SMSG_STDERR_DATA
) {
1730 int plen
= GET_32BIT(pktin
.body
);
1731 fwrite(pktin
.body
+ 4, plen
, 1, stderr
);
1732 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1733 logevent("Received disconnect request");
1734 } else if (pktin
.type
== SSH1_SMSG_SUCCESS
||
1735 pktin
.type
== SSH1_SMSG_FAILURE
) {
1737 } else if (pktin
.type
== SSH1_SMSG_EXIT_STATUS
) {
1739 sprintf(logbuf
, "Remote exit status: %d", GET_32BIT(pktin
.body
));
1741 send_packet(SSH1_CMSG_EXIT_CONFIRMATION
, PKT_END
);
1742 logevent("Closing connection");
1746 fatalbox("Strange packet received: type %d", pktin
.type
);
1754 * Send a block of data over the SSH link.
1755 * Block until all data is sent.
1756 * (only used by pSCP)
1758 void ssh_scp_send(unsigned char *buf
, int len
)
1761 if (s
== INVALID_SOCKET
)
1763 send_packet(SSH1_CMSG_STDIN_DATA
,
1764 PKT_INT
, len
, PKT_DATA
, buf
, len
, PKT_END
);
1768 * Send an EOF notification to the server.
1769 * (only used by pSCP)
1771 void ssh_scp_send_eof(void)
1774 if (s
== INVALID_SOCKET
)
1776 send_packet(SSH1_CMSG_EOF
, PKT_END
);
1780 * Set up the connection, login on the remote host and
1781 * start execution of a command.
1782 * Returns an error message, or NULL on success.
1783 * (only used by pSCP)
1785 char *ssh_scp_init(char *host
, int port
, char *cmd
, char **realhost
)
1792 if (crypto_startup() == 0)
1793 return "Microsoft high encryption pack not installed!";
1796 p
= connect_to_host(host
, port
, realhost
);
1803 return "Protocol initialisation error";
1805 /* Exchange keys and login */
1808 if (s
== INVALID_SOCKET
)
1809 return "Connection closed by remote host";
1810 } while (!do_ssh1_login(NULL
, 0, 1));
1812 if (ssh_state
== SSH_STATE_CLOSED
) {
1815 return "Session initialisation error";
1818 /* Execute command */
1819 sprintf(buf
, "Sending command: %.100s", cmd
);
1821 send_packet(SSH1_CMSG_EXEC_CMD
, PKT_STR
, cmd
, PKT_END
);
1827 Backend ssh_backend
= {