2 * Network proxy abstraction in PuTTY
4 * A proxy layer, if necessary, wedges itself between the network
5 * code and the higher level backend.
12 #define DEFINE_PLUG_METHOD_MACROS
17 #define do_proxy_dns(conf) \
18 (conf_get_int(conf, CONF_proxy_dns) == FORCE_ON || \
19 (conf_get_int(conf, CONF_proxy_dns) == AUTO && \
20 conf_get_int(conf, CONF_proxy_type) != PROXY_SOCKS4))
23 * Call this when proxy negotiation is complete, so that this
24 * socket can begin working normally.
26 void proxy_activate (Proxy_Socket p
)
30 long output_before
, output_after
;
32 p
->state
= PROXY_STATE_ACTIVE
;
34 /* we want to ignore new receive events until we have sent
35 * all of our buffered receive data.
37 sk_set_frozen(p
->sub_socket
, 1);
39 /* how many bytes of output have we buffered? */
40 output_before
= bufchain_size(&p
->pending_oob_output_data
) +
41 bufchain_size(&p
->pending_output_data
);
42 /* and keep track of how many bytes do not get sent. */
45 /* send buffered OOB writes */
46 while (bufchain_size(&p
->pending_oob_output_data
) > 0) {
47 bufchain_prefix(&p
->pending_oob_output_data
, &data
, &len
);
48 output_after
+= sk_write_oob(p
->sub_socket
, data
, len
);
49 bufchain_consume(&p
->pending_oob_output_data
, len
);
52 /* send buffered normal writes */
53 while (bufchain_size(&p
->pending_output_data
) > 0) {
54 bufchain_prefix(&p
->pending_output_data
, &data
, &len
);
55 output_after
+= sk_write(p
->sub_socket
, data
, len
);
56 bufchain_consume(&p
->pending_output_data
, len
);
59 /* if we managed to send any data, let the higher levels know. */
60 if (output_after
< output_before
)
61 plug_sent(p
->plug
, output_after
);
63 /* if we were asked to flush the output during
64 * the proxy negotiation process, do so now.
66 if (p
->pending_flush
) sk_flush(p
->sub_socket
);
68 /* if we have a pending EOF to send, send it */
69 if (p
->pending_eof
) sk_write_eof(p
->sub_socket
);
71 /* if the backend wanted the socket unfrozen, try to unfreeze.
72 * our set_frozen handler will flush buffered receive data before
73 * unfreezing the actual underlying socket.
76 sk_set_frozen((Socket
)p
, 0);
79 /* basic proxy socket functions */
81 static Plug
sk_proxy_plug (Socket s
, Plug p
)
83 Proxy_Socket ps
= (Proxy_Socket
) s
;
90 static void sk_proxy_close (Socket s
)
92 Proxy_Socket ps
= (Proxy_Socket
) s
;
94 sk_close(ps
->sub_socket
);
95 sk_addr_free(ps
->remote_addr
);
99 static int sk_proxy_write (Socket s
, const char *data
, int len
)
101 Proxy_Socket ps
= (Proxy_Socket
) s
;
103 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
104 bufchain_add(&ps
->pending_output_data
, data
, len
);
105 return bufchain_size(&ps
->pending_output_data
);
107 return sk_write(ps
->sub_socket
, data
, len
);
110 static int sk_proxy_write_oob (Socket s
, const char *data
, int len
)
112 Proxy_Socket ps
= (Proxy_Socket
) s
;
114 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
115 bufchain_clear(&ps
->pending_output_data
);
116 bufchain_clear(&ps
->pending_oob_output_data
);
117 bufchain_add(&ps
->pending_oob_output_data
, data
, len
);
120 return sk_write_oob(ps
->sub_socket
, data
, len
);
123 static void sk_proxy_write_eof (Socket s
)
125 Proxy_Socket ps
= (Proxy_Socket
) s
;
127 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
131 sk_write_eof(ps
->sub_socket
);
134 static void sk_proxy_flush (Socket s
)
136 Proxy_Socket ps
= (Proxy_Socket
) s
;
138 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
139 ps
->pending_flush
= 1;
142 sk_flush(ps
->sub_socket
);
145 static void sk_proxy_set_private_ptr (Socket s
, void *ptr
)
147 Proxy_Socket ps
= (Proxy_Socket
) s
;
148 sk_set_private_ptr(ps
->sub_socket
, ptr
);
151 static void * sk_proxy_get_private_ptr (Socket s
)
153 Proxy_Socket ps
= (Proxy_Socket
) s
;
154 return sk_get_private_ptr(ps
->sub_socket
);
157 static void sk_proxy_set_frozen (Socket s
, int is_frozen
)
159 Proxy_Socket ps
= (Proxy_Socket
) s
;
161 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
162 ps
->freeze
= is_frozen
;
166 /* handle any remaining buffered recv data first */
167 if (bufchain_size(&ps
->pending_input_data
) > 0) {
168 ps
->freeze
= is_frozen
;
170 /* loop while we still have buffered data, and while we are
171 * unfrozen. the plug_receive call in the loop could result
172 * in a call back into this function refreezing the socket,
173 * so we have to check each time.
175 while (!ps
->freeze
&& bufchain_size(&ps
->pending_input_data
) > 0) {
179 bufchain_prefix(&ps
->pending_input_data
, &data
, &len
);
180 if (len
> lenof(databuf
))
181 len
= lenof(databuf
);
182 memcpy(databuf
, data
, len
);
183 bufchain_consume(&ps
->pending_input_data
, len
);
184 plug_receive(ps
->plug
, 0, databuf
, len
);
187 /* if we're still frozen, we'll have to wait for another
188 * call from the backend to finish unbuffering the data.
190 if (ps
->freeze
) return;
193 sk_set_frozen(ps
->sub_socket
, is_frozen
);
196 static const char * sk_proxy_socket_error (Socket s
)
198 Proxy_Socket ps
= (Proxy_Socket
) s
;
199 if (ps
->error
!= NULL
|| ps
->sub_socket
== NULL
) {
202 return sk_socket_error(ps
->sub_socket
);
205 /* basic proxy plug functions */
207 static void plug_proxy_log(Plug plug
, int type
, SockAddr addr
, int port
,
208 const char *error_msg
, int error_code
)
210 Proxy_Plug pp
= (Proxy_Plug
) plug
;
211 Proxy_Socket ps
= pp
->proxy_socket
;
213 plug_log(ps
->plug
, type
, addr
, port
, error_msg
, error_code
);
216 static int plug_proxy_closing (Plug p
, const char *error_msg
,
217 int error_code
, int calling_back
)
219 Proxy_Plug pp
= (Proxy_Plug
) p
;
220 Proxy_Socket ps
= pp
->proxy_socket
;
222 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
223 ps
->closing_error_msg
= error_msg
;
224 ps
->closing_error_code
= error_code
;
225 ps
->closing_calling_back
= calling_back
;
226 return ps
->negotiate(ps
, PROXY_CHANGE_CLOSING
);
228 return plug_closing(ps
->plug
, error_msg
,
229 error_code
, calling_back
);
232 static int plug_proxy_receive (Plug p
, int urgent
, char *data
, int len
)
234 Proxy_Plug pp
= (Proxy_Plug
) p
;
235 Proxy_Socket ps
= pp
->proxy_socket
;
237 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
238 /* we will lose the urgentness of this data, but since most,
239 * if not all, of this data will be consumed by the negotiation
240 * process, hopefully it won't affect the protocol above us
242 bufchain_add(&ps
->pending_input_data
, data
, len
);
243 ps
->receive_urgent
= urgent
;
244 ps
->receive_data
= data
;
245 ps
->receive_len
= len
;
246 return ps
->negotiate(ps
, PROXY_CHANGE_RECEIVE
);
248 return plug_receive(ps
->plug
, urgent
, data
, len
);
251 static void plug_proxy_sent (Plug p
, int bufsize
)
253 Proxy_Plug pp
= (Proxy_Plug
) p
;
254 Proxy_Socket ps
= pp
->proxy_socket
;
256 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
257 ps
->sent_bufsize
= bufsize
;
258 ps
->negotiate(ps
, PROXY_CHANGE_SENT
);
261 plug_sent(ps
->plug
, bufsize
);
264 static int plug_proxy_accepting (Plug p
, OSSocket sock
)
266 Proxy_Plug pp
= (Proxy_Plug
) p
;
267 Proxy_Socket ps
= pp
->proxy_socket
;
269 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
270 ps
->accepting_sock
= sock
;
271 return ps
->negotiate(ps
, PROXY_CHANGE_ACCEPTING
);
273 return plug_accepting(ps
->plug
, sock
);
277 * This function can accept a NULL pointer as `addr', in which case
278 * it will only check the host name.
280 static int proxy_for_destination (SockAddr addr
, char *hostname
, int port
,
285 int hostip_len
, hostname_len
;
286 const char *exclude_list
;
289 * Special local connections such as Unix-domain sockets
290 * unconditionally cannot be proxied, even in proxy-localhost
291 * mode. There just isn't any way to ask any known proxy type for
294 if (addr
&& sk_address_is_special_local(addr
))
295 return 0; /* do not proxy */
298 * Check the host name and IP against the hard-coded
299 * representations of `localhost'.
301 if (!conf_get_int(conf
, CONF_even_proxy_localhost
) &&
302 (sk_hostname_is_local(hostname
) ||
303 (addr
&& sk_address_is_local(addr
))))
304 return 0; /* do not proxy */
306 /* we want a string representation of the IP address for comparisons */
308 sk_getaddr(addr
, hostip
, 64);
309 hostip_len
= strlen(hostip
);
311 hostip_len
= 0; /* placate gcc; shouldn't be required */
313 hostname_len
= strlen(hostname
);
315 exclude_list
= conf_get_str(conf
, CONF_proxy_exclude_list
);
317 /* now parse the exclude list, and see if either our IP
318 * or hostname matches anything in it.
321 while (exclude_list
[s
]) {
322 while (exclude_list
[s
] &&
323 (isspace((unsigned char)exclude_list
[s
]) ||
324 exclude_list
[s
] == ',')) s
++;
326 if (!exclude_list
[s
]) break;
330 while (exclude_list
[e
] &&
331 (isalnum((unsigned char)exclude_list
[e
]) ||
332 exclude_list
[e
] == '-' ||
333 exclude_list
[e
] == '.' ||
334 exclude_list
[e
] == '*')) e
++;
336 if (exclude_list
[s
] == '*') {
337 /* wildcard at beginning of entry */
339 if ((addr
&& strnicmp(hostip
+ hostip_len
- (e
- s
- 1),
340 exclude_list
+ s
+ 1, e
- s
- 1) == 0) ||
341 strnicmp(hostname
+ hostname_len
- (e
- s
- 1),
342 exclude_list
+ s
+ 1, e
- s
- 1) == 0)
343 return 0; /* IP/hostname range excluded. do not use proxy. */
345 } else if (exclude_list
[e
-1] == '*') {
346 /* wildcard at end of entry */
348 if ((addr
&& strnicmp(hostip
, exclude_list
+ s
, e
- s
- 1) == 0) ||
349 strnicmp(hostname
, exclude_list
+ s
, e
- s
- 1) == 0)
350 return 0; /* IP/hostname range excluded. do not use proxy. */
353 /* no wildcard at either end, so let's try an absolute
354 * match (ie. a specific IP)
357 if (addr
&& strnicmp(hostip
, exclude_list
+ s
, e
- s
) == 0)
358 return 0; /* IP/hostname excluded. do not use proxy. */
359 if (strnicmp(hostname
, exclude_list
+ s
, e
- s
) == 0)
360 return 0; /* IP/hostname excluded. do not use proxy. */
365 /* Make sure we really have reached the next comma or end-of-string */
366 while (exclude_list
[s
] &&
367 !isspace((unsigned char)exclude_list
[s
]) &&
368 exclude_list
[s
] != ',') s
++;
371 /* no matches in the exclude list, so use the proxy */
375 SockAddr
name_lookup(char *host
, int port
, char **canonicalname
,
376 Conf
*conf
, int addressfamily
)
378 if (conf_get_int(conf
, CONF_proxy_type
) != PROXY_NONE
&&
379 do_proxy_dns(conf
) &&
380 proxy_for_destination(NULL
, host
, port
, conf
)) {
381 *canonicalname
= dupstr(host
);
382 return sk_nonamelookup(host
);
385 return sk_namelookup(host
, canonicalname
, addressfamily
);
388 Socket
new_connection(SockAddr addr
, char *hostname
,
389 int port
, int privport
,
390 int oobinline
, int nodelay
, int keepalive
,
391 Plug plug
, Conf
*conf
)
393 static const struct socket_function_table socket_fn_table
= {
400 sk_proxy_set_private_ptr
,
401 sk_proxy_get_private_ptr
,
403 sk_proxy_socket_error
406 static const struct plug_function_table plug_fn_table
= {
414 if (conf_get_int(conf
, CONF_proxy_type
) != PROXY_NONE
&&
415 proxy_for_destination(addr
, hostname
, port
, conf
))
420 char *proxy_canonical_name
;
424 if ((sret
= platform_new_connection(addr
, hostname
, port
, privport
,
425 oobinline
, nodelay
, keepalive
,
430 ret
= snew(struct Socket_proxy_tag
);
431 ret
->fn
= &socket_fn_table
;
432 ret
->conf
= conf_copy(conf
);
434 ret
->remote_addr
= addr
; /* will need to be freed on close */
435 ret
->remote_port
= port
;
438 ret
->pending_flush
= 0;
439 ret
->pending_eof
= 0;
442 bufchain_init(&ret
->pending_input_data
);
443 bufchain_init(&ret
->pending_output_data
);
444 bufchain_init(&ret
->pending_oob_output_data
);
446 ret
->sub_socket
= NULL
;
447 ret
->state
= PROXY_STATE_NEW
;
448 ret
->negotiate
= NULL
;
450 type
= conf_get_int(conf
, CONF_proxy_type
);
451 if (type
== PROXY_HTTP
) {
452 ret
->negotiate
= proxy_http_negotiate
;
453 } else if (type
== PROXY_SOCKS4
) {
454 ret
->negotiate
= proxy_socks4_negotiate
;
455 } else if (type
== PROXY_SOCKS5
) {
456 ret
->negotiate
= proxy_socks5_negotiate
;
457 } else if (type
== PROXY_TELNET
) {
458 ret
->negotiate
= proxy_telnet_negotiate
;
460 ret
->error
= "Proxy error: Unknown proxy method";
464 /* create the proxy plug to map calls from the actual
465 * socket into our proxy socket layer */
466 pplug
= snew(struct Plug_proxy_tag
);
467 pplug
->fn
= &plug_fn_table
;
468 pplug
->proxy_socket
= ret
;
471 proxy_addr
= sk_namelookup(conf_get_str(conf
, CONF_proxy_host
),
472 &proxy_canonical_name
,
473 conf_get_int(conf
, CONF_addressfamily
));
474 if (sk_addr_error(proxy_addr
) != NULL
) {
475 ret
->error
= "Proxy error: Unable to resolve proxy host name";
478 sfree(proxy_canonical_name
);
480 /* create the actual socket we will be using,
481 * connected to our proxy server and port.
483 ret
->sub_socket
= sk_new(proxy_addr
,
484 conf_get_int(conf
, CONF_proxy_port
),
486 nodelay
, keepalive
, (Plug
) pplug
);
487 if (sk_socket_error(ret
->sub_socket
) != NULL
)
490 /* start the proxy negotiation process... */
491 sk_set_frozen(ret
->sub_socket
, 0);
492 ret
->negotiate(ret
, PROXY_CHANGE_NEW
);
497 /* no proxy, so just return the direct socket */
498 return sk_new(addr
, port
, privport
, oobinline
, nodelay
, keepalive
, plug
);
501 Socket
new_listener(char *srcaddr
, int port
, Plug plug
, int local_host_only
,
502 Conf
*conf
, int addressfamily
)
504 /* TODO: SOCKS (and potentially others) support inbound
505 * TODO: connections via the proxy. support them.
508 return sk_newlistener(srcaddr
, port
, plug
, local_host_only
, addressfamily
);
511 /* ----------------------------------------------------------------------
512 * HTTP CONNECT proxy type.
515 static int get_line_end (char * data
, int len
)
521 if (data
[off
] == '\n') {
522 /* we have a newline */
525 /* is that the only thing on this line? */
526 if (off
<= 2) return off
;
528 /* if not, then there is the possibility that this header
529 * continues onto the next line, if it starts with a space
534 data
[off
+1] != ' ' &&
535 data
[off
+1] != '\t') return off
;
537 /* the line does continue, so we have to keep going
538 * until we see an the header's "real" end of line.
549 int proxy_http_negotiate (Proxy_Socket p
, int change
)
551 if (p
->state
== PROXY_STATE_NEW
) {
552 /* we are just beginning the proxy negotiate process,
553 * so we'll send off the initial bits of the request.
554 * for this proxy method, it's just a simple HTTP
557 char *buf
, dest
[512];
558 char *username
, *password
;
560 sk_getaddr(p
->remote_addr
, dest
, lenof(dest
));
562 buf
= dupprintf("CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
563 dest
, p
->remote_port
, dest
, p
->remote_port
);
564 sk_write(p
->sub_socket
, buf
, strlen(buf
));
567 username
= conf_get_str(p
->conf
, CONF_proxy_username
);
568 password
= conf_get_str(p
->conf
, CONF_proxy_password
);
569 if (username
[0] || password
[0]) {
572 buf
= dupprintf("%s:%s", username
, password
);
574 buf2
= snewn(len
* 4 / 3 + 100, char);
575 sprintf(buf2
, "Proxy-Authorization: Basic ");
576 for (i
= 0, j
= strlen(buf2
); i
< len
; i
+= 3, j
+= 4)
577 base64_encode_atom((unsigned char *)(buf
+i
),
578 (len
-i
> 3 ?
3 : len
-i
), buf2
+j
);
579 strcpy(buf2
+j
, "\r\n");
580 sk_write(p
->sub_socket
, buf2
, strlen(buf2
));
585 sk_write(p
->sub_socket
, "\r\n", 2);
591 if (change
== PROXY_CHANGE_CLOSING
) {
592 /* if our proxy negotiation process involves closing and opening
593 * new sockets, then we would want to intercept this closing
594 * callback when we were expecting it. if we aren't anticipating
595 * a socket close, then some error must have occurred. we'll
596 * just pass those errors up to the backend.
598 return plug_closing(p
->plug
, p
->closing_error_msg
,
599 p
->closing_error_code
,
600 p
->closing_calling_back
);
603 if (change
== PROXY_CHANGE_SENT
) {
604 /* some (or all) of what we wrote to the proxy was sent.
605 * we don't do anything new, however, until we receive the
606 * proxy's response. we might want to set a timer so we can
607 * timeout the proxy negotiation after a while...
612 if (change
== PROXY_CHANGE_ACCEPTING
) {
613 /* we should _never_ see this, as we are using our socket to
614 * connect to a proxy, not accepting inbound connections.
615 * what should we do? close the socket with an appropriate
618 return plug_accepting(p
->plug
, p
->accepting_sock
);
621 if (change
== PROXY_CHANGE_RECEIVE
) {
622 /* we have received data from the underlying socket, which
623 * we'll need to parse, process, and respond to appropriately.
632 int min_ver
, maj_ver
, status
;
634 /* get the status line */
635 len
= bufchain_size(&p
->pending_input_data
);
636 assert(len
> 0); /* or we wouldn't be here */
637 data
= snewn(len
+1, char);
638 bufchain_fetch(&p
->pending_input_data
, data
, len
);
640 * We must NUL-terminate this data, because Windows
641 * sscanf appears to require a NUL at the end of the
642 * string because it strlens it _first_. Sigh.
646 eol
= get_line_end(data
, len
);
653 /* We can't rely on whether the %n incremented the sscanf return */
654 if (sscanf((char *)data
, "HTTP/%i.%i %n",
655 &maj_ver
, &min_ver
, &status
) < 2 || status
== -1) {
656 plug_closing(p
->plug
, "Proxy error: HTTP response was absent",
657 PROXY_ERROR_GENERAL
, 0);
662 /* remove the status line from the input buffer. */
663 bufchain_consume(&p
->pending_input_data
, eol
);
664 if (data
[status
] != '2') {
668 while (eol
> status
&&
669 (data
[eol
-1] == '\r' || data
[eol
-1] == '\n'))
671 buf
= dupprintf("Proxy error: %s", data
+status
);
672 plug_closing(p
->plug
, buf
, PROXY_ERROR_GENERAL
, 0);
685 /* get headers. we're done when we get a
686 * header of length 2, (ie. just "\r\n")
689 len
= bufchain_size(&p
->pending_input_data
);
690 assert(len
> 0); /* or we wouldn't be here */
691 data
= snewn(len
, char);
693 bufchain_fetch(&p
->pending_input_data
, data
, len
);
695 eol
= get_line_end(datap
, len
);
702 bufchain_consume(&p
->pending_input_data
, eol
);
705 eol
= get_line_end(datap
, len
);
710 bufchain_consume(&p
->pending_input_data
, 2);
712 /* proxy activate will have dealt with
713 * whatever is left of the buffer */
723 plug_closing(p
->plug
, "Proxy error: unexpected proxy error",
724 PROXY_ERROR_UNEXPECTED
, 0);
728 /* ----------------------------------------------------------------------
732 /* SOCKS version 4 */
733 int proxy_socks4_negotiate (Proxy_Socket p
, int change
)
735 if (p
->state
== PROXY_CHANGE_NEW
) {
738 * version number (1 byte) = 4
739 * command code (1 byte)
742 * dest. port (2 bytes) [network order]
743 * dest. address (4 bytes)
744 * user ID (variable length, null terminated string)
747 int length
, type
, namelen
;
748 char *command
, addr
[4], hostname
[512];
751 type
= sk_addrtype(p
->remote_addr
);
752 if (type
== ADDRTYPE_IPV6
) {
753 p
->error
= "Proxy error: SOCKS version 4 does not support IPv6";
755 } else if (type
== ADDRTYPE_IPV4
) {
757 sk_addrcopy(p
->remote_addr
, addr
);
758 } else { /* type == ADDRTYPE_NAME */
759 assert(type
== ADDRTYPE_NAME
);
760 sk_getaddr(p
->remote_addr
, hostname
, lenof(hostname
));
761 namelen
= strlen(hostname
) + 1; /* include the NUL */
762 addr
[0] = addr
[1] = addr
[2] = 0;
766 username
= conf_get_str(p
->conf
, CONF_proxy_username
);
767 length
= strlen(username
) + namelen
+ 9;
768 command
= snewn(length
, char);
769 strcpy(command
+ 8, username
);
771 command
[0] = 4; /* version 4 */
772 command
[1] = 1; /* CONNECT command */
775 command
[2] = (char) (p
->remote_port
>> 8) & 0xff;
776 command
[3] = (char) p
->remote_port
& 0xff;
779 memcpy(command
+ 4, addr
, 4);
782 memcpy(command
+ 8 + strlen(username
) + 1,
785 sk_write(p
->sub_socket
, command
, length
);
793 if (change
== PROXY_CHANGE_CLOSING
) {
794 /* if our proxy negotiation process involves closing and opening
795 * new sockets, then we would want to intercept this closing
796 * callback when we were expecting it. if we aren't anticipating
797 * a socket close, then some error must have occurred. we'll
798 * just pass those errors up to the backend.
800 return plug_closing(p
->plug
, p
->closing_error_msg
,
801 p
->closing_error_code
,
802 p
->closing_calling_back
);
805 if (change
== PROXY_CHANGE_SENT
) {
806 /* some (or all) of what we wrote to the proxy was sent.
807 * we don't do anything new, however, until we receive the
808 * proxy's response. we might want to set a timer so we can
809 * timeout the proxy negotiation after a while...
814 if (change
== PROXY_CHANGE_ACCEPTING
) {
815 /* we should _never_ see this, as we are using our socket to
816 * connect to a proxy, not accepting inbound connections.
817 * what should we do? close the socket with an appropriate
820 return plug_accepting(p
->plug
, p
->accepting_sock
);
823 if (change
== PROXY_CHANGE_RECEIVE
) {
824 /* we have received data from the underlying socket, which
825 * we'll need to parse, process, and respond to appropriately.
830 * version number (1 byte) = 4
831 * reply code (1 byte)
832 * 90 = request granted
833 * 91 = request rejected or failed
834 * 92 = request rejected due to lack of IDENTD on client
835 * 93 = request rejected due to difference in user ID
836 * (what we sent vs. what IDENTD said)
837 * dest. port (2 bytes)
838 * dest. address (4 bytes)
843 if (bufchain_size(&p
->pending_input_data
) < 8)
844 return 1; /* not got anything yet */
846 /* get the response */
847 bufchain_fetch(&p
->pending_input_data
, data
, 8);
850 plug_closing(p
->plug
, "Proxy error: SOCKS proxy responded with "
851 "unexpected reply code version",
852 PROXY_ERROR_GENERAL
, 0);
860 plug_closing(p
->plug
, "Proxy error: SOCKS server wanted IDENTD on client",
861 PROXY_ERROR_GENERAL
, 0);
864 plug_closing(p
->plug
, "Proxy error: Username and IDENTD on client don't agree",
865 PROXY_ERROR_GENERAL
, 0);
869 plug_closing(p
->plug
, "Proxy error: Error while communicating with proxy",
870 PROXY_ERROR_GENERAL
, 0);
876 bufchain_consume(&p
->pending_input_data
, 8);
880 /* proxy activate will have dealt with
881 * whatever is left of the buffer */
886 plug_closing(p
->plug
, "Proxy error: unexpected proxy error",
887 PROXY_ERROR_UNEXPECTED
, 0);
891 /* SOCKS version 5 */
892 int proxy_socks5_negotiate (Proxy_Socket p
, int change
)
894 if (p
->state
== PROXY_CHANGE_NEW
) {
897 * version number (1 byte) = 5
898 * number of available authentication methods (1 byte)
899 * available authentication methods (1 byte * previous value)
900 * authentication methods:
901 * 0x00 = no authentication
903 * 0x02 = username/password
908 char *username
, *password
;
911 command
[0] = 5; /* version 5 */
912 username
= conf_get_str(p
->conf
, CONF_proxy_username
);
913 password
= conf_get_str(p
->conf
, CONF_proxy_password
);
914 if (username
[0] || password
[0]) {
915 command
[2] = 0x00; /* no authentication */
917 proxy_socks5_offerencryptedauth (command
, &len
);
918 command
[len
++] = 0x02; /* username/password */
919 command
[1] = len
- 2; /* Number of methods supported */
921 command
[1] = 1; /* one methods supported: */
922 command
[2] = 0x00; /* no authentication */
926 sk_write(p
->sub_socket
, command
, len
);
932 if (change
== PROXY_CHANGE_CLOSING
) {
933 /* if our proxy negotiation process involves closing and opening
934 * new sockets, then we would want to intercept this closing
935 * callback when we were expecting it. if we aren't anticipating
936 * a socket close, then some error must have occurred. we'll
937 * just pass those errors up to the backend.
939 return plug_closing(p
->plug
, p
->closing_error_msg
,
940 p
->closing_error_code
,
941 p
->closing_calling_back
);
944 if (change
== PROXY_CHANGE_SENT
) {
945 /* some (or all) of what we wrote to the proxy was sent.
946 * we don't do anything new, however, until we receive the
947 * proxy's response. we might want to set a timer so we can
948 * timeout the proxy negotiation after a while...
953 if (change
== PROXY_CHANGE_ACCEPTING
) {
954 /* we should _never_ see this, as we are using our socket to
955 * connect to a proxy, not accepting inbound connections.
956 * what should we do? close the socket with an appropriate
959 return plug_accepting(p
->plug
, p
->accepting_sock
);
962 if (change
== PROXY_CHANGE_RECEIVE
) {
963 /* we have received data from the underlying socket, which
964 * we'll need to parse, process, and respond to appropriately.
970 * version number (1 byte) = 5
971 * authentication method (1 byte)
972 * authentication methods:
973 * 0x00 = no authentication
975 * 0x02 = username/password
977 * 0xff = no acceptable methods
981 if (bufchain_size(&p
->pending_input_data
) < 2)
982 return 1; /* not got anything yet */
984 /* get the response */
985 bufchain_fetch(&p
->pending_input_data
, data
, 2);
988 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned unexpected version",
989 PROXY_ERROR_GENERAL
, 0);
993 if (data
[1] == 0x00) p
->state
= 2; /* no authentication needed */
994 else if (data
[1] == 0x01) p
->state
= 4; /* GSSAPI authentication */
995 else if (data
[1] == 0x02) p
->state
= 5; /* username/password authentication */
996 else if (data
[1] == 0x03) p
->state
= 6; /* CHAP authentication */
998 plug_closing(p
->plug
, "Proxy error: SOCKS proxy did not accept our authentication",
999 PROXY_ERROR_GENERAL
, 0);
1002 bufchain_consume(&p
->pending_input_data
, 2);
1005 if (p
->state
== 7) {
1007 /* password authentication reply format:
1008 * version number (1 bytes) = 1
1009 * reply code (1 byte)
1015 if (bufchain_size(&p
->pending_input_data
) < 2)
1016 return 1; /* not got anything yet */
1018 /* get the response */
1019 bufchain_fetch(&p
->pending_input_data
, data
, 2);
1022 plug_closing(p
->plug
, "Proxy error: SOCKS password "
1023 "subnegotiation contained wrong version number",
1024 PROXY_ERROR_GENERAL
, 0);
1030 plug_closing(p
->plug
, "Proxy error: SOCKS proxy refused"
1031 " password authentication",
1032 PROXY_ERROR_GENERAL
, 0);
1036 bufchain_consume(&p
->pending_input_data
, 2);
1037 p
->state
= 2; /* now proceed as authenticated */
1040 if (p
->state
== 8) {
1042 ret
= proxy_socks5_handlechap(p
);
1043 if (ret
) return ret
;
1046 if (p
->state
== 2) {
1049 * version number (1 byte) = 5
1050 * command code (1 byte)
1054 * reserved (1 byte) = 0x00
1055 * address type (1 byte)
1057 * 3 = domainname (first byte has length, no terminating null)
1059 * dest. address (variable)
1060 * dest. port (2 bytes) [network order]
1067 type
= sk_addrtype(p
->remote_addr
);
1068 if (type
== ADDRTYPE_IPV4
) {
1069 len
= 10; /* 4 hdr + 4 addr + 2 trailer */
1070 command
[3] = 1; /* IPv4 */
1071 sk_addrcopy(p
->remote_addr
, command
+4);
1072 } else if (type
== ADDRTYPE_IPV6
) {
1073 len
= 22; /* 4 hdr + 16 addr + 2 trailer */
1074 command
[3] = 4; /* IPv6 */
1075 sk_addrcopy(p
->remote_addr
, command
+4);
1077 assert(type
== ADDRTYPE_NAME
);
1079 sk_getaddr(p
->remote_addr
, command
+5, 256);
1080 command
[4] = strlen(command
+5);
1081 len
= 7 + command
[4]; /* 4 hdr, 1 len, N addr, 2 trailer */
1084 command
[0] = 5; /* version 5 */
1085 command
[1] = 1; /* CONNECT command */
1089 command
[len
-2] = (char) (p
->remote_port
>> 8) & 0xff;
1090 command
[len
-1] = (char) p
->remote_port
& 0xff;
1092 sk_write(p
->sub_socket
, command
, len
);
1098 if (p
->state
== 3) {
1101 * version number (1 bytes) = 5
1102 * reply code (1 byte)
1104 * 1 = general SOCKS server failure
1105 * 2 = connection not allowed by ruleset
1106 * 3 = network unreachable
1107 * 4 = host unreachable
1108 * 5 = connection refused
1110 * 7 = command not supported
1111 * 8 = address type not supported
1112 * reserved (1 byte) = x00
1113 * address type (1 byte)
1115 * 3 = domainname (first byte has length, no terminating null)
1117 * server bound address (variable)
1118 * server bound port (2 bytes) [network order]
1123 /* First 5 bytes of packet are enough to tell its length. */
1124 if (bufchain_size(&p
->pending_input_data
) < 5)
1125 return 1; /* not got anything yet */
1127 /* get the response */
1128 bufchain_fetch(&p
->pending_input_data
, data
, 5);
1131 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned wrong version number",
1132 PROXY_ERROR_GENERAL
, 0);
1139 strcpy(buf
, "Proxy error: ");
1142 case 1: strcat(buf
, "General SOCKS server failure"); break;
1143 case 2: strcat(buf
, "Connection not allowed by ruleset"); break;
1144 case 3: strcat(buf
, "Network unreachable"); break;
1145 case 4: strcat(buf
, "Host unreachable"); break;
1146 case 5: strcat(buf
, "Connection refused"); break;
1147 case 6: strcat(buf
, "TTL expired"); break;
1148 case 7: strcat(buf
, "Command not supported"); break;
1149 case 8: strcat(buf
, "Address type not supported"); break;
1150 default: sprintf(buf
+strlen(buf
),
1151 "Unrecognised SOCKS error code %d",
1155 plug_closing(p
->plug
, buf
, PROXY_ERROR_GENERAL
, 0);
1161 * Eat the rest of the reply packet.
1163 len
= 6; /* first 4 bytes, last 2 */
1165 case 1: len
+= 4; break; /* IPv4 address */
1166 case 4: len
+= 16; break;/* IPv6 address */
1167 case 3: len
+= (unsigned char)data
[4]; break; /* domain name */
1169 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned "
1170 "unrecognised address format",
1171 PROXY_ERROR_GENERAL
, 0);
1174 if (bufchain_size(&p
->pending_input_data
) < len
)
1175 return 1; /* not got whole reply yet */
1176 bufchain_consume(&p
->pending_input_data
, len
);
1183 if (p
->state
== 4) {
1184 /* TODO: Handle GSSAPI authentication */
1185 plug_closing(p
->plug
, "Proxy error: We don't support GSSAPI authentication",
1186 PROXY_ERROR_GENERAL
, 0);
1190 if (p
->state
== 5) {
1191 char *username
= conf_get_str(p
->conf
, CONF_proxy_username
);
1192 char *password
= conf_get_str(p
->conf
, CONF_proxy_password
);
1193 if (username
[0] || password
[0]) {
1194 char userpwbuf
[255 + 255 + 3];
1196 ulen
= strlen(username
);
1197 if (ulen
> 255) ulen
= 255; if (ulen
< 1) ulen
= 1;
1198 plen
= strlen(password
);
1199 if (plen
> 255) plen
= 255; if (plen
< 1) plen
= 1;
1200 userpwbuf
[0] = 1; /* version number of subnegotiation */
1201 userpwbuf
[1] = ulen
;
1202 memcpy(userpwbuf
+2, username
, ulen
);
1203 userpwbuf
[ulen
+2] = plen
;
1204 memcpy(userpwbuf
+ulen
+3, password
, plen
);
1205 sk_write(p
->sub_socket
, userpwbuf
, ulen
+ plen
+ 3);
1208 plug_closing(p
->plug
, "Proxy error: Server chose "
1209 "username/password authentication but we "
1211 PROXY_ERROR_GENERAL
, 0);
1215 if (p
->state
== 6) {
1217 ret
= proxy_socks5_selectchap(p
);
1218 if (ret
) return ret
;
1223 plug_closing(p
->plug
, "Proxy error: Unexpected proxy error",
1224 PROXY_ERROR_UNEXPECTED
, 0);
1228 /* ----------------------------------------------------------------------
1229 * `Telnet' proxy type.
1231 * (This is for ad-hoc proxies where you connect to the proxy's
1232 * telnet port and send a command such as `connect host port'. The
1233 * command is configurable, since this proxy type is typically not
1234 * standardised or at all well-defined.)
1237 char *format_telnet_command(SockAddr addr
, int port
, Conf
*conf
)
1239 char *fmt
= conf_get_str(conf
, CONF_proxy_telnet_command
);
1241 int retlen
= 0, retsize
= 0;
1243 #define ENSURE(n) do { \
1244 if (retsize < retlen + n) { \
1245 retsize = retlen + n + 512; \
1246 ret = sresize(ret, retsize, char); \
1250 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1251 * %%, %host, %port, %user, and %pass
1254 while (fmt
[eo
] != 0) {
1256 /* scan forward until we hit end-of-line,
1257 * or an escape character (\ or %) */
1258 while (fmt
[eo
] != 0 && fmt
[eo
] != '%' && fmt
[eo
] != '\\')
1261 /* if we hit eol, break out of our escaping loop */
1262 if (fmt
[eo
] == 0) break;
1264 /* if there was any unescaped text before the escape
1265 * character, send that now */
1268 memcpy(ret
+ retlen
, fmt
+ so
, eo
- so
);
1274 /* if the escape character was the last character of
1275 * the line, we'll just stop and send it. */
1276 if (fmt
[eo
] == 0) break;
1278 if (fmt
[so
] == '\\') {
1280 /* we recognize \\, \%, \r, \n, \t, \x??.
1281 * anything else, we just send unescaped (including the \).
1288 ret
[retlen
++] = '\\';
1294 ret
[retlen
++] = '%';
1300 ret
[retlen
++] = '\r';
1306 ret
[retlen
++] = '\n';
1312 ret
[retlen
++] = '\t';
1319 /* escaped hexadecimal value (ie. \xff) */
1320 unsigned char v
= 0;
1325 if (fmt
[eo
] >= '0' && fmt
[eo
] <= '9')
1327 else if (fmt
[eo
] >= 'a' && fmt
[eo
] <= 'f')
1328 v
+= fmt
[eo
] - 'a' + 10;
1329 else if (fmt
[eo
] >= 'A' && fmt
[eo
] <= 'F')
1330 v
+= fmt
[eo
] - 'A' + 10;
1332 /* non hex character, so we abort and just
1333 * send the whole thing unescaped (including \x)
1336 ret
[retlen
++] = '\\';
1341 /* we only extract two hex characters */
1357 memcpy(ret
+retlen
, fmt
+ so
, 2);
1364 /* % escape. we recognize %%, %host, %port, %user, %pass.
1365 * %proxyhost, %proxyport. Anything else we just send
1366 * unescaped (including the %).
1369 if (fmt
[eo
] == '%') {
1371 ret
[retlen
++] = '%';
1374 else if (strnicmp(fmt
+ eo
, "host", 4) == 0) {
1377 sk_getaddr(addr
, dest
, lenof(dest
));
1378 destlen
= strlen(dest
);
1380 memcpy(ret
+retlen
, dest
, destlen
);
1384 else if (strnicmp(fmt
+ eo
, "port", 4) == 0) {
1385 char portstr
[8], portlen
;
1386 portlen
= sprintf(portstr
, "%i", port
);
1388 memcpy(ret
+ retlen
, portstr
, portlen
);
1392 else if (strnicmp(fmt
+ eo
, "user", 4) == 0) {
1393 char *username
= conf_get_str(conf
, CONF_proxy_username
);
1394 int userlen
= strlen(username
);
1396 memcpy(ret
+retlen
, username
, userlen
);
1400 else if (strnicmp(fmt
+ eo
, "pass", 4) == 0) {
1401 char *password
= conf_get_str(conf
, CONF_proxy_password
);
1402 int passlen
= strlen(password
);
1404 memcpy(ret
+retlen
, password
, passlen
);
1408 else if (strnicmp(fmt
+ eo
, "proxyhost", 9) == 0) {
1409 char *host
= conf_get_str(conf
, CONF_proxy_host
);
1410 int phlen
= strlen(host
);
1412 memcpy(ret
+retlen
, host
, phlen
);
1416 else if (strnicmp(fmt
+ eo
, "proxyport", 9) == 0) {
1417 int port
= conf_get_int(conf
, CONF_proxy_port
);
1420 sprintf(pport
, "%d", port
);
1421 pplen
= strlen(pport
);
1423 memcpy(ret
+retlen
, pport
, pplen
);
1428 /* we don't escape this, so send the % now, and
1429 * don't advance eo, so that we'll consider the
1430 * text immediately following the % as unescaped.
1433 ret
[retlen
++] = '%';
1437 /* resume scanning for additional escapes after this one. */
1441 /* if there is any unescaped text at the end of the line, send it */
1444 memcpy(ret
+ retlen
, fmt
+ so
, eo
- so
);
1455 int proxy_telnet_negotiate (Proxy_Socket p
, int change
)
1457 if (p
->state
== PROXY_CHANGE_NEW
) {
1458 char *formatted_cmd
;
1460 formatted_cmd
= format_telnet_command(p
->remote_addr
, p
->remote_port
,
1463 sk_write(p
->sub_socket
, formatted_cmd
, strlen(formatted_cmd
));
1464 sfree(formatted_cmd
);
1470 if (change
== PROXY_CHANGE_CLOSING
) {
1471 /* if our proxy negotiation process involves closing and opening
1472 * new sockets, then we would want to intercept this closing
1473 * callback when we were expecting it. if we aren't anticipating
1474 * a socket close, then some error must have occurred. we'll
1475 * just pass those errors up to the backend.
1477 return plug_closing(p
->plug
, p
->closing_error_msg
,
1478 p
->closing_error_code
,
1479 p
->closing_calling_back
);
1482 if (change
== PROXY_CHANGE_SENT
) {
1483 /* some (or all) of what we wrote to the proxy was sent.
1484 * we don't do anything new, however, until we receive the
1485 * proxy's response. we might want to set a timer so we can
1486 * timeout the proxy negotiation after a while...
1491 if (change
== PROXY_CHANGE_ACCEPTING
) {
1492 /* we should _never_ see this, as we are using our socket to
1493 * connect to a proxy, not accepting inbound connections.
1494 * what should we do? close the socket with an appropriate
1497 return plug_accepting(p
->plug
, p
->accepting_sock
);
1500 if (change
== PROXY_CHANGE_RECEIVE
) {
1501 /* we have received data from the underlying socket, which
1502 * we'll need to parse, process, and respond to appropriately.
1507 /* proxy activate will have dealt with
1508 * whatever is left of the buffer */
1512 plug_closing(p
->plug
, "Proxy error: Unexpected proxy error",
1513 PROXY_ERROR_UNEXPECTED
, 0);