+++ /dev/null
-/*\r
- * SSH backend.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <stdarg.h>\r
-#include <assert.h>\r
-#include <limits.h>\r
-#include <signal.h>\r
-\r
-#include "putty.h"\r
-#include "tree234.h"\r
-#include "ssh.h"\r
-#ifndef NO_GSSAPI\r
-#include "sshgssc.h"\r
-#include "sshgss.h"\r
-#endif\r
-\r
-#ifndef FALSE\r
-#define FALSE 0\r
-#endif\r
-#ifndef TRUE\r
-#define TRUE 1\r
-#endif\r
-\r
-#define SSH1_MSG_DISCONNECT 1 /* 0x1 */\r
-#define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */\r
-#define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */\r
-#define SSH1_CMSG_USER 4 /* 0x4 */\r
-#define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */\r
-#define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */\r
-#define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */\r
-#define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */\r
-#define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */\r
-#define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */\r
-#define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */\r
-#define SSH1_CMSG_EXEC_CMD 13 /* 0xd */\r
-#define SSH1_SMSG_SUCCESS 14 /* 0xe */\r
-#define SSH1_SMSG_FAILURE 15 /* 0xf */\r
-#define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */\r
-#define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */\r
-#define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */\r
-#define SSH1_CMSG_EOF 19 /* 0x13 */\r
-#define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */\r
-#define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */\r
-#define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */\r
-#define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */\r
-#define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */\r
-#define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */\r
-#define SSH1_SMSG_X11_OPEN 27 /* 0x1b */\r
-#define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */\r
-#define SSH1_MSG_PORT_OPEN 29 /* 0x1d */\r
-#define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */\r
-#define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */\r
-#define SSH1_MSG_IGNORE 32 /* 0x20 */\r
-#define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */\r
-#define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */\r
-#define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */\r
-#define SSH1_MSG_DEBUG 36 /* 0x24 */\r
-#define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */\r
-#define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */\r
-#define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */\r
-#define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */\r
-#define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */\r
-#define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */\r
-#define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */\r
-\r
-#define SSH1_AUTH_RHOSTS 1 /* 0x1 */\r
-#define SSH1_AUTH_RSA 2 /* 0x2 */\r
-#define SSH1_AUTH_PASSWORD 3 /* 0x3 */\r
-#define SSH1_AUTH_RHOSTS_RSA 4 /* 0x4 */\r
-#define SSH1_AUTH_TIS 5 /* 0x5 */\r
-#define SSH1_AUTH_CCARD 16 /* 0x10 */\r
-\r
-#define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */\r
-/* Mask for protoflags we will echo back to server if seen */\r
-#define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */\r
-\r
-#define SSH2_MSG_DISCONNECT 1 /* 0x1 */\r
-#define SSH2_MSG_IGNORE 2 /* 0x2 */\r
-#define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */\r
-#define SSH2_MSG_DEBUG 4 /* 0x4 */\r
-#define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */\r
-#define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */\r
-#define SSH2_MSG_KEXINIT 20 /* 0x14 */\r
-#define SSH2_MSG_NEWKEYS 21 /* 0x15 */\r
-#define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */\r
-#define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */\r
-#define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */\r
-#define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */\r
-#define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */\r
-#define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */\r
-#define SSH2_MSG_KEXRSA_PUBKEY 30 /* 0x1e */\r
-#define SSH2_MSG_KEXRSA_SECRET 31 /* 0x1f */\r
-#define SSH2_MSG_KEXRSA_DONE 32 /* 0x20 */\r
-#define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */\r
-#define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */\r
-#define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */\r
-#define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */\r
-#define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */\r
-#define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */\r
-#define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */\r
-#define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */\r
-#define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */\r
-#define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */\r
-#define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */\r
-#define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */\r
-#define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */\r
-#define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */\r
-#define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */\r
-#define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */\r
-#define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */\r
-#define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */\r
-#define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */\r
-#define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */\r
-#define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */\r
-#define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */\r
-#define SSH2_MSG_USERAUTH_GSSAPI_RESPONSE 60\r
-#define SSH2_MSG_USERAUTH_GSSAPI_TOKEN 61\r
-#define SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE 63\r
-#define SSH2_MSG_USERAUTH_GSSAPI_ERROR 64\r
-#define SSH2_MSG_USERAUTH_GSSAPI_ERRTOK 65\r
-#define SSH2_MSG_USERAUTH_GSSAPI_MIC 66\r
-\r
-/*\r
- * Packet type contexts, so that ssh2_pkt_type can correctly decode\r
- * the ambiguous type numbers back into the correct type strings.\r
- */\r
-typedef enum {\r
- SSH2_PKTCTX_NOKEX,\r
- SSH2_PKTCTX_DHGROUP,\r
- SSH2_PKTCTX_DHGEX,\r
- SSH2_PKTCTX_RSAKEX\r
-} Pkt_KCtx;\r
-typedef enum {\r
- SSH2_PKTCTX_NOAUTH,\r
- SSH2_PKTCTX_PUBLICKEY,\r
- SSH2_PKTCTX_PASSWORD,\r
- SSH2_PKTCTX_GSSAPI,\r
- SSH2_PKTCTX_KBDINTER\r
-} Pkt_ACtx;\r
-\r
-#define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */\r
-#define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */\r
-#define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */\r
-#define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */\r
-#define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */\r
-#define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */\r
-#define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */\r
-#define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */\r
-#define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */\r
-#define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */\r
-#define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */\r
-#define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */\r
-#define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */\r
-#define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */\r
-#define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */\r
-\r
-static const char *const ssh2_disconnect_reasons[] = {\r
- NULL,\r
- "host not allowed to connect",\r
- "protocol error",\r
- "key exchange failed",\r
- "host authentication failed",\r
- "MAC error",\r
- "compression error",\r
- "service not available",\r
- "protocol version not supported",\r
- "host key not verifiable",\r
- "connection lost",\r
- "by application",\r
- "too many connections",\r
- "auth cancelled by user",\r
- "no more auth methods available",\r
- "illegal user name",\r
-};\r
-\r
-#define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */\r
-#define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */\r
-#define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */\r
-#define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */\r
-\r
-#define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */\r
-\r
-/*\r
- * Various remote-bug flags.\r
- */\r
-#define BUG_CHOKES_ON_SSH1_IGNORE 1\r
-#define BUG_SSH2_HMAC 2\r
-#define BUG_NEEDS_SSH1_PLAIN_PASSWORD 4\r
-#define BUG_CHOKES_ON_RSA 8\r
-#define BUG_SSH2_RSA_PADDING 16\r
-#define BUG_SSH2_DERIVEKEY 32\r
-#define BUG_SSH2_REKEY 64\r
-#define BUG_SSH2_PK_SESSIONID 128\r
-#define BUG_SSH2_MAXPKT 256\r
-#define BUG_CHOKES_ON_SSH2_IGNORE 512\r
-\r
-/*\r
- * Codes for terminal modes.\r
- * Most of these are the same in SSH-1 and SSH-2.\r
- * This list is derived from RFC 4254 and\r
- * SSH-1 RFC-1.2.31.\r
- */\r
-static const struct {\r
- const char* const mode;\r
- int opcode;\r
- enum { TTY_OP_CHAR, TTY_OP_BOOL } type;\r
-} ssh_ttymodes[] = {\r
- /* "V" prefix discarded for special characters relative to SSH specs */\r
- { "INTR", 1, TTY_OP_CHAR },\r
- { "QUIT", 2, TTY_OP_CHAR },\r
- { "ERASE", 3, TTY_OP_CHAR },\r
- { "KILL", 4, TTY_OP_CHAR },\r
- { "EOF", 5, TTY_OP_CHAR },\r
- { "EOL", 6, TTY_OP_CHAR },\r
- { "EOL2", 7, TTY_OP_CHAR },\r
- { "START", 8, TTY_OP_CHAR },\r
- { "STOP", 9, TTY_OP_CHAR },\r
- { "SUSP", 10, TTY_OP_CHAR },\r
- { "DSUSP", 11, TTY_OP_CHAR },\r
- { "REPRINT", 12, TTY_OP_CHAR },\r
- { "WERASE", 13, TTY_OP_CHAR },\r
- { "LNEXT", 14, TTY_OP_CHAR },\r
- { "FLUSH", 15, TTY_OP_CHAR },\r
- { "SWTCH", 16, TTY_OP_CHAR },\r
- { "STATUS", 17, TTY_OP_CHAR },\r
- { "DISCARD", 18, TTY_OP_CHAR },\r
- { "IGNPAR", 30, TTY_OP_BOOL },\r
- { "PARMRK", 31, TTY_OP_BOOL },\r
- { "INPCK", 32, TTY_OP_BOOL },\r
- { "ISTRIP", 33, TTY_OP_BOOL },\r
- { "INLCR", 34, TTY_OP_BOOL },\r
- { "IGNCR", 35, TTY_OP_BOOL },\r
- { "ICRNL", 36, TTY_OP_BOOL },\r
- { "IUCLC", 37, TTY_OP_BOOL },\r
- { "IXON", 38, TTY_OP_BOOL },\r
- { "IXANY", 39, TTY_OP_BOOL },\r
- { "IXOFF", 40, TTY_OP_BOOL },\r
- { "IMAXBEL", 41, TTY_OP_BOOL },\r
- { "ISIG", 50, TTY_OP_BOOL },\r
- { "ICANON", 51, TTY_OP_BOOL },\r
- { "XCASE", 52, TTY_OP_BOOL },\r
- { "ECHO", 53, TTY_OP_BOOL },\r
- { "ECHOE", 54, TTY_OP_BOOL },\r
- { "ECHOK", 55, TTY_OP_BOOL },\r
- { "ECHONL", 56, TTY_OP_BOOL },\r
- { "NOFLSH", 57, TTY_OP_BOOL },\r
- { "TOSTOP", 58, TTY_OP_BOOL },\r
- { "IEXTEN", 59, TTY_OP_BOOL },\r
- { "ECHOCTL", 60, TTY_OP_BOOL },\r
- { "ECHOKE", 61, TTY_OP_BOOL },\r
- { "PENDIN", 62, TTY_OP_BOOL }, /* XXX is this a real mode? */\r
- { "OPOST", 70, TTY_OP_BOOL },\r
- { "OLCUC", 71, TTY_OP_BOOL },\r
- { "ONLCR", 72, TTY_OP_BOOL },\r
- { "OCRNL", 73, TTY_OP_BOOL },\r
- { "ONOCR", 74, TTY_OP_BOOL },\r
- { "ONLRET", 75, TTY_OP_BOOL },\r
- { "CS7", 90, TTY_OP_BOOL },\r
- { "CS8", 91, TTY_OP_BOOL },\r
- { "PARENB", 92, TTY_OP_BOOL },\r
- { "PARODD", 93, TTY_OP_BOOL }\r
-};\r
-\r
-/* Miscellaneous other tty-related constants. */\r
-#define SSH_TTY_OP_END 0\r
-/* The opcodes for ISPEED/OSPEED differ between SSH-1 and SSH-2. */\r
-#define SSH1_TTY_OP_ISPEED 192\r
-#define SSH1_TTY_OP_OSPEED 193\r
-#define SSH2_TTY_OP_ISPEED 128\r
-#define SSH2_TTY_OP_OSPEED 129\r
-\r
-/* Helper functions for parsing tty-related config. */\r
-static unsigned int ssh_tty_parse_specchar(char *s)\r
-{\r
- unsigned int ret;\r
- if (*s) {\r
- char *next = NULL;\r
- ret = ctrlparse(s, &next);\r
- if (!next) ret = s[0];\r
- } else {\r
- ret = 255; /* special value meaning "don't set" */\r
- }\r
- return ret;\r
-}\r
-static unsigned int ssh_tty_parse_boolean(char *s)\r
-{\r
- if (stricmp(s, "yes") == 0 ||\r
- stricmp(s, "on") == 0 ||\r
- stricmp(s, "true") == 0 ||\r
- stricmp(s, "+") == 0)\r
- return 1; /* true */\r
- else if (stricmp(s, "no") == 0 ||\r
- stricmp(s, "off") == 0 ||\r
- stricmp(s, "false") == 0 ||\r
- stricmp(s, "-") == 0)\r
- return 0; /* false */\r
- else\r
- return (atoi(s) != 0);\r
-}\r
-\r
-#define translate(x) if (type == x) return #x\r
-#define translatek(x,ctx) if (type == x && (pkt_kctx == ctx)) return #x\r
-#define translatea(x,ctx) if (type == x && (pkt_actx == ctx)) return #x\r
-static char *ssh1_pkt_type(int type)\r
-{\r
- translate(SSH1_MSG_DISCONNECT);\r
- translate(SSH1_SMSG_PUBLIC_KEY);\r
- translate(SSH1_CMSG_SESSION_KEY);\r
- translate(SSH1_CMSG_USER);\r
- translate(SSH1_CMSG_AUTH_RSA);\r
- translate(SSH1_SMSG_AUTH_RSA_CHALLENGE);\r
- translate(SSH1_CMSG_AUTH_RSA_RESPONSE);\r
- translate(SSH1_CMSG_AUTH_PASSWORD);\r
- translate(SSH1_CMSG_REQUEST_PTY);\r
- translate(SSH1_CMSG_WINDOW_SIZE);\r
- translate(SSH1_CMSG_EXEC_SHELL);\r
- translate(SSH1_CMSG_EXEC_CMD);\r
- translate(SSH1_SMSG_SUCCESS);\r
- translate(SSH1_SMSG_FAILURE);\r
- translate(SSH1_CMSG_STDIN_DATA);\r
- translate(SSH1_SMSG_STDOUT_DATA);\r
- translate(SSH1_SMSG_STDERR_DATA);\r
- translate(SSH1_CMSG_EOF);\r
- translate(SSH1_SMSG_EXIT_STATUS);\r
- translate(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION);\r
- translate(SSH1_MSG_CHANNEL_OPEN_FAILURE);\r
- translate(SSH1_MSG_CHANNEL_DATA);\r
- translate(SSH1_MSG_CHANNEL_CLOSE);\r
- translate(SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION);\r
- translate(SSH1_SMSG_X11_OPEN);\r
- translate(SSH1_CMSG_PORT_FORWARD_REQUEST);\r
- translate(SSH1_MSG_PORT_OPEN);\r
- translate(SSH1_CMSG_AGENT_REQUEST_FORWARDING);\r
- translate(SSH1_SMSG_AGENT_OPEN);\r
- translate(SSH1_MSG_IGNORE);\r
- translate(SSH1_CMSG_EXIT_CONFIRMATION);\r
- translate(SSH1_CMSG_X11_REQUEST_FORWARDING);\r
- translate(SSH1_CMSG_AUTH_RHOSTS_RSA);\r
- translate(SSH1_MSG_DEBUG);\r
- translate(SSH1_CMSG_REQUEST_COMPRESSION);\r
- translate(SSH1_CMSG_AUTH_TIS);\r
- translate(SSH1_SMSG_AUTH_TIS_CHALLENGE);\r
- translate(SSH1_CMSG_AUTH_TIS_RESPONSE);\r
- translate(SSH1_CMSG_AUTH_CCARD);\r
- translate(SSH1_SMSG_AUTH_CCARD_CHALLENGE);\r
- translate(SSH1_CMSG_AUTH_CCARD_RESPONSE);\r
- return "unknown";\r
-}\r
-static char *ssh2_pkt_type(Pkt_KCtx pkt_kctx, Pkt_ACtx pkt_actx, int type)\r
-{\r
- translatea(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,SSH2_PKTCTX_GSSAPI);\r
- translatea(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,SSH2_PKTCTX_GSSAPI);\r
- translatea(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,SSH2_PKTCTX_GSSAPI);\r
- translatea(SSH2_MSG_USERAUTH_GSSAPI_ERROR,SSH2_PKTCTX_GSSAPI);\r
- translatea(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,SSH2_PKTCTX_GSSAPI);\r
- translatea(SSH2_MSG_USERAUTH_GSSAPI_MIC, SSH2_PKTCTX_GSSAPI);\r
- translate(SSH2_MSG_DISCONNECT);\r
- translate(SSH2_MSG_IGNORE);\r
- translate(SSH2_MSG_UNIMPLEMENTED);\r
- translate(SSH2_MSG_DEBUG);\r
- translate(SSH2_MSG_SERVICE_REQUEST);\r
- translate(SSH2_MSG_SERVICE_ACCEPT);\r
- translate(SSH2_MSG_KEXINIT);\r
- translate(SSH2_MSG_NEWKEYS);\r
- translatek(SSH2_MSG_KEXDH_INIT, SSH2_PKTCTX_DHGROUP);\r
- translatek(SSH2_MSG_KEXDH_REPLY, SSH2_PKTCTX_DHGROUP);\r
- translatek(SSH2_MSG_KEX_DH_GEX_REQUEST, SSH2_PKTCTX_DHGEX);\r
- translatek(SSH2_MSG_KEX_DH_GEX_GROUP, SSH2_PKTCTX_DHGEX);\r
- translatek(SSH2_MSG_KEX_DH_GEX_INIT, SSH2_PKTCTX_DHGEX);\r
- translatek(SSH2_MSG_KEX_DH_GEX_REPLY, SSH2_PKTCTX_DHGEX);\r
- translatek(SSH2_MSG_KEXRSA_PUBKEY, SSH2_PKTCTX_RSAKEX);\r
- translatek(SSH2_MSG_KEXRSA_SECRET, SSH2_PKTCTX_RSAKEX);\r
- translatek(SSH2_MSG_KEXRSA_DONE, SSH2_PKTCTX_RSAKEX);\r
- translate(SSH2_MSG_USERAUTH_REQUEST);\r
- translate(SSH2_MSG_USERAUTH_FAILURE);\r
- translate(SSH2_MSG_USERAUTH_SUCCESS);\r
- translate(SSH2_MSG_USERAUTH_BANNER);\r
- translatea(SSH2_MSG_USERAUTH_PK_OK, SSH2_PKTCTX_PUBLICKEY);\r
- translatea(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, SSH2_PKTCTX_PASSWORD);\r
- translatea(SSH2_MSG_USERAUTH_INFO_REQUEST, SSH2_PKTCTX_KBDINTER);\r
- translatea(SSH2_MSG_USERAUTH_INFO_RESPONSE, SSH2_PKTCTX_KBDINTER);\r
- translate(SSH2_MSG_GLOBAL_REQUEST);\r
- translate(SSH2_MSG_REQUEST_SUCCESS);\r
- translate(SSH2_MSG_REQUEST_FAILURE);\r
- translate(SSH2_MSG_CHANNEL_OPEN);\r
- translate(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);\r
- translate(SSH2_MSG_CHANNEL_OPEN_FAILURE);\r
- translate(SSH2_MSG_CHANNEL_WINDOW_ADJUST);\r
- translate(SSH2_MSG_CHANNEL_DATA);\r
- translate(SSH2_MSG_CHANNEL_EXTENDED_DATA);\r
- translate(SSH2_MSG_CHANNEL_EOF);\r
- translate(SSH2_MSG_CHANNEL_CLOSE);\r
- translate(SSH2_MSG_CHANNEL_REQUEST);\r
- translate(SSH2_MSG_CHANNEL_SUCCESS);\r
- translate(SSH2_MSG_CHANNEL_FAILURE);\r
- return "unknown";\r
-}\r
-#undef translate\r
-#undef translatec\r
-\r
-/* Enumeration values for fields in SSH-1 packets */\r
-enum {\r
- PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM,\r
- /* These values are for communicating relevant semantics of\r
- * fields to the packet logging code. */\r
- PKTT_OTHER, PKTT_PASSWORD, PKTT_DATA\r
-};\r
-\r
-/*\r
- * Coroutine mechanics for the sillier bits of the code. If these\r
- * macros look impenetrable to you, you might find it helpful to\r
- * read\r
- * \r
- * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\r
- * \r
- * which explains the theory behind these macros.\r
- * \r
- * In particular, if you are getting `case expression not constant'\r
- * errors when building with MS Visual Studio, this is because MS's\r
- * Edit and Continue debugging feature causes their compiler to\r
- * violate ANSI C. To disable Edit and Continue debugging:\r
- * \r
- * - right-click ssh.c in the FileView\r
- * - click Settings\r
- * - select the C/C++ tab and the General category\r
- * - under `Debug info:', select anything _other_ than `Program\r
- * Database for Edit and Continue'.\r
- */\r
-#define crBegin(v) { int *crLine = &v; switch(v) { case 0:;\r
-#define crState(t) \\r
- struct t *s; \\r
- if (!ssh->t) ssh->t = snew(struct t); \\r
- s = ssh->t;\r
-#define crFinish(z) } *crLine = 0; return (z); }\r
-#define crFinishV } *crLine = 0; return; }\r
-#define crReturn(z) \\r
- do {\\r
- *crLine =__LINE__; return (z); case __LINE__:;\\r
- } while (0)\r
-#define crReturnV \\r
- do {\\r
- *crLine=__LINE__; return; case __LINE__:;\\r
- } while (0)\r
-#define crStop(z) do{ *crLine = 0; return (z); }while(0)\r
-#define crStopV do{ *crLine = 0; return; }while(0)\r
-#define crWaitUntil(c) do { crReturn(0); } while (!(c))\r
-#define crWaitUntilV(c) do { crReturnV; } while (!(c))\r
-\r
-typedef struct ssh_tag *Ssh;\r
-struct Packet;\r
-\r
-static struct Packet *ssh1_pkt_init(int pkt_type);\r
-static struct Packet *ssh2_pkt_init(int pkt_type);\r
-static void ssh_pkt_ensure(struct Packet *, int length);\r
-static void ssh_pkt_adddata(struct Packet *, void *data, int len);\r
-static void ssh_pkt_addbyte(struct Packet *, unsigned char value);\r
-static void ssh2_pkt_addbool(struct Packet *, unsigned char value);\r
-static void ssh_pkt_adduint32(struct Packet *, unsigned long value);\r
-static void ssh_pkt_addstring_start(struct Packet *);\r
-static void ssh_pkt_addstring_str(struct Packet *, char *data);\r
-static void ssh_pkt_addstring_data(struct Packet *, char *data, int len);\r
-static void ssh_pkt_addstring(struct Packet *, char *data);\r
-static unsigned char *ssh2_mpint_fmt(Bignum b, int *len);\r
-static void ssh1_pkt_addmp(struct Packet *, Bignum b);\r
-static void ssh2_pkt_addmp(struct Packet *, Bignum b);\r
-static int ssh2_pkt_construct(Ssh, struct Packet *);\r
-static void ssh2_pkt_send(Ssh, struct Packet *);\r
-static void ssh2_pkt_send_noqueue(Ssh, struct Packet *);\r
-static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,\r
- struct Packet *pktin);\r
-static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,\r
- struct Packet *pktin);\r
-\r
-/*\r
- * Buffer management constants. There are several of these for\r
- * various different purposes:\r
- * \r
- * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up\r
- * on a local data stream before we throttle the whole SSH\r
- * connection (in SSH-1 only). Throttling the whole connection is\r
- * pretty drastic so we set this high in the hope it won't\r
- * happen very often.\r
- * \r
- * - SSH_MAX_BACKLOG is the amount of backlog that must build up\r
- * on the SSH connection itself before we defensively throttle\r
- * _all_ local data streams. This is pretty drastic too (though\r
- * thankfully unlikely in SSH-2 since the window mechanism should\r
- * ensure that the server never has any need to throttle its end\r
- * of the connection), so we set this high as well.\r
- * \r
- * - OUR_V2_WINSIZE is the maximum window size we present on SSH-2\r
- * channels.\r
- *\r
- * - OUR_V2_BIGWIN is the window size we advertise for the only\r
- * channel in a simple connection. It must be <= INT_MAX.\r
- *\r
- * - OUR_V2_MAXPKT is the official "maximum packet size" we send\r
- * to the remote side. This actually has nothing to do with the\r
- * size of the _packet_, but is instead a limit on the amount\r
- * of data we're willing to receive in a single SSH2 channel\r
- * data message.\r
- *\r
- * - OUR_V2_PACKETLIMIT is actually the maximum size of SSH\r
- * _packet_ we're prepared to cope with. It must be a multiple\r
- * of the cipher block size, and must be at least 35000.\r
- */\r
-\r
-#define SSH1_BUFFER_LIMIT 32768\r
-#define SSH_MAX_BACKLOG 32768\r
-#define OUR_V2_WINSIZE 16384\r
-#define OUR_V2_BIGWIN 0x7fffffff\r
-#define OUR_V2_MAXPKT 0x4000UL\r
-#define OUR_V2_PACKETLIMIT 0x9000UL\r
-\r
-/* Maximum length of passwords/passphrases (arbitrary) */\r
-#define SSH_MAX_PASSWORD_LEN 100\r
-\r
-const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss };\r
-\r
-const static struct ssh_mac *macs[] = {\r
- &ssh_hmac_sha1, &ssh_hmac_sha1_96, &ssh_hmac_md5\r
-};\r
-const static struct ssh_mac *buggymacs[] = {\r
- &ssh_hmac_sha1_buggy, &ssh_hmac_sha1_96_buggy, &ssh_hmac_md5\r
-};\r
-\r
-static void *ssh_comp_none_init(void)\r
-{\r
- return NULL;\r
-}\r
-static void ssh_comp_none_cleanup(void *handle)\r
-{\r
-}\r
-static int ssh_comp_none_block(void *handle, unsigned char *block, int len,\r
- unsigned char **outblock, int *outlen)\r
-{\r
- return 0;\r
-}\r
-static int ssh_comp_none_disable(void *handle)\r
-{\r
- return 0;\r
-}\r
-const static struct ssh_compress ssh_comp_none = {\r
- "none", NULL,\r
- ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block,\r
- ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block,\r
- ssh_comp_none_disable, NULL\r
-};\r
-extern const struct ssh_compress ssh_zlib;\r
-const static struct ssh_compress *compressions[] = {\r
- &ssh_zlib, &ssh_comp_none\r
-};\r
-\r
-enum { /* channel types */\r
- CHAN_MAINSESSION,\r
- CHAN_X11,\r
- CHAN_AGENT,\r
- CHAN_SOCKDATA,\r
- CHAN_SOCKDATA_DORMANT /* one the remote hasn't confirmed */\r
-};\r
-\r
-/*\r
- * little structure to keep track of outstanding WINDOW_ADJUSTs\r
- */\r
-struct winadj {\r
- struct winadj *next;\r
- unsigned size;\r
-};\r
-\r
-/*\r
- * 2-3-4 tree storing channels.\r
- */\r
-struct ssh_channel {\r
- Ssh ssh; /* pointer back to main context */\r
- unsigned remoteid, localid;\r
- int type;\r
- /* True if we opened this channel but server hasn't confirmed. */\r
- int halfopen;\r
- /*\r
- * In SSH-1, this value contains four bits:\r
- * \r
- * 1 We have sent SSH1_MSG_CHANNEL_CLOSE.\r
- * 2 We have sent SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION.\r
- * 4 We have received SSH1_MSG_CHANNEL_CLOSE.\r
- * 8 We have received SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION.\r
- * \r
- * A channel is completely finished with when all four bits are set.\r
- */\r
- int closes;\r
-\r
- /*\r
- * This flag indicates that a close is pending on the outgoing\r
- * side of the channel: that is, wherever we're getting the data\r
- * for this channel has sent us some data followed by EOF. We\r
- * can't actually close the channel until we've finished sending\r
- * the data, so we set this flag instead to remind us to\r
- * initiate the closing process once our buffer is clear.\r
- */\r
- int pending_close;\r
-\r
- /*\r
- * True if this channel is causing the underlying connection to be\r
- * throttled.\r
- */\r
- int throttling_conn;\r
- union {\r
- struct ssh2_data_channel {\r
- bufchain outbuffer;\r
- unsigned remwindow, remmaxpkt;\r
- /* locwindow is signed so we can cope with excess data. */\r
- int locwindow, locmaxwin;\r
- /*\r
- * remlocwin is the amount of local window that we think\r
- * the remote end had available to it after it sent the\r
- * last data packet or window adjust ack.\r
- */\r
- int remlocwin;\r
- /*\r
- * These store the list of window adjusts that haven't\r
- * been acked.\r
- */\r
- struct winadj *winadj_head, *winadj_tail;\r
- enum { THROTTLED, UNTHROTTLING, UNTHROTTLED } throttle_state;\r
- } v2;\r
- } v;\r
- union {\r
- struct ssh_agent_channel {\r
- unsigned char *message;\r
- unsigned char msglen[4];\r
- unsigned lensofar, totallen;\r
- } a;\r
- struct ssh_x11_channel {\r
- Socket s;\r
- } x11;\r
- struct ssh_pfd_channel {\r
- Socket s;\r
- } pfd;\r
- } u;\r
-};\r
-\r
-/*\r
- * 2-3-4 tree storing remote->local port forwardings. SSH-1 and SSH-2\r
- * use this structure in different ways, reflecting SSH-2's\r
- * altogether saner approach to port forwarding.\r
- * \r
- * In SSH-1, you arrange a remote forwarding by sending the server\r
- * the remote port number, and the local destination host:port.\r
- * When a connection comes in, the server sends you back that\r
- * host:port pair, and you connect to it. This is a ready-made\r
- * security hole if you're not on the ball: a malicious server\r
- * could send you back _any_ host:port pair, so if you trustingly\r
- * connect to the address it gives you then you've just opened the\r
- * entire inside of your corporate network just by connecting\r
- * through it to a dodgy SSH server. Hence, we must store a list of\r
- * host:port pairs we _are_ trying to forward to, and reject a\r
- * connection request from the server if it's not in the list.\r
- * \r
- * In SSH-2, each side of the connection minds its own business and\r
- * doesn't send unnecessary information to the other. You arrange a\r
- * remote forwarding by sending the server just the remote port\r
- * number. When a connection comes in, the server tells you which\r
- * of its ports was connected to; and _you_ have to remember what\r
- * local host:port pair went with that port number.\r
- * \r
- * Hence, in SSH-1 this structure is indexed by destination\r
- * host:port pair, whereas in SSH-2 it is indexed by source port.\r
- */\r
-struct ssh_portfwd; /* forward declaration */\r
-\r
-struct ssh_rportfwd {\r
- unsigned sport, dport;\r
- char dhost[256];\r
- char *sportdesc;\r
- struct ssh_portfwd *pfrec;\r
-};\r
-#define free_rportfwd(pf) ( \\r
- ((pf) ? (sfree((pf)->sportdesc)) : (void)0 ), sfree(pf) )\r
-\r
-/*\r
- * Separately to the rportfwd tree (which is for looking up port\r
- * open requests from the server), a tree of _these_ structures is\r
- * used to keep track of all the currently open port forwardings,\r
- * so that we can reconfigure in mid-session if the user requests\r
- * it.\r
- */\r
-struct ssh_portfwd {\r
- enum { DESTROY, KEEP, CREATE } status;\r
- int type;\r
- unsigned sport, dport;\r
- char *saddr, *daddr;\r
- char *sserv, *dserv;\r
- struct ssh_rportfwd *remote;\r
- int addressfamily;\r
- void *local;\r
-};\r
-#define free_portfwd(pf) ( \\r
- ((pf) ? (sfree((pf)->saddr), sfree((pf)->daddr), \\r
- sfree((pf)->sserv), sfree((pf)->dserv)) : (void)0 ), sfree(pf) )\r
-\r
-struct Packet {\r
- long length; /* length of `data' actually used */\r
- long forcepad; /* SSH-2: force padding to at least this length */\r
- int type; /* only used for incoming packets */\r
- unsigned long sequence; /* SSH-2 incoming sequence number */\r
- unsigned char *data; /* allocated storage */\r
- unsigned char *body; /* offset of payload within `data' */\r
- long savedpos; /* temporary index into `data' (for strings) */\r
- long maxlen; /* amount of storage allocated for `data' */\r
- long encrypted_len; /* for SSH-2 total-size counting */\r
-\r
- /*\r
- * State associated with packet logging\r
- */\r
- int logmode;\r
- int nblanks;\r
- struct logblank_t *blanks;\r
-};\r
-\r
-static void ssh1_protocol(Ssh ssh, void *vin, int inlen,\r
- struct Packet *pktin);\r
-static void ssh2_protocol(Ssh ssh, void *vin, int inlen,\r
- struct Packet *pktin);\r
-static void ssh1_protocol_setup(Ssh ssh);\r
-static void ssh2_protocol_setup(Ssh ssh);\r
-static void ssh_size(void *handle, int width, int height);\r
-static void ssh_special(void *handle, Telnet_Special);\r
-static int ssh2_try_send(struct ssh_channel *c);\r
-static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len);\r
-static void ssh_throttle_all(Ssh ssh, int enable, int bufsize);\r
-static void ssh2_set_window(struct ssh_channel *c, int newwin);\r
-static int ssh_sendbuffer(void *handle);\r
-static int ssh_do_close(Ssh ssh, int notify_exit);\r
-static unsigned long ssh_pkt_getuint32(struct Packet *pkt);\r
-static int ssh2_pkt_getbool(struct Packet *pkt);\r
-static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length);\r
-static void ssh2_timer(void *ctx, long now);\r
-static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,\r
- struct Packet *pktin);\r
-\r
-struct rdpkt1_state_tag {\r
- long len, pad, biglen, to_read;\r
- unsigned long realcrc, gotcrc;\r
- unsigned char *p;\r
- int i;\r
- int chunk;\r
- struct Packet *pktin;\r
-};\r
-\r
-struct rdpkt2_state_tag {\r
- long len, pad, payload, packetlen, maclen;\r
- int i;\r
- int cipherblk;\r
- unsigned long incoming_sequence;\r
- struct Packet *pktin;\r
-};\r
-\r
-typedef void (*handler_fn_t)(Ssh ssh, struct Packet *pktin);\r
-typedef void (*chandler_fn_t)(Ssh ssh, struct Packet *pktin, void *ctx);\r
-\r
-struct queued_handler;\r
-struct queued_handler {\r
- int msg1, msg2;\r
- chandler_fn_t handler;\r
- void *ctx;\r
- struct queued_handler *next;\r
-};\r
-\r
-struct ssh_tag {\r
- const struct plug_function_table *fn;\r
- /* the above field _must_ be first in the structure */\r
-\r
- char *v_c, *v_s;\r
- void *exhash;\r
-\r
- Socket s;\r
-\r
- void *ldisc;\r
- void *logctx;\r
-\r
- unsigned char session_key[32];\r
- int v1_compressing;\r
- int v1_remote_protoflags;\r
- int v1_local_protoflags;\r
- int agentfwd_enabled;\r
- int X11_fwd_enabled;\r
- int remote_bugs;\r
- const struct ssh_cipher *cipher;\r
- void *v1_cipher_ctx;\r
- void *crcda_ctx;\r
- const struct ssh2_cipher *cscipher, *sccipher;\r
- void *cs_cipher_ctx, *sc_cipher_ctx;\r
- const struct ssh_mac *csmac, *scmac;\r
- void *cs_mac_ctx, *sc_mac_ctx;\r
- const struct ssh_compress *cscomp, *sccomp;\r
- void *cs_comp_ctx, *sc_comp_ctx;\r
- const struct ssh_kex *kex;\r
- const struct ssh_signkey *hostkey;\r
- unsigned char v2_session_id[SSH2_KEX_MAX_HASH_LEN];\r
- int v2_session_id_len;\r
- void *kex_ctx;\r
-\r
- char *savedhost;\r
- int savedport;\r
- int send_ok;\r
- int echoing, editing;\r
-\r
- void *frontend;\r
-\r
- int ospeed, ispeed; /* temporaries */\r
- int term_width, term_height;\r
-\r
- tree234 *channels; /* indexed by local id */\r
- struct ssh_channel *mainchan; /* primary session channel */\r
- int ncmode; /* is primary channel direct-tcpip? */\r
- int exitcode;\r
- int close_expected;\r
- int clean_exit;\r
-\r
- tree234 *rportfwds, *portfwds;\r
-\r
- enum {\r
- SSH_STATE_PREPACKET,\r
- SSH_STATE_BEFORE_SIZE,\r
- SSH_STATE_INTERMED,\r
- SSH_STATE_SESSION,\r
- SSH_STATE_CLOSED\r
- } state;\r
-\r
- int size_needed, eof_needed;\r
-\r
- struct Packet **queue;\r
- int queuelen, queuesize;\r
- int queueing;\r
- unsigned char *deferred_send_data;\r
- int deferred_len, deferred_size;\r
-\r
- /*\r
- * Gross hack: pscp will try to start SFTP but fall back to\r
- * scp1 if that fails. This variable is the means by which\r
- * scp.c can reach into the SSH code and find out which one it\r
- * got.\r
- */\r
- int fallback_cmd;\r
-\r
- bufchain banner; /* accumulates banners during do_ssh2_authconn */\r
-\r
- Pkt_KCtx pkt_kctx;\r
- Pkt_ACtx pkt_actx;\r
-\r
- struct X11Display *x11disp;\r
-\r
- int version;\r
- int conn_throttle_count;\r
- int overall_bufsize;\r
- int throttled_all;\r
- int v1_stdout_throttling;\r
- unsigned long v2_outgoing_sequence;\r
-\r
- int ssh1_rdpkt_crstate;\r
- int ssh2_rdpkt_crstate;\r
- int do_ssh_init_crstate;\r
- int ssh_gotdata_crstate;\r
- int do_ssh1_login_crstate;\r
- int do_ssh1_connection_crstate;\r
- int do_ssh2_transport_crstate;\r
- int do_ssh2_authconn_crstate;\r
-\r
- void *do_ssh_init_state;\r
- void *do_ssh1_login_state;\r
- void *do_ssh2_transport_state;\r
- void *do_ssh2_authconn_state;\r
-\r
- struct rdpkt1_state_tag rdpkt1_state;\r
- struct rdpkt2_state_tag rdpkt2_state;\r
-\r
- /* SSH-1 and SSH-2 use this for different things, but both use it */\r
- int protocol_initial_phase_done;\r
-\r
- void (*protocol) (Ssh ssh, void *vin, int inlen,\r
- struct Packet *pkt);\r
- struct Packet *(*s_rdpkt) (Ssh ssh, unsigned char **data, int *datalen);\r
-\r
- /*\r
- * We maintain a full _copy_ of a Config structure here, not\r
- * merely a pointer to it. That way, when we're passed a new\r
- * one for reconfiguration, we can check the differences and\r
- * potentially reconfigure port forwardings etc in mid-session.\r
- */\r
- Config cfg;\r
-\r
- /*\r
- * Used to transfer data back from async callbacks.\r
- */\r
- void *agent_response;\r
- int agent_response_len;\r
- int user_response;\r
-\r
- /*\r
- * The SSH connection can be set as `frozen', meaning we are\r
- * not currently accepting incoming data from the network. This\r
- * is slightly more serious than setting the _socket_ as\r
- * frozen, because we may already have had data passed to us\r
- * from the network which we need to delay processing until\r
- * after the freeze is lifted, so we also need a bufchain to\r
- * store that data.\r
- */\r
- int frozen;\r
- bufchain queued_incoming_data;\r
-\r
- /*\r
- * Dispatch table for packet types that we may have to deal\r
- * with at any time.\r
- */\r
- handler_fn_t packet_dispatch[256];\r
-\r
- /*\r
- * Queues of one-off handler functions for success/failure\r
- * indications from a request.\r
- */\r
- struct queued_handler *qhead, *qtail;\r
-\r
- /*\r
- * This module deals with sending keepalives.\r
- */\r
- Pinger pinger;\r
-\r
- /*\r
- * Track incoming and outgoing data sizes and time, for\r
- * size-based rekeys.\r
- */\r
- unsigned long incoming_data_size, outgoing_data_size, deferred_data_size;\r
- unsigned long max_data_size;\r
- int kex_in_progress;\r
- long next_rekey, last_rekey;\r
- char *deferred_rekey_reason; /* points to STATIC string; don't free */\r
-\r
- /*\r
- * Fully qualified host name, which we need if doing GSSAPI.\r
- */\r
- char *fullhostname;\r
-\r
-#ifndef NO_GSSAPI\r
- /*\r
- * GSSAPI libraries for this session.\r
- */\r
- struct ssh_gss_liblist *gsslibs;\r
-#endif\r
-};\r
-\r
-#define logevent(s) logevent(ssh->frontend, s)\r
-\r
-/* logevent, only printf-formatted. */\r
-static void logeventf(Ssh ssh, const char *fmt, ...)\r
-{\r
- va_list ap;\r
- char *buf;\r
-\r
- va_start(ap, fmt);\r
- buf = dupvprintf(fmt, ap);\r
- va_end(ap);\r
- logevent(buf);\r
- sfree(buf);\r
-}\r
-\r
-#define bombout(msg) \\r
- do { \\r
- char *text = dupprintf msg; \\r
- ssh_do_close(ssh, FALSE); \\r
- logevent(text); \\r
- connection_fatal(ssh->frontend, "%s", text); \\r
- sfree(text); \\r
- } while (0)\r
-\r
-/* Functions to leave bits out of the SSH packet log file. */\r
-\r
-static void dont_log_password(Ssh ssh, struct Packet *pkt, int blanktype)\r
-{\r
- if (ssh->cfg.logomitpass)\r
- pkt->logmode = blanktype;\r
-}\r
-\r
-static void dont_log_data(Ssh ssh, struct Packet *pkt, int blanktype)\r
-{\r
- if (ssh->cfg.logomitdata)\r
- pkt->logmode = blanktype;\r
-}\r
-\r
-static void end_log_omission(Ssh ssh, struct Packet *pkt)\r
-{\r
- pkt->logmode = PKTLOG_EMIT;\r
-}\r
-\r
-/* Helper function for common bits of parsing cfg.ttymodes. */\r
-static void parse_ttymodes(Ssh ssh, char *modes,\r
- void (*do_mode)(void *data, char *mode, char *val),\r
- void *data)\r
-{\r
- while (*modes) {\r
- char *t = strchr(modes, '\t');\r
- char *m = snewn(t-modes+1, char);\r
- char *val;\r
- strncpy(m, modes, t-modes);\r
- m[t-modes] = '\0';\r
- if (*(t+1) == 'A')\r
- val = get_ttymode(ssh->frontend, m);\r
- else\r
- val = dupstr(t+2);\r
- if (val)\r
- do_mode(data, m, val);\r
- sfree(m);\r
- sfree(val);\r
- modes += strlen(modes) + 1;\r
- }\r
-}\r
-\r
-static int ssh_channelcmp(void *av, void *bv)\r
-{\r
- struct ssh_channel *a = (struct ssh_channel *) av;\r
- struct ssh_channel *b = (struct ssh_channel *) bv;\r
- if (a->localid < b->localid)\r
- return -1;\r
- if (a->localid > b->localid)\r
- return +1;\r
- return 0;\r
-}\r
-static int ssh_channelfind(void *av, void *bv)\r
-{\r
- unsigned *a = (unsigned *) av;\r
- struct ssh_channel *b = (struct ssh_channel *) bv;\r
- if (*a < b->localid)\r
- return -1;\r
- if (*a > b->localid)\r
- return +1;\r
- return 0;\r
-}\r
-\r
-static int ssh_rportcmp_ssh1(void *av, void *bv)\r
-{\r
- struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;\r
- struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;\r
- int i;\r
- if ( (i = strcmp(a->dhost, b->dhost)) != 0)\r
- return i < 0 ? -1 : +1;\r
- if (a->dport > b->dport)\r
- return +1;\r
- if (a->dport < b->dport)\r
- return -1;\r
- return 0;\r
-}\r
-\r
-static int ssh_rportcmp_ssh2(void *av, void *bv)\r
-{\r
- struct ssh_rportfwd *a = (struct ssh_rportfwd *) av;\r
- struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv;\r
-\r
- if (a->sport > b->sport)\r
- return +1;\r
- if (a->sport < b->sport)\r
- return -1;\r
- return 0;\r
-}\r
-\r
-/*\r
- * Special form of strcmp which can cope with NULL inputs. NULL is\r
- * defined to sort before even the empty string.\r
- */\r
-static int nullstrcmp(const char *a, const char *b)\r
-{\r
- if (a == NULL && b == NULL)\r
- return 0;\r
- if (a == NULL)\r
- return -1;\r
- if (b == NULL)\r
- return +1;\r
- return strcmp(a, b);\r
-}\r
-\r
-static int ssh_portcmp(void *av, void *bv)\r
-{\r
- struct ssh_portfwd *a = (struct ssh_portfwd *) av;\r
- struct ssh_portfwd *b = (struct ssh_portfwd *) bv;\r
- int i;\r
- if (a->type > b->type)\r
- return +1;\r
- if (a->type < b->type)\r
- return -1;\r
- if (a->addressfamily > b->addressfamily)\r
- return +1;\r
- if (a->addressfamily < b->addressfamily)\r
- return -1;\r
- if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0)\r
- return i < 0 ? -1 : +1;\r
- if (a->sport > b->sport)\r
- return +1;\r
- if (a->sport < b->sport)\r
- return -1;\r
- if (a->type != 'D') {\r
- if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0)\r
- return i < 0 ? -1 : +1;\r
- if (a->dport > b->dport)\r
- return +1;\r
- if (a->dport < b->dport)\r
- return -1;\r
- }\r
- return 0;\r
-}\r
-\r
-static int alloc_channel_id(Ssh ssh)\r
-{\r
- const unsigned CHANNEL_NUMBER_OFFSET = 256;\r
- unsigned low, high, mid;\r
- int tsize;\r
- struct ssh_channel *c;\r
-\r
- /*\r
- * First-fit allocation of channel numbers: always pick the\r
- * lowest unused one. To do this, binary-search using the\r
- * counted B-tree to find the largest channel ID which is in a\r
- * contiguous sequence from the beginning. (Precisely\r
- * everything in that sequence must have ID equal to its tree\r
- * index plus CHANNEL_NUMBER_OFFSET.)\r
- */\r
- tsize = count234(ssh->channels);\r
-\r
- low = -1;\r
- high = tsize;\r
- while (high - low > 1) {\r
- mid = (high + low) / 2;\r
- c = index234(ssh->channels, mid);\r
- if (c->localid == mid + CHANNEL_NUMBER_OFFSET)\r
- low = mid; /* this one is fine */\r
- else\r
- high = mid; /* this one is past it */\r
- }\r
- /*\r
- * Now low points to either -1, or the tree index of the\r
- * largest ID in the initial sequence.\r
- */\r
- {\r
- unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET;\r
- assert(NULL == find234(ssh->channels, &i, ssh_channelfind));\r
- }\r
- return low + 1 + CHANNEL_NUMBER_OFFSET;\r
-}\r
-\r
-static void c_write_stderr(int trusted, const char *buf, int len)\r
-{\r
- int i;\r
- for (i = 0; i < len; i++)\r
- if (buf[i] != '\r' && (trusted || buf[i] == '\n' || (buf[i] & 0x60)))\r
- fputc(buf[i], stderr);\r
-}\r
-\r
-static void c_write(Ssh ssh, const char *buf, int len)\r
-{\r
- if (flags & FLAG_STDERR)\r
- c_write_stderr(1, buf, len);\r
- else\r
- from_backend(ssh->frontend, 1, buf, len);\r
-}\r
-\r
-static void c_write_untrusted(Ssh ssh, const char *buf, int len)\r
-{\r
- if (flags & FLAG_STDERR)\r
- c_write_stderr(0, buf, len);\r
- else\r
- from_backend_untrusted(ssh->frontend, buf, len);\r
-}\r
-\r
-static void c_write_str(Ssh ssh, const char *buf)\r
-{\r
- c_write(ssh, buf, strlen(buf));\r
-}\r
-\r
-static void ssh_free_packet(struct Packet *pkt)\r
-{\r
- sfree(pkt->data);\r
- sfree(pkt);\r
-}\r
-static struct Packet *ssh_new_packet(void)\r
-{\r
- struct Packet *pkt = snew(struct Packet);\r
-\r
- pkt->body = pkt->data = NULL;\r
- pkt->maxlen = 0;\r
- pkt->logmode = PKTLOG_EMIT;\r
- pkt->nblanks = 0;\r
- pkt->blanks = NULL;\r
-\r
- return pkt;\r
-}\r
-\r
-/*\r
- * Collect incoming data in the incoming packet buffer.\r
- * Decipher and verify the packet when it is completely read.\r
- * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets.\r
- * Update the *data and *datalen variables.\r
- * Return a Packet structure when a packet is completed.\r
- */\r
-static struct Packet *ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen)\r
-{\r
- struct rdpkt1_state_tag *st = &ssh->rdpkt1_state;\r
-\r
- crBegin(ssh->ssh1_rdpkt_crstate);\r
-\r
- st->pktin = ssh_new_packet();\r
-\r
- st->pktin->type = 0;\r
- st->pktin->length = 0;\r
-\r
- for (st->i = st->len = 0; st->i < 4; st->i++) {\r
- while ((*datalen) == 0)\r
- crReturn(NULL);\r
- st->len = (st->len << 8) + **data;\r
- (*data)++, (*datalen)--;\r
- }\r
-\r
- st->pad = 8 - (st->len % 8);\r
- st->biglen = st->len + st->pad;\r
- st->pktin->length = st->len - 5;\r
-\r
- if (st->biglen < 0) {\r
- bombout(("Extremely large packet length from server suggests"\r
- " data stream corruption"));\r
- ssh_free_packet(st->pktin);\r
- crStop(NULL);\r
- }\r
-\r
- st->pktin->maxlen = st->biglen;\r
- st->pktin->data = snewn(st->biglen + APIEXTRA, unsigned char);\r
-\r
- st->to_read = st->biglen;\r
- st->p = st->pktin->data;\r
- while (st->to_read > 0) {\r
- st->chunk = st->to_read;\r
- while ((*datalen) == 0)\r
- crReturn(NULL);\r
- if (st->chunk > (*datalen))\r
- st->chunk = (*datalen);\r
- memcpy(st->p, *data, st->chunk);\r
- *data += st->chunk;\r
- *datalen -= st->chunk;\r
- st->p += st->chunk;\r
- st->to_read -= st->chunk;\r
- }\r
-\r
- if (ssh->cipher && detect_attack(ssh->crcda_ctx, st->pktin->data,\r
- st->biglen, NULL)) {\r
- bombout(("Network attack (CRC compensation) detected!"));\r
- ssh_free_packet(st->pktin);\r
- crStop(NULL);\r
- }\r
-\r
- if (ssh->cipher)\r
- ssh->cipher->decrypt(ssh->v1_cipher_ctx, st->pktin->data, st->biglen);\r
-\r
- st->realcrc = crc32_compute(st->pktin->data, st->biglen - 4);\r
- st->gotcrc = GET_32BIT(st->pktin->data + st->biglen - 4);\r
- if (st->gotcrc != st->realcrc) {\r
- bombout(("Incorrect CRC received on packet"));\r
- ssh_free_packet(st->pktin);\r
- crStop(NULL);\r
- }\r
-\r
- st->pktin->body = st->pktin->data + st->pad + 1;\r
- st->pktin->savedpos = 0;\r
-\r
- if (ssh->v1_compressing) {\r
- unsigned char *decompblk;\r
- int decomplen;\r
- if (!zlib_decompress_block(ssh->sc_comp_ctx,\r
- st->pktin->body - 1, st->pktin->length + 1,\r
- &decompblk, &decomplen)) {\r
- bombout(("Zlib decompression encountered invalid data"));\r
- ssh_free_packet(st->pktin);\r
- crStop(NULL);\r
- }\r
-\r
- if (st->pktin->maxlen < st->pad + decomplen) {\r
- st->pktin->maxlen = st->pad + decomplen;\r
- st->pktin->data = sresize(st->pktin->data,\r
- st->pktin->maxlen + APIEXTRA,\r
- unsigned char);\r
- st->pktin->body = st->pktin->data + st->pad + 1;\r
- }\r
-\r
- memcpy(st->pktin->body - 1, decompblk, decomplen);\r
- sfree(decompblk);\r
- st->pktin->length = decomplen - 1;\r
- }\r
-\r
- st->pktin->type = st->pktin->body[-1];\r
-\r
- /*\r
- * Log incoming packet, possibly omitting sensitive fields.\r
- */\r
- if (ssh->logctx) {\r
- int nblanks = 0;\r
- struct logblank_t blank;\r
- if (ssh->cfg.logomitdata) {\r
- int do_blank = FALSE, blank_prefix = 0;\r
- /* "Session data" packets - omit the data field */\r
- if ((st->pktin->type == SSH1_SMSG_STDOUT_DATA) ||\r
- (st->pktin->type == SSH1_SMSG_STDERR_DATA)) {\r
- do_blank = TRUE; blank_prefix = 4;\r
- } else if (st->pktin->type == SSH1_MSG_CHANNEL_DATA) {\r
- do_blank = TRUE; blank_prefix = 8;\r
- }\r
- if (do_blank) {\r
- blank.offset = blank_prefix;\r
- blank.len = st->pktin->length;\r
- blank.type = PKTLOG_OMIT;\r
- nblanks = 1;\r
- }\r
- }\r
- log_packet(ssh->logctx,\r
- PKT_INCOMING, st->pktin->type,\r
- ssh1_pkt_type(st->pktin->type),\r
- st->pktin->body, st->pktin->length,\r
- nblanks, &blank, NULL);\r
- }\r
-\r
- crFinish(st->pktin);\r
-}\r
-\r
-static struct Packet *ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen)\r
-{\r
- struct rdpkt2_state_tag *st = &ssh->rdpkt2_state;\r
-\r
- crBegin(ssh->ssh2_rdpkt_crstate);\r
-\r
- st->pktin = ssh_new_packet();\r
-\r
- st->pktin->type = 0;\r
- st->pktin->length = 0;\r
- if (ssh->sccipher)\r
- st->cipherblk = ssh->sccipher->blksize;\r
- else\r
- st->cipherblk = 8;\r
- if (st->cipherblk < 8)\r
- st->cipherblk = 8;\r
- st->maclen = ssh->scmac ? ssh->scmac->len : 0;\r
-\r
- if (ssh->sccipher && (ssh->sccipher->flags & SSH_CIPHER_IS_CBC) &&\r
- ssh->scmac) {\r
- /*\r
- * When dealing with a CBC-mode cipher, we want to avoid the\r
- * possibility of an attacker's tweaking the ciphertext stream\r
- * so as to cause us to feed the same block to the block\r
- * cipher more than once and thus leak information\r
- * (VU#958563). The way we do this is not to take any\r
- * decisions on the basis of anything we've decrypted until\r
- * we've verified it with a MAC. That includes the packet\r
- * length, so we just read data and check the MAC repeatedly,\r
- * and when the MAC passes, see if the length we've got is\r
- * plausible.\r
- */\r
-\r
- /* May as well allocate the whole lot now. */\r
- st->pktin->data = snewn(OUR_V2_PACKETLIMIT + st->maclen + APIEXTRA,\r
- unsigned char);\r
-\r
- /* Read an amount corresponding to the MAC. */\r
- for (st->i = 0; st->i < st->maclen; st->i++) {\r
- while ((*datalen) == 0)\r
- crReturn(NULL);\r
- st->pktin->data[st->i] = *(*data)++;\r
- (*datalen)--;\r
- }\r
-\r
- st->packetlen = 0;\r
- {\r
- unsigned char seq[4];\r
- ssh->scmac->start(ssh->sc_mac_ctx);\r
- PUT_32BIT(seq, st->incoming_sequence);\r
- ssh->scmac->bytes(ssh->sc_mac_ctx, seq, 4);\r
- }\r
-\r
- for (;;) { /* Once around this loop per cipher block. */\r
- /* Read another cipher-block's worth, and tack it onto the end. */\r
- for (st->i = 0; st->i < st->cipherblk; st->i++) {\r
- while ((*datalen) == 0)\r
- crReturn(NULL);\r
- st->pktin->data[st->packetlen+st->maclen+st->i] = *(*data)++;\r
- (*datalen)--;\r
- }\r
- /* Decrypt one more block (a little further back in the stream). */\r
- ssh->sccipher->decrypt(ssh->sc_cipher_ctx,\r
- st->pktin->data + st->packetlen,\r
- st->cipherblk);\r
- /* Feed that block to the MAC. */\r
- ssh->scmac->bytes(ssh->sc_mac_ctx,\r
- st->pktin->data + st->packetlen, st->cipherblk);\r
- st->packetlen += st->cipherblk;\r
- /* See if that gives us a valid packet. */\r
- if (ssh->scmac->verresult(ssh->sc_mac_ctx,\r
- st->pktin->data + st->packetlen) &&\r
- (st->len = GET_32BIT(st->pktin->data)) + 4 == st->packetlen)\r
- break;\r
- if (st->packetlen >= OUR_V2_PACKETLIMIT) {\r
- bombout(("No valid incoming packet found"));\r
- ssh_free_packet(st->pktin);\r
- crStop(NULL);\r
- } \r
- }\r
- st->pktin->maxlen = st->packetlen + st->maclen;\r
- st->pktin->data = sresize(st->pktin->data,\r
- st->pktin->maxlen + APIEXTRA,\r
- unsigned char);\r
- } else {\r
- st->pktin->data = snewn(st->cipherblk + APIEXTRA, unsigned char);\r
-\r
- /*\r
- * Acquire and decrypt the first block of the packet. This will\r
- * contain the length and padding details.\r
- */\r
- for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) {\r
- while ((*datalen) == 0)\r
- crReturn(NULL);\r
- st->pktin->data[st->i] = *(*data)++;\r
- (*datalen)--;\r
- }\r
-\r
- if (ssh->sccipher)\r
- ssh->sccipher->decrypt(ssh->sc_cipher_ctx,\r
- st->pktin->data, st->cipherblk);\r
-\r
- /*\r
- * Now get the length figure.\r
- */\r
- st->len = GET_32BIT(st->pktin->data);\r
-\r
- /*\r
- * _Completely_ silly lengths should be stomped on before they\r
- * do us any more damage.\r
- */\r
- if (st->len < 0 || st->len > OUR_V2_PACKETLIMIT ||\r
- (st->len + 4) % st->cipherblk != 0) {\r
- bombout(("Incoming packet was garbled on decryption"));\r
- ssh_free_packet(st->pktin);\r
- crStop(NULL);\r
- }\r
-\r
- /*\r
- * So now we can work out the total packet length.\r
- */\r
- st->packetlen = st->len + 4;\r
-\r
- /*\r
- * Allocate memory for the rest of the packet.\r
- */\r
- st->pktin->maxlen = st->packetlen + st->maclen;\r
- st->pktin->data = sresize(st->pktin->data,\r
- st->pktin->maxlen + APIEXTRA,\r
- unsigned char);\r
-\r
- /*\r
- * Read and decrypt the remainder of the packet.\r
- */\r
- for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen;\r
- st->i++) {\r
- while ((*datalen) == 0)\r
- crReturn(NULL);\r
- st->pktin->data[st->i] = *(*data)++;\r
- (*datalen)--;\r
- }\r
- /* Decrypt everything _except_ the MAC. */\r
- if (ssh->sccipher)\r
- ssh->sccipher->decrypt(ssh->sc_cipher_ctx,\r
- st->pktin->data + st->cipherblk,\r
- st->packetlen - st->cipherblk);\r
-\r
- /*\r
- * Check the MAC.\r
- */\r
- if (ssh->scmac\r
- && !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data,\r
- st->len + 4, st->incoming_sequence)) {\r
- bombout(("Incorrect MAC received on packet"));\r
- ssh_free_packet(st->pktin);\r
- crStop(NULL);\r
- }\r
- }\r
- /* Get and sanity-check the amount of random padding. */\r
- st->pad = st->pktin->data[4];\r
- if (st->pad < 4 || st->len - st->pad < 1) {\r
- bombout(("Invalid padding length on received packet"));\r
- ssh_free_packet(st->pktin);\r
- crStop(NULL);\r
- }\r
- /*\r
- * This enables us to deduce the payload length.\r
- */\r
- st->payload = st->len - st->pad - 1;\r
-\r
- st->pktin->length = st->payload + 5;\r
- st->pktin->encrypted_len = st->packetlen;\r
-\r
- st->pktin->sequence = st->incoming_sequence++;\r
-\r
- /*\r
- * Decompress packet payload.\r
- */\r
- {\r
- unsigned char *newpayload;\r
- int newlen;\r
- if (ssh->sccomp &&\r
- ssh->sccomp->decompress(ssh->sc_comp_ctx,\r
- st->pktin->data + 5, st->pktin->length - 5,\r
- &newpayload, &newlen)) {\r
- if (st->pktin->maxlen < newlen + 5) {\r
- st->pktin->maxlen = newlen + 5;\r
- st->pktin->data = sresize(st->pktin->data,\r
- st->pktin->maxlen + APIEXTRA,\r
- unsigned char);\r
- }\r
- st->pktin->length = 5 + newlen;\r
- memcpy(st->pktin->data + 5, newpayload, newlen);\r
- sfree(newpayload);\r
- }\r
- }\r
-\r
- st->pktin->savedpos = 6;\r
- st->pktin->body = st->pktin->data;\r
- st->pktin->type = st->pktin->data[5];\r
-\r
- /*\r
- * Log incoming packet, possibly omitting sensitive fields.\r
- */\r
- if (ssh->logctx) {\r
- int nblanks = 0;\r
- struct logblank_t blank;\r
- if (ssh->cfg.logomitdata) {\r
- int do_blank = FALSE, blank_prefix = 0;\r
- /* "Session data" packets - omit the data field */\r
- if (st->pktin->type == SSH2_MSG_CHANNEL_DATA) {\r
- do_blank = TRUE; blank_prefix = 8;\r
- } else if (st->pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA) {\r
- do_blank = TRUE; blank_prefix = 12;\r
- }\r
- if (do_blank) {\r
- blank.offset = blank_prefix;\r
- blank.len = (st->pktin->length-6) - blank_prefix;\r
- blank.type = PKTLOG_OMIT;\r
- nblanks = 1;\r
- }\r
- }\r
- log_packet(ssh->logctx, PKT_INCOMING, st->pktin->type,\r
- ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx,\r
- st->pktin->type),\r
- st->pktin->data+6, st->pktin->length-6,\r
- nblanks, &blank, &st->pktin->sequence);\r
- }\r
-\r
- crFinish(st->pktin);\r
-}\r
-\r
-static int s_wrpkt_prepare(Ssh ssh, struct Packet *pkt, int *offset_p)\r
-{\r
- int pad, biglen, i, pktoffs;\r
- unsigned long crc;\r
-#ifdef __SC__\r
- /*\r
- * XXX various versions of SC (including 8.8.4) screw up the\r
- * register allocation in this function and use the same register\r
- * (D6) for len and as a temporary, with predictable results. The\r
- * following sledgehammer prevents this.\r
- */\r
- volatile\r
-#endif\r
- int len;\r
-\r
- if (ssh->logctx)\r
- log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[12],\r
- ssh1_pkt_type(pkt->data[12]),\r
- pkt->body, pkt->length - (pkt->body - pkt->data),\r
- pkt->nblanks, pkt->blanks, NULL);\r
- sfree(pkt->blanks); pkt->blanks = NULL;\r
- pkt->nblanks = 0;\r
-\r
- if (ssh->v1_compressing) {\r
- unsigned char *compblk;\r
- int complen;\r
- zlib_compress_block(ssh->cs_comp_ctx,\r
- pkt->data + 12, pkt->length - 12,\r
- &compblk, &complen);\r
- ssh_pkt_ensure(pkt, complen + 2); /* just in case it's got bigger */\r
- memcpy(pkt->data + 12, compblk, complen);\r
- sfree(compblk);\r
- pkt->length = complen + 12;\r
- }\r
-\r
- ssh_pkt_ensure(pkt, pkt->length + 4); /* space for CRC */\r
- pkt->length += 4;\r
- len = pkt->length - 4 - 8; /* len(type+data+CRC) */\r
- pad = 8 - (len % 8);\r
- pktoffs = 8 - pad;\r
- biglen = len + pad; /* len(padding+type+data+CRC) */\r
-\r
- for (i = pktoffs; i < 4+8; i++)\r
- pkt->data[i] = random_byte();\r
- crc = crc32_compute(pkt->data + pktoffs + 4, biglen - 4); /* all ex len */\r
- PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc);\r
- PUT_32BIT(pkt->data + pktoffs, len);\r
-\r
- if (ssh->cipher)\r
- ssh->cipher->encrypt(ssh->v1_cipher_ctx,\r
- pkt->data + pktoffs + 4, biglen);\r
-\r
- if (offset_p) *offset_p = pktoffs;\r
- return biglen + 4; /* len(length+padding+type+data+CRC) */\r
-}\r
-\r
-static int s_write(Ssh ssh, void *data, int len)\r
-{\r
- if (ssh->logctx)\r
- log_packet(ssh->logctx, PKT_OUTGOING, -1, NULL, data, len,\r
- 0, NULL, NULL);\r
- return sk_write(ssh->s, (char *)data, len);\r
-}\r
-\r
-static void s_wrpkt(Ssh ssh, struct Packet *pkt)\r
-{\r
- int len, backlog, offset;\r
- len = s_wrpkt_prepare(ssh, pkt, &offset);\r
- backlog = s_write(ssh, pkt->data + offset, len);\r
- if (backlog > SSH_MAX_BACKLOG)\r
- ssh_throttle_all(ssh, 1, backlog);\r
- ssh_free_packet(pkt);\r
-}\r
-\r
-static void s_wrpkt_defer(Ssh ssh, struct Packet *pkt)\r
-{\r
- int len, offset;\r
- len = s_wrpkt_prepare(ssh, pkt, &offset);\r
- if (ssh->deferred_len + len > ssh->deferred_size) {\r
- ssh->deferred_size = ssh->deferred_len + len + 128;\r
- ssh->deferred_send_data = sresize(ssh->deferred_send_data,\r
- ssh->deferred_size,\r
- unsigned char);\r
- }\r
- memcpy(ssh->deferred_send_data + ssh->deferred_len,\r
- pkt->data + offset, len);\r
- ssh->deferred_len += len;\r
- ssh_free_packet(pkt);\r
-}\r
-\r
-/*\r
- * Construct a SSH-1 packet with the specified contents.\r
- * (This all-at-once interface used to be the only one, but now SSH-1\r
- * packets can also be constructed incrementally.)\r
- */\r
-static struct Packet *construct_packet(Ssh ssh, int pkttype, va_list ap)\r
-{\r
- int argtype;\r
- Bignum bn;\r
- struct Packet *pkt;\r
-\r
- pkt = ssh1_pkt_init(pkttype);\r
-\r
- while ((argtype = va_arg(ap, int)) != PKT_END) {\r
- unsigned char *argp, argchar;\r
- char *sargp;\r
- unsigned long argint;\r
- int arglen;\r
- switch (argtype) {\r
- /* Actual fields in the packet */\r
- case PKT_INT:\r
- argint = va_arg(ap, int);\r
- ssh_pkt_adduint32(pkt, argint);\r
- break;\r
- case PKT_CHAR:\r
- argchar = (unsigned char) va_arg(ap, int);\r
- ssh_pkt_addbyte(pkt, argchar);\r
- break;\r
- case PKT_DATA:\r
- argp = va_arg(ap, unsigned char *);\r
- arglen = va_arg(ap, int);\r
- ssh_pkt_adddata(pkt, argp, arglen);\r
- break;\r
- case PKT_STR:\r
- sargp = va_arg(ap, char *);\r
- ssh_pkt_addstring(pkt, sargp);\r
- break;\r
- case PKT_BIGNUM:\r
- bn = va_arg(ap, Bignum);\r
- ssh1_pkt_addmp(pkt, bn);\r
- break;\r
- /* Tokens for modifications to packet logging */\r
- case PKTT_PASSWORD:\r
- dont_log_password(ssh, pkt, PKTLOG_BLANK);\r
- break;\r
- case PKTT_DATA:\r
- dont_log_data(ssh, pkt, PKTLOG_OMIT);\r
- break;\r
- case PKTT_OTHER:\r
- end_log_omission(ssh, pkt);\r
- break;\r
- }\r
- }\r
-\r
- return pkt;\r
-}\r
-\r
-static void send_packet(Ssh ssh, int pkttype, ...)\r
-{\r
- struct Packet *pkt;\r
- va_list ap;\r
- va_start(ap, pkttype);\r
- pkt = construct_packet(ssh, pkttype, ap);\r
- va_end(ap);\r
- s_wrpkt(ssh, pkt);\r
-}\r
-\r
-static void defer_packet(Ssh ssh, int pkttype, ...)\r
-{\r
- struct Packet *pkt;\r
- va_list ap;\r
- va_start(ap, pkttype);\r
- pkt = construct_packet(ssh, pkttype, ap);\r
- va_end(ap);\r
- s_wrpkt_defer(ssh, pkt);\r
-}\r
-\r
-static int ssh_versioncmp(char *a, char *b)\r
-{\r
- char *ae, *be;\r
- unsigned long av, bv;\r
-\r
- av = strtoul(a, &ae, 10);\r
- bv = strtoul(b, &be, 10);\r
- if (av != bv)\r
- return (av < bv ? -1 : +1);\r
- if (*ae == '.')\r
- ae++;\r
- if (*be == '.')\r
- be++;\r
- av = strtoul(ae, &ae, 10);\r
- bv = strtoul(be, &be, 10);\r
- if (av != bv)\r
- return (av < bv ? -1 : +1);\r
- return 0;\r
-}\r
-\r
-/*\r
- * Utility routines for putting an SSH-protocol `string' and\r
- * `uint32' into a hash state.\r
- */\r
-static void hash_string(const struct ssh_hash *h, void *s, void *str, int len)\r
-{\r
- unsigned char lenblk[4];\r
- PUT_32BIT(lenblk, len);\r
- h->bytes(s, lenblk, 4);\r
- h->bytes(s, str, len);\r
-}\r
-\r
-static void hash_uint32(const struct ssh_hash *h, void *s, unsigned i)\r
-{\r
- unsigned char intblk[4];\r
- PUT_32BIT(intblk, i);\r
- h->bytes(s, intblk, 4);\r
-}\r
-\r
-/*\r
- * Packet construction functions. Mostly shared between SSH-1 and SSH-2.\r
- */\r
-static void ssh_pkt_ensure(struct Packet *pkt, int length)\r
-{\r
- if (pkt->maxlen < length) {\r
- unsigned char *body = pkt->body;\r
- int offset = body ? body - pkt->data : 0;\r
- pkt->maxlen = length + 256;\r
- pkt->data = sresize(pkt->data, pkt->maxlen + APIEXTRA, unsigned char);\r
- if (body) pkt->body = pkt->data + offset;\r
- }\r
-}\r
-static void ssh_pkt_adddata(struct Packet *pkt, void *data, int len)\r
-{\r
- if (pkt->logmode != PKTLOG_EMIT) {\r
- pkt->nblanks++;\r
- pkt->blanks = sresize(pkt->blanks, pkt->nblanks, struct logblank_t);\r
- assert(pkt->body);\r
- pkt->blanks[pkt->nblanks-1].offset = pkt->length -\r
- (pkt->body - pkt->data);\r
- pkt->blanks[pkt->nblanks-1].len = len;\r
- pkt->blanks[pkt->nblanks-1].type = pkt->logmode;\r
- }\r
- pkt->length += len;\r
- ssh_pkt_ensure(pkt, pkt->length);\r
- memcpy(pkt->data + pkt->length - len, data, len);\r
-}\r
-static void ssh_pkt_addbyte(struct Packet *pkt, unsigned char byte)\r
-{\r
- ssh_pkt_adddata(pkt, &byte, 1);\r
-}\r
-static void ssh2_pkt_addbool(struct Packet *pkt, unsigned char value)\r
-{\r
- ssh_pkt_adddata(pkt, &value, 1);\r
-}\r
-static void ssh_pkt_adduint32(struct Packet *pkt, unsigned long value)\r
-{\r
- unsigned char x[4];\r
- PUT_32BIT(x, value);\r
- ssh_pkt_adddata(pkt, x, 4);\r
-}\r
-static void ssh_pkt_addstring_start(struct Packet *pkt)\r
-{\r
- ssh_pkt_adduint32(pkt, 0);\r
- pkt->savedpos = pkt->length;\r
-}\r
-static void ssh_pkt_addstring_str(struct Packet *pkt, char *data)\r
-{\r
- ssh_pkt_adddata(pkt, data, strlen(data));\r
- PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);\r
-}\r
-static void ssh_pkt_addstring_data(struct Packet *pkt, char *data, int len)\r
-{\r
- ssh_pkt_adddata(pkt, data, len);\r
- PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);\r
-}\r
-static void ssh_pkt_addstring(struct Packet *pkt, char *data)\r
-{\r
- ssh_pkt_addstring_start(pkt);\r
- ssh_pkt_addstring_str(pkt, data);\r
-}\r
-static void ssh1_pkt_addmp(struct Packet *pkt, Bignum b)\r
-{\r
- int len = ssh1_bignum_length(b);\r
- unsigned char *data = snewn(len, unsigned char);\r
- (void) ssh1_write_bignum(data, b);\r
- ssh_pkt_adddata(pkt, data, len);\r
- sfree(data);\r
-}\r
-static unsigned char *ssh2_mpint_fmt(Bignum b, int *len)\r
-{\r
- unsigned char *p;\r
- int i, n = (bignum_bitcount(b) + 7) / 8;\r
- p = snewn(n + 1, unsigned char);\r
- p[0] = 0;\r
- for (i = 1; i <= n; i++)\r
- p[i] = bignum_byte(b, n - i);\r
- i = 0;\r
- while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0)\r
- i++;\r
- memmove(p, p + i, n + 1 - i);\r
- *len = n + 1 - i;\r
- return p;\r
-}\r
-static void ssh2_pkt_addmp(struct Packet *pkt, Bignum b)\r
-{\r
- unsigned char *p;\r
- int len;\r
- p = ssh2_mpint_fmt(b, &len);\r
- ssh_pkt_addstring_start(pkt);\r
- ssh_pkt_addstring_data(pkt, (char *)p, len);\r
- sfree(p);\r
-}\r
-\r
-static struct Packet *ssh1_pkt_init(int pkt_type)\r
-{\r
- struct Packet *pkt = ssh_new_packet();\r
- pkt->length = 4 + 8; /* space for length + max padding */\r
- ssh_pkt_addbyte(pkt, pkt_type);\r
- pkt->body = pkt->data + pkt->length;\r
- return pkt;\r
-}\r
-\r
-/* For legacy code (SSH-1 and -2 packet construction used to be separate) */\r
-#define ssh2_pkt_ensure(pkt, length) ssh_pkt_ensure(pkt, length)\r
-#define ssh2_pkt_adddata(pkt, data, len) ssh_pkt_adddata(pkt, data, len)\r
-#define ssh2_pkt_addbyte(pkt, byte) ssh_pkt_addbyte(pkt, byte)\r
-#define ssh2_pkt_adduint32(pkt, value) ssh_pkt_adduint32(pkt, value)\r
-#define ssh2_pkt_addstring_start(pkt) ssh_pkt_addstring_start(pkt)\r
-#define ssh2_pkt_addstring_str(pkt, data) ssh_pkt_addstring_str(pkt, data)\r
-#define ssh2_pkt_addstring_data(pkt, data, len) ssh_pkt_addstring_data(pkt, data, len)\r
-#define ssh2_pkt_addstring(pkt, data) ssh_pkt_addstring(pkt, data)\r
-\r
-static struct Packet *ssh2_pkt_init(int pkt_type)\r
-{\r
- struct Packet *pkt = ssh_new_packet();\r
- pkt->length = 5; /* space for packet length + padding length */\r
- pkt->forcepad = 0;\r
- ssh_pkt_addbyte(pkt, (unsigned char) pkt_type);\r
- pkt->body = pkt->data + pkt->length; /* after packet type */\r
- return pkt;\r
-}\r
-\r
-/*\r
- * Construct an SSH-2 final-form packet: compress it, encrypt it,\r
- * put the MAC on it. Final packet, ready to be sent, is stored in\r
- * pkt->data. Total length is returned.\r
- */\r
-static int ssh2_pkt_construct(Ssh ssh, struct Packet *pkt)\r
-{\r
- int cipherblk, maclen, padding, i;\r
-\r
- if (ssh->logctx)\r
- log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[5],\r
- ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx, pkt->data[5]),\r
- pkt->body, pkt->length - (pkt->body - pkt->data),\r
- pkt->nblanks, pkt->blanks, &ssh->v2_outgoing_sequence);\r
- sfree(pkt->blanks); pkt->blanks = NULL;\r
- pkt->nblanks = 0;\r
-\r
- /*\r
- * Compress packet payload.\r
- */\r
- {\r
- unsigned char *newpayload;\r
- int newlen;\r
- if (ssh->cscomp &&\r
- ssh->cscomp->compress(ssh->cs_comp_ctx, pkt->data + 5,\r
- pkt->length - 5,\r
- &newpayload, &newlen)) {\r
- pkt->length = 5;\r
- ssh2_pkt_adddata(pkt, newpayload, newlen);\r
- sfree(newpayload);\r
- }\r
- }\r
-\r
- /*\r
- * Add padding. At least four bytes, and must also bring total\r
- * length (minus MAC) up to a multiple of the block size.\r
- * If pkt->forcepad is set, make sure the packet is at least that size\r
- * after padding.\r
- */\r
- cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */\r
- cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */\r
- padding = 4;\r
- if (pkt->length + padding < pkt->forcepad)\r
- padding = pkt->forcepad - pkt->length;\r
- padding +=\r
- (cipherblk - (pkt->length + padding) % cipherblk) % cipherblk;\r
- assert(padding <= 255);\r
- maclen = ssh->csmac ? ssh->csmac->len : 0;\r
- ssh2_pkt_ensure(pkt, pkt->length + padding + maclen);\r
- pkt->data[4] = padding;\r
- for (i = 0; i < padding; i++)\r
- pkt->data[pkt->length + i] = random_byte();\r
- PUT_32BIT(pkt->data, pkt->length + padding - 4);\r
- if (ssh->csmac)\r
- ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data,\r
- pkt->length + padding,\r
- ssh->v2_outgoing_sequence);\r
- ssh->v2_outgoing_sequence++; /* whether or not we MACed */\r
-\r
- if (ssh->cscipher)\r
- ssh->cscipher->encrypt(ssh->cs_cipher_ctx,\r
- pkt->data, pkt->length + padding);\r
-\r
- pkt->encrypted_len = pkt->length + padding;\r
-\r
- /* Ready-to-send packet starts at pkt->data. We return length. */\r
- return pkt->length + padding + maclen;\r
-}\r
-\r
-/*\r
- * Routines called from the main SSH code to send packets. There\r
- * are quite a few of these, because we have two separate\r
- * mechanisms for delaying the sending of packets:\r
- * \r
- * - In order to send an IGNORE message and a password message in\r
- * a single fixed-length blob, we require the ability to\r
- * concatenate the encrypted forms of those two packets _into_ a\r
- * single blob and then pass it to our <network.h> transport\r
- * layer in one go. Hence, there's a deferment mechanism which\r
- * works after packet encryption.\r
- * \r
- * - In order to avoid sending any connection-layer messages\r
- * during repeat key exchange, we have to queue up any such\r
- * outgoing messages _before_ they are encrypted (and in\r
- * particular before they're allocated sequence numbers), and\r
- * then send them once we've finished.\r
- * \r
- * I call these mechanisms `defer' and `queue' respectively, so as\r
- * to distinguish them reasonably easily.\r
- * \r
- * The functions send_noqueue() and defer_noqueue() free the packet\r
- * structure they are passed. Every outgoing packet goes through\r
- * precisely one of these functions in its life; packets passed to\r
- * ssh2_pkt_send() or ssh2_pkt_defer() either go straight to one of\r
- * these or get queued, and then when the queue is later emptied\r
- * the packets are all passed to defer_noqueue().\r
- *\r
- * When using a CBC-mode cipher, it's necessary to ensure that an\r
- * attacker can't provide data to be encrypted using an IV that they\r
- * know. We ensure this by prefixing each packet that might contain\r
- * user data with an SSH_MSG_IGNORE. This is done using the deferral\r
- * mechanism, so in this case send_noqueue() ends up redirecting to\r
- * defer_noqueue(). If you don't like this inefficiency, don't use\r
- * CBC.\r
- */\r
-\r
-static void ssh2_pkt_defer_noqueue(Ssh, struct Packet *, int);\r
-static void ssh_pkt_defersend(Ssh);\r
-\r
-/*\r
- * Send an SSH-2 packet immediately, without queuing or deferring.\r
- */\r
-static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt)\r
-{\r
- int len;\r
- int backlog;\r
- if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC)) {\r
- /* We need to send two packets, so use the deferral mechanism. */\r
- ssh2_pkt_defer_noqueue(ssh, pkt, FALSE);\r
- ssh_pkt_defersend(ssh);\r
- return;\r
- }\r
- len = ssh2_pkt_construct(ssh, pkt);\r
- backlog = s_write(ssh, pkt->data, len);\r
- if (backlog > SSH_MAX_BACKLOG)\r
- ssh_throttle_all(ssh, 1, backlog);\r
-\r
- ssh->outgoing_data_size += pkt->encrypted_len;\r
- if (!ssh->kex_in_progress &&\r
- ssh->max_data_size != 0 &&\r
- ssh->outgoing_data_size > ssh->max_data_size)\r
- do_ssh2_transport(ssh, "too much data sent", -1, NULL);\r
-\r
- ssh_free_packet(pkt);\r
-}\r
-\r
-/*\r
- * Defer an SSH-2 packet.\r
- */\r
-static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt, int noignore)\r
-{\r
- int len;\r
- if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC) &&\r
- ssh->deferred_len == 0 && !noignore &&\r
- !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {\r
- /*\r
- * Interpose an SSH_MSG_IGNORE to ensure that user data don't\r
- * get encrypted with a known IV.\r
- */\r
- struct Packet *ipkt = ssh2_pkt_init(SSH2_MSG_IGNORE);\r
- ssh2_pkt_addstring_start(ipkt);\r
- ssh2_pkt_defer_noqueue(ssh, ipkt, TRUE);\r
- }\r
- len = ssh2_pkt_construct(ssh, pkt);\r
- if (ssh->deferred_len + len > ssh->deferred_size) {\r
- ssh->deferred_size = ssh->deferred_len + len + 128;\r
- ssh->deferred_send_data = sresize(ssh->deferred_send_data,\r
- ssh->deferred_size,\r
- unsigned char);\r
- }\r
- memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->data, len);\r
- ssh->deferred_len += len;\r
- ssh->deferred_data_size += pkt->encrypted_len;\r
- ssh_free_packet(pkt);\r
-}\r
-\r
-/*\r
- * Queue an SSH-2 packet.\r
- */\r
-static void ssh2_pkt_queue(Ssh ssh, struct Packet *pkt)\r
-{\r
- assert(ssh->queueing);\r
-\r
- if (ssh->queuelen >= ssh->queuesize) {\r
- ssh->queuesize = ssh->queuelen + 32;\r
- ssh->queue = sresize(ssh->queue, ssh->queuesize, struct Packet *);\r
- }\r
-\r
- ssh->queue[ssh->queuelen++] = pkt;\r
-}\r
-\r
-/*\r
- * Either queue or send a packet, depending on whether queueing is\r
- * set.\r
- */\r
-static void ssh2_pkt_send(Ssh ssh, struct Packet *pkt)\r
-{\r
- if (ssh->queueing)\r
- ssh2_pkt_queue(ssh, pkt);\r
- else\r
- ssh2_pkt_send_noqueue(ssh, pkt);\r
-}\r
-\r
-/*\r
- * Either queue or defer a packet, depending on whether queueing is\r
- * set.\r
- */\r
-static void ssh2_pkt_defer(Ssh ssh, struct Packet *pkt)\r
-{\r
- if (ssh->queueing)\r
- ssh2_pkt_queue(ssh, pkt);\r
- else\r
- ssh2_pkt_defer_noqueue(ssh, pkt, FALSE);\r
-}\r
-\r
-/*\r
- * Send the whole deferred data block constructed by\r
- * ssh2_pkt_defer() or SSH-1's defer_packet().\r
- * \r
- * The expected use of the defer mechanism is that you call\r
- * ssh2_pkt_defer() a few times, then call ssh_pkt_defersend(). If\r
- * not currently queueing, this simply sets up deferred_send_data\r
- * and then sends it. If we _are_ currently queueing, the calls to\r
- * ssh2_pkt_defer() put the deferred packets on to the queue\r
- * instead, and therefore ssh_pkt_defersend() has no deferred data\r
- * to send. Hence, there's no need to make it conditional on\r
- * ssh->queueing.\r
- */\r
-static void ssh_pkt_defersend(Ssh ssh)\r
-{\r
- int backlog;\r
- backlog = s_write(ssh, ssh->deferred_send_data, ssh->deferred_len);\r
- ssh->deferred_len = ssh->deferred_size = 0;\r
- sfree(ssh->deferred_send_data);\r
- ssh->deferred_send_data = NULL;\r
- if (backlog > SSH_MAX_BACKLOG)\r
- ssh_throttle_all(ssh, 1, backlog);\r
-\r
- ssh->outgoing_data_size += ssh->deferred_data_size;\r
- if (!ssh->kex_in_progress &&\r
- ssh->max_data_size != 0 &&\r
- ssh->outgoing_data_size > ssh->max_data_size)\r
- do_ssh2_transport(ssh, "too much data sent", -1, NULL);\r
- ssh->deferred_data_size = 0;\r
-}\r
-\r
-/*\r
- * Send a packet whose length needs to be disguised (typically\r
- * passwords or keyboard-interactive responses).\r
- */\r
-static void ssh2_pkt_send_with_padding(Ssh ssh, struct Packet *pkt,\r
- int padsize)\r
-{\r
-#if 0\r
- if (0) {\r
- /*\r
- * The simplest way to do this is to adjust the\r
- * variable-length padding field in the outgoing packet.\r
- * \r
- * Currently compiled out, because some Cisco SSH servers\r
- * don't like excessively padded packets (bah, why's it\r
- * always Cisco?)\r
- */\r
- pkt->forcepad = padsize;\r
- ssh2_pkt_send(ssh, pkt);\r
- } else\r
-#endif\r
- {\r
- /*\r
- * If we can't do that, however, an alternative approach is\r
- * to use the pkt_defer mechanism to bundle the packet\r
- * tightly together with an SSH_MSG_IGNORE such that their\r
- * combined length is a constant. So first we construct the\r
- * final form of this packet and defer its sending.\r
- */\r
- ssh2_pkt_defer(ssh, pkt);\r
-\r
- /*\r
- * Now construct an SSH_MSG_IGNORE which includes a string\r
- * that's an exact multiple of the cipher block size. (If\r
- * the cipher is NULL so that the block size is\r
- * unavailable, we don't do this trick at all, because we\r
- * gain nothing by it.)\r
- */\r
- if (ssh->cscipher &&\r
- !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {\r
- int stringlen, i;\r
-\r
- stringlen = (256 - ssh->deferred_len);\r
- stringlen += ssh->cscipher->blksize - 1;\r
- stringlen -= (stringlen % ssh->cscipher->blksize);\r
- if (ssh->cscomp) {\r
- /*\r
- * Temporarily disable actual compression, so we\r
- * can guarantee to get this string exactly the\r
- * length we want it. The compression-disabling\r
- * routine should return an integer indicating how\r
- * many bytes we should adjust our string length\r
- * by.\r
- */\r
- stringlen -=\r
- ssh->cscomp->disable_compression(ssh->cs_comp_ctx);\r
- }\r
- pkt = ssh2_pkt_init(SSH2_MSG_IGNORE);\r
- ssh2_pkt_addstring_start(pkt);\r
- for (i = 0; i < stringlen; i++) {\r
- char c = (char) random_byte();\r
- ssh2_pkt_addstring_data(pkt, &c, 1);\r
- }\r
- ssh2_pkt_defer(ssh, pkt);\r
- }\r
- ssh_pkt_defersend(ssh);\r
- }\r
-}\r
-\r
-/*\r
- * Send all queued SSH-2 packets. We send them by means of\r
- * ssh2_pkt_defer_noqueue(), in case they included a pair of\r
- * packets that needed to be lumped together.\r
- */\r
-static void ssh2_pkt_queuesend(Ssh ssh)\r
-{\r
- int i;\r
-\r
- assert(!ssh->queueing);\r
-\r
- for (i = 0; i < ssh->queuelen; i++)\r
- ssh2_pkt_defer_noqueue(ssh, ssh->queue[i], FALSE);\r
- ssh->queuelen = 0;\r
-\r
- ssh_pkt_defersend(ssh);\r
-}\r
-\r
-#if 0\r
-void bndebug(char *string, Bignum b)\r
-{\r
- unsigned char *p;\r
- int i, len;\r
- p = ssh2_mpint_fmt(b, &len);\r
- debug(("%s", string));\r
- for (i = 0; i < len; i++)\r
- debug((" %02x", p[i]));\r
- debug(("\n"));\r
- sfree(p);\r
-}\r
-#endif\r
-\r
-static void hash_mpint(const struct ssh_hash *h, void *s, Bignum b)\r
-{\r
- unsigned char *p;\r
- int len;\r
- p = ssh2_mpint_fmt(b, &len);\r
- hash_string(h, s, p, len);\r
- sfree(p);\r
-}\r
-\r
-/*\r
- * Packet decode functions for both SSH-1 and SSH-2.\r
- */\r
-static unsigned long ssh_pkt_getuint32(struct Packet *pkt)\r
-{\r
- unsigned long value;\r
- if (pkt->length - pkt->savedpos < 4)\r
- return 0; /* arrgh, no way to decline (FIXME?) */\r
- value = GET_32BIT(pkt->body + pkt->savedpos);\r
- pkt->savedpos += 4;\r
- return value;\r
-}\r
-static int ssh2_pkt_getbool(struct Packet *pkt)\r
-{\r
- unsigned long value;\r
- if (pkt->length - pkt->savedpos < 1)\r
- return 0; /* arrgh, no way to decline (FIXME?) */\r
- value = pkt->body[pkt->savedpos] != 0;\r
- pkt->savedpos++;\r
- return value;\r
-}\r
-static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length)\r
-{\r
- int len;\r
- *p = NULL;\r
- *length = 0;\r
- if (pkt->length - pkt->savedpos < 4)\r
- return;\r
- len = GET_32BIT(pkt->body + pkt->savedpos);\r
- if (len < 0)\r
- return;\r
- *length = len;\r
- pkt->savedpos += 4;\r
- if (pkt->length - pkt->savedpos < *length)\r
- return;\r
- *p = (char *)(pkt->body + pkt->savedpos);\r
- pkt->savedpos += *length;\r
-}\r
-static void *ssh_pkt_getdata(struct Packet *pkt, int length)\r
-{\r
- if (pkt->length - pkt->savedpos < length)\r
- return NULL;\r
- pkt->savedpos += length;\r
- return pkt->body + (pkt->savedpos - length);\r
-}\r
-static int ssh1_pkt_getrsakey(struct Packet *pkt, struct RSAKey *key,\r
- unsigned char **keystr)\r
-{\r
- int j;\r
-\r
- j = makekey(pkt->body + pkt->savedpos,\r
- pkt->length - pkt->savedpos,\r
- key, keystr, 0);\r
-\r
- if (j < 0)\r
- return FALSE;\r
- \r
- pkt->savedpos += j;\r
- assert(pkt->savedpos < pkt->length);\r
-\r
- return TRUE;\r
-}\r
-static Bignum ssh1_pkt_getmp(struct Packet *pkt)\r
-{\r
- int j;\r
- Bignum b;\r
-\r
- j = ssh1_read_bignum(pkt->body + pkt->savedpos,\r
- pkt->length - pkt->savedpos, &b);\r
-\r
- if (j < 0)\r
- return NULL;\r
-\r
- pkt->savedpos += j;\r
- return b;\r
-}\r
-static Bignum ssh2_pkt_getmp(struct Packet *pkt)\r
-{\r
- char *p;\r
- int length;\r
- Bignum b;\r
-\r
- ssh_pkt_getstring(pkt, &p, &length);\r
- if (!p)\r
- return NULL;\r
- if (p[0] & 0x80)\r
- return NULL;\r
- b = bignum_from_bytes((unsigned char *)p, length);\r
- return b;\r
-}\r
-\r
-/*\r
- * Helper function to add an SSH-2 signature blob to a packet.\r
- * Expects to be shown the public key blob as well as the signature\r
- * blob. Normally works just like ssh2_pkt_addstring, but will\r
- * fiddle with the signature packet if necessary for\r
- * BUG_SSH2_RSA_PADDING.\r
- */\r
-static void ssh2_add_sigblob(Ssh ssh, struct Packet *pkt,\r
- void *pkblob_v, int pkblob_len,\r
- void *sigblob_v, int sigblob_len)\r
-{\r
- unsigned char *pkblob = (unsigned char *)pkblob_v;\r
- unsigned char *sigblob = (unsigned char *)sigblob_v;\r
-\r
- /* dmemdump(pkblob, pkblob_len); */\r
- /* dmemdump(sigblob, sigblob_len); */\r
-\r
- /*\r
- * See if this is in fact an ssh-rsa signature and a buggy\r
- * server; otherwise we can just do this the easy way.\r
- */\r
- if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) &&\r
- (GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) {\r
- int pos, len, siglen;\r
-\r
- /*\r
- * Find the byte length of the modulus.\r
- */\r
-\r
- pos = 4+7; /* skip over "ssh-rsa" */\r
- pos += 4 + GET_32BIT(pkblob+pos); /* skip over exponent */\r
- len = GET_32BIT(pkblob+pos); /* find length of modulus */\r
- pos += 4; /* find modulus itself */\r
- while (len > 0 && pkblob[pos] == 0)\r
- len--, pos++;\r
- /* debug(("modulus length is %d\n", len)); */\r
-\r
- /*\r
- * Now find the signature integer.\r
- */\r
- pos = 4+7; /* skip over "ssh-rsa" */\r
- siglen = GET_32BIT(sigblob+pos);\r
- /* debug(("signature length is %d\n", siglen)); */\r
-\r
- if (len != siglen) {\r
- unsigned char newlen[4];\r
- ssh2_pkt_addstring_start(pkt);\r
- ssh2_pkt_addstring_data(pkt, (char *)sigblob, pos);\r
- /* dmemdump(sigblob, pos); */\r
- pos += 4; /* point to start of actual sig */\r
- PUT_32BIT(newlen, len);\r
- ssh2_pkt_addstring_data(pkt, (char *)newlen, 4);\r
- /* dmemdump(newlen, 4); */\r
- newlen[0] = 0;\r
- while (len-- > siglen) {\r
- ssh2_pkt_addstring_data(pkt, (char *)newlen, 1);\r
- /* dmemdump(newlen, 1); */\r
- }\r
- ssh2_pkt_addstring_data(pkt, (char *)(sigblob+pos), siglen);\r
- /* dmemdump(sigblob+pos, siglen); */\r
- return;\r
- }\r
-\r
- /* Otherwise fall through and do it the easy way. */\r
- }\r
-\r
- ssh2_pkt_addstring_start(pkt);\r
- ssh2_pkt_addstring_data(pkt, (char *)sigblob, sigblob_len);\r
-}\r
-\r
-/*\r
- * Examine the remote side's version string and compare it against\r
- * a list of known buggy implementations.\r
- */\r
-static void ssh_detect_bugs(Ssh ssh, char *vstring)\r
-{\r
- char *imp; /* pointer to implementation part */\r
- imp = vstring;\r
- imp += strcspn(imp, "-");\r
- if (*imp) imp++;\r
- imp += strcspn(imp, "-");\r
- if (*imp) imp++;\r
-\r
- ssh->remote_bugs = 0;\r
-\r
- /*\r
- * General notes on server version strings:\r
- * - Not all servers reporting "Cisco-1.25" have all the bugs listed\r
- * here -- in particular, we've heard of one that's perfectly happy\r
- * with SSH1_MSG_IGNOREs -- but this string never seems to change,\r
- * so we can't distinguish them.\r
- */\r
- if (ssh->cfg.sshbug_ignore1 == FORCE_ON ||\r
- (ssh->cfg.sshbug_ignore1 == AUTO &&\r
- (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") ||\r
- !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") ||\r
- !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25") ||\r
- !strcmp(imp, "OSU_1.4alpha3") || !strcmp(imp, "OSU_1.5alpha4")))) {\r
- /*\r
- * These versions don't support SSH1_MSG_IGNORE, so we have\r
- * to use a different defence against password length\r
- * sniffing.\r
- */\r
- ssh->remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE;\r
- logevent("We believe remote version has SSH-1 ignore bug");\r
- }\r
-\r
- if (ssh->cfg.sshbug_plainpw1 == FORCE_ON ||\r
- (ssh->cfg.sshbug_plainpw1 == AUTO &&\r
- (!strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3")))) {\r
- /*\r
- * These versions need a plain password sent; they can't\r
- * handle having a null and a random length of data after\r
- * the password.\r
- */\r
- ssh->remote_bugs |= BUG_NEEDS_SSH1_PLAIN_PASSWORD;\r
- logevent("We believe remote version needs a plain SSH-1 password");\r
- }\r
-\r
- if (ssh->cfg.sshbug_rsa1 == FORCE_ON ||\r
- (ssh->cfg.sshbug_rsa1 == AUTO &&\r
- (!strcmp(imp, "Cisco-1.25")))) {\r
- /*\r
- * These versions apparently have no clue whatever about\r
- * RSA authentication and will panic and die if they see\r
- * an AUTH_RSA message.\r
- */\r
- ssh->remote_bugs |= BUG_CHOKES_ON_RSA;\r
- logevent("We believe remote version can't handle SSH-1 RSA authentication");\r
- }\r
-\r
- if (ssh->cfg.sshbug_hmac2 == FORCE_ON ||\r
- (ssh->cfg.sshbug_hmac2 == AUTO &&\r
- !wc_match("* VShell", imp) &&\r
- (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) ||\r
- wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) ||\r
- wc_match("2.1 *", imp)))) {\r
- /*\r
- * These versions have the HMAC bug.\r
- */\r
- ssh->remote_bugs |= BUG_SSH2_HMAC;\r
- logevent("We believe remote version has SSH-2 HMAC bug");\r
- }\r
-\r
- if (ssh->cfg.sshbug_derivekey2 == FORCE_ON ||\r
- (ssh->cfg.sshbug_derivekey2 == AUTO &&\r
- !wc_match("* VShell", imp) &&\r
- (wc_match("2.0.0*", imp) || wc_match("2.0.10*", imp) ))) {\r
- /*\r
- * These versions have the key-derivation bug (failing to\r
- * include the literal shared secret in the hashes that\r
- * generate the keys).\r
- */\r
- ssh->remote_bugs |= BUG_SSH2_DERIVEKEY;\r
- logevent("We believe remote version has SSH-2 key-derivation bug");\r
- }\r
-\r
- if (ssh->cfg.sshbug_rsapad2 == FORCE_ON ||\r
- (ssh->cfg.sshbug_rsapad2 == AUTO &&\r
- (wc_match("OpenSSH_2.[5-9]*", imp) ||\r
- wc_match("OpenSSH_3.[0-2]*", imp)))) {\r
- /*\r
- * These versions have the SSH-2 RSA padding bug.\r
- */\r
- ssh->remote_bugs |= BUG_SSH2_RSA_PADDING;\r
- logevent("We believe remote version has SSH-2 RSA padding bug");\r
- }\r
-\r
- if (ssh->cfg.sshbug_pksessid2 == FORCE_ON ||\r
- (ssh->cfg.sshbug_pksessid2 == AUTO &&\r
- wc_match("OpenSSH_2.[0-2]*", imp))) {\r
- /*\r
- * These versions have the SSH-2 session-ID bug in\r
- * public-key authentication.\r
- */\r
- ssh->remote_bugs |= BUG_SSH2_PK_SESSIONID;\r
- logevent("We believe remote version has SSH-2 public-key-session-ID bug");\r
- }\r
-\r
- if (ssh->cfg.sshbug_rekey2 == FORCE_ON ||\r
- (ssh->cfg.sshbug_rekey2 == AUTO &&\r
- (wc_match("DigiSSH_2.0", imp) ||\r
- wc_match("OpenSSH_2.[0-4]*", imp) ||\r
- wc_match("OpenSSH_2.5.[0-3]*", imp) ||\r
- wc_match("Sun_SSH_1.0", imp) ||\r
- wc_match("Sun_SSH_1.0.1", imp) ||\r
- /* All versions <= 1.2.6 (they changed their format in 1.2.7) */\r
- wc_match("WeOnlyDo-*", imp)))) {\r
- /*\r
- * These versions have the SSH-2 rekey bug.\r
- */\r
- ssh->remote_bugs |= BUG_SSH2_REKEY;\r
- logevent("We believe remote version has SSH-2 rekey bug");\r
- }\r
-\r
- if (ssh->cfg.sshbug_maxpkt2 == FORCE_ON ||\r
- (ssh->cfg.sshbug_maxpkt2 == AUTO &&\r
- (wc_match("1.36_sshlib GlobalSCAPE", imp) ||\r
- wc_match("1.36 sshlib: GlobalScape", imp)))) {\r
- /*\r
- * This version ignores our makpkt and needs to be throttled.\r
- */\r
- ssh->remote_bugs |= BUG_SSH2_MAXPKT;\r
- logevent("We believe remote version ignores SSH-2 maximum packet size");\r
- }\r
-\r
- if (ssh->cfg.sshbug_ignore2 == FORCE_ON) {\r
- /*\r
- * Servers that don't support SSH2_MSG_IGNORE. Currently,\r
- * none detected automatically.\r
- */\r
- ssh->remote_bugs |= BUG_CHOKES_ON_SSH2_IGNORE;\r
- logevent("We believe remote version has SSH-2 ignore bug");\r
- }\r
-}\r
-\r
-/*\r
- * The `software version' part of an SSH version string is required\r
- * to contain no spaces or minus signs.\r
- */\r
-static void ssh_fix_verstring(char *str)\r
-{\r
- /* Eat "SSH-<protoversion>-". */\r
- assert(*str == 'S'); str++;\r
- assert(*str == 'S'); str++;\r
- assert(*str == 'H'); str++;\r
- assert(*str == '-'); str++;\r
- while (*str && *str != '-') str++;\r
- assert(*str == '-'); str++;\r
-\r
- /* Convert minus signs and spaces in the remaining string into\r
- * underscores. */\r
- while (*str) {\r
- if (*str == '-' || *str == ' ')\r
- *str = '_';\r
- str++;\r
- }\r
-}\r
-\r
-/*\r
- * Send an appropriate SSH version string.\r
- */\r
-static void ssh_send_verstring(Ssh ssh, char *svers)\r
-{\r
- char *verstring;\r
-\r
- if (ssh->version == 2) {\r
- /*\r
- * Construct a v2 version string.\r
- */\r
- verstring = dupprintf("SSH-2.0-%s\015\012", sshver);\r
- } else {\r
- /*\r
- * Construct a v1 version string.\r
- */\r
- verstring = dupprintf("SSH-%s-%s\012",\r
- (ssh_versioncmp(svers, "1.5") <= 0 ?\r
- svers : "1.5"),\r
- sshver);\r
- }\r
-\r
- ssh_fix_verstring(verstring);\r
-\r
- if (ssh->version == 2) {\r
- size_t len;\r
- /*\r
- * Record our version string.\r
- */\r
- len = strcspn(verstring, "\015\012");\r
- ssh->v_c = snewn(len + 1, char);\r
- memcpy(ssh->v_c, verstring, len);\r
- ssh->v_c[len] = 0;\r
- }\r
-\r
- logeventf(ssh, "We claim version: %.*s",\r
- strcspn(verstring, "\015\012"), verstring);\r
- s_write(ssh, verstring, strlen(verstring));\r
- sfree(verstring);\r
-}\r
-\r
-static int do_ssh_init(Ssh ssh, unsigned char c)\r
-{\r
- struct do_ssh_init_state {\r
- int vslen;\r
- char version[10];\r
- char *vstring;\r
- int vstrsize;\r
- int i;\r
- int proto1, proto2;\r
- };\r
- crState(do_ssh_init_state);\r
-\r
- crBegin(ssh->do_ssh_init_crstate);\r
-\r
- /* Search for a line beginning with the string "SSH-" in the input. */\r
- for (;;) {\r
- if (c != 'S') goto no;\r
- crReturn(1);\r
- if (c != 'S') goto no;\r
- crReturn(1);\r
- if (c != 'H') goto no;\r
- crReturn(1);\r
- if (c != '-') goto no;\r
- break;\r
- no:\r
- while (c != '\012')\r
- crReturn(1);\r
- crReturn(1);\r
- }\r
-\r
- s->vstrsize = 16;\r
- s->vstring = snewn(s->vstrsize, char);\r
- strcpy(s->vstring, "SSH-");\r
- s->vslen = 4;\r
- s->i = 0;\r
- while (1) {\r
- crReturn(1); /* get another char */\r
- if (s->vslen >= s->vstrsize - 1) {\r
- s->vstrsize += 16;\r
- s->vstring = sresize(s->vstring, s->vstrsize, char);\r
- }\r
- s->vstring[s->vslen++] = c;\r
- if (s->i >= 0) {\r
- if (c == '-') {\r
- s->version[s->i] = '\0';\r
- s->i = -1;\r
- } else if (s->i < sizeof(s->version) - 1)\r
- s->version[s->i++] = c;\r
- } else if (c == '\012')\r
- break;\r
- }\r
-\r
- ssh->agentfwd_enabled = FALSE;\r
- ssh->rdpkt2_state.incoming_sequence = 0;\r
-\r
- s->vstring[s->vslen] = 0;\r
- s->vstring[strcspn(s->vstring, "\015\012")] = '\0';/* remove EOL chars */\r
- logeventf(ssh, "Server version: %s", s->vstring);\r
- ssh_detect_bugs(ssh, s->vstring);\r
-\r
- /*\r
- * Decide which SSH protocol version to support.\r
- */\r
-\r
- /* Anything strictly below "2.0" means protocol 1 is supported. */\r
- s->proto1 = ssh_versioncmp(s->version, "2.0") < 0;\r
- /* Anything greater or equal to "1.99" means protocol 2 is supported. */\r
- s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0;\r
-\r
- if (ssh->cfg.sshprot == 0 && !s->proto1) {\r
- bombout(("SSH protocol version 1 required by user but not provided by server"));\r
- crStop(0);\r
- }\r
- if (ssh->cfg.sshprot == 3 && !s->proto2) {\r
- bombout(("SSH protocol version 2 required by user but not provided by server"));\r
- crStop(0);\r
- }\r
-\r
- if (s->proto2 && (ssh->cfg.sshprot >= 2 || !s->proto1))\r
- ssh->version = 2;\r
- else\r
- ssh->version = 1;\r
-\r
- logeventf(ssh, "Using SSH protocol version %d", ssh->version);\r
-\r
- /* Send the version string, if we haven't already */\r
- if (ssh->cfg.sshprot != 3)\r
- ssh_send_verstring(ssh, s->version);\r
-\r
- if (ssh->version == 2) {\r
- size_t len;\r
- /*\r
- * Record their version string.\r
- */\r
- len = strcspn(s->vstring, "\015\012");\r
- ssh->v_s = snewn(len + 1, char);\r
- memcpy(ssh->v_s, s->vstring, len);\r
- ssh->v_s[len] = 0;\r
- \r
- /*\r
- * Initialise SSH-2 protocol.\r
- */\r
- ssh->protocol = ssh2_protocol;\r
- ssh2_protocol_setup(ssh);\r
- ssh->s_rdpkt = ssh2_rdpkt;\r
- } else {\r
- /*\r
- * Initialise SSH-1 protocol.\r
- */\r
- ssh->protocol = ssh1_protocol;\r
- ssh1_protocol_setup(ssh);\r
- ssh->s_rdpkt = ssh1_rdpkt;\r
- }\r
- if (ssh->version == 2)\r
- do_ssh2_transport(ssh, NULL, -1, NULL);\r
-\r
- update_specials_menu(ssh->frontend);\r
- ssh->state = SSH_STATE_BEFORE_SIZE;\r
- ssh->pinger = pinger_new(&ssh->cfg, &ssh_backend, ssh);\r
-\r
- sfree(s->vstring);\r
-\r
- crFinish(0);\r
-}\r
-\r
-static void ssh_process_incoming_data(Ssh ssh,\r
- unsigned char **data, int *datalen)\r
-{\r
- struct Packet *pktin;\r
-\r
- pktin = ssh->s_rdpkt(ssh, data, datalen);\r
- if (pktin) {\r
- ssh->protocol(ssh, NULL, 0, pktin);\r
- ssh_free_packet(pktin);\r
- }\r
-}\r
-\r
-static void ssh_queue_incoming_data(Ssh ssh,\r
- unsigned char **data, int *datalen)\r
-{\r
- bufchain_add(&ssh->queued_incoming_data, *data, *datalen);\r
- *data += *datalen;\r
- *datalen = 0;\r
-}\r
-\r
-static void ssh_process_queued_incoming_data(Ssh ssh)\r
-{\r
- void *vdata;\r
- unsigned char *data;\r
- int len, origlen;\r
-\r
- while (!ssh->frozen && bufchain_size(&ssh->queued_incoming_data)) {\r
- bufchain_prefix(&ssh->queued_incoming_data, &vdata, &len);\r
- data = vdata;\r
- origlen = len;\r
-\r
- while (!ssh->frozen && len > 0)\r
- ssh_process_incoming_data(ssh, &data, &len);\r
-\r
- if (origlen > len)\r
- bufchain_consume(&ssh->queued_incoming_data, origlen - len);\r
- }\r
-}\r
-\r
-static void ssh_set_frozen(Ssh ssh, int frozen)\r
-{\r
- if (ssh->s)\r
- sk_set_frozen(ssh->s, frozen);\r
- ssh->frozen = frozen;\r
-}\r
-\r
-static void ssh_gotdata(Ssh ssh, unsigned char *data, int datalen)\r
-{\r
- /* Log raw data, if we're in that mode. */\r
- if (ssh->logctx)\r
- log_packet(ssh->logctx, PKT_INCOMING, -1, NULL, data, datalen,\r
- 0, NULL, NULL);\r
-\r
- crBegin(ssh->ssh_gotdata_crstate);\r
-\r
- /*\r
- * To begin with, feed the characters one by one to the\r
- * protocol initialisation / selection function do_ssh_init().\r
- * When that returns 0, we're done with the initial greeting\r
- * exchange and can move on to packet discipline.\r
- */\r
- while (1) {\r
- int ret; /* need not be kept across crReturn */\r
- if (datalen == 0)\r
- crReturnV; /* more data please */\r
- ret = do_ssh_init(ssh, *data);\r
- data++;\r
- datalen--;\r
- if (ret == 0)\r
- break;\r
- }\r
-\r
- /*\r
- * We emerge from that loop when the initial negotiation is\r
- * over and we have selected an s_rdpkt function. Now pass\r
- * everything to s_rdpkt, and then pass the resulting packets\r
- * to the proper protocol handler.\r
- */\r
-\r
- while (1) {\r
- while (bufchain_size(&ssh->queued_incoming_data) > 0 || datalen > 0) {\r
- if (ssh->frozen) {\r
- ssh_queue_incoming_data(ssh, &data, &datalen);\r
- /* This uses up all data and cannot cause anything interesting\r
- * to happen; indeed, for anything to happen at all, we must\r
- * return, so break out. */\r
- break;\r
- } else if (bufchain_size(&ssh->queued_incoming_data) > 0) {\r
- /* This uses up some or all data, and may freeze the\r
- * session. */\r
- ssh_process_queued_incoming_data(ssh);\r
- } else {\r
- /* This uses up some or all data, and may freeze the\r
- * session. */\r
- ssh_process_incoming_data(ssh, &data, &datalen);\r
- }\r
- /* FIXME this is probably EBW. */\r
- if (ssh->state == SSH_STATE_CLOSED)\r
- return;\r
- }\r
- /* We're out of data. Go and get some more. */\r
- crReturnV;\r
- }\r
- crFinishV;\r
-}\r
-\r
-static int ssh_do_close(Ssh ssh, int notify_exit)\r
-{\r
- int ret = 0;\r
- struct ssh_channel *c;\r
-\r
- ssh->state = SSH_STATE_CLOSED;\r
- expire_timer_context(ssh);\r
- if (ssh->s) {\r
- sk_close(ssh->s);\r
- ssh->s = NULL;\r
- if (notify_exit)\r
- notify_remote_exit(ssh->frontend);\r
- else\r
- ret = 1;\r
- }\r
- /*\r
- * Now we must shut down any port- and X-forwarded channels going\r
- * through this connection.\r
- */\r
- if (ssh->channels) {\r
- while (NULL != (c = index234(ssh->channels, 0))) {\r
- switch (c->type) {\r
- case CHAN_X11:\r
- x11_close(c->u.x11.s);\r
- break;\r
- case CHAN_SOCKDATA:\r
- case CHAN_SOCKDATA_DORMANT:\r
- pfd_close(c->u.pfd.s);\r
- break;\r
- }\r
- del234(ssh->channels, c); /* moving next one to index 0 */\r
- if (ssh->version == 2)\r
- bufchain_clear(&c->v.v2.outbuffer);\r
- sfree(c);\r
- }\r
- }\r
- /*\r
- * Go through port-forwardings, and close any associated\r
- * listening sockets.\r
- */\r
- if (ssh->portfwds) {\r
- struct ssh_portfwd *pf;\r
- while (NULL != (pf = index234(ssh->portfwds, 0))) {\r
- /* Dispose of any listening socket. */\r
- if (pf->local)\r
- pfd_terminate(pf->local);\r
- del234(ssh->portfwds, pf); /* moving next one to index 0 */\r
- free_portfwd(pf);\r
- }\r
- freetree234(ssh->portfwds);\r
- ssh->portfwds = NULL;\r
- }\r
-\r
- return ret;\r
-}\r
-\r
-static void ssh_log(Plug plug, int type, SockAddr addr, int port,\r
- const char *error_msg, int error_code)\r
-{\r
- Ssh ssh = (Ssh) plug;\r
- char addrbuf[256], *msg;\r
-\r
- sk_getaddr(addr, addrbuf, lenof(addrbuf));\r
-\r
- if (type == 0)\r
- msg = dupprintf("Connecting to %s port %d", addrbuf, port);\r
- else\r
- msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);\r
-\r
- logevent(msg);\r
- sfree(msg);\r
-}\r
-\r
-static int ssh_closing(Plug plug, const char *error_msg, int error_code,\r
- int calling_back)\r
-{\r
- Ssh ssh = (Ssh) plug;\r
- int need_notify = ssh_do_close(ssh, FALSE);\r
-\r
- if (!error_msg) {\r
- if (!ssh->close_expected)\r
- error_msg = "Server unexpectedly closed network connection";\r
- else\r
- error_msg = "Server closed network connection";\r
- }\r
-\r
- if (ssh->close_expected && ssh->clean_exit && ssh->exitcode < 0)\r
- ssh->exitcode = 0;\r
-\r
- if (need_notify)\r
- notify_remote_exit(ssh->frontend);\r
-\r
- if (error_msg)\r
- logevent(error_msg);\r
- if (!ssh->close_expected || !ssh->clean_exit)\r
- connection_fatal(ssh->frontend, "%s", error_msg);\r
- return 0;\r
-}\r
-\r
-static int ssh_receive(Plug plug, int urgent, char *data, int len)\r
-{\r
- Ssh ssh = (Ssh) plug;\r
- ssh_gotdata(ssh, (unsigned char *)data, len);\r
- if (ssh->state == SSH_STATE_CLOSED) {\r
- ssh_do_close(ssh, TRUE);\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-static void ssh_sent(Plug plug, int bufsize)\r
-{\r
- Ssh ssh = (Ssh) plug;\r
- /*\r
- * If the send backlog on the SSH socket itself clears, we\r
- * should unthrottle the whole world if it was throttled.\r
- */\r
- if (bufsize < SSH_MAX_BACKLOG)\r
- ssh_throttle_all(ssh, 0, bufsize);\r
-}\r
-\r
-/*\r
- * Connect to specified host and port.\r
- * Returns an error message, or NULL on success.\r
- * Also places the canonical host name into `realhost'. It must be\r
- * freed by the caller.\r
- */\r
-static const char *connect_to_host(Ssh ssh, char *host, int port,\r
- char **realhost, int nodelay, int keepalive)\r
-{\r
- static const struct plug_function_table fn_table = {\r
- ssh_log,\r
- ssh_closing,\r
- ssh_receive,\r
- ssh_sent,\r
- NULL\r
- };\r
-\r
- SockAddr addr;\r
- const char *err;\r
-\r
- if (*ssh->cfg.loghost) {\r
- char *colon;\r
-\r
- ssh->savedhost = dupstr(ssh->cfg.loghost);\r
- ssh->savedport = 22; /* default ssh port */\r
-\r
- /*\r
- * A colon suffix on savedhost also lets us affect\r
- * savedport.\r
- * \r
- * (FIXME: do something about IPv6 address literals here.)\r
- */\r
- colon = strrchr(ssh->savedhost, ':');\r
- if (colon) {\r
- *colon++ = '\0';\r
- if (*colon)\r
- ssh->savedport = atoi(colon);\r
- }\r
- } else {\r
- ssh->savedhost = dupstr(host);\r
- if (port < 0)\r
- port = 22; /* default ssh port */\r
- ssh->savedport = port;\r
- }\r
-\r
- /*\r
- * Try to find host.\r
- */\r
- logeventf(ssh, "Looking up host \"%s\"%s", host,\r
- (ssh->cfg.addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :\r
- (ssh->cfg.addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : "")));\r
- addr = name_lookup(host, port, realhost, &ssh->cfg,\r
- ssh->cfg.addressfamily);\r
- if ((err = sk_addr_error(addr)) != NULL) {\r
- sk_addr_free(addr);\r
- return err;\r
- }\r
- ssh->fullhostname = dupstr(*realhost); /* save in case of GSSAPI */\r
-\r
- /*\r
- * Open socket.\r
- */\r
- ssh->fn = &fn_table;\r
- ssh->s = new_connection(addr, *realhost, port,\r
- 0, 1, nodelay, keepalive, (Plug) ssh, &ssh->cfg);\r
- if ((err = sk_socket_error(ssh->s)) != NULL) {\r
- ssh->s = NULL;\r
- notify_remote_exit(ssh->frontend);\r
- return err;\r
- }\r
-\r
- /*\r
- * If the SSH version number's fixed, set it now, and if it's SSH-2,\r
- * send the version string too.\r
- */\r
- if (ssh->cfg.sshprot == 0)\r
- ssh->version = 1;\r
- if (ssh->cfg.sshprot == 3) {\r
- ssh->version = 2;\r
- ssh_send_verstring(ssh, NULL);\r
- }\r
-\r
- /*\r
- * loghost, if configured, overrides realhost.\r
- */\r
- if (*ssh->cfg.loghost) {\r
- sfree(*realhost);\r
- *realhost = dupstr(ssh->cfg.loghost);\r
- }\r
-\r
- return NULL;\r
-}\r
-\r
-/*\r
- * Throttle or unthrottle the SSH connection.\r
- */\r
-static void ssh_throttle_conn(Ssh ssh, int adjust)\r
-{\r
- int old_count = ssh->conn_throttle_count;\r
- ssh->conn_throttle_count += adjust;\r
- assert(ssh->conn_throttle_count >= 0);\r
- if (ssh->conn_throttle_count && !old_count) {\r
- ssh_set_frozen(ssh, 1);\r
- } else if (!ssh->conn_throttle_count && old_count) {\r
- ssh_set_frozen(ssh, 0);\r
- }\r
-}\r
-\r
-/*\r
- * Throttle or unthrottle _all_ local data streams (for when sends\r
- * on the SSH connection itself back up).\r
- */\r
-static void ssh_throttle_all(Ssh ssh, int enable, int bufsize)\r
-{\r
- int i;\r
- struct ssh_channel *c;\r
-\r
- if (enable == ssh->throttled_all)\r
- return;\r
- ssh->throttled_all = enable;\r
- ssh->overall_bufsize = bufsize;\r
- if (!ssh->channels)\r
- return;\r
- for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) {\r
- switch (c->type) {\r
- case CHAN_MAINSESSION:\r
- /*\r
- * This is treated separately, outside the switch.\r
- */\r
- break;\r
- case CHAN_X11:\r
- x11_override_throttle(c->u.x11.s, enable);\r
- break;\r
- case CHAN_AGENT:\r
- /* Agent channels require no buffer management. */\r
- break;\r
- case CHAN_SOCKDATA:\r
- pfd_override_throttle(c->u.pfd.s, enable);\r
- break;\r
- }\r
- }\r
-}\r
-\r
-static void ssh_agent_callback(void *sshv, void *reply, int replylen)\r
-{\r
- Ssh ssh = (Ssh) sshv;\r
-\r
- ssh->agent_response = reply;\r
- ssh->agent_response_len = replylen;\r
-\r
- if (ssh->version == 1)\r
- do_ssh1_login(ssh, NULL, -1, NULL);\r
- else\r
- do_ssh2_authconn(ssh, NULL, -1, NULL);\r
-}\r
-\r
-static void ssh_dialog_callback(void *sshv, int ret)\r
-{\r
- Ssh ssh = (Ssh) sshv;\r
-\r
- ssh->user_response = ret;\r
-\r
- if (ssh->version == 1)\r
- do_ssh1_login(ssh, NULL, -1, NULL);\r
- else\r
- do_ssh2_transport(ssh, NULL, -1, NULL);\r
-\r
- /*\r
- * This may have unfrozen the SSH connection, so do a\r
- * queued-data run.\r
- */\r
- ssh_process_queued_incoming_data(ssh);\r
-}\r
-\r
-static void ssh_agentf_callback(void *cv, void *reply, int replylen)\r
-{\r
- struct ssh_channel *c = (struct ssh_channel *)cv;\r
- Ssh ssh = c->ssh;\r
- void *sentreply = reply;\r
-\r
- if (!sentreply) {\r
- /* Fake SSH_AGENT_FAILURE. */\r
- sentreply = "\0\0\0\1\5";\r
- replylen = 5;\r
- }\r
- if (ssh->version == 2) {\r
- ssh2_add_channel_data(c, sentreply, replylen);\r
- ssh2_try_send(c);\r
- } else {\r
- send_packet(ssh, SSH1_MSG_CHANNEL_DATA,\r
- PKT_INT, c->remoteid,\r
- PKT_INT, replylen,\r
- PKTT_DATA,\r
- PKT_DATA, sentreply, replylen,\r
- PKTT_OTHER,\r
- PKT_END);\r
- }\r
- if (reply)\r
- sfree(reply);\r
-}\r
-\r
-/*\r
- * Client-initiated disconnection. Send a DISCONNECT if `wire_reason'\r
- * non-NULL, otherwise just close the connection. `client_reason' == NULL\r
- * => log `wire_reason'.\r
- */\r
-static void ssh_disconnect(Ssh ssh, char *client_reason, char *wire_reason,\r
- int code, int clean_exit)\r
-{\r
- char *error;\r
- if (!client_reason)\r
- client_reason = wire_reason;\r
- if (client_reason)\r
- error = dupprintf("Disconnected: %s", client_reason);\r
- else\r
- error = dupstr("Disconnected");\r
- if (wire_reason) {\r
- if (ssh->version == 1) {\r
- send_packet(ssh, SSH1_MSG_DISCONNECT, PKT_STR, wire_reason,\r
- PKT_END);\r
- } else if (ssh->version == 2) {\r
- struct Packet *pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT);\r
- ssh2_pkt_adduint32(pktout, code);\r
- ssh2_pkt_addstring(pktout, wire_reason);\r
- ssh2_pkt_addstring(pktout, "en"); /* language tag */\r
- ssh2_pkt_send_noqueue(ssh, pktout);\r
- }\r
- }\r
- ssh->close_expected = TRUE;\r
- ssh->clean_exit = clean_exit;\r
- ssh_closing((Plug)ssh, error, 0, 0);\r
- sfree(error);\r
-}\r
-\r
-/*\r
- * Handle the key exchange and user authentication phases.\r
- */\r
-static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,\r
- struct Packet *pktin)\r
-{\r
- int i, j, ret;\r
- unsigned char cookie[8], *ptr;\r
- struct RSAKey servkey, hostkey;\r
- struct MD5Context md5c;\r
- struct do_ssh1_login_state {\r
- int len;\r
- unsigned char *rsabuf, *keystr1, *keystr2;\r
- unsigned long supported_ciphers_mask, supported_auths_mask;\r
- int tried_publickey, tried_agent;\r
- int tis_auth_refused, ccard_auth_refused;\r
- unsigned char session_id[16];\r
- int cipher_type;\r
- char username[100];\r
- void *publickey_blob;\r
- int publickey_bloblen;\r
- char *publickey_comment;\r
- int publickey_encrypted;\r
- prompts_t *cur_prompt;\r
- char c;\r
- int pwpkt_type;\r
- unsigned char request[5], *response, *p;\r
- int responselen;\r
- int keyi, nkeys;\r
- int authed;\r
- struct RSAKey key;\r
- Bignum challenge;\r
- char *commentp;\r
- int commentlen;\r
- int dlgret;\r
- };\r
- crState(do_ssh1_login_state);\r
-\r
- crBegin(ssh->do_ssh1_login_crstate);\r
-\r
- if (!pktin)\r
- crWaitUntil(pktin);\r
-\r
- if (pktin->type != SSH1_SMSG_PUBLIC_KEY) {\r
- bombout(("Public key packet not received"));\r
- crStop(0);\r
- }\r
-\r
- logevent("Received public keys");\r
-\r
- ptr = ssh_pkt_getdata(pktin, 8);\r
- if (!ptr) {\r
- bombout(("SSH-1 public key packet stopped before random cookie"));\r
- crStop(0);\r
- }\r
- memcpy(cookie, ptr, 8);\r
-\r
- if (!ssh1_pkt_getrsakey(pktin, &servkey, &s->keystr1) ||\r
- !ssh1_pkt_getrsakey(pktin, &hostkey, &s->keystr2)) { \r
- bombout(("Failed to read SSH-1 public keys from public key packet"));\r
- crStop(0);\r
- }\r
-\r
- /*\r
- * Log the host key fingerprint.\r
- */\r
- {\r
- char logmsg[80];\r
- logevent("Host key fingerprint is:");\r
- strcpy(logmsg, " ");\r
- hostkey.comment = NULL;\r
- rsa_fingerprint(logmsg + strlen(logmsg),\r
- sizeof(logmsg) - strlen(logmsg), &hostkey);\r
- logevent(logmsg);\r
- }\r
-\r
- ssh->v1_remote_protoflags = ssh_pkt_getuint32(pktin);\r
- s->supported_ciphers_mask = ssh_pkt_getuint32(pktin);\r
- s->supported_auths_mask = ssh_pkt_getuint32(pktin);\r
- if ((ssh->remote_bugs & BUG_CHOKES_ON_RSA))\r
- s->supported_auths_mask &= ~(1 << SSH1_AUTH_RSA);\r
-\r
- ssh->v1_local_protoflags =\r
- ssh->v1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED;\r
- ssh->v1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER;\r
-\r
- MD5Init(&md5c);\r
- MD5Update(&md5c, s->keystr2, hostkey.bytes);\r
- MD5Update(&md5c, s->keystr1, servkey.bytes);\r
- MD5Update(&md5c, cookie, 8);\r
- MD5Final(s->session_id, &md5c);\r
-\r
- for (i = 0; i < 32; i++)\r
- ssh->session_key[i] = random_byte();\r
-\r
- /*\r
- * Verify that the `bits' and `bytes' parameters match.\r
- */\r
- if (hostkey.bits > hostkey.bytes * 8 ||\r
- servkey.bits > servkey.bytes * 8) {\r
- bombout(("SSH-1 public keys were badly formatted"));\r
- crStop(0);\r
- }\r
-\r
- s->len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);\r
-\r
- s->rsabuf = snewn(s->len, unsigned char);\r
-\r
- /*\r
- * Verify the host key.\r
- */\r
- {\r
- /*\r
- * First format the key into a string.\r
- */\r
- int len = rsastr_len(&hostkey);\r
- char fingerprint[100];\r
- char *keystr = snewn(len, char);\r
- rsastr_fmt(keystr, &hostkey);\r
- rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey);\r
-\r
- ssh_set_frozen(ssh, 1);\r
- s->dlgret = verify_ssh_host_key(ssh->frontend,\r
- ssh->savedhost, ssh->savedport,\r
- "rsa", keystr, fingerprint,\r
- ssh_dialog_callback, ssh);\r
- sfree(keystr);\r
- if (s->dlgret < 0) {\r
- do {\r
- crReturn(0);\r
- if (pktin) {\r
- bombout(("Unexpected data from server while waiting"\r
- " for user host key response"));\r
- crStop(0);\r
- }\r
- } while (pktin || inlen > 0);\r
- s->dlgret = ssh->user_response;\r
- }\r
- ssh_set_frozen(ssh, 0);\r
-\r
- if (s->dlgret == 0) {\r
- ssh_disconnect(ssh, "User aborted at host key verification",\r
- NULL, 0, TRUE);\r
- crStop(0);\r
- }\r
- }\r
-\r
- for (i = 0; i < 32; i++) {\r
- s->rsabuf[i] = ssh->session_key[i];\r
- if (i < 16)\r
- s->rsabuf[i] ^= s->session_id[i];\r
- }\r
-\r
- if (hostkey.bytes > servkey.bytes) {\r
- ret = rsaencrypt(s->rsabuf, 32, &servkey);\r
- if (ret)\r
- ret = rsaencrypt(s->rsabuf, servkey.bytes, &hostkey);\r
- } else {\r
- ret = rsaencrypt(s->rsabuf, 32, &hostkey);\r
- if (ret)\r
- ret = rsaencrypt(s->rsabuf, hostkey.bytes, &servkey);\r
- }\r
- if (!ret) {\r
- bombout(("SSH-1 public key encryptions failed due to bad formatting"));\r
- crStop(0); \r
- }\r
-\r
- logevent("Encrypted session key");\r
-\r
- {\r
- int cipher_chosen = 0, warn = 0;\r
- char *cipher_string = NULL;\r
- int i;\r
- for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) {\r
- int next_cipher = ssh->cfg.ssh_cipherlist[i];\r
- if (next_cipher == CIPHER_WARN) {\r
- /* If/when we choose a cipher, warn about it */\r
- warn = 1;\r
- } else if (next_cipher == CIPHER_AES) {\r
- /* XXX Probably don't need to mention this. */\r
- logevent("AES not supported in SSH-1, skipping");\r
- } else {\r
- switch (next_cipher) {\r
- case CIPHER_3DES: s->cipher_type = SSH_CIPHER_3DES;\r
- cipher_string = "3DES"; break;\r
- case CIPHER_BLOWFISH: s->cipher_type = SSH_CIPHER_BLOWFISH;\r
- cipher_string = "Blowfish"; break;\r
- case CIPHER_DES: s->cipher_type = SSH_CIPHER_DES;\r
- cipher_string = "single-DES"; break;\r
- }\r
- if (s->supported_ciphers_mask & (1 << s->cipher_type))\r
- cipher_chosen = 1;\r
- }\r
- }\r
- if (!cipher_chosen) {\r
- if ((s->supported_ciphers_mask & (1 << SSH_CIPHER_3DES)) == 0)\r
- bombout(("Server violates SSH-1 protocol by not "\r
- "supporting 3DES encryption"));\r
- else\r
- /* shouldn't happen */\r
- bombout(("No supported ciphers found"));\r
- crStop(0);\r
- }\r
-\r
- /* Warn about chosen cipher if necessary. */\r
- if (warn) {\r
- ssh_set_frozen(ssh, 1);\r
- s->dlgret = askalg(ssh->frontend, "cipher", cipher_string,\r
- ssh_dialog_callback, ssh);\r
- if (s->dlgret < 0) {\r
- do {\r
- crReturn(0);\r
- if (pktin) {\r
- bombout(("Unexpected data from server while waiting"\r
- " for user response"));\r
- crStop(0);\r
- }\r
- } while (pktin || inlen > 0);\r
- s->dlgret = ssh->user_response;\r
- }\r
- ssh_set_frozen(ssh, 0);\r
- if (s->dlgret == 0) {\r
- ssh_disconnect(ssh, "User aborted at cipher warning", NULL,\r
- 0, TRUE);\r
- crStop(0);\r
- }\r
- }\r
- }\r
-\r
- switch (s->cipher_type) {\r
- case SSH_CIPHER_3DES:\r
- logevent("Using 3DES encryption");\r
- break;\r
- case SSH_CIPHER_DES:\r
- logevent("Using single-DES encryption");\r
- break;\r
- case SSH_CIPHER_BLOWFISH:\r
- logevent("Using Blowfish encryption");\r
- break;\r
- }\r
-\r
- send_packet(ssh, SSH1_CMSG_SESSION_KEY,\r
- PKT_CHAR, s->cipher_type,\r
- PKT_DATA, cookie, 8,\r
- PKT_CHAR, (s->len * 8) >> 8, PKT_CHAR, (s->len * 8) & 0xFF,\r
- PKT_DATA, s->rsabuf, s->len,\r
- PKT_INT, ssh->v1_local_protoflags, PKT_END);\r
-\r
- logevent("Trying to enable encryption...");\r
-\r
- sfree(s->rsabuf);\r
-\r
- ssh->cipher = (s->cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 :\r
- s->cipher_type == SSH_CIPHER_DES ? &ssh_des :\r
- &ssh_3des);\r
- ssh->v1_cipher_ctx = ssh->cipher->make_context();\r
- ssh->cipher->sesskey(ssh->v1_cipher_ctx, ssh->session_key);\r
- logeventf(ssh, "Initialised %s encryption", ssh->cipher->text_name);\r
-\r
- ssh->crcda_ctx = crcda_make_context();\r
- logevent("Installing CRC compensation attack detector");\r
-\r
- if (servkey.modulus) {\r
- sfree(servkey.modulus);\r
- servkey.modulus = NULL;\r
- }\r
- if (servkey.exponent) {\r
- sfree(servkey.exponent);\r
- servkey.exponent = NULL;\r
- }\r
- if (hostkey.modulus) {\r
- sfree(hostkey.modulus);\r
- hostkey.modulus = NULL;\r
- }\r
- if (hostkey.exponent) {\r
- sfree(hostkey.exponent);\r
- hostkey.exponent = NULL;\r
- }\r
- crWaitUntil(pktin);\r
-\r
- if (pktin->type != SSH1_SMSG_SUCCESS) {\r
- bombout(("Encryption not successfully enabled"));\r
- crStop(0);\r
- }\r
-\r
- logevent("Successfully started encryption");\r
-\r
- fflush(stdout); /* FIXME eh? */\r
- {\r
- if (!get_remote_username(&ssh->cfg, s->username,\r
- sizeof(s->username))) {\r
- int ret; /* need not be kept over crReturn */\r
- s->cur_prompt = new_prompts(ssh->frontend);\r
- s->cur_prompt->to_server = TRUE;\r
- s->cur_prompt->name = dupstr("SSH login name");\r
- add_prompt(s->cur_prompt, dupstr("login as: "), TRUE,\r
- lenof(s->username)); \r
- ret = get_userpass_input(s->cur_prompt, NULL, 0);\r
- while (ret < 0) {\r
- ssh->send_ok = 1;\r
- crWaitUntil(!pktin);\r
- ret = get_userpass_input(s->cur_prompt, in, inlen);\r
- ssh->send_ok = 0;\r
- }\r
- if (!ret) {\r
- /*\r
- * Failed to get a username. Terminate.\r
- */\r
- free_prompts(s->cur_prompt);\r
- ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE);\r
- crStop(0);\r
- }\r
- memcpy(s->username, s->cur_prompt->prompts[0]->result,\r
- lenof(s->username));\r
- free_prompts(s->cur_prompt);\r
- }\r
-\r
- send_packet(ssh, SSH1_CMSG_USER, PKT_STR, s->username, PKT_END);\r
- {\r
- char *userlog = dupprintf("Sent username \"%s\"", s->username);\r
- logevent(userlog);\r
- if (flags & FLAG_INTERACTIVE &&\r
- (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) {\r
- c_write_str(ssh, userlog);\r
- c_write_str(ssh, "\r\n");\r
- }\r
- sfree(userlog);\r
- }\r
- }\r
-\r
- crWaitUntil(pktin);\r
-\r
- if ((s->supported_auths_mask & (1 << SSH1_AUTH_RSA)) == 0) {\r
- /* We must not attempt PK auth. Pretend we've already tried it. */\r
- s->tried_publickey = s->tried_agent = 1;\r
- } else {\r
- s->tried_publickey = s->tried_agent = 0;\r
- }\r
- s->tis_auth_refused = s->ccard_auth_refused = 0;\r
- /*\r
- * Load the public half of any configured keyfile for later use.\r
- */\r
- if (!filename_is_null(ssh->cfg.keyfile)) {\r
- int keytype;\r
- logeventf(ssh, "Reading private key file \"%.150s\"",\r
- filename_to_str(&ssh->cfg.keyfile));\r
- keytype = key_type(&ssh->cfg.keyfile);\r
- if (keytype == SSH_KEYTYPE_SSH1) {\r
- const char *error;\r
- if (rsakey_pubblob(&ssh->cfg.keyfile,\r
- &s->publickey_blob, &s->publickey_bloblen,\r
- &s->publickey_comment, &error)) {\r
- s->publickey_encrypted = rsakey_encrypted(&ssh->cfg.keyfile,\r
- NULL);\r
- } else {\r
- char *msgbuf;\r
- logeventf(ssh, "Unable to load private key (%s)", error);\r
- msgbuf = dupprintf("Unable to load private key file "\r
- "\"%.150s\" (%s)\r\n",\r
- filename_to_str(&ssh->cfg.keyfile),\r
- error);\r
- c_write_str(ssh, msgbuf);\r
- sfree(msgbuf);\r
- s->publickey_blob = NULL;\r
- }\r
- } else {\r
- char *msgbuf;\r
- logeventf(ssh, "Unable to use this key file (%s)",\r
- key_type_to_str(keytype));\r
- msgbuf = dupprintf("Unable to use key file \"%.150s\""\r
- " (%s)\r\n",\r
- filename_to_str(&ssh->cfg.keyfile),\r
- key_type_to_str(keytype));\r
- c_write_str(ssh, msgbuf);\r
- sfree(msgbuf);\r
- s->publickey_blob = NULL;\r
- }\r
- } else\r
- s->publickey_blob = NULL;\r
-\r
- while (pktin->type == SSH1_SMSG_FAILURE) {\r
- s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD;\r
-\r
- if (ssh->cfg.tryagent && agent_exists() && !s->tried_agent) {\r
- /*\r
- * Attempt RSA authentication using Pageant.\r
- */\r
- void *r;\r
-\r
- s->authed = FALSE;\r
- s->tried_agent = 1;\r
- logevent("Pageant is running. Requesting keys.");\r
-\r
- /* Request the keys held by the agent. */\r
- PUT_32BIT(s->request, 1);\r
- s->request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES;\r
- if (!agent_query(s->request, 5, &r, &s->responselen,\r
- ssh_agent_callback, ssh)) {\r
- do {\r
- crReturn(0);\r
- if (pktin) {\r
- bombout(("Unexpected data from server while waiting"\r
- " for agent response"));\r
- crStop(0);\r
- }\r
- } while (pktin || inlen > 0);\r
- r = ssh->agent_response;\r
- s->responselen = ssh->agent_response_len;\r
- }\r
- s->response = (unsigned char *) r;\r
- if (s->response && s->responselen >= 5 &&\r
- s->response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) {\r
- s->p = s->response + 5;\r
- s->nkeys = GET_32BIT(s->p);\r
- s->p += 4;\r
- logeventf(ssh, "Pageant has %d SSH-1 keys", s->nkeys);\r
- for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) {\r
- unsigned char *pkblob = s->p;\r
- s->p += 4;\r
- {\r
- int n, ok = FALSE;\r
- do { /* do while (0) to make breaking easy */\r
- n = ssh1_read_bignum\r
- (s->p, s->responselen-(s->p-s->response),\r
- &s->key.exponent);\r
- if (n < 0)\r
- break;\r
- s->p += n;\r
- n = ssh1_read_bignum\r
- (s->p, s->responselen-(s->p-s->response),\r
- &s->key.modulus);\r
- if (n < 0)\r
- break;\r
- s->p += n;\r
- if (s->responselen - (s->p-s->response) < 4)\r
- break;\r
- s->commentlen = GET_32BIT(s->p);\r
- s->p += 4;\r
- if (s->responselen - (s->p-s->response) <\r
- s->commentlen)\r
- break;\r
- s->commentp = (char *)s->p;\r
- s->p += s->commentlen;\r
- ok = TRUE;\r
- } while (0);\r
- if (!ok) {\r
- logevent("Pageant key list packet was truncated");\r
- break;\r
- }\r
- }\r
- if (s->publickey_blob) {\r
- if (!memcmp(pkblob, s->publickey_blob,\r
- s->publickey_bloblen)) {\r
- logeventf(ssh, "Pageant key #%d matches "\r
- "configured key file", s->keyi);\r
- s->tried_publickey = 1;\r
- } else\r
- /* Skip non-configured key */\r
- continue;\r
- }\r
- logeventf(ssh, "Trying Pageant key #%d", s->keyi);\r
- send_packet(ssh, SSH1_CMSG_AUTH_RSA,\r
- PKT_BIGNUM, s->key.modulus, PKT_END);\r
- crWaitUntil(pktin);\r
- if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {\r
- logevent("Key refused");\r
- continue;\r
- }\r
- logevent("Received RSA challenge");\r
- if ((s->challenge = ssh1_pkt_getmp(pktin)) == NULL) {\r
- bombout(("Server's RSA challenge was badly formatted"));\r
- crStop(0);\r
- }\r
-\r
- {\r
- char *agentreq, *q, *ret;\r
- void *vret;\r
- int len, retlen;\r
- len = 1 + 4; /* message type, bit count */\r
- len += ssh1_bignum_length(s->key.exponent);\r
- len += ssh1_bignum_length(s->key.modulus);\r
- len += ssh1_bignum_length(s->challenge);\r
- len += 16; /* session id */\r
- len += 4; /* response format */\r
- agentreq = snewn(4 + len, char);\r
- PUT_32BIT(agentreq, len);\r
- q = agentreq + 4;\r
- *q++ = SSH1_AGENTC_RSA_CHALLENGE;\r
- PUT_32BIT(q, bignum_bitcount(s->key.modulus));\r
- q += 4;\r
- q += ssh1_write_bignum(q, s->key.exponent);\r
- q += ssh1_write_bignum(q, s->key.modulus);\r
- q += ssh1_write_bignum(q, s->challenge);\r
- memcpy(q, s->session_id, 16);\r
- q += 16;\r
- PUT_32BIT(q, 1); /* response format */\r
- if (!agent_query(agentreq, len + 4, &vret, &retlen,\r
- ssh_agent_callback, ssh)) {\r
- sfree(agentreq);\r
- do {\r
- crReturn(0);\r
- if (pktin) {\r
- bombout(("Unexpected data from server"\r
- " while waiting for agent"\r
- " response"));\r
- crStop(0);\r
- }\r
- } while (pktin || inlen > 0);\r
- vret = ssh->agent_response;\r
- retlen = ssh->agent_response_len;\r
- } else\r
- sfree(agentreq);\r
- ret = vret;\r
- if (ret) {\r
- if (ret[4] == SSH1_AGENT_RSA_RESPONSE) {\r
- logevent("Sending Pageant's response");\r
- send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE,\r
- PKT_DATA, ret + 5, 16,\r
- PKT_END);\r
- sfree(ret);\r
- crWaitUntil(pktin);\r
- if (pktin->type == SSH1_SMSG_SUCCESS) {\r
- logevent\r
- ("Pageant's response accepted");\r
- if (flags & FLAG_VERBOSE) {\r
- c_write_str(ssh, "Authenticated using"\r
- " RSA key \"");\r
- c_write(ssh, s->commentp,\r
- s->commentlen);\r
- c_write_str(ssh, "\" from agent\r\n");\r
- }\r
- s->authed = TRUE;\r
- } else\r
- logevent\r
- ("Pageant's response not accepted");\r
- } else {\r
- logevent\r
- ("Pageant failed to answer challenge");\r
- sfree(ret);\r
- }\r
- } else {\r
- logevent("No reply received from Pageant");\r
- }\r
- }\r
- freebn(s->key.exponent);\r
- freebn(s->key.modulus);\r
- freebn(s->challenge);\r
- if (s->authed)\r
- break;\r
- }\r
- sfree(s->response);\r
- if (s->publickey_blob && !s->tried_publickey)\r
- logevent("Configured key file not in Pageant");\r
- } else {\r
- logevent("Failed to get reply from Pageant");\r
- }\r
- if (s->authed)\r
- break;\r
- }\r
- if (s->publickey_blob && !s->tried_publickey) {\r
- /*\r
- * Try public key authentication with the specified\r
- * key file.\r
- */\r
- int got_passphrase; /* need not be kept over crReturn */\r
- if (flags & FLAG_VERBOSE)\r
- c_write_str(ssh, "Trying public key authentication.\r\n");\r
- logeventf(ssh, "Trying public key \"%s\"",\r
- filename_to_str(&ssh->cfg.keyfile));\r
- s->tried_publickey = 1;\r
- got_passphrase = FALSE;\r
- while (!got_passphrase) {\r
- /*\r
- * Get a passphrase, if necessary.\r
- */\r
- char *passphrase = NULL; /* only written after crReturn */\r
- const char *error;\r
- if (!s->publickey_encrypted) {\r
- if (flags & FLAG_VERBOSE)\r
- c_write_str(ssh, "No passphrase required.\r\n");\r
- passphrase = NULL;\r
- } else {\r
- int ret; /* need not be kept over crReturn */\r
- s->cur_prompt = new_prompts(ssh->frontend);\r
- s->cur_prompt->to_server = FALSE;\r
- s->cur_prompt->name = dupstr("SSH key passphrase");\r
- add_prompt(s->cur_prompt,\r
- dupprintf("Passphrase for key \"%.100s\": ",\r
- s->publickey_comment),\r
- FALSE, SSH_MAX_PASSWORD_LEN);\r
- ret = get_userpass_input(s->cur_prompt, NULL, 0);\r
- while (ret < 0) {\r
- ssh->send_ok = 1;\r
- crWaitUntil(!pktin);\r
- ret = get_userpass_input(s->cur_prompt, in, inlen);\r
- ssh->send_ok = 0;\r
- }\r
- if (!ret) {\r
- /* Failed to get a passphrase. Terminate. */\r
- free_prompts(s->cur_prompt);\r
- ssh_disconnect(ssh, NULL, "Unable to authenticate",\r
- 0, TRUE);\r
- crStop(0);\r
- }\r
- passphrase = dupstr(s->cur_prompt->prompts[0]->result);\r
- free_prompts(s->cur_prompt);\r
- }\r
- /*\r
- * Try decrypting key with passphrase.\r
- */\r
- ret = loadrsakey(&ssh->cfg.keyfile, &s->key, passphrase,\r
- &error);\r
- if (passphrase) {\r
- memset(passphrase, 0, strlen(passphrase));\r
- sfree(passphrase);\r
- }\r
- if (ret == 1) {\r
- /* Correct passphrase. */\r
- got_passphrase = TRUE;\r
- } else if (ret == 0) {\r
- c_write_str(ssh, "Couldn't load private key from ");\r
- c_write_str(ssh, filename_to_str(&ssh->cfg.keyfile));\r
- c_write_str(ssh, " (");\r
- c_write_str(ssh, error);\r
- c_write_str(ssh, ").\r\n");\r
- got_passphrase = FALSE;\r
- break; /* go and try something else */\r
- } else if (ret == -1) {\r
- c_write_str(ssh, "Wrong passphrase.\r\n"); /* FIXME */\r
- got_passphrase = FALSE;\r
- /* and try again */\r
- } else {\r
- assert(0 && "unexpected return from loadrsakey()");\r
- got_passphrase = FALSE; /* placate optimisers */\r
- }\r
- }\r
-\r
- if (got_passphrase) {\r
-\r
- /*\r
- * Send a public key attempt.\r
- */\r
- send_packet(ssh, SSH1_CMSG_AUTH_RSA,\r
- PKT_BIGNUM, s->key.modulus, PKT_END);\r
-\r
- crWaitUntil(pktin);\r
- if (pktin->type == SSH1_SMSG_FAILURE) {\r
- c_write_str(ssh, "Server refused our public key.\r\n");\r
- continue; /* go and try something else */\r
- }\r
- if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) {\r
- bombout(("Bizarre response to offer of public key"));\r
- crStop(0);\r
- }\r
-\r
- {\r
- int i;\r
- unsigned char buffer[32];\r
- Bignum challenge, response;\r
-\r
- if ((challenge = ssh1_pkt_getmp(pktin)) == NULL) {\r
- bombout(("Server's RSA challenge was badly formatted"));\r
- crStop(0);\r
- }\r
- response = rsadecrypt(challenge, &s->key);\r
- freebn(s->key.private_exponent);/* burn the evidence */\r
-\r
- for (i = 0; i < 32; i++) {\r
- buffer[i] = bignum_byte(response, 31 - i);\r
- }\r
-\r
- MD5Init(&md5c);\r
- MD5Update(&md5c, buffer, 32);\r
- MD5Update(&md5c, s->session_id, 16);\r
- MD5Final(buffer, &md5c);\r
-\r
- send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE,\r
- PKT_DATA, buffer, 16, PKT_END);\r
-\r
- freebn(challenge);\r
- freebn(response);\r
- }\r
-\r
- crWaitUntil(pktin);\r
- if (pktin->type == SSH1_SMSG_FAILURE) {\r
- if (flags & FLAG_VERBOSE)\r
- c_write_str(ssh, "Failed to authenticate with"\r
- " our public key.\r\n");\r
- continue; /* go and try something else */\r
- } else if (pktin->type != SSH1_SMSG_SUCCESS) {\r
- bombout(("Bizarre response to RSA authentication response"));\r
- crStop(0);\r
- }\r
-\r
- break; /* we're through! */\r
- }\r
-\r
- }\r
-\r
- /*\r
- * Otherwise, try various forms of password-like authentication.\r
- */\r
- s->cur_prompt = new_prompts(ssh->frontend);\r
-\r
- if (ssh->cfg.try_tis_auth &&\r
- (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) &&\r
- !s->tis_auth_refused) {\r
- s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE;\r
- logevent("Requested TIS authentication");\r
- send_packet(ssh, SSH1_CMSG_AUTH_TIS, PKT_END);\r
- crWaitUntil(pktin);\r
- if (pktin->type != SSH1_SMSG_AUTH_TIS_CHALLENGE) {\r
- logevent("TIS authentication declined");\r
- if (flags & FLAG_INTERACTIVE)\r
- c_write_str(ssh, "TIS authentication refused.\r\n");\r
- s->tis_auth_refused = 1;\r
- continue;\r
- } else {\r
- char *challenge;\r
- int challengelen;\r
- char *instr_suf, *prompt;\r
-\r
- ssh_pkt_getstring(pktin, &challenge, &challengelen);\r
- if (!challenge) {\r
- bombout(("TIS challenge packet was badly formed"));\r
- crStop(0);\r
- }\r
- logevent("Received TIS challenge");\r
- s->cur_prompt->to_server = TRUE;\r
- s->cur_prompt->name = dupstr("SSH TIS authentication");\r
- /* Prompt heuristic comes from OpenSSH */\r
- if (memchr(challenge, '\n', challengelen)) {\r
- instr_suf = dupstr("");\r
- prompt = dupprintf("%.*s", challengelen, challenge);\r
- } else {\r
- instr_suf = dupprintf("%.*s", challengelen, challenge);\r
- prompt = dupstr("Response: ");\r
- }\r
- s->cur_prompt->instruction =\r
- dupprintf("Using TIS authentication.%s%s",\r
- (*instr_suf) ? "\n" : "",\r
- instr_suf);\r
- s->cur_prompt->instr_reqd = TRUE;\r
- add_prompt(s->cur_prompt, prompt, FALSE, SSH_MAX_PASSWORD_LEN);\r
- sfree(instr_suf);\r
- }\r
- }\r
- if (ssh->cfg.try_tis_auth &&\r
- (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) &&\r
- !s->ccard_auth_refused) {\r
- s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE;\r
- logevent("Requested CryptoCard authentication");\r
- send_packet(ssh, SSH1_CMSG_AUTH_CCARD, PKT_END);\r
- crWaitUntil(pktin);\r
- if (pktin->type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) {\r
- logevent("CryptoCard authentication declined");\r
- c_write_str(ssh, "CryptoCard authentication refused.\r\n");\r
- s->ccard_auth_refused = 1;\r
- continue;\r
- } else {\r
- char *challenge;\r
- int challengelen;\r
- char *instr_suf, *prompt;\r
-\r
- ssh_pkt_getstring(pktin, &challenge, &challengelen);\r
- if (!challenge) {\r
- bombout(("CryptoCard challenge packet was badly formed"));\r
- crStop(0);\r
- }\r
- logevent("Received CryptoCard challenge");\r
- s->cur_prompt->to_server = TRUE;\r
- s->cur_prompt->name = dupstr("SSH CryptoCard authentication");\r
- s->cur_prompt->name_reqd = FALSE;\r
- /* Prompt heuristic comes from OpenSSH */\r
- if (memchr(challenge, '\n', challengelen)) {\r
- instr_suf = dupstr("");\r
- prompt = dupprintf("%.*s", challengelen, challenge);\r
- } else {\r
- instr_suf = dupprintf("%.*s", challengelen, challenge);\r
- prompt = dupstr("Response: ");\r
- }\r
- s->cur_prompt->instruction =\r
- dupprintf("Using CryptoCard authentication.%s%s",\r
- (*instr_suf) ? "\n" : "",\r
- instr_suf);\r
- s->cur_prompt->instr_reqd = TRUE;\r
- add_prompt(s->cur_prompt, prompt, FALSE, SSH_MAX_PASSWORD_LEN);\r
- sfree(instr_suf);\r
- }\r
- }\r
- if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {\r
- if ((s->supported_auths_mask & (1 << SSH1_AUTH_PASSWORD)) == 0) {\r
- bombout(("No supported authentication methods available"));\r
- crStop(0);\r
- }\r
- s->cur_prompt->to_server = TRUE;\r
- s->cur_prompt->name = dupstr("SSH password");\r
- add_prompt(s->cur_prompt, dupprintf("%.90s@%.90s's password: ",\r
- s->username, ssh->savedhost),\r
- FALSE, SSH_MAX_PASSWORD_LEN);\r
- }\r
-\r
- /*\r
- * Show password prompt, having first obtained it via a TIS\r
- * or CryptoCard exchange if we're doing TIS or CryptoCard\r
- * authentication.\r
- */\r
- {\r
- int ret; /* need not be kept over crReturn */\r
- ret = get_userpass_input(s->cur_prompt, NULL, 0);\r
- while (ret < 0) {\r
- ssh->send_ok = 1;\r
- crWaitUntil(!pktin);\r
- ret = get_userpass_input(s->cur_prompt, in, inlen);\r
- ssh->send_ok = 0;\r
- }\r
- if (!ret) {\r
- /*\r
- * Failed to get a password (for example\r
- * because one was supplied on the command line\r
- * which has already failed to work). Terminate.\r
- */\r
- free_prompts(s->cur_prompt);\r
- ssh_disconnect(ssh, NULL, "Unable to authenticate", 0, TRUE);\r
- crStop(0);\r
- }\r
- }\r
-\r
- if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) {\r
- /*\r
- * Defence against traffic analysis: we send a\r
- * whole bunch of packets containing strings of\r
- * different lengths. One of these strings is the\r
- * password, in a SSH1_CMSG_AUTH_PASSWORD packet.\r
- * The others are all random data in\r
- * SSH1_MSG_IGNORE packets. This way a passive\r
- * listener can't tell which is the password, and\r
- * hence can't deduce the password length.\r
- * \r
- * Anybody with a password length greater than 16\r
- * bytes is going to have enough entropy in their\r
- * password that a listener won't find it _that_\r
- * much help to know how long it is. So what we'll\r
- * do is:\r
- * \r
- * - if password length < 16, we send 15 packets\r
- * containing string lengths 1 through 15\r
- * \r
- * - otherwise, we let N be the nearest multiple\r
- * of 8 below the password length, and send 8\r
- * packets containing string lengths N through\r
- * N+7. This won't obscure the order of\r
- * magnitude of the password length, but it will\r
- * introduce a bit of extra uncertainty.\r
- * \r
- * A few servers can't deal with SSH1_MSG_IGNORE, at\r
- * least in this context. For these servers, we need\r
- * an alternative defence. We make use of the fact\r
- * that the password is interpreted as a C string:\r
- * so we can append a NUL, then some random data.\r
- * \r
- * A few servers can deal with neither SSH1_MSG_IGNORE\r
- * here _nor_ a padded password string.\r
- * For these servers we are left with no defences\r
- * against password length sniffing.\r
- */\r
- if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE) &&\r
- !(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) {\r
- /*\r
- * The server can deal with SSH1_MSG_IGNORE, so\r
- * we can use the primary defence.\r
- */\r
- int bottom, top, pwlen, i;\r
- char *randomstr;\r
-\r
- pwlen = strlen(s->cur_prompt->prompts[0]->result);\r
- if (pwlen < 16) {\r
- bottom = 0; /* zero length passwords are OK! :-) */\r
- top = 15;\r
- } else {\r
- bottom = pwlen & ~7;\r
- top = bottom + 7;\r
- }\r
-\r
- assert(pwlen >= bottom && pwlen <= top);\r
-\r
- randomstr = snewn(top + 1, char);\r
-\r
- for (i = bottom; i <= top; i++) {\r
- if (i == pwlen) {\r
- defer_packet(ssh, s->pwpkt_type,\r
- PKTT_PASSWORD, PKT_STR,\r
- s->cur_prompt->prompts[0]->result,\r
- PKTT_OTHER, PKT_END);\r
- } else {\r
- for (j = 0; j < i; j++) {\r
- do {\r
- randomstr[j] = random_byte();\r
- } while (randomstr[j] == '\0');\r
- }\r
- randomstr[i] = '\0';\r
- defer_packet(ssh, SSH1_MSG_IGNORE,\r
- PKT_STR, randomstr, PKT_END);\r
- }\r
- }\r
- logevent("Sending password with camouflage packets");\r
- ssh_pkt_defersend(ssh);\r
- sfree(randomstr);\r
- } \r
- else if (!(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) {\r
- /*\r
- * The server can't deal with SSH1_MSG_IGNORE\r
- * but can deal with padded passwords, so we\r
- * can use the secondary defence.\r
- */\r
- char string[64];\r
- char *ss;\r
- int len;\r
-\r
- len = strlen(s->cur_prompt->prompts[0]->result);\r
- if (len < sizeof(string)) {\r
- ss = string;\r
- strcpy(string, s->cur_prompt->prompts[0]->result);\r
- len++; /* cover the zero byte */\r
- while (len < sizeof(string)) {\r
- string[len++] = (char) random_byte();\r
- }\r
- } else {\r
- ss = s->cur_prompt->prompts[0]->result;\r
- }\r
- logevent("Sending length-padded password");\r
- send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD,\r
- PKT_INT, len, PKT_DATA, ss, len,\r
- PKTT_OTHER, PKT_END);\r
- } else {\r
- /*\r
- * The server is believed unable to cope with\r
- * any of our password camouflage methods.\r
- */\r
- int len;\r
- len = strlen(s->cur_prompt->prompts[0]->result);\r
- logevent("Sending unpadded password");\r
- send_packet(ssh, s->pwpkt_type,\r
- PKTT_PASSWORD, PKT_INT, len,\r
- PKT_DATA, s->cur_prompt->prompts[0]->result, len,\r
- PKTT_OTHER, PKT_END);\r
- }\r
- } else {\r
- send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD,\r
- PKT_STR, s->cur_prompt->prompts[0]->result,\r
- PKTT_OTHER, PKT_END);\r
- }\r
- logevent("Sent password");\r
- free_prompts(s->cur_prompt);\r
- crWaitUntil(pktin);\r
- if (pktin->type == SSH1_SMSG_FAILURE) {\r
- if (flags & FLAG_VERBOSE)\r
- c_write_str(ssh, "Access denied\r\n");\r
- logevent("Authentication refused");\r
- } else if (pktin->type != SSH1_SMSG_SUCCESS) {\r
- bombout(("Strange packet received, type %d", pktin->type));\r
- crStop(0);\r
- }\r
- }\r
-\r
- /* Clear up */\r
- if (s->publickey_blob) {\r
- sfree(s->publickey_blob);\r
- sfree(s->publickey_comment);\r
- }\r
-\r
- logevent("Authentication successful");\r
-\r
- crFinish(1);\r
-}\r
-\r
-void sshfwd_close(struct ssh_channel *c)\r
-{\r
- Ssh ssh = c->ssh;\r
-\r
- if (ssh->state == SSH_STATE_CLOSED)\r
- return;\r
-\r
- if (!c->closes) {\r
- /*\r
- * If halfopen is true, we have sent\r
- * CHANNEL_OPEN for this channel, but it hasn't even been\r
- * acknowledged by the server. So we must set a close flag\r
- * on it now, and then when the server acks the channel\r
- * open, we can close it then.\r
- */\r
- if (!c->halfopen) {\r
- if (ssh->version == 1) {\r
- send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid,\r
- PKT_END);\r
- c->closes = 1; /* sent MSG_CLOSE */\r
- } else {\r
- int bytes_to_send = bufchain_size(&c->v.v2.outbuffer);\r
- if (bytes_to_send > 0) {\r
- /*\r
- * If we still have unsent data in our outgoing\r
- * buffer for this channel, we can't actually\r
- * initiate a close operation yet or that data\r
- * will be lost. Instead, set the pending_close\r
- * flag so that when we do clear the buffer\r
- * we'll start closing the channel.\r
- */\r
- char logmsg[160] = {'\0'};\r
- sprintf(\r
- logmsg,\r
- "Forwarded port pending to be closed : "\r
- "%d bytes remaining",\r
- bytes_to_send);\r
- logevent(logmsg);\r
-\r
- c->pending_close = TRUE;\r
- } else {\r
- /*\r
- * No locally buffered data, so we can send the\r
- * close message immediately.\r
- */\r
- struct Packet *pktout;\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_send(ssh, pktout);\r
- c->closes = 1; /* sent MSG_CLOSE */\r
- logevent("Nothing left to send, closing channel");\r
- }\r
- }\r
- }\r
-\r
- if (c->type == CHAN_X11) {\r
- c->u.x11.s = NULL;\r
- logevent("Forwarded X11 connection terminated");\r
- } else if (c->type == CHAN_SOCKDATA ||\r
- c->type == CHAN_SOCKDATA_DORMANT) {\r
- c->u.pfd.s = NULL;\r
- logevent("Forwarded port closed");\r
- }\r
- }\r
-}\r
-\r
-int sshfwd_write(struct ssh_channel *c, char *buf, int len)\r
-{\r
- Ssh ssh = c->ssh;\r
-\r
- if (ssh->state == SSH_STATE_CLOSED)\r
- return 0;\r
-\r
- if (ssh->version == 1) {\r
- send_packet(ssh, SSH1_MSG_CHANNEL_DATA,\r
- PKT_INT, c->remoteid,\r
- PKT_INT, len, PKTT_DATA, PKT_DATA, buf, len,\r
- PKTT_OTHER, PKT_END);\r
- /*\r
- * In SSH-1 we can return 0 here - implying that forwarded\r
- * connections are never individually throttled - because\r
- * the only circumstance that can cause throttling will be\r
- * the whole SSH connection backing up, in which case\r
- * _everything_ will be throttled as a whole.\r
- */\r
- return 0;\r
- } else {\r
- ssh2_add_channel_data(c, buf, len);\r
- return ssh2_try_send(c);\r
- }\r
-}\r
-\r
-void sshfwd_unthrottle(struct ssh_channel *c, int bufsize)\r
-{\r
- Ssh ssh = c->ssh;\r
- int buflimit;\r
-\r
- if (ssh->state == SSH_STATE_CLOSED)\r
- return;\r
-\r
- if (ssh->version == 1) {\r
- buflimit = SSH1_BUFFER_LIMIT;\r
- } else {\r
- buflimit = c->v.v2.locmaxwin;\r
- ssh2_set_window(c, bufsize < buflimit ? buflimit - bufsize : 0);\r
- }\r
- if (c->throttling_conn && bufsize <= buflimit) {\r
- c->throttling_conn = 0;\r
- ssh_throttle_conn(ssh, -1);\r
- }\r
-}\r
-\r
-static void ssh_queueing_handler(Ssh ssh, struct Packet *pktin)\r
-{\r
- struct queued_handler *qh = ssh->qhead;\r
-\r
- assert(qh != NULL);\r
-\r
- assert(pktin->type == qh->msg1 || pktin->type == qh->msg2);\r
-\r
- if (qh->msg1 > 0) {\r
- assert(ssh->packet_dispatch[qh->msg1] == ssh_queueing_handler);\r
- ssh->packet_dispatch[qh->msg1] = NULL;\r
- }\r
- if (qh->msg2 > 0) {\r
- assert(ssh->packet_dispatch[qh->msg2] == ssh_queueing_handler);\r
- ssh->packet_dispatch[qh->msg2] = NULL;\r
- }\r
-\r
- if (qh->next) {\r
- ssh->qhead = qh->next;\r
-\r
- if (ssh->qhead->msg1 > 0) {\r
- assert(ssh->packet_dispatch[ssh->qhead->msg1] == NULL);\r
- ssh->packet_dispatch[ssh->qhead->msg1] = ssh_queueing_handler;\r
- }\r
- if (ssh->qhead->msg2 > 0) {\r
- assert(ssh->packet_dispatch[ssh->qhead->msg2] == NULL);\r
- ssh->packet_dispatch[ssh->qhead->msg2] = ssh_queueing_handler;\r
- }\r
- } else {\r
- ssh->qhead = ssh->qtail = NULL;\r
- ssh->packet_dispatch[pktin->type] = NULL;\r
- }\r
-\r
- qh->handler(ssh, pktin, qh->ctx);\r
-\r
- sfree(qh);\r
-}\r
-\r
-static void ssh_queue_handler(Ssh ssh, int msg1, int msg2,\r
- chandler_fn_t handler, void *ctx)\r
-{\r
- struct queued_handler *qh;\r
-\r
- qh = snew(struct queued_handler);\r
- qh->msg1 = msg1;\r
- qh->msg2 = msg2;\r
- qh->handler = handler;\r
- qh->ctx = ctx;\r
- qh->next = NULL;\r
-\r
- if (ssh->qtail == NULL) {\r
- ssh->qhead = qh;\r
-\r
- if (qh->msg1 > 0) {\r
- assert(ssh->packet_dispatch[qh->msg1] == NULL);\r
- ssh->packet_dispatch[qh->msg1] = ssh_queueing_handler;\r
- }\r
- if (qh->msg2 > 0) {\r
- assert(ssh->packet_dispatch[qh->msg2] == NULL);\r
- ssh->packet_dispatch[qh->msg2] = ssh_queueing_handler;\r
- }\r
- } else {\r
- ssh->qtail->next = qh;\r
- }\r
- ssh->qtail = qh;\r
-}\r
-\r
-static void ssh_rportfwd_succfail(Ssh ssh, struct Packet *pktin, void *ctx)\r
-{\r
- struct ssh_rportfwd *rpf, *pf = (struct ssh_rportfwd *)ctx;\r
-\r
- if (pktin->type == (ssh->version == 1 ? SSH1_SMSG_SUCCESS :\r
- SSH2_MSG_REQUEST_SUCCESS)) {\r
- logeventf(ssh, "Remote port forwarding from %s enabled",\r
- pf->sportdesc);\r
- } else {\r
- logeventf(ssh, "Remote port forwarding from %s refused",\r
- pf->sportdesc);\r
-\r
- rpf = del234(ssh->rportfwds, pf);\r
- assert(rpf == pf);\r
- pf->pfrec->remote = NULL;\r
- free_rportfwd(pf);\r
- }\r
-}\r
-\r
-static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)\r
-{\r
- const char *portfwd_strptr = cfg->portfwd;\r
- struct ssh_portfwd *epf;\r
- int i;\r
-\r
- if (!ssh->portfwds) {\r
- ssh->portfwds = newtree234(ssh_portcmp);\r
- } else {\r
- /*\r
- * Go through the existing port forwardings and tag them\r
- * with status==DESTROY. Any that we want to keep will be\r
- * re-enabled (status==KEEP) as we go through the\r
- * configuration and find out which bits are the same as\r
- * they were before.\r
- */\r
- struct ssh_portfwd *epf;\r
- int i;\r
- for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++)\r
- epf->status = DESTROY;\r
- }\r
-\r
- while (*portfwd_strptr) {\r
- char address_family, type;\r
- int sport,dport,sserv,dserv;\r
- char sports[256], dports[256], saddr[256], host[256];\r
- int n;\r
-\r
- address_family = 'A';\r
- type = 'L';\r
- if (*portfwd_strptr == 'A' ||\r
- *portfwd_strptr == '4' ||\r
- *portfwd_strptr == '6')\r
- address_family = *portfwd_strptr++;\r
- if (*portfwd_strptr == 'L' ||\r
- *portfwd_strptr == 'R' ||\r
- *portfwd_strptr == 'D')\r
- type = *portfwd_strptr++;\r
-\r
- saddr[0] = '\0';\r
-\r
- n = 0;\r
- while (*portfwd_strptr && *portfwd_strptr != '\t') {\r
- if (*portfwd_strptr == ':') {\r
- /*\r
- * We've seen a colon in the middle of the\r
- * source port number. This means that\r
- * everything we've seen until now is the\r
- * source _address_, so we'll move it into\r
- * saddr and start sports from the beginning\r
- * again.\r
- */\r
- portfwd_strptr++;\r
- sports[n] = '\0';\r
- if (ssh->version == 1 && type == 'R') {\r
- logeventf(ssh, "SSH-1 cannot handle remote source address "\r
- "spec \"%s\"; ignoring", sports);\r
- } else\r
- strcpy(saddr, sports);\r
- n = 0;\r
- }\r
- if (n < lenof(sports)-1) sports[n++] = *portfwd_strptr++;\r
- }\r
- sports[n] = 0;\r
- if (type != 'D') {\r
- if (*portfwd_strptr == '\t')\r
- portfwd_strptr++;\r
- n = 0;\r
- while (*portfwd_strptr && *portfwd_strptr != ':') {\r
- if (n < lenof(host)-1) host[n++] = *portfwd_strptr++;\r
- }\r
- host[n] = 0;\r
- if (*portfwd_strptr == ':')\r
- portfwd_strptr++;\r
- n = 0;\r
- while (*portfwd_strptr) {\r
- if (n < lenof(dports)-1) dports[n++] = *portfwd_strptr++;\r
- }\r
- dports[n] = 0;\r
- portfwd_strptr++;\r
- dport = atoi(dports);\r
- dserv = 0;\r
- if (dport == 0) {\r
- dserv = 1;\r
- dport = net_service_lookup(dports);\r
- if (!dport) {\r
- logeventf(ssh, "Service lookup failed for destination"\r
- " port \"%s\"", dports);\r
- }\r
- }\r
- } else {\r
- while (*portfwd_strptr) portfwd_strptr++;\r
- host[0] = 0;\r
- dports[0] = 0;\r
- dport = dserv = -1;\r
- portfwd_strptr++; /* eat the NUL and move to next one */\r
- }\r
- sport = atoi(sports);\r
- sserv = 0;\r
- if (sport == 0) {\r
- sserv = 1;\r
- sport = net_service_lookup(sports);\r
- if (!sport) {\r
- logeventf(ssh, "Service lookup failed for source"\r
- " port \"%s\"", sports);\r
- }\r
- }\r
- if (sport && dport) {\r
- /* Set up a description of the source port. */\r
- struct ssh_portfwd *pfrec, *epfrec;\r
-\r
- pfrec = snew(struct ssh_portfwd);\r
- pfrec->type = type;\r
- pfrec->saddr = *saddr ? dupstr(saddr) : NULL;\r
- pfrec->sserv = sserv ? dupstr(sports) : NULL;\r
- pfrec->sport = sport;\r
- pfrec->daddr = *host ? dupstr(host) : NULL;\r
- pfrec->dserv = dserv ? dupstr(dports) : NULL;\r
- pfrec->dport = dport;\r
- pfrec->local = NULL;\r
- pfrec->remote = NULL;\r
- pfrec->addressfamily = (address_family == '4' ? ADDRTYPE_IPV4 :\r
- address_family == '6' ? ADDRTYPE_IPV6 :\r
- ADDRTYPE_UNSPEC);\r
-\r
- epfrec = add234(ssh->portfwds, pfrec);\r
- if (epfrec != pfrec) {\r
- if (epfrec->status == DESTROY) {\r
- /*\r
- * We already have a port forwarding up and running\r
- * with precisely these parameters. Hence, no need\r
- * to do anything; simply re-tag the existing one\r
- * as KEEP.\r
- */\r
- epfrec->status = KEEP;\r
- }\r
- /*\r
- * Anything else indicates that there was a duplicate\r
- * in our input, which we'll silently ignore.\r
- */\r
- free_portfwd(pfrec);\r
- } else {\r
- pfrec->status = CREATE;\r
- }\r
- }\r
- }\r
-\r
- /*\r
- * Now go through and destroy any port forwardings which were\r
- * not re-enabled.\r
- */\r
- for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++)\r
- if (epf->status == DESTROY) {\r
- char *message;\r
-\r
- message = dupprintf("%s port forwarding from %s%s%d",\r
- epf->type == 'L' ? "local" :\r
- epf->type == 'R' ? "remote" : "dynamic",\r
- epf->saddr ? epf->saddr : "",\r
- epf->saddr ? ":" : "",\r
- epf->sport);\r
-\r
- if (epf->type != 'D') {\r
- char *msg2 = dupprintf("%s to %s:%d", message,\r
- epf->daddr, epf->dport);\r
- sfree(message);\r
- message = msg2;\r
- }\r
-\r
- logeventf(ssh, "Cancelling %s", message);\r
- sfree(message);\r
-\r
- /* epf->remote or epf->local may be NULL if setting up a\r
- * forwarding failed. */\r
- if (epf->remote) {\r
- struct ssh_rportfwd *rpf = epf->remote;\r
- struct Packet *pktout;\r
-\r
- /*\r
- * Cancel the port forwarding at the server\r
- * end.\r
- */\r
- if (ssh->version == 1) {\r
- /*\r
- * We cannot cancel listening ports on the\r
- * server side in SSH-1! There's no message\r
- * to support it. Instead, we simply remove\r
- * the rportfwd record from the local end\r
- * so that any connections the server tries\r
- * to make on it are rejected.\r
- */\r
- } else {\r
- pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST);\r
- ssh2_pkt_addstring(pktout, "cancel-tcpip-forward");\r
- ssh2_pkt_addbool(pktout, 0);/* _don't_ want reply */\r
- if (epf->saddr) {\r
- ssh2_pkt_addstring(pktout, epf->saddr);\r
- } else if (ssh->cfg.rport_acceptall) {\r
- /* XXX: ssh->cfg.rport_acceptall may not represent\r
- * what was used to open the original connection,\r
- * since it's reconfigurable. */\r
- ssh2_pkt_addstring(pktout, "0.0.0.0");\r
- } else {\r
- ssh2_pkt_addstring(pktout, "127.0.0.1");\r
- }\r
- ssh2_pkt_adduint32(pktout, epf->sport);\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
-\r
- del234(ssh->rportfwds, rpf);\r
- free_rportfwd(rpf);\r
- } else if (epf->local) {\r
- pfd_terminate(epf->local);\r
- }\r
-\r
- delpos234(ssh->portfwds, i);\r
- free_portfwd(epf);\r
- i--; /* so we don't skip one in the list */\r
- }\r
-\r
- /*\r
- * And finally, set up any new port forwardings (status==CREATE).\r
- */\r
- for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++)\r
- if (epf->status == CREATE) {\r
- char *sportdesc, *dportdesc;\r
- sportdesc = dupprintf("%s%s%s%s%d%s",\r
- epf->saddr ? epf->saddr : "",\r
- epf->saddr ? ":" : "",\r
- epf->sserv ? epf->sserv : "",\r
- epf->sserv ? "(" : "",\r
- epf->sport,\r
- epf->sserv ? ")" : "");\r
- if (epf->type == 'D') {\r
- dportdesc = NULL;\r
- } else {\r
- dportdesc = dupprintf("%s:%s%s%d%s",\r
- epf->daddr,\r
- epf->dserv ? epf->dserv : "",\r
- epf->dserv ? "(" : "",\r
- epf->dport,\r
- epf->dserv ? ")" : "");\r
- }\r
-\r
- if (epf->type == 'L') {\r
- const char *err = pfd_addforward(epf->daddr, epf->dport,\r
- epf->saddr, epf->sport,\r
- ssh, cfg,\r
- &epf->local,\r
- epf->addressfamily);\r
-\r
- logeventf(ssh, "Local %sport %s forwarding to %s%s%s",\r
- epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " :\r
- epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "",\r
- sportdesc, dportdesc,\r
- err ? " failed: " : "", err ? err : "");\r
- } else if (epf->type == 'D') {\r
- const char *err = pfd_addforward(NULL, -1,\r
- epf->saddr, epf->sport,\r
- ssh, cfg,\r
- &epf->local,\r
- epf->addressfamily);\r
-\r
- logeventf(ssh, "Local %sport %s SOCKS dynamic forwarding%s%s",\r
- epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " :\r
- epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "",\r
- sportdesc,\r
- err ? " failed: " : "", err ? err : "");\r
- } else {\r
- struct ssh_rportfwd *pf;\r
-\r
- /*\r
- * Ensure the remote port forwardings tree exists.\r
- */\r
- if (!ssh->rportfwds) {\r
- if (ssh->version == 1)\r
- ssh->rportfwds = newtree234(ssh_rportcmp_ssh1);\r
- else\r
- ssh->rportfwds = newtree234(ssh_rportcmp_ssh2);\r
- }\r
-\r
- pf = snew(struct ssh_rportfwd);\r
- strncpy(pf->dhost, epf->daddr, lenof(pf->dhost)-1);\r
- pf->dhost[lenof(pf->dhost)-1] = '\0';\r
- pf->dport = epf->dport;\r
- pf->sport = epf->sport;\r
- if (add234(ssh->rportfwds, pf) != pf) {\r
- logeventf(ssh, "Duplicate remote port forwarding to %s:%d",\r
- epf->daddr, epf->dport);\r
- sfree(pf);\r
- } else {\r
- logeventf(ssh, "Requesting remote port %s"\r
- " forward to %s", sportdesc, dportdesc);\r
-\r
- pf->sportdesc = sportdesc;\r
- sportdesc = NULL;\r
- epf->remote = pf;\r
- pf->pfrec = epf;\r
-\r
- if (ssh->version == 1) {\r
- send_packet(ssh, SSH1_CMSG_PORT_FORWARD_REQUEST,\r
- PKT_INT, epf->sport,\r
- PKT_STR, epf->daddr,\r
- PKT_INT, epf->dport,\r
- PKT_END);\r
- ssh_queue_handler(ssh, SSH1_SMSG_SUCCESS,\r
- SSH1_SMSG_FAILURE,\r
- ssh_rportfwd_succfail, pf);\r
- } else {\r
- struct Packet *pktout;\r
- pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST);\r
- ssh2_pkt_addstring(pktout, "tcpip-forward");\r
- ssh2_pkt_addbool(pktout, 1);/* want reply */\r
- if (epf->saddr) {\r
- ssh2_pkt_addstring(pktout, epf->saddr);\r
- } else if (cfg->rport_acceptall) {\r
- ssh2_pkt_addstring(pktout, "0.0.0.0");\r
- } else {\r
- ssh2_pkt_addstring(pktout, "127.0.0.1");\r
- }\r
- ssh2_pkt_adduint32(pktout, epf->sport);\r
- ssh2_pkt_send(ssh, pktout);\r
-\r
- ssh_queue_handler(ssh, SSH2_MSG_REQUEST_SUCCESS,\r
- SSH2_MSG_REQUEST_FAILURE,\r
- ssh_rportfwd_succfail, pf);\r
- }\r
- }\r
- }\r
- sfree(sportdesc);\r
- sfree(dportdesc);\r
- }\r
-}\r
-\r
-static void ssh1_smsg_stdout_stderr_data(Ssh ssh, struct Packet *pktin)\r
-{\r
- char *string;\r
- int stringlen, bufsize;\r
-\r
- ssh_pkt_getstring(pktin, &string, &stringlen);\r
- if (string == NULL) {\r
- bombout(("Incoming terminal data packet was badly formed"));\r
- return;\r
- }\r
-\r
- bufsize = from_backend(ssh->frontend, pktin->type == SSH1_SMSG_STDERR_DATA,\r
- string, stringlen);\r
- if (!ssh->v1_stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) {\r
- ssh->v1_stdout_throttling = 1;\r
- ssh_throttle_conn(ssh, +1);\r
- }\r
-}\r
-\r
-static void ssh1_smsg_x11_open(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* Remote side is trying to open a channel to talk to our\r
- * X-Server. Give them back a local channel number. */\r
- struct ssh_channel *c;\r
- int remoteid = ssh_pkt_getuint32(pktin);\r
-\r
- logevent("Received X11 connect request");\r
- /* Refuse if X11 forwarding is disabled. */\r
- if (!ssh->X11_fwd_enabled) {\r
- send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,\r
- PKT_INT, remoteid, PKT_END);\r
- logevent("Rejected X11 connect request");\r
- } else {\r
- c = snew(struct ssh_channel);\r
- c->ssh = ssh;\r
-\r
- if (x11_init(&c->u.x11.s, ssh->x11disp, c,\r
- NULL, -1, &ssh->cfg) != NULL) {\r
- logevent("Opening X11 forward connection failed");\r
- sfree(c);\r
- send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,\r
- PKT_INT, remoteid, PKT_END);\r
- } else {\r
- logevent\r
- ("Opening X11 forward connection succeeded");\r
- c->remoteid = remoteid;\r
- c->halfopen = FALSE;\r
- c->localid = alloc_channel_id(ssh);\r
- c->closes = 0;\r
- c->pending_close = FALSE;\r
- c->throttling_conn = 0;\r
- c->type = CHAN_X11; /* identify channel type */\r
- add234(ssh->channels, c);\r
- send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,\r
- PKT_INT, c->remoteid, PKT_INT,\r
- c->localid, PKT_END);\r
- logevent("Opened X11 forward channel");\r
- }\r
- }\r
-}\r
-\r
-static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* Remote side is trying to open a channel to talk to our\r
- * agent. Give them back a local channel number. */\r
- struct ssh_channel *c;\r
- int remoteid = ssh_pkt_getuint32(pktin);\r
-\r
- /* Refuse if agent forwarding is disabled. */\r
- if (!ssh->agentfwd_enabled) {\r
- send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,\r
- PKT_INT, remoteid, PKT_END);\r
- } else {\r
- c = snew(struct ssh_channel);\r
- c->ssh = ssh;\r
- c->remoteid = remoteid;\r
- c->halfopen = FALSE;\r
- c->localid = alloc_channel_id(ssh);\r
- c->closes = 0;\r
- c->pending_close = FALSE;\r
- c->throttling_conn = 0;\r
- c->type = CHAN_AGENT; /* identify channel type */\r
- c->u.a.lensofar = 0;\r
- add234(ssh->channels, c);\r
- send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,\r
- PKT_INT, c->remoteid, PKT_INT, c->localid,\r
- PKT_END);\r
- }\r
-}\r
-\r
-static void ssh1_msg_port_open(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* Remote side is trying to open a channel to talk to a\r
- * forwarded port. Give them back a local channel number. */\r
- struct ssh_channel *c;\r
- struct ssh_rportfwd pf, *pfp;\r
- int remoteid;\r
- int hostsize, port;\r
- char *host;\r
- const char *e;\r
- c = snew(struct ssh_channel);\r
- c->ssh = ssh;\r
-\r
- remoteid = ssh_pkt_getuint32(pktin);\r
- ssh_pkt_getstring(pktin, &host, &hostsize);\r
- port = ssh_pkt_getuint32(pktin);\r
-\r
- if (hostsize >= lenof(pf.dhost))\r
- hostsize = lenof(pf.dhost)-1;\r
- memcpy(pf.dhost, host, hostsize);\r
- pf.dhost[hostsize] = '\0';\r
- pf.dport = port;\r
- pfp = find234(ssh->rportfwds, &pf, NULL);\r
-\r
- if (pfp == NULL) {\r
- logeventf(ssh, "Rejected remote port open request for %s:%d",\r
- pf.dhost, port);\r
- send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,\r
- PKT_INT, remoteid, PKT_END);\r
- } else {\r
- logeventf(ssh, "Received remote port open request for %s:%d",\r
- pf.dhost, port);\r
- e = pfd_newconnect(&c->u.pfd.s, pf.dhost, port,\r
- c, &ssh->cfg, pfp->pfrec->addressfamily);\r
- if (e != NULL) {\r
- logeventf(ssh, "Port open failed: %s", e);\r
- sfree(c);\r
- send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,\r
- PKT_INT, remoteid, PKT_END);\r
- } else {\r
- c->remoteid = remoteid;\r
- c->halfopen = FALSE;\r
- c->localid = alloc_channel_id(ssh);\r
- c->closes = 0;\r
- c->pending_close = FALSE;\r
- c->throttling_conn = 0;\r
- c->type = CHAN_SOCKDATA; /* identify channel type */\r
- add234(ssh->channels, c);\r
- send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION,\r
- PKT_INT, c->remoteid, PKT_INT,\r
- c->localid, PKT_END);\r
- logevent("Forwarded port opened successfully");\r
- }\r
- }\r
-}\r
-\r
-static void ssh1_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin)\r
-{\r
- unsigned int remoteid = ssh_pkt_getuint32(pktin);\r
- unsigned int localid = ssh_pkt_getuint32(pktin);\r
- struct ssh_channel *c;\r
-\r
- c = find234(ssh->channels, &remoteid, ssh_channelfind);\r
- if (c && c->type == CHAN_SOCKDATA_DORMANT) {\r
- c->remoteid = localid;\r
- c->halfopen = FALSE;\r
- c->type = CHAN_SOCKDATA;\r
- c->throttling_conn = 0;\r
- pfd_confirm(c->u.pfd.s);\r
- }\r
-\r
- if (c && c->closes) {\r
- /*\r
- * We have a pending close on this channel,\r
- * which we decided on before the server acked\r
- * the channel open. So now we know the\r
- * remoteid, we can close it again.\r
- */\r
- send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE,\r
- PKT_INT, c->remoteid, PKT_END);\r
- }\r
-}\r
-\r
-static void ssh1_msg_channel_open_failure(Ssh ssh, struct Packet *pktin)\r
-{\r
- unsigned int remoteid = ssh_pkt_getuint32(pktin);\r
- struct ssh_channel *c;\r
-\r
- c = find234(ssh->channels, &remoteid, ssh_channelfind);\r
- if (c && c->type == CHAN_SOCKDATA_DORMANT) {\r
- logevent("Forwarded connection refused by server");\r
- pfd_close(c->u.pfd.s);\r
- del234(ssh->channels, c);\r
- sfree(c);\r
- }\r
-}\r
-\r
-static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* Remote side closes a channel. */\r
- unsigned i = ssh_pkt_getuint32(pktin);\r
- struct ssh_channel *c;\r
- c = find234(ssh->channels, &i, ssh_channelfind);\r
- if (c && !c->halfopen) {\r
- int closetype;\r
- closetype =\r
- (pktin->type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2);\r
-\r
- if ((c->closes == 0) && (c->type == CHAN_X11)) {\r
- logevent("Forwarded X11 connection terminated");\r
- assert(c->u.x11.s != NULL);\r
- x11_close(c->u.x11.s);\r
- c->u.x11.s = NULL;\r
- }\r
- if ((c->closes == 0) && (c->type == CHAN_SOCKDATA)) {\r
- logevent("Forwarded port closed");\r
- assert(c->u.pfd.s != NULL);\r
- pfd_close(c->u.pfd.s);\r
- c->u.pfd.s = NULL;\r
- }\r
-\r
- c->closes |= (closetype << 2); /* seen this message */\r
- if (!(c->closes & closetype)) {\r
- send_packet(ssh, pktin->type, PKT_INT, c->remoteid,\r
- PKT_END);\r
- c->closes |= closetype; /* sent it too */\r
- }\r
-\r
- if (c->closes == 15) {\r
- del234(ssh->channels, c);\r
- sfree(c);\r
- }\r
- } else {\r
- bombout(("Received CHANNEL_CLOSE%s for %s channel %d\n",\r
- pktin->type == SSH1_MSG_CHANNEL_CLOSE ? "" :\r
- "_CONFIRMATION", c ? "half-open" : "nonexistent",\r
- i));\r
- }\r
-}\r
-\r
-static void ssh1_msg_channel_data(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* Data sent down one of our channels. */\r
- int i = ssh_pkt_getuint32(pktin);\r
- char *p;\r
- int len;\r
- struct ssh_channel *c;\r
-\r
- ssh_pkt_getstring(pktin, &p, &len);\r
-\r
- c = find234(ssh->channels, &i, ssh_channelfind);\r
- if (c) {\r
- int bufsize = 0;\r
- switch (c->type) {\r
- case CHAN_X11:\r
- bufsize = x11_send(c->u.x11.s, p, len);\r
- break;\r
- case CHAN_SOCKDATA:\r
- bufsize = pfd_send(c->u.pfd.s, p, len);\r
- break;\r
- case CHAN_AGENT:\r
- /* Data for an agent message. Buffer it. */\r
- while (len > 0) {\r
- if (c->u.a.lensofar < 4) {\r
- unsigned int l = min(4 - c->u.a.lensofar, (unsigned)len);\r
- memcpy(c->u.a.msglen + c->u.a.lensofar, p,\r
- l);\r
- p += l;\r
- len -= l;\r
- c->u.a.lensofar += l;\r
- }\r
- if (c->u.a.lensofar == 4) {\r
- c->u.a.totallen =\r
- 4 + GET_32BIT(c->u.a.msglen);\r
- c->u.a.message = snewn(c->u.a.totallen,\r
- unsigned char);\r
- memcpy(c->u.a.message, c->u.a.msglen, 4);\r
- }\r
- if (c->u.a.lensofar >= 4 && len > 0) {\r
- unsigned int l =\r
- min(c->u.a.totallen - c->u.a.lensofar,\r
- (unsigned)len);\r
- memcpy(c->u.a.message + c->u.a.lensofar, p,\r
- l);\r
- p += l;\r
- len -= l;\r
- c->u.a.lensofar += l;\r
- }\r
- if (c->u.a.lensofar == c->u.a.totallen) {\r
- void *reply;\r
- int replylen;\r
- if (agent_query(c->u.a.message,\r
- c->u.a.totallen,\r
- &reply, &replylen,\r
- ssh_agentf_callback, c))\r
- ssh_agentf_callback(c, reply, replylen);\r
- sfree(c->u.a.message);\r
- c->u.a.lensofar = 0;\r
- }\r
- }\r
- bufsize = 0; /* agent channels never back up */\r
- break;\r
- }\r
- if (!c->throttling_conn && bufsize > SSH1_BUFFER_LIMIT) {\r
- c->throttling_conn = 1;\r
- ssh_throttle_conn(ssh, +1);\r
- }\r
- }\r
-}\r
-\r
-static void ssh1_smsg_exit_status(Ssh ssh, struct Packet *pktin)\r
-{\r
- ssh->exitcode = ssh_pkt_getuint32(pktin);\r
- logeventf(ssh, "Server sent command exit status %d", ssh->exitcode);\r
- send_packet(ssh, SSH1_CMSG_EXIT_CONFIRMATION, PKT_END);\r
- /*\r
- * In case `helpful' firewalls or proxies tack\r
- * extra human-readable text on the end of the\r
- * session which we might mistake for another\r
- * encrypted packet, we close the session once\r
- * we've sent EXIT_CONFIRMATION.\r
- */\r
- ssh_disconnect(ssh, NULL, NULL, 0, TRUE);\r
-}\r
-\r
-/* Helper function to deal with sending tty modes for REQUEST_PTY */\r
-static void ssh1_send_ttymode(void *data, char *mode, char *val)\r
-{\r
- struct Packet *pktout = (struct Packet *)data;\r
- int i = 0;\r
- unsigned int arg = 0;\r
- while (strcmp(mode, ssh_ttymodes[i].mode) != 0) i++;\r
- if (i == lenof(ssh_ttymodes)) return;\r
- switch (ssh_ttymodes[i].type) {\r
- case TTY_OP_CHAR:\r
- arg = ssh_tty_parse_specchar(val);\r
- break;\r
- case TTY_OP_BOOL:\r
- arg = ssh_tty_parse_boolean(val);\r
- break;\r
- }\r
- ssh2_pkt_addbyte(pktout, ssh_ttymodes[i].opcode);\r
- ssh2_pkt_addbyte(pktout, arg);\r
-}\r
-\r
-\r
-static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen,\r
- struct Packet *pktin)\r
-{\r
- crBegin(ssh->do_ssh1_connection_crstate);\r
-\r
- ssh->packet_dispatch[SSH1_SMSG_STDOUT_DATA] = \r
- ssh->packet_dispatch[SSH1_SMSG_STDERR_DATA] =\r
- ssh1_smsg_stdout_stderr_data;\r
-\r
- ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_CONFIRMATION] =\r
- ssh1_msg_channel_open_confirmation;\r
- ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_FAILURE] =\r
- ssh1_msg_channel_open_failure;\r
- ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE] =\r
- ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION] =\r
- ssh1_msg_channel_close;\r
- ssh->packet_dispatch[SSH1_MSG_CHANNEL_DATA] = ssh1_msg_channel_data;\r
- ssh->packet_dispatch[SSH1_SMSG_EXIT_STATUS] = ssh1_smsg_exit_status;\r
-\r
- if (ssh->cfg.agentfwd && agent_exists()) {\r
- logevent("Requesting agent forwarding");\r
- send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END);\r
- do {\r
- crReturnV;\r
- } while (!pktin);\r
- if (pktin->type != SSH1_SMSG_SUCCESS\r
- && pktin->type != SSH1_SMSG_FAILURE) {\r
- bombout(("Protocol confusion"));\r
- crStopV;\r
- } else if (pktin->type == SSH1_SMSG_FAILURE) {\r
- logevent("Agent forwarding refused");\r
- } else {\r
- logevent("Agent forwarding enabled");\r
- ssh->agentfwd_enabled = TRUE;\r
- ssh->packet_dispatch[SSH1_SMSG_AGENT_OPEN] = ssh1_smsg_agent_open;\r
- }\r
- }\r
-\r
- if (ssh->cfg.x11_forward &&\r
- (ssh->x11disp = x11_setup_display(ssh->cfg.x11_display,\r
- ssh->cfg.x11_auth, &ssh->cfg))) {\r
- logevent("Requesting X11 forwarding");\r
- /*\r
- * Note that while we blank the X authentication data here, we don't\r
- * take any special action to blank the start of an X11 channel,\r
- * so using MIT-MAGIC-COOKIE-1 and actually opening an X connection\r
- * without having session blanking enabled is likely to leak your\r
- * cookie into the log.\r
- */\r
- if (ssh->v1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) {\r
- send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING,\r
- PKT_STR, ssh->x11disp->remoteauthprotoname,\r
- PKTT_PASSWORD,\r
- PKT_STR, ssh->x11disp->remoteauthdatastring,\r
- PKTT_OTHER,\r
- PKT_INT, ssh->x11disp->screennum,\r
- PKT_END);\r
- } else {\r
- send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING,\r
- PKT_STR, ssh->x11disp->remoteauthprotoname,\r
- PKTT_PASSWORD,\r
- PKT_STR, ssh->x11disp->remoteauthdatastring,\r
- PKTT_OTHER,\r
- PKT_END);\r
- }\r
- do {\r
- crReturnV;\r
- } while (!pktin);\r
- if (pktin->type != SSH1_SMSG_SUCCESS\r
- && pktin->type != SSH1_SMSG_FAILURE) {\r
- bombout(("Protocol confusion"));\r
- crStopV;\r
- } else if (pktin->type == SSH1_SMSG_FAILURE) {\r
- logevent("X11 forwarding refused");\r
- } else {\r
- logevent("X11 forwarding enabled");\r
- ssh->X11_fwd_enabled = TRUE;\r
- ssh->packet_dispatch[SSH1_SMSG_X11_OPEN] = ssh1_smsg_x11_open;\r
- }\r
- }\r
-\r
- ssh_setup_portfwd(ssh, &ssh->cfg);\r
- ssh->packet_dispatch[SSH1_MSG_PORT_OPEN] = ssh1_msg_port_open;\r
-\r
- if (!ssh->cfg.nopty) {\r
- struct Packet *pkt;\r
- /* Unpick the terminal-speed string. */\r
- /* XXX perhaps we should allow no speeds to be sent. */\r
- ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */\r
- sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed);\r
- /* Send the pty request. */\r
- pkt = ssh1_pkt_init(SSH1_CMSG_REQUEST_PTY);\r
- ssh_pkt_addstring(pkt, ssh->cfg.termtype);\r
- ssh_pkt_adduint32(pkt, ssh->term_height);\r
- ssh_pkt_adduint32(pkt, ssh->term_width);\r
- ssh_pkt_adduint32(pkt, 0); /* width in pixels */\r
- ssh_pkt_adduint32(pkt, 0); /* height in pixels */\r
- parse_ttymodes(ssh, ssh->cfg.ttymodes,\r
- ssh1_send_ttymode, (void *)pkt);\r
- ssh_pkt_addbyte(pkt, SSH1_TTY_OP_ISPEED);\r
- ssh_pkt_adduint32(pkt, ssh->ispeed);\r
- ssh_pkt_addbyte(pkt, SSH1_TTY_OP_OSPEED);\r
- ssh_pkt_adduint32(pkt, ssh->ospeed);\r
- ssh_pkt_addbyte(pkt, SSH_TTY_OP_END);\r
- s_wrpkt(ssh, pkt);\r
- ssh->state = SSH_STATE_INTERMED;\r
- do {\r
- crReturnV;\r
- } while (!pktin);\r
- if (pktin->type != SSH1_SMSG_SUCCESS\r
- && pktin->type != SSH1_SMSG_FAILURE) {\r
- bombout(("Protocol confusion"));\r
- crStopV;\r
- } else if (pktin->type == SSH1_SMSG_FAILURE) {\r
- c_write_str(ssh, "Server refused to allocate pty\r\n");\r
- ssh->editing = ssh->echoing = 1;\r
- }\r
- logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)",\r
- ssh->ospeed, ssh->ispeed);\r
- } else {\r
- ssh->editing = ssh->echoing = 1;\r
- }\r
-\r
- if (ssh->cfg.compression) {\r
- send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END);\r
- do {\r
- crReturnV;\r
- } while (!pktin);\r
- if (pktin->type != SSH1_SMSG_SUCCESS\r
- && pktin->type != SSH1_SMSG_FAILURE) {\r
- bombout(("Protocol confusion"));\r
- crStopV;\r
- } else if (pktin->type == SSH1_SMSG_FAILURE) {\r
- c_write_str(ssh, "Server refused to compress\r\n");\r
- }\r
- logevent("Started compression");\r
- ssh->v1_compressing = TRUE;\r
- ssh->cs_comp_ctx = zlib_compress_init();\r
- logevent("Initialised zlib (RFC1950) compression");\r
- ssh->sc_comp_ctx = zlib_decompress_init();\r
- logevent("Initialised zlib (RFC1950) decompression");\r
- }\r
-\r
- /*\r
- * Start the shell or command.\r
- * \r
- * Special case: if the first-choice command is an SSH-2\r
- * subsystem (hence not usable here) and the second choice\r
- * exists, we fall straight back to that.\r
- */\r
- {\r
- char *cmd = ssh->cfg.remote_cmd_ptr;\r
-\r
- if (!cmd) cmd = ssh->cfg.remote_cmd;\r
- \r
- if (ssh->cfg.ssh_subsys && ssh->cfg.remote_cmd_ptr2) {\r
- cmd = ssh->cfg.remote_cmd_ptr2;\r
- ssh->fallback_cmd = TRUE;\r
- }\r
- if (*cmd)\r
- send_packet(ssh, SSH1_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END);\r
- else\r
- send_packet(ssh, SSH1_CMSG_EXEC_SHELL, PKT_END);\r
- logevent("Started session");\r
- }\r
-\r
- ssh->state = SSH_STATE_SESSION;\r
- if (ssh->size_needed)\r
- ssh_size(ssh, ssh->term_width, ssh->term_height);\r
- if (ssh->eof_needed)\r
- ssh_special(ssh, TS_EOF);\r
-\r
- if (ssh->ldisc)\r
- ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */\r
- ssh->send_ok = 1;\r
- ssh->channels = newtree234(ssh_channelcmp);\r
- while (1) {\r
-\r
- /*\r
- * By this point, most incoming packets are already being\r
- * handled by the dispatch table, and we need only pay\r
- * attention to the unusual ones.\r
- */\r
-\r
- crReturnV;\r
- if (pktin) {\r
- if (pktin->type == SSH1_SMSG_SUCCESS) {\r
- /* may be from EXEC_SHELL on some servers */\r
- } else if (pktin->type == SSH1_SMSG_FAILURE) {\r
- /* may be from EXEC_SHELL on some servers\r
- * if no pty is available or in other odd cases. Ignore */\r
- } else {\r
- bombout(("Strange packet received: type %d", pktin->type));\r
- crStopV;\r
- }\r
- } else {\r
- while (inlen > 0) {\r
- int len = min(inlen, 512);\r
- send_packet(ssh, SSH1_CMSG_STDIN_DATA,\r
- PKT_INT, len, PKTT_DATA, PKT_DATA, in, len,\r
- PKTT_OTHER, PKT_END);\r
- in += len;\r
- inlen -= len;\r
- }\r
- }\r
- }\r
-\r
- crFinishV;\r
-}\r
-\r
-/*\r
- * Handle the top-level SSH-2 protocol.\r
- */\r
-static void ssh1_msg_debug(Ssh ssh, struct Packet *pktin)\r
-{\r
- char *msg;\r
- int msglen;\r
-\r
- ssh_pkt_getstring(pktin, &msg, &msglen);\r
- logeventf(ssh, "Remote debug message: %.*s", msglen, msg);\r
-}\r
-\r
-static void ssh1_msg_disconnect(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* log reason code in disconnect message */\r
- char *msg;\r
- int msglen;\r
-\r
- ssh_pkt_getstring(pktin, &msg, &msglen);\r
- bombout(("Server sent disconnect message:\n\"%.*s\"", msglen, msg));\r
-}\r
-\r
-static void ssh_msg_ignore(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* Do nothing, because we're ignoring it! Duhh. */\r
-}\r
-\r
-static void ssh1_protocol_setup(Ssh ssh)\r
-{\r
- int i;\r
-\r
- /*\r
- * Most messages are handled by the coroutines.\r
- */\r
- for (i = 0; i < 256; i++)\r
- ssh->packet_dispatch[i] = NULL;\r
-\r
- /*\r
- * These special message types we install handlers for.\r
- */\r
- ssh->packet_dispatch[SSH1_MSG_DISCONNECT] = ssh1_msg_disconnect;\r
- ssh->packet_dispatch[SSH1_MSG_IGNORE] = ssh_msg_ignore;\r
- ssh->packet_dispatch[SSH1_MSG_DEBUG] = ssh1_msg_debug;\r
-}\r
-\r
-static void ssh1_protocol(Ssh ssh, void *vin, int inlen,\r
- struct Packet *pktin)\r
-{\r
- unsigned char *in=(unsigned char*)vin;\r
- if (ssh->state == SSH_STATE_CLOSED)\r
- return;\r
-\r
- if (pktin && ssh->packet_dispatch[pktin->type]) {\r
- ssh->packet_dispatch[pktin->type](ssh, pktin);\r
- return;\r
- }\r
-\r
- if (!ssh->protocol_initial_phase_done) {\r
- if (do_ssh1_login(ssh, in, inlen, pktin))\r
- ssh->protocol_initial_phase_done = TRUE;\r
- else\r
- return;\r
- }\r
-\r
- do_ssh1_connection(ssh, in, inlen, pktin);\r
-}\r
-\r
-/*\r
- * Utility routine for decoding comma-separated strings in KEXINIT.\r
- */\r
-static int in_commasep_string(char *needle, char *haystack, int haylen)\r
-{\r
- int needlen;\r
- if (!needle || !haystack) /* protect against null pointers */\r
- return 0;\r
- needlen = strlen(needle);\r
- while (1) {\r
- /*\r
- * Is it at the start of the string?\r
- */\r
- if (haylen >= needlen && /* haystack is long enough */\r
- !memcmp(needle, haystack, needlen) && /* initial match */\r
- (haylen == needlen || haystack[needlen] == ',')\r
- /* either , or EOS follows */\r
- )\r
- return 1;\r
- /*\r
- * If not, search for the next comma and resume after that.\r
- * If no comma found, terminate.\r
- */\r
- while (haylen > 0 && *haystack != ',')\r
- haylen--, haystack++;\r
- if (haylen == 0)\r
- return 0;\r
- haylen--, haystack++; /* skip over comma itself */\r
- }\r
-}\r
-\r
-/*\r
- * Similar routine for checking whether we have the first string in a list.\r
- */\r
-static int first_in_commasep_string(char *needle, char *haystack, int haylen)\r
-{\r
- int needlen;\r
- if (!needle || !haystack) /* protect against null pointers */\r
- return 0;\r
- needlen = strlen(needle);\r
- /*\r
- * Is it at the start of the string?\r
- */\r
- if (haylen >= needlen && /* haystack is long enough */\r
- !memcmp(needle, haystack, needlen) && /* initial match */\r
- (haylen == needlen || haystack[needlen] == ',')\r
- /* either , or EOS follows */\r
- )\r
- return 1;\r
- return 0;\r
-}\r
-\r
-\r
-/*\r
- * SSH-2 key creation method.\r
- * (Currently assumes 2 lots of any hash are sufficient to generate\r
- * keys/IVs for any cipher/MAC. SSH2_MKKEY_ITERS documents this assumption.)\r
- */\r
-#define SSH2_MKKEY_ITERS (2)\r
-static void ssh2_mkkey(Ssh ssh, Bignum K, unsigned char *H, char chr,\r
- unsigned char *keyspace)\r
-{\r
- const struct ssh_hash *h = ssh->kex->hash;\r
- void *s;\r
- /* First hlen bytes. */\r
- s = h->init();\r
- if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY))\r
- hash_mpint(h, s, K);\r
- h->bytes(s, H, h->hlen);\r
- h->bytes(s, &chr, 1);\r
- h->bytes(s, ssh->v2_session_id, ssh->v2_session_id_len);\r
- h->final(s, keyspace);\r
- /* Next hlen bytes. */\r
- s = h->init();\r
- if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY))\r
- hash_mpint(h, s, K);\r
- h->bytes(s, H, h->hlen);\r
- h->bytes(s, keyspace, h->hlen);\r
- h->final(s, keyspace + h->hlen);\r
-}\r
-\r
-/*\r
- * Handle the SSH-2 transport layer.\r
- */\r
-static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,\r
- struct Packet *pktin)\r
-{\r
- unsigned char *in = (unsigned char *)vin;\r
- struct do_ssh2_transport_state {\r
- int nbits, pbits, warn_kex, warn_cscipher, warn_sccipher;\r
- Bignum p, g, e, f, K;\r
- void *our_kexinit;\r
- int our_kexinitlen;\r
- int kex_init_value, kex_reply_value;\r
- const struct ssh_mac **maclist;\r
- int nmacs;\r
- const struct ssh2_cipher *cscipher_tobe;\r
- const struct ssh2_cipher *sccipher_tobe;\r
- const struct ssh_mac *csmac_tobe;\r
- const struct ssh_mac *scmac_tobe;\r
- const struct ssh_compress *cscomp_tobe;\r
- const struct ssh_compress *sccomp_tobe;\r
- char *hostkeydata, *sigdata, *rsakeydata, *keystr, *fingerprint;\r
- int hostkeylen, siglen, rsakeylen;\r
- void *hkey; /* actual host key */\r
- void *rsakey; /* for RSA kex */\r
- unsigned char exchange_hash[SSH2_KEX_MAX_HASH_LEN];\r
- int n_preferred_kex;\r
- const struct ssh_kexes *preferred_kex[KEX_MAX];\r
- int n_preferred_ciphers;\r
- const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX];\r
- const struct ssh_compress *preferred_comp;\r
- int userauth_succeeded; /* for delayed compression */\r
- int pending_compression;\r
- int got_session_id, activated_authconn;\r
- struct Packet *pktout;\r
- int dlgret;\r
- int guessok;\r
- int ignorepkt;\r
- };\r
- crState(do_ssh2_transport_state);\r
-\r
- crBegin(ssh->do_ssh2_transport_crstate);\r
-\r
- s->cscipher_tobe = s->sccipher_tobe = NULL;\r
- s->csmac_tobe = s->scmac_tobe = NULL;\r
- s->cscomp_tobe = s->sccomp_tobe = NULL;\r
-\r
- s->got_session_id = s->activated_authconn = FALSE;\r
- s->userauth_succeeded = FALSE;\r
- s->pending_compression = FALSE;\r
-\r
- /*\r
- * Be prepared to work around the buggy MAC problem.\r
- */\r
- if (ssh->remote_bugs & BUG_SSH2_HMAC)\r
- s->maclist = buggymacs, s->nmacs = lenof(buggymacs);\r
- else\r
- s->maclist = macs, s->nmacs = lenof(macs);\r
-\r
- begin_key_exchange:\r
- ssh->pkt_kctx = SSH2_PKTCTX_NOKEX;\r
- {\r
- int i, j, commalist_started;\r
-\r
- /*\r
- * Set up the preferred key exchange. (NULL => warn below here)\r
- */\r
- s->n_preferred_kex = 0;\r
- for (i = 0; i < KEX_MAX; i++) {\r
- switch (ssh->cfg.ssh_kexlist[i]) {\r
- case KEX_DHGEX:\r
- s->preferred_kex[s->n_preferred_kex++] =\r
- &ssh_diffiehellman_gex;\r
- break;\r
- case KEX_DHGROUP14:\r
- s->preferred_kex[s->n_preferred_kex++] =\r
- &ssh_diffiehellman_group14;\r
- break;\r
- case KEX_DHGROUP1:\r
- s->preferred_kex[s->n_preferred_kex++] =\r
- &ssh_diffiehellman_group1;\r
- break;\r
- case KEX_RSA:\r
- s->preferred_kex[s->n_preferred_kex++] =\r
- &ssh_rsa_kex;\r
- break;\r
- case KEX_WARN:\r
- /* Flag for later. Don't bother if it's the last in\r
- * the list. */\r
- if (i < KEX_MAX - 1) {\r
- s->preferred_kex[s->n_preferred_kex++] = NULL;\r
- }\r
- break;\r
- }\r
- }\r
-\r
- /*\r
- * Set up the preferred ciphers. (NULL => warn below here)\r
- */\r
- s->n_preferred_ciphers = 0;\r
- for (i = 0; i < CIPHER_MAX; i++) {\r
- switch (ssh->cfg.ssh_cipherlist[i]) {\r
- case CIPHER_BLOWFISH:\r
- s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_blowfish;\r
- break;\r
- case CIPHER_DES:\r
- if (ssh->cfg.ssh2_des_cbc) {\r
- s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_des;\r
- }\r
- break;\r
- case CIPHER_3DES:\r
- s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_3des;\r
- break;\r
- case CIPHER_AES:\r
- s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_aes;\r
- break;\r
- case CIPHER_ARCFOUR:\r
- s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_arcfour;\r
- break;\r
- case CIPHER_WARN:\r
- /* Flag for later. Don't bother if it's the last in\r
- * the list. */\r
- if (i < CIPHER_MAX - 1) {\r
- s->preferred_ciphers[s->n_preferred_ciphers++] = NULL;\r
- }\r
- break;\r
- }\r
- }\r
-\r
- /*\r
- * Set up preferred compression.\r
- */\r
- if (ssh->cfg.compression)\r
- s->preferred_comp = &ssh_zlib;\r
- else\r
- s->preferred_comp = &ssh_comp_none;\r
-\r
- /*\r
- * Enable queueing of outgoing auth- or connection-layer\r
- * packets while we are in the middle of a key exchange.\r
- */\r
- ssh->queueing = TRUE;\r
-\r
- /*\r
- * Flag that KEX is in progress.\r
- */\r
- ssh->kex_in_progress = TRUE;\r
-\r
- /*\r
- * Construct and send our key exchange packet.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_KEXINIT);\r
- for (i = 0; i < 16; i++)\r
- ssh2_pkt_addbyte(s->pktout, (unsigned char) random_byte());\r
- /* List key exchange algorithms. */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- commalist_started = 0;\r
- for (i = 0; i < s->n_preferred_kex; i++) {\r
- const struct ssh_kexes *k = s->preferred_kex[i];\r
- if (!k) continue; /* warning flag */\r
- for (j = 0; j < k->nkexes; j++) {\r
- if (commalist_started)\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- ssh2_pkt_addstring_str(s->pktout, k->list[j]->name);\r
- commalist_started = 1;\r
- }\r
- }\r
- /* List server host key algorithms. */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- for (i = 0; i < lenof(hostkey_algs); i++) {\r
- ssh2_pkt_addstring_str(s->pktout, hostkey_algs[i]->name);\r
- if (i < lenof(hostkey_algs) - 1)\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- }\r
- /* List client->server encryption algorithms. */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- commalist_started = 0;\r
- for (i = 0; i < s->n_preferred_ciphers; i++) {\r
- const struct ssh2_ciphers *c = s->preferred_ciphers[i];\r
- if (!c) continue; /* warning flag */\r
- for (j = 0; j < c->nciphers; j++) {\r
- if (commalist_started)\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- ssh2_pkt_addstring_str(s->pktout, c->list[j]->name);\r
- commalist_started = 1;\r
- }\r
- }\r
- /* List server->client encryption algorithms. */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- commalist_started = 0;\r
- for (i = 0; i < s->n_preferred_ciphers; i++) {\r
- const struct ssh2_ciphers *c = s->preferred_ciphers[i];\r
- if (!c) continue; /* warning flag */\r
- for (j = 0; j < c->nciphers; j++) {\r
- if (commalist_started)\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- ssh2_pkt_addstring_str(s->pktout, c->list[j]->name);\r
- commalist_started = 1;\r
- }\r
- }\r
- /* List client->server MAC algorithms. */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- for (i = 0; i < s->nmacs; i++) {\r
- ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name);\r
- if (i < s->nmacs - 1)\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- }\r
- /* List server->client MAC algorithms. */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- for (i = 0; i < s->nmacs; i++) {\r
- ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name);\r
- if (i < s->nmacs - 1)\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- }\r
- /* List client->server compression algorithms,\r
- * then server->client compression algorithms. (We use the\r
- * same set twice.) */\r
- for (j = 0; j < 2; j++) {\r
- ssh2_pkt_addstring_start(s->pktout);\r
- assert(lenof(compressions) > 1);\r
- /* Prefer non-delayed versions */\r
- ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name);\r
- /* We don't even list delayed versions of algorithms until\r
- * they're allowed to be used, to avoid a race. See the end of\r
- * this function. */\r
- if (s->userauth_succeeded && s->preferred_comp->delayed_name) {\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- ssh2_pkt_addstring_str(s->pktout,\r
- s->preferred_comp->delayed_name);\r
- }\r
- for (i = 0; i < lenof(compressions); i++) {\r
- const struct ssh_compress *c = compressions[i];\r
- if (c != s->preferred_comp) {\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- ssh2_pkt_addstring_str(s->pktout, c->name);\r
- if (s->userauth_succeeded && c->delayed_name) {\r
- ssh2_pkt_addstring_str(s->pktout, ",");\r
- ssh2_pkt_addstring_str(s->pktout, c->delayed_name);\r
- }\r
- }\r
- }\r
- }\r
- /* List client->server languages. Empty list. */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- /* List server->client languages. Empty list. */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- /* First KEX packet does _not_ follow, because we're not that brave. */\r
- ssh2_pkt_addbool(s->pktout, FALSE);\r
- /* Reserved. */\r
- ssh2_pkt_adduint32(s->pktout, 0);\r
- }\r
-\r
- s->our_kexinitlen = s->pktout->length - 5;\r
- s->our_kexinit = snewn(s->our_kexinitlen, unsigned char);\r
- memcpy(s->our_kexinit, s->pktout->data + 5, s->our_kexinitlen); \r
-\r
- ssh2_pkt_send_noqueue(ssh, s->pktout);\r
-\r
- if (!pktin)\r
- crWaitUntil(pktin);\r
-\r
- /*\r
- * Now examine the other side's KEXINIT to see what we're up\r
- * to.\r
- */\r
- {\r
- char *str, *preferred;\r
- int i, j, len;\r
-\r
- if (pktin->type != SSH2_MSG_KEXINIT) {\r
- bombout(("expected key exchange packet from server"));\r
- crStop(0);\r
- }\r
- ssh->kex = NULL;\r
- ssh->hostkey = NULL;\r
- s->cscipher_tobe = NULL;\r
- s->sccipher_tobe = NULL;\r
- s->csmac_tobe = NULL;\r
- s->scmac_tobe = NULL;\r
- s->cscomp_tobe = NULL;\r
- s->sccomp_tobe = NULL;\r
- s->warn_kex = s->warn_cscipher = s->warn_sccipher = FALSE;\r
-\r
- pktin->savedpos += 16; /* skip garbage cookie */\r
- ssh_pkt_getstring(pktin, &str, &len); /* key exchange algorithms */\r
-\r
- preferred = NULL;\r
- for (i = 0; i < s->n_preferred_kex; i++) {\r
- const struct ssh_kexes *k = s->preferred_kex[i];\r
- if (!k) {\r
- s->warn_kex = TRUE;\r
- } else {\r
- for (j = 0; j < k->nkexes; j++) {\r
- if (!preferred) preferred = k->list[j]->name;\r
- if (in_commasep_string(k->list[j]->name, str, len)) {\r
- ssh->kex = k->list[j];\r
- break;\r
- }\r
- }\r
- }\r
- if (ssh->kex)\r
- break;\r
- }\r
- if (!ssh->kex) {\r
- bombout(("Couldn't agree a key exchange algorithm (available: %s)",\r
- str ? str : "(null)"));\r
- crStop(0);\r
- }\r
- /*\r
- * Note that the server's guess is considered wrong if it doesn't match\r
- * the first algorithm in our list, even if it's still the algorithm\r
- * we end up using.\r
- */\r
- s->guessok = first_in_commasep_string(preferred, str, len);\r
- ssh_pkt_getstring(pktin, &str, &len); /* host key algorithms */\r
- for (i = 0; i < lenof(hostkey_algs); i++) {\r
- if (in_commasep_string(hostkey_algs[i]->name, str, len)) {\r
- ssh->hostkey = hostkey_algs[i];\r
- break;\r
- }\r
- }\r
- s->guessok = s->guessok &&\r
- first_in_commasep_string(hostkey_algs[0]->name, str, len);\r
- ssh_pkt_getstring(pktin, &str, &len); /* client->server cipher */\r
- for (i = 0; i < s->n_preferred_ciphers; i++) {\r
- const struct ssh2_ciphers *c = s->preferred_ciphers[i];\r
- if (!c) {\r
- s->warn_cscipher = TRUE;\r
- } else {\r
- for (j = 0; j < c->nciphers; j++) {\r
- if (in_commasep_string(c->list[j]->name, str, len)) {\r
- s->cscipher_tobe = c->list[j];\r
- break;\r
- }\r
- }\r
- }\r
- if (s->cscipher_tobe)\r
- break;\r
- }\r
- if (!s->cscipher_tobe) {\r
- bombout(("Couldn't agree a client-to-server cipher (available: %s)",\r
- str ? str : "(null)"));\r
- crStop(0);\r
- }\r
-\r
- ssh_pkt_getstring(pktin, &str, &len); /* server->client cipher */\r
- for (i = 0; i < s->n_preferred_ciphers; i++) {\r
- const struct ssh2_ciphers *c = s->preferred_ciphers[i];\r
- if (!c) {\r
- s->warn_sccipher = TRUE;\r
- } else {\r
- for (j = 0; j < c->nciphers; j++) {\r
- if (in_commasep_string(c->list[j]->name, str, len)) {\r
- s->sccipher_tobe = c->list[j];\r
- break;\r
- }\r
- }\r
- }\r
- if (s->sccipher_tobe)\r
- break;\r
- }\r
- if (!s->sccipher_tobe) {\r
- bombout(("Couldn't agree a server-to-client cipher (available: %s)",\r
- str ? str : "(null)"));\r
- crStop(0);\r
- }\r
-\r
- ssh_pkt_getstring(pktin, &str, &len); /* client->server mac */\r
- for (i = 0; i < s->nmacs; i++) {\r
- if (in_commasep_string(s->maclist[i]->name, str, len)) {\r
- s->csmac_tobe = s->maclist[i];\r
- break;\r
- }\r
- }\r
- ssh_pkt_getstring(pktin, &str, &len); /* server->client mac */\r
- for (i = 0; i < s->nmacs; i++) {\r
- if (in_commasep_string(s->maclist[i]->name, str, len)) {\r
- s->scmac_tobe = s->maclist[i];\r
- break;\r
- }\r
- }\r
- ssh_pkt_getstring(pktin, &str, &len); /* client->server compression */\r
- for (i = 0; i < lenof(compressions) + 1; i++) {\r
- const struct ssh_compress *c =\r
- i == 0 ? s->preferred_comp : compressions[i - 1];\r
- if (in_commasep_string(c->name, str, len)) {\r
- s->cscomp_tobe = c;\r
- break;\r
- } else if (in_commasep_string(c->delayed_name, str, len)) {\r
- if (s->userauth_succeeded) {\r
- s->cscomp_tobe = c;\r
- break;\r
- } else {\r
- s->pending_compression = TRUE; /* try this later */\r
- }\r
- }\r
- }\r
- ssh_pkt_getstring(pktin, &str, &len); /* server->client compression */\r
- for (i = 0; i < lenof(compressions) + 1; i++) {\r
- const struct ssh_compress *c =\r
- i == 0 ? s->preferred_comp : compressions[i - 1];\r
- if (in_commasep_string(c->name, str, len)) {\r
- s->sccomp_tobe = c;\r
- break;\r
- } else if (in_commasep_string(c->delayed_name, str, len)) {\r
- if (s->userauth_succeeded) {\r
- s->sccomp_tobe = c;\r
- break;\r
- } else {\r
- s->pending_compression = TRUE; /* try this later */\r
- }\r
- }\r
- }\r
- if (s->pending_compression) {\r
- logevent("Server supports delayed compression; "\r
- "will try this later");\r
- }\r
- ssh_pkt_getstring(pktin, &str, &len); /* client->server language */\r
- ssh_pkt_getstring(pktin, &str, &len); /* server->client language */\r
- s->ignorepkt = ssh2_pkt_getbool(pktin) && !s->guessok;\r
-\r
- if (s->warn_kex) {\r
- ssh_set_frozen(ssh, 1);\r
- s->dlgret = askalg(ssh->frontend, "key-exchange algorithm",\r
- ssh->kex->name,\r
- ssh_dialog_callback, ssh);\r
- if (s->dlgret < 0) {\r
- do {\r
- crReturn(0);\r
- if (pktin) {\r
- bombout(("Unexpected data from server while"\r
- " waiting for user response"));\r
- crStop(0);\r
- }\r
- } while (pktin || inlen > 0);\r
- s->dlgret = ssh->user_response;\r
- }\r
- ssh_set_frozen(ssh, 0);\r
- if (s->dlgret == 0) {\r
- ssh_disconnect(ssh, "User aborted at kex warning", NULL,\r
- 0, TRUE);\r
- crStop(0);\r
- }\r
- }\r
-\r
- if (s->warn_cscipher) {\r
- ssh_set_frozen(ssh, 1);\r
- s->dlgret = askalg(ssh->frontend,\r
- "client-to-server cipher",\r
- s->cscipher_tobe->name,\r
- ssh_dialog_callback, ssh);\r
- if (s->dlgret < 0) {\r
- do {\r
- crReturn(0);\r
- if (pktin) {\r
- bombout(("Unexpected data from server while"\r
- " waiting for user response"));\r
- crStop(0);\r
- }\r
- } while (pktin || inlen > 0);\r
- s->dlgret = ssh->user_response;\r
- }\r
- ssh_set_frozen(ssh, 0);\r
- if (s->dlgret == 0) {\r
- ssh_disconnect(ssh, "User aborted at cipher warning", NULL,\r
- 0, TRUE);\r
- crStop(0);\r
- }\r
- }\r
-\r
- if (s->warn_sccipher) {\r
- ssh_set_frozen(ssh, 1);\r
- s->dlgret = askalg(ssh->frontend,\r
- "server-to-client cipher",\r
- s->sccipher_tobe->name,\r
- ssh_dialog_callback, ssh);\r
- if (s->dlgret < 0) {\r
- do {\r
- crReturn(0);\r
- if (pktin) {\r
- bombout(("Unexpected data from server while"\r
- " waiting for user response"));\r
- crStop(0);\r
- }\r
- } while (pktin || inlen > 0);\r
- s->dlgret = ssh->user_response;\r
- }\r
- ssh_set_frozen(ssh, 0);\r
- if (s->dlgret == 0) {\r
- ssh_disconnect(ssh, "User aborted at cipher warning", NULL,\r
- 0, TRUE);\r
- crStop(0);\r
- }\r
- }\r
-\r
- ssh->exhash = ssh->kex->hash->init();\r
- hash_string(ssh->kex->hash, ssh->exhash, ssh->v_c, strlen(ssh->v_c));\r
- hash_string(ssh->kex->hash, ssh->exhash, ssh->v_s, strlen(ssh->v_s));\r
- hash_string(ssh->kex->hash, ssh->exhash,\r
- s->our_kexinit, s->our_kexinitlen);\r
- sfree(s->our_kexinit);\r
- if (pktin->length > 5)\r
- hash_string(ssh->kex->hash, ssh->exhash,\r
- pktin->data + 5, pktin->length - 5);\r
-\r
- if (s->ignorepkt) /* first_kex_packet_follows */\r
- crWaitUntil(pktin); /* Ignore packet */\r
- }\r
-\r
- if (ssh->kex->main_type == KEXTYPE_DH) {\r
- /*\r
- * Work out the number of bits of key we will need from the\r
- * key exchange. We start with the maximum key length of\r
- * either cipher...\r
- */\r
- {\r
- int csbits, scbits;\r
-\r
- csbits = s->cscipher_tobe->keylen;\r
- scbits = s->sccipher_tobe->keylen;\r
- s->nbits = (csbits > scbits ? csbits : scbits);\r
- }\r
- /* The keys only have hlen-bit entropy, since they're based on\r
- * a hash. So cap the key size at hlen bits. */\r
- if (s->nbits > ssh->kex->hash->hlen * 8)\r
- s->nbits = ssh->kex->hash->hlen * 8;\r
-\r
- /*\r
- * If we're doing Diffie-Hellman group exchange, start by\r
- * requesting a group.\r
- */\r
- if (!ssh->kex->pdata) {\r
- logevent("Doing Diffie-Hellman group exchange");\r
- ssh->pkt_kctx = SSH2_PKTCTX_DHGEX;\r
- /*\r
- * Work out how big a DH group we will need to allow that\r
- * much data.\r
- */\r
- s->pbits = 512 << ((s->nbits - 1) / 64);\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST);\r
- ssh2_pkt_adduint32(s->pktout, s->pbits);\r
- ssh2_pkt_send_noqueue(ssh, s->pktout);\r
-\r
- crWaitUntil(pktin);\r
- if (pktin->type != SSH2_MSG_KEX_DH_GEX_GROUP) {\r
- bombout(("expected key exchange group packet from server"));\r
- crStop(0);\r
- }\r
- s->p = ssh2_pkt_getmp(pktin);\r
- s->g = ssh2_pkt_getmp(pktin);\r
- if (!s->p || !s->g) {\r
- bombout(("unable to read mp-ints from incoming group packet"));\r
- crStop(0);\r
- }\r
- ssh->kex_ctx = dh_setup_gex(s->p, s->g);\r
- s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT;\r
- s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY;\r
- } else {\r
- ssh->pkt_kctx = SSH2_PKTCTX_DHGROUP;\r
- ssh->kex_ctx = dh_setup_group(ssh->kex);\r
- s->kex_init_value = SSH2_MSG_KEXDH_INIT;\r
- s->kex_reply_value = SSH2_MSG_KEXDH_REPLY;\r
- logeventf(ssh, "Using Diffie-Hellman with standard group \"%s\"",\r
- ssh->kex->groupname);\r
- }\r
-\r
- logeventf(ssh, "Doing Diffie-Hellman key exchange with hash %s",\r
- ssh->kex->hash->text_name);\r
- /*\r
- * Now generate and send e for Diffie-Hellman.\r
- */\r
- set_busy_status(ssh->frontend, BUSY_CPU); /* this can take a while */\r
- s->e = dh_create_e(ssh->kex_ctx, s->nbits * 2);\r
- s->pktout = ssh2_pkt_init(s->kex_init_value);\r
- ssh2_pkt_addmp(s->pktout, s->e);\r
- ssh2_pkt_send_noqueue(ssh, s->pktout);\r
-\r
- set_busy_status(ssh->frontend, BUSY_WAITING); /* wait for server */\r
- crWaitUntil(pktin);\r
- if (pktin->type != s->kex_reply_value) {\r
- bombout(("expected key exchange reply packet from server"));\r
- crStop(0);\r
- }\r
- set_busy_status(ssh->frontend, BUSY_CPU); /* cogitate */\r
- ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen);\r
- s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen);\r
- s->f = ssh2_pkt_getmp(pktin);\r
- if (!s->f) {\r
- bombout(("unable to parse key exchange reply packet"));\r
- crStop(0);\r
- }\r
- ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen);\r
-\r
- s->K = dh_find_K(ssh->kex_ctx, s->f);\r
-\r
- /* We assume everything from now on will be quick, and it might\r
- * involve user interaction. */\r
- set_busy_status(ssh->frontend, BUSY_NOT);\r
-\r
- hash_string(ssh->kex->hash, ssh->exhash, s->hostkeydata, s->hostkeylen);\r
- if (!ssh->kex->pdata) {\r
- hash_uint32(ssh->kex->hash, ssh->exhash, s->pbits);\r
- hash_mpint(ssh->kex->hash, ssh->exhash, s->p);\r
- hash_mpint(ssh->kex->hash, ssh->exhash, s->g);\r
- }\r
- hash_mpint(ssh->kex->hash, ssh->exhash, s->e);\r
- hash_mpint(ssh->kex->hash, ssh->exhash, s->f);\r
-\r
- dh_cleanup(ssh->kex_ctx);\r
- freebn(s->f);\r
- if (!ssh->kex->pdata) {\r
- freebn(s->g);\r
- freebn(s->p);\r
- }\r
- } else {\r
- logeventf(ssh, "Doing RSA key exchange with hash %s",\r
- ssh->kex->hash->text_name);\r
- ssh->pkt_kctx = SSH2_PKTCTX_RSAKEX;\r
- /*\r
- * RSA key exchange. First expect a KEXRSA_PUBKEY packet\r
- * from the server.\r
- */\r
- crWaitUntil(pktin);\r
- if (pktin->type != SSH2_MSG_KEXRSA_PUBKEY) {\r
- bombout(("expected RSA public key packet from server"));\r
- crStop(0);\r
- }\r
-\r
- ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen);\r
- hash_string(ssh->kex->hash, ssh->exhash,\r
- s->hostkeydata, s->hostkeylen);\r
- s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen);\r
-\r
- {\r
- char *keydata;\r
- ssh_pkt_getstring(pktin, &keydata, &s->rsakeylen);\r
- s->rsakeydata = snewn(s->rsakeylen, char);\r
- memcpy(s->rsakeydata, keydata, s->rsakeylen);\r
- }\r
-\r
- s->rsakey = ssh_rsakex_newkey(s->rsakeydata, s->rsakeylen);\r
- if (!s->rsakey) {\r
- sfree(s->rsakeydata);\r
- bombout(("unable to parse RSA public key from server"));\r
- crStop(0);\r
- }\r
-\r
- hash_string(ssh->kex->hash, ssh->exhash, s->rsakeydata, s->rsakeylen);\r
-\r
- /*\r
- * Next, set up a shared secret K, of precisely KLEN -\r
- * 2*HLEN - 49 bits, where KLEN is the bit length of the\r
- * RSA key modulus and HLEN is the bit length of the hash\r
- * we're using.\r
- */\r
- {\r
- int klen = ssh_rsakex_klen(s->rsakey);\r
- int nbits = klen - (2*ssh->kex->hash->hlen*8 + 49);\r
- int i, byte = 0;\r
- unsigned char *kstr1, *kstr2, *outstr;\r
- int kstr1len, kstr2len, outstrlen;\r
-\r
- s->K = bn_power_2(nbits - 1);\r
-\r
- for (i = 0; i < nbits; i++) {\r
- if ((i & 7) == 0) {\r
- byte = random_byte();\r
- }\r
- bignum_set_bit(s->K, i, (byte >> (i & 7)) & 1);\r
- }\r
-\r
- /*\r
- * Encode this as an mpint.\r
- */\r
- kstr1 = ssh2_mpint_fmt(s->K, &kstr1len);\r
- kstr2 = snewn(kstr2len = 4 + kstr1len, unsigned char);\r
- PUT_32BIT(kstr2, kstr1len);\r
- memcpy(kstr2 + 4, kstr1, kstr1len);\r
-\r
- /*\r
- * Encrypt it with the given RSA key.\r
- */\r
- outstrlen = (klen + 7) / 8;\r
- outstr = snewn(outstrlen, unsigned char);\r
- ssh_rsakex_encrypt(ssh->kex->hash, kstr2, kstr2len,\r
- outstr, outstrlen, s->rsakey);\r
-\r
- /*\r
- * And send it off in a return packet.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_KEXRSA_SECRET);\r
- ssh2_pkt_addstring_start(s->pktout);\r
- ssh2_pkt_addstring_data(s->pktout, (char *)outstr, outstrlen);\r
- ssh2_pkt_send_noqueue(ssh, s->pktout);\r
-\r
- hash_string(ssh->kex->hash, ssh->exhash, outstr, outstrlen);\r
-\r
- sfree(kstr2);\r
- sfree(kstr1);\r
- sfree(outstr);\r
- }\r
-\r
- ssh_rsakex_freekey(s->rsakey);\r
-\r
- crWaitUntil(pktin);\r
- if (pktin->type != SSH2_MSG_KEXRSA_DONE) {\r
- sfree(s->rsakeydata);\r
- bombout(("expected signature packet from server"));\r
- crStop(0);\r
- }\r
-\r
- ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen);\r
-\r
- sfree(s->rsakeydata);\r
- }\r
-\r
- hash_mpint(ssh->kex->hash, ssh->exhash, s->K);\r
- assert(ssh->kex->hash->hlen <= sizeof(s->exchange_hash));\r
- ssh->kex->hash->final(ssh->exhash, s->exchange_hash);\r
-\r
- ssh->kex_ctx = NULL;\r
-\r
-#if 0\r
- debug(("Exchange hash is:\n"));\r
- dmemdump(s->exchange_hash, ssh->kex->hash->hlen);\r
-#endif\r
-\r
- if (!s->hkey ||\r
- !ssh->hostkey->verifysig(s->hkey, s->sigdata, s->siglen,\r
- (char *)s->exchange_hash,\r
- ssh->kex->hash->hlen)) {\r
- bombout(("Server's host key did not match the signature supplied"));\r
- crStop(0);\r
- }\r
-\r
- /*\r
- * Authenticate remote host: verify host key. (We've already\r
- * checked the signature of the exchange hash.)\r
- */\r
- s->keystr = ssh->hostkey->fmtkey(s->hkey);\r
- s->fingerprint = ssh->hostkey->fingerprint(s->hkey);\r
- ssh_set_frozen(ssh, 1);\r
- s->dlgret = verify_ssh_host_key(ssh->frontend,\r
- ssh->savedhost, ssh->savedport,\r
- ssh->hostkey->keytype, s->keystr,\r
- s->fingerprint,\r
- ssh_dialog_callback, ssh);\r
- if (s->dlgret < 0) {\r
- do {\r
- crReturn(0);\r
- if (pktin) {\r
- bombout(("Unexpected data from server while waiting"\r
- " for user host key response"));\r
- crStop(0);\r
- }\r
- } while (pktin || inlen > 0);\r
- s->dlgret = ssh->user_response;\r
- }\r
- ssh_set_frozen(ssh, 0);\r
- if (s->dlgret == 0) {\r
- ssh_disconnect(ssh, "User aborted at host key verification", NULL,\r
- 0, TRUE);\r
- crStop(0);\r
- }\r
- if (!s->got_session_id) { /* don't bother logging this in rekeys */\r
- logevent("Host key fingerprint is:");\r
- logevent(s->fingerprint);\r
- }\r
- sfree(s->fingerprint);\r
- sfree(s->keystr);\r
- ssh->hostkey->freekey(s->hkey);\r
-\r
- /*\r
- * The exchange hash from the very first key exchange is also\r
- * the session id, used in session key construction and\r
- * authentication.\r
- */\r
- if (!s->got_session_id) {\r
- assert(sizeof(s->exchange_hash) <= sizeof(ssh->v2_session_id));\r
- memcpy(ssh->v2_session_id, s->exchange_hash,\r
- sizeof(s->exchange_hash));\r
- ssh->v2_session_id_len = ssh->kex->hash->hlen;\r
- assert(ssh->v2_session_id_len <= sizeof(ssh->v2_session_id));\r
- s->got_session_id = TRUE;\r
- }\r
-\r
- /*\r
- * Send SSH2_MSG_NEWKEYS.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_NEWKEYS);\r
- ssh2_pkt_send_noqueue(ssh, s->pktout);\r
- ssh->outgoing_data_size = 0; /* start counting from here */\r
-\r
- /*\r
- * We've sent client NEWKEYS, so create and initialise\r
- * client-to-server session keys.\r
- */\r
- if (ssh->cs_cipher_ctx)\r
- ssh->cscipher->free_context(ssh->cs_cipher_ctx);\r
- ssh->cscipher = s->cscipher_tobe;\r
- ssh->cs_cipher_ctx = ssh->cscipher->make_context();\r
-\r
- if (ssh->cs_mac_ctx)\r
- ssh->csmac->free_context(ssh->cs_mac_ctx);\r
- ssh->csmac = s->csmac_tobe;\r
- ssh->cs_mac_ctx = ssh->csmac->make_context();\r
-\r
- if (ssh->cs_comp_ctx)\r
- ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx);\r
- ssh->cscomp = s->cscomp_tobe;\r
- ssh->cs_comp_ctx = ssh->cscomp->compress_init();\r
-\r
- /*\r
- * Set IVs on client-to-server keys. Here we use the exchange\r
- * hash from the _first_ key exchange.\r
- */\r
- {\r
- unsigned char keyspace[SSH2_KEX_MAX_HASH_LEN * SSH2_MKKEY_ITERS];\r
- assert(sizeof(keyspace) >= ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);\r
- ssh2_mkkey(ssh,s->K,s->exchange_hash,'C',keyspace);\r
- assert((ssh->cscipher->keylen+7) / 8 <=\r
- ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);\r
- ssh->cscipher->setkey(ssh->cs_cipher_ctx, keyspace);\r
- ssh2_mkkey(ssh,s->K,s->exchange_hash,'A',keyspace);\r
- assert(ssh->cscipher->blksize <=\r
- ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);\r
- ssh->cscipher->setiv(ssh->cs_cipher_ctx, keyspace);\r
- ssh2_mkkey(ssh,s->K,s->exchange_hash,'E',keyspace);\r
- assert(ssh->csmac->len <=\r
- ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);\r
- ssh->csmac->setkey(ssh->cs_mac_ctx, keyspace);\r
- memset(keyspace, 0, sizeof(keyspace));\r
- }\r
-\r
- logeventf(ssh, "Initialised %.200s client->server encryption",\r
- ssh->cscipher->text_name);\r
- logeventf(ssh, "Initialised %.200s client->server MAC algorithm",\r
- ssh->csmac->text_name);\r
- if (ssh->cscomp->text_name)\r
- logeventf(ssh, "Initialised %s compression",\r
- ssh->cscomp->text_name);\r
-\r
- /*\r
- * Now our end of the key exchange is complete, we can send all\r
- * our queued higher-layer packets.\r
- */\r
- ssh->queueing = FALSE;\r
- ssh2_pkt_queuesend(ssh);\r
-\r
- /*\r
- * Expect SSH2_MSG_NEWKEYS from server.\r
- */\r
- crWaitUntil(pktin);\r
- if (pktin->type != SSH2_MSG_NEWKEYS) {\r
- bombout(("expected new-keys packet from server"));\r
- crStop(0);\r
- }\r
- ssh->incoming_data_size = 0; /* start counting from here */\r
-\r
- /*\r
- * We've seen server NEWKEYS, so create and initialise\r
- * server-to-client session keys.\r
- */\r
- if (ssh->sc_cipher_ctx)\r
- ssh->sccipher->free_context(ssh->sc_cipher_ctx);\r
- ssh->sccipher = s->sccipher_tobe;\r
- ssh->sc_cipher_ctx = ssh->sccipher->make_context();\r
-\r
- if (ssh->sc_mac_ctx)\r
- ssh->scmac->free_context(ssh->sc_mac_ctx);\r
- ssh->scmac = s->scmac_tobe;\r
- ssh->sc_mac_ctx = ssh->scmac->make_context();\r
-\r
- if (ssh->sc_comp_ctx)\r
- ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx);\r
- ssh->sccomp = s->sccomp_tobe;\r
- ssh->sc_comp_ctx = ssh->sccomp->decompress_init();\r
-\r
- /*\r
- * Set IVs on server-to-client keys. Here we use the exchange\r
- * hash from the _first_ key exchange.\r
- */\r
- {\r
- unsigned char keyspace[SSH2_KEX_MAX_HASH_LEN * SSH2_MKKEY_ITERS];\r
- assert(sizeof(keyspace) >= ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);\r
- ssh2_mkkey(ssh,s->K,s->exchange_hash,'D',keyspace);\r
- assert((ssh->sccipher->keylen+7) / 8 <=\r
- ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);\r
- ssh->sccipher->setkey(ssh->sc_cipher_ctx, keyspace);\r
- ssh2_mkkey(ssh,s->K,s->exchange_hash,'B',keyspace);\r
- assert(ssh->sccipher->blksize <=\r
- ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);\r
- ssh->sccipher->setiv(ssh->sc_cipher_ctx, keyspace);\r
- ssh2_mkkey(ssh,s->K,s->exchange_hash,'F',keyspace);\r
- assert(ssh->scmac->len <=\r
- ssh->kex->hash->hlen * SSH2_MKKEY_ITERS);\r
- ssh->scmac->setkey(ssh->sc_mac_ctx, keyspace);\r
- memset(keyspace, 0, sizeof(keyspace));\r
- }\r
- logeventf(ssh, "Initialised %.200s server->client encryption",\r
- ssh->sccipher->text_name);\r
- logeventf(ssh, "Initialised %.200s server->client MAC algorithm",\r
- ssh->scmac->text_name);\r
- if (ssh->sccomp->text_name)\r
- logeventf(ssh, "Initialised %s decompression",\r
- ssh->sccomp->text_name);\r
-\r
- /*\r
- * Free shared secret.\r
- */\r
- freebn(s->K);\r
-\r
- /*\r
- * Key exchange is over. Loop straight back round if we have a\r
- * deferred rekey reason.\r
- */\r
- if (ssh->deferred_rekey_reason) {\r
- logevent(ssh->deferred_rekey_reason);\r
- pktin = NULL;\r
- ssh->deferred_rekey_reason = NULL;\r
- goto begin_key_exchange;\r
- }\r
-\r
- /*\r
- * Otherwise, schedule a timer for our next rekey.\r
- */\r
- ssh->kex_in_progress = FALSE;\r
- ssh->last_rekey = GETTICKCOUNT();\r
- if (ssh->cfg.ssh_rekey_time != 0)\r
- ssh->next_rekey = schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC,\r
- ssh2_timer, ssh);\r
-\r
- /*\r
- * If this is the first key exchange phase, we must pass the\r
- * SSH2_MSG_NEWKEYS packet to the next layer, not because it\r
- * wants to see it but because it will need time to initialise\r
- * itself before it sees an actual packet. In subsequent key\r
- * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because\r
- * it would only confuse the layer above.\r
- */\r
- if (s->activated_authconn) {\r
- crReturn(0);\r
- }\r
- s->activated_authconn = TRUE;\r
-\r
- /*\r
- * Now we're encrypting. Begin returning 1 to the protocol main\r
- * function so that other things can run on top of the\r
- * transport. If we ever see a KEXINIT, we must go back to the\r
- * start.\r
- * \r
- * We _also_ go back to the start if we see pktin==NULL and\r
- * inlen negative, because this is a special signal meaning\r
- * `initiate client-driven rekey', and `in' contains a message\r
- * giving the reason for the rekey.\r
- *\r
- * inlen==-1 means always initiate a rekey;\r
- * inlen==-2 means that userauth has completed successfully and\r
- * we should consider rekeying (for delayed compression).\r
- */\r
- while (!((pktin && pktin->type == SSH2_MSG_KEXINIT) ||\r
- (!pktin && inlen < 0))) {\r
- wait_for_rekey:\r
- crReturn(1);\r
- }\r
- if (pktin) {\r
- logevent("Server initiated key re-exchange");\r
- } else {\r
- if (inlen == -2) {\r
- /* \r
- * authconn has seen a USERAUTH_SUCCEEDED. Time to enable\r
- * delayed compression, if it's available.\r
- *\r
- * draft-miller-secsh-compression-delayed-00 says that you\r
- * negotiate delayed compression in the first key exchange, and\r
- * both sides start compressing when the server has sent\r
- * USERAUTH_SUCCESS. This has a race condition -- the server\r
- * can't know when the client has seen it, and thus which incoming\r
- * packets it should treat as compressed.\r
- *\r
- * Instead, we do the initial key exchange without offering the\r
- * delayed methods, but note if the server offers them; when we\r
- * get here, if a delayed method was available that was higher\r
- * on our list than what we got, we initiate a rekey in which we\r
- * _do_ list the delayed methods (and hopefully get it as a\r
- * result). Subsequent rekeys will do the same.\r
- */\r
- assert(!s->userauth_succeeded); /* should only happen once */\r
- s->userauth_succeeded = TRUE;\r
- if (!s->pending_compression)\r
- /* Can't see any point rekeying. */\r
- goto wait_for_rekey; /* this is utterly horrid */\r
- /* else fall through to rekey... */\r
- s->pending_compression = FALSE;\r
- }\r
- /*\r
- * Now we've decided to rekey.\r
- *\r
- * Special case: if the server bug is set that doesn't\r
- * allow rekeying, we give a different log message and\r
- * continue waiting. (If such a server _initiates_ a rekey,\r
- * we process it anyway!)\r
- */\r
- if ((ssh->remote_bugs & BUG_SSH2_REKEY)) {\r
- logeventf(ssh, "Server bug prevents key re-exchange (%s)",\r
- (char *)in);\r
- /* Reset the counters, so that at least this message doesn't\r
- * hit the event log _too_ often. */\r
- ssh->outgoing_data_size = 0;\r
- ssh->incoming_data_size = 0;\r
- if (ssh->cfg.ssh_rekey_time != 0) {\r
- ssh->next_rekey =\r
- schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC,\r
- ssh2_timer, ssh);\r
- }\r
- goto wait_for_rekey; /* this is still utterly horrid */\r
- } else {\r
- logeventf(ssh, "Initiating key re-exchange (%s)", (char *)in);\r
- }\r
- }\r
- goto begin_key_exchange;\r
-\r
- crFinish(1);\r
-}\r
-\r
-/*\r
- * Add data to an SSH-2 channel output buffer.\r
- */\r
-static void ssh2_add_channel_data(struct ssh_channel *c, char *buf,\r
- int len)\r
-{\r
- bufchain_add(&c->v.v2.outbuffer, buf, len);\r
-}\r
-\r
-/*\r
- * Attempt to send data on an SSH-2 channel.\r
- */\r
-static int ssh2_try_send(struct ssh_channel *c)\r
-{\r
- Ssh ssh = c->ssh;\r
- struct Packet *pktout;\r
-\r
- while (c->v.v2.remwindow > 0 && bufchain_size(&c->v.v2.outbuffer) > 0) {\r
- int len;\r
- void *data;\r
- bufchain_prefix(&c->v.v2.outbuffer, &data, &len);\r
- if ((unsigned)len > c->v.v2.remwindow)\r
- len = c->v.v2.remwindow;\r
- if ((unsigned)len > c->v.v2.remmaxpkt)\r
- len = c->v.v2.remmaxpkt;\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_addstring_start(pktout);\r
- dont_log_data(ssh, pktout, PKTLOG_OMIT);\r
- ssh2_pkt_addstring_data(pktout, data, len);\r
- end_log_omission(ssh, pktout);\r
- ssh2_pkt_send(ssh, pktout);\r
- bufchain_consume(&c->v.v2.outbuffer, len);\r
- c->v.v2.remwindow -= len;\r
- }\r
-\r
- /*\r
- * After having sent as much data as we can, return the amount\r
- * still buffered.\r
- */\r
- return bufchain_size(&c->v.v2.outbuffer);\r
-}\r
-\r
-static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)\r
-{\r
- int bufsize;\r
- if (c->closes)\r
- return; /* don't send on closing channels */\r
- bufsize = ssh2_try_send(c);\r
- if (bufsize == 0) {\r
- switch (c->type) {\r
- case CHAN_MAINSESSION:\r
- /* stdin need not receive an unthrottle\r
- * notification since it will be polled */\r
- break;\r
- case CHAN_X11:\r
- x11_unthrottle(c->u.x11.s);\r
- break;\r
- case CHAN_AGENT:\r
- /* agent sockets are request/response and need no\r
- * buffer management */\r
- break;\r
- case CHAN_SOCKDATA:\r
- pfd_unthrottle(c->u.pfd.s);\r
- break;\r
- }\r
- }\r
-\r
- /*\r
- * If we've emptied the channel's output buffer and there's a\r
- * pending close event, start the channel-closing procedure.\r
- */\r
- if (c->pending_close && bufchain_size(&c->v.v2.outbuffer) == 0) {\r
- struct Packet *pktout;\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_send(ssh, pktout);\r
- c->closes = 1;\r
- c->pending_close = FALSE;\r
- }\r
-}\r
-\r
-/*\r
- * Set up most of a new ssh_channel for SSH-2.\r
- */\r
-static void ssh2_channel_init(struct ssh_channel *c)\r
-{\r
- Ssh ssh = c->ssh;\r
- c->localid = alloc_channel_id(ssh);\r
- c->closes = 0;\r
- c->pending_close = FALSE;\r
- c->throttling_conn = FALSE;\r
- c->v.v2.locwindow = c->v.v2.locmaxwin = c->v.v2.remlocwin =\r
- ssh->cfg.ssh_simple ? OUR_V2_BIGWIN : OUR_V2_WINSIZE;\r
- c->v.v2.winadj_head = c->v.v2.winadj_tail = NULL;\r
- c->v.v2.throttle_state = UNTHROTTLED;\r
- bufchain_init(&c->v.v2.outbuffer);\r
-}\r
-\r
-/*\r
- * Potentially enlarge the window on an SSH-2 channel.\r
- */\r
-static void ssh2_set_window(struct ssh_channel *c, int newwin)\r
-{\r
- Ssh ssh = c->ssh;\r
-\r
- /*\r
- * Never send WINDOW_ADJUST for a channel that the remote side\r
- * already thinks it's closed; there's no point, since it won't\r
- * be sending any more data anyway.\r
- */\r
- if (c->closes != 0)\r
- return;\r
-\r
- /*\r
- * If the remote end has a habit of ignoring maxpkt, limit the\r
- * window so that it has no choice (assuming it doesn't ignore the\r
- * window as well).\r
- */\r
- if ((ssh->remote_bugs & BUG_SSH2_MAXPKT) && newwin > OUR_V2_MAXPKT)\r
- newwin = OUR_V2_MAXPKT;\r
- \r
-\r
- /*\r
- * Only send a WINDOW_ADJUST if there's significantly more window\r
- * available than the other end thinks there is. This saves us\r
- * sending a WINDOW_ADJUST for every character in a shell session.\r
- *\r
- * "Significant" is arbitrarily defined as half the window size.\r
- */\r
- if (newwin / 2 >= c->v.v2.locwindow) {\r
- struct Packet *pktout;\r
- struct winadj *wa;\r
-\r
- /*\r
- * In order to keep track of how much window the client\r
- * actually has available, we'd like it to acknowledge each\r
- * WINDOW_ADJUST. We can't do that directly, so we accompany\r
- * it with a CHANNEL_REQUEST that has to be acknowledged.\r
- *\r
- * This is only necessary if we're opening the window wide.\r
- * If we're not, then throughput is being constrained by\r
- * something other than the maximum window size anyway.\r
- *\r
- * We also only send this if the main channel has finished its\r
- * initial CHANNEL_REQUESTs and installed the default\r
- * CHANNEL_FAILURE handler, so as not to risk giving it\r
- * unexpected CHANNEL_FAILUREs.\r
- */\r
- if (newwin == c->v.v2.locmaxwin &&\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE]) {\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_addstring(pktout, "winadj@putty.projects.tartarus.org");\r
- ssh2_pkt_addbool(pktout, TRUE);\r
- ssh2_pkt_send(ssh, pktout);\r
-\r
- /*\r
- * CHANNEL_FAILURE doesn't come with any indication of\r
- * what message caused it, so we have to keep track of the\r
- * outstanding CHANNEL_REQUESTs ourselves.\r
- */\r
- wa = snew(struct winadj);\r
- wa->size = newwin - c->v.v2.locwindow;\r
- wa->next = NULL;\r
- if (!c->v.v2.winadj_head)\r
- c->v.v2.winadj_head = wa;\r
- else\r
- c->v.v2.winadj_tail->next = wa;\r
- c->v.v2.winadj_tail = wa;\r
- if (c->v.v2.throttle_state != UNTHROTTLED)\r
- c->v.v2.throttle_state = UNTHROTTLING;\r
- } else {\r
- /* Pretend the WINDOW_ADJUST was acked immediately. */\r
- c->v.v2.remlocwin = newwin;\r
- c->v.v2.throttle_state = THROTTLED;\r
- }\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_adduint32(pktout, newwin - c->v.v2.locwindow);\r
- ssh2_pkt_send(ssh, pktout);\r
- c->v.v2.locwindow = newwin;\r
- }\r
-}\r
-\r
-/*\r
- * Find the channel associated with a message. If there's no channel,\r
- * or it's not properly open, make a noise about it and return NULL.\r
- */\r
-static struct ssh_channel *ssh2_channel_msg(Ssh ssh, struct Packet *pktin)\r
-{\r
- unsigned localid = ssh_pkt_getuint32(pktin);\r
- struct ssh_channel *c;\r
-\r
- c = find234(ssh->channels, &localid, ssh_channelfind);\r
- if (!c ||\r
- (c->halfopen && pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION &&\r
- pktin->type != SSH2_MSG_CHANNEL_OPEN_FAILURE)) {\r
- char *buf = dupprintf("Received %s for %s channel %u",\r
- ssh2_pkt_type(ssh->pkt_kctx, ssh->pkt_actx,\r
- pktin->type),\r
- c ? "half-open" : "nonexistent", localid);\r
- ssh_disconnect(ssh, NULL, buf, SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE);\r
- sfree(buf);\r
- return NULL;\r
- }\r
- return c;\r
-}\r
-\r
-static int ssh2_handle_winadj_response(struct ssh_channel *c)\r
-{\r
- struct winadj *wa = c->v.v2.winadj_head;\r
- if (!wa)\r
- return FALSE;\r
- c->v.v2.winadj_head = wa->next;\r
- c->v.v2.remlocwin += wa->size;\r
- sfree(wa);\r
- /*\r
- * winadj messages are only sent when the window is fully open, so\r
- * if we get an ack of one, we know any pending unthrottle is\r
- * complete.\r
- */\r
- if (c->v.v2.throttle_state == UNTHROTTLING)\r
- c->v.v2.throttle_state = UNTHROTTLED;\r
- return TRUE;\r
-}\r
-\r
-static void ssh2_msg_channel_success(Ssh ssh, struct Packet *pktin)\r
-{\r
- /*\r
- * This should never get called. All channel requests are either\r
- * sent with want_reply false, are sent before this handler gets\r
- * installed, or are "winadj@putty" requests, which servers should\r
- * never respond to with success.\r
- *\r
- * However, at least one server ("boks_sshd") is known to return\r
- * SUCCESS for channel requests it's never heard of, such as\r
- * "winadj@putty". Raised with foxt.com as bug 090916-090424, but\r
- * for the sake of a quiet life, we handle it just the same as the\r
- * expected FAILURE.\r
- */\r
- struct ssh_channel *c;\r
-\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
- if (!ssh2_handle_winadj_response(c))\r
- ssh_disconnect(ssh, NULL,\r
- "Received unsolicited SSH_MSG_CHANNEL_SUCCESS",\r
- SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE);\r
-}\r
-\r
-static void ssh2_msg_channel_failure(Ssh ssh, struct Packet *pktin)\r
-{\r
- /*\r
- * The only time this should get called is for "winadj@putty"\r
- * messages sent above. All other channel requests are either\r
- * sent with want_reply false or are sent before this handler gets\r
- * installed.\r
- */\r
- struct ssh_channel *c;\r
-\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
- if (!ssh2_handle_winadj_response(c))\r
- ssh_disconnect(ssh, NULL,\r
- "Received unsolicited SSH_MSG_CHANNEL_FAILURE",\r
- SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE);\r
-}\r
-\r
-static void ssh2_msg_channel_window_adjust(Ssh ssh, struct Packet *pktin)\r
-{\r
- struct ssh_channel *c;\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
- if (!c->closes) {\r
- c->v.v2.remwindow += ssh_pkt_getuint32(pktin);\r
- ssh2_try_send_and_unthrottle(ssh, c);\r
- }\r
-}\r
-\r
-static void ssh2_msg_channel_data(Ssh ssh, struct Packet *pktin)\r
-{\r
- char *data;\r
- int length;\r
- struct ssh_channel *c;\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
- if (pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA &&\r
- ssh_pkt_getuint32(pktin) != SSH2_EXTENDED_DATA_STDERR)\r
- return; /* extended but not stderr */\r
- ssh_pkt_getstring(pktin, &data, &length);\r
- if (data) {\r
- int bufsize = 0;\r
- c->v.v2.locwindow -= length;\r
- c->v.v2.remlocwin -= length;\r
- switch (c->type) {\r
- case CHAN_MAINSESSION:\r
- bufsize =\r
- from_backend(ssh->frontend, pktin->type ==\r
- SSH2_MSG_CHANNEL_EXTENDED_DATA,\r
- data, length);\r
- break;\r
- case CHAN_X11:\r
- bufsize = x11_send(c->u.x11.s, data, length);\r
- break;\r
- case CHAN_SOCKDATA:\r
- bufsize = pfd_send(c->u.pfd.s, data, length);\r
- break;\r
- case CHAN_AGENT:\r
- while (length > 0) {\r
- if (c->u.a.lensofar < 4) {\r
- unsigned int l = min(4 - c->u.a.lensofar,\r
- (unsigned)length);\r
- memcpy(c->u.a.msglen + c->u.a.lensofar,\r
- data, l);\r
- data += l;\r
- length -= l;\r
- c->u.a.lensofar += l;\r
- }\r
- if (c->u.a.lensofar == 4) {\r
- c->u.a.totallen =\r
- 4 + GET_32BIT(c->u.a.msglen);\r
- c->u.a.message = snewn(c->u.a.totallen,\r
- unsigned char);\r
- memcpy(c->u.a.message, c->u.a.msglen, 4);\r
- }\r
- if (c->u.a.lensofar >= 4 && length > 0) {\r
- unsigned int l =\r
- min(c->u.a.totallen - c->u.a.lensofar,\r
- (unsigned)length);\r
- memcpy(c->u.a.message + c->u.a.lensofar,\r
- data, l);\r
- data += l;\r
- length -= l;\r
- c->u.a.lensofar += l;\r
- }\r
- if (c->u.a.lensofar == c->u.a.totallen) {\r
- void *reply;\r
- int replylen;\r
- if (agent_query(c->u.a.message,\r
- c->u.a.totallen,\r
- &reply, &replylen,\r
- ssh_agentf_callback, c))\r
- ssh_agentf_callback(c, reply, replylen);\r
- sfree(c->u.a.message);\r
- c->u.a.lensofar = 0;\r
- }\r
- }\r
- bufsize = 0;\r
- break;\r
- }\r
- /*\r
- * If it looks like the remote end hit the end of its window,\r
- * and we didn't want it to do that, think about using a\r
- * larger window.\r
- */\r
- if (c->v.v2.remlocwin <= 0 && c->v.v2.throttle_state == UNTHROTTLED &&\r
- c->v.v2.locmaxwin < 0x40000000)\r
- c->v.v2.locmaxwin += OUR_V2_WINSIZE;\r
- /*\r
- * If we are not buffering too much data,\r
- * enlarge the window again at the remote side.\r
- * If we are buffering too much, we may still\r
- * need to adjust the window if the server's\r
- * sent excess data.\r
- */\r
- ssh2_set_window(c, bufsize < c->v.v2.locmaxwin ?\r
- c->v.v2.locmaxwin - bufsize : 0);\r
- /*\r
- * If we're either buffering way too much data, or if we're\r
- * buffering anything at all and we're in "simple" mode,\r
- * throttle the whole channel.\r
- */\r
- if ((bufsize > c->v.v2.locmaxwin ||\r
- (ssh->cfg.ssh_simple && bufsize > 0)) &&\r
- !c->throttling_conn) {\r
- c->throttling_conn = 1;\r
- ssh_throttle_conn(ssh, +1);\r
- }\r
- }\r
-}\r
-\r
-static void ssh2_msg_channel_eof(Ssh ssh, struct Packet *pktin)\r
-{\r
- struct ssh_channel *c;\r
-\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
-\r
- if (c->type == CHAN_X11) {\r
- /*\r
- * Remote EOF on an X11 channel means we should\r
- * wrap up and close the channel ourselves.\r
- */\r
- x11_close(c->u.x11.s);\r
- c->u.x11.s = NULL;\r
- sshfwd_close(c);\r
- } else if (c->type == CHAN_AGENT) {\r
- sshfwd_close(c);\r
- } else if (c->type == CHAN_SOCKDATA) {\r
- pfd_close(c->u.pfd.s);\r
- c->u.pfd.s = NULL;\r
- sshfwd_close(c);\r
- }\r
-}\r
-\r
-static void ssh2_msg_channel_close(Ssh ssh, struct Packet *pktin)\r
-{\r
- struct ssh_channel *c;\r
- struct Packet *pktout;\r
-\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
- /* Do pre-close processing on the channel. */\r
- switch (c->type) {\r
- case CHAN_MAINSESSION:\r
- ssh->mainchan = NULL;\r
- update_specials_menu(ssh->frontend);\r
- break;\r
- case CHAN_X11:\r
- if (c->u.x11.s != NULL)\r
- x11_close(c->u.x11.s);\r
- sshfwd_close(c);\r
- break;\r
- case CHAN_AGENT:\r
- sshfwd_close(c);\r
- break;\r
- case CHAN_SOCKDATA:\r
- if (c->u.pfd.s != NULL)\r
- pfd_close(c->u.pfd.s);\r
- sshfwd_close(c);\r
- break;\r
- }\r
- if (c->closes == 0) {\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
- del234(ssh->channels, c);\r
- bufchain_clear(&c->v.v2.outbuffer);\r
- sfree(c);\r
-\r
- /*\r
- * See if that was the last channel left open.\r
- * (This is only our termination condition if we're\r
- * not running in -N mode.)\r
- */\r
- if (!ssh->cfg.ssh_no_shell && count234(ssh->channels) == 0) {\r
- /*\r
- * We used to send SSH_MSG_DISCONNECT here,\r
- * because I'd believed that _every_ conforming\r
- * SSH-2 connection had to end with a disconnect\r
- * being sent by at least one side; apparently\r
- * I was wrong and it's perfectly OK to\r
- * unceremoniously slam the connection shut\r
- * when you're done, and indeed OpenSSH feels\r
- * this is more polite than sending a\r
- * DISCONNECT. So now we don't.\r
- */\r
- ssh_disconnect(ssh, "All channels closed", NULL, 0, TRUE);\r
- }\r
-}\r
-\r
-static void ssh2_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin)\r
-{\r
- struct ssh_channel *c;\r
- struct Packet *pktout;\r
-\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
- if (c->type != CHAN_SOCKDATA_DORMANT)\r
- return; /* dunno why they're confirming this */\r
- c->remoteid = ssh_pkt_getuint32(pktin);\r
- c->halfopen = FALSE;\r
- c->type = CHAN_SOCKDATA;\r
- c->v.v2.remwindow = ssh_pkt_getuint32(pktin);\r
- c->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin);\r
- if (c->u.pfd.s)\r
- pfd_confirm(c->u.pfd.s);\r
- if (c->closes) {\r
- /*\r
- * We have a pending close on this channel,\r
- * which we decided on before the server acked\r
- * the channel open. So now we know the\r
- * remoteid, we can close it again.\r
- */\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
-}\r
-\r
-static void ssh2_msg_channel_open_failure(Ssh ssh, struct Packet *pktin)\r
-{\r
- static const char *const reasons[] = {\r
- "<unknown reason code>",\r
- "Administratively prohibited",\r
- "Connect failed",\r
- "Unknown channel type",\r
- "Resource shortage",\r
- };\r
- unsigned reason_code;\r
- char *reason_string;\r
- int reason_length;\r
- struct ssh_channel *c;\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
- if (c->type != CHAN_SOCKDATA_DORMANT)\r
- return; /* dunno why they're failing this */\r
-\r
- reason_code = ssh_pkt_getuint32(pktin);\r
- if (reason_code >= lenof(reasons))\r
- reason_code = 0; /* ensure reasons[reason_code] in range */\r
- ssh_pkt_getstring(pktin, &reason_string, &reason_length);\r
- logeventf(ssh, "Forwarded connection refused by server: %s [%.*s]",\r
- reasons[reason_code], reason_length, reason_string);\r
-\r
- pfd_close(c->u.pfd.s);\r
-\r
- del234(ssh->channels, c);\r
- sfree(c);\r
-}\r
-\r
-static void ssh2_msg_channel_request(Ssh ssh, struct Packet *pktin)\r
-{\r
- char *type;\r
- int typelen, want_reply;\r
- int reply = SSH2_MSG_CHANNEL_FAILURE; /* default */\r
- struct ssh_channel *c;\r
- struct Packet *pktout;\r
-\r
- c = ssh2_channel_msg(ssh, pktin);\r
- if (!c)\r
- return;\r
- ssh_pkt_getstring(pktin, &type, &typelen);\r
- want_reply = ssh2_pkt_getbool(pktin);\r
-\r
- /*\r
- * Having got the channel number, we now look at\r
- * the request type string to see if it's something\r
- * we recognise.\r
- */\r
- if (c == ssh->mainchan) {\r
- /*\r
- * We recognise "exit-status" and "exit-signal" on\r
- * the primary channel.\r
- */\r
- if (typelen == 11 &&\r
- !memcmp(type, "exit-status", 11)) {\r
-\r
- ssh->exitcode = ssh_pkt_getuint32(pktin);\r
- logeventf(ssh, "Server sent command exit status %d",\r
- ssh->exitcode);\r
- reply = SSH2_MSG_CHANNEL_SUCCESS;\r
-\r
- } else if (typelen == 11 &&\r
- !memcmp(type, "exit-signal", 11)) {\r
-\r
- int is_plausible = TRUE, is_int = FALSE;\r
- char *fmt_sig = "", *fmt_msg = "";\r
- char *msg;\r
- int msglen = 0, core = FALSE;\r
- /* ICK: older versions of OpenSSH (e.g. 3.4p1)\r
- * provide an `int' for the signal, despite its\r
- * having been a `string' in the drafts of RFC 4254 since at\r
- * least 2001. (Fixed in session.c 1.147.) Try to\r
- * infer which we can safely parse it as. */\r
- {\r
- unsigned char *p = pktin->body +\r
- pktin->savedpos;\r
- long len = pktin->length - pktin->savedpos;\r
- unsigned long num = GET_32BIT(p); /* what is it? */\r
- /* If it's 0, it hardly matters; assume string */\r
- if (num == 0) {\r
- is_int = FALSE;\r
- } else {\r
- int maybe_int = FALSE, maybe_str = FALSE;\r
-#define CHECK_HYPOTHESIS(offset, result) \\r
- do { \\r
- long q = offset; \\r
- if (q >= 0 && q+4 <= len) { \\r
- q = q + 4 + GET_32BIT(p+q); \\r
- if (q >= 0 && q+4 <= len && \\r
- ((q = q + 4 + GET_32BIT(p+q))!= 0) && q == len) \\r
- result = TRUE; \\r
- } \\r
- } while(0)\r
- CHECK_HYPOTHESIS(4+1, maybe_int);\r
- CHECK_HYPOTHESIS(4+num+1, maybe_str);\r
-#undef CHECK_HYPOTHESIS\r
- if (maybe_int && !maybe_str)\r
- is_int = TRUE;\r
- else if (!maybe_int && maybe_str)\r
- is_int = FALSE;\r
- else\r
- /* Crikey. Either or neither. Panic. */\r
- is_plausible = FALSE;\r
- }\r
- }\r
- ssh->exitcode = 128; /* means `unknown signal' */\r
- if (is_plausible) {\r
- if (is_int) {\r
- /* Old non-standard OpenSSH. */\r
- int signum = ssh_pkt_getuint32(pktin);\r
- fmt_sig = dupprintf(" %d", signum);\r
- ssh->exitcode = 128 + signum;\r
- } else {\r
- /* As per RFC 4254. */\r
- char *sig;\r
- int siglen;\r
- ssh_pkt_getstring(pktin, &sig, &siglen);\r
- /* Signal name isn't supposed to be blank, but\r
- * let's cope gracefully if it is. */\r
- if (siglen) {\r
- fmt_sig = dupprintf(" \"%.*s\"",\r
- siglen, sig);\r
- }\r
-\r
- /*\r
- * Really hideous method of translating the\r
- * signal description back into a locally\r
- * meaningful number.\r
- */\r
-\r
- if (0)\r
- ;\r
-#define TRANSLATE_SIGNAL(s) \\r
- else if (siglen == lenof(#s)-1 && !memcmp(sig, #s, siglen)) \\r
- ssh->exitcode = 128 + SIG ## s\r
-#ifdef SIGABRT\r
- TRANSLATE_SIGNAL(ABRT);\r
-#endif\r
-#ifdef SIGALRM\r
- TRANSLATE_SIGNAL(ALRM);\r
-#endif\r
-#ifdef SIGFPE\r
- TRANSLATE_SIGNAL(FPE);\r
-#endif\r
-#ifdef SIGHUP\r
- TRANSLATE_SIGNAL(HUP);\r
-#endif\r
-#ifdef SIGILL\r
- TRANSLATE_SIGNAL(ILL);\r
-#endif\r
-#ifdef SIGINT\r
- TRANSLATE_SIGNAL(INT);\r
-#endif\r
-#ifdef SIGKILL\r
- TRANSLATE_SIGNAL(KILL);\r
-#endif\r
-#ifdef SIGPIPE\r
- TRANSLATE_SIGNAL(PIPE);\r
-#endif\r
-#ifdef SIGQUIT\r
- TRANSLATE_SIGNAL(QUIT);\r
-#endif\r
-#ifdef SIGSEGV\r
- TRANSLATE_SIGNAL(SEGV);\r
-#endif\r
-#ifdef SIGTERM\r
- TRANSLATE_SIGNAL(TERM);\r
-#endif\r
-#ifdef SIGUSR1\r
- TRANSLATE_SIGNAL(USR1);\r
-#endif\r
-#ifdef SIGUSR2\r
- TRANSLATE_SIGNAL(USR2);\r
-#endif\r
-#undef TRANSLATE_SIGNAL\r
- else\r
- ssh->exitcode = 128;\r
- }\r
- core = ssh2_pkt_getbool(pktin);\r
- ssh_pkt_getstring(pktin, &msg, &msglen);\r
- if (msglen) {\r
- fmt_msg = dupprintf(" (\"%.*s\")", msglen, msg);\r
- }\r
- /* ignore lang tag */\r
- } /* else don't attempt to parse */\r
- logeventf(ssh, "Server exited on signal%s%s%s",\r
- fmt_sig, core ? " (core dumped)" : "",\r
- fmt_msg);\r
- if (*fmt_sig) sfree(fmt_sig);\r
- if (*fmt_msg) sfree(fmt_msg);\r
- reply = SSH2_MSG_CHANNEL_SUCCESS;\r
-\r
- }\r
- } else {\r
- /*\r
- * This is a channel request we don't know\r
- * about, so we now either ignore the request\r
- * or respond with CHANNEL_FAILURE, depending\r
- * on want_reply.\r
- */\r
- reply = SSH2_MSG_CHANNEL_FAILURE;\r
- }\r
- if (want_reply) {\r
- pktout = ssh2_pkt_init(reply);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
-}\r
-\r
-static void ssh2_msg_global_request(Ssh ssh, struct Packet *pktin)\r
-{\r
- char *type;\r
- int typelen, want_reply;\r
- struct Packet *pktout;\r
-\r
- ssh_pkt_getstring(pktin, &type, &typelen);\r
- want_reply = ssh2_pkt_getbool(pktin);\r
-\r
- /*\r
- * We currently don't support any global requests\r
- * at all, so we either ignore the request or\r
- * respond with REQUEST_FAILURE, depending on\r
- * want_reply.\r
- */\r
- if (want_reply) {\r
- pktout = ssh2_pkt_init(SSH2_MSG_REQUEST_FAILURE);\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
-}\r
-\r
-static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)\r
-{\r
- char *type;\r
- int typelen;\r
- char *peeraddr;\r
- int peeraddrlen;\r
- int peerport;\r
- char *error = NULL;\r
- struct ssh_channel *c;\r
- unsigned remid, winsize, pktsize;\r
- struct Packet *pktout;\r
-\r
- ssh_pkt_getstring(pktin, &type, &typelen);\r
- c = snew(struct ssh_channel);\r
- c->ssh = ssh;\r
-\r
- remid = ssh_pkt_getuint32(pktin);\r
- winsize = ssh_pkt_getuint32(pktin);\r
- pktsize = ssh_pkt_getuint32(pktin);\r
-\r
- if (typelen == 3 && !memcmp(type, "x11", 3)) {\r
- char *addrstr;\r
- const char *x11err;\r
-\r
- ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen);\r
- addrstr = snewn(peeraddrlen+1, char);\r
- memcpy(addrstr, peeraddr, peeraddrlen);\r
- addrstr[peeraddrlen] = '\0';\r
- peerport = ssh_pkt_getuint32(pktin);\r
-\r
- logeventf(ssh, "Received X11 connect request from %s:%d",\r
- addrstr, peerport);\r
-\r
- if (!ssh->X11_fwd_enabled)\r
- error = "X11 forwarding is not enabled";\r
- else if ((x11err = x11_init(&c->u.x11.s, ssh->x11disp, c,\r
- addrstr, peerport, &ssh->cfg)) != NULL) {\r
- logeventf(ssh, "Local X11 connection failed: %s", x11err);\r
- error = "Unable to open an X11 connection";\r
- } else {\r
- logevent("Opening X11 forward connection succeeded");\r
- c->type = CHAN_X11;\r
- }\r
-\r
- sfree(addrstr);\r
- } else if (typelen == 15 &&\r
- !memcmp(type, "forwarded-tcpip", 15)) {\r
- struct ssh_rportfwd pf, *realpf;\r
- char *dummy;\r
- int dummylen;\r
- ssh_pkt_getstring(pktin, &dummy, &dummylen);/* skip address */\r
- pf.sport = ssh_pkt_getuint32(pktin);\r
- ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen);\r
- peerport = ssh_pkt_getuint32(pktin);\r
- realpf = find234(ssh->rportfwds, &pf, NULL);\r
- logeventf(ssh, "Received remote port %d open request "\r
- "from %s:%d", pf.sport, peeraddr, peerport);\r
- if (realpf == NULL) {\r
- error = "Remote port is not recognised";\r
- } else {\r
- const char *e = pfd_newconnect(&c->u.pfd.s,\r
- realpf->dhost,\r
- realpf->dport, c,\r
- &ssh->cfg,\r
- realpf->pfrec->addressfamily);\r
- logeventf(ssh, "Attempting to forward remote port to "\r
- "%s:%d", realpf->dhost, realpf->dport);\r
- if (e != NULL) {\r
- logeventf(ssh, "Port open failed: %s", e);\r
- error = "Port open failed";\r
- } else {\r
- logevent("Forwarded port opened successfully");\r
- c->type = CHAN_SOCKDATA;\r
- }\r
- }\r
- } else if (typelen == 22 &&\r
- !memcmp(type, "auth-agent@openssh.com", 22)) {\r
- if (!ssh->agentfwd_enabled)\r
- error = "Agent forwarding is not enabled";\r
- else {\r
- c->type = CHAN_AGENT; /* identify channel type */\r
- c->u.a.lensofar = 0;\r
- }\r
- } else {\r
- error = "Unsupported channel type requested";\r
- }\r
-\r
- c->remoteid = remid;\r
- c->halfopen = FALSE;\r
- if (error) {\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_adduint32(pktout, SSH2_OPEN_CONNECT_FAILED);\r
- ssh2_pkt_addstring(pktout, error);\r
- ssh2_pkt_addstring(pktout, "en"); /* language tag */\r
- ssh2_pkt_send(ssh, pktout);\r
- logeventf(ssh, "Rejected channel open: %s", error);\r
- sfree(c);\r
- } else {\r
- ssh2_channel_init(c);\r
- c->v.v2.remwindow = winsize;\r
- c->v.v2.remmaxpkt = pktsize;\r
- add234(ssh->channels, c);\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);\r
- ssh2_pkt_adduint32(pktout, c->remoteid);\r
- ssh2_pkt_adduint32(pktout, c->localid);\r
- ssh2_pkt_adduint32(pktout, c->v.v2.locwindow);\r
- ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
-}\r
-\r
-/*\r
- * Buffer banner messages for later display at some convenient point,\r
- * if we're going to display them.\r
- */\r
-static void ssh2_msg_userauth_banner(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* Arbitrary limit to prevent unbounded inflation of buffer */\r
- if (ssh->cfg.ssh_show_banner &&\r
- bufchain_size(&ssh->banner) <= 131072) {\r
- char *banner = NULL;\r
- int size = 0;\r
- ssh_pkt_getstring(pktin, &banner, &size);\r
- if (banner)\r
- bufchain_add(&ssh->banner, banner, size);\r
- }\r
-}\r
-\r
-/* Helper function to deal with sending tty modes for "pty-req" */\r
-static void ssh2_send_ttymode(void *data, char *mode, char *val)\r
-{\r
- struct Packet *pktout = (struct Packet *)data;\r
- int i = 0;\r
- unsigned int arg = 0;\r
- while (strcmp(mode, ssh_ttymodes[i].mode) != 0) i++;\r
- if (i == lenof(ssh_ttymodes)) return;\r
- switch (ssh_ttymodes[i].type) {\r
- case TTY_OP_CHAR:\r
- arg = ssh_tty_parse_specchar(val);\r
- break;\r
- case TTY_OP_BOOL:\r
- arg = ssh_tty_parse_boolean(val);\r
- break;\r
- }\r
- ssh2_pkt_addbyte(pktout, ssh_ttymodes[i].opcode);\r
- ssh2_pkt_adduint32(pktout, arg);\r
-}\r
-\r
-/*\r
- * Handle the SSH-2 userauth and connection layers.\r
- */\r
-static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,\r
- struct Packet *pktin)\r
-{\r
- struct do_ssh2_authconn_state {\r
- enum {\r
- AUTH_TYPE_NONE,\r
- AUTH_TYPE_PUBLICKEY,\r
- AUTH_TYPE_PUBLICKEY_OFFER_LOUD,\r
- AUTH_TYPE_PUBLICKEY_OFFER_QUIET,\r
- AUTH_TYPE_PASSWORD,\r
- AUTH_TYPE_GSSAPI,\r
- AUTH_TYPE_KEYBOARD_INTERACTIVE,\r
- AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET\r
- } type;\r
- int done_service_req;\r
- int gotit, need_pw, can_pubkey, can_passwd, can_keyb_inter;\r
- int tried_pubkey_config, done_agent;\r
-#ifndef NO_GSSAPI\r
- int can_gssapi;\r
- int tried_gssapi;\r
-#endif\r
- int kbd_inter_refused;\r
- int we_are_in, userauth_success;\r
- prompts_t *cur_prompt;\r
- int num_prompts;\r
- char username[100];\r
- char *password;\r
- int got_username;\r
- void *publickey_blob;\r
- int publickey_bloblen;\r
- int publickey_encrypted;\r
- char *publickey_algorithm;\r
- char *publickey_comment;\r
- unsigned char agent_request[5], *agent_response, *agentp;\r
- int agent_responselen;\r
- unsigned char *pkblob_in_agent;\r
- int keyi, nkeys;\r
- char *pkblob, *alg, *commentp;\r
- int pklen, alglen, commentlen;\r
- int siglen, retlen, len;\r
- char *q, *agentreq, *ret;\r
- int try_send;\r
- int num_env, env_left, env_ok;\r
- struct Packet *pktout;\r
-#ifndef NO_GSSAPI\r
- struct ssh_gss_library *gsslib;\r
- Ssh_gss_ctx gss_ctx;\r
- Ssh_gss_buf gss_buf;\r
- Ssh_gss_buf gss_rcvtok, gss_sndtok;\r
- Ssh_gss_name gss_srv_name;\r
- Ssh_gss_stat gss_stat;\r
-#endif\r
- };\r
- crState(do_ssh2_authconn_state);\r
-\r
- crBegin(ssh->do_ssh2_authconn_crstate);\r
-\r
- s->done_service_req = FALSE;\r
- s->we_are_in = s->userauth_success = FALSE;\r
-#ifndef NO_GSSAPI\r
- s->tried_gssapi = FALSE;\r
-#endif\r
-\r
- if (!ssh->cfg.ssh_no_userauth) {\r
- /*\r
- * Request userauth protocol, and await a response to it.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, "ssh-userauth");\r
- ssh2_pkt_send(ssh, s->pktout);\r
- crWaitUntilV(pktin);\r
- if (pktin->type == SSH2_MSG_SERVICE_ACCEPT)\r
- s->done_service_req = TRUE;\r
- }\r
- if (!s->done_service_req) {\r
- /*\r
- * Request connection protocol directly, without authentication.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- ssh2_pkt_send(ssh, s->pktout);\r
- crWaitUntilV(pktin);\r
- if (pktin->type == SSH2_MSG_SERVICE_ACCEPT) {\r
- s->we_are_in = TRUE; /* no auth required */\r
- } else {\r
- bombout(("Server refused service request"));\r
- crStopV;\r
- }\r
- }\r
-\r
- /* Arrange to be able to deal with any BANNERs that come in.\r
- * (We do this now as packets may come in during the next bit.) */\r
- bufchain_init(&ssh->banner);\r
- ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] =\r
- ssh2_msg_userauth_banner;\r
-\r
- /*\r
- * Misc one-time setup for authentication.\r
- */\r
- s->publickey_blob = NULL;\r
- if (!s->we_are_in) {\r
-\r
- /*\r
- * Load the public half of any configured public key file\r
- * for later use.\r
- */\r
- if (!filename_is_null(ssh->cfg.keyfile)) {\r
- int keytype;\r
- logeventf(ssh, "Reading private key file \"%.150s\"",\r
- filename_to_str(&ssh->cfg.keyfile));\r
- keytype = key_type(&ssh->cfg.keyfile);\r
- if (keytype == SSH_KEYTYPE_SSH2) {\r
- const char *error;\r
- s->publickey_blob =\r
- ssh2_userkey_loadpub(&ssh->cfg.keyfile,\r
- &s->publickey_algorithm,\r
- &s->publickey_bloblen, \r
- &s->publickey_comment, &error);\r
- if (s->publickey_blob) {\r
- s->publickey_encrypted =\r
- ssh2_userkey_encrypted(&ssh->cfg.keyfile, NULL);\r
- } else {\r
- char *msgbuf;\r
- logeventf(ssh, "Unable to load private key (%s)", \r
- error);\r
- msgbuf = dupprintf("Unable to load private key file "\r
- "\"%.150s\" (%s)\r\n",\r
- filename_to_str(&ssh->cfg.keyfile),\r
- error);\r
- c_write_str(ssh, msgbuf);\r
- sfree(msgbuf);\r
- }\r
- } else {\r
- char *msgbuf;\r
- logeventf(ssh, "Unable to use this key file (%s)",\r
- key_type_to_str(keytype));\r
- msgbuf = dupprintf("Unable to use key file \"%.150s\""\r
- " (%s)\r\n",\r
- filename_to_str(&ssh->cfg.keyfile),\r
- key_type_to_str(keytype));\r
- c_write_str(ssh, msgbuf);\r
- sfree(msgbuf);\r
- s->publickey_blob = NULL;\r
- }\r
- }\r
-\r
- /*\r
- * Find out about any keys Pageant has (but if there's a\r
- * public key configured, filter out all others).\r
- */\r
- s->nkeys = 0;\r
- s->agent_response = NULL;\r
- s->pkblob_in_agent = NULL;\r
- if (ssh->cfg.tryagent && agent_exists()) {\r
-\r
- void *r;\r
-\r
- logevent("Pageant is running. Requesting keys.");\r
-\r
- /* Request the keys held by the agent. */\r
- PUT_32BIT(s->agent_request, 1);\r
- s->agent_request[4] = SSH2_AGENTC_REQUEST_IDENTITIES;\r
- if (!agent_query(s->agent_request, 5, &r, &s->agent_responselen,\r
- ssh_agent_callback, ssh)) {\r
- do {\r
- crReturnV;\r
- if (pktin) {\r
- bombout(("Unexpected data from server while"\r
- " waiting for agent response"));\r
- crStopV;\r
- }\r
- } while (pktin || inlen > 0);\r
- r = ssh->agent_response;\r
- s->agent_responselen = ssh->agent_response_len;\r
- }\r
- s->agent_response = (unsigned char *) r;\r
- if (s->agent_response && s->agent_responselen >= 5 &&\r
- s->agent_response[4] == SSH2_AGENT_IDENTITIES_ANSWER) {\r
- int keyi;\r
- unsigned char *p;\r
- p = s->agent_response + 5;\r
- s->nkeys = GET_32BIT(p);\r
- p += 4;\r
- logeventf(ssh, "Pageant has %d SSH-2 keys", s->nkeys);\r
- if (s->publickey_blob) {\r
- /* See if configured key is in agent. */\r
- for (keyi = 0; keyi < s->nkeys; keyi++) {\r
- s->pklen = GET_32BIT(p);\r
- if (s->pklen == s->publickey_bloblen &&\r
- !memcmp(p+4, s->publickey_blob,\r
- s->publickey_bloblen)) {\r
- logeventf(ssh, "Pageant key #%d matches "\r
- "configured key file", keyi);\r
- s->keyi = keyi;\r
- s->pkblob_in_agent = p;\r
- break;\r
- }\r
- p += 4 + s->pklen;\r
- p += GET_32BIT(p) + 4; /* comment */\r
- }\r
- if (!s->pkblob_in_agent) {\r
- logevent("Configured key file not in Pageant");\r
- s->nkeys = 0;\r
- }\r
- }\r
- } else {\r
- logevent("Failed to get reply from Pageant");\r
- }\r
- }\r
-\r
- }\r
-\r
- /*\r
- * We repeat this whole loop, including the username prompt,\r
- * until we manage a successful authentication. If the user\r
- * types the wrong _password_, they can be sent back to the\r
- * beginning to try another username, if this is configured on.\r
- * (If they specify a username in the config, they are never\r
- * asked, even if they do give a wrong password.)\r
- * \r
- * I think this best serves the needs of\r
- * \r
- * - the people who have no configuration, no keys, and just\r
- * want to try repeated (username,password) pairs until they\r
- * type both correctly\r
- * \r
- * - people who have keys and configuration but occasionally\r
- * need to fall back to passwords\r
- * \r
- * - people with a key held in Pageant, who might not have\r
- * logged in to a particular machine before; so they want to\r
- * type a username, and then _either_ their key will be\r
- * accepted, _or_ they will type a password. If they mistype\r
- * the username they will want to be able to get back and\r
- * retype it!\r
- */\r
- s->username[0] = '\0';\r
- s->got_username = FALSE;\r
- while (!s->we_are_in) {\r
- /*\r
- * Get a username.\r
- */\r
- if (s->got_username && !ssh->cfg.change_username) {\r
- /*\r
- * We got a username last time round this loop, and\r
- * with change_username turned off we don't try to get\r
- * it again.\r
- */\r
- } else if (!get_remote_username(&ssh->cfg, s->username,\r
- sizeof(s->username))) {\r
- int ret; /* need not be kept over crReturn */\r
- s->cur_prompt = new_prompts(ssh->frontend);\r
- s->cur_prompt->to_server = TRUE;\r
- s->cur_prompt->name = dupstr("SSH login name");\r
- add_prompt(s->cur_prompt, dupstr("login as: "), TRUE,\r
- lenof(s->username)); \r
- ret = get_userpass_input(s->cur_prompt, NULL, 0);\r
- while (ret < 0) {\r
- ssh->send_ok = 1;\r
- crWaitUntilV(!pktin);\r
- ret = get_userpass_input(s->cur_prompt, in, inlen);\r
- ssh->send_ok = 0;\r
- }\r
- if (!ret) {\r
- /*\r
- * get_userpass_input() failed to get a username.\r
- * Terminate.\r
- */\r
- free_prompts(s->cur_prompt);\r
- ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE);\r
- crStopV;\r
- }\r
- memcpy(s->username, s->cur_prompt->prompts[0]->result,\r
- lenof(s->username));\r
- free_prompts(s->cur_prompt);\r
- } else {\r
- char *stuff;\r
- if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {\r
- stuff = dupprintf("Using username \"%s\".\r\n", s->username);\r
- c_write_str(ssh, stuff);\r
- sfree(stuff);\r
- }\r
- }\r
- s->got_username = TRUE;\r
-\r
- /*\r
- * Send an authentication request using method "none": (a)\r
- * just in case it succeeds, and (b) so that we know what\r
- * authentication methods we can usefully try next.\r
- */\r
- ssh->pkt_actx = SSH2_PKTCTX_NOAUTH;\r
-\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");/* service requested */\r
- ssh2_pkt_addstring(s->pktout, "none"); /* method */\r
- ssh2_pkt_send(ssh, s->pktout);\r
- s->type = AUTH_TYPE_NONE;\r
- s->gotit = FALSE;\r
- s->we_are_in = FALSE;\r
-\r
- s->tried_pubkey_config = FALSE;\r
- s->kbd_inter_refused = FALSE;\r
-\r
- /* Reset agent request state. */\r
- s->done_agent = FALSE;\r
- if (s->agent_response) {\r
- if (s->pkblob_in_agent) {\r
- s->agentp = s->pkblob_in_agent;\r
- } else {\r
- s->agentp = s->agent_response + 5 + 4;\r
- s->keyi = 0;\r
- }\r
- }\r
-\r
- while (1) {\r
- char *methods = NULL;\r
- int methlen = 0;\r
-\r
- /*\r
- * Wait for the result of the last authentication request.\r
- */\r
- if (!s->gotit)\r
- crWaitUntilV(pktin);\r
- /*\r
- * Now is a convenient point to spew any banner material\r
- * that we've accumulated. (This should ensure that when\r
- * we exit the auth loop, we haven't any left to deal\r
- * with.)\r
- */\r
- {\r
- int size = bufchain_size(&ssh->banner);\r
- /*\r
- * Don't show the banner if we're operating in\r
- * non-verbose non-interactive mode. (It's probably\r
- * a script, which means nobody will read the\r
- * banner _anyway_, and moreover the printing of\r
- * the banner will screw up processing on the\r
- * output of (say) plink.)\r
- */\r
- if (size && (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE))) {\r
- char *banner = snewn(size, char);\r
- bufchain_fetch(&ssh->banner, banner, size);\r
- c_write_untrusted(ssh, banner, size);\r
- sfree(banner);\r
- }\r
- bufchain_clear(&ssh->banner);\r
- }\r
- if (pktin->type == SSH2_MSG_USERAUTH_SUCCESS) {\r
- logevent("Access granted");\r
- s->we_are_in = s->userauth_success = TRUE;\r
- break;\r
- }\r
-\r
- if (pktin->type != SSH2_MSG_USERAUTH_FAILURE && s->type != AUTH_TYPE_GSSAPI) {\r
- bombout(("Strange packet received during authentication: "\r
- "type %d", pktin->type));\r
- crStopV;\r
- }\r
-\r
- s->gotit = FALSE;\r
-\r
- /*\r
- * OK, we're now sitting on a USERAUTH_FAILURE message, so\r
- * we can look at the string in it and know what we can\r
- * helpfully try next.\r
- */\r
- if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) {\r
- ssh_pkt_getstring(pktin, &methods, &methlen);\r
- if (!ssh2_pkt_getbool(pktin)) {\r
- /*\r
- * We have received an unequivocal Access\r
- * Denied. This can translate to a variety of\r
- * messages:\r
- * \r
- * - if we'd just tried "none" authentication,\r
- * it's not worth printing anything at all\r
- * \r
- * - if we'd just tried a public key _offer_,\r
- * the message should be "Server refused our\r
- * key" (or no message at all if the key\r
- * came from Pageant)\r
- * \r
- * - if we'd just tried anything else, the\r
- * message really should be "Access denied".\r
- * \r
- * Additionally, if we'd just tried password\r
- * authentication, we should break out of this\r
- * whole loop so as to go back to the username\r
- * prompt (iff we're configured to allow\r
- * username change attempts).\r
- */\r
- if (s->type == AUTH_TYPE_NONE) {\r
- /* do nothing */\r
- } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD ||\r
- s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) {\r
- if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD)\r
- c_write_str(ssh, "Server refused our key\r\n");\r
- logevent("Server refused public key");\r
- } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) {\r
- /* server declined keyboard-interactive; ignore */\r
- } else {\r
- c_write_str(ssh, "Access denied\r\n");\r
- logevent("Access denied");\r
- if (s->type == AUTH_TYPE_PASSWORD &&\r
- ssh->cfg.change_username) {\r
- /* XXX perhaps we should allow\r
- * keyboard-interactive to do this too? */\r
- s->we_are_in = FALSE;\r
- break;\r
- }\r
- }\r
- } else {\r
- c_write_str(ssh, "Further authentication required\r\n");\r
- logevent("Further authentication required");\r
- }\r
-\r
- s->can_pubkey =\r
- in_commasep_string("publickey", methods, methlen);\r
- s->can_passwd =\r
- in_commasep_string("password", methods, methlen);\r
- s->can_keyb_inter = ssh->cfg.try_ki_auth &&\r
- in_commasep_string("keyboard-interactive", methods, methlen);\r
-#ifndef NO_GSSAPI\r
- if (!ssh->gsslibs)\r
- ssh->gsslibs = ssh_gss_setup(&ssh->cfg);\r
- s->can_gssapi = ssh->cfg.try_gssapi_auth &&\r
- in_commasep_string("gssapi-with-mic", methods, methlen) &&\r
- ssh->gsslibs->nlibraries > 0;\r
-#endif\r
- }\r
-\r
- ssh->pkt_actx = SSH2_PKTCTX_NOAUTH;\r
-\r
- if (s->can_pubkey && !s->done_agent && s->nkeys) {\r
-\r
- /*\r
- * Attempt public-key authentication using a key from Pageant.\r
- */\r
-\r
- ssh->pkt_actx = SSH2_PKTCTX_PUBLICKEY;\r
-\r
- logeventf(ssh, "Trying Pageant key #%d", s->keyi);\r
-\r
- /* Unpack key from agent response */\r
- s->pklen = GET_32BIT(s->agentp);\r
- s->agentp += 4;\r
- s->pkblob = (char *)s->agentp;\r
- s->agentp += s->pklen;\r
- s->alglen = GET_32BIT(s->pkblob);\r
- s->alg = s->pkblob + 4;\r
- s->commentlen = GET_32BIT(s->agentp);\r
- s->agentp += 4;\r
- s->commentp = (char *)s->agentp;\r
- s->agentp += s->commentlen;\r
- /* s->agentp now points at next key, if any */\r
-\r
- /* See if server will accept it */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- /* service requested */\r
- ssh2_pkt_addstring(s->pktout, "publickey");\r
- /* method */\r
- ssh2_pkt_addbool(s->pktout, FALSE); /* no signature included */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen);\r
- ssh2_pkt_addstring_start(s->pktout);\r
- ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen);\r
- ssh2_pkt_send(ssh, s->pktout);\r
- s->type = AUTH_TYPE_PUBLICKEY_OFFER_QUIET;\r
-\r
- crWaitUntilV(pktin);\r
- if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {\r
-\r
- /* Offer of key refused. */\r
- s->gotit = TRUE;\r
-\r
- } else {\r
- \r
- void *vret;\r
-\r
- if (flags & FLAG_VERBOSE) {\r
- c_write_str(ssh, "Authenticating with "\r
- "public key \"");\r
- c_write(ssh, s->commentp, s->commentlen);\r
- c_write_str(ssh, "\" from agent\r\n");\r
- }\r
-\r
- /*\r
- * Server is willing to accept the key.\r
- * Construct a SIGN_REQUEST.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- /* service requested */\r
- ssh2_pkt_addstring(s->pktout, "publickey");\r
- /* method */\r
- ssh2_pkt_addbool(s->pktout, TRUE); /* signature included */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen);\r
- ssh2_pkt_addstring_start(s->pktout);\r
- ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen);\r
-\r
- /* Ask agent for signature. */\r
- s->siglen = s->pktout->length - 5 + 4 +\r
- ssh->v2_session_id_len;\r
- if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)\r
- s->siglen -= 4;\r
- s->len = 1; /* message type */\r
- s->len += 4 + s->pklen; /* key blob */\r
- s->len += 4 + s->siglen; /* data to sign */\r
- s->len += 4; /* flags */\r
- s->agentreq = snewn(4 + s->len, char);\r
- PUT_32BIT(s->agentreq, s->len);\r
- s->q = s->agentreq + 4;\r
- *s->q++ = SSH2_AGENTC_SIGN_REQUEST;\r
- PUT_32BIT(s->q, s->pklen);\r
- s->q += 4;\r
- memcpy(s->q, s->pkblob, s->pklen);\r
- s->q += s->pklen;\r
- PUT_32BIT(s->q, s->siglen);\r
- s->q += 4;\r
- /* Now the data to be signed... */\r
- if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) {\r
- PUT_32BIT(s->q, ssh->v2_session_id_len);\r
- s->q += 4;\r
- }\r
- memcpy(s->q, ssh->v2_session_id,\r
- ssh->v2_session_id_len);\r
- s->q += ssh->v2_session_id_len;\r
- memcpy(s->q, s->pktout->data + 5,\r
- s->pktout->length - 5);\r
- s->q += s->pktout->length - 5;\r
- /* And finally the (zero) flags word. */\r
- PUT_32BIT(s->q, 0);\r
- if (!agent_query(s->agentreq, s->len + 4,\r
- &vret, &s->retlen,\r
- ssh_agent_callback, ssh)) {\r
- do {\r
- crReturnV;\r
- if (pktin) {\r
- bombout(("Unexpected data from server"\r
- " while waiting for agent"\r
- " response"));\r
- crStopV;\r
- }\r
- } while (pktin || inlen > 0);\r
- vret = ssh->agent_response;\r
- s->retlen = ssh->agent_response_len;\r
- }\r
- s->ret = vret;\r
- sfree(s->agentreq);\r
- if (s->ret) {\r
- if (s->ret[4] == SSH2_AGENT_SIGN_RESPONSE) {\r
- logevent("Sending Pageant's response");\r
- ssh2_add_sigblob(ssh, s->pktout,\r
- s->pkblob, s->pklen,\r
- s->ret + 9,\r
- GET_32BIT(s->ret + 5));\r
- ssh2_pkt_send(ssh, s->pktout);\r
- s->type = AUTH_TYPE_PUBLICKEY;\r
- } else {\r
- /* FIXME: less drastic response */\r
- bombout(("Pageant failed to answer challenge"));\r
- crStopV;\r
- }\r
- }\r
- }\r
-\r
- /* Do we have any keys left to try? */\r
- if (s->pkblob_in_agent) {\r
- s->done_agent = TRUE;\r
- s->tried_pubkey_config = TRUE;\r
- } else {\r
- s->keyi++;\r
- if (s->keyi >= s->nkeys)\r
- s->done_agent = TRUE;\r
- }\r
-\r
- } else if (s->can_pubkey && s->publickey_blob &&\r
- !s->tried_pubkey_config) {\r
-\r
- struct ssh2_userkey *key; /* not live over crReturn */\r
- char *passphrase; /* not live over crReturn */\r
-\r
- ssh->pkt_actx = SSH2_PKTCTX_PUBLICKEY;\r
-\r
- s->tried_pubkey_config = TRUE;\r
-\r
- /*\r
- * Try the public key supplied in the configuration.\r
- *\r
- * First, offer the public blob to see if the server is\r
- * willing to accept it.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- /* service requested */\r
- ssh2_pkt_addstring(s->pktout, "publickey"); /* method */\r
- ssh2_pkt_addbool(s->pktout, FALSE);\r
- /* no signature included */\r
- ssh2_pkt_addstring(s->pktout, s->publickey_algorithm);\r
- ssh2_pkt_addstring_start(s->pktout);\r
- ssh2_pkt_addstring_data(s->pktout,\r
- (char *)s->publickey_blob,\r
- s->publickey_bloblen);\r
- ssh2_pkt_send(ssh, s->pktout);\r
- logevent("Offered public key");\r
-\r
- crWaitUntilV(pktin);\r
- if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {\r
- /* Key refused. Give up. */\r
- s->gotit = TRUE; /* reconsider message next loop */\r
- s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD;\r
- continue; /* process this new message */\r
- }\r
- logevent("Offer of public key accepted");\r
-\r
- /*\r
- * Actually attempt a serious authentication using\r
- * the key.\r
- */\r
- if (flags & FLAG_VERBOSE) {\r
- c_write_str(ssh, "Authenticating with public key \"");\r
- c_write_str(ssh, s->publickey_comment);\r
- c_write_str(ssh, "\"\r\n");\r
- }\r
- key = NULL;\r
- while (!key) {\r
- const char *error; /* not live over crReturn */\r
- if (s->publickey_encrypted) {\r
- /*\r
- * Get a passphrase from the user.\r
- */\r
- int ret; /* need not be kept over crReturn */\r
- s->cur_prompt = new_prompts(ssh->frontend);\r
- s->cur_prompt->to_server = FALSE;\r
- s->cur_prompt->name = dupstr("SSH key passphrase");\r
- add_prompt(s->cur_prompt,\r
- dupprintf("Passphrase for key \"%.100s\": ",\r
- s->publickey_comment),\r
- FALSE, SSH_MAX_PASSWORD_LEN);\r
- ret = get_userpass_input(s->cur_prompt, NULL, 0);\r
- while (ret < 0) {\r
- ssh->send_ok = 1;\r
- crWaitUntilV(!pktin);\r
- ret = get_userpass_input(s->cur_prompt,\r
- in, inlen);\r
- ssh->send_ok = 0;\r
- }\r
- if (!ret) {\r
- /* Failed to get a passphrase. Terminate. */\r
- free_prompts(s->cur_prompt);\r
- ssh_disconnect(ssh, NULL,\r
- "Unable to authenticate",\r
- SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER,\r
- TRUE);\r
- crStopV;\r
- }\r
- passphrase =\r
- dupstr(s->cur_prompt->prompts[0]->result);\r
- free_prompts(s->cur_prompt);\r
- } else {\r
- passphrase = NULL; /* no passphrase needed */\r
- }\r
-\r
- /*\r
- * Try decrypting the key.\r
- */\r
- key = ssh2_load_userkey(&ssh->cfg.keyfile, passphrase,\r
- &error);\r
- if (passphrase) {\r
- /* burn the evidence */\r
- memset(passphrase, 0, strlen(passphrase));\r
- sfree(passphrase);\r
- }\r
- if (key == SSH2_WRONG_PASSPHRASE || key == NULL) {\r
- if (passphrase &&\r
- (key == SSH2_WRONG_PASSPHRASE)) {\r
- c_write_str(ssh, "Wrong passphrase\r\n");\r
- key = NULL;\r
- /* and loop again */\r
- } else {\r
- c_write_str(ssh, "Unable to load private key (");\r
- c_write_str(ssh, error);\r
- c_write_str(ssh, ")\r\n");\r
- key = NULL;\r
- break; /* try something else */\r
- }\r
- }\r
- }\r
-\r
- if (key) {\r
- unsigned char *pkblob, *sigblob, *sigdata;\r
- int pkblob_len, sigblob_len, sigdata_len;\r
- int p;\r
-\r
- /*\r
- * We have loaded the private key and the server\r
- * has announced that it's willing to accept it.\r
- * Hallelujah. Generate a signature and send it.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- /* service requested */\r
- ssh2_pkt_addstring(s->pktout, "publickey");\r
- /* method */\r
- ssh2_pkt_addbool(s->pktout, TRUE);\r
- /* signature follows */\r
- ssh2_pkt_addstring(s->pktout, key->alg->name);\r
- pkblob = key->alg->public_blob(key->data,\r
- &pkblob_len);\r
- ssh2_pkt_addstring_start(s->pktout);\r
- ssh2_pkt_addstring_data(s->pktout, (char *)pkblob,\r
- pkblob_len);\r
-\r
- /*\r
- * The data to be signed is:\r
- *\r
- * string session-id\r
- *\r
- * followed by everything so far placed in the\r
- * outgoing packet.\r
- */\r
- sigdata_len = s->pktout->length - 5 + 4 +\r
- ssh->v2_session_id_len;\r
- if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)\r
- sigdata_len -= 4;\r
- sigdata = snewn(sigdata_len, unsigned char);\r
- p = 0;\r
- if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) {\r
- PUT_32BIT(sigdata+p, ssh->v2_session_id_len);\r
- p += 4;\r
- }\r
- memcpy(sigdata+p, ssh->v2_session_id,\r
- ssh->v2_session_id_len);\r
- p += ssh->v2_session_id_len;\r
- memcpy(sigdata+p, s->pktout->data + 5,\r
- s->pktout->length - 5);\r
- p += s->pktout->length - 5;\r
- assert(p == sigdata_len);\r
- sigblob = key->alg->sign(key->data, (char *)sigdata,\r
- sigdata_len, &sigblob_len);\r
- ssh2_add_sigblob(ssh, s->pktout, pkblob, pkblob_len,\r
- sigblob, sigblob_len);\r
- sfree(pkblob);\r
- sfree(sigblob);\r
- sfree(sigdata);\r
-\r
- ssh2_pkt_send(ssh, s->pktout);\r
- s->type = AUTH_TYPE_PUBLICKEY;\r
- key->alg->freekey(key->data);\r
- }\r
-\r
-#ifndef NO_GSSAPI\r
- } else if (s->can_gssapi && !s->tried_gssapi) {\r
-\r
- /* GSSAPI Authentication */\r
-\r
- int micoffset, len;\r
- char *data;\r
- Ssh_gss_buf mic;\r
- s->type = AUTH_TYPE_GSSAPI;\r
- s->tried_gssapi = TRUE;\r
- s->gotit = TRUE;\r
- ssh->pkt_actx = SSH2_PKTCTX_GSSAPI;\r
-\r
- /*\r
- * Pick the highest GSS library on the preference\r
- * list.\r
- */\r
- {\r
- int i, j;\r
- s->gsslib = NULL;\r
- for (i = 0; i < ngsslibs; i++) {\r
- int want_id = ssh->cfg.ssh_gsslist[i];\r
- for (j = 0; j < ssh->gsslibs->nlibraries; j++)\r
- if (ssh->gsslibs->libraries[j].id == want_id) {\r
- s->gsslib = &ssh->gsslibs->libraries[j];\r
- goto got_gsslib; /* double break */\r
- }\r
- }\r
- got_gsslib:\r
- /*\r
- * We always expect to have found something in\r
- * the above loop: we only came here if there\r
- * was at least one viable GSS library, and the\r
- * preference list should always mention\r
- * everything and only change the order.\r
- */\r
- assert(s->gsslib);\r
- }\r
-\r
- if (s->gsslib->gsslogmsg)\r
- logevent(s->gsslib->gsslogmsg);\r
-\r
- /* Sending USERAUTH_REQUEST with "gssapi-with-mic" method */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- ssh2_pkt_addstring(s->pktout, "gssapi-with-mic");\r
-\r
- /* add mechanism info */\r
- s->gsslib->indicate_mech(s->gsslib, &s->gss_buf);\r
-\r
- /* number of GSSAPI mechanisms */\r
- ssh2_pkt_adduint32(s->pktout,1);\r
-\r
- /* length of OID + 2 */\r
- ssh2_pkt_adduint32(s->pktout, s->gss_buf.length + 2);\r
- ssh2_pkt_addbyte(s->pktout, SSH2_GSS_OIDTYPE);\r
-\r
- /* length of OID */\r
- ssh2_pkt_addbyte(s->pktout, (unsigned char) s->gss_buf.length);\r
-\r
- ssh_pkt_adddata(s->pktout, s->gss_buf.value,\r
- s->gss_buf.length);\r
- ssh2_pkt_send(ssh, s->pktout);\r
- crWaitUntilV(pktin);\r
- if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_RESPONSE) {\r
- logevent("GSSAPI authentication request refused");\r
- continue;\r
- }\r
-\r
- /* check returned packet ... */\r
-\r
- ssh_pkt_getstring(pktin, &data, &len);\r
- s->gss_rcvtok.value = data;\r
- s->gss_rcvtok.length = len;\r
- if (s->gss_rcvtok.length != s->gss_buf.length + 2 ||\r
- ((char *)s->gss_rcvtok.value)[0] != SSH2_GSS_OIDTYPE ||\r
- ((char *)s->gss_rcvtok.value)[1] != s->gss_buf.length ||\r
- memcmp((char *)s->gss_rcvtok.value + 2,\r
- s->gss_buf.value,s->gss_buf.length) ) {\r
- logevent("GSSAPI authentication - wrong response from server");\r
- continue;\r
- }\r
-\r
- /* now start running */\r
- s->gss_stat = s->gsslib->import_name(s->gsslib,\r
- ssh->fullhostname,\r
- &s->gss_srv_name);\r
- if (s->gss_stat != SSH_GSS_OK) {\r
- if (s->gss_stat == SSH_GSS_BAD_HOST_NAME)\r
- logevent("GSSAPI import name failed - Bad service name");\r
- else\r
- logevent("GSSAPI import name failed");\r
- continue;\r
- }\r
-\r
- /* fetch TGT into GSS engine */\r
- s->gss_stat = s->gsslib->acquire_cred(s->gsslib, &s->gss_ctx);\r
-\r
- if (s->gss_stat != SSH_GSS_OK) {\r
- logevent("GSSAPI authentication failed to get credentials");\r
- s->gsslib->release_name(s->gsslib, &s->gss_srv_name);\r
- continue;\r
- }\r
-\r
- /* initial tokens are empty */\r
- SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);\r
- SSH_GSS_CLEAR_BUF(&s->gss_sndtok);\r
-\r
- /* now enter the loop */\r
- do {\r
- s->gss_stat = s->gsslib->init_sec_context\r
- (s->gsslib,\r
- &s->gss_ctx,\r
- s->gss_srv_name,\r
- ssh->cfg.gssapifwd,\r
- &s->gss_rcvtok,\r
- &s->gss_sndtok);\r
-\r
- if (s->gss_stat!=SSH_GSS_S_COMPLETE &&\r
- s->gss_stat!=SSH_GSS_S_CONTINUE_NEEDED) {\r
- logevent("GSSAPI authentication initialisation failed");\r
-\r
- if (s->gsslib->display_status(s->gsslib, s->gss_ctx,\r
- &s->gss_buf) == SSH_GSS_OK) {\r
- logevent(s->gss_buf.value);\r
- sfree(s->gss_buf.value);\r
- }\r
-\r
- break;\r
- }\r
- logevent("GSSAPI authentication initialised");\r
-\r
- /* Client and server now exchange tokens until GSSAPI\r
- * no longer says CONTINUE_NEEDED */\r
-\r
- if (s->gss_sndtok.length != 0) {\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);\r
- ssh_pkt_addstring_start(s->pktout);\r
- ssh_pkt_addstring_data(s->pktout,s->gss_sndtok.value,s->gss_sndtok.length);\r
- ssh2_pkt_send(ssh, s->pktout);\r
- s->gsslib->free_tok(s->gsslib, &s->gss_sndtok);\r
- }\r
-\r
- if (s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED) {\r
- crWaitUntilV(pktin);\r
- if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_TOKEN) {\r
- logevent("GSSAPI authentication - bad server response");\r
- s->gss_stat = SSH_GSS_FAILURE;\r
- break;\r
- }\r
- ssh_pkt_getstring(pktin, &data, &len);\r
- s->gss_rcvtok.value = data;\r
- s->gss_rcvtok.length = len;\r
- }\r
- } while (s-> gss_stat == SSH_GSS_S_CONTINUE_NEEDED);\r
-\r
- if (s->gss_stat != SSH_GSS_OK) {\r
- s->gsslib->release_name(s->gsslib, &s->gss_srv_name);\r
- s->gsslib->release_cred(s->gsslib, &s->gss_ctx);\r
- continue;\r
- }\r
- logevent("GSSAPI authentication loop finished OK");\r
-\r
- /* Now send the MIC */\r
-\r
- s->pktout = ssh2_pkt_init(0);\r
- micoffset = s->pktout->length;\r
- ssh_pkt_addstring_start(s->pktout);\r
- ssh_pkt_addstring_data(s->pktout, (char *)ssh->v2_session_id, ssh->v2_session_id_len);\r
- ssh_pkt_addbyte(s->pktout, SSH2_MSG_USERAUTH_REQUEST);\r
- ssh_pkt_addstring(s->pktout, s->username);\r
- ssh_pkt_addstring(s->pktout, "ssh-connection");\r
- ssh_pkt_addstring(s->pktout, "gssapi-with-mic");\r
-\r
- s->gss_buf.value = (char *)s->pktout->data + micoffset;\r
- s->gss_buf.length = s->pktout->length - micoffset;\r
-\r
- s->gsslib->get_mic(s->gsslib, s->gss_ctx, &s->gss_buf, &mic);\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_GSSAPI_MIC);\r
- ssh_pkt_addstring_start(s->pktout);\r
- ssh_pkt_addstring_data(s->pktout, mic.value, mic.length);\r
- ssh2_pkt_send(ssh, s->pktout);\r
- s->gsslib->free_mic(s->gsslib, &mic);\r
-\r
- s->gotit = FALSE;\r
-\r
- s->gsslib->release_name(s->gsslib, &s->gss_srv_name);\r
- s->gsslib->release_cred(s->gsslib, &s->gss_ctx);\r
- continue;\r
-#endif\r
- } else if (s->can_keyb_inter && !s->kbd_inter_refused) {\r
-\r
- /*\r
- * Keyboard-interactive authentication.\r
- */\r
-\r
- s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE;\r
-\r
- ssh->pkt_actx = SSH2_PKTCTX_KBDINTER;\r
-\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- /* service requested */\r
- ssh2_pkt_addstring(s->pktout, "keyboard-interactive");\r
- /* method */\r
- ssh2_pkt_addstring(s->pktout, ""); /* lang */\r
- ssh2_pkt_addstring(s->pktout, ""); /* submethods */\r
- ssh2_pkt_send(ssh, s->pktout);\r
-\r
- crWaitUntilV(pktin);\r
- if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) {\r
- /* Server is not willing to do keyboard-interactive\r
- * at all (or, bizarrely but legally, accepts the\r
- * user without actually issuing any prompts).\r
- * Give up on it entirely. */\r
- s->gotit = TRUE;\r
- if (pktin->type == SSH2_MSG_USERAUTH_FAILURE)\r
- logevent("Keyboard-interactive authentication refused");\r
- s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET;\r
- s->kbd_inter_refused = TRUE; /* don't try it again */\r
- continue;\r
- }\r
-\r
- /*\r
- * Loop while the server continues to send INFO_REQUESTs.\r
- */\r
- while (pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) {\r
-\r
- char *name, *inst, *lang;\r
- int name_len, inst_len, lang_len;\r
- int i;\r
-\r
- /*\r
- * We've got a fresh USERAUTH_INFO_REQUEST.\r
- * Get the preamble and start building a prompt.\r
- */\r
- ssh_pkt_getstring(pktin, &name, &name_len);\r
- ssh_pkt_getstring(pktin, &inst, &inst_len);\r
- ssh_pkt_getstring(pktin, &lang, &lang_len);\r
- s->cur_prompt = new_prompts(ssh->frontend);\r
- s->cur_prompt->to_server = TRUE;\r
-\r
- /*\r
- * Get any prompt(s) from the packet.\r
- */\r
- s->num_prompts = ssh_pkt_getuint32(pktin);\r
- for (i = 0; i < s->num_prompts; i++) {\r
- char *prompt;\r
- int prompt_len;\r
- int echo;\r
- static char noprompt[] =\r
- "<server failed to send prompt>: ";\r
-\r
- ssh_pkt_getstring(pktin, &prompt, &prompt_len);\r
- echo = ssh2_pkt_getbool(pktin);\r
- if (!prompt_len) {\r
- prompt = noprompt;\r
- prompt_len = lenof(noprompt)-1;\r
- }\r
- add_prompt(s->cur_prompt,\r
- dupprintf("%.*s", prompt_len, prompt),\r
- echo, SSH_MAX_PASSWORD_LEN);\r
- }\r
-\r
- if (name_len) {\r
- /* FIXME: better prefix to distinguish from\r
- * local prompts? */\r
- s->cur_prompt->name =\r
- dupprintf("SSH server: %.*s", name_len, name);\r
- s->cur_prompt->name_reqd = TRUE;\r
- } else {\r
- s->cur_prompt->name =\r
- dupstr("SSH server authentication");\r
- s->cur_prompt->name_reqd = FALSE;\r
- }\r
- /* We add a prefix to try to make it clear that a prompt\r
- * has come from the server.\r
- * FIXME: ugly to print "Using..." in prompt _every_\r
- * time round. Can this be done more subtly? */\r
- /* Special case: for reasons best known to themselves,\r
- * some servers send k-i requests with no prompts and\r
- * nothing to display. Keep quiet in this case. */\r
- if (s->num_prompts || name_len || inst_len) {\r
- s->cur_prompt->instruction =\r
- dupprintf("Using keyboard-interactive authentication.%s%.*s",\r
- inst_len ? "\n" : "", inst_len, inst);\r
- s->cur_prompt->instr_reqd = TRUE;\r
- } else {\r
- s->cur_prompt->instr_reqd = FALSE;\r
- }\r
-\r
- /*\r
- * Display any instructions, and get the user's\r
- * response(s).\r
- */\r
- {\r
- int ret; /* not live over crReturn */\r
- ret = get_userpass_input(s->cur_prompt, NULL, 0);\r
- while (ret < 0) {\r
- ssh->send_ok = 1;\r
- crWaitUntilV(!pktin);\r
- ret = get_userpass_input(s->cur_prompt, in, inlen);\r
- ssh->send_ok = 0;\r
- }\r
- if (!ret) {\r
- /*\r
- * Failed to get responses. Terminate.\r
- */\r
- free_prompts(s->cur_prompt);\r
- ssh_disconnect(ssh, NULL, "Unable to authenticate",\r
- SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER,\r
- TRUE);\r
- crStopV;\r
- }\r
- }\r
-\r
- /*\r
- * Send the response(s) to the server.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_INFO_RESPONSE);\r
- ssh2_pkt_adduint32(s->pktout, s->num_prompts);\r
- for (i=0; i < s->num_prompts; i++) {\r
- dont_log_password(ssh, s->pktout, PKTLOG_BLANK);\r
- ssh2_pkt_addstring(s->pktout,\r
- s->cur_prompt->prompts[i]->result);\r
- end_log_omission(ssh, s->pktout);\r
- }\r
- ssh2_pkt_send_with_padding(ssh, s->pktout, 256);\r
-\r
- /*\r
- * Get the next packet in case it's another\r
- * INFO_REQUEST.\r
- */\r
- crWaitUntilV(pktin);\r
-\r
- }\r
-\r
- /*\r
- * We should have SUCCESS or FAILURE now.\r
- */\r
- s->gotit = TRUE;\r
-\r
- } else if (s->can_passwd) {\r
-\r
- /*\r
- * Plain old password authentication.\r
- */\r
- int ret; /* not live over crReturn */\r
- int changereq_first_time; /* not live over crReturn */\r
-\r
- ssh->pkt_actx = SSH2_PKTCTX_PASSWORD;\r
-\r
- s->cur_prompt = new_prompts(ssh->frontend);\r
- s->cur_prompt->to_server = TRUE;\r
- s->cur_prompt->name = dupstr("SSH password");\r
- add_prompt(s->cur_prompt, dupprintf("%.90s@%.90s's password: ",\r
- s->username,\r
- ssh->savedhost),\r
- FALSE, SSH_MAX_PASSWORD_LEN);\r
-\r
- ret = get_userpass_input(s->cur_prompt, NULL, 0);\r
- while (ret < 0) {\r
- ssh->send_ok = 1;\r
- crWaitUntilV(!pktin);\r
- ret = get_userpass_input(s->cur_prompt, in, inlen);\r
- ssh->send_ok = 0;\r
- }\r
- if (!ret) {\r
- /*\r
- * Failed to get responses. Terminate.\r
- */\r
- free_prompts(s->cur_prompt);\r
- ssh_disconnect(ssh, NULL, "Unable to authenticate",\r
- SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER,\r
- TRUE);\r
- crStopV;\r
- }\r
- /*\r
- * Squirrel away the password. (We may need it later if\r
- * asked to change it.)\r
- */\r
- s->password = dupstr(s->cur_prompt->prompts[0]->result);\r
- free_prompts(s->cur_prompt);\r
-\r
- /*\r
- * Send the password packet.\r
- *\r
- * We pad out the password packet to 256 bytes to make\r
- * it harder for an attacker to find the length of the\r
- * user's password.\r
- *\r
- * Anyone using a password longer than 256 bytes\r
- * probably doesn't have much to worry about from\r
- * people who find out how long their password is!\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- /* service requested */\r
- ssh2_pkt_addstring(s->pktout, "password");\r
- ssh2_pkt_addbool(s->pktout, FALSE);\r
- dont_log_password(ssh, s->pktout, PKTLOG_BLANK);\r
- ssh2_pkt_addstring(s->pktout, s->password);\r
- end_log_omission(ssh, s->pktout);\r
- ssh2_pkt_send_with_padding(ssh, s->pktout, 256);\r
- logevent("Sent password");\r
- s->type = AUTH_TYPE_PASSWORD;\r
-\r
- /*\r
- * Wait for next packet, in case it's a password change\r
- * request.\r
- */\r
- crWaitUntilV(pktin);\r
- changereq_first_time = TRUE;\r
-\r
- while (pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) {\r
-\r
- /* \r
- * We're being asked for a new password\r
- * (perhaps not for the first time).\r
- * Loop until the server accepts it.\r
- */\r
-\r
- int got_new = FALSE; /* not live over crReturn */\r
- char *prompt; /* not live over crReturn */\r
- int prompt_len; /* not live over crReturn */\r
- \r
- {\r
- char *msg;\r
- if (changereq_first_time)\r
- msg = "Server requested password change";\r
- else\r
- msg = "Server rejected new password";\r
- logevent(msg);\r
- c_write_str(ssh, msg);\r
- c_write_str(ssh, "\r\n");\r
- }\r
-\r
- ssh_pkt_getstring(pktin, &prompt, &prompt_len);\r
-\r
- s->cur_prompt = new_prompts(ssh->frontend);\r
- s->cur_prompt->to_server = TRUE;\r
- s->cur_prompt->name = dupstr("New SSH password");\r
- s->cur_prompt->instruction =\r
- dupprintf("%.*s", prompt_len, prompt);\r
- s->cur_prompt->instr_reqd = TRUE;\r
- /*\r
- * There's no explicit requirement in the protocol\r
- * for the "old" passwords in the original and\r
- * password-change messages to be the same, and\r
- * apparently some Cisco kit supports password change\r
- * by the user entering a blank password originally\r
- * and the real password subsequently, so,\r
- * reluctantly, we prompt for the old password again.\r
- *\r
- * (On the other hand, some servers don't even bother\r
- * to check this field.)\r
- */\r
- add_prompt(s->cur_prompt,\r
- dupstr("Current password (blank for previously entered password): "),\r
- FALSE, SSH_MAX_PASSWORD_LEN);\r
- add_prompt(s->cur_prompt, dupstr("Enter new password: "),\r
- FALSE, SSH_MAX_PASSWORD_LEN);\r
- add_prompt(s->cur_prompt, dupstr("Confirm new password: "),\r
- FALSE, SSH_MAX_PASSWORD_LEN);\r
-\r
- /*\r
- * Loop until the user manages to enter the same\r
- * password twice.\r
- */\r
- while (!got_new) {\r
-\r
- ret = get_userpass_input(s->cur_prompt, NULL, 0);\r
- while (ret < 0) {\r
- ssh->send_ok = 1;\r
- crWaitUntilV(!pktin);\r
- ret = get_userpass_input(s->cur_prompt, in, inlen);\r
- ssh->send_ok = 0;\r
- }\r
- if (!ret) {\r
- /*\r
- * Failed to get responses. Terminate.\r
- */\r
- /* burn the evidence */\r
- free_prompts(s->cur_prompt);\r
- memset(s->password, 0, strlen(s->password));\r
- sfree(s->password);\r
- ssh_disconnect(ssh, NULL, "Unable to authenticate",\r
- SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER,\r
- TRUE);\r
- crStopV;\r
- }\r
-\r
- /*\r
- * If the user specified a new original password\r
- * (IYSWIM), overwrite any previously specified\r
- * one.\r
- * (A side effect is that the user doesn't have to\r
- * re-enter it if they louse up the new password.)\r
- */\r
- if (s->cur_prompt->prompts[0]->result[0]) {\r
- memset(s->password, 0, strlen(s->password));\r
- /* burn the evidence */\r
- sfree(s->password);\r
- s->password =\r
- dupstr(s->cur_prompt->prompts[0]->result);\r
- }\r
-\r
- /*\r
- * Check the two new passwords match.\r
- */\r
- got_new = (strcmp(s->cur_prompt->prompts[1]->result,\r
- s->cur_prompt->prompts[2]->result)\r
- == 0);\r
- if (!got_new)\r
- /* They don't. Silly user. */\r
- c_write_str(ssh, "Passwords do not match\r\n");\r
-\r
- }\r
-\r
- /*\r
- * Send the new password (along with the old one).\r
- * (see above for padding rationale)\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);\r
- ssh2_pkt_addstring(s->pktout, s->username);\r
- ssh2_pkt_addstring(s->pktout, "ssh-connection");\r
- /* service requested */\r
- ssh2_pkt_addstring(s->pktout, "password");\r
- ssh2_pkt_addbool(s->pktout, TRUE);\r
- dont_log_password(ssh, s->pktout, PKTLOG_BLANK);\r
- ssh2_pkt_addstring(s->pktout, s->password);\r
- ssh2_pkt_addstring(s->pktout,\r
- s->cur_prompt->prompts[1]->result);\r
- free_prompts(s->cur_prompt);\r
- end_log_omission(ssh, s->pktout);\r
- ssh2_pkt_send_with_padding(ssh, s->pktout, 256);\r
- logevent("Sent new password");\r
- \r
- /*\r
- * Now see what the server has to say about it.\r
- * (If it's CHANGEREQ again, it's not happy with the\r
- * new password.)\r
- */\r
- crWaitUntilV(pktin);\r
- changereq_first_time = FALSE;\r
-\r
- }\r
-\r
- /*\r
- * We need to reexamine the current pktin at the top\r
- * of the loop. Either:\r
- * - we weren't asked to change password at all, in\r
- * which case it's a SUCCESS or FAILURE with the\r
- * usual meaning\r
- * - we sent a new password, and the server was\r
- * either OK with it (SUCCESS or FAILURE w/partial\r
- * success) or unhappy with the _old_ password\r
- * (FAILURE w/o partial success)\r
- * In any of these cases, we go back to the top of\r
- * the loop and start again.\r
- */\r
- s->gotit = TRUE;\r
-\r
- /*\r
- * We don't need the old password any more, in any\r
- * case. Burn the evidence.\r
- */\r
- memset(s->password, 0, strlen(s->password));\r
- sfree(s->password);\r
-\r
- } else {\r
- char *str = dupprintf("No supported authentication methods available"\r
- " (server sent: %.*s)",\r
- methlen, methods);\r
-\r
- ssh_disconnect(ssh, str,\r
- "No supported authentication methods available",\r
- SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE,\r
- FALSE);\r
- sfree(str);\r
-\r
- crStopV;\r
-\r
- }\r
-\r
- }\r
- }\r
- ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = NULL;\r
-\r
- /* Clear up various bits and pieces from authentication. */\r
- if (s->publickey_blob) {\r
- sfree(s->publickey_blob);\r
- sfree(s->publickey_comment);\r
- }\r
- if (s->agent_response)\r
- sfree(s->agent_response);\r
-\r
- if (s->userauth_success) {\r
- /*\r
- * We've just received USERAUTH_SUCCESS, and we haven't sent any\r
- * packets since. Signal the transport layer to consider enacting\r
- * delayed compression.\r
- *\r
- * (Relying on we_are_in is not sufficient, as\r
- * draft-miller-secsh-compression-delayed is quite clear that it\r
- * triggers on USERAUTH_SUCCESS specifically, and we_are_in can\r
- * become set for other reasons.)\r
- */\r
- do_ssh2_transport(ssh, "enabling delayed compression", -2, NULL);\r
- }\r
-\r
- /*\r
- * Now the connection protocol has started, one way or another.\r
- */\r
-\r
- ssh->channels = newtree234(ssh_channelcmp);\r
-\r
- /*\r
- * Set up handlers for some connection protocol messages, so we\r
- * don't have to handle them repeatedly in this coroutine.\r
- */\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] =\r
- ssh2_msg_channel_window_adjust;\r
- ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] =\r
- ssh2_msg_global_request;\r
-\r
- /*\r
- * Create the main session channel.\r
- */\r
- if (ssh->cfg.ssh_no_shell) {\r
- ssh->mainchan = NULL;\r
- } else if (*ssh->cfg.ssh_nc_host) {\r
- /*\r
- * Just start a direct-tcpip channel and use it as the main\r
- * channel.\r
- */\r
- ssh->mainchan = snew(struct ssh_channel);\r
- ssh->mainchan->ssh = ssh;\r
- ssh2_channel_init(ssh->mainchan);\r
- logeventf(ssh,\r
- "Opening direct-tcpip channel to %s:%d in place of session",\r
- ssh->cfg.ssh_nc_host, ssh->cfg.ssh_nc_port);\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);\r
- ssh2_pkt_addstring(s->pktout, "direct-tcpip");\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->localid);\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->v.v2.locwindow);/* our window size */\r
- ssh2_pkt_adduint32(s->pktout, OUR_V2_MAXPKT); /* our max pkt size */\r
- ssh2_pkt_addstring(s->pktout, ssh->cfg.ssh_nc_host);\r
- ssh2_pkt_adduint32(s->pktout, ssh->cfg.ssh_nc_port);\r
- /*\r
- * There's nothing meaningful to put in the originator\r
- * fields, but some servers insist on syntactically correct\r
- * information.\r
- */\r
- ssh2_pkt_addstring(s->pktout, "0.0.0.0");\r
- ssh2_pkt_adduint32(s->pktout, 0);\r
- ssh2_pkt_send(ssh, s->pktout);\r
-\r
- crWaitUntilV(pktin);\r
- if (pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) {\r
- bombout(("Server refused to open a direct-tcpip channel"));\r
- crStopV;\r
- /* FIXME: error data comes back in FAILURE packet */\r
- }\r
- if (ssh_pkt_getuint32(pktin) != ssh->mainchan->localid) {\r
- bombout(("Server's channel confirmation cited wrong channel"));\r
- crStopV;\r
- }\r
- ssh->mainchan->remoteid = ssh_pkt_getuint32(pktin);\r
- ssh->mainchan->halfopen = FALSE;\r
- ssh->mainchan->type = CHAN_MAINSESSION;\r
- ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(pktin);\r
- ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin);\r
- add234(ssh->channels, ssh->mainchan);\r
- update_specials_menu(ssh->frontend);\r
- logevent("Opened direct-tcpip channel");\r
- ssh->ncmode = TRUE;\r
- } else {\r
- ssh->mainchan = snew(struct ssh_channel);\r
- ssh->mainchan->ssh = ssh;\r
- ssh2_channel_init(ssh->mainchan);\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);\r
- ssh2_pkt_addstring(s->pktout, "session");\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->localid);\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->v.v2.locwindow);/* our window size */\r
- ssh2_pkt_adduint32(s->pktout, OUR_V2_MAXPKT); /* our max pkt size */\r
- ssh2_pkt_send(ssh, s->pktout);\r
- crWaitUntilV(pktin);\r
- if (pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) {\r
- bombout(("Server refused to open a session"));\r
- crStopV;\r
- /* FIXME: error data comes back in FAILURE packet */\r
- }\r
- if (ssh_pkt_getuint32(pktin) != ssh->mainchan->localid) {\r
- bombout(("Server's channel confirmation cited wrong channel"));\r
- crStopV;\r
- }\r
- ssh->mainchan->remoteid = ssh_pkt_getuint32(pktin);\r
- ssh->mainchan->halfopen = FALSE;\r
- ssh->mainchan->type = CHAN_MAINSESSION;\r
- ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(pktin);\r
- ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin);\r
- add234(ssh->channels, ssh->mainchan);\r
- update_specials_menu(ssh->frontend);\r
- logevent("Opened channel for session");\r
- ssh->ncmode = FALSE;\r
- }\r
-\r
- /*\r
- * Now we have a channel, make dispatch table entries for\r
- * general channel-based messages.\r
- */\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] =\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] =\r
- ssh2_msg_channel_data;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_channel_eof;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_channel_close;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] =\r
- ssh2_msg_channel_open_confirmation;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] =\r
- ssh2_msg_channel_open_failure;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] =\r
- ssh2_msg_channel_request;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] =\r
- ssh2_msg_channel_open;\r
-\r
- if (ssh->mainchan && ssh->cfg.ssh_simple) {\r
- /*\r
- * This message indicates to the server that we promise\r
- * not to try to run any other channel in parallel with\r
- * this one, so it's safe for it to advertise a very large\r
- * window and leave the flow control to TCP.\r
- */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid);\r
- ssh2_pkt_addstring(s->pktout, "simple@putty.projects.tartarus.org");\r
- ssh2_pkt_addbool(s->pktout, 0); /* no reply */\r
- ssh2_pkt_send(ssh, s->pktout);\r
- }\r
-\r
- /*\r
- * Potentially enable X11 forwarding.\r
- */\r
- if (ssh->mainchan && !ssh->ncmode && ssh->cfg.x11_forward &&\r
- (ssh->x11disp = x11_setup_display(ssh->cfg.x11_display,\r
- ssh->cfg.x11_auth, &ssh->cfg))) {\r
- logevent("Requesting X11 forwarding");\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid);\r
- ssh2_pkt_addstring(s->pktout, "x11-req");\r
- ssh2_pkt_addbool(s->pktout, 1); /* want reply */\r
- ssh2_pkt_addbool(s->pktout, 0); /* many connections */\r
- ssh2_pkt_addstring(s->pktout, ssh->x11disp->remoteauthprotoname);\r
- /*\r
- * Note that while we blank the X authentication data here, we don't\r
- * take any special action to blank the start of an X11 channel,\r
- * so using MIT-MAGIC-COOKIE-1 and actually opening an X connection\r
- * without having session blanking enabled is likely to leak your\r
- * cookie into the log.\r
- */\r
- dont_log_password(ssh, s->pktout, PKTLOG_BLANK);\r
- ssh2_pkt_addstring(s->pktout, ssh->x11disp->remoteauthdatastring);\r
- end_log_omission(ssh, s->pktout);\r
- ssh2_pkt_adduint32(s->pktout, ssh->x11disp->screennum);\r
- ssh2_pkt_send(ssh, s->pktout);\r
-\r
- crWaitUntilV(pktin);\r
-\r
- if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) {\r
- if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) {\r
- bombout(("Unexpected response to X11 forwarding request:"\r
- " packet type %d", pktin->type));\r
- crStopV;\r
- }\r
- logevent("X11 forwarding refused");\r
- } else {\r
- logevent("X11 forwarding enabled");\r
- ssh->X11_fwd_enabled = TRUE;\r
- }\r
- }\r
-\r
- /*\r
- * Enable port forwardings.\r
- */\r
- ssh_setup_portfwd(ssh, &ssh->cfg);\r
-\r
- /*\r
- * Potentially enable agent forwarding.\r
- */\r
- if (ssh->mainchan && !ssh->ncmode && ssh->cfg.agentfwd && agent_exists()) {\r
- logevent("Requesting OpenSSH-style agent forwarding");\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid);\r
- ssh2_pkt_addstring(s->pktout, "auth-agent-req@openssh.com");\r
- ssh2_pkt_addbool(s->pktout, 1); /* want reply */\r
- ssh2_pkt_send(ssh, s->pktout);\r
-\r
- crWaitUntilV(pktin);\r
-\r
- if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) {\r
- if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) {\r
- bombout(("Unexpected response to agent forwarding request:"\r
- " packet type %d", pktin->type));\r
- crStopV;\r
- }\r
- logevent("Agent forwarding refused");\r
- } else {\r
- logevent("Agent forwarding enabled");\r
- ssh->agentfwd_enabled = TRUE;\r
- }\r
- }\r
-\r
- /*\r
- * Now allocate a pty for the session.\r
- */\r
- if (ssh->mainchan && !ssh->ncmode && !ssh->cfg.nopty) {\r
- /* Unpick the terminal-speed string. */\r
- /* XXX perhaps we should allow no speeds to be sent. */\r
- ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */\r
- sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed);\r
- /* Build the pty request. */\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); /* recipient channel */\r
- ssh2_pkt_addstring(s->pktout, "pty-req");\r
- ssh2_pkt_addbool(s->pktout, 1); /* want reply */\r
- ssh2_pkt_addstring(s->pktout, ssh->cfg.termtype);\r
- ssh2_pkt_adduint32(s->pktout, ssh->term_width);\r
- ssh2_pkt_adduint32(s->pktout, ssh->term_height);\r
- ssh2_pkt_adduint32(s->pktout, 0); /* pixel width */\r
- ssh2_pkt_adduint32(s->pktout, 0); /* pixel height */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- parse_ttymodes(ssh, ssh->cfg.ttymodes,\r
- ssh2_send_ttymode, (void *)s->pktout);\r
- ssh2_pkt_addbyte(s->pktout, SSH2_TTY_OP_ISPEED);\r
- ssh2_pkt_adduint32(s->pktout, ssh->ispeed);\r
- ssh2_pkt_addbyte(s->pktout, SSH2_TTY_OP_OSPEED);\r
- ssh2_pkt_adduint32(s->pktout, ssh->ospeed);\r
- ssh2_pkt_addstring_data(s->pktout, "\0", 1); /* TTY_OP_END */\r
- ssh2_pkt_send(ssh, s->pktout);\r
- ssh->state = SSH_STATE_INTERMED;\r
-\r
- crWaitUntilV(pktin);\r
-\r
- if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) {\r
- if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) {\r
- bombout(("Unexpected response to pty request:"\r
- " packet type %d", pktin->type));\r
- crStopV;\r
- }\r
- c_write_str(ssh, "Server refused to allocate pty\r\n");\r
- ssh->editing = ssh->echoing = 1;\r
- } else {\r
- logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)",\r
- ssh->ospeed, ssh->ispeed);\r
- }\r
- } else {\r
- ssh->editing = ssh->echoing = 1;\r
- }\r
-\r
- /*\r
- * Send environment variables.\r
- * \r
- * Simplest thing here is to send all the requests at once, and\r
- * then wait for a whole bunch of successes or failures.\r
- */\r
- if (ssh->mainchan && !ssh->ncmode && *ssh->cfg.environmt) {\r
- char *e = ssh->cfg.environmt;\r
- char *var, *varend, *val;\r
-\r
- s->num_env = 0;\r
-\r
- while (*e) {\r
- var = e;\r
- while (*e && *e != '\t') e++;\r
- varend = e;\r
- if (*e == '\t') e++;\r
- val = e;\r
- while (*e) e++;\r
- e++;\r
-\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid);\r
- ssh2_pkt_addstring(s->pktout, "env");\r
- ssh2_pkt_addbool(s->pktout, 1); /* want reply */\r
- ssh2_pkt_addstring_start(s->pktout);\r
- ssh2_pkt_addstring_data(s->pktout, var, varend-var);\r
- ssh2_pkt_addstring(s->pktout, val);\r
- ssh2_pkt_send(ssh, s->pktout);\r
-\r
- s->num_env++;\r
- }\r
-\r
- logeventf(ssh, "Sent %d environment variables", s->num_env);\r
-\r
- s->env_ok = 0;\r
- s->env_left = s->num_env;\r
-\r
- while (s->env_left > 0) {\r
- crWaitUntilV(pktin);\r
-\r
- if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) {\r
- if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) {\r
- bombout(("Unexpected response to environment request:"\r
- " packet type %d", pktin->type));\r
- crStopV;\r
- }\r
- } else {\r
- s->env_ok++;\r
- }\r
-\r
- s->env_left--;\r
- }\r
-\r
- if (s->env_ok == s->num_env) {\r
- logevent("All environment variables successfully set");\r
- } else if (s->env_ok == 0) {\r
- logevent("All environment variables refused");\r
- c_write_str(ssh, "Server refused to set environment variables\r\n");\r
- } else {\r
- logeventf(ssh, "%d environment variables refused",\r
- s->num_env - s->env_ok);\r
- c_write_str(ssh, "Server refused to set all environment variables\r\n");\r
- }\r
- }\r
-\r
- /*\r
- * Start a shell or a remote command. We may have to attempt\r
- * this twice if the config data has provided a second choice\r
- * of command.\r
- */\r
- if (ssh->mainchan && !ssh->ncmode) while (1) {\r
- int subsys;\r
- char *cmd;\r
-\r
- if (ssh->fallback_cmd) {\r
- subsys = ssh->cfg.ssh_subsys2;\r
- cmd = ssh->cfg.remote_cmd_ptr2;\r
- } else {\r
- subsys = ssh->cfg.ssh_subsys;\r
- cmd = ssh->cfg.remote_cmd_ptr;\r
- if (!cmd) cmd = ssh->cfg.remote_cmd;\r
- }\r
-\r
- s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); /* recipient channel */\r
- if (subsys) {\r
- ssh2_pkt_addstring(s->pktout, "subsystem");\r
- ssh2_pkt_addbool(s->pktout, 1); /* want reply */\r
- ssh2_pkt_addstring(s->pktout, cmd);\r
- } else if (*cmd) {\r
- ssh2_pkt_addstring(s->pktout, "exec");\r
- ssh2_pkt_addbool(s->pktout, 1); /* want reply */\r
- ssh2_pkt_addstring(s->pktout, cmd);\r
- } else {\r
- ssh2_pkt_addstring(s->pktout, "shell");\r
- ssh2_pkt_addbool(s->pktout, 1); /* want reply */\r
- }\r
- ssh2_pkt_send(ssh, s->pktout);\r
-\r
- crWaitUntilV(pktin);\r
-\r
- if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) {\r
- if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) {\r
- bombout(("Unexpected response to shell/command request:"\r
- " packet type %d", pktin->type));\r
- crStopV;\r
- }\r
- /*\r
- * We failed to start the command. If this is the\r
- * fallback command, we really are finished; if it's\r
- * not, and if the fallback command exists, try falling\r
- * back to it before complaining.\r
- */\r
- if (!ssh->fallback_cmd && ssh->cfg.remote_cmd_ptr2 != NULL) {\r
- logevent("Primary command failed; attempting fallback");\r
- ssh->fallback_cmd = TRUE;\r
- continue;\r
- }\r
- bombout(("Server refused to start a shell/command"));\r
- crStopV;\r
- } else {\r
- logevent("Started a shell/command");\r
- }\r
- break;\r
- }\r
-\r
- ssh->state = SSH_STATE_SESSION;\r
- if (ssh->size_needed)\r
- ssh_size(ssh, ssh->term_width, ssh->term_height);\r
- if (ssh->eof_needed)\r
- ssh_special(ssh, TS_EOF);\r
-\r
- /*\r
- * All the initial channel requests are done, so install the default\r
- * failure handler.\r
- */\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = ssh2_msg_channel_success;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = ssh2_msg_channel_failure;\r
-\r
- /*\r
- * Transfer data!\r
- */\r
- if (ssh->ldisc)\r
- ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */\r
- if (ssh->mainchan)\r
- ssh->send_ok = 1;\r
- while (1) {\r
- crReturnV;\r
- s->try_send = FALSE;\r
- if (pktin) {\r
-\r
- /*\r
- * _All_ the connection-layer packets we expect to\r
- * receive are now handled by the dispatch table.\r
- * Anything that reaches here must be bogus.\r
- */\r
-\r
- bombout(("Strange packet received: type %d", pktin->type));\r
- crStopV;\r
- } else if (ssh->mainchan) {\r
- /*\r
- * We have spare data. Add it to the channel buffer.\r
- */\r
- ssh2_add_channel_data(ssh->mainchan, (char *)in, inlen);\r
- s->try_send = TRUE;\r
- }\r
- if (s->try_send) {\r
- int i;\r
- struct ssh_channel *c;\r
- /*\r
- * Try to send data on all channels if we can.\r
- */\r
- for (i = 0; NULL != (c = index234(ssh->channels, i)); i++)\r
- ssh2_try_send_and_unthrottle(ssh, c);\r
- }\r
- }\r
-\r
- crFinishV;\r
-}\r
-\r
-/*\r
- * Handlers for SSH-2 messages that might arrive at any moment.\r
- */\r
-static void ssh2_msg_disconnect(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* log reason code in disconnect message */\r
- char *buf, *msg;\r
- int reason, msglen;\r
-\r
- reason = ssh_pkt_getuint32(pktin);\r
- ssh_pkt_getstring(pktin, &msg, &msglen);\r
-\r
- if (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) {\r
- buf = dupprintf("Received disconnect message (%s)",\r
- ssh2_disconnect_reasons[reason]);\r
- } else {\r
- buf = dupprintf("Received disconnect message (unknown"\r
- " type %d)", reason);\r
- }\r
- logevent(buf);\r
- sfree(buf);\r
- buf = dupprintf("Disconnection message text: %.*s",\r
- msglen, msg);\r
- logevent(buf);\r
- bombout(("Server sent disconnect message\ntype %d (%s):\n\"%.*s\"",\r
- reason,\r
- (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) ?\r
- ssh2_disconnect_reasons[reason] : "unknown",\r
- msglen, msg));\r
- sfree(buf);\r
-}\r
-\r
-static void ssh2_msg_debug(Ssh ssh, struct Packet *pktin)\r
-{\r
- /* log the debug message */\r
- char *msg;\r
- int msglen;\r
-\r
- /* XXX maybe we should actually take notice of the return value */\r
- ssh2_pkt_getbool(pktin);\r
- ssh_pkt_getstring(pktin, &msg, &msglen);\r
-\r
- logeventf(ssh, "Remote debug message: %.*s", msglen, msg);\r
-}\r
-\r
-static void ssh2_msg_something_unimplemented(Ssh ssh, struct Packet *pktin)\r
-{\r
- struct Packet *pktout;\r
- pktout = ssh2_pkt_init(SSH2_MSG_UNIMPLEMENTED);\r
- ssh2_pkt_adduint32(pktout, pktin->sequence);\r
- /*\r
- * UNIMPLEMENTED messages MUST appear in the same order as the\r
- * messages they respond to. Hence, never queue them.\r
- */\r
- ssh2_pkt_send_noqueue(ssh, pktout);\r
-}\r
-\r
-/*\r
- * Handle the top-level SSH-2 protocol.\r
- */\r
-static void ssh2_protocol_setup(Ssh ssh)\r
-{\r
- int i;\r
-\r
- /*\r
- * Most messages cause SSH2_MSG_UNIMPLEMENTED.\r
- */\r
- for (i = 0; i < 256; i++)\r
- ssh->packet_dispatch[i] = ssh2_msg_something_unimplemented;\r
-\r
- /*\r
- * Any message we actually understand, we set to NULL so that\r
- * the coroutines will get it.\r
- */\r
- ssh->packet_dispatch[SSH2_MSG_UNIMPLEMENTED] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_SERVICE_REQUEST] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_SERVICE_ACCEPT] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_KEXINIT] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_NEWKEYS] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_KEXDH_INIT] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_KEXDH_REPLY] = NULL;\r
- /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REQUEST] = NULL; duplicate case value */\r
- /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_GROUP] = NULL; duplicate case value */\r
- ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_INIT] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REPLY] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_USERAUTH_REQUEST] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_USERAUTH_FAILURE] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_USERAUTH_SUCCESS] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_USERAUTH_PK_OK] = NULL;\r
- /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ] = NULL; duplicate case value */\r
- /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_REQUEST] = NULL; duplicate case value */\r
- ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_RESPONSE] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_REQUEST_SUCCESS] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_REQUEST_FAILURE] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = NULL;\r
- ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = NULL;\r
-\r
- /*\r
- * These special message types we install handlers for.\r
- */\r
- ssh->packet_dispatch[SSH2_MSG_DISCONNECT] = ssh2_msg_disconnect;\r
- ssh->packet_dispatch[SSH2_MSG_IGNORE] = ssh_msg_ignore; /* shared with SSH-1 */\r
- ssh->packet_dispatch[SSH2_MSG_DEBUG] = ssh2_msg_debug;\r
-}\r
-\r
-static void ssh2_timer(void *ctx, long now)\r
-{\r
- Ssh ssh = (Ssh)ctx;\r
-\r
- if (ssh->state == SSH_STATE_CLOSED)\r
- return;\r
-\r
- if (!ssh->kex_in_progress && ssh->cfg.ssh_rekey_time != 0 &&\r
- now - ssh->next_rekey >= 0) {\r
- do_ssh2_transport(ssh, "timeout", -1, NULL);\r
- }\r
-}\r
-\r
-static void ssh2_protocol(Ssh ssh, void *vin, int inlen,\r
- struct Packet *pktin)\r
-{\r
- unsigned char *in = (unsigned char *)vin;\r
- if (ssh->state == SSH_STATE_CLOSED)\r
- return;\r
-\r
- if (pktin) {\r
- ssh->incoming_data_size += pktin->encrypted_len;\r
- if (!ssh->kex_in_progress &&\r
- ssh->max_data_size != 0 &&\r
- ssh->incoming_data_size > ssh->max_data_size)\r
- do_ssh2_transport(ssh, "too much data received", -1, NULL);\r
- }\r
-\r
- if (pktin && ssh->packet_dispatch[pktin->type]) {\r
- ssh->packet_dispatch[pktin->type](ssh, pktin);\r
- return;\r
- }\r
-\r
- if (!ssh->protocol_initial_phase_done ||\r
- (pktin && pktin->type >= 20 && pktin->type < 50)) {\r
- if (do_ssh2_transport(ssh, in, inlen, pktin) &&\r
- !ssh->protocol_initial_phase_done) {\r
- ssh->protocol_initial_phase_done = TRUE;\r
- /*\r
- * Allow authconn to initialise itself.\r
- */\r
- do_ssh2_authconn(ssh, NULL, 0, NULL);\r
- }\r
- } else {\r
- do_ssh2_authconn(ssh, in, inlen, pktin);\r
- }\r
-}\r
-\r
-/*\r
- * Called to set up the connection.\r
- *\r
- * Returns an error message, or NULL on success.\r
- */\r
-static const char *ssh_init(void *frontend_handle, void **backend_handle,\r
- Config *cfg,\r
- char *host, int port, char **realhost, int nodelay,\r
- int keepalive)\r
-{\r
- const char *p;\r
- Ssh ssh;\r
-\r
- ssh = snew(struct ssh_tag);\r
- ssh->cfg = *cfg; /* STRUCTURE COPY */\r
- ssh->version = 0; /* when not ready yet */\r
- ssh->s = NULL;\r
- ssh->cipher = NULL;\r
- ssh->v1_cipher_ctx = NULL;\r
- ssh->crcda_ctx = NULL;\r
- ssh->cscipher = NULL;\r
- ssh->cs_cipher_ctx = NULL;\r
- ssh->sccipher = NULL;\r
- ssh->sc_cipher_ctx = NULL;\r
- ssh->csmac = NULL;\r
- ssh->cs_mac_ctx = NULL;\r
- ssh->scmac = NULL;\r
- ssh->sc_mac_ctx = NULL;\r
- ssh->cscomp = NULL;\r
- ssh->cs_comp_ctx = NULL;\r
- ssh->sccomp = NULL;\r
- ssh->sc_comp_ctx = NULL;\r
- ssh->kex = NULL;\r
- ssh->kex_ctx = NULL;\r
- ssh->hostkey = NULL;\r
- ssh->exitcode = -1;\r
- ssh->close_expected = FALSE;\r
- ssh->clean_exit = FALSE;\r
- ssh->state = SSH_STATE_PREPACKET;\r
- ssh->size_needed = FALSE;\r
- ssh->eof_needed = FALSE;\r
- ssh->ldisc = NULL;\r
- ssh->logctx = NULL;\r
- ssh->deferred_send_data = NULL;\r
- ssh->deferred_len = 0;\r
- ssh->deferred_size = 0;\r
- ssh->fallback_cmd = 0;\r
- ssh->pkt_kctx = SSH2_PKTCTX_NOKEX;\r
- ssh->pkt_actx = SSH2_PKTCTX_NOAUTH;\r
- ssh->x11disp = NULL;\r
- ssh->v1_compressing = FALSE;\r
- ssh->v2_outgoing_sequence = 0;\r
- ssh->ssh1_rdpkt_crstate = 0;\r
- ssh->ssh2_rdpkt_crstate = 0;\r
- ssh->do_ssh_init_crstate = 0;\r
- ssh->ssh_gotdata_crstate = 0;\r
- ssh->do_ssh1_connection_crstate = 0;\r
- ssh->do_ssh1_login_crstate = 0;\r
- ssh->do_ssh2_transport_crstate = 0;\r
- ssh->do_ssh2_authconn_crstate = 0;\r
- ssh->do_ssh_init_state = NULL;\r
- ssh->do_ssh1_login_state = NULL;\r
- ssh->do_ssh2_transport_state = NULL;\r
- ssh->do_ssh2_authconn_state = NULL;\r
- ssh->v_c = NULL;\r
- ssh->v_s = NULL;\r
- ssh->mainchan = NULL;\r
- ssh->throttled_all = 0;\r
- ssh->v1_stdout_throttling = 0;\r
- ssh->queue = NULL;\r
- ssh->queuelen = ssh->queuesize = 0;\r
- ssh->queueing = FALSE;\r
- ssh->qhead = ssh->qtail = NULL;\r
- ssh->deferred_rekey_reason = NULL;\r
- bufchain_init(&ssh->queued_incoming_data);\r
- ssh->frozen = FALSE;\r
-\r
- *backend_handle = ssh;\r
-\r
-#ifdef MSCRYPTOAPI\r
- if (crypto_startup() == 0)\r
- return "Microsoft high encryption pack not installed!";\r
-#endif\r
-\r
- ssh->frontend = frontend_handle;\r
- ssh->term_width = ssh->cfg.width;\r
- ssh->term_height = ssh->cfg.height;\r
-\r
- ssh->channels = NULL;\r
- ssh->rportfwds = NULL;\r
- ssh->portfwds = NULL;\r
-\r
- ssh->send_ok = 0;\r
- ssh->editing = 0;\r
- ssh->echoing = 0;\r
- ssh->conn_throttle_count = 0;\r
- ssh->overall_bufsize = 0;\r
- ssh->fallback_cmd = 0;\r
-\r
- ssh->protocol = NULL;\r
-\r
- ssh->protocol_initial_phase_done = FALSE;\r
-\r
- ssh->pinger = NULL;\r
-\r
- ssh->incoming_data_size = ssh->outgoing_data_size =\r
- ssh->deferred_data_size = 0L;\r
- ssh->max_data_size = parse_blocksize(ssh->cfg.ssh_rekey_data);\r
- ssh->kex_in_progress = FALSE;\r
-\r
-#ifndef NO_GSSAPI\r
- ssh->gsslibs = NULL;\r
-#endif\r
-\r
- p = connect_to_host(ssh, host, port, realhost, nodelay, keepalive);\r
- if (p != NULL)\r
- return p;\r
-\r
- random_ref();\r
-\r
- return NULL;\r
-}\r
-\r
-static void ssh_free(void *handle)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- struct ssh_channel *c;\r
- struct ssh_rportfwd *pf;\r
-\r
- if (ssh->v1_cipher_ctx)\r
- ssh->cipher->free_context(ssh->v1_cipher_ctx);\r
- if (ssh->cs_cipher_ctx)\r
- ssh->cscipher->free_context(ssh->cs_cipher_ctx);\r
- if (ssh->sc_cipher_ctx)\r
- ssh->sccipher->free_context(ssh->sc_cipher_ctx);\r
- if (ssh->cs_mac_ctx)\r
- ssh->csmac->free_context(ssh->cs_mac_ctx);\r
- if (ssh->sc_mac_ctx)\r
- ssh->scmac->free_context(ssh->sc_mac_ctx);\r
- if (ssh->cs_comp_ctx) {\r
- if (ssh->cscomp)\r
- ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx);\r
- else\r
- zlib_compress_cleanup(ssh->cs_comp_ctx);\r
- }\r
- if (ssh->sc_comp_ctx) {\r
- if (ssh->sccomp)\r
- ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx);\r
- else\r
- zlib_decompress_cleanup(ssh->sc_comp_ctx);\r
- }\r
- if (ssh->kex_ctx)\r
- dh_cleanup(ssh->kex_ctx);\r
- sfree(ssh->savedhost);\r
-\r
- while (ssh->queuelen-- > 0)\r
- ssh_free_packet(ssh->queue[ssh->queuelen]);\r
- sfree(ssh->queue);\r
-\r
- while (ssh->qhead) {\r
- struct queued_handler *qh = ssh->qhead;\r
- ssh->qhead = qh->next;\r
- sfree(ssh->qhead);\r
- }\r
- ssh->qhead = ssh->qtail = NULL;\r
-\r
- if (ssh->channels) {\r
- while ((c = delpos234(ssh->channels, 0)) != NULL) {\r
- switch (c->type) {\r
- case CHAN_X11:\r
- if (c->u.x11.s != NULL)\r
- x11_close(c->u.x11.s);\r
- break;\r
- case CHAN_SOCKDATA:\r
- case CHAN_SOCKDATA_DORMANT:\r
- if (c->u.pfd.s != NULL)\r
- pfd_close(c->u.pfd.s);\r
- break;\r
- }\r
- sfree(c);\r
- }\r
- freetree234(ssh->channels);\r
- ssh->channels = NULL;\r
- }\r
-\r
- if (ssh->rportfwds) {\r
- while ((pf = delpos234(ssh->rportfwds, 0)) != NULL)\r
- free_rportfwd(pf);\r
- freetree234(ssh->rportfwds);\r
- ssh->rportfwds = NULL;\r
- }\r
- sfree(ssh->deferred_send_data);\r
- if (ssh->x11disp)\r
- x11_free_display(ssh->x11disp);\r
- sfree(ssh->do_ssh_init_state);\r
- sfree(ssh->do_ssh1_login_state);\r
- sfree(ssh->do_ssh2_transport_state);\r
- sfree(ssh->do_ssh2_authconn_state);\r
- sfree(ssh->v_c);\r
- sfree(ssh->v_s);\r
- sfree(ssh->fullhostname);\r
- if (ssh->crcda_ctx) {\r
- crcda_free_context(ssh->crcda_ctx);\r
- ssh->crcda_ctx = NULL;\r
- }\r
- if (ssh->s)\r
- ssh_do_close(ssh, TRUE);\r
- expire_timer_context(ssh);\r
- if (ssh->pinger)\r
- pinger_free(ssh->pinger);\r
- bufchain_clear(&ssh->queued_incoming_data);\r
-#ifndef NO_GSSAPI\r
- if (ssh->gsslibs)\r
- ssh_gss_cleanup(ssh->gsslibs);\r
-#endif\r
- sfree(ssh);\r
-\r
- random_unref();\r
-}\r
-\r
-/*\r
- * Reconfigure the SSH backend.\r
- */\r
-static void ssh_reconfig(void *handle, Config *cfg)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- char *rekeying = NULL, rekey_mandatory = FALSE;\r
- unsigned long old_max_data_size;\r
-\r
- pinger_reconfig(ssh->pinger, &ssh->cfg, cfg);\r
- if (ssh->portfwds)\r
- ssh_setup_portfwd(ssh, cfg);\r
-\r
- if (ssh->cfg.ssh_rekey_time != cfg->ssh_rekey_time &&\r
- cfg->ssh_rekey_time != 0) {\r
- long new_next = ssh->last_rekey + cfg->ssh_rekey_time*60*TICKSPERSEC;\r
- long now = GETTICKCOUNT();\r
-\r
- if (new_next - now < 0) {\r
- rekeying = "timeout shortened";\r
- } else {\r
- ssh->next_rekey = schedule_timer(new_next - now, ssh2_timer, ssh);\r
- }\r
- }\r
-\r
- old_max_data_size = ssh->max_data_size;\r
- ssh->max_data_size = parse_blocksize(cfg->ssh_rekey_data);\r
- if (old_max_data_size != ssh->max_data_size &&\r
- ssh->max_data_size != 0) {\r
- if (ssh->outgoing_data_size > ssh->max_data_size ||\r
- ssh->incoming_data_size > ssh->max_data_size)\r
- rekeying = "data limit lowered";\r
- }\r
-\r
- if (ssh->cfg.compression != cfg->compression) {\r
- rekeying = "compression setting changed";\r
- rekey_mandatory = TRUE;\r
- }\r
-\r
- if (ssh->cfg.ssh2_des_cbc != cfg->ssh2_des_cbc ||\r
- memcmp(ssh->cfg.ssh_cipherlist, cfg->ssh_cipherlist,\r
- sizeof(ssh->cfg.ssh_cipherlist))) {\r
- rekeying = "cipher settings changed";\r
- rekey_mandatory = TRUE;\r
- }\r
-\r
- ssh->cfg = *cfg; /* STRUCTURE COPY */\r
-\r
- if (rekeying) {\r
- if (!ssh->kex_in_progress) {\r
- do_ssh2_transport(ssh, rekeying, -1, NULL);\r
- } else if (rekey_mandatory) {\r
- ssh->deferred_rekey_reason = rekeying;\r
- }\r
- }\r
-}\r
-\r
-/*\r
- * Called to send data down the SSH connection.\r
- */\r
-static int ssh_send(void *handle, char *buf, int len)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
-\r
- if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL)\r
- return 0;\r
-\r
- ssh->protocol(ssh, (unsigned char *)buf, len, 0);\r
-\r
- return ssh_sendbuffer(ssh);\r
-}\r
-\r
-/*\r
- * Called to query the current amount of buffered stdin data.\r
- */\r
-static int ssh_sendbuffer(void *handle)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- int override_value;\r
-\r
- if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL)\r
- return 0;\r
-\r
- /*\r
- * If the SSH socket itself has backed up, add the total backup\r
- * size on that to any individual buffer on the stdin channel.\r
- */\r
- override_value = 0;\r
- if (ssh->throttled_all)\r
- override_value = ssh->overall_bufsize;\r
-\r
- if (ssh->version == 1) {\r
- return override_value;\r
- } else if (ssh->version == 2) {\r
- if (!ssh->mainchan || ssh->mainchan->closes > 0)\r
- return override_value;\r
- else\r
- return (override_value +\r
- bufchain_size(&ssh->mainchan->v.v2.outbuffer));\r
- }\r
-\r
- return 0;\r
-}\r
-\r
-/*\r
- * Called to set the size of the window from SSH's POV.\r
- */\r
-static void ssh_size(void *handle, int width, int height)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- struct Packet *pktout;\r
-\r
- ssh->term_width = width;\r
- ssh->term_height = height;\r
-\r
- switch (ssh->state) {\r
- case SSH_STATE_BEFORE_SIZE:\r
- case SSH_STATE_PREPACKET:\r
- case SSH_STATE_CLOSED:\r
- break; /* do nothing */\r
- case SSH_STATE_INTERMED:\r
- ssh->size_needed = TRUE; /* buffer for later */\r
- break;\r
- case SSH_STATE_SESSION:\r
- if (!ssh->cfg.nopty) {\r
- if (ssh->version == 1) {\r
- send_packet(ssh, SSH1_CMSG_WINDOW_SIZE,\r
- PKT_INT, ssh->term_height,\r
- PKT_INT, ssh->term_width,\r
- PKT_INT, 0, PKT_INT, 0, PKT_END);\r
- } else if (ssh->mainchan) {\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid);\r
- ssh2_pkt_addstring(pktout, "window-change");\r
- ssh2_pkt_addbool(pktout, 0);\r
- ssh2_pkt_adduint32(pktout, ssh->term_width);\r
- ssh2_pkt_adduint32(pktout, ssh->term_height);\r
- ssh2_pkt_adduint32(pktout, 0);\r
- ssh2_pkt_adduint32(pktout, 0);\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
- }\r
- break;\r
- }\r
-}\r
-\r
-/*\r
- * Return a list of the special codes that make sense in this\r
- * protocol.\r
- */\r
-static const struct telnet_special *ssh_get_specials(void *handle)\r
-{\r
- static const struct telnet_special ssh1_ignore_special[] = {\r
- {"IGNORE message", TS_NOP}\r
- };\r
- static const struct telnet_special ssh2_ignore_special[] = {\r
- {"IGNORE message", TS_NOP},\r
- };\r
- static const struct telnet_special ssh2_rekey_special[] = {\r
- {"Repeat key exchange", TS_REKEY},\r
- };\r
- static const struct telnet_special ssh2_session_specials[] = {\r
- {NULL, TS_SEP},\r
- {"Break", TS_BRK},\r
- /* These are the signal names defined by RFC 4254.\r
- * They include all the ISO C signals, but are a subset of the POSIX\r
- * required signals. */\r
- {"SIGINT (Interrupt)", TS_SIGINT},\r
- {"SIGTERM (Terminate)", TS_SIGTERM},\r
- {"SIGKILL (Kill)", TS_SIGKILL},\r
- {"SIGQUIT (Quit)", TS_SIGQUIT},\r
- {"SIGHUP (Hangup)", TS_SIGHUP},\r
- {"More signals", TS_SUBMENU},\r
- {"SIGABRT", TS_SIGABRT}, {"SIGALRM", TS_SIGALRM},\r
- {"SIGFPE", TS_SIGFPE}, {"SIGILL", TS_SIGILL},\r
- {"SIGPIPE", TS_SIGPIPE}, {"SIGSEGV", TS_SIGSEGV},\r
- {"SIGUSR1", TS_SIGUSR1}, {"SIGUSR2", TS_SIGUSR2},\r
- {NULL, TS_EXITMENU}\r
- };\r
- static const struct telnet_special specials_end[] = {\r
- {NULL, TS_EXITMENU}\r
- };\r
- /* XXX review this length for any changes: */\r
- static struct telnet_special ssh_specials[lenof(ssh2_ignore_special) +\r
- lenof(ssh2_rekey_special) +\r
- lenof(ssh2_session_specials) +\r
- lenof(specials_end)];\r
- Ssh ssh = (Ssh) handle;\r
- int i = 0;\r
-#define ADD_SPECIALS(name) \\r
- do { \\r
- assert((i + lenof(name)) <= lenof(ssh_specials)); \\r
- memcpy(&ssh_specials[i], name, sizeof name); \\r
- i += lenof(name); \\r
- } while(0)\r
-\r
- if (ssh->version == 1) {\r
- /* Don't bother offering IGNORE if we've decided the remote\r
- * won't cope with it, since we wouldn't bother sending it if\r
- * asked anyway. */\r
- if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE))\r
- ADD_SPECIALS(ssh1_ignore_special);\r
- } else if (ssh->version == 2) {\r
- if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE))\r
- ADD_SPECIALS(ssh2_ignore_special);\r
- if (!(ssh->remote_bugs & BUG_SSH2_REKEY))\r
- ADD_SPECIALS(ssh2_rekey_special);\r
- if (ssh->mainchan)\r
- ADD_SPECIALS(ssh2_session_specials);\r
- } /* else we're not ready yet */\r
-\r
- if (i) {\r
- ADD_SPECIALS(specials_end);\r
- return ssh_specials;\r
- } else {\r
- return NULL;\r
- }\r
-#undef ADD_SPECIALS\r
-}\r
-\r
-/*\r
- * Send special codes. TS_EOF is useful for `plink', so you\r
- * can send an EOF and collect resulting output (e.g. `plink\r
- * hostname sort').\r
- */\r
-static void ssh_special(void *handle, Telnet_Special code)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- struct Packet *pktout;\r
-\r
- if (code == TS_EOF) {\r
- if (ssh->state != SSH_STATE_SESSION) {\r
- /*\r
- * Buffer the EOF in case we are pre-SESSION, so we can\r
- * send it as soon as we reach SESSION.\r
- */\r
- if (code == TS_EOF)\r
- ssh->eof_needed = TRUE;\r
- return;\r
- }\r
- if (ssh->version == 1) {\r
- send_packet(ssh, SSH1_CMSG_EOF, PKT_END);\r
- } else if (ssh->mainchan) {\r
- struct Packet *pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF);\r
- ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid);\r
- ssh2_pkt_send(ssh, pktout);\r
- ssh->send_ok = 0; /* now stop trying to read from stdin */\r
- }\r
- logevent("Sent EOF message");\r
- } else if (code == TS_PING || code == TS_NOP) {\r
- if (ssh->state == SSH_STATE_CLOSED\r
- || ssh->state == SSH_STATE_PREPACKET) return;\r
- if (ssh->version == 1) {\r
- if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE))\r
- send_packet(ssh, SSH1_MSG_IGNORE, PKT_STR, "", PKT_END);\r
- } else {\r
- if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {\r
- pktout = ssh2_pkt_init(SSH2_MSG_IGNORE);\r
- ssh2_pkt_addstring_start(pktout);\r
- ssh2_pkt_send_noqueue(ssh, pktout);\r
- }\r
- }\r
- } else if (code == TS_REKEY) {\r
- if (!ssh->kex_in_progress && ssh->version == 2) {\r
- do_ssh2_transport(ssh, "at user request", -1, NULL);\r
- }\r
- } else if (code == TS_BRK) {\r
- if (ssh->state == SSH_STATE_CLOSED\r
- || ssh->state == SSH_STATE_PREPACKET) return;\r
- if (ssh->version == 1) {\r
- logevent("Unable to send BREAK signal in SSH-1");\r
- } else if (ssh->mainchan) {\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid);\r
- ssh2_pkt_addstring(pktout, "break");\r
- ssh2_pkt_addbool(pktout, 0);\r
- ssh2_pkt_adduint32(pktout, 0); /* default break length */\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
- } else {\r
- /* Is is a POSIX signal? */\r
- char *signame = NULL;\r
- if (code == TS_SIGABRT) signame = "ABRT";\r
- if (code == TS_SIGALRM) signame = "ALRM";\r
- if (code == TS_SIGFPE) signame = "FPE";\r
- if (code == TS_SIGHUP) signame = "HUP";\r
- if (code == TS_SIGILL) signame = "ILL";\r
- if (code == TS_SIGINT) signame = "INT";\r
- if (code == TS_SIGKILL) signame = "KILL";\r
- if (code == TS_SIGPIPE) signame = "PIPE";\r
- if (code == TS_SIGQUIT) signame = "QUIT";\r
- if (code == TS_SIGSEGV) signame = "SEGV";\r
- if (code == TS_SIGTERM) signame = "TERM";\r
- if (code == TS_SIGUSR1) signame = "USR1";\r
- if (code == TS_SIGUSR2) signame = "USR2";\r
- /* The SSH-2 protocol does in principle support arbitrary named\r
- * signals, including signame@domain, but we don't support those. */\r
- if (signame) {\r
- /* It's a signal. */\r
- if (ssh->version == 2 && ssh->mainchan) {\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);\r
- ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid);\r
- ssh2_pkt_addstring(pktout, "signal");\r
- ssh2_pkt_addbool(pktout, 0);\r
- ssh2_pkt_addstring(pktout, signame);\r
- ssh2_pkt_send(ssh, pktout);\r
- logeventf(ssh, "Sent signal SIG%s", signame);\r
- }\r
- } else {\r
- /* Never heard of it. Do nothing */\r
- }\r
- }\r
-}\r
-\r
-void *new_sock_channel(void *handle, Socket s)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- struct ssh_channel *c;\r
- c = snew(struct ssh_channel);\r
-\r
- c->ssh = ssh;\r
- ssh2_channel_init(c);\r
- c->halfopen = TRUE;\r
- c->type = CHAN_SOCKDATA_DORMANT;/* identify channel type */\r
- c->u.pfd.s = s;\r
- add234(ssh->channels, c);\r
- return c;\r
-}\r
-\r
-/*\r
- * This is called when stdout/stderr (the entity to which\r
- * from_backend sends data) manages to clear some backlog.\r
- */\r
-static void ssh_unthrottle(void *handle, int bufsize)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- int buflimit;\r
-\r
- if (ssh->version == 1) {\r
- if (ssh->v1_stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) {\r
- ssh->v1_stdout_throttling = 0;\r
- ssh_throttle_conn(ssh, -1);\r
- }\r
- } else {\r
- if (ssh->mainchan) {\r
- ssh2_set_window(ssh->mainchan,\r
- bufsize < ssh->mainchan->v.v2.locmaxwin ?\r
- ssh->mainchan->v.v2.locmaxwin - bufsize : 0);\r
- if (ssh->cfg.ssh_simple)\r
- buflimit = 0;\r
- else\r
- buflimit = ssh->mainchan->v.v2.locmaxwin;\r
- if (ssh->mainchan->throttling_conn && bufsize <= buflimit) {\r
- ssh->mainchan->throttling_conn = 0;\r
- ssh_throttle_conn(ssh, -1);\r
- }\r
- }\r
- }\r
-}\r
-\r
-void ssh_send_port_open(void *channel, char *hostname, int port, char *org)\r
-{\r
- struct ssh_channel *c = (struct ssh_channel *)channel;\r
- Ssh ssh = c->ssh;\r
- struct Packet *pktout;\r
-\r
- logeventf(ssh, "Opening forwarded connection to %s:%d", hostname, port);\r
-\r
- if (ssh->version == 1) {\r
- send_packet(ssh, SSH1_MSG_PORT_OPEN,\r
- PKT_INT, c->localid,\r
- PKT_STR, hostname,\r
- PKT_INT, port,\r
- /* PKT_STR, <org:orgport>, */\r
- PKT_END);\r
- } else {\r
- pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);\r
- ssh2_pkt_addstring(pktout, "direct-tcpip");\r
- ssh2_pkt_adduint32(pktout, c->localid);\r
- ssh2_pkt_adduint32(pktout, c->v.v2.locwindow);/* our window size */\r
- ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */\r
- ssh2_pkt_addstring(pktout, hostname);\r
- ssh2_pkt_adduint32(pktout, port);\r
- /*\r
- * We make up values for the originator data; partly it's\r
- * too much hassle to keep track, and partly I'm not\r
- * convinced the server should be told details like that\r
- * about my local network configuration.\r
- * The "originator IP address" is syntactically a numeric\r
- * IP address, and some servers (e.g., Tectia) get upset\r
- * if it doesn't match this syntax.\r
- */\r
- ssh2_pkt_addstring(pktout, "0.0.0.0");\r
- ssh2_pkt_adduint32(pktout, 0);\r
- ssh2_pkt_send(ssh, pktout);\r
- }\r
-}\r
-\r
-static int ssh_connected(void *handle)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- return ssh->s != NULL;\r
-}\r
-\r
-static int ssh_sendok(void *handle)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- return ssh->send_ok;\r
-}\r
-\r
-static int ssh_ldisc(void *handle, int option)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- if (option == LD_ECHO)\r
- return ssh->echoing;\r
- if (option == LD_EDIT)\r
- return ssh->editing;\r
- return FALSE;\r
-}\r
-\r
-static void ssh_provide_ldisc(void *handle, void *ldisc)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- ssh->ldisc = ldisc;\r
-}\r
-\r
-static void ssh_provide_logctx(void *handle, void *logctx)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- ssh->logctx = logctx;\r
-}\r
-\r
-static int ssh_return_exitcode(void *handle)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- if (ssh->s != NULL)\r
- return -1;\r
- else\r
- return (ssh->exitcode >= 0 ? ssh->exitcode : INT_MAX);\r
-}\r
-\r
-/*\r
- * cfg_info for SSH is the currently running version of the\r
- * protocol. (1 for 1; 2 for 2; 0 for not-decided-yet.)\r
- */\r
-static int ssh_cfg_info(void *handle)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- return ssh->version;\r
-}\r
-\r
-/*\r
- * Gross hack: pscp will try to start SFTP but fall back to scp1 if\r
- * that fails. This variable is the means by which scp.c can reach\r
- * into the SSH code and find out which one it got.\r
- */\r
-extern int ssh_fallback_cmd(void *handle)\r
-{\r
- Ssh ssh = (Ssh) handle;\r
- return ssh->fallback_cmd;\r
-}\r
-\r
-Backend ssh_backend = {\r
- ssh_init,\r
- ssh_free,\r
- ssh_reconfig,\r
- ssh_send,\r
- ssh_sendbuffer,\r
- ssh_size,\r
- ssh_special,\r
- ssh_get_specials,\r
- ssh_connected,\r
- ssh_return_exitcode,\r
- ssh_sendok,\r
- ssh_ldisc,\r
- ssh_provide_ldisc,\r
- ssh_provide_logctx,\r
- ssh_unthrottle,\r
- ssh_cfg_info,\r
- "ssh",\r
- PROT_SSH,\r
- 22\r
-};\r