00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifdef HAVE_CONFIG_H
00014 # include <config.h>
00015 #endif
00016
00017
00018 #include "ctplugin_p.h"
00019 #include "i18n_l.h"
00020
00021 #include <gwenhywfar/gwenhywfar.h>
00022 #include <gwenhywfar/misc.h>
00023 #include <gwenhywfar/debug.h>
00024 #include <gwenhywfar/gui.h>
00025 #include <gwenhywfar/pathmanager.h>
00026
00027
00028 #ifdef OS_WIN32
00029 # define DIRSEP "\\"
00030 #else
00031 # define DIRSEP "/"
00032 #endif
00033
00034
00035
00036 GWEN_INHERIT(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN)
00037
00038
00039
00040
00041 int GWEN_Crypt_Token_ModuleInit(){
00042 GWEN_PLUGIN_MANAGER *pm;
00043 int err;
00044 GWEN_STRINGLIST *sl;
00045
00046 pm=GWEN_PluginManager_new(GWEN_CRYPT_TOKEN_PLUGIN_TYPENAME, GWEN_PM_LIBNAME);
00047 err=GWEN_PluginManager_Register(pm);
00048 if (err) {
00049 DBG_ERROR(GWEN_LOGDOMAIN, "Could not register CryptToken plugin manager");
00050 return err;
00051 }
00052
00053
00054 sl=GWEN_PathManager_GetPaths(GWEN_PM_LIBNAME, GWEN_PM_PLUGINDIR);
00055 if (sl) {
00056 GWEN_STRINGLISTENTRY *se;
00057 GWEN_BUFFER *pbuf;
00058
00059 pbuf=GWEN_Buffer_new(0, 256, 0, 1);
00060
00061 se=GWEN_StringList_FirstEntry(sl);
00062 while(se) {
00063 GWEN_Buffer_AppendString(pbuf, GWEN_StringListEntry_Data(se));
00064 GWEN_Buffer_AppendString(pbuf, DIRSEP GWEN_CRYPT_TOKEN_FOLDER);
00065 DBG_INFO(GWEN_LOGDOMAIN, "Adding plugin path [%s]",
00066 GWEN_Buffer_GetStart(pbuf));
00067 GWEN_PluginManager_AddPath(pm, GWEN_PM_LIBNAME,
00068 GWEN_Buffer_GetStart(pbuf));
00069 GWEN_Buffer_Reset(pbuf);
00070 se=GWEN_StringListEntry_Next(se);
00071 }
00072 GWEN_Buffer_free(pbuf);
00073 GWEN_StringList_free(sl);
00074 }
00075
00076 return 0;
00077 }
00078
00079
00080
00081 int GWEN_Crypt_Token_ModuleFini(){
00082 GWEN_PLUGIN_MANAGER *pm;
00083
00084 pm=GWEN_PluginManager_FindPluginManager(GWEN_CRYPT_TOKEN_PLUGIN_TYPENAME);
00085 if (pm) {
00086 int rv;
00087
00088 rv=GWEN_PluginManager_Unregister(pm);
00089 if (rv) {
00090 DBG_ERROR(GWEN_LOGDOMAIN,
00091 "Could not unregister CryptToken plugin manager (%d)", rv);
00092 return rv;
00093 }
00094 else
00095 GWEN_PluginManager_free(pm);
00096 }
00097
00098 return 0;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 GWEN_PLUGIN *GWEN_Crypt_Token_Plugin_new(GWEN_PLUGIN_MANAGER *mgr,
00112 GWEN_CRYPT_TOKEN_DEVICE devType,
00113 const char *typeName,
00114 const char *fileName) {
00115 GWEN_PLUGIN *pl;
00116 GWEN_CRYPT_TOKEN_PLUGIN *xpl;
00117
00118 pl=GWEN_Plugin_new(mgr, typeName, fileName);
00119 GWEN_NEW_OBJECT(GWEN_CRYPT_TOKEN_PLUGIN, xpl);
00120 GWEN_INHERIT_SETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl, xpl, GWEN_Crypt_Token_Plugin_FreeData);
00121 xpl->devType=devType;
00122
00123 return pl;
00124 }
00125
00126
00127
00128 GWENHYWFAR_CB
00129 void GWEN_Crypt_Token_Plugin_FreeData(GWEN_UNUSED void *bp, void *p) {
00130 GWEN_CRYPT_TOKEN_PLUGIN *xpl;
00131
00132 xpl=(GWEN_CRYPT_TOKEN_PLUGIN*)p;
00133
00134 GWEN_FREE_OBJECT(xpl);
00135 }
00136
00137
00138
00139 GWEN_CRYPT_TOKEN *GWEN_Crypt_Token_Plugin_CreateToken(GWEN_PLUGIN *pl, const char *name) {
00140 GWEN_CRYPT_TOKEN_PLUGIN *xpl;
00141
00142 assert(pl);
00143 xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
00144 assert(xpl);
00145
00146 if (xpl->createTokenFn)
00147 return xpl->createTokenFn(pl, name);
00148 else {
00149 DBG_WARN(GWEN_LOGDOMAIN, "No createTokenFn");
00150 return NULL;
00151 }
00152 }
00153
00154
00155
00156 int GWEN_Crypt_Token_Plugin_CheckToken(GWEN_PLUGIN *pl, GWEN_BUFFER *name) {
00157 GWEN_CRYPT_TOKEN_PLUGIN *xpl;
00158
00159 assert(pl);
00160 xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
00161 assert(xpl);
00162
00163 if (xpl->checkTokenFn)
00164 return xpl->checkTokenFn(pl, name);
00165 else {
00166 DBG_INFO(GWEN_LOGDOMAIN, "No checkTokenFn");
00167 return GWEN_ERROR_NOT_IMPLEMENTED;
00168 }
00169 }
00170
00171
00172
00173 GWEN_CRYPT_TOKEN_DEVICE GWEN_Crypt_Token_Plugin_GetDeviceType(const GWEN_PLUGIN *pl) {
00174 GWEN_CRYPT_TOKEN_PLUGIN *xpl;
00175
00176 assert(pl);
00177 xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
00178 assert(xpl);
00179
00180 return xpl->devType;
00181 }
00182
00183
00184
00185 GWEN_CRYPT_TOKEN_PLUGIN_CREATETOKEN_FN GWEN_Crypt_Token_Plugin_SetCreateTokenFn(GWEN_PLUGIN *pl,
00186 GWEN_CRYPT_TOKEN_PLUGIN_CREATETOKEN_FN fn) {
00187 GWEN_CRYPT_TOKEN_PLUGIN *xpl;
00188 GWEN_CRYPT_TOKEN_PLUGIN_CREATETOKEN_FN of;
00189
00190 assert(pl);
00191 xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
00192 assert(xpl);
00193
00194 of=xpl->createTokenFn;
00195 xpl->createTokenFn=fn;
00196
00197 return of;
00198 }
00199
00200
00201
00202 GWEN_CRYPT_TOKEN_PLUGIN_CHECKTOKEN_FN GWEN_Crypt_Token_Plugin_SetCheckTokenFn(GWEN_PLUGIN *pl,
00203 GWEN_CRYPT_TOKEN_PLUGIN_CHECKTOKEN_FN fn) {
00204 GWEN_CRYPT_TOKEN_PLUGIN *xpl;
00205 GWEN_CRYPT_TOKEN_PLUGIN_CHECKTOKEN_FN of;
00206
00207 assert(pl);
00208 xpl=GWEN_INHERIT_GETDATA(GWEN_PLUGIN, GWEN_CRYPT_TOKEN_PLUGIN, pl);
00209 assert(xpl);
00210
00211 of=xpl->checkTokenFn;
00212 xpl->checkTokenFn=fn;
00213
00214 return of;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 GWEN_PLUGIN_DESCRIPTION_LIST2 *GWEN_Crypt_Token_PluginManager_GetPluginDescrs(GWEN_PLUGIN_MANAGER *pm,
00227 GWEN_CRYPT_TOKEN_DEVICE devt) {
00228 GWEN_PLUGIN_DESCRIPTION_LIST2 *pl1;
00229
00230 pl1=GWEN_PluginManager_GetPluginDescrs(pm);
00231 if (pl1) {
00232 GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR *pit;
00233 GWEN_PLUGIN_DESCRIPTION_LIST2 *pl2;
00234
00235 pl2=GWEN_PluginDescription_List2_new();
00236 pit=GWEN_PluginDescription_List2_First(pl1);
00237 if (pit) {
00238 GWEN_PLUGIN_DESCRIPTION *pd;
00239 const char *ts;
00240
00241 if (devt==GWEN_Crypt_Token_Device_Any)
00242 ts=NULL;
00243 else
00244 ts=GWEN_Crypt_Token_Device_toString(devt);
00245 pd=GWEN_PluginDescription_List2Iterator_Data(pit);
00246 while(pd) {
00247 GWEN_XMLNODE *node;
00248 const char *nts;
00249 int match=0;
00250
00251 node=GWEN_PluginDescription_GetXmlNode(pd);
00252 assert(node);
00253 nts=GWEN_XMLNode_GetProperty(node, "device", 0);
00254 if (nts) {
00255 if (!ts || (ts && strcasecmp(ts, nts)==0))
00256 match=1;
00257 }
00258 else if (!ts)
00259 match=1;
00260
00261 if (match) {
00262 GWEN_PLUGIN_DESCRIPTION *pd2;
00263
00264 pd2=GWEN_PluginDescription_dup(pd);
00265 GWEN_PluginDescription_List2_PushBack(pl2, pd2);
00266 }
00267
00268 pd=GWEN_PluginDescription_List2Iterator_Next(pit);
00269 }
00270 GWEN_PluginDescription_List2Iterator_free(pit);
00271 }
00272 GWEN_PluginDescription_List2_freeAll(pl1);
00273
00274 if (GWEN_PluginDescription_List2_GetSize(pl2)==0) {
00275 GWEN_PluginDescription_List2_freeAll(pl2);
00276 DBG_ERROR(GWEN_LOGDOMAIN,
00277 "No matching plugin descriptions for the given device type");
00278 return NULL;
00279 }
00280 return pl2;
00281 }
00282 else {
00283 DBG_ERROR(GWEN_LOGDOMAIN, "No plugin descriptions at all");
00284 }
00285 return NULL;
00286 }
00287
00288
00289
00290 int GWEN_Crypt_Token_PluginManager_CheckToken(GWEN_PLUGIN_MANAGER *pm,
00291 GWEN_CRYPT_TOKEN_DEVICE devt,
00292 GWEN_BUFFER *typeName,
00293 GWEN_BUFFER *tokenName,
00294 uint32_t guiid) {
00295 GWEN_PLUGIN_DESCRIPTION_LIST2 *pdl;
00296
00297 assert(pm);
00298
00299 pdl=GWEN_Crypt_Token_PluginManager_GetPluginDescrs(pm, devt);
00300 if (pdl==NULL) {
00301 DBG_ERROR(GWEN_LOGDOMAIN, "No plugin descriptions found for this device type");
00302 GWEN_Gui_ProgressLog(guiid,
00303 GWEN_LoggerLevel_Error,
00304 I18N("No plugin found for this device type"));
00305 GWEN_Gui_ProgressLog(guiid,
00306 GWEN_LoggerLevel_Error,
00307 I18N("If you're using a Debian/Ubuntu based system "
00308 "please consider to install package "
00309 LIBCHIPCARD_GWENHYWFAR_PLUGIN_PACKAGE));
00310 return GWEN_ERROR_NOT_FOUND;
00311 }
00312 else {
00313 GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR *pit;
00314
00315 pit=GWEN_PluginDescription_List2_First(pdl);
00316 if (pit) {
00317 GWEN_PLUGIN_DESCRIPTION *pd;
00318 uint32_t progressId;
00319 unsigned int pdcount;
00320 unsigned int cnt=0;
00321
00322 pdcount=GWEN_PluginDescription_List2_GetSize(pdl);
00323 progressId=GWEN_Gui_ProgressStart(GWEN_GUI_PROGRESS_DELAY |
00324 GWEN_GUI_PROGRESS_ALLOW_EMBED |
00325 GWEN_GUI_PROGRESS_SHOW_PROGRESS |
00326 GWEN_GUI_PROGRESS_SHOW_ABORT,
00327 I18N("Determining plugin module..."),
00328 NULL,
00329 pdcount,
00330 guiid);
00331
00332 pd=GWEN_PluginDescription_List2Iterator_Data(pit);
00333 assert(pd);
00334 while(pd) {
00335 GWEN_XMLNODE *n;
00336 int err;
00337 GWEN_PLUGIN *pl;
00338 char logbuffer[256];
00339
00340 n=GWEN_PluginDescription_GetXmlNode(pd);
00341 assert(n);
00342
00343 snprintf(logbuffer, sizeof(logbuffer)-1,
00344 I18N("Loading plugin \"%s\""),
00345 GWEN_PluginDescription_GetName(pd));
00346 logbuffer[sizeof(logbuffer)-1]=0;
00347 GWEN_Gui_ProgressLog(progressId,
00348 GWEN_LoggerLevel_Notice,
00349 logbuffer);
00350
00351
00352 pl=GWEN_PluginManager_GetPlugin(pm, GWEN_PluginDescription_GetName(pd));
00353 if (pl) {
00354 GWEN_BUFFER *lTokenName;
00355 int rv;
00356
00357 lTokenName=GWEN_Buffer_dup(tokenName);
00358
00359 snprintf(logbuffer, sizeof(logbuffer)-1,
00360 I18N("Checking plugin \"%s\""),
00361 GWEN_Plugin_GetName(pl));
00362 logbuffer[sizeof(logbuffer)-1]=0;
00363 GWEN_Gui_ProgressLog(progressId,
00364 GWEN_LoggerLevel_Notice,
00365 logbuffer);
00366
00367 DBG_INFO(GWEN_LOGDOMAIN,
00368 "Checking plugin \"%s\" for [%s]",
00369 GWEN_Plugin_GetName(pl),
00370 GWEN_Buffer_GetStart(lTokenName));
00371
00372 rv=GWEN_Crypt_Token_Plugin_CheckToken(pl, lTokenName);
00373 switch(rv) {
00374 case 0:
00375
00376 snprintf(logbuffer, sizeof(logbuffer)-1,
00377 I18N("Plugin \"%s\" supports this token"),
00378 GWEN_Plugin_GetName(pl));
00379 logbuffer[sizeof(logbuffer)-1]=0;
00380 err=GWEN_Gui_ProgressLog(progressId,
00381 GWEN_LoggerLevel_Notice,
00382 logbuffer);
00383 if (err==GWEN_ERROR_USER_ABORTED) {
00384 GWEN_Gui_ProgressEnd(progressId);
00385 GWEN_Buffer_free(lTokenName);
00386 GWEN_PluginDescription_List2Iterator_free(pit);
00387 GWEN_PluginDescription_List2_freeAll(pdl);
00388 return err;
00389 }
00390
00391 GWEN_Buffer_Reset(typeName);
00392 GWEN_Buffer_AppendString(typeName, GWEN_Plugin_GetName(pl));
00393 GWEN_Buffer_Reset(tokenName);
00394 GWEN_Buffer_AppendBuffer(tokenName, lTokenName);
00395 GWEN_Buffer_free(lTokenName);
00396 GWEN_PluginDescription_List2Iterator_free(pit);
00397 GWEN_PluginDescription_List2_freeAll(pdl);
00398 GWEN_Gui_ProgressEnd(progressId);
00399 return 0;
00400
00401 case GWEN_ERROR_NOT_IMPLEMENTED:
00402 snprintf(logbuffer, sizeof(logbuffer)-1,
00403 I18N("Plugin \"%s\": Function not implemented"),
00404 GWEN_Plugin_GetName(pl));
00405 logbuffer[sizeof(logbuffer)-1]=0;
00406 GWEN_Gui_ProgressLog(progressId,
00407 GWEN_LoggerLevel_Notice,
00408 logbuffer);
00409 break;
00410
00411 case GWEN_ERROR_NOT_SUPPORTED:
00412 snprintf(logbuffer, sizeof(logbuffer)-1,
00413 I18N("Plugin \"%s\" does not support this token"),
00414 GWEN_Plugin_GetName(pl));
00415 logbuffer[sizeof(logbuffer)-1]=0;
00416 GWEN_Gui_ProgressLog(progressId,
00417 GWEN_LoggerLevel_Info,
00418 logbuffer);
00419 break;
00420
00421 case GWEN_ERROR_BAD_NAME:
00422 snprintf(logbuffer, sizeof(logbuffer)-1,
00423 I18N("Plugin \"%s\" supports this token, but the name "
00424 "did not match"),
00425 GWEN_Plugin_GetName(pl));
00426 logbuffer[sizeof(logbuffer)-1]=0;
00427 GWEN_Gui_ProgressLog(progressId,
00428 GWEN_LoggerLevel_Info,
00429 logbuffer);
00430 break;
00431
00432 default:
00433 snprintf(logbuffer, sizeof(logbuffer)-1,
00434 I18N("Plugin \"%s\": Unexpected error (%d)"),
00435 GWEN_Plugin_GetName(pl), rv);
00436 logbuffer[sizeof(logbuffer)-1]=0;
00437 GWEN_Gui_ProgressLog(progressId,
00438 GWEN_LoggerLevel_Info,
00439 logbuffer);
00440 break;
00441 }
00442 }
00443 else {
00444 snprintf(logbuffer, sizeof(logbuffer)-1,
00445 I18N("Could not load plugin \"%s\""),
00446 GWEN_PluginDescription_GetName(pd));
00447 logbuffer[sizeof(logbuffer)-1]=0;
00448 GWEN_Gui_ProgressLog(progressId,
00449 GWEN_LoggerLevel_Warning,
00450 logbuffer);
00451 }
00452
00453 cnt++;
00454 err=GWEN_Gui_ProgressAdvance(progressId, cnt);
00455 if (err) {
00456 DBG_INFO(GWEN_LOGDOMAIN, "User aborted");
00457 GWEN_Gui_ProgressEnd(progressId);
00458 GWEN_PluginDescription_List2Iterator_free(pit);
00459 GWEN_PluginDescription_List2_freeAll(pdl);
00460 GWEN_Gui_ProgressEnd(progressId);
00461 return err;
00462 }
00463
00464 pd=GWEN_PluginDescription_List2Iterator_Next(pit);
00465 }
00466
00467 GWEN_Gui_ProgressEnd(progressId);
00468 GWEN_PluginDescription_List2Iterator_free(pit);
00469 }
00470 GWEN_PluginDescription_List2_freeAll(pdl);
00471 }
00472
00473 return GWEN_ERROR_NOT_SUPPORTED;
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483