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