2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @author Martin Schanzenbach
22 * @file namestore/plugin_rest_namestore.c
23 * @brief GNUnet Namestore REST plugin
28 #include "gnunet_rest_plugin.h"
29 #include "gnunet_gns_service.h"
30 #include "gnunet_namestore_service.h"
31 #include "gnunet_identity_service.h"
32 #include "gnunet_rest_lib.h"
33 #include "gnunet_jsonapi_lib.h"
34 #include "gnunet_jsonapi_util.h"
35 #include "microhttpd.h"
38 #define GNUNET_REST_API_NS_NAMESTORE "/names"
40 #define GNUNET_REST_API_NS_NAMESTORE_ZKEY "/names/zkey"
42 #define GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO "record"
44 #define GNUNET_REST_JSONAPI_NAMESTORE_NAME "name"
46 #define GNUNET_REST_JSONAPI_NAMESTORE_REVINFO "revinfo"
48 #define GNUNET_REST_JSONAPI_NAMESTORE_RECORD GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO
50 #define GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE "record_type"
52 #define GNUNET_REST_JSONAPI_NAMESTORE_VALUE "value"
54 #define GNUNET_REST_JSONAPI_NAMESTORE_PUBLIC "public"
56 #define GNUNET_REST_JSONAPI_NAMESTORE_SHADOW "shadow"
58 #define GNUNET_REST_JSONAPI_NAMESTORE_PKEY "pkey"
60 #define GNUNET_REST_JSONAPI_NAMESTORE_ZKEY "zkey"
62 #define GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION "expiration"
64 #define GNUNET_REST_JSONAPI_NAMESTORE_EGO "ego"
67 * @brief struct returned by the initialization function of the plugin
71 const struct GNUNET_CONFIGURATION_Handle *cfg;
76 * HTTP methods allows for this plugin
78 static char* allow_methods;
80 const struct GNUNET_CONFIGURATION_Handle *cfg;
87 struct RecordEntry *next;
92 struct RecordEntry *prev;
101 struct RecordEntry *record_head;
106 struct record_entry *record_tail;
109 * JSON response object
111 struct GNUNET_JSONAPI_Document *resp_object;
116 struct GNUNET_REST_RequestHandle *rest_handle;
119 * Handle to GNS service.
121 struct GNUNET_IDENTITY_Handle *identity_handle;
124 * Handle to NAMESTORE
126 struct GNUNET_NAMESTORE_Handle *ns_handle;
129 * Handle to NAMESTORE it
131 struct GNUNET_NAMESTORE_ZoneIterator *list_it;
134 * Private key for the zone
136 struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
139 * Handle to identity lookup
141 struct GNUNET_IDENTITY_EgoLookup *ego_lookup;
144 * Default Ego operation
146 struct GNUNET_IDENTITY_Operation *get_default;
164 * Name of the record to modify
169 * Value of the record
176 const char* zkey_str;
186 struct GNUNET_GNSRECORD_Data *rd;
191 unsigned int rd_count;
194 * NAMESTORE Operation
196 struct GNUNET_NAMESTORE_QueueEntry *add_qe;
199 * NAMESTORE Operation
201 struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
204 * Desired timeout for the lookup (default is no timeout).
206 struct GNUNET_TIME_Relative timeout;
209 * ID of a task associated with the resolution process.
211 struct GNUNET_SCHEDULER_Task * timeout_task;
214 * The plugin result processor
216 GNUNET_REST_ResultProcessor proc;
219 * The closure of the result processor
231 const struct GNUNET_CONFIGURATION_Handle *cfg;
242 * Cleanup lookup handle
244 * @param handle Handle to clean up
247 cleanup_handle (struct RequestHandle *handle)
249 struct RecordEntry *record_entry;
250 struct RecordEntry *record_tmp;
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 if (NULL != handle->resp_object)
255 GNUNET_JSONAPI_document_delete (handle->resp_object);
256 if (NULL != handle->name)
257 GNUNET_free (handle->name);
258 if (NULL != handle->timeout_task)
259 GNUNET_SCHEDULER_cancel (handle->timeout_task);
260 if (NULL != handle->ego_lookup)
261 GNUNET_IDENTITY_ego_lookup_cancel (handle->ego_lookup);
262 if (NULL != handle->get_default)
263 GNUNET_IDENTITY_cancel (handle->get_default);
264 if (NULL != handle->list_it)
265 GNUNET_NAMESTORE_zone_iteration_stop (handle->list_it);
266 if (NULL != handle->add_qe)
267 GNUNET_NAMESTORE_cancel (handle->add_qe);
268 if (NULL != handle->identity_handle)
269 GNUNET_IDENTITY_disconnect (handle->identity_handle);
270 if (NULL != handle->ns_handle)
271 GNUNET_NAMESTORE_disconnect (handle->ns_handle);
272 if (NULL != handle->url)
273 GNUNET_free (handle->url);
274 if (NULL != handle->value)
275 GNUNET_free (handle->value);
276 if (NULL != handle->rd)
278 for (unsigned int i = 0; i < handle->rd_count; i++)
280 if (NULL != handle->rd[i].data)
281 GNUNET_free ((void*)handle->rd[i].data);
283 GNUNET_free (handle->rd);
285 if (NULL != handle->ego_name)
286 GNUNET_free (handle->ego_name);
287 for (record_entry = handle->record_head;
288 NULL != record_entry;)
290 record_tmp = record_entry;
291 record_entry = record_entry->next;
292 GNUNET_free (record_tmp);
294 GNUNET_free (handle);
299 * Create json representation of a GNSRECORD
301 * @param rd the GNSRECORD_Data
304 gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd)
306 const char *typename;
311 typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type);
312 string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type,
316 if (NULL == string_val)
318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
319 "Record of type %d malformed, skipping\n",
320 (int) rd->record_type);
323 record_obj = json_object();
324 json_object_set_new (record_obj,
325 GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE,
326 json_string (typename));
327 json_object_set_new (record_obj,
328 GNUNET_REST_JSONAPI_NAMESTORE_VALUE,
329 json_string (string_val));
330 GNUNET_free (string_val);
332 if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags)
334 struct GNUNET_TIME_Relative time_rel;
335 time_rel.rel_value_us = rd->expiration_time;
336 exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
340 struct GNUNET_TIME_Absolute time_abs;
341 time_abs.abs_value_us = rd->expiration_time;
342 exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
344 json_object_set_new (record_obj, GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION, json_string (exp_str));
346 json_object_set_new (record_obj, "expired",
347 json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd)));
353 * Task run on error. Generates error response and cleans up.
355 * @param cls the request to generate an error response for
360 struct RequestHandle *handle = cls;
361 struct MHD_Response *resp = GNUNET_REST_create_response (NULL);
363 handle->proc (handle->proc_cls, resp, handle->response_code);
364 cleanup_handle (handle);
369 * Task run on timeout.
371 * @param cls the request to time out
374 do_timeout (void *cls)
376 struct RequestHandle *handle = cls;
378 handle->timeout_task = NULL;
384 cleanup_handle_delayed (void *cls)
386 cleanup_handle (cls);
391 * Iteration over all results finished, build final
394 * @param cls the `struct RequestHandle`
397 namestore_list_finished (void *cls)
399 struct RequestHandle *handle = cls;
401 struct MHD_Response *resp;
403 handle->list_it = NULL;
404 if (NULL == handle->resp_object)
405 handle->resp_object = GNUNET_JSONAPI_document_new ();
408 GNUNET_JSONAPI_document_serialize (handle->resp_object,
411 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
412 GNUNET_SCHEDULER_add_now (&do_error,
416 resp = GNUNET_REST_create_response (result);
417 handle->proc (handle->proc_cls,
420 GNUNET_free_non_null (result);
421 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed,
428 * Create a response with requested records
430 * @param handle the RequestHandle
433 namestore_list_response (void *cls,
434 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
437 const struct GNUNET_GNSRECORD_Data *rd)
439 struct RequestHandle *handle = cls;
440 struct GNUNET_JSONAPI_Resource *json_resource;
441 json_t *result_array;
444 if (NULL == handle->resp_object)
445 handle->resp_object = GNUNET_JSONAPI_document_new ();
447 if ( (NULL != handle->name) &&
448 (0 != strcmp (handle->name,
451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
452 "%s does not match %s\n",
455 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it,
460 result_array = json_array ();
461 for (unsigned int i=0; i<rd_len; i++)
463 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
464 (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)) )
467 if ( (rd[i].record_type != handle->type) &&
468 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
470 record_obj = gnsrecord_to_json (&rd[i]);
471 json_array_append (result_array,
473 json_decref (record_obj);
476 if (0 < json_array_size(result_array))
478 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO,
480 GNUNET_JSONAPI_resource_add_attr (json_resource,
481 GNUNET_REST_JSONAPI_NAMESTORE_RECORD,
483 GNUNET_JSONAPI_document_resource_add (handle->resp_object, json_resource);
486 json_decref (result_array);
487 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it,
493 create_finished (void *cls, int32_t success, const char *emsg)
495 struct RequestHandle *handle = cls;
496 struct MHD_Response *resp;
498 handle->add_qe = NULL;
499 if (GNUNET_YES != success)
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 "Error storing records%s%s\n",
503 (NULL == emsg) ? "" : ": ",
504 (NULL == emsg) ? "" : emsg);
505 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
508 resp = GNUNET_REST_create_response (NULL);
509 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
510 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
515 * We're storing a new record; this requires
516 * that no record already exists
518 * @param cls closure, unused
519 * @param zone_key private key of the zone
520 * @param rec_name name that is being mapped (at most 255 characters long)
521 * @param rd_count number of entries in @a rd array
522 * @param rd array of records with data to store
525 create_new_record_cont (void *cls,
526 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
527 const char *rec_name,
528 unsigned int rd_count,
529 const struct GNUNET_GNSRECORD_Data *rd)
531 struct RequestHandle *handle = cls;
533 handle->add_qe = NULL;
534 if (0 != strcmp (rec_name, handle->name))
543 handle->proc (handle->proc_cls,
544 GNUNET_REST_create_response (NULL),
546 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
550 GNUNET_assert (NULL != handle->name);
551 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
562 del_finished (void *cls,
566 struct RequestHandle *handle = cls;
568 handle->add_qe = NULL;
569 if (GNUNET_NO == success)
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
572 _("Deleting record failed, record does not exist%s%s\n"),
573 (NULL != emsg) ? ": " : "",
574 (NULL != emsg) ? emsg : "");
575 GNUNET_SCHEDULER_add_now (&do_error, handle); //do_not_found TODO
578 if (GNUNET_SYSERR == success)
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
581 _("Deleting record failed%s%s\n"),
582 (NULL != emsg) ? ": " : "",
583 (NULL != emsg) ? emsg : "");
584 GNUNET_SCHEDULER_add_now (&do_error, handle);
587 handle->proc (handle->proc_cls,
588 GNUNET_REST_create_response (NULL),
589 MHD_HTTP_NO_CONTENT);
590 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
596 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
598 unsigned int rd_count,
599 const struct GNUNET_GNSRECORD_Data *rd)
601 struct RequestHandle *handle = cls;
603 handle->add_qe = NULL;
606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
607 _("There are no records under label `%s' that could be deleted.\n"),
613 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
623 namestore_delete_cont (struct GNUNET_REST_RequestHandle *con,
627 struct RequestHandle *handle = cls;
629 if (NULL == handle->name)
631 GNUNET_SCHEDULER_add_now (&do_error, handle);
635 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
646 json_to_gnsrecord (const json_t *records_json,
647 struct GNUNET_GNSRECORD_Data **rd,
648 unsigned int *rd_count)
650 struct GNUNET_TIME_Relative etime_rel;
651 struct GNUNET_TIME_Absolute etime_abs;
655 const char *typestring;
656 const char *expirationstring;
662 *rd_count = json_array_size (records_json);
663 *rd = GNUNET_new_array (*rd_count,
664 struct GNUNET_GNSRECORD_Data);
665 for (unsigned int i = 0; i < *rd_count; i++)
669 sizeof (struct GNUNET_GNSRECORD_Data));
670 record_json = json_array_get (records_json,
672 type_json = json_object_get (record_json,
673 GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE);
674 if (! json_is_string (type_json))
676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
677 "Type property is no string\n");
678 return GNUNET_SYSERR;
680 typestring = json_string_value (type_json);
681 (*rd)[i].record_type = GNUNET_GNSRECORD_typename_to_number (typestring);
682 if (UINT32_MAX == (*rd)[i].record_type)
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported type `%s'\n"),
685 json_string_value (type_json));
686 return GNUNET_SYSERR;
688 value_json = json_object_get (record_json,
689 GNUNET_REST_JSONAPI_NAMESTORE_VALUE);
690 if (! json_is_string (value_json))
692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
693 "Value property is no string\n");
694 return GNUNET_SYSERR;
696 value = GNUNET_strdup (json_string_value (value_json));
698 GNUNET_GNSRECORD_string_to_value ((*rd)[i].record_type,
703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
704 _("Value `%s' invalid for record type `%s'\n"),
707 return GNUNET_SYSERR;
709 (*rd)[i].data = rdata;
710 (*rd)[i].data_size = rdata_size;
712 * if (1 == handle->is_shadow)
713 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
714 if (1 != handle->is_public)
715 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
717 exp_json = json_object_get (record_json,
718 GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION);
719 if (! json_is_string (exp_json))
721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
722 "Expiration property is no string\n");
723 return GNUNET_SYSERR;
725 expirationstring = json_string_value (exp_json);
726 if (0 == strcmp (expirationstring, "never"))
728 (*rd)[i].expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
730 else if (GNUNET_OK ==
731 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
734 (*rd)[i].expiration_time = etime_rel.rel_value_us;
735 (*rd)[i].flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
737 else if (GNUNET_OK ==
738 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
741 (*rd)[i].expiration_time = etime_abs.abs_value_us;
745 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
746 _("Value `%s' invalid for record type `%s'\n"),
749 return GNUNET_SYSERR;
757 namestore_create_cont (struct GNUNET_REST_RequestHandle *con,
761 struct RequestHandle *handle = cls;
762 struct MHD_Response *resp;
763 struct GNUNET_JSONAPI_Document *json_obj;
764 struct GNUNET_JSONAPI_Resource *json_res;
765 json_t *records_json;
768 char term_data[handle->rest_handle->data_size+1];
769 struct GNUNET_JSON_Specification docspec[] = {
770 GNUNET_JSON_spec_jsonapi_document (&json_obj),
771 GNUNET_JSON_spec_end()
774 if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url))
776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
777 "Cannot create under %s\n", handle->url);
778 GNUNET_SCHEDULER_add_now (&do_error, handle);
781 if (0 >= handle->rest_handle->data_size)
783 GNUNET_SCHEDULER_add_now (&do_error, handle);
786 term_data[handle->rest_handle->data_size] = '\0';
787 GNUNET_memcpy (term_data,
788 handle->rest_handle->data,
789 handle->rest_handle->data_size);
790 data_js = json_loads (term_data,
793 GNUNET_assert (GNUNET_OK ==
794 GNUNET_JSON_parse (data_js, docspec,
796 json_decref (data_js);
797 if (NULL == json_obj)
799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
800 "Unable to parse JSONAPI Object from %s\n",
802 GNUNET_SCHEDULER_add_now (&do_error, handle);
805 if (1 != GNUNET_JSONAPI_document_resource_count (json_obj))
807 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
808 "Cannot create more than 1 resource! (Got %d)\n",
809 GNUNET_JSONAPI_document_resource_count (json_obj));
810 GNUNET_JSONAPI_document_delete (json_obj);
811 GNUNET_SCHEDULER_add_now (&do_error, handle);
814 json_res = GNUNET_JSONAPI_document_get_resource (json_obj, 0);
815 if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res,
816 GNUNET_REST_JSONAPI_NAMESTORE_RECORD))
818 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
819 "Unsupported JSON data type\n");
820 GNUNET_JSONAPI_document_delete (json_obj);
821 resp = GNUNET_REST_create_response (NULL);
822 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
823 cleanup_handle (handle);
826 handle->name = GNUNET_strdup (GNUNET_JSONAPI_resource_get_id (json_res));
827 records_json = GNUNET_JSONAPI_resource_read_attr (json_res,
828 GNUNET_REST_JSONAPI_NAMESTORE_RECORD);
829 if (NULL == records_json)
831 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
832 "No records given\n");
833 GNUNET_JSONAPI_document_delete (json_obj);
834 GNUNET_SCHEDULER_add_now (&do_error, handle);
837 if (GNUNET_SYSERR == json_to_gnsrecord (records_json, &handle->rd, &handle->rd_count))
839 GNUNET_JSONAPI_document_delete (json_obj);
840 GNUNET_SCHEDULER_add_now (&do_error, handle);
843 GNUNET_JSONAPI_document_delete (json_obj);
845 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
850 &create_new_record_cont,
856 namestore_zkey_response (void *cls,
857 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
859 unsigned int rd_count,
860 const struct GNUNET_GNSRECORD_Data *rd)
862 struct RequestHandle *handle = cls;
863 struct MHD_Response *resp;
864 struct GNUNET_JSONAPI_Document *json_obj;
865 struct GNUNET_JSONAPI_Resource *json_res;
869 handle->reverse_qe = NULL;
870 json_obj = GNUNET_JSONAPI_document_new ();
873 name_json = json_string (label);
874 json_res = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_REVINFO,
876 GNUNET_JSONAPI_resource_add_attr (json_res,
877 GNUNET_REST_JSONAPI_NAMESTORE_NAME,
879 GNUNET_JSONAPI_document_resource_add (json_obj, json_res);
880 json_decref (name_json);
883 if (GNUNET_SYSERR == GNUNET_JSONAPI_document_serialize (json_obj, &result))
885 GNUNET_JSONAPI_document_delete (json_obj);
886 GNUNET_SCHEDULER_add_now (&do_error, handle);
889 resp = GNUNET_REST_create_response (result);
890 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
891 GNUNET_JSONAPI_document_delete (json_obj);
892 GNUNET_free (result);
893 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
898 namestore_zkey_cont (struct GNUNET_REST_RequestHandle *con,
902 struct RequestHandle *handle = cls;
903 struct GNUNET_HashCode key;
904 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
906 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_NAMESTORE_ZKEY,
907 strlen (GNUNET_REST_JSONAPI_NAMESTORE_ZKEY),
910 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
914 "No zkey given %s\n", handle->url);
915 GNUNET_SCHEDULER_add_now (&do_error, handle);
918 handle->zkey_str = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
920 if ((NULL == handle->zkey_str) ||
922 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->zkey_str,
923 strlen (handle->zkey_str),
926 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
927 "Zkey invalid %s\n", handle->zkey_str);
928 GNUNET_SCHEDULER_add_now (&do_error, handle);
931 handle->reverse_qe = GNUNET_NAMESTORE_zone_to_name (handle->ns_handle,
936 &namestore_zkey_response,
942 namestore_info_cont (struct GNUNET_REST_RequestHandle *con,
946 struct RequestHandle *handle = cls;
948 handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
952 &namestore_list_response,
954 &namestore_list_finished,
960 get_name_from_url (const char* url)
962 if (strlen (url) <= strlen (GNUNET_REST_API_NS_NAMESTORE))
964 return (char*)url + strlen (GNUNET_REST_API_NS_NAMESTORE) + 1;
968 * Respond to OPTIONS request
970 * @param con_handle the connection handle
972 * @param cls the RequestHandle
975 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
979 struct MHD_Response *resp;
980 struct RequestHandle *handle = cls;
982 //For now, independent of path return all options
983 resp = GNUNET_REST_create_response (NULL);
984 MHD_add_response_header (resp,
985 "Access-Control-Allow-Methods",
987 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
988 cleanup_handle (handle);
993 * Callback invoked from identity service with ego information.
994 * An @a ego of NULL means the ego was not found.
996 * @param cls closure with the configuration
997 * @param ego an ego known to identity service, or NULL
1000 identity_cb (void *cls,
1001 const struct GNUNET_IDENTITY_Ego *ego)
1003 struct RequestHandle *handle = cls;
1004 struct MHD_Response *resp;
1005 struct GNUNET_REST_RequestHandlerError err;
1006 static const struct GNUNET_REST_RequestHandler handlers[] = {
1007 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE_ZKEY, &namestore_zkey_cont}, //reverse
1008 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_info_cont}, //list
1009 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_create_cont}, //create
1010 // {MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_NAMESTORE, &namestore_edit_cont}, //update. TODO this shoul be PATCH
1011 {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, &namestore_delete_cont}, //delete
1012 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_NAMESTORE, &options_cont},
1013 GNUNET_REST_HANDLER_END
1016 handle->ego_lookup = NULL;
1019 if (NULL != handle->ego_name)
1021 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1022 _("Ego `%s' not known to identity service\n"),
1025 resp = GNUNET_REST_create_response (NULL);
1026 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
1027 cleanup_handle (handle);
1030 handle->zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
1031 handle->ns_handle = GNUNET_NAMESTORE_connect (cfg);
1032 if (NULL == handle->ns_handle)
1034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1035 _("Failed to connect to namestore\n"));
1036 GNUNET_SCHEDULER_add_now (&do_error, handle);
1041 GNUNET_JSONAPI_handle_request (handle->rest_handle,
1046 handle->response_code = err.error_code;
1047 GNUNET_SCHEDULER_add_now (&do_error,
1054 default_ego_cb (void *cls,
1055 struct GNUNET_IDENTITY_Ego *ego,
1059 struct RequestHandle *handle = cls;
1060 struct MHD_Response *resp;
1061 handle->get_default = NULL;
1064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1065 _("No default ego configured in identity service\n"));
1066 resp = GNUNET_REST_create_response (NULL);
1067 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
1068 cleanup_handle (handle);
1073 identity_cb (cls, ego);
1078 id_connect_cb (void *cls,
1079 struct GNUNET_IDENTITY_Ego *ego,
1083 struct RequestHandle *handle = cls;
1086 handle->get_default = GNUNET_IDENTITY_get (handle->identity_handle,
1088 &default_ego_cb, handle);
1094 * Function processing the REST call
1096 * @param method HTTP method
1097 * @param url URL of the HTTP request
1098 * @param data body of the HTTP request (optional)
1099 * @param data_size length of the body
1100 * @param proc callback function for the result
1101 * @param proc_cls closure for callback function
1102 * @return #GNUNET_OK if request accepted
1105 rest_identity_process_request(struct GNUNET_REST_RequestHandle *rest_handle,
1106 GNUNET_REST_ResultProcessor proc,
1109 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
1110 struct MHD_Response *resp;
1111 struct GNUNET_HashCode key;
1116 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1117 handle->proc_cls = proc_cls;
1118 handle->proc = proc;
1119 handle->rest_handle = rest_handle;
1120 handle->url = GNUNET_strdup (rest_handle->url);
1121 if (handle->url[strlen (handle->url)-1] == '/')
1122 handle->url[strlen (handle->url)-1] = '\0';
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_NAMESTORE_EGO,
1128 strlen (GNUNET_REST_JSONAPI_NAMESTORE_EGO),
1131 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1134 ego = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
1138 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
1139 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE,
1140 strlen (GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE),
1143 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1146 type = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
1149 handle->type = GNUNET_GNSRECORD_typename_to_number (type);
1151 name = get_name_from_url (handle->url);
1153 handle->ego_name = GNUNET_strdup (ego);
1155 handle->name = GNUNET_strdup (name);
1156 if (NULL == handle->ego_name)
1158 handle->identity_handle = GNUNET_IDENTITY_connect (handle->cfg, &id_connect_cb, handle);
1159 if (NULL == handle->identity_handle)
1161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot connect to identity service\n"));
1162 resp = GNUNET_REST_create_response (NULL);
1163 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
1164 cleanup_handle (handle);
1168 handle->ego_lookup = GNUNET_IDENTITY_ego_lookup (cfg,
1172 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
1178 * Entry point for the plugin.
1180 * @param cls Config info
1181 * @return NULL on error, otherwise the plugin context
1184 libgnunet_plugin_rest_namestore_init (void *cls)
1186 static struct Plugin plugin;
1188 struct GNUNET_REST_Plugin *api;
1190 if (NULL != plugin.cfg)
1191 return NULL; /* can only initialize once! */
1192 memset (&plugin, 0, sizeof (struct Plugin));
1194 api = GNUNET_new (struct GNUNET_REST_Plugin);
1196 api->name = GNUNET_REST_API_NS_NAMESTORE;
1197 api->process_request = &rest_identity_process_request;
1198 GNUNET_asprintf (&allow_methods,
1199 "%s, %s, %s, %s, %s",
1200 MHD_HTTP_METHOD_GET,
1201 MHD_HTTP_METHOD_POST,
1202 MHD_HTTP_METHOD_PUT,
1203 MHD_HTTP_METHOD_DELETE,
1204 MHD_HTTP_METHOD_OPTIONS);
1205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1206 _("Namestore REST API initialized\n"));
1212 * Exit point from the plugin.
1214 * @param cls the plugin context (as returned by "init")
1215 * @return always NULL
1218 libgnunet_plugin_rest_namestore_done (void *cls)
1220 struct GNUNET_REST_Plugin *api = cls;
1221 struct Plugin *plugin = api->cls;
1225 GNUNET_free_non_null (allow_methods);
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Namestore REST plugin is finished\n");
1231 /* end of plugin_rest_namestore.c */