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_RsaKeyGenerationContext * 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_RsaPrivateKey *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
138 * Task run on shutdown. Cleans up everything.
141 * @param tc scheduler context
144 do_shutdown (void *cls,
145 const struct GNUNET_SCHEDULER_TaskContext *tc)
149 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
155 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
160 GNUNET_NAMESTORE_cancel (add_qe);
163 if (NULL != add_qe_uri)
165 GNUNET_NAMESTORE_cancel (add_qe_uri);
170 GNUNET_NAMESTORE_cancel (del_qe);
175 GNUNET_NAMESTORE_disconnect (ns);
178 if (NULL != zone_pkey)
180 GNUNET_CRYPTO_rsa_key_free (zone_pkey);
192 * Continuation called to notify client about result of the
195 * @param cls closure, location of the QueueEntry pointer to NULL out
196 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
197 * GNUNET_NO if content was already there
198 * GNUNET_YES (or other positive value) on success
199 * @param emsg NULL on success, otherwise an error message
202 add_continuation (void *cls,
206 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
209 if (GNUNET_YES != success)
212 _("Adding record failed: %s\n"),
213 (GNUNET_NO == success) ? "record exists" : emsg);
214 if (GNUNET_NO != success)
217 if ( (NULL == add_qe) &&
218 (NULL == add_qe_uri) &&
221 GNUNET_SCHEDULER_shutdown ();
226 * Continuation called to notify client about result of the
229 * @param cls closure, unused
230 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
231 * GNUNET_NO if content was already there
232 * GNUNET_YES (or other positive value) on success
233 * @param emsg NULL on success, otherwise an error message
236 del_continuation (void *cls,
241 if (success != GNUNET_YES)
243 _("Deleting record failed: %s\n"),
245 if ( (NULL == add_qe) &&
246 (NULL == add_qe_uri) &&
248 GNUNET_SCHEDULER_shutdown ();
253 * Process a record that was stored in the namestore.
256 * @param zone_key public key of the zone
257 * @param expire when does the corresponding block in the DHT expire (until
258 * when should we never do a DHT lookup for the same name again)?;
259 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
260 * or the expiration time of the block in the namestore (even if there are zero
261 * records matching the desired record type)
262 * @param name name that is being mapped (at most 255 characters long)
263 * @param rd_len number of entries in 'rd' array
264 * @param rd array of records with data to store
265 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
266 * because the user queried for a particular record type only)
269 display_record (void *cls,
270 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
271 struct GNUNET_TIME_Absolute expire,
274 const struct GNUNET_NAMESTORE_RecordData *rd,
275 const struct GNUNET_CRYPTO_RsaSignature *signature)
277 const char *typestring;
281 struct GNUNET_TIME_Absolute aex;
282 struct GNUNET_TIME_Relative rex;
287 if ( (NULL == del_qe) &&
288 (NULL == add_qe_uri) &&
290 GNUNET_SCHEDULER_shutdown ();
296 for (i=0;i<rd_len;i++)
298 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
299 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
304 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
305 (unsigned int) rd[i].record_type);
308 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
310 rex.rel_value = rd[i].expiration_time;
311 etime = GNUNET_STRINGS_relative_time_to_string (rex, GNUNET_YES);
315 aex.abs_value = rd[i].expiration_time;
316 etime = GNUNET_STRINGS_absolute_time_to_string (aex);
318 FPRINTF (stdout, "\t%s: %s (%s %s)\n", typestring, s,
319 (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
320 ? _(/* what follows is relative expiration */ "for at least")
321 : _(/* what follows is absolute expiration */ "until"),
325 FPRINTF (stdout, "%s", "\n");
326 GNUNET_NAMESTORE_zone_iterator_next (list_it);
330 key_generation_cb (void *cls,
331 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
334 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
335 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
338 size_t data_size = 0;
339 struct GNUNET_TIME_Relative etime_rel;
340 struct GNUNET_TIME_Absolute etime_abs;
341 int etime_is_rel = GNUNET_SYSERR;
342 struct GNUNET_NAMESTORE_RecordData rd;
347 GNUNET_SCHEDULER_shutdown ();
352 if (! (add|del|list|(NULL != uri)))
354 /* nothing more to be done */
356 _("No options given\n"));
357 GNUNET_CRYPTO_rsa_key_free (zone_pkey);
361 if (NULL == zone_pkey)
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364 _("Failed to read or create private zone key\n"));
367 GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
369 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
371 ns = GNUNET_NAMESTORE_connect (cfg);
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375 _("Failed to connect to namestore\n"));
378 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
380 if (NULL == typestring)
383 type = GNUNET_NAMESTORE_typename_to_number (typestring);
384 if (UINT32_MAX == type)
386 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
387 GNUNET_SCHEDULER_shutdown ();
391 if ((NULL == typestring) && (add | del))
394 _("Missing option `%s' for operation `%s'\n"),
396 GNUNET_SCHEDULER_shutdown ();
403 GNUNET_NAMESTORE_string_to_value (type,
408 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
411 GNUNET_SCHEDULER_shutdown ();
415 } else if (add | del)
418 _("Missing option `%s' for operation `%s'\n"),
421 GNUNET_SCHEDULER_shutdown ();
424 if (NULL != expirationstring)
426 if (0 == strcmp (expirationstring, "never"))
428 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
429 etime_is_rel = GNUNET_NO;
431 else if (GNUNET_OK ==
432 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
435 etime_is_rel = GNUNET_YES;
437 else if (GNUNET_OK ==
438 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
441 etime_is_rel = GNUNET_NO;
446 _("Invalid time format `%s'\n"),
448 GNUNET_SCHEDULER_shutdown ();
452 if (etime_is_rel && del)
455 _("Deletion requires either absolute time, or no time at all. Got relative time `%s' instead.\n"),
457 GNUNET_SCHEDULER_shutdown ();
465 _("Missing option `%s' for operation `%s'\n"),
467 GNUNET_SCHEDULER_shutdown ();
471 memset (&rd, 0, sizeof (rd));
477 _("Missing option `%s' for operation `%s'\n"),
479 GNUNET_SCHEDULER_shutdown ();
484 rd.data_size = data_size;
485 rd.record_type = type;
486 if (GNUNET_YES == etime_is_rel)
488 rd.expiration_time = etime_rel.rel_value;
489 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
491 else if (GNUNET_NO == etime_is_rel)
492 rd.expiration_time = etime_abs.abs_value;
496 _("No valid expiration time for operation `%s'\n"),
498 GNUNET_SCHEDULER_shutdown ();
502 if (1 != nonauthority)
503 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
505 rd.flags |= GNUNET_NAMESTORE_RF_PRIVATE;
506 add_qe = GNUNET_NAMESTORE_record_create (ns,
518 _("Missing option `%s' for operation `%s'\n"),
520 GNUNET_SCHEDULER_shutdown ();
525 rd.data_size = data_size;
526 rd.record_type = type;
527 rd.expiration_time = 0;
529 rd.expiration_time = etime_abs.abs_value;
530 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
531 del_qe = GNUNET_NAMESTORE_record_remove (ns,
540 uint32_t must_not_flags = 0;
542 if (1 == nonauthority) /* List non-authority records */
543 must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
546 must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
548 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
550 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION,
559 struct GNUNET_CRYPTO_ShortHashCode sc;
561 if ( (2 != (sscanf (uri,
562 "gnunet://gns/%52s/%63s",
566 GNUNET_CRYPTO_short_hash_from_string (sh, &sc)) )
569 _("Invalid URI `%s'\n"),
571 GNUNET_SCHEDULER_shutdown ();
576 rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
577 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
578 if (GNUNET_YES == etime_is_rel)
580 rd.expiration_time = etime_rel.rel_value;
581 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
583 else if (GNUNET_NO == etime_is_rel)
584 rd.expiration_time = etime_abs.abs_value;
586 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
587 if (1 != nonauthority)
588 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
590 add_qe_uri = GNUNET_NAMESTORE_record_create (ns,
597 GNUNET_free_non_null (data);
603 testservice_task (void *cls,
604 const struct GNUNET_SCHEDULER_TaskContext *tc)
606 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
608 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
610 FPRINTF (stderr, _("Service `%s' is not running\n"), "namestore");
617 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
618 "ZONEKEY", &keyfile))
620 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
625 _("Using default zone file `%s'\n"),
628 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, key_generation_cb, cfg);
629 GNUNET_free (keyfile);
633 GNUNET_SCHEDULER_shutdown ();
640 * Main function that will be run.
643 * @param args remaining command-line arguments
644 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
645 * @param cfg configuration
648 run (void *cls, char *const *args, const char *cfgfile,
649 const struct GNUNET_CONFIGURATION_Handle *cfg)
652 if ( (NULL != args[0]) && (NULL == uri) )
653 uri = GNUNET_strdup (args[0]);
655 GNUNET_CLIENT_service_test ("namestore", cfg,
656 GNUNET_TIME_UNIT_SECONDS,
663 * The main function for gnunet-namestore.
665 * @param argc number of arguments from the command line
666 * @param argv command line arguments
667 * @return 0 ok, 1 on error
670 main (int argc, char *const *argv)
675 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
677 gettext_noop ("add record"), 0,
678 &GNUNET_GETOPT_set_one, &add},
679 {'d', "delete", NULL,
680 gettext_noop ("delete record"), 0,
681 &GNUNET_GETOPT_set_one, &del},
682 {'D', "display", NULL,
683 gettext_noop ("display records"), 0,
684 &GNUNET_GETOPT_set_one, &list},
685 {'e', "expiration", "TIME",
686 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
687 &GNUNET_GETOPT_set_string, &expirationstring},
688 {'n', "name", "NAME",
689 gettext_noop ("name of the record to add/delete/display"), 1,
690 &GNUNET_GETOPT_set_string, &name},
691 {'t', "type", "TYPE",
692 gettext_noop ("type of the record to add/delete/display"), 1,
693 &GNUNET_GETOPT_set_string, &typestring},
695 gettext_noop ("URI to import into our zone"), 1,
696 &GNUNET_GETOPT_set_string, &uri},
697 {'V', "value", "VALUE",
698 gettext_noop ("value of the record to add/delete"), 1,
699 &GNUNET_GETOPT_set_string, &value},
700 {'p', "public", NULL,
701 gettext_noop ("create or list public record"), 0,
702 &GNUNET_GETOPT_set_one, &public},
703 {'N', "non-authority", NULL,
704 gettext_noop ("create or list non-authority record"), 0,
705 &GNUNET_GETOPT_set_one, &nonauthority},
706 {'z', "zonekey", "FILENAME",
707 gettext_noop ("filename with the zone key"), 1,
708 &GNUNET_GETOPT_set_string, &keyfile},
709 GNUNET_GETOPT_OPTION_END
712 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
715 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
717 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
718 _("GNUnet zone manipulation tool"),
722 GNUNET_free ((void*) argv);
725 GNUNET_free ((void*) argv);
729 /* end of gnunet-namestore.c */