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