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 /* XXX review these initialisations, and initialise other fields
318 * in Proxy_Socket structure */
320 ret->pending_flush = 0;
323 bufchain_init(&ret->pending_input_data);
324 bufchain_init(&ret->pending_output_data);
325 bufchain_init(&ret->pending_oob_output_data);
327 ret->sub_socket = NULL;
328 ret->state = PROXY_STATE_NEW;
330 if (cfg.proxy_type == PROXY_HTTP) {
331 ret->negotiate = proxy_http_negotiate;
332 } else if (cfg.proxy_type == PROXY_SOCKS) {
333 if (cfg.proxy_socks_version == 4)
334 ret->negotiate = proxy_socks4_negotiate;
336 ret->negotiate = proxy_socks5_negotiate;
337 } else if (cfg.proxy_type == PROXY_TELNET) {
338 ret->negotiate = proxy_telnet_negotiate;
340 ret->error = "Proxy error: Unknown proxy method";
344 /* create the proxy plug to map calls from the actual
345 * socket into our proxy socket layer */
346 pplug = smalloc(sizeof(struct Plug_proxy_tag));
347 pplug->fn = &plug_fn_table;
348 pplug->proxy_socket = ret;
351 proxy_addr = sk_namelookup(cfg.proxy_host,
352 &proxy_canonical_name);
353 sfree(proxy_canonical_name);
355 /* create the actual socket we will be using,
356 * connected to our proxy server and port.
358 ret->sub_socket = sk_new(proxy_addr, cfg.proxy_port,
360 nodelay, (Plug) pplug);
361 if (sk_socket_error(ret->sub_socket) != NULL)
364 sk_addr_free(proxy_addr);
366 /* start the proxy negotiation process... */
367 sk_set_frozen(ret->sub_socket, 0);
368 ret->negotiate(ret, PROXY_CHANGE_NEW);
373 /* no proxy, so just return the direct socket */
374 return sk_new(addr, port, privport, oobinline, nodelay, plug);
377 Socket new_listener(int port, Plug plug, int local_host_only)
379 /* TODO: SOCKS (and potentially others) support inbound
380 * TODO: connections via the proxy. support them.
383 return sk_newlistener(port, plug, local_host_only);
386 /* ----------------------------------------------------------------------
387 * HTTP CONNECT proxy type.
390 static int get_line_end (char * data, int len)
396 if (data[off] == '\n') {
397 /* we have a newline */
400 /* is that the only thing on this line? */
401 if (off <= 2) return off;
403 /* if not, then there is the possibility that this header
404 * continues onto the next line, if it starts with a space
409 data[off+1] != ' ' &&
410 data[off+1] != '\t') return off;
412 /* the line does continue, so we have to keep going
413 * until we see an the header's "real" end of line.
424 int proxy_http_negotiate (Proxy_Socket p, int change)
426 if (p->state == PROXY_STATE_NEW) {
427 /* we are just beginning the proxy negotiate process,
428 * so we'll send off the initial bits of the request.
429 * for this proxy method, it's just a simple HTTP
432 char buf[256], dest[64];
434 sk_getaddr(p->remote_addr, dest, 64);
436 sprintf(buf, "CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
437 dest, p->remote_port, dest, p->remote_port);
438 sk_write(p->sub_socket, buf, strlen(buf));
440 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
441 char buf[sizeof(cfg.proxy_username)+sizeof(cfg.proxy_password)];
442 char buf2[sizeof(buf)*4/3 + 100];
444 sprintf(buf, "%s:%s", cfg.proxy_username, cfg.proxy_password);
446 sprintf(buf2, "Proxy-Authorization: basic ");
447 for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)
448 base64_encode_atom(buf+i, (len-i > 3 ? 3 : len-i), buf2+j);
449 strcpy(buf2+j, "\r\n");
450 sk_write(p->sub_socket, buf2, strlen(buf2));
453 sprintf(buf, "\r\n");
454 sk_write(p->sub_socket, buf, strlen(buf));
460 if (change == PROXY_CHANGE_CLOSING) {
461 /* if our proxy negotiation process involves closing and opening
462 * new sockets, then we would want to intercept this closing
463 * callback when we were expecting it. if we aren't anticipating
464 * a socket close, then some error must have occurred. we'll
465 * just pass those errors up to the backend.
467 return plug_closing(p->plug, p->closing_error_msg,
468 p->closing_error_code,
469 p->closing_calling_back);
472 if (change == PROXY_CHANGE_SENT) {
473 /* some (or all) of what we wrote to the proxy was sent.
474 * we don't do anything new, however, until we receive the
475 * proxy's response. we might want to set a timer so we can
476 * timeout the proxy negotiation after a while...
481 if (change == PROXY_CHANGE_ACCEPTING) {
482 /* we should _never_ see this, as we are using our socket to
483 * connect to a proxy, not accepting inbound connections.
484 * what should we do? close the socket with an appropriate
487 return plug_accepting(p->plug, p->accepting_sock);
490 if (change == PROXY_CHANGE_RECEIVE) {
491 /* we have received data from the underlying socket, which
492 * we'll need to parse, process, and respond to appropriately.
501 int min_ver, maj_ver, status;
503 /* get the status line */
504 len = bufchain_size(&p->pending_input_data);
505 assert(len > 0); /* or we wouldn't be here */
507 bufchain_fetch(&p->pending_input_data, data, len);
509 eol = get_line_end(data, len);
516 /* We can't rely on whether the %n incremented the sscanf return */
517 if (sscanf((char *)data, "HTTP/%i.%i %n",
518 &maj_ver, &min_ver, &status) < 2 || status == -1) {
519 plug_closing(p->plug, "Proxy error: HTTP response was absent",
520 PROXY_ERROR_GENERAL, 0);
525 /* remove the status line from the input buffer. */
526 bufchain_consume(&p->pending_input_data, eol);
527 if (data[status] != '2') {
531 while (eol > status &&
532 (data[eol-1] == '\r' || data[eol-1] == '\n'))
534 sprintf(buf, "Proxy error: %.900s",
536 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
548 /* get headers. we're done when we get a
549 * header of length 2, (ie. just "\r\n")
552 len = bufchain_size(&p->pending_input_data);
553 assert(len > 0); /* or we wouldn't be here */
556 bufchain_fetch(&p->pending_input_data, data, len);
558 eol = get_line_end(datap, len);
565 bufchain_consume(&p->pending_input_data, eol);
567 eol = get_line_end(datap, len);
572 bufchain_consume(&p->pending_input_data, 2);
574 /* proxy activate will have dealt with
575 * whatever is left of the buffer */
585 plug_closing(p->plug, "Proxy error: unexpected proxy error",
586 PROXY_ERROR_UNEXPECTED, 0);
590 /* ----------------------------------------------------------------------
594 /* SOCKS version 4 */
595 int proxy_socks4_negotiate (Proxy_Socket p, int change)
597 if (p->state == PROXY_CHANGE_NEW) {
600 * version number (1 byte) = 4
601 * command code (1 byte)
604 * dest. port (2 bytes) [network order]
605 * dest. address (4 bytes)
606 * user ID (variable length, null terminated string)
612 if (sk_addrtype(p->remote_addr) != AF_INET) {
613 plug_closing(p->plug, "Proxy error: SOCKS version 4 does"
614 " not support IPv6", PROXY_ERROR_GENERAL, 0);
618 length = strlen(cfg.proxy_username) + 9;
619 command = (char*) malloc(length);
620 strcpy(command + 8, cfg.proxy_username);
622 command[0] = 4; /* version 4 */
623 command[1] = 1; /* CONNECT command */
626 command[2] = (char) (p->remote_port >> 8) & 0xff;
627 command[3] = (char) p->remote_port & 0xff;
630 sk_addrcopy(p->remote_addr, command + 4);
632 sk_write(p->sub_socket, command, length);
639 if (change == PROXY_CHANGE_CLOSING) {
640 /* if our proxy negotiation process involves closing and opening
641 * new sockets, then we would want to intercept this closing
642 * callback when we were expecting it. if we aren't anticipating
643 * a socket close, then some error must have occurred. we'll
644 * just pass those errors up to the backend.
646 return plug_closing(p->plug, p->closing_error_msg,
647 p->closing_error_code,
648 p->closing_calling_back);
651 if (change == PROXY_CHANGE_SENT) {
652 /* some (or all) of what we wrote to the proxy was sent.
653 * we don't do anything new, however, until we receive the
654 * proxy's response. we might want to set a timer so we can
655 * timeout the proxy negotiation after a while...
660 if (change == PROXY_CHANGE_ACCEPTING) {
661 /* we should _never_ see this, as we are using our socket to
662 * connect to a proxy, not accepting inbound connections.
663 * what should we do? close the socket with an appropriate
666 return plug_accepting(p->plug, p->accepting_sock);
669 if (change == PROXY_CHANGE_RECEIVE) {
670 /* we have received data from the underlying socket, which
671 * we'll need to parse, process, and respond to appropriately.
676 * version number (1 byte) = 4
677 * reply code (1 byte)
678 * 90 = request granted
679 * 91 = request rejected or failed
680 * 92 = request rejected due to lack of IDENTD on client
681 * 93 = request rejected due to difference in user ID
682 * (what we sent vs. what IDENTD said)
683 * dest. port (2 bytes)
684 * dest. address (4 bytes)
689 if (bufchain_size(&p->pending_input_data) < 8)
690 return 1; /* not got anything yet */
692 /* get the response */
693 bufchain_fetch(&p->pending_input_data, data, 8);
696 plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "
697 "unexpected reply code version",
698 PROXY_ERROR_GENERAL, 0);
706 plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",
707 PROXY_ERROR_GENERAL, 0);
710 plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",
711 PROXY_ERROR_GENERAL, 0);
715 plug_closing(p->plug, "Proxy error: Error while communicating with proxy",
716 PROXY_ERROR_GENERAL, 0);
722 bufchain_consume(&p->pending_input_data, 8);
726 /* proxy activate will have dealt with
727 * whatever is left of the buffer */
732 plug_closing(p->plug, "Proxy error: unexpected proxy error",
733 PROXY_ERROR_UNEXPECTED, 0);
737 /* SOCKS version 5 */
738 int proxy_socks5_negotiate (Proxy_Socket p, int change)
740 if (p->state == PROXY_CHANGE_NEW) {
743 * version number (1 byte) = 5
744 * number of available authentication methods (1 byte)
745 * available authentication methods (1 byte * previous value)
746 * authentication methods:
747 * 0x00 = no authentication
749 * 0x02 = username/password
756 command[0] = 5; /* version 5 */
757 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
758 command[1] = 2; /* two methods supported: */
759 command[2] = 0x00; /* no authentication */
760 command[3] = 0x02; /* username/password */
763 command[1] = 1; /* one methods supported: */
764 command[2] = 0x00; /* no authentication */
768 sk_write(p->sub_socket, command, len);
774 if (change == PROXY_CHANGE_CLOSING) {
775 /* if our proxy negotiation process involves closing and opening
776 * new sockets, then we would want to intercept this closing
777 * callback when we were expecting it. if we aren't anticipating
778 * a socket close, then some error must have occurred. we'll
779 * just pass those errors up to the backend.
781 return plug_closing(p->plug, p->closing_error_msg,
782 p->closing_error_code,
783 p->closing_calling_back);
786 if (change == PROXY_CHANGE_SENT) {
787 /* some (or all) of what we wrote to the proxy was sent.
788 * we don't do anything new, however, until we receive the
789 * proxy's response. we might want to set a timer so we can
790 * timeout the proxy negotiation after a while...
795 if (change == PROXY_CHANGE_ACCEPTING) {
796 /* we should _never_ see this, as we are using our socket to
797 * connect to a proxy, not accepting inbound connections.
798 * what should we do? close the socket with an appropriate
801 return plug_accepting(p->plug, p->accepting_sock);
804 if (change == PROXY_CHANGE_RECEIVE) {
805 /* we have received data from the underlying socket, which
806 * we'll need to parse, process, and respond to appropriately.
812 * version number (1 byte) = 5
813 * authentication method (1 byte)
814 * authentication methods:
815 * 0x00 = no authentication
817 * 0x02 = username/password
819 * 0xff = no acceptable methods
823 if (bufchain_size(&p->pending_input_data) < 2)
824 return 1; /* not got anything yet */
826 /* get the response */
827 bufchain_fetch(&p->pending_input_data, data, 2);
830 plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",
831 PROXY_ERROR_GENERAL, 0);
835 if (data[1] == 0x00) p->state = 2; /* no authentication needed */
836 else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */
837 else if (data[1] == 0x02) p->state = 5; /* username/password authentication */
838 else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */
840 plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",
841 PROXY_ERROR_GENERAL, 0);
844 bufchain_consume(&p->pending_input_data, 2);
849 /* password authentication reply format:
850 * version number (1 bytes) = 1
851 * reply code (1 byte)
857 if (bufchain_size(&p->pending_input_data) < 2)
858 return 1; /* not got anything yet */
860 /* get the response */
861 bufchain_fetch(&p->pending_input_data, data, 2);
864 plug_closing(p->plug, "Proxy error: SOCKS password "
865 "subnegotiation contained wrong version number",
866 PROXY_ERROR_GENERAL, 0);
872 plug_closing(p->plug, "Proxy error: SOCKS proxy refused"
873 " password authentication",
874 PROXY_ERROR_GENERAL, 0);
878 bufchain_consume(&p->pending_input_data, 2);
879 p->state = 2; /* now proceed as authenticated */
885 * version number (1 byte) = 5
886 * command code (1 byte)
890 * reserved (1 byte) = 0x00
891 * address type (1 byte)
893 * 3 = domainname (first byte has length, no terminating null)
895 * dest. address (variable)
896 * dest. port (2 bytes) [network order]
902 if (sk_addrtype(p->remote_addr) == AF_INET) {
904 command[3] = 1; /* IPv4 */
907 command[3] = 4; /* IPv6 */
910 command[0] = 5; /* version 5 */
911 command[1] = 1; /* CONNECT command */
915 sk_addrcopy(p->remote_addr, command+4);
918 command[len-2] = (char) (p->remote_port >> 8) & 0xff;
919 command[len-1] = (char) p->remote_port & 0xff;
921 sk_write(p->sub_socket, command, len);
930 * version number (1 bytes) = 5
931 * reply code (1 byte)
933 * 1 = general SOCKS server failure
934 * 2 = connection not allowed by ruleset
935 * 3 = network unreachable
936 * 4 = host unreachable
937 * 5 = connection refused
939 * 7 = command not supported
940 * 8 = address type not supported
941 * reserved (1 byte) = x00
942 * address type (1 byte)
944 * 3 = domainname (first byte has length, no terminating null)
946 * server bound address (variable)
947 * server bound port (2 bytes) [network order]
952 /* First 5 bytes of packet are enough to tell its length. */
953 if (bufchain_size(&p->pending_input_data) < 5)
954 return 1; /* not got anything yet */
956 /* get the response */
957 bufchain_fetch(&p->pending_input_data, data, 5);
960 plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",
961 PROXY_ERROR_GENERAL, 0);
968 strcpy(buf, "Proxy error: ");
971 case 1: strcat(buf, "General SOCKS server failure"); break;
972 case 2: strcat(buf, "Connection not allowed by ruleset"); break;
973 case 3: strcat(buf, "Network unreachable"); break;
974 case 4: strcat(buf, "Host unreachable"); break;
975 case 5: strcat(buf, "Connection refused"); break;
976 case 6: strcat(buf, "TTL expired"); break;
977 case 7: strcat(buf, "Command not supported"); break;
978 case 8: strcat(buf, "Address type not supported"); break;
979 default: sprintf(buf+strlen(buf),
980 "Unrecognised SOCKS error code %d",
984 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
990 * Eat the rest of the reply packet.
992 len = 6; /* first 4 bytes, last 2 */
994 case 1: len += 4; break; /* IPv4 address */
995 case 4: len += 16; break;/* IPv6 address */
996 case 3: len += (unsigned char)data[4]; break; /* domain name */
998 plug_closing(p->plug, "Proxy error: SOCKS proxy returned "
999 "unrecognised address format",
1000 PROXY_ERROR_GENERAL, 0);
1003 if (bufchain_size(&p->pending_input_data) < len)
1004 return 1; /* not got whole reply yet */
1005 bufchain_consume(&p->pending_input_data, len);
1012 if (p->state == 4) {
1013 /* TODO: Handle GSSAPI authentication */
1014 plug_closing(p->plug, "Proxy error: We don't support GSSAPI authentication",
1015 PROXY_ERROR_GENERAL, 0);
1019 if (p->state == 5) {
1020 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
1021 char userpwbuf[514];
1023 ulen = strlen(cfg.proxy_username);
1024 if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
1025 plen = strlen(cfg.proxy_password);
1026 if (plen > 255) plen = 255; if (plen < 1) plen = 1;
1027 userpwbuf[0] = 1; /* version number of subnegotiation */
1028 userpwbuf[1] = ulen;
1029 memcpy(userpwbuf+2, cfg.proxy_username, ulen);
1030 userpwbuf[ulen+2] = plen;
1031 memcpy(userpwbuf+ulen+3, cfg.proxy_password, plen);
1032 sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);
1035 plug_closing(p->plug, "Proxy error: Server chose "
1036 "username/password authentication but we "
1038 PROXY_ERROR_GENERAL, 0);
1042 if (p->state == 6) {
1043 /* TODO: Handle CHAP authentication */
1044 plug_closing(p->plug, "Proxy error: We don't support CHAP authentication",
1045 PROXY_ERROR_GENERAL, 0);
1051 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
1052 PROXY_ERROR_UNEXPECTED, 0);
1056 /* ----------------------------------------------------------------------
1057 * `Telnet' proxy type.
1059 * (This is for ad-hoc proxies where you connect to the proxy's
1060 * telnet port and send a command such as `connect host port'. The
1061 * command is configurable, since this proxy type is typically not
1062 * standardised or at all well-defined.)
1065 int proxy_telnet_negotiate (Proxy_Socket p, int change)
1067 if (p->state == PROXY_CHANGE_NEW) {
1071 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1072 * %%, %host, and %port
1075 while (cfg.proxy_telnet_command[eo] != 0) {
1077 /* scan forward until we hit end-of-line,
1078 * or an escape character (\ or %) */
1079 while (cfg.proxy_telnet_command[eo] != 0 &&
1080 cfg.proxy_telnet_command[eo] != '%' &&
1081 cfg.proxy_telnet_command[eo] != '\\') eo++;
1083 /* if we hit eol, break out of our escaping loop */
1084 if (cfg.proxy_telnet_command[eo] == 0) break;
1086 /* if there was any unescaped text before the escape
1087 * character, send that now */
1089 sk_write(p->sub_socket,
1090 cfg.proxy_telnet_command + so, eo - so);
1095 /* if the escape character was the last character of
1096 * the line, we'll just stop and send it. */
1097 if (cfg.proxy_telnet_command[eo] == 0) break;
1099 if (cfg.proxy_telnet_command[so] == '\\') {
1101 /* we recognize \\, \%, \r, \n, \t, \x??.
1102 * anything else, we just send unescaped (including the \).
1105 switch (cfg.proxy_telnet_command[eo]) {
1108 sk_write(p->sub_socket, "\\", 1);
1113 sk_write(p->sub_socket, "%%", 1);
1118 sk_write(p->sub_socket, "\r", 1);
1123 sk_write(p->sub_socket, "\n", 1);
1128 sk_write(p->sub_socket, "\t", 1);
1135 /* escaped hexadecimal value (ie. \xff) */
1136 unsigned char v = 0;
1141 if (cfg.proxy_telnet_command[eo] >= '0' &&
1142 cfg.proxy_telnet_command[eo] <= '9')
1143 v += cfg.proxy_telnet_command[eo] - '0';
1144 else if (cfg.proxy_telnet_command[eo] >= 'a' &&
1145 cfg.proxy_telnet_command[eo] <= 'f')
1146 v += cfg.proxy_telnet_command[eo] - 'a' + 10;
1147 else if (cfg.proxy_telnet_command[eo] >= 'A' &&
1148 cfg.proxy_telnet_command[eo] <= 'F')
1149 v += cfg.proxy_telnet_command[eo] - 'A' + 10;
1151 /* non hex character, so we abort and just
1152 * send the whole thing unescaped (including \x)
1154 sk_write(p->sub_socket, "\\", 1);
1159 /* we only extract two hex characters */
1161 sk_write(p->sub_socket, &v, 1);
1173 sk_write(p->sub_socket,
1174 cfg.proxy_telnet_command + so, 2);
1180 /* % escape. we recognize %%, %host, %port. anything else,
1181 * we just send unescaped (including the %). */
1183 if (cfg.proxy_telnet_command[eo] == '%') {
1184 sk_write(p->sub_socket, "%", 1);
1187 else if (strnicmp(cfg.proxy_telnet_command + eo,
1190 sk_getaddr(p->remote_addr, dest, 64);
1191 sk_write(p->sub_socket, dest, strlen(dest));
1194 else if (strnicmp(cfg.proxy_telnet_command + eo,
1197 sprintf(port, "%i", p->remote_port);
1198 sk_write(p->sub_socket, port, strlen(port));
1202 /* we don't escape this, so send the % now, and
1203 * don't advance eo, so that we'll consider the
1204 * text immediately following the % as unescaped.
1206 sk_write(p->sub_socket, "%", 1);
1210 /* resume scanning for additional escapes after this one. */
1214 /* if there is any unescaped text at the end of the line, send it */
1216 sk_write(p->sub_socket, cfg.proxy_telnet_command + so, eo - so);
1223 if (change == PROXY_CHANGE_CLOSING) {
1224 /* if our proxy negotiation process involves closing and opening
1225 * new sockets, then we would want to intercept this closing
1226 * callback when we were expecting it. if we aren't anticipating
1227 * a socket close, then some error must have occurred. we'll
1228 * just pass those errors up to the backend.
1230 return plug_closing(p->plug, p->closing_error_msg,
1231 p->closing_error_code,
1232 p->closing_calling_back);
1235 if (change == PROXY_CHANGE_SENT) {
1236 /* some (or all) of what we wrote to the proxy was sent.
1237 * we don't do anything new, however, until we receive the
1238 * proxy's response. we might want to set a timer so we can
1239 * timeout the proxy negotiation after a while...
1244 if (change == PROXY_CHANGE_ACCEPTING) {
1245 /* we should _never_ see this, as we are using our socket to
1246 * connect to a proxy, not accepting inbound connections.
1247 * what should we do? close the socket with an appropriate
1250 return plug_accepting(p->plug, p->accepting_sock);
1253 if (change == PROXY_CHANGE_RECEIVE) {
1254 /* we have received data from the underlying socket, which
1255 * we'll need to parse, process, and respond to appropriately.
1260 /* proxy activate will have dealt with
1261 * whatever is left of the buffer */
1265 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
1266 PROXY_ERROR_UNEXPECTED, 0);