ctplugin.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  $RCSfile$
00003                              -------------------
00004     cvs         : $Id: crypttoken.h 1113 2007-01-10 09:14:16Z martin $
00005     begin       : Wed Mar 16 2005
00006     copyright   : (C) 2005 by Martin Preuss
00007     email       : martin@libchipcard.de
00008 
00009  ***************************************************************************
00010  *          Please see toplevel file COPYING for license details           *
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   /* create plugin paths */
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   }
00074 
00075   return 0;
00076 }
00077 
00078 
00079 
00080 int GWEN_Crypt_Token_ModuleFini(){
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(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_INFO(GWEN_LOGDOMAIN, "No plugin descriptions found for this device type");
00301     return GWEN_ERROR_NOT_FOUND;
00302   }
00303   else {
00304     GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR *pit;
00305 
00306     pit=GWEN_PluginDescription_List2_First(pdl);
00307     if (pit) {
00308       GWEN_PLUGIN_DESCRIPTION *pd;
00309       uint32_t progressId;
00310       unsigned int pdcount;
00311       unsigned int cnt=0;
00312 
00313       pdcount=GWEN_PluginDescription_List2_GetSize(pdl);
00314       progressId=GWEN_Gui_ProgressStart(GWEN_GUI_PROGRESS_DELAY |
00315                                         GWEN_GUI_PROGRESS_ALLOW_EMBED |
00316                                         GWEN_GUI_PROGRESS_SHOW_PROGRESS |
00317                                         GWEN_GUI_PROGRESS_SHOW_ABORT,
00318                                         I18N("Determining plugin module..."),
00319                                         NULL,
00320                                         pdcount,
00321                                         guiid);
00322 
00323       pd=GWEN_PluginDescription_List2Iterator_Data(pit);
00324       assert(pd);
00325       while(pd) {
00326         GWEN_XMLNODE *n;
00327         int err;
00328         GWEN_PLUGIN *pl;
00329         char logbuffer[256];
00330 
00331         n=GWEN_PluginDescription_GetXmlNode(pd);
00332         assert(n);
00333 
00334         snprintf(logbuffer, sizeof(logbuffer)-1,
00335                  I18N("Loading plugin \"%s\""),
00336                  GWEN_PluginDescription_GetName(pd));
00337         logbuffer[sizeof(logbuffer)-1]=0;
00338         GWEN_Gui_ProgressLog(progressId,
00339                              GWEN_LoggerLevel_Notice,
00340                              logbuffer);
00341 
00342         /* device type matches, check this plugin */
00343         pl=GWEN_PluginManager_GetPlugin(pm, GWEN_PluginDescription_GetName(pd));
00344         if (pl) {
00345           GWEN_BUFFER *lTokenName;
00346           int rv;
00347 
00348           lTokenName=GWEN_Buffer_dup(tokenName);
00349 
00350           snprintf(logbuffer, sizeof(logbuffer)-1,
00351                    I18N("Checking plugin \"%s\""),
00352                    GWEN_Plugin_GetName(pl));
00353           logbuffer[sizeof(logbuffer)-1]=0;
00354           GWEN_Gui_ProgressLog(progressId,
00355                                GWEN_LoggerLevel_Notice,
00356                                logbuffer);
00357 
00358           DBG_INFO(GWEN_LOGDOMAIN,
00359                    "Checking plugin \"%s\" for [%s]",
00360                    GWEN_Plugin_GetName(pl),
00361                    GWEN_Buffer_GetStart(lTokenName));
00362 
00363           rv=GWEN_Crypt_Token_Plugin_CheckToken(pl, lTokenName);
00364           switch(rv) {
00365           case 0:
00366             /* responsive plugin found */
00367             snprintf(logbuffer, sizeof(logbuffer)-1,
00368                      I18N("Plugin \"%s\" supports this token"),
00369                      GWEN_Plugin_GetName(pl));
00370             logbuffer[sizeof(logbuffer)-1]=0;
00371             err=GWEN_Gui_ProgressLog(progressId,
00372                                      GWEN_LoggerLevel_Notice,
00373                                      logbuffer);
00374             if (err==GWEN_ERROR_USER_ABORTED) {
00375               GWEN_Gui_ProgressEnd(progressId);
00376               GWEN_Buffer_free(lTokenName);
00377               GWEN_PluginDescription_List2Iterator_free(pit);
00378               GWEN_PluginDescription_List2_freeAll(pdl);
00379               return err;
00380             }
00381 
00382             GWEN_Buffer_Reset(typeName);
00383             GWEN_Buffer_AppendString(typeName, GWEN_Plugin_GetName(pl));
00384             GWEN_Buffer_Reset(tokenName);
00385             GWEN_Buffer_AppendBuffer(tokenName, lTokenName);
00386             GWEN_Buffer_free(lTokenName);
00387             GWEN_PluginDescription_List2Iterator_free(pit);
00388             GWEN_PluginDescription_List2_freeAll(pdl);
00389             GWEN_Gui_ProgressEnd(progressId);
00390             return 0;
00391 
00392           case GWEN_ERROR_NOT_IMPLEMENTED:
00393             snprintf(logbuffer, sizeof(logbuffer)-1,
00394                      I18N("Plugin \"%s\": Function not implemented"),
00395                      GWEN_Plugin_GetName(pl));
00396             logbuffer[sizeof(logbuffer)-1]=0;
00397             GWEN_Gui_ProgressLog(progressId,
00398                                  GWEN_LoggerLevel_Notice,
00399                                  logbuffer);
00400             break;
00401 
00402           case GWEN_ERROR_NOT_SUPPORTED:
00403             snprintf(logbuffer, sizeof(logbuffer)-1,
00404                      I18N("Plugin \"%s\" does not support this token"),
00405                      GWEN_Plugin_GetName(pl));
00406             logbuffer[sizeof(logbuffer)-1]=0;
00407             GWEN_Gui_ProgressLog(progressId,
00408                                  GWEN_LoggerLevel_Info,
00409                                  logbuffer);
00410             break;
00411 
00412           case GWEN_ERROR_BAD_NAME:
00413             snprintf(logbuffer, sizeof(logbuffer)-1,
00414                      I18N("Plugin \"%s\" supports this token, but the name "
00415                           "did not match"),
00416                      GWEN_Plugin_GetName(pl));
00417             logbuffer[sizeof(logbuffer)-1]=0;
00418             GWEN_Gui_ProgressLog(progressId,
00419                                  GWEN_LoggerLevel_Info,
00420                                  logbuffer);
00421             break;
00422 
00423           default:
00424             snprintf(logbuffer, sizeof(logbuffer)-1,
00425                      I18N("Plugin \"%s\": Unexpected error (%d)"),
00426                      GWEN_Plugin_GetName(pl), rv);
00427             logbuffer[sizeof(logbuffer)-1]=0;
00428             GWEN_Gui_ProgressLog(progressId,
00429                                  GWEN_LoggerLevel_Info,
00430                                  logbuffer);
00431             break;
00432           } /* switch */
00433         } /* if plugin loaded */
00434         else {
00435           snprintf(logbuffer, sizeof(logbuffer)-1,
00436                    I18N("Could not load plugin \"%s\""),
00437                    GWEN_PluginDescription_GetName(pd));
00438           logbuffer[sizeof(logbuffer)-1]=0;
00439           GWEN_Gui_ProgressLog(progressId,
00440                                GWEN_LoggerLevel_Warning,
00441                                logbuffer);
00442         }
00443 
00444         cnt++;
00445         err=GWEN_Gui_ProgressAdvance(progressId, cnt);
00446         if (err) {
00447           DBG_INFO(GWEN_LOGDOMAIN, "User aborted");
00448           GWEN_Gui_ProgressEnd(progressId);
00449           GWEN_PluginDescription_List2Iterator_free(pit);
00450           GWEN_PluginDescription_List2_freeAll(pdl);
00451           GWEN_Gui_ProgressEnd(progressId);
00452           return err;
00453         }
00454 
00455         pd=GWEN_PluginDescription_List2Iterator_Next(pit);
00456       }
00457 
00458       GWEN_Gui_ProgressEnd(progressId);
00459       GWEN_PluginDescription_List2Iterator_free(pit);
00460     }
00461     GWEN_PluginDescription_List2_freeAll(pdl);
00462   }
00463 
00464   return GWEN_ERROR_NOT_SUPPORTED;
00465 }
00466 
00467 
00468 
00469 
00470 
00471 
00472 
00473 
00474 

Generated on Wed Jul 9 13:12:27 2008 for gwenhywfar by  doxygen 1.5.6