fe6c3efa71f0ed921e45f9562859b4ead3909110
[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_log (GNUNET_ERROR_TYPE_DEBUG,
195               "Sending result %d (%s) to client\n",
196               (int) result_code,
197               emsg);
198   GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_NO);  
199   GNUNET_free (rcm);
200 }
201
202
203 /**
204  * Create an update message with information about the current state of an ego.
205  *
206  * @param ego ego to create message for
207  * @return corresponding update message
208  */
209 static struct GNUNET_IDENTITY_UpdateMessage *
210 create_update_message (struct Ego *ego)
211 {
212   struct GNUNET_IDENTITY_UpdateMessage *um;
213   char *str;
214   uint16_t pk_len;
215   size_t name_len;
216   struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
217
218   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
219   enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
220   pk_len = ntohs (enc->size);
221   um = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
222   um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
223   um->header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
224   um->name_len = htons (name_len);
225   um->pk_len = htons (pk_len);
226   str = (char *) &um[1];
227   memcpy (str, enc, pk_len);
228   memcpy (&str[pk_len], ego->identifier, name_len);
229   GNUNET_free (enc);
230   return um;
231 }
232
233
234 /**
235  * Create a set default message with information about the current state of an ego.
236  *
237  * @param ego ego to create message for
238  * @param servicename name of the service to provide in the message
239  * @return corresponding set default message
240  */
241 static struct GNUNET_IDENTITY_SetDefaultMessage *
242 create_set_default_message (struct Ego *ego,
243                             const char *servicename)
244 {
245   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
246   char *str;
247   uint16_t pk_len;
248   size_t name_len;
249   struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
250
251   name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
252   enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
253   pk_len = ntohs (enc->size);
254   sdm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
255   sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
256   sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
257   sdm->name_len = htons (name_len);
258   sdm->pk_len = htons (pk_len);
259   str = (char *) &sdm[1];
260   memcpy (str, enc, pk_len);
261   memcpy (&str[pk_len], servicename, name_len);
262   GNUNET_free (enc);
263   return sdm;
264 }
265
266
267 /**
268  * Handler for START message from client, sends information
269  * about all identities to the client immediately and 
270  * adds the client to the notification context for future
271  * updates.
272  *
273  * @param cls unused
274  * @param client who sent the message
275  * @param message the message received
276  */
277 static void
278 handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
279                       const struct GNUNET_MessageHeader *message)
280 {
281   struct GNUNET_IDENTITY_UpdateMessage *um;
282   struct GNUNET_IDENTITY_UpdateMessage ume;
283   struct Ego *ego;
284
285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
286               "Received START message from client\n");
287   GNUNET_SERVER_notification_context_add (nc, client);
288   for (ego = ego_head; NULL != ego; ego = ego->next)
289   {
290     um = create_update_message (ego);
291     GNUNET_SERVER_notification_context_unicast (nc, client, &um->header, GNUNET_NO);
292     GNUNET_free (um);
293   }
294   ume.header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
295   ume.header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage));
296   ume.pk_len = htons (0);
297   ume.name_len = htons (0);
298   GNUNET_SERVER_notification_context_unicast (nc, client, &ume.header, GNUNET_NO);  
299   GNUNET_SERVER_receive_done (client, GNUNET_OK);
300 }
301
302
303 /**
304  * Handler for GET_DEFAULT message from client, returns
305  * default identity for some service.
306  *
307  * @param cls unused
308  * @param client who sent the message
309  * @param message the message received
310  */
311 static void
312 handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
313                             const struct GNUNET_MessageHeader *message)
314 {
315   const struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
316   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
317   uint16_t size;
318   uint16_t name_len;
319   struct Ego *ego;
320   const char *name;
321   char *identifier;
322
323   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
324               "Received GET_DEFAULT message from client\n");
325   size = ntohs (message->size);
326   if (size <= sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
327   {
328     GNUNET_break (0);
329     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
330     return;
331   }
332   gdm = (const struct GNUNET_IDENTITY_GetDefaultMessage *) message;
333   name = (const char *) &gdm[1];
334   name_len = ntohs (gdm->name_len);
335   if ( (name_len + sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) != size) ||
336        (0 != ntohs (gdm->reserved)) ||
337        ('\0' != name[name_len - 1]) )
338   {
339     GNUNET_break (0);
340     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
341     return;
342   }
343   if (GNUNET_OK !=
344       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
345                                              name,
346                                              "DEFAULT_IDENTIFIER",
347                                              &identifier))
348   {
349     send_result_code (client, 1, gettext_noop ("no default known"));
350     GNUNET_SERVER_receive_done (client, GNUNET_OK);   
351     return;
352   }
353   for (ego = ego_head; NULL != ego; ego = ego->next)
354   {
355     if (0 == strcmp (ego->identifier,
356                      identifier))
357     {
358       sdm = create_set_default_message (ego,
359                                         name);
360       GNUNET_SERVER_notification_context_broadcast (nc, &sdm->header, GNUNET_NO);
361       GNUNET_free (sdm);
362       GNUNET_SERVER_receive_done (client, GNUNET_OK);
363       return;
364     }
365   }
366   send_result_code (client, 1, 
367                     gettext_noop ("default configured, but ego unknown (internal error)"));
368   GNUNET_SERVER_receive_done (client, GNUNET_OK);
369 }
370
371
372 /**
373  * Compare the given two private keys for equality.
374  * 
375  * @param pk1 one private key
376  * @param pk2 another private key
377  * @return 0 if the keys are equal
378  */
379 static int
380 key_cmp (const struct GNUNET_CRYPTO_EccPrivateKey *pk1,
381          const struct GNUNET_CRYPTO_EccPrivateKey *pk2)
382 {
383   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p1;
384   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p2;
385   
386   GNUNET_CRYPTO_ecc_key_get_public (pk1, &p1);
387   GNUNET_CRYPTO_ecc_key_get_public (pk2, &p2);
388   return memcmp (&p1, &p2, sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
389 }
390
391
392 /**
393  * Handler for SET_DEFAULT message from client, updates
394  * default identity for some service.
395  *
396  * @param cls unused
397  * @param client who sent the message
398  * @param message the message received
399  */
400 static void
401 handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
402                             const struct GNUNET_MessageHeader *message)
403 {
404   const struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
405   uint16_t size;
406   uint16_t name_len;
407   uint16_t pk_len;
408   struct Ego *ego;
409   const char *str;
410   struct GNUNET_CRYPTO_EccPrivateKey *pk;
411
412   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
413               "Received SET_DEFAULT message from client\n");
414   size = ntohs (message->size);
415   if (size <= sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
416   {
417     GNUNET_break (0);
418     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
419     return;
420   }
421   sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) message;
422   name_len = ntohs (sdm->name_len);
423   pk_len = ntohs (sdm->pk_len);
424   str = (const char *) &sdm[1];
425   if ( (name_len + pk_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) != size) ||
426        (NULL == (pk = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES))) )
427   {
428     GNUNET_break (0);
429     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
430     return;
431   }
432   str = &str[pk_len];
433   if ('\0' != str[name_len - 1])
434   {
435     GNUNET_break (0);
436     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
437     return;
438   }
439   for (ego = ego_head; NULL != ego; ego = ego->next)
440   {
441     if (0 == key_cmp (ego->pk,
442                       pk))
443     {
444       GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
445                                              str,
446                                              "DEFAULT_IDENTIFIER",
447                                              ego->identifier);
448       if (GNUNET_OK != 
449           GNUNET_CONFIGURATION_write (subsystem_cfg,
450                                       subsystem_cfg_file))
451         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
452                     _("Failed to write subsystem default identifier map to `%s'.\n"),
453                     subsystem_cfg_file);
454       send_result_code (client, 0, NULL);
455       GNUNET_SERVER_receive_done (client, GNUNET_OK);
456       GNUNET_CRYPTO_ecc_key_free (pk);
457       return;
458     }
459   }  
460   send_result_code (client, 1, _("Unknown ego specified for service (internal error)"));
461   GNUNET_SERVER_receive_done (client, GNUNET_OK);
462   GNUNET_CRYPTO_ecc_key_free (pk);
463 }
464
465
466 /**
467  * Send an updated message for the given ego to all listeners.
468  *
469  * @param ego ego to send the update for
470  */
471 static void
472 notify_listeners (struct Ego *ego)
473 {
474   struct GNUNET_IDENTITY_UpdateMessage *um;
475
476   um = create_update_message (ego);
477   GNUNET_SERVER_notification_context_broadcast (nc, &um->header, GNUNET_NO);
478   GNUNET_free (um);
479 }
480
481
482 /**
483  * Handler for CREATE message from client, creates
484  * new identity.
485  *
486  * @param cls unused
487  * @param client who sent the message
488  * @param message the message received
489  */
490 static void
491 handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
492                        const struct GNUNET_MessageHeader *message)
493 {
494   const struct GNUNET_IDENTITY_CreateRequestMessage *crm;
495   uint16_t size;
496   uint16_t name_len;
497   uint16_t pk_len;
498   struct Ego *ego;
499   const char *pks;
500   const char *str;
501   struct GNUNET_CRYPTO_EccPrivateKey *pk;
502   char *fn;
503
504   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
505               "Received CREATE message from client\n");
506   size = ntohs (message->size);
507   if (size <= sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
508   {
509     GNUNET_break (0);
510     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
511     return;
512   }
513   crm = (const struct GNUNET_IDENTITY_CreateRequestMessage *) message;
514   name_len = ntohs (crm->name_len);
515   pk_len = ntohs (crm->pk_len);
516   pks = (const char *) &crm[1];
517   if ( (name_len + pk_len + sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) != size) ||
518        (NULL == (pk = GNUNET_CRYPTO_ecc_decode_key (pks, pk_len, GNUNET_YES))) )
519   {
520     GNUNET_break (0);
521     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
522     return;
523   }
524   str = &pks[pk_len];
525   if ('\0' != str[name_len - 1])
526   {
527     GNUNET_break (0);
528     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
529     return;
530   }
531   for (ego = ego_head; NULL != ego; ego = ego->next)
532   {
533     if (0 == strcmp (ego->identifier,
534                      str))
535     {
536       send_result_code (client, 1, gettext_noop ("identifier already in use for another ego"));
537       GNUNET_SERVER_receive_done (client, GNUNET_OK);
538       GNUNET_CRYPTO_ecc_key_free (pk);
539       return;
540     }
541   }
542   ego = GNUNET_new (struct Ego);
543   ego->pk = pk;
544   ego->identifier = GNUNET_strdup (str);
545   GNUNET_CONTAINER_DLL_insert (ego_head,
546                                ego_tail,
547                                ego);
548   send_result_code (client, 0, NULL);
549   fn = get_ego_filename (ego);
550   (void) GNUNET_DISK_directory_create_for_file (fn);
551   if (pk_len !=
552       GNUNET_DISK_fn_write (fn, pks, pk_len,
553                             GNUNET_DISK_PERM_USER_READ |
554                             GNUNET_DISK_PERM_USER_WRITE))
555     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
556                               "write", fn);
557   GNUNET_free (fn);
558   notify_listeners (ego);  
559   GNUNET_SERVER_receive_done (client, GNUNET_OK);
560 }
561
562
563 /**
564  * Closure for 'handle_ego_rename'.
565  */
566 struct RenameContext 
567 {
568   /**
569    * Old name.
570    */
571   const char *old_name;
572
573   /**
574    * New name.
575    */
576   const char *new_name;
577 };
578
579
580 /**
581  * An ego was renamed; rename it in all subsystems where it is
582  * currently set as the default.
583  *
584  * @param cls the 'struct RenameContext'
585  * @param section a section in the configuration to process
586  */
587 static void
588 handle_ego_rename (void *cls,
589                    const char *section)
590 {
591   struct RenameContext *rc = cls;
592   char *id;
593
594   if (GNUNET_OK !=
595       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
596                                              section,
597                                              "DEFAULT_IDENTIFIER",
598                                              &id))
599     return;
600   if (0 != strcmp (id, rc->old_name))
601   {
602     GNUNET_free (id);  
603     return;
604   }
605   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
606                                          section,
607                                          "DEFAULT_IDENTIFIER",
608                                          rc->new_name);
609   GNUNET_free (id);  
610 }
611
612
613 /**
614  * Handler for RENAME message from client, creates
615  * new identity.
616  *
617  * @param cls unused
618  * @param client who sent the message
619  * @param message the message received
620  */
621 static void
622 handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
623                        const struct GNUNET_MessageHeader *message)
624 {
625   const struct GNUNET_IDENTITY_RenameMessage *rm;
626   uint16_t size;
627   uint16_t old_name_len;
628   uint16_t new_name_len;
629   struct Ego *ego;
630   const char *old_name;
631   const char *new_name;
632   struct RenameContext rename_ctx;
633   char *fn_old;
634   char *fn_new;
635
636   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
637               "Received RENAME message from client\n");
638   size = ntohs (message->size);
639   if (size <= sizeof (struct GNUNET_IDENTITY_RenameMessage))
640   {
641     GNUNET_break (0);
642     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
643     return;
644   }
645   rm = (const struct GNUNET_IDENTITY_RenameMessage *) message;
646   old_name_len = ntohs (rm->old_name_len);
647   new_name_len = ntohs (rm->new_name_len);
648   old_name = (const char *) &rm[1];
649   new_name = &old_name[old_name_len];
650   if ( (old_name_len + new_name_len + sizeof (struct GNUNET_IDENTITY_RenameMessage) != size) ||
651        ('\0' != old_name[old_name_len - 1]) ||
652        ('\0' != new_name[new_name_len - 1]) )
653   {
654     GNUNET_break (0);
655     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
656     return;
657   }
658
659   /* check if new name is already in use */
660   for (ego = ego_head; NULL != ego; ego = ego->next)
661   {
662     if (0 == strcmp (ego->identifier,
663                      new_name))
664     {
665       send_result_code (client, 1, gettext_noop ("target name already exists"));
666       GNUNET_SERVER_receive_done (client, GNUNET_OK);      
667       return;
668     }
669   }
670
671   /* locate old name and, if found, perform rename */
672   for (ego = ego_head; NULL != ego; ego = ego->next)
673   {
674     if (0 == strcmp (ego->identifier,
675                      old_name))
676     {
677       fn_old = get_ego_filename (ego);
678       GNUNET_free (ego->identifier);
679       rename_ctx.old_name = old_name;
680       rename_ctx.new_name = new_name;
681       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
682                                              &handle_ego_rename,
683                                              &rename_ctx);
684       if (GNUNET_OK != 
685           GNUNET_CONFIGURATION_write (subsystem_cfg,
686                                       subsystem_cfg_file))
687         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
688                     _("Failed to write subsystem default identifier map to `%s'.\n"),
689                     subsystem_cfg_file);
690       ego->identifier = GNUNET_strdup (new_name);
691       fn_new = get_ego_filename (ego);      
692       if (0 != RENAME (fn_old, fn_new))
693         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rename", fn_old);
694       GNUNET_free (fn_old);
695       GNUNET_free (fn_new);
696       notify_listeners (ego);
697       send_result_code (client, 0, NULL);
698       GNUNET_SERVER_receive_done (client, GNUNET_OK);
699       return;
700     }
701   }
702
703   /* failed to locate old name */
704   send_result_code (client, 1, gettext_noop ("no matching ego found"));
705   GNUNET_SERVER_receive_done (client, GNUNET_OK);
706 }
707
708
709 /**
710  * An ego was removed, remove it from all subsystems where it is
711  * currently set as the default.
712  *
713  * @param cls name of the removed ego (const char *)
714  * @param section a section in the configuration to process
715  */
716 static void
717 handle_ego_delete (void *cls,
718                    const char *section)
719 {
720   const char *identifier = cls;
721   char *id;
722
723   if (GNUNET_OK !=
724       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
725                                              section,
726                                              "DEFAULT_IDENTIFIER",
727                                              &id))
728     return;
729   if (0 != strcmp (id, identifier))
730   {
731     GNUNET_free (id);  
732     return;
733   }
734   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
735                                          section,
736                                          "DEFAULT_IDENTIFIER",
737                                          NULL);
738   GNUNET_free (id);  
739 }
740
741
742 /**
743  * Handler for DELETE message from client, creates
744  * new identity.
745  *
746  * @param cls unused
747  * @param client who sent the message
748  * @param message the message received
749  */
750 static void
751 handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
752                        const struct GNUNET_MessageHeader *message)
753 {
754   const struct GNUNET_IDENTITY_DeleteMessage *dm;
755   uint16_t size;
756   uint16_t name_len;
757   struct Ego *ego;
758   const char *name;
759   char *fn;
760
761   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
762               "Received DELETE message from client\n");
763   size = ntohs (message->size);
764   if (size <= sizeof (struct GNUNET_IDENTITY_DeleteMessage))
765   {
766     GNUNET_break (0);
767     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
768     return;
769   }
770   dm = (const struct GNUNET_IDENTITY_DeleteMessage *) message;
771   name = (const char *) &dm[1];
772   name_len = ntohs (dm->name_len);
773   if ( (name_len + sizeof (struct GNUNET_IDENTITY_DeleteMessage) != size) ||
774        (0 != ntohs (dm->reserved)) ||
775        ('\0' != name[name_len - 1]) )
776   {
777     GNUNET_break (0);
778     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
779     return;
780   }
781   for (ego = ego_head; NULL != ego; ego = ego->next)
782   {
783     if (0 == strcmp (ego->identifier,
784                      name))
785     {
786       GNUNET_CONTAINER_DLL_remove (ego_head,
787                                    ego_tail,
788                                    ego);
789       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
790                                              &handle_ego_delete,
791                                              ego->identifier);
792       if (GNUNET_OK != 
793           GNUNET_CONFIGURATION_write (subsystem_cfg,
794                                       subsystem_cfg_file))
795         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
796                     _("Failed to write subsystem default identifier map to `%s'.\n"),
797                     subsystem_cfg_file);
798       fn = get_ego_filename (ego);
799       if (0 != UNLINK (fn))
800         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
801       GNUNET_free (fn);
802       GNUNET_free (ego->identifier);
803       ego->identifier = NULL;
804       notify_listeners (ego);
805       GNUNET_CRYPTO_ecc_key_free (ego->pk);
806       GNUNET_free (ego);
807       send_result_code (client, 0, NULL);
808       GNUNET_SERVER_receive_done (client, GNUNET_OK);
809       return;
810     }
811   }
812
813   send_result_code (client, 1, gettext_noop ("no matching ego found"));
814   GNUNET_SERVER_receive_done (client, GNUNET_OK);
815 }
816
817
818 /**
819  * Process the given file from the "EGODIR".  Parses the file
820  * and creates the respective 'struct Ego' in memory.
821  *
822  * @param cls NULL
823  * @param filename name of the file to parse
824  * @return GNUNET_OK to continue to iterate,
825  *  GNUNET_NO to stop iteration with no error,
826  *  GNUNET_SYSERR to abort iteration with error!
827  */
828 static int
829 process_ego_file (void *cls,
830                   const char *filename)
831 {
832   struct Ego *ego;
833   const char *fn;
834
835   fn = strrchr (filename, (int) DIR_SEPARATOR);
836   if (NULL == fn)
837   {
838     GNUNET_break (0);
839     return GNUNET_OK;
840   }
841   ego = GNUNET_new (struct Ego);
842   ego->pk = GNUNET_CRYPTO_ecc_key_create_from_file (filename);
843   if (NULL == ego->pk)
844     {
845       GNUNET_free (ego);
846       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
847                   _("Failed to parse ego information in `%s'\n"),
848                   filename);
849       return GNUNET_OK;
850     }
851   
852   ego->identifier = GNUNET_strdup (fn);
853   GNUNET_CONTAINER_DLL_insert (ego_head,
854                                ego_tail,
855                                ego);
856   return GNUNET_OK;
857 }
858
859
860 /**
861  * Handle network size estimate clients.
862  *
863  * @param cls closure
864  * @param server the initialized server
865  * @param c configuration to use
866  */
867 static void
868 run (void *cls, 
869      struct GNUNET_SERVER_Handle *server,
870      const struct GNUNET_CONFIGURATION_Handle *c)
871 {
872   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
873     {&handle_start_message, NULL,
874      GNUNET_MESSAGE_TYPE_IDENTITY_START, sizeof (struct GNUNET_MessageHeader)},
875     {&handle_get_default_message, NULL,
876      GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT, 0},
877     {&handle_set_default_message, NULL,
878      GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT, 0},
879     {&handle_create_message, NULL,
880      GNUNET_MESSAGE_TYPE_IDENTITY_CREATE, 0},
881     {&handle_rename_message, NULL,
882      GNUNET_MESSAGE_TYPE_IDENTITY_RENAME, 0},
883     {&handle_delete_message, NULL,
884      GNUNET_MESSAGE_TYPE_IDENTITY_DELETE, 0},
885     {NULL, NULL, 0, 0}
886   };
887
888   cfg = c;
889   if (GNUNET_OK !=
890       GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
891                                                "EGODIR",
892                                                &ego_directory))
893   {
894     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
895     GNUNET_SCHEDULER_shutdown ();
896     return;
897   }
898   if (GNUNET_OK !=
899       GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
900                                                "SUBSYSTEM_CFG",
901                                                &subsystem_cfg_file))
902   {
903     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "SUBSYSTEM_CFG");
904     GNUNET_SCHEDULER_shutdown ();
905     return;
906   }
907   subsystem_cfg = GNUNET_CONFIGURATION_create ();
908   if ( (GNUNET_YES ==
909         GNUNET_DISK_file_test (subsystem_cfg_file)) &&
910        (GNUNET_OK != 
911         GNUNET_CONFIGURATION_parse (subsystem_cfg,
912                                     subsystem_cfg_file)) )
913   {
914     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
915                 _("Failed to parse subsystem identity configuration file `%s'\n"),
916                 subsystem_cfg_file);
917     GNUNET_SCHEDULER_shutdown ();
918     return;
919   }
920   stats = GNUNET_STATISTICS_create ("identity", cfg);
921   GNUNET_SERVER_add_handlers (server, handlers);
922   nc = GNUNET_SERVER_notification_context_create (server, 1);
923   if (GNUNET_OK !=
924       GNUNET_DISK_directory_create (ego_directory))
925   {
926     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
927                 _("Failed to create directory `%s' for storing egos\n"),
928                 ego_directory);
929   }
930   GNUNET_DISK_directory_scan (ego_directory,
931                               &process_ego_file,
932                               NULL);
933   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
934                                 NULL);
935 }
936
937
938 /**
939  * The main function for the network size estimation service.
940  *
941  * @param argc number of arguments from the command line
942  * @param argv command line arguments
943  * @return 0 ok, 1 on error
944  */
945 int
946 main (int argc, char *const *argv)
947 {
948   return (GNUNET_OK ==
949           GNUNET_SERVICE_run (argc, argv, "identity", 
950                               GNUNET_SERVICE_OPTION_NONE,
951                               &run, NULL)) ? 0 : 1;
952 }
953
954
955 /* end of gnunet-service-identity.c */