- formatting
[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_shutdown ();
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   GNUNET_break (NULL == id_op);
269   id_op = GNUNET_IDENTITY_get (identity,
270                                "gns-short",
271                                &identity_shorten_cb,
272                                pkeym);
273   if (NULL == id_op)
274   {
275     GNUNET_break (0);
276     lookup_with_keys (pkey, NULL);
277   }
278 }
279
280
281 /**
282  * Method called to with the ego we are to use for the lookup,
283  * when the ego is determined by a name.
284  *
285  * @param cls closure (NULL, unused)
286  * @param ego ego handle, NULL if not found
287  */
288 static void
289 identity_zone_cb (void *cls,
290                   const struct GNUNET_IDENTITY_Ego *ego)
291 {
292   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
293
294   el = NULL;
295   if (NULL == ego)
296   {
297     fprintf (stderr,
298              _("Ego for `%s' not found, cannot perform lookup.\n"),
299              zone_ego_name);
300     GNUNET_SCHEDULER_shutdown ();
301   }
302   else
303   {
304     GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
305     lookup_with_public_key (&pkey);
306   }
307   GNUNET_free_non_null (zone_ego_name);
308   zone_ego_name = NULL;
309 }
310
311
312 /**
313  * Method called to with the ego we are to use for the lookup,
314  * when the ego is the one for the default master zone.
315  *
316  * @param cls closure (NULL, unused)
317  * @param ego ego handle, NULL if not found
318  * @param ctx context for application to store data for this ego
319  *                 (during the lifetime of this process, initially NULL)
320  * @param name name assigned by the user for this ego,
321  *                   NULL if the user just deleted the ego and it
322  *                   must thus no longer be used
323  */
324 static void
325 identity_master_cb (void *cls,
326                     struct GNUNET_IDENTITY_Ego *ego,
327                     void **ctx,
328                     const char *name)
329 {
330   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
331
332   id_op = NULL;
333   if (NULL == ego)
334   {
335     fprintf (stderr,
336              _("Ego for `gns-master' not found, cannot perform lookup.  Did you run gnunet-gns-import.sh?\n"));
337     GNUNET_SCHEDULER_shutdown ();
338     return;
339   }
340   GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
341   lookup_with_public_key (&pkey);
342 }
343
344
345 /**
346  * Main function that will be run.
347  *
348  * @param cls closure
349  * @param args remaining command-line arguments
350  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
351  * @param c configuration
352  */
353 static void
354 run (void *cls, char *const *args, const char *cfgfile,
355      const struct GNUNET_CONFIGURATION_Handle *c)
356 {
357   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
358
359   cfg = c;
360   gns = GNUNET_GNS_connect (cfg);
361   identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
362   if (NULL == gns)
363   {
364     fprintf (stderr,
365              _("Failed to connect to GNS\n"));
366     return;
367   }
368   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
369                                 &do_shutdown, NULL);
370   if (NULL != public_key)
371   {
372     if (GNUNET_OK !=
373         GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
374                                                   strlen (public_key),
375                                                   &pkey))
376     {
377       fprintf (stderr,
378                _("Public key `%s' is not well-formed\n"),
379                public_key);
380       GNUNET_SCHEDULER_shutdown ();
381       return;
382     }
383     lookup_with_public_key (&pkey);
384     return;
385   }
386   if (NULL != zone_ego_name)
387   {
388     el = GNUNET_IDENTITY_ego_lookup (cfg,
389                                      zone_ego_name,
390                                      &identity_zone_cb,
391                                      NULL);
392     return;
393   }
394   if ( (NULL != lookup_name) &&
395        (strlen (lookup_name) > 4) &&
396        (0 == strcmp (".zkey",
397                      &lookup_name[strlen (lookup_name) - 4])) )
398   {
399     /* no zone required, use 'anonymous' zone */
400     GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
401                                       &pkey);
402     lookup_with_public_key (&pkey);
403   }
404   else
405   {
406     GNUNET_break (NULL == id_op);
407     id_op = GNUNET_IDENTITY_get (identity,
408                                  "gns-master",
409                                  &identity_master_cb,
410                                  NULL);
411     GNUNET_assert (NULL != id_op);
412   }
413 }
414
415
416 /**
417  * The main function for gnunet-gns.
418  *
419  * @param argc number of arguments from the command line
420  * @param argv command line arguments
421  * @return 0 ok, 1 on error
422  */
423 int
424 main (int argc, char *const *argv)
425 {
426   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
427     {'u', "lookup", "NAME",
428       gettext_noop ("Lookup a record for the given name"), 1,
429       &GNUNET_GETOPT_set_string, &lookup_name},
430     {'t', "type", "TYPE",
431       gettext_noop ("Specify the type of the record to lookup"), 1,
432       &GNUNET_GETOPT_set_string, &lookup_type},
433     {'r', "raw", NULL,
434       gettext_noop ("No unneeded output"), 0,
435       &GNUNET_GETOPT_set_one, &raw},
436     {'p', "public-key", "PKEY",
437       gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
438       &GNUNET_GETOPT_set_string, &public_key},
439     {'z', "zone", "NAME",
440       gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
441       &GNUNET_GETOPT_set_string, &zone_ego_name},
442     GNUNET_GETOPT_OPTION_END
443   };
444   int ret;
445
446   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
447     return 2;
448
449   GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
450   ret =
451       (GNUNET_OK ==
452        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
453                            _("GNUnet GNS resolver tool"),
454                            options,
455                            &run, NULL)) ? 0 : 1;
456   GNUNET_free ((void*) argv);
457   return ret;
458 }
459
460 /* end of gnunet-gns.c */