aboutsummaryrefslogtreecommitdiff
path: root/src/util/pseudonym.c
blob: 6d91466134aecd762eca80da5e047baa87d16ee9 (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
     This file is part of GNUnet
     (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file util/pseudonym.c
 * @brief helper functions
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_container_lib.h"
#include "gnunet_disk_lib.h"
#include "gnunet_pseudonym_lib.h"

#define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/metadata" DIR_SEPARATOR_STR
#define PS_NAMES_DIR    DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/names"    DIR_SEPARATOR_STR

struct DiscoveryCallback
{
  struct DiscoveryCallback *next;
  GNUNET_PSEUDONYM_Iterator callback;
  void *closure;
};

static struct DiscoveryCallback *head;

/**
 * Internal notification about new tracked URI.
 */
static void
internal_notify (const GNUNET_HashCode * id,
                 const struct GNUNET_CONTAINER_MetaData *md, int rating)
{
  struct DiscoveryCallback *pos;

  pos = head;
  while (pos != NULL)
    {
      pos->callback (pos->closure, id, md, rating);
      pos = pos->next;
    }
}

/**
 * Register callback to be invoked whenever we discover
 * a new pseudonym.
 */
int
GNUNET_PSEUDONYM_discovery_callback_register (struct
                                              GNUNET_CONFIGURATION_Handle
                                              *cfg,
                                              GNUNET_PSEUDONYM_Iterator
                                              iterator, void *closure)
{
  struct DiscoveryCallback *list;

  list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
  list->callback = iterator;
  list->closure = closure;
  list->next = head;
  head = list;
  GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
  return GNUNET_OK;
}

/**
 * Unregister pseudonym discovery callback.
 */
int
GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
                                                iterator, void *closure)
{
  struct DiscoveryCallback *prev;
  struct DiscoveryCallback *pos;

  prev = NULL;
  pos = head;
  while ((pos != NULL) &&
         ((pos->callback != iterator) || (pos->closure != closure)))
    {
      prev = pos;
      pos = pos->next;
    }
  if (pos == NULL)
    return GNUNET_SYSERR;
  if (prev == NULL)
    head = pos->next;
  else
    prev->next = pos->next;
  GNUNET_free (pos);
  return GNUNET_OK;
}


/**
 * Get the filename (or directory name) for the given
 * pseudonym identifier and directory prefix.
 */
static char *
get_data_filename (struct GNUNET_CONFIGURATION_Handle
                   *cfg, const char *prefix, const GNUNET_HashCode * psid)
{
  struct GNUNET_CRYPTO_HashAsciiEncoded enc;

  if (psid != NULL)
    GNUNET_CRYPTO_hash_to_enc (psid, &enc);
  return GNUNET_DISK_get_home_filename (cfg,
                                        GNUNET_CLIENT_SERVICE_NAME,
                                        prefix,
                                        (psid ==
                                         NULL) ? NULL : (const char *) &enc,
                                        NULL);
}

static void
write_pseudonym_info (struct GNUNET_CONFIGURATION_Handle *cfg,
                      const GNUNET_HashCode * nsid,
                      const struct GNUNET_CONTAINER_MetaData *meta,
                      int32_t ranking, const char *ns_name)
{
  unsigned int size;
  unsigned int tag;
  unsigned int off;
  char *buf;
  char *fn;

  fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
  GNUNET_assert (fn != NULL);
  size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
                                                         GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
  tag = size + sizeof (int) + 1;
  off = 0;
  if (ns_name != NULL)
    {
      off = strlen (ns_name);
      tag += off;
    }
  buf = GNUNET_malloc (tag);
  ((int *) buf)[0] = htonl (ranking);   /* ranking */
  if (ns_name != NULL)
    {
      memcpy (&buf[sizeof (int)], ns_name, off + 1);
    }
  else
    {
      buf[sizeof (int)] = '\0';
    }
  GNUNET_assert
    (size == GNUNET_CONTAINER_meta_data_serialize (meta,
                                                   &buf[sizeof
                                                        (int) +
                                                        off + 1],
                                                   size,
                                                   GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL));
  GNUNET_DISK_file_write (fn, buf, tag, "660");
  GNUNET_free (fn);
  GNUNET_free (buf);
  /* create entry for pseudonym name in names */
  GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
}

static int
read_info (struct GNUNET_CONFIGURATION_Handle *cfg,
           const GNUNET_HashCode * nsid,
           struct GNUNET_CONTAINER_MetaData **meta,
           int32_t * ranking, char **ns_name)
{
  unsigned long long len;
  unsigned int size;
  unsigned int zend;
  struct stat sbuf;
  char *buf;
  char *fn;

  if (meta != NULL)
    *meta = NULL;
  if (ns_name != NULL)
    *ns_name = NULL;
  fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
  GNUNET_assert (fn != NULL);

  if ((0 != STAT (fn, &sbuf))
      || (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES)))
    {
      GNUNET_free (fn);
      return GNUNET_SYSERR;
    }
  if (len <= sizeof (int) + 1)
    {
      GNUNET_free (fn);
      return GNUNET_SYSERR;
    }
  if (len > 16 * 1024 * 1024)
    {
      /* too big, must be invalid! remove! */
      GNUNET_break (0);
      if (0 != UNLINK (fn))
        GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
      GNUNET_free (fn);
      return GNUNET_SYSERR;
    }
  buf = GNUNET_malloc (len);
  if (len != GNUNET_DISK_file_read (fn, len, buf))
    {
      GNUNET_free (buf);
      GNUNET_free (fn);
      return GNUNET_SYSERR;
    }
  if (ranking != NULL)
    *ranking = ntohl (((int *) buf)[0]);
  zend = sizeof (int);
  while ((zend < len) && (buf[zend] != '\0'))
    zend++;
  if (zend == len)
    {
      GNUNET_free (buf);
      GNUNET_free (fn);
      return GNUNET_SYSERR;
    }
  if (ns_name != NULL)
    {
      if (zend != sizeof (int))
        *ns_name = GNUNET_strdup (&buf[sizeof (int)]);
      else
        *ns_name = NULL;
    }
  zend++;
  size = len - zend;
  if (meta != NULL)
    {
      *meta = GNUNET_CONTAINER_meta_data_deserialize (&buf[zend], size);
      if ((*meta) == NULL)
        {
          /* invalid data! remove! */
          GNUNET_break (0);
          if (0 != UNLINK (fn))
            GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
                                      "unlink", fn);
          GNUNET_free (buf);
          GNUNET_free (fn);
          return GNUNET_SYSERR;
        }
    }
  GNUNET_free (fn);
  GNUNET_free (buf);
  return GNUNET_OK;
}



/**
 * Return the unique, human readable name for the given namespace.
 *
 * @return NULL on failure (should never happen)
 */
char *
GNUNET_PSEUDONYM_id_to_name (struct GNUNET_CONFIGURATION_Handle *cfg,
                             const GNUNET_HashCode * nsid)
{
  struct GNUNET_CONTAINER_MetaData *meta;
  char *name;
  GNUNET_HashCode nh;
  char *fn;
  unsigned long long len;
  int fd;
  unsigned int i;
  unsigned int idx;
  char *ret;
  struct stat sbuf;

  meta = NULL;
  name = NULL;
  if (GNUNET_OK == read_info (cfg, nsid, &meta, NULL, &name))
    {
      if ((meta != NULL) && (name == NULL))
        name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
                                                              EXTRACTOR_TITLE,
                                                              EXTRACTOR_FILENAME,
                                                              EXTRACTOR_DESCRIPTION,
                                                              EXTRACTOR_SUBJECT,
                                                              EXTRACTOR_PUBLISHER,
                                                              EXTRACTOR_AUTHOR,
                                                              EXTRACTOR_COMMENT,
                                                              EXTRACTOR_SUMMARY,
                                                              EXTRACTOR_OWNER,
                                                              -1);
      if (meta != NULL)
        {
          GNUNET_CONTAINER_meta_data_destroy (meta);
          meta = NULL;
        }
    }
  if (name == NULL)
    name = GNUNET_strdup (_("no-name"));
  GNUNET_CRYPTO_hash (name, strlen (name), &nh);
  fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
  GNUNET_assert (fn != NULL);

  len = 0;
  if (0 == STAT (fn, &sbuf))
    GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
  fd = GNUNET_DISK_file_open (fn, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  i = 0;
  idx = -1;
  while ((len >= sizeof (GNUNET_HashCode)) &&
         (sizeof (GNUNET_HashCode)
          == READ (fd, &nh, sizeof (GNUNET_HashCode))))
    {
      if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
        {
          idx = i;
          break;
        }
      i++;
      len -= sizeof (GNUNET_HashCode);
    }
  if (idx == -1)
    {
      idx = i;
      if (sizeof (GNUNET_HashCode) !=
          WRITE (fd, nsid, sizeof (GNUNET_HashCode)))
        GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
    }
  CLOSE (fd);
  ret = GNUNET_malloc (strlen (name) + 32);
  GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
  GNUNET_free (name);
  GNUNET_free (fn);
  return ret;
}

/**
 * Get the namespace ID belonging to the given namespace name.
 *
 * @return GNUNET_OK on success
 */
int
GNUNET_PSEUDONYM_name_to_id (struct GNUNET_CONFIGURATION_Handle *cfg,
                             const char *ns_uname, GNUNET_HashCode * nsid)
{
  size_t slen;
  unsigned long long len;
  unsigned int idx;
  char *name;
  GNUNET_HashCode nh;
  char *fn;
  int fd;

  idx = -1;
  slen = strlen (ns_uname);
  while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
    slen--;
  if (slen == 0)
    return GNUNET_SYSERR;
  name = GNUNET_strdup (ns_uname);
  name[slen - 1] = '\0';
  GNUNET_CRYPTO_hash (name, strlen (name), &nh);
  GNUNET_free (name);
  fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
  GNUNET_assert (fn != NULL);

  if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
       (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
      ((idx + 1) * sizeof (GNUNET_HashCode) > len))
    {
      GNUNET_free (fn);
      return GNUNET_SYSERR;
    }
  fd = GNUNET_DISK_file_open (fn, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  GNUNET_free (fn);
  LSEEK (fd, idx * sizeof (GNUNET_HashCode), SEEK_SET);
  if (sizeof (GNUNET_HashCode) != READ (fd, nsid, sizeof (GNUNET_HashCode)))
    {
      CLOSE (fd);
      return GNUNET_SYSERR;
    }
  CLOSE (fd);
  return GNUNET_OK;
}




struct ListPseudonymClosure
{
  GNUNET_PSEUDONYM_Iterator iterator;
  void *closure;
  struct GNUNET_CONFIGURATION_Handle *cfg;
};

static int
list_pseudonym_helper (void *cls, const char *fullname)
{
  struct ListPseudonymClosure *c = cls;
  int ret;
  GNUNET_HashCode id;
  int rating;
  struct GNUNET_CONTAINER_MetaData *meta;
  const char *fn;

  if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
    return GNUNET_OK;
  fn =
    &fullname[strlen (fullname) + 1 -
              sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
  if (fn[-1] != DIR_SEPARATOR)
    return GNUNET_OK;
  ret = GNUNET_OK;
  if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
    return GNUNET_OK;           /* invalid name */
  if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, NULL))
    return GNUNET_OK;           /* ignore entry */
  if (c->iterator != NULL)
    ret = c->iterator (c->closure, &id, meta, rating);
  GNUNET_CONTAINER_meta_data_destroy (meta);
  return ret;
}

/**
 * List all available pseudonyms.
 */
int
GNUNET_PSEUDONYM_list_all (struct GNUNET_CONFIGURATION_Handle *cfg,
                           GNUNET_PSEUDONYM_Iterator iterator, void *closure)
{
  struct ListPseudonymClosure cls;
  char *fn;
  int ret;

  cls.iterator = iterator;
  cls.closure = closure;
  cls.cfg = cfg;
  fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
  GNUNET_assert (fn != NULL);
  GNUNET_DISK_directory_create (fn);
  ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
  GNUNET_free (fn);
  return ret;
}

/**
 * Change the ranking of a pseudonym.
 *
 * @param nsid id of the pseudonym
 * @param delta by how much should the rating be
 *  changed?
 * @return new rating of the pseudonym
 */
int
GNUNET_PSEUDONYM_rank (struct GNUNET_CONFIGURATION_Handle *cfg,
                       const GNUNET_HashCode * nsid, int delta)
{
  struct GNUNET_CONTAINER_MetaData *meta;
  int ret;
  int32_t ranking;
  char *name;

  name = NULL;
  ret = read_info (cfg, nsid, &meta, &ranking, &name);
  if (ret == GNUNET_SYSERR)
    {
      ranking = 0;
      meta = GNUNET_CONTAINER_meta_data_create ();
    }
  ranking += delta;
  write_pseudonym_info (cfg, nsid, meta, ranking, name);
  GNUNET_CONTAINER_meta_data_destroy (meta);
  GNUNET_free_non_null (name);
  return ranking;
}

/**
 * Insert metadata into existing MD record (passed as cls).
 */
static int
merge_meta_helper (EXTRACTOR_KeywordType type, const char *data, void *cls)
{
  struct GNUNET_CONTAINER_MetaData *meta = cls;
  GNUNET_CONTAINER_meta_data_insert (meta, type, data);
  return GNUNET_OK;
}



/**
 * Add a pseudonym to the set of known pseudonyms.
 * For all pseudonym advertisements that we discover
 * FSUI should automatically call this function.
 *
 * @param id the pseudonym identifier
 */
void
GNUNET_PSEUDONYM_add (struct GNUNET_CONFIGURATION_Handle *cfg,
                      const GNUNET_HashCode * id,
                      const struct GNUNET_CONTAINER_MetaData *meta)
{
  char *name;
  int32_t ranking;
  struct GNUNET_CONTAINER_MetaData *old;
  char *fn;
  struct stat sbuf;

  ranking = 0;
  fn = get_data_filename (cfg, PS_METADATA_DIR, id);
  GNUNET_assert (fn != NULL);

  if ((0 == STAT (fn, &sbuf)) &&
      (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
    {
      GNUNET_CONTAINER_meta_data_get_contents (meta, &merge_meta_helper, old);
      write_pseudonym_info (cfg, id, old, ranking, name);
      GNUNET_CONTAINER_meta_data_destroy (old);
      GNUNET_free_non_null (name);
    }
  else
    {
      write_pseudonym_info (cfg, id, meta, ranking, NULL);
    }
  GNUNET_free (fn);
  internal_notify (id, meta, ranking);
}





/* end of pseudonym.c */