From 4e35e0bacaf1a6e2ab7a98f0a729f5ea493aef3d Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 9 May 2019 18:55:58 +0200 Subject: [PATCH] allow using -e as filter on ego list --- doc/man/gnunet-identity.1 | 4 +- src/identity/gnunet-identity.c | 212 ++++++++++++++------------------- 2 files changed, 91 insertions(+), 125 deletions(-) diff --git a/doc/man/gnunet-identity.1 b/doc/man/gnunet-identity.1 index 3f4510d99..4a8970cf5 100644 --- a/doc/man/gnunet-identity.1 +++ b/doc/man/gnunet-identity.1 @@ -58,9 +58,7 @@ Creates a new ego with the given Delete the ego with the given .Ar NAME . .It Fl e Ar NAME | Fl \-ego= Ns Ar NAME -Perform "set" operation with the respective ego. -Needs to be used together with option -.Fl s . +Perform "set" operation with the respective ego or restrict "display" operation to the respective ego. .It Fl h | \-help Print the help page. .It d | \-display diff --git a/src/identity/gnunet-identity.c b/src/identity/gnunet-identity.c index 583305710..8366b9918 100644 --- a/src/identity/gnunet-identity.c +++ b/src/identity/gnunet-identity.c @@ -11,7 +11,7 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . @@ -119,6 +119,11 @@ shutdown_task (void *cls) GNUNET_IDENTITY_cancel (delete_op); delete_op = NULL; } + if (NULL != set_ego) + { + GNUNET_free (set_ego); + set_ego = NULL; + } GNUNET_IDENTITY_disconnect (sh); sh = NULL; } @@ -130,12 +135,8 @@ shutdown_task (void *cls) static void test_finished () { - if ( (NULL == create_op) && - (NULL == delete_op) && - (NULL == set_op) && - (NULL == set_ego) && - (! list) && - (! monitor) ) + if ((NULL == create_op) && (NULL == delete_op) && (NULL == set_op) && + (NULL == set_subsystem) && (! list) && (! monitor)) { if (TIMEOUT_STATUS_CODE == global_ret) global_ret = 0; @@ -151,16 +152,13 @@ test_finished () * @param emsg NULL on success, otherwise an error message */ static void -delete_finished (void *cls, - const char *emsg) +delete_finished (void *cls, const char *emsg) { struct GNUNET_IDENTITY_Operation **op = cls; *op = NULL; if (NULL != emsg) - fprintf (stderr, - "%s\n", - gettext (emsg)); + fprintf (stderr, "%s\n", gettext (emsg)); test_finished (); } @@ -174,30 +172,25 @@ delete_finished (void *cls, */ static void create_finished (void *cls, - const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk, - const char *emsg) + const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk, + const char *emsg) { struct GNUNET_IDENTITY_Operation **op = cls; *op = NULL; if (NULL == pk) { - fprintf (stderr, - _("Failed to create ego: %s\n"), - emsg); + fprintf (stderr, _ ("Failed to create ego: %s\n"), emsg); global_ret = 1; } else if (verbose) { struct GNUNET_CRYPTO_EcdsaPublicKey pub; char *pubs; - - GNUNET_CRYPTO_ecdsa_key_get_public (pk, - &pub); + + GNUNET_CRYPTO_ecdsa_key_get_public (pk, &pub); pubs = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pub); - fprintf (stdout, - "%s\n", - pubs); + fprintf (stdout, "%s\n", pubs); GNUNET_free (pubs); } test_finished (); @@ -211,15 +204,12 @@ create_finished (void *cls, * @param emsg error message (NULL on success) */ static void -set_done (void *cls, - const char *emsg) +set_done (void *cls, const char *emsg) { set_op = NULL; if (NULL != emsg) { - fprintf (stderr, - _("Failed to set default ego: %s\n"), - emsg); + fprintf (stderr, _ ("Failed to set default ego: %s\n"), emsg); global_ret = 1; } test_finished (); @@ -261,42 +251,34 @@ set_done (void *cls, */ static void print_ego (void *cls, - struct GNUNET_IDENTITY_Ego *ego, - void **ctx, - const char *identifier) + struct GNUNET_IDENTITY_Ego *ego, + void **ctx, + const char *identifier) { struct GNUNET_CRYPTO_EcdsaPublicKey pk; char *s; - if ( (NULL != set_ego) && - (NULL != ego) && - (NULL != identifier) && - (0 == strcmp (identifier, - set_ego)) ) - { - set_op = GNUNET_IDENTITY_set (sh, - set_subsystem, - ego, - &set_done, - NULL); - GNUNET_free (set_subsystem); - set_subsystem = NULL; - GNUNET_free (set_ego); - set_ego = NULL; - } - if ( (NULL == ego) && - (NULL != set_ego) ) + if ((NULL != set_ego) && (NULL != set_subsystem) && (NULL != ego) && + (NULL != identifier) && (0 == strcmp (identifier, set_ego))) + { + set_op = GNUNET_IDENTITY_set (sh, set_subsystem, ego, &set_done, NULL); + GNUNET_free (set_subsystem); + set_subsystem = NULL; + GNUNET_free (set_ego); + set_ego = NULL; + } + if ((NULL == ego) && (NULL != set_ego) && (NULL != set_subsystem)) { fprintf (stderr, - "Could not set ego to `%s' for subsystem `%s', ego not known\n", - set_ego, - set_subsystem); + "Could not set ego to `%s' for subsystem `%s', ego not known\n", + set_ego, + set_subsystem); GNUNET_free (set_subsystem); set_subsystem = NULL; GNUNET_free (set_ego); set_ego = NULL; } - if ( (NULL == ego) && (! monitor) ) + if ((NULL == ego) && (! monitor)) { list = 0; test_finished (); @@ -306,15 +288,12 @@ print_ego (void *cls, return; if (NULL == ego) return; - GNUNET_IDENTITY_ego_get_public_key (ego, - &pk); + if ((NULL != set_ego) && (0 != strcmp (identifier, set_ego))) + return; + GNUNET_IDENTITY_ego_get_public_key (ego, &pk); s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); - if ( (monitor) || - (NULL != identifier) ) - fprintf (stdout, - "%s - %s\n", - identifier, - s); + if ((monitor) || (NULL != identifier)) + fprintf (stdout, "%s - %s\n", identifier, s); GNUNET_free (s); } @@ -333,28 +312,19 @@ run (void *cls, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { - if ( (NULL == set_subsystem) ^ - (NULL == set_ego) ) + if ((NULL != set_subsystem) && (NULL == set_ego)) { - fprintf (stderr, - "Options -e and -s must always be specified together\n"); + fprintf (stderr, "Option -s requires option -e to be specified as well.\n"); return; } - sh = GNUNET_IDENTITY_connect (cfg, - &print_ego, - NULL); + sh = GNUNET_IDENTITY_connect (cfg, &print_ego, NULL); if (NULL != delete_ego) - delete_op = GNUNET_IDENTITY_delete (sh, - delete_ego, - &delete_finished, - &delete_op); + delete_op = + GNUNET_IDENTITY_delete (sh, delete_ego, &delete_finished, &delete_op); if (NULL != create_ego) - create_op = GNUNET_IDENTITY_create (sh, - create_ego, - &create_finished, - &create_op); - GNUNET_SCHEDULER_add_shutdown (&shutdown_task, - NULL); + create_op = + GNUNET_IDENTITY_create (sh, create_ego, &create_finished, &create_op); + GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); test_finished (); } @@ -369,55 +339,53 @@ run (void *cls, int main (int argc, char *const *argv) { - struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_option_string ('C', - "create", - "NAME", - gettext_noop ("create ego NAME"), - &create_ego), - - GNUNET_GETOPT_option_string ('D', - "delete", - "NAME", - gettext_noop ("delete ego NAME "), - &delete_ego), - - GNUNET_GETOPT_option_flag ('d', - "display", - gettext_noop ("display all egos"), - &list), - - GNUNET_GETOPT_option_string ('e', - "ego", - "NAME", - gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -s)"), - &set_ego), - - GNUNET_GETOPT_option_flag ('m', - "monitor", - gettext_noop ("run in monitor mode egos"), - &monitor), - - GNUNET_GETOPT_option_string ('s', - "set", - "SUBSYSTEM", - gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"), - &set_subsystem), - GNUNET_GETOPT_option_verbose (&verbose), - GNUNET_GETOPT_OPTION_END - }; + struct GNUNET_GETOPT_CommandLineOption options[] = + {GNUNET_GETOPT_option_string ('C', + "create", + "NAME", + gettext_noop ("create ego NAME"), + &create_ego), + GNUNET_GETOPT_option_string ('D', + "delete", + "NAME", + gettext_noop ("delete ego NAME "), + &delete_ego), + GNUNET_GETOPT_option_flag ('d', + "display", + gettext_noop ("display all egos"), + &list), + GNUNET_GETOPT_option_string ( + 'e', + "ego", + "NAME", + gettext_noop ( + "set default identity to NAME for a subsystem SUBSYSTEM (use together with -s) or restrict results to NAME (use together with -d)"), + &set_ego), + GNUNET_GETOPT_option_flag ('m', + "monitor", + gettext_noop ("run in monitor mode egos"), + &monitor), + GNUNET_GETOPT_option_string ( + 's', + "set", + "SUBSYSTEM", + gettext_noop ( + "set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"), + &set_subsystem), + GNUNET_GETOPT_option_verbose (&verbose), + GNUNET_GETOPT_OPTION_END}; int res; - if (GNUNET_OK != - GNUNET_STRINGS_get_utf8_args (argc, argv, - &argc, &argv)) + if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) return 4; global_ret = TIMEOUT_STATUS_CODE; /* timeout */ - res = GNUNET_PROGRAM_run (argc, argv, + res = GNUNET_PROGRAM_run (argc, + argv, "gnunet-identity", - gettext_noop ("Maintain egos"), - options, &run, - NULL); + gettext_noop ("Maintain egos"), + options, + &run, + NULL); GNUNET_free ((void *) argv); if (GNUNET_OK != res) -- 2.25.1