Merge branch 'master' into schanzen/reclaim_256bit
[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      SPDX-License-Identifier: AGPL3.0-or-later
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    * We keep egos in a DLL.
49    */
50   struct Ego *next;
51
52   /**
53    * We keep egos in a DLL.
54    */
55   struct Ego *prev;
56
57   /**
58    * Private key of the ego.
59    */
60   struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
61
62   /**
63    * String identifier for the ego.
64    */
65   char *identifier;
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 /**
132  * Called whenever a client is disconnected.
133  *
134  * @param cls closure
135  * @param client identification of the client
136  * @param app_ctx @a client
137  */
138 static void
139 client_disconnect_cb (void *cls,
140                       struct GNUNET_SERVICE_Client *client,
141                       void *app_ctx)
142 {
143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected\n", client);
144 }
145
146
147 /**
148  * Add a client to our list of active clients.
149  *
150  * @param cls NULL
151  * @param client client to add
152  * @param mq message queue for @a client
153  * @return internal namestore client structure for this client
154  */
155 static void *
156 client_connect_cb (void *cls,
157                    struct GNUNET_SERVICE_Client *client,
158                    struct GNUNET_MQ_Handle *mq)
159 {
160   return client;
161 }
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 =
221     GNUNET_MQ_msg_extra (rcm, elen, GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
222   rcm->result_code = htonl (result_code);
223   if (0 < elen)
224     GNUNET_memcpy (&rcm[1], emsg, elen);
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226               "Sending result %d (%s) to client\n",
227               (int) result_code,
228               emsg);
229   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
230 }
231
232
233 /**
234  * Create an update message with information about the current state of an ego.
235  *
236  * @param ego ego to create message for
237  * @return corresponding update message
238  */
239 static struct GNUNET_MQ_Envelope *
240 create_update_message (struct Ego *ego)
241 {
242   struct UpdateMessage *um;
243   struct GNUNET_MQ_Envelope *env;
244   size_t name_len;
245
246   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
247   env = GNUNET_MQ_msg_extra (um, name_len, GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
248   um->name_len = htons (name_len);
249   um->end_of_list = htons (GNUNET_NO);
250   um->private_key = *ego->pk;
251   GNUNET_memcpy (&um[1], ego->identifier, name_len);
252   return env;
253 }
254
255
256 /**
257  * Create a set default message with information about the current state of an ego.
258  *
259  * @param ego ego to create message for
260  * @param servicename name of the service to provide in the message
261  * @return corresponding set default message
262  */
263 static struct GNUNET_MQ_Envelope *
264 create_set_default_message (struct Ego *ego, const char *servicename)
265 {
266   struct SetDefaultMessage *sdm;
267   struct GNUNET_MQ_Envelope *env;
268   size_t name_len;
269
270   name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
271   env = GNUNET_MQ_msg_extra (sdm,
272                              name_len,
273                              GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
274   sdm->name_len = htons (name_len);
275   sdm->reserved = htons (0);
276   sdm->private_key = *ego->pk;
277   GNUNET_memcpy (&sdm[1], servicename, name_len);
278   return env;
279 }
280
281
282 /**
283  * Handler for START message from client, sends information
284  * about all identities to the client immediately and
285  * adds the client to the notification context for future
286  * updates.
287  *
288  * @param cls a `struct GNUNET_SERVICE_Client *`
289  * @param message the message received
290  */
291 static void
292 handle_start_message (void *cls, const struct GNUNET_MessageHeader *message)
293 {
294   struct GNUNET_SERVICE_Client *client = cls;
295   struct UpdateMessage *ume;
296   struct GNUNET_MQ_Envelope *env;
297   struct Ego *ego;
298
299   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received START message from client\n");
300   GNUNET_SERVICE_client_mark_monitor (client);
301   GNUNET_SERVICE_client_disable_continue_warning (client);
302   GNUNET_notification_context_add (nc, GNUNET_SERVICE_client_get_mq (client));
303   for (ego = ego_head; NULL != ego; ego = ego->next)
304   {
305     env = create_update_message (ego);
306     GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
307   }
308   env = GNUNET_MQ_msg_extra (ume, 0, GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
309   ume->end_of_list = htons (GNUNET_YES);
310   ume->name_len = htons (0);
311   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
312   GNUNET_SERVICE_client_continue (client);
313 }
314
315
316 /**
317  * Handler for LOOKUP message from client, sends information
318  * about ONE identity to the client immediately.
319  *
320  * @param cls unused
321  * @param message the message received
322  * @return #GNUNET_SYSERR if message was ill-formed
323  */
324 static int
325 check_lookup_message (void *cls, const struct LookupMessage *message)
326 {
327   GNUNET_MQ_check_zero_termination (message);
328   return GNUNET_OK;
329 }
330
331
332 /**
333  * Handler for LOOKUP message from client, sends information
334  * about ONE identity to the client immediately.
335  *
336  * @param cls a `struct GNUNET_SERVICE_Client *`
337  * @param message the message received
338  */
339 static void
340 handle_lookup_message (void *cls, const struct LookupMessage *message)
341 {
342   struct GNUNET_SERVICE_Client *client = cls;
343   const char *name;
344   struct GNUNET_MQ_Envelope *env;
345   struct Ego *ego;
346
347   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received LOOKUP message from client\n");
348   name = (const char *) &message[1];
349   for (ego = ego_head; NULL != ego; ego = ego->next)
350   {
351     if (0 != strcasecmp (name, ego->identifier))
352       continue;
353     env = create_update_message (ego);
354     GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
355     GNUNET_SERVICE_client_continue (client);
356     return;
357   }
358   send_result_code (client, 0, "ego not found");
359   GNUNET_SERVICE_client_continue (client);
360 }
361
362
363 /**
364  * Handler for LOOKUP message from client, sends information
365  * about ONE identity to the client immediately.
366  *
367  * @param cls unused
368  * @param message the message received
369  * @return #GNUNET_SYSERR if message was ill-formed
370  */
371 static int
372 check_lookup_by_suffix_message (void *cls, const struct LookupMessage *message)
373 {
374   GNUNET_MQ_check_zero_termination (message);
375   return GNUNET_OK;
376 }
377
378
379 /**
380  * Handler for LOOKUP_BY_SUFFIX message from client, sends information
381  * about ONE identity to the client immediately.
382  *
383  * @param cls a `struct GNUNET_SERVICE_Client *`
384  * @param message the message received
385  */
386 static void
387 handle_lookup_by_suffix_message (void *cls, const struct LookupMessage *message)
388 {
389   struct GNUNET_SERVICE_Client *client = cls;
390   const char *name;
391   struct GNUNET_MQ_Envelope *env;
392   struct Ego *lprefix;
393
394   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395               "Received LOOKUP_BY_SUFFIX message from client\n");
396   name = (const char *) &message[1];
397   lprefix = NULL;
398   for (struct Ego *ego = ego_head; NULL != ego; ego = ego->next)
399   {
400     if ((strlen (ego->identifier) <= strlen (name)) &&
401         (0 == strcmp (ego->identifier,
402                       &name[strlen (name) - strlen (ego->identifier)])) &&
403         ((strlen (name) == strlen (ego->identifier)) ||
404          ('.' == name[strlen (name) - strlen (ego->identifier) - 1])) &&
405         ((NULL == lprefix) ||
406          (strlen (ego->identifier) > strlen (lprefix->identifier))))
407     {
408       /* found better match, update! */
409       lprefix = ego;
410     }
411   }
412   if (NULL != lprefix)
413   {
414     env = create_update_message (lprefix);
415     GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
416     GNUNET_SERVICE_client_continue (client);
417     return;
418   }
419   send_result_code (client, 0, "ego not found");
420   GNUNET_SERVICE_client_continue (client);
421 }
422
423
424 /**
425  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT message
426  *
427  * @param cls client sending the message
428  * @param msg message of type `struct GetDefaultMessage`
429  * @return #GNUNET_OK if @a msg is well-formed
430  */
431 static int
432 check_get_default_message (void *cls, const struct GetDefaultMessage *msg)
433 {
434   uint16_t size;
435   uint16_t name_len;
436   const char *name;
437
438   size = ntohs (msg->header.size);
439   if (size <= sizeof(struct GetDefaultMessage))
440   {
441     GNUNET_break (0);
442     return GNUNET_SYSERR;
443   }
444   name = (const char *) &msg[1];
445   name_len = ntohs (msg->name_len);
446   if ((name_len + sizeof(struct GetDefaultMessage) != size) ||
447       (0 != ntohs (msg->reserved)) || ('\0' != name[name_len - 1]))
448   {
449     GNUNET_break (0);
450     return GNUNET_SYSERR;
451   }
452   return GNUNET_OK;
453 }
454
455
456 /**
457  * Handler for GET_DEFAULT message from client, returns
458  * default identity for some service.
459  *
460  * @param cls unused
461  * @param client who sent the message
462  * @param message the message received
463  */
464 static void
465 handle_get_default_message (void *cls, const struct GetDefaultMessage *gdm)
466 {
467   struct GNUNET_MQ_Envelope *env;
468   struct GNUNET_SERVICE_Client *client = cls;
469   struct Ego *ego;
470   char *name;
471   char *identifier;
472
473
474   name = GNUNET_strdup ((const char *) &gdm[1]);
475   GNUNET_STRINGS_utf8_tolower ((const char *) &gdm[1], name);
476   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477               "Received GET_DEFAULT for service `%s' from client\n",
478               name);
479   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
480                                                           name,
481                                                           "DEFAULT_IDENTIFIER",
482                                                           &identifier))
483   {
484     send_result_code (client, 1, gettext_noop ("no default known"));
485     GNUNET_SERVICE_client_continue (client);
486     GNUNET_free (name);
487     return;
488   }
489   for (ego = ego_head; NULL != ego; ego = ego->next)
490   {
491     if (0 == strcmp (ego->identifier, identifier))
492     {
493       env = create_set_default_message (ego, name);
494       GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
495       GNUNET_SERVICE_client_continue (client);
496       GNUNET_free (identifier);
497       GNUNET_free (name);
498       return;
499     }
500   }
501   GNUNET_free (identifier);
502   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to find ego `%s'\n", name);
503   GNUNET_free (name);
504   send_result_code (client,
505                     1,
506                     gettext_noop (
507                       "default configured, but ego unknown (internal error)"));
508   GNUNET_SERVICE_client_continue (client);
509 }
510
511
512 /**
513  * Compare the given two private keys for equality.
514  *
515  * @param pk1 one private key
516  * @param pk2 another private key
517  * @return 0 if the keys are equal
518  */
519 static int
520 key_cmp (const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk1,
521          const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk2)
522 {
523   return GNUNET_memcmp (pk1, pk2);
524 }
525
526
527 /**
528  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT message
529  *
530  * @param cls client sending the message
531  * @param msg message of type `struct SetDefaultMessage`
532  * @return #GNUNET_OK if @a msg is well-formed
533  */
534 static int
535 check_set_default_message (void *cls, const struct SetDefaultMessage *msg)
536 {
537   uint16_t size;
538   uint16_t name_len;
539   const char *str;
540
541   size = ntohs (msg->header.size);
542   if (size <= sizeof(struct SetDefaultMessage))
543   {
544     GNUNET_break (0);
545     return GNUNET_SYSERR;
546   }
547   name_len = ntohs (msg->name_len);
548   GNUNET_break (0 == ntohs (msg->reserved));
549   if (name_len + sizeof(struct SetDefaultMessage) != size)
550   {
551     GNUNET_break (0);
552     return GNUNET_SYSERR;
553   }
554   str = (const char *) &msg[1];
555   if ('\0' != str[name_len - 1])
556   {
557     GNUNET_break (0);
558     return GNUNET_SYSERR;
559   }
560   return GNUNET_OK;
561 }
562
563
564 /**
565  * Handler for SET_DEFAULT message from client, updates
566  * default identity for some service.
567  *
568  * @param cls unused
569  * @param client who sent the message
570  * @param message the message received
571  */
572 static void
573 handle_set_default_message (void *cls, const struct SetDefaultMessage *sdm)
574 {
575   struct Ego *ego;
576   struct GNUNET_SERVICE_Client *client = cls;
577   char *str;
578
579   str = GNUNET_strdup ((const char *) &sdm[1]);
580   GNUNET_STRINGS_utf8_tolower ((const char *) &sdm[1], str);
581
582   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583               "Received SET_DEFAULT for service `%s' from client\n",
584               str);
585   for (ego = ego_head; NULL != ego; ego = ego->next)
586   {
587     if (0 == key_cmp (ego->pk, &sdm->private_key))
588     {
589       GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
590                                              str,
591                                              "DEFAULT_IDENTIFIER",
592                                              ego->identifier);
593       if (GNUNET_OK !=
594           GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
595         GNUNET_log (
596           GNUNET_ERROR_TYPE_ERROR,
597           _ ("Failed to write subsystem default identifier map to `%s'.\n"),
598           subsystem_cfg_file);
599       send_result_code (client, 0, NULL);
600       GNUNET_SERVICE_client_continue (client);
601       GNUNET_free (str);
602       return;
603     }
604   }
605   send_result_code (client,
606                     1,
607                     _ ("Unknown ego specified for service (internal error)"));
608   GNUNET_free (str);
609   GNUNET_SERVICE_client_continue (client);
610 }
611
612
613 /**
614  * Send an updated message for the given ego to all listeners.
615  *
616  * @param ego ego to send the update for
617  */
618 static void
619 notify_listeners (struct Ego *ego)
620 {
621   struct UpdateMessage *um;
622   size_t name_len;
623
624   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
625   um = GNUNET_malloc (sizeof(struct UpdateMessage) + name_len);
626   um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
627   um->header.size = htons (sizeof(struct UpdateMessage) + name_len);
628   um->name_len = htons (name_len);
629   um->end_of_list = htons (GNUNET_NO);
630   um->private_key = *ego->pk;
631   GNUNET_memcpy (&um[1], ego->identifier, name_len);
632   GNUNET_notification_context_broadcast (nc, &um->header, GNUNET_NO);
633   GNUNET_free (um);
634 }
635
636
637 /**
638  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_CREATE message
639  *
640  * @param cls client sending the message
641  * @param msg message of type `struct CreateRequestMessage`
642  * @return #GNUNET_OK if @a msg is well-formed
643  */
644 static int
645 check_create_message (void *cls, const struct CreateRequestMessage *msg)
646 {
647   uint16_t size;
648   uint16_t name_len;
649   const char *str;
650
651   size = ntohs (msg->header.size);
652   if (size <= sizeof(struct CreateRequestMessage))
653   {
654     GNUNET_break (0);
655     return GNUNET_SYSERR;
656   }
657   name_len = ntohs (msg->name_len);
658   GNUNET_break (0 == ntohs (msg->reserved));
659   if (name_len + sizeof(struct CreateRequestMessage) != size)
660   {
661     GNUNET_break (0);
662     return GNUNET_SYSERR;
663   }
664   str = (const char *) &msg[1];
665   if ('\0' != str[name_len - 1])
666   {
667     GNUNET_break (0);
668     return GNUNET_SYSERR;
669   }
670   return GNUNET_OK;
671 }
672
673
674 /**
675  * Handler for CREATE message from client, creates
676  * new identity.
677  *
678  * @param cls unused
679  * @param client who sent the message
680  * @param message the message received
681  */
682 static void
683 handle_create_message (void *cls, const struct CreateRequestMessage *crm)
684 {
685   struct GNUNET_SERVICE_Client *client = cls;
686   struct Ego *ego;
687   char *str;
688   char *fn;
689
690   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received CREATE message from client\n");
691   str = GNUNET_strdup ((const char *) &crm[1]);
692   GNUNET_STRINGS_utf8_tolower ((const char *) &crm[1], str);
693   for (ego = ego_head; NULL != ego; ego = ego->next)
694   {
695     if (0 == strcmp (ego->identifier, str))
696     {
697       send_result_code (client,
698                         1,
699                         gettext_noop (
700                           "identifier already in use for another ego"));
701       GNUNET_SERVICE_client_continue (client);
702       GNUNET_free (str);
703       return;
704     }
705   }
706   ego = GNUNET_new (struct Ego);
707   ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
708   *ego->pk = crm->private_key;
709   ego->identifier = GNUNET_strdup (str);
710   GNUNET_CONTAINER_DLL_insert (ego_head, ego_tail, ego);
711   send_result_code (client, 0, NULL);
712   fn = get_ego_filename (ego);
713   (void) GNUNET_DISK_directory_create_for_file (fn);
714   if (sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey) !=
715       GNUNET_DISK_fn_write (fn,
716                             &crm->private_key,
717                             sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
718                             GNUNET_DISK_PERM_USER_READ
719                             | GNUNET_DISK_PERM_USER_WRITE))
720     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", fn);
721   GNUNET_free (fn);
722   GNUNET_free (str);
723   notify_listeners (ego);
724   GNUNET_SERVICE_client_continue (client);
725 }
726
727
728 /**
729  * Closure for 'handle_ego_rename'.
730  */
731 struct RenameContext
732 {
733   /**
734    * Old name.
735    */
736   const char *old_name;
737
738   /**
739    * New name.
740    */
741   const char *new_name;
742 };
743
744 /**
745  * An ego was renamed; rename it in all subsystems where it is
746  * currently set as the default.
747  *
748  * @param cls the 'struct RenameContext'
749  * @param section a section in the configuration to process
750  */
751 static void
752 handle_ego_rename (void *cls, const char *section)
753 {
754   struct RenameContext *rc = cls;
755   char *id;
756
757   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
758                                                           section,
759                                                           "DEFAULT_IDENTIFIER",
760                                                           &id))
761     return;
762   if (0 != strcmp (id, rc->old_name))
763   {
764     GNUNET_free (id);
765     return;
766   }
767   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
768                                          section,
769                                          "DEFAULT_IDENTIFIER",
770                                          rc->new_name);
771   GNUNET_free (id);
772 }
773
774
775 /**
776  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_RENAME message
777  *
778  * @param cls client sending the message
779  * @param msg message of type `struct RenameMessage`
780  * @return #GNUNET_OK if @a msg is well-formed
781  */
782 static int
783 check_rename_message (void *cls, const struct RenameMessage *msg)
784 {
785   uint16_t size;
786   uint16_t old_name_len;
787   uint16_t new_name_len;
788   const char *old_name;
789   const char *new_name;
790
791   size = ntohs (msg->header.size);
792   if (size <= sizeof(struct RenameMessage))
793   {
794     GNUNET_break (0);
795     return GNUNET_SYSERR;
796   }
797   old_name_len = ntohs (msg->old_name_len);
798   new_name_len = ntohs (msg->new_name_len);
799   old_name = (const char *) &msg[1];
800   new_name = &old_name[old_name_len];
801   if ((old_name_len + new_name_len + sizeof(struct RenameMessage) != size) ||
802       ('\0' != old_name[old_name_len - 1]) ||
803       ('\0' != new_name[new_name_len - 1]))
804   {
805     GNUNET_break (0);
806     return GNUNET_SYSERR;
807   }
808
809   return GNUNET_OK;
810 }
811
812
813 /**
814  * Handler for RENAME message from client, creates
815  * new identity.
816  *
817  * @param cls unused
818  * @param client who sent the message
819  * @param message the message received
820  */
821 static void
822 handle_rename_message (void *cls, const struct RenameMessage *rm)
823 {
824   uint16_t old_name_len;
825   struct Ego *ego;
826   char *old_name;
827   char *new_name;
828   struct RenameContext rename_ctx;
829   struct GNUNET_SERVICE_Client *client = cls;
830   char *fn_old;
831   char *fn_new;
832   const char *old_name_tmp;
833
834   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received RENAME message from client\n");
835   old_name_len = ntohs (rm->old_name_len);
836   old_name_tmp = (const char *) &rm[1];
837   old_name = GNUNET_strdup (old_name_tmp);
838   GNUNET_STRINGS_utf8_tolower (old_name_tmp, old_name);
839   new_name = GNUNET_strdup (&old_name_tmp[old_name_len]);
840   GNUNET_STRINGS_utf8_tolower (&old_name_tmp[old_name_len], new_name);
841
842   /* check if new name is already in use */
843   for (ego = ego_head; NULL != ego; ego = ego->next)
844   {
845     if (0 == strcmp (ego->identifier, new_name))
846     {
847       send_result_code (client, 1, gettext_noop ("target name already exists"));
848       GNUNET_SERVICE_client_continue (client);
849       GNUNET_free (old_name);
850       GNUNET_free (new_name);
851       return;
852     }
853   }
854
855   /* locate old name and, if found, perform rename */
856   for (ego = ego_head; NULL != ego; ego = ego->next)
857   {
858     if (0 == strcmp (ego->identifier, old_name))
859     {
860       fn_old = get_ego_filename (ego);
861       GNUNET_free (ego->identifier);
862       rename_ctx.old_name = old_name;
863       rename_ctx.new_name = new_name;
864       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
865                                              &handle_ego_rename,
866                                              &rename_ctx);
867       if (GNUNET_OK !=
868           GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
869         GNUNET_log (
870           GNUNET_ERROR_TYPE_ERROR,
871           _ ("Failed to write subsystem default identifier map to `%s'.\n"),
872           subsystem_cfg_file);
873       ego->identifier = GNUNET_strdup (new_name);
874       fn_new = get_ego_filename (ego);
875       if (0 != rename (fn_old, fn_new))
876         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rename", fn_old);
877       GNUNET_free (fn_old);
878       GNUNET_free (fn_new);
879       GNUNET_free (old_name);
880       GNUNET_free (new_name);
881       notify_listeners (ego);
882       send_result_code (client, 0, NULL);
883       GNUNET_SERVICE_client_continue (client);
884       return;
885     }
886   }
887
888   /* failed to locate old name */
889   send_result_code (client, 1, gettext_noop ("no matching ego found"));
890   GNUNET_free (old_name);
891   GNUNET_free (new_name);
892   GNUNET_SERVICE_client_continue (client);
893 }
894
895
896 /**
897  * An ego was removed, remove it from all subsystems where it is
898  * currently set as the default.
899  *
900  * @param cls name of the removed ego (const char *)
901  * @param section a section in the configuration to process
902  */
903 static void
904 handle_ego_delete (void *cls, const char *section)
905 {
906   const char *identifier = cls;
907   char *id;
908
909   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
910                                                           section,
911                                                           "DEFAULT_IDENTIFIER",
912                                                           &id))
913     return;
914   if (0 != strcmp (id, identifier))
915   {
916     GNUNET_free (id);
917     return;
918   }
919   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
920                                          section,
921                                          "DEFAULT_IDENTIFIER",
922                                          NULL);
923   GNUNET_free (id);
924 }
925
926
927 /**
928  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_DELETE message
929  *
930  * @param cls client sending the message
931  * @param msg message of type `struct DeleteMessage`
932  * @return #GNUNET_OK if @a msg is well-formed
933  */
934 static int
935 check_delete_message (void *cls, const struct DeleteMessage *msg)
936 {
937   uint16_t size;
938   uint16_t name_len;
939   const char *name;
940
941   size = ntohs (msg->header.size);
942   if (size <= sizeof(struct DeleteMessage))
943   {
944     GNUNET_break (0);
945     return GNUNET_SYSERR;
946   }
947   name = (const char *) &msg[1];
948   name_len = ntohs (msg->name_len);
949   if ((name_len + sizeof(struct DeleteMessage) != size) ||
950       (0 != ntohs (msg->reserved)) || ('\0' != name[name_len - 1]))
951   {
952     GNUNET_break (0);
953     return GNUNET_SYSERR;
954   }
955   return GNUNET_OK;
956 }
957
958
959 /**
960  * Handler for DELETE message from client, creates
961  * new identity.
962  *
963  * @param cls unused
964  * @param client who sent the message
965  * @param message the message received
966  */
967 static void
968 handle_delete_message (void *cls, const struct DeleteMessage *dm)
969 {
970   struct Ego *ego;
971   char *name;
972   char *fn;
973   struct GNUNET_SERVICE_Client *client = cls;
974
975   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received DELETE message from client\n");
976   name = GNUNET_strdup ((const char *) &dm[1]);
977   GNUNET_STRINGS_utf8_tolower ((const char *) &dm[1], name);
978
979   for (ego = ego_head; NULL != ego; ego = ego->next)
980   {
981     if (0 == strcmp (ego->identifier, name))
982     {
983       GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, ego);
984       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
985                                              &handle_ego_delete,
986                                              ego->identifier);
987       if (GNUNET_OK !=
988           GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
989         GNUNET_log (
990           GNUNET_ERROR_TYPE_ERROR,
991           _ ("Failed to write subsystem default identifier map to `%s'.\n"),
992           subsystem_cfg_file);
993       fn = get_ego_filename (ego);
994       if (0 != unlink (fn))
995         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
996       GNUNET_free (fn);
997       GNUNET_free (ego->identifier);
998       ego->identifier = NULL;
999       notify_listeners (ego);
1000       GNUNET_free (ego->pk);
1001       GNUNET_free (ego);
1002       GNUNET_free (name);
1003       send_result_code (client, 0, NULL);
1004       GNUNET_SERVICE_client_continue (client);
1005       return;
1006     }
1007   }
1008
1009   send_result_code (client, 1, gettext_noop ("no matching ego found"));
1010   GNUNET_free (name);
1011   GNUNET_SERVICE_client_continue (client);
1012 }
1013
1014
1015 /**
1016  * Process the given file from the "EGODIR".  Parses the file
1017  * and creates the respective 'struct Ego' in memory.
1018  *
1019  * @param cls NULL
1020  * @param filename name of the file to parse
1021  * @return #GNUNET_OK to continue to iterate,
1022  *  #GNUNET_NO to stop iteration with no error,
1023  *  #GNUNET_SYSERR to abort iteration with error!
1024  */
1025 static int
1026 process_ego_file (void *cls, const char *filename)
1027 {
1028   struct Ego *ego;
1029   const char *fn;
1030
1031   fn = strrchr (filename, (int) DIR_SEPARATOR);
1032   if (NULL == fn)
1033   {
1034     GNUNET_break (0);
1035     return GNUNET_OK;
1036   }
1037   ego = GNUNET_new (struct Ego);
1038   ego->pk = GNUNET_CRYPTO_ecdsa_key_create_from_file (filename);
1039   if (NULL == ego->pk)
1040   {
1041     GNUNET_free (ego);
1042     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1043                 _ ("Failed to parse ego information in `%s'\n"),
1044                 filename);
1045     return GNUNET_OK;
1046   }
1047   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded ego `%s'\n", fn + 1);
1048   ego->identifier = GNUNET_strdup (fn + 1);
1049   GNUNET_CONTAINER_DLL_insert (ego_head, ego_tail, ego);
1050   return GNUNET_OK;
1051 }
1052
1053
1054 /**
1055  * Handle network size estimate clients.
1056  *
1057  * @param cls closure
1058  * @param server the initialized server
1059  * @param c configuration to use
1060  */
1061 static void
1062 run (void *cls,
1063      const struct GNUNET_CONFIGURATION_Handle *c,
1064      struct GNUNET_SERVICE_Handle *service)
1065 {
1066   cfg = c;
1067   nc = GNUNET_notification_context_create (1);
1068   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
1069                                                             "identity",
1070                                                             "EGODIR",
1071                                                             &ego_directory))
1072   {
1073     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
1074     GNUNET_SCHEDULER_shutdown ();
1075     return;
1076   }
1077   if (GNUNET_OK !=
1078       GNUNET_CONFIGURATION_get_value_filename (cfg,
1079                                                "identity",
1080                                                "SUBSYSTEM_CFG",
1081                                                &subsystem_cfg_file))
1082   {
1083     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1084                                "identity",
1085                                "SUBSYSTEM_CFG");
1086     GNUNET_SCHEDULER_shutdown ();
1087     return;
1088   }
1089   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090               "Loading subsystem configuration `%s'\n",
1091               subsystem_cfg_file);
1092   subsystem_cfg = GNUNET_CONFIGURATION_create ();
1093   if ((GNUNET_YES == GNUNET_DISK_file_test (subsystem_cfg_file)) &&
1094       (GNUNET_OK !=
1095        GNUNET_CONFIGURATION_parse (subsystem_cfg, subsystem_cfg_file)))
1096   {
1097     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1098                 _ (
1099                   "Failed to parse subsystem identity configuration file `%s'\n"),
1100                 subsystem_cfg_file);
1101     GNUNET_SCHEDULER_shutdown ();
1102     return;
1103   }
1104   stats = GNUNET_STATISTICS_create ("identity", cfg);
1105   if (GNUNET_OK != GNUNET_DISK_directory_create (ego_directory))
1106   {
1107     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1108                 _ ("Failed to create directory `%s' for storing egos\n"),
1109                 ego_directory);
1110   }
1111   GNUNET_DISK_directory_scan (ego_directory, &process_ego_file, NULL);
1112   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1113 }
1114
1115
1116 /**
1117  * Define "main" method using service macro.
1118  */
1119 GNUNET_SERVICE_MAIN (
1120   "identity",
1121   GNUNET_SERVICE_OPTION_NONE,
1122   &run,
1123   &client_connect_cb,
1124   &client_disconnect_cb,
1125   NULL,
1126   GNUNET_MQ_hd_fixed_size (start_message,
1127                            GNUNET_MESSAGE_TYPE_IDENTITY_START,
1128                            struct GNUNET_MessageHeader,
1129                            NULL),
1130   GNUNET_MQ_hd_var_size (lookup_message,
1131                          GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP,
1132                          struct LookupMessage,
1133                          NULL),
1134   GNUNET_MQ_hd_var_size (lookup_by_suffix_message,
1135                          GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP_BY_SUFFIX,
1136                          struct LookupMessage,
1137                          NULL),
1138   GNUNET_MQ_hd_var_size (get_default_message,
1139                          GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT,
1140                          struct GetDefaultMessage,
1141                          NULL),
1142   GNUNET_MQ_hd_var_size (set_default_message,
1143                          GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT,
1144                          struct SetDefaultMessage,
1145                          NULL),
1146   GNUNET_MQ_hd_var_size (create_message,
1147                          GNUNET_MESSAGE_TYPE_IDENTITY_CREATE,
1148                          struct CreateRequestMessage,
1149                          NULL),
1150   GNUNET_MQ_hd_var_size (rename_message,
1151                          GNUNET_MESSAGE_TYPE_IDENTITY_RENAME,
1152                          struct RenameMessage,
1153                          NULL),
1154   GNUNET_MQ_hd_var_size (delete_message,
1155                          GNUNET_MESSAGE_TYPE_IDENTITY_DELETE,
1156                          struct DeleteMessage,
1157                          NULL),
1158   GNUNET_MQ_handler_end ());
1159
1160
1161 /* end of gnunet-service-identity.c */