Direct Namestore Deletion Implemented
authorMarkus Voggenreiter <Markus.Voggenreiter@tum.de>
Sat, 12 Oct 2019 20:26:24 +0000 (22:26 +0200)
committerSchanzenbach, Martin <mschanzenbach@posteo.de>
Mon, 13 Jan 2020 12:31:01 +0000 (13:31 +0100)
src/reclaim/plugin_rest_reclaim.c

index b423949060da19815674086032ef176db2b7e396..bb08e6385ffe49303e5e9b43453b0addc8c3dc83 100644 (file)
@@ -297,6 +297,73 @@ struct AttributeStoreHandle
   uint32_t r_id;
 };
 
+/**
+ * Handle for attribute deletion request
+ */
+struct AttributeDeleteHandle
+{
+  /**
+   * DLL
+   */
+  struct AttributeDeleteHandle *next;
+
+  /**
+   * DLL
+   */
+  struct AttributeDeleteHandle *prev;
+
+  /**
+   * Client connection
+   */
+  struct IdpClient *client;
+
+  /**
+   * Identity
+   */
+  struct GNUNET_CRYPTO_EcdsaPrivateKey identity;
+
+
+  /**
+   * QueueEntry
+   */
+  struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
+
+  /**
+   * Iterator
+   */
+  struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
+
+  /**
+   * The attribute to delete
+   */
+  struct GNUNET_RECLAIM_ATTRIBUTE_Claim *claim;
+
+  /**
+   * The attestation to store
+   */
+  struct GNUNET_RECLAIM_ATTESTATION_Claim *attest;
+
+  /**
+   * Tickets to update
+   */
+  struct TicketRecordsEntry *tickets_to_update_head;
+
+  /**
+   * Tickets to update
+   */
+  struct TicketRecordsEntry *tickets_to_update_tail;
+
+  /**
+   * Attribute label
+   */
+  char *label;
+
+  /**
+   * request id
+   */
+  uint32_t r_id;
+};
+
 /**
  * Handle to the service.
  */
@@ -502,6 +569,21 @@ finished_cont (void *cls, int32_t success, const char *emsg)
   GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, 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);
+}
 
 /**
  * Return attributes for identity
@@ -674,10 +756,6 @@ add_attestation_cont (struct GNUNET_REST_RequestHandle *con_handle,
   ash->exp.rel_value_us = exp_interval->rel_value_us;
   ash->attest = attribute;
   ash->client = idp;
-  /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Type:%u\n", type);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ID:%s\n", id_str);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Size:%u\n", data_size);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data:%s\n", data);*/
   buf_size = GNUNET_RECLAIM_ATTESTATION_serialize_get_size (ash->attest);
   buf = GNUNET_malloc (buf_size);
   // Give the ash a new id if unset
@@ -716,15 +794,87 @@ list_attestation_cont (struct GNUNET_REST_RequestHandle *con_handle,
   return;
 }
 
-/*Placeholder*/
+/*WIP*/
 static void
 delete_attestation_cont (struct GNUNET_REST_RequestHandle *con_handle,
                          const char *url,
                          void *cls)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Deleting Attestations not supported\n");
-  GNUNET_SCHEDULER_add_now (&do_error, cls);
-  return;
+  const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
+  struct RequestHandle *handle = cls;
+  struct GNUNET_RECLAIM_ATTESTATION_Claim attr;
+  struct EgoEntry *ego_entry;
+  char *identity_id_str;
+  char *identity;
+  char *id;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting attestation.\n");
+  if (strlen (GNUNET_REST_API_NS_RECLAIM_ATTESTATION_REFERENCE) >= 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_ATTESTATION_REFERENCE) + 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_ATTESTATION_Claim));
+  GNUNET_STRINGS_string_to_data (id, strlen (id), &attr.id, sizeof(uint64_t));
+  attr.name = "";
+
+  struct GNUNET_RECLAIM_Handle *h = handle->idp;
+  struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey = priv_key;
+
+  struct AttributeDeleteHandle *adh;
+  struct IdpClient *idp = handle;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ATTRIBUTE_DELETE message\n");
+  struct GNUNET_NAMESTORE_Handle *nsh;
+  nsh = GNUNET_NAMESTORE_connect (cfg);
+  adh = GNUNET_new (struct AttributeDeleteHandle);
+  adh->attest = &attr;
+  adh->r_id = h->r_id_gen++;
+  adh->identity = *pkey;
+  adh->label = GNUNET_STRINGS_data_to_string_alloc (&adh->attest->id,
+                                                    sizeof(uint64_t));
+  /*GNUNET_SERVICE_client_continue (idp->client);*/
+  adh->client = idp;
+  /*GNUNET_CONTAINER_DLL_insert (idp->delete_op_head, idp->delete_op_tail, adh);*/
+  adh->ns_qe = GNUNET_NAMESTORE_records_store (nsh,
+                                               &adh->identity,
+                                               adh->label,
+                                               0,
+                                               NULL,
+                                               &delete_finished_cb,
+                                               adh);
+
+
+  GNUNET_free (identity_id_str);
 }
 
 /**
@@ -958,23 +1108,6 @@ 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
  *