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 return GNUNET_SYSERR;
206 GNUNET_DISK_file_close (fh);
210 line = strtok (buffer, "\n");
211 while (line != NULL) {
212 zone_private_key = strtok (line, ",");
213 if (NULL == zone_private_key)
215 rvalue = strtok (NULL, ",");
218 record_count = strtok (NULL, ",");
219 if (NULL == record_count)
221 record_data_b64 = strtok (NULL, ",");
222 if (NULL == record_data_b64)
224 label = strtok (NULL, ",");
227 line = strtok (NULL, "\n");
228 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
229 if (1 != sscanf (rvalue, "%lu", &entry->rvalue))
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232 "Error parsing entry\n");
236 if (1 != sscanf (record_count, "%u", &entry->record_count))
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239 "Error parsing entry\n");
243 entry->label = GNUNET_strdup (label);
244 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
245 strlen (record_data_b64),
248 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
249 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (record_data_size,
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255 "Unable to deserialize record %s\n", label);
256 GNUNET_free (entry->label);
258 GNUNET_free (record_data);
261 GNUNET_free (record_data);
262 GNUNET_STRINGS_base64_decode (zone_private_key,
263 strlen (zone_private_key),
264 (char**)&entry->private_key);
265 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
266 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
267 GNUNET_memcpy (key, label, strlen (label));
268 GNUNET_memcpy (key+strlen(label),
270 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
271 GNUNET_CRYPTO_hash (key,
276 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
279 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
286 GNUNET_free (buffer);
292 * Store values in hashmap in file and free data
294 * @param plugin the plugin context
297 store_and_free_entries (void *cls,
298 const struct GNUNET_HashCode *key,
301 struct GNUNET_DISK_FileHandle *fh = cls;
302 struct FlatFileEntry *entry = value;
304 char *zone_private_key;
305 char *record_data_b64;
308 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
309 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
311 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
314 char data[data_size];
317 GNUNET_GNSRECORD_records_serialize (entry->record_count,
323 GNUNET_free (zone_private_key);
324 return GNUNET_SYSERR;
326 GNUNET_STRINGS_base64_encode (data,
330 GNUNET_asprintf (&line,
337 GNUNET_free (record_data_b64);
338 GNUNET_free (zone_private_key);
340 GNUNET_DISK_file_write (fh,
344 GNUNET_free (entry->private_key);
345 GNUNET_free (entry->label);
346 GNUNET_free (entry->record_data);
353 * Shutdown database connection and associate data
355 * @param plugin the plugin context (state for this module)
358 database_shutdown (struct Plugin *plugin)
360 struct GNUNET_DISK_FileHandle *fh;
361 fh = GNUNET_DISK_file_open (plugin->fn,
362 GNUNET_DISK_OPEN_CREATE |
363 GNUNET_DISK_OPEN_TRUNCATE |
364 GNUNET_DISK_OPEN_READWRITE,
365 GNUNET_DISK_PERM_USER_WRITE |
366 GNUNET_DISK_PERM_USER_READ);
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 _("Unable to initialize file: %s.\n"),
375 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
376 &store_and_free_entries,
378 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
379 GNUNET_DISK_file_close (fh);
384 * Store a record in the datastore. Removes any existing record in the
385 * same zone with the same name.
387 * @param cls closure (internal context for the plugin)
388 * @param zone_key private key of the zone
389 * @param label name that is being mapped (at most 255 characters long)
390 * @param rd_count number of entries in @a rd array
391 * @param rd array of records with data to store
392 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
395 namestore_store_records (void *cls,
396 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
398 unsigned int rd_count,
399 const struct GNUNET_GNSRECORD_Data *rd)
401 struct Plugin *plugin = cls;
405 struct GNUNET_HashCode hkey;
406 struct FlatFileEntry *entry;
409 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
410 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
411 key = GNUNET_malloc (key_len);
412 GNUNET_memcpy (key, label, strlen (label));
413 GNUNET_memcpy (key+strlen(label),
415 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
416 GNUNET_CRYPTO_hash (key,
420 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
424 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
425 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
426 GNUNET_asprintf (&entry->label,
429 GNUNET_memcpy (entry->private_key,
431 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
432 entry->rvalue = rvalue;
433 entry->record_count = rd_count;
434 entry->record_data = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * rd_count);
435 for (i = 0; i < rd_count; i++)
437 entry->record_data[i].expiration_time = rd[i].expiration_time;
438 entry->record_data[i].record_type = rd[i].record_type;
439 entry->record_data[i].flags = rd[i].flags;
440 entry->record_data[i].data_size = rd[i].data_size;
441 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
442 GNUNET_memcpy ((char*)entry->record_data[i].data, rd[i].data, rd[i].data_size);
444 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
454 * Lookup records in the datastore for which we are the authority.
456 * @param cls closure (internal context for the plugin)
457 * @param zone private key of the zone
458 * @param label name of the record in the zone
459 * @param iter function to call with the result
460 * @param iter_cls closure for @a iter
461 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
464 namestore_lookup_records (void *cls,
465 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
467 GNUNET_NAMESTORE_RecordIterator iter,
470 struct Plugin *plugin = cls;
471 struct FlatFileEntry *entry;
472 struct GNUNET_HashCode hkey;
478 return GNUNET_SYSERR;
480 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
481 key = GNUNET_malloc (key_len);
482 GNUNET_memcpy (key, label, strlen (label));
483 GNUNET_memcpy (key+strlen(label),
485 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
486 GNUNET_CRYPTO_hash (key,
491 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
496 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
502 iterate_zones (void *cls,
503 const struct GNUNET_HashCode *key,
506 struct Plugin *plugin = cls;
507 struct FlatFileEntry *entry = value;
509 if ((plugin->target_offset > plugin->offset) ||
510 ( (NULL != plugin->iter_zone) &&
511 (0 != memcmp (entry->private_key,
513 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))))) {
517 plugin->iter (plugin->iter_cls,
522 plugin->iter_result_found = GNUNET_YES;
527 * Iterate over the results for a particular key and zone in the
528 * datastore. Will return at most one result to the iterator.
530 * @param cls closure (internal context for the plugin)
531 * @param zone hash of public key of the zone, NULL to iterate over all zones
532 * @param offset offset in the list of all matching records
533 * @param iter function to call with the result
534 * @param iter_cls closure for @a iter
535 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
538 namestore_iterate_records (void *cls,
539 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
541 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
543 struct Plugin *plugin = cls;
544 plugin->target_offset = offset;
547 plugin->iter_cls = iter_cls;
548 plugin->iter_zone = zone;
549 plugin->iter_result_found = GNUNET_NO;
550 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
553 return plugin->iter_result_found;
557 zone_to_name (void *cls,
558 const struct GNUNET_HashCode *key,
561 struct Plugin *plugin = cls;
562 struct FlatFileEntry *entry = value;
565 if (0 != memcmp (entry->private_key,
567 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
570 for (i = 0; i < entry->record_count; i++) {
571 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
573 if (0 == memcmp (plugin->iter_pkey,
574 entry->record_data[i].data,
575 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
577 plugin->iter (plugin->iter_cls,
582 plugin->iter_result_found = GNUNET_YES;
591 * Look for an existing PKEY delegation record for a given public key.
592 * Returns at most one result to the iterator.
594 * @param cls closure (internal context for the plugin)
595 * @param zone private key of the zone to look up in, never NULL
596 * @param value_zone public key of the target zone (value), never NULL
597 * @param iter function to call with the result
598 * @param iter_cls closure for @a iter
599 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
602 namestore_zone_to_name (void *cls,
603 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
604 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
605 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
607 struct Plugin *plugin = cls;
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "Performing reverse lookup for `%s'\n",
611 GNUNET_GNSRECORD_z2s (value_zone));
613 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
618 return plugin->iter_result_found;
623 * Entry point for the plugin.
625 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
626 * @return NULL on error, otherwise the plugin context
629 libgnunet_plugin_namestore_flat_init (void *cls)
631 static struct Plugin plugin;
632 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
633 struct GNUNET_NAMESTORE_PluginFunctions *api;
635 if (NULL != plugin.cfg)
636 return NULL; /* can only initialize once! */
637 memset (&plugin, 0, sizeof (struct Plugin));
639 if (GNUNET_OK != database_setup (&plugin))
641 database_shutdown (&plugin);
644 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
646 api->store_records = &namestore_store_records;
647 api->iterate_records = &namestore_iterate_records;
648 api->zone_to_name = &namestore_zone_to_name;
649 api->lookup_records = &namestore_lookup_records;
650 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
651 _("flat file database running\n"));
657 * Exit point from the plugin.
659 * @param cls the plugin context (as returned by "init")
660 * @return always NULL
663 libgnunet_plugin_namestore_flat_done (void *cls)
665 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
666 struct Plugin *plugin = api->cls;
668 database_shutdown (plugin);
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "flat file plugin is finished\n");
676 /* end of plugin_namestore_flat.c */