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