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