GNUnet  0.11.x
gnunet-identity.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2013, 2018, 2019 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
31 
32 
36 #define TIMEOUT_STATUS_CODE 40
37 
41 static struct GNUNET_IDENTITY_Handle *sh;
42 
46 static int list;
47 
51 static int monitor;
52 
56 static int private_keys;
57 
61 static unsigned int verbose;
62 
66 static int quiet;
67 
71 static char *create_ego;
72 
76 static char *delete_ego;
77 
81 static char *privkey_ego;
82 
86 static char *set_ego;
87 
91 static char *set_subsystem;
92 
97 
102 
107 
112 
116 static int global_ret;
117 
118 
124 static void
125 shutdown_task (void *cls)
126 {
127  if (NULL != set_op)
128  {
129  GNUNET_IDENTITY_cancel (set_op);
130  set_op = NULL;
131  }
132  if (NULL != create_op)
133  {
134  GNUNET_IDENTITY_cancel (create_op);
135  create_op = NULL;
136  }
137  if (NULL != delete_op)
138  {
139  GNUNET_IDENTITY_cancel (delete_op);
140  delete_op = NULL;
141  }
142  if (NULL != set_ego)
143  {
145  set_ego = NULL;
146  }
148  sh = NULL;
149 }
150 
151 
155 static void
157 {
158  if ( (NULL == create_op) &&
159  (NULL == delete_op) &&
160  (NULL == set_op) &&
161  (NULL == set_subsystem) &&
162  (! list) &&
163  (! monitor))
164  {
166  global_ret = 0;
168  }
169 }
170 
171 
178 static void
179 delete_finished (void *cls,
180  const char *emsg)
181 {
182  struct GNUNET_IDENTITY_Operation **op = cls;
183 
184  *op = NULL;
185  if (NULL != emsg)
186  fprintf (stderr, "%s\n", gettext (emsg));
187  test_finished ();
188 }
189 
190 
198 static void
200  const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
201  const char *emsg)
202 {
203  struct GNUNET_IDENTITY_Operation **op = cls;
204 
205  *op = NULL;
206  if (NULL == pk)
207  {
208  fprintf (stderr,
209  _ ("Failed to create ego: %s\n"),
210  emsg);
211  global_ret = 1;
212  }
213  else if (verbose)
214  {
215  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
216  char *pubs;
217 
220  if (private_keys)
221  {
222  char *privs;
223 
225  fprintf (stdout, "%s - %s\n", pubs, privs);
226  GNUNET_free (privs);
227  }
228  else
229  {
230  fprintf (stdout, "%s\n", pubs);
231  }
232  GNUNET_free (pubs);
233  }
234  test_finished ();
235 }
236 
237 
244 static void
245 set_done (void *cls, const char *emsg)
246 {
247  set_op = NULL;
248  if (NULL != emsg)
249  {
250  fprintf (stderr, _ ("Failed to set default ego: %s\n"), emsg);
251  global_ret = 1;
252  }
253  test_finished ();
254 }
255 
256 
290 static void
291 print_ego (void *cls,
292  struct GNUNET_IDENTITY_Ego *ego,
293  void **ctx,
294  const char *identifier)
295 {
297  char *s;
298  char *privs;
299 
300  if ( (NULL != set_ego) &&
301  (NULL != set_subsystem) &&
302  (NULL != ego) &&
303  (NULL != identifier) &&
304  (0 == strcmp (identifier, set_ego)))
305  {
306  set_op = GNUNET_IDENTITY_set (sh,
308  ego,
309  &set_done,
310  NULL);
312  set_subsystem = NULL;
314  set_ego = NULL;
315  }
316  if ( (NULL == ego) &&
317  (NULL != set_ego) &&
318  (NULL != set_subsystem) )
319  {
320  fprintf (stderr,
321  "Could not set ego to `%s' for subsystem `%s', ego not known\n",
322  set_ego,
323  set_subsystem);
325  set_subsystem = NULL;
327  set_ego = NULL;
328  }
329  if ((NULL == ego) && (! monitor))
330  {
331  list = 0;
332  test_finished ();
333  return;
334  }
335  if (! (list | monitor))
336  return;
337  if ( (NULL == ego) ||
338  (NULL == identifier) )
339  return;
340  if ( (NULL != set_ego) &&
341  (0 != strcmp (identifier,
342  set_ego)) )
343  return;
348  if ((monitor) || (NULL != identifier))
349  {
350  if (quiet)
351  {
352  if (private_keys)
353  fprintf (stdout, "%s - %s\n", s, privs);
354  else
355  fprintf (stdout, "%s\n", s);
356  }
357  else
358  {
359  if (private_keys)
360  fprintf (stdout, "%s - %s - %s\n", identifier, s, privs);
361  else
362  fprintf (stdout, "%s - %s\n", identifier, s);
363  }
364  }
365  GNUNET_free (privs);
366  GNUNET_free (s);
367 }
368 
369 
378 static void
379 run (void *cls,
380  char *const *args,
381  const char *cfgfile,
382  const struct GNUNET_CONFIGURATION_Handle *cfg)
383 {
384  if ((NULL != set_subsystem) && (NULL == set_ego))
385  {
386  fprintf (stderr, "Option -s requires option -e to be specified as well.\n");
387  return;
388  }
389  sh = GNUNET_IDENTITY_connect (cfg,
390  (monitor | list) ||
391  (NULL != set_ego) ||
392  (NULL != set_subsystem)
393  ? &print_ego
394  : NULL,
395  NULL);
396  if (NULL != delete_ego)
397  delete_op =
399  delete_ego,
401  &delete_op);
402  if (NULL != create_ego)
403  {
404  if (NULL != privkey_ego)
405  {
407  strlen (privkey_ego),
408  &pk,
409  sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
410  create_op =
412  create_ego,
413  &pk,
415  &create_op);
416  }
417  else
418  create_op =
420  create_ego,
421  NULL,
423  &create_op);
424  }
426  NULL);
427  test_finished ();
428 }
429 
430 
438 int
439 main (int argc, char *const *argv)
440 {
441  struct GNUNET_GETOPT_CommandLineOption options[] = {
443  "create",
444  "NAME",
445  gettext_noop ("create ego NAME"),
446  &create_ego),
448  "delete",
449  "NAME",
450  gettext_noop ("delete ego NAME "),
451  &delete_ego),
453  "privkey",
454  "PRIVATE_KEY",
455  gettext_noop ("set the private key for the identity to PRIVATE_KEY (use together with -C)"),
456  &privkey_ego),
458  "display",
459  gettext_noop ("display all egos"),
460  &list),
462  "quiet",
463  gettext_noop ("reduce output"),
464  &quiet),
466  'e',
467  "ego",
468  "NAME",
469  gettext_noop (
470  "set default identity to NAME for a subsystem SUBSYSTEM (use together with -s) or restrict results to NAME (use together with -d)"),
471  &set_ego),
473  "monitor",
474  gettext_noop ("run in monitor mode egos"),
475  &monitor),
477  "private-keys",
478  gettext_noop ("display private keys as well"),
479  &private_keys),
481  's',
482  "set",
483  "SUBSYSTEM",
484  gettext_noop (
485  "set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
486  &set_subsystem),
489  };
490  int res;
491 
492  if (GNUNET_OK !=
493  GNUNET_STRINGS_get_utf8_args (argc, argv,
494  &argc, &argv))
495  return 4;
496  global_ret = TIMEOUT_STATUS_CODE; /* timeout */
497  res = GNUNET_PROGRAM_run (argc,
498  argv,
499  "gnunet-identity",
500  gettext_noop ("Maintain egos"),
501  options,
502  &run,
503  NULL);
504  GNUNET_free_nz ((void *) argv);
505 
506  if (GNUNET_OK != res)
507  return 3;
508  return global_ret;
509 }
510 
511 
512 /* end of gnunet-identity.c */
struct GNUNET_IDENTITY_Handle * GNUNET_IDENTITY_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_IDENTITY_Callback cb, void *cb_cls)
Connect to the identity service.
Definition: identity_api.c:566
static int global_ret
Value to return from main().
void GNUNET_IDENTITY_cancel(struct GNUNET_IDENTITY_Operation *op)
Cancel an identity operation.
Definition: identity_api.c:867
static char * set_ego
-s option.
static void shutdown_task(void *cls)
Task run on shutdown.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1331
struct GNUNET_CRYPTO_EcdsaPrivateKey pk
Private key from command line option, or NULL.
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_delete(struct GNUNET_IDENTITY_Handle *h, const char *name, GNUNET_IDENTITY_Continuation cb, void *cb_cls)
Delete an existing identity.
Definition: identity_api.c:826
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the &#39;-V&#39; verbosity option.
#define gettext(Msgid)
Definition: gettext.h:45
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1457
static void delete_finished(void *cls, const char *emsg)
Deletion operation finished.
static int private_keys
Was "private" specified?
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_set(struct GNUNET_IDENTITY_Handle *h, const char *service_name, struct GNUNET_IDENTITY_Ego *ego, GNUNET_IDENTITY_Continuation cont, void *cont_cls)
Set the preferred/default identity for a service.
Definition: identity_api.c:677
const struct GNUNET_CRYPTO_EcdsaPrivateKey * GNUNET_IDENTITY_ego_get_private_key(const struct GNUNET_IDENTITY_Ego *ego)
Obtain the ECC key associated with a ego.
Definition: identity_api.c:595
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_create(struct GNUNET_IDENTITY_Handle *h, const char *name, const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey, GNUNET_IDENTITY_CreateContinuation cont, void *cont_cls)
Create a new identity with the given name.
Definition: identity_api.c:724
static int monitor
Was "monitor" specified?
Definition of a command line option.
Private ECC key encoded for transmission.
static struct GNUNET_IDENTITY_Operation * set_op
Operation handle for set operation.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:531
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
static char * privkey_ego
-P option
Handle for an operation with the identity service.
Definition: identity_api.c:39
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
static void test_finished(void)
Test if we are finished yet.
Handle for an ego.
Definition: identity.h:245
Handle for the service.
Definition: identity_api.c:95
#define TIMEOUT_STATUS_CODE
Return value from main on timeout.
void GNUNET_IDENTITY_disconnect(struct GNUNET_IDENTITY_Handle *h)
Disconnect from identity service.
Definition: identity_api.c:884
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
static void set_done(void *cls, const char *emsg)
Function called by GNUNET_IDENTITY_set up on completion.
static int res
char * GNUNET_CRYPTO_ecdsa_private_key_to_string(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
Convert a private key to a string.
Definition: crypto_ecc.c:331
char * GNUNET_CRYPTO_ecdsa_public_key_to_string(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a public key to a string.
Definition: crypto_ecc.c:232
#define GNUNET_free_nz(ptr)
Wrapper around free.
static char * set_subsystem
-S option.
void GNUNET_CRYPTO_ecdsa_key_get_public(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:176
static int quiet
Was "quiet" specified?
static struct GNUNET_IDENTITY_Operation * create_op
Handle for create operation.
int main(int argc, char *const *argv)
The main function.
static unsigned int verbose
Was "verbose" specified?
configuration data
Definition: configuration.c:84
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
void * cls
Closure for cont or cb.
Definition: identity_api.c:88
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
static char * create_ego
-C option
static void create_finished(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk, const char *emsg)
Creation operation finished.
static struct GNUNET_IDENTITY_Operation * delete_op
Handle for delete operation.
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
static char * delete_ego
-D option
void GNUNET_IDENTITY_ego_get_public_key(struct GNUNET_IDENTITY_Ego *ego, struct GNUNET_CRYPTO_EcdsaPublicKey *pk)
Get the identifier (public key) of an ego.
Definition: identity_api.c:608
static int list
Was "list" specified?
int GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:970
#define GNUNET_free(ptr)
Wrapper around free.
static void print_ego(void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, const char *identifier)
If listing is enabled, prints information about the egos.
#define gettext_noop(String)
Definition: gettext.h:69