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