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