gwenhywfar  4.3.1
fox16/cppgui.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     begin       : Mon Mar 01 2004
00003     copyright   : (C) 2004-2010 by Martin Preuss
00004     email       : martin@libchipcard.de
00005 
00006  ***************************************************************************
00007  *          Please see toplevel file COPYING for license details           *
00008  ***************************************************************************/
00009 
00010 
00011 #ifdef HAVE_CONFIG_H
00012 # include <config.h>
00013 #endif
00014 
00015 #include "cppgui_p.hpp"
00016 #include <assert.h>
00017 
00018 #include <gwenhywfar/inherit.h>
00019 #include <gwenhywfar/debug.h>
00020 #include <gwenhywfar/gui_be.h>
00021 #include <gwenhywfar/i18n.h>
00022 
00023 #include <gwenhywfar/text.h>
00024 #include <gwenhywfar/mdigest.h>
00025 #include <gwenhywfar/debug.h>
00026 
00027 
00028 
00029 GWEN_INHERIT(GWEN_GUI, CppGui);
00030 
00031 
00032 
00033 
00034 int CppGuiLinker::Print(GWEN_GUI *gui,
00035                         const char *docTitle,
00036                         const char *docType,
00037                         const char *descr,
00038                         const char *text,
00039                         uint32_t guiid){
00040   CppGui *xgui;
00041 
00042   assert(gui);
00043   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00044   assert(xgui);
00045 
00046   return xgui->print(docTitle, docType, descr, text, guiid);
00047 }
00048 
00049 
00050 
00051 int CppGuiLinker::GetPassword(GWEN_GUI *gui,
00052                               uint32_t flags,
00053                               const char *token,
00054                               const char *title,
00055                               const char *text,
00056                               char *buffer,
00057                               int minLen,
00058                               int maxLen,
00059                               uint32_t guiid) {
00060   CppGui *xgui;
00061 
00062   assert(gui);
00063   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00064   assert(xgui);
00065 
00066   return xgui->getPassword(flags, token, title, text, buffer, minLen, maxLen, guiid);
00067 }
00068 
00069 
00070 
00071 int CppGuiLinker::SetPasswordStatus(GWEN_GUI *gui,
00072                                     const char *token,
00073                                     const char *pin,
00074                                     GWEN_GUI_PASSWORD_STATUS status,
00075                                     uint32_t guiid) {
00076   CppGui *xgui;
00077 
00078   assert(gui);
00079   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00080   assert(xgui);
00081 
00082   return xgui->setPasswordStatus(token, pin, status, guiid);
00083 }
00084 
00085 
00086 
00087 int CppGuiLinker::CheckCert(GWEN_GUI *gui,
00088                             const GWEN_SSLCERTDESCR *cert,
00089                             GWEN_SYNCIO *sio,
00090                             uint32_t guiid) {
00091   CppGui *xgui;
00092 
00093   assert(gui);
00094   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00095   assert(xgui);
00096 
00097   return xgui->checkCert(cert, sio, guiid);
00098 }
00099 
00100 
00101 
00102 int CppGuiLinker::LogHook(GWEN_GUI *gui,
00103                           const char *logDomain,
00104                           GWEN_LOGGER_LEVEL priority, const char *s) {
00105   CppGui *xgui;
00106 
00107   assert(gui);
00108   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00109   assert(xgui);
00110 
00111   return xgui->logHook(logDomain, priority, s);
00112 }
00113 
00114 
00115 
00116 int CppGuiLinker::ExecDialog(GWEN_GUI *gui,
00117                              GWEN_DIALOG *dlg,
00118                              uint32_t guiid) {
00119   CppGui *xgui;
00120 
00121   assert(gui);
00122   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00123   assert(xgui);
00124 
00125   return xgui->execDialog(dlg, guiid);
00126 }
00127 
00128 
00129 
00130 int CppGuiLinker::OpenDialog(GWEN_GUI *gui,
00131                              GWEN_DIALOG *dlg,
00132                              uint32_t guiid) {
00133   CppGui *xgui;
00134 
00135   assert(gui);
00136   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00137   assert(xgui);
00138 
00139   return xgui->openDialog(dlg, guiid);
00140 }
00141 
00142 
00143 
00144 int CppGuiLinker::CloseDialog(GWEN_GUI *gui,
00145                               GWEN_DIALOG *dlg) {
00146   CppGui *xgui;
00147 
00148   assert(gui);
00149   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00150   assert(xgui);
00151 
00152   return xgui->closeDialog(dlg);
00153 }
00154 
00155 
00156 
00157 int CppGuiLinker::RunDialog(GWEN_GUI *gui,
00158                             GWEN_DIALOG *dlg,
00159                             int untilEnd) {
00160   CppGui *xgui;
00161 
00162   assert(gui);
00163   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00164   assert(xgui);
00165 
00166   return xgui->runDialog(dlg, untilEnd);
00167 }
00168 
00169 
00170 
00171 int CppGuiLinker::GetFileName(GWEN_GUI *gui,
00172                               const char *caption,
00173                               GWEN_GUI_FILENAME_TYPE fnt,
00174                               uint32_t flags,
00175                               const char *patterns,
00176                               GWEN_BUFFER *pathBuffer,
00177                               uint32_t guiid) {
00178   CppGui *xgui;
00179 
00180   assert(gui);
00181   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00182   assert(xgui);
00183 
00184   return xgui->getFileName(caption, fnt, flags, patterns, pathBuffer, guiid);
00185 }
00186 
00187 
00188 
00189 GWENHYWFAR_CB
00190 void CppGuiLinker::freeData(void *bp, void *p) {
00191   CppGui *xgui;
00192 
00193   DBG_NOTICE(0, "CppGuiLinker: Freeing CppGui");
00194   xgui=(CppGui*)p;
00195   if (xgui->_gui) {
00196     xgui->_gui=0;
00197   }
00198   delete xgui;
00199 }
00200 
00201 
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 
00211 
00212 CppGui::CppGui()
00213 :_checkCertFn(NULL)
00214 ,_dbPasswords(NULL)
00215 ,_gui(NULL) {
00216   _gui=GWEN_Gui_new();
00217   _dbPasswords=GWEN_DB_Group_new("passwords");
00218 
00219   GWEN_INHERIT_SETDATA(GWEN_GUI, CppGui,
00220                        _gui, this,
00221                        CppGuiLinker::freeData);
00222   GWEN_Gui_UseDialogs(_gui);
00223   _printFn=GWEN_Gui_SetPrintFn(_gui, CppGuiLinker::Print);
00224   _getPasswordFn=GWEN_Gui_SetGetPasswordFn(_gui, CppGuiLinker::GetPassword);
00225   _setPasswordStatusFn=GWEN_Gui_SetSetPasswordStatusFn(_gui, CppGuiLinker::SetPasswordStatus);
00226   _checkCertFn=GWEN_Gui_SetCheckCertFn(_gui, CppGuiLinker::CheckCert);
00227   GWEN_Gui_SetLogHookFn(_gui, CppGuiLinker::LogHook);
00228   _execDialogFn=GWEN_Gui_SetExecDialogFn(_gui, CppGuiLinker::ExecDialog);
00229   _openDialogFn=GWEN_Gui_SetOpenDialogFn(_gui, CppGuiLinker::OpenDialog);
00230   _closeDialogFn=GWEN_Gui_SetCloseDialogFn(_gui, CppGuiLinker::CloseDialog);
00231   _runDialogFn=GWEN_Gui_SetRunDialogFn(_gui, CppGuiLinker::RunDialog);
00232   _getFileNameFn=GWEN_Gui_SetGetFileNameFn(_gui, CppGuiLinker::GetFileName);
00233 }
00234 
00235 
00236 
00237 CppGui::~CppGui(){
00238   if (_gui) {
00239     GWEN_INHERIT_UNLINK(GWEN_GUI, CppGui, _gui)
00240     GWEN_Gui_free(_gui);
00241   }
00242   GWEN_DB_Group_free(_dbPasswords);
00243 }
00244 
00245 
00246 
00247 int CppGui::print(const char *docTitle,
00248                   const char *docType,
00249                   const char *descr,
00250                   const char *text,
00251                   uint32_t guiid){
00252   if (_printFn)
00253     return _printFn(_gui, docTitle, docType, descr, text, guiid);
00254   else
00255     return GWEN_ERROR_NOT_SUPPORTED;
00256 }
00257 
00258 
00259 
00260 std::string CppGui::_getPasswordHash(const char *token, const char *pin) {
00261   GWEN_MDIGEST *md;
00262   std::string s;
00263   GWEN_BUFFER *buf;
00264   int rv;
00265 
00266   /* hash token and pin */
00267   md=GWEN_MDigest_Md5_new();
00268   rv=GWEN_MDigest_Begin(md);
00269   if (rv==0)
00270     rv=GWEN_MDigest_Update(md, (const uint8_t*)token, strlen(token));
00271   if (rv==0)
00272     rv=GWEN_MDigest_Update(md, (const uint8_t*)pin, strlen(pin));
00273   if (rv==0)
00274     rv=GWEN_MDigest_End(md);
00275   if (rv<0) {
00276     DBG_ERROR(GWEN_LOGDOMAIN, "Hash error (%d)", rv);
00277     GWEN_MDigest_free(md);
00278     return "";
00279   }
00280 
00281   buf=GWEN_Buffer_new(0, 256, 0, 1);
00282   GWEN_Text_ToHexBuffer((const char*)GWEN_MDigest_GetDigestPtr(md),
00283                         GWEN_MDigest_GetDigestSize(md),
00284                         buf,
00285                         0, 0, 0);
00286   s=std::string(GWEN_Buffer_GetStart(buf),
00287                 GWEN_Buffer_GetUsedBytes(buf));
00288   GWEN_Buffer_free(buf);
00289 
00290   GWEN_MDigest_free(md);
00291   return s;
00292 }
00293 
00294 
00295 
00296 int CppGui::getPassword(uint32_t flags,
00297                         const char *token,
00298                         const char *title,
00299                         const char *text,
00300                         char *buffer,
00301                         int minLen,
00302                         int maxLen,
00303                         uint32_t guiid) {
00304   if (flags & GWEN_GUI_INPUT_FLAGS_TAN) {
00305     return GWEN_Gui_InputBox(flags,
00306                              title,
00307                              text,
00308                              buffer,
00309                              minLen,
00310                              maxLen,
00311                              guiid);
00312   }
00313   else {
00314     GWEN_BUFFER *buf;
00315     int rv;
00316     const char *s;
00317   
00318     buf=GWEN_Buffer_new(0, 256, 0, 1);
00319     GWEN_Text_EscapeToBufferTolerant(token, buf);
00320   
00321     if (!(flags & GWEN_GUI_INPUT_FLAGS_CONFIRM)) {
00322       s=GWEN_DB_GetCharValue(_dbPasswords,
00323                              GWEN_Buffer_GetStart(buf),
00324                              0, NULL);
00325       if (s) {
00326         int i;
00327 
00328         i=strlen(s);
00329         if (i>=minLen && i<=maxLen) {
00330           memmove(buffer, s, i+1);
00331           GWEN_Buffer_free(buf);
00332           return 0;
00333         }
00334       }
00335     }
00336   
00337     for (;;) {
00338       rv=GWEN_Gui_InputBox(flags,
00339                            title,
00340                            text,
00341                            buffer,
00342                            minLen,
00343                            maxLen,
00344                            guiid);
00345       if (rv) {
00346         GWEN_Buffer_free(buf);
00347         return rv;
00348       }
00349       else {
00350         std::string s;
00351         std::list<std::string>::iterator it;
00352         bool isBad=false;
00353   
00354         s=_getPasswordHash(token, buffer);
00355         for (it=_badPasswords.begin();
00356              it!=_badPasswords.end();
00357              it++) {
00358           if (*it==s) {
00359             /* password is bad */
00360             isBad=true;
00361             break;
00362           }
00363         }
00364     
00365         if (!isBad)
00366           break;
00367         rv=GWEN_Gui_MessageBox(GWEN_GUI_MSG_FLAGS_TYPE_ERROR |
00368                                GWEN_GUI_MSG_FLAGS_CONFIRM_B1 |
00369                                GWEN_GUI_MSG_FLAGS_SEVERITY_DANGEROUS,
00370                                I18N("Enforce PIN"),
00371                                I18N(
00372                                    "You entered the same PIN twice.\n"
00373                                    "The PIN is marked as bad, do you want\n"
00374                                    "to use it anyway?"
00375                                    "<html>"
00376                                    "<p>"
00377                                    "You entered the same PIN twice."
00378                                    "</p>"
00379                                    "<p>"
00380                                    "The PIN is marked as <b>bad</b>, "
00381                                    "do you want to use it anyway?"
00382                                    "</p>"
00383                                    "</html>"),
00384                                I18N("Use my input"),
00385                                I18N("Re-enter"),
00386                                0,
00387                                guiid);
00388         if (rv==1) {
00389           /* accept this input */
00390           _badPasswords.remove(s);
00391           break;
00392         }
00393       }
00394     }
00395 
00396     GWEN_Buffer_free(buf);
00397     return 0;
00398   }
00399 }
00400 
00401 
00402 
00403 int CppGui::checkCert(const GWEN_SSLCERTDESCR *cd,
00404                       GWEN_SYNCIO *sio,
00405                       uint32_t guiid) {
00406   return checkCertBuiltIn(cd, sio, guiid);
00407 }
00408 
00409 
00410 
00411 int CppGui::logHook(const char *logDomain,
00412                     GWEN_LOGGER_LEVEL priority, const char *s) {
00413   /* not hooked */
00414   return 0;
00415 }
00416 
00417 
00418 
00419 int CppGui::execDialog(GWEN_DIALOG *dlg, uint32_t guiid) {
00420   return GWEN_ERROR_NOT_SUPPORTED;
00421 }
00422 
00423 
00424 
00425 int CppGui::openDialog(GWEN_DIALOG *dlg, uint32_t guiid) {
00426   return GWEN_ERROR_NOT_SUPPORTED;
00427 }
00428 
00429 
00430 
00431 int CppGui::closeDialog(GWEN_DIALOG *dlg) {
00432   return GWEN_ERROR_NOT_SUPPORTED;
00433 }
00434 
00435 
00436 
00437 int CppGui::runDialog(GWEN_DIALOG *dlg, int untilEnd) {
00438   return GWEN_ERROR_NOT_SUPPORTED;
00439 }
00440 
00441 
00442 
00443 int CppGui::getFileName(const char *caption,
00444                         GWEN_GUI_FILENAME_TYPE fnt,
00445                         uint32_t flags,
00446                         const char *patterns,
00447                         GWEN_BUFFER *pathBuffer,
00448                         uint32_t guiid) {
00449   DBG_ERROR(GWEN_LOGDOMAIN, "Not supported");
00450   return GWEN_ERROR_NOT_SUPPORTED;
00451 }
00452 
00453 
00454 
00455 
00456 int CppGui::checkCertBuiltIn(const GWEN_SSLCERTDESCR *cert,
00457                              GWEN_SYNCIO *sio,
00458                              uint32_t guiid) {
00459   if (_checkCertFn)
00460     return _checkCertFn(_gui, cert, sio, guiid);
00461   else {
00462     DBG_ERROR(GWEN_LOGDOMAIN, "No built-in checkcert function?");
00463     return GWEN_ERROR_NOT_SUPPORTED;
00464   }
00465 }
00466 
00467 
00468 
00469 int CppGui::setPasswordStatus(const char *token,
00470                               const char *pin,
00471                               GWEN_GUI_PASSWORD_STATUS status,
00472                               uint32_t guiid) {
00473   if (token==NULL && pin==NULL && status==GWEN_Gui_PasswordStatus_Remove) {
00474     GWEN_DB_ClearGroup(_dbPasswords, NULL);
00475   }
00476   else {
00477     GWEN_BUFFER *buf;
00478     std::string s;
00479 
00480     buf=GWEN_Buffer_new(0, 256, 0, 1);
00481     GWEN_Text_EscapeToBufferTolerant(token, buf);
00482 
00483     s=_getPasswordHash(token, pin);
00484     if (status==GWEN_Gui_PasswordStatus_Bad) {
00485       std::list<std::string>::iterator it;
00486 
00487       s=_getPasswordHash(token, pin);
00488       for (it=_badPasswords.begin();
00489            it!=_badPasswords.end();
00490            it++) {
00491         if (*it==s) {
00492           /* bad password already in list */
00493           GWEN_Buffer_free(buf);
00494           return 0;
00495         }
00496       }
00497       _badPasswords.push_back(s);
00498     }
00499     else if (status==GWEN_Gui_PasswordStatus_Ok) {
00500       /* only store passwords of which we know that they are ok */
00501       GWEN_DB_SetCharValue(_dbPasswords, GWEN_DB_FLAGS_OVERWRITE_VARS,
00502                            GWEN_Buffer_GetStart(buf), pin);
00503     }
00504     GWEN_Buffer_free(buf);
00505   }
00506 
00507   return 0;
00508 }
00509 
00510 
00511 
00512 GWEN_GUI *CppGui::getCInterface(){
00513   return _gui;
00514 }
00515 
00516 
00517 
00518 CppGui *CppGui::getCppGui(){
00519   GWEN_GUI *gui;
00520   CppGui *xgui;
00521 
00522   gui=GWEN_Gui_GetGui();
00523   if (gui==NULL)
00524     return NULL;
00525   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00526   return xgui;
00527 }
00528 
00529 
00530 
00531 
00532