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"
33 * Handle to IDENTITY service.
35 static struct GNUNET_IDENTITY_Handle *sh;
38 * Was "list" specified?
43 * Was "monitor" specified?
50 static char *create_ego;
55 static char *delete_ego;
65 static char *set_subsystem;
68 * Operation handle for set operation.
70 static struct GNUNET_IDENTITY_Operation *set_op;
73 * Handle for create operation.
75 static struct GNUNET_IDENTITY_Operation *create_op;
78 * Handle for delete operation.
80 static struct GNUNET_IDENTITY_Operation *delete_op;
84 * Task run on shutdown.
89 shutdown_task (void *cls)
93 GNUNET_IDENTITY_cancel (set_op);
96 if (NULL != create_op)
98 GNUNET_IDENTITY_cancel (create_op);
101 if (NULL != delete_op)
103 GNUNET_IDENTITY_cancel (delete_op);
106 GNUNET_IDENTITY_disconnect (sh);
112 * Test if we are finished yet.
117 if ( (NULL == create_op) &&
118 (NULL == delete_op) &&
123 GNUNET_SCHEDULER_shutdown ();
128 * Deletion operation finished.
130 * @param cls pointer to operation handle
131 * @param emsg NULL on success, otherwise an error message
134 delete_finished (void *cls,
137 struct GNUNET_IDENTITY_Operation **op = cls;
149 * Creation operation finished.
151 * @param cls pointer to operation handle
152 * @param emsg error message, NULL on success
155 create_finished (void *cls,
158 struct GNUNET_IDENTITY_Operation **op = cls;
163 _("Failed to create ego: %s\n"),
170 * Function called by #GNUNET_IDENTITY_set up on completion.
173 * @param emsg error message (NULL on success)
182 _("Failed to set default ego: %s\n"),
189 * If listing is enabled, prints information about the egos.
191 * This function is initially called for all egos and then again
192 * whenever a ego's identifier changes or if it is deleted. At the
193 * end of the initial pass over all egos, the function is once called
194 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
195 * be invoked in the future or that there was an error.
197 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
198 * this function is only called ONCE, and 'NULL' being passed in
199 * 'ego' does indicate an error (i.e. name is taken or no default
200 * value is known). If 'ego' is non-NULL and if '*ctx'
201 * is set in those callbacks, the value WILL be passed to a subsequent
202 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
203 * that one was not NULL).
205 * When an identity is renamed, this function is called with the
206 * (known) ego but the NEW identifier.
208 * When an identity is deleted, this function is called with the
209 * (known) ego and "NULL" for the 'identifier'. In this case,
210 * the 'ego' is henceforth invalid (and the 'ctx' should also be
214 * @param ego ego handle
215 * @param ctx context for application to store data for this ego
216 * (during the lifetime of this process, initially NULL)
217 * @param identifier identifier assigned by the user for this ego,
218 * NULL if the user just deleted the ego and it
219 * must thus no longer be used
222 print_ego (void *cls,
223 struct GNUNET_IDENTITY_Ego *ego,
225 const char *identifier)
227 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
230 if ( (NULL != set_ego) &&
232 (NULL != identifier) &&
233 (0 == strcmp (identifier,
236 set_op = GNUNET_IDENTITY_set (sh,
241 GNUNET_free (set_subsystem);
242 set_subsystem = NULL;
243 GNUNET_free (set_ego);
246 if ( (NULL == ego) &&
250 "Could not set ego to `%s' for subsystem `%s', ego not known\n",
253 GNUNET_free (set_subsystem);
254 set_subsystem = NULL;
255 GNUNET_free (set_ego);
258 if ( (NULL == ego) && (! monitor) )
260 GNUNET_SCHEDULER_shutdown ();
263 if (! (list | monitor))
267 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
268 s = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
269 if ( (monitor) || (NULL != identifier) )
270 fprintf (stdout, "%s - %s\n", identifier, s);
276 * Main function that will be run by the scheduler.
279 * @param args remaining command-line arguments
280 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
281 * @param cfg configuration
284 run (void *cls, char *const *args, const char *cfgfile,
285 const struct GNUNET_CONFIGURATION_Handle *cfg)
287 if ( (NULL == set_subsystem) ^
291 "Options -e and -s must always be specified together\n");
294 sh = GNUNET_IDENTITY_connect (cfg, &print_ego, NULL);
295 if (NULL != delete_ego)
296 delete_op = GNUNET_IDENTITY_delete (sh,
300 if (NULL != create_ego)
301 create_op = GNUNET_IDENTITY_create (sh,
305 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
313 * @param argc number of arguments from the command line
314 * @param argv command line arguments
315 * @return 0 ok, 1 on error
318 main (int argc, char *const *argv)
322 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
323 {'C', "create", "NAME",
324 gettext_noop ("create ego NAME"),
325 1, &GNUNET_GETOPT_set_string, &create_ego},
326 {'D', "delete", "NAME",
327 gettext_noop ("delete ego NAME "),
328 1, &GNUNET_GETOPT_set_string, &delete_ego},
329 {'d', "display", NULL,
330 gettext_noop ("display all egos"),
331 0, &GNUNET_GETOPT_set_one, &list},
333 gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -s)"),
334 1, &GNUNET_GETOPT_set_string, &set_ego},
335 {'m', "monitor", NULL,
336 gettext_noop ("run in monitor mode egos"),
337 0, &GNUNET_GETOPT_set_one, &monitor},
338 {'s', "set", "SUBSYSTEM",
339 gettext_noop ("set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
340 1, &GNUNET_GETOPT_set_string, &set_subsystem},
341 GNUNET_GETOPT_OPTION_END
344 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
347 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-identity",
348 gettext_noop ("Maintain egos"),
351 GNUNET_free ((void *) argv);
353 if (GNUNET_OK != res)
358 /* end of gnunet-identity.c */