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