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_namestore_service.h>
37 * Handle to the namestore.
39 static struct GNUNET_NAMESTORE_Handle *ns;
42 * Private key for the our zone.
44 static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
47 * Handle to identity lookup.
49 static struct GNUNET_IDENTITY_EgoLookup *el;
52 * Name of the ego controlling the zone.
54 static char *ego_name;
57 * Desired action is to add a record.
62 * Iterator for the 'add' operation.
64 static struct GNUNET_NAMESTORE_ZoneIterator *add_zit;
67 * Queue entry for the 'add-uri' operation.
69 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
72 * Queue entry for the 'add' operation.
74 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
77 * Queue entry for the 'list' operation (in combination with a name).
79 static struct GNUNET_NAMESTORE_QueueEntry *list_qe;
82 * Queue entry for the 'reverse lookup' operation (in combination with a name).
84 static struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
87 * Desired action is to list records.
92 * List iterator for the 'list' operation.
94 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
97 * Desired action is to remove a record.
102 * Is record public (opposite of #GNUNET_NAMESTORE_RF_PRIVATE)
107 * Is record a shadow record (#GNUNET_NAMESTORE_RF_SHADOW_RECORD)
112 * Queue entry for the 'del' operation.
114 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
117 * Name of the records to add/list/remove.
122 * Value of the record to add/remove.
132 * Reverse lookup to perform.
134 static char *reverse_pkey;
137 * Type of the record to add/remove, NULL to remove all.
139 static char *typestring;
142 * Desired expiration time.
144 static char *expirationstring;
147 * Global return value
152 * Type string converted to DNS type value.
154 static uint32_t type;
157 * Value in binary format.
162 * Number of bytes in 'data'.
164 static size_t data_size;
167 * Expirationstring converted to relative time.
169 static struct GNUNET_TIME_Relative etime_rel;
172 * Expirationstring converted to absolute time.
174 static struct GNUNET_TIME_Absolute etime_abs;
177 * Is expiration time relative or absolute time?
179 static int etime_is_rel = GNUNET_SYSERR;
184 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
187 * Enables monitor mode.
193 * Task run on shutdown. Cleans up everything.
196 * @param tc scheduler context
199 do_shutdown (void *cls,
200 const struct GNUNET_SCHEDULER_TaskContext *tc)
204 GNUNET_IDENTITY_ego_lookup_cancel (el);
209 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
214 GNUNET_NAMESTORE_cancel (add_qe);
219 GNUNET_NAMESTORE_cancel (list_qe);
222 if (NULL != add_qe_uri)
224 GNUNET_NAMESTORE_cancel (add_qe_uri);
229 GNUNET_NAMESTORE_cancel (del_qe);
234 GNUNET_NAMESTORE_disconnect (ns);
237 memset (&zone_pkey, 0, sizeof (zone_pkey));
245 GNUNET_NAMESTORE_zone_monitor_stop (zm);
257 * Check if we are finished, and if so, perform shutdown.
262 if ( (NULL == add_qe) &&
264 (NULL == add_qe_uri) &&
266 (NULL == reverse_qe) &&
268 GNUNET_SCHEDULER_shutdown ();
273 * Continuation called to notify client about result of the
276 * @param cls closure, location of the QueueEntry pointer to NULL out
277 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
278 * #GNUNET_NO if content was already there
279 * #GNUNET_YES (or other positive value) on success
280 * @param emsg NULL on success, otherwise an error message
283 add_continuation (void *cls,
287 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
290 if (GNUNET_YES != success)
293 _("Adding record failed: %s\n"),
294 (GNUNET_NO == success) ? "record exists" : emsg);
295 if (GNUNET_NO != success)
303 * Continuation called to notify client about result of the
306 * @param cls closure, unused
307 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
308 * #GNUNET_NO if content was already there
309 * #GNUNET_YES (or other positive value) on success
310 * @param emsg NULL on success, otherwise an error message
313 del_continuation (void *cls,
318 if (success != GNUNET_YES)
320 _("Deleting record failed: %s\n"),
327 * Process a record that was stored in the namestore.
330 * @param zone_key private key of the zone
331 * @param name 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_NAMESTORE_RecordData *rd)
342 const char *typestring;
346 struct GNUNET_TIME_Absolute at;
347 struct GNUNET_TIME_Relative rt;
358 for (i=0;i<rd_len;i++)
360 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
361 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
366 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
367 (unsigned int) rd[i].record_type);
370 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
372 rt.rel_value_us = rd[i].expiration_time;
373 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
377 at.abs_value_us = rd[i].expiration_time;
378 ets = GNUNET_STRINGS_absolute_time_to_string (at);
387 FPRINTF (stdout, "%s", "\n");
388 GNUNET_NAMESTORE_zone_iterator_next (list_it);
393 * Function called once we are in sync in monitor mode.
400 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
405 * We're storing a record; this function is given the existing record
406 * so that we can merge the information.
408 * @param cls closure, unused
409 * @param zone_key private key of the zone
410 * @param rec_name name that is being mapped (at most 255 characters long)
411 * @param rd_count number of entries in @a rd array
412 * @param rd array of records with data to store
415 get_existing_record (void *cls,
416 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
417 const char *rec_name,
418 unsigned int rd_count,
419 const struct GNUNET_NAMESTORE_RecordData *rd)
421 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
422 struct GNUNET_NAMESTORE_RecordData *rde;
424 if ( (NULL != zone_key) &&
425 (0 != strcmp (rec_name, name)) )
427 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
430 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
431 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
432 /* FIXME: should add some logic to overwrite records if there
433 can only be one record of a particular type, and to check
434 if the combination of records is valid to begin with... */
437 rde->data_size = data_size;
438 rde->record_type = type;
440 rde->flags |= GNUNET_NAMESTORE_RF_SHADOW_RECORD;
442 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
443 if (GNUNET_YES == etime_is_rel)
445 rde->expiration_time = etime_rel.rel_value_us;
446 rde->flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
448 else if (GNUNET_NO == etime_is_rel)
449 rde->expiration_time = etime_abs.abs_value_us;
451 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
452 GNUNET_assert (NULL != name);
453 add_qe = GNUNET_NAMESTORE_records_store (ns,
460 /* only cancel if we were not told that this
461 was the end of the iteration already */
462 if (NULL != rec_name)
463 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
470 * Process a record that was stored in the namestore in a block.
472 * @param cls closure, NULL
473 * @param rd_len number of entries in @a rd array
474 * @param rd array of records with data to store
477 display_records_from_block (void *cls,
479 const struct GNUNET_NAMESTORE_RecordData *rd)
481 const char *typestring;
488 _("No records found for `%s'"),
495 for (i=0;i<rd_len;i++)
497 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
498 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
503 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
504 (unsigned int) rd[i].record_type);
513 FPRINTF (stdout, "%s", "\n");
518 * Display block obtained from listing (by name).
521 * @param block NULL if not found
524 handle_block (void *cls,
525 const struct GNUNET_NAMESTORE_Block *block)
527 struct GNUNET_CRYPTO_EcdsaPublicKey zone_pubkey;
530 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
535 "No matching block found\n");
537 else if (GNUNET_OK !=
538 GNUNET_NAMESTORE_block_decrypt (block,
541 &display_records_from_block,
545 "Failed to decrypt block!\n");
552 * Function called with the result of our attempt to obtain a name for a given
556 * @param zone private key of the zone; NULL on disconnect
557 * @param label label of the records; NULL on disconnect
558 * @param rd_count number of entries in @a rd array, 0 if label was deleted
559 * @param rd array of records with data to store
562 handle_reverse_lookup (void *cls,
563 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
565 unsigned int rd_count,
566 const struct GNUNET_NAMESTORE_RecordData *rd)
582 * Function called with the result from the check if the namestore
583 * service is actually running. If it is, we start the actual
586 * @param cls closure with our configuration
587 * @param result #GNUNET_YES if the namestore service is running
590 testservice_task (void *cls,
593 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
594 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
595 struct GNUNET_NAMESTORE_RecordData rd;
597 if (GNUNET_YES != result)
599 FPRINTF (stderr, _("Service `%s' is not running\n"),
603 if (! (add|del|list|(NULL != uri)|(NULL != reverse_pkey)) )
605 /* nothing more to be done */
607 _("No options given\n"));
608 GNUNET_SCHEDULER_shutdown ();
611 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
614 ns = GNUNET_NAMESTORE_connect (cfg);
617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
618 _("Failed to connect to namestore\n"));
626 _("Missing option `%s' for operation `%s'\n"),
628 GNUNET_SCHEDULER_shutdown ();
632 if (NULL == typestring)
635 _("Missing option `%s' for operation `%s'\n"),
637 GNUNET_SCHEDULER_shutdown ();
641 type = GNUNET_NAMESTORE_typename_to_number (typestring);
642 if (UINT32_MAX == type)
644 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
645 GNUNET_SCHEDULER_shutdown ();
652 _("Missing option `%s' for operation `%s'\n"),
655 GNUNET_SCHEDULER_shutdown ();
659 GNUNET_NAMESTORE_string_to_value (type,
664 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
667 GNUNET_SCHEDULER_shutdown ();
671 if (NULL == expirationstring)
674 _("Missing option `%s' for operation `%s'\n"),
676 GNUNET_SCHEDULER_shutdown ();
680 if (0 == strcmp (expirationstring, "never"))
682 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
683 etime_is_rel = GNUNET_NO;
685 else if (GNUNET_OK ==
686 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
689 etime_is_rel = GNUNET_YES;
691 else if (GNUNET_OK ==
692 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
695 etime_is_rel = GNUNET_NO;
700 _("Invalid time format `%s'\n"),
702 GNUNET_SCHEDULER_shutdown ();
706 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
708 &get_existing_record,
716 _("Missing option `%s' for operation `%s'\n"),
718 GNUNET_SCHEDULER_shutdown ();
722 del_qe = GNUNET_NAMESTORE_records_store (ns,
733 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
740 struct GNUNET_HashCode query;
741 struct GNUNET_CRYPTO_EcdsaPublicKey zone_pubkey;
743 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
745 GNUNET_NAMESTORE_query_from_public_key (&zone_pubkey,
748 list_qe = GNUNET_NAMESTORE_lookup_block (ns,
754 if (NULL != reverse_pkey)
756 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
759 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
760 strlen (reverse_pkey),
764 _("Invalid public key for reverse lookup `%s'\n"),
766 GNUNET_SCHEDULER_shutdown ();
768 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
771 &handle_reverse_lookup,
778 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
780 if ( (2 != (sscanf (uri,
781 "gnunet://gns/%104s/%63s",
785 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
788 _("Invalid URI `%s'\n"),
790 GNUNET_SCHEDULER_shutdown ();
794 memset (&rd, 0, sizeof (rd));
796 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
797 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
798 if (GNUNET_YES == etime_is_rel)
800 rd.expiration_time = etime_rel.rel_value_us;
801 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
803 else if (GNUNET_NO == etime_is_rel)
804 rd.expiration_time = etime_abs.abs_value_us;
806 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
808 rd.flags |= GNUNET_NAMESTORE_RF_SHADOW_RECORD;
809 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
819 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
829 * Callback invoked from identity service with ego information.
830 * An @a ego of NULL means the ego was not found.
832 * @param cls closure with the configuration
833 * @param ego an ego known to identity service, or NULL
836 identity_cb (void *cls,
837 const struct GNUNET_IDENTITY_Ego *ego)
839 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
845 _("Ego `%s' not known to identity service\n"),
847 GNUNET_SCHEDULER_shutdown ();
850 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
851 GNUNET_free (ego_name);
853 GNUNET_CLIENT_service_test ("namestore", cfg,
854 GNUNET_TIME_UNIT_SECONDS,
861 * Main function that will be run.
864 * @param args remaining command-line arguments
865 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
866 * @param cfg configuration
869 run (void *cls, char *const *args, const char *cfgfile,
870 const struct GNUNET_CONFIGURATION_Handle *cfg)
872 if (NULL == ego_name)
875 _("You must specify which zone should be accessed\n"));
878 if ( (NULL != args[0]) && (NULL == uri) )
879 uri = GNUNET_strdup (args[0]);
880 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
882 el = GNUNET_IDENTITY_ego_lookup (cfg,
890 * The main function for gnunet-namestore.
892 * @param argc number of arguments from the command line
893 * @param argv command line arguments
894 * @return 0 ok, 1 on error
897 main (int argc, char *const *argv)
901 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
903 gettext_noop ("add record"), 0,
904 &GNUNET_GETOPT_set_one, &add},
905 {'d', "delete", NULL,
906 gettext_noop ("delete record"), 0,
907 &GNUNET_GETOPT_set_one, &del},
908 {'D', "display", NULL,
909 gettext_noop ("display records"), 0,
910 &GNUNET_GETOPT_set_one, &list},
911 {'e', "expiration", "TIME",
912 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
913 &GNUNET_GETOPT_set_string, &expirationstring},
914 {'m', "monitor", NULL,
915 gettext_noop ("monitor changes in the namestore"), 0,
916 &GNUNET_GETOPT_set_one, &monitor},
917 {'n', "name", "NAME",
918 gettext_noop ("name of the record to add/delete/display"), 1,
919 &GNUNET_GETOPT_set_string, &name},
920 {'r', "reverse", "PKEY",
921 gettext_noop ("determine our name for the given PKEY"), 1,
922 &GNUNET_GETOPT_set_string, &reverse_pkey},
923 {'t', "type", "TYPE",
924 gettext_noop ("type of the record to add/delete/display"), 1,
925 &GNUNET_GETOPT_set_string, &typestring},
927 gettext_noop ("URI to import into our zone"), 1,
928 &GNUNET_GETOPT_set_string, &uri},
929 {'V', "value", "VALUE",
930 gettext_noop ("value of the record to add/delete"), 1,
931 &GNUNET_GETOPT_set_string, &value},
932 {'p', "public", NULL,
933 gettext_noop ("create or list public record"), 0,
934 &GNUNET_GETOPT_set_one, &public},
935 {'s', "shadow", NULL,
936 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
937 &GNUNET_GETOPT_set_one, &shadow},
939 gettext_noop ("name of the ego controlling the zone"), 1,
940 &GNUNET_GETOPT_set_string, &ego_name},
941 GNUNET_GETOPT_OPTION_END
944 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
947 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
949 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
950 _("GNUnet zone manipulation tool"),
954 GNUNET_free ((void*) argv);
955 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
958 GNUNET_free ((void*) argv);
959 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
963 /* end of gnunet-namestore.c */