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 (GNUNET_NO == success)
306 _("Deleting record failed, record does not exist%s%s\n"),
307 (NULL != emsg) ? ": " : "",
308 (NULL != emsg) ? emsg : "");
310 if (GNUNET_SYSERR == success)
313 _("Deleting record failed%s%s\n"),
314 (NULL != emsg) ? ": " : "",
315 (NULL != emsg) ? emsg : "");
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);
382 "\t%s: %s (%s)\t%s\t%s\t%s\n",
386 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE)) ? "PRIVATE" : "PUBLIC",
387 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)) ? "SHADOW" : "",
388 (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PENDING)) ? "PENDING" : "");
391 FPRINTF (stdout, "%s", "\n");
392 GNUNET_NAMESTORE_zone_iterator_next (list_it);
397 * Function called once we are in sync in monitor mode.
404 FPRINTF (stdout, "%s", "Monitor is now in sync.\n");
409 * We're storing a record; this function is given the existing record
410 * so that we can merge the information.
412 * @param cls closure, unused
413 * @param zone_key private key of the zone
414 * @param rec_name name that is being mapped (at most 255 characters long)
415 * @param rd_count number of entries in @a rd array
416 * @param rd array of records with data to store
419 get_existing_record (void *cls,
420 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
421 const char *rec_name,
422 unsigned int rd_count,
423 const struct GNUNET_GNSRECORD_Data *rd)
425 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
426 struct GNUNET_GNSRECORD_Data *rde;
429 if ( (NULL != zone_key) &&
430 (0 != strcmp (rec_name, name)) )
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u records for name `%s'\n",
439 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data));
440 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
441 /* FIXME: should add some logic to overwrite records if there
442 can only be one record of a particular type, and to check
443 if the combination of records is valid to begin with... */
446 rde->data_size = data_size;
447 rde->record_type = type;
449 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
451 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
452 if (GNUNET_YES == etime_is_rel)
454 rde->expiration_time = etime_rel.rel_value_us;
455 rde->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
457 else if (GNUNET_NO == etime_is_rel)
458 rde->expiration_time = etime_abs.abs_value_us;
460 rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
461 GNUNET_assert (NULL != name);
462 add_qe = GNUNET_NAMESTORE_records_store (ns,
473 * Function called with the result of our attempt to obtain a name for a given
477 * @param zone private key of the zone; NULL on disconnect
478 * @param label label of the records; NULL on disconnect
479 * @param rd_count number of entries in @a rd array, 0 if label was deleted
480 * @param rd array of records with data to store
483 handle_reverse_lookup (void *cls,
484 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
486 unsigned int rd_count,
487 const struct GNUNET_GNSRECORD_Data *rd)
503 * Function called with the result from the check if the namestore
504 * service is actually running. If it is, we start the actual
507 * @param cls closure with our configuration
508 * @param result #GNUNET_YES if the namestore service is running
511 testservice_task (void *cls,
514 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
515 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
516 struct GNUNET_GNSRECORD_Data rd;
518 if (GNUNET_YES != result)
520 FPRINTF (stderr, _("Service `%s' is not running\n"),
524 if (! (add|del|list|(NULL != uri)|(NULL != reverse_pkey)) )
526 /* nothing more to be done */
528 _("No options given\n"));
529 GNUNET_SCHEDULER_shutdown ();
532 GNUNET_CRYPTO_ecdsa_key_get_public (&zone_pkey,
535 ns = GNUNET_NAMESTORE_connect (cfg);
538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
539 _("Failed to connect to namestore\n"));
547 _("Missing option `%s' for operation `%s'\n"),
549 GNUNET_SCHEDULER_shutdown ();
553 if (NULL == typestring)
556 _("Missing option `%s' for operation `%s'\n"),
558 GNUNET_SCHEDULER_shutdown ();
562 type = GNUNET_GNSRECORD_typename_to_number (typestring);
563 if (UINT32_MAX == type)
565 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
566 GNUNET_SCHEDULER_shutdown ();
573 _("Missing option `%s' for operation `%s'\n"),
576 GNUNET_SCHEDULER_shutdown ();
580 GNUNET_GNSRECORD_string_to_value (type,
585 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
588 GNUNET_SCHEDULER_shutdown ();
592 if (NULL == expirationstring)
595 _("Missing option `%s' for operation `%s'\n"),
597 GNUNET_SCHEDULER_shutdown ();
601 if (0 == strcmp (expirationstring, "never"))
603 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
604 etime_is_rel = GNUNET_NO;
606 else if (GNUNET_OK ==
607 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
610 etime_is_rel = GNUNET_YES;
612 else if (GNUNET_OK ==
613 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
616 etime_is_rel = GNUNET_NO;
621 _("Invalid time format `%s'\n"),
623 GNUNET_SCHEDULER_shutdown ();
627 add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name,
628 &get_existing_record, NULL );
635 _("Missing option `%s' for operation `%s'\n"),
637 GNUNET_SCHEDULER_shutdown ();
641 del_qe = GNUNET_NAMESTORE_records_store (ns,
650 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
655 if (NULL != reverse_pkey)
657 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
660 GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_pkey,
661 strlen (reverse_pkey),
665 _("Invalid public key for reverse lookup `%s'\n"),
667 GNUNET_SCHEDULER_shutdown ();
669 reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns,
672 &handle_reverse_lookup,
679 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
681 if ( (2 != (sscanf (uri,
682 "gnunet://gns/%104s/%63s",
686 GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) )
689 _("Invalid URI `%s'\n"),
691 GNUNET_SCHEDULER_shutdown ();
695 memset (&rd, 0, sizeof (rd));
697 rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
698 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
699 if (GNUNET_YES == etime_is_rel)
701 rd.expiration_time = etime_rel.rel_value_us;
702 rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
704 else if (GNUNET_NO == etime_is_rel)
705 rd.expiration_time = etime_abs.abs_value_us;
707 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
709 rd.flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
710 add_qe_uri = GNUNET_NAMESTORE_records_store (ns,
720 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
731 * Callback invoked from identity service with ego information.
732 * An @a ego of NULL means the ego was not found.
734 * @param cls closure with the configuration
735 * @param ego an ego known to identity service, or NULL
738 identity_cb (void *cls,
739 const struct GNUNET_IDENTITY_Ego *ego)
741 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
747 _("Ego `%s' not known to identity service\n"),
749 GNUNET_SCHEDULER_shutdown ();
752 zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
753 GNUNET_free (ego_name);
755 GNUNET_CLIENT_service_test ("namestore", cfg,
756 GNUNET_TIME_UNIT_SECONDS,
763 * Main function that will be run.
766 * @param args remaining command-line arguments
767 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
768 * @param cfg configuration
771 run (void *cls, char *const *args, const char *cfgfile,
772 const struct GNUNET_CONFIGURATION_Handle *cfg)
774 if (NULL == ego_name)
777 _("You must specify which zone should be accessed\n"));
780 if ( (NULL != args[0]) && (NULL == uri) )
781 uri = GNUNET_strdup (args[0]);
782 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
784 el = GNUNET_IDENTITY_ego_lookup (cfg,
792 * The main function for gnunet-namestore.
794 * @param argc number of arguments from the command line
795 * @param argv command line arguments
796 * @return 0 ok, 1 on error
799 main (int argc, char *const *argv)
804 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
806 gettext_noop ("add record"), 0,
807 &GNUNET_GETOPT_set_one, &add},
808 {'d', "delete", NULL,
809 gettext_noop ("delete record"), 0,
810 &GNUNET_GETOPT_set_one, &del},
811 {'D', "display", NULL,
812 gettext_noop ("display records"), 0,
813 &GNUNET_GETOPT_set_one, &list},
814 {'e', "expiration", "TIME",
815 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
816 &GNUNET_GETOPT_set_string, &expirationstring},
817 {'m', "monitor", NULL,
818 gettext_noop ("monitor changes in the namestore"), 0,
819 &GNUNET_GETOPT_set_one, &monitor},
820 {'n', "name", "NAME",
821 gettext_noop ("name of the record to add/delete/display"), 1,
822 &GNUNET_GETOPT_set_string, &name},
823 {'r', "reverse", "PKEY",
824 gettext_noop ("determine our name for the given PKEY"), 1,
825 &GNUNET_GETOPT_set_string, &reverse_pkey},
826 {'t', "type", "TYPE",
827 gettext_noop ("type of the record to add/delete/display"), 1,
828 &GNUNET_GETOPT_set_string, &typestring},
830 gettext_noop ("URI to import into our zone"), 1,
831 &GNUNET_GETOPT_set_string, &uri},
832 {'V', "value", "VALUE",
833 gettext_noop ("value of the record to add/delete"), 1,
834 &GNUNET_GETOPT_set_string, &value},
835 {'p', "public", NULL,
836 gettext_noop ("create or list public record"), 0,
837 &GNUNET_GETOPT_set_one, &public},
838 {'s', "shadow", NULL,
839 gettext_noop ("create shadow record (only valid if all other records of the same type have expired"), 0,
840 &GNUNET_GETOPT_set_one, &shadow},
842 gettext_noop ("name of the ego controlling the zone"), 1,
843 &GNUNET_GETOPT_set_string, &ego_name},
844 GNUNET_GETOPT_OPTION_END
847 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
850 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
852 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
853 _("GNUnet zone manipulation tool"),
857 GNUNET_free ((void*) argv);
858 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
861 GNUNET_free ((void*) argv);
862 GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
866 /* end of gnunet-namestore.c */