- interactive option to disable waiting on keystroke but wait instead for termination...
[oweals/gnunet.git] / src / gns / gnunet-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012-2013 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-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  * GNS name to lookup. (-u option)
45  */
46 static char *lookup_name;
47
48 /**
49  * record type to look up (-t option)
50  */
51 static char *lookup_type;
52
53 /**
54  * Identity of the zone to use for the lookup (-z option)
55  */
56 static char *zone_ego_name;
57
58 /**
59  * Public key of the zone to use for the lookup (-p option)
60  */
61 static char *public_key;
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 /**
95  * Task run on shutdown.  Cleans up everything.
96  *
97  * @param cls unused
98  * @param tc scheduler context
99  */
100 static void
101 do_shutdown (void *cls,
102              const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   if (NULL != el)
105   {
106     GNUNET_IDENTITY_ego_lookup_cancel (el);
107     el = NULL;
108   }
109   if (NULL != id_op)
110   {
111     GNUNET_IDENTITY_cancel (id_op);
112     id_op = NULL;
113   }
114   if (NULL != lookup_request)
115   {
116     GNUNET_GNS_lookup_cancel (lookup_request);
117     lookup_request = NULL;
118   }
119   if (NULL != identity)
120   {
121     GNUNET_IDENTITY_disconnect (identity);
122     identity = NULL;
123   }
124   if (NULL != gns)
125   {
126     GNUNET_GNS_disconnect (gns);
127     gns = NULL;
128   }
129 }
130
131
132 /**
133  * Function called with the result of a GNS lookup.
134  *
135  * @param cls the 'const char *' name that was resolved
136  * @param rd_count number of records returned
137  * @param rd array of @a rd_count records with the results
138  */
139 static void
140 process_lookup_result (void *cls, uint32_t rd_count,
141                        const struct GNUNET_GNSRECORD_Data *rd)
142 {
143   const char *name = cls;
144   uint32_t i;
145   const char *typename;
146   char* string_val;
147
148   lookup_request = NULL;
149   if (!raw)
150   {
151     if (0 == rd_count)
152       printf ("No results.\n");
153     else
154       printf ("%s:\n",
155               name);
156   }
157   for (i=0; i<rd_count; i++)
158   {
159     if ( (rd[i].record_type != rtype) &&
160          (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
161       continue;
162     typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
163     string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
164                                                    rd[i].data,
165                                                    rd[i].data_size);
166     if (NULL == string_val)
167     {
168       fprintf (stderr,
169                "Record %u of type %d malformed, skipping\n",
170                (unsigned int) i,
171                (int) rd[i].record_type);
172       continue;
173     }
174     if (raw)
175       printf ("%s\n",
176               string_val);
177     else
178       printf ("Got `%s' record: %s\n",
179               typename,
180               string_val);
181     GNUNET_free (string_val);
182   }
183   GNUNET_SCHEDULER_shutdown ();
184 }
185
186
187 /**
188  * Perform the actual resolution, starting with the zone
189  * identified by the given public key and the shorten zone.
190  *
191  * @param pkey public key to use for the zone, can be NULL
192  * @param shorten_key private key used for shortening, can be NULL
193  */
194 static void
195 lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
196                   const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
197 {
198   if (NULL != lookup_type)
199     rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
200   else
201     rtype = GNUNET_DNSPARSER_TYPE_A;
202
203   if (NULL != lookup_name)
204   {
205     lookup_request = GNUNET_GNS_lookup (gns,
206                                         lookup_name,
207                                         pkey,
208                                         rtype,
209                                         GNUNET_NO, /* Use DHT */
210                                         shorten_key,
211                                         &process_lookup_result,
212                                         lookup_name);
213   }
214   else
215   {
216     fprintf (stderr,
217              _("Please specify name to lookup!\n"));
218     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
219     return;
220   }
221 }
222
223
224 /**
225  * Method called to with the ego we are to use for shortening
226  * during the lookup.
227  *
228  * @param cls closure contains the public key to use
229  * @param ego ego handle, NULL if not found
230  * @param ctx context for application to store data for this ego
231  *                 (during the lifetime of this process, initially NULL)
232  * @param name name assigned by the user for this ego,
233  *                   NULL if the user just deleted the ego and it
234  *                   must thus no longer be used
235  */
236 static void
237 identity_shorten_cb (void *cls,
238                      struct GNUNET_IDENTITY_Ego *ego,
239                      void **ctx,
240                      const char *name)
241 {
242   struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
243
244   id_op = NULL;
245   if (NULL == ego)
246     lookup_with_keys (pkeym, NULL);
247   else
248     lookup_with_keys (pkeym,
249                       GNUNET_IDENTITY_ego_get_private_key (ego));
250   GNUNET_free (pkeym);
251 }
252
253
254 /**
255  * Perform the actual resolution, starting with the zone
256  * identified by the given public key.
257  *
258  * @param pkey public key to use for the zone
259  */
260 static void
261 lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
262 {
263   struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
264
265   GNUNET_assert (NULL != pkey);
266   pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
267   *pkeym = *pkey;
268   id_op = GNUNET_IDENTITY_get (identity,
269                                "gns-short",
270                                &identity_shorten_cb,
271                                pkeym);
272   if (NULL == id_op)
273   {
274     GNUNET_break (0);
275     lookup_with_keys (pkey, NULL);
276   }
277 }
278
279
280 /**
281  * Method called to with the ego we are to use for the lookup,
282  * when the ego is determined by a name.
283  *
284  * @param cls closure (NULL, unused)
285  * @param ego ego handle, NULL if not found
286  */
287 static void
288 identity_zone_cb (void *cls,
289                   const struct GNUNET_IDENTITY_Ego *ego)
290 {
291   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
292
293   el = NULL;
294   if (NULL == ego)
295   {
296     fprintf (stderr,
297              _("Ego for `%s' not found, cannot perform lookup.\n"),
298              zone_ego_name);
299     GNUNET_SCHEDULER_shutdown ();
300   }
301   else
302   {
303     GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
304     lookup_with_public_key (&pkey);
305   }
306   GNUNET_free_non_null (zone_ego_name);
307   zone_ego_name = NULL;
308 }
309
310
311 /**
312  * Method called to with the ego we are to use for the lookup,
313  * when the ego is the one for the default master zone.
314  *
315  * @param cls closure (NULL, unused)
316  * @param ego ego handle, NULL if not found
317  * @param ctx context for application to store data for this ego
318  *                 (during the lifetime of this process, initially NULL)
319  * @param name name assigned by the user for this ego,
320  *                   NULL if the user just deleted the ego and it
321  *                   must thus no longer be used
322  */
323 static void
324 identity_master_cb (void *cls,
325                     struct GNUNET_IDENTITY_Ego *ego,
326                     void **ctx,
327                     const char *name)
328 {
329   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
330
331   id_op = NULL;
332   if (NULL == ego)
333   {
334     fprintf (stderr,
335              _("Ego for `master-zone' not found, cannot perform lookup.  Did you run gnunet-gns-import.sh?\n"));
336     GNUNET_SCHEDULER_shutdown ();
337     return;
338   }
339   GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
340   lookup_with_public_key (&pkey);
341 }
342
343
344 /**
345  * Main function that will be run.
346  *
347  * @param cls closure
348  * @param args remaining command-line arguments
349  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
350  * @param c configuration
351  */
352 static void
353 run (void *cls, char *const *args, const char *cfgfile,
354      const struct GNUNET_CONFIGURATION_Handle *c)
355 {
356   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
357
358   cfg = c;
359   gns = GNUNET_GNS_connect (cfg);
360   identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
361   if (NULL == gns)
362   {
363     fprintf (stderr,
364              _("Failed to connect to GNS\n"));
365     return;
366   }
367   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
368                                 &do_shutdown, NULL);
369   if (NULL != public_key)
370   {
371     if (GNUNET_OK !=
372         GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
373                                                   strlen (public_key),
374                                                   &pkey))
375     {
376       fprintf (stderr,
377                _("Public key `%s' is not well-formed\n"),
378                public_key);
379       GNUNET_SCHEDULER_shutdown ();
380       return;
381     }
382     lookup_with_public_key (&pkey);
383     return;
384   }
385   if (NULL != zone_ego_name)
386   {
387     el = GNUNET_IDENTITY_ego_lookup (cfg,
388                                      zone_ego_name,
389                                      &identity_zone_cb,
390                                      NULL);
391     return;
392   }
393   if ( (NULL != lookup_name) &&
394        (strlen (lookup_name) > 4) &&
395        (0 == strcmp (".zkey",
396                      &lookup_name[strlen (lookup_name) - 4])) )
397   {
398     /* no zone required, use 'anonymous' zone */
399     GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
400                                       &pkey);
401     lookup_with_public_key (&pkey);
402   }
403   else
404   {
405     id_op = GNUNET_IDENTITY_get (identity,
406                                  "master-zone",
407                                  &identity_master_cb,
408                                  NULL);
409     GNUNET_assert (NULL != id_op);
410   }
411 }
412
413
414 /**
415  * The main function for gnunet-gns.
416  *
417  * @param argc number of arguments from the command line
418  * @param argv command line arguments
419  * @return 0 ok, 1 on error
420  */
421 int
422 main (int argc, char *const *argv)
423 {
424   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
425     {'u', "lookup", "NAME",
426       gettext_noop ("Lookup a record for the given name"), 1,
427       &GNUNET_GETOPT_set_string, &lookup_name},
428     {'t', "type", "TYPE",
429       gettext_noop ("Specify the type of the record to lookup"), 1,
430       &GNUNET_GETOPT_set_string, &lookup_type},
431     {'r', "raw", NULL,
432       gettext_noop ("No unneeded output"), 0,
433       &GNUNET_GETOPT_set_one, &raw},
434     {'p', "public-key", "PKEY",
435       gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
436       &GNUNET_GETOPT_set_string, &public_key},
437     {'z', "zone", "NAME",
438       gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
439       &GNUNET_GETOPT_set_string, &zone_ego_name},
440     GNUNET_GETOPT_OPTION_END
441   };
442   int ret;
443
444   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
445     return 2;
446
447   GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
448   ret =
449       (GNUNET_OK ==
450        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
451                            _("GNUnet GNS resolver tool"),
452                            options,
453                            &run, NULL)) ? 0 : 1;
454   GNUNET_free ((void*) argv);
455   return ret;
456 }
457
458 /* end of gnunet-gns.c */