diff options
Diffstat (limited to 'src/gns/gnunet-gns-proxy.c')
-rw-r--r-- | src/gns/gnunet-gns-proxy.c | 3909 |
1 files changed, 0 insertions, 3909 deletions
diff --git a/src/gns/gnunet-gns-proxy.c b/src/gns/gnunet-gns-proxy.c deleted file mode 100644 index ec9fb12ae..000000000 --- a/src/gns/gnunet-gns-proxy.c +++ /dev/null | |||
@@ -1,3909 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @author Martin Schanzenbach | ||
22 | * @author Christian Grothoff | ||
23 | * @file src/gns/gnunet-gns-proxy.c | ||
24 | * @brief HTTP(S) proxy that rewrites URIs and fakes certificates to make GNS work | ||
25 | * with legacy browsers | ||
26 | * | ||
27 | * TODO: | ||
28 | * - double-check queueing logic | ||
29 | */ | ||
30 | #include "platform.h" | ||
31 | #include <microhttpd.h> | ||
32 | /* Just included for the right curl.h */ | ||
33 | #include "gnunet_curl_lib.h" | ||
34 | #include <gnutls/gnutls.h> | ||
35 | #include <gnutls/x509.h> | ||
36 | #include <gnutls/abstract.h> | ||
37 | #include <gnutls/crypto.h> | ||
38 | #if HAVE_GNUTLS_DANE | ||
39 | #include <gnutls/dane.h> | ||
40 | #endif | ||
41 | #include <regex.h> | ||
42 | #include "gnunet_util_lib.h" | ||
43 | #include "gnunet_gns_service.h" | ||
44 | #include "gnunet_identity_service.h" | ||
45 | #include "gns.h" | ||
46 | #include "gnunet_mhd_compat.h" | ||
47 | |||
48 | /** | ||
49 | * Default Socks5 listen port. | ||
50 | */ | ||
51 | #define GNUNET_GNS_PROXY_PORT 7777 | ||
52 | |||
53 | /** | ||
54 | * Maximum supported length for a URI. | ||
55 | * Should die. @deprecated | ||
56 | */ | ||
57 | #define MAX_HTTP_URI_LENGTH 2048 | ||
58 | |||
59 | /** | ||
60 | * Maximum number of DANE records we support | ||
61 | * per domain name (and port and protocol). | ||
62 | */ | ||
63 | #define MAX_DANES 32 | ||
64 | |||
65 | /** | ||
66 | * Size of the buffer for the data upload / download. Must be | ||
67 | * enough for curl, thus CURL_MAX_WRITE_SIZE is needed here (16k). | ||
68 | */ | ||
69 | #define IO_BUFFERSIZE CURL_MAX_WRITE_SIZE | ||
70 | |||
71 | /** | ||
72 | * Size of the read/write buffers for Socks. Uses | ||
73 | * 256 bytes for the hostname (at most), plus a few | ||
74 | * bytes overhead for the messages. | ||
75 | */ | ||
76 | #define SOCKS_BUFFERSIZE (256 + 32) | ||
77 | |||
78 | /** | ||
79 | * Port for plaintext HTTP. | ||
80 | */ | ||
81 | #define HTTP_PORT 80 | ||
82 | |||
83 | /** | ||
84 | * Port for HTTPS. | ||
85 | */ | ||
86 | #define HTTPS_PORT 443 | ||
87 | |||
88 | /** | ||
89 | * Largest allowed size for a PEM certificate. | ||
90 | */ | ||
91 | #define MAX_PEM_SIZE (10 * 1024) | ||
92 | |||
93 | /** | ||
94 | * After how long do we clean up unused MHD TLS instances? | ||
95 | */ | ||
96 | #define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply ( \ | ||
97 | GNUNET_TIME_UNIT_MINUTES, 5) | ||
98 | |||
99 | /** | ||
100 | * After how long do we clean up Socks5 handles that failed to show any activity | ||
101 | * with their respective MHD instance? | ||
102 | */ | ||
103 | #define HTTP_HANDSHAKE_TIMEOUT GNUNET_TIME_relative_multiply ( \ | ||
104 | GNUNET_TIME_UNIT_SECONDS, 15) | ||
105 | |||
106 | |||
107 | /** | ||
108 | * Log curl error. | ||
109 | * | ||
110 | * @param level log level | ||
111 | * @param fun name of curl_easy-function that gave the error | ||
112 | * @param rc return code from curl | ||
113 | */ | ||
114 | #define LOG_CURL_EASY(level, fun, rc) \ | ||
115 | GNUNET_log (level, \ | ||
116 | _ ("%s failed at %s:%d: `%s'\n"), \ | ||
117 | fun, \ | ||
118 | __FILE__, \ | ||
119 | __LINE__, \ | ||
120 | curl_easy_strerror (rc)) | ||
121 | |||
122 | |||
123 | /* *************** Socks protocol definitions (move to TUN?) ****************** */ | ||
124 | |||
125 | /** | ||
126 | * Which SOCKS version do we speak? | ||
127 | */ | ||
128 | #define SOCKS_VERSION_5 0x05 | ||
129 | |||
130 | /** | ||
131 | * Flag to set for 'no authentication'. | ||
132 | */ | ||
133 | #define SOCKS_AUTH_NONE 0 | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Commands in Socks5. | ||
138 | */ | ||
139 | enum Socks5Commands | ||
140 | { | ||
141 | /** | ||
142 | * Establish TCP/IP stream. | ||
143 | */ | ||
144 | SOCKS5_CMD_TCP_STREAM = 1, | ||
145 | |||
146 | /** | ||
147 | * Establish TCP port binding. | ||
148 | */ | ||
149 | SOCKS5_CMD_TCP_PORT = 2, | ||
150 | |||
151 | /** | ||
152 | * Establish UDP port binding. | ||
153 | */ | ||
154 | SOCKS5_CMD_UDP_PORT = 3 | ||
155 | }; | ||
156 | |||
157 | |||
158 | /** | ||
159 | * Address types in Socks5. | ||
160 | */ | ||
161 | enum Socks5AddressType | ||
162 | { | ||
163 | /** | ||
164 | * IPv4 address. | ||
165 | */ | ||
166 | SOCKS5_AT_IPV4 = 1, | ||
167 | |||
168 | /** | ||
169 | * IPv4 address. | ||
170 | */ | ||
171 | SOCKS5_AT_DOMAINNAME = 3, | ||
172 | |||
173 | /** | ||
174 | * IPv6 address. | ||
175 | */ | ||
176 | SOCKS5_AT_IPV6 = 4 | ||
177 | }; | ||
178 | |||
179 | |||
180 | /** | ||
181 | * Status codes in Socks5 response. | ||
182 | */ | ||
183 | enum Socks5StatusCode | ||
184 | { | ||
185 | SOCKS5_STATUS_REQUEST_GRANTED = 0, | ||
186 | SOCKS5_STATUS_GENERAL_FAILURE = 1, | ||
187 | SOCKS5_STATUS_CONNECTION_NOT_ALLOWED_BY_RULE = 2, | ||
188 | SOCKS5_STATUS_NETWORK_UNREACHABLE = 3, | ||
189 | SOCKS5_STATUS_HOST_UNREACHABLE = 4, | ||
190 | SOCKS5_STATUS_CONNECTION_REFUSED_BY_HOST = 5, | ||
191 | SOCKS5_STATUS_TTL_EXPIRED = 6, | ||
192 | SOCKS5_STATUS_COMMAND_NOT_SUPPORTED = 7, | ||
193 | SOCKS5_STATUS_ADDRESS_TYPE_NOT_SUPPORTED = 8 | ||
194 | }; | ||
195 | |||
196 | |||
197 | /** | ||
198 | * Client hello in Socks5 protocol. | ||
199 | */ | ||
200 | struct Socks5ClientHelloMessage | ||
201 | { | ||
202 | /** | ||
203 | * Should be #SOCKS_VERSION_5. | ||
204 | */ | ||
205 | uint8_t version; | ||
206 | |||
207 | /** | ||
208 | * How many authentication methods does the client support. | ||
209 | */ | ||
210 | uint8_t num_auth_methods; | ||
211 | |||
212 | /* followed by supported authentication methods, 1 byte per method */ | ||
213 | }; | ||
214 | |||
215 | |||
216 | /** | ||
217 | * Server hello in Socks5 protocol. | ||
218 | */ | ||
219 | struct Socks5ServerHelloMessage | ||
220 | { | ||
221 | /** | ||
222 | * Should be #SOCKS_VERSION_5. | ||
223 | */ | ||
224 | uint8_t version; | ||
225 | |||
226 | /** | ||
227 | * Chosen authentication method, for us always #SOCKS_AUTH_NONE, | ||
228 | * which skips the authentication step. | ||
229 | */ | ||
230 | uint8_t auth_method; | ||
231 | }; | ||
232 | |||
233 | |||
234 | /** | ||
235 | * Client socks request in Socks5 protocol. | ||
236 | */ | ||
237 | struct Socks5ClientRequestMessage | ||
238 | { | ||
239 | /** | ||
240 | * Should be #SOCKS_VERSION_5. | ||
241 | */ | ||
242 | uint8_t version; | ||
243 | |||
244 | /** | ||
245 | * Command code, we only uspport #SOCKS5_CMD_TCP_STREAM. | ||
246 | */ | ||
247 | uint8_t command; | ||
248 | |||
249 | /** | ||
250 | * Reserved, always zero. | ||
251 | */ | ||
252 | uint8_t resvd; | ||
253 | |||
254 | /** | ||
255 | * Address type, an `enum Socks5AddressType`. | ||
256 | */ | ||
257 | uint8_t addr_type; | ||
258 | |||
259 | /* | ||
260 | * Followed by either an ip4/ipv6 address or a domain name with a | ||
261 | * length field (uint8_t) in front (depending on @e addr_type). | ||
262 | * followed by port number in network byte order (uint16_t). | ||
263 | */ | ||
264 | }; | ||
265 | |||
266 | |||
267 | /** | ||
268 | * Server response to client requests in Socks5 protocol. | ||
269 | */ | ||
270 | struct Socks5ServerResponseMessage | ||
271 | { | ||
272 | /** | ||
273 | * Should be #SOCKS_VERSION_5. | ||
274 | */ | ||
275 | uint8_t version; | ||
276 | |||
277 | /** | ||
278 | * Status code, an `enum Socks5StatusCode` | ||
279 | */ | ||
280 | uint8_t reply; | ||
281 | |||
282 | /** | ||
283 | * Always zero. | ||
284 | */ | ||
285 | uint8_t reserved; | ||
286 | |||
287 | /** | ||
288 | * Address type, an `enum Socks5AddressType`. | ||
289 | */ | ||
290 | uint8_t addr_type; | ||
291 | |||
292 | /* | ||
293 | * Followed by either an ip4/ipv6 address or a domain name with a | ||
294 | * length field (uint8_t) in front (depending on @e addr_type). | ||
295 | * followed by port number in network byte order (uint16_t). | ||
296 | */ | ||
297 | }; | ||
298 | |||
299 | |||
300 | /* *********************** Datastructures for HTTP handling ****************** */ | ||
301 | |||
302 | /** | ||
303 | * A structure for CA cert/key | ||
304 | */ | ||
305 | struct ProxyCA | ||
306 | { | ||
307 | /** | ||
308 | * The certificate | ||
309 | */ | ||
310 | gnutls_x509_crt_t cert; | ||
311 | |||
312 | /** | ||
313 | * The private key | ||
314 | */ | ||
315 | gnutls_x509_privkey_t key; | ||
316 | }; | ||
317 | |||
318 | |||
319 | /** | ||
320 | * Structure for GNS certificates | ||
321 | */ | ||
322 | struct ProxyGNSCertificate | ||
323 | { | ||
324 | /** | ||
325 | * The certificate as PEM | ||
326 | */ | ||
327 | char cert[MAX_PEM_SIZE]; | ||
328 | |||
329 | /** | ||
330 | * The private key as PEM | ||
331 | */ | ||
332 | char key[MAX_PEM_SIZE]; | ||
333 | }; | ||
334 | |||
335 | |||
336 | /** | ||
337 | * A structure for all running Httpds | ||
338 | */ | ||
339 | struct MhdHttpList | ||
340 | { | ||
341 | /** | ||
342 | * DLL for httpds | ||
343 | */ | ||
344 | struct MhdHttpList *prev; | ||
345 | |||
346 | /** | ||
347 | * DLL for httpds | ||
348 | */ | ||
349 | struct MhdHttpList *next; | ||
350 | |||
351 | /** | ||
352 | * the domain name to server (only important for TLS) | ||
353 | */ | ||
354 | char *domain; | ||
355 | |||
356 | /** | ||
357 | * The daemon handle | ||
358 | */ | ||
359 | struct MHD_Daemon *daemon; | ||
360 | |||
361 | /** | ||
362 | * Optional proxy certificate used | ||
363 | */ | ||
364 | struct ProxyGNSCertificate *proxy_cert; | ||
365 | |||
366 | /** | ||
367 | * The task ID | ||
368 | */ | ||
369 | struct GNUNET_SCHEDULER_Task *httpd_task; | ||
370 | |||
371 | /** | ||
372 | * is this an ssl daemon? | ||
373 | */ | ||
374 | int is_ssl; | ||
375 | }; | ||
376 | |||
377 | |||
378 | /* ***************** Datastructures for Socks handling **************** */ | ||
379 | |||
380 | |||
381 | /** | ||
382 | * The socks phases. | ||
383 | */ | ||
384 | enum SocksPhase | ||
385 | { | ||
386 | /** | ||
387 | * We're waiting to get the client hello. | ||
388 | */ | ||
389 | SOCKS5_INIT, | ||
390 | |||
391 | /** | ||
392 | * We're waiting to get the initial request. | ||
393 | */ | ||
394 | SOCKS5_REQUEST, | ||
395 | |||
396 | /** | ||
397 | * We are currently resolving the destination. | ||
398 | */ | ||
399 | SOCKS5_RESOLVING, | ||
400 | |||
401 | /** | ||
402 | * We're in transfer mode. | ||
403 | */ | ||
404 | SOCKS5_DATA_TRANSFER, | ||
405 | |||
406 | /** | ||
407 | * Finish writing the write buffer, then clean up. | ||
408 | */ | ||
409 | SOCKS5_WRITE_THEN_CLEANUP, | ||
410 | |||
411 | /** | ||
412 | * Socket has been passed to MHD, do not close it anymore. | ||
413 | */ | ||
414 | SOCKS5_SOCKET_WITH_MHD, | ||
415 | |||
416 | /** | ||
417 | * We've started receiving upload data from MHD. | ||
418 | */ | ||
419 | SOCKS5_SOCKET_UPLOAD_STARTED, | ||
420 | |||
421 | /** | ||
422 | * We've finished receiving upload data from MHD. | ||
423 | */ | ||
424 | SOCKS5_SOCKET_UPLOAD_DONE, | ||
425 | |||
426 | /** | ||
427 | * We've finished uploading data via CURL and can now download. | ||
428 | */ | ||
429 | SOCKS5_SOCKET_DOWNLOAD_STARTED, | ||
430 | |||
431 | /** | ||
432 | * We've finished receiving download data from cURL. | ||
433 | */ | ||
434 | SOCKS5_SOCKET_DOWNLOAD_DONE | ||
435 | }; | ||
436 | |||
437 | |||
438 | /** | ||
439 | * A header list | ||
440 | */ | ||
441 | struct HttpResponseHeader | ||
442 | { | ||
443 | /** | ||
444 | * DLL | ||
445 | */ | ||
446 | struct HttpResponseHeader *next; | ||
447 | |||
448 | /** | ||
449 | * DLL | ||
450 | */ | ||
451 | struct HttpResponseHeader *prev; | ||
452 | |||
453 | /** | ||
454 | * Header type | ||
455 | */ | ||
456 | char *type; | ||
457 | |||
458 | /** | ||
459 | * Header value | ||
460 | */ | ||
461 | char *value; | ||
462 | }; | ||
463 | |||
464 | /** | ||
465 | * A structure for socks requests | ||
466 | */ | ||
467 | struct Socks5Request | ||
468 | { | ||
469 | /** | ||
470 | * DLL. | ||
471 | */ | ||
472 | struct Socks5Request *next; | ||
473 | |||
474 | /** | ||
475 | * DLL. | ||
476 | */ | ||
477 | struct Socks5Request *prev; | ||
478 | |||
479 | /** | ||
480 | * The client socket | ||
481 | */ | ||
482 | struct GNUNET_NETWORK_Handle *sock; | ||
483 | |||
484 | /** | ||
485 | * Handle to GNS lookup, during #SOCKS5_RESOLVING phase. | ||
486 | */ | ||
487 | struct GNUNET_GNS_LookupWithTldRequest *gns_lookup; | ||
488 | |||
489 | /** | ||
490 | * Client socket read task | ||
491 | */ | ||
492 | struct GNUNET_SCHEDULER_Task *rtask; | ||
493 | |||
494 | /** | ||
495 | * Client socket write task | ||
496 | */ | ||
497 | struct GNUNET_SCHEDULER_Task *wtask; | ||
498 | |||
499 | /** | ||
500 | * Timeout task | ||
501 | */ | ||
502 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
503 | |||
504 | /** | ||
505 | * Read buffer | ||
506 | */ | ||
507 | char rbuf[SOCKS_BUFFERSIZE]; | ||
508 | |||
509 | /** | ||
510 | * Write buffer | ||
511 | */ | ||
512 | char wbuf[SOCKS_BUFFERSIZE]; | ||
513 | |||
514 | /** | ||
515 | * Buffer we use for moving data between MHD and curl (in both directions). | ||
516 | */ | ||
517 | char io_buf[IO_BUFFERSIZE]; | ||
518 | |||
519 | /** | ||
520 | * MHD HTTP instance handling this request, NULL for none. | ||
521 | */ | ||
522 | struct MhdHttpList *hd; | ||
523 | |||
524 | /** | ||
525 | * MHD connection for this request. | ||
526 | */ | ||
527 | struct MHD_Connection *con; | ||
528 | |||
529 | /** | ||
530 | * MHD response object for this request. | ||
531 | */ | ||
532 | struct MHD_Response *response; | ||
533 | |||
534 | /** | ||
535 | * the domain name to server (only important for TLS) | ||
536 | */ | ||
537 | char *domain; | ||
538 | |||
539 | /** | ||
540 | * DNS Legacy Host Name as given by GNS, NULL if not given. | ||
541 | */ | ||
542 | char *leho; | ||
543 | |||
544 | /** | ||
545 | * Payload of the DANE records encountered. | ||
546 | */ | ||
547 | char *dane_data[MAX_DANES + 1]; | ||
548 | |||
549 | /** | ||
550 | * The URL to fetch | ||
551 | */ | ||
552 | char *url; | ||
553 | |||
554 | /** | ||
555 | * Handle to cURL | ||
556 | */ | ||
557 | CURL *curl; | ||
558 | |||
559 | /** | ||
560 | * HTTP request headers for the curl request. | ||
561 | */ | ||
562 | struct curl_slist *headers; | ||
563 | |||
564 | /** | ||
565 | * DNS->IP mappings resolved through GNS | ||
566 | */ | ||
567 | struct curl_slist *hosts; | ||
568 | |||
569 | /** | ||
570 | * HTTP response code to give to MHD for the response. | ||
571 | */ | ||
572 | unsigned int response_code; | ||
573 | |||
574 | /** | ||
575 | * Number of bytes in @e dane_data. | ||
576 | */ | ||
577 | int dane_data_len[MAX_DANES + 1]; | ||
578 | |||
579 | /** | ||
580 | * Number of entries used in @e dane_data_len | ||
581 | * and @e dane_data. | ||
582 | */ | ||
583 | unsigned int num_danes; | ||
584 | |||
585 | /** | ||
586 | * Number of bytes already in read buffer | ||
587 | */ | ||
588 | size_t rbuf_len; | ||
589 | |||
590 | /** | ||
591 | * Number of bytes already in write buffer | ||
592 | */ | ||
593 | size_t wbuf_len; | ||
594 | |||
595 | /** | ||
596 | * Number of bytes already in the IO buffer. | ||
597 | */ | ||
598 | size_t io_len; | ||
599 | |||
600 | /** | ||
601 | * Once known, what's the target address for the connection? | ||
602 | */ | ||
603 | struct sockaddr_storage destination_address; | ||
604 | |||
605 | /** | ||
606 | * The socks state | ||
607 | */ | ||
608 | enum SocksPhase state; | ||
609 | |||
610 | /** | ||
611 | * Desired destination port. | ||
612 | */ | ||
613 | uint16_t port; | ||
614 | |||
615 | /** | ||
616 | * Headers from response | ||
617 | */ | ||
618 | struct HttpResponseHeader *header_head; | ||
619 | |||
620 | /** | ||
621 | * Headers from response | ||
622 | */ | ||
623 | struct HttpResponseHeader *header_tail; | ||
624 | |||
625 | /** | ||
626 | * X.509 Certificate status | ||
627 | */ | ||
628 | int ssl_checked; | ||
629 | |||
630 | /** | ||
631 | * Was the hostname resolved via GNS? | ||
632 | */ | ||
633 | int is_gns; | ||
634 | |||
635 | /** | ||
636 | * This is (probably) a TLS connection | ||
637 | */ | ||
638 | int is_tls; | ||
639 | |||
640 | /** | ||
641 | * Did we suspend MHD processing? | ||
642 | */ | ||
643 | int suspended; | ||
644 | |||
645 | /** | ||
646 | * Did we pause CURL processing? | ||
647 | */ | ||
648 | int curl_paused; | ||
649 | }; | ||
650 | |||
651 | |||
652 | /* *********************** Globals **************************** */ | ||
653 | |||
654 | /** | ||
655 | * The address to bind to | ||
656 | */ | ||
657 | static in_addr_t address; | ||
658 | |||
659 | /** | ||
660 | * The IPv6 address to bind to | ||
661 | */ | ||
662 | static struct in6_addr address6; | ||
663 | |||
664 | /** | ||
665 | * The port the proxy is running on (default 7777) | ||
666 | */ | ||
667 | static uint16_t port = GNUNET_GNS_PROXY_PORT; | ||
668 | |||
669 | /** | ||
670 | * The CA file (pem) to use for the proxy CA | ||
671 | */ | ||
672 | static char *cafile_opt; | ||
673 | |||
674 | /** | ||
675 | * The listen socket of the proxy for IPv4 | ||
676 | */ | ||
677 | static struct GNUNET_NETWORK_Handle *lsock4; | ||
678 | |||
679 | /** | ||
680 | * The listen socket of the proxy for IPv6 | ||
681 | */ | ||
682 | static struct GNUNET_NETWORK_Handle *lsock6; | ||
683 | |||
684 | /** | ||
685 | * The listen task ID for IPv4 | ||
686 | */ | ||
687 | static struct GNUNET_SCHEDULER_Task *ltask4; | ||
688 | |||
689 | /** | ||
690 | * The listen task ID for IPv6 | ||
691 | */ | ||
692 | static struct GNUNET_SCHEDULER_Task *ltask6; | ||
693 | |||
694 | /** | ||
695 | * The cURL download task (curl multi API). | ||
696 | */ | ||
697 | static struct GNUNET_SCHEDULER_Task *curl_download_task; | ||
698 | |||
699 | /** | ||
700 | * The cURL multi handle | ||
701 | */ | ||
702 | static CURLM *curl_multi; | ||
703 | |||
704 | /** | ||
705 | * Handle to the GNS service | ||
706 | */ | ||
707 | static struct GNUNET_GNS_Handle *gns_handle; | ||
708 | |||
709 | /** | ||
710 | * Disable IPv6. | ||
711 | */ | ||
712 | static int disable_v6; | ||
713 | |||
714 | /** | ||
715 | * DLL for http/https daemons | ||
716 | */ | ||
717 | static struct MhdHttpList *mhd_httpd_head; | ||
718 | |||
719 | /** | ||
720 | * DLL for http/https daemons | ||
721 | */ | ||
722 | static struct MhdHttpList *mhd_httpd_tail; | ||
723 | |||
724 | /** | ||
725 | * Daemon for HTTP (we have one per X.509 certificate, and then one for | ||
726 | * all HTTP connections; this is the one for HTTP, not HTTPS). | ||
727 | */ | ||
728 | static struct MhdHttpList *httpd; | ||
729 | |||
730 | /** | ||
731 | * DLL of active socks requests. | ||
732 | */ | ||
733 | static struct Socks5Request *s5r_head; | ||
734 | |||
735 | /** | ||
736 | * DLL of active socks requests. | ||
737 | */ | ||
738 | static struct Socks5Request *s5r_tail; | ||
739 | |||
740 | /** | ||
741 | * The CA for X.509 certificate generation | ||
742 | */ | ||
743 | static struct ProxyCA proxy_ca; | ||
744 | |||
745 | /** | ||
746 | * Response we return on cURL failures. | ||
747 | */ | ||
748 | static struct MHD_Response *curl_failure_response; | ||
749 | |||
750 | /** | ||
751 | * Our configuration. | ||
752 | */ | ||
753 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
754 | |||
755 | |||
756 | /* ************************* Global helpers ********************* */ | ||
757 | |||
758 | |||
759 | /** | ||
760 | * Run MHD now, we have extra data ready for the callback. | ||
761 | * | ||
762 | * @param hd the daemon to run now. | ||
763 | */ | ||
764 | static void | ||
765 | run_mhd_now (struct MhdHttpList *hd); | ||
766 | |||
767 | |||
768 | /** | ||
769 | * Clean up s5r handles. | ||
770 | * | ||
771 | * @param s5r the handle to destroy | ||
772 | */ | ||
773 | static void | ||
774 | cleanup_s5r (struct Socks5Request *s5r) | ||
775 | { | ||
776 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
777 | "Cleaning up socks request\n"); | ||
778 | if (NULL != s5r->curl) | ||
779 | { | ||
780 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
781 | "Cleaning up cURL handle\n"); | ||
782 | curl_multi_remove_handle (curl_multi, | ||
783 | s5r->curl); | ||
784 | curl_easy_cleanup (s5r->curl); | ||
785 | s5r->curl = NULL; | ||
786 | } | ||
787 | if (s5r->suspended) | ||
788 | { | ||
789 | s5r->suspended = GNUNET_NO; | ||
790 | MHD_resume_connection (s5r->con); | ||
791 | } | ||
792 | curl_slist_free_all (s5r->headers); | ||
793 | if (NULL != s5r->hosts) | ||
794 | { | ||
795 | curl_slist_free_all (s5r->hosts); | ||
796 | } | ||
797 | if ((NULL != s5r->response) && | ||
798 | (curl_failure_response != s5r->response)) | ||
799 | { | ||
800 | MHD_destroy_response (s5r->response); | ||
801 | s5r->response = NULL; | ||
802 | } | ||
803 | if (NULL != s5r->rtask) | ||
804 | { | ||
805 | GNUNET_SCHEDULER_cancel (s5r->rtask); | ||
806 | s5r->rtask = NULL; | ||
807 | } | ||
808 | if (NULL != s5r->timeout_task) | ||
809 | { | ||
810 | GNUNET_SCHEDULER_cancel (s5r->timeout_task); | ||
811 | s5r->timeout_task = NULL; | ||
812 | } | ||
813 | if (NULL != s5r->wtask) | ||
814 | { | ||
815 | GNUNET_SCHEDULER_cancel (s5r->wtask); | ||
816 | s5r->wtask = NULL; | ||
817 | } | ||
818 | if (NULL != s5r->gns_lookup) | ||
819 | { | ||
820 | GNUNET_GNS_lookup_with_tld_cancel (s5r->gns_lookup); | ||
821 | s5r->gns_lookup = NULL; | ||
822 | } | ||
823 | if (NULL != s5r->sock) | ||
824 | { | ||
825 | if (SOCKS5_SOCKET_WITH_MHD <= s5r->state) | ||
826 | GNUNET_NETWORK_socket_free_memory_only_ (s5r->sock); | ||
827 | else | ||
828 | GNUNET_NETWORK_socket_close (s5r->sock); | ||
829 | s5r->sock = NULL; | ||
830 | } | ||
831 | GNUNET_CONTAINER_DLL_remove (s5r_head, | ||
832 | s5r_tail, | ||
833 | s5r); | ||
834 | GNUNET_free (s5r->domain); | ||
835 | GNUNET_free (s5r->leho); | ||
836 | GNUNET_free (s5r->url); | ||
837 | for (unsigned int i = 0; i < s5r->num_danes; i++) | ||
838 | GNUNET_free (s5r->dane_data[i]); | ||
839 | GNUNET_free (s5r); | ||
840 | } | ||
841 | |||
842 | |||
843 | /* ************************* HTTP handling with cURL *********************** */ | ||
844 | |||
845 | static void | ||
846 | curl_download_prepare (); | ||
847 | |||
848 | |||
849 | /** | ||
850 | * Callback for MHD response generation. This function is called from | ||
851 | * MHD whenever MHD expects to get data back. Copies data from the | ||
852 | * io_buf, if available. | ||
853 | * | ||
854 | * @param cls closure with our `struct Socks5Request` | ||
855 | * @param pos in buffer | ||
856 | * @param buf where to copy data | ||
857 | * @param max available space in @a buf | ||
858 | * @return number of bytes written to @a buf | ||
859 | */ | ||
860 | static ssize_t | ||
861 | mhd_content_cb (void *cls, | ||
862 | uint64_t pos, | ||
863 | char*buf, | ||
864 | size_t max) | ||
865 | { | ||
866 | struct Socks5Request *s5r = cls; | ||
867 | size_t bytes_to_copy; | ||
868 | |||
869 | if ((SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state) || | ||
870 | (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state)) | ||
871 | { | ||
872 | /* we're still not done with the upload, do not yet | ||
873 | start the download, the IO buffer is still full | ||
874 | with upload data. */ | ||
875 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
876 | "Pausing MHD download %s%s, not yet ready for download\n", | ||
877 | s5r->domain, | ||
878 | s5r->url); | ||
879 | return 0; /* not yet ready for data download */ | ||
880 | } | ||
881 | bytes_to_copy = GNUNET_MIN (max, | ||
882 | s5r->io_len); | ||
883 | if ((0 == bytes_to_copy) && | ||
884 | (SOCKS5_SOCKET_DOWNLOAD_DONE != s5r->state)) | ||
885 | { | ||
886 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
887 | "Pausing MHD download %s%s, no data available\n", | ||
888 | s5r->domain, | ||
889 | s5r->url); | ||
890 | if (NULL != s5r->curl) | ||
891 | { | ||
892 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
893 | "Continuing CURL interaction for %s%s\n", | ||
894 | s5r->domain, | ||
895 | s5r->url); | ||
896 | if (GNUNET_YES == s5r->curl_paused) | ||
897 | { | ||
898 | s5r->curl_paused = GNUNET_NO; | ||
899 | curl_easy_pause (s5r->curl, | ||
900 | CURLPAUSE_CONT); | ||
901 | } | ||
902 | curl_download_prepare (); | ||
903 | } | ||
904 | if (GNUNET_NO == s5r->suspended) | ||
905 | { | ||
906 | MHD_suspend_connection (s5r->con); | ||
907 | s5r->suspended = GNUNET_YES; | ||
908 | } | ||
909 | return 0; /* more data later */ | ||
910 | } | ||
911 | if ((0 == bytes_to_copy) && | ||
912 | (SOCKS5_SOCKET_DOWNLOAD_DONE == s5r->state)) | ||
913 | { | ||
914 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
915 | "Completed MHD download %s%s\n", | ||
916 | s5r->domain, | ||
917 | s5r->url); | ||
918 | return MHD_CONTENT_READER_END_OF_STREAM; | ||
919 | } | ||
920 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
921 | "Writing %llu/%llu bytes to %s%s\n", | ||
922 | (unsigned long long) bytes_to_copy, | ||
923 | (unsigned long long) s5r->io_len, | ||
924 | s5r->domain, | ||
925 | s5r->url); | ||
926 | GNUNET_memcpy (buf, | ||
927 | s5r->io_buf, | ||
928 | bytes_to_copy); | ||
929 | memmove (s5r->io_buf, | ||
930 | &s5r->io_buf[bytes_to_copy], | ||
931 | s5r->io_len - bytes_to_copy); | ||
932 | s5r->io_len -= bytes_to_copy; | ||
933 | if ((NULL != s5r->curl) && | ||
934 | (GNUNET_YES == s5r->curl_paused)) | ||
935 | { | ||
936 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
937 | "Continuing CURL interaction for %s%s\n", | ||
938 | s5r->domain, | ||
939 | s5r->url); | ||
940 | s5r->curl_paused = GNUNET_NO; | ||
941 | curl_easy_pause (s5r->curl, | ||
942 | CURLPAUSE_CONT); | ||
943 | } | ||
944 | return bytes_to_copy; | ||
945 | } | ||
946 | |||
947 | |||
948 | /** | ||
949 | * Check that the website has presented us with a valid X.509 certificate. | ||
950 | * The certificate must either match the domain name or the LEHO name | ||
951 | * (or, if available, the TLSA record). | ||
952 | * | ||
953 | * @param s5r request to check for. | ||
954 | * @return #GNUNET_OK if the certificate is valid | ||
955 | */ | ||
956 | static int | ||
957 | check_ssl_certificate (struct Socks5Request *s5r) | ||
958 | { | ||
959 | unsigned int cert_list_size; | ||
960 | const gnutls_datum_t *chainp; | ||
961 | const struct curl_tlssessioninfo *tlsinfo; | ||
962 | char certdn[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 3]; | ||
963 | size_t size; | ||
964 | gnutls_x509_crt_t x509_cert; | ||
965 | int rc; | ||
966 | const char *name; | ||
967 | |||
968 | s5r->ssl_checked = GNUNET_YES; | ||
969 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
970 | "Checking X.509 certificate\n"); | ||
971 | if (CURLE_OK != | ||
972 | curl_easy_getinfo (s5r->curl, | ||
973 | CURLINFO_TLS_SESSION, | ||
974 | &tlsinfo)) | ||
975 | return GNUNET_SYSERR; | ||
976 | if (CURLSSLBACKEND_GNUTLS != tlsinfo->backend) | ||
977 | { | ||
978 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
979 | _ ("Unsupported CURL TLS backend %d\n"), | ||
980 | tlsinfo->backend); | ||
981 | return GNUNET_SYSERR; | ||
982 | } | ||
983 | chainp = gnutls_certificate_get_peers (tlsinfo->internals, | ||
984 | &cert_list_size); | ||
985 | if ((! chainp) || | ||
986 | (0 == cert_list_size)) | ||
987 | return GNUNET_SYSERR; | ||
988 | |||
989 | size = sizeof(certdn); | ||
990 | /* initialize an X.509 certificate structure. */ | ||
991 | gnutls_x509_crt_init (&x509_cert); | ||
992 | gnutls_x509_crt_import (x509_cert, | ||
993 | chainp, | ||
994 | GNUTLS_X509_FMT_DER); | ||
995 | |||
996 | if (0 != (rc = gnutls_x509_crt_get_dn_by_oid (x509_cert, | ||
997 | GNUTLS_OID_X520_COMMON_NAME, | ||
998 | 0, /* the first and only one */ | ||
999 | 0 /* no DER encoding */, | ||
1000 | certdn, | ||
1001 | &size))) | ||
1002 | { | ||
1003 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1004 | _ ("Failed to fetch CN from cert: %s\n"), | ||
1005 | gnutls_strerror (rc)); | ||
1006 | gnutls_x509_crt_deinit (x509_cert); | ||
1007 | return GNUNET_SYSERR; | ||
1008 | } | ||
1009 | /* check for TLSA/DANE records */ | ||
1010 | #if HAVE_GNUTLS_DANE | ||
1011 | if (0 != s5r->num_danes) | ||
1012 | { | ||
1013 | dane_state_t dane_state; | ||
1014 | dane_query_t dane_query; | ||
1015 | unsigned int verify; | ||
1016 | |||
1017 | /* FIXME: add flags to gnutls to NOT read UNBOUND_ROOT_KEY_FILE here! */ | ||
1018 | if (0 != (rc = dane_state_init (&dane_state, | ||
1019 | #ifdef DANE_F_IGNORE_DNSSEC | ||
1020 | DANE_F_IGNORE_DNSSEC | | ||
1021 | #endif | ||
1022 | DANE_F_IGNORE_LOCAL_RESOLVER))) | ||
1023 | { | ||
1024 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1025 | _ ("Failed to initialize DANE: %s\n"), | ||
1026 | dane_strerror (rc)); | ||
1027 | gnutls_x509_crt_deinit (x509_cert); | ||
1028 | return GNUNET_SYSERR; | ||
1029 | } | ||
1030 | s5r->dane_data[s5r->num_danes] = NULL; | ||
1031 | s5r->dane_data_len[s5r->num_danes] = 0; | ||
1032 | if (0 != (rc = dane_raw_tlsa (dane_state, | ||
1033 | &dane_query, | ||
1034 | s5r->dane_data, | ||
1035 | s5r->dane_data_len, | ||
1036 | GNUNET_YES, | ||
1037 | GNUNET_NO))) | ||
1038 | { | ||
1039 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1040 | _ ("Failed to parse DANE record: %s\n"), | ||
1041 | dane_strerror (rc)); | ||
1042 | dane_state_deinit (dane_state); | ||
1043 | gnutls_x509_crt_deinit (x509_cert); | ||
1044 | return GNUNET_SYSERR; | ||
1045 | } | ||
1046 | if (0 != (rc = dane_verify_crt_raw (dane_state, | ||
1047 | chainp, | ||
1048 | cert_list_size, | ||
1049 | gnutls_certificate_type_get ( | ||
1050 | tlsinfo->internals), | ||
1051 | dane_query, | ||
1052 | 0, 0, | ||
1053 | &verify))) | ||
1054 | { | ||
1055 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1056 | _ ("Failed to verify TLS connection using DANE: %s\n"), | ||
1057 | dane_strerror (rc)); | ||
1058 | dane_query_deinit (dane_query); | ||
1059 | dane_state_deinit (dane_state); | ||
1060 | gnutls_x509_crt_deinit (x509_cert); | ||
1061 | return GNUNET_SYSERR; | ||
1062 | } | ||
1063 | if (0 != verify) | ||
1064 | { | ||
1065 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1066 | _ ( | ||
1067 | "Failed DANE verification failed with GnuTLS verify status code: %u\n"), | ||
1068 | verify); | ||
1069 | dane_query_deinit (dane_query); | ||
1070 | dane_state_deinit (dane_state); | ||
1071 | gnutls_x509_crt_deinit (x509_cert); | ||
1072 | return GNUNET_SYSERR; | ||
1073 | } | ||
1074 | dane_query_deinit (dane_query); | ||
1075 | dane_state_deinit (dane_state); | ||
1076 | /* success! */ | ||
1077 | } | ||
1078 | else | ||
1079 | #endif | ||
1080 | { | ||
1081 | /* try LEHO or ordinary domain name X509 verification */ | ||
1082 | name = s5r->domain; | ||
1083 | if (NULL != s5r->leho) | ||
1084 | name = s5r->leho; | ||
1085 | if (NULL != name) | ||
1086 | { | ||
1087 | if (0 == (rc = gnutls_x509_crt_check_hostname (x509_cert, | ||
1088 | name))) | ||
1089 | { | ||
1090 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1091 | _ ( | ||
1092 | "TLS certificate subject name (%s) does not match `%s': %d\n"), | ||
1093 | certdn, | ||
1094 | name, | ||
1095 | rc); | ||
1096 | gnutls_x509_crt_deinit (x509_cert); | ||
1097 | return GNUNET_SYSERR; | ||
1098 | } | ||
1099 | } | ||
1100 | else | ||
1101 | { | ||
1102 | /* we did not even have the domain name!? */ | ||
1103 | GNUNET_break (0); | ||
1104 | return GNUNET_SYSERR; | ||
1105 | } | ||
1106 | } | ||
1107 | gnutls_x509_crt_deinit (x509_cert); | ||
1108 | return GNUNET_OK; | ||
1109 | } | ||
1110 | |||
1111 | |||
1112 | /** | ||
1113 | * We're getting an HTTP response header from cURL. Convert it to the | ||
1114 | * MHD response headers. Mostly copies the headers, but makes special | ||
1115 | * adjustments to "Set-Cookie" and "Location" headers as those may need | ||
1116 | * to be changed from the LEHO to the domain the browser expects. | ||
1117 | * | ||
1118 | * @param buffer curl buffer with a single line of header data; not 0-terminated! | ||
1119 | * @param size curl blocksize | ||
1120 | * @param nmemb curl blocknumber | ||
1121 | * @param cls our `struct Socks5Request *` | ||
1122 | * @return size of processed bytes | ||
1123 | */ | ||
1124 | static size_t | ||
1125 | curl_check_hdr (void *buffer, | ||
1126 | size_t size, | ||
1127 | size_t nmemb, | ||
1128 | void *cls) | ||
1129 | { | ||
1130 | struct Socks5Request *s5r = cls; | ||
1131 | struct HttpResponseHeader *header; | ||
1132 | size_t bytes = size * nmemb; | ||
1133 | char *ndup; | ||
1134 | const char *hdr_type; | ||
1135 | const char *cookie_domain; | ||
1136 | char *hdr_val; | ||
1137 | char *new_cookie_hdr; | ||
1138 | char *new_location; | ||
1139 | size_t offset; | ||
1140 | size_t delta_cdomain; | ||
1141 | int domain_matched; | ||
1142 | char *tok; | ||
1143 | |||
1144 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1145 | "Receiving HTTP response header from CURL\n"); | ||
1146 | /* first, check TLS certificate */ | ||
1147 | if ((GNUNET_YES != s5r->ssl_checked) && | ||
1148 | (GNUNET_YES == s5r->is_tls)) | ||
1149 | // (HTTPS_PORT == s5r->port)) | ||
1150 | { | ||
1151 | if (GNUNET_OK != check_ssl_certificate (s5r)) | ||
1152 | return 0; | ||
1153 | } | ||
1154 | ndup = GNUNET_strndup (buffer, | ||
1155 | bytes); | ||
1156 | hdr_type = strtok (ndup, | ||
1157 | ":"); | ||
1158 | if (NULL == hdr_type) | ||
1159 | { | ||
1160 | GNUNET_free (ndup); | ||
1161 | return bytes; | ||
1162 | } | ||
1163 | hdr_val = strtok (NULL, | ||
1164 | ""); | ||
1165 | if (NULL == hdr_val) | ||
1166 | { | ||
1167 | GNUNET_free (ndup); | ||
1168 | return bytes; | ||
1169 | } | ||
1170 | if (' ' == *hdr_val) | ||
1171 | hdr_val++; | ||
1172 | |||
1173 | /* custom logic for certain header types */ | ||
1174 | new_cookie_hdr = NULL; | ||
1175 | if ((NULL != s5r->leho) && | ||
1176 | (0 == strcasecmp (hdr_type, | ||
1177 | MHD_HTTP_HEADER_SET_COOKIE))) | ||
1178 | |||
1179 | { | ||
1180 | new_cookie_hdr = GNUNET_malloc (strlen (hdr_val) | ||
1181 | + strlen (s5r->domain) + 1); | ||
1182 | offset = 0; | ||
1183 | domain_matched = GNUNET_NO; /* make sure we match domain at most once */ | ||
1184 | for (tok = strtok (hdr_val, ";"); NULL != tok; tok = strtok (NULL, ";")) | ||
1185 | { | ||
1186 | if ((0 == strncasecmp (tok, | ||
1187 | " domain", | ||
1188 | strlen (" domain"))) && | ||
1189 | (GNUNET_NO == domain_matched)) | ||
1190 | { | ||
1191 | domain_matched = GNUNET_YES; | ||
1192 | cookie_domain = tok + strlen (" domain") + 1; | ||
1193 | if (strlen (cookie_domain) < strlen (s5r->leho)) | ||
1194 | { | ||
1195 | delta_cdomain = strlen (s5r->leho) - strlen (cookie_domain); | ||
1196 | if (0 == strcasecmp (cookie_domain, | ||
1197 | s5r->leho + delta_cdomain)) | ||
1198 | { | ||
1199 | offset += sprintf (new_cookie_hdr + offset, | ||
1200 | " domain=%s;", | ||
1201 | s5r->domain); | ||
1202 | continue; | ||
1203 | } | ||
1204 | } | ||
1205 | else if (0 == strcmp (cookie_domain, | ||
1206 | s5r->leho)) | ||
1207 | { | ||
1208 | offset += sprintf (new_cookie_hdr + offset, | ||
1209 | " domain=%s;", | ||
1210 | s5r->domain); | ||
1211 | continue; | ||
1212 | } | ||
1213 | else if (('.' == cookie_domain[0]) && | ||
1214 | (0 == strcmp (&cookie_domain[1], | ||
1215 | s5r->leho))) | ||
1216 | { | ||
1217 | offset += sprintf (new_cookie_hdr + offset, | ||
1218 | " domain=.%s;", | ||
1219 | s5r->domain); | ||
1220 | continue; | ||
1221 | } | ||
1222 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1223 | _ ("Cookie domain `%s' supplied by server is invalid\n"), | ||
1224 | tok); | ||
1225 | } | ||
1226 | GNUNET_memcpy (new_cookie_hdr + offset, | ||
1227 | tok, | ||
1228 | strlen (tok)); | ||
1229 | offset += strlen (tok); | ||
1230 | new_cookie_hdr[offset++] = ';'; | ||
1231 | } | ||
1232 | hdr_val = new_cookie_hdr; | ||
1233 | } | ||
1234 | |||
1235 | new_location = NULL; | ||
1236 | if (0 == strcasecmp (MHD_HTTP_HEADER_TRANSFER_ENCODING, | ||
1237 | hdr_type)) | ||
1238 | { | ||
1239 | /* Ignore transfer encoding, set automatically by MHD if required */ | ||
1240 | goto cleanup; | ||
1241 | } | ||
1242 | if ((0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, | ||
1243 | hdr_type))) | ||
1244 | { | ||
1245 | char *leho_host; | ||
1246 | |||
1247 | GNUNET_asprintf (&leho_host, | ||
1248 | (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port) | ||
1249 | ? "http://%s" | ||
1250 | : "https://%s", | ||
1251 | s5r->leho); | ||
1252 | if (0 == strncmp (leho_host, | ||
1253 | hdr_val, | ||
1254 | strlen (leho_host))) | ||
1255 | { | ||
1256 | GNUNET_asprintf (&new_location, | ||
1257 | "%s%s%s", | ||
1258 | (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port) | ||
1259 | ? "http://" | ||
1260 | : "https://", | ||
1261 | s5r->domain, | ||
1262 | hdr_val + strlen (leho_host)); | ||
1263 | hdr_val = new_location; | ||
1264 | } | ||
1265 | GNUNET_free (leho_host); | ||
1266 | } | ||
1267 | else if (0 == strcasecmp (MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, | ||
1268 | hdr_type)) | ||
1269 | { | ||
1270 | char *leho_host; | ||
1271 | |||
1272 | GNUNET_asprintf (&leho_host, | ||
1273 | (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port) | ||
1274 | ? "http://%s" | ||
1275 | : "https://%s", | ||
1276 | s5r->leho); | ||
1277 | if (0 == strncmp (leho_host, | ||
1278 | hdr_val, | ||
1279 | strlen (leho_host))) | ||
1280 | { | ||
1281 | GNUNET_asprintf (&new_location, | ||
1282 | "%s%s", | ||
1283 | (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port) | ||
1284 | ? "http://" | ||
1285 | : "https://", | ||
1286 | s5r->domain); | ||
1287 | hdr_val = new_location; | ||
1288 | } | ||
1289 | GNUNET_free (leho_host); | ||
1290 | } | ||
1291 | |||
1292 | /* MHD does not allow certain characters in values, remove those */ | ||
1293 | if (NULL != (tok = strchr (hdr_val, '\n'))) | ||
1294 | *tok = '\0'; | ||
1295 | if (NULL != (tok = strchr (hdr_val, '\r'))) | ||
1296 | *tok = '\0'; | ||
1297 | if (NULL != (tok = strchr (hdr_val, '\t'))) | ||
1298 | *tok = '\0'; | ||
1299 | if (0 != strlen (hdr_val)) /* Rely in MHD to set those */ | ||
1300 | { | ||
1301 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1302 | "Adding header %s: %s to MHD response\n", | ||
1303 | hdr_type, | ||
1304 | hdr_val); | ||
1305 | header = GNUNET_new (struct HttpResponseHeader); | ||
1306 | header->type = GNUNET_strdup (hdr_type); | ||
1307 | header->value = GNUNET_strdup (hdr_val); | ||
1308 | GNUNET_CONTAINER_DLL_insert (s5r->header_head, | ||
1309 | s5r->header_tail, | ||
1310 | header); | ||
1311 | } | ||
1312 | cleanup: | ||
1313 | GNUNET_free (ndup); | ||
1314 | GNUNET_free (new_cookie_hdr); | ||
1315 | GNUNET_free (new_location); | ||
1316 | return bytes; | ||
1317 | } | ||
1318 | |||
1319 | |||
1320 | /** | ||
1321 | * Create an MHD response object in @a s5r matching the | ||
1322 | * information we got from curl. | ||
1323 | * | ||
1324 | * @param s5r the request for which we convert the response | ||
1325 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if response was | ||
1326 | * already initialized before | ||
1327 | */ | ||
1328 | static int | ||
1329 | create_mhd_response_from_s5r (struct Socks5Request *s5r) | ||
1330 | { | ||
1331 | long resp_code; | ||
1332 | double content_length; | ||
1333 | |||
1334 | if (NULL != s5r->response) | ||
1335 | { | ||
1336 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1337 | "Response already set!\n"); | ||
1338 | return GNUNET_SYSERR; | ||
1339 | } | ||
1340 | |||
1341 | GNUNET_break (CURLE_OK == | ||
1342 | curl_easy_getinfo (s5r->curl, | ||
1343 | CURLINFO_RESPONSE_CODE, | ||
1344 | &resp_code)); | ||
1345 | GNUNET_break (CURLE_OK == | ||
1346 | curl_easy_getinfo (s5r->curl, | ||
1347 | CURLINFO_CONTENT_LENGTH_DOWNLOAD, | ||
1348 | &content_length)); | ||
1349 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1350 | "Creating MHD response with code %d and size %d for %s%s\n", | ||
1351 | (int) resp_code, | ||
1352 | (int) content_length, | ||
1353 | s5r->domain, | ||
1354 | s5r->url); | ||
1355 | s5r->response_code = resp_code; | ||
1356 | s5r->response = MHD_create_response_from_callback ((-1 == content_length) | ||
1357 | ? MHD_SIZE_UNKNOWN | ||
1358 | : content_length, | ||
1359 | IO_BUFFERSIZE, | ||
1360 | &mhd_content_cb, | ||
1361 | s5r, | ||
1362 | NULL); | ||
1363 | for (struct HttpResponseHeader *header = s5r->header_head; | ||
1364 | NULL != header; | ||
1365 | header = header->next) | ||
1366 | { | ||
1367 | if (0 == strcasecmp (header->type, | ||
1368 | MHD_HTTP_HEADER_CONTENT_LENGTH)) | ||
1369 | continue; /* MHD won't let us mess with those, for good reason */ | ||
1370 | if ((0 == strcasecmp (header->type, | ||
1371 | MHD_HTTP_HEADER_TRANSFER_ENCODING)) && | ||
1372 | ((0 == strcasecmp (header->value, | ||
1373 | "identity")) || | ||
1374 | (0 == strcasecmp (header->value, | ||
1375 | "chunked")))) | ||
1376 | continue; /* MHD won't let us mess with those, for good reason */ | ||
1377 | if (MHD_YES != | ||
1378 | MHD_add_response_header (s5r->response, | ||
1379 | header->type, | ||
1380 | header->value)) | ||
1381 | { | ||
1382 | GNUNET_break (0); | ||
1383 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1384 | "Failed to add header `%s:%s'\n", | ||
1385 | header->type, | ||
1386 | header->value); | ||
1387 | } | ||
1388 | } | ||
1389 | /* force connection to be closed after each request, as we | ||
1390 | do not support HTTP pipelining (yet, FIXME!) */ | ||
1391 | /*GNUNET_break (MHD_YES == | ||
1392 | MHD_add_response_header (s5r->response, | ||
1393 | MHD_HTTP_HEADER_CONNECTION, | ||
1394 | "close"));*/ | ||
1395 | MHD_resume_connection (s5r->con); | ||
1396 | s5r->suspended = GNUNET_NO; | ||
1397 | return GNUNET_OK; | ||
1398 | } | ||
1399 | |||
1400 | |||
1401 | /** | ||
1402 | * Handle response payload data from cURL. Copies it into our `io_buf` to make | ||
1403 | * it available to MHD. | ||
1404 | * | ||
1405 | * @param ptr pointer to the data | ||
1406 | * @param size number of blocks of data | ||
1407 | * @param nmemb blocksize | ||
1408 | * @param ctx our `struct Socks5Request *` | ||
1409 | * @return number of bytes handled | ||
1410 | */ | ||
1411 | static size_t | ||
1412 | curl_download_cb (void *ptr, | ||
1413 | size_t size, | ||
1414 | size_t nmemb, | ||
1415 | void*ctx) | ||
1416 | { | ||
1417 | struct Socks5Request *s5r = ctx; | ||
1418 | size_t total = size * nmemb; | ||
1419 | |||
1420 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1421 | "Receiving %ux%u bytes for `%s%s' from cURL to download\n", | ||
1422 | (unsigned int) size, | ||
1423 | (unsigned int) nmemb, | ||
1424 | s5r->domain, | ||
1425 | s5r->url); | ||
1426 | if (NULL == s5r->response) | ||
1427 | GNUNET_assert (GNUNET_OK == | ||
1428 | create_mhd_response_from_s5r (s5r)); | ||
1429 | if ((SOCKS5_SOCKET_UPLOAD_DONE == s5r->state) && | ||
1430 | (0 == s5r->io_len)) | ||
1431 | { | ||
1432 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1433 | "Previous upload finished... starting DOWNLOAD.\n"); | ||
1434 | s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED; | ||
1435 | } | ||
1436 | if ((SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state) || | ||
1437 | (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state)) | ||
1438 | { | ||
1439 | /* we're still not done with the upload, do not yet | ||
1440 | start the download, the IO buffer is still full | ||
1441 | with upload data. */ | ||
1442 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1443 | "Pausing CURL download `%s%s', waiting for UPLOAD to finish\n", | ||
1444 | s5r->domain, | ||
1445 | s5r->url); | ||
1446 | s5r->curl_paused = GNUNET_YES; | ||
1447 | return CURL_WRITEFUNC_PAUSE; /* not yet ready for data download */ | ||
1448 | } | ||
1449 | if (sizeof(s5r->io_buf) - s5r->io_len < total) | ||
1450 | { | ||
1451 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1452 | "Pausing CURL `%s%s' download, not enough space %llu %llu %llu\n", | ||
1453 | s5r->domain, | ||
1454 | s5r->url, | ||
1455 | (unsigned long long) sizeof(s5r->io_buf), | ||
1456 | (unsigned long long) s5r->io_len, | ||
1457 | (unsigned long long) total); | ||
1458 | s5r->curl_paused = GNUNET_YES; | ||
1459 | return CURL_WRITEFUNC_PAUSE; /* not enough space */ | ||
1460 | } | ||
1461 | GNUNET_memcpy (&s5r->io_buf[s5r->io_len], | ||
1462 | ptr, | ||
1463 | total); | ||
1464 | s5r->io_len += total; | ||
1465 | if (GNUNET_YES == s5r->suspended) | ||
1466 | { | ||
1467 | MHD_resume_connection (s5r->con); | ||
1468 | s5r->suspended = GNUNET_NO; | ||
1469 | } | ||
1470 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1471 | "Received %llu bytes of payload via cURL from %s\n", | ||
1472 | (unsigned long long) total, | ||
1473 | s5r->domain); | ||
1474 | if (s5r->io_len == total) | ||
1475 | run_mhd_now (s5r->hd); | ||
1476 | return total; | ||
1477 | } | ||
1478 | |||
1479 | |||
1480 | /** | ||
1481 | * cURL callback for uploaded (PUT/POST) data. Copies it into our `io_buf` | ||
1482 | * to make it available to MHD. | ||
1483 | * | ||
1484 | * @param buf where to write the data | ||
1485 | * @param size number of bytes per member | ||
1486 | * @param nmemb number of members available in @a buf | ||
1487 | * @param cls our `struct Socks5Request` that generated the data | ||
1488 | * @return number of bytes copied to @a buf | ||
1489 | */ | ||
1490 | static size_t | ||
1491 | curl_upload_cb (void *buf, | ||
1492 | size_t size, | ||
1493 | size_t nmemb, | ||
1494 | void *cls) | ||
1495 | { | ||
1496 | struct Socks5Request *s5r = cls; | ||
1497 | size_t len = size * nmemb; | ||
1498 | size_t to_copy; | ||
1499 | |||
1500 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1501 | "Receiving %ux%u bytes for `%s%s' from cURL to upload\n", | ||
1502 | (unsigned int) size, | ||
1503 | (unsigned int) nmemb, | ||
1504 | s5r->domain, | ||
1505 | s5r->url); | ||
1506 | |||
1507 | if ((0 == s5r->io_len) && | ||
1508 | (SOCKS5_SOCKET_UPLOAD_DONE != s5r->state)) | ||
1509 | { | ||
1510 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1511 | "Pausing CURL UPLOAD %s%s, need more data\n", | ||
1512 | s5r->domain, | ||
1513 | s5r->url); | ||
1514 | return CURL_READFUNC_PAUSE; | ||
1515 | } | ||
1516 | if ((0 == s5r->io_len) && | ||
1517 | (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state)) | ||
1518 | { | ||
1519 | s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED; | ||
1520 | if (GNUNET_YES == s5r->curl_paused) | ||
1521 | { | ||
1522 | s5r->curl_paused = GNUNET_NO; | ||
1523 | curl_easy_pause (s5r->curl, | ||
1524 | CURLPAUSE_CONT); | ||
1525 | } | ||
1526 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1527 | "Completed CURL UPLOAD %s%s\n", | ||
1528 | s5r->domain, | ||
1529 | s5r->url); | ||
1530 | return 0; /* upload finished, can now download */ | ||
1531 | } | ||
1532 | if ((SOCKS5_SOCKET_UPLOAD_STARTED != s5r->state) && | ||
1533 | (SOCKS5_SOCKET_UPLOAD_DONE != s5r->state)) | ||
1534 | { | ||
1535 | GNUNET_break (0); | ||
1536 | return CURL_READFUNC_ABORT; | ||
1537 | } | ||
1538 | to_copy = GNUNET_MIN (s5r->io_len, | ||
1539 | len); | ||
1540 | GNUNET_memcpy (buf, | ||
1541 | s5r->io_buf, | ||
1542 | to_copy); | ||
1543 | memmove (s5r->io_buf, | ||
1544 | &s5r->io_buf[to_copy], | ||
1545 | s5r->io_len - to_copy); | ||
1546 | s5r->io_len -= to_copy; | ||
1547 | if (s5r->io_len + to_copy == sizeof(s5r->io_buf)) | ||
1548 | run_mhd_now (s5r->hd); /* got more space for upload now */ | ||
1549 | return to_copy; | ||
1550 | } | ||
1551 | |||
1552 | |||
1553 | /* ************************** main loop of cURL interaction ****************** */ | ||
1554 | |||
1555 | |||
1556 | /** | ||
1557 | * Task that is run when we are ready to receive more data | ||
1558 | * from curl | ||
1559 | * | ||
1560 | * @param cls closure | ||
1561 | */ | ||
1562 | static void | ||
1563 | curl_task_download (void *cls); | ||
1564 | |||
1565 | |||
1566 | /** | ||
1567 | * Ask cURL for the select() sets and schedule cURL operations. | ||
1568 | */ | ||
1569 | static void | ||
1570 | curl_download_prepare () | ||
1571 | { | ||
1572 | CURLMcode mret; | ||
1573 | fd_set rs; | ||
1574 | fd_set ws; | ||
1575 | fd_set es; | ||
1576 | int max; | ||
1577 | struct GNUNET_NETWORK_FDSet *grs; | ||
1578 | struct GNUNET_NETWORK_FDSet *gws; | ||
1579 | long to; | ||
1580 | struct GNUNET_TIME_Relative rtime; | ||
1581 | |||
1582 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1583 | "Scheduling CURL interaction\n"); | ||
1584 | if (NULL != curl_download_task) | ||
1585 | { | ||
1586 | GNUNET_SCHEDULER_cancel (curl_download_task); | ||
1587 | curl_download_task = NULL; | ||
1588 | } | ||
1589 | max = -1; | ||
1590 | FD_ZERO (&rs); | ||
1591 | FD_ZERO (&ws); | ||
1592 | FD_ZERO (&es); | ||
1593 | if (CURLM_OK != (mret = curl_multi_fdset (curl_multi, | ||
1594 | &rs, | ||
1595 | &ws, | ||
1596 | &es, | ||
1597 | &max))) | ||
1598 | { | ||
1599 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1600 | "%s failed at %s:%d: `%s'\n", | ||
1601 | "curl_multi_fdset", __FILE__, __LINE__, | ||
1602 | curl_multi_strerror (mret)); | ||
1603 | return; | ||
1604 | } | ||
1605 | to = -1; | ||
1606 | GNUNET_break (CURLM_OK == | ||
1607 | curl_multi_timeout (curl_multi, | ||
1608 | &to)); | ||
1609 | if (-1 == to) | ||
1610 | rtime = GNUNET_TIME_UNIT_FOREVER_REL; | ||
1611 | else | ||
1612 | rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, | ||
1613 | to); | ||
1614 | if (-1 != max) | ||
1615 | { | ||
1616 | grs = GNUNET_NETWORK_fdset_create (); | ||
1617 | gws = GNUNET_NETWORK_fdset_create (); | ||
1618 | GNUNET_NETWORK_fdset_copy_native (grs, | ||
1619 | &rs, | ||
1620 | max + 1); | ||
1621 | GNUNET_NETWORK_fdset_copy_native (gws, | ||
1622 | &ws, | ||
1623 | max + 1); | ||
1624 | curl_download_task = GNUNET_SCHEDULER_add_select ( | ||
1625 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
1626 | rtime, | ||
1627 | grs, | ||
1628 | gws, | ||
1629 | &curl_task_download, | ||
1630 | curl_multi); | ||
1631 | GNUNET_NETWORK_fdset_destroy (gws); | ||
1632 | GNUNET_NETWORK_fdset_destroy (grs); | ||
1633 | } | ||
1634 | else | ||
1635 | { | ||
1636 | curl_download_task = GNUNET_SCHEDULER_add_delayed (rtime, | ||
1637 | &curl_task_download, | ||
1638 | curl_multi); | ||
1639 | } | ||
1640 | } | ||
1641 | |||
1642 | |||
1643 | /** | ||
1644 | * Task that is run when we are ready to receive more data from curl. | ||
1645 | * | ||
1646 | * @param cls closure, NULL | ||
1647 | */ | ||
1648 | static void | ||
1649 | curl_task_download (void *cls) | ||
1650 | { | ||
1651 | int running; | ||
1652 | int msgnum; | ||
1653 | struct CURLMsg *msg; | ||
1654 | CURLMcode mret; | ||
1655 | struct Socks5Request *s5r; | ||
1656 | |||
1657 | curl_download_task = NULL; | ||
1658 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1659 | "Running CURL interaction\n"); | ||
1660 | do | ||
1661 | { | ||
1662 | running = 0; | ||
1663 | mret = curl_multi_perform (curl_multi, | ||
1664 | &running); | ||
1665 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1666 | "Checking CURL multi status: %d\n", | ||
1667 | mret); | ||
1668 | while (NULL != (msg = curl_multi_info_read (curl_multi, | ||
1669 | &msgnum))) | ||
1670 | { | ||
1671 | GNUNET_break (CURLE_OK == | ||
1672 | curl_easy_getinfo (msg->easy_handle, | ||
1673 | CURLINFO_PRIVATE, | ||
1674 | (char **) &s5r)); | ||
1675 | if (NULL == s5r) | ||
1676 | { | ||
1677 | GNUNET_break (0); | ||
1678 | continue; | ||
1679 | } | ||
1680 | switch (msg->msg) | ||
1681 | { | ||
1682 | case CURLMSG_NONE: | ||
1683 | /* documentation says this is not used */ | ||
1684 | GNUNET_break (0); | ||
1685 | break; | ||
1686 | |||
1687 | case CURLMSG_DONE: | ||
1688 | switch (msg->data.result) | ||
1689 | { | ||
1690 | case CURLE_OK: | ||
1691 | case CURLE_GOT_NOTHING: | ||
1692 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1693 | "CURL download %s%s completed.\n", | ||
1694 | s5r->domain, | ||
1695 | s5r->url); | ||
1696 | if (NULL == s5r->response) | ||
1697 | { | ||
1698 | GNUNET_assert (GNUNET_OK == | ||
1699 | create_mhd_response_from_s5r (s5r)); | ||
1700 | } | ||
1701 | s5r->state = SOCKS5_SOCKET_DOWNLOAD_DONE; | ||
1702 | if (GNUNET_YES == s5r->suspended) | ||
1703 | { | ||
1704 | MHD_resume_connection (s5r->con); | ||
1705 | s5r->suspended = GNUNET_NO; | ||
1706 | } | ||
1707 | run_mhd_now (s5r->hd); | ||
1708 | break; | ||
1709 | |||
1710 | default: | ||
1711 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1712 | "Download curl %s%s failed: %s\n", | ||
1713 | s5r->domain, | ||
1714 | s5r->url, | ||
1715 | curl_easy_strerror (msg->data.result)); | ||
1716 | /* FIXME: indicate error somehow? close MHD connection badly as well? */ | ||
1717 | s5r->state = SOCKS5_SOCKET_DOWNLOAD_DONE; | ||
1718 | if (GNUNET_YES == s5r->suspended) | ||
1719 | { | ||
1720 | MHD_resume_connection (s5r->con); | ||
1721 | s5r->suspended = GNUNET_NO; | ||
1722 | } | ||
1723 | run_mhd_now (s5r->hd); | ||
1724 | break; | ||
1725 | } | ||
1726 | if (NULL == s5r->response) | ||
1727 | s5r->response = curl_failure_response; | ||
1728 | break; | ||
1729 | |||
1730 | case CURLMSG_LAST: | ||
1731 | /* documentation says this is not used */ | ||
1732 | GNUNET_break (0); | ||
1733 | break; | ||
1734 | |||
1735 | default: | ||
1736 | /* unexpected status code */ | ||
1737 | GNUNET_break (0); | ||
1738 | break; | ||
1739 | } | ||
1740 | } | ||
1741 | ; | ||
1742 | } | ||
1743 | while (mret == CURLM_CALL_MULTI_PERFORM); | ||
1744 | if (CURLM_OK != mret) | ||
1745 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1746 | "%s failed at %s:%d: `%s'\n", | ||
1747 | "curl_multi_perform", __FILE__, __LINE__, | ||
1748 | curl_multi_strerror (mret)); | ||
1749 | if (0 == running) | ||
1750 | { | ||
1751 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1752 | "Suspending cURL multi loop, no more events pending\n"); | ||
1753 | if (NULL != curl_download_task) | ||
1754 | { | ||
1755 | GNUNET_SCHEDULER_cancel (curl_download_task); | ||
1756 | curl_download_task = NULL; | ||
1757 | } | ||
1758 | return; /* nothing more in progress */ | ||
1759 | } | ||
1760 | curl_download_prepare (); | ||
1761 | } | ||
1762 | |||
1763 | |||
1764 | /* ********************************* MHD response generation ******************* */ | ||
1765 | |||
1766 | |||
1767 | /** | ||
1768 | * Read HTTP request header field from the request. Copies the fields | ||
1769 | * over to the 'headers' that will be given to curl. However, 'Host' | ||
1770 | * is substituted with the LEHO if present. We also change the | ||
1771 | * 'Connection' header value to "close" as the proxy does not support | ||
1772 | * pipelining. | ||
1773 | * | ||
1774 | * @param cls our `struct Socks5Request` | ||
1775 | * @param kind value kind | ||
1776 | * @param key field key | ||
1777 | * @param value field value | ||
1778 | * @return #MHD_YES to continue to iterate | ||
1779 | */ | ||
1780 | static int | ||
1781 | con_val_iter (void *cls, | ||
1782 | enum MHD_ValueKind kind, | ||
1783 | const char *key, | ||
1784 | const char *value) | ||
1785 | { | ||
1786 | struct Socks5Request *s5r = cls; | ||
1787 | char *hdr; | ||
1788 | |||
1789 | if ((0 == strcasecmp (MHD_HTTP_HEADER_HOST, | ||
1790 | key)) && | ||
1791 | (NULL != s5r->leho)) | ||
1792 | value = s5r->leho; | ||
1793 | GNUNET_asprintf (&hdr, | ||
1794 | "%s: %s", | ||
1795 | key, | ||
1796 | value); | ||
1797 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1798 | "Adding HEADER `%s' to HTTP request\n", | ||
1799 | hdr); | ||
1800 | s5r->headers = curl_slist_append (s5r->headers, | ||
1801 | hdr); | ||
1802 | GNUNET_free (hdr); | ||
1803 | return MHD_YES; | ||
1804 | } | ||
1805 | |||
1806 | |||
1807 | /** | ||
1808 | * Main MHD callback for handling requests. | ||
1809 | * | ||
1810 | * @param cls unused | ||
1811 | * @param con MHD connection handle | ||
1812 | * @param url the url in the request | ||
1813 | * @param meth the HTTP method used ("GET", "PUT", etc.) | ||
1814 | * @param ver the HTTP version string ("HTTP/1.1" for version 1.1, etc.) | ||
1815 | * @param upload_data the data being uploaded (excluding HEADERS, | ||
1816 | * for a POST that fits into memory and that is encoded | ||
1817 | * with a supported encoding, the POST data will NOT be | ||
1818 | * given in upload_data and is instead available as | ||
1819 | * part of MHD_get_connection_values; very large POST | ||
1820 | * data *will* be made available incrementally in | ||
1821 | * upload_data) | ||
1822 | * @param upload_data_size set initially to the size of the | ||
1823 | * @a upload_data provided; the method must update this | ||
1824 | * value to the number of bytes NOT processed; | ||
1825 | * @param con_cls pointer to location where we store the `struct Request` | ||
1826 | * @return #MHD_YES if the connection was handled successfully, | ||
1827 | * #MHD_NO if the socket must be closed due to a serious | ||
1828 | * error while handling the request | ||
1829 | */ | ||
1830 | static MHD_RESULT | ||
1831 | create_response (void *cls, | ||
1832 | struct MHD_Connection *con, | ||
1833 | const char *url, | ||
1834 | const char *meth, | ||
1835 | const char *ver, | ||
1836 | const char *upload_data, | ||
1837 | size_t *upload_data_size, | ||
1838 | void **con_cls) | ||
1839 | { | ||
1840 | struct Socks5Request *s5r = *con_cls; | ||
1841 | char *curlurl; | ||
1842 | char ipstring[INET6_ADDRSTRLEN]; | ||
1843 | char ipaddr[INET6_ADDRSTRLEN + 2]; | ||
1844 | char *curl_hosts; | ||
1845 | const struct sockaddr *sa; | ||
1846 | const struct sockaddr_in *s4; | ||
1847 | const struct sockaddr_in6 *s6; | ||
1848 | uint16_t port; | ||
1849 | size_t left; | ||
1850 | |||
1851 | if (NULL == s5r) | ||
1852 | { | ||
1853 | GNUNET_break (0); | ||
1854 | return MHD_NO; | ||
1855 | } | ||
1856 | s5r->con = con; | ||
1857 | /* Fresh connection. */ | ||
1858 | if (SOCKS5_SOCKET_WITH_MHD == s5r->state) | ||
1859 | { | ||
1860 | /* first time here, initialize curl handle */ | ||
1861 | if (s5r->is_gns) | ||
1862 | { | ||
1863 | sa = (const struct sockaddr *) &s5r->destination_address; | ||
1864 | switch (sa->sa_family) | ||
1865 | { | ||
1866 | case AF_INET: | ||
1867 | s4 = (const struct sockaddr_in *) &s5r->destination_address; | ||
1868 | if (NULL == inet_ntop (AF_INET, | ||
1869 | &s4->sin_addr, | ||
1870 | ipstring, | ||
1871 | sizeof(ipstring))) | ||
1872 | { | ||
1873 | GNUNET_break (0); | ||
1874 | return MHD_NO; | ||
1875 | } | ||
1876 | GNUNET_snprintf (ipaddr, | ||
1877 | sizeof(ipaddr), | ||
1878 | "%s", | ||
1879 | ipstring); | ||
1880 | port = ntohs (s4->sin_port); | ||
1881 | break; | ||
1882 | |||
1883 | case AF_INET6: | ||
1884 | s6 = (const struct sockaddr_in6 *) &s5r->destination_address; | ||
1885 | if (NULL == inet_ntop (AF_INET6, | ||
1886 | &s6->sin6_addr, | ||
1887 | ipstring, | ||
1888 | sizeof(ipstring))) | ||
1889 | { | ||
1890 | GNUNET_break (0); | ||
1891 | return MHD_NO; | ||
1892 | } | ||
1893 | GNUNET_snprintf (ipaddr, | ||
1894 | sizeof(ipaddr), | ||
1895 | "%s", | ||
1896 | ipstring); | ||
1897 | port = ntohs (s6->sin6_port); | ||
1898 | break; | ||
1899 | |||
1900 | default: | ||
1901 | GNUNET_break (0); | ||
1902 | return MHD_NO; | ||
1903 | } | ||
1904 | GNUNET_asprintf (&curl_hosts, | ||
1905 | "%s:%d:%s", | ||
1906 | s5r->leho, | ||
1907 | port, | ||
1908 | ipaddr); | ||
1909 | s5r->hosts = curl_slist_append (NULL, | ||
1910 | curl_hosts); | ||
1911 | GNUNET_free (curl_hosts); | ||
1912 | } | ||
1913 | else | ||
1914 | { | ||
1915 | port = s5r->port; | ||
1916 | } | ||
1917 | if (NULL == s5r->curl) | ||
1918 | s5r->curl = curl_easy_init (); | ||
1919 | if (NULL == s5r->curl) | ||
1920 | return MHD_queue_response (con, | ||
1921 | MHD_HTTP_INTERNAL_SERVER_ERROR, | ||
1922 | curl_failure_response); | ||
1923 | curl_easy_setopt (s5r->curl, | ||
1924 | CURLOPT_HEADERFUNCTION, | ||
1925 | &curl_check_hdr); | ||
1926 | curl_easy_setopt (s5r->curl, | ||
1927 | CURLOPT_HEADERDATA, | ||
1928 | s5r); | ||
1929 | curl_easy_setopt (s5r->curl, | ||
1930 | CURLOPT_FOLLOWLOCATION, | ||
1931 | 0); | ||
1932 | if (s5r->is_gns) | ||
1933 | curl_easy_setopt (s5r->curl, | ||
1934 | CURLOPT_IPRESOLVE, | ||
1935 | CURL_IPRESOLVE_V4); | ||
1936 | curl_easy_setopt (s5r->curl, | ||
1937 | CURLOPT_CONNECTTIMEOUT, | ||
1938 | 600L); | ||
1939 | curl_easy_setopt (s5r->curl, | ||
1940 | CURLOPT_TIMEOUT, | ||
1941 | 600L); | ||
1942 | curl_easy_setopt (s5r->curl, | ||
1943 | CURLOPT_NOSIGNAL, | ||
1944 | 1L); | ||
1945 | curl_easy_setopt (s5r->curl, | ||
1946 | CURLOPT_HTTP_CONTENT_DECODING, | ||
1947 | 0); | ||
1948 | curl_easy_setopt (s5r->curl, | ||
1949 | CURLOPT_NOSIGNAL, | ||
1950 | 1L); | ||
1951 | curl_easy_setopt (s5r->curl, | ||
1952 | CURLOPT_PRIVATE, | ||
1953 | s5r); | ||
1954 | curl_easy_setopt (s5r->curl, | ||
1955 | CURLOPT_VERBOSE, | ||
1956 | 0L); | ||
1957 | /** | ||
1958 | * Pre-populate cache to resolve Hostname. | ||
1959 | * This is necessary as the DNS name in the CURLOPT_URL is used | ||
1960 | * for SNI http://de.wikipedia.org/wiki/Server_Name_Indication | ||
1961 | */ | ||
1962 | if ((NULL != s5r->leho) && | ||
1963 | (NULL != s5r->hosts)) | ||
1964 | { | ||
1965 | curl_easy_setopt (s5r->curl, | ||
1966 | CURLOPT_RESOLVE, | ||
1967 | s5r->hosts); | ||
1968 | } | ||
1969 | if (s5r->is_gns) | ||
1970 | { | ||
1971 | GNUNET_asprintf (&curlurl, | ||
1972 | (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port) | ||
1973 | ? "http://%s:%d%s" | ||
1974 | : "https://%s:%d%s", | ||
1975 | (NULL != s5r->leho) | ||
1976 | ? s5r->leho | ||
1977 | : ipaddr, | ||
1978 | port, | ||
1979 | s5r->url); | ||
1980 | } | ||
1981 | else | ||
1982 | { | ||
1983 | GNUNET_asprintf (&curlurl, | ||
1984 | (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port) | ||
1985 | ? "http://%s:%d%s" | ||
1986 | : "https://%s:%d%s", | ||
1987 | s5r->domain, | ||
1988 | port, | ||
1989 | s5r->url); | ||
1990 | } | ||
1991 | curl_easy_setopt (s5r->curl, | ||
1992 | CURLOPT_URL, | ||
1993 | curlurl); | ||
1994 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1995 | "Launching %s CURL interaction, fetching `%s'\n", | ||
1996 | (s5r->is_gns) ? "GNS" : "DNS", | ||
1997 | curlurl); | ||
1998 | GNUNET_free (curlurl); | ||
1999 | if (0 == strcasecmp (meth, | ||
2000 | MHD_HTTP_METHOD_PUT)) | ||
2001 | { | ||
2002 | s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED; | ||
2003 | curl_easy_setopt (s5r->curl, | ||
2004 | CURLOPT_UPLOAD, | ||
2005 | 1L); | ||
2006 | curl_easy_setopt (s5r->curl, | ||
2007 | CURLOPT_WRITEFUNCTION, | ||
2008 | &curl_download_cb); | ||
2009 | curl_easy_setopt (s5r->curl, | ||
2010 | CURLOPT_WRITEDATA, | ||
2011 | s5r); | ||
2012 | GNUNET_assert (CURLE_OK == | ||
2013 | curl_easy_setopt (s5r->curl, | ||
2014 | CURLOPT_READFUNCTION, | ||
2015 | &curl_upload_cb)); | ||
2016 | curl_easy_setopt (s5r->curl, | ||
2017 | CURLOPT_READDATA, | ||
2018 | s5r); | ||
2019 | { | ||
2020 | const char *us; | ||
2021 | long upload_size = 0; | ||
2022 | |||
2023 | us = MHD_lookup_connection_value (con, | ||
2024 | MHD_HEADER_KIND, | ||
2025 | MHD_HTTP_HEADER_CONTENT_LENGTH); | ||
2026 | if ((1 == sscanf (us, | ||
2027 | "%ld", | ||
2028 | &upload_size)) && | ||
2029 | (upload_size >= 0)) | ||
2030 | { | ||
2031 | curl_easy_setopt (s5r->curl, | ||
2032 | CURLOPT_INFILESIZE, | ||
2033 | upload_size); | ||
2034 | } | ||
2035 | } | ||
2036 | } | ||
2037 | else if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST)) | ||
2038 | { | ||
2039 | s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED; | ||
2040 | curl_easy_setopt (s5r->curl, | ||
2041 | CURLOPT_POST, | ||
2042 | 1L); | ||
2043 | curl_easy_setopt (s5r->curl, | ||
2044 | CURLOPT_WRITEFUNCTION, | ||
2045 | &curl_download_cb); | ||
2046 | curl_easy_setopt (s5r->curl, | ||
2047 | CURLOPT_WRITEDATA, | ||
2048 | s5r); | ||
2049 | curl_easy_setopt (s5r->curl, | ||
2050 | CURLOPT_READFUNCTION, | ||
2051 | &curl_upload_cb); | ||
2052 | curl_easy_setopt (s5r->curl, | ||
2053 | CURLOPT_READDATA, | ||
2054 | s5r); | ||
2055 | { | ||
2056 | const char *us; | ||
2057 | long upload_size; | ||
2058 | |||
2059 | upload_size = 0; | ||
2060 | us = MHD_lookup_connection_value (con, | ||
2061 | MHD_HEADER_KIND, | ||
2062 | MHD_HTTP_HEADER_CONTENT_LENGTH); | ||
2063 | if ((NULL != us) && | ||
2064 | (1 == sscanf (us, | ||
2065 | "%ld", | ||
2066 | &upload_size)) && | ||
2067 | (upload_size >= 0)) | ||
2068 | { | ||
2069 | curl_easy_setopt (s5r->curl, | ||
2070 | CURLOPT_INFILESIZE, | ||
2071 | upload_size); | ||
2072 | } | ||
2073 | else | ||
2074 | { | ||
2075 | curl_easy_setopt (s5r->curl, | ||
2076 | CURLOPT_INFILESIZE, | ||
2077 | upload_size); | ||
2078 | } | ||
2079 | } | ||
2080 | } | ||
2081 | else if (0 == strcasecmp (meth, | ||
2082 | MHD_HTTP_METHOD_HEAD)) | ||
2083 | { | ||
2084 | s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED; | ||
2085 | curl_easy_setopt (s5r->curl, | ||
2086 | CURLOPT_NOBODY, | ||
2087 | 1L); | ||
2088 | } | ||
2089 | else if (0 == strcasecmp (meth, | ||
2090 | MHD_HTTP_METHOD_OPTIONS)) | ||
2091 | { | ||
2092 | s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED; | ||
2093 | curl_easy_setopt (s5r->curl, | ||
2094 | CURLOPT_CUSTOMREQUEST, | ||
2095 | "OPTIONS"); | ||
2096 | curl_easy_setopt (s5r->curl, | ||
2097 | CURLOPT_WRITEFUNCTION, | ||
2098 | &curl_download_cb); | ||
2099 | curl_easy_setopt (s5r->curl, | ||
2100 | CURLOPT_WRITEDATA, | ||
2101 | s5r); | ||
2102 | } | ||
2103 | else if (0 == strcasecmp (meth, | ||
2104 | MHD_HTTP_METHOD_GET)) | ||
2105 | { | ||
2106 | s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED; | ||
2107 | curl_easy_setopt (s5r->curl, | ||
2108 | CURLOPT_HTTPGET, | ||
2109 | 1L); | ||
2110 | curl_easy_setopt (s5r->curl, | ||
2111 | CURLOPT_WRITEFUNCTION, | ||
2112 | &curl_download_cb); | ||
2113 | curl_easy_setopt (s5r->curl, | ||
2114 | CURLOPT_WRITEDATA, | ||
2115 | s5r); | ||
2116 | } | ||
2117 | else if (0 == strcasecmp (meth, | ||
2118 | MHD_HTTP_METHOD_DELETE)) | ||
2119 | { | ||
2120 | s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED; | ||
2121 | curl_easy_setopt (s5r->curl, | ||
2122 | CURLOPT_CUSTOMREQUEST, | ||
2123 | "DELETE"); | ||
2124 | curl_easy_setopt (s5r->curl, | ||
2125 | CURLOPT_WRITEFUNCTION, | ||
2126 | &curl_download_cb); | ||
2127 | curl_easy_setopt (s5r->curl, | ||
2128 | CURLOPT_WRITEDATA, | ||
2129 | s5r); | ||
2130 | } | ||
2131 | else | ||
2132 | { | ||
2133 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
2134 | _ ("Unsupported HTTP method `%s'\n"), | ||
2135 | meth); | ||
2136 | curl_easy_cleanup (s5r->curl); | ||
2137 | s5r->curl = NULL; | ||
2138 | return MHD_NO; | ||
2139 | } | ||
2140 | |||
2141 | if (0 == strcasecmp (ver, MHD_HTTP_VERSION_1_0)) | ||
2142 | { | ||
2143 | curl_easy_setopt (s5r->curl, | ||
2144 | CURLOPT_HTTP_VERSION, | ||
2145 | CURL_HTTP_VERSION_1_0); | ||
2146 | } | ||
2147 | else if (0 == strcasecmp (ver, MHD_HTTP_VERSION_1_1)) | ||
2148 | { | ||
2149 | curl_easy_setopt (s5r->curl, | ||
2150 | CURLOPT_HTTP_VERSION, | ||
2151 | CURL_HTTP_VERSION_1_1); | ||
2152 | } | ||
2153 | else | ||
2154 | { | ||
2155 | curl_easy_setopt (s5r->curl, | ||
2156 | CURLOPT_HTTP_VERSION, | ||
2157 | CURL_HTTP_VERSION_NONE); | ||
2158 | } | ||
2159 | |||
2160 | if (GNUNET_YES == s5r->is_tls) // (HTTPS_PORT == s5r->port) | ||
2161 | { | ||
2162 | curl_easy_setopt (s5r->curl, | ||
2163 | CURLOPT_USE_SSL, | ||
2164 | CURLUSESSL_ALL); | ||
2165 | if (0 < s5r->num_danes) | ||
2166 | curl_easy_setopt (s5r->curl, | ||
2167 | CURLOPT_SSL_VERIFYPEER, | ||
2168 | 0L); | ||
2169 | else | ||
2170 | curl_easy_setopt (s5r->curl, | ||
2171 | CURLOPT_SSL_VERIFYPEER, | ||
2172 | 1L); | ||
2173 | /* Disable cURL checking the hostname, as we will check ourselves | ||
2174 | as only we have the domain name or the LEHO or the DANE record */ | ||
2175 | curl_easy_setopt (s5r->curl, | ||
2176 | CURLOPT_SSL_VERIFYHOST, | ||
2177 | 0L); | ||
2178 | } | ||
2179 | else | ||
2180 | { | ||
2181 | curl_easy_setopt (s5r->curl, | ||
2182 | CURLOPT_USE_SSL, | ||
2183 | CURLUSESSL_NONE); | ||
2184 | } | ||
2185 | |||
2186 | if (CURLM_OK != | ||
2187 | curl_multi_add_handle (curl_multi, | ||
2188 | s5r->curl)) | ||
2189 | { | ||
2190 | GNUNET_break (0); | ||
2191 | curl_easy_cleanup (s5r->curl); | ||
2192 | s5r->curl = NULL; | ||
2193 | return MHD_NO; | ||
2194 | } | ||
2195 | MHD_get_connection_values (con, | ||
2196 | MHD_HEADER_KIND, | ||
2197 | (MHD_KeyValueIterator) & con_val_iter, | ||
2198 | s5r); | ||
2199 | curl_easy_setopt (s5r->curl, | ||
2200 | CURLOPT_HTTPHEADER, | ||
2201 | s5r->headers); | ||
2202 | curl_download_prepare (); | ||
2203 | return MHD_YES; | ||
2204 | } | ||
2205 | |||
2206 | /* continuing to process request */ | ||
2207 | if (0 != *upload_data_size) | ||
2208 | { | ||
2209 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2210 | "Processing %u bytes UPLOAD\n", | ||
2211 | (unsigned int) *upload_data_size); | ||
2212 | |||
2213 | /* FIXME: This must be set or a header with Transfer-Encoding: chunked. Else | ||
2214 | * upload callback is not called! | ||
2215 | */ | ||
2216 | curl_easy_setopt (s5r->curl, | ||
2217 | CURLOPT_POSTFIELDSIZE, | ||
2218 | *upload_data_size); | ||
2219 | |||
2220 | left = GNUNET_MIN (*upload_data_size, | ||
2221 | sizeof(s5r->io_buf) - s5r->io_len); | ||
2222 | GNUNET_memcpy (&s5r->io_buf[s5r->io_len], | ||
2223 | upload_data, | ||
2224 | left); | ||
2225 | s5r->io_len += left; | ||
2226 | *upload_data_size -= left; | ||
2227 | GNUNET_assert (NULL != s5r->curl); | ||
2228 | if (GNUNET_YES == s5r->curl_paused) | ||
2229 | { | ||
2230 | s5r->curl_paused = GNUNET_NO; | ||
2231 | curl_easy_pause (s5r->curl, | ||
2232 | CURLPAUSE_CONT); | ||
2233 | } | ||
2234 | return MHD_YES; | ||
2235 | } | ||
2236 | if (SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state) | ||
2237 | { | ||
2238 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2239 | "Finished processing UPLOAD\n"); | ||
2240 | s5r->state = SOCKS5_SOCKET_UPLOAD_DONE; | ||
2241 | } | ||
2242 | if (NULL == s5r->response) | ||
2243 | { | ||
2244 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2245 | "Waiting for HTTP response for %s%s...\n", | ||
2246 | s5r->domain, | ||
2247 | s5r->url); | ||
2248 | MHD_suspend_connection (con); | ||
2249 | s5r->suspended = GNUNET_YES; | ||
2250 | return MHD_YES; | ||
2251 | } | ||
2252 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2253 | "Queueing response for %s%s with MHD\n", | ||
2254 | s5r->domain, | ||
2255 | s5r->url); | ||
2256 | run_mhd_now (s5r->hd); | ||
2257 | return MHD_queue_response (con, | ||
2258 | s5r->response_code, | ||
2259 | s5r->response); | ||
2260 | } | ||
2261 | |||
2262 | |||
2263 | /* ******************** MHD HTTP setup and event loop ******************** */ | ||
2264 | |||
2265 | |||
2266 | /** | ||
2267 | * Function called when MHD decides that we are done with a request. | ||
2268 | * | ||
2269 | * @param cls NULL | ||
2270 | * @param connection connection handle | ||
2271 | * @param con_cls value as set by the last call to | ||
2272 | * the MHD_AccessHandlerCallback, should be our `struct Socks5Request *` | ||
2273 | * @param toe reason for request termination (ignored) | ||
2274 | */ | ||
2275 | static void | ||
2276 | mhd_completed_cb (void *cls, | ||
2277 | struct MHD_Connection *connection, | ||
2278 | void **con_cls, | ||
2279 | enum MHD_RequestTerminationCode toe) | ||
2280 | { | ||
2281 | struct Socks5Request *s5r = *con_cls; | ||
2282 | |||
2283 | if (NULL == s5r) | ||
2284 | return; | ||
2285 | if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) | ||
2286 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
2287 | "MHD encountered error handling request: %d\n", | ||
2288 | toe); | ||
2289 | if (NULL != s5r->curl) | ||
2290 | { | ||
2291 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2292 | "Removing cURL handle (MHD interaction complete)\n"); | ||
2293 | curl_multi_remove_handle (curl_multi, | ||
2294 | s5r->curl); | ||
2295 | curl_slist_free_all (s5r->headers); | ||
2296 | s5r->headers = NULL; | ||
2297 | curl_easy_reset (s5r->curl); | ||
2298 | s5r->rbuf_len = 0; | ||
2299 | s5r->wbuf_len = 0; | ||
2300 | s5r->io_len = 0; | ||
2301 | curl_download_prepare (); | ||
2302 | } | ||
2303 | if ((NULL != s5r->response) && | ||
2304 | (curl_failure_response != s5r->response)) | ||
2305 | MHD_destroy_response (s5r->response); | ||
2306 | for (struct HttpResponseHeader *header = s5r->header_head; | ||
2307 | NULL != header; | ||
2308 | header = s5r->header_head) | ||
2309 | { | ||
2310 | GNUNET_CONTAINER_DLL_remove (s5r->header_head, | ||
2311 | s5r->header_tail, | ||
2312 | header); | ||
2313 | GNUNET_free (header->type); | ||
2314 | GNUNET_free (header->value); | ||
2315 | GNUNET_free (header); | ||
2316 | } | ||
2317 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2318 | "Finished request for %s\n", | ||
2319 | s5r->url); | ||
2320 | GNUNET_free (s5r->url); | ||
2321 | s5r->state = SOCKS5_SOCKET_WITH_MHD; | ||
2322 | s5r->url = NULL; | ||
2323 | s5r->response = NULL; | ||
2324 | *con_cls = NULL; | ||
2325 | } | ||
2326 | |||
2327 | |||
2328 | /** | ||
2329 | * Function called when MHD connection is opened or closed. | ||
2330 | * | ||
2331 | * @param cls NULL | ||
2332 | * @param connection connection handle | ||
2333 | * @param con_cls value as set by the last call to | ||
2334 | * the MHD_AccessHandlerCallback, should be our `struct Socks5Request *` | ||
2335 | * @param toe connection notification type | ||
2336 | */ | ||
2337 | static void | ||
2338 | mhd_connection_cb (void *cls, | ||
2339 | struct MHD_Connection *connection, | ||
2340 | void **con_cls, | ||
2341 | enum MHD_ConnectionNotificationCode cnc) | ||
2342 | { | ||
2343 | struct Socks5Request *s5r; | ||
2344 | const union MHD_ConnectionInfo *ci; | ||
2345 | int sock; | ||
2346 | |||
2347 | switch (cnc) | ||
2348 | { | ||
2349 | case MHD_CONNECTION_NOTIFY_STARTED: | ||
2350 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection started...\n"); | ||
2351 | ci = MHD_get_connection_info (connection, | ||
2352 | MHD_CONNECTION_INFO_CONNECTION_FD); | ||
2353 | if (NULL == ci) | ||
2354 | { | ||
2355 | GNUNET_break (0); | ||
2356 | return; | ||
2357 | } | ||
2358 | sock = ci->connect_fd; | ||
2359 | for (s5r = s5r_head; NULL != s5r; s5r = s5r->next) | ||
2360 | { | ||
2361 | if (GNUNET_NETWORK_get_fd (s5r->sock) == sock) | ||
2362 | { | ||
2363 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2364 | "Context set...\n"); | ||
2365 | s5r->ssl_checked = GNUNET_NO; | ||
2366 | *con_cls = s5r; | ||
2367 | break; | ||
2368 | } | ||
2369 | } | ||
2370 | break; | ||
2371 | |||
2372 | case MHD_CONNECTION_NOTIFY_CLOSED: | ||
2373 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2374 | "Connection closed... cleaning up\n"); | ||
2375 | s5r = *con_cls; | ||
2376 | if (NULL == s5r) | ||
2377 | { | ||
2378 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2379 | "Connection stale!\n"); | ||
2380 | return; | ||
2381 | } | ||
2382 | cleanup_s5r (s5r); | ||
2383 | curl_download_prepare (); | ||
2384 | *con_cls = NULL; | ||
2385 | break; | ||
2386 | |||
2387 | default: | ||
2388 | GNUNET_break (0); | ||
2389 | } | ||
2390 | } | ||
2391 | |||
2392 | |||
2393 | /** | ||
2394 | * Function called when MHD first processes an incoming connection. | ||
2395 | * Gives us the respective URI information. | ||
2396 | * | ||
2397 | * We use this to associate the `struct MHD_Connection` with our | ||
2398 | * internal `struct Socks5Request` data structure (by checking | ||
2399 | * for matching sockets). | ||
2400 | * | ||
2401 | * @param cls the HTTP server handle (a `struct MhdHttpList`) | ||
2402 | * @param url the URL that is being requested | ||
2403 | * @param connection MHD connection object for the request | ||
2404 | * @return the `struct Socks5Request` that this @a connection is for | ||
2405 | */ | ||
2406 | static void * | ||
2407 | mhd_log_callback (void *cls, | ||
2408 | const char *url, | ||
2409 | struct MHD_Connection *connection) | ||
2410 | { | ||
2411 | struct Socks5Request *s5r; | ||
2412 | const union MHD_ConnectionInfo *ci; | ||
2413 | |||
2414 | ci = MHD_get_connection_info (connection, | ||
2415 | MHD_CONNECTION_INFO_SOCKET_CONTEXT); | ||
2416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing %s\n", url); | ||
2417 | if (NULL == ci) | ||
2418 | { | ||
2419 | GNUNET_break (0); | ||
2420 | return NULL; | ||
2421 | } | ||
2422 | s5r = ci->socket_context; | ||
2423 | if (NULL != s5r->url) | ||
2424 | { | ||
2425 | GNUNET_break (0); | ||
2426 | return NULL; | ||
2427 | } | ||
2428 | s5r->url = GNUNET_strdup (url); | ||
2429 | if (NULL != s5r->timeout_task) | ||
2430 | { | ||
2431 | GNUNET_SCHEDULER_cancel (s5r->timeout_task); | ||
2432 | s5r->timeout_task = NULL; | ||
2433 | } | ||
2434 | GNUNET_assert (s5r->state == SOCKS5_SOCKET_WITH_MHD); | ||
2435 | return s5r; | ||
2436 | } | ||
2437 | |||
2438 | |||
2439 | /** | ||
2440 | * Kill the given MHD daemon. | ||
2441 | * | ||
2442 | * @param hd daemon to stop | ||
2443 | */ | ||
2444 | static void | ||
2445 | kill_httpd (struct MhdHttpList *hd) | ||
2446 | { | ||
2447 | GNUNET_CONTAINER_DLL_remove (mhd_httpd_head, | ||
2448 | mhd_httpd_tail, | ||
2449 | hd); | ||
2450 | GNUNET_free (hd->domain); | ||
2451 | MHD_stop_daemon (hd->daemon); | ||
2452 | if (NULL != hd->httpd_task) | ||
2453 | { | ||
2454 | GNUNET_SCHEDULER_cancel (hd->httpd_task); | ||
2455 | hd->httpd_task = NULL; | ||
2456 | } | ||
2457 | GNUNET_free (hd->proxy_cert); | ||
2458 | if (hd == httpd) | ||
2459 | httpd = NULL; | ||
2460 | GNUNET_free (hd); | ||
2461 | } | ||
2462 | |||
2463 | |||
2464 | /** | ||
2465 | * Task run whenever HTTP server is idle for too long. Kill it. | ||
2466 | * | ||
2467 | * @param cls the `struct MhdHttpList *` | ||
2468 | */ | ||
2469 | static void | ||
2470 | kill_httpd_task (void *cls) | ||
2471 | { | ||
2472 | struct MhdHttpList *hd = cls; | ||
2473 | |||
2474 | hd->httpd_task = NULL; | ||
2475 | kill_httpd (hd); | ||
2476 | } | ||
2477 | |||
2478 | |||
2479 | /** | ||
2480 | * Task run whenever HTTP server operations are pending. | ||
2481 | * | ||
2482 | * @param cls the `struct MhdHttpList *` of the daemon that is being run | ||
2483 | */ | ||
2484 | static void | ||
2485 | do_httpd (void *cls); | ||
2486 | |||
2487 | |||
2488 | /** | ||
2489 | * Schedule MHD. This function should be called initially when an | ||
2490 | * MHD is first getting its client socket, and will then automatically | ||
2491 | * always be called later whenever there is work to be done. | ||
2492 | * | ||
2493 | * @param hd the daemon to schedule | ||
2494 | */ | ||
2495 | static void | ||
2496 | schedule_httpd (struct MhdHttpList *hd) | ||
2497 | { | ||
2498 | fd_set rs; | ||
2499 | fd_set ws; | ||
2500 | fd_set es; | ||
2501 | struct GNUNET_NETWORK_FDSet *wrs; | ||
2502 | struct GNUNET_NETWORK_FDSet *wws; | ||
2503 | int max; | ||
2504 | int haveto; | ||
2505 | MHD_UNSIGNED_LONG_LONG timeout; | ||
2506 | struct GNUNET_TIME_Relative tv; | ||
2507 | |||
2508 | FD_ZERO (&rs); | ||
2509 | FD_ZERO (&ws); | ||
2510 | FD_ZERO (&es); | ||
2511 | max = -1; | ||
2512 | if (MHD_YES != | ||
2513 | MHD_get_fdset (hd->daemon, | ||
2514 | &rs, | ||
2515 | &ws, | ||
2516 | &es, | ||
2517 | &max)) | ||
2518 | { | ||
2519 | kill_httpd (hd); | ||
2520 | return; | ||
2521 | } | ||
2522 | haveto = MHD_get_timeout (hd->daemon, | ||
2523 | &timeout); | ||
2524 | if (MHD_YES == haveto) | ||
2525 | tv.rel_value_us = (uint64_t) timeout * 1000LL; | ||
2526 | else | ||
2527 | tv = GNUNET_TIME_UNIT_FOREVER_REL; | ||
2528 | if (-1 != max) | ||
2529 | { | ||
2530 | wrs = GNUNET_NETWORK_fdset_create (); | ||
2531 | wws = GNUNET_NETWORK_fdset_create (); | ||
2532 | GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1); | ||
2533 | GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1); | ||
2534 | } | ||
2535 | else | ||
2536 | { | ||
2537 | wrs = NULL; | ||
2538 | wws = NULL; | ||
2539 | } | ||
2540 | if (NULL != hd->httpd_task) | ||
2541 | { | ||
2542 | GNUNET_SCHEDULER_cancel (hd->httpd_task); | ||
2543 | hd->httpd_task = NULL; | ||
2544 | } | ||
2545 | if ((MHD_YES != haveto) && | ||
2546 | (-1 == max) && | ||
2547 | (hd != httpd)) | ||
2548 | { | ||
2549 | /* daemon is idle, kill after timeout */ | ||
2550 | hd->httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT, | ||
2551 | &kill_httpd_task, | ||
2552 | hd); | ||
2553 | } | ||
2554 | else | ||
2555 | { | ||
2556 | hd->httpd_task = | ||
2557 | GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
2558 | tv, wrs, wws, | ||
2559 | &do_httpd, hd); | ||
2560 | } | ||
2561 | if (NULL != wrs) | ||
2562 | GNUNET_NETWORK_fdset_destroy (wrs); | ||
2563 | if (NULL != wws) | ||
2564 | GNUNET_NETWORK_fdset_destroy (wws); | ||
2565 | } | ||
2566 | |||
2567 | |||
2568 | /** | ||
2569 | * Task run whenever HTTP server operations are pending. | ||
2570 | * | ||
2571 | * @param cls the `struct MhdHttpList` of the daemon that is being run | ||
2572 | */ | ||
2573 | static void | ||
2574 | do_httpd (void *cls) | ||
2575 | { | ||
2576 | struct MhdHttpList *hd = cls; | ||
2577 | |||
2578 | hd->httpd_task = NULL; | ||
2579 | MHD_run (hd->daemon); | ||
2580 | schedule_httpd (hd); | ||
2581 | } | ||
2582 | |||
2583 | |||
2584 | /** | ||
2585 | * Run MHD now, we have extra data ready for the callback. | ||
2586 | * | ||
2587 | * @param hd the daemon to run now. | ||
2588 | */ | ||
2589 | static void | ||
2590 | run_mhd_now (struct MhdHttpList *hd) | ||
2591 | { | ||
2592 | if (NULL != hd->httpd_task) | ||
2593 | GNUNET_SCHEDULER_cancel (hd->httpd_task); | ||
2594 | hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, | ||
2595 | hd); | ||
2596 | } | ||
2597 | |||
2598 | |||
2599 | /** | ||
2600 | * Read file in filename | ||
2601 | * | ||
2602 | * @param filename file to read | ||
2603 | * @param size pointer where filesize is stored | ||
2604 | * @return NULL on error | ||
2605 | */ | ||
2606 | static void* | ||
2607 | load_file (const char*filename, | ||
2608 | unsigned int*size) | ||
2609 | { | ||
2610 | void *buffer; | ||
2611 | uint64_t fsize; | ||
2612 | |||
2613 | if (GNUNET_OK != | ||
2614 | GNUNET_DISK_file_size (filename, | ||
2615 | &fsize, | ||
2616 | GNUNET_YES, | ||
2617 | GNUNET_YES)) | ||
2618 | return NULL; | ||
2619 | if (fsize > MAX_PEM_SIZE) | ||
2620 | return NULL; | ||
2621 | *size = (unsigned int) fsize; | ||
2622 | buffer = GNUNET_malloc (*size); | ||
2623 | if (fsize != | ||
2624 | GNUNET_DISK_fn_read (filename, | ||
2625 | buffer, | ||
2626 | (size_t) fsize)) | ||
2627 | { | ||
2628 | GNUNET_free (buffer); | ||
2629 | return NULL; | ||
2630 | } | ||
2631 | return buffer; | ||
2632 | } | ||
2633 | |||
2634 | |||
2635 | /** | ||
2636 | * Load PEM key from file | ||
2637 | * | ||
2638 | * @param key where to store the data | ||
2639 | * @param keyfile path to the PEM file | ||
2640 | * @return #GNUNET_OK on success | ||
2641 | */ | ||
2642 | static int | ||
2643 | load_key_from_file (gnutls_x509_privkey_t key, | ||
2644 | const char*keyfile) | ||
2645 | { | ||
2646 | gnutls_datum_t key_data; | ||
2647 | int ret; | ||
2648 | |||
2649 | key_data.data = load_file (keyfile, | ||
2650 | &key_data.size); | ||
2651 | if (NULL == key_data.data) | ||
2652 | return GNUNET_SYSERR; | ||
2653 | ret = gnutls_x509_privkey_import (key, &key_data, | ||
2654 | GNUTLS_X509_FMT_PEM); | ||
2655 | if (GNUTLS_E_SUCCESS != ret) | ||
2656 | { | ||
2657 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2658 | _ ("Unable to import private key from file `%s'\n"), | ||
2659 | keyfile); | ||
2660 | } | ||
2661 | GNUNET_free (key_data.data); | ||
2662 | return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK; | ||
2663 | } | ||
2664 | |||
2665 | |||
2666 | /** | ||
2667 | * Load cert from file | ||
2668 | * | ||
2669 | * @param crt struct to store data in | ||
2670 | * @param certfile path to pem file | ||
2671 | * @return #GNUNET_OK on success | ||
2672 | */ | ||
2673 | static int | ||
2674 | load_cert_from_file (gnutls_x509_crt_t crt, | ||
2675 | const char*certfile) | ||
2676 | { | ||
2677 | gnutls_datum_t cert_data; | ||
2678 | int ret; | ||
2679 | |||
2680 | cert_data.data = load_file (certfile, | ||
2681 | &cert_data.size); | ||
2682 | if (NULL == cert_data.data) | ||
2683 | return GNUNET_SYSERR; | ||
2684 | ret = gnutls_x509_crt_import (crt, | ||
2685 | &cert_data, | ||
2686 | GNUTLS_X509_FMT_PEM); | ||
2687 | if (GNUTLS_E_SUCCESS != ret) | ||
2688 | { | ||
2689 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2690 | _ ("Unable to import certificate from `%s'\n"), | ||
2691 | certfile); | ||
2692 | } | ||
2693 | GNUNET_free (cert_data.data); | ||
2694 | return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK; | ||
2695 | } | ||
2696 | |||
2697 | |||
2698 | /** | ||
2699 | * Generate new certificate for specific name | ||
2700 | * | ||
2701 | * @param name the subject name to generate a cert for | ||
2702 | * @return a struct holding the PEM data, NULL on error | ||
2703 | */ | ||
2704 | static struct ProxyGNSCertificate * | ||
2705 | generate_gns_certificate (const char *name) | ||
2706 | { | ||
2707 | unsigned int serial; | ||
2708 | size_t key_buf_size; | ||
2709 | size_t cert_buf_size; | ||
2710 | gnutls_x509_crt_t request; | ||
2711 | time_t etime; | ||
2712 | struct tm *tm_data; | ||
2713 | struct ProxyGNSCertificate *pgc; | ||
2714 | |||
2715 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2716 | "Generating x.509 certificate for `%s'\n", | ||
2717 | name); | ||
2718 | GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_init (&request)); | ||
2719 | GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_key (request, | ||
2720 | proxy_ca.key)); | ||
2721 | pgc = GNUNET_new (struct ProxyGNSCertificate); | ||
2722 | gnutls_x509_crt_set_dn_by_oid (request, | ||
2723 | GNUTLS_OID_X520_COUNTRY_NAME, | ||
2724 | 0, | ||
2725 | "ZZ", | ||
2726 | strlen ("ZZ")); | ||
2727 | gnutls_x509_crt_set_dn_by_oid (request, | ||
2728 | GNUTLS_OID_X520_ORGANIZATION_NAME, | ||
2729 | 0, | ||
2730 | "GNU Name System", | ||
2731 | strlen ("GNU Name System")); | ||
2732 | gnutls_x509_crt_set_dn_by_oid (request, | ||
2733 | GNUTLS_OID_X520_COMMON_NAME, | ||
2734 | 0, | ||
2735 | name, | ||
2736 | strlen (name)); | ||
2737 | gnutls_x509_crt_set_subject_alternative_name (request, | ||
2738 | GNUTLS_SAN_DNSNAME, | ||
2739 | name); | ||
2740 | GNUNET_break (GNUTLS_E_SUCCESS == | ||
2741 | gnutls_x509_crt_set_version (request, | ||
2742 | 3)); | ||
2743 | gnutls_rnd (GNUTLS_RND_NONCE, | ||
2744 | &serial, | ||
2745 | sizeof(serial)); | ||
2746 | gnutls_x509_crt_set_serial (request, | ||
2747 | &serial, | ||
2748 | sizeof(serial)); | ||
2749 | etime = time (NULL); | ||
2750 | tm_data = localtime (&etime); | ||
2751 | tm_data->tm_hour--; | ||
2752 | etime = mktime (tm_data); | ||
2753 | gnutls_x509_crt_set_activation_time (request, | ||
2754 | etime); | ||
2755 | tm_data->tm_year++; | ||
2756 | etime = mktime (tm_data); | ||
2757 | gnutls_x509_crt_set_expiration_time (request, | ||
2758 | etime); | ||
2759 | gnutls_x509_crt_sign2 (request, | ||
2760 | proxy_ca.cert, | ||
2761 | proxy_ca.key, | ||
2762 | GNUTLS_DIG_SHA512, | ||
2763 | 0); | ||
2764 | key_buf_size = sizeof(pgc->key); | ||
2765 | cert_buf_size = sizeof(pgc->cert); | ||
2766 | gnutls_x509_crt_export (request, | ||
2767 | GNUTLS_X509_FMT_PEM, | ||
2768 | pgc->cert, | ||
2769 | &cert_buf_size); | ||
2770 | gnutls_x509_privkey_export (proxy_ca.key, | ||
2771 | GNUTLS_X509_FMT_PEM, | ||
2772 | pgc->key, | ||
2773 | &key_buf_size); | ||
2774 | gnutls_x509_crt_deinit (request); | ||
2775 | return pgc; | ||
2776 | } | ||
2777 | |||
2778 | |||
2779 | /** | ||
2780 | * Function called by MHD with errors, suppresses them all. | ||
2781 | * | ||
2782 | * @param cls closure | ||
2783 | * @param fm format string (`printf()`-style) | ||
2784 | * @param ap arguments to @a fm | ||
2785 | */ | ||
2786 | static void | ||
2787 | mhd_error_log_callback (void *cls, | ||
2788 | const char *fm, | ||
2789 | va_list ap) | ||
2790 | { | ||
2791 | /* do nothing */ | ||
2792 | } | ||
2793 | |||
2794 | |||
2795 | /** | ||
2796 | * Lookup (or create) an TLS MHD instance for a particular domain. | ||
2797 | * | ||
2798 | * @param domain the domain the TLS daemon has to serve | ||
2799 | * @return NULL on error | ||
2800 | */ | ||
2801 | static struct MhdHttpList * | ||
2802 | lookup_ssl_httpd (const char*domain) | ||
2803 | { | ||
2804 | struct MhdHttpList *hd; | ||
2805 | struct ProxyGNSCertificate *pgc; | ||
2806 | |||
2807 | if (NULL == domain) | ||
2808 | { | ||
2809 | GNUNET_break (0); | ||
2810 | return NULL; | ||
2811 | } | ||
2812 | for (hd = mhd_httpd_head; NULL != hd; hd = hd->next) | ||
2813 | if ((NULL != hd->domain) && | ||
2814 | (0 == strcmp (hd->domain, domain))) | ||
2815 | return hd; | ||
2816 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2817 | "Starting fresh MHD HTTPS instance for domain `%s'\n", | ||
2818 | domain); | ||
2819 | pgc = generate_gns_certificate (domain); | ||
2820 | hd = GNUNET_new (struct MhdHttpList); | ||
2821 | hd->is_ssl = GNUNET_YES; | ||
2822 | hd->domain = GNUNET_strdup (domain); | ||
2823 | hd->proxy_cert = pgc; | ||
2824 | hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL | ||
2825 | | MHD_USE_NO_LISTEN_SOCKET | ||
2826 | | MHD_ALLOW_SUSPEND_RESUME, | ||
2827 | 0, | ||
2828 | NULL, NULL, | ||
2829 | &create_response, hd, | ||
2830 | MHD_OPTION_CONNECTION_TIMEOUT, (unsigned | ||
2831 | int) 16, | ||
2832 | MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, | ||
2833 | NULL, | ||
2834 | MHD_OPTION_NOTIFY_CONNECTION, | ||
2835 | &mhd_connection_cb, NULL, | ||
2836 | MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, | ||
2837 | NULL, | ||
2838 | MHD_OPTION_EXTERNAL_LOGGER, | ||
2839 | &mhd_error_log_callback, NULL, | ||
2840 | MHD_OPTION_HTTPS_MEM_KEY, pgc->key, | ||
2841 | MHD_OPTION_HTTPS_MEM_CERT, pgc->cert, | ||
2842 | MHD_OPTION_END); | ||
2843 | if (NULL == hd->daemon) | ||
2844 | { | ||
2845 | GNUNET_free (pgc); | ||
2846 | GNUNET_free (hd); | ||
2847 | return NULL; | ||
2848 | } | ||
2849 | GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, | ||
2850 | mhd_httpd_tail, | ||
2851 | hd); | ||
2852 | return hd; | ||
2853 | } | ||
2854 | |||
2855 | |||
2856 | /** | ||
2857 | * Task run when a Socks5Request somehow fails to be associated with | ||
2858 | * an MHD connection (e.g. because the client never speaks HTTP after | ||
2859 | * the SOCKS5 handshake). Clean up. | ||
2860 | * | ||
2861 | * @param cls the `struct Socks5Request *` | ||
2862 | */ | ||
2863 | static void | ||
2864 | timeout_s5r_handshake (void *cls) | ||
2865 | { | ||
2866 | struct Socks5Request *s5r = cls; | ||
2867 | |||
2868 | s5r->timeout_task = NULL; | ||
2869 | cleanup_s5r (s5r); | ||
2870 | } | ||
2871 | |||
2872 | |||
2873 | /** | ||
2874 | * We're done with the Socks5 protocol, now we need to pass the | ||
2875 | * connection data through to the final destination, either | ||
2876 | * direct (if the protocol might not be HTTP), or via MHD | ||
2877 | * (if the port looks like it should be HTTP). | ||
2878 | * | ||
2879 | * @param s5r socks request that has reached the final stage | ||
2880 | */ | ||
2881 | static void | ||
2882 | setup_data_transfer (struct Socks5Request *s5r) | ||
2883 | { | ||
2884 | struct MhdHttpList *hd; | ||
2885 | int fd; | ||
2886 | const struct sockaddr *addr; | ||
2887 | socklen_t len; | ||
2888 | char *domain; | ||
2889 | |||
2890 | if (GNUNET_YES == s5r->is_tls) | ||
2891 | { | ||
2892 | GNUNET_asprintf (&domain, | ||
2893 | "%s", | ||
2894 | s5r->domain); | ||
2895 | hd = lookup_ssl_httpd (domain); | ||
2896 | if (NULL == hd) | ||
2897 | { | ||
2898 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2899 | _ ("Failed to start HTTPS server for `%s'\n"), | ||
2900 | s5r->domain); | ||
2901 | cleanup_s5r (s5r); | ||
2902 | GNUNET_free (domain); | ||
2903 | return; | ||
2904 | } | ||
2905 | } | ||
2906 | else | ||
2907 | { | ||
2908 | domain = NULL; | ||
2909 | GNUNET_assert (NULL != httpd); | ||
2910 | hd = httpd; | ||
2911 | } | ||
2912 | fd = GNUNET_NETWORK_get_fd (s5r->sock); | ||
2913 | addr = GNUNET_NETWORK_get_addr (s5r->sock); | ||
2914 | len = GNUNET_NETWORK_get_addrlen (s5r->sock); | ||
2915 | s5r->state = SOCKS5_SOCKET_WITH_MHD; | ||
2916 | if (MHD_YES != | ||
2917 | MHD_add_connection (hd->daemon, | ||
2918 | fd, | ||
2919 | addr, | ||
2920 | len)) | ||
2921 | { | ||
2922 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
2923 | _ ("Failed to pass client to MHD\n")); | ||
2924 | cleanup_s5r (s5r); | ||
2925 | GNUNET_free (domain); | ||
2926 | return; | ||
2927 | } | ||
2928 | s5r->hd = hd; | ||
2929 | schedule_httpd (hd); | ||
2930 | s5r->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_HANDSHAKE_TIMEOUT, | ||
2931 | &timeout_s5r_handshake, | ||
2932 | s5r); | ||
2933 | GNUNET_free (domain); | ||
2934 | } | ||
2935 | |||
2936 | |||
2937 | /* ********************* SOCKS handling ************************* */ | ||
2938 | |||
2939 | |||
2940 | /** | ||
2941 | * Write data from buffer to socks5 client, then continue with state machine. | ||
2942 | * | ||
2943 | * @param cls the closure with the `struct Socks5Request` | ||
2944 | */ | ||
2945 | static void | ||
2946 | do_write (void *cls) | ||
2947 | { | ||
2948 | struct Socks5Request *s5r = cls; | ||
2949 | ssize_t len; | ||
2950 | |||
2951 | s5r->wtask = NULL; | ||
2952 | len = GNUNET_NETWORK_socket_send (s5r->sock, | ||
2953 | s5r->wbuf, | ||
2954 | s5r->wbuf_len); | ||
2955 | if (len <= 0) | ||
2956 | { | ||
2957 | /* write error: connection closed, shutdown, etc.; just clean up */ | ||
2958 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2959 | "Write Error\n"); | ||
2960 | cleanup_s5r (s5r); | ||
2961 | return; | ||
2962 | } | ||
2963 | memmove (s5r->wbuf, | ||
2964 | &s5r->wbuf[len], | ||
2965 | s5r->wbuf_len - len); | ||
2966 | s5r->wbuf_len -= len; | ||
2967 | if (s5r->wbuf_len > 0) | ||
2968 | { | ||
2969 | /* not done writing */ | ||
2970 | s5r->wtask = | ||
2971 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
2972 | s5r->sock, | ||
2973 | &do_write, s5r); | ||
2974 | return; | ||
2975 | } | ||
2976 | |||
2977 | /* we're done writing, continue with state machine! */ | ||
2978 | |||
2979 | switch (s5r->state) | ||
2980 | { | ||
2981 | case SOCKS5_INIT: | ||
2982 | GNUNET_assert (0); | ||
2983 | break; | ||
2984 | |||
2985 | case SOCKS5_REQUEST: | ||
2986 | GNUNET_assert (NULL != s5r->rtask); | ||
2987 | break; | ||
2988 | |||
2989 | case SOCKS5_DATA_TRANSFER: | ||
2990 | setup_data_transfer (s5r); | ||
2991 | return; | ||
2992 | |||
2993 | case SOCKS5_WRITE_THEN_CLEANUP: | ||
2994 | cleanup_s5r (s5r); | ||
2995 | return; | ||
2996 | |||
2997 | default: | ||
2998 | GNUNET_break (0); | ||
2999 | break; | ||
3000 | } | ||
3001 | } | ||
3002 | |||
3003 | |||
3004 | /** | ||
3005 | * Return a server response message indicating a failure to the client. | ||
3006 | * | ||
3007 | * @param s5r request to return failure code for | ||
3008 | * @param sc status code to return | ||
3009 | */ | ||
3010 | static void | ||
3011 | signal_socks_failure (struct Socks5Request *s5r, | ||
3012 | enum Socks5StatusCode sc) | ||
3013 | { | ||
3014 | struct Socks5ServerResponseMessage *s_resp; | ||
3015 | |||
3016 | GNUNET_break (0 == s5r->wbuf_len); /* Should happen first in any transmission, right? */ | ||
3017 | GNUNET_assert (SOCKS_BUFFERSIZE - s5r->wbuf_len >= | ||
3018 | sizeof(struct Socks5ServerResponseMessage)); | ||
3019 | s_resp = (struct Socks5ServerResponseMessage *) &s5r->wbuf[s5r->wbuf_len]; | ||
3020 | memset (s_resp, 0, sizeof(struct Socks5ServerResponseMessage)); | ||
3021 | s_resp->version = SOCKS_VERSION_5; | ||
3022 | s_resp->reply = sc; | ||
3023 | s5r->state = SOCKS5_WRITE_THEN_CLEANUP; | ||
3024 | if (NULL != s5r->wtask) | ||
3025 | s5r->wtask = | ||
3026 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3027 | s5r->sock, | ||
3028 | &do_write, s5r); | ||
3029 | } | ||
3030 | |||
3031 | |||
3032 | /** | ||
3033 | * Return a server response message indicating success. | ||
3034 | * | ||
3035 | * @param s5r request to return success status message for | ||
3036 | */ | ||
3037 | static void | ||
3038 | signal_socks_success (struct Socks5Request *s5r) | ||
3039 | { | ||
3040 | struct Socks5ServerResponseMessage *s_resp; | ||
3041 | |||
3042 | s_resp = (struct Socks5ServerResponseMessage *) &s5r->wbuf[s5r->wbuf_len]; | ||
3043 | s_resp->version = SOCKS_VERSION_5; | ||
3044 | s_resp->reply = SOCKS5_STATUS_REQUEST_GRANTED; | ||
3045 | s_resp->reserved = 0; | ||
3046 | s_resp->addr_type = SOCKS5_AT_IPV4; | ||
3047 | /* zero out IPv4 address and port */ | ||
3048 | memset (&s_resp[1], | ||
3049 | 0, | ||
3050 | sizeof(struct in_addr) + sizeof(uint16_t)); | ||
3051 | s5r->wbuf_len += sizeof(struct Socks5ServerResponseMessage) | ||
3052 | + sizeof(struct in_addr) + sizeof(uint16_t); | ||
3053 | if (NULL == s5r->wtask) | ||
3054 | s5r->wtask = | ||
3055 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3056 | s5r->sock, | ||
3057 | &do_write, s5r); | ||
3058 | } | ||
3059 | |||
3060 | |||
3061 | /** | ||
3062 | * Process GNS results for target domain. | ||
3063 | * | ||
3064 | * @param cls the `struct Socks5Request *` | ||
3065 | * @param tld #GNUNET_YES if this was a GNS TLD. | ||
3066 | * @param rd_count number of records returned | ||
3067 | * @param rd record data | ||
3068 | */ | ||
3069 | static void | ||
3070 | handle_gns_result (void *cls, | ||
3071 | int tld, | ||
3072 | uint32_t rd_count, | ||
3073 | const struct GNUNET_GNSRECORD_Data *rd) | ||
3074 | { | ||
3075 | struct Socks5Request *s5r = cls; | ||
3076 | const struct GNUNET_GNSRECORD_Data *r; | ||
3077 | int got_ip; | ||
3078 | |||
3079 | s5r->gns_lookup = NULL; | ||
3080 | s5r->is_gns = tld; | ||
3081 | got_ip = GNUNET_NO; | ||
3082 | for (uint32_t i = 0; i < rd_count; i++) | ||
3083 | { | ||
3084 | r = &rd[i]; | ||
3085 | switch (r->record_type) | ||
3086 | { | ||
3087 | case GNUNET_DNSPARSER_TYPE_A: | ||
3088 | { | ||
3089 | struct sockaddr_in *in; | ||
3090 | |||
3091 | if (sizeof(struct in_addr) != r->data_size) | ||
3092 | { | ||
3093 | GNUNET_break_op (0); | ||
3094 | break; | ||
3095 | } | ||
3096 | if (GNUNET_YES == got_ip) | ||
3097 | break; | ||
3098 | if (GNUNET_OK != | ||
3099 | GNUNET_NETWORK_test_pf (PF_INET)) | ||
3100 | break; | ||
3101 | got_ip = GNUNET_YES; | ||
3102 | in = (struct sockaddr_in *) &s5r->destination_address; | ||
3103 | in->sin_family = AF_INET; | ||
3104 | GNUNET_memcpy (&in->sin_addr, | ||
3105 | r->data, | ||
3106 | r->data_size); | ||
3107 | in->sin_port = htons (s5r->port); | ||
3108 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
3109 | in->sin_len = sizeof(*in); | ||
3110 | #endif | ||
3111 | } | ||
3112 | break; | ||
3113 | |||
3114 | case GNUNET_DNSPARSER_TYPE_AAAA: | ||
3115 | { | ||
3116 | struct sockaddr_in6 *in; | ||
3117 | |||
3118 | if (sizeof(struct in6_addr) != r->data_size) | ||
3119 | { | ||
3120 | GNUNET_break_op (0); | ||
3121 | break; | ||
3122 | } | ||
3123 | if (GNUNET_YES == got_ip) | ||
3124 | break; | ||
3125 | if (GNUNET_YES == disable_v6) | ||
3126 | break; | ||
3127 | if (GNUNET_OK != | ||
3128 | GNUNET_NETWORK_test_pf (PF_INET6)) | ||
3129 | break; | ||
3130 | /* FIXME: allow user to disable IPv6 per configuration option... */ | ||
3131 | got_ip = GNUNET_YES; | ||
3132 | in = (struct sockaddr_in6 *) &s5r->destination_address; | ||
3133 | in->sin6_family = AF_INET6; | ||
3134 | GNUNET_memcpy (&in->sin6_addr, | ||
3135 | r->data, | ||
3136 | r->data_size); | ||
3137 | in->sin6_port = htons (s5r->port); | ||
3138 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
3139 | in->sin6_len = sizeof(*in); | ||
3140 | #endif | ||
3141 | } | ||
3142 | break; | ||
3143 | |||
3144 | case GNUNET_GNSRECORD_TYPE_VPN: | ||
3145 | GNUNET_break (0); /* should have been translated within GNS */ | ||
3146 | break; | ||
3147 | |||
3148 | case GNUNET_GNSRECORD_TYPE_LEHO: | ||
3149 | GNUNET_free (s5r->leho); | ||
3150 | s5r->leho = GNUNET_strndup (r->data, | ||
3151 | r->data_size); | ||
3152 | break; | ||
3153 | |||
3154 | case GNUNET_GNSRECORD_TYPE_BOX: | ||
3155 | { | ||
3156 | const struct GNUNET_GNSRECORD_BoxRecord *box; | ||
3157 | |||
3158 | if (r->data_size < sizeof(struct GNUNET_GNSRECORD_BoxRecord)) | ||
3159 | { | ||
3160 | GNUNET_break_op (0); | ||
3161 | break; | ||
3162 | } | ||
3163 | box = r->data; | ||
3164 | if ((ntohl (box->record_type) != GNUNET_DNSPARSER_TYPE_TLSA) || | ||
3165 | (ntohs (box->protocol) != IPPROTO_TCP) || | ||
3166 | (ntohs (box->service) != s5r->port)) | ||
3167 | break; /* BOX record does not apply */ | ||
3168 | if (s5r->num_danes >= MAX_DANES) | ||
3169 | { | ||
3170 | GNUNET_break (0); /* MAX_DANES too small */ | ||
3171 | break; | ||
3172 | } | ||
3173 | s5r->is_tls = GNUNET_YES; /* This should be TLS */ | ||
3174 | s5r->dane_data_len[s5r->num_danes] | ||
3175 | = r->data_size - sizeof(struct GNUNET_GNSRECORD_BoxRecord); | ||
3176 | s5r->dane_data[s5r->num_danes] | ||
3177 | = GNUNET_memdup (&box[1], | ||
3178 | s5r->dane_data_len[s5r->num_danes]); | ||
3179 | s5r->num_danes++; | ||
3180 | break; | ||
3181 | } | ||
3182 | |||
3183 | default: | ||
3184 | /* don't care */ | ||
3185 | break; | ||
3186 | } | ||
3187 | } | ||
3188 | if ((GNUNET_YES != got_ip) && | ||
3189 | (GNUNET_YES == tld)) | ||
3190 | { | ||
3191 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3192 | "Name resolution failed to yield useful IP address.\n"); | ||
3193 | signal_socks_failure (s5r, | ||
3194 | SOCKS5_STATUS_GENERAL_FAILURE); | ||
3195 | return; | ||
3196 | } | ||
3197 | s5r->state = SOCKS5_DATA_TRANSFER; | ||
3198 | signal_socks_success (s5r); | ||
3199 | } | ||
3200 | |||
3201 | |||
3202 | /** | ||
3203 | * Remove the first @a len bytes from the beginning of the read buffer. | ||
3204 | * | ||
3205 | * @param s5r the handle clear the read buffer for | ||
3206 | * @param len number of bytes in read buffer to advance | ||
3207 | */ | ||
3208 | static void | ||
3209 | clear_from_s5r_rbuf (struct Socks5Request *s5r, | ||
3210 | size_t len) | ||
3211 | { | ||
3212 | GNUNET_assert (len <= s5r->rbuf_len); | ||
3213 | memmove (s5r->rbuf, | ||
3214 | &s5r->rbuf[len], | ||
3215 | s5r->rbuf_len - len); | ||
3216 | s5r->rbuf_len -= len; | ||
3217 | } | ||
3218 | |||
3219 | |||
3220 | /** | ||
3221 | * Read data from incoming Socks5 connection | ||
3222 | * | ||
3223 | * @param cls the closure with the `struct Socks5Request` | ||
3224 | */ | ||
3225 | static void | ||
3226 | do_s5r_read (void *cls) | ||
3227 | { | ||
3228 | struct Socks5Request *s5r = cls; | ||
3229 | const struct Socks5ClientHelloMessage *c_hello; | ||
3230 | struct Socks5ServerHelloMessage *s_hello; | ||
3231 | const struct Socks5ClientRequestMessage *c_req; | ||
3232 | ssize_t rlen; | ||
3233 | size_t alen; | ||
3234 | const struct GNUNET_SCHEDULER_TaskContext *tc; | ||
3235 | |||
3236 | s5r->rtask = NULL; | ||
3237 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
3238 | if ((NULL != tc->read_ready) && | ||
3239 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, | ||
3240 | s5r->sock))) | ||
3241 | { | ||
3242 | rlen = GNUNET_NETWORK_socket_recv (s5r->sock, | ||
3243 | &s5r->rbuf[s5r->rbuf_len], | ||
3244 | sizeof(s5r->rbuf) - s5r->rbuf_len); | ||
3245 | if (rlen <= 0) | ||
3246 | { | ||
3247 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3248 | "socks5 client disconnected.\n"); | ||
3249 | cleanup_s5r (s5r); | ||
3250 | return; | ||
3251 | } | ||
3252 | s5r->rbuf_len += rlen; | ||
3253 | } | ||
3254 | s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3255 | s5r->sock, | ||
3256 | &do_s5r_read, s5r); | ||
3257 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3258 | "Processing %zu bytes of socks data in state %d\n", | ||
3259 | s5r->rbuf_len, | ||
3260 | s5r->state); | ||
3261 | switch (s5r->state) | ||
3262 | { | ||
3263 | case SOCKS5_INIT: | ||
3264 | c_hello = (const struct Socks5ClientHelloMessage*) &s5r->rbuf; | ||
3265 | if ((s5r->rbuf_len < sizeof(struct Socks5ClientHelloMessage)) || | ||
3266 | (s5r->rbuf_len < sizeof(struct Socks5ClientHelloMessage) | ||
3267 | + c_hello->num_auth_methods)) | ||
3268 | return; /* need more data */ | ||
3269 | if (SOCKS_VERSION_5 != c_hello->version) | ||
3270 | { | ||
3271 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3272 | _ ("Unsupported socks version %d\n"), | ||
3273 | (int) c_hello->version); | ||
3274 | cleanup_s5r (s5r); | ||
3275 | return; | ||
3276 | } | ||
3277 | clear_from_s5r_rbuf (s5r, | ||
3278 | sizeof(struct Socks5ClientHelloMessage) | ||
3279 | + c_hello->num_auth_methods); | ||
3280 | GNUNET_assert (0 == s5r->wbuf_len); | ||
3281 | s_hello = (struct Socks5ServerHelloMessage *) &s5r->wbuf; | ||
3282 | s5r->wbuf_len = sizeof(struct Socks5ServerHelloMessage); | ||
3283 | s_hello->version = SOCKS_VERSION_5; | ||
3284 | s_hello->auth_method = SOCKS_AUTH_NONE; | ||
3285 | GNUNET_assert (NULL == s5r->wtask); | ||
3286 | s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3287 | s5r->sock, | ||
3288 | &do_write, s5r); | ||
3289 | s5r->state = SOCKS5_REQUEST; | ||
3290 | return; | ||
3291 | |||
3292 | case SOCKS5_REQUEST: | ||
3293 | c_req = (const struct Socks5ClientRequestMessage *) &s5r->rbuf; | ||
3294 | if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage)) | ||
3295 | return; | ||
3296 | switch (c_req->command) | ||
3297 | { | ||
3298 | case SOCKS5_CMD_TCP_STREAM: | ||
3299 | /* handled below */ | ||
3300 | break; | ||
3301 | |||
3302 | default: | ||
3303 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3304 | _ ("Unsupported socks command %d\n"), | ||
3305 | (int) c_req->command); | ||
3306 | signal_socks_failure (s5r, | ||
3307 | SOCKS5_STATUS_COMMAND_NOT_SUPPORTED); | ||
3308 | return; | ||
3309 | } | ||
3310 | switch (c_req->addr_type) | ||
3311 | { | ||
3312 | case SOCKS5_AT_IPV4: | ||
3313 | { | ||
3314 | const struct in_addr *v4 = (const struct in_addr *) &c_req[1]; | ||
3315 | const uint16_t *port = (const uint16_t *) &v4[1]; | ||
3316 | struct sockaddr_in *in; | ||
3317 | |||
3318 | s5r->port = ntohs (*port); | ||
3319 | alen = sizeof(struct in_addr); | ||
3320 | if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage) | ||
3321 | + alen + sizeof(uint16_t)) | ||
3322 | return; /* need more data */ | ||
3323 | in = (struct sockaddr_in *) &s5r->destination_address; | ||
3324 | in->sin_family = AF_INET; | ||
3325 | in->sin_addr = *v4; | ||
3326 | in->sin_port = *port; | ||
3327 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
3328 | in->sin_len = sizeof(*in); | ||
3329 | #endif | ||
3330 | s5r->state = SOCKS5_DATA_TRANSFER; | ||
3331 | } | ||
3332 | break; | ||
3333 | |||
3334 | case SOCKS5_AT_IPV6: | ||
3335 | { | ||
3336 | const struct in6_addr *v6 = (const struct in6_addr *) &c_req[1]; | ||
3337 | const uint16_t *port = (const uint16_t *) &v6[1]; | ||
3338 | struct sockaddr_in6 *in; | ||
3339 | |||
3340 | s5r->port = ntohs (*port); | ||
3341 | alen = sizeof(struct in6_addr); | ||
3342 | if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage) | ||
3343 | + alen + sizeof(uint16_t)) | ||
3344 | return; /* need more data */ | ||
3345 | in = (struct sockaddr_in6 *) &s5r->destination_address; | ||
3346 | in->sin6_family = AF_INET6; | ||
3347 | in->sin6_addr = *v6; | ||
3348 | in->sin6_port = *port; | ||
3349 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
3350 | in->sin6_len = sizeof(*in); | ||
3351 | #endif | ||
3352 | s5r->state = SOCKS5_DATA_TRANSFER; | ||
3353 | } | ||
3354 | break; | ||
3355 | |||
3356 | case SOCKS5_AT_DOMAINNAME: | ||
3357 | { | ||
3358 | const uint8_t *dom_len; | ||
3359 | const char *dom_name; | ||
3360 | const uint16_t *port; | ||
3361 | |||
3362 | dom_len = (const uint8_t *) &c_req[1]; | ||
3363 | alen = *dom_len + 1; | ||
3364 | if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage) | ||
3365 | + alen + sizeof(uint16_t)) | ||
3366 | return; /* need more data */ | ||
3367 | dom_name = (const char *) &dom_len[1]; | ||
3368 | port = (const uint16_t *) &dom_name[*dom_len]; | ||
3369 | s5r->domain = GNUNET_strndup (dom_name, | ||
3370 | *dom_len); | ||
3371 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3372 | "Requested connection is to %s:%d\n", | ||
3373 | // (HTTPS_PORT == s5r->port) ? "s" : "", | ||
3374 | s5r->domain, | ||
3375 | ntohs (*port)); | ||
3376 | s5r->state = SOCKS5_RESOLVING; | ||
3377 | s5r->port = ntohs (*port); | ||
3378 | s5r->is_tls = (HTTPS_PORT == s5r->port) ? GNUNET_YES : GNUNET_NO; | ||
3379 | s5r->gns_lookup = GNUNET_GNS_lookup_with_tld (gns_handle, | ||
3380 | s5r->domain, | ||
3381 | GNUNET_DNSPARSER_TYPE_A, | ||
3382 | GNUNET_GNS_LO_LOCAL_MASTER /* only cached */, | ||
3383 | &handle_gns_result, | ||
3384 | s5r); | ||
3385 | break; | ||
3386 | } | ||
3387 | |||
3388 | default: | ||
3389 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3390 | _ ("Unsupported socks address type %d\n"), | ||
3391 | (int) c_req->addr_type); | ||
3392 | signal_socks_failure (s5r, | ||
3393 | SOCKS5_STATUS_ADDRESS_TYPE_NOT_SUPPORTED); | ||
3394 | return; | ||
3395 | } | ||
3396 | clear_from_s5r_rbuf (s5r, | ||
3397 | sizeof(struct Socks5ClientRequestMessage) | ||
3398 | + alen + sizeof(uint16_t)); | ||
3399 | if (0 != s5r->rbuf_len) | ||
3400 | { | ||
3401 | /* read more bytes than healthy, why did the client send more!? */ | ||
3402 | GNUNET_break_op (0); | ||
3403 | signal_socks_failure (s5r, | ||
3404 | SOCKS5_STATUS_GENERAL_FAILURE); | ||
3405 | return; | ||
3406 | } | ||
3407 | if (SOCKS5_DATA_TRANSFER == s5r->state) | ||
3408 | { | ||
3409 | /* if we are not waiting for GNS resolution, signal success */ | ||
3410 | signal_socks_success (s5r); | ||
3411 | } | ||
3412 | /* We are done reading right now */ | ||
3413 | GNUNET_SCHEDULER_cancel (s5r->rtask); | ||
3414 | s5r->rtask = NULL; | ||
3415 | return; | ||
3416 | |||
3417 | case SOCKS5_RESOLVING: | ||
3418 | GNUNET_assert (0); | ||
3419 | return; | ||
3420 | |||
3421 | case SOCKS5_DATA_TRANSFER: | ||
3422 | GNUNET_assert (0); | ||
3423 | return; | ||
3424 | |||
3425 | default: | ||
3426 | GNUNET_assert (0); | ||
3427 | return; | ||
3428 | } | ||
3429 | } | ||
3430 | |||
3431 | |||
3432 | /** | ||
3433 | * Accept new incoming connections | ||
3434 | * | ||
3435 | * @param cls the closure with the lsock4 or lsock6 | ||
3436 | * @param tc the scheduler context | ||
3437 | */ | ||
3438 | static void | ||
3439 | do_accept (void *cls) | ||
3440 | { | ||
3441 | struct GNUNET_NETWORK_Handle *lsock = cls; | ||
3442 | struct GNUNET_NETWORK_Handle *s; | ||
3443 | struct Socks5Request *s5r; | ||
3444 | |||
3445 | GNUNET_assert (NULL != lsock); | ||
3446 | if (lsock == lsock4) | ||
3447 | ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3448 | lsock, | ||
3449 | &do_accept, | ||
3450 | lsock); | ||
3451 | else if (lsock == lsock6) | ||
3452 | ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3453 | lsock, | ||
3454 | &do_accept, | ||
3455 | lsock); | ||
3456 | else | ||
3457 | GNUNET_assert (0); | ||
3458 | s = GNUNET_NETWORK_socket_accept (lsock, | ||
3459 | NULL, | ||
3460 | NULL); | ||
3461 | if (NULL == s) | ||
3462 | { | ||
3463 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | ||
3464 | "accept"); | ||
3465 | return; | ||
3466 | } | ||
3467 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3468 | "Got an inbound connection, waiting for data\n"); | ||
3469 | s5r = GNUNET_new (struct Socks5Request); | ||
3470 | GNUNET_CONTAINER_DLL_insert (s5r_head, | ||
3471 | s5r_tail, | ||
3472 | s5r); | ||
3473 | s5r->sock = s; | ||
3474 | s5r->state = SOCKS5_INIT; | ||
3475 | s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3476 | s5r->sock, | ||
3477 | &do_s5r_read, | ||
3478 | s5r); | ||
3479 | } | ||
3480 | |||
3481 | |||
3482 | /* ******************* General / main code ********************* */ | ||
3483 | |||
3484 | |||
3485 | /** | ||
3486 | * Task run on shutdown | ||
3487 | * | ||
3488 | * @param cls closure | ||
3489 | */ | ||
3490 | static void | ||
3491 | do_shutdown (void *cls) | ||
3492 | { | ||
3493 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
3494 | "Shutting down...\n"); | ||
3495 | /* MHD requires resuming before destroying the daemons */ | ||
3496 | for (struct Socks5Request *s5r = s5r_head; | ||
3497 | NULL != s5r; | ||
3498 | s5r = s5r->next) | ||
3499 | { | ||
3500 | if (s5r->suspended) | ||
3501 | { | ||
3502 | s5r->suspended = GNUNET_NO; | ||
3503 | MHD_resume_connection (s5r->con); | ||
3504 | } | ||
3505 | } | ||
3506 | while (NULL != mhd_httpd_head) | ||
3507 | kill_httpd (mhd_httpd_head); | ||
3508 | while (NULL != s5r_head) | ||
3509 | cleanup_s5r (s5r_head); | ||
3510 | if (NULL != lsock4) | ||
3511 | { | ||
3512 | GNUNET_NETWORK_socket_close (lsock4); | ||
3513 | lsock4 = NULL; | ||
3514 | } | ||
3515 | if (NULL != lsock6) | ||
3516 | { | ||
3517 | GNUNET_NETWORK_socket_close (lsock6); | ||
3518 | lsock6 = NULL; | ||
3519 | } | ||
3520 | if (NULL != curl_multi) | ||
3521 | { | ||
3522 | curl_multi_cleanup (curl_multi); | ||
3523 | curl_multi = NULL; | ||
3524 | } | ||
3525 | if (NULL != gns_handle) | ||
3526 | { | ||
3527 | GNUNET_GNS_disconnect (gns_handle); | ||
3528 | gns_handle = NULL; | ||
3529 | } | ||
3530 | if (NULL != curl_download_task) | ||
3531 | { | ||
3532 | GNUNET_SCHEDULER_cancel (curl_download_task); | ||
3533 | curl_download_task = NULL; | ||
3534 | } | ||
3535 | if (NULL != ltask4) | ||
3536 | { | ||
3537 | GNUNET_SCHEDULER_cancel (ltask4); | ||
3538 | ltask4 = NULL; | ||
3539 | } | ||
3540 | if (NULL != ltask6) | ||
3541 | { | ||
3542 | GNUNET_SCHEDULER_cancel (ltask6); | ||
3543 | ltask6 = NULL; | ||
3544 | } | ||
3545 | gnutls_x509_crt_deinit (proxy_ca.cert); | ||
3546 | gnutls_x509_privkey_deinit (proxy_ca.key); | ||
3547 | gnutls_global_deinit (); | ||
3548 | } | ||
3549 | |||
3550 | |||
3551 | /** | ||
3552 | * Create an IPv4 listen socket bound to our port. | ||
3553 | * | ||
3554 | * @return NULL on error | ||
3555 | */ | ||
3556 | static struct GNUNET_NETWORK_Handle * | ||
3557 | bind_v4 () | ||
3558 | { | ||
3559 | struct GNUNET_NETWORK_Handle *ls; | ||
3560 | struct sockaddr_in sa4; | ||
3561 | int eno; | ||
3562 | |||
3563 | memset (&sa4, 0, sizeof(sa4)); | ||
3564 | sa4.sin_family = AF_INET; | ||
3565 | sa4.sin_port = htons (port); | ||
3566 | sa4.sin_addr.s_addr = address; | ||
3567 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
3568 | sa4.sin_len = sizeof(sa4); | ||
3569 | #endif | ||
3570 | ls = GNUNET_NETWORK_socket_create (AF_INET, | ||
3571 | SOCK_STREAM, | ||
3572 | 0); | ||
3573 | if (NULL == ls) | ||
3574 | return NULL; | ||
3575 | if (GNUNET_OK != | ||
3576 | GNUNET_NETWORK_socket_bind (ls, | ||
3577 | (const struct sockaddr *) &sa4, | ||
3578 | sizeof(sa4))) | ||
3579 | { | ||
3580 | eno = errno; | ||
3581 | GNUNET_NETWORK_socket_close (ls); | ||
3582 | errno = eno; | ||
3583 | return NULL; | ||
3584 | } | ||
3585 | return ls; | ||
3586 | } | ||
3587 | |||
3588 | |||
3589 | /** | ||
3590 | * Create an IPv6 listen socket bound to our port. | ||
3591 | * | ||
3592 | * @return NULL on error | ||
3593 | */ | ||
3594 | static struct GNUNET_NETWORK_Handle * | ||
3595 | bind_v6 () | ||
3596 | { | ||
3597 | struct GNUNET_NETWORK_Handle *ls; | ||
3598 | struct sockaddr_in6 sa6; | ||
3599 | int eno; | ||
3600 | |||
3601 | memset (&sa6, 0, sizeof(sa6)); | ||
3602 | sa6.sin6_family = AF_INET6; | ||
3603 | sa6.sin6_port = htons (port); | ||
3604 | sa6.sin6_addr = address6; | ||
3605 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
3606 | sa6.sin6_len = sizeof(sa6); | ||
3607 | #endif | ||
3608 | ls = GNUNET_NETWORK_socket_create (AF_INET6, | ||
3609 | SOCK_STREAM, | ||
3610 | 0); | ||
3611 | if (NULL == ls) | ||
3612 | return NULL; | ||
3613 | if (GNUNET_OK != | ||
3614 | GNUNET_NETWORK_socket_bind (ls, | ||
3615 | (const struct sockaddr *) &sa6, | ||
3616 | sizeof(sa6))) | ||
3617 | { | ||
3618 | eno = errno; | ||
3619 | GNUNET_NETWORK_socket_close (ls); | ||
3620 | errno = eno; | ||
3621 | return NULL; | ||
3622 | } | ||
3623 | return ls; | ||
3624 | } | ||
3625 | |||
3626 | |||
3627 | /** | ||
3628 | * Main function that will be run | ||
3629 | * | ||
3630 | * @param cls closure | ||
3631 | * @param args remaining command-line arguments | ||
3632 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
3633 | * @param c configuration | ||
3634 | */ | ||
3635 | static void | ||
3636 | run (void *cls, | ||
3637 | char *const *args, | ||
3638 | const char *cfgfile, | ||
3639 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
3640 | { | ||
3641 | char*cafile_cfg = NULL; | ||
3642 | char*cafile; | ||
3643 | char*addr_str; | ||
3644 | struct MhdHttpList *hd; | ||
3645 | |||
3646 | cfg = c; | ||
3647 | |||
3648 | /* Get address to bind to */ | ||
3649 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns-proxy", | ||
3650 | "BIND_TO", | ||
3651 | &addr_str)) | ||
3652 | { | ||
3653 | // No address specified | ||
3654 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3655 | "Don't know what to bind to...\n"); | ||
3656 | GNUNET_free (addr_str); | ||
3657 | GNUNET_SCHEDULER_shutdown (); | ||
3658 | return; | ||
3659 | } | ||
3660 | if (1 != inet_pton (AF_INET, addr_str, &address)) | ||
3661 | { | ||
3662 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3663 | "Unable to parse address %s\n", | ||
3664 | addr_str); | ||
3665 | GNUNET_free (addr_str); | ||
3666 | GNUNET_SCHEDULER_shutdown (); | ||
3667 | return; | ||
3668 | } | ||
3669 | GNUNET_free (addr_str); | ||
3670 | /* Get address to bind to */ | ||
3671 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns-proxy", | ||
3672 | "BIND_TO6", | ||
3673 | &addr_str)) | ||
3674 | { | ||
3675 | // No address specified | ||
3676 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3677 | "Don't know what to bind6 to...\n"); | ||
3678 | GNUNET_free (addr_str); | ||
3679 | GNUNET_SCHEDULER_shutdown (); | ||
3680 | return; | ||
3681 | } | ||
3682 | if (1 != inet_pton (AF_INET6, addr_str, &address6)) | ||
3683 | { | ||
3684 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3685 | "Unable to parse IPv6 address %s\n", | ||
3686 | addr_str); | ||
3687 | GNUNET_free (addr_str); | ||
3688 | GNUNET_SCHEDULER_shutdown (); | ||
3689 | return; | ||
3690 | } | ||
3691 | GNUNET_free (addr_str); | ||
3692 | |||
3693 | if (NULL == (curl_multi = curl_multi_init ())) | ||
3694 | { | ||
3695 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3696 | "Failed to create cURL multi handle!\n"); | ||
3697 | return; | ||
3698 | } | ||
3699 | cafile = cafile_opt; | ||
3700 | if (NULL == cafile) | ||
3701 | { | ||
3702 | if (GNUNET_OK != | ||
3703 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
3704 | "gns-proxy", | ||
3705 | "PROXY_CACERT", | ||
3706 | &cafile_cfg)) | ||
3707 | { | ||
3708 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
3709 | "gns-proxy", | ||
3710 | "PROXY_CACERT"); | ||
3711 | return; | ||
3712 | } | ||
3713 | cafile = cafile_cfg; | ||
3714 | } | ||
3715 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3716 | "Using `%s' as CA\n", | ||
3717 | cafile); | ||
3718 | |||
3719 | gnutls_global_init (); | ||
3720 | gnutls_x509_crt_init (&proxy_ca.cert); | ||
3721 | gnutls_x509_privkey_init (&proxy_ca.key); | ||
3722 | |||
3723 | if ((GNUNET_OK != | ||
3724 | load_cert_from_file (proxy_ca.cert, | ||
3725 | cafile)) || | ||
3726 | (GNUNET_OK != | ||
3727 | load_key_from_file (proxy_ca.key, | ||
3728 | cafile))) | ||
3729 | { | ||
3730 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3731 | _ ("Failed to load X.509 key and certificate from `%s'\n"), | ||
3732 | cafile); | ||
3733 | gnutls_x509_crt_deinit (proxy_ca.cert); | ||
3734 | gnutls_x509_privkey_deinit (proxy_ca.key); | ||
3735 | gnutls_global_deinit (); | ||
3736 | GNUNET_free (cafile_cfg); | ||
3737 | return; | ||
3738 | } | ||
3739 | GNUNET_free (cafile_cfg); | ||
3740 | if (NULL == (gns_handle = GNUNET_GNS_connect (cfg))) | ||
3741 | { | ||
3742 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3743 | "Unable to connect to GNS!\n"); | ||
3744 | gnutls_x509_crt_deinit (proxy_ca.cert); | ||
3745 | gnutls_x509_privkey_deinit (proxy_ca.key); | ||
3746 | gnutls_global_deinit (); | ||
3747 | return; | ||
3748 | } | ||
3749 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | ||
3750 | NULL); | ||
3751 | |||
3752 | /* Open listen socket for socks proxy */ | ||
3753 | lsock6 = bind_v6 (); | ||
3754 | if (NULL == lsock6) | ||
3755 | { | ||
3756 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | ||
3757 | "bind"); | ||
3758 | } | ||
3759 | else | ||
3760 | { | ||
3761 | if (GNUNET_OK != | ||
3762 | GNUNET_NETWORK_socket_listen (lsock6, | ||
3763 | 5)) | ||
3764 | { | ||
3765 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | ||
3766 | "listen"); | ||
3767 | GNUNET_NETWORK_socket_close (lsock6); | ||
3768 | lsock6 = NULL; | ||
3769 | } | ||
3770 | else | ||
3771 | { | ||
3772 | ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3773 | lsock6, | ||
3774 | &do_accept, | ||
3775 | lsock6); | ||
3776 | } | ||
3777 | } | ||
3778 | lsock4 = bind_v4 (); | ||
3779 | if (NULL == lsock4) | ||
3780 | { | ||
3781 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | ||
3782 | "bind"); | ||
3783 | } | ||
3784 | else | ||
3785 | { | ||
3786 | if (GNUNET_OK != | ||
3787 | GNUNET_NETWORK_socket_listen (lsock4, | ||
3788 | 5)) | ||
3789 | { | ||
3790 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | ||
3791 | "listen"); | ||
3792 | GNUNET_NETWORK_socket_close (lsock4); | ||
3793 | lsock4 = NULL; | ||
3794 | } | ||
3795 | else | ||
3796 | { | ||
3797 | ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3798 | lsock4, | ||
3799 | &do_accept, | ||
3800 | lsock4); | ||
3801 | } | ||
3802 | } | ||
3803 | if ((NULL == lsock4) && | ||
3804 | (NULL == lsock6)) | ||
3805 | { | ||
3806 | GNUNET_SCHEDULER_shutdown (); | ||
3807 | return; | ||
3808 | } | ||
3809 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | ||
3810 | { | ||
3811 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3812 | "cURL global init failed!\n"); | ||
3813 | GNUNET_SCHEDULER_shutdown (); | ||
3814 | return; | ||
3815 | } | ||
3816 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3817 | "Proxy listens on port %u\n", | ||
3818 | (unsigned int) port); | ||
3819 | |||
3820 | /* start MHD daemon for HTTP */ | ||
3821 | hd = GNUNET_new (struct MhdHttpList); | ||
3822 | hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET | ||
3823 | | MHD_ALLOW_SUSPEND_RESUME, | ||
3824 | 0, | ||
3825 | NULL, NULL, | ||
3826 | &create_response, hd, | ||
3827 | MHD_OPTION_CONNECTION_TIMEOUT, (unsigned | ||
3828 | int) 16, | ||
3829 | MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, | ||
3830 | NULL, | ||
3831 | MHD_OPTION_NOTIFY_CONNECTION, | ||
3832 | &mhd_connection_cb, NULL, | ||
3833 | MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, | ||
3834 | NULL, | ||
3835 | MHD_OPTION_END); | ||
3836 | if (NULL == hd->daemon) | ||
3837 | { | ||
3838 | GNUNET_free (hd); | ||
3839 | GNUNET_SCHEDULER_shutdown (); | ||
3840 | return; | ||
3841 | } | ||
3842 | httpd = hd; | ||
3843 | GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, | ||
3844 | mhd_httpd_tail, | ||
3845 | hd); | ||
3846 | } | ||
3847 | |||
3848 | |||
3849 | /** | ||
3850 | * The main function for gnunet-gns-proxy. | ||
3851 | * | ||
3852 | * @param argc number of arguments from the command line | ||
3853 | * @param argv command line arguments | ||
3854 | * @return 0 ok, 1 on error | ||
3855 | */ | ||
3856 | int | ||
3857 | main (int argc, | ||
3858 | char *const *argv) | ||
3859 | { | ||
3860 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
3861 | GNUNET_GETOPT_option_uint16 ('p', | ||
3862 | "port", | ||
3863 | NULL, | ||
3864 | gettext_noop ( | ||
3865 | "listen on specified port (default: 7777)"), | ||
3866 | &port), | ||
3867 | GNUNET_GETOPT_option_string ('a', | ||
3868 | "authority", | ||
3869 | NULL, | ||
3870 | gettext_noop ("pem file to use as CA"), | ||
3871 | &cafile_opt), | ||
3872 | GNUNET_GETOPT_option_flag ('6', | ||
3873 | "disable-ivp6", | ||
3874 | gettext_noop ("disable use of IPv6"), | ||
3875 | &disable_v6), | ||
3876 | |||
3877 | GNUNET_GETOPT_OPTION_END | ||
3878 | }; | ||
3879 | static const char*page = | ||
3880 | "<html><head><title>gnunet-gns-proxy</title>" | ||
3881 | "</head><body>cURL fail</body></html>"; | ||
3882 | int ret; | ||
3883 | |||
3884 | if (GNUNET_OK != | ||
3885 | GNUNET_STRINGS_get_utf8_args (argc, argv, | ||
3886 | &argc, &argv)) | ||
3887 | return 2; | ||
3888 | GNUNET_log_setup ("gnunet-gns-proxy", | ||
3889 | "WARNING", | ||
3890 | NULL); | ||
3891 | curl_failure_response | ||
3892 | = MHD_create_response_from_buffer (strlen (page), | ||
3893 | (void *) page, | ||
3894 | MHD_RESPMEM_PERSISTENT); | ||
3895 | |||
3896 | ret = | ||
3897 | (GNUNET_OK == | ||
3898 | GNUNET_PROGRAM_run (argc, argv, | ||
3899 | "gnunet-gns-proxy", | ||
3900 | _ ("GNUnet GNS proxy"), | ||
3901 | options, | ||
3902 | &run, NULL)) ? 0 : 1; | ||
3903 | MHD_destroy_response (curl_failure_response); | ||
3904 | GNUNET_free_nz ((char *) argv); | ||
3905 | return ret; | ||
3906 | } | ||
3907 | |||
3908 | |||
3909 | /* end of gnunet-gns-proxy.c */ | ||