569c0e2197120a4b65864b13b12ab7cecd888e7c
[ardour.git] / gtk2_ardour / ardour_http.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <cstring>
23
24 #include <glibmm.h>
25
26 #include "pbd/compose.h"
27 #include "pbd/i18n.h"
28 #include "pbd/error.h"
29
30 #include "ardour_http.h"
31
32 #ifdef WAF_BUILD
33 #include "gtk2ardour-version.h"
34 #endif
35
36 #ifndef ARDOUR_CURL_TIMEOUT
37 #define ARDOUR_CURL_TIMEOUT (60)
38 #endif
39
40 using namespace ArdourCurl;
41
42 const char* HttpGet::ca_path = NULL;
43 const char* HttpGet::ca_info = NULL;
44
45 void
46 HttpGet::setup_certificate_paths ()
47 {
48         /* this is only needed for Linux Bundles.
49          * (on OSX, Windows, we use system-wide ssl (darwinssl, winssl)
50          * Gnu/Linux distro will link against system-wide libcurl.
51          *
52          * but for linux-bundles we get to enjoy:
53          * https://www.happyassassin.net/2015/01/12/a-note-about-ssltls-trusted-certificate-stores-and-platforms/
54          *
55          * (we do ship curl + nss + nss-pem)
56          *
57          * Short of this mess: we could simply bundle a .crt of
58          * COMODO (ardour) and ghandi (freesound) and be done with it.
59          */
60         assert (!ca_path && !ca_info); // call once
61
62         curl_global_init (CURL_GLOBAL_DEFAULT);
63
64         if (Glib::file_test ("/etc/pki/tls/certs/ca-bundle.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
65                 // Fedora / RHEL, Arch
66                 ca_info = "/etc/pki/tls/certs/ca-bundle.crt";
67         }
68         else if (Glib::file_test ("/etc/ssl/certs/ca-certificates.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
69                 // Debian and derivatives
70                 ca_info = "/etc/ssl/certs/ca-certificates.crt";
71         }
72         else if (Glib::file_test ("/etc/pki/tls/cert.pem", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
73                 // GNU/TLS can keep extra stuff here
74                 ca_info = "/etc/pki/tls/cert.pem";
75         }
76         // else NULL: use default (currently) "/etc/ssl/certs/ca-certificates.crt" if it exists
77
78         if (Glib::file_test ("/etc/pki/tls/certs/ca-bundle.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
79                 // we're on RHEL // https://bugzilla.redhat.com/show_bug.cgi?id=1053882
80                 ca_path = "/nonexistent_path"; // don't try "/etc/ssl/certs" in case it's curl's default
81         }
82         else if (Glib::file_test ("/etc/ssl/certs", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
83                 // Debian and derivs + OpenSuSe
84                 ca_path = "/etc/ssl/certs";
85         } else {
86                 ca_path = "/nonexistent_path"; // don't try -- just in case:
87         }
88
89         /* If we don't set anything defaults are used. at the time of writing we compile bundled curl on debian
90          * and it'll default to  /etc/ssl/certs and /etc/ssl/certs/ca-certificates.crt
91          */
92 }
93
94 static size_t
95 WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data) {
96         size_t realsize = size * nmemb;
97         struct HttpGet::MemStruct *mem = (struct HttpGet::MemStruct*)data;
98
99         mem->data = (char *)realloc (mem->data, mem->size + realsize + 1);
100         if (mem->data) {
101                 memcpy (&(mem->data[mem->size]), ptr, realsize);
102                 mem->size += realsize;
103                 mem->data[mem->size] = 0;
104         }
105         return realsize;
106 }
107
108
109 HttpGet::HttpGet (bool p, bool ssl)
110         : persist (p)
111         , _status (-1)
112         , _result (-1)
113 {
114         error_buffer[0] = '\0';
115         _curl = curl_easy_init ();
116
117         curl_easy_setopt (_curl, CURLOPT_WRITEDATA, (void *)&mem);
118         curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
119         curl_easy_setopt (_curl, CURLOPT_USERAGENT, PROGRAM_NAME VERSIONSTRING);
120         curl_easy_setopt (_curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT);
121         curl_easy_setopt (_curl, CURLOPT_NOSIGNAL, 1);
122         curl_easy_setopt (_curl, CURLOPT_ERRORBUFFER, error_buffer);
123
124         // by default use curl's default.
125         if (ssl && ca_info) {
126                 curl_easy_setopt (_curl, CURLOPT_CAINFO, ca_info);
127         }
128         if (ssl && ca_path) {
129                 curl_easy_setopt (_curl, CURLOPT_CAPATH, ca_path);
130         }
131 }
132
133 HttpGet::~HttpGet ()
134 {
135         curl_easy_cleanup (_curl);
136         if (!persist) {
137                 free (mem.data);
138         }
139 }
140
141 char*
142 HttpGet::get (const char* url)
143 {
144         if (!_curl) {
145                 return NULL;
146         }
147
148         if (strncmp ("http://", url, 7) && strncmp ("https://", url, 8)) {
149                 return NULL;
150         }
151
152         if (!persist) {
153                 free (mem.data);
154         }
155         mem.size = 0;
156
157         curl_easy_setopt (_curl, CURLOPT_URL, url);
158         _result = curl_easy_perform (_curl);
159         curl_easy_getinfo (_curl, CURLINFO_RESPONSE_CODE, &_status);
160
161         if (_result) {
162                 PBD::error << string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer);
163                 return NULL;
164         }
165         if (_status != 200) {
166                 PBD::error << string_compose (_("HTTP request status: %1"), _status);
167                 return NULL;
168         }
169
170         return mem.data;
171 }
172
173 std::string
174 HttpGet::error () const {
175         if (_result != 0) {
176                 return string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer);
177         }
178         if (_status != 200) {
179                 return string_compose (_("HTTP request status: %1"), _status);
180         }
181         return "No Error";
182 }
183
184 char*
185 ArdourCurl::http_get (const char* url, int* status) {
186         HttpGet h (true);
187         char* rv = h.get (url);
188         if (status) {
189                 *status = h.status ();
190         }
191         return rv;
192 }
193
194 std::string
195 ArdourCurl::http_get (const std::string& url) {
196         return HttpGet (false).get (url);
197 }