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 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
65 uint32_t record_count;
75 struct GNUNET_GNSRECORD_Data *record_data;
87 * Initialize the database connections and associated
88 * data structures (create tables and indices
91 * @param plugin the plugin context (state for this module)
92 * @return #GNUNET_OK on success
95 database_setup (struct Plugin *plugin)
100 char *zone_private_key;
101 char *record_data_b64;
107 size_t record_data_size;
110 struct GNUNET_HashCode hkey;
111 struct GNUNET_DISK_FileHandle *fh;
112 struct FlatFileEntry *entry;
115 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
120 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
123 return GNUNET_SYSERR;
126 GNUNET_DISK_file_test (afsdir))
129 GNUNET_DISK_directory_create_for_file (afsdir))
132 GNUNET_free (afsdir);
133 return GNUNET_SYSERR;
136 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
139 /* Load data from file into hashmap */
140 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
142 fh = GNUNET_DISK_file_open (afsdir,
143 GNUNET_DISK_OPEN_CREATE |
144 GNUNET_DISK_OPEN_READWRITE,
145 GNUNET_DISK_PERM_USER_WRITE |
146 GNUNET_DISK_PERM_USER_READ);
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 _("Unable to initialize file: %s.\n"),
152 return GNUNET_SYSERR;
155 GNUNET_DISK_file_size (afsdir,
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161 _("Unable to get filesize: %s.\n"),
163 GNUNET_DISK_file_close (fh);
164 return GNUNET_SYSERR;
167 buffer = GNUNET_malloc (size + 1);
169 GNUNET_DISK_file_read (fh,
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 _("Unable to read file: %s.\n"),
176 GNUNET_free (buffer);
177 GNUNET_DISK_file_close (fh);
178 return GNUNET_SYSERR;
181 GNUNET_DISK_file_close (fh);
185 line = strtok (buffer, "\n");
188 zone_private_key = strtok (line, ",");
189 if (NULL == zone_private_key)
191 rvalue = strtok (NULL, ",");
194 record_count = strtok (NULL, ",");
195 if (NULL == record_count)
197 record_data_b64 = strtok (NULL, ",");
198 if (NULL == record_data_b64)
200 label = strtok (NULL, ",");
203 line = strtok (NULL, "\n");
204 entry = GNUNET_new (struct FlatFileEntry);
206 unsigned long long ll;
208 if (1 != sscanf (rvalue,
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213 "Error parsing entry\n");
217 entry->rvalue = (uint64_t) ll;
222 if (1 != sscanf (record_count,
226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227 "Error parsing entry\n");
231 entry->record_count = (uint32_t) ui;
233 entry->label = GNUNET_strdup (label);
235 = GNUNET_STRINGS_base64_decode (record_data_b64,
236 strlen (record_data_b64),
239 GNUNET_new_array (entry->record_count,
240 struct GNUNET_GNSRECORD_Data);
242 GNUNET_GNSRECORD_records_deserialize (record_data_size,
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248 "Unable to deserialize record %s\n",
250 GNUNET_free (entry->label);
252 GNUNET_free (record_data);
255 GNUNET_free (record_data);
256 GNUNET_STRINGS_base64_decode (zone_private_key,
257 strlen (zone_private_key),
258 (char**)&entry->private_key);
259 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
260 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
264 GNUNET_memcpy (key+strlen(label),
266 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
267 GNUNET_CRYPTO_hash (key,
272 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
275 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
282 GNUNET_free (buffer);
288 * Store values in hashmap in file and free data
290 * @param plugin the plugin context
291 * @param key key in the map
292 * @param value a `struct FlatFileEntry`
295 store_and_free_entries (void *cls,
296 const struct GNUNET_HashCode *key,
299 struct GNUNET_DISK_FileHandle *fh = cls;
300 struct FlatFileEntry *entry = value;
302 char *zone_private_key;
303 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 ret = GNUNET_GNSRECORD_records_serialize (entry->record_count,
321 (data_size != (size_t) ret) )
324 GNUNET_free (zone_private_key);
325 return GNUNET_SYSERR;
327 GNUNET_STRINGS_base64_encode (data,
331 GNUNET_asprintf (&line,
332 "%s,%llu,%u,%s,%s\n",
334 (unsigned long long) entry->rvalue,
335 (unsigned int) entry->record_count,
338 GNUNET_free (record_data_b64);
339 GNUNET_free (zone_private_key);
341 GNUNET_DISK_file_write (fh,
346 GNUNET_free (entry->private_key);
347 GNUNET_free (entry->label);
348 GNUNET_free (entry->record_data);
355 * Shutdown database connection and associate data
357 * @param plugin the plugin context (state for this module)
360 database_shutdown (struct Plugin *plugin)
362 struct GNUNET_DISK_FileHandle *fh;
364 fh = GNUNET_DISK_file_open (plugin->fn,
365 GNUNET_DISK_OPEN_CREATE |
366 GNUNET_DISK_OPEN_TRUNCATE |
367 GNUNET_DISK_OPEN_READWRITE,
368 GNUNET_DISK_PERM_USER_WRITE |
369 GNUNET_DISK_PERM_USER_READ);
372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
373 _("Unable to initialize file: %s.\n"),
378 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
379 &store_and_free_entries,
381 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
382 GNUNET_DISK_file_close (fh);
387 * Store a record in the datastore. Removes any existing record in the
388 * same zone with the same name.
390 * @param cls closure (internal context for the plugin)
391 * @param zone_key private key of the zone
392 * @param label name that is being mapped (at most 255 characters long)
393 * @param rd_count number of entries in @a rd array
394 * @param rd array of records with data to store
395 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
398 namestore_flat_store_records (void *cls,
399 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
401 unsigned int rd_count,
402 const struct GNUNET_GNSRECORD_Data *rd)
404 struct Plugin *plugin = cls;
408 struct GNUNET_HashCode hkey;
409 struct FlatFileEntry *entry;
411 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
413 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
414 key = GNUNET_malloc (key_len);
418 GNUNET_memcpy (key + strlen(label),
420 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
421 GNUNET_CRYPTO_hash (key,
424 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm,
428 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
433 entry = GNUNET_new (struct FlatFileEntry);
434 entry->private_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
435 GNUNET_asprintf (&entry->label,
438 GNUNET_memcpy (entry->private_key,
440 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
441 entry->rvalue = rvalue;
442 entry->record_count = rd_count;
443 entry->record_data = GNUNET_new_array (rd_count,
444 struct GNUNET_GNSRECORD_Data);
445 for (unsigned int i = 0; i < rd_count; i++)
447 entry->record_data[i].expiration_time = rd[i].expiration_time;
448 entry->record_data[i].record_type = rd[i].record_type;
449 entry->record_data[i].flags = rd[i].flags;
450 entry->record_data[i].data_size = rd[i].data_size;
451 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
452 GNUNET_memcpy ((char*)entry->record_data[i].data,
456 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
459 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
464 * Lookup records in the datastore for which we are the authority.
466 * @param cls closure (internal context for the plugin)
467 * @param zone private key of the zone
468 * @param label name of the record in the zone
469 * @param iter function to call with the result
470 * @param iter_cls closure for @a iter
471 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
474 namestore_flat_lookup_records (void *cls,
475 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
477 GNUNET_NAMESTORE_RecordIterator iter,
480 struct Plugin *plugin = cls;
481 struct FlatFileEntry *entry;
482 struct GNUNET_HashCode hkey;
489 return GNUNET_SYSERR;
491 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
492 key = GNUNET_malloc (key_len);
496 GNUNET_memcpy (key+strlen(label),
498 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
499 GNUNET_CRYPTO_hash (key,
504 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm,
521 * Closure for #iterate_zones.
523 struct IterateContext
526 * How many more records should we skip before returning results?
531 * How many more records should we return?
536 * What is the position of the current entry, counting
544 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone;
547 * Function to call on each record.
549 GNUNET_NAMESTORE_RecordIterator iter;
552 * Closure for @e iter.
560 * Helper function for #namestore_flat_iterate_records().
562 * @param cls a `struct IterateContext`
564 * @param value a `struct FlatFileEntry`
565 * @return #GNUNET_YES to continue the iteration
568 iterate_zones (void *cls,
569 const struct GNUNET_HashCode *key,
572 struct IterateContext *ic = cls;
573 struct FlatFileEntry *entry = value;
579 if ( (NULL != ic->zone) &&
580 (0 != memcmp (entry->private_key,
582 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
589 ic->iter (ic->iter_cls,
603 * Iterate over the results for a particular key and zone in the
604 * datastore. Will return at most one result to the iterator.
606 * @param cls closure (internal context for the plugin)
607 * @param zone hash of public key of the zone, NULL to iterate over all zones
608 * @param serial serial number to exclude in the list of all matching records
609 * @param limit maximum number of results to return to @a iter
610 * @param iter function to call with the result
611 * @param iter_cls closure for @a iter
612 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
615 namestore_flat_iterate_records (void *cls,
616 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
619 GNUNET_NAMESTORE_RecordIterator iter,
622 struct Plugin *plugin = cls;
623 struct IterateContext ic;
629 ic.iter_cls = iter_cls;
631 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
634 return (0 == ic.limit) ? GNUNET_OK : GNUNET_NO;
639 * Closure for #zone_to_name.
641 struct ZoneToNameContext
643 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone;
644 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone;
645 GNUNET_NAMESTORE_RecordIterator iter;
653 zone_to_name (void *cls,
654 const struct GNUNET_HashCode *key,
657 struct ZoneToNameContext *ztn = cls;
658 struct FlatFileEntry *entry = value;
661 if (0 != memcmp (entry->private_key,
663 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
666 for (unsigned int i = 0; i < entry->record_count; i++)
668 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
670 if (0 == memcmp (ztn->value_zone,
671 entry->record_data[i].data,
672 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
674 ztn->iter (ztn->iter_cls,
680 ztn->result_found = GNUNET_YES;
688 * Look for an existing PKEY delegation record for a given public key.
689 * Returns at most one result to the iterator.
691 * @param cls closure (internal context for the plugin)
692 * @param zone private key of the zone to look up in, never NULL
693 * @param value_zone public key of the target zone (value), never NULL
694 * @param iter function to call with the result
695 * @param iter_cls closure for @a iter
696 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
699 namestore_flat_zone_to_name (void *cls,
700 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
701 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
702 GNUNET_NAMESTORE_RecordIterator iter,
705 struct Plugin *plugin = cls;
706 struct ZoneToNameContext ztn = {
708 .iter_cls = iter_cls,
710 .value_zone = value_zone,
711 .result_found = GNUNET_NO
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 "Performing reverse lookup for `%s'\n",
716 GNUNET_GNSRECORD_z2s (value_zone));
717 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
720 return ztn.result_found;
725 * Entry point for the plugin.
727 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
728 * @return NULL on error, otherwise the plugin context
731 libgnunet_plugin_namestore_flat_init (void *cls)
733 static struct Plugin plugin;
734 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
735 struct GNUNET_NAMESTORE_PluginFunctions *api;
737 if (NULL != plugin.cfg)
738 return NULL; /* can only initialize once! */
741 sizeof (struct Plugin));
743 if (GNUNET_OK != database_setup (&plugin))
745 database_shutdown (&plugin);
748 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
750 api->store_records = &namestore_flat_store_records;
751 api->iterate_records = &namestore_flat_iterate_records;
752 api->zone_to_name = &namestore_flat_zone_to_name;
753 api->lookup_records = &namestore_flat_lookup_records;
754 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
755 _("flat file database running\n"));
761 * Exit point from the plugin.
763 * @param cls the plugin context (as returned by "init")
764 * @return always NULL
767 libgnunet_plugin_namestore_flat_done (void *cls)
769 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
770 struct Plugin *plugin = api->cls;
772 database_shutdown (plugin);
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "flat file plugin is finished\n");
780 /* end of plugin_namestore_flat.c */