| 1 | /* |
| 2 | * SSH port forwarding. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | #include "putty.h" |
| 9 | #include "ssh.h" |
| 10 | |
| 11 | #ifndef FALSE |
| 12 | #define FALSE 0 |
| 13 | #endif |
| 14 | #ifndef TRUE |
| 15 | #define TRUE 1 |
| 16 | #endif |
| 17 | |
| 18 | struct PFwdPrivate { |
| 19 | const struct plug_function_table *fn; |
| 20 | /* the above variable absolutely *must* be the first in this structure */ |
| 21 | void *c; /* (channel) data used by ssh.c */ |
| 22 | void *backhandle; /* instance of SSH backend itself */ |
| 23 | /* Note that backhandle need not be filled in if c is non-NULL */ |
| 24 | Socket s; |
| 25 | int throttled, throttle_override; |
| 26 | int ready; |
| 27 | /* |
| 28 | * `dynamic' does double duty. It's set to 0 for an ordinary |
| 29 | * forwarded port, and nonzero for SOCKS-style dynamic port |
| 30 | * forwarding; but it also represents the state of the SOCKS |
| 31 | * exchange. |
| 32 | */ |
| 33 | int dynamic; |
| 34 | /* |
| 35 | * `hostname' and `port' are the real hostname and port, once |
| 36 | * we know what we're connecting to. |
| 37 | */ |
| 38 | char *hostname; |
| 39 | int port; |
| 40 | /* |
| 41 | * `socksbuf' is the buffer we use to accumulate a SOCKS request. |
| 42 | */ |
| 43 | char *socksbuf; |
| 44 | int sockslen, sockssize; |
| 45 | /* |
| 46 | * When doing dynamic port forwarding, we can receive |
| 47 | * connection data before we are actually able to send it; so |
| 48 | * we may have to temporarily hold some in a dynamically |
| 49 | * allocated buffer here. |
| 50 | */ |
| 51 | void *buffer; |
| 52 | int buflen; |
| 53 | }; |
| 54 | |
| 55 | static struct PFwdPrivate *new_portfwd_private(void) |
| 56 | { |
| 57 | struct PFwdPrivate *pr = snew(struct PFwdPrivate); |
| 58 | pr->hostname = NULL; |
| 59 | pr->socksbuf = NULL; |
| 60 | pr->sockslen = pr->sockssize = 0; |
| 61 | pr->buffer = NULL; |
| 62 | return pr; |
| 63 | } |
| 64 | |
| 65 | static void free_portfwd_private(struct PFwdPrivate *pr) |
| 66 | { |
| 67 | if (!pr) |
| 68 | return; |
| 69 | sfree(pr->hostname); |
| 70 | sfree(pr->socksbuf); |
| 71 | sfree(pr->buffer); |
| 72 | sfree(pr); |
| 73 | } |
| 74 | |
| 75 | static void pfd_log(Plug plug, int type, SockAddr addr, int port, |
| 76 | const char *error_msg, int error_code) |
| 77 | { |
| 78 | /* we have to dump these since we have no interface to logging.c */ |
| 79 | } |
| 80 | |
| 81 | static int pfd_closing(Plug plug, const char *error_msg, int error_code, |
| 82 | int calling_back) |
| 83 | { |
| 84 | struct PFwdPrivate *pr = (struct PFwdPrivate *) plug; |
| 85 | |
| 86 | if (error_msg) { |
| 87 | /* |
| 88 | * Socket error. Slam the connection instantly shut. |
| 89 | */ |
| 90 | if (pr->c) { |
| 91 | sshfwd_unclean_close(pr->c); |
| 92 | } else { |
| 93 | /* |
| 94 | * We might not have an SSH channel, if a socket error |
| 95 | * occurred during SOCKS negotiation. If not, we must |
| 96 | * clean ourself up without sshfwd_unclean_close's call |
| 97 | * back to pfd_close. |
| 98 | */ |
| 99 | pfd_close(pr->s); |
| 100 | } |
| 101 | } else { |
| 102 | /* |
| 103 | * Ordinary EOF received on socket. Send an EOF on the SSH |
| 104 | * channel. |
| 105 | */ |
| 106 | if (pr->c) |
| 107 | sshfwd_write_eof(pr->c); |
| 108 | } |
| 109 | |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | static int pfd_receive(Plug plug, int urgent, char *data, int len) |
| 114 | { |
| 115 | struct PFwdPrivate *pr = (struct PFwdPrivate *) plug; |
| 116 | if (pr->dynamic) { |
| 117 | while (len--) { |
| 118 | if (pr->sockslen >= pr->sockssize) { |
| 119 | pr->sockssize = pr->sockslen * 5 / 4 + 256; |
| 120 | pr->socksbuf = sresize(pr->socksbuf, pr->sockssize, char); |
| 121 | } |
| 122 | pr->socksbuf[pr->sockslen++] = *data++; |
| 123 | |
| 124 | /* |
| 125 | * Now check what's in the buffer to see if it's a |
| 126 | * valid and complete message in the SOCKS exchange. |
| 127 | */ |
| 128 | if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 4) && |
| 129 | pr->socksbuf[0] == 4) { |
| 130 | /* |
| 131 | * SOCKS 4. |
| 132 | */ |
| 133 | if (pr->dynamic == 1) |
| 134 | pr->dynamic = 0x4000; |
| 135 | if (pr->sockslen < 2) |
| 136 | continue; /* don't have command code yet */ |
| 137 | if (pr->socksbuf[1] != 1) { |
| 138 | /* Not CONNECT. */ |
| 139 | /* Send back a SOCKS 4 error before closing. */ |
| 140 | char data[8]; |
| 141 | memset(data, 0, sizeof(data)); |
| 142 | data[1] = 91; /* generic `request rejected' */ |
| 143 | sk_write(pr->s, data, 8); |
| 144 | pfd_close(pr->s); |
| 145 | return 1; |
| 146 | } |
| 147 | if (pr->sockslen <= 8) |
| 148 | continue; /* haven't started user/hostname */ |
| 149 | if (pr->socksbuf[pr->sockslen-1] != 0) |
| 150 | continue; /* haven't _finished_ user/hostname */ |
| 151 | /* |
| 152 | * Now we have a full SOCKS 4 request. Check it to |
| 153 | * see if it's a SOCKS 4A request. |
| 154 | */ |
| 155 | if (pr->socksbuf[4] == 0 && pr->socksbuf[5] == 0 && |
| 156 | pr->socksbuf[6] == 0 && pr->socksbuf[7] != 0) { |
| 157 | /* |
| 158 | * It's SOCKS 4A. So if we haven't yet |
| 159 | * collected the host name, we should continue |
| 160 | * waiting for data in order to do so; if we |
| 161 | * have, we can go ahead. |
| 162 | */ |
| 163 | int len; |
| 164 | if (pr->dynamic == 0x4000) { |
| 165 | pr->dynamic = 0x4001; |
| 166 | pr->sockslen = 8; /* reset buffer to overwrite name */ |
| 167 | continue; |
| 168 | } |
| 169 | pr->socksbuf[0] = 0; /* reply version code */ |
| 170 | pr->socksbuf[1] = 90; /* request granted */ |
| 171 | sk_write(pr->s, pr->socksbuf, 8); |
| 172 | len = pr->sockslen - 8; |
| 173 | pr->port = GET_16BIT_MSB_FIRST(pr->socksbuf+2); |
| 174 | pr->hostname = snewn(len+1, char); |
| 175 | pr->hostname[len] = '\0'; |
| 176 | memcpy(pr->hostname, pr->socksbuf + 8, len); |
| 177 | goto connect; |
| 178 | } else { |
| 179 | /* |
| 180 | * It's SOCKS 4, which means we should format |
| 181 | * the IP address into the hostname string and |
| 182 | * then just go. |
| 183 | */ |
| 184 | pr->socksbuf[0] = 0; /* reply version code */ |
| 185 | pr->socksbuf[1] = 90; /* request granted */ |
| 186 | sk_write(pr->s, pr->socksbuf, 8); |
| 187 | pr->port = GET_16BIT_MSB_FIRST(pr->socksbuf+2); |
| 188 | pr->hostname = dupprintf("%d.%d.%d.%d", |
| 189 | (unsigned char)pr->socksbuf[4], |
| 190 | (unsigned char)pr->socksbuf[5], |
| 191 | (unsigned char)pr->socksbuf[6], |
| 192 | (unsigned char)pr->socksbuf[7]); |
| 193 | goto connect; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 5) && |
| 198 | pr->socksbuf[0] == 5) { |
| 199 | /* |
| 200 | * SOCKS 5. |
| 201 | */ |
| 202 | if (pr->dynamic == 1) |
| 203 | pr->dynamic = 0x5000; |
| 204 | |
| 205 | if (pr->dynamic == 0x5000) { |
| 206 | int i, method; |
| 207 | char data[2]; |
| 208 | /* |
| 209 | * We're receiving a set of method identifiers. |
| 210 | */ |
| 211 | if (pr->sockslen < 2) |
| 212 | continue; /* no method count yet */ |
| 213 | if (pr->sockslen < 2 + (unsigned char)pr->socksbuf[1]) |
| 214 | continue; /* no methods yet */ |
| 215 | method = 0xFF; /* invalid */ |
| 216 | for (i = 0; i < (unsigned char)pr->socksbuf[1]; i++) |
| 217 | if (pr->socksbuf[2+i] == 0) { |
| 218 | method = 0;/* no auth */ |
| 219 | break; |
| 220 | } |
| 221 | data[0] = 5; |
| 222 | data[1] = method; |
| 223 | sk_write(pr->s, data, 2); |
| 224 | pr->dynamic = 0x5001; |
| 225 | pr->sockslen = 0; /* re-empty the buffer */ |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | if (pr->dynamic == 0x5001) { |
| 230 | /* |
| 231 | * We're receiving a SOCKS request. |
| 232 | */ |
| 233 | unsigned char reply[10]; /* SOCKS5 atyp=1 reply */ |
| 234 | int atype, alen = 0; |
| 235 | |
| 236 | /* |
| 237 | * Pre-fill reply packet. |
| 238 | * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0 |
| 239 | * (atyp=1) in the reply; if we succeed, we don't know |
| 240 | * the right answers, and if we fail, they should be |
| 241 | * ignored. |
| 242 | */ |
| 243 | memset(reply, 0, lenof(reply)); |
| 244 | reply[0] = 5; /* VER */ |
| 245 | reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */ |
| 246 | |
| 247 | if (pr->sockslen < 6) continue; |
| 248 | atype = (unsigned char)pr->socksbuf[3]; |
| 249 | if (atype == 1) /* IPv4 address */ |
| 250 | alen = 4; |
| 251 | if (atype == 4) /* IPv6 address */ |
| 252 | alen = 16; |
| 253 | if (atype == 3) /* domain name has leading length */ |
| 254 | alen = 1 + (unsigned char)pr->socksbuf[4]; |
| 255 | if (pr->sockslen < 6 + alen) continue; |
| 256 | if (pr->socksbuf[1] != 1 || pr->socksbuf[2] != 0) { |
| 257 | /* Not CONNECT or reserved field nonzero - error */ |
| 258 | reply[1] = 1; /* generic failure */ |
| 259 | sk_write(pr->s, (char *) reply, lenof(reply)); |
| 260 | pfd_close(pr->s); |
| 261 | return 1; |
| 262 | } |
| 263 | /* |
| 264 | * Now we have a viable connect request. Switch |
| 265 | * on atype. |
| 266 | */ |
| 267 | pr->port = GET_16BIT_MSB_FIRST(pr->socksbuf+4+alen); |
| 268 | if (atype == 1) { |
| 269 | /* REP=0 (success) already */ |
| 270 | sk_write(pr->s, (char *) reply, lenof(reply)); |
| 271 | pr->hostname = dupprintf("%d.%d.%d.%d", |
| 272 | (unsigned char)pr->socksbuf[4], |
| 273 | (unsigned char)pr->socksbuf[5], |
| 274 | (unsigned char)pr->socksbuf[6], |
| 275 | (unsigned char)pr->socksbuf[7]); |
| 276 | goto connect; |
| 277 | } else if (atype == 3) { |
| 278 | /* REP=0 (success) already */ |
| 279 | sk_write(pr->s, (char *) reply, lenof(reply)); |
| 280 | pr->hostname = snewn(alen, char); |
| 281 | pr->hostname[alen-1] = '\0'; |
| 282 | memcpy(pr->hostname, pr->socksbuf + 5, alen-1); |
| 283 | goto connect; |
| 284 | } else { |
| 285 | /* |
| 286 | * Unknown address type. (FIXME: support IPv6!) |
| 287 | */ |
| 288 | reply[1] = 8; /* atype not supported */ |
| 289 | sk_write(pr->s, (char *) reply, lenof(reply)); |
| 290 | pfd_close(pr->s); |
| 291 | return 1; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * If we get here without either having done `continue' |
| 298 | * or `goto connect', it must be because there is no |
| 299 | * sensible interpretation of what's in our buffer. So |
| 300 | * close the connection rudely. |
| 301 | */ |
| 302 | pfd_close(pr->s); |
| 303 | return 1; |
| 304 | } |
| 305 | return 1; |
| 306 | |
| 307 | /* |
| 308 | * We come here when we're ready to make an actual |
| 309 | * connection. |
| 310 | */ |
| 311 | connect: |
| 312 | sfree(pr->socksbuf); |
| 313 | pr->socksbuf = NULL; |
| 314 | |
| 315 | /* |
| 316 | * Freeze the socket until the SSH server confirms the |
| 317 | * connection. |
| 318 | */ |
| 319 | sk_set_frozen(pr->s, 1); |
| 320 | |
| 321 | pr->c = new_sock_channel(pr->backhandle, pr->s); |
| 322 | if (pr->c == NULL) { |
| 323 | pfd_close(pr->s); |
| 324 | return 1; |
| 325 | } else { |
| 326 | /* asks to forward to the specified host/port for this */ |
| 327 | ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding"); |
| 328 | } |
| 329 | pr->dynamic = 0; |
| 330 | |
| 331 | /* |
| 332 | * If there's any data remaining in our current buffer, |
| 333 | * save it to be sent on pfd_confirm(). |
| 334 | */ |
| 335 | if (len > 0) { |
| 336 | pr->buffer = snewn(len, char); |
| 337 | memcpy(pr->buffer, data, len); |
| 338 | pr->buflen = len; |
| 339 | } |
| 340 | } |
| 341 | if (pr->ready) { |
| 342 | if (sshfwd_write(pr->c, data, len) > 0) { |
| 343 | pr->throttled = 1; |
| 344 | sk_set_frozen(pr->s, 1); |
| 345 | } |
| 346 | } |
| 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | static void pfd_sent(Plug plug, int bufsize) |
| 351 | { |
| 352 | struct PFwdPrivate *pr = (struct PFwdPrivate *) plug; |
| 353 | |
| 354 | if (pr->c) |
| 355 | sshfwd_unthrottle(pr->c, bufsize); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Called when receiving a PORT OPEN from the server |
| 360 | */ |
| 361 | const char *pfd_newconnect(Socket *s, char *hostname, int port, |
| 362 | void *c, Conf *conf, int addressfamily) |
| 363 | { |
| 364 | static const struct plug_function_table fn_table = { |
| 365 | pfd_log, |
| 366 | pfd_closing, |
| 367 | pfd_receive, |
| 368 | pfd_sent, |
| 369 | NULL |
| 370 | }; |
| 371 | |
| 372 | SockAddr addr; |
| 373 | const char *err; |
| 374 | char *dummy_realhost; |
| 375 | struct PFwdPrivate *pr; |
| 376 | |
| 377 | /* |
| 378 | * Try to find host. |
| 379 | */ |
| 380 | addr = name_lookup(hostname, port, &dummy_realhost, conf, addressfamily); |
| 381 | if ((err = sk_addr_error(addr)) != NULL) { |
| 382 | sk_addr_free(addr); |
| 383 | sfree(dummy_realhost); |
| 384 | return err; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Open socket. |
| 389 | */ |
| 390 | pr = new_portfwd_private(); |
| 391 | pr->fn = &fn_table; |
| 392 | pr->throttled = pr->throttle_override = 0; |
| 393 | pr->ready = 1; |
| 394 | pr->c = c; |
| 395 | pr->backhandle = NULL; /* we shouldn't need this */ |
| 396 | pr->dynamic = 0; |
| 397 | |
| 398 | pr->s = *s = new_connection(addr, dummy_realhost, port, |
| 399 | 0, 1, 0, 0, (Plug) pr, conf); |
| 400 | sfree(dummy_realhost); |
| 401 | if ((err = sk_socket_error(*s)) != NULL) { |
| 402 | free_portfwd_private(pr); |
| 403 | return err; |
| 404 | } |
| 405 | |
| 406 | sk_set_private_ptr(*s, pr); |
| 407 | return NULL; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | called when someone connects to the local port |
| 412 | */ |
| 413 | |
| 414 | static int pfd_accepting(Plug p, OSSocket sock) |
| 415 | { |
| 416 | static const struct plug_function_table fn_table = { |
| 417 | pfd_log, |
| 418 | pfd_closing, |
| 419 | pfd_receive, |
| 420 | pfd_sent, |
| 421 | NULL |
| 422 | }; |
| 423 | struct PFwdPrivate *pr, *org; |
| 424 | Socket s; |
| 425 | const char *err; |
| 426 | |
| 427 | org = (struct PFwdPrivate *)p; |
| 428 | pr = new_portfwd_private(); |
| 429 | pr->fn = &fn_table; |
| 430 | |
| 431 | pr->c = NULL; |
| 432 | pr->backhandle = org->backhandle; |
| 433 | |
| 434 | pr->s = s = sk_register(sock, (Plug) pr); |
| 435 | if ((err = sk_socket_error(s)) != NULL) { |
| 436 | free_portfwd_private(pr); |
| 437 | return err != NULL; |
| 438 | } |
| 439 | |
| 440 | sk_set_private_ptr(s, pr); |
| 441 | |
| 442 | pr->throttled = pr->throttle_override = 0; |
| 443 | pr->ready = 0; |
| 444 | |
| 445 | if (org->dynamic) { |
| 446 | pr->dynamic = 1; |
| 447 | pr->port = 0; /* "hostname" buffer is so far empty */ |
| 448 | sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */ |
| 449 | } else { |
| 450 | pr->dynamic = 0; |
| 451 | pr->hostname = dupstr(org->hostname); |
| 452 | pr->port = org->port; |
| 453 | pr->c = new_sock_channel(org->backhandle, s); |
| 454 | |
| 455 | if (pr->c == NULL) { |
| 456 | free_portfwd_private(pr); |
| 457 | return 1; |
| 458 | } else { |
| 459 | /* asks to forward to the specified host/port for this */ |
| 460 | ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding"); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /* Add a new forwarding from port -> desthost:destport |
| 469 | sets up a listener on the local machine on (srcaddr:)port |
| 470 | */ |
| 471 | const char *pfd_addforward(char *desthost, int destport, char *srcaddr, |
| 472 | int port, void *backhandle, Conf *conf, |
| 473 | void **sockdata, int address_family) |
| 474 | { |
| 475 | static const struct plug_function_table fn_table = { |
| 476 | pfd_log, |
| 477 | pfd_closing, |
| 478 | pfd_receive, /* should not happen... */ |
| 479 | pfd_sent, /* also should not happen */ |
| 480 | pfd_accepting |
| 481 | }; |
| 482 | |
| 483 | const char *err; |
| 484 | struct PFwdPrivate *pr; |
| 485 | Socket s; |
| 486 | |
| 487 | /* |
| 488 | * Open socket. |
| 489 | */ |
| 490 | pr = new_portfwd_private(); |
| 491 | pr->fn = &fn_table; |
| 492 | pr->c = NULL; |
| 493 | if (desthost) { |
| 494 | pr->hostname = dupstr(desthost); |
| 495 | pr->port = destport; |
| 496 | pr->dynamic = 0; |
| 497 | } else |
| 498 | pr->dynamic = 1; |
| 499 | pr->throttled = pr->throttle_override = 0; |
| 500 | pr->ready = 0; |
| 501 | pr->backhandle = backhandle; |
| 502 | |
| 503 | pr->s = s = new_listener(srcaddr, port, (Plug) pr, |
| 504 | !conf_get_int(conf, CONF_lport_acceptall), |
| 505 | conf, address_family); |
| 506 | if ((err = sk_socket_error(s)) != NULL) { |
| 507 | free_portfwd_private(pr); |
| 508 | return err; |
| 509 | } |
| 510 | |
| 511 | sk_set_private_ptr(s, pr); |
| 512 | |
| 513 | *sockdata = (void *)s; |
| 514 | |
| 515 | return NULL; |
| 516 | } |
| 517 | |
| 518 | void pfd_close(Socket s) |
| 519 | { |
| 520 | struct PFwdPrivate *pr; |
| 521 | |
| 522 | if (!s) |
| 523 | return; |
| 524 | |
| 525 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
| 526 | |
| 527 | free_portfwd_private(pr); |
| 528 | |
| 529 | sk_close(s); |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Terminate a listener. |
| 534 | */ |
| 535 | void pfd_terminate(void *sv) |
| 536 | { |
| 537 | pfd_close((Socket)sv); |
| 538 | } |
| 539 | |
| 540 | void pfd_unthrottle(Socket s) |
| 541 | { |
| 542 | struct PFwdPrivate *pr; |
| 543 | if (!s) |
| 544 | return; |
| 545 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
| 546 | |
| 547 | pr->throttled = 0; |
| 548 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
| 549 | } |
| 550 | |
| 551 | void pfd_override_throttle(Socket s, int enable) |
| 552 | { |
| 553 | struct PFwdPrivate *pr; |
| 554 | if (!s) |
| 555 | return; |
| 556 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
| 557 | |
| 558 | pr->throttle_override = enable; |
| 559 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Called to send data down the raw connection. |
| 564 | */ |
| 565 | int pfd_send(Socket s, char *data, int len) |
| 566 | { |
| 567 | if (s == NULL) |
| 568 | return 0; |
| 569 | return sk_write(s, data, len); |
| 570 | } |
| 571 | |
| 572 | void pfd_send_eof(Socket s) |
| 573 | { |
| 574 | sk_write_eof(s); |
| 575 | } |
| 576 | |
| 577 | void pfd_confirm(Socket s) |
| 578 | { |
| 579 | struct PFwdPrivate *pr; |
| 580 | |
| 581 | if (s == NULL) |
| 582 | return; |
| 583 | |
| 584 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
| 585 | pr->ready = 1; |
| 586 | sk_set_frozen(s, 0); |
| 587 | sk_write(s, NULL, 0); |
| 588 | if (pr->buffer) { |
| 589 | sshfwd_write(pr->c, pr->buffer, pr->buflen); |
| 590 | sfree(pr->buffer); |
| 591 | pr->buffer = NULL; |
| 592 | } |
| 593 | } |