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 int type_eddsa;
72 
76 static char *create_ego;
77 
81 static char *delete_ego;
82 
86 static char *privkey_ego;
87 
91 static char *set_ego;
92 
96 static char *set_subsystem;
97 
102 
107 
112 
117 
121 static int global_ret;
122 
123 
129 static void
130 shutdown_task (void *cls)
131 {
132  if (NULL != set_op)
133  {
134  GNUNET_IDENTITY_cancel (set_op);
135  set_op = NULL;
136  }
137  if (NULL != create_op)
138  {
139  GNUNET_IDENTITY_cancel (create_op);
140  create_op = NULL;
141  }
142  if (NULL != delete_op)
143  {
144  GNUNET_IDENTITY_cancel (delete_op);
145  delete_op = NULL;
146  }
147  if (NULL != set_ego)
148  {
150  set_ego = NULL;
151  }
153  sh = NULL;
154 }
155 
156 
160 static void
162 {
163  if ( (NULL == create_op) &&
164  (NULL == delete_op) &&
165  (NULL == set_op) &&
166  (NULL == set_subsystem) &&
167  (! list) &&
168  (! monitor))
169  {
171  global_ret = 0;
173  }
174 }
175 
176 
183 static void
184 delete_finished (void *cls,
185  const char *emsg)
186 {
187  struct GNUNET_IDENTITY_Operation **op = cls;
188 
189  *op = NULL;
190  if (NULL != emsg)
191  fprintf (stderr, "%s\n", gettext (emsg));
192  test_finished ();
193 }
194 
195 
203 static void
205  const struct GNUNET_IDENTITY_PrivateKey *pk,
206  const char *emsg)
207 {
208  struct GNUNET_IDENTITY_Operation **op = cls;
209 
210  *op = NULL;
211  if (NULL == pk)
212  {
213  fprintf (stderr,
214  _ ("Failed to create ego: %s\n"),
215  emsg);
216  global_ret = 1;
217  }
218  else if (verbose)
219  {
220  struct GNUNET_IDENTITY_PublicKey pub;
221  char *pubs;
222 
225  if (private_keys)
226  {
227  char *privs;
228 
230  fprintf (stdout, "%s - %s\n", pubs, privs);
231  GNUNET_free (privs);
232  }
233  else
234  {
235  fprintf (stdout, "%s\n", pubs);
236  }
237  GNUNET_free (pubs);
238  }
239  test_finished ();
240 }
241 
242 
249 static void
250 set_done (void *cls, const char *emsg)
251 {
252  set_op = NULL;
253  if (NULL != emsg)
254  {
255  fprintf (stderr, _ ("Failed to set default ego: %s\n"), emsg);
256  global_ret = 1;
257  }
258  test_finished ();
259 }
260 
261 
294 static void
295 print_ego (void *cls,
296  struct GNUNET_IDENTITY_Ego *ego,
297  void **ctx,
298  const char *identifier)
299 {
300  struct GNUNET_IDENTITY_PublicKey pk;
301  char *s;
302  char *privs;
303 
304  if ( (NULL != set_ego) &&
305  (NULL != set_subsystem) &&
306  (NULL != ego) &&
307  (NULL != identifier) &&
308  (0 == strcmp (identifier, set_ego)))
309  {
310  set_op = GNUNET_IDENTITY_set (sh,
312  ego,
313  &set_done,
314  NULL);
316  set_subsystem = NULL;
318  set_ego = NULL;
319  }
320  if ( (NULL == ego) &&
321  (NULL != set_ego) &&
322  (NULL != set_subsystem) )
323  {
324  fprintf (stderr,
325  "Could not set ego to `%s' for subsystem `%s', ego not known\n",
326  set_ego,
327  set_subsystem);
329  set_subsystem = NULL;
331  set_ego = NULL;
332  }
333  if ((NULL == ego) && (! monitor))
334  {
335  list = 0;
336  test_finished ();
337  return;
338  }
339  if (! (list | monitor))
340  return;
341  if ( (NULL == ego) ||
342  (NULL == identifier) )
343  return;
344  if ( (NULL != set_ego) &&
345  (0 != strcmp (identifier,
346  set_ego)) )
347  return;
352  if ((monitor) || (NULL != identifier))
353  {
354  if (quiet)
355  {
356  if (private_keys)
357  fprintf (stdout, "%s - %s\n", s, privs);
358  else
359  fprintf (stdout, "%s\n", s);
360  }
361  else
362  {
363  if (private_keys)
364  fprintf (stdout, "%s - %s - %s - %s\n",
365  identifier, s, privs,
366  (ntohl (pk.type) == GNUNET_IDENTITY_TYPE_ECDSA) ?
367  "ECDSA" : "EdDSA");
368  else
369  fprintf (stdout, "%s - %s - %s\n",
370  identifier, s,
371  (ntohl (pk.type) == GNUNET_IDENTITY_TYPE_ECDSA) ?
372  "ECDSA" : "EdDSA");
373 
374  }
375  }
376  GNUNET_free (privs);
377  GNUNET_free (s);
378 }
379 
380 
389 static void
390 run (void *cls,
391  char *const *args,
392  const char *cfgfile,
393  const struct GNUNET_CONFIGURATION_Handle *cfg)
394 {
395  if ((NULL != set_subsystem) && (NULL == set_ego))
396  {
397  fprintf (stderr, "Option -s requires option -e to be specified as well.\n");
398  return;
399  }
400  sh = GNUNET_IDENTITY_connect (cfg,
401  (monitor | list) ||
402  (NULL != set_ego) ||
403  (NULL != set_subsystem)
404  ? &print_ego
405  : NULL,
406  NULL);
407  if (NULL != delete_ego)
408  delete_op =
410  delete_ego,
412  &delete_op);
413  if (NULL != create_ego)
414  {
415  if (NULL != privkey_ego)
416  {
418  strlen (privkey_ego),
419  &pk,
420  sizeof(struct
422  create_op =
424  create_ego,
425  &pk,
426  0, // Ignored
428  &create_op);
429  }
430  else
431  create_op =
433  create_ego,
434  NULL,
435  (type_eddsa) ?
439  &create_op);
440  }
442  NULL);
443  test_finished ();
444 }
445 
446 
454 int
455 main (int argc, char *const *argv)
456 {
457  struct GNUNET_GETOPT_CommandLineOption options[] = {
459  "create",
460  "NAME",
461  gettext_noop ("create ego NAME"),
462  &create_ego),
464  "delete",
465  "NAME",
466  gettext_noop ("delete ego NAME "),
467  &delete_ego),
469  "privkey",
470  "PRIVATE_KEY",
471  gettext_noop (
472  "set the private key for the identity to PRIVATE_KEY (use together with -C)"),
473  &privkey_ego),
475  "eddsa",
476  gettext_noop (
477  "generate an EdDSA identity. (use together with -C) EXPERIMENTAL"),
478  &type_eddsa),
480  "display",
481  gettext_noop ("display all egos"),
482  &list),
484  "quiet",
485  gettext_noop ("reduce output"),
486  &quiet),
488  'e',
489  "ego",
490  "NAME",
491  gettext_noop (
492  "set default identity to NAME for a subsystem SUBSYSTEM (use together with -s) or restrict results to NAME (use together with -d)"),
493  &set_ego),
495  "monitor",
496  gettext_noop ("run in monitor mode egos"),
497  &monitor),
499  "private-keys",
500  gettext_noop ("display private keys as well"),
501  &private_keys),
503  's',
504  "set",
505  "SUBSYSTEM",
506  gettext_noop (
507  "set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
508  &set_subsystem),
511  };
512  int res;
513 
514  if (GNUNET_OK !=
515  GNUNET_STRINGS_get_utf8_args (argc, argv,
516  &argc, &argv))
517  return 4;
518  global_ret = TIMEOUT_STATUS_CODE; /* timeout */
519  res = GNUNET_PROGRAM_run (argc,
520  argv,
521  "gnunet-identity",
522  gettext_noop ("Maintain egos"),
523  options,
524  &run,
525  NULL);
526  GNUNET_free_nz ((void *) argv);
527 
528  if (GNUNET_OK != res)
529  return 3;
530  return global_ret;
531 }
532 
533 
534 /* 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:610
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:904
static char * set_ego
-s option.
enum GNUNET_GenericReturnValue 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:363
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
static void create_finished(void *cls, const struct GNUNET_IDENTITY_PrivateKey *pk, const char *emsg)
Creation operation finished.
uint32_t type
Type of public key.
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_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:863
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
char * GNUNET_IDENTITY_public_key_to_string(const struct GNUNET_IDENTITY_PublicKey *key)
Creates a (Base32) string representation of the public key.
static void delete_finished(void *cls, const char *emsg)
Deletion operation finished.
static int private_keys
Was "private" specified?
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1209
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:720
A private key for an identity as per LSD0001.
static int monitor
Was "monitor" specified?
Definition of a command line option.
static struct GNUNET_IDENTITY_Operation * set_op
Operation handle for set operation.
void GNUNET_IDENTITY_ego_get_public_key(struct GNUNET_IDENTITY_Ego *ego, struct GNUNET_IDENTITY_PublicKey *pk)
Get the identifier (public key) of an ego.
Definition: identity_api.c:652
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:178
char * GNUNET_IDENTITY_private_key_to_string(const struct GNUNET_IDENTITY_PrivateKey *key)
Creates a (Base32) string representation of the private key.
static void test_finished(void)
Test if we are finished yet.
Handle for an ego.
Definition: identity.h:36
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_create(struct GNUNET_IDENTITY_Handle *h, const char *name, const struct GNUNET_IDENTITY_PrivateKey *privkey, enum GNUNET_IDENTITY_KeyType ktype, GNUNET_IDENTITY_CreateContinuation cont, void *cont_cls)
Create a new ego with the given name.
Definition: identity_api.c:757
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:921
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_key_get_public(const struct GNUNET_IDENTITY_PrivateKey *privkey, struct GNUNET_IDENTITY_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: identity_api.c:175
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
#define GNUNET_free_nz(ptr)
Wrapper around free.
static char * set_subsystem
-S option.
enum GNUNET_GenericReturnValue 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:775
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?
An identity key as per LSD0001.
configuration data
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
const struct GNUNET_IDENTITY_PrivateKey * GNUNET_IDENTITY_ego_get_private_key(const struct GNUNET_IDENTITY_Ego *ego)
Obtain the ECC key associated with a ego.
Definition: identity_api.c:639
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.
static int type_eddsa
Was "eddsa" specified?
static char * create_ego
-C option
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
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