Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added comment to describe Connect in carddav/carddav-connect.cpp
[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         // Connect to the CardDAV server.
34         
35         PageData.Clear();
36         PageHeader.Clear();
38         SSLStatus = TRUE;
39         AuthPassed = TRUE;
40         AbortConnection = FALSE;
42         CURL *conn;
43         CURLcode conncode;
44         wxString ServerAddressURL;
45         wxString ServerAuth;
46         wxString ServerAddressSSL;
47         wxString ServerAddressNormal;   
49         conn = curl_easy_init();
51         struct CardDAVCURLPasser {
52         
53                 CardDAV *Data;
54                 bool HeaderMode = TRUE;
55         
56         } CardDAVHeader, CardDAVFooter;
58         CardDAVHeader.Data = this;
59         CardDAVHeader.HeaderMode = TRUE;
60         
61         CardDAVFooter.Data = this;
62         CardDAVFooter.HeaderMode = FALSE;
64         wxString Data1;
65         wxString Data2;
66         
67         ServerAddressURL = ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");
68         ServerAddressSSL = wxT("https://") + ServerAddressURL;
69         ServerAddressNormal = wxT("http://") + ServerAddressURL;
70         
71         ServerAuth = ServerUser + wxT(":") + ServerPass;
72         
73         // Try SSL first.
76         /*
77         char *ServerAdrSSLChar = new char[(ServerAddressSSL.Length() - 1)];
78         //memset(ServerAdrSSLChar, 0, ServerAddressSSL.Length());
79         strncpy(ServerAdrSSLChar, (const char*)ServerAddressSSL.mb_str(wxConvUTF8), (ServerAddressSSL.Length() - 1));
80         
81         char *ServerAdrNorChar = new char[(ServerAddressNormal.Length() - 1)];
82         //memset(ServerAdrNorChar, 0, ServerAddressSSL.Length());       
83         strncpy(ServerAdrNorChar, (const char*)ServerAddressNormal.mb_str(wxConvUTF8), (ServerAddressNormal.Length() - 1));
85         char *ServerAuthChar = new char[(ServerAuth.Length() - 1)];
86         //memset(ServerAuthChar, 0, ServerAddressSSL.Length()); 
87         strncpy(ServerAuthChar, (const char*)ServerAuth.mb_str(wxConvUTF8), (ServerAuth.Length() - 1));
88         
89         */
90         
91         if (ServerSSL){
93                 union {
94                         struct curl_slist       *certdata;
95                         struct curl_certinfo    *certinfo;
96                 } ptr;
98                 ptr.certdata = NULL;
100                 // Setup two initial connections and attempt to get the certificate data.
102                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
103                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
105                 conncode = (curl_easy_perform(conn));
107                 // Check if the SSL certificate is valid or self-signed or some other
108                 // error occured.
110                 if (conncode == CURLE_OK){
112                         // Connection is OK. Do nothing.
114                 } else if (conncode == CURLE_SSL_CACERT){
116                         // Post message saying SSL certificate is invalid. 
118                         curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
120                 } else {
122                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
123                                 curl_easy_strerror(conncode));
125                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
127                         *ServerResult = FALSE;
128                         return FALSE;
130                 }
132                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
133                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
134                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
135                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
136                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
137                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);             
138                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
139                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
140                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
141                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
142                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
143                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
144                 
145                 if (AllowSelfSign == TRUE){
146                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0L);
147                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0L);
148                 }
150                 conncode = (curl_easy_perform(conn));
151                 
152                 ptr.certdata = NULL;
154                 curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
155                 
156                 if (conncode == CURLE_OK){
158                         // Process the server header response and look for
159                         // 'addressbook' within the DAV header.
161                         wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
162                         wxString wxSHeaderLine;
163                         std::map<int, wxString> DAVHeaderLines;
165                         while (wxSHeaderLines.HasMoreTokens()){
167                                 wxSHeaderLine = wxSHeaderLines.GetNextToken();
169                                 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
171                                         // Look for address book in the line.
173                                         if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
175                                                 HasCalDAVSupport = TRUE;
177                                         }
179                                 }
181                         }
183                         *ServerResult = TRUE;
184                         ValidResponse = TRUE;
185                         AuthPassed = TRUE;
186                         SSLStatus = TRUE;
187                         return TRUE;
189                 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
190                 
191                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
192                                         curl_easy_strerror(conncode));
193                                 
194                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
196                         *ServerResult = TRUE;
197                         ValidResponse = FALSE;
198                         AuthPassed = FALSE;
199                         SSLStatus = TRUE;
200                         return TRUE;
201                 
202                 } else {
204                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
205                                         curl_easy_strerror(conncode));
207                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
209                         *ServerResult = FALSE;
210                         return FALSE;                                   
212                 }
214         } else {
215         
216         // No SSL.
217                 
218                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressNormal.mb_str(wxConvUTF8));
219                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
220                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
221                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);    
222                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
223                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
224                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
225                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
226                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
227                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
228                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
230                 conncode = (curl_easy_perform(conn));
232                 if (conncode == CURLE_OK){
234                         // Process the server header response and look for
235                         // 'addressbook' within the DAV header.
236                         
237                         wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
238                         wxString wxSHeaderLine;
239                         std::map<int, wxString> DAVHeaderLines;
240                         
241                         while (wxSHeaderLines.HasMoreTokens()){
242                         
243                                 wxSHeaderLine = wxSHeaderLines.GetNextToken();
244                                 
245                                 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
246                                 
247                                         // Look for address book in the line.
248                                         
249                                         if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
250                                         
251                                                 HasCalDAVSupport = TRUE;
252                                         
253                                         }
254                                 
255                                 }
256                         
257                         }
259                         *ServerResult = TRUE;
260                         ValidResponse = TRUE;                   
261                         AuthPassed = TRUE;
262                         SSLStatus = FALSE;
263                         return TRUE;
265                 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
266                 
267                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
268                                         curl_easy_strerror(conncode));
269                                         
270                         *ServerResult = TRUE;
271                         ValidResponse = FALSE;
272                         AuthPassed = FALSE;
273                         SSLStatus = FALSE;
274                         return TRUE;
275                 
276                 } else {
278                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
279                                         curl_easy_strerror(conncode));
280                                 
281                         *ServerResult = FALSE;
282                         return FALSE;
284                 }
285                 
286                 // TODO: Double check and make sure HTTP Authentication is possible.
287                 
288         }               
289                 
290         *ServerResult = TRUE;
291         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