2 * Network proxy abstraction in PuTTY
\r
4 * A proxy layer, if necessary, wedges itself between the network
\r
5 * code and the higher level backend.
\r
12 #define DEFINE_PLUG_METHOD_MACROS
\r
14 #include "network.h"
\r
17 #define do_proxy_dns(cfg) \
\r
18 (cfg->proxy_dns == FORCE_ON || \
\r
19 (cfg->proxy_dns == AUTO && cfg->proxy_type != PROXY_SOCKS4))
\r
22 * Call this when proxy negotiation is complete, so that this
\r
23 * socket can begin working normally.
\r
25 void proxy_activate (Proxy_Socket p)
\r
29 long output_before, output_after;
\r
31 p->state = PROXY_STATE_ACTIVE;
\r
33 /* we want to ignore new receive events until we have sent
\r
34 * all of our buffered receive data.
\r
36 sk_set_frozen(p->sub_socket, 1);
\r
38 /* how many bytes of output have we buffered? */
\r
39 output_before = bufchain_size(&p->pending_oob_output_data) +
\r
40 bufchain_size(&p->pending_output_data);
\r
41 /* and keep track of how many bytes do not get sent. */
\r
44 /* send buffered OOB writes */
\r
45 while (bufchain_size(&p->pending_oob_output_data) > 0) {
\r
46 bufchain_prefix(&p->pending_oob_output_data, &data, &len);
\r
47 output_after += sk_write_oob(p->sub_socket, data, len);
\r
48 bufchain_consume(&p->pending_oob_output_data, len);
\r
51 /* send buffered normal writes */
\r
52 while (bufchain_size(&p->pending_output_data) > 0) {
\r
53 bufchain_prefix(&p->pending_output_data, &data, &len);
\r
54 output_after += sk_write(p->sub_socket, data, len);
\r
55 bufchain_consume(&p->pending_output_data, len);
\r
58 /* if we managed to send any data, let the higher levels know. */
\r
59 if (output_after < output_before)
\r
60 plug_sent(p->plug, output_after);
\r
62 /* if we were asked to flush the output during
\r
63 * the proxy negotiation process, do so now.
\r
65 if (p->pending_flush) sk_flush(p->sub_socket);
\r
67 /* if the backend wanted the socket unfrozen, try to unfreeze.
\r
68 * our set_frozen handler will flush buffered receive data before
\r
69 * unfreezing the actual underlying socket.
\r
72 sk_set_frozen((Socket)p, 0);
\r
75 /* basic proxy socket functions */
\r
77 static Plug sk_proxy_plug (Socket s, Plug p)
\r
79 Proxy_Socket ps = (Proxy_Socket) s;
\r
80 Plug ret = ps->plug;
\r
86 static void sk_proxy_close (Socket s)
\r
88 Proxy_Socket ps = (Proxy_Socket) s;
\r
90 sk_close(ps->sub_socket);
\r
91 sk_addr_free(ps->remote_addr);
\r
95 static int sk_proxy_write (Socket s, const char *data, int len)
\r
97 Proxy_Socket ps = (Proxy_Socket) s;
\r
99 if (ps->state != PROXY_STATE_ACTIVE) {
\r
100 bufchain_add(&ps->pending_output_data, data, len);
\r
101 return bufchain_size(&ps->pending_output_data);
\r
103 return sk_write(ps->sub_socket, data, len);
\r
106 static int sk_proxy_write_oob (Socket s, const char *data, int len)
\r
108 Proxy_Socket ps = (Proxy_Socket) s;
\r
110 if (ps->state != PROXY_STATE_ACTIVE) {
\r
111 bufchain_clear(&ps->pending_output_data);
\r
112 bufchain_clear(&ps->pending_oob_output_data);
\r
113 bufchain_add(&ps->pending_oob_output_data, data, len);
\r
116 return sk_write_oob(ps->sub_socket, data, len);
\r
119 static void sk_proxy_flush (Socket s)
\r
121 Proxy_Socket ps = (Proxy_Socket) s;
\r
123 if (ps->state != PROXY_STATE_ACTIVE) {
\r
124 ps->pending_flush = 1;
\r
127 sk_flush(ps->sub_socket);
\r
130 static void sk_proxy_set_private_ptr (Socket s, void *ptr)
\r
132 Proxy_Socket ps = (Proxy_Socket) s;
\r
133 sk_set_private_ptr(ps->sub_socket, ptr);
\r
136 static void * sk_proxy_get_private_ptr (Socket s)
\r
138 Proxy_Socket ps = (Proxy_Socket) s;
\r
139 return sk_get_private_ptr(ps->sub_socket);
\r
142 static void sk_proxy_set_frozen (Socket s, int is_frozen)
\r
144 Proxy_Socket ps = (Proxy_Socket) s;
\r
146 if (ps->state != PROXY_STATE_ACTIVE) {
\r
147 ps->freeze = is_frozen;
\r
151 /* handle any remaining buffered recv data first */
\r
152 if (bufchain_size(&ps->pending_input_data) > 0) {
\r
153 ps->freeze = is_frozen;
\r
155 /* loop while we still have buffered data, and while we are
\r
156 * unfrozen. the plug_receive call in the loop could result
\r
157 * in a call back into this function refreezing the socket,
\r
158 * so we have to check each time.
\r
160 while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {
\r
164 bufchain_prefix(&ps->pending_input_data, &data, &len);
\r
165 if (len > lenof(databuf))
\r
166 len = lenof(databuf);
\r
167 memcpy(databuf, data, len);
\r
168 bufchain_consume(&ps->pending_input_data, len);
\r
169 plug_receive(ps->plug, 0, databuf, len);
\r
172 /* if we're still frozen, we'll have to wait for another
\r
173 * call from the backend to finish unbuffering the data.
\r
175 if (ps->freeze) return;
\r
178 sk_set_frozen(ps->sub_socket, is_frozen);
\r
181 static const char * sk_proxy_socket_error (Socket s)
\r
183 Proxy_Socket ps = (Proxy_Socket) s;
\r
184 if (ps->error != NULL || ps->sub_socket == NULL) {
\r
187 return sk_socket_error(ps->sub_socket);
\r
190 /* basic proxy plug functions */
\r
192 static void plug_proxy_log(Plug plug, int type, SockAddr addr, int port,
\r
193 const char *error_msg, int error_code)
\r
195 Proxy_Plug pp = (Proxy_Plug) plug;
\r
196 Proxy_Socket ps = pp->proxy_socket;
\r
198 plug_log(ps->plug, type, addr, port, error_msg, error_code);
\r
201 static int plug_proxy_closing (Plug p, const char *error_msg,
\r
202 int error_code, int calling_back)
\r
204 Proxy_Plug pp = (Proxy_Plug) p;
\r
205 Proxy_Socket ps = pp->proxy_socket;
\r
207 if (ps->state != PROXY_STATE_ACTIVE) {
\r
208 ps->closing_error_msg = error_msg;
\r
209 ps->closing_error_code = error_code;
\r
210 ps->closing_calling_back = calling_back;
\r
211 return ps->negotiate(ps, PROXY_CHANGE_CLOSING);
\r
213 return plug_closing(ps->plug, error_msg,
\r
214 error_code, calling_back);
\r
217 static int plug_proxy_receive (Plug p, int urgent, char *data, int len)
\r
219 Proxy_Plug pp = (Proxy_Plug) p;
\r
220 Proxy_Socket ps = pp->proxy_socket;
\r
222 if (ps->state != PROXY_STATE_ACTIVE) {
\r
223 /* we will lose the urgentness of this data, but since most,
\r
224 * if not all, of this data will be consumed by the negotiation
\r
225 * process, hopefully it won't affect the protocol above us
\r
227 bufchain_add(&ps->pending_input_data, data, len);
\r
228 ps->receive_urgent = urgent;
\r
229 ps->receive_data = data;
\r
230 ps->receive_len = len;
\r
231 return ps->negotiate(ps, PROXY_CHANGE_RECEIVE);
\r
233 return plug_receive(ps->plug, urgent, data, len);
\r
236 static void plug_proxy_sent (Plug p, int bufsize)
\r
238 Proxy_Plug pp = (Proxy_Plug) p;
\r
239 Proxy_Socket ps = pp->proxy_socket;
\r
241 if (ps->state != PROXY_STATE_ACTIVE) {
\r
242 ps->sent_bufsize = bufsize;
\r
243 ps->negotiate(ps, PROXY_CHANGE_SENT);
\r
246 plug_sent(ps->plug, bufsize);
\r
249 static int plug_proxy_accepting (Plug p, OSSocket sock)
\r
251 Proxy_Plug pp = (Proxy_Plug) p;
\r
252 Proxy_Socket ps = pp->proxy_socket;
\r
254 if (ps->state != PROXY_STATE_ACTIVE) {
\r
255 ps->accepting_sock = sock;
\r
256 return ps->negotiate(ps, PROXY_CHANGE_ACCEPTING);
\r
258 return plug_accepting(ps->plug, sock);
\r
262 * This function can accept a NULL pointer as `addr', in which case
\r
263 * it will only check the host name.
\r
265 static int proxy_for_destination (SockAddr addr, char *hostname, int port,
\r
270 int hostip_len, hostname_len;
\r
271 const char *exclude_list;
\r
274 * Check the host name and IP against the hard-coded
\r
275 * representations of `localhost'.
\r
277 if (!cfg->even_proxy_localhost &&
\r
278 (sk_hostname_is_local(hostname) ||
\r
279 (addr && sk_address_is_local(addr))))
\r
280 return 0; /* do not proxy */
\r
282 /* we want a string representation of the IP address for comparisons */
\r
284 sk_getaddr(addr, hostip, 64);
\r
285 hostip_len = strlen(hostip);
\r
287 hostip_len = 0; /* placate gcc; shouldn't be required */
\r
289 hostname_len = strlen(hostname);
\r
291 exclude_list = cfg->proxy_exclude_list;
\r
293 /* now parse the exclude list, and see if either our IP
\r
294 * or hostname matches anything in it.
\r
297 while (exclude_list[s]) {
\r
298 while (exclude_list[s] &&
\r
299 (isspace((unsigned char)exclude_list[s]) ||
\r
300 exclude_list[s] == ',')) s++;
\r
302 if (!exclude_list[s]) break;
\r
306 while (exclude_list[e] &&
\r
307 (isalnum((unsigned char)exclude_list[e]) ||
\r
308 exclude_list[e] == '-' ||
\r
309 exclude_list[e] == '.' ||
\r
310 exclude_list[e] == '*')) e++;
\r
312 if (exclude_list[s] == '*') {
\r
313 /* wildcard at beginning of entry */
\r
315 if ((addr && strnicmp(hostip + hostip_len - (e - s - 1),
\r
316 exclude_list + s + 1, e - s - 1) == 0) ||
\r
317 strnicmp(hostname + hostname_len - (e - s - 1),
\r
318 exclude_list + s + 1, e - s - 1) == 0)
\r
319 return 0; /* IP/hostname range excluded. do not use proxy. */
\r
321 } else if (exclude_list[e-1] == '*') {
\r
322 /* wildcard at end of entry */
\r
324 if ((addr && strnicmp(hostip, exclude_list + s, e - s - 1) == 0) ||
\r
325 strnicmp(hostname, exclude_list + s, e - s - 1) == 0)
\r
326 return 0; /* IP/hostname range excluded. do not use proxy. */
\r
329 /* no wildcard at either end, so let's try an absolute
\r
330 * match (ie. a specific IP)
\r
333 if (addr && strnicmp(hostip, exclude_list + s, e - s) == 0)
\r
334 return 0; /* IP/hostname excluded. do not use proxy. */
\r
335 if (strnicmp(hostname, exclude_list + s, e - s) == 0)
\r
336 return 0; /* IP/hostname excluded. do not use proxy. */
\r
341 /* Make sure we really have reached the next comma or end-of-string */
\r
342 while (exclude_list[s] &&
\r
343 !isspace((unsigned char)exclude_list[s]) &&
\r
344 exclude_list[s] != ',') s++;
\r
347 /* no matches in the exclude list, so use the proxy */
\r
351 SockAddr name_lookup(char *host, int port, char **canonicalname,
\r
352 const Config *cfg, int addressfamily)
\r
354 if (cfg->proxy_type != PROXY_NONE &&
\r
355 do_proxy_dns(cfg) &&
\r
356 proxy_for_destination(NULL, host, port, cfg)) {
\r
357 *canonicalname = dupstr(host);
\r
358 return sk_nonamelookup(host);
\r
361 return sk_namelookup(host, canonicalname, addressfamily);
\r
364 Socket new_connection(SockAddr addr, char *hostname,
\r
365 int port, int privport,
\r
366 int oobinline, int nodelay, int keepalive,
\r
367 Plug plug, const Config *cfg)
\r
369 static const struct socket_function_table socket_fn_table = {
\r
373 sk_proxy_write_oob,
\r
375 sk_proxy_set_private_ptr,
\r
376 sk_proxy_get_private_ptr,
\r
377 sk_proxy_set_frozen,
\r
378 sk_proxy_socket_error
\r
381 static const struct plug_function_table plug_fn_table = {
\r
383 plug_proxy_closing,
\r
384 plug_proxy_receive,
\r
386 plug_proxy_accepting
\r
389 if (cfg->proxy_type != PROXY_NONE &&
\r
390 proxy_for_destination(addr, hostname, port, cfg))
\r
394 SockAddr proxy_addr;
\r
395 char *proxy_canonical_name;
\r
398 if ((sret = platform_new_connection(addr, hostname, port, privport,
\r
399 oobinline, nodelay, keepalive,
\r
404 ret = snew(struct Socket_proxy_tag);
\r
405 ret->fn = &socket_fn_table;
\r
406 ret->cfg = *cfg; /* STRUCTURE COPY */
\r
408 ret->remote_addr = addr; /* will need to be freed on close */
\r
409 ret->remote_port = port;
\r
412 ret->pending_flush = 0;
\r
415 bufchain_init(&ret->pending_input_data);
\r
416 bufchain_init(&ret->pending_output_data);
\r
417 bufchain_init(&ret->pending_oob_output_data);
\r
419 ret->sub_socket = NULL;
\r
420 ret->state = PROXY_STATE_NEW;
\r
421 ret->negotiate = NULL;
\r
423 if (cfg->proxy_type == PROXY_HTTP) {
\r
424 ret->negotiate = proxy_http_negotiate;
\r
425 } else if (cfg->proxy_type == PROXY_SOCKS4) {
\r
426 ret->negotiate = proxy_socks4_negotiate;
\r
427 } else if (cfg->proxy_type == PROXY_SOCKS5) {
\r
428 ret->negotiate = proxy_socks5_negotiate;
\r
429 } else if (cfg->proxy_type == PROXY_TELNET) {
\r
430 ret->negotiate = proxy_telnet_negotiate;
\r
432 ret->error = "Proxy error: Unknown proxy method";
\r
433 return (Socket) ret;
\r
436 /* create the proxy plug to map calls from the actual
\r
437 * socket into our proxy socket layer */
\r
438 pplug = snew(struct Plug_proxy_tag);
\r
439 pplug->fn = &plug_fn_table;
\r
440 pplug->proxy_socket = ret;
\r
442 /* look-up proxy */
\r
443 proxy_addr = sk_namelookup(cfg->proxy_host,
\r
444 &proxy_canonical_name, cfg->addressfamily);
\r
445 if (sk_addr_error(proxy_addr) != NULL) {
\r
446 ret->error = "Proxy error: Unable to resolve proxy host name";
\r
447 return (Socket)ret;
\r
449 sfree(proxy_canonical_name);
\r
451 /* create the actual socket we will be using,
\r
452 * connected to our proxy server and port.
\r
454 ret->sub_socket = sk_new(proxy_addr, cfg->proxy_port,
\r
455 privport, oobinline,
\r
456 nodelay, keepalive, (Plug) pplug);
\r
457 if (sk_socket_error(ret->sub_socket) != NULL)
\r
458 return (Socket) ret;
\r
460 /* start the proxy negotiation process... */
\r
461 sk_set_frozen(ret->sub_socket, 0);
\r
462 ret->negotiate(ret, PROXY_CHANGE_NEW);
\r
464 return (Socket) ret;
\r
467 /* no proxy, so just return the direct socket */
\r
468 return sk_new(addr, port, privport, oobinline, nodelay, keepalive, plug);
\r
471 Socket new_listener(char *srcaddr, int port, Plug plug, int local_host_only,
\r
472 const Config *cfg, int addressfamily)
\r
474 /* TODO: SOCKS (and potentially others) support inbound
\r
475 * TODO: connections via the proxy. support them.
\r
478 return sk_newlistener(srcaddr, port, plug, local_host_only, addressfamily);
\r
481 /* ----------------------------------------------------------------------
\r
482 * HTTP CONNECT proxy type.
\r
485 static int get_line_end (char * data, int len)
\r
491 if (data[off] == '\n') {
\r
492 /* we have a newline */
\r
495 /* is that the only thing on this line? */
\r
496 if (off <= 2) return off;
\r
498 /* if not, then there is the possibility that this header
\r
499 * continues onto the next line, if it starts with a space
\r
503 if (off + 1 < len &&
\r
504 data[off+1] != ' ' &&
\r
505 data[off+1] != '\t') return off;
\r
507 /* the line does continue, so we have to keep going
\r
508 * until we see an the header's "real" end of line.
\r
519 int proxy_http_negotiate (Proxy_Socket p, int change)
\r
521 if (p->state == PROXY_STATE_NEW) {
\r
522 /* we are just beginning the proxy negotiate process,
\r
523 * so we'll send off the initial bits of the request.
\r
524 * for this proxy method, it's just a simple HTTP
\r
527 char *buf, dest[512];
\r
529 sk_getaddr(p->remote_addr, dest, lenof(dest));
\r
531 buf = dupprintf("CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
\r
532 dest, p->remote_port, dest, p->remote_port);
\r
533 sk_write(p->sub_socket, buf, strlen(buf));
\r
536 if (p->cfg.proxy_username[0] || p->cfg.proxy_password[0]) {
\r
537 char buf[sizeof(p->cfg.proxy_username)+sizeof(p->cfg.proxy_password)];
\r
538 char buf2[sizeof(buf)*4/3 + 100];
\r
540 sprintf(buf, "%s:%s", p->cfg.proxy_username, p->cfg.proxy_password);
\r
542 sprintf(buf2, "Proxy-Authorization: Basic ");
\r
543 for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)
\r
544 base64_encode_atom((unsigned char *)(buf+i),
\r
545 (len-i > 3 ? 3 : len-i), buf2+j);
\r
546 strcpy(buf2+j, "\r\n");
\r
547 sk_write(p->sub_socket, buf2, strlen(buf2));
\r
550 sk_write(p->sub_socket, "\r\n", 2);
\r
556 if (change == PROXY_CHANGE_CLOSING) {
\r
557 /* if our proxy negotiation process involves closing and opening
\r
558 * new sockets, then we would want to intercept this closing
\r
559 * callback when we were expecting it. if we aren't anticipating
\r
560 * a socket close, then some error must have occurred. we'll
\r
561 * just pass those errors up to the backend.
\r
563 return plug_closing(p->plug, p->closing_error_msg,
\r
564 p->closing_error_code,
\r
565 p->closing_calling_back);
\r
568 if (change == PROXY_CHANGE_SENT) {
\r
569 /* some (or all) of what we wrote to the proxy was sent.
\r
570 * we don't do anything new, however, until we receive the
\r
571 * proxy's response. we might want to set a timer so we can
\r
572 * timeout the proxy negotiation after a while...
\r
577 if (change == PROXY_CHANGE_ACCEPTING) {
\r
578 /* we should _never_ see this, as we are using our socket to
\r
579 * connect to a proxy, not accepting inbound connections.
\r
580 * what should we do? close the socket with an appropriate
\r
583 return plug_accepting(p->plug, p->accepting_sock);
\r
586 if (change == PROXY_CHANGE_RECEIVE) {
\r
587 /* we have received data from the underlying socket, which
\r
588 * we'll need to parse, process, and respond to appropriately.
\r
591 char *data, *datap;
\r
595 if (p->state == 1) {
\r
597 int min_ver, maj_ver, status;
\r
599 /* get the status line */
\r
600 len = bufchain_size(&p->pending_input_data);
\r
601 assert(len > 0); /* or we wouldn't be here */
\r
602 data = snewn(len+1, char);
\r
603 bufchain_fetch(&p->pending_input_data, data, len);
\r
605 * We must NUL-terminate this data, because Windows
\r
606 * sscanf appears to require a NUL at the end of the
\r
607 * string because it strlens it _first_. Sigh.
\r
611 eol = get_line_end(data, len);
\r
618 /* We can't rely on whether the %n incremented the sscanf return */
\r
619 if (sscanf((char *)data, "HTTP/%i.%i %n",
\r
620 &maj_ver, &min_ver, &status) < 2 || status == -1) {
\r
621 plug_closing(p->plug, "Proxy error: HTTP response was absent",
\r
622 PROXY_ERROR_GENERAL, 0);
\r
627 /* remove the status line from the input buffer. */
\r
628 bufchain_consume(&p->pending_input_data, eol);
\r
629 if (data[status] != '2') {
\r
633 while (eol > status &&
\r
634 (data[eol-1] == '\r' || data[eol-1] == '\n'))
\r
635 data[--eol] = '\0';
\r
636 buf = dupprintf("Proxy error: %s", data+status);
\r
637 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
\r
648 if (p->state == 2) {
\r
650 /* get headers. we're done when we get a
\r
651 * header of length 2, (ie. just "\r\n")
\r
654 len = bufchain_size(&p->pending_input_data);
\r
655 assert(len > 0); /* or we wouldn't be here */
\r
656 data = snewn(len, char);
\r
658 bufchain_fetch(&p->pending_input_data, data, len);
\r
660 eol = get_line_end(datap, len);
\r
667 bufchain_consume(&p->pending_input_data, eol);
\r
670 eol = get_line_end(datap, len);
\r
675 bufchain_consume(&p->pending_input_data, 2);
\r
677 /* proxy activate will have dealt with
\r
678 * whatever is left of the buffer */
\r
688 plug_closing(p->plug, "Proxy error: unexpected proxy error",
\r
689 PROXY_ERROR_UNEXPECTED, 0);
\r
693 /* ----------------------------------------------------------------------
\r
694 * SOCKS proxy type.
\r
697 /* SOCKS version 4 */
\r
698 int proxy_socks4_negotiate (Proxy_Socket p, int change)
\r
700 if (p->state == PROXY_CHANGE_NEW) {
\r
703 * version number (1 byte) = 4
\r
704 * command code (1 byte)
\r
707 * dest. port (2 bytes) [network order]
\r
708 * dest. address (4 bytes)
\r
709 * user ID (variable length, null terminated string)
\r
712 int length, type, namelen;
\r
713 char *command, addr[4], hostname[512];
\r
715 type = sk_addrtype(p->remote_addr);
\r
716 if (type == ADDRTYPE_IPV6) {
\r
717 plug_closing(p->plug, "Proxy error: SOCKS version 4 does"
\r
718 " not support IPv6", PROXY_ERROR_GENERAL, 0);
\r
720 } else if (type == ADDRTYPE_IPV4) {
\r
722 sk_addrcopy(p->remote_addr, addr);
\r
723 } else { /* type == ADDRTYPE_NAME */
\r
724 assert(type == ADDRTYPE_NAME);
\r
725 sk_getaddr(p->remote_addr, hostname, lenof(hostname));
\r
726 namelen = strlen(hostname) + 1; /* include the NUL */
\r
727 addr[0] = addr[1] = addr[2] = 0;
\r
731 length = strlen(p->cfg.proxy_username) + namelen + 9;
\r
732 command = snewn(length, char);
\r
733 strcpy(command + 8, p->cfg.proxy_username);
\r
735 command[0] = 4; /* version 4 */
\r
736 command[1] = 1; /* CONNECT command */
\r
739 command[2] = (char) (p->remote_port >> 8) & 0xff;
\r
740 command[3] = (char) p->remote_port & 0xff;
\r
743 memcpy(command + 4, addr, 4);
\r
746 memcpy(command + 8 + strlen(p->cfg.proxy_username) + 1,
\r
747 hostname, namelen);
\r
749 sk_write(p->sub_socket, command, length);
\r
756 if (change == PROXY_CHANGE_CLOSING) {
\r
757 /* if our proxy negotiation process involves closing and opening
\r
758 * new sockets, then we would want to intercept this closing
\r
759 * callback when we were expecting it. if we aren't anticipating
\r
760 * a socket close, then some error must have occurred. we'll
\r
761 * just pass those errors up to the backend.
\r
763 return plug_closing(p->plug, p->closing_error_msg,
\r
764 p->closing_error_code,
\r
765 p->closing_calling_back);
\r
768 if (change == PROXY_CHANGE_SENT) {
\r
769 /* some (or all) of what we wrote to the proxy was sent.
\r
770 * we don't do anything new, however, until we receive the
\r
771 * proxy's response. we might want to set a timer so we can
\r
772 * timeout the proxy negotiation after a while...
\r
777 if (change == PROXY_CHANGE_ACCEPTING) {
\r
778 /* we should _never_ see this, as we are using our socket to
\r
779 * connect to a proxy, not accepting inbound connections.
\r
780 * what should we do? close the socket with an appropriate
\r
783 return plug_accepting(p->plug, p->accepting_sock);
\r
786 if (change == PROXY_CHANGE_RECEIVE) {
\r
787 /* we have received data from the underlying socket, which
\r
788 * we'll need to parse, process, and respond to appropriately.
\r
791 if (p->state == 1) {
\r
792 /* response format:
\r
793 * version number (1 byte) = 4
\r
794 * reply code (1 byte)
\r
795 * 90 = request granted
\r
796 * 91 = request rejected or failed
\r
797 * 92 = request rejected due to lack of IDENTD on client
\r
798 * 93 = request rejected due to difference in user ID
\r
799 * (what we sent vs. what IDENTD said)
\r
800 * dest. port (2 bytes)
\r
801 * dest. address (4 bytes)
\r
806 if (bufchain_size(&p->pending_input_data) < 8)
\r
807 return 1; /* not got anything yet */
\r
809 /* get the response */
\r
810 bufchain_fetch(&p->pending_input_data, data, 8);
\r
812 if (data[0] != 0) {
\r
813 plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "
\r
814 "unexpected reply code version",
\r
815 PROXY_ERROR_GENERAL, 0);
\r
819 if (data[1] != 90) {
\r
823 plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",
\r
824 PROXY_ERROR_GENERAL, 0);
\r
827 plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",
\r
828 PROXY_ERROR_GENERAL, 0);
\r
832 plug_closing(p->plug, "Proxy error: Error while communicating with proxy",
\r
833 PROXY_ERROR_GENERAL, 0);
\r
839 bufchain_consume(&p->pending_input_data, 8);
\r
843 /* proxy activate will have dealt with
\r
844 * whatever is left of the buffer */
\r
849 plug_closing(p->plug, "Proxy error: unexpected proxy error",
\r
850 PROXY_ERROR_UNEXPECTED, 0);
\r
854 /* SOCKS version 5 */
\r
855 int proxy_socks5_negotiate (Proxy_Socket p, int change)
\r
857 if (p->state == PROXY_CHANGE_NEW) {
\r
859 /* initial command:
\r
860 * version number (1 byte) = 5
\r
861 * number of available authentication methods (1 byte)
\r
862 * available authentication methods (1 byte * previous value)
\r
863 * authentication methods:
\r
864 * 0x00 = no authentication
\r
866 * 0x02 = username/password
\r
873 command[0] = 5; /* version 5 */
\r
874 if (p->cfg.proxy_username[0] || p->cfg.proxy_password[0]) {
\r
875 command[2] = 0x00; /* no authentication */
\r
877 proxy_socks5_offerencryptedauth (command, &len);
\r
878 command[len++] = 0x02; /* username/password */
\r
879 command[1] = len - 2; /* Number of methods supported */
\r
881 command[1] = 1; /* one methods supported: */
\r
882 command[2] = 0x00; /* no authentication */
\r
886 sk_write(p->sub_socket, command, len);
\r
892 if (change == PROXY_CHANGE_CLOSING) {
\r
893 /* if our proxy negotiation process involves closing and opening
\r
894 * new sockets, then we would want to intercept this closing
\r
895 * callback when we were expecting it. if we aren't anticipating
\r
896 * a socket close, then some error must have occurred. we'll
\r
897 * just pass those errors up to the backend.
\r
899 return plug_closing(p->plug, p->closing_error_msg,
\r
900 p->closing_error_code,
\r
901 p->closing_calling_back);
\r
904 if (change == PROXY_CHANGE_SENT) {
\r
905 /* some (or all) of what we wrote to the proxy was sent.
\r
906 * we don't do anything new, however, until we receive the
\r
907 * proxy's response. we might want to set a timer so we can
\r
908 * timeout the proxy negotiation after a while...
\r
913 if (change == PROXY_CHANGE_ACCEPTING) {
\r
914 /* we should _never_ see this, as we are using our socket to
\r
915 * connect to a proxy, not accepting inbound connections.
\r
916 * what should we do? close the socket with an appropriate
\r
919 return plug_accepting(p->plug, p->accepting_sock);
\r
922 if (change == PROXY_CHANGE_RECEIVE) {
\r
923 /* we have received data from the underlying socket, which
\r
924 * we'll need to parse, process, and respond to appropriately.
\r
927 if (p->state == 1) {
\r
929 /* initial response:
\r
930 * version number (1 byte) = 5
\r
931 * authentication method (1 byte)
\r
932 * authentication methods:
\r
933 * 0x00 = no authentication
\r
935 * 0x02 = username/password
\r
937 * 0xff = no acceptable methods
\r
941 if (bufchain_size(&p->pending_input_data) < 2)
\r
942 return 1; /* not got anything yet */
\r
944 /* get the response */
\r
945 bufchain_fetch(&p->pending_input_data, data, 2);
\r
947 if (data[0] != 5) {
\r
948 plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",
\r
949 PROXY_ERROR_GENERAL, 0);
\r
953 if (data[1] == 0x00) p->state = 2; /* no authentication needed */
\r
954 else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */
\r
955 else if (data[1] == 0x02) p->state = 5; /* username/password authentication */
\r
956 else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */
\r
958 plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",
\r
959 PROXY_ERROR_GENERAL, 0);
\r
962 bufchain_consume(&p->pending_input_data, 2);
\r
965 if (p->state == 7) {
\r
967 /* password authentication reply format:
\r
968 * version number (1 bytes) = 1
\r
969 * reply code (1 byte)
\r
975 if (bufchain_size(&p->pending_input_data) < 2)
\r
976 return 1; /* not got anything yet */
\r
978 /* get the response */
\r
979 bufchain_fetch(&p->pending_input_data, data, 2);
\r
981 if (data[0] != 1) {
\r
982 plug_closing(p->plug, "Proxy error: SOCKS password "
\r
983 "subnegotiation contained wrong version number",
\r
984 PROXY_ERROR_GENERAL, 0);
\r
988 if (data[1] != 0) {
\r
990 plug_closing(p->plug, "Proxy error: SOCKS proxy refused"
\r
991 " password authentication",
\r
992 PROXY_ERROR_GENERAL, 0);
\r
996 bufchain_consume(&p->pending_input_data, 2);
\r
997 p->state = 2; /* now proceed as authenticated */
\r
1000 if (p->state == 8) {
\r
1002 ret = proxy_socks5_handlechap(p);
\r
1003 if (ret) return ret;
\r
1006 if (p->state == 2) {
\r
1008 /* request format:
\r
1009 * version number (1 byte) = 5
\r
1010 * command code (1 byte)
\r
1013 * 3 = UDP ASSOCIATE
\r
1014 * reserved (1 byte) = 0x00
\r
1015 * address type (1 byte)
\r
1017 * 3 = domainname (first byte has length, no terminating null)
\r
1019 * dest. address (variable)
\r
1020 * dest. port (2 bytes) [network order]
\r
1023 char command[512];
\r
1027 type = sk_addrtype(p->remote_addr);
\r
1028 if (type == ADDRTYPE_IPV4) {
\r
1029 len = 10; /* 4 hdr + 4 addr + 2 trailer */
\r
1030 command[3] = 1; /* IPv4 */
\r
1031 sk_addrcopy(p->remote_addr, command+4);
\r
1032 } else if (type == ADDRTYPE_IPV6) {
\r
1033 len = 22; /* 4 hdr + 16 addr + 2 trailer */
\r
1034 command[3] = 4; /* IPv6 */
\r
1035 sk_addrcopy(p->remote_addr, command+4);
\r
1037 assert(type == ADDRTYPE_NAME);
\r
1039 sk_getaddr(p->remote_addr, command+5, 256);
\r
1040 command[4] = strlen(command+5);
\r
1041 len = 7 + command[4]; /* 4 hdr, 1 len, N addr, 2 trailer */
\r
1044 command[0] = 5; /* version 5 */
\r
1045 command[1] = 1; /* CONNECT command */
\r
1046 command[2] = 0x00;
\r
1049 command[len-2] = (char) (p->remote_port >> 8) & 0xff;
\r
1050 command[len-1] = (char) p->remote_port & 0xff;
\r
1052 sk_write(p->sub_socket, command, len);
\r
1058 if (p->state == 3) {
\r
1061 * version number (1 bytes) = 5
\r
1062 * reply code (1 byte)
\r
1064 * 1 = general SOCKS server failure
\r
1065 * 2 = connection not allowed by ruleset
\r
1066 * 3 = network unreachable
\r
1067 * 4 = host unreachable
\r
1068 * 5 = connection refused
\r
1070 * 7 = command not supported
\r
1071 * 8 = address type not supported
\r
1072 * reserved (1 byte) = x00
\r
1073 * address type (1 byte)
\r
1075 * 3 = domainname (first byte has length, no terminating null)
\r
1077 * server bound address (variable)
\r
1078 * server bound port (2 bytes) [network order]
\r
1083 /* First 5 bytes of packet are enough to tell its length. */
\r
1084 if (bufchain_size(&p->pending_input_data) < 5)
\r
1085 return 1; /* not got anything yet */
\r
1087 /* get the response */
\r
1088 bufchain_fetch(&p->pending_input_data, data, 5);
\r
1090 if (data[0] != 5) {
\r
1091 plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",
\r
1092 PROXY_ERROR_GENERAL, 0);
\r
1096 if (data[1] != 0) {
\r
1099 strcpy(buf, "Proxy error: ");
\r
1101 switch (data[1]) {
\r
1102 case 1: strcat(buf, "General SOCKS server failure"); break;
\r
1103 case 2: strcat(buf, "Connection not allowed by ruleset"); break;
\r
1104 case 3: strcat(buf, "Network unreachable"); break;
\r
1105 case 4: strcat(buf, "Host unreachable"); break;
\r
1106 case 5: strcat(buf, "Connection refused"); break;
\r
1107 case 6: strcat(buf, "TTL expired"); break;
\r
1108 case 7: strcat(buf, "Command not supported"); break;
\r
1109 case 8: strcat(buf, "Address type not supported"); break;
\r
1110 default: sprintf(buf+strlen(buf),
\r
1111 "Unrecognised SOCKS error code %d",
\r
1115 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
\r
1121 * Eat the rest of the reply packet.
\r
1123 len = 6; /* first 4 bytes, last 2 */
\r
1124 switch (data[3]) {
\r
1125 case 1: len += 4; break; /* IPv4 address */
\r
1126 case 4: len += 16; break;/* IPv6 address */
\r
1127 case 3: len += (unsigned char)data[4]; break; /* domain name */
\r
1129 plug_closing(p->plug, "Proxy error: SOCKS proxy returned "
\r
1130 "unrecognised address format",
\r
1131 PROXY_ERROR_GENERAL, 0);
\r
1134 if (bufchain_size(&p->pending_input_data) < len)
\r
1135 return 1; /* not got whole reply yet */
\r
1136 bufchain_consume(&p->pending_input_data, len);
\r
1139 proxy_activate(p);
\r
1143 if (p->state == 4) {
\r
1144 /* TODO: Handle GSSAPI authentication */
\r
1145 plug_closing(p->plug, "Proxy error: We don't support GSSAPI authentication",
\r
1146 PROXY_ERROR_GENERAL, 0);
\r
1150 if (p->state == 5) {
\r
1151 if (p->cfg.proxy_username[0] || p->cfg.proxy_password[0]) {
\r
1152 char userpwbuf[514];
\r
1154 ulen = strlen(p->cfg.proxy_username);
\r
1155 if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
\r
1156 plen = strlen(p->cfg.proxy_password);
\r
1157 if (plen > 255) plen = 255; if (plen < 1) plen = 1;
\r
1158 userpwbuf[0] = 1; /* version number of subnegotiation */
\r
1159 userpwbuf[1] = ulen;
\r
1160 memcpy(userpwbuf+2, p->cfg.proxy_username, ulen);
\r
1161 userpwbuf[ulen+2] = plen;
\r
1162 memcpy(userpwbuf+ulen+3, p->cfg.proxy_password, plen);
\r
1163 sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);
\r
1166 plug_closing(p->plug, "Proxy error: Server chose "
\r
1167 "username/password authentication but we "
\r
1168 "didn't offer it!",
\r
1169 PROXY_ERROR_GENERAL, 0);
\r
1173 if (p->state == 6) {
\r
1175 ret = proxy_socks5_selectchap(p);
\r
1176 if (ret) return ret;
\r
1181 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
\r
1182 PROXY_ERROR_UNEXPECTED, 0);
\r
1186 /* ----------------------------------------------------------------------
\r
1187 * `Telnet' proxy type.
\r
1189 * (This is for ad-hoc proxies where you connect to the proxy's
\r
1190 * telnet port and send a command such as `connect host port'. The
\r
1191 * command is configurable, since this proxy type is typically not
\r
1192 * standardised or at all well-defined.)
\r
1195 char *format_telnet_command(SockAddr addr, int port, const Config *cfg)
\r
1198 int retlen = 0, retsize = 0;
\r
1199 int so = 0, eo = 0;
\r
1200 #define ENSURE(n) do { \
\r
1201 if (retsize < retlen + n) { \
\r
1202 retsize = retlen + n + 512; \
\r
1203 ret = sresize(ret, retsize, char); \
\r
1207 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
\r
1208 * %%, %host, %port, %user, and %pass
\r
1211 while (cfg->proxy_telnet_command[eo] != 0) {
\r
1213 /* scan forward until we hit end-of-line,
\r
1214 * or an escape character (\ or %) */
\r
1215 while (cfg->proxy_telnet_command[eo] != 0 &&
\r
1216 cfg->proxy_telnet_command[eo] != '%' &&
\r
1217 cfg->proxy_telnet_command[eo] != '\\') eo++;
\r
1219 /* if we hit eol, break out of our escaping loop */
\r
1220 if (cfg->proxy_telnet_command[eo] == 0) break;
\r
1222 /* if there was any unescaped text before the escape
\r
1223 * character, send that now */
\r
1226 memcpy(ret + retlen, cfg->proxy_telnet_command + so, eo - so);
\r
1227 retlen += eo - so;
\r
1232 /* if the escape character was the last character of
\r
1233 * the line, we'll just stop and send it. */
\r
1234 if (cfg->proxy_telnet_command[eo] == 0) break;
\r
1236 if (cfg->proxy_telnet_command[so] == '\\') {
\r
1238 /* we recognize \\, \%, \r, \n, \t, \x??.
\r
1239 * anything else, we just send unescaped (including the \).
\r
1242 switch (cfg->proxy_telnet_command[eo]) {
\r
1246 ret[retlen++] = '\\';
\r
1252 ret[retlen++] = '%';
\r
1258 ret[retlen++] = '\r';
\r
1264 ret[retlen++] = '\n';
\r
1270 ret[retlen++] = '\t';
\r
1277 /* escaped hexadecimal value (ie. \xff) */
\r
1278 unsigned char v = 0;
\r
1283 if (cfg->proxy_telnet_command[eo] >= '0' &&
\r
1284 cfg->proxy_telnet_command[eo] <= '9')
\r
1285 v += cfg->proxy_telnet_command[eo] - '0';
\r
1286 else if (cfg->proxy_telnet_command[eo] >= 'a' &&
\r
1287 cfg->proxy_telnet_command[eo] <= 'f')
\r
1288 v += cfg->proxy_telnet_command[eo] - 'a' + 10;
\r
1289 else if (cfg->proxy_telnet_command[eo] >= 'A' &&
\r
1290 cfg->proxy_telnet_command[eo] <= 'F')
\r
1291 v += cfg->proxy_telnet_command[eo] - 'A' + 10;
\r
1293 /* non hex character, so we abort and just
\r
1294 * send the whole thing unescaped (including \x)
\r
1297 ret[retlen++] = '\\';
\r
1302 /* we only extract two hex characters */
\r
1305 ret[retlen++] = v;
\r
1318 memcpy(ret+retlen, cfg->proxy_telnet_command + so, 2);
\r
1325 /* % escape. we recognize %%, %host, %port, %user, %pass.
\r
1326 * %proxyhost, %proxyport. Anything else we just send
\r
1327 * unescaped (including the %).
\r
1330 if (cfg->proxy_telnet_command[eo] == '%') {
\r
1332 ret[retlen++] = '%';
\r
1335 else if (strnicmp(cfg->proxy_telnet_command + eo,
\r
1336 "host", 4) == 0) {
\r
1339 sk_getaddr(addr, dest, lenof(dest));
\r
1340 destlen = strlen(dest);
\r
1342 memcpy(ret+retlen, dest, destlen);
\r
1343 retlen += destlen;
\r
1346 else if (strnicmp(cfg->proxy_telnet_command + eo,
\r
1347 "port", 4) == 0) {
\r
1348 char portstr[8], portlen;
\r
1349 portlen = sprintf(portstr, "%i", port);
\r
1351 memcpy(ret + retlen, portstr, portlen);
\r
1352 retlen += portlen;
\r
1355 else if (strnicmp(cfg->proxy_telnet_command + eo,
\r
1356 "user", 4) == 0) {
\r
1357 int userlen = strlen(cfg->proxy_username);
\r
1359 memcpy(ret+retlen, cfg->proxy_username, userlen);
\r
1360 retlen += userlen;
\r
1363 else if (strnicmp(cfg->proxy_telnet_command + eo,
\r
1364 "pass", 4) == 0) {
\r
1365 int passlen = strlen(cfg->proxy_password);
\r
1367 memcpy(ret+retlen, cfg->proxy_password, passlen);
\r
1368 retlen += passlen;
\r
1371 else if (strnicmp(cfg->proxy_telnet_command + eo,
\r
1372 "proxyhost", 9) == 0) {
\r
1373 int phlen = strlen(cfg->proxy_host);
\r
1375 memcpy(ret+retlen, cfg->proxy_host, phlen);
\r
1379 else if (strnicmp(cfg->proxy_telnet_command + eo,
\r
1380 "proxyport", 9) == 0) {
\r
1383 sprintf(pport, "%d", cfg->proxy_port);
\r
1384 pplen = strlen(pport);
\r
1386 memcpy(ret+retlen, pport, pplen);
\r
1391 /* we don't escape this, so send the % now, and
\r
1392 * don't advance eo, so that we'll consider the
\r
1393 * text immediately following the % as unescaped.
\r
1396 ret[retlen++] = '%';
\r
1400 /* resume scanning for additional escapes after this one. */
\r
1404 /* if there is any unescaped text at the end of the line, send it */
\r
1407 memcpy(ret + retlen, cfg->proxy_telnet_command + so, eo - so);
\r
1408 retlen += eo - so;
\r
1412 ret[retlen] = '\0';
\r
1418 int proxy_telnet_negotiate (Proxy_Socket p, int change)
\r
1420 if (p->state == PROXY_CHANGE_NEW) {
\r
1421 char *formatted_cmd;
\r
1423 formatted_cmd = format_telnet_command(p->remote_addr, p->remote_port,
\r
1426 sk_write(p->sub_socket, formatted_cmd, strlen(formatted_cmd));
\r
1427 sfree(formatted_cmd);
\r
1433 if (change == PROXY_CHANGE_CLOSING) {
\r
1434 /* if our proxy negotiation process involves closing and opening
\r
1435 * new sockets, then we would want to intercept this closing
\r
1436 * callback when we were expecting it. if we aren't anticipating
\r
1437 * a socket close, then some error must have occurred. we'll
\r
1438 * just pass those errors up to the backend.
\r
1440 return plug_closing(p->plug, p->closing_error_msg,
\r
1441 p->closing_error_code,
\r
1442 p->closing_calling_back);
\r
1445 if (change == PROXY_CHANGE_SENT) {
\r
1446 /* some (or all) of what we wrote to the proxy was sent.
\r
1447 * we don't do anything new, however, until we receive the
\r
1448 * proxy's response. we might want to set a timer so we can
\r
1449 * timeout the proxy negotiation after a while...
\r
1454 if (change == PROXY_CHANGE_ACCEPTING) {
\r
1455 /* we should _never_ see this, as we are using our socket to
\r
1456 * connect to a proxy, not accepting inbound connections.
\r
1457 * what should we do? close the socket with an appropriate
\r
1460 return plug_accepting(p->plug, p->accepting_sock);
\r
1463 if (change == PROXY_CHANGE_RECEIVE) {
\r
1464 /* we have received data from the underlying socket, which
\r
1465 * we'll need to parse, process, and respond to appropriately.
\r
1469 proxy_activate(p);
\r
1470 /* proxy activate will have dealt with
\r
1471 * whatever is left of the buffer */
\r
1475 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
\r
1476 PROXY_ERROR_UNEXPECTED, 0);
\r