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
28 * - add options to list/lookup individual records
31 #include <gnunet_util_lib.h>
32 #include <gnunet_dnsparser_lib.h>
33 #include <gnunet_identity_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_EccPrivateKey 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 'list' operation (in combination with a name).
80 static struct GNUNET_NAMESTORE_QueueEntry *list_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_NAMESTORE_RF_PRIVATE)
103 * Is record a shadow record (#GNUNET_NAMESTORE_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 * 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);
210 GNUNET_NAMESTORE_cancel (list_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 * Continuation called to notify client about result of the
251 * @param cls closure, location of the QueueEntry pointer to NULL out
252 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
253 * #GNUNET_NO if content was already there
254 * #GNUNET_YES (or other positive value) on success
255 * @param emsg NULL on success, otherwise an error message
258 add_continuation (void *cls,
262 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
265 if (GNUNET_YES != success)
268 _("Adding record failed: %s\n"),
269 (GNUNET_NO == success) ? "record exists" : emsg);
270 if (GNUNET_NO != success)
273 if ( (NULL == add_qe) &&
275 (NULL == add_qe_uri) &&
278 GNUNET_SCHEDULER_shutdown ();
283 * Continuation called to notify client about result of the
286 * @param cls closure, unused
287 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
288 * #GNUNET_NO if content was already there
289 * #GNUNET_YES (or other positive value) on success
290 * @param emsg NULL on success, otherwise an error message
293 del_continuation (void *cls,
298 if (success != GNUNET_YES)
300 _("Deleting record failed: %s\n"),
302 if ( (NULL == add_qe) &&
304 (NULL == add_qe_uri) &&
306 GNUNET_SCHEDULER_shutdown ();
311 * Process a record that was stored in the namestore.
314 * @param zone_key private key of the zone
315 * @param name name that is being mapped (at most 255 characters long)
316 * @param rd_len number of entries in @a rd array
317 * @param rd array of records with data to store
320 display_record (void *cls,
321 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
324 const struct GNUNET_NAMESTORE_RecordData *rd)
326 const char *typestring;
330 struct GNUNET_TIME_Absolute at;
331 struct GNUNET_TIME_Relative rt;
336 if ( (NULL == del_qe) &&
338 (NULL == add_qe_uri) &&
340 GNUNET_SCHEDULER_shutdown ();
346 for (i=0;i<rd_len;i++)
348 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
349 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
354 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
355 (unsigned int) rd[i].record_type);
358 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
360 rt.rel_value_us = rd[i].expiration_time;
361 ets = GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_YES);
365 at.abs_value_us = rd[i].expiration_time;
366 ets = GNUNET_STRINGS_absolute_time_to_string (at);
375 FPRINTF (stdout, "%s", "\n");
376 GNUNET_NAMESTORE_zone_iterator_next (list_it);
381 * Function called once we are in sync in monitor mode.
388 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
393 * We're storing a record; this function is given the existing record
394 * so that we can merge the information.
396 * @param cls closure, unused
397 * @param zone_key private key of the zone
398 * @param rec_name name that is being mapped (at most 255 characters long)
399 * @param rd_count number of entries in @a rd array
400 * @param rd array of records with data to store
403 get_existing_record (void *cls,
404 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
405 const char *rec_name,
406 unsigned int rd_count,
407 const struct GNUNET_NAMESTORE_RecordData *rd)
409 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
410 struct GNUNET_NAMESTORE_RecordData *rde;
412 if ( (NULL != zone_key) &&
413 (0 != strcmp (rec_name, name)) )
415 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
418 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
419 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
420 /* FIXME: should add some logic to overwrite records if there
421 can only be one record of a particular type, and to check
422 if the combination of records is valid to begin with... */
425 rde->data_size = data_size;
426 rde->record_type = type;
428 rde->flags |= GNUNET_NAMESTORE_RF_SHADOW_RECORD;
430 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
431 if (GNUNET_YES == etime_is_rel)
433 rde->expiration_time = etime_rel.rel_value_us;
434 rde->flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
436 else if (GNUNET_NO == etime_is_rel)
437 rde->expiration_time = etime_abs.abs_value_us;
439 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
440 GNUNET_assert (NULL != name);
441 add_qe = GNUNET_NAMESTORE_records_store (ns,
448 /* only cancel if we were not told that this
449 was the end of the iteration already */
450 if (NULL != rec_name)
451 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
458 * Process a record that was stored in the namestore in a block.
460 * @param cls closure, NULL
461 * @param rd_len number of entries in @a rd array
462 * @param rd array of records with data to store
465 display_records_from_block (void *cls,
467 const struct GNUNET_NAMESTORE_RecordData *rd)
469 const char *typestring;
476 _("No records found for `%s'"),
483 for (i=0;i<rd_len;i++)
485 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
486 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
491 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
492 (unsigned int) rd[i].record_type);
501 FPRINTF (stdout, "%s", "\n");
506 * Display block obtained from listing (by name).
509 * @param block NULL if not found
512 handle_block (void *cls,
513 const struct GNUNET_NAMESTORE_Block *block)
515 struct GNUNET_CRYPTO_EccPublicSignKey zone_pubkey;
518 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&zone_pkey,
523 "No matching block found\n");
525 else if (GNUNET_OK !=
526 GNUNET_NAMESTORE_block_decrypt (block,
529 &display_records_from_block,
533 "Failed to decrypt block!\n");
535 if ( (NULL == del_qe) &&
537 (NULL == add_qe_uri) &&
539 GNUNET_SCHEDULER_shutdown ();
544 * Function called with the result from the check if the namestore
545 * service is actually running. If it is, we start the actual
548 * @param cls closure with our configuration
549 * @param result #GNUNET_YES if the namestore service is running
552 testservice_task (void *cls,
555 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
556 struct GNUNET_CRYPTO_EccPublicSignKey pub;
557 struct GNUNET_NAMESTORE_RecordData rd;
559 if (GNUNET_YES != result)
561 FPRINTF (stderr, _("Service `%s' is not running\n"),
565 if (! (add|del|list|(NULL != uri)))
567 /* nothing more to be done */
569 _("No options given\n"));
570 GNUNET_SCHEDULER_shutdown ();
573 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&zone_pkey,
576 ns = GNUNET_NAMESTORE_connect (cfg);
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 _("Failed to connect to namestore\n"));
588 _("Missing option `%s' for operation `%s'\n"),
590 GNUNET_SCHEDULER_shutdown ();
594 if (NULL == typestring)
597 _("Missing option `%s' for operation `%s'\n"),
599 GNUNET_SCHEDULER_shutdown ();
603 type = GNUNET_NAMESTORE_typename_to_number (typestring);
604 if (UINT32_MAX == type)
606 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
607 GNUNET_SCHEDULER_shutdown ();
614 _("Missing option `%s' for operation `%s'\n"),
617 GNUNET_SCHEDULER_shutdown ();
621 GNUNET_NAMESTORE_string_to_value (type,
626 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
629 GNUNET_SCHEDULER_shutdown ();
633 if (NULL == expirationstring)
636 _("Missing option `%s' for operation `%s'\n"),
638 GNUNET_SCHEDULER_shutdown ();
642 if (0 == strcmp (expirationstring, "never"))
644 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
645 etime_is_rel = GNUNET_NO;
647 else if (GNUNET_OK ==
648 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
651 etime_is_rel = GNUNET_YES;
653 else if (GNUNET_OK ==
654 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
657 etime_is_rel = GNUNET_NO;
662 _("Invalid time format `%s'\n"),
664 GNUNET_SCHEDULER_shutdown ();
668 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
670 &get_existing_record,
678 _("Missing option `%s' for operation `%s'\n"),
680 GNUNET_SCHEDULER_shutdown ();
684 del_qe = GNUNET_NAMESTORE_records_store (ns,
695 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
702 struct GNUNET_HashCode query;
703 struct GNUNET_CRYPTO_EccPublicSignKey zone_pubkey;
705 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&zone_pkey,
707 GNUNET_NAMESTORE_query_from_public_key (&zone_pubkey,
710 list_qe = GNUNET_NAMESTORE_lookup_block (ns,
720 struct GNUNET_CRYPTO_EccPublicSignKey pkey;
722 if ( (2 != (sscanf (uri,
723 "gnunet://gns/%104s/%63s",
727 GNUNET_CRYPTO_ecc_public_sign_key_from_string (sh, strlen (sh), &pkey)) )
730 _("Invalid URI `%s'\n"),
732 GNUNET_SCHEDULER_shutdown ();
736 memset (&rd, 0, sizeof (rd));
738 rd.data_size = sizeof (struct GNUNET_CRYPTO_EccPublicSignKey);
739 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
740 if (GNUNET_YES == etime_is_rel)
742 rd.expiration_time = etime_rel.rel_value_us;
743 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
745 else if (GNUNET_NO == etime_is_rel)
746 rd.expiration_time = etime_abs.abs_value_us;
748 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
750 rd.flags |= GNUNET_NAMESTORE_RF_SHADOW_RECORD;
751 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
761 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
771 * Callback invoked from identity service with ego information.
772 * An @a ego of NULL means the ego was not found.
774 * @param cls closure with the configuration
775 * @param ego an ego known to identity service, or NULL
778 identity_cb (void *cls,
779 const struct GNUNET_IDENTITY_Ego *ego)
781 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
787 _("Ego `%s' not known to identity service\n"),
789 GNUNET_SCHEDULER_shutdown ();
792 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
793 GNUNET_free (ego_name);
795 GNUNET_CLIENT_service_test ("namestore", cfg,
796 GNUNET_TIME_UNIT_SECONDS,
803 * Main function that will be run.
806 * @param args remaining command-line arguments
807 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
808 * @param cfg configuration
811 run (void *cls, char *const *args, const char *cfgfile,
812 const struct GNUNET_CONFIGURATION_Handle *cfg)
814 if (NULL == ego_name)
817 _("You must specify which zone should be accessed\n"));
820 if ( (NULL != args[0]) && (NULL == uri) )
821 uri = GNUNET_strdup (args[0]);
822 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
824 el = GNUNET_IDENTITY_ego_lookup (cfg,
832 * The main function for gnunet-namestore.
834 * @param argc number of arguments from the command line
835 * @param argv command line arguments
836 * @return 0 ok, 1 on error
839 main (int argc, char *const *argv)
843 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
845 gettext_noop ("add record"), 0,
846 &GNUNET_GETOPT_set_one, &add},
847 {'d', "delete", NULL,
848 gettext_noop ("delete record"), 0,
849 &GNUNET_GETOPT_set_one, &del},
850 {'D', "display", NULL,
851 gettext_noop ("display records"), 0,
852 &GNUNET_GETOPT_set_one, &list},
853 {'e', "expiration", "TIME",
854 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
855 &GNUNET_GETOPT_set_string, &expirationstring},
856 {'m', "monitor", NULL,
857 gettext_noop ("monitor changes in the namestore"), 0,
858 &GNUNET_GETOPT_set_one, &monitor},
859 {'n', "name", "NAME",
860 gettext_noop ("name of the record to add/delete/display"), 1,
861 &GNUNET_GETOPT_set_string, &name},
862 {'t', "type", "TYPE",
863 gettext_noop ("type of the record to add/delete/display"), 1,
864 &GNUNET_GETOPT_set_string, &typestring},
866 gettext_noop ("URI to import into our zone"), 1,
867 &GNUNET_GETOPT_set_string, &uri},
868 {'V', "value", "VALUE",
869 gettext_noop ("value of the record to add/delete"), 1,
870 &GNUNET_GETOPT_set_string, &value},
871 {'p', "public", NULL,
872 gettext_noop ("create or list public record"), 0,
873 &GNUNET_GETOPT_set_one, &public},
874 {'s', "shadow", NULL,
875 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
876 &GNUNET_GETOPT_set_one, &shadow},
878 gettext_noop ("name of the ego controlling the zone"), 1,
879 &GNUNET_GETOPT_set_string, &ego_name},
880 GNUNET_GETOPT_OPTION_END
883 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
886 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
888 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
889 _("GNUnet zone manipulation tool"),
893 GNUNET_free ((void*) argv);
894 GNUNET_CRYPTO_ecc_key_clear (&zone_pkey);
897 GNUNET_free ((void*) argv);
898 GNUNET_CRYPTO_ecc_key_clear (&zone_pkey);
902 /* end of gnunet-namestore.c */