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 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 + 1);
197 GNUNET_DISK_file_read (fh,
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 _("Unable to read file: %s.\n"),
204 GNUNET_free (buffer);
205 GNUNET_DISK_file_close (fh);
206 return GNUNET_SYSERR;
209 GNUNET_DISK_file_close (fh);
213 line = strtok (buffer, "\n");
214 while (line != NULL) {
215 zone_private_key = strtok (line, ",");
216 if (NULL == zone_private_key)
218 rvalue = strtok (NULL, ",");
221 record_count = strtok (NULL, ",");
222 if (NULL == record_count)
224 record_data_b64 = strtok (NULL, ",");
225 if (NULL == record_data_b64)
227 label = strtok (NULL, ",");
230 line = strtok (NULL, "\n");
231 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
232 if (1 != sscanf (rvalue, "%lu", &entry->rvalue))
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Error parsing entry\n");
239 if (1 != sscanf (record_count, "%u", &entry->record_count))
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 "Error parsing entry\n");
246 entry->label = GNUNET_strdup (label);
247 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
248 strlen (record_data_b64),
251 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
252 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (record_data_size,
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258 "Unable to deserialize record %s\n", label);
259 GNUNET_free (entry->label);
261 GNUNET_free (record_data);
264 GNUNET_free (record_data);
265 GNUNET_STRINGS_base64_decode (zone_private_key,
266 strlen (zone_private_key),
267 (char**)&entry->private_key);
268 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
269 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
270 GNUNET_memcpy (key, label, strlen (label));
271 GNUNET_memcpy (key+strlen(label),
273 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
274 GNUNET_CRYPTO_hash (key,
279 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
282 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
289 GNUNET_free (buffer);
295 * Store values in hashmap in file and free data
297 * @param plugin the plugin context
300 store_and_free_entries (void *cls,
301 const struct GNUNET_HashCode *key,
304 struct GNUNET_DISK_FileHandle *fh = cls;
305 struct FlatFileEntry *entry = value;
307 char *zone_private_key;
308 char *record_data_b64;
311 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
312 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
314 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
317 char data[data_size];
320 GNUNET_GNSRECORD_records_serialize (entry->record_count,
326 GNUNET_free (zone_private_key);
327 return GNUNET_SYSERR;
329 GNUNET_STRINGS_base64_encode (data,
333 GNUNET_asprintf (&line,
340 GNUNET_free (record_data_b64);
341 GNUNET_free (zone_private_key);
343 GNUNET_DISK_file_write (fh,
348 GNUNET_free (entry->private_key);
349 GNUNET_free (entry->label);
350 GNUNET_free (entry->record_data);
357 * Shutdown database connection and associate data
359 * @param plugin the plugin context (state for this module)
362 database_shutdown (struct Plugin *plugin)
364 struct GNUNET_DISK_FileHandle *fh;
365 fh = GNUNET_DISK_file_open (plugin->fn,
366 GNUNET_DISK_OPEN_CREATE |
367 GNUNET_DISK_OPEN_TRUNCATE |
368 GNUNET_DISK_OPEN_READWRITE,
369 GNUNET_DISK_PERM_USER_WRITE |
370 GNUNET_DISK_PERM_USER_READ);
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
374 _("Unable to initialize file: %s.\n"),
379 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
380 &store_and_free_entries,
382 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
383 GNUNET_DISK_file_close (fh);
388 * Store a record in the datastore. Removes any existing record in the
389 * same zone with the same name.
391 * @param cls closure (internal context for the plugin)
392 * @param zone_key private key of the zone
393 * @param label name that is being mapped (at most 255 characters long)
394 * @param rd_count number of entries in @a rd array
395 * @param rd array of records with data to store
396 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
399 namestore_store_records (void *cls,
400 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
402 unsigned int rd_count,
403 const struct GNUNET_GNSRECORD_Data *rd)
405 struct Plugin *plugin = cls;
409 struct GNUNET_HashCode hkey;
410 struct FlatFileEntry *entry;
413 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
414 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
415 key = GNUNET_malloc (key_len);
416 GNUNET_memcpy (key, label, strlen (label));
417 GNUNET_memcpy (key+strlen(label),
419 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
420 GNUNET_CRYPTO_hash (key,
424 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
428 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
429 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
430 GNUNET_asprintf (&entry->label,
433 GNUNET_memcpy (entry->private_key,
435 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
436 entry->rvalue = rvalue;
437 entry->record_count = rd_count;
438 entry->record_data = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * rd_count);
439 for (i = 0; i < rd_count; i++)
441 entry->record_data[i].expiration_time = rd[i].expiration_time;
442 entry->record_data[i].record_type = rd[i].record_type;
443 entry->record_data[i].flags = rd[i].flags;
444 entry->record_data[i].data_size = rd[i].data_size;
445 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
446 GNUNET_memcpy ((char*)entry->record_data[i].data, rd[i].data, rd[i].data_size);
448 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
451 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
458 * Lookup records in the datastore for which we are the authority.
460 * @param cls closure (internal context for the plugin)
461 * @param zone private key of the zone
462 * @param label name of the record in the zone
463 * @param iter function to call with the result
464 * @param iter_cls closure for @a iter
465 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
468 namestore_lookup_records (void *cls,
469 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
471 GNUNET_NAMESTORE_RecordIterator iter,
474 struct Plugin *plugin = cls;
475 struct FlatFileEntry *entry;
476 struct GNUNET_HashCode hkey;
482 return GNUNET_SYSERR;
484 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
485 key = GNUNET_malloc (key_len);
486 GNUNET_memcpy (key, label, strlen (label));
487 GNUNET_memcpy (key+strlen(label),
489 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
490 GNUNET_CRYPTO_hash (key,
495 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
500 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
506 iterate_zones (void *cls,
507 const struct GNUNET_HashCode *key,
510 struct Plugin *plugin = cls;
511 struct FlatFileEntry *entry = value;
513 if ((plugin->target_offset > plugin->offset) ||
514 ( (NULL != plugin->iter_zone) &&
515 (0 != memcmp (entry->private_key,
517 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))))) {
521 plugin->iter (plugin->iter_cls,
526 plugin->iter_result_found = GNUNET_YES;
531 * Iterate over the results for a particular key and zone in the
532 * datastore. Will return at most one result to the iterator.
534 * @param cls closure (internal context for the plugin)
535 * @param zone hash of public key of the zone, NULL to iterate over all zones
536 * @param offset offset in the list of all matching records
537 * @param iter function to call with the result
538 * @param iter_cls closure for @a iter
539 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
542 namestore_iterate_records (void *cls,
543 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
545 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
547 struct Plugin *plugin = cls;
548 plugin->target_offset = offset;
551 plugin->iter_cls = iter_cls;
552 plugin->iter_zone = zone;
553 plugin->iter_result_found = GNUNET_NO;
554 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
557 return plugin->iter_result_found;
561 zone_to_name (void *cls,
562 const struct GNUNET_HashCode *key,
565 struct Plugin *plugin = cls;
566 struct FlatFileEntry *entry = value;
569 if (0 != memcmp (entry->private_key,
571 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
574 for (i = 0; i < entry->record_count; i++) {
575 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
577 if (0 == memcmp (plugin->iter_pkey,
578 entry->record_data[i].data,
579 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
581 plugin->iter (plugin->iter_cls,
586 plugin->iter_result_found = GNUNET_YES;
595 * Look for an existing PKEY delegation record for a given public key.
596 * Returns at most one result to the iterator.
598 * @param cls closure (internal context for the plugin)
599 * @param zone private key of the zone to look up in, never NULL
600 * @param value_zone public key of the target zone (value), never NULL
601 * @param iter function to call with the result
602 * @param iter_cls closure for @a iter
603 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
606 namestore_zone_to_name (void *cls,
607 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
608 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
609 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
611 struct Plugin *plugin = cls;
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Performing reverse lookup for `%s'\n",
615 GNUNET_GNSRECORD_z2s (value_zone));
617 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
622 return plugin->iter_result_found;
627 * Entry point for the plugin.
629 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
630 * @return NULL on error, otherwise the plugin context
633 libgnunet_plugin_namestore_flat_init (void *cls)
635 static struct Plugin plugin;
636 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
637 struct GNUNET_NAMESTORE_PluginFunctions *api;
639 if (NULL != plugin.cfg)
640 return NULL; /* can only initialize once! */
641 memset (&plugin, 0, sizeof (struct Plugin));
643 if (GNUNET_OK != database_setup (&plugin))
645 database_shutdown (&plugin);
648 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
650 api->store_records = &namestore_store_records;
651 api->iterate_records = &namestore_iterate_records;
652 api->zone_to_name = &namestore_zone_to_name;
653 api->lookup_records = &namestore_lookup_records;
654 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
655 _("flat file database running\n"));
661 * Exit point from the plugin.
663 * @param cls the plugin context (as returned by "init")
664 * @return always NULL
667 libgnunet_plugin_namestore_flat_done (void *cls)
669 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
670 struct Plugin *plugin = api->cls;
672 database_shutdown (plugin);
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "flat file plugin is finished\n");
680 /* end of plugin_namestore_flat.c */