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