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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file identity/gnunet-identity.c
20 * @brief IDENTITY management command line tool
21 * @author Christian Grothoff
24 * - add options to get default egos
27 #include "gnunet_util_lib.h"
28 #include "gnunet_identity_service.h"
32 * Return value from main on timeout.
34 #define TIMEOUT_STATUS_CODE 40
37 * Handle to IDENTITY service.
39 static struct GNUNET_IDENTITY_Handle *sh;
42 * Was "list" specified?
47 * Was "monitor" specified?
54 static char *create_ego;
59 static char *delete_ego;
69 static char *set_subsystem;
72 * Operation handle for set operation.
74 static struct GNUNET_IDENTITY_Operation *set_op;
77 * Handle for create operation.
79 static struct GNUNET_IDENTITY_Operation *create_op;
82 * Handle for delete operation.
84 static struct GNUNET_IDENTITY_Operation *delete_op;
87 * Value to return from #main().
89 static int global_ret;
93 * Task run on shutdown.
98 shutdown_task (void *cls)
102 GNUNET_IDENTITY_cancel (set_op);
105 if (NULL != create_op)
107 GNUNET_IDENTITY_cancel (create_op);
110 if (NULL != delete_op)
112 GNUNET_IDENTITY_cancel (delete_op);
115 GNUNET_IDENTITY_disconnect (sh);
121 * Test if we are finished yet.
126 if ( (NULL == create_op) &&
127 (NULL == delete_op) &&
133 if (TIMEOUT_STATUS_CODE == global_ret)
135 GNUNET_SCHEDULER_shutdown ();
141 * Deletion operation finished.
143 * @param cls pointer to operation handle
144 * @param emsg NULL on success, otherwise an error message
147 delete_finished (void *cls,
150 struct GNUNET_IDENTITY_Operation **op = cls;
162 * Creation operation finished.
164 * @param cls pointer to operation handle
165 * @param emsg error message, NULL on success
168 create_finished (void *cls,
171 struct GNUNET_IDENTITY_Operation **op = cls;
177 _("Failed to create ego: %s\n"),
186 * Function called by #GNUNET_IDENTITY_set up on completion.
189 * @param emsg error message (NULL on success)
199 _("Failed to set default ego: %s\n"),
208 * If listing is enabled, prints information about the egos.
210 * This function is initially called for all egos and then again
211 * whenever a ego's identifier changes or if it is deleted. At the
212 * end of the initial pass over all egos, the function is once called
213 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
214 * be invoked in the future or that there was an error.
216 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
217 * this function is only called ONCE, and 'NULL' being passed in
218 * 'ego' does indicate an error (i.e. name is taken or no default
219 * value is known). If 'ego' is non-NULL and if '*ctx'
220 * is set in those callbacks, the value WILL be passed to a subsequent
221 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
222 * that one was not NULL).
224 * When an identity is renamed, this function is called with the
225 * (known) ego but the NEW identifier.
227 * When an identity is deleted, this function is called with the
228 * (known) ego and "NULL" for the 'identifier'. In this case,
229 * the 'ego' is henceforth invalid (and the 'ctx' should also be
233 * @param ego ego handle
234 * @param ctx context for application to store data for this ego
235 * (during the lifetime of this process, initially NULL)
236 * @param identifier identifier assigned by the user for this ego,
237 * NULL if the user just deleted the ego and it
238 * must thus no longer be used
241 print_ego (void *cls,
242 struct GNUNET_IDENTITY_Ego *ego,
244 const char *identifier)
246 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
249 if ( (NULL != set_ego) &&
251 (NULL != identifier) &&
252 (0 == strcmp (identifier,
255 set_op = GNUNET_IDENTITY_set (sh,
260 GNUNET_free (set_subsystem);
261 set_subsystem = NULL;
262 GNUNET_free (set_ego);
265 if ( (NULL == ego) &&
269 "Could not set ego to `%s' for subsystem `%s', ego not known\n",
272 GNUNET_free (set_subsystem);
273 set_subsystem = NULL;
274 GNUNET_free (set_ego);
277 if ( (NULL == ego) && (! monitor) )
283 if (! (list | monitor))
287 GNUNET_IDENTITY_ego_get_public_key (ego,
289 s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
291 (NULL != identifier) )
301 * Main function that will be run by the scheduler.
304 * @param args remaining command-line arguments
305 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
306 * @param cfg configuration
312 const struct GNUNET_CONFIGURATION_Handle *cfg)
314 if ( (NULL == set_subsystem) ^
318 "Options -e and -s must always be specified together\n");
321 sh = GNUNET_IDENTITY_connect (cfg,
324 if (NULL != delete_ego)
325 delete_op = GNUNET_IDENTITY_delete (sh,
329 if (NULL != create_ego)
330 create_op = GNUNET_IDENTITY_create (sh,
334 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
343 * @param argc number of arguments from the command line
344 * @param argv command line arguments
345 * @return 0 ok, 1 on error
348 main (int argc, char *const *argv)
350 struct GNUNET_GETOPT_CommandLineOption options[] = {
351 GNUNET_GETOPT_option_string ('C',
354 gettext_noop ("create ego NAME"),
357 GNUNET_GETOPT_option_string ('D',
360 gettext_noop ("delete ego NAME "),
363 GNUNET_GETOPT_option_flag ('d',
365 gettext_noop ("display all egos"),
368 GNUNET_GETOPT_option_string ('e',
371 gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -s)"),
374 GNUNET_GETOPT_option_flag ('m',
376 gettext_noop ("run in monitor mode egos"),
379 GNUNET_GETOPT_option_string ('s',
382 gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
385 GNUNET_GETOPT_OPTION_END
390 GNUNET_STRINGS_get_utf8_args (argc, argv,
393 global_ret = TIMEOUT_STATUS_CODE; /* timeout */
394 res = GNUNET_PROGRAM_run (argc, argv,
396 gettext_noop ("Maintain egos"),
399 GNUNET_free ((void *) argv);
401 if (GNUNET_OK != res)
406 /* end of gnunet-identity.c */