3 This file is part of GNUnet.
4 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
22 * @file gnunet-namestore.c
23 * @brief command line tool to manipulate the local zone
24 * @author Christian Grothoff
30 #include <gnunet_util_lib.h>
31 #include <gnunet_dnsparser_lib.h>
32 #include <gnunet_identity_service.h>
33 #include <gnunet_gnsrecord_lib.h>
34 #include <gnunet_namestore_service.h>
38 * Handle to the namestore.
40 static struct GNUNET_NAMESTORE_Handle *ns;
43 * Private key for the our zone.
45 static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
48 * Handle to identity lookup.
50 static struct GNUNET_IDENTITY_EgoLookup *el;
53 * Name of the ego controlling the zone.
55 static char *ego_name;
58 * Desired action is to add a record.
63 * Iterator for the 'add' operation.
65 static struct GNUNET_NAMESTORE_ZoneIterator *add_zit;
68 * Queue entry for the 'add-uri' operation.
70 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
73 * Queue entry for the 'add' operation.
75 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
78 * Queue entry for the 'reverse lookup' operation (in combination with a name).
80 static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
83 * Desired action is to list records.
88 * List iterator for the 'list' operation.
90 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
93 * Desired action is to remove a record.
98 * Is record public (opposite of #GNUNET_GNSRECORD_RF_PRIVATE)
103 * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
108 * Queue entry for the 'del' operation.
110 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
113 * Name of the records to add/list/remove.
118 * Value of the record to add/remove.
128 * Reverse lookup to perform.
130 static char *reverse_pkey;
133 * Type of the record to add/remove, NULL to remove all.
135 static char *typestring;
138 * Desired expiration time.
140 static char *expirationstring;
143 * Global return value
148 * Type string converted to DNS type value.
150 static uint32_t type;
153 * Value in binary format.
158 * Number of bytes in 'data'.
160 static size_t data_size;
163 * Expirationstring converted to relative time.
165 static struct GNUNET_TIME_Relative etime_rel;
168 * Expirationstring converted to absolute time.
170 static struct GNUNET_TIME_Absolute etime_abs;
173 * Is expiration time relative or absolute time?
175 static int etime_is_rel = GNUNET_SYSERR;
180 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
183 * Enables monitor mode.
189 * Task run on shutdown. Cleans up everything.
192 * @param tc scheduler context
195 do_shutdown (void *cls,
196 const struct GNUNET_SCHEDULER_TaskContext *tc)
200 GNUNET_IDENTITY_ego_lookup_cancel (el);
205 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
210 GNUNET_NAMESTORE_cancel (add_qe);
213 if (NULL != add_qe_uri)
215 GNUNET_NAMESTORE_cancel (add_qe_uri);
220 GNUNET_NAMESTORE_cancel (del_qe);
225 GNUNET_NAMESTORE_disconnect (ns);
228 memset (&zone_pkey, 0, sizeof (zone_pkey));
236 GNUNET_NAMESTORE_zone_monitor_stop (zm);
248 * Check if we are finished, and if so, perform shutdown.
253 if ( (NULL == add_qe) &&
254 (NULL == add_qe_uri) &&
256 (NULL == reverse_qe) &&
258 GNUNET_SCHEDULER_shutdown ();
263 * Continuation called to notify client about result of the
266 * @param cls closure, location of the QueueEntry pointer to NULL out
267 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
268 * #GNUNET_NO if content was already there
269 * #GNUNET_YES (or other positive value) on success
270 * @param emsg NULL on success, otherwise an error message
273 add_continuation (void *cls,
277 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
280 if (GNUNET_YES != success)
283 _("Adding record failed: %s\n"),
284 (GNUNET_NO == success) ? "record exists" : emsg);
285 if (GNUNET_NO != success)
293 * Continuation called to notify client about result of the
296 * @param cls closure, unused
297 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
298 * #GNUNET_NO if content was already there
299 * #GNUNET_YES (or other positive value) on success
300 * @param emsg NULL on success, otherwise an error message
303 del_continuation (void *cls,
308 if (success != GNUNET_YES)
310 _("Deleting record failed: %s\n"),
317 * Process a record that was stored in the namestore.
320 * @param zone_key private key of the zone
321 * @param rname name that is being mapped (at most 255 characters long)
322 * @param rd_len number of entries in @a rd array
323 * @param rd array of records with data to store
326 display_record (void *cls,
327 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
330 const struct GNUNET_GNSRECORD_Data *rd)
332 const char *typestring;
336 struct GNUNET_TIME_Absolute at;
337 struct GNUNET_TIME_Relative rt;
345 if ( (NULL != name) &&
346 (0 != strcmp (name, rname)) )
348 GNUNET_NAMESTORE_zone_iterator_next (list_it);
354 for (i=0;i<rd_len;i++)
356 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
357 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
362 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
363 (unsigned int) rd[i].record_type);
366 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
368 rt.rel_value_us = rd[i].expiration_time;
369 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
373 at.abs_value_us = rd[i].expiration_time;
374 ets = GNUNET_STRINGS_absolute_time_to_string (at);
383 FPRINTF (stdout, "%s", "\n");
384 GNUNET_NAMESTORE_zone_iterator_next (list_it);
389 * Function called once we are in sync in monitor mode.
396 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
401 * We're storing a record; this function is given the existing record
402 * so that we can merge the information.
404 * @param cls closure, unused
405 * @param zone_key private key of the zone
406 * @param rec_name name that is being mapped (at most 255 characters long)
407 * @param rd_count number of entries in @a rd array
408 * @param rd array of records with data to store
411 get_existing_record (void *cls,
412 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
413 const char *rec_name,
414 unsigned int rd_count,
415 const struct GNUNET_GNSRECORD_Data *rd)
417 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
418 struct GNUNET_GNSRECORD_Data *rde;
420 if ( (NULL != zone_key) &&
421 (0 != strcmp (rec_name, name)) )
423 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
426 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
427 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
428 /* FIXME: should add some logic to overwrite records if there
429 can only be one record of a particular type, and to check
430 if the combination of records is valid to begin with... */
433 rde->data_size = data_size;
434 rde->record_type = type;
436 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
438 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
439 if (GNUNET_YES == etime_is_rel)
441 rde->expiration_time = etime_rel.rel_value_us;
442 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
444 else if (GNUNET_NO == etime_is_rel)
445 rde->expiration_time = etime_abs.abs_value_us;
447 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
448 GNUNET_assert (NULL != name);
449 add_qe = GNUNET_NAMESTORE_records_store (ns,
456 /* only cancel if we were not told that this
457 was the end of the iteration already */
458 if (NULL != rec_name)
459 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
465 * Function called with the result of our attempt to obtain a name for a given
469 * @param zone private key of the zone; NULL on disconnect
470 * @param label label of the records; NULL on disconnect
471 * @param rd_count number of entries in @a rd array, 0 if label was deleted
472 * @param rd array of records with data to store
475 handle_reverse_lookup (void *cls,
476 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
478 unsigned int rd_count,
479 const struct GNUNET_GNSRECORD_Data *rd)
495 * Function called with the result from the check if the namestore
496 * service is actually running. If it is, we start the actual
499 * @param cls closure with our configuration
500 * @param result #GNUNET_YES if the namestore service is running
503 testservice_task (void *cls,
506 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
507 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
508 struct GNUNET_GNSRECORD_Data rd;
510 if (GNUNET_YES != result)
512 FPRINTF (stderr, _("Service `%s' is not running\n"),
516 if (! (add|del|list|(NULL != uri)|(NULL != reverse_pkey)) )
518 /* nothing more to be done */
520 _("No options given\n"));
521 GNUNET_SCHEDULER_shutdown ();
524 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
527 ns = GNUNET_NAMESTORE_connect (cfg);
530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
531 _("Failed to connect to namestore\n"));
539 _("Missing option `%s' for operation `%s'\n"),
541 GNUNET_SCHEDULER_shutdown ();
545 if (NULL == typestring)
548 _("Missing option `%s' for operation `%s'\n"),
550 GNUNET_SCHEDULER_shutdown ();
554 type = GNUNET_GNSRECORD_typename_to_number (typestring);
555 if (UINT32_MAX == type)
557 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
558 GNUNET_SCHEDULER_shutdown ();
565 _("Missing option `%s' for operation `%s'\n"),
568 GNUNET_SCHEDULER_shutdown ();
572 GNUNET_GNSRECORD_string_to_value (type,
577 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
580 GNUNET_SCHEDULER_shutdown ();
584 if (NULL == expirationstring)
587 _("Missing option `%s' for operation `%s'\n"),
589 GNUNET_SCHEDULER_shutdown ();
593 if (0 == strcmp (expirationstring, "never"))
595 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
596 etime_is_rel = GNUNET_NO;
598 else if (GNUNET_OK ==
599 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
602 etime_is_rel = GNUNET_YES;
604 else if (GNUNET_OK ==
605 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
608 etime_is_rel = GNUNET_NO;
613 _("Invalid time format `%s'\n"),
615 GNUNET_SCHEDULER_shutdown ();
619 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
621 &get_existing_record,
629 _("Missing option `%s' for operation `%s'\n"),
631 GNUNET_SCHEDULER_shutdown ();
635 del_qe = GNUNET_NAMESTORE_records_store (ns,
644 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
649 if (NULL != reverse_pkey)
651 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
654 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
655 strlen (reverse_pkey),
659 _("Invalid public key for reverse lookup `%s'\n"),
661 GNUNET_SCHEDULER_shutdown ();
663 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
666 &handle_reverse_lookup,
673 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
675 if ( (2 != (sscanf (uri,
676 "gnunet://gns/%104s/%63s",
680 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
683 _("Invalid URI `%s'\n"),
685 GNUNET_SCHEDULER_shutdown ();
689 memset (&rd, 0, sizeof (rd));
691 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
692 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
693 if (GNUNET_YES == etime_is_rel)
695 rd.expiration_time = etime_rel.rel_value_us;
696 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
698 else if (GNUNET_NO == etime_is_rel)
699 rd.expiration_time = etime_abs.abs_value_us;
701 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
703 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
704 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
714 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
724 * Callback invoked from identity service with ego information.
725 * An @a ego of NULL means the ego was not found.
727 * @param cls closure with the configuration
728 * @param ego an ego known to identity service, or NULL
731 identity_cb (void *cls,
732 const struct GNUNET_IDENTITY_Ego *ego)
734 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
740 _("Ego `%s' not known to identity service\n"),
742 GNUNET_SCHEDULER_shutdown ();
745 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
746 GNUNET_free (ego_name);
748 GNUNET_CLIENT_service_test ("namestore", cfg,
749 GNUNET_TIME_UNIT_SECONDS,
756 * Main function that will be run.
759 * @param args remaining command-line arguments
760 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
761 * @param cfg configuration
764 run (void *cls, char *const *args, const char *cfgfile,
765 const struct GNUNET_CONFIGURATION_Handle *cfg)
767 if (NULL == ego_name)
770 _("You must specify which zone should be accessed\n"));
773 if ( (NULL != args[0]) && (NULL == uri) )
774 uri = GNUNET_strdup (args[0]);
775 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
777 el = GNUNET_IDENTITY_ego_lookup (cfg,
785 * The main function for gnunet-namestore.
787 * @param argc number of arguments from the command line
788 * @param argv command line arguments
789 * @return 0 ok, 1 on error
792 main (int argc, char *const *argv)
796 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
798 gettext_noop ("add record"), 0,
799 &GNUNET_GETOPT_set_one, &add},
800 {'d', "delete", NULL,
801 gettext_noop ("delete record"), 0,
802 &GNUNET_GETOPT_set_one, &del},
803 {'D', "display", NULL,
804 gettext_noop ("display records"), 0,
805 &GNUNET_GETOPT_set_one, &list},
806 {'e', "expiration", "TIME",
807 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
808 &GNUNET_GETOPT_set_string, &expirationstring},
809 {'m', "monitor", NULL,
810 gettext_noop ("monitor changes in the namestore"), 0,
811 &GNUNET_GETOPT_set_one, &monitor},
812 {'n', "name", "NAME",
813 gettext_noop ("name of the record to add/delete/display"), 1,
814 &GNUNET_GETOPT_set_string, &name},
815 {'r', "reverse", "PKEY",
816 gettext_noop ("determine our name for the given PKEY"), 1,
817 &GNUNET_GETOPT_set_string, &reverse_pkey},
818 {'t', "type", "TYPE",
819 gettext_noop ("type of the record to add/delete/display"), 1,
820 &GNUNET_GETOPT_set_string, &typestring},
822 gettext_noop ("URI to import into our zone"), 1,
823 &GNUNET_GETOPT_set_string, &uri},
824 {'V', "value", "VALUE",
825 gettext_noop ("value of the record to add/delete"), 1,
826 &GNUNET_GETOPT_set_string, &value},
827 {'p', "public", NULL,
828 gettext_noop ("create or list public record"), 0,
829 &GNUNET_GETOPT_set_one, &public},
830 {'s', "shadow", NULL,
831 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
832 &GNUNET_GETOPT_set_one, &shadow},
834 gettext_noop ("name of the ego controlling the zone"), 1,
835 &GNUNET_GETOPT_set_string, &ego_name},
836 GNUNET_GETOPT_OPTION_END
839 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
842 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
844 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
845 _("GNUnet zone manipulation tool"),
849 GNUNET_free ((void*) argv);
850 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
853 GNUNET_free ((void*) argv);
854 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
858 /* end of gnunet-namestore.c */