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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file identity/gnunet-identity.c
22 * @brief IDENTITY management command line tool
23 * @author Christian Grothoff
26 * - add options to get default egos
29 #include "gnunet_util_lib.h"
30 #include "gnunet_identity_service.h"
34 * Return value from main on timeout.
36 #define TIMEOUT_STATUS_CODE 40
39 * Handle to IDENTITY service.
41 static struct GNUNET_IDENTITY_Handle *sh;
44 * Was "list" specified?
49 * Was "monitor" specified?
56 static char *create_ego;
61 static char *delete_ego;
71 static char *set_subsystem;
74 * Operation handle for set operation.
76 static struct GNUNET_IDENTITY_Operation *set_op;
79 * Handle for create operation.
81 static struct GNUNET_IDENTITY_Operation *create_op;
84 * Handle for delete operation.
86 static struct GNUNET_IDENTITY_Operation *delete_op;
89 * Value to return from #main().
91 static int global_ret;
95 * Task run on shutdown.
100 shutdown_task (void *cls)
104 GNUNET_IDENTITY_cancel (set_op);
107 if (NULL != create_op)
109 GNUNET_IDENTITY_cancel (create_op);
112 if (NULL != delete_op)
114 GNUNET_IDENTITY_cancel (delete_op);
117 GNUNET_IDENTITY_disconnect (sh);
123 * Test if we are finished yet.
128 if ( (NULL == create_op) &&
129 (NULL == delete_op) &&
135 if (TIMEOUT_STATUS_CODE == global_ret)
137 GNUNET_SCHEDULER_shutdown ();
143 * Deletion operation finished.
145 * @param cls pointer to operation handle
146 * @param emsg NULL on success, otherwise an error message
149 delete_finished (void *cls,
152 struct GNUNET_IDENTITY_Operation **op = cls;
164 * Creation operation finished.
166 * @param cls pointer to operation handle
167 * @param emsg error message, NULL on success
170 create_finished (void *cls,
173 struct GNUNET_IDENTITY_Operation **op = cls;
179 _("Failed to create ego: %s\n"),
188 * Function called by #GNUNET_IDENTITY_set up on completion.
191 * @param emsg error message (NULL on success)
201 _("Failed to set default ego: %s\n"),
210 * If listing is enabled, prints information about the egos.
212 * This function is initially called for all egos and then again
213 * whenever a ego's identifier changes or if it is deleted. At the
214 * end of the initial pass over all egos, the function is once called
215 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
216 * be invoked in the future or that there was an error.
218 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
219 * this function is only called ONCE, and 'NULL' being passed in
220 * 'ego' does indicate an error (i.e. name is taken or no default
221 * value is known). If 'ego' is non-NULL and if '*ctx'
222 * is set in those callbacks, the value WILL be passed to a subsequent
223 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
224 * that one was not NULL).
226 * When an identity is renamed, this function is called with the
227 * (known) ego but the NEW identifier.
229 * When an identity is deleted, this function is called with the
230 * (known) ego and "NULL" for the 'identifier'. In this case,
231 * the 'ego' is henceforth invalid (and the 'ctx' should also be
235 * @param ego ego handle
236 * @param ctx context for application to store data for this ego
237 * (during the lifetime of this process, initially NULL)
238 * @param identifier identifier assigned by the user for this ego,
239 * NULL if the user just deleted the ego and it
240 * must thus no longer be used
243 print_ego (void *cls,
244 struct GNUNET_IDENTITY_Ego *ego,
246 const char *identifier)
248 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
251 if ( (NULL != set_ego) &&
253 (NULL != identifier) &&
254 (0 == strcmp (identifier,
257 set_op = GNUNET_IDENTITY_set (sh,
262 GNUNET_free (set_subsystem);
263 set_subsystem = NULL;
264 GNUNET_free (set_ego);
267 if ( (NULL == ego) &&
271 "Could not set ego to `%s' for subsystem `%s', ego not known\n",
274 GNUNET_free (set_subsystem);
275 set_subsystem = NULL;
276 GNUNET_free (set_ego);
279 if ( (NULL == ego) && (! monitor) )
285 if (! (list | monitor))
289 GNUNET_IDENTITY_ego_get_public_key (ego,
291 s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
293 (NULL != identifier) )
303 * Main function that will be run by the scheduler.
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
314 const struct GNUNET_CONFIGURATION_Handle *cfg)
316 if ( (NULL == set_subsystem) ^
320 "Options -e and -s must always be specified together\n");
323 sh = GNUNET_IDENTITY_connect (cfg,
326 if (NULL != delete_ego)
327 delete_op = GNUNET_IDENTITY_delete (sh,
331 if (NULL != create_ego)
332 create_op = GNUNET_IDENTITY_create (sh,
336 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
345 * @param argc number of arguments from the command line
346 * @param argv command line arguments
347 * @return 0 ok, 1 on error
350 main (int argc, char *const *argv)
352 struct GNUNET_GETOPT_CommandLineOption options[] = {
353 GNUNET_GETOPT_OPTION_STRING ('C',
356 gettext_noop ("create ego NAME"),
359 GNUNET_GETOPT_OPTION_STRING ('D',
362 gettext_noop ("delete ego NAME "),
365 GNUNET_GETOPT_OPTION_SET_ONE ('d',
367 gettext_noop ("display all egos"),
370 GNUNET_GETOPT_OPTION_STRING ('e',
373 gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -s)"),
376 GNUNET_GETOPT_OPTION_SET_ONE ('m',
378 gettext_noop ("run in monitor mode egos"),
381 GNUNET_GETOPT_OPTION_STRING ('s',
384 gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
387 GNUNET_GETOPT_OPTION_END
392 GNUNET_STRINGS_get_utf8_args (argc, argv,
395 global_ret = TIMEOUT_STATUS_CODE; /* timeout */
396 res = GNUNET_PROGRAM_run (argc, argv,
398 gettext_noop ("Maintain egos"),
401 GNUNET_free ((void *) argv);
403 if (GNUNET_OK != res)
408 /* end of gnunet-identity.c */