From 78f323a26ba33c640162741d4f44b6996239b0e8 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 13 Aug 2013 22:00:45 +0000 Subject: -adapting fcfsd to new namestore API, moving from gns to namestore subsystem --- doc/man/Makefile.am | 2 +- doc/man/gnunet-gns-fcfsd.1 | 38 -- doc/man/gnunet-gns.1 | 11 +- doc/man/gnunet-namestore-fcfsd.1 | 38 ++ src/gns/Makefile.am | 10 - src/gns/gnunet-gns-fcfsd.c | 980 --------------------------------- src/gns/gnunet-service-gns_resolver.c | 55 +- src/include/gnunet_namestore_service.h | 39 +- src/namestore/Makefile.am | 14 +- src/namestore/gnunet-namestore-fcfsd.c | 947 +++++++++++++++++++++++++++++++ src/namestore/namestore_api.c | 20 + src/namestore/namestore_api_common.c | 2 +- 12 files changed, 1080 insertions(+), 1076 deletions(-) delete mode 100644 doc/man/gnunet-gns-fcfsd.1 create mode 100644 doc/man/gnunet-namestore-fcfsd.1 delete mode 100644 src/gns/gnunet-gns-fcfsd.c create mode 100644 src/namestore/gnunet-namestore-fcfsd.c diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am index db26f0d6a..9e63bb243 100644 --- a/doc/man/Makefile.am +++ b/doc/man/Makefile.am @@ -13,10 +13,10 @@ man_MANS = \ gnunet-ecc.1 \ gnunet-fs.1 \ gnunet-gns.1 \ - gnunet-gns-fcfsd.1 \ gnunet-gns-proxy.1 \ gnunet-identity.1 \ gnunet-namestore.1 \ + gnunet-namestore-fcfsd.1 \ gnunet-nat-server.1 \ gnunet-peerinfo.1 \ gnunet-pseudonym.1 \ diff --git a/doc/man/gnunet-gns-fcfsd.1 b/doc/man/gnunet-gns-fcfsd.1 deleted file mode 100644 index fbd45e4a2..000000000 --- a/doc/man/gnunet-gns-fcfsd.1 +++ /dev/null @@ -1,38 +0,0 @@ -.TH GNUNET\-GNS-FCFSD 1 "Oct 25, 2012" "GNUnet" - -.SH NAME -gnunet\-gns-fcfsd \- HTTP server for GADS domain registration - -.SH SYNOPSIS -.B gnunet\-gns-fcfsd -.RI [ options ] -.br - -.SH DESCRIPTION -Most users will not want to run an FCFS\-zone and thus will not need this program. - -\fBgnunet\-gns-fcfsd\fP runs a web server where users can register names to be mapped to their GADS zone. Names are made available on a First Come First Served basis (hence fcfs). Registered names do not expire. The HTTP server is run on the port that is specified in the configuration file in section "[fcfsd]" under the name "HTTPPORT". The key of the zone in which the names are registered must be specified under the name "ZONEKEY" in the same section. It is possible to manage gnunet\-gns\-fcfsd using gnunet\-(service\-arm) by starting the daemon using "gnunet\-arm -i fcfsd" or by adding "fcfds" to the "DEFAULTSERVICES" option. - -An FCFS\-zone is run at http://gnunet.org/fcfs/. The respective zone key can be imported into an individual user's zone using "gnunet-gns-import.sh". GADS users are encouraged to register their zone with the gnunet.org FCFS authority. - -.SH OPTIONS -.B -.IP "\-c FILENAME, \-\-config=FILENAME" -Use the configuration file FILENAME. -.B -.IP "\-h, \-\-help" -Print short help on options. -.B -.IP "\-L LOGLEVEL, \-\-loglevel=LOGLEVEL" -Use LOGLEVEL for logging. Valid values are DEBUG, INFO, WARNING and ERROR. -.B -.IP "\-v, \-\-version" -Print GNUnet version number. - - -.SH BUGS -Report bugs by using Mantis or by sending electronic mail to - -.SH SEE ALSO -gnunet\-gns(1), gnunet\-gns\-proxy(1) - diff --git a/doc/man/gnunet-gns.1 b/doc/man/gnunet-gns.1 index ef5a0f9bc..e5e2240b9 100644 --- a/doc/man/gnunet-gns.1 +++ b/doc/man/gnunet-gns.1 @@ -1,4 +1,4 @@ -.TH GNUNET\-GNS 1 "Aug 8, 2012" "GNUnet" +.TH GNUNET\-GNS 1 "Aug 8, 2013" "GNUnet" .SH NAME gnunet\-gns \- Access to GNUnet Name Service @@ -13,10 +13,6 @@ gnunet\-gns \- Access to GNUnet Name Service .SH OPTIONS .B -.IP "\-a NAME, \-\-authority=NAME" -Get the authority of a particular name. -For example the authority for "www.fcfs.gads" is "fcfs.gads". -.B .IP "\-c FILENAME, \-\-config=FILENAME" Use the configuration file FILENAME. .B @@ -26,11 +22,6 @@ This is a quiet mode where only important information is displayed. For example a lookup for an IP address will only yield the IP address, no descriptive text. .B -.IP "\-s NAME, \-\-shorten NAME" -Shorten GNUnet Name Service Name. -The service will try to shorten the delegation chain of the name if a "closer" -authority chain exists relative to your local root zone. -.B .IP "\-t RRTYPE, \-\-type=RRTYPE" Resource Record Type (RRTYPE) to look for. Supported RRTYPE's are: A, AAAA, CNAME, NS, PKEY, PSEU, TLSA, SRV, SOA, MX, LEHO, VPN, REV, PTR, TXT diff --git a/doc/man/gnunet-namestore-fcfsd.1 b/doc/man/gnunet-namestore-fcfsd.1 new file mode 100644 index 000000000..42b658018 --- /dev/null +++ b/doc/man/gnunet-namestore-fcfsd.1 @@ -0,0 +1,38 @@ +.TH GNUNET\-NAMESTORE-FCFSD 1 "Oct 25, 2012" "GNUnet" + +.SH NAME +gnunet\-namestore-fcfsd \- HTTP server for GNS domain registration + +.SH SYNOPSIS +.B gnunet\-namestore-fcfsd +.RI [ options ] +.br + +.SH DESCRIPTION +Most users will not want to run an FCFS\-zone and thus will not need this program. + +\fBgnunet\-gns-fcfsd\fP runs a web server where users can register names to be mapped to their GADS zone. Names are made available on a First Come First Served basis (hence fcfs). Registered names do not expire. The HTTP server is run on the port that is specified in the configuration file in section "[fcfsd]" under the name "HTTPPORT". The key of the zone in which the names are registered must be specified under the name "ZONEKEY" in the same section. It is possible to manage gnunet\-gns\-fcfsd using gnunet\-(service\-arm) by starting the daemon using "gnunet\-arm -i fcfsd" or by adding "fcfds" to the "DEFAULTSERVICES" option. + +An FCFS\-zone is run at http://gnunet.org/fcfs/. The respective zone key can be imported into an individual user's zone using "gnunet-gns-import.sh". GADS users are encouraged to register their zone with the gnunet.org FCFS authority. + +.SH OPTIONS +.B +.IP "\-c FILENAME, \-\-config=FILENAME" +Use the configuration file FILENAME. +.B +.IP "\-h, \-\-help" +Print short help on options. +.B +.IP "\-L LOGLEVEL, \-\-loglevel=LOGLEVEL" +Use LOGLEVEL for logging. Valid values are DEBUG, INFO, WARNING and ERROR. +.B +.IP "\-v, \-\-version" +Print GNUnet version number. + + +.SH BUGS +Report bugs by using Mantis or by sending electronic mail to + +.SH SEE ALSO +gnunet\-gns(1), gnunet\-gns\-proxy(1) + diff --git a/src/gns/Makefile.am b/src/gns/Makefile.am index 16bd6ca1c..e91014021 100644 --- a/src/gns/Makefile.am +++ b/src/gns/Makefile.am @@ -154,16 +154,6 @@ gnunet_service_gns_DEPENDENCIES = \ -gnunet_gns_fcfsd_SOURCES = \ - gnunet-gns-fcfsd.c -gnunet_gns_fcfsd_LDADD = -lmicrohttpd \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(top_builddir)/src/namestore/libgnunetnamestore.la \ - $(GN_LIBINTL) -gnunet_gns_fcfsd_DEPENDENCIES = \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(top_builddir)/src/namestore/libgnunetnamestore.la - libw32nsp_la_SOURCES = \ w32nsp.c libw32nsp_la_LIBADD = \ diff --git a/src/gns/gnunet-gns-fcfsd.c b/src/gns/gnunet-gns-fcfsd.c deleted file mode 100644 index dd4572cb1..000000000 --- a/src/gns/gnunet-gns-fcfsd.c +++ /dev/null @@ -1,980 +0,0 @@ -/* - This file is part of GNUnet. - (C) 2012 Christian Grothoff (and other contributing authors) - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. -*/ -/** - * @file gnunet-gns-fcfsd.c - * @brief HTTP daemon that offers first-come-first-serve GNS domain registration - * @author Christian Grothoff - * - * TODO: - * - the code currently contains a 'race' between checking that the - * domain name is available and allocating it to the new public key - * (should this race be solved by namestore or by fcfsd?) - * - nicer error reporting to browser - * - figure out where this binary should go (is gns the right directory!?) - */ -#include "platform.h" -#include -#include -#include - -/** - * Invalid method page. - */ -#define METHOD_ERROR "Illegal requestGo away." - -/** - * Front page. (/) - */ -#define MAIN_PAGE "GNUnet FCFS Authority Name Registration Service
What is your desired domain name? (at most 63 lowercase characters, no dots allowed.)

What is your public key? (Copy from gnunet-setup.)
List of all registered names " - -/** - * Second page (/S) - */ -#define SUBMIT_PAGE "%s%s" - -/** - * Fcfs zoneinfo page (/Zoneinfo) - */ -#define ZONEINFO_PAGE "FCFS Zoneinfo

FCFS Zoneinfo

%s
namePKEY
" - -#define FCFS_ZONEINFO_URL "/Zoneinfo" - -/** - * Mime type for HTML pages. - */ -#define MIME_HTML "text/html" - -/** - * Name of our cookie. - */ -#define COOKIE_NAME "gns-fcfs" - -#define DEFAULT_ZONEINFO_BUFSIZE 2048 - -/** - * Phases a request goes through. - */ -enum Phase - { - /** - * Start phase (parsing POST, checking). - */ - RP_START = 0, - - /** - * Lookup to see if the domain name is taken. - */ - RP_LOOKUP, - - /** - * Storing of the record. - */ - RP_PUT, - - /** - * We're done with success. - */ - RP_SUCCESS, - - /** - * Send failure message. - */ - RP_FAIL - }; - - -/** - * Data kept per request. - */ -struct Request -{ - - /** - * Associated session. - */ - struct Session *session; - - /** - * Post processor handling form data (IF this is - * a POST request). - */ - struct MHD_PostProcessor *pp; - - /** - * URL to serve in response to this POST (if this request - * was a 'POST') - */ - const char *post_url; - - /** - * Active request with the namestore. - */ - struct GNUNET_NAMESTORE_QueueEntry *qe; - - /** - * Current processing phase. - */ - enum Phase phase; - - /** - * Domain name submitted via form. - */ - char domain_name[64]; - - /** - * Public key submitted via form. - */ - char public_key[64]; - -}; - -/** - * Zoneinfo request - */ -struct ZoneinfoRequest -{ - /** - * Connection - */ - struct MHD_Connection *connection; - - /** - * List iterator - */ - struct GNUNET_NAMESTORE_ZoneIterator *list_it; - - /** - * Buffer - */ - char* zoneinfo; - - /** - * Buffer length - */ - size_t buf_len; - - /** - * Buffer write offset - */ - size_t write_offset; -}; - -/** - * MHD deamon reference. - */ -static struct MHD_Daemon *httpd; - -/** - * Main HTTP task. - */ -static GNUNET_SCHEDULER_TaskIdentifier httpd_task; - -/** - * Handle to the namestore. - */ -static struct GNUNET_NAMESTORE_Handle *ns; - -/** - * Hash of the public key of the fcfsd zone. - */ -static struct GNUNET_CRYPTO_ShortHashCode fcfsd_zone; - -/** - * Private key for the fcfsd zone. - */ -static struct GNUNET_CRYPTO_EccPrivateKey *fcfs_zone_pkey; - - -/** - * Task run whenever HTTP server operations are pending. - * - * @param cls unused - * @param tc scheduler context - */ -static void -do_httpd (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc); - - -/** - * Schedule task to run MHD server now. - */ -static void -run_httpd_now () -{ - if (GNUNET_SCHEDULER_NO_TASK != httpd_task) - { - GNUNET_SCHEDULER_cancel (httpd_task); - httpd_task = GNUNET_SCHEDULER_NO_TASK; - } - httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL); -} - -static void -iterate_cb (void *cls, - const struct GNUNET_CRYPTO_EccPublicKey *zone_key, - struct GNUNET_TIME_Absolute expire, - const char *name, - unsigned int rd_len, - const struct GNUNET_NAMESTORE_RecordData *rd, - const struct GNUNET_CRYPTO_EccSignature *signature) -{ - struct ZoneinfoRequest *zr = cls; - struct MHD_Response *response; - char* full_page; - size_t bytes_free; - char* pkey; - char* new_buf; - - - if (NULL == name) - { - zr->list_it = NULL; - - /* return static form */ - GNUNET_asprintf (&full_page, - ZONEINFO_PAGE, - zr->zoneinfo, - zr->zoneinfo); - response = MHD_create_response_from_buffer (strlen (full_page), - (void *) full_page, - MHD_RESPMEM_MUST_FREE); - MHD_add_response_header (response, - MHD_HTTP_HEADER_CONTENT_TYPE, - MIME_HTML); - MHD_queue_response (zr->connection, - MHD_HTTP_OK, - response); - MHD_destroy_response (response); - GNUNET_free (zr->zoneinfo); - GNUNET_free (zr); - run_httpd_now (); - return; - } - - if (1 != rd_len) - { - GNUNET_NAMESTORE_zone_iterator_next (zr->list_it); - return; - } - - if (GNUNET_NAMESTORE_TYPE_PKEY != rd->record_type) - { - GNUNET_NAMESTORE_zone_iterator_next (zr->list_it); - return; - } - - bytes_free = zr->buf_len - zr->write_offset; - pkey = GNUNET_NAMESTORE_value_to_string (rd->record_type, - rd->data, - rd->data_size); - - if (bytes_free < (strlen (name) + strlen (pkey) + 40)) - { - new_buf = GNUNET_malloc (zr->buf_len * 2); - memcpy (new_buf, zr->zoneinfo, zr->write_offset); - GNUNET_free (zr->zoneinfo); - zr->zoneinfo = new_buf; - zr->buf_len *= 2; - } - sprintf (zr->zoneinfo + zr->write_offset, - "%s%s", - name, - pkey); - zr->write_offset = strlen (zr->zoneinfo); - GNUNET_NAMESTORE_zone_iterator_next (zr->list_it); - GNUNET_free (pkey); -} - - - -/** - * Handler that returns FCFS zoneinfo page. - * - * @param connection connection to use - * @return MHD_YES on success - */ -static int -serve_zoneinfo_page (struct MHD_Connection *connection) -{ - struct ZoneinfoRequest *zr; - - zr = GNUNET_malloc (sizeof (struct ZoneinfoRequest)); - - zr->zoneinfo = GNUNET_malloc (DEFAULT_ZONEINFO_BUFSIZE); - zr->buf_len = DEFAULT_ZONEINFO_BUFSIZE; - zr->connection = connection; - zr->write_offset = 0; - - printf ("adsadad1!\n"); - zr->list_it = GNUNET_NAMESTORE_zone_iteration_start (ns, - &fcfsd_zone, - GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION, - GNUNET_NAMESTORE_RF_PRIVATE, - &iterate_cb, - zr); - - return MHD_YES; -} - - -/** - * Handler that returns a simple static HTTP page. - * - * @param connection connection to use - * @return MHD_YES on success - */ -static int -serve_main_page (struct MHD_Connection *connection) -{ - int ret; - struct MHD_Response *response; - - /* return static form */ - response = MHD_create_response_from_buffer (strlen (MAIN_PAGE), - (void *) MAIN_PAGE, - MHD_RESPMEM_PERSISTENT); - MHD_add_response_header (response, - MHD_HTTP_HEADER_CONTENT_TYPE, - MIME_HTML); - ret = MHD_queue_response (connection, - MHD_HTTP_OK, - response); - MHD_destroy_response (response); - return ret; -} - - -/** - * Send the 'SUBMIT_PAGE'. - * - * @param info information string to send to the user - * @param request request information - * @param connection connection to use - */ -static int -fill_s_reply (const char *info, - struct Request *request, - struct MHD_Connection *connection) -{ - int ret; - char *reply; - struct MHD_Response *response; - - GNUNET_asprintf (&reply, - SUBMIT_PAGE, - info, - info); - /* return static form */ - response = MHD_create_response_from_buffer (strlen (reply), - (void *) reply, - MHD_RESPMEM_MUST_FREE); - MHD_add_response_header (response, - MHD_HTTP_HEADER_CONTENT_TYPE, - MIME_HTML); - ret = MHD_queue_response (connection, - MHD_HTTP_OK, - response); - MHD_destroy_response (response); - return ret; -} - - -/** - * Iterator over key-value pairs where the value - * maybe made available in increments and/or may - * not be zero-terminated. Used for processing - * POST data. - * - * @param cls user-specified closure - * @param kind type of the value - * @param key 0-terminated key for the value - * @param filename name of the uploaded file, NULL if not known - * @param content_type mime-type of the data, NULL if not known - * @param transfer_encoding encoding of the data, NULL if not known - * @param data pointer to size bytes of data at the - * specified offset - * @param off offset of data in the overall value - * @param size number of bytes in data available - * @return MHD_YES to continue iterating, - * MHD_NO to abort the iteration - */ -static int -post_iterator (void *cls, - enum MHD_ValueKind kind, - const char *key, - const char *filename, - const char *content_type, - const char *transfer_encoding, - const char *data, uint64_t off, size_t size) -{ - struct Request *request = cls; - - if (0 == strcmp ("domain", key)) - { - if (size + off >= sizeof(request->domain_name)) - size = sizeof (request->domain_name) - off - 1; - memcpy (&request->domain_name[off], - data, - size); - request->domain_name[size+off] = '\0'; - return MHD_YES; - } - if (0 == strcmp ("pkey", key)) - { - if (size + off >= sizeof(request->public_key)) - size = sizeof (request->public_key) - off - 1; - memcpy (&request->public_key[off], - data, - size); - request->public_key[size+off] = '\0'; - return MHD_YES; - } - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Unsupported form value `%s'\n"), - key); - return MHD_YES; -} - - - - -/** - * Continuation called to notify client about result of the - * operation. - * - * @param cls closure - * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate) - * GNUNET_NO if content was already there - * GNUNET_YES (or other positive value) on success - * @param emsg NULL on success, otherwise an error message - */ -static void -put_continuation (void *cls, - int32_t success, - const char *emsg) -{ - struct Request *request = cls; - - request->qe = NULL; - if (0 >= success) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Failed to create record for domain `%s': %s\n"), - request->domain_name, - emsg); - request->phase = RP_FAIL; - } - else - request->phase = RP_SUCCESS; - run_httpd_now (); -} - - -/** - * Test if a name mapping was found, if so, refuse. If not, initiate storing of the record. - * - * @param cls closure - * @param zone_key public key of the zone - * @param expire when does the corresponding block in the DHT expire (until - * when should we never do a DHT lookup for the same name again)?; - * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore, - * or the expiration time of the block in the namestore (even if there are zero - * records matching the desired record type) - * @param name name that is being mapped (at most 255 characters long) - * @param rd_count number of entries in 'rd' array - * @param rd array of records with data to store - * @param signature signature of the record block, NULL if signature is unavailable (i.e. - * because the user queried for a particular record type only) - */ -static void -zone_to_name_cb (void *cls, - const struct GNUNET_CRYPTO_EccPublicKey *zone_key, - struct GNUNET_TIME_Absolute expire, - const char *name, - unsigned int rd_count, - const struct GNUNET_NAMESTORE_RecordData *rd, - const struct GNUNET_CRYPTO_EccSignature *signature) -{ - struct Request *request = cls; - struct GNUNET_NAMESTORE_RecordData r; - struct GNUNET_CRYPTO_ShortHashCode pub; - - request->qe = NULL; - if (NULL != name) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Found existing name `%s' for the given key\n"), - name); - request->phase = RP_FAIL; - run_httpd_now (); - return; - } - GNUNET_assert (GNUNET_OK == - GNUNET_CRYPTO_short_hash_from_string2 (request->public_key, - strlen (request->public_key), - &pub)); - r.data = &pub; - r.data_size = sizeof (pub); - r.expiration_time = UINT64_MAX; - r.record_type = GNUNET_NAMESTORE_TYPE_PKEY; - r.flags = GNUNET_NAMESTORE_RF_AUTHORITY; - request->qe = GNUNET_NAMESTORE_record_put_by_authority (ns, - fcfs_zone_pkey, - request->domain_name, - 1, - &r, - &put_continuation, - request); -} - - -/** - * Process a record that was stored in the namestore. Used to check if - * the requested name already exists in the namestore. If not, - * proceed to check if the requested key already exists. - * - * @param cls closure - * @param zone_key public key of the zone - * @param expire when does the corresponding block in the DHT expire (until - * when should we never do a DHT lookup for the same name again)?; - * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore, - * or the expiration time of the block in the namestore (even if there are zero - * records matching the desired record type) - * @param name name that is being mapped (at most 255 characters long) - * @param rd_count number of entries in 'rd' array - * @param rd array of records with data to store - * @param signature signature of the record block, NULL if signature is unavailable (i.e. - * because the user queried for a particular record type only) - */ -static void -lookup_result_processor (void *cls, - const struct GNUNET_CRYPTO_EccPublicKey *zone_key, - struct GNUNET_TIME_Absolute expire, - const char *name, - unsigned int rd_count, - const struct GNUNET_NAMESTORE_RecordData *rd, - const struct GNUNET_CRYPTO_EccSignature *signature) -{ - struct Request *request = cls; - struct GNUNET_CRYPTO_ShortHashCode pub; - - request->qe = NULL; - GNUNET_assert (GNUNET_OK == - GNUNET_CRYPTO_short_hash_from_string2 (request->public_key, - strlen (request->public_key), - &pub)); - if (0 != rd_count) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Found %u existing records for domain `%s'\n"), - rd_count, - request->domain_name); - request->phase = RP_FAIL; - run_httpd_now (); - return; - } - request->qe = GNUNET_NAMESTORE_zone_to_name (ns, - &fcfsd_zone, - &pub, - &zone_to_name_cb, - request); -} - - -/** - * Main MHD callback for handling requests. - * - * @param cls unused - * @param connection MHD connection handle - * @param url the requested url - * @param method the HTTP method used ("GET", "PUT", etc.) - * @param version the HTTP version string (i.e. "HTTP/1.1") - * @param upload_data the data being uploaded (excluding HEADERS, - * for a POST that fits into memory and that is encoded - * with a supported encoding, the POST data will NOT be - * given in upload_data and is instead available as - * part of MHD_get_connection_values; very large POST - * data *will* be made available incrementally in - * upload_data) - * @param upload_data_size set initially to the size of the - * upload_data provided; the method must update this - * value to the number of bytes NOT processed; - * @param ptr pointer to location where we store the 'struct Request' - * @return MHD_YES if the connection was handled successfully, - * MHD_NO if the socket must be closed due to a serious - * error while handling the request - */ -static int -create_response (void *cls, - struct MHD_Connection *connection, - const char *url, - const char *method, - const char *version, - const char *upload_data, - size_t *upload_data_size, - void **ptr) -{ - struct MHD_Response *response; - struct Request *request; - int ret; - struct GNUNET_CRYPTO_ShortHashCode pub; - - if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) || - (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) ) - { - if (0 == strcmp (url, FCFS_ZONEINFO_URL)) - ret = serve_zoneinfo_page (connection); - else - ret = serve_main_page (connection); - if (ret != MHD_YES) - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to create page for `%s'\n"), - url); - return ret; - } - if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) - { - request = *ptr; - if (NULL == request) - { - request = GNUNET_malloc (sizeof (struct Request)); - *ptr = request; - request->pp = MHD_create_post_processor (connection, 1024, - &post_iterator, request); - if (NULL == request->pp) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to setup post processor for `%s'\n"), - url); - return MHD_NO; /* internal error */ - } - return MHD_YES; - } - if (NULL != request->pp) - { - /* evaluate POST data */ - MHD_post_process (request->pp, - upload_data, - *upload_data_size); - if (0 != *upload_data_size) - { - *upload_data_size = 0; - return MHD_YES; - } - /* done with POST data, serve response */ - MHD_destroy_post_processor (request->pp); - request->pp = NULL; - } - if (GNUNET_OK != - GNUNET_CRYPTO_short_hash_from_string2 (request->public_key, - strlen (request->public_key), - &pub)) - { - /* parse error */ - return fill_s_reply ("Failed to parse given public key", - request, connection); - } - switch (request->phase) - { - case RP_START: - if (NULL != strchr (request->domain_name, (int) '.')) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Domain name must not contain `.'\n")); - request->phase = RP_FAIL; - return fill_s_reply ("Domain name must not contain `.', sorry.", - request, connection); - } - if (NULL != strchr (request->domain_name, (int) '+')) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - _("Domain name must not contain `+'\n")); - request->phase = RP_FAIL; - return fill_s_reply ("Domain name must not contain `+', sorry.", - request, connection); - } - request->phase = RP_LOOKUP; - request->qe = GNUNET_NAMESTORE_lookup_record (ns, - &fcfsd_zone, - request->domain_name, - GNUNET_NAMESTORE_TYPE_PKEY, - &lookup_result_processor, - request); - break; - case RP_LOOKUP: - break; - case RP_PUT: - break; - case RP_FAIL: - return fill_s_reply ("Request failed, sorry.", - request, connection); - case RP_SUCCESS: - return fill_s_reply ("Success.", - request, connection); - default: - GNUNET_break (0); - return MHD_NO; - } - return MHD_YES; /* will have a reply later... */ - } - /* unsupported HTTP method */ - response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), - (void *) METHOD_ERROR, - MHD_RESPMEM_PERSISTENT); - ret = MHD_queue_response (connection, - MHD_HTTP_METHOD_NOT_ACCEPTABLE, - response); - MHD_destroy_response (response); - return ret; -} - - -/** - * Callback called upon completion of a request. - * Decrements session reference counter. - * - * @param cls not used - * @param connection connection that completed - * @param con_cls session handle - * @param toe status code - */ -static void -request_completed_callback (void *cls, - struct MHD_Connection *connection, - void **con_cls, - enum MHD_RequestTerminationCode toe) -{ - struct Request *request = *con_cls; - - if (NULL == request) - return; - if (NULL != request->pp) - MHD_destroy_post_processor (request->pp); - if (NULL != request->qe) - GNUNET_NAMESTORE_cancel (request->qe); - GNUNET_free (request); -} - - -#define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG - - -/** - * Schedule tasks to run MHD server. - */ -static void -run_httpd () -{ - fd_set rs; - fd_set ws; - fd_set es; - struct GNUNET_NETWORK_FDSet *wrs; - struct GNUNET_NETWORK_FDSet *wws; - struct GNUNET_NETWORK_FDSet *wes; - int max; - int haveto; - UNSIGNED_MHD_LONG_LONG timeout; - struct GNUNET_TIME_Relative tv; - - FD_ZERO (&rs); - FD_ZERO (&ws); - FD_ZERO (&es); - wrs = GNUNET_NETWORK_fdset_create (); - wes = GNUNET_NETWORK_fdset_create (); - wws = GNUNET_NETWORK_fdset_create (); - max = -1; - GNUNET_assert (MHD_YES == MHD_get_fdset (httpd, &rs, &ws, &es, &max)); - haveto = MHD_get_timeout (httpd, &timeout); - if (haveto == MHD_YES) - tv.rel_value_us = (uint64_t) timeout * 1000LL; - else - tv = GNUNET_TIME_UNIT_FOREVER_REL; - GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1); - GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1); - GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1); - httpd_task = - GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, - tv, wrs, wws, - &do_httpd, NULL); - GNUNET_NETWORK_fdset_destroy (wrs); - GNUNET_NETWORK_fdset_destroy (wws); - GNUNET_NETWORK_fdset_destroy (wes); -} - - -/** - * Task run whenever HTTP server operations are pending. - * - * @param cls unused - * @param tc scheduler context - */ -static void -do_httpd (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - httpd_task = GNUNET_SCHEDULER_NO_TASK; - MHD_run (httpd); - run_httpd (); -} - - -/** - * Task run on shutdown. Cleans up everything. - * - * @param cls unused - * @param tc scheduler context - */ -static void -do_shutdown (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if (GNUNET_SCHEDULER_NO_TASK != httpd_task) - { - GNUNET_SCHEDULER_cancel (httpd_task); - httpd_task = GNUNET_SCHEDULER_NO_TASK; - } - if (NULL != ns) - { - GNUNET_NAMESTORE_disconnect (ns); - ns = NULL; - } - if (NULL != httpd) - { - MHD_stop_daemon (httpd); - httpd = NULL; - } - if (NULL != fcfs_zone_pkey) - { - GNUNET_CRYPTO_ecc_key_free (fcfs_zone_pkey); - fcfs_zone_pkey = NULL; - } -} - - -/** - * Main function that will be run. - * - * @param cls closure - * @param args remaining command-line arguments - * @param cfgfile name of the configuration file used (for saving, can be NULL!) - * @param cfg configuration - */ -static void -run (void *cls, char *const *args, const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) -{ - char *keyfile; - unsigned long long port; - struct GNUNET_CRYPTO_EccPublicKey pub; - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number (cfg, - "fcfsd", - "HTTPPORT", - &port)) - { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "fcfsd", "HTTPPORT"); - return; - } - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (cfg, - "fcfsd", - "ZONEKEY", - &keyfile)) - { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "fcfsd", "ZONEKEY"); - return; - } - fcfs_zone_pkey = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile); - GNUNET_free (keyfile); - if (NULL == fcfs_zone_pkey) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to read or create private zone key\n")); - return; - } - GNUNET_CRYPTO_ecc_key_get_public (fcfs_zone_pkey, - &pub); - GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &fcfsd_zone); - ns = GNUNET_NAMESTORE_connect (cfg); - if (NULL == ns) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to connect to namestore\n")); - return; - } - httpd = MHD_start_daemon (MHD_USE_DEBUG, - (uint16_t) port, - NULL, NULL, - &create_response, NULL, - MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128, - MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1, - MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16, - MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (4 * 1024), - MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL, - MHD_OPTION_END); - if (NULL == httpd) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Failed to start HTTP server\n")); - GNUNET_NAMESTORE_disconnect (ns); - ns = NULL; - return; - } - run_httpd (); - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, - &do_shutdown, NULL); -} - - -/** - * The main function for the fcfs daemon. - * - * @param argc number of arguments from the command line - * @param argv command line arguments - * @return 0 ok, 1 on error - */ -int -main (int argc, char *const *argv) -{ - static const struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END - }; - - int ret; - - if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) - return 2; - - GNUNET_log_setup ("fcfsd", "WARNING", NULL); - ret = - (GNUNET_OK == - GNUNET_PROGRAM_run (argc, argv, "fcfsd", - _("GNUnet GNS first come first serve registration service"), - options, - &run, NULL)) ? 0 : 1; - GNUNET_free ((void*) argv); - return ret; -} - -/* end of gnunet-gns-fcfsd.c */ diff --git a/src/gns/gnunet-service-gns_resolver.c b/src/gns/gnunet-service-gns_resolver.c index 8aa9e8188..de9e7e014 100644 --- a/src/gns/gnunet-service-gns_resolver.c +++ b/src/gns/gnunet-service-gns_resolver.c @@ -491,6 +491,7 @@ process_pseu_lookup_ns (void *cls, { struct GetPseuAuthorityHandle *gph = cls; struct GNUNET_NAMESTORE_RecordData new_pkey; + struct GNUNET_CRYPTO_EccPublicKey pub; gph->namestore_task = NULL; if (rd_count > 0) @@ -502,14 +503,19 @@ process_pseu_lookup_ns (void *cls, time, this time not using PSEU but the original label */ if (0 == strcmp (name, gph->label)) + { free_get_pseu_authority_handle (gph); + } else + { + GNUNET_CRYPTO_ecc_key_get_public (&gph->shorten_zone_key, + &pub); gph->namestore_task = GNUNET_NAMESTORE_lookup (namestore_handle, - &gph->shorten_zone_key, + &pub, gph->label, - GNUNET_NAMESTORE_TYPE_ANY, &process_pseu_lookup_ns, gph); + } return; } /* name is available */ @@ -543,6 +549,10 @@ static void process_pseu_result (struct GetPseuAuthorityHandle* gph, const char *pseu) { + struct GNUNET_CRYPTO_EccPublicKey pub; + + GNUNET_CRYPTO_ecc_key_get_public (&gph->shorten_zone_key, + &pub); if (NULL == pseu) { /* no PSEU found, try original label */ @@ -550,9 +560,8 @@ process_pseu_result (struct GetPseuAuthorityHandle* gph, "No PSEU found, trying original label `%s' instead.\n", gph->label); gph->namestore_task = GNUNET_NAMESTORE_lookup (namestore_handle, - &gph->shorten_zone_key, + &pub, gph->label, - GNUNET_NAMESTORE_TYPE_ANY, &process_pseu_lookup_ns, gph); return; @@ -560,9 +569,8 @@ process_pseu_result (struct GetPseuAuthorityHandle* gph, /* check if 'pseu' is taken */ gph->namestore_task = GNUNET_NAMESTORE_lookup (namestore_handle, - &gph->shorten_zone_key, + &pub, pseu, - GNUNET_NAMESTORE_TYPE_ANY, &process_pseu_lookup_ns, gph); } @@ -1927,12 +1935,12 @@ process_delegation_result_dht (void* cls, /* Check for key revocation and delegate */ - rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle, - &rh->authority, - GNUNET_GNS_MASTERZONE_STR, - GNUNET_NAMESTORE_TYPE_REV, - &process_pkey_revocation_result_ns, - rh); + rh->namestore_task = GNUNET_NAMESTORE_lookup (namestore_handle, + &rh->authority, + GNUNET_GNS_MASTERZONE_STR, + GNUNET_NAMESTORE_TYPE_REV, + &process_pkey_revocation_result_ns, + rh); return; } @@ -2801,12 +2809,12 @@ process_delegation_result_ns (void* cls, memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size); rh->rd_count = 1; /* Check for key revocation and delegate */ - rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle, - &rh->authority, - GNUNET_GNS_MASTERZONE_STR, - GNUNET_NAMESTORE_TYPE_REV, - &process_pkey_revocation_result_ns, - rh); + rh->namestore_task = GNUNET_NAMESTORE_lookup (namestore_handle, + &rh->authority, + GNUNET_GNS_MASTERZONE_STR, + GNUNET_NAMESTORE_TYPE_REV, + &process_pkey_revocation_result_ns, + rh); return; default: /* ignore, move to next result */ @@ -2867,12 +2875,11 @@ resolve_delegation_ns (struct ResolverHandle *rh) rh->name, rh->authority_name, GNUNET_short_h2s (&rh->authority)); - rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle, - &rh->authority, - rh->authority_name, - GNUNET_DNSPARSER_TYPE_ANY, - &process_delegation_result_ns, - rh); + rh->namestore_task = GNUNET_NAMESTORE_lookup (namestore_handle, + &rh->authority, + rh->authority_name, + &process_delegation_result_ns, + rh); } #endif diff --git a/src/include/gnunet_namestore_service.h b/src/include/gnunet_namestore_service.h index 986e957ec..0fbbb0084 100644 --- a/src/include/gnunet_namestore_service.h +++ b/src/include/gnunet_namestore_service.h @@ -366,6 +366,34 @@ GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls); +/** + * Process a records that were decrypted from a block. + * + * @param cls closure + * @param rd_count number of entries in @a rd array + * @param rd array of records with data to store + */ +typedef void (*GNUNET_NAMESTORE_RecordCallback) (void *cls, + unsigned int rd_count, + const struct GNUNET_NAMESTORE_RecordData *rd); + + +/** + * Perform a lookup and decrypt the resulting block. + * + * @param h namestore to perform lookup in + * @param value_zone zone to look up record in + * @param label label to look for + * @param proc function to call with the result + * @param proc_cls closure for @a proc + */ +struct GNUNET_NAMESTORE_QueueEntry * +GNUNET_NAMESTORE_lookup (struct GNUNET_NAMESTORE_Handle *h, + const struct GNUNET_CRYPTO_EccPublicKey *value_zone, + const char *label, + GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls); + + /** * Cancel a namestore operation. The final callback from the * operation must not have been done yet. Must be called on any @@ -655,17 +683,6 @@ int GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block); -/** - * Process a records that were decrypted from a block. - * - * @param cls closure - * @param rd_count number of entries in @a rd array - * @param rd array of records with data to store - */ -typedef void (*GNUNET_NAMESTORE_RecordCallback) (void *cls, - unsigned int rd_count, - const struct GNUNET_NAMESTORE_RecordData *rd); - /** * Decrypt block. * diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am index 7c602ac13..b7ff99998 100644 --- a/src/namestore/Makefile.am +++ b/src/namestore/Makefile.am @@ -92,7 +92,8 @@ libexec_PROGRAMS = \ gnunet-service-namestore bin_PROGRAMS = \ - gnunet-namestore + gnunet-namestore \ + gnunet-namestore-fcfsd gnunet_namestore_SOURCES = \ @@ -106,6 +107,17 @@ gnunet_namestore_DEPENDENCIES = \ libgnunetnamestore.la +gnunet_namestore_fcfsd_SOURCES = \ + gnunet-namestore-fcfsd.c +gnunet_namestore_fcfsd_LDADD = -lmicrohttpd \ + $(top_builddir)/src/util/libgnunetutil.la \ + $(top_builddir)/src/namestore/libgnunetnamestore.la \ + $(GN_LIBINTL) +gnunet_namestore_fcfsd_DEPENDENCIES = \ + $(top_builddir)/src/util/libgnunetutil.la \ + $(top_builddir)/src/namestore/libgnunetnamestore.la + + gnunet_service_namestore_SOURCES = \ gnunet-service-namestore.c diff --git a/src/namestore/gnunet-namestore-fcfsd.c b/src/namestore/gnunet-namestore-fcfsd.c new file mode 100644 index 000000000..d949806db --- /dev/null +++ b/src/namestore/gnunet-namestore-fcfsd.c @@ -0,0 +1,947 @@ +/* + This file is part of GNUnet. + (C) 2012-2013 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +/** + * @file gnunet-gns-fcfsd.c + * @brief HTTP daemon that offers first-come-first-serve GNS domain registration + * @author Christian Grothoff + * + * TODO: + * - the code currently contains a 'race' between checking that the + * domain name is available and allocating it to the new public key + * (should this race be solved by namestore or by fcfsd?) + * - nicer error reporting to browser + */ +#include "platform.h" +#include +#include "gnunet_util_lib.h" +#include "gnunet_namestore_service.h" + +/** + * Invalid method page. + */ +#define METHOD_ERROR "Illegal requestGo away." + +/** + * Front page. (/) + */ +#define MAIN_PAGE "GNUnet FCFS Authority Name Registration ServiceWhat is your desired domain name? (at most 63 lowercase characters, no dots allowed.)

What is your public key? (Copy from gnunet-setup.)
List of all registered names " + +/** + * Second page (/S) + */ +#define SUBMIT_PAGE "%s%s" + +/** + * Fcfs zoneinfo page (/Zoneinfo) + */ +#define ZONEINFO_PAGE "FCFS Zoneinfo

FCFS Zoneinfo

%s
namePKEY
" + +#define FCFS_ZONEINFO_URL "/Zoneinfo" + +/** + * Mime type for HTML pages. + */ +#define MIME_HTML "text/html" + +/** + * Name of our cookie. + */ +#define COOKIE_NAME "gns-fcfs" + +#define DEFAULT_ZONEINFO_BUFSIZE 2048 + +/** + * Phases a request goes through. + */ +enum Phase + { + /** + * Start phase (parsing POST, checking). + */ + RP_START = 0, + + /** + * Lookup to see if the domain name is taken. + */ + RP_LOOKUP, + + /** + * Storing of the record. + */ + RP_PUT, + + /** + * We're done with success. + */ + RP_SUCCESS, + + /** + * Send failure message. + */ + RP_FAIL + }; + + +/** + * Data kept per request. + */ +struct Request +{ + + /** + * Associated session. + */ + struct Session *session; + + /** + * Post processor handling form data (IF this is + * a POST request). + */ + struct MHD_PostProcessor *pp; + + /** + * URL to serve in response to this POST (if this request + * was a 'POST') + */ + const char *post_url; + + /** + * Active request with the namestore. + */ + struct GNUNET_NAMESTORE_QueueEntry *qe; + + /** + * Current processing phase. + */ + enum Phase phase; + + /** + * Domain name submitted via form. + */ + char domain_name[64]; + + /** + * Public key submitted via form. + */ + char public_key[128]; + +}; + +/** + * Zoneinfo request + */ +struct ZoneinfoRequest +{ + /** + * Connection + */ + struct MHD_Connection *connection; + + /** + * List iterator + */ + struct GNUNET_NAMESTORE_ZoneIterator *list_it; + + /** + * Buffer + */ + char* zoneinfo; + + /** + * Buffer length + */ + size_t buf_len; + + /** + * Buffer write offset + */ + size_t write_offset; +}; + +/** + * MHD deamon reference. + */ +static struct MHD_Daemon *httpd; + +/** + * Main HTTP task. + */ +static GNUNET_SCHEDULER_TaskIdentifier httpd_task; + +/** + * Handle to the namestore. + */ +static struct GNUNET_NAMESTORE_Handle *ns; + +/** + * Private key for the fcfsd zone. + */ +static struct GNUNET_CRYPTO_EccPrivateKey *fcfs_zone_pkey; + + +/** + * Task run whenever HTTP server operations are pending. + * + * @param cls unused + * @param tc scheduler context + */ +static void +do_httpd (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc); + + +/** + * Schedule task to run MHD server now. + */ +static void +run_httpd_now () +{ + if (GNUNET_SCHEDULER_NO_TASK != httpd_task) + { + GNUNET_SCHEDULER_cancel (httpd_task); + httpd_task = GNUNET_SCHEDULER_NO_TASK; + } + httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL); +} + + +static void +iterate_cb (void *cls, + const struct GNUNET_CRYPTO_EccPrivateKey *zone_key, + const char *name, + unsigned int rd_len, + const struct GNUNET_NAMESTORE_RecordData *rd) +{ + struct ZoneinfoRequest *zr = cls; + struct MHD_Response *response; + char* full_page; + size_t bytes_free; + char* pkey; + char* new_buf; + + + if (NULL == name) + { + zr->list_it = NULL; + + /* return static form */ + GNUNET_asprintf (&full_page, + ZONEINFO_PAGE, + zr->zoneinfo, + zr->zoneinfo); + response = MHD_create_response_from_buffer (strlen (full_page), + (void *) full_page, + MHD_RESPMEM_MUST_FREE); + MHD_add_response_header (response, + MHD_HTTP_HEADER_CONTENT_TYPE, + MIME_HTML); + MHD_queue_response (zr->connection, + MHD_HTTP_OK, + response); + MHD_destroy_response (response); + GNUNET_free (zr->zoneinfo); + GNUNET_free (zr); + run_httpd_now (); + return; + } + + if (1 != rd_len) + { + GNUNET_NAMESTORE_zone_iterator_next (zr->list_it); + return; + } + + if (GNUNET_NAMESTORE_TYPE_PKEY != rd->record_type) + { + GNUNET_NAMESTORE_zone_iterator_next (zr->list_it); + return; + } + + bytes_free = zr->buf_len - zr->write_offset; + pkey = GNUNET_NAMESTORE_value_to_string (rd->record_type, + rd->data, + rd->data_size); + + if (bytes_free < (strlen (name) + strlen (pkey) + 40)) + { + new_buf = GNUNET_malloc (zr->buf_len * 2); + memcpy (new_buf, zr->zoneinfo, zr->write_offset); + GNUNET_free (zr->zoneinfo); + zr->zoneinfo = new_buf; + zr->buf_len *= 2; + } + sprintf (zr->zoneinfo + zr->write_offset, + "%s%s", + name, + pkey); + zr->write_offset = strlen (zr->zoneinfo); + GNUNET_NAMESTORE_zone_iterator_next (zr->list_it); + GNUNET_free (pkey); +} + + + +/** + * Handler that returns FCFS zoneinfo page. + * + * @param connection connection to use + * @return MHD_YES on success + */ +static int +serve_zoneinfo_page (struct MHD_Connection *connection) +{ + struct ZoneinfoRequest *zr; + + zr = GNUNET_new (struct ZoneinfoRequest); + zr->zoneinfo = GNUNET_malloc (DEFAULT_ZONEINFO_BUFSIZE); + zr->buf_len = DEFAULT_ZONEINFO_BUFSIZE; + zr->connection = connection; + zr->write_offset = 0; + zr->list_it = GNUNET_NAMESTORE_zone_iteration_start (ns, + fcfs_zone_pkey, + &iterate_cb, + zr); + return MHD_YES; +} + + +/** + * Handler that returns a simple static HTTP page. + * + * @param connection connection to use + * @return MHD_YES on success + */ +static int +serve_main_page (struct MHD_Connection *connection) +{ + int ret; + struct MHD_Response *response; + + /* return static form */ + response = MHD_create_response_from_buffer (strlen (MAIN_PAGE), + (void *) MAIN_PAGE, + MHD_RESPMEM_PERSISTENT); + MHD_add_response_header (response, + MHD_HTTP_HEADER_CONTENT_TYPE, + MIME_HTML); + ret = MHD_queue_response (connection, + MHD_HTTP_OK, + response); + MHD_destroy_response (response); + return ret; +} + + +/** + * Send the 'SUBMIT_PAGE'. + * + * @param info information string to send to the user + * @param request request information + * @param connection connection to use + */ +static int +fill_s_reply (const char *info, + struct Request *request, + struct MHD_Connection *connection) +{ + int ret; + char *reply; + struct MHD_Response *response; + + GNUNET_asprintf (&reply, + SUBMIT_PAGE, + info, + info); + /* return static form */ + response = MHD_create_response_from_buffer (strlen (reply), + (void *) reply, + MHD_RESPMEM_MUST_FREE); + MHD_add_response_header (response, + MHD_HTTP_HEADER_CONTENT_TYPE, + MIME_HTML); + ret = MHD_queue_response (connection, + MHD_HTTP_OK, + response); + MHD_destroy_response (response); + return ret; +} + + +/** + * Iterator over key-value pairs where the value + * maybe made available in increments and/or may + * not be zero-terminated. Used for processing + * POST data. + * + * @param cls user-specified closure + * @param kind type of the value + * @param key 0-terminated key for the value + * @param filename name of the uploaded file, NULL if not known + * @param content_type mime-type of the data, NULL if not known + * @param transfer_encoding encoding of the data, NULL if not known + * @param data pointer to size bytes of data at the + * specified offset + * @param off offset of data in the overall value + * @param size number of bytes in data available + * @return MHD_YES to continue iterating, + * MHD_NO to abort the iteration + */ +static int +post_iterator (void *cls, + enum MHD_ValueKind kind, + const char *key, + const char *filename, + const char *content_type, + const char *transfer_encoding, + const char *data, uint64_t off, size_t size) +{ + struct Request *request = cls; + + if (0 == strcmp ("domain", key)) + { + if (size + off >= sizeof(request->domain_name)) + size = sizeof (request->domain_name) - off - 1; + memcpy (&request->domain_name[off], + data, + size); + request->domain_name[size+off] = '\0'; + return MHD_YES; + } + if (0 == strcmp ("pkey", key)) + { + if (size + off >= sizeof(request->public_key)) + size = sizeof (request->public_key) - off - 1; + memcpy (&request->public_key[off], + data, + size); + request->public_key[size+off] = '\0'; + return MHD_YES; + } + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Unsupported form value `%s'\n"), + key); + return MHD_YES; +} + + + + +/** + * Continuation called to notify client about result of the + * operation. + * + * @param cls closure + * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate) + * GNUNET_NO if content was already there + * GNUNET_YES (or other positive value) on success + * @param emsg NULL on success, otherwise an error message + */ +static void +put_continuation (void *cls, + int32_t success, + const char *emsg) +{ + struct Request *request = cls; + + request->qe = NULL; + if (0 >= success) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("Failed to create record for domain `%s': %s\n"), + request->domain_name, + emsg); + request->phase = RP_FAIL; + } + else + request->phase = RP_SUCCESS; + run_httpd_now (); +} + + +/** + * Test if a name mapping was found, if so, refuse. If not, initiate storing of the record. + * + * @param cls closure + * @param zone_key public key of the zone + * @param name name that is being mapped (at most 255 characters long) + * @param rd_count number of entries in 'rd' array + * @param rd array of records with data to store + */ +static void +zone_to_name_cb (void *cls, + const struct GNUNET_CRYPTO_EccPrivateKey *zone_key, + const char *name, + unsigned int rd_count, + const struct GNUNET_NAMESTORE_RecordData *rd) +{ + struct Request *request = cls; + struct GNUNET_NAMESTORE_RecordData r; + struct GNUNET_CRYPTO_ShortHashCode pub; + + request->qe = NULL; + if (NULL != name) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Found existing name `%s' for the given key\n"), + name); + request->phase = RP_FAIL; + run_httpd_now (); + return; + } + r.data = &pub; + r.data_size = sizeof (pub); + r.expiration_time = UINT64_MAX; + r.record_type = GNUNET_NAMESTORE_TYPE_PKEY; + r.flags = GNUNET_NAMESTORE_RF_AUTHORITY; + request->qe = GNUNET_NAMESTORE_records_store (ns, + fcfs_zone_pkey, + request->domain_name, + 1, &r, + &put_continuation, + request); +} + + +/** + * Process a record that was stored in the namestore. Used to check if + * the requested name already exists in the namestore. If not, + * proceed to check if the requested key already exists. + * + * @param cls closure + * @param zone_key private key of the zone + * @param name name that is being mapped (at most 255 characters long) + * @param rd_count number of entries in 'rd' array + * @param rd array of records with data to store + */ +static void +lookup_result_processor (void *cls, + const struct GNUNET_CRYPTO_EccPrivateKey *zone_key, + const char *name, + unsigned int rd_count, + const struct GNUNET_NAMESTORE_RecordData *rd) +{ + struct Request *request = cls; + struct GNUNET_CRYPTO_EccPublicKey pub; + + request->qe = NULL; + if (0 != rd_count) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Found %u existing records for domain `%s'\n"), + rd_count, + request->domain_name); + request->phase = RP_FAIL; + run_httpd_now (); + return; + } + if (GNUNET_OK != + GNUNET_CRYPTO_ecc_public_key_from_string (request->public_key, + strlen (request->public_key), + &pub)) + { + GNUNET_break (0); + request->phase = RP_FAIL; + run_httpd_now (); + return; + } + request->qe = GNUNET_NAMESTORE_zone_to_name (ns, + fcfs_zone_pkey, + &pub, + &zone_to_name_cb, + request); +} + + +/** + * Main MHD callback for handling requests. + * + * @param cls unused + * @param connection MHD connection handle + * @param url the requested url + * @param method the HTTP method used ("GET", "PUT", etc.) + * @param version the HTTP version string (i.e. "HTTP/1.1") + * @param upload_data the data being uploaded (excluding HEADERS, + * for a POST that fits into memory and that is encoded + * with a supported encoding, the POST data will NOT be + * given in upload_data and is instead available as + * part of MHD_get_connection_values; very large POST + * data *will* be made available incrementally in + * upload_data) + * @param upload_data_size set initially to the size of the + * upload_data provided; the method must update this + * value to the number of bytes NOT processed; + * @param ptr pointer to location where we store the 'struct Request' + * @return MHD_YES if the connection was handled successfully, + * MHD_NO if the socket must be closed due to a serious + * error while handling the request + */ +static int +create_response (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, + size_t *upload_data_size, + void **ptr) +{ + struct MHD_Response *response; + struct Request *request; + int ret; + struct GNUNET_CRYPTO_EccPublicKey pub; + + if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) || + (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) ) + { + if (0 == strcmp (url, FCFS_ZONEINFO_URL)) + ret = serve_zoneinfo_page (connection); + else + ret = serve_main_page (connection); + if (ret != MHD_YES) + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Failed to create page for `%s'\n"), + url); + return ret; + } + if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) + { + request = *ptr; + if (NULL == request) + { + request = GNUNET_malloc (sizeof (struct Request)); + *ptr = request; + request->pp = MHD_create_post_processor (connection, 1024, + &post_iterator, request); + if (NULL == request->pp) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Failed to setup post processor for `%s'\n"), + url); + return MHD_NO; /* internal error */ + } + return MHD_YES; + } + if (NULL != request->pp) + { + /* evaluate POST data */ + MHD_post_process (request->pp, + upload_data, + *upload_data_size); + if (0 != *upload_data_size) + { + *upload_data_size = 0; + return MHD_YES; + } + /* done with POST data, serve response */ + MHD_destroy_post_processor (request->pp); + request->pp = NULL; + } + if (GNUNET_OK != + GNUNET_CRYPTO_ecc_public_key_from_string (request->public_key, + strlen (request->public_key), + &pub)) + { + /* parse error */ + return fill_s_reply ("Failed to parse given public key", + request, connection); + } + switch (request->phase) + { + case RP_START: + if (NULL != strchr (request->domain_name, (int) '.')) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Domain name must not contain `.'\n")); + request->phase = RP_FAIL; + return fill_s_reply ("Domain name must not contain `.', sorry.", + request, connection); + } + if (NULL != strchr (request->domain_name, (int) '+')) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _("Domain name must not contain `+'\n")); + request->phase = RP_FAIL; + return fill_s_reply ("Domain name must not contain `+', sorry.", + request, connection); + } + request->phase = RP_LOOKUP; + GNUNET_CRYPTO_ecc_key_get_public (fcfs_zone_pkey, + &pub); + request->qe = GNUNET_NAMESTORE_lookup (ns, + &pub, + request->domain_name, + &lookup_result_processor, + request); + break; + case RP_LOOKUP: + break; + case RP_PUT: + break; + case RP_FAIL: + return fill_s_reply ("Request failed, sorry.", + request, connection); + case RP_SUCCESS: + return fill_s_reply ("Success.", + request, connection); + default: + GNUNET_break (0); + return MHD_NO; + } + return MHD_YES; /* will have a reply later... */ + } + /* unsupported HTTP method */ + response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), + (void *) METHOD_ERROR, + MHD_RESPMEM_PERSISTENT); + ret = MHD_queue_response (connection, + MHD_HTTP_METHOD_NOT_ACCEPTABLE, + response); + MHD_destroy_response (response); + return ret; +} + + +/** + * Callback called upon completion of a request. + * Decrements session reference counter. + * + * @param cls not used + * @param connection connection that completed + * @param con_cls session handle + * @param toe status code + */ +static void +request_completed_callback (void *cls, + struct MHD_Connection *connection, + void **con_cls, + enum MHD_RequestTerminationCode toe) +{ + struct Request *request = *con_cls; + + if (NULL == request) + return; + if (NULL != request->pp) + MHD_destroy_post_processor (request->pp); + if (NULL != request->qe) + GNUNET_NAMESTORE_cancel (request->qe); + GNUNET_free (request); +} + + +#define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG + + +/** + * Schedule tasks to run MHD server. + */ +static void +run_httpd () +{ + fd_set rs; + fd_set ws; + fd_set es; + struct GNUNET_NETWORK_FDSet *wrs; + struct GNUNET_NETWORK_FDSet *wws; + struct GNUNET_NETWORK_FDSet *wes; + int max; + int haveto; + UNSIGNED_MHD_LONG_LONG timeout; + struct GNUNET_TIME_Relative tv; + + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + wrs = GNUNET_NETWORK_fdset_create (); + wes = GNUNET_NETWORK_fdset_create (); + wws = GNUNET_NETWORK_fdset_create (); + max = -1; + GNUNET_assert (MHD_YES == MHD_get_fdset (httpd, &rs, &ws, &es, &max)); + haveto = MHD_get_timeout (httpd, &timeout); + if (haveto == MHD_YES) + tv.rel_value_us = (uint64_t) timeout * 1000LL; + else + tv = GNUNET_TIME_UNIT_FOREVER_REL; + GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1); + GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1); + GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1); + httpd_task = + GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, + tv, wrs, wws, + &do_httpd, NULL); + GNUNET_NETWORK_fdset_destroy (wrs); + GNUNET_NETWORK_fdset_destroy (wws); + GNUNET_NETWORK_fdset_destroy (wes); +} + + +/** + * Task run whenever HTTP server operations are pending. + * + * @param cls unused + * @param tc scheduler context + */ +static void +do_httpd (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + httpd_task = GNUNET_SCHEDULER_NO_TASK; + MHD_run (httpd); + run_httpd (); +} + + +/** + * Task run on shutdown. Cleans up everything. + * + * @param cls unused + * @param tc scheduler context + */ +static void +do_shutdown (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + if (GNUNET_SCHEDULER_NO_TASK != httpd_task) + { + GNUNET_SCHEDULER_cancel (httpd_task); + httpd_task = GNUNET_SCHEDULER_NO_TASK; + } + if (NULL != ns) + { + GNUNET_NAMESTORE_disconnect (ns); + ns = NULL; + } + if (NULL != httpd) + { + MHD_stop_daemon (httpd); + httpd = NULL; + } + if (NULL != fcfs_zone_pkey) + { + GNUNET_CRYPTO_ecc_key_free (fcfs_zone_pkey); + fcfs_zone_pkey = NULL; + } +} + + +/** + * Main function that will be run. + * + * @param cls closure + * @param args remaining command-line arguments + * @param cfgfile name of the configuration file used (for saving, can be NULL!) + * @param cfg configuration + */ +static void +run (void *cls, char *const *args, const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + char *keyfile; + unsigned long long port; + + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_number (cfg, + "fcfsd", + "HTTPPORT", + &port)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, + "fcfsd", "HTTPPORT"); + return; + } + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_filename (cfg, + "fcfsd", + "ZONEKEY", + &keyfile)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, + "fcfsd", "ZONEKEY"); + return; + } + fcfs_zone_pkey = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile); + GNUNET_free (keyfile); + if (NULL == fcfs_zone_pkey) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Failed to read or create private zone key\n")); + return; + } + ns = GNUNET_NAMESTORE_connect (cfg); + if (NULL == ns) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Failed to connect to namestore\n")); + return; + } + httpd = MHD_start_daemon (MHD_USE_DEBUG, + (uint16_t) port, + NULL, NULL, + &create_response, NULL, + MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128, + MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1, + MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16, + MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (4 * 1024), + MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL, + MHD_OPTION_END); + if (NULL == httpd) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Failed to start HTTP server\n")); + GNUNET_NAMESTORE_disconnect (ns); + ns = NULL; + return; + } + run_httpd (); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, + &do_shutdown, NULL); +} + + +/** + * The main function for the fcfs daemon. + * + * @param argc number of arguments from the command line + * @param argv command line arguments + * @return 0 ok, 1 on error + */ +int +main (int argc, char *const *argv) +{ + static const struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + + int ret; + + if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) + return 2; + + GNUNET_log_setup ("fcfsd", "WARNING", NULL); + ret = + (GNUNET_OK == + GNUNET_PROGRAM_run (argc, argv, "fcfsd", + _("GNUnet GNS first come first serve registration service"), + options, + &run, NULL)) ? 0 : 1; + GNUNET_free ((void*) argv); + return ret; +} + +/* end of gnunet-gns-fcfsd.c */ diff --git a/src/namestore/namestore_api.c b/src/namestore/namestore_api.c index 98148ba1e..13cc32035 100644 --- a/src/namestore/namestore_api.c +++ b/src/namestore/namestore_api.c @@ -1131,6 +1131,26 @@ GNUNET_NAMESTORE_lookup_block (struct GNUNET_NAMESTORE_Handle *h, } +/** + * Perform a lookup and decrypt the resulting block. + * + * @param h namestore to perform lookup in + * @param value_zone zone to look up record in + * @param label label to look for + * @param proc function to call with the result + * @param proc_cls closure for @a proc + */ +struct GNUNET_NAMESTORE_QueueEntry * +GNUNET_NAMESTORE_lookup (struct GNUNET_NAMESTORE_Handle *h, + const struct GNUNET_CRYPTO_EccPublicKey *value_zone, + const char *label, + GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls) +{ + GNUNET_break (0); // FIXME: not implemented + return NULL; +} + + /** * Look for an existing PKEY delegation record for a given public key. * Returns at most one result to the processor. diff --git a/src/namestore/namestore_api_common.c b/src/namestore/namestore_api_common.c index 5a27f14a1..bfab39384 100644 --- a/src/namestore/namestore_api_common.c +++ b/src/namestore/namestore_api_common.c @@ -364,7 +364,7 @@ int GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block, const struct GNUNET_CRYPTO_EccPublicKey *zone_key, const char *label, - GNUNET_NAMESTORE_RecordMonitor proc, + GNUNET_NAMESTORE_RecordCallback proc, void *proc_cls) { GNUNET_break (0); -- cgit v1.2.3