GNUnet  0.10.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 *set_ego;
82 
86 static char *set_subsystem;
87 
92 
97 
102 
106 static int global_ret;
107 
108 
114 static void
116 {
117  if (NULL != set_op)
118  {
119  GNUNET_IDENTITY_cancel (set_op);
120  set_op = NULL;
121  }
122  if (NULL != create_op)
123  {
124  GNUNET_IDENTITY_cancel (create_op);
125  create_op = NULL;
126  }
127  if (NULL != delete_op)
128  {
129  GNUNET_IDENTITY_cancel (delete_op);
130  delete_op = NULL;
131  }
132  if (NULL != set_ego)
133  {
135  set_ego = NULL;
136  }
138  sh = NULL;
139 }
140 
141 
145 static void
147 {
148  if ((NULL == create_op) && (NULL == delete_op) && (NULL == set_op) &&
149  (NULL == set_subsystem) && (! list) && (! monitor))
150  {
152  global_ret = 0;
154  }
155 }
156 
157 
164 static void
165 delete_finished (void *cls, const char *emsg)
166 {
167  struct GNUNET_IDENTITY_Operation **op = cls;
168 
169  *op = NULL;
170  if (NULL != emsg)
171  fprintf (stderr, "%s\n", gettext (emsg));
172  test_finished ();
173 }
174 
175 
183 static void
185  const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
186  const char *emsg)
187 {
188  struct GNUNET_IDENTITY_Operation **op = cls;
189 
190  *op = NULL;
191  if (NULL == pk)
192  {
193  fprintf (stderr, _ ("Failed to create ego: %s\n"), emsg);
194  global_ret = 1;
195  }
196  else if (verbose)
197  {
198  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
199  char *pubs;
200 
203  if (private_keys)
204  {
205  char *privs;
206 
208  fprintf (stdout, "%s - %s\n", pubs, privs);
209  GNUNET_free (privs);
210  }
211  else
212  {
213  fprintf (stdout, "%s\n", pubs);
214  }
215  GNUNET_free (pubs);
216  }
217  test_finished ();
218 }
219 
220 
227 static void
228 set_done (void *cls, const char *emsg)
229 {
230  set_op = NULL;
231  if (NULL != emsg)
232  {
233  fprintf (stderr, _ ("Failed to set default ego: %s\n"), emsg);
234  global_ret = 1;
235  }
236  test_finished ();
237 }
238 
239 
273 static void
274 print_ego (void *cls,
275  struct GNUNET_IDENTITY_Ego *ego,
276  void **ctx,
277  const char *identifier)
278 {
280  char *s;
281  char *privs;
282 
283  if ((NULL != set_ego) && (NULL != set_subsystem) && (NULL != ego) &&
284  (NULL != identifier) && (0 == strcmp (identifier, set_ego)))
285  {
286  set_op = GNUNET_IDENTITY_set (sh, set_subsystem, ego, &set_done, NULL);
288  set_subsystem = NULL;
290  set_ego = NULL;
291  }
292  if ((NULL == ego) && (NULL != set_ego) && (NULL != set_subsystem))
293  {
294  fprintf (stderr,
295  "Could not set ego to `%s' for subsystem `%s', ego not known\n",
296  set_ego,
297  set_subsystem);
299  set_subsystem = NULL;
301  set_ego = NULL;
302  }
303  if ((NULL == ego) && (! monitor))
304  {
305  list = 0;
306  test_finished ();
307  return;
308  }
309  if (! (list | monitor))
310  return;
311  if ((NULL == ego) || (NULL == identifier))
312  return;
313  if ((NULL != set_ego) && (0 != strcmp (identifier, set_ego)))
314  return;
319  if ((monitor) || (NULL != identifier))
320  {
321  if (quiet)
322  {
323  if (private_keys)
324  fprintf (stdout, "%s - %s\n", s, privs);
325  else
326  fprintf (stdout, "%s\n", s);
327  }
328  else
329  {
330  if (private_keys)
331  fprintf (stdout, "%s - %s - %s\n", identifier, s, privs);
332  else
333  fprintf (stdout, "%s - %s\n", identifier, s);
334  }
335  }
336  GNUNET_free (privs);
337  GNUNET_free (s);
338 }
339 
340 
349 static void
350 run (void *cls,
351  char *const *args,
352  const char *cfgfile,
353  const struct GNUNET_CONFIGURATION_Handle *cfg)
354 {
355  if ((NULL != set_subsystem) && (NULL == set_ego))
356  {
357  fprintf (stderr, "Option -s requires option -e to be specified as well.\n");
358  return;
359  }
360  sh = GNUNET_IDENTITY_connect (cfg,
361  (monitor | list) || (NULL != set_ego) ||
362  (NULL != set_subsystem)
363  ? &print_ego
364  : NULL,
365  NULL);
366  if (NULL != delete_ego)
367  delete_op =
369  if (NULL != create_ego)
370  create_op =
373  test_finished ();
374 }
375 
376 
384 int
385 main (int argc, char *const *argv)
386 {
387  struct GNUNET_GETOPT_CommandLineOption options[] =
389  "create",
390  "NAME",
391  gettext_noop ("create ego NAME"),
392  &create_ego),
394  "delete",
395  "NAME",
396  gettext_noop ("delete ego NAME "),
397  &delete_ego),
399  "display",
400  gettext_noop ("display all egos"),
401  &list),
403  "quiet",
404  gettext_noop ("reduce output"),
405  &quiet),
407  'e',
408  "ego",
409  "NAME",
410  gettext_noop (
411  "set default identity to NAME for a subsystem SUBSYSTEM (use together with -s) or restrict results to NAME (use together with -d)"),
412  &set_ego),
414  "monitor",
415  gettext_noop ("run in monitor mode egos"),
416  &monitor),
418  "private-keys",
419  gettext_noop ("display private keys as well"),
420  &private_keys),
422  's',
423  "set",
424  "SUBSYSTEM",
425  gettext_noop (
426  "set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
427  &set_subsystem),
430  int res;
431 
432  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
433  return 4;
434  global_ret = TIMEOUT_STATUS_CODE; /* timeout */
435  res = GNUNET_PROGRAM_run (argc,
436  argv,
437  "gnunet-identity",
438  gettext_noop ("Maintain egos"),
439  options,
440  &run,
441  NULL);
442  GNUNET_free ((void *) argv);
443 
444  if (GNUNET_OK != res)
445  return 3;
446  return global_ret;
447 }
448 
449 /* 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:528
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:819
static char * set_ego
-s option.
static struct GNUNET_CRYPTO_EddsaPrivateKey * pk
Private key of this peer.
static void shutdown_task(void *cls)
Task run on shutdown.
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:1293
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:778
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
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_create(struct GNUNET_IDENTITY_Handle *h, const char *name, GNUNET_IDENTITY_CreateContinuation cont, void *cont_cls)
Create a new identity with the given name.
Definition: identity_api.c:678
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
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:632
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:556
static int monitor
Was "monitor" specified?
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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:524
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.
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:208
Handle for an ego.
Definition: identity.h:245
Handle for the service.
Definition: identity_api.c:96
#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:838
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:433
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:334
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
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:241
static void test_finished()
Test if we are finished yet.
void GNUNET_IDENTITY_ego_get_public_key(const struct GNUNET_IDENTITY_Ego *ego, struct GNUNET_CRYPTO_EcdsaPublicKey *pk)
Get the identifier (public key) of an ego.
Definition: identity_api.c:569
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:85
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:89
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:361
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:139
static char * delete_ego
-D option
static int list
Was "list" specified?
#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