2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file namestore/plugin_namestore_flat.c
23 * @brief file-based namestore backend
24 * @author Martin Schanzenbach
28 #include "gnunet_namestore_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "namestore.h"
34 * Context for all functions in this plugin.
39 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 struct GNUNET_CONTAINER_MultiHashMap *hm;
59 uint32_t target_offset;
69 GNUNET_NAMESTORE_RecordIterator iter;
74 const struct GNUNET_CRYPTO_EcdsaPrivateKey *iter_zone;
77 * PKEY to look for in zone to name
79 struct GNUNET_CRYPTO_EcdsaPublicKey *iter_pkey;
82 * Iteration result found
84 int iter_result_found;
93 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
98 uint32_t record_count;
108 struct GNUNET_GNSRECORD_Data *record_data;
120 * Initialize the database connections and associated
121 * data structures (create tables and indices
122 * as needed as well).
124 * @param plugin the plugin context (state for this module)
125 * @return #GNUNET_OK on success
128 database_setup (struct Plugin *plugin)
133 char *zone_private_key;
134 char *record_data_b64;
140 size_t record_data_size;
143 struct GNUNET_HashCode hkey;
144 struct GNUNET_DISK_FileHandle *fh;
145 struct FlatFileEntry *entry;
148 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
150 "FILENAME", &afsdir))
152 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
153 "namestore-flat", "FILENAME");
154 return GNUNET_SYSERR;
156 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
158 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
161 GNUNET_free (afsdir);
162 return GNUNET_SYSERR;
165 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
168 /* Load data from file into hashmap */
169 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
171 fh = GNUNET_DISK_file_open (afsdir,
172 GNUNET_DISK_OPEN_CREATE |
173 GNUNET_DISK_OPEN_READWRITE,
174 GNUNET_DISK_PERM_USER_WRITE |
175 GNUNET_DISK_PERM_USER_READ);
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 _("Unable to initialize file: %s.\n"),
181 return GNUNET_SYSERR;
184 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 _("Unable to get filesize: %s.\n"),
192 return GNUNET_SYSERR;
195 buffer = GNUNET_malloc (size);
197 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 _("Unable to read file: %s.\n"),
204 return GNUNET_SYSERR;
207 GNUNET_DISK_file_close (fh);
209 line = strtok (buffer, "\n");
210 while (line != NULL) {
211 zone_private_key = strtok (line, ",");
212 if (NULL == zone_private_key)
214 rvalue = strtok (NULL, ",");
217 record_count = strtok (NULL, ",");
218 if (NULL == record_count)
220 record_data_b64 = strtok (NULL, ",");
221 if (NULL == record_data_b64)
223 label = strtok (NULL, ",");
226 line = strtok (NULL, "\n");
227 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
228 if (1 != sscanf (rvalue, "%lu", &entry->rvalue))
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "Error parsing entry\n");
235 if (1 != sscanf (record_count, "%u", &entry->record_count))
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 "Error parsing entry\n");
242 entry->label = GNUNET_strdup (label);
243 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
244 strlen (record_data_b64),
247 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
248 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (record_data_size,
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
254 "Unable to deserialize record %s\n", label);
255 GNUNET_free (entry->label);
257 GNUNET_free (record_data);
260 GNUNET_free (record_data);
261 GNUNET_STRINGS_base64_decode (zone_private_key,
262 strlen (zone_private_key),
263 (char**)&entry->private_key);
264 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
265 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
266 GNUNET_memcpy (key, label, strlen (label));
267 GNUNET_memcpy (key+strlen(label),
269 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
270 GNUNET_CRYPTO_hash (key,
275 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
278 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
285 GNUNET_free (buffer);
291 * Store values in hashmap in file and free data
293 * @param plugin the plugin context
296 store_and_free_entries (void *cls,
297 const struct GNUNET_HashCode *key,
300 struct GNUNET_DISK_FileHandle *fh = cls;
301 struct FlatFileEntry *entry = value;
303 char *zone_private_key;
304 char *record_data_b64;
307 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
308 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
310 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
313 char data[data_size];
316 GNUNET_GNSRECORD_records_serialize (entry->record_count,
322 GNUNET_free (zone_private_key);
323 return GNUNET_SYSERR;
325 GNUNET_STRINGS_base64_encode (data,
329 GNUNET_asprintf (&line,
336 GNUNET_free (record_data_b64);
337 GNUNET_free (zone_private_key);
339 GNUNET_DISK_file_write (fh,
343 GNUNET_free (entry->private_key);
344 GNUNET_free (entry->label);
345 GNUNET_free (entry->record_data);
351 * Shutdown database connection and associate data
353 * @param plugin the plugin context (state for this module)
356 database_shutdown (struct Plugin *plugin)
358 struct GNUNET_DISK_FileHandle *fh;
359 fh = GNUNET_DISK_file_open (plugin->fn,
360 GNUNET_DISK_OPEN_CREATE |
361 GNUNET_DISK_OPEN_TRUNCATE |
362 GNUNET_DISK_OPEN_READWRITE,
363 GNUNET_DISK_PERM_USER_WRITE |
364 GNUNET_DISK_PERM_USER_READ);
367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
368 _("Unable to initialize file: %s.\n"),
373 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
374 &store_and_free_entries,
376 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
377 GNUNET_DISK_file_close (fh);
382 * Store a record in the datastore. Removes any existing record in the
383 * same zone with the same name.
385 * @param cls closure (internal context for the plugin)
386 * @param zone_key private key of the zone
387 * @param label name that is being mapped (at most 255 characters long)
388 * @param rd_count number of entries in @a rd array
389 * @param rd array of records with data to store
390 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
393 namestore_store_records (void *cls,
394 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
396 unsigned int rd_count,
397 const struct GNUNET_GNSRECORD_Data *rd)
399 struct Plugin *plugin = cls;
403 struct GNUNET_HashCode hkey;
404 struct FlatFileEntry *entry;
407 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
408 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
409 key = GNUNET_malloc (key_len);
410 GNUNET_memcpy (key, label, strlen (label));
411 GNUNET_memcpy (key+strlen(label),
413 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
414 GNUNET_CRYPTO_hash (key,
418 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
422 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
423 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
424 GNUNET_asprintf (&entry->label,
427 GNUNET_memcpy (entry->private_key,
429 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
430 entry->rvalue = rvalue;
431 entry->record_count = rd_count;
432 entry->record_data = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * rd_count);
433 for (i = 0; i < rd_count; i++)
435 entry->record_data[i].expiration_time = rd[i].expiration_time;
436 entry->record_data[i].record_type = rd[i].record_type;
437 entry->record_data[i].flags = rd[i].flags;
438 entry->record_data[i].data_size = rd[i].data_size;
439 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
440 GNUNET_memcpy ((char*)entry->record_data[i].data, rd[i].data, rd[i].data_size);
442 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
445 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
452 * Lookup records in the datastore for which we are the authority.
454 * @param cls closure (internal context for the plugin)
455 * @param zone private key of the zone
456 * @param label name of the record in the zone
457 * @param iter function to call with the result
458 * @param iter_cls closure for @a iter
459 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
462 namestore_lookup_records (void *cls,
463 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
465 GNUNET_NAMESTORE_RecordIterator iter,
468 struct Plugin *plugin = cls;
469 struct FlatFileEntry *entry;
470 struct GNUNET_HashCode hkey;
476 return GNUNET_SYSERR;
478 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
479 key = GNUNET_malloc (key_len);
480 GNUNET_memcpy (key, label, strlen (label));
481 GNUNET_memcpy (key+strlen(label),
483 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
484 GNUNET_CRYPTO_hash (key,
489 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
494 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
500 iterate_zones (void *cls,
501 const struct GNUNET_HashCode *key,
504 struct Plugin *plugin = cls;
505 struct FlatFileEntry *entry = value;
507 if ((plugin->target_offset > plugin->offset) ||
508 ( (NULL != plugin->iter_zone) &&
509 (0 != memcmp (entry->private_key,
511 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))))) {
515 plugin->iter (plugin->iter_cls,
520 plugin->iter_result_found = GNUNET_YES;
525 * Iterate over the results for a particular key and zone in the
526 * datastore. Will return at most one result to the iterator.
528 * @param cls closure (internal context for the plugin)
529 * @param zone hash of public key of the zone, NULL to iterate over all zones
530 * @param offset offset in the list of all matching records
531 * @param iter function to call with the result
532 * @param iter_cls closure for @a iter
533 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
536 namestore_iterate_records (void *cls,
537 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
539 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
541 struct Plugin *plugin = cls;
542 plugin->target_offset = offset;
545 plugin->iter_cls = iter_cls;
546 plugin->iter_zone = zone;
547 plugin->iter_result_found = GNUNET_NO;
548 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
551 return plugin->iter_result_found;
555 zone_to_name (void *cls,
556 const struct GNUNET_HashCode *key,
559 struct Plugin *plugin = cls;
560 struct FlatFileEntry *entry = value;
563 if (0 != memcmp (entry->private_key,
565 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
568 for (i = 0; i < entry->record_count; i++) {
569 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
571 if (0 == memcmp (plugin->iter_pkey,
572 entry->record_data[i].data,
573 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
575 plugin->iter (plugin->iter_cls,
580 plugin->iter_result_found = GNUNET_YES;
589 * Look for an existing PKEY delegation record for a given public key.
590 * Returns at most one result to the iterator.
592 * @param cls closure (internal context for the plugin)
593 * @param zone private key of the zone to look up in, never NULL
594 * @param value_zone public key of the target zone (value), never NULL
595 * @param iter function to call with the result
596 * @param iter_cls closure for @a iter
597 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
600 namestore_zone_to_name (void *cls,
601 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
602 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
603 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
605 struct Plugin *plugin = cls;
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
608 "Performing reverse lookup for `%s'\n",
609 GNUNET_GNSRECORD_z2s (value_zone));
611 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
616 return plugin->iter_result_found;
621 * Entry point for the plugin.
623 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
624 * @return NULL on error, otherwise the plugin context
627 libgnunet_plugin_namestore_flat_init (void *cls)
629 static struct Plugin plugin;
630 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
631 struct GNUNET_NAMESTORE_PluginFunctions *api;
633 if (NULL != plugin.cfg)
634 return NULL; /* can only initialize once! */
635 memset (&plugin, 0, sizeof (struct Plugin));
637 if (GNUNET_OK != database_setup (&plugin))
639 database_shutdown (&plugin);
642 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
644 api->store_records = &namestore_store_records;
645 api->iterate_records = &namestore_iterate_records;
646 api->zone_to_name = &namestore_zone_to_name;
647 api->lookup_records = &namestore_lookup_records;
648 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
649 _("flat file database running\n"));
655 * Exit point from the plugin.
657 * @param cls the plugin context (as returned by "init")
658 * @return always NULL
661 libgnunet_plugin_namestore_flat_done (void *cls)
663 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
664 struct Plugin *plugin = api->cls;
666 database_shutdown (plugin);
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "flat file plugin is finished\n");
674 /* end of plugin_namestore_flat.c */