18 #define SSH_MSG_DISCONNECT 1
19 #define SSH_SMSG_PUBLIC_KEY 2
20 #define SSH_CMSG_SESSION_KEY 3
21 #define SSH_CMSG_USER 4
22 #define SSH_CMSG_AUTH_PASSWORD 9
23 #define SSH_CMSG_REQUEST_PTY 10
24 #define SSH_CMSG_WINDOW_SIZE 11
25 #define SSH_CMSG_EXEC_SHELL 12
26 #define SSH_CMSG_EXEC_CMD 13
27 #define SSH_SMSG_SUCCESS 14
28 #define SSH_SMSG_FAILURE 15
29 #define SSH_CMSG_STDIN_DATA 16
30 #define SSH_SMSG_STDOUT_DATA 17
31 #define SSH_SMSG_STDERR_DATA 18
32 #define SSH_CMSG_EOF 19
33 #define SSH_SMSG_EXIT_STATUS 20
34 #define SSH_CMSG_EXIT_CONFIRMATION 33
35 #define SSH_MSG_IGNORE 32
36 #define SSH_MSG_DEBUG 36
37 #define SSH_CMSG_REQUEST_COMPRESSION 37
38 #define SSH_CMSG_AUTH_TIS 39
39 #define SSH_SMSG_AUTH_TIS_CHALLENGE 40
40 #define SSH_CMSG_AUTH_TIS_RESPONSE 41
42 #define SSH_AUTH_TIS 5
43 #define COMPRESSION_LEVEL 5
45 #define GET_32BIT(cp) \
46 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
47 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
48 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
49 ((unsigned long)(unsigned char)(cp)[3]))
51 #define PUT_32BIT(cp, value) { \
52 (cp)[0] = (unsigned char)((value) >> 24); \
53 (cp)[1] = (unsigned char)((value) >> 16); \
54 (cp)[2] = (unsigned char)((value) >> 8); \
55 (cp)[3] = (unsigned char)(value); }
57 enum { PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR };
59 /* Coroutine mechanics for the sillier bits of the code */
60 #define crBegin1 static int crLine = 0;
61 #define crBegin2 switch(crLine) { case 0:;
62 #define crBegin crBegin1; crBegin2;
63 #define crFinish(z) } crLine = 0; return (z)
64 #define crFinishV } crLine = 0; return
67 crLine=__LINE__; return (z); case __LINE__:;\
71 crLine=__LINE__; return; case __LINE__:;\
73 #define crStop(z) do{ crLine = 0; return (z); }while(0)
74 #define crStopV do{ crLine = 0; return; }while(0)
75 #define crWaitUntil(c) do { crReturn(0); } while (!(c))
77 static SOCKET s = INVALID_SOCKET;
79 static unsigned char session_key[32];
80 static struct ssh_cipher *cipher = NULL;
81 static int use_compression = 0;
82 void (*ssh_get_password)(const char *prompt, char *str, int maxlen) = NULL;
84 static char *savedhost;
87 SSH_STATE_BEFORE_SIZE,
91 } ssh_state = SSH_STATE_BEFORE_SIZE;
93 static int size_needed = FALSE;
95 static void s_write (char *buf, int len) {
97 int i = send (s, buf, len, 0);
101 fatalbox("Lost connection while sending");
108 static int s_read (char *buf, int len) {
111 int i = recv (s, buf, len, 0);
115 len -= i, buf += i, ret += i;
122 static void c_write (char *buf, int len) {
124 if (len > 0 && buf[len-1] == '\n') len--;
125 if (len > 0 && buf[len-1] == '\r') len--;
126 if (len > 0) { fwrite(buf, len, 1, stderr); fputc('\n', stderr); }
130 int new_head = (inbuf_head + 1) & INBUF_MASK;
131 if (new_head != inbuf_reap) {
132 inbuf[inbuf_head] = *buf++;
133 inbuf_head = new_head;
136 if( inbuf_head == inbuf_reap ) len++; else break;
149 static struct Packet pktin = { 0, 0, NULL, NULL, 0 };
150 static struct Packet pktout = { 0, 0, NULL, NULL, 0 };
152 static void ssh_protocol(unsigned char *in, int inlen, int ispkt);
153 static void ssh_size(void);
156 static void pktalloc(struct Packet *pkt, long newlen)
158 if (pkt->maxlen < newlen) {
159 pkt->maxlen = newlen;
161 /* Allocate enough buffer space for extra block
162 * for MS CryptEncrypt() */
163 pkt->data = (pkt->data == NULL ? malloc(newlen+8) :
164 realloc(pkt->data, newlen+8));
166 pkt->data = (pkt->data == NULL ? malloc(newlen) :
167 realloc(pkt->data, newlen));
170 fatalbox("Out of memory");
175 * Collect incoming data in the incoming packet buffer.
176 * Decihper and verify the packet when it is completely read.
177 * Drop SSH_MSG_DEBUG and SSH_MSG_IGNORE packets.
178 * Update the *data and *datalen variables.
179 * Return the additional nr of bytes needed, or 0 when
180 * a complete packet is available.
182 static int s_rdpkt(unsigned char **data, int *datalen)
184 static long len, pad, biglen, to_read;
185 static unsigned long realcrc, gotcrc;
186 static unsigned char *p;
196 for (i = len = 0; i < 4; i++) {
197 while ((*datalen) == 0)
199 len = (len << 8) + **data;
200 (*data)++, (*datalen)--;
204 if (len == 0x52656d6f) { /* "Remo"te server has closed ... */
205 len = 0x300; /* big enough to carry to end */
212 if (pktin.maxlen < biglen)
213 pktalloc(&pktin, biglen);
217 while (to_read > 0) {
220 while ((*datalen) == 0)
222 if (chunk > (*datalen))
224 memcpy(p, *data, chunk);
232 cipher->decrypt(pktin.data, biglen);
234 realcrc = crc32(pktin.data, biglen-4);
235 gotcrc = GET_32BIT(pktin.data+biglen-4);
236 if (gotcrc != realcrc) {
237 fatalbox("Incorrect CRC received on packet");
240 if (use_compression) {
241 ssh_decompress(pktin.data + pad, len - 4, &p, &len);
242 pktin.length = len - 1;
246 pktin.length = len - 5;
247 pktin.type = pktin.data[pad];
248 pktin.body = pktin.data + pad + 1;
251 if (pktin.type == SSH_SMSG_STDOUT_DATA ||
252 pktin.type == SSH_SMSG_STDERR_DATA ||
253 pktin.type == SSH_MSG_DEBUG ||
254 pktin.type == SSH_SMSG_AUTH_TIS_CHALLENGE) {
255 long strlen = GET_32BIT(pktin.body);
256 if (strlen + 4 != pktin.length)
257 fatalbox("Received data packet with bogus string length");
260 if (pktin.type == SSH_MSG_DEBUG) {
261 /* log debug message */
263 int strlen = GET_32BIT(pktin.body);
264 strcpy(buf, "Remote: ");
265 if (strlen > 70) strlen = 70;
266 memcpy(buf+8, pktin.body+4, strlen);
267 buf[8+strlen] = '\0';
270 } else if (pktin.type == SSH_MSG_IGNORE) {
278 static void ssh_gotdata(unsigned char *data, int datalen)
280 while (datalen > 0) {
281 if ( s_rdpkt(&data, &datalen) == 0 )
282 ssh_protocol(NULL, 0, 1);
287 static void s_wrpkt_start(int type, int len) {
290 len += 5; /* type and CRC */
294 pktout.length = len-5;
295 if (pktout.maxlen < biglen + 4)
296 pktalloc(&pktout, biglen + 4);
299 pktout.body = pktout.data+4+pad+1;
302 static void s_wrpkt(void) {
303 int pad, len, biglen, i;
306 len = pktout.length + 5; /* type and CRC */
309 pktout.body[-1] = pktout.type;
311 if (use_compression) {
315 ssh_compress(pktout.data + 4 + pad, pktout.length + 1, &p, &n);
319 if (pktout.maxlen < biglen + 4)
320 pktalloc(&pktout, biglen + 4);
321 memcpy(pktout.data + 4 + pad, p, n);
324 for (i=0; i<pad; i++)
325 pktout.data[i+4] = random_byte();
326 crc = crc32(pktout.data+4, biglen-4);
327 PUT_32BIT(pktout.data+biglen, crc);
328 PUT_32BIT(pktout.data, len);
331 cipher->encrypt(pktout.data+4, biglen);
333 s_write(pktout.data, biglen+4);
337 * Construct a packet with the specified contents and
338 * send it to the server.
340 static void send_packet(int pkttype, ...)
343 unsigned char *p, *argp, argchar;
344 unsigned long argint;
345 int pktlen, argtype, arglen;
348 va_start(args, pkttype);
349 while ((argtype = va_arg(args, int)) != PKT_END) {
352 (void) va_arg(args, int);
356 (void) va_arg(args, char);
360 (void) va_arg(args, unsigned char *);
361 arglen = va_arg(args, int);
365 argp = va_arg(args, unsigned char *);
366 arglen = strlen(argp);
367 pktlen += 4 + arglen;
375 s_wrpkt_start(pkttype, pktlen);
378 va_start(args, pkttype);
379 while ((argtype = va_arg(args, int)) != PKT_END) {
382 argint = va_arg(args, int);
383 PUT_32BIT(p, argint);
387 argchar = va_arg(args, unsigned char);
392 argp = va_arg(args, unsigned char *);
393 arglen = va_arg(args, int);
394 memcpy(p, argp, arglen);
398 argp = va_arg(args, unsigned char *);
399 arglen = strlen(argp);
400 PUT_32BIT(p, arglen);
401 memcpy(p + 4, argp, arglen);
413 * Connect to specified host and port.
414 * Returns an error message, or NULL on success.
415 * Also places the canonical host name into `realhost'.
417 static char *connect_to_host(char *host, int port, char **realhost)
427 savedhost = malloc(1+strlen(host));
429 fatalbox("Out of memory");
430 strcpy(savedhost, host);
433 port = 22; /* default ssh port */
445 if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
446 if ( (h = gethostbyname(host)) == NULL)
447 switch (WSAGetLastError()) {
448 case WSAENETDOWN: return "Network is down";
449 case WSAHOST_NOT_FOUND: case WSANO_DATA:
450 return "Host does not exist";
451 case WSATRY_AGAIN: return "Host not found";
452 default: return "gethostbyname: unknown error";
454 memcpy (&a, h->h_addr, sizeof(a));
455 *realhost = h->h_name;
466 s = socket(AF_INET, SOCK_STREAM, 0);
467 if (s == INVALID_SOCKET)
468 switch (WSAGetLastError()) {
469 case WSAENETDOWN: return "Network is down";
470 case WSAEAFNOSUPPORT: return "TCP/IP support not present";
471 default: return "socket(): unknown error";
475 * Bind to local address.
477 addr.sin_family = AF_INET;
478 addr.sin_addr.s_addr = htonl(INADDR_ANY);
479 addr.sin_port = htons(0);
480 if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
481 switch (WSAGetLastError()) {
482 case WSAENETDOWN: return "Network is down";
483 default: return "bind(): unknown error";
487 * Connect to remote address.
489 addr.sin_addr.s_addr = htonl(a);
490 addr.sin_port = htons((short)port);
491 if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
492 switch (WSAGetLastError()) {
493 case WSAENETDOWN: return "Network is down";
494 case WSAECONNREFUSED: return "Connection refused";
495 case WSAENETUNREACH: return "Network is unreachable";
496 case WSAEHOSTUNREACH: return "No route to host";
497 default: return "connect(): unknown error";
501 send(s, "connect ", 8, 0);
502 send(s, FWhost, strlen(FWhost), 0);
505 sprintf(buf, " %d\n", FWport);
506 send (s, buf, strlen(buf), 0);
513 static int ssh_versioncmp(char *a, char *b) {
515 unsigned long av, bv;
517 av = strtoul(a, &ae, 10);
518 bv = strtoul(b, &be, 10);
519 if (av != bv) return (av < bv ? -1 : +1);
520 if (*ae == '.') ae++;
521 if (*be == '.') be++;
522 av = strtoul(ae, &ae, 10);
523 bv = strtoul(be, &be, 10);
524 if (av != bv) return (av < bv ? -1 : +1);
528 static int do_ssh_init(void) {
532 char vlog[sizeof(vstring)+20];
537 while (s_read(&c, 1) == 1) {
538 if (c == 'S' && i < 2) i++;
539 else if (c == 'S' && i == 2) i = 2;
540 else if (c == 'H' && i == 2) break;
544 if (s_read(&c,1) != 1 || c != 'S') return 0;
545 if (s_read(&c,1) != 1 || c != 'S') return 0;
546 if (s_read(&c,1) != 1 || c != 'H') return 0;
548 strcpy(vstring, "SSH-");
550 if (s_read(&c,1) != 1 || c != '-') return 0;
553 if (s_read(&c,1) != 1)
555 if (vsp < vstring+sizeof(vstring)-1)
561 } else if (i < sizeof(version)-1)
569 sprintf(vlog, "Server version: %s", vstring);
570 vlog[strcspn(vlog, "\r\n")] = '\0';
573 sprintf(vstring, "SSH-%s-PuTTY\n",
574 (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5"));
575 sprintf(vlog, "We claim version: %s", vstring);
576 vlog[strcspn(vlog, "\r\n")] = '\0';
578 s_write(vstring, strlen(vstring));
583 * Handle the key exchange and user authentication phases.
585 static int do_ssh_login(unsigned char *in, int inlen, int ispkt)
588 unsigned char session_id[16];
589 unsigned char *rsabuf, *keystr1, *keystr2;
590 unsigned char cookie[8];
591 struct RSAKey servkey, hostkey;
592 struct MD5Context md5c;
593 static unsigned long supported_ciphers_mask, supported_auths_mask;
596 extern struct ssh_cipher ssh_3des;
597 extern struct ssh_cipher ssh_des;
598 extern struct ssh_cipher ssh_blowfish;
602 if (!ispkt) crWaitUntil(ispkt);
604 if (pktin.type != SSH_SMSG_PUBLIC_KEY)
605 fatalbox("Public key packet not received");
607 logevent("Received public keys");
609 memcpy(cookie, pktin.body, 8);
611 i = makekey(pktin.body+8, &servkey, &keystr1);
612 j = makekey(pktin.body+8+i, &hostkey, &keystr2);
615 * Hash the host key and print the hash in the log box. Just as
616 * a last resort in case the registry's host key checking is
617 * compromised, we'll allow the user some ability to verify
621 MD5Update(&md5c, keystr2, hostkey.bytes);
622 MD5Final(session_id, &md5c);
626 logevent("Host key MD5 is:");
628 for (i = 0; i < 16; i++)
629 sprintf(logmsg+strlen(logmsg), "%02x", session_id[i]);
633 supported_ciphers_mask = GET_32BIT(pktin.body+12+i+j);
634 supported_auths_mask = GET_32BIT(pktin.body+16+i+j);
637 MD5Update(&md5c, keystr2, hostkey.bytes);
638 MD5Update(&md5c, keystr1, servkey.bytes);
639 MD5Update(&md5c, pktin.body, 8);
640 MD5Final(session_id, &md5c);
643 session_key[i] = random_byte();
645 len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
647 rsabuf = malloc(len);
649 fatalbox("Out of memory");
652 * Verify the host key.
656 * First format the key into a string.
658 int len = rsastr_len(&hostkey);
659 char *keystr = malloc(len);
661 fatalbox("Out of memory");
662 rsastr_fmt(keystr, &hostkey);
663 verify_ssh_host_key(savedhost, keystr);
667 for (i=0; i<32; i++) {
668 rsabuf[i] = session_key[i];
670 rsabuf[i] ^= session_id[i];
673 if (hostkey.bytes > servkey.bytes) {
674 rsaencrypt(rsabuf, 32, &servkey);
675 rsaencrypt(rsabuf, servkey.bytes, &hostkey);
677 rsaencrypt(rsabuf, 32, &hostkey);
678 rsaencrypt(rsabuf, hostkey.bytes, &servkey);
681 logevent("Encrypted session key");
683 cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH :
684 cfg.cipher == CIPHER_DES ? SSH_CIPHER_DES :
686 if ((supported_ciphers_mask & (1 << cipher_type)) == 0) {
687 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
688 cipher_type = SSH_CIPHER_3DES;
690 switch (cipher_type) {
691 case SSH_CIPHER_3DES: logevent("Using 3DES encryption"); break;
692 case SSH_CIPHER_DES: logevent("Using single-DES encryption"); break;
693 case SSH_CIPHER_BLOWFISH: logevent("Using Blowfish encryption"); break;
696 send_packet(SSH_CMSG_SESSION_KEY,
697 PKT_CHAR, cipher_type,
699 PKT_CHAR, (len*8) >> 8, PKT_CHAR, (len*8) & 0xFF,
700 PKT_DATA, rsabuf, len,
704 logevent("Trying to enable encryption...");
708 cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish :
709 cipher_type == SSH_CIPHER_DES ? &ssh_des :
711 cipher->sesskey(session_key);
715 if (pktin.type != SSH_SMSG_SUCCESS)
716 fatalbox("Encryption not successfully enabled");
718 logevent("Successfully started encryption");
722 static char username[100];
725 if (!IS_SCP && !*cfg.username) {
726 c_write("login as: ", 10);
729 while (inlen--) switch (c = *in++) {
751 if (((c >= ' ' && c <= '~') ||
752 ((unsigned char)c >= 160)) && pos < 40) {
760 username[strcspn(username, "\n\r")] = '\0';
763 strncpy(username, cfg.username, 99);
766 sprintf(stuff, "Sent username \"%s\".\r\n", username);
767 c_write(stuff, strlen(stuff));
771 send_packet(SSH_CMSG_USER, PKT_STR, username, PKT_END);
773 char userlog[20+sizeof(username)];
774 sprintf(userlog, "Sent username \"%s\"", username);
781 while (pktin.type == SSH_SMSG_FAILURE) {
782 static char password[100];
785 static int pwpkt_type;
788 * Show password prompt, having first obtained it via a TIS
789 * exchange if we're doing TIS authentication.
791 pwpkt_type = SSH_CMSG_AUTH_PASSWORD;
795 sprintf(prompt, "%s@%s's password: ", cfg.username, savedhost);
796 ssh_get_password(prompt, password, sizeof(password));
799 if (pktin.type == SSH_SMSG_FAILURE &&
801 (supported_auths_mask & (1<<SSH_AUTH_TIS))) {
802 pwpkt_type = SSH_CMSG_AUTH_TIS_RESPONSE;
803 logevent("Requested TIS authentication");
804 send_packet(SSH_CMSG_AUTH_TIS, PKT_END);
806 if (pktin.type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
807 logevent("TIS authentication declined");
808 c_write("TIS authentication refused.\r\n", 29);
810 int challengelen = ((pktin.body[0] << 24) |
811 (pktin.body[1] << 16) |
812 (pktin.body[2] << 8) |
814 logevent("Received TIS challenge");
815 c_write(pktin.body+4, challengelen);
818 if (pwpkt_type == SSH_CMSG_AUTH_PASSWORD)
819 c_write("password: ", 10);
824 while (inlen--) switch (c = *in++) {
841 if (((c >= ' ' && c <= '~') ||
842 ((unsigned char)c >= 160)) && pos < 40)
851 send_packet(pwpkt_type, PKT_STR, password, PKT_END);
852 logevent("Sent password");
853 memset(password, 0, strlen(password));
855 if (pktin.type == SSH_SMSG_FAILURE) {
856 c_write("Access denied\r\n", 15);
857 logevent("Authentication refused");
858 } else if (pktin.type == SSH_MSG_DISCONNECT) {
859 logevent("Received disconnect request");
861 } else if (pktin.type != SSH_SMSG_SUCCESS) {
862 fatalbox("Strange packet received, type %d", pktin.type);
866 logevent("Authentication successful");
868 if (cfg.ssh_compression &&
869 ssh_compression_init(COMPRESSION_LEVEL)) {
870 send_packet(SSH_CMSG_REQUEST_COMPRESSION,
871 PKT_INT, COMPRESSION_LEVEL, PKT_END);
873 if (pktin.type == SSH_SMSG_SUCCESS) {
875 logevent("Enabled SSH packet compression");
876 } else if (pktin.type == SSH_SMSG_FAILURE) {
877 logevent("Server does not support packet compression");
879 fatalbox("Strange packet received, type %d", pktin.type);
886 static void ssh_protocol(unsigned char *in, int inlen, int ispkt) {
891 while (!do_ssh_login(in, inlen, ispkt))
895 send_packet(SSH_CMSG_REQUEST_PTY,
896 PKT_STR, cfg.termtype,
897 PKT_INT, rows, PKT_INT, cols,
898 PKT_INT, 0, PKT_INT, 0,
901 ssh_state = SSH_STATE_INTERMED;
902 do { crReturnV; } while (!ispkt);
903 if (pktin.type != SSH_SMSG_SUCCESS && pktin.type != SSH_SMSG_FAILURE) {
904 fatalbox("Protocol confusion");
905 } else if (pktin.type == SSH_SMSG_FAILURE) {
906 c_write("Server refused to allocate pty\r\n", 32);
908 logevent("Allocated pty");
911 send_packet(SSH_CMSG_EXEC_SHELL, PKT_END);
912 logevent("Started session");
914 ssh_state = SSH_STATE_SESSION;
921 if (pktin.type == SSH_SMSG_STDOUT_DATA ||
922 pktin.type == SSH_SMSG_STDERR_DATA) {
923 long len = GET_32BIT(pktin.body);
924 c_write(pktin.body+4, len);
925 } else if (pktin.type == SSH_MSG_DISCONNECT) {
926 ssh_state = SSH_STATE_CLOSED;
927 logevent("Received disconnect request");
928 } else if (pktin.type == SSH_SMSG_SUCCESS) {
929 /* may be from EXEC_SHELL on some servers */
930 } else if (pktin.type == SSH_SMSG_FAILURE) {
931 /* may be from EXEC_SHELL on some servers
932 * if no pty is available or in other odd cases. Ignore */
933 } else if (pktin.type == SSH_SMSG_EXIT_STATUS) {
934 send_packet(SSH_CMSG_EXIT_CONFIRMATION, PKT_END);
936 fatalbox("Strange packet received: type %d", pktin.type);
939 send_packet(SSH_CMSG_STDIN_DATA,
940 PKT_INT, inlen, PKT_DATA, in, inlen, PKT_END);
948 * Called to set up the connection. Will arrange for WM_NETEVENT
949 * messages to be passed to the specified window, whose window
950 * procedure should then call telnet_msg().
952 * Returns an error message, or NULL on success.
954 static char *ssh_init (HWND hwnd, char *host, int port, char **realhost) {
958 if(crypto_startup() == 0)
959 return "Microsoft high encryption pack not installed!";
962 p = connect_to_host(host, port, realhost);
967 return "Protocol initialisation error";
969 if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | FD_CLOSE) == SOCKET_ERROR)
970 switch (WSAGetLastError()) {
971 case WSAENETDOWN: return "Network is down";
972 default: return "WSAAsyncSelect(): unknown error";
979 * Process a WM_NETEVENT message. Will return 0 if the connection
980 * has closed, or <0 for a socket error.
982 static int ssh_msg (WPARAM wParam, LPARAM lParam) {
987 * Because reading less than the whole of the available pending
988 * data can generate an FD_READ event, we need to allow for the
989 * possibility that FD_READ may arrive with FD_CLOSE already in
990 * the queue; so it's possible that we can get here even with s
991 * invalid. If so, we return 1 and don't worry about it.
993 if (s == INVALID_SOCKET)
996 if (WSAGETSELECTERROR(lParam) != 0)
997 return -WSAGETSELECTERROR(lParam);
999 switch (WSAGETSELECTEVENT(lParam)) {
1002 ret = recv(s, buf, sizeof(buf), 0);
1003 if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
1005 if (ret < 0) /* any _other_ error */
1006 return -10000-WSAGetLastError();
1011 ssh_gotdata (buf, ret);
1014 return 1; /* shouldn't happen, but WTF */
1018 * Called to send data down the Telnet connection.
1020 static void ssh_send (char *buf, int len) {
1021 if (s == INVALID_SOCKET)
1024 ssh_protocol(buf, len, 0);
1028 * Called to set the size of the window from Telnet's POV.
1030 static void ssh_size(void) {
1031 switch (ssh_state) {
1032 case SSH_STATE_BEFORE_SIZE:
1033 case SSH_STATE_CLOSED:
1034 break; /* do nothing */
1035 case SSH_STATE_INTERMED:
1036 size_needed = TRUE; /* buffer for later */
1038 case SSH_STATE_SESSION:
1040 send_packet(SSH_CMSG_WINDOW_SIZE,
1041 PKT_INT, rows, PKT_INT, cols,
1042 PKT_INT, 0, PKT_INT, 0, PKT_END);
1048 * (Send Telnet special codes)
1050 static void ssh_special (Telnet_Special code) {
1056 * Read and decrypt one incoming SSH packet.
1057 * (only used by pSCP)
1059 static void get_packet(void)
1061 unsigned char buf[4096], *p;
1070 while ((to_read = s_rdpkt(&p, &len)) > 0) {
1071 if (to_read > sizeof(buf)) to_read = sizeof(buf);
1072 len = s_read(buf, to_read);
1073 if (len != to_read) {
1085 * Receive a block of data over the SSH link. Block until
1086 * all data is available. Return nr of bytes read (0 if lost connection).
1087 * (only used by pSCP)
1089 int ssh_scp_recv(unsigned char *buf, int len)
1091 static int pending_input_len = 0;
1092 static unsigned char *pending_input_ptr;
1097 if (pending_input_len >= to_read) {
1098 memcpy(buf, pending_input_ptr, to_read);
1099 pending_input_ptr += to_read;
1100 pending_input_len -= to_read;
1104 if (pending_input_len > 0) {
1105 memcpy(buf, pending_input_ptr, pending_input_len);
1106 buf += pending_input_len;
1107 to_read -= pending_input_len;
1108 pending_input_len = 0;
1111 if (s == INVALID_SOCKET)
1113 while (to_read > 0) {
1115 if (s == INVALID_SOCKET)
1117 if (pktin.type == SSH_SMSG_STDOUT_DATA) {
1118 int plen = GET_32BIT(pktin.body);
1119 if (plen <= to_read) {
1120 memcpy(buf, pktin.body + 4, plen);
1124 memcpy(buf, pktin.body + 4, to_read);
1125 pending_input_len = plen - to_read;
1126 pending_input_ptr = pktin.body + 4 + to_read;
1129 } else if (pktin.type == SSH_SMSG_STDERR_DATA) {
1130 int plen = GET_32BIT(pktin.body);
1131 fwrite(pktin.body + 4, plen, 1, stderr);
1132 } else if (pktin.type == SSH_MSG_DISCONNECT) {
1133 logevent("Received disconnect request");
1134 } else if (pktin.type == SSH_SMSG_SUCCESS ||
1135 pktin.type == SSH_SMSG_FAILURE) {
1137 } else if (pktin.type == SSH_SMSG_EXIT_STATUS) {
1139 sprintf(logbuf, "Remote exit status: %d", GET_32BIT(pktin.body));
1141 send_packet(SSH_CMSG_EXIT_CONFIRMATION, PKT_END);
1142 logevent("Closing connection");
1146 fatalbox("Strange packet received: type %d", pktin.type);
1154 * Send a block of data over the SSH link.
1155 * Block until all data is sent.
1156 * (only used by pSCP)
1158 void ssh_scp_send(unsigned char *buf, int len)
1161 if (s == INVALID_SOCKET)
1163 send_packet(SSH_CMSG_STDIN_DATA,
1164 PKT_INT, len, PKT_DATA, buf, len, PKT_END);
1168 * Send an EOF notification to the server.
1169 * (only used by pSCP)
1171 void ssh_scp_send_eof(void)
1174 if (s == INVALID_SOCKET)
1176 send_packet(SSH_CMSG_EOF, PKT_END);
1180 * Set up the connection, login on the remote host and
1181 * start execution of a command.
1182 * Returns an error message, or NULL on success.
1183 * (only used by pSCP)
1185 char *ssh_scp_init(char *host, int port, char *cmd, char **realhost)
1192 if (crypto_startup() == 0)
1193 return "Microsoft high encryption pack not installed!";
1196 p = connect_to_host(host, port, realhost);
1203 return "Protocol initialisation error";
1205 /* Exchange keys and login */
1208 if (s == INVALID_SOCKET)
1209 return "Connection closed by remote host";
1210 } while (!do_ssh_login(NULL, 0, 1));
1212 /* Execute command */
1213 sprintf(buf, "Sending command: %.100s", cmd);
1215 send_packet(SSH_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END);
1221 Backend ssh_backend = {