39ff59721b593c8888744796065a61409dc17f19
[sgt/putty] / portfwd.c
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; 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 */
41 char hostname[256+8];
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;
51 };
52
53 static 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
59 static int pfd_closing(Plug plug, const char *error_msg, int error_code,
60 int calling_back)
61 {
62 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
63
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 }
77
78 return 1;
79 }
80
81 static int pfd_receive(Plug plug, int urgent, char *data, int len)
82 {
83 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
84 if (pr->dynamic) {
85 while (len--) {
86 /*
87 * Throughout SOCKS negotiation, "hostname" is re-used as a
88 * random protocol buffer with "port" storing the length.
89 */
90 if (pr->port >= lenof(pr->hostname)) {
91 /* Request too long. */
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) {
117 /* Not CONNECT. */
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 }
126 if (pr->port <= 8) continue; /* haven't started user/hostname */
127 if (pr->hostname[pr->port-1] != 0)
128 continue; /* haven't _finished_ user/hostname */
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;
144 pr->port = 8; /* reset buffer to overwrite name */
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);
150 len= pr->port - 8;
151 pr->port = GET_16BIT_MSB_FIRST(pr->hostname+2);
152 memmove(pr->hostname, pr->hostname + 8, len);
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) {
205 /*
206 * We're receiving a SOCKS request.
207 */
208 unsigned char reply[10]; /* SOCKS5 atyp=1 reply */
209 int atype, alen = 0;
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
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) {
232 /* Not CONNECT or reserved field nonzero - error */
233 reply[1] = 1; /* generic failure */
234 sk_write(pr->s, (char *) reply, lenof(reply));
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) {
244 /* REP=0 (success) already */
245 sk_write(pr->s, (char *) reply, lenof(reply));
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) {
253 /* REP=0 (success) already */
254 sk_write(pr->s, (char *) reply, lenof(reply));
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 */
262 reply[1] = 8; /* atype not supported */
263 sk_write(pr->s, (char *) reply, lenof(reply));
264 pfd_close(pr->s);
265 return 1;
266 }
267 }
268 }
269
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
287 /*
288 * Freeze the socket until the SSH server confirms the
289 * connection.
290 */
291 sk_set_frozen(pr->s, 1);
292
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 /*
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 }
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 }
319 return 1;
320 }
321
322 static void pfd_sent(Plug plug, int bufsize)
323 {
324 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
325
326 if (pr->c)
327 sshfwd_unthrottle(pr->c, bufsize);
328 }
329
330 /*
331 * Called when receiving a PORT OPEN from the server
332 */
333 const char *pfd_newconnect(Socket *s, char *hostname, int port,
334 void *c, Conf *conf, int addressfamily)
335 {
336 static const struct plug_function_table fn_table = {
337 pfd_log,
338 pfd_closing,
339 pfd_receive,
340 pfd_sent,
341 NULL
342 };
343
344 SockAddr addr;
345 const char *err;
346 char *dummy_realhost;
347 struct PFwdPrivate *pr;
348
349 /*
350 * Try to find host.
351 */
352 addr = name_lookup(hostname, port, &dummy_realhost, conf, addressfamily);
353 if ((err = sk_addr_error(addr)) != NULL) {
354 sk_addr_free(addr);
355 return err;
356 }
357
358 /*
359 * Open socket.
360 */
361 pr = snew(struct PFwdPrivate);
362 pr->buffer = NULL;
363 pr->fn = &fn_table;
364 pr->throttled = pr->throttle_override = 0;
365 pr->ready = 1;
366 pr->c = c;
367 pr->backhandle = NULL; /* we shouldn't need this */
368 pr->dynamic = 0;
369
370 pr->s = *s = new_connection(addr, dummy_realhost, port,
371 0, 1, 0, 0, (Plug) pr, conf);
372 if ((err = sk_socket_error(*s)) != NULL) {
373 sfree(pr);
374 return err;
375 }
376
377 sk_set_private_ptr(*s, pr);
378 return NULL;
379 }
380
381 /*
382 called when someone connects to the local port
383 */
384
385 static int pfd_accepting(Plug p, OSSocket sock)
386 {
387 static const struct plug_function_table fn_table = {
388 pfd_log,
389 pfd_closing,
390 pfd_receive,
391 pfd_sent,
392 NULL
393 };
394 struct PFwdPrivate *pr, *org;
395 Socket s;
396 const char *err;
397
398 org = (struct PFwdPrivate *)p;
399 pr = snew(struct PFwdPrivate);
400 pr->buffer = NULL;
401 pr->fn = &fn_table;
402
403 pr->c = NULL;
404 pr->backhandle = org->backhandle;
405
406 pr->s = s = sk_register(sock, (Plug) pr);
407 if ((err = sk_socket_error(s)) != NULL) {
408 sfree(pr);
409 return err != NULL;
410 }
411
412 sk_set_private_ptr(s, pr);
413
414 pr->throttled = pr->throttle_override = 0;
415 pr->ready = 0;
416
417 if (org->dynamic) {
418 pr->dynamic = 1;
419 pr->port = 0; /* "hostname" buffer is so far empty */
420 sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
421 } else {
422 pr->dynamic = 0;
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 }
434 }
435
436 return 0;
437 }
438
439
440 /* Add a new forwarding from port -> desthost:destport
441 sets up a listener on the local machine on (srcaddr:)port
442 */
443 const char *pfd_addforward(char *desthost, int destport, char *srcaddr,
444 int port, void *backhandle, Conf *conf,
445 void **sockdata, int address_family)
446 {
447 static const struct plug_function_table fn_table = {
448 pfd_log,
449 pfd_closing,
450 pfd_receive, /* should not happen... */
451 pfd_sent, /* also should not happen */
452 pfd_accepting
453 };
454
455 const char *err;
456 struct PFwdPrivate *pr;
457 Socket s;
458
459 /*
460 * Open socket.
461 */
462 pr = snew(struct PFwdPrivate);
463 pr->buffer = NULL;
464 pr->fn = &fn_table;
465 pr->c = NULL;
466 if (desthost) {
467 strcpy(pr->hostname, desthost);
468 pr->port = destport;
469 pr->dynamic = 0;
470 } else
471 pr->dynamic = 1;
472 pr->throttled = pr->throttle_override = 0;
473 pr->ready = 0;
474 pr->backhandle = backhandle;
475
476 pr->s = s = new_listener(srcaddr, port, (Plug) pr,
477 !conf_get_int(conf, CONF_lport_acceptall),
478 conf, address_family);
479 if ((err = sk_socket_error(s)) != NULL) {
480 sfree(pr);
481 return err;
482 }
483
484 sk_set_private_ptr(s, pr);
485
486 *sockdata = (void *)s;
487
488 return NULL;
489 }
490
491 void 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
500 sfree(pr->buffer);
501 sfree(pr);
502
503 sk_close(s);
504 }
505
506 /*
507 * Terminate a listener.
508 */
509 void pfd_terminate(void *sv)
510 {
511 pfd_close((Socket)sv);
512 }
513
514 void 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
525 void 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
536 /*
537 * Called to send data down the raw connection.
538 */
539 int pfd_send(Socket s, char *data, int len)
540 {
541 if (s == NULL)
542 return 0;
543 return sk_write(s, data, len);
544 }
545
546 void pfd_send_eof(Socket s)
547 {
548 sk_write_eof(s);
549 }
550
551 void pfd_confirm(Socket s)
552 {
553 struct PFwdPrivate *pr;
554
555 if (s == NULL)
556 return;
557
558 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
559 pr->ready = 1;
560 sk_set_frozen(s, 0);
561 sk_write(s, NULL, 0);
562 if (pr->buffer) {
563 sshfwd_write(pr->c, pr->buffer, pr->buflen);
564 sfree(pr->buffer);
565 pr->buffer = NULL;
566 }
567 }