gnunet-identity.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. * -C option
  57. */
  58. static char *create_ego;
  59. /**
  60. * -D option
  61. */
  62. static char *delete_ego;
  63. /**
  64. * -s option.
  65. */
  66. static char *set_ego;
  67. /**
  68. * -S option.
  69. */
  70. static char *set_subsystem;
  71. /**
  72. * Operation handle for set operation.
  73. */
  74. static struct GNUNET_IDENTITY_Operation *set_op;
  75. /**
  76. * Handle for create operation.
  77. */
  78. static struct GNUNET_IDENTITY_Operation *create_op;
  79. /**
  80. * Handle for delete operation.
  81. */
  82. static struct GNUNET_IDENTITY_Operation *delete_op;
  83. /**
  84. * Value to return from #main().
  85. */
  86. static int global_ret;
  87. /**
  88. * Task run on shutdown.
  89. *
  90. * @param cls NULL
  91. */
  92. static void
  93. shutdown_task (void *cls)
  94. {
  95. if (NULL != set_op)
  96. {
  97. GNUNET_IDENTITY_cancel (set_op);
  98. set_op = NULL;
  99. }
  100. if (NULL != create_op)
  101. {
  102. GNUNET_IDENTITY_cancel (create_op);
  103. create_op = NULL;
  104. }
  105. if (NULL != delete_op)
  106. {
  107. GNUNET_IDENTITY_cancel (delete_op);
  108. delete_op = NULL;
  109. }
  110. if (NULL != set_ego)
  111. {
  112. GNUNET_free (set_ego);
  113. set_ego = NULL;
  114. }
  115. GNUNET_IDENTITY_disconnect (sh);
  116. sh = NULL;
  117. }
  118. /**
  119. * Test if we are finished yet.
  120. */
  121. static void
  122. test_finished ()
  123. {
  124. if ((NULL == create_op) && (NULL == delete_op) && (NULL == set_op) &&
  125. (NULL == set_subsystem) && (! list) && (! monitor))
  126. {
  127. if (TIMEOUT_STATUS_CODE == global_ret)
  128. global_ret = 0;
  129. GNUNET_SCHEDULER_shutdown ();
  130. }
  131. }
  132. /**
  133. * Deletion operation finished.
  134. *
  135. * @param cls pointer to operation handle
  136. * @param emsg NULL on success, otherwise an error message
  137. */
  138. static void
  139. delete_finished (void *cls, const char *emsg)
  140. {
  141. struct GNUNET_IDENTITY_Operation **op = cls;
  142. *op = NULL;
  143. if (NULL != emsg)
  144. fprintf (stderr, "%s\n", gettext (emsg));
  145. test_finished ();
  146. }
  147. /**
  148. * Creation operation finished.
  149. *
  150. * @param cls pointer to operation handle
  151. * @param pk private key of the ego, or NULL on error
  152. * @param emsg error message, NULL on success
  153. */
  154. static void
  155. create_finished (void *cls,
  156. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
  157. const char *emsg)
  158. {
  159. struct GNUNET_IDENTITY_Operation **op = cls;
  160. *op = NULL;
  161. if (NULL == pk)
  162. {
  163. fprintf (stderr, _ ("Failed to create ego: %s\n"), emsg);
  164. global_ret = 1;
  165. }
  166. else if (verbose)
  167. {
  168. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  169. char *pubs;
  170. GNUNET_CRYPTO_ecdsa_key_get_public (pk, &pub);
  171. pubs = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pub);
  172. if (private_keys)
  173. {
  174. char *privs;
  175. privs = GNUNET_CRYPTO_ecdsa_private_key_to_string (pk);
  176. fprintf (stdout, "%s - %s\n", pubs, privs);
  177. GNUNET_free (privs);
  178. }
  179. else
  180. {
  181. fprintf (stdout, "%s\n", pubs);
  182. }
  183. GNUNET_free (pubs);
  184. }
  185. test_finished ();
  186. }
  187. /**
  188. * Function called by #GNUNET_IDENTITY_set up on completion.
  189. *
  190. * @param cls NULL
  191. * @param emsg error message (NULL on success)
  192. */
  193. static void
  194. set_done (void *cls, const char *emsg)
  195. {
  196. set_op = NULL;
  197. if (NULL != emsg)
  198. {
  199. fprintf (stderr, _ ("Failed to set default ego: %s\n"), emsg);
  200. global_ret = 1;
  201. }
  202. test_finished ();
  203. }
  204. /**
  205. * If listing is enabled, prints information about the egos.
  206. *
  207. * This function is initially called for all egos and then again
  208. * whenever a ego's identifier changes or if it is deleted. At the
  209. * end of the initial pass over all egos, the function is once called
  210. * with 'NULL' for 'ego'. That does NOT mean that the callback won't
  211. * be invoked in the future or that there was an error.
  212. *
  213. * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
  214. * this function is only called ONCE, and 'NULL' being passed in
  215. * 'ego' does indicate an error (i.e. name is taken or no default
  216. * value is known). If 'ego' is non-NULL and if '*ctx'
  217. * is set in those callbacks, the value WILL be passed to a subsequent
  218. * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
  219. * that one was not NULL).
  220. *
  221. * When an identity is renamed, this function is called with the
  222. * (known) ego but the NEW identifier.
  223. *
  224. * When an identity is deleted, this function is called with the
  225. * (known) ego and "NULL" for the 'identifier'. In this case,
  226. * the 'ego' is henceforth invalid (and the 'ctx' should also be
  227. * cleaned up).
  228. *
  229. * @param cls closure
  230. * @param ego ego handle
  231. * @param ctx context for application to store data for this ego
  232. * (during the lifetime of this process, initially NULL)
  233. * @param identifier identifier assigned by the user for this ego,
  234. * NULL if the user just deleted the ego and it
  235. * must thus no longer be used
  236. */
  237. static void
  238. print_ego (void *cls,
  239. struct GNUNET_IDENTITY_Ego *ego,
  240. void **ctx,
  241. const char *identifier)
  242. {
  243. struct GNUNET_CRYPTO_EcdsaPublicKey pk;
  244. char *s;
  245. char *privs;
  246. if ((NULL != set_ego) && (NULL != set_subsystem) && (NULL != ego) &&
  247. (NULL != identifier) && (0 == strcmp (identifier, set_ego)))
  248. {
  249. set_op = GNUNET_IDENTITY_set (sh, set_subsystem, ego, &set_done, NULL);
  250. GNUNET_free (set_subsystem);
  251. set_subsystem = NULL;
  252. GNUNET_free (set_ego);
  253. set_ego = NULL;
  254. }
  255. if ((NULL == ego) && (NULL != set_ego) && (NULL != set_subsystem))
  256. {
  257. fprintf (stderr,
  258. "Could not set ego to `%s' for subsystem `%s', ego not known\n",
  259. set_ego,
  260. set_subsystem);
  261. GNUNET_free (set_subsystem);
  262. set_subsystem = NULL;
  263. GNUNET_free (set_ego);
  264. set_ego = NULL;
  265. }
  266. if ((NULL == ego) && (! monitor))
  267. {
  268. list = 0;
  269. test_finished ();
  270. return;
  271. }
  272. if (! (list | monitor))
  273. return;
  274. if ((NULL == ego) || (NULL == identifier))
  275. return;
  276. if ((NULL != set_ego) && (0 != strcmp (identifier, set_ego)))
  277. return;
  278. GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
  279. s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
  280. privs = GNUNET_CRYPTO_ecdsa_private_key_to_string (
  281. GNUNET_IDENTITY_ego_get_private_key (ego));
  282. if ((monitor) || (NULL != identifier))
  283. {
  284. if (quiet)
  285. {
  286. if (private_keys)
  287. fprintf (stdout, "%s - %s\n", s, privs);
  288. else
  289. fprintf (stdout, "%s\n", s);
  290. }
  291. else
  292. {
  293. if (private_keys)
  294. fprintf (stdout, "%s - %s - %s\n", identifier, s, privs);
  295. else
  296. fprintf (stdout, "%s - %s\n", identifier, s);
  297. }
  298. }
  299. GNUNET_free (privs);
  300. GNUNET_free (s);
  301. }
  302. /**
  303. * Main function that will be run by the scheduler.
  304. *
  305. * @param cls closure
  306. * @param args remaining command-line arguments
  307. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  308. * @param cfg configuration
  309. */
  310. static void
  311. run (void *cls,
  312. char *const *args,
  313. const char *cfgfile,
  314. const struct GNUNET_CONFIGURATION_Handle *cfg)
  315. {
  316. if ((NULL != set_subsystem) && (NULL == set_ego))
  317. {
  318. fprintf (stderr, "Option -s requires option -e to be specified as well.\n");
  319. return;
  320. }
  321. sh = GNUNET_IDENTITY_connect (cfg,
  322. (monitor | list) || (NULL != set_ego) ||
  323. (NULL != set_subsystem)
  324. ? &print_ego
  325. : NULL,
  326. NULL);
  327. if (NULL != delete_ego)
  328. delete_op =
  329. GNUNET_IDENTITY_delete (sh, delete_ego, &delete_finished, &delete_op);
  330. if (NULL != create_ego)
  331. create_op =
  332. GNUNET_IDENTITY_create (sh, create_ego, &create_finished, &create_op);
  333. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  334. test_finished ();
  335. }
  336. /**
  337. * The main function.
  338. *
  339. * @param argc number of arguments from the command line
  340. * @param argv command line arguments
  341. * @return 0 ok, 1 on error
  342. */
  343. int
  344. main (int argc, char *const *argv)
  345. {
  346. struct GNUNET_GETOPT_CommandLineOption options[] =
  347. {GNUNET_GETOPT_option_string ('C',
  348. "create",
  349. "NAME",
  350. gettext_noop ("create ego NAME"),
  351. &create_ego),
  352. GNUNET_GETOPT_option_string ('D',
  353. "delete",
  354. "NAME",
  355. gettext_noop ("delete ego NAME "),
  356. &delete_ego),
  357. GNUNET_GETOPT_option_flag ('d',
  358. "display",
  359. gettext_noop ("display all egos"),
  360. &list),
  361. GNUNET_GETOPT_option_flag ('q',
  362. "quiet",
  363. gettext_noop ("reduce output"),
  364. &quiet),
  365. GNUNET_GETOPT_option_string (
  366. 'e',
  367. "ego",
  368. "NAME",
  369. gettext_noop (
  370. "set default identity to NAME for a subsystem SUBSYSTEM (use together with -s) or restrict results to NAME (use together with -d)"),
  371. &set_ego),
  372. GNUNET_GETOPT_option_flag ('m',
  373. "monitor",
  374. gettext_noop ("run in monitor mode egos"),
  375. &monitor),
  376. GNUNET_GETOPT_option_flag ('p',
  377. "private-keys",
  378. gettext_noop ("display private keys as well"),
  379. &private_keys),
  380. GNUNET_GETOPT_option_string (
  381. 's',
  382. "set",
  383. "SUBSYSTEM",
  384. gettext_noop (
  385. "set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
  386. &set_subsystem),
  387. GNUNET_GETOPT_option_verbose (&verbose),
  388. GNUNET_GETOPT_OPTION_END};
  389. int res;
  390. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  391. return 4;
  392. global_ret = TIMEOUT_STATUS_CODE; /* timeout */
  393. res = GNUNET_PROGRAM_run (argc,
  394. argv,
  395. "gnunet-identity",
  396. gettext_noop ("Maintain egos"),
  397. options,
  398. &run,
  399. NULL);
  400. GNUNET_free ((void *) argv);
  401. if (GNUNET_OK != res)
  402. return 3;
  403. return global_ret;
  404. }
  405. /* end of gnunet-identity.c */