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 'list' operation (in combination with a name).
80 static struct GNUNET_NAMESTORE_QueueEntry *list_qe;
83 * Queue entry for the 'reverse lookup' operation (in combination with a name).
85 static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
88 * Desired action is to list records.
93 * List iterator for the 'list' operation.
95 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
98 * Desired action is to remove a record.
103 * Is record public (opposite of #GNUNET_GNSRECORD_RF_PRIVATE)
108 * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
113 * Queue entry for the 'del' operation.
115 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
118 * Name of the records to add/list/remove.
123 * Value of the record to add/remove.
133 * Reverse lookup to perform.
135 static char *reverse_pkey;
138 * Type of the record to add/remove, NULL to remove all.
140 static char *typestring;
143 * Desired expiration time.
145 static char *expirationstring;
148 * Global return value
153 * Type string converted to DNS type value.
155 static uint32_t type;
158 * Value in binary format.
163 * Number of bytes in 'data'.
165 static size_t data_size;
168 * Expirationstring converted to relative time.
170 static struct GNUNET_TIME_Relative etime_rel;
173 * Expirationstring converted to absolute time.
175 static struct GNUNET_TIME_Absolute etime_abs;
178 * Is expiration time relative or absolute time?
180 static int etime_is_rel = GNUNET_SYSERR;
185 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
188 * Enables monitor mode.
194 * Task run on shutdown. Cleans up everything.
197 * @param tc scheduler context
200 do_shutdown (void *cls,
201 const struct GNUNET_SCHEDULER_TaskContext *tc)
205 GNUNET_IDENTITY_ego_lookup_cancel (el);
210 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
215 GNUNET_NAMESTORE_cancel (add_qe);
220 GNUNET_NAMESTORE_cancel (list_qe);
223 if (NULL != add_qe_uri)
225 GNUNET_NAMESTORE_cancel (add_qe_uri);
230 GNUNET_NAMESTORE_cancel (del_qe);
235 GNUNET_NAMESTORE_disconnect (ns);
238 memset (&zone_pkey, 0, sizeof (zone_pkey));
246 GNUNET_NAMESTORE_zone_monitor_stop (zm);
258 * Check if we are finished, and if so, perform shutdown.
263 if ( (NULL == add_qe) &&
265 (NULL == add_qe_uri) &&
267 (NULL == reverse_qe) &&
269 GNUNET_SCHEDULER_shutdown ();
274 * Continuation called to notify client about result of the
277 * @param cls closure, location of the QueueEntry pointer to NULL out
278 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
279 * #GNUNET_NO if content was already there
280 * #GNUNET_YES (or other positive value) on success
281 * @param emsg NULL on success, otherwise an error message
284 add_continuation (void *cls,
288 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
291 if (GNUNET_YES != success)
294 _("Adding record failed: %s\n"),
295 (GNUNET_NO == success) ? "record exists" : emsg);
296 if (GNUNET_NO != success)
304 * Continuation called to notify client about result of the
307 * @param cls closure, unused
308 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
309 * #GNUNET_NO if content was already there
310 * #GNUNET_YES (or other positive value) on success
311 * @param emsg NULL on success, otherwise an error message
314 del_continuation (void *cls,
319 if (success != GNUNET_YES)
321 _("Deleting record failed: %s\n"),
328 * Process a record that was stored in the namestore.
331 * @param zone_key private key of the zone
332 * @param name name that is being mapped (at most 255 characters long)
333 * @param rd_len number of entries in @a rd array
334 * @param rd array of records with data to store
337 display_record (void *cls,
338 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
341 const struct GNUNET_GNSRECORD_Data *rd)
343 const char *typestring;
347 struct GNUNET_TIME_Absolute at;
348 struct GNUNET_TIME_Relative rt;
359 for (i=0;i<rd_len;i++)
361 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
362 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
367 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
368 (unsigned int) rd[i].record_type);
371 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
373 rt.rel_value_us = rd[i].expiration_time;
374 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
378 at.abs_value_us = rd[i].expiration_time;
379 ets = GNUNET_STRINGS_absolute_time_to_string (at);
388 FPRINTF (stdout, "%s", "\n");
389 GNUNET_NAMESTORE_zone_iterator_next (list_it);
394 * Function called once we are in sync in monitor mode.
401 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
406 * We're storing a record; this function is given the existing record
407 * so that we can merge the information.
409 * @param cls closure, unused
410 * @param zone_key private key of the zone
411 * @param rec_name name that is being mapped (at most 255 characters long)
412 * @param rd_count number of entries in @a rd array
413 * @param rd array of records with data to store
416 get_existing_record (void *cls,
417 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
418 const char *rec_name,
419 unsigned int rd_count,
420 const struct GNUNET_GNSRECORD_Data *rd)
422 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
423 struct GNUNET_GNSRECORD_Data *rde;
425 if ( (NULL != zone_key) &&
426 (0 != strcmp (rec_name, name)) )
428 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
431 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
432 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
433 /* FIXME: should add some logic to overwrite records if there
434 can only be one record of a particular type, and to check
435 if the combination of records is valid to begin with... */
438 rde->data_size = data_size;
439 rde->record_type = type;
441 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
443 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
444 if (GNUNET_YES == etime_is_rel)
446 rde->expiration_time = etime_rel.rel_value_us;
447 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
449 else if (GNUNET_NO == etime_is_rel)
450 rde->expiration_time = etime_abs.abs_value_us;
452 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
453 GNUNET_assert (NULL != name);
454 add_qe = GNUNET_NAMESTORE_records_store (ns,
461 /* only cancel if we were not told that this
462 was the end of the iteration already */
463 if (NULL != rec_name)
464 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
471 * Process a record that was stored in the namestore in a block.
473 * @param cls closure, NULL
474 * @param rd_len number of entries in @a rd array
475 * @param rd array of records with data to store
478 display_records_from_block (void *cls,
480 const struct GNUNET_GNSRECORD_Data *rd)
482 const char *typestring;
489 _("No records found for `%s'"),
496 for (i=0;i<rd_len;i++)
498 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
499 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
504 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
505 (unsigned int) rd[i].record_type);
514 FPRINTF (stdout, "%s", "\n");
519 * Display block obtained from listing (by name).
522 * @param block NULL if not found
525 handle_block (void *cls,
526 const struct GNUNET_GNSRECORD_Block *block)
528 struct GNUNET_CRYPTO_EcdsaPublicKey zone_pubkey;
531 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
536 "No matching block found\n");
538 else if (GNUNET_OK !=
539 GNUNET_GNSRECORD_block_decrypt (block,
542 &display_records_from_block,
546 "Failed to decrypt block!\n");
553 * Function called with the result of our attempt to obtain a name for a given
557 * @param zone private key of the zone; NULL on disconnect
558 * @param label label of the records; NULL on disconnect
559 * @param rd_count number of entries in @a rd array, 0 if label was deleted
560 * @param rd array of records with data to store
563 handle_reverse_lookup (void *cls,
564 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
566 unsigned int rd_count,
567 const struct GNUNET_GNSRECORD_Data *rd)
583 * Function called with the result from the check if the namestore
584 * service is actually running. If it is, we start the actual
587 * @param cls closure with our configuration
588 * @param result #GNUNET_YES if the namestore service is running
591 testservice_task (void *cls,
594 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
595 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
596 struct GNUNET_GNSRECORD_Data rd;
598 if (GNUNET_YES != result)
600 FPRINTF (stderr, _("Service `%s' is not running\n"),
604 if (! (add|del|list|(NULL != uri)|(NULL != reverse_pkey)) )
606 /* nothing more to be done */
608 _("No options given\n"));
609 GNUNET_SCHEDULER_shutdown ();
612 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
615 ns = GNUNET_NAMESTORE_connect (cfg);
618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619 _("Failed to connect to namestore\n"));
627 _("Missing option `%s' for operation `%s'\n"),
629 GNUNET_SCHEDULER_shutdown ();
633 if (NULL == typestring)
636 _("Missing option `%s' for operation `%s'\n"),
638 GNUNET_SCHEDULER_shutdown ();
642 type = GNUNET_GNSRECORD_typename_to_number (typestring);
643 if (UINT32_MAX == type)
645 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
646 GNUNET_SCHEDULER_shutdown ();
653 _("Missing option `%s' for operation `%s'\n"),
656 GNUNET_SCHEDULER_shutdown ();
660 GNUNET_GNSRECORD_string_to_value (type,
665 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
668 GNUNET_SCHEDULER_shutdown ();
672 if (NULL == expirationstring)
675 _("Missing option `%s' for operation `%s'\n"),
677 GNUNET_SCHEDULER_shutdown ();
681 if (0 == strcmp (expirationstring, "never"))
683 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
684 etime_is_rel = GNUNET_NO;
686 else if (GNUNET_OK ==
687 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
690 etime_is_rel = GNUNET_YES;
692 else if (GNUNET_OK ==
693 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
696 etime_is_rel = GNUNET_NO;
701 _("Invalid time format `%s'\n"),
703 GNUNET_SCHEDULER_shutdown ();
707 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
709 &get_existing_record,
717 _("Missing option `%s' for operation `%s'\n"),
719 GNUNET_SCHEDULER_shutdown ();
723 del_qe = GNUNET_NAMESTORE_records_store (ns,
734 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
741 struct GNUNET_HashCode query;
742 struct GNUNET_CRYPTO_EcdsaPublicKey zone_pubkey;
744 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
746 GNUNET_GNSRECORD_query_from_public_key (&zone_pubkey,
749 list_qe = GNUNET_NAMESTORE_lookup_block (ns,
755 if (NULL != reverse_pkey)
757 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
760 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
761 strlen (reverse_pkey),
765 _("Invalid public key for reverse lookup `%s'\n"),
767 GNUNET_SCHEDULER_shutdown ();
769 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
772 &handle_reverse_lookup,
779 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
781 if ( (2 != (sscanf (uri,
782 "gnunet://gns/%104s/%63s",
786 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
789 _("Invalid URI `%s'\n"),
791 GNUNET_SCHEDULER_shutdown ();
795 memset (&rd, 0, sizeof (rd));
797 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
798 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
799 if (GNUNET_YES == etime_is_rel)
801 rd.expiration_time = etime_rel.rel_value_us;
802 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
804 else if (GNUNET_NO == etime_is_rel)
805 rd.expiration_time = etime_abs.abs_value_us;
807 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
809 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
810 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
820 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
830 * Callback invoked from identity service with ego information.
831 * An @a ego of NULL means the ego was not found.
833 * @param cls closure with the configuration
834 * @param ego an ego known to identity service, or NULL
837 identity_cb (void *cls,
838 const struct GNUNET_IDENTITY_Ego *ego)
840 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
846 _("Ego `%s' not known to identity service\n"),
848 GNUNET_SCHEDULER_shutdown ();
851 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
852 GNUNET_free (ego_name);
854 GNUNET_CLIENT_service_test ("namestore", cfg,
855 GNUNET_TIME_UNIT_SECONDS,
862 * Main function that will be run.
865 * @param args remaining command-line arguments
866 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
867 * @param cfg configuration
870 run (void *cls, char *const *args, const char *cfgfile,
871 const struct GNUNET_CONFIGURATION_Handle *cfg)
873 if (NULL == ego_name)
876 _("You must specify which zone should be accessed\n"));
879 if ( (NULL != args[0]) && (NULL == uri) )
880 uri = GNUNET_strdup (args[0]);
881 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
883 el = GNUNET_IDENTITY_ego_lookup (cfg,
891 * The main function for gnunet-namestore.
893 * @param argc number of arguments from the command line
894 * @param argv command line arguments
895 * @return 0 ok, 1 on error
898 main (int argc, char *const *argv)
902 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
904 gettext_noop ("add record"), 0,
905 &GNUNET_GETOPT_set_one, &add},
906 {'d', "delete", NULL,
907 gettext_noop ("delete record"), 0,
908 &GNUNET_GETOPT_set_one, &del},
909 {'D', "display", NULL,
910 gettext_noop ("display records"), 0,
911 &GNUNET_GETOPT_set_one, &list},
912 {'e', "expiration", "TIME",
913 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
914 &GNUNET_GETOPT_set_string, &expirationstring},
915 {'m', "monitor", NULL,
916 gettext_noop ("monitor changes in the namestore"), 0,
917 &GNUNET_GETOPT_set_one, &monitor},
918 {'n', "name", "NAME",
919 gettext_noop ("name of the record to add/delete/display"), 1,
920 &GNUNET_GETOPT_set_string, &name},
921 {'r', "reverse", "PKEY",
922 gettext_noop ("determine our name for the given PKEY"), 1,
923 &GNUNET_GETOPT_set_string, &reverse_pkey},
924 {'t', "type", "TYPE",
925 gettext_noop ("type of the record to add/delete/display"), 1,
926 &GNUNET_GETOPT_set_string, &typestring},
928 gettext_noop ("URI to import into our zone"), 1,
929 &GNUNET_GETOPT_set_string, &uri},
930 {'V', "value", "VALUE",
931 gettext_noop ("value of the record to add/delete"), 1,
932 &GNUNET_GETOPT_set_string, &value},
933 {'p', "public", NULL,
934 gettext_noop ("create or list public record"), 0,
935 &GNUNET_GETOPT_set_one, &public},
936 {'s', "shadow", NULL,
937 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
938 &GNUNET_GETOPT_set_one, &shadow},
940 gettext_noop ("name of the ego controlling the zone"), 1,
941 &GNUNET_GETOPT_set_string, &ego_name},
942 GNUNET_GETOPT_OPTION_END
945 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
948 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
950 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
951 _("GNUnet zone manipulation tool"),
955 GNUNET_free ((void*) argv);
956 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
959 GNUNET_free ((void*) argv);
960 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
964 /* end of gnunet-namestore.c */