2 This file is part of GNUnet.
3 (C) 2012-2013 Christian Grothoff (and other contributing authors)
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.
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.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file gnunet-gns-fcfsd.c
22 * @brief HTTP daemon that offers first-come-first-serve GNS domain registration
23 * @author Christian Grothoff
26 * - the code currently contains a 'race' between checking that the
27 * domain name is available and allocating it to the new public key
28 * (should this race be solved by namestore or by fcfsd?)
29 * - nicer error reporting to browser
32 #include <microhttpd.h>
33 #include "gnunet_util_lib.h"
34 #include "gnunet_namestore_service.h"
37 * Invalid method page.
39 #define METHOD_ERROR "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>Illegal request</title></head><body>Go away.</body></html>"
44 #define MAIN_PAGE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>GNUnet FCFS Authority Name Registration Service</title></head><body><form action=\"S\" method=\"post\">What is your desired domain name? (at most 63 lowercase characters, no dots allowed.) <input type=\"text\" name=\"domain\" /> <p> What is your public key? (Copy from gnunet-setup.) <input type=\"text\" name=\"pkey\" /> <input type=\"submit\" value=\"Next\" /><br/><a href=./Zoneinfo> List of all registered names </a></body></html>"
49 #define SUBMIT_PAGE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>%s</title></head><body>%s</body></html>"
52 * Fcfs zoneinfo page (/Zoneinfo)
54 #define ZONEINFO_PAGE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>FCFS Zoneinfo</title></head><body><h1> FCFS Zoneinfo </h1><table border=\"1\"><th>name</th><th>PKEY</th>%s</table></body></html>"
56 #define FCFS_ZONEINFO_URL "/Zoneinfo"
59 * Mime type for HTML pages.
61 #define MIME_HTML "text/html"
66 #define COOKIE_NAME "gns-fcfs"
68 #define DEFAULT_ZONEINFO_BUFSIZE 2048
71 * Phases a request goes through.
76 * Start phase (parsing POST, checking).
81 * Lookup to see if the domain name is taken.
86 * Storing of the record.
91 * We're done with success.
96 * Send failure message.
103 * Data kept per request.
109 * Associated session.
111 struct Session *session;
114 * Post processor handling form data (IF this is
117 struct MHD_PostProcessor *pp;
120 * URL to serve in response to this POST (if this request
123 const char *post_url;
126 * Active request with the namestore.
128 struct GNUNET_NAMESTORE_QueueEntry *qe;
131 * Current processing phase.
136 * Domain name submitted via form.
138 char domain_name[64];
141 * Public key submitted via form.
143 char public_key[128];
150 struct ZoneinfoRequest
155 struct MHD_Connection *connection;
160 struct GNUNET_NAMESTORE_ZoneIterator *list_it;
173 * Buffer write offset
179 * MHD deamon reference.
181 static struct MHD_Daemon *httpd;
186 static GNUNET_SCHEDULER_TaskIdentifier httpd_task;
189 * Handle to the namestore.
191 static struct GNUNET_NAMESTORE_Handle *ns;
194 * Private key for the fcfsd zone.
196 static struct GNUNET_CRYPTO_EccPrivateKey *fcfs_zone_pkey;
200 * Task run whenever HTTP server operations are pending.
203 * @param tc scheduler context
207 const struct GNUNET_SCHEDULER_TaskContext *tc);
211 * Schedule task to run MHD server now.
216 if (GNUNET_SCHEDULER_NO_TASK != httpd_task)
218 GNUNET_SCHEDULER_cancel (httpd_task);
219 httpd_task = GNUNET_SCHEDULER_NO_TASK;
221 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL);
226 iterate_cb (void *cls,
227 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
230 const struct GNUNET_NAMESTORE_RecordData *rd)
232 struct ZoneinfoRequest *zr = cls;
233 struct MHD_Response *response;
244 /* return static form */
245 GNUNET_asprintf (&full_page,
249 response = MHD_create_response_from_buffer (strlen (full_page),
251 MHD_RESPMEM_MUST_FREE);
252 MHD_add_response_header (response,
253 MHD_HTTP_HEADER_CONTENT_TYPE,
255 MHD_queue_response (zr->connection,
258 MHD_destroy_response (response);
259 GNUNET_free (zr->zoneinfo);
267 GNUNET_NAMESTORE_zone_iterator_next (zr->list_it);
271 if (GNUNET_NAMESTORE_TYPE_PKEY != rd->record_type)
273 GNUNET_NAMESTORE_zone_iterator_next (zr->list_it);
277 bytes_free = zr->buf_len - zr->write_offset;
278 pkey = GNUNET_NAMESTORE_value_to_string (rd->record_type,
282 if (bytes_free < (strlen (name) + strlen (pkey) + 40))
284 new_buf = GNUNET_malloc (zr->buf_len * 2);
285 memcpy (new_buf, zr->zoneinfo, zr->write_offset);
286 GNUNET_free (zr->zoneinfo);
287 zr->zoneinfo = new_buf;
290 sprintf (zr->zoneinfo + zr->write_offset,
291 "<tr><td>%s</td><td>%s</td></tr>",
294 zr->write_offset = strlen (zr->zoneinfo);
295 GNUNET_NAMESTORE_zone_iterator_next (zr->list_it);
302 * Handler that returns FCFS zoneinfo page.
304 * @param connection connection to use
305 * @return MHD_YES on success
308 serve_zoneinfo_page (struct MHD_Connection *connection)
310 struct ZoneinfoRequest *zr;
312 zr = GNUNET_new (struct ZoneinfoRequest);
313 zr->zoneinfo = GNUNET_malloc (DEFAULT_ZONEINFO_BUFSIZE);
314 zr->buf_len = DEFAULT_ZONEINFO_BUFSIZE;
315 zr->connection = connection;
316 zr->write_offset = 0;
317 zr->list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
326 * Handler that returns a simple static HTTP page.
328 * @param connection connection to use
329 * @return MHD_YES on success
332 serve_main_page (struct MHD_Connection *connection)
335 struct MHD_Response *response;
337 /* return static form */
338 response = MHD_create_response_from_buffer (strlen (MAIN_PAGE),
340 MHD_RESPMEM_PERSISTENT);
341 MHD_add_response_header (response,
342 MHD_HTTP_HEADER_CONTENT_TYPE,
344 ret = MHD_queue_response (connection,
347 MHD_destroy_response (response);
353 * Send the 'SUBMIT_PAGE'.
355 * @param info information string to send to the user
356 * @param request request information
357 * @param connection connection to use
360 fill_s_reply (const char *info,
361 struct Request *request,
362 struct MHD_Connection *connection)
366 struct MHD_Response *response;
368 GNUNET_asprintf (&reply,
372 /* return static form */
373 response = MHD_create_response_from_buffer (strlen (reply),
375 MHD_RESPMEM_MUST_FREE);
376 MHD_add_response_header (response,
377 MHD_HTTP_HEADER_CONTENT_TYPE,
379 ret = MHD_queue_response (connection,
382 MHD_destroy_response (response);
388 * Iterator over key-value pairs where the value
389 * maybe made available in increments and/or may
390 * not be zero-terminated. Used for processing
393 * @param cls user-specified closure
394 * @param kind type of the value
395 * @param key 0-terminated key for the value
396 * @param filename name of the uploaded file, NULL if not known
397 * @param content_type mime-type of the data, NULL if not known
398 * @param transfer_encoding encoding of the data, NULL if not known
399 * @param data pointer to size bytes of data at the
401 * @param off offset of data in the overall value
402 * @param size number of bytes in data available
403 * @return MHD_YES to continue iterating,
404 * MHD_NO to abort the iteration
407 post_iterator (void *cls,
408 enum MHD_ValueKind kind,
410 const char *filename,
411 const char *content_type,
412 const char *transfer_encoding,
413 const char *data, uint64_t off, size_t size)
415 struct Request *request = cls;
417 if (0 == strcmp ("domain", key))
419 if (size + off >= sizeof(request->domain_name))
420 size = sizeof (request->domain_name) - off - 1;
421 memcpy (&request->domain_name[off],
424 request->domain_name[size+off] = '\0';
427 if (0 == strcmp ("pkey", key))
429 if (size + off >= sizeof(request->public_key))
430 size = sizeof (request->public_key) - off - 1;
431 memcpy (&request->public_key[off],
434 request->public_key[size+off] = '\0';
437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
438 _("Unsupported form value `%s'\n"),
447 * Continuation called to notify client about result of the
451 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
452 * GNUNET_NO if content was already there
453 * GNUNET_YES (or other positive value) on success
454 * @param emsg NULL on success, otherwise an error message
457 put_continuation (void *cls,
461 struct Request *request = cls;
466 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
467 _("Failed to create record for domain `%s': %s\n"),
468 request->domain_name,
470 request->phase = RP_FAIL;
473 request->phase = RP_SUCCESS;
479 * Test if a name mapping was found, if so, refuse. If not, initiate storing of the record.
482 * @param zone_key public key of the zone
483 * @param name name that is being mapped (at most 255 characters long)
484 * @param rd_count number of entries in 'rd' array
485 * @param rd array of records with data to store
488 zone_to_name_cb (void *cls,
489 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
491 unsigned int rd_count,
492 const struct GNUNET_NAMESTORE_RecordData *rd)
494 struct Request *request = cls;
495 struct GNUNET_NAMESTORE_RecordData r;
496 struct GNUNET_CRYPTO_ShortHashCode pub;
501 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
502 _("Found existing name `%s' for the given key\n"),
504 request->phase = RP_FAIL;
509 r.data_size = sizeof (pub);
510 r.expiration_time = UINT64_MAX;
511 r.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
512 r.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
513 request->qe = GNUNET_NAMESTORE_records_store (ns,
515 request->domain_name,
523 * Process a record that was stored in the namestore. Used to check if
524 * the requested name already exists in the namestore. If not,
525 * proceed to check if the requested key already exists.
528 * @param zone_key private key of the zone
529 * @param name name that is being mapped (at most 255 characters long)
530 * @param rd_count number of entries in 'rd' array
531 * @param rd array of records with data to store
534 lookup_result_processor (void *cls,
535 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
537 unsigned int rd_count,
538 const struct GNUNET_NAMESTORE_RecordData *rd)
540 struct Request *request = cls;
541 struct GNUNET_CRYPTO_EccPublicKey pub;
546 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
547 _("Found %u existing records for domain `%s'\n"),
549 request->domain_name);
550 request->phase = RP_FAIL;
555 GNUNET_CRYPTO_ecc_public_key_from_string (request->public_key,
556 strlen (request->public_key),
560 request->phase = RP_FAIL;
564 request->qe = GNUNET_NAMESTORE_zone_to_name (ns,
573 * Main MHD callback for handling requests.
576 * @param connection MHD connection handle
577 * @param url the requested url
578 * @param method the HTTP method used ("GET", "PUT", etc.)
579 * @param version the HTTP version string (i.e. "HTTP/1.1")
580 * @param upload_data the data being uploaded (excluding HEADERS,
581 * for a POST that fits into memory and that is encoded
582 * with a supported encoding, the POST data will NOT be
583 * given in upload_data and is instead available as
584 * part of MHD_get_connection_values; very large POST
585 * data *will* be made available incrementally in
587 * @param upload_data_size set initially to the size of the
588 * upload_data provided; the method must update this
589 * value to the number of bytes NOT processed;
590 * @param ptr pointer to location where we store the 'struct Request'
591 * @return MHD_YES if the connection was handled successfully,
592 * MHD_NO if the socket must be closed due to a serious
593 * error while handling the request
596 create_response (void *cls,
597 struct MHD_Connection *connection,
601 const char *upload_data,
602 size_t *upload_data_size,
605 struct MHD_Response *response;
606 struct Request *request;
608 struct GNUNET_CRYPTO_EccPublicKey pub;
610 if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
611 (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
613 if (0 == strcmp (url, FCFS_ZONEINFO_URL))
614 ret = serve_zoneinfo_page (connection);
616 ret = serve_main_page (connection);
618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619 _("Failed to create page for `%s'\n"),
623 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
628 request = GNUNET_malloc (sizeof (struct Request));
630 request->pp = MHD_create_post_processor (connection, 1024,
631 &post_iterator, request);
632 if (NULL == request->pp)
634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
635 _("Failed to setup post processor for `%s'\n"),
637 return MHD_NO; /* internal error */
641 if (NULL != request->pp)
643 /* evaluate POST data */
644 MHD_post_process (request->pp,
647 if (0 != *upload_data_size)
649 *upload_data_size = 0;
652 /* done with POST data, serve response */
653 MHD_destroy_post_processor (request->pp);
657 GNUNET_CRYPTO_ecc_public_key_from_string (request->public_key,
658 strlen (request->public_key),
662 return fill_s_reply ("Failed to parse given public key",
663 request, connection);
665 switch (request->phase)
668 if (NULL != strchr (request->domain_name, (int) '.'))
670 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
671 _("Domain name must not contain `.'\n"));
672 request->phase = RP_FAIL;
673 return fill_s_reply ("Domain name must not contain `.', sorry.",
674 request, connection);
676 if (NULL != strchr (request->domain_name, (int) '+'))
678 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
679 _("Domain name must not contain `+'\n"));
680 request->phase = RP_FAIL;
681 return fill_s_reply ("Domain name must not contain `+', sorry.",
682 request, connection);
684 request->phase = RP_LOOKUP;
685 GNUNET_CRYPTO_ecc_key_get_public (fcfs_zone_pkey,
687 request->qe = GNUNET_NAMESTORE_lookup (ns,
689 request->domain_name,
690 &lookup_result_processor,
698 return fill_s_reply ("Request failed, sorry.",
699 request, connection);
701 return fill_s_reply ("Success.",
702 request, connection);
707 return MHD_YES; /* will have a reply later... */
709 /* unsupported HTTP method */
710 response = MHD_create_response_from_buffer (strlen (METHOD_ERROR),
711 (void *) METHOD_ERROR,
712 MHD_RESPMEM_PERSISTENT);
713 ret = MHD_queue_response (connection,
714 MHD_HTTP_METHOD_NOT_ACCEPTABLE,
716 MHD_destroy_response (response);
722 * Callback called upon completion of a request.
723 * Decrements session reference counter.
725 * @param cls not used
726 * @param connection connection that completed
727 * @param con_cls session handle
728 * @param toe status code
731 request_completed_callback (void *cls,
732 struct MHD_Connection *connection,
734 enum MHD_RequestTerminationCode toe)
736 struct Request *request = *con_cls;
740 if (NULL != request->pp)
741 MHD_destroy_post_processor (request->pp);
742 if (NULL != request->qe)
743 GNUNET_NAMESTORE_cancel (request->qe);
744 GNUNET_free (request);
748 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
752 * Schedule tasks to run MHD server.
760 struct GNUNET_NETWORK_FDSet *wrs;
761 struct GNUNET_NETWORK_FDSet *wws;
762 struct GNUNET_NETWORK_FDSet *wes;
765 UNSIGNED_MHD_LONG_LONG timeout;
766 struct GNUNET_TIME_Relative tv;
771 wrs = GNUNET_NETWORK_fdset_create ();
772 wes = GNUNET_NETWORK_fdset_create ();
773 wws = GNUNET_NETWORK_fdset_create ();
775 GNUNET_assert (MHD_YES == MHD_get_fdset (httpd, &rs, &ws, &es, &max));
776 haveto = MHD_get_timeout (httpd, &timeout);
777 if (haveto == MHD_YES)
778 tv.rel_value_us = (uint64_t) timeout * 1000LL;
780 tv = GNUNET_TIME_UNIT_FOREVER_REL;
781 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
782 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
783 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
785 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
788 GNUNET_NETWORK_fdset_destroy (wrs);
789 GNUNET_NETWORK_fdset_destroy (wws);
790 GNUNET_NETWORK_fdset_destroy (wes);
795 * Task run whenever HTTP server operations are pending.
798 * @param tc scheduler context
802 const struct GNUNET_SCHEDULER_TaskContext *tc)
804 httpd_task = GNUNET_SCHEDULER_NO_TASK;
811 * Task run on shutdown. Cleans up everything.
814 * @param tc scheduler context
817 do_shutdown (void *cls,
818 const struct GNUNET_SCHEDULER_TaskContext *tc)
820 if (GNUNET_SCHEDULER_NO_TASK != httpd_task)
822 GNUNET_SCHEDULER_cancel (httpd_task);
823 httpd_task = GNUNET_SCHEDULER_NO_TASK;
827 GNUNET_NAMESTORE_disconnect (ns);
832 MHD_stop_daemon (httpd);
835 if (NULL != fcfs_zone_pkey)
837 GNUNET_CRYPTO_ecc_key_free (fcfs_zone_pkey);
838 fcfs_zone_pkey = NULL;
844 * Main function that will be run.
847 * @param args remaining command-line arguments
848 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
849 * @param cfg configuration
852 run (void *cls, char *const *args, const char *cfgfile,
853 const struct GNUNET_CONFIGURATION_Handle *cfg)
856 unsigned long long port;
859 GNUNET_CONFIGURATION_get_value_number (cfg,
864 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
865 "fcfsd", "HTTPPORT");
869 GNUNET_CONFIGURATION_get_value_filename (cfg,
874 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
878 fcfs_zone_pkey = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
879 GNUNET_free (keyfile);
880 if (NULL == fcfs_zone_pkey)
882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
883 _("Failed to read or create private zone key\n"));
886 ns = GNUNET_NAMESTORE_connect (cfg);
889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
890 _("Failed to connect to namestore\n"));
893 httpd = MHD_start_daemon (MHD_USE_DEBUG,
896 &create_response, NULL,
897 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
898 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
899 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
900 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (4 * 1024),
901 MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL,
905 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
906 _("Failed to start HTTP server\n"));
907 GNUNET_NAMESTORE_disconnect (ns);
912 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
918 * The main function for the fcfs daemon.
920 * @param argc number of arguments from the command line
921 * @param argv command line arguments
922 * @return 0 ok, 1 on error
925 main (int argc, char *const *argv)
927 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
928 GNUNET_GETOPT_OPTION_END
933 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
936 GNUNET_log_setup ("fcfsd", "WARNING", NULL);
939 GNUNET_PROGRAM_run (argc, argv, "fcfsd",
940 _("GNUnet GNS first come first serve registration service"),
942 &run, NULL)) ? 0 : 1;
943 GNUNET_free ((void*) argv);
947 /* end of gnunet-gns-fcfsd.c */