2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015, 2018 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
25 * @author Christian Grothoff
29 #include "gnunet_namestore_plugin.h"
30 #include "gnunet_namestore_service.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "namestore.h"
35 * Context for all functions in this plugin.
40 const struct GNUNET_CONFIGURATION_Handle *cfg;
50 struct GNUNET_CONTAINER_MultiHashMap *hm;
60 uint32_t target_offset;
70 GNUNET_NAMESTORE_RecordIterator iter;
75 const struct GNUNET_CRYPTO_EcdsaPrivateKey *iter_zone;
78 * PKEY to look for in zone to name
80 const struct GNUNET_CRYPTO_EcdsaPublicKey *iter_pkey;
83 * Iteration result found
85 int iter_result_found;
95 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
100 uint32_t record_count;
110 struct GNUNET_GNSRECORD_Data *record_data;
122 * Initialize the database connections and associated
123 * data structures (create tables and indices
124 * as needed as well).
126 * @param plugin the plugin context (state for this module)
127 * @return #GNUNET_OK on success
130 database_setup (struct Plugin *plugin)
135 char *zone_private_key;
136 char *record_data_b64;
142 size_t record_data_size;
145 struct GNUNET_HashCode hkey;
146 struct GNUNET_DISK_FileHandle *fh;
147 struct FlatFileEntry *entry;
150 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
155 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
158 return GNUNET_SYSERR;
161 GNUNET_DISK_file_test (afsdir))
164 GNUNET_DISK_directory_create_for_file (afsdir))
167 GNUNET_free (afsdir);
168 return GNUNET_SYSERR;
171 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
174 /* Load data from file into hashmap */
175 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
177 fh = GNUNET_DISK_file_open (afsdir,
178 GNUNET_DISK_OPEN_CREATE |
179 GNUNET_DISK_OPEN_READWRITE,
180 GNUNET_DISK_PERM_USER_WRITE |
181 GNUNET_DISK_PERM_USER_READ);
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 _("Unable to initialize file: %s.\n"),
187 return GNUNET_SYSERR;
190 GNUNET_DISK_file_size (afsdir,
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196 _("Unable to get filesize: %s.\n"),
198 GNUNET_DISK_file_close (fh);
199 return GNUNET_SYSERR;
202 buffer = GNUNET_malloc (size + 1);
204 GNUNET_DISK_file_read (fh,
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209 _("Unable to read file: %s.\n"),
211 GNUNET_free (buffer);
212 GNUNET_DISK_file_close (fh);
213 return GNUNET_SYSERR;
216 GNUNET_DISK_file_close (fh);
220 line = strtok (buffer, "\n");
221 while (line != NULL) {
222 zone_private_key = strtok (line, ",");
223 if (NULL == zone_private_key)
225 rvalue = strtok (NULL, ",");
228 record_count = strtok (NULL, ",");
229 if (NULL == record_count)
231 record_data_b64 = strtok (NULL, ",");
232 if (NULL == record_data_b64)
234 label = strtok (NULL, ",");
237 line = strtok (NULL, "\n");
238 entry = GNUNET_new (struct FlatFileEntry);
239 if (1 != sscanf (rvalue,
243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244 "Error parsing entry\n");
248 if (1 != sscanf (record_count,
250 &entry->record_count))
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 "Error parsing entry\n");
257 entry->label = GNUNET_strdup (label);
259 = GNUNET_STRINGS_base64_decode (record_data_b64,
260 strlen (record_data_b64),
263 GNUNET_new_array (entry->record_count,
264 struct GNUNET_GNSRECORD_Data);
266 GNUNET_GNSRECORD_records_deserialize (record_data_size,
271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
272 "Unable to deserialize record %s\n",
274 GNUNET_free (entry->label);
276 GNUNET_free (record_data);
279 GNUNET_free (record_data);
280 GNUNET_STRINGS_base64_decode (zone_private_key,
281 strlen (zone_private_key),
282 (char**)&entry->private_key);
283 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
284 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
288 GNUNET_memcpy (key+strlen(label),
290 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
291 GNUNET_CRYPTO_hash (key,
296 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
299 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
306 GNUNET_free (buffer);
312 * Store values in hashmap in file and free data
314 * @param plugin the plugin context
315 * @param key key in the map
316 * @param value a `struct FlatFileEntry`
319 store_and_free_entries (void *cls,
320 const struct GNUNET_HashCode *key,
323 struct GNUNET_DISK_FileHandle *fh = cls;
324 struct FlatFileEntry *entry = value;
326 char *zone_private_key;
327 char *record_data_b64;
331 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
332 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
334 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
337 char data[data_size];
340 ret = GNUNET_GNSRECORD_records_serialize (entry->record_count,
345 (data_size != (size_t) ret) )
348 GNUNET_free (zone_private_key);
349 return GNUNET_SYSERR;
351 GNUNET_STRINGS_base64_encode (data,
355 GNUNET_asprintf (&line,
362 GNUNET_free (record_data_b64);
363 GNUNET_free (zone_private_key);
365 GNUNET_DISK_file_write (fh,
370 GNUNET_free (entry->private_key);
371 GNUNET_free (entry->label);
372 GNUNET_free (entry->record_data);
379 * Shutdown database connection and associate data
381 * @param plugin the plugin context (state for this module)
384 database_shutdown (struct Plugin *plugin)
386 struct GNUNET_DISK_FileHandle *fh;
388 fh = GNUNET_DISK_file_open (plugin->fn,
389 GNUNET_DISK_OPEN_CREATE |
390 GNUNET_DISK_OPEN_TRUNCATE |
391 GNUNET_DISK_OPEN_READWRITE,
392 GNUNET_DISK_PERM_USER_WRITE |
393 GNUNET_DISK_PERM_USER_READ);
396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
397 _("Unable to initialize file: %s.\n"),
402 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
403 &store_and_free_entries,
405 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
406 GNUNET_DISK_file_close (fh);
411 * Store a record in the datastore. Removes any existing record in the
412 * same zone with the same name.
414 * @param cls closure (internal context for the plugin)
415 * @param zone_key private key of the zone
416 * @param label name that is being mapped (at most 255 characters long)
417 * @param rd_count number of entries in @a rd array
418 * @param rd array of records with data to store
419 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
422 namestore_flat_store_records (void *cls,
423 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
425 unsigned int rd_count,
426 const struct GNUNET_GNSRECORD_Data *rd)
428 struct Plugin *plugin = cls;
432 struct GNUNET_HashCode hkey;
433 struct FlatFileEntry *entry;
435 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
437 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
438 key = GNUNET_malloc (key_len);
442 GNUNET_memcpy (key + strlen(label),
444 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
445 GNUNET_CRYPTO_hash (key,
448 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm,
452 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
457 entry = GNUNET_new (struct FlatFileEntry);
458 entry->private_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
459 GNUNET_asprintf (&entry->label,
462 GNUNET_memcpy (entry->private_key,
464 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
465 entry->rvalue = rvalue;
466 entry->record_count = rd_count;
467 entry->record_data = GNUNET_new_array (rd_count,
468 struct GNUNET_GNSRECORD_Data);
469 for (unsigned int i = 0; i < rd_count; i++)
471 entry->record_data[i].expiration_time = rd[i].expiration_time;
472 entry->record_data[i].record_type = rd[i].record_type;
473 entry->record_data[i].flags = rd[i].flags;
474 entry->record_data[i].data_size = rd[i].data_size;
475 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
476 GNUNET_memcpy ((char*)entry->record_data[i].data,
480 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
483 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
488 * Lookup records in the datastore for which we are the authority.
490 * @param cls closure (internal context for the plugin)
491 * @param zone private key of the zone
492 * @param label name of the record in the zone
493 * @param iter function to call with the result
494 * @param iter_cls closure for @a iter
495 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
498 namestore_flat_lookup_records (void *cls,
499 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
501 GNUNET_NAMESTORE_RecordIterator iter,
504 struct Plugin *plugin = cls;
505 struct FlatFileEntry *entry;
506 struct GNUNET_HashCode hkey;
513 return GNUNET_SYSERR;
515 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
516 key = GNUNET_malloc (key_len);
520 GNUNET_memcpy (key+strlen(label),
522 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
523 GNUNET_CRYPTO_hash (key,
528 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm,
545 * Closure for #iterate_zones.
547 struct IterateContext
550 * How many more records should we skip before returning results?
555 * How many more records should we return?
560 * What is the position of the current entry, counting
568 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone;
571 * Function to call on each record.
573 GNUNET_NAMESTORE_RecordIterator iter;
576 * Closure for @e iter.
584 * Helper function for #namestore_flat_iterate_records().
586 * @param cls a `struct IterateContext`
588 * @param value a `struct FlatFileEntry`
589 * @return #GNUNET_YES to continue the iteration
592 iterate_zones (void *cls,
593 const struct GNUNET_HashCode *key,
596 struct IterateContext *ic = cls;
597 struct FlatFileEntry *entry = value;
603 if ( (NULL != ic->zone) &&
604 (0 != memcmp (entry->private_key,
606 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
613 ic->iter (ic->iter_cls,
627 * Iterate over the results for a particular key and zone in the
628 * datastore. Will return at most one result to the iterator.
630 * @param cls closure (internal context for the plugin)
631 * @param zone hash of public key of the zone, NULL to iterate over all zones
632 * @param serial serial number to exclude in the list of all matching records
633 * @param limit maximum number of results to return to @a iter
634 * @param iter function to call with the result
635 * @param iter_cls closure for @a iter
636 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
639 namestore_flat_iterate_records (void *cls,
640 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
643 GNUNET_NAMESTORE_RecordIterator iter,
646 struct Plugin *plugin = cls;
647 struct IterateContext ic;
653 ic.iter_cls = iter_cls;
655 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
658 return (0 == ic.limit) ? GNUNET_OK : GNUNET_NO;
663 zone_to_name (void *cls,
664 const struct GNUNET_HashCode *key,
667 struct Plugin *plugin = cls;
668 struct FlatFileEntry *entry = value;
671 if (0 != memcmp (entry->private_key,
673 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
676 for (unsigned int i = 0; i < entry->record_count; i++)
678 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
680 if (0 == memcmp (plugin->iter_pkey,
681 entry->record_data[i].data,
682 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
684 plugin->iter (plugin->iter_cls,
690 plugin->iter_result_found = GNUNET_YES;
699 * Look for an existing PKEY delegation record for a given public key.
700 * Returns at most one result to the iterator.
702 * @param cls closure (internal context for the plugin)
703 * @param zone private key of the zone to look up in, never NULL
704 * @param value_zone public key of the target zone (value), never NULL
705 * @param iter function to call with the result
706 * @param iter_cls closure for @a iter
707 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
710 namestore_flat_zone_to_name (void *cls,
711 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
712 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
713 GNUNET_NAMESTORE_RecordIterator iter,
716 struct Plugin *plugin = cls;
718 /* FIXME: maybe use separate closure to better handle
721 plugin->iter_cls = iter_cls;
722 plugin->iter_zone = zone;
723 plugin->iter_pkey = value_zone;
724 plugin->iter_result_found = GNUNET_NO;
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Performing reverse lookup for `%s'\n",
727 GNUNET_GNSRECORD_z2s (value_zone));
728 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
731 return plugin->iter_result_found;
736 * Entry point for the plugin.
738 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
739 * @return NULL on error, otherwise the plugin context
742 libgnunet_plugin_namestore_flat_init (void *cls)
744 static struct Plugin plugin;
745 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
746 struct GNUNET_NAMESTORE_PluginFunctions *api;
748 if (NULL != plugin.cfg)
749 return NULL; /* can only initialize once! */
752 sizeof (struct Plugin));
754 if (GNUNET_OK != database_setup (&plugin))
756 database_shutdown (&plugin);
759 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
761 api->store_records = &namestore_flat_store_records;
762 api->iterate_records = &namestore_flat_iterate_records;
763 api->zone_to_name = &namestore_flat_zone_to_name;
764 api->lookup_records = &namestore_flat_lookup_records;
765 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
766 _("flat file database running\n"));
772 * Exit point from the plugin.
774 * @param cls the plugin context (as returned by "init")
775 * @return always NULL
778 libgnunet_plugin_namestore_flat_done (void *cls)
780 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
781 struct Plugin *plugin = api->cls;
783 database_shutdown (plugin);
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "flat file plugin is finished\n");
791 /* end of plugin_namestore_flat.c */