| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdarg.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "putty.h" |
| 7 | #include "tree234.h" |
| 8 | #include "ssh.h" |
| 9 | |
| 10 | #ifndef FALSE |
| 11 | #define FALSE 0 |
| 12 | #endif |
| 13 | #ifndef TRUE |
| 14 | #define TRUE 1 |
| 15 | #endif |
| 16 | |
| 17 | #define SSH1_MSG_DISCONNECT 1 /* 0x1 */ |
| 18 | #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */ |
| 19 | #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */ |
| 20 | #define SSH1_CMSG_USER 4 /* 0x4 */ |
| 21 | #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */ |
| 22 | #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */ |
| 23 | #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */ |
| 24 | #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */ |
| 25 | #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */ |
| 26 | #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */ |
| 27 | #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */ |
| 28 | #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */ |
| 29 | #define SSH1_SMSG_SUCCESS 14 /* 0xe */ |
| 30 | #define SSH1_SMSG_FAILURE 15 /* 0xf */ |
| 31 | #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */ |
| 32 | #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */ |
| 33 | #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */ |
| 34 | #define SSH1_CMSG_EOF 19 /* 0x13 */ |
| 35 | #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */ |
| 36 | #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */ |
| 37 | #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */ |
| 38 | #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */ |
| 39 | #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */ |
| 40 | #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */ |
| 41 | #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */ |
| 42 | #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */ |
| 43 | #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */ |
| 44 | #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */ |
| 45 | #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */ |
| 46 | #define SSH1_MSG_IGNORE 32 /* 0x20 */ |
| 47 | #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */ |
| 48 | #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */ |
| 49 | #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */ |
| 50 | #define SSH1_MSG_DEBUG 36 /* 0x24 */ |
| 51 | #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */ |
| 52 | #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */ |
| 53 | #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */ |
| 54 | #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */ |
| 55 | #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */ |
| 56 | #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */ |
| 57 | #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */ |
| 58 | |
| 59 | #define SSH1_AUTH_TIS 5 /* 0x5 */ |
| 60 | #define SSH1_AUTH_CCARD 16 /* 0x10 */ |
| 61 | |
| 62 | #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */ |
| 63 | /* Mask for protoflags we will echo back to server if seen */ |
| 64 | #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */ |
| 65 | |
| 66 | #define SSH2_MSG_DISCONNECT 1 /* 0x1 */ |
| 67 | #define SSH2_MSG_IGNORE 2 /* 0x2 */ |
| 68 | #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */ |
| 69 | #define SSH2_MSG_DEBUG 4 /* 0x4 */ |
| 70 | #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */ |
| 71 | #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */ |
| 72 | #define SSH2_MSG_KEXINIT 20 /* 0x14 */ |
| 73 | #define SSH2_MSG_NEWKEYS 21 /* 0x15 */ |
| 74 | #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */ |
| 75 | #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */ |
| 76 | #define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */ |
| 77 | #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */ |
| 78 | #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */ |
| 79 | #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */ |
| 80 | #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */ |
| 81 | #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */ |
| 82 | #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */ |
| 83 | #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */ |
| 84 | #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */ |
| 85 | #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */ |
| 86 | #define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */ |
| 87 | #define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */ |
| 88 | #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */ |
| 89 | #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */ |
| 90 | #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */ |
| 91 | #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */ |
| 92 | #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */ |
| 93 | #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */ |
| 94 | #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */ |
| 95 | #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */ |
| 96 | #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */ |
| 97 | #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */ |
| 98 | #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */ |
| 99 | #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */ |
| 100 | #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */ |
| 101 | #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */ |
| 102 | |
| 103 | /* |
| 104 | * Packet type contexts, so that ssh2_pkt_type can correctly decode |
| 105 | * the ambiguous type numbers back into the correct type strings. |
| 106 | */ |
| 107 | #define SSH2_PKTCTX_DHGROUP 0x0001 |
| 108 | #define SSH2_PKTCTX_DHGEX 0x0002 |
| 109 | #define SSH2_PKTCTX_KEX_MASK 0x000F |
| 110 | #define SSH2_PKTCTX_PUBLICKEY 0x0010 |
| 111 | #define SSH2_PKTCTX_PASSWORD 0x0020 |
| 112 | #define SSH2_PKTCTX_KBDINTER 0x0040 |
| 113 | #define SSH2_PKTCTX_AUTH_MASK 0x00F0 |
| 114 | |
| 115 | #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */ |
| 116 | #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */ |
| 117 | #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */ |
| 118 | #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */ |
| 119 | #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */ |
| 120 | #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */ |
| 121 | #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */ |
| 122 | #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */ |
| 123 | #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */ |
| 124 | #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */ |
| 125 | #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */ |
| 126 | #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */ |
| 127 | #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */ |
| 128 | #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */ |
| 129 | #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */ |
| 130 | |
| 131 | static const char *const ssh2_disconnect_reasons[] = { |
| 132 | NULL, |
| 133 | "SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT", |
| 134 | "SSH_DISCONNECT_PROTOCOL_ERROR", |
| 135 | "SSH_DISCONNECT_KEY_EXCHANGE_FAILED", |
| 136 | "SSH_DISCONNECT_HOST_AUTHENTICATION_FAILED", |
| 137 | "SSH_DISCONNECT_MAC_ERROR", |
| 138 | "SSH_DISCONNECT_COMPRESSION_ERROR", |
| 139 | "SSH_DISCONNECT_SERVICE_NOT_AVAILABLE", |
| 140 | "SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED", |
| 141 | "SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE", |
| 142 | "SSH_DISCONNECT_CONNECTION_LOST", |
| 143 | "SSH_DISCONNECT_BY_APPLICATION", |
| 144 | "SSH_DISCONNECT_TOO_MANY_CONNECTIONS", |
| 145 | "SSH_DISCONNECT_AUTH_CANCELLED_BY_USER", |
| 146 | "SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE", |
| 147 | "SSH_DISCONNECT_ILLEGAL_USER_NAME", |
| 148 | }; |
| 149 | |
| 150 | #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */ |
| 151 | #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */ |
| 152 | #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */ |
| 153 | #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */ |
| 154 | |
| 155 | #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */ |
| 156 | |
| 157 | /* |
| 158 | * Various remote-bug flags. |
| 159 | */ |
| 160 | #define BUG_CHOKES_ON_SSH1_IGNORE 1 |
| 161 | #define BUG_SSH2_HMAC 2 |
| 162 | #define BUG_NEEDS_SSH1_PLAIN_PASSWORD 4 |
| 163 | #define BUG_CHOKES_ON_RSA 8 |
| 164 | #define BUG_SSH2_RSA_PADDING 16 |
| 165 | #define BUG_SSH2_DERIVEKEY 32 |
| 166 | #define BUG_SSH2_REKEY 64 |
| 167 | #define BUG_SSH2_PK_SESSIONID 128 |
| 168 | |
| 169 | #define translate(x) if (type == x) return #x |
| 170 | #define translatec(x,ctx) if (type == x && (pkt_ctx & ctx)) return #x |
| 171 | static char *ssh1_pkt_type(int type) |
| 172 | { |
| 173 | translate(SSH1_MSG_DISCONNECT); |
| 174 | translate(SSH1_SMSG_PUBLIC_KEY); |
| 175 | translate(SSH1_CMSG_SESSION_KEY); |
| 176 | translate(SSH1_CMSG_USER); |
| 177 | translate(SSH1_CMSG_AUTH_RSA); |
| 178 | translate(SSH1_SMSG_AUTH_RSA_CHALLENGE); |
| 179 | translate(SSH1_CMSG_AUTH_RSA_RESPONSE); |
| 180 | translate(SSH1_CMSG_AUTH_PASSWORD); |
| 181 | translate(SSH1_CMSG_REQUEST_PTY); |
| 182 | translate(SSH1_CMSG_WINDOW_SIZE); |
| 183 | translate(SSH1_CMSG_EXEC_SHELL); |
| 184 | translate(SSH1_CMSG_EXEC_CMD); |
| 185 | translate(SSH1_SMSG_SUCCESS); |
| 186 | translate(SSH1_SMSG_FAILURE); |
| 187 | translate(SSH1_CMSG_STDIN_DATA); |
| 188 | translate(SSH1_SMSG_STDOUT_DATA); |
| 189 | translate(SSH1_SMSG_STDERR_DATA); |
| 190 | translate(SSH1_CMSG_EOF); |
| 191 | translate(SSH1_SMSG_EXIT_STATUS); |
| 192 | translate(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 193 | translate(SSH1_MSG_CHANNEL_OPEN_FAILURE); |
| 194 | translate(SSH1_MSG_CHANNEL_DATA); |
| 195 | translate(SSH1_MSG_CHANNEL_CLOSE); |
| 196 | translate(SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION); |
| 197 | translate(SSH1_SMSG_X11_OPEN); |
| 198 | translate(SSH1_CMSG_PORT_FORWARD_REQUEST); |
| 199 | translate(SSH1_MSG_PORT_OPEN); |
| 200 | translate(SSH1_CMSG_AGENT_REQUEST_FORWARDING); |
| 201 | translate(SSH1_SMSG_AGENT_OPEN); |
| 202 | translate(SSH1_MSG_IGNORE); |
| 203 | translate(SSH1_CMSG_EXIT_CONFIRMATION); |
| 204 | translate(SSH1_CMSG_X11_REQUEST_FORWARDING); |
| 205 | translate(SSH1_CMSG_AUTH_RHOSTS_RSA); |
| 206 | translate(SSH1_MSG_DEBUG); |
| 207 | translate(SSH1_CMSG_REQUEST_COMPRESSION); |
| 208 | translate(SSH1_CMSG_AUTH_TIS); |
| 209 | translate(SSH1_SMSG_AUTH_TIS_CHALLENGE); |
| 210 | translate(SSH1_CMSG_AUTH_TIS_RESPONSE); |
| 211 | translate(SSH1_CMSG_AUTH_CCARD); |
| 212 | translate(SSH1_SMSG_AUTH_CCARD_CHALLENGE); |
| 213 | translate(SSH1_CMSG_AUTH_CCARD_RESPONSE); |
| 214 | return "unknown"; |
| 215 | } |
| 216 | static char *ssh2_pkt_type(int pkt_ctx, int type) |
| 217 | { |
| 218 | translate(SSH2_MSG_DISCONNECT); |
| 219 | translate(SSH2_MSG_IGNORE); |
| 220 | translate(SSH2_MSG_UNIMPLEMENTED); |
| 221 | translate(SSH2_MSG_DEBUG); |
| 222 | translate(SSH2_MSG_SERVICE_REQUEST); |
| 223 | translate(SSH2_MSG_SERVICE_ACCEPT); |
| 224 | translate(SSH2_MSG_KEXINIT); |
| 225 | translate(SSH2_MSG_NEWKEYS); |
| 226 | translatec(SSH2_MSG_KEXDH_INIT, SSH2_PKTCTX_DHGROUP); |
| 227 | translatec(SSH2_MSG_KEXDH_REPLY, SSH2_PKTCTX_DHGROUP); |
| 228 | translatec(SSH2_MSG_KEX_DH_GEX_REQUEST, SSH2_PKTCTX_DHGEX); |
| 229 | translatec(SSH2_MSG_KEX_DH_GEX_GROUP, SSH2_PKTCTX_DHGEX); |
| 230 | translatec(SSH2_MSG_KEX_DH_GEX_INIT, SSH2_PKTCTX_DHGEX); |
| 231 | translatec(SSH2_MSG_KEX_DH_GEX_REPLY, SSH2_PKTCTX_DHGEX); |
| 232 | translate(SSH2_MSG_USERAUTH_REQUEST); |
| 233 | translate(SSH2_MSG_USERAUTH_FAILURE); |
| 234 | translate(SSH2_MSG_USERAUTH_SUCCESS); |
| 235 | translate(SSH2_MSG_USERAUTH_BANNER); |
| 236 | translatec(SSH2_MSG_USERAUTH_PK_OK, SSH2_PKTCTX_PUBLICKEY); |
| 237 | translatec(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, SSH2_PKTCTX_PASSWORD); |
| 238 | translatec(SSH2_MSG_USERAUTH_INFO_REQUEST, SSH2_PKTCTX_KBDINTER); |
| 239 | translatec(SSH2_MSG_USERAUTH_INFO_RESPONSE, SSH2_PKTCTX_KBDINTER); |
| 240 | translate(SSH2_MSG_GLOBAL_REQUEST); |
| 241 | translate(SSH2_MSG_REQUEST_SUCCESS); |
| 242 | translate(SSH2_MSG_REQUEST_FAILURE); |
| 243 | translate(SSH2_MSG_CHANNEL_OPEN); |
| 244 | translate(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 245 | translate(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 246 | translate(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 247 | translate(SSH2_MSG_CHANNEL_DATA); |
| 248 | translate(SSH2_MSG_CHANNEL_EXTENDED_DATA); |
| 249 | translate(SSH2_MSG_CHANNEL_EOF); |
| 250 | translate(SSH2_MSG_CHANNEL_CLOSE); |
| 251 | translate(SSH2_MSG_CHANNEL_REQUEST); |
| 252 | translate(SSH2_MSG_CHANNEL_SUCCESS); |
| 253 | translate(SSH2_MSG_CHANNEL_FAILURE); |
| 254 | return "unknown"; |
| 255 | } |
| 256 | #undef translate |
| 257 | #undef translatec |
| 258 | |
| 259 | #define GET_32BIT(cp) \ |
| 260 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
| 261 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
| 262 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
| 263 | ((unsigned long)(unsigned char)(cp)[3])) |
| 264 | |
| 265 | #define PUT_32BIT(cp, value) { \ |
| 266 | (cp)[0] = (unsigned char)((value) >> 24); \ |
| 267 | (cp)[1] = (unsigned char)((value) >> 16); \ |
| 268 | (cp)[2] = (unsigned char)((value) >> 8); \ |
| 269 | (cp)[3] = (unsigned char)(value); } |
| 270 | |
| 271 | /* Enumeration values for fields in SSH-1 packets */ |
| 272 | enum { |
| 273 | PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM, |
| 274 | /* These values are for communicating relevant semantics of |
| 275 | * fields to the packet logging code. */ |
| 276 | PKTT_OTHER, PKTT_PASSWORD, PKTT_DATA |
| 277 | }; |
| 278 | |
| 279 | /* |
| 280 | * Coroutine mechanics for the sillier bits of the code. If these |
| 281 | * macros look impenetrable to you, you might find it helpful to |
| 282 | * read |
| 283 | * |
| 284 | * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html |
| 285 | * |
| 286 | * which explains the theory behind these macros. |
| 287 | * |
| 288 | * In particular, if you are getting `case expression not constant' |
| 289 | * errors when building with MS Visual Studio, this is because MS's |
| 290 | * Edit and Continue debugging feature causes their compiler to |
| 291 | * violate ANSI C. To disable Edit and Continue debugging: |
| 292 | * |
| 293 | * - right-click ssh.c in the FileView |
| 294 | * - click Settings |
| 295 | * - select the C/C++ tab and the General category |
| 296 | * - under `Debug info:', select anything _other_ than `Program |
| 297 | * Database for Edit and Continue'. |
| 298 | */ |
| 299 | #define crBegin(v) { int *crLine = &v; switch(v) { case 0:; |
| 300 | #define crState(t) \ |
| 301 | struct t *s; \ |
| 302 | if (!ssh->t) ssh->t = snew(struct t); \ |
| 303 | s = ssh->t; |
| 304 | #define crFinish(z) } *crLine = 0; return (z); } |
| 305 | #define crFinishV } *crLine = 0; return; } |
| 306 | #define crReturn(z) \ |
| 307 | do {\ |
| 308 | *crLine =__LINE__; return (z); case __LINE__:;\ |
| 309 | } while (0) |
| 310 | #define crReturnV \ |
| 311 | do {\ |
| 312 | *crLine=__LINE__; return; case __LINE__:;\ |
| 313 | } while (0) |
| 314 | #define crStop(z) do{ *crLine = 0; return (z); }while(0) |
| 315 | #define crStopV do{ *crLine = 0; return; }while(0) |
| 316 | #define crWaitUntil(c) do { crReturn(0); } while (!(c)) |
| 317 | #define crWaitUntilV(c) do { crReturnV; } while (!(c)) |
| 318 | |
| 319 | typedef struct ssh_tag *Ssh; |
| 320 | struct Packet; |
| 321 | |
| 322 | static struct Packet *ssh2_pkt_init(int pkt_type); |
| 323 | static void ssh2_pkt_addbool(struct Packet *, unsigned char value); |
| 324 | static void ssh2_pkt_adduint32(struct Packet *, unsigned long value); |
| 325 | static void ssh2_pkt_addstring_start(struct Packet *); |
| 326 | static void ssh2_pkt_addstring_str(struct Packet *, char *data); |
| 327 | static void ssh2_pkt_addstring_data(struct Packet *, char *data, int len); |
| 328 | static void ssh2_pkt_addstring(struct Packet *, char *data); |
| 329 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len); |
| 330 | static void ssh2_pkt_addmp(struct Packet *, Bignum b); |
| 331 | static int ssh2_pkt_construct(Ssh, struct Packet *); |
| 332 | static void ssh2_pkt_send(Ssh, struct Packet *); |
| 333 | static void ssh2_pkt_send_noqueue(Ssh, struct Packet *); |
| 334 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, |
| 335 | struct Packet *pktin); |
| 336 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, |
| 337 | struct Packet *pktin); |
| 338 | |
| 339 | /* |
| 340 | * Buffer management constants. There are several of these for |
| 341 | * various different purposes: |
| 342 | * |
| 343 | * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up |
| 344 | * on a local data stream before we throttle the whole SSH |
| 345 | * connection (in SSH1 only). Throttling the whole connection is |
| 346 | * pretty drastic so we set this high in the hope it won't |
| 347 | * happen very often. |
| 348 | * |
| 349 | * - SSH_MAX_BACKLOG is the amount of backlog that must build up |
| 350 | * on the SSH connection itself before we defensively throttle |
| 351 | * _all_ local data streams. This is pretty drastic too (though |
| 352 | * thankfully unlikely in SSH2 since the window mechanism should |
| 353 | * ensure that the server never has any need to throttle its end |
| 354 | * of the connection), so we set this high as well. |
| 355 | * |
| 356 | * - OUR_V2_WINSIZE is the maximum window size we present on SSH2 |
| 357 | * channels. |
| 358 | */ |
| 359 | |
| 360 | #define SSH1_BUFFER_LIMIT 32768 |
| 361 | #define SSH_MAX_BACKLOG 32768 |
| 362 | #define OUR_V2_WINSIZE 16384 |
| 363 | #define OUR_V2_MAXPKT 0x4000UL |
| 364 | |
| 365 | const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss }; |
| 366 | |
| 367 | static void *nullmac_make_context(void) |
| 368 | { |
| 369 | return NULL; |
| 370 | } |
| 371 | static void nullmac_free_context(void *handle) |
| 372 | { |
| 373 | } |
| 374 | static void nullmac_key(void *handle, unsigned char *key) |
| 375 | { |
| 376 | } |
| 377 | static void nullmac_generate(void *handle, unsigned char *blk, int len, |
| 378 | unsigned long seq) |
| 379 | { |
| 380 | } |
| 381 | static int nullmac_verify(void *handle, unsigned char *blk, int len, |
| 382 | unsigned long seq) |
| 383 | { |
| 384 | return 1; |
| 385 | } |
| 386 | const static struct ssh_mac ssh_mac_none = { |
| 387 | nullmac_make_context, nullmac_free_context, nullmac_key, |
| 388 | nullmac_generate, nullmac_verify, "none", 0 |
| 389 | }; |
| 390 | const static struct ssh_mac *macs[] = { |
| 391 | &ssh_sha1, &ssh_md5, &ssh_mac_none |
| 392 | }; |
| 393 | const static struct ssh_mac *buggymacs[] = { |
| 394 | &ssh_sha1_buggy, &ssh_md5, &ssh_mac_none |
| 395 | }; |
| 396 | |
| 397 | static void *ssh_comp_none_init(void) |
| 398 | { |
| 399 | return NULL; |
| 400 | } |
| 401 | static void ssh_comp_none_cleanup(void *handle) |
| 402 | { |
| 403 | } |
| 404 | static int ssh_comp_none_block(void *handle, unsigned char *block, int len, |
| 405 | unsigned char **outblock, int *outlen) |
| 406 | { |
| 407 | return 0; |
| 408 | } |
| 409 | static int ssh_comp_none_disable(void *handle) |
| 410 | { |
| 411 | return 0; |
| 412 | } |
| 413 | const static struct ssh_compress ssh_comp_none = { |
| 414 | "none", |
| 415 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
| 416 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
| 417 | ssh_comp_none_disable, NULL |
| 418 | }; |
| 419 | extern const struct ssh_compress ssh_zlib; |
| 420 | const static struct ssh_compress *compressions[] = { |
| 421 | &ssh_zlib, &ssh_comp_none |
| 422 | }; |
| 423 | |
| 424 | enum { /* channel types */ |
| 425 | CHAN_MAINSESSION, |
| 426 | CHAN_X11, |
| 427 | CHAN_AGENT, |
| 428 | CHAN_SOCKDATA, |
| 429 | CHAN_SOCKDATA_DORMANT /* one the remote hasn't confirmed */ |
| 430 | }; |
| 431 | |
| 432 | /* |
| 433 | * 2-3-4 tree storing channels. |
| 434 | */ |
| 435 | struct ssh_channel { |
| 436 | Ssh ssh; /* pointer back to main context */ |
| 437 | unsigned remoteid, localid; |
| 438 | int type; |
| 439 | /* |
| 440 | * In SSH1, this value contains four bits: |
| 441 | * |
| 442 | * 1 We have sent SSH1_MSG_CHANNEL_CLOSE. |
| 443 | * 2 We have sent SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
| 444 | * 4 We have received SSH1_MSG_CHANNEL_CLOSE. |
| 445 | * 8 We have received SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
| 446 | * |
| 447 | * A channel is completely finished with when all four bits are set. |
| 448 | */ |
| 449 | int closes; |
| 450 | union { |
| 451 | struct ssh1_data_channel { |
| 452 | int throttling; |
| 453 | } v1; |
| 454 | struct ssh2_data_channel { |
| 455 | bufchain outbuffer; |
| 456 | unsigned remwindow, remmaxpkt; |
| 457 | unsigned locwindow; |
| 458 | } v2; |
| 459 | } v; |
| 460 | union { |
| 461 | struct ssh_agent_channel { |
| 462 | unsigned char *message; |
| 463 | unsigned char msglen[4]; |
| 464 | unsigned lensofar, totallen; |
| 465 | } a; |
| 466 | struct ssh_x11_channel { |
| 467 | Socket s; |
| 468 | } x11; |
| 469 | struct ssh_pfd_channel { |
| 470 | Socket s; |
| 471 | } pfd; |
| 472 | } u; |
| 473 | }; |
| 474 | |
| 475 | /* |
| 476 | * 2-3-4 tree storing remote->local port forwardings. SSH 1 and SSH |
| 477 | * 2 use this structure in different ways, reflecting SSH 2's |
| 478 | * altogether saner approach to port forwarding. |
| 479 | * |
| 480 | * In SSH 1, you arrange a remote forwarding by sending the server |
| 481 | * the remote port number, and the local destination host:port. |
| 482 | * When a connection comes in, the server sends you back that |
| 483 | * host:port pair, and you connect to it. This is a ready-made |
| 484 | * security hole if you're not on the ball: a malicious server |
| 485 | * could send you back _any_ host:port pair, so if you trustingly |
| 486 | * connect to the address it gives you then you've just opened the |
| 487 | * entire inside of your corporate network just by connecting |
| 488 | * through it to a dodgy SSH server. Hence, we must store a list of |
| 489 | * host:port pairs we _are_ trying to forward to, and reject a |
| 490 | * connection request from the server if it's not in the list. |
| 491 | * |
| 492 | * In SSH 2, each side of the connection minds its own business and |
| 493 | * doesn't send unnecessary information to the other. You arrange a |
| 494 | * remote forwarding by sending the server just the remote port |
| 495 | * number. When a connection comes in, the server tells you which |
| 496 | * of its ports was connected to; and _you_ have to remember what |
| 497 | * local host:port pair went with that port number. |
| 498 | * |
| 499 | * Hence, in SSH 1 this structure is indexed by destination |
| 500 | * host:port pair, whereas in SSH 2 it is indexed by source port. |
| 501 | */ |
| 502 | struct ssh_portfwd; /* forward declaration */ |
| 503 | |
| 504 | struct ssh_rportfwd { |
| 505 | unsigned sport, dport; |
| 506 | char dhost[256]; |
| 507 | char *sportdesc; |
| 508 | struct ssh_portfwd *pfrec; |
| 509 | }; |
| 510 | #define free_rportfwd(pf) ( \ |
| 511 | ((pf) ? (sfree((pf)->sportdesc)) : (void)0 ), sfree(pf) ) |
| 512 | |
| 513 | /* |
| 514 | * Separately to the rportfwd tree (which is for looking up port |
| 515 | * open requests from the server), a tree of _these_ structures is |
| 516 | * used to keep track of all the currently open port forwardings, |
| 517 | * so that we can reconfigure in mid-session if the user requests |
| 518 | * it. |
| 519 | */ |
| 520 | struct ssh_portfwd { |
| 521 | enum { DESTROY, KEEP, CREATE } status; |
| 522 | int type; |
| 523 | unsigned sport, dport; |
| 524 | char *saddr, *daddr; |
| 525 | char *sserv, *dserv; |
| 526 | struct ssh_rportfwd *remote; |
| 527 | int addressfamily; |
| 528 | void *local; |
| 529 | }; |
| 530 | #define free_portfwd(pf) ( \ |
| 531 | ((pf) ? (sfree((pf)->saddr), sfree((pf)->daddr), \ |
| 532 | sfree((pf)->sserv), sfree((pf)->dserv)) : (void)0 ), sfree(pf) ) |
| 533 | |
| 534 | struct Packet { |
| 535 | long length; |
| 536 | int type; |
| 537 | unsigned long sequence; |
| 538 | unsigned char *data; |
| 539 | unsigned char *body; |
| 540 | long savedpos; |
| 541 | long maxlen; |
| 542 | long encrypted_len; /* for SSH2 total-size counting */ |
| 543 | |
| 544 | /* |
| 545 | * State associated with packet logging |
| 546 | */ |
| 547 | int logmode; |
| 548 | int nblanks; |
| 549 | struct logblank_t *blanks; |
| 550 | }; |
| 551 | |
| 552 | static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, |
| 553 | struct Packet *pktin); |
| 554 | static void ssh2_protocol(Ssh ssh, unsigned char *in, int inlen, |
| 555 | struct Packet *pktin); |
| 556 | static void ssh1_protocol_setup(Ssh ssh); |
| 557 | static void ssh2_protocol_setup(Ssh ssh); |
| 558 | static void ssh_size(void *handle, int width, int height); |
| 559 | static void ssh_special(void *handle, Telnet_Special); |
| 560 | static int ssh2_try_send(struct ssh_channel *c); |
| 561 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len); |
| 562 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize); |
| 563 | static void ssh2_set_window(struct ssh_channel *c, unsigned newwin); |
| 564 | static int ssh_sendbuffer(void *handle); |
| 565 | static int ssh_do_close(Ssh ssh, int notify_exit); |
| 566 | static unsigned long ssh_pkt_getuint32(struct Packet *pkt); |
| 567 | static int ssh2_pkt_getbool(struct Packet *pkt); |
| 568 | static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length); |
| 569 | static void ssh2_timer(void *ctx, long now); |
| 570 | static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen, |
| 571 | struct Packet *pktin); |
| 572 | |
| 573 | struct rdpkt1_state_tag { |
| 574 | long len, pad, biglen, to_read; |
| 575 | unsigned long realcrc, gotcrc; |
| 576 | unsigned char *p; |
| 577 | int i; |
| 578 | int chunk; |
| 579 | struct Packet *pktin; |
| 580 | }; |
| 581 | |
| 582 | struct rdpkt2_state_tag { |
| 583 | long len, pad, payload, packetlen, maclen; |
| 584 | int i; |
| 585 | int cipherblk; |
| 586 | unsigned long incoming_sequence; |
| 587 | struct Packet *pktin; |
| 588 | }; |
| 589 | |
| 590 | typedef void (*handler_fn_t)(Ssh ssh, struct Packet *pktin); |
| 591 | typedef void (*chandler_fn_t)(Ssh ssh, struct Packet *pktin, void *ctx); |
| 592 | |
| 593 | struct queued_handler; |
| 594 | struct queued_handler { |
| 595 | int msg1, msg2; |
| 596 | chandler_fn_t handler; |
| 597 | void *ctx; |
| 598 | struct queued_handler *next; |
| 599 | }; |
| 600 | |
| 601 | struct ssh_tag { |
| 602 | const struct plug_function_table *fn; |
| 603 | /* the above field _must_ be first in the structure */ |
| 604 | |
| 605 | SHA_State exhash, exhashbase; |
| 606 | |
| 607 | Socket s; |
| 608 | |
| 609 | void *ldisc; |
| 610 | void *logctx; |
| 611 | |
| 612 | unsigned char session_key[32]; |
| 613 | int v1_compressing; |
| 614 | int v1_remote_protoflags; |
| 615 | int v1_local_protoflags; |
| 616 | int agentfwd_enabled; |
| 617 | int X11_fwd_enabled; |
| 618 | int remote_bugs; |
| 619 | const struct ssh_cipher *cipher; |
| 620 | void *v1_cipher_ctx; |
| 621 | void *crcda_ctx; |
| 622 | const struct ssh2_cipher *cscipher, *sccipher; |
| 623 | void *cs_cipher_ctx, *sc_cipher_ctx; |
| 624 | const struct ssh_mac *csmac, *scmac; |
| 625 | void *cs_mac_ctx, *sc_mac_ctx; |
| 626 | const struct ssh_compress *cscomp, *sccomp; |
| 627 | void *cs_comp_ctx, *sc_comp_ctx; |
| 628 | const struct ssh_kex *kex; |
| 629 | const struct ssh_signkey *hostkey; |
| 630 | unsigned char v2_session_id[20]; |
| 631 | void *kex_ctx; |
| 632 | |
| 633 | char *savedhost; |
| 634 | int savedport; |
| 635 | int send_ok; |
| 636 | int echoing, editing; |
| 637 | |
| 638 | void *frontend; |
| 639 | |
| 640 | int ospeed, ispeed; /* temporaries */ |
| 641 | int term_width, term_height; |
| 642 | |
| 643 | tree234 *channels; /* indexed by local id */ |
| 644 | struct ssh_channel *mainchan; /* primary session channel */ |
| 645 | int exitcode; |
| 646 | int close_expected; |
| 647 | |
| 648 | tree234 *rportfwds, *portfwds; |
| 649 | |
| 650 | enum { |
| 651 | SSH_STATE_PREPACKET, |
| 652 | SSH_STATE_BEFORE_SIZE, |
| 653 | SSH_STATE_INTERMED, |
| 654 | SSH_STATE_SESSION, |
| 655 | SSH_STATE_CLOSED |
| 656 | } state; |
| 657 | |
| 658 | int size_needed, eof_needed; |
| 659 | |
| 660 | struct Packet **queue; |
| 661 | int queuelen, queuesize; |
| 662 | int queueing; |
| 663 | unsigned char *deferred_send_data; |
| 664 | int deferred_len, deferred_size; |
| 665 | |
| 666 | /* |
| 667 | * Gross hack: pscp will try to start SFTP but fall back to |
| 668 | * scp1 if that fails. This variable is the means by which |
| 669 | * scp.c can reach into the SSH code and find out which one it |
| 670 | * got. |
| 671 | */ |
| 672 | int fallback_cmd; |
| 673 | |
| 674 | /* |
| 675 | * Used for username and password input. |
| 676 | */ |
| 677 | char *userpass_input_buffer; |
| 678 | int userpass_input_buflen; |
| 679 | int userpass_input_bufpos; |
| 680 | int userpass_input_echo; |
| 681 | |
| 682 | int pkt_ctx; |
| 683 | |
| 684 | void *x11auth; |
| 685 | |
| 686 | int version; |
| 687 | int v1_throttle_count; |
| 688 | int overall_bufsize; |
| 689 | int throttled_all; |
| 690 | int v1_stdout_throttling; |
| 691 | int v2_outgoing_sequence; |
| 692 | |
| 693 | int ssh1_rdpkt_crstate; |
| 694 | int ssh2_rdpkt_crstate; |
| 695 | int do_ssh_init_crstate; |
| 696 | int ssh_gotdata_crstate; |
| 697 | int do_ssh1_login_crstate; |
| 698 | int do_ssh1_connection_crstate; |
| 699 | int do_ssh2_transport_crstate; |
| 700 | int do_ssh2_authconn_crstate; |
| 701 | |
| 702 | void *do_ssh_init_state; |
| 703 | void *do_ssh1_login_state; |
| 704 | void *do_ssh2_transport_state; |
| 705 | void *do_ssh2_authconn_state; |
| 706 | |
| 707 | struct rdpkt1_state_tag rdpkt1_state; |
| 708 | struct rdpkt2_state_tag rdpkt2_state; |
| 709 | |
| 710 | /* ssh1 and ssh2 use this for different things, but both use it */ |
| 711 | int protocol_initial_phase_done; |
| 712 | |
| 713 | void (*protocol) (Ssh ssh, unsigned char *in, int inlen, |
| 714 | struct Packet *pkt); |
| 715 | struct Packet *(*s_rdpkt) (Ssh ssh, unsigned char **data, int *datalen); |
| 716 | |
| 717 | /* |
| 718 | * We maintain a full _copy_ of a Config structure here, not |
| 719 | * merely a pointer to it. That way, when we're passed a new |
| 720 | * one for reconfiguration, we can check the differences and |
| 721 | * potentially reconfigure port forwardings etc in mid-session. |
| 722 | */ |
| 723 | Config cfg; |
| 724 | |
| 725 | /* |
| 726 | * Used to transfer data back from async agent callbacks. |
| 727 | */ |
| 728 | void *agent_response; |
| 729 | int agent_response_len; |
| 730 | |
| 731 | /* |
| 732 | * Dispatch table for packet types that we may have to deal |
| 733 | * with at any time. |
| 734 | */ |
| 735 | handler_fn_t packet_dispatch[256]; |
| 736 | |
| 737 | /* |
| 738 | * Queues of one-off handler functions for success/failure |
| 739 | * indications from a request. |
| 740 | */ |
| 741 | struct queued_handler *qhead, *qtail; |
| 742 | |
| 743 | /* |
| 744 | * This module deals with sending keepalives. |
| 745 | */ |
| 746 | Pinger pinger; |
| 747 | |
| 748 | /* |
| 749 | * Track incoming and outgoing data sizes and time, for |
| 750 | * size-based rekeys. |
| 751 | */ |
| 752 | unsigned long incoming_data_size, outgoing_data_size, deferred_data_size; |
| 753 | unsigned long max_data_size; |
| 754 | int kex_in_progress; |
| 755 | long next_rekey, last_rekey; |
| 756 | char *deferred_rekey_reason; /* points to STATIC string; don't free */ |
| 757 | }; |
| 758 | |
| 759 | #define logevent(s) logevent(ssh->frontend, s) |
| 760 | |
| 761 | /* logevent, only printf-formatted. */ |
| 762 | static void logeventf(Ssh ssh, const char *fmt, ...) |
| 763 | { |
| 764 | va_list ap; |
| 765 | char *buf; |
| 766 | |
| 767 | va_start(ap, fmt); |
| 768 | buf = dupvprintf(fmt, ap); |
| 769 | va_end(ap); |
| 770 | logevent(buf); |
| 771 | sfree(buf); |
| 772 | } |
| 773 | |
| 774 | #define bombout(msg) \ |
| 775 | do { \ |
| 776 | char *text = dupprintf msg; \ |
| 777 | ssh_do_close(ssh, FALSE); \ |
| 778 | logevent(text); \ |
| 779 | connection_fatal(ssh->frontend, "%s", text); \ |
| 780 | sfree(text); \ |
| 781 | } while (0) |
| 782 | |
| 783 | /* Functions to leave bits out of the SSH packet log file. */ |
| 784 | |
| 785 | static void dont_log_password(Ssh ssh, struct Packet *pkt, int blanktype) |
| 786 | { |
| 787 | if (ssh->cfg.logomitpass) |
| 788 | pkt->logmode = blanktype; |
| 789 | } |
| 790 | |
| 791 | static void dont_log_data(Ssh ssh, struct Packet *pkt, int blanktype) |
| 792 | { |
| 793 | if (ssh->cfg.logomitdata) |
| 794 | pkt->logmode = blanktype; |
| 795 | } |
| 796 | |
| 797 | static void end_log_omission(Ssh ssh, struct Packet *pkt) |
| 798 | { |
| 799 | pkt->logmode = PKTLOG_EMIT; |
| 800 | } |
| 801 | |
| 802 | static int ssh_channelcmp(void *av, void *bv) |
| 803 | { |
| 804 | struct ssh_channel *a = (struct ssh_channel *) av; |
| 805 | struct ssh_channel *b = (struct ssh_channel *) bv; |
| 806 | if (a->localid < b->localid) |
| 807 | return -1; |
| 808 | if (a->localid > b->localid) |
| 809 | return +1; |
| 810 | return 0; |
| 811 | } |
| 812 | static int ssh_channelfind(void *av, void *bv) |
| 813 | { |
| 814 | unsigned *a = (unsigned *) av; |
| 815 | struct ssh_channel *b = (struct ssh_channel *) bv; |
| 816 | if (*a < b->localid) |
| 817 | return -1; |
| 818 | if (*a > b->localid) |
| 819 | return +1; |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int ssh_rportcmp_ssh1(void *av, void *bv) |
| 824 | { |
| 825 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
| 826 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
| 827 | int i; |
| 828 | if ( (i = strcmp(a->dhost, b->dhost)) != 0) |
| 829 | return i < 0 ? -1 : +1; |
| 830 | if (a->dport > b->dport) |
| 831 | return +1; |
| 832 | if (a->dport < b->dport) |
| 833 | return -1; |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | static int ssh_rportcmp_ssh2(void *av, void *bv) |
| 838 | { |
| 839 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
| 840 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
| 841 | |
| 842 | if (a->sport > b->sport) |
| 843 | return +1; |
| 844 | if (a->sport < b->sport) |
| 845 | return -1; |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Special form of strcmp which can cope with NULL inputs. NULL is |
| 851 | * defined to sort before even the empty string. |
| 852 | */ |
| 853 | static int nullstrcmp(const char *a, const char *b) |
| 854 | { |
| 855 | if (a == NULL && b == NULL) |
| 856 | return 0; |
| 857 | if (a == NULL) |
| 858 | return -1; |
| 859 | if (b == NULL) |
| 860 | return +1; |
| 861 | return strcmp(a, b); |
| 862 | } |
| 863 | |
| 864 | static int ssh_portcmp(void *av, void *bv) |
| 865 | { |
| 866 | struct ssh_portfwd *a = (struct ssh_portfwd *) av; |
| 867 | struct ssh_portfwd *b = (struct ssh_portfwd *) bv; |
| 868 | int i; |
| 869 | if (a->type > b->type) |
| 870 | return +1; |
| 871 | if (a->type < b->type) |
| 872 | return -1; |
| 873 | if (a->addressfamily > b->addressfamily) |
| 874 | return +1; |
| 875 | if (a->addressfamily < b->addressfamily) |
| 876 | return -1; |
| 877 | if ( (i = nullstrcmp(a->saddr, b->saddr)) != 0) |
| 878 | return i < 0 ? -1 : +1; |
| 879 | if (a->sport > b->sport) |
| 880 | return +1; |
| 881 | if (a->sport < b->sport) |
| 882 | return -1; |
| 883 | if (a->type != 'D') { |
| 884 | if ( (i = nullstrcmp(a->daddr, b->daddr)) != 0) |
| 885 | return i < 0 ? -1 : +1; |
| 886 | if (a->dport > b->dport) |
| 887 | return +1; |
| 888 | if (a->dport < b->dport) |
| 889 | return -1; |
| 890 | } |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static int alloc_channel_id(Ssh ssh) |
| 895 | { |
| 896 | const unsigned CHANNEL_NUMBER_OFFSET = 256; |
| 897 | unsigned low, high, mid; |
| 898 | int tsize; |
| 899 | struct ssh_channel *c; |
| 900 | |
| 901 | /* |
| 902 | * First-fit allocation of channel numbers: always pick the |
| 903 | * lowest unused one. To do this, binary-search using the |
| 904 | * counted B-tree to find the largest channel ID which is in a |
| 905 | * contiguous sequence from the beginning. (Precisely |
| 906 | * everything in that sequence must have ID equal to its tree |
| 907 | * index plus CHANNEL_NUMBER_OFFSET.) |
| 908 | */ |
| 909 | tsize = count234(ssh->channels); |
| 910 | |
| 911 | low = -1; |
| 912 | high = tsize; |
| 913 | while (high - low > 1) { |
| 914 | mid = (high + low) / 2; |
| 915 | c = index234(ssh->channels, mid); |
| 916 | if (c->localid == mid + CHANNEL_NUMBER_OFFSET) |
| 917 | low = mid; /* this one is fine */ |
| 918 | else |
| 919 | high = mid; /* this one is past it */ |
| 920 | } |
| 921 | /* |
| 922 | * Now low points to either -1, or the tree index of the |
| 923 | * largest ID in the initial sequence. |
| 924 | */ |
| 925 | { |
| 926 | unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET; |
| 927 | assert(NULL == find234(ssh->channels, &i, ssh_channelfind)); |
| 928 | } |
| 929 | return low + 1 + CHANNEL_NUMBER_OFFSET; |
| 930 | } |
| 931 | |
| 932 | static void c_write(Ssh ssh, const char *buf, int len) |
| 933 | { |
| 934 | if ((flags & FLAG_STDERR)) { |
| 935 | int i; |
| 936 | for (i = 0; i < len; i++) |
| 937 | if (buf[i] != '\r') |
| 938 | fputc(buf[i], stderr); |
| 939 | return; |
| 940 | } |
| 941 | from_backend(ssh->frontend, 1, buf, len); |
| 942 | } |
| 943 | |
| 944 | static void c_write_untrusted(Ssh ssh, const char *buf, int len) |
| 945 | { |
| 946 | int i; |
| 947 | for (i = 0; i < len; i++) { |
| 948 | if (buf[i] == '\n') |
| 949 | c_write(ssh, "\r\n", 2); |
| 950 | else if ((buf[i] & 0x60) || (buf[i] == '\r')) |
| 951 | c_write(ssh, buf + i, 1); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | static void c_write_str(Ssh ssh, const char *buf) |
| 956 | { |
| 957 | c_write(ssh, buf, strlen(buf)); |
| 958 | } |
| 959 | |
| 960 | static void ssh_free_packet(struct Packet *pkt) |
| 961 | { |
| 962 | sfree(pkt->data); |
| 963 | sfree(pkt); |
| 964 | } |
| 965 | static struct Packet *ssh_new_packet(void) |
| 966 | { |
| 967 | struct Packet *pkt = snew(struct Packet); |
| 968 | |
| 969 | pkt->data = NULL; |
| 970 | pkt->maxlen = 0; |
| 971 | pkt->logmode = PKTLOG_EMIT; |
| 972 | pkt->nblanks = 0; |
| 973 | pkt->blanks = NULL; |
| 974 | |
| 975 | return pkt; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Collect incoming data in the incoming packet buffer. |
| 980 | * Decipher and verify the packet when it is completely read. |
| 981 | * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets. |
| 982 | * Update the *data and *datalen variables. |
| 983 | * Return a Packet structure when a packet is completed. |
| 984 | */ |
| 985 | static struct Packet *ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
| 986 | { |
| 987 | struct rdpkt1_state_tag *st = &ssh->rdpkt1_state; |
| 988 | |
| 989 | crBegin(ssh->ssh1_rdpkt_crstate); |
| 990 | |
| 991 | st->pktin = ssh_new_packet(); |
| 992 | |
| 993 | st->pktin->type = 0; |
| 994 | st->pktin->length = 0; |
| 995 | |
| 996 | for (st->i = st->len = 0; st->i < 4; st->i++) { |
| 997 | while ((*datalen) == 0) |
| 998 | crReturn(NULL); |
| 999 | st->len = (st->len << 8) + **data; |
| 1000 | (*data)++, (*datalen)--; |
| 1001 | } |
| 1002 | |
| 1003 | st->pad = 8 - (st->len % 8); |
| 1004 | st->biglen = st->len + st->pad; |
| 1005 | st->pktin->length = st->len - 5; |
| 1006 | |
| 1007 | if (st->biglen < 0) { |
| 1008 | bombout(("Extremely large packet length from server suggests" |
| 1009 | " data stream corruption")); |
| 1010 | ssh_free_packet(st->pktin); |
| 1011 | crStop(NULL); |
| 1012 | } |
| 1013 | |
| 1014 | st->pktin->maxlen = st->biglen; |
| 1015 | st->pktin->data = snewn(st->biglen + APIEXTRA, unsigned char); |
| 1016 | |
| 1017 | st->to_read = st->biglen; |
| 1018 | st->p = st->pktin->data; |
| 1019 | while (st->to_read > 0) { |
| 1020 | st->chunk = st->to_read; |
| 1021 | while ((*datalen) == 0) |
| 1022 | crReturn(NULL); |
| 1023 | if (st->chunk > (*datalen)) |
| 1024 | st->chunk = (*datalen); |
| 1025 | memcpy(st->p, *data, st->chunk); |
| 1026 | *data += st->chunk; |
| 1027 | *datalen -= st->chunk; |
| 1028 | st->p += st->chunk; |
| 1029 | st->to_read -= st->chunk; |
| 1030 | } |
| 1031 | |
| 1032 | if (ssh->cipher && detect_attack(ssh->crcda_ctx, st->pktin->data, |
| 1033 | st->biglen, NULL)) { |
| 1034 | bombout(("Network attack (CRC compensation) detected!")); |
| 1035 | ssh_free_packet(st->pktin); |
| 1036 | crStop(NULL); |
| 1037 | } |
| 1038 | |
| 1039 | if (ssh->cipher) |
| 1040 | ssh->cipher->decrypt(ssh->v1_cipher_ctx, st->pktin->data, st->biglen); |
| 1041 | |
| 1042 | st->realcrc = crc32_compute(st->pktin->data, st->biglen - 4); |
| 1043 | st->gotcrc = GET_32BIT(st->pktin->data + st->biglen - 4); |
| 1044 | if (st->gotcrc != st->realcrc) { |
| 1045 | bombout(("Incorrect CRC received on packet")); |
| 1046 | ssh_free_packet(st->pktin); |
| 1047 | crStop(NULL); |
| 1048 | } |
| 1049 | |
| 1050 | st->pktin->body = st->pktin->data + st->pad + 1; |
| 1051 | st->pktin->savedpos = 0; |
| 1052 | |
| 1053 | if (ssh->v1_compressing) { |
| 1054 | unsigned char *decompblk; |
| 1055 | int decomplen; |
| 1056 | if (!zlib_decompress_block(ssh->sc_comp_ctx, |
| 1057 | st->pktin->body - 1, st->pktin->length + 1, |
| 1058 | &decompblk, &decomplen)) { |
| 1059 | bombout(("Zlib decompression encountered invalid data")); |
| 1060 | ssh_free_packet(st->pktin); |
| 1061 | crStop(NULL); |
| 1062 | } |
| 1063 | |
| 1064 | if (st->pktin->maxlen < st->pad + decomplen) { |
| 1065 | st->pktin->maxlen = st->pad + decomplen; |
| 1066 | st->pktin->data = sresize(st->pktin->data, |
| 1067 | st->pktin->maxlen + APIEXTRA, |
| 1068 | unsigned char); |
| 1069 | st->pktin->body = st->pktin->data + st->pad + 1; |
| 1070 | } |
| 1071 | |
| 1072 | memcpy(st->pktin->body - 1, decompblk, decomplen); |
| 1073 | sfree(decompblk); |
| 1074 | st->pktin->length = decomplen - 1; |
| 1075 | } |
| 1076 | |
| 1077 | st->pktin->type = st->pktin->body[-1]; |
| 1078 | |
| 1079 | /* |
| 1080 | * Log incoming packet, possibly omitting sensitive fields. |
| 1081 | */ |
| 1082 | if (ssh->logctx) { |
| 1083 | int nblanks = 0; |
| 1084 | struct logblank_t blank; |
| 1085 | if (ssh->cfg.logomitdata) { |
| 1086 | int do_blank = FALSE, blank_prefix = 0; |
| 1087 | /* "Session data" packets - omit the data field */ |
| 1088 | if ((st->pktin->type == SSH1_SMSG_STDOUT_DATA) || |
| 1089 | (st->pktin->type == SSH1_SMSG_STDERR_DATA)) { |
| 1090 | do_blank = TRUE; blank_prefix = 0; |
| 1091 | } else if (st->pktin->type == SSH1_MSG_CHANNEL_DATA) { |
| 1092 | do_blank = TRUE; blank_prefix = 4; |
| 1093 | } |
| 1094 | if (do_blank) { |
| 1095 | blank.offset = blank_prefix; |
| 1096 | blank.len = st->pktin->length; |
| 1097 | blank.type = PKTLOG_OMIT; |
| 1098 | nblanks = 1; |
| 1099 | } |
| 1100 | } |
| 1101 | log_packet(ssh->logctx, |
| 1102 | PKT_INCOMING, st->pktin->type, |
| 1103 | ssh1_pkt_type(st->pktin->type), |
| 1104 | st->pktin->body, st->pktin->length, |
| 1105 | nblanks, &blank); |
| 1106 | } |
| 1107 | |
| 1108 | crFinish(st->pktin); |
| 1109 | } |
| 1110 | |
| 1111 | static struct Packet *ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
| 1112 | { |
| 1113 | struct rdpkt2_state_tag *st = &ssh->rdpkt2_state; |
| 1114 | |
| 1115 | crBegin(ssh->ssh2_rdpkt_crstate); |
| 1116 | |
| 1117 | st->pktin = ssh_new_packet(); |
| 1118 | |
| 1119 | st->pktin->type = 0; |
| 1120 | st->pktin->length = 0; |
| 1121 | if (ssh->sccipher) |
| 1122 | st->cipherblk = ssh->sccipher->blksize; |
| 1123 | else |
| 1124 | st->cipherblk = 8; |
| 1125 | if (st->cipherblk < 8) |
| 1126 | st->cipherblk = 8; |
| 1127 | |
| 1128 | st->pktin->data = snewn(st->cipherblk + APIEXTRA, unsigned char); |
| 1129 | |
| 1130 | /* |
| 1131 | * Acquire and decrypt the first block of the packet. This will |
| 1132 | * contain the length and padding details. |
| 1133 | */ |
| 1134 | for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { |
| 1135 | while ((*datalen) == 0) |
| 1136 | crReturn(NULL); |
| 1137 | st->pktin->data[st->i] = *(*data)++; |
| 1138 | (*datalen)--; |
| 1139 | } |
| 1140 | |
| 1141 | if (ssh->sccipher) |
| 1142 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
| 1143 | st->pktin->data, st->cipherblk); |
| 1144 | |
| 1145 | /* |
| 1146 | * Now get the length and padding figures. |
| 1147 | */ |
| 1148 | st->len = GET_32BIT(st->pktin->data); |
| 1149 | st->pad = st->pktin->data[4]; |
| 1150 | |
| 1151 | /* |
| 1152 | * _Completely_ silly lengths should be stomped on before they |
| 1153 | * do us any more damage. |
| 1154 | */ |
| 1155 | if (st->len < 0 || st->pad < 0 || st->len + st->pad < 0) { |
| 1156 | bombout(("Incoming packet was garbled on decryption")); |
| 1157 | ssh_free_packet(st->pktin); |
| 1158 | crStop(NULL); |
| 1159 | } |
| 1160 | |
| 1161 | /* |
| 1162 | * This enables us to deduce the payload length. |
| 1163 | */ |
| 1164 | st->payload = st->len - st->pad - 1; |
| 1165 | |
| 1166 | st->pktin->length = st->payload + 5; |
| 1167 | |
| 1168 | /* |
| 1169 | * So now we can work out the total packet length. |
| 1170 | */ |
| 1171 | st->packetlen = st->len + 4; |
| 1172 | st->maclen = ssh->scmac ? ssh->scmac->len : 0; |
| 1173 | |
| 1174 | /* |
| 1175 | * Allocate memory for the rest of the packet. |
| 1176 | */ |
| 1177 | st->pktin->maxlen = st->packetlen + st->maclen; |
| 1178 | st->pktin->data = sresize(st->pktin->data, |
| 1179 | st->pktin->maxlen + APIEXTRA, |
| 1180 | unsigned char); |
| 1181 | |
| 1182 | /* |
| 1183 | * Read and decrypt the remainder of the packet. |
| 1184 | */ |
| 1185 | for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; |
| 1186 | st->i++) { |
| 1187 | while ((*datalen) == 0) |
| 1188 | crReturn(NULL); |
| 1189 | st->pktin->data[st->i] = *(*data)++; |
| 1190 | (*datalen)--; |
| 1191 | } |
| 1192 | /* Decrypt everything _except_ the MAC. */ |
| 1193 | if (ssh->sccipher) |
| 1194 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
| 1195 | st->pktin->data + st->cipherblk, |
| 1196 | st->packetlen - st->cipherblk); |
| 1197 | |
| 1198 | st->pktin->encrypted_len = st->packetlen; |
| 1199 | |
| 1200 | /* |
| 1201 | * Check the MAC. |
| 1202 | */ |
| 1203 | if (ssh->scmac |
| 1204 | && !ssh->scmac->verify(ssh->sc_mac_ctx, st->pktin->data, st->len + 4, |
| 1205 | st->incoming_sequence)) { |
| 1206 | bombout(("Incorrect MAC received on packet")); |
| 1207 | ssh_free_packet(st->pktin); |
| 1208 | crStop(NULL); |
| 1209 | } |
| 1210 | |
| 1211 | st->pktin->sequence = st->incoming_sequence++; |
| 1212 | |
| 1213 | /* |
| 1214 | * Decompress packet payload. |
| 1215 | */ |
| 1216 | { |
| 1217 | unsigned char *newpayload; |
| 1218 | int newlen; |
| 1219 | if (ssh->sccomp && |
| 1220 | ssh->sccomp->decompress(ssh->sc_comp_ctx, |
| 1221 | st->pktin->data + 5, st->pktin->length - 5, |
| 1222 | &newpayload, &newlen)) { |
| 1223 | if (st->pktin->maxlen < newlen + 5) { |
| 1224 | st->pktin->maxlen = newlen + 5; |
| 1225 | st->pktin->data = sresize(st->pktin->data, |
| 1226 | st->pktin->maxlen + APIEXTRA, |
| 1227 | unsigned char); |
| 1228 | } |
| 1229 | st->pktin->length = 5 + newlen; |
| 1230 | memcpy(st->pktin->data + 5, newpayload, newlen); |
| 1231 | sfree(newpayload); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | st->pktin->savedpos = 6; |
| 1236 | st->pktin->body = st->pktin->data; |
| 1237 | st->pktin->type = st->pktin->data[5]; |
| 1238 | |
| 1239 | /* |
| 1240 | * Log incoming packet, possibly omitting sensitive fields. |
| 1241 | */ |
| 1242 | if (ssh->logctx) { |
| 1243 | int nblanks = 0; |
| 1244 | struct logblank_t blank; |
| 1245 | if (ssh->cfg.logomitdata) { |
| 1246 | int do_blank = FALSE, blank_prefix = 0; |
| 1247 | /* "Session data" packets - omit the data field */ |
| 1248 | if (st->pktin->type == SSH2_MSG_CHANNEL_DATA) { |
| 1249 | do_blank = TRUE; blank_prefix = 4; |
| 1250 | } else if (st->pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
| 1251 | do_blank = TRUE; blank_prefix = 8; |
| 1252 | } |
| 1253 | if (do_blank) { |
| 1254 | blank.offset = blank_prefix; |
| 1255 | blank.len = (st->pktin->length-6) - blank_prefix; |
| 1256 | blank.type = PKTLOG_OMIT; |
| 1257 | nblanks = 1; |
| 1258 | } |
| 1259 | } |
| 1260 | log_packet(ssh->logctx, PKT_INCOMING, st->pktin->type, |
| 1261 | ssh2_pkt_type(ssh->pkt_ctx, st->pktin->type), |
| 1262 | st->pktin->data+6, st->pktin->length-6, |
| 1263 | nblanks, &blank); |
| 1264 | } |
| 1265 | |
| 1266 | crFinish(st->pktin); |
| 1267 | } |
| 1268 | |
| 1269 | static void ssh1_pktout_size(struct Packet *pkt, int len) |
| 1270 | { |
| 1271 | int pad, biglen; |
| 1272 | |
| 1273 | len += 5; /* type and CRC */ |
| 1274 | pad = 8 - (len % 8); |
| 1275 | biglen = len + pad; |
| 1276 | |
| 1277 | pkt->length = len - 5; |
| 1278 | if (pkt->maxlen < biglen) { |
| 1279 | pkt->maxlen = biglen; |
| 1280 | pkt->data = sresize(pkt->data, biglen + 4 + APIEXTRA, unsigned char); |
| 1281 | } |
| 1282 | pkt->body = pkt->data + 4 + pad + 1; |
| 1283 | } |
| 1284 | |
| 1285 | static struct Packet *s_wrpkt_start(int type, int len) |
| 1286 | { |
| 1287 | struct Packet *pkt = ssh_new_packet(); |
| 1288 | ssh1_pktout_size(pkt, len); |
| 1289 | pkt->type = type; |
| 1290 | /* Initialise log omission state */ |
| 1291 | pkt->nblanks = 0; |
| 1292 | pkt->blanks = NULL; |
| 1293 | return pkt; |
| 1294 | } |
| 1295 | |
| 1296 | static int s_wrpkt_prepare(Ssh ssh, struct Packet *pkt) |
| 1297 | { |
| 1298 | int pad, biglen, i; |
| 1299 | unsigned long crc; |
| 1300 | #ifdef __SC__ |
| 1301 | /* |
| 1302 | * XXX various versions of SC (including 8.8.4) screw up the |
| 1303 | * register allocation in this function and use the same register |
| 1304 | * (D6) for len and as a temporary, with predictable results. The |
| 1305 | * following sledgehammer prevents this. |
| 1306 | */ |
| 1307 | volatile |
| 1308 | #endif |
| 1309 | int len; |
| 1310 | |
| 1311 | pkt->body[-1] = pkt->type; |
| 1312 | |
| 1313 | if (ssh->logctx) |
| 1314 | log_packet(ssh->logctx, PKT_OUTGOING, pkt->type, |
| 1315 | ssh1_pkt_type(pkt->type), |
| 1316 | pkt->body, pkt->length, |
| 1317 | pkt->nblanks, pkt->blanks); |
| 1318 | sfree(pkt->blanks); pkt->blanks = NULL; |
| 1319 | pkt->nblanks = 0; |
| 1320 | |
| 1321 | if (ssh->v1_compressing) { |
| 1322 | unsigned char *compblk; |
| 1323 | int complen; |
| 1324 | zlib_compress_block(ssh->cs_comp_ctx, |
| 1325 | pkt->body - 1, pkt->length + 1, |
| 1326 | &compblk, &complen); |
| 1327 | ssh1_pktout_size(pkt, complen - 1); |
| 1328 | memcpy(pkt->body - 1, compblk, complen); |
| 1329 | sfree(compblk); |
| 1330 | } |
| 1331 | |
| 1332 | len = pkt->length + 5; /* type and CRC */ |
| 1333 | pad = 8 - (len % 8); |
| 1334 | biglen = len + pad; |
| 1335 | |
| 1336 | for (i = 0; i < pad; i++) |
| 1337 | pkt->data[i + 4] = random_byte(); |
| 1338 | crc = crc32_compute(pkt->data + 4, biglen - 4); |
| 1339 | PUT_32BIT(pkt->data + biglen, crc); |
| 1340 | PUT_32BIT(pkt->data, len); |
| 1341 | |
| 1342 | if (ssh->cipher) |
| 1343 | ssh->cipher->encrypt(ssh->v1_cipher_ctx, pkt->data + 4, biglen); |
| 1344 | |
| 1345 | return biglen + 4; |
| 1346 | } |
| 1347 | |
| 1348 | static void s_wrpkt(Ssh ssh, struct Packet *pkt) |
| 1349 | { |
| 1350 | int len, backlog; |
| 1351 | len = s_wrpkt_prepare(ssh, pkt); |
| 1352 | backlog = sk_write(ssh->s, (char *)pkt->data, len); |
| 1353 | if (backlog > SSH_MAX_BACKLOG) |
| 1354 | ssh_throttle_all(ssh, 1, backlog); |
| 1355 | } |
| 1356 | |
| 1357 | static void s_wrpkt_defer(Ssh ssh, struct Packet *pkt) |
| 1358 | { |
| 1359 | int len; |
| 1360 | len = s_wrpkt_prepare(ssh, pkt); |
| 1361 | if (ssh->deferred_len + len > ssh->deferred_size) { |
| 1362 | ssh->deferred_size = ssh->deferred_len + len + 128; |
| 1363 | ssh->deferred_send_data = sresize(ssh->deferred_send_data, |
| 1364 | ssh->deferred_size, |
| 1365 | unsigned char); |
| 1366 | } |
| 1367 | memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->data, len); |
| 1368 | ssh->deferred_len += len; |
| 1369 | } |
| 1370 | |
| 1371 | /* |
| 1372 | * Construct a packet with the specified contents. |
| 1373 | */ |
| 1374 | static struct Packet *construct_packet(Ssh ssh, int pkttype, |
| 1375 | va_list ap1, va_list ap2) |
| 1376 | { |
| 1377 | unsigned char *p, *argp, argchar; |
| 1378 | unsigned long argint; |
| 1379 | int pktlen, argtype, arglen; |
| 1380 | Bignum bn; |
| 1381 | struct Packet *pkt; |
| 1382 | |
| 1383 | pktlen = 0; |
| 1384 | while ((argtype = va_arg(ap1, int)) != PKT_END) { |
| 1385 | switch (argtype) { |
| 1386 | case PKT_INT: |
| 1387 | (void) va_arg(ap1, int); |
| 1388 | pktlen += 4; |
| 1389 | break; |
| 1390 | case PKT_CHAR: |
| 1391 | (void) va_arg(ap1, int); |
| 1392 | pktlen++; |
| 1393 | break; |
| 1394 | case PKT_DATA: |
| 1395 | (void) va_arg(ap1, unsigned char *); |
| 1396 | arglen = va_arg(ap1, int); |
| 1397 | pktlen += arglen; |
| 1398 | break; |
| 1399 | case PKT_STR: |
| 1400 | argp = va_arg(ap1, unsigned char *); |
| 1401 | arglen = strlen((char *)argp); |
| 1402 | pktlen += 4 + arglen; |
| 1403 | break; |
| 1404 | case PKT_BIGNUM: |
| 1405 | bn = va_arg(ap1, Bignum); |
| 1406 | pktlen += ssh1_bignum_length(bn); |
| 1407 | break; |
| 1408 | case PKTT_PASSWORD: |
| 1409 | case PKTT_DATA: |
| 1410 | case PKTT_OTHER: |
| 1411 | /* ignore this pass */ |
| 1412 | break; |
| 1413 | default: |
| 1414 | assert(0); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | pkt = s_wrpkt_start(pkttype, pktlen); |
| 1419 | p = pkt->body; |
| 1420 | |
| 1421 | while ((argtype = va_arg(ap2, int)) != PKT_END) { |
| 1422 | int offset = p - pkt->body, len = 0; |
| 1423 | switch (argtype) { |
| 1424 | /* Actual fields in the packet */ |
| 1425 | case PKT_INT: |
| 1426 | argint = va_arg(ap2, int); |
| 1427 | PUT_32BIT(p, argint); |
| 1428 | len = 4; |
| 1429 | break; |
| 1430 | case PKT_CHAR: |
| 1431 | argchar = (unsigned char) va_arg(ap2, int); |
| 1432 | *p = argchar; |
| 1433 | len = 1; |
| 1434 | break; |
| 1435 | case PKT_DATA: |
| 1436 | argp = va_arg(ap2, unsigned char *); |
| 1437 | arglen = va_arg(ap2, int); |
| 1438 | memcpy(p, argp, arglen); |
| 1439 | len = arglen; |
| 1440 | break; |
| 1441 | case PKT_STR: |
| 1442 | argp = va_arg(ap2, unsigned char *); |
| 1443 | arglen = strlen((char *)argp); |
| 1444 | PUT_32BIT(p, arglen); |
| 1445 | memcpy(p + 4, argp, arglen); |
| 1446 | len = arglen + 4; |
| 1447 | break; |
| 1448 | case PKT_BIGNUM: |
| 1449 | bn = va_arg(ap2, Bignum); |
| 1450 | len = ssh1_write_bignum(p, bn); |
| 1451 | break; |
| 1452 | /* Tokens for modifications to packet logging */ |
| 1453 | case PKTT_PASSWORD: |
| 1454 | dont_log_password(ssh, pkt, PKTLOG_BLANK); |
| 1455 | break; |
| 1456 | case PKTT_DATA: |
| 1457 | dont_log_data(ssh, pkt, PKTLOG_OMIT); |
| 1458 | break; |
| 1459 | case PKTT_OTHER: |
| 1460 | end_log_omission(ssh, pkt); |
| 1461 | break; |
| 1462 | } |
| 1463 | p += len; |
| 1464 | /* Deal with logfile omission, if required. */ |
| 1465 | if (len && (pkt->logmode != PKTLOG_EMIT)) { |
| 1466 | pkt->nblanks++; |
| 1467 | pkt->blanks = sresize(pkt->blanks, pkt->nblanks, |
| 1468 | struct logblank_t); |
| 1469 | pkt->blanks[pkt->nblanks-1].offset = offset; |
| 1470 | pkt->blanks[pkt->nblanks-1].len = len; |
| 1471 | pkt->blanks[pkt->nblanks-1].type = pkt->logmode; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | return pkt; |
| 1476 | } |
| 1477 | |
| 1478 | static void send_packet(Ssh ssh, int pkttype, ...) |
| 1479 | { |
| 1480 | struct Packet *pkt; |
| 1481 | va_list ap1, ap2; |
| 1482 | va_start(ap1, pkttype); |
| 1483 | va_start(ap2, pkttype); |
| 1484 | pkt = construct_packet(ssh, pkttype, ap1, ap2); |
| 1485 | va_end(ap2); |
| 1486 | va_end(ap1); |
| 1487 | s_wrpkt(ssh, pkt); |
| 1488 | ssh_free_packet(pkt); |
| 1489 | } |
| 1490 | |
| 1491 | static void defer_packet(Ssh ssh, int pkttype, ...) |
| 1492 | { |
| 1493 | struct Packet *pkt; |
| 1494 | va_list ap1, ap2; |
| 1495 | va_start(ap1, pkttype); |
| 1496 | va_start(ap2, pkttype); |
| 1497 | pkt = construct_packet(ssh, pkttype, ap1, ap2); |
| 1498 | va_end(ap2); |
| 1499 | va_end(ap1); |
| 1500 | s_wrpkt_defer(ssh, pkt); |
| 1501 | ssh_free_packet(pkt); |
| 1502 | } |
| 1503 | |
| 1504 | static int ssh_versioncmp(char *a, char *b) |
| 1505 | { |
| 1506 | char *ae, *be; |
| 1507 | unsigned long av, bv; |
| 1508 | |
| 1509 | av = strtoul(a, &ae, 10); |
| 1510 | bv = strtoul(b, &be, 10); |
| 1511 | if (av != bv) |
| 1512 | return (av < bv ? -1 : +1); |
| 1513 | if (*ae == '.') |
| 1514 | ae++; |
| 1515 | if (*be == '.') |
| 1516 | be++; |
| 1517 | av = strtoul(ae, &ae, 10); |
| 1518 | bv = strtoul(be, &be, 10); |
| 1519 | if (av != bv) |
| 1520 | return (av < bv ? -1 : +1); |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * Utility routines for putting an SSH-protocol `string' and |
| 1526 | * `uint32' into a SHA state. |
| 1527 | */ |
| 1528 | #include <stdio.h> |
| 1529 | static void sha_string(SHA_State * s, void *str, int len) |
| 1530 | { |
| 1531 | unsigned char lenblk[4]; |
| 1532 | PUT_32BIT(lenblk, len); |
| 1533 | SHA_Bytes(s, lenblk, 4); |
| 1534 | SHA_Bytes(s, str, len); |
| 1535 | } |
| 1536 | |
| 1537 | static void sha_uint32(SHA_State * s, unsigned i) |
| 1538 | { |
| 1539 | unsigned char intblk[4]; |
| 1540 | PUT_32BIT(intblk, i); |
| 1541 | SHA_Bytes(s, intblk, 4); |
| 1542 | } |
| 1543 | |
| 1544 | /* |
| 1545 | * SSH2 packet construction functions. |
| 1546 | */ |
| 1547 | static void ssh2_pkt_ensure(struct Packet *pkt, int length) |
| 1548 | { |
| 1549 | if (pkt->maxlen < length) { |
| 1550 | pkt->maxlen = length + 256; |
| 1551 | pkt->data = sresize(pkt->data, pkt->maxlen + APIEXTRA, unsigned char); |
| 1552 | } |
| 1553 | } |
| 1554 | static void ssh2_pkt_adddata(struct Packet *pkt, void *data, int len) |
| 1555 | { |
| 1556 | if (pkt->logmode != PKTLOG_EMIT) { |
| 1557 | pkt->nblanks++; |
| 1558 | pkt->blanks = sresize(pkt->blanks, pkt->nblanks, struct logblank_t); |
| 1559 | pkt->blanks[pkt->nblanks-1].offset = pkt->length - 6; |
| 1560 | pkt->blanks[pkt->nblanks-1].len = len; |
| 1561 | pkt->blanks[pkt->nblanks-1].type = pkt->logmode; |
| 1562 | } |
| 1563 | pkt->length += len; |
| 1564 | ssh2_pkt_ensure(pkt, pkt->length); |
| 1565 | memcpy(pkt->data + pkt->length - len, data, len); |
| 1566 | } |
| 1567 | static void ssh2_pkt_addbyte(struct Packet *pkt, unsigned char byte) |
| 1568 | { |
| 1569 | ssh2_pkt_adddata(pkt, &byte, 1); |
| 1570 | } |
| 1571 | static struct Packet *ssh2_pkt_init(int pkt_type) |
| 1572 | { |
| 1573 | struct Packet *pkt = ssh_new_packet(); |
| 1574 | pkt->length = 5; |
| 1575 | ssh2_pkt_addbyte(pkt, (unsigned char) pkt_type); |
| 1576 | return pkt; |
| 1577 | } |
| 1578 | static void ssh2_pkt_addbool(struct Packet *pkt, unsigned char value) |
| 1579 | { |
| 1580 | ssh2_pkt_adddata(pkt, &value, 1); |
| 1581 | } |
| 1582 | static void ssh2_pkt_adduint32(struct Packet *pkt, unsigned long value) |
| 1583 | { |
| 1584 | unsigned char x[4]; |
| 1585 | PUT_32BIT(x, value); |
| 1586 | ssh2_pkt_adddata(pkt, x, 4); |
| 1587 | } |
| 1588 | static void ssh2_pkt_addstring_start(struct Packet *pkt) |
| 1589 | { |
| 1590 | ssh2_pkt_adduint32(pkt, 0); |
| 1591 | pkt->savedpos = pkt->length; |
| 1592 | } |
| 1593 | static void ssh2_pkt_addstring_str(struct Packet *pkt, char *data) |
| 1594 | { |
| 1595 | ssh2_pkt_adddata(pkt, data, strlen(data)); |
| 1596 | PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos); |
| 1597 | } |
| 1598 | static void ssh2_pkt_addstring_data(struct Packet *pkt, char *data, int len) |
| 1599 | { |
| 1600 | ssh2_pkt_adddata(pkt, data, len); |
| 1601 | PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos); |
| 1602 | } |
| 1603 | static void ssh2_pkt_addstring(struct Packet *pkt, char *data) |
| 1604 | { |
| 1605 | ssh2_pkt_addstring_start(pkt); |
| 1606 | ssh2_pkt_addstring_str(pkt, data); |
| 1607 | } |
| 1608 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len) |
| 1609 | { |
| 1610 | unsigned char *p; |
| 1611 | int i, n = (bignum_bitcount(b) + 7) / 8; |
| 1612 | p = snewn(n + 1, unsigned char); |
| 1613 | if (!p) |
| 1614 | fatalbox("out of memory"); |
| 1615 | p[0] = 0; |
| 1616 | for (i = 1; i <= n; i++) |
| 1617 | p[i] = bignum_byte(b, n - i); |
| 1618 | i = 0; |
| 1619 | while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0) |
| 1620 | i++; |
| 1621 | memmove(p, p + i, n + 1 - i); |
| 1622 | *len = n + 1 - i; |
| 1623 | return p; |
| 1624 | } |
| 1625 | static void ssh2_pkt_addmp(struct Packet *pkt, Bignum b) |
| 1626 | { |
| 1627 | unsigned char *p; |
| 1628 | int len; |
| 1629 | p = ssh2_mpint_fmt(b, &len); |
| 1630 | ssh2_pkt_addstring_start(pkt); |
| 1631 | ssh2_pkt_addstring_data(pkt, (char *)p, len); |
| 1632 | sfree(p); |
| 1633 | } |
| 1634 | |
| 1635 | /* |
| 1636 | * Construct an SSH2 final-form packet: compress it, encrypt it, |
| 1637 | * put the MAC on it. Final packet, ready to be sent, is stored in |
| 1638 | * pkt->data. Total length is returned. |
| 1639 | */ |
| 1640 | static int ssh2_pkt_construct(Ssh ssh, struct Packet *pkt) |
| 1641 | { |
| 1642 | int cipherblk, maclen, padding, i; |
| 1643 | |
| 1644 | if (ssh->logctx) |
| 1645 | log_packet(ssh->logctx, PKT_OUTGOING, pkt->data[5], |
| 1646 | ssh2_pkt_type(ssh->pkt_ctx, pkt->data[5]), |
| 1647 | pkt->data + 6, pkt->length - 6, |
| 1648 | pkt->nblanks, pkt->blanks); |
| 1649 | sfree(pkt->blanks); pkt->blanks = NULL; |
| 1650 | pkt->nblanks = 0; |
| 1651 | |
| 1652 | /* |
| 1653 | * Compress packet payload. |
| 1654 | */ |
| 1655 | { |
| 1656 | unsigned char *newpayload; |
| 1657 | int newlen; |
| 1658 | if (ssh->cscomp && |
| 1659 | ssh->cscomp->compress(ssh->cs_comp_ctx, pkt->data + 5, |
| 1660 | pkt->length - 5, |
| 1661 | &newpayload, &newlen)) { |
| 1662 | pkt->length = 5; |
| 1663 | ssh2_pkt_adddata(pkt, newpayload, newlen); |
| 1664 | sfree(newpayload); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | /* |
| 1669 | * Add padding. At least four bytes, and must also bring total |
| 1670 | * length (minus MAC) up to a multiple of the block size. |
| 1671 | */ |
| 1672 | cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */ |
| 1673 | cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ |
| 1674 | padding = 4; |
| 1675 | padding += |
| 1676 | (cipherblk - (pkt->length + padding) % cipherblk) % cipherblk; |
| 1677 | maclen = ssh->csmac ? ssh->csmac->len : 0; |
| 1678 | ssh2_pkt_ensure(pkt, pkt->length + padding + maclen); |
| 1679 | pkt->data[4] = padding; |
| 1680 | for (i = 0; i < padding; i++) |
| 1681 | pkt->data[pkt->length + i] = random_byte(); |
| 1682 | PUT_32BIT(pkt->data, pkt->length + padding - 4); |
| 1683 | if (ssh->csmac) |
| 1684 | ssh->csmac->generate(ssh->cs_mac_ctx, pkt->data, |
| 1685 | pkt->length + padding, |
| 1686 | ssh->v2_outgoing_sequence); |
| 1687 | ssh->v2_outgoing_sequence++; /* whether or not we MACed */ |
| 1688 | |
| 1689 | if (ssh->cscipher) |
| 1690 | ssh->cscipher->encrypt(ssh->cs_cipher_ctx, |
| 1691 | pkt->data, pkt->length + padding); |
| 1692 | |
| 1693 | pkt->encrypted_len = pkt->length + padding; |
| 1694 | |
| 1695 | /* Ready-to-send packet starts at pkt->data. We return length. */ |
| 1696 | return pkt->length + padding + maclen; |
| 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | * Routines called from the main SSH code to send packets. There |
| 1701 | * are quite a few of these, because we have two separate |
| 1702 | * mechanisms for delaying the sending of packets: |
| 1703 | * |
| 1704 | * - In order to send an IGNORE message and a password message in |
| 1705 | * a single fixed-length blob, we require the ability to |
| 1706 | * concatenate the encrypted forms of those two packets _into_ a |
| 1707 | * single blob and then pass it to our <network.h> transport |
| 1708 | * layer in one go. Hence, there's a deferment mechanism which |
| 1709 | * works after packet encryption. |
| 1710 | * |
| 1711 | * - In order to avoid sending any connection-layer messages |
| 1712 | * during repeat key exchange, we have to queue up any such |
| 1713 | * outgoing messages _before_ they are encrypted (and in |
| 1714 | * particular before they're allocated sequence numbers), and |
| 1715 | * then send them once we've finished. |
| 1716 | * |
| 1717 | * I call these mechanisms `defer' and `queue' respectively, so as |
| 1718 | * to distinguish them reasonably easily. |
| 1719 | * |
| 1720 | * The functions send_noqueue() and defer_noqueue() free the packet |
| 1721 | * structure they are passed. Every outgoing packet goes through |
| 1722 | * precisely one of these functions in its life; packets passed to |
| 1723 | * ssh2_pkt_send() or ssh2_pkt_defer() either go straight to one of |
| 1724 | * these or get queued, and then when the queue is later emptied |
| 1725 | * the packets are all passed to defer_noqueue(). |
| 1726 | */ |
| 1727 | |
| 1728 | /* |
| 1729 | * Send an SSH2 packet immediately, without queuing or deferring. |
| 1730 | */ |
| 1731 | static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt) |
| 1732 | { |
| 1733 | int len; |
| 1734 | int backlog; |
| 1735 | len = ssh2_pkt_construct(ssh, pkt); |
| 1736 | backlog = sk_write(ssh->s, (char *)pkt->data, len); |
| 1737 | if (backlog > SSH_MAX_BACKLOG) |
| 1738 | ssh_throttle_all(ssh, 1, backlog); |
| 1739 | |
| 1740 | ssh->outgoing_data_size += pkt->encrypted_len; |
| 1741 | if (!ssh->kex_in_progress && |
| 1742 | ssh->max_data_size != 0 && |
| 1743 | ssh->outgoing_data_size > ssh->max_data_size) |
| 1744 | do_ssh2_transport(ssh, "too much data sent", -1, NULL); |
| 1745 | |
| 1746 | ssh_free_packet(pkt); |
| 1747 | } |
| 1748 | |
| 1749 | /* |
| 1750 | * Defer an SSH2 packet. |
| 1751 | */ |
| 1752 | static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt) |
| 1753 | { |
| 1754 | int len = ssh2_pkt_construct(ssh, pkt); |
| 1755 | if (ssh->deferred_len + len > ssh->deferred_size) { |
| 1756 | ssh->deferred_size = ssh->deferred_len + len + 128; |
| 1757 | ssh->deferred_send_data = sresize(ssh->deferred_send_data, |
| 1758 | ssh->deferred_size, |
| 1759 | unsigned char); |
| 1760 | } |
| 1761 | memcpy(ssh->deferred_send_data + ssh->deferred_len, pkt->data, len); |
| 1762 | ssh->deferred_len += len; |
| 1763 | ssh->deferred_data_size += pkt->encrypted_len; |
| 1764 | ssh_free_packet(pkt); |
| 1765 | } |
| 1766 | |
| 1767 | /* |
| 1768 | * Queue an SSH2 packet. |
| 1769 | */ |
| 1770 | static void ssh2_pkt_queue(Ssh ssh, struct Packet *pkt) |
| 1771 | { |
| 1772 | assert(ssh->queueing); |
| 1773 | |
| 1774 | if (ssh->queuelen >= ssh->queuesize) { |
| 1775 | ssh->queuesize = ssh->queuelen + 32; |
| 1776 | ssh->queue = sresize(ssh->queue, ssh->queuesize, struct Packet *); |
| 1777 | } |
| 1778 | |
| 1779 | ssh->queue[ssh->queuelen++] = pkt; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * Either queue or send a packet, depending on whether queueing is |
| 1784 | * set. |
| 1785 | */ |
| 1786 | static void ssh2_pkt_send(Ssh ssh, struct Packet *pkt) |
| 1787 | { |
| 1788 | if (ssh->queueing) |
| 1789 | ssh2_pkt_queue(ssh, pkt); |
| 1790 | else |
| 1791 | ssh2_pkt_send_noqueue(ssh, pkt); |
| 1792 | } |
| 1793 | |
| 1794 | /* |
| 1795 | * Either queue or defer a packet, depending on whether queueing is |
| 1796 | * set. |
| 1797 | */ |
| 1798 | static void ssh2_pkt_defer(Ssh ssh, struct Packet *pkt) |
| 1799 | { |
| 1800 | if (ssh->queueing) |
| 1801 | ssh2_pkt_queue(ssh, pkt); |
| 1802 | else |
| 1803 | ssh2_pkt_defer_noqueue(ssh, pkt); |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * Send the whole deferred data block constructed by |
| 1808 | * ssh2_pkt_defer() or SSH1's defer_packet(). |
| 1809 | * |
| 1810 | * The expected use of the defer mechanism is that you call |
| 1811 | * ssh2_pkt_defer() a few times, then call ssh_pkt_defersend(). If |
| 1812 | * not currently queueing, this simply sets up deferred_send_data |
| 1813 | * and then sends it. If we _are_ currently queueing, the calls to |
| 1814 | * ssh2_pkt_defer() put the deferred packets on to the queue |
| 1815 | * instead, and therefore ssh_pkt_defersend() has no deferred data |
| 1816 | * to send. Hence, there's no need to make it conditional on |
| 1817 | * ssh->queueing. |
| 1818 | */ |
| 1819 | static void ssh_pkt_defersend(Ssh ssh) |
| 1820 | { |
| 1821 | int backlog; |
| 1822 | backlog = sk_write(ssh->s, (char *)ssh->deferred_send_data, |
| 1823 | ssh->deferred_len); |
| 1824 | ssh->deferred_len = ssh->deferred_size = 0; |
| 1825 | sfree(ssh->deferred_send_data); |
| 1826 | ssh->deferred_send_data = NULL; |
| 1827 | if (backlog > SSH_MAX_BACKLOG) |
| 1828 | ssh_throttle_all(ssh, 1, backlog); |
| 1829 | |
| 1830 | ssh->outgoing_data_size += ssh->deferred_data_size; |
| 1831 | if (!ssh->kex_in_progress && |
| 1832 | ssh->max_data_size != 0 && |
| 1833 | ssh->outgoing_data_size > ssh->max_data_size) |
| 1834 | do_ssh2_transport(ssh, "too much data sent", -1, NULL); |
| 1835 | ssh->deferred_data_size = 0; |
| 1836 | } |
| 1837 | |
| 1838 | /* |
| 1839 | * Send all queued SSH2 packets. We send them by means of |
| 1840 | * ssh2_pkt_defer_noqueue(), in case they included a pair of |
| 1841 | * packets that needed to be lumped together. |
| 1842 | */ |
| 1843 | static void ssh2_pkt_queuesend(Ssh ssh) |
| 1844 | { |
| 1845 | int i; |
| 1846 | |
| 1847 | assert(!ssh->queueing); |
| 1848 | |
| 1849 | for (i = 0; i < ssh->queuelen; i++) |
| 1850 | ssh2_pkt_defer_noqueue(ssh, ssh->queue[i]); |
| 1851 | ssh->queuelen = 0; |
| 1852 | |
| 1853 | ssh_pkt_defersend(ssh); |
| 1854 | } |
| 1855 | |
| 1856 | #if 0 |
| 1857 | void bndebug(char *string, Bignum b) |
| 1858 | { |
| 1859 | unsigned char *p; |
| 1860 | int i, len; |
| 1861 | p = ssh2_mpint_fmt(b, &len); |
| 1862 | debug(("%s", string)); |
| 1863 | for (i = 0; i < len; i++) |
| 1864 | debug((" %02x", p[i])); |
| 1865 | debug(("\n")); |
| 1866 | sfree(p); |
| 1867 | } |
| 1868 | #endif |
| 1869 | |
| 1870 | static void sha_mpint(SHA_State * s, Bignum b) |
| 1871 | { |
| 1872 | unsigned char *p; |
| 1873 | int len; |
| 1874 | p = ssh2_mpint_fmt(b, &len); |
| 1875 | sha_string(s, p, len); |
| 1876 | sfree(p); |
| 1877 | } |
| 1878 | |
| 1879 | /* |
| 1880 | * Packet decode functions for both SSH1 and SSH2. |
| 1881 | */ |
| 1882 | static unsigned long ssh_pkt_getuint32(struct Packet *pkt) |
| 1883 | { |
| 1884 | unsigned long value; |
| 1885 | if (pkt->length - pkt->savedpos < 4) |
| 1886 | return 0; /* arrgh, no way to decline (FIXME?) */ |
| 1887 | value = GET_32BIT(pkt->body + pkt->savedpos); |
| 1888 | pkt->savedpos += 4; |
| 1889 | return value; |
| 1890 | } |
| 1891 | static int ssh2_pkt_getbool(struct Packet *pkt) |
| 1892 | { |
| 1893 | unsigned long value; |
| 1894 | if (pkt->length - pkt->savedpos < 1) |
| 1895 | return 0; /* arrgh, no way to decline (FIXME?) */ |
| 1896 | value = pkt->body[pkt->savedpos] != 0; |
| 1897 | pkt->savedpos++; |
| 1898 | return value; |
| 1899 | } |
| 1900 | static void ssh_pkt_getstring(struct Packet *pkt, char **p, int *length) |
| 1901 | { |
| 1902 | int len; |
| 1903 | *p = NULL; |
| 1904 | *length = 0; |
| 1905 | if (pkt->length - pkt->savedpos < 4) |
| 1906 | return; |
| 1907 | len = GET_32BIT(pkt->body + pkt->savedpos); |
| 1908 | if (len < 0) |
| 1909 | return; |
| 1910 | *length = len; |
| 1911 | pkt->savedpos += 4; |
| 1912 | if (pkt->length - pkt->savedpos < *length) |
| 1913 | return; |
| 1914 | *p = (char *)(pkt->body + pkt->savedpos); |
| 1915 | pkt->savedpos += *length; |
| 1916 | } |
| 1917 | static void *ssh_pkt_getdata(struct Packet *pkt, int length) |
| 1918 | { |
| 1919 | if (pkt->length - pkt->savedpos < length) |
| 1920 | return NULL; |
| 1921 | pkt->savedpos += length; |
| 1922 | return pkt->body + (pkt->savedpos - length); |
| 1923 | } |
| 1924 | static int ssh1_pkt_getrsakey(struct Packet *pkt, struct RSAKey *key, |
| 1925 | unsigned char **keystr) |
| 1926 | { |
| 1927 | int j; |
| 1928 | |
| 1929 | j = makekey(pkt->body + pkt->savedpos, |
| 1930 | pkt->length - pkt->savedpos, |
| 1931 | key, keystr, 0); |
| 1932 | |
| 1933 | if (j < 0) |
| 1934 | return FALSE; |
| 1935 | |
| 1936 | pkt->savedpos += j; |
| 1937 | assert(pkt->savedpos < pkt->length); |
| 1938 | |
| 1939 | return TRUE; |
| 1940 | } |
| 1941 | static Bignum ssh1_pkt_getmp(struct Packet *pkt) |
| 1942 | { |
| 1943 | int j; |
| 1944 | Bignum b; |
| 1945 | |
| 1946 | j = ssh1_read_bignum(pkt->body + pkt->savedpos, |
| 1947 | pkt->length - pkt->savedpos, &b); |
| 1948 | |
| 1949 | if (j < 0) |
| 1950 | return NULL; |
| 1951 | |
| 1952 | pkt->savedpos += j; |
| 1953 | return b; |
| 1954 | } |
| 1955 | static Bignum ssh2_pkt_getmp(struct Packet *pkt) |
| 1956 | { |
| 1957 | char *p; |
| 1958 | int length; |
| 1959 | Bignum b; |
| 1960 | |
| 1961 | ssh_pkt_getstring(pkt, &p, &length); |
| 1962 | if (!p) |
| 1963 | return NULL; |
| 1964 | if (p[0] & 0x80) |
| 1965 | return NULL; |
| 1966 | b = bignum_from_bytes((unsigned char *)p, length); |
| 1967 | return b; |
| 1968 | } |
| 1969 | |
| 1970 | /* |
| 1971 | * Helper function to add an SSH2 signature blob to a packet. |
| 1972 | * Expects to be shown the public key blob as well as the signature |
| 1973 | * blob. Normally works just like ssh2_pkt_addstring, but will |
| 1974 | * fiddle with the signature packet if necessary for |
| 1975 | * BUG_SSH2_RSA_PADDING. |
| 1976 | */ |
| 1977 | static void ssh2_add_sigblob(Ssh ssh, struct Packet *pkt, |
| 1978 | void *pkblob_v, int pkblob_len, |
| 1979 | void *sigblob_v, int sigblob_len) |
| 1980 | { |
| 1981 | unsigned char *pkblob = (unsigned char *)pkblob_v; |
| 1982 | unsigned char *sigblob = (unsigned char *)sigblob_v; |
| 1983 | |
| 1984 | /* dmemdump(pkblob, pkblob_len); */ |
| 1985 | /* dmemdump(sigblob, sigblob_len); */ |
| 1986 | |
| 1987 | /* |
| 1988 | * See if this is in fact an ssh-rsa signature and a buggy |
| 1989 | * server; otherwise we can just do this the easy way. |
| 1990 | */ |
| 1991 | if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) && |
| 1992 | (GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) { |
| 1993 | int pos, len, siglen; |
| 1994 | |
| 1995 | /* |
| 1996 | * Find the byte length of the modulus. |
| 1997 | */ |
| 1998 | |
| 1999 | pos = 4+7; /* skip over "ssh-rsa" */ |
| 2000 | pos += 4 + GET_32BIT(pkblob+pos); /* skip over exponent */ |
| 2001 | len = GET_32BIT(pkblob+pos); /* find length of modulus */ |
| 2002 | pos += 4; /* find modulus itself */ |
| 2003 | while (len > 0 && pkblob[pos] == 0) |
| 2004 | len--, pos++; |
| 2005 | /* debug(("modulus length is %d\n", len)); */ |
| 2006 | |
| 2007 | /* |
| 2008 | * Now find the signature integer. |
| 2009 | */ |
| 2010 | pos = 4+7; /* skip over "ssh-rsa" */ |
| 2011 | siglen = GET_32BIT(sigblob+pos); |
| 2012 | /* debug(("signature length is %d\n", siglen)); */ |
| 2013 | |
| 2014 | if (len != siglen) { |
| 2015 | unsigned char newlen[4]; |
| 2016 | ssh2_pkt_addstring_start(pkt); |
| 2017 | ssh2_pkt_addstring_data(pkt, (char *)sigblob, pos); |
| 2018 | /* dmemdump(sigblob, pos); */ |
| 2019 | pos += 4; /* point to start of actual sig */ |
| 2020 | PUT_32BIT(newlen, len); |
| 2021 | ssh2_pkt_addstring_data(pkt, (char *)newlen, 4); |
| 2022 | /* dmemdump(newlen, 4); */ |
| 2023 | newlen[0] = 0; |
| 2024 | while (len-- > siglen) { |
| 2025 | ssh2_pkt_addstring_data(pkt, (char *)newlen, 1); |
| 2026 | /* dmemdump(newlen, 1); */ |
| 2027 | } |
| 2028 | ssh2_pkt_addstring_data(pkt, (char *)(sigblob+pos), siglen); |
| 2029 | /* dmemdump(sigblob+pos, siglen); */ |
| 2030 | return; |
| 2031 | } |
| 2032 | |
| 2033 | /* Otherwise fall through and do it the easy way. */ |
| 2034 | } |
| 2035 | |
| 2036 | ssh2_pkt_addstring_start(pkt); |
| 2037 | ssh2_pkt_addstring_data(pkt, (char *)sigblob, sigblob_len); |
| 2038 | } |
| 2039 | |
| 2040 | /* |
| 2041 | * Examine the remote side's version string and compare it against |
| 2042 | * a list of known buggy implementations. |
| 2043 | */ |
| 2044 | static void ssh_detect_bugs(Ssh ssh, char *vstring) |
| 2045 | { |
| 2046 | char *imp; /* pointer to implementation part */ |
| 2047 | imp = vstring; |
| 2048 | imp += strcspn(imp, "-"); |
| 2049 | if (*imp) imp++; |
| 2050 | imp += strcspn(imp, "-"); |
| 2051 | if (*imp) imp++; |
| 2052 | |
| 2053 | ssh->remote_bugs = 0; |
| 2054 | |
| 2055 | if (ssh->cfg.sshbug_ignore1 == FORCE_ON || |
| 2056 | (ssh->cfg.sshbug_ignore1 == AUTO && |
| 2057 | (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") || |
| 2058 | !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") || |
| 2059 | !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25") || |
| 2060 | !strcmp(imp, "OSU_1.4alpha3") || !strcmp(imp, "OSU_1.5alpha4")))) { |
| 2061 | /* |
| 2062 | * These versions don't support SSH1_MSG_IGNORE, so we have |
| 2063 | * to use a different defence against password length |
| 2064 | * sniffing. |
| 2065 | */ |
| 2066 | ssh->remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE; |
| 2067 | logevent("We believe remote version has SSH1 ignore bug"); |
| 2068 | } |
| 2069 | |
| 2070 | if (ssh->cfg.sshbug_plainpw1 == FORCE_ON || |
| 2071 | (ssh->cfg.sshbug_plainpw1 == AUTO && |
| 2072 | (!strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3")))) { |
| 2073 | /* |
| 2074 | * These versions need a plain password sent; they can't |
| 2075 | * handle having a null and a random length of data after |
| 2076 | * the password. |
| 2077 | */ |
| 2078 | ssh->remote_bugs |= BUG_NEEDS_SSH1_PLAIN_PASSWORD; |
| 2079 | logevent("We believe remote version needs a plain SSH1 password"); |
| 2080 | } |
| 2081 | |
| 2082 | if (ssh->cfg.sshbug_rsa1 == FORCE_ON || |
| 2083 | (ssh->cfg.sshbug_rsa1 == AUTO && |
| 2084 | (!strcmp(imp, "Cisco-1.25")))) { |
| 2085 | /* |
| 2086 | * These versions apparently have no clue whatever about |
| 2087 | * RSA authentication and will panic and die if they see |
| 2088 | * an AUTH_RSA message. |
| 2089 | */ |
| 2090 | ssh->remote_bugs |= BUG_CHOKES_ON_RSA; |
| 2091 | logevent("We believe remote version can't handle RSA authentication"); |
| 2092 | } |
| 2093 | |
| 2094 | if (ssh->cfg.sshbug_hmac2 == FORCE_ON || |
| 2095 | (ssh->cfg.sshbug_hmac2 == AUTO && |
| 2096 | !wc_match("* VShell", imp) && |
| 2097 | (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) || |
| 2098 | wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) || |
| 2099 | wc_match("2.1 *", imp)))) { |
| 2100 | /* |
| 2101 | * These versions have the HMAC bug. |
| 2102 | */ |
| 2103 | ssh->remote_bugs |= BUG_SSH2_HMAC; |
| 2104 | logevent("We believe remote version has SSH2 HMAC bug"); |
| 2105 | } |
| 2106 | |
| 2107 | if (ssh->cfg.sshbug_derivekey2 == FORCE_ON || |
| 2108 | (ssh->cfg.sshbug_derivekey2 == AUTO && |
| 2109 | !wc_match("* VShell", imp) && |
| 2110 | (wc_match("2.0.0*", imp) || wc_match("2.0.10*", imp) ))) { |
| 2111 | /* |
| 2112 | * These versions have the key-derivation bug (failing to |
| 2113 | * include the literal shared secret in the hashes that |
| 2114 | * generate the keys). |
| 2115 | */ |
| 2116 | ssh->remote_bugs |= BUG_SSH2_DERIVEKEY; |
| 2117 | logevent("We believe remote version has SSH2 key-derivation bug"); |
| 2118 | } |
| 2119 | |
| 2120 | if (ssh->cfg.sshbug_rsapad2 == FORCE_ON || |
| 2121 | (ssh->cfg.sshbug_rsapad2 == AUTO && |
| 2122 | (wc_match("OpenSSH_2.[5-9]*", imp) || |
| 2123 | wc_match("OpenSSH_3.[0-2]*", imp)))) { |
| 2124 | /* |
| 2125 | * These versions have the SSH2 RSA padding bug. |
| 2126 | */ |
| 2127 | ssh->remote_bugs |= BUG_SSH2_RSA_PADDING; |
| 2128 | logevent("We believe remote version has SSH2 RSA padding bug"); |
| 2129 | } |
| 2130 | |
| 2131 | if (ssh->cfg.sshbug_pksessid2 == FORCE_ON || |
| 2132 | (ssh->cfg.sshbug_pksessid2 == AUTO && |
| 2133 | wc_match("OpenSSH_2.[0-2]*", imp))) { |
| 2134 | /* |
| 2135 | * These versions have the SSH2 session-ID bug in |
| 2136 | * public-key authentication. |
| 2137 | */ |
| 2138 | ssh->remote_bugs |= BUG_SSH2_PK_SESSIONID; |
| 2139 | logevent("We believe remote version has SSH2 public-key-session-ID bug"); |
| 2140 | } |
| 2141 | |
| 2142 | if (ssh->cfg.sshbug_rekey2 == FORCE_ON || |
| 2143 | (ssh->cfg.sshbug_rekey2 == AUTO && |
| 2144 | (wc_match("OpenSSH_2.[0-4]*", imp) || |
| 2145 | wc_match("OpenSSH_2.5.[0-3]*", imp) || |
| 2146 | wc_match("Sun_SSH_1.0", imp) || |
| 2147 | wc_match("Sun_SSH_1.0.1", imp)))) { |
| 2148 | /* |
| 2149 | * These versions have the SSH2 rekey bug. |
| 2150 | */ |
| 2151 | ssh->remote_bugs |= BUG_SSH2_REKEY; |
| 2152 | logevent("We believe remote version has SSH2 rekey bug"); |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * The `software version' part of an SSH version string is required |
| 2158 | * to contain no spaces or minus signs. |
| 2159 | */ |
| 2160 | static void ssh_fix_verstring(char *str) |
| 2161 | { |
| 2162 | /* Eat "SSH-<protoversion>-". */ |
| 2163 | assert(*str == 'S'); str++; |
| 2164 | assert(*str == 'S'); str++; |
| 2165 | assert(*str == 'H'); str++; |
| 2166 | assert(*str == '-'); str++; |
| 2167 | while (*str && *str != '-') str++; |
| 2168 | assert(*str == '-'); str++; |
| 2169 | |
| 2170 | /* Convert minus signs and spaces in the remaining string into |
| 2171 | * underscores. */ |
| 2172 | while (*str) { |
| 2173 | if (*str == '-' || *str == ' ') |
| 2174 | *str = '_'; |
| 2175 | str++; |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | static int do_ssh_init(Ssh ssh, unsigned char c) |
| 2180 | { |
| 2181 | struct do_ssh_init_state { |
| 2182 | int vslen; |
| 2183 | char version[10]; |
| 2184 | char *vstring; |
| 2185 | int vstrsize; |
| 2186 | int i; |
| 2187 | int proto1, proto2; |
| 2188 | }; |
| 2189 | crState(do_ssh_init_state); |
| 2190 | |
| 2191 | crBegin(ssh->do_ssh_init_crstate); |
| 2192 | |
| 2193 | /* Search for the string "SSH-" in the input. */ |
| 2194 | s->i = 0; |
| 2195 | while (1) { |
| 2196 | static const int transS[] = { 1, 2, 2, 1 }; |
| 2197 | static const int transH[] = { 0, 0, 3, 0 }; |
| 2198 | static const int transminus[] = { 0, 0, 0, -1 }; |
| 2199 | if (c == 'S') |
| 2200 | s->i = transS[s->i]; |
| 2201 | else if (c == 'H') |
| 2202 | s->i = transH[s->i]; |
| 2203 | else if (c == '-') |
| 2204 | s->i = transminus[s->i]; |
| 2205 | else |
| 2206 | s->i = 0; |
| 2207 | if (s->i < 0) |
| 2208 | break; |
| 2209 | crReturn(1); /* get another character */ |
| 2210 | } |
| 2211 | |
| 2212 | s->vstrsize = 16; |
| 2213 | s->vstring = snewn(s->vstrsize, char); |
| 2214 | strcpy(s->vstring, "SSH-"); |
| 2215 | s->vslen = 4; |
| 2216 | s->i = 0; |
| 2217 | while (1) { |
| 2218 | crReturn(1); /* get another char */ |
| 2219 | if (s->vslen >= s->vstrsize - 1) { |
| 2220 | s->vstrsize += 16; |
| 2221 | s->vstring = sresize(s->vstring, s->vstrsize, char); |
| 2222 | } |
| 2223 | s->vstring[s->vslen++] = c; |
| 2224 | if (s->i >= 0) { |
| 2225 | if (c == '-') { |
| 2226 | s->version[s->i] = '\0'; |
| 2227 | s->i = -1; |
| 2228 | } else if (s->i < sizeof(s->version) - 1) |
| 2229 | s->version[s->i++] = c; |
| 2230 | } else if (c == '\012') |
| 2231 | break; |
| 2232 | } |
| 2233 | |
| 2234 | ssh->agentfwd_enabled = FALSE; |
| 2235 | ssh->rdpkt2_state.incoming_sequence = 0; |
| 2236 | |
| 2237 | s->vstring[s->vslen] = 0; |
| 2238 | s->vstring[strcspn(s->vstring, "\015\012")] = '\0';/* remove EOL chars */ |
| 2239 | { |
| 2240 | char *vlog; |
| 2241 | vlog = snewn(20 + s->vslen, char); |
| 2242 | sprintf(vlog, "Server version: %s", s->vstring); |
| 2243 | logevent(vlog); |
| 2244 | sfree(vlog); |
| 2245 | } |
| 2246 | ssh_detect_bugs(ssh, s->vstring); |
| 2247 | |
| 2248 | /* |
| 2249 | * Decide which SSH protocol version to support. |
| 2250 | */ |
| 2251 | |
| 2252 | /* Anything strictly below "2.0" means protocol 1 is supported. */ |
| 2253 | s->proto1 = ssh_versioncmp(s->version, "2.0") < 0; |
| 2254 | /* Anything greater or equal to "1.99" means protocol 2 is supported. */ |
| 2255 | s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0; |
| 2256 | |
| 2257 | if (ssh->cfg.sshprot == 0 && !s->proto1) { |
| 2258 | bombout(("SSH protocol version 1 required by user but not provided by server")); |
| 2259 | crStop(0); |
| 2260 | } |
| 2261 | if (ssh->cfg.sshprot == 3 && !s->proto2) { |
| 2262 | bombout(("SSH protocol version 2 required by user but not provided by server")); |
| 2263 | crStop(0); |
| 2264 | } |
| 2265 | |
| 2266 | { |
| 2267 | char *verstring; |
| 2268 | |
| 2269 | if (s->proto2 && (ssh->cfg.sshprot >= 2 || !s->proto1)) { |
| 2270 | /* |
| 2271 | * Construct a v2 version string. |
| 2272 | */ |
| 2273 | verstring = dupprintf("SSH-2.0-%s\015\012", sshver); |
| 2274 | ssh->version = 2; |
| 2275 | } else { |
| 2276 | /* |
| 2277 | * Construct a v1 version string. |
| 2278 | */ |
| 2279 | verstring = dupprintf("SSH-%s-%s\012", |
| 2280 | (ssh_versioncmp(s->version, "1.5") <= 0 ? |
| 2281 | s->version : "1.5"), |
| 2282 | sshver); |
| 2283 | ssh->version = 1; |
| 2284 | } |
| 2285 | |
| 2286 | ssh_fix_verstring(verstring); |
| 2287 | |
| 2288 | if (ssh->version == 2) { |
| 2289 | /* |
| 2290 | * Hash our version string and their version string. |
| 2291 | */ |
| 2292 | SHA_Init(&ssh->exhashbase); |
| 2293 | sha_string(&ssh->exhashbase, verstring, |
| 2294 | strcspn(verstring, "\015\012")); |
| 2295 | sha_string(&ssh->exhashbase, s->vstring, |
| 2296 | strcspn(s->vstring, "\015\012")); |
| 2297 | |
| 2298 | /* |
| 2299 | * Initialise SSHv2 protocol. |
| 2300 | */ |
| 2301 | ssh->protocol = ssh2_protocol; |
| 2302 | ssh2_protocol_setup(ssh); |
| 2303 | ssh->s_rdpkt = ssh2_rdpkt; |
| 2304 | } else { |
| 2305 | /* |
| 2306 | * Initialise SSHv1 protocol. |
| 2307 | */ |
| 2308 | ssh->protocol = ssh1_protocol; |
| 2309 | ssh1_protocol_setup(ssh); |
| 2310 | ssh->s_rdpkt = ssh1_rdpkt; |
| 2311 | } |
| 2312 | logeventf(ssh, "We claim version: %.*s", |
| 2313 | strcspn(verstring, "\015\012"), verstring); |
| 2314 | sk_write(ssh->s, verstring, strlen(verstring)); |
| 2315 | sfree(verstring); |
| 2316 | } |
| 2317 | |
| 2318 | logeventf(ssh, "Using SSH protocol version %d", ssh->version); |
| 2319 | |
| 2320 | update_specials_menu(ssh->frontend); |
| 2321 | ssh->state = SSH_STATE_BEFORE_SIZE; |
| 2322 | ssh->pinger = pinger_new(&ssh->cfg, &ssh_backend, ssh); |
| 2323 | |
| 2324 | sfree(s->vstring); |
| 2325 | |
| 2326 | crFinish(0); |
| 2327 | } |
| 2328 | |
| 2329 | static void ssh_gotdata(Ssh ssh, unsigned char *data, int datalen) |
| 2330 | { |
| 2331 | crBegin(ssh->ssh_gotdata_crstate); |
| 2332 | |
| 2333 | /* |
| 2334 | * To begin with, feed the characters one by one to the |
| 2335 | * protocol initialisation / selection function do_ssh_init(). |
| 2336 | * When that returns 0, we're done with the initial greeting |
| 2337 | * exchange and can move on to packet discipline. |
| 2338 | */ |
| 2339 | while (1) { |
| 2340 | int ret; /* need not be kept across crReturn */ |
| 2341 | if (datalen == 0) |
| 2342 | crReturnV; /* more data please */ |
| 2343 | ret = do_ssh_init(ssh, *data); |
| 2344 | data++; |
| 2345 | datalen--; |
| 2346 | if (ret == 0) |
| 2347 | break; |
| 2348 | } |
| 2349 | |
| 2350 | /* |
| 2351 | * We emerge from that loop when the initial negotiation is |
| 2352 | * over and we have selected an s_rdpkt function. Now pass |
| 2353 | * everything to s_rdpkt, and then pass the resulting packets |
| 2354 | * to the proper protocol handler. |
| 2355 | */ |
| 2356 | if (datalen == 0) |
| 2357 | crReturnV; |
| 2358 | while (1) { |
| 2359 | while (datalen > 0) { |
| 2360 | struct Packet *pktin = ssh->s_rdpkt(ssh, &data, &datalen); |
| 2361 | if (pktin) { |
| 2362 | ssh->protocol(ssh, NULL, 0, pktin); |
| 2363 | ssh_free_packet(pktin); |
| 2364 | } |
| 2365 | if (ssh->state == SSH_STATE_CLOSED) |
| 2366 | return; |
| 2367 | } |
| 2368 | crReturnV; |
| 2369 | } |
| 2370 | crFinishV; |
| 2371 | } |
| 2372 | |
| 2373 | static int ssh_do_close(Ssh ssh, int notify_exit) |
| 2374 | { |
| 2375 | int i, ret = 0; |
| 2376 | struct ssh_channel *c; |
| 2377 | |
| 2378 | ssh->state = SSH_STATE_CLOSED; |
| 2379 | if (ssh->s) { |
| 2380 | sk_close(ssh->s); |
| 2381 | ssh->s = NULL; |
| 2382 | if (notify_exit) |
| 2383 | notify_remote_exit(ssh->frontend); |
| 2384 | else |
| 2385 | ret = 1; |
| 2386 | } |
| 2387 | /* |
| 2388 | * Now we must shut down any port and X forwardings going |
| 2389 | * through this connection. |
| 2390 | */ |
| 2391 | if (ssh->channels) { |
| 2392 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
| 2393 | switch (c->type) { |
| 2394 | case CHAN_X11: |
| 2395 | x11_close(c->u.x11.s); |
| 2396 | break; |
| 2397 | case CHAN_SOCKDATA: |
| 2398 | pfd_close(c->u.pfd.s); |
| 2399 | break; |
| 2400 | } |
| 2401 | del234(ssh->channels, c); |
| 2402 | if (ssh->version == 2) |
| 2403 | bufchain_clear(&c->v.v2.outbuffer); |
| 2404 | sfree(c); |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | return ret; |
| 2409 | } |
| 2410 | |
| 2411 | static int ssh_closing(Plug plug, const char *error_msg, int error_code, |
| 2412 | int calling_back) |
| 2413 | { |
| 2414 | Ssh ssh = (Ssh) plug; |
| 2415 | int need_notify = ssh_do_close(ssh, FALSE); |
| 2416 | |
| 2417 | if (!error_msg && !ssh->close_expected) { |
| 2418 | error_msg = "Server unexpectedly closed network connection"; |
| 2419 | } |
| 2420 | |
| 2421 | if (error_msg) { |
| 2422 | /* A socket error has occurred. */ |
| 2423 | logevent(error_msg); |
| 2424 | connection_fatal(ssh->frontend, "%s", error_msg); |
| 2425 | } else { |
| 2426 | logevent("Server closed network connection"); |
| 2427 | } |
| 2428 | if (need_notify) |
| 2429 | notify_remote_exit(ssh->frontend); |
| 2430 | return 0; |
| 2431 | } |
| 2432 | |
| 2433 | static int ssh_receive(Plug plug, int urgent, char *data, int len) |
| 2434 | { |
| 2435 | Ssh ssh = (Ssh) plug; |
| 2436 | ssh_gotdata(ssh, (unsigned char *)data, len); |
| 2437 | if (ssh->state == SSH_STATE_CLOSED) { |
| 2438 | ssh_do_close(ssh, TRUE); |
| 2439 | return 0; |
| 2440 | } |
| 2441 | return 1; |
| 2442 | } |
| 2443 | |
| 2444 | static void ssh_sent(Plug plug, int bufsize) |
| 2445 | { |
| 2446 | Ssh ssh = (Ssh) plug; |
| 2447 | /* |
| 2448 | * If the send backlog on the SSH socket itself clears, we |
| 2449 | * should unthrottle the whole world if it was throttled. |
| 2450 | */ |
| 2451 | if (bufsize < SSH_MAX_BACKLOG) |
| 2452 | ssh_throttle_all(ssh, 0, bufsize); |
| 2453 | } |
| 2454 | |
| 2455 | /* |
| 2456 | * Connect to specified host and port. |
| 2457 | * Returns an error message, or NULL on success. |
| 2458 | * Also places the canonical host name into `realhost'. It must be |
| 2459 | * freed by the caller. |
| 2460 | */ |
| 2461 | static const char *connect_to_host(Ssh ssh, char *host, int port, |
| 2462 | char **realhost, int nodelay, int keepalive) |
| 2463 | { |
| 2464 | static const struct plug_function_table fn_table = { |
| 2465 | ssh_closing, |
| 2466 | ssh_receive, |
| 2467 | ssh_sent, |
| 2468 | NULL |
| 2469 | }; |
| 2470 | |
| 2471 | SockAddr addr; |
| 2472 | const char *err; |
| 2473 | |
| 2474 | ssh->savedhost = snewn(1 + strlen(host), char); |
| 2475 | if (!ssh->savedhost) |
| 2476 | fatalbox("Out of memory"); |
| 2477 | strcpy(ssh->savedhost, host); |
| 2478 | |
| 2479 | if (port < 0) |
| 2480 | port = 22; /* default ssh port */ |
| 2481 | ssh->savedport = port; |
| 2482 | |
| 2483 | /* |
| 2484 | * Try to find host. |
| 2485 | */ |
| 2486 | logeventf(ssh, "Looking up host \"%s\"%s", host, |
| 2487 | (ssh->cfg.addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : |
| 2488 | (ssh->cfg.addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); |
| 2489 | addr = name_lookup(host, port, realhost, &ssh->cfg, |
| 2490 | ssh->cfg.addressfamily); |
| 2491 | if ((err = sk_addr_error(addr)) != NULL) { |
| 2492 | sk_addr_free(addr); |
| 2493 | return err; |
| 2494 | } |
| 2495 | |
| 2496 | /* |
| 2497 | * Open socket. |
| 2498 | */ |
| 2499 | { |
| 2500 | char addrbuf[100]; |
| 2501 | sk_getaddr(addr, addrbuf, 100); |
| 2502 | logeventf(ssh, "Connecting to %s port %d", addrbuf, port); |
| 2503 | } |
| 2504 | ssh->fn = &fn_table; |
| 2505 | ssh->s = new_connection(addr, *realhost, port, |
| 2506 | 0, 1, nodelay, keepalive, (Plug) ssh, &ssh->cfg); |
| 2507 | if ((err = sk_socket_error(ssh->s)) != NULL) { |
| 2508 | ssh->s = NULL; |
| 2509 | notify_remote_exit(ssh->frontend); |
| 2510 | return err; |
| 2511 | } |
| 2512 | |
| 2513 | return NULL; |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * Throttle or unthrottle the SSH connection. |
| 2518 | */ |
| 2519 | static void ssh1_throttle(Ssh ssh, int adjust) |
| 2520 | { |
| 2521 | int old_count = ssh->v1_throttle_count; |
| 2522 | ssh->v1_throttle_count += adjust; |
| 2523 | assert(ssh->v1_throttle_count >= 0); |
| 2524 | if (ssh->v1_throttle_count && !old_count) { |
| 2525 | sk_set_frozen(ssh->s, 1); |
| 2526 | } else if (!ssh->v1_throttle_count && old_count) { |
| 2527 | sk_set_frozen(ssh->s, 0); |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | /* |
| 2532 | * Throttle or unthrottle _all_ local data streams (for when sends |
| 2533 | * on the SSH connection itself back up). |
| 2534 | */ |
| 2535 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize) |
| 2536 | { |
| 2537 | int i; |
| 2538 | struct ssh_channel *c; |
| 2539 | |
| 2540 | if (enable == ssh->throttled_all) |
| 2541 | return; |
| 2542 | ssh->throttled_all = enable; |
| 2543 | ssh->overall_bufsize = bufsize; |
| 2544 | if (!ssh->channels) |
| 2545 | return; |
| 2546 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
| 2547 | switch (c->type) { |
| 2548 | case CHAN_MAINSESSION: |
| 2549 | /* |
| 2550 | * This is treated separately, outside the switch. |
| 2551 | */ |
| 2552 | break; |
| 2553 | case CHAN_X11: |
| 2554 | x11_override_throttle(c->u.x11.s, enable); |
| 2555 | break; |
| 2556 | case CHAN_AGENT: |
| 2557 | /* Agent channels require no buffer management. */ |
| 2558 | break; |
| 2559 | case CHAN_SOCKDATA: |
| 2560 | pfd_override_throttle(c->u.pfd.s, enable); |
| 2561 | break; |
| 2562 | } |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | /* |
| 2567 | * Username and password input, abstracted off into routines |
| 2568 | * reusable in several places - even between SSH1 and SSH2. |
| 2569 | */ |
| 2570 | |
| 2571 | /* Set up a username or password input loop on a given buffer. */ |
| 2572 | static void setup_userpass_input(Ssh ssh, char *buffer, int buflen, int echo) |
| 2573 | { |
| 2574 | ssh->userpass_input_buffer = buffer; |
| 2575 | ssh->userpass_input_buflen = buflen; |
| 2576 | ssh->userpass_input_bufpos = 0; |
| 2577 | ssh->userpass_input_echo = echo; |
| 2578 | } |
| 2579 | |
| 2580 | /* |
| 2581 | * Process some terminal data in the course of username/password |
| 2582 | * input. Returns >0 for success (line of input returned in |
| 2583 | * buffer), <0 for failure (user hit ^C/^D, bomb out and exit), 0 |
| 2584 | * for inconclusive (keep waiting for more input please). |
| 2585 | */ |
| 2586 | static int process_userpass_input(Ssh ssh, unsigned char *in, int inlen) |
| 2587 | { |
| 2588 | char c; |
| 2589 | |
| 2590 | while (inlen--) { |
| 2591 | switch (c = *in++) { |
| 2592 | case 10: |
| 2593 | case 13: |
| 2594 | ssh->userpass_input_buffer[ssh->userpass_input_bufpos] = 0; |
| 2595 | ssh->userpass_input_buffer[ssh->userpass_input_buflen-1] = 0; |
| 2596 | return +1; |
| 2597 | break; |
| 2598 | case 8: |
| 2599 | case 127: |
| 2600 | if (ssh->userpass_input_bufpos > 0) { |
| 2601 | if (ssh->userpass_input_echo) |
| 2602 | c_write_str(ssh, "\b \b"); |
| 2603 | ssh->userpass_input_bufpos--; |
| 2604 | } |
| 2605 | break; |
| 2606 | case 21: |
| 2607 | case 27: |
| 2608 | while (ssh->userpass_input_bufpos > 0) { |
| 2609 | if (ssh->userpass_input_echo) |
| 2610 | c_write_str(ssh, "\b \b"); |
| 2611 | ssh->userpass_input_bufpos--; |
| 2612 | } |
| 2613 | break; |
| 2614 | case 3: |
| 2615 | case 4: |
| 2616 | return -1; |
| 2617 | break; |
| 2618 | default: |
| 2619 | /* |
| 2620 | * This simplistic check for printability is disabled |
| 2621 | * when we're doing password input, because some people |
| 2622 | * have control characters in their passwords.o |
| 2623 | */ |
| 2624 | if ((!ssh->userpass_input_echo || |
| 2625 | (c >= ' ' && c <= '~') || |
| 2626 | ((unsigned char) c >= 160)) |
| 2627 | && ssh->userpass_input_bufpos < ssh->userpass_input_buflen-1) { |
| 2628 | ssh->userpass_input_buffer[ssh->userpass_input_bufpos++] = c; |
| 2629 | if (ssh->userpass_input_echo) |
| 2630 | c_write(ssh, &c, 1); |
| 2631 | } |
| 2632 | break; |
| 2633 | } |
| 2634 | } |
| 2635 | return 0; |
| 2636 | } |
| 2637 | |
| 2638 | static void ssh_agent_callback(void *sshv, void *reply, int replylen) |
| 2639 | { |
| 2640 | Ssh ssh = (Ssh) sshv; |
| 2641 | |
| 2642 | ssh->agent_response = reply; |
| 2643 | ssh->agent_response_len = replylen; |
| 2644 | |
| 2645 | if (ssh->version == 1) |
| 2646 | do_ssh1_login(ssh, NULL, -1, NULL); |
| 2647 | else |
| 2648 | do_ssh2_authconn(ssh, NULL, -1, NULL); |
| 2649 | } |
| 2650 | |
| 2651 | static void ssh_agentf_callback(void *cv, void *reply, int replylen) |
| 2652 | { |
| 2653 | struct ssh_channel *c = (struct ssh_channel *)cv; |
| 2654 | Ssh ssh = c->ssh; |
| 2655 | void *sentreply = reply; |
| 2656 | |
| 2657 | if (!sentreply) { |
| 2658 | /* Fake SSH_AGENT_FAILURE. */ |
| 2659 | sentreply = "\0\0\0\1\5"; |
| 2660 | replylen = 5; |
| 2661 | } |
| 2662 | if (ssh->version == 2) { |
| 2663 | ssh2_add_channel_data(c, sentreply, replylen); |
| 2664 | ssh2_try_send(c); |
| 2665 | } else { |
| 2666 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
| 2667 | PKT_INT, c->remoteid, |
| 2668 | PKTT_DATA, |
| 2669 | PKT_INT, replylen, |
| 2670 | PKT_DATA, sentreply, replylen, |
| 2671 | PKTT_OTHER, |
| 2672 | PKT_END); |
| 2673 | } |
| 2674 | if (reply) |
| 2675 | sfree(reply); |
| 2676 | } |
| 2677 | |
| 2678 | /* |
| 2679 | * Handle the key exchange and user authentication phases. |
| 2680 | */ |
| 2681 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, |
| 2682 | struct Packet *pktin) |
| 2683 | { |
| 2684 | int i, j, ret; |
| 2685 | unsigned char cookie[8], *ptr; |
| 2686 | struct RSAKey servkey, hostkey; |
| 2687 | struct MD5Context md5c; |
| 2688 | struct do_ssh1_login_state { |
| 2689 | int len; |
| 2690 | unsigned char *rsabuf, *keystr1, *keystr2; |
| 2691 | unsigned long supported_ciphers_mask, supported_auths_mask; |
| 2692 | int tried_publickey, tried_agent; |
| 2693 | int tis_auth_refused, ccard_auth_refused; |
| 2694 | unsigned char session_id[16]; |
| 2695 | int cipher_type; |
| 2696 | char username[100]; |
| 2697 | void *publickey_blob; |
| 2698 | int publickey_bloblen; |
| 2699 | char password[100]; |
| 2700 | char prompt[200]; |
| 2701 | int pos; |
| 2702 | char c; |
| 2703 | int pwpkt_type; |
| 2704 | unsigned char request[5], *response, *p; |
| 2705 | int responselen; |
| 2706 | int keyi, nkeys; |
| 2707 | int authed; |
| 2708 | struct RSAKey key; |
| 2709 | Bignum challenge; |
| 2710 | char *commentp; |
| 2711 | int commentlen; |
| 2712 | }; |
| 2713 | crState(do_ssh1_login_state); |
| 2714 | |
| 2715 | crBegin(ssh->do_ssh1_login_crstate); |
| 2716 | |
| 2717 | if (!pktin) |
| 2718 | crWaitUntil(pktin); |
| 2719 | |
| 2720 | if (pktin->type != SSH1_SMSG_PUBLIC_KEY) { |
| 2721 | bombout(("Public key packet not received")); |
| 2722 | crStop(0); |
| 2723 | } |
| 2724 | |
| 2725 | logevent("Received public keys"); |
| 2726 | |
| 2727 | ptr = ssh_pkt_getdata(pktin, 8); |
| 2728 | if (!ptr) { |
| 2729 | bombout(("SSH1 public key packet stopped before random cookie")); |
| 2730 | crStop(0); |
| 2731 | } |
| 2732 | memcpy(cookie, ptr, 8); |
| 2733 | |
| 2734 | if (!ssh1_pkt_getrsakey(pktin, &servkey, &s->keystr1) || |
| 2735 | !ssh1_pkt_getrsakey(pktin, &hostkey, &s->keystr2)) { |
| 2736 | bombout(("Failed to read SSH1 public keys from public key packet")); |
| 2737 | crStop(0); |
| 2738 | } |
| 2739 | |
| 2740 | /* |
| 2741 | * Log the host key fingerprint. |
| 2742 | */ |
| 2743 | { |
| 2744 | char logmsg[80]; |
| 2745 | logevent("Host key fingerprint is:"); |
| 2746 | strcpy(logmsg, " "); |
| 2747 | hostkey.comment = NULL; |
| 2748 | rsa_fingerprint(logmsg + strlen(logmsg), |
| 2749 | sizeof(logmsg) - strlen(logmsg), &hostkey); |
| 2750 | logevent(logmsg); |
| 2751 | } |
| 2752 | |
| 2753 | ssh->v1_remote_protoflags = ssh_pkt_getuint32(pktin); |
| 2754 | s->supported_ciphers_mask = ssh_pkt_getuint32(pktin); |
| 2755 | s->supported_auths_mask = ssh_pkt_getuint32(pktin); |
| 2756 | |
| 2757 | ssh->v1_local_protoflags = |
| 2758 | ssh->v1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED; |
| 2759 | ssh->v1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER; |
| 2760 | |
| 2761 | MD5Init(&md5c); |
| 2762 | MD5Update(&md5c, s->keystr2, hostkey.bytes); |
| 2763 | MD5Update(&md5c, s->keystr1, servkey.bytes); |
| 2764 | MD5Update(&md5c, cookie, 8); |
| 2765 | MD5Final(s->session_id, &md5c); |
| 2766 | |
| 2767 | for (i = 0; i < 32; i++) |
| 2768 | ssh->session_key[i] = random_byte(); |
| 2769 | |
| 2770 | /* |
| 2771 | * Verify that the `bits' and `bytes' parameters match. |
| 2772 | */ |
| 2773 | if (hostkey.bits > hostkey.bytes * 8 || |
| 2774 | servkey.bits > servkey.bytes * 8) { |
| 2775 | bombout(("SSH1 public keys were badly formatted")); |
| 2776 | crStop(0); |
| 2777 | } |
| 2778 | |
| 2779 | s->len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); |
| 2780 | |
| 2781 | s->rsabuf = snewn(s->len, unsigned char); |
| 2782 | if (!s->rsabuf) |
| 2783 | fatalbox("Out of memory"); |
| 2784 | |
| 2785 | /* |
| 2786 | * Verify the host key. |
| 2787 | */ |
| 2788 | { |
| 2789 | /* |
| 2790 | * First format the key into a string. |
| 2791 | */ |
| 2792 | int len = rsastr_len(&hostkey); |
| 2793 | char fingerprint[100]; |
| 2794 | char *keystr = snewn(len, char); |
| 2795 | if (!keystr) |
| 2796 | fatalbox("Out of memory"); |
| 2797 | rsastr_fmt(keystr, &hostkey); |
| 2798 | rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey); |
| 2799 | verify_ssh_host_key(ssh->frontend, |
| 2800 | ssh->savedhost, ssh->savedport, "rsa", keystr, |
| 2801 | fingerprint); |
| 2802 | sfree(keystr); |
| 2803 | } |
| 2804 | |
| 2805 | for (i = 0; i < 32; i++) { |
| 2806 | s->rsabuf[i] = ssh->session_key[i]; |
| 2807 | if (i < 16) |
| 2808 | s->rsabuf[i] ^= s->session_id[i]; |
| 2809 | } |
| 2810 | |
| 2811 | if (hostkey.bytes > servkey.bytes) { |
| 2812 | ret = rsaencrypt(s->rsabuf, 32, &servkey); |
| 2813 | if (ret) |
| 2814 | ret = rsaencrypt(s->rsabuf, servkey.bytes, &hostkey); |
| 2815 | } else { |
| 2816 | ret = rsaencrypt(s->rsabuf, 32, &hostkey); |
| 2817 | if (ret) |
| 2818 | ret = rsaencrypt(s->rsabuf, hostkey.bytes, &servkey); |
| 2819 | } |
| 2820 | if (!ret) { |
| 2821 | bombout(("SSH1 public key encryptions failed due to bad formatting")); |
| 2822 | crStop(0); |
| 2823 | } |
| 2824 | |
| 2825 | logevent("Encrypted session key"); |
| 2826 | |
| 2827 | { |
| 2828 | int cipher_chosen = 0, warn = 0; |
| 2829 | char *cipher_string = NULL; |
| 2830 | int i; |
| 2831 | for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) { |
| 2832 | int next_cipher = ssh->cfg.ssh_cipherlist[i]; |
| 2833 | if (next_cipher == CIPHER_WARN) { |
| 2834 | /* If/when we choose a cipher, warn about it */ |
| 2835 | warn = 1; |
| 2836 | } else if (next_cipher == CIPHER_AES) { |
| 2837 | /* XXX Probably don't need to mention this. */ |
| 2838 | logevent("AES not supported in SSH1, skipping"); |
| 2839 | } else { |
| 2840 | switch (next_cipher) { |
| 2841 | case CIPHER_3DES: s->cipher_type = SSH_CIPHER_3DES; |
| 2842 | cipher_string = "3DES"; break; |
| 2843 | case CIPHER_BLOWFISH: s->cipher_type = SSH_CIPHER_BLOWFISH; |
| 2844 | cipher_string = "Blowfish"; break; |
| 2845 | case CIPHER_DES: s->cipher_type = SSH_CIPHER_DES; |
| 2846 | cipher_string = "single-DES"; break; |
| 2847 | } |
| 2848 | if (s->supported_ciphers_mask & (1 << s->cipher_type)) |
| 2849 | cipher_chosen = 1; |
| 2850 | } |
| 2851 | } |
| 2852 | if (!cipher_chosen) { |
| 2853 | if ((s->supported_ciphers_mask & (1 << SSH_CIPHER_3DES)) == 0) |
| 2854 | bombout(("Server violates SSH 1 protocol by not " |
| 2855 | "supporting 3DES encryption")); |
| 2856 | else |
| 2857 | /* shouldn't happen */ |
| 2858 | bombout(("No supported ciphers found")); |
| 2859 | crStop(0); |
| 2860 | } |
| 2861 | |
| 2862 | /* Warn about chosen cipher if necessary. */ |
| 2863 | if (warn) { |
| 2864 | sk_set_frozen(ssh->s, 1); |
| 2865 | askalg(ssh->frontend, "cipher", cipher_string); |
| 2866 | sk_set_frozen(ssh->s, 0); |
| 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | switch (s->cipher_type) { |
| 2871 | case SSH_CIPHER_3DES: |
| 2872 | logevent("Using 3DES encryption"); |
| 2873 | break; |
| 2874 | case SSH_CIPHER_DES: |
| 2875 | logevent("Using single-DES encryption"); |
| 2876 | break; |
| 2877 | case SSH_CIPHER_BLOWFISH: |
| 2878 | logevent("Using Blowfish encryption"); |
| 2879 | break; |
| 2880 | } |
| 2881 | |
| 2882 | send_packet(ssh, SSH1_CMSG_SESSION_KEY, |
| 2883 | PKT_CHAR, s->cipher_type, |
| 2884 | PKT_DATA, cookie, 8, |
| 2885 | PKT_CHAR, (s->len * 8) >> 8, PKT_CHAR, (s->len * 8) & 0xFF, |
| 2886 | PKT_DATA, s->rsabuf, s->len, |
| 2887 | PKT_INT, ssh->v1_local_protoflags, PKT_END); |
| 2888 | |
| 2889 | logevent("Trying to enable encryption..."); |
| 2890 | |
| 2891 | sfree(s->rsabuf); |
| 2892 | |
| 2893 | ssh->cipher = (s->cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 : |
| 2894 | s->cipher_type == SSH_CIPHER_DES ? &ssh_des : |
| 2895 | &ssh_3des); |
| 2896 | ssh->v1_cipher_ctx = ssh->cipher->make_context(); |
| 2897 | ssh->cipher->sesskey(ssh->v1_cipher_ctx, ssh->session_key); |
| 2898 | logeventf(ssh, "Initialised %s encryption", ssh->cipher->text_name); |
| 2899 | |
| 2900 | ssh->crcda_ctx = crcda_make_context(); |
| 2901 | logevent("Installing CRC compensation attack detector"); |
| 2902 | |
| 2903 | if (servkey.modulus) { |
| 2904 | sfree(servkey.modulus); |
| 2905 | servkey.modulus = NULL; |
| 2906 | } |
| 2907 | if (servkey.exponent) { |
| 2908 | sfree(servkey.exponent); |
| 2909 | servkey.exponent = NULL; |
| 2910 | } |
| 2911 | if (hostkey.modulus) { |
| 2912 | sfree(hostkey.modulus); |
| 2913 | hostkey.modulus = NULL; |
| 2914 | } |
| 2915 | if (hostkey.exponent) { |
| 2916 | sfree(hostkey.exponent); |
| 2917 | hostkey.exponent = NULL; |
| 2918 | } |
| 2919 | crWaitUntil(pktin); |
| 2920 | |
| 2921 | if (pktin->type != SSH1_SMSG_SUCCESS) { |
| 2922 | bombout(("Encryption not successfully enabled")); |
| 2923 | crStop(0); |
| 2924 | } |
| 2925 | |
| 2926 | logevent("Successfully started encryption"); |
| 2927 | |
| 2928 | fflush(stdout); |
| 2929 | { |
| 2930 | if (!*ssh->cfg.username) { |
| 2931 | if (ssh_get_line && !ssh_getline_pw_only) { |
| 2932 | if (!ssh_get_line("login as: ", |
| 2933 | s->username, sizeof(s->username), FALSE)) { |
| 2934 | /* |
| 2935 | * get_line failed to get a username. |
| 2936 | * Terminate. |
| 2937 | */ |
| 2938 | logevent("No username provided. Abandoning session."); |
| 2939 | ssh->close_expected = TRUE; |
| 2940 | ssh_closing((Plug)ssh, NULL, 0, 0); |
| 2941 | crStop(1); |
| 2942 | } |
| 2943 | } else { |
| 2944 | int ret; /* need not be kept over crReturn */ |
| 2945 | c_write_str(ssh, "login as: "); |
| 2946 | ssh->send_ok = 1; |
| 2947 | |
| 2948 | setup_userpass_input(ssh, s->username, sizeof(s->username), 1); |
| 2949 | do { |
| 2950 | crWaitUntil(!pktin); |
| 2951 | ret = process_userpass_input(ssh, in, inlen); |
| 2952 | } while (ret == 0); |
| 2953 | if (ret < 0) |
| 2954 | cleanup_exit(0); |
| 2955 | c_write_str(ssh, "\r\n"); |
| 2956 | } |
| 2957 | } else { |
| 2958 | strncpy(s->username, ssh->cfg.username, sizeof(s->username)); |
| 2959 | s->username[sizeof(s->username)-1] = '\0'; |
| 2960 | } |
| 2961 | |
| 2962 | send_packet(ssh, SSH1_CMSG_USER, PKT_STR, s->username, PKT_END); |
| 2963 | { |
| 2964 | char userlog[22 + sizeof(s->username)]; |
| 2965 | sprintf(userlog, "Sent username \"%s\"", s->username); |
| 2966 | logevent(userlog); |
| 2967 | if (flags & FLAG_INTERACTIVE && |
| 2968 | (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) { |
| 2969 | strcat(userlog, "\r\n"); |
| 2970 | c_write_str(ssh, userlog); |
| 2971 | } |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | crWaitUntil(pktin); |
| 2976 | |
| 2977 | if ((ssh->remote_bugs & BUG_CHOKES_ON_RSA)) { |
| 2978 | /* We must not attempt PK auth. Pretend we've already tried it. */ |
| 2979 | s->tried_publickey = s->tried_agent = 1; |
| 2980 | } else { |
| 2981 | s->tried_publickey = s->tried_agent = 0; |
| 2982 | } |
| 2983 | s->tis_auth_refused = s->ccard_auth_refused = 0; |
| 2984 | /* Load the public half of ssh->cfg.keyfile so we notice if it's in Pageant */ |
| 2985 | if (!filename_is_null(ssh->cfg.keyfile)) { |
| 2986 | if (!rsakey_pubblob(&ssh->cfg.keyfile, |
| 2987 | &s->publickey_blob, &s->publickey_bloblen, NULL)) |
| 2988 | s->publickey_blob = NULL; |
| 2989 | } else |
| 2990 | s->publickey_blob = NULL; |
| 2991 | |
| 2992 | while (pktin->type == SSH1_SMSG_FAILURE) { |
| 2993 | s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD; |
| 2994 | |
| 2995 | if (agent_exists() && !s->tried_agent) { |
| 2996 | /* |
| 2997 | * Attempt RSA authentication using Pageant. |
| 2998 | */ |
| 2999 | void *r; |
| 3000 | |
| 3001 | s->authed = FALSE; |
| 3002 | s->tried_agent = 1; |
| 3003 | logevent("Pageant is running. Requesting keys."); |
| 3004 | |
| 3005 | /* Request the keys held by the agent. */ |
| 3006 | PUT_32BIT(s->request, 1); |
| 3007 | s->request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
| 3008 | if (!agent_query(s->request, 5, &r, &s->responselen, |
| 3009 | ssh_agent_callback, ssh)) { |
| 3010 | do { |
| 3011 | crReturn(0); |
| 3012 | if (pktin) { |
| 3013 | bombout(("Unexpected data from server while waiting" |
| 3014 | " for agent response")); |
| 3015 | crStop(0); |
| 3016 | } |
| 3017 | } while (pktin || inlen > 0); |
| 3018 | r = ssh->agent_response; |
| 3019 | s->responselen = ssh->agent_response_len; |
| 3020 | } |
| 3021 | s->response = (unsigned char *) r; |
| 3022 | if (s->response && s->responselen >= 5 && |
| 3023 | s->response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) { |
| 3024 | s->p = s->response + 5; |
| 3025 | s->nkeys = GET_32BIT(s->p); |
| 3026 | s->p += 4; |
| 3027 | { |
| 3028 | char buf[64]; |
| 3029 | sprintf(buf, "Pageant has %d SSH1 keys", s->nkeys); |
| 3030 | logevent(buf); |
| 3031 | } |
| 3032 | for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) { |
| 3033 | { |
| 3034 | char buf[64]; |
| 3035 | sprintf(buf, "Trying Pageant key #%d", s->keyi); |
| 3036 | logevent(buf); |
| 3037 | } |
| 3038 | if (s->publickey_blob && |
| 3039 | !memcmp(s->p, s->publickey_blob, |
| 3040 | s->publickey_bloblen)) { |
| 3041 | logevent("This key matches configured key file"); |
| 3042 | s->tried_publickey = 1; |
| 3043 | } |
| 3044 | s->p += 4; |
| 3045 | { |
| 3046 | int n, ok = FALSE; |
| 3047 | do { /* do while (0) to make breaking easy */ |
| 3048 | n = ssh1_read_bignum |
| 3049 | (s->p, s->responselen-(s->p-s->response), |
| 3050 | &s->key.exponent); |
| 3051 | if (n < 0) |
| 3052 | break; |
| 3053 | s->p += n; |
| 3054 | n = ssh1_read_bignum |
| 3055 | (s->p, s->responselen-(s->p-s->response), |
| 3056 | &s->key.modulus); |
| 3057 | if (n < 0) |
| 3058 | break; |
| 3059 | s->p += n; |
| 3060 | if (s->responselen - (s->p-s->response) < 4) |
| 3061 | break; |
| 3062 | s->commentlen = GET_32BIT(s->p); |
| 3063 | s->p += 4; |
| 3064 | if (s->responselen - (s->p-s->response) < |
| 3065 | s->commentlen) |
| 3066 | break; |
| 3067 | s->commentp = (char *)s->p; |
| 3068 | s->p += s->commentlen; |
| 3069 | ok = TRUE; |
| 3070 | } while (0); |
| 3071 | if (!ok) { |
| 3072 | logevent("Pageant key list packet was truncated"); |
| 3073 | break; |
| 3074 | } |
| 3075 | } |
| 3076 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
| 3077 | PKT_BIGNUM, s->key.modulus, PKT_END); |
| 3078 | crWaitUntil(pktin); |
| 3079 | if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
| 3080 | logevent("Key refused"); |
| 3081 | continue; |
| 3082 | } |
| 3083 | logevent("Received RSA challenge"); |
| 3084 | if ((s->challenge = ssh1_pkt_getmp(pktin)) == NULL) { |
| 3085 | bombout(("Server's RSA challenge was badly formatted")); |
| 3086 | crStop(0); |
| 3087 | } |
| 3088 | |
| 3089 | { |
| 3090 | char *agentreq, *q, *ret; |
| 3091 | void *vret; |
| 3092 | int len, retlen; |
| 3093 | len = 1 + 4; /* message type, bit count */ |
| 3094 | len += ssh1_bignum_length(s->key.exponent); |
| 3095 | len += ssh1_bignum_length(s->key.modulus); |
| 3096 | len += ssh1_bignum_length(s->challenge); |
| 3097 | len += 16; /* session id */ |
| 3098 | len += 4; /* response format */ |
| 3099 | agentreq = snewn(4 + len, char); |
| 3100 | PUT_32BIT(agentreq, len); |
| 3101 | q = agentreq + 4; |
| 3102 | *q++ = SSH1_AGENTC_RSA_CHALLENGE; |
| 3103 | PUT_32BIT(q, bignum_bitcount(s->key.modulus)); |
| 3104 | q += 4; |
| 3105 | q += ssh1_write_bignum(q, s->key.exponent); |
| 3106 | q += ssh1_write_bignum(q, s->key.modulus); |
| 3107 | q += ssh1_write_bignum(q, s->challenge); |
| 3108 | memcpy(q, s->session_id, 16); |
| 3109 | q += 16; |
| 3110 | PUT_32BIT(q, 1); /* response format */ |
| 3111 | if (!agent_query(agentreq, len + 4, &vret, &retlen, |
| 3112 | ssh_agent_callback, ssh)) { |
| 3113 | sfree(agentreq); |
| 3114 | do { |
| 3115 | crReturn(0); |
| 3116 | if (pktin) { |
| 3117 | bombout(("Unexpected data from server" |
| 3118 | " while waiting for agent" |
| 3119 | " response")); |
| 3120 | crStop(0); |
| 3121 | } |
| 3122 | } while (pktin || inlen > 0); |
| 3123 | vret = ssh->agent_response; |
| 3124 | retlen = ssh->agent_response_len; |
| 3125 | } else |
| 3126 | sfree(agentreq); |
| 3127 | ret = vret; |
| 3128 | if (ret) { |
| 3129 | if (ret[4] == SSH1_AGENT_RSA_RESPONSE) { |
| 3130 | logevent("Sending Pageant's response"); |
| 3131 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
| 3132 | PKT_DATA, ret + 5, 16, |
| 3133 | PKT_END); |
| 3134 | sfree(ret); |
| 3135 | crWaitUntil(pktin); |
| 3136 | if (pktin->type == SSH1_SMSG_SUCCESS) { |
| 3137 | logevent |
| 3138 | ("Pageant's response accepted"); |
| 3139 | if (flags & FLAG_VERBOSE) { |
| 3140 | c_write_str(ssh, "Authenticated using" |
| 3141 | " RSA key \""); |
| 3142 | c_write(ssh, s->commentp, |
| 3143 | s->commentlen); |
| 3144 | c_write_str(ssh, "\" from agent\r\n"); |
| 3145 | } |
| 3146 | s->authed = TRUE; |
| 3147 | } else |
| 3148 | logevent |
| 3149 | ("Pageant's response not accepted"); |
| 3150 | } else { |
| 3151 | logevent |
| 3152 | ("Pageant failed to answer challenge"); |
| 3153 | sfree(ret); |
| 3154 | } |
| 3155 | } else { |
| 3156 | logevent("No reply received from Pageant"); |
| 3157 | } |
| 3158 | } |
| 3159 | freebn(s->key.exponent); |
| 3160 | freebn(s->key.modulus); |
| 3161 | freebn(s->challenge); |
| 3162 | if (s->authed) |
| 3163 | break; |
| 3164 | } |
| 3165 | sfree(s->response); |
| 3166 | } |
| 3167 | if (s->authed) |
| 3168 | break; |
| 3169 | } |
| 3170 | if (!filename_is_null(ssh->cfg.keyfile) && !s->tried_publickey) |
| 3171 | s->pwpkt_type = SSH1_CMSG_AUTH_RSA; |
| 3172 | |
| 3173 | if (ssh->cfg.try_tis_auth && |
| 3174 | (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) && |
| 3175 | !s->tis_auth_refused) { |
| 3176 | s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE; |
| 3177 | logevent("Requested TIS authentication"); |
| 3178 | send_packet(ssh, SSH1_CMSG_AUTH_TIS, PKT_END); |
| 3179 | crWaitUntil(pktin); |
| 3180 | if (pktin->type != SSH1_SMSG_AUTH_TIS_CHALLENGE) { |
| 3181 | logevent("TIS authentication declined"); |
| 3182 | if (flags & FLAG_INTERACTIVE) |
| 3183 | c_write_str(ssh, "TIS authentication refused.\r\n"); |
| 3184 | s->tis_auth_refused = 1; |
| 3185 | continue; |
| 3186 | } else { |
| 3187 | char *challenge; |
| 3188 | int challengelen; |
| 3189 | |
| 3190 | ssh_pkt_getstring(pktin, &challenge, &challengelen); |
| 3191 | if (!challenge) { |
| 3192 | bombout(("TIS challenge packet was badly formed")); |
| 3193 | crStop(0); |
| 3194 | } |
| 3195 | logevent("Received TIS challenge"); |
| 3196 | if (challengelen > sizeof(s->prompt) - 1) |
| 3197 | challengelen = sizeof(s->prompt) - 1;/* prevent overrun */ |
| 3198 | memcpy(s->prompt, challenge, challengelen); |
| 3199 | /* Prompt heuristic comes from OpenSSH */ |
| 3200 | strncpy(s->prompt + challengelen, |
| 3201 | memchr(s->prompt, '\n', challengelen) ? |
| 3202 | "": "\r\nResponse: ", |
| 3203 | (sizeof s->prompt) - challengelen); |
| 3204 | s->prompt[(sizeof s->prompt) - 1] = '\0'; |
| 3205 | } |
| 3206 | } |
| 3207 | if (ssh->cfg.try_tis_auth && |
| 3208 | (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) && |
| 3209 | !s->ccard_auth_refused) { |
| 3210 | s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE; |
| 3211 | logevent("Requested CryptoCard authentication"); |
| 3212 | send_packet(ssh, SSH1_CMSG_AUTH_CCARD, PKT_END); |
| 3213 | crWaitUntil(pktin); |
| 3214 | if (pktin->type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
| 3215 | logevent("CryptoCard authentication declined"); |
| 3216 | c_write_str(ssh, "CryptoCard authentication refused.\r\n"); |
| 3217 | s->ccard_auth_refused = 1; |
| 3218 | continue; |
| 3219 | } else { |
| 3220 | char *challenge; |
| 3221 | int challengelen; |
| 3222 | |
| 3223 | ssh_pkt_getstring(pktin, &challenge, &challengelen); |
| 3224 | if (!challenge) { |
| 3225 | bombout(("CryptoCard challenge packet was badly formed")); |
| 3226 | crStop(0); |
| 3227 | } |
| 3228 | logevent("Received CryptoCard challenge"); |
| 3229 | if (challengelen > sizeof(s->prompt) - 1) |
| 3230 | challengelen = sizeof(s->prompt) - 1;/* prevent overrun */ |
| 3231 | memcpy(s->prompt, challenge, challengelen); |
| 3232 | strncpy(s->prompt + challengelen, |
| 3233 | memchr(s->prompt, '\n', challengelen) ? |
| 3234 | "" : "\r\nResponse: ", |
| 3235 | sizeof(s->prompt) - challengelen); |
| 3236 | s->prompt[sizeof(s->prompt) - 1] = '\0'; |
| 3237 | } |
| 3238 | } |
| 3239 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
| 3240 | sprintf(s->prompt, "%.90s@%.90s's password: ", |
| 3241 | s->username, ssh->savedhost); |
| 3242 | } |
| 3243 | if (s->pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
| 3244 | char *comment = NULL; |
| 3245 | int type; |
| 3246 | char msgbuf[256]; |
| 3247 | if (flags & FLAG_VERBOSE) |
| 3248 | c_write_str(ssh, "Trying public key authentication.\r\n"); |
| 3249 | logeventf(ssh, "Trying public key \"%s\"", |
| 3250 | filename_to_str(&ssh->cfg.keyfile)); |
| 3251 | type = key_type(&ssh->cfg.keyfile); |
| 3252 | if (type != SSH_KEYTYPE_SSH1) { |
| 3253 | sprintf(msgbuf, "Key is of wrong type (%s)", |
| 3254 | key_type_to_str(type)); |
| 3255 | logevent(msgbuf); |
| 3256 | c_write_str(ssh, msgbuf); |
| 3257 | c_write_str(ssh, "\r\n"); |
| 3258 | s->tried_publickey = 1; |
| 3259 | continue; |
| 3260 | } |
| 3261 | if (!rsakey_encrypted(&ssh->cfg.keyfile, &comment)) { |
| 3262 | if (flags & FLAG_VERBOSE) |
| 3263 | c_write_str(ssh, "No passphrase required.\r\n"); |
| 3264 | goto tryauth; |
| 3265 | } |
| 3266 | sprintf(s->prompt, "Passphrase for key \"%.100s\": ", comment); |
| 3267 | sfree(comment); |
| 3268 | } |
| 3269 | |
| 3270 | /* |
| 3271 | * Show password prompt, having first obtained it via a TIS |
| 3272 | * or CryptoCard exchange if we're doing TIS or CryptoCard |
| 3273 | * authentication. |
| 3274 | */ |
| 3275 | if (ssh_get_line) { |
| 3276 | if (!ssh_get_line(s->prompt, s->password, |
| 3277 | sizeof(s->password), TRUE)) { |
| 3278 | /* |
| 3279 | * get_line failed to get a password (for example |
| 3280 | * because one was supplied on the command line |
| 3281 | * which has already failed to work). Terminate. |
| 3282 | */ |
| 3283 | send_packet(ssh, SSH1_MSG_DISCONNECT, |
| 3284 | PKT_STR, "No more passwords available to try", |
| 3285 | PKT_END); |
| 3286 | logevent("Unable to authenticate"); |
| 3287 | connection_fatal(ssh->frontend, "Unable to authenticate"); |
| 3288 | ssh->close_expected = TRUE; |
| 3289 | ssh_closing((Plug)ssh, NULL, 0, 0); |
| 3290 | crStop(1); |
| 3291 | } |
| 3292 | } else { |
| 3293 | /* Prompt may have come from server. We've munged it a bit, so |
| 3294 | * we know it to be zero-terminated at least once. */ |
| 3295 | int ret; /* need not be saved over crReturn */ |
| 3296 | c_write_untrusted(ssh, s->prompt, strlen(s->prompt)); |
| 3297 | s->pos = 0; |
| 3298 | |
| 3299 | setup_userpass_input(ssh, s->password, sizeof(s->password), 0); |
| 3300 | do { |
| 3301 | crWaitUntil(!pktin); |
| 3302 | ret = process_userpass_input(ssh, in, inlen); |
| 3303 | } while (ret == 0); |
| 3304 | if (ret < 0) |
| 3305 | cleanup_exit(0); |
| 3306 | c_write_str(ssh, "\r\n"); |
| 3307 | } |
| 3308 | |
| 3309 | tryauth: |
| 3310 | if (s->pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
| 3311 | /* |
| 3312 | * Try public key authentication with the specified |
| 3313 | * key file. |
| 3314 | */ |
| 3315 | s->tried_publickey = 1; |
| 3316 | |
| 3317 | { |
| 3318 | const char *error = NULL; |
| 3319 | int ret = loadrsakey(&ssh->cfg.keyfile, &s->key, s->password, |
| 3320 | &error); |
| 3321 | if (ret == 0) { |
| 3322 | c_write_str(ssh, "Couldn't load private key from "); |
| 3323 | c_write_str(ssh, filename_to_str(&ssh->cfg.keyfile)); |
| 3324 | c_write_str(ssh, " ("); |
| 3325 | c_write_str(ssh, error); |
| 3326 | c_write_str(ssh, ").\r\n"); |
| 3327 | continue; /* go and try password */ |
| 3328 | } |
| 3329 | if (ret == -1) { |
| 3330 | c_write_str(ssh, "Wrong passphrase.\r\n"); |
| 3331 | s->tried_publickey = 0; |
| 3332 | continue; /* try again */ |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | /* |
| 3337 | * Send a public key attempt. |
| 3338 | */ |
| 3339 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
| 3340 | PKT_BIGNUM, s->key.modulus, PKT_END); |
| 3341 | |
| 3342 | crWaitUntil(pktin); |
| 3343 | if (pktin->type == SSH1_SMSG_FAILURE) { |
| 3344 | c_write_str(ssh, "Server refused our public key.\r\n"); |
| 3345 | continue; /* go and try password */ |
| 3346 | } |
| 3347 | if (pktin->type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
| 3348 | bombout(("Bizarre response to offer of public key")); |
| 3349 | crStop(0); |
| 3350 | } |
| 3351 | |
| 3352 | { |
| 3353 | int i; |
| 3354 | unsigned char buffer[32]; |
| 3355 | Bignum challenge, response; |
| 3356 | |
| 3357 | if ((challenge = ssh1_pkt_getmp(pktin)) == NULL) { |
| 3358 | bombout(("Server's RSA challenge was badly formatted")); |
| 3359 | crStop(0); |
| 3360 | } |
| 3361 | response = rsadecrypt(challenge, &s->key); |
| 3362 | freebn(s->key.private_exponent);/* burn the evidence */ |
| 3363 | |
| 3364 | for (i = 0; i < 32; i++) { |
| 3365 | buffer[i] = bignum_byte(response, 31 - i); |
| 3366 | } |
| 3367 | |
| 3368 | MD5Init(&md5c); |
| 3369 | MD5Update(&md5c, buffer, 32); |
| 3370 | MD5Update(&md5c, s->session_id, 16); |
| 3371 | MD5Final(buffer, &md5c); |
| 3372 | |
| 3373 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
| 3374 | PKT_DATA, buffer, 16, PKT_END); |
| 3375 | |
| 3376 | freebn(challenge); |
| 3377 | freebn(response); |
| 3378 | } |
| 3379 | |
| 3380 | crWaitUntil(pktin); |
| 3381 | if (pktin->type == SSH1_SMSG_FAILURE) { |
| 3382 | if (flags & FLAG_VERBOSE) |
| 3383 | c_write_str(ssh, "Failed to authenticate with" |
| 3384 | " our public key.\r\n"); |
| 3385 | continue; /* go and try password */ |
| 3386 | } else if (pktin->type != SSH1_SMSG_SUCCESS) { |
| 3387 | bombout(("Bizarre response to RSA authentication response")); |
| 3388 | crStop(0); |
| 3389 | } |
| 3390 | |
| 3391 | break; /* we're through! */ |
| 3392 | } else { |
| 3393 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
| 3394 | /* |
| 3395 | * Defence against traffic analysis: we send a |
| 3396 | * whole bunch of packets containing strings of |
| 3397 | * different lengths. One of these strings is the |
| 3398 | * password, in a SSH1_CMSG_AUTH_PASSWORD packet. |
| 3399 | * The others are all random data in |
| 3400 | * SSH1_MSG_IGNORE packets. This way a passive |
| 3401 | * listener can't tell which is the password, and |
| 3402 | * hence can't deduce the password length. |
| 3403 | * |
| 3404 | * Anybody with a password length greater than 16 |
| 3405 | * bytes is going to have enough entropy in their |
| 3406 | * password that a listener won't find it _that_ |
| 3407 | * much help to know how long it is. So what we'll |
| 3408 | * do is: |
| 3409 | * |
| 3410 | * - if password length < 16, we send 15 packets |
| 3411 | * containing string lengths 1 through 15 |
| 3412 | * |
| 3413 | * - otherwise, we let N be the nearest multiple |
| 3414 | * of 8 below the password length, and send 8 |
| 3415 | * packets containing string lengths N through |
| 3416 | * N+7. This won't obscure the order of |
| 3417 | * magnitude of the password length, but it will |
| 3418 | * introduce a bit of extra uncertainty. |
| 3419 | * |
| 3420 | * A few servers (the old 1.2.18 through 1.2.22) |
| 3421 | * can't deal with SSH1_MSG_IGNORE. For these |
| 3422 | * servers, we need an alternative defence. We make |
| 3423 | * use of the fact that the password is interpreted |
| 3424 | * as a C string: so we can append a NUL, then some |
| 3425 | * random data. |
| 3426 | * |
| 3427 | * One server (a Cisco one) can deal with neither |
| 3428 | * SSH1_MSG_IGNORE _nor_ a padded password string. |
| 3429 | * For this server we are left with no defences |
| 3430 | * against password length sniffing. |
| 3431 | */ |
| 3432 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) { |
| 3433 | /* |
| 3434 | * The server can deal with SSH1_MSG_IGNORE, so |
| 3435 | * we can use the primary defence. |
| 3436 | */ |
| 3437 | int bottom, top, pwlen, i; |
| 3438 | char *randomstr; |
| 3439 | |
| 3440 | pwlen = strlen(s->password); |
| 3441 | if (pwlen < 16) { |
| 3442 | bottom = 0; /* zero length passwords are OK! :-) */ |
| 3443 | top = 15; |
| 3444 | } else { |
| 3445 | bottom = pwlen & ~7; |
| 3446 | top = bottom + 7; |
| 3447 | } |
| 3448 | |
| 3449 | assert(pwlen >= bottom && pwlen <= top); |
| 3450 | |
| 3451 | randomstr = snewn(top + 1, char); |
| 3452 | |
| 3453 | for (i = bottom; i <= top; i++) { |
| 3454 | if (i == pwlen) { |
| 3455 | defer_packet(ssh, s->pwpkt_type, |
| 3456 | PKTT_PASSWORD, PKT_STR, s->password, |
| 3457 | PKTT_OTHER, PKT_END); |
| 3458 | } else { |
| 3459 | for (j = 0; j < i; j++) { |
| 3460 | do { |
| 3461 | randomstr[j] = random_byte(); |
| 3462 | } while (randomstr[j] == '\0'); |
| 3463 | } |
| 3464 | randomstr[i] = '\0'; |
| 3465 | defer_packet(ssh, SSH1_MSG_IGNORE, |
| 3466 | PKT_STR, randomstr, PKT_END); |
| 3467 | } |
| 3468 | } |
| 3469 | logevent("Sending password with camouflage packets"); |
| 3470 | ssh_pkt_defersend(ssh); |
| 3471 | sfree(randomstr); |
| 3472 | } |
| 3473 | else if (!(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) { |
| 3474 | /* |
| 3475 | * The server can't deal with SSH1_MSG_IGNORE |
| 3476 | * but can deal with padded passwords, so we |
| 3477 | * can use the secondary defence. |
| 3478 | */ |
| 3479 | char string[64]; |
| 3480 | char *ss; |
| 3481 | int len; |
| 3482 | |
| 3483 | len = strlen(s->password); |
| 3484 | if (len < sizeof(string)) { |
| 3485 | ss = string; |
| 3486 | strcpy(string, s->password); |
| 3487 | len++; /* cover the zero byte */ |
| 3488 | while (len < sizeof(string)) { |
| 3489 | string[len++] = (char) random_byte(); |
| 3490 | } |
| 3491 | } else { |
| 3492 | ss = s->password; |
| 3493 | } |
| 3494 | logevent("Sending length-padded password"); |
| 3495 | send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD, |
| 3496 | PKT_INT, len, PKT_DATA, ss, len, |
| 3497 | PKTT_OTHER, PKT_END); |
| 3498 | } else { |
| 3499 | /* |
| 3500 | * The server has _both_ |
| 3501 | * BUG_CHOKES_ON_SSH1_IGNORE and |
| 3502 | * BUG_NEEDS_SSH1_PLAIN_PASSWORD. There is |
| 3503 | * therefore nothing we can do. |
| 3504 | */ |
| 3505 | int len; |
| 3506 | len = strlen(s->password); |
| 3507 | logevent("Sending unpadded password"); |
| 3508 | send_packet(ssh, s->pwpkt_type, |
| 3509 | PKTT_PASSWORD, PKT_INT, len, |
| 3510 | PKT_DATA, s->password, len, |
| 3511 | PKTT_OTHER, PKT_END); |
| 3512 | } |
| 3513 | } else { |
| 3514 | send_packet(ssh, s->pwpkt_type, PKTT_PASSWORD, |
| 3515 | PKT_STR, s->password, PKTT_OTHER, PKT_END); |
| 3516 | } |
| 3517 | } |
| 3518 | logevent("Sent password"); |
| 3519 | memset(s->password, 0, strlen(s->password)); |
| 3520 | crWaitUntil(pktin); |
| 3521 | if (pktin->type == SSH1_SMSG_FAILURE) { |
| 3522 | if (flags & FLAG_VERBOSE) |
| 3523 | c_write_str(ssh, "Access denied\r\n"); |
| 3524 | logevent("Authentication refused"); |
| 3525 | } else if (pktin->type != SSH1_SMSG_SUCCESS) { |
| 3526 | bombout(("Strange packet received, type %d", pktin->type)); |
| 3527 | crStop(0); |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | logevent("Authentication successful"); |
| 3532 | |
| 3533 | crFinish(1); |
| 3534 | } |
| 3535 | |
| 3536 | void sshfwd_close(struct ssh_channel *c) |
| 3537 | { |
| 3538 | Ssh ssh = c->ssh; |
| 3539 | |
| 3540 | if (ssh->state != SSH_STATE_SESSION) { |
| 3541 | assert(ssh->state == SSH_STATE_CLOSED); |
| 3542 | return; |
| 3543 | } |
| 3544 | |
| 3545 | if (c && !c->closes) { |
| 3546 | /* |
| 3547 | * If the channel's remoteid is -1, we have sent |
| 3548 | * CHANNEL_OPEN for this channel, but it hasn't even been |
| 3549 | * acknowledged by the server. So we must set a close flag |
| 3550 | * on it now, and then when the server acks the channel |
| 3551 | * open, we can close it then. |
| 3552 | */ |
| 3553 | if (((int)c->remoteid) != -1) { |
| 3554 | if (ssh->version == 1) { |
| 3555 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, |
| 3556 | PKT_END); |
| 3557 | } else { |
| 3558 | struct Packet *pktout; |
| 3559 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
| 3560 | ssh2_pkt_adduint32(pktout, c->remoteid); |
| 3561 | ssh2_pkt_send(ssh, pktout); |
| 3562 | } |
| 3563 | } |
| 3564 | c->closes = 1; /* sent MSG_CLOSE */ |
| 3565 | if (c->type == CHAN_X11) { |
| 3566 | c->u.x11.s = NULL; |
| 3567 | logevent("Forwarded X11 connection terminated"); |
| 3568 | } else if (c->type == CHAN_SOCKDATA || |
| 3569 | c->type == CHAN_SOCKDATA_DORMANT) { |
| 3570 | c->u.pfd.s = NULL; |
| 3571 | logevent("Forwarded port closed"); |
| 3572 | } |
| 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | int sshfwd_write(struct ssh_channel *c, char *buf, int len) |
| 3577 | { |
| 3578 | Ssh ssh = c->ssh; |
| 3579 | |
| 3580 | if (ssh->state != SSH_STATE_SESSION) { |
| 3581 | assert(ssh->state == SSH_STATE_CLOSED); |
| 3582 | return 0; |
| 3583 | } |
| 3584 | |
| 3585 | if (ssh->version == 1) { |
| 3586 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
| 3587 | PKT_INT, c->remoteid, |
| 3588 | PKTT_DATA, |
| 3589 | PKT_INT, len, PKT_DATA, buf, len, |
| 3590 | PKTT_OTHER, PKT_END); |
| 3591 | /* |
| 3592 | * In SSH1 we can return 0 here - implying that forwarded |
| 3593 | * connections are never individually throttled - because |
| 3594 | * the only circumstance that can cause throttling will be |
| 3595 | * the whole SSH connection backing up, in which case |
| 3596 | * _everything_ will be throttled as a whole. |
| 3597 | */ |
| 3598 | return 0; |
| 3599 | } else { |
| 3600 | ssh2_add_channel_data(c, buf, len); |
| 3601 | return ssh2_try_send(c); |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | void sshfwd_unthrottle(struct ssh_channel *c, int bufsize) |
| 3606 | { |
| 3607 | Ssh ssh = c->ssh; |
| 3608 | |
| 3609 | if (ssh->state != SSH_STATE_SESSION) { |
| 3610 | assert(ssh->state == SSH_STATE_CLOSED); |
| 3611 | return; |
| 3612 | } |
| 3613 | |
| 3614 | if (ssh->version == 1) { |
| 3615 | if (c->v.v1.throttling && bufsize < SSH1_BUFFER_LIMIT) { |
| 3616 | c->v.v1.throttling = 0; |
| 3617 | ssh1_throttle(ssh, -1); |
| 3618 | } |
| 3619 | } else { |
| 3620 | ssh2_set_window(c, OUR_V2_WINSIZE - bufsize); |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | static void ssh_queueing_handler(Ssh ssh, struct Packet *pktin) |
| 3625 | { |
| 3626 | struct queued_handler *qh = ssh->qhead; |
| 3627 | |
| 3628 | assert(qh != NULL); |
| 3629 | |
| 3630 | assert(pktin->type == qh->msg1 || pktin->type == qh->msg2); |
| 3631 | |
| 3632 | if (qh->msg1 > 0) { |
| 3633 | assert(ssh->packet_dispatch[qh->msg1] == ssh_queueing_handler); |
| 3634 | ssh->packet_dispatch[qh->msg1] = NULL; |
| 3635 | } |
| 3636 | if (qh->msg2 > 0) { |
| 3637 | assert(ssh->packet_dispatch[qh->msg2] == ssh_queueing_handler); |
| 3638 | ssh->packet_dispatch[qh->msg2] = NULL; |
| 3639 | } |
| 3640 | |
| 3641 | if (qh->next) { |
| 3642 | ssh->qhead = qh->next; |
| 3643 | |
| 3644 | if (ssh->qhead->msg1 > 0) { |
| 3645 | assert(ssh->packet_dispatch[ssh->qhead->msg1] == NULL); |
| 3646 | ssh->packet_dispatch[ssh->qhead->msg1] = ssh_queueing_handler; |
| 3647 | } |
| 3648 | if (ssh->qhead->msg2 > 0) { |
| 3649 | assert(ssh->packet_dispatch[ssh->qhead->msg2] == NULL); |
| 3650 | ssh->packet_dispatch[ssh->qhead->msg2] = ssh_queueing_handler; |
| 3651 | } |
| 3652 | } else { |
| 3653 | ssh->qhead = ssh->qtail = NULL; |
| 3654 | ssh->packet_dispatch[pktin->type] = NULL; |
| 3655 | } |
| 3656 | |
| 3657 | qh->handler(ssh, pktin, qh->ctx); |
| 3658 | |
| 3659 | sfree(qh); |
| 3660 | } |
| 3661 | |
| 3662 | static void ssh_queue_handler(Ssh ssh, int msg1, int msg2, |
| 3663 | chandler_fn_t handler, void *ctx) |
| 3664 | { |
| 3665 | struct queued_handler *qh; |
| 3666 | |
| 3667 | qh = snew(struct queued_handler); |
| 3668 | qh->msg1 = msg1; |
| 3669 | qh->msg2 = msg2; |
| 3670 | qh->handler = handler; |
| 3671 | qh->ctx = ctx; |
| 3672 | qh->next = NULL; |
| 3673 | |
| 3674 | if (ssh->qtail == NULL) { |
| 3675 | ssh->qhead = qh; |
| 3676 | |
| 3677 | if (qh->msg1 > 0) { |
| 3678 | assert(ssh->packet_dispatch[qh->msg1] == NULL); |
| 3679 | ssh->packet_dispatch[qh->msg1] = ssh_queueing_handler; |
| 3680 | } |
| 3681 | if (qh->msg2 > 0) { |
| 3682 | assert(ssh->packet_dispatch[qh->msg2] == NULL); |
| 3683 | ssh->packet_dispatch[qh->msg2] = ssh_queueing_handler; |
| 3684 | } |
| 3685 | } else { |
| 3686 | ssh->qtail->next = qh; |
| 3687 | } |
| 3688 | ssh->qtail = qh; |
| 3689 | } |
| 3690 | |
| 3691 | static void ssh_rportfwd_succfail(Ssh ssh, struct Packet *pktin, void *ctx) |
| 3692 | { |
| 3693 | struct ssh_rportfwd *rpf, *pf = (struct ssh_rportfwd *)ctx; |
| 3694 | |
| 3695 | if (pktin->type == (ssh->version == 1 ? SSH1_SMSG_SUCCESS : |
| 3696 | SSH2_MSG_REQUEST_SUCCESS)) { |
| 3697 | logeventf(ssh, "Remote port forwarding from %s enabled", |
| 3698 | pf->sportdesc); |
| 3699 | } else { |
| 3700 | logeventf(ssh, "Remote port forwarding from %s refused", |
| 3701 | pf->sportdesc); |
| 3702 | |
| 3703 | rpf = del234(ssh->rportfwds, pf); |
| 3704 | assert(rpf == pf); |
| 3705 | free_rportfwd(pf); |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | static void ssh_setup_portfwd(Ssh ssh, const Config *cfg) |
| 3710 | { |
| 3711 | const char *portfwd_strptr = cfg->portfwd; |
| 3712 | struct ssh_portfwd *epf; |
| 3713 | int i; |
| 3714 | |
| 3715 | if (!ssh->portfwds) { |
| 3716 | ssh->portfwds = newtree234(ssh_portcmp); |
| 3717 | } else { |
| 3718 | /* |
| 3719 | * Go through the existing port forwardings and tag them |
| 3720 | * with status==DESTROY. Any that we want to keep will be |
| 3721 | * re-enabled (status==KEEP) as we go through the |
| 3722 | * configuration and find out which bits are the same as |
| 3723 | * they were before. |
| 3724 | */ |
| 3725 | struct ssh_portfwd *epf; |
| 3726 | int i; |
| 3727 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
| 3728 | epf->status = DESTROY; |
| 3729 | } |
| 3730 | |
| 3731 | while (*portfwd_strptr) { |
| 3732 | char address_family, type; |
| 3733 | int sport,dport,sserv,dserv; |
| 3734 | char sports[256], dports[256], saddr[256], host[256]; |
| 3735 | int n; |
| 3736 | |
| 3737 | address_family = 'A'; |
| 3738 | type = 'L'; |
| 3739 | if (*portfwd_strptr == 'A' || |
| 3740 | *portfwd_strptr == '4' || |
| 3741 | *portfwd_strptr == '6') |
| 3742 | address_family = *portfwd_strptr++; |
| 3743 | if (*portfwd_strptr == 'L' || |
| 3744 | *portfwd_strptr == 'R' || |
| 3745 | *portfwd_strptr == 'D') |
| 3746 | type = *portfwd_strptr++; |
| 3747 | |
| 3748 | saddr[0] = '\0'; |
| 3749 | |
| 3750 | n = 0; |
| 3751 | while (*portfwd_strptr && *portfwd_strptr != '\t') { |
| 3752 | if (*portfwd_strptr == ':') { |
| 3753 | /* |
| 3754 | * We've seen a colon in the middle of the |
| 3755 | * source port number. This means that |
| 3756 | * everything we've seen until now is the |
| 3757 | * source _address_, so we'll move it into |
| 3758 | * saddr and start sports from the beginning |
| 3759 | * again. |
| 3760 | */ |
| 3761 | portfwd_strptr++; |
| 3762 | sports[n] = '\0'; |
| 3763 | if (ssh->version == 1 && type == 'R') { |
| 3764 | logeventf(ssh, "SSH1 cannot handle remote source address " |
| 3765 | "spec \"%s\"; ignoring", sports); |
| 3766 | } else |
| 3767 | strcpy(saddr, sports); |
| 3768 | n = 0; |
| 3769 | } |
| 3770 | if (n < 255) sports[n++] = *portfwd_strptr++; |
| 3771 | } |
| 3772 | sports[n] = 0; |
| 3773 | if (type != 'D') { |
| 3774 | if (*portfwd_strptr == '\t') |
| 3775 | portfwd_strptr++; |
| 3776 | n = 0; |
| 3777 | while (*portfwd_strptr && *portfwd_strptr != ':') { |
| 3778 | if (n < 255) host[n++] = *portfwd_strptr++; |
| 3779 | } |
| 3780 | host[n] = 0; |
| 3781 | if (*portfwd_strptr == ':') |
| 3782 | portfwd_strptr++; |
| 3783 | n = 0; |
| 3784 | while (*portfwd_strptr) { |
| 3785 | if (n < 255) dports[n++] = *portfwd_strptr++; |
| 3786 | } |
| 3787 | dports[n] = 0; |
| 3788 | portfwd_strptr++; |
| 3789 | dport = atoi(dports); |
| 3790 | dserv = 0; |
| 3791 | if (dport == 0) { |
| 3792 | dserv = 1; |
| 3793 | dport = net_service_lookup(dports); |
| 3794 | if (!dport) { |
| 3795 | logeventf(ssh, "Service lookup failed for destination" |
| 3796 | " port \"%s\"", dports); |
| 3797 | } |
| 3798 | } |
| 3799 | } else { |
| 3800 | while (*portfwd_strptr) portfwd_strptr++; |
| 3801 | dport = dserv = -1; |
| 3802 | portfwd_strptr++; /* eat the NUL and move to next one */ |
| 3803 | } |
| 3804 | sport = atoi(sports); |
| 3805 | sserv = 0; |
| 3806 | if (sport == 0) { |
| 3807 | sserv = 1; |
| 3808 | sport = net_service_lookup(sports); |
| 3809 | if (!sport) { |
| 3810 | logeventf(ssh, "Service lookup failed for source" |
| 3811 | " port \"%s\"", sports); |
| 3812 | } |
| 3813 | } |
| 3814 | if (sport && dport) { |
| 3815 | /* Set up a description of the source port. */ |
| 3816 | struct ssh_portfwd *pfrec, *epfrec; |
| 3817 | |
| 3818 | pfrec = snew(struct ssh_portfwd); |
| 3819 | pfrec->type = type; |
| 3820 | pfrec->saddr = *saddr ? dupstr(saddr) : NULL; |
| 3821 | pfrec->sserv = sserv ? dupstr(sports) : NULL; |
| 3822 | pfrec->sport = sport; |
| 3823 | pfrec->daddr = *host ? dupstr(host) : NULL; |
| 3824 | pfrec->dserv = dserv ? dupstr(dports) : NULL; |
| 3825 | pfrec->dport = dport; |
| 3826 | pfrec->local = NULL; |
| 3827 | pfrec->remote = NULL; |
| 3828 | pfrec->addressfamily = (address_family == '4' ? ADDRTYPE_IPV4 : |
| 3829 | address_family == '6' ? ADDRTYPE_IPV6 : |
| 3830 | ADDRTYPE_UNSPEC); |
| 3831 | |
| 3832 | epfrec = add234(ssh->portfwds, pfrec); |
| 3833 | if (epfrec != pfrec) { |
| 3834 | /* |
| 3835 | * We already have a port forwarding with precisely |
| 3836 | * these parameters. Hence, no need to do anything; |
| 3837 | * simply tag the existing one as KEEP. |
| 3838 | */ |
| 3839 | epfrec->status = KEEP; |
| 3840 | free_portfwd(pfrec); |
| 3841 | } else { |
| 3842 | pfrec->status = CREATE; |
| 3843 | } |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | /* |
| 3848 | * Now go through and destroy any port forwardings which were |
| 3849 | * not re-enabled. |
| 3850 | */ |
| 3851 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
| 3852 | if (epf->status == DESTROY) { |
| 3853 | char *message; |
| 3854 | |
| 3855 | message = dupprintf("%s port forwarding from %s%s%d", |
| 3856 | epf->type == 'L' ? "local" : |
| 3857 | epf->type == 'R' ? "remote" : "dynamic", |
| 3858 | epf->saddr ? epf->saddr : "", |
| 3859 | epf->saddr ? ":" : "", |
| 3860 | epf->sport); |
| 3861 | |
| 3862 | if (epf->type != 'D') { |
| 3863 | char *msg2 = dupprintf("%s to %s:%d", message, |
| 3864 | epf->daddr, epf->dport); |
| 3865 | sfree(message); |
| 3866 | message = msg2; |
| 3867 | } |
| 3868 | |
| 3869 | logeventf(ssh, "Cancelling %s", message); |
| 3870 | sfree(message); |
| 3871 | |
| 3872 | if (epf->remote) { |
| 3873 | struct ssh_rportfwd *rpf = epf->remote; |
| 3874 | struct Packet *pktout; |
| 3875 | |
| 3876 | /* |
| 3877 | * Cancel the port forwarding at the server |
| 3878 | * end. |
| 3879 | */ |
| 3880 | if (ssh->version == 1) { |
| 3881 | /* |
| 3882 | * We cannot cancel listening ports on the |
| 3883 | * server side in SSH1! There's no message |
| 3884 | * to support it. Instead, we simply remove |
| 3885 | * the rportfwd record from the local end |
| 3886 | * so that any connections the server tries |
| 3887 | * to make on it are rejected. |
| 3888 | */ |
| 3889 | } else { |
| 3890 | pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST); |
| 3891 | ssh2_pkt_addstring(pktout, "cancel-tcpip-forward"); |
| 3892 | ssh2_pkt_addbool(pktout, 0);/* _don't_ want reply */ |
| 3893 | if (epf->saddr) { |
| 3894 | ssh2_pkt_addstring(pktout, epf->saddr); |
| 3895 | } else if (ssh->cfg.rport_acceptall) { |
| 3896 | ssh2_pkt_addstring(pktout, "0.0.0.0"); |
| 3897 | } else { |
| 3898 | ssh2_pkt_addstring(pktout, "127.0.0.1"); |
| 3899 | } |
| 3900 | ssh2_pkt_adduint32(pktout, epf->sport); |
| 3901 | ssh2_pkt_send(ssh, pktout); |
| 3902 | } |
| 3903 | |
| 3904 | del234(ssh->rportfwds, rpf); |
| 3905 | free_rportfwd(rpf); |
| 3906 | } else if (epf->local) { |
| 3907 | pfd_terminate(epf->local); |
| 3908 | } |
| 3909 | |
| 3910 | delpos234(ssh->portfwds, i); |
| 3911 | free_portfwd(epf); |
| 3912 | i--; /* so we don't skip one in the list */ |
| 3913 | } |
| 3914 | |
| 3915 | /* |
| 3916 | * And finally, set up any new port forwardings (status==CREATE). |
| 3917 | */ |
| 3918 | for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++) |
| 3919 | if (epf->status == CREATE) { |
| 3920 | char *sportdesc, *dportdesc; |
| 3921 | sportdesc = dupprintf("%s%s%s%s%d%s", |
| 3922 | epf->saddr ? epf->saddr : "", |
| 3923 | epf->saddr ? ":" : "", |
| 3924 | epf->sserv ? epf->sserv : "", |
| 3925 | epf->sserv ? "(" : "", |
| 3926 | epf->sport, |
| 3927 | epf->sserv ? ")" : ""); |
| 3928 | if (epf->type == 'D') { |
| 3929 | dportdesc = NULL; |
| 3930 | } else { |
| 3931 | dportdesc = dupprintf("%s:%s%s%d%s", |
| 3932 | epf->daddr, |
| 3933 | epf->dserv ? epf->dserv : "", |
| 3934 | epf->dserv ? "(" : "", |
| 3935 | epf->dport, |
| 3936 | epf->dserv ? ")" : ""); |
| 3937 | } |
| 3938 | |
| 3939 | if (epf->type == 'L') { |
| 3940 | const char *err = pfd_addforward(epf->daddr, epf->dport, |
| 3941 | epf->saddr, epf->sport, |
| 3942 | ssh, &ssh->cfg, |
| 3943 | &epf->local, |
| 3944 | epf->addressfamily); |
| 3945 | |
| 3946 | logeventf(ssh, "Local %sport %s forwarding to %s%s%s", |
| 3947 | epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " : |
| 3948 | epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "", |
| 3949 | sportdesc, dportdesc, |
| 3950 | err ? " failed: " : "", err ? err : ""); |
| 3951 | } else if (epf->type == 'D') { |
| 3952 | const char *err = pfd_addforward(NULL, -1, |
| 3953 | epf->saddr, epf->sport, |
| 3954 | ssh, &ssh->cfg, |
| 3955 | &epf->local, |
| 3956 | epf->addressfamily); |
| 3957 | |
| 3958 | logeventf(ssh, "Local %sport %s SOCKS dynamic forwarding%s%s", |
| 3959 | epf->addressfamily == ADDRTYPE_IPV4 ? "IPv4 " : |
| 3960 | epf->addressfamily == ADDRTYPE_IPV6 ? "IPv6 " : "", |
| 3961 | sportdesc, |
| 3962 | err ? " failed: " : "", err ? err : ""); |
| 3963 | } else { |
| 3964 | struct ssh_rportfwd *pf; |
| 3965 | |
| 3966 | /* |
| 3967 | * Ensure the remote port forwardings tree exists. |
| 3968 | */ |
| 3969 | if (!ssh->rportfwds) { |
| 3970 | if (ssh->version == 1) |
| 3971 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh1); |
| 3972 | else |
| 3973 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh2); |
| 3974 | } |
| 3975 | |
| 3976 | pf = snew(struct ssh_rportfwd); |
| 3977 | strncpy(pf->dhost, epf->daddr, lenof(pf->dhost)-1); |
| 3978 | pf->dhost[lenof(pf->dhost)-1] = '\0'; |
| 3979 | pf->dport = epf->dport; |
| 3980 | pf->sport = epf->sport; |
| 3981 | if (add234(ssh->rportfwds, pf) != pf) { |
| 3982 | logeventf(ssh, "Duplicate remote port forwarding to %s:%d", |
| 3983 | epf->daddr, epf->dport); |
| 3984 | sfree(pf); |
| 3985 | } else { |
| 3986 | logeventf(ssh, "Requesting remote port %s" |
| 3987 | " forward to %s", sportdesc, dportdesc); |
| 3988 | |
| 3989 | pf->sportdesc = sportdesc; |
| 3990 | sportdesc = NULL; |
| 3991 | epf->remote = pf; |
| 3992 | pf->pfrec = epf; |
| 3993 | |
| 3994 | if (ssh->version == 1) { |
| 3995 | send_packet(ssh, SSH1_CMSG_PORT_FORWARD_REQUEST, |
| 3996 | PKT_INT, epf->sport, |
| 3997 | PKT_STR, epf->daddr, |
| 3998 | PKT_INT, epf->dport, |
| 3999 | PKT_END); |
| 4000 | ssh_queue_handler(ssh, SSH1_SMSG_SUCCESS, |
| 4001 | SSH1_SMSG_FAILURE, |
| 4002 | ssh_rportfwd_succfail, pf); |
| 4003 | } else { |
| 4004 | struct Packet *pktout; |
| 4005 | pktout = ssh2_pkt_init(SSH2_MSG_GLOBAL_REQUEST); |
| 4006 | ssh2_pkt_addstring(pktout, "tcpip-forward"); |
| 4007 | ssh2_pkt_addbool(pktout, 1);/* want reply */ |
| 4008 | if (epf->saddr) { |
| 4009 | ssh2_pkt_addstring(pktout, epf->saddr); |
| 4010 | } else if (ssh->cfg.rport_acceptall) { |
| 4011 | ssh2_pkt_addstring(pktout, "0.0.0.0"); |
| 4012 | } else { |
| 4013 | ssh2_pkt_addstring(pktout, "127.0.0.1"); |
| 4014 | } |
| 4015 | ssh2_pkt_adduint32(pktout, epf->sport); |
| 4016 | ssh2_pkt_send(ssh, pktout); |
| 4017 | |
| 4018 | ssh_queue_handler(ssh, SSH2_MSG_REQUEST_SUCCESS, |
| 4019 | SSH2_MSG_REQUEST_FAILURE, |
| 4020 | ssh_rportfwd_succfail, pf); |
| 4021 | } |
| 4022 | } |
| 4023 | } |
| 4024 | sfree(sportdesc); |
| 4025 | sfree(dportdesc); |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | static void ssh1_smsg_stdout_stderr_data(Ssh ssh, struct Packet *pktin) |
| 4030 | { |
| 4031 | char *string; |
| 4032 | int stringlen, bufsize; |
| 4033 | |
| 4034 | ssh_pkt_getstring(pktin, &string, &stringlen); |
| 4035 | if (string == NULL) { |
| 4036 | bombout(("Incoming terminal data packet was badly formed")); |
| 4037 | return; |
| 4038 | } |
| 4039 | |
| 4040 | bufsize = from_backend(ssh->frontend, pktin->type == SSH1_SMSG_STDERR_DATA, |
| 4041 | string, stringlen); |
| 4042 | if (!ssh->v1_stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) { |
| 4043 | ssh->v1_stdout_throttling = 1; |
| 4044 | ssh1_throttle(ssh, +1); |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | static void ssh1_smsg_x11_open(Ssh ssh, struct Packet *pktin) |
| 4049 | { |
| 4050 | /* Remote side is trying to open a channel to talk to our |
| 4051 | * X-Server. Give them back a local channel number. */ |
| 4052 | struct ssh_channel *c; |
| 4053 | int remoteid = ssh_pkt_getuint32(pktin); |
| 4054 | |
| 4055 | logevent("Received X11 connect request"); |
| 4056 | /* Refuse if X11 forwarding is disabled. */ |
| 4057 | if (!ssh->X11_fwd_enabled) { |
| 4058 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
| 4059 | PKT_INT, remoteid, PKT_END); |
| 4060 | logevent("Rejected X11 connect request"); |
| 4061 | } else { |
| 4062 | c = snew(struct ssh_channel); |
| 4063 | c->ssh = ssh; |
| 4064 | |
| 4065 | if (x11_init(&c->u.x11.s, ssh->cfg.x11_display, c, |
| 4066 | ssh->x11auth, NULL, -1, &ssh->cfg) != NULL) { |
| 4067 | logevent("Opening X11 forward connection failed"); |
| 4068 | sfree(c); |
| 4069 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
| 4070 | PKT_INT, remoteid, PKT_END); |
| 4071 | } else { |
| 4072 | logevent |
| 4073 | ("Opening X11 forward connection succeeded"); |
| 4074 | c->remoteid = remoteid; |
| 4075 | c->localid = alloc_channel_id(ssh); |
| 4076 | c->closes = 0; |
| 4077 | c->v.v1.throttling = 0; |
| 4078 | c->type = CHAN_X11; /* identify channel type */ |
| 4079 | add234(ssh->channels, c); |
| 4080 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
| 4081 | PKT_INT, c->remoteid, PKT_INT, |
| 4082 | c->localid, PKT_END); |
| 4083 | logevent("Opened X11 forward channel"); |
| 4084 | } |
| 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin) |
| 4089 | { |
| 4090 | /* Remote side is trying to open a channel to talk to our |
| 4091 | * agent. Give them back a local channel number. */ |
| 4092 | struct ssh_channel *c; |
| 4093 | int remoteid = ssh_pkt_getuint32(pktin); |
| 4094 | |
| 4095 | /* Refuse if agent forwarding is disabled. */ |
| 4096 | if (!ssh->agentfwd_enabled) { |
| 4097 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
| 4098 | PKT_INT, remoteid, PKT_END); |
| 4099 | } else { |
| 4100 | c = snew(struct ssh_channel); |
| 4101 | c->ssh = ssh; |
| 4102 | c->remoteid = remoteid; |
| 4103 | c->localid = alloc_channel_id(ssh); |
| 4104 | c->closes = 0; |
| 4105 | c->v.v1.throttling = 0; |
| 4106 | c->type = CHAN_AGENT; /* identify channel type */ |
| 4107 | c->u.a.lensofar = 0; |
| 4108 | add234(ssh->channels, c); |
| 4109 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
| 4110 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
| 4111 | PKT_END); |
| 4112 | } |
| 4113 | } |
| 4114 | |
| 4115 | static void ssh1_msg_port_open(Ssh ssh, struct Packet *pktin) |
| 4116 | { |
| 4117 | /* Remote side is trying to open a channel to talk to a |
| 4118 | * forwarded port. Give them back a local channel number. */ |
| 4119 | struct ssh_channel *c; |
| 4120 | struct ssh_rportfwd pf, *pfp; |
| 4121 | int remoteid; |
| 4122 | int hostsize, port; |
| 4123 | char *host, buf[1024]; |
| 4124 | const char *e; |
| 4125 | c = snew(struct ssh_channel); |
| 4126 | c->ssh = ssh; |
| 4127 | |
| 4128 | remoteid = ssh_pkt_getuint32(pktin); |
| 4129 | ssh_pkt_getstring(pktin, &host, &hostsize); |
| 4130 | port = ssh_pkt_getuint32(pktin); |
| 4131 | |
| 4132 | if (hostsize >= lenof(pf.dhost)) |
| 4133 | hostsize = lenof(pf.dhost)-1; |
| 4134 | memcpy(pf.dhost, host, hostsize); |
| 4135 | pf.dhost[hostsize] = '\0'; |
| 4136 | pf.dport = port; |
| 4137 | pfp = find234(ssh->rportfwds, &pf, NULL); |
| 4138 | |
| 4139 | if (pfp == NULL) { |
| 4140 | sprintf(buf, "Rejected remote port open request for %s:%d", |
| 4141 | pf.dhost, port); |
| 4142 | logevent(buf); |
| 4143 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
| 4144 | PKT_INT, remoteid, PKT_END); |
| 4145 | } else { |
| 4146 | sprintf(buf, "Received remote port open request for %s:%d", |
| 4147 | pf.dhost, port); |
| 4148 | logevent(buf); |
| 4149 | e = pfd_newconnect(&c->u.pfd.s, pf.dhost, port, |
| 4150 | c, &ssh->cfg, pfp->pfrec->addressfamily); |
| 4151 | if (e != NULL) { |
| 4152 | char buf[256]; |
| 4153 | sprintf(buf, "Port open failed: %s", e); |
| 4154 | logevent(buf); |
| 4155 | sfree(c); |
| 4156 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
| 4157 | PKT_INT, remoteid, PKT_END); |
| 4158 | } else { |
| 4159 | c->remoteid = remoteid; |
| 4160 | c->localid = alloc_channel_id(ssh); |
| 4161 | c->closes = 0; |
| 4162 | c->v.v1.throttling = 0; |
| 4163 | c->type = CHAN_SOCKDATA; /* identify channel type */ |
| 4164 | add234(ssh->channels, c); |
| 4165 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
| 4166 | PKT_INT, c->remoteid, PKT_INT, |
| 4167 | c->localid, PKT_END); |
| 4168 | logevent("Forwarded port opened successfully"); |
| 4169 | } |
| 4170 | } |
| 4171 | } |
| 4172 | |
| 4173 | static void ssh1_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin) |
| 4174 | { |
| 4175 | unsigned int remoteid = ssh_pkt_getuint32(pktin); |
| 4176 | unsigned int localid = ssh_pkt_getuint32(pktin); |
| 4177 | struct ssh_channel *c; |
| 4178 | |
| 4179 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
| 4180 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
| 4181 | c->remoteid = localid; |
| 4182 | c->type = CHAN_SOCKDATA; |
| 4183 | c->v.v1.throttling = 0; |
| 4184 | pfd_confirm(c->u.pfd.s); |
| 4185 | } |
| 4186 | |
| 4187 | if (c && c->closes) { |
| 4188 | /* |
| 4189 | * We have a pending close on this channel, |
| 4190 | * which we decided on before the server acked |
| 4191 | * the channel open. So now we know the |
| 4192 | * remoteid, we can close it again. |
| 4193 | */ |
| 4194 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, |
| 4195 | PKT_INT, c->remoteid, PKT_END); |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | static void ssh1_msg_channel_open_failure(Ssh ssh, struct Packet *pktin) |
| 4200 | { |
| 4201 | unsigned int remoteid = ssh_pkt_getuint32(pktin); |
| 4202 | struct ssh_channel *c; |
| 4203 | |
| 4204 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
| 4205 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
| 4206 | logevent("Forwarded connection refused by server"); |
| 4207 | pfd_close(c->u.pfd.s); |
| 4208 | del234(ssh->channels, c); |
| 4209 | sfree(c); |
| 4210 | } |
| 4211 | } |
| 4212 | |
| 4213 | static void ssh1_msg_channel_close(Ssh ssh, struct Packet *pktin) |
| 4214 | { |
| 4215 | /* Remote side closes a channel. */ |
| 4216 | unsigned i = ssh_pkt_getuint32(pktin); |
| 4217 | struct ssh_channel *c; |
| 4218 | c = find234(ssh->channels, &i, ssh_channelfind); |
| 4219 | if (c && ((int)c->remoteid) != -1) { |
| 4220 | int closetype; |
| 4221 | closetype = |
| 4222 | (pktin->type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2); |
| 4223 | |
| 4224 | if ((c->closes == 0) && (c->type == CHAN_X11)) { |
| 4225 | logevent("Forwarded X11 connection terminated"); |
| 4226 | assert(c->u.x11.s != NULL); |
| 4227 | x11_close(c->u.x11.s); |
| 4228 | c->u.x11.s = NULL; |
| 4229 | } |
| 4230 | if ((c->closes == 0) && (c->type == CHAN_SOCKDATA)) { |
| 4231 | logevent("Forwarded port closed"); |
| 4232 | assert(c->u.pfd.s != NULL); |
| 4233 | pfd_close(c->u.pfd.s); |
| 4234 | c->u.pfd.s = NULL; |
| 4235 | } |
| 4236 | |
| 4237 | c->closes |= (closetype << 2); /* seen this message */ |
| 4238 | if (!(c->closes & closetype)) { |
| 4239 | send_packet(ssh, pktin->type, PKT_INT, c->remoteid, |
| 4240 | PKT_END); |
| 4241 | c->closes |= closetype; /* sent it too */ |
| 4242 | } |
| 4243 | |
| 4244 | if (c->closes == 15) { |
| 4245 | del234(ssh->channels, c); |
| 4246 | sfree(c); |
| 4247 | } |
| 4248 | } else { |
| 4249 | bombout(("Received CHANNEL_CLOSE%s for %s channel %d\n", |
| 4250 | pktin->type == SSH1_MSG_CHANNEL_CLOSE ? "" : |
| 4251 | "_CONFIRMATION", c ? "half-open" : "nonexistent", |
| 4252 | i)); |
| 4253 | } |
| 4254 | } |
| 4255 | |
| 4256 | static void ssh1_msg_channel_data(Ssh ssh, struct Packet *pktin) |
| 4257 | { |
| 4258 | /* Data sent down one of our channels. */ |
| 4259 | int i = ssh_pkt_getuint32(pktin); |
| 4260 | char *p; |
| 4261 | unsigned int len; |
| 4262 | struct ssh_channel *c; |
| 4263 | |
| 4264 | ssh_pkt_getstring(pktin, &p, &len); |
| 4265 | |
| 4266 | c = find234(ssh->channels, &i, ssh_channelfind); |
| 4267 | if (c) { |
| 4268 | int bufsize = 0; |
| 4269 | switch (c->type) { |
| 4270 | case CHAN_X11: |
| 4271 | bufsize = x11_send(c->u.x11.s, p, len); |
| 4272 | break; |
| 4273 | case CHAN_SOCKDATA: |
| 4274 | bufsize = pfd_send(c->u.pfd.s, p, len); |
| 4275 | break; |
| 4276 | case CHAN_AGENT: |
| 4277 | /* Data for an agent message. Buffer it. */ |
| 4278 | while (len > 0) { |
| 4279 | if (c->u.a.lensofar < 4) { |
| 4280 | unsigned int l = min(4 - c->u.a.lensofar, len); |
| 4281 | memcpy(c->u.a.msglen + c->u.a.lensofar, p, |
| 4282 | l); |
| 4283 | p += l; |
| 4284 | len -= l; |
| 4285 | c->u.a.lensofar += l; |
| 4286 | } |
| 4287 | if (c->u.a.lensofar == 4) { |
| 4288 | c->u.a.totallen = |
| 4289 | 4 + GET_32BIT(c->u.a.msglen); |
| 4290 | c->u.a.message = snewn(c->u.a.totallen, |
| 4291 | unsigned char); |
| 4292 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
| 4293 | } |
| 4294 | if (c->u.a.lensofar >= 4 && len > 0) { |
| 4295 | unsigned int l = |
| 4296 | min(c->u.a.totallen - c->u.a.lensofar, |
| 4297 | len); |
| 4298 | memcpy(c->u.a.message + c->u.a.lensofar, p, |
| 4299 | l); |
| 4300 | p += l; |
| 4301 | len -= l; |
| 4302 | c->u.a.lensofar += l; |
| 4303 | } |
| 4304 | if (c->u.a.lensofar == c->u.a.totallen) { |
| 4305 | void *reply; |
| 4306 | int replylen; |
| 4307 | if (agent_query(c->u.a.message, |
| 4308 | c->u.a.totallen, |
| 4309 | &reply, &replylen, |
| 4310 | ssh_agentf_callback, c)) |
| 4311 | ssh_agentf_callback(c, reply, replylen); |
| 4312 | sfree(c->u.a.message); |
| 4313 | c->u.a.lensofar = 0; |
| 4314 | } |
| 4315 | } |
| 4316 | bufsize = 0; /* agent channels never back up */ |
| 4317 | break; |
| 4318 | } |
| 4319 | if (!c->v.v1.throttling && bufsize > SSH1_BUFFER_LIMIT) { |
| 4320 | c->v.v1.throttling = 1; |
| 4321 | ssh1_throttle(ssh, +1); |
| 4322 | } |
| 4323 | } |
| 4324 | } |
| 4325 | |
| 4326 | static void ssh1_smsg_exit_status(Ssh ssh, struct Packet *pktin) |
| 4327 | { |
| 4328 | char buf[100]; |
| 4329 | ssh->exitcode = ssh_pkt_getuint32(pktin); |
| 4330 | sprintf(buf, "Server sent command exit status %d", |
| 4331 | ssh->exitcode); |
| 4332 | logevent(buf); |
| 4333 | send_packet(ssh, SSH1_CMSG_EXIT_CONFIRMATION, PKT_END); |
| 4334 | /* |
| 4335 | * In case `helpful' firewalls or proxies tack |
| 4336 | * extra human-readable text on the end of the |
| 4337 | * session which we might mistake for another |
| 4338 | * encrypted packet, we close the session once |
| 4339 | * we've sent EXIT_CONFIRMATION. |
| 4340 | */ |
| 4341 | ssh->close_expected = TRUE; |
| 4342 | ssh_closing((Plug)ssh, NULL, 0, 0); |
| 4343 | } |
| 4344 | |
| 4345 | static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen, |
| 4346 | struct Packet *pktin) |
| 4347 | { |
| 4348 | crBegin(ssh->do_ssh1_connection_crstate); |
| 4349 | |
| 4350 | ssh->packet_dispatch[SSH1_SMSG_STDOUT_DATA] = |
| 4351 | ssh->packet_dispatch[SSH1_SMSG_STDERR_DATA] = |
| 4352 | ssh1_smsg_stdout_stderr_data; |
| 4353 | |
| 4354 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_CONFIRMATION] = |
| 4355 | ssh1_msg_channel_open_confirmation; |
| 4356 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_FAILURE] = |
| 4357 | ssh1_msg_channel_open_failure; |
| 4358 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE] = |
| 4359 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION] = |
| 4360 | ssh1_msg_channel_close; |
| 4361 | ssh->packet_dispatch[SSH1_MSG_CHANNEL_DATA] = ssh1_msg_channel_data; |
| 4362 | ssh->packet_dispatch[SSH1_SMSG_EXIT_STATUS] = ssh1_smsg_exit_status; |
| 4363 | |
| 4364 | if (ssh->cfg.agentfwd && agent_exists()) { |
| 4365 | logevent("Requesting agent forwarding"); |
| 4366 | send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END); |
| 4367 | do { |
| 4368 | crReturnV; |
| 4369 | } while (!pktin); |
| 4370 | if (pktin->type != SSH1_SMSG_SUCCESS |
| 4371 | && pktin->type != SSH1_SMSG_FAILURE) { |
| 4372 | bombout(("Protocol confusion")); |
| 4373 | crStopV; |
| 4374 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
| 4375 | logevent("Agent forwarding refused"); |
| 4376 | } else { |
| 4377 | logevent("Agent forwarding enabled"); |
| 4378 | ssh->agentfwd_enabled = TRUE; |
| 4379 | ssh->packet_dispatch[SSH1_SMSG_AGENT_OPEN] = ssh1_smsg_agent_open; |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | if (ssh->cfg.x11_forward) { |
| 4384 | char proto[20], data[64]; |
| 4385 | logevent("Requesting X11 forwarding"); |
| 4386 | ssh->x11auth = x11_invent_auth(proto, sizeof(proto), |
| 4387 | data, sizeof(data), ssh->cfg.x11_auth); |
| 4388 | x11_get_real_auth(ssh->x11auth, ssh->cfg.x11_display); |
| 4389 | if (ssh->v1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) { |
| 4390 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
| 4391 | PKT_STR, proto, PKT_STR, data, |
| 4392 | PKT_INT, x11_get_screen_number(ssh->cfg.x11_display), |
| 4393 | PKT_END); |
| 4394 | } else { |
| 4395 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
| 4396 | PKT_STR, proto, PKT_STR, data, PKT_END); |
| 4397 | } |
| 4398 | do { |
| 4399 | crReturnV; |
| 4400 | } while (!pktin); |
| 4401 | if (pktin->type != SSH1_SMSG_SUCCESS |
| 4402 | && pktin->type != SSH1_SMSG_FAILURE) { |
| 4403 | bombout(("Protocol confusion")); |
| 4404 | crStopV; |
| 4405 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
| 4406 | logevent("X11 forwarding refused"); |
| 4407 | } else { |
| 4408 | logevent("X11 forwarding enabled"); |
| 4409 | ssh->X11_fwd_enabled = TRUE; |
| 4410 | ssh->packet_dispatch[SSH1_SMSG_X11_OPEN] = ssh1_smsg_x11_open; |
| 4411 | } |
| 4412 | } |
| 4413 | |
| 4414 | ssh_setup_portfwd(ssh, &ssh->cfg); |
| 4415 | ssh->packet_dispatch[SSH1_MSG_PORT_OPEN] = ssh1_msg_port_open; |
| 4416 | |
| 4417 | if (!ssh->cfg.nopty) { |
| 4418 | /* Unpick the terminal-speed string. */ |
| 4419 | /* XXX perhaps we should allow no speeds to be sent. */ |
| 4420 | ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */ |
| 4421 | sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed); |
| 4422 | /* Send the pty request. */ |
| 4423 | send_packet(ssh, SSH1_CMSG_REQUEST_PTY, |
| 4424 | PKT_STR, ssh->cfg.termtype, |
| 4425 | PKT_INT, ssh->term_height, |
| 4426 | PKT_INT, ssh->term_width, |
| 4427 | PKT_INT, 0, PKT_INT, 0, /* width,height in pixels */ |
| 4428 | PKT_CHAR, 192, PKT_INT, ssh->ispeed, /* TTY_OP_ISPEED */ |
| 4429 | PKT_CHAR, 193, PKT_INT, ssh->ospeed, /* TTY_OP_OSPEED */ |
| 4430 | PKT_CHAR, 0, PKT_END); |
| 4431 | ssh->state = SSH_STATE_INTERMED; |
| 4432 | do { |
| 4433 | crReturnV; |
| 4434 | } while (!pktin); |
| 4435 | if (pktin->type != SSH1_SMSG_SUCCESS |
| 4436 | && pktin->type != SSH1_SMSG_FAILURE) { |
| 4437 | bombout(("Protocol confusion")); |
| 4438 | crStopV; |
| 4439 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
| 4440 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
| 4441 | ssh->editing = ssh->echoing = 1; |
| 4442 | } |
| 4443 | logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)", |
| 4444 | ssh->ospeed, ssh->ispeed); |
| 4445 | } else { |
| 4446 | ssh->editing = ssh->echoing = 1; |
| 4447 | } |
| 4448 | |
| 4449 | if (ssh->cfg.compression) { |
| 4450 | send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END); |
| 4451 | do { |
| 4452 | crReturnV; |
| 4453 | } while (!pktin); |
| 4454 | if (pktin->type != SSH1_SMSG_SUCCESS |
| 4455 | && pktin->type != SSH1_SMSG_FAILURE) { |
| 4456 | bombout(("Protocol confusion")); |
| 4457 | crStopV; |
| 4458 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
| 4459 | c_write_str(ssh, "Server refused to compress\r\n"); |
| 4460 | } |
| 4461 | logevent("Started compression"); |
| 4462 | ssh->v1_compressing = TRUE; |
| 4463 | ssh->cs_comp_ctx = zlib_compress_init(); |
| 4464 | logevent("Initialised zlib (RFC1950) compression"); |
| 4465 | ssh->sc_comp_ctx = zlib_decompress_init(); |
| 4466 | logevent("Initialised zlib (RFC1950) decompression"); |
| 4467 | } |
| 4468 | |
| 4469 | /* |
| 4470 | * Start the shell or command. |
| 4471 | * |
| 4472 | * Special case: if the first-choice command is an SSH2 |
| 4473 | * subsystem (hence not usable here) and the second choice |
| 4474 | * exists, we fall straight back to that. |
| 4475 | */ |
| 4476 | { |
| 4477 | char *cmd = ssh->cfg.remote_cmd_ptr; |
| 4478 | |
| 4479 | if (ssh->cfg.ssh_subsys && ssh->cfg.remote_cmd_ptr2) { |
| 4480 | cmd = ssh->cfg.remote_cmd_ptr2; |
| 4481 | ssh->fallback_cmd = TRUE; |
| 4482 | } |
| 4483 | if (*cmd) |
| 4484 | send_packet(ssh, SSH1_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END); |
| 4485 | else |
| 4486 | send_packet(ssh, SSH1_CMSG_EXEC_SHELL, PKT_END); |
| 4487 | logevent("Started session"); |
| 4488 | } |
| 4489 | |
| 4490 | ssh->state = SSH_STATE_SESSION; |
| 4491 | if (ssh->size_needed) |
| 4492 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
| 4493 | if (ssh->eof_needed) |
| 4494 | ssh_special(ssh, TS_EOF); |
| 4495 | |
| 4496 | if (ssh->ldisc) |
| 4497 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
| 4498 | ssh->send_ok = 1; |
| 4499 | ssh->channels = newtree234(ssh_channelcmp); |
| 4500 | while (1) { |
| 4501 | |
| 4502 | /* |
| 4503 | * By this point, most incoming packets are already being |
| 4504 | * handled by the dispatch table, and we need only pay |
| 4505 | * attention to the unusual ones. |
| 4506 | */ |
| 4507 | |
| 4508 | crReturnV; |
| 4509 | if (pktin) { |
| 4510 | if (pktin->type == SSH1_SMSG_SUCCESS) { |
| 4511 | /* may be from EXEC_SHELL on some servers */ |
| 4512 | } else if (pktin->type == SSH1_SMSG_FAILURE) { |
| 4513 | /* may be from EXEC_SHELL on some servers |
| 4514 | * if no pty is available or in other odd cases. Ignore */ |
| 4515 | } else { |
| 4516 | bombout(("Strange packet received: type %d", pktin->type)); |
| 4517 | crStopV; |
| 4518 | } |
| 4519 | } else { |
| 4520 | while (inlen > 0) { |
| 4521 | int len = min(inlen, 512); |
| 4522 | send_packet(ssh, SSH1_CMSG_STDIN_DATA, PKTT_DATA, |
| 4523 | PKT_INT, len, PKT_DATA, in, len, |
| 4524 | PKTT_OTHER, PKT_END); |
| 4525 | in += len; |
| 4526 | inlen -= len; |
| 4527 | } |
| 4528 | } |
| 4529 | } |
| 4530 | |
| 4531 | crFinishV; |
| 4532 | } |
| 4533 | |
| 4534 | /* |
| 4535 | * Handle the top-level SSH2 protocol. |
| 4536 | */ |
| 4537 | static void ssh1_msg_debug(Ssh ssh, struct Packet *pktin) |
| 4538 | { |
| 4539 | char *buf, *msg; |
| 4540 | int msglen; |
| 4541 | |
| 4542 | ssh_pkt_getstring(pktin, &msg, &msglen); |
| 4543 | buf = dupprintf("Remote debug message: %.*s", msglen, msg); |
| 4544 | logevent(buf); |
| 4545 | sfree(buf); |
| 4546 | } |
| 4547 | |
| 4548 | static void ssh1_msg_disconnect(Ssh ssh, struct Packet *pktin) |
| 4549 | { |
| 4550 | /* log reason code in disconnect message */ |
| 4551 | char *msg; |
| 4552 | int msglen; |
| 4553 | |
| 4554 | ssh_pkt_getstring(pktin, &msg, &msglen); |
| 4555 | bombout(("Server sent disconnect message:\n\"%.*s\"", msglen, msg)); |
| 4556 | } |
| 4557 | |
| 4558 | void ssh_msg_ignore(Ssh ssh, struct Packet *pktin) |
| 4559 | { |
| 4560 | /* Do nothing, because we're ignoring it! Duhh. */ |
| 4561 | } |
| 4562 | |
| 4563 | static void ssh1_protocol_setup(Ssh ssh) |
| 4564 | { |
| 4565 | int i; |
| 4566 | |
| 4567 | /* |
| 4568 | * Most messages are handled by the coroutines. |
| 4569 | */ |
| 4570 | for (i = 0; i < 256; i++) |
| 4571 | ssh->packet_dispatch[i] = NULL; |
| 4572 | |
| 4573 | /* |
| 4574 | * These special message types we install handlers for. |
| 4575 | */ |
| 4576 | ssh->packet_dispatch[SSH1_MSG_DISCONNECT] = ssh1_msg_disconnect; |
| 4577 | ssh->packet_dispatch[SSH1_MSG_IGNORE] = ssh_msg_ignore; |
| 4578 | ssh->packet_dispatch[SSH1_MSG_DEBUG] = ssh1_msg_debug; |
| 4579 | } |
| 4580 | |
| 4581 | static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, |
| 4582 | struct Packet *pktin) |
| 4583 | { |
| 4584 | if (ssh->state == SSH_STATE_CLOSED) |
| 4585 | return; |
| 4586 | |
| 4587 | if (pktin && ssh->packet_dispatch[pktin->type]) { |
| 4588 | ssh->packet_dispatch[pktin->type](ssh, pktin); |
| 4589 | return; |
| 4590 | } |
| 4591 | |
| 4592 | if (!ssh->protocol_initial_phase_done) { |
| 4593 | if (do_ssh1_login(ssh, in, inlen, pktin)) |
| 4594 | ssh->protocol_initial_phase_done = TRUE; |
| 4595 | else |
| 4596 | return; |
| 4597 | } |
| 4598 | |
| 4599 | do_ssh1_connection(ssh, in, inlen, pktin); |
| 4600 | } |
| 4601 | |
| 4602 | /* |
| 4603 | * Utility routine for decoding comma-separated strings in KEXINIT. |
| 4604 | */ |
| 4605 | static int in_commasep_string(char *needle, char *haystack, int haylen) |
| 4606 | { |
| 4607 | int needlen; |
| 4608 | if (!needle || !haystack) /* protect against null pointers */ |
| 4609 | return 0; |
| 4610 | needlen = strlen(needle); |
| 4611 | while (1) { |
| 4612 | /* |
| 4613 | * Is it at the start of the string? |
| 4614 | */ |
| 4615 | if (haylen >= needlen && /* haystack is long enough */ |
| 4616 | !memcmp(needle, haystack, needlen) && /* initial match */ |
| 4617 | (haylen == needlen || haystack[needlen] == ',') |
| 4618 | /* either , or EOS follows */ |
| 4619 | ) |
| 4620 | return 1; |
| 4621 | /* |
| 4622 | * If not, search for the next comma and resume after that. |
| 4623 | * If no comma found, terminate. |
| 4624 | */ |
| 4625 | while (haylen > 0 && *haystack != ',') |
| 4626 | haylen--, haystack++; |
| 4627 | if (haylen == 0) |
| 4628 | return 0; |
| 4629 | haylen--, haystack++; /* skip over comma itself */ |
| 4630 | } |
| 4631 | } |
| 4632 | |
| 4633 | /* |
| 4634 | * SSH2 key creation method. |
| 4635 | */ |
| 4636 | static void ssh2_mkkey(Ssh ssh, Bignum K, unsigned char *H, |
| 4637 | unsigned char *sessid, char chr, |
| 4638 | unsigned char *keyspace) |
| 4639 | { |
| 4640 | SHA_State s; |
| 4641 | /* First 20 bytes. */ |
| 4642 | SHA_Init(&s); |
| 4643 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
| 4644 | sha_mpint(&s, K); |
| 4645 | SHA_Bytes(&s, H, 20); |
| 4646 | SHA_Bytes(&s, &chr, 1); |
| 4647 | SHA_Bytes(&s, sessid, 20); |
| 4648 | SHA_Final(&s, keyspace); |
| 4649 | /* Next 20 bytes. */ |
| 4650 | SHA_Init(&s); |
| 4651 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
| 4652 | sha_mpint(&s, K); |
| 4653 | SHA_Bytes(&s, H, 20); |
| 4654 | SHA_Bytes(&s, keyspace, 20); |
| 4655 | SHA_Final(&s, keyspace + 20); |
| 4656 | } |
| 4657 | |
| 4658 | /* |
| 4659 | * Handle the SSH2 transport layer. |
| 4660 | */ |
| 4661 | static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen, |
| 4662 | struct Packet *pktin) |
| 4663 | { |
| 4664 | struct do_ssh2_transport_state { |
| 4665 | int nbits, pbits, warn; |
| 4666 | Bignum p, g, e, f, K; |
| 4667 | int kex_init_value, kex_reply_value; |
| 4668 | const struct ssh_mac **maclist; |
| 4669 | int nmacs; |
| 4670 | const struct ssh2_cipher *cscipher_tobe; |
| 4671 | const struct ssh2_cipher *sccipher_tobe; |
| 4672 | const struct ssh_mac *csmac_tobe; |
| 4673 | const struct ssh_mac *scmac_tobe; |
| 4674 | const struct ssh_compress *cscomp_tobe; |
| 4675 | const struct ssh_compress *sccomp_tobe; |
| 4676 | char *hostkeydata, *sigdata, *keystr, *fingerprint; |
| 4677 | int hostkeylen, siglen; |
| 4678 | void *hkey; /* actual host key */ |
| 4679 | unsigned char exchange_hash[20]; |
| 4680 | int n_preferred_kex; |
| 4681 | const struct ssh_kex *preferred_kex[KEX_MAX]; |
| 4682 | int n_preferred_ciphers; |
| 4683 | const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX]; |
| 4684 | const struct ssh_compress *preferred_comp; |
| 4685 | int got_session_id, activated_authconn; |
| 4686 | struct Packet *pktout; |
| 4687 | }; |
| 4688 | crState(do_ssh2_transport_state); |
| 4689 | |
| 4690 | crBegin(ssh->do_ssh2_transport_crstate); |
| 4691 | |
| 4692 | s->cscipher_tobe = s->sccipher_tobe = NULL; |
| 4693 | s->csmac_tobe = s->scmac_tobe = NULL; |
| 4694 | s->cscomp_tobe = s->sccomp_tobe = NULL; |
| 4695 | |
| 4696 | s->got_session_id = s->activated_authconn = FALSE; |
| 4697 | |
| 4698 | /* |
| 4699 | * Be prepared to work around the buggy MAC problem. |
| 4700 | */ |
| 4701 | if (ssh->remote_bugs & BUG_SSH2_HMAC) |
| 4702 | s->maclist = buggymacs, s->nmacs = lenof(buggymacs); |
| 4703 | else |
| 4704 | s->maclist = macs, s->nmacs = lenof(macs); |
| 4705 | |
| 4706 | begin_key_exchange: |
| 4707 | ssh->pkt_ctx &= ~SSH2_PKTCTX_KEX_MASK; |
| 4708 | { |
| 4709 | int i, j, commalist_started; |
| 4710 | |
| 4711 | /* |
| 4712 | * Set up the preferred key exchange. (NULL => warn below here) |
| 4713 | */ |
| 4714 | s->n_preferred_kex = 0; |
| 4715 | for (i = 0; i < KEX_MAX; i++) { |
| 4716 | switch (ssh->cfg.ssh_kexlist[i]) { |
| 4717 | case KEX_DHGEX: |
| 4718 | s->preferred_kex[s->n_preferred_kex++] = |
| 4719 | &ssh_diffiehellman_gex; |
| 4720 | break; |
| 4721 | case KEX_DHGROUP14: |
| 4722 | s->preferred_kex[s->n_preferred_kex++] = |
| 4723 | &ssh_diffiehellman_group14; |
| 4724 | break; |
| 4725 | case KEX_DHGROUP1: |
| 4726 | s->preferred_kex[s->n_preferred_kex++] = |
| 4727 | &ssh_diffiehellman_group1; |
| 4728 | break; |
| 4729 | case CIPHER_WARN: |
| 4730 | /* Flag for later. Don't bother if it's the last in |
| 4731 | * the list. */ |
| 4732 | if (i < KEX_MAX - 1) { |
| 4733 | s->preferred_kex[s->n_preferred_kex++] = NULL; |
| 4734 | } |
| 4735 | break; |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | /* |
| 4740 | * Set up the preferred ciphers. (NULL => warn below here) |
| 4741 | */ |
| 4742 | s->n_preferred_ciphers = 0; |
| 4743 | for (i = 0; i < CIPHER_MAX; i++) { |
| 4744 | switch (ssh->cfg.ssh_cipherlist[i]) { |
| 4745 | case CIPHER_BLOWFISH: |
| 4746 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_blowfish; |
| 4747 | break; |
| 4748 | case CIPHER_DES: |
| 4749 | if (ssh->cfg.ssh2_des_cbc) { |
| 4750 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_des; |
| 4751 | } |
| 4752 | break; |
| 4753 | case CIPHER_3DES: |
| 4754 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_3des; |
| 4755 | break; |
| 4756 | case CIPHER_AES: |
| 4757 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_aes; |
| 4758 | break; |
| 4759 | case CIPHER_WARN: |
| 4760 | /* Flag for later. Don't bother if it's the last in |
| 4761 | * the list. */ |
| 4762 | if (i < CIPHER_MAX - 1) { |
| 4763 | s->preferred_ciphers[s->n_preferred_ciphers++] = NULL; |
| 4764 | } |
| 4765 | break; |
| 4766 | } |
| 4767 | } |
| 4768 | |
| 4769 | /* |
| 4770 | * Set up preferred compression. |
| 4771 | */ |
| 4772 | if (ssh->cfg.compression) |
| 4773 | s->preferred_comp = &ssh_zlib; |
| 4774 | else |
| 4775 | s->preferred_comp = &ssh_comp_none; |
| 4776 | |
| 4777 | /* |
| 4778 | * Enable queueing of outgoing auth- or connection-layer |
| 4779 | * packets while we are in the middle of a key exchange. |
| 4780 | */ |
| 4781 | ssh->queueing = TRUE; |
| 4782 | |
| 4783 | /* |
| 4784 | * Flag that KEX is in progress. |
| 4785 | */ |
| 4786 | ssh->kex_in_progress = TRUE; |
| 4787 | |
| 4788 | /* |
| 4789 | * Construct and send our key exchange packet. |
| 4790 | */ |
| 4791 | s->pktout = ssh2_pkt_init(SSH2_MSG_KEXINIT); |
| 4792 | for (i = 0; i < 16; i++) |
| 4793 | ssh2_pkt_addbyte(s->pktout, (unsigned char) random_byte()); |
| 4794 | /* List key exchange algorithms. */ |
| 4795 | ssh2_pkt_addstring_start(s->pktout); |
| 4796 | commalist_started = 0; |
| 4797 | for (i = 0; i < s->n_preferred_kex; i++) { |
| 4798 | const struct ssh_kex *k = s->preferred_kex[i]; |
| 4799 | if (!k) continue; /* warning flag */ |
| 4800 | if (commalist_started) |
| 4801 | ssh2_pkt_addstring_str(s->pktout, ","); |
| 4802 | ssh2_pkt_addstring_str(s->pktout, s->preferred_kex[i]->name); |
| 4803 | commalist_started = 1; |
| 4804 | } |
| 4805 | /* List server host key algorithms. */ |
| 4806 | ssh2_pkt_addstring_start(s->pktout); |
| 4807 | for (i = 0; i < lenof(hostkey_algs); i++) { |
| 4808 | ssh2_pkt_addstring_str(s->pktout, hostkey_algs[i]->name); |
| 4809 | if (i < lenof(hostkey_algs) - 1) |
| 4810 | ssh2_pkt_addstring_str(s->pktout, ","); |
| 4811 | } |
| 4812 | /* List client->server encryption algorithms. */ |
| 4813 | ssh2_pkt_addstring_start(s->pktout); |
| 4814 | commalist_started = 0; |
| 4815 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
| 4816 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
| 4817 | if (!c) continue; /* warning flag */ |
| 4818 | for (j = 0; j < c->nciphers; j++) { |
| 4819 | if (commalist_started) |
| 4820 | ssh2_pkt_addstring_str(s->pktout, ","); |
| 4821 | ssh2_pkt_addstring_str(s->pktout, c->list[j]->name); |
| 4822 | commalist_started = 1; |
| 4823 | } |
| 4824 | } |
| 4825 | /* List server->client encryption algorithms. */ |
| 4826 | ssh2_pkt_addstring_start(s->pktout); |
| 4827 | commalist_started = 0; |
| 4828 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
| 4829 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
| 4830 | if (!c) continue; /* warning flag */ |
| 4831 | for (j = 0; j < c->nciphers; j++) { |
| 4832 | if (commalist_started) |
| 4833 | ssh2_pkt_addstring_str(s->pktout, ","); |
| 4834 | ssh2_pkt_addstring_str(s->pktout, c->list[j]->name); |
| 4835 | commalist_started = 1; |
| 4836 | } |
| 4837 | } |
| 4838 | /* List client->server MAC algorithms. */ |
| 4839 | ssh2_pkt_addstring_start(s->pktout); |
| 4840 | for (i = 0; i < s->nmacs; i++) { |
| 4841 | ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name); |
| 4842 | if (i < s->nmacs - 1) |
| 4843 | ssh2_pkt_addstring_str(s->pktout, ","); |
| 4844 | } |
| 4845 | /* List server->client MAC algorithms. */ |
| 4846 | ssh2_pkt_addstring_start(s->pktout); |
| 4847 | for (i = 0; i < s->nmacs; i++) { |
| 4848 | ssh2_pkt_addstring_str(s->pktout, s->maclist[i]->name); |
| 4849 | if (i < s->nmacs - 1) |
| 4850 | ssh2_pkt_addstring_str(s->pktout, ","); |
| 4851 | } |
| 4852 | /* List client->server compression algorithms. */ |
| 4853 | ssh2_pkt_addstring_start(s->pktout); |
| 4854 | assert(lenof(compressions) > 1); |
| 4855 | ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name); |
| 4856 | for (i = 0; i < lenof(compressions); i++) { |
| 4857 | const struct ssh_compress *c = compressions[i]; |
| 4858 | if (c != s->preferred_comp) { |
| 4859 | ssh2_pkt_addstring_str(s->pktout, ","); |
| 4860 | ssh2_pkt_addstring_str(s->pktout, c->name); |
| 4861 | } |
| 4862 | } |
| 4863 | /* List server->client compression algorithms. */ |
| 4864 | ssh2_pkt_addstring_start(s->pktout); |
| 4865 | assert(lenof(compressions) > 1); |
| 4866 | ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name); |
| 4867 | for (i = 0; i < lenof(compressions); i++) { |
| 4868 | const struct ssh_compress *c = compressions[i]; |
| 4869 | if (c != s->preferred_comp) { |
| 4870 | ssh2_pkt_addstring_str(s->pktout, ","); |
| 4871 | ssh2_pkt_addstring_str(s->pktout, c->name); |
| 4872 | } |
| 4873 | } |
| 4874 | /* List client->server languages. Empty list. */ |
| 4875 | ssh2_pkt_addstring_start(s->pktout); |
| 4876 | /* List server->client languages. Empty list. */ |
| 4877 | ssh2_pkt_addstring_start(s->pktout); |
| 4878 | /* First KEX packet does _not_ follow, because we're not that brave. */ |
| 4879 | ssh2_pkt_addbool(s->pktout, FALSE); |
| 4880 | /* Reserved. */ |
| 4881 | ssh2_pkt_adduint32(s->pktout, 0); |
| 4882 | } |
| 4883 | |
| 4884 | ssh->exhash = ssh->exhashbase; |
| 4885 | sha_string(&ssh->exhash, s->pktout->data + 5, s->pktout->length - 5); |
| 4886 | |
| 4887 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
| 4888 | |
| 4889 | if (!pktin) |
| 4890 | crWaitUntil(pktin); |
| 4891 | if (pktin->length > 5) |
| 4892 | sha_string(&ssh->exhash, pktin->data + 5, pktin->length - 5); |
| 4893 | |
| 4894 | /* |
| 4895 | * Now examine the other side's KEXINIT to see what we're up |
| 4896 | * to. |
| 4897 | */ |
| 4898 | { |
| 4899 | char *str; |
| 4900 | int i, j, len; |
| 4901 | |
| 4902 | if (pktin->type != SSH2_MSG_KEXINIT) { |
| 4903 | bombout(("expected key exchange packet from server")); |
| 4904 | crStop(0); |
| 4905 | } |
| 4906 | ssh->kex = NULL; |
| 4907 | ssh->hostkey = NULL; |
| 4908 | s->cscipher_tobe = NULL; |
| 4909 | s->sccipher_tobe = NULL; |
| 4910 | s->csmac_tobe = NULL; |
| 4911 | s->scmac_tobe = NULL; |
| 4912 | s->cscomp_tobe = NULL; |
| 4913 | s->sccomp_tobe = NULL; |
| 4914 | pktin->savedpos += 16; /* skip garbage cookie */ |
| 4915 | ssh_pkt_getstring(pktin, &str, &len); /* key exchange algorithms */ |
| 4916 | s->warn = 0; |
| 4917 | for (i = 0; i < s->n_preferred_kex; i++) { |
| 4918 | const struct ssh_kex *k = s->preferred_kex[i]; |
| 4919 | if (!k) { |
| 4920 | s->warn = 1; |
| 4921 | } else if (in_commasep_string(k->name, str, len)) { |
| 4922 | ssh->kex = k; |
| 4923 | } |
| 4924 | if (ssh->kex) { |
| 4925 | if (s->warn) { |
| 4926 | sk_set_frozen(ssh->s, 1); |
| 4927 | askalg(ssh->frontend, "key-exchange algorithm", |
| 4928 | ssh->kex->name); |
| 4929 | sk_set_frozen(ssh->s, 0); |
| 4930 | } |
| 4931 | break; |
| 4932 | } |
| 4933 | } |
| 4934 | if (!ssh->kex) { |
| 4935 | bombout(("Couldn't agree a key exchange algorithm (available: %s)", |
| 4936 | str ? str : "(null)")); |
| 4937 | crStop(0); |
| 4938 | } |
| 4939 | ssh_pkt_getstring(pktin, &str, &len); /* host key algorithms */ |
| 4940 | for (i = 0; i < lenof(hostkey_algs); i++) { |
| 4941 | if (in_commasep_string(hostkey_algs[i]->name, str, len)) { |
| 4942 | ssh->hostkey = hostkey_algs[i]; |
| 4943 | break; |
| 4944 | } |
| 4945 | } |
| 4946 | ssh_pkt_getstring(pktin, &str, &len); /* client->server cipher */ |
| 4947 | s->warn = 0; |
| 4948 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
| 4949 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
| 4950 | if (!c) { |
| 4951 | s->warn = 1; |
| 4952 | } else { |
| 4953 | for (j = 0; j < c->nciphers; j++) { |
| 4954 | if (in_commasep_string(c->list[j]->name, str, len)) { |
| 4955 | s->cscipher_tobe = c->list[j]; |
| 4956 | break; |
| 4957 | } |
| 4958 | } |
| 4959 | } |
| 4960 | if (s->cscipher_tobe) { |
| 4961 | if (s->warn) { |
| 4962 | sk_set_frozen(ssh->s, 1); |
| 4963 | askalg(ssh->frontend, "client-to-server cipher", |
| 4964 | s->cscipher_tobe->name); |
| 4965 | sk_set_frozen(ssh->s, 0); |
| 4966 | } |
| 4967 | break; |
| 4968 | } |
| 4969 | } |
| 4970 | if (!s->cscipher_tobe) { |
| 4971 | bombout(("Couldn't agree a client-to-server cipher (available: %s)", |
| 4972 | str ? str : "(null)")); |
| 4973 | crStop(0); |
| 4974 | } |
| 4975 | |
| 4976 | ssh_pkt_getstring(pktin, &str, &len); /* server->client cipher */ |
| 4977 | s->warn = 0; |
| 4978 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
| 4979 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
| 4980 | if (!c) { |
| 4981 | s->warn = 1; |
| 4982 | } else { |
| 4983 | for (j = 0; j < c->nciphers; j++) { |
| 4984 | if (in_commasep_string(c->list[j]->name, str, len)) { |
| 4985 | s->sccipher_tobe = c->list[j]; |
| 4986 | break; |
| 4987 | } |
| 4988 | } |
| 4989 | } |
| 4990 | if (s->sccipher_tobe) { |
| 4991 | if (s->warn) { |
| 4992 | sk_set_frozen(ssh->s, 1); |
| 4993 | askalg(ssh->frontend, "server-to-client cipher", |
| 4994 | s->sccipher_tobe->name); |
| 4995 | sk_set_frozen(ssh->s, 0); |
| 4996 | } |
| 4997 | break; |
| 4998 | } |
| 4999 | } |
| 5000 | if (!s->sccipher_tobe) { |
| 5001 | bombout(("Couldn't agree a server-to-client cipher (available: %s)", |
| 5002 | str ? str : "(null)")); |
| 5003 | crStop(0); |
| 5004 | } |
| 5005 | |
| 5006 | ssh_pkt_getstring(pktin, &str, &len); /* client->server mac */ |
| 5007 | for (i = 0; i < s->nmacs; i++) { |
| 5008 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
| 5009 | s->csmac_tobe = s->maclist[i]; |
| 5010 | break; |
| 5011 | } |
| 5012 | } |
| 5013 | ssh_pkt_getstring(pktin, &str, &len); /* server->client mac */ |
| 5014 | for (i = 0; i < s->nmacs; i++) { |
| 5015 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
| 5016 | s->scmac_tobe = s->maclist[i]; |
| 5017 | break; |
| 5018 | } |
| 5019 | } |
| 5020 | ssh_pkt_getstring(pktin, &str, &len); /* client->server compression */ |
| 5021 | for (i = 0; i < lenof(compressions) + 1; i++) { |
| 5022 | const struct ssh_compress *c = |
| 5023 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
| 5024 | if (in_commasep_string(c->name, str, len)) { |
| 5025 | s->cscomp_tobe = c; |
| 5026 | break; |
| 5027 | } |
| 5028 | } |
| 5029 | ssh_pkt_getstring(pktin, &str, &len); /* server->client compression */ |
| 5030 | for (i = 0; i < lenof(compressions) + 1; i++) { |
| 5031 | const struct ssh_compress *c = |
| 5032 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
| 5033 | if (in_commasep_string(c->name, str, len)) { |
| 5034 | s->sccomp_tobe = c; |
| 5035 | break; |
| 5036 | } |
| 5037 | } |
| 5038 | } |
| 5039 | |
| 5040 | /* |
| 5041 | * Work out the number of bits of key we will need from the key |
| 5042 | * exchange. We start with the maximum key length of either |
| 5043 | * cipher... |
| 5044 | */ |
| 5045 | { |
| 5046 | int csbits, scbits; |
| 5047 | |
| 5048 | csbits = s->cscipher_tobe->keylen; |
| 5049 | scbits = s->sccipher_tobe->keylen; |
| 5050 | s->nbits = (csbits > scbits ? csbits : scbits); |
| 5051 | } |
| 5052 | /* The keys only have 160-bit entropy, since they're based on |
| 5053 | * a SHA-1 hash. So cap the key size at 160 bits. */ |
| 5054 | if (s->nbits > 160) |
| 5055 | s->nbits = 160; |
| 5056 | |
| 5057 | /* |
| 5058 | * If we're doing Diffie-Hellman group exchange, start by |
| 5059 | * requesting a group. |
| 5060 | */ |
| 5061 | if (!ssh->kex->pdata) { |
| 5062 | logevent("Doing Diffie-Hellman group exchange"); |
| 5063 | ssh->pkt_ctx |= SSH2_PKTCTX_DHGEX; |
| 5064 | /* |
| 5065 | * Work out how big a DH group we will need to allow that |
| 5066 | * much data. |
| 5067 | */ |
| 5068 | s->pbits = 512 << ((s->nbits - 1) / 64); |
| 5069 | s->pktout = ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST); |
| 5070 | ssh2_pkt_adduint32(s->pktout, s->pbits); |
| 5071 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
| 5072 | |
| 5073 | crWaitUntil(pktin); |
| 5074 | if (pktin->type != SSH2_MSG_KEX_DH_GEX_GROUP) { |
| 5075 | bombout(("expected key exchange group packet from server")); |
| 5076 | crStop(0); |
| 5077 | } |
| 5078 | s->p = ssh2_pkt_getmp(pktin); |
| 5079 | s->g = ssh2_pkt_getmp(pktin); |
| 5080 | if (!s->p || !s->g) { |
| 5081 | bombout(("unable to read mp-ints from incoming group packet")); |
| 5082 | crStop(0); |
| 5083 | } |
| 5084 | ssh->kex_ctx = dh_setup_gex(s->p, s->g); |
| 5085 | s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT; |
| 5086 | s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY; |
| 5087 | } else { |
| 5088 | ssh->pkt_ctx |= SSH2_PKTCTX_DHGROUP; |
| 5089 | ssh->kex_ctx = dh_setup_group(ssh->kex); |
| 5090 | s->kex_init_value = SSH2_MSG_KEXDH_INIT; |
| 5091 | s->kex_reply_value = SSH2_MSG_KEXDH_REPLY; |
| 5092 | logeventf(ssh, "Using Diffie-Hellman with standard group \"%s\"", |
| 5093 | ssh->kex->groupname); |
| 5094 | } |
| 5095 | |
| 5096 | logevent("Doing Diffie-Hellman key exchange"); |
| 5097 | /* |
| 5098 | * Now generate and send e for Diffie-Hellman. |
| 5099 | */ |
| 5100 | s->e = dh_create_e(ssh->kex_ctx, s->nbits * 2); |
| 5101 | s->pktout = ssh2_pkt_init(s->kex_init_value); |
| 5102 | ssh2_pkt_addmp(s->pktout, s->e); |
| 5103 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
| 5104 | |
| 5105 | crWaitUntil(pktin); |
| 5106 | if (pktin->type != s->kex_reply_value) { |
| 5107 | bombout(("expected key exchange reply packet from server")); |
| 5108 | crStop(0); |
| 5109 | } |
| 5110 | ssh_pkt_getstring(pktin, &s->hostkeydata, &s->hostkeylen); |
| 5111 | s->f = ssh2_pkt_getmp(pktin); |
| 5112 | if (!s->f) { |
| 5113 | bombout(("unable to parse key exchange reply packet")); |
| 5114 | crStop(0); |
| 5115 | } |
| 5116 | ssh_pkt_getstring(pktin, &s->sigdata, &s->siglen); |
| 5117 | |
| 5118 | s->K = dh_find_K(ssh->kex_ctx, s->f); |
| 5119 | |
| 5120 | sha_string(&ssh->exhash, s->hostkeydata, s->hostkeylen); |
| 5121 | if (ssh->kex == &ssh_diffiehellman_gex) { |
| 5122 | sha_uint32(&ssh->exhash, s->pbits); |
| 5123 | sha_mpint(&ssh->exhash, s->p); |
| 5124 | sha_mpint(&ssh->exhash, s->g); |
| 5125 | } |
| 5126 | sha_mpint(&ssh->exhash, s->e); |
| 5127 | sha_mpint(&ssh->exhash, s->f); |
| 5128 | sha_mpint(&ssh->exhash, s->K); |
| 5129 | SHA_Final(&ssh->exhash, s->exchange_hash); |
| 5130 | |
| 5131 | dh_cleanup(ssh->kex_ctx); |
| 5132 | ssh->kex_ctx = NULL; |
| 5133 | |
| 5134 | #if 0 |
| 5135 | debug(("Exchange hash is:\n")); |
| 5136 | dmemdump(s->exchange_hash, 20); |
| 5137 | #endif |
| 5138 | |
| 5139 | s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen); |
| 5140 | if (!s->hkey || |
| 5141 | !ssh->hostkey->verifysig(s->hkey, s->sigdata, s->siglen, |
| 5142 | (char *)s->exchange_hash, 20)) { |
| 5143 | bombout(("Server's host key did not match the signature supplied")); |
| 5144 | crStop(0); |
| 5145 | } |
| 5146 | |
| 5147 | /* |
| 5148 | * Authenticate remote host: verify host key. (We've already |
| 5149 | * checked the signature of the exchange hash.) |
| 5150 | */ |
| 5151 | s->keystr = ssh->hostkey->fmtkey(s->hkey); |
| 5152 | s->fingerprint = ssh->hostkey->fingerprint(s->hkey); |
| 5153 | sk_set_frozen(ssh->s, 1); |
| 5154 | verify_ssh_host_key(ssh->frontend, |
| 5155 | ssh->savedhost, ssh->savedport, ssh->hostkey->keytype, |
| 5156 | s->keystr, s->fingerprint); |
| 5157 | sk_set_frozen(ssh->s, 0); |
| 5158 | if (!s->got_session_id) { /* don't bother logging this in rekeys */ |
| 5159 | logevent("Host key fingerprint is:"); |
| 5160 | logevent(s->fingerprint); |
| 5161 | } |
| 5162 | sfree(s->fingerprint); |
| 5163 | sfree(s->keystr); |
| 5164 | ssh->hostkey->freekey(s->hkey); |
| 5165 | |
| 5166 | /* |
| 5167 | * The exchange hash from the very first key exchange is also |
| 5168 | * the session id, used in session key construction and |
| 5169 | * authentication. |
| 5170 | */ |
| 5171 | if (!s->got_session_id) { |
| 5172 | memcpy(ssh->v2_session_id, s->exchange_hash, |
| 5173 | sizeof(s->exchange_hash)); |
| 5174 | s->got_session_id = TRUE; |
| 5175 | } |
| 5176 | |
| 5177 | /* |
| 5178 | * Send SSH2_MSG_NEWKEYS. |
| 5179 | */ |
| 5180 | s->pktout = ssh2_pkt_init(SSH2_MSG_NEWKEYS); |
| 5181 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
| 5182 | ssh->outgoing_data_size = 0; /* start counting from here */ |
| 5183 | |
| 5184 | /* |
| 5185 | * We've sent client NEWKEYS, so create and initialise |
| 5186 | * client-to-server session keys. |
| 5187 | */ |
| 5188 | if (ssh->cs_cipher_ctx) |
| 5189 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
| 5190 | ssh->cscipher = s->cscipher_tobe; |
| 5191 | ssh->cs_cipher_ctx = ssh->cscipher->make_context(); |
| 5192 | |
| 5193 | if (ssh->cs_mac_ctx) |
| 5194 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
| 5195 | ssh->csmac = s->csmac_tobe; |
| 5196 | ssh->cs_mac_ctx = ssh->csmac->make_context(); |
| 5197 | |
| 5198 | if (ssh->cs_comp_ctx) |
| 5199 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
| 5200 | ssh->cscomp = s->cscomp_tobe; |
| 5201 | ssh->cs_comp_ctx = ssh->cscomp->compress_init(); |
| 5202 | |
| 5203 | /* |
| 5204 | * Set IVs on client-to-server keys. Here we use the exchange |
| 5205 | * hash from the _first_ key exchange. |
| 5206 | */ |
| 5207 | { |
| 5208 | unsigned char keyspace[40]; |
| 5209 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'C',keyspace); |
| 5210 | ssh->cscipher->setkey(ssh->cs_cipher_ctx, keyspace); |
| 5211 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'A',keyspace); |
| 5212 | ssh->cscipher->setiv(ssh->cs_cipher_ctx, keyspace); |
| 5213 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'E',keyspace); |
| 5214 | ssh->csmac->setkey(ssh->cs_mac_ctx, keyspace); |
| 5215 | } |
| 5216 | |
| 5217 | logeventf(ssh, "Initialised %.200s client->server encryption", |
| 5218 | ssh->cscipher->text_name); |
| 5219 | logeventf(ssh, "Initialised %.200s client->server MAC algorithm", |
| 5220 | ssh->csmac->text_name); |
| 5221 | if (ssh->cscomp->text_name) |
| 5222 | logeventf(ssh, "Initialised %s compression", |
| 5223 | ssh->cscomp->text_name); |
| 5224 | |
| 5225 | /* |
| 5226 | * Now our end of the key exchange is complete, we can send all |
| 5227 | * our queued higher-layer packets. |
| 5228 | */ |
| 5229 | ssh->queueing = FALSE; |
| 5230 | ssh2_pkt_queuesend(ssh); |
| 5231 | |
| 5232 | /* |
| 5233 | * Expect SSH2_MSG_NEWKEYS from server. |
| 5234 | */ |
| 5235 | crWaitUntil(pktin); |
| 5236 | if (pktin->type != SSH2_MSG_NEWKEYS) { |
| 5237 | bombout(("expected new-keys packet from server")); |
| 5238 | crStop(0); |
| 5239 | } |
| 5240 | ssh->incoming_data_size = 0; /* start counting from here */ |
| 5241 | |
| 5242 | /* |
| 5243 | * We've seen server NEWKEYS, so create and initialise |
| 5244 | * server-to-client session keys. |
| 5245 | */ |
| 5246 | if (ssh->sc_cipher_ctx) |
| 5247 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
| 5248 | ssh->sccipher = s->sccipher_tobe; |
| 5249 | ssh->sc_cipher_ctx = ssh->sccipher->make_context(); |
| 5250 | |
| 5251 | if (ssh->sc_mac_ctx) |
| 5252 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
| 5253 | ssh->scmac = s->scmac_tobe; |
| 5254 | ssh->sc_mac_ctx = ssh->scmac->make_context(); |
| 5255 | |
| 5256 | if (ssh->sc_comp_ctx) |
| 5257 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
| 5258 | ssh->sccomp = s->sccomp_tobe; |
| 5259 | ssh->sc_comp_ctx = ssh->sccomp->decompress_init(); |
| 5260 | |
| 5261 | /* |
| 5262 | * Set IVs on server-to-client keys. Here we use the exchange |
| 5263 | * hash from the _first_ key exchange. |
| 5264 | */ |
| 5265 | { |
| 5266 | unsigned char keyspace[40]; |
| 5267 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'D',keyspace); |
| 5268 | ssh->sccipher->setkey(ssh->sc_cipher_ctx, keyspace); |
| 5269 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'B',keyspace); |
| 5270 | ssh->sccipher->setiv(ssh->sc_cipher_ctx, keyspace); |
| 5271 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'F',keyspace); |
| 5272 | ssh->scmac->setkey(ssh->sc_mac_ctx, keyspace); |
| 5273 | } |
| 5274 | logeventf(ssh, "Initialised %.200s server->client encryption", |
| 5275 | ssh->sccipher->text_name); |
| 5276 | logeventf(ssh, "Initialised %.200s server->client MAC algorithm", |
| 5277 | ssh->scmac->text_name); |
| 5278 | if (ssh->sccomp->text_name) |
| 5279 | logeventf(ssh, "Initialised %s decompression", |
| 5280 | ssh->sccomp->text_name); |
| 5281 | |
| 5282 | /* |
| 5283 | * Free key exchange data. |
| 5284 | */ |
| 5285 | freebn(s->f); |
| 5286 | freebn(s->K); |
| 5287 | if (ssh->kex == &ssh_diffiehellman_gex) { |
| 5288 | freebn(s->g); |
| 5289 | freebn(s->p); |
| 5290 | } |
| 5291 | |
| 5292 | /* |
| 5293 | * Key exchange is over. Loop straight back round if we have a |
| 5294 | * deferred rekey reason. |
| 5295 | */ |
| 5296 | if (ssh->deferred_rekey_reason) { |
| 5297 | logevent(ssh->deferred_rekey_reason); |
| 5298 | pktin = NULL; |
| 5299 | ssh->deferred_rekey_reason = NULL; |
| 5300 | goto begin_key_exchange; |
| 5301 | } |
| 5302 | |
| 5303 | /* |
| 5304 | * Otherwise, schedule a timer for our next rekey. |
| 5305 | */ |
| 5306 | ssh->kex_in_progress = FALSE; |
| 5307 | ssh->last_rekey = GETTICKCOUNT(); |
| 5308 | if (ssh->cfg.ssh_rekey_time != 0) |
| 5309 | ssh->next_rekey = schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC, |
| 5310 | ssh2_timer, ssh); |
| 5311 | |
| 5312 | /* |
| 5313 | * If this is the first key exchange phase, we must pass the |
| 5314 | * SSH2_MSG_NEWKEYS packet to the next layer, not because it |
| 5315 | * wants to see it but because it will need time to initialise |
| 5316 | * itself before it sees an actual packet. In subsequent key |
| 5317 | * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because |
| 5318 | * it would only confuse the layer above. |
| 5319 | */ |
| 5320 | if (s->activated_authconn) { |
| 5321 | crReturn(1); |
| 5322 | } |
| 5323 | s->activated_authconn = TRUE; |
| 5324 | |
| 5325 | /* |
| 5326 | * Now we're encrypting. Begin returning 1 to the protocol main |
| 5327 | * function so that other things can run on top of the |
| 5328 | * transport. If we ever see a KEXINIT, we must go back to the |
| 5329 | * start. |
| 5330 | * |
| 5331 | * We _also_ go back to the start if we see pktin==NULL and |
| 5332 | * inlen==-1, because this is a special signal meaning |
| 5333 | * `initiate client-driven rekey', and `in' contains a message |
| 5334 | * giving the reason for the rekey. |
| 5335 | */ |
| 5336 | while (!((pktin && pktin->type == SSH2_MSG_KEXINIT) || |
| 5337 | (!pktin && inlen == -1))) { |
| 5338 | wait_for_rekey: |
| 5339 | crReturn(1); |
| 5340 | } |
| 5341 | if (pktin) { |
| 5342 | logevent("Server initiated key re-exchange"); |
| 5343 | } else { |
| 5344 | /* |
| 5345 | * Special case: if the server bug is set that doesn't |
| 5346 | * allow rekeying, we give a different log message and |
| 5347 | * continue waiting. (If such a server _initiates_ a rekey, |
| 5348 | * we process it anyway!) |
| 5349 | */ |
| 5350 | if ((ssh->remote_bugs & BUG_SSH2_REKEY)) { |
| 5351 | logeventf(ssh, "Server bug prevents key re-exchange (%s)", |
| 5352 | (char *)in); |
| 5353 | /* Reset the counters, so that at least this message doesn't |
| 5354 | * hit the event log _too_ often. */ |
| 5355 | ssh->outgoing_data_size = 0; |
| 5356 | ssh->incoming_data_size = 0; |
| 5357 | if (ssh->cfg.ssh_rekey_time != 0) { |
| 5358 | ssh->next_rekey = |
| 5359 | schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC, |
| 5360 | ssh2_timer, ssh); |
| 5361 | } |
| 5362 | goto wait_for_rekey; /* this is utterly horrid */ |
| 5363 | } else { |
| 5364 | logeventf(ssh, "Initiating key re-exchange (%s)", (char *)in); |
| 5365 | } |
| 5366 | } |
| 5367 | goto begin_key_exchange; |
| 5368 | |
| 5369 | crFinish(1); |
| 5370 | } |
| 5371 | |
| 5372 | /* |
| 5373 | * Add data to an SSH2 channel output buffer. |
| 5374 | */ |
| 5375 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, |
| 5376 | int len) |
| 5377 | { |
| 5378 | bufchain_add(&c->v.v2.outbuffer, buf, len); |
| 5379 | } |
| 5380 | |
| 5381 | /* |
| 5382 | * Attempt to send data on an SSH2 channel. |
| 5383 | */ |
| 5384 | static int ssh2_try_send(struct ssh_channel *c) |
| 5385 | { |
| 5386 | Ssh ssh = c->ssh; |
| 5387 | struct Packet *pktout; |
| 5388 | |
| 5389 | while (c->v.v2.remwindow > 0 && bufchain_size(&c->v.v2.outbuffer) > 0) { |
| 5390 | int len; |
| 5391 | void *data; |
| 5392 | bufchain_prefix(&c->v.v2.outbuffer, &data, &len); |
| 5393 | if ((unsigned)len > c->v.v2.remwindow) |
| 5394 | len = c->v.v2.remwindow; |
| 5395 | if ((unsigned)len > c->v.v2.remmaxpkt) |
| 5396 | len = c->v.v2.remmaxpkt; |
| 5397 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA); |
| 5398 | ssh2_pkt_adduint32(pktout, c->remoteid); |
| 5399 | dont_log_data(ssh, pktout, PKTLOG_OMIT); |
| 5400 | ssh2_pkt_addstring_start(pktout); |
| 5401 | ssh2_pkt_addstring_data(pktout, data, len); |
| 5402 | end_log_omission(ssh, pktout); |
| 5403 | ssh2_pkt_send(ssh, pktout); |
| 5404 | bufchain_consume(&c->v.v2.outbuffer, len); |
| 5405 | c->v.v2.remwindow -= len; |
| 5406 | } |
| 5407 | |
| 5408 | /* |
| 5409 | * After having sent as much data as we can, return the amount |
| 5410 | * still buffered. |
| 5411 | */ |
| 5412 | return bufchain_size(&c->v.v2.outbuffer); |
| 5413 | } |
| 5414 | |
| 5415 | /* |
| 5416 | * Potentially enlarge the window on an SSH2 channel. |
| 5417 | */ |
| 5418 | static void ssh2_set_window(struct ssh_channel *c, unsigned newwin) |
| 5419 | { |
| 5420 | Ssh ssh = c->ssh; |
| 5421 | |
| 5422 | /* |
| 5423 | * Never send WINDOW_ADJUST for a channel that the remote side |
| 5424 | * already thinks it's closed; there's no point, since it won't |
| 5425 | * be sending any more data anyway. |
| 5426 | */ |
| 5427 | if (c->closes != 0) |
| 5428 | return; |
| 5429 | |
| 5430 | if (newwin > c->v.v2.locwindow) { |
| 5431 | struct Packet *pktout; |
| 5432 | |
| 5433 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 5434 | ssh2_pkt_adduint32(pktout, c->remoteid); |
| 5435 | ssh2_pkt_adduint32(pktout, newwin - c->v.v2.locwindow); |
| 5436 | ssh2_pkt_send(ssh, pktout); |
| 5437 | c->v.v2.locwindow = newwin; |
| 5438 | } |
| 5439 | } |
| 5440 | |
| 5441 | static void ssh2_msg_channel_window_adjust(Ssh ssh, struct Packet *pktin) |
| 5442 | { |
| 5443 | unsigned i = ssh_pkt_getuint32(pktin); |
| 5444 | struct ssh_channel *c; |
| 5445 | c = find234(ssh->channels, &i, ssh_channelfind); |
| 5446 | if (c && !c->closes) |
| 5447 | c->v.v2.remwindow += ssh_pkt_getuint32(pktin); |
| 5448 | } |
| 5449 | |
| 5450 | static void ssh2_msg_channel_data(Ssh ssh, struct Packet *pktin) |
| 5451 | { |
| 5452 | char *data; |
| 5453 | unsigned int length; |
| 5454 | unsigned i = ssh_pkt_getuint32(pktin); |
| 5455 | struct ssh_channel *c; |
| 5456 | c = find234(ssh->channels, &i, ssh_channelfind); |
| 5457 | if (!c) |
| 5458 | return; /* nonexistent channel */ |
| 5459 | if (pktin->type == SSH2_MSG_CHANNEL_EXTENDED_DATA && |
| 5460 | ssh_pkt_getuint32(pktin) != SSH2_EXTENDED_DATA_STDERR) |
| 5461 | return; /* extended but not stderr */ |
| 5462 | ssh_pkt_getstring(pktin, &data, &length); |
| 5463 | if (data) { |
| 5464 | int bufsize = 0; |
| 5465 | c->v.v2.locwindow -= length; |
| 5466 | switch (c->type) { |
| 5467 | case CHAN_MAINSESSION: |
| 5468 | bufsize = |
| 5469 | from_backend(ssh->frontend, pktin->type == |
| 5470 | SSH2_MSG_CHANNEL_EXTENDED_DATA, |
| 5471 | data, length); |
| 5472 | break; |
| 5473 | case CHAN_X11: |
| 5474 | bufsize = x11_send(c->u.x11.s, data, length); |
| 5475 | break; |
| 5476 | case CHAN_SOCKDATA: |
| 5477 | bufsize = pfd_send(c->u.pfd.s, data, length); |
| 5478 | break; |
| 5479 | case CHAN_AGENT: |
| 5480 | while (length > 0) { |
| 5481 | if (c->u.a.lensofar < 4) { |
| 5482 | unsigned int l = min(4 - c->u.a.lensofar, length); |
| 5483 | memcpy(c->u.a.msglen + c->u.a.lensofar, |
| 5484 | data, l); |
| 5485 | data += l; |
| 5486 | length -= l; |
| 5487 | c->u.a.lensofar += l; |
| 5488 | } |
| 5489 | if (c->u.a.lensofar == 4) { |
| 5490 | c->u.a.totallen = |
| 5491 | 4 + GET_32BIT(c->u.a.msglen); |
| 5492 | c->u.a.message = snewn(c->u.a.totallen, |
| 5493 | unsigned char); |
| 5494 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
| 5495 | } |
| 5496 | if (c->u.a.lensofar >= 4 && length > 0) { |
| 5497 | unsigned int l = |
| 5498 | min(c->u.a.totallen - c->u.a.lensofar, |
| 5499 | length); |
| 5500 | memcpy(c->u.a.message + c->u.a.lensofar, |
| 5501 | data, l); |
| 5502 | data += l; |
| 5503 | length -= l; |
| 5504 | c->u.a.lensofar += l; |
| 5505 | } |
| 5506 | if (c->u.a.lensofar == c->u.a.totallen) { |
| 5507 | void *reply; |
| 5508 | int replylen; |
| 5509 | if (agent_query(c->u.a.message, |
| 5510 | c->u.a.totallen, |
| 5511 | &reply, &replylen, |
| 5512 | ssh_agentf_callback, c)) |
| 5513 | ssh_agentf_callback(c, reply, replylen); |
| 5514 | sfree(c->u.a.message); |
| 5515 | c->u.a.lensofar = 0; |
| 5516 | } |
| 5517 | } |
| 5518 | bufsize = 0; |
| 5519 | break; |
| 5520 | } |
| 5521 | /* |
| 5522 | * If we are not buffering too much data, |
| 5523 | * enlarge the window again at the remote side. |
| 5524 | */ |
| 5525 | if (bufsize < OUR_V2_WINSIZE) |
| 5526 | ssh2_set_window(c, OUR_V2_WINSIZE - bufsize); |
| 5527 | } |
| 5528 | } |
| 5529 | |
| 5530 | static void ssh2_msg_channel_eof(Ssh ssh, struct Packet *pktin) |
| 5531 | { |
| 5532 | unsigned i = ssh_pkt_getuint32(pktin); |
| 5533 | struct ssh_channel *c; |
| 5534 | |
| 5535 | c = find234(ssh->channels, &i, ssh_channelfind); |
| 5536 | if (!c) |
| 5537 | return; /* nonexistent channel */ |
| 5538 | |
| 5539 | if (c->type == CHAN_X11) { |
| 5540 | /* |
| 5541 | * Remote EOF on an X11 channel means we should |
| 5542 | * wrap up and close the channel ourselves. |
| 5543 | */ |
| 5544 | x11_close(c->u.x11.s); |
| 5545 | sshfwd_close(c); |
| 5546 | } else if (c->type == CHAN_AGENT) { |
| 5547 | sshfwd_close(c); |
| 5548 | } else if (c->type == CHAN_SOCKDATA) { |
| 5549 | pfd_close(c->u.pfd.s); |
| 5550 | sshfwd_close(c); |
| 5551 | } |
| 5552 | } |
| 5553 | |
| 5554 | static void ssh2_msg_channel_close(Ssh ssh, struct Packet *pktin) |
| 5555 | { |
| 5556 | unsigned i = ssh_pkt_getuint32(pktin); |
| 5557 | struct ssh_channel *c; |
| 5558 | struct Packet *pktout; |
| 5559 | |
| 5560 | c = find234(ssh->channels, &i, ssh_channelfind); |
| 5561 | if (!c || ((int)c->remoteid) == -1) { |
| 5562 | bombout(("Received CHANNEL_CLOSE for %s channel %d\n", |
| 5563 | c ? "half-open" : "nonexistent", i)); |
| 5564 | return; |
| 5565 | } |
| 5566 | /* Do pre-close processing on the channel. */ |
| 5567 | switch (c->type) { |
| 5568 | case CHAN_MAINSESSION: |
| 5569 | ssh->mainchan = NULL; |
| 5570 | update_specials_menu(ssh->frontend); |
| 5571 | break; |
| 5572 | case CHAN_X11: |
| 5573 | if (c->u.x11.s != NULL) |
| 5574 | x11_close(c->u.x11.s); |
| 5575 | sshfwd_close(c); |
| 5576 | break; |
| 5577 | case CHAN_AGENT: |
| 5578 | sshfwd_close(c); |
| 5579 | break; |
| 5580 | case CHAN_SOCKDATA: |
| 5581 | if (c->u.pfd.s != NULL) |
| 5582 | pfd_close(c->u.pfd.s); |
| 5583 | sshfwd_close(c); |
| 5584 | break; |
| 5585 | } |
| 5586 | if (c->closes == 0) { |
| 5587 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
| 5588 | ssh2_pkt_adduint32(pktout, c->remoteid); |
| 5589 | ssh2_pkt_send(ssh, pktout); |
| 5590 | } |
| 5591 | del234(ssh->channels, c); |
| 5592 | bufchain_clear(&c->v.v2.outbuffer); |
| 5593 | sfree(c); |
| 5594 | |
| 5595 | /* |
| 5596 | * See if that was the last channel left open. |
| 5597 | * (This is only our termination condition if we're |
| 5598 | * not running in -N mode.) |
| 5599 | */ |
| 5600 | if (!ssh->cfg.ssh_no_shell && count234(ssh->channels) == 0) { |
| 5601 | logevent("All channels closed. Disconnecting"); |
| 5602 | #if 0 |
| 5603 | /* |
| 5604 | * We used to send SSH_MSG_DISCONNECT here, |
| 5605 | * because I'd believed that _every_ conforming |
| 5606 | * SSH2 connection had to end with a disconnect |
| 5607 | * being sent by at least one side; apparently |
| 5608 | * I was wrong and it's perfectly OK to |
| 5609 | * unceremoniously slam the connection shut |
| 5610 | * when you're done, and indeed OpenSSH feels |
| 5611 | * this is more polite than sending a |
| 5612 | * DISCONNECT. So now we don't. |
| 5613 | */ |
| 5614 | s->pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
| 5615 | ssh2_pkt_adduint32(s->pktout, SSH2_DISCONNECT_BY_APPLICATION); |
| 5616 | ssh2_pkt_addstring(s->pktout, "All open channels closed"); |
| 5617 | ssh2_pkt_addstring(s->pktout, "en"); /* language tag */ |
| 5618 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
| 5619 | #endif |
| 5620 | ssh->close_expected = TRUE; |
| 5621 | ssh_closing((Plug)ssh, NULL, 0, 0); |
| 5622 | } |
| 5623 | } |
| 5624 | |
| 5625 | static void ssh2_msg_channel_open_confirmation(Ssh ssh, struct Packet *pktin) |
| 5626 | { |
| 5627 | unsigned i = ssh_pkt_getuint32(pktin); |
| 5628 | struct ssh_channel *c; |
| 5629 | struct Packet *pktout; |
| 5630 | |
| 5631 | c = find234(ssh->channels, &i, ssh_channelfind); |
| 5632 | if (!c) |
| 5633 | return; /* nonexistent channel */ |
| 5634 | if (c->type != CHAN_SOCKDATA_DORMANT) |
| 5635 | return; /* dunno why they're confirming this */ |
| 5636 | c->remoteid = ssh_pkt_getuint32(pktin); |
| 5637 | c->type = CHAN_SOCKDATA; |
| 5638 | c->v.v2.remwindow = ssh_pkt_getuint32(pktin); |
| 5639 | c->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin); |
| 5640 | if (c->u.pfd.s) |
| 5641 | pfd_confirm(c->u.pfd.s); |
| 5642 | if (c->closes) { |
| 5643 | /* |
| 5644 | * We have a pending close on this channel, |
| 5645 | * which we decided on before the server acked |
| 5646 | * the channel open. So now we know the |
| 5647 | * remoteid, we can close it again. |
| 5648 | */ |
| 5649 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
| 5650 | ssh2_pkt_adduint32(pktout, c->remoteid); |
| 5651 | ssh2_pkt_send(ssh, pktout); |
| 5652 | } |
| 5653 | } |
| 5654 | |
| 5655 | static void ssh2_msg_channel_open_failure(Ssh ssh, struct Packet *pktin) |
| 5656 | { |
| 5657 | static const char *const reasons[] = { |
| 5658 | "<unknown reason code>", |
| 5659 | "Administratively prohibited", |
| 5660 | "Connect failed", |
| 5661 | "Unknown channel type", |
| 5662 | "Resource shortage", |
| 5663 | }; |
| 5664 | unsigned i = ssh_pkt_getuint32(pktin); |
| 5665 | unsigned reason_code; |
| 5666 | char *reason_string; |
| 5667 | int reason_length; |
| 5668 | char *message; |
| 5669 | struct ssh_channel *c; |
| 5670 | c = find234(ssh->channels, &i, ssh_channelfind); |
| 5671 | if (!c) |
| 5672 | return; /* nonexistent channel */ |
| 5673 | if (c->type != CHAN_SOCKDATA_DORMANT) |
| 5674 | return; /* dunno why they're failing this */ |
| 5675 | |
| 5676 | reason_code = ssh_pkt_getuint32(pktin); |
| 5677 | if (reason_code >= lenof(reasons)) |
| 5678 | reason_code = 0; /* ensure reasons[reason_code] in range */ |
| 5679 | ssh_pkt_getstring(pktin, &reason_string, &reason_length); |
| 5680 | message = dupprintf("Forwarded connection refused by" |
| 5681 | " server: %s [%.*s]", reasons[reason_code], |
| 5682 | reason_length, reason_string); |
| 5683 | logevent(message); |
| 5684 | sfree(message); |
| 5685 | |
| 5686 | pfd_close(c->u.pfd.s); |
| 5687 | |
| 5688 | del234(ssh->channels, c); |
| 5689 | sfree(c); |
| 5690 | } |
| 5691 | |
| 5692 | static void ssh2_msg_channel_request(Ssh ssh, struct Packet *pktin) |
| 5693 | { |
| 5694 | unsigned localid; |
| 5695 | char *type; |
| 5696 | int typelen, want_reply; |
| 5697 | int reply = SSH2_MSG_CHANNEL_FAILURE; /* default */ |
| 5698 | struct ssh_channel *c; |
| 5699 | struct Packet *pktout; |
| 5700 | |
| 5701 | localid = ssh_pkt_getuint32(pktin); |
| 5702 | ssh_pkt_getstring(pktin, &type, &typelen); |
| 5703 | want_reply = ssh2_pkt_getbool(pktin); |
| 5704 | |
| 5705 | /* |
| 5706 | * First, check that the channel exists. Otherwise, |
| 5707 | * we can instantly disconnect with a rude message. |
| 5708 | */ |
| 5709 | c = find234(ssh->channels, &localid, ssh_channelfind); |
| 5710 | if (!c) { |
| 5711 | char buf[80]; |
| 5712 | sprintf(buf, "Received channel request for nonexistent" |
| 5713 | " channel %d", localid); |
| 5714 | logevent(buf); |
| 5715 | pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
| 5716 | ssh2_pkt_adduint32(pktout, SSH2_DISCONNECT_BY_APPLICATION); |
| 5717 | ssh2_pkt_addstring(pktout, buf); |
| 5718 | ssh2_pkt_addstring(pktout, "en"); /* language tag */ |
| 5719 | ssh2_pkt_send_noqueue(ssh, pktout); |
| 5720 | connection_fatal(ssh->frontend, "%s", buf); |
| 5721 | ssh->close_expected = TRUE; |
| 5722 | ssh_closing((Plug)ssh, NULL, 0, 0); |
| 5723 | return; |
| 5724 | } |
| 5725 | |
| 5726 | /* |
| 5727 | * Having got the channel number, we now look at |
| 5728 | * the request type string to see if it's something |
| 5729 | * we recognise. |
| 5730 | */ |
| 5731 | if (c == ssh->mainchan) { |
| 5732 | /* |
| 5733 | * We recognise "exit-status" and "exit-signal" on |
| 5734 | * the primary channel. |
| 5735 | */ |
| 5736 | if (typelen == 11 && |
| 5737 | !memcmp(type, "exit-status", 11)) { |
| 5738 | |
| 5739 | ssh->exitcode = ssh_pkt_getuint32(pktin); |
| 5740 | logeventf(ssh, "Server sent command exit status %d", |
| 5741 | ssh->exitcode); |
| 5742 | reply = SSH2_MSG_CHANNEL_SUCCESS; |
| 5743 | |
| 5744 | } else if (typelen == 11 && |
| 5745 | !memcmp(type, "exit-signal", 11)) { |
| 5746 | |
| 5747 | int is_plausible = TRUE, is_int = FALSE; |
| 5748 | char *fmt_sig = "", *fmt_msg = ""; |
| 5749 | char *msg; |
| 5750 | int msglen = 0, core = FALSE; |
| 5751 | /* ICK: older versions of OpenSSH (e.g. 3.4p1) |
| 5752 | * provide an `int' for the signal, despite its |
| 5753 | * having been a `string' in the drafts since at |
| 5754 | * least 2001. (Fixed in session.c 1.147.) Try to |
| 5755 | * infer which we can safely parse it as. */ |
| 5756 | { |
| 5757 | unsigned char *p = pktin->body + |
| 5758 | pktin->savedpos; |
| 5759 | long len = pktin->length - pktin->savedpos; |
| 5760 | unsigned long num = GET_32BIT(p); /* what is it? */ |
| 5761 | /* If it's 0, it hardly matters; assume string */ |
| 5762 | if (num == 0) { |
| 5763 | is_int = FALSE; |
| 5764 | } else { |
| 5765 | int maybe_int = FALSE, maybe_str = FALSE; |
| 5766 | #define CHECK_HYPOTHESIS(offset, result) \ |
| 5767 | do { \ |
| 5768 | long q = offset; \ |
| 5769 | if (q >= 0 && q+4 <= len) { \ |
| 5770 | q = q + 4 + GET_32BIT(p+q); \ |
| 5771 | if (q >= 0 && q+4 <= len && \ |
| 5772 | (q = q + 4 + GET_32BIT(p+q)) && q == len) \ |
| 5773 | result = TRUE; \ |
| 5774 | } \ |
| 5775 | } while(0) |
| 5776 | CHECK_HYPOTHESIS(4+1, maybe_int); |
| 5777 | CHECK_HYPOTHESIS(4+num+1, maybe_str); |
| 5778 | #undef CHECK_HYPOTHESIS |
| 5779 | if (maybe_int && !maybe_str) |
| 5780 | is_int = TRUE; |
| 5781 | else if (!maybe_int && maybe_str) |
| 5782 | is_int = FALSE; |
| 5783 | else |
| 5784 | /* Crikey. Either or neither. Panic. */ |
| 5785 | is_plausible = FALSE; |
| 5786 | } |
| 5787 | } |
| 5788 | if (is_plausible) { |
| 5789 | if (is_int) { |
| 5790 | /* Old non-standard OpenSSH. */ |
| 5791 | int signum = ssh_pkt_getuint32(pktin); |
| 5792 | fmt_sig = dupprintf(" %d", signum); |
| 5793 | } else { |
| 5794 | /* As per the drafts. */ |
| 5795 | char *sig; |
| 5796 | int siglen; |
| 5797 | ssh_pkt_getstring(pktin, &sig, &siglen); |
| 5798 | /* Signal name isn't supposed to be blank, but |
| 5799 | * let's cope gracefully if it is. */ |
| 5800 | if (siglen) { |
| 5801 | fmt_sig = dupprintf(" \"%.*s\"", |
| 5802 | siglen, sig); |
| 5803 | } |
| 5804 | } |
| 5805 | core = ssh2_pkt_getbool(pktin); |
| 5806 | ssh_pkt_getstring(pktin, &msg, &msglen); |
| 5807 | if (msglen) { |
| 5808 | fmt_msg = dupprintf(" (\"%.*s\")", msglen, msg); |
| 5809 | } |
| 5810 | /* ignore lang tag */ |
| 5811 | } /* else don't attempt to parse */ |
| 5812 | logeventf(ssh, "Server exited on signal%s%s%s", |
| 5813 | fmt_sig, core ? " (core dumped)" : "", |
| 5814 | fmt_msg); |
| 5815 | if (*fmt_sig) sfree(fmt_sig); |
| 5816 | if (*fmt_msg) sfree(fmt_msg); |
| 5817 | reply = SSH2_MSG_CHANNEL_SUCCESS; |
| 5818 | |
| 5819 | } |
| 5820 | } else { |
| 5821 | /* |
| 5822 | * This is a channel request we don't know |
| 5823 | * about, so we now either ignore the request |
| 5824 | * or respond with CHANNEL_FAILURE, depending |
| 5825 | * on want_reply. |
| 5826 | */ |
| 5827 | reply = SSH2_MSG_CHANNEL_FAILURE; |
| 5828 | } |
| 5829 | if (want_reply) { |
| 5830 | pktout = ssh2_pkt_init(reply); |
| 5831 | ssh2_pkt_adduint32(pktout, c->remoteid); |
| 5832 | ssh2_pkt_send(ssh, pktout); |
| 5833 | } |
| 5834 | } |
| 5835 | |
| 5836 | static void ssh2_msg_global_request(Ssh ssh, struct Packet *pktin) |
| 5837 | { |
| 5838 | char *type; |
| 5839 | int typelen, want_reply; |
| 5840 | struct Packet *pktout; |
| 5841 | |
| 5842 | ssh_pkt_getstring(pktin, &type, &typelen); |
| 5843 | want_reply = ssh2_pkt_getbool(pktin); |
| 5844 | |
| 5845 | /* |
| 5846 | * We currently don't support any global requests |
| 5847 | * at all, so we either ignore the request or |
| 5848 | * respond with REQUEST_FAILURE, depending on |
| 5849 | * want_reply. |
| 5850 | */ |
| 5851 | if (want_reply) { |
| 5852 | pktout = ssh2_pkt_init(SSH2_MSG_REQUEST_FAILURE); |
| 5853 | ssh2_pkt_send(ssh, pktout); |
| 5854 | } |
| 5855 | } |
| 5856 | |
| 5857 | static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin) |
| 5858 | { |
| 5859 | char *type; |
| 5860 | int typelen; |
| 5861 | char *peeraddr; |
| 5862 | int peeraddrlen; |
| 5863 | int peerport; |
| 5864 | char *error = NULL; |
| 5865 | struct ssh_channel *c; |
| 5866 | unsigned remid, winsize, pktsize; |
| 5867 | struct Packet *pktout; |
| 5868 | |
| 5869 | ssh_pkt_getstring(pktin, &type, &typelen); |
| 5870 | c = snew(struct ssh_channel); |
| 5871 | c->ssh = ssh; |
| 5872 | |
| 5873 | remid = ssh_pkt_getuint32(pktin); |
| 5874 | winsize = ssh_pkt_getuint32(pktin); |
| 5875 | pktsize = ssh_pkt_getuint32(pktin); |
| 5876 | |
| 5877 | if (typelen == 3 && !memcmp(type, "x11", 3)) { |
| 5878 | char *addrstr; |
| 5879 | |
| 5880 | ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen); |
| 5881 | addrstr = snewn(peeraddrlen+1, char); |
| 5882 | memcpy(addrstr, peeraddr, peeraddrlen); |
| 5883 | addrstr[peeraddrlen] = '\0'; |
| 5884 | peerport = ssh_pkt_getuint32(pktin); |
| 5885 | |
| 5886 | logeventf(ssh, "Received X11 connect request from %s:%d", |
| 5887 | addrstr, peerport); |
| 5888 | |
| 5889 | if (!ssh->X11_fwd_enabled) |
| 5890 | error = "X11 forwarding is not enabled"; |
| 5891 | else if (x11_init(&c->u.x11.s, ssh->cfg.x11_display, c, |
| 5892 | ssh->x11auth, addrstr, peerport, |
| 5893 | &ssh->cfg) != NULL) { |
| 5894 | error = "Unable to open an X11 connection"; |
| 5895 | } else { |
| 5896 | logevent("Opening X11 forward connection succeeded"); |
| 5897 | c->type = CHAN_X11; |
| 5898 | } |
| 5899 | |
| 5900 | sfree(addrstr); |
| 5901 | } else if (typelen == 15 && |
| 5902 | !memcmp(type, "forwarded-tcpip", 15)) { |
| 5903 | struct ssh_rportfwd pf, *realpf; |
| 5904 | char *dummy; |
| 5905 | int dummylen; |
| 5906 | ssh_pkt_getstring(pktin, &dummy, &dummylen);/* skip address */ |
| 5907 | pf.sport = ssh_pkt_getuint32(pktin); |
| 5908 | ssh_pkt_getstring(pktin, &peeraddr, &peeraddrlen); |
| 5909 | peerport = ssh_pkt_getuint32(pktin); |
| 5910 | realpf = find234(ssh->rportfwds, &pf, NULL); |
| 5911 | logeventf(ssh, "Received remote port %d open request " |
| 5912 | "from %s:%d", pf.sport, peeraddr, peerport); |
| 5913 | if (realpf == NULL) { |
| 5914 | error = "Remote port is not recognised"; |
| 5915 | } else { |
| 5916 | const char *e = pfd_newconnect(&c->u.pfd.s, |
| 5917 | realpf->dhost, |
| 5918 | realpf->dport, c, |
| 5919 | &ssh->cfg, |
| 5920 | realpf->pfrec->addressfamily); |
| 5921 | logeventf(ssh, "Attempting to forward remote port to " |
| 5922 | "%s:%d", realpf->dhost, realpf->dport); |
| 5923 | if (e != NULL) { |
| 5924 | logeventf(ssh, "Port open failed: %s", e); |
| 5925 | error = "Port open failed"; |
| 5926 | } else { |
| 5927 | logevent("Forwarded port opened successfully"); |
| 5928 | c->type = CHAN_SOCKDATA; |
| 5929 | } |
| 5930 | } |
| 5931 | } else if (typelen == 22 && |
| 5932 | !memcmp(type, "auth-agent@openssh.com", 3)) { |
| 5933 | if (!ssh->agentfwd_enabled) |
| 5934 | error = "Agent forwarding is not enabled"; |
| 5935 | else { |
| 5936 | c->type = CHAN_AGENT; /* identify channel type */ |
| 5937 | c->u.a.lensofar = 0; |
| 5938 | } |
| 5939 | } else { |
| 5940 | error = "Unsupported channel type requested"; |
| 5941 | } |
| 5942 | |
| 5943 | c->remoteid = remid; |
| 5944 | if (error) { |
| 5945 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 5946 | ssh2_pkt_adduint32(pktout, c->remoteid); |
| 5947 | ssh2_pkt_adduint32(pktout, SSH2_OPEN_CONNECT_FAILED); |
| 5948 | ssh2_pkt_addstring(pktout, error); |
| 5949 | ssh2_pkt_addstring(pktout, "en"); /* language tag */ |
| 5950 | ssh2_pkt_send(ssh, pktout); |
| 5951 | logeventf(ssh, "Rejected channel open: %s", error); |
| 5952 | sfree(c); |
| 5953 | } else { |
| 5954 | c->localid = alloc_channel_id(ssh); |
| 5955 | c->closes = 0; |
| 5956 | c->v.v2.locwindow = OUR_V2_WINSIZE; |
| 5957 | c->v.v2.remwindow = winsize; |
| 5958 | c->v.v2.remmaxpkt = pktsize; |
| 5959 | bufchain_init(&c->v.v2.outbuffer); |
| 5960 | add234(ssh->channels, c); |
| 5961 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 5962 | ssh2_pkt_adduint32(pktout, c->remoteid); |
| 5963 | ssh2_pkt_adduint32(pktout, c->localid); |
| 5964 | ssh2_pkt_adduint32(pktout, c->v.v2.locwindow); |
| 5965 | ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
| 5966 | ssh2_pkt_send(ssh, pktout); |
| 5967 | } |
| 5968 | } |
| 5969 | |
| 5970 | /* |
| 5971 | * Handle the SSH2 userauth and connection layers. |
| 5972 | */ |
| 5973 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, |
| 5974 | struct Packet *pktin) |
| 5975 | { |
| 5976 | struct do_ssh2_authconn_state { |
| 5977 | enum { |
| 5978 | AUTH_INVALID, AUTH_PUBLICKEY_AGENT, AUTH_PUBLICKEY_FILE, |
| 5979 | AUTH_PASSWORD, |
| 5980 | AUTH_KEYBOARD_INTERACTIVE |
| 5981 | } method; |
| 5982 | enum { |
| 5983 | AUTH_TYPE_NONE, |
| 5984 | AUTH_TYPE_PUBLICKEY, |
| 5985 | AUTH_TYPE_PUBLICKEY_OFFER_LOUD, |
| 5986 | AUTH_TYPE_PUBLICKEY_OFFER_QUIET, |
| 5987 | AUTH_TYPE_PASSWORD, |
| 5988 | AUTH_TYPE_KEYBOARD_INTERACTIVE, |
| 5989 | AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET |
| 5990 | } type; |
| 5991 | int gotit, need_pw, can_pubkey, can_passwd, can_keyb_inter; |
| 5992 | int tried_pubkey_config, tried_agent; |
| 5993 | int kbd_inter_running, kbd_inter_refused; |
| 5994 | int we_are_in; |
| 5995 | int num_prompts, curr_prompt, echo; |
| 5996 | char username[100]; |
| 5997 | int got_username; |
| 5998 | char pwprompt[512]; |
| 5999 | char password[100]; |
| 6000 | void *publickey_blob; |
| 6001 | int publickey_bloblen; |
| 6002 | unsigned char request[5], *response, *p; |
| 6003 | int responselen; |
| 6004 | int keyi, nkeys; |
| 6005 | int authed; |
| 6006 | char *pkblob, *alg, *commentp; |
| 6007 | int pklen, alglen, commentlen; |
| 6008 | int siglen, retlen, len; |
| 6009 | char *q, *agentreq, *ret; |
| 6010 | int try_send; |
| 6011 | int num_env, env_left, env_ok; |
| 6012 | struct Packet *pktout; |
| 6013 | }; |
| 6014 | crState(do_ssh2_authconn_state); |
| 6015 | |
| 6016 | crBegin(ssh->do_ssh2_authconn_crstate); |
| 6017 | |
| 6018 | /* |
| 6019 | * Request userauth protocol, and await a response to it. |
| 6020 | */ |
| 6021 | s->pktout = ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST); |
| 6022 | ssh2_pkt_addstring(s->pktout, "ssh-userauth"); |
| 6023 | ssh2_pkt_send(ssh, s->pktout); |
| 6024 | crWaitUntilV(pktin); |
| 6025 | if (pktin->type != SSH2_MSG_SERVICE_ACCEPT) { |
| 6026 | bombout(("Server refused user authentication protocol")); |
| 6027 | crStopV; |
| 6028 | } |
| 6029 | |
| 6030 | /* |
| 6031 | * We repeat this whole loop, including the username prompt, |
| 6032 | * until we manage a successful authentication. If the user |
| 6033 | * types the wrong _password_, they can be sent back to the |
| 6034 | * beginning to try another username, if this is configured on. |
| 6035 | * (If they specify a username in the config, they are never |
| 6036 | * asked, even if they do give a wrong password.) |
| 6037 | * |
| 6038 | * I think this best serves the needs of |
| 6039 | * |
| 6040 | * - the people who have no configuration, no keys, and just |
| 6041 | * want to try repeated (username,password) pairs until they |
| 6042 | * type both correctly |
| 6043 | * |
| 6044 | * - people who have keys and configuration but occasionally |
| 6045 | * need to fall back to passwords |
| 6046 | * |
| 6047 | * - people with a key held in Pageant, who might not have |
| 6048 | * logged in to a particular machine before; so they want to |
| 6049 | * type a username, and then _either_ their key will be |
| 6050 | * accepted, _or_ they will type a password. If they mistype |
| 6051 | * the username they will want to be able to get back and |
| 6052 | * retype it! |
| 6053 | */ |
| 6054 | s->username[0] = '\0'; |
| 6055 | s->got_username = FALSE; |
| 6056 | do { |
| 6057 | /* |
| 6058 | * Get a username. |
| 6059 | */ |
| 6060 | if (s->got_username && !ssh->cfg.change_username) { |
| 6061 | /* |
| 6062 | * We got a username last time round this loop, and |
| 6063 | * with change_username turned off we don't try to get |
| 6064 | * it again. |
| 6065 | */ |
| 6066 | } else if (!*ssh->cfg.username) { |
| 6067 | if (ssh_get_line && !ssh_getline_pw_only) { |
| 6068 | if (!ssh_get_line("login as: ", |
| 6069 | s->username, sizeof(s->username), FALSE)) { |
| 6070 | /* |
| 6071 | * get_line failed to get a username. |
| 6072 | * Terminate. |
| 6073 | */ |
| 6074 | logevent("No username provided. Abandoning session."); |
| 6075 | ssh->close_expected = TRUE; |
| 6076 | ssh_closing((Plug)ssh, NULL, 0, 0); |
| 6077 | crStopV; |
| 6078 | } |
| 6079 | } else { |
| 6080 | int ret; /* need not be saved across crReturn */ |
| 6081 | c_write_str(ssh, "login as: "); |
| 6082 | ssh->send_ok = 1; |
| 6083 | setup_userpass_input(ssh, s->username, sizeof(s->username), 1); |
| 6084 | do { |
| 6085 | crWaitUntilV(!pktin); |
| 6086 | ret = process_userpass_input(ssh, in, inlen); |
| 6087 | } while (ret == 0); |
| 6088 | if (ret < 0) |
| 6089 | cleanup_exit(0); |
| 6090 | c_write_str(ssh, "\r\n"); |
| 6091 | } |
| 6092 | s->username[strcspn(s->username, "\n\r")] = '\0'; |
| 6093 | } else { |
| 6094 | char *stuff; |
| 6095 | strncpy(s->username, ssh->cfg.username, sizeof(s->username)); |
| 6096 | s->username[sizeof(s->username)-1] = '\0'; |
| 6097 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
| 6098 | stuff = dupprintf("Using username \"%s\".\r\n", s->username); |
| 6099 | c_write_str(ssh, stuff); |
| 6100 | sfree(stuff); |
| 6101 | } |
| 6102 | } |
| 6103 | s->got_username = TRUE; |
| 6104 | |
| 6105 | /* |
| 6106 | * Send an authentication request using method "none": (a) |
| 6107 | * just in case it succeeds, and (b) so that we know what |
| 6108 | * authentication methods we can usefully try next. |
| 6109 | */ |
| 6110 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
| 6111 | |
| 6112 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
| 6113 | ssh2_pkt_addstring(s->pktout, s->username); |
| 6114 | ssh2_pkt_addstring(s->pktout, "ssh-connection");/* service requested */ |
| 6115 | ssh2_pkt_addstring(s->pktout, "none"); /* method */ |
| 6116 | ssh2_pkt_send(ssh, s->pktout); |
| 6117 | s->type = AUTH_TYPE_NONE; |
| 6118 | s->gotit = FALSE; |
| 6119 | s->we_are_in = FALSE; |
| 6120 | |
| 6121 | s->tried_pubkey_config = FALSE; |
| 6122 | s->tried_agent = FALSE; |
| 6123 | s->kbd_inter_running = FALSE; |
| 6124 | s->kbd_inter_refused = FALSE; |
| 6125 | /* Load the pub half of ssh->cfg.keyfile so we notice if it's in Pageant */ |
| 6126 | if (!filename_is_null(ssh->cfg.keyfile)) { |
| 6127 | int keytype; |
| 6128 | logeventf(ssh, "Reading private key file \"%.150s\"", |
| 6129 | filename_to_str(&ssh->cfg.keyfile)); |
| 6130 | keytype = key_type(&ssh->cfg.keyfile); |
| 6131 | if (keytype == SSH_KEYTYPE_SSH2) { |
| 6132 | s->publickey_blob = |
| 6133 | ssh2_userkey_loadpub(&ssh->cfg.keyfile, NULL, |
| 6134 | &s->publickey_bloblen, NULL); |
| 6135 | } else { |
| 6136 | char *msgbuf; |
| 6137 | logeventf(ssh, "Unable to use this key file (%s)", |
| 6138 | key_type_to_str(keytype)); |
| 6139 | msgbuf = dupprintf("Unable to use key file \"%.150s\"" |
| 6140 | " (%s)\r\n", |
| 6141 | filename_to_str(&ssh->cfg.keyfile), |
| 6142 | key_type_to_str(keytype)); |
| 6143 | c_write_str(ssh, msgbuf); |
| 6144 | sfree(msgbuf); |
| 6145 | s->publickey_blob = NULL; |
| 6146 | } |
| 6147 | } else |
| 6148 | s->publickey_blob = NULL; |
| 6149 | |
| 6150 | while (1) { |
| 6151 | /* |
| 6152 | * Wait for the result of the last authentication request. |
| 6153 | */ |
| 6154 | if (!s->gotit) |
| 6155 | crWaitUntilV(pktin); |
| 6156 | while (pktin->type == SSH2_MSG_USERAUTH_BANNER) { |
| 6157 | char *banner; |
| 6158 | int size; |
| 6159 | /* |
| 6160 | * Don't show the banner if we're operating in |
| 6161 | * non-verbose non-interactive mode. (It's probably |
| 6162 | * a script, which means nobody will read the |
| 6163 | * banner _anyway_, and moreover the printing of |
| 6164 | * the banner will screw up processing on the |
| 6165 | * output of (say) plink.) |
| 6166 | */ |
| 6167 | if (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE)) { |
| 6168 | ssh_pkt_getstring(pktin, &banner, &size); |
| 6169 | if (banner) |
| 6170 | c_write_untrusted(ssh, banner, size); |
| 6171 | } |
| 6172 | crWaitUntilV(pktin); |
| 6173 | } |
| 6174 | if (pktin->type == SSH2_MSG_USERAUTH_SUCCESS) { |
| 6175 | logevent("Access granted"); |
| 6176 | s->we_are_in = TRUE; |
| 6177 | break; |
| 6178 | } |
| 6179 | |
| 6180 | if (s->kbd_inter_running && |
| 6181 | pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) { |
| 6182 | /* |
| 6183 | * This is either a further set-of-prompts packet |
| 6184 | * in keyboard-interactive authentication, or it's |
| 6185 | * the same one and we came back here with `gotit' |
| 6186 | * set. In the former case, we must reset the |
| 6187 | * curr_prompt variable. |
| 6188 | */ |
| 6189 | if (!s->gotit) |
| 6190 | s->curr_prompt = 0; |
| 6191 | } else if (pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) { |
| 6192 | /* FIXME: perhaps we should support this? */ |
| 6193 | bombout(("PASSWD_CHANGEREQ not yet supported")); |
| 6194 | crStopV; |
| 6195 | } else if (pktin->type != SSH2_MSG_USERAUTH_FAILURE) { |
| 6196 | bombout(("Strange packet received during authentication: type %d", |
| 6197 | pktin->type)); |
| 6198 | crStopV; |
| 6199 | } |
| 6200 | |
| 6201 | s->gotit = FALSE; |
| 6202 | |
| 6203 | /* |
| 6204 | * OK, we're now sitting on a USERAUTH_FAILURE message, so |
| 6205 | * we can look at the string in it and know what we can |
| 6206 | * helpfully try next. |
| 6207 | */ |
| 6208 | if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) { |
| 6209 | char *methods; |
| 6210 | int methlen; |
| 6211 | ssh_pkt_getstring(pktin, &methods, &methlen); |
| 6212 | s->kbd_inter_running = FALSE; |
| 6213 | if (!ssh2_pkt_getbool(pktin)) { |
| 6214 | /* |
| 6215 | * We have received an unequivocal Access |
| 6216 | * Denied. This can translate to a variety of |
| 6217 | * messages: |
| 6218 | * |
| 6219 | * - if we'd just tried "none" authentication, |
| 6220 | * it's not worth printing anything at all |
| 6221 | * |
| 6222 | * - if we'd just tried a public key _offer_, |
| 6223 | * the message should be "Server refused our |
| 6224 | * key" (or no message at all if the key |
| 6225 | * came from Pageant) |
| 6226 | * |
| 6227 | * - if we'd just tried anything else, the |
| 6228 | * message really should be "Access denied". |
| 6229 | * |
| 6230 | * Additionally, if we'd just tried password |
| 6231 | * authentication, we should break out of this |
| 6232 | * whole loop so as to go back to the username |
| 6233 | * prompt (iff we're configured to allow |
| 6234 | * username change attempts). |
| 6235 | */ |
| 6236 | if (s->type == AUTH_TYPE_NONE) { |
| 6237 | /* do nothing */ |
| 6238 | } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD || |
| 6239 | s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) { |
| 6240 | if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD) |
| 6241 | c_write_str(ssh, "Server refused our key\r\n"); |
| 6242 | logevent("Server refused public key"); |
| 6243 | } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) { |
| 6244 | /* server declined keyboard-interactive; ignore */ |
| 6245 | } else { |
| 6246 | c_write_str(ssh, "Access denied\r\n"); |
| 6247 | logevent("Access denied"); |
| 6248 | if (s->type == AUTH_TYPE_PASSWORD && |
| 6249 | ssh->cfg.change_username) { |
| 6250 | /* XXX perhaps we should allow |
| 6251 | * keyboard-interactive to do this too? */ |
| 6252 | s->we_are_in = FALSE; |
| 6253 | break; |
| 6254 | } |
| 6255 | } |
| 6256 | } else { |
| 6257 | c_write_str(ssh, "Further authentication required\r\n"); |
| 6258 | logevent("Further authentication required"); |
| 6259 | } |
| 6260 | |
| 6261 | s->can_pubkey = |
| 6262 | in_commasep_string("publickey", methods, methlen); |
| 6263 | s->can_passwd = |
| 6264 | in_commasep_string("password", methods, methlen); |
| 6265 | s->can_keyb_inter = ssh->cfg.try_ki_auth && |
| 6266 | in_commasep_string("keyboard-interactive", methods, methlen); |
| 6267 | } |
| 6268 | |
| 6269 | s->method = 0; |
| 6270 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
| 6271 | s->need_pw = FALSE; |
| 6272 | |
| 6273 | /* |
| 6274 | * Most password/passphrase prompts will be |
| 6275 | * non-echoing, so we set this to 0 by default. |
| 6276 | * Exception is that some keyboard-interactive prompts |
| 6277 | * can be echoing, in which case we'll set this to 1. |
| 6278 | */ |
| 6279 | s->echo = 0; |
| 6280 | |
| 6281 | if (!s->method && s->can_pubkey && |
| 6282 | agent_exists() && !s->tried_agent) { |
| 6283 | /* |
| 6284 | * Attempt public-key authentication using Pageant. |
| 6285 | */ |
| 6286 | void *r; |
| 6287 | s->authed = FALSE; |
| 6288 | |
| 6289 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
| 6290 | ssh->pkt_ctx |= SSH2_PKTCTX_PUBLICKEY; |
| 6291 | |
| 6292 | s->tried_agent = TRUE; |
| 6293 | |
| 6294 | logevent("Pageant is running. Requesting keys."); |
| 6295 | |
| 6296 | /* Request the keys held by the agent. */ |
| 6297 | PUT_32BIT(s->request, 1); |
| 6298 | s->request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
| 6299 | if (!agent_query(s->request, 5, &r, &s->responselen, |
| 6300 | ssh_agent_callback, ssh)) { |
| 6301 | do { |
| 6302 | crReturnV; |
| 6303 | if (pktin) { |
| 6304 | bombout(("Unexpected data from server while" |
| 6305 | " waiting for agent response")); |
| 6306 | crStopV; |
| 6307 | } |
| 6308 | } while (pktin || inlen > 0); |
| 6309 | r = ssh->agent_response; |
| 6310 | s->responselen = ssh->agent_response_len; |
| 6311 | } |
| 6312 | s->response = (unsigned char *) r; |
| 6313 | if (s->response && s->responselen >= 5 && |
| 6314 | s->response[4] == SSH2_AGENT_IDENTITIES_ANSWER) { |
| 6315 | s->p = s->response + 5; |
| 6316 | s->nkeys = GET_32BIT(s->p); |
| 6317 | s->p += 4; |
| 6318 | { |
| 6319 | char buf[64]; |
| 6320 | sprintf(buf, "Pageant has %d SSH2 keys", s->nkeys); |
| 6321 | logevent(buf); |
| 6322 | } |
| 6323 | for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) { |
| 6324 | void *vret; |
| 6325 | |
| 6326 | { |
| 6327 | char buf[64]; |
| 6328 | sprintf(buf, "Trying Pageant key #%d", s->keyi); |
| 6329 | logevent(buf); |
| 6330 | } |
| 6331 | s->pklen = GET_32BIT(s->p); |
| 6332 | s->p += 4; |
| 6333 | if (s->publickey_blob && |
| 6334 | s->pklen == s->publickey_bloblen && |
| 6335 | !memcmp(s->p, s->publickey_blob, |
| 6336 | s->publickey_bloblen)) { |
| 6337 | logevent("This key matches configured key file"); |
| 6338 | s->tried_pubkey_config = 1; |
| 6339 | } |
| 6340 | s->pkblob = (char *)s->p; |
| 6341 | s->p += s->pklen; |
| 6342 | s->alglen = GET_32BIT(s->pkblob); |
| 6343 | s->alg = s->pkblob + 4; |
| 6344 | s->commentlen = GET_32BIT(s->p); |
| 6345 | s->p += 4; |
| 6346 | s->commentp = (char *)s->p; |
| 6347 | s->p += s->commentlen; |
| 6348 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
| 6349 | ssh2_pkt_addstring(s->pktout, s->username); |
| 6350 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); /* service requested */ |
| 6351 | ssh2_pkt_addstring(s->pktout, "publickey"); /* method */ |
| 6352 | ssh2_pkt_addbool(s->pktout, FALSE); /* no signature included */ |
| 6353 | ssh2_pkt_addstring_start(s->pktout); |
| 6354 | ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen); |
| 6355 | ssh2_pkt_addstring_start(s->pktout); |
| 6356 | ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen); |
| 6357 | ssh2_pkt_send(ssh, s->pktout); |
| 6358 | |
| 6359 | crWaitUntilV(pktin); |
| 6360 | if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) { |
| 6361 | logevent("Key refused"); |
| 6362 | continue; |
| 6363 | } |
| 6364 | |
| 6365 | if (flags & FLAG_VERBOSE) { |
| 6366 | c_write_str(ssh, "Authenticating with " |
| 6367 | "public key \""); |
| 6368 | c_write(ssh, s->commentp, s->commentlen); |
| 6369 | c_write_str(ssh, "\" from agent\r\n"); |
| 6370 | } |
| 6371 | |
| 6372 | /* |
| 6373 | * Server is willing to accept the key. |
| 6374 | * Construct a SIGN_REQUEST. |
| 6375 | */ |
| 6376 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
| 6377 | ssh2_pkt_addstring(s->pktout, s->username); |
| 6378 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); /* service requested */ |
| 6379 | ssh2_pkt_addstring(s->pktout, "publickey"); /* method */ |
| 6380 | ssh2_pkt_addbool(s->pktout, TRUE); |
| 6381 | ssh2_pkt_addstring_start(s->pktout); |
| 6382 | ssh2_pkt_addstring_data(s->pktout, s->alg, s->alglen); |
| 6383 | ssh2_pkt_addstring_start(s->pktout); |
| 6384 | ssh2_pkt_addstring_data(s->pktout, s->pkblob, s->pklen); |
| 6385 | |
| 6386 | s->siglen = s->pktout->length - 5 + 4 + 20; |
| 6387 | if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID) |
| 6388 | s->siglen -= 4; |
| 6389 | s->len = 1; /* message type */ |
| 6390 | s->len += 4 + s->pklen; /* key blob */ |
| 6391 | s->len += 4 + s->siglen; /* data to sign */ |
| 6392 | s->len += 4; /* flags */ |
| 6393 | s->agentreq = snewn(4 + s->len, char); |
| 6394 | PUT_32BIT(s->agentreq, s->len); |
| 6395 | s->q = s->agentreq + 4; |
| 6396 | *s->q++ = SSH2_AGENTC_SIGN_REQUEST; |
| 6397 | PUT_32BIT(s->q, s->pklen); |
| 6398 | s->q += 4; |
| 6399 | memcpy(s->q, s->pkblob, s->pklen); |
| 6400 | s->q += s->pklen; |
| 6401 | PUT_32BIT(s->q, s->siglen); |
| 6402 | s->q += 4; |
| 6403 | /* Now the data to be signed... */ |
| 6404 | if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) { |
| 6405 | PUT_32BIT(s->q, 20); |
| 6406 | s->q += 4; |
| 6407 | } |
| 6408 | memcpy(s->q, ssh->v2_session_id, 20); |
| 6409 | s->q += 20; |
| 6410 | memcpy(s->q, s->pktout->data + 5, |
| 6411 | s->pktout->length - 5); |
| 6412 | s->q += s->pktout->length - 5; |
| 6413 | /* And finally the (zero) flags word. */ |
| 6414 | PUT_32BIT(s->q, 0); |
| 6415 | if (!agent_query(s->agentreq, s->len + 4, |
| 6416 | &vret, &s->retlen, |
| 6417 | ssh_agent_callback, ssh)) { |
| 6418 | do { |
| 6419 | crReturnV; |
| 6420 | if (pktin) { |
| 6421 | bombout(("Unexpected data from server" |
| 6422 | " while waiting for agent" |
| 6423 | " response")); |
| 6424 | crStopV; |
| 6425 | } |
| 6426 | } while (pktin || inlen > 0); |
| 6427 | vret = ssh->agent_response; |
| 6428 | s->retlen = ssh->agent_response_len; |
| 6429 | } |
| 6430 | s->ret = vret; |
| 6431 | sfree(s->agentreq); |
| 6432 | if (s->ret) { |
| 6433 | if (s->ret[4] == SSH2_AGENT_SIGN_RESPONSE) { |
| 6434 | logevent("Sending Pageant's response"); |
| 6435 | ssh2_add_sigblob(ssh, s->pktout, |
| 6436 | s->pkblob, s->pklen, |
| 6437 | s->ret + 9, |
| 6438 | GET_32BIT(s->ret + 5)); |
| 6439 | ssh2_pkt_send(ssh, s->pktout); |
| 6440 | s->authed = TRUE; |
| 6441 | break; |
| 6442 | } else { |
| 6443 | logevent |
| 6444 | ("Pageant failed to answer challenge"); |
| 6445 | sfree(s->ret); |
| 6446 | } |
| 6447 | } |
| 6448 | } |
| 6449 | if (s->authed) |
| 6450 | continue; |
| 6451 | } |
| 6452 | sfree(s->response); |
| 6453 | } |
| 6454 | |
| 6455 | if (!s->method && s->can_pubkey && s->publickey_blob |
| 6456 | && !s->tried_pubkey_config) { |
| 6457 | unsigned char *pub_blob; |
| 6458 | char *algorithm, *comment; |
| 6459 | int pub_blob_len; |
| 6460 | |
| 6461 | s->tried_pubkey_config = TRUE; |
| 6462 | |
| 6463 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
| 6464 | ssh->pkt_ctx |= SSH2_PKTCTX_PUBLICKEY; |
| 6465 | |
| 6466 | /* |
| 6467 | * Try the public key supplied in the configuration. |
| 6468 | * |
| 6469 | * First, offer the public blob to see if the server is |
| 6470 | * willing to accept it. |
| 6471 | */ |
| 6472 | pub_blob = |
| 6473 | (unsigned char *)ssh2_userkey_loadpub(&ssh->cfg.keyfile, |
| 6474 | &algorithm, |
| 6475 | &pub_blob_len, |
| 6476 | NULL); |
| 6477 | if (pub_blob) { |
| 6478 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
| 6479 | ssh2_pkt_addstring(s->pktout, s->username); |
| 6480 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); /* service requested */ |
| 6481 | ssh2_pkt_addstring(s->pktout, "publickey"); /* method */ |
| 6482 | ssh2_pkt_addbool(s->pktout, FALSE); /* no signature included */ |
| 6483 | ssh2_pkt_addstring(s->pktout, algorithm); |
| 6484 | ssh2_pkt_addstring_start(s->pktout); |
| 6485 | ssh2_pkt_addstring_data(s->pktout, (char *)pub_blob, |
| 6486 | pub_blob_len); |
| 6487 | ssh2_pkt_send(ssh, s->pktout); |
| 6488 | logevent("Offered public key"); |
| 6489 | |
| 6490 | crWaitUntilV(pktin); |
| 6491 | if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) { |
| 6492 | s->gotit = TRUE; |
| 6493 | s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD; |
| 6494 | continue; /* key refused; give up on it */ |
| 6495 | } |
| 6496 | |
| 6497 | logevent("Offer of public key accepted"); |
| 6498 | /* |
| 6499 | * Actually attempt a serious authentication using |
| 6500 | * the key. |
| 6501 | */ |
| 6502 | if (ssh2_userkey_encrypted(&ssh->cfg.keyfile, &comment)) { |
| 6503 | sprintf(s->pwprompt, |
| 6504 | "Passphrase for key \"%.100s\": ", |
| 6505 | comment); |
| 6506 | s->need_pw = TRUE; |
| 6507 | } else { |
| 6508 | s->need_pw = FALSE; |
| 6509 | } |
| 6510 | if (flags & FLAG_VERBOSE) { |
| 6511 | c_write_str(ssh, "Authenticating with public key \""); |
| 6512 | c_write_str(ssh, comment); |
| 6513 | c_write_str(ssh, "\"\r\n"); |
| 6514 | } |
| 6515 | s->method = AUTH_PUBLICKEY_FILE; |
| 6516 | } |
| 6517 | } |
| 6518 | |
| 6519 | if (!s->method && s->can_keyb_inter && !s->kbd_inter_refused && |
| 6520 | !s->kbd_inter_running) { |
| 6521 | s->method = AUTH_KEYBOARD_INTERACTIVE; |
| 6522 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
| 6523 | |
| 6524 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
| 6525 | ssh->pkt_ctx |= SSH2_PKTCTX_KBDINTER; |
| 6526 | |
| 6527 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
| 6528 | ssh2_pkt_addstring(s->pktout, s->username); |
| 6529 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); /* service requested */ |
| 6530 | ssh2_pkt_addstring(s->pktout, "keyboard-interactive"); /* method */ |
| 6531 | ssh2_pkt_addstring(s->pktout, ""); /* lang */ |
| 6532 | ssh2_pkt_addstring(s->pktout, ""); |
| 6533 | ssh2_pkt_send(ssh, s->pktout); |
| 6534 | |
| 6535 | crWaitUntilV(pktin); |
| 6536 | if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) { |
| 6537 | if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) |
| 6538 | s->gotit = TRUE; |
| 6539 | logevent("Keyboard-interactive authentication refused"); |
| 6540 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET; |
| 6541 | s->kbd_inter_refused = TRUE; /* don't try it again */ |
| 6542 | continue; |
| 6543 | } |
| 6544 | |
| 6545 | s->kbd_inter_running = TRUE; |
| 6546 | s->curr_prompt = 0; |
| 6547 | } |
| 6548 | |
| 6549 | if (s->kbd_inter_running) { |
| 6550 | s->method = AUTH_KEYBOARD_INTERACTIVE; |
| 6551 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
| 6552 | |
| 6553 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
| 6554 | ssh->pkt_ctx |= SSH2_PKTCTX_KBDINTER; |
| 6555 | |
| 6556 | if (s->curr_prompt == 0) { |
| 6557 | /* |
| 6558 | * We've got a fresh USERAUTH_INFO_REQUEST. |
| 6559 | * Display header data, and start going through |
| 6560 | * the prompts. |
| 6561 | */ |
| 6562 | char *name, *inst, *lang; |
| 6563 | int name_len, inst_len, lang_len; |
| 6564 | |
| 6565 | ssh_pkt_getstring(pktin, &name, &name_len); |
| 6566 | ssh_pkt_getstring(pktin, &inst, &inst_len); |
| 6567 | ssh_pkt_getstring(pktin, &lang, &lang_len); |
| 6568 | if (name_len > 0) { |
| 6569 | c_write_untrusted(ssh, name, name_len); |
| 6570 | c_write_str(ssh, "\r\n"); |
| 6571 | } |
| 6572 | if (inst_len > 0) { |
| 6573 | c_write_untrusted(ssh, inst, inst_len); |
| 6574 | c_write_str(ssh, "\r\n"); |
| 6575 | } |
| 6576 | s->num_prompts = ssh_pkt_getuint32(pktin); |
| 6577 | } |
| 6578 | |
| 6579 | /* |
| 6580 | * If there are prompts remaining in the packet, |
| 6581 | * display one and get a response. |
| 6582 | */ |
| 6583 | if (s->curr_prompt < s->num_prompts) { |
| 6584 | char *prompt; |
| 6585 | int prompt_len; |
| 6586 | |
| 6587 | ssh_pkt_getstring(pktin, &prompt, &prompt_len); |
| 6588 | if (prompt_len > 0) { |
| 6589 | static const char trunc[] = "<prompt truncated>: "; |
| 6590 | static const int prlen = sizeof(s->pwprompt) - |
| 6591 | lenof(trunc); |
| 6592 | if (prompt_len > prlen) { |
| 6593 | memcpy(s->pwprompt, prompt, prlen); |
| 6594 | strcpy(s->pwprompt + prlen, trunc); |
| 6595 | } else { |
| 6596 | memcpy(s->pwprompt, prompt, prompt_len); |
| 6597 | s->pwprompt[prompt_len] = '\0'; |
| 6598 | } |
| 6599 | } else { |
| 6600 | strcpy(s->pwprompt, |
| 6601 | "<server failed to send prompt>: "); |
| 6602 | } |
| 6603 | s->echo = ssh2_pkt_getbool(pktin); |
| 6604 | s->need_pw = TRUE; |
| 6605 | } else |
| 6606 | s->need_pw = FALSE; |
| 6607 | } |
| 6608 | |
| 6609 | if (!s->method && s->can_passwd) { |
| 6610 | s->method = AUTH_PASSWORD; |
| 6611 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
| 6612 | ssh->pkt_ctx |= SSH2_PKTCTX_PASSWORD; |
| 6613 | sprintf(s->pwprompt, "%.90s@%.90s's password: ", s->username, |
| 6614 | ssh->savedhost); |
| 6615 | s->need_pw = TRUE; |
| 6616 | } |
| 6617 | |
| 6618 | if (s->need_pw) { |
| 6619 | if (ssh_get_line) { |
| 6620 | if (!ssh_get_line(s->pwprompt, s->password, |
| 6621 | sizeof(s->password), TRUE)) { |
| 6622 | /* |
| 6623 | * get_line failed to get a password (for |
| 6624 | * example because one was supplied on the |
| 6625 | * command line which has already failed to |
| 6626 | * work). Terminate. |
| 6627 | */ |
| 6628 | s->pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
| 6629 | ssh2_pkt_adduint32(s->pktout,SSH2_DISCONNECT_BY_APPLICATION); |
| 6630 | ssh2_pkt_addstring(s->pktout, "No more passwords available" |
| 6631 | " to try"); |
| 6632 | ssh2_pkt_addstring(s->pktout, "en"); /* language tag */ |
| 6633 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
| 6634 | logevent("Unable to authenticate"); |
| 6635 | connection_fatal(ssh->frontend, |
| 6636 | "Unable to authenticate"); |
| 6637 | ssh->close_expected = TRUE; |
| 6638 | ssh_closing((Plug)ssh, NULL, 0, 0); |
| 6639 | crStopV; |
| 6640 | } |
| 6641 | } else { |
| 6642 | int ret; /* need not be saved across crReturn */ |
| 6643 | c_write_untrusted(ssh, s->pwprompt, strlen(s->pwprompt)); |
| 6644 | ssh->send_ok = 1; |
| 6645 | |
| 6646 | setup_userpass_input(ssh, s->password, |
| 6647 | sizeof(s->password), s->echo); |
| 6648 | do { |
| 6649 | crWaitUntilV(!pktin); |
| 6650 | ret = process_userpass_input(ssh, in, inlen); |
| 6651 | } while (ret == 0); |
| 6652 | if (ret < 0) |
| 6653 | cleanup_exit(0); |
| 6654 | c_write_str(ssh, "\r\n"); |
| 6655 | } |
| 6656 | } |
| 6657 | |
| 6658 | if (s->method == AUTH_PUBLICKEY_FILE) { |
| 6659 | /* |
| 6660 | * We have our passphrase. Now try the actual authentication. |
| 6661 | */ |
| 6662 | struct ssh2_userkey *key; |
| 6663 | const char *error = NULL; |
| 6664 | |
| 6665 | key = ssh2_load_userkey(&ssh->cfg.keyfile, s->password, |
| 6666 | &error); |
| 6667 | if (key == SSH2_WRONG_PASSPHRASE || key == NULL) { |
| 6668 | if (key == SSH2_WRONG_PASSPHRASE) { |
| 6669 | c_write_str(ssh, "Wrong passphrase\r\n"); |
| 6670 | s->tried_pubkey_config = FALSE; |
| 6671 | } else { |
| 6672 | c_write_str(ssh, "Unable to load private key ("); |
| 6673 | c_write_str(ssh, error); |
| 6674 | c_write_str(ssh, ")\r\n"); |
| 6675 | s->tried_pubkey_config = TRUE; |
| 6676 | } |
| 6677 | /* Send a spurious AUTH_NONE to return to the top. */ |
| 6678 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
| 6679 | ssh2_pkt_addstring(s->pktout, s->username); |
| 6680 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); /* service requested */ |
| 6681 | ssh2_pkt_addstring(s->pktout, "none"); /* method */ |
| 6682 | ssh2_pkt_send(ssh, s->pktout); |
| 6683 | s->type = AUTH_TYPE_NONE; |
| 6684 | } else { |
| 6685 | unsigned char *pkblob, *sigblob, *sigdata; |
| 6686 | int pkblob_len, sigblob_len, sigdata_len; |
| 6687 | int p; |
| 6688 | |
| 6689 | /* |
| 6690 | * We have loaded the private key and the server |
| 6691 | * has announced that it's willing to accept it. |
| 6692 | * Hallelujah. Generate a signature and send it. |
| 6693 | */ |
| 6694 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
| 6695 | ssh2_pkt_addstring(s->pktout, s->username); |
| 6696 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); /* service requested */ |
| 6697 | ssh2_pkt_addstring(s->pktout, "publickey"); /* method */ |
| 6698 | ssh2_pkt_addbool(s->pktout, TRUE); |
| 6699 | ssh2_pkt_addstring(s->pktout, key->alg->name); |
| 6700 | pkblob = key->alg->public_blob(key->data, &pkblob_len); |
| 6701 | ssh2_pkt_addstring_start(s->pktout); |
| 6702 | ssh2_pkt_addstring_data(s->pktout, (char *)pkblob, pkblob_len); |
| 6703 | |
| 6704 | /* |
| 6705 | * The data to be signed is: |
| 6706 | * |
| 6707 | * string session-id |
| 6708 | * |
| 6709 | * followed by everything so far placed in the |
| 6710 | * outgoing packet. |
| 6711 | */ |
| 6712 | sigdata_len = s->pktout->length - 5 + 4 + 20; |
| 6713 | if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID) |
| 6714 | sigdata_len -= 4; |
| 6715 | sigdata = snewn(sigdata_len, unsigned char); |
| 6716 | p = 0; |
| 6717 | if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) { |
| 6718 | PUT_32BIT(sigdata+p, 20); |
| 6719 | p += 4; |
| 6720 | } |
| 6721 | memcpy(sigdata+p, ssh->v2_session_id, 20); p += 20; |
| 6722 | memcpy(sigdata+p, s->pktout->data + 5, |
| 6723 | s->pktout->length - 5); |
| 6724 | p += s->pktout->length - 5; |
| 6725 | assert(p == sigdata_len); |
| 6726 | sigblob = key->alg->sign(key->data, (char *)sigdata, |
| 6727 | sigdata_len, &sigblob_len); |
| 6728 | ssh2_add_sigblob(ssh, s->pktout, pkblob, pkblob_len, |
| 6729 | sigblob, sigblob_len); |
| 6730 | sfree(pkblob); |
| 6731 | sfree(sigblob); |
| 6732 | sfree(sigdata); |
| 6733 | |
| 6734 | ssh2_pkt_send(ssh, s->pktout); |
| 6735 | s->type = AUTH_TYPE_PUBLICKEY; |
| 6736 | key->alg->freekey(key->data); |
| 6737 | } |
| 6738 | } else if (s->method == AUTH_PASSWORD) { |
| 6739 | /* |
| 6740 | * We send the password packet lumped tightly together with |
| 6741 | * an SSH_MSG_IGNORE packet. The IGNORE packet contains a |
| 6742 | * string long enough to make the total length of the two |
| 6743 | * packets constant. This should ensure that a passive |
| 6744 | * listener doing traffic analyis can't work out the length |
| 6745 | * of the password. |
| 6746 | * |
| 6747 | * For this to work, we need an assumption about the |
| 6748 | * maximum length of the password packet. I think 256 is |
| 6749 | * pretty conservative. Anyone using a password longer than |
| 6750 | * that probably doesn't have much to worry about from |
| 6751 | * people who find out how long their password is! |
| 6752 | */ |
| 6753 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
| 6754 | ssh2_pkt_addstring(s->pktout, s->username); |
| 6755 | ssh2_pkt_addstring(s->pktout, "ssh-connection"); /* service requested */ |
| 6756 | ssh2_pkt_addstring(s->pktout, "password"); |
| 6757 | ssh2_pkt_addbool(s->pktout, FALSE); |
| 6758 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
| 6759 | ssh2_pkt_addstring(s->pktout, s->password); |
| 6760 | memset(s->password, 0, sizeof(s->password)); |
| 6761 | end_log_omission(ssh, s->pktout); |
| 6762 | ssh2_pkt_defer(ssh, s->pktout); |
| 6763 | /* |
| 6764 | * We'll include a string that's an exact multiple of the |
| 6765 | * cipher block size. If the cipher is NULL for some |
| 6766 | * reason, we don't do this trick at all because we gain |
| 6767 | * nothing by it. |
| 6768 | */ |
| 6769 | if (ssh->cscipher) { |
| 6770 | int stringlen, i; |
| 6771 | |
| 6772 | stringlen = (256 - ssh->deferred_len); |
| 6773 | stringlen += ssh->cscipher->blksize - 1; |
| 6774 | stringlen -= (stringlen % ssh->cscipher->blksize); |
| 6775 | if (ssh->cscomp) { |
| 6776 | /* |
| 6777 | * Temporarily disable actual compression, |
| 6778 | * so we can guarantee to get this string |
| 6779 | * exactly the length we want it. The |
| 6780 | * compression-disabling routine should |
| 6781 | * return an integer indicating how many |
| 6782 | * bytes we should adjust our string length |
| 6783 | * by. |
| 6784 | */ |
| 6785 | stringlen -= |
| 6786 | ssh->cscomp->disable_compression(ssh->cs_comp_ctx); |
| 6787 | } |
| 6788 | s->pktout = ssh2_pkt_init(SSH2_MSG_IGNORE); |
| 6789 | ssh2_pkt_addstring_start(s->pktout); |
| 6790 | for (i = 0; i < stringlen; i++) { |
| 6791 | char c = (char) random_byte(); |
| 6792 | ssh2_pkt_addstring_data(s->pktout, &c, 1); |
| 6793 | } |
| 6794 | ssh2_pkt_defer(ssh, s->pktout); |
| 6795 | } |
| 6796 | ssh_pkt_defersend(ssh); |
| 6797 | logevent("Sent password"); |
| 6798 | s->type = AUTH_TYPE_PASSWORD; |
| 6799 | } else if (s->method == AUTH_KEYBOARD_INTERACTIVE) { |
| 6800 | if (s->curr_prompt == 0) { |
| 6801 | s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
| 6802 | ssh2_pkt_adduint32(s->pktout, s->num_prompts); |
| 6803 | } |
| 6804 | if (s->need_pw) { /* only add pw if we just got one! */ |
| 6805 | dont_log_password(ssh, s->pktout, PKTLOG_BLANK); |
| 6806 | ssh2_pkt_addstring(s->pktout, s->password); |
| 6807 | memset(s->password, 0, sizeof(s->password)); |
| 6808 | end_log_omission(ssh, s->pktout); |
| 6809 | s->curr_prompt++; |
| 6810 | } |
| 6811 | if (s->curr_prompt >= s->num_prompts) { |
| 6812 | ssh2_pkt_send(ssh, s->pktout); |
| 6813 | } else { |
| 6814 | /* |
| 6815 | * If there are prompts remaining, we set |
| 6816 | * `gotit' so that we won't attempt to get |
| 6817 | * another packet. Then we go back round the |
| 6818 | * loop and will end up retrieving another |
| 6819 | * prompt out of the existing packet. Funky or |
| 6820 | * what? |
| 6821 | */ |
| 6822 | s->gotit = TRUE; |
| 6823 | } |
| 6824 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
| 6825 | } else { |
| 6826 | c_write_str(ssh, "No supported authentication methods" |
| 6827 | " left to try!\r\n"); |
| 6828 | logevent("No supported authentications offered." |
| 6829 | " Disconnecting"); |
| 6830 | s->pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
| 6831 | ssh2_pkt_adduint32(s->pktout, SSH2_DISCONNECT_BY_APPLICATION); |
| 6832 | ssh2_pkt_addstring(s->pktout, "No supported authentication" |
| 6833 | " methods available"); |
| 6834 | ssh2_pkt_addstring(s->pktout, "en"); /* language tag */ |
| 6835 | ssh2_pkt_send_noqueue(ssh, s->pktout); |
| 6836 | ssh->close_expected = TRUE; |
| 6837 | ssh_closing((Plug)ssh, NULL, 0, 0); |
| 6838 | crStopV; |
| 6839 | } |
| 6840 | } |
| 6841 | } while (!s->we_are_in); |
| 6842 | |
| 6843 | /* |
| 6844 | * Now we're authenticated for the connection protocol. The |
| 6845 | * connection protocol will automatically have started at this |
| 6846 | * point; there's no need to send SERVICE_REQUEST. |
| 6847 | */ |
| 6848 | |
| 6849 | ssh->channels = newtree234(ssh_channelcmp); |
| 6850 | |
| 6851 | /* |
| 6852 | * Set up handlers for some connection protocol messages, so we |
| 6853 | * don't have to handle them repeatedly in this coroutine. |
| 6854 | */ |
| 6855 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = |
| 6856 | ssh2_msg_channel_window_adjust; |
| 6857 | ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = |
| 6858 | ssh2_msg_global_request; |
| 6859 | |
| 6860 | /* |
| 6861 | * Create the main session channel. |
| 6862 | */ |
| 6863 | if (!ssh->cfg.ssh_no_shell) { |
| 6864 | ssh->mainchan = snew(struct ssh_channel); |
| 6865 | ssh->mainchan->ssh = ssh; |
| 6866 | ssh->mainchan->localid = alloc_channel_id(ssh); |
| 6867 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
| 6868 | ssh2_pkt_addstring(s->pktout, "session"); |
| 6869 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->localid); |
| 6870 | ssh->mainchan->v.v2.locwindow = OUR_V2_WINSIZE; |
| 6871 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->v.v2.locwindow);/* our window size */ |
| 6872 | ssh2_pkt_adduint32(s->pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
| 6873 | ssh2_pkt_send(ssh, s->pktout); |
| 6874 | crWaitUntilV(pktin); |
| 6875 | if (pktin->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
| 6876 | bombout(("Server refused to open a session")); |
| 6877 | crStopV; |
| 6878 | /* FIXME: error data comes back in FAILURE packet */ |
| 6879 | } |
| 6880 | if (ssh_pkt_getuint32(pktin) != ssh->mainchan->localid) { |
| 6881 | bombout(("Server's channel confirmation cited wrong channel")); |
| 6882 | crStopV; |
| 6883 | } |
| 6884 | ssh->mainchan->remoteid = ssh_pkt_getuint32(pktin); |
| 6885 | ssh->mainchan->type = CHAN_MAINSESSION; |
| 6886 | ssh->mainchan->closes = 0; |
| 6887 | ssh->mainchan->v.v2.remwindow = ssh_pkt_getuint32(pktin); |
| 6888 | ssh->mainchan->v.v2.remmaxpkt = ssh_pkt_getuint32(pktin); |
| 6889 | bufchain_init(&ssh->mainchan->v.v2.outbuffer); |
| 6890 | add234(ssh->channels, ssh->mainchan); |
| 6891 | update_specials_menu(ssh->frontend); |
| 6892 | logevent("Opened channel for session"); |
| 6893 | } else |
| 6894 | ssh->mainchan = NULL; |
| 6895 | |
| 6896 | /* |
| 6897 | * Now we have a channel, make dispatch table entries for |
| 6898 | * general channel-based messages. |
| 6899 | */ |
| 6900 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = |
| 6901 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = |
| 6902 | ssh2_msg_channel_data; |
| 6903 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = ssh2_msg_channel_eof; |
| 6904 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = ssh2_msg_channel_close; |
| 6905 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = |
| 6906 | ssh2_msg_channel_open_confirmation; |
| 6907 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = |
| 6908 | ssh2_msg_channel_open_failure; |
| 6909 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] = |
| 6910 | ssh2_msg_channel_request; |
| 6911 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = |
| 6912 | ssh2_msg_channel_open; |
| 6913 | |
| 6914 | /* |
| 6915 | * Potentially enable X11 forwarding. |
| 6916 | */ |
| 6917 | if (ssh->mainchan && ssh->cfg.x11_forward) { |
| 6918 | char proto[20], data[64]; |
| 6919 | logevent("Requesting X11 forwarding"); |
| 6920 | ssh->x11auth = x11_invent_auth(proto, sizeof(proto), |
| 6921 | data, sizeof(data), ssh->cfg.x11_auth); |
| 6922 | x11_get_real_auth(ssh->x11auth, ssh->cfg.x11_display); |
| 6923 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
| 6924 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); |
| 6925 | ssh2_pkt_addstring(s->pktout, "x11-req"); |
| 6926 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
| 6927 | ssh2_pkt_addbool(s->pktout, 0); /* many connections */ |
| 6928 | ssh2_pkt_addstring(s->pktout, proto); |
| 6929 | ssh2_pkt_addstring(s->pktout, data); |
| 6930 | ssh2_pkt_adduint32(s->pktout, x11_get_screen_number(ssh->cfg.x11_display)); |
| 6931 | ssh2_pkt_send(ssh, s->pktout); |
| 6932 | |
| 6933 | crWaitUntilV(pktin); |
| 6934 | |
| 6935 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
| 6936 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
| 6937 | bombout(("Unexpected response to X11 forwarding request:" |
| 6938 | " packet type %d", pktin->type)); |
| 6939 | crStopV; |
| 6940 | } |
| 6941 | logevent("X11 forwarding refused"); |
| 6942 | } else { |
| 6943 | logevent("X11 forwarding enabled"); |
| 6944 | ssh->X11_fwd_enabled = TRUE; |
| 6945 | } |
| 6946 | } |
| 6947 | |
| 6948 | /* |
| 6949 | * Enable port forwardings. |
| 6950 | */ |
| 6951 | ssh_setup_portfwd(ssh, &ssh->cfg); |
| 6952 | |
| 6953 | /* |
| 6954 | * Potentially enable agent forwarding. |
| 6955 | */ |
| 6956 | if (ssh->mainchan && ssh->cfg.agentfwd && agent_exists()) { |
| 6957 | logevent("Requesting OpenSSH-style agent forwarding"); |
| 6958 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
| 6959 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); |
| 6960 | ssh2_pkt_addstring(s->pktout, "auth-agent-req@openssh.com"); |
| 6961 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
| 6962 | ssh2_pkt_send(ssh, s->pktout); |
| 6963 | |
| 6964 | crWaitUntilV(pktin); |
| 6965 | |
| 6966 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
| 6967 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
| 6968 | bombout(("Unexpected response to agent forwarding request:" |
| 6969 | " packet type %d", pktin->type)); |
| 6970 | crStopV; |
| 6971 | } |
| 6972 | logevent("Agent forwarding refused"); |
| 6973 | } else { |
| 6974 | logevent("Agent forwarding enabled"); |
| 6975 | ssh->agentfwd_enabled = TRUE; |
| 6976 | } |
| 6977 | } |
| 6978 | |
| 6979 | /* |
| 6980 | * Now allocate a pty for the session. |
| 6981 | */ |
| 6982 | if (ssh->mainchan && !ssh->cfg.nopty) { |
| 6983 | /* Unpick the terminal-speed string. */ |
| 6984 | /* XXX perhaps we should allow no speeds to be sent. */ |
| 6985 | ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */ |
| 6986 | sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed); |
| 6987 | /* Build the pty request. */ |
| 6988 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
| 6989 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); /* recipient channel */ |
| 6990 | ssh2_pkt_addstring(s->pktout, "pty-req"); |
| 6991 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
| 6992 | ssh2_pkt_addstring(s->pktout, ssh->cfg.termtype); |
| 6993 | ssh2_pkt_adduint32(s->pktout, ssh->term_width); |
| 6994 | ssh2_pkt_adduint32(s->pktout, ssh->term_height); |
| 6995 | ssh2_pkt_adduint32(s->pktout, 0); /* pixel width */ |
| 6996 | ssh2_pkt_adduint32(s->pktout, 0); /* pixel height */ |
| 6997 | ssh2_pkt_addstring_start(s->pktout); |
| 6998 | ssh2_pkt_addbyte(s->pktout, 128); /* TTY_OP_ISPEED */ |
| 6999 | ssh2_pkt_adduint32(s->pktout, ssh->ispeed); |
| 7000 | ssh2_pkt_addbyte(s->pktout, 129); /* TTY_OP_OSPEED */ |
| 7001 | ssh2_pkt_adduint32(s->pktout, ssh->ospeed); |
| 7002 | ssh2_pkt_addstring_data(s->pktout, "\0", 1); /* TTY_OP_END */ |
| 7003 | ssh2_pkt_send(ssh, s->pktout); |
| 7004 | ssh->state = SSH_STATE_INTERMED; |
| 7005 | |
| 7006 | crWaitUntilV(pktin); |
| 7007 | |
| 7008 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
| 7009 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
| 7010 | bombout(("Unexpected response to pty request:" |
| 7011 | " packet type %d", pktin->type)); |
| 7012 | crStopV; |
| 7013 | } |
| 7014 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
| 7015 | ssh->editing = ssh->echoing = 1; |
| 7016 | } else { |
| 7017 | logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)", |
| 7018 | ssh->ospeed, ssh->ispeed); |
| 7019 | } |
| 7020 | } else { |
| 7021 | ssh->editing = ssh->echoing = 1; |
| 7022 | } |
| 7023 | |
| 7024 | /* |
| 7025 | * Send environment variables. |
| 7026 | * |
| 7027 | * Simplest thing here is to send all the requests at once, and |
| 7028 | * then wait for a whole bunch of successes or failures. |
| 7029 | */ |
| 7030 | if (ssh->mainchan && *ssh->cfg.environmt) { |
| 7031 | char *e = ssh->cfg.environmt; |
| 7032 | char *var, *varend, *val; |
| 7033 | |
| 7034 | s->num_env = 0; |
| 7035 | |
| 7036 | while (*e) { |
| 7037 | var = e; |
| 7038 | while (*e && *e != '\t') e++; |
| 7039 | varend = e; |
| 7040 | if (*e == '\t') e++; |
| 7041 | val = e; |
| 7042 | while (*e) e++; |
| 7043 | e++; |
| 7044 | |
| 7045 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
| 7046 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); |
| 7047 | ssh2_pkt_addstring(s->pktout, "env"); |
| 7048 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
| 7049 | ssh2_pkt_addstring_start(s->pktout); |
| 7050 | ssh2_pkt_addstring_data(s->pktout, var, varend-var); |
| 7051 | ssh2_pkt_addstring(s->pktout, val); |
| 7052 | ssh2_pkt_send(ssh, s->pktout); |
| 7053 | |
| 7054 | s->num_env++; |
| 7055 | } |
| 7056 | |
| 7057 | logeventf(ssh, "Sent %d environment variables", s->num_env); |
| 7058 | |
| 7059 | s->env_ok = 0; |
| 7060 | s->env_left = s->num_env; |
| 7061 | |
| 7062 | while (s->env_left > 0) { |
| 7063 | crWaitUntilV(pktin); |
| 7064 | |
| 7065 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
| 7066 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
| 7067 | bombout(("Unexpected response to environment request:" |
| 7068 | " packet type %d", pktin->type)); |
| 7069 | crStopV; |
| 7070 | } |
| 7071 | } else { |
| 7072 | s->env_ok++; |
| 7073 | } |
| 7074 | |
| 7075 | s->env_left--; |
| 7076 | } |
| 7077 | |
| 7078 | if (s->env_ok == s->num_env) { |
| 7079 | logevent("All environment variables successfully set"); |
| 7080 | } else if (s->env_ok == 0) { |
| 7081 | logevent("All environment variables refused"); |
| 7082 | c_write_str(ssh, "Server refused to set environment variables\r\n"); |
| 7083 | } else { |
| 7084 | logeventf(ssh, "%d environment variables refused", |
| 7085 | s->num_env - s->env_ok); |
| 7086 | c_write_str(ssh, "Server refused to set all environment variables\r\n"); |
| 7087 | } |
| 7088 | } |
| 7089 | |
| 7090 | /* |
| 7091 | * Start a shell or a remote command. We may have to attempt |
| 7092 | * this twice if the config data has provided a second choice |
| 7093 | * of command. |
| 7094 | */ |
| 7095 | if (ssh->mainchan) while (1) { |
| 7096 | int subsys; |
| 7097 | char *cmd; |
| 7098 | |
| 7099 | if (ssh->fallback_cmd) { |
| 7100 | subsys = ssh->cfg.ssh_subsys2; |
| 7101 | cmd = ssh->cfg.remote_cmd_ptr2; |
| 7102 | } else { |
| 7103 | subsys = ssh->cfg.ssh_subsys; |
| 7104 | cmd = ssh->cfg.remote_cmd_ptr; |
| 7105 | } |
| 7106 | |
| 7107 | s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
| 7108 | ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); /* recipient channel */ |
| 7109 | if (subsys) { |
| 7110 | ssh2_pkt_addstring(s->pktout, "subsystem"); |
| 7111 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
| 7112 | ssh2_pkt_addstring(s->pktout, cmd); |
| 7113 | } else if (*cmd) { |
| 7114 | ssh2_pkt_addstring(s->pktout, "exec"); |
| 7115 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
| 7116 | ssh2_pkt_addstring(s->pktout, cmd); |
| 7117 | } else { |
| 7118 | ssh2_pkt_addstring(s->pktout, "shell"); |
| 7119 | ssh2_pkt_addbool(s->pktout, 1); /* want reply */ |
| 7120 | } |
| 7121 | ssh2_pkt_send(ssh, s->pktout); |
| 7122 | |
| 7123 | crWaitUntilV(pktin); |
| 7124 | |
| 7125 | if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) { |
| 7126 | if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) { |
| 7127 | bombout(("Unexpected response to shell/command request:" |
| 7128 | " packet type %d", pktin->type)); |
| 7129 | crStopV; |
| 7130 | } |
| 7131 | /* |
| 7132 | * We failed to start the command. If this is the |
| 7133 | * fallback command, we really are finished; if it's |
| 7134 | * not, and if the fallback command exists, try falling |
| 7135 | * back to it before complaining. |
| 7136 | */ |
| 7137 | if (!ssh->fallback_cmd && ssh->cfg.remote_cmd_ptr2 != NULL) { |
| 7138 | logevent("Primary command failed; attempting fallback"); |
| 7139 | ssh->fallback_cmd = TRUE; |
| 7140 | continue; |
| 7141 | } |
| 7142 | bombout(("Server refused to start a shell/command")); |
| 7143 | crStopV; |
| 7144 | } else { |
| 7145 | logevent("Started a shell/command"); |
| 7146 | } |
| 7147 | break; |
| 7148 | } |
| 7149 | |
| 7150 | ssh->state = SSH_STATE_SESSION; |
| 7151 | if (ssh->size_needed) |
| 7152 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
| 7153 | if (ssh->eof_needed) |
| 7154 | ssh_special(ssh, TS_EOF); |
| 7155 | |
| 7156 | /* |
| 7157 | * Transfer data! |
| 7158 | */ |
| 7159 | if (ssh->ldisc) |
| 7160 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
| 7161 | if (ssh->mainchan) |
| 7162 | ssh->send_ok = 1; |
| 7163 | while (1) { |
| 7164 | crReturnV; |
| 7165 | s->try_send = FALSE; |
| 7166 | if (pktin) { |
| 7167 | |
| 7168 | /* |
| 7169 | * _All_ the connection-layer packets we expect to |
| 7170 | * receive are now handled by the dispatch table. |
| 7171 | * Anything that reaches here must be bogus. |
| 7172 | */ |
| 7173 | |
| 7174 | bombout(("Strange packet received: type %d", pktin->type)); |
| 7175 | crStopV; |
| 7176 | } else if (ssh->mainchan) { |
| 7177 | /* |
| 7178 | * We have spare data. Add it to the channel buffer. |
| 7179 | */ |
| 7180 | ssh2_add_channel_data(ssh->mainchan, (char *)in, inlen); |
| 7181 | s->try_send = TRUE; |
| 7182 | } |
| 7183 | if (s->try_send) { |
| 7184 | int i; |
| 7185 | struct ssh_channel *c; |
| 7186 | /* |
| 7187 | * Try to send data on all channels if we can. |
| 7188 | */ |
| 7189 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
| 7190 | int bufsize; |
| 7191 | if (c->closes) |
| 7192 | continue; /* don't send on closing channels */ |
| 7193 | bufsize = ssh2_try_send(c); |
| 7194 | if (bufsize == 0) { |
| 7195 | switch (c->type) { |
| 7196 | case CHAN_MAINSESSION: |
| 7197 | /* stdin need not receive an unthrottle |
| 7198 | * notification since it will be polled */ |
| 7199 | break; |
| 7200 | case CHAN_X11: |
| 7201 | x11_unthrottle(c->u.x11.s); |
| 7202 | break; |
| 7203 | case CHAN_AGENT: |
| 7204 | /* agent sockets are request/response and need no |
| 7205 | * buffer management */ |
| 7206 | break; |
| 7207 | case CHAN_SOCKDATA: |
| 7208 | pfd_unthrottle(c->u.pfd.s); |
| 7209 | break; |
| 7210 | } |
| 7211 | } |
| 7212 | } |
| 7213 | } |
| 7214 | } |
| 7215 | |
| 7216 | crFinishV; |
| 7217 | } |
| 7218 | |
| 7219 | /* |
| 7220 | * Handlers for SSH2 messages that might arrive at any moment. |
| 7221 | */ |
| 7222 | void ssh2_msg_disconnect(Ssh ssh, struct Packet *pktin) |
| 7223 | { |
| 7224 | /* log reason code in disconnect message */ |
| 7225 | char *buf, *msg; |
| 7226 | int nowlen, reason, msglen; |
| 7227 | |
| 7228 | reason = ssh_pkt_getuint32(pktin); |
| 7229 | ssh_pkt_getstring(pktin, &msg, &msglen); |
| 7230 | |
| 7231 | if (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) { |
| 7232 | buf = dupprintf("Received disconnect message (%s)", |
| 7233 | ssh2_disconnect_reasons[reason]); |
| 7234 | } else { |
| 7235 | buf = dupprintf("Received disconnect message (unknown" |
| 7236 | " type %d)", reason); |
| 7237 | } |
| 7238 | logevent(buf); |
| 7239 | sfree(buf); |
| 7240 | buf = dupprintf("Disconnection message text: %n%.*s", |
| 7241 | &nowlen, msglen, msg); |
| 7242 | logevent(buf); |
| 7243 | bombout(("Server sent disconnect message\ntype %d (%s):\n\"%s\"", |
| 7244 | reason, |
| 7245 | (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) ? |
| 7246 | ssh2_disconnect_reasons[reason] : "unknown", |
| 7247 | buf+nowlen)); |
| 7248 | sfree(buf); |
| 7249 | } |
| 7250 | |
| 7251 | void ssh2_msg_debug(Ssh ssh, struct Packet *pktin) |
| 7252 | { |
| 7253 | /* log the debug message */ |
| 7254 | char *buf, *msg; |
| 7255 | int msglen; |
| 7256 | int always_display; |
| 7257 | |
| 7258 | /* XXX maybe we should actually take notice of this */ |
| 7259 | always_display = ssh2_pkt_getbool(pktin); |
| 7260 | ssh_pkt_getstring(pktin, &msg, &msglen); |
| 7261 | |
| 7262 | buf = dupprintf("Remote debug message: %.*s", msglen, msg); |
| 7263 | logevent(buf); |
| 7264 | sfree(buf); |
| 7265 | } |
| 7266 | |
| 7267 | void ssh2_msg_something_unimplemented(Ssh ssh, struct Packet *pktin) |
| 7268 | { |
| 7269 | struct Packet *pktout; |
| 7270 | pktout = ssh2_pkt_init(SSH2_MSG_UNIMPLEMENTED); |
| 7271 | ssh2_pkt_adduint32(pktout, pktin->sequence); |
| 7272 | /* |
| 7273 | * UNIMPLEMENTED messages MUST appear in the same order as the |
| 7274 | * messages they respond to. Hence, never queue them. |
| 7275 | */ |
| 7276 | ssh2_pkt_send_noqueue(ssh, pktout); |
| 7277 | } |
| 7278 | |
| 7279 | /* |
| 7280 | * Handle the top-level SSH2 protocol. |
| 7281 | */ |
| 7282 | static void ssh2_protocol_setup(Ssh ssh) |
| 7283 | { |
| 7284 | int i; |
| 7285 | |
| 7286 | /* |
| 7287 | * Most messages cause SSH2_MSG_UNIMPLEMENTED. |
| 7288 | */ |
| 7289 | for (i = 0; i < 256; i++) |
| 7290 | ssh->packet_dispatch[i] = ssh2_msg_something_unimplemented; |
| 7291 | |
| 7292 | /* |
| 7293 | * Any message we actually understand, we set to NULL so that |
| 7294 | * the coroutines will get it. |
| 7295 | */ |
| 7296 | ssh->packet_dispatch[SSH2_MSG_UNIMPLEMENTED] = NULL; |
| 7297 | ssh->packet_dispatch[SSH2_MSG_SERVICE_REQUEST] = NULL; |
| 7298 | ssh->packet_dispatch[SSH2_MSG_SERVICE_ACCEPT] = NULL; |
| 7299 | ssh->packet_dispatch[SSH2_MSG_KEXINIT] = NULL; |
| 7300 | ssh->packet_dispatch[SSH2_MSG_NEWKEYS] = NULL; |
| 7301 | ssh->packet_dispatch[SSH2_MSG_KEXDH_INIT] = NULL; |
| 7302 | ssh->packet_dispatch[SSH2_MSG_KEXDH_REPLY] = NULL; |
| 7303 | /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REQUEST] = NULL; duplicate case value */ |
| 7304 | /* ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_GROUP] = NULL; duplicate case value */ |
| 7305 | ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_INIT] = NULL; |
| 7306 | ssh->packet_dispatch[SSH2_MSG_KEX_DH_GEX_REPLY] = NULL; |
| 7307 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_REQUEST] = NULL; |
| 7308 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_FAILURE] = NULL; |
| 7309 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_SUCCESS] = NULL; |
| 7310 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_BANNER] = NULL; |
| 7311 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_PK_OK] = NULL; |
| 7312 | /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ] = NULL; duplicate case value */ |
| 7313 | /* ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_REQUEST] = NULL; duplicate case value */ |
| 7314 | ssh->packet_dispatch[SSH2_MSG_USERAUTH_INFO_RESPONSE] = NULL; |
| 7315 | ssh->packet_dispatch[SSH2_MSG_GLOBAL_REQUEST] = NULL; |
| 7316 | ssh->packet_dispatch[SSH2_MSG_REQUEST_SUCCESS] = NULL; |
| 7317 | ssh->packet_dispatch[SSH2_MSG_REQUEST_FAILURE] = NULL; |
| 7318 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] = NULL; |
| 7319 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_CONFIRMATION] = NULL; |
| 7320 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN_FAILURE] = NULL; |
| 7321 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_WINDOW_ADJUST] = NULL; |
| 7322 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_DATA] = NULL; |
| 7323 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EXTENDED_DATA] = NULL; |
| 7324 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_EOF] = NULL; |
| 7325 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_CLOSE] = NULL; |
| 7326 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_REQUEST] = NULL; |
| 7327 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_SUCCESS] = NULL; |
| 7328 | ssh->packet_dispatch[SSH2_MSG_CHANNEL_FAILURE] = NULL; |
| 7329 | |
| 7330 | /* |
| 7331 | * These special message types we install handlers for. |
| 7332 | */ |
| 7333 | ssh->packet_dispatch[SSH2_MSG_DISCONNECT] = ssh2_msg_disconnect; |
| 7334 | ssh->packet_dispatch[SSH2_MSG_IGNORE] = ssh_msg_ignore; /* shared with ssh1 */ |
| 7335 | ssh->packet_dispatch[SSH2_MSG_DEBUG] = ssh2_msg_debug; |
| 7336 | } |
| 7337 | |
| 7338 | static void ssh2_timer(void *ctx, long now) |
| 7339 | { |
| 7340 | Ssh ssh = (Ssh)ctx; |
| 7341 | |
| 7342 | if (!ssh->kex_in_progress && ssh->cfg.ssh_rekey_time != 0 && |
| 7343 | now - ssh->next_rekey >= 0) { |
| 7344 | do_ssh2_transport(ssh, "timeout", -1, NULL); |
| 7345 | } |
| 7346 | } |
| 7347 | |
| 7348 | static void ssh2_protocol(Ssh ssh, unsigned char *in, int inlen, |
| 7349 | struct Packet *pktin) |
| 7350 | { |
| 7351 | if (ssh->state == SSH_STATE_CLOSED) |
| 7352 | return; |
| 7353 | |
| 7354 | if (pktin) { |
| 7355 | ssh->incoming_data_size += pktin->encrypted_len; |
| 7356 | if (!ssh->kex_in_progress && |
| 7357 | ssh->max_data_size != 0 && |
| 7358 | ssh->incoming_data_size > ssh->max_data_size) |
| 7359 | do_ssh2_transport(ssh, "too much data received", -1, NULL); |
| 7360 | } |
| 7361 | |
| 7362 | if (pktin && ssh->packet_dispatch[pktin->type]) { |
| 7363 | ssh->packet_dispatch[pktin->type](ssh, pktin); |
| 7364 | return; |
| 7365 | } |
| 7366 | |
| 7367 | if (!ssh->protocol_initial_phase_done || |
| 7368 | (pktin && pktin->type >= 20 && pktin->type < 50)) { |
| 7369 | if (do_ssh2_transport(ssh, in, inlen, pktin) && |
| 7370 | !ssh->protocol_initial_phase_done) { |
| 7371 | ssh->protocol_initial_phase_done = TRUE; |
| 7372 | /* |
| 7373 | * Allow authconn to initialise itself. |
| 7374 | */ |
| 7375 | do_ssh2_authconn(ssh, NULL, 0, NULL); |
| 7376 | } |
| 7377 | } else { |
| 7378 | do_ssh2_authconn(ssh, in, inlen, pktin); |
| 7379 | } |
| 7380 | } |
| 7381 | |
| 7382 | /* |
| 7383 | * Called to set up the connection. |
| 7384 | * |
| 7385 | * Returns an error message, or NULL on success. |
| 7386 | */ |
| 7387 | static const char *ssh_init(void *frontend_handle, void **backend_handle, |
| 7388 | Config *cfg, |
| 7389 | char *host, int port, char **realhost, int nodelay, |
| 7390 | int keepalive) |
| 7391 | { |
| 7392 | const char *p; |
| 7393 | Ssh ssh; |
| 7394 | |
| 7395 | ssh = snew(struct ssh_tag); |
| 7396 | ssh->cfg = *cfg; /* STRUCTURE COPY */ |
| 7397 | ssh->version = 0; /* when not ready yet */ |
| 7398 | ssh->s = NULL; |
| 7399 | ssh->cipher = NULL; |
| 7400 | ssh->v1_cipher_ctx = NULL; |
| 7401 | ssh->crcda_ctx = NULL; |
| 7402 | ssh->cscipher = NULL; |
| 7403 | ssh->cs_cipher_ctx = NULL; |
| 7404 | ssh->sccipher = NULL; |
| 7405 | ssh->sc_cipher_ctx = NULL; |
| 7406 | ssh->csmac = NULL; |
| 7407 | ssh->cs_mac_ctx = NULL; |
| 7408 | ssh->scmac = NULL; |
| 7409 | ssh->sc_mac_ctx = NULL; |
| 7410 | ssh->cscomp = NULL; |
| 7411 | ssh->cs_comp_ctx = NULL; |
| 7412 | ssh->sccomp = NULL; |
| 7413 | ssh->sc_comp_ctx = NULL; |
| 7414 | ssh->kex = NULL; |
| 7415 | ssh->kex_ctx = NULL; |
| 7416 | ssh->hostkey = NULL; |
| 7417 | ssh->exitcode = -1; |
| 7418 | ssh->close_expected = FALSE; |
| 7419 | ssh->state = SSH_STATE_PREPACKET; |
| 7420 | ssh->size_needed = FALSE; |
| 7421 | ssh->eof_needed = FALSE; |
| 7422 | ssh->ldisc = NULL; |
| 7423 | ssh->logctx = NULL; |
| 7424 | ssh->deferred_send_data = NULL; |
| 7425 | ssh->deferred_len = 0; |
| 7426 | ssh->deferred_size = 0; |
| 7427 | ssh->fallback_cmd = 0; |
| 7428 | ssh->pkt_ctx = 0; |
| 7429 | ssh->x11auth = NULL; |
| 7430 | ssh->v1_compressing = FALSE; |
| 7431 | ssh->v2_outgoing_sequence = 0; |
| 7432 | ssh->ssh1_rdpkt_crstate = 0; |
| 7433 | ssh->ssh2_rdpkt_crstate = 0; |
| 7434 | ssh->do_ssh_init_crstate = 0; |
| 7435 | ssh->ssh_gotdata_crstate = 0; |
| 7436 | ssh->do_ssh1_connection_crstate = 0; |
| 7437 | ssh->do_ssh1_login_crstate = 0; |
| 7438 | ssh->do_ssh2_transport_crstate = 0; |
| 7439 | ssh->do_ssh2_authconn_crstate = 0; |
| 7440 | ssh->do_ssh_init_state = NULL; |
| 7441 | ssh->do_ssh1_login_state = NULL; |
| 7442 | ssh->do_ssh2_transport_state = NULL; |
| 7443 | ssh->do_ssh2_authconn_state = NULL; |
| 7444 | ssh->mainchan = NULL; |
| 7445 | ssh->throttled_all = 0; |
| 7446 | ssh->v1_stdout_throttling = 0; |
| 7447 | ssh->queue = NULL; |
| 7448 | ssh->queuelen = ssh->queuesize = 0; |
| 7449 | ssh->queueing = FALSE; |
| 7450 | ssh->qhead = ssh->qtail = NULL; |
| 7451 | ssh->deferred_rekey_reason = NULL; |
| 7452 | |
| 7453 | *backend_handle = ssh; |
| 7454 | |
| 7455 | #ifdef MSCRYPTOAPI |
| 7456 | if (crypto_startup() == 0) |
| 7457 | return "Microsoft high encryption pack not installed!"; |
| 7458 | #endif |
| 7459 | |
| 7460 | ssh->frontend = frontend_handle; |
| 7461 | ssh->term_width = ssh->cfg.width; |
| 7462 | ssh->term_height = ssh->cfg.height; |
| 7463 | |
| 7464 | ssh->channels = NULL; |
| 7465 | ssh->rportfwds = NULL; |
| 7466 | ssh->portfwds = NULL; |
| 7467 | |
| 7468 | ssh->send_ok = 0; |
| 7469 | ssh->editing = 0; |
| 7470 | ssh->echoing = 0; |
| 7471 | ssh->v1_throttle_count = 0; |
| 7472 | ssh->overall_bufsize = 0; |
| 7473 | ssh->fallback_cmd = 0; |
| 7474 | |
| 7475 | ssh->protocol = NULL; |
| 7476 | |
| 7477 | ssh->protocol_initial_phase_done = FALSE; |
| 7478 | |
| 7479 | ssh->pinger = NULL; |
| 7480 | |
| 7481 | ssh->incoming_data_size = ssh->outgoing_data_size = |
| 7482 | ssh->deferred_data_size = 0L; |
| 7483 | ssh->max_data_size = parse_blocksize(ssh->cfg.ssh_rekey_data); |
| 7484 | ssh->kex_in_progress = FALSE; |
| 7485 | |
| 7486 | p = connect_to_host(ssh, host, port, realhost, nodelay, keepalive); |
| 7487 | if (p != NULL) |
| 7488 | return p; |
| 7489 | |
| 7490 | random_ref(); |
| 7491 | |
| 7492 | return NULL; |
| 7493 | } |
| 7494 | |
| 7495 | static void ssh_free(void *handle) |
| 7496 | { |
| 7497 | Ssh ssh = (Ssh) handle; |
| 7498 | struct ssh_channel *c; |
| 7499 | struct ssh_rportfwd *pf; |
| 7500 | |
| 7501 | if (ssh->v1_cipher_ctx) |
| 7502 | ssh->cipher->free_context(ssh->v1_cipher_ctx); |
| 7503 | if (ssh->cs_cipher_ctx) |
| 7504 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
| 7505 | if (ssh->sc_cipher_ctx) |
| 7506 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
| 7507 | if (ssh->cs_mac_ctx) |
| 7508 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
| 7509 | if (ssh->sc_mac_ctx) |
| 7510 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
| 7511 | if (ssh->cs_comp_ctx) { |
| 7512 | if (ssh->cscomp) |
| 7513 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
| 7514 | else |
| 7515 | zlib_compress_cleanup(ssh->cs_comp_ctx); |
| 7516 | } |
| 7517 | if (ssh->sc_comp_ctx) { |
| 7518 | if (ssh->sccomp) |
| 7519 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
| 7520 | else |
| 7521 | zlib_decompress_cleanup(ssh->sc_comp_ctx); |
| 7522 | } |
| 7523 | if (ssh->kex_ctx) |
| 7524 | dh_cleanup(ssh->kex_ctx); |
| 7525 | sfree(ssh->savedhost); |
| 7526 | |
| 7527 | while (ssh->queuelen-- > 0) |
| 7528 | ssh_free_packet(ssh->queue[ssh->queuelen]); |
| 7529 | sfree(ssh->queue); |
| 7530 | |
| 7531 | while (ssh->qhead) { |
| 7532 | struct queued_handler *qh = ssh->qhead; |
| 7533 | ssh->qhead = qh->next; |
| 7534 | sfree(ssh->qhead); |
| 7535 | } |
| 7536 | ssh->qhead = ssh->qtail = NULL; |
| 7537 | |
| 7538 | if (ssh->channels) { |
| 7539 | while ((c = delpos234(ssh->channels, 0)) != NULL) { |
| 7540 | switch (c->type) { |
| 7541 | case CHAN_X11: |
| 7542 | if (c->u.x11.s != NULL) |
| 7543 | x11_close(c->u.x11.s); |
| 7544 | break; |
| 7545 | case CHAN_SOCKDATA: |
| 7546 | if (c->u.pfd.s != NULL) |
| 7547 | pfd_close(c->u.pfd.s); |
| 7548 | break; |
| 7549 | } |
| 7550 | sfree(c); |
| 7551 | } |
| 7552 | freetree234(ssh->channels); |
| 7553 | ssh->channels = NULL; |
| 7554 | } |
| 7555 | |
| 7556 | if (ssh->rportfwds) { |
| 7557 | while ((pf = delpos234(ssh->rportfwds, 0)) != NULL) |
| 7558 | sfree(pf); |
| 7559 | freetree234(ssh->rportfwds); |
| 7560 | ssh->rportfwds = NULL; |
| 7561 | } |
| 7562 | sfree(ssh->deferred_send_data); |
| 7563 | if (ssh->x11auth) |
| 7564 | x11_free_auth(ssh->x11auth); |
| 7565 | sfree(ssh->do_ssh_init_state); |
| 7566 | sfree(ssh->do_ssh1_login_state); |
| 7567 | sfree(ssh->do_ssh2_transport_state); |
| 7568 | sfree(ssh->do_ssh2_authconn_state); |
| 7569 | if (ssh->crcda_ctx) { |
| 7570 | crcda_free_context(ssh->crcda_ctx); |
| 7571 | ssh->crcda_ctx = NULL; |
| 7572 | } |
| 7573 | if (ssh->s) |
| 7574 | ssh_do_close(ssh, TRUE); |
| 7575 | expire_timer_context(ssh); |
| 7576 | if (ssh->pinger) |
| 7577 | pinger_free(ssh->pinger); |
| 7578 | sfree(ssh); |
| 7579 | |
| 7580 | random_unref(); |
| 7581 | } |
| 7582 | |
| 7583 | /* |
| 7584 | * Reconfigure the SSH backend. |
| 7585 | */ |
| 7586 | static void ssh_reconfig(void *handle, Config *cfg) |
| 7587 | { |
| 7588 | Ssh ssh = (Ssh) handle; |
| 7589 | char *rekeying = NULL, rekey_mandatory = FALSE; |
| 7590 | unsigned long old_max_data_size; |
| 7591 | |
| 7592 | pinger_reconfig(ssh->pinger, &ssh->cfg, cfg); |
| 7593 | ssh_setup_portfwd(ssh, cfg); |
| 7594 | |
| 7595 | if (ssh->cfg.ssh_rekey_time != cfg->ssh_rekey_time && |
| 7596 | cfg->ssh_rekey_time != 0) { |
| 7597 | long new_next = ssh->last_rekey + cfg->ssh_rekey_time*60*TICKSPERSEC; |
| 7598 | long now = GETTICKCOUNT(); |
| 7599 | |
| 7600 | if (new_next - now < 0) { |
| 7601 | rekeying = "timeout shortened"; |
| 7602 | } else { |
| 7603 | ssh->next_rekey = schedule_timer(new_next - now, ssh2_timer, ssh); |
| 7604 | } |
| 7605 | } |
| 7606 | |
| 7607 | old_max_data_size = ssh->max_data_size; |
| 7608 | ssh->max_data_size = parse_blocksize(cfg->ssh_rekey_data); |
| 7609 | if (old_max_data_size != ssh->max_data_size && |
| 7610 | ssh->max_data_size != 0) { |
| 7611 | if (ssh->outgoing_data_size > ssh->max_data_size || |
| 7612 | ssh->incoming_data_size > ssh->max_data_size) |
| 7613 | rekeying = "data limit lowered"; |
| 7614 | } |
| 7615 | |
| 7616 | if (ssh->cfg.compression != cfg->compression) { |
| 7617 | rekeying = "compression setting changed"; |
| 7618 | rekey_mandatory = TRUE; |
| 7619 | } |
| 7620 | |
| 7621 | if (ssh->cfg.ssh2_des_cbc != cfg->ssh2_des_cbc || |
| 7622 | memcmp(ssh->cfg.ssh_cipherlist, cfg->ssh_cipherlist, |
| 7623 | sizeof(ssh->cfg.ssh_cipherlist))) { |
| 7624 | rekeying = "cipher settings changed"; |
| 7625 | rekey_mandatory = TRUE; |
| 7626 | } |
| 7627 | |
| 7628 | ssh->cfg = *cfg; /* STRUCTURE COPY */ |
| 7629 | |
| 7630 | if (rekeying) { |
| 7631 | if (!ssh->kex_in_progress) { |
| 7632 | do_ssh2_transport(ssh, rekeying, -1, NULL); |
| 7633 | } else if (rekey_mandatory) { |
| 7634 | ssh->deferred_rekey_reason = rekeying; |
| 7635 | } |
| 7636 | } |
| 7637 | } |
| 7638 | |
| 7639 | /* |
| 7640 | * Called to send data down the Telnet connection. |
| 7641 | */ |
| 7642 | static int ssh_send(void *handle, char *buf, int len) |
| 7643 | { |
| 7644 | Ssh ssh = (Ssh) handle; |
| 7645 | |
| 7646 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
| 7647 | return 0; |
| 7648 | |
| 7649 | ssh->protocol(ssh, (unsigned char *)buf, len, 0); |
| 7650 | |
| 7651 | return ssh_sendbuffer(ssh); |
| 7652 | } |
| 7653 | |
| 7654 | /* |
| 7655 | * Called to query the current amount of buffered stdin data. |
| 7656 | */ |
| 7657 | static int ssh_sendbuffer(void *handle) |
| 7658 | { |
| 7659 | Ssh ssh = (Ssh) handle; |
| 7660 | int override_value; |
| 7661 | |
| 7662 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
| 7663 | return 0; |
| 7664 | |
| 7665 | /* |
| 7666 | * If the SSH socket itself has backed up, add the total backup |
| 7667 | * size on that to any individual buffer on the stdin channel. |
| 7668 | */ |
| 7669 | override_value = 0; |
| 7670 | if (ssh->throttled_all) |
| 7671 | override_value = ssh->overall_bufsize; |
| 7672 | |
| 7673 | if (ssh->version == 1) { |
| 7674 | return override_value; |
| 7675 | } else if (ssh->version == 2) { |
| 7676 | if (!ssh->mainchan || ssh->mainchan->closes > 0) |
| 7677 | return override_value; |
| 7678 | else |
| 7679 | return (override_value + |
| 7680 | bufchain_size(&ssh->mainchan->v.v2.outbuffer)); |
| 7681 | } |
| 7682 | |
| 7683 | return 0; |
| 7684 | } |
| 7685 | |
| 7686 | /* |
| 7687 | * Called to set the size of the window from SSH's POV. |
| 7688 | */ |
| 7689 | static void ssh_size(void *handle, int width, int height) |
| 7690 | { |
| 7691 | Ssh ssh = (Ssh) handle; |
| 7692 | struct Packet *pktout; |
| 7693 | |
| 7694 | ssh->term_width = width; |
| 7695 | ssh->term_height = height; |
| 7696 | |
| 7697 | switch (ssh->state) { |
| 7698 | case SSH_STATE_BEFORE_SIZE: |
| 7699 | case SSH_STATE_PREPACKET: |
| 7700 | case SSH_STATE_CLOSED: |
| 7701 | break; /* do nothing */ |
| 7702 | case SSH_STATE_INTERMED: |
| 7703 | ssh->size_needed = TRUE; /* buffer for later */ |
| 7704 | break; |
| 7705 | case SSH_STATE_SESSION: |
| 7706 | if (!ssh->cfg.nopty) { |
| 7707 | if (ssh->version == 1) { |
| 7708 | send_packet(ssh, SSH1_CMSG_WINDOW_SIZE, |
| 7709 | PKT_INT, ssh->term_height, |
| 7710 | PKT_INT, ssh->term_width, |
| 7711 | PKT_INT, 0, PKT_INT, 0, PKT_END); |
| 7712 | } else if (ssh->mainchan) { |
| 7713 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
| 7714 | ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid); |
| 7715 | ssh2_pkt_addstring(pktout, "window-change"); |
| 7716 | ssh2_pkt_addbool(pktout, 0); |
| 7717 | ssh2_pkt_adduint32(pktout, ssh->term_width); |
| 7718 | ssh2_pkt_adduint32(pktout, ssh->term_height); |
| 7719 | ssh2_pkt_adduint32(pktout, 0); |
| 7720 | ssh2_pkt_adduint32(pktout, 0); |
| 7721 | ssh2_pkt_send(ssh, pktout); |
| 7722 | } |
| 7723 | } |
| 7724 | break; |
| 7725 | } |
| 7726 | } |
| 7727 | |
| 7728 | /* |
| 7729 | * Return a list of the special codes that make sense in this |
| 7730 | * protocol. |
| 7731 | */ |
| 7732 | static const struct telnet_special *ssh_get_specials(void *handle) |
| 7733 | { |
| 7734 | static const struct telnet_special ssh1_ignore_special[] = { |
| 7735 | {"IGNORE message", TS_NOP} |
| 7736 | }; |
| 7737 | static const struct telnet_special ssh2_transport_specials[] = { |
| 7738 | {"IGNORE message", TS_NOP}, |
| 7739 | {"Repeat key exchange", TS_REKEY}, |
| 7740 | }; |
| 7741 | static const struct telnet_special ssh2_session_specials[] = { |
| 7742 | {NULL, TS_SEP}, |
| 7743 | {"Break", TS_BRK}, |
| 7744 | /* These are the signal names defined by draft-ietf-secsh-connect-23. |
| 7745 | * They include all the ISO C signals, but are a subset of the POSIX |
| 7746 | * required signals. */ |
| 7747 | {"SIGINT (Interrupt)", TS_SIGINT}, |
| 7748 | {"SIGTERM (Terminate)", TS_SIGTERM}, |
| 7749 | {"SIGKILL (Kill)", TS_SIGKILL}, |
| 7750 | {"SIGQUIT (Quit)", TS_SIGQUIT}, |
| 7751 | {"SIGHUP (Hangup)", TS_SIGHUP}, |
| 7752 | {"More signals", TS_SUBMENU}, |
| 7753 | {"SIGABRT", TS_SIGABRT}, {"SIGALRM", TS_SIGALRM}, |
| 7754 | {"SIGFPE", TS_SIGFPE}, {"SIGILL", TS_SIGILL}, |
| 7755 | {"SIGPIPE", TS_SIGPIPE}, {"SIGSEGV", TS_SIGSEGV}, |
| 7756 | {"SIGUSR1", TS_SIGUSR1}, {"SIGUSR2", TS_SIGUSR2}, |
| 7757 | {NULL, TS_EXITMENU} |
| 7758 | }; |
| 7759 | static const struct telnet_special specials_end[] = { |
| 7760 | {NULL, TS_EXITMENU} |
| 7761 | }; |
| 7762 | /* XXX review this length for any changes: */ |
| 7763 | static struct telnet_special ssh_specials[lenof(ssh2_transport_specials) + |
| 7764 | lenof(ssh2_session_specials) + |
| 7765 | lenof(specials_end)]; |
| 7766 | Ssh ssh = (Ssh) handle; |
| 7767 | int i = 0; |
| 7768 | #define ADD_SPECIALS(name) \ |
| 7769 | do { \ |
| 7770 | assert((i + lenof(name)) <= lenof(ssh_specials)); \ |
| 7771 | memcpy(&ssh_specials[i], name, sizeof name); \ |
| 7772 | i += lenof(name); \ |
| 7773 | } while(0) |
| 7774 | |
| 7775 | if (ssh->version == 1) { |
| 7776 | /* Don't bother offering IGNORE if we've decided the remote |
| 7777 | * won't cope with it, since we wouldn't bother sending it if |
| 7778 | * asked anyway. */ |
| 7779 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) |
| 7780 | ADD_SPECIALS(ssh1_ignore_special); |
| 7781 | } else if (ssh->version == 2) { |
| 7782 | ADD_SPECIALS(ssh2_transport_specials); |
| 7783 | if (ssh->mainchan) |
| 7784 | ADD_SPECIALS(ssh2_session_specials); |
| 7785 | } /* else we're not ready yet */ |
| 7786 | |
| 7787 | if (i) { |
| 7788 | ADD_SPECIALS(specials_end); |
| 7789 | return ssh_specials; |
| 7790 | } else { |
| 7791 | return NULL; |
| 7792 | } |
| 7793 | #undef ADD_SPECIALS |
| 7794 | } |
| 7795 | |
| 7796 | /* |
| 7797 | * Send Telnet special codes. TS_EOF is useful for `plink', so you |
| 7798 | * can send an EOF and collect resulting output (e.g. `plink |
| 7799 | * hostname sort'). |
| 7800 | */ |
| 7801 | static void ssh_special(void *handle, Telnet_Special code) |
| 7802 | { |
| 7803 | Ssh ssh = (Ssh) handle; |
| 7804 | struct Packet *pktout; |
| 7805 | |
| 7806 | if (code == TS_EOF) { |
| 7807 | if (ssh->state != SSH_STATE_SESSION) { |
| 7808 | /* |
| 7809 | * Buffer the EOF in case we are pre-SESSION, so we can |
| 7810 | * send it as soon as we reach SESSION. |
| 7811 | */ |
| 7812 | if (code == TS_EOF) |
| 7813 | ssh->eof_needed = TRUE; |
| 7814 | return; |
| 7815 | } |
| 7816 | if (ssh->version == 1) { |
| 7817 | send_packet(ssh, SSH1_CMSG_EOF, PKT_END); |
| 7818 | } else if (ssh->mainchan) { |
| 7819 | struct Packet *pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF); |
| 7820 | ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid); |
| 7821 | ssh2_pkt_send(ssh, pktout); |
| 7822 | } |
| 7823 | logevent("Sent EOF message"); |
| 7824 | } else if (code == TS_PING || code == TS_NOP) { |
| 7825 | if (ssh->state == SSH_STATE_CLOSED |
| 7826 | || ssh->state == SSH_STATE_PREPACKET) return; |
| 7827 | if (ssh->version == 1) { |
| 7828 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) |
| 7829 | send_packet(ssh, SSH1_MSG_IGNORE, PKT_STR, "", PKT_END); |
| 7830 | } else { |
| 7831 | pktout = ssh2_pkt_init(SSH2_MSG_IGNORE); |
| 7832 | ssh2_pkt_addstring_start(pktout); |
| 7833 | ssh2_pkt_send_noqueue(ssh, pktout); |
| 7834 | } |
| 7835 | } else if (code == TS_REKEY) { |
| 7836 | if (!ssh->kex_in_progress && ssh->version == 2) { |
| 7837 | do_ssh2_transport(ssh, "at user request", -1, NULL); |
| 7838 | } |
| 7839 | } else if (code == TS_BRK) { |
| 7840 | if (ssh->state == SSH_STATE_CLOSED |
| 7841 | || ssh->state == SSH_STATE_PREPACKET) return; |
| 7842 | if (ssh->version == 1) { |
| 7843 | logevent("Unable to send BREAK signal in SSH1"); |
| 7844 | } else if (ssh->mainchan) { |
| 7845 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
| 7846 | ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid); |
| 7847 | ssh2_pkt_addstring(pktout, "break"); |
| 7848 | ssh2_pkt_addbool(pktout, 0); |
| 7849 | ssh2_pkt_adduint32(pktout, 0); /* default break length */ |
| 7850 | ssh2_pkt_send(ssh, pktout); |
| 7851 | } |
| 7852 | } else { |
| 7853 | /* Is is a POSIX signal? */ |
| 7854 | char *signame = NULL; |
| 7855 | if (code == TS_SIGABRT) signame = "ABRT"; |
| 7856 | if (code == TS_SIGALRM) signame = "ALRM"; |
| 7857 | if (code == TS_SIGFPE) signame = "FPE"; |
| 7858 | if (code == TS_SIGHUP) signame = "HUP"; |
| 7859 | if (code == TS_SIGILL) signame = "ILL"; |
| 7860 | if (code == TS_SIGINT) signame = "INT"; |
| 7861 | if (code == TS_SIGKILL) signame = "KILL"; |
| 7862 | if (code == TS_SIGPIPE) signame = "PIPE"; |
| 7863 | if (code == TS_SIGQUIT) signame = "QUIT"; |
| 7864 | if (code == TS_SIGSEGV) signame = "SEGV"; |
| 7865 | if (code == TS_SIGTERM) signame = "TERM"; |
| 7866 | if (code == TS_SIGUSR1) signame = "USR1"; |
| 7867 | if (code == TS_SIGUSR2) signame = "USR2"; |
| 7868 | /* The SSH-2 protocol does in principle support arbitrary named |
| 7869 | * signals, including signame@domain, but we don't support those. */ |
| 7870 | if (signame) { |
| 7871 | /* It's a signal. */ |
| 7872 | if (ssh->version == 2 && ssh->mainchan) { |
| 7873 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
| 7874 | ssh2_pkt_adduint32(pktout, ssh->mainchan->remoteid); |
| 7875 | ssh2_pkt_addstring(pktout, "signal"); |
| 7876 | ssh2_pkt_addbool(pktout, 0); |
| 7877 | ssh2_pkt_addstring(pktout, signame); |
| 7878 | ssh2_pkt_send(ssh, pktout); |
| 7879 | logeventf(ssh, "Sent signal SIG%s", signame); |
| 7880 | } |
| 7881 | } else { |
| 7882 | /* Never heard of it. Do nothing */ |
| 7883 | } |
| 7884 | } |
| 7885 | } |
| 7886 | |
| 7887 | void *new_sock_channel(void *handle, Socket s) |
| 7888 | { |
| 7889 | Ssh ssh = (Ssh) handle; |
| 7890 | struct ssh_channel *c; |
| 7891 | c = snew(struct ssh_channel); |
| 7892 | c->ssh = ssh; |
| 7893 | |
| 7894 | if (c) { |
| 7895 | c->remoteid = -1; /* to be set when open confirmed */ |
| 7896 | c->localid = alloc_channel_id(ssh); |
| 7897 | c->closes = 0; |
| 7898 | c->type = CHAN_SOCKDATA_DORMANT;/* identify channel type */ |
| 7899 | c->u.pfd.s = s; |
| 7900 | bufchain_init(&c->v.v2.outbuffer); |
| 7901 | add234(ssh->channels, c); |
| 7902 | } |
| 7903 | return c; |
| 7904 | } |
| 7905 | |
| 7906 | /* |
| 7907 | * This is called when stdout/stderr (the entity to which |
| 7908 | * from_backend sends data) manages to clear some backlog. |
| 7909 | */ |
| 7910 | static void ssh_unthrottle(void *handle, int bufsize) |
| 7911 | { |
| 7912 | Ssh ssh = (Ssh) handle; |
| 7913 | if (ssh->version == 1) { |
| 7914 | if (ssh->v1_stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) { |
| 7915 | ssh->v1_stdout_throttling = 0; |
| 7916 | ssh1_throttle(ssh, -1); |
| 7917 | } |
| 7918 | } else { |
| 7919 | if (ssh->mainchan && ssh->mainchan->closes == 0) |
| 7920 | ssh2_set_window(ssh->mainchan, OUR_V2_WINSIZE - bufsize); |
| 7921 | } |
| 7922 | } |
| 7923 | |
| 7924 | void ssh_send_port_open(void *channel, char *hostname, int port, char *org) |
| 7925 | { |
| 7926 | struct ssh_channel *c = (struct ssh_channel *)channel; |
| 7927 | Ssh ssh = c->ssh; |
| 7928 | struct Packet *pktout; |
| 7929 | |
| 7930 | logeventf(ssh, "Opening forwarded connection to %s:%d", hostname, port); |
| 7931 | |
| 7932 | if (ssh->version == 1) { |
| 7933 | send_packet(ssh, SSH1_MSG_PORT_OPEN, |
| 7934 | PKT_INT, c->localid, |
| 7935 | PKT_STR, hostname, |
| 7936 | PKT_INT, port, |
| 7937 | /* PKT_STR, <org:orgport>, */ |
| 7938 | PKT_END); |
| 7939 | } else { |
| 7940 | pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
| 7941 | ssh2_pkt_addstring(pktout, "direct-tcpip"); |
| 7942 | ssh2_pkt_adduint32(pktout, c->localid); |
| 7943 | c->v.v2.locwindow = OUR_V2_WINSIZE; |
| 7944 | ssh2_pkt_adduint32(pktout, c->v.v2.locwindow);/* our window size */ |
| 7945 | ssh2_pkt_adduint32(pktout, OUR_V2_MAXPKT); /* our max pkt size */ |
| 7946 | ssh2_pkt_addstring(pktout, hostname); |
| 7947 | ssh2_pkt_adduint32(pktout, port); |
| 7948 | /* |
| 7949 | * We make up values for the originator data; partly it's |
| 7950 | * too much hassle to keep track, and partly I'm not |
| 7951 | * convinced the server should be told details like that |
| 7952 | * about my local network configuration. |
| 7953 | */ |
| 7954 | ssh2_pkt_addstring(pktout, "client-side-connection"); |
| 7955 | ssh2_pkt_adduint32(pktout, 0); |
| 7956 | ssh2_pkt_send(ssh, pktout); |
| 7957 | } |
| 7958 | } |
| 7959 | |
| 7960 | static Socket ssh_socket(void *handle) |
| 7961 | { |
| 7962 | Ssh ssh = (Ssh) handle; |
| 7963 | return ssh->s; |
| 7964 | } |
| 7965 | |
| 7966 | static int ssh_sendok(void *handle) |
| 7967 | { |
| 7968 | Ssh ssh = (Ssh) handle; |
| 7969 | return ssh->send_ok; |
| 7970 | } |
| 7971 | |
| 7972 | static int ssh_ldisc(void *handle, int option) |
| 7973 | { |
| 7974 | Ssh ssh = (Ssh) handle; |
| 7975 | if (option == LD_ECHO) |
| 7976 | return ssh->echoing; |
| 7977 | if (option == LD_EDIT) |
| 7978 | return ssh->editing; |
| 7979 | return FALSE; |
| 7980 | } |
| 7981 | |
| 7982 | static void ssh_provide_ldisc(void *handle, void *ldisc) |
| 7983 | { |
| 7984 | Ssh ssh = (Ssh) handle; |
| 7985 | ssh->ldisc = ldisc; |
| 7986 | } |
| 7987 | |
| 7988 | static void ssh_provide_logctx(void *handle, void *logctx) |
| 7989 | { |
| 7990 | Ssh ssh = (Ssh) handle; |
| 7991 | ssh->logctx = logctx; |
| 7992 | } |
| 7993 | |
| 7994 | static int ssh_return_exitcode(void *handle) |
| 7995 | { |
| 7996 | Ssh ssh = (Ssh) handle; |
| 7997 | if (ssh->s != NULL) |
| 7998 | return -1; |
| 7999 | else |
| 8000 | return (ssh->exitcode >= 0 ? ssh->exitcode : 0); |
| 8001 | } |
| 8002 | |
| 8003 | /* |
| 8004 | * cfg_info for SSH is the currently running version of the |
| 8005 | * protocol. (1 for 1; 2 for 2; 0 for not-decided-yet.) |
| 8006 | */ |
| 8007 | static int ssh_cfg_info(void *handle) |
| 8008 | { |
| 8009 | Ssh ssh = (Ssh) handle; |
| 8010 | return ssh->version; |
| 8011 | } |
| 8012 | |
| 8013 | /* |
| 8014 | * Gross hack: pscp will try to start SFTP but fall back to scp1 if |
| 8015 | * that fails. This variable is the means by which scp.c can reach |
| 8016 | * into the SSH code and find out which one it got. |
| 8017 | */ |
| 8018 | extern int ssh_fallback_cmd(void *handle) |
| 8019 | { |
| 8020 | Ssh ssh = (Ssh) handle; |
| 8021 | return ssh->fallback_cmd; |
| 8022 | } |
| 8023 | |
| 8024 | Backend ssh_backend = { |
| 8025 | ssh_init, |
| 8026 | ssh_free, |
| 8027 | ssh_reconfig, |
| 8028 | ssh_send, |
| 8029 | ssh_sendbuffer, |
| 8030 | ssh_size, |
| 8031 | ssh_special, |
| 8032 | ssh_get_specials, |
| 8033 | ssh_socket, |
| 8034 | ssh_return_exitcode, |
| 8035 | ssh_sendok, |
| 8036 | ssh_ldisc, |
| 8037 | ssh_provide_ldisc, |
| 8038 | ssh_provide_logctx, |
| 8039 | ssh_unthrottle, |
| 8040 | ssh_cfg_info, |
| 8041 | 22 |
| 8042 | }; |