2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014 GNUnet e.V.
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.
204 do_shutdown (void *cls)
207 if (NULL != get_default)
209 GNUNET_IDENTITY_cancel (get_default);
214 GNUNET_IDENTITY_disconnect (idh);
219 GNUNET_IDENTITY_ego_lookup_cancel (el);
224 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
229 GNUNET_NAMESTORE_cancel (add_qe);
232 if (NULL != add_qe_uri)
234 GNUNET_NAMESTORE_cancel (add_qe_uri);
239 GNUNET_NAMESTORE_cancel (del_qe);
244 GNUNET_NAMESTORE_disconnect (ns);
247 memset (&zone_pkey, 0, sizeof (zone_pkey));
255 GNUNET_NAMESTORE_zone_monitor_stop (zm);
267 * Check if we are finished, and if so, perform shutdown.
272 if ( (NULL == add_qe) &&
273 (NULL == add_qe_uri) &&
275 (NULL == reverse_qe) &&
277 GNUNET_SCHEDULER_shutdown ();
282 * Continuation called to notify client about result of the
285 * @param cls closure, location of the QueueEntry pointer to NULL out
286 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
287 * #GNUNET_NO if content was already there
288 * #GNUNET_YES (or other positive value) on success
289 * @param emsg NULL on success, otherwise an error message
292 add_continuation (void *cls,
296 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
299 if (GNUNET_YES != success)
302 _("Adding record failed: %s\n"),
303 (GNUNET_NO == success) ? "record exists" : emsg);
304 if (GNUNET_NO != success)
313 * Continuation called to notify client about result of the
316 * @param cls closure, unused
317 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
318 * #GNUNET_NO if content was already there
319 * #GNUNET_YES (or other positive value) on success
320 * @param emsg NULL on success, otherwise an error message
323 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 * Function called when we are done with a zone iteration.
351 zone_iteration_finished (void *cls)
360 * Function called when we encountered an error in a zone iteration.
363 zone_iteration_error_cb (void *cls)
368 "Error iterating over zone\n");
375 * Process a record that was stored in the namestore.
378 * @param zone_key private key of the zone
379 * @param rname name that is being mapped (at most 255 characters long)
380 * @param rd_len number of entries in @a rd array
381 * @param rd array of records with data to store
384 display_record (void *cls,
385 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
388 const struct GNUNET_GNSRECORD_Data *rd)
390 const char *typestring;
393 struct GNUNET_TIME_Absolute at;
394 struct GNUNET_TIME_Relative rt;
398 if ( (NULL != name) &&
399 (0 != strcmp (name, rname)) )
401 GNUNET_NAMESTORE_zone_iterator_next (list_it,
408 for (unsigned int i=0;i<rd_len;i++)
410 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
412 GNUNET_GNS_EMPTY_LABEL_AT)) )
414 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
415 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
421 _("\tCorrupt or unsupported record of type %u\n"),
422 (unsigned int) rd[i].record_type);
425 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
427 rt.rel_value_us = rd[i].expiration_time;
428 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
432 at.abs_value_us = rd[i].expiration_time;
433 ets = GNUNET_STRINGS_absolute_time_to_string (at);
436 "\t%s: %s (%s)\t%s\t%s\n",
440 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
441 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "");
444 FPRINTF (stdout, "%s", "\n");
445 GNUNET_NAMESTORE_zone_iterator_next (list_it,
451 * Function called once we are in sync in monitor mode.
461 "Monitor is now in sync.\n");
466 * Function called on errors while monitoring.
471 monitor_error_cb (void *cls)
476 "Monitor disconnected and out of sync.\n");
481 * Function called if lookup fails.
484 lookup_error_cb (void *cls)
495 * We're storing a record; this function is given the existing record
496 * so that we can merge the information.
498 * @param cls closure, unused
499 * @param zone_key private key of the zone
500 * @param rec_name name that is being mapped (at most 255 characters long)
501 * @param rd_count number of entries in @a rd array
502 * @param rd array of records with data to store
505 get_existing_record (void *cls,
506 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
507 const char *rec_name,
508 unsigned int rd_count,
509 const struct GNUNET_GNSRECORD_Data *rd)
511 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
512 struct GNUNET_GNSRECORD_Data *rde;
517 if (0 != strcmp (rec_name, name))
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Received %u records for name `%s'\n",
528 for (unsigned int i=0;i<rd_count;i++)
530 switch (rd[i].record_type)
532 case GNUNET_DNSPARSER_TYPE_CNAME:
534 _("A %s record exists already under `%s', no other records can be added.\n"),
540 case GNUNET_GNSRECORD_TYPE_PKEY:
542 _("A %s record exists already under `%s', no other records can be added.\n"),
548 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
550 _("A %s record exists already under `%s', no other records can be added.\n"),
560 case GNUNET_DNSPARSER_TYPE_CNAME:
564 _("Records already exist under `%s', cannot add `%s' record.\n"),
572 case GNUNET_GNSRECORD_TYPE_PKEY:
576 _("Records already exist under `%s', cannot add `%s' record.\n"),
584 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
588 _("Records already exist under `%s', cannot add `%s' record.\n"),
599 sizeof (struct GNUNET_GNSRECORD_Data));
600 GNUNET_memcpy (&rdn[1],
602 rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
605 rde->data_size = data_size;
606 rde->record_type = type;
608 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
610 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
611 if (GNUNET_YES == etime_is_rel)
613 rde->expiration_time = etime_rel.rel_value_us;
614 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
616 else if (GNUNET_NO == etime_is_rel)
617 rde->expiration_time = etime_abs.abs_value_us;
619 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
620 GNUNET_assert (NULL != name);
621 add_qe = GNUNET_NAMESTORE_records_store (ns,
632 * Function called if we encountered an error in zone-to-name.
635 reverse_error_cb (void *cls)
646 * Function called with the result of our attempt to obtain a name for a given
650 * @param zone private key of the zone; NULL on disconnect
651 * @param label label of the records; NULL on disconnect
652 * @param rd_count number of entries in @a rd array, 0 if label was deleted
653 * @param rd array of records with data to store
656 handle_reverse_lookup (void *cls,
657 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
659 unsigned int rd_count,
660 const struct GNUNET_GNSRECORD_Data *rd)
680 * Function called if lookup for deletion fails.
683 del_lookup_error_cb (void *cls)
694 * We were asked to delete something; this function is called with
695 * the existing records. Now we should determine what should be
696 * deleted and then issue the deletion operation.
699 * @param zone private key of the zone we are deleting from
700 * @param label name of the records we are editing
701 * @param rd_count size of the @a rd array
702 * @param rd existing records
705 del_monitor (void *cls,
706 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
708 unsigned int rd_count,
709 const struct GNUNET_GNSRECORD_Data *rd)
711 struct GNUNET_GNSRECORD_Data rdx[rd_count];
712 unsigned int rd_left;
722 _("There are no records under label `%s' that could be deleted.\n"),
728 if ( (NULL == value) &&
729 (NULL == typestring) )
731 /* delete everything */
732 del_qe = GNUNET_NAMESTORE_records_store (ns,
742 if (NULL != typestring)
743 type = GNUNET_GNSRECORD_typename_to_number (typestring);
745 type = GNUNET_GNSRECORD_TYPE_ANY;
746 for (unsigned int i=0;i<rd_count;i++)
749 if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
750 (rd[i].record_type == type) ) &&
752 (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
754 rd[i].data_size)))) ||
755 (0 == strcmp (vs, value)) ) ) )
756 rdx[rd_left++] = rd[i];
757 GNUNET_free_non_null (vs);
759 if (rd_count == rd_left)
761 /* nothing got deleted */
763 _("There are no records under label `%s' that match the request for deletion.\n"),
768 /* delete everything but what we copied to 'rdx' */
769 del_qe = GNUNET_NAMESTORE_records_store (ns,
780 * Callback invoked from identity service with ego information.
781 * An @a ego of NULL means the ego was not found.
783 * @param cls closure with the configuration
784 * @param ego an ego known to identity service, or NULL
787 identity_cb (void *cls,
788 const struct GNUNET_IDENTITY_Ego *ego)
790 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
791 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
792 struct GNUNET_GNSRECORD_Data rd;
797 if (NULL != ego_name)
800 _("Ego `%s' not known to identity service\n"),
803 GNUNET_SCHEDULER_shutdown ();
807 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
808 GNUNET_free_non_null (ego_name);
811 if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
813 /* nothing more to be done */
815 _("No options given\n"));
816 GNUNET_SCHEDULER_shutdown ();
819 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
822 ns = GNUNET_NAMESTORE_connect (cfg);
825 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
826 _("Failed to connect to namestore\n"));
834 _("Missing option `%s' for operation `%s'\n"),
836 GNUNET_SCHEDULER_shutdown ();
840 if (NULL == typestring)
843 _("Missing option `%s' for operation `%s'\n"),
845 GNUNET_SCHEDULER_shutdown ();
849 type = GNUNET_GNSRECORD_typename_to_number (typestring);
850 if (UINT32_MAX == type)
853 _("Unsupported type `%s'\n"),
855 GNUNET_SCHEDULER_shutdown ();
862 _("Missing option `%s' for operation `%s'\n"),
865 GNUNET_SCHEDULER_shutdown ();
869 GNUNET_GNSRECORD_string_to_value (type,
875 _("Value `%s' invalid for record type `%s'\n"),
878 GNUNET_SCHEDULER_shutdown ();
882 if (NULL == expirationstring)
885 _("Missing option `%s' for operation `%s'\n"),
888 GNUNET_SCHEDULER_shutdown ();
892 if (0 == strcmp (expirationstring,
895 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
896 etime_is_rel = GNUNET_NO;
898 else if (GNUNET_OK ==
899 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
902 etime_is_rel = GNUNET_YES;
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Storing record with relative expiration time of %s\n",
905 GNUNET_STRINGS_relative_time_to_string (etime_rel,
908 else if (GNUNET_OK ==
909 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
912 etime_is_rel = GNUNET_NO;
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 "Storing record with absolute expiration time of %s\n",
915 GNUNET_STRINGS_absolute_time_to_string (etime_abs));
920 _("Invalid time format `%s'\n"),
922 GNUNET_SCHEDULER_shutdown ();
926 add_qe = GNUNET_NAMESTORE_records_lookup (ns,
931 &get_existing_record,
939 _("Missing option `%s' for operation `%s'\n"),
941 GNUNET_SCHEDULER_shutdown ();
945 del_qe = GNUNET_NAMESTORE_records_lookup (ns,
948 &del_lookup_error_cb,
955 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
957 &zone_iteration_error_cb,
961 &zone_iteration_finished,
964 if (NULL != reverse_pkey)
966 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
969 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
970 strlen (reverse_pkey),
974 _("Invalid public key for reverse lookup `%s'\n"),
976 GNUNET_SCHEDULER_shutdown ();
978 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
983 &handle_reverse_lookup,
990 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
992 GNUNET_STRINGS_utf8_tolower (uri, uri);
993 if ( (2 != (sscanf (uri,
994 "gnunet://gns/%52s/%63s",
998 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh,
1003 _("Invalid URI `%s'\n"),
1005 GNUNET_SCHEDULER_shutdown ();
1009 memset (&rd, 0, sizeof (rd));
1011 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1012 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
1013 if (GNUNET_YES == etime_is_rel)
1015 rd.expiration_time = etime_rel.rel_value_us;
1016 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1018 else if (GNUNET_NO == etime_is_rel)
1019 rd.expiration_time = etime_abs.abs_value_us;
1021 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1024 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
1025 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
1033 if (NULL != nickstring)
1035 if (0 == strlen(nickstring))
1038 _("Invalid nick `%s'\n"),
1040 GNUNET_SCHEDULER_shutdown ();
1044 add_qe_uri = GNUNET_NAMESTORE_set_nick (ns,
1052 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
1066 default_ego_cb (void *cls,
1067 struct GNUNET_IDENTITY_Ego *ego,
1078 _("No default ego configured in identity service\n"));
1079 GNUNET_SCHEDULER_shutdown ();
1085 identity_cb (cls, ego);
1091 id_connect_cb (void *cls,
1092 struct GNUNET_IDENTITY_Ego *ego,
1096 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1103 get_default = GNUNET_IDENTITY_get (idh,
1112 * Main function that will be run.
1114 * @param cls closure
1115 * @param args remaining command-line arguments
1116 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1117 * @param cfg configuration
1122 const char *cfgfile,
1123 const struct GNUNET_CONFIGURATION_Handle *cfg)
1128 if ( (NULL != args[0]) &&
1130 uri = GNUNET_strdup (args[0]);
1132 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
1135 if (NULL == ego_name)
1137 idh = GNUNET_IDENTITY_connect (cfg,
1142 _("Cannot connect to identity service\n"));
1146 el = GNUNET_IDENTITY_ego_lookup (cfg,
1154 * The main function for gnunet-namestore.
1156 * @param argc number of arguments from the command line
1157 * @param argv command line arguments
1158 * @return 0 ok, 1 on error
1164 struct GNUNET_GETOPT_CommandLineOption options[] = {
1165 GNUNET_GETOPT_option_flag ('a',
1167 gettext_noop ("add record"),
1169 GNUNET_GETOPT_option_flag ('d',
1171 gettext_noop ("delete record"),
1173 GNUNET_GETOPT_option_flag ('D',
1175 gettext_noop ("display records"),
1177 GNUNET_GETOPT_option_string ('e',
1180 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"),
1182 GNUNET_GETOPT_option_string ('i',
1185 gettext_noop ("set the desired nick name for the zone"),
1187 GNUNET_GETOPT_option_flag ('m',
1189 gettext_noop ("monitor changes in the namestore"),
1191 GNUNET_GETOPT_option_string ('n',
1194 gettext_noop ("name of the record to add/delete/display"),
1196 GNUNET_GETOPT_option_string ('r',
1199 gettext_noop ("determine our name for the given PKEY"),
1201 GNUNET_GETOPT_option_string ('t',
1204 gettext_noop ("type of the record to add/delete/display"),
1206 GNUNET_GETOPT_option_string ('u',
1209 gettext_noop ("URI to import into our zone"),
1211 GNUNET_GETOPT_option_string ('V',
1214 gettext_noop ("value of the record to add/delete"),
1216 GNUNET_GETOPT_option_flag ('p',
1218 gettext_noop ("create or list public record"),
1220 GNUNET_GETOPT_option_flag ('s',
1222 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"),
1224 GNUNET_GETOPT_option_string ('z',
1227 gettext_noop ("name of the ego controlling the zone"),
1229 GNUNET_GETOPT_OPTION_END
1233 GNUNET_STRINGS_get_utf8_args (argc, argv,
1239 GNUNET_log_setup ("gnunet-namestore",
1243 GNUNET_PROGRAM_run (argc,
1246 _("GNUnet zone manipulation tool"),
1250 GNUNET_free ((void*) argv);
1251 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1254 GNUNET_free ((void*) argv);
1255 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1259 /* end of gnunet-namestore.c */