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