25352170b5927099eaa439cd5f143242026847b9
[oweals/gnunet.git] / src / identity / gnunet-service-identity.c
1 /*
2   This file is part of GNUnet.
3   (C) 2013 Christian Grothoff (and other contributing authors)
4
5   GNUnet is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published
7   by the Free Software Foundation; either version 3, or (at your
8   option) any later version.
9
10   GNUnet is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with GNUnet; see the file COPYING.  If not, write to the
17   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18   Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file identity/gnunet-service-identity.c
23  * @brief identity management service
24  * @author Christian Grothoff
25  *
26  * The purpose of this service is to manage private keys that
27  * represent the various egos/pseudonyms/identities of a GNUnet user.
28  *
29  * TODO:
30  * - disk operations
31  * - default identity set/get handlers
32  */
33 #include "platform.h"
34 #include "gnunet_util_lib.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_identity_service.h"
39 #include "identity.h"
40
41
42 /**
43  * Information we keep about each ego.
44  */
45 struct Ego
46 {
47
48   /**
49    * We keep egos in a DLL.
50    */ 
51   struct Ego *next;
52
53   /**
54    * We keep egos in a DLL.
55    */ 
56   struct Ego *prev;
57
58   /**
59    * Private key of the ego.
60    */
61   struct GNUNET_CRYPTO_EccPrivateKey *pk;
62
63   /**
64    * String identifier for the ego.
65    */
66   char *identifier;
67
68 };
69
70
71 /**
72  * Handle to our current configuration.
73  */
74 static const struct GNUNET_CONFIGURATION_Handle *cfg;
75
76 /**
77  * Handle to subsystem configuration which for each subsystem contains
78  * the name of the default ego.
79  */
80 static struct GNUNET_CONFIGURATION_Handle *subsystem_cfg;
81
82 /**
83  * Handle to the statistics service.
84  */
85 static struct GNUNET_STATISTICS_Handle *stats;
86
87 /**
88  * Notification context, simplifies client broadcasts.
89  */
90 static struct GNUNET_SERVER_NotificationContext *nc;
91
92 /**
93  * Directory where we store the identities.
94  */
95 static char *ego_directory;
96
97 /**
98  * Configuration file name where subsystem information is kept.
99  */
100 static char *subsystem_cfg_file;
101
102 /**
103  * Head of DLL of all egos.
104  */
105 static struct Ego *ego_head;
106
107 /**
108  * Tail of DLL of all egos.
109  */
110 static struct Ego *ego_tail;
111
112
113 /**
114  * Task run during shutdown.
115  *
116  * @param cls unused
117  * @param tc unused
118  */
119 static void
120 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 {
122   struct Ego *e;
123
124   if (NULL != nc)
125   {
126     GNUNET_SERVER_notification_context_destroy (nc);
127     nc = NULL;
128   }
129   if (NULL != stats)
130   {
131     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
132     stats = NULL;
133   }
134   GNUNET_CONFIGURATION_destroy (subsystem_cfg);
135   subsystem_cfg = NULL;
136   GNUNET_free (subsystem_cfg_file);
137   subsystem_cfg_file = NULL;
138   GNUNET_free (ego_directory);
139   ego_directory = NULL;
140   while (NULL != (e = ego_head))
141   {
142     GNUNET_CONTAINER_DLL_remove (ego_head, ego_tail, e);
143     GNUNET_CRYPTO_ecc_key_free (e->pk);
144     GNUNET_free (e);
145   }
146 }
147
148
149 /**
150  * Send a result code back to the client.
151  *
152  * @param client client that should receive the result code
153  * @param result_code code to transmit
154  * @param emsg error message to include (or NULL for none)
155  */
156 static void
157 send_result_code (struct GNUNET_SERVER_Client *client,
158                   uint32_t result_code,
159                   const char *emsg)
160 {
161   struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
162   size_t elen;
163
164   if (NULL == emsg)
165     elen = 0;
166   else
167     elen = strlen (emsg) + 1;
168   rcm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
169   rcm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE);
170   rcm->header.size = htons (sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) + elen);
171   rcm->result_code = htonl (result_code);
172   memcpy (&rcm[1], emsg, elen);
173   GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header, GNUNET_YES);  
174   GNUNET_free (rcm);
175 }
176
177
178 /**
179  * Create an update message with information about the current state of an ego.
180  *
181  * @param ego ego to create message for
182  * @return corresponding update message
183  */
184 static struct GNUNET_IDENTITY_UpdateMessage *
185 create_update_message (struct Ego *ego)
186 {
187   struct GNUNET_IDENTITY_UpdateMessage *um;
188   char *str;
189   uint16_t pk_len;
190   size_t name_len;
191   struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
192
193   name_len = (NULL == ego->identifier) ? 0 : (strlen (ego->identifier) + 1);
194   enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
195   pk_len = ntohs (enc->size);
196   um = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
197   um->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE);
198   um->header.size = htons (sizeof (struct GNUNET_IDENTITY_UpdateMessage) + pk_len + name_len);
199   um->name_len = htons (name_len);
200   um->pk_len = htons (pk_len);
201   str = (char *) &um[1];
202   memcpy (str, enc, pk_len);
203   memcpy (&str[pk_len], ego->identifier, name_len);
204   GNUNET_free (enc);
205   return um;
206 }
207
208
209 /**
210  * Create a set default message with information about the current state of an ego.
211  *
212  * @param ego ego to create message for
213  * @param servicename name of the service to provide in the message
214  * @return corresponding set default message
215  */
216 static struct GNUNET_IDENTITY_SetDefaultMessage *
217 create_set_default_message (struct Ego *ego,
218                             const char *servicename)
219 {
220   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
221   char *str;
222   uint16_t pk_len;
223   size_t name_len;
224   struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
225
226   name_len = (NULL == servicename) ? 0 : (strlen (servicename) + 1);
227   enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
228   pk_len = ntohs (enc->size);
229   sdm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
230   sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
231   sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) + pk_len + name_len);
232   sdm->name_len = htons (name_len);
233   sdm->pk_len = htons (pk_len);
234   str = (char *) &sdm[1];
235   memcpy (str, enc, pk_len);
236   memcpy (&str[pk_len], servicename, name_len);
237   GNUNET_free (enc);
238   return sdm;
239 }
240
241
242 /**
243  * Handler for START message from client, sends information
244  * about all identities to the client immediately and 
245  * adds the client to the notification context for future
246  * updates.
247  *
248  * @param cls unused
249  * @param client who sent the message
250  * @param message the message received
251  */
252 static void
253 handle_start_message (void *cls, struct GNUNET_SERVER_Client *client,
254                       const struct GNUNET_MessageHeader *message)
255 {
256   struct GNUNET_IDENTITY_UpdateMessage *um;
257   struct Ego *ego;
258
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
260               "Received START message from client\n");
261   GNUNET_SERVER_notification_context_add (nc, client);
262   for (ego = ego_head; NULL != ego; ego = ego->next)
263   {
264     um = create_update_message (ego);
265     GNUNET_SERVER_notification_context_unicast (nc, client, &um->header, GNUNET_YES);
266     GNUNET_free (um);
267   }
268   GNUNET_SERVER_receive_done (client, GNUNET_OK);
269 }
270
271
272 /**
273  * Handler for GET_DEFAULT message from client, returns
274  * default identity for some service.
275  *
276  * @param cls unused
277  * @param client who sent the message
278  * @param message the message received
279  */
280 static void
281 handle_get_default_message (void *cls, struct GNUNET_SERVER_Client *client,
282                             const struct GNUNET_MessageHeader *message)
283 {
284   const struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
285   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
286   uint16_t size;
287   uint16_t name_len;
288   struct Ego *ego;
289   const char *name;
290   char *identifier;
291
292   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
293               "Received GET_DEFAULT message from client\n");
294   size = ntohs (message->size);
295   if (size <= sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
296   {
297     GNUNET_break (0);
298     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
299     return;
300   }
301   gdm = (const struct GNUNET_IDENTITY_GetDefaultMessage *) message;
302   name = (const char *) &gdm[1];
303   name_len = ntohs (gdm->name_len);
304   if ( (name_len + sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) != size) ||
305        (0 != ntohs (gdm->reserved)) ||
306        ('\0' != name[name_len - 1]) )
307   {
308     GNUNET_break (0);
309     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
310     return;
311   }
312   if (GNUNET_OK !=
313       GNUNET_CONFIGURATION_get_value_string (subsystem_cfg,
314                                              name,
315                                              "DEFAULT_IDENTIFIER",
316                                              &identifier))
317   {
318     send_result_code (client, 1, gettext_noop ("no default known"));
319     GNUNET_SERVER_receive_done (client, GNUNET_OK);   
320     return;
321   }
322   for (ego = ego_head; NULL != ego; ego = ego->next)
323   {
324     if (0 == strcmp (ego->identifier,
325                      identifier))
326     {
327       sdm = create_set_default_message (ego,
328                                         name);
329       GNUNET_SERVER_notification_context_broadcast (nc, &sdm->header, GNUNET_YES);
330       GNUNET_free (sdm);
331       GNUNET_SERVER_receive_done (client, GNUNET_OK);
332       return;
333     }
334   }
335   send_result_code (client, 1, 
336                     gettext_noop ("default configured, but ego unknown (internal error)"));
337   GNUNET_SERVER_receive_done (client, GNUNET_OK);
338 }
339
340
341 /**
342  * Handler for SET_DEFAULT message from client, updates
343  * default identity for some service.
344  *
345  * @param cls unused
346  * @param client who sent the message
347  * @param message the message received
348  */
349 static void
350 handle_set_default_message (void *cls, struct GNUNET_SERVER_Client *client,
351                             const struct GNUNET_MessageHeader *message)
352 {
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
354               "Received SET_DEFAULT message from client\n");
355   // setup_estimate_message (&em);
356   // GNUNET_SERVER_notification_context_unicast (nc, client, &em.header, GNUNET_YES);
357   GNUNET_break (0); // not implemented!
358   GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
359 }
360
361
362 /**
363  * Send an updated message for the given ego to all listeners.
364  *
365  * @param ego ego to send the update for
366  */
367 static void
368 notify_listeners (struct Ego *ego)
369 {
370   struct GNUNET_IDENTITY_UpdateMessage *um;
371
372   um = create_update_message (ego);
373   GNUNET_SERVER_notification_context_broadcast (nc, &um->header, GNUNET_YES);
374   GNUNET_free (um);
375 }
376
377
378 /**
379  * Handler for CREATE message from client, creates
380  * new identity.
381  *
382  * @param cls unused
383  * @param client who sent the message
384  * @param message the message received
385  */
386 static void
387 handle_create_message (void *cls, struct GNUNET_SERVER_Client *client,
388                        const struct GNUNET_MessageHeader *message)
389 {
390   const struct GNUNET_IDENTITY_CreateRequestMessage *crm;
391   uint16_t size;
392   uint16_t name_len;
393   uint16_t pk_len;
394   struct Ego *ego;
395   const char *str;
396   struct GNUNET_CRYPTO_EccPrivateKey *pk;
397
398   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
399               "Received CREATE message from client\n");
400   size = ntohs (message->size);
401   if (size <= sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
402   {
403     GNUNET_break (0);
404     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
405     return;
406   }
407   crm = (const struct GNUNET_IDENTITY_CreateRequestMessage *) message;
408   name_len = ntohs (crm->name_len);
409   pk_len = ntohs (crm->pk_len);
410   str = (const char *) &crm[1];
411   if ( (name_len + pk_len + sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) != size) ||
412        (NULL == (pk = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES))) )
413   {
414     GNUNET_break (0);
415     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
416     return;
417   }
418   str = &str[pk_len];
419   if ('\0' != str[name_len - 1])
420   {
421     GNUNET_break (0);
422     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
423     return;
424   }
425   for (ego = ego_head; NULL != ego; ego = ego->next)
426   {
427     if (0 == strcmp (ego->identifier,
428                      str))
429     {
430       send_result_code (client, 1, gettext_noop ("identifier already in use for another ego"));
431       GNUNET_SERVER_receive_done (client, GNUNET_OK);
432       GNUNET_CRYPTO_ecc_key_free (pk);
433       return;
434     }
435   }
436   ego = GNUNET_new (struct Ego);
437   ego->pk = pk;
438   ego->identifier = GNUNET_strdup (str);
439   GNUNET_CONTAINER_DLL_insert (ego_head,
440                                ego_tail,
441                                ego);
442   send_result_code (client, 0, NULL);
443   /* FIXME: also write to file! */
444   notify_listeners (ego);  
445   GNUNET_SERVER_receive_done (client, GNUNET_OK);
446 }
447
448
449
450 /**
451  * Handler for RENAME message from client, creates
452  * new identity.
453  *
454  * @param cls unused
455  * @param client who sent the message
456  * @param message the message received
457  */
458 static void
459 handle_rename_message (void *cls, struct GNUNET_SERVER_Client *client,
460                        const struct GNUNET_MessageHeader *message)
461 {
462   const struct GNUNET_IDENTITY_RenameMessage *rm;
463   uint16_t size;
464   uint16_t old_name_len;
465   uint16_t new_name_len;
466   struct Ego *ego;
467   const char *old_name;
468   const char *new_name;
469
470   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
471               "Received RENAME message from client\n");
472   size = ntohs (message->size);
473   if (size <= sizeof (struct GNUNET_IDENTITY_RenameMessage))
474   {
475     GNUNET_break (0);
476     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
477     return;
478   }
479   rm = (const struct GNUNET_IDENTITY_RenameMessage *) message;
480   old_name_len = ntohs (rm->old_name_len);
481   new_name_len = ntohs (rm->new_name_len);
482   old_name = (const char *) &rm[1];
483   new_name = &old_name[old_name_len];
484   if ( (old_name_len + new_name_len + sizeof (struct GNUNET_IDENTITY_RenameMessage) != size) ||
485        ('\0' != old_name[old_name_len - 1]) ||
486        ('\0' != new_name[new_name_len - 1]) )
487   {
488     GNUNET_break (0);
489     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
490     return;
491   }
492   for (ego = ego_head; NULL != ego; ego = ego->next)
493   {
494     if (0 == strcmp (ego->identifier,
495                      old_name))
496     {
497       GNUNET_free (ego->identifier);
498       ego->identifier = GNUNET_strdup (new_name);
499       /* FIXME: also rename file! */
500       notify_listeners (ego);
501       send_result_code (client, 0, NULL);
502       GNUNET_SERVER_receive_done (client, GNUNET_OK);
503       return;
504     }
505   }
506
507   send_result_code (client, 1, gettext_noop ("no matching ego found"));
508   GNUNET_SERVER_receive_done (client, GNUNET_OK);
509 }
510
511
512 /**
513  * Handler for DELETE message from client, creates
514  * new identity.
515  *
516  * @param cls unused
517  * @param client who sent the message
518  * @param message the message received
519  */
520 static void
521 handle_delete_message (void *cls, struct GNUNET_SERVER_Client *client,
522                        const struct GNUNET_MessageHeader *message)
523 {
524   const struct GNUNET_IDENTITY_DeleteMessage *dm;
525   uint16_t size;
526   uint16_t name_len;
527   struct Ego *ego;
528   const char *name;
529
530   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
531               "Received DELETE message from client\n");
532   size = ntohs (message->size);
533   if (size <= sizeof (struct GNUNET_IDENTITY_DeleteMessage))
534   {
535     GNUNET_break (0);
536     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
537     return;
538   }
539   dm = (const struct GNUNET_IDENTITY_DeleteMessage *) message;
540   name = (const char *) &dm[1];
541   name_len = ntohs (dm->name_len);
542   if ( (name_len + sizeof (struct GNUNET_IDENTITY_DeleteMessage) != size) ||
543        (0 != ntohs (dm->reserved)) ||
544        ('\0' != name[name_len - 1]) )
545   {
546     GNUNET_break (0);
547     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
548     return;
549   }
550   for (ego = ego_head; NULL != ego; ego = ego->next)
551   {
552     if (0 == strcmp (ego->identifier,
553                      name))
554     {
555       GNUNET_CONTAINER_DLL_remove (ego_head,
556                                    ego_tail,
557                                    ego);
558       /* FIXME: also delete file! */
559       GNUNET_free (ego->identifier);
560       ego->identifier = NULL;
561       notify_listeners (ego);
562       GNUNET_CRYPTO_ecc_key_free (ego->pk);
563       GNUNET_free (ego);
564       send_result_code (client, 0, NULL);
565       GNUNET_SERVER_receive_done (client, GNUNET_OK);
566       return;
567     }
568   }
569
570   send_result_code (client, 1, gettext_noop ("no matching ego found"));
571   GNUNET_SERVER_receive_done (client, GNUNET_OK);
572 }
573
574
575 /**
576  * Process the given file from the "EGODIR".  Parses the file
577  * and creates the respective 'struct Ego' in memory.
578  *
579  * @param cls NULL
580  * @param filename name of the file to parse
581  * @return GNUNET_OK to continue to iterate,
582  *  GNUNET_NO to stop iteration with no error,
583  *  GNUNET_SYSERR to abort iteration with error!
584  */
585 static int
586 process_ego_file (void *cls,
587                   const char *filename)
588 {
589   GNUNET_break (0); // not implemented
590   return GNUNET_OK;
591 }
592
593
594 /**
595  * Handle network size estimate clients.
596  *
597  * @param cls closure
598  * @param server the initialized server
599  * @param c configuration to use
600  */
601 static void
602 run (void *cls, 
603      struct GNUNET_SERVER_Handle *server,
604      const struct GNUNET_CONFIGURATION_Handle *c)
605 {
606   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
607     {&handle_start_message, NULL,
608      GNUNET_MESSAGE_TYPE_IDENTITY_START, sizeof (struct GNUNET_MessageHeader)},
609     {&handle_get_default_message, NULL,
610      GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT, 0},
611     {&handle_set_default_message, NULL,
612      GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT, 0},
613     {&handle_create_message, NULL,
614      GNUNET_MESSAGE_TYPE_IDENTITY_CREATE, 0},
615     {&handle_rename_message, NULL,
616      GNUNET_MESSAGE_TYPE_IDENTITY_RENAME, 0},
617     {&handle_delete_message, NULL,
618      GNUNET_MESSAGE_TYPE_IDENTITY_DELETE, 0},
619     {NULL, NULL, 0, 0}
620   };
621
622   cfg = c;
623   if (GNUNET_OK !=
624       GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
625                                                "EGODIR",
626                                                &ego_directory))
627   {
628     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "EGODIR");
629     GNUNET_SCHEDULER_shutdown ();
630     return;
631   }
632   if (GNUNET_OK !=
633       GNUNET_CONFIGURATION_get_value_filename (cfg, "identity",
634                                                "SUBSYSTEM_CFG",
635                                                &subsystem_cfg_file))
636   {
637     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "identity", "SUBSYSTEM_CFG");
638     GNUNET_SCHEDULER_shutdown ();
639     return;
640   }
641   subsystem_cfg = GNUNET_CONFIGURATION_create ();
642   if ( (GNUNET_YES ==
643         GNUNET_DISK_file_test (subsystem_cfg_file)) &&
644        (GNUNET_OK != 
645         GNUNET_CONFIGURATION_parse (subsystem_cfg,
646                                     subsystem_cfg_file)) )
647   {
648     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
649                 _("Failed to parse subsystem identity configuration file `%s'\n"),
650                 subsystem_cfg_file);
651     GNUNET_SCHEDULER_shutdown ();
652     return;
653   }
654   stats = GNUNET_STATISTICS_create ("identity", cfg);
655   GNUNET_SERVER_add_handlers (server, handlers);
656   nc = GNUNET_SERVER_notification_context_create (server, 1);
657   GNUNET_DISK_directory_scan (ego_directory,
658                               &process_ego_file,
659                               NULL);
660   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
661                                 NULL);
662 }
663
664
665 /**
666  * The main function for the network size estimation service.
667  *
668  * @param argc number of arguments from the command line
669  * @param argv command line arguments
670  * @return 0 ok, 1 on error
671  */
672 int
673 main (int argc, char *const *argv)
674 {
675   return (GNUNET_OK ==
676           GNUNET_SERVICE_run (argc, argv, "identity", 
677                               GNUNET_SERVICE_OPTION_NONE,
678                               &run, NULL)) ? 0 : 1;
679 }
680
681
682 /* end of gnunet-service-identity.c */