2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014 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.
21 * @file gnunet-namestore.c
22 * @brief command line tool to manipulate the local zone
23 * @author Christian Grothoff
29 #include <gnunet_util_lib.h>
30 #include <gnunet_dnsparser_lib.h>
31 #include <gnunet_identity_service.h>
32 #include <gnunet_gnsrecord_lib.h>
33 #include <gnunet_gns_service.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 * Identity service handle
55 static struct GNUNET_IDENTITY_Handle *idh;
60 struct GNUNET_IDENTITY_Operation *get_default;
63 * Name of the ego controlling the zone.
65 static char *ego_name;
68 * Desired action is to add a record.
73 * Queue entry for the 'add-uri' operation.
75 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
78 * Queue entry for the 'add' operation.
80 static struct GNUNET_NAMESTORE_QueueEntry *add_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)
105 static int is_public;
108 * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
110 static int is_shadow;
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;
150 static char *nickstring;
153 * Global return value
158 * Type string converted to DNS type value.
160 static uint32_t type;
163 * Value in binary format.
168 * Number of bytes in 'data'.
170 static size_t data_size;
173 * Expirationstring converted to relative time.
175 static struct GNUNET_TIME_Relative etime_rel;
178 * Expirationstring converted to absolute time.
180 static struct GNUNET_TIME_Absolute etime_abs;
183 * Is expiration time relative or absolute time?
185 static int etime_is_rel = GNUNET_SYSERR;
190 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
193 * Enables monitor mode.
199 * Task run on shutdown. Cleans up everything.
202 * @param tc scheduler context
205 do_shutdown (void *cls,
206 const struct GNUNET_SCHEDULER_TaskContext *tc)
208 if (NULL != get_default)
210 GNUNET_IDENTITY_cancel (get_default);
215 GNUNET_IDENTITY_disconnect (idh);
220 GNUNET_IDENTITY_ego_lookup_cancel (el);
225 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
230 GNUNET_NAMESTORE_cancel (add_qe);
233 if (NULL != add_qe_uri)
235 GNUNET_NAMESTORE_cancel (add_qe_uri);
240 GNUNET_NAMESTORE_cancel (del_qe);
245 GNUNET_NAMESTORE_disconnect (ns);
248 memset (&zone_pkey, 0, sizeof (zone_pkey));
256 GNUNET_NAMESTORE_zone_monitor_stop (zm);
268 * Check if we are finished, and if so, perform shutdown.
273 if ( (NULL == add_qe) &&
274 (NULL == add_qe_uri) &&
276 (NULL == reverse_qe) &&
278 GNUNET_SCHEDULER_shutdown ();
283 * Continuation called to notify client about result of the
286 * @param cls closure, location of the QueueEntry pointer to NULL out
287 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
288 * #GNUNET_NO if content was already there
289 * #GNUNET_YES (or other positive value) on success
290 * @param emsg NULL on success, otherwise an error message
293 add_continuation (void *cls,
297 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
300 if (GNUNET_YES != success)
303 _("Adding record failed: %s\n"),
304 (GNUNET_NO == success) ? "record exists" : emsg);
305 if (GNUNET_NO != success)
314 * Continuation called to notify client about result of the
317 * @param cls closure, unused
318 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
319 * #GNUNET_NO if content was already there
320 * #GNUNET_YES (or other positive value) on success
321 * @param emsg NULL on success, otherwise an error message
324 del_continuation (void *cls,
329 if (GNUNET_NO == success)
332 _("Deleting record failed, record does not exist%s%s\n"),
333 (NULL != emsg) ? ": " : "",
334 (NULL != emsg) ? emsg : "");
336 if (GNUNET_SYSERR == success)
339 _("Deleting record failed%s%s\n"),
340 (NULL != emsg) ? ": " : "",
341 (NULL != emsg) ? emsg : "");
348 * Process a record that was stored in the namestore.
351 * @param zone_key private key of the zone
352 * @param rname name that is being mapped (at most 255 characters long)
353 * @param rd_len number of entries in @a rd array
354 * @param rd array of records with data to store
357 display_record (void *cls,
358 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
361 const struct GNUNET_GNSRECORD_Data *rd)
363 const char *typestring;
367 struct GNUNET_TIME_Absolute at;
368 struct GNUNET_TIME_Relative rt;
376 if ( (NULL != name) &&
377 (0 != strcmp (name, rname)) )
379 GNUNET_NAMESTORE_zone_iterator_next (list_it);
385 for (i=0;i<rd_len;i++)
387 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
391 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
392 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
397 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
398 (unsigned int) rd[i].record_type);
401 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
403 rt.rel_value_us = rd[i].expiration_time;
404 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
408 at.abs_value_us = rd[i].expiration_time;
409 ets = GNUNET_STRINGS_absolute_time_to_string (at);
412 "\t%s: %s (%s)\t%s\t%s\n",
416 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
417 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "");
420 FPRINTF (stdout, "%s", "\n");
421 GNUNET_NAMESTORE_zone_iterator_next (list_it);
426 * Function called once we are in sync in monitor mode.
433 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
438 * We're storing a record; this function is given the existing record
439 * so that we can merge the information.
441 * @param cls closure, unused
442 * @param zone_key private key of the zone
443 * @param rec_name name that is being mapped (at most 255 characters long)
444 * @param rd_count number of entries in @a rd array
445 * @param rd array of records with data to store
448 get_existing_record (void *cls,
449 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
450 const char *rec_name,
451 unsigned int rd_count,
452 const struct GNUNET_GNSRECORD_Data *rd)
454 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
455 struct GNUNET_GNSRECORD_Data *rde;
459 if ( (NULL != zone_key) &&
460 (0 != strcmp (rec_name, name)) )
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469 "Received %u records for name `%s'\n",
471 for (i=0;i<rd_count;i++)
473 switch (rd[i].record_type)
475 case GNUNET_DNSPARSER_TYPE_CNAME:
477 _("A %s record exists already under `%s', no other records can be added.\n"),
483 case GNUNET_GNSRECORD_TYPE_PKEY:
485 _("A %s record exists already under `%s', no other records can be added.\n"),
491 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
493 _("A %s record exists already under `%s', no other records can be added.\n"),
503 case GNUNET_DNSPARSER_TYPE_CNAME:
507 _("Records already exist under `%s', cannot add `%s' record.\n"),
515 case GNUNET_GNSRECORD_TYPE_PKEY:
519 _("Records already exist under `%s', cannot add `%s' record.\n"),
527 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
531 _("Records already exist under `%s', cannot add `%s' record.\n"),
540 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
541 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
544 rde->data_size = data_size;
545 rde->record_type = type;
547 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
549 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
550 if (GNUNET_YES == etime_is_rel)
552 rde->expiration_time = etime_rel.rel_value_us;
553 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
555 else if (GNUNET_NO == etime_is_rel)
556 rde->expiration_time = etime_abs.abs_value_us;
558 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
559 GNUNET_assert (NULL != name);
560 add_qe = GNUNET_NAMESTORE_records_store (ns,
571 * Function called with the result of our attempt to obtain a name for a given
575 * @param zone private key of the zone; NULL on disconnect
576 * @param label label of the records; NULL on disconnect
577 * @param rd_count number of entries in @a rd array, 0 if label was deleted
578 * @param rd array of records with data to store
581 handle_reverse_lookup (void *cls,
582 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
584 unsigned int rd_count,
585 const struct GNUNET_GNSRECORD_Data *rd)
601 * We were asked to delete something; this function is called with
602 * the existing records. Now we should determine what should be
603 * deleted and then issue the deletion operation.
606 * @param zone private key of the zone we are deleting from
607 * @param label name of the records we are editing
608 * @param rd_count size of the @a rd array
609 * @param rd existing records
612 del_monitor (void *cls,
613 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
615 unsigned int rd_count,
616 const struct GNUNET_GNSRECORD_Data *rd)
618 struct GNUNET_GNSRECORD_Data rdx[rd_count];
619 unsigned int rd_left;
628 _("There are no records under label `%s' that could be deleted.\n"),
633 if ( (NULL == value) &&
634 (NULL == typestring) )
636 /* delete everything */
637 del_qe = GNUNET_NAMESTORE_records_store (ns,
646 if (NULL != typestring)
647 type = GNUNET_GNSRECORD_typename_to_number (typestring);
649 type = GNUNET_GNSRECORD_TYPE_ANY;
650 for (i=0;i<rd_count;i++)
653 if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
654 (rd[i].record_type == type) ) &&
656 (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
658 rd[i].data_size)))) ||
659 (0 == strcmp (vs, value)) ) ) )
660 rdx[rd_left++] = rd[i];
661 GNUNET_free_non_null (vs);
663 if (rd_count == rd_left)
665 /* nothing got deleted */
667 _("There are no records under label `%s' that match the request for deletion.\n"),
672 /* delete everything but what we copied to 'rdx' */
673 del_qe = GNUNET_NAMESTORE_records_store (ns,
683 * Function called with the result from the check if the namestore
684 * service is actually running. If it is, we start the actual
687 * @param cls closure with our configuration
688 * @param result #GNUNET_YES if the namestore service is running
691 testservice_task (void *cls,
694 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
695 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
696 struct GNUNET_GNSRECORD_Data rd;
698 if (GNUNET_YES != result)
700 FPRINTF (stderr, _("Service `%s' is not running\n"),
704 if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
706 /* nothing more to be done */
708 _("No options given\n"));
709 GNUNET_SCHEDULER_shutdown ();
712 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
715 ns = GNUNET_NAMESTORE_connect (cfg);
718 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
719 _("Failed to connect to namestore\n"));
727 _("Missing option `%s' for operation `%s'\n"),
729 GNUNET_SCHEDULER_shutdown ();
733 if (NULL == typestring)
736 _("Missing option `%s' for operation `%s'\n"),
738 GNUNET_SCHEDULER_shutdown ();
742 type = GNUNET_GNSRECORD_typename_to_number (typestring);
743 if (UINT32_MAX == type)
745 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
746 GNUNET_SCHEDULER_shutdown ();
753 _("Missing option `%s' for operation `%s'\n"),
756 GNUNET_SCHEDULER_shutdown ();
760 GNUNET_GNSRECORD_string_to_value (type,
765 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
768 GNUNET_SCHEDULER_shutdown ();
772 if (NULL == expirationstring)
775 _("Missing option `%s' for operation `%s'\n"),
777 GNUNET_SCHEDULER_shutdown ();
781 if (0 == strcmp (expirationstring, "never"))
783 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
784 etime_is_rel = GNUNET_NO;
786 else if (GNUNET_OK ==
787 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
790 etime_is_rel = GNUNET_YES;
792 else if (GNUNET_OK ==
793 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
796 etime_is_rel = GNUNET_NO;
801 _("Invalid time format `%s'\n"),
803 GNUNET_SCHEDULER_shutdown ();
807 add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
808 &get_existing_record, NULL );
815 _("Missing option `%s' for operation `%s'\n"),
817 GNUNET_SCHEDULER_shutdown ();
821 del_qe = GNUNET_NAMESTORE_records_lookup (ns,
829 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
834 if (NULL != reverse_pkey)
836 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
839 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
840 strlen (reverse_pkey),
844 _("Invalid public key for reverse lookup `%s'\n"),
846 GNUNET_SCHEDULER_shutdown ();
848 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
851 &handle_reverse_lookup,
858 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
860 GNUNET_STRINGS_utf8_tolower (uri, uri);
861 if ( (2 != (sscanf (uri,
862 "gnunet://gns/%52s/%63s",
865 (GNUNET_OK != GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
868 _("Invalid URI `%s'\n"),
870 GNUNET_SCHEDULER_shutdown ();
874 memset (&rd, 0, sizeof (rd));
876 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
877 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
878 if (GNUNET_YES == etime_is_rel)
880 rd.expiration_time = etime_rel.rel_value_us;
881 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
883 else if (GNUNET_NO == etime_is_rel)
884 rd.expiration_time = etime_abs.abs_value_us;
886 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
889 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
890 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
898 if (NULL != nickstring)
900 if (0 == strlen(nickstring))
903 _("Invalid nick `%s'\n"),
905 GNUNET_SCHEDULER_shutdown ();
909 add_qe_uri = GNUNET_NAMESTORE_set_nick(ns, &zone_pkey, nickstring,
910 &add_continuation, &add_qe_uri);
914 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
925 * Callback invoked from identity service with ego information.
926 * An @a ego of NULL means the ego was not found.
928 * @param cls closure with the configuration
929 * @param ego an ego known to identity service, or NULL
932 identity_cb (void *cls,
933 const struct GNUNET_IDENTITY_Ego *ego)
935 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
940 if (NULL != ego_name)
943 _("Ego `%s' not known to identity service\n"),
946 GNUNET_SCHEDULER_shutdown ();
950 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
951 GNUNET_free_non_null (ego_name);
953 GNUNET_CLIENT_service_test ("namestore", cfg,
954 GNUNET_TIME_UNIT_SECONDS,
961 default_ego_cb (void *cls,
962 struct GNUNET_IDENTITY_Ego *ego,
970 _("No default ego configured in identity service\n"));
971 GNUNET_SCHEDULER_shutdown ();
977 identity_cb (cls, ego);
983 id_connect_cb (void *cls,
984 struct GNUNET_IDENTITY_Ego *ego,
988 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
992 get_default = GNUNET_IDENTITY_get (idh,
994 &default_ego_cb, (void *) cfg);
1000 testservice_id_task (void *cls, int result)
1002 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1003 if (result != GNUNET_YES)
1006 _("Identity service is not running\n"));
1007 GNUNET_SCHEDULER_shutdown ();
1011 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1012 &do_shutdown, (void *) cfg);
1014 if (NULL == ego_name)
1016 idh = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, (void *) cfg);
1018 fprintf (stderr, _("Cannot connect to identity service\n"));
1022 el = GNUNET_IDENTITY_ego_lookup (cfg,
1030 * Main function that will be run.
1032 * @param cls closure
1033 * @param args remaining command-line arguments
1034 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1035 * @param cfg configuration
1038 run (void *cls, char *const *args, const char *cfgfile,
1039 const struct GNUNET_CONFIGURATION_Handle *cfg)
1041 if ( (NULL != args[0]) && (NULL == uri) )
1042 uri = GNUNET_strdup (args[0]);
1044 GNUNET_CLIENT_service_test ("identity", cfg,
1045 GNUNET_TIME_UNIT_SECONDS,
1046 &testservice_id_task,
1052 * The main function for gnunet-namestore.
1054 * @param argc number of arguments from the command line
1055 * @param argv command line arguments
1056 * @return 0 ok, 1 on error
1059 main (int argc, char *const *argv)
1064 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1066 gettext_noop ("add record"), 0,
1067 &GNUNET_GETOPT_set_one, &add},
1068 {'d', "delete", NULL,
1069 gettext_noop ("delete record"), 0,
1070 &GNUNET_GETOPT_set_one, &del},
1071 {'D', "display", NULL,
1072 gettext_noop ("display records"), 0,
1073 &GNUNET_GETOPT_set_one, &list},
1074 {'e', "expiration", "TIME",
1075 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
1076 &GNUNET_GETOPT_set_string, &expirationstring},
1077 {'i', "nick", "NICKNAME",
1078 gettext_noop ("set the desired nick name for the zone"), 1,
1079 &GNUNET_GETOPT_set_string, &nickstring},
1080 {'m', "monitor", NULL,
1081 gettext_noop ("monitor changes in the namestore"), 0,
1082 &GNUNET_GETOPT_set_one, &monitor},
1083 {'n', "name", "NAME",
1084 gettext_noop ("name of the record to add/delete/display"), 1,
1085 &GNUNET_GETOPT_set_string, &name},
1086 {'r', "reverse", "PKEY",
1087 gettext_noop ("determine our name for the given PKEY"), 1,
1088 &GNUNET_GETOPT_set_string, &reverse_pkey},
1089 {'t', "type", "TYPE",
1090 gettext_noop ("type of the record to add/delete/display"), 1,
1091 &GNUNET_GETOPT_set_string, &typestring},
1093 gettext_noop ("URI to import into our zone"), 1,
1094 &GNUNET_GETOPT_set_string, &uri},
1095 {'V', "value", "VALUE",
1096 gettext_noop ("value of the record to add/delete"), 1,
1097 &GNUNET_GETOPT_set_string, &value},
1098 {'p', "public", NULL,
1099 gettext_noop ("create or list public record"), 0,
1100 &GNUNET_GETOPT_set_one, &is_public},
1101 {'s', "shadow", NULL,
1102 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
1103 &GNUNET_GETOPT_set_one, &is_shadow},
1104 {'z', "zone", "EGO",
1105 gettext_noop ("name of the ego controlling the zone"), 1,
1106 &GNUNET_GETOPT_set_string, &ego_name},
1107 GNUNET_GETOPT_OPTION_END
1110 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1113 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
1115 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
1116 _("GNUnet zone manipulation tool"),
1120 GNUNET_free ((void*) argv);
1121 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1124 GNUNET_free ((void*) argv);
1125 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1129 /* end of gnunet-namestore.c */