2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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.
90 shutdown_task (void *cls,
91 const struct GNUNET_SCHEDULER_TaskContext *tc)
95 GNUNET_IDENTITY_cancel (set_op);
98 if (NULL != create_op)
100 GNUNET_IDENTITY_cancel (create_op);
103 if (NULL != delete_op)
105 GNUNET_IDENTITY_cancel (delete_op);
108 GNUNET_IDENTITY_disconnect (sh);
114 * Test if we are finished yet.
119 if ( (NULL == create_op) &&
120 (NULL == delete_op) &&
125 GNUNET_SCHEDULER_shutdown ();
130 * Deletion operation finished.
132 * @param cls pointer to operation handle
133 * @param emsg NULL on success, otherwise an error message
136 delete_finished (void *cls,
139 struct GNUNET_IDENTITY_Operation **op = cls;
151 * Creation operation finished.
153 * @param cls pointer to operation handle
154 * @param emsg error message, NULL on success
157 create_finished (void *cls,
160 struct GNUNET_IDENTITY_Operation **op = cls;
165 _("Failed to create ego: %s\n"),
172 * Function called by #GNUNET_IDENTITY_set up on completion.
175 * @param emsg error message (NULL on success)
184 _("Failed to set default ego: %s\n"),
191 * If listing is enabled, prints information about the egos.
193 * This function is initially called for all egos and then again
194 * whenever a ego's identifier changes or if it is deleted. At the
195 * end of the initial pass over all egos, the function is once called
196 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
197 * be invoked in the future or that there was an error.
199 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
200 * this function is only called ONCE, and 'NULL' being passed in
201 * 'ego' does indicate an error (i.e. name is taken or no default
202 * value is known). If 'ego' is non-NULL and if '*ctx'
203 * is set in those callbacks, the value WILL be passed to a subsequent
204 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
205 * that one was not NULL).
207 * When an identity is renamed, this function is called with the
208 * (known) ego but the NEW identifier.
210 * When an identity is deleted, this function is called with the
211 * (known) ego and "NULL" for the 'identifier'. In this case,
212 * the 'ego' is henceforth invalid (and the 'ctx' should also be
216 * @param ego ego handle
217 * @param ctx context for application to store data for this ego
218 * (during the lifetime of this process, initially NULL)
219 * @param identifier identifier assigned by the user for this ego,
220 * NULL if the user just deleted the ego and it
221 * must thus no longer be used
224 print_ego (void *cls,
225 struct GNUNET_IDENTITY_Ego *ego,
227 const char *identifier)
229 struct GNUNET_CRYPTO_EccPublicKey pk;
232 if ( (NULL != set_ego) &&
234 (NULL != identifier) &&
235 (0 == strcmp (identifier,
238 set_op = GNUNET_IDENTITY_set (sh,
243 GNUNET_free (set_subsystem);
244 set_subsystem = NULL;
245 GNUNET_free (set_ego);
248 if ( (NULL == ego) &&
252 "Could not set ego to `%s' for subsystem `%s', ego not known\n",
255 GNUNET_free (set_subsystem);
256 set_subsystem = NULL;
257 GNUNET_free (set_ego);
260 if ( (NULL == ego) && (! monitor) )
262 GNUNET_SCHEDULER_shutdown ();
265 if (! (list | monitor))
267 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
268 s = GNUNET_CRYPTO_ecc_public_key_to_string (&pk);
269 if ( (monitor) || (NULL != identifier) )
270 fprintf (stderr, "%s - %s\n", identifier, s);
275 * Main function that will be run by the scheduler.
278 * @param args remaining command-line arguments
279 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
280 * @param cfg configuration
283 run (void *cls, char *const *args, const char *cfgfile,
284 const struct GNUNET_CONFIGURATION_Handle *cfg)
286 if ( (NULL == set_subsystem) ^
290 "Options -e and -s must always be specified together\n");
293 sh = GNUNET_IDENTITY_connect (cfg, &print_ego, NULL);
294 if (NULL != delete_ego)
295 delete_op = GNUNET_IDENTITY_delete (sh,
299 if (NULL != create_ego)
300 create_op = GNUNET_IDENTITY_create (sh,
304 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
305 &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", "SUBSYSYSTEM",
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 */