2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015 Christian Grothoff (and other contributing authors)
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 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey;
103 uint32_t record_count;
113 struct GNUNET_GNSRECORD_Data *record_data;
125 * Initialize the database connections and associated
126 * data structures (create tables and indices
127 * as needed as well).
129 * @param plugin the plugin context (state for this module)
130 * @return #GNUNET_OK on success
133 database_setup (struct Plugin *plugin)
138 char *zone_private_key;
140 char *record_data_b64;
146 size_t record_data_size;
148 struct GNUNET_HashCode hkey;
149 struct GNUNET_DISK_FileHandle *fh;
150 struct FlatFileEntry *entry;
153 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
155 "FILENAME", &afsdir))
157 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
158 "namestore-flat", "FILENAME");
159 return GNUNET_SYSERR;
161 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
163 if (GNUNET_OK != 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 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195 _("Unable to get filesize: %s.\n"),
197 return GNUNET_SYSERR;
200 buffer = GNUNET_malloc (size);
202 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 _("Unable to read file: %s.\n"),
209 return GNUNET_SYSERR;
212 GNUNET_DISK_file_close (fh);
214 line = strtok ("\n", buffer);
215 while (line != NULL) {
216 zone_private_key = strtok (",", line);
217 pkey = strtok (NULL, line);
218 rvalue = strtok (NULL, line);
219 record_count = strtok (NULL, line);
220 record_data_b64 = strtok (NULL, line);
221 label = strtok (NULL, line);
222 line = strtok ("\n", buffer);
223 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
224 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
227 sscanf (rvalue, "%lu", &entry->rvalue);
228 sscanf (record_count, "%u", &entry->record_count);
229 entry->label = GNUNET_strdup (label);
230 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
231 strlen (record_data_b64),
234 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
235 GNUNET_GNSRECORD_records_deserialize (record_data_size,
239 GNUNET_free (record_data);
240 GNUNET_STRINGS_base64_decode (zone_private_key,
241 strlen (zone_private_key),
242 (char**)&entry->private_key);
243 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
244 memcpy (key_str, label, strlen (label));
245 memcpy (key_str+strlen(label),
247 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
248 GNUNET_CRYPTO_hash (key_str,
251 GNUNET_free (key_str);
253 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
262 GNUNET_free (buffer);
268 * Store values in hashmap in file and free data
270 * @param plugin the plugin context
273 store_and_free_entries (void *cls,
274 const struct GNUNET_HashCode *key,
277 struct GNUNET_DISK_FileHandle *fh = cls;
278 struct FlatFileEntry *entry = value;
280 char *zone_private_key;
284 char *record_data_buf;
285 char *record_data_b64;
286 size_t record_data_len;
288 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
289 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
291 pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (entry->pkey);
292 GNUNET_asprintf (&rvalue, "%hhu", entry->rvalue);
293 GNUNET_asprintf (&record_count, "%u", entry->record_count);
295 record_data_len = GNUNET_GNSRECORD_records_get_size (entry->record_count,
298 record_data_buf = GNUNET_malloc (record_data_len);
299 GNUNET_GNSRECORD_records_serialize (entry->record_count,
304 GNUNET_STRINGS_base64_encode (record_data_buf,
305 strlen (record_data_buf),
308 GNUNET_asprintf (&line,
309 "%s,%s,%s,%s,%s,%s\n",
317 GNUNET_free (rvalue);
318 GNUNET_free (record_count);
319 GNUNET_free (record_data_buf);
320 GNUNET_free (record_data_b64);
322 GNUNET_DISK_file_write (fh,
326 GNUNET_free (entry->private_key);
327 GNUNET_free (entry->pkey);
328 GNUNET_free (entry->label);
329 GNUNET_free (entry->record_data);
335 * Shutdown database connection and associate data
337 * @param plugin the plugin context (state for this module)
340 database_shutdown (struct Plugin *plugin)
342 struct GNUNET_DISK_FileHandle *fh;
343 fh = GNUNET_DISK_file_open (plugin->fn,
344 GNUNET_DISK_OPEN_CREATE |
345 GNUNET_DISK_OPEN_TRUNCATE,
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;
383 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
388 struct GNUNET_HashCode hkey;
389 struct FlatFileEntry *entry;
391 memset (&pkey, 0, sizeof (pkey));
392 for (i=0;i<rd_count;i++)
393 if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
395 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) == rd[i].data_size);
401 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
402 data_size = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
403 if (data_size > 64 * 65536)
406 return GNUNET_SYSERR;
408 char data[data_size];
410 if (data_size != GNUNET_GNSRECORD_records_serialize (rd_count, rd,
414 return GNUNET_SYSERR;
417 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
418 memcpy (key_str, label, strlen (label));
419 memcpy (key_str+strlen(label),
421 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
422 GNUNET_CRYPTO_hash (key_str,
426 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
430 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
431 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
432 memcpy (&entry->private_key,
434 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
435 entry->pkey = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
438 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
439 entry->rvalue = rvalue;
440 entry->record_count = rd_count;
441 entry->record_data = GNUNET_malloc (data_size);
442 memcpy (&entry->record_data, data, data_size);
443 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
453 * Lookup records in the datastore for which we are the authority.
455 * @param cls closure (internal context for the plugin)
456 * @param zone private key of the zone
457 * @param label name of the record in the zone
458 * @param iter function to call with the result
459 * @param iter_cls closure for @a iter
460 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
463 namestore_lookup_records (void *cls,
464 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
466 GNUNET_NAMESTORE_RecordIterator iter,
469 struct Plugin *plugin = cls;
470 struct FlatFileEntry *entry;
471 struct GNUNET_HashCode hkey;
476 return GNUNET_SYSERR;
478 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
479 memcpy (key_str, label, strlen (label));
480 memcpy (key_str+strlen(label),
482 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
483 GNUNET_CRYPTO_hash (key_str,
486 GNUNET_free (key_str);
488 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
493 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
499 iterate_zones (void *cls,
500 const struct GNUNET_HashCode *key,
503 struct Plugin *plugin = cls;
504 struct FlatFileEntry *entry = value;
507 if ((plugin->target_offset > plugin->offset) ||
508 (0 != memcmp (entry->private_key,
510 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))) {
515 plugin->iter (plugin->iter_cls,
520 plugin->iter_result_found = GNUNET_YES;
525 * Iterate over the results for a particular key and zone in the
526 * datastore. Will return at most one result to the iterator.
528 * @param cls closure (internal context for the plugin)
529 * @param zone hash of public key of the zone, NULL to iterate over all zones
530 * @param offset offset in the list of all matching records
531 * @param iter function to call with the result
532 * @param iter_cls closure for @a iter
533 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
536 namestore_iterate_records (void *cls,
537 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
539 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
541 struct Plugin *plugin = cls;
542 plugin->target_offset = offset;
545 plugin->iter_cls = cls;
546 plugin->iter_zone = zone;
547 plugin->iter_result_found = GNUNET_NO;
548 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
551 return plugin->iter_result_found;
555 zone_to_name (void *cls,
556 const struct GNUNET_HashCode *key,
559 struct Plugin *plugin = cls;
560 struct FlatFileEntry *entry = value;
563 if (0 != memcmp (entry->private_key,
565 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
568 for (i = 0; i < entry->record_count; i++) {
569 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
571 if (0 == memcmp (plugin->iter_pkey,
572 entry->record_data[i].data,
573 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
575 plugin->iter (plugin->iter_cls,
580 plugin->iter_result_found = GNUNET_YES;
589 * Look for an existing PKEY delegation record for a given public key.
590 * Returns at most one result to the iterator.
592 * @param cls closure (internal context for the plugin)
593 * @param zone private key of the zone to look up in, never NULL
594 * @param value_zone public key of the target zone (value), never NULL
595 * @param iter function to call with the result
596 * @param iter_cls closure for @a iter
597 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
600 namestore_zone_to_name (void *cls,
601 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
602 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
603 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
605 struct Plugin *plugin = cls;
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
608 "Performing reverse lookup for `%s'\n",
609 GNUNET_GNSRECORD_z2s (value_zone));
611 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
616 return plugin->iter_result_found;
621 * Entry point for the plugin.
623 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
624 * @return NULL on error, otherwise the plugin context
627 libgnunet_plugin_namestore_flat_init (void *cls)
629 static struct Plugin plugin;
630 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
631 struct GNUNET_NAMESTORE_PluginFunctions *api;
633 if (NULL != plugin.cfg)
634 return NULL; /* can only initialize once! */
635 memset (&plugin, 0, sizeof (struct Plugin));
637 if (GNUNET_OK != database_setup (&plugin))
639 database_shutdown (&plugin);
642 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
644 api->store_records = &namestore_store_records;
645 api->iterate_records = &namestore_iterate_records;
646 api->zone_to_name = &namestore_zone_to_name;
647 api->lookup_records = &namestore_lookup_records;
648 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
649 _("flat file database running\n"));
655 * Exit point from the plugin.
657 * @param cls the plugin context (as returned by "init")
658 * @return always NULL
661 libgnunet_plugin_namestore_flat_done (void *cls)
663 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
664 struct Plugin *plugin = api->cls;
666 database_shutdown (plugin);
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "flat file plugin is finished\n");
674 /* end of plugin_namestore_flat.c */