starting major change towards implementing #2564, this breaks some FS tests and FS...
[oweals/gnunet.git] / src / util / pseudonym.c
1 /*
2      This file is part of GNUnet
3      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2013 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  * @file util/pseudonym.c
22  * @brief helper functions
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - all cryptographic operations are currently NOT implemented and
27  *   provided by stubs that merely pretend to work!
28  */
29 #include "platform.h"
30 #include "gnunet_common.h"
31 #include "gnunet_container_lib.h"
32 #include "gnunet_disk_lib.h"
33 #include "gnunet_pseudonym_lib.h"
34 #include "gnunet_bio_lib.h"
35 #include <gcrypt.h>
36
37 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
38
39 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
40
41 /**
42  * Name of the directory which stores meta data for pseudonym
43  */
44 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonym" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
45
46 /**
47  * Name of the directory which stores names for pseudonyms
48  */
49 #define PS_NAMES_DIR    DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonym" DIR_SEPARATOR_STR "names"    DIR_SEPARATOR_STR
50
51
52 /**
53  * Configuration section we use.
54  */
55 #define GNUNET_CLIENT_SERVICE_NAME "client"
56
57
58 /* ************************* Disk operations (pseudonym data mgmt) **************** */
59
60 /**
61  * Registered callbacks for discovery of pseudonyms.
62  */
63 struct GNUNET_PSEUDONYM_DiscoveryHandle
64 {
65   /**
66    * This is a doubly linked list.
67    */
68   struct GNUNET_PSEUDONYM_DiscoveryHandle *next;
69
70   /**
71    * This is a doubly linked list.
72    */
73   struct GNUNET_PSEUDONYM_DiscoveryHandle *prev;
74
75   /**
76    * Function to call each time a pseudonym is discovered.
77    */
78   GNUNET_PSEUDONYM_Iterator callback;
79
80   /**
81    * Closure for callback.
82    */
83   void *callback_cls;
84 };
85
86
87 /**
88  * Head of the linked list of functions to call when
89  * new pseudonyms are added.
90  */
91 static struct GNUNET_PSEUDONYM_DiscoveryHandle *disco_head;
92
93 /**
94  * Tail of the linked list of functions to call when
95  * new pseudonyms are added.
96  */
97 static struct GNUNET_PSEUDONYM_DiscoveryHandle *disco_tail;
98
99
100 /**
101  * Internal notification about new tracked URI.
102  *
103  * @param pseudonym public key of the pseudonym
104  * @param md meta data to be written
105  * @param rating rating of pseudonym
106  */
107 static void
108 internal_notify (const struct GNUNET_PseudonymIdentifier *pseudonym,
109                  const struct GNUNET_CONTAINER_MetaData *md, int rating)
110 {
111   struct GNUNET_PSEUDONYM_DiscoveryHandle *pos;
112
113   for (pos = disco_head; NULL != pos; pos = pos->next)
114     pos->callback (pos->callback_cls, pseudonym, NULL, NULL, md, rating);
115 }
116
117
118 /**
119  * Register callback to be invoked whenever we discover
120  * a new pseudonym.
121  * Will immediately call provided iterator callback for all
122  * already discovered pseudonyms.
123  *
124  * @param cfg configuration to use
125  * @param iterator iterator over pseudonym
126  * @param iterator_cls point to a closure
127  * @return registration handle
128  */
129 struct GNUNET_PSEUDONYM_DiscoveryHandle *
130 GNUNET_PSEUDONYM_discovery_callback_register (const struct
131                                               GNUNET_CONFIGURATION_Handle *cfg,
132                                               GNUNET_PSEUDONYM_Iterator iterator, 
133                                               void *iterator_cls)
134 {
135   struct GNUNET_PSEUDONYM_DiscoveryHandle *dh;
136
137   dh = GNUNET_malloc (sizeof (struct GNUNET_PSEUDONYM_DiscoveryHandle));
138   dh->callback = iterator;
139   dh->callback_cls = iterator_cls;
140   GNUNET_CONTAINER_DLL_insert (disco_head, disco_tail, dh);
141   GNUNET_PSEUDONYM_list_all (cfg, iterator, iterator_cls);
142   return dh;
143 }
144
145
146 /**
147  * Unregister pseudonym discovery callback.
148  *
149  * @param dh registration to unregister
150  */
151 void
152 GNUNET_PSEUDONYM_discovery_callback_unregister (struct GNUNET_PSEUDONYM_DiscoveryHandle *dh)
153 {
154   GNUNET_CONTAINER_DLL_remove (disco_head, disco_tail, dh);
155   GNUNET_free (dh);
156 }
157
158
159 /**
160  * Get the filename (or directory name) for the given
161  * pseudonym identifier and directory prefix.
162  *
163  * @param cfg configuration to use
164  * @param prefix path components to append to the private directory name
165  * @param pseudonym the pseudonym, can be NULL
166  * @return filename of the pseudonym (if pseudonym != NULL) or directory with the data (if pseudonym == NULL)
167  */
168 static char *
169 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
170                    const char *prefix, 
171                    const struct GNUNET_PseudonymIdentifier *pseudonym)
172 {
173   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
174   struct GNUNET_HashCode psid;
175
176   if (NULL != pseudonym)
177   {
178     GNUNET_CRYPTO_hash (pseudonym,
179                         sizeof (struct GNUNET_PseudonymIdentifier),
180                         &psid);
181     GNUNET_CRYPTO_hash_to_enc (&psid, &enc);
182   }
183   return GNUNET_DISK_get_home_filename (cfg, 
184                                         GNUNET_CLIENT_SERVICE_NAME, prefix,
185                                         (NULL == pseudonym) 
186                                         ? NULL 
187                                         : (const char *) &enc,
188                                         NULL);
189 }
190
191
192 /**
193  * Get the filename (or directory name) for the given
194  * hash code and directory prefix.
195  *
196  * @param cfg configuration to use
197  * @param prefix path components to append to the private directory name
198  * @param hc some hash code
199  * @return filename of the pseudonym (if hc != NULL) or directory with the data (if hc == NULL)
200  */
201 static char *
202 get_data_filename_hash (const struct GNUNET_CONFIGURATION_Handle *cfg,
203                         const char *prefix, 
204                         const struct GNUNET_HashCode *hc)
205 {
206   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
207
208   if (NULL != hc)
209     GNUNET_CRYPTO_hash_to_enc (hc, &enc);
210   return GNUNET_DISK_get_home_filename (cfg, 
211                                         GNUNET_CLIENT_SERVICE_NAME, prefix,
212                                         (NULL == hc) 
213                                         ? NULL 
214                                         : (const char *) &enc,
215                                         NULL);
216 }
217
218
219 /**
220  * Set the pseudonym metadata, rank and name.
221  * Writes the pseudonym infomation into a file
222  *
223  * @param cfg overall configuration
224  * @param nsid id of the pseudonym
225  * @param name name to set. Must be the non-unique version of it.
226  *        May be NULL, in which case it erases pseudonym's name!
227  * @param md metadata to set
228  *        May be NULL, in which case it erases pseudonym's metadata!
229  * @param rank rank to assign
230  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
231  */
232 int
233 GNUNET_PSEUDONYM_set_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
234                            const struct GNUNET_PseudonymIdentifier *pseudonym,
235                            const char *name,
236                            const struct GNUNET_CONTAINER_MetaData *md, 
237                            int32_t rank)
238 {
239   char *fn;
240   struct GNUNET_BIO_WriteHandle *fileW;
241
242   fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
243   if (NULL == (fileW = GNUNET_BIO_write_open (fn)))
244   {
245     GNUNET_free (fn);
246     return GNUNET_SYSERR;
247   }
248   if ((GNUNET_OK != GNUNET_BIO_write (fileW, pseudonym, 
249                                       sizeof (struct GNUNET_PseudonymIdentifier))) ||
250       (GNUNET_OK != GNUNET_BIO_write_int32 (fileW, rank)) ||
251       (GNUNET_OK != GNUNET_BIO_write_string (fileW, name)) ||
252       (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, md)))
253   {
254     (void) GNUNET_BIO_write_close (fileW);
255     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
256     GNUNET_free (fn);
257     return GNUNET_SYSERR;
258   }
259   if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
260   {
261     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
262     GNUNET_free (fn);
263     return GNUNET_SYSERR;
264   } 
265   GNUNET_free (fn);
266   /* create entry for pseudonym name in names */
267   if (NULL != name)
268     GNUNET_free_non_null (GNUNET_PSEUDONYM_name_uniquify (cfg, pseudonym, 
269                                                           name, NULL));
270   return GNUNET_OK;
271 }
272
273
274 /**
275  * Read pseudonym infomation from a file
276  *
277  * @param cfg configuration to use
278  * @param nsid hash code of a pseudonym
279  * @param meta meta data to be read from a file
280  * @param rank rank of a pseudonym
281  * @param ns_name name of a pseudonym
282  * @return GNUNET_OK on success, GNUNET_SYSERR on error
283  */
284 static int
285 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
286            const struct GNUNET_PseudonymIdentifier *pseudonym,
287            struct GNUNET_CONTAINER_MetaData **meta,
288            int32_t *rank,
289            char **ns_name)
290 {
291   struct GNUNET_PseudonymIdentifier pd;
292   char *fn;
293   char *emsg;
294   struct GNUNET_BIO_ReadHandle *fileR;
295
296   fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
297   if (GNUNET_YES !=
298       GNUNET_DISK_file_test (fn))
299   {
300     GNUNET_free (fn);
301     return GNUNET_SYSERR;
302   }
303   if (NULL == (fileR = GNUNET_BIO_read_open (fn)))
304   {
305     GNUNET_free (fn);
306     return GNUNET_SYSERR;
307   }
308   emsg = NULL;
309   *ns_name = NULL;
310   if ( (GNUNET_OK != GNUNET_BIO_read (fileR, "pseudonym", &pd, sizeof (pd))) ||
311        (0 != memcmp (&pd, pseudonym, sizeof (pd))) ||
312        (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, rank)) ||
313        (GNUNET_OK !=
314         GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
315        (GNUNET_OK !=
316        GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)) )
317   {
318     (void) GNUNET_BIO_read_close (fileR, &emsg);
319     GNUNET_free_non_null (emsg);
320     GNUNET_free_non_null (*ns_name);
321     *ns_name = NULL;
322     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
323     GNUNET_free (fn);
324     return GNUNET_SYSERR;
325   }
326   if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
327   {
328     LOG (GNUNET_ERROR_TYPE_WARNING,
329          _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
330          emsg);
331     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
332     GNUNET_CONTAINER_meta_data_destroy (*meta);
333     *meta = NULL;
334     GNUNET_free_non_null (*ns_name);
335     *ns_name = NULL;
336     GNUNET_free_non_null (emsg);
337     GNUNET_free (fn);
338     return GNUNET_SYSERR;
339   }
340   GNUNET_free (fn);
341   return GNUNET_OK;
342 }
343
344
345 /**
346  * Return unique variant of the namespace name.  Use it after
347  * GNUNET_PSEUDONYM_get_info() to make sure that name is unique.
348  *
349  * @param cfg configuration
350  * @param pseudonym public key of the pseudonym
351  * @param name name to uniquify
352  * @param suffix if not NULL, filled with the suffix value
353  * @return NULL on failure (should never happen), name on success.
354  *         Free the name with GNUNET_free().
355  */
356 char *
357 GNUNET_PSEUDONYM_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
358                                 const struct GNUNET_PseudonymIdentifier *pseudonym,
359                                 const char *name,
360                                 unsigned int *suffix)
361 {
362   struct GNUNET_HashCode nh;
363   struct GNUNET_PseudonymIdentifier pi;
364   uint64_t len;
365   char *fn;
366   struct GNUNET_DISK_FileHandle *fh;
367   unsigned int i;
368   unsigned int idx;
369   char *ret;
370   struct stat sbuf;
371
372   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
373   fn = get_data_filename_hash (cfg, PS_NAMES_DIR, &nh);
374   len = 0;
375   if (0 == STAT (fn, &sbuf))
376     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES));
377   fh = GNUNET_DISK_file_open (fn,
378                               GNUNET_DISK_OPEN_CREATE |
379                               GNUNET_DISK_OPEN_READWRITE,
380                               GNUNET_DISK_PERM_USER_READ |
381                               GNUNET_DISK_PERM_USER_WRITE);
382   i = 0;
383   idx = -1;
384   while ((len >= sizeof (struct GNUNET_PseudonymIdentifier)) &&
385          (sizeof (struct GNUNET_PseudonymIdentifier) ==
386           GNUNET_DISK_file_read (fh, &pi, sizeof (struct GNUNET_PseudonymIdentifier))))
387   {
388     if (0 == memcmp (&pi, pseudonym, sizeof (struct GNUNET_PseudonymIdentifier)))
389     {
390       idx = i;
391       break;
392     }
393     i++;
394     len -= sizeof (struct GNUNET_HashCode);
395   }
396   if (-1 == idx)
397   {
398     idx = i;
399     if (sizeof (struct GNUNET_PseudonymIdentifier) !=
400         GNUNET_DISK_file_write (fh, pseudonym, sizeof (struct GNUNET_PseudonymIdentifier)))
401       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
402   }
403   GNUNET_DISK_file_close (fh);
404   ret = GNUNET_malloc (strlen (name) + 32);
405   GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
406   if (suffix != NULL)
407     *suffix = idx;
408   GNUNET_free (fn);
409   return ret;
410 }
411
412
413 /**
414  * Get namespace name, metadata and rank
415  * This is a wrapper around internal read_info() call, and ensures that
416  * returned data is not invalid (not NULL).
417  *
418  * @param cfg configuration
419  * @param pseudonym public key of the pseudonym
420  * @param ret_meta a location to store metadata pointer. NULL, if metadata
421  *        is not needed. Destroy with GNUNET_CONTAINER_meta_data_destroy().
422  * @param ret_rank a location to store rank. NULL, if rank not needed.
423  * @param ret_name a location to store human-readable name. Name is not unique.
424  *        NULL, if name is not needed. Free with GNUNET_free().
425  * @param name_is_a_dup is set to GNUNET_YES, if ret_name was filled with
426  *        a duplicate of a "no-name" placeholder
427  * @return GNUNET_OK on success. GNUENT_SYSERR if the data was
428  *         unobtainable (in that case ret_* are filled with placeholders - 
429  *         empty metadata container, rank -1 and a "no-name" name).
430  */
431 int
432 GNUNET_PSEUDONYM_get_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
433                            const struct GNUNET_PseudonymIdentifier *pseudonym, 
434                            struct GNUNET_CONTAINER_MetaData **ret_meta,
435                            int32_t *ret_rank, 
436                            char **ret_name, 
437                            int *name_is_a_dup)
438 {
439   struct GNUNET_CONTAINER_MetaData *meta;
440   char *name;
441   int32_t rank = -1;
442
443   meta = NULL;
444   name = NULL;
445   if (GNUNET_OK == read_info (cfg, pseudonym, &meta, &rank, &name))
446   {
447     if ((meta != NULL) && (name == NULL))
448       name =
449           GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
450                                                          EXTRACTOR_METATYPE_TITLE,
451                                                          EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
452                                                          EXTRACTOR_METATYPE_FILENAME,
453                                                          EXTRACTOR_METATYPE_DESCRIPTION,
454                                                          EXTRACTOR_METATYPE_SUBJECT,
455                                                          EXTRACTOR_METATYPE_PUBLISHER,
456                                                          EXTRACTOR_METATYPE_AUTHOR_NAME,
457                                                          EXTRACTOR_METATYPE_COMMENT,
458                                                          EXTRACTOR_METATYPE_SUMMARY,
459                                                          -1);
460     if (ret_name != NULL)
461     {
462       if (name == NULL)
463       {
464         name = GNUNET_strdup (_("no-name"));
465         if (name_is_a_dup != NULL)
466           *name_is_a_dup = GNUNET_YES;
467       }
468       else if (name_is_a_dup != NULL)
469         *name_is_a_dup = GNUNET_NO;
470       *ret_name = name;
471     }
472     else if (name != NULL)
473       GNUNET_free (name);
474
475     if (ret_meta != NULL)
476     {
477       if (meta == NULL)
478         meta = GNUNET_CONTAINER_meta_data_create ();
479       *ret_meta = meta;
480     }
481     else if (meta != NULL)
482       GNUNET_CONTAINER_meta_data_destroy (meta);
483
484     if (ret_rank != NULL)
485       *ret_rank = rank;
486
487     return GNUNET_OK;
488   }
489   if (ret_name != NULL)
490     *ret_name = GNUNET_strdup (_("no-name"));
491   if (ret_meta != NULL)
492     *ret_meta = GNUNET_CONTAINER_meta_data_create ();
493   if (ret_rank != NULL)
494     *ret_rank = -1;
495   if (name_is_a_dup != NULL)
496     *name_is_a_dup = GNUNET_YES;
497   return GNUNET_SYSERR;
498 }
499
500
501 /**
502  * Get the namespace ID belonging to the given namespace name.
503  *
504  * @param cfg configuration to use
505  * @param ns_uname unique (!) human-readable name for the namespace
506  * @param pseudonym set to public key of pseudonym based on 'ns_uname'
507  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
508  */
509 int
510 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
511                              const char *ns_uname, 
512                              struct GNUNET_PseudonymIdentifier *pseudonym)
513 {
514   size_t slen;
515   uint64_t len;
516   unsigned int idx;
517   char *name;
518   struct GNUNET_HashCode nh;
519   char *fn;
520   struct GNUNET_DISK_FileHandle *fh;
521
522   idx = -1;
523   slen = strlen (ns_uname);
524   while ((slen > 0) && (1 != SSCANF (&ns_uname[slen - 1], "-%u", &idx)))
525     slen--;
526   if (0 == slen)
527     return GNUNET_SYSERR;
528   name = GNUNET_strdup (ns_uname);
529   name[slen - 1] = '\0';
530
531   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
532   GNUNET_free (name);
533   fn = get_data_filename_hash (cfg, PS_NAMES_DIR, &nh);
534
535   if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
536        (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES))) ||
537       ((idx + 1) * sizeof (struct GNUNET_PseudonymIdentifier) > len))
538   {
539     GNUNET_free (fn);
540     return GNUNET_SYSERR;
541   }
542   fh = GNUNET_DISK_file_open (fn,
543                               GNUNET_DISK_OPEN_CREATE |
544                               GNUNET_DISK_OPEN_READWRITE,
545                               GNUNET_DISK_PERM_USER_READ |
546                               GNUNET_DISK_PERM_USER_WRITE);
547   GNUNET_free (fn);
548   if (GNUNET_SYSERR ==
549       GNUNET_DISK_file_seek (fh, idx * sizeof (struct GNUNET_PseudonymIdentifier),
550                              GNUNET_DISK_SEEK_SET))
551   {
552     GNUNET_DISK_file_close (fh);
553     return GNUNET_SYSERR;
554   }
555   if (sizeof (struct GNUNET_PseudonymIdentifier) !=
556       GNUNET_DISK_file_read (fh, pseudonym, sizeof (struct GNUNET_PseudonymIdentifier)))
557   {
558     GNUNET_DISK_file_close (fh);
559     return GNUNET_SYSERR;
560   }
561   GNUNET_DISK_file_close (fh);
562   return GNUNET_OK;
563 }
564
565
566
567 /**
568  * struct used to list the pseudonym
569  */
570 struct ListPseudonymClosure
571 {
572
573   /**
574    * iterator over pseudonym
575    */
576   GNUNET_PSEUDONYM_Iterator iterator;
577
578   /**
579    * Closure for iterator.
580    */
581   void *iterator_cls;
582
583   /**
584    * Configuration to use.
585    */
586   const struct GNUNET_CONFIGURATION_Handle *cfg;
587 };
588
589
590
591 /**
592  * Helper function to list all available pseudonyms
593  *
594  * @param cls point to a struct ListPseudonymClosure
595  * @param fullname name of pseudonym
596  */
597 static int
598 list_pseudonym_helper (void *cls, const char *fullname)
599 {
600   struct ListPseudonymClosure *lpc = cls;
601   struct GNUNET_PseudonymIdentifier pd;
602   char *emsg;
603   struct GNUNET_BIO_ReadHandle *fileR;
604   int32_t rank;
605   char *ns_name;
606   struct GNUNET_CONTAINER_MetaData *meta;
607   int ret; 
608   char *name_unique;
609
610   if (NULL == (fileR = GNUNET_BIO_read_open (fullname)))
611     return GNUNET_SYSERR;
612   emsg = NULL;
613   ns_name = NULL;
614   if ( (GNUNET_OK != GNUNET_BIO_read (fileR, "pseudonym", &pd, sizeof (pd))) ||
615        (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, &rank)) ||
616        (GNUNET_OK !=
617         GNUNET_BIO_read_string (fileR, "Read string error!", &ns_name, 200)) ||
618        (GNUNET_OK !=
619        GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", &meta)) )
620   {
621     (void) GNUNET_BIO_read_close (fileR, &emsg);
622     GNUNET_free_non_null (emsg);
623     GNUNET_free_non_null (ns_name);
624     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fullname));
625     return GNUNET_SYSERR;
626   }
627   if (NULL == ns_name)
628     ns_name = GNUNET_strdup (_("no-name"));
629   if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
630   {
631     LOG (GNUNET_ERROR_TYPE_WARNING,
632          _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fullname,
633          emsg);
634     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fullname));
635     GNUNET_CONTAINER_meta_data_destroy (meta);
636     GNUNET_free (ns_name);
637     GNUNET_free_non_null (emsg);
638     return GNUNET_SYSERR;
639   }
640   ret = GNUNET_OK;
641   name_unique = GNUNET_PSEUDONYM_name_uniquify (lpc->cfg, &pd, ns_name, NULL);
642   if (NULL != lpc->iterator)
643     ret = lpc->iterator (lpc->iterator_cls, &pd, ns_name, name_unique, meta, rank);
644   GNUNET_free (ns_name);
645   GNUNET_free_non_null (name_unique);
646   GNUNET_CONTAINER_meta_data_destroy (meta);
647   return ret;
648 }
649
650
651 /**
652  * List all available pseudonyms.
653  *
654  * @param cfg overall configuration
655  * @param iterator function to call for each pseudonym
656  * @param iterator_cls closure for iterator
657  * @return number of pseudonyms found
658  */
659 int
660 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
661                            GNUNET_PSEUDONYM_Iterator iterator, 
662                            void *iterator_cls)
663 {
664   struct ListPseudonymClosure cls;
665   char *fn;
666   int ret;
667
668   cls.iterator = iterator;
669   cls.iterator_cls = iterator_cls;
670   cls.cfg = cfg;
671   fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
672   GNUNET_assert (fn != NULL);
673   GNUNET_DISK_directory_create (fn);
674   ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
675   GNUNET_free (fn);
676   return ret;
677 }
678
679
680 /**
681  * Change the rank of a pseudonym.
682  *
683  * @param cfg overall configuration
684  * @param pseudonym the pseudonym
685  * @param delta by how much should the rating be changed?
686  * @return new rating of the pseudonym
687  */
688 int
689 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
690                        const struct GNUNET_PseudonymIdentifier *pseudonym, 
691                        int32_t delta)
692 {
693   struct GNUNET_CONTAINER_MetaData *meta;
694   int ret;
695   int32_t rank;
696   char *name;
697
698   name = NULL;
699   ret = read_info (cfg, pseudonym, &meta, &rank, &name);
700   if (ret == GNUNET_SYSERR)
701   {
702     rank = 0;
703     meta = GNUNET_CONTAINER_meta_data_create ();
704   }
705   rank += delta;
706   GNUNET_PSEUDONYM_set_info (cfg, pseudonym, name, meta, rank);
707   GNUNET_CONTAINER_meta_data_destroy (meta);
708   GNUNET_free_non_null (name);
709   return rank;
710 }
711
712
713 /**
714  * Add a pseudonym to the set of known pseudonyms.
715  * For all pseudonym advertisements that we discover
716  * FS should automatically call this function.
717  *
718  * @param cfg overall configuration
719  * @param pseudonym the pseudonym to add
720  * @param meta metadata for the pseudonym
721  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
722  */
723 int
724 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
725                       const struct GNUNET_PseudonymIdentifier *pseudonym,
726                       const struct GNUNET_CONTAINER_MetaData *meta)
727 {
728   char *name;
729   int32_t rank;
730   struct GNUNET_CONTAINER_MetaData *old;
731   char *fn;
732   struct stat sbuf;
733   int ret;
734
735   rank = 0;
736   fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
737   GNUNET_assert (fn != NULL);
738
739   if ((0 == STAT (fn, &sbuf)) &&
740       (GNUNET_OK == read_info (cfg, pseudonym, &old, &rank, &name)))
741   {
742     GNUNET_CONTAINER_meta_data_merge (old, meta);
743     ret = GNUNET_PSEUDONYM_set_info (cfg, pseudonym, name, old, rank);
744     GNUNET_CONTAINER_meta_data_destroy (old);
745     GNUNET_free_non_null (name);
746   }
747   else
748   {
749     ret = GNUNET_PSEUDONYM_set_info (cfg, pseudonym, NULL, meta, rank);
750   }
751   GNUNET_free (fn);
752   internal_notify (pseudonym, meta, rank);
753   return ret;
754 }
755
756
757 /* ***************************** cryptographic operations ************************* */
758
759 /**
760  * Handle for a pseudonym (private key).
761  */
762 struct GNUNET_PseudonymHandle
763 {
764   /**
765    * FIXME.
766    */
767   char data[42];
768 };
769
770
771 /**
772  * Create a pseudonym.
773  *
774  * @param filename name of the file to use for storage, NULL for in-memory only
775  * @return handle to the private key of the pseudonym
776  */
777 struct GNUNET_PseudonymHandle *
778 GNUNET_PSEUDONYM_create (const char *filename)
779 {
780   struct GNUNET_PseudonymHandle *ph;
781   ssize_t ret;
782
783   ph = GNUNET_malloc (sizeof (struct GNUNET_PseudonymHandle));
784   if (NULL != filename)
785   {
786     ret = GNUNET_DISK_fn_read (filename, ph, 
787                                sizeof (struct GNUNET_PseudonymHandle));
788     if (sizeof (struct GNUNET_PseudonymHandle) == ret)
789       return ph;
790   }
791   GNUNET_break (0); // not implemented...
792   gcry_randomize (ph, sizeof (struct GNUNET_PseudonymHandle),
793                   GCRY_STRONG_RANDOM);
794   if (NULL != filename)
795   {
796     ret = GNUNET_DISK_fn_write (filename, ph, sizeof (struct GNUNET_PseudonymHandle),
797                                 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
798     if (sizeof (struct GNUNET_PseudonymHandle) != ret)
799     {
800       GNUNET_free (ph);
801       return NULL;
802     }
803   }
804   return ph;
805 }
806
807
808 /**
809  * Create a pseudonym, from a file that must already exist.
810  *
811  * @param filename name of the file to use for storage, NULL for in-memory only
812  * @return handle to the private key of the pseudonym
813  */
814 struct GNUNET_PseudonymHandle *
815 GNUNET_PSEUDONYM_create_from_existing_file (const char *filename)
816 {
817   struct GNUNET_PseudonymHandle *ph;
818   ssize_t ret;
819
820   ph = GNUNET_malloc (sizeof (struct GNUNET_PseudonymHandle));
821   ret = GNUNET_DISK_fn_read (filename, ph, 
822                              sizeof (struct GNUNET_PseudonymHandle));
823   if (sizeof (struct GNUNET_PseudonymHandle) == ret)
824     return ph;
825   GNUNET_free (ph);
826   return NULL;
827 }
828
829
830 /**
831  * Get the handle for the 'anonymous' pseudonym shared by all users.
832  * That pseudonym uses a fixed 'secret' for the private key; this
833  * construction is useful to make anonymous and pseudonymous APIs
834  * (and packets) indistinguishable on the network.  See #2564.
835  *
836  * @return handle to the (non-secret) private key of the 'anonymous' pseudonym
837  */
838 struct GNUNET_PseudonymHandle *
839 GNUNET_PSEUDONYM_get_anonymous_pseudonym_handle ()
840 {
841   struct GNUNET_PseudonymHandle *ph;
842
843   ph = GNUNET_malloc (sizeof (struct GNUNET_PseudonymHandle));
844   GNUNET_break (0);
845   return ph;
846 }
847
848
849 /**
850  * Destroy a pseudonym handle.  Does NOT remove the private key from
851  * the disk.
852  *
853  * @param ph pseudonym handle to destroy
854  */
855 void
856 GNUNET_PSEUDONYM_destroy (struct GNUNET_PseudonymHandle *ph)
857 {
858   GNUNET_free (ph);
859 }
860
861
862 /**
863  * Cryptographically sign some data with the pseudonym.
864  *
865  * @param ph private key used for signing (corresponds to 'x' in #2564)
866  * @param purpose data to sign
867  * @param seed hash of the plaintext of the data that we are signing, 
868  *             used for deterministic PRNG for anonymous signing;
869  *             corresponds to 'k' in section 2.7 of #2564
870  * @param signing_key modifier to apply to the private key for signing;
871  *                    corresponds to 'h' in section 2.3 of #2564.
872  * @param signature where to store the signature
873  */
874 void
875 GNUNET_PSEUDONYM_sign (struct GNUNET_PseudonymHandle *ph,
876                        const struct GNUNET_PseudonymSignaturePurpose *purpose,
877                        const struct GNUNET_HashCode *seed,
878                        const struct GNUNET_HashCode *signing_key,
879                        struct GNUNET_PseudonymSignature *signature)
880 {
881   memset (signature, 0, sizeof (struct GNUNET_PseudonymSignature));
882   GNUNET_break (0);
883 }
884
885
886 /**
887  * Given a pseudonym and a signing key, derive the corresponding public
888  * key that would be used to verify the resulting signature.
889  *
890  * @param pseudonym the public key (g^x)
891  * @param signing_key input to derive 'h' (see section 2.4 of #2564)
892  * @param verification_key resulting public key to verify the signature
893  *        created from the 'ph' of 'pseudonym' and the 'signing_key';
894  *        the value stored here can then be given to GNUNET_PSEUDONYM_verify.
895  */
896 void
897 GNUNET_PSEUDONYM_derive_verification_key (struct GNUNET_PseudonymIdentifier *pseudonym,
898                                           const struct GNUNET_HashCode *signing_key,
899                                           struct GNUNET_PseudonymIdentifier *verification_key)
900 {
901   struct GNUNET_HashCode hc;
902   struct GNUNET_HashCode x;
903
904   GNUNET_break (0);
905   GNUNET_CRYPTO_hash (pseudonym, sizeof (*pseudonym), &hc);
906   GNUNET_CRYPTO_hash_xor (&hc, signing_key, &x);  
907   memset (verification_key, 0, sizeof (struct GNUNET_PseudonymIdentifier));
908   memcpy (verification_key, &x, GNUNET_MIN (sizeof (x), sizeof (*verification_key)));
909 }
910
911
912 /**
913  * Verify a signature made with a pseudonym.
914  *
915  * @param purpose data that was signed
916  * @param signature signature to verify
917  * @param verification_key public key to use for checking the signature;
918  *                    corresponds to 'g^(x+h)' in section 2.4 of #2564.
919  * @return GNUNET_OK on success (signature valid, 'pseudonym' set),
920  *         GNUNET_SYSERR if the signature is invalid
921  */
922 int
923 GNUNET_PSEUDONYM_verify (const struct GNUNET_PseudonymSignaturePurpose *purpose,
924                          const struct GNUNET_PseudonymSignature *signature,
925                          const struct GNUNET_PseudonymIdentifier *verification_key)
926 {
927   GNUNET_break (0);
928   return GNUNET_OK;
929 }
930
931
932 /**
933  * Get the identifier (public key) of a pseudonym.
934  *
935  * @param ph pseudonym handle with the private key
936  * @param pseudonym pseudonym identifier (set based on 'ph')
937  */
938 void
939 GNUNET_PSEUDONYM_get_identifier (struct GNUNET_PseudonymHandle *ph,
940                                  struct GNUNET_PseudonymIdentifier *pseudonym)
941 {
942   GNUNET_break (0);
943   memcpy (pseudonym, ph, 
944           GNUNET_MIN (sizeof (struct GNUNET_PseudonymIdentifier),
945                       sizeof (*ph)));
946 }
947
948
949 /* end of pseudonym.c */