gwenhywfar  4.3.3
cpp/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   DBG_ERROR(0, "CppGuiLinker::OpenDialog");
00140 
00141   return xgui->openDialog(dlg, guiid);
00142 }
00143 
00144 
00145 
00146 int CppGuiLinker::CloseDialog(GWEN_GUI *gui,
00147                               GWEN_DIALOG *dlg) {
00148   CppGui *xgui;
00149 
00150   assert(gui);
00151   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00152   assert(xgui);
00153 
00154   return xgui->closeDialog(dlg);
00155 }
00156 
00157 
00158 
00159 int CppGuiLinker::RunDialog(GWEN_GUI *gui,
00160                             GWEN_DIALOG *dlg,
00161                             int untilEnd) {
00162   CppGui *xgui;
00163 
00164   assert(gui);
00165   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00166   assert(xgui);
00167 
00168   return xgui->runDialog(dlg, untilEnd);
00169 }
00170 
00171 
00172 
00173 int CppGuiLinker::GetFileName(GWEN_GUI *gui,
00174                               const char *caption,
00175                               GWEN_GUI_FILENAME_TYPE fnt,
00176                               uint32_t flags,
00177                               const char *patterns,
00178                               GWEN_BUFFER *pathBuffer,
00179                               uint32_t guiid) {
00180   CppGui *xgui;
00181 
00182   assert(gui);
00183   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00184   assert(xgui);
00185 
00186   return xgui->getFileName(caption, fnt, flags, patterns, pathBuffer, guiid);
00187 }
00188 
00189 
00190 
00191 GWENHYWFAR_CB
00192 void CppGuiLinker::freeData(void *bp, void *p) {
00193   CppGui *xgui;
00194 
00195   DBG_NOTICE(0, "CppGuiLinker: Freeing CppGui");
00196   xgui=(CppGui*)p;
00197   if (xgui->_gui) {
00198     xgui->_gui=0;
00199   }
00200   delete xgui;
00201 }
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 
00211 
00212 
00213 
00214 CppGui::CppGui()
00215 :_checkCertFn(NULL)
00216 ,_dbPasswords(NULL)
00217 ,_gui(NULL) {
00218   _gui=GWEN_Gui_new();
00219   _dbPasswords=GWEN_DB_Group_new("passwords");
00220 
00221   GWEN_INHERIT_SETDATA(GWEN_GUI, CppGui,
00222                        _gui, this,
00223                        CppGuiLinker::freeData);
00224   GWEN_Gui_UseDialogs(_gui);
00225   _printFn=GWEN_Gui_SetPrintFn(_gui, CppGuiLinker::Print);
00226   _getPasswordFn=GWEN_Gui_SetGetPasswordFn(_gui, CppGuiLinker::GetPassword);
00227   _setPasswordStatusFn=GWEN_Gui_SetSetPasswordStatusFn(_gui, CppGuiLinker::SetPasswordStatus);
00228   _checkCertFn=GWEN_Gui_SetCheckCertFn(_gui, CppGuiLinker::CheckCert);
00229   GWEN_Gui_SetLogHookFn(_gui, CppGuiLinker::LogHook);
00230   _execDialogFn=GWEN_Gui_SetExecDialogFn(_gui, CppGuiLinker::ExecDialog);
00231   _openDialogFn=GWEN_Gui_SetOpenDialogFn(_gui, CppGuiLinker::OpenDialog);
00232   _closeDialogFn=GWEN_Gui_SetCloseDialogFn(_gui, CppGuiLinker::CloseDialog);
00233   _runDialogFn=GWEN_Gui_SetRunDialogFn(_gui, CppGuiLinker::RunDialog);
00234   _getFileNameFn=GWEN_Gui_SetGetFileNameFn(_gui, CppGuiLinker::GetFileName);
00235 }
00236 
00237 
00238 
00239 CppGui::~CppGui(){
00240   if (_gui) {
00241     GWEN_INHERIT_UNLINK(GWEN_GUI, CppGui, _gui)
00242     GWEN_Gui_free(_gui);
00243   }
00244   GWEN_DB_Group_free(_dbPasswords);
00245 }
00246 
00247 
00248 
00249 int CppGui::print(const char *docTitle,
00250                   const char *docType,
00251                   const char *descr,
00252                   const char *text,
00253                   uint32_t guiid){
00254   if (_printFn)
00255     return _printFn(_gui, docTitle, docType, descr, text, guiid);
00256   else
00257     return GWEN_ERROR_NOT_SUPPORTED;
00258 }
00259 
00260 
00261 
00262 std::string CppGui::_getPasswordHash(const char *token, const char *pin) {
00263   GWEN_MDIGEST *md;
00264   std::string s;
00265   GWEN_BUFFER *buf;
00266   int rv;
00267 
00268   /* hash token and pin */
00269   md=GWEN_MDigest_Md5_new();
00270   rv=GWEN_MDigest_Begin(md);
00271   if (rv==0)
00272     rv=GWEN_MDigest_Update(md, (const uint8_t*)token, strlen(token));
00273   if (rv==0)
00274     rv=GWEN_MDigest_Update(md, (const uint8_t*)pin, strlen(pin));
00275   if (rv==0)
00276     rv=GWEN_MDigest_End(md);
00277   if (rv<0) {
00278     DBG_ERROR(GWEN_LOGDOMAIN, "Hash error (%d)", rv);
00279     GWEN_MDigest_free(md);
00280     return "";
00281   }
00282 
00283   buf=GWEN_Buffer_new(0, 256, 0, 1);
00284   GWEN_Text_ToHexBuffer((const char*)GWEN_MDigest_GetDigestPtr(md),
00285                         GWEN_MDigest_GetDigestSize(md),
00286                         buf,
00287                         0, 0, 0);
00288   s=std::string(GWEN_Buffer_GetStart(buf),
00289                 GWEN_Buffer_GetUsedBytes(buf));
00290   GWEN_Buffer_free(buf);
00291 
00292   GWEN_MDigest_free(md);
00293   return s;
00294 }
00295 
00296 
00297 
00298 int CppGui::getPassword(uint32_t flags,
00299                         const char *token,
00300                         const char *title,
00301                         const char *text,
00302                         char *buffer,
00303                         int minLen,
00304                         int maxLen,
00305                         uint32_t guiid) {
00306   if (flags & GWEN_GUI_INPUT_FLAGS_TAN) {
00307     return GWEN_Gui_InputBox(flags,
00308                              title,
00309                              text,
00310                              buffer,
00311                              minLen,
00312                              maxLen,
00313                              guiid);
00314   }
00315   else {
00316     GWEN_BUFFER *buf;
00317     int rv;
00318     const char *s;
00319   
00320     buf=GWEN_Buffer_new(0, 256, 0, 1);
00321     GWEN_Text_EscapeToBufferTolerant(token, buf);
00322   
00323     if (!(flags & GWEN_GUI_INPUT_FLAGS_CONFIRM)) {
00324       s=GWEN_DB_GetCharValue(_dbPasswords,
00325                              GWEN_Buffer_GetStart(buf),
00326                              0, NULL);
00327       if (s) {
00328         int i;
00329 
00330         i=strlen(s);
00331         if (i>=minLen && i<=maxLen) {
00332           memmove(buffer, s, i+1);
00333           GWEN_Buffer_free(buf);
00334           return 0;
00335         }
00336       }
00337     }
00338   
00339     for (;;) {
00340       rv=GWEN_Gui_InputBox(flags,
00341                            title,
00342                            text,
00343                            buffer,
00344                            minLen,
00345                            maxLen,
00346                            guiid);
00347       if (rv) {
00348         GWEN_Buffer_free(buf);
00349         return rv;
00350       }
00351       else {
00352         std::string s;
00353         std::list<std::string>::iterator it;
00354         bool isBad=false;
00355   
00356         s=_getPasswordHash(token, buffer);
00357         for (it=_badPasswords.begin();
00358              it!=_badPasswords.end();
00359              it++) {
00360           if (*it==s) {
00361             /* password is bad */
00362             isBad=true;
00363             break;
00364           }
00365         }
00366     
00367         if (!isBad)
00368           break;
00369         rv=GWEN_Gui_MessageBox(GWEN_GUI_MSG_FLAGS_TYPE_ERROR |
00370                                GWEN_GUI_MSG_FLAGS_CONFIRM_B1 |
00371                                GWEN_GUI_MSG_FLAGS_SEVERITY_DANGEROUS,
00372                                I18N("Enforce PIN"),
00373                                I18N(
00374                                    "You entered the same PIN twice.\n"
00375                                    "The PIN is marked as bad, do you want\n"
00376                                    "to use it anyway?"
00377                                    "<html>"
00378                                    "<p>"
00379                                    "You entered the same PIN twice."
00380                                    "</p>"
00381                                    "<p>"
00382                                    "The PIN is marked as <b>bad</b>, "
00383                                    "do you want to use it anyway?"
00384                                    "</p>"
00385                                    "</html>"),
00386                                I18N("Use my input"),
00387                                I18N("Re-enter"),
00388                                0,
00389                                guiid);
00390         if (rv==1) {
00391           /* accept this input */
00392           _badPasswords.remove(s);
00393           break;
00394         }
00395       }
00396     }
00397 
00398     GWEN_Buffer_free(buf);
00399     return 0;
00400   }
00401 }
00402 
00403 
00404 
00405 int CppGui::checkCert(const GWEN_SSLCERTDESCR *cd,
00406                       GWEN_SYNCIO *sio,
00407                       uint32_t guiid) {
00408   return checkCertBuiltIn(cd, sio, guiid);
00409 }
00410 
00411 
00412 
00413 int CppGui::logHook(const char *logDomain,
00414                     GWEN_LOGGER_LEVEL priority, const char *s) {
00415   /* not hooked */
00416   return 0;
00417 }
00418 
00419 
00420 
00421 int CppGui::execDialog(GWEN_DIALOG *dlg, uint32_t guiid) {
00422   return GWEN_ERROR_NOT_SUPPORTED;
00423 }
00424 
00425 
00426 
00427 int CppGui::openDialog(GWEN_DIALOG *dlg, uint32_t guiid) {
00428   DBG_ERROR(0, "CppGui::OpenDialog");
00429 
00430   return GWEN_ERROR_NOT_SUPPORTED;
00431 }
00432 
00433 
00434 
00435 int CppGui::closeDialog(GWEN_DIALOG *dlg) {
00436   return GWEN_ERROR_NOT_SUPPORTED;
00437 }
00438 
00439 
00440 
00441 int CppGui::runDialog(GWEN_DIALOG *dlg, int untilEnd) {
00442   return GWEN_ERROR_NOT_SUPPORTED;
00443 }
00444 
00445 
00446 
00447 int CppGui::getFileName(const char *caption,
00448                         GWEN_GUI_FILENAME_TYPE fnt,
00449                         uint32_t flags,
00450                         const char *patterns,
00451                         GWEN_BUFFER *pathBuffer,
00452                         uint32_t guiid) {
00453   DBG_ERROR(0, "Not supported");
00454   return GWEN_ERROR_NOT_SUPPORTED;
00455 }
00456 
00457 
00458 
00459 
00460 int CppGui::checkCertBuiltIn(const GWEN_SSLCERTDESCR *cert,
00461                              GWEN_SYNCIO *sio,
00462                              uint32_t guiid) {
00463   if (_checkCertFn)
00464     return _checkCertFn(_gui, cert, sio, guiid);
00465   else {
00466     DBG_ERROR(0, "No built-in checkcert function?");
00467     return GWEN_ERROR_NOT_SUPPORTED;
00468   }
00469 }
00470 
00471 
00472 
00473 int CppGui::setPasswordStatus(const char *token,
00474                               const char *pin,
00475                               GWEN_GUI_PASSWORD_STATUS status,
00476                               uint32_t guiid) {
00477   if (token==NULL && pin==NULL && status==GWEN_Gui_PasswordStatus_Remove) {
00478     GWEN_DB_ClearGroup(_dbPasswords, NULL);
00479   }
00480   else {
00481     GWEN_BUFFER *buf;
00482     std::string s;
00483 
00484     buf=GWEN_Buffer_new(0, 256, 0, 1);
00485     GWEN_Text_EscapeToBufferTolerant(token, buf);
00486 
00487     s=_getPasswordHash(token, pin);
00488     if (status==GWEN_Gui_PasswordStatus_Bad) {
00489       std::list<std::string>::iterator it;
00490 
00491       s=_getPasswordHash(token, pin);
00492       for (it=_badPasswords.begin();
00493            it!=_badPasswords.end();
00494            it++) {
00495         if (*it==s) {
00496           /* bad password already in list */
00497           GWEN_Buffer_free(buf);
00498           return 0;
00499         }
00500       }
00501       _badPasswords.push_back(s);
00502     }
00503     else if (status==GWEN_Gui_PasswordStatus_Ok) {
00504       /* only store passwords of which we know that they are ok */
00505       GWEN_DB_SetCharValue(_dbPasswords, GWEN_DB_FLAGS_OVERWRITE_VARS,
00506                            GWEN_Buffer_GetStart(buf), pin);
00507     }
00508     GWEN_Buffer_free(buf);
00509   }
00510 
00511   return 0;
00512 }
00513 
00514 
00515 
00516 GWEN_GUI *CppGui::getCInterface(){
00517   return _gui;
00518 }
00519 
00520 
00521 
00522 CppGui *CppGui::getCppGui(){
00523   GWEN_GUI *gui;
00524   CppGui *xgui;
00525 
00526   gui=GWEN_Gui_GetGui();
00527   if (gui==NULL)
00528     return NULL;
00529   xgui=GWEN_INHERIT_GETDATA(GWEN_GUI, CppGui, gui);
00530   return xgui;
00531 }
00532 
00533 
00534 
00535 
00536