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;
78 struct curl_slist *certdata;
79 struct curl_certinfo *certinfo;
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
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);
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;
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);
129 if (AllowSelfSign == TRUE){
130 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0L);
131 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0L);
134 conncode = (curl_easy_perform(conn));
138 curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
140 if (conncode == CURLE_OK){
142 // Process the server header response and look for
143 // 'addressbook' within the DAV header.
145 wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
146 wxString wxSHeaderLine;
147 std::map<int, wxString> DAVHeaderLines;
149 while (wxSHeaderLines.HasMoreTokens()){
151 wxSHeaderLine = wxSHeaderLines.GetNextToken();
153 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
155 // Look for address book in the line.
157 if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
159 HasCalDAVSupport = TRUE;
167 *ServerResult = TRUE;
168 ValidResponse = TRUE;
173 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
175 fprintf(stderr, "curl_easy_perform() failed: %s\n",
176 curl_easy_strerror(conncode));
178 ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
180 *ServerResult = TRUE;
181 ValidResponse = FALSE;
188 fprintf(stderr, "curl_easy_perform() failed: %s\n",
189 curl_easy_strerror(conncode));
191 ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
193 *ServerResult = FALSE;
202 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressNormal.mb_str(wxConvUTF8));
203 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
204 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
205 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
206 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
207 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
208 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
209 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
210 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
211 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
212 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
214 conncode = (curl_easy_perform(conn));
216 if (conncode == CURLE_OK){
218 // Process the server header response and look for
219 // 'addressbook' within the DAV header.
221 wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
222 wxString wxSHeaderLine;
223 std::map<int, wxString> DAVHeaderLines;
225 while (wxSHeaderLines.HasMoreTokens()){
227 wxSHeaderLine = wxSHeaderLines.GetNextToken();
229 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
231 // Look for address book in the line.
233 if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
235 HasCalDAVSupport = TRUE;
243 *ServerResult = TRUE;
244 ValidResponse = TRUE;
249 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
251 fprintf(stderr, "curl_easy_perform() failed: %s\n",
252 curl_easy_strerror(conncode));
254 *ServerResult = TRUE;
255 ValidResponse = FALSE;
262 fprintf(stderr, "curl_easy_perform() failed: %s\n",
263 curl_easy_strerror(conncode));
265 *ServerResult = FALSE;
270 // TODO: Double check and make sure HTTP Authentication is possible.
274 *ServerResult = TRUE;