aboutsummaryrefslogtreecommitdiff
path: root/src/identity/plugin_rest_identity.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2015-09-11 15:56:39 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2015-09-11 15:56:39 +0000
commitc8aace43c41d50e03bd28b6e696b33c1da8b6c4c (patch)
tree375181e89bd4f099e0a2039ed0962bf7cfd48131 /src/identity/plugin_rest_identity.c
parenta12f20bd4c621a7b5e88ca52830ad1bb74a8e2d8 (diff)
downloadgnunet-c8aace43c41d50e03bd28b6e696b33c1da8b6c4c.tar.gz
gnunet-c8aace43c41d50e03bd28b6e696b33c1da8b6c4c.zip
- move rest plugins into rest directory where they belong
Diffstat (limited to 'src/identity/plugin_rest_identity.c')
-rw-r--r--src/identity/plugin_rest_identity.c1073
1 files changed, 0 insertions, 1073 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c
deleted file mode 100644
index 870ed3446..000000000
--- a/src/identity/plugin_rest_identity.c
+++ /dev/null
@@ -1,1073 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20/**
21 * @author Martin Schanzenbach
22 * @file identity/plugin_rest_identity.c
23 * @brief GNUnet Namestore REST plugin
24 *
25 */
26
27#include "platform.h"
28#include "gnunet_rest_plugin.h"
29#include "gnunet_identity_service.h"
30#include "gnunet_rest_lib.h"
31#include "microhttpd.h"
32#include <jansson.h>
33#include "gnunet_signatures.h"
34
35/**
36 * REST root namespace
37 */
38#define GNUNET_REST_API_NS_IDENTITY "/identity"
39
40/**
41 * State while collecting all egos
42 */
43#define ID_REST_STATE_INIT 0
44
45/**
46 * Done collecting egos
47 */
48#define ID_REST_STATE_POST_INIT 1
49
50/**
51 * Resource type
52 */
53#define GNUNET_REST_JSONAPI_IDENTITY_EGO "ego"
54
55/**
56 * Name attribute
57 */
58#define GNUNET_REST_JSONAPI_IDENTITY_NAME "name"
59
60/**
61 * Attribute to rename "name" TODO we changed id to the pubkey
62 * so this can be unified with "name"
63 */
64#define GNUNET_REST_JSONAPI_IDENTITY_NEWNAME "newname"
65
66/**
67 * URL parameter to change the subsytem for ego
68 */
69#define GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM "subsystem"
70
71
72/**
73 * URL parameter to create a GNUid token for a specific audience
74 */
75#define GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN "create_token_for"
76
77/**
78 * Attribute containing the GNUid token if
79 * GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN was requested
80 */
81#define GNUNET_REST_JSONAPI_IDENTITY_GNUID "gnuid_token"
82
83/**
84 * Error messages
85 */
86#define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid"
87#define GNUNET_REST_ERROR_NO_DATA "No data"
88
89/**
90 * GNUid token lifetime
91 */
92#define GNUNET_GNUID_TOKEN_EXPIRATION_SECONDS 360
93
94/**
95 * The configuration handle
96 */
97const struct GNUNET_CONFIGURATION_Handle *cfg;
98
99/**
100 * HTTP methods allows for this plugin
101 */
102static char* allow_methods;
103
104/**
105 * @brief struct returned by the initialization function of the plugin
106 */
107struct Plugin
108{
109 const struct GNUNET_CONFIGURATION_Handle *cfg;
110};
111
112/**
113 * The ego list
114 */
115struct EgoEntry
116{
117 /**
118 * DLL
119 */
120 struct EgoEntry *next;
121
122 /**
123 * DLL
124 */
125 struct EgoEntry *prev;
126
127 /**
128 * Ego Identifier
129 */
130 char *identifier;
131
132 /**
133 * Public key string
134 */
135 char *keystring;
136
137 /**
138 * The Ego
139 */
140 struct GNUNET_IDENTITY_Ego *ego;
141};
142
143
144struct RequestHandle
145{
146 /**
147 * Ego list
148 */
149 struct EgoEntry *ego_head;
150
151 /**
152 * Ego list
153 */
154 struct EgoEntry *ego_tail;
155
156 /**
157 * Handle to the rest connection
158 */
159 struct RestConnectionDataHandle *conndata_handle;
160
161 /**
162 * The processing state
163 */
164 int state;
165
166 /**
167 * Handle to GNS service.
168 */
169 struct GNUNET_IDENTITY_Handle *identity_handle;
170
171 /**
172 * IDENTITY Operation
173 */
174 struct GNUNET_IDENTITY_Operation *op;
175
176 /**
177 * Desired timeout for the lookup (default is no timeout).
178 */
179 struct GNUNET_TIME_Relative timeout;
180
181 /**
182 * ID of a task associated with the resolution process.
183 */
184 struct GNUNET_SCHEDULER_Task * timeout_task;
185
186 /**
187 * The plugin result processor
188 */
189 GNUNET_REST_ResultProcessor proc;
190
191 /**
192 * The closure of the result processor
193 */
194 void *proc_cls;
195
196 /**
197 * The name to look up
198 */
199 char *name;
200
201 /**
202 * The subsystem set from REST
203 */
204 char *subsys;
205
206 /**
207 * The url
208 */
209 char *url;
210
211 /**
212 * The data from the REST request
213 */
214 const char* data;
215
216 /**
217 * the length of the REST data
218 */
219 size_t data_size;
220
221 /**
222 * HTTP method
223 */
224 const char* method;
225
226 /**
227 * Error response message
228 */
229 char *emsg;
230
231};
232
233
234/**
235 * Cleanup lookup handle
236 * @param handle Handle to clean up
237 */
238static void
239cleanup_handle (struct RequestHandle *handle)
240{
241 struct EgoEntry *ego_entry;
242 struct EgoEntry *ego_tmp;
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Cleaning up\n");
245 if (NULL != handle->name)
246 GNUNET_free (handle->name);
247 if (NULL != handle->timeout_task)
248 GNUNET_SCHEDULER_cancel (handle->timeout_task);
249 if (NULL != handle->identity_handle)
250 GNUNET_IDENTITY_disconnect (handle->identity_handle);
251 if (NULL != handle->subsys)
252 GNUNET_free (handle->subsys);
253 if (NULL != handle->url)
254 GNUNET_free (handle->url);
255 if (NULL != handle->emsg)
256 GNUNET_free (handle->emsg);
257 for (ego_entry = handle->ego_head;
258 NULL != ego_entry;)
259 {
260 ego_tmp = ego_entry;
261 ego_entry = ego_entry->next;
262 GNUNET_free (ego_tmp->identifier);
263 GNUNET_free (ego_tmp->keystring);
264 GNUNET_free (ego_tmp);
265 }
266 GNUNET_free (handle);
267}
268
269
270/**
271 * Task run on shutdown. Cleans up everything.
272 *
273 * @param cls unused
274 * @param tc scheduler context
275 */
276static void
277do_error (void *cls,
278 const struct GNUNET_SCHEDULER_TaskContext *tc)
279{
280 struct RequestHandle *handle = cls;
281 struct MHD_Response *resp;
282 char *json_error;
283
284 GNUNET_asprintf (&json_error,
285 "{Error while processing request: %s}",
286 &handle->emsg);
287
288 resp = GNUNET_REST_create_json_response (json_error);
289 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
290 cleanup_handle (handle);
291 GNUNET_free (json_error);
292}
293
294/**
295 * Build a GNUid token for identity
296 * @param handle the handle
297 * @param ego_entry the ego to build the token for
298 * @param name name of the ego
299 * @param token_aud token audience
300 * @param token the resulting gnuid token
301 */
302static void
303make_gnuid_token (struct RequestHandle *handle,
304 struct EgoEntry *ego_entry,
305 const char *name,
306 const char *token_aud,
307 char **token)
308{
309 uint64_t time;
310 char *header_str;
311 char *payload_str;
312 char *header_base64;
313 char *payload_base64;
314 char *sig_str;
315 json_t *header;
316 json_t *payload;
317 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
318 struct GNUNET_CRYPTO_EcdsaSignature sig;
319 struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
320
321 time = GNUNET_TIME_absolute_get().abs_value_us;
322 header = json_object ();
323 json_object_set_new (header, "alg", json_string ("ED512"));
324 json_object_set_new (header, "typ", json_string ("JWT"));
325
326 payload = json_object ();
327 json_object_set_new (payload, "iss", json_string (ego_entry->keystring));
328 json_object_set_new (payload, "sub", json_string (name));
329 json_object_set_new (payload, "nbf", json_integer (time));
330 json_object_set_new (payload, "iat", json_integer (time));
331 json_object_set_new (payload, "exp", json_integer (time+GNUNET_GNUID_TOKEN_EXPIRATION_SECONDS));
332 json_object_set_new (payload, "aud", json_string (token_aud));
333 header_str = json_dumps (header, JSON_COMPACT);
334 GNUNET_STRINGS_base64_encode (header_str,
335 strlen (header_str),
336 &header_base64);
337 char* padding = strtok(header_base64, "=");
338 while (NULL != padding)
339 padding = strtok(NULL, "=");
340
341 payload_str = json_dumps (payload, JSON_COMPACT);
342 GNUNET_STRINGS_base64_encode (payload_str,
343 strlen (payload_str),
344 &payload_base64);
345 padding = strtok(payload_base64, "=");
346 while (NULL != padding)
347 padding = strtok(NULL, "=");
348
349 GNUNET_asprintf (token, "%s,%s", header_base64, payload_base64);
350 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
351 purpose =
352 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
353 strlen (*token));
354 purpose->size =
355 htonl (strlen (*token) + sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
356 purpose->purpose = htonl(GNUNET_SIGNATURE_PURPOSE_GNUID_TOKEN);
357 memcpy (&purpose[1], *token, strlen (*token));
358 if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_sign (priv_key,
359 purpose,
360 &sig))
361 GNUNET_break(0);
362 GNUNET_free (*token);
363 sig_str = GNUNET_STRINGS_data_to_string_alloc (&sig,
364 sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
365 GNUNET_asprintf (token, "%s.%s.%s",
366 header_base64, payload_base64, sig_str);
367 GNUNET_free (sig_str);
368 GNUNET_free (header_str);
369 GNUNET_free (header_base64);
370 GNUNET_free (payload_str);
371 GNUNET_free (payload_base64);
372 GNUNET_free (purpose);
373 json_decref (header);
374 json_decref (payload);
375}
376
377/**
378 * Callback for IDENTITY_get()
379 *
380 * @param cls the RequestHandle
381 * @param ego the Ego found
382 * @param ctx the context
383 * @param name the id of the ego
384 */
385static void
386get_ego_for_subsys (void *cls,
387 struct GNUNET_IDENTITY_Ego *ego,
388 void **ctx,
389 const char *name)
390{
391 struct RequestHandle *handle = cls;
392 struct JsonApiObject *json_object;
393 struct JsonApiResource *json_resource;
394 struct EgoEntry *ego_entry;
395 struct MHD_Response *resp;
396 json_t *name_json;
397 char *result_str;
398
399 json_object = GNUNET_REST_jsonapi_object_new ();
400
401 for (ego_entry = handle->ego_head;
402 NULL != ego_entry;
403 ego_entry = ego_entry->next)
404 {
405 if ( (NULL != name) && (0 != strcmp (name, ego_entry->identifier)) )
406 continue;
407 if (NULL == name)
408 continue;
409 json_resource = GNUNET_REST_jsonapi_resource_new
410 (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring);
411 name_json = json_string (ego_entry->identifier);
412 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
413 GNUNET_REST_JSONAPI_IDENTITY_NAME,
414 name_json);
415 json_decref (name_json);
416 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
417 break;
418 }
419 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
420 {
421 GNUNET_REST_jsonapi_object_delete (json_object);
422 handle->emsg = GNUNET_strdup("No identity matches results!");
423 GNUNET_SCHEDULER_add_now (&do_error, handle);
424 return;
425 }
426 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
428 resp = GNUNET_REST_create_json_response (result_str);
429 GNUNET_REST_jsonapi_object_delete (json_object);
430 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
431 GNUNET_free (result_str);
432 cleanup_handle (handle);
433}
434
435/**
436 * Create a response with requested ego(s)
437 *
438 * @param con the Rest handle
439 * @param url the requested url
440 * @param cls the request handle
441 */
442static void
443ego_info_response (struct RestConnectionDataHandle *con,
444 const char *url,
445 void *cls)
446{
447 const char *egoname;
448 char *result_str;
449 char *subsys_val;
450 char *create_token_for;
451 char *token;
452 char *keystring;
453 struct RequestHandle *handle = cls;
454 struct EgoEntry *ego_entry;
455 struct GNUNET_HashCode key;
456 struct MHD_Response *resp;
457 struct JsonApiObject *json_object;
458 struct JsonApiResource *json_resource;
459 json_t *name_str;
460 json_t *token_str;
461
462 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY))
463 {
464 resp = GNUNET_REST_create_json_response (NULL);
465 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
466 cleanup_handle (handle);
467 return;
468 }
469 egoname = NULL;
470 keystring = NULL;
471 if (strlen (GNUNET_REST_API_NS_IDENTITY) < strlen (handle->url))
472 {
473 keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY)+1];
474 //Return all egos
475 for (ego_entry = handle->ego_head;
476 NULL != ego_entry;
477 ego_entry = ego_entry->next)
478 {
479 if ( (NULL != keystring) && (0 != strcmp (keystring, ego_entry->keystring)) )
480 continue;
481 egoname = ego_entry->identifier;
482 }
483 }
484
485 if ( NULL == egoname ) {
486 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM,
487 strlen (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM),
488 &key);
489 if ( GNUNET_YES ==
490 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
491 &key) )
492 {
493 subsys_val = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
494 &key);
495 if (NULL != subsys_val)
496 {
497 GNUNET_asprintf (&handle->subsys, "%s", subsys_val);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for %s's ego\n", subsys_val);
499 handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
500 handle->subsys,
501 &get_ego_for_subsys,
502 handle);
503 return;
504 }
505 }
506 }
507
508 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN,
509 strlen (GNUNET_REST_JSONAPI_IDENTITY_CREATE_TOKEN),
510 &key);
511
512 //Token audience
513 create_token_for = NULL;
514 if ( GNUNET_YES ==
515 GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map,
516 &key) )
517 create_token_for = GNUNET_CONTAINER_multihashmap_get (handle->conndata_handle->url_param_map,
518 &key);
519
520 json_object = GNUNET_REST_jsonapi_object_new ();
521
522 //Return all egos
523 for (ego_entry = handle->ego_head;
524 NULL != ego_entry;
525 ego_entry = ego_entry->next)
526 {
527 if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) )
528 continue;
529 json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO,
530 ego_entry->keystring);
531 name_str = json_string (ego_entry->identifier);
532 GNUNET_REST_jsonapi_resource_add_attr (
533 json_resource,
534 GNUNET_REST_JSONAPI_IDENTITY_NAME,
535 name_str);
536 json_decref (name_str);
537 if (NULL != create_token_for)
538 {
539 make_gnuid_token (handle,
540 ego_entry,
541 ego_entry->identifier,
542 create_token_for,
543 &token);
544 token_str = json_string (token);
545 GNUNET_free (token);
546 GNUNET_REST_jsonapi_resource_add_attr (json_resource,
547 GNUNET_REST_JSONAPI_IDENTITY_GNUID,
548 token_str);
549 json_decref (token_str);
550 }
551 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource);
552 }
553 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object))
554 {
555 GNUNET_REST_jsonapi_object_delete (json_object);
556 handle->emsg = GNUNET_strdup ("No identities found!");
557 GNUNET_SCHEDULER_add_now (&do_error, handle);
558 return;
559 }
560 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str);
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
562 resp = GNUNET_REST_create_json_response (result_str);
563 GNUNET_REST_jsonapi_object_delete (json_object);
564 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
565 GNUNET_free (result_str);
566 cleanup_handle (handle);
567}
568
569/**
570 * Processing finished
571 *
572 * @param cls request handle
573 * @param emsg error message
574 */
575static void
576do_finished (void *cls, const char *emsg)
577{
578 struct RequestHandle *handle = cls;
579 struct MHD_Response *resp;
580
581 handle->op = NULL;
582 if (NULL != emsg)
583 {
584 handle->emsg = GNUNET_strdup (emsg);
585 GNUNET_SCHEDULER_add_now (&do_error, handle);
586 return;
587 }
588 resp = GNUNET_REST_create_json_response (NULL);
589 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
590 cleanup_handle (handle);
591}
592
593/**
594 * Create a new ego
595 *
596 * @param con rest handle
597 * @param url url
598 * @param cls request handle
599 */
600static void
601ego_create_cont (struct RestConnectionDataHandle *con,
602 const char *url,
603 void *cls)
604{
605 struct RequestHandle *handle = cls;
606 struct EgoEntry *ego_entry;
607 struct MHD_Response *resp;
608 struct JsonApiObject *json_obj;
609 struct JsonApiResource *json_res;
610 json_t *egoname_json;
611 const char* egoname;
612 char term_data[handle->data_size+1];
613
614 if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url))
615 {
616 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
617 GNUNET_SCHEDULER_add_now (&do_error, handle);
618 return;
619 }
620 if (0 >= handle->data_size)
621 {
622 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA);
623 GNUNET_SCHEDULER_add_now (&do_error, handle);
624 return;
625 }
626 term_data[handle->data_size] = '\0';
627 memcpy (term_data, handle->data, handle->data_size);
628 json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
629 if (NULL == json_obj)
630 {
631 GNUNET_SCHEDULER_add_now (&do_error, handle);
632 return;
633 }
634 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
635 {
636 GNUNET_REST_jsonapi_object_delete (json_obj);
637 handle->emsg = GNUNET_strdup ("Provided resource count invalid");
638 GNUNET_SCHEDULER_add_now (&do_error, handle);
639 return;
640 }
641 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
642 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
643 {
644 GNUNET_REST_jsonapi_object_delete (json_obj);
645 resp = GNUNET_REST_create_json_response (NULL);
646 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
647 cleanup_handle (handle);
648 return;
649 }
650 egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME);
651 if (!json_is_string (egoname_json))
652 {
653 GNUNET_REST_jsonapi_object_delete (json_obj);
654 handle->emsg = GNUNET_strdup ("No name provided");
655 GNUNET_SCHEDULER_add_now (&do_error, handle);
656 return;
657 }
658 egoname = json_string_value (egoname_json);
659 for (ego_entry = handle->ego_head;
660 NULL != ego_entry;
661 ego_entry = ego_entry->next)
662 {
663 if (0 == strcasecmp (egoname, ego_entry->identifier))
664 {
665 GNUNET_REST_jsonapi_object_delete (json_obj);
666 resp = GNUNET_REST_create_json_response (NULL);
667 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
668 cleanup_handle (handle);
669 return;
670 }
671 }
672 GNUNET_asprintf (&handle->name, "%s", egoname);
673 GNUNET_REST_jsonapi_object_delete (json_obj);
674 handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
675 handle->name,
676 &do_finished,
677 handle);
678}
679
680
681/**
682 * Handle ego edit request
683 *
684 * @param con rest connection handle
685 * @param url the url that is requested
686 * @param cls the RequestHandle
687 */
688static void
689ego_edit_cont (struct RestConnectionDataHandle *con,
690 const char *url,
691 void *cls)
692{
693 struct JsonApiObject *json_obj;
694 struct JsonApiResource *json_res;
695 struct RequestHandle *handle = cls;
696 struct EgoEntry *ego_entry;
697 struct MHD_Response *resp;
698 json_t *subsys_json;
699 json_t *name_json;
700 const char *keystring;
701 const char *subsys;
702 const char *newname;
703 char term_data[handle->data_size+1];
704 int ego_exists = GNUNET_NO;
705
706 if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url))
707 {
708 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
709 GNUNET_SCHEDULER_add_now (&do_error, handle);
710 return;
711 }
712
713 keystring = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
714
715 for (ego_entry = handle->ego_head;
716 NULL != ego_entry;
717 ego_entry = ego_entry->next)
718 {
719 if (0 != strcasecmp (keystring, ego_entry->keystring))
720 continue;
721 ego_exists = GNUNET_YES;
722 break;
723 }
724
725 if (GNUNET_NO == ego_exists)
726 {
727 resp = GNUNET_REST_create_json_response (NULL);
728 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
729 cleanup_handle (handle);
730 return;
731 }
732
733 if (0 >= handle->data_size)
734 {
735 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA);
736 GNUNET_SCHEDULER_add_now (&do_error, handle);
737 return;
738 }
739
740 term_data[handle->data_size] = '\0';
741 memcpy (term_data, handle->data, handle->data_size);
742 json_obj = GNUNET_REST_jsonapi_object_parse (term_data);
743
744 if (NULL == json_obj)
745 {
746 handle->emsg = GNUNET_strdup ("Data invalid");
747 GNUNET_SCHEDULER_add_now (&do_error, handle);
748 return;
749 }
750
751 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj))
752 {
753 GNUNET_REST_jsonapi_object_delete (json_obj);
754 handle->emsg = GNUNET_strdup ("Resource amount invalid");
755 GNUNET_SCHEDULER_add_now (&do_error, handle);
756 return;
757 }
758 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0);
759
760 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
761 {
762 GNUNET_REST_jsonapi_object_delete (json_obj);
763 handle->emsg = GNUNET_strdup ("Resource type invalid");
764 GNUNET_SCHEDULER_add_now (&do_error, handle);
765 return;
766 }
767
768 //This is a rename
769 name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res,
770 GNUNET_REST_JSONAPI_IDENTITY_NEWNAME);
771 if ((NULL != name_json) && json_is_string (name_json))
772 {
773 newname = json_string_value (name_json);
774 for (ego_entry = handle->ego_head;
775 NULL != ego_entry;
776 ego_entry = ego_entry->next)
777 {
778 if (0 == strcasecmp (newname, ego_entry->identifier) &&
779 0 != strcasecmp (keystring, ego_entry->keystring))
780 {
781 //Ego with same name not allowed
782 GNUNET_REST_jsonapi_object_delete (json_obj);
783 resp = GNUNET_REST_create_json_response (NULL);
784 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
785 cleanup_handle (handle);
786 return;
787 }
788 }
789 handle->op = GNUNET_IDENTITY_rename (handle->identity_handle,
790 ego_entry->identifier,
791 newname,
792 &do_finished,
793 handle);
794 GNUNET_REST_jsonapi_object_delete (json_obj);
795 return;
796 }
797
798 //Set subsystem
799 subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM);
800 if ( (NULL != subsys_json) && json_is_string (subsys_json))
801 {
802 subsys = json_string_value (subsys_json);
803 GNUNET_asprintf (&handle->subsys, "%s", subsys);
804 GNUNET_REST_jsonapi_object_delete (json_obj);
805 handle->op = GNUNET_IDENTITY_set (handle->identity_handle,
806 handle->subsys,
807 ego_entry->ego,
808 &do_finished,
809 handle);
810 return;
811 }
812 GNUNET_REST_jsonapi_object_delete (json_obj);
813 handle->emsg = GNUNET_strdup ("Subsystem not provided");
814 GNUNET_SCHEDULER_add_now (&do_error, handle);
815}
816
817void
818ego_delete_cont (struct RestConnectionDataHandle *con_handle,
819 const char* url,
820 void *cls)
821{
822 const char *keystring;
823 struct EgoEntry *ego_entry;
824 struct MHD_Response *resp;
825 struct RequestHandle *handle = cls;
826 int ego_exists = GNUNET_NO;
827
828 if (strlen (GNUNET_REST_API_NS_IDENTITY) >= strlen (handle->url))
829 {
830 handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID);
831 GNUNET_SCHEDULER_add_now (&do_error, handle);
832 return;
833 }
834
835 keystring = &handle->url[strlen(GNUNET_REST_API_NS_IDENTITY)+1];
836 for (ego_entry = handle->ego_head;
837 NULL != ego_entry;
838 ego_entry = ego_entry->next)
839 {
840 if (0 != strcasecmp (keystring, ego_entry->keystring))
841 continue;
842 ego_exists = GNUNET_YES;
843 break;
844 }
845 if (GNUNET_NO == ego_exists)
846 {
847 resp = GNUNET_REST_create_json_response (NULL);
848 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
849 cleanup_handle (handle);
850 return;
851 }
852 handle->op = GNUNET_IDENTITY_delete (handle->identity_handle,
853 ego_entry->identifier,
854 &do_finished,
855 handle);
856
857}
858
859
860/**
861 * Respond to OPTIONS request
862 *
863 * @param con_handle the connection handle
864 * @param url the url
865 * @param cls the RequestHandle
866 */
867static void
868options_cont (struct RestConnectionDataHandle *con_handle,
869 const char* url,
870 void *cls)
871{
872 struct MHD_Response *resp;
873 struct RequestHandle *handle = cls;
874
875 //For now, independent of path return all options
876 resp = GNUNET_REST_create_json_response (NULL);
877 MHD_add_response_header (resp,
878 "Access-Control-Allow-Methods",
879 allow_methods);
880 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
881 cleanup_handle (handle);
882 return;
883}
884
885/**
886 * Handle rest request
887 *
888 * @param handle the request handle
889 */
890static void
891init_cont (struct RequestHandle *handle)
892{
893 static const struct GNUNET_REST_RestConnectionHandler handlers[] = {
894 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY, &ego_info_response},
895 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create_cont},
896 {MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY, &ego_edit_cont},
897 {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY, &ego_delete_cont},
898 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY, &options_cont},
899 GNUNET_REST_HANDLER_END
900 };
901
902 if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, handlers, handle))
903 {
904 handle->emsg = GNUNET_strdup ("Request unsupported");
905 GNUNET_SCHEDULER_add_now (&do_error, handle);
906 }
907}
908
909/**
910 * If listing is enabled, prints information about the egos.
911 *
912 * This function is initially called for all egos and then again
913 * whenever a ego's identifier changes or if it is deleted. At the
914 * end of the initial pass over all egos, the function is once called
915 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
916 * be invoked in the future or that there was an error.
917 *
918 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
919 * this function is only called ONCE, and 'NULL' being passed in
920 * 'ego' does indicate an error (i.e. name is taken or no default
921 * value is known). If 'ego' is non-NULL and if '*ctx'
922 * is set in those callbacks, the value WILL be passed to a subsequent
923 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
924 * that one was not NULL).
925 *
926 * When an identity is renamed, this function is called with the
927 * (known) ego but the NEW identifier.
928 *
929 * When an identity is deleted, this function is called with the
930 * (known) ego and "NULL" for the 'identifier'. In this case,
931 * the 'ego' is henceforth invalid (and the 'ctx' should also be
932 * cleaned up).
933 *
934 * @param cls closure
935 * @param ego ego handle
936 * @param ctx context for application to store data for this ego
937 * (during the lifetime of this process, initially NULL)
938 * @param identifier identifier assigned by the user for this ego,
939 * NULL if the user just deleted the ego and it
940 * must thus no longer be used
941 */
942static void
943list_ego (void *cls,
944 struct GNUNET_IDENTITY_Ego *ego,
945 void **ctx,
946 const char *identifier)
947{
948 struct RequestHandle *handle = cls;
949 struct EgoEntry *ego_entry;
950 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
951
952 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
953 {
954 handle->state = ID_REST_STATE_POST_INIT;
955 init_cont (handle);
956 return;
957 }
958 if (ID_REST_STATE_INIT == handle->state) {
959 ego_entry = GNUNET_new (struct EgoEntry);
960 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
961 ego_entry->keystring =
962 GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
963 ego_entry->ego = ego;
964 GNUNET_asprintf (&ego_entry->identifier, "%s", identifier);
965 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,handle->ego_tail, ego_entry);
966 }
967
968}
969
970/**
971 * Function processing the REST call
972 *
973 * @param method HTTP method
974 * @param url URL of the HTTP request
975 * @param data body of the HTTP request (optional)
976 * @param data_size length of the body
977 * @param proc callback function for the result
978 * @param proc_cls closure for callback function
979 * @return GNUNET_OK if request accepted
980 */
981static void
982rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle,
983 GNUNET_REST_ResultProcessor proc,
984 void *proc_cls)
985{
986 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
987
988
989
990 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
991
992 handle->proc_cls = proc_cls;
993 handle->proc = proc;
994 handle->state = ID_REST_STATE_INIT;
995 handle->conndata_handle = conndata_handle;
996 handle->data = conndata_handle->data;
997 handle->data_size = conndata_handle->data_size;
998 handle->method = conndata_handle->method;
999 GNUNET_asprintf (&handle->url, "%s", conndata_handle->url);
1000 if (handle->url[strlen (handle->url)-1] == '/')
1001 handle->url[strlen (handle->url)-1] = '\0';
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Connecting...\n");
1004 handle->identity_handle = GNUNET_IDENTITY_connect (cfg,
1005 &list_ego,
1006 handle);
1007 GNUNET_strdup ("Timeout");
1008 handle->timeout_task =
1009 GNUNET_SCHEDULER_add_delayed (handle->timeout,
1010 &do_error,
1011 handle);
1012
1013
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "Connected\n");
1016}
1017
1018/**
1019 * Entry point for the plugin.
1020 *
1021 * @param cls Config info
1022 * @return NULL on error, otherwise the plugin context
1023 */
1024void *
1025libgnunet_plugin_rest_identity_init (void *cls)
1026{
1027 static struct Plugin plugin;
1028 struct GNUNET_REST_Plugin *api;
1029
1030 cfg = cls;
1031 if (NULL != plugin.cfg)
1032 return NULL; /* can only initialize once! */
1033 memset (&plugin, 0, sizeof (struct Plugin));
1034 plugin.cfg = cfg;
1035 api = GNUNET_new (struct GNUNET_REST_Plugin);
1036 api->cls = &plugin;
1037 api->name = GNUNET_REST_API_NS_IDENTITY;
1038 api->process_request = &rest_identity_process_request;
1039 GNUNET_asprintf (&allow_methods,
1040 "%s, %s, %s, %s, %s",
1041 MHD_HTTP_METHOD_GET,
1042 MHD_HTTP_METHOD_POST,
1043 MHD_HTTP_METHOD_PUT,
1044 MHD_HTTP_METHOD_DELETE,
1045 MHD_HTTP_METHOD_OPTIONS);
1046
1047 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1048 _("Identity REST API initialized\n"));
1049 return api;
1050}
1051
1052
1053/**
1054 * Exit point from the plugin.
1055 *
1056 * @param cls the plugin context (as returned by "init")
1057 * @return always NULL
1058 */
1059void *
1060libgnunet_plugin_rest_identity_done (void *cls)
1061{
1062 struct GNUNET_REST_Plugin *api = cls;
1063 struct Plugin *plugin = api->cls;
1064
1065 plugin->cfg = NULL;
1066 GNUNET_free_non_null (allow_methods);
1067 GNUNET_free (api);
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Identity REST plugin is finished\n");
1070 return NULL;
1071}
1072
1073/* end of plugin_rest_gns.c */