gwenhywfar  5.4.1
gcttool/main.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Tue May 03 2005
3  copyright : (C) 2005-2010 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 /*#define GCT_IS_EXPERIMENTAL
15 #define DEBUG_GCT_TOOL */
16 
17 #include <gwenhywfar/debug.h>
18 #include <gwenhywfar/pathmanager.h>
19 #include <gwenhywfar/cgui.h>
20 
21 #include "globals.h"
22 
23 #ifdef OS_WIN32
24 # define DIRSEP "\\"
25 #else
26 # define DIRSEP "/"
27 #endif
28 
29 #ifdef HAVE_I18N
30 # include <libintl.h>
31 # include <locale.h>
32 #endif
33 
34 
35 #ifdef HAVE_OPENSSL
36 # include <gwenhywfar/gui_be.h>
37 
38 # include <openssl/err.h>
39 # include <openssl/ssl.h>
40 # include <openssl/des.h>
41 #endif
42 
43 
44 
45 
46 #ifdef HAVE_OPENSSL
47 int getKeyDataFromTextOpenSSL(GWEN_GUI *gui,
48  const char *password,
49  unsigned char *buffer,
50  unsigned int bsize)
51 {
52  DES_cblock left, right;
53  int i;
54 
55  if (bsize!=16) {
56  DBG_ERROR(GWEN_LOGDOMAIN, "Buffer must be exact 16 bytes in length");
57  return -1;
58  }
59  DES_string_to_2keys(password, &left, &right);
60  for (i=0; i<8; i++)
61  *(buffer++)=left[i];
62  for (i=0; i<8; i++)
63  *(buffer++)=right[i];
64  return 0;
65 }
66 
67 #endif
68 
69 
70 GWEN_CRYPT_TOKEN *getCryptToken(const char *ttype,
71  const char *tname)
72 {
74  GWEN_PLUGIN *pl;
75  GWEN_CRYPT_TOKEN *ct;
76 
77  /* get crypt token */
79  if (pm==0) {
80  DBG_ERROR(0, "Plugin manager not found");
81  return NULL;
82  }
83 
84  pl=GWEN_PluginManager_GetPlugin(pm, ttype);
85  if (pl==0) {
86  DBG_ERROR(0, "Plugin not found");
87 
88  fprintf(stderr, "%s",
89  I18N("The plugin could not be found.\n"
90  "Please make sure that you have the following "
91  "packages installed (at least on Debian/Ubuntu):\n"
92  "- libchipcard-plugins-libgwenhywfar (for chipcards)\n"
93  "- libaqbanking-plugins-libgwenhywfar\n"));
94  return NULL;
95  }
96  DBG_INFO(0, "Plugin found");
97 
99  if (ct==0) {
100  DBG_ERROR(0, "Could not create crypt token");
101  return NULL;
102  }
103 
104  return ct;
105 }
106 
107 
108 
109 int main(int argc, char **argv)
110 {
111  GWEN_DB_NODE *db;
112  const char *cmd;
113  int rv;
114  int err;
115  GWEN_GUI *gui;
116  const GWEN_ARGS args[]= {
117  {
118  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
119  GWEN_ArgsType_Char, /* type */
120  "cfgfile", /* name */
121  0, /* minnum */
122  1, /* maxnum */
123  "C", /* short option */
124  "cfgfile", /* long option */
125  "Specify the configuration file", /* short description */
126  "Specify the configuration file" /* long description */
127  },
128  {
129  0, /* flags */
130  GWEN_ArgsType_Int, /* type */
131  "forcePin", /* name */
132  0, /* minnum */
133  1, /* maxnum */
134  0, /* short option */
135  "forcepin", /* long option */
136  "force pin entry", /* short description */
137  "force pin entry even if the error counter is not zero"
138  },
139  {
141  GWEN_ArgsType_Int, /* type */
142  "help", /* name */
143  0, /* minnum */
144  0, /* maxnum */
145  "h", /* short option */
146  "help", /* long option */
147  "Show this help screen", /* short description */
148  "Show this help screen" /* long description */
149  }
150  };
151 
152  err=GWEN_Init();
153  if (err) {
154  fprintf(stderr, "Could not initialize Gwenhywfar.\n");
155  return 2;
156  }
157 
158  gui=GWEN_Gui_CGui_new();
159  GWEN_Gui_SetGui(gui);
160 
161 #ifdef HAVE_OPENSSL
162  /* for conversion purposes */
163  SSL_load_error_strings();
164  SSL_library_init();
165  GWEN_Gui_SetKeyDataFromTextOpenSslFn(gui, getKeyDataFromTextOpenSSL);
166 #endif
167 
168  GWEN_Logger_Open(GCT_LOGDOMAIN, "gct-tool", 0,
171 
174 
175 #ifdef GCT_IS_EXPERIMENTAL
176  fprintf(stderr, "\n");
177  fprintf(stderr, "\n");
178  fprintf(stderr, "=================== WARNING ===================\n");
179  fprintf(stderr, "This tool is still EXPERIMENTAL !!!\n");
180  fprintf(stderr, "Please DON'T USE it with your daily key files !\n");
181  fprintf(stderr, "===============================================\n");
182  fprintf(stderr, "\n");
183  fprintf(stderr, "\n");
184 #endif
185 
186  db=GWEN_DB_Group_new("arguments");
187  rv=GWEN_Args_Check(argc, argv, 1,
190  args,
191  db);
192  if (rv==GWEN_ARGS_RESULT_ERROR) {
193  fprintf(stderr, "ERROR: Could not parse arguments main\n");
194  return -1;
195  }
196  else if (rv==GWEN_ARGS_RESULT_HELP) {
197  GWEN_BUFFER *ubuf;
198 
199  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
201  I18N("Usage: "));
202  GWEN_Buffer_AppendString(ubuf, argv[0]);
204  I18N(" [GLOBAL OPTIONS] COMMAND "
205  "[LOCAL OPTIONS]\n"));
207  I18N("\nGlobal Options:\n"));
208  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
209  fprintf(stderr, "ERROR: Could not create help string\n");
210  return 1;
211  }
213  I18N("\nCommands:\n\n"));
215  I18N(" create:\n"
216  " This command creates a crypt token"
217  "\n\n"));
219  I18N(" showuser:\n"
220  " Display user data stored on the "
221  "token\n\n"));
223  I18N(" showkey:\n"
224  " Display key info stored on the "
225  "token\n\n"));
227  I18N(" update:\n"
228  " Update Crypt Token to newer version (e.g. OpenHBCI key"
229  "files)\n\n"));
231  I18N(" showpasswords:\n"
232  " Display passwords store in a GWEN_PASSWD_STORE file\n\n"));
233 
234  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
235  GWEN_Buffer_free(ubuf);
236  return 0;
237  }
238  if (rv) {
239  argc-=rv-1;
240  argv+=rv-1;
241  }
242 
243  cmd=GWEN_DB_GetCharValue(db, "params", 0, 0);
244  if (!cmd) {
245  fprintf(stderr, "ERROR: Command needed.\n");
246  return 1;
247  }
248 
249  if (strcasecmp(cmd, "create")==0) {
250  rv=createToken(db, argc, argv);
251  }
252  else if (strcasecmp(cmd, "showuser")==0) {
253  rv=showUser(db, argc, argv);
254  }
255  else if (strcasecmp(cmd, "showkey")==0) {
256  rv=showKey(db, argc, argv);
257  }
258  else if (strcasecmp(cmd, "genkey")==0) {
259  rv=genKey(db, argc, argv);
260  }
261  else if (strcasecmp(cmd, "activatekey")==0) {
262  rv=activateKey(db, argc, argv);
263  }
264  else if (strcasecmp(cmd, "update")==0) {
265  rv=updateToken(db, argc, argv);
266  }
267  else if (strcasecmp(cmd, "setsignseq")==0) {
268  rv=setSignSeq(db, argc, argv);
269  }
270  else if (strcasecmp(cmd, "changepin")==0) {
271  rv=changePin(db, argc, argv);
272  }
273  else if (strcasecmp(cmd, "setkey")==0) {
274  rv=setKey(db, argc, argv);
275  }
276  else if (strcasecmp(cmd, "hashTree")==0) {
277  rv=hashTree(db, argc, argv);
278  }
279  else if (strcasecmp(cmd, "checkTree")==0) {
280  rv=checkTree(db, argc, argv);
281  }
282  else if (strcasecmp(cmd, "showpasswords")==0) {
283  rv=showPasswords(db, argc, argv);
284  }
285  else {
286  fprintf(stderr, "ERROR: Unknown command \"%s\".\n", cmd);
287  rv=1;
288  }
289 
290  err=GWEN_Fini();
291  if (err) {
292  fprintf(stderr,
293  "WARNING: Could not deinitialize Gwenhywfar.\n");
294  }
295 
296  return rv;
297 }
298 
299 
300 
struct GWEN_PLUGIN_MANAGER GWEN_PLUGIN_MANAGER
Definition: plugin.h:40
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
#define I18N(m)
Definition: error.c:42
int setSignSeq(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: setsignseq.c:29
#define GCT_LOGDOMAIN
Definition: cttest.c:47
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
struct GWEN_PLUGIN GWEN_PLUGIN
Definition: plugin.h:39
void GWEN_Logger_SetLevel(const char *logDomain, GWEN_LOGGER_LEVEL l)
Definition: logger.c:627
#define NULL
Definition: binreloc.c:300
GWEN_GUI_KEYDATAFROMTEXT_OPENSSL_FN GWEN_Gui_SetKeyDataFromTextOpenSslFn(GWEN_GUI *gui, GWEN_GUI_KEYDATAFROMTEXT_OPENSSL_FN f)
Definition: gui.c:685
int updateToken(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: update.c:29
GWEN_PLUGIN * GWEN_PluginManager_GetPlugin(GWEN_PLUGIN_MANAGER *pm, const char *s)
Definition: plugin.c:529
#define GWEN_LOGDOMAIN
Definition: logger.h:35
int showKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: showkey.c:29
GWEN_CRYPT_TOKEN * GWEN_Crypt_Token_Plugin_CreateToken(GWEN_PLUGIN *pl, const char *name)
Definition: ctplugin.c:142
#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
int hashTree(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: hashtree.c:27
GWEN_PLUGIN_MANAGER * GWEN_PluginManager_FindPluginManager(const char *s)
Definition: plugin.c:548
#define GWEN_ARGS_RESULT_HELP
Definition: src/base/args.h:58
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
int activateKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: activatekey.c:26
#define GWEN_ARGS_MODE_STOP_AT_FREEPARAM
Definition: src/base/args.h:55
int showUser(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: showuser.c:27
int setKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: setkey.c:29
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
#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:969
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_Fini(void)
Definition: gwenhywfar.c:298
struct GWEN_CRYPT_TOKEN GWEN_CRYPT_TOKEN
Definition: ct.h:19
#define GWEN_ARGS_FLAGS_LAST
Definition: src/base/args.h:51
int GWEN_Logger_Open(const char *logDomain, const char *ident, const char *file, GWEN_LOGGER_LOGTYPE logtype, GWEN_LOGGER_FACILITY facility)
Definition: logger.c:225
int changePin(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: changepin.c:27
int genKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: genkey.c:26
int main(int argc, char **argv)
Definition: gcttool/main.c:109
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
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
int checkTree(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: checktree.c:29
int createToken(GWEN_DB_NODE *dbArgs, int argc, char **argv)
GWEN_CRYPT_TOKEN * getCryptToken(const char *ttype, const char *tname)
Definition: gcttool/main.c:70
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:178
struct GWEN_GUI GWEN_GUI
Definition: gui.h:176
GWEN_DB_NODE * GWEN_DB_Group_new(const char *name)
Definition: db.c:171
void GWEN_Gui_SetGui(GWEN_GUI *gui)
Definition: gui.c:156
int showPasswords(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: showpasswords.c:30
int GWEN_Init(void)
Definition: gwenhywfar.c:91
GWEN_GUI * GWEN_Gui_CGui_new(void)
Definition: cgui.c:74
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989