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 * Iterator for the 'add' operation.
65 static struct GNUNET_NAMESTORE_ZoneIterator *add_zit;
68 * Queue entry for the 'add-uri' operation.
70 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
73 * Queue entry for the 'add' operation.
75 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
78 * Queue entry for the 'reverse lookup' operation (in combination with a name).
80 static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
83 * Desired action is to list records.
88 * List iterator for the 'list' operation.
90 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
93 * Desired action is to remove a record.
98 * Is record public (opposite of #GNUNET_GNSRECORD_RF_PRIVATE)
103 * Is record a shadow record (#GNUNET_GNSRECORD_RF_SHADOW_RECORD)
108 * Queue entry for the 'del' operation.
110 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
113 * Name of the records to add/list/remove.
118 * Value of the record to add/remove.
128 * Reverse lookup to perform.
130 static char *reverse_pkey;
133 * Type of the record to add/remove, NULL to remove all.
135 static char *typestring;
138 * Desired expiration time.
140 static char *expirationstring;
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_zone_iteration_stop (add_zit);
215 GNUNET_NAMESTORE_cancel (add_qe);
218 if (NULL != add_qe_uri)
220 GNUNET_NAMESTORE_cancel (add_qe_uri);
225 GNUNET_NAMESTORE_cancel (del_qe);
230 GNUNET_NAMESTORE_disconnect (ns);
233 memset (&zone_pkey, 0, sizeof (zone_pkey));
241 GNUNET_NAMESTORE_zone_monitor_stop (zm);
253 * Check if we are finished, and if so, perform shutdown.
258 if ( (NULL == add_qe) &&
259 (NULL == add_qe_uri) &&
261 (NULL == reverse_qe) &&
263 GNUNET_SCHEDULER_shutdown ();
268 * Continuation called to notify client about result of the
271 * @param cls closure, location of the QueueEntry pointer to NULL out
272 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
273 * #GNUNET_NO if content was already there
274 * #GNUNET_YES (or other positive value) on success
275 * @param emsg NULL on success, otherwise an error message
278 add_continuation (void *cls,
282 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
285 if (GNUNET_YES != success)
288 _("Adding record failed: %s\n"),
289 (GNUNET_NO == success) ? "record exists" : emsg);
290 if (GNUNET_NO != success)
298 * Continuation called to notify client about result of the
301 * @param cls closure, unused
302 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
303 * #GNUNET_NO if content was already there
304 * #GNUNET_YES (or other positive value) on success
305 * @param emsg NULL on success, otherwise an error message
308 del_continuation (void *cls,
313 if (success != GNUNET_YES)
315 _("Deleting record failed: %s\n"),
322 * Process a record that was stored in the namestore.
325 * @param zone_key private key of the zone
326 * @param rname name that is being mapped (at most 255 characters long)
327 * @param rd_len number of entries in @a rd array
328 * @param rd array of records with data to store
331 display_record (void *cls,
332 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
335 const struct GNUNET_GNSRECORD_Data *rd)
337 const char *typestring;
341 struct GNUNET_TIME_Absolute at;
342 struct GNUNET_TIME_Relative rt;
350 if ( (NULL != name) &&
351 (0 != strcmp (name, rname)) )
353 GNUNET_NAMESTORE_zone_iterator_next (list_it);
359 for (i=0;i<rd_len;i++)
361 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
362 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
367 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
368 (unsigned int) rd[i].record_type);
371 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
373 rt.rel_value_us = rd[i].expiration_time;
374 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
378 at.abs_value_us = rd[i].expiration_time;
379 ets = GNUNET_STRINGS_absolute_time_to_string (at);
388 FPRINTF (stdout, "%s", "\n");
389 GNUNET_NAMESTORE_zone_iterator_next (list_it);
394 * Function called once we are in sync in monitor mode.
401 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
406 * We're storing a record; this function is given the existing record
407 * so that we can merge the information.
409 * @param cls closure, unused
410 * @param zone_key private key of the zone
411 * @param rec_name name that is being mapped (at most 255 characters long)
412 * @param rd_count number of entries in @a rd array
413 * @param rd array of records with data to store
416 get_existing_record (void *cls,
417 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
418 const char *rec_name,
419 unsigned int rd_count,
420 const struct GNUNET_GNSRECORD_Data *rd)
422 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
423 struct GNUNET_GNSRECORD_Data *rde;
425 if ( (NULL != zone_key) &&
426 (0 != strcmp (rec_name, name)) )
428 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
431 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
432 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
433 /* FIXME: should add some logic to overwrite records if there
434 can only be one record of a particular type, and to check
435 if the combination of records is valid to begin with... */
438 rde->data_size = data_size;
439 rde->record_type = type;
441 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
443 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
444 if (GNUNET_YES == etime_is_rel)
446 rde->expiration_time = etime_rel.rel_value_us;
447 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
449 else if (GNUNET_NO == etime_is_rel)
450 rde->expiration_time = etime_abs.abs_value_us;
452 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
453 GNUNET_assert (NULL != name);
454 add_qe = GNUNET_NAMESTORE_records_store (ns,
461 /* only cancel if we were not told that this
462 was the end of the iteration already */
463 if (NULL != rec_name)
464 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
470 * Function called with the result of our attempt to obtain a name for a given
474 * @param zone private key of the zone; NULL on disconnect
475 * @param label label of the records; NULL on disconnect
476 * @param rd_count number of entries in @a rd array, 0 if label was deleted
477 * @param rd array of records with data to store
480 handle_reverse_lookup (void *cls,
481 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
483 unsigned int rd_count,
484 const struct GNUNET_GNSRECORD_Data *rd)
500 * Function called with the result from the check if the namestore
501 * service is actually running. If it is, we start the actual
504 * @param cls closure with our configuration
505 * @param result #GNUNET_YES if the namestore service is running
508 testservice_task (void *cls,
511 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
512 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
513 struct GNUNET_GNSRECORD_Data rd;
515 if (GNUNET_YES != result)
517 FPRINTF (stderr, _("Service `%s' is not running\n"),
521 if (! (add|del|list|(NULL != uri)|(NULL != reverse_pkey)) )
523 /* nothing more to be done */
525 _("No options given\n"));
526 GNUNET_SCHEDULER_shutdown ();
529 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
532 ns = GNUNET_NAMESTORE_connect (cfg);
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536 _("Failed to connect to namestore\n"));
544 _("Missing option `%s' for operation `%s'\n"),
546 GNUNET_SCHEDULER_shutdown ();
550 if (NULL == typestring)
553 _("Missing option `%s' for operation `%s'\n"),
555 GNUNET_SCHEDULER_shutdown ();
559 type = GNUNET_GNSRECORD_typename_to_number (typestring);
560 if (UINT32_MAX == type)
562 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
563 GNUNET_SCHEDULER_shutdown ();
570 _("Missing option `%s' for operation `%s'\n"),
573 GNUNET_SCHEDULER_shutdown ();
577 GNUNET_GNSRECORD_string_to_value (type,
582 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
585 GNUNET_SCHEDULER_shutdown ();
589 if (NULL == expirationstring)
592 _("Missing option `%s' for operation `%s'\n"),
594 GNUNET_SCHEDULER_shutdown ();
598 if (0 == strcmp (expirationstring, "never"))
600 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
601 etime_is_rel = GNUNET_NO;
603 else if (GNUNET_OK ==
604 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
607 etime_is_rel = GNUNET_YES;
609 else if (GNUNET_OK ==
610 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
613 etime_is_rel = GNUNET_NO;
618 _("Invalid time format `%s'\n"),
620 GNUNET_SCHEDULER_shutdown ();
624 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
626 &get_existing_record,
634 _("Missing option `%s' for operation `%s'\n"),
636 GNUNET_SCHEDULER_shutdown ();
640 del_qe = GNUNET_NAMESTORE_records_store (ns,
649 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
654 if (NULL != reverse_pkey)
656 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
659 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
660 strlen (reverse_pkey),
664 _("Invalid public key for reverse lookup `%s'\n"),
666 GNUNET_SCHEDULER_shutdown ();
668 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
671 &handle_reverse_lookup,
678 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
680 if ( (2 != (sscanf (uri,
681 "gnunet://gns/%104s/%63s",
685 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
688 _("Invalid URI `%s'\n"),
690 GNUNET_SCHEDULER_shutdown ();
694 memset (&rd, 0, sizeof (rd));
696 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
697 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
698 if (GNUNET_YES == etime_is_rel)
700 rd.expiration_time = etime_rel.rel_value_us;
701 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
703 else if (GNUNET_NO == etime_is_rel)
704 rd.expiration_time = etime_abs.abs_value_us;
706 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
708 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
709 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
719 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
729 * Callback invoked from identity service with ego information.
730 * An @a ego of NULL means the ego was not found.
732 * @param cls closure with the configuration
733 * @param ego an ego known to identity service, or NULL
736 identity_cb (void *cls,
737 const struct GNUNET_IDENTITY_Ego *ego)
739 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
745 _("Ego `%s' not known to identity service\n"),
747 GNUNET_SCHEDULER_shutdown ();
750 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
751 GNUNET_free (ego_name);
753 GNUNET_CLIENT_service_test ("namestore", cfg,
754 GNUNET_TIME_UNIT_SECONDS,
761 * Main function that will be run.
764 * @param args remaining command-line arguments
765 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
766 * @param cfg configuration
769 run (void *cls, char *const *args, const char *cfgfile,
770 const struct GNUNET_CONFIGURATION_Handle *cfg)
772 if (NULL == ego_name)
775 _("You must specify which zone should be accessed\n"));
778 if ( (NULL != args[0]) && (NULL == uri) )
779 uri = GNUNET_strdup (args[0]);
780 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
782 el = GNUNET_IDENTITY_ego_lookup (cfg,
790 * The main function for gnunet-namestore.
792 * @param argc number of arguments from the command line
793 * @param argv command line arguments
794 * @return 0 ok, 1 on error
797 main (int argc, char *const *argv)
801 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
803 gettext_noop ("add record"), 0,
804 &GNUNET_GETOPT_set_one, &add},
805 {'d', "delete", NULL,
806 gettext_noop ("delete record"), 0,
807 &GNUNET_GETOPT_set_one, &del},
808 {'D', "display", NULL,
809 gettext_noop ("display records"), 0,
810 &GNUNET_GETOPT_set_one, &list},
811 {'e', "expiration", "TIME",
812 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
813 &GNUNET_GETOPT_set_string, &expirationstring},
814 {'m', "monitor", NULL,
815 gettext_noop ("monitor changes in the namestore"), 0,
816 &GNUNET_GETOPT_set_one, &monitor},
817 {'n', "name", "NAME",
818 gettext_noop ("name of the record to add/delete/display"), 1,
819 &GNUNET_GETOPT_set_string, &name},
820 {'r', "reverse", "PKEY",
821 gettext_noop ("determine our name for the given PKEY"), 1,
822 &GNUNET_GETOPT_set_string, &reverse_pkey},
823 {'t', "type", "TYPE",
824 gettext_noop ("type of the record to add/delete/display"), 1,
825 &GNUNET_GETOPT_set_string, &typestring},
827 gettext_noop ("URI to import into our zone"), 1,
828 &GNUNET_GETOPT_set_string, &uri},
829 {'V', "value", "VALUE",
830 gettext_noop ("value of the record to add/delete"), 1,
831 &GNUNET_GETOPT_set_string, &value},
832 {'p', "public", NULL,
833 gettext_noop ("create or list public record"), 0,
834 &GNUNET_GETOPT_set_one, &public},
835 {'s', "shadow", NULL,
836 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
837 &GNUNET_GETOPT_set_one, &shadow},
839 gettext_noop ("name of the ego controlling the zone"), 1,
840 &GNUNET_GETOPT_set_string, &ego_name},
841 GNUNET_GETOPT_OPTION_END
844 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
847 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
849 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
850 _("GNUnet zone manipulation tool"),
854 GNUNET_free ((void*) argv);
855 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
858 GNUNET_free ((void*) argv);
859 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
863 /* end of gnunet-namestore.c */