- fix for 2192
[oweals/gnunet.git] / src / namestore / namestore_api.c
index d3d5e60efbeddb710fe7429e541ab6819fdb3d77..e4dae859bfd3b61159eddeda1cd35bfeb4850b47 100644 (file)
 #include "gnunet_crypto_lib.h"
 #include "gnunet_constants.h"
 #include "gnunet_arm_service.h"
+#include "gnunet_signatures.h"
 #include "gnunet_namestore_service.h"
 #include "namestore.h"
 
-#include "platform.h"
-#include <gcrypt.h>
-#include "gnunet_common.h"
-#include "gnunet_crypto_lib.h"
-#include "gnunet_disk_lib.h"
-
 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
 
 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
@@ -321,29 +316,96 @@ handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
               "RECORD_CREATE_RESPONSE");
 
   struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
-  int res = GNUNET_OK;
 
-  if (ntohs (msg->op_result) == GNUNET_OK)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' %i\n",
+              "RECORD_CREATE_RESPONSE", ntohs (msg->op_result));
+  if (ntohs (msg->op_result) == GNUNET_YES)
   {
-    res = GNUNET_OK;
     if (qe->cont != NULL)
     {
-      qe->cont (qe->cont_cls, res, _("Namestore added record successfully"));
+      qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore added record successfully"));
     }
 
   }
   else if (ntohs (msg->op_result) == GNUNET_NO)
   {
-    res = GNUNET_SYSERR;
     if (qe->cont != NULL)
     {
-      qe->cont (qe->cont_cls, res, _("Namestore failed to add record"));
+      qe->cont (qe->cont_cls, GNUNET_NO, _("Namestore record already existed"));
     }
   }
   else
   {
-    GNUNET_break_op (0);
-    return;
+    if (qe->cont != NULL)
+    {
+      qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Namestore failed to add record\n"));
+    }
+  }
+
+  /* Operation done, remove */
+  GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe);
+
+  GNUNET_free (qe);
+}
+
+
+static void
+handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
+                             struct RecordRemoveResponseMessage* msg,
+                             size_t size)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n",
+              "RECORD_REMOVE_RESPONSE");
+
+  struct GNUNET_NAMESTORE_Handle *h = qe->nsh;
+  int res = ntohs (msg->op_result);
+
+  /**
+   *  result:
+   *  0 : successful
+   *  1 : No records for entry
+   *  2 : Could not find record to remove
+   *  3 : Failed to create new signature
+   *  4 : Failed to put new set of records in database
+   */
+  switch (res) {
+    case 0:
+      if (qe->cont != NULL)
+      {
+        qe->cont (qe->cont_cls, GNUNET_YES, _("Namestore removed record successfully"));
+      }
+
+      break;
+    case 1:
+      if (qe->cont != NULL)
+      {
+        qe->cont (qe->cont_cls, GNUNET_NO, _("No records for entry"));
+      }
+
+      break;
+    case 2:
+      if (qe->cont != NULL)
+      {
+        qe->cont (qe->cont_cls, GNUNET_NO, _("Could not find record to remove"));
+      }
+
+      break;
+    case 3:
+      if (qe->cont != NULL)
+      {
+        qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to create new signature"));
+      }
+
+      break;
+    case 4:
+      if (qe->cont != NULL)
+      {
+        qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Failed to put new set of records in database"));
+      }
+      break;
+    default:
+        GNUNET_break_op (0);
+      break;
   }
 
   /* Operation done, remove */
@@ -385,6 +447,14 @@ manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
         }
         handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size);
       break;
+    case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
+        if (size != sizeof (struct RecordRemoveResponseMessage))
+        {
+          GNUNET_break_op (0);
+          break;
+        }
+        handle_record_remove_response (qe, (struct RecordRemoveResponseMessage *) msg, size);
+      break;
     default:
       GNUNET_break_op (0);
       break;
@@ -673,23 +743,21 @@ GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
   return h;
 }
 
+struct DisconnectContext
+{
+  struct GNUNET_NAMESTORE_Handle *h;
+  int drop;
+};
 
-/**
- * Disconnect from the namestore service (and free associated
- * resources).
- *
- * @param h handle to the namestore
- * @param drop set to GNUNET_YES to delete all data in namestore (!)
- */
-void
-GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
+static void
+clean_up_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PendingMessage *p;
   struct GNUNET_NAMESTORE_QueueEntry *q;
   struct GNUNET_NAMESTORE_ZoneIterator *z;
-
+  struct GNUNET_NAMESTORE_Handle *h = cls;
   GNUNET_assert (h != NULL);
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
   while (NULL != (p = h->pending_head))
   {
     GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, p);
@@ -698,6 +766,7 @@ GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
 
   while (NULL != (q = h->op_head))
   {
+    GNUNET_break (0);
     GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, q);
     GNUNET_free (q);
   }
@@ -720,6 +789,71 @@ GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
   }
   GNUNET_free(h);
   h = NULL;
+};
+
+static size_t
+transmit_disconnect_to_namestore (void *cls, size_t size, void *buf)
+{
+  struct DisconnectContext * d_ctx = cls;
+  struct DisconnectMessage d_msg;
+  struct GNUNET_NAMESTORE_Handle *h = d_ctx->h;
+  int res;
+
+  d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_DISCONNECT);
+  d_msg.header.size = htons (sizeof (struct DisconnectMessage));
+  d_msg.drop = htonl (d_ctx->drop);
+
+  h->th = NULL;
+  if ((size == 0) || (buf == NULL) || (size < sizeof (struct DisconnectMessage)))
+  {
+    GNUNET_break (0);
+    res = 0;
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to service \n", "NAMESTORE_DISCONNECT");
+    memcpy (buf, &d_msg, sizeof (struct DisconnectMessage));
+    res = sizeof (struct DisconnectMessage);
+  }
+
+  GNUNET_SCHEDULER_add_now (&clean_up_task, h);
+  GNUNET_free (d_ctx);
+  return res;
+}
+
+/**
+ * Disconnect from the namestore service (and free associated
+ * resources).
+ *
+ * @param h handle to the namestore
+ * @param drop set to GNUNET_YES to delete all data in namestore (!)
+ */
+void
+GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop)
+{
+  if (h->th != NULL)
+  {
+    GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
+    h->th = NULL;
+  }
+  if (h->client != NULL)
+  {
+    struct DisconnectContext *d_ctx = GNUNET_malloc (sizeof (struct DisconnectContext));
+    d_ctx->h = h;
+    d_ctx->drop = drop;
+
+    h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, sizeof (struct DisconnectMessage),
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           GNUNET_NO, &transmit_disconnect_to_namestore,
+                                           d_ctx);
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not send disconnect notification to namestore service, we are not connected!\n");
+    if (GNUNET_YES == drop)
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NAMESTORE will not drop content\n");
+    GNUNET_SCHEDULER_add_now (&clean_up_task, h);
+  }
 }
 
 
@@ -818,7 +952,6 @@ GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h,
   msg->rd_count = htons (rd_count);
 
   memcpy (rd_tmp, rd_ser, rd_ser_len);
-  GNUNET_free (rd_ser);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size);
 
@@ -847,7 +980,36 @@ GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_RsaPublicKeyBinary
                                   const struct GNUNET_NAMESTORE_RecordData *rd,
                                   const struct GNUNET_CRYPTO_RsaSignature *signature)
 {
-  return GNUNET_SYSERR;
+  int res = GNUNET_SYSERR;
+  size_t rd_ser_len = 0;
+  size_t name_len = 0;
+  char * name_tmp;
+  char * rd_tmp;
+  struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
+
+  GNUNET_assert (public_key != NULL);
+  GNUNET_assert (name != NULL);
+  GNUNET_assert (signature != NULL);
+
+  rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
+  char rd_ser[rd_ser_len];
+  GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
+
+  name_len = strlen (name) + 1;
+
+  sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
+  sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
+  sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
+  name_tmp = (char *) &sig_purpose[1];
+  rd_tmp = &name_tmp[name_len];
+  memcpy (name_tmp, name, name_len);
+  memcpy (rd_tmp, rd_ser, rd_ser_len);
+
+  res = GNUNET_CRYPTO_rsa_verify(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, sig_purpose, signature, public_key);
+
+  GNUNET_free (sig_purpose);
+
+  return res;
 }
 
 /**
@@ -934,7 +1096,6 @@ GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
   memcpy (pkey_tmp, pkey_enc, key_len);
   memcpy (name_tmp, name, name_len);
   memcpy (rd_tmp, rd_ser, rd_ser_len);
-  GNUNET_free (rd_ser);
   GNUNET_free (pkey_enc);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size);
@@ -970,11 +1131,13 @@ GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
 {
   struct GNUNET_NAMESTORE_QueueEntry *qe;
   struct PendingMessage *pe;
-  char * rd_tmp;
-  char * name_tmp;
+  char *pkey_tmp;
+  char *rd_tmp;
+  char *name_tmp;
   size_t rd_ser_len = 0;
   size_t msg_size = 0;
   size_t name_len = 0;
+  size_t key_len = 0;
   uint32_t rid = 0;
 
   GNUNET_assert (NULL != h);
@@ -985,14 +1148,21 @@ GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
   qe->cont = cont;
   qe->cont_cls = cont_cls;
   qe->op_id = rid;
+  GNUNET_CONTAINER_DLL_insert_tail(h->op_head, h->op_tail, qe);
 
   /* set msg_size*/
+  struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded * pkey_enc = GNUNET_CRYPTO_rsa_encode_key (pkey);
+  GNUNET_assert (pkey_enc != NULL);
+  key_len = ntohs (pkey_enc->len);
+
   rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd);
   char rd_ser[rd_ser_len];
   GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
 
+  name_len = strlen (name) + 1;
+
   struct RecordRemoveMessage * msg;
-  msg_size = sizeof (struct RecordRemoveMessage) + name_len + rd_ser_len;
+  msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
 
   /* create msg here */
   pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size);
@@ -1000,17 +1170,22 @@ GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
   pe->is_init = GNUNET_NO;
   msg = (struct RecordRemoveMessage *) &pe[1];
 
-  name_tmp = (char *) &msg[1];
+  pkey_tmp = (char *) &msg[1];
+  name_tmp = &pkey_tmp[key_len];
   rd_tmp = &name_tmp[name_len];
 
   msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
   msg->gns_header.header.size = htons (msg_size);
   msg->gns_header.r_id = htonl (rid);
-  //msg->signature = *signature;
   msg->name_len = htons (name_len);
+  msg->rd_len = htons (rd_ser_len);
+  msg->rd_count = htons (1);
+  msg->key_len = htons (key_len);
+  memcpy (pkey_tmp, pkey_enc, key_len);
   memcpy (name_tmp, name, name_len);
   memcpy (rd_tmp, rd_ser, rd_ser_len);
-  GNUNET_free (rd_ser);
+
+  GNUNET_free (pkey_enc);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size);