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