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 * Queue entry for the 'add-uri' operation.
65 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
68 * Queue entry for the 'add' operation.
70 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
73 * Queue entry for the 'reverse lookup' operation (in combination with a name).
75 static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
78 * Desired action is to list records.
83 * List iterator for the 'list' operation.
85 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
88 * Desired action is to remove a record.
93 * Is record public (opposite of #GNUNET_GNSRECORD_RF_PRIVATE)
98 * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
103 * Queue entry for the 'del' operation.
105 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
108 * Name of the records to add/list/remove.
113 * Value of the record to add/remove.
123 * Reverse lookup to perform.
125 static char *reverse_pkey;
128 * Type of the record to add/remove, NULL to remove all.
130 static char *typestring;
133 * Desired expiration time.
135 static char *expirationstring;
140 static char *nickstring;
143 * Global return value
148 * Type string converted to DNS type value.
150 static uint32_t type;
153 * Value in binary format.
158 * Number of bytes in 'data'.
160 static size_t data_size;
163 * Expirationstring converted to relative time.
165 static struct GNUNET_TIME_Relative etime_rel;
168 * Expirationstring converted to absolute time.
170 static struct GNUNET_TIME_Absolute etime_abs;
173 * Is expiration time relative or absolute time?
175 static int etime_is_rel = GNUNET_SYSERR;
180 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
183 * Enables monitor mode.
189 * Task run on shutdown. Cleans up everything.
192 * @param tc scheduler context
195 do_shutdown (void *cls,
196 const struct GNUNET_SCHEDULER_TaskContext *tc)
200 GNUNET_IDENTITY_ego_lookup_cancel (el);
205 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
210 GNUNET_NAMESTORE_cancel (add_qe);
213 if (NULL != add_qe_uri)
215 GNUNET_NAMESTORE_cancel (add_qe_uri);
220 GNUNET_NAMESTORE_cancel (del_qe);
225 GNUNET_NAMESTORE_disconnect (ns);
228 memset (&zone_pkey, 0, sizeof (zone_pkey));
236 GNUNET_NAMESTORE_zone_monitor_stop (zm);
248 * Check if we are finished, and if so, perform shutdown.
253 if ( (NULL == add_qe) &&
254 (NULL == add_qe_uri) &&
256 (NULL == reverse_qe) &&
258 GNUNET_SCHEDULER_shutdown ();
263 * Continuation called to notify client about result of the
266 * @param cls closure, location of the QueueEntry pointer to NULL out
267 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
268 * #GNUNET_NO if content was already there
269 * #GNUNET_YES (or other positive value) on success
270 * @param emsg NULL on success, otherwise an error message
273 add_continuation (void *cls,
277 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
280 if (GNUNET_YES != success)
283 _("Adding record failed: %s\n"),
284 (GNUNET_NO == success) ? "record exists" : emsg);
285 if (GNUNET_NO != success)
293 * Continuation called to notify client about result of the
296 * @param cls closure, unused
297 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
298 * #GNUNET_NO if content was already there
299 * #GNUNET_YES (or other positive value) on success
300 * @param emsg NULL on success, otherwise an error message
303 del_continuation (void *cls,
308 if (GNUNET_NO == success)
311 _("Deleting record failed, record does not exist%s%s\n"),
312 (NULL != emsg) ? ": " : "",
313 (NULL != emsg) ? emsg : "");
315 if (GNUNET_SYSERR == success)
318 _("Deleting record failed%s%s\n"),
319 (NULL != emsg) ? ": " : "",
320 (NULL != emsg) ? emsg : "");
327 * Process a record that was stored in the namestore.
330 * @param zone_key private key of the zone
331 * @param rname name that is being mapped (at most 255 characters long)
332 * @param rd_len number of entries in @a rd array
333 * @param rd array of records with data to store
336 display_record (void *cls,
337 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
340 const struct GNUNET_GNSRECORD_Data *rd)
342 const char *typestring;
346 struct GNUNET_TIME_Absolute at;
347 struct GNUNET_TIME_Relative rt;
355 if ( (NULL != name) &&
356 (0 != strcmp (name, rname)) )
358 GNUNET_NAMESTORE_zone_iterator_next (list_it);
364 for (i=0;i<rd_len;i++)
366 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
367 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
372 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
373 (unsigned int) rd[i].record_type);
376 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
378 rt.rel_value_us = rd[i].expiration_time;
379 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
383 at.abs_value_us = rd[i].expiration_time;
384 ets = GNUNET_STRINGS_absolute_time_to_string (at);
387 "\t%s: %s (%s)\t%s\t%s\t%s\n",
391 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
392 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "",
393 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PENDING)) ? "PENDING" : "");
396 FPRINTF (stdout, "%s", "\n");
397 GNUNET_NAMESTORE_zone_iterator_next (list_it);
402 * Function called once we are in sync in monitor mode.
409 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
414 * We're storing a record; this function is given the existing record
415 * so that we can merge the information.
417 * @param cls closure, unused
418 * @param zone_key private key of the zone
419 * @param rec_name name that is being mapped (at most 255 characters long)
420 * @param rd_count number of entries in @a rd array
421 * @param rd array of records with data to store
424 get_existing_record (void *cls,
425 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
426 const char *rec_name,
427 unsigned int rd_count,
428 const struct GNUNET_GNSRECORD_Data *rd)
430 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
431 struct GNUNET_GNSRECORD_Data *rde;
434 if ( (NULL != zone_key) &&
435 (0 != strcmp (rec_name, name)) )
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u records for name `%s'\n",
444 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
445 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
446 /* FIXME: should add some logic to overwrite records if there
447 can only be one record of a particular type, and to check
448 if the combination of records is valid to begin with... */
451 rde->data_size = data_size;
452 rde->record_type = type;
454 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
456 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
457 if (GNUNET_YES == etime_is_rel)
459 rde->expiration_time = etime_rel.rel_value_us;
460 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
462 else if (GNUNET_NO == etime_is_rel)
463 rde->expiration_time = etime_abs.abs_value_us;
465 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
466 GNUNET_assert (NULL != name);
467 add_qe = GNUNET_NAMESTORE_records_store (ns,
478 * Function called with the result of our attempt to obtain a name for a given
482 * @param zone private key of the zone; NULL on disconnect
483 * @param label label of the records; NULL on disconnect
484 * @param rd_count number of entries in @a rd array, 0 if label was deleted
485 * @param rd array of records with data to store
488 handle_reverse_lookup (void *cls,
489 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
491 unsigned int rd_count,
492 const struct GNUNET_GNSRECORD_Data *rd)
508 * Function called with the result from the check if the namestore
509 * service is actually running. If it is, we start the actual
512 * @param cls closure with our configuration
513 * @param result #GNUNET_YES if the namestore service is running
516 testservice_task (void *cls,
519 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
520 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
521 struct GNUNET_GNSRECORD_Data rd;
523 if (GNUNET_YES != result)
525 FPRINTF (stderr, _("Service `%s' is not running\n"),
529 if (! (add|del|list|(NULL != nickstring)|(NULL != uri)|(NULL != reverse_pkey)) )
531 /* nothing more to be done */
533 _("No options given\n"));
534 GNUNET_SCHEDULER_shutdown ();
537 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
540 ns = GNUNET_NAMESTORE_connect (cfg);
543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
544 _("Failed to connect to namestore\n"));
552 _("Missing option `%s' for operation `%s'\n"),
554 GNUNET_SCHEDULER_shutdown ();
558 if (NULL == typestring)
561 _("Missing option `%s' for operation `%s'\n"),
563 GNUNET_SCHEDULER_shutdown ();
567 type = GNUNET_GNSRECORD_typename_to_number (typestring);
568 if (UINT32_MAX == type)
570 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
571 GNUNET_SCHEDULER_shutdown ();
578 _("Missing option `%s' for operation `%s'\n"),
581 GNUNET_SCHEDULER_shutdown ();
585 GNUNET_GNSRECORD_string_to_value (type,
590 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
593 GNUNET_SCHEDULER_shutdown ();
597 if (NULL == expirationstring)
600 _("Missing option `%s' for operation `%s'\n"),
602 GNUNET_SCHEDULER_shutdown ();
606 if (0 == strcmp (expirationstring, "never"))
608 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
609 etime_is_rel = GNUNET_NO;
611 else if (GNUNET_OK ==
612 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
615 etime_is_rel = GNUNET_YES;
617 else if (GNUNET_OK ==
618 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
621 etime_is_rel = GNUNET_NO;
626 _("Invalid time format `%s'\n"),
628 GNUNET_SCHEDULER_shutdown ();
632 add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
633 &get_existing_record, NULL );
640 _("Missing option `%s' for operation `%s'\n"),
642 GNUNET_SCHEDULER_shutdown ();
646 del_qe = GNUNET_NAMESTORE_records_store (ns,
655 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
660 if (NULL != reverse_pkey)
662 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
665 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
666 strlen (reverse_pkey),
670 _("Invalid public key for reverse lookup `%s'\n"),
672 GNUNET_SCHEDULER_shutdown ();
674 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
677 &handle_reverse_lookup,
684 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
686 if ( (2 != (sscanf (uri,
687 "gnunet://gns/%104s/%63s",
691 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
694 _("Invalid URI `%s'\n"),
696 GNUNET_SCHEDULER_shutdown ();
700 memset (&rd, 0, sizeof (rd));
702 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
703 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
704 if (GNUNET_YES == etime_is_rel)
706 rd.expiration_time = etime_rel.rel_value_us;
707 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
709 else if (GNUNET_NO == etime_is_rel)
710 rd.expiration_time = etime_abs.abs_value_us;
712 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
715 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
716 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
724 if (NULL != nickstring)
726 if (0 == strlen(nickstring))
729 _("Invalid nick `%s'\n"),
731 GNUNET_SCHEDULER_shutdown ();
735 memset (&rd, 0, sizeof (rd));
736 rd.data = nickstring;
737 rd.data_size = strlen(nickstring);
738 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
739 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
740 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
741 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
751 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
762 * Callback invoked from identity service with ego information.
763 * An @a ego of NULL means the ego was not found.
765 * @param cls closure with the configuration
766 * @param ego an ego known to identity service, or NULL
769 identity_cb (void *cls,
770 const struct GNUNET_IDENTITY_Ego *ego)
772 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
778 _("Ego `%s' not known to identity service\n"),
780 GNUNET_SCHEDULER_shutdown ();
783 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
784 GNUNET_free (ego_name);
786 GNUNET_CLIENT_service_test ("namestore", cfg,
787 GNUNET_TIME_UNIT_SECONDS,
794 * Main function that will be run.
797 * @param args remaining command-line arguments
798 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
799 * @param cfg configuration
802 run (void *cls, char *const *args, const char *cfgfile,
803 const struct GNUNET_CONFIGURATION_Handle *cfg)
805 if (NULL == ego_name)
808 _("You must specify which zone should be accessed\n"));
812 if ( (NULL != args[0]) && (NULL == uri) )
813 uri = GNUNET_strdup (args[0]);
814 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
816 el = GNUNET_IDENTITY_ego_lookup (cfg,
824 * The main function for gnunet-namestore.
826 * @param argc number of arguments from the command line
827 * @param argv command line arguments
828 * @return 0 ok, 1 on error
831 main (int argc, char *const *argv)
836 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
838 gettext_noop ("add record"), 0,
839 &GNUNET_GETOPT_set_one, &add},
840 {'d', "delete", NULL,
841 gettext_noop ("delete record"), 0,
842 &GNUNET_GETOPT_set_one, &del},
843 {'D', "display", NULL,
844 gettext_noop ("display records"), 0,
845 &GNUNET_GETOPT_set_one, &list},
846 {'e', "expiration", "TIME",
847 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
848 &GNUNET_GETOPT_set_string, &expirationstring},
849 {'i', "nick", "NICKNAME",
850 gettext_noop ("set the desired nick name for the zone"), 1,
851 &GNUNET_GETOPT_set_string, &nickstring},
852 {'m', "monitor", NULL,
853 gettext_noop ("monitor changes in the namestore"), 0,
854 &GNUNET_GETOPT_set_one, &monitor},
855 {'n', "name", "NAME",
856 gettext_noop ("name of the record to add/delete/display"), 1,
857 &GNUNET_GETOPT_set_string, &name},
858 {'r', "reverse", "PKEY",
859 gettext_noop ("determine our name for the given PKEY"), 1,
860 &GNUNET_GETOPT_set_string, &reverse_pkey},
861 {'t', "type", "TYPE",
862 gettext_noop ("type of the record to add/delete/display"), 1,
863 &GNUNET_GETOPT_set_string, &typestring},
865 gettext_noop ("URI to import into our zone"), 1,
866 &GNUNET_GETOPT_set_string, &uri},
867 {'V', "value", "VALUE",
868 gettext_noop ("value of the record to add/delete"), 1,
869 &GNUNET_GETOPT_set_string, &value},
870 {'p', "public", NULL,
871 gettext_noop ("create or list public record"), 0,
872 &GNUNET_GETOPT_set_one, &public},
873 {'s', "shadow", NULL,
874 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
875 &GNUNET_GETOPT_set_one, &shadow},
877 gettext_noop ("name of the ego controlling the zone"), 1,
878 &GNUNET_GETOPT_set_string, &ego_name},
879 GNUNET_GETOPT_OPTION_END
882 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
885 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
887 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
888 _("GNUnet zone manipulation tool"),
892 GNUNET_free ((void*) argv);
893 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
896 GNUNET_free ((void*) argv);
897 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
901 /* end of gnunet-namestore.c */