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