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(cfg) \
18 (cfg->proxy_dns == FORCE_ON || \
19 (cfg->proxy_dns == AUTO && \
20 cfg->proxy_type != PROXY_SOCKS4 && \
21 cfg->proxy_type != PROXY_SOCKS5))
24 * Call this when proxy negotiation is complete, so that this
25 * socket can begin working normally.
27 void proxy_activate (Proxy_Socket p
)
31 long output_before
, output_after
;
33 p
->state
= PROXY_STATE_ACTIVE
;
35 /* we want to ignore new receive events until we have sent
36 * all of our buffered receive data.
38 sk_set_frozen(p
->sub_socket
, 1);
40 /* how many bytes of output have we buffered? */
41 output_before
= bufchain_size(&p
->pending_oob_output_data
) +
42 bufchain_size(&p
->pending_output_data
);
43 /* and keep track of how many bytes do not get sent. */
46 /* send buffered OOB writes */
47 while (bufchain_size(&p
->pending_oob_output_data
) > 0) {
48 bufchain_prefix(&p
->pending_oob_output_data
, &data
, &len
);
49 output_after
+= sk_write_oob(p
->sub_socket
, data
, len
);
50 bufchain_consume(&p
->pending_oob_output_data
, len
);
53 /* send buffered normal writes */
54 while (bufchain_size(&p
->pending_output_data
) > 0) {
55 bufchain_prefix(&p
->pending_output_data
, &data
, &len
);
56 output_after
+= sk_write(p
->sub_socket
, data
, len
);
57 bufchain_consume(&p
->pending_output_data
, len
);
60 /* if we managed to send any data, let the higher levels know. */
61 if (output_after
< output_before
)
62 plug_sent(p
->plug
, output_after
);
64 /* if we were asked to flush the output during
65 * the proxy negotiation process, do so now.
67 if (p
->pending_flush
) sk_flush(p
->sub_socket
);
69 /* if the backend wanted the socket unfrozen, try to unfreeze.
70 * our set_frozen handler will flush buffered receive data before
71 * unfreezing the actual underlying socket.
74 sk_set_frozen((Socket
)p
, 0);
77 /* basic proxy socket functions */
79 static Plug
sk_proxy_plug (Socket s
, Plug p
)
81 Proxy_Socket ps
= (Proxy_Socket
) s
;
88 static void sk_proxy_close (Socket s
)
90 Proxy_Socket ps
= (Proxy_Socket
) s
;
92 sk_close(ps
->sub_socket
);
93 sk_addr_free(ps
->remote_addr
);
97 static int sk_proxy_write (Socket s
, const char *data
, int len
)
99 Proxy_Socket ps
= (Proxy_Socket
) s
;
101 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
102 bufchain_add(&ps
->pending_output_data
, data
, len
);
103 return bufchain_size(&ps
->pending_output_data
);
105 return sk_write(ps
->sub_socket
, data
, len
);
108 static int sk_proxy_write_oob (Socket s
, const char *data
, int len
)
110 Proxy_Socket ps
= (Proxy_Socket
) s
;
112 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
113 bufchain_clear(&ps
->pending_output_data
);
114 bufchain_clear(&ps
->pending_oob_output_data
);
115 bufchain_add(&ps
->pending_oob_output_data
, data
, len
);
118 return sk_write_oob(ps
->sub_socket
, data
, len
);
121 static void sk_proxy_flush (Socket s
)
123 Proxy_Socket ps
= (Proxy_Socket
) s
;
125 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
126 ps
->pending_flush
= 1;
129 sk_flush(ps
->sub_socket
);
132 static void sk_proxy_set_private_ptr (Socket s
, void *ptr
)
134 Proxy_Socket ps
= (Proxy_Socket
) s
;
135 sk_set_private_ptr(ps
->sub_socket
, ptr
);
138 static void * sk_proxy_get_private_ptr (Socket s
)
140 Proxy_Socket ps
= (Proxy_Socket
) s
;
141 return sk_get_private_ptr(ps
->sub_socket
);
144 static void sk_proxy_set_frozen (Socket s
, int is_frozen
)
146 Proxy_Socket ps
= (Proxy_Socket
) s
;
148 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
149 ps
->freeze
= is_frozen
;
153 /* handle any remaining buffered recv data first */
154 if (bufchain_size(&ps
->pending_input_data
) > 0) {
155 ps
->freeze
= is_frozen
;
157 /* loop while we still have buffered data, and while we are
158 * unfrozen. the plug_receive call in the loop could result
159 * in a call back into this function refreezing the socket,
160 * so we have to check each time.
162 while (!ps
->freeze
&& bufchain_size(&ps
->pending_input_data
) > 0) {
166 bufchain_prefix(&ps
->pending_input_data
, &data
, &len
);
167 if (len
> lenof(databuf
))
168 len
= lenof(databuf
);
169 memcpy(databuf
, data
, len
);
170 bufchain_consume(&ps
->pending_input_data
, len
);
171 plug_receive(ps
->plug
, 0, databuf
, len
);
174 /* if we're still frozen, we'll have to wait for another
175 * call from the backend to finish unbuffering the data.
177 if (ps
->freeze
) return;
180 sk_set_frozen(ps
->sub_socket
, is_frozen
);
183 static const char * sk_proxy_socket_error (Socket s
)
185 Proxy_Socket ps
= (Proxy_Socket
) s
;
186 if (ps
->error
!= NULL
|| ps
->sub_socket
== NULL
) {
189 return sk_socket_error(ps
->sub_socket
);
192 /* basic proxy plug functions */
194 static void plug_proxy_log(Plug plug
, int type
, SockAddr addr
, int port
,
195 const char *error_msg
, int error_code
)
197 Proxy_Plug pp
= (Proxy_Plug
) plug
;
198 Proxy_Socket ps
= pp
->proxy_socket
;
200 plug_log(ps
->plug
, type
, addr
, port
, error_msg
, error_code
);
203 static int plug_proxy_closing (Plug p
, const char *error_msg
,
204 int error_code
, int calling_back
)
206 Proxy_Plug pp
= (Proxy_Plug
) p
;
207 Proxy_Socket ps
= pp
->proxy_socket
;
209 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
210 ps
->closing_error_msg
= error_msg
;
211 ps
->closing_error_code
= error_code
;
212 ps
->closing_calling_back
= calling_back
;
213 return ps
->negotiate(ps
, PROXY_CHANGE_CLOSING
);
215 return plug_closing(ps
->plug
, error_msg
,
216 error_code
, calling_back
);
219 static int plug_proxy_receive (Plug p
, int urgent
, char *data
, int len
)
221 Proxy_Plug pp
= (Proxy_Plug
) p
;
222 Proxy_Socket ps
= pp
->proxy_socket
;
224 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
225 /* we will lose the urgentness of this data, but since most,
226 * if not all, of this data will be consumed by the negotiation
227 * process, hopefully it won't affect the protocol above us
229 bufchain_add(&ps
->pending_input_data
, data
, len
);
230 ps
->receive_urgent
= urgent
;
231 ps
->receive_data
= data
;
232 ps
->receive_len
= len
;
233 return ps
->negotiate(ps
, PROXY_CHANGE_RECEIVE
);
235 return plug_receive(ps
->plug
, urgent
, data
, len
);
238 static void plug_proxy_sent (Plug p
, int bufsize
)
240 Proxy_Plug pp
= (Proxy_Plug
) p
;
241 Proxy_Socket ps
= pp
->proxy_socket
;
243 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
244 ps
->sent_bufsize
= bufsize
;
245 ps
->negotiate(ps
, PROXY_CHANGE_SENT
);
248 plug_sent(ps
->plug
, bufsize
);
251 static int plug_proxy_accepting (Plug p
, OSSocket sock
)
253 Proxy_Plug pp
= (Proxy_Plug
) p
;
254 Proxy_Socket ps
= pp
->proxy_socket
;
256 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
257 ps
->accepting_sock
= sock
;
258 return ps
->negotiate(ps
, PROXY_CHANGE_ACCEPTING
);
260 return plug_accepting(ps
->plug
, sock
);
264 * This function can accept a NULL pointer as `addr', in which case
265 * it will only check the host name.
267 static int proxy_for_destination (SockAddr addr
, char *hostname
, int port
,
272 int hostip_len
, hostname_len
;
273 const char *exclude_list
;
276 * Check the host name and IP against the hard-coded
277 * representations of `localhost'.
279 if (!cfg
->even_proxy_localhost
&&
280 (sk_hostname_is_local(hostname
) ||
281 (addr
&& sk_address_is_local(addr
))))
282 return 0; /* do not proxy */
284 /* we want a string representation of the IP address for comparisons */
286 sk_getaddr(addr
, hostip
, 64);
287 hostip_len
= strlen(hostip
);
289 hostip_len
= 0; /* placate gcc; shouldn't be required */
291 hostname_len
= strlen(hostname
);
293 exclude_list
= cfg
->proxy_exclude_list
;
295 /* now parse the exclude list, and see if either our IP
296 * or hostname matches anything in it.
299 while (exclude_list
[s
]) {
300 while (exclude_list
[s
] &&
301 (isspace((unsigned char)exclude_list
[s
]) ||
302 exclude_list
[s
] == ',')) s
++;
304 if (!exclude_list
[s
]) break;
308 while (exclude_list
[e
] &&
309 (isalnum((unsigned char)exclude_list
[e
]) ||
310 exclude_list
[e
] == '-' ||
311 exclude_list
[e
] == '.' ||
312 exclude_list
[e
] == '*')) e
++;
314 if (exclude_list
[s
] == '*') {
315 /* wildcard at beginning of entry */
317 if ((addr
&& strnicmp(hostip
+ hostip_len
- (e
- s
- 1),
318 exclude_list
+ s
+ 1, e
- s
- 1) == 0) ||
319 strnicmp(hostname
+ hostname_len
- (e
- s
- 1),
320 exclude_list
+ s
+ 1, e
- s
- 1) == 0)
321 return 0; /* IP/hostname range excluded. do not use proxy. */
323 } else if (exclude_list
[e
-1] == '*') {
324 /* wildcard at end of entry */
326 if ((addr
&& strnicmp(hostip
, exclude_list
+ s
, e
- s
- 1) == 0) ||
327 strnicmp(hostname
, exclude_list
+ s
, e
- s
- 1) == 0)
328 return 0; /* IP/hostname range excluded. do not use proxy. */
331 /* no wildcard at either end, so let's try an absolute
332 * match (ie. a specific IP)
335 if (addr
&& strnicmp(hostip
, exclude_list
+ s
, e
- s
) == 0)
336 return 0; /* IP/hostname excluded. do not use proxy. */
337 if (strnicmp(hostname
, exclude_list
+ s
, e
- s
) == 0)
338 return 0; /* IP/hostname excluded. do not use proxy. */
343 /* Make sure we really have reached the next comma or end-of-string */
344 while (exclude_list
[s
] &&
345 !isspace((unsigned char)exclude_list
[s
]) &&
346 exclude_list
[s
] != ',') s
++;
349 /* no matches in the exclude list, so use the proxy */
353 SockAddr
name_lookup(char *host
, int port
, char **canonicalname
,
354 const Config
*cfg
, int addressfamily
)
356 if (cfg
->proxy_type
!= PROXY_NONE
&&
358 proxy_for_destination(NULL
, host
, port
, cfg
)) {
359 *canonicalname
= dupstr(host
);
360 return sk_nonamelookup(host
);
363 return sk_namelookup(host
, canonicalname
, addressfamily
);
366 Socket
new_connection(SockAddr addr
, char *hostname
,
367 int port
, int privport
,
368 int oobinline
, int nodelay
, int keepalive
,
369 Plug plug
, const Config
*cfg
)
371 static const struct socket_function_table socket_fn_table
= {
377 sk_proxy_set_private_ptr
,
378 sk_proxy_get_private_ptr
,
380 sk_proxy_socket_error
383 static const struct plug_function_table plug_fn_table
= {
391 if (cfg
->proxy_type
!= PROXY_NONE
&&
392 proxy_for_destination(addr
, hostname
, port
, cfg
))
397 char *proxy_canonical_name
;
400 if ((sret
= platform_new_connection(addr
, hostname
, port
, privport
,
401 oobinline
, nodelay
, keepalive
,
406 ret
= snew(struct Socket_proxy_tag
);
407 ret
->fn
= &socket_fn_table
;
408 ret
->cfg
= *cfg
; /* STRUCTURE COPY */
410 ret
->remote_addr
= addr
; /* will need to be freed on close */
411 ret
->remote_port
= port
;
414 ret
->pending_flush
= 0;
417 bufchain_init(&ret
->pending_input_data
);
418 bufchain_init(&ret
->pending_output_data
);
419 bufchain_init(&ret
->pending_oob_output_data
);
421 ret
->sub_socket
= NULL
;
422 ret
->state
= PROXY_STATE_NEW
;
423 ret
->negotiate
= NULL
;
425 if (cfg
->proxy_type
== PROXY_HTTP
) {
426 ret
->negotiate
= proxy_http_negotiate
;
427 } else if (cfg
->proxy_type
== PROXY_SOCKS4
) {
428 ret
->negotiate
= proxy_socks4_negotiate
;
429 } else if (cfg
->proxy_type
== PROXY_SOCKS5
) {
430 ret
->negotiate
= proxy_socks5_negotiate
;
431 } else if (cfg
->proxy_type
== PROXY_TELNET
) {
432 ret
->negotiate
= proxy_telnet_negotiate
;
434 ret
->error
= "Proxy error: Unknown proxy method";
438 /* create the proxy plug to map calls from the actual
439 * socket into our proxy socket layer */
440 pplug
= snew(struct Plug_proxy_tag
);
441 pplug
->fn
= &plug_fn_table
;
442 pplug
->proxy_socket
= ret
;
445 proxy_addr
= sk_namelookup(cfg
->proxy_host
,
446 &proxy_canonical_name
, cfg
->addressfamily
);
447 if (sk_addr_error(proxy_addr
) != NULL
) {
448 ret
->error
= "Proxy error: Unable to resolve proxy host name";
451 sfree(proxy_canonical_name
);
453 /* create the actual socket we will be using,
454 * connected to our proxy server and port.
456 ret
->sub_socket
= sk_new(proxy_addr
, cfg
->proxy_port
,
458 nodelay
, keepalive
, (Plug
) pplug
);
459 if (sk_socket_error(ret
->sub_socket
) != NULL
)
462 /* start the proxy negotiation process... */
463 sk_set_frozen(ret
->sub_socket
, 0);
464 ret
->negotiate(ret
, PROXY_CHANGE_NEW
);
469 /* no proxy, so just return the direct socket */
470 return sk_new(addr
, port
, privport
, oobinline
, nodelay
, keepalive
, plug
);
473 Socket
new_listener(char *srcaddr
, int port
, Plug plug
, int local_host_only
,
474 const Config
*cfg
, int addressfamily
)
476 /* TODO: SOCKS (and potentially others) support inbound
477 * TODO: connections via the proxy. support them.
480 return sk_newlistener(srcaddr
, port
, plug
, local_host_only
, addressfamily
);
483 /* ----------------------------------------------------------------------
484 * HTTP CONNECT proxy type.
487 static int get_line_end (char * data
, int len
)
493 if (data
[off
] == '\n') {
494 /* we have a newline */
497 /* is that the only thing on this line? */
498 if (off
<= 2) return off
;
500 /* if not, then there is the possibility that this header
501 * continues onto the next line, if it starts with a space
506 data
[off
+1] != ' ' &&
507 data
[off
+1] != '\t') return off
;
509 /* the line does continue, so we have to keep going
510 * until we see an the header's "real" end of line.
521 int proxy_http_negotiate (Proxy_Socket p
, int change
)
523 if (p
->state
== PROXY_STATE_NEW
) {
524 /* we are just beginning the proxy negotiate process,
525 * so we'll send off the initial bits of the request.
526 * for this proxy method, it's just a simple HTTP
529 char *buf
, dest
[512];
531 sk_getaddr(p
->remote_addr
, dest
, lenof(dest
));
533 buf
= dupprintf("CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
534 dest
, p
->remote_port
, dest
, p
->remote_port
);
535 sk_write(p
->sub_socket
, buf
, strlen(buf
));
538 if (p
->cfg
.proxy_username
[0] || p
->cfg
.proxy_password
[0]) {
539 char buf
[sizeof(p
->cfg
.proxy_username
)+sizeof(p
->cfg
.proxy_password
)];
540 char buf2
[sizeof(buf
)*4/3 + 100];
542 sprintf(buf
, "%s:%s", p
->cfg
.proxy_username
, p
->cfg
.proxy_password
);
544 sprintf(buf2
, "Proxy-Authorization: Basic ");
545 for (i
= 0, j
= strlen(buf2
); i
< len
; i
+= 3, j
+= 4)
546 base64_encode_atom((unsigned char *)(buf
+i
),
547 (len
-i
> 3 ?
3 : len
-i
), buf2
+j
);
548 strcpy(buf2
+j
, "\r\n");
549 sk_write(p
->sub_socket
, buf2
, strlen(buf2
));
552 sk_write(p
->sub_socket
, "\r\n", 2);
558 if (change
== PROXY_CHANGE_CLOSING
) {
559 /* if our proxy negotiation process involves closing and opening
560 * new sockets, then we would want to intercept this closing
561 * callback when we were expecting it. if we aren't anticipating
562 * a socket close, then some error must have occurred. we'll
563 * just pass those errors up to the backend.
565 return plug_closing(p
->plug
, p
->closing_error_msg
,
566 p
->closing_error_code
,
567 p
->closing_calling_back
);
570 if (change
== PROXY_CHANGE_SENT
) {
571 /* some (or all) of what we wrote to the proxy was sent.
572 * we don't do anything new, however, until we receive the
573 * proxy's response. we might want to set a timer so we can
574 * timeout the proxy negotiation after a while...
579 if (change
== PROXY_CHANGE_ACCEPTING
) {
580 /* we should _never_ see this, as we are using our socket to
581 * connect to a proxy, not accepting inbound connections.
582 * what should we do? close the socket with an appropriate
585 return plug_accepting(p
->plug
, p
->accepting_sock
);
588 if (change
== PROXY_CHANGE_RECEIVE
) {
589 /* we have received data from the underlying socket, which
590 * we'll need to parse, process, and respond to appropriately.
599 int min_ver
, maj_ver
, status
;
601 /* get the status line */
602 len
= bufchain_size(&p
->pending_input_data
);
603 assert(len
> 0); /* or we wouldn't be here */
604 data
= snewn(len
+1, char);
605 bufchain_fetch(&p
->pending_input_data
, data
, len
);
607 * We must NUL-terminate this data, because Windows
608 * sscanf appears to require a NUL at the end of the
609 * string because it strlens it _first_. Sigh.
613 eol
= get_line_end(data
, len
);
620 /* We can't rely on whether the %n incremented the sscanf return */
621 if (sscanf((char *)data
, "HTTP/%i.%i %n",
622 &maj_ver
, &min_ver
, &status
) < 2 || status
== -1) {
623 plug_closing(p
->plug
, "Proxy error: HTTP response was absent",
624 PROXY_ERROR_GENERAL
, 0);
629 /* remove the status line from the input buffer. */
630 bufchain_consume(&p
->pending_input_data
, eol
);
631 if (data
[status
] != '2') {
635 while (eol
> status
&&
636 (data
[eol
-1] == '\r' || data
[eol
-1] == '\n'))
638 buf
= dupprintf("Proxy error: %s", data
+status
);
639 plug_closing(p
->plug
, buf
, PROXY_ERROR_GENERAL
, 0);
652 /* get headers. we're done when we get a
653 * header of length 2, (ie. just "\r\n")
656 len
= bufchain_size(&p
->pending_input_data
);
657 assert(len
> 0); /* or we wouldn't be here */
658 data
= snewn(len
, char);
660 bufchain_fetch(&p
->pending_input_data
, data
, len
);
662 eol
= get_line_end(datap
, len
);
669 bufchain_consume(&p
->pending_input_data
, eol
);
672 eol
= get_line_end(datap
, len
);
677 bufchain_consume(&p
->pending_input_data
, 2);
679 /* proxy activate will have dealt with
680 * whatever is left of the buffer */
690 plug_closing(p
->plug
, "Proxy error: unexpected proxy error",
691 PROXY_ERROR_UNEXPECTED
, 0);
695 /* ----------------------------------------------------------------------
699 /* SOCKS version 4 */
700 int proxy_socks4_negotiate (Proxy_Socket p
, int change
)
702 if (p
->state
== PROXY_CHANGE_NEW
) {
705 * version number (1 byte) = 4
706 * command code (1 byte)
709 * dest. port (2 bytes) [network order]
710 * dest. address (4 bytes)
711 * user ID (variable length, null terminated string)
714 int length
, type
, namelen
;
715 char *command
, addr
[4], hostname
[512];
717 type
= sk_addrtype(p
->remote_addr
);
718 if (type
== ADDRTYPE_IPV6
) {
719 plug_closing(p
->plug
, "Proxy error: SOCKS version 4 does"
720 " not support IPv6", PROXY_ERROR_GENERAL
, 0);
722 } else if (type
== ADDRTYPE_IPV4
) {
724 sk_addrcopy(p
->remote_addr
, addr
);
725 } else { /* type == ADDRTYPE_NAME */
726 assert(type
== ADDRTYPE_NAME
);
727 sk_getaddr(p
->remote_addr
, hostname
, lenof(hostname
));
728 namelen
= strlen(hostname
) + 1; /* include the NUL */
729 addr
[0] = addr
[1] = addr
[2] = 0;
733 length
= strlen(p
->cfg
.proxy_username
) + namelen
+ 9;
734 command
= snewn(length
, char);
735 strcpy(command
+ 8, p
->cfg
.proxy_username
);
737 command
[0] = 4; /* version 4 */
738 command
[1] = 1; /* CONNECT command */
741 command
[2] = (char) (p
->remote_port
>> 8) & 0xff;
742 command
[3] = (char) p
->remote_port
& 0xff;
745 memcpy(command
+ 4, addr
, 4);
748 memcpy(command
+ 8 + strlen(p
->cfg
.proxy_username
) + 1,
751 sk_write(p
->sub_socket
, command
, length
);
758 if (change
== PROXY_CHANGE_CLOSING
) {
759 /* if our proxy negotiation process involves closing and opening
760 * new sockets, then we would want to intercept this closing
761 * callback when we were expecting it. if we aren't anticipating
762 * a socket close, then some error must have occurred. we'll
763 * just pass those errors up to the backend.
765 return plug_closing(p
->plug
, p
->closing_error_msg
,
766 p
->closing_error_code
,
767 p
->closing_calling_back
);
770 if (change
== PROXY_CHANGE_SENT
) {
771 /* some (or all) of what we wrote to the proxy was sent.
772 * we don't do anything new, however, until we receive the
773 * proxy's response. we might want to set a timer so we can
774 * timeout the proxy negotiation after a while...
779 if (change
== PROXY_CHANGE_ACCEPTING
) {
780 /* we should _never_ see this, as we are using our socket to
781 * connect to a proxy, not accepting inbound connections.
782 * what should we do? close the socket with an appropriate
785 return plug_accepting(p
->plug
, p
->accepting_sock
);
788 if (change
== PROXY_CHANGE_RECEIVE
) {
789 /* we have received data from the underlying socket, which
790 * we'll need to parse, process, and respond to appropriately.
795 * version number (1 byte) = 4
796 * reply code (1 byte)
797 * 90 = request granted
798 * 91 = request rejected or failed
799 * 92 = request rejected due to lack of IDENTD on client
800 * 93 = request rejected due to difference in user ID
801 * (what we sent vs. what IDENTD said)
802 * dest. port (2 bytes)
803 * dest. address (4 bytes)
808 if (bufchain_size(&p
->pending_input_data
) < 8)
809 return 1; /* not got anything yet */
811 /* get the response */
812 bufchain_fetch(&p
->pending_input_data
, data
, 8);
815 plug_closing(p
->plug
, "Proxy error: SOCKS proxy responded with "
816 "unexpected reply code version",
817 PROXY_ERROR_GENERAL
, 0);
825 plug_closing(p
->plug
, "Proxy error: SOCKS server wanted IDENTD on client",
826 PROXY_ERROR_GENERAL
, 0);
829 plug_closing(p
->plug
, "Proxy error: Username and IDENTD on client don't agree",
830 PROXY_ERROR_GENERAL
, 0);
834 plug_closing(p
->plug
, "Proxy error: Error while communicating with proxy",
835 PROXY_ERROR_GENERAL
, 0);
841 bufchain_consume(&p
->pending_input_data
, 8);
845 /* proxy activate will have dealt with
846 * whatever is left of the buffer */
851 plug_closing(p
->plug
, "Proxy error: unexpected proxy error",
852 PROXY_ERROR_UNEXPECTED
, 0);
856 /* SOCKS version 5 */
857 int proxy_socks5_negotiate (Proxy_Socket p
, int change
)
859 if (p
->state
== PROXY_CHANGE_NEW
) {
862 * version number (1 byte) = 5
863 * number of available authentication methods (1 byte)
864 * available authentication methods (1 byte * previous value)
865 * authentication methods:
866 * 0x00 = no authentication
868 * 0x02 = username/password
875 command
[0] = 5; /* version 5 */
876 if (p
->cfg
.proxy_username
[0] || p
->cfg
.proxy_password
[0]) {
877 command
[2] = 0x00; /* no authentication */
879 proxy_socks5_offerencryptedauth (command
, &len
);
880 command
[len
++] = 0x02; /* username/password */
881 command
[1] = len
- 2; /* Number of methods supported */
883 command
[1] = 1; /* one methods supported: */
884 command
[2] = 0x00; /* no authentication */
888 sk_write(p
->sub_socket
, command
, len
);
894 if (change
== PROXY_CHANGE_CLOSING
) {
895 /* if our proxy negotiation process involves closing and opening
896 * new sockets, then we would want to intercept this closing
897 * callback when we were expecting it. if we aren't anticipating
898 * a socket close, then some error must have occurred. we'll
899 * just pass those errors up to the backend.
901 return plug_closing(p
->plug
, p
->closing_error_msg
,
902 p
->closing_error_code
,
903 p
->closing_calling_back
);
906 if (change
== PROXY_CHANGE_SENT
) {
907 /* some (or all) of what we wrote to the proxy was sent.
908 * we don't do anything new, however, until we receive the
909 * proxy's response. we might want to set a timer so we can
910 * timeout the proxy negotiation after a while...
915 if (change
== PROXY_CHANGE_ACCEPTING
) {
916 /* we should _never_ see this, as we are using our socket to
917 * connect to a proxy, not accepting inbound connections.
918 * what should we do? close the socket with an appropriate
921 return plug_accepting(p
->plug
, p
->accepting_sock
);
924 if (change
== PROXY_CHANGE_RECEIVE
) {
925 /* we have received data from the underlying socket, which
926 * we'll need to parse, process, and respond to appropriately.
932 * version number (1 byte) = 5
933 * authentication method (1 byte)
934 * authentication methods:
935 * 0x00 = no authentication
937 * 0x02 = username/password
939 * 0xff = no acceptable methods
943 if (bufchain_size(&p
->pending_input_data
) < 2)
944 return 1; /* not got anything yet */
946 /* get the response */
947 bufchain_fetch(&p
->pending_input_data
, data
, 2);
950 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned unexpected version",
951 PROXY_ERROR_GENERAL
, 0);
955 if (data
[1] == 0x00) p
->state
= 2; /* no authentication needed */
956 else if (data
[1] == 0x01) p
->state
= 4; /* GSSAPI authentication */
957 else if (data
[1] == 0x02) p
->state
= 5; /* username/password authentication */
958 else if (data
[1] == 0x03) p
->state
= 6; /* CHAP authentication */
960 plug_closing(p
->plug
, "Proxy error: SOCKS proxy did not accept our authentication",
961 PROXY_ERROR_GENERAL
, 0);
964 bufchain_consume(&p
->pending_input_data
, 2);
969 /* password authentication reply format:
970 * version number (1 bytes) = 1
971 * reply code (1 byte)
977 if (bufchain_size(&p
->pending_input_data
) < 2)
978 return 1; /* not got anything yet */
980 /* get the response */
981 bufchain_fetch(&p
->pending_input_data
, data
, 2);
984 plug_closing(p
->plug
, "Proxy error: SOCKS password "
985 "subnegotiation contained wrong version number",
986 PROXY_ERROR_GENERAL
, 0);
992 plug_closing(p
->plug
, "Proxy error: SOCKS proxy refused"
993 " password authentication",
994 PROXY_ERROR_GENERAL
, 0);
998 bufchain_consume(&p
->pending_input_data
, 2);
999 p
->state
= 2; /* now proceed as authenticated */
1002 if (p
->state
== 8) {
1004 ret
= proxy_socks5_handlechap(p
);
1005 if (ret
) return ret
;
1008 if (p
->state
== 2) {
1011 * version number (1 byte) = 5
1012 * command code (1 byte)
1016 * reserved (1 byte) = 0x00
1017 * address type (1 byte)
1019 * 3 = domainname (first byte has length, no terminating null)
1021 * dest. address (variable)
1022 * dest. port (2 bytes) [network order]
1029 type
= sk_addrtype(p
->remote_addr
);
1030 if (type
== ADDRTYPE_IPV4
) {
1031 len
= 10; /* 4 hdr + 4 addr + 2 trailer */
1032 command
[3] = 1; /* IPv4 */
1033 sk_addrcopy(p
->remote_addr
, command
+4);
1034 } else if (type
== ADDRTYPE_IPV6
) {
1035 len
= 22; /* 4 hdr + 16 addr + 2 trailer */
1036 command
[3] = 4; /* IPv6 */
1037 sk_addrcopy(p
->remote_addr
, command
+4);
1039 assert(type
== ADDRTYPE_NAME
);
1041 sk_getaddr(p
->remote_addr
, command
+5, 256);
1042 command
[4] = strlen(command
+5);
1043 len
= 7 + command
[4]; /* 4 hdr, 1 len, N addr, 2 trailer */
1046 command
[0] = 5; /* version 5 */
1047 command
[1] = 1; /* CONNECT command */
1051 command
[len
-2] = (char) (p
->remote_port
>> 8) & 0xff;
1052 command
[len
-1] = (char) p
->remote_port
& 0xff;
1054 sk_write(p
->sub_socket
, command
, len
);
1060 if (p
->state
== 3) {
1063 * version number (1 bytes) = 5
1064 * reply code (1 byte)
1066 * 1 = general SOCKS server failure
1067 * 2 = connection not allowed by ruleset
1068 * 3 = network unreachable
1069 * 4 = host unreachable
1070 * 5 = connection refused
1072 * 7 = command not supported
1073 * 8 = address type not supported
1074 * reserved (1 byte) = x00
1075 * address type (1 byte)
1077 * 3 = domainname (first byte has length, no terminating null)
1079 * server bound address (variable)
1080 * server bound port (2 bytes) [network order]
1085 /* First 5 bytes of packet are enough to tell its length. */
1086 if (bufchain_size(&p
->pending_input_data
) < 5)
1087 return 1; /* not got anything yet */
1089 /* get the response */
1090 bufchain_fetch(&p
->pending_input_data
, data
, 5);
1093 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned wrong version number",
1094 PROXY_ERROR_GENERAL
, 0);
1101 strcpy(buf
, "Proxy error: ");
1104 case 1: strcat(buf
, "General SOCKS server failure"); break;
1105 case 2: strcat(buf
, "Connection not allowed by ruleset"); break;
1106 case 3: strcat(buf
, "Network unreachable"); break;
1107 case 4: strcat(buf
, "Host unreachable"); break;
1108 case 5: strcat(buf
, "Connection refused"); break;
1109 case 6: strcat(buf
, "TTL expired"); break;
1110 case 7: strcat(buf
, "Command not supported"); break;
1111 case 8: strcat(buf
, "Address type not supported"); break;
1112 default: sprintf(buf
+strlen(buf
),
1113 "Unrecognised SOCKS error code %d",
1117 plug_closing(p
->plug
, buf
, PROXY_ERROR_GENERAL
, 0);
1123 * Eat the rest of the reply packet.
1125 len
= 6; /* first 4 bytes, last 2 */
1127 case 1: len
+= 4; break; /* IPv4 address */
1128 case 4: len
+= 16; break;/* IPv6 address */
1129 case 3: len
+= (unsigned char)data
[4]; break; /* domain name */
1131 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned "
1132 "unrecognised address format",
1133 PROXY_ERROR_GENERAL
, 0);
1136 if (bufchain_size(&p
->pending_input_data
) < len
)
1137 return 1; /* not got whole reply yet */
1138 bufchain_consume(&p
->pending_input_data
, len
);
1145 if (p
->state
== 4) {
1146 /* TODO: Handle GSSAPI authentication */
1147 plug_closing(p
->plug
, "Proxy error: We don't support GSSAPI authentication",
1148 PROXY_ERROR_GENERAL
, 0);
1152 if (p
->state
== 5) {
1153 if (p
->cfg
.proxy_username
[0] || p
->cfg
.proxy_password
[0]) {
1154 char userpwbuf
[514];
1156 ulen
= strlen(p
->cfg
.proxy_username
);
1157 if (ulen
> 255) ulen
= 255; if (ulen
< 1) ulen
= 1;
1158 plen
= strlen(p
->cfg
.proxy_password
);
1159 if (plen
> 255) plen
= 255; if (plen
< 1) plen
= 1;
1160 userpwbuf
[0] = 1; /* version number of subnegotiation */
1161 userpwbuf
[1] = ulen
;
1162 memcpy(userpwbuf
+2, p
->cfg
.proxy_username
, ulen
);
1163 userpwbuf
[ulen
+2] = plen
;
1164 memcpy(userpwbuf
+ulen
+3, p
->cfg
.proxy_password
, plen
);
1165 sk_write(p
->sub_socket
, userpwbuf
, ulen
+ plen
+ 3);
1168 plug_closing(p
->plug
, "Proxy error: Server chose "
1169 "username/password authentication but we "
1171 PROXY_ERROR_GENERAL
, 0);
1175 if (p
->state
== 6) {
1177 ret
= proxy_socks5_selectchap(p
);
1178 if (ret
) return ret
;
1183 plug_closing(p
->plug
, "Proxy error: Unexpected proxy error",
1184 PROXY_ERROR_UNEXPECTED
, 0);
1188 /* ----------------------------------------------------------------------
1189 * `Telnet' proxy type.
1191 * (This is for ad-hoc proxies where you connect to the proxy's
1192 * telnet port and send a command such as `connect host port'. The
1193 * command is configurable, since this proxy type is typically not
1194 * standardised or at all well-defined.)
1197 char *format_telnet_command(SockAddr addr
, int port
, const Config
*cfg
)
1200 int retlen
= 0, retsize
= 0;
1202 #define ENSURE(n) do { \
1203 if (retsize < retlen + n) { \
1204 retsize = retlen + n + 512; \
1205 ret = sresize(ret, retsize, char); \
1209 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1210 * %%, %host, %port, %user, and %pass
1213 while (cfg
->proxy_telnet_command
[eo
] != 0) {
1215 /* scan forward until we hit end-of-line,
1216 * or an escape character (\ or %) */
1217 while (cfg
->proxy_telnet_command
[eo
] != 0 &&
1218 cfg
->proxy_telnet_command
[eo
] != '%' &&
1219 cfg
->proxy_telnet_command
[eo
] != '\\') eo
++;
1221 /* if we hit eol, break out of our escaping loop */
1222 if (cfg
->proxy_telnet_command
[eo
] == 0) break;
1224 /* if there was any unescaped text before the escape
1225 * character, send that now */
1228 memcpy(ret
+ retlen
, cfg
->proxy_telnet_command
+ so
, eo
- so
);
1234 /* if the escape character was the last character of
1235 * the line, we'll just stop and send it. */
1236 if (cfg
->proxy_telnet_command
[eo
] == 0) break;
1238 if (cfg
->proxy_telnet_command
[so
] == '\\') {
1240 /* we recognize \\, \%, \r, \n, \t, \x??.
1241 * anything else, we just send unescaped (including the \).
1244 switch (cfg
->proxy_telnet_command
[eo
]) {
1248 ret
[retlen
++] = '\\';
1254 ret
[retlen
++] = '%';
1260 ret
[retlen
++] = '\r';
1266 ret
[retlen
++] = '\n';
1272 ret
[retlen
++] = '\t';
1279 /* escaped hexadecimal value (ie. \xff) */
1280 unsigned char v
= 0;
1285 if (cfg
->proxy_telnet_command
[eo
] >= '0' &&
1286 cfg
->proxy_telnet_command
[eo
] <= '9')
1287 v
+= cfg
->proxy_telnet_command
[eo
] - '0';
1288 else if (cfg
->proxy_telnet_command
[eo
] >= 'a' &&
1289 cfg
->proxy_telnet_command
[eo
] <= 'f')
1290 v
+= cfg
->proxy_telnet_command
[eo
] - 'a' + 10;
1291 else if (cfg
->proxy_telnet_command
[eo
] >= 'A' &&
1292 cfg
->proxy_telnet_command
[eo
] <= 'F')
1293 v
+= cfg
->proxy_telnet_command
[eo
] - 'A' + 10;
1295 /* non hex character, so we abort and just
1296 * send the whole thing unescaped (including \x)
1299 ret
[retlen
++] = '\\';
1304 /* we only extract two hex characters */
1320 memcpy(ret
+retlen
, cfg
->proxy_telnet_command
+ so
, 2);
1327 /* % escape. we recognize %%, %host, %port, %user, %pass.
1328 * %proxyhost, %proxyport. Anything else we just send
1329 * unescaped (including the %).
1332 if (cfg
->proxy_telnet_command
[eo
] == '%') {
1334 ret
[retlen
++] = '%';
1337 else if (strnicmp(cfg
->proxy_telnet_command
+ eo
,
1341 sk_getaddr(addr
, dest
, lenof(dest
));
1342 destlen
= strlen(dest
);
1344 memcpy(ret
+retlen
, dest
, destlen
);
1348 else if (strnicmp(cfg
->proxy_telnet_command
+ eo
,
1350 char portstr
[8], portlen
;
1351 portlen
= sprintf(portstr
, "%i", port
);
1353 memcpy(ret
+ retlen
, portstr
, portlen
);
1357 else if (strnicmp(cfg
->proxy_telnet_command
+ eo
,
1359 int userlen
= strlen(cfg
->proxy_username
);
1361 memcpy(ret
+retlen
, cfg
->proxy_username
, userlen
);
1365 else if (strnicmp(cfg
->proxy_telnet_command
+ eo
,
1367 int passlen
= strlen(cfg
->proxy_password
);
1369 memcpy(ret
+retlen
, cfg
->proxy_password
, passlen
);
1373 else if (strnicmp(cfg
->proxy_telnet_command
+ eo
,
1374 "proxyhost", 4) == 0) {
1375 int phlen
= strlen(cfg
->proxy_host
);
1377 memcpy(ret
+retlen
, cfg
->proxy_host
, phlen
);
1381 else if (strnicmp(cfg
->proxy_telnet_command
+ eo
,
1382 "proxyport", 4) == 0) {
1385 sprintf(pport
, "%d", cfg
->proxy_port
);
1386 pplen
= strlen(cfg
->proxy_host
);
1388 memcpy(ret
+retlen
, pport
, pplen
);
1393 /* we don't escape this, so send the % now, and
1394 * don't advance eo, so that we'll consider the
1395 * text immediately following the % as unescaped.
1398 ret
[retlen
++] = '%';
1402 /* resume scanning for additional escapes after this one. */
1406 /* if there is any unescaped text at the end of the line, send it */
1409 memcpy(ret
+ retlen
, cfg
->proxy_telnet_command
+ so
, eo
- so
);
1420 int proxy_telnet_negotiate (Proxy_Socket p
, int change
)
1422 if (p
->state
== PROXY_CHANGE_NEW
) {
1423 char *formatted_cmd
;
1425 formatted_cmd
= format_telnet_command(p
->remote_addr
, p
->remote_port
,
1428 sk_write(p
->sub_socket
, formatted_cmd
, strlen(formatted_cmd
));
1429 sfree(formatted_cmd
);
1435 if (change
== PROXY_CHANGE_CLOSING
) {
1436 /* if our proxy negotiation process involves closing and opening
1437 * new sockets, then we would want to intercept this closing
1438 * callback when we were expecting it. if we aren't anticipating
1439 * a socket close, then some error must have occurred. we'll
1440 * just pass those errors up to the backend.
1442 return plug_closing(p
->plug
, p
->closing_error_msg
,
1443 p
->closing_error_code
,
1444 p
->closing_calling_back
);
1447 if (change
== PROXY_CHANGE_SENT
) {
1448 /* some (or all) of what we wrote to the proxy was sent.
1449 * we don't do anything new, however, until we receive the
1450 * proxy's response. we might want to set a timer so we can
1451 * timeout the proxy negotiation after a while...
1456 if (change
== PROXY_CHANGE_ACCEPTING
) {
1457 /* we should _never_ see this, as we are using our socket to
1458 * connect to a proxy, not accepting inbound connections.
1459 * what should we do? close the socket with an appropriate
1462 return plug_accepting(p
->plug
, p
->accepting_sock
);
1465 if (change
== PROXY_CHANGE_RECEIVE
) {
1466 /* we have received data from the underlying socket, which
1467 * we'll need to parse, process, and respond to appropriately.
1472 /* proxy activate will have dealt with
1473 * whatever is left of the buffer */
1477 plug_closing(p
->plug
, "Proxy error: Unexpected proxy error",
1478 PROXY_ERROR_UNEXPECTED
, 0);