use namestore API for zone import instead of using plugin directly
[oweals/gnunet.git] / src / identity-provider / gnunet-idp.c
index bc30a1148f3314b75780028326774d6dfd43a21f..4f17919b9597d4406b8535de615bbf044ea942fa 100644 (file)
 #include "gnunet_identity_service.h"
 #include "gnunet_signatures.h"
 
+/**
+ * return value
+ */
+static int ret;
+
 /**
  * List attribute flag
  */
@@ -61,6 +66,16 @@ static char* issue_attrs;
  */
 static char* consume_ticket;
 
+/**
+ * Attribute type
+ */
+static char* type_str;
+
+/**
+ * Ticket to revoke
+ */
+static char* revoke_ticket;
+
 /**
  * Ego name
  */
@@ -109,11 +124,25 @@ static struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
 /**
  * Attribute list
  */
-static struct GNUNET_IDENTITY_PROVIDER_AttributeList *attr_list;
+static struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attr_list;
+
+/**
+ * Attribute expiration interval
+ */
+static struct GNUNET_TIME_Relative exp_interval;
+
+/**
+ * Timeout task
+ */
+static struct GNUNET_SCHEDULER_Task *timeout;
 
 static void
 do_cleanup(void *cls)
 {
+  if (NULL != timeout)
+    GNUNET_SCHEDULER_cancel (timeout);
+  if (NULL != idp_op)
+    GNUNET_IDENTITY_PROVIDER_cancel (idp_op);
   if (NULL != attr_iterator)
     GNUNET_IDENTITY_PROVIDER_get_attributes_stop (attr_iterator);
   if (NULL != idp_handle)
@@ -131,6 +160,7 @@ ticket_issue_cb (void* cls,
                  const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
 {
   char* ticket_str;
+  idp_op = NULL;
   if (NULL != ticket) {
     ticket_str = GNUNET_STRINGS_data_to_string_alloc (ticket,
                                                       sizeof (struct GNUNET_IDENTITY_PROVIDER_Ticket));
@@ -146,13 +176,10 @@ store_attr_cont (void *cls,
                  int32_t success,
                  const char*emsg)
 {
+  idp_op = NULL;
   if (GNUNET_SYSERR == success) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "%s\n", emsg);
-  } else {
-    GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
-                "Successfully added identity attribute %s=%s\n",
-                attr_name, attr_value);
   }
   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
 }
@@ -160,15 +187,25 @@ store_attr_cont (void *cls,
 static void
 process_attrs (void *cls,
          const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
-         const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr)
+         const struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr)
 {
+  char *value_str;
   if (NULL == identity)
   {
+    idp_op = NULL;
     GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
     return;
   }
+  if (NULL == attr)
+  {
+    ret = 1;
+    return;
+  }
+  value_str = GNUNET_IDENTITY_ATTRIBUTE_value_to_string (attr->type,
+                                                     attr->data,
+                                                     attr->data_size);
   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
-              "%s: %s\n", attr->name, (char*)attr->data);
+              "%s: %s\n", attr->name, value_str);
 }
 
 
@@ -181,41 +218,92 @@ iter_error (void *cls)
   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
 }
 
+static void
+timeout_task (void *cls)
+{
+  timeout = NULL;
+  ret = 1;
+  GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+              "Timeout\n");
+  GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
+}
+
+static void
+process_rvk (void *cls, int success, const char* msg)
+{
+  idp_op = NULL;
+  if (GNUNET_OK != success)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                "Revocation failed.\n");
+    ret = 1;
+  }
+  GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
+}
+
 static void
 iter_finished (void *cls)
 {
-  struct GNUNET_IDENTITY_PROVIDER_Attribute *attr;
+  struct GNUNET_IDENTITY_ATTRIBUTE_Claim *claim;
+  char *data;
+  size_t data_size;
+  int type;
 
   attr_iterator = NULL;
-  if (list) {
+  if (list)
+  {
     GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
     return;
   }
 
-  if (issue_attrs) {
-    idp_op = GNUNET_IDENTITY_PROVIDER_idp_ticket_issue (idp_handle,
-                                                        pkey,
-                                                        &rp_key,
-                                                        attr_list,
-                                                        &ticket_issue_cb,
-                                                        NULL);
+  if (issue_attrs)
+  {
+    idp_op = GNUNET_IDENTITY_PROVIDER_ticket_issue (idp_handle,
+                                                    pkey,
+                                                    &rp_key,
+                                                    attr_list,
+                                                    &ticket_issue_cb,
+                                                    NULL);
+    return;
+  }
+  if (consume_ticket)
+  {
+    idp_op = GNUNET_IDENTITY_PROVIDER_ticket_consume (idp_handle,
+                                                      pkey,
+                                                      &ticket,
+                                                      &process_attrs,
+                                                      NULL);
+    timeout = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10),
+                                            &timeout_task,
+                                            NULL);
     return;
   }
-  if (consume_ticket) {
-    idp_op = GNUNET_IDENTITY_PROVIDER_rp_ticket_consume (idp_handle,
-                                                         pkey,
-                                                         &ticket,
-                                                         &process_attrs,
-                                                         NULL);
+  if (revoke_ticket)
+  {
+    idp_op = GNUNET_IDENTITY_PROVIDER_ticket_revoke (idp_handle,
+                                                     pkey,
+                                                     &ticket,
+                                                     &process_rvk,
+                                                     NULL);
     return;
   }
-  attr = GNUNET_IDENTITY_PROVIDER_attribute_new (attr_name,
-                                                 GNUNET_IDENTITY_PROVIDER_AT_STRING,
-                                                 attr_value,
-                                                 strlen (attr_value));
+  if (NULL == type_str)
+    type = GNUNET_IDENTITY_ATTRIBUTE_TYPE_STRING;
+  else
+    type = GNUNET_IDENTITY_ATTRIBUTE_typename_to_number (type_str);
+
+  GNUNET_assert (GNUNET_SYSERR != GNUNET_IDENTITY_ATTRIBUTE_string_to_value (type,
+                                             attr_value,
+                                             (void**)&data,
+                                             &data_size));
+  claim = GNUNET_IDENTITY_ATTRIBUTE_claim_new (attr_name,
+                                               type,
+                                               data,
+                                               data_size);
   idp_op = GNUNET_IDENTITY_PROVIDER_attribute_store (idp_handle,
                                                      pkey,
-                                                     attr,
+                                                     claim,
+                                                     &exp_interval,
                                                      &store_attr_cont,
                                                      NULL);
 
@@ -225,9 +313,9 @@ iter_finished (void *cls)
 static void
 iter_cb (void *cls,
          const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
-         const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr)
+         const struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr)
 {
-  struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *le;
+  struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le;
   char *attrs_tmp;
   char *attr_str;
 
@@ -240,11 +328,11 @@ iter_cb (void *cls,
         attr_str = strtok (NULL, ",");
         continue;
       }
-      le = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry);
-      le->attribute = GNUNET_IDENTITY_PROVIDER_attribute_new (attr->name,
-                                                              attr->attribute_type,
-                                                              attr->data,
-                                                              attr->data_size);
+      le = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry);
+      le->claim = GNUNET_IDENTITY_ATTRIBUTE_claim_new (attr->name,
+                                                       attr->type,
+                                                       attr->data,
+                                                       attr->data_size);
       GNUNET_CONTAINER_DLL_insert (attr_list->list_head,
                                    attr_list->list_tail,
                                    le);
@@ -279,8 +367,14 @@ ego_cb (void *cls,
                                    strlen (consume_ticket),
                                    &ticket,
                                    sizeof (struct GNUNET_IDENTITY_PROVIDER_Ticket));
+  if (NULL != revoke_ticket)
+    GNUNET_STRINGS_string_to_data (revoke_ticket,
+                                   strlen (revoke_ticket),
+                                   &ticket,
+                                   sizeof (struct GNUNET_IDENTITY_PROVIDER_Ticket));
+
 
-  attr_list = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_AttributeList);
+  attr_list = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList);
 
   attr_iterator = GNUNET_IDENTITY_PROVIDER_get_attributes_start (idp_handle,
                                                                  pkey,
@@ -300,7 +394,7 @@ run (void *cls,
      const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
-
+  ret = 0;
   if (NULL == ego_name)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
@@ -321,6 +415,7 @@ run (void *cls,
 int
 main(int argc, char *const argv[])
 {
+  exp_interval = GNUNET_TIME_UNIT_HOURS;
   struct GNUNET_GETOPT_CommandLineOption options[] = {
 
     GNUNET_GETOPT_option_string ('a',
@@ -358,9 +453,28 @@ main(int argc, char *const argv[])
                                  NULL,
                                  gettext_noop ("Consume a ticket"),
                                  &consume_ticket),
+    GNUNET_GETOPT_option_string ('R',
+                                 "revoke",
+                                 NULL,
+                                 gettext_noop ("Revoke a ticket"),
+                                 &revoke_ticket),
+    GNUNET_GETOPT_option_string ('t',
+                                 "type",
+                                 NULL,
+                                 gettext_noop ("Type of attribute"),
+                                 &type_str),
+    GNUNET_GETOPT_option_relative_time ('E',
+                                        "expiration",
+                                        NULL,
+                                        gettext_noop ("Expiration interval of the attribute"),
+                                        &exp_interval),
+
     GNUNET_GETOPT_OPTION_END
   };
-  return GNUNET_PROGRAM_run (argc, argv, "ct",
-                             "ct", options,
-                             &run, NULL);
+  if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "ct",
+                      "ct", options,
+                      &run, NULL))
+    return 1;
+  else
+    return ret;
 }