2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 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 * @author Martin Schanzenbach
22 * @file identity/plugin_rest_identity.c
23 * @brief GNUnet Namestore REST plugin
28 #include "gnunet_rest_plugin.h"
29 #include "gnunet_identity_service.h"
30 #include "gnunet_rest_lib.h"
31 #include "microhttpd.h"
34 #define API_NAMESPACE "/identity"
36 #define EGO_NAMESPACE "/identity/egos"
38 #define ID_REST_STATE_INIT 0
40 #define ID_REST_STATE_POST_INIT 1
42 #define URL_PARAM_SUBSYS "service"
44 #define GNUNET_REST_JSONAPI_IDENTITY_EGO "ego"
46 #define GNUNET_REST_JSONAPI_IDENTITY_KEY "key"
48 #define GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM "subsystem"
51 * @brief struct returned by the initialization function of the plugin
55 const struct GNUNET_CONFIGURATION_Handle *cfg;
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
65 struct EgoEntry *next;
70 struct EgoEntry *prev;
80 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
85 struct GNUNET_IDENTITY_Ego *ego;
93 struct EgoEntry *ego_head;
98 struct EgoEntry *ego_tail;
100 struct RestConnectionDataHandle *conndata_handle;
103 * The processing state
108 * Handle to GNS service.
110 struct GNUNET_IDENTITY_Handle *identity_handle;
115 struct GNUNET_IDENTITY_Operation *op;
118 * Desired timeout for the lookup (default is no timeout).
120 struct GNUNET_TIME_Relative timeout;
123 * ID of a task associated with the resolution process.
125 struct GNUNET_SCHEDULER_Task * timeout_task;
128 * The root of the received JSON or NULL
133 * The plugin result processor
135 GNUNET_REST_ResultProcessor proc;
138 * The closure of the result processor
143 * The name to look up
148 * The subsystem set from REST
158 * The data from the REST request
163 * the length of the REST data
176 * Cleanup lookup handle
177 * @praram handle Handle to clean up
180 cleanup_handle (struct RequestHandle *handle)
182 struct EgoEntry *ego_entry;
183 struct EgoEntry *ego_tmp;
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 if (NULL != handle->json_root)
187 json_decref (handle->json_root);
188 if (NULL != handle->name)
189 GNUNET_free (handle->name);
190 if (NULL != handle->timeout_task)
191 GNUNET_SCHEDULER_cancel (handle->timeout_task);
192 if (NULL != handle->identity_handle)
193 GNUNET_IDENTITY_disconnect (handle->identity_handle);
194 if (NULL != handle->subsys)
195 GNUNET_free (handle->subsys);
196 if (NULL != handle->url)
197 GNUNET_free (handle->url);
198 for (ego_entry = handle->ego_head;
202 ego_entry = ego_entry->next;
203 GNUNET_free (ego_tmp->identifier);
204 GNUNET_free (ego_tmp);
206 GNUNET_free (handle);
211 * Task run on shutdown. Cleans up everything.
214 * @param tc scheduler context
218 const struct GNUNET_SCHEDULER_TaskContext *tc)
220 struct RequestHandle *handle = cls;
221 struct MHD_Response *resp = GNUNET_REST_create_json_response (NULL);
222 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
223 cleanup_handle (handle);
227 * Callback for IDENTITY_get()
229 * @param cls the RequestHandle
230 * @param ego the Ego found
231 * @param ctx the context
232 * @param name the id of the ego
235 get_ego_for_subsys (void *cls,
236 struct GNUNET_IDENTITY_Ego *ego,
240 struct RequestHandle *handle = cls;
241 struct JsonApiObject *json_object;
242 struct JsonApiResource *json_resource;
243 struct EgoEntry *ego_entry;
244 struct MHD_Response *resp;
249 json_object = GNUNET_REST_jsonapi_object_new ();
251 for (ego_entry = handle->ego_head;
253 ego_entry = ego_entry->next)
255 if ( (NULL != name) && (0 != strcmp (name, ego_entry->identifier)) )
259 json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->identifier);
260 keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk);
261 key_json = json_string (keystring);
262 GNUNET_REST_jsonapi_resource_add_attr (json_resource, GNUNET_REST_JSONAPI_IDENTITY_KEY, key_json);
263 json_decref (key_json);
264 GNUNET_free (keystring);
265 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
268 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
270 GNUNET_REST_jsonapi_object_delete (json_object);
271 GNUNET_SCHEDULER_add_now (&do_error, handle);
274 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
276 resp = GNUNET_REST_create_json_response (result_str);
277 GNUNET_REST_jsonapi_object_delete (json_object);
278 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
279 GNUNET_free (result_str);
280 cleanup_handle (handle);
284 * Create a response with requested ego(s)
286 * @param handle the RequestHandle
289 ego_info_response (struct RestConnectionDataHandle *con,
297 struct RequestHandle *handle = cls;
298 struct EgoEntry *ego_entry;
299 struct GNUNET_HashCode key;
300 struct MHD_Response *resp;
301 struct JsonApiObject *json_object;
302 struct JsonApiResource *json_resource;
305 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, EGO_NAMESPACE))
307 resp = GNUNET_REST_create_json_response (NULL);
308 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
309 cleanup_handle (handle);
313 if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) {
314 GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key);
316 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
319 subsys_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
321 if (NULL != subsys_val)
323 GNUNET_asprintf (&handle->subsys, "%s", subsys_val);
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for %s's ego\n", subsys_val);
325 handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
333 json_object = GNUNET_REST_jsonapi_object_new ();
334 egoname = &handle->url[strlen (EGO_NAMESPACE)+1];
336 if (strlen (EGO_NAMESPACE) == strlen (handle->url))
342 for (ego_entry = handle->ego_head;
344 ego_entry = ego_entry->next)
346 if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) )
348 keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk);
349 json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->identifier);
350 key_str = json_string (keystring);
351 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
352 GNUNET_REST_JSONAPI_IDENTITY_KEY,
354 json_decref (key_str);
355 GNUNET_free (keystring);
356 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
359 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
361 resp = GNUNET_REST_create_json_response (result_str);
362 GNUNET_REST_jsonapi_object_delete (json_object);
363 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
364 GNUNET_free (result_str);
365 cleanup_handle (handle);
369 do_finished (void *cls, const char *emsg)
371 struct RequestHandle *handle = cls;
372 struct MHD_Response *resp;
377 GNUNET_SCHEDULER_add_now (&do_error, handle);
380 resp = GNUNET_REST_create_json_response (NULL);
381 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
382 cleanup_handle (handle);
386 set_finished (void *cls, const char *emsg)
388 struct RequestHandle *handle = cls;
389 struct MHD_Response *resp;
394 GNUNET_SCHEDULER_add_now (&do_error, handle);
397 resp = GNUNET_REST_create_json_response (NULL);
398 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
399 cleanup_handle (handle);
403 create_finished (void *cls, const char *emsg)
405 struct RequestHandle *handle = cls;
406 struct MHD_Response *resp;
411 GNUNET_SCHEDULER_add_now (&do_error, handle);
414 resp = GNUNET_REST_create_json_response (NULL);
415 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
416 cleanup_handle (handle);
420 ego_create_cont (struct RestConnectionDataHandle *con,
424 struct RequestHandle *handle = cls;
425 struct EgoEntry *ego_entry;
426 struct MHD_Response *resp;
427 struct JsonApiObject *json_obj;
428 struct JsonApiResource *json_res;
429 json_t *egoname_json;
430 char term_data[handle->data_size+1];
433 if (strlen (API_NAMESPACE) != strlen (handle->url))
435 GNUNET_SCHEDULER_add_now (&do_error, handle);
438 if (0 >= handle->data_size)
440 GNUNET_SCHEDULER_add_now (&do_error, handle);
443 term_data[handle->data_size] = '\0';
444 memcpy (term_data, handle->data, handle->data_size);
445 json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
447 if (NULL == json_obj)
449 GNUNET_SCHEDULER_add_now (&do_error, handle);
452 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
454 GNUNET_REST_jsonapi_object_delete (json_obj);
455 GNUNET_SCHEDULER_add_now (&do_error, handle);
459 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
460 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
462 GNUNET_REST_jsonapi_object_delete (json_obj);
463 resp = GNUNET_REST_create_json_response (NULL);
464 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
465 cleanup_handle (handle);
469 egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_KEY_ID);
470 if (!json_is_string (egoname_json))
472 GNUNET_REST_jsonapi_object_delete (json_obj);
473 GNUNET_SCHEDULER_add_now (&do_error, handle);
476 egoname = json_string_value (egoname_json);
477 for (ego_entry = handle->ego_head;
479 ego_entry = ego_entry->next)
481 if (0 == strcasecmp (egoname, ego_entry->identifier))
483 json_decref (egoname_json);
484 GNUNET_REST_jsonapi_object_delete (json_obj);
485 resp = GNUNET_REST_create_json_response (NULL);
486 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
487 cleanup_handle (handle);
491 GNUNET_asprintf (&handle->name, "%s", egoname);
492 json_decref (egoname_json);
493 GNUNET_REST_jsonapi_object_delete (json_obj);
494 handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
501 subsys_set_cont (struct RestConnectionDataHandle *con,
505 struct JsonApiObject *json_obj;
506 struct JsonApiResource *json_res;
509 struct RequestHandle *handle = cls;
510 char term_data[handle->data_size+1];
511 struct EgoEntry *ego_entry;
512 struct MHD_Response *resp;
513 int ego_exists = GNUNET_NO;
516 if (strlen (API_NAMESPACE) > strlen (handle->url))
518 GNUNET_SCHEDULER_add_now (&do_error, handle);
522 egoname = &handle->url[strlen(EGO_NAMESPACE)+1];
523 for (ego_entry = handle->ego_head;
525 ego_entry = ego_entry->next)
527 if (0 == strcasecmp (egoname, ego_entry->identifier))
529 ego_exists = GNUNET_YES;
533 if (GNUNET_NO == ego_exists)
535 resp = GNUNET_REST_create_json_response (NULL);
536 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
537 cleanup_handle (handle);
541 if (0 >= handle->data_size)
543 GNUNET_SCHEDULER_add_now (&do_error, handle);
547 term_data[handle->data_size] = '\0';
548 memcpy (term_data, handle->data, handle->data_size);
549 json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
551 if (NULL == json_obj)
553 GNUNET_SCHEDULER_add_now (&do_error, handle);
556 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
558 GNUNET_REST_jsonapi_object_delete (json_obj);
559 GNUNET_SCHEDULER_add_now (&do_error, handle);
563 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
564 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_id (json_res, egoname))
566 GNUNET_REST_jsonapi_object_delete (json_obj);
567 GNUNET_SCHEDULER_add_now (&do_error, handle);
571 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_id (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
573 GNUNET_REST_jsonapi_object_delete (json_obj);
574 GNUNET_SCHEDULER_add_now (&do_error, handle);
578 subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM);
579 if (!json_is_string (subsys_json))
581 json_decref (subsys_json);
582 GNUNET_REST_jsonapi_object_delete (json_obj);
583 GNUNET_SCHEDULER_add_now (&do_error, handle);
586 subsys = json_string_value (subsys_json);
587 GNUNET_asprintf (&handle->subsys, "%s", subsys);
588 json_decref (subsys_json);
589 GNUNET_REST_jsonapi_object_delete (json_obj);
590 handle->op = GNUNET_IDENTITY_set (handle->identity_handle,
598 ego_delete_cont (struct RestConnectionDataHandle *con_handle,
603 struct EgoEntry *ego_entry;
604 struct MHD_Response *resp;
605 struct RequestHandle *handle = cls;
606 int ego_exists = GNUNET_NO;
608 if (strlen (API_NAMESPACE) >= strlen (handle->url))
610 GNUNET_SCHEDULER_add_now (&do_error, handle);
614 egoname = &handle->url[strlen(API_NAMESPACE)+1];
615 for (ego_entry = handle->ego_head;
617 ego_entry = ego_entry->next)
619 if (0 == strcasecmp (egoname, ego_entry->identifier))
621 ego_exists = GNUNET_YES;
625 if (GNUNET_NO == ego_exists)
627 resp = GNUNET_REST_create_json_response (NULL);
628 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
629 cleanup_handle (handle);
632 handle->op = GNUNET_IDENTITY_delete (handle->identity_handle,
640 init_cont (struct RequestHandle *handle)
642 static const struct GNUNET_REST_RestConnectionHandler handlers[] = {
643 {MHD_HTTP_METHOD_GET, API_NAMESPACE, &ego_info_response},
644 {MHD_HTTP_METHOD_POST, API_NAMESPACE, &ego_create_cont},
645 {MHD_HTTP_METHOD_PUT, API_NAMESPACE, &subsys_set_cont},
646 {MHD_HTTP_METHOD_DELETE, API_NAMESPACE, &ego_delete_cont},
647 GNUNET_REST_HANDLER_END
650 if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle))
651 GNUNET_SCHEDULER_add_now (&do_error, handle);
655 * If listing is enabled, prints information about the egos.
657 * This function is initially called for all egos and then again
658 * whenever a ego's identifier changes or if it is deleted. At the
659 * end of the initial pass over all egos, the function is once called
660 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
661 * be invoked in the future or that there was an error.
663 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
664 * this function is only called ONCE, and 'NULL' being passed in
665 * 'ego' does indicate an error (i.e. name is taken or no default
666 * value is known). If 'ego' is non-NULL and if '*ctx'
667 * is set in those callbacks, the value WILL be passed to a subsequent
668 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
669 * that one was not NULL).
671 * When an identity is renamed, this function is called with the
672 * (known) ego but the NEW identifier.
674 * When an identity is deleted, this function is called with the
675 * (known) ego and "NULL" for the 'identifier'. In this case,
676 * the 'ego' is henceforth invalid (and the 'ctx' should also be
680 * @param ego ego handle
681 * @param ctx context for application to store data for this ego
682 * (during the lifetime of this process, initially NULL)
683 * @param identifier identifier assigned by the user for this ego,
684 * NULL if the user just deleted the ego and it
685 * must thus no longer be used
689 struct GNUNET_IDENTITY_Ego *ego,
691 const char *identifier)
693 struct RequestHandle *handle = cls;
694 struct EgoEntry *ego_entry;
696 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
698 handle->state = ID_REST_STATE_POST_INIT;
702 if (ID_REST_STATE_INIT == handle->state) {
703 ego_entry = GNUNET_new (struct EgoEntry);
704 GNUNET_IDENTITY_ego_get_public_key (ego, &(ego_entry->pk));
705 ego_entry->ego = ego;
706 GNUNET_asprintf (&ego_entry->identifier, "%s", identifier);
707 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
713 * Function processing the REST call
715 * @param method HTTP method
716 * @param url URL of the HTTP request
717 * @param data body of the HTTP request (optional)
718 * @param data_size length of the body
719 * @param proc callback function for the result
720 * @param proc_cls closure for callback function
721 * @return GNUNET_OK if request accepted
724 rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle,
725 GNUNET_REST_ResultProcessor proc,
728 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
732 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
734 handle->proc_cls = proc_cls;
736 handle->state = ID_REST_STATE_INIT;
737 handle->conndata_handle = conndata_handle;
738 handle->data = conndata_handle->data;
739 handle->data_size = conndata_handle->data_size;
740 handle->method = conndata_handle->method;
741 GNUNET_asprintf (&handle->url, "%s", conndata_handle->url);
742 if (handle->url[strlen (handle->url)-1] == '/')
743 handle->url[strlen (handle->url)-1] = '\0';
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
749 handle->timeout_task =
750 GNUNET_SCHEDULER_add_delayed (handle->timeout,
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 * Entry point for the plugin.
762 * @param cls Config info
763 * @return NULL on error, otherwise the plugin context
766 libgnunet_plugin_rest_identity_init (void *cls)
768 static struct Plugin plugin;
770 struct GNUNET_REST_Plugin *api;
772 if (NULL != plugin.cfg)
773 return NULL; /* can only initialize once! */
774 memset (&plugin, 0, sizeof (struct Plugin));
776 api = GNUNET_new (struct GNUNET_REST_Plugin);
778 api->name = API_NAMESPACE;
779 api->process_request = &rest_identity_process_request;
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
781 _("Identity REST API initialized\n"));
787 * Exit point from the plugin.
789 * @param cls the plugin context (as returned by "init")
790 * @return always NULL
793 libgnunet_plugin_rest_identity_done (void *cls)
795 struct GNUNET_REST_Plugin *api = cls;
796 struct Plugin *plugin = api->cls;
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Identity REST plugin is finished\n");
805 /* end of plugin_rest_gns.c */