aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_ego_store.c
blob: 3eb313b08dae4fa5b5f47b2631fb8921bd3fd682 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
   This file is part of GNUnet.
   Copyright (C) 2020--2022 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */
/**
 * @author Tobias Frisch
 * @file src/messenger/gnunet-service-messenger_ego_store.c
 * @brief GNUnet MESSENGER service
 */

#include "platform.h"
#include "gnunet-service-messenger_ego_store.h"

#include "gnunet-service-messenger_handle.h"

static void
callback_update_ego (void *cls,
                     struct GNUNET_IDENTITY_Ego *ego,
                     void **ctx,
                     const char *identifier)
{
  if ((!ctx) || (!identifier))
    return;

  struct GNUNET_MESSENGER_EgoStore *store = cls;

  if (ego)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New ego in use: '%s'\n", identifier);
    update_store_ego (store, identifier, GNUNET_IDENTITY_ego_get_private_key (ego));
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ego got deleted: '%s'\n", identifier);
    delete_store_ego (store, identifier);
  }
}

void
init_ego_store(struct GNUNET_MESSENGER_EgoStore *store,
               const struct GNUNET_CONFIGURATION_Handle *config)
{
  GNUNET_assert ((store) && (config));

  store->cfg = config;
  store->identity = GNUNET_IDENTITY_connect (config, &callback_update_ego, store);
  store->egos = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
  store->handles = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);

  store->lu_start = NULL;
  store->lu_end = NULL;

  store->op_start = NULL;
  store->op_end = NULL;
}

static int
iterate_destroy_egos (void *cls,
                      const struct GNUNET_HashCode *key,
                      void *value)
{
  struct GNUNET_MESSENGER_Ego *ego = value;
  GNUNET_free(ego);
  return GNUNET_YES;
}

void
clear_ego_store(struct GNUNET_MESSENGER_EgoStore *store)
{
  GNUNET_assert (store);

  struct GNUNET_MESSENGER_EgoOperation *op;

  while (store->op_start)
  {
    op = store->op_start;

    GNUNET_IDENTITY_cancel (op->operation);
    GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, op);

    if (op->identifier)
      GNUNET_free (op->identifier);

    GNUNET_free (op);
  }

  struct GNUNET_MESSENGER_EgoLookup *lu;

  while (store->lu_start)
  {
    lu = store->lu_start;

    GNUNET_IDENTITY_ego_lookup_cancel(lu->lookup);
    GNUNET_CONTAINER_DLL_remove (store->lu_start, store->lu_end, lu);

    if (lu->identifier)
      GNUNET_free(lu->identifier);

    GNUNET_free (lu);
  }

  GNUNET_CONTAINER_multihashmap_iterate (store->egos, iterate_destroy_egos, NULL);
  GNUNET_CONTAINER_multihashmap_destroy (store->egos);

  GNUNET_CONTAINER_multihashmap_destroy (store->handles);

  if (store->identity)
  {
    GNUNET_IDENTITY_disconnect (store->identity);

    store->identity = NULL;
  }
}

static int
iterate_create_ego (void *cls,
                    const struct GNUNET_HashCode *key,
                    void *value)
{
  struct GNUNET_MESSENGER_SrvHandle *handle = value;
  set_srv_handle_ego (handle, (struct GNUNET_MESSENGER_Ego*) cls);
  return GNUNET_YES;
}

static void
callback_ego_create (void *cls,
                     const struct GNUNET_IDENTITY_PrivateKey *key,
                     enum GNUNET_ErrorCode ec)
{
  struct GNUNET_MESSENGER_EgoOperation *element = cls;
  struct GNUNET_MESSENGER_EgoStore *store = element->store;

  GNUNET_assert (element->identifier);

  /**
   * FIXME: This is dangerous, please handle errors
   */
  if (GNUNET_EC_NONE != ec)
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n",
                GNUNET_ErrorCode_get_hint (ec));

  if (key)
  {
    struct GNUNET_MESSENGER_Ego *msg_ego = update_store_ego (store, element->identifier, key);

    struct GNUNET_HashCode hash;
    GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash);

    GNUNET_CONTAINER_multihashmap_get_multiple (store->handles, &hash, iterate_create_ego, msg_ego);
  }
  else
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating ego failed!\n");

  GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element);
  GNUNET_free (element->identifier);
  GNUNET_free (element);
}

void
create_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
                  const char *identifier)
{
  GNUNET_assert ((store) && (identifier));

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store create ego: %s\n", identifier);

  struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation);

  element->store = store;
  element->cls = NULL;

  element->identifier = GNUNET_strdup (identifier);

  element->operation = GNUNET_IDENTITY_create (
      store->identity,
      identifier,
      NULL,
      GNUNET_IDENTITY_TYPE_ECDSA,
      callback_ego_create,
      element
  );

  GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element);
}

void
bind_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
                const char *identifier,
                void *handle)
{
  GNUNET_assert ((store) && (identifier) && (handle));

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store bind ego: %s\n", identifier);

  struct GNUNET_HashCode hash;
  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);

  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value(store->handles, &hash, handle))
    return;

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(store->handles, &hash, handle,
                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE))
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Putting handle binding to ego store failed!\n");
}

void
unbind_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
                  const char *identifier,
                  void *handle)
{
  GNUNET_assert ((store) && (identifier) && (handle));

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store unbind ego: %s\n", identifier);

  struct GNUNET_HashCode hash;
  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);

  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(store->handles, &hash, handle))
    return;

  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(store->handles, &hash, handle))
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Removing handle binding from ego store failed!\n");
}

static void
callback_ego_lookup (void *cls,
                     struct GNUNET_IDENTITY_Ego *ego)
{
  struct GNUNET_MESSENGER_EgoLookup *element = cls;
  struct GNUNET_MESSENGER_EgoStore *store = element->store;

  GNUNET_assert (element->identifier);

  struct GNUNET_MESSENGER_Ego *msg_ego = NULL;

  if (ego)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New ego looked up: '%s'\n", element->identifier);
    msg_ego = update_store_ego (
        store,
        element->identifier,
        GNUNET_IDENTITY_ego_get_private_key(ego)
    );
  }
  else
  {
    struct GNUNET_HashCode hash;
    GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash);

    if (GNUNET_CONTAINER_multihashmap_get (store->egos, &hash))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looked up ego got deleted: '%s'\n", element->identifier);
      delete_store_ego(store, element->identifier);
    }
  }

  if (element->cb)
    element->cb(element->cls, element->identifier, msg_ego);

  GNUNET_CONTAINER_DLL_remove (store->lu_start, store->lu_end, element);
  GNUNET_free (element->identifier);
  GNUNET_free (element);
}

void
lookup_store_ego(struct GNUNET_MESSENGER_EgoStore *store,
                 const char *identifier,
                 GNUNET_MESSENGER_EgoLookupCallback lookup,
                 void *cls)
{
  GNUNET_assert (store);

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store lookup ego: %s\n", identifier);

  if (!identifier)
  {
    lookup(cls, identifier, NULL);
    return;
  }

  struct GNUNET_MESSENGER_EgoLookup *element = GNUNET_new (struct GNUNET_MESSENGER_EgoLookup);

  element->store = store;

  element->cb = lookup;
  element->cls = cls;

  element->identifier = GNUNET_strdup (identifier);

  element->lookup = GNUNET_IDENTITY_ego_lookup(store->cfg, identifier, callback_ego_lookup, element);

  GNUNET_CONTAINER_DLL_insert (store->lu_start, store->lu_end, element);
}

struct GNUNET_MESSENGER_Ego*
update_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
                  const char *identifier,
                  const struct GNUNET_IDENTITY_PrivateKey *key)
{
  GNUNET_assert ((store) && (identifier) && (key));

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store update ego: %s\n", identifier);

  struct GNUNET_HashCode hash;
  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);

  struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);

  if (!ego)
  {
    ego = GNUNET_new(struct GNUNET_MESSENGER_Ego);
    GNUNET_CONTAINER_multihashmap_put (store->egos, &hash, ego, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  }

  GNUNET_memcpy(&(ego->priv), key, sizeof(*key));

  if (GNUNET_OK != GNUNET_IDENTITY_key_get_public (key, &(ego->pub)))
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Updating invalid ego key failed!\n");

  return ego;
}

void
delete_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
                  const char *identifier)
{
  GNUNET_assert ((store) && (identifier));

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store delete ego: %s\n", identifier);

  struct GNUNET_HashCode hash;
  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);

  struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);

  if (ego)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Ego is not stored!\n");
    return;
  }

  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (store->egos, &hash, ego))
  {
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Removing ego from store failed!\n");
    return;
  }

  GNUNET_free(ego);
}

static void
callback_ego_rename (void *cls,
                     enum GNUNET_ErrorCode ec)
{
  struct GNUNET_MESSENGER_EgoOperation *element = cls;
  struct GNUNET_MESSENGER_EgoStore *store = element->store;

  GNUNET_assert (element->identifier);

  /**
   * FIXME: Dangerous, handle error
   */
  if (GNUNET_EC_NONE != ec)
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n",
                GNUNET_ErrorCode_get_hint (ec));

  struct GNUNET_HashCode hash;
  GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash);

  struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);

  if (!ego)
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Ego is not stored!\n");

  char *identifier = (char*) element->cls;

  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (store->egos, &hash, ego))
  {
    GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);

    GNUNET_CONTAINER_multihashmap_put (store->egos, &hash, ego,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  }
  else
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Renaming ego failed!\n");

  GNUNET_free (identifier);

  GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element);
  GNUNET_free (element->identifier);
  GNUNET_free (element);
}

void
rename_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
                  const char *old_identifier,
                  const char *new_identifier)
{
  GNUNET_assert ((store) && (old_identifier) && (new_identifier));

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store rename ego: %s -> %s\n", old_identifier, new_identifier);

  struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation);

  element->store = store;
  element->cls = GNUNET_strdup (new_identifier);

  element->identifier = GNUNET_strdup (old_identifier);

  element->operation = GNUNET_IDENTITY_rename (
      store->identity,
      old_identifier,
      new_identifier,
      callback_ego_rename,
      element
  );

  GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element);
}

static void
callback_ego_delete (void *cls,
                     enum GNUNET_ErrorCode ec)
{
  struct GNUNET_MESSENGER_EgoOperation *element = cls;
  struct GNUNET_MESSENGER_EgoStore *store = element->store;

  GNUNET_assert (element->identifier);

  /**
   * FIXME: Dangerous, handle error
   */
  if (GNUNET_EC_NONE != ec)
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n",
                GNUNET_ErrorCode_get_hint (ec));

  create_store_ego (store, element->identifier);

  GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element);
  GNUNET_free (element->identifier);
  GNUNET_free (element);
}

void
renew_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
                 const char *identifier)
{
  GNUNET_assert ((store) && (identifier));

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store renew ego: %s\n", identifier);

  struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation);

  element->store = store;
  element->cls = NULL;

  element->identifier = GNUNET_strdup (identifier);

  element->operation = GNUNET_IDENTITY_delete(
      store->identity,
      identifier,
      callback_ego_delete,
      element
  );

  GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element);
}