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