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_PERM_USER_WRITE |
179 GNUNET_DISK_PERM_USER_READ);
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 _("Unable to initialize file: %s.\n"),
185 return GNUNET_SYSERR;
188 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 _("Unable to get filesize: %s.\n"),
196 return GNUNET_SYSERR;
199 buffer = GNUNET_malloc (size);
201 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 _("Unable to read file: %s.\n"),
208 return GNUNET_SYSERR;
211 GNUNET_DISK_file_close (fh);
213 line = strtok ("\n", buffer);
214 while (line != NULL) {
215 zone_private_key = strtok (",", line);
216 pkey = strtok (NULL, line);
217 rvalue = strtok (NULL, line);
218 record_count = strtok (NULL, line);
219 record_data_b64 = strtok (NULL, line);
220 label = strtok (NULL, line);
221 line = strtok ("\n", buffer);
222 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
223 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
226 sscanf (rvalue, "%lu", &entry->rvalue);
227 sscanf (record_count, "%u", &entry->record_count);
228 entry->label = GNUNET_strdup (label);
229 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
230 strlen (record_data_b64),
233 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
234 GNUNET_GNSRECORD_records_deserialize (record_data_size,
238 GNUNET_free (record_data);
239 GNUNET_STRINGS_base64_decode (zone_private_key,
240 strlen (zone_private_key),
241 (char**)&entry->private_key);
242 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
243 memcpy (key_str, label, strlen (label));
244 memcpy (key_str+strlen(label),
246 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
247 GNUNET_CRYPTO_hash (key_str,
250 GNUNET_free (key_str);
252 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
261 GNUNET_free (buffer);
267 * Store values in hashmap in file and free data
269 * @param plugin the plugin context
272 store_and_free_entries (void *cls,
273 const struct GNUNET_HashCode *key,
276 struct GNUNET_DISK_FileHandle *fh = cls;
277 struct FlatFileEntry *entry = value;
279 char *zone_private_key;
283 char *record_data_buf;
284 char *record_data_b64;
285 size_t record_data_len;
287 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
288 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
290 pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (entry->pkey);
291 GNUNET_asprintf (&rvalue, "%hhu", entry->rvalue);
292 GNUNET_asprintf (&record_count, "%u", entry->record_count);
294 record_data_len = GNUNET_GNSRECORD_records_get_size (entry->record_count,
297 record_data_buf = GNUNET_malloc (record_data_len);
298 GNUNET_GNSRECORD_records_serialize (entry->record_count,
303 GNUNET_STRINGS_base64_encode (record_data_buf,
304 strlen (record_data_buf),
307 GNUNET_asprintf (&line,
308 "%s.%s.%s.%s.%s.%s\n",
316 GNUNET_free (rvalue);
317 GNUNET_free (record_count);
318 GNUNET_free (record_data_buf);
319 GNUNET_free (record_data_b64);
321 GNUNET_DISK_file_write (fh,
325 GNUNET_free (entry->private_key);
326 GNUNET_free (entry->pkey);
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_PERM_USER_WRITE |
346 GNUNET_DISK_PERM_USER_READ);
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350 _("Unable to initialize file: %s.\n"),
355 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
356 &store_and_free_entries,
358 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
359 GNUNET_DISK_file_close (fh);
364 * Store a record in the datastore. Removes any existing record in the
365 * same zone with the same name.
367 * @param cls closure (internal context for the plugin)
368 * @param zone_key private key of the zone
369 * @param label name that is being mapped (at most 255 characters long)
370 * @param rd_count number of entries in @a rd array
371 * @param rd array of records with data to store
372 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
375 namestore_store_records (void *cls,
376 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
378 unsigned int rd_count,
379 const struct GNUNET_GNSRECORD_Data *rd)
381 struct Plugin *plugin = cls;
382 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
387 struct GNUNET_HashCode hkey;
388 struct FlatFileEntry *entry;
390 memset (&pkey, 0, sizeof (pkey));
391 for (i=0;i<rd_count;i++)
392 if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
394 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) == rd[i].data_size);
400 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
401 data_size = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
402 if (data_size > 64 * 65536)
405 return GNUNET_SYSERR;
407 char data[data_size];
409 if (data_size != GNUNET_GNSRECORD_records_serialize (rd_count, rd,
413 return GNUNET_SYSERR;
416 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
417 memcpy (key_str, label, strlen (label));
418 memcpy (key_str+strlen(label),
420 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
421 GNUNET_CRYPTO_hash (key_str,
425 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
429 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
430 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
431 memcpy (&entry->private_key,
433 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
434 entry->pkey = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
437 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
438 entry->rvalue = rvalue;
439 entry->record_count = rd_count;
440 entry->record_data = GNUNET_malloc (data_size);
441 memcpy (&entry->record_data, data, data_size);
442 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
445 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
452 * Lookup records in the datastore for which we are the authority.
454 * @param cls closure (internal context for the plugin)
455 * @param zone private key of the zone
456 * @param label name of the record in the zone
457 * @param iter function to call with the result
458 * @param iter_cls closure for @a iter
459 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
462 namestore_lookup_records (void *cls,
463 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
465 GNUNET_NAMESTORE_RecordIterator iter,
468 struct Plugin *plugin = cls;
469 struct FlatFileEntry *entry;
470 struct GNUNET_HashCode hkey;
475 return GNUNET_SYSERR;
477 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
478 memcpy (key_str, label, strlen (label));
479 memcpy (key_str+strlen(label),
481 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
482 GNUNET_CRYPTO_hash (key_str,
485 GNUNET_free (key_str);
487 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
492 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
498 iterate_zones (void *cls,
499 const struct GNUNET_HashCode *key,
502 struct Plugin *plugin = cls;
503 struct FlatFileEntry *entry = value;
506 if ((plugin->target_offset > plugin->offset) ||
507 (0 != memcmp (entry->private_key,
509 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))) {
514 plugin->iter (plugin->iter_cls,
519 plugin->iter_result_found = GNUNET_YES;
524 * Iterate over the results for a particular key and zone in the
525 * datastore. Will return at most one result to the iterator.
527 * @param cls closure (internal context for the plugin)
528 * @param zone hash of public key of the zone, NULL to iterate over all zones
529 * @param offset offset in the list of all matching records
530 * @param iter function to call with the result
531 * @param iter_cls closure for @a iter
532 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
535 namestore_iterate_records (void *cls,
536 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
538 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
540 struct Plugin *plugin = cls;
541 plugin->target_offset = offset;
544 plugin->iter_cls = cls;
545 plugin->iter_zone = zone;
546 plugin->iter_result_found = GNUNET_NO;
547 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
550 return plugin->iter_result_found;
554 zone_to_name (void *cls,
555 const struct GNUNET_HashCode *key,
558 struct Plugin *plugin = cls;
559 struct FlatFileEntry *entry = value;
562 if (0 != memcmp (entry->private_key,
564 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
567 for (i = 0; i < entry->record_count; i++) {
568 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
570 if (0 == memcmp (plugin->iter_pkey,
571 entry->record_data[i].data,
572 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
574 plugin->iter (plugin->iter_cls,
579 plugin->iter_result_found = GNUNET_YES;
588 * Look for an existing PKEY delegation record for a given public key.
589 * Returns at most one result to the iterator.
591 * @param cls closure (internal context for the plugin)
592 * @param zone private key of the zone to look up in, never NULL
593 * @param value_zone public key of the target zone (value), never NULL
594 * @param iter function to call with the result
595 * @param iter_cls closure for @a iter
596 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
599 namestore_zone_to_name (void *cls,
600 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
601 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
602 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
604 struct Plugin *plugin = cls;
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Performing reverse lookup for `%s'\n",
608 GNUNET_GNSRECORD_z2s (value_zone));
610 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
615 return plugin->iter_result_found;
620 * Entry point for the plugin.
622 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
623 * @return NULL on error, otherwise the plugin context
626 libgnunet_plugin_namestore_flat_init (void *cls)
628 static struct Plugin plugin;
629 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
630 struct GNUNET_NAMESTORE_PluginFunctions *api;
632 if (NULL != plugin.cfg)
633 return NULL; /* can only initialize once! */
634 memset (&plugin, 0, sizeof (struct Plugin));
636 if (GNUNET_OK != database_setup (&plugin))
638 database_shutdown (&plugin);
641 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
643 api->store_records = &namestore_store_records;
644 api->iterate_records = &namestore_iterate_records;
645 api->zone_to_name = &namestore_zone_to_name;
646 api->lookup_records = &namestore_lookup_records;
647 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
648 _("flat file database running\n"));
654 * Exit point from the plugin.
656 * @param cls the plugin context (as returned by "init")
657 * @return always NULL
660 libgnunet_plugin_namestore_flat_done (void *cls)
662 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
663 struct Plugin *plugin = api->cls;
665 database_shutdown (plugin);
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "flat file plugin is finished\n");
673 /* end of plugin_namestore_flat.c */