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 */
25 int throttled
, throttle_override
;
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
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.
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.
53 static void pfd_log(Plug plug
, int type
, SockAddr addr
, int port
,
54 const char *error_msg
, int error_code
)
56 /* we have to dump these since we have no interface to logging.c */
59 static int pfd_closing(Plug plug
, const char *error_msg
, int error_code
,
62 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
66 * Socket error. Slam the connection instantly shut.
68 sshfwd_unclean_close(pr
->c
);
71 * Ordinary EOF received on socket. Send an EOF on the SSH
75 sshfwd_write_eof(pr
->c
);
81 static int pfd_receive(Plug plug
, int urgent
, char *data
, int len
)
83 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
87 * Throughout SOCKS negotiation, "hostname" is re-used as a
88 * random protocol buffer with "port" storing the length.
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. */
95 memset(data
, 0, sizeof(data
));
96 data
[1] = 91; /* generic `request rejected' */
97 sk_write(pr
->s
, data
, 8);
102 pr
->hostname
[pr
->port
++] = *data
++;
105 * Now check what's in the buffer to see if it's a
106 * valid and complete message in the SOCKS exchange.
108 if ((pr
->dynamic
== 1 || (pr
->dynamic
>> 12) == 4) &&
109 pr
->hostname
[0] == 4) {
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) {
118 /* Send back a SOCKS 4 error before closing. */
120 memset(data
, 0, sizeof(data
));
121 data
[1] = 91; /* generic `request rejected' */
122 sk_write(pr
->s
, data
, 8);
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 */
130 * Now we have a full SOCKS 4 request. Check it to
131 * see if it's a SOCKS 4A request.
133 if (pr
->hostname
[4] == 0 && pr
->hostname
[5] == 0 &&
134 pr
->hostname
[6] == 0 && pr
->hostname
[7] != 0) {
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.
142 if (pr
->dynamic
== 0x4000) {
143 pr
->dynamic
= 0x4001;
144 pr
->port
= 8; /* reset buffer to overwrite name */
147 pr
->hostname
[0] = 0; /* reply version code */
148 pr
->hostname
[1] = 90; /* request granted */
149 sk_write(pr
->s
, pr
->hostname
, 8);
151 pr
->port
= GET_16BIT_MSB_FIRST(pr
->hostname
+2);
152 memmove(pr
->hostname
, pr
->hostname
+ 8, len
);
156 * It's SOCKS 4, which means we should format
157 * the IP address into the hostname string and
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]);
173 if ((pr
->dynamic
== 1 || (pr
->dynamic
>> 12) == 5) &&
174 pr
->hostname
[0] == 5) {
178 if (pr
->dynamic
== 1)
179 pr
->dynamic
= 0x5000;
181 if (pr
->dynamic
== 0x5000) {
185 * We're receiving a set of method identifiers.
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 */
198 sk_write(pr
->s
, data
, 2);
199 pr
->dynamic
= 0x5001;
200 pr
->port
= 0; /* re-empty the buffer */
204 if (pr
->dynamic
== 0x5001) {
206 * We're receiving a SOCKS request.
208 unsigned char reply
[10]; /* SOCKS5 atyp=1 reply */
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
218 memset(reply
, 0, lenof(reply
));
219 reply
[0] = 5; /* VER */
220 reply
[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
222 if (pr
->port
< 6) continue;
223 atype
= (unsigned char)pr
->hostname
[3];
224 if (atype
== 1) /* IPv4 address */
226 if (atype
== 4) /* IPv6 address */
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
));
239 * Now we have a viable connect request. Switch
242 pr
->port
= GET_16BIT_MSB_FIRST(pr
->hostname
+4+alen
);
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]);
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';
260 * Unknown address type. (FIXME: support IPv6!)
262 reply
[1] = 8; /* atype not supported */
263 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
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.
282 * We come here when we're ready to make an actual
288 * Freeze the socket until the SSH server confirms the
291 sk_set_frozen(pr
->s
, 1);
293 pr
->c
= new_sock_channel(pr
->backhandle
, pr
->s
);
298 /* asks to forward to the specified host/port for this */
299 ssh_send_port_open(pr
->c
, pr
->hostname
, pr
->port
, "forwarding");
304 * If there's any data remaining in our current buffer,
305 * save it to be sent on pfd_confirm().
308 pr
->buffer
= snewn(len
, char);
309 memcpy(pr
->buffer
, data
, len
);
314 if (sshfwd_write(pr
->c
, data
, len
) > 0) {
316 sk_set_frozen(pr
->s
, 1);
322 static void pfd_sent(Plug plug
, int bufsize
)
324 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
327 sshfwd_unthrottle(pr
->c
, bufsize
);
331 * Called when receiving a PORT OPEN from the server
333 const char *pfd_newconnect(Socket
*s
, char *hostname
, int port
,
334 void *c
, Conf
*conf
, int addressfamily
)
336 static const struct plug_function_table fn_table
= {
346 char *dummy_realhost
;
347 struct PFwdPrivate
*pr
;
352 addr
= name_lookup(hostname
, port
, &dummy_realhost
, conf
, addressfamily
);
353 if ((err
= sk_addr_error(addr
)) != NULL
) {
361 pr
= snew(struct PFwdPrivate
);
364 pr
->throttled
= pr
->throttle_override
= 0;
367 pr
->backhandle
= NULL
; /* we shouldn't need this */
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
) {
377 sk_set_private_ptr(*s
, pr
);
382 called when someone connects to the local port
385 static int pfd_accepting(Plug p
, OSSocket sock
)
387 static const struct plug_function_table fn_table
= {
394 struct PFwdPrivate
*pr
, *org
;
398 org
= (struct PFwdPrivate
*)p
;
399 pr
= snew(struct PFwdPrivate
);
404 pr
->backhandle
= org
->backhandle
;
406 pr
->s
= s
= sk_register(sock
, (Plug
) pr
);
407 if ((err
= sk_socket_error(s
)) != NULL
) {
412 sk_set_private_ptr(s
, pr
);
414 pr
->throttled
= pr
->throttle_override
= 0;
419 pr
->port
= 0; /* "hostname" buffer is so far empty */
420 sk_set_frozen(s
, 0); /* we want to receive SOCKS _now_! */
423 strcpy(pr
->hostname
, org
->hostname
);
424 pr
->port
= org
->port
;
425 pr
->c
= new_sock_channel(org
->backhandle
, s
);
431 /* asks to forward to the specified host/port for this */
432 ssh_send_port_open(pr
->c
, pr
->hostname
, pr
->port
, "forwarding");
440 /* Add a new forwarding from port -> desthost:destport
441 sets up a listener on the local machine on (srcaddr:)port
443 const char *pfd_addforward(char *desthost
, int destport
, char *srcaddr
,
444 int port
, void *backhandle
, Conf
*conf
,
445 void **sockdata
, int address_family
)
447 static const struct plug_function_table fn_table
= {
450 pfd_receive
, /* should not happen... */
451 pfd_sent
, /* also should not happen */
456 struct PFwdPrivate
*pr
;
462 pr
= snew(struct PFwdPrivate
);
467 strcpy(pr
->hostname
, desthost
);
472 pr
->throttled
= pr
->throttle_override
= 0;
474 pr
->backhandle
= backhandle
;
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
) {
484 sk_set_private_ptr(s
, pr
);
486 *sockdata
= (void *)s
;
491 void pfd_close(Socket s
)
493 struct PFwdPrivate
*pr
;
498 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
507 * Terminate a listener.
509 void pfd_terminate(void *sv
)
511 pfd_close((Socket
)sv
);
514 void pfd_unthrottle(Socket s
)
516 struct PFwdPrivate
*pr
;
519 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
522 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
525 void pfd_override_throttle(Socket s
, int enable
)
527 struct PFwdPrivate
*pr
;
530 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
532 pr
->throttle_override
= enable
;
533 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
537 * Called to send data down the raw connection.
539 int pfd_send(Socket s
, char *data
, int len
)
543 return sk_write(s
, data
, len
);
546 void pfd_send_eof(Socket s
)
551 void pfd_confirm(Socket s
)
553 struct PFwdPrivate
*pr
;
558 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
561 sk_write(s
, NULL
, 0);
563 sshfwd_write(pr
->c
, pr
->buffer
, pr
->buflen
);