gnunet-gns.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2012-2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file gnunet-gns.c
  19. * @brief command line tool to access distributed GNS
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include <gnunet_util_lib.h>
  24. #include <gnunet_dnsparser_lib.h>
  25. #include <gnunet_identity_service.h>
  26. #include <gnunet_gnsrecord_lib.h>
  27. #include <gnunet_namestore_service.h>
  28. #include <gnunet_gns_service.h>
  29. /**
  30. * Configuration we are using.
  31. */
  32. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  33. /**
  34. * Handle to GNS service.
  35. */
  36. static struct GNUNET_GNS_Handle *gns;
  37. /**
  38. * Desired timeout for the lookup (default is no timeout).
  39. */
  40. static struct GNUNET_TIME_Relative timeout;
  41. /**
  42. * GNS name to lookup. (-u option)
  43. */
  44. static char *lookup_name;
  45. /**
  46. * record type to look up (-t option)
  47. */
  48. static char *lookup_type;
  49. /**
  50. * Identity of the zone to use for the lookup (-z option)
  51. */
  52. static char *zone_ego_name;
  53. /**
  54. * Public key of the zone to use for the lookup (-p option)
  55. */
  56. static char *public_key;
  57. /**
  58. * Set to GNUNET_GNS_LO_LOCAL_MASTER if we are looking up in the master zone.
  59. */
  60. static enum GNUNET_GNS_LocalOptions local_options;
  61. /**
  62. * raw output
  63. */
  64. static int raw;
  65. /**
  66. * Requested record type.
  67. */
  68. static int rtype;
  69. /**
  70. * Handle to lookup request
  71. */
  72. static struct GNUNET_GNS_LookupRequest *lookup_request;
  73. /**
  74. * Lookup an ego with the identity service.
  75. */
  76. static struct GNUNET_IDENTITY_EgoLookup *el;
  77. /**
  78. * Handle for identity service.
  79. */
  80. static struct GNUNET_IDENTITY_Handle *identity;
  81. /**
  82. * Active operation on identity service.
  83. */
  84. static struct GNUNET_IDENTITY_Operation *id_op;
  85. /**
  86. * Task run on shutdown. Cleans up everything.
  87. *
  88. * @param cls unused
  89. * @param tc scheduler context
  90. */
  91. static void
  92. do_shutdown (void *cls,
  93. const struct GNUNET_SCHEDULER_TaskContext *tc)
  94. {
  95. if (NULL != el)
  96. {
  97. GNUNET_IDENTITY_ego_lookup_cancel (el);
  98. el = NULL;
  99. }
  100. if (NULL != id_op)
  101. {
  102. GNUNET_IDENTITY_cancel (id_op);
  103. id_op = NULL;
  104. }
  105. if (NULL != lookup_request)
  106. {
  107. GNUNET_GNS_lookup_cancel (lookup_request);
  108. lookup_request = NULL;
  109. }
  110. if (NULL != identity)
  111. {
  112. GNUNET_IDENTITY_disconnect (identity);
  113. identity = NULL;
  114. }
  115. if (NULL != gns)
  116. {
  117. GNUNET_GNS_disconnect (gns);
  118. gns = NULL;
  119. }
  120. }
  121. /**
  122. * Function called with the result of a GNS lookup.
  123. *
  124. * @param cls the 'const char *' name that was resolved
  125. * @param rd_count number of records returned
  126. * @param rd array of @a rd_count records with the results
  127. */
  128. static void
  129. process_lookup_result (void *cls, uint32_t rd_count,
  130. const struct GNUNET_GNSRECORD_Data *rd)
  131. {
  132. const char *name = cls;
  133. uint32_t i;
  134. const char *typename;
  135. char* string_val;
  136. lookup_request = NULL;
  137. if (!raw)
  138. {
  139. if (0 == rd_count)
  140. printf ("No results.\n");
  141. else
  142. printf ("%s:\n",
  143. name);
  144. }
  145. for (i=0; i<rd_count; i++)
  146. {
  147. if ( (rd[i].record_type != rtype) &&
  148. (GNUNET_GNSRECORD_TYPE_ANY != rtype) )
  149. continue;
  150. typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
  151. string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  152. rd[i].data,
  153. rd[i].data_size);
  154. if (NULL == string_val)
  155. {
  156. fprintf (stderr,
  157. "Record %u of type %d malformed, skipping\n",
  158. (unsigned int) i,
  159. (int) rd[i].record_type);
  160. continue;
  161. }
  162. if (raw)
  163. printf ("%s\n",
  164. string_val);
  165. else
  166. printf ("Got `%s' record: %s\n",
  167. typename,
  168. string_val);
  169. GNUNET_free (string_val);
  170. }
  171. GNUNET_SCHEDULER_shutdown ();
  172. }
  173. /**
  174. * Perform the actual resolution, starting with the zone
  175. * identified by the given public key and the shorten zone.
  176. *
  177. * @param pkey public key to use for the zone, can be NULL
  178. * @param shorten_key private key used for shortening, can be NULL
  179. */
  180. static void
  181. lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
  182. const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
  183. {
  184. if (NULL != lookup_type)
  185. rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
  186. else
  187. rtype = GNUNET_DNSPARSER_TYPE_A;
  188. if (UINT32_MAX == rtype)
  189. {
  190. fprintf (stderr,
  191. _("Invalid typename specified, assuming `ANY'\n"));
  192. rtype = GNUNET_GNSRECORD_TYPE_ANY;
  193. }
  194. if (NULL != lookup_name)
  195. {
  196. lookup_request = GNUNET_GNS_lookup (gns,
  197. lookup_name,
  198. pkey,
  199. rtype,
  200. local_options,
  201. shorten_key,
  202. &process_lookup_result,
  203. lookup_name);
  204. }
  205. else
  206. {
  207. fprintf (stderr,
  208. _("Please specify name to lookup!\n"));
  209. GNUNET_SCHEDULER_shutdown ();
  210. return;
  211. }
  212. }
  213. /**
  214. * Method called to with the ego we are to use for shortening
  215. * during the lookup.
  216. *
  217. * @param cls closure contains the public key to use
  218. * @param ego ego handle, NULL if not found
  219. * @param ctx context for application to store data for this ego
  220. * (during the lifetime of this process, initially NULL)
  221. * @param name name assigned by the user for this ego,
  222. * NULL if the user just deleted the ego and it
  223. * must thus no longer be used
  224. */
  225. static void
  226. identity_shorten_cb (void *cls,
  227. struct GNUNET_IDENTITY_Ego *ego,
  228. void **ctx,
  229. const char *name)
  230. {
  231. struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym = cls;
  232. id_op = NULL;
  233. if (NULL == ego)
  234. lookup_with_keys (pkeym, NULL);
  235. else
  236. lookup_with_keys (pkeym,
  237. GNUNET_IDENTITY_ego_get_private_key (ego));
  238. GNUNET_free (pkeym);
  239. }
  240. /**
  241. * Perform the actual resolution, starting with the zone
  242. * identified by the given public key.
  243. *
  244. * @param pkey public key to use for the zone
  245. */
  246. static void
  247. lookup_with_public_key (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
  248. {
  249. struct GNUNET_CRYPTO_EcdsaPublicKey *pkeym;
  250. GNUNET_assert (NULL != pkey);
  251. pkeym = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
  252. *pkeym = *pkey;
  253. GNUNET_break (NULL == id_op);
  254. id_op = GNUNET_IDENTITY_get (identity,
  255. "gns-short",
  256. &identity_shorten_cb,
  257. pkeym);
  258. if (NULL == id_op)
  259. {
  260. GNUNET_break (0);
  261. lookup_with_keys (pkey, NULL);
  262. }
  263. }
  264. /**
  265. * Method called to with the ego we are to use for the lookup,
  266. * when the ego is determined by a name.
  267. *
  268. * @param cls closure (NULL, unused)
  269. * @param ego ego handle, NULL if not found
  270. */
  271. static void
  272. identity_zone_cb (void *cls,
  273. const struct GNUNET_IDENTITY_Ego *ego)
  274. {
  275. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  276. el = NULL;
  277. if (NULL == ego)
  278. {
  279. fprintf (stderr,
  280. _("Ego for `%s' not found, cannot perform lookup.\n"),
  281. zone_ego_name);
  282. GNUNET_SCHEDULER_shutdown ();
  283. }
  284. else
  285. {
  286. GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
  287. lookup_with_public_key (&pkey);
  288. }
  289. GNUNET_free_non_null (zone_ego_name);
  290. zone_ego_name = NULL;
  291. }
  292. /**
  293. * Method called to with the ego we are to use for the lookup,
  294. * when the ego is the one for the default master zone.
  295. *
  296. * @param cls closure (NULL, unused)
  297. * @param ego ego handle, NULL if not found
  298. * @param ctx context for application to store data for this ego
  299. * (during the lifetime of this process, initially NULL)
  300. * @param name name assigned by the user for this ego,
  301. * NULL if the user just deleted the ego and it
  302. * must thus no longer be used
  303. */
  304. static void
  305. identity_master_cb (void *cls,
  306. struct GNUNET_IDENTITY_Ego *ego,
  307. void **ctx,
  308. const char *name)
  309. {
  310. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  311. const char *dot;
  312. id_op = NULL;
  313. if (NULL == ego)
  314. {
  315. fprintf (stderr,
  316. _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
  317. GNUNET_SCHEDULER_shutdown ();
  318. return;
  319. }
  320. GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
  321. /* main name is our own master zone, do no look for that in the DHT */
  322. local_options = GNUNET_GNS_LO_LOCAL_MASTER;
  323. /* if the name is of the form 'label.gnu', never go to the DHT */
  324. dot = NULL;
  325. if (NULL != lookup_name)
  326. dot = strchr (lookup_name, '.');
  327. if ( (NULL != dot) &&
  328. (0 == strcasecmp (dot, ".gnu")) )
  329. local_options = GNUNET_GNS_LO_NO_DHT;
  330. lookup_with_public_key (&pkey);
  331. }
  332. /**
  333. * Main function that will be run.
  334. *
  335. * @param cls closure
  336. * @param args remaining command-line arguments
  337. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  338. * @param c configuration
  339. */
  340. static void
  341. run (void *cls, char *const *args, const char *cfgfile,
  342. const struct GNUNET_CONFIGURATION_Handle *c)
  343. {
  344. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  345. cfg = c;
  346. gns = GNUNET_GNS_connect (cfg);
  347. identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
  348. if (NULL == gns)
  349. {
  350. fprintf (stderr,
  351. _("Failed to connect to GNS\n"));
  352. return;
  353. }
  354. GNUNET_SCHEDULER_add_delayed (timeout,
  355. &do_shutdown, NULL);
  356. if (NULL != public_key)
  357. {
  358. if (GNUNET_OK !=
  359. GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
  360. strlen (public_key),
  361. &pkey))
  362. {
  363. fprintf (stderr,
  364. _("Public key `%s' is not well-formed\n"),
  365. public_key);
  366. GNUNET_SCHEDULER_shutdown ();
  367. return;
  368. }
  369. lookup_with_public_key (&pkey);
  370. return;
  371. }
  372. if (NULL != zone_ego_name)
  373. {
  374. el = GNUNET_IDENTITY_ego_lookup (cfg,
  375. zone_ego_name,
  376. &identity_zone_cb,
  377. NULL);
  378. return;
  379. }
  380. if ( (NULL != lookup_name) &&
  381. (strlen (lookup_name) > 4) &&
  382. (0 == strcmp (".zkey",
  383. &lookup_name[strlen (lookup_name) - 4])) )
  384. {
  385. /* no zone required, use 'anonymous' zone */
  386. GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
  387. &pkey);
  388. lookup_with_public_key (&pkey);
  389. }
  390. else
  391. {
  392. GNUNET_break (NULL == id_op);
  393. id_op = GNUNET_IDENTITY_get (identity,
  394. "gns-master",
  395. &identity_master_cb,
  396. NULL);
  397. GNUNET_assert (NULL != id_op);
  398. }
  399. }
  400. /**
  401. * The main function for gnunet-gns.
  402. *
  403. * @param argc number of arguments from the command line
  404. * @param argv command line arguments
  405. * @return 0 ok, 1 on error
  406. */
  407. int
  408. main (int argc, char *const *argv)
  409. {
  410. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  411. {'u', "lookup", "NAME",
  412. gettext_noop ("Lookup a record for the given name"), 1,
  413. &GNUNET_GETOPT_set_string, &lookup_name},
  414. {'t', "type", "TYPE",
  415. gettext_noop ("Specify the type of the record to lookup"), 1,
  416. &GNUNET_GETOPT_set_string, &lookup_type},
  417. { 'T', "timeout", "DELAY",
  418. gettext_noop ("Specify timeout for the lookup"), 1,
  419. &GNUNET_GETOPT_set_relative_time, &timeout },
  420. {'r', "raw", NULL,
  421. gettext_noop ("No unneeded output"), 0,
  422. &GNUNET_GETOPT_set_one, &raw},
  423. {'p', "public-key", "PKEY",
  424. gettext_noop ("Specify the public key of the zone to lookup the record in"), 1,
  425. &GNUNET_GETOPT_set_string, &public_key},
  426. {'z', "zone", "NAME",
  427. gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
  428. &GNUNET_GETOPT_set_string, &zone_ego_name},
  429. GNUNET_GETOPT_OPTION_END
  430. };
  431. int ret;
  432. timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  433. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  434. return 2;
  435. GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
  436. ret =
  437. (GNUNET_OK ==
  438. GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
  439. _("GNUnet GNS resolver tool"),
  440. options,
  441. &run, NULL)) ? 0 : 1;
  442. GNUNET_free ((void*) argv);
  443. return ret;
  444. }
  445. /* end of gnunet-gns.c */