Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added copyright and license headers on C++ source and header files in the carddav...
[xestiaab/.git] / source / carddav / carddav-connect.cpp
1 // carddav-connect.cpp - CardDAV Object - Connect subroutines.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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/>
19 #include "carddav.h"
20 #include "../version.h"
21 #include <wx/wx.h>
22 #include <wx/tokenzr.h>
23 #include <wx/ffile.h>
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <map>
27 #include <thread>
28 #include "../vcard/vcard.h"
29 #include "../common/dirs.h"
31 bool CardDAV::Connect(){
33         PageData.Clear();
34         PageHeader.Clear();
36         SSLStatus = TRUE;
37         AuthPassed = TRUE;
38         AbortConnection = FALSE;
40         CURL *conn;
41         CURLcode conncode;
42         wxString ServerAddressURL;
43         wxString ServerAuth;
44         wxString ServerAddressSSL;
45         wxString ServerAddressNormal;   
47         conn = curl_easy_init();
49         struct CardDAVCURLPasser {
50         
51                 CardDAV *Data;
52                 bool HeaderMode = TRUE;
53         
54         } CardDAVHeader, CardDAVFooter;
56         CardDAVHeader.Data = this;
57         CardDAVHeader.HeaderMode = TRUE;
58         
59         CardDAVFooter.Data = this;
60         CardDAVFooter.HeaderMode = FALSE;
62         wxString Data1;
63         wxString Data2;
64         
65         ServerAddressURL = ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");
66         ServerAddressSSL = wxT("https://") + ServerAddressURL;
67         ServerAddressNormal = wxT("http://") + ServerAddressURL;
68         
69         ServerAuth = ServerUser + wxT(":") + ServerPass;
70         
71         // Try SSL first.
74         /*
75         char *ServerAdrSSLChar = new char[(ServerAddressSSL.Length() - 1)];
76         //memset(ServerAdrSSLChar, 0, ServerAddressSSL.Length());
77         strncpy(ServerAdrSSLChar, (const char*)ServerAddressSSL.mb_str(wxConvUTF8), (ServerAddressSSL.Length() - 1));
78         
79         char *ServerAdrNorChar = new char[(ServerAddressNormal.Length() - 1)];
80         //memset(ServerAdrNorChar, 0, ServerAddressSSL.Length());       
81         strncpy(ServerAdrNorChar, (const char*)ServerAddressNormal.mb_str(wxConvUTF8), (ServerAddressNormal.Length() - 1));
83         char *ServerAuthChar = new char[(ServerAuth.Length() - 1)];
84         //memset(ServerAuthChar, 0, ServerAddressSSL.Length()); 
85         strncpy(ServerAuthChar, (const char*)ServerAuth.mb_str(wxConvUTF8), (ServerAuth.Length() - 1));
86         
87         */
88         
89         if (ServerSSL){
91                 union {
92                         struct curl_slist       *certdata;
93                         struct curl_certinfo    *certinfo;
94                 } ptr;
96                 ptr.certdata = NULL;
98                 // Setup two initial connections and attempt to get the certificate data.
100                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
101                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
103                 conncode = (curl_easy_perform(conn));
105                 // Check if the SSL certificate is valid or self-signed or some other
106                 // error occured.
108                 if (conncode == CURLE_OK){
110                         // Connection is OK. Do nothing.
112                 } else if (conncode == CURLE_SSL_CACERT){
114                         // Post message saying SSL certificate is invalid. 
116                         curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
118                 } else {
120                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
121                                 curl_easy_strerror(conncode));
123                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
125                         *ServerResult = FALSE;
126                         return FALSE;
128                 }
130                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
131                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
132                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
133                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
134                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
135                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);             
136                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
137                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
138                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
139                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
140                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
141                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
142                 
143                 if (AllowSelfSign == TRUE){
144                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0L);
145                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0L);
146                 }
148                 conncode = (curl_easy_perform(conn));
149                 
150                 ptr.certdata = NULL;
152                 curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
153                 
154                 if (conncode == CURLE_OK){
156                         // Process the server header response and look for
157                         // 'addressbook' within the DAV header.
159                         wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
160                         wxString wxSHeaderLine;
161                         std::map<int, wxString> DAVHeaderLines;
163                         while (wxSHeaderLines.HasMoreTokens()){
165                                 wxSHeaderLine = wxSHeaderLines.GetNextToken();
167                                 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
169                                         // Look for address book in the line.
171                                         if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
173                                                 HasCalDAVSupport = TRUE;
175                                         }
177                                 }
179                         }
181                         *ServerResult = TRUE;
182                         ValidResponse = TRUE;
183                         AuthPassed = TRUE;
184                         SSLStatus = TRUE;
185                         return TRUE;
187                 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
188                 
189                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
190                                         curl_easy_strerror(conncode));
191                                 
192                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
194                         *ServerResult = TRUE;
195                         ValidResponse = FALSE;
196                         AuthPassed = FALSE;
197                         SSLStatus = TRUE;
198                         return TRUE;
199                 
200                 } else {
202                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
203                                         curl_easy_strerror(conncode));
205                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
207                         *ServerResult = FALSE;
208                         return FALSE;                                   
210                 }
212         } else {
213         
214         // No SSL.
215                 
216                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressNormal.mb_str(wxConvUTF8));
217                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
218                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
219                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);    
220                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
221                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
222                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
223                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
224                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
225                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
226                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
228                 conncode = (curl_easy_perform(conn));
230                 if (conncode == CURLE_OK){
232                         // Process the server header response and look for
233                         // 'addressbook' within the DAV header.
234                         
235                         wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
236                         wxString wxSHeaderLine;
237                         std::map<int, wxString> DAVHeaderLines;
238                         
239                         while (wxSHeaderLines.HasMoreTokens()){
240                         
241                                 wxSHeaderLine = wxSHeaderLines.GetNextToken();
242                                 
243                                 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
244                                 
245                                         // Look for address book in the line.
246                                         
247                                         if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
248                                         
249                                                 HasCalDAVSupport = TRUE;
250                                         
251                                         }
252                                 
253                                 }
254                         
255                         }
257                         *ServerResult = TRUE;
258                         ValidResponse = TRUE;                   
259                         AuthPassed = TRUE;
260                         SSLStatus = FALSE;
261                         return TRUE;
263                 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
264                 
265                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
266                                         curl_easy_strerror(conncode));
267                                         
268                         *ServerResult = TRUE;
269                         ValidResponse = FALSE;
270                         AuthPassed = FALSE;
271                         SSLStatus = FALSE;
272                         return TRUE;
273                 
274                 } else {
276                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
277                                         curl_easy_strerror(conncode));
278                                 
279                         *ServerResult = FALSE;
280                         return FALSE;
282                 }
283                 
284                 // TODO: Double check and make sure HTTP Authentication is possible.
285                 
286         }               
287                 
288         *ServerResult = TRUE;
289         return TRUE;    
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