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