use NULL value in load_path_suffix to NOT load any files
[oweals/gnunet.git] / src / identity / identity_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2016 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/identity_api.c
23  * @brief api to interact with the identity service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_identity_service.h"
31 #include "identity.h"
32
33 #define LOG(kind, ...) GNUNET_log_from (kind, "identity-api", __VA_ARGS__)
34
35
36 /**
37  * Handle for an operation with the identity service.
38  */
39 struct GNUNET_IDENTITY_Operation
40 {
41   /**
42    * Main identity handle.
43    */
44   struct GNUNET_IDENTITY_Handle *h;
45
46   /**
47    * We keep operations in a DLL.
48    */
49   struct GNUNET_IDENTITY_Operation *next;
50
51   /**
52    * We keep operations in a DLL.
53    */
54   struct GNUNET_IDENTITY_Operation *prev;
55
56   /**
57    * Message to send to the identity service.
58    * Allocated at the end of this struct.
59    */
60   const struct GNUNET_MessageHeader *msg;
61
62   /**
63    * Continuation to invoke with the result of the transmission; @e cb
64    * and @e create_cont will be NULL in this case.
65    */
66   GNUNET_IDENTITY_Continuation cont;
67
68   /**
69    * Continuation to invoke with the result of the transmission; @e cb
70    * and @a cb will be NULL in this case.
71    */
72   GNUNET_IDENTITY_CreateContinuation create_cont;
73
74   /**
75    * Private key to return to @e create_cont, or NULL.
76    */
77   struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
78
79   /**
80    * Continuation to invoke with the result of the transmission for
81    * 'get' operations (@e cont and @a create_cont will be NULL in this case).
82    */
83   GNUNET_IDENTITY_Callback cb;
84
85   /**
86    * Closure for @e cont or @e cb.
87    */
88   void *cls;
89 };
90
91
92 /**
93  * Handle for the service.
94  */
95 struct GNUNET_IDENTITY_Handle
96 {
97   /**
98    * Configuration to use.
99    */
100   const struct GNUNET_CONFIGURATION_Handle *cfg;
101
102   /**
103    * Connection to service.
104    */
105   struct GNUNET_MQ_Handle *mq;
106
107   /**
108    * Hash map from the hash of the public key to the
109    * respective `GNUNET_IDENTITY_Ego` handle.
110    */
111   struct GNUNET_CONTAINER_MultiHashMap *egos;
112
113   /**
114    * Function to call when we receive updates.
115    */
116   GNUNET_IDENTITY_Callback cb;
117
118   /**
119    * Closure for @e cb.
120    */
121   void *cb_cls;
122
123   /**
124    * Head of active operations.
125    */
126   struct GNUNET_IDENTITY_Operation *op_head;
127
128   /**
129    * Tail of active operations.
130    */
131   struct GNUNET_IDENTITY_Operation *op_tail;
132
133   /**
134    * Task doing exponential back-off trying to reconnect.
135    */
136   struct GNUNET_SCHEDULER_Task *reconnect_task;
137
138   /**
139    * Time for next connect retry.
140    */
141   struct GNUNET_TIME_Relative reconnect_delay;
142
143   /**
144    * Are we polling for incoming messages right now?
145    */
146   int in_receive;
147 };
148
149
150 /**
151  * Obtain the ego representing 'anonymous' users.
152  *
153  * @return handle for the anonymous user, must not be freed
154  */
155 const struct GNUNET_IDENTITY_Ego *
156 GNUNET_IDENTITY_ego_get_anonymous ()
157 {
158   static struct GNUNET_IDENTITY_Ego anon;
159   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
160
161   if (NULL != anon.pk)
162     return &anon;
163   anon.pk = (struct GNUNET_CRYPTO_EcdsaPrivateKey *)
164             GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
165   GNUNET_CRYPTO_ecdsa_key_get_public (anon.pk, &pub);
166   GNUNET_CRYPTO_hash (&pub, sizeof(pub), &anon.id);
167   return &anon;
168 }
169
170
171 /**
172  * Try again to connect to the identity service.
173  *
174  * @param cls handle to the identity service.
175  */
176 static void
177 reconnect (void *cls);
178
179
180 /**
181  * Free ego from hash map.
182  *
183  * @param cls identity service handle
184  * @param key unused
185  * @param value ego to free
186  * @return #GNUNET_OK (continue to iterate)
187  */
188 static int
189 free_ego (void *cls, const struct GNUNET_HashCode *key, void *value)
190 {
191   struct GNUNET_IDENTITY_Handle *h = cls;
192   struct GNUNET_IDENTITY_Ego *ego = value;
193
194   if (NULL != h->cb)
195     h->cb (h->cb_cls, ego, &ego->ctx, NULL);
196   GNUNET_free (ego->pk);
197   GNUNET_free (ego->name);
198   GNUNET_assert (GNUNET_YES ==
199                  GNUNET_CONTAINER_multihashmap_remove (h->egos, key, value));
200   GNUNET_free (ego);
201   return GNUNET_OK;
202 }
203
204
205 /**
206  * Reschedule a connect attempt to the service.
207  *
208  * @param h transport service to reconnect
209  */
210 static void
211 reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
212 {
213   struct GNUNET_IDENTITY_Operation *op;
214
215   GNUNET_assert (NULL == h->reconnect_task);
216
217   if (NULL != h->mq)
218   {
219     GNUNET_MQ_destroy (h->mq);
220     h->mq = NULL;
221   }
222   while (NULL != (op = h->op_head))
223   {
224     GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, op);
225     if (NULL != op->cont)
226       op->cont (op->cls, "Error in communication with the identity service");
227     else if (NULL != op->cb)
228       op->cb (op->cls, NULL, NULL, NULL);
229     else if (NULL != op->create_cont)
230       op->create_cont (op->cls,
231                        NULL,
232                        "Failed to communicate with the identity service");
233     GNUNET_free_non_null (op->pk);
234     GNUNET_free (op);
235   }
236   GNUNET_CONTAINER_multihashmap_iterate (h->egos, &free_ego, h);
237   LOG (GNUNET_ERROR_TYPE_DEBUG,
238        "Scheduling task to reconnect to identity service in %s.\n",
239        GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
240   h->reconnect_task =
241     GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
242   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
243 }
244
245
246 /**
247  * Generic error handler, called with the appropriate error code and
248  * the same closure specified at the creation of the message queue.
249  * Not every message queue implementation supports an error handler.
250  *
251  * @param cls closure with the `struct GNUNET_IDENTITY_Handle *`
252  * @param error error code
253  */
254 static void
255 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
256 {
257   struct GNUNET_IDENTITY_Handle *h = cls;
258
259   reschedule_connect (h);
260 }
261
262
263 /**
264  * We received a result code from the service.  Check the message
265  * is well-formed.
266  *
267  * @param cls closure
268  * @param rcm result message received
269  * @return #GNUNET_OK if the message is well-formed
270  */
271 static int
272 check_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
273 {
274   if (sizeof(*rcm) != htons (rcm->header.size))
275     GNUNET_MQ_check_zero_termination (rcm);
276   return GNUNET_OK;
277 }
278
279
280 /**
281  * We received a result code from the service.
282  *
283  * @param cls closure
284  * @param rcm result message received
285  */
286 static void
287 handle_identity_result_code (void *cls, const struct ResultCodeMessage *rcm)
288 {
289   struct GNUNET_IDENTITY_Handle *h = cls;
290   struct GNUNET_IDENTITY_Operation *op;
291   uint16_t size = ntohs (rcm->header.size) - sizeof(*rcm);
292   const char *str = (0 == size) ? NULL : (const char *) &rcm[1];
293
294   op = h->op_head;
295   if (NULL == op)
296   {
297     GNUNET_break (0);
298     reschedule_connect (h);
299     return;
300   }
301   GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, op);
302   if (NULL != op->cont)
303     op->cont (op->cls, str);
304   else if (NULL != op->cb)
305     op->cb (op->cls, NULL, NULL, NULL);
306   else if (NULL != op->create_cont)
307     op->create_cont (op->cls, (NULL == str) ? op->pk : NULL, str);
308   GNUNET_free_non_null (op->pk);
309   GNUNET_free (op);
310 }
311
312
313 /**
314  * Check validity of identity update message.
315  *
316  * @param cls closure
317  * @param um message received
318  * @return #GNUNET_OK if the message is well-formed
319  */
320 static int
321 check_identity_update (void *cls, const struct UpdateMessage *um)
322 {
323   uint16_t size = ntohs (um->header.size);
324   uint16_t name_len = ntohs (um->name_len);
325   const char *str = (const char *) &um[1];
326
327   if ((size != name_len + sizeof(struct UpdateMessage)) ||
328       ((0 != name_len) && ('\0' != str[name_len - 1])))
329   {
330     GNUNET_break (0);
331     return GNUNET_SYSERR;
332   }
333   return GNUNET_OK;
334 }
335
336
337 /**
338  * Handle identity update message.
339  *
340  * @param cls closure
341  * @param um message received
342  */
343 static void
344 handle_identity_update (void *cls, const struct UpdateMessage *um)
345 {
346   struct GNUNET_IDENTITY_Handle *h = cls;
347   uint16_t name_len = ntohs (um->name_len);
348   const char *str = (0 == name_len) ? NULL : (const char *) &um[1];
349   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
350   struct GNUNET_HashCode id;
351   struct GNUNET_IDENTITY_Ego *ego;
352
353   if (GNUNET_YES == ntohs (um->end_of_list))
354   {
355     /* end of initial list of data */
356     if (NULL != h->cb)
357       h->cb (h->cb_cls, NULL, NULL, NULL);
358     return;
359   }
360   GNUNET_CRYPTO_ecdsa_key_get_public (&um->private_key, &pub);
361   GNUNET_CRYPTO_hash (&pub, sizeof(pub), &id);
362   ego = GNUNET_CONTAINER_multihashmap_get (h->egos, &id);
363   if (NULL == ego)
364   {
365     /* ego was created */
366     if (NULL == str)
367     {
368       /* deletion of unknown ego? not allowed */
369       GNUNET_break (0);
370       reschedule_connect (h);
371       return;
372     }
373     ego = GNUNET_new (struct GNUNET_IDENTITY_Ego);
374     ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
375     *ego->pk = um->private_key;
376     ego->name = GNUNET_strdup (str);
377     ego->id = id;
378     GNUNET_assert (GNUNET_YES ==
379                    GNUNET_CONTAINER_multihashmap_put (
380                      h->egos,
381                      &ego->id,
382                      ego,
383                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
384   }
385   if (NULL == str)
386   {
387     /* ego was deleted */
388     GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (h->egos,
389                                                                        &ego->id,
390                                                                        ego));
391   }
392   else
393   {
394     /* ego changed name */
395     GNUNET_free (ego->name);
396     ego->name = GNUNET_strdup (str);
397   }
398   /* inform application about change */
399   if (NULL != h->cb)
400     h->cb (h->cb_cls, ego, &ego->ctx, str);
401   /* complete deletion */
402   if (NULL == str)
403   {
404     GNUNET_free (ego->pk);
405     GNUNET_free (ego->name);
406     GNUNET_free (ego);
407   }
408 }
409
410
411 /**
412  * Function called when we receive a set default message from the
413  * service.
414  *
415  * @param cls closure
416  * @param sdm message received
417  * @return #GNUNET_OK if the message is well-formed
418  */
419 static int
420 check_identity_set_default (void *cls, const struct SetDefaultMessage *sdm)
421 {
422   uint16_t size = ntohs (sdm->header.size) - sizeof(*sdm);
423   uint16_t name_len = ntohs (sdm->name_len);
424   const char *str = (const char *) &sdm[1];
425
426   if ((size != name_len) || ((0 != name_len) && ('\0' != str[name_len - 1])))
427   {
428     GNUNET_break (0);
429     return GNUNET_SYSERR;
430   }
431   GNUNET_break (0 == ntohs (sdm->reserved));
432   return GNUNET_OK;
433 }
434
435
436 /**
437  * Type of a function to call when we receive a message
438  * from the service.
439  *
440  * @param cls closure
441  * @param sdm message received
442  */
443 static void
444 handle_identity_set_default (void *cls, const struct SetDefaultMessage *sdm)
445 {
446   struct GNUNET_IDENTITY_Handle *h = cls;
447   struct GNUNET_IDENTITY_Operation *op;
448   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
449   struct GNUNET_HashCode id;
450   struct GNUNET_IDENTITY_Ego *ego;
451
452   GNUNET_CRYPTO_ecdsa_key_get_public (&sdm->private_key, &pub);
453   GNUNET_CRYPTO_hash (&pub, sizeof(pub), &id);
454   ego = GNUNET_CONTAINER_multihashmap_get (h->egos, &id);
455   if (NULL == ego)
456   {
457     GNUNET_break (0);
458     reschedule_connect (h);
459     return;
460   }
461   op = h->op_head;
462   if (NULL == op)
463   {
464     GNUNET_break (0);
465     reschedule_connect (h);
466     return;
467   }
468   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469               "Received SET_DEFAULT message from identity service\n");
470   GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, op);
471   if (NULL != op->cb)
472     op->cb (op->cls, ego, &ego->ctx, ego->name);
473   GNUNET_free (op);
474 }
475
476
477 /**
478  * Try again to connect to the identity service.
479  *
480  * @param cls handle to the identity service.
481  */
482 static void
483 reconnect (void *cls)
484 {
485   struct GNUNET_IDENTITY_Handle *h = cls;
486   struct GNUNET_MQ_MessageHandler handlers[] =
487   { GNUNET_MQ_hd_var_size (identity_result_code,
488                            GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE,
489                            struct ResultCodeMessage,
490                            h),
491     GNUNET_MQ_hd_var_size (identity_update,
492                            GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE,
493                            struct UpdateMessage,
494                            h),
495     GNUNET_MQ_hd_var_size (identity_set_default,
496                            GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT,
497                            struct SetDefaultMessage,
498                            h),
499     GNUNET_MQ_handler_end () };
500   struct GNUNET_MQ_Envelope *env;
501   struct GNUNET_MessageHeader *msg;
502
503   h->reconnect_task = NULL;
504   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to identity service.\n");
505   GNUNET_assert (NULL == h->mq);
506   h->mq =
507     GNUNET_CLIENT_connect (h->cfg, "identity", handlers, &mq_error_handler, h);
508   if (NULL == h->mq)
509     return;
510   if (NULL != h->cb)
511   {
512     env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_IDENTITY_START);
513     GNUNET_MQ_send (h->mq, env);
514   }
515 }
516
517
518 /**
519  * Connect to the identity service.
520  *
521  * @param cfg the configuration to use
522  * @param cb function to call on all identity events, can be NULL
523  * @param cb_cls closure for @a cb
524  * @return handle to use
525  */
526 struct GNUNET_IDENTITY_Handle *
527 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
528                          GNUNET_IDENTITY_Callback cb,
529                          void *cb_cls)
530 {
531   struct GNUNET_IDENTITY_Handle *h;
532
533   h = GNUNET_new (struct GNUNET_IDENTITY_Handle);
534   h->cfg = cfg;
535   h->cb = cb;
536   h->cb_cls = cb_cls;
537   h->egos = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
538   reconnect (h);
539   if (NULL == h->mq)
540   {
541     GNUNET_free (h);
542     return NULL;
543   }
544   return h;
545 }
546
547
548 /**
549  * Obtain the ECC key associated with a ego.
550  *
551  * @param ego the ego
552  * @return associated ECC key, valid as long as the ego is valid
553  */
554 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
555 GNUNET_IDENTITY_ego_get_private_key (const struct GNUNET_IDENTITY_Ego *ego)
556 {
557   return ego->pk;
558 }
559
560
561 /**
562  * Get the identifier (public key) of an ego.
563  *
564  * @param ego identity handle with the private key
565  * @param pk set to ego's public key
566  */
567 void
568 GNUNET_IDENTITY_ego_get_public_key (const struct GNUNET_IDENTITY_Ego *ego,
569                                     struct GNUNET_CRYPTO_EcdsaPublicKey *pk)
570 {
571   GNUNET_CRYPTO_ecdsa_key_get_public (ego->pk, pk);
572 }
573
574
575 /**
576  * Obtain the identity that is currently preferred/default
577  * for a service.
578  *
579  * @param h identity service to query
580  * @param service_name for which service is an identity wanted
581  * @param cb function to call with the result (will only be called once)
582  * @param cb_cls closure for @a cb
583  * @return handle to abort the operation
584  */
585 struct GNUNET_IDENTITY_Operation *
586 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *h,
587                      const char *service_name,
588                      GNUNET_IDENTITY_Callback cb,
589                      void *cb_cls)
590 {
591   struct GNUNET_IDENTITY_Operation *op;
592   struct GNUNET_MQ_Envelope *env;
593   struct GetDefaultMessage *gdm;
594   size_t slen;
595
596   if (NULL == h->mq)
597     return NULL;
598   GNUNET_assert (NULL != h->cb);
599   slen = strlen (service_name) + 1;
600   if (slen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(struct GetDefaultMessage))
601   {
602     GNUNET_break (0);
603     return NULL;
604   }
605   op = GNUNET_new (struct GNUNET_IDENTITY_Operation);
606   op->h = h;
607   op->cb = cb;
608   op->cls = cb_cls;
609   GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, op);
610   env =
611     GNUNET_MQ_msg_extra (gdm, slen, GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
612   gdm->name_len = htons (slen);
613   gdm->reserved = htons (0);
614   GNUNET_memcpy (&gdm[1], service_name, slen);
615   GNUNET_MQ_send (h->mq, env);
616   return op;
617 }
618
619
620 /**
621  * Set the preferred/default identity for a service.
622  *
623  * @param h identity service to inform
624  * @param service_name for which service is an identity set
625  * @param ego new default identity to be set for this service
626  * @param cont function to call once the operation finished
627  * @param cont_cls closure for @a cont
628  * @return handle to abort the operation
629  */
630 struct GNUNET_IDENTITY_Operation *
631 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *h,
632                      const char *service_name,
633                      struct GNUNET_IDENTITY_Ego *ego,
634                      GNUNET_IDENTITY_Continuation cont,
635                      void *cont_cls)
636 {
637   struct GNUNET_IDENTITY_Operation *op;
638   struct GNUNET_MQ_Envelope *env;
639   struct SetDefaultMessage *sdm;
640   size_t slen;
641
642   if (NULL == h->mq)
643     return NULL;
644   GNUNET_assert (NULL != h->cb);
645   slen = strlen (service_name) + 1;
646   if (slen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(struct SetDefaultMessage))
647   {
648     GNUNET_break (0);
649     return NULL;
650   }
651   op = GNUNET_new (struct GNUNET_IDENTITY_Operation);
652   op->h = h;
653   op->cont = cont;
654   op->cls = cont_cls;
655   GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, op);
656   env =
657     GNUNET_MQ_msg_extra (sdm, slen, GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
658   sdm->name_len = htons (slen);
659   sdm->reserved = htons (0);
660   sdm->private_key = *ego->pk;
661   GNUNET_memcpy (&sdm[1], service_name, slen);
662   GNUNET_MQ_send (h->mq, env);
663   return op;
664 }
665
666
667 /**
668  * Create a new identity with the given name.
669  *
670  * @param h identity service to use
671  * @param name desired name
672  * @param cont function to call with the result (will only be called once)
673  * @param cont_cls closure for @a cont
674  * @return handle to abort the operation
675  */
676 struct GNUNET_IDENTITY_Operation *
677 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *h,
678                         const char *name,
679                         GNUNET_IDENTITY_CreateContinuation cont,
680                         void *cont_cls)
681 {
682   struct GNUNET_IDENTITY_Operation *op;
683   struct GNUNET_MQ_Envelope *env;
684   struct CreateRequestMessage *crm;
685   struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
686   size_t slen;
687
688   if (NULL == h->mq)
689     return NULL;
690   slen = strlen (name) + 1;
691   if (slen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(struct CreateRequestMessage))
692   {
693     GNUNET_break (0);
694     return NULL;
695   }
696   op = GNUNET_new (struct GNUNET_IDENTITY_Operation);
697   op->h = h;
698   op->create_cont = cont;
699   op->cls = cont_cls;
700   GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, op);
701   env = GNUNET_MQ_msg_extra (crm, slen, GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
702   crm->name_len = htons (slen);
703   crm->reserved = htons (0);
704   pk = GNUNET_CRYPTO_ecdsa_key_create ();
705   crm->private_key = *pk;
706   op->pk = pk;
707   GNUNET_memcpy (&crm[1], name, slen);
708   GNUNET_MQ_send (h->mq, env);
709   return op;
710 }
711
712
713 /**
714  * Renames an existing identity.
715  *
716  * @param h identity service to use
717  * @param old_name old name
718  * @param new_name desired new name
719  * @param cb function to call with the result (will only be called once)
720  * @param cb_cls closure for @a cb
721  * @return handle to abort the operation
722  */
723 struct GNUNET_IDENTITY_Operation *
724 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *h,
725                         const char *old_name,
726                         const char *new_name,
727                         GNUNET_IDENTITY_Continuation cb,
728                         void *cb_cls)
729 {
730   struct GNUNET_IDENTITY_Operation *op;
731   struct GNUNET_MQ_Envelope *env;
732   struct RenameMessage *grm;
733   size_t slen_old;
734   size_t slen_new;
735   char *dst;
736
737   if (NULL == h->mq)
738     return NULL;
739   slen_old = strlen (old_name) + 1;
740   slen_new = strlen (new_name) + 1;
741   if ((slen_old >= GNUNET_MAX_MESSAGE_SIZE) ||
742       (slen_new >= GNUNET_MAX_MESSAGE_SIZE) ||
743       (slen_old + slen_new >=
744        GNUNET_MAX_MESSAGE_SIZE - sizeof(struct RenameMessage)))
745   {
746     GNUNET_break (0);
747     return NULL;
748   }
749   op = GNUNET_new (struct GNUNET_IDENTITY_Operation);
750   op->h = h;
751   op->cont = cb;
752   op->cls = cb_cls;
753   GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, op);
754   env = GNUNET_MQ_msg_extra (grm,
755                              slen_old + slen_new,
756                              GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
757   grm->old_name_len = htons (slen_old);
758   grm->new_name_len = htons (slen_new);
759   dst = (char *) &grm[1];
760   GNUNET_memcpy (dst, old_name, slen_old);
761   GNUNET_memcpy (&dst[slen_old], new_name, slen_new);
762   GNUNET_MQ_send (h->mq, env);
763   return op;
764 }
765
766
767 /**
768  * Delete an existing identity.
769  *
770  * @param h identity service to use
771  * @param name name of the identity to delete
772  * @param cb function to call with the result (will only be called once)
773  * @param cb_cls closure for @a cb
774  * @return handle to abort the operation
775  */
776 struct GNUNET_IDENTITY_Operation *
777 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *h,
778                         const char *name,
779                         GNUNET_IDENTITY_Continuation cb,
780                         void *cb_cls)
781 {
782   struct GNUNET_IDENTITY_Operation *op;
783   struct GNUNET_MQ_Envelope *env;
784   struct DeleteMessage *gdm;
785   size_t slen;
786
787   if (NULL == h->mq)
788     return NULL;
789   slen = strlen (name) + 1;
790   if (slen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(struct DeleteMessage))
791   {
792     GNUNET_break (0);
793     return NULL;
794   }
795   op = GNUNET_new (struct GNUNET_IDENTITY_Operation);
796   op->h = h;
797   op->cont = cb;
798   op->cls = cb_cls;
799   GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, op);
800   env = GNUNET_MQ_msg_extra (gdm, slen, GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
801   gdm->name_len = htons (slen);
802   gdm->reserved = htons (0);
803   GNUNET_memcpy (&gdm[1], name, slen);
804   GNUNET_MQ_send (h->mq, env);
805   return op;
806 }
807
808
809 /**
810  * Cancel an identity operation. Note that the operation MAY still
811  * be executed; this merely cancels the continuation; if the request
812  * was already transmitted, the service may still choose to complete
813  * the operation.
814  *
815  * @param op operation to cancel
816  */
817 void
818 GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
819 {
820   op->cont = NULL;
821   op->cb = NULL;
822   op->create_cont = NULL;
823   if (NULL != op->pk)
824   {
825     GNUNET_free (op->pk);
826     op->pk = NULL;
827   }
828 }
829
830
831 /**
832  * Disconnect from identity service
833  *
834  * @param h handle to destroy
835  */
836 void
837 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
838 {
839   struct GNUNET_IDENTITY_Operation *op;
840
841   GNUNET_assert (NULL != h);
842   if (h->reconnect_task != NULL)
843   {
844     GNUNET_SCHEDULER_cancel (h->reconnect_task);
845     h->reconnect_task = NULL;
846   }
847   if (NULL != h->egos)
848   {
849     GNUNET_CONTAINER_multihashmap_iterate (h->egos, &free_ego, h);
850     GNUNET_CONTAINER_multihashmap_destroy (h->egos);
851     h->egos = NULL;
852   }
853   while (NULL != (op = h->op_head))
854   {
855     GNUNET_break (NULL == op->cont);
856     GNUNET_CONTAINER_DLL_remove (h->op_head, h->op_tail, op);
857     GNUNET_free_non_null (op->pk);
858     GNUNET_free (op);
859   }
860   if (NULL != h->mq)
861   {
862     GNUNET_MQ_destroy (h->mq);
863     h->mq = NULL;
864   }
865   GNUNET_free (h);
866 }
867
868
869 /* end of identity_api.c */