2 * Network proxy abstraction in PuTTY
4 * A proxy layer, if necessary, wedges itself between the network
5 * code and the higher level backend.
14 #define DEFINE_PLUG_METHOD_MACROS
20 * Call this when proxy negotiation is complete, so that this
21 * socket can begin working normally.
23 void proxy_activate (Proxy_Socket p)
27 long output_before, output_after;
29 p->state = PROXY_STATE_ACTIVE;
31 /* we want to ignore new receive events until we have sent
32 * all of our buffered receive data.
34 sk_set_frozen(p->sub_socket, 1);
36 /* how many bytes of output have we buffered? */
37 output_before = bufchain_size(&p->pending_oob_output_data) +
38 bufchain_size(&p->pending_output_data);
39 /* and keep track of how many bytes do not get sent. */
42 /* send buffered OOB writes */
43 while (bufchain_size(&p->pending_oob_output_data) > 0) {
44 bufchain_prefix(&p->pending_oob_output_data, &data, &len);
45 output_after += sk_write_oob(p->sub_socket, data, len);
46 bufchain_consume(&p->pending_oob_output_data, len);
49 /* send buffered normal writes */
50 while (bufchain_size(&p->pending_output_data) > 0) {
51 bufchain_prefix(&p->pending_output_data, &data, &len);
52 output_after += sk_write(p->sub_socket, data, len);
53 bufchain_consume(&p->pending_output_data, len);
56 /* if we managed to send any data, let the higher levels know. */
57 if (output_after < output_before)
58 plug_sent(p->plug, output_after);
60 /* if we were asked to flush the output during
61 * the proxy negotiation process, do so now.
63 if (p->pending_flush) sk_flush(p->sub_socket);
65 /* if the backend wanted the socket unfrozen, try to unfreeze.
66 * our set_frozen handler will flush buffered receive data before
67 * unfreezing the actual underlying socket.
70 sk_set_frozen((Socket)p, 0);
73 /* basic proxy socket functions */
75 static Plug sk_proxy_plug (Socket s, Plug p)
77 Proxy_Socket ps = (Proxy_Socket) s;
84 static void sk_proxy_close (Socket s)
86 Proxy_Socket ps = (Proxy_Socket) s;
88 sk_close(ps->sub_socket);
92 static int sk_proxy_write (Socket s, char *data, int len)
94 Proxy_Socket ps = (Proxy_Socket) s;
96 if (ps->state != PROXY_STATE_ACTIVE) {
97 bufchain_add(&ps->pending_output_data, data, len);
98 return bufchain_size(&ps->pending_output_data);
100 return sk_write(ps->sub_socket, data, len);
103 static int sk_proxy_write_oob (Socket s, char *data, int len)
105 Proxy_Socket ps = (Proxy_Socket) s;
107 if (ps->state != PROXY_STATE_ACTIVE) {
108 bufchain_clear(&ps->pending_output_data);
109 bufchain_clear(&ps->pending_oob_output_data);
110 bufchain_add(&ps->pending_oob_output_data, data, len);
113 return sk_write_oob(ps->sub_socket, data, len);
116 static void sk_proxy_flush (Socket s)
118 Proxy_Socket ps = (Proxy_Socket) s;
120 if (ps->state != PROXY_STATE_ACTIVE) {
121 ps->pending_flush = 1;
124 sk_flush(ps->sub_socket);
127 static void sk_proxy_set_private_ptr (Socket s, void *ptr)
129 Proxy_Socket ps = (Proxy_Socket) s;
130 sk_set_private_ptr(ps->sub_socket, ptr);
133 static void * sk_proxy_get_private_ptr (Socket s)
135 Proxy_Socket ps = (Proxy_Socket) s;
136 return sk_get_private_ptr(ps->sub_socket);
139 static void sk_proxy_set_frozen (Socket s, int is_frozen)
141 Proxy_Socket ps = (Proxy_Socket) s;
143 if (ps->state != PROXY_STATE_ACTIVE) {
144 ps->freeze = is_frozen;
148 /* handle any remaining buffered recv data first */
149 if (bufchain_size(&ps->pending_input_data) > 0) {
150 ps->freeze = is_frozen;
152 /* loop while we still have buffered data, and while we are
153 * unfrozen. the plug_receive call in the loop could result
154 * in a call back into this function refreezing the socket,
155 * so we have to check each time.
157 while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {
160 bufchain_prefix(&ps->pending_input_data, &data, &len);
161 plug_receive(ps->plug, 0, data, len);
162 bufchain_consume(&ps->pending_input_data, len);
165 /* if we're still frozen, we'll have to wait for another
166 * call from the backend to finish unbuffering the data.
168 if (ps->freeze) return;
171 sk_set_frozen(ps->sub_socket, is_frozen);
174 static char * sk_proxy_socket_error (Socket s)
176 Proxy_Socket ps = (Proxy_Socket) s;
177 if (ps->error != NULL || ps->sub_socket == NULL) {
180 return sk_socket_error(ps->sub_socket);
183 /* basic proxy plug functions */
185 static int plug_proxy_closing (Plug p, char *error_msg,
186 int error_code, int calling_back)
188 Proxy_Plug pp = (Proxy_Plug) p;
189 Proxy_Socket ps = pp->proxy_socket;
191 if (ps->state != PROXY_STATE_ACTIVE) {
192 ps->closing_error_msg = error_msg;
193 ps->closing_error_code = error_code;
194 ps->closing_calling_back = calling_back;
195 return ps->negotiate(ps, PROXY_CHANGE_CLOSING);
197 return plug_closing(ps->plug, error_msg,
198 error_code, calling_back);
201 static int plug_proxy_receive (Plug p, int urgent, char *data, int len)
203 Proxy_Plug pp = (Proxy_Plug) p;
204 Proxy_Socket ps = pp->proxy_socket;
206 if (ps->state != PROXY_STATE_ACTIVE) {
207 /* we will lose the urgentness of this data, but since most,
208 * if not all, of this data will be consumed by the negotiation
209 * process, hopefully it won't affect the protocol above us
211 bufchain_add(&ps->pending_input_data, data, len);
212 ps->receive_urgent = urgent;
213 ps->receive_data = data;
214 ps->receive_len = len;
215 return ps->negotiate(ps, PROXY_CHANGE_RECEIVE);
217 return plug_receive(ps->plug, urgent, data, len);
220 static void plug_proxy_sent (Plug p, int bufsize)
222 Proxy_Plug pp = (Proxy_Plug) p;
223 Proxy_Socket ps = pp->proxy_socket;
225 if (ps->state != PROXY_STATE_ACTIVE) {
226 ps->sent_bufsize = bufsize;
227 ps->negotiate(ps, PROXY_CHANGE_SENT);
230 plug_sent(ps->plug, bufsize);
233 static int plug_proxy_accepting (Plug p, void *sock)
235 Proxy_Plug pp = (Proxy_Plug) p;
236 Proxy_Socket ps = pp->proxy_socket;
238 if (ps->state != PROXY_STATE_ACTIVE) {
239 ps->accepting_sock = sock;
240 return ps->negotiate(ps, PROXY_CHANGE_ACCEPTING);
242 return plug_accepting(ps->plug, sock);
245 static int proxy_for_destination (SockAddr addr, char * hostname, int port)
249 int hostip_len, hostname_len;
252 /* we want a string representation of the IP address for comparisons */
253 sk_getaddr(addr, hostip, 64);
255 hostip_len = strlen(hostip);
256 hostname_len = strlen(hostname);
258 exclude_list = cfg.proxy_exclude_list;
260 /* now parse the exclude list, and see if either our IP
261 * or hostname matches anything in it.
264 while (exclude_list[s]) {
265 while (exclude_list[s] &&
266 (isspace(exclude_list[s]) ||
267 exclude_list[s] == ',')) s++;
269 if (!exclude_list[s]) break;
273 while (exclude_list[e] &&
274 (isalnum(exclude_list[e]) ||
275 exclude_list[e] == '-' ||
276 exclude_list[e] == '.' ||
277 exclude_list[e] == '*')) e++;
279 if (exclude_list[s] == '*') {
280 /* wildcard at beginning of entry */
282 if (strnicmp(hostip + hostip_len - (e - s - 1),
283 exclude_list + s + 1, e - s - 1) == 0 ||
284 strnicmp(hostname + hostname_len - (e - s - 1),
285 exclude_list + s + 1, e - s - 1) == 0)
286 return 0; /* IP/hostname range excluded. do not use proxy. */
288 } else if (exclude_list[e-1] == '*') {
289 /* wildcard at end of entry */
291 if (strnicmp(hostip, exclude_list + s, e - s - 1) == 0 ||
292 strnicmp(hostname, exclude_list + s, e - s - 1) == 0)
293 return 0; /* IP/hostname range excluded. do not use proxy. */
296 /* no wildcard at either end, so let's try an absolute
297 * match (ie. a specific IP)
300 if (stricmp(hostip, exclude_list + s) == 0)
301 return 0; /* IP/hostname excluded. do not use proxy. */
302 if (stricmp(hostname, exclude_list + s) == 0)
303 return 0; /* IP/hostname excluded. do not use proxy. */
309 /* no matches in the exclude list, so use the proxy */
313 Socket new_connection(SockAddr addr, char *hostname,
314 int port, int privport,
315 int oobinline, int nodelay, Plug plug)
317 static struct socket_function_table socket_fn_table = {
323 sk_proxy_set_private_ptr,
324 sk_proxy_get_private_ptr,
326 sk_proxy_socket_error
329 static struct plug_function_table plug_fn_table = {
336 if (cfg.proxy_type != PROXY_NONE &&
337 proxy_for_destination(addr, hostname, port))
342 char * proxy_canonical_name;
344 ret = smalloc(sizeof(struct Socket_proxy_tag));
345 ret->fn = &socket_fn_table;
347 ret->remote_addr = addr;
348 ret->remote_port = port;
351 ret->pending_flush = 0;
354 bufchain_init(&ret->pending_input_data);
355 bufchain_init(&ret->pending_output_data);
356 bufchain_init(&ret->pending_oob_output_data);
358 ret->sub_socket = NULL;
359 ret->state = PROXY_STATE_NEW;
360 ret->negotiate = NULL;
362 if (cfg.proxy_type == PROXY_HTTP) {
363 ret->negotiate = proxy_http_negotiate;
364 } else if (cfg.proxy_type == PROXY_SOCKS) {
365 if (cfg.proxy_socks_version == 4)
366 ret->negotiate = proxy_socks4_negotiate;
368 ret->negotiate = proxy_socks5_negotiate;
369 } else if (cfg.proxy_type == PROXY_TELNET) {
370 ret->negotiate = proxy_telnet_negotiate;
372 ret->error = "Proxy error: Unknown proxy method";
376 /* create the proxy plug to map calls from the actual
377 * socket into our proxy socket layer */
378 pplug = smalloc(sizeof(struct Plug_proxy_tag));
379 pplug->fn = &plug_fn_table;
380 pplug->proxy_socket = ret;
383 proxy_addr = sk_namelookup(cfg.proxy_host,
384 &proxy_canonical_name);
385 sfree(proxy_canonical_name);
387 /* create the actual socket we will be using,
388 * connected to our proxy server and port.
390 ret->sub_socket = sk_new(proxy_addr, cfg.proxy_port,
392 nodelay, (Plug) pplug);
393 if (sk_socket_error(ret->sub_socket) != NULL)
396 sk_addr_free(proxy_addr);
398 /* start the proxy negotiation process... */
399 sk_set_frozen(ret->sub_socket, 0);
400 ret->negotiate(ret, PROXY_CHANGE_NEW);
405 /* no proxy, so just return the direct socket */
406 return sk_new(addr, port, privport, oobinline, nodelay, plug);
409 Socket new_listener(int port, Plug plug, int local_host_only)
411 /* TODO: SOCKS (and potentially others) support inbound
412 * TODO: connections via the proxy. support them.
415 return sk_newlistener(port, plug, local_host_only);
418 /* ----------------------------------------------------------------------
419 * HTTP CONNECT proxy type.
422 static int get_line_end (char * data, int len)
428 if (data[off] == '\n') {
429 /* we have a newline */
432 /* is that the only thing on this line? */
433 if (off <= 2) return off;
435 /* if not, then there is the possibility that this header
436 * continues onto the next line, if it starts with a space
441 data[off+1] != ' ' &&
442 data[off+1] != '\t') return off;
444 /* the line does continue, so we have to keep going
445 * until we see an the header's "real" end of line.
456 int proxy_http_negotiate (Proxy_Socket p, int change)
458 if (p->state == PROXY_STATE_NEW) {
459 /* we are just beginning the proxy negotiate process,
460 * so we'll send off the initial bits of the request.
461 * for this proxy method, it's just a simple HTTP
464 char buf[256], dest[64];
466 sk_getaddr(p->remote_addr, dest, 64);
468 sprintf(buf, "CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
469 dest, p->remote_port, dest, p->remote_port);
470 sk_write(p->sub_socket, buf, strlen(buf));
472 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
473 char buf[sizeof(cfg.proxy_username)+sizeof(cfg.proxy_password)];
474 char buf2[sizeof(buf)*4/3 + 100];
476 sprintf(buf, "%s:%s", cfg.proxy_username, cfg.proxy_password);
478 sprintf(buf2, "Proxy-Authorization: basic ");
479 for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)
480 base64_encode_atom(buf+i, (len-i > 3 ? 3 : len-i), buf2+j);
481 strcpy(buf2+j, "\r\n");
482 sk_write(p->sub_socket, buf2, strlen(buf2));
485 sprintf(buf, "\r\n");
486 sk_write(p->sub_socket, buf, strlen(buf));
492 if (change == PROXY_CHANGE_CLOSING) {
493 /* if our proxy negotiation process involves closing and opening
494 * new sockets, then we would want to intercept this closing
495 * callback when we were expecting it. if we aren't anticipating
496 * a socket close, then some error must have occurred. we'll
497 * just pass those errors up to the backend.
499 return plug_closing(p->plug, p->closing_error_msg,
500 p->closing_error_code,
501 p->closing_calling_back);
504 if (change == PROXY_CHANGE_SENT) {
505 /* some (or all) of what we wrote to the proxy was sent.
506 * we don't do anything new, however, until we receive the
507 * proxy's response. we might want to set a timer so we can
508 * timeout the proxy negotiation after a while...
513 if (change == PROXY_CHANGE_ACCEPTING) {
514 /* we should _never_ see this, as we are using our socket to
515 * connect to a proxy, not accepting inbound connections.
516 * what should we do? close the socket with an appropriate
519 return plug_accepting(p->plug, p->accepting_sock);
522 if (change == PROXY_CHANGE_RECEIVE) {
523 /* we have received data from the underlying socket, which
524 * we'll need to parse, process, and respond to appropriately.
533 int min_ver, maj_ver, status;
535 /* get the status line */
536 len = bufchain_size(&p->pending_input_data);
537 assert(len > 0); /* or we wouldn't be here */
539 bufchain_fetch(&p->pending_input_data, data, len);
541 eol = get_line_end(data, len);
548 /* We can't rely on whether the %n incremented the sscanf return */
549 if (sscanf((char *)data, "HTTP/%i.%i %n",
550 &maj_ver, &min_ver, &status) < 2 || status == -1) {
551 plug_closing(p->plug, "Proxy error: HTTP response was absent",
552 PROXY_ERROR_GENERAL, 0);
557 /* remove the status line from the input buffer. */
558 bufchain_consume(&p->pending_input_data, eol);
559 if (data[status] != '2') {
563 while (eol > status &&
564 (data[eol-1] == '\r' || data[eol-1] == '\n'))
566 sprintf(buf, "Proxy error: %.900s",
568 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
580 /* get headers. we're done when we get a
581 * header of length 2, (ie. just "\r\n")
584 len = bufchain_size(&p->pending_input_data);
585 assert(len > 0); /* or we wouldn't be here */
588 bufchain_fetch(&p->pending_input_data, data, len);
590 eol = get_line_end(datap, len);
597 bufchain_consume(&p->pending_input_data, eol);
599 eol = get_line_end(datap, len);
604 bufchain_consume(&p->pending_input_data, 2);
606 /* proxy activate will have dealt with
607 * whatever is left of the buffer */
617 plug_closing(p->plug, "Proxy error: unexpected proxy error",
618 PROXY_ERROR_UNEXPECTED, 0);
622 /* ----------------------------------------------------------------------
626 /* SOCKS version 4 */
627 int proxy_socks4_negotiate (Proxy_Socket p, int change)
629 if (p->state == PROXY_CHANGE_NEW) {
632 * version number (1 byte) = 4
633 * command code (1 byte)
636 * dest. port (2 bytes) [network order]
637 * dest. address (4 bytes)
638 * user ID (variable length, null terminated string)
644 if (sk_addrtype(p->remote_addr) != AF_INET) {
645 plug_closing(p->plug, "Proxy error: SOCKS version 4 does"
646 " not support IPv6", PROXY_ERROR_GENERAL, 0);
650 length = strlen(cfg.proxy_username) + 9;
651 command = (char*) smalloc(length);
652 strcpy(command + 8, cfg.proxy_username);
654 command[0] = 4; /* version 4 */
655 command[1] = 1; /* CONNECT command */
658 command[2] = (char) (p->remote_port >> 8) & 0xff;
659 command[3] = (char) p->remote_port & 0xff;
662 sk_addrcopy(p->remote_addr, command + 4);
664 sk_write(p->sub_socket, command, length);
671 if (change == PROXY_CHANGE_CLOSING) {
672 /* if our proxy negotiation process involves closing and opening
673 * new sockets, then we would want to intercept this closing
674 * callback when we were expecting it. if we aren't anticipating
675 * a socket close, then some error must have occurred. we'll
676 * just pass those errors up to the backend.
678 return plug_closing(p->plug, p->closing_error_msg,
679 p->closing_error_code,
680 p->closing_calling_back);
683 if (change == PROXY_CHANGE_SENT) {
684 /* some (or all) of what we wrote to the proxy was sent.
685 * we don't do anything new, however, until we receive the
686 * proxy's response. we might want to set a timer so we can
687 * timeout the proxy negotiation after a while...
692 if (change == PROXY_CHANGE_ACCEPTING) {
693 /* we should _never_ see this, as we are using our socket to
694 * connect to a proxy, not accepting inbound connections.
695 * what should we do? close the socket with an appropriate
698 return plug_accepting(p->plug, p->accepting_sock);
701 if (change == PROXY_CHANGE_RECEIVE) {
702 /* we have received data from the underlying socket, which
703 * we'll need to parse, process, and respond to appropriately.
708 * version number (1 byte) = 4
709 * reply code (1 byte)
710 * 90 = request granted
711 * 91 = request rejected or failed
712 * 92 = request rejected due to lack of IDENTD on client
713 * 93 = request rejected due to difference in user ID
714 * (what we sent vs. what IDENTD said)
715 * dest. port (2 bytes)
716 * dest. address (4 bytes)
721 if (bufchain_size(&p->pending_input_data) < 8)
722 return 1; /* not got anything yet */
724 /* get the response */
725 bufchain_fetch(&p->pending_input_data, data, 8);
728 plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "
729 "unexpected reply code version",
730 PROXY_ERROR_GENERAL, 0);
738 plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",
739 PROXY_ERROR_GENERAL, 0);
742 plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",
743 PROXY_ERROR_GENERAL, 0);
747 plug_closing(p->plug, "Proxy error: Error while communicating with proxy",
748 PROXY_ERROR_GENERAL, 0);
754 bufchain_consume(&p->pending_input_data, 8);
758 /* proxy activate will have dealt with
759 * whatever is left of the buffer */
764 plug_closing(p->plug, "Proxy error: unexpected proxy error",
765 PROXY_ERROR_UNEXPECTED, 0);
769 /* SOCKS version 5 */
770 int proxy_socks5_negotiate (Proxy_Socket p, int change)
772 if (p->state == PROXY_CHANGE_NEW) {
775 * version number (1 byte) = 5
776 * number of available authentication methods (1 byte)
777 * available authentication methods (1 byte * previous value)
778 * authentication methods:
779 * 0x00 = no authentication
781 * 0x02 = username/password
788 command[0] = 5; /* version 5 */
789 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
790 command[1] = 2; /* two methods supported: */
791 command[2] = 0x00; /* no authentication */
792 command[3] = 0x02; /* username/password */
795 command[1] = 1; /* one methods supported: */
796 command[2] = 0x00; /* no authentication */
800 sk_write(p->sub_socket, command, len);
806 if (change == PROXY_CHANGE_CLOSING) {
807 /* if our proxy negotiation process involves closing and opening
808 * new sockets, then we would want to intercept this closing
809 * callback when we were expecting it. if we aren't anticipating
810 * a socket close, then some error must have occurred. we'll
811 * just pass those errors up to the backend.
813 return plug_closing(p->plug, p->closing_error_msg,
814 p->closing_error_code,
815 p->closing_calling_back);
818 if (change == PROXY_CHANGE_SENT) {
819 /* some (or all) of what we wrote to the proxy was sent.
820 * we don't do anything new, however, until we receive the
821 * proxy's response. we might want to set a timer so we can
822 * timeout the proxy negotiation after a while...
827 if (change == PROXY_CHANGE_ACCEPTING) {
828 /* we should _never_ see this, as we are using our socket to
829 * connect to a proxy, not accepting inbound connections.
830 * what should we do? close the socket with an appropriate
833 return plug_accepting(p->plug, p->accepting_sock);
836 if (change == PROXY_CHANGE_RECEIVE) {
837 /* we have received data from the underlying socket, which
838 * we'll need to parse, process, and respond to appropriately.
844 * version number (1 byte) = 5
845 * authentication method (1 byte)
846 * authentication methods:
847 * 0x00 = no authentication
849 * 0x02 = username/password
851 * 0xff = no acceptable methods
855 if (bufchain_size(&p->pending_input_data) < 2)
856 return 1; /* not got anything yet */
858 /* get the response */
859 bufchain_fetch(&p->pending_input_data, data, 2);
862 plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",
863 PROXY_ERROR_GENERAL, 0);
867 if (data[1] == 0x00) p->state = 2; /* no authentication needed */
868 else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */
869 else if (data[1] == 0x02) p->state = 5; /* username/password authentication */
870 else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */
872 plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",
873 PROXY_ERROR_GENERAL, 0);
876 bufchain_consume(&p->pending_input_data, 2);
881 /* password authentication reply format:
882 * version number (1 bytes) = 1
883 * reply code (1 byte)
889 if (bufchain_size(&p->pending_input_data) < 2)
890 return 1; /* not got anything yet */
892 /* get the response */
893 bufchain_fetch(&p->pending_input_data, data, 2);
896 plug_closing(p->plug, "Proxy error: SOCKS password "
897 "subnegotiation contained wrong version number",
898 PROXY_ERROR_GENERAL, 0);
904 plug_closing(p->plug, "Proxy error: SOCKS proxy refused"
905 " password authentication",
906 PROXY_ERROR_GENERAL, 0);
910 bufchain_consume(&p->pending_input_data, 2);
911 p->state = 2; /* now proceed as authenticated */
917 * version number (1 byte) = 5
918 * command code (1 byte)
922 * reserved (1 byte) = 0x00
923 * address type (1 byte)
925 * 3 = domainname (first byte has length, no terminating null)
927 * dest. address (variable)
928 * dest. port (2 bytes) [network order]
934 if (sk_addrtype(p->remote_addr) == AF_INET) {
936 command[3] = 1; /* IPv4 */
939 command[3] = 4; /* IPv6 */
942 command[0] = 5; /* version 5 */
943 command[1] = 1; /* CONNECT command */
947 sk_addrcopy(p->remote_addr, command+4);
950 command[len-2] = (char) (p->remote_port >> 8) & 0xff;
951 command[len-1] = (char) p->remote_port & 0xff;
953 sk_write(p->sub_socket, command, len);
962 * version number (1 bytes) = 5
963 * reply code (1 byte)
965 * 1 = general SOCKS server failure
966 * 2 = connection not allowed by ruleset
967 * 3 = network unreachable
968 * 4 = host unreachable
969 * 5 = connection refused
971 * 7 = command not supported
972 * 8 = address type not supported
973 * reserved (1 byte) = x00
974 * address type (1 byte)
976 * 3 = domainname (first byte has length, no terminating null)
978 * server bound address (variable)
979 * server bound port (2 bytes) [network order]
984 /* First 5 bytes of packet are enough to tell its length. */
985 if (bufchain_size(&p->pending_input_data) < 5)
986 return 1; /* not got anything yet */
988 /* get the response */
989 bufchain_fetch(&p->pending_input_data, data, 5);
992 plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",
993 PROXY_ERROR_GENERAL, 0);
1000 strcpy(buf, "Proxy error: ");
1003 case 1: strcat(buf, "General SOCKS server failure"); break;
1004 case 2: strcat(buf, "Connection not allowed by ruleset"); break;
1005 case 3: strcat(buf, "Network unreachable"); break;
1006 case 4: strcat(buf, "Host unreachable"); break;
1007 case 5: strcat(buf, "Connection refused"); break;
1008 case 6: strcat(buf, "TTL expired"); break;
1009 case 7: strcat(buf, "Command not supported"); break;
1010 case 8: strcat(buf, "Address type not supported"); break;
1011 default: sprintf(buf+strlen(buf),
1012 "Unrecognised SOCKS error code %d",
1016 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
1022 * Eat the rest of the reply packet.
1024 len = 6; /* first 4 bytes, last 2 */
1026 case 1: len += 4; break; /* IPv4 address */
1027 case 4: len += 16; break;/* IPv6 address */
1028 case 3: len += (unsigned char)data[4]; break; /* domain name */
1030 plug_closing(p->plug, "Proxy error: SOCKS proxy returned "
1031 "unrecognised address format",
1032 PROXY_ERROR_GENERAL, 0);
1035 if (bufchain_size(&p->pending_input_data) < len)
1036 return 1; /* not got whole reply yet */
1037 bufchain_consume(&p->pending_input_data, len);
1044 if (p->state == 4) {
1045 /* TODO: Handle GSSAPI authentication */
1046 plug_closing(p->plug, "Proxy error: We don't support GSSAPI authentication",
1047 PROXY_ERROR_GENERAL, 0);
1051 if (p->state == 5) {
1052 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
1053 char userpwbuf[514];
1055 ulen = strlen(cfg.proxy_username);
1056 if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
1057 plen = strlen(cfg.proxy_password);
1058 if (plen > 255) plen = 255; if (plen < 1) plen = 1;
1059 userpwbuf[0] = 1; /* version number of subnegotiation */
1060 userpwbuf[1] = ulen;
1061 memcpy(userpwbuf+2, cfg.proxy_username, ulen);
1062 userpwbuf[ulen+2] = plen;
1063 memcpy(userpwbuf+ulen+3, cfg.proxy_password, plen);
1064 sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);
1067 plug_closing(p->plug, "Proxy error: Server chose "
1068 "username/password authentication but we "
1070 PROXY_ERROR_GENERAL, 0);
1074 if (p->state == 6) {
1075 /* TODO: Handle CHAP authentication */
1076 plug_closing(p->plug, "Proxy error: We don't support CHAP authentication",
1077 PROXY_ERROR_GENERAL, 0);
1083 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
1084 PROXY_ERROR_UNEXPECTED, 0);
1088 /* ----------------------------------------------------------------------
1089 * `Telnet' proxy type.
1091 * (This is for ad-hoc proxies where you connect to the proxy's
1092 * telnet port and send a command such as `connect host port'. The
1093 * command is configurable, since this proxy type is typically not
1094 * standardised or at all well-defined.)
1097 int proxy_telnet_negotiate (Proxy_Socket p, int change)
1099 if (p->state == PROXY_CHANGE_NEW) {
1103 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1104 * %%, %host, %port, %user, and %pass
1107 while (cfg.proxy_telnet_command[eo] != 0) {
1109 /* scan forward until we hit end-of-line,
1110 * or an escape character (\ or %) */
1111 while (cfg.proxy_telnet_command[eo] != 0 &&
1112 cfg.proxy_telnet_command[eo] != '%' &&
1113 cfg.proxy_telnet_command[eo] != '\\') eo++;
1115 /* if we hit eol, break out of our escaping loop */
1116 if (cfg.proxy_telnet_command[eo] == 0) break;
1118 /* if there was any unescaped text before the escape
1119 * character, send that now */
1121 sk_write(p->sub_socket,
1122 cfg.proxy_telnet_command + so, eo - so);
1127 /* if the escape character was the last character of
1128 * the line, we'll just stop and send it. */
1129 if (cfg.proxy_telnet_command[eo] == 0) break;
1131 if (cfg.proxy_telnet_command[so] == '\\') {
1133 /* we recognize \\, \%, \r, \n, \t, \x??.
1134 * anything else, we just send unescaped (including the \).
1137 switch (cfg.proxy_telnet_command[eo]) {
1140 sk_write(p->sub_socket, "\\", 1);
1145 sk_write(p->sub_socket, "%%", 1);
1150 sk_write(p->sub_socket, "\r", 1);
1155 sk_write(p->sub_socket, "\n", 1);
1160 sk_write(p->sub_socket, "\t", 1);
1167 /* escaped hexadecimal value (ie. \xff) */
1168 unsigned char v = 0;
1173 if (cfg.proxy_telnet_command[eo] >= '0' &&
1174 cfg.proxy_telnet_command[eo] <= '9')
1175 v += cfg.proxy_telnet_command[eo] - '0';
1176 else if (cfg.proxy_telnet_command[eo] >= 'a' &&
1177 cfg.proxy_telnet_command[eo] <= 'f')
1178 v += cfg.proxy_telnet_command[eo] - 'a' + 10;
1179 else if (cfg.proxy_telnet_command[eo] >= 'A' &&
1180 cfg.proxy_telnet_command[eo] <= 'F')
1181 v += cfg.proxy_telnet_command[eo] - 'A' + 10;
1183 /* non hex character, so we abort and just
1184 * send the whole thing unescaped (including \x)
1186 sk_write(p->sub_socket, "\\", 1);
1191 /* we only extract two hex characters */
1193 sk_write(p->sub_socket, &v, 1);
1205 sk_write(p->sub_socket,
1206 cfg.proxy_telnet_command + so, 2);
1212 /* % escape. we recognize %%, %host, %port, %user, %pass.
1213 * anything else, we just send unescaped (including the %).
1216 if (cfg.proxy_telnet_command[eo] == '%') {
1217 sk_write(p->sub_socket, "%", 1);
1220 else if (strnicmp(cfg.proxy_telnet_command + eo,
1223 sk_getaddr(p->remote_addr, dest, 64);
1224 sk_write(p->sub_socket, dest, strlen(dest));
1227 else if (strnicmp(cfg.proxy_telnet_command + eo,
1230 sprintf(port, "%i", p->remote_port);
1231 sk_write(p->sub_socket, port, strlen(port));
1234 else if (strnicmp(cfg.proxy_telnet_command + eo,
1236 sk_write(p->sub_socket, cfg.proxy_username,
1237 strlen(cfg.proxy_username));
1240 else if (strnicmp(cfg.proxy_telnet_command + eo,
1242 sk_write(p->sub_socket, cfg.proxy_password,
1243 strlen(cfg.proxy_password));
1247 /* we don't escape this, so send the % now, and
1248 * don't advance eo, so that we'll consider the
1249 * text immediately following the % as unescaped.
1251 sk_write(p->sub_socket, "%", 1);
1255 /* resume scanning for additional escapes after this one. */
1259 /* if there is any unescaped text at the end of the line, send it */
1261 sk_write(p->sub_socket, cfg.proxy_telnet_command + so, eo - so);
1268 if (change == PROXY_CHANGE_CLOSING) {
1269 /* if our proxy negotiation process involves closing and opening
1270 * new sockets, then we would want to intercept this closing
1271 * callback when we were expecting it. if we aren't anticipating
1272 * a socket close, then some error must have occurred. we'll
1273 * just pass those errors up to the backend.
1275 return plug_closing(p->plug, p->closing_error_msg,
1276 p->closing_error_code,
1277 p->closing_calling_back);
1280 if (change == PROXY_CHANGE_SENT) {
1281 /* some (or all) of what we wrote to the proxy was sent.
1282 * we don't do anything new, however, until we receive the
1283 * proxy's response. we might want to set a timer so we can
1284 * timeout the proxy negotiation after a while...
1289 if (change == PROXY_CHANGE_ACCEPTING) {
1290 /* we should _never_ see this, as we are using our socket to
1291 * connect to a proxy, not accepting inbound connections.
1292 * what should we do? close the socket with an appropriate
1295 return plug_accepting(p->plug, p->accepting_sock);
1298 if (change == PROXY_CHANGE_RECEIVE) {
1299 /* we have received data from the underlying socket, which
1300 * we'll need to parse, process, and respond to appropriately.
1305 /* proxy activate will have dealt with
1306 * whatever is left of the buffer */
1310 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
1311 PROXY_ERROR_UNEXPECTED, 0);