202e02a5023fd4cc1b6d807db83fbc71e6f9eb3a
[oweals/gnunet.git] / src / gns / gnunet-gns.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2013, 2017-2018 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  * Set to GNUNET_GNS_LO_LOCAL_MASTER if we are looking up in the master zone.
60  */
61 static enum GNUNET_GNS_LocalOptions local_options;
62
63 /**
64  * raw output
65  */
66 static int raw;
67
68 /**
69  * Requested record type.
70  */
71 static int rtype;
72
73 /**
74  * Handle to lookup request
75  */
76 static struct GNUNET_GNS_LookupRequest *lookup_request;
77
78 /**
79  * Lookup an ego with the identity service.
80  */
81 static struct GNUNET_IDENTITY_EgoLookup *el;
82
83 /**
84  * Handle for identity service.
85  */
86 static struct GNUNET_IDENTITY_Handle *identity;
87
88 /**
89  * Active operation on identity service.
90  */
91 static struct GNUNET_IDENTITY_Operation *id_op;
92
93 /**
94  * Task scheduled to handle timeout.
95  */
96 static struct GNUNET_SCHEDULER_Task *tt;
97
98 /**
99  * Global return value.
100  * 0 on success (default),
101  * 1 on internal failures, 2 on launch failure,
102  * 3 if the name is not a GNS-supported TLD,
103  * 4 on timeout
104  */
105 static int global_ret;
106
107
108 /**
109  * Task run on shutdown.  Cleans up everything.
110  *
111  * @param cls unused
112  */
113 static void
114 do_shutdown (void *cls)
115 {
116   if (NULL != el)
117   {
118     GNUNET_IDENTITY_ego_lookup_cancel (el);
119     el = NULL;
120   }
121   if (NULL != id_op)
122   {
123     GNUNET_IDENTITY_cancel (id_op);
124     id_op = NULL;
125   }
126   if (NULL != lookup_request)
127   {
128     GNUNET_GNS_lookup_cancel (lookup_request);
129     lookup_request = NULL;
130   }
131   if (NULL != identity)
132   {
133     GNUNET_IDENTITY_disconnect (identity);
134     identity = NULL;
135   }
136   if (NULL != gns)
137   {
138     GNUNET_GNS_disconnect (gns);
139     gns = NULL;
140   }
141   if (NULL != tt)
142   {
143     GNUNET_SCHEDULER_cancel (tt);
144     tt = NULL;
145   }
146 }
147
148
149 /**
150  * Task run on timeout. Triggers shutdown.
151  *
152  * @param cls unused
153  */
154 static void
155 do_timeout (void *cls)
156 {
157   tt = NULL;
158   GNUNET_SCHEDULER_shutdown ();
159   global_ret = 4;
160 }
161
162
163 /**
164  * Function called with the result of a GNS lookup.
165  *
166  * @param cls the 'const char *' name that was resolved
167  * @param rd_count number of records returned
168  * @param rd array of @a rd_count records with the results
169  */
170 static void
171 process_lookup_result (void *cls,
172                        uint32_t rd_count,
173                        const struct GNUNET_GNSRECORD_Data *rd)
174 {
175   const char *name = cls;
176   const char *typename;
177   char* string_val;
178
179   lookup_request = NULL;
180   if (!raw)
181   {
182     if (0 == rd_count)
183       printf ("No results.\n");
184     else
185       printf ("%s:\n",
186               name);
187   }
188   for (uint32_t i=0; i<rd_count; i++)
189   {
190     if ( (rd[i].record_type != rtype) &&
191          (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
192       continue;
193     typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
194     string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
195                                                    rd[i].data,
196                                                    rd[i].data_size);
197     if (NULL == string_val)
198     {
199       fprintf (stderr,
200                "Record %u of type %d malformed, skipping\n",
201                (unsigned int) i,
202                (int) rd[i].record_type);
203       continue;
204     }
205     if (raw)
206       printf ("%s\n",
207               string_val);
208     else
209       printf ("Got `%s' record: %s\n",
210               typename,
211               string_val);
212     GNUNET_free (string_val);
213   }
214   GNUNET_SCHEDULER_shutdown ();
215 }
216
217
218 /**
219  * Perform the actual resolution, starting with the zone
220  * identified by the given public key and the shorten zone.
221  *
222  * @param pkey public key to use for the zone, can be NULL
223  */
224 static void
225 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
226 {
227   if (NULL != lookup_type)
228     rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
229   else
230     rtype = GNUNET_DNSPARSER_TYPE_A;
231   if (UINT32_MAX == rtype)
232   {
233     fprintf (stderr,
234              _("Invalid typename specified, assuming `ANY'\n"));
235     rtype = GNUNET_GNSRECORD_TYPE_ANY;
236   }
237
238   if (NULL != lookup_name)
239   {
240     lookup_request = GNUNET_GNS_lookup (gns,
241                                         lookup_name,
242                                         pkey,
243                                         rtype,
244                                         local_options,
245                                         &process_lookup_result,
246                                         lookup_name);
247   }
248   else
249   {
250     fprintf (stderr,
251              _("Please specify name to lookup!\n"));
252     GNUNET_SCHEDULER_shutdown ();
253     return;
254   }
255 }
256
257
258 /**
259  * Method called to with the ego we are to use for the lookup,
260  * when the ego is determined by a name.
261  *
262  * @param cls closure (NULL, unused)
263  * @param ego ego handle, NULL if not found
264  */
265 static void
266 identity_zone_cb (void *cls,
267                   const struct GNUNET_IDENTITY_Ego *ego)
268 {
269   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
270
271   el = NULL;
272   if (NULL == ego)
273   {
274     global_ret = 3; /* Not a GNS TLD */
275     GNUNET_SCHEDULER_shutdown ();
276   }
277   else
278   {
279     GNUNET_IDENTITY_ego_get_public_key (ego,
280                                         &pkey);
281     lookup_with_public_key (&pkey);
282   }
283 }
284
285
286 /**
287  * Obtain the TLD of the given @a name.
288  *
289  * @param name a name
290  * @return the part of @a name after the last ".",
291  *         or @a name if @a name does not contain a "."
292  */
293 static const char *
294 get_tld (const char *name)
295 {
296   const char *tld;
297
298   tld = strrchr (name,
299                  (unsigned char) '.');
300   if (NULL == tld)
301     tld = name;
302   else
303     tld++; /* skip the '.' */
304   return tld;
305 }
306
307
308 /**
309  * Eat the TLD of the given @a name.
310  *
311  * @param name a name
312  */
313 static void
314 eat_tld (char *name)
315 {
316   char *tld;
317
318   GNUNET_assert (0 < strlen (name));
319   tld = strrchr (name,
320                  (unsigned char) '.');
321   if (NULL == tld)
322     strcpy (name,
323             GNUNET_GNS_MASTERZONE_STR);
324   else
325     *tld = '\0';
326 }
327
328
329 /**
330  * Main function that will be run.
331  *
332  * @param cls closure
333  * @param args remaining command-line arguments
334  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
335  * @param c configuration
336  */
337 static void
338 run (void *cls,
339      char *const *args,
340      const char *cfgfile,
341      const struct GNUNET_CONFIGURATION_Handle *c)
342 {
343   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
344   const char *tld;
345   char *dot_tld;
346   char *zonestr;
347
348   cfg = c;
349   gns = GNUNET_GNS_connect (cfg);
350   if (NULL == gns)
351   {
352     fprintf (stderr,
353              _("Failed to connect to GNS\n"));
354     return;
355   }
356   tt = GNUNET_SCHEDULER_add_delayed (timeout,
357                                      &do_timeout,
358                                      NULL);
359   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
360                                  NULL);
361   /* start with trivial case: TLD is zkey */
362   tld = get_tld (lookup_name);
363   if (GNUNET_OK ==
364       GNUNET_CRYPTO_ecdsa_public_key_from_string (tld,
365                                                   strlen (tld),
366                                                   &pkey))
367   {
368     eat_tld (lookup_name);
369     lookup_with_public_key (&pkey);
370     return;
371   }
372
373   /* second case: TLD is mapped in our configuration file */
374   GNUNET_asprintf (&dot_tld,
375                    ".%s",
376                    tld);
377   if (GNUNET_OK ==
378       GNUNET_CONFIGURATION_get_value_string (cfg,
379                                              "gns",
380                                              dot_tld,
381                                              &zonestr))
382   {
383     if (GNUNET_OK !=
384         GNUNET_CRYPTO_ecdsa_public_key_from_string (zonestr,
385                                                     strlen (zonestr),
386                                                     &pkey))
387     {
388       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
389                                  "gns",
390                                  dot_tld,
391                                  _("Expected a base32-encoded public zone key\n"));
392       GNUNET_free (zonestr);
393       GNUNET_free (dot_tld);
394       GNUNET_SCHEDULER_shutdown ();
395       return;
396     }
397     GNUNET_free (dot_tld);
398     GNUNET_free (zonestr);
399     eat_tld (lookup_name);
400     lookup_with_public_key (&pkey);
401     return;
402   }
403   GNUNET_free (dot_tld);
404
405   /* Final case: TLD matches one of our egos */
406   eat_tld (lookup_name);
407
408   /* if the name is of the form 'label.gnu', never go to the DHT */
409   if (NULL == strchr (lookup_name,
410                       (unsigned char) '.'))
411     local_options = GNUNET_GNS_LO_NO_DHT;
412   identity = GNUNET_IDENTITY_connect (cfg,
413                                       NULL,
414                                       NULL);
415   el = GNUNET_IDENTITY_ego_lookup (cfg,
416                                    tld,
417                                    &identity_zone_cb,
418                                    NULL);
419 }
420
421
422 /**
423  * The main function for gnunet-gns.
424  *
425  * @param argc number of arguments from the command line
426  * @param argv command line arguments
427  * @return 0 ok, 1 on error
428  */
429 int
430 main (int argc,
431       char *const *argv)
432 {
433   struct GNUNET_GETOPT_CommandLineOption options[] = {
434     GNUNET_GETOPT_option_mandatory
435     (GNUNET_GETOPT_option_string ('u',
436                                   "lookup",
437                                   "NAME",
438                                   gettext_noop ("Lookup a record for the given name"),
439                                   &lookup_name)),
440     GNUNET_GETOPT_option_string ('t',
441                                  "type",
442                                  "TYPE",
443                                  gettext_noop ("Specify the type of the record to lookup"),
444                                  &lookup_type),
445     GNUNET_GETOPT_option_relative_time ('T',
446                                         "timeout",
447                                         "DELAY",
448                                         gettext_noop ("Specify timeout for the lookup"),
449                                         &timeout),
450     GNUNET_GETOPT_option_flag ('r',
451                                "raw",
452                                gettext_noop ("No unneeded output"),
453                                &raw),
454     GNUNET_GETOPT_OPTION_END
455   };
456   int ret;
457
458   timeout = GNUNET_TIME_UNIT_FOREVER_REL;
459   if (GNUNET_OK !=
460       GNUNET_STRINGS_get_utf8_args (argc, argv,
461                                     &argc, &argv))
462     return 2;
463
464   GNUNET_log_setup ("gnunet-gns",
465                     "WARNING",
466                     NULL);
467   ret = GNUNET_PROGRAM_run (argc, argv,
468                             "gnunet-gns",
469                             _("GNUnet GNS resolver tool"),
470                             options,
471                             &run, NULL);
472   GNUNET_free ((void*) argv);
473   if (GNUNET_OK != ret)
474     return 1;
475   return global_ret;
476 }
477
478 /* end of gnunet-gns.c */