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
17 #define do_proxy_dns(conf) \
18 (conf_get_int(conf, CONF_proxy_dns) == FORCE_ON || \
19 (conf_get_int(conf, CONF_proxy_dns) == AUTO && \
20 conf_get_int(conf, CONF_proxy_type) != PROXY_SOCKS4))
23 * Call this when proxy negotiation is complete, so that this
24 * socket can begin working normally.
26 void proxy_activate (Proxy_Socket p)
30 long output_before, output_after;
32 p->state = PROXY_STATE_ACTIVE;
34 /* we want to ignore new receive events until we have sent
35 * all of our buffered receive data.
37 sk_set_frozen(p->sub_socket, 1);
39 /* how many bytes of output have we buffered? */
40 output_before = bufchain_size(&p->pending_oob_output_data) +
41 bufchain_size(&p->pending_output_data);
42 /* and keep track of how many bytes do not get sent. */
45 /* send buffered OOB writes */
46 while (bufchain_size(&p->pending_oob_output_data) > 0) {
47 bufchain_prefix(&p->pending_oob_output_data, &data, &len);
48 output_after += sk_write_oob(p->sub_socket, data, len);
49 bufchain_consume(&p->pending_oob_output_data, len);
52 /* send buffered normal writes */
53 while (bufchain_size(&p->pending_output_data) > 0) {
54 bufchain_prefix(&p->pending_output_data, &data, &len);
55 output_after += sk_write(p->sub_socket, data, len);
56 bufchain_consume(&p->pending_output_data, len);
59 /* if we managed to send any data, let the higher levels know. */
60 if (output_after < output_before)
61 plug_sent(p->plug, output_after);
63 /* if we were asked to flush the output during
64 * the proxy negotiation process, do so now.
66 if (p->pending_flush) sk_flush(p->sub_socket);
68 /* if we have a pending EOF to send, send it */
69 if (p->pending_eof) sk_write_eof(p->sub_socket);
71 /* if the backend wanted the socket unfrozen, try to unfreeze.
72 * our set_frozen handler will flush buffered receive data before
73 * unfreezing the actual underlying socket.
76 sk_set_frozen((Socket)p, 0);
79 /* basic proxy socket functions */
81 static Plug sk_proxy_plug (Socket s, Plug p)
83 Proxy_Socket ps = (Proxy_Socket) s;
90 static void sk_proxy_close (Socket s)
92 Proxy_Socket ps = (Proxy_Socket) s;
94 sk_close(ps->sub_socket);
95 sk_addr_free(ps->remote_addr);
99 static int sk_proxy_write (Socket s, const char *data, int len)
101 Proxy_Socket ps = (Proxy_Socket) s;
103 if (ps->state != PROXY_STATE_ACTIVE) {
104 bufchain_add(&ps->pending_output_data, data, len);
105 return bufchain_size(&ps->pending_output_data);
107 return sk_write(ps->sub_socket, data, len);
110 static int sk_proxy_write_oob (Socket s, const char *data, int len)
112 Proxy_Socket ps = (Proxy_Socket) s;
114 if (ps->state != PROXY_STATE_ACTIVE) {
115 bufchain_clear(&ps->pending_output_data);
116 bufchain_clear(&ps->pending_oob_output_data);
117 bufchain_add(&ps->pending_oob_output_data, data, len);
120 return sk_write_oob(ps->sub_socket, data, len);
123 static void sk_proxy_write_eof (Socket s)
125 Proxy_Socket ps = (Proxy_Socket) s;
127 if (ps->state != PROXY_STATE_ACTIVE) {
131 sk_write_eof(ps->sub_socket);
134 static void sk_proxy_flush (Socket s)
136 Proxy_Socket ps = (Proxy_Socket) s;
138 if (ps->state != PROXY_STATE_ACTIVE) {
139 ps->pending_flush = 1;
142 sk_flush(ps->sub_socket);
145 static void sk_proxy_set_frozen (Socket s, int is_frozen)
147 Proxy_Socket ps = (Proxy_Socket) s;
149 if (ps->state != PROXY_STATE_ACTIVE) {
150 ps->freeze = is_frozen;
154 /* handle any remaining buffered recv data first */
155 if (bufchain_size(&ps->pending_input_data) > 0) {
156 ps->freeze = is_frozen;
158 /* loop while we still have buffered data, and while we are
159 * unfrozen. the plug_receive call in the loop could result
160 * in a call back into this function refreezing the socket,
161 * so we have to check each time.
163 while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {
167 bufchain_prefix(&ps->pending_input_data, &data, &len);
168 if (len > lenof(databuf))
169 len = lenof(databuf);
170 memcpy(databuf, data, len);
171 bufchain_consume(&ps->pending_input_data, len);
172 plug_receive(ps->plug, 0, databuf, len);
175 /* if we're still frozen, we'll have to wait for another
176 * call from the backend to finish unbuffering the data.
178 if (ps->freeze) return;
181 sk_set_frozen(ps->sub_socket, is_frozen);
184 static const char * sk_proxy_socket_error (Socket s)
186 Proxy_Socket ps = (Proxy_Socket) s;
187 if (ps->error != NULL || ps->sub_socket == NULL) {
190 return sk_socket_error(ps->sub_socket);
193 /* basic proxy plug functions */
195 static void plug_proxy_log(Plug plug, int type, SockAddr addr, int port,
196 const char *error_msg, int error_code)
198 Proxy_Plug pp = (Proxy_Plug) plug;
199 Proxy_Socket ps = pp->proxy_socket;
201 plug_log(ps->plug, type, addr, port, error_msg, error_code);
204 static int plug_proxy_closing (Plug p, const char *error_msg,
205 int error_code, int calling_back)
207 Proxy_Plug pp = (Proxy_Plug) p;
208 Proxy_Socket ps = pp->proxy_socket;
210 if (ps->state != PROXY_STATE_ACTIVE) {
211 ps->closing_error_msg = error_msg;
212 ps->closing_error_code = error_code;
213 ps->closing_calling_back = calling_back;
214 return ps->negotiate(ps, PROXY_CHANGE_CLOSING);
216 return plug_closing(ps->plug, error_msg,
217 error_code, calling_back);
220 static int plug_proxy_receive (Plug p, int urgent, char *data, int len)
222 Proxy_Plug pp = (Proxy_Plug) p;
223 Proxy_Socket ps = pp->proxy_socket;
225 if (ps->state != PROXY_STATE_ACTIVE) {
226 /* we will lose the urgentness of this data, but since most,
227 * if not all, of this data will be consumed by the negotiation
228 * process, hopefully it won't affect the protocol above us
230 bufchain_add(&ps->pending_input_data, data, len);
231 ps->receive_urgent = urgent;
232 ps->receive_data = data;
233 ps->receive_len = len;
234 return ps->negotiate(ps, PROXY_CHANGE_RECEIVE);
236 return plug_receive(ps->plug, urgent, data, len);
239 static void plug_proxy_sent (Plug p, int bufsize)
241 Proxy_Plug pp = (Proxy_Plug) p;
242 Proxy_Socket ps = pp->proxy_socket;
244 if (ps->state != PROXY_STATE_ACTIVE) {
245 ps->sent_bufsize = bufsize;
246 ps->negotiate(ps, PROXY_CHANGE_SENT);
249 plug_sent(ps->plug, bufsize);
252 static int plug_proxy_accepting(Plug p,
253 accept_fn_t constructor, accept_ctx_t ctx)
255 Proxy_Plug pp = (Proxy_Plug) p;
256 Proxy_Socket ps = pp->proxy_socket;
258 if (ps->state != PROXY_STATE_ACTIVE) {
259 ps->accepting_constructor = constructor;
260 ps->accepting_ctx = ctx;
261 return ps->negotiate(ps, PROXY_CHANGE_ACCEPTING);
263 return plug_accepting(ps->plug, constructor, ctx);
267 * This function can accept a NULL pointer as `addr', in which case
268 * it will only check the host name.
270 int proxy_for_destination (SockAddr addr, const char *hostname,
271 int port, Conf *conf)
275 int hostip_len, hostname_len;
276 const char *exclude_list;
279 * Special local connections such as Unix-domain sockets
280 * unconditionally cannot be proxied, even in proxy-localhost
281 * mode. There just isn't any way to ask any known proxy type for
284 if (addr && sk_address_is_special_local(addr))
285 return 0; /* do not proxy */
288 * Check the host name and IP against the hard-coded
289 * representations of `localhost'.
291 if (!conf_get_int(conf, CONF_even_proxy_localhost) &&
292 (sk_hostname_is_local(hostname) ||
293 (addr && sk_address_is_local(addr))))
294 return 0; /* do not proxy */
296 /* we want a string representation of the IP address for comparisons */
298 sk_getaddr(addr, hostip, 64);
299 hostip_len = strlen(hostip);
301 hostip_len = 0; /* placate gcc; shouldn't be required */
303 hostname_len = strlen(hostname);
305 exclude_list = conf_get_str(conf, CONF_proxy_exclude_list);
307 /* now parse the exclude list, and see if either our IP
308 * or hostname matches anything in it.
311 while (exclude_list[s]) {
312 while (exclude_list[s] &&
313 (isspace((unsigned char)exclude_list[s]) ||
314 exclude_list[s] == ',')) s++;
316 if (!exclude_list[s]) break;
320 while (exclude_list[e] &&
321 (isalnum((unsigned char)exclude_list[e]) ||
322 exclude_list[e] == '-' ||
323 exclude_list[e] == '.' ||
324 exclude_list[e] == '*')) e++;
326 if (exclude_list[s] == '*') {
327 /* wildcard at beginning of entry */
329 if ((addr && strnicmp(hostip + hostip_len - (e - s - 1),
330 exclude_list + s + 1, e - s - 1) == 0) ||
331 strnicmp(hostname + hostname_len - (e - s - 1),
332 exclude_list + s + 1, e - s - 1) == 0)
333 return 0; /* IP/hostname range excluded. do not use proxy. */
335 } else if (exclude_list[e-1] == '*') {
336 /* wildcard at end of entry */
338 if ((addr && strnicmp(hostip, exclude_list + s, e - s - 1) == 0) ||
339 strnicmp(hostname, exclude_list + s, e - s - 1) == 0)
340 return 0; /* IP/hostname range excluded. do not use proxy. */
343 /* no wildcard at either end, so let's try an absolute
344 * match (ie. a specific IP)
347 if (addr && strnicmp(hostip, exclude_list + s, e - s) == 0)
348 return 0; /* IP/hostname excluded. do not use proxy. */
349 if (strnicmp(hostname, exclude_list + s, e - s) == 0)
350 return 0; /* IP/hostname excluded. do not use proxy. */
355 /* Make sure we really have reached the next comma or end-of-string */
356 while (exclude_list[s] &&
357 !isspace((unsigned char)exclude_list[s]) &&
358 exclude_list[s] != ',') s++;
361 /* no matches in the exclude list, so use the proxy */
365 SockAddr name_lookup(const char *host, int port, char **canonicalname,
366 Conf *conf, int addressfamily)
368 if (conf_get_int(conf, CONF_proxy_type) != PROXY_NONE &&
369 do_proxy_dns(conf) &&
370 proxy_for_destination(NULL, host, port, conf)) {
371 *canonicalname = dupstr(host);
372 return sk_nonamelookup(host);
375 return sk_namelookup(host, canonicalname, addressfamily);
378 Socket new_connection(SockAddr addr, const char *hostname,
379 int port, int privport,
380 int oobinline, int nodelay, int keepalive,
381 Plug plug, Conf *conf)
383 static const struct socket_function_table socket_fn_table = {
391 sk_proxy_socket_error,
392 NULL, /* peer_info */
395 static const struct plug_function_table plug_fn_table = {
403 if (conf_get_int(conf, CONF_proxy_type) != PROXY_NONE &&
404 proxy_for_destination(addr, hostname, port, conf))
409 char *proxy_canonical_name;
413 if ((sret = platform_new_connection(addr, hostname, port, privport,
414 oobinline, nodelay, keepalive,
419 ret = snew(struct Socket_proxy_tag);
420 ret->fn = &socket_fn_table;
421 ret->conf = conf_copy(conf);
423 ret->remote_addr = addr; /* will need to be freed on close */
424 ret->remote_port = port;
427 ret->pending_flush = 0;
428 ret->pending_eof = 0;
431 bufchain_init(&ret->pending_input_data);
432 bufchain_init(&ret->pending_output_data);
433 bufchain_init(&ret->pending_oob_output_data);
435 ret->sub_socket = NULL;
436 ret->state = PROXY_STATE_NEW;
437 ret->negotiate = NULL;
439 type = conf_get_int(conf, CONF_proxy_type);
440 if (type == PROXY_HTTP) {
441 ret->negotiate = proxy_http_negotiate;
442 } else if (type == PROXY_SOCKS4) {
443 ret->negotiate = proxy_socks4_negotiate;
444 } else if (type == PROXY_SOCKS5) {
445 ret->negotiate = proxy_socks5_negotiate;
446 } else if (type == PROXY_TELNET) {
447 ret->negotiate = proxy_telnet_negotiate;
449 ret->error = "Proxy error: Unknown proxy method";
453 /* create the proxy plug to map calls from the actual
454 * socket into our proxy socket layer */
455 pplug = snew(struct Plug_proxy_tag);
456 pplug->fn = &plug_fn_table;
457 pplug->proxy_socket = ret;
460 proxy_addr = sk_namelookup(conf_get_str(conf, CONF_proxy_host),
461 &proxy_canonical_name,
462 conf_get_int(conf, CONF_addressfamily));
463 if (sk_addr_error(proxy_addr) != NULL) {
464 ret->error = "Proxy error: Unable to resolve proxy host name";
466 sk_addr_free(proxy_addr);
469 sfree(proxy_canonical_name);
471 /* create the actual socket we will be using,
472 * connected to our proxy server and port.
474 ret->sub_socket = sk_new(proxy_addr,
475 conf_get_int(conf, CONF_proxy_port),
477 nodelay, keepalive, (Plug) pplug);
478 if (sk_socket_error(ret->sub_socket) != NULL)
481 /* start the proxy negotiation process... */
482 sk_set_frozen(ret->sub_socket, 0);
483 ret->negotiate(ret, PROXY_CHANGE_NEW);
488 /* no proxy, so just return the direct socket */
489 return sk_new(addr, port, privport, oobinline, nodelay, keepalive, plug);
492 Socket new_listener(const char *srcaddr, int port, Plug plug,
493 int local_host_only, Conf *conf, int addressfamily)
495 /* TODO: SOCKS (and potentially others) support inbound
496 * TODO: connections via the proxy. support them.
499 return sk_newlistener(srcaddr, port, plug, local_host_only, addressfamily);
502 /* ----------------------------------------------------------------------
503 * HTTP CONNECT proxy type.
506 static int get_line_end (char * data, int len)
512 if (data[off] == '\n') {
513 /* we have a newline */
516 /* is that the only thing on this line? */
517 if (off <= 2) return off;
519 /* if not, then there is the possibility that this header
520 * continues onto the next line, if it starts with a space
525 data[off+1] != ' ' &&
526 data[off+1] != '\t') return off;
528 /* the line does continue, so we have to keep going
529 * until we see an the header's "real" end of line.
540 int proxy_http_negotiate (Proxy_Socket p, int change)
542 if (p->state == PROXY_STATE_NEW) {
543 /* we are just beginning the proxy negotiate process,
544 * so we'll send off the initial bits of the request.
545 * for this proxy method, it's just a simple HTTP
548 char *buf, dest[512];
549 char *username, *password;
551 sk_getaddr(p->remote_addr, dest, lenof(dest));
553 buf = dupprintf("CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
554 dest, p->remote_port, dest, p->remote_port);
555 sk_write(p->sub_socket, buf, strlen(buf));
558 username = conf_get_str(p->conf, CONF_proxy_username);
559 password = conf_get_str(p->conf, CONF_proxy_password);
560 if (username[0] || password[0]) {
563 buf = dupprintf("%s:%s", username, password);
565 buf2 = snewn(len * 4 / 3 + 100, char);
566 sprintf(buf2, "Proxy-Authorization: Basic ");
567 for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)
568 base64_encode_atom((unsigned char *)(buf+i),
569 (len-i > 3 ? 3 : len-i), buf2+j);
570 strcpy(buf2+j, "\r\n");
571 sk_write(p->sub_socket, buf2, strlen(buf2));
576 sk_write(p->sub_socket, "\r\n", 2);
582 if (change == PROXY_CHANGE_CLOSING) {
583 /* if our proxy negotiation process involves closing and opening
584 * new sockets, then we would want to intercept this closing
585 * callback when we were expecting it. if we aren't anticipating
586 * a socket close, then some error must have occurred. we'll
587 * just pass those errors up to the backend.
589 return plug_closing(p->plug, p->closing_error_msg,
590 p->closing_error_code,
591 p->closing_calling_back);
594 if (change == PROXY_CHANGE_SENT) {
595 /* some (or all) of what we wrote to the proxy was sent.
596 * we don't do anything new, however, until we receive the
597 * proxy's response. we might want to set a timer so we can
598 * timeout the proxy negotiation after a while...
603 if (change == PROXY_CHANGE_ACCEPTING) {
604 /* we should _never_ see this, as we are using our socket to
605 * connect to a proxy, not accepting inbound connections.
606 * what should we do? close the socket with an appropriate
609 return plug_accepting(p->plug,
610 p->accepting_constructor, p->accepting_ctx);
613 if (change == PROXY_CHANGE_RECEIVE) {
614 /* we have received data from the underlying socket, which
615 * we'll need to parse, process, and respond to appropriately.
624 int min_ver, maj_ver, status;
626 /* get the status line */
627 len = bufchain_size(&p->pending_input_data);
628 assert(len > 0); /* or we wouldn't be here */
629 data = snewn(len+1, char);
630 bufchain_fetch(&p->pending_input_data, data, len);
632 * We must NUL-terminate this data, because Windows
633 * sscanf appears to require a NUL at the end of the
634 * string because it strlens it _first_. Sigh.
638 eol = get_line_end(data, len);
645 /* We can't rely on whether the %n incremented the sscanf return */
646 if (sscanf((char *)data, "HTTP/%i.%i %n",
647 &maj_ver, &min_ver, &status) < 2 || status == -1) {
648 plug_closing(p->plug, "Proxy error: HTTP response was absent",
649 PROXY_ERROR_GENERAL, 0);
654 /* remove the status line from the input buffer. */
655 bufchain_consume(&p->pending_input_data, eol);
656 if (data[status] != '2') {
660 while (eol > status &&
661 (data[eol-1] == '\r' || data[eol-1] == '\n'))
663 buf = dupprintf("Proxy error: %s", data+status);
664 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
677 /* get headers. we're done when we get a
678 * header of length 2, (ie. just "\r\n")
681 len = bufchain_size(&p->pending_input_data);
682 assert(len > 0); /* or we wouldn't be here */
683 data = snewn(len, char);
685 bufchain_fetch(&p->pending_input_data, data, len);
687 eol = get_line_end(datap, len);
694 bufchain_consume(&p->pending_input_data, eol);
697 eol = get_line_end(datap, len);
702 bufchain_consume(&p->pending_input_data, 2);
704 /* proxy activate will have dealt with
705 * whatever is left of the buffer */
715 plug_closing(p->plug, "Proxy error: unexpected proxy error",
716 PROXY_ERROR_UNEXPECTED, 0);
720 /* ----------------------------------------------------------------------
724 /* SOCKS version 4 */
725 int proxy_socks4_negotiate (Proxy_Socket p, int change)
727 if (p->state == PROXY_CHANGE_NEW) {
730 * version number (1 byte) = 4
731 * command code (1 byte)
734 * dest. port (2 bytes) [network order]
735 * dest. address (4 bytes)
736 * user ID (variable length, null terminated string)
739 int length, type, namelen;
740 char *command, addr[4], hostname[512];
743 type = sk_addrtype(p->remote_addr);
744 if (type == ADDRTYPE_IPV6) {
745 p->error = "Proxy error: SOCKS version 4 does not support IPv6";
747 } else if (type == ADDRTYPE_IPV4) {
749 sk_addrcopy(p->remote_addr, addr);
750 } else { /* type == ADDRTYPE_NAME */
751 assert(type == ADDRTYPE_NAME);
752 sk_getaddr(p->remote_addr, hostname, lenof(hostname));
753 namelen = strlen(hostname) + 1; /* include the NUL */
754 addr[0] = addr[1] = addr[2] = 0;
758 username = conf_get_str(p->conf, CONF_proxy_username);
759 length = strlen(username) + namelen + 9;
760 command = snewn(length, char);
761 strcpy(command + 8, username);
763 command[0] = 4; /* version 4 */
764 command[1] = 1; /* CONNECT command */
767 command[2] = (char) (p->remote_port >> 8) & 0xff;
768 command[3] = (char) p->remote_port & 0xff;
771 memcpy(command + 4, addr, 4);
774 memcpy(command + 8 + strlen(username) + 1,
777 sk_write(p->sub_socket, command, length);
785 if (change == PROXY_CHANGE_CLOSING) {
786 /* if our proxy negotiation process involves closing and opening
787 * new sockets, then we would want to intercept this closing
788 * callback when we were expecting it. if we aren't anticipating
789 * a socket close, then some error must have occurred. we'll
790 * just pass those errors up to the backend.
792 return plug_closing(p->plug, p->closing_error_msg,
793 p->closing_error_code,
794 p->closing_calling_back);
797 if (change == PROXY_CHANGE_SENT) {
798 /* some (or all) of what we wrote to the proxy was sent.
799 * we don't do anything new, however, until we receive the
800 * proxy's response. we might want to set a timer so we can
801 * timeout the proxy negotiation after a while...
806 if (change == PROXY_CHANGE_ACCEPTING) {
807 /* we should _never_ see this, as we are using our socket to
808 * connect to a proxy, not accepting inbound connections.
809 * what should we do? close the socket with an appropriate
812 return plug_accepting(p->plug,
813 p->accepting_constructor, p->accepting_ctx);
816 if (change == PROXY_CHANGE_RECEIVE) {
817 /* we have received data from the underlying socket, which
818 * we'll need to parse, process, and respond to appropriately.
823 * version number (1 byte) = 4
824 * reply code (1 byte)
825 * 90 = request granted
826 * 91 = request rejected or failed
827 * 92 = request rejected due to lack of IDENTD on client
828 * 93 = request rejected due to difference in user ID
829 * (what we sent vs. what IDENTD said)
830 * dest. port (2 bytes)
831 * dest. address (4 bytes)
836 if (bufchain_size(&p->pending_input_data) < 8)
837 return 1; /* not got anything yet */
839 /* get the response */
840 bufchain_fetch(&p->pending_input_data, data, 8);
843 plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "
844 "unexpected reply code version",
845 PROXY_ERROR_GENERAL, 0);
853 plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",
854 PROXY_ERROR_GENERAL, 0);
857 plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",
858 PROXY_ERROR_GENERAL, 0);
862 plug_closing(p->plug, "Proxy error: Error while communicating with proxy",
863 PROXY_ERROR_GENERAL, 0);
869 bufchain_consume(&p->pending_input_data, 8);
873 /* proxy activate will have dealt with
874 * whatever is left of the buffer */
879 plug_closing(p->plug, "Proxy error: unexpected proxy error",
880 PROXY_ERROR_UNEXPECTED, 0);
884 /* SOCKS version 5 */
885 int proxy_socks5_negotiate (Proxy_Socket p, int change)
887 if (p->state == PROXY_CHANGE_NEW) {
890 * version number (1 byte) = 5
891 * number of available authentication methods (1 byte)
892 * available authentication methods (1 byte * previous value)
893 * authentication methods:
894 * 0x00 = no authentication
896 * 0x02 = username/password
901 char *username, *password;
904 command[0] = 5; /* version 5 */
905 username = conf_get_str(p->conf, CONF_proxy_username);
906 password = conf_get_str(p->conf, CONF_proxy_password);
907 if (username[0] || password[0]) {
908 command[2] = 0x00; /* no authentication */
910 proxy_socks5_offerencryptedauth (command, &len);
911 command[len++] = 0x02; /* username/password */
912 command[1] = len - 2; /* Number of methods supported */
914 command[1] = 1; /* one methods supported: */
915 command[2] = 0x00; /* no authentication */
919 sk_write(p->sub_socket, command, len);
925 if (change == PROXY_CHANGE_CLOSING) {
926 /* if our proxy negotiation process involves closing and opening
927 * new sockets, then we would want to intercept this closing
928 * callback when we were expecting it. if we aren't anticipating
929 * a socket close, then some error must have occurred. we'll
930 * just pass those errors up to the backend.
932 return plug_closing(p->plug, p->closing_error_msg,
933 p->closing_error_code,
934 p->closing_calling_back);
937 if (change == PROXY_CHANGE_SENT) {
938 /* some (or all) of what we wrote to the proxy was sent.
939 * we don't do anything new, however, until we receive the
940 * proxy's response. we might want to set a timer so we can
941 * timeout the proxy negotiation after a while...
946 if (change == PROXY_CHANGE_ACCEPTING) {
947 /* we should _never_ see this, as we are using our socket to
948 * connect to a proxy, not accepting inbound connections.
949 * what should we do? close the socket with an appropriate
952 return plug_accepting(p->plug,
953 p->accepting_constructor, p->accepting_ctx);
956 if (change == PROXY_CHANGE_RECEIVE) {
957 /* we have received data from the underlying socket, which
958 * we'll need to parse, process, and respond to appropriately.
964 * version number (1 byte) = 5
965 * authentication method (1 byte)
966 * authentication methods:
967 * 0x00 = no authentication
969 * 0x02 = username/password
971 * 0xff = no acceptable methods
975 if (bufchain_size(&p->pending_input_data) < 2)
976 return 1; /* not got anything yet */
978 /* get the response */
979 bufchain_fetch(&p->pending_input_data, data, 2);
982 plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",
983 PROXY_ERROR_GENERAL, 0);
987 if (data[1] == 0x00) p->state = 2; /* no authentication needed */
988 else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */
989 else if (data[1] == 0x02) p->state = 5; /* username/password authentication */
990 else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */
992 plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",
993 PROXY_ERROR_GENERAL, 0);
996 bufchain_consume(&p->pending_input_data, 2);
1001 /* password authentication reply format:
1002 * version number (1 bytes) = 1
1003 * reply code (1 byte)
1009 if (bufchain_size(&p->pending_input_data) < 2)
1010 return 1; /* not got anything yet */
1012 /* get the response */
1013 bufchain_fetch(&p->pending_input_data, data, 2);
1016 plug_closing(p->plug, "Proxy error: SOCKS password "
1017 "subnegotiation contained wrong version number",
1018 PROXY_ERROR_GENERAL, 0);
1024 plug_closing(p->plug, "Proxy error: SOCKS proxy refused"
1025 " password authentication",
1026 PROXY_ERROR_GENERAL, 0);
1030 bufchain_consume(&p->pending_input_data, 2);
1031 p->state = 2; /* now proceed as authenticated */
1034 if (p->state == 8) {
1036 ret = proxy_socks5_handlechap(p);
1037 if (ret) return ret;
1040 if (p->state == 2) {
1043 * version number (1 byte) = 5
1044 * command code (1 byte)
1048 * reserved (1 byte) = 0x00
1049 * address type (1 byte)
1051 * 3 = domainname (first byte has length, no terminating null)
1053 * dest. address (variable)
1054 * dest. port (2 bytes) [network order]
1061 type = sk_addrtype(p->remote_addr);
1062 if (type == ADDRTYPE_IPV4) {
1063 len = 10; /* 4 hdr + 4 addr + 2 trailer */
1064 command[3] = 1; /* IPv4 */
1065 sk_addrcopy(p->remote_addr, command+4);
1066 } else if (type == ADDRTYPE_IPV6) {
1067 len = 22; /* 4 hdr + 16 addr + 2 trailer */
1068 command[3] = 4; /* IPv6 */
1069 sk_addrcopy(p->remote_addr, command+4);
1071 assert(type == ADDRTYPE_NAME);
1073 sk_getaddr(p->remote_addr, command+5, 256);
1074 command[4] = strlen(command+5);
1075 len = 7 + command[4]; /* 4 hdr, 1 len, N addr, 2 trailer */
1078 command[0] = 5; /* version 5 */
1079 command[1] = 1; /* CONNECT command */
1083 command[len-2] = (char) (p->remote_port >> 8) & 0xff;
1084 command[len-1] = (char) p->remote_port & 0xff;
1086 sk_write(p->sub_socket, command, len);
1092 if (p->state == 3) {
1095 * version number (1 bytes) = 5
1096 * reply code (1 byte)
1098 * 1 = general SOCKS server failure
1099 * 2 = connection not allowed by ruleset
1100 * 3 = network unreachable
1101 * 4 = host unreachable
1102 * 5 = connection refused
1104 * 7 = command not supported
1105 * 8 = address type not supported
1106 * reserved (1 byte) = x00
1107 * address type (1 byte)
1109 * 3 = domainname (first byte has length, no terminating null)
1111 * server bound address (variable)
1112 * server bound port (2 bytes) [network order]
1117 /* First 5 bytes of packet are enough to tell its length. */
1118 if (bufchain_size(&p->pending_input_data) < 5)
1119 return 1; /* not got anything yet */
1121 /* get the response */
1122 bufchain_fetch(&p->pending_input_data, data, 5);
1125 plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",
1126 PROXY_ERROR_GENERAL, 0);
1133 strcpy(buf, "Proxy error: ");
1136 case 1: strcat(buf, "General SOCKS server failure"); break;
1137 case 2: strcat(buf, "Connection not allowed by ruleset"); break;
1138 case 3: strcat(buf, "Network unreachable"); break;
1139 case 4: strcat(buf, "Host unreachable"); break;
1140 case 5: strcat(buf, "Connection refused"); break;
1141 case 6: strcat(buf, "TTL expired"); break;
1142 case 7: strcat(buf, "Command not supported"); break;
1143 case 8: strcat(buf, "Address type not supported"); break;
1144 default: sprintf(buf+strlen(buf),
1145 "Unrecognised SOCKS error code %d",
1149 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
1155 * Eat the rest of the reply packet.
1157 len = 6; /* first 4 bytes, last 2 */
1159 case 1: len += 4; break; /* IPv4 address */
1160 case 4: len += 16; break;/* IPv6 address */
1161 case 3: len += (unsigned char)data[4]; break; /* domain name */
1163 plug_closing(p->plug, "Proxy error: SOCKS proxy returned "
1164 "unrecognised address format",
1165 PROXY_ERROR_GENERAL, 0);
1168 if (bufchain_size(&p->pending_input_data) < len)
1169 return 1; /* not got whole reply yet */
1170 bufchain_consume(&p->pending_input_data, len);
1177 if (p->state == 4) {
1178 /* TODO: Handle GSSAPI authentication */
1179 plug_closing(p->plug, "Proxy error: We don't support GSSAPI authentication",
1180 PROXY_ERROR_GENERAL, 0);
1184 if (p->state == 5) {
1185 char *username = conf_get_str(p->conf, CONF_proxy_username);
1186 char *password = conf_get_str(p->conf, CONF_proxy_password);
1187 if (username[0] || password[0]) {
1188 char userpwbuf[255 + 255 + 3];
1190 ulen = strlen(username);
1191 if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
1192 plen = strlen(password);
1193 if (plen > 255) plen = 255; if (plen < 1) plen = 1;
1194 userpwbuf[0] = 1; /* version number of subnegotiation */
1195 userpwbuf[1] = ulen;
1196 memcpy(userpwbuf+2, username, ulen);
1197 userpwbuf[ulen+2] = plen;
1198 memcpy(userpwbuf+ulen+3, password, plen);
1199 sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);
1202 plug_closing(p->plug, "Proxy error: Server chose "
1203 "username/password authentication but we "
1205 PROXY_ERROR_GENERAL, 0);
1209 if (p->state == 6) {
1211 ret = proxy_socks5_selectchap(p);
1212 if (ret) return ret;
1217 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
1218 PROXY_ERROR_UNEXPECTED, 0);
1222 /* ----------------------------------------------------------------------
1223 * `Telnet' proxy type.
1225 * (This is for ad-hoc proxies where you connect to the proxy's
1226 * telnet port and send a command such as `connect host port'. The
1227 * command is configurable, since this proxy type is typically not
1228 * standardised or at all well-defined.)
1231 char *format_telnet_command(SockAddr addr, int port, Conf *conf)
1233 char *fmt = conf_get_str(conf, CONF_proxy_telnet_command);
1235 int retlen = 0, retsize = 0;
1237 #define ENSURE(n) do { \
1238 if (retsize < retlen + n) { \
1239 retsize = retlen + n + 512; \
1240 ret = sresize(ret, retsize, char); \
1244 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1245 * %%, %host, %port, %user, and %pass
1248 while (fmt[eo] != 0) {
1250 /* scan forward until we hit end-of-line,
1251 * or an escape character (\ or %) */
1252 while (fmt[eo] != 0 && fmt[eo] != '%' && fmt[eo] != '\\')
1255 /* if we hit eol, break out of our escaping loop */
1256 if (fmt[eo] == 0) break;
1258 /* if there was any unescaped text before the escape
1259 * character, send that now */
1262 memcpy(ret + retlen, fmt + so, eo - so);
1268 /* if the escape character was the last character of
1269 * the line, we'll just stop and send it. */
1270 if (fmt[eo] == 0) break;
1272 if (fmt[so] == '\\') {
1274 /* we recognize \\, \%, \r, \n, \t, \x??.
1275 * anything else, we just send unescaped (including the \).
1282 ret[retlen++] = '\\';
1288 ret[retlen++] = '%';
1294 ret[retlen++] = '\r';
1300 ret[retlen++] = '\n';
1306 ret[retlen++] = '\t';
1313 /* escaped hexadecimal value (ie. \xff) */
1314 unsigned char v = 0;
1319 if (fmt[eo] >= '0' && fmt[eo] <= '9')
1321 else if (fmt[eo] >= 'a' && fmt[eo] <= 'f')
1322 v += fmt[eo] - 'a' + 10;
1323 else if (fmt[eo] >= 'A' && fmt[eo] <= 'F')
1324 v += fmt[eo] - 'A' + 10;
1326 /* non hex character, so we abort and just
1327 * send the whole thing unescaped (including \x)
1330 ret[retlen++] = '\\';
1335 /* we only extract two hex characters */
1351 memcpy(ret+retlen, fmt + so, 2);
1358 /* % escape. we recognize %%, %host, %port, %user, %pass.
1359 * %proxyhost, %proxyport. Anything else we just send
1360 * unescaped (including the %).
1363 if (fmt[eo] == '%') {
1365 ret[retlen++] = '%';
1368 else if (strnicmp(fmt + eo, "host", 4) == 0) {
1371 sk_getaddr(addr, dest, lenof(dest));
1372 destlen = strlen(dest);
1374 memcpy(ret+retlen, dest, destlen);
1378 else if (strnicmp(fmt + eo, "port", 4) == 0) {
1379 char portstr[8], portlen;
1380 portlen = sprintf(portstr, "%i", port);
1382 memcpy(ret + retlen, portstr, portlen);
1386 else if (strnicmp(fmt + eo, "user", 4) == 0) {
1387 char *username = conf_get_str(conf, CONF_proxy_username);
1388 int userlen = strlen(username);
1390 memcpy(ret+retlen, username, userlen);
1394 else if (strnicmp(fmt + eo, "pass", 4) == 0) {
1395 char *password = conf_get_str(conf, CONF_proxy_password);
1396 int passlen = strlen(password);
1398 memcpy(ret+retlen, password, passlen);
1402 else if (strnicmp(fmt + eo, "proxyhost", 9) == 0) {
1403 char *host = conf_get_str(conf, CONF_proxy_host);
1404 int phlen = strlen(host);
1406 memcpy(ret+retlen, host, phlen);
1410 else if (strnicmp(fmt + eo, "proxyport", 9) == 0) {
1411 int port = conf_get_int(conf, CONF_proxy_port);
1414 sprintf(pport, "%d", port);
1415 pplen = strlen(pport);
1417 memcpy(ret+retlen, pport, pplen);
1422 /* we don't escape this, so send the % now, and
1423 * don't advance eo, so that we'll consider the
1424 * text immediately following the % as unescaped.
1427 ret[retlen++] = '%';
1431 /* resume scanning for additional escapes after this one. */
1435 /* if there is any unescaped text at the end of the line, send it */
1438 memcpy(ret + retlen, fmt + so, eo - so);
1449 int proxy_telnet_negotiate (Proxy_Socket p, int change)
1451 if (p->state == PROXY_CHANGE_NEW) {
1452 char *formatted_cmd;
1454 formatted_cmd = format_telnet_command(p->remote_addr, p->remote_port,
1457 sk_write(p->sub_socket, formatted_cmd, strlen(formatted_cmd));
1458 sfree(formatted_cmd);
1464 if (change == PROXY_CHANGE_CLOSING) {
1465 /* if our proxy negotiation process involves closing and opening
1466 * new sockets, then we would want to intercept this closing
1467 * callback when we were expecting it. if we aren't anticipating
1468 * a socket close, then some error must have occurred. we'll
1469 * just pass those errors up to the backend.
1471 return plug_closing(p->plug, p->closing_error_msg,
1472 p->closing_error_code,
1473 p->closing_calling_back);
1476 if (change == PROXY_CHANGE_SENT) {
1477 /* some (or all) of what we wrote to the proxy was sent.
1478 * we don't do anything new, however, until we receive the
1479 * proxy's response. we might want to set a timer so we can
1480 * timeout the proxy negotiation after a while...
1485 if (change == PROXY_CHANGE_ACCEPTING) {
1486 /* we should _never_ see this, as we are using our socket to
1487 * connect to a proxy, not accepting inbound connections.
1488 * what should we do? close the socket with an appropriate
1491 return plug_accepting(p->plug,
1492 p->accepting_constructor, p->accepting_ctx);
1495 if (change == PROXY_CHANGE_RECEIVE) {
1496 /* we have received data from the underlying socket, which
1497 * we'll need to parse, process, and respond to appropriately.
1502 /* proxy activate will have dealt with
1503 * whatever is left of the buffer */
1507 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
1508 PROXY_ERROR_UNEXPECTED, 0);