NAMESTORE/JSON: fix parsing exp and flags
[oweals/gnunet.git] / src / namestore / plugin_rest_namestore.c
index afe010b79dadae332fd8e7266ee80881c0b27d5c..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_JSON_NAMESTORE_RECORD_TYPE "record_type"
-#define GNUNET_REST_JSON_NAMESTORE_VALUE "value"
-#define GNUNET_REST_JSON_NAMESTORE_EXPIRATION "expiration"
-#define GNUNET_REST_JSON_NAMESTORE_EXPIRED "expired"
-#define GNUNET_REST_ERROR_UNKNOWN "Unknown Error"
+/**
+ * Error message No identity found
+ */
+#define GNUNET_REST_IDENTITY_NOT_FOUND "No identity found"
 
-#define GNUNET_REST_NAMESTORE_RD_COUNT 1
+/**
+ * Error message No default zone specified
+ */
+#define GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE "No default zone specified"
 
-//TODO define other variables
+/**
+ * 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,17 +96,25 @@ struct Plugin
   const struct GNUNET_CONFIGURATION_Handle *cfg;
 };
 
-//TODO add specific structs
-
 /**
  * The default namestore ego
  */
 struct EgoEntry
 {
+  /**
+   * DLL
+   */
+  struct EgoEntry *next;
+
+  /**
+   * DLL
+   */
+  struct EgoEntry *prev;
+
   /**
    * Ego Identifier
    */
-  const char *identifier;
+  char *identifier;
 
   /**
    * Public key string
@@ -87,15 +127,15 @@ struct EgoEntry
   struct GNUNET_IDENTITY_Ego *ego;
 };
 
-
+/**
+ * The request handle
+ */
 struct RequestHandle
 {
-  //TODO add specific entries
-
   /**
    * Records to store
    */
-  char *label_name;
+  char *record_name;
 
   /**
    * Records to store
@@ -112,6 +152,11 @@ struct RequestHandle
    */
   json_t *resp_object;
 
+  /**
+   * The processing state
+   */
+  int state;
+
   /**
    * Handle to NAMESTORE
    */
@@ -125,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
    */
@@ -146,7 +201,7 @@ struct RequestHandle
    * Rest connection
    */
   struct GNUNET_REST_RequestHandle *rest_handle;
-  
+
   /**
    * Desired timeout for the lookup (default is no timeout).
    */
@@ -178,14 +233,12 @@ struct RequestHandle
   char *emsg;
 
   /**
-   * Reponse code
+   * Response code
    */
   int response_code;
 
 };
 
-
-//TODO add specific cleanup
 /**
  * Cleanup lookup handle
  * @param handle Handle to clean up
@@ -194,8 +247,8 @@ static void
 cleanup_handle (void *cls)
 {
   struct RequestHandle *handle = cls;
-  size_t index;
-  json_t *json_ego;
+  struct EgoEntry *ego_entry;
+  struct EgoEntry *ego_tmp;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Cleaning up\n");
@@ -204,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)
@@ -229,20 +282,18 @@ 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)
   {
-    json_array_foreach(handle->resp_object, index, json_ego )
-    {
-      json_decref (json_ego);
-    }
     json_decref(handle->resp_object);
   }
 
@@ -264,7 +315,7 @@ do_error (void *cls)
   char *response;
 
   if (NULL == handle->emsg)
-    handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_UNKNOWN);
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_ERROR_UNKNOWN);
 
   json_object_set_new(json_error,"error", json_string(handle->emsg));
 
@@ -278,74 +329,57 @@ do_error (void *cls)
   GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
 }
 
+
 /**
- * Does internal server error when iteration failed.
+ * 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
  */
-static void
-namestore_iteration_error (void *cls)
+struct EgoEntry*
+get_egoentry_namestore(struct RequestHandle *handle, char *name)
 {
-  struct RequestHandle *handle = cls;
-  struct MHD_Response *resp = GNUNET_REST_create_response (NULL);
-  handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
-  handle->proc (handle->proc_cls, resp, handle->response_code);
-  GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
+  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;
 }
 
+
 /**
- * Create json representation of a GNSRECORD
+ * Does internal server error when iteration failed.
  *
- * @param rd the GNSRECORD_Data
+ * @param cls the `struct RequestHandle`
  */
-static json_t *
-gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd)
+static void
+namestore_iteration_error (void *cls)
 {
-  const char *typename;
-  char *string_val;
-  const char *exp_str;
-  json_t *record_obj;
-
-  typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type);
-  string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type,
-                                                 rd->data,
-                                                 rd->data_size);
-
-  if (NULL == string_val)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Record of type %d malformed, skipping\n",
-                (int) rd->record_type);
-    return NULL;
-  }
-  record_obj = json_object();
-  json_object_set_new (record_obj,
-                       GNUNET_REST_JSON_NAMESTORE_RECORD_TYPE,
-                       json_string (typename));
-  json_object_set_new (record_obj,
-                       GNUNET_REST_JSON_NAMESTORE_VALUE,
-                       json_string (string_val));
-  //GNUNET_free (string_val);
-
-  if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags)
-  {
-    struct GNUNET_TIME_Relative time_rel;
-    time_rel.rel_value_us = rd->expiration_time;
-    exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
-  }
-  else
-  {
-    struct GNUNET_TIME_Absolute time_abs;
-    time_abs.abs_value_us = rd->expiration_time;
-    exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
-  }
-  json_object_set_new (record_obj,
-                      GNUNET_REST_JSON_NAMESTORE_EXPIRATION,
-                      json_string (exp_str));
-  json_object_set_new (record_obj, "expired",
-                       json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd)));
-  return record_obj;
+  struct RequestHandle *handle = cls;
+  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)
 {
@@ -355,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;
@@ -364,6 +404,46 @@ 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)
+{
+  struct RequestHandle *handle = cls;
+
+  handle->add_qe = NULL;
+  if (GNUNET_NO == success)
+  {
+    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;
+  }
+  handle->proc (handle->proc_cls,
+                GNUNET_REST_create_response (NULL),
+                MHD_HTTP_NO_CONTENT);
+  GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
+}
+
+
 /**
  * Iteration over all results finished, build final
  * response.
@@ -379,13 +459,8 @@ namestore_list_finished (void *cls)
 
   handle->list_it = NULL;
 
-  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "HEY\n");
   if (NULL == handle->resp_object)
-  {
-    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "OH\n");
-    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);
@@ -396,7 +471,6 @@ namestore_list_finished (void *cls)
 }
 
 
-
 /**
  * Create a response with requested records
  *
@@ -415,181 +489,172 @@ namestore_list_iteration (void *cls,
   if (NULL == handle->resp_object)
     handle->resp_object = json_array();
 
-  char *result_str = json_dumps (handle->resp_object, 0);
-  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", result_str);
-  GNUNET_free(result_str);
-  /*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 = gnsrecord_to_json (&rd[i]);
+    record_obj = GNUNET_JSON_from_gns_record (rname, &rd[i]);
 
     if(NULL == record_obj)
       continue;
 
-    json_array_append (handle->resp_object,
-                      record_obj);
+    json_array_append (handle->resp_object, record_obj);
     json_decref (record_obj);
   }
 
   GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1);
 }
 
-
 /**
- * Handle namestore GET request
- *
- * @param con_handle the connection handle
- * @param url the url
- * @param cls the RequestHandle
+ * @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
  */
-void
-namestore_get (struct GNUNET_REST_RequestHandle *con_handle,
-                 const char* url,
-                 void *cls)
+static void
+default_ego_get (void *cls,
+                struct GNUNET_IDENTITY_Ego *ego,
+                void **ctx,
+                const char *identifier)
 {
   struct RequestHandle *handle = cls;
-  if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url))
+  handle->op = NULL;
+
+  if (ego == NULL)
   {
-    handle->emsg = GNUNET_strdup("Wrong URL");
+    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,
+                                                           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;
+  }
 }
-/*
 
-//TODO filter input
-static int
-json_to_gnsrecord (struct RequestHandle *handle)
+
+/**
+ * Handle namestore GET request
+ *
+ * @param con_handle the connection handle
+ * @param url the url
+ * @param cls the RequestHandle
+ */
+void
+namestore_get (struct GNUNET_REST_RequestHandle *con_handle,
+                 const char* url,
+                 void *cls)
 {
-  struct GNUNET_TIME_Relative etime_rel;
-  struct GNUNET_TIME_Absolute etime_abs;
-  void *rdata;
-  size_t rdata_size;
+  struct RequestHandle *handle = cls;
+  struct EgoEntry *ego_entry;
+  char *egoname;
 
-  handle->rd = GNUNET_new_array(GNUNET_REST_NAMESTORE_RD_COUNT,
-                                struct GNUNET_GNSRECORD_Data);
-  memset (handle->rd, 0, sizeof(struct GNUNET_GNSRECORD_Data));
-  handle->rd->record_type = GNUNET_GNSRECORD_typename_to_number (
-      handle->json_data->type);
-  if (UINT32_MAX == (*handle->rd).record_type)
-  {
-    handle->emsg = GNUNET_strdup("Unsupported type");
-    return GNUNET_SYSERR;
-  }
-  if (GNUNET_OK
-      != GNUNET_GNSRECORD_string_to_value ((*handle->rd).record_type,
-                                          handle->json_data->value, &rdata,
-                                          &rdata_size))
-  {
-    handle->emsg = GNUNET_strdup("Value invalid for record type");
-    return GNUNET_SYSERR;
-  }
-  (*handle->rd).data = rdata;
-  (*handle->rd).data_size = rdata_size;
-  //TODO other flags
-  if (0 == handle->json_data->is_public)
-  {
-    handle->rd->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
-  }
-  /**TODO
-   * if (1 == handle->is_shadow)
-   rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
-   if (1 != handle->is_public)
-   rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
-   *
-  if (0 == strcmp (handle->json_data->expiration_time, "never"))
+  egoname = NULL;
+  ego_entry = NULL;
+
+  //set zone to name if given
+  if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url))
   {
-    (*handle->rd).expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
+    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;
+    }
   }
-  else if (GNUNET_OK
-      == GNUNET_STRINGS_fancy_time_to_relative (
-         handle->json_data->expiration_time, &etime_rel))
+  if ( NULL != ego_entry )
   {
-    (*handle->rd).expiration_time = etime_rel.rel_value_us;
-    (*handle->rd).flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
+    handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
   }
-  else if (GNUNET_OK
-      == GNUNET_STRINGS_fancy_time_to_absolute (
-         handle->json_data->expiration_time, &etime_abs))
+
+  if (NULL == handle->zone_pkey)
   {
-    (*handle->rd).expiration_time = etime_abs.abs_value_us;
+    handle->op = GNUNET_IDENTITY_get (handle->identity_handle,
+                                     "namestore",
+                                     &default_ego_get,
+                                     handle);
+    return;
   }
-  else
+  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("Value invalid for record type");
-    return GNUNET_SYSERR;
+    handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
   }
-  return GNUNET_OK;
 }
-*/
+
 
 /**
- * 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);
-    do_error (handle);
+    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
  *
@@ -604,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;
   }
@@ -629,30 +691,125 @@ 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
+default_ego_delete (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->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;
+  }
 }
 
 
@@ -669,12 +826,66 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle,
                  void *cls)
 {
   struct RequestHandle *handle = cls;
-  
-  //TODO add behaviour and response
-  
-  handle->emsg = GNUNET_strdup ("Not implemented yet");
-  GNUNET_SCHEDULER_add_now (&do_error, handle);
-  return;
+  struct GNUNET_HashCode key;
+  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))
+  {
+    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(GNUNET_REST_NAMESTORE_INVALID_DATA);
+    GNUNET_SCHEDULER_add_now (&do_error, handle);
+    return;
+  }
+  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;
+  }
 }
 
 
@@ -713,7 +924,6 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle,
 static void
 init_cont (struct RequestHandle *handle)
 {
-  //TODO specify parameter of init_cont if necessary
   struct GNUNET_REST_RequestHandlerError err;
   static const struct GNUNET_REST_RequestHandler handlers[] = {
     {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_get},
@@ -733,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);
   }
+
 }
 
 
@@ -802,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");
 }