Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added #defined(__APPLE__) around SetConnectionObject in the CardDAV object.
[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.
75         if (ServerSSL){
77                 union {
78                         struct curl_slist       *certdata;
79                         struct curl_certinfo    *certinfo;
80                 } ptr;
82                 ptr.certdata = NULL;
84                 // Setup two initial connections and attempt to get the certificate data.
86                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
87                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
89                 conncode = (curl_easy_perform(conn));
91                 // Check if the SSL certificate is valid or self-signed or some other
92                 // error occured.
94                 if (conncode == CURLE_OK){
96                         // Connection is OK. Do nothing.
98                 } else if (conncode == CURLE_SSL_CACERT){
100                         // Post message saying SSL certificate is invalid. 
102                         curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
104                 } else {
106                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
107                                 curl_easy_strerror(conncode));
109                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
111                         *ServerResult = FALSE;
112                         return FALSE;
114                 }
116                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
117                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
118                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
119                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
120                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
121                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);             
122                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
123                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
124                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
125                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
126                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
127                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
128                 
129                 if (AllowSelfSign == TRUE){
130                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0L);
131                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0L);
132                 }
134 #if defined(__APPLE__)
135                 
136                 SetConnectionObject(conn);
137         
138 #endif
139                 
140                 conncode = (curl_easy_perform(conn));
141                 
142                 ptr.certdata = NULL;
144                 curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
145                 
146                 if (conncode == CURLE_OK){
148                         // Process the server header response and look for
149                         // 'addressbook' within the DAV header.
151                         wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
152                         wxString wxSHeaderLine;
153                         std::map<int, wxString> DAVHeaderLines;
155                         while (wxSHeaderLines.HasMoreTokens()){
157                                 wxSHeaderLine = wxSHeaderLines.GetNextToken();
159                                 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
161                                         // Look for address book in the line.
163                                         if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
165                                                 HasCalDAVSupport = TRUE;
167                                         }
169                                 }
171                         }
173                         *ServerResult = TRUE;
174                         ValidResponse = TRUE;
175                         AuthPassed = TRUE;
176                         SSLStatus = TRUE;
177                         return TRUE;
179                 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
180                 
181                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
182                                         curl_easy_strerror(conncode));
183                                 
184                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
186                         *ServerResult = TRUE;
187                         ValidResponse = FALSE;
188                         AuthPassed = FALSE;
189                         SSLStatus = TRUE;
190                         return TRUE;
191                 
192                 } else {
194                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
195                                         curl_easy_strerror(conncode));
197                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
199                         *ServerResult = FALSE;
200                         return FALSE;                                   
202                 }
204         } else {
205         
206         // No SSL.
207                 
208                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressNormal.mb_str(wxConvUTF8));
209                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
210                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
211                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);    
212                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
213                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
214                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
215                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
216                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
217                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
218                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
220                 conncode = (curl_easy_perform(conn));
222                 if (conncode == CURLE_OK){
224                         // Process the server header response and look for
225                         // 'addressbook' within the DAV header.
226                         
227                         wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
228                         wxString wxSHeaderLine;
229                         std::map<int, wxString> DAVHeaderLines;
230                         
231                         while (wxSHeaderLines.HasMoreTokens()){
232                         
233                                 wxSHeaderLine = wxSHeaderLines.GetNextToken();
234                                 
235                                 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
236                                 
237                                         // Look for address book in the line.
238                                         
239                                         if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
240                                         
241                                                 HasCalDAVSupport = TRUE;
242                                         
243                                         }
244                                 
245                                 }
246                         
247                         }
249                         *ServerResult = TRUE;
250                         ValidResponse = TRUE;                   
251                         AuthPassed = TRUE;
252                         SSLStatus = FALSE;
253                         return TRUE;
255                 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
256                 
257                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
258                                         curl_easy_strerror(conncode));
259                                         
260                         *ServerResult = TRUE;
261                         ValidResponse = FALSE;
262                         AuthPassed = FALSE;
263                         SSLStatus = FALSE;
264                         return TRUE;
265                 
266                 } else {
268                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
269                                         curl_easy_strerror(conncode));
270                                 
271                         *ServerResult = FALSE;
272                         return FALSE;
274                 }
275                 
276                 // TODO: Double check and make sure HTTP Authentication is possible.
277                 
278         }               
279                 
280         *ServerResult = TRUE;
281         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