Add timestamps to the 'SSH raw data' logging mode.
[u/mdw/putty] / proxy.c
CommitLineData
8eebd221 1/*
2 * Network proxy abstraction in PuTTY
3 *
4 * A proxy layer, if necessary, wedges itself between the network
5 * code and the higher level backend.
6 */
7
7983f47e 8#include <assert.h>
de9aaffb 9#include <ctype.h>
10#include <string.h>
7983f47e 11
8eebd221 12#define DEFINE_PLUG_METHOD_MACROS
13#include "putty.h"
14#include "network.h"
15#include "proxy.h"
16
4a693cfc 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))
b7a189f3 21
8eebd221 22/*
23 * Call this when proxy negotiation is complete, so that this
24 * socket can begin working normally.
25 */
26void proxy_activate (Proxy_Socket p)
27{
28 void *data;
29 int len;
2d129d8e 30 long output_before, output_after;
31
8eebd221 32 p->state = PROXY_STATE_ACTIVE;
33
2d129d8e 34 /* we want to ignore new receive events until we have sent
35 * all of our buffered receive data.
36 */
8eebd221 37 sk_set_frozen(p->sub_socket, 1);
38
2d129d8e 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. */
43 output_after = 0;
44
0e8f4cda 45 /* send buffered OOB writes */
8eebd221 46 while (bufchain_size(&p->pending_oob_output_data) > 0) {
47 bufchain_prefix(&p->pending_oob_output_data, &data, &len);
2d129d8e 48 output_after += sk_write_oob(p->sub_socket, data, len);
8eebd221 49 bufchain_consume(&p->pending_oob_output_data, len);
50 }
8eebd221 51
0e8f4cda 52 /* send buffered normal writes */
8eebd221 53 while (bufchain_size(&p->pending_output_data) > 0) {
54 bufchain_prefix(&p->pending_output_data, &data, &len);
2d129d8e 55 output_after += sk_write(p->sub_socket, data, len);
8eebd221 56 bufchain_consume(&p->pending_output_data, len);
57 }
2d129d8e 58
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);
8eebd221 62
0e8f4cda 63 /* if we were asked to flush the output during
64 * the proxy negotiation process, do so now.
65 */
8eebd221 66 if (p->pending_flush) sk_flush(p->sub_socket);
8eebd221 67
bc06669b 68 /* if we have a pending EOF to send, send it */
69 if (p->pending_eof) sk_write_eof(p->sub_socket);
70
2d129d8e 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.
74 */
75 if (!p->freeze)
76 sk_set_frozen((Socket)p, 0);
8eebd221 77}
78
79/* basic proxy socket functions */
80
81static Plug sk_proxy_plug (Socket s, Plug p)
82{
83 Proxy_Socket ps = (Proxy_Socket) s;
84 Plug ret = ps->plug;
85 if (p)
86 ps->plug = p;
87 return ret;
88}
89
90static void sk_proxy_close (Socket s)
91{
92 Proxy_Socket ps = (Proxy_Socket) s;
93
8eebd221 94 sk_close(ps->sub_socket);
f85e6f6e 95 sk_addr_free(ps->remote_addr);
8eebd221 96 sfree(ps);
97}
98
e0e7dff8 99static int sk_proxy_write (Socket s, const char *data, int len)
8eebd221 100{
101 Proxy_Socket ps = (Proxy_Socket) s;
102
8eebd221 103 if (ps->state != PROXY_STATE_ACTIVE) {
104 bufchain_add(&ps->pending_output_data, data, len);
105 return bufchain_size(&ps->pending_output_data);
106 }
107 return sk_write(ps->sub_socket, data, len);
108}
109
e0e7dff8 110static int sk_proxy_write_oob (Socket s, const char *data, int len)
8eebd221 111{
112 Proxy_Socket ps = (Proxy_Socket) s;
113
8eebd221 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);
118 return len;
119 }
120 return sk_write_oob(ps->sub_socket, data, len);
121}
122
bc06669b 123static void sk_proxy_write_eof (Socket s)
124{
125 Proxy_Socket ps = (Proxy_Socket) s;
126
127 if (ps->state != PROXY_STATE_ACTIVE) {
128 ps->pending_eof = 1;
129 return;
130 }
131 sk_write_eof(ps->sub_socket);
132}
133
8eebd221 134static void sk_proxy_flush (Socket s)
135{
136 Proxy_Socket ps = (Proxy_Socket) s;
137
8eebd221 138 if (ps->state != PROXY_STATE_ACTIVE) {
139 ps->pending_flush = 1;
140 return;
141 }
142 sk_flush(ps->sub_socket);
143}
144
145static void sk_proxy_set_private_ptr (Socket s, void *ptr)
146{
147 Proxy_Socket ps = (Proxy_Socket) s;
148 sk_set_private_ptr(ps->sub_socket, ptr);
149}
150
151static void * sk_proxy_get_private_ptr (Socket s)
152{
153 Proxy_Socket ps = (Proxy_Socket) s;
154 return sk_get_private_ptr(ps->sub_socket);
155}
156
157static void sk_proxy_set_frozen (Socket s, int is_frozen)
158{
159 Proxy_Socket ps = (Proxy_Socket) s;
160
8eebd221 161 if (ps->state != PROXY_STATE_ACTIVE) {
162 ps->freeze = is_frozen;
163 return;
164 }
2d129d8e 165
166 /* handle any remaining buffered recv data first */
167 if (bufchain_size(&ps->pending_input_data) > 0) {
168 ps->freeze = is_frozen;
169
170 /* loop while we still have buffered data, and while we are
171 * unfrozen. the plug_receive call in the loop could result
172 * in a call back into this function refreezing the socket,
173 * so we have to check each time.
174 */
175 while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {
68a49acb 176 void *data;
d9d006d5 177 char databuf[512];
2d129d8e 178 int len;
179 bufchain_prefix(&ps->pending_input_data, &data, &len);
d9d006d5 180 if (len > lenof(databuf))
181 len = lenof(databuf);
182 memcpy(databuf, data, len);
2d129d8e 183 bufchain_consume(&ps->pending_input_data, len);
d9d006d5 184 plug_receive(ps->plug, 0, databuf, len);
2d129d8e 185 }
186
187 /* if we're still frozen, we'll have to wait for another
188 * call from the backend to finish unbuffering the data.
189 */
190 if (ps->freeze) return;
191 }
192
8eebd221 193 sk_set_frozen(ps->sub_socket, is_frozen);
194}
195
cbe2d68f 196static const char * sk_proxy_socket_error (Socket s)
8eebd221 197{
198 Proxy_Socket ps = (Proxy_Socket) s;
199 if (ps->error != NULL || ps->sub_socket == NULL) {
200 return ps->error;
201 }
202 return sk_socket_error(ps->sub_socket);
203}
204
205/* basic proxy plug functions */
206
7555d6a5 207static void plug_proxy_log(Plug plug, int type, SockAddr addr, int port,
208 const char *error_msg, int error_code)
209{
210 Proxy_Plug pp = (Proxy_Plug) plug;
211 Proxy_Socket ps = pp->proxy_socket;
212
213 plug_log(ps->plug, type, addr, port, error_msg, error_code);
214}
215
cbe2d68f 216static int plug_proxy_closing (Plug p, const char *error_msg,
8eebd221 217 int error_code, int calling_back)
218{
219 Proxy_Plug pp = (Proxy_Plug) p;
220 Proxy_Socket ps = pp->proxy_socket;
221
8eebd221 222 if (ps->state != PROXY_STATE_ACTIVE) {
223 ps->closing_error_msg = error_msg;
224 ps->closing_error_code = error_code;
225 ps->closing_calling_back = calling_back;
226 return ps->negotiate(ps, PROXY_CHANGE_CLOSING);
227 }
228 return plug_closing(ps->plug, error_msg,
229 error_code, calling_back);
230}
231
232static int plug_proxy_receive (Plug p, int urgent, char *data, int len)
233{
234 Proxy_Plug pp = (Proxy_Plug) p;
235 Proxy_Socket ps = pp->proxy_socket;
236
8eebd221 237 if (ps->state != PROXY_STATE_ACTIVE) {
238 /* we will lose the urgentness of this data, but since most,
239 * if not all, of this data will be consumed by the negotiation
240 * process, hopefully it won't affect the protocol above us
241 */
242 bufchain_add(&ps->pending_input_data, data, len);
243 ps->receive_urgent = urgent;
244 ps->receive_data = data;
245 ps->receive_len = len;
246 return ps->negotiate(ps, PROXY_CHANGE_RECEIVE);
247 }
248 return plug_receive(ps->plug, urgent, data, len);
249}
250
251static void plug_proxy_sent (Plug p, int bufsize)
252{
253 Proxy_Plug pp = (Proxy_Plug) p;
254 Proxy_Socket ps = pp->proxy_socket;
255
8eebd221 256 if (ps->state != PROXY_STATE_ACTIVE) {
257 ps->sent_bufsize = bufsize;
258 ps->negotiate(ps, PROXY_CHANGE_SENT);
259 return;
260 }
261 plug_sent(ps->plug, bufsize);
262}
263
f7f9fb5c 264static int plug_proxy_accepting (Plug p, OSSocket sock)
8eebd221 265{
266 Proxy_Plug pp = (Proxy_Plug) p;
267 Proxy_Socket ps = pp->proxy_socket;
268
8eebd221 269 if (ps->state != PROXY_STATE_ACTIVE) {
270 ps->accepting_sock = sock;
271 return ps->negotiate(ps, PROXY_CHANGE_ACCEPTING);
272 }
273 return plug_accepting(ps->plug, sock);
274}
275
b7a189f3 276/*
277 * This function can accept a NULL pointer as `addr', in which case
278 * it will only check the host name.
279 */
e8fa8f62 280static int proxy_for_destination (SockAddr addr, char *hostname, int port,
4a693cfc 281 Conf *conf)
8eebd221 282{
283 int s = 0, e = 0;
284 char hostip[64];
285 int hostip_len, hostname_len;
e8fa8f62 286 const char *exclude_list;
8eebd221 287
b804e1e5 288 /*
289 * Check the host name and IP against the hard-coded
290 * representations of `localhost'.
291 */
4a693cfc 292 if (!conf_get_int(conf, CONF_even_proxy_localhost) &&
b7a189f3 293 (sk_hostname_is_local(hostname) ||
294 (addr && sk_address_is_local(addr))))
b804e1e5 295 return 0; /* do not proxy */
296
8eebd221 297 /* we want a string representation of the IP address for comparisons */
b7a189f3 298 if (addr) {
299 sk_getaddr(addr, hostip, 64);
300 hostip_len = strlen(hostip);
7ffdbc1a 301 } else
302 hostip_len = 0; /* placate gcc; shouldn't be required */
8eebd221 303
8eebd221 304 hostname_len = strlen(hostname);
305
4a693cfc 306 exclude_list = conf_get_str(conf, CONF_proxy_exclude_list);
8eebd221 307
308 /* now parse the exclude list, and see if either our IP
309 * or hostname matches anything in it.
310 */
311
312 while (exclude_list[s]) {
313 while (exclude_list[s] &&
e93ed432 314 (isspace((unsigned char)exclude_list[s]) ||
8eebd221 315 exclude_list[s] == ',')) s++;
316
317 if (!exclude_list[s]) break;
318
319 e = s;
320
321 while (exclude_list[e] &&
e93ed432 322 (isalnum((unsigned char)exclude_list[e]) ||
8eebd221 323 exclude_list[e] == '-' ||
324 exclude_list[e] == '.' ||
325 exclude_list[e] == '*')) e++;
326
327 if (exclude_list[s] == '*') {
328 /* wildcard at beginning of entry */
329
b7a189f3 330 if ((addr && strnicmp(hostip + hostip_len - (e - s - 1),
331 exclude_list + s + 1, e - s - 1) == 0) ||
8eebd221 332 strnicmp(hostname + hostname_len - (e - s - 1),
333 exclude_list + s + 1, e - s - 1) == 0)
334 return 0; /* IP/hostname range excluded. do not use proxy. */
335
336 } else if (exclude_list[e-1] == '*') {
337 /* wildcard at end of entry */
338
b7a189f3 339 if ((addr && strnicmp(hostip, exclude_list + s, e - s - 1) == 0) ||
8eebd221 340 strnicmp(hostname, exclude_list + s, e - s - 1) == 0)
341 return 0; /* IP/hostname range excluded. do not use proxy. */
342
343 } else {
344 /* no wildcard at either end, so let's try an absolute
345 * match (ie. a specific IP)
346 */
347
d6f842e3 348 if (addr && strnicmp(hostip, exclude_list + s, e - s) == 0)
8eebd221 349 return 0; /* IP/hostname excluded. do not use proxy. */
d6f842e3 350 if (strnicmp(hostname, exclude_list + s, e - s) == 0)
8eebd221 351 return 0; /* IP/hostname excluded. do not use proxy. */
352 }
353
354 s = e;
1b08e5d7 355
356 /* Make sure we really have reached the next comma or end-of-string */
357 while (exclude_list[s] &&
e93ed432 358 !isspace((unsigned char)exclude_list[s]) &&
1b08e5d7 359 exclude_list[s] != ',') s++;
8eebd221 360 }
361
362 /* no matches in the exclude list, so use the proxy */
363 return 1;
364}
365
e8fa8f62 366SockAddr name_lookup(char *host, int port, char **canonicalname,
4a693cfc 367 Conf *conf, int addressfamily)
b7a189f3 368{
4a693cfc 369 if (conf_get_int(conf, CONF_proxy_type) != PROXY_NONE &&
370 do_proxy_dns(conf) &&
371 proxy_for_destination(NULL, host, port, conf)) {
b7a189f3 372 *canonicalname = dupstr(host);
373 return sk_nonamelookup(host);
374 }
375
05581745 376 return sk_namelookup(host, canonicalname, addressfamily);
b7a189f3 377}
378
8eebd221 379Socket new_connection(SockAddr addr, char *hostname,
380 int port, int privport,
79bf227b 381 int oobinline, int nodelay, int keepalive,
4a693cfc 382 Plug plug, Conf *conf)
8eebd221 383{
7b6c9b99 384 static const struct socket_function_table socket_fn_table = {
8eebd221 385 sk_proxy_plug,
386 sk_proxy_close,
387 sk_proxy_write,
388 sk_proxy_write_oob,
bc06669b 389 sk_proxy_write_eof,
8eebd221 390 sk_proxy_flush,
391 sk_proxy_set_private_ptr,
392 sk_proxy_get_private_ptr,
393 sk_proxy_set_frozen,
394 sk_proxy_socket_error
395 };
396
7b6c9b99 397 static const struct plug_function_table plug_fn_table = {
7555d6a5 398 plug_proxy_log,
8eebd221 399 plug_proxy_closing,
400 plug_proxy_receive,
401 plug_proxy_sent,
402 plug_proxy_accepting
403 };
404
4a693cfc 405 if (conf_get_int(conf, CONF_proxy_type) != PROXY_NONE &&
406 proxy_for_destination(addr, hostname, port, conf))
8eebd221 407 {
408 Proxy_Socket ret;
409 Proxy_Plug pplug;
410 SockAddr proxy_addr;
cbe2d68f 411 char *proxy_canonical_name;
0f0a2507 412 Socket sret;
4a693cfc 413 int type;
0f0a2507 414
eba898cf 415 if ((sret = platform_new_connection(addr, hostname, port, privport,
79bf227b 416 oobinline, nodelay, keepalive,
4a693cfc 417 plug, conf)) !=
eba898cf 418 NULL)
0f0a2507 419 return sret;
8eebd221 420
3d88e64d 421 ret = snew(struct Socket_proxy_tag);
8eebd221 422 ret->fn = &socket_fn_table;
4a693cfc 423 ret->conf = conf_copy(conf);
8eebd221 424 ret->plug = plug;
f85e6f6e 425 ret->remote_addr = addr; /* will need to be freed on close */
8eebd221 426 ret->remote_port = port;
427
65c78e88 428 ret->error = NULL;
429 ret->pending_flush = 0;
bc06669b 430 ret->pending_eof = 0;
65c78e88 431 ret->freeze = 0;
432
8eebd221 433 bufchain_init(&ret->pending_input_data);
434 bufchain_init(&ret->pending_output_data);
435 bufchain_init(&ret->pending_oob_output_data);
436
8eebd221 437 ret->sub_socket = NULL;
438 ret->state = PROXY_STATE_NEW;
2d129d8e 439 ret->negotiate = NULL;
4a693cfc 440
441 type = conf_get_int(conf, CONF_proxy_type);
442 if (type == PROXY_HTTP) {
8eebd221 443 ret->negotiate = proxy_http_negotiate;
4a693cfc 444 } else if (type == PROXY_SOCKS4) {
10068a0b 445 ret->negotiate = proxy_socks4_negotiate;
4a693cfc 446 } else if (type == PROXY_SOCKS5) {
10068a0b 447 ret->negotiate = proxy_socks5_negotiate;
4a693cfc 448 } else if (type == PROXY_TELNET) {
8eebd221 449 ret->negotiate = proxy_telnet_negotiate;
450 } else {
7983f47e 451 ret->error = "Proxy error: Unknown proxy method";
8eebd221 452 return (Socket) ret;
453 }
454
455 /* create the proxy plug to map calls from the actual
456 * socket into our proxy socket layer */
3d88e64d 457 pplug = snew(struct Plug_proxy_tag);
8eebd221 458 pplug->fn = &plug_fn_table;
459 pplug->proxy_socket = ret;
460
461 /* look-up proxy */
4a693cfc 462 proxy_addr = sk_namelookup(conf_get_str(conf, CONF_proxy_host),
463 &proxy_canonical_name,
464 conf_get_int(conf, CONF_addressfamily));
cbe2d68f 465 if (sk_addr_error(proxy_addr) != NULL) {
42389a9e 466 ret->error = "Proxy error: Unable to resolve proxy host name";
467 return (Socket)ret;
468 }
8eebd221 469 sfree(proxy_canonical_name);
470
471 /* create the actual socket we will be using,
472 * connected to our proxy server and port.
473 */
4a693cfc 474 ret->sub_socket = sk_new(proxy_addr,
475 conf_get_int(conf, CONF_proxy_port),
8eebd221 476 privport, oobinline,
79bf227b 477 nodelay, keepalive, (Plug) pplug);
8eebd221 478 if (sk_socket_error(ret->sub_socket) != NULL)
479 return (Socket) ret;
480
8eebd221 481 /* start the proxy negotiation process... */
482 sk_set_frozen(ret->sub_socket, 0);
483 ret->negotiate(ret, PROXY_CHANGE_NEW);
484
485 return (Socket) ret;
486 }
487
488 /* no proxy, so just return the direct socket */
79bf227b 489 return sk_new(addr, port, privport, oobinline, nodelay, keepalive, plug);
8eebd221 490}
491
e8fa8f62 492Socket new_listener(char *srcaddr, int port, Plug plug, int local_host_only,
4a693cfc 493 Conf *conf, int addressfamily)
8eebd221 494{
495 /* TODO: SOCKS (and potentially others) support inbound
496 * TODO: connections via the proxy. support them.
497 */
498
05581745 499 return sk_newlistener(srcaddr, port, plug, local_host_only, addressfamily);
8eebd221 500}
501
502/* ----------------------------------------------------------------------
503 * HTTP CONNECT proxy type.
504 */
505
506static int get_line_end (char * data, int len)
507{
508 int off = 0;
509
510 while (off < len)
511 {
512 if (data[off] == '\n') {
513 /* we have a newline */
514 off++;
515
516 /* is that the only thing on this line? */
517 if (off <= 2) return off;
518
519 /* if not, then there is the possibility that this header
520 * continues onto the next line, if it starts with a space
521 * or a tab.
522 */
523
524 if (off + 1 < len &&
525 data[off+1] != ' ' &&
526 data[off+1] != '\t') return off;
527
528 /* the line does continue, so we have to keep going
529 * until we see an the header's "real" end of line.
530 */
531 off++;
532 }
533
534 off++;
535 }
536
537 return -1;
538}
539
540int proxy_http_negotiate (Proxy_Socket p, int change)
541{
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
546 * request
547 */
b7a189f3 548 char *buf, dest[512];
4a693cfc 549 char *username, *password;
8eebd221 550
b7a189f3 551 sk_getaddr(p->remote_addr, dest, lenof(dest));
8eebd221 552
57356d63 553 buf = dupprintf("CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
554 dest, p->remote_port, dest, p->remote_port);
8eebd221 555 sk_write(p->sub_socket, buf, strlen(buf));
57356d63 556 sfree(buf);
8eebd221 557
4a693cfc 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]) {
561 char *buf, *buf2;
1549e076 562 int i, j, len;
4a693cfc 563 buf = dupprintf("%s:%s", username, password);
1549e076 564 len = strlen(buf);
4a693cfc 565 buf2 = snewn(len * 4 / 3 + 100, char);
6c434e1f 566 sprintf(buf2, "Proxy-Authorization: Basic ");
1549e076 567 for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)
616a115c 568 base64_encode_atom((unsigned char *)(buf+i),
569 (len-i > 3 ? 3 : len-i), buf2+j);
1549e076 570 strcpy(buf2+j, "\r\n");
571 sk_write(p->sub_socket, buf2, strlen(buf2));
4a693cfc 572 sfree(buf);
573 sfree(buf2);
1549e076 574 }
575
9ddf18a4 576 sk_write(p->sub_socket, "\r\n", 2);
1549e076 577
8eebd221 578 p->state = 1;
8eebd221 579 return 0;
580 }
581
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.
588 */
589 return plug_closing(p->plug, p->closing_error_msg,
590 p->closing_error_code,
591 p->closing_calling_back);
592 }
593
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...
599 */
600 return 0;
601 }
602
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
607 * error message?
608 */
609 return plug_accepting(p->plug, p->accepting_sock);
610 }
611
612 if (change == PROXY_CHANGE_RECEIVE) {
613 /* we have received data from the underlying socket, which
614 * we'll need to parse, process, and respond to appropriately.
615 */
616
7983f47e 617 char *data, *datap;
8eebd221 618 int len;
619 int eol;
620
621 if (p->state == 1) {
622
623 int min_ver, maj_ver, status;
624
625 /* get the status line */
7983f47e 626 len = bufchain_size(&p->pending_input_data);
627 assert(len > 0); /* or we wouldn't be here */
8d8af571 628 data = snewn(len+1, char);
7983f47e 629 bufchain_fetch(&p->pending_input_data, data, len);
8d8af571 630 /*
631 * We must NUL-terminate this data, because Windows
632 * sscanf appears to require a NUL at the end of the
633 * string because it strlens it _first_. Sigh.
634 */
635 data[len] = '\0';
7983f47e 636
8eebd221 637 eol = get_line_end(data, len);
7983f47e 638 if (eol < 0) {
639 sfree(data);
640 return 1;
641 }
8eebd221 642
7983f47e 643 status = -1;
644 /* We can't rely on whether the %n incremented the sscanf return */
645 if (sscanf((char *)data, "HTTP/%i.%i %n",
646 &maj_ver, &min_ver, &status) < 2 || status == -1) {
647 plug_closing(p->plug, "Proxy error: HTTP response was absent",
648 PROXY_ERROR_GENERAL, 0);
649 sfree(data);
650 return 1;
651 }
8eebd221 652
653 /* remove the status line from the input buffer. */
654 bufchain_consume(&p->pending_input_data, eol);
7983f47e 655 if (data[status] != '2') {
8eebd221 656 /* error */
57356d63 657 char *buf;
7983f47e 658 data[eol] = '\0';
659 while (eol > status &&
660 (data[eol-1] == '\r' || data[eol-1] == '\n'))
661 data[--eol] = '\0';
57356d63 662 buf = dupprintf("Proxy error: %s", data+status);
7983f47e 663 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
57356d63 664 sfree(buf);
7983f47e 665 sfree(data);
8eebd221 666 return 1;
667 }
668
7983f47e 669 sfree(data);
670
8eebd221 671 p->state = 2;
672 }
673
674 if (p->state == 2) {
675
676 /* get headers. we're done when we get a
677 * header of length 2, (ie. just "\r\n")
678 */
679
7983f47e 680 len = bufchain_size(&p->pending_input_data);
681 assert(len > 0); /* or we wouldn't be here */
3d88e64d 682 data = snewn(len, char);
7983f47e 683 datap = data;
684 bufchain_fetch(&p->pending_input_data, data, len);
685
686 eol = get_line_end(datap, len);
687 if (eol < 0) {
688 sfree(data);
689 return 1;
690 }
8eebd221 691 while (eol > 2)
692 {
8eebd221 693 bufchain_consume(&p->pending_input_data, eol);
7983f47e 694 datap += eol;
9ddf18a4 695 len -= eol;
7983f47e 696 eol = get_line_end(datap, len);
8eebd221 697 }
698
699 if (eol == 2) {
700 /* we're done */
701 bufchain_consume(&p->pending_input_data, 2);
702 proxy_activate(p);
703 /* proxy activate will have dealt with
704 * whatever is left of the buffer */
7983f47e 705 sfree(data);
8eebd221 706 return 1;
707 }
708
7983f47e 709 sfree(data);
8eebd221 710 return 1;
711 }
712 }
713
7983f47e 714 plug_closing(p->plug, "Proxy error: unexpected proxy error",
8eebd221 715 PROXY_ERROR_UNEXPECTED, 0);
6971bbe7 716 return 1;
8eebd221 717}
718
719/* ----------------------------------------------------------------------
6971bbe7 720 * SOCKS proxy type.
8eebd221 721 */
722
6971bbe7 723/* SOCKS version 4 */
724int proxy_socks4_negotiate (Proxy_Socket p, int change)
8eebd221 725{
6971bbe7 726 if (p->state == PROXY_CHANGE_NEW) {
727
728 /* request format:
729 * version number (1 byte) = 4
730 * command code (1 byte)
731 * 1 = CONNECT
732 * 2 = BIND
733 * dest. port (2 bytes) [network order]
734 * dest. address (4 bytes)
735 * user ID (variable length, null terminated string)
736 */
737
b7a189f3 738 int length, type, namelen;
739 char *command, addr[4], hostname[512];
4a693cfc 740 char *username;
6971bbe7 741
b7a189f3 742 type = sk_addrtype(p->remote_addr);
743 if (type == ADDRTYPE_IPV6) {
7983f47e 744 plug_closing(p->plug, "Proxy error: SOCKS version 4 does"
745 " not support IPv6", PROXY_ERROR_GENERAL, 0);
6971bbe7 746 return 1;
b7a189f3 747 } else if (type == ADDRTYPE_IPV4) {
748 namelen = 0;
749 sk_addrcopy(p->remote_addr, addr);
750 } else { /* type == ADDRTYPE_NAME */
5e391f65 751 assert(type == ADDRTYPE_NAME);
b7a189f3 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;
755 addr[3] = 1;
6971bbe7 756 }
757
4a693cfc 758 username = conf_get_str(p->conf, CONF_proxy_username);
759 length = strlen(username) + namelen + 9;
3d88e64d 760 command = snewn(length, char);
4a693cfc 761 strcpy(command + 8, username);
6971bbe7 762
763 command[0] = 4; /* version 4 */
764 command[1] = 1; /* CONNECT command */
765
766 /* port */
767 command[2] = (char) (p->remote_port >> 8) & 0xff;
768 command[3] = (char) p->remote_port & 0xff;
769
770 /* address */
b7a189f3 771 memcpy(command + 4, addr, 4);
772
773 /* hostname */
4a693cfc 774 memcpy(command + 8 + strlen(username) + 1,
b7a189f3 775 hostname, namelen);
6971bbe7 776
777 sk_write(p->sub_socket, command, length);
4a693cfc 778 sfree(username);
de9aaffb 779 sfree(command);
6971bbe7 780
781 p->state = 1;
782 return 0;
783 }
784
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.
791 */
792 return plug_closing(p->plug, p->closing_error_msg,
793 p->closing_error_code,
794 p->closing_calling_back);
795 }
796
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...
802 */
803 return 0;
804 }
805
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
810 * error message?
811 */
812 return plug_accepting(p->plug, p->accepting_sock);
813 }
814
815 if (change == PROXY_CHANGE_RECEIVE) {
816 /* we have received data from the underlying socket, which
817 * we'll need to parse, process, and respond to appropriately.
818 */
819
820 if (p->state == 1) {
821 /* response format:
822 * version number (1 byte) = 4
823 * reply code (1 byte)
824 * 90 = request granted
825 * 91 = request rejected or failed
826 * 92 = request rejected due to lack of IDENTD on client
827 * 93 = request rejected due to difference in user ID
828 * (what we sent vs. what IDENTD said)
829 * dest. port (2 bytes)
830 * dest. address (4 bytes)
831 */
832
7983f47e 833 char data[8];
6971bbe7 834
7983f47e 835 if (bufchain_size(&p->pending_input_data) < 8)
836 return 1; /* not got anything yet */
837
6971bbe7 838 /* get the response */
7983f47e 839 bufchain_fetch(&p->pending_input_data, data, 8);
6971bbe7 840
841 if (data[0] != 0) {
7983f47e 842 plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "
6971bbe7 843 "unexpected reply code version",
844 PROXY_ERROR_GENERAL, 0);
845 return 1;
846 }
847
848 if (data[1] != 90) {
849
850 switch (data[1]) {
851 case 92:
7983f47e 852 plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",
6971bbe7 853 PROXY_ERROR_GENERAL, 0);
854 break;
855 case 93:
7983f47e 856 plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",
6971bbe7 857 PROXY_ERROR_GENERAL, 0);
858 break;
859 case 91:
860 default:
7983f47e 861 plug_closing(p->plug, "Proxy error: Error while communicating with proxy",
6971bbe7 862 PROXY_ERROR_GENERAL, 0);
863 break;
864 }
865
866 return 1;
867 }
7983f47e 868 bufchain_consume(&p->pending_input_data, 8);
6971bbe7 869
870 /* we're done */
871 proxy_activate(p);
872 /* proxy activate will have dealt with
873 * whatever is left of the buffer */
874 return 1;
875 }
876 }
877
7983f47e 878 plug_closing(p->plug, "Proxy error: unexpected proxy error",
6971bbe7 879 PROXY_ERROR_UNEXPECTED, 0);
880 return 1;
881}
882
883/* SOCKS version 5 */
884int proxy_socks5_negotiate (Proxy_Socket p, int change)
885{
886 if (p->state == PROXY_CHANGE_NEW) {
887
888 /* initial command:
889 * version number (1 byte) = 5
890 * number of available authentication methods (1 byte)
891 * available authentication methods (1 byte * previous value)
892 * authentication methods:
893 * 0x00 = no authentication
894 * 0x01 = GSSAPI
895 * 0x02 = username/password
896 * 0x03 = CHAP
897 */
898
f33ba69e 899 char command[5];
4a693cfc 900 char *username, *password;
aab91a3e 901 int len;
6971bbe7 902
903 command[0] = 5; /* version 5 */
4a693cfc 904 username = conf_get_str(p->conf, CONF_proxy_username);
905 password = conf_get_str(p->conf, CONF_proxy_password);
906 if (username[0] || password[0]) {
aab91a3e 907 command[2] = 0x00; /* no authentication */
f33ba69e 908 len = 3;
909 proxy_socks5_offerencryptedauth (command, &len);
910 command[len++] = 0x02; /* username/password */
911 command[1] = len - 2; /* Number of methods supported */
aab91a3e 912 } else {
913 command[1] = 1; /* one methods supported: */
914 command[2] = 0x00; /* no authentication */
915 len = 3;
916 }
6971bbe7 917
aab91a3e 918 sk_write(p->sub_socket, command, len);
6971bbe7 919
920 p->state = 1;
921 return 0;
922 }
923
924 if (change == PROXY_CHANGE_CLOSING) {
925 /* if our proxy negotiation process involves closing and opening
926 * new sockets, then we would want to intercept this closing
927 * callback when we were expecting it. if we aren't anticipating
928 * a socket close, then some error must have occurred. we'll
929 * just pass those errors up to the backend.
930 */
931 return plug_closing(p->plug, p->closing_error_msg,
932 p->closing_error_code,
933 p->closing_calling_back);
934 }
935
936 if (change == PROXY_CHANGE_SENT) {
937 /* some (or all) of what we wrote to the proxy was sent.
938 * we don't do anything new, however, until we receive the
939 * proxy's response. we might want to set a timer so we can
940 * timeout the proxy negotiation after a while...
941 */
942 return 0;
943 }
944
945 if (change == PROXY_CHANGE_ACCEPTING) {
946 /* we should _never_ see this, as we are using our socket to
947 * connect to a proxy, not accepting inbound connections.
948 * what should we do? close the socket with an appropriate
949 * error message?
950 */
951 return plug_accepting(p->plug, p->accepting_sock);
952 }
953
954 if (change == PROXY_CHANGE_RECEIVE) {
955 /* we have received data from the underlying socket, which
956 * we'll need to parse, process, and respond to appropriately.
957 */
958
6971bbe7 959 if (p->state == 1) {
960
961 /* initial response:
962 * version number (1 byte) = 5
963 * authentication method (1 byte)
964 * authentication methods:
965 * 0x00 = no authentication
966 * 0x01 = GSSAPI
f33ba69e 967 * 0x02 = username/password
6971bbe7 968 * 0x03 = CHAP
969 * 0xff = no acceptable methods
970 */
7983f47e 971 char data[2];
972
973 if (bufchain_size(&p->pending_input_data) < 2)
974 return 1; /* not got anything yet */
6971bbe7 975
976 /* get the response */
7983f47e 977 bufchain_fetch(&p->pending_input_data, data, 2);
6971bbe7 978
979 if (data[0] != 5) {
7983f47e 980 plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",
6971bbe7 981 PROXY_ERROR_GENERAL, 0);
982 return 1;
983 }
984
985 if (data[1] == 0x00) p->state = 2; /* no authentication needed */
986 else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */
987 else if (data[1] == 0x02) p->state = 5; /* username/password authentication */
988 else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */
989 else {
7983f47e 990 plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",
6971bbe7 991 PROXY_ERROR_GENERAL, 0);
992 return 1;
993 }
aab91a3e 994 bufchain_consume(&p->pending_input_data, 2);
995 }
996
997 if (p->state == 7) {
998
999 /* password authentication reply format:
1000 * version number (1 bytes) = 1
1001 * reply code (1 byte)
1002 * 0 = succeeded
1003 * >0 = failed
1004 */
7983f47e 1005 char data[2];
1006
1007 if (bufchain_size(&p->pending_input_data) < 2)
1008 return 1; /* not got anything yet */
aab91a3e 1009
1010 /* get the response */
7983f47e 1011 bufchain_fetch(&p->pending_input_data, data, 2);
aab91a3e 1012
1013 if (data[0] != 1) {
7983f47e 1014 plug_closing(p->plug, "Proxy error: SOCKS password "
1015 "subnegotiation contained wrong version number",
aab91a3e 1016 PROXY_ERROR_GENERAL, 0);
1017 return 1;
1018 }
1019
1020 if (data[1] != 0) {
1021
7983f47e 1022 plug_closing(p->plug, "Proxy error: SOCKS proxy refused"
1023 " password authentication",
aab91a3e 1024 PROXY_ERROR_GENERAL, 0);
1025 return 1;
1026 }
1027
1028 bufchain_consume(&p->pending_input_data, 2);
1029 p->state = 2; /* now proceed as authenticated */
6971bbe7 1030 }
1031
f33ba69e 1032 if (p->state == 8) {
1033 int ret;
1034 ret = proxy_socks5_handlechap(p);
1035 if (ret) return ret;
1036 }
1037
6971bbe7 1038 if (p->state == 2) {
1039
1040 /* request format:
1041 * version number (1 byte) = 5
1042 * command code (1 byte)
1043 * 1 = CONNECT
1044 * 2 = BIND
1045 * 3 = UDP ASSOCIATE
1046 * reserved (1 byte) = 0x00
1047 * address type (1 byte)
1048 * 1 = IPv4
1049 * 3 = domainname (first byte has length, no terminating null)
1050 * 4 = IPv6
1051 * dest. address (variable)
1052 * dest. port (2 bytes) [network order]
1053 */
1054
b7a189f3 1055 char command[512];
6971bbe7 1056 int len;
b7a189f3 1057 int type;
6971bbe7 1058
b7a189f3 1059 type = sk_addrtype(p->remote_addr);
1060 if (type == ADDRTYPE_IPV4) {
1061 len = 10; /* 4 hdr + 4 addr + 2 trailer */
6971bbe7 1062 command[3] = 1; /* IPv4 */
b7a189f3 1063 sk_addrcopy(p->remote_addr, command+4);
1064 } else if (type == ADDRTYPE_IPV6) {
1065 len = 22; /* 4 hdr + 16 addr + 2 trailer */
6971bbe7 1066 command[3] = 4; /* IPv6 */
b7a189f3 1067 sk_addrcopy(p->remote_addr, command+4);
5e391f65 1068 } else {
1069 assert(type == ADDRTYPE_NAME);
b7a189f3 1070 command[3] = 3;
1071 sk_getaddr(p->remote_addr, command+5, 256);
1072 command[4] = strlen(command+5);
1073 len = 7 + command[4]; /* 4 hdr, 1 len, N addr, 2 trailer */
6971bbe7 1074 }
1075
1076 command[0] = 5; /* version 5 */
1077 command[1] = 1; /* CONNECT command */
1078 command[2] = 0x00;
1079
6971bbe7 1080 /* port */
1081 command[len-2] = (char) (p->remote_port >> 8) & 0xff;
1082 command[len-1] = (char) p->remote_port & 0xff;
1083
1084 sk_write(p->sub_socket, command, len);
1085
1086 p->state = 3;
1087 return 1;
1088 }
1089
1090 if (p->state == 3) {
1091
1092 /* reply format:
1093 * version number (1 bytes) = 5
1094 * reply code (1 byte)
1095 * 0 = succeeded
1096 * 1 = general SOCKS server failure
1097 * 2 = connection not allowed by ruleset
1098 * 3 = network unreachable
1099 * 4 = host unreachable
1100 * 5 = connection refused
1101 * 6 = TTL expired
1102 * 7 = command not supported
1103 * 8 = address type not supported
1104 * reserved (1 byte) = x00
1105 * address type (1 byte)
1106 * 1 = IPv4
1107 * 3 = domainname (first byte has length, no terminating null)
1108 * 4 = IPv6
1109 * server bound address (variable)
1110 * server bound port (2 bytes) [network order]
1111 */
7983f47e 1112 char data[5];
1113 int len;
1114
1115 /* First 5 bytes of packet are enough to tell its length. */
1116 if (bufchain_size(&p->pending_input_data) < 5)
1117 return 1; /* not got anything yet */
6971bbe7 1118
1119 /* get the response */
7983f47e 1120 bufchain_fetch(&p->pending_input_data, data, 5);
6971bbe7 1121
1122 if (data[0] != 5) {
7983f47e 1123 plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",
6971bbe7 1124 PROXY_ERROR_GENERAL, 0);
1125 return 1;
1126 }
1127
1128 if (data[1] != 0) {
7983f47e 1129 char buf[256];
1130
1131 strcpy(buf, "Proxy error: ");
6971bbe7 1132
1133 switch (data[1]) {
7983f47e 1134 case 1: strcat(buf, "General SOCKS server failure"); break;
1135 case 2: strcat(buf, "Connection not allowed by ruleset"); break;
1136 case 3: strcat(buf, "Network unreachable"); break;
1137 case 4: strcat(buf, "Host unreachable"); break;
1138 case 5: strcat(buf, "Connection refused"); break;
1139 case 6: strcat(buf, "TTL expired"); break;
1140 case 7: strcat(buf, "Command not supported"); break;
1141 case 8: strcat(buf, "Address type not supported"); break;
1142 default: sprintf(buf+strlen(buf),
1143 "Unrecognised SOCKS error code %d",
1144 data[1]);
6971bbe7 1145 break;
1146 }
7983f47e 1147 plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
1148
1149 return 1;
1150 }
6971bbe7 1151
7983f47e 1152 /*
1153 * Eat the rest of the reply packet.
1154 */
1155 len = 6; /* first 4 bytes, last 2 */
1156 switch (data[3]) {
1157 case 1: len += 4; break; /* IPv4 address */
1158 case 4: len += 16; break;/* IPv6 address */
1159 case 3: len += (unsigned char)data[4]; break; /* domain name */
1160 default:
1161 plug_closing(p->plug, "Proxy error: SOCKS proxy returned "
1162 "unrecognised address format",
1163 PROXY_ERROR_GENERAL, 0);
6971bbe7 1164 return 1;
1165 }
7983f47e 1166 if (bufchain_size(&p->pending_input_data) < len)
1167 return 1; /* not got whole reply yet */
1168 bufchain_consume(&p->pending_input_data, len);
6971bbe7 1169
1170 /* we're done */
1171 proxy_activate(p);
6971bbe7 1172 return 1;
1173 }
1174
1175 if (p->state == 4) {
1176 /* TODO: Handle GSSAPI authentication */
2192a8e4 1177 plug_closing(p->plug, "Proxy error: We don't support GSSAPI authentication",
6971bbe7 1178 PROXY_ERROR_GENERAL, 0);
1179 return 1;
1180 }
1181
1182 if (p->state == 5) {
4a693cfc 1183 char *username = conf_get_str(p->conf, CONF_proxy_username);
1184 char *password = conf_get_str(p->conf, CONF_proxy_password);
1185 if (username[0] || password[0]) {
1186 char userpwbuf[255 + 255 + 3];
aab91a3e 1187 int ulen, plen;
4a693cfc 1188 ulen = strlen(username);
aab91a3e 1189 if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
4a693cfc 1190 plen = strlen(password);
aab91a3e 1191 if (plen > 255) plen = 255; if (plen < 1) plen = 1;
1192 userpwbuf[0] = 1; /* version number of subnegotiation */
1193 userpwbuf[1] = ulen;
4a693cfc 1194 memcpy(userpwbuf+2, username, ulen);
aab91a3e 1195 userpwbuf[ulen+2] = plen;
4a693cfc 1196 memcpy(userpwbuf+ulen+3, password, plen);
aab91a3e 1197 sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);
1198 p->state = 7;
1199 } else
2192a8e4 1200 plug_closing(p->plug, "Proxy error: Server chose "
aab91a3e 1201 "username/password authentication but we "
1202 "didn't offer it!",
6971bbe7 1203 PROXY_ERROR_GENERAL, 0);
1204 return 1;
1205 }
1206
1207 if (p->state == 6) {
f33ba69e 1208 int ret;
1209 ret = proxy_socks5_selectchap(p);
1210 if (ret) return ret;
6971bbe7 1211 }
aab91a3e 1212
6971bbe7 1213 }
1214
2192a8e4 1215 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
6971bbe7 1216 PROXY_ERROR_UNEXPECTED, 0);
1217 return 1;
8eebd221 1218}
1219
1220/* ----------------------------------------------------------------------
0e8f4cda 1221 * `Telnet' proxy type.
8eebd221 1222 *
1223 * (This is for ad-hoc proxies where you connect to the proxy's
1224 * telnet port and send a command such as `connect host port'. The
1225 * command is configurable, since this proxy type is typically not
1226 * standardised or at all well-defined.)
1227 */
1228
4a693cfc 1229char *format_telnet_command(SockAddr addr, int port, Conf *conf)
8eebd221 1230{
4a693cfc 1231 char *fmt = conf_get_str(conf, CONF_proxy_telnet_command);
0f0a2507 1232 char *ret = NULL;
1233 int retlen = 0, retsize = 0;
1234 int so = 0, eo = 0;
1235#define ENSURE(n) do { \
1236 if (retsize < retlen + n) { \
1237 retsize = retlen + n + 512; \
1238 ret = sresize(ret, retsize, char); \
1239 } \
1240} while (0)
1241
1242 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
1243 * %%, %host, %port, %user, and %pass
1244 */
0e8f4cda 1245
4a693cfc 1246 while (fmt[eo] != 0) {
0e8f4cda 1247
0f0a2507 1248 /* scan forward until we hit end-of-line,
1249 * or an escape character (\ or %) */
4a693cfc 1250 while (fmt[eo] != 0 && fmt[eo] != '%' && fmt[eo] != '\\')
1251 eo++;
0e8f4cda 1252
0f0a2507 1253 /* if we hit eol, break out of our escaping loop */
4a693cfc 1254 if (fmt[eo] == 0) break;
0e8f4cda 1255
0f0a2507 1256 /* if there was any unescaped text before the escape
1257 * character, send that now */
1258 if (eo != so) {
1259 ENSURE(eo - so);
4a693cfc 1260 memcpy(ret + retlen, fmt + so, eo - so);
0f0a2507 1261 retlen += eo - so;
1262 }
0e8f4cda 1263
0f0a2507 1264 so = eo++;
0e8f4cda 1265
0f0a2507 1266 /* if the escape character was the last character of
1267 * the line, we'll just stop and send it. */
4a693cfc 1268 if (fmt[eo] == 0) break;
0e8f4cda 1269
4a693cfc 1270 if (fmt[so] == '\\') {
0e8f4cda 1271
0f0a2507 1272 /* we recognize \\, \%, \r, \n, \t, \x??.
1273 * anything else, we just send unescaped (including the \).
1274 */
0e8f4cda 1275
4a693cfc 1276 switch (fmt[eo]) {
0f0a2507 1277
1278 case '\\':
1279 ENSURE(1);
1280 ret[retlen++] = '\\';
1281 eo++;
1282 break;
1283
1284 case '%':
1285 ENSURE(1);
1286 ret[retlen++] = '%';
1287 eo++;
1288 break;
1289
1290 case 'r':
1291 ENSURE(1);
1292 ret[retlen++] = '\r';
1293 eo++;
1294 break;
1295
1296 case 'n':
1297 ENSURE(1);
1298 ret[retlen++] = '\n';
1299 eo++;
1300 break;
1301
1302 case 't':
1303 ENSURE(1);
1304 ret[retlen++] = '\t';
1305 eo++;
1306 break;
1307
1308 case 'x':
1309 case 'X':
1310 {
0e8f4cda 1311 /* escaped hexadecimal value (ie. \xff) */
1312 unsigned char v = 0;
1313 int i = 0;
1314
1315 for (;;) {
1316 eo++;
4a693cfc 1317 if (fmt[eo] >= '0' && fmt[eo] <= '9')
1318 v += fmt[eo] - '0';
1319 else if (fmt[eo] >= 'a' && fmt[eo] <= 'f')
1320 v += fmt[eo] - 'a' + 10;
1321 else if (fmt[eo] >= 'A' && fmt[eo] <= 'F')
1322 v += fmt[eo] - 'A' + 10;
0e8f4cda 1323 else {
1324 /* non hex character, so we abort and just
1325 * send the whole thing unescaped (including \x)
1326 */
0f0a2507 1327 ENSURE(1);
1328 ret[retlen++] = '\\';
0e8f4cda 1329 eo = so + 1;
1330 break;
1331 }
1332
1333 /* we only extract two hex characters */
1334 if (i == 1) {
0f0a2507 1335 ENSURE(1);
1336 ret[retlen++] = v;
0e8f4cda 1337 eo++;
1338 break;
1339 }
1340
1341 i++;
1342 v <<= 4;
1343 }
0e8f4cda 1344 }
0f0a2507 1345 break;
0e8f4cda 1346
0f0a2507 1347 default:
1348 ENSURE(2);
4a693cfc 1349 memcpy(ret+retlen, fmt + so, 2);
0f0a2507 1350 retlen += 2;
1351 eo++;
1352 break;
0e8f4cda 1353 }
0f0a2507 1354 } else {
0e8f4cda 1355
0f0a2507 1356 /* % escape. we recognize %%, %host, %port, %user, %pass.
c4190b30 1357 * %proxyhost, %proxyport. Anything else we just send
1358 * unescaped (including the %).
0f0a2507 1359 */
0e8f4cda 1360
4a693cfc 1361 if (fmt[eo] == '%') {
0f0a2507 1362 ENSURE(1);
1363 ret[retlen++] = '%';
1364 eo++;
1365 }
4a693cfc 1366 else if (strnicmp(fmt + eo, "host", 4) == 0) {
0f0a2507 1367 char dest[512];
1368 int destlen;
1369 sk_getaddr(addr, dest, lenof(dest));
1370 destlen = strlen(dest);
1371 ENSURE(destlen);
1372 memcpy(ret+retlen, dest, destlen);
1373 retlen += destlen;
1374 eo += 4;
1375 }
4a693cfc 1376 else if (strnicmp(fmt + eo, "port", 4) == 0) {
0f0a2507 1377 char portstr[8], portlen;
1378 portlen = sprintf(portstr, "%i", port);
1379 ENSURE(portlen);
1380 memcpy(ret + retlen, portstr, portlen);
1381 retlen += portlen;
1382 eo += 4;
1383 }
4a693cfc 1384 else if (strnicmp(fmt + eo, "user", 4) == 0) {
1385 char *username = conf_get_str(conf, CONF_proxy_username);
1386 int userlen = strlen(username);
0f0a2507 1387 ENSURE(userlen);
4a693cfc 1388 memcpy(ret+retlen, username, userlen);
0f0a2507 1389 retlen += userlen;
1390 eo += 4;
1391 }
4a693cfc 1392 else if (strnicmp(fmt + eo, "pass", 4) == 0) {
1393 char *password = conf_get_str(conf, CONF_proxy_password);
1394 int passlen = strlen(password);
0f0a2507 1395 ENSURE(passlen);
4a693cfc 1396 memcpy(ret+retlen, password, passlen);
0f0a2507 1397 retlen += passlen;
1398 eo += 4;
1399 }
4a693cfc 1400 else if (strnicmp(fmt + eo, "proxyhost", 9) == 0) {
1401 char *host = conf_get_str(conf, CONF_proxy_host);
1402 int phlen = strlen(host);
c4190b30 1403 ENSURE(phlen);
4a693cfc 1404 memcpy(ret+retlen, host, phlen);
c4190b30 1405 retlen += phlen;
1406 eo += 9;
1407 }
4a693cfc 1408 else if (strnicmp(fmt + eo, "proxyport", 9) == 0) {
1409 int port = conf_get_int(conf, CONF_proxy_port);
c4190b30 1410 char pport[50];
1411 int pplen;
4a693cfc 1412 sprintf(pport, "%d", port);
3d291a54 1413 pplen = strlen(pport);
c4190b30 1414 ENSURE(pplen);
1415 memcpy(ret+retlen, pport, pplen);
1416 retlen += pplen;
1417 eo += 9;
1418 }
0f0a2507 1419 else {
1420 /* we don't escape this, so send the % now, and
1421 * don't advance eo, so that we'll consider the
1422 * text immediately following the % as unescaped.
1423 */
1424 ENSURE(1);
1425 ret[retlen++] = '%';
1426 }
0e8f4cda 1427 }
1428
0f0a2507 1429 /* resume scanning for additional escapes after this one. */
1430 so = eo;
1431 }
1432
1433 /* if there is any unescaped text at the end of the line, send it */
1434 if (eo != so) {
1435 ENSURE(eo - so);
4a693cfc 1436 memcpy(ret + retlen, fmt + so, eo - so);
0f0a2507 1437 retlen += eo - so;
1438 }
1439
1440 ENSURE(1);
1441 ret[retlen] = '\0';
1442 return ret;
1443
1444#undef ENSURE
1445}
1446
1447int proxy_telnet_negotiate (Proxy_Socket p, int change)
1448{
1449 if (p->state == PROXY_CHANGE_NEW) {
1450 char *formatted_cmd;
1451
1452 formatted_cmd = format_telnet_command(p->remote_addr, p->remote_port,
4a693cfc 1453 p->conf);
0f0a2507 1454
1455 sk_write(p->sub_socket, formatted_cmd, strlen(formatted_cmd));
1456 sfree(formatted_cmd);
1457
0e8f4cda 1458 p->state = 1;
0e8f4cda 1459 return 0;
1460 }
1461
1462 if (change == PROXY_CHANGE_CLOSING) {
1463 /* if our proxy negotiation process involves closing and opening
1464 * new sockets, then we would want to intercept this closing
1465 * callback when we were expecting it. if we aren't anticipating
1466 * a socket close, then some error must have occurred. we'll
1467 * just pass those errors up to the backend.
1468 */
1469 return plug_closing(p->plug, p->closing_error_msg,
1470 p->closing_error_code,
1471 p->closing_calling_back);
1472 }
1473
1474 if (change == PROXY_CHANGE_SENT) {
1475 /* some (or all) of what we wrote to the proxy was sent.
1476 * we don't do anything new, however, until we receive the
1477 * proxy's response. we might want to set a timer so we can
1478 * timeout the proxy negotiation after a while...
1479 */
1480 return 0;
1481 }
1482
1483 if (change == PROXY_CHANGE_ACCEPTING) {
1484 /* we should _never_ see this, as we are using our socket to
1485 * connect to a proxy, not accepting inbound connections.
1486 * what should we do? close the socket with an appropriate
1487 * error message?
1488 */
1489 return plug_accepting(p->plug, p->accepting_sock);
1490 }
1491
1492 if (change == PROXY_CHANGE_RECEIVE) {
1493 /* we have received data from the underlying socket, which
1494 * we'll need to parse, process, and respond to appropriately.
1495 */
1496
1497 /* we're done */
1498 proxy_activate(p);
1499 /* proxy activate will have dealt with
1500 * whatever is left of the buffer */
1501 return 1;
1502 }
1503
2192a8e4 1504 plug_closing(p->plug, "Proxy error: Unexpected proxy error",
0e8f4cda 1505 PROXY_ERROR_UNEXPECTED, 0);
6971bbe7 1506 return 1;
8eebd221 1507}