Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Altered CardDAV object to accommodate for SSL support for OS X (and other OSes in...
[xestiaab/.git] / source / carddav / carddav.cpp
1 // carddav.cpp - Main CardDAV Object file.\r
2 //\r
3 // (c) 2012-2015 Xestia Software Development.\r
4 //\r
5 // This file is part of Xestia Address Book.\r
6 //\r
7 // Xestia Address Book is free software: you can redistribute it and/or modify\r
8 // it under the terms of the GNU General Public License as published by the\r
9 // Free Software Foundation, version 3 of the license.\r
10 //\r
11 // Xestia Address Book is distributed in the hope that it will be useful,\r
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
14 // GNU General Public License for more details.\r
15 //\r
16 // You should have received a copy of the GNU General Public License along\r
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>\r
18 \r
19 #include "carddav.h"\r
20 #include "../version.h"\r
21 #include <wx/wx.h>\r
22 #include <wx/tokenzr.h>\r
23 #include <wx/ffile.h>\r
24 #include <libxml/parser.h>\r
25 #include <libxml/tree.h>\r
26 #include <map>\r
27 #include <thread>\r
28 #include "../vcard/vcard.h"\r
29 #include "../common/dirs.h"\r
30 \r
31 size_t CardDAV::WritebackFunc(char *ptr, size_t size, size_t nmemb, wxString *stream){\r
32         \r
33         // Writeback function for the CardDAV object.\r
34         \r
35         wxString Data;\r
36         Data = wxString::FromUTF8((char *)ptr);\r
37         \r
38         stream->Append(Data);\r
39         \r
40         // Get the SSL engine pointer and trust if required on certain operating systems.\r
41         \r
42 #if defined(__APPLE__)\r
43         \r
44         const struct curl_tlssessioninfo *TLSInfo;\r
45         CURLcode TLSCode;\r
46         CURL *Connection = GetConnectionObject();\r
47         TLSCode = curl_easy_getinfo(Connection, CURLINFO_TLS_SSL_PTR, &TLSInfo);\r
48         \r
49         if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK){\r
50                 SSLCopyPeerTrust((SSLContext*)TLSInfo->internals, &SecTrustObject);\r
51         }\r
52         \r
53 #endif\r
54         \r
55         return size * nmemb;\r
56 \r
57 }\r
58 \r
59 int ProgressFunc(void *clientdata, double TTDown, double NDown, double TTUp, double NUp){\r
60 \r
61         // Progress function for the CardDAV object.\r
62         \r
63         int ProgressRet;\r
64         \r
65         CardDAV *IncCardDAV = static_cast<CardDAV*>(clientdata);\r
66         ProgressRet = IncCardDAV->ProgressFuncProc(clientdata, TTDown, NDown, TTUp, NUp);\r
67         if (ProgressRet != 0){\r
68                 return 1;\r
69         }\r
70         return 0;\r
71 \r
72 }\r
73 \r
74 wxString CardDAV::ServerAddress;\r
75 int CardDAV::ServerPort;\r
76 wxString CardDAV::ServerUser;\r
77 wxString CardDAV::ServerPass;\r
78 wxString CardDAV::ServerPrefix;\r
79 wxString CardDAV::ServerAccount;\r
80 bool CardDAV::ServerSSL;\r
81 bool *CardDAV::ServerResult;\r
82 bool *CardDAV::ServerMonitor;\r
83 bool CardDAV::SSLStatus;\r
84 bool CardDAV::SSLVerified;\r
85 bool CardDAV::ValidResponse;\r
86 bool CardDAV::AuthPassed;\r
87 bool CardDAV::HasCalDAVSupport;\r
88 bool CardDAV::AbortConnection;\r
89 wxString CardDAV::ServerResponse;\r
90 wxString CardDAV::ServerMethod;\r
91 wxString CardDAV::ServerFilenameLocation;\r
92 wxString CardDAV::ServerUploadData;\r
93 wxString CardDAV::ETagData;\r
94 wxString CardDAV::ETagResult;\r
95 bool CardDAV::UploadMode;\r
96 bool CardDAV::EditMode;\r
97 long CardDAV::ItemIndex;\r
98 std::map<int, int> *CardDAV::ActivityListPtr;\r
99 char CardDAV::curlerrbuffer[CURL_ERROR_SIZE];\r
100 SSLCertCollection CardDAV::SSLCertCol;\r
101 int CardDAV::SSLErrorCode;\r
102 int CardDAV::ConnectionErrorCode;\r
103 wxString CardDAV::PageHeader;\r
104 wxString CardDAV::PageData;\r
105 CURLcode CardDAV::claconncode;\r
106 int CardDAV::HTTPErrorCode;\r
107 wxString CardDAV::ErrorMessage;\r
108 SSLCertCollection CardDAV::VerifyCertCollection;\r
109 bool CardDAV::AllowSelfSign;\r
110 SSLContext *CardDAV::SSLContextPointer;\r
111 SecTrustRef CardDAV::SecTrustObject;\r
112 CURL *CardDAV::ConnectionObject;\r
113 \r
114 CardDAV::CardDAV(){\r
115         \r
116         // Setup the CardDAV object.\r
117         \r
118         ServerPort = 8080;\r
119         SSLStatus = FALSE;\r
120         SSLVerified = FALSE;\r
121         AuthPassed = FALSE;\r
122         ValidResponse = FALSE;\r
123         HasCalDAVSupport = FALSE;\r
124         SSLCertCol.SuccessCode = 0;\r
125         AllowSelfSign = FALSE;\r
126 \r
127         AbortConnection = FALSE;\r
128         UploadMode = FALSE;\r
129         EditMode = FALSE;\r
130         HTTPErrorCode = 0;\r
131         \r
132 }\r
133 \r
134 CardDAV::~CardDAV(){\r
135 \r
136         // Destroy the CardDAV object.\r
137         \r
138 }\r
139 \r
140 size_t UploadReadFunc(void *ptr, size_t size, size_t nmemb, void *userdata){\r
141 \r
142         \r
143         // Upload function for the CardDAV object.\r
144         \r
145         struct UploadDataStruc *UploadPtr = (struct UploadDataStruc *)userdata;\r
146 \r
147         if (UploadPtr->sizeleft){\r
148 \r
149                 //MeepMoop->sizeleft--;\r
150                 //return 1;\r
151 \r
152                 UploadPtr->sizeleft--;\r
153                 wxString wxSChar;\r
154 \r
155                 wxSChar = UploadPtr->readptr->Mid(UploadPtr->seek,1);\r
156 \r
157                 //*(char *)ptr = (char)wxSChar.mb_str();\r
158 \r
159                 strncpy((char *)ptr, (const char*)wxSChar.mb_str(wxConvUTF8), 1);\r
160 \r
161                 UploadPtr->seek++;\r
162 \r
163                 return 1;\r
164 \r
165         }\r
166 \r
167         return 0;\r
168 \r
169 }\r
170 \r
171 bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL){\r
172 \r
173         // Setup the CardDAV connection without the prefix/account.\r
174         \r
175         ServerAddress = SvrAddress;\r
176         ServerPort = SvrPort;\r
177         ServerUser = SvrUser;\r
178         ServerPass = SvrPass;\r
179         ServerSSL = SvrSSL;\r
180         \r
181         return TRUE;\r
182 \r
183 }\r
184 \r
185 bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL, wxString SvrPrefix, wxString SvrAccount){\r
186 \r
187         // Setup the CardDAV connection with the prefix/account.\r
188         \r
189         ServerAddress = SvrAddress;\r
190         ServerPort = SvrPort;\r
191         ServerUser = SvrUser;\r
192         ServerPass = SvrPass;\r
193         ServerSSL = SvrSSL;\r
194         ServerPrefix = SvrPrefix;\r
195         ServerAccount = SvrAccount;\r
196 \r
197         return TRUE;\r
198 \r
199 }\r
200 \r
201 bool CardDAV::SetupResultBools(bool *SvrResult, bool *SvrMonitor){\r
202 \r
203         // Setup the result booleans.\r
204         \r
205         ServerResult = SvrResult;\r
206         ServerMonitor = SvrMonitor;\r
207 \r
208         return TRUE;\r
209 \r
210 }\r
211 \r
212 bool CardDAV::HasValidResponse(){\r
213 \r
214         // Check that CardDAV server gave a valid response.\r
215         \r
216         return ValidResponse;\r
217 \r
218 }\r
219 \r
220 bool CardDAV::CanDoCardDAV(){\r
221 \r
222         // Check that the server has CardDAV support.\r
223         \r
224         return HasCalDAVSupport;\r
225 \r
226 }\r
227 \r
228 bool CardDAV::CanDoSSL(){\r
229 \r
230         // Check that the server can do SSL.\r
231         \r
232         return SSLStatus;\r
233 \r
234 }\r
235 \r
236 bool CardDAV::SSLVerify(){\r
237 \r
238         // Check that the server can verify SSL.\r
239         \r
240         return SSLVerified;\r
241 \r
242 }\r
243 \r
244 bool CardDAV::AbleToLogin(){\r
245 \r
246         // Check that the user is able to login.\r
247         \r
248         return AuthPassed;\r
249 \r
250 }\r
251 \r
252 bool CardDAV::IsSelfSigned(){\r
253 \r
254         // Check that self-signed certificates are allowed.\r
255         \r
256         return AllowSelfSign;\r
257 \r
258 }\r
259 \r
260 int CardDAV::ProgressFuncProc(void *clientdata, double TTUp, double NUp, double TTDown, double NDown){\r
261 \r
262         // Progress function processing.\r
263         \r
264         if (AbortConnection == TRUE){\r
265         \r
266                 return -1;\r
267         \r
268         } else {\r
269         \r
270                 return 0;\r
271         \r
272         }\r
273 \r
274 }\r
275 \r
276 void CardDAV::Abort(){\r
277 \r
278         // Abort (close) the connection.\r
279         \r
280         AbortConnection = TRUE;\r
281 \r
282 }\r
283 \r
284 SSLCertCollection CardDAV::GetSSLVerifyResults(){\r
285         \r
286         // Get the SSL verification results.\r
287         \r
288         return VerifyCertCollection;\r
289         \r
290 }\r
291 \r
292 void CardDAV::AllowSelfSignTest(bool AllowSelfSignIn){\r
293         \r
294         // Set the value to enable/disable SSL self-signed certificates.\r
295         \r
296         AllowSelfSign = AllowSelfSignIn;\r
297 }\r
298 \r
299 void CardDAV::GetSSLResults(){\r
300 \r
301         // Get the SSL results.\r
302 \r
303 }\r
304 \r
305 void CardDAV::SetServerFilename(wxString Filename){\r
306 \r
307         // Set the server filename.\r
308         \r
309         ServerFilenameLocation = Filename;\r
310 \r
311 }\r
312 \r
313 wxString CardDAV::GetPageData()\r
314 {\r
315 \r
316         // Get the server page data.\r
317         \r
318         return PageData;\r
319 \r
320 }\r
321 \r
322 wxString CardDAV::ETagValueResult(){\r
323 \r
324         // Get the ETag Result value.\r
325         \r
326         return ETagResult;\r
327 \r
328 }\r
329 \r
330 void CardDAV::SetupData(wxString Method, wxString FilenameLocation, wxString UploadData){\r
331 \r
332         // Setup the data to use with the CardDAV connection.\r
333         \r
334         ServerMethod = Method;\r
335         ServerFilenameLocation = FilenameLocation;\r
336         ServerUploadData = UploadData;\r
337 \r
338         // Check if ServerFilenameLocation has a / at \r
339         // the start and if not then append it.\r
340         \r
341         /*if (ServerFilenameLocation.Left(1) != wxT("/")){\r
342         \r
343                 // Not there so insert.\r
344                 \r
345                 ServerFilenameLocation = wxT("/") + ServerFilenameLocation;\r
346         \r
347         }*/\r
348 \r
349 }\r
350 \r
351 void CardDAV::SetupVariables(std::map<int, int> *actlist, int actindex){\r
352 \r
353         // Setup the variable pointers.\r
354         \r
355         ActivityListPtr = actlist;\r
356         ItemIndex = actindex;\r
357 \r
358 }\r
359 \r
360 wxString CardDAV::GetETagData(){\r
361 \r
362         // Get the ETag data.\r
363         \r
364         return ETagData;\r
365 \r
366 }\r
367 \r
368 void CardDAV::SetUploadMode(bool IncMode){\r
369 \r
370         // Set the upload mode.\r
371         \r
372         UploadMode = IncMode;\r
373 \r
374 }\r
375 \r
376 void CardDAV::SetEditMode(bool EditModeInc){\r
377 \r
378         // Set the edit mode.\r
379         \r
380         EditMode = EditModeInc;\r
381 \r
382 }\r
383 \r
384 int CardDAV::GetResultCode(){\r
385 \r
386         // Get the result code.\r
387         \r
388         return (int)claconncode;\r
389 \r
390 }\r
391 \r
392 int CardDAV::GetHTTPCode(){\r
393 \r
394         // Get the HTTP error code.\r
395         \r
396         return HTTPErrorCode;\r
397 \r
398 }\r
399 \r
400 wxString CardDAV::GetErrorBuffer(){\r
401 \r
402         // Get the error buffer.\r
403         \r
404         wxString ErrorBuffer = wxString::FromUTF8(curlerrbuffer);\r
405 \r
406         return ErrorBuffer;\r
407 \r
408 }\r
409 \r
410 SSLCertCollection CardDAV::BuildSSLCollection(CURL *conn){\r
411 \r
412         // Build and return the SSL collection.\r
413         \r
414         SSLCertCollection SSLCertInfo;\r
415 \r
416         // Grab the certificate data.\r
417 \r
418         union {\r
419                 struct curl_slist *certdata;\r
420                 struct curl_certinfo *certinfo;\r
421         } certptr;\r
422 \r
423         certptr.certdata = NULL;\r
424         \r
425         curl_easy_getinfo(conn, CURLINFO_CERTINFO, &certptr.certinfo);\r
426 \r
427         wxString CertPropName;\r
428         wxString CertPropValue;\r
429 \r
430         for (int i = 0; i < certptr.certinfo->num_of_certs; i++){\r
431 \r
432                 struct curl_slist *slist;\r
433                 SSLCertData SSLCertDataInc;\r
434                 \r
435                 for (slist = certptr.certinfo->certinfo[i]; slist; slist = slist->next){\r
436                         \r
437                         wxStringTokenizer CertDataInc(wxString::FromUTF8(slist->data), ":");\r
438                         \r
439                         // Get first token as the property name.\r
440                         \r
441                         CertPropName = CertDataInc.GetNextToken();\r
442                         \r
443                         // Get remaining tokens as the property value.\r
444                         \r
445                         while(CertDataInc.HasMoreTokens()){\r
446                         \r
447                                 CertPropValue.Append(CertDataInc.GetNextToken());\r
448                         \r
449                         }\r
450                         \r
451                         SSLCertDataInc.CertData.insert(std::make_pair(CertPropName, CertPropValue));\r
452                         CertPropName.clear();\r
453                         CertPropValue.clear();\r
454                         \r
455                 }\r
456         \r
457                 SSLCertInfo.SSLCollection.insert(std::make_pair(i, SSLCertDataInc));\r
458         \r
459         }\r
460         \r
461         return SSLCertInfo;\r
462 \r
463 }\r
464 \r
465 SSLCertCollection CardDAV::GetCertificateData(){\r
466 \r
467         // Pass on the collected certificate data.\r
468 \r
469         return SSLCertCol;\r
470 \r
471 }\r
472 \r
473 wxString CardDAV::GetErrorMessage(){\r
474 \r
475         // Get the error message.\r
476         \r
477         return ErrorMessage;\r
478 \r
479 }\r
480 \r
481 CURL* CardDAV::GetConnectionObject(){\r
482         \r
483         // Get the CardDAV connection object.\r
484         \r
485         return ConnectionObject;\r
486         \r
487 }\r
488 \r
489 void CardDAV::SetConnectionObject(CURL *ConnectionObjectIn){\r
490 \r
491         // Set the connection object.\r
492         \r
493         ConnectionObject = ConnectionObjectIn;\r
494         \r
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy