fix uninit variable causing crash on 0 value
[oweals/gnunet.git] / src / gns / gnunet-gns.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file gnunet-gns.c
22  * @brief command line tool to access distributed GNS
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include <gnunet_util_lib.h>
27 #include <gnunet_dnsparser_lib.h>
28 #include <gnunet_identity_service.h>
29 #include <gnunet_gnsrecord_lib.h>
30 #include <gnunet_namestore_service.h>
31 #include <gnunet_gns_service.h>
32
33 /**
34  * Configuration we are using.
35  */
36 static const struct GNUNET_CONFIGURATION_Handle *cfg;
37
38 /**
39  * Handle to GNS service.
40  */
41 static struct GNUNET_GNS_Handle *gns;
42
43 /**
44  * Desired timeout for the lookup (default is no timeout).
45  */
46 static struct GNUNET_TIME_Relative timeout;
47
48 /**
49  * GNS name to lookup. (-u option)
50  */
51 static char *lookup_name;
52
53 /**
54  * record type to look up (-t option)
55  */
56 static char *lookup_type;
57
58 /**
59  * Identity of the zone to use for the lookup (-z option)
60  */
61 static char *zone_ego_name;
62
63 /**
64  * Public key of the zone to use for the lookup (-p option)
65  */
66 static char *public_key;
67
68 /**
69  * Reverse key
70  */
71 static char *reverse_key;
72
73 /**
74  * Reverse key
75  */
76 static struct GNUNET_CRYPTO_EcdsaPublicKey rkey;
77
78 /**
79  * Set to GNUNET_GNS_LO_LOCAL_MASTER if we are looking up in the master zone.
80  */
81 static enum GNUNET_GNS_LocalOptions local_options;
82
83 /**
84  * raw output
85  */
86 static int raw;
87
88 /**
89  * Requested record type.
90  */
91 static int rtype;
92
93 /**
94  * Handle to lookup request
95  */
96 static struct GNUNET_GNS_LookupRequest *lookup_request;
97
98 /**
99  * Handle to reverse lookup request
100  */
101 static struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_request;
102
103 /**
104  * Lookup an ego with the identity service.
105  */
106 static struct GNUNET_IDENTITY_EgoLookup *el;
107
108 /**
109  * Handle for identity service.
110  */
111 static struct GNUNET_IDENTITY_Handle *identity;
112
113 /**
114  * Active operation on identity service.
115  */
116 static struct GNUNET_IDENTITY_Operation *id_op;
117
118 /**
119  * Task scheduled to handle timeout.
120  */
121 static struct GNUNET_SCHEDULER_Task *tt;
122
123
124 /**
125  * Task run on shutdown.  Cleans up everything.
126  *
127  * @param cls unused
128  */
129 static void
130 do_shutdown (void *cls)
131 {
132   if (NULL != el)
133   {
134     GNUNET_IDENTITY_ego_lookup_cancel (el);
135     el = NULL;
136   }
137   if (NULL != id_op)
138   {
139     GNUNET_IDENTITY_cancel (id_op);
140     id_op = NULL;
141   }
142   if (NULL != lookup_request)
143   {
144     GNUNET_GNS_lookup_cancel (lookup_request);
145     lookup_request = NULL;
146   }
147   if (NULL != identity)
148   {
149     GNUNET_IDENTITY_disconnect (identity);
150     identity = NULL;
151   }
152   if (NULL != gns)
153   {
154     GNUNET_GNS_disconnect (gns);
155     gns = NULL;
156   }
157   if (NULL != tt)
158   {
159     GNUNET_SCHEDULER_cancel (tt);
160     tt = NULL;
161   }
162 }
163
164
165 /**
166  * Task run on timeout. Triggers shutdown.
167  *
168  * @param cls unused
169  */
170 static void
171 do_timeout (void *cls)
172 {
173   tt = NULL;
174   GNUNET_SCHEDULER_shutdown ();
175 }
176
177 static void
178 process_reverse_result (void *cls,
179                         const char *name)
180 {
181   rev_lookup_request = NULL;
182   if (NULL == name)
183   {
184     printf ("No name found.\n");
185     return;
186   }
187   if (raw)
188     printf ("%s\n", name);
189   else
190     printf ("%s is known as %s\n",
191             reverse_key,
192             name);
193   GNUNET_SCHEDULER_shutdown ();
194 }
195
196 /**
197  * Function called with the result of a GNS lookup.
198  *
199  * @param cls the 'const char *' name that was resolved
200  * @param rd_count number of records returned
201  * @param rd array of @a rd_count records with the results
202  */
203 static void
204 process_lookup_result (void *cls, uint32_t rd_count,
205                        const struct GNUNET_GNSRECORD_Data *rd)
206 {
207   const char *name = cls;
208   uint32_t i;
209   const char *typename;
210   char* string_val;
211
212   lookup_request = NULL;
213   if (!raw)
214   {
215     if (0 == rd_count)
216       printf ("No results.\n");
217     else
218       printf ("%s:\n",
219               name);
220   }
221   for (i=0; i<rd_count; i++)
222   {
223     if ( (rd[i].record_type != rtype) &&
224          (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
225       continue;
226     typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
227     string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
228                                                    rd[i].data,
229                                                    rd[i].data_size);
230     if (NULL == string_val)
231     {
232       fprintf (stderr,
233                "Record %u of type %d malformed, skipping\n",
234                (unsigned int) i,
235                (int) rd[i].record_type);
236       continue;
237     }
238     if (raw)
239       printf ("%s\n",
240               string_val);
241     else
242       printf ("Got `%s' record: %s\n",
243               typename,
244               string_val);
245     GNUNET_free (string_val);
246   }
247   GNUNET_SCHEDULER_shutdown ();
248 }
249
250
251 /**
252  * Perform the actual resolution, starting with the zone
253  * identified by the given public key and the shorten zone.
254  *
255  * @param pkey public key to use for the zone, can be NULL
256  * @param shorten_key private key used for shortening, can be NULL
257  */
258 static void
259 lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
260                   const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
261 {
262   if (NULL != lookup_type)
263     rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
264   else
265     rtype = GNUNET_DNSPARSER_TYPE_A;
266   if (UINT32_MAX == rtype)
267   {
268     fprintf (stderr,
269              _("Invalid typename specified, assuming `ANY'\n"));
270     rtype = GNUNET_GNSRECORD_TYPE_ANY;
271   }
272
273   if (NULL != lookup_name)
274   {
275     lookup_request = GNUNET_GNS_lookup (gns,
276                                         lookup_name,
277                                         pkey,
278                                         rtype,
279                                         local_options,
280                                         shorten_key,
281                                         &process_lookup_result,
282                                         lookup_name);
283   }
284   else if (NULL != reverse_key)
285   {
286     rev_lookup_request = GNUNET_GNS_reverse_lookup (gns,
287                                                 &rkey,
288                                                 pkey,
289                                                 &process_reverse_result,
290                                                 NULL);
291   }
292   else
293   {
294     fprintf (stderr,
295              _("Please specify name to lookup!\n"));
296     GNUNET_SCHEDULER_shutdown ();
297     return;
298   }
299 }
300
301
302 /**
303  * Method called to with the ego we are to use for shortening
304  * during the lookup.
305  *
306  * @param cls closure contains the public key to use
307  * @param ego ego handle, NULL if not found
308  * @param ctx context for application to store data for this ego
309  *                 (during the lifetime of this process, initially NULL)
310  * @param name name assigned by the user for this ego,
311  *                   NULL if the user just deleted the ego and it
312  *                   must thus no longer be used
313  */
314 static void
315 identity_shorten_cb (void *cls,
316                      struct GNUNET_IDENTITY_Ego *ego,
317                      void **ctx,
318                      const char *name)
319 {
320   struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
321
322   id_op = NULL;
323   if (NULL == ego)
324     lookup_with_keys (pkeym, NULL);
325   else
326     lookup_with_keys (pkeym,
327                       GNUNET_IDENTITY_ego_get_private_key (ego));
328   GNUNET_free (pkeym);
329 }
330
331
332 /**
333  * Perform the actual resolution, starting with the zone
334  * identified by the given public key.
335  *
336  * @param pkey public key to use for the zone
337  */
338 static void
339 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
340 {
341   struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
342
343   GNUNET_assert (NULL != pkey);
344   pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
345   *pkeym = *pkey;
346   GNUNET_break (NULL == id_op);
347   id_op = GNUNET_IDENTITY_get (identity,
348                                "gns-short",
349                                &identity_shorten_cb,
350                                pkeym);
351   if (NULL == id_op)
352   {
353     GNUNET_break (0);
354     lookup_with_keys (pkey, NULL);
355   }
356 }
357
358
359 /**
360  * Method called to with the ego we are to use for the lookup,
361  * when the ego is determined by a name.
362  *
363  * @param cls closure (NULL, unused)
364  * @param ego ego handle, NULL if not found
365  */
366 static void
367 identity_zone_cb (void *cls,
368                   const struct GNUNET_IDENTITY_Ego *ego)
369 {
370   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
371
372   el = NULL;
373   if (NULL == ego)
374   {
375     fprintf (stderr,
376              _("Ego for `%s' not found, cannot perform lookup.\n"),
377              zone_ego_name);
378     GNUNET_SCHEDULER_shutdown ();
379   }
380   else
381   {
382     GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
383     lookup_with_public_key (&pkey);
384   }
385   GNUNET_free_non_null (zone_ego_name);
386   zone_ego_name = NULL;
387 }
388
389
390 /**
391  * Method called to with the ego we are to use for the lookup,
392  * when the ego is the one for the default master zone.
393  *
394  * @param cls closure (NULL, unused)
395  * @param ego ego handle, NULL if not found
396  * @param ctx context for application to store data for this ego
397  *                 (during the lifetime of this process, initially NULL)
398  * @param name name assigned by the user for this ego,
399  *                   NULL if the user just deleted the ego and it
400  *                   must thus no longer be used
401  */
402 static void
403 identity_master_cb (void *cls,
404                     struct GNUNET_IDENTITY_Ego *ego,
405                     void **ctx,
406                     const char *name)
407 {
408   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
409   const char *dot;
410
411   id_op = NULL;
412   if (NULL == ego)
413   {
414     fprintf (stderr,
415              _("Ego for `gns-master' not found, cannot perform lookup.  Did you run gnunet-gns-import.sh?\n"));
416     GNUNET_SCHEDULER_shutdown ();
417     return;
418   }
419   GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
420   /* main name is our own master zone, do no look for that in the DHT */
421   local_options = GNUNET_GNS_LO_LOCAL_MASTER;
422
423   /* if the name is of the form 'label.gnu', never go to the DHT */
424   dot = NULL;
425   if (NULL != lookup_name)
426     dot = strchr (lookup_name, '.');
427   if ( (NULL != dot) &&
428        (0 == strcasecmp (dot, ".gnu")) )
429     local_options = GNUNET_GNS_LO_NO_DHT;
430   lookup_with_public_key (&pkey);
431 }
432
433
434 /**
435  * Main function that will be run.
436  *
437  * @param cls closure
438  * @param args remaining command-line arguments
439  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
440  * @param c configuration
441  */
442 static void
443 run (void *cls,
444      char *const *args,
445      const char *cfgfile,
446      const struct GNUNET_CONFIGURATION_Handle *c)
447 {
448   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
449
450   cfg = c;
451   gns = GNUNET_GNS_connect (cfg);
452   identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
453   if (NULL == gns)
454   {
455     fprintf (stderr,
456              _("Failed to connect to GNS\n"));
457     return;
458   }
459   tt = GNUNET_SCHEDULER_add_delayed (timeout,
460                                      &do_timeout, NULL);
461   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
462   if (NULL != reverse_key)
463   {
464     if (GNUNET_OK !=
465         GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_key,
466                                                     strlen (reverse_key),
467                                                     &rkey))
468     {
469       fprintf (stderr,
470                _("Reverse key `%s' is not well-formed\n"),
471                reverse_key);
472       GNUNET_SCHEDULER_shutdown ();
473       return;
474     }
475   }
476   if (NULL != public_key)
477   {
478     if (GNUNET_OK !=
479         GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
480                                                     strlen (public_key),
481                                                     &pkey))
482     {
483       fprintf (stderr,
484                _("Public key `%s' is not well-formed\n"),
485                public_key);
486       GNUNET_SCHEDULER_shutdown ();
487       return;
488     }
489     lookup_with_public_key (&pkey);
490     return;
491   }
492   if (NULL != reverse_key)
493   {
494     if (GNUNET_OK !=
495         GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_key,
496                                                     strlen (reverse_key),
497                                                     &rkey))
498     {
499       fprintf (stderr,
500                _("Reverse key `%s' is not well-formed\n"),
501                reverse_key);
502       GNUNET_SCHEDULER_shutdown ();
503       return;
504     }
505   }
506   if (NULL != zone_ego_name)
507   {
508     el = GNUNET_IDENTITY_ego_lookup (cfg,
509                                      zone_ego_name,
510                                      &identity_zone_cb,
511                                      NULL);
512     return;
513   }
514   if ( (NULL != lookup_name) &&
515        (strlen (lookup_name) > 4) &&
516        (0 == strcmp (".zkey",
517                      &lookup_name[strlen (lookup_name) - 4])) )
518   {
519     /* no zone required, use 'anonymous' zone */
520     GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
521                                         &pkey);
522     lookup_with_public_key (&pkey);
523   }
524   else
525   {
526     GNUNET_break (NULL == id_op);
527     id_op = GNUNET_IDENTITY_get (identity,
528                                  "gns-master",
529                                  &identity_master_cb,
530                                  NULL);
531     GNUNET_assert (NULL != id_op);
532   }
533 }
534
535
536 /**
537  * The main function for gnunet-gns.
538  *
539  * @param argc number of arguments from the command line
540  * @param argv command line arguments
541  * @return 0 ok, 1 on error
542  */
543 int
544 main (int argc, char *const *argv)
545 {
546   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
547     {'u', "lookup", "NAME",
548       gettext_noop ("Lookup a record for the given name"), 1,
549       &GNUNET_GETOPT_set_string, &lookup_name},
550     {'t', "type", "TYPE",
551       gettext_noop ("Specify the type of the record to lookup"), 1,
552       &GNUNET_GETOPT_set_string, &lookup_type},
553     { 'T', "timeout", "DELAY",
554       gettext_noop ("Specify timeout for the lookup"), 1,
555       &GNUNET_GETOPT_set_relative_time, &timeout },
556     {'r', "raw", NULL,
557       gettext_noop ("No unneeded output"), 0,
558       &GNUNET_GETOPT_set_one, &raw},
559     {'p', "public-key", "PKEY",
560       gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
561       &GNUNET_GETOPT_set_string, &public_key},
562     {'z', "zone", "NAME",
563       gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
564       &GNUNET_GETOPT_set_string, &zone_ego_name},
565     {'R', "reverse", "PKEY",
566       gettext_noop ("Specify the public key of the zone to reverse lookup a name for"), 1,
567       &GNUNET_GETOPT_set_string, &reverse_key},
568     GNUNET_GETOPT_OPTION_END
569   };
570   int ret;
571
572   timeout = GNUNET_TIME_UNIT_FOREVER_REL;
573   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
574     return 2;
575
576   GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
577   ret =
578     (GNUNET_OK ==
579      GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
580                          _("GNUnet GNS resolver tool"),
581                          options,
582                          &run, NULL)) ? 0 : 1;
583   GNUNET_free ((void*) argv);
584   return ret;
585 }
586
587 /* end of gnunet-gns.c */