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_OPEN_READWRITE,
347 GNUNET_DISK_PERM_USER_WRITE |
348 GNUNET_DISK_PERM_USER_READ);
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 _("Unable to initialize file: %s.\n"),
357 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
358 &store_and_free_entries,
360 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
361 GNUNET_DISK_file_close (fh);
366 * Store a record in the datastore. Removes any existing record in the
367 * same zone with the same name.
369 * @param cls closure (internal context for the plugin)
370 * @param zone_key private key of the zone
371 * @param label name that is being mapped (at most 255 characters long)
372 * @param rd_count number of entries in @a rd array
373 * @param rd array of records with data to store
374 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
377 namestore_store_records (void *cls,
378 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
380 unsigned int rd_count,
381 const struct GNUNET_GNSRECORD_Data *rd)
383 struct Plugin *plugin = cls;
384 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
389 struct GNUNET_HashCode hkey;
390 struct FlatFileEntry *entry;
392 memset (&pkey, 0, sizeof (pkey));
393 for (i=0;i<rd_count;i++)
394 if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
396 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) == rd[i].data_size);
402 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
403 data_size = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
404 if (data_size > 64 * 65536)
407 return GNUNET_SYSERR;
409 char data[data_size];
411 if (data_size != GNUNET_GNSRECORD_records_serialize (rd_count, rd,
415 return GNUNET_SYSERR;
418 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
419 memcpy (key_str, label, strlen (label));
420 memcpy (key_str+strlen(label),
422 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
423 GNUNET_CRYPTO_hash (key_str,
427 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
431 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
432 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
433 memcpy (&entry->private_key,
435 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
436 entry->pkey = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
439 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
440 entry->rvalue = rvalue;
441 entry->record_count = rd_count;
442 entry->record_data = GNUNET_malloc (data_size);
443 memcpy (&entry->record_data, data, 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;
477 return GNUNET_SYSERR;
479 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
480 memcpy (key_str, label, strlen (label));
481 memcpy (key_str+strlen(label),
483 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
484 GNUNET_CRYPTO_hash (key_str,
487 GNUNET_free (key_str);
489 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
494 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
500 iterate_zones (void *cls,
501 const struct GNUNET_HashCode *key,
504 struct Plugin *plugin = cls;
505 struct FlatFileEntry *entry = value;
508 if ((plugin->target_offset > plugin->offset) ||
509 (0 != memcmp (entry->private_key,
511 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))) {
516 plugin->iter (plugin->iter_cls,
521 plugin->iter_result_found = GNUNET_YES;
526 * Iterate over the results for a particular key and zone in the
527 * datastore. Will return at most one result to the iterator.
529 * @param cls closure (internal context for the plugin)
530 * @param zone hash of public key of the zone, NULL to iterate over all zones
531 * @param offset offset in the list of all matching records
532 * @param iter function to call with the result
533 * @param iter_cls closure for @a iter
534 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
537 namestore_iterate_records (void *cls,
538 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
540 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
542 struct Plugin *plugin = cls;
543 plugin->target_offset = offset;
546 plugin->iter_cls = cls;
547 plugin->iter_zone = zone;
548 plugin->iter_result_found = GNUNET_NO;
549 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
552 return plugin->iter_result_found;
556 zone_to_name (void *cls,
557 const struct GNUNET_HashCode *key,
560 struct Plugin *plugin = cls;
561 struct FlatFileEntry *entry = value;
564 if (0 != memcmp (entry->private_key,
566 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
569 for (i = 0; i < entry->record_count; i++) {
570 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
572 if (0 == memcmp (plugin->iter_pkey,
573 entry->record_data[i].data,
574 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
576 plugin->iter (plugin->iter_cls,
581 plugin->iter_result_found = GNUNET_YES;
590 * Look for an existing PKEY delegation record for a given public key.
591 * Returns at most one result to the iterator.
593 * @param cls closure (internal context for the plugin)
594 * @param zone private key of the zone to look up in, never NULL
595 * @param value_zone public key of the target zone (value), never NULL
596 * @param iter function to call with the result
597 * @param iter_cls closure for @a iter
598 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
601 namestore_zone_to_name (void *cls,
602 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
603 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
604 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
606 struct Plugin *plugin = cls;
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
609 "Performing reverse lookup for `%s'\n",
610 GNUNET_GNSRECORD_z2s (value_zone));
612 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
617 return plugin->iter_result_found;
622 * Entry point for the plugin.
624 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
625 * @return NULL on error, otherwise the plugin context
628 libgnunet_plugin_namestore_flat_init (void *cls)
630 static struct Plugin plugin;
631 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
632 struct GNUNET_NAMESTORE_PluginFunctions *api;
634 if (NULL != plugin.cfg)
635 return NULL; /* can only initialize once! */
636 memset (&plugin, 0, sizeof (struct Plugin));
638 if (GNUNET_OK != database_setup (&plugin))
640 database_shutdown (&plugin);
643 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
645 api->store_records = &namestore_store_records;
646 api->iterate_records = &namestore_iterate_records;
647 api->zone_to_name = &namestore_zone_to_name;
648 api->lookup_records = &namestore_lookup_records;
649 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
650 _("flat file database running\n"));
656 * Exit point from the plugin.
658 * @param cls the plugin context (as returned by "init")
659 * @return always NULL
662 libgnunet_plugin_namestore_flat_done (void *cls)
664 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
665 struct Plugin *plugin = api->cls;
667 database_shutdown (plugin);
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "flat file plugin is finished\n");
675 /* end of plugin_namestore_flat.c */