NAMESTORE/JSON: fix parsing exp and flags
[oweals/gnunet.git] / src / namestore / plugin_rest_namestore.c
index 90928165e4461cc2f51045aa90b243e430c7c06b..cad019292a8fa65a89350cdc0736e769e96e7212 100644 (file)
@@ -14,6 +14,8 @@
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
    */
 /**
  * @author Martin Schanzenbach
 #include "microhttpd.h"
 #include <jansson.h>
 
-
+/**
+ * Namestore Namespace
+ */
 #define GNUNET_REST_API_NS_NAMESTORE "/namestore"
 
-#define GNUNET_REST_SUBSYSTEM_NAMESTORE "namestore"
-
+/**
+ * Error message Unknown Error
+ */
 #define GNUNET_REST_NAMESTORE_ERROR_UNKNOWN "Unknown Error"
 
-#define GNUNET_REST_NAMESTORE_RD_COUNT 1
+/**
+ * Error message No identity found
+ */
+#define GNUNET_REST_IDENTITY_NOT_FOUND "No identity found"
+
+/**
+ * Error message No default zone specified
+ */
+#define GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE "No default zone specified"
+
+/**
+ * Error message Failed request
+ */
+#define GNUNET_REST_NAMESTORE_FAILED "Namestore action failed"
+
+/**
+ * Error message invalid data
+ */
+#define GNUNET_REST_NAMESTORE_INVALID_DATA "Data invalid"
+
+/**
+ * Error message No data
+ */
+#define GNUNET_REST_NAMESTORE_NO_DATA "No data"
+
+/**
+ * State while collecting all egos
+ */
+#define ID_REST_STATE_INIT 0
 
+/**
+ * Done collecting egos
+ */
+#define ID_REST_STATE_POST_INIT 1
 /**
  * The configuration handle
  */
@@ -64,10 +101,20 @@ struct Plugin
  */
 struct EgoEntry
 {
+  /**
+   * DLL
+   */
+  struct EgoEntry *next;
+
+  /**
+   * DLL
+   */
+  struct EgoEntry *prev;
+
   /**
    * Ego Identifier
    */
-  const char *identifier;
+  char *identifier;
 
   /**
    * Public key string
@@ -80,13 +127,15 @@ struct EgoEntry
   struct GNUNET_IDENTITY_Ego *ego;
 };
 
-
+/**
+ * The request handle
+ */
 struct RequestHandle
 {
   /**
    * Records to store
    */
-  char *label_name;
+  char *record_name;
 
   /**
    * Records to store
@@ -103,6 +152,11 @@ struct RequestHandle
    */
   json_t *resp_object;
 
+  /**
+   * The processing state
+   */
+  int state;
+
   /**
    * Handle to NAMESTORE
    */
@@ -116,13 +170,23 @@ struct RequestHandle
   /**
    * Private key for the zone
    */
-  struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
+  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_pkey;
 
   /**
    * IDENTITY Operation
    */
   struct EgoEntry *ego_entry;
 
+  /**
+   * Ego list
+   */
+  struct EgoEntry *ego_head;
+
+  /**
+   * Ego list
+   */
+  struct EgoEntry *ego_tail;
+
   /**
    * IDENTITY Operation
    */
@@ -137,7 +201,7 @@ struct RequestHandle
    * Rest connection
    */
   struct GNUNET_REST_RequestHandle *rest_handle;
-  
+
   /**
    * Desired timeout for the lookup (default is no timeout).
    */
@@ -169,7 +233,7 @@ struct RequestHandle
   char *emsg;
 
   /**
-   * Reponse code
+   * Response code
    */
   int response_code;
 
@@ -183,6 +247,8 @@ static void
 cleanup_handle (void *cls)
 {
   struct RequestHandle *handle = cls;
+  struct EgoEntry *ego_entry;
+  struct EgoEntry *ego_tmp;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Cleaning up\n");
@@ -191,8 +257,8 @@ cleanup_handle (void *cls)
     GNUNET_SCHEDULER_cancel (handle->timeout_task);
     handle->timeout_task = NULL;
   }
-  if (NULL != handle->label_name)
-    GNUNET_free(handle->label_name);
+  if (NULL != handle->record_name)
+    GNUNET_free(handle->record_name);
   if (NULL != handle->url)
     GNUNET_free(handle->url);
   if (NULL != handle->emsg)
@@ -216,12 +282,14 @@ cleanup_handle (void *cls)
     GNUNET_NAMESTORE_disconnect(handle->ns_handle);
   }
 
-  if (NULL != handle->ego_entry)
+  for (ego_entry = handle->ego_head;
+  NULL != ego_entry;)
   {
-    if (NULL != handle->ego_entry->keystring)
-      GNUNET_free(handle->ego_entry->keystring);
-
-    GNUNET_free(handle->ego_entry);
+    ego_tmp = ego_entry;
+    ego_entry = ego_entry->next;
+    GNUNET_free(ego_tmp->identifier);
+    GNUNET_free(ego_tmp->keystring);
+    GNUNET_free(ego_tmp);
   }
 
   if(NULL != handle->resp_object)
@@ -261,18 +329,57 @@ do_error (void *cls)
   GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
 }
 
+
+/**
+ * Get EgoEntry from list with either a public key or a name
+ * If public key and name are not NULL, it returns the public key result first
+ *
+ * @param handle the RequestHandle
+ * @param pubkey the public key of an identity (only one can be NULL)
+ * @param name the name of an identity (only one can be NULL)
+ * @return EgoEntry or NULL if not found
+ */
+struct EgoEntry*
+get_egoentry_namestore(struct RequestHandle *handle, char *name)
+{
+  struct EgoEntry *ego_entry;
+  if (NULL != name)
+  {
+    for (ego_entry = handle->ego_head;
+       NULL != ego_entry;
+       ego_entry = ego_entry->next)
+    {
+      if (0 != strcasecmp (name, ego_entry->identifier))
+       continue;
+      return ego_entry;
+    }
+  }
+  return NULL;
+}
+
+
 /**
  * Does internal server error when iteration failed.
+ *
+ * @param cls the `struct RequestHandle`
  */
 static void
 namestore_iteration_error (void *cls)
 {
   struct RequestHandle *handle = cls;
-  struct MHD_Response *resp = GNUNET_REST_create_response (NULL);
-  handle->proc (handle->proc_cls, resp, MHD_HTTP_INTERNAL_SERVER_ERROR);
-  GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
+  handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+  GNUNET_SCHEDULER_add_now (&do_error, handle);
+  return;
 }
 
+
+/**
+ * Create finished callback
+ *
+ * @param cls the `struct RequestHandle`
+ * @param success the success indicating integer, GNUNET_OK on success
+ * @param emsg the error message (can be NULL)
+ */
 static void
 create_finished (void *cls, int32_t success, const char *emsg)
 {
@@ -282,6 +389,12 @@ create_finished (void *cls, int32_t success, const char *emsg)
   handle->add_qe = NULL;
   if (GNUNET_YES != success)
   {
+    if (NULL != emsg)
+    {
+      handle->emsg = GNUNET_strdup(emsg);
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
+      return;
+    }
     handle->emsg = GNUNET_strdup("Error storing records");
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
@@ -291,6 +404,14 @@ create_finished (void *cls, int32_t success, const char *emsg)
   GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
 }
 
+
+/**
+ * Delete finished callback
+ *
+ * @param cls the `struct RequestHandle`
+ * @param success the success indicating integer, GNUNET_OK on success
+ * @param emsg the error message (can be NULL)
+ */
 static void
 del_finished (void *cls, int32_t success, const char *emsg)
 {
@@ -299,12 +420,19 @@ del_finished (void *cls, int32_t success, const char *emsg)
   handle->add_qe = NULL;
   if (GNUNET_NO == success)
   {
-    handle->emsg = GNUNET_strdup("Deleting record failed. Record does not exist");
+    handle->response_code = MHD_HTTP_NOT_FOUND;
+    handle->emsg = GNUNET_strdup("No record found");
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
   if (GNUNET_SYSERR == success)
   {
+    if (NULL != emsg)
+    {
+      handle->emsg = GNUNET_strdup(emsg);
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
+      return;
+    }
     handle->emsg = GNUNET_strdup("Deleting record failed");
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
@@ -314,6 +442,8 @@ del_finished (void *cls, int32_t success, const char *emsg)
                 MHD_HTTP_NO_CONTENT);
   GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
 }
+
+
 /**
  * Iteration over all results finished, build final
  * response.
@@ -330,10 +460,7 @@ namestore_list_finished (void *cls)
   handle->list_it = NULL;
 
   if (NULL == handle->resp_object)
-  {
-    GNUNET_SCHEDULER_add_now (&do_error, handle);
-    return;
-  }
+    handle->resp_object = json_array();
 
   result_str = json_dumps (handle->resp_object, 0);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
@@ -344,7 +471,6 @@ namestore_list_finished (void *cls)
 }
 
 
-
 /**
  * Create a response with requested records
  *
@@ -363,24 +489,13 @@ namestore_list_iteration (void *cls,
   if (NULL == handle->resp_object)
     handle->resp_object = json_array();
 
-  /*if ( (NULL != handle->ego_entry->identifier) &&
-       (0 != strcmp (handle->ego_entry->identifier,
-                    rname)) )
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "%s does not match %s\n", rname,
-              handle->ego_entry->identifier);
-    GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1);
-    return;
-  }*/
-
   for (unsigned int i = 0; i < rd_len; i++)
   {
     if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
          (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)) )
       continue;
 
-    record_obj = GNUNET_JSON_from_gns_record(rname,rd);
+    record_obj = GNUNET_JSON_from_gns_record (rname, &rd[i]);
 
     if(NULL == record_obj)
       continue;
@@ -392,6 +507,48 @@ namestore_list_iteration (void *cls,
   GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1);
 }
 
+/**
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param identifier identifier assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
+static void
+default_ego_get (void *cls,
+                struct GNUNET_IDENTITY_Ego *ego,
+                void **ctx,
+                const char *identifier)
+{
+  struct RequestHandle *handle = cls;
+  handle->op = NULL;
+
+  if (ego == NULL)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
+
+  handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
+                                                           handle->zone_pkey,
+                                                           &namestore_iteration_error,
+                                                           handle,
+                                                           &namestore_list_iteration,
+                                                           handle,
+                                                           &namestore_list_finished,
+                                                           handle);
+  if (NULL == handle->list_it)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+}
+
 
 /**
  * Handle namestore GET request
@@ -406,67 +563,98 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle,
                  void *cls)
 {
   struct RequestHandle *handle = cls;
-  if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url))
+  struct EgoEntry *ego_entry;
+  char *egoname;
+
+  egoname = NULL;
+  ego_entry = NULL;
+
+  //set zone to name if given
+  if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url))
   {
-    handle->emsg = GNUNET_strdup("Wrong URL");
-    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE)+1];
+    ego_entry = get_egoentry_namestore(handle, egoname);
+
+    if (NULL == ego_entry)
+    {
+      handle->response_code = MHD_HTTP_NOT_FOUND;
+      handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND);
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
+      return;
+    }
+  }
+  if ( NULL != ego_entry )
+  {
+    handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
+  }
+
+  if (NULL == handle->zone_pkey)
+  {
+    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
+                                     "namestore",
+                                     &default_ego_get,
+                                     handle);
     return;
   }
   handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
-                                                           &handle->zone_pkey,
+                                                           handle->zone_pkey,
                                                            &namestore_iteration_error,
                                                            handle,
                                                            &namestore_list_iteration,
                                                            handle,
                                                            &namestore_list_finished,
                                                            handle);
+  if (NULL == handle->list_it)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
 }
 
 
 /**
- * We're storing a new record; this requires
- * that no record already exists
- *
- * @param cls closure, unused
- * @param zone_key private key of the zone
- * @param rec_name name that is being mapped (at most 255 characters long)
- * @param rd_count number of entries in @a rd array
- * @param rd array of records with data to store
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param identifier identifier assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
  */
 static void
-create_new_record_cont (void *cls,
-                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
-                        const char *rec_name,
-                        unsigned int rd_count,
-                        const struct GNUNET_GNSRECORD_Data *rd)
+default_ego_post (void *cls,
+                 struct GNUNET_IDENTITY_Ego *ego,
+                 void **ctx,
+                 const char *identifier)
 {
   struct RequestHandle *handle = cls;
+  handle->op = NULL;
 
-  handle->add_qe = NULL;
-  if (0 != strcmp (rec_name, handle->label_name))
+  if (ego == NULL)
   {
-    GNUNET_break (0);
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
+  handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
 
-  if (0 != rd_count)
+  handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
+                                                  handle->zone_pkey,
+                                                  handle->record_name,
+                                                  1,
+                                                  handle->rd,
+                                                  &create_finished,
+                                                  handle);
+  if (NULL == handle->add_qe)
   {
-    handle->proc (handle->proc_cls,
-                  GNUNET_REST_create_response (NULL),
-                  MHD_HTTP_CONFLICT);
-    GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
-                                                   &handle->zone_pkey,
-                                                   handle->label_name,
-                                                   GNUNET_REST_NAMESTORE_RD_COUNT,
-                                                   handle->rd,
-                                                   &create_finished,
-                                                   handle);
 }
 
+
 /**
  * Handle namestore POST request
  *
@@ -481,24 +669,21 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
 {
   struct RequestHandle *handle = cls;
   struct GNUNET_GNSRECORD_Data *gns_record;
+  struct EgoEntry *ego_entry;
+  char *egoname;
   json_t *data_js;
   json_t *name_json;
   json_error_t err;
   char term_data[handle->rest_handle->data_size + 1];
+
   struct GNUNET_JSON_Specification gnsspec[] = {
     GNUNET_JSON_spec_gnsrecord_data(&gns_record),
     GNUNET_JSON_spec_end ()
   };
 
-  if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url))
-  {
-    handle->emsg = GNUNET_strdup("Wrong URL");
-    GNUNET_SCHEDULER_add_now (&do_error, handle);
-    return;
-  }
   if (0 >= handle->rest_handle->data_size)
   {
-    handle->emsg = GNUNET_strdup("No data");
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DATA);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
@@ -506,58 +691,128 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
   GNUNET_memcpy(term_data, handle->rest_handle->data,
                handle->rest_handle->data_size);
   data_js = json_loads (term_data, JSON_DECODE_ANY, &err);
-  GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL));
-  name_json = json_object_get(data_js, "label");
+  if (GNUNET_OK != GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL))
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    GNUNET_JSON_parse_free(gnsspec);
+    json_decref (data_js);
+    return;
+  }
+  handle->rd = gns_record;
+
+  name_json = json_object_get(data_js, "record_name");
   if (!json_is_string(name_json))
   {
-    handle->emsg = GNUNET_strdup("Missing name");
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    json_decref (data_js);
+    return;
+  }
+  handle->record_name = GNUNET_strdup(json_string_value(name_json));
+  if(NULL == handle->record_name)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
+    json_decref (data_js);
     return;
   }
-  handle->label_name = GNUNET_strdup(json_string_value(name_json));
-  if(NULL == handle->label_name)
+  if (0 >= strlen(handle->record_name))
   {
-    handle->emsg = GNUNET_strdup("Missing name");
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
+    json_decref (data_js);
     return;
   }
   json_decref (data_js);
-  handle->rd = gns_record;
-  handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
-                                                   &handle->zone_pkey,
-                                                   handle->label_name,
-                                                   &do_error,
-                                                   handle,
-                                                   &create_new_record_cont,
-                                                   handle);
+
+  egoname = NULL;
+  ego_entry = NULL;
+
+  //set zone to name if given
+  if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url))
+  {
+    egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE)+1];
+    ego_entry = get_egoentry_namestore(handle, egoname);
+
+    if (NULL == ego_entry)
+    {
+      handle->response_code = MHD_HTTP_NOT_FOUND;
+      handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND);
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
+      return;
+    }
+  }
+  if (NULL != ego_entry)
+  {
+    handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
+  }
+  if (NULL == handle->zone_pkey)
+  {
+    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
+                                     "namestore",
+                                     &default_ego_post,
+                                     handle);
+    return;
+  }
+  handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
+                                                  handle->zone_pkey,
+                                                  handle->record_name,
+                                                  1,
+                                                  handle->rd,
+                                                  &create_finished,
+                                                  handle);
+  if (NULL == handle->add_qe)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
 }
 
 
+/**
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param identifier identifier assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
 static void
-del_cont (void *cls,
-          const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
-          const char *label,
-          unsigned int rd_count,
-          const struct GNUNET_GNSRECORD_Data *rd)
+default_ego_delete (void *cls,
+                   struct GNUNET_IDENTITY_Ego *ego,
+                   void **ctx,
+                   const char *identifier)
 {
   struct RequestHandle *handle = cls;
+  handle->op = NULL;
 
-  handle->add_qe = NULL;
-  if (0 == rd_count)
+  if (ego == NULL)
   {
-    handle->emsg = GNUNET_strdup("Record not found");
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
+  handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
 
   handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
-                                                   &handle->zone_pkey,
-                                                   handle->label_name,
-                                                   0, NULL,
+                                                   handle->zone_pkey,
+                                                   handle->record_name,
+                                                   0,
+                                                  NULL,
                                                    &del_finished,
                                                    handle);
+  if (NULL == handle->add_qe)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
 }
 
+
 /**
  * Handle namestore DELETE request
  *
@@ -572,27 +827,65 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle,
 {
   struct RequestHandle *handle = cls;
   struct GNUNET_HashCode key;
+  struct EgoEntry *ego_entry;
+  char *egoname;
 
-  GNUNET_CRYPTO_hash ("label", strlen ("label"), &key);
+  egoname = NULL;
+  ego_entry = NULL;
+
+  //set zone to name if given
+  if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url))
+  {
+    egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE)+1];
+    ego_entry = get_egoentry_namestore(handle, egoname);
+
+    if (NULL == ego_entry)
+    {
+      handle->response_code = MHD_HTTP_NOT_FOUND;
+      handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND);
+      GNUNET_SCHEDULER_add_now (&do_error, handle);
+      return;
+    }
+  }
+  if ( NULL != ego_entry )
+  {
+    handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
+  }
+
+  GNUNET_CRYPTO_hash ("record_name", strlen ("record_name"), &key);
   if ( GNUNET_NO
       == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map,
                                                 &key))
   {
-    handle->emsg = GNUNET_strdup("Missing name");
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA);
     GNUNET_SCHEDULER_add_now (&do_error, handle);
     return;
   }
-  handle->label_name = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map,
-                                                         &key);
-  
-  handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
-                                                    &handle->zone_pkey,
-                                                    handle->label_name,
-                                                    &do_error,
-                                                    handle,
-                                                    &del_cont,
-                                                    handle);
+  handle->record_name = GNUNET_strdup(
+      GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key));
+
+  if (NULL == handle->zone_pkey)
+  {
+    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
+                                     "namestore",
+                                     &default_ego_delete,
+                                     handle);
+    return;
+  }
 
+  handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
+                                                   handle->zone_pkey,
+                                                   handle->record_name,
+                                                   0,
+                                                  NULL,
+                                                   &del_finished,
+                                                   handle);
+  if (NULL == handle->add_qe)
+  {
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
 }
 
 
@@ -650,55 +943,65 @@ init_cont (struct RequestHandle *handle)
   }
 }
 
+
 /**
+ * This function is initially called for all egos and then again
+ * whenever a ego's identifier changes or if it is deleted.  At the
+ * end of the initial pass over all egos, the function is once called
+ * with 'NULL' for 'ego'. That does NOT mean that the callback won't
+ * be invoked in the future or that there was an error.
+ *
+ * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
+ * this function is only called ONCE, and 'NULL' being passed in
+ * 'ego' does indicate an error (i.e. name is taken or no default
+ * value is known).  If 'ego' is non-NULL and if '*ctx'
+ * is set in those callbacks, the value WILL be passed to a subsequent
+ * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
+ * that one was not NULL).
+ *
+ * When an identity is renamed, this function is called with the
+ * (known) ego but the NEW identifier.
  *
+ * When an identity is deleted, this function is called with the
+ * (known) ego and "NULL" for the 'identifier'.  In this case,
+ * the 'ego' is henceforth invalid (and the 'ctx' should also be
+ * cleaned up).
+ *
+ * @param cls closure
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param name identifier assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
  */
 static void
-default_ego_cb (void *cls,
-                struct GNUNET_IDENTITY_Ego *ego,
-                void **ctx,
-                const char *name)
+id_connect_cb (void *cls,
+               struct GNUNET_IDENTITY_Ego *ego,
+               void **ctx,
+               const char *name)
 {
   struct RequestHandle *handle = cls;
   struct EgoEntry *ego_entry;
   struct GNUNET_CRYPTO_EcdsaPublicKey pk;
 
-  handle->op = NULL;
-
-  if (NULL == ego)
+  if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
   {
-    handle->emsg = GNUNET_strdup ("No default ego configured in identity service");
-    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    handle->state = ID_REST_STATE_POST_INIT;
+    init_cont(handle);
     return;
   }
-  ego_entry = GNUNET_new(struct EgoEntry);
-  GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
-  ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
-  ego_entry->identifier = name;
-  ego_entry->ego = ego;
-  handle->ego_entry = ego_entry;
-  handle->zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
-  init_cont (handle);
-}
-
-
-/**
- * Connect to identity callback
- */
-static void
-id_connect_cb (void *cls,
-               struct GNUNET_IDENTITY_Ego *ego,
-               void **ctx,
-               const char *name)
-{
-  struct RequestHandle *handle = cls;
-  if (NULL == ego)
+  if (ID_REST_STATE_INIT == handle->state)
   {
-    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
-                                     GNUNET_REST_SUBSYSTEM_NAMESTORE,
-                                     &default_ego_cb,
-                                     handle);
+    ego_entry = GNUNET_new(struct EgoEntry);
+    GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
+    ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
+    ego_entry->ego = ego;
+    GNUNET_asprintf (&ego_entry->identifier, "%s", name);
+    GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head, handle->ego_tail,
+                                    ego_entry);
   }
+
 }
 
 
@@ -719,25 +1022,26 @@ rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle,
                               void *proc_cls)
 {
   struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
-  
+
   handle->response_code = 0;
   handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
   handle->proc_cls = proc_cls;
   handle->proc = proc;
   handle->rest_handle = rest_handle;
-  
+  handle->zone_pkey = NULL;
+
   handle->url = GNUNET_strdup (rest_handle->url);
   if (handle->url[strlen (handle->url)-1] == '/')
     handle->url[strlen (handle->url)-1] = '\0';
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
 
-  handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, handle);
   handle->ns_handle = GNUNET_NAMESTORE_connect (cfg);
+  handle->identity_handle = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, handle);
   handle->timeout_task =
     GNUNET_SCHEDULER_add_delayed (handle->timeout,
                                   &do_error,
                                   handle);
-  
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
 }