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);
407 for (unsigned int i=0;i<rd_len;i++)
409 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
411 GNUNET_GNS_MASTERZONE_STR)) )
413 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
414 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
420 _("\tCorrupt or unsupported record of type %u\n"),
421 (unsigned int) rd[i].record_type);
424 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
426 rt.rel_value_us = rd[i].expiration_time;
427 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
431 at.abs_value_us = rd[i].expiration_time;
432 ets = GNUNET_STRINGS_absolute_time_to_string (at);
435 "\t%s: %s (%s)\t%s\t%s\n",
439 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
440 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "");
443 FPRINTF (stdout, "%s", "\n");
444 GNUNET_NAMESTORE_zone_iterator_next (list_it);
449 * Function called once we are in sync in monitor mode.
459 "Monitor is now in sync.\n");
464 * Function called on errors while monitoring.
469 monitor_error_cb (void *cls)
474 "Monitor disconnected and out of sync.\n");
479 * Function called if lookup fails.
482 lookup_error_cb (void *cls)
493 * We're storing a record; this function is given the existing record
494 * so that we can merge the information.
496 * @param cls closure, unused
497 * @param zone_key private key of the zone
498 * @param rec_name name that is being mapped (at most 255 characters long)
499 * @param rd_count number of entries in @a rd array
500 * @param rd array of records with data to store
503 get_existing_record (void *cls,
504 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
505 const char *rec_name,
506 unsigned int rd_count,
507 const struct GNUNET_GNSRECORD_Data *rd)
509 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
510 struct GNUNET_GNSRECORD_Data *rde;
515 if (0 != strcmp (rec_name, name))
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Received %u records for name `%s'\n",
526 for (unsigned int i=0;i<rd_count;i++)
528 switch (rd[i].record_type)
530 case GNUNET_DNSPARSER_TYPE_CNAME:
532 _("A %s record exists already under `%s', no other records can be added.\n"),
538 case GNUNET_GNSRECORD_TYPE_PKEY:
540 _("A %s record exists already under `%s', no other records can be added.\n"),
546 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
548 _("A %s record exists already under `%s', no other records can be added.\n"),
558 case GNUNET_DNSPARSER_TYPE_CNAME:
562 _("Records already exist under `%s', cannot add `%s' record.\n"),
570 case GNUNET_GNSRECORD_TYPE_PKEY:
574 _("Records already exist under `%s', cannot add `%s' record.\n"),
582 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
586 _("Records already exist under `%s', cannot add `%s' record.\n"),
597 sizeof (struct GNUNET_GNSRECORD_Data));
598 GNUNET_memcpy (&rdn[1],
600 rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
603 rde->data_size = data_size;
604 rde->record_type = type;
606 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
608 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
609 if (GNUNET_YES == etime_is_rel)
611 rde->expiration_time = etime_rel.rel_value_us;
612 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
614 else if (GNUNET_NO == etime_is_rel)
615 rde->expiration_time = etime_abs.abs_value_us;
617 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
618 GNUNET_assert (NULL != name);
619 add_qe = GNUNET_NAMESTORE_records_store (ns,
630 * Function called if we encountered an error in zone-to-name.
633 reverse_error_cb (void *cls)
644 * Function called with the result of our attempt to obtain a name for a given
648 * @param zone private key of the zone; NULL on disconnect
649 * @param label label of the records; NULL on disconnect
650 * @param rd_count number of entries in @a rd array, 0 if label was deleted
651 * @param rd array of records with data to store
654 handle_reverse_lookup (void *cls,
655 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
657 unsigned int rd_count,
658 const struct GNUNET_GNSRECORD_Data *rd)
676 * Function called if lookup for deletion fails.
679 del_lookup_error_cb (void *cls)
690 * We were asked to delete something; this function is called with
691 * the existing records. Now we should determine what should be
692 * deleted and then issue the deletion operation.
695 * @param zone private key of the zone we are deleting from
696 * @param label name of the records we are editing
697 * @param rd_count size of the @a rd array
698 * @param rd existing records
701 del_monitor (void *cls,
702 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
704 unsigned int rd_count,
705 const struct GNUNET_GNSRECORD_Data *rd)
707 struct GNUNET_GNSRECORD_Data rdx[rd_count];
708 unsigned int rd_left;
717 _("There are no records under label `%s' that could be deleted.\n"),
723 if ( (NULL == value) &&
724 (NULL == typestring) )
726 /* delete everything */
727 del_qe = GNUNET_NAMESTORE_records_store (ns,
737 if (NULL != typestring)
738 type = GNUNET_GNSRECORD_typename_to_number (typestring);
740 type = GNUNET_GNSRECORD_TYPE_ANY;
741 for (unsigned int i=0;i<rd_count;i++)
744 if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
745 (rd[i].record_type == type) ) &&
747 (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
749 rd[i].data_size)))) ||
750 (0 == strcmp (vs, value)) ) ) )
751 rdx[rd_left++] = rd[i];
752 GNUNET_free_non_null (vs);
754 if (rd_count == rd_left)
756 /* nothing got deleted */
758 _("There are no records under label `%s' that match the request for deletion.\n"),
763 /* delete everything but what we copied to 'rdx' */
764 del_qe = GNUNET_NAMESTORE_records_store (ns,
775 * Callback invoked from identity service with ego information.
776 * An @a ego of NULL means the ego was not found.
778 * @param cls closure with the configuration
779 * @param ego an ego known to identity service, or NULL
782 identity_cb (void *cls,
783 const struct GNUNET_IDENTITY_Ego *ego)
785 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
786 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
787 struct GNUNET_GNSRECORD_Data rd;
792 if (NULL != ego_name)
795 _("Ego `%s' not known to identity service\n"),
798 GNUNET_SCHEDULER_shutdown ();
802 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
803 GNUNET_free_non_null (ego_name);
806 if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
808 /* nothing more to be done */
810 _("No options given\n"));
811 GNUNET_SCHEDULER_shutdown ();
814 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
817 ns = GNUNET_NAMESTORE_connect (cfg);
820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
821 _("Failed to connect to namestore\n"));
829 _("Missing option `%s' for operation `%s'\n"),
831 GNUNET_SCHEDULER_shutdown ();
835 if (NULL == typestring)
838 _("Missing option `%s' for operation `%s'\n"),
840 GNUNET_SCHEDULER_shutdown ();
844 type = GNUNET_GNSRECORD_typename_to_number (typestring);
845 if (UINT32_MAX == type)
848 _("Unsupported type `%s'\n"),
850 GNUNET_SCHEDULER_shutdown ();
857 _("Missing option `%s' for operation `%s'\n"),
860 GNUNET_SCHEDULER_shutdown ();
864 GNUNET_GNSRECORD_string_to_value (type,
870 _("Value `%s' invalid for record type `%s'\n"),
873 GNUNET_SCHEDULER_shutdown ();
877 if (NULL == expirationstring)
880 _("Missing option `%s' for operation `%s'\n"),
883 GNUNET_SCHEDULER_shutdown ();
887 if (0 == strcmp (expirationstring,
890 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
891 etime_is_rel = GNUNET_NO;
893 else if (GNUNET_OK ==
894 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
897 etime_is_rel = GNUNET_YES;
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "Storing record with relative expiration time of %s\n",
900 GNUNET_STRINGS_relative_time_to_string (etime_rel,
903 else if (GNUNET_OK ==
904 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
907 etime_is_rel = GNUNET_NO;
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "Storing record with absolute expiration time of %s\n",
910 GNUNET_STRINGS_absolute_time_to_string (etime_abs));
915 _("Invalid time format `%s'\n"),
917 GNUNET_SCHEDULER_shutdown ();
921 add_qe = GNUNET_NAMESTORE_records_lookup (ns,
926 &get_existing_record,
934 _("Missing option `%s' for operation `%s'\n"),
936 GNUNET_SCHEDULER_shutdown ();
940 del_qe = GNUNET_NAMESTORE_records_lookup (ns,
943 &del_lookup_error_cb,
950 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
952 &zone_iteration_error_cb,
956 &zone_iteration_finished,
959 if (NULL != reverse_pkey)
961 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
964 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
965 strlen (reverse_pkey),
969 _("Invalid public key for reverse lookup `%s'\n"),
971 GNUNET_SCHEDULER_shutdown ();
973 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
978 &handle_reverse_lookup,
985 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
987 GNUNET_STRINGS_utf8_tolower (uri, uri);
988 if ( (2 != (sscanf (uri,
989 "gnunet://gns/%52s/%63s",
993 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh,
998 _("Invalid URI `%s'\n"),
1000 GNUNET_SCHEDULER_shutdown ();
1004 memset (&rd, 0, sizeof (rd));
1006 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1007 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
1008 if (GNUNET_YES == etime_is_rel)
1010 rd.expiration_time = etime_rel.rel_value_us;
1011 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1013 else if (GNUNET_NO == etime_is_rel)
1014 rd.expiration_time = etime_abs.abs_value_us;
1016 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
1019 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
1020 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
1028 if (NULL != nickstring)
1030 if (0 == strlen(nickstring))
1033 _("Invalid nick `%s'\n"),
1035 GNUNET_SCHEDULER_shutdown ();
1039 add_qe_uri = GNUNET_NAMESTORE_set_nick (ns,
1047 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
1061 default_ego_cb (void *cls,
1062 struct GNUNET_IDENTITY_Ego *ego,
1073 _("No default ego configured in identity service\n"));
1074 GNUNET_SCHEDULER_shutdown ();
1080 identity_cb (cls, ego);
1086 id_connect_cb (void *cls,
1087 struct GNUNET_IDENTITY_Ego *ego,
1091 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1095 get_default = GNUNET_IDENTITY_get (idh,
1104 * Main function that will be run.
1106 * @param cls closure
1107 * @param args remaining command-line arguments
1108 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1109 * @param cfg configuration
1114 const char *cfgfile,
1115 const struct GNUNET_CONFIGURATION_Handle *cfg)
1120 if ( (NULL != args[0]) &&
1122 uri = GNUNET_strdup (args[0]);
1124 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
1127 if (NULL == ego_name)
1129 idh = GNUNET_IDENTITY_connect (cfg,
1134 _("Cannot connect to identity service\n"));
1138 el = GNUNET_IDENTITY_ego_lookup (cfg,
1146 * The main function for gnunet-namestore.
1148 * @param argc number of arguments from the command line
1149 * @param argv command line arguments
1150 * @return 0 ok, 1 on error
1156 struct GNUNET_GETOPT_CommandLineOption options[] = {
1157 GNUNET_GETOPT_option_flag ('a',
1159 gettext_noop ("add record"),
1161 GNUNET_GETOPT_option_flag ('d',
1163 gettext_noop ("delete record"),
1165 GNUNET_GETOPT_option_flag ('D',
1167 gettext_noop ("display records"),
1169 GNUNET_GETOPT_option_string ('e',
1172 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"),
1174 GNUNET_GETOPT_option_string ('i',
1177 gettext_noop ("set the desired nick name for the zone"),
1179 GNUNET_GETOPT_option_flag ('m',
1181 gettext_noop ("monitor changes in the namestore"),
1183 GNUNET_GETOPT_option_string ('n',
1186 gettext_noop ("name of the record to add/delete/display"),
1188 GNUNET_GETOPT_option_string ('r',
1191 gettext_noop ("determine our name for the given PKEY"),
1193 GNUNET_GETOPT_option_string ('t',
1196 gettext_noop ("type of the record to add/delete/display"),
1198 GNUNET_GETOPT_option_string ('u',
1201 gettext_noop ("URI to import into our zone"),
1203 GNUNET_GETOPT_option_string ('V',
1206 gettext_noop ("value of the record to add/delete"),
1208 GNUNET_GETOPT_option_flag ('p',
1210 gettext_noop ("create or list public record"),
1212 GNUNET_GETOPT_option_flag ('s',
1214 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"),
1216 GNUNET_GETOPT_option_string ('z',
1219 gettext_noop ("name of the ego controlling the zone"),
1221 GNUNET_GETOPT_OPTION_END
1225 GNUNET_STRINGS_get_utf8_args (argc, argv,
1231 GNUNET_log_setup ("gnunet-namestore",
1235 GNUNET_PROGRAM_run (argc,
1238 _("GNUnet zone manipulation tool"),
1242 GNUNET_free ((void*) argv);
1243 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1246 GNUNET_free ((void*) argv);
1247 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1251 /* end of gnunet-namestore.c */