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
27 * - allow users to set record options (not just 'RF_AUTHORITY')
29 * - parsing SOA, PTR and MX value specifications (and define format!)
30 * - add options to list/lookup individual records
33 #include <gnunet_util_lib.h>
34 #include <gnunet_dnsparser_lib.h>
35 #include <gnunet_namestore_service.h>
38 * Handle to the namestore.
40 static struct GNUNET_NAMESTORE_Handle *ns;
43 * Hash of the public key of our zone.
45 static GNUNET_HashCode zone;
48 * Private key for the our zone.
50 static struct GNUNET_CRYPTO_RsaPrivateKey *zone_pkey;
53 * Keyfile to manipulate.
58 * Desired action is to add a record.
63 * Queue entry for the 'add' operation.
65 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
68 * Desired action is to list records.
73 * List iterator for the 'list' operation.
75 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
78 * Desired action is to remove a record.
83 * Queue entry for the 'del' operation.
85 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
88 * Name of the records to add/list/remove.
93 * Value of the record to add/remove.
98 * Type of the record to add/remove, NULL to remove all.
100 static char *typestring;
103 * Desired expiration time.
105 static char *expirationstring;
109 * Task run on shutdown. Cleans up everything.
112 * @param tc scheduler context
115 do_shutdown (void *cls,
116 const struct GNUNET_SCHEDULER_TaskContext *tc)
120 GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
123 if (NULL != zone_pkey)
125 GNUNET_CRYPTO_rsa_key_free (zone_pkey);
132 * Continuation called to notify client about result of the
135 * @param cls closure, unused
136 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
137 * GNUNET_NO if content was already there
138 * GNUNET_YES (or other positive value) on success
139 * @param emsg NULL on success, otherwise an error message
142 add_continuation (void *cls,
147 if (success != GNUNET_YES)
149 _("Adding record failed: %s\n"),
150 (success == GNUNET_NO) ? "record exists" : emsg);
151 if ( (NULL == del_qe) &&
153 GNUNET_SCHEDULER_shutdown ();
158 * Continuation called to notify client about result of the
161 * @param cls closure, unused
162 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
163 * GNUNET_NO if content was already there
164 * GNUNET_YES (or other positive value) on success
165 * @param emsg NULL on success, otherwise an error message
168 del_continuation (void *cls,
173 if (success != GNUNET_YES)
175 _("Deleting record failed: %s\n"),
177 if ( (NULL == add_qe) &&
179 GNUNET_SCHEDULER_shutdown ();
184 * Process a record that was stored in the namestore.
187 * @param zone_key public key of the zone
188 * @param expire when does the corresponding block in the DHT expire (until
189 * when should we never do a DHT lookup for the same name again)?;
190 * GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
191 * or the expiration time of the block in the namestore (even if there are zero
192 * records matching the desired record type)
193 * @param name name that is being mapped (at most 255 characters long)
194 * @param rd_count number of entries in 'rd' array
195 * @param rd array of records with data to store
196 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
197 * because the user queried for a particular record type only)
200 display_record (void *cls,
201 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
202 struct GNUNET_TIME_Absolute expire,
205 const struct GNUNET_NAMESTORE_RecordData *rd,
206 const struct GNUNET_CRYPTO_RsaSignature *signature)
211 if ( (NULL == del_qe) &&
213 GNUNET_SCHEDULER_shutdown ();
216 // FIXME: display record!
217 GNUNET_NAMESTORE_zone_iterator_next (list_it);
222 * Main function that will be run.
225 * @param args remaining command-line arguments
226 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
227 * @param cfg configuration
230 run (void *cls, char *const *args, const char *cfgfile,
231 const struct GNUNET_CONFIGURATION_Handle *cfg)
233 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
237 struct in_addr value_a;
238 struct in6_addr value_aaaa;
239 struct GNUNET_TIME_Relative etime;
240 struct GNUNET_NAMESTORE_RecordData rd;
245 _("Option `%s' not given, but I need a zone key file!\n"),
249 zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
250 GNUNET_free (keyfile);
252 if (! (add|del|list))
254 /* nothing more to be done */
255 GNUNET_CRYPTO_rsa_key_free (zone_pkey);
259 if (NULL == zone_pkey)
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262 _("Failed to read or create private zone key\n"));
265 GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
267 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &zone);
269 ns = GNUNET_NAMESTORE_connect (cfg);
272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273 _("Failed to connect to namestore\n"));
276 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
278 if (NULL == typestring)
281 type = GNUNET_NAMESTORE_typename_to_number (typestring);
282 if (UINT32_MAX == type)
284 fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
285 GNUNET_SCHEDULER_shutdown ();
287 } else if (add | del)
290 _("Missing option `%s' for operation `%s'\n"),
292 GNUNET_SCHEDULER_shutdown ();
300 fprintf (stderr, _("Need a record type to interpret value `%s'\n"), value);
301 GNUNET_SCHEDULER_shutdown ();
303 case GNUNET_DNSPARSER_TYPE_A:
304 if (1 != inet_pton (AF_INET, value, &value_a))
306 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
309 GNUNET_SCHEDULER_shutdown ();
313 data_size = sizeof (value_a);
315 case GNUNET_DNSPARSER_TYPE_NS:
317 data_size = strlen (value);
319 case GNUNET_DNSPARSER_TYPE_CNAME:
321 data_size = strlen (value);
323 case GNUNET_DNSPARSER_TYPE_SOA:
325 fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
326 GNUNET_SCHEDULER_shutdown ();
328 case GNUNET_DNSPARSER_TYPE_PTR:
330 fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
331 GNUNET_SCHEDULER_shutdown ();
333 case GNUNET_DNSPARSER_TYPE_MX:
335 fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
336 GNUNET_SCHEDULER_shutdown ();
338 case GNUNET_DNSPARSER_TYPE_TXT:
340 data_size = strlen (value);
342 case GNUNET_DNSPARSER_TYPE_AAAA:
343 if (1 != inet_pton (AF_INET6, value, &value_aaaa))
345 fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"),
348 GNUNET_SCHEDULER_shutdown ();
352 data_size = sizeof (value_aaaa);
354 case GNUNET_NAMESTORE_TYPE_PKEY:
355 fprintf (stderr, _("Record type `%s' not implemented yet\n"), typestring);
356 GNUNET_SCHEDULER_shutdown ();
358 case GNUNET_NAMESTORE_TYPE_PSEU:
360 data_size = strlen (value);
365 } else if (add | del)
368 _("Missing option `%s' for operation `%s'\n"),
370 GNUNET_SCHEDULER_shutdown ();
373 if (NULL != expirationstring)
376 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
380 _("Invalid time format `%s'\n"),
382 GNUNET_SCHEDULER_shutdown ();
385 } else if (add | del)
388 _("Missing option `%s' for operation `%s'\n"),
390 GNUNET_SCHEDULER_shutdown ();
398 _("Missing option `%s' for operation `%s'\n"),
400 GNUNET_SCHEDULER_shutdown ();
404 rd.data_size = data_size;
405 rd.record_type = type;
406 rd.expiration = GNUNET_TIME_relative_to_absolute (etime);
407 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY; // FIXME: not always...
408 add_qe = GNUNET_NAMESTORE_record_create (ns,
420 _("Missing option `%s' for operation `%s'\n"),
422 GNUNET_SCHEDULER_shutdown ();
426 rd.data_size = data_size;
427 rd.record_type = type;
428 rd.expiration = GNUNET_TIME_relative_to_absolute (etime);
429 rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY; // FIXME: not always...
430 del_qe = GNUNET_NAMESTORE_record_create (ns,
439 list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
449 * The main function for gnunet-namestore.
451 * @param argc number of arguments from the command line
452 * @param argv command line arguments
453 * @return 0 ok, 1 on error
456 main (int argc, char *const *argv)
458 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
460 gettext_noop ("add record"), 0,
461 &GNUNET_GETOPT_set_one, &add},
462 {'d', "delete", NULL,
463 gettext_noop ("delete record"), 0,
464 &GNUNET_GETOPT_set_one, &del},
465 {'D', "display", NULL,
466 gettext_noop ("display records"), 0,
467 &GNUNET_GETOPT_set_one, &list},
468 {'e', "expiration", "TIME",
469 gettext_noop ("expiration time to use (for adding only)"), 1,
470 &GNUNET_GETOPT_set_string, &expirationstring},
471 {'n', "name", "NAME",
472 gettext_noop ("name of the record to add/delete/display"), 1,
473 &GNUNET_GETOPT_set_string, &name},
474 {'t', "type", "TYPE",
475 gettext_noop ("type of the record to add/delete/display"), 1,
476 &GNUNET_GETOPT_set_string, &typestring},
477 {'V', "value", "VALUE",
478 gettext_noop ("value of the record to add/delete"), 1,
479 &GNUNET_GETOPT_set_string, &value},
480 {'z', "zonekey", "FILENAME",
481 gettext_noop ("filename with the zone key"), 1,
482 &GNUNET_GETOPT_set_string, &keyfile},
483 GNUNET_GETOPT_OPTION_END
488 GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
491 GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
492 _("GNUnet zone manipulation tool"),
494 &run, NULL)) ? 0 : 1;
499 /* end of gnunet-namestore.c */