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 #if defined(__APPLE__)
136 SetConnectionObject(conn);
140 conncode = (curl_easy_perform(conn));
144 curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
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;
173 *ServerResult = TRUE;
174 ValidResponse = TRUE;
179 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
181 fprintf(stderr, "curl_easy_perform() failed: %s\n",
182 curl_easy_strerror(conncode));
184 ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
186 *ServerResult = TRUE;
187 ValidResponse = FALSE;
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;
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.
227 wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
228 wxString wxSHeaderLine;
229 std::map<int, wxString> DAVHeaderLines;
231 while (wxSHeaderLines.HasMoreTokens()){
233 wxSHeaderLine = wxSHeaderLines.GetNextToken();
235 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
237 // Look for address book in the line.
239 if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
241 HasCalDAVSupport = TRUE;
249 *ServerResult = TRUE;
250 ValidResponse = TRUE;
255 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
257 fprintf(stderr, "curl_easy_perform() failed: %s\n",
258 curl_easy_strerror(conncode));
260 *ServerResult = TRUE;
261 ValidResponse = FALSE;
268 fprintf(stderr, "curl_easy_perform() failed: %s\n",
269 curl_easy_strerror(conncode));
271 *ServerResult = FALSE;
276 // TODO: Double check and make sure HTTP Authentication is possible.
280 *ServerResult = TRUE;