gwenhywfar  5.10.1
showpasswords.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Oct 10 2016
3  copyright : (C) 2016 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 #include "globals.h"
15 
16 #include <gwenhywfar/debug.h>
17 //#include <gwenhywfar/ct.h>
18 //#include <gwenhywfar/ctplugin.h>
19 //#include <gwenhywfar/text.h>
20 #include <gwenhywfar/passwdstore.h>
21 
22 
23 
24 
25 /*
26  * Show passwords from a GWEN_PASSWD_STORE file.
27  */
28 
29 
30 int showPasswords(GWEN_DB_NODE *dbArgs, int argc, char **argv)
31 {
32  GWEN_DB_NODE *db;
33  const char *file;
34  GWEN_PASSWD_STORE *sto;
35  GWEN_STRINGLIST *sl;
37  int rv;
38  const GWEN_ARGS args[]= {
39  {
40  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
41  GWEN_ArgsType_Char, /* type */
42  "file", /* name */
43  1, /* minnum */
44  1, /* maxnum */
45  "f", /* short option */
46  "file", /* long option */
47  "Specify the password file", /* short description */
48  "Specify the password file" /* long description */
49  },
50  {
52  GWEN_ArgsType_Int, /* type */
53  "help", /* name */
54  0, /* minnum */
55  0, /* maxnum */
56  "h", /* short option */
57  "help", /* long option */
58  "Show this help screen", /* short description */
59  "Show this help screen" /* long description */
60  }
61  };
62 
63  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
64  rv=GWEN_Args_Check(argc, argv, 1,
66  args,
67  db);
68  if (rv==GWEN_ARGS_RESULT_ERROR) {
69  fprintf(stderr, "ERROR: Could not parse arguments\n");
70  return 1;
71  }
72  else if (rv==GWEN_ARGS_RESULT_HELP) {
73  GWEN_BUFFER *ubuf;
74 
75  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
76  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
77  fprintf(stderr, "ERROR: Could not create help string\n");
78  return 1;
79  }
80  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
81  GWEN_Buffer_free(ubuf);
82  return 0;
83  }
84 
85  file=GWEN_DB_GetCharValue(db, "file", 0, NULL);
86  assert(file);
87 
88  sto=GWEN_PasswordStore_new(file);
90 
92  if (rv<0) {
93  fprintf(stderr, "ERROR: Could not get token list: %d\n", rv);
95  return 3;
96  }
97 
99  while (se) {
100  const char *s;
101 
103  if (s && *s) {
104  char passwd[512];
105 
106  rv=GWEN_PasswordStore_GetPassword(sto, s, passwd, 1, sizeof(passwd)-1);
107  if (rv<0) {
108  fprintf(stderr, "ERROR: Could not get password for token \"%s\": %d\n", s, rv);
110  return 3;
111  }
112  passwd[sizeof(passwd)-1]=0;
113  fprintf(stdout, "\"%s\" -> \"%s\"\n", s, passwd);
114  memset(passwd, 0, sizeof(passwd));
115  }
117  }
119 
121 
122  return 0;
123 }
124 
125 
126 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
void GWEN_PasswordStore_free(GWEN_PASSWD_STORE *sto)
Definition: passwdstore.c:60
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:53
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define NULL
Definition: binreloc.c:300
GWEN_PASSWD_STORE * GWEN_PasswordStore_new(const char *fname)
Definition: passwdstore.c:46
int GWEN_PasswordStore_GetPassword(GWEN_PASSWD_STORE *sto, const char *token, char *buffer, int minLen, int maxLen)
Definition: passwdstore.c:695
#define GWEN_ARGS_FLAGS_HELP
Definition: src/base/args.h:52
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:390
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:406
#define GWEN_ARGS_RESULT_HELP
Definition: src/base/args.h:58
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:62
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
#define GWEN_ARGS_MODE_ALLOW_FREEPARAM
Definition: src/base/args.h:54
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition: db.c:971
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1381
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
int GWEN_PasswordStore_GetTokenList(GWEN_PASSWD_STORE *sto, GWEN_STRINGLIST *sl)
Definition: passwdstore.c:744
#define GWEN_ARGS_FLAGS_LAST
Definition: src/base/args.h:51
int showPasswords(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: showpasswords.c:30
int GWEN_Args_Check(int argc, char **argv, int startAt, uint32_t mode, const GWEN_ARGS *args, GWEN_DB_NODE *db)
Definition: src/base/args.c:45
struct GWEN_PASSWD_STORE GWEN_PASSWD_STORE
Definition: passwdstore.h:37
GWEN_STRINGLISTENTRY * GWEN_StringListEntry_Next(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:398
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:50
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168