gnunet-gns.c 12 KB

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