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
18 * Call this when proxy negotiation is complete, so that this
19 * socket can begin working normally.
21 void proxy_activate (Proxy_Socket p
)
25 long output_before
, output_after
;
27 p
->state
= PROXY_STATE_ACTIVE
;
29 /* we want to ignore new receive events until we have sent
30 * all of our buffered receive data.
32 sk_set_frozen(p
->sub_socket
, 1);
34 /* how many bytes of output have we buffered? */
35 output_before
= bufchain_size(&p
->pending_oob_output_data
) +
36 bufchain_size(&p
->pending_output_data
);
37 /* and keep track of how many bytes do not get sent. */
40 /* send buffered OOB writes */
41 while (bufchain_size(&p
->pending_oob_output_data
) > 0) {
42 bufchain_prefix(&p
->pending_oob_output_data
, &data
, &len
);
43 output_after
+= sk_write_oob(p
->sub_socket
, data
, len
);
44 bufchain_consume(&p
->pending_oob_output_data
, len
);
47 /* send buffered normal writes */
48 while (bufchain_size(&p
->pending_output_data
) > 0) {
49 bufchain_prefix(&p
->pending_output_data
, &data
, &len
);
50 output_after
+= sk_write(p
->sub_socket
, data
, len
);
51 bufchain_consume(&p
->pending_output_data
, len
);
54 /* if we managed to send any data, let the higher levels know. */
55 if (output_after
< output_before
)
56 plug_sent(p
->plug
, output_after
);
58 /* if we were asked to flush the output during
59 * the proxy negotiation process, do so now.
61 if (p
->pending_flush
) sk_flush(p
->sub_socket
);
63 /* if the backend wanted the socket unfrozen, try to unfreeze.
64 * our set_frozen handler will flush buffered receive data before
65 * unfreezing the actual underlying socket.
68 sk_set_frozen((Socket
)p
, 0);
71 /* basic proxy socket functions */
73 static Plug
sk_proxy_plug (Socket s
, Plug p
)
75 Proxy_Socket ps
= (Proxy_Socket
) s
;
82 static void sk_proxy_close (Socket s
)
84 Proxy_Socket ps
= (Proxy_Socket
) s
;
86 sk_close(ps
->sub_socket
);
90 static int sk_proxy_write (Socket s
, char *data
, int len
)
92 Proxy_Socket ps
= (Proxy_Socket
) s
;
94 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
95 bufchain_add(&ps
->pending_output_data
, data
, len
);
96 return bufchain_size(&ps
->pending_output_data
);
98 return sk_write(ps
->sub_socket
, data
, len
);
101 static int sk_proxy_write_oob (Socket s
, char *data
, int len
)
103 Proxy_Socket ps
= (Proxy_Socket
) s
;
105 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
106 bufchain_clear(&ps
->pending_output_data
);
107 bufchain_clear(&ps
->pending_oob_output_data
);
108 bufchain_add(&ps
->pending_oob_output_data
, data
, len
);
111 return sk_write_oob(ps
->sub_socket
, data
, len
);
114 static void sk_proxy_flush (Socket s
)
116 Proxy_Socket ps
= (Proxy_Socket
) s
;
118 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
119 ps
->pending_flush
= 1;
122 sk_flush(ps
->sub_socket
);
125 static void sk_proxy_set_private_ptr (Socket s
, void *ptr
)
127 Proxy_Socket ps
= (Proxy_Socket
) s
;
128 sk_set_private_ptr(ps
->sub_socket
, ptr
);
131 static void * sk_proxy_get_private_ptr (Socket s
)
133 Proxy_Socket ps
= (Proxy_Socket
) s
;
134 return sk_get_private_ptr(ps
->sub_socket
);
137 static void sk_proxy_set_frozen (Socket s
, int is_frozen
)
139 Proxy_Socket ps
= (Proxy_Socket
) s
;
141 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
142 ps
->freeze
= is_frozen
;
146 /* handle any remaining buffered recv data first */
147 if (bufchain_size(&ps
->pending_input_data
) > 0) {
148 ps
->freeze
= is_frozen
;
150 /* loop while we still have buffered data, and while we are
151 * unfrozen. the plug_receive call in the loop could result
152 * in a call back into this function refreezing the socket,
153 * so we have to check each time.
155 while (!ps
->freeze
&& bufchain_size(&ps
->pending_input_data
) > 0) {
158 bufchain_prefix(&ps
->pending_input_data
, &data
, &len
);
159 plug_receive(ps
->plug
, 0, data
, len
);
160 bufchain_consume(&ps
->pending_input_data
, len
);
163 /* if we're still frozen, we'll have to wait for another
164 * call from the backend to finish unbuffering the data.
166 if (ps
->freeze
) return;
169 sk_set_frozen(ps
->sub_socket
, is_frozen
);
172 static char * sk_proxy_socket_error (Socket s
)
174 Proxy_Socket ps
= (Proxy_Socket
) s
;
175 if (ps
->error
!= NULL
|| ps
->sub_socket
== NULL
) {
178 return sk_socket_error(ps
->sub_socket
);
181 /* basic proxy plug functions */
183 static int plug_proxy_closing (Plug p
, char *error_msg
,
184 int error_code
, int calling_back
)
186 Proxy_Plug pp
= (Proxy_Plug
) p
;
187 Proxy_Socket ps
= pp
->proxy_socket
;
189 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
190 ps
->closing_error_msg
= error_msg
;
191 ps
->closing_error_code
= error_code
;
192 ps
->closing_calling_back
= calling_back
;
193 return ps
->negotiate(ps
, PROXY_CHANGE_CLOSING
);
195 return plug_closing(ps
->plug
, error_msg
,
196 error_code
, calling_back
);
199 static int plug_proxy_receive (Plug p
, int urgent
, char *data
, int len
)
201 Proxy_Plug pp
= (Proxy_Plug
) p
;
202 Proxy_Socket ps
= pp
->proxy_socket
;
204 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
205 /* we will lose the urgentness of this data, but since most,
206 * if not all, of this data will be consumed by the negotiation
207 * process, hopefully it won't affect the protocol above us
209 bufchain_add(&ps
->pending_input_data
, data
, len
);
210 ps
->receive_urgent
= urgent
;
211 ps
->receive_data
= data
;
212 ps
->receive_len
= len
;
213 return ps
->negotiate(ps
, PROXY_CHANGE_RECEIVE
);
215 return plug_receive(ps
->plug
, urgent
, data
, len
);
218 static void plug_proxy_sent (Plug p
, int bufsize
)
220 Proxy_Plug pp
= (Proxy_Plug
) p
;
221 Proxy_Socket ps
= pp
->proxy_socket
;
223 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
224 ps
->sent_bufsize
= bufsize
;
225 ps
->negotiate(ps
, PROXY_CHANGE_SENT
);
228 plug_sent(ps
->plug
, bufsize
);
231 static int plug_proxy_accepting (Plug p
, void *sock
)
233 Proxy_Plug pp
= (Proxy_Plug
) p
;
234 Proxy_Socket ps
= pp
->proxy_socket
;
236 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
237 ps
->accepting_sock
= sock
;
238 return ps
->negotiate(ps
, PROXY_CHANGE_ACCEPTING
);
240 return plug_accepting(ps
->plug
, sock
);
243 static int proxy_for_destination (SockAddr addr
, char * hostname
, int port
)
247 int hostip_len
, hostname_len
;
251 * Check the host name and IP against the hard-coded
252 * representations of `localhost'.
254 if (!cfg
.even_proxy_localhost
&&
255 (sk_hostname_is_local(hostname
) || sk_address_is_local(addr
)))
256 return 0; /* do not proxy */
258 /* we want a string representation of the IP address for comparisons */
259 sk_getaddr(addr
, hostip
, 64);
261 hostip_len
= strlen(hostip
);
262 hostname_len
= strlen(hostname
);
264 exclude_list
= cfg
.proxy_exclude_list
;
266 /* now parse the exclude list, and see if either our IP
267 * or hostname matches anything in it.
270 while (exclude_list
[s
]) {
271 while (exclude_list
[s
] &&
272 (isspace(exclude_list
[s
]) ||
273 exclude_list
[s
] == ',')) s
++;
275 if (!exclude_list
[s
]) break;
279 while (exclude_list
[e
] &&
280 (isalnum(exclude_list
[e
]) ||
281 exclude_list
[e
] == '-' ||
282 exclude_list
[e
] == '.' ||
283 exclude_list
[e
] == '*')) e
++;
285 if (exclude_list
[s
] == '*') {
286 /* wildcard at beginning of entry */
288 if (strnicmp(hostip
+ hostip_len
- (e
- s
- 1),
289 exclude_list
+ s
+ 1, e
- s
- 1) == 0 ||
290 strnicmp(hostname
+ hostname_len
- (e
- s
- 1),
291 exclude_list
+ s
+ 1, e
- s
- 1) == 0)
292 return 0; /* IP/hostname range excluded. do not use proxy. */
294 } else if (exclude_list
[e
-1] == '*') {
295 /* wildcard at end of entry */
297 if (strnicmp(hostip
, exclude_list
+ s
, e
- s
- 1) == 0 ||
298 strnicmp(hostname
, exclude_list
+ s
, e
- s
- 1) == 0)
299 return 0; /* IP/hostname range excluded. do not use proxy. */
302 /* no wildcard at either end, so let's try an absolute
303 * match (ie. a specific IP)
306 if (stricmp(hostip
, exclude_list
+ s
) == 0)
307 return 0; /* IP/hostname excluded. do not use proxy. */
308 if (stricmp(hostname
, exclude_list
+ s
) == 0)
309 return 0; /* IP/hostname excluded. do not use proxy. */
315 /* no matches in the exclude list, so use the proxy */
319 Socket
new_connection(SockAddr addr
, char *hostname
,
320 int port
, int privport
,
321 int oobinline
, int nodelay
, Plug plug
)
323 static const struct socket_function_table socket_fn_table
= {
329 sk_proxy_set_private_ptr
,
330 sk_proxy_get_private_ptr
,
332 sk_proxy_socket_error
335 static const struct plug_function_table plug_fn_table
= {
342 if (cfg
.proxy_type
!= PROXY_NONE
&&
343 proxy_for_destination(addr
, hostname
, port
))
348 char * proxy_canonical_name
;
350 ret
= smalloc(sizeof(struct Socket_proxy_tag
));
351 ret
->fn
= &socket_fn_table
;
353 ret
->remote_addr
= addr
;
354 ret
->remote_port
= port
;
357 ret
->pending_flush
= 0;
360 bufchain_init(&ret
->pending_input_data
);
361 bufchain_init(&ret
->pending_output_data
);
362 bufchain_init(&ret
->pending_oob_output_data
);
364 ret
->sub_socket
= NULL
;
365 ret
->state
= PROXY_STATE_NEW
;
366 ret
->negotiate
= NULL
;
368 if (cfg
.proxy_type
== PROXY_HTTP
) {
369 ret
->negotiate
= proxy_http_negotiate
;
370 } else if (cfg
.proxy_type
== PROXY_SOCKS
) {
371 if (cfg
.proxy_socks_version
== 4)
372 ret
->negotiate
= proxy_socks4_negotiate
;
374 ret
->negotiate
= proxy_socks5_negotiate
;
375 } else if (cfg
.proxy_type
== PROXY_TELNET
) {
376 ret
->negotiate
= proxy_telnet_negotiate
;
378 ret
->error
= "Proxy error: Unknown proxy method";
382 /* create the proxy plug to map calls from the actual
383 * socket into our proxy socket layer */
384 pplug
= smalloc(sizeof(struct Plug_proxy_tag
));
385 pplug
->fn
= &plug_fn_table
;
386 pplug
->proxy_socket
= ret
;
389 proxy_addr
= sk_namelookup(cfg
.proxy_host
,
390 &proxy_canonical_name
);
391 sfree(proxy_canonical_name
);
393 /* create the actual socket we will be using,
394 * connected to our proxy server and port.
396 ret
->sub_socket
= sk_new(proxy_addr
, cfg
.proxy_port
,
398 nodelay
, (Plug
) pplug
);
399 if (sk_socket_error(ret
->sub_socket
) != NULL
)
402 sk_addr_free(proxy_addr
);
404 /* start the proxy negotiation process... */
405 sk_set_frozen(ret
->sub_socket
, 0);
406 ret
->negotiate(ret
, PROXY_CHANGE_NEW
);
411 /* no proxy, so just return the direct socket */
412 return sk_new(addr
, port
, privport
, oobinline
, nodelay
, plug
);
415 Socket
new_listener(char *srcaddr
, int port
, Plug plug
, int local_host_only
)
417 /* TODO: SOCKS (and potentially others) support inbound
418 * TODO: connections via the proxy. support them.
421 return sk_newlistener(srcaddr
, port
, plug
, local_host_only
);
424 /* ----------------------------------------------------------------------
425 * HTTP CONNECT proxy type.
428 static int get_line_end (char * data
, int len
)
434 if (data
[off
] == '\n') {
435 /* we have a newline */
438 /* is that the only thing on this line? */
439 if (off
<= 2) return off
;
441 /* if not, then there is the possibility that this header
442 * continues onto the next line, if it starts with a space
447 data
[off
+1] != ' ' &&
448 data
[off
+1] != '\t') return off
;
450 /* the line does continue, so we have to keep going
451 * until we see an the header's "real" end of line.
462 int proxy_http_negotiate (Proxy_Socket p
, int change
)
464 if (p
->state
== PROXY_STATE_NEW
) {
465 /* we are just beginning the proxy negotiate process,
466 * so we'll send off the initial bits of the request.
467 * for this proxy method, it's just a simple HTTP
472 sk_getaddr(p
->remote_addr
, dest
, 64);
474 buf
= dupprintf("CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
475 dest
, p
->remote_port
, dest
, p
->remote_port
);
476 sk_write(p
->sub_socket
, buf
, strlen(buf
));
479 if (cfg
.proxy_username
[0] || cfg
.proxy_password
[0]) {
480 char buf
[sizeof(cfg
.proxy_username
)+sizeof(cfg
.proxy_password
)];
481 char buf2
[sizeof(buf
)*4/3 + 100];
483 sprintf(buf
, "%s:%s", cfg
.proxy_username
, cfg
.proxy_password
);
485 sprintf(buf2
, "Proxy-Authorization: basic ");
486 for (i
= 0, j
= strlen(buf2
); i
< len
; i
+= 3, j
+= 4)
487 base64_encode_atom(buf
+i
, (len
-i
> 3 ?
3 : len
-i
), buf2
+j
);
488 strcpy(buf2
+j
, "\r\n");
489 sk_write(p
->sub_socket
, buf2
, strlen(buf2
));
492 sk_write(p
->sub_socket
, "\r\n", 2);
498 if (change
== PROXY_CHANGE_CLOSING
) {
499 /* if our proxy negotiation process involves closing and opening
500 * new sockets, then we would want to intercept this closing
501 * callback when we were expecting it. if we aren't anticipating
502 * a socket close, then some error must have occurred. we'll
503 * just pass those errors up to the backend.
505 return plug_closing(p
->plug
, p
->closing_error_msg
,
506 p
->closing_error_code
,
507 p
->closing_calling_back
);
510 if (change
== PROXY_CHANGE_SENT
) {
511 /* some (or all) of what we wrote to the proxy was sent.
512 * we don't do anything new, however, until we receive the
513 * proxy's response. we might want to set a timer so we can
514 * timeout the proxy negotiation after a while...
519 if (change
== PROXY_CHANGE_ACCEPTING
) {
520 /* we should _never_ see this, as we are using our socket to
521 * connect to a proxy, not accepting inbound connections.
522 * what should we do? close the socket with an appropriate
525 return plug_accepting(p
->plug
, p
->accepting_sock
);
528 if (change
== PROXY_CHANGE_RECEIVE
) {
529 /* we have received data from the underlying socket, which
530 * we'll need to parse, process, and respond to appropriately.
539 int min_ver
, maj_ver
, status
;
541 /* get the status line */
542 len
= bufchain_size(&p
->pending_input_data
);
543 assert(len
> 0); /* or we wouldn't be here */
545 bufchain_fetch(&p
->pending_input_data
, data
, len
);
547 eol
= get_line_end(data
, len
);
554 /* We can't rely on whether the %n incremented the sscanf return */
555 if (sscanf((char *)data
, "HTTP/%i.%i %n",
556 &maj_ver
, &min_ver
, &status
) < 2 || status
== -1) {
557 plug_closing(p
->plug
, "Proxy error: HTTP response was absent",
558 PROXY_ERROR_GENERAL
, 0);
563 /* remove the status line from the input buffer. */
564 bufchain_consume(&p
->pending_input_data
, eol
);
565 if (data
[status
] != '2') {
569 while (eol
> status
&&
570 (data
[eol
-1] == '\r' || data
[eol
-1] == '\n'))
572 buf
= dupprintf("Proxy error: %s", data
+status
);
573 plug_closing(p
->plug
, buf
, PROXY_ERROR_GENERAL
, 0);
586 /* get headers. we're done when we get a
587 * header of length 2, (ie. just "\r\n")
590 len
= bufchain_size(&p
->pending_input_data
);
591 assert(len
> 0); /* or we wouldn't be here */
594 bufchain_fetch(&p
->pending_input_data
, data
, len
);
596 eol
= get_line_end(datap
, len
);
603 bufchain_consume(&p
->pending_input_data
, eol
);
606 eol
= get_line_end(datap
, len
);
611 bufchain_consume(&p
->pending_input_data
, 2);
613 /* proxy activate will have dealt with
614 * whatever is left of the buffer */
624 plug_closing(p
->plug
, "Proxy error: unexpected proxy error",
625 PROXY_ERROR_UNEXPECTED
, 0);
629 /* ----------------------------------------------------------------------
633 /* SOCKS version 4 */
634 int proxy_socks4_negotiate (Proxy_Socket p
, int change
)
636 if (p
->state
== PROXY_CHANGE_NEW
) {
639 * version number (1 byte) = 4
640 * command code (1 byte)
643 * dest. port (2 bytes) [network order]
644 * dest. address (4 bytes)
645 * user ID (variable length, null terminated string)
651 if (sk_addrtype(p
->remote_addr
) != ADDRTYPE_IPV4
) {
652 plug_closing(p
->plug
, "Proxy error: SOCKS version 4 does"
653 " not support IPv6", PROXY_ERROR_GENERAL
, 0);
657 length
= strlen(cfg
.proxy_username
) + 9;
658 command
= (char*) smalloc(length
);
659 strcpy(command
+ 8, cfg
.proxy_username
);
661 command
[0] = 4; /* version 4 */
662 command
[1] = 1; /* CONNECT command */
665 command
[2] = (char) (p
->remote_port
>> 8) & 0xff;
666 command
[3] = (char) p
->remote_port
& 0xff;
669 sk_addrcopy(p
->remote_addr
, command
+ 4);
671 sk_write(p
->sub_socket
, command
, length
);
678 if (change
== PROXY_CHANGE_CLOSING
) {
679 /* if our proxy negotiation process involves closing and opening
680 * new sockets, then we would want to intercept this closing
681 * callback when we were expecting it. if we aren't anticipating
682 * a socket close, then some error must have occurred. we'll
683 * just pass those errors up to the backend.
685 return plug_closing(p
->plug
, p
->closing_error_msg
,
686 p
->closing_error_code
,
687 p
->closing_calling_back
);
690 if (change
== PROXY_CHANGE_SENT
) {
691 /* some (or all) of what we wrote to the proxy was sent.
692 * we don't do anything new, however, until we receive the
693 * proxy's response. we might want to set a timer so we can
694 * timeout the proxy negotiation after a while...
699 if (change
== PROXY_CHANGE_ACCEPTING
) {
700 /* we should _never_ see this, as we are using our socket to
701 * connect to a proxy, not accepting inbound connections.
702 * what should we do? close the socket with an appropriate
705 return plug_accepting(p
->plug
, p
->accepting_sock
);
708 if (change
== PROXY_CHANGE_RECEIVE
) {
709 /* we have received data from the underlying socket, which
710 * we'll need to parse, process, and respond to appropriately.
715 * version number (1 byte) = 4
716 * reply code (1 byte)
717 * 90 = request granted
718 * 91 = request rejected or failed
719 * 92 = request rejected due to lack of IDENTD on client
720 * 93 = request rejected due to difference in user ID
721 * (what we sent vs. what IDENTD said)
722 * dest. port (2 bytes)
723 * dest. address (4 bytes)
728 if (bufchain_size(&p
->pending_input_data
) < 8)
729 return 1; /* not got anything yet */
731 /* get the response */
732 bufchain_fetch(&p
->pending_input_data
, data
, 8);
735 plug_closing(p
->plug
, "Proxy error: SOCKS proxy responded with "
736 "unexpected reply code version",
737 PROXY_ERROR_GENERAL
, 0);
745 plug_closing(p
->plug
, "Proxy error: SOCKS server wanted IDENTD on client",
746 PROXY_ERROR_GENERAL
, 0);
749 plug_closing(p
->plug
, "Proxy error: Username and IDENTD on client don't agree",
750 PROXY_ERROR_GENERAL
, 0);
754 plug_closing(p
->plug
, "Proxy error: Error while communicating with proxy",
755 PROXY_ERROR_GENERAL
, 0);
761 bufchain_consume(&p
->pending_input_data
, 8);
765 /* proxy activate will have dealt with
766 * whatever is left of the buffer */
771 plug_closing(p
->plug
, "Proxy error: unexpected proxy error",
772 PROXY_ERROR_UNEXPECTED
, 0);
776 /* SOCKS version 5 */
777 int proxy_socks5_negotiate (Proxy_Socket p
, int change
)
779 if (p
->state
== PROXY_CHANGE_NEW
) {
782 * version number (1 byte) = 5
783 * number of available authentication methods (1 byte)
784 * available authentication methods (1 byte * previous value)
785 * authentication methods:
786 * 0x00 = no authentication
788 * 0x02 = username/password
795 command
[0] = 5; /* version 5 */
796 if (cfg
.proxy_username
[0] || cfg
.proxy_password
[0]) {
797 command
[1] = 2; /* two methods supported: */
798 command
[2] = 0x00; /* no authentication */
799 command
[3] = 0x02; /* username/password */
802 command
[1] = 1; /* one methods supported: */
803 command
[2] = 0x00; /* no authentication */
807 sk_write(p
->sub_socket
, command
, len
);
813 if (change
== PROXY_CHANGE_CLOSING
) {
814 /* if our proxy negotiation process involves closing and opening
815 * new sockets, then we would want to intercept this closing
816 * callback when we were expecting it. if we aren't anticipating
817 * a socket close, then some error must have occurred. we'll
818 * just pass those errors up to the backend.
820 return plug_closing(p
->plug
, p
->closing_error_msg
,
821 p
->closing_error_code
,
822 p
->closing_calling_back
);
825 if (change
== PROXY_CHANGE_SENT
) {
826 /* some (or all) of what we wrote to the proxy was sent.
827 * we don't do anything new, however, until we receive the
828 * proxy's response. we might want to set a timer so we can
829 * timeout the proxy negotiation after a while...
834 if (change
== PROXY_CHANGE_ACCEPTING
) {
835 /* we should _never_ see this, as we are using our socket to
836 * connect to a proxy, not accepting inbound connections.
837 * what should we do? close the socket with an appropriate
840 return plug_accepting(p
->plug
, p
->accepting_sock
);
843 if (change
== PROXY_CHANGE_RECEIVE
) {
844 /* we have received data from the underlying socket, which
845 * we'll need to parse, process, and respond to appropriately.
851 * version number (1 byte) = 5
852 * authentication method (1 byte)
853 * authentication methods:
854 * 0x00 = no authentication
856 * 0x02 = username/password
858 * 0xff = no acceptable methods
862 if (bufchain_size(&p
->pending_input_data
) < 2)
863 return 1; /* not got anything yet */
865 /* get the response */
866 bufchain_fetch(&p
->pending_input_data
, data
, 2);
869 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned unexpected version",
870 PROXY_ERROR_GENERAL
, 0);
874 if (data
[1] == 0x00) p
->state
= 2; /* no authentication needed */
875 else if (data
[1] == 0x01) p
->state
= 4; /* GSSAPI authentication */
876 else if (data
[1] == 0x02) p
->state
= 5; /* username/password authentication */
877 else if (data
[1] == 0x03) p
->state
= 6; /* CHAP authentication */
879 plug_closing(p
->plug
, "Proxy error: SOCKS proxy did not accept our authentication",
880 PROXY_ERROR_GENERAL
, 0);
883 bufchain_consume(&p
->pending_input_data
, 2);
888 /* password authentication reply format:
889 * version number (1 bytes) = 1
890 * reply code (1 byte)
896 if (bufchain_size(&p
->pending_input_data
) < 2)
897 return 1; /* not got anything yet */
899 /* get the response */
900 bufchain_fetch(&p
->pending_input_data
, data
, 2);
903 plug_closing(p
->plug
, "Proxy error: SOCKS password "
904 "subnegotiation contained wrong version number",
905 PROXY_ERROR_GENERAL
, 0);
911 plug_closing(p
->plug
, "Proxy error: SOCKS proxy refused"
912 " password authentication",
913 PROXY_ERROR_GENERAL
, 0);
917 bufchain_consume(&p
->pending_input_data
, 2);
918 p
->state
= 2; /* now proceed as authenticated */
924 * version number (1 byte) = 5
925 * command code (1 byte)
929 * reserved (1 byte) = 0x00
930 * address type (1 byte)
932 * 3 = domainname (first byte has length, no terminating null)
934 * dest. address (variable)
935 * dest. port (2 bytes) [network order]
941 if (sk_addrtype(p
->remote_addr
) == ADDRTYPE_IPV4
) {
943 command
[3] = 1; /* IPv4 */
946 command
[3] = 4; /* IPv6 */
949 command
[0] = 5; /* version 5 */
950 command
[1] = 1; /* CONNECT command */
954 sk_addrcopy(p
->remote_addr
, command
+4);
957 command
[len
-2] = (char) (p
->remote_port
>> 8) & 0xff;
958 command
[len
-1] = (char) p
->remote_port
& 0xff;
960 sk_write(p
->sub_socket
, command
, len
);
969 * version number (1 bytes) = 5
970 * reply code (1 byte)
972 * 1 = general SOCKS server failure
973 * 2 = connection not allowed by ruleset
974 * 3 = network unreachable
975 * 4 = host unreachable
976 * 5 = connection refused
978 * 7 = command not supported
979 * 8 = address type not supported
980 * reserved (1 byte) = x00
981 * address type (1 byte)
983 * 3 = domainname (first byte has length, no terminating null)
985 * server bound address (variable)
986 * server bound port (2 bytes) [network order]
991 /* First 5 bytes of packet are enough to tell its length. */
992 if (bufchain_size(&p
->pending_input_data
) < 5)
993 return 1; /* not got anything yet */
995 /* get the response */
996 bufchain_fetch(&p
->pending_input_data
, data
, 5);
999 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned wrong version number",
1000 PROXY_ERROR_GENERAL
, 0);
1007 strcpy(buf
, "Proxy error: ");
1010 case 1: strcat(buf
, "General SOCKS server failure"); break;
1011 case 2: strcat(buf
, "Connection not allowed by ruleset"); break;
1012 case 3: strcat(buf
, "Network unreachable"); break;
1013 case 4: strcat(buf
, "Host unreachable"); break;
1014 case 5: strcat(buf
, "Connection refused"); break;
1015 case 6: strcat(buf
, "TTL expired"); break;
1016 case 7: strcat(buf
, "Command not supported"); break;
1017 case 8: strcat(buf
, "Address type not supported"); break;
1018 default: sprintf(buf
+strlen(buf
),
1019 "Unrecognised SOCKS error code %d",
1023 plug_closing(p
->plug
, buf
, PROXY_ERROR_GENERAL
, 0);
1029 * Eat the rest of the reply packet.
1031 len
= 6; /* first 4 bytes, last 2 */
1033 case 1: len
+= 4; break; /* IPv4 address */
1034 case 4: len
+= 16; break;/* IPv6 address */
1035 case 3: len
+= (unsigned char)data
[4]; break; /* domain name */
1037 plug_closing(p
->plug
, "Proxy error: SOCKS proxy returned "
1038 "unrecognised address format",
1039 PROXY_ERROR_GENERAL
, 0);
1042 if (bufchain_size(&p
->pending_input_data
) < len
)
1043 return 1; /* not got whole reply yet */
1044 bufchain_consume(&p
->pending_input_data
, len
);
1051 if (p
->state
== 4) {
1052 /* TODO: Handle GSSAPI authentication */
1053 plug_closing(p
->plug
, "Proxy error: We don't support GSSAPI authentication",
1054 PROXY_ERROR_GENERAL
, 0);
1058 if (p
->state
== 5) {
1059 if (cfg
.proxy_username
[0] || cfg
.proxy_password
[0]) {
1060 char userpwbuf
[514];
1062 ulen
= strlen(cfg
.proxy_username
);
1063 if (ulen
> 255) ulen
= 255; if (ulen
< 1) ulen
= 1;
1064 plen
= strlen(cfg
.proxy_password
);
1065 if (plen
> 255) plen
= 255; if (plen
< 1) plen
= 1;
1066 userpwbuf
[0] = 1; /* version number of subnegotiation */
1067 userpwbuf
[1] = ulen
;
1068 memcpy(userpwbuf
+2, cfg
.proxy_username
, ulen
);
1069 userpwbuf
[ulen
+2] = plen
;
1070 memcpy(userpwbuf
+ulen
+3, cfg
.proxy_password
, plen
);
1071 sk_write(p
->sub_socket
, userpwbuf
, ulen
+ plen
+ 3);
1074 plug_closing(p
->plug
, "Proxy error: Server chose "
1075 "username/password authentication but we "
1077 PROXY_ERROR_GENERAL
, 0);
1081 if (p
->state
== 6) {
1082 /* TODO: Handle CHAP authentication */
1083 plug_closing(p
->plug
, "Proxy error: We don't support CHAP authentication",
1084 PROXY_ERROR_GENERAL
, 0);
1090 plug_closing(p
->plug
, "Proxy error: Unexpected proxy error",
1091 PROXY_ERROR_UNEXPECTED
, 0);
1095 /* ----------------------------------------------------------------------
1096 * `Telnet' proxy type.
1098 * (This is for ad-hoc proxies where you connect to the proxy's
1099 * telnet port and send a command such as `connect host port'. The
1100 * command is configurable, since this proxy type is typically not
1101 * standardised or at all well-defined.)
1104 int proxy_telnet_negotiate (Proxy_Socket p
, int change
)
1106 if (p
->state
== PROXY_CHANGE_NEW
) {
1110 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1111 * %%, %host, %port, %user, and %pass
1114 while (cfg
.proxy_telnet_command
[eo
] != 0) {
1116 /* scan forward until we hit end-of-line,
1117 * or an escape character (\ or %) */
1118 while (cfg
.proxy_telnet_command
[eo
] != 0 &&
1119 cfg
.proxy_telnet_command
[eo
] != '%' &&
1120 cfg
.proxy_telnet_command
[eo
] != '\\') eo
++;
1122 /* if we hit eol, break out of our escaping loop */
1123 if (cfg
.proxy_telnet_command
[eo
] == 0) break;
1125 /* if there was any unescaped text before the escape
1126 * character, send that now */
1128 sk_write(p
->sub_socket
,
1129 cfg
.proxy_telnet_command
+ so
, eo
- so
);
1134 /* if the escape character was the last character of
1135 * the line, we'll just stop and send it. */
1136 if (cfg
.proxy_telnet_command
[eo
] == 0) break;
1138 if (cfg
.proxy_telnet_command
[so
] == '\\') {
1140 /* we recognize \\, \%, \r, \n, \t, \x??.
1141 * anything else, we just send unescaped (including the \).
1144 switch (cfg
.proxy_telnet_command
[eo
]) {
1147 sk_write(p
->sub_socket
, "\\", 1);
1152 sk_write(p
->sub_socket
, "%%", 1);
1157 sk_write(p
->sub_socket
, "\r", 1);
1162 sk_write(p
->sub_socket
, "\n", 1);
1167 sk_write(p
->sub_socket
, "\t", 1);
1174 /* escaped hexadecimal value (ie. \xff) */
1175 unsigned char v
= 0;
1180 if (cfg
.proxy_telnet_command
[eo
] >= '0' &&
1181 cfg
.proxy_telnet_command
[eo
] <= '9')
1182 v
+= cfg
.proxy_telnet_command
[eo
] - '0';
1183 else if (cfg
.proxy_telnet_command
[eo
] >= 'a' &&
1184 cfg
.proxy_telnet_command
[eo
] <= 'f')
1185 v
+= cfg
.proxy_telnet_command
[eo
] - 'a' + 10;
1186 else if (cfg
.proxy_telnet_command
[eo
] >= 'A' &&
1187 cfg
.proxy_telnet_command
[eo
] <= 'F')
1188 v
+= cfg
.proxy_telnet_command
[eo
] - 'A' + 10;
1190 /* non hex character, so we abort and just
1191 * send the whole thing unescaped (including \x)
1193 sk_write(p
->sub_socket
, "\\", 1);
1198 /* we only extract two hex characters */
1200 sk_write(p
->sub_socket
, &v
, 1);
1212 sk_write(p
->sub_socket
,
1213 cfg
.proxy_telnet_command
+ so
, 2);
1219 /* % escape. we recognize %%, %host, %port, %user, %pass.
1220 * anything else, we just send unescaped (including the %).
1223 if (cfg
.proxy_telnet_command
[eo
] == '%') {
1224 sk_write(p
->sub_socket
, "%", 1);
1227 else if (strnicmp(cfg
.proxy_telnet_command
+ eo
,
1230 sk_getaddr(p
->remote_addr
, dest
, 64);
1231 sk_write(p
->sub_socket
, dest
, strlen(dest
));
1234 else if (strnicmp(cfg
.proxy_telnet_command
+ eo
,
1237 sprintf(port
, "%i", p
->remote_port
);
1238 sk_write(p
->sub_socket
, port
, strlen(port
));
1241 else if (strnicmp(cfg
.proxy_telnet_command
+ eo
,
1243 sk_write(p
->sub_socket
, cfg
.proxy_username
,
1244 strlen(cfg
.proxy_username
));
1247 else if (strnicmp(cfg
.proxy_telnet_command
+ eo
,
1249 sk_write(p
->sub_socket
, cfg
.proxy_password
,
1250 strlen(cfg
.proxy_password
));
1254 /* we don't escape this, so send the % now, and
1255 * don't advance eo, so that we'll consider the
1256 * text immediately following the % as unescaped.
1258 sk_write(p
->sub_socket
, "%", 1);
1262 /* resume scanning for additional escapes after this one. */
1266 /* if there is any unescaped text at the end of the line, send it */
1268 sk_write(p
->sub_socket
, cfg
.proxy_telnet_command
+ so
, eo
- so
);
1275 if (change
== PROXY_CHANGE_CLOSING
) {
1276 /* if our proxy negotiation process involves closing and opening
1277 * new sockets, then we would want to intercept this closing
1278 * callback when we were expecting it. if we aren't anticipating
1279 * a socket close, then some error must have occurred. we'll
1280 * just pass those errors up to the backend.
1282 return plug_closing(p
->plug
, p
->closing_error_msg
,
1283 p
->closing_error_code
,
1284 p
->closing_calling_back
);
1287 if (change
== PROXY_CHANGE_SENT
) {
1288 /* some (or all) of what we wrote to the proxy was sent.
1289 * we don't do anything new, however, until we receive the
1290 * proxy's response. we might want to set a timer so we can
1291 * timeout the proxy negotiation after a while...
1296 if (change
== PROXY_CHANGE_ACCEPTING
) {
1297 /* we should _never_ see this, as we are using our socket to
1298 * connect to a proxy, not accepting inbound connections.
1299 * what should we do? close the socket with an appropriate
1302 return plug_accepting(p
->plug
, p
->accepting_sock
);
1305 if (change
== PROXY_CHANGE_RECEIVE
) {
1306 /* we have received data from the underlying socket, which
1307 * we'll need to parse, process, and respond to appropriately.
1312 /* proxy activate will have dealt with
1313 * whatever is left of the buffer */
1317 plug_closing(p
->plug
, "Proxy error: Unexpected proxy error",
1318 PROXY_ERROR_UNEXPECTED
, 0);