- not required anymore
[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  * - printing records
27  * - allow users to set record options (not just 'RF_AUTHORITY')
28  * - test
29  * - parsing SOA, PTR and MX value specifications (and define format!)
30  * - add options to list/lookup individual records
31  */
32 #include "platform.h"
33 #include <gnunet_util_lib.h>
34 #include <gnunet_dnsparser_lib.h>
35 #include <gnunet_namestore_service.h>
36
37 /**
38  * Handle to the namestore.
39  */
40 static struct GNUNET_NAMESTORE_Handle *ns;
41
42 /**
43  * Hash of the public key of our zone.
44  */
45 static GNUNET_HashCode zone;
46
47 /**
48  * Private key for the our zone.
49  */
50 static struct GNUNET_CRYPTO_RsaPrivateKey *zone_pkey;
51
52 /**
53  * Keyfile to manipulate.
54  */
55 static char *keyfile;   
56
57 /**
58  * Desired action is to add a record.
59  */
60 static int add;
61
62 /**
63  * Queue entry for the 'add' operation.
64  */
65 static struct GNUNET_NAMESTORE_QueueEntry *add_qe;
66
67 /**
68  * Desired action is to list records.
69  */
70 static int list;
71
72 /**
73  * List iterator for the 'list' operation.
74  */
75 static struct GNUNET_NAMESTORE_ZoneIterator *list_it;
76
77 /**
78  * Desired action is to remove a record.
79  */
80 static int del;
81
82 /**
83  * Queue entry for the 'del' operation.
84  */
85 static struct GNUNET_NAMESTORE_QueueEntry *del_qe;
86
87 /**
88  * Name of the records to add/list/remove.
89  */
90 static char *name;
91
92 /**
93  * Value of the record to add/remove.
94  */
95 static char *value;
96
97 /**
98  * Type of the record to add/remove, NULL to remove all.
99  */
100 static char *typestring;
101
102 /**
103  * Desired expiration time.
104  */
105 static char *expirationstring;
106
107
108 /**
109  * Task run on shutdown.  Cleans up everything.
110  *
111  * @param cls unused
112  * @param tc scheduler context
113  */
114 static void
115 do_shutdown (void *cls,
116              const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   if (NULL != ns)
119   {
120     GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
121     ns = NULL;
122   }
123   if (NULL != zone_pkey)
124   {
125     GNUNET_CRYPTO_rsa_key_free (zone_pkey);
126     zone_pkey = NULL;
127   }
128 }
129
130
131 /**
132  * Continuation called to notify client about result of the
133  * operation.
134  *
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
140  */
141 static void
142 add_continuation (void *cls,
143                   int32_t success,
144                   const char *emsg)
145 {
146   add_qe = NULL;
147   if (success != GNUNET_YES)
148     fprintf (stderr,
149              _("Adding record failed: %s\n"),
150              (success == GNUNET_NO) ? "record exists" : emsg);
151   if ( (NULL == del_qe) &&
152        (NULL == list_it) )
153     GNUNET_SCHEDULER_shutdown ();
154 }
155
156
157 /**
158  * Continuation called to notify client about result of the
159  * operation.
160  *
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
166  */
167 static void
168 del_continuation (void *cls,
169                   int32_t success,
170                   const char *emsg)
171 {
172   del_qe = NULL;
173   if (success != GNUNET_YES)
174     fprintf (stderr,
175              _("Deleting record failed: %s\n"),
176              emsg);
177   if ( (NULL == add_qe) &&
178        (NULL == list_it) )
179     GNUNET_SCHEDULER_shutdown ();
180 }
181
182
183 /**
184  * Process a record that was stored in the namestore.
185  *
186  * @param cls closure
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_len 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)
198  */
199 static void
200 display_record (void *cls,
201                 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
202                 struct GNUNET_TIME_Absolute expire,                         
203                 const char *name,
204                 unsigned int rd_len,
205                 const struct GNUNET_NAMESTORE_RecordData *rd,
206                 const struct GNUNET_CRYPTO_RsaSignature *signature)
207 {
208   const char *typestring;
209   char *s;
210   unsigned int i;
211
212   if (NULL == name)
213   {
214     list_it = NULL;
215     if ( (NULL == del_qe) &&
216          (NULL == add_qe) )
217       GNUNET_SCHEDULER_shutdown ();
218     return;
219   }
220   FPRINTF (stdout,
221            "%s:\n",
222            name);
223   for (i=0;i<rd_len;i++)
224   {
225     typestring = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
226     s = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
227                                           rd[i].data,
228                                           rd[i].data_size);
229     if (NULL == s)
230     {
231       FPRINTF (stdout, _("\tCorrupt or unsupported record of type %u\n"),
232                (unsigned int) rd[i].record_type);
233       continue;
234     }
235     FPRINTF (stdout, "\t%s: %s\n", typestring, s);
236     GNUNET_free (s);    
237   }
238   FPRINTF (stdout, "%s", "\n");
239   GNUNET_NAMESTORE_zone_iterator_next (list_it);
240 }
241
242
243 /**
244  * Main function that will be run.
245  *
246  * @param cls closure
247  * @param args remaining command-line arguments
248  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
249  * @param cfg configuration
250  */
251 static void
252 run (void *cls, char *const *args, const char *cfgfile,
253      const struct GNUNET_CONFIGURATION_Handle *cfg)
254 {
255   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
256   uint32_t type;
257   void *data = NULL;
258   size_t data_size = 0;
259   struct GNUNET_TIME_Relative etime;
260   struct GNUNET_NAMESTORE_RecordData rd;
261
262   if (NULL == keyfile)
263   {
264     fprintf (stderr,
265              _("Option `%s' not given, but I need a zone key file!\n"),
266              "z");
267     return;
268   }
269   zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
270   GNUNET_free (keyfile);
271   keyfile = NULL;
272   if (! (add|del|list))
273   {
274     /* nothing more to be done */  
275     GNUNET_CRYPTO_rsa_key_free (zone_pkey);
276     zone_pkey = NULL;
277     return; 
278   }
279   if (NULL == zone_pkey)
280   {
281     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282                 _("Failed to read or create private zone key\n"));
283     return;
284   }
285   GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
286                                     &pub);
287   GNUNET_CRYPTO_hash (&pub, sizeof (pub), &zone);
288
289   ns = GNUNET_NAMESTORE_connect (cfg);
290   if (NULL == ns)
291   {
292     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293                 _("Failed to connect to namestore\n"));
294     return;
295   }
296   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
297                                 &do_shutdown, NULL);
298   if (NULL == typestring)
299     type = 0;
300   else
301     type = GNUNET_NAMESTORE_typename_to_number (typestring);
302   if (UINT32_MAX == type)
303   {
304     fprintf (stderr, _("Unsupported type `%s'\n"), typestring);
305     GNUNET_SCHEDULER_shutdown ();
306     return;
307   } else if (add | del)
308   {
309     fprintf (stderr,
310              _("Missing option `%s' for operation `%s'\n"),
311              "-t", _("add/del"));
312     GNUNET_SCHEDULER_shutdown ();
313     return;     
314   }
315   if (NULL != value)
316   {
317     if (GNUNET_OK !=
318         GNUNET_NAMESTORE_string_to_value (type,
319                                           value,
320                                           &data,
321                                           &data_size))
322       {
323         fprintf (stderr, _("Value `%s' invalid for record type `%s'\n"), 
324                  value,
325                  typestring);
326         GNUNET_SCHEDULER_shutdown ();
327         return;
328       }
329   } else if (add | del)
330   {
331     fprintf (stderr,
332              _("Missing option `%s' for operation `%s'\n"),
333              "-V", _("add/del"));
334     GNUNET_SCHEDULER_shutdown ();
335     return;     
336   }
337   if (NULL != expirationstring)
338   {
339     if (GNUNET_OK !=
340         GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
341                                                &etime))
342     {
343       fprintf (stderr,
344                _("Invalid time format `%s'\n"),
345                expirationstring);
346       GNUNET_SCHEDULER_shutdown ();
347       return;     
348     }
349   } else if (add | del)
350   {
351     fprintf (stderr,
352              _("Missing option `%s' for operation `%s'\n"),
353              "-e", _("add/del"));
354     GNUNET_SCHEDULER_shutdown ();
355     return;     
356   }
357   if (add)
358   {
359     if (NULL == name)
360     {
361       fprintf (stderr,
362                _("Missing option `%s' for operation `%s'\n"),
363                "-n", _("add"));
364       GNUNET_SCHEDULER_shutdown ();
365       return;     
366     }
367     rd.data = data;
368     rd.data_size = data_size;
369     rd.record_type = type;
370     rd.expiration = GNUNET_TIME_relative_to_absolute (etime);
371     rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY; // FIXME: not always...
372     add_qe = GNUNET_NAMESTORE_record_create (ns,
373                                              zone_pkey,
374                                              name,
375                                              &rd,
376                                              &add_continuation,
377                                              NULL);
378   }
379   if (del)
380   {
381     if (NULL == name)
382     {
383       fprintf (stderr,
384                _("Missing option `%s' for operation `%s'\n"),
385                "-n", _("del"));
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     rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY; // FIXME: not always...
394     del_qe = GNUNET_NAMESTORE_record_remove (ns,
395                                              zone_pkey,
396                                              name,
397                                              &rd,
398                                              &del_continuation,
399                                              NULL);
400   }
401   if (list)
402   {
403     list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
404                                                      &zone,
405                                                      0, 0,
406                                                      &display_record,
407                                                      NULL);
408   }
409   GNUNET_free_non_null (data);
410 }
411
412
413 /**
414  * The main function for gnunet-namestore.
415  *
416  * @param argc number of arguments from the command line
417  * @param argv command line arguments
418  * @return 0 ok, 1 on error
419  */
420 int
421 main (int argc, char *const *argv)
422 {
423   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
424     {'a', "add", NULL,
425      gettext_noop ("add record"), 0,
426      &GNUNET_GETOPT_set_one, &add},
427     {'d', "delete", NULL,
428      gettext_noop ("delete record"), 0,
429      &GNUNET_GETOPT_set_one, &del},   
430     {'D', "display", NULL,
431      gettext_noop ("display records"), 0,
432      &GNUNET_GETOPT_set_one, &list},   
433     {'e', "expiration", "TIME",
434      gettext_noop ("expiration time for record to use (for adding only)"), 1,
435      &GNUNET_GETOPT_set_string, &expirationstring},   
436     {'n', "name", "NAME",
437      gettext_noop ("name of the record to add/delete/display"), 1,
438      &GNUNET_GETOPT_set_string, &name},   
439     {'t', "type", "TYPE",
440      gettext_noop ("type of the record to add/delete/display"), 1,
441      &GNUNET_GETOPT_set_string, &typestring},   
442     {'V', "value", "VALUE",
443      gettext_noop ("value of the record to add/delete"), 1,
444      &GNUNET_GETOPT_set_string, &value},   
445     {'z', "zonekey", "FILENAME",
446      gettext_noop ("filename with the zone key"), 1,
447      &GNUNET_GETOPT_set_string, &keyfile},   
448     GNUNET_GETOPT_OPTION_END
449   };
450
451   int ret;
452
453   GNUNET_log_setup ("gnunet-namestore", "WARNING", NULL);
454   ret =
455       (GNUNET_OK ==
456        GNUNET_PROGRAM_run (argc, argv, "gnunet-namestore",
457                            _("GNUnet zone manipulation tool"), 
458                            options,
459                            &run, NULL)) ? 0 : 1;
460
461   return ret;
462 }
463
464 /* end of gnunet-namestore.c */