1 // carddav-servercontact.cpp - CardDAV Object - Server Contact subroutines.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
20 #include "../version.h"
22 #include <wx/tokenzr.h>
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
28 #include "../vcard/vcard.h"
29 #include "../common/dirs.h"
31 void CardDAV::GetServerContactData()
39 AbortConnection = FALSE;
41 wxString ServerCertFilename;
42 bool MatchingCert = FALSE;
46 wxString ServerAddressURL;
48 wxString ServerAddressSSL;
49 wxString ServerAddressNormal;
51 conn = curl_easy_init();
53 struct CardDAVCURLPasser {
56 bool HeaderMode = TRUE;
58 } CardDAVHeader, CardDAVFooter;
60 CardDAVHeader.Data = this;
61 CardDAVHeader.HeaderMode = TRUE;
63 CardDAVFooter.Data = this;
64 CardDAVFooter.HeaderMode = FALSE;
69 ServerAddressURL = ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/") + ServerPrefix + ServerFilenameLocation;
70 ServerAddressSSL = wxT("https://") + ServerAddressURL;
71 ServerAddressNormal = wxT("http://") + ServerAddressURL;
73 ServerAuth = ServerUser + wxT(":") + ServerPass;
79 char *ServerAdrSSLChar = new char[(ServerAddressSSL.Length() - 1)];
80 //memset(ServerAdrSSLChar, 0, ServerAddressSSL.Length());
81 strncpy(ServerAdrSSLChar, (const char*)ServerAddressSSL.mb_str(wxConvUTF8), (ServerAddressSSL.Length() - 1));
83 char *ServerAdrNorChar = new char[(ServerAddressNormal.Length() - 1)];
84 //memset(ServerAdrNorChar, 0, ServerAddressSSL.Length());
85 strncpy(ServerAdrNorChar, (const char*)ServerAddressNormal.mb_str(wxConvUTF8), (ServerAddressNormal.Length() - 1));
87 char *ServerAuthChar = new char[(ServerAuth.Length() - 1)];
88 //memset(ServerAuthChar, 0, ServerAddressSSL.Length());
89 strncpy(ServerAuthChar, (const char*)ServerAuth.mb_str(wxConvUTF8), (ServerAuth.Length() - 1));
93 //std::string WriteDataString = std::string(ServerUploadData.mb_str());
95 std::map<int,int>::iterator ActIter;
96 struct UploadDataStruc UploadData;
99 ActIter = ActivityListPtr->find((int)ItemIndex);
101 //ActIter->second = 1;
103 /*wxString CardDAVDataQuery = wxT("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
104 CardDAVDataQuery.Append(wxT("<C:addressbook-multiget xmlns:D=\"DAV:\"\r\n"));
105 CardDAVDataQuery.Append(wxT(" xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\r\n"));
106 CardDAVDataQuery.Append(wxT("<D:prop><D:getetag/>\r\n"));
107 CardDAVDataQuery.Append(wxT("<C:address-data>\r\n"));
108 CardDAVDataQuery.Append(wxT(" <C:allprop/>\r\n"));
109 CardDAVDataQuery.Append(wxT("</C:address-data></D:prop>\r\n"));
110 CardDAVDataQuery.Append(wxT("</C:addressbook-multiget>"));*/
114 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
115 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
116 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
117 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
118 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
119 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
120 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
121 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
122 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
123 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
124 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
126 //UploadData.readptr = &CardDAVDataQuery;
127 //UploadData.sizeleft = CardDAVDataQuery.Len();
128 //curl_easy_setopt(conn, CURLOPT_UPLOAD, 1);
129 //curl_easy_setopt(conn, CURLOPT_READDATA, &UploadData);
130 //curl_easy_setopt(conn, CURLOPT_READFUNCTION, UploadReadFunc);
132 ServerCertFilename = GetAccountDir(ServerAccount, TRUE);
134 if (wxFile::Exists(ServerCertFilename) == TRUE){
136 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 1);
137 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 2);
138 curl_easy_setopt(conn, CURLOPT_CAINFO, (const char*)ServerCertFilename.mb_str(wxConvUTF8));
142 claconncode = (curl_easy_perform(conn));
144 // If CURLE_PEER_FAILED_VERIFICATION is returned, retry without
145 // the local certificate in use.
147 if (claconncode == CURLE_PEER_FAILED_VERIFICATION){
149 curl_easy_cleanup(conn);
150 conn = curl_easy_init();
152 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
153 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
154 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
155 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
156 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
157 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
158 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
159 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
160 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
161 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
162 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
164 //UploadData.readptr = &CardDAVDataQuery;
165 //UploadData.sizeleft = CardDAVDataQuery.Len();
167 claconncode = (curl_easy_perform(conn));
169 // If claconncode is CURLE_OK then delete the certificate file as that
170 // is no longer needed.
172 if (claconncode == CURLE_OK){
174 // Delete the certificate file.
176 wxRemoveFile(ServerCertFilename);
182 // Check if it fails with a CURLE_SSL_CACERT then compare
183 // the certificates as PEM files.
185 if (claconncode == CURLE_SSL_CACERT && wxFile::Exists(ServerCertFilename) == TRUE){
187 //curl_easy_cleanup(conn);
188 //conn = curl_easy_init();
191 sslerrconn = curl_easy_init();
192 CURLcode sslerrconncode;
194 //claconncode = (curl_easy_perform(conn));
196 wxString ServerAddressOnly = wxT("https://") + ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");
198 curl_easy_setopt(sslerrconn, CURLOPT_URL, (const char*)ServerAddressOnly.mb_str(wxConvUTF8));
199 curl_easy_setopt(sslerrconn, CURLOPT_NOPROGRESS, 1L);
200 curl_easy_setopt(sslerrconn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
201 curl_easy_setopt(sslerrconn, CURLOPT_TIMEOUT, 60);
202 curl_easy_setopt(sslerrconn, CURLOPT_FAILONERROR, TRUE);
203 curl_easy_setopt(sslerrconn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
204 curl_easy_setopt(sslerrconn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
205 curl_easy_setopt(sslerrconn, CURLOPT_WRITEFUNCTION, WritebackFunc);
206 curl_easy_setopt(sslerrconn, CURLOPT_WRITEDATA, &PageData);
207 curl_easy_setopt(sslerrconn, CURLOPT_WRITEHEADER, &PageHeader);
208 curl_easy_setopt(sslerrconn, CURLOPT_NOSIGNAL, 1);
210 //UploadData.readptr = &CardDAVDataQuery;
211 //UploadData.sizeleft = CardDAVDataQuery.Len();
212 curl_easy_setopt(sslerrconn, CURLOPT_SSL_VERIFYPEER, 0);
213 curl_easy_setopt(sslerrconn, CURLOPT_SSL_VERIFYHOST, 0);
214 curl_easy_setopt(sslerrconn, CURLOPT_CERTINFO, 1);
216 wxString SSLLocalData;
217 wxString SSLServerData;
219 sslerrconncode = (curl_easy_perform(sslerrconn));
221 SSLCertCol = BuildSSLCollection(sslerrconn);
222 std::map<int, SSLCertData>::iterator SSLCDIter = SSLCertCol.SSLCollection.find(0);
223 std::multimap<wxString,wxString>::iterator SSLDataIter = SSLCDIter->second.CertData.find(wxT("Cert"));
225 wxFFile SSLLocalFile;
227 #if wxABI_VERSION < 20900
228 SSLLocalFile.Open(ServerCertFilename.c_str(), wxT("r"));
230 SSLLocalFile.Open(ServerCertFilename, wxT("r"));
233 // Load the recovery database for tasks not done.
235 if (SSLLocalFile.IsOpened() == TRUE){
237 SSLLocalFile.ReadAll(&SSLLocalData, wxConvAuto());
242 SSLServerData = SSLDataIter->second;
244 if (SSLLocalData == SSLServerData){
246 // Server key matches with local key so retry with CURLOPT_SSL_VERIFYPEER
247 // and CURLOPT_SSL_VERIFYHOST off.
249 curl_easy_cleanup(conn);
250 conn = curl_easy_init();
255 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
256 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
257 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
258 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
259 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
260 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
261 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
262 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
263 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
264 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
265 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
267 //UploadData.readptr = &CardDAVDataQuery;
268 //UploadData.sizeleft = CardDAVDataQuery.Len();
269 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0);
270 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0);
272 claconncode = (curl_easy_perform(conn));
278 if (MatchingCert == FALSE){
280 claconncode = CURLE_SSL_CACERT;
285 curl_easy_cleanup(sslerrconn);
289 // Sort out SSL error.
291 // When SSL cert error occurs, connect again and fetch certificates.
292 // Display a message to the user explaining that an invalid
293 // certificate has been given and let the user decide what
296 if (claconncode == CURLE_OK){
298 } else if (claconncode == CURLE_SSL_CACERT || claconncode == CURLE_PEER_FAILED_VERIFICATION){
301 sslerrconn = curl_easy_init();
302 CURLcode sslerrconncode;
304 wxString ServerAddressOnly = wxT("https://") + ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");
306 // Replace conn with sslerrconn!
308 curl_easy_setopt(sslerrconn, CURLOPT_URL, (const char*)ServerAddressOnly.mb_str(wxConvUTF8));
309 curl_easy_setopt(sslerrconn, CURLOPT_NOPROGRESS, 0);
310 curl_easy_setopt(sslerrconn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
311 curl_easy_setopt(sslerrconn, CURLOPT_TIMEOUT, 60);
312 curl_easy_setopt(sslerrconn, CURLOPT_FAILONERROR, TRUE);
313 curl_easy_setopt(sslerrconn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
314 curl_easy_setopt(sslerrconn, CURLOPT_WRITEFUNCTION, WritebackFunc);
315 curl_easy_setopt(sslerrconn, CURLOPT_WRITEDATA, &PageData);
316 curl_easy_setopt(sslerrconn, CURLOPT_WRITEHEADER, &PageHeader);
317 curl_easy_setopt(sslerrconn, CURLOPT_PROGRESSDATA, this);
318 curl_easy_setopt(sslerrconn, CURLOPT_PROGRESSFUNCTION, ProgressFunc);
319 curl_easy_setopt(sslerrconn, CURLOPT_NOSIGNAL, 1);
320 curl_easy_setopt(sslerrconn, CURLOPT_SSL_VERIFYPEER, 0);
321 curl_easy_setopt(sslerrconn, CURLOPT_CERTINFO, 1);
323 sslerrconncode = (curl_easy_perform(sslerrconn));
325 SSLCertCol = BuildSSLCollection(sslerrconn);
326 SSLCertCol.SuccessCode = 1;
330 } else if (claconncode == CURLE_HTTP_RETURNED_ERROR){
332 fprintf(stderr, "curl_easy_perform() failed: %s\n",
333 curl_easy_strerror(claconncode));
335 curl_easy_getinfo(conn, CURLINFO_RESPONSE_CODE, &http_code);
336 fprintf(stderr, "Error code was: %d\n", http_code);
342 fprintf(stderr, "curl_easy_perform() failed: %s\n",
343 curl_easy_strerror(claconncode));
345 curl_easy_getinfo(conn, CURLINFO_RESPONSE_CODE, &http_code);
346 fprintf(stderr, "Error code was: %d\n", http_code);
356 wxString EmptyString;
358 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressNormal.mb_str(wxConvUTF8));
359 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
360 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
361 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
362 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
363 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
364 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
365 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
366 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
367 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
368 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
370 //UploadData.readptr = &CardDAVDataQuery;
371 //UploadData.sizeleft = CardDAVDataQuery.Len();
372 //curl_easy_setopt(conn, CURLOPT_UPLOAD, 1);
373 //curl_easy_setopt(conn, CURLOPT_READDATA, &UploadData);
374 //curl_easy_setopt(conn, CURLOPT_READFUNCTION, UploadReadFunc);
376 //UploadData.readptr = &CardDAVDataQuery;
377 //UploadData.sizeleft = CardDAVDataQuery.Len();
378 //curl_easy_setopt(conn, CURLOPT_UPLOAD, 1);
379 //curl_easy_setopt(conn, CURLOPT_READDATA, &UploadData);
380 //curl_easy_setopt(conn, CURLOPT_READFUNCTION, UploadReadFunc);
382 //curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writefunc);
387 conncode = (curl_easy_perform(conn));
389 if (conncode == CURLE_OK){
391 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
393 fprintf(stderr, "curl_easy_perform() failed: %s\n",
394 curl_easy_strerror(conncode));
396 fprintf(stderr, "curl_easy_perform() HTTP code was: %i\n",
403 fprintf(stderr, "curl_easy_perform() failed: %s\n",
404 curl_easy_strerror(conncode));