1 // carddav-connect.cpp - CardDAV Object - Connect 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 bool CardDAV::Connect(){
33 // Connect to the CardDAV server.
40 AbortConnection = FALSE;
44 wxString ServerAddressURL;
46 wxString ServerAddressSSL;
47 wxString ServerAddressNormal;
49 conn = curl_easy_init();
51 struct CardDAVCURLPasser {
54 bool HeaderMode = TRUE;
56 } CardDAVHeader, CardDAVFooter;
58 CardDAVHeader.Data = this;
59 CardDAVHeader.HeaderMode = TRUE;
61 CardDAVFooter.Data = this;
62 CardDAVFooter.HeaderMode = FALSE;
67 ServerAddressURL = ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");
68 ServerAddressSSL = wxT("https://") + ServerAddressURL;
69 ServerAddressNormal = wxT("http://") + ServerAddressURL;
71 ServerAuth = ServerUser + wxT(":") + ServerPass;
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));
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));
94 struct curl_slist *certdata;
95 struct curl_certinfo *certinfo;
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
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);
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;
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);
145 if (AllowSelfSign == TRUE){
146 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0L);
147 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0L);
150 conncode = (curl_easy_perform(conn));
154 curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
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;
183 *ServerResult = TRUE;
184 ValidResponse = TRUE;
189 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
191 fprintf(stderr, "curl_easy_perform() failed: %s\n",
192 curl_easy_strerror(conncode));
194 ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
196 *ServerResult = TRUE;
197 ValidResponse = FALSE;
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;
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.
237 wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
238 wxString wxSHeaderLine;
239 std::map<int, wxString> DAVHeaderLines;
241 while (wxSHeaderLines.HasMoreTokens()){
243 wxSHeaderLine = wxSHeaderLines.GetNextToken();
245 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
247 // Look for address book in the line.
249 if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
251 HasCalDAVSupport = TRUE;
259 *ServerResult = TRUE;
260 ValidResponse = TRUE;
265 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
267 fprintf(stderr, "curl_easy_perform() failed: %s\n",
268 curl_easy_strerror(conncode));
270 *ServerResult = TRUE;
271 ValidResponse = FALSE;
278 fprintf(stderr, "curl_easy_perform() failed: %s\n",
279 curl_easy_strerror(conncode));
281 *ServerResult = FALSE;
286 // TODO: Double check and make sure HTTP Authentication is possible.
290 *ServerResult = TRUE;