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;
138 * Global return value
143 * Type string converted to DNS type value.
145 static uint32_t type;
148 * Value in binary format.
153 * Number of bytes in 'data'.
155 static size_t data_size;
158 * Expirationstring converted to relative time.
160 static struct GNUNET_TIME_Relative etime_rel;
163 * Expirationstring converted to absolute time.
165 static struct GNUNET_TIME_Absolute etime_abs;
168 * Is expiration time relative or absolute time?
170 static int etime_is_rel = GNUNET_SYSERR;
175 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
178 * Enables monitor mode.
184 * Task run on shutdown. Cleans up everything.
187 * @param tc scheduler context
190 do_shutdown (void *cls,
191 const struct GNUNET_SCHEDULER_TaskContext *tc)
195 GNUNET_IDENTITY_ego_lookup_cancel (el);
200 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
205 GNUNET_NAMESTORE_cancel (add_qe);
208 if (NULL != add_qe_uri)
210 GNUNET_NAMESTORE_cancel (add_qe_uri);
215 GNUNET_NAMESTORE_cancel (del_qe);
220 GNUNET_NAMESTORE_disconnect (ns);
223 memset (&zone_pkey, 0, sizeof (zone_pkey));
231 GNUNET_NAMESTORE_zone_monitor_stop (zm);
243 * Check if we are finished, and if so, perform shutdown.
248 if ( (NULL == add_qe) &&
249 (NULL == add_qe_uri) &&
251 (NULL == reverse_qe) &&
253 GNUNET_SCHEDULER_shutdown ();
258 * Continuation called to notify client about result of the
261 * @param cls closure, location of the QueueEntry pointer to NULL out
262 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
263 * #GNUNET_NO if content was already there
264 * #GNUNET_YES (or other positive value) on success
265 * @param emsg NULL on success, otherwise an error message
268 add_continuation (void *cls,
272 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
275 if (GNUNET_YES != success)
278 _("Adding record failed: %s\n"),
279 (GNUNET_NO == success) ? "record exists" : emsg);
280 if (GNUNET_NO != success)
288 * Continuation called to notify client about result of the
291 * @param cls closure, unused
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 del_continuation (void *cls,
303 if (success != GNUNET_YES)
305 _("Deleting record failed: %s\n"),
312 * Process a record that was stored in the namestore.
315 * @param zone_key private key of the zone
316 * @param rname name that is being mapped (at most 255 characters long)
317 * @param rd_len number of entries in @a rd array
318 * @param rd array of records with data to store
321 display_record (void *cls,
322 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
325 const struct GNUNET_GNSRECORD_Data *rd)
327 const char *typestring;
331 struct GNUNET_TIME_Absolute at;
332 struct GNUNET_TIME_Relative rt;
340 if ( (NULL != name) &&
341 (0 != strcmp (name, rname)) )
343 GNUNET_NAMESTORE_zone_iterator_next (list_it);
349 for (i=0;i<rd_len;i++)
351 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
352 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
357 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
358 (unsigned int) rd[i].record_type);
361 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
363 rt.rel_value_us = rd[i].expiration_time;
364 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
368 at.abs_value_us = rd[i].expiration_time;
369 ets = GNUNET_STRINGS_absolute_time_to_string (at);
378 FPRINTF (stdout, "%s", "\n");
379 GNUNET_NAMESTORE_zone_iterator_next (list_it);
384 * Function called once we are in sync in monitor mode.
391 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
396 * We're storing a record; this function is given the existing record
397 * so that we can merge the information.
399 * @param cls closure, unused
400 * @param zone_key private key of the zone
401 * @param rec_name name that is being mapped (at most 255 characters long)
402 * @param rd_count number of entries in @a rd array
403 * @param rd array of records with data to store
406 get_existing_record (void *cls,
407 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
408 const char *rec_name,
409 unsigned int rd_count,
410 const struct GNUNET_GNSRECORD_Data *rd)
412 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
413 struct GNUNET_GNSRECORD_Data *rde;
416 if ( (NULL != zone_key) &&
417 (0 != strcmp (rec_name, name)) )
422 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
423 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
424 /* FIXME: should add some logic to overwrite records if there
425 can only be one record of a particular type, and to check
426 if the combination of records is valid to begin with... */
429 rde->data_size = data_size;
430 rde->record_type = type;
432 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
434 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
435 if (GNUNET_YES == etime_is_rel)
437 rde->expiration_time = etime_rel.rel_value_us;
438 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
440 else if (GNUNET_NO == etime_is_rel)
441 rde->expiration_time = etime_abs.abs_value_us;
443 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
444 GNUNET_assert (NULL != name);
445 add_qe = GNUNET_NAMESTORE_records_store (ns,
456 * Function called with the result of our attempt to obtain a name for a given
460 * @param zone private key of the zone; NULL on disconnect
461 * @param label label of the records; NULL on disconnect
462 * @param rd_count number of entries in @a rd array, 0 if label was deleted
463 * @param rd array of records with data to store
466 handle_reverse_lookup (void *cls,
467 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
469 unsigned int rd_count,
470 const struct GNUNET_GNSRECORD_Data *rd)
486 * Function called with the result from the check if the namestore
487 * service is actually running. If it is, we start the actual
490 * @param cls closure with our configuration
491 * @param result #GNUNET_YES if the namestore service is running
494 testservice_task (void *cls,
497 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
498 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
499 struct GNUNET_GNSRECORD_Data rd;
501 if (GNUNET_YES != result)
503 FPRINTF (stderr, _("Service `%s' is not running\n"),
507 if (! (add|del|list|(NULL != uri)|(NULL != reverse_pkey)) )
509 /* nothing more to be done */
511 _("No options given\n"));
512 GNUNET_SCHEDULER_shutdown ();
515 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
518 ns = GNUNET_NAMESTORE_connect (cfg);
521 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
522 _("Failed to connect to namestore\n"));
530 _("Missing option `%s' for operation `%s'\n"),
532 GNUNET_SCHEDULER_shutdown ();
536 if (NULL == typestring)
539 _("Missing option `%s' for operation `%s'\n"),
541 GNUNET_SCHEDULER_shutdown ();
545 type = GNUNET_GNSRECORD_typename_to_number (typestring);
546 if (UINT32_MAX == type)
548 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
549 GNUNET_SCHEDULER_shutdown ();
556 _("Missing option `%s' for operation `%s'\n"),
559 GNUNET_SCHEDULER_shutdown ();
563 GNUNET_GNSRECORD_string_to_value (type,
568 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
571 GNUNET_SCHEDULER_shutdown ();
575 if (NULL == expirationstring)
578 _("Missing option `%s' for operation `%s'\n"),
580 GNUNET_SCHEDULER_shutdown ();
584 if (0 == strcmp (expirationstring, "never"))
586 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
587 etime_is_rel = GNUNET_NO;
589 else if (GNUNET_OK ==
590 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
593 etime_is_rel = GNUNET_YES;
595 else if (GNUNET_OK ==
596 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
599 etime_is_rel = GNUNET_NO;
604 _("Invalid time format `%s'\n"),
606 GNUNET_SCHEDULER_shutdown ();
610 add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
611 &get_existing_record, NULL );
618 _("Missing option `%s' for operation `%s'\n"),
620 GNUNET_SCHEDULER_shutdown ();
624 del_qe = GNUNET_NAMESTORE_records_store (ns,
633 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
638 if (NULL != reverse_pkey)
640 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
643 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
644 strlen (reverse_pkey),
648 _("Invalid public key for reverse lookup `%s'\n"),
650 GNUNET_SCHEDULER_shutdown ();
652 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
655 &handle_reverse_lookup,
662 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
664 if ( (2 != (sscanf (uri,
665 "gnunet://gns/%104s/%63s",
669 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
672 _("Invalid URI `%s'\n"),
674 GNUNET_SCHEDULER_shutdown ();
678 memset (&rd, 0, sizeof (rd));
680 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
681 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
682 if (GNUNET_YES == etime_is_rel)
684 rd.expiration_time = etime_rel.rel_value_us;
685 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
687 else if (GNUNET_NO == etime_is_rel)
688 rd.expiration_time = etime_abs.abs_value_us;
690 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
692 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
693 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
703 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
714 * Callback invoked from identity service with ego information.
715 * An @a ego of NULL means the ego was not found.
717 * @param cls closure with the configuration
718 * @param ego an ego known to identity service, or NULL
721 identity_cb (void *cls,
722 const struct GNUNET_IDENTITY_Ego *ego)
724 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
730 _("Ego `%s' not known to identity service\n"),
732 GNUNET_SCHEDULER_shutdown ();
735 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
736 GNUNET_free (ego_name);
738 GNUNET_CLIENT_service_test ("namestore", cfg,
739 GNUNET_TIME_UNIT_SECONDS,
746 * Main function that will be run.
749 * @param args remaining command-line arguments
750 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
751 * @param cfg configuration
754 run (void *cls, char *const *args, const char *cfgfile,
755 const struct GNUNET_CONFIGURATION_Handle *cfg)
757 if (NULL == ego_name)
760 _("You must specify which zone should be accessed\n"));
763 if ( (NULL != args[0]) && (NULL == uri) )
764 uri = GNUNET_strdup (args[0]);
765 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
767 el = GNUNET_IDENTITY_ego_lookup (cfg,
775 * The main function for gnunet-namestore.
777 * @param argc number of arguments from the command line
778 * @param argv command line arguments
779 * @return 0 ok, 1 on error
782 main (int argc, char *const *argv)
786 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
788 gettext_noop ("add record"), 0,
789 &GNUNET_GETOPT_set_one, &add},
790 {'d', "delete", NULL,
791 gettext_noop ("delete record"), 0,
792 &GNUNET_GETOPT_set_one, &del},
793 {'D', "display", NULL,
794 gettext_noop ("display records"), 0,
795 &GNUNET_GETOPT_set_one, &list},
796 {'e', "expiration", "TIME",
797 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
798 &GNUNET_GETOPT_set_string, &expirationstring},
799 {'m', "monitor", NULL,
800 gettext_noop ("monitor changes in the namestore"), 0,
801 &GNUNET_GETOPT_set_one, &monitor},
802 {'n', "name", "NAME",
803 gettext_noop ("name of the record to add/delete/display"), 1,
804 &GNUNET_GETOPT_set_string, &name},
805 {'r', "reverse", "PKEY",
806 gettext_noop ("determine our name for the given PKEY"), 1,
807 &GNUNET_GETOPT_set_string, &reverse_pkey},
808 {'t', "type", "TYPE",
809 gettext_noop ("type of the record to add/delete/display"), 1,
810 &GNUNET_GETOPT_set_string, &typestring},
812 gettext_noop ("URI to import into our zone"), 1,
813 &GNUNET_GETOPT_set_string, &uri},
814 {'V', "value", "VALUE",
815 gettext_noop ("value of the record to add/delete"), 1,
816 &GNUNET_GETOPT_set_string, &value},
817 {'p', "public", NULL,
818 gettext_noop ("create or list public record"), 0,
819 &GNUNET_GETOPT_set_one, &public},
820 {'s', "shadow", NULL,
821 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
822 &GNUNET_GETOPT_set_one, &shadow},
824 gettext_noop ("name of the ego controlling the zone"), 1,
825 &GNUNET_GETOPT_set_string, &ego_name},
826 GNUNET_GETOPT_OPTION_END
829 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
832 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
834 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
835 _("GNUnet zone manipulation tool"),
839 GNUNET_free ((void*) argv);
840 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
843 GNUNET_free ((void*) argv);
844 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
848 /* end of gnunet-namestore.c */