first step to remove plibc
[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   /**
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_EcdsaPrivateKey *pk;
62
63   /**
64    * String identifier for the ego.
65    */
66   char *identifier;
67 };
68
69
70 /**
71  * Handle to our current configuration.
72  */
73 static const struct GNUNET_CONFIGURATION_Handle *cfg;
74
75 /**
76  * Handle to subsystem configuration which for each subsystem contains
77  * the name of the default ego.
78  */
79 static struct GNUNET_CONFIGURATION_Handle *subsystem_cfg;
80
81 /**
82  * Handle to the statistics service.
83  */
84 static struct GNUNET_STATISTICS_Handle *stats;
85
86 /**
87  * Notification context, simplifies client broadcasts.
88  */
89 static struct GNUNET_NotificationContext *nc;
90
91 /**
92  * Directory where we store the identities.
93  */
94 static char *ego_directory;
95
96 /**
97  * Configuration file name where subsystem information is kept.
98  */
99 static char *subsystem_cfg_file;
100
101 /**
102  * Head of DLL of all egos.
103  */
104 static struct Ego *ego_head;
105
106 /**
107  * Tail of DLL of all egos.
108  */
109 static struct Ego *ego_tail;
110
111
112 /**
113  * Get the name of the file we use to store a given ego.
114  *
115  * @param ego ego for which we need the filename
116  * @return full filename for the given ego
117  */
118 static char *
119 get_ego_filename (struct Ego *ego)
120 {
121   char *filename;
122
123   GNUNET_asprintf (&filename,
124                    "%s%s%s",
125                    ego_directory,
126                    DIR_SEPARATOR_STR,
127                    ego->identifier);
128   return filename;
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  * Task run during shutdown.
165  *
166  * @param cls unused
167  */
168 static void
169 shutdown_task (void *cls)
170 {
171   struct Ego *e;
172
173   if (NULL != nc)
174   {
175     GNUNET_notification_context_destroy (nc);
176     nc = NULL;
177   }
178   if (NULL != stats)
179   {
180     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
181     stats = NULL;
182   }
183   GNUNET_CONFIGURATION_destroy (subsystem_cfg);
184   subsystem_cfg = NULL;
185   GNUNET_free (subsystem_cfg_file);
186   subsystem_cfg_file = NULL;
187   GNUNET_free (ego_directory);
188   ego_directory = NULL;
189   while (NULL != (e = ego_head))
190   {
191     GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
192     GNUNET_free (e->pk);
193     GNUNET_free (e->identifier);
194     GNUNET_free (e);
195   }
196 }
197
198
199 /**
200  * Send a result code back to the client.
201  *
202  * @param client client that should receive the result code
203  * @param result_code code to transmit
204  * @param emsg error message to include (or NULL for none)
205  */
206 static void
207 send_result_code (struct GNUNET_SERVICE_Client *client,
208                   uint32_t result_code,
209                   const char *emsg)
210 {
211   struct ResultCodeMessage *rcm;
212   struct GNUNET_MQ_Envelope *env;
213   size_t elen;
214
215   if (NULL == emsg)
216     elen = 0;
217   else
218     elen = strlen (emsg) + 1;
219   env =
220     GNUNET_MQ_msg_extra (rcm, elen, GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
221   rcm->result_code = htonl (result_code);
222   if (0 < elen)
223     GNUNET_memcpy (&rcm[1], emsg, elen);
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225               "Sending result %d (%s) to client\n",
226               (int) result_code,
227               emsg);
228   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
229 }
230
231
232 /**
233  * Create an update message with information about the current state of an ego.
234  *
235  * @param ego ego to create message for
236  * @return corresponding update message
237  */
238 static struct GNUNET_MQ_Envelope *
239 create_update_message (struct Ego *ego)
240 {
241   struct UpdateMessage *um;
242   struct GNUNET_MQ_Envelope *env;
243   size_t name_len;
244
245   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
246   env = GNUNET_MQ_msg_extra (um, name_len, GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
247   um->name_len = htons (name_len);
248   um->end_of_list = htons (GNUNET_NO);
249   um->private_key = *ego->pk;
250   GNUNET_memcpy (&um[1], ego->identifier, name_len);
251   return env;
252 }
253
254
255 /**
256  * Create a set default message with information about the current state of an ego.
257  *
258  * @param ego ego to create message for
259  * @param servicename name of the service to provide in the message
260  * @return corresponding set default message
261  */
262 static struct GNUNET_MQ_Envelope *
263 create_set_default_message (struct Ego *ego, const char *servicename)
264 {
265   struct SetDefaultMessage *sdm;
266   struct GNUNET_MQ_Envelope *env;
267   size_t name_len;
268
269   name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
270   env = GNUNET_MQ_msg_extra (sdm,
271                              name_len,
272                              GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
273   sdm->name_len = htons (name_len);
274   sdm->reserved = htons (0);
275   sdm->private_key = *ego->pk;
276   GNUNET_memcpy (&sdm[1], servicename, name_len);
277   return env;
278 }
279
280
281 /**
282  * Handler for START message from client, sends information
283  * about all identities to the client immediately and
284  * adds the client to the notification context for future
285  * updates.
286  *
287  * @param cls a `struct GNUNET_SERVICE_Client *`
288  * @param message the message received
289  */
290 static void
291 handle_start_message (void *cls, const struct GNUNET_MessageHeader *message)
292 {
293   struct GNUNET_SERVICE_Client *client = cls;
294   struct UpdateMessage *ume;
295   struct GNUNET_MQ_Envelope *env;
296   struct Ego *ego;
297
298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received START message from client\n");
299   GNUNET_SERVICE_client_mark_monitor (client);
300   GNUNET_SERVICE_client_disable_continue_warning (client);
301   GNUNET_notification_context_add (nc, GNUNET_SERVICE_client_get_mq (client));
302   for (ego = ego_head; NULL != ego; ego = ego->next)
303   {
304     env = create_update_message (ego);
305     GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
306   }
307   env = GNUNET_MQ_msg_extra (ume, 0, GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
308   ume->end_of_list = htons (GNUNET_YES);
309   ume->name_len = htons (0);
310   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
311   GNUNET_SERVICE_client_continue (client);
312 }
313
314
315 /**
316  * Handler for LOOKUP message from client, sends information
317  * about ONE identity to the client immediately.
318  *
319  * @param cls unused
320  * @param message the message received
321  * @return #GNUNET_SYSERR if message was ill-formed
322  */
323 static int
324 check_lookup_message (void *cls, const struct LookupMessage *message)
325 {
326   GNUNET_MQ_check_zero_termination (message);
327   return GNUNET_OK;
328 }
329
330
331 /**
332  * Handler for LOOKUP message from client, sends information
333  * about ONE identity to the client immediately.
334  *
335  * @param cls a `struct GNUNET_SERVICE_Client *`
336  * @param message the message received
337  */
338 static void
339 handle_lookup_message (void *cls, const struct LookupMessage *message)
340 {
341   struct GNUNET_SERVICE_Client *client = cls;
342   const char *name;
343   struct GNUNET_MQ_Envelope *env;
344   struct Ego *ego;
345
346   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received LOOKUP message from client\n");
347   name = (const char *) &message[1];
348   for (ego = ego_head; NULL != ego; ego = ego->next)
349   {
350     if (0 != strcasecmp (name, ego->identifier))
351       continue;
352     env = create_update_message (ego);
353     GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
354     GNUNET_SERVICE_client_continue (client);
355     return;
356   }
357   send_result_code (client, 0, "ego not found");
358   GNUNET_SERVICE_client_continue (client);
359 }
360
361
362 /**
363  * Handler for LOOKUP message from client, sends information
364  * about ONE identity to the client immediately.
365  *
366  * @param cls unused
367  * @param message the message received
368  * @return #GNUNET_SYSERR if message was ill-formed
369  */
370 static int
371 check_lookup_by_suffix_message (void *cls, const struct LookupMessage *message)
372 {
373   GNUNET_MQ_check_zero_termination (message);
374   return GNUNET_OK;
375 }
376
377
378 /**
379  * Handler for LOOKUP_BY_SUFFIX message from client, sends information
380  * about ONE identity to the client immediately.
381  *
382  * @param cls a `struct GNUNET_SERVICE_Client *`
383  * @param message the message received
384  */
385 static void
386 handle_lookup_by_suffix_message (void *cls, const struct LookupMessage *message)
387 {
388   struct GNUNET_SERVICE_Client *client = cls;
389   const char *name;
390   struct GNUNET_MQ_Envelope *env;
391   struct Ego *lprefix;
392
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394               "Received LOOKUP_BY_SUFFIX message from client\n");
395   name = (const char *) &message[1];
396   lprefix = NULL;
397   for (struct Ego *ego = ego_head; NULL != ego; ego = ego->next)
398   {
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  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT message
528  *
529  * @param cls client sending the message
530  * @param msg message of type `struct SetDefaultMessage`
531  * @return #GNUNET_OK if @a msg is well-formed
532  */
533 static int
534 check_set_default_message (void *cls, const struct SetDefaultMessage *msg)
535 {
536   uint16_t size;
537   uint16_t name_len;
538   const char *str;
539
540   size = ntohs (msg->header.size);
541   if (size <= sizeof (struct SetDefaultMessage))
542   {
543     GNUNET_break (0);
544     return GNUNET_SYSERR;
545   }
546   name_len = ntohs (msg->name_len);
547   GNUNET_break (0 == ntohs (msg->reserved));
548   if (name_len + sizeof (struct SetDefaultMessage) != size)
549   {
550     GNUNET_break (0);
551     return GNUNET_SYSERR;
552   }
553   str = (const char *) &msg[1];
554   if ('\0' != str[name_len - 1])
555   {
556     GNUNET_break (0);
557     return GNUNET_SYSERR;
558   }
559   return GNUNET_OK;
560 }
561
562 /**
563  * Handler for SET_DEFAULT message from client, updates
564  * default identity for some service.
565  *
566  * @param cls unused
567  * @param client who sent the message
568  * @param message the message received
569  */
570 static void
571 handle_set_default_message (void *cls, const struct SetDefaultMessage *sdm)
572 {
573   struct Ego *ego;
574   struct GNUNET_SERVICE_Client *client = cls;
575   char *str;
576
577   str = GNUNET_strdup ((const char *) &sdm[1]);
578   GNUNET_STRINGS_utf8_tolower ((const char *) &sdm[1], str);
579
580   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581               "Received SET_DEFAULT for service `%s' from client\n",
582               str);
583   for (ego = ego_head; NULL != ego; ego = ego->next)
584   {
585     if (0 == key_cmp (ego->pk, &sdm->private_key))
586     {
587       GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
588                                              str,
589                                              "DEFAULT_IDENTIFIER",
590                                              ego->identifier);
591       if (GNUNET_OK !=
592           GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
593         GNUNET_log (
594           GNUNET_ERROR_TYPE_ERROR,
595           _ ("Failed to write subsystem default identifier map to `%s'.\n"),
596           subsystem_cfg_file);
597       send_result_code (client, 0, NULL);
598       GNUNET_SERVICE_client_continue (client);
599       GNUNET_free (str);
600       return;
601     }
602   }
603   send_result_code (client,
604                     1,
605                     _ ("Unknown ego specified for service (internal error)"));
606   GNUNET_free (str);
607   GNUNET_SERVICE_client_continue (client);
608 }
609
610
611 /**
612  * Send an updated message for the given ego to all listeners.
613  *
614  * @param ego ego to send the update for
615  */
616 static void
617 notify_listeners (struct Ego *ego)
618 {
619   struct UpdateMessage *um;
620   size_t name_len;
621
622   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
623   um = GNUNET_malloc (sizeof (struct UpdateMessage) + name_len);
624   um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
625   um->header.size = htons (sizeof (struct UpdateMessage) + name_len);
626   um->name_len = htons (name_len);
627   um->end_of_list = htons (GNUNET_NO);
628   um->private_key = *ego->pk;
629   GNUNET_memcpy (&um[1], ego->identifier, name_len);
630   GNUNET_notification_context_broadcast (nc, &um->header, GNUNET_NO);
631   GNUNET_free (um);
632 }
633
634 /**
635  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_CREATE message
636  *
637  * @param cls client sending the message
638  * @param msg message of type `struct CreateRequestMessage`
639  * @return #GNUNET_OK if @a msg is well-formed
640  */
641 static int
642 check_create_message (void *cls, const struct CreateRequestMessage *msg)
643 {
644
645   uint16_t size;
646   uint16_t name_len;
647   const char *str;
648
649   size = ntohs (msg->header.size);
650   if (size <= sizeof (struct CreateRequestMessage))
651   {
652     GNUNET_break (0);
653     return GNUNET_SYSERR;
654   }
655   name_len = ntohs (msg->name_len);
656   GNUNET_break (0 == ntohs (msg->reserved));
657   if (name_len + sizeof (struct CreateRequestMessage) != size)
658   {
659     GNUNET_break (0);
660     return GNUNET_SYSERR;
661   }
662   str = (const char *) &msg[1];
663   if ('\0' != str[name_len - 1])
664   {
665     GNUNET_break (0);
666     return GNUNET_SYSERR;
667   }
668   return GNUNET_OK;
669 }
670
671 /**
672  * Handler for CREATE message from client, creates
673  * new identity.
674  *
675  * @param cls unused
676  * @param client who sent the message
677  * @param message the message received
678  */
679 static void
680 handle_create_message (void *cls, const struct CreateRequestMessage *crm)
681 {
682   struct GNUNET_SERVICE_Client *client = cls;
683   struct Ego *ego;
684   char *str;
685   char *fn;
686
687   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received CREATE message from client\n");
688   str = GNUNET_strdup ((const char *) &crm[1]);
689   GNUNET_STRINGS_utf8_tolower ((const char *) &crm[1], str);
690   for (ego = ego_head; NULL != ego; ego = ego->next)
691   {
692     if (0 == strcmp (ego->identifier, str))
693     {
694       send_result_code (client,
695                         1,
696                         gettext_noop (
697                           "identifier already in use for another ego"));
698       GNUNET_SERVICE_client_continue (client);
699       GNUNET_free (str);
700       return;
701     }
702   }
703   ego = GNUNET_new (struct Ego);
704   ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
705   *ego->pk = crm->private_key;
706   ego->identifier = GNUNET_strdup (str);
707   GNUNET_CONTAINER_DLL_insert (ego_head, ego_tail, ego);
708   send_result_code (client, 0, NULL);
709   fn = get_ego_filename (ego);
710   (void) GNUNET_DISK_directory_create_for_file (fn);
711   if (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey) !=
712       GNUNET_DISK_fn_write (fn,
713                             &crm->private_key,
714                             sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
715                             GNUNET_DISK_PERM_USER_READ |
716                               GNUNET_DISK_PERM_USER_WRITE))
717     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", fn);
718   GNUNET_free (fn);
719   GNUNET_free (str);
720   notify_listeners (ego);
721   GNUNET_SERVICE_client_continue (client);
722 }
723
724
725 /**
726  * Closure for 'handle_ego_rename'.
727  */
728 struct RenameContext
729 {
730   /**
731    * Old name.
732    */
733   const char *old_name;
734
735   /**
736    * New name.
737    */
738   const char *new_name;
739 };
740
741 /**
742  * An ego was renamed; rename it in all subsystems where it is
743  * currently set as the default.
744  *
745  * @param cls the 'struct RenameContext'
746  * @param section a section in the configuration to process
747  */
748 static void
749 handle_ego_rename (void *cls, const char *section)
750 {
751   struct RenameContext *rc = cls;
752   char *id;
753
754   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
755                                                           section,
756                                                           "DEFAULT_IDENTIFIER",
757                                                           &id))
758     return;
759   if (0 != strcmp (id, rc->old_name))
760   {
761     GNUNET_free (id);
762     return;
763   }
764   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
765                                          section,
766                                          "DEFAULT_IDENTIFIER",
767                                          rc->new_name);
768   GNUNET_free (id);
769 }
770
771 /**
772  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_RENAME message
773  *
774  * @param cls client sending the message
775  * @param msg message of type `struct RenameMessage`
776  * @return #GNUNET_OK if @a msg is well-formed
777  */
778 static int
779 check_rename_message (void *cls, const struct RenameMessage *msg)
780 {
781   uint16_t size;
782   uint16_t old_name_len;
783   uint16_t new_name_len;
784   const char *old_name;
785   const char *new_name;
786
787   size = ntohs (msg->header.size);
788   if (size <= sizeof (struct RenameMessage))
789   {
790     GNUNET_break (0);
791     return GNUNET_SYSERR;
792   }
793   old_name_len = ntohs (msg->old_name_len);
794   new_name_len = ntohs (msg->new_name_len);
795   old_name = (const char *) &msg[1];
796   new_name = &old_name[old_name_len];
797   if ((old_name_len + new_name_len + sizeof (struct RenameMessage) != size) ||
798       ('\0' != old_name[old_name_len - 1]) ||
799       ('\0' != new_name[new_name_len - 1]))
800   {
801     GNUNET_break (0);
802     return GNUNET_SYSERR;
803   }
804
805   return GNUNET_OK;
806 }
807
808
809 /**
810  * Handler for RENAME message from client, creates
811  * new identity.
812  *
813  * @param cls unused
814  * @param client who sent the message
815  * @param message the message received
816  */
817 static void
818 handle_rename_message (void *cls, const struct RenameMessage *rm)
819 {
820   uint16_t old_name_len;
821   struct Ego *ego;
822   char *old_name;
823   char *new_name;
824   struct RenameContext rename_ctx;
825   struct GNUNET_SERVICE_Client *client = cls;
826   char *fn_old;
827   char *fn_new;
828   const char *old_name_tmp;
829
830   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received RENAME message from client\n");
831   old_name_len = ntohs (rm->old_name_len);
832   old_name_tmp = (const char *) &rm[1];
833   old_name = GNUNET_strdup (old_name_tmp);
834   GNUNET_STRINGS_utf8_tolower (old_name_tmp, old_name);
835   new_name = GNUNET_strdup (&old_name_tmp[old_name_len]);
836   GNUNET_STRINGS_utf8_tolower (&old_name_tmp[old_name_len], new_name);
837
838   /* check if new name is already in use */
839   for (ego = ego_head; NULL != ego; ego = ego->next)
840   {
841     if (0 == strcmp (ego->identifier, new_name))
842     {
843       send_result_code (client, 1, gettext_noop ("target name already exists"));
844       GNUNET_SERVICE_client_continue (client);
845       GNUNET_free (old_name);
846       GNUNET_free (new_name);
847       return;
848     }
849   }
850
851   /* locate old name and, if found, perform rename */
852   for (ego = ego_head; NULL != ego; ego = ego->next)
853   {
854     if (0 == strcmp (ego->identifier, old_name))
855     {
856       fn_old = get_ego_filename (ego);
857       GNUNET_free (ego->identifier);
858       rename_ctx.old_name = old_name;
859       rename_ctx.new_name = new_name;
860       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
861                                              &handle_ego_rename,
862                                              &rename_ctx);
863       if (GNUNET_OK !=
864           GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
865         GNUNET_log (
866           GNUNET_ERROR_TYPE_ERROR,
867           _ ("Failed to write subsystem default identifier map to `%s'.\n"),
868           subsystem_cfg_file);
869       ego->identifier = GNUNET_strdup (new_name);
870       fn_new = get_ego_filename (ego);
871       if (0 != rename (fn_old, fn_new))
872         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rename", fn_old);
873       GNUNET_free (fn_old);
874       GNUNET_free (fn_new);
875       GNUNET_free (old_name);
876       GNUNET_free (new_name);
877       notify_listeners (ego);
878       send_result_code (client, 0, NULL);
879       GNUNET_SERVICE_client_continue (client);
880       return;
881     }
882   }
883
884   /* failed to locate old name */
885   send_result_code (client, 1, gettext_noop ("no matching ego found"));
886   GNUNET_free (old_name);
887   GNUNET_free (new_name);
888   GNUNET_SERVICE_client_continue (client);
889 }
890
891
892 /**
893  * An ego was removed, remove it from all subsystems where it is
894  * currently set as the default.
895  *
896  * @param cls name of the removed ego (const char *)
897  * @param section a section in the configuration to process
898  */
899 static void
900 handle_ego_delete (void *cls, const char *section)
901 {
902   const char *identifier = cls;
903   char *id;
904
905   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
906                                                           section,
907                                                           "DEFAULT_IDENTIFIER",
908                                                           &id))
909     return;
910   if (0 != strcmp (id, identifier))
911   {
912     GNUNET_free (id);
913     return;
914   }
915   GNUNET_CONFIGURATION_set_value_string (subsystem_cfg,
916                                          section,
917                                          "DEFAULT_IDENTIFIER",
918                                          NULL);
919   GNUNET_free (id);
920 }
921
922 /**
923  * Checks a #GNUNET_MESSAGE_TYPE_IDENTITY_DELETE message
924  *
925  * @param cls client sending the message
926  * @param msg message of type `struct DeleteMessage`
927  * @return #GNUNET_OK if @a msg is well-formed
928  */
929 static int
930 check_delete_message (void *cls, const struct DeleteMessage *msg)
931 {
932   uint16_t size;
933   uint16_t name_len;
934   const char *name;
935
936   size = ntohs (msg->header.size);
937   if (size <= sizeof (struct DeleteMessage))
938   {
939     GNUNET_break (0);
940     return GNUNET_SYSERR;
941   }
942   name = (const char *) &msg[1];
943   name_len = ntohs (msg->name_len);
944   if ((name_len + sizeof (struct DeleteMessage) != size) ||
945       (0 != ntohs (msg->reserved)) || ('\0' != name[name_len - 1]))
946   {
947     GNUNET_break (0);
948     return GNUNET_SYSERR;
949   }
950   return GNUNET_OK;
951 }
952
953
954 /**
955  * Handler for DELETE message from client, creates
956  * new identity.
957  *
958  * @param cls unused
959  * @param client who sent the message
960  * @param message the message received
961  */
962 static void
963 handle_delete_message (void *cls, const struct DeleteMessage *dm)
964 {
965   struct Ego *ego;
966   char *name;
967   char *fn;
968   struct GNUNET_SERVICE_Client *client = cls;
969
970   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received DELETE message from client\n");
971   name = GNUNET_strdup ((const char *) &dm[1]);
972   GNUNET_STRINGS_utf8_tolower ((const char *) &dm[1], name);
973
974   for (ego = ego_head; NULL != ego; ego = ego->next)
975   {
976     if (0 == strcmp (ego->identifier, name))
977     {
978       GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, ego);
979       GNUNET_CONFIGURATION_iterate_sections (subsystem_cfg,
980                                              &handle_ego_delete,
981                                              ego->identifier);
982       if (GNUNET_OK !=
983           GNUNET_CONFIGURATION_write (subsystem_cfg, subsystem_cfg_file))
984         GNUNET_log (
985           GNUNET_ERROR_TYPE_ERROR,
986           _ ("Failed to write subsystem default identifier map to `%s'.\n"),
987           subsystem_cfg_file);
988       fn = get_ego_filename (ego);
989       if (0 != unlink (fn))
990         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
991       GNUNET_free (fn);
992       GNUNET_free (ego->identifier);
993       ego->identifier = NULL;
994       notify_listeners (ego);
995       GNUNET_free (ego->pk);
996       GNUNET_free (ego);
997       GNUNET_free (name);
998       send_result_code (client, 0, NULL);
999       GNUNET_SERVICE_client_continue (client);
1000       return;
1001     }
1002   }
1003
1004   send_result_code (client, 1, gettext_noop ("no matching ego found"));
1005   GNUNET_free (name);
1006   GNUNET_SERVICE_client_continue (client);
1007 }
1008
1009
1010 /**
1011  * Process the given file from the "EGODIR".  Parses the file
1012  * and creates the respective 'struct Ego' in memory.
1013  *
1014  * @param cls NULL
1015  * @param filename name of the file to parse
1016  * @return #GNUNET_OK to continue to iterate,
1017  *  #GNUNET_NO to stop iteration with no error,
1018  *  #GNUNET_SYSERR to abort iteration with error!
1019  */
1020 static int
1021 process_ego_file (void *cls, const char *filename)
1022 {
1023   struct Ego *ego;
1024   const char *fn;
1025
1026   fn = strrchr (filename, (int) DIR_SEPARATOR);
1027   if (NULL == fn)
1028   {
1029     GNUNET_break (0);
1030     return GNUNET_OK;
1031   }
1032   ego = GNUNET_new (struct Ego);
1033   ego->pk = GNUNET_CRYPTO_ecdsa_key_create_from_file (filename);
1034   if (NULL == ego->pk)
1035   {
1036     GNUNET_free (ego);
1037     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1038                 _ ("Failed to parse ego information in `%s'\n"),
1039                 filename);
1040     return GNUNET_OK;
1041   }
1042   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded ego `%s'\n", fn + 1);
1043   ego->identifier = GNUNET_strdup (fn + 1);
1044   GNUNET_CONTAINER_DLL_insert (ego_head, ego_tail, ego);
1045   return GNUNET_OK;
1046 }
1047
1048
1049 /**
1050  * Handle network size estimate clients.
1051  *
1052  * @param cls closure
1053  * @param server the initialized server
1054  * @param c configuration to use
1055  */
1056 static void
1057 run (void *cls,
1058      const struct GNUNET_CONFIGURATION_Handle *c,
1059      struct GNUNET_SERVICE_Handle *service)
1060 {
1061   cfg = c;
1062   nc = GNUNET_notification_context_create (1);
1063   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
1064                                                             "identity",
1065                                                             "EGODIR",
1066                                                             &ego_directory))
1067   {
1068     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
1069     GNUNET_SCHEDULER_shutdown ();
1070     return;
1071   }
1072   if (GNUNET_OK !=
1073       GNUNET_CONFIGURATION_get_value_filename (cfg,
1074                                                "identity",
1075                                                "SUBSYSTEM_CFG",
1076                                                &subsystem_cfg_file))
1077   {
1078     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1079                                "identity",
1080                                "SUBSYSTEM_CFG");
1081     GNUNET_SCHEDULER_shutdown ();
1082     return;
1083   }
1084   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085               "Loading subsystem configuration `%s'\n",
1086               subsystem_cfg_file);
1087   subsystem_cfg = GNUNET_CONFIGURATION_create ();
1088   if ((GNUNET_YES == GNUNET_DISK_file_test (subsystem_cfg_file)) &&
1089       (GNUNET_OK !=
1090        GNUNET_CONFIGURATION_parse (subsystem_cfg, subsystem_cfg_file)))
1091   {
1092     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1093                 _ (
1094                   "Failed to parse subsystem identity configuration file `%s'\n"),
1095                 subsystem_cfg_file);
1096     GNUNET_SCHEDULER_shutdown ();
1097     return;
1098   }
1099   stats = GNUNET_STATISTICS_create ("identity", cfg);
1100   if (GNUNET_OK != GNUNET_DISK_directory_create (ego_directory))
1101   {
1102     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1103                 _ ("Failed to create directory `%s' for storing egos\n"),
1104                 ego_directory);
1105   }
1106   GNUNET_DISK_directory_scan (ego_directory, &process_ego_file, NULL);
1107   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1108 }
1109
1110
1111 /**
1112  * Define "main" method using service macro.
1113  */
1114 GNUNET_SERVICE_MAIN (
1115   "identity",
1116   GNUNET_SERVICE_OPTION_NONE,
1117   &run,
1118   &client_connect_cb,
1119   &client_disconnect_cb,
1120   NULL,
1121   GNUNET_MQ_hd_fixed_size (start_message,
1122                            GNUNET_MESSAGE_TYPE_IDENTITY_START,
1123                            struct GNUNET_MessageHeader,
1124                            NULL),
1125   GNUNET_MQ_hd_var_size (lookup_message,
1126                          GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP,
1127                          struct LookupMessage,
1128                          NULL),
1129   GNUNET_MQ_hd_var_size (lookup_by_suffix_message,
1130                          GNUNET_MESSAGE_TYPE_IDENTITY_LOOKUP_BY_SUFFIX,
1131                          struct LookupMessage,
1132                          NULL),
1133   GNUNET_MQ_hd_var_size (get_default_message,
1134                          GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT,
1135                          struct GetDefaultMessage,
1136                          NULL),
1137   GNUNET_MQ_hd_var_size (set_default_message,
1138                          GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT,
1139                          struct SetDefaultMessage,
1140                          NULL),
1141   GNUNET_MQ_hd_var_size (create_message,
1142                          GNUNET_MESSAGE_TYPE_IDENTITY_CREATE,
1143                          struct CreateRequestMessage,
1144                          NULL),
1145   GNUNET_MQ_hd_var_size (rename_message,
1146                          GNUNET_MESSAGE_TYPE_IDENTITY_RENAME,
1147                          struct RenameMessage,
1148                          NULL),
1149   GNUNET_MQ_hd_var_size (delete_message,
1150                          GNUNET_MESSAGE_TYPE_IDENTITY_DELETE,
1151                          struct DeleteMessage,
1152                          NULL),
1153   GNUNET_MQ_handler_end ());
1154
1155
1156 /* end of gnunet-service-identity.c */