summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/pingsender/pingsender_unix_common.cpp
blob: 1f201d177e619c32f2826145bb4ec9e98c3acb98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <algorithm>
#include <cerrno>
#include <cstring>
#include <dlfcn.h>
#include <string>
#include <unistd.h>
#include "mozilla/Unused.h"
#include "third_party/curl/curl.h"

#include "pingsender.h"

namespace PingSender {

using std::string;

using mozilla::Unused;

/**
 * A simple wrapper around libcurl "easy" functions. Provides RAII opening
 * and initialization of the curl library
 */
class CurlWrapper {
 public:
  CurlWrapper();
  ~CurlWrapper();
  bool Init();
  bool IsValidDestination(const string& url);
  bool Post(const string& url, const string& payload);

  // libcurl functions
  CURL* (*easy_init)(void);
  CURLcode (*easy_setopt)(CURL*, CURLoption, ...);
  CURLcode (*easy_perform)(CURL*);
  CURLcode (*easy_getinfo)(CURL*, CURLINFO, ...);
  curl_slist* (*slist_append)(curl_slist*, const char*);
  void (*slist_free_all)(curl_slist*);
  const char* (*easy_strerror)(CURLcode);
  void (*easy_cleanup)(CURL*);
  void (*global_cleanup)(void);

  CURLU* (*curl_url)();
  CURLUcode (*curl_url_get)(CURLU*, CURLUPart, char**, unsigned int);
  CURLUcode (*curl_url_set)(CURLU*, CURLUPart, const char*, unsigned int);
  void (*curl_free)(char*);
  void (*curl_url_cleanup)(CURLU*);

 private:
  void* mLib;
  void* mCurl;
  bool mCanParseUrl;
};

CurlWrapper::CurlWrapper()
    : easy_init(nullptr),
      easy_setopt(nullptr),
      easy_perform(nullptr),
      easy_getinfo(nullptr),
      slist_append(nullptr),
      slist_free_all(nullptr),
      easy_strerror(nullptr),
      easy_cleanup(nullptr),
      global_cleanup(nullptr),
      curl_url(nullptr),
      curl_url_get(nullptr),
      curl_url_set(nullptr),
      curl_free(nullptr),
      curl_url_cleanup(nullptr),
      mLib(nullptr),
      mCurl(nullptr) {}

CurlWrapper::~CurlWrapper() {
  if (mLib) {
    if (mCurl && easy_cleanup) {
      easy_cleanup(mCurl);
    }

    if (global_cleanup) {
      global_cleanup();
    }

    dlclose(mLib);
  }
}

bool CurlWrapper::Init() {
  const char* libcurlPaths[] = {
#if defined(XP_MACOSX)
    // macOS
    "/usr/lib/libcurl.dylib",
    "/usr/lib/libcurl.4.dylib",
    "/usr/lib/libcurl.3.dylib",
#else  // Linux, *BSD, ...
    "libcurl.so",
    "libcurl.so.4",
    // Debian gives libcurl a different name when it is built against GnuTLS
    "libcurl-gnutls.so",
    "libcurl-gnutls.so.4",
    // Older versions in case we find nothing better
    "libcurl.so.3",
    "libcurl-gnutls.so.3",  // See above for Debian
#endif
  };

  // libcurl might show up under different names & paths, try them all until
  // we find it
  for (const char* libname : libcurlPaths) {
    mLib = dlopen(libname, RTLD_NOW);

    if (mLib) {
      break;
    }
  }

  if (!mLib) {
    PINGSENDER_LOG("ERROR: Could not find libcurl\n");
    return false;
  }

  *(void**)(&easy_init) = dlsym(mLib, "curl_easy_init");
  *(void**)(&easy_setopt) = dlsym(mLib, "curl_easy_setopt");
  *(void**)(&easy_perform) = dlsym(mLib, "curl_easy_perform");
  *(void**)(&easy_getinfo) = dlsym(mLib, "curl_easy_getinfo");
  *(void**)(&slist_append) = dlsym(mLib, "curl_slist_append");
  *(void**)(&slist_free_all) = dlsym(mLib, "curl_slist_free_all");
  *(void**)(&easy_strerror) = dlsym(mLib, "curl_easy_strerror");
  *(void**)(&easy_cleanup) = dlsym(mLib, "curl_easy_cleanup");
  *(void**)(&global_cleanup) = dlsym(mLib, "curl_global_cleanup");

  *(void**)(&curl_url) = dlsym(mLib, "curl_url");
  *(void**)(&curl_url_set) = dlsym(mLib, "curl_url_set");
  *(void**)(&curl_url_get) = dlsym(mLib, "curl_url_get");
  *(void**)(&curl_free) = dlsym(mLib, "curl_free");
  *(void**)(&curl_url_cleanup) = dlsym(mLib, "curl_url_cleanup");

  if (!easy_init || !easy_setopt || !easy_perform || !easy_getinfo ||
      !slist_append || !slist_free_all || !easy_strerror || !easy_cleanup ||
      !global_cleanup) {
    PINGSENDER_LOG("ERROR: libcurl is missing one of the required symbols\n");
    return false;
  }

  mCanParseUrl = true;
  if (!curl_url || !curl_url_get || !curl_url_set || !curl_free ||
      !curl_url_cleanup) {
    mCanParseUrl = false;
    PINGSENDER_LOG("WARNING: Do not have url parsing functions in libcurl\n");
  }

  mCurl = easy_init();

  if (!mCurl) {
    PINGSENDER_LOG("ERROR: Could not initialize libcurl\n");
    return false;
  }

  return true;
}

static size_t DummyWriteCallback(char* ptr, size_t size, size_t nmemb,
                                 void* userdata) {
  Unused << ptr;
  Unused << size;
  Unused << nmemb;
  Unused << userdata;

  return size * nmemb;
}

// If we can't use curl's URL parsing (which is safer) we have to fallback
// to this handwritten one (which is only as safe as we are clever.)
bool FallbackIsValidDestination(const string& aUrl) {
  // Lowercase the url
  string url = aUrl;
  std::transform(url.begin(), url.end(), url.begin(),
                 [](unsigned char c) { return std::tolower(c); });
  // Strip off the scheme in the beginning
  if (url.find("http://") == 0) {
    url = url.substr(7);
  } else if (url.find("https://") == 0) {
    url = url.substr(8);
  }

  // Remove any user information. If a @ appeared in the userinformation,
  // it would need to be encoded.
  unsigned long atStart = url.find_first_of("@");
  url = (atStart == std::string::npos) ? url : url.substr(atStart + 1);

  // Remove any path or fragment information, leaving us with a url that may
  // contain host, and port.
  unsigned long fragStart = url.find_first_of("#");
  url = (fragStart == std::string::npos) ? url : url.substr(0, fragStart);
  unsigned long pathStart = url.find_first_of("/");
  url = (pathStart == std::string::npos) ? url : url.substr(0, pathStart);

  // Remove the port, because we run tests targeting localhost:port
  unsigned long portStart = url.find_last_of(":");
  url = (portStart == std::string::npos) ? url : url.substr(0, portStart);

  return PingSender::IsValidDestination(url);
}

bool CurlWrapper::IsValidDestination(const string& aUrl) {
  if (!mCanParseUrl) {
    return FallbackIsValidDestination(aUrl);
  }

  bool ret = false;
  CURLU* h = curl_url();
  if (!h) {
    return FallbackIsValidDestination(aUrl);
  }

  if (CURLUE_OK != curl_url_set(h, CURLUPART_URL, aUrl.c_str(), 0)) {
    goto cleanup;
  }

  char* host;
  if (CURLUE_OK != curl_url_get(h, CURLUPART_HOST, &host, 0)) {
    goto cleanup;
  }

  ret = PingSender::IsValidDestination(host);
  curl_free(host);

cleanup:
  curl_url_cleanup(h);
  return ret;
}

bool CurlWrapper::Post(const string& url, const string& payload) {
  easy_setopt(mCurl, CURLOPT_URL, url.c_str());
  easy_setopt(mCurl, CURLOPT_USERAGENT, kUserAgent);
  easy_setopt(mCurl, CURLOPT_WRITEFUNCTION, DummyWriteCallback);

  // Build the date header.
  std::string dateHeader = GenerateDateHeader();

  // Set the custom headers.
  curl_slist* headerChunk = nullptr;
  headerChunk = slist_append(headerChunk, kCustomVersionHeader);
  headerChunk = slist_append(headerChunk, kContentEncodingHeader);
  headerChunk = slist_append(headerChunk, dateHeader.c_str());
  CURLcode err = easy_setopt(mCurl, CURLOPT_HTTPHEADER, headerChunk);
  if (err != CURLE_OK) {
    PINGSENDER_LOG("ERROR: Failed to set HTTP headers, %s\n",
                   easy_strerror(err));
    slist_free_all(headerChunk);
    return false;
  }

  // Set the size of the POST data
  easy_setopt(mCurl, CURLOPT_POSTFIELDSIZE, payload.length());

  // Set the contents of the POST data
  easy_setopt(mCurl, CURLOPT_POSTFIELDS, payload.c_str());

  // Fail if the server returns a 4xx code
  easy_setopt(mCurl, CURLOPT_FAILONERROR, 1);

  // Override the default connection timeout, which is 5 minutes.
  easy_setopt(mCurl, CURLOPT_CONNECTTIMEOUT_MS, kConnectionTimeoutMs);

  // Block until the operation is performend. Ignore the response, if the POST
  // fails we can't do anything about it.
  err = easy_perform(mCurl);
  // Whatever happens, we want to clean up the header memory.
  slist_free_all(headerChunk);

  if (err != CURLE_OK) {
    PINGSENDER_LOG("ERROR: Failed to send HTTP request, %s\n",
                   easy_strerror(err));
    return false;
  }

  return true;
}

bool Post(const string& url, const string& payload) {
  CurlWrapper curl;

  if (!curl.Init()) {
    return false;
  }
  if (!curl.IsValidDestination(url)) {
    PINGSENDER_LOG("ERROR: Invalid destination host\n");
    return false;
  }

  return curl.Post(url, payload);
}

void ChangeCurrentWorkingDirectory(const string& pingPath) {
  // This is not needed under Linux/macOS
}

}  // namespace PingSender