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 * Handle to the namestore.
39 static struct GNUNET_NAMESTORE_Handle *ns;
42 * Hash of the public key of our zone.
44 static struct GNUNET_CRYPTO_ShortHashCode zone;
47 * Private key for the our zone.
49 static struct GNUNET_CRYPTO_RsaPrivateKey *zone_pkey;
52 * Keyfile to manipulate.
57 * Desired action is to add a record.
62 * Queue entry for the 'add' operation.
64 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
67 * Queue entry for the 'add-uri' operation.
69 static struct GNUNET_NAMESTORE_QueueEntry *add_qe_uri;
72 * Desired action is to list records.
77 * List iterator for the 'list' operation.
79 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
82 * Desired action is to remove a record.
94 static int nonauthority;
97 * Queue entry for the 'del' operation.
99 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
102 * Name of the records to add/list/remove.
107 * Value of the record to add/remove.
117 * Type of the record to add/remove, NULL to remove all.
119 static char *typestring;
122 * Desired expiration time.
124 static char *expirationstring;
127 * Global return value
133 * Task run on shutdown. Cleans up everything.
136 * @param tc scheduler context
139 do_shutdown (void *cls,
140 const struct GNUNET_SCHEDULER_TaskContext *tc)
144 GNUNET_NAMESTORE_zone_iteration_stop (list_it);
149 GNUNET_NAMESTORE_cancel (add_qe);
152 if (NULL != add_qe_uri)
154 GNUNET_NAMESTORE_cancel (add_qe_uri);
159 GNUNET_NAMESTORE_cancel (del_qe);
164 GNUNET_NAMESTORE_disconnect (ns);
167 if (NULL != zone_pkey)
169 GNUNET_CRYPTO_rsa_key_free (zone_pkey);
181 * Continuation called to notify client about result of the
184 * @param cls closure, location of the QueueEntry pointer to NULL out
185 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
186 * GNUNET_NO if content was already there
187 * GNUNET_YES (or other positive value) on success
188 * @param emsg NULL on success, otherwise an error message
191 add_continuation (void *cls,
195 struct GNUNET_NAMESTORE_QueueEntry **qe = cls;
198 if (GNUNET_YES != success)
201 _("Adding record failed: %s\n"),
202 (GNUNET_NO == success) ? "record exists" : emsg);
203 if (GNUNET_NO != success)
206 if ( (NULL == add_qe) &&
207 (NULL == add_qe_uri) &&
210 GNUNET_SCHEDULER_shutdown ();
215 * Continuation called to notify client about result of the
218 * @param cls closure, unused
219 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
220 * GNUNET_NO if content was already there
221 * GNUNET_YES (or other positive value) on success
222 * @param emsg NULL on success, otherwise an error message
225 del_continuation (void *cls,
230 if (success != GNUNET_YES)
232 _("Deleting record failed: %s\n"),
234 if ( (NULL == add_qe) &&
235 (NULL == add_qe_uri) &&
237 GNUNET_SCHEDULER_shutdown ();
242 * Process a record that was stored in the namestore.
245 * @param zone_key public key of the zone
246 * @param expire when does the corresponding block in the DHT expire (until
247 * when should we never do a DHT lookup for the same name again)?;
248 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
249 * or the expiration time of the block in the namestore (even if there are zero
250 * records matching the desired record type)
251 * @param name name that is being mapped (at most 255 characters long)
252 * @param rd_len number of entries in 'rd' array
253 * @param rd array of records with data to store
254 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
255 * because the user queried for a particular record type only)
258 display_record (void *cls,
259 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
260 struct GNUNET_TIME_Absolute expire,
263 const struct GNUNET_NAMESTORE_RecordData *rd,
264 const struct GNUNET_CRYPTO_RsaSignature *signature)
266 const char *typestring;
270 struct GNUNET_TIME_Absolute aex;
271 struct GNUNET_TIME_Relative rex;
276 if ( (NULL == del_qe) &&
277 (NULL == add_qe_uri) &&
279 GNUNET_SCHEDULER_shutdown ();
285 for (i=0;i<rd_len;i++)
287 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
288 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
293 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
294 (unsigned int) rd[i].record_type);
297 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
299 rex.rel_value = rd[i].expiration_time;
300 etime = GNUNET_STRINGS_relative_time_to_string (rex);
304 aex.abs_value = rd[i].expiration_time;
305 etime = GNUNET_STRINGS_absolute_time_to_string (aex);
307 FPRINTF (stdout, "\t%s: %s (%s %s)\n", typestring, s,
308 (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
309 ? _(/* what follows is relative expiration */ "for at least")
310 : _(/* what follows is absolute expiration */ "until"),
315 FPRINTF (stdout, "%s", "\n");
316 GNUNET_NAMESTORE_zone_iterator_next (list_it);
321 testservice_task (void *cls,
322 const struct GNUNET_SCHEDULER_TaskContext *tc)
324 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
327 size_t data_size = 0;
328 struct GNUNET_TIME_Relative etime_rel;
329 struct GNUNET_TIME_Absolute etime_abs;
330 int etime_is_rel = GNUNET_SYSERR;
331 struct GNUNET_NAMESTORE_RecordData rd;
333 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
335 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
337 FPRINTF (stderr, _("Service `%s' is not running\n"), "namestore");
344 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
345 "ZONEKEY", &keyfile))
348 _("Option `%s' not given, but I need a zone key file!\n"),
353 _("Using default zone file `%s'\n"),
356 zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
357 GNUNET_free (keyfile);
359 if (! (add|del|list|(NULL != uri)))
361 /* nothing more to be done */
363 _("No options given\n"));
364 GNUNET_CRYPTO_rsa_key_free (zone_pkey);
368 if (NULL == zone_pkey)
370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371 _("Failed to read or create private zone key\n"));
374 GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
376 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
378 ns = GNUNET_NAMESTORE_connect (cfg);
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382 _("Failed to connect to namestore\n"));
385 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
387 if (NULL == typestring)
390 type = GNUNET_NAMESTORE_typename_to_number (typestring);
391 if (UINT32_MAX == type)
393 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
394 GNUNET_SCHEDULER_shutdown ();
398 if ((NULL == typestring) && (add | del))
401 _("Missing option `%s' for operation `%s'\n"),
403 GNUNET_SCHEDULER_shutdown ();
410 GNUNET_NAMESTORE_string_to_value (type,
415 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
418 GNUNET_SCHEDULER_shutdown ();
422 } else if (add | del)
425 _("Missing option `%s' for operation `%s'\n"),
428 GNUNET_SCHEDULER_shutdown ();
431 if (NULL != expirationstring)
433 if (0 == strcmp (expirationstring, "never"))
435 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
436 etime_is_rel = GNUNET_NO;
438 else if (GNUNET_OK ==
439 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
442 etime_is_rel = GNUNET_YES;
444 else if (GNUNET_OK ==
445 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
448 etime_is_rel = GNUNET_NO;
453 _("Invalid time format `%s'\n"),
455 GNUNET_SCHEDULER_shutdown ();
459 if (etime_is_rel && del)
462 _("Deletion requires either absolute time, or no time at all. Got relative time `%s' instead.\n"),
464 GNUNET_SCHEDULER_shutdown ();
472 _("Missing option `%s' for operation `%s'\n"),
474 GNUNET_SCHEDULER_shutdown ();
478 memset (&rd, 0, sizeof (rd));
484 _("Missing option `%s' for operation `%s'\n"),
486 GNUNET_SCHEDULER_shutdown ();
491 rd.data_size = data_size;
492 rd.record_type = type;
493 if (GNUNET_YES == etime_is_rel)
495 rd.expiration_time = etime_rel.rel_value;
496 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
498 else if (GNUNET_NO == etime_is_rel)
499 rd.expiration_time = etime_abs.abs_value;
503 _("No valid expiration time for operation `%s'\n"),
505 GNUNET_SCHEDULER_shutdown ();
509 if (1 != nonauthority)
510 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
512 rd.flags |= GNUNET_NAMESTORE_RF_PRIVATE;
513 add_qe = GNUNET_NAMESTORE_record_create (ns,
525 _("Missing option `%s' for operation `%s'\n"),
527 GNUNET_SCHEDULER_shutdown ();
532 rd.data_size = data_size;
533 rd.record_type = type;
534 rd.expiration_time = 0;
536 rd.expiration_time = etime_abs.abs_value;
537 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
538 del_qe = GNUNET_NAMESTORE_record_remove (ns,
547 uint32_t must_not_flags = 0;
549 if (1 == nonauthority) /* List non-authority records */
550 must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
553 must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
555 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
557 GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION,
566 struct GNUNET_CRYPTO_ShortHashCode sc;
568 if ( (2 != (sscanf (uri,
569 "gnunet://gns/%52s/%63s",
573 GNUNET_CRYPTO_short_hash_from_string (sh, &sc)) )
576 _("Invalid URI `%s'\n"),
578 GNUNET_SCHEDULER_shutdown ();
583 rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
584 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
585 if (GNUNET_YES == etime_is_rel)
587 rd.expiration_time = etime_rel.rel_value;
588 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
590 else if (GNUNET_NO == etime_is_rel)
591 rd.expiration_time = etime_abs.abs_value;
593 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
594 if (1 != nonauthority)
595 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
597 add_qe_uri = GNUNET_NAMESTORE_record_create (ns,
604 GNUNET_free_non_null (data);
609 * Main function that will be run.
612 * @param args remaining command-line arguments
613 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
614 * @param cfg configuration
617 run (void *cls, char *const *args, const char *cfgfile,
618 const struct GNUNET_CONFIGURATION_Handle *cfg)
621 if ( (NULL != args[0]) && (NULL == uri) )
622 uri = GNUNET_strdup (args[0]);
624 GNUNET_CLIENT_service_test ("namestore", cfg,
625 GNUNET_TIME_UNIT_SECONDS,
632 * The main function for gnunet-namestore.
634 * @param argc number of arguments from the command line
635 * @param argv command line arguments
636 * @return 0 ok, 1 on error
639 main (int argc, char *const *argv)
644 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
646 gettext_noop ("add record"), 0,
647 &GNUNET_GETOPT_set_one, &add},
648 {'d', "delete", NULL,
649 gettext_noop ("delete record"), 0,
650 &GNUNET_GETOPT_set_one, &del},
651 {'D', "display", NULL,
652 gettext_noop ("display records"), 0,
653 &GNUNET_GETOPT_set_one, &list},
654 {'e', "expiration", "TIME",
655 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
656 &GNUNET_GETOPT_set_string, &expirationstring},
657 {'n', "name", "NAME",
658 gettext_noop ("name of the record to add/delete/display"), 1,
659 &GNUNET_GETOPT_set_string, &name},
660 {'t', "type", "TYPE",
661 gettext_noop ("type of the record to add/delete/display"), 1,
662 &GNUNET_GETOPT_set_string, &typestring},
664 gettext_noop ("URI to import into our zone"), 1,
665 &GNUNET_GETOPT_set_string, &uri},
666 {'V', "value", "VALUE",
667 gettext_noop ("value of the record to add/delete"), 1,
668 &GNUNET_GETOPT_set_string, &value},
669 {'p', "public", NULL,
670 gettext_noop ("create or list public record"), 0,
671 &GNUNET_GETOPT_set_one, &public},
672 {'N', "non-authority", NULL,
673 gettext_noop ("create or list non-authority record"), 0,
674 &GNUNET_GETOPT_set_one, &nonauthority},
675 {'z', "zonekey", "FILENAME",
676 gettext_noop ("filename with the zone key"), 1,
677 &GNUNET_GETOPT_set_string, &keyfile},
678 GNUNET_GETOPT_OPTION_END
681 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
684 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
686 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
687 _("GNUnet zone manipulation tool"),
695 /* end of gnunet-namestore.c */