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.
30 #include "gnunet_util_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_identity_service.h"
39 * Information we keep about each ego.
45 * We keep egos in a DLL.
50 * We keep egos in a DLL.
55 * Private key of the ego.
57 struct GNUNET_CRYPTO_EccPrivateKey *pk;
60 * String identifier for the ego.
68 * Handle to our current configuration.
70 static const struct GNUNET_CONFIGURATION_Handle *cfg;
73 * Handle to subsystem configuration which for each subsystem contains
74 * the name of the default ego.
76 static struct GNUNET_CONFIGURATION_Handle *subsystem_cfg;
79 * Handle to the statistics service.
81 static struct GNUNET_STATISTICS_Handle *stats;
84 * Notification context, simplifies client broadcasts.
86 static struct GNUNET_SERVER_NotificationContext *nc;
89 * Directory where we store the identities.
91 static char *ego_directory;
94 * Configuration file name where subsystem information is kept.
96 static char *subsystem_cfg_file;
99 * Head of DLL of all egos.
101 static struct Ego *ego_head;
104 * Tail of DLL of all egos.
106 static struct Ego *ego_tail;
110 * Task run during shutdown.
116 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 GNUNET_SERVER_notification_context_destroy (nc);
127 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
130 GNUNET_CONFIGURATION_destroy (subsystem_cfg);
131 subsystem_cfg = NULL;
132 GNUNET_free (subsystem_cfg_file);
133 subsystem_cfg_file = NULL;
134 GNUNET_free (ego_directory);
135 ego_directory = NULL;
136 while (NULL != (e = ego_head))
138 GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
139 GNUNET_CRYPTO_ecc_key_free (e->pk);
146 * Send a result code back to the client.
148 * @param client client that should receive the result code
149 * @param result_code code to transmit
150 * @param emsg error message to include (or NULL for none)
153 send_result_code (struct GNUNET_SERVER_Client *client,
154 uint32_t result_code,
157 struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
163 elen = strlen (emsg) + 1;
164 rcm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
165 rcm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
166 rcm->header.size = htons (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
167 rcm->result_code = htonl (result_code);
168 memcpy (&rcm[1], emsg, elen);
169 GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_YES);
175 * Create an update message with information about the current state of an ego.
177 * @param ego ego to create message for
178 * @return corresponding update message
180 static struct GNUNET_IDENTITY_UpdateMessage *
181 create_update_message (struct Ego *ego)
183 struct GNUNET_IDENTITY_UpdateMessage *um;
187 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
189 name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
190 enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
191 pk_len = ntohs (enc->size);
192 um = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
193 um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
194 um->header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
195 um->name_len = htons (name_len);
196 um->pk_len = htons (pk_len);
197 str = (char *) &um[1];
198 memcpy (str, enc, pk_len);
199 memcpy (&str[pk_len], ego->identifier, name_len);
206 * Handler for START message from client, sends information
207 * about all identities to the client immediately and
208 * adds the client to the notification context for future
212 * @param client who sent the message
213 * @param message the message received
216 handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
217 const struct GNUNET_MessageHeader *message)
219 struct GNUNET_IDENTITY_UpdateMessage *um;
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Received START message from client\n");
224 GNUNET_SERVER_notification_context_add (nc, client);
225 for (ego = ego_head; NULL != ego; ego = ego->next)
227 um = create_update_message (ego);
228 GNUNET_SERVER_notification_context_unicast (nc, client, &um->header, GNUNET_YES);
231 GNUNET_SERVER_receive_done (client, GNUNET_OK);
236 * Handler for GET_DEFAULT message from client, returns
237 * default identity for some service.
240 * @param client who sent the message
241 * @param message the message received
244 handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
245 const struct GNUNET_MessageHeader *message)
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Received GET_DEFAULT message from client\n");
249 // setup_estimate_message (&em);
250 // GNUNET_SERVER_notification_context_unicast (nc, client, &em.header, GNUNET_YES);
251 GNUNET_break (0); // not implemented!
252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
257 * Handler for SET_DEFAULT message from client, updates
258 * default identity for some service.
261 * @param client who sent the message
262 * @param message the message received
265 handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
266 const struct GNUNET_MessageHeader *message)
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "Received SET_DEFAULT message from client\n");
270 // setup_estimate_message (&em);
271 // GNUNET_SERVER_notification_context_unicast (nc, client, &em.header, GNUNET_YES);
272 GNUNET_break (0); // not implemented!
273 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
278 * Send an updated message for the given ego to all listeners.
280 * @param ego ego to send the update for
283 notify_listeners (struct Ego *ego)
285 struct GNUNET_IDENTITY_UpdateMessage *um;
287 um = create_update_message (ego);
288 GNUNET_SERVER_notification_context_broadcast (nc, &um->header, GNUNET_YES);
294 * Handler for CREATE message from client, creates
298 * @param client who sent the message
299 * @param message the message received
302 handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
303 const struct GNUNET_MessageHeader *message)
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Received CREATE message from client\n");
307 // setup_estimate_message (&em);
308 // GNUNET_SERVER_notification_context_unicast (nc, client, &em.header, GNUNET_YES);
309 GNUNET_break (0); // not implemented!
310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
316 * Handler for RENAME message from client, creates
320 * @param client who sent the message
321 * @param message the message received
324 handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
325 const struct GNUNET_MessageHeader *message)
327 const struct GNUNET_IDENTITY_RenameMessage *rm;
329 uint16_t old_name_len;
330 uint16_t new_name_len;
332 const char *old_name;
333 const char *new_name;
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "Received RENAME message from client\n");
337 size = ntohs (message->size);
338 if (size <= sizeof (struct GNUNET_IDENTITY_RenameMessage))
341 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
344 rm = (const struct GNUNET_IDENTITY_RenameMessage *) message;
345 old_name_len = ntohs (rm->old_name_len);
346 new_name_len = ntohs (rm->new_name_len);
347 old_name = (const char *) &rm[1];
348 new_name = &old_name[old_name_len];
349 if ( (old_name_len + new_name_len + sizeof (struct GNUNET_IDENTITY_RenameMessage) != size) ||
350 ('\0' != old_name[old_name_len - 1]) ||
351 ('\0' != new_name[new_name_len - 1]) )
354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
357 for (ego = ego_head; NULL != ego; ego = ego->next)
359 if (0 == strcmp (ego->identifier,
362 GNUNET_free (ego->identifier);
363 ego->identifier = GNUNET_strdup (new_name);
364 /* FIXME: also rename file! */
365 notify_listeners (ego);
366 send_result_code (client, 0, NULL);
367 GNUNET_SERVER_receive_done (client, GNUNET_OK);
372 send_result_code (client, 1, gettext_noop ("no matching ego found"));
373 GNUNET_SERVER_receive_done (client, GNUNET_OK);
378 * Handler for DELETE message from client, creates
382 * @param client who sent the message
383 * @param message the message received
386 handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
387 const struct GNUNET_MessageHeader *message)
389 const struct GNUNET_IDENTITY_DeleteMessage *dm;
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Received DELETE message from client\n");
397 size = ntohs (message->size);
398 if (size <= sizeof (struct GNUNET_IDENTITY_DeleteMessage))
401 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
404 dm = (const struct GNUNET_IDENTITY_DeleteMessage *) message;
405 name = (const char *) &dm[1];
406 name_len = ntohs (dm->name_len);
407 if ( (name_len + sizeof (struct GNUNET_IDENTITY_DeleteMessage) != size) ||
408 (0 != ntohs (dm->reserved)) ||
409 ('\0' != name[name_len - 1]) )
412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
415 for (ego = ego_head; NULL != ego; ego = ego->next)
417 if (0 == strcmp (ego->identifier,
420 GNUNET_CONTAINER_DLL_remove (ego_head,
423 /* FIXME: also delete file! */
424 GNUNET_free (ego->identifier);
425 ego->identifier = NULL;
426 notify_listeners (ego);
427 GNUNET_CRYPTO_ecc_key_free (ego->pk);
429 send_result_code (client, 0, NULL);
430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
435 send_result_code (client, 1, gettext_noop ("no matching ego found"));
436 GNUNET_SERVER_receive_done (client, GNUNET_OK);
441 * Handle network size estimate clients.
444 * @param server the initialized server
445 * @param c configuration to use
449 struct GNUNET_SERVER_Handle *server,
450 const struct GNUNET_CONFIGURATION_Handle *c)
452 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
453 {&handle_start_message, NULL,
454 GNUNET_MESSAGE_TYPE_IDENTITY_START, sizeof (struct GNUNET_MessageHeader)},
455 {&handle_get_default_message, NULL,
456 GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT, 0},
457 {&handle_set_default_message, NULL,
458 GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT, 0},
459 {&handle_create_message, NULL,
460 GNUNET_MESSAGE_TYPE_IDENTITY_CREATE, 0},
461 {&handle_rename_message, NULL,
462 GNUNET_MESSAGE_TYPE_IDENTITY_RENAME, 0},
463 {&handle_delete_message, NULL,
464 GNUNET_MESSAGE_TYPE_IDENTITY_DELETE, 0},
470 GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
474 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
475 GNUNET_SCHEDULER_shutdown ();
479 GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
481 &subsystem_cfg_file))
483 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "SUBSYSTEM_CFG");
484 GNUNET_SCHEDULER_shutdown ();
487 subsystem_cfg = GNUNET_CONFIGURATION_create ();
489 GNUNET_DISK_file_test (subsystem_cfg_file)) &&
491 GNUNET_CONFIGURATION_parse (subsystem_cfg,
492 subsystem_cfg_file)) )
494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
495 _("Failed to parse subsystem identity configuration file `%s'\n"),
497 GNUNET_SCHEDULER_shutdown ();
500 stats = GNUNET_STATISTICS_create ("identity", cfg);
501 GNUNET_SERVER_add_handlers (server, handlers);
502 nc = GNUNET_SERVER_notification_context_create (server, 1);
503 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
509 * The main function for the network size estimation service.
511 * @param argc number of arguments from the command line
512 * @param argv command line arguments
513 * @return 0 ok, 1 on error
516 main (int argc, char *const *argv)
519 GNUNET_SERVICE_run (argc, argv, "identity",
520 GNUNET_SERVICE_OPTION_NONE,
521 &run, NULL)) ? 0 : 1;
525 /* end of gnunet-service-identity.c */