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.
31 * - default identity set/get handlers
34 #include "gnunet_util_lib.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_identity_service.h"
43 * Information we keep about each ego.
49 * We keep egos in a DLL.
54 * We keep egos in a DLL.
59 * Private key of the ego.
61 struct GNUNET_CRYPTO_EccPrivateKey *pk;
64 * String identifier for the ego.
72 * Handle to our current configuration.
74 static const struct GNUNET_CONFIGURATION_Handle *cfg;
77 * Handle to subsystem configuration which for each subsystem contains
78 * the name of the default ego.
80 static struct GNUNET_CONFIGURATION_Handle *subsystem_cfg;
83 * Handle to the statistics service.
85 static struct GNUNET_STATISTICS_Handle *stats;
88 * Notification context, simplifies client broadcasts.
90 static struct GNUNET_SERVER_NotificationContext *nc;
93 * Directory where we store the identities.
95 static char *ego_directory;
98 * Configuration file name where subsystem information is kept.
100 static char *subsystem_cfg_file;
103 * Head of DLL of all egos.
105 static struct Ego *ego_head;
108 * Tail of DLL of all egos.
110 static struct Ego *ego_tail;
114 * Task run during shutdown.
120 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 GNUNET_SERVER_notification_context_destroy (nc);
131 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
134 GNUNET_CONFIGURATION_destroy (subsystem_cfg);
135 subsystem_cfg = NULL;
136 GNUNET_free (subsystem_cfg_file);
137 subsystem_cfg_file = NULL;
138 GNUNET_free (ego_directory);
139 ego_directory = NULL;
140 while (NULL != (e = ego_head))
142 GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
143 GNUNET_CRYPTO_ecc_key_free (e->pk);
150 * Send a result code back to the client.
152 * @param client client that should receive the result code
153 * @param result_code code to transmit
154 * @param emsg error message to include (or NULL for none)
157 send_result_code (struct GNUNET_SERVER_Client *client,
158 uint32_t result_code,
161 struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
167 elen = strlen (emsg) + 1;
168 rcm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
169 rcm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
170 rcm->header.size = htons (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
171 rcm->result_code = htonl (result_code);
172 memcpy (&rcm[1], emsg, elen);
173 GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_YES);
179 * Create an update message with information about the current state of an ego.
181 * @param ego ego to create message for
182 * @return corresponding update message
184 static struct GNUNET_IDENTITY_UpdateMessage *
185 create_update_message (struct Ego *ego)
187 struct GNUNET_IDENTITY_UpdateMessage *um;
191 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
193 name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
194 enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
195 pk_len = ntohs (enc->size);
196 um = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
197 um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
198 um->header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
199 um->name_len = htons (name_len);
200 um->pk_len = htons (pk_len);
201 str = (char *) &um[1];
202 memcpy (str, enc, pk_len);
203 memcpy (&str[pk_len], ego->identifier, name_len);
210 * Create a set default message with information about the current state of an ego.
212 * @param ego ego to create message for
213 * @param servicename name of the service to provide in the message
214 * @return corresponding set default message
216 static struct GNUNET_IDENTITY_SetDefaultMessage *
217 create_set_default_message (struct Ego *ego,
218 const char *servicename)
220 struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
224 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
226 name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
227 enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
228 pk_len = ntohs (enc->size);
229 sdm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
230 sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
231 sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
232 sdm->name_len = htons (name_len);
233 sdm->pk_len = htons (pk_len);
234 str = (char *) &sdm[1];
235 memcpy (str, enc, pk_len);
236 memcpy (&str[pk_len], servicename, name_len);
243 * Handler for START message from client, sends information
244 * about all identities to the client immediately and
245 * adds the client to the notification context for future
249 * @param client who sent the message
250 * @param message the message received
253 handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
254 const struct GNUNET_MessageHeader *message)
256 struct GNUNET_IDENTITY_UpdateMessage *um;
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Received START message from client\n");
261 GNUNET_SERVER_notification_context_add (nc, client);
262 for (ego = ego_head; NULL != ego; ego = ego->next)
264 um = create_update_message (ego);
265 GNUNET_SERVER_notification_context_unicast (nc, client, &um->header, GNUNET_YES);
268 GNUNET_SERVER_receive_done (client, GNUNET_OK);
273 * Handler for GET_DEFAULT message from client, returns
274 * default identity for some service.
277 * @param client who sent the message
278 * @param message the message received
281 handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
282 const struct GNUNET_MessageHeader *message)
284 const struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
285 struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Received GET_DEFAULT message from client\n");
294 size = ntohs (message->size);
295 if (size <= sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
298 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
301 gdm = (const struct GNUNET_IDENTITY_GetDefaultMessage *) message;
302 name = (const char *) &gdm[1];
303 name_len = ntohs (gdm->name_len);
304 if ( (name_len + sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) != size) ||
305 (0 != ntohs (gdm->reserved)) ||
306 ('\0' != name[name_len - 1]) )
309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
313 GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
315 "DEFAULT_IDENTIFIER",
318 /* FIXME: not sure client API handles this case correctly right now... */
319 send_result_code (client, 1, gettext_noop ("no default known"));
320 GNUNET_SERVER_receive_done (client, GNUNET_OK);
323 for (ego = ego_head; NULL != ego; ego = ego->next)
325 if (0 == strcmp (ego->identifier,
328 sdm = create_set_default_message (ego,
330 GNUNET_SERVER_notification_context_broadcast (nc, &sdm->header, GNUNET_YES);
332 GNUNET_SERVER_receive_done (client, GNUNET_OK);
336 /* FIXME: not sure client API handles this case correctly right now... */
337 send_result_code (client, 1,
338 gettext_noop ("default configured, but ego unknown (internal error)"));
339 GNUNET_SERVER_receive_done (client, GNUNET_OK);
344 * Handler for SET_DEFAULT message from client, updates
345 * default identity for some service.
348 * @param client who sent the message
349 * @param message the message received
352 handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
353 const struct GNUNET_MessageHeader *message)
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Received SET_DEFAULT message from client\n");
357 // setup_estimate_message (&em);
358 // GNUNET_SERVER_notification_context_unicast (nc, client, &em.header, GNUNET_YES);
359 GNUNET_break (0); // not implemented!
360 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
365 * Send an updated message for the given ego to all listeners.
367 * @param ego ego to send the update for
370 notify_listeners (struct Ego *ego)
372 struct GNUNET_IDENTITY_UpdateMessage *um;
374 um = create_update_message (ego);
375 GNUNET_SERVER_notification_context_broadcast (nc, &um->header, GNUNET_YES);
381 * Handler for CREATE message from client, creates
385 * @param client who sent the message
386 * @param message the message received
389 handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
390 const struct GNUNET_MessageHeader *message)
392 const struct GNUNET_IDENTITY_CreateRequestMessage *crm;
398 struct GNUNET_CRYPTO_EccPrivateKey *pk;
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "Received CREATE message from client\n");
402 size = ntohs (message->size);
403 if (size <= sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
409 crm = (const struct GNUNET_IDENTITY_CreateRequestMessage *) message;
410 name_len = ntohs (crm->name_len);
411 pk_len = ntohs (crm->pk_len);
412 str = (const char *) &crm[1];
413 if ( (name_len + pk_len + sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) != size) ||
414 (NULL == (pk = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES))) )
417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
421 if ('\0' != str[name_len - 1])
424 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
427 for (ego = ego_head; NULL != ego; ego = ego->next)
429 if (0 == strcmp (ego->identifier,
432 send_result_code (client, 1, gettext_noop ("identifier already in use for another ego"));
433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
434 GNUNET_CRYPTO_ecc_key_free (pk);
438 ego = GNUNET_new (struct Ego);
440 ego->identifier = GNUNET_strdup (str);
441 GNUNET_CONTAINER_DLL_insert (ego_head,
444 send_result_code (client, 0, NULL);
445 /* FIXME: also write to file! */
446 notify_listeners (ego);
447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
453 * Handler for RENAME message from client, creates
457 * @param client who sent the message
458 * @param message the message received
461 handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
462 const struct GNUNET_MessageHeader *message)
464 const struct GNUNET_IDENTITY_RenameMessage *rm;
466 uint16_t old_name_len;
467 uint16_t new_name_len;
469 const char *old_name;
470 const char *new_name;
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Received RENAME message from client\n");
474 size = ntohs (message->size);
475 if (size <= sizeof (struct GNUNET_IDENTITY_RenameMessage))
478 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
481 rm = (const struct GNUNET_IDENTITY_RenameMessage *) message;
482 old_name_len = ntohs (rm->old_name_len);
483 new_name_len = ntohs (rm->new_name_len);
484 old_name = (const char *) &rm[1];
485 new_name = &old_name[old_name_len];
486 if ( (old_name_len + new_name_len + sizeof (struct GNUNET_IDENTITY_RenameMessage) != size) ||
487 ('\0' != old_name[old_name_len - 1]) ||
488 ('\0' != new_name[new_name_len - 1]) )
491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
494 for (ego = ego_head; NULL != ego; ego = ego->next)
496 if (0 == strcmp (ego->identifier,
499 GNUNET_free (ego->identifier);
500 ego->identifier = GNUNET_strdup (new_name);
501 /* FIXME: also rename file! */
502 notify_listeners (ego);
503 send_result_code (client, 0, NULL);
504 GNUNET_SERVER_receive_done (client, GNUNET_OK);
509 send_result_code (client, 1, gettext_noop ("no matching ego found"));
510 GNUNET_SERVER_receive_done (client, GNUNET_OK);
515 * Handler for DELETE message from client, creates
519 * @param client who sent the message
520 * @param message the message received
523 handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
524 const struct GNUNET_MessageHeader *message)
526 const struct GNUNET_IDENTITY_DeleteMessage *dm;
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Received DELETE message from client\n");
534 size = ntohs (message->size);
535 if (size <= sizeof (struct GNUNET_IDENTITY_DeleteMessage))
538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
541 dm = (const struct GNUNET_IDENTITY_DeleteMessage *) message;
542 name = (const char *) &dm[1];
543 name_len = ntohs (dm->name_len);
544 if ( (name_len + sizeof (struct GNUNET_IDENTITY_DeleteMessage) != size) ||
545 (0 != ntohs (dm->reserved)) ||
546 ('\0' != name[name_len - 1]) )
549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
552 for (ego = ego_head; NULL != ego; ego = ego->next)
554 if (0 == strcmp (ego->identifier,
557 GNUNET_CONTAINER_DLL_remove (ego_head,
560 /* FIXME: also delete file! */
561 GNUNET_free (ego->identifier);
562 ego->identifier = NULL;
563 notify_listeners (ego);
564 GNUNET_CRYPTO_ecc_key_free (ego->pk);
566 send_result_code (client, 0, NULL);
567 GNUNET_SERVER_receive_done (client, GNUNET_OK);
572 send_result_code (client, 1, gettext_noop ("no matching ego found"));
573 GNUNET_SERVER_receive_done (client, GNUNET_OK);
578 * Process the given file from the "EGODIR". Parses the file
579 * and creates the respective 'struct Ego' in memory.
582 * @param filename name of the file to parse
583 * @return GNUNET_OK to continue to iterate,
584 * GNUNET_NO to stop iteration with no error,
585 * GNUNET_SYSERR to abort iteration with error!
588 process_ego_file (void *cls,
589 const char *filename)
591 GNUNET_break (0); // not implemented
597 * Handle network size estimate clients.
600 * @param server the initialized server
601 * @param c configuration to use
605 struct GNUNET_SERVER_Handle *server,
606 const struct GNUNET_CONFIGURATION_Handle *c)
608 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
609 {&handle_start_message, NULL,
610 GNUNET_MESSAGE_TYPE_IDENTITY_START, sizeof (struct GNUNET_MessageHeader)},
611 {&handle_get_default_message, NULL,
612 GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT, 0},
613 {&handle_set_default_message, NULL,
614 GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT, 0},
615 {&handle_create_message, NULL,
616 GNUNET_MESSAGE_TYPE_IDENTITY_CREATE, 0},
617 {&handle_rename_message, NULL,
618 GNUNET_MESSAGE_TYPE_IDENTITY_RENAME, 0},
619 {&handle_delete_message, NULL,
620 GNUNET_MESSAGE_TYPE_IDENTITY_DELETE, 0},
626 GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
630 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
631 GNUNET_SCHEDULER_shutdown ();
635 GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
637 &subsystem_cfg_file))
639 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "SUBSYSTEM_CFG");
640 GNUNET_SCHEDULER_shutdown ();
643 subsystem_cfg = GNUNET_CONFIGURATION_create ();
645 GNUNET_DISK_file_test (subsystem_cfg_file)) &&
647 GNUNET_CONFIGURATION_parse (subsystem_cfg,
648 subsystem_cfg_file)) )
650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
651 _("Failed to parse subsystem identity configuration file `%s'\n"),
653 GNUNET_SCHEDULER_shutdown ();
656 stats = GNUNET_STATISTICS_create ("identity", cfg);
657 GNUNET_SERVER_add_handlers (server, handlers);
658 nc = GNUNET_SERVER_notification_context_create (server, 1);
659 GNUNET_DISK_directory_scan (ego_directory,
662 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
668 * The main function for the network size estimation service.
670 * @param argc number of arguments from the command line
671 * @param argv command line arguments
672 * @return 0 ok, 1 on error
675 main (int argc, char *const *argv)
678 GNUNET_SERVICE_run (argc, argv, "identity",
679 GNUNET_SERVICE_OPTION_NONE,
680 &run, NULL)) ? 0 : 1;
684 /* end of gnunet-service-identity.c */