- revert plugin move. Add new identity-token
[oweals/gnunet.git] / src / identity / gnunet-service-identity.c
index b9384ea6530e7defd410c4a3c35f5f319d7405f0..5d2d22a0ab6ed8e889354818284f569e83a3887d 100644 (file)
@@ -1,6 +1,6 @@
 /*
   This file is part of GNUnet.
-  (C) 2013 Christian Grothoff (and other contributing authors)
+  Copyright (C) 2013 Christian Grothoff (and other contributing authors)
 
   GNUnet is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
   You should have received a copy of the GNU General Public License
   along with GNUnet; see the file COPYING.  If not, write to the
-  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-  Boston, MA 02111-1307, USA.
+  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+  Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -47,18 +47,18 @@ struct Ego
 
   /**
    * We keep egos in a DLL.
-   */ 
+   */
   struct Ego *next;
 
   /**
    * We keep egos in a DLL.
-   */ 
+   */
   struct Ego *prev;
 
   /**
    * Private key of the ego.
    */
-  struct GNUNET_CRYPTO_EccPrivateKey *pk;
+  struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
 
   /**
    * String identifier for the ego.
@@ -160,7 +160,8 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   while (NULL != (e = ego_head))
   {
     GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
-    GNUNET_CRYPTO_ecc_key_free (e->pk);
+    GNUNET_free (e->pk);
+    GNUNET_free (e->identifier);
     GNUNET_free (e);
   }
 }
@@ -189,12 +190,13 @@ send_result_code (struct GNUNET_SERVER_Client *client,
   rcm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
   rcm->header.size = htons (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
   rcm->result_code = htonl (result_code);
-  memcpy (&rcm[1], emsg, elen);
+  if (0 < elen)
+    memcpy (&rcm[1], emsg, elen);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Sending result %d (%s) to client\n",
              (int) result_code,
              emsg);
-  GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_NO);  
+  GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_NO);
   GNUNET_free (rcm);
 }
 
@@ -210,7 +212,7 @@ create_update_message (struct Ego *ego)
 {
   struct GNUNET_IDENTITY_UpdateMessage *um;
   size_t name_len;
+
   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
   um = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + name_len);
   um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
@@ -251,7 +253,7 @@ create_set_default_message (struct Ego *ego,
 
 /**
  * Handler for START message from client, sends information
- * about all identities to the client immediately and 
+ * about all identities to the client immediately and
  * adds the client to the notification context for future
  * updates.
  *
@@ -267,7 +269,7 @@ handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
   struct GNUNET_IDENTITY_UpdateMessage ume;
   struct Ego *ego;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received START message from client\n");
   GNUNET_SERVER_notification_context_add (nc, client);
   for (ego = ego_head; NULL != ego; ego = ego->next)
@@ -281,7 +283,7 @@ handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
   ume.header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage));
   ume.end_of_list = htons (GNUNET_YES);
   ume.name_len = htons (0);
-  GNUNET_SERVER_notification_context_unicast (nc, client, &ume.header, GNUNET_NO);  
+  GNUNET_SERVER_notification_context_unicast (nc, client, &ume.header, GNUNET_NO);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -324,7 +326,7 @@ handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received GET_DEFAULT for service `%s' from client\n",
              name);
   if (GNUNET_OK !=
@@ -334,7 +336,7 @@ handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
                                             &identifier))
   {
     send_result_code (client, 1, gettext_noop ("no default known"));
-    GNUNET_SERVER_receive_done (client, GNUNET_OK);   
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
   for (ego = ego_head; NULL != ego; ego = ego->next)
@@ -344,16 +346,19 @@ handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
     {
       sdm = create_set_default_message (ego,
                                        name);
-      GNUNET_SERVER_notification_context_broadcast (nc, &sdm->header, GNUNET_NO);
+      GNUNET_SERVER_notification_context_unicast (nc, client,
+                                                  &sdm->header, GNUNET_NO);
       GNUNET_free (sdm);
       GNUNET_SERVER_receive_done (client, GNUNET_OK);
+      GNUNET_free (identifier);
       return;
     }
   }
+  GNUNET_free (identifier);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Failed to find ego `%s'\n",
              name);
-  send_result_code (client, 1, 
+  send_result_code (client, 1,
                    gettext_noop ("default configured, but ego unknown (internal error)"));
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -361,16 +366,16 @@ handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
 
 /**
  * Compare the given two private keys for equality.
- * 
+ *
  * @param pk1 one private key
  * @param pk2 another private key
  * @return 0 if the keys are equal
  */
 static int
-key_cmp (const struct GNUNET_CRYPTO_EccPrivateKey *pk1,
-        const struct GNUNET_CRYPTO_EccPrivateKey *pk2)
+key_cmp (const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk1,
+        const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk2)
 {
-  return memcmp (pk1, pk2, sizeof (struct GNUNET_CRYPTO_EccPrivateKey));
+  return memcmp (pk1, pk2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
 }
 
 
@@ -402,7 +407,7 @@ handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
   sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) message;
   name_len = ntohs (sdm->name_len);
   GNUNET_break (0 == ntohs (sdm->reserved));
-  if (name_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) != size) 
+  if (name_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) != size)
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -415,7 +420,7 @@ handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received SET_DEFAULT for service `%s' from client\n",
              str);
   for (ego = ego_head; NULL != ego; ego = ego->next)
@@ -427,7 +432,7 @@ handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
                                             str,
                                             "DEFAULT_IDENTIFIER",
                                             ego->identifier);
-      if (GNUNET_OK != 
+      if (GNUNET_OK !=
          GNUNET_CONFIGURATION_write (subsystem_cfg,
                                      subsystem_cfg_file))
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -437,7 +442,7 @@ handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
       GNUNET_SERVER_receive_done (client, GNUNET_OK);
       return;
     }
-  }  
+  }
   send_result_code (client, 1, _("Unknown ego specified for service (internal error)"));
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -478,7 +483,7 @@ handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
   const char *str;
   char *fn;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received CREATE message from client\n");
   size = ntohs (message->size);
   if (size <= sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
@@ -490,7 +495,7 @@ handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
   crm = (const struct GNUNET_IDENTITY_CreateRequestMessage *) message;
   name_len = ntohs (crm->name_len);
   GNUNET_break (0 == ntohs (crm->reserved));
-  if (name_len + sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) != size) 
+  if (name_len + sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) != size)
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -514,7 +519,7 @@ handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
     }
   }
   ego = GNUNET_new (struct Ego);
-  ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
+  ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
   *ego->pk = crm->private_key;
   ego->identifier = GNUNET_strdup (str);
   GNUNET_CONTAINER_DLL_insert (ego_head,
@@ -523,16 +528,16 @@ handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
   send_result_code (client, 0, NULL);
   fn = get_ego_filename (ego);
   (void) GNUNET_DISK_directory_create_for_file (fn);
-  if (sizeof (struct GNUNET_CRYPTO_EccPrivateKey) !=
+  if (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey) !=
       GNUNET_DISK_fn_write (fn,
-                           &crm->private_key, 
-                           sizeof (struct GNUNET_CRYPTO_EccPrivateKey),
+                           &crm->private_key,
+                           sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
                            GNUNET_DISK_PERM_USER_READ |
                            GNUNET_DISK_PERM_USER_WRITE))
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
                              "write", fn);
   GNUNET_free (fn);
-  notify_listeners (ego);  
+  notify_listeners (ego);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -540,7 +545,7 @@ handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
 /**
  * Closure for 'handle_ego_rename'.
  */
-struct RenameContext 
+struct RenameContext
 {
   /**
    * Old name.
@@ -576,14 +581,14 @@ handle_ego_rename (void *cls,
     return;
   if (0 != strcmp (id, rc->old_name))
   {
-    GNUNET_free (id);  
+    GNUNET_free (id);
     return;
   }
   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
                                         section,
                                         "DEFAULT_IDENTIFIER",
                                         rc->new_name);
-  GNUNET_free (id);  
+  GNUNET_free (id);
 }
 
 
@@ -610,7 +615,7 @@ handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
   char *fn_old;
   char *fn_new;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received RENAME message from client\n");
   size = ntohs (message->size);
   if (size <= sizeof (struct GNUNET_IDENTITY_RenameMessage))
@@ -640,7 +645,7 @@ handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
                     new_name))
     {
       send_result_code (client, 1, gettext_noop ("target name already exists"));
-      GNUNET_SERVER_receive_done (client, GNUNET_OK);      
+      GNUNET_SERVER_receive_done (client, GNUNET_OK);
       return;
     }
   }
@@ -658,14 +663,14 @@ handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
                                             &handle_ego_rename,
                                             &rename_ctx);
-      if (GNUNET_OK != 
+      if (GNUNET_OK !=
          GNUNET_CONFIGURATION_write (subsystem_cfg,
                                      subsystem_cfg_file))
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    _("Failed to write subsystem default identifier map to `%s'.\n"),
                    subsystem_cfg_file);
       ego->identifier = GNUNET_strdup (new_name);
-      fn_new = get_ego_filename (ego);      
+      fn_new = get_ego_filename (ego);
       if (0 != RENAME (fn_old, fn_new))
        GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rename", fn_old);
       GNUNET_free (fn_old);
@@ -705,14 +710,14 @@ handle_ego_delete (void *cls,
     return;
   if (0 != strcmp (id, identifier))
   {
-    GNUNET_free (id);  
+    GNUNET_free (id);
     return;
   }
   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
                                         section,
                                         "DEFAULT_IDENTIFIER",
                                         NULL);
-  GNUNET_free (id);  
+  GNUNET_free (id);
 }
 
 
@@ -735,7 +740,7 @@ handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
   const char *name;
   char *fn;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received DELETE message from client\n");
   size = ntohs (message->size);
   if (size <= sizeof (struct GNUNET_IDENTITY_DeleteMessage))
@@ -766,7 +771,7 @@ handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
                                             &handle_ego_delete,
                                             ego->identifier);
-      if (GNUNET_OK != 
+      if (GNUNET_OK !=
          GNUNET_CONFIGURATION_write (subsystem_cfg,
                                      subsystem_cfg_file))
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -779,7 +784,7 @@ handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
       GNUNET_free (ego->identifier);
       ego->identifier = NULL;
       notify_listeners (ego);
-      GNUNET_CRYPTO_ecc_key_free (ego->pk);
+      GNUNET_free (ego->pk);
       GNUNET_free (ego);
       send_result_code (client, 0, NULL);
       GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -798,9 +803,9 @@ handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
  *
  * @param cls NULL
  * @param filename name of the file to parse
- * @return GNUNET_OK to continue to iterate,
- *  GNUNET_NO to stop iteration with no error,
- *  GNUNET_SYSERR to abort iteration with error!
+ * @return #GNUNET_OK to continue to iterate,
+ *  #GNUNET_NO to stop iteration with no error,
+ *  #GNUNET_SYSERR to abort iteration with error!
  */
 static int
 process_ego_file (void *cls,
@@ -816,18 +821,18 @@ process_ego_file (void *cls,
     return GNUNET_OK;
   }
   ego = GNUNET_new (struct Ego);
-  ego->pk = GNUNET_CRYPTO_ecc_key_create_from_file (filename);
+  ego->pk = GNUNET_CRYPTO_ecdsa_key_create_from_file (filename);
   if (NULL == ego->pk)
-    {
-      GNUNET_free (ego);
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Failed to parse ego information in `%s'\n"),
-                 filename);
-      return GNUNET_OK;
-    }
+  {
+    GNUNET_free (ego);
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                _("Failed to parse ego information in `%s'\n"),
+                filename);
+    return GNUNET_OK;
+  }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Loaded ego `%s'\n",
-             fn + 1);  
+             fn + 1);
   ego->identifier = GNUNET_strdup (fn + 1);
   GNUNET_CONTAINER_DLL_insert (ego_head,
                               ego_tail,
@@ -844,7 +849,7 @@ process_ego_file (void *cls,
  * @param c configuration to use
  */
 static void
-run (void *cls, 
+run (void *cls,
      struct GNUNET_SERVER_Handle *server,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
@@ -889,7 +894,7 @@ run (void *cls,
   subsystem_cfg = GNUNET_CONFIGURATION_create ();
   if ( (GNUNET_YES ==
        GNUNET_DISK_file_test (subsystem_cfg_file)) &&
-       (GNUNET_OK != 
+       (GNUNET_OK !=
        GNUNET_CONFIGURATION_parse (subsystem_cfg,
                                    subsystem_cfg_file)) )
   {
@@ -928,7 +933,7 @@ int
 main (int argc, char *const *argv)
 {
   return (GNUNET_OK ==
-          GNUNET_SERVICE_run (argc, argv, "identity", 
+          GNUNET_SERVICE_run (argc, argv, "identity",
                              GNUNET_SERVICE_OPTION_NONE,
                               &run, NULL)) ? 0 : 1;
 }