doxygen fix
[oweals/gnunet.git] / src / namestore / gnunet-namestore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file gnunet-namestore.c
22  * @brief command line tool to manipulate the local zone
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - allow users to set record options (not just 'RF_AUTHORITY')
27  * - test
28  * - add options to list/lookup individual records
29  */
30 #include "platform.h"
31 #include <gnunet_util_lib.h>
32 #include <gnunet_dnsparser_lib.h>
33 #include <gnunet_namestore_service.h>
34
35 /**
36  * Handle to the namestore.
37  */
38 static struct GNUNET_NAMESTORE_Handle *ns;
39
40 /**
41  * Hash of the public key of our zone.
42  */
43 static struct GNUNET_CRYPTO_ShortHashCode zone;
44
45 /**
46  * Private key for the our zone.
47  */
48 static struct GNUNET_CRYPTO_RsaPrivateKey *zone_pkey;
49
50 /**
51  * Keyfile to manipulate.
52  */
53 static char *keyfile;   
54
55 /**
56  * Desired action is to add a record.
57  */
58 static int add;
59
60 /**
61  * Queue entry for the 'add' operation.
62  */
63 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
64
65 /**
66  * Desired action is to list records.
67  */
68 static int list;
69
70 /**
71  * List iterator for the 'list' operation.
72  */
73 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
74
75 /**
76  * Desired action is to remove a record.
77  */
78 static int del;
79
80 /**
81  * Is record public
82  */
83 static int public;
84
85 /**
86  * Is record authority
87  */
88 static int nonauthority;
89
90 /**
91  * Queue entry for the 'del' operation.
92  */
93 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
94
95 /**
96  * Name of the records to add/list/remove.
97  */
98 static char *name;
99
100 /**
101  * Value of the record to add/remove.
102  */
103 static char *value;
104
105 /**
106  * Type of the record to add/remove, NULL to remove all.
107  */
108 static char *typestring;
109
110 /**
111  * Desired expiration time.
112  */
113 static char *expirationstring;
114
115
116 /**
117  * Task run on shutdown.  Cleans up everything.
118  *
119  * @param cls unused
120  * @param tc scheduler context
121  */
122 static void
123 do_shutdown (void *cls,
124              const struct GNUNET_SCHEDULER_TaskContext *tc)
125 {
126   if (NULL != ns)
127   {
128     GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
129     ns = NULL;
130   }
131   if (NULL != zone_pkey)
132   {
133     GNUNET_CRYPTO_rsa_key_free (zone_pkey);
134     zone_pkey = NULL;
135   }
136 }
137
138
139 /**
140  * Continuation called to notify client about result of the
141  * operation.
142  *
143  * @param cls closure, unused
144  * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
145  *                GNUNET_NO if content was already there
146  *                GNUNET_YES (or other positive value) on success
147  * @param emsg NULL on success, otherwise an error message
148  */
149 static void
150 add_continuation (void *cls,
151                   int32_t success,
152                   const char *emsg)
153 {
154   add_qe = NULL;
155   if (success != GNUNET_YES)
156     fprintf (stderr,
157              _("Adding record failed: %s\n"),
158              (success == GNUNET_NO) ? "record exists" : emsg);
159   if ( (NULL == del_qe) &&
160        (NULL == list_it) )
161     GNUNET_SCHEDULER_shutdown ();
162 }
163
164
165 /**
166  * Continuation called to notify client about result of the
167  * operation.
168  *
169  * @param cls closure, unused
170  * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
171  *                GNUNET_NO if content was already there
172  *                GNUNET_YES (or other positive value) on success
173  * @param emsg NULL on success, otherwise an error message
174  */
175 static void
176 del_continuation (void *cls,
177                   int32_t success,
178                   const char *emsg)
179 {
180   del_qe = NULL;
181   if (success != GNUNET_YES)
182     fprintf (stderr,
183              _("Deleting record failed: %s\n"),
184              emsg);
185   if ( (NULL == add_qe) &&
186        (NULL == list_it) )
187     GNUNET_SCHEDULER_shutdown ();
188 }
189
190
191 /**
192  * Process a record that was stored in the namestore.
193  *
194  * @param cls closure
195  * @param zone_key public key of the zone
196  * @param expire when does the corresponding block in the DHT expire (until
197  *               when should we never do a DHT lookup for the same name again)?; 
198  *               GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
199  *               or the expiration time of the block in the namestore (even if there are zero
200  *               records matching the desired record type)
201  * @param name name that is being mapped (at most 255 characters long)
202  * @param rd_len number of entries in 'rd' array
203  * @param rd array of records with data to store
204  * @param signature signature of the record block, NULL if signature is unavailable (i.e. 
205  *        because the user queried for a particular record type only)
206  */
207 static void
208 display_record (void *cls,
209                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
210                 struct GNUNET_TIME_Absolute expire,                         
211                 const char *name,
212                 unsigned int rd_len,
213                 const struct GNUNET_NAMESTORE_RecordData *rd,
214                 const struct GNUNET_CRYPTO_RsaSignature *signature)
215 {
216   const char *typestring;
217   char *s;
218   unsigned int i;
219
220   if (NULL == name)
221   {
222     list_it = NULL;
223     if ( (NULL == del_qe) &&
224          (NULL == add_qe) )
225       GNUNET_SCHEDULER_shutdown ();
226     return;
227   }
228   FPRINTF (stdout,
229            "%s:\n",
230            name);
231   for (i=0;i<rd_len;i++)
232   {
233     typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
234     s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
235                                           rd[i].data,
236                                           rd[i].data_size);
237     if (NULL == s)
238     {
239       FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
240                (unsigned int) rd[i].record_type);
241       continue;
242     }
243     FPRINTF (stdout, "\t%s: %s\n", typestring, s);
244     GNUNET_free (s);    
245   }
246   FPRINTF (stdout, "%s", "\n");
247   GNUNET_NAMESTORE_zone_iterator_next (list_it);
248 }
249
250
251 /**
252  * Main function that will be run.
253  *
254  * @param cls closure
255  * @param args remaining command-line arguments
256  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
257  * @param cfg configuration
258  */
259 static void
260 run (void *cls, char *const *args, const char *cfgfile,
261      const struct GNUNET_CONFIGURATION_Handle *cfg)
262 {
263   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
264   uint32_t type;
265   void *data = NULL;
266   size_t data_size = 0;
267   struct GNUNET_TIME_Relative etime;
268   struct GNUNET_NAMESTORE_RecordData rd;
269
270   if (NULL == keyfile)
271   {
272       if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
273                                                  "ZONEKEY", &keyfile))
274       {
275         fprintf (stderr,
276                  _("Option `%s' not given, but I need a zone key file!\n"),
277                  "z");
278         return;
279       }
280       fprintf (stderr,
281                _("Using default zone file `%s'\n"),
282                keyfile);
283   }
284   zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
285   GNUNET_free (keyfile);
286   keyfile = NULL;
287   if (! (add|del|list))
288   {
289     /* nothing more to be done */  
290     fprintf (stderr,
291              _("No options given\n"));
292     GNUNET_CRYPTO_rsa_key_free (zone_pkey);
293     zone_pkey = NULL;
294     return; 
295   }
296   if (NULL == zone_pkey)
297   {
298     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299                 _("Failed to read or create private zone key\n"));
300     return;
301   }
302   GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
303                                     &pub);
304   GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &zone);
305
306   ns = GNUNET_NAMESTORE_connect (cfg);
307   if (NULL == ns)
308   {
309     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310                 _("Failed to connect to namestore\n"));
311     return;
312   }
313   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
314                                 &do_shutdown, NULL);
315   if (NULL == typestring)
316     type = 0;
317   else
318     type = GNUNET_NAMESTORE_typename_to_number (typestring);
319   if (UINT32_MAX == type)
320   {
321     fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
322     GNUNET_SCHEDULER_shutdown ();
323     return;
324   }
325   if ((NULL == typestring) && (add | del))
326   {
327     fprintf (stderr,
328              _("Missing option `%s' for operation `%s'\n"),
329              "-t", _("add/del"));
330     GNUNET_SCHEDULER_shutdown ();
331     return;     
332   }
333   if (NULL != value)
334   {
335     if (GNUNET_OK !=
336         GNUNET_NAMESTORE_string_to_value (type,
337                                           value,
338                                           &data,
339                                           &data_size))
340       {
341         fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"), 
342                  value,
343                  typestring);
344         GNUNET_SCHEDULER_shutdown ();
345         return;
346       }
347   } else if (add | del)
348   {
349     fprintf (stderr,
350              _("Missing option `%s' for operation `%s'\n"),
351              "-V", _("add/del"));
352     GNUNET_SCHEDULER_shutdown ();
353     return;     
354   }
355   if (NULL != expirationstring)
356   {
357     if (0 == strcmp (expirationstring, "never"))
358     {
359       etime = GNUNET_TIME_relative_get_forever();
360     }
361     else if (GNUNET_OK !=
362         GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
363                                                &etime))
364     {
365       fprintf (stderr,
366                _("Invalid time format `%s'\n"),
367                expirationstring);
368       GNUNET_SCHEDULER_shutdown ();
369       return;     
370     }
371   } else if (add)
372   {
373     fprintf (stderr,
374              _("Missing option `%s' for operation `%s'\n"),
375              "-e", _("add"));
376     GNUNET_SCHEDULER_shutdown ();
377     return;     
378   }
379   if (add)
380   {
381     if (NULL == name)
382     {
383       fprintf (stderr,
384                _("Missing option `%s' for operation `%s'\n"),
385                "-n", _("add"));
386       GNUNET_SCHEDULER_shutdown ();
387       return;     
388     }
389     rd.data = data;
390     rd.data_size = data_size;
391     rd.record_type = type;
392     rd.expiration = GNUNET_TIME_relative_to_absolute (etime);
393     if (1 != nonauthority)
394       rd.flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
395     if (1 != public)
396       rd.flags |= GNUNET_NAMESTORE_RF_PRIVATE;
397     add_qe = GNUNET_NAMESTORE_record_create (ns,
398                                              zone_pkey,
399                                              name,
400                                              &rd,
401                                              &add_continuation,
402                                              NULL);
403   }
404   if (del)
405   {
406     if (NULL == name)
407     {
408       fprintf (stderr,
409                _("Missing option `%s' for operation `%s'\n"),
410                "-n", _("del"));
411       GNUNET_SCHEDULER_shutdown ();
412       return;     
413     }
414     rd.data = data;
415     rd.data_size = data_size;
416     rd.record_type = type;
417     rd.expiration.abs_value = 0;
418     rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;
419     del_qe = GNUNET_NAMESTORE_record_remove (ns,
420                                              zone_pkey,
421                                              name,
422                                              &rd,
423                                              &del_continuation,
424                                              NULL);
425   }
426   if (list)
427   {
428     uint32_t must_not_flags = 0;
429
430     if (1 == nonauthority) /* List non-authority records */
431       must_not_flags |= GNUNET_NAMESTORE_RF_AUTHORITY;
432
433     if (1 == public)
434       must_not_flags |= GNUNET_NAMESTORE_RF_PRIVATE;
435
436     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
437                                                      &zone,
438                                                      0,
439                                                      must_not_flags,
440                                                      &display_record,
441                                                      NULL);
442   }
443   GNUNET_free_non_null (data);
444 }
445
446
447 /**
448  * The main function for gnunet-namestore.
449  *
450  * @param argc number of arguments from the command line
451  * @param argv command line arguments
452  * @return 0 ok, 1 on error
453  */
454 int
455 main (int argc, char *const *argv)
456 {
457   nonauthority = -1;
458   public = -1;
459
460   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
461     {'a', "add", NULL,
462      gettext_noop ("add record"), 0,
463      &GNUNET_GETOPT_set_one, &add},
464     {'d', "delete", NULL,
465      gettext_noop ("delete record"), 0,
466      &GNUNET_GETOPT_set_one, &del},   
467     {'D', "display", NULL,
468      gettext_noop ("display records"), 0,
469      &GNUNET_GETOPT_set_one, &list},   
470     {'e', "expiration", "TIME",
471      gettext_noop ("expiration time for record to use (for adding only), \"never\" is possible"), 1,
472      &GNUNET_GETOPT_set_string, &expirationstring},   
473     {'n', "name", "NAME",
474      gettext_noop ("name of the record to add/delete/display"), 1,
475      &GNUNET_GETOPT_set_string, &name},   
476     {'t', "type", "TYPE",
477      gettext_noop ("type of the record to add/delete/display"), 1,
478      &GNUNET_GETOPT_set_string, &typestring},   
479     {'V', "value", "VALUE",
480      gettext_noop ("value of the record to add/delete"), 1,
481      &GNUNET_GETOPT_set_string, &value},   
482     {'p', "public", NULL,
483      gettext_noop ("create or list public record"), 0,
484      &GNUNET_GETOPT_set_one, &public},
485     {'N', "non-authority", NULL,
486      gettext_noop ("create or list non-authority record"), 0,
487      &GNUNET_GETOPT_set_one, &nonauthority},
488     {'z', "zonekey", "FILENAME",
489      gettext_noop ("filename with the zone key"), 1,
490      &GNUNET_GETOPT_set_string, &keyfile},   
491     GNUNET_GETOPT_OPTION_END
492   };
493
494   int ret;
495
496   GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
497   ret =
498       (GNUNET_OK ==
499        GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
500                            _("GNUnet zone manipulation tool"), 
501                            options,
502                            &run, NULL)) ? 0 : 1;
503
504   return ret;
505 }
506
507 /* end of gnunet-namestore.c */