18 #define logevent(s) { logevent(s); \
19 if (!(flags & FLAG_CONNECTION) && (flags & FLAG_VERBOSE)) \
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_RSA 6
27 #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7
28 #define SSH1_CMSG_AUTH_RSA_RESPONSE 8
29 #define SSH1_CMSG_AUTH_PASSWORD 9
30 #define SSH1_CMSG_REQUEST_PTY 10
31 #define SSH1_CMSG_WINDOW_SIZE 11
32 #define SSH1_CMSG_EXEC_SHELL 12
33 #define SSH1_CMSG_EXEC_CMD 13
34 #define SSH1_SMSG_SUCCESS 14
35 #define SSH1_SMSG_FAILURE 15
36 #define SSH1_CMSG_STDIN_DATA 16
37 #define SSH1_SMSG_STDOUT_DATA 17
38 #define SSH1_SMSG_STDERR_DATA 18
39 #define SSH1_CMSG_EOF 19
40 #define SSH1_SMSG_EXIT_STATUS 20
41 #define SSH1_CMSG_EXIT_CONFIRMATION 33
42 #define SSH1_MSG_IGNORE 32
43 #define SSH1_MSG_DEBUG 36
44 #define SSH1_CMSG_AUTH_TIS 39
45 #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40
46 #define SSH1_CMSG_AUTH_TIS_RESPONSE 41
47 #define SSH1_CMSG_AUTH_CCARD 70
48 #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71
49 #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72
51 #define SSH1_AUTH_TIS 5
52 #define SSH1_AUTH_CCARD 16
54 #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1
55 #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2
56 #define SSH_AGENTC_RSA_CHALLENGE 3
57 #define SSH_AGENT_RSA_RESPONSE 4
58 #define SSH_AGENT_FAILURE 5
59 #define SSH_AGENT_SUCCESS 6
60 #define SSH_AGENTC_ADD_RSA_IDENTITY 7
61 #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8
63 #define SSH2_MSG_DISCONNECT 1
64 #define SSH2_MSG_IGNORE 2
65 #define SSH2_MSG_UNIMPLEMENTED 3
66 #define SSH2_MSG_DEBUG 4
67 #define SSH2_MSG_SERVICE_REQUEST 5
68 #define SSH2_MSG_SERVICE_ACCEPT 6
69 #define SSH2_MSG_KEXINIT 20
70 #define SSH2_MSG_NEWKEYS 21
71 #define SSH2_MSG_KEXDH_INIT 30
72 #define SSH2_MSG_KEXDH_REPLY 31
73 #define SSH2_MSG_USERAUTH_REQUEST 50
74 #define SSH2_MSG_USERAUTH_FAILURE 51
75 #define SSH2_MSG_USERAUTH_SUCCESS 52
76 #define SSH2_MSG_USERAUTH_BANNER 53
77 #define SSH2_MSG_USERAUTH_PK_OK 60
78 #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60
79 #define SSH2_MSG_GLOBAL_REQUEST 80
80 #define SSH2_MSG_REQUEST_SUCCESS 81
81 #define SSH2_MSG_REQUEST_FAILURE 82
82 #define SSH2_MSG_CHANNEL_OPEN 90
83 #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91
84 #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92
85 #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93
86 #define SSH2_MSG_CHANNEL_DATA 94
87 #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95
88 #define SSH2_MSG_CHANNEL_EOF 96
89 #define SSH2_MSG_CHANNEL_CLOSE 97
90 #define SSH2_MSG_CHANNEL_REQUEST 98
91 #define SSH2_MSG_CHANNEL_SUCCESS 99
92 #define SSH2_MSG_CHANNEL_FAILURE 100
94 #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1
95 #define SSH2_OPEN_CONNECT_FAILED 2
96 #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3
97 #define SSH2_OPEN_RESOURCE_SHORTAGE 4
98 #define SSH2_EXTENDED_DATA_STDERR 1
100 #define GET_32BIT(cp) \
101 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
102 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
103 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
104 ((unsigned long)(unsigned char)(cp)[3]))
106 #define PUT_32BIT(cp, value) { \
107 (cp)[0] = (unsigned char)((value) >> 24); \
108 (cp)[1] = (unsigned char)((value) >> 16); \
109 (cp)[2] = (unsigned char)((value) >> 8); \
110 (cp)[3] = (unsigned char)(value); }
112 enum { PKT_END
, PKT_INT
, PKT_CHAR
, PKT_DATA
, PKT_STR
, PKT_BIGNUM
};
114 /* Coroutine mechanics for the sillier bits of the code */
115 #define crBegin1 static int crLine = 0;
116 #define crBegin2 switch(crLine) { case 0:;
117 #define crBegin crBegin1; crBegin2;
118 #define crFinish(z) } crLine = 0; return (z)
119 #define crFinishV } crLine = 0; return
120 #define crReturn(z) \
122 crLine=__LINE__; return (z); case __LINE__:;\
126 crLine=__LINE__; return; case __LINE__:;\
128 #define crStop(z) do{ crLine = 0; return (z); }while(0)
129 #define crStopV do{ crLine = 0; return; }while(0)
130 #define crWaitUntil(c) do { crReturn(0); } while (!(c))
131 #define crWaitUntilV(c) do { crReturnV; } while (!(c))
133 extern struct ssh_cipher ssh_3des
;
134 extern struct ssh_cipher ssh_3des_ssh2
;
135 extern struct ssh_cipher ssh_des
;
136 extern struct ssh_cipher ssh_blowfish
;
138 /* for ssh 2; we miss out single-DES because it isn't supported */
139 struct ssh_cipher
*ciphers
[] = { &ssh_3des_ssh2
, &ssh_blowfish
};
141 extern struct ssh_kex ssh_diffiehellman
;
142 struct ssh_kex
*kex_algs
[] = { &ssh_diffiehellman
};
144 extern struct ssh_hostkey ssh_dss
;
145 struct ssh_hostkey
*hostkey_algs
[] = { &ssh_dss
};
147 extern struct ssh_mac ssh_sha1
;
151 static void nullmac_key(unsigned char *key
) { }
152 static void nullmac_generate(unsigned char *blk
, int len
, unsigned long seq
) { }
153 static int nullmac_verify(unsigned char *blk
, int len
, unsigned long seq
) { return 1; }
154 struct ssh_mac ssh_mac_none
= {
155 nullmac_key
, nullmac_key
, nullmac_generate
, nullmac_verify
, "none", 0
157 struct ssh_mac
*macs
[] = { &ssh_sha1
, &ssh_mac_none
};
159 struct ssh_compress ssh_comp_none
= {
162 struct ssh_compress
*compressions
[] = { &ssh_comp_none
};
164 static SOCKET s
= INVALID_SOCKET
;
166 static unsigned char session_key
[32];
167 static struct ssh_cipher
*cipher
= NULL
;
168 static struct ssh_cipher
*cscipher
= NULL
;
169 static struct ssh_cipher
*sccipher
= NULL
;
170 static struct ssh_mac
*csmac
= NULL
;
171 static struct ssh_mac
*scmac
= NULL
;
172 static struct ssh_compress
*cscomp
= NULL
;
173 static struct ssh_compress
*sccomp
= NULL
;
174 static struct ssh_kex
*kex
= NULL
;
175 static struct ssh_hostkey
*hostkey
= NULL
;
176 int (*ssh_get_password
)(const char *prompt
, char *str
, int maxlen
) = NULL
;
178 static char *savedhost
;
179 static int ssh_send_ok
;
182 SSH_STATE_BEFORE_SIZE
,
186 } ssh_state
= SSH_STATE_BEFORE_SIZE
;
188 static int size_needed
= FALSE
;
190 static void s_write (char *buf
, int len
) {
192 int i
= send (s
, buf
, len
, 0);
195 fatalbox("Lost connection while sending");
201 static int s_read (char *buf
, int len
) {
204 int i
= recv (s
, buf
, len
, 0);
207 len
-= i
, buf
+= i
, ret
+= i
;
214 static void c_write (char *buf
, int len
) {
215 if (!(flags
& FLAG_CONNECTION
)) {
217 for (i
= 0; i
< len
; i
++)
219 fputc(buf
[i
], stderr
);
235 static struct Packet pktin
= { 0, 0, NULL
, NULL
, 0 };
236 static struct Packet pktout
= { 0, 0, NULL
, NULL
, 0 };
238 static int ssh_version
;
239 static void (*ssh_protocol
)(unsigned char *in
, int inlen
, int ispkt
);
240 static void ssh1_protocol(unsigned char *in
, int inlen
, int ispkt
);
241 static void ssh2_protocol(unsigned char *in
, int inlen
, int ispkt
);
242 static void ssh_size(void);
244 static int (*s_rdpkt
)(unsigned char **data
, int *datalen
);
247 * Collect incoming data in the incoming packet buffer.
248 * Decipher and verify the packet when it is completely read.
249 * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets.
250 * Update the *data and *datalen variables.
251 * Return the additional nr of bytes needed, or 0 when
252 * a complete packet is available.
254 static int ssh1_rdpkt(unsigned char **data
, int *datalen
)
256 static long len
, pad
, biglen
, to_read
;
257 static unsigned long realcrc
, gotcrc
;
258 static unsigned char *p
;
268 for (i
= len
= 0; i
< 4; i
++) {
269 while ((*datalen
) == 0)
271 len
= (len
<< 8) + **data
;
272 (*data
)++, (*datalen
)--;
276 if (len
== 0x52656d6f) { /* "Remo"te server has closed ... */
277 len
= 0x300; /* big enough to carry to end */
283 pktin
.length
= len
- 5;
285 if (pktin
.maxlen
< biglen
) {
286 pktin
.maxlen
= biglen
;
287 pktin
.data
= (pktin
.data
== NULL ?
malloc(biglen
+APIEXTRA
) :
288 realloc(pktin
.data
, biglen
+APIEXTRA
));
290 fatalbox("Out of memory");
295 while (to_read
> 0) {
298 while ((*datalen
) == 0)
300 if (chunk
> (*datalen
))
302 memcpy(p
, *data
, chunk
);
310 cipher
->decrypt(pktin
.data
, biglen
);
312 pktin
.type
= pktin
.data
[pad
];
313 pktin
.body
= pktin
.data
+ pad
+ 1;
315 realcrc
= crc32(pktin
.data
, biglen
-4);
316 gotcrc
= GET_32BIT(pktin
.data
+biglen
-4);
317 if (gotcrc
!= realcrc
) {
318 fatalbox("Incorrect CRC received on packet");
321 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
||
322 pktin
.type
== SSH1_SMSG_STDERR_DATA
||
323 pktin
.type
== SSH1_MSG_DEBUG
||
324 pktin
.type
== SSH1_SMSG_AUTH_TIS_CHALLENGE
||
325 pktin
.type
== SSH1_SMSG_AUTH_CCARD_CHALLENGE
) {
326 long strlen
= GET_32BIT(pktin
.body
);
327 if (strlen
+ 4 != pktin
.length
)
328 fatalbox("Received data packet with bogus string length");
331 if (pktin
.type
== SSH1_MSG_DEBUG
) {
332 /* log debug message */
334 int strlen
= GET_32BIT(pktin
.body
);
335 strcpy(buf
, "Remote: ");
336 if (strlen
> 70) strlen
= 70;
337 memcpy(buf
+8, pktin
.body
+4, strlen
);
338 buf
[8+strlen
] = '\0';
341 } else if (pktin
.type
== SSH1_MSG_IGNORE
) {
349 static int ssh2_rdpkt(unsigned char **data
, int *datalen
)
351 static long len
, pad
, payload
, packetlen
, maclen
;
353 static int cipherblk
;
354 static unsigned long incoming_sequence
= 0;
364 cipherblk
= cipher
->blksize
;
370 if (pktin
.maxlen
< cipherblk
) {
371 pktin
.maxlen
= cipherblk
;
372 pktin
.data
= (pktin
.data
== NULL ?
malloc(cipherblk
+APIEXTRA
) :
373 realloc(pktin
.data
, cipherblk
+APIEXTRA
));
375 fatalbox("Out of memory");
379 * Acquire and decrypt the first block of the packet. This will
380 * contain the length and padding details.
382 for (i
= len
= 0; i
< cipherblk
; i
++) {
383 while ((*datalen
) == 0)
384 crReturn(cipherblk
-i
);
385 pktin
.data
[i
] = *(*data
)++;
389 if (!memcmp(pktin
.data
, "Remo", 4)) {/* "Remo"te server has closed ... */
394 sccipher
->decrypt(pktin
.data
, cipherblk
);
397 * Now get the length and padding figures.
399 len
= GET_32BIT(pktin
.data
);
403 * This enables us to deduce the payload length.
405 payload
= len
- pad
- 1;
407 pktin
.length
= payload
+ 5;
410 * So now we can work out the total packet length.
413 maclen
= scmac ? scmac
->len
: 0;
416 * Adjust memory allocation if packet is too big.
418 if (pktin
.maxlen
< packetlen
) {
419 pktin
.maxlen
= packetlen
;
420 pktin
.data
= (pktin
.data
== NULL ?
malloc(packetlen
+APIEXTRA
) :
421 realloc(pktin
.data
, packetlen
+APIEXTRA
));
423 fatalbox("Out of memory");
427 * Read and decrypt the remainder of the packet.
429 for (i
= cipherblk
; i
< packetlen
+ maclen
; i
++) {
430 while ((*datalen
) == 0)
431 crReturn(packetlen
+ maclen
- i
);
432 pktin
.data
[i
] = *(*data
)++;
435 /* Decrypt everything _except_ the MAC. */
437 sccipher
->decrypt(pktin
.data
+ cipherblk
, packetlen
- cipherblk
);
440 debug(("Got packet len=%d pad=%d\r\n", len
, pad
));
441 for (i
= 0; i
< packetlen
; i
++)
442 debug((" %02x", (unsigned char)pktin
.data
[i
]));
449 if (scmac
&& !scmac
->verify(pktin
.data
, len
+4, incoming_sequence
))
450 fatalbox("Incorrect MAC received on packet");
451 incoming_sequence
++; /* whether or not we MACed */
454 pktin
.type
= pktin
.data
[5];
456 if (pktin
.type
== SSH2_MSG_IGNORE
|| pktin
.type
== SSH2_MSG_DEBUG
)
457 goto next_packet
; /* FIXME: print DEBUG message */
462 static void ssh_gotdata(unsigned char *data
, int datalen
)
464 while (datalen
> 0) {
465 if ( s_rdpkt(&data
, &datalen
) == 0 ) {
466 ssh_protocol(NULL
, 0, 1);
467 if (ssh_state
== SSH_STATE_CLOSED
) {
475 static void s_wrpkt_start(int type
, int len
) {
478 len
+= 5; /* type and CRC */
482 pktout
.length
= len
-5;
483 if (pktout
.maxlen
< biglen
) {
484 pktout
.maxlen
= biglen
;
486 /* Allocate enough buffer space for extra block
487 * for MS CryptEncrypt() */
488 pktout
.data
= (pktout
.data
== NULL ?
malloc(biglen
+12) :
489 realloc(pktout
.data
, biglen
+12));
491 pktout
.data
= (pktout
.data
== NULL ?
malloc(biglen
+4) :
492 realloc(pktout
.data
, biglen
+4));
495 fatalbox("Out of memory");
499 pktout
.body
= pktout
.data
+4+pad
+1;
502 static void s_wrpkt(void) {
503 int pad
, len
, biglen
, i
;
506 len
= pktout
.length
+ 5; /* type and CRC */
510 pktout
.body
[-1] = pktout
.type
;
511 for (i
=0; i
<pad
; i
++)
512 pktout
.data
[i
+4] = random_byte();
513 crc
= crc32(pktout
.data
+4, biglen
-4);
514 PUT_32BIT(pktout
.data
+biglen
, crc
);
515 PUT_32BIT(pktout
.data
, len
);
518 cipher
->encrypt(pktout
.data
+4, biglen
);
520 s_write(pktout
.data
, biglen
+4);
524 * Construct a packet with the specified contents and
525 * send it to the server.
527 static void send_packet(int pkttype
, ...)
530 unsigned char *p
, *argp
, argchar
;
531 unsigned long argint
;
532 int pktlen
, argtype
, arglen
;
536 va_start(args
, pkttype
);
537 while ((argtype
= va_arg(args
, int)) != PKT_END
) {
540 (void) va_arg(args
, int);
544 (void) va_arg(args
, char);
548 (void) va_arg(args
, unsigned char *);
549 arglen
= va_arg(args
, int);
553 argp
= va_arg(args
, unsigned char *);
554 arglen
= strlen(argp
);
555 pktlen
+= 4 + arglen
;
558 bn
= va_arg(args
, Bignum
);
559 pktlen
+= ssh1_bignum_length(bn
);
567 s_wrpkt_start(pkttype
, pktlen
);
570 va_start(args
, pkttype
);
571 while ((argtype
= va_arg(args
, int)) != PKT_END
) {
574 argint
= va_arg(args
, int);
575 PUT_32BIT(p
, argint
);
579 argchar
= va_arg(args
, unsigned char);
584 argp
= va_arg(args
, unsigned char *);
585 arglen
= va_arg(args
, int);
586 memcpy(p
, argp
, arglen
);
590 argp
= va_arg(args
, unsigned char *);
591 arglen
= strlen(argp
);
592 PUT_32BIT(p
, arglen
);
593 memcpy(p
+ 4, argp
, arglen
);
597 bn
= va_arg(args
, Bignum
);
598 p
+= ssh1_write_bignum(p
, bn
);
609 * Connect to specified host and port.
610 * Returns an error message, or NULL on success.
611 * Also places the canonical host name into `realhost'.
613 static char *connect_to_host(char *host
, int port
, char **realhost
)
623 savedhost
= malloc(1+strlen(host
));
625 fatalbox("Out of memory");
626 strcpy(savedhost
, host
);
629 port
= 22; /* default ssh port */
641 if ( (a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
642 if ( (h
= gethostbyname(host
)) == NULL
)
643 switch (WSAGetLastError()) {
644 case WSAENETDOWN
: return "Network is down";
645 case WSAHOST_NOT_FOUND
: case WSANO_DATA
:
646 return "Host does not exist";
647 case WSATRY_AGAIN
: return "Host not found";
648 default: return "gethostbyname: unknown error";
650 memcpy (&a
, h
->h_addr
, sizeof(a
));
651 *realhost
= h
->h_name
;
662 s
= socket(AF_INET
, SOCK_STREAM
, 0);
663 if (s
== INVALID_SOCKET
)
664 switch (WSAGetLastError()) {
665 case WSAENETDOWN
: return "Network is down";
666 case WSAEAFNOSUPPORT
: return "TCP/IP support not present";
667 default: return "socket(): unknown error";
671 * Bind to local address.
673 addr
.sin_family
= AF_INET
;
674 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
675 addr
.sin_port
= htons(0);
676 if (bind (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
677 switch (WSAGetLastError()) {
678 case WSAENETDOWN
: return "Network is down";
679 default: return "bind(): unknown error";
683 * Connect to remote address.
685 addr
.sin_addr
.s_addr
= htonl(a
);
686 addr
.sin_port
= htons((short)port
);
687 if (connect (s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == SOCKET_ERROR
)
688 switch (WSAGetLastError()) {
689 case WSAENETDOWN
: return "Network is down";
690 case WSAECONNREFUSED
: return "Connection refused";
691 case WSAENETUNREACH
: return "Network is unreachable";
692 case WSAEHOSTUNREACH
: return "No route to host";
693 default: return "connect(): unknown error";
697 send(s
, "connect ", 8, 0);
698 send(s
, FWhost
, strlen(FWhost
), 0);
701 sprintf(buf
, " %d\n", FWport
);
702 send (s
, buf
, strlen(buf
), 0);
709 static int ssh_versioncmp(char *a
, char *b
) {
711 unsigned long av
, bv
;
713 av
= strtoul(a
, &ae
, 10);
714 bv
= strtoul(b
, &be
, 10);
715 if (av
!= bv
) return (av
< bv ?
-1 : +1);
716 if (*ae
== '.') ae
++;
717 if (*be
== '.') be
++;
718 av
= strtoul(ae
, &ae
, 10);
719 bv
= strtoul(be
, &be
, 10);
720 if (av
!= bv
) return (av
< bv ?
-1 : +1);
726 * Utility routine for putting an SSH-protocol `string' into a SHA
730 void sha_string(SHA_State
*s
, void *str
, int len
) {
731 unsigned char lenblk
[4];
732 PUT_32BIT(lenblk
, len
);
733 SHA_Bytes(s
, lenblk
, 4);
734 SHA_Bytes(s
, str
, len
);
738 * SSH2 packet construction functions.
740 void ssh2_pkt_adddata(void *data
, int len
) {
741 pktout
.length
+= len
;
742 if (pktout
.maxlen
< pktout
.length
) {
743 pktout
.maxlen
= pktout
.length
+ 256;
744 pktout
.data
= (pktout
.data
== NULL ?
malloc(pktout
.maxlen
+APIEXTRA
) :
745 realloc(pktout
.data
, pktout
.maxlen
+APIEXTRA
));
747 fatalbox("Out of memory");
749 memcpy(pktout
.data
+pktout
.length
-len
, data
, len
);
751 void ssh2_pkt_addbyte(unsigned char byte
) {
752 ssh2_pkt_adddata(&byte
, 1);
754 void ssh2_pkt_init(int pkt_type
) {
756 ssh2_pkt_addbyte((unsigned char)pkt_type
);
758 void ssh2_pkt_addbool(unsigned char value
) {
759 ssh2_pkt_adddata(&value
, 1);
761 void ssh2_pkt_adduint32(unsigned long value
) {
764 ssh2_pkt_adddata(x
, 4);
766 void ssh2_pkt_addstring_start(void) {
767 ssh2_pkt_adduint32(0);
768 pktout
.savedpos
= pktout
.length
;
770 void ssh2_pkt_addstring_str(char *data
) {
771 ssh2_pkt_adddata(data
, strlen(data
));
772 PUT_32BIT(pktout
.data
+ pktout
.savedpos
- 4,
773 pktout
.length
- pktout
.savedpos
);
775 void ssh2_pkt_addstring_data(char *data
, int len
) {
776 ssh2_pkt_adddata(data
, len
);
777 PUT_32BIT(pktout
.data
+ pktout
.savedpos
- 4,
778 pktout
.length
- pktout
.savedpos
);
780 void ssh2_pkt_addstring(char *data
) {
781 ssh2_pkt_addstring_start();
782 ssh2_pkt_addstring_str(data
);
784 char *ssh2_mpint_fmt(Bignum b
, int *len
) {
787 p
= malloc(n
* 2 + 1);
789 fatalbox("out of memory");
791 for (i
= 0; i
< n
; i
++) {
792 p
[i
*2+1] = (b
[n
-i
] >> 8) & 0xFF;
793 p
[i
*2+2] = (b
[n
-i
] ) & 0xFF;
796 while (p
[i
] == 0 && (p
[i
+1] & 0x80) == 0)
798 memmove(p
, p
+i
, n
*2+1-i
);
802 void ssh2_pkt_addmp(Bignum b
) {
805 p
= ssh2_mpint_fmt(b
, &len
);
806 ssh2_pkt_addstring_start();
807 ssh2_pkt_addstring_data(p
, len
);
810 void ssh2_pkt_send(void) {
811 int cipherblk
, maclen
, padding
, i
;
812 static unsigned long outgoing_sequence
= 0;
815 * Add padding. At least four bytes, and must also bring total
816 * length (minus MAC) up to a multiple of the block size.
818 cipherblk
= cipher ? cipher
->blksize
: 8; /* block size */
819 cipherblk
= cipherblk
< 8 ?
8 : cipherblk
; /* or 8 if blksize < 8 */
821 padding
+= (cipherblk
- (pktout
.length
+ padding
) % cipherblk
) % cipherblk
;
822 pktout
.data
[4] = padding
;
823 for (i
= 0; i
< padding
; i
++)
824 pktout
.data
[pktout
.length
+ i
] = random_byte();
825 PUT_32BIT(pktout
.data
, pktout
.length
+ padding
- 4);
827 csmac
->generate(pktout
.data
, pktout
.length
+ padding
,
829 outgoing_sequence
++; /* whether or not we MACed */
832 debug(("Sending packet len=%d\r\n", pktout
.length
+padding
));
833 for (i
= 0; i
< pktout
.length
+padding
; i
++)
834 debug((" %02x", (unsigned char)pktout
.data
[i
]));
839 cscipher
->encrypt(pktout
.data
, pktout
.length
+ padding
);
840 maclen
= csmac ? csmac
->len
: 0;
842 s_write(pktout
.data
, pktout
.length
+ padding
+ maclen
);
846 void bndebug(char *string
, Bignum b
) {
849 p
= ssh2_mpint_fmt(b
, &len
);
850 debug(("%s", string
));
851 for (i
= 0; i
< len
; i
++)
852 debug((" %02x", p
[i
]));
858 void sha_mpint(SHA_State
*s
, Bignum b
) {
861 p
= ssh2_mpint_fmt(b
, &len
);
862 sha_string(s
, p
, len
);
867 * SSH2 packet decode functions.
869 unsigned long ssh2_pkt_getuint32(void) {
871 if (pktin
.length
- pktin
.savedpos
< 4)
872 return 0; /* arrgh, no way to decline (FIXME?) */
873 value
= GET_32BIT(pktin
.data
+pktin
.savedpos
);
877 void ssh2_pkt_getstring(char **p
, int *length
) {
879 if (pktin
.length
- pktin
.savedpos
< 4)
881 *length
= GET_32BIT(pktin
.data
+pktin
.savedpos
);
883 if (pktin
.length
- pktin
.savedpos
< *length
)
885 *p
= pktin
.data
+pktin
.savedpos
;
886 pktin
.savedpos
+= *length
;
888 Bignum
ssh2_pkt_getmp(void) {
893 ssh2_pkt_getstring(&p
, &length
);
897 fatalbox("internal error: Can't handle negative mpints");
898 b
= newbn((length
+1)/2);
899 for (i
= 0; i
< length
; i
++) {
902 b
[j
/2+1] |= ((unsigned char)p
[i
]) << 8;
904 b
[j
/2+1] |= ((unsigned char)p
[i
]);
909 static int do_ssh_init(void) {
913 char vlog
[sizeof(vstring
)+20];
918 while (s_read(&c
, 1) == 1) {
919 if (c
== 'S' && i
< 2) i
++;
920 else if (c
== 'S' && i
== 2) i
= 2;
921 else if (c
== 'H' && i
== 2) break;
925 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
926 if (s_read(&c
,1) != 1 || c
!= 'S') return 0;
927 if (s_read(&c
,1) != 1 || c
!= 'H') return 0;
929 strcpy(vstring
, "SSH-");
931 if (s_read(&c
,1) != 1 || c
!= '-') return 0;
934 if (s_read(&c
,1) != 1)
936 if (vsp
< vstring
+sizeof(vstring
)-1)
942 } else if (i
< sizeof(version
)-1)
950 sprintf(vlog
, "Server version: %s", vstring
);
951 vlog
[strcspn(vlog
, "\r\n")] = '\0';
955 * Server version "1.99" means we can choose whether we use v1
956 * or v2 protocol. Choice is based on cfg.sshprot.
958 if (ssh_versioncmp(version
, cfg
.sshprot
== 1 ?
"2.0" : "1.99") >= 0) {
960 * This is a v2 server. Begin v2 protocol.
962 char *verstring
= "SSH-2.0-PuTTY";
965 * Hash our version string and their version string.
967 sha_string(&exhash
, verstring
, strlen(verstring
));
968 sha_string(&exhash
, vstring
, strcspn(vstring
, "\r\n"));
969 sprintf(vstring
, "%s\n", verstring
);
970 sprintf(vlog
, "We claim version: %s", verstring
);
972 logevent("Using SSH protocol version 2");
973 s_write(vstring
, strlen(vstring
));
974 ssh_protocol
= ssh2_protocol
;
976 s_rdpkt
= ssh2_rdpkt
;
979 * This is a v1 server. Begin v1 protocol.
981 sprintf(vstring
, "SSH-%s-PuTTY\n",
982 (ssh_versioncmp(version
, "1.5") <= 0 ? version
: "1.5"));
983 sprintf(vlog
, "We claim version: %s", vstring
);
984 vlog
[strcspn(vlog
, "\r\n")] = '\0';
986 logevent("Using SSH protocol version 1");
987 s_write(vstring
, strlen(vstring
));
988 ssh_protocol
= ssh1_protocol
;
990 s_rdpkt
= ssh1_rdpkt
;
997 * Handle the key exchange and user authentication phases.
999 static int do_ssh1_login(unsigned char *in
, int inlen
, int ispkt
)
1002 unsigned char *rsabuf
, *keystr1
, *keystr2
;
1003 unsigned char cookie
[8];
1004 struct RSAKey servkey
, hostkey
;
1005 struct MD5Context md5c
;
1006 static unsigned long supported_ciphers_mask
, supported_auths_mask
;
1007 static int tried_publickey
;
1008 static unsigned char session_id
[16];
1013 if (!ispkt
) crWaitUntil(ispkt
);
1015 if (pktin
.type
!= SSH1_SMSG_PUBLIC_KEY
)
1016 fatalbox("Public key packet not received");
1018 logevent("Received public keys");
1020 memcpy(cookie
, pktin
.body
, 8);
1022 i
= makekey(pktin
.body
+8, &servkey
, &keystr1
, 0);
1023 j
= makekey(pktin
.body
+8+i
, &hostkey
, &keystr2
, 0);
1026 * Hash the host key and print the hash in the log box. Just as
1027 * a last resort in case the registry's host key checking is
1028 * compromised, we'll allow the user some ability to verify
1032 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
1033 MD5Final(session_id
, &md5c
);
1037 logevent("Host key MD5 is:");
1038 strcpy(logmsg
, " ");
1039 for (i
= 0; i
< 16; i
++)
1040 sprintf(logmsg
+strlen(logmsg
), "%02x", session_id
[i
]);
1044 supported_ciphers_mask
= GET_32BIT(pktin
.body
+12+i
+j
);
1045 supported_auths_mask
= GET_32BIT(pktin
.body
+16+i
+j
);
1048 MD5Update(&md5c
, keystr2
, hostkey
.bytes
);
1049 MD5Update(&md5c
, keystr1
, servkey
.bytes
);
1050 MD5Update(&md5c
, pktin
.body
, 8);
1051 MD5Final(session_id
, &md5c
);
1053 for (i
=0; i
<32; i
++)
1054 session_key
[i
] = random_byte();
1056 len
= (hostkey
.bytes
> servkey
.bytes ? hostkey
.bytes
: servkey
.bytes
);
1058 rsabuf
= malloc(len
);
1060 fatalbox("Out of memory");
1063 * Verify the host key.
1067 * First format the key into a string.
1069 int len
= rsastr_len(&hostkey
);
1070 char *keystr
= malloc(len
);
1072 fatalbox("Out of memory");
1073 rsastr_fmt(keystr
, &hostkey
);
1074 verify_ssh_host_key(savedhost
, keystr
);
1078 for (i
=0; i
<32; i
++) {
1079 rsabuf
[i
] = session_key
[i
];
1081 rsabuf
[i
] ^= session_id
[i
];
1084 if (hostkey
.bytes
> servkey
.bytes
) {
1085 rsaencrypt(rsabuf
, 32, &servkey
);
1086 rsaencrypt(rsabuf
, servkey
.bytes
, &hostkey
);
1088 rsaencrypt(rsabuf
, 32, &hostkey
);
1089 rsaencrypt(rsabuf
, hostkey
.bytes
, &servkey
);
1092 logevent("Encrypted session key");
1094 cipher_type
= cfg
.cipher
== CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH
:
1095 cfg
.cipher
== CIPHER_DES ? SSH_CIPHER_DES
:
1097 if ((supported_ciphers_mask
& (1 << cipher_type
)) == 0) {
1098 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
1099 cipher_type
= SSH_CIPHER_3DES
;
1101 switch (cipher_type
) {
1102 case SSH_CIPHER_3DES
: logevent("Using 3DES encryption"); break;
1103 case SSH_CIPHER_DES
: logevent("Using single-DES encryption"); break;
1104 case SSH_CIPHER_BLOWFISH
: logevent("Using Blowfish encryption"); break;
1107 send_packet(SSH1_CMSG_SESSION_KEY
,
1108 PKT_CHAR
, cipher_type
,
1109 PKT_DATA
, cookie
, 8,
1110 PKT_CHAR
, (len
*8) >> 8, PKT_CHAR
, (len
*8) & 0xFF,
1111 PKT_DATA
, rsabuf
, len
,
1115 logevent("Trying to enable encryption...");
1119 cipher
= cipher_type
== SSH_CIPHER_BLOWFISH ?
&ssh_blowfish
:
1120 cipher_type
== SSH_CIPHER_DES ?
&ssh_des
:
1122 cipher
->sesskey(session_key
);
1126 if (pktin
.type
!= SSH1_SMSG_SUCCESS
)
1127 fatalbox("Encryption not successfully enabled");
1129 logevent("Successfully started encryption");
1133 static char username
[100];
1136 if ((flags
& FLAG_CONNECTION
) && !*cfg
.username
) {
1137 c_write("login as: ", 10);
1139 crWaitUntil(!ispkt
);
1140 while (inlen
--) switch (c
= *in
++) {
1147 c_write("\b \b", 3);
1153 c_write("\b \b", 3);
1162 if (((c
>= ' ' && c
<= '~') ||
1163 ((unsigned char)c
>= 160)) && pos
< 40) {
1164 username
[pos
++] = c
;
1171 username
[strcspn(username
, "\n\r")] = '\0';
1174 strncpy(username
, cfg
.username
, 99);
1175 username
[99] = '\0';
1176 if (flags
& FLAG_VERBOSE
) {
1177 sprintf(stuff
, "Sent username \"%s\".\r\n", username
);
1178 c_write(stuff
, strlen(stuff
));
1182 send_packet(SSH1_CMSG_USER
, PKT_STR
, username
, PKT_END
);
1184 char userlog
[20+sizeof(username
)];
1185 sprintf(userlog
, "Sent username \"%s\"", username
);
1192 tried_publickey
= 0;
1194 while (pktin
.type
== SSH1_SMSG_FAILURE
) {
1195 static char password
[100];
1198 static int pwpkt_type
;
1200 * Show password prompt, having first obtained it via a TIS
1201 * or CryptoCard exchange if we're doing TIS or CryptoCard
1204 pwpkt_type
= SSH1_CMSG_AUTH_PASSWORD
;
1205 if (agent_exists()) {
1207 * Attempt RSA authentication using Pageant.
1209 static unsigned char request
[5], *response
, *p
;
1210 static int responselen
;
1211 static int i
, nkeys
;
1212 static int authed
= FALSE
;
1215 logevent("Pageant is running. Requesting keys.");
1217 /* Request the keys held by the agent. */
1218 PUT_32BIT(request
, 1);
1219 request
[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES
;
1220 agent_query(request
, 5, &r
, &responselen
);
1221 response
= (unsigned char *)r
;
1224 nkeys
= GET_32BIT(p
); p
+= 4;
1225 { char buf
[64]; sprintf(buf
, "Pageant has %d keys", nkeys
);
1227 for (i
= 0; i
< nkeys
; i
++) {
1228 static struct RSAKey key
;
1229 static Bignum challenge
;
1231 { char buf
[64]; sprintf(buf
, "Trying Pageant key #%d", i
);
1234 p
+= ssh1_read_bignum(p
, &key
.exponent
);
1235 p
+= ssh1_read_bignum(p
, &key
.modulus
);
1236 send_packet(SSH1_CMSG_AUTH_RSA
,
1237 PKT_BIGNUM
, key
.modulus
, PKT_END
);
1239 if (pktin
.type
!= SSH1_SMSG_AUTH_RSA_CHALLENGE
) {
1240 logevent("Key refused");
1243 logevent("Received RSA challenge");
1244 ssh1_read_bignum(pktin
.body
, &challenge
);
1246 char *agentreq
, *q
, *ret
;
1248 len
= 1 + 4; /* message type, bit count */
1249 len
+= ssh1_bignum_length(key
.exponent
);
1250 len
+= ssh1_bignum_length(key
.modulus
);
1251 len
+= ssh1_bignum_length(challenge
);
1252 len
+= 16; /* session id */
1253 len
+= 4; /* response format */
1254 agentreq
= malloc(4 + len
);
1255 PUT_32BIT(agentreq
, len
);
1257 *q
++ = SSH_AGENTC_RSA_CHALLENGE
;
1258 PUT_32BIT(q
, ssh1_bignum_bitcount(key
.modulus
));
1260 q
+= ssh1_write_bignum(q
, key
.exponent
);
1261 q
+= ssh1_write_bignum(q
, key
.modulus
);
1262 q
+= ssh1_write_bignum(q
, challenge
);
1263 memcpy(q
, session_id
, 16); q
+= 16;
1264 PUT_32BIT(q
, 1); /* response format */
1265 agent_query(agentreq
, len
+4, &ret
, &retlen
);
1268 if (ret
[4] == SSH_AGENT_RSA_RESPONSE
) {
1269 logevent("Sending Pageant's response");
1270 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE
,
1271 PKT_DATA
, ret
+5, 16, PKT_END
);
1274 if (pktin
.type
== SSH1_SMSG_SUCCESS
) {
1275 logevent("Pageant's response accepted");
1278 logevent("Pageant's response not accepted");
1280 logevent("Pageant failed to answer challenge");
1284 logevent("No reply received from Pageant");
1287 freebn(key
.exponent
);
1288 freebn(key
.modulus
);
1297 if (*cfg
.keyfile
&& !tried_publickey
)
1298 pwpkt_type
= SSH1_CMSG_AUTH_RSA
;
1300 if (pwpkt_type
== SSH1_CMSG_AUTH_PASSWORD
&& !FLAG_WINDOWED
) {
1302 sprintf(prompt
, "%s@%s's password: ", cfg
.username
, savedhost
);
1303 if (!ssh_get_password(prompt
, password
, sizeof(password
))) {
1305 * get_password failed to get a password (for
1306 * example because one was supplied on the command
1307 * line which has already failed to work).
1310 logevent("No more passwords to try");
1311 ssh_state
= SSH_STATE_CLOSED
;
1316 if (pktin
.type
== SSH1_SMSG_FAILURE
&&
1318 (supported_auths_mask
& (1<<SSH1_AUTH_TIS
))) {
1319 pwpkt_type
= SSH1_CMSG_AUTH_TIS_RESPONSE
;
1320 logevent("Requested TIS authentication");
1321 send_packet(SSH1_CMSG_AUTH_TIS
, PKT_END
);
1323 if (pktin
.type
!= SSH1_SMSG_AUTH_TIS_CHALLENGE
) {
1324 logevent("TIS authentication declined");
1325 c_write("TIS authentication refused.\r\n", 29);
1327 int challengelen
= ((pktin
.body
[0] << 24) |
1328 (pktin
.body
[1] << 16) |
1329 (pktin
.body
[2] << 8) |
1331 logevent("Received TIS challenge");
1332 c_write(pktin
.body
+4, challengelen
);
1335 if (pktin
.type
== SSH1_SMSG_FAILURE
&&
1337 (supported_auths_mask
& (1<<SSH1_AUTH_CCARD
))) {
1338 pwpkt_type
= SSH1_CMSG_AUTH_CCARD_RESPONSE
;
1339 logevent("Requested CryptoCard authentication");
1340 send_packet(SSH1_CMSG_AUTH_CCARD
, PKT_END
);
1342 if (pktin
.type
!= SSH1_SMSG_AUTH_CCARD_CHALLENGE
) {
1343 logevent("CryptoCard authentication declined");
1344 c_write("CryptoCard authentication refused.\r\n", 29);
1346 int challengelen
= ((pktin
.body
[0] << 24) |
1347 (pktin
.body
[1] << 16) |
1348 (pktin
.body
[2] << 8) |
1350 logevent("Received CryptoCard challenge");
1351 c_write(pktin
.body
+4, challengelen
);
1352 c_write("\r\nResponse : ", 13);
1355 if (pwpkt_type
== SSH1_CMSG_AUTH_PASSWORD
)
1356 c_write("password: ", 10);
1357 if (pwpkt_type
== SSH1_CMSG_AUTH_RSA
) {
1358 if (flags
& FLAG_VERBOSE
)
1359 c_write("Trying public key authentication.\r\n", 35);
1360 if (!rsakey_encrypted(cfg
.keyfile
)) {
1361 if (flags
& FLAG_VERBOSE
)
1362 c_write("No passphrase required.\r\n", 25);
1365 c_write("passphrase: ", 12);
1370 crWaitUntil(!ispkt
);
1371 while (inlen
--) switch (c
= *in
++) {
1388 if (((c
>= ' ' && c
<= '~') ||
1389 ((unsigned char)c
>= 160)) && pos
< sizeof(password
))
1390 password
[pos
++] = c
;
1399 if (pwpkt_type
== SSH1_CMSG_AUTH_RSA
) {
1401 * Try public key authentication with the specified
1404 static struct RSAKey pubkey
;
1405 static Bignum challenge
, response
;
1407 static unsigned char buffer
[32];
1409 tried_publickey
= 1;
1410 i
= loadrsakey(cfg
.keyfile
, &pubkey
, password
);
1412 c_write("Couldn't load public key from ", 30);
1413 c_write(cfg
.keyfile
, strlen(cfg
.keyfile
));
1414 c_write(".\r\n", 3);
1415 continue; /* go and try password */
1418 c_write("Wrong passphrase.\r\n", 19);
1419 tried_publickey
= 0;
1420 continue; /* try again */
1424 * Send a public key attempt.
1426 send_packet(SSH1_CMSG_AUTH_RSA
,
1427 PKT_BIGNUM
, pubkey
.modulus
, PKT_END
);
1430 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1431 if (flags
& FLAG_VERBOSE
)
1432 c_write("Server refused our public key.\r\n", 32);
1433 continue; /* go and try password */
1435 if (pktin
.type
!= SSH1_SMSG_AUTH_RSA_CHALLENGE
)
1436 fatalbox("Bizarre response to offer of public key");
1437 ssh1_read_bignum(pktin
.body
, &challenge
);
1438 response
= rsadecrypt(challenge
, &pubkey
);
1439 freebn(pubkey
.private_exponent
); /* burn the evidence */
1441 for (i
= 0; i
< 32; i
+= 2) {
1442 buffer
[i
] = response
[16-i
/2] >> 8;
1443 buffer
[i
+1] = response
[16-i
/2] & 0xFF;
1447 MD5Update(&md5c
, buffer
, 32);
1448 MD5Update(&md5c
, session_id
, 16);
1449 MD5Final(buffer
, &md5c
);
1451 send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE
,
1452 PKT_DATA
, buffer
, 16, PKT_END
);
1455 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1456 if (flags
& FLAG_VERBOSE
)
1457 c_write("Failed to authenticate with our public key.\r\n",
1459 continue; /* go and try password */
1460 } else if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1461 fatalbox("Bizarre response to RSA authentication response");
1464 break; /* we're through! */
1466 send_packet(pwpkt_type
, PKT_STR
, password
, PKT_END
);
1468 logevent("Sent password");
1469 memset(password
, 0, strlen(password
));
1471 if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1472 if (flags
& FLAG_VERBOSE
)
1473 c_write("Access denied\r\n", 15);
1474 logevent("Authentication refused");
1475 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1476 logevent("Received disconnect request");
1477 ssh_state
= SSH_STATE_CLOSED
;
1479 } else if (pktin
.type
!= SSH1_SMSG_SUCCESS
) {
1480 fatalbox("Strange packet received, type %d", pktin
.type
);
1484 logevent("Authentication successful");
1489 static void ssh1_protocol(unsigned char *in
, int inlen
, int ispkt
) {
1494 while (!do_ssh1_login(in
, inlen
, ispkt
)) {
1497 if (ssh_state
== SSH_STATE_CLOSED
)
1501 send_packet(SSH1_CMSG_REQUEST_PTY
,
1502 PKT_STR
, cfg
.termtype
,
1503 PKT_INT
, rows
, PKT_INT
, cols
,
1504 PKT_INT
, 0, PKT_INT
, 0,
1507 ssh_state
= SSH_STATE_INTERMED
;
1508 do { crReturnV
; } while (!ispkt
);
1509 if (pktin
.type
!= SSH1_SMSG_SUCCESS
&& pktin
.type
!= SSH1_SMSG_FAILURE
) {
1510 fatalbox("Protocol confusion");
1511 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1512 c_write("Server refused to allocate pty\r\n", 32);
1514 logevent("Allocated pty");
1517 if (*cfg
.remote_cmd
)
1518 send_packet(SSH1_CMSG_EXEC_CMD
, PKT_STR
, cfg
.remote_cmd
, PKT_END
);
1520 send_packet(SSH1_CMSG_EXEC_SHELL
, PKT_END
);
1521 logevent("Started session");
1523 ssh_state
= SSH_STATE_SESSION
;
1531 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
||
1532 pktin
.type
== SSH1_SMSG_STDERR_DATA
) {
1533 long len
= GET_32BIT(pktin
.body
);
1534 c_write(pktin
.body
+4, len
);
1535 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
1536 ssh_state
= SSH_STATE_CLOSED
;
1537 logevent("Received disconnect request");
1538 } else if (pktin
.type
== SSH1_SMSG_SUCCESS
) {
1539 /* may be from EXEC_SHELL on some servers */
1540 } else if (pktin
.type
== SSH1_SMSG_FAILURE
) {
1541 /* may be from EXEC_SHELL on some servers
1542 * if no pty is available or in other odd cases. Ignore */
1543 } else if (pktin
.type
== SSH1_SMSG_EXIT_STATUS
) {
1544 send_packet(SSH1_CMSG_EXIT_CONFIRMATION
, PKT_END
);
1546 fatalbox("Strange packet received: type %d", pktin
.type
);
1549 send_packet(SSH1_CMSG_STDIN_DATA
,
1550 PKT_INT
, inlen
, PKT_DATA
, in
, inlen
, PKT_END
);
1558 * Utility routine for decoding comma-separated strings in KEXINIT.
1560 int in_commasep_string(char *needle
, char *haystack
, int haylen
) {
1561 int needlen
= strlen(needle
);
1564 * Is it at the start of the string?
1566 if (haylen
>= needlen
&& /* haystack is long enough */
1567 !memcmp(needle
, haystack
, needlen
) && /* initial match */
1568 (haylen
== needlen
|| haystack
[needlen
] == ',')
1569 /* either , or EOS follows */
1573 * If not, search for the next comma and resume after that.
1574 * If no comma found, terminate.
1576 while (haylen
> 0 && *haystack
!= ',')
1577 haylen
--, haystack
++;
1580 haylen
--, haystack
++; /* skip over comma itself */
1585 * SSH2 key creation method.
1587 void ssh2_mkkey(Bignum K
, char *H
, char chr
, char *keyspace
) {
1589 /* First 20 bytes. */
1592 SHA_Bytes(&s
, H
, 20);
1593 SHA_Bytes(&s
, &chr
, 1);
1594 SHA_Bytes(&s
, H
, 20);
1595 SHA_Final(&s
, keyspace
);
1596 /* Next 20 bytes. */
1599 SHA_Bytes(&s
, H
, 20);
1600 SHA_Bytes(&s
, keyspace
, 20);
1601 SHA_Final(&s
, keyspace
+20);
1605 * Handle the SSH2 transport layer.
1607 static int do_ssh2_transport(unsigned char *in
, int inlen
, int ispkt
)
1611 static Bignum e
, f
, K
;
1612 static struct ssh_cipher
*cscipher_tobe
= NULL
;
1613 static struct ssh_cipher
*sccipher_tobe
= NULL
;
1614 static struct ssh_mac
*csmac_tobe
= NULL
;
1615 static struct ssh_mac
*scmac_tobe
= NULL
;
1616 static struct ssh_compress
*cscomp_tobe
= NULL
;
1617 static struct ssh_compress
*sccomp_tobe
= NULL
;
1618 static char *hostkeydata
, *sigdata
, *keystr
;
1619 static int hostkeylen
, siglen
;
1620 static unsigned char exchange_hash
[20];
1621 static unsigned char keyspace
[40];
1628 * Construct and send our key exchange packet.
1630 ssh2_pkt_init(SSH2_MSG_KEXINIT
);
1631 for (i
= 0; i
< 16; i
++)
1632 ssh2_pkt_addbyte((unsigned char)random_byte());
1633 /* List key exchange algorithms. */
1634 ssh2_pkt_addstring_start();
1635 for (i
= 0; i
< lenof(kex_algs
); i
++) {
1636 ssh2_pkt_addstring_str(kex_algs
[i
]->name
);
1637 if (i
< lenof(kex_algs
)-1)
1638 ssh2_pkt_addstring_str(",");
1640 /* List server host key algorithms. */
1641 ssh2_pkt_addstring_start();
1642 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
1643 ssh2_pkt_addstring_str(hostkey_algs
[i
]->name
);
1644 if (i
< lenof(hostkey_algs
)-1)
1645 ssh2_pkt_addstring_str(",");
1647 /* List client->server encryption algorithms. */
1648 ssh2_pkt_addstring_start();
1649 for (i
= 0; i
< lenof(ciphers
); i
++) {
1650 ssh2_pkt_addstring_str(ciphers
[i
]->name
);
1651 if (i
< lenof(ciphers
)-1)
1652 ssh2_pkt_addstring_str(",");
1654 /* List server->client encryption algorithms. */
1655 ssh2_pkt_addstring_start();
1656 for (i
= 0; i
< lenof(ciphers
); i
++) {
1657 ssh2_pkt_addstring_str(ciphers
[i
]->name
);
1658 if (i
< lenof(ciphers
)-1)
1659 ssh2_pkt_addstring_str(",");
1661 /* List client->server MAC algorithms. */
1662 ssh2_pkt_addstring_start();
1663 for (i
= 0; i
< lenof(macs
); i
++) {
1664 ssh2_pkt_addstring_str(macs
[i
]->name
);
1665 if (i
< lenof(macs
)-1)
1666 ssh2_pkt_addstring_str(",");
1668 /* List server->client MAC algorithms. */
1669 ssh2_pkt_addstring_start();
1670 for (i
= 0; i
< lenof(macs
); i
++) {
1671 ssh2_pkt_addstring_str(macs
[i
]->name
);
1672 if (i
< lenof(macs
)-1)
1673 ssh2_pkt_addstring_str(",");
1675 /* List client->server compression algorithms. */
1676 ssh2_pkt_addstring_start();
1677 for (i
= 0; i
< lenof(compressions
); i
++) {
1678 ssh2_pkt_addstring_str(compressions
[i
]->name
);
1679 if (i
< lenof(compressions
)-1)
1680 ssh2_pkt_addstring_str(",");
1682 /* List server->client compression algorithms. */
1683 ssh2_pkt_addstring_start();
1684 for (i
= 0; i
< lenof(compressions
); i
++) {
1685 ssh2_pkt_addstring_str(compressions
[i
]->name
);
1686 if (i
< lenof(compressions
)-1)
1687 ssh2_pkt_addstring_str(",");
1689 /* List client->server languages. Empty list. */
1690 ssh2_pkt_addstring_start();
1691 /* List server->client languages. Empty list. */
1692 ssh2_pkt_addstring_start();
1693 /* First KEX packet does _not_ follow, because we're not that brave. */
1694 ssh2_pkt_addbool(FALSE
);
1696 ssh2_pkt_adduint32(0);
1697 sha_string(&exhash
, pktout
.data
+5, pktout
.length
-5);
1700 if (!ispkt
) crWaitUntil(ispkt
);
1701 sha_string(&exhash
, pktin
.data
+5, pktin
.length
-5);
1704 * Now examine the other side's KEXINIT to see what we're up
1707 if (pktin
.type
!= SSH2_MSG_KEXINIT
) {
1708 fatalbox("expected key exchange packet from server");
1710 kex
= NULL
; hostkey
= NULL
; cscipher_tobe
= NULL
; sccipher_tobe
= NULL
;
1711 csmac_tobe
= NULL
; scmac_tobe
= NULL
; cscomp_tobe
= NULL
; sccomp_tobe
= NULL
;
1712 pktin
.savedpos
+= 16; /* skip garbage cookie */
1713 ssh2_pkt_getstring(&str
, &len
); /* key exchange algorithms */
1714 for (i
= 0; i
< lenof(kex_algs
); i
++) {
1715 if (in_commasep_string(kex_algs
[i
]->name
, str
, len
)) {
1720 ssh2_pkt_getstring(&str
, &len
); /* host key algorithms */
1721 for (i
= 0; i
< lenof(hostkey_algs
); i
++) {
1722 if (in_commasep_string(hostkey_algs
[i
]->name
, str
, len
)) {
1723 hostkey
= hostkey_algs
[i
];
1727 ssh2_pkt_getstring(&str
, &len
); /* client->server cipher */
1728 for (i
= 0; i
< lenof(ciphers
); i
++) {
1729 if (in_commasep_string(ciphers
[i
]->name
, str
, len
)) {
1730 cscipher_tobe
= ciphers
[i
];
1734 ssh2_pkt_getstring(&str
, &len
); /* server->client cipher */
1735 for (i
= 0; i
< lenof(ciphers
); i
++) {
1736 if (in_commasep_string(ciphers
[i
]->name
, str
, len
)) {
1737 sccipher_tobe
= ciphers
[i
];
1741 ssh2_pkt_getstring(&str
, &len
); /* client->server mac */
1742 for (i
= 0; i
< lenof(macs
); i
++) {
1743 if (in_commasep_string(macs
[i
]->name
, str
, len
)) {
1744 csmac_tobe
= macs
[i
];
1748 ssh2_pkt_getstring(&str
, &len
); /* server->client mac */
1749 for (i
= 0; i
< lenof(macs
); i
++) {
1750 if (in_commasep_string(macs
[i
]->name
, str
, len
)) {
1751 scmac_tobe
= macs
[i
];
1755 ssh2_pkt_getstring(&str
, &len
); /* client->server compression */
1756 for (i
= 0; i
< lenof(compressions
); i
++) {
1757 if (in_commasep_string(compressions
[i
]->name
, str
, len
)) {
1758 cscomp_tobe
= compressions
[i
];
1762 ssh2_pkt_getstring(&str
, &len
); /* server->client compression */
1763 for (i
= 0; i
< lenof(compressions
); i
++) {
1764 if (in_commasep_string(compressions
[i
]->name
, str
, len
)) {
1765 sccomp_tobe
= compressions
[i
];
1771 * Currently we only support Diffie-Hellman and DSS, so let's
1772 * bomb out if those aren't selected.
1774 if (kex
!= &ssh_diffiehellman
|| hostkey
!= &ssh_dss
)
1775 fatalbox("internal fault: chaos in SSH 2 transport layer");
1778 * Now we begin the fun. Generate and send e for Diffie-Hellman.
1781 ssh2_pkt_init(SSH2_MSG_KEXDH_INIT
);
1786 if (pktin
.type
!= SSH2_MSG_KEXDH_REPLY
) {
1787 fatalbox("expected key exchange packet from server");
1789 ssh2_pkt_getstring(&hostkeydata
, &hostkeylen
);
1790 f
= ssh2_pkt_getmp();
1791 ssh2_pkt_getstring(&sigdata
, &siglen
);
1795 sha_string(&exhash
, hostkeydata
, hostkeylen
);
1796 sha_mpint(&exhash
, e
);
1797 sha_mpint(&exhash
, f
);
1798 sha_mpint(&exhash
, K
);
1799 SHA_Final(&exhash
, exchange_hash
);
1802 debug(("Exchange hash is:\r\n"));
1803 for (i
= 0; i
< 20; i
++)
1804 debug((" %02x", exchange_hash
[i
]));
1808 hostkey
->setkey(hostkeydata
, hostkeylen
);
1809 if (!hostkey
->verifysig(sigdata
, siglen
, exchange_hash
, 20))
1810 fatalbox("Server failed host key check");
1813 * Expect SSH2_MSG_NEWKEYS from server.
1816 if (pktin
.type
!= SSH2_MSG_NEWKEYS
)
1817 fatalbox("expected new-keys packet from server");
1820 * Authenticate remote host: verify host key. (We've already
1821 * checked the signature of the exchange hash.)
1823 keystr
= hostkey
->fmtkey();
1824 verify_ssh_host_key(savedhost
, keystr
);
1828 * Send SSH2_MSG_NEWKEYS.
1830 ssh2_pkt_init(SSH2_MSG_NEWKEYS
);
1834 * Create and initialise session keys.
1836 cscipher
= cscipher_tobe
;
1837 sccipher
= sccipher_tobe
;
1840 cscomp
= cscomp_tobe
;
1841 sccomp
= sccomp_tobe
;
1843 * Set IVs after keys.
1845 ssh2_mkkey(K
, exchange_hash
, 'C', keyspace
); cscipher
->setcskey(keyspace
);
1846 ssh2_mkkey(K
, exchange_hash
, 'D', keyspace
); cscipher
->setsckey(keyspace
);
1847 ssh2_mkkey(K
, exchange_hash
, 'A', keyspace
); cscipher
->setcsiv(keyspace
);
1848 ssh2_mkkey(K
, exchange_hash
, 'B', keyspace
); sccipher
->setsciv(keyspace
);
1849 ssh2_mkkey(K
, exchange_hash
, 'E', keyspace
); csmac
->setcskey(keyspace
);
1850 ssh2_mkkey(K
, exchange_hash
, 'F', keyspace
); scmac
->setsckey(keyspace
);
1853 * Now we're encrypting. Begin returning 1 to the protocol main
1854 * function so that other things can run on top of the
1855 * transport. If we ever see a KEXINIT, we must go back to the
1860 } while (!(ispkt
&& pktin
.type
== SSH2_MSG_KEXINIT
));
1861 goto begin_key_exchange
;
1867 * SSH2: remote identifier for the main session channel.
1869 static unsigned long ssh_remote_channel
;
1872 * Handle the SSH2 userauth and connection layers.
1874 static void do_ssh2_authconn(unsigned char *in
, int inlen
, int ispkt
)
1876 static unsigned long remote_winsize
;
1877 static unsigned long remote_maxpkt
;
1882 * Request userauth protocol, and await a response to it.
1884 ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST
);
1885 ssh2_pkt_addstring("ssh-userauth");
1887 crWaitUntilV(ispkt
);
1888 if (pktin
.type
!= SSH2_MSG_SERVICE_ACCEPT
)
1889 fatalbox("Server refused user authentication protocol");
1892 * FIXME: currently we support only password authentication.
1893 * (This places us technically in violation of the SSH2 spec.
1894 * We must fix this.)
1898 * Get a username and a password.
1900 static char username
[100];
1901 static char password
[100];
1905 if ((flags
& FLAG_CONNECTION
) && !*cfg
.username
) {
1906 c_write("login as: ", 10);
1908 crWaitUntilV(!ispkt
);
1909 while (inlen
--) switch (c
= *in
++) {
1916 c_write("\b \b", 3);
1922 c_write("\b \b", 3);
1931 if (((c
>= ' ' && c
<= '~') ||
1932 ((unsigned char)c
>= 160)) && pos
< 40) {
1933 username
[pos
++] = c
;
1940 username
[strcspn(username
, "\n\r")] = '\0';
1943 strncpy(username
, cfg
.username
, 99);
1944 username
[99] = '\0';
1945 if (flags
& FLAG_VERBOSE
) {
1946 sprintf(stuff
, "Using username \"%s\".\r\n", username
);
1947 c_write(stuff
, strlen(stuff
));
1951 if (!(flags
& FLAG_WINDOWED
)) {
1953 sprintf(prompt
, "%s@%s's password: ", cfg
.username
, savedhost
);
1954 if (!ssh_get_password(prompt
, password
, sizeof(password
))) {
1956 * get_password failed to get a password (for
1957 * example because one was supplied on the command
1958 * line which has already failed to work).
1961 logevent("No more passwords to try");
1962 ssh_state
= SSH_STATE_CLOSED
;
1966 c_write("password: ", 10);
1970 crWaitUntilV(!ispkt
);
1971 while (inlen
--) switch (c
= *in
++) {
1988 if (((c
>= ' ' && c
<= '~') ||
1989 ((unsigned char)c
>= 160)) && pos
< 40)
1990 password
[pos
++] = c
;
1997 ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST
);
1998 ssh2_pkt_addstring(username
);
1999 ssh2_pkt_addstring("ssh-connection"); /* service requested */
2000 ssh2_pkt_addstring("password");
2001 ssh2_pkt_addbool(FALSE
);
2002 ssh2_pkt_addstring(password
);
2005 crWaitUntilV(ispkt
);
2006 if (pktin
.type
!= SSH2_MSG_USERAUTH_SUCCESS
) {
2007 c_write("Access denied\r\n", 15);
2008 logevent("Authentication refused");
2014 * Now we're authenticated for the connection protocol. The
2015 * connection protocol will automatically have started at this
2016 * point; there's no need to send SERVICE_REQUEST.
2020 * So now create a channel with a session in it.
2022 ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN
);
2023 ssh2_pkt_addstring("session");
2024 ssh2_pkt_adduint32(100); /* as good as any */
2025 ssh2_pkt_adduint32(0xFFFFFFFFUL
); /* very big window which we ignore */
2026 ssh2_pkt_adduint32(0xFFFFFFFFUL
); /* very big max pkt size */
2028 crWaitUntilV(ispkt
);
2029 if (pktin
.type
!= SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
) {
2030 fatalbox("Server refused to open a session");
2031 /* FIXME: error data comes back in FAILURE packet */
2033 if (ssh2_pkt_getuint32() != 100) {
2034 fatalbox("Server's channel confirmation cited wrong channel");
2036 ssh_remote_channel
= ssh2_pkt_getuint32();
2037 remote_winsize
= ssh2_pkt_getuint32();
2038 remote_maxpkt
= ssh2_pkt_getuint32();
2039 logevent("Opened channel for session");
2042 * Now allocate a pty for the session.
2044 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
2045 ssh2_pkt_adduint32(ssh_remote_channel
); /* recipient channel */
2046 ssh2_pkt_addstring("pty-req");
2047 ssh2_pkt_addbool(1); /* want reply */
2048 ssh2_pkt_addstring(cfg
.termtype
);
2049 ssh2_pkt_adduint32(cols
);
2050 ssh2_pkt_adduint32(rows
);
2051 ssh2_pkt_adduint32(0); /* pixel width */
2052 ssh2_pkt_adduint32(0); /* pixel height */
2053 ssh2_pkt_addstring_start();
2054 ssh2_pkt_addstring_data("\0", 1); /* TTY_OP_END, no special options */
2057 do { /* FIXME: pay attention to these */
2058 crWaitUntilV(ispkt
);
2059 } while (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
2061 if (pktin
.type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
2062 if (pktin
.type
!= SSH2_MSG_CHANNEL_FAILURE
) {
2063 fatalbox("Server got confused by pty request");
2065 c_write("Server refused to allocate pty\r\n", 32);
2067 logevent("Allocated pty");
2073 ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST
);
2074 ssh2_pkt_adduint32(ssh_remote_channel
); /* recipient channel */
2075 ssh2_pkt_addstring("shell");
2076 ssh2_pkt_addbool(1); /* want reply */
2078 do { /* FIXME: pay attention to these */
2079 crWaitUntilV(ispkt
);
2080 } while (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
);
2081 if (pktin
.type
!= SSH2_MSG_CHANNEL_SUCCESS
) {
2082 if (pktin
.type
!= SSH2_MSG_CHANNEL_FAILURE
) {
2083 fatalbox("Server got confused by shell request");
2085 fatalbox("Server refused to start a shell");
2087 logevent("Started a shell");
2097 if (pktin
.type
== SSH2_MSG_CHANNEL_DATA
||
2098 pktin
.type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
) {
2101 if (ssh2_pkt_getuint32() != 100)
2102 continue; /* wrong channel */
2103 if (pktin
.type
== SSH2_MSG_CHANNEL_EXTENDED_DATA
&&
2104 ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR
)
2105 continue; /* extended but not stderr */
2106 ssh2_pkt_getstring(&data
, &length
);
2108 c_write(data
, length
);
2109 } else if (pktin
.type
== SSH2_MSG_DISCONNECT
) {
2110 ssh_state
= SSH_STATE_CLOSED
;
2111 logevent("Received disconnect request");
2112 } else if (pktin
.type
== SSH2_MSG_CHANNEL_REQUEST
) {
2113 continue; /* exit status et al; ignore (FIXME?) */
2114 } else if (pktin
.type
== SSH2_MSG_CHANNEL_WINDOW_ADJUST
) {
2115 continue; /* ignore for now (FIXME!) */
2117 fatalbox("Strange packet received: type %d", pktin
.type
);
2120 /* FIXME: for now, ignore window size */
2121 ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA
);
2122 ssh2_pkt_adduint32(ssh_remote_channel
);
2123 ssh2_pkt_addstring_start();
2124 ssh2_pkt_addstring_data(in
, inlen
);
2133 * Handle the top-level SSH2 protocol.
2135 static void ssh2_protocol(unsigned char *in
, int inlen
, int ispkt
)
2137 if (do_ssh2_transport(in
, inlen
, ispkt
) == 0)
2139 do_ssh2_authconn(in
, inlen
, ispkt
);
2143 * Called to set up the connection. Will arrange for WM_NETEVENT
2144 * messages to be passed to the specified window, whose window
2145 * procedure should then call telnet_msg().
2147 * Returns an error message, or NULL on success.
2149 static char *ssh_init (HWND hwnd
, char *host
, int port
, char **realhost
) {
2153 if(crypto_startup() == 0)
2154 return "Microsoft high encryption pack not installed!";
2157 p
= connect_to_host(host
, port
, realhost
);
2162 return "Protocol initialisation error";
2164 if (hwnd
&& WSAAsyncSelect (s
, hwnd
, WM_NETEVENT
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
2165 switch (WSAGetLastError()) {
2166 case WSAENETDOWN
: return "Network is down";
2167 default: return "WSAAsyncSelect(): unknown error";
2174 * Process a WM_NETEVENT message. Will return 0 if the connection
2175 * has closed, or <0 for a socket error.
2177 static int ssh_msg (WPARAM wParam
, LPARAM lParam
) {
2182 * Because reading less than the whole of the available pending
2183 * data can generate an FD_READ event, we need to allow for the
2184 * possibility that FD_READ may arrive with FD_CLOSE already in
2185 * the queue; so it's possible that we can get here even with s
2186 * invalid. If so, we return 1 and don't worry about it.
2188 if (s
== INVALID_SOCKET
)
2191 if (WSAGETSELECTERROR(lParam
) != 0)
2192 return -WSAGETSELECTERROR(lParam
);
2194 switch (WSAGETSELECTEVENT(lParam
)) {
2197 ret
= recv(s
, buf
, sizeof(buf
), 0);
2198 if (ret
< 0 && WSAGetLastError() == WSAEWOULDBLOCK
)
2200 if (ret
< 0) /* any _other_ error */
2201 return -10000-WSAGetLastError();
2206 ssh_gotdata (buf
, ret
);
2207 if (ssh_state
== SSH_STATE_CLOSED
) {
2214 return 1; /* shouldn't happen, but WTF */
2218 * Called to send data down the Telnet connection.
2220 static void ssh_send (char *buf
, int len
) {
2221 if (s
== INVALID_SOCKET
)
2224 ssh_protocol(buf
, len
, 0);
2228 * Called to set the size of the window from Telnet's POV.
2230 static void ssh_size(void) {
2231 switch (ssh_state
) {
2232 case SSH_STATE_BEFORE_SIZE
:
2233 case SSH_STATE_CLOSED
:
2234 break; /* do nothing */
2235 case SSH_STATE_INTERMED
:
2236 size_needed
= TRUE
; /* buffer for later */
2238 case SSH_STATE_SESSION
:
2240 send_packet(SSH1_CMSG_WINDOW_SIZE
,
2241 PKT_INT
, rows
, PKT_INT
, cols
,
2242 PKT_INT
, 0, PKT_INT
, 0, PKT_END
);
2248 * Send Telnet special codes. TS_EOF is useful for `plink', so you
2249 * can send an EOF and collect resulting output (e.g. `plink
2252 static void ssh_special (Telnet_Special code
) {
2253 if (code
== TS_EOF
) {
2254 if (ssh_version
= 1) {
2255 send_packet(SSH1_CMSG_EOF
, PKT_END
);
2257 ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF
);
2258 ssh2_pkt_adduint32(ssh_remote_channel
);
2268 * Read and decrypt one incoming SSH packet.
2269 * (only used by pSCP)
2271 static void get_packet(void)
2273 unsigned char buf
[4096], *p
;
2280 while ((to_read
= s_rdpkt(&p
, &len
)) > 0) {
2281 if (to_read
> sizeof(buf
)) to_read
= sizeof(buf
);
2282 len
= s_read(buf
, to_read
);
2283 if (len
!= to_read
) {
2295 * Receive a block of data over the SSH link. Block until
2296 * all data is available. Return nr of bytes read (0 if lost connection).
2297 * (only used by pSCP)
2299 int ssh_scp_recv(unsigned char *buf
, int len
)
2301 static int pending_input_len
= 0;
2302 static unsigned char *pending_input_ptr
;
2305 if (pending_input_len
>= to_read
) {
2306 memcpy(buf
, pending_input_ptr
, to_read
);
2307 pending_input_ptr
+= to_read
;
2308 pending_input_len
-= to_read
;
2312 if (pending_input_len
> 0) {
2313 memcpy(buf
, pending_input_ptr
, pending_input_len
);
2314 buf
+= pending_input_len
;
2315 to_read
-= pending_input_len
;
2316 pending_input_len
= 0;
2319 if (s
== INVALID_SOCKET
)
2321 while (to_read
> 0) {
2323 if (s
== INVALID_SOCKET
)
2325 if (pktin
.type
== SSH1_SMSG_STDOUT_DATA
) {
2326 int plen
= GET_32BIT(pktin
.body
);
2327 if (plen
<= to_read
) {
2328 memcpy(buf
, pktin
.body
+ 4, plen
);
2332 memcpy(buf
, pktin
.body
+ 4, to_read
);
2333 pending_input_len
= plen
- to_read
;
2334 pending_input_ptr
= pktin
.body
+ 4 + to_read
;
2337 } else if (pktin
.type
== SSH1_SMSG_STDERR_DATA
) {
2338 int plen
= GET_32BIT(pktin
.body
);
2339 fwrite(pktin
.body
+ 4, plen
, 1, stderr
);
2340 } else if (pktin
.type
== SSH1_MSG_DISCONNECT
) {
2341 logevent("Received disconnect request");
2342 } else if (pktin
.type
== SSH1_SMSG_SUCCESS
||
2343 pktin
.type
== SSH1_SMSG_FAILURE
) {
2345 } else if (pktin
.type
== SSH1_SMSG_EXIT_STATUS
) {
2347 sprintf(logbuf
, "Remote exit status: %d", GET_32BIT(pktin
.body
));
2349 send_packet(SSH1_CMSG_EXIT_CONFIRMATION
, PKT_END
);
2350 logevent("Closing connection");
2354 fatalbox("Strange packet received: type %d", pktin
.type
);
2362 * Send a block of data over the SSH link.
2363 * Block until all data is sent.
2364 * (only used by pSCP)
2366 void ssh_scp_send(unsigned char *buf
, int len
)
2368 if (s
== INVALID_SOCKET
)
2370 send_packet(SSH1_CMSG_STDIN_DATA
,
2371 PKT_INT
, len
, PKT_DATA
, buf
, len
, PKT_END
);
2375 * Send an EOF notification to the server.
2376 * (only used by pSCP)
2378 void ssh_scp_send_eof(void)
2380 if (s
== INVALID_SOCKET
)
2382 send_packet(SSH1_CMSG_EOF
, PKT_END
);
2386 * Set up the connection, login on the remote host and
2387 * start execution of a command.
2388 * Returns an error message, or NULL on success.
2389 * (only used by pSCP)
2391 char *ssh_scp_init(char *host
, int port
, char *cmd
, char **realhost
)
2396 if (crypto_startup() == 0)
2397 return "Microsoft high encryption pack not installed!";
2400 p
= connect_to_host(host
, port
, realhost
);
2407 return "Protocol initialisation error";
2409 /* Exchange keys and login */
2412 if (s
== INVALID_SOCKET
)
2413 return "Connection closed by remote host";
2414 } while (!do_ssh1_login(NULL
, 0, 1));
2416 if (ssh_state
== SSH_STATE_CLOSED
) {
2419 return "Session initialisation error";
2422 /* Execute command */
2423 sprintf(buf
, "Sending command: %.100s", cmd
);
2425 send_packet(SSH1_CMSG_EXEC_CMD
, PKT_STR
, cmd
, PKT_END
);
2430 static SOCKET
ssh_socket(void) { return s
; }
2432 static int ssh_sendok(void) { return ssh_send_ok
; }
2434 Backend ssh_backend
= {