Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / identity-provider / gnunet-service-identity-provider.c
index 7a68fe902ec518ef029a42ae90a72e6605576c7d..17c2883682e0e1627ecba4474eb31dd83add27e0 100644 (file)
@@ -148,11 +148,6 @@ static struct GNUNET_CRYPTO_EcdhePrivateKey ecdhe_privkey;
  */
 static struct GNUNET_STATISTICS_Handle *stats;
 
-/**
- * Notification context, simplifies client broadcasts.
- */
-static struct GNUNET_SERVER_NotificationContext *nc;
-
 /**
  * Our configuration.
  */
@@ -165,7 +160,7 @@ struct ExchangeHandle
   /**
    * Client connection
    */
-  struct GNUNET_SERVER_Client *client;
+  struct GNUNET_SERVICE_Client *client;
 
   /**
    * Ticket
@@ -191,6 +186,11 @@ struct ExchangeHandle
    * Label to return
    */
   char *label;
+
+  /**
+   * request id
+   */
+  uint32_t r_id;
 };
 
 struct IssueHandle
@@ -199,7 +199,7 @@ struct IssueHandle
   /**
    * Client connection
    */
-  struct GNUNET_SERVER_Client *client;
+  struct GNUNET_SERVICE_Client *client;
 
   /**
    * Issuer Key
@@ -260,6 +260,11 @@ struct IssueHandle
    * The label the token is stored under
    */
   char *label;
+
+  /**
+   * request id
+   */
+  uint32_t r_id;
 };
 
 /**
@@ -531,6 +536,41 @@ clear_ego_attrs (void *cls,
 }
 
 
+static void
+token_collect_error_cb (void *cls)
+{
+  struct EgoEntry *ego_entry = cls;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              ">>> Updating Ego failed!\n");
+  //Clear attribute map for ego
+  GNUNET_CONTAINER_multihashmap_iterate (ego_entry->attr_map,
+                                         &clear_ego_attrs,
+                                         ego_entry);
+  GNUNET_CONTAINER_multihashmap_clear (ego_entry->attr_map);
+  update_task = GNUNET_SCHEDULER_add_now (&update_identities,
+                                          ego_entry->next);
+
+}
+
+
+static void
+token_collect_finished_cb (void *cls)
+{
+  struct EgoEntry *ego_entry = cls;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              ">>> Updating Ego finished\n");
+  //Clear attribute map for ego
+  GNUNET_CONTAINER_multihashmap_iterate (ego_entry->attr_map,
+                                         &clear_ego_attrs,
+                                         ego_entry);
+  GNUNET_CONTAINER_multihashmap_clear (ego_entry->attr_map);
+  update_task = GNUNET_SCHEDULER_add_now (&update_identities,
+                                          ego_entry->next);
+}
+
+
 /**
  *
  * Update all ID_TOKEN records for an identity and store them
@@ -552,21 +592,7 @@ token_collect (void *cls,
   const struct GNUNET_GNSRECORD_Data *token_record;
   const struct GNUNET_GNSRECORD_Data *token_metadata_record;
   struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key;
-
-  if (NULL == lbl)
-  {
-    //Done
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                ">>> Updating Ego finished\n");
-    //Clear attribute map for ego
-    GNUNET_CONTAINER_multihashmap_iterate (ego_entry->attr_map,
-                                           &clear_ego_attrs,
-                                           ego_entry);
-    GNUNET_CONTAINER_multihashmap_clear (ego_entry->attr_map);
-    update_task = GNUNET_SCHEDULER_add_now (&update_identities,
-                                            ego_entry->next);
-    return;
-  }
+  struct GNUNET_CRYPTO_EcdhePrivateKey *priv_key;
 
   //There should be only a single record for a token under a label
   if (2 != rd_count)
@@ -597,8 +623,9 @@ token_collect (void *cls,
   }
 
   //Get metadata and decrypt token
-  ecdhe_privkey = *((struct GNUNET_CRYPTO_EcdhePrivateKey *)token_metadata_record->data);
-  aud_key = (struct GNUNET_CRYPTO_EcdsaPublicKey *)&(&ecdhe_privkey)[1];
+  priv_key = (struct GNUNET_CRYPTO_EcdhePrivateKey *)token_metadata_record->data;
+  ecdhe_privkey = *priv_key;
+  aud_key = (struct GNUNET_CRYPTO_EcdsaPublicKey *)&priv_key[1];
   scopes = GNUNET_strdup ((char*) aud_key+sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
 
   token_parse2 (token_record->data,
@@ -610,7 +637,33 @@ token_collect (void *cls,
   rd_exp = token_record->expiration_time;
 
   GNUNET_SCHEDULER_add_now (&handle_token_update,
-                           ego_entry);
+                            ego_entry);
+}
+
+
+static void
+attribute_collect_error_cb (void *cls)
+{
+  struct EgoEntry *ego_entry = cls;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              ">>> Updating Attributes failed!\n");
+  ego_entry->attributes_dirty = GNUNET_NO;
+  update_task = GNUNET_SCHEDULER_add_now (&update_identities,
+                                          ego_entry);
+}
+
+
+static void
+attribute_collect_finished_cb (void *cls)
+{
+  struct EgoEntry *ego_entry = cls;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              ">>> Updating Attributes finished\n");
+  ego_entry->attributes_dirty = GNUNET_NO;
+  update_task = GNUNET_SCHEDULER_add_now (&update_identities,
+                                          ego_entry);
 }
 
 
@@ -639,17 +692,6 @@ attribute_collect (void *cls,
   char *val_str;
   int i;
 
-  if (NULL == lbl)
-  {
-    //Done
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                ">>> Updating Attributes finished\n");
-    ego_entry->attributes_dirty = GNUNET_NO;
-    update_task = GNUNET_SCHEDULER_add_now (&update_identities,
-                                           ego_entry);
-    return;
-  }
-
   if (0 == rd_count)
   {
     GNUNET_NAMESTORE_zone_iterator_next (ns_it);
@@ -663,8 +705,8 @@ attribute_collect (void *cls,
     if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
     {
       val_str = GNUNET_GNSRECORD_value_to_string (rd->record_type,
-                                              rd->data,
-                                              rd->data_size);
+                                                  rd->data,
+                                                  rd->data_size);
       attr = GNUNET_malloc (sizeof (struct TokenAttr));
       attr->name = GNUNET_strdup (lbl);
       val = GNUNET_malloc (sizeof (struct TokenAttrValue));
@@ -672,10 +714,11 @@ attribute_collect (void *cls,
       GNUNET_CONTAINER_DLL_insert (attr->val_head,
                                    attr->val_tail,
                                    val);
-      GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
-                                         &key,
-                                         attr,
-                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+      GNUNET_assert (GNUNET_OK ==
+                     GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
+                                                        &key,
+                                                        attr,
+                                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
     }
 
     GNUNET_NAMESTORE_zone_iterator_next (ns_it);
@@ -699,11 +742,10 @@ attribute_collect (void *cls,
     }
   }
   GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
-                                     &key,
-                                     attr,
-                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+                                                                 &key,
+                                                                 attr,
+                                                                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   GNUNET_NAMESTORE_zone_iterator_next (ns_it);
-  return;
 }
 
 /**
@@ -718,13 +760,14 @@ update_identities(void *cls)
 {
   struct EgoEntry *next_ego = cls;
   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
+
   update_task = NULL;
   if (NULL == next_ego)
   {
     if (min_rel_exp.rel_value_us < MIN_WAIT_TIME.rel_value_us)
       min_rel_exp = MIN_WAIT_TIME;
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                ">>> Finished. Rescheduling in %lu\n",
+                ">>> Finished. Rescheduling in %"SCNu64"\n",
                 min_rel_exp.rel_value_us);
     ns_it = NULL;
     //finished -> reschedule
@@ -740,7 +783,11 @@ update_identities(void *cls)
     //Starting over. We must update the Attributes for they might have changed.
     ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
                                                    priv_key,
+                                                   &attribute_collect_error_cb,
+                                                   next_ego,
                                                    &attribute_collect,
+                                                   next_ego,
+                                                   &attribute_collect_finished_cb,
                                                    next_ego);
 
   }
@@ -750,7 +797,11 @@ update_identities(void *cls)
     next_ego->attributes_dirty = GNUNET_YES;
     ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
                                                    priv_key,
+                                                   &token_collect_error_cb,
+                                                   next_ego,
                                                    &token_collect,
+                                                   next_ego,
+                                                   &token_collect_finished_cb,
                                                    next_ego);
   }
 }
@@ -765,7 +816,7 @@ init_cont ()
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, ">>> Starting Service\n");
   //Initially iterate all itenties and refresh all tokens
   update_task = GNUNET_SCHEDULER_add_now (&update_identities,
-                                         ego_head);
+                                          ego_head);
 }
 
 
@@ -811,11 +862,6 @@ cleanup()
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Cleaning up\n");
-  if (NULL != nc)
-  {
-    GNUNET_SERVER_notification_context_destroy (nc);
-    nc = NULL;
-  }
   if (NULL != stats)
   {
     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
@@ -873,45 +919,46 @@ do_shutdown (void *cls)
 }
 
 
-static struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage*
+static struct GNUNET_MQ_Envelope*
 create_exchange_result_message (const char* token,
                                 const char* label,
-                                uint64_t ticket_nonce)
+                                uint64_t ticket_nonce,
+                                uint64_t id)
 {
-  struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
+  struct GNUNET_MQ_Envelope *env;
+  struct ExchangeResultMessage *erm;
   uint16_t token_len = strlen (token) + 1;
-  erm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage)
-                       + token_len);
-  erm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE_RESULT);
-  erm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage)
-                            + token_len);
+
+  env = GNUNET_MQ_msg_extra (erm,
+                             token_len,
+                             GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE_RESULT);
   erm->ticket_nonce = htonl (ticket_nonce);
+  erm->id = id;
   GNUNET_memcpy (&erm[1], token, token_len);
-  return erm;
+  return env;
 }
 
 
-static struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage*
+static struct GNUNET_MQ_Envelope*
 create_issue_result_message (const char* label,
                              const char* ticket,
-                             const char* token)
+                             const char* token,
+                             uint64_t id)
 {
-  struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
+  struct GNUNET_MQ_Envelope *env;
+  struct IssueResultMessage *irm;
   char *tmp_str;
+  size_t len;
 
-  irm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage)
-                       + strlen (label) + 1
-                       + strlen (ticket) + 1
-                       + strlen (token) + 1);
-  irm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE_RESULT);
-  irm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage)
-                            + strlen (label) + 1
-                            + strlen (ticket) + 1
-                            + strlen (token) + 1);
   GNUNET_asprintf (&tmp_str, "%s,%s,%s", label, ticket, token);
+  len = strlen (tmp_str) + 1;
+  env = GNUNET_MQ_msg_extra (irm,
+                             len,
+                             GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE_RESULT);
+  irm->id = id;
   GNUNET_memcpy (&irm[1], tmp_str, strlen (tmp_str) + 1);
   GNUNET_free (tmp_str);
-  return irm;
+  return env;
 }
 
 static void
@@ -922,7 +969,7 @@ cleanup_issue_handle (struct IssueHandle *handle)
   if (NULL != handle->scopes)
     GNUNET_free (handle->scopes);
   if (NULL != handle->token)
-   token_destroy (handle->token);
+    token_destroy (handle->token);
   if (NULL != handle->ticket)
     ticket_destroy (handle->ticket);
   if (NULL != handle->label)
@@ -936,10 +983,10 @@ store_token_issue_cont (void *cls,
                         const char *emsg)
 {
   struct IssueHandle *handle = cls;
-  struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
+  struct GNUNET_MQ_Envelope *env;
   char *ticket_str;
   char *token_str;
-  
+
   handle->ns_qe = NULL;
   if (GNUNET_SYSERR == success)
   {
@@ -970,32 +1017,22 @@ store_token_issue_cont (void *cls,
     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
-  irm = create_issue_result_message (handle->label,
-                                    ticket_str,
-                                    token_str);
-  GNUNET_SERVER_notification_context_unicast (nc,
-                                              handle->client,
-                                              &irm->header,
-                                              GNUNET_NO);
-  GNUNET_SERVER_client_set_user_context (handle->client, NULL);
+  env = create_issue_result_message (handle->label,
+                                     ticket_str,
+                                     token_str,
+                                     handle->r_id);
+  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(handle->client),
+                  env);
   cleanup_issue_handle (handle);
-  GNUNET_free (irm);
   GNUNET_free (ticket_str);
   GNUNET_free (token_str);
 }
 
 
 /**
- * Build a GNUid token for identity
+ * Build a token and store it
  *
- * FIXME: doxygen is very wrong here!
- *
- * @param handle the handle
- * @param ego_entry the ego to build the token for
- * @param name name of the ego
- * @param token_aud token audience
- * @param token the resulting gnuid token
- * @return identifier string of token (label)
+ * @param cls the IssueHandle
  */
 static void
 sign_and_return_token (void *cls)
@@ -1077,6 +1114,29 @@ sign_and_return_token (void *cls)
   GNUNET_free (token_metadata);
 }
 
+
+static void
+attr_collect_error (void *cls)
+{
+  struct IssueHandle *handle = cls;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding attribute Error!\n");
+  handle->ns_it = NULL;
+  GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle);
+}
+
+
+static void
+attr_collect_finished (void *cls)
+{
+  struct IssueHandle *handle = cls;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute END: \n");
+  handle->ns_it = NULL;
+  GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle);
+}
+
+
 /**
  * Collect attributes for token
  */
@@ -1087,19 +1147,11 @@ attr_collect (void *cls,
               unsigned int rd_count,
               const struct GNUNET_GNSRECORD_Data *rd)
 {
+  struct IssueHandle *handle = cls;
   int i;
   char* data;
-  struct IssueHandle *handle = cls;
   struct GNUNET_HashCode key;
 
-  if (NULL == label)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute END: \n");
-    handle->ns_it = NULL;
-    GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle);
-    return;
-  }
-
   GNUNET_CRYPTO_hash (label,
                       strlen (label),
                       &key);
@@ -1166,7 +1218,7 @@ process_lookup_result (void *cls, uint32_t rd_count,
                        const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct ExchangeHandle *handle = cls;
-  struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
+  struct GNUNET_MQ_Envelope *env;
   char* token_str;
   char* record_str;
 
@@ -1196,23 +1248,38 @@ process_lookup_result (void *cls, uint32_t rd_count,
                                                &handle->aud_privkey,
                                                &token_str));
 
-  erm = create_exchange_result_message (token_str,
+  env = create_exchange_result_message (token_str,
                                         handle->label,
-                                        handle->ticket->payload->nonce);
-  GNUNET_SERVER_notification_context_unicast (nc,
-                                              handle->client,
-                                              &erm->header,
-                                              GNUNET_NO);
-  GNUNET_SERVER_client_set_user_context (handle->client, NULL);
-
+                                        handle->ticket->payload->nonce,
+                                        handle->r_id);
+  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(handle->client),
+                  env);
   cleanup_exchange_handle (handle);
   GNUNET_free (record_str);
   GNUNET_free (token_str);
-  GNUNET_free (erm);
-
 }
 
+/**
+ * Checks a exchange message
+ *
+ * @param cls client sending the message
+ * @param xm message of type `struct ExchangeMessage`
+ * @return #GNUNET_OK if @a xm is well-formed
+ */
+static int
+check_exchange_message (void *cls,
+                        const struct ExchangeMessage *xm)
+{
+  uint16_t size;
 
+  size = ntohs (xm->header.size);
+  if (size <= sizeof (struct ExchangeMessage))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
+}
 
 /**
  *
@@ -1224,36 +1291,26 @@ process_lookup_result (void *cls, uint32_t rd_count,
  */
 static void
 handle_exchange_message (void *cls,
-                         struct GNUNET_SERVER_Client *client,
-                         const struct GNUNET_MessageHeader *message)
+                         const struct ExchangeMessage *xm)
 {
-  const struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage *em;
   struct ExchangeHandle *xchange_handle;
-  uint16_t size;
+  struct GNUNET_SERVICE_Client *client = cls;
   const char *ticket;
   char *lookup_query;
 
-  size = ntohs (message->size);
-  if (size <= sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  em = (const struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage *) message;
-  ticket = (const char *) &em[1];
+  ticket = (const char *) &xm[1];
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received EXCHANGE of `%s' from client\n",
               ticket);
   xchange_handle = GNUNET_malloc (sizeof (struct ExchangeHandle));
-  xchange_handle->aud_privkey = em->aud_privkey;
-
+  xchange_handle->aud_privkey = xm->aud_privkey;
+  xchange_handle->r_id = xm->id;
   if (GNUNET_SYSERR == ticket_parse (ticket,
                                      &xchange_handle->aud_privkey,
                                      &xchange_handle->ticket))
   {
     GNUNET_free (xchange_handle);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    GNUNET_SERVICE_client_drop (client);
     return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for token under %s\n",
@@ -1261,23 +1318,57 @@ handle_exchange_message (void *cls,
   GNUNET_asprintf (&lookup_query,
                    "%s.gnu",
                    xchange_handle->ticket->payload->label);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  GNUNET_SERVER_notification_context_add (nc, client);
-  GNUNET_SERVER_client_set_user_context (client, xchange_handle);
+  GNUNET_SERVICE_client_continue (client);
   xchange_handle->client = client;
-  xchange_handle->lookup_request = GNUNET_GNS_lookup (gns_handle,
-                                                      lookup_query,
-                                                      &xchange_handle->ticket->payload->identity_key,
-                                                      GNUNET_GNSRECORD_TYPE_ID_TOKEN,
-                                                      GNUNET_GNS_LO_LOCAL_MASTER,
-                                                      NULL,
-                                                      &process_lookup_result,
-                                                      xchange_handle);
+  xchange_handle->lookup_request
+    = GNUNET_GNS_lookup (gns_handle,
+                         lookup_query,
+                         &xchange_handle->ticket->payload->identity_key,
+                         GNUNET_GNSRECORD_TYPE_ID_TOKEN,
+                         GNUNET_GNS_LO_LOCAL_MASTER,
+                         &process_lookup_result,
+                         xchange_handle);
   GNUNET_free (lookup_query);
 
 }
 
 
+static void
+find_existing_token_error (void *cls)
+{
+  struct IssueHandle *handle = cls;
+  cleanup_issue_handle (handle);
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error looking for existing token\n");
+  GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
+}
+
+
+static void
+find_existing_token_finished (void *cls)
+{
+  struct IssueHandle *handle = cls;
+  uint64_t rnd_key;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              ">>> No existing token found\n");
+  rnd_key =
+    GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
+                              UINT64_MAX);
+  GNUNET_STRINGS_base64_encode ((char*)&rnd_key,
+                                sizeof (uint64_t),
+                                &handle->label);
+  handle->ns_it = NULL;
+  handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
+                                                         &handle->iss_key,
+                                                         &attr_collect_error,
+                                                         handle,
+                                                         &attr_collect,
+                                                         handle,
+                                                         &attr_collect_finished,
+                                                         handle);
+}
+
+
 /**
  *
  * Look for existing token
@@ -1301,30 +1392,9 @@ find_existing_token (void *cls,
   struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key;
   struct GNUNET_HashCode key;
   int scope_count_token;
-  uint64_t rnd_key;
   char *scope;
   char *tmp_scopes;
 
-  if (NULL == lbl)
-  {
-    //Done
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                ">>> No existing token found\n");
-    //Label
-    rnd_key =
-      GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
-                                UINT64_MAX);
-    GNUNET_STRINGS_base64_encode ((char*)&rnd_key,
-                                  sizeof (uint64_t),
-                                  &handle->label);
-    handle->ns_it = NULL;
-    handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
-                                                           &handle->iss_key,
-                                                           &attr_collect,
-                                                           handle);
-    return;
-  }
-
   //There should be only a single record for a token under a label
   if (2 != rd_count)
   {
@@ -1335,7 +1405,9 @@ find_existing_token (void *cls,
   if (rd[0].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
   {
     token_metadata_record = &rd[0];
-  } else {
+  }
+  else
+  {
     token_metadata_record = &rd[1];
   }
   if (token_metadata_record->record_type != GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
@@ -1399,7 +1471,11 @@ find_existing_token (void *cls,
     handle->ns_it = NULL;
     handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
                                                            &handle->iss_key,
+                                                           &attr_collect_error,
+                                                           handle,
                                                            &attr_collect,
+                                                           handle,
+                                                           &attr_collect_finished,
                                                            handle);
 
     return;
@@ -1410,6 +1486,35 @@ find_existing_token (void *cls,
   GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
 }
 
+/**
+ * Checks an issue message
+ *
+ * @param cls client sending the message
+ * @param im message of type `struct IssueMessage`
+ * @return #GNUNET_OK if @a im is well-formed
+ */
+static int
+check_issue_message(void *cls,
+                    const struct IssueMessage *im)
+{
+  uint16_t size;
+
+  size = ntohs (im->header.size);
+  if (size <= sizeof (struct IssueMessage))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  scopes = (char *) &im[1];
+  if ('\0' != scopes[size - sizeof (struct IssueMessage) - 1])
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Malformed scopes received!\n");
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
+}
 
 /**
  *
@@ -1421,35 +1526,22 @@ find_existing_token (void *cls,
  */
 static void
 handle_issue_message (void *cls,
-                      struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
+                      const struct IssueMessage *im)
 {
-  const struct GNUNET_IDENTITY_PROVIDER_IssueMessage *im;
   const char *scopes;
-
-  uint16_t size;
   char *scopes_tmp;
   char *scope;
   struct GNUNET_HashCode key;
   struct IssueHandle *issue_handle;
+  struct GNUNET_SERVICE_Client *client = cls;
 
-  size = ntohs (message->size);
-  if (size <= sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueMessage))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  im = (const struct GNUNET_IDENTITY_PROVIDER_IssueMessage *) message;
   scopes = (const char *) &im[1];
   issue_handle = GNUNET_malloc (sizeof (struct IssueHandle));
   issue_handle->attr_map = GNUNET_CONTAINER_multihashmap_create (5,
                                                                  GNUNET_NO);
-  /* FIXME: check that scopes is 0-termianted, Out-of-bounds access
-     possible here!!! */
   scopes_tmp = GNUNET_strdup (scopes);
-  scope = strtok(scopes_tmp, ",");
-  for (; NULL != scope; scope = strtok (NULL, ","))
+
+  for (scope = strtok (scopes_tmp, ","); NULL != scope; scope = strtok (NULL, ","))
   {
     GNUNET_CRYPTO_hash (scope,
                         strlen (scope),
@@ -1460,16 +1552,14 @@ handle_issue_message (void *cls,
                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
   }
   GNUNET_free (scopes_tmp);
-
+  issue_handle->r_id = im->id;
   issue_handle->aud_key = im->aud_key;
   issue_handle->iss_key = im->iss_key;
   GNUNET_CRYPTO_ecdsa_key_get_public (&im->iss_key,
                                       &issue_handle->iss_pkey);
   issue_handle->expiration = GNUNET_TIME_absolute_ntoh (im->expiration);
   issue_handle->nonce = ntohl (im->nonce);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  GNUNET_SERVER_notification_context_add (nc, client);
-  GNUNET_SERVER_client_set_user_context (client, issue_handle);
+  GNUNET_SERVICE_client_continue (client);
   issue_handle->client = client;
   issue_handle->scopes = GNUNET_strdup (scopes);
   issue_handle->token = token_create (&issue_handle->iss_pkey,
@@ -1477,10 +1567,15 @@ handle_issue_message (void *cls,
 
   issue_handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
                                                                &im->iss_key,
+                                                               &find_existing_token_error,
+                                                               issue_handle,
                                                                &find_existing_token,
+                                                               issue_handle,
+                                                               &find_existing_token_finished,
                                                                issue_handle);
 }
 
+
 /**
  * Main function that will be run
  *
@@ -1491,22 +1586,12 @@ handle_issue_message (void *cls,
  */
 static void
 run (void *cls,
-     struct GNUNET_SERVER_Handle *server,
-     const struct GNUNET_CONFIGURATION_Handle *c)
+     const struct GNUNET_CONFIGURATION_Handle *c,
+     struct GNUNET_SERVICE_Handle *server)
 {
-  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
-    {&handle_issue_message, NULL,
-      GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE, 0},
-    {&handle_exchange_message, NULL,
-      GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE, 0},
-    {NULL, NULL, 0, 0}
-  };
-
   cfg = c;
 
   stats = GNUNET_STATISTICS_create ("identity-provider", cfg);
-  GNUNET_SERVER_add_handlers (server, handlers);
-  nc = GNUNET_SERVER_notification_context_create (server, 1);
 
   //Connect to identity and namestore services
   ns_handle = GNUNET_NAMESTORE_connect (cfg);
@@ -1542,21 +1627,62 @@ run (void *cls,
   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
 }
 
+/**
+ * Called whenever a client is disconnected.
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param app_ctx @a client
+ */
+static void
+client_disconnect_cb (void *cls,
+                      struct GNUNET_SERVICE_Client *client,
+                      void *app_ctx)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Client %p disconnected\n",
+              client);
+}
+
 
 /**
- * The main function
+ * Add a client to our list of active clients.
  *
- * @param argc number of arguments from the cli
- * @param argv command line arguments
- * @return 0 ok, 1 on error
+ * @param cls NULL
+ * @param client client to add
+ * @param mq message queue for @a client
+ * @return internal namestore client structure for this client
  */
-int
-main (int argc, char *const *argv)
+static void *
+client_connect_cb (void *cls,
+                   struct GNUNET_SERVICE_Client *client,
+                   struct GNUNET_MQ_Handle *mq)
 {
-  return  (GNUNET_OK ==
-           GNUNET_SERVICE_run (argc, argv, "identity-provider",
-                               GNUNET_SERVICE_OPTION_NONE,
-                               &run, NULL)) ? 0 : 1;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Client %p connected\n",
+              client);
+  return client;
 }
 
+
+
+/**
+ * Define "main" method using service macro.
+ */
+GNUNET_SERVICE_MAIN
+("identity-provider",
+ GNUNET_SERVICE_OPTION_NONE,
+ &run,
+ &client_connect_cb,
+ &client_disconnect_cb,
+ NULL,
+ GNUNET_MQ_hd_var_size (issue_message,
+                        GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE,
+                        struct IssueMessage,
+                        NULL),
+ GNUNET_MQ_hd_var_size (exchange_message,
+                        GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE,
+                        struct ExchangeMessage,
+                        NULL),
+ GNUNET_MQ_handler_end());
 /* end of gnunet-service-identity-provider.c */