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;
250 /* we want a string representation of the IP address for comparisons */
251 sk_getaddr(addr, hostip, 64);
253 hostip_len = strlen(hostip);
254 hostname_len = strlen(hostname);
256 exclude_list = cfg.proxy_exclude_list;
258 /* now parse the exclude list, and see if either our IP
259 * or hostname matches anything in it.
262 while (exclude_list[s]) {
263 while (exclude_list[s] &&
264 (isspace(exclude_list[s]) ||
265 exclude_list[s] == ',')) s++;
267 if (!exclude_list[s]) break;
271 while (exclude_list[e] &&
272 (isalnum(exclude_list[e]) ||
273 exclude_list[e] == '-' ||
274 exclude_list[e] == '.' ||
275 exclude_list[e] == '*')) e++;
277 if (exclude_list[s] == '*') {
278 /* wildcard at beginning of entry */
280 if (strnicmp(hostip + hostip_len - (e - s - 1),
281 exclude_list + s + 1, e - s - 1) == 0 ||
282 strnicmp(hostname + hostname_len - (e - s - 1),
283 exclude_list + s + 1, e - s - 1) == 0)
284 return 0; /* IP/hostname range excluded. do not use proxy. */
286 } else if (exclude_list[e-1] == '*') {
287 /* wildcard at end of entry */
289 if (strnicmp(hostip, exclude_list + s, e - s - 1) == 0 ||
290 strnicmp(hostname, exclude_list + s, e - s - 1) == 0)
291 return 0; /* IP/hostname range excluded. do not use proxy. */
294 /* no wildcard at either end, so let's try an absolute
295 * match (ie. a specific IP)
298 if (stricmp(hostip, exclude_list + s) == 0)
299 return 0; /* IP/hostname excluded. do not use proxy. */
300 if (stricmp(hostname, exclude_list + s) == 0)
301 return 0; /* IP/hostname excluded. do not use proxy. */
307 /* no matches in the exclude list, so use the proxy */
311 Socket new_connection(SockAddr addr, char *hostname,
312 int port, int privport,
313 int oobinline, int nodelay, Plug plug)
315 static struct socket_function_table socket_fn_table = {
321 sk_proxy_set_private_ptr,
322 sk_proxy_get_private_ptr,
324 sk_proxy_socket_error
327 static struct plug_function_table plug_fn_table = {
334 if (cfg.proxy_type != PROXY_NONE &&
335 proxy_for_destination(addr, hostname, port))
340 char * proxy_canonical_name;
342 ret = smalloc(sizeof(struct Socket_proxy_tag));
343 ret->fn = &socket_fn_table;
345 ret->remote_addr = addr;
346 ret->remote_port = port;
349 ret->pending_flush = 0;
352 bufchain_init(&ret->pending_input_data);
353 bufchain_init(&ret->pending_output_data);
354 bufchain_init(&ret->pending_oob_output_data);
356 ret->sub_socket = NULL;
357 ret->state = PROXY_STATE_NEW;
358 ret->negotiate = NULL;
360 if (cfg.proxy_type == PROXY_HTTP) {
361 ret->negotiate = proxy_http_negotiate;
362 } else if (cfg.proxy_type == PROXY_SOCKS) {
363 if (cfg.proxy_socks_version == 4)
364 ret->negotiate = proxy_socks4_negotiate;
366 ret->negotiate = proxy_socks5_negotiate;
367 } else if (cfg.proxy_type == PROXY_TELNET) {
368 ret->negotiate = proxy_telnet_negotiate;
370 ret->error = "Proxy error: Unknown proxy method";
374 /* create the proxy plug to map calls from the actual
375 * socket into our proxy socket layer */
376 pplug = smalloc(sizeof(struct Plug_proxy_tag));
377 pplug->fn = &plug_fn_table;
378 pplug->proxy_socket = ret;
381 proxy_addr = sk_namelookup(cfg.proxy_host,
382 &proxy_canonical_name);
383 sfree(proxy_canonical_name);
385 /* create the actual socket we will be using,
386 * connected to our proxy server and port.
388 ret->sub_socket = sk_new(proxy_addr, cfg.proxy_port,
390 nodelay, (Plug) pplug);
391 if (sk_socket_error(ret->sub_socket) != NULL)
394 sk_addr_free(proxy_addr);
396 /* start the proxy negotiation process... */
397 sk_set_frozen(ret->sub_socket, 0);
398 ret->negotiate(ret, PROXY_CHANGE_NEW);
403 /* no proxy, so just return the direct socket */
404 return sk_new(addr, port, privport, oobinline, nodelay, plug);
407 Socket new_listener(int port, Plug plug, int local_host_only)
409 /* TODO: SOCKS (and potentially others) support inbound
410 * TODO: connections via the proxy. support them.
413 return sk_newlistener(port, plug, local_host_only);
416 /* ----------------------------------------------------------------------
417 * HTTP CONNECT proxy type.
420 static int get_line_end (char * data, int len)
426 if (data[off] == '\n') {
427 /* we have a newline */
430 /* is that the only thing on this line? */
431 if (off <= 2) return off;
433 /* if not, then there is the possibility that this header
434 * continues onto the next line, if it starts with a space
439 data[off+1] != ' ' &&
440 data[off+1] != '\t') return off;
442 /* the line does continue, so we have to keep going
443 * until we see an the header's "real" end of line.
454 int proxy_http_negotiate (Proxy_Socket p, int change)
456 if (p->state == PROXY_STATE_NEW) {
457 /* we are just beginning the proxy negotiate process,
458 * so we'll send off the initial bits of the request.
459 * for this proxy method, it's just a simple HTTP
462 char buf[256], dest[64];
464 sk_getaddr(p->remote_addr, dest, 64);
466 sprintf(buf, "CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
467 dest, p->remote_port, dest, p->remote_port);
468 sk_write(p->sub_socket, buf, strlen(buf));
470 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
471 char buf[sizeof(cfg.proxy_username)+sizeof(cfg.proxy_password)];
472 char buf2[sizeof(buf)*4/3 + 100];
474 sprintf(buf, "%s:%s", cfg.proxy_username, cfg.proxy_password);
476 sprintf(buf2, "Proxy-Authorization: basic ");
477 for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)
478 base64_encode_atom(buf+i, (len-i > 3 ? 3 : len-i), buf2+j);
479 strcpy(buf2+j, "\r\n");
480 sk_write(p->sub_socket, buf2, strlen(buf2));
483 sprintf(buf, "\r\n");
484 sk_write(p->sub_socket, buf, strlen(buf));
490 if (change == PROXY_CHANGE_CLOSING) {
491 /* if our proxy negotiation process involves closing and opening
492 * new sockets, then we would want to intercept this closing
493 * callback when we were expecting it. if we aren't anticipating
494 * a socket close, then some error must have occurred. we'll
495 * just pass those errors up to the backend.
497 return plug_closing(p->plug, p->closing_error_msg,
498 p->closing_error_code,
499 p->closing_calling_back);
502 if (change == PROXY_CHANGE_SENT) {
503 /* some (or all) of what we wrote to the proxy was sent.
504 * we don't do anything new, however, until we receive the
505 * proxy's response. we might want to set a timer so we can
506 * timeout the proxy negotiation after a while...
511 if (change == PROXY_CHANGE_ACCEPTING) {
512 /* we should _never_ see this, as we are using our socket to
513 * connect to a proxy, not accepting inbound connections.
514 * what should we do? close the socket with an appropriate
517 return plug_accepting(p->plug, p->accepting_sock);
520 if (change == PROXY_CHANGE_RECEIVE) {
521 /* we have received data from the underlying socket, which
522 * we'll need to parse, process, and respond to appropriately.
531 int min_ver, maj_ver, status;
533 /* get the status line */
534 len = bufchain_size(&p->pending_input_data);
535 assert(len > 0); /* or we wouldn't be here */
537 bufchain_fetch(&p->pending_input_data, data, len);
539 eol = get_line_end(data, len);
546 /* We can't rely on whether the %n incremented the sscanf return */
547 if (sscanf((char *)data, "HTTP/%i.%i %n",
548 &maj_ver, &min_ver, &status) < 2 || status == -1) {
549 plug_closing(p->plug, "Proxy error: HTTP response was absent",
550 PROXY_ERROR_GENERAL, 0);
555 /* remove the status line from the input buffer. */
556 bufchain_consume(&p->pending_input_data, eol);
557 if (data[status] != '2') {
561 while (eol > status &&
562 (data[eol-1] == '\r' || data[eol-1] == '\n'))
564 sprintf(buf, "Proxy error: %.900s",
566 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
578 /* get headers. we're done when we get a
579 * header of length 2, (ie. just "\r\n")
582 len = bufchain_size(&p->pending_input_data);
583 assert(len > 0); /* or we wouldn't be here */
586 bufchain_fetch(&p->pending_input_data, data, len);
588 eol = get_line_end(datap, len);
595 bufchain_consume(&p->pending_input_data, eol);
597 eol = get_line_end(datap, len);
602 bufchain_consume(&p->pending_input_data, 2);
604 /* proxy activate will have dealt with
605 * whatever is left of the buffer */
615 plug_closing(p->plug, "Proxy error: unexpected proxy error",
616 PROXY_ERROR_UNEXPECTED, 0);
620 /* ----------------------------------------------------------------------
624 /* SOCKS version 4 */
625 int proxy_socks4_negotiate (Proxy_Socket p, int change)
627 if (p->state == PROXY_CHANGE_NEW) {
630 * version number (1 byte) = 4
631 * command code (1 byte)
634 * dest. port (2 bytes) [network order]
635 * dest. address (4 bytes)
636 * user ID (variable length, null terminated string)
642 if (sk_addrtype(p->remote_addr) != AF_INET) {
643 plug_closing(p->plug, "Proxy error: SOCKS version 4 does"
644 " not support IPv6", PROXY_ERROR_GENERAL, 0);
648 length = strlen(cfg.proxy_username) + 9;
649 command = (char*) malloc(length);
650 strcpy(command + 8, cfg.proxy_username);
652 command[0] = 4; /* version 4 */
653 command[1] = 1; /* CONNECT command */
656 command[2] = (char) (p->remote_port >> 8) & 0xff;
657 command[3] = (char) p->remote_port & 0xff;
660 sk_addrcopy(p->remote_addr, command + 4);
662 sk_write(p->sub_socket, command, length);
669 if (change == PROXY_CHANGE_CLOSING) {
670 /* if our proxy negotiation process involves closing and opening
671 * new sockets, then we would want to intercept this closing
672 * callback when we were expecting it. if we aren't anticipating
673 * a socket close, then some error must have occurred. we'll
674 * just pass those errors up to the backend.
676 return plug_closing(p->plug, p->closing_error_msg,
677 p->closing_error_code,
678 p->closing_calling_back);
681 if (change == PROXY_CHANGE_SENT) {
682 /* some (or all) of what we wrote to the proxy was sent.
683 * we don't do anything new, however, until we receive the
684 * proxy's response. we might want to set a timer so we can
685 * timeout the proxy negotiation after a while...
690 if (change == PROXY_CHANGE_ACCEPTING) {
691 /* we should _never_ see this, as we are using our socket to
692 * connect to a proxy, not accepting inbound connections.
693 * what should we do? close the socket with an appropriate
696 return plug_accepting(p->plug, p->accepting_sock);
699 if (change == PROXY_CHANGE_RECEIVE) {
700 /* we have received data from the underlying socket, which
701 * we'll need to parse, process, and respond to appropriately.
706 * version number (1 byte) = 4
707 * reply code (1 byte)
708 * 90 = request granted
709 * 91 = request rejected or failed
710 * 92 = request rejected due to lack of IDENTD on client
711 * 93 = request rejected due to difference in user ID
712 * (what we sent vs. what IDENTD said)
713 * dest. port (2 bytes)
714 * dest. address (4 bytes)
719 if (bufchain_size(&p->pending_input_data) < 8)
720 return 1; /* not got anything yet */
722 /* get the response */
723 bufchain_fetch(&p->pending_input_data, data, 8);
726 plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "
727 "unexpected reply code version",
728 PROXY_ERROR_GENERAL, 0);
736 plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",
737 PROXY_ERROR_GENERAL, 0);
740 plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",
741 PROXY_ERROR_GENERAL, 0);
745 plug_closing(p->plug, "Proxy error: Error while communicating with proxy",
746 PROXY_ERROR_GENERAL, 0);
752 bufchain_consume(&p->pending_input_data, 8);
756 /* proxy activate will have dealt with
757 * whatever is left of the buffer */
762 plug_closing(p->plug, "Proxy error: unexpected proxy error",
763 PROXY_ERROR_UNEXPECTED, 0);
767 /* SOCKS version 5 */
768 int proxy_socks5_negotiate (Proxy_Socket p, int change)
770 if (p->state == PROXY_CHANGE_NEW) {
773 * version number (1 byte) = 5
774 * number of available authentication methods (1 byte)
775 * available authentication methods (1 byte * previous value)
776 * authentication methods:
777 * 0x00 = no authentication
779 * 0x02 = username/password
786 command[0] = 5; /* version 5 */
787 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
788 command[1] = 2; /* two methods supported: */
789 command[2] = 0x00; /* no authentication */
790 command[3] = 0x02; /* username/password */
793 command[1] = 1; /* one methods supported: */
794 command[2] = 0x00; /* no authentication */
798 sk_write(p->sub_socket, command, len);
804 if (change == PROXY_CHANGE_CLOSING) {
805 /* if our proxy negotiation process involves closing and opening
806 * new sockets, then we would want to intercept this closing
807 * callback when we were expecting it. if we aren't anticipating
808 * a socket close, then some error must have occurred. we'll
809 * just pass those errors up to the backend.
811 return plug_closing(p->plug, p->closing_error_msg,
812 p->closing_error_code,
813 p->closing_calling_back);
816 if (change == PROXY_CHANGE_SENT) {
817 /* some (or all) of what we wrote to the proxy was sent.
818 * we don't do anything new, however, until we receive the
819 * proxy's response. we might want to set a timer so we can
820 * timeout the proxy negotiation after a while...
825 if (change == PROXY_CHANGE_ACCEPTING) {
826 /* we should _never_ see this, as we are using our socket to
827 * connect to a proxy, not accepting inbound connections.
828 * what should we do? close the socket with an appropriate
831 return plug_accepting(p->plug, p->accepting_sock);
834 if (change == PROXY_CHANGE_RECEIVE) {
835 /* we have received data from the underlying socket, which
836 * we'll need to parse, process, and respond to appropriately.
842 * version number (1 byte) = 5
843 * authentication method (1 byte)
844 * authentication methods:
845 * 0x00 = no authentication
847 * 0x02 = username/password
849 * 0xff = no acceptable methods
853 if (bufchain_size(&p->pending_input_data) < 2)
854 return 1; /* not got anything yet */
856 /* get the response */
857 bufchain_fetch(&p->pending_input_data, data, 2);
860 plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",
861 PROXY_ERROR_GENERAL, 0);
865 if (data[1] == 0x00) p->state = 2; /* no authentication needed */
866 else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */
867 else if (data[1] == 0x02) p->state = 5; /* username/password authentication */
868 else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */
870 plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",
871 PROXY_ERROR_GENERAL, 0);
874 bufchain_consume(&p->pending_input_data, 2);
879 /* password authentication reply format:
880 * version number (1 bytes) = 1
881 * reply code (1 byte)
887 if (bufchain_size(&p->pending_input_data) < 2)
888 return 1; /* not got anything yet */
890 /* get the response */
891 bufchain_fetch(&p->pending_input_data, data, 2);
894 plug_closing(p->plug, "Proxy error: SOCKS password "
895 "subnegotiation contained wrong version number",
896 PROXY_ERROR_GENERAL, 0);
902 plug_closing(p->plug, "Proxy error: SOCKS proxy refused"
903 " password authentication",
904 PROXY_ERROR_GENERAL, 0);
908 bufchain_consume(&p->pending_input_data, 2);
909 p->state = 2; /* now proceed as authenticated */
915 * version number (1 byte) = 5
916 * command code (1 byte)
920 * reserved (1 byte) = 0x00
921 * address type (1 byte)
923 * 3 = domainname (first byte has length, no terminating null)
925 * dest. address (variable)
926 * dest. port (2 bytes) [network order]
932 if (sk_addrtype(p->remote_addr) == AF_INET) {
934 command[3] = 1; /* IPv4 */
937 command[3] = 4; /* IPv6 */
940 command[0] = 5; /* version 5 */
941 command[1] = 1; /* CONNECT command */
945 sk_addrcopy(p->remote_addr, command+4);
948 command[len-2] = (char) (p->remote_port >> 8) & 0xff;
949 command[len-1] = (char) p->remote_port & 0xff;
951 sk_write(p->sub_socket, command, len);
960 * version number (1 bytes) = 5
961 * reply code (1 byte)
963 * 1 = general SOCKS server failure
964 * 2 = connection not allowed by ruleset
965 * 3 = network unreachable
966 * 4 = host unreachable
967 * 5 = connection refused
969 * 7 = command not supported
970 * 8 = address type not supported
971 * reserved (1 byte) = x00
972 * address type (1 byte)
974 * 3 = domainname (first byte has length, no terminating null)
976 * server bound address (variable)
977 * server bound port (2 bytes) [network order]
982 /* First 5 bytes of packet are enough to tell its length. */
983 if (bufchain_size(&p->pending_input_data) < 5)
984 return 1; /* not got anything yet */
986 /* get the response */
987 bufchain_fetch(&p->pending_input_data, data, 5);
990 plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",
991 PROXY_ERROR_GENERAL, 0);
998 strcpy(buf, "Proxy error: ");
1001 case 1: strcat(buf, "General SOCKS server failure"); break;
1002 case 2: strcat(buf, "Connection not allowed by ruleset"); break;
1003 case 3: strcat(buf, "Network unreachable"); break;
1004 case 4: strcat(buf, "Host unreachable"); break;
1005 case 5: strcat(buf, "Connection refused"); break;
1006 case 6: strcat(buf, "TTL expired"); break;
1007 case 7: strcat(buf, "Command not supported"); break;
1008 case 8: strcat(buf, "Address type not supported"); break;
1009 default: sprintf(buf+strlen(buf),
1010 "Unrecognised SOCKS error code %d",
1014 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
1020 * Eat the rest of the reply packet.
1022 len = 6; /* first 4 bytes, last 2 */
1024 case 1: len += 4; break; /* IPv4 address */
1025 case 4: len += 16; break;/* IPv6 address */
1026 case 3: len += (unsigned char)data[4]; break; /* domain name */
1028 plug_closing(p->plug, "Proxy error: SOCKS proxy returned "
1029 "unrecognised address format",
1030 PROXY_ERROR_GENERAL, 0);
1033 if (bufchain_size(&p->pending_input_data) < len)
1034 return 1; /* not got whole reply yet */
1035 bufchain_consume(&p->pending_input_data, len);
1042 if (p->state == 4) {
1043 /* TODO: Handle GSSAPI authentication */
1044 plug_closing(p->plug, "Proxy error: We don't support GSSAPI authentication",
1045 PROXY_ERROR_GENERAL, 0);
1049 if (p->state == 5) {
1050 if (cfg.proxy_username[0] || cfg.proxy_password[0]) {
1051 char userpwbuf[514];
1053 ulen = strlen(cfg.proxy_username);
1054 if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
1055 plen = strlen(cfg.proxy_password);
1056 if (plen > 255) plen = 255; if (plen < 1) plen = 1;
1057 userpwbuf[0] = 1; /* version number of subnegotiation */
1058 userpwbuf[1] = ulen;
1059 memcpy(userpwbuf+2, cfg.proxy_username, ulen);
1060 userpwbuf[ulen+2] = plen;
1061 memcpy(userpwbuf+ulen+3, cfg.proxy_password, plen);
1062 sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);
1065 plug_closing(p->plug, "Proxy error: Server chose "
1066 "username/password authentication but we "
1068 PROXY_ERROR_GENERAL, 0);
1072 if (p->state == 6) {
1073 /* TODO: Handle CHAP authentication */
1074 plug_closing(p->plug, "Proxy error: We don't support CHAP authentication",
1075 PROXY_ERROR_GENERAL, 0);
1081 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
1082 PROXY_ERROR_UNEXPECTED, 0);
1086 /* ----------------------------------------------------------------------
1087 * `Telnet' proxy type.
1089 * (This is for ad-hoc proxies where you connect to the proxy's
1090 * telnet port and send a command such as `connect host port'. The
1091 * command is configurable, since this proxy type is typically not
1092 * standardised or at all well-defined.)
1095 int proxy_telnet_negotiate (Proxy_Socket p, int change)
1097 if (p->state == PROXY_CHANGE_NEW) {
1101 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1102 * %%, %host, %port, %user, and %pass
1105 while (cfg.proxy_telnet_command[eo] != 0) {
1107 /* scan forward until we hit end-of-line,
1108 * or an escape character (\ or %) */
1109 while (cfg.proxy_telnet_command[eo] != 0 &&
1110 cfg.proxy_telnet_command[eo] != '%' &&
1111 cfg.proxy_telnet_command[eo] != '\\') eo++;
1113 /* if we hit eol, break out of our escaping loop */
1114 if (cfg.proxy_telnet_command[eo] == 0) break;
1116 /* if there was any unescaped text before the escape
1117 * character, send that now */
1119 sk_write(p->sub_socket,
1120 cfg.proxy_telnet_command + so, eo - so);
1125 /* if the escape character was the last character of
1126 * the line, we'll just stop and send it. */
1127 if (cfg.proxy_telnet_command[eo] == 0) break;
1129 if (cfg.proxy_telnet_command[so] == '\\') {
1131 /* we recognize \\, \%, \r, \n, \t, \x??.
1132 * anything else, we just send unescaped (including the \).
1135 switch (cfg.proxy_telnet_command[eo]) {
1138 sk_write(p->sub_socket, "\\", 1);
1143 sk_write(p->sub_socket, "%%", 1);
1148 sk_write(p->sub_socket, "\r", 1);
1153 sk_write(p->sub_socket, "\n", 1);
1158 sk_write(p->sub_socket, "\t", 1);
1165 /* escaped hexadecimal value (ie. \xff) */
1166 unsigned char v = 0;
1171 if (cfg.proxy_telnet_command[eo] >= '0' &&
1172 cfg.proxy_telnet_command[eo] <= '9')
1173 v += cfg.proxy_telnet_command[eo] - '0';
1174 else if (cfg.proxy_telnet_command[eo] >= 'a' &&
1175 cfg.proxy_telnet_command[eo] <= 'f')
1176 v += cfg.proxy_telnet_command[eo] - 'a' + 10;
1177 else if (cfg.proxy_telnet_command[eo] >= 'A' &&
1178 cfg.proxy_telnet_command[eo] <= 'F')
1179 v += cfg.proxy_telnet_command[eo] - 'A' + 10;
1181 /* non hex character, so we abort and just
1182 * send the whole thing unescaped (including \x)
1184 sk_write(p->sub_socket, "\\", 1);
1189 /* we only extract two hex characters */
1191 sk_write(p->sub_socket, &v, 1);
1203 sk_write(p->sub_socket,
1204 cfg.proxy_telnet_command + so, 2);
1210 /* % escape. we recognize %%, %host, %port, %user, %pass.
1211 * anything else, we just send unescaped (including the %).
1214 if (cfg.proxy_telnet_command[eo] == '%') {
1215 sk_write(p->sub_socket, "%", 1);
1218 else if (strnicmp(cfg.proxy_telnet_command + eo,
1221 sk_getaddr(p->remote_addr, dest, 64);
1222 sk_write(p->sub_socket, dest, strlen(dest));
1225 else if (strnicmp(cfg.proxy_telnet_command + eo,
1228 sprintf(port, "%i", p->remote_port);
1229 sk_write(p->sub_socket, port, strlen(port));
1232 else if (strnicmp(cfg.proxy_telnet_command + eo,
1234 sk_write(p->sub_socket, cfg.proxy_username,
1235 strlen(cfg.proxy_username));
1238 else if (strnicmp(cfg.proxy_telnet_command + eo,
1240 sk_write(p->sub_socket, cfg.proxy_password,
1241 strlen(cfg.proxy_password));
1245 /* we don't escape this, so send the % now, and
1246 * don't advance eo, so that we'll consider the
1247 * text immediately following the % as unescaped.
1249 sk_write(p->sub_socket, "%", 1);
1253 /* resume scanning for additional escapes after this one. */
1257 /* if there is any unescaped text at the end of the line, send it */
1259 sk_write(p->sub_socket, cfg.proxy_telnet_command + so, eo - so);
1266 if (change == PROXY_CHANGE_CLOSING) {
1267 /* if our proxy negotiation process involves closing and opening
1268 * new sockets, then we would want to intercept this closing
1269 * callback when we were expecting it. if we aren't anticipating
1270 * a socket close, then some error must have occurred. we'll
1271 * just pass those errors up to the backend.
1273 return plug_closing(p->plug, p->closing_error_msg,
1274 p->closing_error_code,
1275 p->closing_calling_back);
1278 if (change == PROXY_CHANGE_SENT) {
1279 /* some (or all) of what we wrote to the proxy was sent.
1280 * we don't do anything new, however, until we receive the
1281 * proxy's response. we might want to set a timer so we can
1282 * timeout the proxy negotiation after a while...
1287 if (change == PROXY_CHANGE_ACCEPTING) {
1288 /* we should _never_ see this, as we are using our socket to
1289 * connect to a proxy, not accepting inbound connections.
1290 * what should we do? close the socket with an appropriate
1293 return plug_accepting(p->plug, p->accepting_sock);
1296 if (change == PROXY_CHANGE_RECEIVE) {
1297 /* we have received data from the underlying socket, which
1298 * we'll need to parse, process, and respond to appropriately.
1303 /* proxy activate will have dealt with
1304 * whatever is left of the buffer */
1308 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
1309 PROXY_ERROR_UNEXPECTED, 0);