diff options
Diffstat (limited to 'src/identity/plugin_rest_identity.c')
-rw-r--r-- | src/identity/plugin_rest_identity.c | 614 |
1 files changed, 395 insertions, 219 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index a518a74cc..9a05a2054 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c | |||
@@ -29,22 +29,69 @@ | |||
29 | #include "microhttpd.h" | 29 | #include "microhttpd.h" |
30 | #include <jansson.h> | 30 | #include <jansson.h> |
31 | 31 | ||
32 | /** | ||
33 | * Identity Namespace | ||
34 | */ | ||
32 | #define GNUNET_REST_API_NS_IDENTITY "/identity" | 35 | #define GNUNET_REST_API_NS_IDENTITY "/identity" |
33 | 36 | ||
34 | /** | 37 | /** |
35 | * Parameter names | 38 | * Identity Namespace with public key specifier |
39 | */ | ||
40 | #define GNUNET_REST_API_NS_IDENTITY_ALL "/identity/all" | ||
41 | |||
42 | /** | ||
43 | * Identity Namespace with public key specifier | ||
36 | */ | 44 | */ |
37 | #define GNUNET_REST_PARAM_PUBKEY "pubkey" | 45 | #define GNUNET_REST_API_NS_IDENTITY_PUBKEY "/identity/pubkey" |
38 | #define GNUNET_REST_PARAM_SUBSYSTEM "subsystem" | ||
39 | #define GNUNET_REST_PARAM_NAME "name" | ||
40 | #define GNUNET_REST_PARAM_NEWNAME "newname" | ||
41 | 46 | ||
42 | /** | 47 | /** |
43 | * Error messages | 48 | * Identity Namespace with public key specifier |
49 | */ | ||
50 | #define GNUNET_REST_API_NS_IDENTITY_NAME "/identity/name" | ||
51 | |||
52 | /** | ||
53 | * Identity Subsystem Namespace | ||
54 | */ | ||
55 | #define GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM "/identity/subsystem" | ||
56 | |||
57 | /** | ||
58 | * Parameter public key | ||
59 | */ | ||
60 | #define GNUNET_REST_IDENTITY_PARAM_PUBKEY "pubkey" | ||
61 | |||
62 | /** | ||
63 | * Parameter subsystem | ||
64 | */ | ||
65 | #define GNUNET_REST_IDENTITY_PARAM_SUBSYSTEM "subsystem" | ||
66 | |||
67 | /** | ||
68 | * Parameter name | ||
69 | */ | ||
70 | #define GNUNET_REST_IDENTITY_PARAM_NAME "name" | ||
71 | |||
72 | /** | ||
73 | * Parameter new name | ||
74 | */ | ||
75 | #define GNUNET_REST_IDENTITY_PARAM_NEWNAME "newname" | ||
76 | |||
77 | /** | ||
78 | * Error message Unknown Error | ||
79 | */ | ||
80 | #define GNUNET_REST_IDENTITY_ERROR_UNKNOWN "Unknown error" | ||
81 | |||
82 | /** | ||
83 | * Error message Resource location invalid | ||
44 | */ | 84 | */ |
45 | #define GNUNET_REST_IDENTITY_ERROR_UNKNOWN "Unknown Error" | ||
46 | #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid" | 85 | #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid" |
86 | |||
87 | /** | ||
88 | * Error message No data | ||
89 | */ | ||
47 | #define GNUNET_REST_ERROR_NO_DATA "No data" | 90 | #define GNUNET_REST_ERROR_NO_DATA "No data" |
91 | |||
92 | /** | ||
93 | * Error message Data invalid | ||
94 | */ | ||
48 | #define GNUNET_REST_ERROR_DATA_INVALID "Data invalid" | 95 | #define GNUNET_REST_ERROR_DATA_INVALID "Data invalid" |
49 | 96 | ||
50 | /** | 97 | /** |
@@ -106,6 +153,9 @@ struct EgoEntry | |||
106 | struct GNUNET_IDENTITY_Ego *ego; | 153 | struct GNUNET_IDENTITY_Ego *ego; |
107 | }; | 154 | }; |
108 | 155 | ||
156 | /** | ||
157 | * The request handle | ||
158 | */ | ||
109 | struct RequestHandle | 159 | struct RequestHandle |
110 | { | 160 | { |
111 | /** | 161 | /** |
@@ -123,10 +173,6 @@ struct RequestHandle | |||
123 | */ | 173 | */ |
124 | size_t data_size; | 174 | size_t data_size; |
125 | 175 | ||
126 | /** | ||
127 | * Requested Subsystem | ||
128 | */ | ||
129 | char *subsystem; | ||
130 | 176 | ||
131 | /** | 177 | /** |
132 | * Ego list | 178 | * Ego list |
@@ -213,8 +259,6 @@ cleanup_handle (void *cls) | |||
213 | handle->timeout_task = NULL; | 259 | handle->timeout_task = NULL; |
214 | } | 260 | } |
215 | 261 | ||
216 | if (NULL != handle->subsystem) | ||
217 | GNUNET_free(handle->subsystem); | ||
218 | if (NULL != handle->url) | 262 | if (NULL != handle->url) |
219 | GNUNET_free(handle->url); | 263 | GNUNET_free(handle->url); |
220 | if (NULL != handle->emsg) | 264 | if (NULL != handle->emsg) |
@@ -315,7 +359,9 @@ get_egoentry(struct RequestHandle *handle, char* pubkey, char *name) | |||
315 | * @param name the id of the ego | 359 | * @param name the id of the ego |
316 | */ | 360 | */ |
317 | static void | 361 | static void |
318 | ego_get_for_subsystem (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, | 362 | ego_get_for_subsystem (void *cls, |
363 | struct GNUNET_IDENTITY_Ego *ego, | ||
364 | void **ctx, | ||
319 | const char *name) | 365 | const char *name) |
320 | { | 366 | { |
321 | struct RequestHandle *handle = cls; | 367 | struct RequestHandle *handle = cls; |
@@ -337,8 +383,12 @@ ego_get_for_subsystem (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, | |||
337 | 383 | ||
338 | // create json with subsystem identity | 384 | // create json with subsystem identity |
339 | json_root = json_object (); | 385 | json_root = json_object (); |
340 | json_object_set_new (json_root, GNUNET_REST_PARAM_PUBKEY, json_string(public_key_string)); | 386 | json_object_set_new (json_root, |
341 | json_object_set_new (json_root, GNUNET_REST_PARAM_NAME, json_string(name)); | 387 | GNUNET_REST_IDENTITY_PARAM_PUBKEY, |
388 | json_string(public_key_string)); | ||
389 | json_object_set_new (json_root, | ||
390 | GNUNET_REST_IDENTITY_PARAM_NAME, | ||
391 | json_string(name)); | ||
342 | 392 | ||
343 | result_str = json_dumps (json_root, 0); | 393 | result_str = json_dumps (json_root, 0); |
344 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 394 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
@@ -352,116 +402,75 @@ ego_get_for_subsystem (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, | |||
352 | } | 402 | } |
353 | 403 | ||
354 | /** | 404 | /** |
355 | * Handle identity GET request | 405 | * Handle identity GET request for subsystem |
356 | * | 406 | * |
357 | * @param con_handle the connection handle | 407 | * @param con_handle the connection handle |
358 | * @param url the url | 408 | * @param url the url |
359 | * @param cls the RequestHandle | 409 | * @param cls the RequestHandle |
360 | */ | 410 | */ |
361 | void | 411 | void |
362 | ego_get (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | 412 | ego_get_subsystem (struct GNUNET_REST_RequestHandle *con_handle, |
363 | void *cls) | 413 | const char* url, |
414 | void *cls) | ||
364 | { | 415 | { |
365 | struct RequestHandle *handle = cls; | 416 | struct RequestHandle *handle = cls; |
366 | struct EgoEntry *ego_entry; | 417 | char *subsystem; |
367 | struct GNUNET_HashCode key; | ||
368 | struct MHD_Response *resp; | ||
369 | char *keystring; | ||
370 | char *egoname; | ||
371 | json_t *json_root; | ||
372 | json_t *json_ego; | ||
373 | char *result_str; | ||
374 | 418 | ||
375 | //requested default identity of subsystem | 419 | if (strlen (GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM) >= strlen (handle->url)) |
376 | GNUNET_CRYPTO_hash (GNUNET_REST_PARAM_SUBSYSTEM, | ||
377 | strlen (GNUNET_REST_PARAM_SUBSYSTEM), &key); | ||
378 | if ( GNUNET_YES | ||
379 | == GNUNET_CONTAINER_multihashmap_contains ( | ||
380 | handle->rest_handle->url_param_map, &key)) | ||
381 | { | 420 | { |
382 | handle->subsystem = GNUNET_strdup( | 421 | handle->emsg = GNUNET_strdup("Missing subsystem"); |
383 | GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map, | 422 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
384 | &key)); | ||
385 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Looking for %s's ego\n", | ||
386 | handle->subsystem); | ||
387 | |||
388 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, | ||
389 | handle->subsystem, | ||
390 | &ego_get_for_subsystem, | ||
391 | handle); | ||
392 | if (NULL == handle->op) | ||
393 | { | ||
394 | handle->emsg = GNUNET_strdup("No identity found for subsystem"); | ||
395 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
396 | return; | ||
397 | } | ||
398 | return; | 423 | return; |
399 | } | 424 | } |
400 | egoname = NULL; | 425 | subsystem = &handle->url[strlen ( |
401 | keystring = NULL; | 426 | GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM) + 1]; |
402 | 427 | //requested default identity of subsystem | |
403 | //one identity requested with key | 428 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Looking for %s's ego\n", subsystem); |
404 | GNUNET_CRYPTO_hash (GNUNET_REST_PARAM_PUBKEY, | ||
405 | strlen (GNUNET_REST_PARAM_PUBKEY), | ||
406 | &key); | ||
407 | if ( GNUNET_YES | ||
408 | == GNUNET_CONTAINER_multihashmap_contains ( | ||
409 | handle->rest_handle->url_param_map, &key)) | ||
410 | { | ||
411 | keystring = GNUNET_CONTAINER_multihashmap_get ( | ||
412 | handle->rest_handle->url_param_map, &key); | ||
413 | 429 | ||
414 | ego_entry = get_egoentry(handle, keystring, NULL); | 430 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, |
415 | if (NULL == ego_entry) | 431 | subsystem, |
416 | { | 432 | &ego_get_for_subsystem, |
417 | handle->emsg = GNUNET_strdup("No identity found for public key"); | 433 | handle); |
418 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
419 | return; | ||
420 | } | ||
421 | egoname = ego_entry->identifier; | ||
422 | } | ||
423 | 434 | ||
424 | //one identity requested with name | 435 | if (NULL == handle->op) |
425 | if (NULL == egoname) | ||
426 | { | 436 | { |
427 | GNUNET_CRYPTO_hash (GNUNET_REST_PARAM_NAME, | 437 | handle->emsg = GNUNET_strdup("No identity found for subsystem"); |
428 | strlen (GNUNET_REST_PARAM_NAME), | 438 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
429 | &key); | 439 | return; |
430 | if ( GNUNET_YES | ||
431 | == GNUNET_CONTAINER_multihashmap_contains ( | ||
432 | handle->rest_handle->url_param_map, &key)) | ||
433 | { | ||
434 | egoname = GNUNET_CONTAINER_multihashmap_get ( | ||
435 | handle->rest_handle->url_param_map, &key); | ||
436 | if (0 >= strlen(egoname)) | ||
437 | { | ||
438 | handle->emsg = GNUNET_strdup("No identity found for name"); | ||
439 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
440 | return; | ||
441 | } | ||
442 | //LOWERCASE ego names? | ||
443 | GNUNET_STRINGS_utf8_tolower(egoname, egoname); | ||
444 | } | ||
445 | } | 440 | } |
441 | } | ||
442 | |||
443 | |||
444 | /** | ||
445 | * Handle identity GET request - responds with all identities | ||
446 | * | ||
447 | * @param con_handle the connection handle | ||
448 | * @param url the url | ||
449 | * @param cls the RequestHandle | ||
450 | */ | ||
451 | void | ||
452 | ego_get_all (struct GNUNET_REST_RequestHandle *con_handle, | ||
453 | const char* url, | ||
454 | void *cls) | ||
455 | { | ||
456 | struct RequestHandle *handle = cls; | ||
457 | struct EgoEntry *ego_entry; | ||
458 | struct MHD_Response *resp; | ||
459 | json_t *json_root; | ||
460 | json_t *json_ego; | ||
461 | char *result_str; | ||
446 | 462 | ||
447 | json_root = json_array (); | 463 | json_root = json_array (); |
448 | //Return ego/egos | 464 | //Return ego/egos |
449 | for (ego_entry = handle->ego_head; | 465 | for (ego_entry = handle->ego_head; |
450 | NULL != ego_entry; ego_entry = ego_entry->next) | 466 | NULL != ego_entry; ego_entry = ego_entry->next) |
451 | { | 467 | { |
452 | //if only one ego requested | ||
453 | if ((NULL != egoname)){ | ||
454 | if(0 != strcmp (egoname, ego_entry->identifier)){ | ||
455 | continue; | ||
456 | } | ||
457 | } | ||
458 | |||
459 | json_ego = json_object (); | 468 | json_ego = json_object (); |
460 | json_object_set_new (json_ego, | 469 | json_object_set_new (json_ego, |
461 | GNUNET_REST_PARAM_PUBKEY, | 470 | GNUNET_REST_IDENTITY_PARAM_PUBKEY, |
462 | json_string (ego_entry->keystring)); | 471 | json_string (ego_entry->keystring)); |
463 | json_object_set_new (json_ego, | 472 | json_object_set_new (json_ego, |
464 | GNUNET_REST_PARAM_NAME, | 473 | GNUNET_REST_IDENTITY_PARAM_NAME, |
465 | json_string (ego_entry->identifier)); | 474 | json_string (ego_entry->identifier)); |
466 | json_array_append (json_root, json_ego); | 475 | json_array_append (json_root, json_ego); |
467 | json_decref (json_ego); | 476 | json_decref (json_ego); |
@@ -487,6 +496,113 @@ ego_get (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
487 | 496 | ||
488 | 497 | ||
489 | /** | 498 | /** |
499 | * Responds with the ego_entry identity | ||
500 | * | ||
501 | * @param handle the struct RequestHandle | ||
502 | * @param ego_entry the struct EgoEntry for the response | ||
503 | */ | ||
504 | void | ||
505 | ego_get_response (struct RequestHandle *handle, struct EgoEntry *ego_entry) | ||
506 | { | ||
507 | struct MHD_Response *resp; | ||
508 | json_t *json_ego; | ||
509 | char *result_str; | ||
510 | |||
511 | json_ego = json_object (); | ||
512 | json_object_set_new (json_ego, | ||
513 | GNUNET_REST_IDENTITY_PARAM_PUBKEY, | ||
514 | json_string (ego_entry->keystring)); | ||
515 | json_object_set_new (json_ego, | ||
516 | GNUNET_REST_IDENTITY_PARAM_NAME, | ||
517 | json_string (ego_entry->identifier)); | ||
518 | |||
519 | result_str = json_dumps (json_ego, 0); | ||
520 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
521 | resp = GNUNET_REST_create_response (result_str); | ||
522 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
523 | |||
524 | json_decref (json_ego); | ||
525 | GNUNET_free(result_str); | ||
526 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
527 | } | ||
528 | |||
529 | |||
530 | /** | ||
531 | * Handle identity GET request with a public key | ||
532 | * | ||
533 | * @param con_handle the connection handle | ||
534 | * @param url the url | ||
535 | * @param cls the RequestHandle | ||
536 | */ | ||
537 | void | ||
538 | ego_get_pubkey (struct GNUNET_REST_RequestHandle *con_handle, | ||
539 | const char* url, | ||
540 | void *cls) | ||
541 | { | ||
542 | struct RequestHandle *handle = cls; | ||
543 | struct EgoEntry *ego_entry; | ||
544 | char *keystring; | ||
545 | |||
546 | keystring = NULL; | ||
547 | |||
548 | if (strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY) >= strlen (handle->url)) | ||
549 | { | ||
550 | handle->emsg = GNUNET_strdup("Missing public key"); | ||
551 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
552 | return; | ||
553 | } | ||
554 | keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY)+1]; | ||
555 | ego_entry = get_egoentry(handle, keystring, NULL); | ||
556 | |||
557 | if (NULL == ego_entry) | ||
558 | { | ||
559 | handle->emsg = GNUNET_strdup("No identity found for public key"); | ||
560 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
561 | return; | ||
562 | } | ||
563 | |||
564 | ego_get_response(handle, ego_entry); | ||
565 | } | ||
566 | |||
567 | /** | ||
568 | * Handle identity GET request with a name | ||
569 | * | ||
570 | * @param con_handle the connection handle | ||
571 | * @param url the url | ||
572 | * @param cls the RequestHandle | ||
573 | */ | ||
574 | void | ||
575 | ego_get_name (struct GNUNET_REST_RequestHandle *con_handle, | ||
576 | const char* url, | ||
577 | void *cls) | ||
578 | { | ||
579 | struct RequestHandle *handle = cls; | ||
580 | struct EgoEntry *ego_entry; | ||
581 | char *egoname; | ||
582 | |||
583 | egoname = NULL; | ||
584 | |||
585 | if (strlen (GNUNET_REST_API_NS_IDENTITY_NAME) >= strlen (handle->url)) | ||
586 | { | ||
587 | handle->emsg = GNUNET_strdup("Missing name"); | ||
588 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
589 | return; | ||
590 | } | ||
591 | egoname = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_NAME)+1]; | ||
592 | ego_entry = get_egoentry(handle, NULL, egoname); | ||
593 | |||
594 | if (NULL == ego_entry) | ||
595 | { | ||
596 | handle->emsg = GNUNET_strdup("No identity found for name"); | ||
597 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
598 | return; | ||
599 | } | ||
600 | |||
601 | ego_get_response(handle, ego_entry); | ||
602 | } | ||
603 | |||
604 | |||
605 | /** | ||
490 | * Processing finished | 606 | * Processing finished |
491 | * | 607 | * |
492 | * @param cls request handle | 608 | * @param cls request handle |
@@ -505,6 +621,10 @@ do_finished (void *cls, const char *emsg) | |||
505 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 621 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
506 | return; | 622 | return; |
507 | } | 623 | } |
624 | if (0 == handle->response_code) | ||
625 | { | ||
626 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
627 | } | ||
508 | resp = GNUNET_REST_create_response (NULL); | 628 | resp = GNUNET_REST_create_response (NULL); |
509 | handle->proc (handle->proc_cls, resp, handle->response_code); | 629 | handle->proc (handle->proc_cls, resp, handle->response_code); |
510 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 630 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
@@ -512,29 +632,22 @@ do_finished (void *cls, const char *emsg) | |||
512 | 632 | ||
513 | 633 | ||
514 | /** | 634 | /** |
515 | * Handle identity PUT request | 635 | * Processing edit ego with EgoEntry ego_entry |
516 | * | 636 | * |
517 | * @param con_handle the connection handle | 637 | * @param handle the struct RequestHandle |
518 | * @param url the url | 638 | * @param ego_entry the struct EgoEntry we want to edit |
519 | * @param cls the RequestHandle | ||
520 | */ | 639 | */ |
521 | void | 640 | void |
522 | ego_edit (struct GNUNET_REST_RequestHandle *con_handle, | 641 | ego_edit (struct RequestHandle *handle, struct EgoEntry *ego_entry) |
523 | const char* url, | ||
524 | void *cls) | ||
525 | { | 642 | { |
526 | struct RequestHandle *handle = cls; | ||
527 | struct EgoEntry *ego_entry; | ||
528 | struct EgoEntry *ego_entry_tmp; | 643 | struct EgoEntry *ego_entry_tmp; |
529 | struct MHD_Response *resp; | 644 | struct MHD_Response *resp; |
530 | int json_state; | ||
531 | json_t *data_js; | 645 | json_t *data_js; |
532 | json_error_t err; | 646 | json_error_t err; |
533 | char *pubkey; | ||
534 | char *name; | ||
535 | char *newsubsys; | 647 | char *newsubsys; |
536 | char *newname; | 648 | char *newname; |
537 | char term_data[handle->data_size + 1]; | 649 | char term_data[handle->data_size + 1]; |
650 | int json_state; | ||
538 | 651 | ||
539 | //if no data | 652 | //if no data |
540 | if (0 >= handle->data_size) | 653 | if (0 >= handle->data_size) |
@@ -555,20 +668,13 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, | |||
555 | return; | 668 | return; |
556 | } | 669 | } |
557 | 670 | ||
558 | ego_entry = NULL; | ||
559 | pubkey = NULL; | ||
560 | name = NULL; | ||
561 | newname = NULL; | 671 | newname = NULL; |
562 | //NEW NAME | 672 | //NEW NAME |
563 | json_state = 0; | 673 | json_state = 0; |
564 | json_state = json_unpack(data_js, | 674 | json_state = json_unpack(data_js, |
565 | "{s:s,s?:s,s?:s}", | 675 | "{s:s!}", |
566 | GNUNET_REST_PARAM_NEWNAME, | 676 | GNUNET_REST_IDENTITY_PARAM_NEWNAME, |
567 | &newname, | 677 | &newname); |
568 | GNUNET_REST_PARAM_PUBKEY, | ||
569 | &pubkey, | ||
570 | GNUNET_REST_PARAM_NAME, | ||
571 | &name); | ||
572 | //Change name with pubkey or name identifier | 678 | //Change name with pubkey or name identifier |
573 | if (0 == json_state) | 679 | if (0 == json_state) |
574 | { | 680 | { |
@@ -587,37 +693,28 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, | |||
587 | json_decref (data_js); | 693 | json_decref (data_js); |
588 | return; | 694 | return; |
589 | } | 695 | } |
590 | //lower case name | ||
591 | GNUNET_STRINGS_utf8_tolower(newname,newname); | 696 | GNUNET_STRINGS_utf8_tolower(newname,newname); |
592 | 697 | ego_entry_tmp = get_egoentry(handle,NULL,newname); | |
593 | ego_entry = get_egoentry(handle,pubkey,name); | 698 | if (NULL != ego_entry_tmp) |
594 | |||
595 | if (NULL == ego_entry) | ||
596 | { | 699 | { |
700 | //Ego with same name not allowed (even if its the ego we change) | ||
597 | resp = GNUNET_REST_create_response (NULL); | 701 | resp = GNUNET_REST_create_response (NULL); |
598 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); | 702 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
599 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 703 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
600 | json_decref (data_js); | 704 | json_decref (data_js); |
601 | return; | 705 | return; |
602 | } | 706 | } |
603 | 707 | handle->op = GNUNET_IDENTITY_rename (handle->identity_handle, | |
604 | for (ego_entry_tmp = handle->ego_head; | 708 | ego_entry->identifier, |
605 | NULL != ego_entry_tmp; ego_entry_tmp = ego_entry_tmp->next) | 709 | newname, |
710 | &do_finished, | ||
711 | handle); | ||
712 | if (NULL == handle->op) | ||
606 | { | 713 | { |
607 | if (0 == strcasecmp (newname, ego_entry_tmp->identifier)) | 714 | handle->emsg = GNUNET_strdup("Rename was not possible"); |
608 | { | 715 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
609 | //Ego with same name not allowed | 716 | return; |
610 | resp = GNUNET_REST_create_response (NULL); | ||
611 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | ||
612 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
613 | json_decref (data_js); | ||
614 | return; | ||
615 | } | ||
616 | } | 717 | } |
617 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
618 | handle->op = GNUNET_IDENTITY_rename (handle->identity_handle, | ||
619 | ego_entry->identifier, newname, | ||
620 | &do_finished, handle); | ||
621 | json_decref (data_js); | 718 | json_decref (data_js); |
622 | return; | 719 | return; |
623 | } | 720 | } |
@@ -626,17 +723,13 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, | |||
626 | //SUBSYSTEM | 723 | //SUBSYSTEM |
627 | json_state = 0; | 724 | json_state = 0; |
628 | json_state = json_unpack(data_js, | 725 | json_state = json_unpack(data_js, |
629 | "{s:s,s?:s,s?:s}", | 726 | "{s:s!}", |
630 | GNUNET_REST_PARAM_SUBSYSTEM, | 727 | GNUNET_REST_IDENTITY_PARAM_SUBSYSTEM, |
631 | &newsubsys, | 728 | &newsubsys); |
632 | GNUNET_REST_PARAM_PUBKEY, | ||
633 | &pubkey, | ||
634 | GNUNET_REST_PARAM_NAME, | ||
635 | &name); | ||
636 | //Change subsystem with pubkey or name identifier | 729 | //Change subsystem with pubkey or name identifier |
637 | if (0 == json_state) | 730 | if (0 == json_state) |
638 | { | 731 | { |
639 | if (NULL == newsubsys || (NULL == pubkey && NULL == name)) | 732 | if (NULL == newsubsys) |
640 | { | 733 | { |
641 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | 734 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); |
642 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 735 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
@@ -652,22 +745,18 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, | |||
652 | return; | 745 | return; |
653 | } | 746 | } |
654 | 747 | ||
655 | ego_entry = get_egoentry(handle, pubkey, name); | ||
656 | |||
657 | if (NULL == ego_entry) | ||
658 | { | ||
659 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_DATA_INVALID); | ||
660 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
661 | json_decref (data_js); | ||
662 | return; | ||
663 | } | ||
664 | |||
665 | handle->response_code = MHD_HTTP_NO_CONTENT; | 748 | handle->response_code = MHD_HTTP_NO_CONTENT; |
666 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, | 749 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, |
667 | newsubsys, | 750 | newsubsys, |
668 | ego_entry->ego, | 751 | ego_entry->ego, |
669 | &do_finished, | 752 | &do_finished, |
670 | handle); | 753 | handle); |
754 | if (NULL == handle->op) | ||
755 | { | ||
756 | handle->emsg = GNUNET_strdup("Setting subsystem was not possible"); | ||
757 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
758 | return; | ||
759 | } | ||
671 | json_decref (data_js); | 760 | json_decref (data_js); |
672 | return; | 761 | return; |
673 | } | 762 | } |
@@ -678,6 +767,85 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, | |||
678 | return; | 767 | return; |
679 | } | 768 | } |
680 | 769 | ||
770 | |||
771 | /** | ||
772 | * Handle identity PUT request with public key | ||
773 | * | ||
774 | * @param con_handle the connection handle | ||
775 | * @param url the url | ||
776 | * @param cls the RequestHandle | ||
777 | */ | ||
778 | void | ||
779 | ego_edit_pubkey (struct GNUNET_REST_RequestHandle *con_handle, | ||
780 | const char* url, | ||
781 | void *cls) | ||
782 | { | ||
783 | struct RequestHandle *handle = cls; | ||
784 | struct EgoEntry *ego_entry; | ||
785 | char *keystring; | ||
786 | |||
787 | keystring = NULL; | ||
788 | |||
789 | if (strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY) >= strlen (handle->url)) | ||
790 | { | ||
791 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
792 | handle->emsg = GNUNET_strdup("Missing public key"); | ||
793 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
794 | return; | ||
795 | } | ||
796 | keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY)+1]; | ||
797 | ego_entry = get_egoentry(handle, keystring, NULL); | ||
798 | |||
799 | if (NULL == ego_entry) | ||
800 | { | ||
801 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
802 | handle->emsg = GNUNET_strdup("No identity found for public key"); | ||
803 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
804 | return; | ||
805 | } | ||
806 | |||
807 | ego_edit(handle,ego_entry); | ||
808 | } | ||
809 | |||
810 | /** | ||
811 | * Handle identity PUT request with name | ||
812 | * | ||
813 | * @param con_handle the connection handle | ||
814 | * @param url the url | ||
815 | * @param cls the RequestHandle | ||
816 | */ | ||
817 | void | ||
818 | ego_edit_name (struct GNUNET_REST_RequestHandle *con_handle, | ||
819 | const char* url, | ||
820 | void *cls) | ||
821 | { | ||
822 | struct RequestHandle *handle = cls; | ||
823 | struct EgoEntry *ego_entry; | ||
824 | char *name; | ||
825 | |||
826 | name = NULL; | ||
827 | |||
828 | if (strlen (GNUNET_REST_API_NS_IDENTITY_NAME) >= strlen (handle->url)) | ||
829 | { | ||
830 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
831 | handle->emsg = GNUNET_strdup("Missing name"); | ||
832 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
833 | return; | ||
834 | } | ||
835 | name = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_NAME)+1]; | ||
836 | ego_entry = get_egoentry(handle, NULL, name); | ||
837 | |||
838 | if (NULL == ego_entry) | ||
839 | { | ||
840 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
841 | handle->emsg = GNUNET_strdup("No identity found for name"); | ||
842 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
843 | return; | ||
844 | } | ||
845 | |||
846 | ego_edit(handle,ego_entry); | ||
847 | } | ||
848 | |||
681 | /** | 849 | /** |
682 | * Handle identity POST request | 850 | * Handle identity POST request |
683 | * | 851 | * |
@@ -686,7 +854,8 @@ ego_edit (struct GNUNET_REST_RequestHandle *con_handle, | |||
686 | * @param cls the RequestHandle | 854 | * @param cls the RequestHandle |
687 | */ | 855 | */ |
688 | void | 856 | void |
689 | ego_create (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | 857 | ego_create (struct GNUNET_REST_RequestHandle *con_handle, |
858 | const char* url, | ||
690 | void *cls) | 859 | void *cls) |
691 | { | 860 | { |
692 | struct RequestHandle *handle = cls; | 861 | struct RequestHandle *handle = cls; |
@@ -726,7 +895,7 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
726 | json_unpack_state = 0; | 895 | json_unpack_state = 0; |
727 | json_unpack_state = json_unpack(data_js, | 896 | json_unpack_state = json_unpack(data_js, |
728 | "{s:s!}", | 897 | "{s:s!}", |
729 | GNUNET_REST_PARAM_NAME, | 898 | GNUNET_REST_IDENTITY_PARAM_NAME, |
730 | &egoname); | 899 | &egoname); |
731 | if (0 != json_unpack_state) | 900 | if (0 != json_unpack_state) |
732 | { | 901 | { |
@@ -771,93 +940,95 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | |||
771 | } | 940 | } |
772 | 941 | ||
773 | /** | 942 | /** |
774 | * Handle identity DELETE request | 943 | * Handle identity DELETE request with public key |
775 | * | 944 | * |
776 | * @param con_handle the connection handle | 945 | * @param con_handle the connection handle |
777 | * @param url the url | 946 | * @param url the url |
778 | * @param cls the RequestHandle | 947 | * @param cls the RequestHandle |
779 | */ | 948 | */ |
780 | void | 949 | void |
781 | ego_delete (struct GNUNET_REST_RequestHandle *con_handle, const char* url, | 950 | ego_delete_pubkey (struct GNUNET_REST_RequestHandle *con_handle, |
782 | void *cls) | 951 | const char* url, |
952 | void *cls) | ||
783 | { | 953 | { |
784 | struct RequestHandle *handle = cls; | 954 | struct RequestHandle *handle = cls; |
785 | struct EgoEntry *ego_entry; | 955 | struct EgoEntry *ego_entry; |
786 | struct GNUNET_HashCode key; | ||
787 | struct MHD_Response *resp; | 956 | struct MHD_Response *resp; |
788 | const char *keystring; | 957 | char *keystring; |
789 | char *egoname; | ||
790 | int ego_exists = GNUNET_NO; | ||
791 | 958 | ||
792 | keystring = NULL; | 959 | keystring = NULL; |
793 | egoname = NULL; | ||
794 | 960 | ||
795 | //delete with pubkey | 961 | if (strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY) >= strlen (handle->url)) |
796 | GNUNET_CRYPTO_hash (GNUNET_REST_PARAM_PUBKEY, | ||
797 | strlen (GNUNET_REST_PARAM_PUBKEY), &key); | ||
798 | if ( GNUNET_YES | ||
799 | == GNUNET_CONTAINER_multihashmap_contains ( | ||
800 | handle->rest_handle->url_param_map, &key)) | ||
801 | { | 962 | { |
802 | keystring = GNUNET_CONTAINER_multihashmap_get ( | 963 | handle->response_code = MHD_HTTP_NOT_FOUND; |
803 | handle->rest_handle->url_param_map,&key); | 964 | handle->emsg = GNUNET_strdup("Missing public key"); |
965 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
966 | return; | ||
804 | } | 967 | } |
968 | keystring = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_PUBKEY)+1]; | ||
969 | ego_entry = get_egoentry(handle, keystring, NULL); | ||
805 | 970 | ||
806 | GNUNET_CRYPTO_hash (GNUNET_REST_PARAM_NAME, | 971 | if (NULL == ego_entry) |
807 | strlen (GNUNET_REST_PARAM_NAME), &key); | ||
808 | if ( GNUNET_YES | ||
809 | == GNUNET_CONTAINER_multihashmap_contains ( | ||
810 | handle->rest_handle->url_param_map, &key)) | ||
811 | { | 972 | { |
812 | egoname = GNUNET_CONTAINER_multihashmap_get ( | 973 | resp = GNUNET_REST_create_response (NULL); |
813 | handle->rest_handle->url_param_map, &key); | 974 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); |
814 | //LOWERCASE ego names? | 975 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
815 | //GNUNET_STRINGS_utf8_tolower(egoname, egoname); | 976 | return; |
816 | } | 977 | } |
817 | 978 | ||
818 | if (NULL != keystring) | 979 | handle->response_code = MHD_HTTP_NO_CONTENT; |
819 | { | 980 | handle->op = GNUNET_IDENTITY_delete (handle->identity_handle, |
820 | for (ego_entry = handle->ego_head; | 981 | ego_entry->identifier, |
821 | NULL != ego_entry; ego_entry = ego_entry->next) | 982 | &do_finished, |
822 | { | 983 | handle); |
823 | if (0 != strcasecmp (keystring, ego_entry->keystring)) | 984 | } |
824 | continue; | 985 | |
825 | ego_exists = GNUNET_YES; | 986 | |
826 | break; | 987 | /** |
827 | } | 988 | * Handle identity DELETE request with name |
828 | } | 989 | * |
829 | else if (NULL != egoname) | 990 | * @param con_handle the connection handle |
830 | { | 991 | * @param url the url |
831 | for (ego_entry = handle->ego_head; | 992 | * @param cls the RequestHandle |
832 | NULL != ego_entry; ego_entry = ego_entry->next) | 993 | */ |
833 | { | 994 | void |
834 | if (0 != strcmp (egoname, ego_entry->identifier)) | 995 | ego_delete_name (struct GNUNET_REST_RequestHandle *con_handle, |
835 | continue; | 996 | const char* url, |
836 | ego_exists = GNUNET_YES; | 997 | void *cls) |
837 | break; | 998 | { |
838 | } | 999 | struct RequestHandle *handle = cls; |
839 | } | 1000 | struct EgoEntry *ego_entry; |
840 | else | 1001 | struct MHD_Response *resp; |
1002 | char *name; | ||
1003 | |||
1004 | name = NULL; | ||
1005 | |||
1006 | if (strlen (GNUNET_REST_API_NS_IDENTITY_NAME) >= strlen (handle->url)) | ||
841 | { | 1007 | { |
842 | handle->emsg = GNUNET_strdup("Missing parameter pubkey or name"); | 1008 | handle->response_code = MHD_HTTP_NOT_FOUND; |
1009 | handle->emsg = GNUNET_strdup("Missing public key"); | ||
843 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 1010 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
844 | return; | 1011 | return; |
845 | } | 1012 | } |
1013 | name = &handle->url[strlen (GNUNET_REST_API_NS_IDENTITY_NAME)+1]; | ||
1014 | ego_entry = get_egoentry(handle, NULL, name); | ||
846 | 1015 | ||
847 | if (GNUNET_NO == ego_exists) | 1016 | if (NULL == ego_entry) |
848 | { | 1017 | { |
849 | resp = GNUNET_REST_create_response (NULL); | 1018 | resp = GNUNET_REST_create_response (NULL); |
850 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); | 1019 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); |
851 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 1020 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
852 | return; | 1021 | return; |
853 | } | 1022 | } |
1023 | |||
854 | handle->response_code = MHD_HTTP_NO_CONTENT; | 1024 | handle->response_code = MHD_HTTP_NO_CONTENT; |
855 | handle->op = GNUNET_IDENTITY_delete (handle->identity_handle, | 1025 | handle->op = GNUNET_IDENTITY_delete (handle->identity_handle, |
856 | ego_entry->identifier, &do_finished, | 1026 | ego_entry->identifier, |
1027 | &do_finished, | ||
857 | handle); | 1028 | handle); |
858 | |||
859 | } | 1029 | } |
860 | 1030 | ||
1031 | |||
861 | /** | 1032 | /** |
862 | * Respond to OPTIONS request | 1033 | * Respond to OPTIONS request |
863 | * | 1034 | * |
@@ -890,10 +1061,15 @@ init_cont (struct RequestHandle *handle) | |||
890 | { | 1061 | { |
891 | struct GNUNET_REST_RequestHandlerError err; | 1062 | struct GNUNET_REST_RequestHandlerError err; |
892 | static const struct GNUNET_REST_RequestHandler handlers[] = { | 1063 | static const struct GNUNET_REST_RequestHandler handlers[] = { |
893 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY, &ego_get }, | 1064 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_ALL, &ego_get_all }, |
894 | { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY, &ego_edit }, | 1065 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_PUBKEY, &ego_get_pubkey }, |
1066 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_NAME, &ego_get_name }, | ||
1067 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_SUBSYSTEM, &ego_get_subsystem }, | ||
1068 | { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY_PUBKEY, &ego_edit_pubkey }, | ||
1069 | { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_IDENTITY_NAME, &ego_edit_name }, | ||
895 | { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create }, | 1070 | { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY, &ego_create }, |
896 | { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY, &ego_delete }, | 1071 | { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY_PUBKEY, &ego_delete_pubkey }, |
1072 | { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_IDENTITY_NAME, &ego_delete_name }, | ||
897 | { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY, &options_cont }, | 1073 | { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_IDENTITY, &options_cont }, |
898 | GNUNET_REST_HANDLER_END | 1074 | GNUNET_REST_HANDLER_END |
899 | }; | 1075 | }; |