X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fidentity%2Fgnunet-identity.c;h=8304a707dcd0c822304d28ebf440ec976ab3d1e0;hb=cea95af17bb3bfaf65224d0ecd2db2308e333764;hp=e56a058071e4e2a763cea70ff729e4b184f96bf7;hpb=46d9f8f827c8f797ae7d0fbfa3392e50d75a6006;p=oweals%2Fgnunet.git diff --git a/src/identity/gnunet-identity.c b/src/identity/gnunet-identity.c index e56a05807..8304a707d 100644 --- a/src/identity/gnunet-identity.c +++ b/src/identity/gnunet-identity.c @@ -23,8 +23,7 @@ * @author Christian Grothoff * * Todo: - * - add options to get/set default egos - * - print short hashes of egos when printing + * - add options to get default egos */ #include "platform.h" #include "gnunet_util_lib.h" @@ -55,6 +54,21 @@ static char *create_ego; */ static char *delete_ego; +/** + * -s option. + */ +static char *set_ego; + +/** + * -S option. + */ +static char *set_subsystem; + +/** + * Operation handle for set operation. + */ +static struct GNUNET_IDENTITY_Operation *set_op; + /** * Handle for create operation. */ @@ -76,12 +90,26 @@ static void shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { + if (NULL != set_op) + { + GNUNET_IDENTITY_cancel (set_op); + set_op = NULL; + } + if (NULL != create_op) + { + GNUNET_IDENTITY_cancel (create_op); + create_op = NULL; + } + if (NULL != delete_op) + { + GNUNET_IDENTITY_cancel (delete_op); + delete_op = NULL; + } GNUNET_IDENTITY_disconnect (sh); sh = NULL; } - /** * Test if we are finished yet. */ @@ -90,8 +118,10 @@ test_finished () { if ( (NULL == create_op) && (NULL == delete_op) && + (NULL == set_op) && + (NULL == set_ego) && (! list) && - (! monitor) ) + (! monitor) ) GNUNET_SCHEDULER_shutdown (); } @@ -138,6 +168,25 @@ create_finished (void *cls, } +/** + * Function called by #GNUNET_IDENTITY_set up on completion. + * + * @param cls NULL + * @param emsg error message (NULL on success) + */ +static void +set_done (void *cls, + const char *emsg) +{ + set_op = NULL; + if (NULL != emsg) + fprintf (stderr, + _("Failed to set default ego: %s\n"), + emsg); + test_finished (); +} + + /** * If listing is enabled, prints information about the egos. * @@ -152,11 +201,11 @@ create_finished (void *cls, * 'ego' does indicate an error (i.e. name is taken or no default * value is known). If 'ego' is non-NULL and if '*ctx' * is set in those callbacks, the value WILL be passed to a subsequent - * call to the identity callback of 'GNUNET_IDENTITY_connect' (if + * call to the identity callback of 'GNUNET_IDENTITY_connect' (if * that one was not NULL). * * When an identity is renamed, this function is called with the - * (known) ego but the NEW identifier. + * (known) ego but the NEW identifier. * * When an identity is deleted, this function is called with the * (known) ego and "NULL" for the 'identifier'. In this case, @@ -177,17 +226,51 @@ print_ego (void *cls, void **ctx, const char *identifier) { - if (! (list | monitor)) - return; + 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) ) + { + fprintf (stderr, + "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) ) { GNUNET_SCHEDULER_shutdown (); return; } - if (monitor) - fprintf (stderr, "%s - %p\n", identifier, ego); - else if (NULL != identifier) - fprintf (stderr, "%s\n", identifier); + if (! (list | monitor)) + return; + if (NULL == 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); + GNUNET_free (s); } @@ -203,6 +286,13 @@ static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { + if ( (NULL == set_subsystem) ^ + (NULL == set_ego) ) + { + fprintf (stderr, + "Options -e and -s must always be specified together\n"); + return; + } sh = GNUNET_IDENTITY_connect (cfg, &print_ego, NULL); if (NULL != delete_ego) delete_op = GNUNET_IDENTITY_delete (sh, @@ -242,9 +332,15 @@ main (int argc, char *const *argv) {'d', "display", NULL, gettext_noop ("display all egos"), 0, &GNUNET_GETOPT_set_one, &list}, + {'e', "ego", "NAME", + gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -s)"), + 1, &GNUNET_GETOPT_set_string, &set_ego}, {'m', "monitor", NULL, gettext_noop ("run in monitor mode egos"), 0, &GNUNET_GETOPT_set_one, &monitor}, + {'s', "set", "SUBSYSYSTEM", + gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"), + 1, &GNUNET_GETOPT_set_string, &set_subsystem}, GNUNET_GETOPT_OPTION_END }; @@ -252,7 +348,7 @@ main (int argc, char *const *argv) return 2; res = GNUNET_PROGRAM_run (argc, argv, "gnunet-identity", - gettext_noop ("Maintain egos"), + gettext_noop ("Maintain egos"), options, &run, NULL); GNUNET_free ((void *) argv);