-fix tld guessing code
[oweals/gnunet.git] / src / gns / gnunet-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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  */
26 #include "platform.h"
27 #include <gnunet_util_lib.h>
28 #include <gnunet_dnsparser_lib.h>
29 #include <gnunet_namestore_service.h>
30 #include <gnunet_gns_service.h>
31
32 /**
33  * Handle to GNS service.
34  */
35 static struct GNUNET_GNS_Handle *gns;
36
37 /**
38  * GNS name to shorten. (-s option)
39  */
40 static char *shorten_name;
41
42 /**
43  * GNS name to lookup. (-u option)
44  */
45 static char *lookup_name;
46
47
48 /**
49  * record type to look up (-t option)
50  */
51 static char *lookup_type;
52
53 /**
54  * name to look up authority for (-a option)
55  */
56 static char *auth_name;
57
58 /**
59  * raw output
60  */
61 static int raw;
62
63 static enum GNUNET_GNS_RecordType rtype;
64
65 /* Handle to lookup request */
66 static struct GNUNET_GNS_LookupRequest *lookup_request;
67
68 /* Handle to shorten request */
69 static struct GNUNET_GNS_ShortenRequest *shorten_request;
70
71 /* Handle to get authority request */
72 static struct GNUNET_GNS_GetAuthRequest *getauth_request;
73
74
75 /**
76  * Task run on shutdown.  Cleans up everything.
77  *
78  * @param cls unused
79  * @param tc scheduler context
80  */
81 static void
82 do_shutdown (void *cls,
83              const struct GNUNET_SCHEDULER_TaskContext *tc)
84 {
85   if (NULL != lookup_request)
86     GNUNET_GNS_cancel_lookup_request (lookup_request);
87
88   if (NULL != shorten_request)
89     GNUNET_GNS_cancel_shorten_request (shorten_request);
90
91   if (NULL != getauth_request)
92     GNUNET_GNS_cancel_get_auth_request (getauth_request);
93
94   if (NULL != gns)
95     GNUNET_GNS_disconnect (gns);
96 }
97
98
99 static void
100 process_shorten_result (void* cls, const char* nshort)
101 {
102   const char *original_name = cls;
103
104   shorten_request = NULL;
105   if (raw)
106     printf("%s", nshort);
107   else
108     printf("%s shortened to %s\n", original_name, nshort);
109   GNUNET_SCHEDULER_shutdown ();
110 }
111
112
113 static void
114 process_lookup_result (void* cls, uint32_t rd_count,
115                        const struct GNUNET_NAMESTORE_RecordData *rd)
116 {
117   const char* name = cls;
118   uint32_t i;
119   const char* typename;
120   char* string_val;
121
122   lookup_request = NULL; 
123   if (!raw) 
124   {
125     if (rd_count == 0)
126       printf("No results.\n");
127     else
128       printf("%s:\n", name);
129   }
130   for (i=0; i<rd_count; i++)
131   {
132     typename = GNUNET_NAMESTORE_number_to_typename (rd[i].record_type);
133     string_val = GNUNET_NAMESTORE_value_to_string (rd[i].record_type,
134                                                    rd[i].data,
135                                                    rd[i].data_size);
136     if (raw)
137       printf ("%s\n", string_val);
138     else
139       printf ("Got `%s' record: %s\n", typename, string_val);
140     GNUNET_free_non_null (string_val);
141   }
142   GNUNET_SCHEDULER_shutdown ();
143 }
144
145
146 static void
147 process_auth_result (void* cls, const char* auth)
148 {
149   getauth_request = NULL;
150   printf ("%s\n", auth);
151   GNUNET_SCHEDULER_shutdown ();
152 }
153
154
155 /**
156  * Main function that will be run.
157  *
158  * @param cls closure
159  * @param args remaining command-line arguments
160  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
161  * @param cfg configuration
162  */
163 static void
164 run (void *cls, char *const *args, const char *cfgfile,
165      const struct GNUNET_CONFIGURATION_Handle *cfg)
166 {
167   char* keyfile;
168   struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
169   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
170   struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
171   struct GNUNET_CRYPTO_ShortHashCode user_zone;
172   struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
173   struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key = NULL;
174   struct GNUNET_CRYPTO_RsaPrivateKey *private_key = NULL;
175   struct GNUNET_CRYPTO_ShortHashCode *private_zone = NULL;
176   struct GNUNET_CRYPTO_ShortHashCode *shorten_zone = NULL;
177
178   gns = GNUNET_GNS_connect (cfg);
179   if (NULL == gns)
180   {
181     fprintf (stderr,
182              _("Failed to connect to GNS\n"));
183     return;
184   }
185   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
186                                                            "ZONEKEY", &keyfile))
187   {
188     if (! raw)
189       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
190                   "No private key for root zone found, using default!\n");
191   }
192   else
193   {
194     key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
195     GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
196     GNUNET_CRYPTO_short_hash (&pkey,
197                               sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
198                               &user_zone);
199     zone = &user_zone;
200     GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
201     if (!raw)
202       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203                   "Using zone: %s!\n", &zonename);
204     GNUNET_CRYPTO_rsa_key_free (key);  
205     GNUNET_free(keyfile);
206   }
207   
208   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
209                                                             "SHORTEN_ZONEKEY", &keyfile))
210   {
211     if (! raw)
212       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
213                   "No shorten key found!\n");
214   }
215   else
216   {
217     shorten_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
218     GNUNET_CRYPTO_rsa_key_get_public (shorten_key, &pkey);
219     shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
220     GNUNET_CRYPTO_short_hash(&pkey,
221                              sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
222                              shorten_zone);
223     GNUNET_CRYPTO_short_hash_to_enc (shorten_zone, &zonename);
224     if (! raw)
225       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226                   "Using shorten zone: %s!\n", &zonename);
227     GNUNET_free (keyfile);
228   }
229     
230   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
231                                                             "PRIVATE_ZONEKEY", &keyfile))
232   {
233     if (! raw)
234       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235                   "No private zone key file name specified in configuration!\n");
236   }
237   else
238   {
239     private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
240     GNUNET_CRYPTO_rsa_key_get_public (private_key, &pkey);
241     private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
242     GNUNET_CRYPTO_short_hash(&pkey,
243                              sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
244                              private_zone);
245     GNUNET_CRYPTO_short_hash_to_enc (private_zone, &zonename);
246     if (! raw)
247       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248                   "Using private zone: %s!\n", &zonename);
249     GNUNET_CRYPTO_rsa_key_free (private_key);
250   }
251   
252   if (NULL != lookup_type)
253     rtype = GNUNET_NAMESTORE_typename_to_number (lookup_type);
254   else
255     rtype = GNUNET_GNS_RECORD_A;
256
257   if ((NULL != shorten_name) && (NULL != shorten_zone) && (NULL != private_zone))
258   {
259     shorten_request = GNUNET_GNS_shorten_zone (gns, shorten_name,
260                              private_zone,
261                              shorten_zone,
262                              zone,
263                              &process_shorten_result,
264                              shorten_name);
265   }
266   if (NULL != lookup_name)
267   {
268     lookup_request = GNUNET_GNS_lookup_zone (gns, lookup_name,
269                                              zone,
270                                              rtype,
271                                              GNUNET_NO, /* Use DHT */
272                                              shorten_key,
273                                              &process_lookup_result, lookup_name);
274   }
275   if (NULL != auth_name)
276   {
277     getauth_request = GNUNET_GNS_get_authority (gns, auth_name,
278                                                 &process_auth_result, auth_name);
279   }
280
281   if (NULL != shorten_key)
282     GNUNET_CRYPTO_rsa_key_free (shorten_key);
283   if (NULL != shorten_zone)
284     GNUNET_free (shorten_zone);
285   if (NULL != private_zone)
286     GNUNET_free (private_zone);
287   
288   if ((NULL == auth_name) &&
289       (NULL == shorten_name) &&
290       (NULL == lookup_name))
291   {
292     fprintf (stderr,
293              _("Please specify lookup, shorten or authority operation!\n"));
294     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
295     return;
296   }
297
298   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
299                                 &do_shutdown, NULL);
300 }
301
302
303 /**
304  * The main function for gnunet-gns.
305  *
306  * @param argc number of arguments from the command line
307  * @param argv command line arguments
308  * @return 0 ok, 1 on error
309  */
310 int
311 main (int argc, char *const *argv)
312 {
313   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
314     {'s', "shorten", NULL,
315      gettext_noop ("try to shorten a given name"), 1,
316      &GNUNET_GETOPT_set_string, &shorten_name},
317     {'u', "lookup", NULL,
318       gettext_noop ("Lookup a record for the given name"), 1,
319       &GNUNET_GETOPT_set_string, &lookup_name},
320     {'a', "authority", NULL,
321       gettext_noop ("Get the authority of a particular name"), 1,
322       &GNUNET_GETOPT_set_string, &auth_name},
323     {'t', "type", NULL,
324       gettext_noop ("Specify the type of the record to lookup"), 1,
325       &GNUNET_GETOPT_set_string, &lookup_type},
326     {'r', "raw", NULL,
327       gettext_noop ("No unneeded output"), 0,
328       &GNUNET_GETOPT_set_one, &raw},
329     GNUNET_GETOPT_OPTION_END
330   };
331
332   int ret;
333
334   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
335     return 2;
336
337   GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
338   ret =
339       (GNUNET_OK ==
340        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
341                            _("GNUnet GNS access tool"), 
342                            options,
343                            &run, NULL)) ? 0 : 1;
344
345   return ret;
346 }
347
348 /* end of gnunet-gns.c */