2 This file is part of GNUnet.
3 (C) 2012 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
30 * - figure out where this binary should go (is gns the right directory!?)
33 #include <gnunet_util_lib.h>
34 #include <microhttpd.h>
35 #include <gnunet_namestore_service.h>
38 * Invalid method page.
40 #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>"
45 #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? <input type=\"text\" name=\"domain\" /> <p> What is your public key? <input type=\"text\" name=\"pkey\" /> <input type=\"submit\" value=\"Next\" /></body></html>"
50 #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>"
53 * Mime type for HTML pages.
55 #define MIME_HTML "text/html"
60 #define COOKIE_NAME "gns-fcfs"
64 * Phases a request goes through.
69 * Start phase (parsing POST, checking).
74 * Lookup to see if the domain name is taken.
79 * Storing of the record.
84 * We're done with success.
89 * Send failure message.
96 * Data kept per request.
102 * Associated session.
104 struct Session *session;
107 * Post processor handling form data (IF this is
110 struct MHD_PostProcessor *pp;
113 * URL to serve in response to this POST (if this request
116 const char *post_url;
119 * Active request with the namestore.
121 struct GNUNET_NAMESTORE_QueueEntry *qe;
124 * Current processing phase.
129 * Domain name submitted via form.
131 char domain_name[64];
134 * Public key submitted via form.
136 char public_key[1024];
142 * MHD deamon reference.
144 static struct MHD_Daemon *httpd;
149 static GNUNET_SCHEDULER_TaskIdentifier httpd_task;
152 * Handle to the namestore.
154 static struct GNUNET_NAMESTORE_Handle *ns;
157 * Hash of the public key of the fcfsd zone.
159 static GNUNET_HashCode fcfsd_zone;
162 * Private key for the fcfsd zone.
164 static struct GNUNET_CRYPTO_RsaPrivateKey *fcfs_zone_pkey;
168 * Handler that returns a simple static HTTP page.
170 * @param connection connection to use
171 * @return MHD_YES on success
174 serve_main_page (struct MHD_Connection *connection)
177 struct MHD_Response *response;
179 /* return static form */
180 response = MHD_create_response_from_buffer (strlen (MAIN_PAGE),
182 MHD_RESPMEM_PERSISTENT);
183 MHD_add_response_header (response,
184 MHD_HTTP_HEADER_CONTENT_ENCODING,
186 ret = MHD_queue_response (connection,
189 MHD_destroy_response (response);
195 * Send the 'SUBMIT_PAGE'.
197 * @param info information string to send to the user
198 * @param request request information
199 * @param connection connection to use
202 fill_s_reply (const char *info,
203 struct Request *request,
204 struct MHD_Connection *connection)
208 struct MHD_Response *response;
210 GNUNET_asprintf (&reply,
214 /* return static form */
215 response = MHD_create_response_from_buffer (strlen (reply),
217 MHD_RESPMEM_MUST_FREE);
218 MHD_add_response_header (response,
219 MHD_HTTP_HEADER_CONTENT_ENCODING,
221 ret = MHD_queue_response (connection,
224 MHD_destroy_response (response);
230 * Iterator over key-value pairs where the value
231 * maybe made available in increments and/or may
232 * not be zero-terminated. Used for processing
235 * @param cls user-specified closure
236 * @param kind type of the value
237 * @param key 0-terminated key for the value
238 * @param filename name of the uploaded file, NULL if not known
239 * @param content_type mime-type of the data, NULL if not known
240 * @param transfer_encoding encoding of the data, NULL if not known
241 * @param data pointer to size bytes of data at the
243 * @param off offset of data in the overall value
244 * @param size number of bytes in data available
245 * @return MHD_YES to continue iterating,
246 * MHD_NO to abort the iteration
249 post_iterator (void *cls,
250 enum MHD_ValueKind kind,
252 const char *filename,
253 const char *content_type,
254 const char *transfer_encoding,
255 const char *data, uint64_t off, size_t size)
257 struct Request *request = cls;
259 if (0 == strcmp ("domain", key))
261 if (size + off >= sizeof(request->domain_name))
262 size = sizeof (request->domain_name) - off - 1;
263 memcpy (&request->domain_name[off],
266 request->domain_name[size+off] = '\0';
269 if (0 == strcmp ("pkey", key))
271 if (size + off >= sizeof(request->public_key))
272 size = sizeof (request->public_key) - off - 1;
273 memcpy (&request->public_key[off],
276 request->public_key[size+off] = '\0';
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280 _("Unsupported form value `%s'\n"),
287 * Task run whenever HTTP server operations are pending.
290 * @param tc scheduler context
294 const struct GNUNET_SCHEDULER_TaskContext *tc);
298 * Schedule task to run MHD server now.
303 if (GNUNET_SCHEDULER_NO_TASK != httpd_task)
305 GNUNET_SCHEDULER_cancel (httpd_task);
306 httpd_task = GNUNET_SCHEDULER_NO_TASK;
308 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL);
313 * Continuation called to notify client about result of the
317 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
318 * GNUNET_NO if content was already there
319 * GNUNET_YES (or other positive value) on success
320 * @param emsg NULL on success, otherwise an error message
323 put_continuation (void *cls,
327 struct Request *request = cls;
332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
333 _("Failed to create record for domain `%s': %s\n"),
334 request->domain_name,
336 request->phase = RP_FAIL;
339 request->phase = RP_SUCCESS;
345 * Process a record that was stored in the namestore.
348 * @param zone_key public key of the zone
349 * @param expire when does the corresponding block in the DHT expire (until
350 * when should we never do a DHT lookup for the same name again)?;
351 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
352 * or the expiration time of the block in the namestore (even if there are zero
353 * records matching the desired record type)
354 * @param name name that is being mapped (at most 255 characters long)
355 * @param rd_count number of entries in 'rd' array
356 * @param rd array of records with data to store
357 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
358 * because the user queried for a particular record type only)
361 lookup_result_processor (void *cls,
362 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
363 struct GNUNET_TIME_Absolute expire,
365 unsigned int rd_count,
366 const struct GNUNET_NAMESTORE_RecordData *rd,
367 const struct GNUNET_CRYPTO_RsaSignature *signature)
369 struct Request *request = cls;
370 struct GNUNET_NAMESTORE_RecordData r;
373 GNUNET_assert (GNUNET_OK ==
374 GNUNET_CRYPTO_hash_from_string2 (request->public_key,
375 strlen (request->public_key),
380 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
381 _("Found %u existing records for domain `%s'\n"),
383 request->domain_name);
384 request->phase = RP_FAIL;
389 r.data_size = sizeof (pub);
390 r.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
391 r.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
392 r.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
393 request->qe = GNUNET_NAMESTORE_record_create (ns,
395 request->domain_name,
403 * Main MHD callback for handling requests.
406 * @param connection MHD connection handle
407 * @param url the requested url
408 * @param method the HTTP method used ("GET", "PUT", etc.)
409 * @param version the HTTP version string (i.e. "HTTP/1.1")
410 * @param upload_data the data being uploaded (excluding HEADERS,
411 * for a POST that fits into memory and that is encoded
412 * with a supported encoding, the POST data will NOT be
413 * given in upload_data and is instead available as
414 * part of MHD_get_connection_values; very large POST
415 * data *will* be made available incrementally in
417 * @param upload_data_size set initially to the size of the
418 * upload_data provided; the method must update this
419 * value to the number of bytes NOT processed;
420 * @param ptr pointer to location where we store the 'struct Request'
421 * @return MHS_YES if the connection was handled successfully,
422 * MHS_NO if the socket must be closed due to a serios
423 * error while handling the request
426 create_response (void *cls,
427 struct MHD_Connection *connection,
431 const char *upload_data,
432 size_t *upload_data_size,
435 struct MHD_Response *response;
436 struct Request *request;
440 if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
441 (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
443 ret = serve_main_page (connection);
445 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
446 _("Failed to create page for `%s'\n"),
450 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
455 request = GNUNET_malloc (sizeof (struct Request));
457 request->pp = MHD_create_post_processor (connection, 1024,
458 &post_iterator, request);
459 if (NULL == request->pp)
461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
462 _("Failed to setup post processor for `%s'\n"),
464 return MHD_NO; /* internal error */
468 if (NULL != request->pp)
470 /* evaluate POST data */
471 MHD_post_process (request->pp,
474 if (0 != *upload_data_size)
476 *upload_data_size = 0;
479 /* done with POST data, serve response */
480 MHD_destroy_post_processor (request->pp);
484 GNUNET_CRYPTO_hash_from_string2 (request->public_key,
485 strlen (request->public_key),
489 return fill_s_reply ("Failed to parse given public key",
490 request, connection);
492 switch (request->phase)
495 request->phase = RP_LOOKUP;
496 request->qe = GNUNET_NAMESTORE_lookup_record (ns,
498 request->domain_name,
499 GNUNET_NAMESTORE_TYPE_PKEY,
500 &lookup_result_processor,
508 return fill_s_reply ("Request failed, sorry.",
509 request, connection);
511 return fill_s_reply ("Success.",
512 request, connection);
517 return MHD_YES; /* will have a reply later... */
519 /* unsupported HTTP method */
520 response = MHD_create_response_from_buffer (strlen (METHOD_ERROR),
521 (void *) METHOD_ERROR,
522 MHD_RESPMEM_PERSISTENT);
523 ret = MHD_queue_response (connection,
524 MHD_HTTP_METHOD_NOT_ACCEPTABLE,
526 MHD_destroy_response (response);
532 * Callback called upon completion of a request.
533 * Decrements session reference counter.
535 * @param cls not used
536 * @param connection connection that completed
537 * @param con_cls session handle
538 * @param toe status code
541 request_completed_callback (void *cls,
542 struct MHD_Connection *connection,
544 enum MHD_RequestTerminationCode toe)
546 struct Request *request = *con_cls;
550 if (NULL != request->pp)
551 MHD_destroy_post_processor (request->pp);
552 if (NULL != request->qe)
553 GNUNET_NAMESTORE_cancel (request->qe);
554 GNUNET_free (request);
559 * Schedule tasks to run MHD server.
567 struct GNUNET_NETWORK_FDSet *wrs;
568 struct GNUNET_NETWORK_FDSet *wws;
569 struct GNUNET_NETWORK_FDSet *wes;
572 unsigned MHD_LONG_LONG timeout;
573 struct GNUNET_TIME_Relative tv;
578 wrs = GNUNET_NETWORK_fdset_create ();
579 wes = GNUNET_NETWORK_fdset_create ();
580 wws = GNUNET_NETWORK_fdset_create ();
582 GNUNET_assert (MHD_YES == MHD_get_fdset (httpd, &rs, &ws, &es, &max));
583 haveto = MHD_get_timeout (httpd, &timeout);
584 if (haveto == MHD_YES)
585 tv.rel_value = (uint64_t) timeout;
587 tv = GNUNET_TIME_UNIT_FOREVER_REL;
588 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
589 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
590 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
592 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
593 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
595 GNUNET_NETWORK_fdset_destroy (wrs);
596 GNUNET_NETWORK_fdset_destroy (wws);
597 GNUNET_NETWORK_fdset_destroy (wes);
602 * Task run whenever HTTP server operations are pending.
605 * @param tc scheduler context
609 const struct GNUNET_SCHEDULER_TaskContext *tc)
611 httpd_task = GNUNET_SCHEDULER_NO_TASK;
618 * Task run on shutdown. Cleans up everything.
621 * @param tc scheduler context
624 do_shutdown (void *cls,
625 const struct GNUNET_SCHEDULER_TaskContext *tc)
627 if (GNUNET_SCHEDULER_NO_TASK != httpd_task)
629 GNUNET_SCHEDULER_cancel (httpd_task);
630 httpd_task = GNUNET_SCHEDULER_NO_TASK;
634 GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
639 MHD_stop_daemon (httpd);
642 if (NULL != fcfs_zone_pkey)
644 GNUNET_CRYPTO_rsa_key_free (fcfs_zone_pkey);
645 fcfs_zone_pkey = NULL;
651 * Main function that will be run.
654 * @param args remaining command-line arguments
655 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
656 * @param cfg configuration
659 run (void *cls, char *const *args, const char *cfgfile,
660 const struct GNUNET_CONFIGURATION_Handle *cfg)
663 unsigned long long port;
664 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
667 GNUNET_CONFIGURATION_get_value_number (cfg,
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 _("Option `%s' not specified in configuration section `%s'\n"),
679 GNUNET_CONFIGURATION_get_value_filename (cfg,
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685 _("Option `%s' not specified in configuration section `%s'\n"),
690 fcfs_zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
691 GNUNET_free (keyfile);
692 if (NULL == fcfs_zone_pkey)
694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
695 _("Failed to read or create private zone key\n"));
698 GNUNET_CRYPTO_rsa_key_get_public (fcfs_zone_pkey,
700 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &fcfsd_zone);
701 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
702 _("Managing `%s' as FCFS zone on port %llu\n"),
703 GNUNET_h2s_full (&fcfsd_zone),
705 ns = GNUNET_NAMESTORE_connect (cfg);
708 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
709 _("Failed to connect to namestore\n"));
712 httpd = MHD_start_daemon (MHD_USE_DEBUG,
715 &create_response, NULL,
716 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
717 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
718 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
719 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (4 * 1024),
720 MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL,
724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
725 _("Failed to start HTTP server\n"));
726 GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
731 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
737 * The main function for the fcfs daemon.
739 * @param argc number of arguments from the command line
740 * @param argv command line arguments
741 * @return 0 ok, 1 on error
744 main (int argc, char *const *argv)
746 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
747 GNUNET_GETOPT_OPTION_END
752 GNUNET_log_setup ("fcfsd", "WARNING", NULL);
755 GNUNET_PROGRAM_run (argc, argv, "fcfsd",
756 _("GNUnet GNS first come first serve registration service"),
758 &run, NULL)) ? 0 : 1;
763 /* end of gnunet-gns-fcfsd.c */