X-Git-Url: http://Server1/repobrowser/?p=xestiaab%2F.git;a=blobdiff_plain;f=source%2Fcarddav2%2Fcarddav2.cpp;h=37bbdf9ea3093d72f2d46d9bfc182ee990b12b9a;hp=2159b989702346034936db97dde64f2aa04c2ebc;hb=547ab1fe605cdb4884179ab011dabba4d0bd7bc2;hpb=a578ed436123242f807cb59eae02fcf6c22e2458 diff --git a/source/carddav2/carddav2.cpp b/source/carddav2/carddav2.cpp index 2159b98..37bbdf9 100644 --- a/source/carddav2/carddav2.cpp +++ b/source/carddav2/carddav2.cpp @@ -22,59 +22,96 @@ using namespace std; +CardDAV2::CardDAV2(string ServerAddress, int ServerPort, string ServerUser, string ServerPass, bool ServerSSL){ + + this->ServerAddress = ServerAddress; + this->ServerPort = ServerPort; + this->ServerUser = ServerUser; + this->ServerPass = ServerPass; + this->ServerSSL = ServerSSL; + + TestMode = true; + this->SetupConnectionObject(); + +} + +CardDAV2::CardDAV2(string ServerAddress, int ServerPort, string ServerUser, string ServerPass, bool ServerSSL, string ServerPrefix, string ServerAccount){ + + this->ServerAddress = ServerAddress; + this->ServerPort = ServerPort; + this->ServerUser = ServerUser; + this->ServerPass = ServerPass; + this->ServerSSL = ServerSSL; + this->ServerPrefix = ServerPrefix; + this->ServerAccount = ServerAccount; + + + TestMode = false; + this->SetupConnectionObject(); + +} + size_t CardDAV2::WritebackFunc(char *ptr, size_t size, size_t nmemb, void *stream){ - - return static_cast(stream)->WritebackFuncImplementation(ptr, size, nmemb, stream); + + return static_cast(stream)->CardDAV2Object->WritebackFuncImplementation(ptr, size, nmemb, stream); } - + size_t CardDAV2::WritebackFuncImplementation(char *ptr, size_t size, size_t nmemb, void *stream){ // Writeback function for the CardDAV object. - string *data = static_cast(stream); - data->append(ptr); + CardDAV2PassObject *data = static_cast(stream); + data->DataSetting->append(ptr); // Get the SSL engine pointer and trust if required on certain operating systems. - - if (ServerSSL){ - + + if (data->ServerUsingSSL == true) { + #if defined(__APPLE__) - + const struct curl_tlssessioninfo *TLSInfo; CURLcode TLSCode; CURL *Connection = GetConnectionObject(); TLSCode = curl_easy_getinfo(Connection, CURLINFO_TLS_SSL_PTR, &TLSInfo); - - if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK){ + + if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK) { SSLCopyPeerTrust((SSLContext*)TLSInfo->internals, &SecTrustObject); } - + #elif defined(__WIN32__) const struct curl_tlssessioninfo *TLSInfo; CURLcode TLSCode; - TLSCode = curl_easy_getinfo(ConnectionSession, CURLINFO_TLS_SSL_PTR, &TLSInfo); + TLSCode = curl_easy_getinfo(data->ConnectionSessionObject, CURLINFO_TLS_SSL_PTR, &TLSInfo); - if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK){ + if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK) { // Free the previous certificate data. - CertFreeCertificateContext(CertificateData); + //CertFreeCertificateContext(CertificateData); + + PCCERT_CONTEXT CertificateData; PCtxtHandle SSLHandle = (PCtxtHandle)TLSInfo->internals; SECURITY_STATUS GetData = QueryContextAttributes(SSLHandle, SECPKG_ATTR_REMOTE_CERT_CONTEXT, &CertificateData); + data->SSLContext = CertificateData; + } #endif } - + return size * nmemb; } +void CardDAV2::SetCertificateData() { + +} + CardDAV2::~CardDAV2(){ curl_easy_cleanup(ConnectionSession); @@ -87,7 +124,11 @@ CardDAV2::~CardDAV2(){ #if defined(__WIN32__) - CertFreeCertificateContext(CertificateData); + if (CertificateData != nullptr) { + + CertFreeCertificateContext(CertificateData); + + } #endif @@ -162,13 +203,13 @@ SSLCertCollectionString CardDAV2::BuildSSLCollection(){ } -void CardDAV2::BypassSSLVerification(bool EnableBypass){ +#endif + +void CardDAV2::BypassSSLVerification(bool EnableBypass) { EnableSSLBypass = EnableBypass; SSLSelfSigned = EnableBypass; } -#endif - void CardDAV2::SetupConnectionObject(){ ConnectionSession = curl_easy_init(); } @@ -192,7 +233,7 @@ COConnectResult CardDAV2::Connect(bool DoAuthentication){ } else { SessionResult = curl_easy_perform(ConnectionSession); } - + switch(SessionResult){ case CURLE_OK: case CURLE_HTTP_RETURNED_ERROR: @@ -211,6 +252,18 @@ COConnectResult CardDAV2::Connect(bool DoAuthentication){ break; }; + // Set the certificate data (if required). + +#if defined(__WIN32__) + + if (ServerSSL) { + + CertificateData = PageHeaderObject.SSLContext; + + } + +#endif + // Check if an error occured before continuing. // Check if authentication was successful. @@ -1335,26 +1388,36 @@ void CardDAV2::SetupDefaultParametersNonSSL(bool DoAuthentication){ string ServerAddressURL = "http://" + ServerAddress + ":" + to_string(ServerPort) + "/"; string UsernamePassword = ServerUser + ":" + ServerPass; - + + PageDataObject.CardDAV2Object = this; + PageDataObject.ConnectionSessionObject = ConnectionSession; + PageDataObject.DataSetting = &PageData; + PageDataObject.ServerUsingSSL = false; + + PageHeaderObject.CardDAV2Object = this; + PageHeaderObject.ConnectionSessionObject = ConnectionSession; + PageHeaderObject.DataSetting = &PageHeader; + PageHeaderObject.ServerUsingSSL = false; + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddress.c_str()); curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC); curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60); curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true); curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT); curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageData); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeader); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageDataObject); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeaderObject); curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr); curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr); curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, 0L); - + if (DoAuthentication == true){ curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str()); } else { - curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, ":"); + curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, NULL); } } @@ -1365,16 +1428,26 @@ void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){ string ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + "/"; string UsernamePassword = ServerUser + ":" + ServerPass; - + + PageDataObject.CardDAV2Object = this; + PageDataObject.ConnectionSessionObject = ConnectionSession; + PageDataObject.DataSetting = &PageData; + PageDataObject.ServerUsingSSL = true; + + PageHeaderObject.CardDAV2Object = this; + PageHeaderObject.ConnectionSessionObject = ConnectionSession; + PageHeaderObject.DataSetting = &PageHeader; + PageHeaderObject.ServerUsingSSL = true; + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC); curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60); curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true); curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT); curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageData); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeader); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageDataObject); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeaderObject); curl_easy_setopt(ConnectionSession, CURLOPT_ERRORBUFFER, SessionErrorBuffer); curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(ConnectionSession, CURLOPT_CERTINFO, 1); @@ -1383,14 +1456,14 @@ void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){ curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr); curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr); curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, 0L); - + if (DoAuthentication == true){ curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str()); } else { - curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, ":"); + curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, NULL); } -#if !defined(__WIN32__) +#if !defined(__APPLE__) if (EnableSSLBypass == true){ curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 0); @@ -1450,6 +1523,7 @@ void CardDAV2::ResetResults(){ TaskCompleted = false; ErrorMessage = ""; SessionErrorBuffer[0] = '\0'; + SessionResult = CURLE_OK; PageData = ""; PageHeader = ""; if (HeaderList != nullptr){