gnunet-gns-import.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file gnunet-gns.c
  18. * @brief binary version of gnunet-gns-import.sh
  19. * (for OSes that have no POSIX shell).
  20. * @author LRN
  21. */
  22. #include "platform.h"
  23. #include <gnunet_util_lib.h>
  24. #include <gnunet_gnsrecord_lib.h>
  25. #include <gnunet_identity_service.h>
  26. #include <gnunet_namestore_service.h>
  27. /**
  28. * Configuration we are using.
  29. */
  30. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  31. /**
  32. * Handle to IDENTITY service.
  33. */
  34. static struct GNUNET_IDENTITY_Handle *sh;
  35. /**
  36. * Zone iterator for master zone
  37. */
  38. struct GNUNET_NAMESTORE_ZoneIterator *list_it;
  39. /**
  40. * Handle to the namestore.
  41. */
  42. static struct GNUNET_NAMESTORE_Handle *ns;
  43. /**
  44. * String version of PKEY for master-zone.
  45. */
  46. static char *master_zone_pkey;
  47. /**
  48. * Binary version of PKEY for master-zone.
  49. */
  50. static struct GNUNET_CRYPTO_EcdsaPrivateKey master_pk;
  51. /**
  52. * String version of PKEY for private-zone.
  53. */
  54. static char *private_zone_pkey;
  55. /**
  56. * String version of PKEY for pin-zone.
  57. */
  58. static char *pin_zone_pkey =
  59. "72QC35CO20UJN1E91KPJFNT9TG4CLKAPB4VK9S3Q758S9MLBRKOG";
  60. /**
  61. * Set to GNUNET_YES if private record was found;
  62. */
  63. static int found_private_rec = GNUNET_NO;
  64. /**
  65. * Set to GNUNET_YES if pin record was found;
  66. */
  67. static int found_pin_rec = GNUNET_NO;
  68. /**
  69. * Exit code.
  70. */
  71. static int ret;
  72. static int
  73. run_process_and_wait (enum GNUNET_OS_InheritStdioFlags std_inheritance,
  74. struct GNUNET_DISK_PipeHandle *pipe_stdin,
  75. struct GNUNET_DISK_PipeHandle *pipe_stdout,
  76. enum GNUNET_OS_ProcessStatusType *st,
  77. unsigned long *code,
  78. const char *filename, ...)
  79. {
  80. static struct GNUNET_OS_Process *p;
  81. int arglen;
  82. char *arg;
  83. char *args;
  84. char *argp;
  85. va_list ap, apc1, apc2;
  86. va_start (ap, filename);
  87. va_copy (apc1, ap);
  88. va_copy (apc2, ap);
  89. arglen = 0;
  90. while (NULL != (arg = va_arg (apc1, char *)))
  91. arglen += strlen (arg) + 1;
  92. va_end (apc1);
  93. args = argp = GNUNET_malloc (arglen);
  94. while (NULL != (arg = va_arg (apc2, char *)))
  95. {
  96. strcpy (argp, arg);
  97. argp += strlen (arg);
  98. *argp = ' ';
  99. argp += 1;
  100. }
  101. va_end (apc2);
  102. if (arglen > 0)
  103. argp[-1] = '\0';
  104. p = GNUNET_OS_start_process_va (std_inheritance,
  105. pipe_stdin,
  106. pipe_stdout,
  107. NULL,
  108. filename, ap);
  109. va_end (ap);
  110. if (NULL == p)
  111. {
  112. ret = 3;
  113. fprintf (stderr, "Failed to run `%s'\n", args);
  114. GNUNET_free (args);
  115. return 1;
  116. }
  117. if (GNUNET_OK != GNUNET_OS_process_wait (p))
  118. {
  119. ret = 4;
  120. fprintf (stderr, "Failed to wait for `%s'\n", args);
  121. GNUNET_free (args);
  122. return 1;
  123. }
  124. switch (GNUNET_OS_process_status (p, st, code))
  125. {
  126. case GNUNET_OK:
  127. break;
  128. case GNUNET_NO:
  129. ret = 5;
  130. fprintf (stderr, "`%s' is still running\n", args);
  131. GNUNET_free (args);
  132. return 1;
  133. default:
  134. case GNUNET_SYSERR:
  135. ret = 6;
  136. fprintf (stderr, "Failed to check the status of `%s'\n", args);
  137. GNUNET_free (args);
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. static void
  143. check_pkey (unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd,
  144. char *pk, int *found_rec)
  145. {
  146. int i;
  147. struct GNUNET_IDENTITY_PublicKey pubkey;
  148. for (i = 0; i < rd_len; i++)
  149. {
  150. char *s;
  151. if (sizeof (uint32_t) > rd[i].data_size)
  152. continue;
  153. if (GNUNET_OK != GNUNET_GNSRECORD_identity_from_data (rd[i].data,
  154. rd[i].data_size,
  155. rd[i].record_type,
  156. &pubkey))
  157. continue;
  158. s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  159. rd[i].data,
  160. rd[i].data_size);
  161. if (NULL == s)
  162. continue;
  163. if (0 == strcmp (s, pk))
  164. *found_rec = GNUNET_YES;
  165. GNUNET_free (s);
  166. }
  167. }
  168. /**
  169. * Process a record that was stored in the namestore.
  170. *
  171. * @param cls closure
  172. * @param zone_key private key of the zone
  173. * @param rname name that is being mapped (at most 255 characters long)
  174. * @param rd_len number of entries in @a rd array
  175. * @param rd array of records with data to store
  176. */
  177. static void
  178. zone_iterator (void *cls,
  179. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
  180. const char *rname, unsigned int rd_len,
  181. const struct GNUNET_GNSRECORD_Data *rd)
  182. {
  183. if (NULL != rname)
  184. {
  185. if (0 == strcmp (rname, "private"))
  186. check_pkey (rd_len, rd, private_zone_pkey, &found_private_rec);
  187. else if (0 == strcmp (rname, "pin"))
  188. check_pkey (rd_len, rd, pin_zone_pkey, &found_pin_rec);
  189. }
  190. GNUNET_NAMESTORE_zone_iterator_next (list_it);
  191. }
  192. static void
  193. zone_iteration_error (void *cls)
  194. {
  195. enum GNUNET_OS_ProcessStatusType st;
  196. unsigned long code;
  197. if (! found_private_rec)
  198. {
  199. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  200. NULL, NULL, &st, &code,
  201. "gnunet-namestore",
  202. "gnunet-namestore", "-z", "master-zone",
  203. "-a", "-e", "never", "-n", "private", "-p",
  204. "-t", "PKEY", "-V",
  205. private_zone_pkey, NULL))
  206. {
  207. ret = 8;
  208. return;
  209. }
  210. }
  211. if (! found_pin_rec)
  212. {
  213. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  214. NULL, NULL, &st, &code,
  215. "gnunet-namestore",
  216. "gnunet-namestore", "-z", "master-zone",
  217. "-a", "-e", "never", "-n", "pin", "-p", "-t",
  218. "PKEY", "-V", pin_zone_pkey,
  219. NULL))
  220. {
  221. ret = 10;
  222. return;
  223. }
  224. }
  225. list_it = NULL;
  226. GNUNET_SCHEDULER_shutdown ();
  227. }
  228. static void
  229. zone_iteration_finished (void *cls)
  230. {
  231. }
  232. /**
  233. * Get master-zone and private-zone keys.
  234. *
  235. * This function is initially called for all egos and then again
  236. * whenever a ego's identifier changes or if it is deleted. At the
  237. * end of the initial pass over all egos, the function is once called
  238. * with 'NULL' for 'ego'. That does NOT mean that the callback won't
  239. * be invoked in the future or that there was an error.
  240. *
  241. * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get', this
  242. * function is only called ONCE, and 'NULL' being passed in 'ego' does
  243. * indicate an error (for example because name is taken or no default value is
  244. * known). If 'ego' is non-NULL and if '*ctx' is set in those callbacks, the
  245. * value WILL be passed to a subsequent call to the identity callback of
  246. * 'GNUNET_IDENTITY_connect' (if that one was not NULL).
  247. *
  248. * When an identity is renamed, this function is called with the
  249. * (known) ego but the NEW identifier.
  250. *
  251. * When an identity is deleted, this function is called with the
  252. * (known) ego and "NULL" for the 'identifier'. In this case,
  253. * the 'ego' is henceforth invalid (and the 'ctx' should also be
  254. * cleaned up).
  255. *
  256. * @param cls closure
  257. * @param ego ego handle
  258. * @param ctx context for application to store data for this ego
  259. * (during the lifetime of this process, initially NULL)
  260. * @param identifier identifier assigned by the user for this ego,
  261. * NULL if the user just deleted the ego and it
  262. * must thus no longer be used
  263. */
  264. static void
  265. get_ego (void *cls,
  266. struct GNUNET_IDENTITY_Ego *ego,
  267. void **ctx,
  268. const char *identifier)
  269. {
  270. static struct GNUNET_CRYPTO_EcdsaPublicKey pk;
  271. if (NULL == ego)
  272. {
  273. if ((NULL == master_zone_pkey) ||
  274. (NULL == private_zone_pkey) )
  275. {
  276. ret = 11;
  277. GNUNET_SCHEDULER_shutdown ();
  278. return;
  279. }
  280. list_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
  281. &master_pk,
  282. &zone_iteration_error,
  283. NULL, &zone_iterator, NULL,
  284. &zone_iteration_finished,
  285. NULL);
  286. if (NULL == list_it)
  287. {
  288. ret = 12;
  289. GNUNET_SCHEDULER_shutdown ();
  290. }
  291. return;
  292. }
  293. GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
  294. if (NULL != identifier)
  295. {
  296. if ((NULL == master_zone_pkey) && (0 == strcmp ("master-zone",
  297. identifier)) )
  298. {
  299. master_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
  300. master_pk = *GNUNET_IDENTITY_ego_get_private_key (ego);
  301. }
  302. else if ((NULL == private_zone_pkey) && (0 == strcmp ("private-zone",
  303. identifier)) )
  304. private_zone_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
  305. }
  306. }
  307. /**
  308. * Task run on shutdown.
  309. *
  310. * @param cls NULL
  311. */
  312. static void
  313. shutdown_task (void *cls)
  314. {
  315. GNUNET_free (master_zone_pkey);
  316. master_zone_pkey = NULL;
  317. GNUNET_free (private_zone_pkey);
  318. private_zone_pkey = NULL;
  319. if (NULL != list_it)
  320. {
  321. GNUNET_NAMESTORE_zone_iteration_stop (list_it);
  322. list_it = NULL;
  323. }
  324. if (NULL != ns)
  325. {
  326. GNUNET_NAMESTORE_disconnect (ns);
  327. ns = NULL;
  328. }
  329. if (NULL != sh)
  330. {
  331. GNUNET_IDENTITY_disconnect (sh);
  332. sh = NULL;
  333. }
  334. }
  335. /**
  336. * Main function that will be run.
  337. *
  338. * @param cls closure
  339. * @param args remaining command-line arguments
  340. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  341. * @param c configuration
  342. */
  343. static void
  344. run (void *cls, char *const *args, const char *cfgfile,
  345. const struct GNUNET_CONFIGURATION_Handle *c)
  346. {
  347. enum GNUNET_OS_ProcessStatusType st;
  348. unsigned long code;
  349. cfg = c;
  350. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_NONE,
  351. NULL, NULL, &st, &code,
  352. "gnunet-arm",
  353. "gnunet-arm", "-I", NULL))
  354. {
  355. if (7 == ret)
  356. fprintf (stderr,
  357. "GNUnet is not running, please start GNUnet before running import\n");
  358. return;
  359. }
  360. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  361. NULL, NULL, &st, &code,
  362. "gnunet-identity",
  363. "gnunet-identity", "-C", "master-zone", NULL))
  364. return;
  365. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  366. NULL, NULL, &st, &code,
  367. "gnunet-identity",
  368. "gnunet-identity", "-C", "private-zone", NULL))
  369. return;
  370. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  371. NULL, NULL, &st, &code,
  372. "gnunet-identity",
  373. "gnunet-identity", "-C", "sks-zone", NULL))
  374. return;
  375. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  376. NULL, NULL, &st, &code,
  377. "gnunet-identity",
  378. "gnunet-identity", "-e", "master-zone", "-s",
  379. "gns-master", NULL))
  380. return;
  381. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  382. NULL, NULL, &st, &code,
  383. "gnunet-identity",
  384. "gnunet-identity", "-e", "master-zone", "-s",
  385. "namestore", NULL))
  386. return;
  387. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  388. NULL, NULL, &st, &code,
  389. "gnunet-identity",
  390. "gnunet-identity", "-e", "master-zone", "-s",
  391. "gns-proxy", NULL))
  392. return;
  393. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  394. NULL, NULL, &st, &code,
  395. "gnunet-identity",
  396. "gnunet-identity", "-e", "master-zone", "-s",
  397. "gns-intercept", NULL))
  398. return;
  399. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  400. NULL, NULL, &st, &code,
  401. "gnunet-identity",
  402. "gnunet-identity", "-e", "private-zone", "-s",
  403. "gns-private", NULL))
  404. return;
  405. if (0 != run_process_and_wait (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  406. NULL, NULL, &st, &code,
  407. "gnunet-identity",
  408. "gnunet-identity", "-e", "sks-zone", "-s",
  409. "fs-sks", NULL))
  410. return;
  411. ns = GNUNET_NAMESTORE_connect (cfg);
  412. sh = GNUNET_IDENTITY_connect (cfg, &get_ego, NULL);
  413. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  414. }
  415. /**
  416. * The main function for gnunet-gns.
  417. *
  418. * @param argc number of arguments from the command line
  419. * @param argv command line arguments
  420. * @return 0 ok, 1 on error
  421. */
  422. int
  423. main (int argc, char *const *argv)
  424. {
  425. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  426. GNUNET_GETOPT_OPTION_END
  427. };
  428. int r;
  429. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  430. return 2;
  431. GNUNET_log_setup ("gnunet-gns-import", "WARNING", NULL);
  432. ret = 0;
  433. r = GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-import",
  434. _ (
  435. "This program will import some GNS authorities into your GNS namestore."),
  436. options,
  437. &run, NULL);
  438. GNUNET_free_nz ((void *) argv);
  439. return GNUNET_OK == r ? ret : 1;
  440. }
  441. /* end of gnunet-gns-import.c */