2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file gnunet-namestore.c
22 * @brief command line tool to manipulate the local zone
23 * @author Christian Grothoff
26 * - allow users to set record options (not just 'RF_AUTHORITY')
28 * - add options to list/lookup individual records
31 #include <gnunet_util_lib.h>
32 #include <gnunet_dnsparser_lib.h>
33 #include <gnunet_namestore_service.h>
37 * Hostkey generation context
39 struct GNUNET_CRYPTO_EccKeyGenerationContext * keygen;
42 * Handle to the namestore.
44 static struct GNUNET_NAMESTORE_Handle *ns;
47 * Hash of the public key of our zone.
49 static struct GNUNET_CRYPTO_ShortHashCode zone;
52 * Private key for the our zone.
54 static struct GNUNET_CRYPTO_EccPrivateKey *zone_pkey;
57 * Keyfile to manipulate.
62 * Desired action is to add a record.
67 * Queue entry for the 'add' operation.
69 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
72 * Queue entry for the 'add-uri' operation.
74 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
77 * Desired action is to list records.
82 * List iterator for the 'list' operation.
84 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
87 * Desired action is to remove a record.
99 static int nonauthority;
102 * Queue entry for the 'del' operation.
104 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
107 * Name of the records to add/list/remove.
112 * Value of the record to add/remove.
122 * Type of the record to add/remove, NULL to remove all.
124 static char *typestring;
127 * Desired expiration time.
129 static char *expirationstring;
132 * Global return value
137 * Type string converted to DNS type value.
139 static uint32_t type;
142 * Value in binary format.
147 * Number of bytes in 'data'.
149 static size_t data_size;
152 * Expirationstring converted to relative time.
154 static struct GNUNET_TIME_Relative etime_rel;
157 * Expirationstring converted to absolute time.
159 static struct GNUNET_TIME_Absolute etime_abs;
162 * Is expiration time relative or absolute time?
164 static int etime_is_rel = GNUNET_SYSERR;
168 * Task run on shutdown. Cleans up everything.
171 * @param tc scheduler context
174 do_shutdown (void *cls,
175 const struct GNUNET_SCHEDULER_TaskContext *tc)
179 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
185 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
190 GNUNET_NAMESTORE_cancel (add_qe);
193 if (NULL != add_qe_uri)
195 GNUNET_NAMESTORE_cancel (add_qe_uri);
200 GNUNET_NAMESTORE_cancel (del_qe);
205 GNUNET_NAMESTORE_disconnect (ns);
208 if (NULL != zone_pkey)
210 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
222 * Continuation called to notify client about result of the
225 * @param cls closure, location of the QueueEntry pointer to NULL out
226 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
227 * GNUNET_NO if content was already there
228 * GNUNET_YES (or other positive value) on success
229 * @param emsg NULL on success, otherwise an error message
232 add_continuation (void *cls,
236 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
239 if (GNUNET_YES != success)
242 _("Adding record failed: %s\n"),
243 (GNUNET_NO == success) ? "record exists" : emsg);
244 if (GNUNET_NO != success)
247 if ( (NULL == add_qe) &&
248 (NULL == add_qe_uri) &&
251 GNUNET_SCHEDULER_shutdown ();
256 * Continuation called to notify client about result of the
259 * @param cls closure, unused
260 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
261 * GNUNET_NO if content was already there
262 * GNUNET_YES (or other positive value) on success
263 * @param emsg NULL on success, otherwise an error message
266 del_continuation (void *cls,
271 if (success != GNUNET_YES)
273 _("Deleting record failed: %s\n"),
275 if ( (NULL == add_qe) &&
276 (NULL == add_qe_uri) &&
278 GNUNET_SCHEDULER_shutdown ();
283 * Process a record that was stored in the namestore.
286 * @param zone_key public key of the zone
287 * @param expire when does the corresponding block in the DHT expire (until
288 * when should we never do a DHT lookup for the same name again)?;
289 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
290 * or the expiration time of the block in the namestore (even if there are zero
291 * records matching the desired record type)
292 * @param name name that is being mapped (at most 255 characters long)
293 * @param rd_len number of entries in 'rd' array
294 * @param rd array of records with data to store
295 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
296 * because the user queried for a particular record type only)
299 display_record (void *cls,
300 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
301 struct GNUNET_TIME_Absolute expire,
304 const struct GNUNET_NAMESTORE_RecordData *rd,
305 const struct GNUNET_CRYPTO_EccSignature *signature)
307 const char *typestring;
311 struct GNUNET_TIME_Absolute aex;
312 struct GNUNET_TIME_Relative rex;
317 if ( (NULL == del_qe) &&
318 (NULL == add_qe_uri) &&
320 GNUNET_SCHEDULER_shutdown ();
326 for (i=0;i<rd_len;i++)
328 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
329 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
334 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
335 (unsigned int) rd[i].record_type);
338 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
340 rex.rel_value = rd[i].expiration_time;
341 etime = GNUNET_STRINGS_relative_time_to_string (rex, GNUNET_YES);
345 aex.abs_value = rd[i].expiration_time;
346 etime = GNUNET_STRINGS_absolute_time_to_string (aex);
348 FPRINTF (stdout, "\t%s: %s (%s %s)\n", typestring, s,
349 (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
350 ? _(/* what follows is relative expiration */ "for at least")
351 : _(/* what follows is absolute expiration */ "until"),
355 FPRINTF (stdout, "%s", "\n");
356 GNUNET_NAMESTORE_zone_iterator_next (list_it);
361 * We're storing a record; this function is given the existing record
362 * so that we can merge the information.
364 * @param cls closure, unused
365 * @param zone_key public key of the zone
366 * @param freshness when does the corresponding block in the DHT expire (until
367 * when should we never do a DHT lookup for the same name again)?;
368 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
369 * or the expiration time of the block in the namestore (even if there are zero
370 * records matching the desired record type)
371 * @param name name that is being mapped (at most 255 characters long)
372 * @param rd_count number of entries in 'rd' array
373 * @param rd array of records with data to store
374 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
375 * because the user queried for a particular record type only)
378 get_existing_record (void *cls,
379 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
380 struct GNUNET_TIME_Absolute freshness,
382 unsigned int rd_count,
383 const struct GNUNET_NAMESTORE_RecordData *rd,
384 const struct GNUNET_CRYPTO_EccSignature *signature)
386 struct GNUNET_NAMESTORE_RecordData rdn[rd_count + 1];
387 struct GNUNET_NAMESTORE_RecordData *rde;
390 memset (rdn, 0, sizeof (struct GNUNET_NAMESTORE_RecordData));
391 memcpy (&rdn[1], rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
392 /* FIXME: should add some logic to overwrite records if there
393 can only be one record of a particular type, and to check
394 if the combination of records is valid to begin with... */
397 rde->data_size = data_size;
398 rde->record_type = type;
399 if (GNUNET_YES == etime_is_rel)
401 rde->expiration_time = etime_rel.rel_value;
402 rde->flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
404 else if (GNUNET_NO == etime_is_rel)
406 rde->expiration_time = etime_abs.abs_value;
408 if (1 != nonauthority)
409 rde->flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
411 rde->flags |= GNUNET_NAMESTORE_RF_PRIVATE;
413 add_qe = GNUNET_NAMESTORE_record_put_by_authority (ns,
424 * Function called with the result of the ECC key generation.
426 * @param cls our configuration
427 * @param pk our private key, NULL on failure
428 * @param emsg NULL on success, otherwise error message
431 key_generation_cb (void *cls,
432 struct GNUNET_CRYPTO_EccPrivateKey *pk,
435 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
436 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
437 struct GNUNET_NAMESTORE_RecordData rd;
442 GNUNET_SCHEDULER_shutdown ();
447 if (! (add|del|list|(NULL != uri)))
449 /* nothing more to be done */
451 _("No options given\n"));
452 GNUNET_CRYPTO_ecc_key_free (zone_pkey);
456 if (NULL == zone_pkey)
458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
459 _("Failed to read or create private zone key\n"));
462 GNUNET_CRYPTO_ecc_key_get_public (zone_pkey,
464 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
466 ns = GNUNET_NAMESTORE_connect (cfg);
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
470 _("Failed to connect to namestore\n"));
473 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
480 _("Missing option `%s' for operation `%s'\n"),
482 GNUNET_SCHEDULER_shutdown ();
486 if (NULL == typestring)
489 _("Missing option `%s' for operation `%s'\n"),
491 GNUNET_SCHEDULER_shutdown ();
495 type = GNUNET_NAMESTORE_typename_to_number (typestring);
496 if (UINT32_MAX == type)
498 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
499 GNUNET_SCHEDULER_shutdown ();
506 _("Missing option `%s' for operation `%s'\n"),
509 GNUNET_SCHEDULER_shutdown ();
513 GNUNET_NAMESTORE_string_to_value (type,
518 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
521 GNUNET_SCHEDULER_shutdown ();
525 if (NULL == expirationstring)
528 _("Missing option `%s' for operation `%s'\n"),
530 GNUNET_SCHEDULER_shutdown ();
534 if (0 == strcmp (expirationstring, "never"))
536 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
537 etime_is_rel = GNUNET_NO;
539 else if (GNUNET_OK ==
540 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
543 etime_is_rel = GNUNET_YES;
545 else if (GNUNET_OK ==
546 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
549 etime_is_rel = GNUNET_NO;
554 _("Invalid time format `%s'\n"),
556 GNUNET_SCHEDULER_shutdown ();
560 add_qe = GNUNET_NAMESTORE_lookup_record (ns,
564 &get_existing_record,
572 _("Missing option `%s' for operation `%s'\n"),
574 GNUNET_SCHEDULER_shutdown ();
578 del_qe = GNUNET_NAMESTORE_record_put_by_authority (ns,
587 uint32_t must_not_flags = 0;
589 if (1 == nonauthority) /* List non-authority records */
590 must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
593 must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
595 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
597 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION,
606 struct GNUNET_CRYPTO_ShortHashCode sc;
608 if ( (2 != (sscanf (uri,
609 "gnunet://gns/%52s/%63s",
613 GNUNET_CRYPTO_short_hash_from_string (sh, &sc)) )
616 _("Invalid URI `%s'\n"),
618 GNUNET_SCHEDULER_shutdown ();
622 memset (&rd, 0, sizeof (rd));
624 rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
625 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
626 if (GNUNET_YES == etime_is_rel)
628 rd.expiration_time = etime_rel.rel_value;
629 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
631 else if (GNUNET_NO == etime_is_rel)
632 rd.expiration_time = etime_abs.abs_value;
634 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
635 if (1 != nonauthority)
636 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
637 add_qe_uri = GNUNET_NAMESTORE_record_put_by_authority (ns,
645 GNUNET_free_non_null (data);
650 * Function called with the result from the check if the namestore
651 * service is actually running. If it is, we start the actual
654 * @param cls closure with our configuration
655 * @param result GNUNET_YES if the namestore service is running
658 testservice_task (void *cls,
661 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
663 if (GNUNET_YES != result)
665 FPRINTF (stderr, _("Service `%s' is not running\n"),
671 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
672 "ZONEKEY", &keyfile))
674 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
679 _("Using default zone file `%s'\n"),
682 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
683 &key_generation_cb, (void *) cfg);
684 GNUNET_free (keyfile);
688 GNUNET_SCHEDULER_shutdown ();
695 * Main function that will be run.
698 * @param args remaining command-line arguments
699 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
700 * @param cfg configuration
703 run (void *cls, char *const *args, const char *cfgfile,
704 const struct GNUNET_CONFIGURATION_Handle *cfg)
707 if ( (NULL != args[0]) && (NULL == uri) )
708 uri = GNUNET_strdup (args[0]);
710 GNUNET_CLIENT_service_test ("namestore", cfg,
711 GNUNET_TIME_UNIT_SECONDS,
718 * The main function for gnunet-namestore.
720 * @param argc number of arguments from the command line
721 * @param argv command line arguments
722 * @return 0 ok, 1 on error
725 main (int argc, char *const *argv)
730 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
732 gettext_noop ("add record"), 0,
733 &GNUNET_GETOPT_set_one, &add},
734 {'d', "delete", NULL,
735 gettext_noop ("delete record"), 0,
736 &GNUNET_GETOPT_set_one, &del},
737 {'D', "display", NULL,
738 gettext_noop ("display records"), 0,
739 &GNUNET_GETOPT_set_one, &list},
740 {'e', "expiration", "TIME",
741 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
742 &GNUNET_GETOPT_set_string, &expirationstring},
743 {'n', "name", "NAME",
744 gettext_noop ("name of the record to add/delete/display"), 1,
745 &GNUNET_GETOPT_set_string, &name},
746 {'t', "type", "TYPE",
747 gettext_noop ("type of the record to add/delete/display"), 1,
748 &GNUNET_GETOPT_set_string, &typestring},
750 gettext_noop ("URI to import into our zone"), 1,
751 &GNUNET_GETOPT_set_string, &uri},
752 {'V', "value", "VALUE",
753 gettext_noop ("value of the record to add/delete"), 1,
754 &GNUNET_GETOPT_set_string, &value},
755 {'p', "public", NULL,
756 gettext_noop ("create or list public record"), 0,
757 &GNUNET_GETOPT_set_one, &public},
758 {'N', "non-authority", NULL,
759 gettext_noop ("create or list non-authority record"), 0,
760 &GNUNET_GETOPT_set_one, &nonauthority},
761 {'z', "zonekey", "FILENAME",
762 gettext_noop ("filename with the zone key"), 1,
763 &GNUNET_GETOPT_set_string, &keyfile},
764 GNUNET_GETOPT_OPTION_END
767 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
770 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
772 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
773 _("GNUnet zone manipulation tool"),
777 GNUNET_free ((void*) argv);
780 GNUNET_free ((void*) argv);
784 /* end of gnunet-namestore.c */