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