2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * Is record pending approval (#GNUNET_GNSRECORD_RF_PENDING)
115 static int is_pending;
118 * Queue entry for the 'del' operation.
120 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
123 * Name of the records to add/list/remove.
128 * Value of the record to add/remove.
138 * Reverse lookup to perform.
140 static char *reverse_pkey;
143 * Type of the record to add/remove, NULL to remove all.
145 static char *typestring;
148 * Desired expiration time.
150 static char *expirationstring;
155 static char *nickstring;
158 * Global return value
163 * Type string converted to DNS type value.
165 static uint32_t type;
168 * Value in binary format.
173 * Number of bytes in 'data'.
175 static size_t data_size;
178 * Expirationstring converted to relative time.
180 static struct GNUNET_TIME_Relative etime_rel;
183 * Expirationstring converted to absolute time.
185 static struct GNUNET_TIME_Absolute etime_abs;
188 * Is expiration time relative or absolute time?
190 static int etime_is_rel = GNUNET_SYSERR;
195 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
198 * Enables monitor mode.
204 * Task run on shutdown. Cleans up everything.
207 * @param tc scheduler context
210 do_shutdown (void *cls,
211 const struct GNUNET_SCHEDULER_TaskContext *tc)
213 if (NULL != get_default)
215 GNUNET_IDENTITY_cancel (get_default);
220 GNUNET_IDENTITY_disconnect (idh);
225 GNUNET_IDENTITY_ego_lookup_cancel (el);
230 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
235 GNUNET_NAMESTORE_cancel (add_qe);
238 if (NULL != add_qe_uri)
240 GNUNET_NAMESTORE_cancel (add_qe_uri);
245 GNUNET_NAMESTORE_cancel (del_qe);
250 GNUNET_NAMESTORE_disconnect (ns);
253 memset (&zone_pkey, 0, sizeof (zone_pkey));
261 GNUNET_NAMESTORE_zone_monitor_stop (zm);
273 * Check if we are finished, and if so, perform shutdown.
278 if ( (NULL == add_qe) &&
279 (NULL == add_qe_uri) &&
281 (NULL == reverse_qe) &&
283 GNUNET_SCHEDULER_shutdown ();
288 * Continuation called to notify client about result of the
291 * @param cls closure, location of the QueueEntry pointer to NULL out
292 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
293 * #GNUNET_NO if content was already there
294 * #GNUNET_YES (or other positive value) on success
295 * @param emsg NULL on success, otherwise an error message
298 add_continuation (void *cls,
302 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
305 if (GNUNET_YES != success)
308 _("Adding record failed: %s\n"),
309 (GNUNET_NO == success) ? "record exists" : emsg);
310 if (GNUNET_NO != success)
319 * Continuation called to notify client about result of the
322 * @param cls closure, unused
323 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
324 * #GNUNET_NO if content was already there
325 * #GNUNET_YES (or other positive value) on success
326 * @param emsg NULL on success, otherwise an error message
329 del_continuation (void *cls,
334 if (GNUNET_NO == success)
337 _("Deleting record failed, record does not exist%s%s\n"),
338 (NULL != emsg) ? ": " : "",
339 (NULL != emsg) ? emsg : "");
341 if (GNUNET_SYSERR == success)
344 _("Deleting record failed%s%s\n"),
345 (NULL != emsg) ? ": " : "",
346 (NULL != emsg) ? emsg : "");
353 * Process a record that was stored in the namestore.
356 * @param zone_key private key of the zone
357 * @param rname name that is being mapped (at most 255 characters long)
358 * @param rd_len number of entries in @a rd array
359 * @param rd array of records with data to store
362 display_record (void *cls,
363 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
366 const struct GNUNET_GNSRECORD_Data *rd)
368 const char *typestring;
372 struct GNUNET_TIME_Absolute at;
373 struct GNUNET_TIME_Relative rt;
381 if ( (NULL != name) &&
382 (0 != strcmp (name, rname)) )
384 GNUNET_NAMESTORE_zone_iterator_next (list_it);
390 for (i=0;i<rd_len;i++)
392 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
396 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
397 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
402 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
403 (unsigned int) rd[i].record_type);
406 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
408 rt.rel_value_us = rd[i].expiration_time;
409 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
413 at.abs_value_us = rd[i].expiration_time;
414 ets = GNUNET_STRINGS_absolute_time_to_string (at);
417 "\t%s: %s (%s)\t%s\t%s\t%s\n",
421 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
422 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "",
423 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PENDING)) ? "PENDING" : "");
426 FPRINTF (stdout, "%s", "\n");
427 GNUNET_NAMESTORE_zone_iterator_next (list_it);
432 * Function called once we are in sync in monitor mode.
439 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
444 * We're storing a record; this function is given the existing record
445 * so that we can merge the information.
447 * @param cls closure, unused
448 * @param zone_key private key of the zone
449 * @param rec_name name that is being mapped (at most 255 characters long)
450 * @param rd_count number of entries in @a rd array
451 * @param rd array of records with data to store
454 get_existing_record (void *cls,
455 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
456 const char *rec_name,
457 unsigned int rd_count,
458 const struct GNUNET_GNSRECORD_Data *rd)
460 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
461 struct GNUNET_GNSRECORD_Data *rde;
464 if ( (NULL != zone_key) &&
465 (0 != strcmp (rec_name, name)) )
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u records for name `%s'\n",
474 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
475 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
476 /* FIXME: should add some logic to overwrite records if there
477 can only be one record of a particular type, and to check
478 if the combination of records is valid to begin with... */
481 rde->data_size = data_size;
482 rde->record_type = type;
484 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
486 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
488 rde->flags |= GNUNET_GNSRECORD_RF_PENDING;
489 if (GNUNET_YES == etime_is_rel)
491 rde->expiration_time = etime_rel.rel_value_us;
492 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
494 else if (GNUNET_NO == etime_is_rel)
495 rde->expiration_time = etime_abs.abs_value_us;
497 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
498 GNUNET_assert (NULL != name);
499 add_qe = GNUNET_NAMESTORE_records_store (ns,
510 * Function called with the result of our attempt to obtain a name for a given
514 * @param zone private key of the zone; NULL on disconnect
515 * @param label label of the records; NULL on disconnect
516 * @param rd_count number of entries in @a rd array, 0 if label was deleted
517 * @param rd array of records with data to store
520 handle_reverse_lookup (void *cls,
521 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
523 unsigned int rd_count,
524 const struct GNUNET_GNSRECORD_Data *rd)
540 * We were asked to delete something; this function is called with
541 * the existing records. Now we should determine what should be
542 * deleted and then issue the deletion operation.
545 * @param zone private key of the zone we are deleting from
546 * @param label name of the records we are editing
547 * @param rd_count size of the @a rd array
548 * @param rd existing records
551 del_monitor (void *cls,
552 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
554 unsigned int rd_count,
555 const struct GNUNET_GNSRECORD_Data *rd)
557 struct GNUNET_GNSRECORD_Data rdx[rd_count];
558 unsigned int rd_left;
567 _("There are no records under label `%s' that could be deleted.\n"),
572 if ( (NULL == value) &&
573 (NULL == typestring) )
575 /* delete everything */
576 del_qe = GNUNET_NAMESTORE_records_store (ns,
585 if (NULL != typestring)
586 type = GNUNET_GNSRECORD_typename_to_number (typestring);
588 type = GNUNET_GNSRECORD_TYPE_ANY;
589 for (i=0;i<rd_count;i++)
592 if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
593 (rd[i].record_type == type) ) &&
595 (NULL == (vs = (GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
597 rd[i].data_size)))) ||
598 (0 == strcmp (vs, value)) ) ) )
599 rdx[rd_left++] = rd[i];
600 GNUNET_free_non_null (vs);
602 if (rd_count == rd_left)
604 /* nothing got deleted */
606 _("There are no records under label `%s' that match the request for deletion.\n"),
611 /* delete everything but what we copied to 'rdx' */
612 del_qe = GNUNET_NAMESTORE_records_store (ns,
622 * Function called with the result from the check if the namestore
623 * service is actually running. If it is, we start the actual
626 * @param cls closure with our configuration
627 * @param result #GNUNET_YES if the namestore service is running
630 testservice_task (void *cls,
633 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
634 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
635 struct GNUNET_GNSRECORD_Data rd;
637 if (GNUNET_YES != result)
639 FPRINTF (stderr, _("Service `%s' is not running\n"),
643 if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
645 /* nothing more to be done */
647 _("No options given\n"));
648 GNUNET_SCHEDULER_shutdown ();
651 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
654 ns = GNUNET_NAMESTORE_connect (cfg);
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
658 _("Failed to connect to namestore\n"));
666 _("Missing option `%s' for operation `%s'\n"),
668 GNUNET_SCHEDULER_shutdown ();
672 if (NULL == typestring)
675 _("Missing option `%s' for operation `%s'\n"),
677 GNUNET_SCHEDULER_shutdown ();
681 type = GNUNET_GNSRECORD_typename_to_number (typestring);
682 if (UINT32_MAX == type)
684 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
685 GNUNET_SCHEDULER_shutdown ();
692 _("Missing option `%s' for operation `%s'\n"),
695 GNUNET_SCHEDULER_shutdown ();
699 GNUNET_GNSRECORD_string_to_value (type,
704 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
707 GNUNET_SCHEDULER_shutdown ();
711 if (NULL == expirationstring)
714 _("Missing option `%s' for operation `%s'\n"),
716 GNUNET_SCHEDULER_shutdown ();
720 if (0 == strcmp (expirationstring, "never"))
722 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
723 etime_is_rel = GNUNET_NO;
725 else if (GNUNET_OK ==
726 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
729 etime_is_rel = GNUNET_YES;
731 else if (GNUNET_OK ==
732 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
735 etime_is_rel = GNUNET_NO;
740 _("Invalid time format `%s'\n"),
742 GNUNET_SCHEDULER_shutdown ();
746 add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
747 &get_existing_record, NULL );
754 _("Missing option `%s' for operation `%s'\n"),
756 GNUNET_SCHEDULER_shutdown ();
760 del_qe = GNUNET_NAMESTORE_records_lookup (ns,
768 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
773 if (NULL != reverse_pkey)
775 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
778 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
779 strlen (reverse_pkey),
783 _("Invalid public key for reverse lookup `%s'\n"),
785 GNUNET_SCHEDULER_shutdown ();
787 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
790 &handle_reverse_lookup,
797 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
799 if ( (2 != (sscanf (uri, "gnunet://gns/%52s/%63s", sh, sname)) ) ||
800 (GNUNET_OK != GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
803 _("Invalid URI `%s'\n"),
805 GNUNET_SCHEDULER_shutdown ();
809 memset (&rd, 0, sizeof (rd));
811 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
812 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
813 if (GNUNET_YES == etime_is_rel)
815 rd.expiration_time = etime_rel.rel_value_us;
816 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
818 else if (GNUNET_NO == etime_is_rel)
819 rd.expiration_time = etime_abs.abs_value_us;
821 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
824 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
825 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
833 if (NULL != nickstring)
835 if (0 == strlen(nickstring))
838 _("Invalid nick `%s'\n"),
840 GNUNET_SCHEDULER_shutdown ();
844 add_qe_uri = GNUNET_NAMESTORE_set_nick(ns, &zone_pkey, nickstring,
845 &add_continuation, &add_qe_uri);
849 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
860 * Callback invoked from identity service with ego information.
861 * An @a ego of NULL means the ego was not found.
863 * @param cls closure with the configuration
864 * @param ego an ego known to identity service, or NULL
867 identity_cb (void *cls,
868 const struct GNUNET_IDENTITY_Ego *ego)
870 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
875 if (NULL != ego_name)
878 _("Ego `%s' not known to identity service\n"),
881 GNUNET_SCHEDULER_shutdown ();
885 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
886 GNUNET_free_non_null (ego_name);
888 GNUNET_CLIENT_service_test ("namestore", cfg,
889 GNUNET_TIME_UNIT_SECONDS,
896 default_ego_cb (void *cls,
897 struct GNUNET_IDENTITY_Ego *ego,
905 _("No default ego configured in identity service\n"));
906 GNUNET_SCHEDULER_shutdown ();
912 identity_cb (cls, ego);
918 id_connect_cb (void *cls,
919 struct GNUNET_IDENTITY_Ego *ego,
923 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
927 get_default = GNUNET_IDENTITY_get (idh,
929 &default_ego_cb, (void *) cfg);
935 testservice_id_task (void *cls, int result)
937 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
938 if (result != GNUNET_YES)
941 _("Identity service is not running\n"));
942 GNUNET_SCHEDULER_shutdown ();
946 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
947 &do_shutdown, (void *) cfg);
949 if (NULL == ego_name)
951 idh = GNUNET_IDENTITY_connect (cfg, &id_connect_cb, (void *) cfg);
953 fprintf (stderr, _("Cannot connect to identity service\n"));
957 el = GNUNET_IDENTITY_ego_lookup (cfg,
965 * Main function that will be run.
968 * @param args remaining command-line arguments
969 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
970 * @param cfg configuration
973 run (void *cls, char *const *args, const char *cfgfile,
974 const struct GNUNET_CONFIGURATION_Handle *cfg)
976 if ( (NULL != args[0]) && (NULL == uri) )
977 uri = GNUNET_strdup (args[0]);
979 GNUNET_CLIENT_service_test ("identity", cfg,
980 GNUNET_TIME_UNIT_SECONDS,
981 &testservice_id_task,
987 * The main function for gnunet-namestore.
989 * @param argc number of arguments from the command line
990 * @param argv command line arguments
991 * @return 0 ok, 1 on error
994 main (int argc, char *const *argv)
1000 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1002 gettext_noop ("add record"), 0,
1003 &GNUNET_GETOPT_set_one, &add},
1004 {'d', "delete", NULL,
1005 gettext_noop ("delete record"), 0,
1006 &GNUNET_GETOPT_set_one, &del},
1007 {'D', "display", NULL,
1008 gettext_noop ("display records"), 0,
1009 &GNUNET_GETOPT_set_one, &list},
1010 {'e', "expiration", "TIME",
1011 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
1012 &GNUNET_GETOPT_set_string, &expirationstring},
1013 {'i', "nick", "NICKNAME",
1014 gettext_noop ("set the desired nick name for the zone"), 1,
1015 &GNUNET_GETOPT_set_string, &nickstring},
1016 {'m', "monitor", NULL,
1017 gettext_noop ("monitor changes in the namestore"), 0,
1018 &GNUNET_GETOPT_set_one, &monitor},
1019 {'n', "name", "NAME",
1020 gettext_noop ("name of the record to add/delete/display"), 1,
1021 &GNUNET_GETOPT_set_string, &name},
1022 {'r', "reverse", "PKEY",
1023 gettext_noop ("determine our name for the given PKEY"), 1,
1024 &GNUNET_GETOPT_set_string, &reverse_pkey},
1025 {'t', "type", "TYPE",
1026 gettext_noop ("type of the record to add/delete/display"), 1,
1027 &GNUNET_GETOPT_set_string, &typestring},
1029 gettext_noop ("URI to import into our zone"), 1,
1030 &GNUNET_GETOPT_set_string, &uri},
1031 {'V', "value", "VALUE",
1032 gettext_noop ("value of the record to add/delete"), 1,
1033 &GNUNET_GETOPT_set_string, &value},
1034 {'p', "public", NULL,
1035 gettext_noop ("create or list public record"), 0,
1036 &GNUNET_GETOPT_set_one, &is_public},
1037 {'P', "pending", NULL,
1038 gettext_noop ("create record that is pending approval (and thus for now inactive)"), 0,
1039 &GNUNET_GETOPT_set_one, &is_pending},
1040 {'s', "shadow", NULL,
1041 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
1042 &GNUNET_GETOPT_set_one, &is_shadow},
1043 {'z', "zone", "EGO",
1044 gettext_noop ("name of the ego controlling the zone"), 1,
1045 &GNUNET_GETOPT_set_string, &ego_name},
1046 GNUNET_GETOPT_OPTION_END
1049 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1052 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
1054 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
1055 _("GNUnet zone manipulation tool"),
1059 GNUNET_free ((void*) argv);
1060 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1063 GNUNET_free ((void*) argv);
1064 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
1068 /* end of gnunet-namestore.c */