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