84da12c2a3b44db2e7b40da06d8cecf679034171
[oweals/gnunet.git] / src / identity / gnunet-service-identity.c
1 /*
2   This file is part of GNUnet.
3   (C) 2013 Christian Grothoff (and other contributing authors)
4
5   GNUnet is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published
7   by the Free Software Foundation; either version 3, or (at your
8   option) any later version.
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   General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with GNUnet; see the file COPYING.  If not, write to the
17   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18   Boston, MA 02111-1307, USA.
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  * - testcases
31  * - auto-initialze default egos; maybe trigger default
32  *   initializations (such as gnunet-gns-import.sh?)
33  */
34 #include "platform.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_constants.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_identity_service.h"
40 #include "identity.h"
41
42
43 /**
44  * Information we keep about each ego.
45  */
46 struct Ego
47 {
48
49   /**
50    * We keep egos in a DLL.
51    */ 
52   struct Ego *next;
53
54   /**
55    * We keep egos in a DLL.
56    */ 
57   struct Ego *prev;
58
59   /**
60    * Private key of the ego.
61    */
62   struct GNUNET_CRYPTO_EccPrivateKey *pk;
63
64   /**
65    * String identifier for the ego.
66    */
67   char *identifier;
68
69 };
70
71
72 /**
73  * Handle to our current configuration.
74  */
75 static const struct GNUNET_CONFIGURATION_Handle *cfg;
76
77 /**
78  * Handle to subsystem configuration which for each subsystem contains
79  * the name of the default ego.
80  */
81 static struct GNUNET_CONFIGURATION_Handle *subsystem_cfg;
82
83 /**
84  * Handle to the statistics service.
85  */
86 static struct GNUNET_STATISTICS_Handle *stats;
87
88 /**
89  * Notification context, simplifies client broadcasts.
90  */
91 static struct GNUNET_SERVER_NotificationContext *nc;
92
93 /**
94  * Directory where we store the identities.
95  */
96 static char *ego_directory;
97
98 /**
99  * Configuration file name where subsystem information is kept.
100  */
101 static char *subsystem_cfg_file;
102
103 /**
104  * Head of DLL of all egos.
105  */
106 static struct Ego *ego_head;
107
108 /**
109  * Tail of DLL of all egos.
110  */
111 static struct Ego *ego_tail;
112
113
114 /**
115  * Get the name of the file we use to store a given ego.
116  *
117  * @param ego ego for which we need the filename
118  * @return full filename for the given ego
119  */
120 static char *
121 get_ego_filename (struct Ego *ego)
122 {
123   char *filename;
124
125   GNUNET_asprintf (&filename,
126                    "%s%s%s",
127                    ego_directory,
128                    DIR_SEPARATOR_STR,
129                    ego->identifier);
130   return filename;
131 }
132
133
134 /**
135  * Task run during shutdown.
136  *
137  * @param cls unused
138  * @param tc unused
139  */
140 static void
141 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 {
143   struct Ego *e;
144
145   if (NULL != nc)
146   {
147     GNUNET_SERVER_notification_context_destroy (nc);
148     nc = NULL;
149   }
150   if (NULL != stats)
151   {
152     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
153     stats = NULL;
154   }
155   GNUNET_CONFIGURATION_destroy (subsystem_cfg);
156   subsystem_cfg = NULL;
157   GNUNET_free (subsystem_cfg_file);
158   subsystem_cfg_file = NULL;
159   GNUNET_free (ego_directory);
160   ego_directory = NULL;
161   while (NULL != (e = ego_head))
162   {
163     GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
164     GNUNET_CRYPTO_ecc_key_free (e->pk);
165     GNUNET_free (e);
166   }
167 }
168
169
170 /**
171  * Send a result code back to the client.
172  *
173  * @param client client that should receive the result code
174  * @param result_code code to transmit
175  * @param emsg error message to include (or NULL for none)
176  */
177 static void
178 send_result_code (struct GNUNET_SERVER_Client *client,
179                   uint32_t result_code,
180                   const char *emsg)
181 {
182   struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
183   size_t elen;
184
185   if (NULL == emsg)
186     elen = 0;
187   else
188     elen = strlen (emsg) + 1;
189   rcm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
190   rcm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
191   rcm->header.size = htons (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
192   rcm->result_code = htonl (result_code);
193   memcpy (&rcm[1], emsg, elen);
194   GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_YES);  
195   GNUNET_free (rcm);
196 }
197
198
199 /**
200  * Create an update message with information about the current state of an ego.
201  *
202  * @param ego ego to create message for
203  * @return corresponding update message
204  */
205 static struct GNUNET_IDENTITY_UpdateMessage *
206 create_update_message (struct Ego *ego)
207 {
208   struct GNUNET_IDENTITY_UpdateMessage *um;
209   char *str;
210   uint16_t pk_len;
211   size_t name_len;
212   struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
213
214   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
215   enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
216   pk_len = ntohs (enc->size);
217   um = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
218   um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
219   um->header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
220   um->name_len = htons (name_len);
221   um->pk_len = htons (pk_len);
222   str = (char *) &um[1];
223   memcpy (str, enc, pk_len);
224   memcpy (&str[pk_len], ego->identifier, name_len);
225   GNUNET_free (enc);
226   return um;
227 }
228
229
230 /**
231  * Create a set default message with information about the current state of an ego.
232  *
233  * @param ego ego to create message for
234  * @param servicename name of the service to provide in the message
235  * @return corresponding set default message
236  */
237 static struct GNUNET_IDENTITY_SetDefaultMessage *
238 create_set_default_message (struct Ego *ego,
239                             const char *servicename)
240 {
241   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
242   char *str;
243   uint16_t pk_len;
244   size_t name_len;
245   struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
246
247   name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
248   enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
249   pk_len = ntohs (enc->size);
250   sdm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
251   sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
252   sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
253   sdm->name_len = htons (name_len);
254   sdm->pk_len = htons (pk_len);
255   str = (char *) &sdm[1];
256   memcpy (str, enc, pk_len);
257   memcpy (&str[pk_len], servicename, name_len);
258   GNUNET_free (enc);
259   return sdm;
260 }
261
262
263 /**
264  * Handler for START message from client, sends information
265  * about all identities to the client immediately and 
266  * adds the client to the notification context for future
267  * updates.
268  *
269  * @param cls unused
270  * @param client who sent the message
271  * @param message the message received
272  */
273 static void
274 handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
275                       const struct GNUNET_MessageHeader *message)
276 {
277   struct GNUNET_IDENTITY_UpdateMessage *um;
278   struct GNUNET_IDENTITY_UpdateMessage ume;
279   struct Ego *ego;
280
281   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
282               "Received START message from client\n");
283   GNUNET_SERVER_notification_context_add (nc, client);
284   for (ego = ego_head; NULL != ego; ego = ego->next)
285   {
286     um = create_update_message (ego);
287     GNUNET_SERVER_notification_context_unicast (nc, client, &um->header, GNUNET_YES);
288     GNUNET_free (um);
289   }
290   ume.header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
291   ume.header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage));
292   ume.pk_len = htons (0);
293   ume.name_len = htons (0);
294   GNUNET_SERVER_notification_context_unicast (nc, client, &ume.header, GNUNET_YES);  
295   GNUNET_SERVER_receive_done (client, GNUNET_OK);
296 }
297
298
299 /**
300  * Handler for GET_DEFAULT message from client, returns
301  * default identity for some service.
302  *
303  * @param cls unused
304  * @param client who sent the message
305  * @param message the message received
306  */
307 static void
308 handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
309                             const struct GNUNET_MessageHeader *message)
310 {
311   const struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
312   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
313   uint16_t size;
314   uint16_t name_len;
315   struct Ego *ego;
316   const char *name;
317   char *identifier;
318
319   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
320               "Received GET_DEFAULT message from client\n");
321   size = ntohs (message->size);
322   if (size <= sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
323   {
324     GNUNET_break (0);
325     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
326     return;
327   }
328   gdm = (const struct GNUNET_IDENTITY_GetDefaultMessage *) message;
329   name = (const char *) &gdm[1];
330   name_len = ntohs (gdm->name_len);
331   if ( (name_len + sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) != size) ||
332        (0 != ntohs (gdm->reserved)) ||
333        ('\0' != name[name_len - 1]) )
334   {
335     GNUNET_break (0);
336     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
337     return;
338   }
339   if (GNUNET_OK !=
340       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
341                                              name,
342                                              "DEFAULT_IDENTIFIER",
343                                              &identifier))
344   {
345     send_result_code (client, 1, gettext_noop ("no default known"));
346     GNUNET_SERVER_receive_done (client, GNUNET_OK);   
347     return;
348   }
349   for (ego = ego_head; NULL != ego; ego = ego->next)
350   {
351     if (0 == strcmp (ego->identifier,
352                      identifier))
353     {
354       sdm = create_set_default_message (ego,
355                                         name);
356       GNUNET_SERVER_notification_context_broadcast (nc, &sdm->header, GNUNET_YES);
357       GNUNET_free (sdm);
358       GNUNET_SERVER_receive_done (client, GNUNET_OK);
359       return;
360     }
361   }
362   send_result_code (client, 1, 
363                     gettext_noop ("default configured, but ego unknown (internal error)"));
364   GNUNET_SERVER_receive_done (client, GNUNET_OK);
365 }
366
367
368 /**
369  * Compare the given two private keys for equality.
370  * 
371  * @param pk1 one private key
372  * @param pk2 another private key
373  * @return 0 if the keys are equal
374  */
375 static int
376 key_cmp (const struct GNUNET_CRYPTO_EccPrivateKey *pk1,
377          const struct GNUNET_CRYPTO_EccPrivateKey *pk2)
378 {
379   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p1;
380   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p2;
381   
382   GNUNET_CRYPTO_ecc_key_get_public (pk1, &p1);
383   GNUNET_CRYPTO_ecc_key_get_public (pk2, &p2);
384   return memcmp (&p1, &p2, sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
385 }
386
387
388 /**
389  * Handler for SET_DEFAULT message from client, updates
390  * default identity for some service.
391  *
392  * @param cls unused
393  * @param client who sent the message
394  * @param message the message received
395  */
396 static void
397 handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
398                             const struct GNUNET_MessageHeader *message)
399 {
400   const struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
401   uint16_t size;
402   uint16_t name_len;
403   uint16_t pk_len;
404   struct Ego *ego;
405   const char *str;
406   struct GNUNET_CRYPTO_EccPrivateKey *pk;
407
408   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
409               "Received SET_DEFAULT message from client\n");
410   size = ntohs (message->size);
411   if (size <= sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
412   {
413     GNUNET_break (0);
414     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
415     return;
416   }
417   sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) message;
418   name_len = ntohs (sdm->name_len);
419   pk_len = ntohs (sdm->pk_len);
420   str = (const char *) &sdm[1];
421   if ( (name_len + pk_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) != size) ||
422        (NULL == (pk = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES))) )
423   {
424     GNUNET_break (0);
425     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
426     return;
427   }
428   str = &str[pk_len];
429   if ('\0' != str[name_len - 1])
430   {
431     GNUNET_break (0);
432     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
433     return;
434   }
435   for (ego = ego_head; NULL != ego; ego = ego->next)
436   {
437     if (0 == key_cmp (ego->pk,
438                       pk))
439     {
440       GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
441                                              str,
442                                              "DEFAULT_IDENTIFIER",
443                                              ego->identifier);
444       if (GNUNET_OK != 
445           GNUNET_CONFIGURATION_write (subsystem_cfg,
446                                       subsystem_cfg_file))
447         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
448                     _("Failed to write subsystem default identifier map to `%s'.\n"),
449                     subsystem_cfg_file);
450       send_result_code (client, 0, NULL);
451       GNUNET_SERVER_receive_done (client, GNUNET_OK);
452       GNUNET_CRYPTO_ecc_key_free (pk);
453       return;
454     }
455   }  
456   send_result_code (client, 1, _("Unknown ego specified for service (internal error)"));
457   GNUNET_SERVER_receive_done (client, GNUNET_OK);
458   GNUNET_CRYPTO_ecc_key_free (pk);
459 }
460
461
462 /**
463  * Send an updated message for the given ego to all listeners.
464  *
465  * @param ego ego to send the update for
466  */
467 static void
468 notify_listeners (struct Ego *ego)
469 {
470   struct GNUNET_IDENTITY_UpdateMessage *um;
471
472   um = create_update_message (ego);
473   GNUNET_SERVER_notification_context_broadcast (nc, &um->header, GNUNET_YES);
474   GNUNET_free (um);
475 }
476
477
478 /**
479  * Handler for CREATE message from client, creates
480  * new identity.
481  *
482  * @param cls unused
483  * @param client who sent the message
484  * @param message the message received
485  */
486 static void
487 handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
488                        const struct GNUNET_MessageHeader *message)
489 {
490   const struct GNUNET_IDENTITY_CreateRequestMessage *crm;
491   uint16_t size;
492   uint16_t name_len;
493   uint16_t pk_len;
494   struct Ego *ego;
495   const char *pks;
496   const char *str;
497   struct GNUNET_CRYPTO_EccPrivateKey *pk;
498   char *fn;
499
500   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
501               "Received CREATE message from client\n");
502   size = ntohs (message->size);
503   if (size <= sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
504   {
505     GNUNET_break (0);
506     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
507     return;
508   }
509   crm = (const struct GNUNET_IDENTITY_CreateRequestMessage *) message;
510   name_len = ntohs (crm->name_len);
511   pk_len = ntohs (crm->pk_len);
512   pks = (const char *) &crm[1];
513   if ( (name_len + pk_len + sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) != size) ||
514        (NULL == (pk = GNUNET_CRYPTO_ecc_decode_key (pks, pk_len, GNUNET_YES))) )
515   {
516     GNUNET_break (0);
517     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
518     return;
519   }
520   str = &pks[pk_len];
521   if ('\0' != str[name_len - 1])
522   {
523     GNUNET_break (0);
524     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
525     return;
526   }
527   for (ego = ego_head; NULL != ego; ego = ego->next)
528   {
529     if (0 == strcmp (ego->identifier,
530                      str))
531     {
532       send_result_code (client, 1, gettext_noop ("identifier already in use for another ego"));
533       GNUNET_SERVER_receive_done (client, GNUNET_OK);
534       GNUNET_CRYPTO_ecc_key_free (pk);
535       return;
536     }
537   }
538   ego = GNUNET_new (struct Ego);
539   ego->pk = pk;
540   ego->identifier = GNUNET_strdup (str);
541   GNUNET_CONTAINER_DLL_insert (ego_head,
542                                ego_tail,
543                                ego);
544   send_result_code (client, 0, NULL);
545   fn = get_ego_filename (ego);
546   (void) GNUNET_DISK_directory_create_for_file (fn);
547   if (pk_len !=
548       GNUNET_DISK_fn_write (fn, pks, pk_len,
549                             GNUNET_DISK_PERM_USER_READ |
550                             GNUNET_DISK_PERM_USER_WRITE))
551     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
552                               "write", fn);
553   GNUNET_free (fn);
554   notify_listeners (ego);  
555   GNUNET_SERVER_receive_done (client, GNUNET_OK);
556 }
557
558
559 /**
560  * Closure for 'handle_ego_rename'.
561  */
562 struct RenameContext 
563 {
564   /**
565    * Old name.
566    */
567   const char *old_name;
568
569   /**
570    * New name.
571    */
572   const char *new_name;
573 };
574
575
576 /**
577  * An ego was renamed; rename it in all subsystems where it is
578  * currently set as the default.
579  *
580  * @param cls the 'struct RenameContext'
581  * @param section a section in the configuration to process
582  */
583 static void
584 handle_ego_rename (void *cls,
585                    const char *section)
586 {
587   struct RenameContext *rc = cls;
588   char *id;
589
590   if (GNUNET_OK !=
591       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
592                                              section,
593                                              "DEFAULT_IDENTIFIER",
594                                              &id))
595     return;
596   if (0 != strcmp (id, rc->old_name))
597   {
598     GNUNET_free (id);  
599     return;
600   }
601   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
602                                          section,
603                                          "DEFAULT_IDENTIFIER",
604                                          rc->new_name);
605   GNUNET_free (id);  
606 }
607
608
609 /**
610  * Handler for RENAME message from client, creates
611  * new identity.
612  *
613  * @param cls unused
614  * @param client who sent the message
615  * @param message the message received
616  */
617 static void
618 handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
619                        const struct GNUNET_MessageHeader *message)
620 {
621   const struct GNUNET_IDENTITY_RenameMessage *rm;
622   uint16_t size;
623   uint16_t old_name_len;
624   uint16_t new_name_len;
625   struct Ego *ego;
626   const char *old_name;
627   const char *new_name;
628   struct RenameContext rename_ctx;
629   char *fn_old;
630   char *fn_new;
631
632   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
633               "Received RENAME message from client\n");
634   size = ntohs (message->size);
635   if (size <= sizeof (struct GNUNET_IDENTITY_RenameMessage))
636   {
637     GNUNET_break (0);
638     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
639     return;
640   }
641   rm = (const struct GNUNET_IDENTITY_RenameMessage *) message;
642   old_name_len = ntohs (rm->old_name_len);
643   new_name_len = ntohs (rm->new_name_len);
644   old_name = (const char *) &rm[1];
645   new_name = &old_name[old_name_len];
646   if ( (old_name_len + new_name_len + sizeof (struct GNUNET_IDENTITY_RenameMessage) != size) ||
647        ('\0' != old_name[old_name_len - 1]) ||
648        ('\0' != new_name[new_name_len - 1]) )
649   {
650     GNUNET_break (0);
651     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
652     return;
653   }
654
655   /* check if new name is already in use */
656   for (ego = ego_head; NULL != ego; ego = ego->next)
657   {
658     if (0 == strcmp (ego->identifier,
659                      new_name))
660     {
661       send_result_code (client, 1, gettext_noop ("target name already exists"));
662       GNUNET_SERVER_receive_done (client, GNUNET_OK);      
663       return;
664     }
665   }
666
667   /* locate old name and, if found, perform rename */
668   for (ego = ego_head; NULL != ego; ego = ego->next)
669   {
670     if (0 == strcmp (ego->identifier,
671                      old_name))
672     {
673       fn_old = get_ego_filename (ego);
674       GNUNET_free (ego->identifier);
675       rename_ctx.old_name = old_name;
676       rename_ctx.new_name = new_name;
677       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
678                                              &handle_ego_rename,
679                                              &rename_ctx);
680       if (GNUNET_OK != 
681           GNUNET_CONFIGURATION_write (subsystem_cfg,
682                                       subsystem_cfg_file))
683         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684                     _("Failed to write subsystem default identifier map to `%s'.\n"),
685                     subsystem_cfg_file);
686       ego->identifier = GNUNET_strdup (new_name);
687       fn_new = get_ego_filename (ego);      
688       if (0 != RENAME (fn_old, fn_new))
689         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rename", fn_old);
690       GNUNET_free (fn_old);
691       GNUNET_free (fn_new);
692       notify_listeners (ego);
693       send_result_code (client, 0, NULL);
694       GNUNET_SERVER_receive_done (client, GNUNET_OK);
695       return;
696     }
697   }
698
699   /* failed to locate old name */
700   send_result_code (client, 1, gettext_noop ("no matching ego found"));
701   GNUNET_SERVER_receive_done (client, GNUNET_OK);
702 }
703
704
705 /**
706  * An ego was removed, remove it from all subsystems where it is
707  * currently set as the default.
708  *
709  * @param cls name of the removed ego (const char *)
710  * @param section a section in the configuration to process
711  */
712 static void
713 handle_ego_delete (void *cls,
714                    const char *section)
715 {
716   const char *identifier = cls;
717   char *id;
718
719   if (GNUNET_OK !=
720       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
721                                              section,
722                                              "DEFAULT_IDENTIFIER",
723                                              &id))
724     return;
725   if (0 != strcmp (id, identifier))
726   {
727     GNUNET_free (id);  
728     return;
729   }
730   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
731                                          section,
732                                          "DEFAULT_IDENTIFIER",
733                                          NULL);
734   GNUNET_free (id);  
735 }
736
737
738 /**
739  * Handler for DELETE message from client, creates
740  * new identity.
741  *
742  * @param cls unused
743  * @param client who sent the message
744  * @param message the message received
745  */
746 static void
747 handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
748                        const struct GNUNET_MessageHeader *message)
749 {
750   const struct GNUNET_IDENTITY_DeleteMessage *dm;
751   uint16_t size;
752   uint16_t name_len;
753   struct Ego *ego;
754   const char *name;
755   char *fn;
756
757   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
758               "Received DELETE message from client\n");
759   size = ntohs (message->size);
760   if (size <= sizeof (struct GNUNET_IDENTITY_DeleteMessage))
761   {
762     GNUNET_break (0);
763     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
764     return;
765   }
766   dm = (const struct GNUNET_IDENTITY_DeleteMessage *) message;
767   name = (const char *) &dm[1];
768   name_len = ntohs (dm->name_len);
769   if ( (name_len + sizeof (struct GNUNET_IDENTITY_DeleteMessage) != size) ||
770        (0 != ntohs (dm->reserved)) ||
771        ('\0' != name[name_len - 1]) )
772   {
773     GNUNET_break (0);
774     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
775     return;
776   }
777   for (ego = ego_head; NULL != ego; ego = ego->next)
778   {
779     if (0 == strcmp (ego->identifier,
780                      name))
781     {
782       GNUNET_CONTAINER_DLL_remove (ego_head,
783                                    ego_tail,
784                                    ego);
785       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
786                                              &handle_ego_delete,
787                                              ego->identifier);
788       if (GNUNET_OK != 
789           GNUNET_CONFIGURATION_write (subsystem_cfg,
790                                       subsystem_cfg_file))
791         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
792                     _("Failed to write subsystem default identifier map to `%s'.\n"),
793                     subsystem_cfg_file);
794       fn = get_ego_filename (ego);
795       if (0 != UNLINK (fn))
796         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
797       GNUNET_free (fn);
798       GNUNET_free (ego->identifier);
799       ego->identifier = NULL;
800       notify_listeners (ego);
801       GNUNET_CRYPTO_ecc_key_free (ego->pk);
802       GNUNET_free (ego);
803       send_result_code (client, 0, NULL);
804       GNUNET_SERVER_receive_done (client, GNUNET_OK);
805       return;
806     }
807   }
808
809   send_result_code (client, 1, gettext_noop ("no matching ego found"));
810   GNUNET_SERVER_receive_done (client, GNUNET_OK);
811 }
812
813
814 /**
815  * Process the given file from the "EGODIR".  Parses the file
816  * and creates the respective 'struct Ego' in memory.
817  *
818  * @param cls NULL
819  * @param filename name of the file to parse
820  * @return GNUNET_OK to continue to iterate,
821  *  GNUNET_NO to stop iteration with no error,
822  *  GNUNET_SYSERR to abort iteration with error!
823  */
824 static int
825 process_ego_file (void *cls,
826                   const char *filename)
827 {
828   struct Ego *ego;
829   const char *fn;
830
831   fn = strrchr (filename, (int) DIR_SEPARATOR);
832   if (NULL == fn)
833   {
834     GNUNET_break (0);
835     return GNUNET_OK;
836   }
837   ego = GNUNET_new (struct Ego);
838   ego->pk = GNUNET_CRYPTO_ecc_key_create_from_file (filename);
839   if (NULL == ego->pk)
840     {
841       GNUNET_free (ego);
842       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
843                   _("Failed to parse ego information in `%s'\n"),
844                   filename);
845       return GNUNET_OK;
846     }
847   
848   ego->identifier = GNUNET_strdup (fn);
849   GNUNET_CONTAINER_DLL_insert (ego_head,
850                                ego_tail,
851                                ego);
852   return GNUNET_OK;
853 }
854
855
856 /**
857  * Handle network size estimate clients.
858  *
859  * @param cls closure
860  * @param server the initialized server
861  * @param c configuration to use
862  */
863 static void
864 run (void *cls, 
865      struct GNUNET_SERVER_Handle *server,
866      const struct GNUNET_CONFIGURATION_Handle *c)
867 {
868   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
869     {&handle_start_message, NULL,
870      GNUNET_MESSAGE_TYPE_IDENTITY_START, sizeof (struct GNUNET_MessageHeader)},
871     {&handle_get_default_message, NULL,
872      GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT, 0},
873     {&handle_set_default_message, NULL,
874      GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT, 0},
875     {&handle_create_message, NULL,
876      GNUNET_MESSAGE_TYPE_IDENTITY_CREATE, 0},
877     {&handle_rename_message, NULL,
878      GNUNET_MESSAGE_TYPE_IDENTITY_RENAME, 0},
879     {&handle_delete_message, NULL,
880      GNUNET_MESSAGE_TYPE_IDENTITY_DELETE, 0},
881     {NULL, NULL, 0, 0}
882   };
883
884   cfg = c;
885   if (GNUNET_OK !=
886       GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
887                                                "EGODIR",
888                                                &ego_directory))
889   {
890     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
891     GNUNET_SCHEDULER_shutdown ();
892     return;
893   }
894   if (GNUNET_OK !=
895       GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
896                                                "SUBSYSTEM_CFG",
897                                                &subsystem_cfg_file))
898   {
899     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "SUBSYSTEM_CFG");
900     GNUNET_SCHEDULER_shutdown ();
901     return;
902   }
903   subsystem_cfg = GNUNET_CONFIGURATION_create ();
904   if ( (GNUNET_YES ==
905         GNUNET_DISK_file_test (subsystem_cfg_file)) &&
906        (GNUNET_OK != 
907         GNUNET_CONFIGURATION_parse (subsystem_cfg,
908                                     subsystem_cfg_file)) )
909   {
910     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
911                 _("Failed to parse subsystem identity configuration file `%s'\n"),
912                 subsystem_cfg_file);
913     GNUNET_SCHEDULER_shutdown ();
914     return;
915   }
916   stats = GNUNET_STATISTICS_create ("identity", cfg);
917   GNUNET_SERVER_add_handlers (server, handlers);
918   nc = GNUNET_SERVER_notification_context_create (server, 1);
919   GNUNET_DISK_directory_scan (ego_directory,
920                               &process_ego_file,
921                               NULL);
922   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
923                                 NULL);
924 }
925
926
927 /**
928  * The main function for the network size estimation service.
929  *
930  * @param argc number of arguments from the command line
931  * @param argv command line arguments
932  * @return 0 ok, 1 on error
933  */
934 int
935 main (int argc, char *const *argv)
936 {
937   return (GNUNET_OK ==
938           GNUNET_SERVICE_run (argc, argv, "identity", 
939                               GNUNET_SERVICE_OPTION_NONE,
940                               &run, NULL)) ? 0 : 1;
941 }
942
943
944 /* end of gnunet-service-identity.c */