2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
16 * @file identity/gnunet-identity.c
17 * @brief IDENTITY management command line tool
18 * @author Christian Grothoff
21 * - add options to get default egos
24 #include "gnunet_util_lib.h"
25 #include "gnunet_identity_service.h"
29 * Return value from main on timeout.
31 #define TIMEOUT_STATUS_CODE 40
34 * Handle to IDENTITY service.
36 static struct GNUNET_IDENTITY_Handle *sh;
39 * Was "list" specified?
44 * Was "monitor" specified?
51 static char *create_ego;
56 static char *delete_ego;
66 static char *set_subsystem;
69 * Operation handle for set operation.
71 static struct GNUNET_IDENTITY_Operation *set_op;
74 * Handle for create operation.
76 static struct GNUNET_IDENTITY_Operation *create_op;
79 * Handle for delete operation.
81 static struct GNUNET_IDENTITY_Operation *delete_op;
84 * Value to return from #main().
86 static int global_ret;
90 * Task run on shutdown.
95 shutdown_task (void *cls)
99 GNUNET_IDENTITY_cancel (set_op);
102 if (NULL != create_op)
104 GNUNET_IDENTITY_cancel (create_op);
107 if (NULL != delete_op)
109 GNUNET_IDENTITY_cancel (delete_op);
112 GNUNET_IDENTITY_disconnect (sh);
118 * Test if we are finished yet.
123 if ( (NULL == create_op) &&
124 (NULL == delete_op) &&
130 if (TIMEOUT_STATUS_CODE == global_ret)
132 GNUNET_SCHEDULER_shutdown ();
138 * Deletion operation finished.
140 * @param cls pointer to operation handle
141 * @param emsg NULL on success, otherwise an error message
144 delete_finished (void *cls,
147 struct GNUNET_IDENTITY_Operation **op = cls;
159 * Creation operation finished.
161 * @param cls pointer to operation handle
162 * @param emsg error message, NULL on success
165 create_finished (void *cls,
168 struct GNUNET_IDENTITY_Operation **op = cls;
174 _("Failed to create ego: %s\n"),
183 * Function called by #GNUNET_IDENTITY_set up on completion.
186 * @param emsg error message (NULL on success)
196 _("Failed to set default ego: %s\n"),
205 * If listing is enabled, prints information about the egos.
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.
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).
221 * When an identity is renamed, this function is called with the
222 * (known) ego but the NEW identifier.
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
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
238 print_ego (void *cls,
239 struct GNUNET_IDENTITY_Ego *ego,
241 const char *identifier)
243 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
246 if ( (NULL != set_ego) &&
248 (NULL != identifier) &&
249 (0 == strcmp (identifier,
252 set_op = GNUNET_IDENTITY_set (sh,
257 GNUNET_free (set_subsystem);
258 set_subsystem = NULL;
259 GNUNET_free (set_ego);
262 if ( (NULL == ego) &&
266 "Could not set ego to `%s' for subsystem `%s', ego not known\n",
269 GNUNET_free (set_subsystem);
270 set_subsystem = NULL;
271 GNUNET_free (set_ego);
274 if ( (NULL == ego) && (! monitor) )
280 if (! (list | monitor))
284 GNUNET_IDENTITY_ego_get_public_key (ego,
286 s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
288 (NULL != identifier) )
298 * Main function that will be run by the scheduler.
301 * @param args remaining command-line arguments
302 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
303 * @param cfg configuration
309 const struct GNUNET_CONFIGURATION_Handle *cfg)
311 if ( (NULL == set_subsystem) ^
315 "Options -e and -s must always be specified together\n");
318 sh = GNUNET_IDENTITY_connect (cfg,
321 if (NULL != delete_ego)
322 delete_op = GNUNET_IDENTITY_delete (sh,
326 if (NULL != create_ego)
327 create_op = GNUNET_IDENTITY_create (sh,
331 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
340 * @param argc number of arguments from the command line
341 * @param argv command line arguments
342 * @return 0 ok, 1 on error
345 main (int argc, char *const *argv)
347 struct GNUNET_GETOPT_CommandLineOption options[] = {
348 GNUNET_GETOPT_option_string ('C',
351 gettext_noop ("create ego NAME"),
354 GNUNET_GETOPT_option_string ('D',
357 gettext_noop ("delete ego NAME "),
360 GNUNET_GETOPT_option_flag ('d',
362 gettext_noop ("display all egos"),
365 GNUNET_GETOPT_option_string ('e',
368 gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -s)"),
371 GNUNET_GETOPT_option_flag ('m',
373 gettext_noop ("run in monitor mode egos"),
376 GNUNET_GETOPT_option_string ('s',
379 gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
382 GNUNET_GETOPT_OPTION_END
387 GNUNET_STRINGS_get_utf8_args (argc, argv,
390 global_ret = TIMEOUT_STATUS_CODE; /* timeout */
391 res = GNUNET_PROGRAM_run (argc, argv,
393 gettext_noop ("Maintain egos"),
396 GNUNET_free ((void *) argv);
398 if (GNUNET_OK != res)
403 /* end of gnunet-identity.c */