gwenhywfar  5.10.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 static int _checkPlugin(GWEN_PLUGIN *pl, GWEN_BUFFER *typeName, GWEN_BUFFER *tokenName, uint32_t progressId);
36 
37 
38 
39 GWEN_INHERIT(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN)
40 
41 
42 
43 
45 {
47  int err;
48  GWEN_STRINGLIST *sl;
49 
52  if (err) {
53  DBG_ERROR(GWEN_LOGDOMAIN, "Could not register CryptToken plugin manager");
54  return err;
55  }
56 
57  /* create plugin paths */
59  if (sl) {
61  GWEN_BUFFER *pbuf;
62 
63  pbuf=GWEN_Buffer_new(0, 256, 0, 1);
64 
66  while (se) {
69  DBG_INFO(GWEN_LOGDOMAIN, "Adding plugin path [%s]",
70  GWEN_Buffer_GetStart(pbuf));
72  GWEN_Buffer_GetStart(pbuf));
73  GWEN_Buffer_Reset(pbuf);
75  }
76  GWEN_Buffer_free(pbuf);
78  }
79 
80  return 0;
81 }
82 
83 
84 
86 {
88 
90  if (pm) {
91  int rv;
92 
94  if (rv) {
96  "Could not unregister CryptToken plugin manager (%d)", rv);
97  return rv;
98  }
99  else
101  }
102 
103  return 0;
104 }
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
117  GWEN_CRYPT_TOKEN_DEVICE devType,
118  const char *typeName,
119  const char *fileName)
120 {
121  GWEN_PLUGIN *pl;
122  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
123 
124  pl=GWEN_Plugin_new(mgr, typeName, fileName);
125  GWEN_NEW_OBJECT(GWEN_CRYPT_TOKEN_PLUGIN, xpl);
126  GWEN_INHERIT_SETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl, xpl, GWEN_Crypt_Token_Plugin_FreeData);
127  xpl->devType=devType;
128 
129  return pl;
130 }
131 
132 
133 
136 {
137  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
138 
139  xpl=(GWEN_CRYPT_TOKEN_PLUGIN *)p;
140 
141  GWEN_FREE_OBJECT(xpl);
142 }
143 
144 
145 
147 {
148  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
149 
150  assert(pl);
151  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
152  assert(xpl);
153 
154  if (xpl->createTokenFn)
155  return xpl->createTokenFn(pl, name);
156  else {
157  DBG_WARN(GWEN_LOGDOMAIN, "No createTokenFn");
158  return NULL;
159  }
160 }
161 
162 
163 
165 {
166  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
167 
168  assert(pl);
169  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
170  assert(xpl);
171 
172  if (xpl->checkTokenFn)
173  return xpl->checkTokenFn(pl, name);
174  else {
175  DBG_INFO(GWEN_LOGDOMAIN, "No checkTokenFn");
177  }
178 }
179 
180 
181 
183 {
184  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
185 
186  assert(pl);
187  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
188  assert(xpl);
189 
190  return xpl->devType;
191 }
192 
193 
194 
197 {
198  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
200 
201  assert(pl);
202  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
203  assert(xpl);
204 
205  of=xpl->createTokenFn;
206  xpl->createTokenFn=fn;
207 
208  return of;
209 }
210 
211 
212 
215 {
216  GWEN_CRYPT_TOKEN_PLUGIN *xpl;
218 
219  assert(pl);
220  xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
221  assert(xpl);
222 
223  of=xpl->checkTokenFn;
224  xpl->checkTokenFn=fn;
225 
226  return of;
227 }
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
240 {
242 
244  if (pl1) {
247 
250  if (pit) {
252  const char *ts;
253 
254  if (devt==GWEN_Crypt_Token_Device_Any)
255  ts=NULL;
256  else
259  while (pd) {
260  GWEN_XMLNODE *node;
261  const char *nts;
262  int match=0;
263 
265  assert(node);
266  nts=GWEN_XMLNode_GetProperty(node, "device", 0);
267  if (nts) {
268  if (!ts || (ts && strcasecmp(ts, nts)==0))
269  match=1;
270  }
271  else if (!ts)
272  match=1;
273 
274  if (match) {
276 
279  }
280 
282  }
284  }
286 
290  "No matching plugin descriptions for the given device type");
291  return NULL;
292  }
293  return pl2;
294  }
295  else {
296  DBG_ERROR(GWEN_LOGDOMAIN, "No plugin descriptions at all");
297  }
298  return NULL;
299 }
300 
301 
302 
305  GWEN_BUFFER *typeName,
306  GWEN_BUFFER *tokenName,
307  uint32_t guiid)
308 {
310 
311  assert(pm);
312 
314  if (pdl==NULL) {
315  DBG_ERROR(GWEN_LOGDOMAIN, "No plugin descriptions found for this device type");
316  GWEN_Gui_ProgressLog(guiid,
318  I18N("No plugin found for this device type"));
319  GWEN_Gui_ProgressLog(guiid,
321  I18N("If you're using a Debian/Ubuntu based system "
322  "please consider to install package "
323  LIBCHIPCARD_GWENHYWFAR_PLUGIN_PACKAGE));
324  return GWEN_ERROR_NOT_FOUND;
325  }
326  else {
328 
330  if (pit) {
332  uint32_t progressId;
333  unsigned int pdcount;
334  unsigned int cnt=0;
335 
343  I18N("Determining plugin module..."),
344  NULL,
345  pdcount,
346  guiid);
347 
349  assert(pd);
350  while (pd) {
351  GWEN_XMLNODE *n;
352  int err;
353  GWEN_PLUGIN *pl;
354 
356  assert(n);
357 
358  GWEN_Gui_ProgressLog2(progressId,
360  I18N("Loading plugin \"%s\""),
362 
363  /* device type matches, check this plugin */
365  if (pl) {
366  int rv;
367 
368  rv=_checkPlugin(pl, typeName, tokenName, progressId);
369  if (rv!=0) {
372  GWEN_Gui_ProgressEnd(progressId);
373  return (rv==1)?0:rv;
374  }
375  } /* if plugin loaded */
376  else {
377  GWEN_Gui_ProgressLog2(progressId,
379  I18N("Could not load plugin \"%s\""),
381  }
382 
383  cnt++;
384  err=GWEN_Gui_ProgressAdvance(progressId, cnt);
385  if (err) {
386  DBG_INFO(GWEN_LOGDOMAIN, "User aborted");
387  GWEN_Gui_ProgressEnd(progressId);
390  GWEN_Gui_ProgressEnd(progressId);
391  return err;
392  }
393 
395  }
396 
397  GWEN_Gui_ProgressEnd(progressId);
399  }
401  }
402 
404 }
405 
406 
407 
408 /* return: 0=not supported, 1=supported, negative otherwise */
409 int _checkPlugin(GWEN_PLUGIN *pl, GWEN_BUFFER *typeName, GWEN_BUFFER *tokenName, uint32_t progressId)
410 {
411  GWEN_BUFFER *lTokenName=NULL;
412  int rv;
413  int err;
414 
415  lTokenName=GWEN_Buffer_dup(tokenName);
416  GWEN_Gui_ProgressLog2(progressId,
418  I18N("Checking plugin \"%s\""),
419  GWEN_Plugin_GetName(pl));
420 
422  "Checking plugin \"%s\" for [%s]",
424  GWEN_Buffer_GetStart(lTokenName));
425 
426  rv=GWEN_Crypt_Token_Plugin_CheckToken(pl, lTokenName);
427  switch (rv) {
428  case 0:
429  /* responsive plugin found */
430  err=GWEN_Gui_ProgressLog2(progressId,
432  I18N("Plugin \"%s\" supports this token"),
433  GWEN_Plugin_GetName(pl));
434  if (err==GWEN_ERROR_USER_ABORTED) {
435  GWEN_Buffer_free(lTokenName);
436  return err;
437  }
438 
439  GWEN_Buffer_Reset(typeName);
441  GWEN_Buffer_Reset(tokenName);
442  GWEN_Buffer_AppendBuffer(tokenName, lTokenName);
443  GWEN_Buffer_free(lTokenName);
444  return 1;
445 
447  GWEN_Gui_ProgressLog2(progressId,
449  I18N("Plugin \"%s\": Function not implemented"),
450  GWEN_Plugin_GetName(pl));
451  break;
452 
454  GWEN_Gui_ProgressLog2(progressId,
456  I18N("Plugin \"%s\" does not support this token"),
457  GWEN_Plugin_GetName(pl));
458  break;
459 
460  case GWEN_ERROR_BAD_NAME:
461  GWEN_Gui_ProgressLog2(progressId,
463  I18N("Plugin \"%s\" supports this token, but the name did not match"),
464  GWEN_Plugin_GetName(pl));
465  break;
466 
467  default:
468  GWEN_Gui_ProgressLog2(progressId,
470  I18N("Plugin \"%s\": Unexpected error (%d)"),
471  GWEN_Plugin_GetName(pl), rv);
472  return GWEN_ERROR_GENERIC;
473  break;
474  } /* switch */
475 
476  return 0;
477 }
478 
479 
480 
481 
482 
483 
484 
485 
486 
487 
488 
GWENHYWFAR_API int GWEN_Gui_ProgressLog(uint32_t id, GWEN_LOGGER_LEVEL level, const char *text)
Definition: gui_virtual.c:444
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:53
GWENHYWFAR_API int GWEN_Gui_ProgressAdvance(uint32_t id, uint32_t progress)
Definition: gui_virtual.c:420
GWEN_CRYPT_TOKEN_DEVICE GWEN_Crypt_Token_Plugin_GetDeviceType(const GWEN_PLUGIN *pl)
Definition: ctplugin.c:182
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:213
#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:125
struct GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR
Definition: listdoc.h:6393
int GWEN_Crypt_Token_ModuleInit(void)
Definition: ctplugin.c:44
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:146
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:390
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:238
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:406
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:62
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:303
#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
GWENHYWFAR_API int GWEN_Gui_ProgressLog2(uint32_t id, GWEN_LOGGER_LEVEL level, const char *text,...)
Definition: gui_virtual.c:458
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:116
GWEN_PLUGIN_MANAGER * GWEN_PluginManager_new(const char *name, const char *destLib)
Definition: plugin.c:180
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
#define GWEN_ERROR_GENERIC
Definition: error.h:62
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:135
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:6388
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
GWENHYWFAR_API uint32_t GWEN_Gui_ProgressStart(uint32_t progressFlags, const char *title, const char *text, uint64_t total, uint32_t guiid)
Definition: gui_virtual.c:404
GWEN_XMLNODE * GWEN_PluginDescription_GetXmlNode(const GWEN_PLUGIN_DESCRIPTION *pd)
Definition: plugindescr.c:412
GWENHYWFAR_API int GWEN_Gui_ProgressEnd(uint32_t id)
Definition: gui_virtual.c:480
#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:164
#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:398
const char * GWEN_Crypt_Token_Device_toString(GWEN_CRYPT_TOKEN_DEVICE d)
Definition: ct.c:1162
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:181
#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:195
#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
static int _checkPlugin(GWEN_PLUGIN *pl, GWEN_BUFFER *typeName, GWEN_BUFFER *tokenName, uint32_t progressId)
Definition: ctplugin.c:409
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:156
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:85
#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