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 if (GNUNET_SYSERR == 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);
197 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 _("Unable to read file: %s.\n"),
204 return GNUNET_SYSERR;
207 GNUNET_DISK_file_close (fh);
209 line = strtok (buffer, "\n");
210 while (line != NULL) {
211 zone_private_key = strtok (line, ",");
212 if (NULL == zone_private_key)
214 rvalue = strtok (NULL, ",");
217 record_count = strtok (NULL, ",");
218 if (NULL == record_count)
220 record_data_b64 = strtok (NULL, ",");
221 if (NULL == record_data_b64)
223 label = strtok (NULL, ",");
226 line = strtok (NULL, "\n");
227 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
228 sscanf (rvalue, "%lu", &entry->rvalue);
229 sscanf (record_count, "%u", &entry->record_count);
230 entry->label = GNUNET_strdup (label);
231 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
232 strlen (record_data_b64),
235 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
236 GNUNET_GNSRECORD_records_deserialize (record_data_size,
240 GNUNET_free (record_data);
241 GNUNET_STRINGS_base64_decode (zone_private_key,
242 strlen (zone_private_key),
243 (char**)&entry->private_key);
244 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
245 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
246 memcpy (key, label, strlen (label));
247 memcpy (key+strlen(label),
249 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
250 GNUNET_CRYPTO_hash (key,
255 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
265 GNUNET_free (buffer);
271 * Store values in hashmap in file and free data
273 * @param plugin the plugin context
276 store_and_free_entries (void *cls,
277 const struct GNUNET_HashCode *key,
280 struct GNUNET_DISK_FileHandle *fh = cls;
281 struct FlatFileEntry *entry = value;
283 char *zone_private_key;
286 char *record_data_b64;
289 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
290 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
292 GNUNET_asprintf (&rvalue, "%lu", entry->rvalue);
293 GNUNET_asprintf (&record_count, "%u", entry->record_count);
295 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
297 char data[data_size];
298 if (data_size != GNUNET_GNSRECORD_records_serialize (entry->record_count,
304 return GNUNET_SYSERR;
306 GNUNET_STRINGS_base64_encode (data,
310 GNUNET_asprintf (&line,
318 GNUNET_free (rvalue);
319 GNUNET_free (record_count);
320 GNUNET_free (record_data_b64);
322 GNUNET_DISK_file_write (fh,
326 GNUNET_free (entry->private_key);
327 GNUNET_free (entry->label);
328 GNUNET_free (entry->record_data);
334 * Shutdown database connection and associate data
336 * @param plugin the plugin context (state for this module)
339 database_shutdown (struct Plugin *plugin)
341 struct GNUNET_DISK_FileHandle *fh;
342 fh = GNUNET_DISK_file_open (plugin->fn,
343 GNUNET_DISK_OPEN_CREATE |
344 GNUNET_DISK_OPEN_TRUNCATE |
345 GNUNET_DISK_OPEN_READWRITE,
346 GNUNET_DISK_PERM_USER_WRITE |
347 GNUNET_DISK_PERM_USER_READ);
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 _("Unable to initialize file: %s.\n"),
356 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
357 &store_and_free_entries,
359 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
360 GNUNET_DISK_file_close (fh);
365 * Store a record in the datastore. Removes any existing record in the
366 * same zone with the same name.
368 * @param cls closure (internal context for the plugin)
369 * @param zone_key private key of the zone
370 * @param label name that is being mapped (at most 255 characters long)
371 * @param rd_count number of entries in @a rd array
372 * @param rd array of records with data to store
373 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
376 namestore_store_records (void *cls,
377 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
379 unsigned int rd_count,
380 const struct GNUNET_GNSRECORD_Data *rd)
382 struct Plugin *plugin = cls;
386 struct GNUNET_HashCode hkey;
387 struct FlatFileEntry *entry;
390 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
391 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
392 key = GNUNET_malloc (key_len);
393 memcpy (key, label, strlen (label));
394 memcpy (key+strlen(label),
396 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
397 GNUNET_CRYPTO_hash (key,
401 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
405 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
406 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
407 GNUNET_asprintf (&entry->label,
410 memcpy (entry->private_key,
412 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
413 entry->rvalue = rvalue;
414 entry->record_count = rd_count;
415 entry->record_data = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * rd_count);
416 for (i = 0; i < rd_count; i++)
418 entry->record_data[i].expiration_time = rd[i].expiration_time;
419 entry->record_data[i].record_type = rd[i].record_type;
420 entry->record_data[i].flags = rd[i].flags;
421 entry->record_data[i].data_size = rd[i].data_size;
422 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
423 memcpy ((char*)entry->record_data[i].data, rd[i].data, rd[i].data_size);
425 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
428 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
435 * Lookup records in the datastore for which we are the authority.
437 * @param cls closure (internal context for the plugin)
438 * @param zone private key of the zone
439 * @param label name of the record in the zone
440 * @param iter function to call with the result
441 * @param iter_cls closure for @a iter
442 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
445 namestore_lookup_records (void *cls,
446 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
448 GNUNET_NAMESTORE_RecordIterator iter,
451 struct Plugin *plugin = cls;
452 struct FlatFileEntry *entry;
453 struct GNUNET_HashCode hkey;
459 return GNUNET_SYSERR;
461 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
462 key = GNUNET_malloc (key_len);
463 memcpy (key, label, strlen (label));
464 memcpy (key+strlen(label),
466 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
467 GNUNET_CRYPTO_hash (key,
472 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
477 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
483 iterate_zones (void *cls,
484 const struct GNUNET_HashCode *key,
487 struct Plugin *plugin = cls;
488 struct FlatFileEntry *entry = value;
490 if ((plugin->target_offset > plugin->offset) ||
491 ( (NULL != plugin->iter_zone) &&
492 (0 != memcmp (entry->private_key,
494 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))))) {
498 plugin->iter (plugin->iter_cls,
503 plugin->iter_result_found = GNUNET_YES;
508 * Iterate over the results for a particular key and zone in the
509 * datastore. Will return at most one result to the iterator.
511 * @param cls closure (internal context for the plugin)
512 * @param zone hash of public key of the zone, NULL to iterate over all zones
513 * @param offset offset in the list of all matching records
514 * @param iter function to call with the result
515 * @param iter_cls closure for @a iter
516 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
519 namestore_iterate_records (void *cls,
520 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
522 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
524 struct Plugin *plugin = cls;
525 plugin->target_offset = offset;
528 plugin->iter_cls = iter_cls;
529 plugin->iter_zone = zone;
530 plugin->iter_result_found = GNUNET_NO;
531 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
534 return plugin->iter_result_found;
538 zone_to_name (void *cls,
539 const struct GNUNET_HashCode *key,
542 struct Plugin *plugin = cls;
543 struct FlatFileEntry *entry = value;
546 if (0 != memcmp (entry->private_key,
548 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
551 for (i = 0; i < entry->record_count; i++) {
552 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
554 if (0 == memcmp (plugin->iter_pkey,
555 entry->record_data[i].data,
556 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
558 plugin->iter (plugin->iter_cls,
563 plugin->iter_result_found = GNUNET_YES;
572 * Look for an existing PKEY delegation record for a given public key.
573 * Returns at most one result to the iterator.
575 * @param cls closure (internal context for the plugin)
576 * @param zone private key of the zone to look up in, never NULL
577 * @param value_zone public key of the target zone (value), never NULL
578 * @param iter function to call with the result
579 * @param iter_cls closure for @a iter
580 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
583 namestore_zone_to_name (void *cls,
584 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
585 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
586 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
588 struct Plugin *plugin = cls;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Performing reverse lookup for `%s'\n",
592 GNUNET_GNSRECORD_z2s (value_zone));
594 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
599 return plugin->iter_result_found;
604 * Entry point for the plugin.
606 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
607 * @return NULL on error, otherwise the plugin context
610 libgnunet_plugin_namestore_flat_init (void *cls)
612 static struct Plugin plugin;
613 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
614 struct GNUNET_NAMESTORE_PluginFunctions *api;
616 if (NULL != plugin.cfg)
617 return NULL; /* can only initialize once! */
618 memset (&plugin, 0, sizeof (struct Plugin));
620 if (GNUNET_OK != database_setup (&plugin))
622 database_shutdown (&plugin);
625 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
627 api->store_records = &namestore_store_records;
628 api->iterate_records = &namestore_iterate_records;
629 api->zone_to_name = &namestore_zone_to_name;
630 api->lookup_records = &namestore_lookup_records;
631 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
632 _("flat file database running\n"));
638 * Exit point from the plugin.
640 * @param cls the plugin context (as returned by "init")
641 * @return always NULL
644 libgnunet_plugin_namestore_flat_done (void *cls)
646 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
647 struct Plugin *plugin = api->cls;
649 database_shutdown (plugin);
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "flat file plugin is finished\n");
657 /* end of plugin_namestore_flat.c */