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