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 * Desired action is to list records.
72 * List iterator for the 'list' operation.
74 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
77 * Desired action is to remove a record.
89 static int nonauthority;
92 * Queue entry for the 'del' operation.
94 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
97 * Name of the records to add/list/remove.
102 * Value of the record to add/remove.
107 * Type of the record to add/remove, NULL to remove all.
109 static char *typestring;
112 * Desired expiration time.
114 static char *expirationstring;
118 * Task run on shutdown. Cleans up everything.
121 * @param tc scheduler context
124 do_shutdown (void *cls,
125 const struct GNUNET_SCHEDULER_TaskContext *tc)
129 GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
132 if (NULL != zone_pkey)
134 GNUNET_CRYPTO_rsa_key_free (zone_pkey);
141 * Continuation called to notify client about result of the
144 * @param cls closure, unused
145 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
146 * GNUNET_NO if content was already there
147 * GNUNET_YES (or other positive value) on success
148 * @param emsg NULL on success, otherwise an error message
151 add_continuation (void *cls,
156 if (success != GNUNET_YES)
158 _("Adding record failed: %s\n"),
159 (success == GNUNET_NO) ? "record exists" : emsg);
160 if ( (NULL == del_qe) &&
162 GNUNET_SCHEDULER_shutdown ();
167 * Continuation called to notify client about result of the
170 * @param cls closure, unused
171 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
172 * GNUNET_NO if content was already there
173 * GNUNET_YES (or other positive value) on success
174 * @param emsg NULL on success, otherwise an error message
177 del_continuation (void *cls,
182 if (success != GNUNET_YES)
184 _("Deleting record failed: %s\n"),
186 if ( (NULL == add_qe) &&
188 GNUNET_SCHEDULER_shutdown ();
193 * Process a record that was stored in the namestore.
196 * @param zone_key public key of the zone
197 * @param expire when does the corresponding block in the DHT expire (until
198 * when should we never do a DHT lookup for the same name again)?;
199 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
200 * or the expiration time of the block in the namestore (even if there are zero
201 * records matching the desired record type)
202 * @param name name that is being mapped (at most 255 characters long)
203 * @param rd_len number of entries in 'rd' array
204 * @param rd array of records with data to store
205 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
206 * because the user queried for a particular record type only)
209 display_record (void *cls,
210 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
211 struct GNUNET_TIME_Absolute expire,
214 const struct GNUNET_NAMESTORE_RecordData *rd,
215 const struct GNUNET_CRYPTO_RsaSignature *signature)
217 const char *typestring;
224 if ( (NULL == del_qe) &&
226 GNUNET_SCHEDULER_shutdown ();
232 for (i=0;i<rd_len;i++)
234 typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
235 s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
240 FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
241 (unsigned int) rd[i].record_type);
244 FPRINTF (stdout, "\t%s: %s\n", typestring, s);
247 FPRINTF (stdout, "%s", "\n");
248 GNUNET_NAMESTORE_zone_iterator_next (list_it);
253 * Main function that will be run.
256 * @param args remaining command-line arguments
257 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
258 * @param cfg configuration
261 run (void *cls, char *const *args, const char *cfgfile,
262 const struct GNUNET_CONFIGURATION_Handle *cfg)
264 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
267 size_t data_size = 0;
268 struct GNUNET_TIME_Relative etime_rel;
269 struct GNUNET_TIME_Absolute etime_abs;
270 int etime_is_rel = GNUNET_SYSERR;
271 struct GNUNET_NAMESTORE_RecordData rd;
275 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
276 "ZONEKEY", &keyfile))
279 _("Option `%s' not given, but I need a zone key file!\n"),
284 _("Using default zone file `%s'\n"),
287 zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
288 GNUNET_free (keyfile);
290 if (! (add|del|list))
292 /* nothing more to be done */
294 _("No options given\n"));
295 GNUNET_CRYPTO_rsa_key_free (zone_pkey);
299 if (NULL == zone_pkey)
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
302 _("Failed to read or create private zone key\n"));
305 GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
307 GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
309 ns = GNUNET_NAMESTORE_connect (cfg);
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313 _("Failed to connect to namestore\n"));
316 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
318 if (NULL == typestring)
321 type = GNUNET_NAMESTORE_typename_to_number (typestring);
322 if (UINT32_MAX == type)
324 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
325 GNUNET_SCHEDULER_shutdown ();
328 if ((NULL == typestring) && (add | del))
331 _("Missing option `%s' for operation `%s'\n"),
333 GNUNET_SCHEDULER_shutdown ();
339 GNUNET_NAMESTORE_string_to_value (type,
344 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
347 GNUNET_SCHEDULER_shutdown ();
350 } else if (add | del)
353 _("Missing option `%s' for operation `%s'\n"),
355 GNUNET_SCHEDULER_shutdown ();
358 if (NULL != expirationstring)
360 if (0 == strcmp (expirationstring, "never"))
362 etime_abs = GNUNET_TIME_UNIT_FOREVER_ABS;
363 etime_is_rel = GNUNET_NO;
365 else if (GNUNET_OK ==
366 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
369 etime_is_rel = GNUNET_YES;
371 else if (GNUNET_OK ==
372 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
375 etime_is_rel = GNUNET_NO;
380 _("Invalid time format `%s'\n"),
382 GNUNET_SCHEDULER_shutdown ();
389 _("Missing option `%s' for operation `%s'\n"),
391 GNUNET_SCHEDULER_shutdown ();
399 _("Missing option `%s' for operation `%s'\n"),
401 GNUNET_SCHEDULER_shutdown ();
405 rd.data_size = data_size;
406 rd.record_type = type;
407 if (GNUNET_YES == etime_is_rel)
409 rd.expiration_time = etime_rel.rel_value;
410 rd.flags |= GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
412 else if (GNUNET_NO == etime_is_rel)
413 rd.expiration_time = etime_abs.abs_value;
417 _("No valid expiration time for operation `%s'\n"),
419 GNUNET_SCHEDULER_shutdown ();
422 if (1 != nonauthority)
423 rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
425 rd.flags |= GNUNET_NAMESTORE_RF_PRIVATE;
426 add_qe = GNUNET_NAMESTORE_record_create (ns,
438 _("Missing option `%s' for operation `%s'\n"),
440 GNUNET_SCHEDULER_shutdown ();
444 rd.data_size = data_size;
445 rd.record_type = type;
446 rd.expiration_time = 0;
447 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
448 del_qe = GNUNET_NAMESTORE_record_remove (ns,
457 uint32_t must_not_flags = 0;
459 if (1 == nonauthority) /* List non-authority records */
460 must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
463 must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
465 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
472 GNUNET_free_non_null (data);
477 * The main function for gnunet-namestore.
479 * @param argc number of arguments from the command line
480 * @param argv command line arguments
481 * @return 0 ok, 1 on error
484 main (int argc, char *const *argv)
489 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
491 gettext_noop ("add record"), 0,
492 &GNUNET_GETOPT_set_one, &add},
493 {'d', "delete", NULL,
494 gettext_noop ("delete record"), 0,
495 &GNUNET_GETOPT_set_one, &del},
496 {'D', "display", NULL,
497 gettext_noop ("display records"), 0,
498 &GNUNET_GETOPT_set_one, &list},
499 {'e', "expiration", "TIME",
500 gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
501 &GNUNET_GETOPT_set_string, &expirationstring},
502 {'n', "name", "NAME",
503 gettext_noop ("name of the record to add/delete/display"), 1,
504 &GNUNET_GETOPT_set_string, &name},
505 {'t', "type", "TYPE",
506 gettext_noop ("type of the record to add/delete/display"), 1,
507 &GNUNET_GETOPT_set_string, &typestring},
508 {'V', "value", "VALUE",
509 gettext_noop ("value of the record to add/delete"), 1,
510 &GNUNET_GETOPT_set_string, &value},
511 {'p', "public", NULL,
512 gettext_noop ("create or list public record"), 0,
513 &GNUNET_GETOPT_set_one, &public},
514 {'N', "non-authority", NULL,
515 gettext_noop ("create or list non-authority record"), 0,
516 &GNUNET_GETOPT_set_one, &nonauthority},
517 {'z', "zonekey", "FILENAME",
518 gettext_noop ("filename with the zone key"), 1,
519 &GNUNET_GETOPT_set_string, &keyfile},
520 GNUNET_GETOPT_OPTION_END
525 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
528 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
531 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
532 _("GNUnet zone manipulation tool"),
534 &run, NULL)) ? 0 : 1;
539 /* end of gnunet-namestore.c */