gnunet-identity.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013, 2018, 2019 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 identity/gnunet-identity.c
  18. * @brief IDENTITY management command line tool
  19. * @author Christian Grothoff
  20. *
  21. * Todo:
  22. * - add options to get default egos
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_identity_service.h"
  27. /**
  28. * Return value from main on timeout.
  29. */
  30. #define TIMEOUT_STATUS_CODE 40
  31. /**
  32. * Handle to IDENTITY service.
  33. */
  34. static struct GNUNET_IDENTITY_Handle *sh;
  35. /**
  36. * Was "list" specified?
  37. */
  38. static int list;
  39. /**
  40. * Was "monitor" specified?
  41. */
  42. static int monitor;
  43. /**
  44. * Was "private" specified?
  45. */
  46. static int private_keys;
  47. /**
  48. * Was "verbose" specified?
  49. */
  50. static unsigned int verbose;
  51. /**
  52. * Was "quiet" specified?
  53. */
  54. static int quiet;
  55. /**
  56. * Was "eddsa" specified?
  57. */
  58. static int type_eddsa;
  59. /**
  60. * -C option
  61. */
  62. static char *create_ego;
  63. /**
  64. * -D option
  65. */
  66. static char *delete_ego;
  67. /**
  68. * -P option
  69. */
  70. static char *privkey_ego;
  71. /**
  72. * -s option.
  73. */
  74. static char *set_ego;
  75. /**
  76. * -S option.
  77. */
  78. static char *set_subsystem;
  79. /**
  80. * Operation handle for set operation.
  81. */
  82. static struct GNUNET_IDENTITY_Operation *set_op;
  83. /**
  84. * Handle for create operation.
  85. */
  86. static struct GNUNET_IDENTITY_Operation *create_op;
  87. /**
  88. * Handle for delete operation.
  89. */
  90. static struct GNUNET_IDENTITY_Operation *delete_op;
  91. /**
  92. * Private key from command line option, or NULL.
  93. */
  94. struct GNUNET_IDENTITY_PrivateKey pk;
  95. /**
  96. * Value to return from #main().
  97. */
  98. static int global_ret;
  99. /**
  100. * Task run on shutdown.
  101. *
  102. * @param cls NULL
  103. */
  104. static void
  105. shutdown_task (void *cls)
  106. {
  107. if (NULL != set_op)
  108. {
  109. GNUNET_IDENTITY_cancel (set_op);
  110. set_op = NULL;
  111. }
  112. if (NULL != create_op)
  113. {
  114. GNUNET_IDENTITY_cancel (create_op);
  115. create_op = NULL;
  116. }
  117. if (NULL != delete_op)
  118. {
  119. GNUNET_IDENTITY_cancel (delete_op);
  120. delete_op = NULL;
  121. }
  122. if (NULL != set_ego)
  123. {
  124. GNUNET_free (set_ego);
  125. set_ego = NULL;
  126. }
  127. GNUNET_IDENTITY_disconnect (sh);
  128. sh = NULL;
  129. }
  130. /**
  131. * Test if we are finished yet.
  132. */
  133. static void
  134. test_finished (void)
  135. {
  136. if ( (NULL == create_op) &&
  137. (NULL == delete_op) &&
  138. (NULL == set_op) &&
  139. (NULL == set_subsystem) &&
  140. (! list) &&
  141. (! monitor))
  142. {
  143. if (TIMEOUT_STATUS_CODE == global_ret)
  144. global_ret = 0;
  145. GNUNET_SCHEDULER_shutdown ();
  146. }
  147. }
  148. /**
  149. * Deletion operation finished.
  150. *
  151. * @param cls pointer to operation handle
  152. * @param emsg NULL on success, otherwise an error message
  153. */
  154. static void
  155. delete_finished (void *cls,
  156. const char *emsg)
  157. {
  158. struct GNUNET_IDENTITY_Operation **op = cls;
  159. *op = NULL;
  160. if (NULL != emsg)
  161. fprintf (stderr, "%s\n", gettext (emsg));
  162. test_finished ();
  163. }
  164. /**
  165. * Creation operation finished.
  166. *
  167. * @param cls pointer to operation handle
  168. * @param pk private key of the ego, or NULL on error
  169. * @param emsg error message, NULL on success
  170. */
  171. static void
  172. create_finished (void *cls,
  173. const struct GNUNET_IDENTITY_PrivateKey *pk,
  174. const char *emsg)
  175. {
  176. struct GNUNET_IDENTITY_Operation **op = cls;
  177. *op = NULL;
  178. if (NULL == pk)
  179. {
  180. fprintf (stderr,
  181. _ ("Failed to create ego: %s\n"),
  182. emsg);
  183. global_ret = 1;
  184. }
  185. else if (verbose)
  186. {
  187. struct GNUNET_IDENTITY_PublicKey pub;
  188. char *pubs;
  189. GNUNET_IDENTITY_key_get_public (pk, &pub);
  190. pubs = GNUNET_IDENTITY_public_key_to_string (&pub);
  191. if (private_keys)
  192. {
  193. char *privs;
  194. privs = GNUNET_IDENTITY_private_key_to_string (pk);
  195. fprintf (stdout, "%s - %s\n", pubs, privs);
  196. GNUNET_free (privs);
  197. }
  198. else
  199. {
  200. fprintf (stdout, "%s\n", pubs);
  201. }
  202. GNUNET_free (pubs);
  203. }
  204. test_finished ();
  205. }
  206. /**
  207. * Function called by #GNUNET_IDENTITY_set up on completion.
  208. *
  209. * @param cls NULL
  210. * @param emsg error message (NULL on success)
  211. */
  212. static void
  213. set_done (void *cls, const char *emsg)
  214. {
  215. set_op = NULL;
  216. if (NULL != emsg)
  217. {
  218. fprintf (stderr, _ ("Failed to set default ego: %s\n"), emsg);
  219. global_ret = 1;
  220. }
  221. test_finished ();
  222. }
  223. /**
  224. * If listing is enabled, prints information about the egos.
  225. *
  226. * This function is initially called for all egos and then again
  227. * whenever a ego's identifier changes or if it is deleted. At the
  228. * end of the initial pass over all egos, the function is once called
  229. * with 'NULL' for 'ego'. That does NOT mean that the callback won't
  230. * be invoked in the future or that there was an error.
  231. *
  232. * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
  233. * this function is only called ONCE, and 'NULL' being passed in
  234. * 'ego' does indicate an error (i.e. name is taken or no default
  235. * value is known). If 'ego' is non-NULL and if '*ctx'
  236. * is set in those callbacks, the value WILL be passed to a subsequent
  237. * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
  238. * that one was not NULL).
  239. *
  240. * When an identity is renamed, this function is called with the
  241. * (known) ego but the NEW identifier.
  242. *
  243. * When an identity is deleted, this function is called with the
  244. * (known) ego and "NULL" for the 'identifier'. In this case,
  245. * the 'ego' is henceforth invalid (and the 'ctx' should also be
  246. * cleaned up).
  247. *
  248. * @param cls closure
  249. * @param ego ego handle
  250. * @param ctx context for application to store data for this ego
  251. * (during the lifetime of this process, initially NULL)
  252. * @param identifier identifier assigned by the user for this ego,
  253. * NULL if the user just deleted the ego and it
  254. * must thus no longer be used
  255. */
  256. static void
  257. print_ego (void *cls,
  258. struct GNUNET_IDENTITY_Ego *ego,
  259. void **ctx,
  260. const char *identifier)
  261. {
  262. struct GNUNET_IDENTITY_PublicKey pk;
  263. char *s;
  264. char *privs;
  265. if ( (NULL != set_ego) &&
  266. (NULL != set_subsystem) &&
  267. (NULL != ego) &&
  268. (NULL != identifier) &&
  269. (0 == strcmp (identifier, set_ego)))
  270. {
  271. set_op = GNUNET_IDENTITY_set (sh,
  272. set_subsystem,
  273. ego,
  274. &set_done,
  275. NULL);
  276. GNUNET_free (set_subsystem);
  277. set_subsystem = NULL;
  278. GNUNET_free (set_ego);
  279. set_ego = NULL;
  280. }
  281. if ( (NULL == ego) &&
  282. (NULL != set_ego) &&
  283. (NULL != set_subsystem) )
  284. {
  285. fprintf (stderr,
  286. "Could not set ego to `%s' for subsystem `%s', ego not known\n",
  287. set_ego,
  288. set_subsystem);
  289. GNUNET_free (set_subsystem);
  290. set_subsystem = NULL;
  291. GNUNET_free (set_ego);
  292. set_ego = NULL;
  293. }
  294. if ((NULL == ego) && (! monitor))
  295. {
  296. list = 0;
  297. test_finished ();
  298. return;
  299. }
  300. if (! (list | monitor))
  301. return;
  302. if ( (NULL == ego) ||
  303. (NULL == identifier) )
  304. return;
  305. if ( (NULL != set_ego) &&
  306. (0 != strcmp (identifier,
  307. set_ego)) )
  308. return;
  309. GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
  310. s = GNUNET_IDENTITY_public_key_to_string (&pk);
  311. privs = GNUNET_IDENTITY_private_key_to_string (
  312. GNUNET_IDENTITY_ego_get_private_key (ego));
  313. if ((monitor) || (NULL != identifier))
  314. {
  315. if (quiet)
  316. {
  317. if (private_keys)
  318. fprintf (stdout, "%s - %s\n", s, privs);
  319. else
  320. fprintf (stdout, "%s\n", s);
  321. }
  322. else
  323. {
  324. if (private_keys)
  325. fprintf (stdout, "%s - %s - %s - %s\n",
  326. identifier, s, privs,
  327. (ntohl (pk.type) == GNUNET_IDENTITY_TYPE_ECDSA) ?
  328. "ECDSA" : "EdDSA");
  329. else
  330. fprintf (stdout, "%s - %s - %s\n",
  331. identifier, s,
  332. (ntohl (pk.type) == GNUNET_IDENTITY_TYPE_ECDSA) ?
  333. "ECDSA" : "EdDSA");
  334. }
  335. }
  336. GNUNET_free (privs);
  337. GNUNET_free (s);
  338. }
  339. /**
  340. * Main function that will be run by the scheduler.
  341. *
  342. * @param cls closure
  343. * @param args remaining command-line arguments
  344. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  345. * @param cfg configuration
  346. */
  347. static void
  348. run (void *cls,
  349. char *const *args,
  350. const char *cfgfile,
  351. const struct GNUNET_CONFIGURATION_Handle *cfg)
  352. {
  353. if ((NULL != set_subsystem) && (NULL == set_ego))
  354. {
  355. fprintf (stderr, "Option -s requires option -e to be specified as well.\n");
  356. return;
  357. }
  358. sh = GNUNET_IDENTITY_connect (cfg,
  359. (monitor | list) ||
  360. (NULL != set_ego) ||
  361. (NULL != set_subsystem)
  362. ? &print_ego
  363. : NULL,
  364. NULL);
  365. if (NULL != delete_ego)
  366. delete_op =
  367. GNUNET_IDENTITY_delete (sh,
  368. delete_ego,
  369. &delete_finished,
  370. &delete_op);
  371. if (NULL != create_ego)
  372. {
  373. if (NULL != privkey_ego)
  374. {
  375. GNUNET_STRINGS_string_to_data (privkey_ego,
  376. strlen (privkey_ego),
  377. &pk,
  378. sizeof(struct
  379. GNUNET_IDENTITY_PrivateKey));
  380. create_op =
  381. GNUNET_IDENTITY_create (sh,
  382. create_ego,
  383. &pk,
  384. 0, // Ignored
  385. &create_finished,
  386. &create_op);
  387. }
  388. else
  389. create_op =
  390. GNUNET_IDENTITY_create (sh,
  391. create_ego,
  392. NULL,
  393. (type_eddsa) ?
  394. GNUNET_IDENTITY_TYPE_EDDSA :
  395. GNUNET_IDENTITY_TYPE_ECDSA,
  396. &create_finished,
  397. &create_op);
  398. }
  399. GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
  400. NULL);
  401. test_finished ();
  402. }
  403. /**
  404. * The main function.
  405. *
  406. * @param argc number of arguments from the command line
  407. * @param argv command line arguments
  408. * @return 0 ok, 1 on error
  409. */
  410. int
  411. main (int argc, char *const *argv)
  412. {
  413. struct GNUNET_GETOPT_CommandLineOption options[] = {
  414. GNUNET_GETOPT_option_string ('C',
  415. "create",
  416. "NAME",
  417. gettext_noop ("create ego NAME"),
  418. &create_ego),
  419. GNUNET_GETOPT_option_string ('D',
  420. "delete",
  421. "NAME",
  422. gettext_noop ("delete ego NAME "),
  423. &delete_ego),
  424. GNUNET_GETOPT_option_string ('P',
  425. "privkey",
  426. "PRIVATE_KEY",
  427. gettext_noop (
  428. "set the private key for the identity to PRIVATE_KEY (use together with -C)"),
  429. &privkey_ego),
  430. GNUNET_GETOPT_option_flag ('X',
  431. "eddsa",
  432. gettext_noop (
  433. "generate an EdDSA identity. (use together with -C) EXPERIMENTAL"),
  434. &type_eddsa),
  435. GNUNET_GETOPT_option_flag ('d',
  436. "display",
  437. gettext_noop ("display all egos"),
  438. &list),
  439. GNUNET_GETOPT_option_flag ('q',
  440. "quiet",
  441. gettext_noop ("reduce output"),
  442. &quiet),
  443. GNUNET_GETOPT_option_string (
  444. 'e',
  445. "ego",
  446. "NAME",
  447. gettext_noop (
  448. "set default identity to NAME for a subsystem SUBSYSTEM (use together with -s) or restrict results to NAME (use together with -d)"),
  449. &set_ego),
  450. GNUNET_GETOPT_option_flag ('m',
  451. "monitor",
  452. gettext_noop ("run in monitor mode egos"),
  453. &monitor),
  454. GNUNET_GETOPT_option_flag ('p',
  455. "private-keys",
  456. gettext_noop ("display private keys as well"),
  457. &private_keys),
  458. GNUNET_GETOPT_option_string (
  459. 's',
  460. "set",
  461. "SUBSYSTEM",
  462. gettext_noop (
  463. "set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
  464. &set_subsystem),
  465. GNUNET_GETOPT_option_verbose (&verbose),
  466. GNUNET_GETOPT_OPTION_END
  467. };
  468. int res;
  469. if (GNUNET_OK !=
  470. GNUNET_STRINGS_get_utf8_args (argc, argv,
  471. &argc, &argv))
  472. return 4;
  473. global_ret = TIMEOUT_STATUS_CODE; /* timeout */
  474. res = GNUNET_PROGRAM_run (argc,
  475. argv,
  476. "gnunet-identity",
  477. gettext_noop ("Maintain egos"),
  478. options,
  479. &run,
  480. NULL);
  481. GNUNET_free_nz ((void *) argv);
  482. if (GNUNET_OK != res)
  483. return 3;
  484. return global_ret;
  485. }
  486. /* end of gnunet-identity.c */