gwenhywfar  4.99.8beta
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  GWEN_DB_NODE *db;
32  const char *file;
33  GWEN_PASSWD_STORE *sto;
34  GWEN_STRINGLIST *sl;
36  int rv;
37  const GWEN_ARGS args[]={
38  {
39  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
40  GWEN_ArgsType_Char, /* type */
41  "file", /* name */
42  1, /* minnum */
43  1, /* maxnum */
44  "f", /* short option */
45  "file", /* long option */
46  "Specify the password file", /* short description */
47  "Specify the password file" /* long description */
48  },
49  {
51  GWEN_ArgsType_Int, /* type */
52  "help", /* name */
53  0, /* minnum */
54  0, /* maxnum */
55  "h", /* short option */
56  "help", /* long option */
57  "Show this help screen", /* short description */
58  "Show this help screen" /* long description */
59  }
60  };
61 
62  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
63  rv=GWEN_Args_Check(argc, argv, 1,
65  args,
66  db);
67  if (rv==GWEN_ARGS_RESULT_ERROR) {
68  fprintf(stderr, "ERROR: Could not parse arguments\n");
69  return 1;
70  }
71  else if (rv==GWEN_ARGS_RESULT_HELP) {
72  GWEN_BUFFER *ubuf;
73 
74  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
75  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
76  fprintf(stderr, "ERROR: Could not create help string\n");
77  return 1;
78  }
79  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
80  GWEN_Buffer_free(ubuf);
81  return 0;
82  }
83 
84  file=GWEN_DB_GetCharValue(db, "file", 0, NULL);
85  assert(file);
86 
87  sto=GWEN_PasswordStore_new(file);
89 
91  if (rv<0) {
92  fprintf(stderr, "ERROR: Could not get token list: %d\n", rv);
94  return 3;
95  }
96 
98  while(se) {
99  const char *s;
100 
102  if (s && *s) {
103  char passwd[512];
104 
105  rv=GWEN_PasswordStore_GetPassword(sto, s, passwd, 1, sizeof(passwd)-1);
106  if (rv<0) {
107  fprintf(stderr, "ERROR: Could not get password for token \"%s\": %d\n", s, rv);
109  return 3;
110  }
111  passwd[sizeof(passwd)-1]=0;
112  fprintf(stdout, "\"%s\" -> \"%s\"\n", s, passwd);
113  memset(passwd, 0, sizeof(passwd));
114  }
116  }
118 
120 
121  return 0;
122 }
123 
124 
125 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
void GWEN_PasswordStore_free(GWEN_PASSWD_STORE *sto)
Definition: passwdstore.c:59
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:51
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define NULL
Definition: binreloc.c:290
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:678
#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:38
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:352
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:366
#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:57
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:54
#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:897
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1260
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:83
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
int GWEN_PasswordStore_GetTokenList(GWEN_PASSWD_STORE *sto, GWEN_STRINGLIST *sl)
Definition: passwdstore.c:726
#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:359
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:46
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168