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;
333 if ( (NULL == del_qe) &&
335 (NULL == add_qe_uri) &&
337 GNUNET_SCHEDULER_shutdown ();
343 for (i=0;i<rd_len;i++)
345 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
346 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
351 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
352 (unsigned int) rd[i].record_type);
361 FPRINTF (stdout, "%s", "\n");
362 GNUNET_NAMESTORE_zone_iterator_next (list_it);
367 * Function called once we are in sync in monitor mode.
374 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
379 * We're storing a record; this function is given the existing record
380 * so that we can merge the information.
382 * @param cls closure, unused
383 * @param zone_key private key of the zone
384 * @param rec_name name that is being mapped (at most 255 characters long)
385 * @param rd_count number of entries in @a rd array
386 * @param rd array of records with data to store
389 get_existing_record (void *cls,
390 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
391 const char *rec_name,
392 unsigned int rd_count,
393 const struct GNUNET_NAMESTORE_RecordData *rd)
395 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
396 struct GNUNET_NAMESTORE_RecordData *rde;
398 if ( (NULL != zone_key) &&
399 (0 != strcmp (rec_name, name)) )
401 GNUNET_NAMESTORE_zone_iterator_next (add_zit);
404 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
405 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
406 /* FIXME: should add some logic to overwrite records if there
407 can only be one record of a particular type, and to check
408 if the combination of records is valid to begin with... */
411 rde->data_size = data_size;
412 rde->record_type = type;
414 rde->flags |= GNUNET_NAMESTORE_RF_SHADOW_RECORD;
416 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
417 GNUNET_assert (NULL != name);
418 add_qe = GNUNET_NAMESTORE_records_store (ns,
425 /* only cancel if we were not told that this
426 was the end of the iteration already */
427 if (NULL != rec_name)
428 GNUNET_NAMESTORE_zone_iteration_stop (add_zit);
435 * Process a record that was stored in the namestore in a block.
437 * @param cls closure, NULL
438 * @param rd_len number of entries in @a rd array
439 * @param rd array of records with data to store
442 display_records_from_block (void *cls,
444 const struct GNUNET_NAMESTORE_RecordData *rd)
446 const char *typestring;
453 _("No records found for `%s'"),
460 for (i=0;i<rd_len;i++)
462 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
463 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
468 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
469 (unsigned int) rd[i].record_type);
478 FPRINTF (stdout, "%s", "\n");
483 * Display block obtained from listing (by name).
486 * @param block NULL if not found
489 handle_block (void *cls,
490 const struct GNUNET_NAMESTORE_Block *block)
492 struct GNUNET_CRYPTO_EccPublicSignKey zone_pubkey;
495 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&zone_pkey,
500 "No matching block found\n");
502 else if (GNUNET_OK !=
503 GNUNET_NAMESTORE_block_decrypt (block,
506 &display_records_from_block,
510 "Failed to decrypt block!\n");
512 if ( (NULL == del_qe) &&
514 (NULL == add_qe_uri) &&
516 GNUNET_SCHEDULER_shutdown ();
521 * Function called with the result from the check if the namestore
522 * service is actually running. If it is, we start the actual
525 * @param cls closure with our configuration
526 * @param result #GNUNET_YES if the namestore service is running
529 testservice_task (void *cls,
532 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
533 struct GNUNET_CRYPTO_EccPublicSignKey pub;
534 struct GNUNET_NAMESTORE_RecordData rd;
536 if (GNUNET_YES != result)
538 FPRINTF (stderr, _("Service `%s' is not running\n"),
542 if (! (add|del|list|(NULL != uri)))
544 /* nothing more to be done */
546 _("No options given\n"));
547 GNUNET_SCHEDULER_shutdown ();
550 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&zone_pkey,
553 ns = GNUNET_NAMESTORE_connect (cfg);
556 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
557 _("Failed to connect to namestore\n"));
565 _("Missing option `%s' for operation `%s'\n"),
567 GNUNET_SCHEDULER_shutdown ();
571 if (NULL == typestring)
574 _("Missing option `%s' for operation `%s'\n"),
576 GNUNET_SCHEDULER_shutdown ();
580 type = GNUNET_NAMESTORE_typename_to_number (typestring);
581 if (UINT32_MAX == type)
583 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
584 GNUNET_SCHEDULER_shutdown ();
591 _("Missing option `%s' for operation `%s'\n"),
594 GNUNET_SCHEDULER_shutdown ();
598 GNUNET_NAMESTORE_string_to_value (type,
603 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
606 GNUNET_SCHEDULER_shutdown ();
610 if (NULL == expirationstring)
613 _("Missing option `%s' for operation `%s'\n"),
615 GNUNET_SCHEDULER_shutdown ();
619 if (0 == strcmp (expirationstring, "never"))
621 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
622 etime_is_rel = GNUNET_NO;
624 else if (GNUNET_OK ==
625 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
628 etime_is_rel = GNUNET_YES;
630 else if (GNUNET_OK ==
631 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
634 etime_is_rel = GNUNET_NO;
639 _("Invalid time format `%s'\n"),
641 GNUNET_SCHEDULER_shutdown ();
645 add_zit = GNUNET_NAMESTORE_zone_iteration_start (ns,
647 &get_existing_record,
655 _("Missing option `%s' for operation `%s'\n"),
657 GNUNET_SCHEDULER_shutdown ();
661 del_qe = GNUNET_NAMESTORE_records_store (ns,
672 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
679 struct GNUNET_HashCode query;
680 struct GNUNET_CRYPTO_EccPublicSignKey zone_pubkey;
682 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&zone_pkey,
684 GNUNET_NAMESTORE_query_from_public_key (&zone_pubkey,
687 list_qe = GNUNET_NAMESTORE_lookup_block (ns,
697 struct GNUNET_CRYPTO_EccPublicSignKey pkey;
699 if ( (2 != (sscanf (uri,
700 "gnunet://gns/%104s/%63s",
704 GNUNET_CRYPTO_ecc_public_sign_key_from_string (sh, strlen (sh), &pkey)) )
707 _("Invalid URI `%s'\n"),
709 GNUNET_SCHEDULER_shutdown ();
713 memset (&rd, 0, sizeof (rd));
715 rd.data_size = sizeof (struct GNUNET_CRYPTO_EccPublicSignKey);
716 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
717 if (GNUNET_YES == etime_is_rel)
719 rd.expiration_time = etime_rel.rel_value_us;
720 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
722 else if (GNUNET_NO == etime_is_rel)
723 rd.expiration_time = etime_abs.abs_value_us;
725 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
727 rd.flags |= GNUNET_NAMESTORE_RF_SHADOW_RECORD;
728 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
738 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
748 * Callback invoked from identity service with ego information.
749 * An @a ego of NULL means the ego was not found.
751 * @param cls closure with the configuration
752 * @param ego an ego known to identity service, or NULL
755 identity_cb (void *cls,
756 const struct GNUNET_IDENTITY_Ego *ego)
758 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
764 _("Ego `%s' not known to identity service\n"),
766 GNUNET_SCHEDULER_shutdown ();
769 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
770 GNUNET_free (ego_name);
772 GNUNET_CLIENT_service_test ("namestore", cfg,
773 GNUNET_TIME_UNIT_SECONDS,
780 * Main function that will be run.
783 * @param args remaining command-line arguments
784 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
785 * @param cfg configuration
788 run (void *cls, char *const *args, const char *cfgfile,
789 const struct GNUNET_CONFIGURATION_Handle *cfg)
791 if (NULL == ego_name)
794 _("You must specify which zone should be accessed\n"));
797 if ( (NULL != args[0]) && (NULL == uri) )
798 uri = GNUNET_strdup (args[0]);
799 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
801 el = GNUNET_IDENTITY_ego_lookup (cfg,
809 * The main function for gnunet-namestore.
811 * @param argc number of arguments from the command line
812 * @param argv command line arguments
813 * @return 0 ok, 1 on error
816 main (int argc, char *const *argv)
820 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
822 gettext_noop ("add record"), 0,
823 &GNUNET_GETOPT_set_one, &add},
824 {'d', "delete", NULL,
825 gettext_noop ("delete record"), 0,
826 &GNUNET_GETOPT_set_one, &del},
827 {'D', "display", NULL,
828 gettext_noop ("display records"), 0,
829 &GNUNET_GETOPT_set_one, &list},
830 {'e', "expiration", "TIME",
831 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
832 &GNUNET_GETOPT_set_string, &expirationstring},
833 {'m', "monitor", NULL,
834 gettext_noop ("monitor changes in the namestore"), 0,
835 &GNUNET_GETOPT_set_one, &monitor},
836 {'n', "name", "NAME",
837 gettext_noop ("name of the record to add/delete/display"), 1,
838 &GNUNET_GETOPT_set_string, &name},
839 {'t', "type", "TYPE",
840 gettext_noop ("type of the record to add/delete/display"), 1,
841 &GNUNET_GETOPT_set_string, &typestring},
843 gettext_noop ("URI to import into our zone"), 1,
844 &GNUNET_GETOPT_set_string, &uri},
845 {'V', "value", "VALUE",
846 gettext_noop ("value of the record to add/delete"), 1,
847 &GNUNET_GETOPT_set_string, &value},
848 {'p', "public", NULL,
849 gettext_noop ("create or list public record"), 0,
850 &GNUNET_GETOPT_set_one, &public},
851 {'s', "shadow", NULL,
852 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
853 &GNUNET_GETOPT_set_one, &shadow},
855 gettext_noop ("name of the ego controlling the zone"), 1,
856 &GNUNET_GETOPT_set_string, &ego_name},
857 GNUNET_GETOPT_OPTION_END
860 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
863 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
865 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
866 _("GNUnet zone manipulation tool"),
870 GNUNET_free ((void*) argv);
871 GNUNET_CRYPTO_ecc_key_clear (&zone_pkey);
874 GNUNET_free ((void*) argv);
875 GNUNET_CRYPTO_ecc_key_clear (&zone_pkey);
879 /* end of gnunet-namestore.c */