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