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