avoid failing hard if 'gnunetcheck' db does not exist
[oweals/gnunet.git] / src / identity / gnunet-service-identity.c
1 /*
2   This file is part of GNUnet.
3   Copyright (C) 2013 GNUnet e.V.
4
5   GNUnet is free software: you can redistribute it and/or modify it
6   under the terms of the GNU Affero General Public License as published
7   by the Free Software Foundation, either version 3 of the License,
8   or (at your option) any later version.
9
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   Affero General Public License for more details.
14  
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file identity/gnunet-service-identity.c
23  * @brief identity management service
24  * @author Christian Grothoff
25  *
26  * The purpose of this service is to manage private keys that
27  * represent the various egos/pseudonyms/identities of a GNUnet user.
28  *
29  * Todo:
30  * - auto-initialze default egos; maybe trigger default
31  *   initializations (such as gnunet-gns-import.sh?)
32  */
33 #include "platform.h"
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"
39 #include "identity.h"
40
41
42 /**
43  * Information we keep about each ego.
44  */
45 struct Ego
46 {
47
48   /**
49    * We keep egos in a DLL.
50    */
51   struct Ego *next;
52
53   /**
54    * We keep egos in a DLL.
55    */
56   struct Ego *prev;
57
58   /**
59    * Private key of the ego.
60    */
61   struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
62
63   /**
64    * String identifier for the ego.
65    */
66   char *identifier;
67
68 };
69
70
71 /**
72  * Handle to our current configuration.
73  */
74 static const struct GNUNET_CONFIGURATION_Handle *cfg;
75
76 /**
77  * Handle to subsystem configuration which for each subsystem contains
78  * the name of the default ego.
79  */
80 static struct GNUNET_CONFIGURATION_Handle *subsystem_cfg;
81
82 /**
83  * Handle to the statistics service.
84  */
85 static struct GNUNET_STATISTICS_Handle *stats;
86
87 /**
88  * Notification context, simplifies client broadcasts.
89  */
90 static struct GNUNET_NotificationContext *nc;
91
92 /**
93  * Directory where we store the identities.
94  */
95 static char *ego_directory;
96
97 /**
98  * Configuration file name where subsystem information is kept.
99  */
100 static char *subsystem_cfg_file;
101
102 /**
103  * Head of DLL of all egos.
104  */
105 static struct Ego *ego_head;
106
107 /**
108  * Tail of DLL of all egos.
109  */
110 static struct Ego *ego_tail;
111
112
113 /**
114  * Get the name of the file we use to store a given ego.
115  *
116  * @param ego ego for which we need the filename
117  * @return full filename for the given ego
118  */
119 static char *
120 get_ego_filename (struct Ego *ego)
121 {
122   char *filename;
123
124   GNUNET_asprintf (&filename,
125                    "%s%s%s",
126                    ego_directory,
127                    DIR_SEPARATOR_STR,
128                    ego->identifier);
129   return filename;
130 }
131
132 /**
133  * Called whenever a client is disconnected.
134  *
135  * @param cls closure
136  * @param client identification of the client
137  * @param app_ctx @a client
138  */
139 static void
140 client_disconnect_cb (void *cls,
141                       struct GNUNET_SERVICE_Client *client,
142                       void *app_ctx)
143 {
144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145               "Client %p disconnected\n",
146               client);
147 }
148
149
150 /**
151  * Add a client to our list of active clients.
152  *
153  * @param cls NULL
154  * @param client client to add
155  * @param mq message queue for @a client
156  * @return internal namestore client structure for this client
157  */
158 static void *
159 client_connect_cb (void *cls,
160                    struct GNUNET_SERVICE_Client *client,
161                    struct GNUNET_MQ_Handle *mq)
162 {
163   return client;
164 }
165
166 /**
167  * Task run during shutdown.
168  *
169  * @param cls unused
170  */
171 static void
172 shutdown_task (void *cls)
173 {
174   struct Ego *e;
175
176   if (NULL != nc)
177   {
178     GNUNET_notification_context_destroy (nc);
179     nc = NULL;
180   }
181   if (NULL != stats)
182   {
183     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
184     stats = NULL;
185   }
186   GNUNET_CONFIGURATION_destroy (subsystem_cfg);
187   subsystem_cfg = NULL;
188   GNUNET_free (subsystem_cfg_file);
189   subsystem_cfg_file = NULL;
190   GNUNET_free (ego_directory);
191   ego_directory = NULL;
192   while (NULL != (e = ego_head))
193   {
194     GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
195     GNUNET_free (e->pk);
196     GNUNET_free (e->identifier);
197     GNUNET_free (e);
198   }
199 }
200
201
202 /**
203  * Send a result code back to the client.
204  *
205  * @param client client that should receive the result code
206  * @param result_code code to transmit
207  * @param emsg error message to include (or NULL for none)
208  */
209 static void
210 send_result_code (struct GNUNET_SERVICE_Client *client,
211                   uint32_t result_code,
212                   const char *emsg)
213 {
214   struct ResultCodeMessage *rcm;
215   struct GNUNET_MQ_Envelope *env;
216   size_t elen;
217
218   if (NULL == emsg)
219     elen = 0;
220   else
221     elen = strlen (emsg) + 1;
222   env = GNUNET_MQ_msg_extra (rcm,
223                              elen,
224                              GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
225   rcm->result_code = htonl (result_code);
226   if (0 < elen)
227     GNUNET_memcpy (&rcm[1], emsg, elen);
228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229               "Sending result %d (%s) to client\n",
230               (int) result_code,
231               emsg);
232   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
233 }
234
235
236 /**
237  * Create an update message with information about the current state of an ego.
238  *
239  * @param ego ego to create message for
240  * @return corresponding update message
241  */
242 static struct GNUNET_MQ_Envelope *
243 create_update_message (struct Ego *ego)
244 {
245   struct UpdateMessage *um;
246   struct GNUNET_MQ_Envelope *env;
247   size_t name_len;
248
249   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
250   env = GNUNET_MQ_msg_extra (um,
251                              name_len,
252                              GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
253   um->name_len = htons (name_len);
254   um->end_of_list = htons (GNUNET_NO);
255   um->private_key = *ego->pk;
256   GNUNET_memcpy (&um[1], ego->identifier, name_len);
257   return env;
258 }
259
260
261 /**
262  * Create a set default message with information about the current state of an ego.
263  *
264  * @param ego ego to create message for
265  * @param servicename name of the service to provide in the message
266  * @return corresponding set default message
267  */
268 static struct GNUNET_MQ_Envelope *
269 create_set_default_message (struct Ego *ego,
270                             const char *servicename)
271 {
272   struct SetDefaultMessage *sdm;
273   struct GNUNET_MQ_Envelope *env;
274   size_t name_len;
275
276   name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
277   env = GNUNET_MQ_msg_extra (sdm,
278                              name_len,
279                              GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
280   sdm->name_len = htons (name_len);
281   sdm->reserved = htons (0);
282   sdm->private_key = *ego->pk;
283   GNUNET_memcpy (&sdm[1], servicename, name_len);
284   return env;
285 }
286
287
288 /**
289  * Handler for START message from client, sends information
290  * about all identities to the client immediately and
291  * adds the client to the notification context for future
292  * updates.
293  *
294  * @param cls unused
295  * @param client who sent the message
296  * @param message the message received
297  */
298 static void
299 handle_start_message (void *cls,
300                       const struct GNUNET_MessageHeader *message)
301 {
302   struct UpdateMessage *ume;
303   struct GNUNET_SERVICE_Client *client = cls;
304   struct GNUNET_MQ_Envelope *env;
305   struct Ego *ego;
306
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308               "Received START message from client\n");
309   GNUNET_SERVICE_client_mark_monitor (client);
310   GNUNET_SERVICE_client_disable_continue_warning (client);
311   GNUNET_notification_context_add (nc,
312                                    GNUNET_SERVICE_client_get_mq(client));
313   for (ego = ego_head; NULL != ego; ego = ego->next)
314   {
315     env = create_update_message (ego);
316     GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(client), env);
317   }
318   env = GNUNET_MQ_msg_extra (ume,
319                              0,
320                              GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
321   ume->end_of_list = htons (GNUNET_YES);
322   ume->name_len = htons (0);
323   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(client), env);
324   GNUNET_SERVICE_client_continue (client);
325 }
326
327 /**
328  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT message
329  *
330  * @param cls client sending the message
331  * @param msg message of type `struct GetDefaultMessage`
332  * @return #GNUNET_OK if @a msg is well-formed
333  */
334 static int
335 check_get_default_message (void *cls,
336                            const struct GetDefaultMessage *msg)
337 {
338   uint16_t size;
339   uint16_t name_len;
340   const char *name;
341
342   size = ntohs (msg->header.size);
343   if (size <= sizeof (struct GetDefaultMessage))
344   {
345     GNUNET_break (0);
346     return GNUNET_SYSERR;
347   }
348   name = (const char *) &msg[1];
349   name_len = ntohs (msg->name_len);
350   if ( (name_len + sizeof (struct GetDefaultMessage) != size) ||
351        (0 != ntohs (msg->reserved)) ||
352        ('\0' != name[name_len - 1]) )
353   {
354     GNUNET_break (0);
355     return GNUNET_SYSERR;
356   }
357   return GNUNET_OK;
358 }
359
360
361 /**
362  * Handler for GET_DEFAULT message from client, returns
363  * default identity for some service.
364  *
365  * @param cls unused
366  * @param client who sent the message
367  * @param message the message received
368  */
369 static void
370 handle_get_default_message (void *cls,
371                             const struct GetDefaultMessage *gdm)
372 {
373   struct GNUNET_MQ_Envelope *env;
374   struct GNUNET_SERVICE_Client *client = cls;
375   struct Ego *ego;
376   char *name;
377   char *identifier;
378
379
380   name = GNUNET_strdup ((const char *) &gdm[1]);
381   GNUNET_STRINGS_utf8_tolower ((const char *) &gdm[1], name);
382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383               "Received GET_DEFAULT for service `%s' from client\n",
384               name);
385   if (GNUNET_OK !=
386       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
387                                              name,
388                                              "DEFAULT_IDENTIFIER",
389                                              &identifier))
390   {
391     send_result_code (client, 1, gettext_noop ("no default known"));
392     GNUNET_SERVICE_client_continue (client);
393     GNUNET_free (name);
394     return;
395   }
396   for (ego = ego_head; NULL != ego; ego = ego->next)
397   {
398     if (0 == strcmp (ego->identifier,
399                      identifier))
400     {
401       env = create_set_default_message (ego,
402                                         name);
403       GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
404       GNUNET_SERVICE_client_continue (client);
405       GNUNET_free (identifier);
406       GNUNET_free (name);
407       return;
408     }
409   }
410   GNUNET_free (identifier);
411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412               "Failed to find ego `%s'\n",
413               name);
414   GNUNET_free (name);
415   send_result_code (client, 1,
416                     gettext_noop ("default configured, but ego unknown (internal error)"));
417   GNUNET_SERVICE_client_continue (client);
418 }
419
420
421 /**
422  * Compare the given two private keys for equality.
423  *
424  * @param pk1 one private key
425  * @param pk2 another private key
426  * @return 0 if the keys are equal
427  */
428 static int
429 key_cmp (const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk1,
430          const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk2)
431 {
432   return memcmp (pk1, pk2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
433 }
434
435 /**
436  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT message
437  *
438  * @param cls client sending the message
439  * @param msg message of type `struct SetDefaultMessage`
440  * @return #GNUNET_OK if @a msg is well-formed
441  */
442 static int
443 check_set_default_message (void *cls,
444                            const struct SetDefaultMessage *msg)
445 {
446   uint16_t size;
447   uint16_t name_len;
448   const char *str;
449
450   size = ntohs (msg->header.size);
451   if (size <= sizeof (struct SetDefaultMessage))
452   {
453     GNUNET_break (0);
454     return GNUNET_SYSERR;
455   }
456   name_len = ntohs (msg->name_len);
457   GNUNET_break (0 == ntohs (msg->reserved));
458   if (name_len + sizeof (struct SetDefaultMessage) != size)
459   {
460     GNUNET_break (0);
461     return GNUNET_SYSERR;
462   }
463   str = (const char *) &msg[1];
464   if ('\0' != str[name_len - 1])
465   {
466     GNUNET_break (0);
467     return GNUNET_SYSERR;
468   }
469   return GNUNET_OK;
470 }
471
472 /**
473  * Handler for SET_DEFAULT message from client, updates
474  * default identity for some service.
475  *
476  * @param cls unused
477  * @param client who sent the message
478  * @param message the message received
479  */
480 static void
481 handle_set_default_message (void *cls,
482                             const struct SetDefaultMessage *sdm)
483 {
484   struct Ego *ego;
485   struct GNUNET_SERVICE_Client *client = cls;
486   char *str;
487
488   str = GNUNET_strdup ((const char *) &sdm[1]);
489   GNUNET_STRINGS_utf8_tolower ((const char *) &sdm[1], str);
490
491   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492               "Received SET_DEFAULT for service `%s' from client\n",
493               str);
494   for (ego = ego_head; NULL != ego; ego = ego->next)
495   {
496     if (0 == key_cmp (ego->pk,
497                       &sdm->private_key))
498     {
499       GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
500                                              str,
501                                              "DEFAULT_IDENTIFIER",
502                                              ego->identifier);
503       if (GNUNET_OK !=
504           GNUNET_CONFIGURATION_write (subsystem_cfg,
505                                       subsystem_cfg_file))
506         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
507                     _("Failed to write subsystem default identifier map to `%s'.\n"),
508                     subsystem_cfg_file);
509       send_result_code (client, 0, NULL);
510       GNUNET_SERVICE_client_continue (client);
511       GNUNET_free (str);
512       return;
513     }
514   }
515   send_result_code (client, 1, _("Unknown ego specified for service (internal error)"));
516   GNUNET_free (str);
517   GNUNET_SERVICE_client_continue (client);
518 }
519
520
521 /**
522  * Send an updated message for the given ego to all listeners.
523  *
524  * @param ego ego to send the update for
525  */
526 static void
527 notify_listeners (struct Ego *ego)
528 {
529   struct UpdateMessage *um;
530   size_t name_len;
531
532   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
533   um = GNUNET_malloc (sizeof (struct UpdateMessage) + name_len);
534   um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
535   um->header.size = htons (sizeof (struct UpdateMessage) + name_len);
536   um->name_len = htons (name_len);
537   um->end_of_list = htons (GNUNET_NO);
538   um->private_key = *ego->pk;
539   GNUNET_memcpy (&um[1], ego->identifier, name_len);
540   GNUNET_notification_context_broadcast (nc,
541                                          &um->header,
542                                          GNUNET_NO);
543   GNUNET_free (um);
544 }
545
546 /**
547  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_CREATE message
548  *
549  * @param cls client sending the message
550  * @param msg message of type `struct CreateRequestMessage`
551  * @return #GNUNET_OK if @a msg is well-formed
552  */
553 static int
554 check_create_message (void *cls,
555                       const struct CreateRequestMessage *msg)
556 {
557   
558   uint16_t size;
559   uint16_t name_len;
560   const char *str;
561
562   size = ntohs (msg->header.size);
563   if (size <= sizeof (struct CreateRequestMessage))
564   {
565     GNUNET_break (0);
566     return GNUNET_SYSERR;
567   }
568   name_len = ntohs (msg->name_len);
569   GNUNET_break (0 == ntohs (msg->reserved));
570   if (name_len + sizeof (struct CreateRequestMessage) != size)
571   {
572     GNUNET_break (0);
573     return GNUNET_SYSERR;
574   }
575   str = (const char *) &msg[1];
576   if ('\0' != str[name_len - 1])
577   {
578     GNUNET_break (0);
579     return GNUNET_SYSERR;
580   }
581   return GNUNET_OK;
582
583
584 /**
585  * Handler for CREATE message from client, creates
586  * new identity.
587  *
588  * @param cls unused
589  * @param client who sent the message
590  * @param message the message received
591  */
592 static void
593 handle_create_message (void *cls,
594                        const struct CreateRequestMessage *crm)
595 {
596   struct GNUNET_SERVICE_Client *client = cls;
597   struct Ego *ego;
598   char *str;
599   char *fn;
600
601   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602               "Received CREATE message from client\n");
603   str = GNUNET_strdup ((const char *) &crm[1]);
604   GNUNET_STRINGS_utf8_tolower ((const char *) &crm[1], str);
605   for (ego = ego_head; NULL != ego; ego = ego->next)
606   {
607     if (0 == strcmp (ego->identifier,
608                      str))
609     {
610       send_result_code (client, 1, gettext_noop ("identifier already in use for another ego"));
611       GNUNET_SERVICE_client_continue (client);
612       GNUNET_free (str);
613       return;
614     }
615   }
616   ego = GNUNET_new (struct Ego);
617   ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
618   *ego->pk = crm->private_key;
619   ego->identifier = GNUNET_strdup (str);
620   GNUNET_CONTAINER_DLL_insert (ego_head,
621                                ego_tail,
622                                ego);
623   send_result_code (client, 0, NULL);
624   fn = get_ego_filename (ego);
625   (void) GNUNET_DISK_directory_create_for_file (fn);
626   if (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey) !=
627       GNUNET_DISK_fn_write (fn,
628                             &crm->private_key,
629                             sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
630                             GNUNET_DISK_PERM_USER_READ |
631                             GNUNET_DISK_PERM_USER_WRITE))
632     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
633                               "write", fn);
634   GNUNET_free (fn);
635   GNUNET_free (str);
636   notify_listeners (ego);
637   GNUNET_SERVICE_client_continue (client);
638 }
639
640
641 /**
642  * Closure for 'handle_ego_rename'.
643  */
644 struct RenameContext
645 {
646   /**
647    * Old name.
648    */
649   const char *old_name;
650
651   /**
652    * New name.
653    */
654   const char *new_name;
655 };
656
657 /**
658  * An ego was renamed; rename it in all subsystems where it is
659  * currently set as the default.
660  *
661  * @param cls the 'struct RenameContext'
662  * @param section a section in the configuration to process
663  */
664 static void
665 handle_ego_rename (void *cls,
666                    const char *section)
667 {
668   struct RenameContext *rc = cls;
669   char *id;
670
671   if (GNUNET_OK !=
672       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
673                                              section,
674                                              "DEFAULT_IDENTIFIER",
675                                              &id))
676     return;
677   if (0 != strcmp (id, rc->old_name))
678   {
679     GNUNET_free (id);
680     return;
681   }
682   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
683                                          section,
684                                          "DEFAULT_IDENTIFIER",
685                                          rc->new_name);
686   GNUNET_free (id);
687 }
688
689 /**
690  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_RENAME message
691  *
692  * @param cls client sending the message
693  * @param msg message of type `struct RenameMessage`
694  * @return #GNUNET_OK if @a msg is well-formed
695  */
696 static int
697 check_rename_message (void *cls,
698                       const struct RenameMessage *msg)
699 {
700   uint16_t size;
701   uint16_t old_name_len;
702   uint16_t new_name_len;
703   const char *old_name;
704   const char *new_name;
705
706   size = ntohs (msg->header.size);
707   if (size <= sizeof (struct RenameMessage))
708   {
709     GNUNET_break (0);
710     return GNUNET_SYSERR;
711   }
712   old_name_len = ntohs (msg->old_name_len);
713   new_name_len = ntohs (msg->new_name_len);
714   old_name = (const char *) &msg[1];
715   new_name = &old_name[old_name_len];
716   if ( (old_name_len + new_name_len + sizeof (struct RenameMessage) != size) ||
717        ('\0' != old_name[old_name_len - 1]) ||
718        ('\0' != new_name[new_name_len - 1]) )
719   {
720     GNUNET_break (0);
721     return GNUNET_SYSERR;
722   }
723
724   return GNUNET_OK;
725 }
726  
727
728 /**
729  * Handler for RENAME message from client, creates
730  * new identity.
731  *
732  * @param cls unused
733  * @param client who sent the message
734  * @param message the message received
735  */
736 static void
737 handle_rename_message (void *cls,
738                        const struct RenameMessage *rm)
739 {
740   uint16_t old_name_len;
741   struct Ego *ego;
742   char *old_name;
743   char *new_name;
744   struct RenameContext rename_ctx;
745   struct GNUNET_SERVICE_Client *client = cls;
746   char *fn_old;
747   char *fn_new;
748   const char *old_name_tmp;
749
750   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751               "Received RENAME message from client\n");
752   old_name_len = ntohs (rm->old_name_len);
753   old_name_tmp = (const char *) &rm[1];
754   old_name = GNUNET_strdup (old_name_tmp);
755   GNUNET_STRINGS_utf8_tolower (old_name_tmp, old_name);
756   new_name = GNUNET_strdup (&old_name_tmp[old_name_len]);
757   GNUNET_STRINGS_utf8_tolower (&old_name_tmp[old_name_len], new_name);
758
759   /* check if new name is already in use */
760   for (ego = ego_head; NULL != ego; ego = ego->next)
761   {
762     if (0 == strcmp (ego->identifier,
763                      new_name))
764     {
765       send_result_code (client, 1, gettext_noop ("target name already exists"));
766       GNUNET_SERVICE_client_continue (client);
767       GNUNET_free (old_name);
768       GNUNET_free (new_name);
769       return;
770     }
771   }
772
773   /* locate old name and, if found, perform rename */
774   for (ego = ego_head; NULL != ego; ego = ego->next)
775   {
776     if (0 == strcmp (ego->identifier,
777                      old_name))
778     {
779       fn_old = get_ego_filename (ego);
780       GNUNET_free (ego->identifier);
781       rename_ctx.old_name = old_name;
782       rename_ctx.new_name = new_name;
783       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
784                                              &handle_ego_rename,
785                                              &rename_ctx);
786       if (GNUNET_OK !=
787           GNUNET_CONFIGURATION_write (subsystem_cfg,
788                                       subsystem_cfg_file))
789         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
790                     _("Failed to write subsystem default identifier map to `%s'.\n"),
791                     subsystem_cfg_file);
792       ego->identifier = GNUNET_strdup (new_name);
793       fn_new = get_ego_filename (ego);
794       if (0 != RENAME (fn_old, fn_new))
795         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rename", fn_old);
796       GNUNET_free (fn_old);
797       GNUNET_free (fn_new);
798       GNUNET_free (old_name);
799       GNUNET_free (new_name);
800       notify_listeners (ego);
801       send_result_code (client, 0, NULL);
802       GNUNET_SERVICE_client_continue (client);
803       return;
804     }
805   }
806
807   /* failed to locate old name */
808   send_result_code (client, 1, gettext_noop ("no matching ego found"));
809   GNUNET_free (old_name);
810   GNUNET_free (new_name);
811   GNUNET_SERVICE_client_continue (client);
812 }
813
814
815 /**
816  * An ego was removed, remove it from all subsystems where it is
817  * currently set as the default.
818  *
819  * @param cls name of the removed ego (const char *)
820  * @param section a section in the configuration to process
821  */
822 static void
823 handle_ego_delete (void *cls,
824                    const char *section)
825 {
826   const char *identifier = cls;
827   char *id;
828
829   if (GNUNET_OK !=
830       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
831                                              section,
832                                              "DEFAULT_IDENTIFIER",
833                                              &id))
834     return;
835   if (0 != strcmp (id, identifier))
836   {
837     GNUNET_free (id);
838     return;
839   }
840   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
841                                          section,
842                                          "DEFAULT_IDENTIFIER",
843                                          NULL);
844   GNUNET_free (id);
845 }
846
847 /**
848  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_DELETE message
849  *
850  * @param cls client sending the message
851  * @param msg message of type `struct DeleteMessage`
852  * @return #GNUNET_OK if @a msg is well-formed
853  */
854 static int
855 check_delete_message (void *cls,
856                       const struct DeleteMessage *msg)
857 {
858   uint16_t size;
859   uint16_t name_len;
860   const char *name;
861
862   size = ntohs (msg->header.size);
863   if (size <= sizeof (struct DeleteMessage))
864   {
865     GNUNET_break (0);
866     return GNUNET_SYSERR;
867   }
868   name = (const char *) &msg[1];
869   name_len = ntohs (msg->name_len);
870   if ( (name_len + sizeof (struct DeleteMessage) != size) ||
871        (0 != ntohs (msg->reserved)) ||
872        ('\0' != name[name_len - 1]) )
873   {
874     GNUNET_break (0);
875     return GNUNET_SYSERR;
876   }
877   return GNUNET_OK;
878 }
879
880
881 /**
882  * Handler for DELETE message from client, creates
883  * new identity.
884  *
885  * @param cls unused
886  * @param client who sent the message
887  * @param message the message received
888  */
889 static void
890 handle_delete_message (void *cls,
891                        const struct DeleteMessage *dm)
892 {
893   struct Ego *ego;
894   char *name;
895   char *fn;
896   struct GNUNET_SERVICE_Client *client = cls;
897
898   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899               "Received DELETE message from client\n");
900   name = GNUNET_strdup ((const char *) &dm[1]);
901   GNUNET_STRINGS_utf8_tolower ((const char *) &dm[1], name);
902
903   for (ego = ego_head; NULL != ego; ego = ego->next)
904   {
905     if (0 == strcmp (ego->identifier,
906                      name))
907     {
908       GNUNET_CONTAINER_DLL_remove (ego_head,
909                                    ego_tail,
910                                    ego);
911       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
912                                              &handle_ego_delete,
913                                              ego->identifier);
914       if (GNUNET_OK !=
915           GNUNET_CONFIGURATION_write (subsystem_cfg,
916                                       subsystem_cfg_file))
917         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
918                     _("Failed to write subsystem default identifier map to `%s'.\n"),
919                     subsystem_cfg_file);
920       fn = get_ego_filename (ego);
921       if (0 != UNLINK (fn))
922         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
923       GNUNET_free (fn);
924       GNUNET_free (ego->identifier);
925       ego->identifier = NULL;
926       notify_listeners (ego);
927       GNUNET_free (ego->pk);
928       GNUNET_free (ego);
929       GNUNET_free (name);
930       send_result_code (client, 0, NULL);
931       GNUNET_SERVICE_client_continue (client);
932       return;
933     }
934   }
935
936   send_result_code (client, 1, gettext_noop ("no matching ego found"));
937   GNUNET_free (name);
938   GNUNET_SERVICE_client_continue (client);
939 }
940
941
942 /**
943  * Process the given file from the "EGODIR".  Parses the file
944  * and creates the respective 'struct Ego' in memory.
945  *
946  * @param cls NULL
947  * @param filename name of the file to parse
948  * @return #GNUNET_OK to continue to iterate,
949  *  #GNUNET_NO to stop iteration with no error,
950  *  #GNUNET_SYSERR to abort iteration with error!
951  */
952 static int
953 process_ego_file (void *cls,
954                   const char *filename)
955 {
956   struct Ego *ego;
957   const char *fn;
958
959   fn = strrchr (filename, (int) DIR_SEPARATOR);
960   if (NULL == fn)
961   {
962     GNUNET_break (0);
963     return GNUNET_OK;
964   }
965   ego = GNUNET_new (struct Ego);
966   ego->pk = GNUNET_CRYPTO_ecdsa_key_create_from_file (filename);
967   if (NULL == ego->pk)
968   {
969     GNUNET_free (ego);
970     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
971                 _("Failed to parse ego information in `%s'\n"),
972                 filename);
973     return GNUNET_OK;
974   }
975   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976               "Loaded ego `%s'\n",
977               fn + 1);
978   ego->identifier = GNUNET_strdup (fn + 1);
979   GNUNET_CONTAINER_DLL_insert (ego_head,
980                                ego_tail,
981                                ego);
982   return GNUNET_OK;
983 }
984
985
986 /**
987  * Handle network size estimate clients.
988  *
989  * @param cls closure
990  * @param server the initialized server
991  * @param c configuration to use
992  */
993 static void
994 run (void *cls,
995      const struct GNUNET_CONFIGURATION_Handle *c,
996      struct GNUNET_SERVICE_Handle *service)
997 {
998   cfg = c;
999   nc = GNUNET_notification_context_create (1);
1000   if (GNUNET_OK !=
1001       GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
1002                                                "EGODIR",
1003                                                &ego_directory))
1004   {
1005     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
1006     GNUNET_SCHEDULER_shutdown ();
1007     return;
1008   }
1009   if (GNUNET_OK !=
1010       GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
1011                                                "SUBSYSTEM_CFG",
1012                                                &subsystem_cfg_file))
1013   {
1014     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "SUBSYSTEM_CFG");
1015     GNUNET_SCHEDULER_shutdown ();
1016     return;
1017   }
1018   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019               "Loading subsystem configuration `%s'\n",
1020               subsystem_cfg_file);
1021   subsystem_cfg = GNUNET_CONFIGURATION_create ();
1022   if ( (GNUNET_YES ==
1023         GNUNET_DISK_file_test (subsystem_cfg_file)) &&
1024        (GNUNET_OK !=
1025         GNUNET_CONFIGURATION_parse (subsystem_cfg,
1026                                     subsystem_cfg_file)) )
1027   {
1028     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1029                 _("Failed to parse subsystem identity configuration file `%s'\n"),
1030                 subsystem_cfg_file);
1031     GNUNET_SCHEDULER_shutdown ();
1032     return;
1033   }
1034   stats = GNUNET_STATISTICS_create ("identity", cfg);
1035   if (GNUNET_OK !=
1036       GNUNET_DISK_directory_create (ego_directory))
1037   {
1038     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1039                 _("Failed to create directory `%s' for storing egos\n"),
1040                 ego_directory);
1041   }
1042   GNUNET_DISK_directory_scan (ego_directory,
1043                               &process_ego_file,
1044                               NULL);
1045   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1046                                  NULL);
1047 }
1048
1049
1050 /**
1051  * Define "main" method using service macro.
1052  */
1053 GNUNET_SERVICE_MAIN
1054 ("identity",
1055  GNUNET_SERVICE_OPTION_NONE,
1056  &run,
1057  &client_connect_cb,
1058  &client_disconnect_cb,
1059  NULL,
1060  GNUNET_MQ_hd_fixed_size (start_message,
1061                           GNUNET_MESSAGE_TYPE_IDENTITY_START,
1062                           struct GNUNET_MessageHeader,
1063                           NULL),
1064  GNUNET_MQ_hd_var_size (get_default_message,
1065                         GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT,
1066                         struct GetDefaultMessage,
1067                         NULL),
1068  GNUNET_MQ_hd_var_size (set_default_message,
1069                         GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT,
1070                         struct SetDefaultMessage,
1071                         NULL),
1072  GNUNET_MQ_hd_var_size (create_message,
1073                         GNUNET_MESSAGE_TYPE_IDENTITY_CREATE,
1074                         struct CreateRequestMessage,
1075                         NULL),
1076  GNUNET_MQ_hd_var_size (rename_message,
1077                         GNUNET_MESSAGE_TYPE_IDENTITY_RENAME,
1078                         struct RenameMessage,
1079                         NULL),
1080  GNUNET_MQ_hd_var_size (delete_message,
1081                         GNUNET_MESSAGE_TYPE_IDENTITY_DELETE,
1082                         struct DeleteMessage,
1083                         NULL),
1084  GNUNET_MQ_handler_end());
1085
1086
1087
1088 /* end of gnunet-service-identity.c */