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)
26 p->state = PROXY_STATE_ACTIVE;
28 /* let's try to keep extra receive events from coming through */
29 sk_set_frozen(p->sub_socket, 1);
31 /* send buffered OOB writes */
32 while (bufchain_size(&p->pending_oob_output_data) > 0) {
33 bufchain_prefix(&p->pending_oob_output_data, &data, &len);
34 sk_write_oob(p->sub_socket, data, len);
35 bufchain_consume(&p->pending_oob_output_data, len);
37 bufchain_clear(&p->pending_oob_output_data);
39 /* send buffered normal writes */
40 while (bufchain_size(&p->pending_output_data) > 0) {
41 bufchain_prefix(&p->pending_output_data, &data, &len);
42 sk_write(p->sub_socket, data, len);
43 bufchain_consume(&p->pending_output_data, len);
45 bufchain_clear(&p->pending_output_data);
47 /* if we were asked to flush the output during
48 * the proxy negotiation process, do so now.
50 if (p->pending_flush) sk_flush(p->sub_socket);
52 /* forward buffered recv data to the backend */
53 while (bufchain_size(&p->pending_input_data) > 0) {
54 bufchain_prefix(&p->pending_input_data, &data, &len);
55 plug_receive(p->plug, 0, data, len);
56 bufchain_consume(&p->pending_input_data, len);
58 bufchain_clear(&p->pending_input_data);
60 /* now set the underlying socket to whatever freeze state they wanted */
61 sk_set_frozen(p->sub_socket, p->freeze);
64 /* basic proxy socket functions */
66 static Plug sk_proxy_plug (Socket s, Plug p)
68 Proxy_Socket ps = (Proxy_Socket) s;
75 static void sk_proxy_close (Socket s)
77 Proxy_Socket ps = (Proxy_Socket) s;
79 sk_close(ps->sub_socket);
83 static int sk_proxy_write (Socket s, char *data, int len)
85 Proxy_Socket ps = (Proxy_Socket) s;
87 if (ps->state != PROXY_STATE_ACTIVE) {
88 bufchain_add(&ps->pending_output_data, data, len);
89 return bufchain_size(&ps->pending_output_data);
91 return sk_write(ps->sub_socket, data, len);
94 static int sk_proxy_write_oob (Socket s, char *data, int len)
96 Proxy_Socket ps = (Proxy_Socket) s;
98 if (ps->state != PROXY_STATE_ACTIVE) {
99 bufchain_clear(&ps->pending_output_data);
100 bufchain_clear(&ps->pending_oob_output_data);
101 bufchain_add(&ps->pending_oob_output_data, data, len);
104 return sk_write_oob(ps->sub_socket, data, len);
107 static void sk_proxy_flush (Socket s)
109 Proxy_Socket ps = (Proxy_Socket) s;
111 if (ps->state != PROXY_STATE_ACTIVE) {
112 ps->pending_flush = 1;
115 sk_flush(ps->sub_socket);
118 static void sk_proxy_set_private_ptr (Socket s, void *ptr)
120 Proxy_Socket ps = (Proxy_Socket) s;
121 sk_set_private_ptr(ps->sub_socket, ptr);
124 static void * sk_proxy_get_private_ptr (Socket s)
126 Proxy_Socket ps = (Proxy_Socket) s;
127 return sk_get_private_ptr(ps->sub_socket);
130 static void sk_proxy_set_frozen (Socket s, int is_frozen)
132 Proxy_Socket ps = (Proxy_Socket) s;
134 if (ps->state != PROXY_STATE_ACTIVE) {
135 ps->freeze = is_frozen;
138 sk_set_frozen(ps->sub_socket, is_frozen);
141 static char * sk_proxy_socket_error (Socket s)
143 Proxy_Socket ps = (Proxy_Socket) s;
144 if (ps->error != NULL || ps->sub_socket == NULL) {
147 return sk_socket_error(ps->sub_socket);
150 /* basic proxy plug functions */
152 static int plug_proxy_closing (Plug p, char *error_msg,
153 int error_code, int calling_back)
155 Proxy_Plug pp = (Proxy_Plug) p;
156 Proxy_Socket ps = pp->proxy_socket;
158 if (ps->state != PROXY_STATE_ACTIVE) {
159 ps->closing_error_msg = error_msg;
160 ps->closing_error_code = error_code;
161 ps->closing_calling_back = calling_back;
162 return ps->negotiate(ps, PROXY_CHANGE_CLOSING);
164 return plug_closing(ps->plug, error_msg,
165 error_code, calling_back);
168 static int plug_proxy_receive (Plug p, int urgent, char *data, int len)
170 Proxy_Plug pp = (Proxy_Plug) p;
171 Proxy_Socket ps = pp->proxy_socket;
173 if (ps->state != PROXY_STATE_ACTIVE) {
174 /* we will lose the urgentness of this data, but since most,
175 * if not all, of this data will be consumed by the negotiation
176 * process, hopefully it won't affect the protocol above us
178 bufchain_add(&ps->pending_input_data, data, len);
179 ps->receive_urgent = urgent;
180 ps->receive_data = data;
181 ps->receive_len = len;
182 return ps->negotiate(ps, PROXY_CHANGE_RECEIVE);
184 return plug_receive(ps->plug, urgent, data, len);
187 static void plug_proxy_sent (Plug p, int bufsize)
189 Proxy_Plug pp = (Proxy_Plug) p;
190 Proxy_Socket ps = pp->proxy_socket;
192 if (ps->state != PROXY_STATE_ACTIVE) {
193 ps->sent_bufsize = bufsize;
194 ps->negotiate(ps, PROXY_CHANGE_SENT);
197 plug_sent(ps->plug, bufsize);
200 static int plug_proxy_accepting (Plug p, void *sock)
202 Proxy_Plug pp = (Proxy_Plug) p;
203 Proxy_Socket ps = pp->proxy_socket;
205 if (ps->state != PROXY_STATE_ACTIVE) {
206 ps->accepting_sock = sock;
207 return ps->negotiate(ps, PROXY_CHANGE_ACCEPTING);
209 return plug_accepting(ps->plug, sock);
212 static int proxy_for_destination (SockAddr addr, char * hostname, int port)
216 int hostip_len, hostname_len;
219 /* we want a string representation of the IP address for comparisons */
220 sk_getaddr(addr, hostip, 64);
222 hostip_len = strlen(hostip);
223 hostname_len = strlen(hostname);
225 exclude_list = cfg.proxy_exclude_list;
227 /* now parse the exclude list, and see if either our IP
228 * or hostname matches anything in it.
231 while (exclude_list[s]) {
232 while (exclude_list[s] &&
233 (isspace(exclude_list[s]) ||
234 exclude_list[s] == ',')) s++;
236 if (!exclude_list[s]) break;
240 while (exclude_list[e] &&
241 (isalnum(exclude_list[e]) ||
242 exclude_list[e] == '-' ||
243 exclude_list[e] == '.' ||
244 exclude_list[e] == '*')) e++;
246 if (exclude_list[s] == '*') {
247 /* wildcard at beginning of entry */
249 if (strnicmp(hostip + hostip_len - (e - s - 1),
250 exclude_list + s + 1, e - s - 1) == 0 ||
251 strnicmp(hostname + hostname_len - (e - s - 1),
252 exclude_list + s + 1, e - s - 1) == 0)
253 return 0; /* IP/hostname range excluded. do not use proxy. */
255 } else if (exclude_list[e-1] == '*') {
256 /* wildcard at end of entry */
258 if (strnicmp(hostip, exclude_list + s, e - s - 1) == 0 ||
259 strnicmp(hostname, exclude_list + s, e - s - 1) == 0)
260 return 0; /* IP/hostname range excluded. do not use proxy. */
263 /* no wildcard at either end, so let's try an absolute
264 * match (ie. a specific IP)
267 if (stricmp(hostip, exclude_list + s) == 0)
268 return 0; /* IP/hostname excluded. do not use proxy. */
269 if (stricmp(hostname, exclude_list + s) == 0)
270 return 0; /* IP/hostname excluded. do not use proxy. */
276 /* no matches in the exclude list, so use the proxy */
280 Socket new_connection(SockAddr addr, char *hostname,
281 int port, int privport,
282 int oobinline, int nodelay, Plug plug)
284 static struct socket_function_table socket_fn_table = {
290 sk_proxy_set_private_ptr,
291 sk_proxy_get_private_ptr,
293 sk_proxy_socket_error
296 static struct plug_function_table plug_fn_table = {
303 if (cfg.proxy_type != PROXY_NONE &&
304 proxy_for_destination(addr, hostname, port))
309 char * proxy_canonical_name;
311 ret = smalloc(sizeof(struct Socket_proxy_tag));
312 ret->fn = &socket_fn_table;
314 ret->remote_addr = addr;
315 ret->remote_port = port;
317 bufchain_init(&ret->pending_input_data);
318 bufchain_init(&ret->pending_output_data);
319 bufchain_init(&ret->pending_oob_output_data);
321 ret->sub_socket = NULL;
322 ret->state = PROXY_STATE_NEW;
324 if (cfg.proxy_type == PROXY_HTTP) {
325 ret->negotiate = proxy_http_negotiate;
326 } else if (cfg.proxy_type == PROXY_SOCKS) {
327 if (cfg.proxy_socks_version == 4)
328 ret->negotiate = proxy_socks4_negotiate;
330 ret->negotiate = proxy_socks5_negotiate;
331 } else if (cfg.proxy_type == PROXY_TELNET) {
332 ret->negotiate = proxy_telnet_negotiate;
334 ret->error = "Proxy error: Unknown proxy method";
338 /* create the proxy plug to map calls from the actual
339 * socket into our proxy socket layer */
340 pplug = smalloc(sizeof(struct Plug_proxy_tag));
341 pplug->fn = &plug_fn_table;
342 pplug->proxy_socket = ret;
345 proxy_addr = sk_namelookup(cfg.proxy_host,
346 &proxy_canonical_name);
347 sfree(proxy_canonical_name);
349 /* create the actual socket we will be using,
350 * connected to our proxy server and port.
352 ret->sub_socket = sk_new(proxy_addr, cfg.proxy_port,
354 nodelay, (Plug) pplug);
355 if (sk_socket_error(ret->sub_socket) != NULL)
358 sk_addr_free(proxy_addr);
360 /* start the proxy negotiation process... */
361 sk_set_frozen(ret->sub_socket, 0);
362 ret->negotiate(ret, PROXY_CHANGE_NEW);
367 /* no proxy, so just return the direct socket */
368 return sk_new(addr, port, privport, oobinline, nodelay, plug);
371 Socket new_listener(int port, Plug plug, int local_host_only)
373 /* TODO: SOCKS (and potentially others) support inbound
374 * TODO: connections via the proxy. support them.
377 return sk_newlistener(port, plug, local_host_only);
380 /* ----------------------------------------------------------------------
381 * HTTP CONNECT proxy type.
384 static int get_line_end (char * data, int len)
390 if (data[off] == '\n') {
391 /* we have a newline */
394 /* is that the only thing on this line? */
395 if (off <= 2) return off;
397 /* if not, then there is the possibility that this header
398 * continues onto the next line, if it starts with a space
403 data[off+1] != ' ' &&
404 data[off+1] != '\t') return off;
406 /* the line does continue, so we have to keep going
407 * until we see an the header's "real" end of line.
418 int proxy_http_negotiate (Proxy_Socket p, int change)
420 if (p->state == PROXY_STATE_NEW) {
421 /* we are just beginning the proxy negotiate process,
422 * so we'll send off the initial bits of the request.
423 * for this proxy method, it's just a simple HTTP
426 char buf[256], dest[64];
428 sk_getaddr(p->remote_addr, dest, 64);
430 sprintf(buf, "CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
431 dest, p->remote_port, dest, p->remote_port);
432 sk_write(p->sub_socket, buf, strlen(buf));
434 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
435 char buf[sizeof(cfg.proxy_username)+sizeof(cfg.proxy_password)];
436 char buf2[sizeof(buf)*4/3 + 100];
438 sprintf(buf, "%s:%s", cfg.proxy_username, cfg.proxy_password);
440 sprintf(buf2, "Proxy-Authorization: basic ");
441 for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)
442 base64_encode_atom(buf+i, (len-i > 3 ? 3 : len-i), buf2+j);
443 strcpy(buf2+j, "\r\n");
444 sk_write(p->sub_socket, buf2, strlen(buf2));
447 sprintf(buf, "\r\n");
448 sk_write(p->sub_socket, buf, strlen(buf));
454 if (change == PROXY_CHANGE_CLOSING) {
455 /* if our proxy negotiation process involves closing and opening
456 * new sockets, then we would want to intercept this closing
457 * callback when we were expecting it. if we aren't anticipating
458 * a socket close, then some error must have occurred. we'll
459 * just pass those errors up to the backend.
461 return plug_closing(p->plug, p->closing_error_msg,
462 p->closing_error_code,
463 p->closing_calling_back);
466 if (change == PROXY_CHANGE_SENT) {
467 /* some (or all) of what we wrote to the proxy was sent.
468 * we don't do anything new, however, until we receive the
469 * proxy's response. we might want to set a timer so we can
470 * timeout the proxy negotiation after a while...
475 if (change == PROXY_CHANGE_ACCEPTING) {
476 /* we should _never_ see this, as we are using our socket to
477 * connect to a proxy, not accepting inbound connections.
478 * what should we do? close the socket with an appropriate
481 return plug_accepting(p->plug, p->accepting_sock);
484 if (change == PROXY_CHANGE_RECEIVE) {
485 /* we have received data from the underlying socket, which
486 * we'll need to parse, process, and respond to appropriately.
495 int min_ver, maj_ver, status;
497 /* get the status line */
498 len = bufchain_size(&p->pending_input_data);
499 assert(len > 0); /* or we wouldn't be here */
501 bufchain_fetch(&p->pending_input_data, data, len);
503 eol = get_line_end(data, len);
510 /* We can't rely on whether the %n incremented the sscanf return */
511 if (sscanf((char *)data, "HTTP/%i.%i %n",
512 &maj_ver, &min_ver, &status) < 2 || status == -1) {
513 plug_closing(p->plug, "Proxy error: HTTP response was absent",
514 PROXY_ERROR_GENERAL, 0);
519 /* remove the status line from the input buffer. */
520 bufchain_consume(&p->pending_input_data, eol);
521 if (data[status] != '2') {
525 while (eol > status &&
526 (data[eol-1] == '\r' || data[eol-1] == '\n'))
528 sprintf(buf, "Proxy error: %.900s",
530 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
542 /* get headers. we're done when we get a
543 * header of length 2, (ie. just "\r\n")
546 len = bufchain_size(&p->pending_input_data);
547 assert(len > 0); /* or we wouldn't be here */
550 bufchain_fetch(&p->pending_input_data, data, len);
552 eol = get_line_end(datap, len);
559 bufchain_consume(&p->pending_input_data, eol);
561 eol = get_line_end(datap, len);
566 bufchain_consume(&p->pending_input_data, 2);
568 /* proxy activate will have dealt with
569 * whatever is left of the buffer */
579 plug_closing(p->plug, "Proxy error: unexpected proxy error",
580 PROXY_ERROR_UNEXPECTED, 0);
584 /* ----------------------------------------------------------------------
588 /* SOCKS version 4 */
589 int proxy_socks4_negotiate (Proxy_Socket p, int change)
591 if (p->state == PROXY_CHANGE_NEW) {
594 * version number (1 byte) = 4
595 * command code (1 byte)
598 * dest. port (2 bytes) [network order]
599 * dest. address (4 bytes)
600 * user ID (variable length, null terminated string)
606 if (sk_addrtype(p->remote_addr) != AF_INET) {
607 plug_closing(p->plug, "Proxy error: SOCKS version 4 does"
608 " not support IPv6", PROXY_ERROR_GENERAL, 0);
612 length = strlen(cfg.proxy_username) + 9;
613 command = (char*) malloc(length);
614 strcpy(command + 8, cfg.proxy_username);
616 command[0] = 4; /* version 4 */
617 command[1] = 1; /* CONNECT command */
620 command[2] = (char) (p->remote_port >> 8) & 0xff;
621 command[3] = (char) p->remote_port & 0xff;
624 sk_addrcopy(p->remote_addr, command + 4);
626 sk_write(p->sub_socket, command, length);
633 if (change == PROXY_CHANGE_CLOSING) {
634 /* if our proxy negotiation process involves closing and opening
635 * new sockets, then we would want to intercept this closing
636 * callback when we were expecting it. if we aren't anticipating
637 * a socket close, then some error must have occurred. we'll
638 * just pass those errors up to the backend.
640 return plug_closing(p->plug, p->closing_error_msg,
641 p->closing_error_code,
642 p->closing_calling_back);
645 if (change == PROXY_CHANGE_SENT) {
646 /* some (or all) of what we wrote to the proxy was sent.
647 * we don't do anything new, however, until we receive the
648 * proxy's response. we might want to set a timer so we can
649 * timeout the proxy negotiation after a while...
654 if (change == PROXY_CHANGE_ACCEPTING) {
655 /* we should _never_ see this, as we are using our socket to
656 * connect to a proxy, not accepting inbound connections.
657 * what should we do? close the socket with an appropriate
660 return plug_accepting(p->plug, p->accepting_sock);
663 if (change == PROXY_CHANGE_RECEIVE) {
664 /* we have received data from the underlying socket, which
665 * we'll need to parse, process, and respond to appropriately.
670 * version number (1 byte) = 4
671 * reply code (1 byte)
672 * 90 = request granted
673 * 91 = request rejected or failed
674 * 92 = request rejected due to lack of IDENTD on client
675 * 93 = request rejected due to difference in user ID
676 * (what we sent vs. what IDENTD said)
677 * dest. port (2 bytes)
678 * dest. address (4 bytes)
683 if (bufchain_size(&p->pending_input_data) < 8)
684 return 1; /* not got anything yet */
686 /* get the response */
687 bufchain_fetch(&p->pending_input_data, data, 8);
690 plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "
691 "unexpected reply code version",
692 PROXY_ERROR_GENERAL, 0);
700 plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",
701 PROXY_ERROR_GENERAL, 0);
704 plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",
705 PROXY_ERROR_GENERAL, 0);
709 plug_closing(p->plug, "Proxy error: Error while communicating with proxy",
710 PROXY_ERROR_GENERAL, 0);
716 bufchain_consume(&p->pending_input_data, 8);
720 /* proxy activate will have dealt with
721 * whatever is left of the buffer */
726 plug_closing(p->plug, "Proxy error: unexpected proxy error",
727 PROXY_ERROR_UNEXPECTED, 0);
731 /* SOCKS version 5 */
732 int proxy_socks5_negotiate (Proxy_Socket p, int change)
734 if (p->state == PROXY_CHANGE_NEW) {
737 * version number (1 byte) = 5
738 * number of available authentication methods (1 byte)
739 * available authentication methods (1 byte * previous value)
740 * authentication methods:
741 * 0x00 = no authentication
743 * 0x02 = username/password
750 command[0] = 5; /* version 5 */
751 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
752 command[1] = 2; /* two methods supported: */
753 command[2] = 0x00; /* no authentication */
754 command[3] = 0x02; /* username/password */
757 command[1] = 1; /* one methods supported: */
758 command[2] = 0x00; /* no authentication */
762 sk_write(p->sub_socket, command, len);
768 if (change == PROXY_CHANGE_CLOSING) {
769 /* if our proxy negotiation process involves closing and opening
770 * new sockets, then we would want to intercept this closing
771 * callback when we were expecting it. if we aren't anticipating
772 * a socket close, then some error must have occurred. we'll
773 * just pass those errors up to the backend.
775 return plug_closing(p->plug, p->closing_error_msg,
776 p->closing_error_code,
777 p->closing_calling_back);
780 if (change == PROXY_CHANGE_SENT) {
781 /* some (or all) of what we wrote to the proxy was sent.
782 * we don't do anything new, however, until we receive the
783 * proxy's response. we might want to set a timer so we can
784 * timeout the proxy negotiation after a while...
789 if (change == PROXY_CHANGE_ACCEPTING) {
790 /* we should _never_ see this, as we are using our socket to
791 * connect to a proxy, not accepting inbound connections.
792 * what should we do? close the socket with an appropriate
795 return plug_accepting(p->plug, p->accepting_sock);
798 if (change == PROXY_CHANGE_RECEIVE) {
799 /* we have received data from the underlying socket, which
800 * we'll need to parse, process, and respond to appropriately.
806 * version number (1 byte) = 5
807 * authentication method (1 byte)
808 * authentication methods:
809 * 0x00 = no authentication
811 * 0x02 = username/password
813 * 0xff = no acceptable methods
817 if (bufchain_size(&p->pending_input_data) < 2)
818 return 1; /* not got anything yet */
820 /* get the response */
821 bufchain_fetch(&p->pending_input_data, data, 2);
824 plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",
825 PROXY_ERROR_GENERAL, 0);
829 if (data[1] == 0x00) p->state = 2; /* no authentication needed */
830 else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */
831 else if (data[1] == 0x02) p->state = 5; /* username/password authentication */
832 else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */
834 plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",
835 PROXY_ERROR_GENERAL, 0);
838 bufchain_consume(&p->pending_input_data, 2);
843 /* password authentication reply format:
844 * version number (1 bytes) = 1
845 * reply code (1 byte)
851 if (bufchain_size(&p->pending_input_data) < 2)
852 return 1; /* not got anything yet */
854 /* get the response */
855 bufchain_fetch(&p->pending_input_data, data, 2);
858 plug_closing(p->plug, "Proxy error: SOCKS password "
859 "subnegotiation contained wrong version number",
860 PROXY_ERROR_GENERAL, 0);
866 plug_closing(p->plug, "Proxy error: SOCKS proxy refused"
867 " password authentication",
868 PROXY_ERROR_GENERAL, 0);
872 bufchain_consume(&p->pending_input_data, 2);
873 p->state = 2; /* now proceed as authenticated */
879 * version number (1 byte) = 5
880 * command code (1 byte)
884 * reserved (1 byte) = 0x00
885 * address type (1 byte)
887 * 3 = domainname (first byte has length, no terminating null)
889 * dest. address (variable)
890 * dest. port (2 bytes) [network order]
896 if (sk_addrtype(p->remote_addr) == AF_INET) {
898 command[3] = 1; /* IPv4 */
901 command[3] = 4; /* IPv6 */
904 command[0] = 5; /* version 5 */
905 command[1] = 1; /* CONNECT command */
909 sk_addrcopy(p->remote_addr, command+4);
912 command[len-2] = (char) (p->remote_port >> 8) & 0xff;
913 command[len-1] = (char) p->remote_port & 0xff;
915 sk_write(p->sub_socket, command, len);
924 * version number (1 bytes) = 5
925 * reply code (1 byte)
927 * 1 = general SOCKS server failure
928 * 2 = connection not allowed by ruleset
929 * 3 = network unreachable
930 * 4 = host unreachable
931 * 5 = connection refused
933 * 7 = command not supported
934 * 8 = address type not supported
935 * reserved (1 byte) = x00
936 * address type (1 byte)
938 * 3 = domainname (first byte has length, no terminating null)
940 * server bound address (variable)
941 * server bound port (2 bytes) [network order]
946 /* First 5 bytes of packet are enough to tell its length. */
947 if (bufchain_size(&p->pending_input_data) < 5)
948 return 1; /* not got anything yet */
950 /* get the response */
951 bufchain_fetch(&p->pending_input_data, data, 5);
954 plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",
955 PROXY_ERROR_GENERAL, 0);
962 strcpy(buf, "Proxy error: ");
965 case 1: strcat(buf, "General SOCKS server failure"); break;
966 case 2: strcat(buf, "Connection not allowed by ruleset"); break;
967 case 3: strcat(buf, "Network unreachable"); break;
968 case 4: strcat(buf, "Host unreachable"); break;
969 case 5: strcat(buf, "Connection refused"); break;
970 case 6: strcat(buf, "TTL expired"); break;
971 case 7: strcat(buf, "Command not supported"); break;
972 case 8: strcat(buf, "Address type not supported"); break;
973 default: sprintf(buf+strlen(buf),
974 "Unrecognised SOCKS error code %d",
978 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
984 * Eat the rest of the reply packet.
986 len = 6; /* first 4 bytes, last 2 */
988 case 1: len += 4; break; /* IPv4 address */
989 case 4: len += 16; break;/* IPv6 address */
990 case 3: len += (unsigned char)data[4]; break; /* domain name */
992 plug_closing(p->plug, "Proxy error: SOCKS proxy returned "
993 "unrecognised address format",
994 PROXY_ERROR_GENERAL, 0);
997 if (bufchain_size(&p->pending_input_data) < len)
998 return 1; /* not got whole reply yet */
999 bufchain_consume(&p->pending_input_data, len);
1006 if (p->state == 4) {
1007 /* TODO: Handle GSSAPI authentication */
1008 plug_closing(p->plug, "Network error: We don't support GSSAPI authentication",
1009 PROXY_ERROR_GENERAL, 0);
1013 if (p->state == 5) {
1014 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
1015 char userpwbuf[514];
1017 ulen = strlen(cfg.proxy_username);
1018 if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
1019 plen = strlen(cfg.proxy_password);
1020 if (plen > 255) plen = 255; if (plen < 1) plen = 1;
1021 userpwbuf[0] = 1; /* version number of subnegotiation */
1022 userpwbuf[1] = ulen;
1023 memcpy(userpwbuf+2, cfg.proxy_username, ulen);
1024 userpwbuf[ulen+2] = plen;
1025 memcpy(userpwbuf+ulen+3, cfg.proxy_password, plen);
1026 sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);
1029 plug_closing(p->plug, "Network error: Server chose "
1030 "username/password authentication but we "
1032 PROXY_ERROR_GENERAL, 0);
1036 if (p->state == 6) {
1037 /* TODO: Handle CHAP authentication */
1038 plug_closing(p->plug, "Network error: We don't support CHAP authentication",
1039 PROXY_ERROR_GENERAL, 0);
1045 plug_closing(p->plug, "Network error: Unexpected proxy error",
1046 PROXY_ERROR_UNEXPECTED, 0);
1050 /* ----------------------------------------------------------------------
1051 * `Telnet' proxy type.
1053 * (This is for ad-hoc proxies where you connect to the proxy's
1054 * telnet port and send a command such as `connect host port'. The
1055 * command is configurable, since this proxy type is typically not
1056 * standardised or at all well-defined.)
1059 int proxy_telnet_negotiate (Proxy_Socket p, int change)
1061 if (p->state == PROXY_CHANGE_NEW) {
1065 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1066 * %%, %host, and %port
1069 while (cfg.proxy_telnet_command[eo] != 0) {
1071 /* scan forward until we hit end-of-line,
1072 * or an escape character (\ or %) */
1073 while (cfg.proxy_telnet_command[eo] != 0 &&
1074 cfg.proxy_telnet_command[eo] != '%' &&
1075 cfg.proxy_telnet_command[eo] != '\\') eo++;
1077 /* if we hit eol, break out of our escaping loop */
1078 if (cfg.proxy_telnet_command[eo] == 0) break;
1080 /* if there was any unescaped text before the escape
1081 * character, send that now */
1083 sk_write(p->sub_socket,
1084 cfg.proxy_telnet_command + so, eo - so);
1089 /* if the escape character was the last character of
1090 * the line, we'll just stop and send it. */
1091 if (cfg.proxy_telnet_command[eo] == 0) break;
1093 if (cfg.proxy_telnet_command[so] == '\\') {
1095 /* we recognize \\, \%, \r, \n, \t, \x??.
1096 * anything else, we just send unescaped (including the \).
1099 switch (cfg.proxy_telnet_command[eo]) {
1102 sk_write(p->sub_socket, "\\", 1);
1107 sk_write(p->sub_socket, "%%", 1);
1112 sk_write(p->sub_socket, "\r", 1);
1117 sk_write(p->sub_socket, "\n", 1);
1122 sk_write(p->sub_socket, "\t", 1);
1129 /* escaped hexadecimal value (ie. \xff) */
1130 unsigned char v = 0;
1135 if (cfg.proxy_telnet_command[eo] >= '0' &&
1136 cfg.proxy_telnet_command[eo] <= '9')
1137 v += cfg.proxy_telnet_command[eo] - '0';
1138 else if (cfg.proxy_telnet_command[eo] >= 'a' &&
1139 cfg.proxy_telnet_command[eo] <= 'f')
1140 v += cfg.proxy_telnet_command[eo] - 'a' + 10;
1141 else if (cfg.proxy_telnet_command[eo] >= 'A' &&
1142 cfg.proxy_telnet_command[eo] <= 'F')
1143 v += cfg.proxy_telnet_command[eo] - 'A' + 10;
1145 /* non hex character, so we abort and just
1146 * send the whole thing unescaped (including \x)
1148 sk_write(p->sub_socket, "\\", 1);
1153 /* we only extract two hex characters */
1155 sk_write(p->sub_socket, &v, 1);
1167 sk_write(p->sub_socket,
1168 cfg.proxy_telnet_command + so, 2);
1174 /* % escape. we recognize %%, %host, %port. anything else,
1175 * we just send unescaped (including the %). */
1177 if (cfg.proxy_telnet_command[eo] == '%') {
1178 sk_write(p->sub_socket, "%", 1);
1181 else if (strnicmp(cfg.proxy_telnet_command + eo,
1184 sk_getaddr(p->remote_addr, dest, 64);
1185 sk_write(p->sub_socket, dest, strlen(dest));
1188 else if (strnicmp(cfg.proxy_telnet_command + eo,
1191 sprintf(port, "%i", p->remote_port);
1192 sk_write(p->sub_socket, port, strlen(port));
1196 /* we don't escape this, so send the % now, and
1197 * don't advance eo, so that we'll consider the
1198 * text immediately following the % as unescaped.
1200 sk_write(p->sub_socket, "%", 1);
1204 /* resume scanning for additional escapes after this one. */
1208 /* if there is any unescaped text at the end of the line, send it */
1210 sk_write(p->sub_socket, cfg.proxy_telnet_command + so, eo - so);
1217 if (change == PROXY_CHANGE_CLOSING) {
1218 /* if our proxy negotiation process involves closing and opening
1219 * new sockets, then we would want to intercept this closing
1220 * callback when we were expecting it. if we aren't anticipating
1221 * a socket close, then some error must have occurred. we'll
1222 * just pass those errors up to the backend.
1224 return plug_closing(p->plug, p->closing_error_msg,
1225 p->closing_error_code,
1226 p->closing_calling_back);
1229 if (change == PROXY_CHANGE_SENT) {
1230 /* some (or all) of what we wrote to the proxy was sent.
1231 * we don't do anything new, however, until we receive the
1232 * proxy's response. we might want to set a timer so we can
1233 * timeout the proxy negotiation after a while...
1238 if (change == PROXY_CHANGE_ACCEPTING) {
1239 /* we should _never_ see this, as we are using our socket to
1240 * connect to a proxy, not accepting inbound connections.
1241 * what should we do? close the socket with an appropriate
1244 return plug_accepting(p->plug, p->accepting_sock);
1247 if (change == PROXY_CHANGE_RECEIVE) {
1248 /* we have received data from the underlying socket, which
1249 * we'll need to parse, process, and respond to appropriately.
1254 /* proxy activate will have dealt with
1255 * whatever is left of the buffer */
1259 plug_closing(p->plug, "Network error: Unexpected proxy error",
1260 PROXY_ERROR_UNEXPECTED, 0);