2 This file is part of GNUnet.
3 (C) 2012 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file gnunet-namestore.c
22 * @brief command line tool to manipulate the local zone
23 * @author Christian Grothoff
26 * - allow users to set record options (not just 'RF_AUTHORITY')
28 * - add options to list/lookup individual records
31 #include <gnunet_util_lib.h>
32 #include <gnunet_dnsparser_lib.h>
33 #include <gnunet_namestore_service.h>
37 * Hostkey generation context
39 struct GNUNET_CRYPTO_EccKeyGenerationContext * keygen;
42 * Handle to the namestore.
44 static struct GNUNET_NAMESTORE_Handle *ns;
47 * Hash of the public key of our zone.
49 static struct GNUNET_CRYPTO_ShortHashCode zone;
52 * Private key for the our zone.
54 static struct GNUNET_CRYPTO_EccPrivateKey *zone_pkey;
57 * Keyfile to manipulate.
62 * Desired action is to add a record.
67 * Queue entry for the 'add' operation.
69 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
72 * Queue entry for the 'add-uri' operation.
74 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
77 * Desired action is to list records.
82 * List iterator for the 'list' operation.
84 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
87 * Desired action is to remove a record.
99 static int nonauthority;
102 * Queue entry for the 'del' operation.
104 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
107 * Name of the records to add/list/remove.
112 * Value of the record to add/remove.
122 * Type of the record to add/remove, NULL to remove all.
124 static char *typestring;
127 * Desired expiration time.
129 static char *expirationstring;
132 * Global return value
137 * Type string converted to DNS type value.
139 static uint32_t type;
142 * Value in binary format.
147 * Number of bytes in 'data'.
149 static size_t data_size;
152 * Expirationstring converted to relative time.
154 static struct GNUNET_TIME_Relative etime_rel;
157 * Expirationstring converted to absolute time.
159 static struct GNUNET_TIME_Absolute etime_abs;
162 * Is expiration time relative or absolute time?
164 static int etime_is_rel = GNUNET_SYSERR;
169 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
172 * Enables monitor mode.
177 * Task run on shutdown. Cleans up everything.
180 * @param tc scheduler context
183 do_shutdown (void *cls,
184 const struct GNUNET_SCHEDULER_TaskContext *tc)
188 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
194 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
199 GNUNET_NAMESTORE_cancel (add_qe);
202 if (NULL != add_qe_uri)
204 GNUNET_NAMESTORE_cancel (add_qe_uri);
209 GNUNET_NAMESTORE_cancel (del_qe);
214 GNUNET_NAMESTORE_disconnect (ns);
217 if (NULL != zone_pkey)
219 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
229 GNUNET_NAMESTORE_zone_monitor_stop (zm);
236 * Continuation called to notify client about result of the
239 * @param cls closure, location of the QueueEntry pointer to NULL out
240 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
241 * GNUNET_NO if content was already there
242 * GNUNET_YES (or other positive value) on success
243 * @param emsg NULL on success, otherwise an error message
246 add_continuation (void *cls,
250 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
253 if (GNUNET_YES != success)
256 _("Adding record failed: %s\n"),
257 (GNUNET_NO == success) ? "record exists" : emsg);
258 if (GNUNET_NO != success)
261 if ( (NULL == add_qe) &&
262 (NULL == add_qe_uri) &&
265 GNUNET_SCHEDULER_shutdown ();
270 * Continuation called to notify client about result of the
273 * @param cls closure, unused
274 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
275 * GNUNET_NO if content was already there
276 * GNUNET_YES (or other positive value) on success
277 * @param emsg NULL on success, otherwise an error message
280 del_continuation (void *cls,
285 if (success != GNUNET_YES)
287 _("Deleting record failed: %s\n"),
289 if ( (NULL == add_qe) &&
290 (NULL == add_qe_uri) &&
292 GNUNET_SCHEDULER_shutdown ();
297 * Process a record that was stored in the namestore.
300 * @param zone_key public key of the zone
301 * @param expire when does the corresponding block in the DHT expire (until
302 * when should we never do a DHT lookup for the same name again)?;
303 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
304 * or the expiration time of the block in the namestore (even if there are zero
305 * records matching the desired record type)
306 * @param name name that is being mapped (at most 255 characters long)
307 * @param rd_len number of entries in 'rd' array
308 * @param rd array of records with data to store
309 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
310 * because the user queried for a particular record type only)
313 display_record (void *cls,
314 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
315 struct GNUNET_TIME_Absolute expire,
318 const struct GNUNET_NAMESTORE_RecordData *rd,
319 const struct GNUNET_CRYPTO_EccSignature *signature)
321 const char *typestring;
325 struct GNUNET_TIME_Absolute aex;
326 struct GNUNET_TIME_Relative rex;
331 if ( (NULL == del_qe) &&
332 (NULL == add_qe_uri) &&
334 GNUNET_SCHEDULER_shutdown ();
340 for (i=0;i<rd_len;i++)
342 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
343 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
348 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
349 (unsigned int) rd[i].record_type);
352 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
354 rex.rel_value = rd[i].expiration_time;
355 etime = GNUNET_STRINGS_relative_time_to_string (rex, GNUNET_YES);
359 aex.abs_value = rd[i].expiration_time;
360 etime = GNUNET_STRINGS_absolute_time_to_string (aex);
362 FPRINTF (stdout, "\t%s: %s (%s %s)\n", typestring, s,
363 (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
364 ? _(/* what follows is relative expiration */ "for at least")
365 : _(/* what follows is absolute expiration */ "until"),
369 FPRINTF (stdout, "%s", "\n");
370 GNUNET_NAMESTORE_zone_iterator_next (list_it);
375 * Function called once we are in sync in monitor mode.
382 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
387 * We're storing a record; this function is given the existing record
388 * so that we can merge the information.
390 * @param cls closure, unused
391 * @param zone_key public key of the zone
392 * @param freshness when does the corresponding block in the DHT expire (until
393 * when should we never do a DHT lookup for the same name again)?;
394 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
395 * or the expiration time of the block in the namestore (even if there are zero
396 * records matching the desired record type)
397 * @param name name that is being mapped (at most 255 characters long)
398 * @param rd_count number of entries in 'rd' array
399 * @param rd array of records with data to store
400 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
401 * because the user queried for a particular record type only)
404 get_existing_record (void *cls,
405 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
406 struct GNUNET_TIME_Absolute freshness,
408 unsigned int rd_count,
409 const struct GNUNET_NAMESTORE_RecordData *rd,
410 const struct GNUNET_CRYPTO_EccSignature *signature)
412 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
413 struct GNUNET_NAMESTORE_RecordData *rde;
416 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
417 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
418 /* FIXME: should add some logic to overwrite records if there
419 can only be one record of a particular type, and to check
420 if the combination of records is valid to begin with... */
423 rde->data_size = data_size;
424 rde->record_type = type;
425 if (GNUNET_YES == etime_is_rel)
427 rde->expiration_time = etime_rel.rel_value;
428 rde->flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
430 else if (GNUNET_NO == etime_is_rel)
432 rde->expiration_time = etime_abs.abs_value;
434 if (1 != nonauthority)
435 rde->flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
437 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
439 add_qe = GNUNET_NAMESTORE_record_put_by_authority (ns,
450 * Function called with the result of the ECC key generation.
452 * @param cls our configuration
453 * @param pk our private key, NULL on failure
454 * @param emsg NULL on success, otherwise error message
457 key_generation_cb (void *cls,
458 struct GNUNET_CRYPTO_EccPrivateKey *pk,
461 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
462 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
463 struct GNUNET_NAMESTORE_RecordData rd;
468 GNUNET_SCHEDULER_shutdown ();
473 if (! (add|del|list|(NULL != uri)))
475 /* nothing more to be done */
477 _("No options given\n"));
478 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
482 if (NULL == zone_pkey)
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
485 _("Failed to read or create private zone key\n"));
488 GNUNET_CRYPTO_ecc_key_get_public (zone_pkey,
490 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
492 ns = GNUNET_NAMESTORE_connect (cfg);
495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
496 _("Failed to connect to namestore\n"));
499 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
506 _("Missing option `%s' for operation `%s'\n"),
508 GNUNET_SCHEDULER_shutdown ();
512 if (NULL == typestring)
515 _("Missing option `%s' for operation `%s'\n"),
517 GNUNET_SCHEDULER_shutdown ();
521 type = GNUNET_NAMESTORE_typename_to_number (typestring);
522 if (UINT32_MAX == type)
524 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
525 GNUNET_SCHEDULER_shutdown ();
532 _("Missing option `%s' for operation `%s'\n"),
535 GNUNET_SCHEDULER_shutdown ();
539 GNUNET_NAMESTORE_string_to_value (type,
544 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
547 GNUNET_SCHEDULER_shutdown ();
551 if (NULL == expirationstring)
554 _("Missing option `%s' for operation `%s'\n"),
556 GNUNET_SCHEDULER_shutdown ();
560 if (0 == strcmp (expirationstring, "never"))
562 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
563 etime_is_rel = GNUNET_NO;
565 else if (GNUNET_OK ==
566 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
569 etime_is_rel = GNUNET_YES;
571 else if (GNUNET_OK ==
572 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
575 etime_is_rel = GNUNET_NO;
580 _("Invalid time format `%s'\n"),
582 GNUNET_SCHEDULER_shutdown ();
586 add_qe = GNUNET_NAMESTORE_lookup_record (ns,
590 &get_existing_record,
598 _("Missing option `%s' for operation `%s'\n"),
600 GNUNET_SCHEDULER_shutdown ();
604 del_qe = GNUNET_NAMESTORE_record_put_by_authority (ns,
613 uint32_t must_not_flags = 0;
615 if (1 == nonauthority) /* List non-authority records */
616 must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
619 must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
621 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
623 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION,
632 struct GNUNET_CRYPTO_ShortHashCode sc;
634 if ( (2 != (sscanf (uri,
635 "gnunet://gns/%52s/%63s",
639 GNUNET_CRYPTO_short_hash_from_string (sh, &sc)) )
642 _("Invalid URI `%s'\n"),
644 GNUNET_SCHEDULER_shutdown ();
648 memset (&rd, 0, sizeof (rd));
650 rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
651 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
652 if (GNUNET_YES == etime_is_rel)
654 rd.expiration_time = etime_rel.rel_value;
655 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
657 else if (GNUNET_NO == etime_is_rel)
658 rd.expiration_time = etime_abs.abs_value;
660 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
661 if (1 != nonauthority)
662 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
663 add_qe_uri = GNUNET_NAMESTORE_record_put_by_authority (ns,
671 GNUNET_free_non_null (data);
674 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
684 * Function called with the result from the check if the namestore
685 * service is actually running. If it is, we start the actual
688 * @param cls closure with our configuration
689 * @param result GNUNET_YES if the namestore service is running
692 testservice_task (void *cls,
695 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
697 if (GNUNET_YES != result)
699 FPRINTF (stderr, _("Service `%s' is not running\n"),
705 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
706 "ZONEKEY", &keyfile))
708 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
713 _("Using default zone file `%s'\n"),
716 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
717 &key_generation_cb, (void *) cfg);
718 GNUNET_free (keyfile);
722 GNUNET_SCHEDULER_shutdown ();
729 * Main function that will be run.
732 * @param args remaining command-line arguments
733 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
734 * @param cfg configuration
737 run (void *cls, char *const *args, const char *cfgfile,
738 const struct GNUNET_CONFIGURATION_Handle *cfg)
741 if ( (NULL != args[0]) && (NULL == uri) )
742 uri = GNUNET_strdup (args[0]);
744 GNUNET_CLIENT_service_test ("namestore", cfg,
745 GNUNET_TIME_UNIT_SECONDS,
752 * The main function for gnunet-namestore.
754 * @param argc number of arguments from the command line
755 * @param argv command line arguments
756 * @return 0 ok, 1 on error
759 main (int argc, char *const *argv)
764 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
766 gettext_noop ("add record"), 0,
767 &GNUNET_GETOPT_set_one, &add},
768 {'d', "delete", NULL,
769 gettext_noop ("delete record"), 0,
770 &GNUNET_GETOPT_set_one, &del},
771 {'D', "display", NULL,
772 gettext_noop ("display records"), 0,
773 &GNUNET_GETOPT_set_one, &list},
774 {'e', "expiration", "TIME",
775 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
776 &GNUNET_GETOPT_set_string, &expirationstring},
777 {'m', "monitor", NULL,
778 gettext_noop ("monitor changes in the namestore"), 0,
779 &GNUNET_GETOPT_set_one, &monitor},
780 {'n', "name", "NAME",
781 gettext_noop ("name of the record to add/delete/display"), 1,
782 &GNUNET_GETOPT_set_string, &name},
783 {'t', "type", "TYPE",
784 gettext_noop ("type of the record to add/delete/display"), 1,
785 &GNUNET_GETOPT_set_string, &typestring},
787 gettext_noop ("URI to import into our zone"), 1,
788 &GNUNET_GETOPT_set_string, &uri},
789 {'V', "value", "VALUE",
790 gettext_noop ("value of the record to add/delete"), 1,
791 &GNUNET_GETOPT_set_string, &value},
792 {'p', "public", NULL,
793 gettext_noop ("create or list public record"), 0,
794 &GNUNET_GETOPT_set_one, &public},
795 {'N', "non-authority", NULL,
796 gettext_noop ("create or list non-authority record"), 0,
797 &GNUNET_GETOPT_set_one, &nonauthority},
798 {'z', "zonekey", "FILENAME",
799 gettext_noop ("filename with the zone key"), 1,
800 &GNUNET_GETOPT_set_string, &keyfile},
801 GNUNET_GETOPT_OPTION_END
804 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
807 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
809 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
810 _("GNUnet zone manipulation tool"),
814 GNUNET_free ((void*) argv);
817 GNUNET_free ((void*) argv);
821 /* end of gnunet-namestore.c */