From 79f1546aa9ad2eeead24324000130caeb26b0262 Mon Sep 17 00:00:00 2001 From: "Schanzenbach, Martin" Date: Sun, 14 Apr 2019 16:40:11 +0200 Subject: [PATCH] RECLAIM/REST: add attribute delete REST call --- src/reclaim-attribute/reclaim_attribute.c | 199 +++++++++------------- src/reclaim/gnunet-service-reclaim.c | 4 +- src/reclaim/plugin_rest_reclaim.c | 77 +++++++++ 3 files changed, 158 insertions(+), 122 deletions(-) diff --git a/src/reclaim-attribute/reclaim_attribute.c b/src/reclaim-attribute/reclaim_attribute.c index 86f0f8f67..b434c386e 100644 --- a/src/reclaim-attribute/reclaim_attribute.c +++ b/src/reclaim-attribute/reclaim_attribute.c @@ -24,10 +24,11 @@ * @author Martin Schanzenbach */ #include "platform.h" + #include "gnunet_util_lib.h" -#include "reclaim_attribute.h" -#include "gnunet_reclaim_attribute_plugin.h" +#include "gnunet_reclaim_attribute_plugin.h" +#include "reclaim_attribute.h" /** * Handle for a plugin */ @@ -63,15 +64,12 @@ static int initialized; * Add a plugin */ static void -add_plugin (void* cls, - const char *library_name, - void *lib_ret) +add_plugin (void *cls, const char *library_name, void *lib_ret) { struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = lib_ret; struct Plugin *plugin; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Loading attribute plugin `%s'\n", + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading attribute plugin `%s'\n", library_name); plugin = GNUNET_new (struct Plugin); plugin->api = api; @@ -83,7 +81,7 @@ add_plugin (void* cls, * Load plugins */ static void -init() +init () { if (GNUNET_YES == initialized) return; @@ -106,11 +104,10 @@ GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (const char *typename) uint32_t ret; init (); - for (i = 0; i < num_plugins; i++) - { + for (i = 0; i < num_plugins; i++) { plugin = attr_plugins[i]; - if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls, - typename))) + if (UINT32_MAX != + (ret = plugin->api->typename_to_number (plugin->api->cls, typename))) return ret; } return UINT32_MAX; @@ -122,7 +119,7 @@ GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (const char *typename) * @param type number of a type * @return corresponding typestring, NULL on error */ -const char* +const char * GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type) { unsigned int i; @@ -130,11 +127,10 @@ GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type) const char *ret; init (); - for (i = 0; i < num_plugins; i++) - { + for (i = 0; i < num_plugins; i++) { plugin = attr_plugins[i]; - if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls, - type))) + if (NULL != + (ret = plugin->api->number_to_typename (plugin->api->cls, type))) return ret; } return NULL; @@ -151,23 +147,17 @@ GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type) * @return #GNUNET_OK on success */ int -GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type, - const char *s, - void **data, - size_t *data_size) +GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type, const char *s, + void **data, size_t *data_size) { unsigned int i; struct Plugin *plugin; init (); - for (i = 0; i < num_plugins; i++) - { + for (i = 0; i < num_plugins; i++) { plugin = attr_plugins[i]; - if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, - type, - s, - data, - data_size)) + if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, type, s, + data, data_size)) return GNUNET_OK; } return GNUNET_SYSERR; @@ -182,22 +172,18 @@ GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type, * @return NULL on error, otherwise human-readable representation of the claim */ char * -GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, - const void* data, +GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, const void *data, size_t data_size) { unsigned int i; struct Plugin *plugin; char *ret; - init(); - for (i = 0; i < num_plugins; i++) - { + init (); + for (i = 0; i < num_plugins; i++) { plugin = attr_plugins[i]; - if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, - type, - data, - data_size))) + if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, type, + data, data_size))) return ret; } return NULL; @@ -213,10 +199,8 @@ GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, * @return the new attribute */ struct GNUNET_RECLAIM_ATTRIBUTE_Claim * -GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char* attr_name, - uint32_t type, - const void* data, - size_t data_size) +GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name, uint32_t type, + const void *data, size_t data_size) { struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr; char *write_ptr; @@ -225,20 +209,15 @@ GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char* attr_name, GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp); attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim) + - strlen (attr_name_tmp) + 1 + - data_size); + strlen (attr_name_tmp) + 1 + data_size); attr->type = type; attr->data_size = data_size; attr->version = 0; - write_ptr = (char*)&attr[1]; - GNUNET_memcpy (write_ptr, - attr_name_tmp, - strlen (attr_name_tmp) + 1); + write_ptr = (char *)&attr[1]; + GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); attr->name = write_ptr; write_ptr += strlen (attr->name) + 1; - GNUNET_memcpy (write_ptr, - data, - data_size); + GNUNET_memcpy (write_ptr, data, data_size); attr->data = write_ptr; GNUNET_free (attr_name_tmp); return attr; @@ -255,25 +234,21 @@ GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char* attr_name, * @return */ void -GNUNET_RECLAIM_ATTRIBUTE_list_add (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list, - const char* attr_name, - uint32_t type, - const void* data, - size_t data_size) +GNUNET_RECLAIM_ATTRIBUTE_list_add ( + struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list, + const char *attr_name, uint32_t type, const void *data, size_t data_size) { struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); - le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, - type, - data, - data_size); - GNUNET_CONTAINER_DLL_insert (claim_list->list_head, - claim_list->list_tail, + le->claim = + GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size); + GNUNET_CONTAINER_DLL_insert (claim_list->list_head, claim_list->list_tail, le); } size_t -GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) +GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size ( + const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) { struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; size_t len = 0; @@ -283,20 +258,18 @@ GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (const struct GNUNET_RECLAIM_AT } size_t -GNUNET_RECLAIM_ATTRIBUTE_list_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, - char *result) +GNUNET_RECLAIM_ATTRIBUTE_list_serialize ( + const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, char *result) { struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; size_t len; size_t total_len; - char* write_ptr; + char *write_ptr; write_ptr = result; total_len = 0; - for (le = attrs->list_head; NULL != le; le = le->next) - { - len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim, - write_ptr); + for (le = attrs->list_head; NULL != le; le = le->next) { + len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim, write_ptr); total_len += len; write_ptr += len; } @@ -304,55 +277,49 @@ GNUNET_RECLAIM_ATTRIBUTE_list_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_C } struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * -GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char* data, - size_t data_size) +GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char *data, size_t data_size) { struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs; struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; size_t attr_len; - const char* read_ptr; + const char *read_ptr; if (data_size < sizeof (struct Attribute)) return NULL; attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); read_ptr = data; - while (((data + data_size) - read_ptr) >= sizeof (struct Attribute)) - { + while (((data + data_size) - read_ptr) >= sizeof (struct Attribute)) { le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); - le->claim = GNUNET_RECLAIM_ATTRIBUTE_deserialize (read_ptr, - data_size - (read_ptr - data)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Deserialized attribute %s\n", le->claim->name); - GNUNET_CONTAINER_DLL_insert (attrs->list_head, - attrs->list_tail, - le); + le->claim = GNUNET_RECLAIM_ATTRIBUTE_deserialize ( + read_ptr, data_size - (read_ptr - data)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserialized attribute %s\n", + le->claim->name); + GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le); attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim); read_ptr += attr_len; } return attrs; } -struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList* -GNUNET_RECLAIM_ATTRIBUTE_list_dup (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) +struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * +GNUNET_RECLAIM_ATTRIBUTE_list_dup ( + const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) { struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le; struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result; result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); - for (le = attrs->list_head; NULL != le; le = le->next) - { + for (le = attrs->list_head; NULL != le; le = le->next) { result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); - result_le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (le->claim->name, - le->claim->type, - le->claim->data, - le->claim->data_size); + result_le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new ( + le->claim->name, le->claim->type, le->claim->data, + le->claim->data_size); result_le->claim->version = le->claim->version; result_le->claim->id = le->claim->id; - GNUNET_CONTAINER_DLL_insert (result->list_head, - result->list_tail, + GNUNET_CONTAINER_DLL_insert (result->list_head, result->list_tail, result_le); } return result; @@ -360,50 +327,48 @@ GNUNET_RECLAIM_ATTRIBUTE_list_dup (const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimLi void -GNUNET_RECLAIM_ATTRIBUTE_list_destroy (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) +GNUNET_RECLAIM_ATTRIBUTE_list_destroy ( + struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) { struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le; - for (le = attrs->list_head; NULL != le;) - { + for (le = attrs->list_head; NULL != le;) { GNUNET_free (le->claim); tmp_le = le; le = le->next; GNUNET_free (tmp_le); } GNUNET_free (attrs); - } size_t -GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr) +GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size ( + const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr) { - return sizeof (struct Attribute) - + strlen (attr->name) - + attr->data_size; + return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size; } size_t -GNUNET_RECLAIM_ATTRIBUTE_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr, - char *result) +GNUNET_RECLAIM_ATTRIBUTE_serialize ( + const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr, char *result) { size_t data_len_ser; size_t name_len; struct Attribute *attr_ser; - char* write_ptr; + char *write_ptr; - attr_ser = (struct Attribute*)result; + attr_ser = (struct Attribute *)result; attr_ser->attribute_type = htons (attr->type); attr_ser->attribute_version = htonl (attr->version); attr_ser->attribute_id = GNUNET_htonll (attr->id); name_len = strlen (attr->name); attr_ser->name_len = htons (name_len); - write_ptr = (char*)&attr_ser[1]; + write_ptr = (char *)&attr_ser[1]; GNUNET_memcpy (write_ptr, attr->name, name_len); write_ptr += name_len; - //TODO plugin-ize - //data_len_ser = plugin->serialize_attribute_value (attr, + // TODO plugin-ize + // data_len_ser = plugin->serialize_attribute_value (attr, // &attr_ser[1]); data_len_ser = attr->data_size; GNUNET_memcpy (write_ptr, attr->data, attr->data_size); @@ -413,42 +378,36 @@ GNUNET_RECLAIM_ATTRIBUTE_serialize (const struct GNUNET_RECLAIM_ATTRIBUTE_Claim } struct GNUNET_RECLAIM_ATTRIBUTE_Claim * -GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char* data, - size_t data_size) +GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size) { struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr; struct Attribute *attr_ser; size_t data_len; size_t name_len; - char* write_ptr; + char *write_ptr; if (data_size < sizeof (struct Attribute)) return NULL; - attr_ser = (struct Attribute*)data; + attr_ser = (struct Attribute *)data; data_len = ntohs (attr_ser->data_size); name_len = ntohs (attr_ser->name_len); - attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim) - + data_len + name_len + 1); + attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim) + + data_len + name_len + 1); attr->type = ntohs (attr_ser->attribute_type); attr->version = ntohl (attr_ser->attribute_version); attr->id = GNUNET_ntohll (attr_ser->attribute_id); attr->data_size = ntohs (attr_ser->data_size); - write_ptr = (char*)&attr[1]; - GNUNET_memcpy (write_ptr, - &attr_ser[1], - name_len); + write_ptr = (char *)&attr[1]; + GNUNET_memcpy (write_ptr, &attr_ser[1], name_len); write_ptr[name_len] = '\0'; attr->name = write_ptr; write_ptr += name_len + 1; - GNUNET_memcpy (write_ptr, - (char*)&attr_ser[1] + name_len, - attr->data_size); + GNUNET_memcpy (write_ptr, (char *)&attr_ser[1] + name_len, attr->data_size); attr->data = write_ptr; return attr; - } /* end of reclaim_attribute.c */ diff --git a/src/reclaim/gnunet-service-reclaim.c b/src/reclaim/gnunet-service-reclaim.c index 5deb482e9..a49962b4d 100644 --- a/src/reclaim/gnunet-service-reclaim.c +++ b/src/reclaim/gnunet-service-reclaim.c @@ -997,8 +997,8 @@ attr_delete_cont (void *cls, int32_t success, const char *emsg) struct AttributeDeleteHandle *adh = cls; adh->ns_qe = NULL; if (GNUNET_SYSERR == success) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error deleting attribute %s (%s)\n", - adh->claim->name, adh->label); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error deleting attribute %s\n", + adh->label); send_delete_response (adh, GNUNET_SYSERR); cleanup_adh (adh); return; diff --git a/src/reclaim/plugin_rest_reclaim.c b/src/reclaim/plugin_rest_reclaim.c index ae46211d5..74071d97b 100644 --- a/src/reclaim/plugin_rest_reclaim.c +++ b/src/reclaim/plugin_rest_reclaim.c @@ -633,6 +633,81 @@ list_attribute_cont (struct GNUNET_REST_RequestHandle *con_handle, } +static void +delete_finished_cb (void *cls, int32_t success, const char *emsg) +{ + struct RequestHandle *handle = cls; + struct MHD_Response *resp; + + resp = GNUNET_REST_create_response (emsg); + if (GNUNET_OK != success) { + GNUNET_SCHEDULER_add_now (&do_error, handle); + return; + } + handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); + GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); +} + + +/** + * List attributes for identity request + * + * @param con_handle the connection handle + * @param url the url + * @param cls the RequestHandle + */ +static void +delete_attribute_cont (struct GNUNET_REST_RequestHandle *con_handle, + const char *url, void *cls) +{ + const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key; + struct RequestHandle *handle = cls; + struct GNUNET_RECLAIM_ATTRIBUTE_Claim attr; + struct EgoEntry *ego_entry; + char *identity_id_str; + char *identity; + char *id; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting attributes.\n"); + if (strlen (GNUNET_REST_API_NS_RECLAIM_ATTRIBUTES) >= strlen (handle->url)) { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No identity given.\n"); + GNUNET_SCHEDULER_add_now (&do_error, handle); + return; + } + identity_id_str = + strdup (handle->url + strlen (GNUNET_REST_API_NS_RECLAIM_ATTRIBUTES) + 1); + identity = strtok (identity_id_str, "/"); + id = strtok (NULL, "/"); + if ((NULL == identity) || (NULL == id)) { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed request.\n"); + GNUNET_free (identity_id_str); + GNUNET_SCHEDULER_add_now (&do_error, handle); + return; + } + + for (ego_entry = handle->ego_head; NULL != ego_entry; + ego_entry = ego_entry->next) + if (0 == strcmp (identity, ego_entry->identifier)) + break; + handle->resp_object = json_array (); + if (NULL == ego_entry) { + // Done + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ego %s not found.\n", identity); + GNUNET_free (identity_id_str); + GNUNET_SCHEDULER_add_now (&return_response, handle); + return; + } + priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); + handle->idp = GNUNET_RECLAIM_connect (cfg); + memset (&attr, 0, sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim)); + GNUNET_STRINGS_string_to_data (id, strlen (id), &attr.id, sizeof (uint64_t)); + attr.name = ""; + handle->idp_op = GNUNET_RECLAIM_attribute_delete ( + handle->idp, priv_key, &attr, &delete_finished_cb, handle); + GNUNET_free (identity_id_str); +} + + static void revoke_ticket_cont (struct GNUNET_REST_RequestHandle *con_handle, const char *url, void *cls) @@ -815,6 +890,8 @@ init_cont (struct RequestHandle *handle) &list_attribute_cont}, {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_RECLAIM_ATTRIBUTES, &add_attribute_cont}, + {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_RECLAIM_ATTRIBUTES, + &delete_attribute_cont}, {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_IDENTITY_TICKETS, &list_tickets_cont}, {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_IDENTITY_REVOKE, -- 2.25.1