2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file identity/gnunet-service-identity.c
23 * @brief identity management service
24 * @author Christian Grothoff
26 * The purpose of this service is to manage private keys that
27 * represent the various egos/pseudonyms/identities of a GNUnet user.
33 #include "gnunet_util_lib.h"
34 #include "gnunet_constants.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_identity_service.h"
42 * Information we keep about each ego.
48 * We keep egos in a DLL.
53 * We keep egos in a DLL.
58 * Private key of the ego.
60 struct GNUNET_CRYPTO_EccPrivateKey *pk;
63 * String identifier for the ego.
71 * Handle to our current configuration.
73 static const struct GNUNET_CONFIGURATION_Handle *cfg;
76 * Handle to subsystem configuration which for each subsystem contains
77 * the name of the default ego.
79 static struct GNUNET_CONFIGURATION_Handle *subsystem_cfg;
82 * Handle to the statistics service.
84 static struct GNUNET_STATISTICS_Handle *stats;
87 * Notification context, simplifies client broadcasts.
89 static struct GNUNET_SERVER_NotificationContext *nc;
92 * Directory where we store the identities.
94 static char *ego_directory;
97 * Configuration file name where subsystem information is kept.
99 static char *subsystem_cfg_file;
102 * Head of DLL of all egos.
104 static struct Ego *ego_head;
107 * Tail of DLL of all egos.
109 static struct Ego *ego_tail;
113 * Task run during shutdown.
119 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
125 GNUNET_SERVER_notification_context_destroy (nc);
130 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
133 GNUNET_CONFIGURATION_destroy (subsystem_cfg);
134 subsystem_cfg = NULL;
135 GNUNET_free (subsystem_cfg_file);
136 subsystem_cfg_file = NULL;
137 GNUNET_free (ego_directory);
138 ego_directory = NULL;
139 while (NULL != (e = ego_head))
141 GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
142 GNUNET_CRYPTO_ecc_key_free (e->pk);
149 * Send a result code back to the client.
151 * @param client client that should receive the result code
152 * @param result_code code to transmit
153 * @param emsg error message to include (or NULL for none)
156 send_result_code (struct GNUNET_SERVER_Client *client,
157 uint32_t result_code,
160 struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
166 elen = strlen (emsg) + 1;
167 rcm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
168 rcm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
169 rcm->header.size = htons (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
170 rcm->result_code = htonl (result_code);
171 memcpy (&rcm[1], emsg, elen);
172 GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_YES);
178 * Create an update message with information about the current state of an ego.
180 * @param ego ego to create message for
181 * @return corresponding update message
183 static struct GNUNET_IDENTITY_UpdateMessage *
184 create_update_message (struct Ego *ego)
186 struct GNUNET_IDENTITY_UpdateMessage *um;
190 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
192 name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
193 enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
194 pk_len = ntohs (enc->size);
195 um = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
196 um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
197 um->header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
198 um->name_len = htons (name_len);
199 um->pk_len = htons (pk_len);
200 str = (char *) &um[1];
201 memcpy (str, enc, pk_len);
202 memcpy (&str[pk_len], ego->identifier, name_len);
209 * Create a set default message with information about the current state of an ego.
211 * @param ego ego to create message for
212 * @param servicename name of the service to provide in the message
213 * @return corresponding set default message
215 static struct GNUNET_IDENTITY_SetDefaultMessage *
216 create_set_default_message (struct Ego *ego,
217 const char *servicename)
219 struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
223 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
225 name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
226 enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
227 pk_len = ntohs (enc->size);
228 sdm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
229 sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
230 sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
231 sdm->name_len = htons (name_len);
232 sdm->pk_len = htons (pk_len);
233 str = (char *) &sdm[1];
234 memcpy (str, enc, pk_len);
235 memcpy (&str[pk_len], servicename, name_len);
242 * Handler for START message from client, sends information
243 * about all identities to the client immediately and
244 * adds the client to the notification context for future
248 * @param client who sent the message
249 * @param message the message received
252 handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
253 const struct GNUNET_MessageHeader *message)
255 struct GNUNET_IDENTITY_UpdateMessage *um;
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Received START message from client\n");
260 GNUNET_SERVER_notification_context_add (nc, client);
261 for (ego = ego_head; NULL != ego; ego = ego->next)
263 um = create_update_message (ego);
264 GNUNET_SERVER_notification_context_unicast (nc, client, &um->header, GNUNET_YES);
267 GNUNET_SERVER_receive_done (client, GNUNET_OK);
272 * Handler for GET_DEFAULT message from client, returns
273 * default identity for some service.
276 * @param client who sent the message
277 * @param message the message received
280 handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
281 const struct GNUNET_MessageHeader *message)
283 const struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
284 struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Received GET_DEFAULT message from client\n");
293 size = ntohs (message->size);
294 if (size <= sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
297 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
300 gdm = (const struct GNUNET_IDENTITY_GetDefaultMessage *) message;
301 name = (const char *) &gdm[1];
302 name_len = ntohs (gdm->name_len);
303 if ( (name_len + sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) != size) ||
304 (0 != ntohs (gdm->reserved)) ||
305 ('\0' != name[name_len - 1]) )
308 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
312 GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
314 "DEFAULT_IDENTIFIER",
317 send_result_code (client, 1, gettext_noop ("no default known"));
318 GNUNET_SERVER_receive_done (client, GNUNET_OK);
321 for (ego = ego_head; NULL != ego; ego = ego->next)
323 if (0 == strcmp (ego->identifier,
326 sdm = create_set_default_message (ego,
328 GNUNET_SERVER_notification_context_broadcast (nc, &sdm->header, GNUNET_YES);
330 GNUNET_SERVER_receive_done (client, GNUNET_OK);
334 send_result_code (client, 1,
335 gettext_noop ("default configured, but ego unknown (internal error)"));
336 GNUNET_SERVER_receive_done (client, GNUNET_OK);
341 * Compare the given two private keys for equality.
343 * @param pk1 one private key
344 * @param pk2 another private key
345 * @return 0 if the keys are equal
348 key_cmp (const struct GNUNET_CRYPTO_EccPrivateKey *pk1,
349 const struct GNUNET_CRYPTO_EccPrivateKey *pk2)
351 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p1;
352 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p2;
354 GNUNET_CRYPTO_ecc_key_get_public (pk1, &p1);
355 GNUNET_CRYPTO_ecc_key_get_public (pk2, &p2);
356 return memcmp (&p1, &p2, sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
361 * Handler for SET_DEFAULT message from client, updates
362 * default identity for some service.
365 * @param client who sent the message
366 * @param message the message received
369 handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
370 const struct GNUNET_MessageHeader *message)
372 const struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
378 struct GNUNET_CRYPTO_EccPrivateKey *pk;
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Received SET_DEFAULT message from client\n");
382 size = ntohs (message->size);
383 if (size <= sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
389 sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) message;
390 name_len = ntohs (sdm->name_len);
391 pk_len = ntohs (sdm->pk_len);
392 str = (const char *) &sdm[1];
393 if ( (name_len + pk_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) != size) ||
394 (NULL == (pk = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES))) )
397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
401 if ('\0' != str[name_len - 1])
404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
407 for (ego = ego_head; NULL != ego; ego = ego->next)
409 if (0 == key_cmp (ego->pk,
412 GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
414 "DEFAULT_IDENTIFIER",
416 /* fixme: write configuration to disk... */
417 send_result_code (client, 0, NULL);
418 GNUNET_SERVER_receive_done (client, GNUNET_OK);
419 GNUNET_CRYPTO_ecc_key_free (pk);
423 send_result_code (client, 1, _("Unknown ego specified for service (internal error)"));
424 GNUNET_SERVER_receive_done (client, GNUNET_OK);
425 GNUNET_CRYPTO_ecc_key_free (pk);
430 * Send an updated message for the given ego to all listeners.
432 * @param ego ego to send the update for
435 notify_listeners (struct Ego *ego)
437 struct GNUNET_IDENTITY_UpdateMessage *um;
439 um = create_update_message (ego);
440 GNUNET_SERVER_notification_context_broadcast (nc, &um->header, GNUNET_YES);
446 * Handler for CREATE message from client, creates
450 * @param client who sent the message
451 * @param message the message received
454 handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
455 const struct GNUNET_MessageHeader *message)
457 const struct GNUNET_IDENTITY_CreateRequestMessage *crm;
463 struct GNUNET_CRYPTO_EccPrivateKey *pk;
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Received CREATE message from client\n");
467 size = ntohs (message->size);
468 if (size <= sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
474 crm = (const struct GNUNET_IDENTITY_CreateRequestMessage *) message;
475 name_len = ntohs (crm->name_len);
476 pk_len = ntohs (crm->pk_len);
477 str = (const char *) &crm[1];
478 if ( (name_len + pk_len + sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) != size) ||
479 (NULL == (pk = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES))) )
482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
486 if ('\0' != str[name_len - 1])
489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
492 for (ego = ego_head; NULL != ego; ego = ego->next)
494 if (0 == strcmp (ego->identifier,
497 send_result_code (client, 1, gettext_noop ("identifier already in use for another ego"));
498 GNUNET_SERVER_receive_done (client, GNUNET_OK);
499 GNUNET_CRYPTO_ecc_key_free (pk);
503 ego = GNUNET_new (struct Ego);
505 ego->identifier = GNUNET_strdup (str);
506 GNUNET_CONTAINER_DLL_insert (ego_head,
509 send_result_code (client, 0, NULL);
510 /* FIXME: also write to file! */
511 notify_listeners (ego);
512 GNUNET_SERVER_receive_done (client, GNUNET_OK);
518 * Handler for RENAME message from client, creates
522 * @param client who sent the message
523 * @param message the message received
526 handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
527 const struct GNUNET_MessageHeader *message)
529 const struct GNUNET_IDENTITY_RenameMessage *rm;
531 uint16_t old_name_len;
532 uint16_t new_name_len;
534 const char *old_name;
535 const char *new_name;
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Received RENAME message from client\n");
539 size = ntohs (message->size);
540 if (size <= sizeof (struct GNUNET_IDENTITY_RenameMessage))
543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
546 rm = (const struct GNUNET_IDENTITY_RenameMessage *) message;
547 old_name_len = ntohs (rm->old_name_len);
548 new_name_len = ntohs (rm->new_name_len);
549 old_name = (const char *) &rm[1];
550 new_name = &old_name[old_name_len];
551 if ( (old_name_len + new_name_len + sizeof (struct GNUNET_IDENTITY_RenameMessage) != size) ||
552 ('\0' != old_name[old_name_len - 1]) ||
553 ('\0' != new_name[new_name_len - 1]) )
556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
559 for (ego = ego_head; NULL != ego; ego = ego->next)
561 if (0 == strcmp (ego->identifier,
564 GNUNET_free (ego->identifier);
565 ego->identifier = GNUNET_strdup (new_name);
566 /* FIXME: also rename file! */
567 notify_listeners (ego);
568 send_result_code (client, 0, NULL);
569 GNUNET_SERVER_receive_done (client, GNUNET_OK);
574 send_result_code (client, 1, gettext_noop ("no matching ego found"));
575 GNUNET_SERVER_receive_done (client, GNUNET_OK);
580 * Handler for DELETE message from client, creates
584 * @param client who sent the message
585 * @param message the message received
588 handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
589 const struct GNUNET_MessageHeader *message)
591 const struct GNUNET_IDENTITY_DeleteMessage *dm;
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Received DELETE message from client\n");
599 size = ntohs (message->size);
600 if (size <= sizeof (struct GNUNET_IDENTITY_DeleteMessage))
603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
606 dm = (const struct GNUNET_IDENTITY_DeleteMessage *) message;
607 name = (const char *) &dm[1];
608 name_len = ntohs (dm->name_len);
609 if ( (name_len + sizeof (struct GNUNET_IDENTITY_DeleteMessage) != size) ||
610 (0 != ntohs (dm->reserved)) ||
611 ('\0' != name[name_len - 1]) )
614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
617 for (ego = ego_head; NULL != ego; ego = ego->next)
619 if (0 == strcmp (ego->identifier,
622 GNUNET_CONTAINER_DLL_remove (ego_head,
625 /* FIXME: also delete file! */
626 GNUNET_free (ego->identifier);
627 ego->identifier = NULL;
628 notify_listeners (ego);
629 GNUNET_CRYPTO_ecc_key_free (ego->pk);
631 send_result_code (client, 0, NULL);
632 GNUNET_SERVER_receive_done (client, GNUNET_OK);
637 send_result_code (client, 1, gettext_noop ("no matching ego found"));
638 GNUNET_SERVER_receive_done (client, GNUNET_OK);
643 * Process the given file from the "EGODIR". Parses the file
644 * and creates the respective 'struct Ego' in memory.
647 * @param filename name of the file to parse
648 * @return GNUNET_OK to continue to iterate,
649 * GNUNET_NO to stop iteration with no error,
650 * GNUNET_SYSERR to abort iteration with error!
653 process_ego_file (void *cls,
654 const char *filename)
656 GNUNET_break (0); // not implemented
662 * Handle network size estimate clients.
665 * @param server the initialized server
666 * @param c configuration to use
670 struct GNUNET_SERVER_Handle *server,
671 const struct GNUNET_CONFIGURATION_Handle *c)
673 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
674 {&handle_start_message, NULL,
675 GNUNET_MESSAGE_TYPE_IDENTITY_START, sizeof (struct GNUNET_MessageHeader)},
676 {&handle_get_default_message, NULL,
677 GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT, 0},
678 {&handle_set_default_message, NULL,
679 GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT, 0},
680 {&handle_create_message, NULL,
681 GNUNET_MESSAGE_TYPE_IDENTITY_CREATE, 0},
682 {&handle_rename_message, NULL,
683 GNUNET_MESSAGE_TYPE_IDENTITY_RENAME, 0},
684 {&handle_delete_message, NULL,
685 GNUNET_MESSAGE_TYPE_IDENTITY_DELETE, 0},
691 GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
695 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
696 GNUNET_SCHEDULER_shutdown ();
700 GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
702 &subsystem_cfg_file))
704 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "SUBSYSTEM_CFG");
705 GNUNET_SCHEDULER_shutdown ();
708 subsystem_cfg = GNUNET_CONFIGURATION_create ();
710 GNUNET_DISK_file_test (subsystem_cfg_file)) &&
712 GNUNET_CONFIGURATION_parse (subsystem_cfg,
713 subsystem_cfg_file)) )
715 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
716 _("Failed to parse subsystem identity configuration file `%s'\n"),
718 GNUNET_SCHEDULER_shutdown ();
721 stats = GNUNET_STATISTICS_create ("identity", cfg);
722 GNUNET_SERVER_add_handlers (server, handlers);
723 nc = GNUNET_SERVER_notification_context_create (server, 1);
724 GNUNET_DISK_directory_scan (ego_directory,
727 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
733 * The main function for the network size estimation service.
735 * @param argc number of arguments from the command line
736 * @param argv command line arguments
737 * @return 0 ok, 1 on error
740 main (int argc, char *const *argv)
743 GNUNET_SERVICE_run (argc, argv, "identity",
744 GNUNET_SERVICE_OPTION_NONE,
745 &run, NULL)) ? 0 : 1;
749 /* end of gnunet-service-identity.c */