04a6a1f78ebcd71805bbf1e680fc6b80beb7f266
[oweals/gnunet.git] / src / util / pseudonym.c
1 /*
2      This file is part of GNUnet
3      (C) 2003, 2004, 2005, 2006, 2007, 2008 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 2, 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 util/pseudonym.c
23  * @brief helper functions
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_disk_lib.h"
31 #include "gnunet_pseudonym_lib.h"
32
33 /** 
34  * FIXME
35  */
36 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/metadata" DIR_SEPARATOR_STR
37
38 /** 
39  * FIXME
40  */
41 #define PS_NAMES_DIR    DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms/names"    DIR_SEPARATOR_STR
42
43
44 /** 
45  * FIXME
46  */
47 struct DiscoveryCallback
48 {
49
50   /** 
51    * FIXME
52    */
53   struct DiscoveryCallback *next;
54
55   /** 
56    * FIXME
57    */
58   GNUNET_PSEUDONYM_Iterator callback;
59
60   /** 
61    * FIXME
62    */
63   void *closure;
64 };
65
66
67 /** 
68  * FIXME
69  */
70 static struct DiscoveryCallback *head;
71
72 /**
73  * Internal notification about new tracked URI.
74  * 
75  * FIXME
76  */
77 static void
78 internal_notify (const GNUNET_HashCode * id,
79                  const struct GNUNET_CONTAINER_MetaData *md, int rating)
80 {
81   struct DiscoveryCallback *pos;
82
83   pos = head;
84   while (pos != NULL)
85     {
86       pos->callback (pos->closure, id, md, rating);
87       pos = pos->next;
88     }
89 }
90
91 /**
92  * Register callback to be invoked whenever we discover
93  * a new pseudonym.
94  * 
95  * FIXME
96  */
97 int
98 GNUNET_PSEUDONYM_discovery_callback_register (const struct
99                                               GNUNET_CONFIGURATION_Handle
100                                               *cfg,
101                                               GNUNET_PSEUDONYM_Iterator
102                                               iterator, void *closure)
103 {
104   struct DiscoveryCallback *list;
105
106   list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
107   list->callback = iterator;
108   list->closure = closure;
109   list->next = head;
110   head = list;
111   GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
112   return GNUNET_OK;
113 }
114
115 /**
116  * Unregister pseudonym discovery callback.
117  * 
118  * FIXME
119  */
120 int
121 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
122                                                 iterator, void *closure)
123 {
124   struct DiscoveryCallback *prev;
125   struct DiscoveryCallback *pos;
126
127   prev = NULL;
128   pos = head;
129   while ((pos != NULL) &&
130          ((pos->callback != iterator) || (pos->closure != closure)))
131     {
132       prev = pos;
133       pos = pos->next;
134     }
135   if (pos == NULL)
136     return GNUNET_SYSERR;
137   if (prev == NULL)
138     head = pos->next;
139   else
140     prev->next = pos->next;
141   GNUNET_free (pos);
142   return GNUNET_OK;
143 }
144
145
146 /**
147  * Get the filename (or directory name) for the given
148  * pseudonym identifier and directory prefix.
149  * 
150  * FIXME
151  */
152 static char *
153 get_data_filename (const struct GNUNET_CONFIGURATION_Handle
154                    *cfg, const char *prefix, const GNUNET_HashCode * psid)
155 {
156   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
157
158   if (psid != NULL)
159     GNUNET_CRYPTO_hash_to_enc (psid, &enc);
160   return GNUNET_DISK_get_home_filename (cfg,
161                                         GNUNET_CLIENT_SERVICE_NAME,
162                                         prefix,
163                                         (psid ==
164                                          NULL) ? NULL : (const char *) &enc,
165                                         NULL);
166 }
167
168
169 /**
170  * FIXME
171  */
172 static void
173 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
174                       const GNUNET_HashCode * nsid,
175                       const struct GNUNET_CONTAINER_MetaData *meta,
176                       int32_t ranking, const char *ns_name)
177 {
178   size_t size;
179   size_t tag;
180   size_t off;
181   char *buf;
182   char *fn;
183
184   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
185   GNUNET_assert (fn != NULL);
186   size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
187                                                          GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
188   tag = size + sizeof (int32_t) + 1;
189   off = 0;
190   if (ns_name != NULL)
191     {
192       off = strlen (ns_name);
193       tag += off;
194     }
195   buf = GNUNET_malloc (tag);
196   ((int32_t *) buf)[0] = htonl (ranking);   /* ranking */
197   if (ns_name != NULL)
198     {
199       memcpy (&buf[sizeof (int32_t)], ns_name, off + 1);
200     }
201   else
202     {
203       buf[sizeof (int)] = '\0';
204     }
205   GNUNET_assert
206     (size == GNUNET_CONTAINER_meta_data_serialize (meta,
207                                                    &buf[sizeof
208                                                         (int32_t) +
209                                                         off + 1],
210                                                    size,
211                                                    GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL));
212   GNUNET_DISK_fn_write (fn, buf, tag, GNUNET_DISK_PERM_USER_READ
213       | GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_READ);
214   GNUNET_free (fn);
215   GNUNET_free (buf);
216   /* create entry for pseudonym name in names */
217   GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
218 }
219
220
221 /**
222  * FIXME
223  */
224 static int
225 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
226            const GNUNET_HashCode * nsid,
227            struct GNUNET_CONTAINER_MetaData **meta,
228            int32_t * ranking, char **ns_name)
229 {
230   uint64_t len;
231   size_t size;
232   size_t zend;
233   struct stat sbuf;
234   char *buf;
235   char *fn;
236
237   if (meta != NULL)
238     *meta = NULL;
239   if (ns_name != NULL)
240     *ns_name = NULL;
241   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
242   GNUNET_assert (fn != NULL);
243
244   if ((0 != STAT (fn, &sbuf))
245       || (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES)))
246     {
247       GNUNET_free (fn);
248       return GNUNET_SYSERR;
249     }
250   if (len <= sizeof (int32_t) + 1)
251     {
252       GNUNET_free (fn);
253       return GNUNET_SYSERR;
254     }
255   if (len > 16 * 1024 * 1024)
256     {
257       /* too big, must be invalid! remove! */
258       GNUNET_break (0);
259       if (0 != UNLINK (fn))
260         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
261       GNUNET_free (fn);
262       return GNUNET_SYSERR;
263     }
264   buf = GNUNET_malloc (len);
265   if (len != GNUNET_DISK_fn_read (fn, buf, len))
266     {
267       GNUNET_free (buf);
268       GNUNET_free (fn);
269       return GNUNET_SYSERR;
270     }
271   if (ranking != NULL)
272     *ranking = ntohl (((int32_t *) buf)[0]);
273   zend = sizeof (int32_t);
274   while ((zend < len) && (buf[zend] != '\0'))
275     zend++;
276   if (zend == len)
277     {
278       GNUNET_free (buf);
279       GNUNET_free (fn);
280       return GNUNET_SYSERR;
281     }
282   if (ns_name != NULL)
283     {
284       if (zend != sizeof (int32_t))
285         *ns_name = GNUNET_strdup (&buf[sizeof (int32_t)]);
286       else
287         *ns_name = NULL;
288     }
289   zend++;
290   size = len - zend;
291   if (meta != NULL)
292     {
293       *meta = GNUNET_CONTAINER_meta_data_deserialize (&buf[zend], size);
294       if ((*meta) == NULL)
295         {
296           /* invalid data! remove! */
297           GNUNET_break (0);
298           if (0 != UNLINK (fn))
299             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
300                                       "unlink", fn);
301           GNUNET_free (buf);
302           GNUNET_free (fn);
303           return GNUNET_SYSERR;
304         }
305     }
306   GNUNET_free (fn);
307   GNUNET_free (buf);
308   return GNUNET_OK;
309 }
310
311
312
313 /**
314  * Return the unique, human readable name for the given namespace.
315  *
316  * @param cfg configuration 
317  * @param nsid cryptographic ID of the namespace
318  * @return NULL on failure (should never happen)
319  */
320 char *
321 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
322                              const GNUNET_HashCode * nsid)
323 {
324   struct GNUNET_CONTAINER_MetaData *meta;
325   char *name;
326   GNUNET_HashCode nh;
327   char *fn;
328   uint64_t len;
329   struct GNUNET_DISK_FileHandle *fh;
330   unsigned int i;
331   unsigned int idx;
332   char *ret;
333   struct stat sbuf;
334
335   meta = NULL;
336   name = NULL;
337   if (GNUNET_OK == read_info (cfg, nsid, &meta, NULL, &name))
338     {
339       if ((meta != NULL) && (name == NULL))
340         name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
341                                                               EXTRACTOR_TITLE,
342                                                               EXTRACTOR_FILENAME,
343                                                               EXTRACTOR_DESCRIPTION,
344                                                               EXTRACTOR_SUBJECT,
345                                                               EXTRACTOR_PUBLISHER,
346                                                               EXTRACTOR_AUTHOR,
347                                                               EXTRACTOR_COMMENT,
348                                                               EXTRACTOR_SUMMARY,
349                                                               EXTRACTOR_OWNER,
350                                                               -1);
351       if (meta != NULL)
352         {
353           GNUNET_CONTAINER_meta_data_destroy (meta);
354           meta = NULL;
355         }
356     }
357   if (name == NULL)
358     name = GNUNET_strdup (_("no-name"));
359   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
360   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
361   GNUNET_assert (fn != NULL);
362
363   len = 0;
364   if (0 == STAT (fn, &sbuf))
365     GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
366   fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
367       | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
368       | GNUNET_DISK_PERM_USER_WRITE);
369   i = 0;
370   idx = -1;
371   while ((len >= sizeof (GNUNET_HashCode)) &&
372          (sizeof (GNUNET_HashCode)
373           == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
374     {
375       if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
376         {
377           idx = i;
378           break;
379         }
380       i++;
381       len -= sizeof (GNUNET_HashCode);
382     }
383   if (idx == -1)
384     {
385       idx = i;
386       if (sizeof (GNUNET_HashCode) !=
387           GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
388         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
389     }
390   GNUNET_DISK_file_close (fh);
391   ret = GNUNET_malloc (strlen (name) + 32);
392   GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
393   GNUNET_free (name);
394   GNUNET_free (fn);
395   return ret;
396 }
397
398 /**
399  * Get the namespace ID belonging to the given namespace name.
400  *
401  * @param cfg configuration to use
402  * @param ns_uname human-readable name for the namespace
403  * @param nsid set to namespace ID based on 'ns_uname'
404  * @return GNUNET_OK on success
405  */
406 int
407 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
408                              const char *ns_uname, GNUNET_HashCode * nsid)
409 {
410   size_t slen;
411   uint64_t len;
412   unsigned int idx;
413   char *name;
414   GNUNET_HashCode nh;
415   char *fn;
416   struct GNUNET_DISK_FileHandle *fh;
417
418   idx = -1;
419   slen = strlen (ns_uname);
420   while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
421     slen--;
422   if (slen == 0)
423     return GNUNET_SYSERR;
424   name = GNUNET_strdup (ns_uname);
425   name[slen - 1] = '\0';
426   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
427   GNUNET_free (name);
428   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
429   GNUNET_assert (fn != NULL);
430
431   if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
432        (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
433       ((idx + 1) * sizeof (GNUNET_HashCode) > len))
434     {
435       GNUNET_free (fn);
436       return GNUNET_SYSERR;
437     }
438   fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
439       | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
440       | GNUNET_DISK_PERM_USER_WRITE);
441   GNUNET_free (fn);
442   GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), GNUNET_DISK_SEEK_SET);
443   if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
444     {
445       GNUNET_DISK_file_close (fh);
446       return GNUNET_SYSERR;
447     }
448   GNUNET_DISK_file_close (fh);
449   return GNUNET_OK;
450 }
451
452
453
454 /**
455  * FIXME
456  */
457 struct ListPseudonymClosure
458 {
459
460   /**
461    * FIXME
462    */
463   GNUNET_PSEUDONYM_Iterator iterator;
464   
465   /**
466    * FIXME
467    */
468   void *closure;
469     
470   /**
471    * FIXME
472    */
473   const struct GNUNET_CONFIGURATION_Handle *cfg;
474 };
475
476
477
478 /**
479  * FIXME
480  */
481 static int
482 list_pseudonym_helper (void *cls, const char *fullname)
483 {
484   struct ListPseudonymClosure *c = cls;
485   int ret;
486   GNUNET_HashCode id;
487   int rating;
488   struct GNUNET_CONTAINER_MetaData *meta;
489   const char *fn;
490
491   if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
492     return GNUNET_OK;
493   fn =
494     &fullname[strlen (fullname) + 1 -
495               sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
496   if (fn[-1] != DIR_SEPARATOR)
497     return GNUNET_OK;
498   ret = GNUNET_OK;
499   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
500     return GNUNET_OK;           /* invalid name */
501   if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, NULL))
502     return GNUNET_OK;           /* ignore entry */
503   if (c->iterator != NULL)
504     ret = c->iterator (c->closure, &id, meta, rating);
505   GNUNET_CONTAINER_meta_data_destroy (meta);
506   return ret;
507 }
508
509
510 /**
511  * List all available pseudonyms.
512  *
513  * @param cfg overall configuration 
514  * @param iterator function to call for each pseudonym
515  * @param closure closure for iterator
516  * @return number of pseudonyms found
517  */
518 int
519 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
520                            GNUNET_PSEUDONYM_Iterator iterator, void *closure)
521 {
522   struct ListPseudonymClosure cls;
523   char *fn;
524   int ret;
525
526   cls.iterator = iterator;
527   cls.closure = closure;
528   cls.cfg = cfg;
529   fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
530   GNUNET_assert (fn != NULL);
531   GNUNET_DISK_directory_create (fn);
532   ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
533   GNUNET_free (fn);
534   return ret;
535 }
536
537 /**
538  * Change the ranking of a pseudonym.
539  *
540  * @param cfg overall configuration
541  * @param nsid id of the pseudonym
542  * @param delta by how much should the rating be
543  *  changed?
544  * @return new rating of the pseudonym
545  */
546 int
547 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
548                        const GNUNET_HashCode * nsid, int delta)
549 {
550   struct GNUNET_CONTAINER_MetaData *meta;
551   int ret;
552   int32_t ranking;
553   char *name;
554
555   name = NULL;
556   ret = read_info (cfg, nsid, &meta, &ranking, &name);
557   if (ret == GNUNET_SYSERR)
558     {
559       ranking = 0;
560       meta = GNUNET_CONTAINER_meta_data_create ();
561     }
562   ranking += delta;
563   write_pseudonym_info (cfg, nsid, meta, ranking, name);
564   GNUNET_CONTAINER_meta_data_destroy (meta);
565   GNUNET_free_non_null (name);
566   return ranking;
567 }
568
569 /**
570  * Insert metadata into existing MD record (passed as cls).
571  *
572  * @param cls metadata to add to
573  * @param type type of entry to insert
574  * @param data value of entry to insert
575  */
576 static int
577 merge_meta_helper (void *cls,
578                    EXTRACTOR_KeywordType type, 
579                    const char *data)
580 {
581   struct GNUNET_CONTAINER_MetaData *meta = cls;
582   GNUNET_CONTAINER_meta_data_insert (meta, type, data);
583   return GNUNET_OK;
584 }
585
586
587
588 /**
589  * Add a pseudonym to the set of known pseudonyms.
590  * For all pseudonym advertisements that we discover
591  * FS should automatically call this function.
592  *
593  * @param cfg overall configuration
594  * @param id the pseudonym identifier
595  * @param meta metadata for the pseudonym
596  */
597 void
598 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
599                       const GNUNET_HashCode * id,
600                       const struct GNUNET_CONTAINER_MetaData *meta)
601 {
602   char *name;
603   int32_t ranking;
604   struct GNUNET_CONTAINER_MetaData *old;
605   char *fn;
606   struct stat sbuf;
607
608   ranking = 0;
609   fn = get_data_filename (cfg, PS_METADATA_DIR, id);
610   GNUNET_assert (fn != NULL);
611
612   if ((0 == STAT (fn, &sbuf)) &&
613       (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
614     {
615       GNUNET_CONTAINER_meta_data_get_contents (meta, &merge_meta_helper, old);
616       write_pseudonym_info (cfg, id, old, ranking, name);
617       GNUNET_CONTAINER_meta_data_destroy (old);
618       GNUNET_free_non_null (name);
619     }
620   else
621     {
622       write_pseudonym_info (cfg, id, meta, ranking, NULL);
623     }
624   GNUNET_free (fn);
625   internal_notify (id, meta, ranking);
626 }
627
628
629
630
631
632 /* end of pseudonym.c */