0465b1b2c92c6d187a4123cb57bf60944ff7a100
[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_break
213     (tag == GNUNET_DISK_fn_write (fn, buf, tag, GNUNET_DISK_PERM_USER_READ
214                                   | GNUNET_DISK_PERM_USER_WRITE |
215                                   GNUNET_DISK_PERM_GROUP_READ));
216   GNUNET_free (fn);
217   GNUNET_free (buf);
218   /* create entry for pseudonym name in names */
219   GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
220 }
221
222
223 /**
224  * FIXME
225  */
226 static int
227 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
228            const GNUNET_HashCode * nsid,
229            struct GNUNET_CONTAINER_MetaData **meta,
230            int32_t * ranking, char **ns_name)
231 {
232   uint64_t len;
233   size_t size;
234   size_t zend;
235   struct stat sbuf;
236   char *buf;
237   char *fn;
238
239   if (meta != NULL)
240     *meta = NULL;
241   if (ns_name != NULL)
242     *ns_name = NULL;
243   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
244   GNUNET_assert (fn != NULL);
245
246   if ((0 != STAT (fn, &sbuf))
247       || (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES)))
248     {
249       GNUNET_free (fn);
250       return GNUNET_SYSERR;
251     }
252   if (len <= sizeof (int32_t) + 1)
253     {
254       GNUNET_free (fn);
255       return GNUNET_SYSERR;
256     }
257   if (len > 16 * 1024 * 1024)
258     {
259       /* too big, must be invalid! remove! */
260       GNUNET_break (0);
261       if (0 != UNLINK (fn))
262         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
263       GNUNET_free (fn);
264       return GNUNET_SYSERR;
265     }
266   buf = GNUNET_malloc (len);
267   if (len != GNUNET_DISK_fn_read (fn, buf, len))
268     {
269       GNUNET_free (buf);
270       GNUNET_free (fn);
271       return GNUNET_SYSERR;
272     }
273   if (ranking != NULL)
274     *ranking = ntohl (((int32_t *) buf)[0]);
275   zend = sizeof (int32_t);
276   while ((zend < len) && (buf[zend] != '\0'))
277     zend++;
278   if (zend == len)
279     {
280       GNUNET_free (buf);
281       GNUNET_free (fn);
282       return GNUNET_SYSERR;
283     }
284   if (ns_name != NULL)
285     {
286       if (zend != sizeof (int32_t))
287         *ns_name = GNUNET_strdup (&buf[sizeof (int32_t)]);
288       else
289         *ns_name = NULL;
290     }
291   zend++;
292   size = len - zend;
293   if (meta != NULL)
294     {
295       *meta = GNUNET_CONTAINER_meta_data_deserialize (&buf[zend], size);
296       if ((*meta) == NULL)
297         {
298           /* invalid data! remove! */
299           GNUNET_break (0);
300           if (0 != UNLINK (fn))
301             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
302                                       "unlink", fn);
303           GNUNET_free (buf);
304           GNUNET_free (fn);
305           return GNUNET_SYSERR;
306         }
307     }
308   GNUNET_free (fn);
309   GNUNET_free (buf);
310   return GNUNET_OK;
311 }
312
313
314
315 /**
316  * Return the unique, human readable name for the given namespace.
317  *
318  * @param cfg configuration 
319  * @param nsid cryptographic ID of the namespace
320  * @return NULL on failure (should never happen)
321  */
322 char *
323 GNUNET_PSEUDONYM_id_to_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
324                              const GNUNET_HashCode * nsid)
325 {
326   struct GNUNET_CONTAINER_MetaData *meta;
327   char *name;
328   GNUNET_HashCode nh;
329   char *fn;
330   uint64_t len;
331   struct GNUNET_DISK_FileHandle *fh;
332   unsigned int i;
333   unsigned int idx;
334   char *ret;
335   struct stat sbuf;
336
337   meta = NULL;
338   name = NULL;
339   if (GNUNET_OK == read_info (cfg, nsid, &meta, NULL, &name))
340     {
341       if ((meta != NULL) && (name == NULL))
342         name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
343                                                               EXTRACTOR_TITLE,
344                                                               EXTRACTOR_FILENAME,
345                                                               EXTRACTOR_DESCRIPTION,
346                                                               EXTRACTOR_SUBJECT,
347                                                               EXTRACTOR_PUBLISHER,
348                                                               EXTRACTOR_AUTHOR,
349                                                               EXTRACTOR_COMMENT,
350                                                               EXTRACTOR_SUMMARY,
351                                                               EXTRACTOR_OWNER,
352                                                               -1);
353       if (meta != NULL)
354         {
355           GNUNET_CONTAINER_meta_data_destroy (meta);
356           meta = NULL;
357         }
358     }
359   if (name == NULL)
360     name = GNUNET_strdup (_("no-name"));
361   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
362   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
363   GNUNET_assert (fn != NULL);
364
365   len = 0;
366   if (0 == STAT (fn, &sbuf))
367     GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
368   fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
369                               | GNUNET_DISK_OPEN_READWRITE,
370                               GNUNET_DISK_PERM_USER_READ |
371                               GNUNET_DISK_PERM_USER_WRITE);
372   i = 0;
373   idx = -1;
374   while ((len >= sizeof (GNUNET_HashCode)) &&
375          (sizeof (GNUNET_HashCode)
376           == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
377     {
378       if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
379         {
380           idx = i;
381           break;
382         }
383       i++;
384       len -= sizeof (GNUNET_HashCode);
385     }
386   if (idx == -1)
387     {
388       idx = i;
389       if (sizeof (GNUNET_HashCode) !=
390           GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
391         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
392     }
393   GNUNET_DISK_file_close (fh);
394   ret = GNUNET_malloc (strlen (name) + 32);
395   GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
396   GNUNET_free (name);
397   GNUNET_free (fn);
398   return ret;
399 }
400
401 /**
402  * Get the namespace ID belonging to the given namespace name.
403  *
404  * @param cfg configuration to use
405  * @param ns_uname human-readable name for the namespace
406  * @param nsid set to namespace ID based on 'ns_uname'
407  * @return GNUNET_OK on success
408  */
409 int
410 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
411                              const char *ns_uname, GNUNET_HashCode * nsid)
412 {
413   size_t slen;
414   uint64_t len;
415   unsigned int idx;
416   char *name;
417   GNUNET_HashCode nh;
418   char *fn;
419   struct GNUNET_DISK_FileHandle *fh;
420
421   idx = -1;
422   slen = strlen (ns_uname);
423   while ((slen > 0) && (1 != sscanf (&ns_uname[slen - 1], "-%u", &idx)))
424     slen--;
425   if (slen == 0)
426     return GNUNET_SYSERR;
427   name = GNUNET_strdup (ns_uname);
428   name[slen - 1] = '\0';
429   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
430   GNUNET_free (name);
431   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
432   GNUNET_assert (fn != NULL);
433
434   if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
435        (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
436       ((idx + 1) * sizeof (GNUNET_HashCode) > len))
437     {
438       GNUNET_free (fn);
439       return GNUNET_SYSERR;
440     }
441   fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
442                               | GNUNET_DISK_OPEN_READWRITE,
443                               GNUNET_DISK_PERM_USER_READ |
444                               GNUNET_DISK_PERM_USER_WRITE);
445   GNUNET_free (fn);
446   GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode),
447                          GNUNET_DISK_SEEK_SET);
448   if (sizeof (GNUNET_HashCode) !=
449       GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
450     {
451       GNUNET_DISK_file_close (fh);
452       return GNUNET_SYSERR;
453     }
454   GNUNET_DISK_file_close (fh);
455   return GNUNET_OK;
456 }
457
458
459
460 /**
461  * FIXME
462  */
463 struct ListPseudonymClosure
464 {
465
466   /**
467    * FIXME
468    */
469   GNUNET_PSEUDONYM_Iterator iterator;
470
471   /**
472    * FIXME
473    */
474   void *closure;
475
476   /**
477    * FIXME
478    */
479   const struct GNUNET_CONFIGURATION_Handle *cfg;
480 };
481
482
483
484 /**
485  * FIXME
486  */
487 static int
488 list_pseudonym_helper (void *cls, const char *fullname)
489 {
490   struct ListPseudonymClosure *c = cls;
491   int ret;
492   GNUNET_HashCode id;
493   int rating;
494   struct GNUNET_CONTAINER_MetaData *meta;
495   const char *fn;
496
497   if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
498     return GNUNET_OK;
499   fn =
500     &fullname[strlen (fullname) + 1 -
501               sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
502   if (fn[-1] != DIR_SEPARATOR)
503     return GNUNET_OK;
504   ret = GNUNET_OK;
505   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
506     return GNUNET_OK;           /* invalid name */
507   if (GNUNET_OK != read_info (c->cfg, &id, &meta, &rating, NULL))
508     return GNUNET_OK;           /* ignore entry */
509   if (c->iterator != NULL)
510     ret = c->iterator (c->closure, &id, meta, rating);
511   GNUNET_CONTAINER_meta_data_destroy (meta);
512   return ret;
513 }
514
515
516 /**
517  * List all available pseudonyms.
518  *
519  * @param cfg overall configuration 
520  * @param iterator function to call for each pseudonym
521  * @param closure closure for iterator
522  * @return number of pseudonyms found
523  */
524 int
525 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
526                            GNUNET_PSEUDONYM_Iterator iterator, void *closure)
527 {
528   struct ListPseudonymClosure cls;
529   char *fn;
530   int ret;
531
532   cls.iterator = iterator;
533   cls.closure = closure;
534   cls.cfg = cfg;
535   fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
536   GNUNET_assert (fn != NULL);
537   GNUNET_DISK_directory_create (fn);
538   ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
539   GNUNET_free (fn);
540   return ret;
541 }
542
543 /**
544  * Change the ranking of a pseudonym.
545  *
546  * @param cfg overall configuration
547  * @param nsid id of the pseudonym
548  * @param delta by how much should the rating be
549  *  changed?
550  * @return new rating of the pseudonym
551  */
552 int
553 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
554                        const GNUNET_HashCode * nsid, int delta)
555 {
556   struct GNUNET_CONTAINER_MetaData *meta;
557   int ret;
558   int32_t ranking;
559   char *name;
560
561   name = NULL;
562   ret = read_info (cfg, nsid, &meta, &ranking, &name);
563   if (ret == GNUNET_SYSERR)
564     {
565       ranking = 0;
566       meta = GNUNET_CONTAINER_meta_data_create ();
567     }
568   ranking += delta;
569   write_pseudonym_info (cfg, nsid, meta, ranking, name);
570   GNUNET_CONTAINER_meta_data_destroy (meta);
571   GNUNET_free_non_null (name);
572   return ranking;
573 }
574
575 /**
576  * Insert metadata into existing MD record (passed as cls).
577  *
578  * @param cls metadata to add to
579  * @param type type of entry to insert
580  * @param data value of entry to insert
581  */
582 static int
583 merge_meta_helper (void *cls, EXTRACTOR_KeywordType type, const char *data)
584 {
585   struct GNUNET_CONTAINER_MetaData *meta = cls;
586   GNUNET_CONTAINER_meta_data_insert (meta, type, data);
587   return GNUNET_OK;
588 }
589
590
591
592 /**
593  * Add a pseudonym to the set of known pseudonyms.
594  * For all pseudonym advertisements that we discover
595  * FS should automatically call this function.
596  *
597  * @param cfg overall configuration
598  * @param id the pseudonym identifier
599  * @param meta metadata for the pseudonym
600  */
601 void
602 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
603                       const GNUNET_HashCode * id,
604                       const struct GNUNET_CONTAINER_MetaData *meta)
605 {
606   char *name;
607   int32_t ranking;
608   struct GNUNET_CONTAINER_MetaData *old;
609   char *fn;
610   struct stat sbuf;
611
612   ranking = 0;
613   fn = get_data_filename (cfg, PS_METADATA_DIR, id);
614   GNUNET_assert (fn != NULL);
615
616   if ((0 == STAT (fn, &sbuf)) &&
617       (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
618     {
619       GNUNET_CONTAINER_meta_data_get_contents (meta, &merge_meta_helper, old);
620       write_pseudonym_info (cfg, id, old, ranking, name);
621       GNUNET_CONTAINER_meta_data_destroy (old);
622       GNUNET_free_non_null (name);
623     }
624   else
625     {
626       write_pseudonym_info (cfg, id, meta, ranking, NULL);
627     }
628   GNUNET_free (fn);
629   internal_notify (id, meta, ranking);
630 }
631
632
633
634
635
636 /* end of pseudonym.c */