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;
94 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
99 uint32_t record_count;
109 struct GNUNET_GNSRECORD_Data *record_data;
121 * Initialize the database connections and associated
122 * data structures (create tables and indices
123 * as needed as well).
125 * @param plugin the plugin context (state for this module)
126 * @return #GNUNET_OK on success
129 database_setup (struct Plugin *plugin)
134 char *zone_private_key;
135 char *record_data_b64;
141 size_t record_data_size;
144 struct GNUNET_HashCode hkey;
145 struct GNUNET_DISK_FileHandle *fh;
146 struct FlatFileEntry *entry;
149 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
154 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
157 return GNUNET_SYSERR;
160 GNUNET_DISK_file_test (afsdir))
163 GNUNET_DISK_directory_create_for_file (afsdir))
166 GNUNET_free (afsdir);
167 return GNUNET_SYSERR;
170 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
173 /* Load data from file into hashmap */
174 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
176 fh = GNUNET_DISK_file_open (afsdir,
177 GNUNET_DISK_OPEN_CREATE |
178 GNUNET_DISK_OPEN_READWRITE,
179 GNUNET_DISK_PERM_USER_WRITE |
180 GNUNET_DISK_PERM_USER_READ);
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184 _("Unable to initialize file: %s.\n"),
186 return GNUNET_SYSERR;
189 GNUNET_DISK_file_size (afsdir,
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195 _("Unable to get filesize: %s.\n"),
197 GNUNET_DISK_file_close (fh);
198 return GNUNET_SYSERR;
201 buffer = GNUNET_malloc (size + 1);
203 GNUNET_DISK_file_read (fh,
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 _("Unable to read file: %s.\n"),
210 GNUNET_free (buffer);
211 GNUNET_DISK_file_close (fh);
212 return GNUNET_SYSERR;
215 GNUNET_DISK_file_close (fh);
219 line = strtok (buffer, "\n");
220 while (line != NULL) {
221 zone_private_key = strtok (line, ",");
222 if (NULL == zone_private_key)
224 rvalue = strtok (NULL, ",");
227 record_count = strtok (NULL, ",");
228 if (NULL == record_count)
230 record_data_b64 = strtok (NULL, ",");
231 if (NULL == record_data_b64)
233 label = strtok (NULL, ",");
236 line = strtok (NULL, "\n");
237 entry = GNUNET_new (struct FlatFileEntry);
238 if (1 != sscanf (rvalue,
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 "Error parsing entry\n");
247 if (1 != sscanf (record_count,
249 &entry->record_count))
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252 "Error parsing entry\n");
256 entry->label = GNUNET_strdup (label);
258 = GNUNET_STRINGS_base64_decode (record_data_b64,
259 strlen (record_data_b64),
262 GNUNET_new_array (entry->record_count,
263 struct GNUNET_GNSRECORD_Data);
265 GNUNET_GNSRECORD_records_deserialize (record_data_size,
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "Unable to deserialize record %s\n",
273 GNUNET_free (entry->label);
275 GNUNET_free (record_data);
278 GNUNET_free (record_data);
279 GNUNET_STRINGS_base64_decode (zone_private_key,
280 strlen (zone_private_key),
281 (char**)&entry->private_key);
282 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
283 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
287 GNUNET_memcpy (key+strlen(label),
289 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
290 GNUNET_CRYPTO_hash (key,
295 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
298 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
305 GNUNET_free (buffer);
311 * Store values in hashmap in file and free data
313 * @param plugin the plugin context
314 * @param key key in the map
315 * @param value a `struct FlatFileEntry`
318 store_and_free_entries (void *cls,
319 const struct GNUNET_HashCode *key,
322 struct GNUNET_DISK_FileHandle *fh = cls;
323 struct FlatFileEntry *entry = value;
325 char *zone_private_key;
326 char *record_data_b64;
330 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
331 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
333 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
336 char data[data_size];
339 ret = GNUNET_GNSRECORD_records_serialize (entry->record_count,
344 (data_size != (size_t) ret) )
347 GNUNET_free (zone_private_key);
348 return GNUNET_SYSERR;
350 GNUNET_STRINGS_base64_encode (data,
354 GNUNET_asprintf (&line,
361 GNUNET_free (record_data_b64);
362 GNUNET_free (zone_private_key);
364 GNUNET_DISK_file_write (fh,
369 GNUNET_free (entry->private_key);
370 GNUNET_free (entry->label);
371 GNUNET_free (entry->record_data);
378 * Shutdown database connection and associate data
380 * @param plugin the plugin context (state for this module)
383 database_shutdown (struct Plugin *plugin)
385 struct GNUNET_DISK_FileHandle *fh;
387 fh = GNUNET_DISK_file_open (plugin->fn,
388 GNUNET_DISK_OPEN_CREATE |
389 GNUNET_DISK_OPEN_TRUNCATE |
390 GNUNET_DISK_OPEN_READWRITE,
391 GNUNET_DISK_PERM_USER_WRITE |
392 GNUNET_DISK_PERM_USER_READ);
395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
396 _("Unable to initialize file: %s.\n"),
401 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
402 &store_and_free_entries,
404 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
405 GNUNET_DISK_file_close (fh);
410 * Store a record in the datastore. Removes any existing record in the
411 * same zone with the same name.
413 * @param cls closure (internal context for the plugin)
414 * @param zone_key private key of the zone
415 * @param label name that is being mapped (at most 255 characters long)
416 * @param rd_count number of entries in @a rd array
417 * @param rd array of records with data to store
418 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
421 namestore_store_records (void *cls,
422 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
424 unsigned int rd_count,
425 const struct GNUNET_GNSRECORD_Data *rd)
427 struct Plugin *plugin = cls;
431 struct GNUNET_HashCode hkey;
432 struct FlatFileEntry *entry;
434 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
436 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
437 key = GNUNET_malloc (key_len);
438 GNUNET_memcpy (key, label, strlen (label));
439 GNUNET_memcpy (key+strlen(label),
441 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
442 GNUNET_CRYPTO_hash (key,
446 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
450 entry = GNUNET_new (struct FlatFileEntry);
451 entry->private_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
452 GNUNET_asprintf (&entry->label,
455 GNUNET_memcpy (entry->private_key,
457 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
458 entry->rvalue = rvalue;
459 entry->record_count = rd_count;
460 entry->record_data = GNUNET_new_array (rd_count,
461 struct GNUNET_GNSRECORD_Data);
462 for (unsigned int i = 0; i < rd_count; i++)
464 entry->record_data[i].expiration_time = rd[i].expiration_time;
465 entry->record_data[i].record_type = rd[i].record_type;
466 entry->record_data[i].flags = rd[i].flags;
467 entry->record_data[i].data_size = rd[i].data_size;
468 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
469 GNUNET_memcpy ((char*)entry->record_data[i].data, rd[i].data, rd[i].data_size);
471 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
481 * Lookup records in the datastore for which we are the authority.
483 * @param cls closure (internal context for the plugin)
484 * @param zone private key of the zone
485 * @param label name of the record in the zone
486 * @param iter function to call with the result
487 * @param iter_cls closure for @a iter
488 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
491 namestore_lookup_records (void *cls,
492 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
494 GNUNET_NAMESTORE_RecordIterator iter,
497 struct Plugin *plugin = cls;
498 struct FlatFileEntry *entry;
499 struct GNUNET_HashCode hkey;
505 return GNUNET_SYSERR;
507 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
508 key = GNUNET_malloc (key_len);
512 GNUNET_memcpy (key+strlen(label),
514 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
515 GNUNET_CRYPTO_hash (key,
520 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm,
536 iterate_zones (void *cls,
537 const struct GNUNET_HashCode *key,
540 struct Plugin *plugin = cls;
541 struct FlatFileEntry *entry = value;
544 if ((plugin->target_offset > plugin->offset) ||
545 ( (NULL != plugin->iter_zone) &&
546 (0 != memcmp (entry->private_key,
548 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))))) {
552 plugin->iter (plugin->iter_cls,
557 plugin->iter_result_found = GNUNET_YES;
563 * Iterate over the results for a particular key and zone in the
564 * datastore. Will return at most one result to the iterator.
566 * @param cls closure (internal context for the plugin)
567 * @param zone hash of public key of the zone, NULL to iterate over all zones
568 * @param offset offset in the list of all matching records
569 * @param iter function to call with the result
570 * @param iter_cls closure for @a iter
571 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
574 namestore_iterate_records (void *cls,
575 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
577 GNUNET_NAMESTORE_RecordIterator iter,
580 struct Plugin *plugin = cls;
582 /* FIXME: maybe use separate closure to better handle
584 plugin->target_offset = offset;
587 plugin->iter_cls = iter_cls;
588 plugin->iter_zone = zone;
589 plugin->iter_result_found = GNUNET_NO;
590 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
593 return plugin->iter_result_found;
598 zone_to_name (void *cls,
599 const struct GNUNET_HashCode *key,
602 struct Plugin *plugin = cls;
603 struct FlatFileEntry *entry = value;
606 if (0 != memcmp (entry->private_key,
608 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
611 for (unsigned int i = 0; i < entry->record_count; i++)
613 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
615 if (0 == memcmp (plugin->iter_pkey,
616 entry->record_data[i].data,
617 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
619 plugin->iter (plugin->iter_cls,
624 plugin->iter_result_found = GNUNET_YES;
633 * Look for an existing PKEY delegation record for a given public key.
634 * Returns at most one result to the iterator.
636 * @param cls closure (internal context for the plugin)
637 * @param zone private key of the zone to look up in, never NULL
638 * @param value_zone public key of the target zone (value), never NULL
639 * @param iter function to call with the result
640 * @param iter_cls closure for @a iter
641 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
644 namestore_zone_to_name (void *cls,
645 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
646 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
647 GNUNET_NAMESTORE_RecordIterator iter,
650 struct Plugin *plugin = cls;
652 /* FIXME: maybe use separate closure to better handle
655 plugin->iter_cls = iter_cls;
656 plugin->iter_zone = zone;
657 plugin->iter_pkey = value_zone;
658 plugin->iter_result_found = GNUNET_NO;
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Performing reverse lookup for `%s'\n",
661 GNUNET_GNSRECORD_z2s (value_zone));
662 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
665 return plugin->iter_result_found;
670 * Entry point for the plugin.
672 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
673 * @return NULL on error, otherwise the plugin context
676 libgnunet_plugin_namestore_flat_init (void *cls)
678 static struct Plugin plugin;
679 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
680 struct GNUNET_NAMESTORE_PluginFunctions *api;
682 if (NULL != plugin.cfg)
683 return NULL; /* can only initialize once! */
686 sizeof (struct Plugin));
688 if (GNUNET_OK != database_setup (&plugin))
690 database_shutdown (&plugin);
693 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
695 api->store_records = &namestore_store_records;
696 api->iterate_records = &namestore_iterate_records;
697 api->zone_to_name = &namestore_zone_to_name;
698 api->lookup_records = &namestore_lookup_records;
699 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
700 _("flat file database running\n"));
706 * Exit point from the plugin.
708 * @param cls the plugin context (as returned by "init")
709 * @return always NULL
712 libgnunet_plugin_namestore_flat_done (void *cls)
714 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
715 struct Plugin *plugin = api->cls;
717 database_shutdown (plugin);
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "flat file plugin is finished\n");
725 /* end of plugin_namestore_flat.c */