From fb9275e87015d9ca017022f7e9837544de74bc83 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sat, 20 Aug 2016 00:16:39 +0100 Subject: [PATCH] frmNewAccount now using the CardDAV2 class --- source/frmNewAccount-CardDAV2.cpp | 134 +++++++++++ source/frmNewAccount.cpp | 371 ++++++++---------------------- source/frmNewAccount.h | 20 ++ 3 files changed, 251 insertions(+), 274 deletions(-) create mode 100644 source/frmNewAccount-CardDAV2.cpp diff --git a/source/frmNewAccount-CardDAV2.cpp b/source/frmNewAccount-CardDAV2.cpp new file mode 100644 index 0000000..f593238 --- /dev/null +++ b/source/frmNewAccount-CardDAV2.cpp @@ -0,0 +1,134 @@ +// frmNewAccount.cpp - New Account form (CardDAV2 account section). +// +// (c) 2012-2015 Xestia Software Development. +// +// This file is part of Xestia Address Book. +// +// Xestia Address Book is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by the +// Free Software Foundation, version 3 of the license. +// +// Xestia Address Book is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with Xestia Address Book. If not, see + +#include "frmNewAccount.h" +#include + +void frmNewAccount::RunCardDAV2Test( wxCommandEvent& event ){ + + NewAccountResult *ResultData = new NewAccountResult; + + lblServerConnResult->SetLabel(_("Testing...")); + lblCardDAVSupportResult->SetLabel(_("")); + lblServerResponse->SetLabel(_("")); + lblServerSSLResult->SetLabel(_("")); + lblServerSSLValid->SetLabel(_("")); + lblAbleToLoginResult->SetLabel(_("")); + bool UsingSSLBypass = false; + + // Setup a CardDAV2 connection object for testing. + + CardDAV2 TestConnection(txtServerAddress->GetValue().ToStdString(), + wxAtoi(txtServerPort->GetValue()), + txtUsername->GetValue().ToStdString(), + txtPassword->GetValue().ToStdString(), + chkUseSSL->GetValue()); + + // Test the connection. + + TestConnection.SetupConnectionObject(); + COConnectResult TestConnectionResult = TestConnection.Connect(false); + + // If server is using SSL, verify that the SSL connection is valid. + + if (TestConnection.SSLVerify() == COSSL_UNABLETOVERIFY){ +#if defined(__APPLE__) +#elif defined(__WIN32__) +#else + + // Connect again and fetch SSL certificate information. + + TestConnection.BypassSSLVerification(true); + + COConnectResult TestConnectionResult = TestConnection.Connect(false); + + TestConnection.BypassSSLVerification(false); + + SSLCertCollectionString CertData = TestConnection.BuildSSLCollection(); + frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this); + + frmICPtr->LoadDataNew(CertData, txtServerAddress->GetValue().ToStdString()); + frmICPtr->ShowModal(); + + int SSLResult = frmICPtr->GetResult(); + + // Clean up before processing response. + + delete frmICPtr; + frmICPtr = NULL; + + // Process the response from the user. + + if (SSLResult == 1){ + + // Accept the Certificate. + + UsingSSLBypass = true; + TestConnection.BypassSSLVerification(true); + + COConnectResult TestConnectionResult = TestConnection.Connect(true); + + TestConnection.BypassSSLVerification(false); + + } else if (SSLResult == 2){ + + // Reject the certificate, abort the task and mark as failed. + + // TODO: Integrate into the code. + + //lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str())); + + } + +#endif + } + + // Get the server prefix if the connection was successful. + + if (TestConnectionResult == COCONNECT_OK){ + + if (UsingSSLBypass == true){ + TestConnection.BypassSSLVerification(true); + } + + std::string ReceivedServerPrefix; + + COServerResponse PrefixRequestResult = TestConnection.GetDefaultPrefix(&ReceivedServerPrefix); + + if (UsingSSLBypass == true){ + TestConnection.BypassSSLVerification(true); + } + + } + + TestConnectionResult == COCONNECT_OK ? ResultData->Connected = true : ResultData->Connected = false; + + ResultData->SSLStatus = TestConnection.CanDoSSL(); + ResultData->SSLVerified = TestConnection.SSLVerify(); + ResultData->ValidResponse = TestConnection.HasValidResponse(); + ResultData->AuthPassed = TestConnection.AbleToLogin(); + ResultData->CanProcess = TestConnection.CanDoProcessing(); + ResultData->ErrorMessage = TestConnection.GetErrorMessage(); + + // Post event back confirming the tests. + + wxCommandEvent ResultsEvent(UPDATERESULTS); + ResultsEvent.SetClientData(ResultData); + wxPostEvent(this, ResultsEvent); + +} \ No newline at end of file diff --git a/source/frmNewAccount.cpp b/source/frmNewAccount.cpp index 8b2a879..23ab525 100644 --- a/source/frmNewAccount.cpp +++ b/source/frmNewAccount.cpp @@ -32,6 +32,14 @@ #include "common/svrblist.h" #include "frmInvalidSSLCertificate.h" +DEFINE_EVENT_TYPE(UPDATERESULTS); +DEFINE_EVENT_TYPE(RUNCARDDAV2TEST); + +BEGIN_EVENT_TABLE(frmNewAccount, wxDialog) +EVT_COMMAND(wxID_ANY, UPDATERESULTS, frmNewAccount::UpdateResults) +EVT_COMMAND(wxID_ANY, RUNCARDDAV2TEST, frmNewAccount::RunCardDAV2Test) +END_EVENT_TABLE() + frmNewAccount::frmNewAccount( wxWindow* parent ) : frmNewAccountADT( parent ) @@ -95,7 +103,7 @@ void frmNewAccount::ProcessPrevious( wxCommandEvent& event ) return; } - + // Currently at the Finish screen. tabType->Hide(); @@ -160,7 +168,7 @@ void frmNewAccount::ProcessNext( wxCommandEvent& event ) tabConn->Show(); tabFinish->Hide(); szrNewAccount->RecalcSizes(); - btnPrevious->Enable(); + btnPrevious->Disable(); // Reset screen. @@ -170,280 +178,12 @@ void frmNewAccount::ProcessNext( wxCommandEvent& event ) lblServerSSLValid->SetLabel(wxT("")); lblAbleToLoginResult->SetLabel(wxT("")); lblCardDAVSupportResult->SetLabel(wxT("")); - - // Spawn a thread and check if server supports CardDAV. - - CardDAV CardDAVConn; - - lblServerConnResult->SetLabel(_("Testing...")); - - UseSSL = chkUseSSL->GetValue(); - CardDAVConn.SetupConnection(txtServerAddress->GetValue(), - wxAtoi(txtServerPort->GetValue()), - txtUsername->GetValue(), - txtPassword->GetValue(), - UseSSL); - CardDAVConn.SetupResultBools(&ServerResult, &ServerAction); - -#if defined(__WIN32__) - - BOOL ModifiedCertificateData = FALSE; - -#endif - - // Verify SSL trust first before doing anything. - - if (UseSSL == TRUE){ - - CURLcode sslcode = CardDAVConn.SSLVerifyTest(); - - if (sslcode == CURLE_OK){ - - - - } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){ - - // Certificate is more than likely a self-signed or - // expired certificate so display the invalid - // SSL certificate message. - - // Setup the data to be sent in the wxPostEvent command. - - int SSLResult; - -#if defined(__APPLE__) - - SSLResult = DisplayTrustPanel(&CardDAVConn); - - if (SSLResult != NSOKButton){ - - lblServerConnResult->SetLabel(_("Failed")); - lblServerResponse->SetLabel(_("Not applicable")); - lblServerSSLResult->SetLabel(_("Used")); - lblServerSSLValid->SetLabel(_("No")); - lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str())); - return; - - } else { - - // Evalulate the trust object. - - SecTrustResultType EvalResult = ProcessResultType(&CardDAVConn); - - switch(EvalResult){ - case kSecTrustResultProceed: - lblServerSSLValid->SetLabel(_("Verified")); - break; - case kSecTrustResultConfirm: - lblServerSSLValid->SetLabel(_("Verified (user)")); - break; - default: - lblServerSSLValid->SetLabel(_("Unable to verify")); - } - - lblServerResponse->SetLabel(_("Not applicable")); - lblServerSSLResult->SetLabel(_("Used")); - - if (EvalResult != kSecTrustResultProceed){ - return; - } - - } - -#elif defined(__WIN32__) - - CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&CardDAVConn, (HWND)this->GetHandle()); - - if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)){ - wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog")); - } - - if (ModifiedCertificateData == FALSE){ - - lblServerConnResult->SetLabel(_("Failed")); - lblServerResponse->SetLabel(_("Not applicable")); - lblServerSSLResult->SetLabel(_("Used")); - lblServerSSLValid->SetLabel(_("No")); - lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str())); - return; - - } - -#else - - frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this); - - frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue()); - frmICPtr->ShowModal(); - - SSLResult = frmICPtr->GetResult(); - - // Clean up before processing response. - - delete frmICPtr; - frmICPtr = NULL; - - // Process the response from the user. - - if (SSLResult == 1){ - - // Accept the Certificate. - - CardDAVConn.AllowSelfSignTest(TRUE); - - } else if (SSLResult == 2){ - - // Reject the certificate, abort the task and mark as failed. - - lblServerConnResult->SetLabel(_("Failed")); - lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str())); - return; - - } - -#endif - - } else { - - // Something else happened. Stop the process and - // display an error message instead. - - CURLcode sslcode = CardDAVConn.SSLVerifyTest(); - - lblServerConnResult->SetLabel(_("Failed")); - lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str())); - return; - - } - - } - - std::thread ConnTest(&CardDAV::Connect, &CardDAVConn); - - ConnTest.join(); - -#if defined(__WIN32__) - if (ServerResult == FALSE && ModifiedCertificateData == FALSE){ -#else - if (ServerResult == FALSE){ -#endif - - lblServerConnResult->SetLabel(_("Failed")); - lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage()); - return; - - } else { - - lblServerConnResult->SetLabel(_("Connected")); - - } - - if (CardDAVConn.CanDoSSL() == TRUE){ - - lblServerSSLResult->SetLabel(_("Used")); - - } else { - - lblServerSSLResult->SetLabel(_("Not Used")); - lblServerSSLValid->SetLabel(_("Not Applicable")); - - } - -#if defined(__APPLE__) - - // Evalulate the trust object. - - SecTrustResultType EvalResult = ProcessResultType(&CardDAVConn); - - switch(EvalResult){ - case kSecTrustResultProceed: - lblServerSSLValid->SetLabel(_("Verified")); - break; - case kSecTrustResultConfirm: - lblServerSSLValid->SetLabel(_("Verified (user)")); - break; - default: - lblServerSSLValid->SetLabel(_("Unable to verify")); - } - -#elif defined(__WIN32__) - - if (ModifiedCertificateData == TRUE){ - lblServerSSLValid->SetLabel(_("Verified (user)")); - } else { - lblServerSSLValid->SetLabel(_("Verified")); - } - -#else - - if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){ - - lblServerSSLValid->SetLabel(_("Verified")); - - } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){ - lblServerSSLValid->SetLabel(_("Verified (user)")); - - } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) { - - lblServerSSLValid->SetLabel(_("Unable to verify")); - - } - -#endif - - if (CardDAVConn.CanDoCardDAV() == TRUE){ - - lblCardDAVSupportResult->SetLabel(_("Supported")); - - } else { - - lblCardDAVSupportResult->SetLabel(_("Unsupported")); - SetErrorMessageLabel(); - return; - - } + // Depending on account type, run the test. - if (CardDAVConn.AbleToLogin() == TRUE){ - - lblAbleToLoginResult->SetLabel(_("Yes")); - - } else { - - lblAbleToLoginResult->SetLabel(_("No")); - SetErrorMessageLabel(); - return; - - } - - // Get the address to process CardDAV requests. - - ServerPrefix = CardDAVConn.GetDefaultAddressBookURL(); - - if (CardDAVConn.HasValidResponse() == TRUE){ - - lblServerResponse->SetLabel(_("Yes")); - - } else { - - lblServerResponse->SetLabel(_("No")); - SetErrorMessageLabel(); - return; - - } - - if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE && - - CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){ - - btnNext->Enable(); - - lblConnectionResultText->SetLabel(_("Click on Next to set the account name.")); - - } else { - - SetErrorMessageLabel(); - + if (cmbServerType->GetCurrentSelection() == 1){ + wxCommandEvent RunTest(RUNCARDDAV2TEST); + wxPostEvent(this, RunTest); } } else if (PageSeek == 2){ @@ -728,6 +468,89 @@ void frmNewAccount::UpdateRequirements( wxCommandEvent& event ) } +void frmNewAccount::UpdateResults( wxCommandEvent& event ) +{ + + NewAccountResult *ResultDataPointer = static_cast(event.GetClientData()); + NewAccountResult ResultData = (*ResultDataPointer); + + delete(ResultDataPointer); + ResultDataPointer = nullptr; + bool ServerResult = true; + + // Process the result if the server connected. + + if (ResultData.Connected == false){ + lblServerConnResult->SetLabel(_("Failed")); + ServerResult = false; + } else { + lblServerConnResult->SetLabel(_("Success")); + } + + // Check the result if the server gave a valid response. + + if (ResultData.ValidResponse == false){ + lblServerResponse->SetLabel(_("No")); + ServerResult = false; + } else { + lblServerResponse->SetLabel(_("Yes")); + } + + // Check the result if the server uses SSL. + + if (ResultData.SSLStatus == false){ + lblServerSSLResult->SetLabel(_("No")); + } else { + lblServerSSLResult->SetLabel(_("Yes")); + } + + // Check the server has a valid SSL certificate. + + switch(ResultData.SSLVerified){ + case COSSL_VERIFIED: + lblServerSSLValid->SetLabel(_("Verified")); + break; + case COSSL_VERIFIED_USER: + lblServerSSLValid->SetLabel(_("Verified (User)")); + break; + case COSSL_UNABLETOVERIFY: + lblServerSSLValid->SetLabel(_("Unable to verify")); + ServerResult = false; + break; + case COSSL_NOTAPPLICABLE: + lblServerSSLValid->SetLabel(_("Not applicable")); + break; + } + + // Check if able to log into the server. + + if (ResultData.AuthPassed == false){ + lblAbleToLoginResult->SetLabel(_("No")); + ServerResult = false; + } else { + lblAbleToLoginResult->SetLabel(_("Yes")); + } + + // Check if server has CardDAV support. + + if (ResultData.CanProcess == false){ + lblCardDAVSupportResult->SetLabel(_("No")); + ServerResult = false; + } else { + lblCardDAVSupportResult->SetLabel(_("Yes")); + } + + if (ServerResult == false){ + lblConnectionResultText->SetLabel(wxString::Format(_("An error occured whilst connecting: %s"), ResultData.ErrorMessage)); + } else { + btnNext->Enable(); + lblConnectionResultText->SetLabel(_("Click on Next to set the account name.")); + } + + btnPrevious->Enable(); + +} + void frmNewAccount::SetupPointers(bool *ReloadAccountInc){ // Setup the pointers for the new account window. diff --git a/source/frmNewAccount.h b/source/frmNewAccount.h index d6f0853..b652607 100644 --- a/source/frmNewAccount.h +++ b/source/frmNewAccount.h @@ -26,7 +26,15 @@ #include "AppXestiaAddrBk.h" #include "carddav/carddav.h" +#include "carddav2/carddav2.h" + +#include "frmInvalidSSLCertificate.h" + #include +#include + +DECLARE_EVENT_TYPE(UPDATERESULTS, wxID_ANY); +DECLARE_EVENT_TYPE(RUNCARDDAV2TEST, wxID_ANY); //// end generated include @@ -40,7 +48,9 @@ protected: void ProcessNext( wxCommandEvent& event ); void CloseWindow( wxCommandEvent& event ); void UpdateRequirements( wxCommandEvent& event ); + void UpdateResults( wxCommandEvent& event ); void WriteAccountDetails( wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName ); + void RunCardDAV2Test( wxCommandEvent& event ); #if defined(__APPLE__) int DisplayTrustPanel(CardDAV *Connection); SecTrustResultType ProcessResultType(CardDAV *Connection); @@ -49,12 +59,22 @@ public: /** Constructor */ frmNewAccount( wxWindow* parent ); void SetupPointers(bool *ReloadAccountInc); + DECLARE_EVENT_TABLE() //// end generated class members private: int PageSeek = 0; wxString ServerPrefix; bool *ReloadAccountConfig = NULL; void SetErrorMessageLabel(); + struct NewAccountResult{ + bool Connected = false; + bool SSLStatus = false; + COSSLVerified SSLVerified = COSSL_NORESULT; + bool ValidResponse = false; + bool AuthPassed = false; + bool CanProcess = false; + std::string ErrorMessage = ""; + }; }; #endif // __frmNewAccount__ -- 2.39.2