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