gwenhywfar  5.4.1
ctplugin.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Wed Mar 16 2005
3  copyright : (C) 2005 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 DISABLE_DEBUGLOG
15 
16 
17 #include "ctplugin_p.h"
18 #include "i18n_l.h"
19 
20 #include <gwenhywfar/gwenhywfar.h>
21 #include <gwenhywfar/misc.h>
22 #include <gwenhywfar/debug.h>
23 #include <gwenhywfar/gui.h>
24 #include <gwenhywfar/pathmanager.h>
25 
26 
27 #ifdef OS_WIN32
28 # define DIRSEP "\\"
29 #else
30 # define DIRSEP "/"
31 #endif
32 
33 
34 
35 GWEN_INHERIT(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN)
36 
37 
38 
39 
41 {
43  int err;
44  GWEN_STRINGLIST *sl;
45 
48  if (err) {
49  DBG_ERROR(GWEN_LOGDOMAIN, "Could not register CryptToken plugin manager");
50  return err;
51  }
52 
53  /* create plugin paths */
55  if (sl) {
57  GWEN_BUFFER *pbuf;
58 
59  pbuf=GWEN_Buffer_new(0, 256, 0, 1);
60 
62  while (se) {
65  DBG_INFO(GWEN_LOGDOMAIN, "Adding plugin path [%s]",
66  GWEN_Buffer_GetStart(pbuf));
68  GWEN_Buffer_GetStart(pbuf));
69  GWEN_Buffer_Reset(pbuf);
71  }
72  GWEN_Buffer_free(pbuf);
74  }
75 
76  return 0;
77 }
78 
79 
80 
82 {
84 
86  if (pm) {
87  int rv;
88 
90  if (rv) {
92  "Could not unregister CryptToken plugin manager (%d)", rv);
93  return rv;
94  }
95  else
97  }
98 
99  return 0;
100 }
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
113  GWEN_CRYPT_TOKEN_DEVICE devType,
114  const char *typeName,
115  const char *fileName)
116 {
117  GWEN_PLUGIN *pl;
118  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
119 
120  pl=GWEN_Plugin_new(mgr, typeName, fileName);
121  GWEN_NEW_OBJECT(GWEN_CRYPT_TOKEN_PLUGIN, xpl);
122  GWEN_INHERIT_SETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl, xpl, GWEN_Crypt_Token_Plugin_FreeData);
123  xpl->devType=devType;
124 
125  return pl;
126 }
127 
128 
129 
132 {
133  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
134 
135  xpl=(GWEN_CRYPT_TOKEN_PLUGIN *)p;
136 
137  GWEN_FREE_OBJECT(xpl);
138 }
139 
140 
141 
143 {
144  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
145 
146  assert(pl);
147  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
148  assert(xpl);
149 
150  if (xpl->createTokenFn)
151  return xpl->createTokenFn(pl, name);
152  else {
153  DBG_WARN(GWEN_LOGDOMAIN, "No createTokenFn");
154  return NULL;
155  }
156 }
157 
158 
159 
161 {
162  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
163 
164  assert(pl);
165  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
166  assert(xpl);
167 
168  if (xpl->checkTokenFn)
169  return xpl->checkTokenFn(pl, name);
170  else {
171  DBG_INFO(GWEN_LOGDOMAIN, "No checkTokenFn");
173  }
174 }
175 
176 
177 
179 {
180  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
181 
182  assert(pl);
183  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
184  assert(xpl);
185 
186  return xpl->devType;
187 }
188 
189 
190 
193 {
194  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
196 
197  assert(pl);
198  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
199  assert(xpl);
200 
201  of=xpl->createTokenFn;
202  xpl->createTokenFn=fn;
203 
204  return of;
205 }
206 
207 
208 
211 {
212  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
214 
215  assert(pl);
216  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
217  assert(xpl);
218 
219  of=xpl->checkTokenFn;
220  xpl->checkTokenFn=fn;
221 
222  return of;
223 }
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
236 {
238 
240  if (pl1) {
243 
246  if (pit) {
248  const char *ts;
249 
250  if (devt==GWEN_Crypt_Token_Device_Any)
251  ts=NULL;
252  else
255  while (pd) {
256  GWEN_XMLNODE *node;
257  const char *nts;
258  int match=0;
259 
261  assert(node);
262  nts=GWEN_XMLNode_GetProperty(node, "device", 0);
263  if (nts) {
264  if (!ts || (ts && strcasecmp(ts, nts)==0))
265  match=1;
266  }
267  else if (!ts)
268  match=1;
269 
270  if (match) {
272 
275  }
276 
278  }
280  }
282 
286  "No matching plugin descriptions for the given device type");
287  return NULL;
288  }
289  return pl2;
290  }
291  else {
292  DBG_ERROR(GWEN_LOGDOMAIN, "No plugin descriptions at all");
293  }
294  return NULL;
295 }
296 
297 
298 
301  GWEN_BUFFER *typeName,
302  GWEN_BUFFER *tokenName,
303  uint32_t guiid)
304 {
306 
307  assert(pm);
308 
310  if (pdl==NULL) {
311  DBG_ERROR(GWEN_LOGDOMAIN, "No plugin descriptions found for this device type");
312  GWEN_Gui_ProgressLog(guiid,
314  I18N("No plugin found for this device type"));
315  GWEN_Gui_ProgressLog(guiid,
317  I18N("If you're using a Debian/Ubuntu based system "
318  "please consider to install package "
319  LIBCHIPCARD_GWENHYWFAR_PLUGIN_PACKAGE));
320  return GWEN_ERROR_NOT_FOUND;
321  }
322  else {
324 
326  if (pit) {
328  uint32_t progressId;
329  unsigned int pdcount;
330  unsigned int cnt=0;
331 
339  I18N("Determining plugin module..."),
340  NULL,
341  pdcount,
342  guiid);
343 
345  assert(pd);
346  while (pd) {
347  GWEN_XMLNODE *n;
348  int err;
349  GWEN_PLUGIN *pl;
350  char logbuffer[256];
351 
353  assert(n);
354 
355  snprintf(logbuffer, sizeof(logbuffer)-1,
356  I18N("Loading plugin \"%s\""),
358  logbuffer[sizeof(logbuffer)-1]=0;
359  GWEN_Gui_ProgressLog(progressId,
361  logbuffer);
362 
363  /* device type matches, check this plugin */
365  if (pl) {
366  GWEN_BUFFER *lTokenName;
367  int rv;
368 
369  lTokenName=GWEN_Buffer_dup(tokenName);
370 
371  snprintf(logbuffer, sizeof(logbuffer)-1,
372  I18N("Checking plugin \"%s\""),
373  GWEN_Plugin_GetName(pl));
374  logbuffer[sizeof(logbuffer)-1]=0;
375  GWEN_Gui_ProgressLog(progressId,
377  logbuffer);
378 
380  "Checking plugin \"%s\" for [%s]",
382  GWEN_Buffer_GetStart(lTokenName));
383 
384  rv=GWEN_Crypt_Token_Plugin_CheckToken(pl, lTokenName);
385  switch (rv) {
386  case 0:
387  /* responsive plugin found */
388  snprintf(logbuffer, sizeof(logbuffer)-1,
389  I18N("Plugin \"%s\" supports this token"),
390  GWEN_Plugin_GetName(pl));
391  logbuffer[sizeof(logbuffer)-1]=0;
392  err=GWEN_Gui_ProgressLog(progressId,
394  logbuffer);
395  if (err==GWEN_ERROR_USER_ABORTED) {
396  GWEN_Gui_ProgressEnd(progressId);
397  GWEN_Buffer_free(lTokenName);
400  return err;
401  }
402 
403  GWEN_Buffer_Reset(typeName);
405  GWEN_Buffer_Reset(tokenName);
406  GWEN_Buffer_AppendBuffer(tokenName, lTokenName);
407  GWEN_Buffer_free(lTokenName);
410  GWEN_Gui_ProgressEnd(progressId);
411  return 0;
412 
414  snprintf(logbuffer, sizeof(logbuffer)-1,
415  I18N("Plugin \"%s\": Function not implemented"),
416  GWEN_Plugin_GetName(pl));
417  logbuffer[sizeof(logbuffer)-1]=0;
418  GWEN_Gui_ProgressLog(progressId,
420  logbuffer);
421  break;
422 
424  snprintf(logbuffer, sizeof(logbuffer)-1,
425  I18N("Plugin \"%s\" does not support this token"),
426  GWEN_Plugin_GetName(pl));
427  logbuffer[sizeof(logbuffer)-1]=0;
428  GWEN_Gui_ProgressLog(progressId,
430  logbuffer);
431  break;
432 
433  case GWEN_ERROR_BAD_NAME:
434  snprintf(logbuffer, sizeof(logbuffer)-1,
435  I18N("Plugin \"%s\" supports this token, but the name "
436  "did not match"),
437  GWEN_Plugin_GetName(pl));
438  logbuffer[sizeof(logbuffer)-1]=0;
439  GWEN_Gui_ProgressLog(progressId,
441  logbuffer);
442  break;
443 
444  default:
445  snprintf(logbuffer, sizeof(logbuffer)-1,
446  I18N("Plugin \"%s\": Unexpected error (%d)"),
447  GWEN_Plugin_GetName(pl), rv);
448  logbuffer[sizeof(logbuffer)-1]=0;
449  GWEN_Gui_ProgressLog(progressId,
451  logbuffer);
452  break;
453  } /* switch */
454  } /* if plugin loaded */
455  else {
456  snprintf(logbuffer, sizeof(logbuffer)-1,
457  I18N("Could not load plugin \"%s\""),
459  logbuffer[sizeof(logbuffer)-1]=0;
460  GWEN_Gui_ProgressLog(progressId,
462  logbuffer);
463  }
464 
465  cnt++;
466  err=GWEN_Gui_ProgressAdvance(progressId, cnt);
467  if (err) {
468  DBG_INFO(GWEN_LOGDOMAIN, "User aborted");
469  GWEN_Gui_ProgressEnd(progressId);
472  GWEN_Gui_ProgressEnd(progressId);
473  return err;
474  }
475 
477  }
478 
479  GWEN_Gui_ProgressEnd(progressId);
481  }
483  }
484 
486 }
487 
488 
489 
490 
491 
492 
493 
494 
495 
int GWEN_Gui_ProgressLog(uint32_t id, GWEN_LOGGER_LEVEL level, const char *text)
Definition: gui.c:1003
GWEN_PLUGIN_DESCRIPTION_LIST2 * GWEN_PluginManager_GetPluginDescrs(GWEN_PLUGIN_MANAGER *pm)
Definition: plugin.c:626
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
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:51
int GWEN_Gui_ProgressAdvance(uint32_t id, uint32_t progress)
Definition: gui.c:981
GWEN_CRYPT_TOKEN_DEVICE GWEN_Crypt_Token_Plugin_GetDeviceType(const GWEN_PLUGIN *pl)
Definition: ctplugin.c:178
struct GWEN_PLUGIN GWEN_PLUGIN
Definition: plugin.h:39
GWEN_CRYPT_TOKEN_PLUGIN_CHECKTOKEN_FN GWEN_Crypt_Token_Plugin_SetCheckTokenFn(GWEN_PLUGIN *pl, GWEN_CRYPT_TOKEN_PLUGIN_CHECKTOKEN_FN fn)
Definition: ctplugin.c:209
#define GWEN_CRYPT_TOKEN_FOLDER
Definition: ctplugin.h:25
GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR * GWEN_PluginDescription_List2_First(GWEN_PLUGIN_DESCRIPTION_LIST2 *l)
#define GWEN_GUI_PROGRESS_ALLOW_EMBED
Definition: gui.h:196
const char * GWEN_XMLNode_GetProperty(const GWEN_XMLNODE *n, const char *name, const char *defaultValue)
Definition: xml.c:239
struct GWEN_PLUGIN_DESCRIPTION GWEN_PLUGIN_DESCRIPTION
Definition: plugindescr.h:41
#define GWEN_CRYPT_TOKEN_PLUGIN_TYPENAME
Definition: ctplugin.h:24
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:61
#define NULL
Definition: binreloc.c:300
#define GWEN_GUI_PROGRESS_ALWAYS_SHOW_LOG
Definition: gui.h:199
void GWEN_PluginManager_free(GWEN_PLUGIN_MANAGER *pm)
Definition: plugin.c:200
#define DIRSEP
Definition: ctplugin.c:30
#define GWEN_GUI_PROGRESS_DELAY
Definition: gui.h:192
#define DBG_WARN(dbg_logger, format, args...)
Definition: debug.h:124
struct GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR
Definition: listdoc.h:6243
int GWEN_Crypt_Token_ModuleInit(void)
Definition: ctplugin.c:40
GWEN_PLUGIN * GWEN_PluginManager_GetPlugin(GWEN_PLUGIN_MANAGER *pm, const char *s)
Definition: plugin.c:529
#define GWEN_LOGDOMAIN
Definition: logger.h:35
void GWEN_PluginDescription_List2_freeAll(GWEN_PLUGIN_DESCRIPTION_LIST2 *pdl)
Definition: plugindescr.c:200
GWEN_CRYPT_TOKEN * GWEN_Crypt_Token_Plugin_CreateToken(GWEN_PLUGIN *pl, const char *name)
Definition: ctplugin.c:142
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
GWEN_PLUGIN_MANAGER * GWEN_PluginManager_FindPluginManager(const char *s)
Definition: plugin.c:548
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:386
void GWEN_PluginDescription_List2Iterator_free(GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR *li)
void GWEN_Buffer_Reset(GWEN_BUFFER *bf)
Definition: buffer.c:650
GWEN_PLUGIN_DESCRIPTION_LIST2 * GWEN_Crypt_Token_PluginManager_GetPluginDescrs(GWEN_PLUGIN_MANAGER *pm, GWEN_CRYPT_TOKEN_DEVICE devt)
Definition: ctplugin.c:234
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:402
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:58
GWEN_PLUGIN_DESCRIPTION_LIST2 * GWEN_PluginDescription_List2_new()
GWEN_PLUGIN_DESCRIPTION * GWEN_PluginDescription_List2Iterator_Next(GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR *li)
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:55
void GWEN_PluginDescription_List2_PushBack(GWEN_PLUGIN_DESCRIPTION_LIST2 *l, GWEN_PLUGIN_DESCRIPTION *p)
int GWEN_Crypt_Token_PluginManager_CheckToken(GWEN_PLUGIN_MANAGER *pm, GWEN_CRYPT_TOKEN_DEVICE devt, GWEN_BUFFER *typeName, GWEN_BUFFER *tokenName, uint32_t guiid)
Definition: ctplugin.c:299
#define GWENHYWFAR_CB
Definition: gwenhywfarapi.h:89
unsigned int GWEN_PluginDescription_List2_GetSize(GWEN_PLUGIN_DESCRIPTION_LIST2 *l)
int GWEN_Buffer_AppendBuffer(GWEN_BUFFER *bf, GWEN_BUFFER *sf)
Definition: buffer.c:507
GWEN_PLUGIN_DESCRIPTION * GWEN_PluginDescription_dup(const GWEN_PLUGIN_DESCRIPTION *pd)
Definition: plugindescr.c:143
GWEN_PLUGIN * GWEN_Crypt_Token_Plugin_new(GWEN_PLUGIN_MANAGER *mgr, GWEN_CRYPT_TOKEN_DEVICE devType, const char *typeName, const char *fileName)
Definition: ctplugin.c:112
GWEN_PLUGIN_MANAGER * GWEN_PluginManager_new(const char *name, const char *destLib)
Definition: plugin.c:180
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:54
GWEN_PLUGIN_DESCRIPTION * GWEN_PluginDescription_List2Iterator_Data(GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR *li)
GWENHYWFAR_CB void GWEN_Crypt_Token_Plugin_FreeData(GWEN_UNUSED void *bp, void *p)
Definition: ctplugin.c:131
const char * GWEN_Plugin_GetName(const GWEN_PLUGIN *p)
Definition: plugin.c:143
int GWEN_PluginManager_Register(GWEN_PLUGIN_MANAGER *pm)
Definition: plugin.c:564
GWEN_STRINGLIST * GWEN_PathManager_GetPaths(const char *destLib, const char *pathName)
Definition: pathmanager.c:494
GWEN_BUFFER * GWEN_Buffer_dup(GWEN_BUFFER *bf)
Definition: buffer.c:105
#define GWEN_ERROR_BAD_NAME
Definition: error.h:110
struct GWEN_PLUGIN_DESCRIPTION_LIST2 GWEN_PLUGIN_DESCRIPTION_LIST2
Definition: listdoc.h:6238
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
struct GWEN_CRYPT_TOKEN GWEN_CRYPT_TOKEN
Definition: ct.h:19
uint32_t GWEN_Gui_ProgressStart(uint32_t progressFlags, const char *title, const char *text, uint64_t total, uint32_t guiid)
Definition: gui.c:963
GWEN_XMLNODE * GWEN_PluginDescription_GetXmlNode(const GWEN_PLUGIN_DESCRIPTION *pd)
Definition: plugindescr.c:412
int GWEN_Gui_ProgressEnd(uint32_t id)
Definition: gui.c:1037
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
int GWEN_Crypt_Token_Plugin_CheckToken(GWEN_PLUGIN *pl, GWEN_BUFFER *name)
Definition: ctplugin.c:160
#define GWEN_GUI_PROGRESS_SHOW_PROGRESS
Definition: gui.h:197
#define GWEN_ERROR_NOT_SUPPORTED
Definition: error.h:109
#define GWEN_GUI_PROGRESS_SHOW_LOG
Definition: gui.h:193
GWEN_STRINGLISTENTRY * GWEN_StringListEntry_Next(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:394
const char * GWEN_Crypt_Token_Device_toString(GWEN_CRYPT_TOKEN_DEVICE d)
Definition: ct.c:1177
const char * GWEN_PluginDescription_GetName(const GWEN_PLUGIN_DESCRIPTION *pd)
Definition: plugindescr.c:232
#define GWEN_ERROR_NOT_FOUND
Definition: error.h:89
#define GWEN_PM_PLUGINDIR
Definition: gwenhywfar.h:53
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:178
#define GWEN_PM_LIBNAME
Definition: gwenhywfar.h:42
GWEN_CRYPT_TOKEN_DEVICE
Definition: ct.h:35
int GWEN_PluginManager_Unregister(GWEN_PLUGIN_MANAGER *pm)
Definition: plugin.c:595
int GWEN_PluginManager_AddPath(GWEN_PLUGIN_MANAGER *pm, const char *callingLib, const char *s)
Definition: plugin.c:223
#define GWEN_INHERIT(bt, t)
Definition: inherit.h:264
GWEN_CRYPT_TOKEN_PLUGIN_CREATETOKEN_FN GWEN_Crypt_Token_Plugin_SetCreateTokenFn(GWEN_PLUGIN *pl, GWEN_CRYPT_TOKEN_PLUGIN_CREATETOKEN_FN fn)
Definition: ctplugin.c:191
#define GWEN_ERROR_USER_ABORTED
Definition: error.h:65
int GWENHYWFAR_CB(* GWEN_CRYPT_TOKEN_PLUGIN_CHECKTOKEN_FN)(GWEN_PLUGIN *pl, GWEN_BUFFER *name)
Definition: ctplugin_be.h:28
GWEN_CRYPT_TOKEN *GWENHYWFAR_CB(* GWEN_CRYPT_TOKEN_PLUGIN_CREATETOKEN_FN)(GWEN_PLUGIN *pl, const char *name)
Definition: ctplugin_be.h:24
#define GWEN_INHERIT_SETDATA(bt, t, element, data, fn)
Definition: inherit.h:292
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:149
GWEN_PLUGIN * GWEN_Plugin_new(GWEN_PLUGIN_MANAGER *pm, const char *name, const char *fileName)
Definition: plugin.c:81
int GWEN_Crypt_Token_ModuleFini(void)
Definition: ctplugin.c:81
#define GWEN_UNUSED
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989
#define GWEN_INHERIT_GETDATA(bt, t, element)
Definition: inherit.h:271
#define GWEN_GUI_PROGRESS_SHOW_ABORT
Definition: gui.h:194
#define GWEN_ERROR_NOT_IMPLEMENTED
Definition: error.h:108