// carddav.cpp - Main CardDAV Object file. // // (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 "carddav.h" #include "../version.h" #include #include #include #include #include #include #include #include "../vcard/vcard.h" #include "../common/dirs.h" size_t CardDAV::WritebackFunc(char *ptr, size_t size, size_t nmemb, wxString *stream){ // Writeback function for the CardDAV object. wxString Data; Data = wxString::FromUTF8((char *)ptr); stream->Append(Data); // Get the SSL engine pointer and trust if required on certain operating systems. #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){ SSLCopyPeerTrust((SSLContext*)TLSInfo->internals, &SecTrustObject); } #endif return size * nmemb; } int ProgressFunc(void *clientdata, double TTDown, double NDown, double TTUp, double NUp){ // Progress function for the CardDAV object. int ProgressRet; CardDAV *IncCardDAV = static_cast(clientdata); ProgressRet = IncCardDAV->ProgressFuncProc(clientdata, TTDown, NDown, TTUp, NUp); if (ProgressRet != 0){ return 1; } return 0; } wxString CardDAV::ServerAddress; int CardDAV::ServerPort; wxString CardDAV::ServerUser; wxString CardDAV::ServerPass; wxString CardDAV::ServerPrefix; wxString CardDAV::ServerAccount; bool CardDAV::ServerSSL; bool *CardDAV::ServerResult; bool *CardDAV::ServerMonitor; bool CardDAV::SSLStatus; bool CardDAV::SSLVerified; bool CardDAV::ValidResponse; bool CardDAV::AuthPassed; bool CardDAV::HasCalDAVSupport; bool CardDAV::AbortConnection; wxString CardDAV::ServerResponse; wxString CardDAV::ServerMethod; wxString CardDAV::ServerFilenameLocation; wxString CardDAV::ServerUploadData; wxString CardDAV::ETagData; wxString CardDAV::ETagResult; bool CardDAV::UploadMode; bool CardDAV::EditMode; long CardDAV::ItemIndex; std::map *CardDAV::ActivityListPtr; char CardDAV::curlerrbuffer[CURL_ERROR_SIZE]; SSLCertCollection CardDAV::SSLCertCol; int CardDAV::SSLErrorCode; int CardDAV::ConnectionErrorCode; wxString CardDAV::PageHeader; wxString CardDAV::PageData; CURLcode CardDAV::claconncode; int CardDAV::HTTPErrorCode; wxString CardDAV::ErrorMessage; SSLCertCollection CardDAV::VerifyCertCollection; bool CardDAV::AllowSelfSign; #if defined(__APPLE__) SSLContext *CardDAV::SSLContextPointer; SecTrustRef CardDAV::SecTrustObject; CURL *CardDAV::ConnectionObject; #endif CardDAV::CardDAV(){ // Setup the CardDAV object. ServerPort = 8080; SSLStatus = FALSE; SSLVerified = FALSE; AuthPassed = FALSE; ValidResponse = FALSE; HasCalDAVSupport = FALSE; SSLCertCol.SuccessCode = 0; AllowSelfSign = FALSE; AbortConnection = FALSE; UploadMode = FALSE; EditMode = FALSE; HTTPErrorCode = 0; } CardDAV::~CardDAV(){ // Destroy the CardDAV object. } size_t UploadReadFunc(void *ptr, size_t size, size_t nmemb, void *userdata){ // Upload function for the CardDAV object. struct UploadDataStruc *UploadPtr = (struct UploadDataStruc *)userdata; if (UploadPtr->sizeleft){ //MeepMoop->sizeleft--; //return 1; UploadPtr->sizeleft--; wxString wxSChar; wxSChar = UploadPtr->readptr->Mid(UploadPtr->seek,1); //*(char *)ptr = (char)wxSChar.mb_str(); strncpy((char *)ptr, (const char*)wxSChar.mb_str(wxConvUTF8), 1); UploadPtr->seek++; return 1; } return 0; } bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL){ // Setup the CardDAV connection without the prefix/account. ServerAddress = SvrAddress; ServerPort = SvrPort; ServerUser = SvrUser; ServerPass = SvrPass; ServerSSL = SvrSSL; return TRUE; } bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL, wxString SvrPrefix, wxString SvrAccount){ // Setup the CardDAV connection with the prefix/account. ServerAddress = SvrAddress; ServerPort = SvrPort; ServerUser = SvrUser; ServerPass = SvrPass; ServerSSL = SvrSSL; ServerPrefix = SvrPrefix; ServerAccount = SvrAccount; return TRUE; } bool CardDAV::SetupResultBools(bool *SvrResult, bool *SvrMonitor){ // Setup the result booleans. ServerResult = SvrResult; ServerMonitor = SvrMonitor; return TRUE; } bool CardDAV::HasValidResponse(){ // Check that CardDAV server gave a valid response. return ValidResponse; } bool CardDAV::CanDoCardDAV(){ // Check that the server has CardDAV support. return HasCalDAVSupport; } bool CardDAV::CanDoSSL(){ // Check that the server can do SSL. return SSLStatus; } bool CardDAV::SSLVerify(){ // Check that the server can verify SSL. return SSLVerified; } bool CardDAV::AbleToLogin(){ // Check that the user is able to login. return AuthPassed; } bool CardDAV::IsSelfSigned(){ // Check that self-signed certificates are allowed. return AllowSelfSign; } int CardDAV::ProgressFuncProc(void *clientdata, double TTUp, double NUp, double TTDown, double NDown){ // Progress function processing. if (AbortConnection == TRUE){ return -1; } else { return 0; } } void CardDAV::Abort(){ // Abort (close) the connection. AbortConnection = TRUE; } SSLCertCollection CardDAV::GetSSLVerifyResults(){ // Get the SSL verification results. return VerifyCertCollection; } void CardDAV::AllowSelfSignTest(bool AllowSelfSignIn){ // Set the value to enable/disable SSL self-signed certificates. AllowSelfSign = AllowSelfSignIn; } void CardDAV::GetSSLResults(){ // Get the SSL results. } void CardDAV::SetServerFilename(wxString Filename){ // Set the server filename. ServerFilenameLocation = Filename; } wxString CardDAV::GetPageData() { // Get the server page data. return PageData; } wxString CardDAV::ETagValueResult(){ // Get the ETag Result value. return ETagResult; } void CardDAV::SetupData(wxString Method, wxString FilenameLocation, wxString UploadData){ // Setup the data to use with the CardDAV connection. ServerMethod = Method; ServerFilenameLocation = FilenameLocation; ServerUploadData = UploadData; // Check if ServerFilenameLocation has a / at // the start and if not then append it. /*if (ServerFilenameLocation.Left(1) != wxT("/")){ // Not there so insert. ServerFilenameLocation = wxT("/") + ServerFilenameLocation; }*/ } void CardDAV::SetupVariables(std::map *actlist, int actindex){ // Setup the variable pointers. ActivityListPtr = actlist; ItemIndex = actindex; } wxString CardDAV::GetETagData(){ // Get the ETag data. return ETagData; } void CardDAV::SetUploadMode(bool IncMode){ // Set the upload mode. UploadMode = IncMode; } void CardDAV::SetEditMode(bool EditModeInc){ // Set the edit mode. EditMode = EditModeInc; } int CardDAV::GetResultCode(){ // Get the result code. return (int)claconncode; } int CardDAV::GetHTTPCode(){ // Get the HTTP error code. return HTTPErrorCode; } wxString CardDAV::GetErrorBuffer(){ // Get the error buffer. wxString ErrorBuffer = wxString::FromUTF8(curlerrbuffer); return ErrorBuffer; } SSLCertCollection CardDAV::BuildSSLCollection(CURL *conn){ // Build and return the SSL collection. SSLCertCollection SSLCertInfo; // Grab the certificate data. union { struct curl_slist *certdata; struct curl_certinfo *certinfo; } certptr; certptr.certdata = NULL; curl_easy_getinfo(conn, CURLINFO_CERTINFO, &certptr.certinfo); wxString CertPropName; wxString CertPropValue; for (int i = 0; i < certptr.certinfo->num_of_certs; i++){ struct curl_slist *slist; SSLCertData SSLCertDataInc; for (slist = certptr.certinfo->certinfo[i]; slist; slist = slist->next){ wxStringTokenizer CertDataInc(wxString::FromUTF8(slist->data), ":"); // Get first token as the property name. CertPropName = CertDataInc.GetNextToken(); // Get remaining tokens as the property value. while(CertDataInc.HasMoreTokens()){ CertPropValue.Append(CertDataInc.GetNextToken()); } SSLCertDataInc.CertData.insert(std::make_pair(CertPropName, CertPropValue)); CertPropName.clear(); CertPropValue.clear(); } SSLCertInfo.SSLCollection.insert(std::make_pair(i, SSLCertDataInc)); } return SSLCertInfo; } SSLCertCollection CardDAV::GetCertificateData(){ // Pass on the collected certificate data. return SSLCertCol; } wxString CardDAV::GetErrorMessage(){ // Get the error message. return ErrorMessage; } #if defined(__APPLE__) CURL* CardDAV::GetConnectionObject(){ // Get the CardDAV connection object. return ConnectionObject; } void CardDAV::SetConnectionObject(CURL *ConnectionObjectIn){ // Set the connection object. ConnectionObject = ConnectionObjectIn; } #endif