2 This file is part of GNUnet
3 (C) 2003-2013 Christian Grothoff (and other contributing authors)
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 3, or (at your
8 option) any later version.
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.
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.
21 * @file fs/fs_pseudonym.c
22 * @brief pseudonym functions; these functions are about namespaces
23 * managed by other users; we might want to eliminate this
24 * entire API and instead manage pseudonyms only via GNS
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
40 * Name of the directory which stores meta data for pseudonym
42 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonym" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
45 * Name of the directory which stores names for pseudonyms
47 #define PS_NAMES_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonym" DIR_SEPARATOR_STR "names" DIR_SEPARATOR_STR
51 * Configuration section we use.
53 #define GNUNET_CLIENT_SERVICE_NAME "fs"
56 /* ************************* Disk operations (pseudonym data mgmt) **************** */
59 * Registered callbacks for discovery of pseudonyms.
61 struct GNUNET_FS_Pseudonym_DiscoveryHandle
64 * This is a doubly linked list.
66 struct GNUNET_FS_Pseudonym_DiscoveryHandle *next;
69 * This is a doubly linked list.
71 struct GNUNET_FS_Pseudonym_DiscoveryHandle *prev;
74 * Function to call each time a pseudonym is discovered.
76 GNUNET_FS_PseudonymIterator callback;
79 * Closure for callback.
86 * Head of the linked list of functions to call when
87 * new pseudonyms are added.
89 static struct GNUNET_FS_Pseudonym_DiscoveryHandle *disco_head;
92 * Tail of the linked list of functions to call when
93 * new pseudonyms are added.
95 static struct GNUNET_FS_Pseudonym_DiscoveryHandle *disco_tail;
99 * Internal notification about new tracked URI.
101 * @param pseudonym public key of the pseudonym
102 * @param md meta data to be written
103 * @param rating rating of pseudonym
106 internal_notify (const struct GNUNET_CRYPTO_EccPublicKey *pseudonym,
107 const struct GNUNET_CONTAINER_MetaData *md, int rating)
109 struct GNUNET_FS_Pseudonym_DiscoveryHandle *pos;
111 for (pos = disco_head; NULL != pos; pos = pos->next)
112 pos->callback (pos->callback_cls, pseudonym, NULL, NULL, md, rating);
117 * Register callback to be invoked whenever we discover
119 * Will immediately call provided iterator callback for all
120 * already discovered pseudonyms.
122 * @param cfg configuration to use
123 * @param iterator iterator over pseudonym
124 * @param iterator_cls point to a closure
125 * @return registration handle
127 struct GNUNET_FS_Pseudonym_DiscoveryHandle *
128 GNUNET_FS_pseudonym_discovery_callback_register (const struct
129 GNUNET_CONFIGURATION_Handle *cfg,
130 GNUNET_FS_PseudonymIterator iterator,
133 struct GNUNET_FS_Pseudonym_DiscoveryHandle *dh;
135 dh = GNUNET_new (struct GNUNET_FS_Pseudonym_DiscoveryHandle);
136 dh->callback = iterator;
137 dh->callback_cls = iterator_cls;
138 GNUNET_CONTAINER_DLL_insert (disco_head, disco_tail, dh);
139 GNUNET_FS_pseudonym_list_all (cfg, iterator, iterator_cls);
145 * Unregister pseudonym discovery callback.
147 * @param dh registration to unregister
150 GNUNET_FS_pseudonym_discovery_callback_unregister (struct GNUNET_FS_Pseudonym_DiscoveryHandle *dh)
152 GNUNET_CONTAINER_DLL_remove (disco_head, disco_tail, dh);
158 * Get the filename (or directory name) for the given
159 * pseudonym identifier and directory prefix.
161 * @param cfg configuration to use
162 * @param prefix path components to append to the private directory name
163 * @param pseudonym the pseudonym, can be NULL
164 * @return filename of the pseudonym (if pseudonym != NULL) or directory with the data (if pseudonym == NULL)
167 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
169 const struct GNUNET_CRYPTO_EccPublicKey *pseudonym)
171 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
172 struct GNUNET_HashCode psid;
174 if (NULL != pseudonym)
176 GNUNET_CRYPTO_hash (pseudonym,
177 sizeof (struct GNUNET_CRYPTO_EccPublicKey),
179 GNUNET_CRYPTO_hash_to_enc (&psid, &enc);
181 return GNUNET_DISK_get_home_filename (cfg,
182 GNUNET_CLIENT_SERVICE_NAME, prefix,
185 : (const char *) &enc,
191 * Get the filename (or directory name) for the given
192 * hash code and directory prefix.
194 * @param cfg configuration to use
195 * @param prefix path components to append to the private directory name
196 * @param hc some hash code
197 * @return filename of the pseudonym (if hc != NULL) or directory with the data (if hc == NULL)
200 get_data_filename_hash (const struct GNUNET_CONFIGURATION_Handle *cfg,
202 const struct GNUNET_HashCode *hc)
204 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
207 GNUNET_CRYPTO_hash_to_enc (hc, &enc);
208 return GNUNET_DISK_get_home_filename (cfg,
209 GNUNET_CLIENT_SERVICE_NAME, prefix,
212 : (const char *) &enc,
218 * Set the pseudonym metadata, rank and name.
219 * Writes the pseudonym infomation into a file
221 * @param cfg overall configuration
222 * @param pseudonym id of the pseudonym
223 * @param name name to set. Must be the non-unique version of it.
224 * May be NULL, in which case it erases pseudonym's name!
225 * @param md metadata to set
226 * May be NULL, in which case it erases pseudonym's metadata!
227 * @param rank rank to assign
228 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
231 GNUNET_FS_pseudonym_set_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
232 const struct GNUNET_CRYPTO_EccPublicKey *pseudonym,
234 const struct GNUNET_CONTAINER_MetaData *md,
238 struct GNUNET_BIO_WriteHandle *fileW;
240 fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
241 if (NULL == (fileW = GNUNET_BIO_write_open (fn)))
244 return GNUNET_SYSERR;
246 if ((GNUNET_OK != GNUNET_BIO_write (fileW, pseudonym,
247 sizeof (struct GNUNET_CRYPTO_EccPublicKey))) ||
248 (GNUNET_OK != GNUNET_BIO_write_int32 (fileW, rank)) ||
249 (GNUNET_OK != GNUNET_BIO_write_string (fileW, name)) ||
250 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, md)))
252 (void) GNUNET_BIO_write_close (fileW);
253 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
255 return GNUNET_SYSERR;
257 if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
259 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
261 return GNUNET_SYSERR;
264 /* create entry for pseudonym name in names */
266 GNUNET_free_non_null (GNUNET_FS_pseudonym_name_uniquify (cfg, pseudonym,
273 * Read pseudonym infomation from a file
275 * @param cfg configuration to use
276 * @param pseudonym hash code of a pseudonym
277 * @param meta meta data to be read from a file
278 * @param rank rank of a pseudonym
279 * @param ns_name name of a pseudonym
280 * @return GNUNET_OK on success, GNUNET_SYSERR on error
283 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
284 const struct GNUNET_CRYPTO_EccPublicKey *pseudonym,
285 struct GNUNET_CONTAINER_MetaData **meta,
289 struct GNUNET_CRYPTO_EccPublicKey pd;
292 struct GNUNET_BIO_ReadHandle *fileR;
294 fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
296 GNUNET_DISK_file_test (fn))
299 return GNUNET_SYSERR;
301 if (NULL == (fileR = GNUNET_BIO_read_open (fn)))
304 return GNUNET_SYSERR;
308 if ( (GNUNET_OK != GNUNET_BIO_read (fileR, "pseudonym", &pd, sizeof (pd))) ||
309 (0 != memcmp (&pd, pseudonym, sizeof (pd))) ||
310 (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, rank)) ||
312 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
314 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)) )
316 (void) GNUNET_BIO_read_close (fileR, &emsg);
317 GNUNET_free_non_null (emsg);
318 GNUNET_free_non_null (*ns_name);
320 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
322 return GNUNET_SYSERR;
324 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
326 LOG (GNUNET_ERROR_TYPE_WARNING,
327 _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
329 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
330 GNUNET_CONTAINER_meta_data_destroy (*meta);
332 GNUNET_free_non_null (*ns_name);
334 GNUNET_free_non_null (emsg);
336 return GNUNET_SYSERR;
344 * Return unique variant of the namespace name. Use it after
345 * GNUNET_FS_pseudonym_get_info() to make sure that name is unique.
347 * @param cfg configuration
348 * @param pseudonym public key of the pseudonym
349 * @param name name to uniquify
350 * @param suffix if not NULL, filled with the suffix value
351 * @return NULL on failure (should never happen), name on success.
352 * Free the name with GNUNET_free().
355 GNUNET_FS_pseudonym_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
356 const struct GNUNET_CRYPTO_EccPublicKey *pseudonym,
358 unsigned int *suffix)
360 struct GNUNET_HashCode nh;
361 struct GNUNET_CRYPTO_EccPublicKey pi;
364 struct GNUNET_DISK_FileHandle *fh;
370 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
371 fn = get_data_filename_hash (cfg, PS_NAMES_DIR, &nh);
373 if (0 == STAT (fn, &sbuf))
374 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES));
375 fh = GNUNET_DISK_file_open (fn,
376 GNUNET_DISK_OPEN_CREATE |
377 GNUNET_DISK_OPEN_READWRITE,
378 GNUNET_DISK_PERM_USER_READ |
379 GNUNET_DISK_PERM_USER_WRITE);
382 while ((len >= sizeof (struct GNUNET_CRYPTO_EccPublicKey)) &&
383 (sizeof (struct GNUNET_CRYPTO_EccPublicKey) ==
384 GNUNET_DISK_file_read (fh, &pi, sizeof (struct GNUNET_CRYPTO_EccPublicKey))))
386 if (0 == memcmp (&pi, pseudonym, sizeof (struct GNUNET_CRYPTO_EccPublicKey)))
392 len -= sizeof (struct GNUNET_HashCode);
397 if (sizeof (struct GNUNET_CRYPTO_EccPublicKey) !=
398 GNUNET_DISK_file_write (fh, pseudonym,
399 sizeof (struct GNUNET_CRYPTO_EccPublicKey)))
400 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
402 GNUNET_DISK_file_close (fh);
403 ret = GNUNET_malloc (strlen (name) + 32);
404 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
413 * Get namespace name, metadata and rank
414 * This is a wrapper around internal read_info() call, and ensures that
415 * returned data is not invalid (not NULL).
417 * @param cfg configuration
418 * @param pseudonym public key of the pseudonym
419 * @param ret_meta a location to store metadata pointer. NULL, if metadata
420 * is not needed. Destroy with GNUNET_CONTAINER_meta_data_destroy().
421 * @param ret_rank a location to store rank. NULL, if rank not needed.
422 * @param ret_name a location to store human-readable name. Name is not unique.
423 * NULL, if name is not needed. Free with GNUNET_free().
424 * @param name_is_a_dup is set to GNUNET_YES, if ret_name was filled with
425 * a duplicate of a "no-name" placeholder
426 * @return GNUNET_OK on success. GNUENT_SYSERR if the data was
427 * unobtainable (in that case ret_* are filled with placeholders -
428 * empty metadata container, rank -1 and a "no-name" name).
431 GNUNET_FS_pseudonym_get_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
432 const struct GNUNET_CRYPTO_EccPublicKey *pseudonym,
433 struct GNUNET_CONTAINER_MetaData **ret_meta,
438 struct GNUNET_CONTAINER_MetaData *meta;
444 if (GNUNET_OK == read_info (cfg, pseudonym, &meta, &rank, &name))
446 if ((meta != NULL) && (name == NULL))
448 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
449 EXTRACTOR_METATYPE_TITLE,
450 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
451 EXTRACTOR_METATYPE_FILENAME,
452 EXTRACTOR_METATYPE_DESCRIPTION,
453 EXTRACTOR_METATYPE_SUBJECT,
454 EXTRACTOR_METATYPE_PUBLISHER,
455 EXTRACTOR_METATYPE_AUTHOR_NAME,
456 EXTRACTOR_METATYPE_COMMENT,
457 EXTRACTOR_METATYPE_SUMMARY,
459 if (ret_name != NULL)
463 name = GNUNET_strdup (_("no-name"));
464 if (name_is_a_dup != NULL)
465 *name_is_a_dup = GNUNET_YES;
467 else if (name_is_a_dup != NULL)
468 *name_is_a_dup = GNUNET_NO;
471 else if (name != NULL)
474 if (ret_meta != NULL)
477 meta = GNUNET_CONTAINER_meta_data_create ();
480 else if (meta != NULL)
481 GNUNET_CONTAINER_meta_data_destroy (meta);
483 if (ret_rank != NULL)
488 if (ret_name != NULL)
489 *ret_name = GNUNET_strdup (_("no-name"));
490 if (ret_meta != NULL)
491 *ret_meta = GNUNET_CONTAINER_meta_data_create ();
492 if (ret_rank != NULL)
494 if (name_is_a_dup != NULL)
495 *name_is_a_dup = GNUNET_YES;
496 return GNUNET_SYSERR;
501 * Get the namespace ID belonging to the given namespace name.
503 * @param cfg configuration to use
504 * @param ns_uname unique (!) human-readable name for the namespace
505 * @param pseudonym set to public key of pseudonym based on 'ns_uname'
506 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
509 GNUNET_FS_pseudonym_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
510 const char *ns_uname,
511 struct GNUNET_CRYPTO_EccPublicKey *pseudonym)
517 struct GNUNET_HashCode nh;
519 struct GNUNET_DISK_FileHandle *fh;
522 slen = strlen (ns_uname);
523 while ((slen > 0) && (1 != SSCANF (&ns_uname[slen - 1], "-%u", &idx)))
526 return GNUNET_SYSERR;
527 name = GNUNET_strdup (ns_uname);
528 name[slen - 1] = '\0';
530 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
532 fn = get_data_filename_hash (cfg, PS_NAMES_DIR, &nh);
534 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
535 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES))) ||
536 ((idx + 1) * sizeof (struct GNUNET_CRYPTO_EccPublicKey) > len))
539 return GNUNET_SYSERR;
541 fh = GNUNET_DISK_file_open (fn,
542 GNUNET_DISK_OPEN_CREATE |
543 GNUNET_DISK_OPEN_READWRITE,
544 GNUNET_DISK_PERM_USER_READ |
545 GNUNET_DISK_PERM_USER_WRITE);
548 GNUNET_DISK_file_seek (fh, idx * sizeof (struct GNUNET_CRYPTO_EccPublicKey),
549 GNUNET_DISK_SEEK_SET))
551 GNUNET_DISK_file_close (fh);
552 return GNUNET_SYSERR;
554 if (sizeof (struct GNUNET_CRYPTO_EccPublicKey) !=
555 GNUNET_DISK_file_read (fh, pseudonym,
556 sizeof (struct GNUNET_CRYPTO_EccPublicKey)))
558 GNUNET_DISK_file_close (fh);
559 return GNUNET_SYSERR;
561 GNUNET_DISK_file_close (fh);
567 * struct used to list the pseudonym
569 struct ListPseudonymClosure
573 * iterator over pseudonym
575 GNUNET_FS_PseudonymIterator iterator;
578 * Closure for iterator.
583 * Configuration to use.
585 const struct GNUNET_CONFIGURATION_Handle *cfg;
591 * Helper function to list all available pseudonyms
593 * @param cls point to a struct ListPseudonymClosure
594 * @param fullname name of pseudonym
597 list_pseudonym_helper (void *cls, const char *fullname)
599 struct ListPseudonymClosure *lpc = cls;
600 struct GNUNET_CRYPTO_EccPublicKey pd;
602 struct GNUNET_BIO_ReadHandle *fileR;
605 struct GNUNET_CONTAINER_MetaData *meta;
609 if (NULL == (fileR = GNUNET_BIO_read_open (fullname)))
610 return GNUNET_SYSERR;
613 if ( (GNUNET_OK != GNUNET_BIO_read (fileR, "pseudonym", &pd, sizeof (pd))) ||
614 (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, &rank)) ||
616 GNUNET_BIO_read_string (fileR, "Read string error!", &ns_name, 200)) ||
618 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", &meta)) )
620 (void) GNUNET_BIO_read_close (fileR, &emsg);
621 GNUNET_free_non_null (emsg);
622 GNUNET_free_non_null (ns_name);
623 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fullname));
624 return GNUNET_SYSERR;
627 ns_name = GNUNET_strdup (_("no-name"));
628 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
630 LOG (GNUNET_ERROR_TYPE_WARNING,
631 _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fullname,
633 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fullname));
634 GNUNET_CONTAINER_meta_data_destroy (meta);
635 GNUNET_free (ns_name);
636 GNUNET_free_non_null (emsg);
637 return GNUNET_SYSERR;
640 name_unique = GNUNET_FS_pseudonym_name_uniquify (lpc->cfg, &pd, ns_name, NULL);
641 if (NULL != lpc->iterator)
642 ret = lpc->iterator (lpc->iterator_cls, &pd, ns_name, name_unique, meta, rank);
643 GNUNET_free (ns_name);
644 GNUNET_free_non_null (name_unique);
645 GNUNET_CONTAINER_meta_data_destroy (meta);
651 * List all available pseudonyms.
653 * @param cfg overall configuration
654 * @param iterator function to call for each pseudonym
655 * @param iterator_cls closure for iterator
656 * @return number of pseudonyms found
659 GNUNET_FS_pseudonym_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
660 GNUNET_FS_PseudonymIterator iterator,
663 struct ListPseudonymClosure cls;
667 cls.iterator = iterator;
668 cls.iterator_cls = iterator_cls;
670 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
671 GNUNET_assert (fn != NULL);
672 GNUNET_DISK_directory_create (fn);
673 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
680 * Change the rank of a pseudonym.
682 * @param cfg overall configuration
683 * @param pseudonym the pseudonym
684 * @param delta by how much should the rating be changed?
685 * @return new rating of the pseudonym
688 GNUNET_FS_pseudonym_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
689 const struct GNUNET_CRYPTO_EccPublicKey *pseudonym,
692 struct GNUNET_CONTAINER_MetaData *meta;
698 ret = read_info (cfg, pseudonym, &meta, &rank, &name);
699 if (ret == GNUNET_SYSERR)
702 meta = GNUNET_CONTAINER_meta_data_create ();
705 GNUNET_FS_pseudonym_set_info (cfg, pseudonym, name, meta, rank);
706 GNUNET_CONTAINER_meta_data_destroy (meta);
707 GNUNET_free_non_null (name);
713 * Add a pseudonym to the set of known pseudonyms.
714 * For all pseudonym advertisements that we discover
715 * FS should automatically call this function.
717 * @param cfg overall configuration
718 * @param pseudonym the pseudonym to add
719 * @param meta metadata for the pseudonym
720 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
723 GNUNET_FS_pseudonym_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
724 const struct GNUNET_CRYPTO_EccPublicKey *pseudonym,
725 const struct GNUNET_CONTAINER_MetaData *meta)
729 struct GNUNET_CONTAINER_MetaData *old;
735 fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
736 GNUNET_assert (fn != NULL);
738 if ((0 == STAT (fn, &sbuf)) &&
739 (GNUNET_OK == read_info (cfg, pseudonym, &old, &rank, &name)))
741 GNUNET_CONTAINER_meta_data_merge (old, meta);
742 ret = GNUNET_FS_pseudonym_set_info (cfg, pseudonym, name, old, rank);
743 GNUNET_CONTAINER_meta_data_destroy (old);
744 GNUNET_free_non_null (name);
748 ret = GNUNET_FS_pseudonym_set_info (cfg, pseudonym, NULL, meta, rank);
751 internal_notify (pseudonym, meta, rank);
756 /* end of fs_pseudonym.c */