27 #include <curl/curl.h> 31 #define MAX_HEADER_LENGTH 1023 34 #define GLOBAL_USERAGENT "asterisk-libcurl-agent/1.0" 93 realsize = size * nitems;
102 memcpy(header, buffer, realsize);
103 header[realsize] =
'\0';
104 value = strchr(header,
':');
111 if (!strcasecmp(header,
"Cache-Control")) {
113 }
else if (!strcasecmp(header,
"Expires")) {
138 curl = curl_easy_init();
143 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
144 curl_easy_setopt(curl, CURLOPT_TIMEOUT, curl_timeout);
146 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
148 curl_easy_setopt(curl, CURLOPT_HEADERDATA, data);
166 const char *template_name =
"certXXXXXX";
169 if (
ast_asprintf(filename,
"%s/%s", path, template_name) < 0) {
176 if ((fd = mkstemp(*filename)) < 0) {
187 FILE *public_key_file;
194 char curl_errbuf[CURL_ERROR_SIZE + 1];
196 curl_errbuf[CURL_ERROR_SIZE] =
'\0';
209 public_key_file = fdopen(fd,
"wb");
210 if (!public_key_file) {
211 ast_log(
LOG_ERROR,
"Failed to open file '%s' to write public key from '%s': %s (%d)\n",
212 tmp_filename, public_cert_url, strerror(
errno),
errno);
214 remove(tmp_filename);
220 ast_log(
LOG_ERROR,
"Failed to set up CURL isntance for '%s'\n", public_cert_url);
221 fclose(public_key_file);
222 remove(tmp_filename);
226 curl_easy_setopt(curl, CURLOPT_URL, public_cert_url);
227 curl_easy_setopt(curl, CURLOPT_WRITEDATA, public_key_file);
228 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
230 if (curl_easy_perform(curl)) {
232 curl_easy_cleanup(curl);
233 fclose(public_key_file);
234 remove(tmp_filename);
238 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
240 curl_easy_cleanup(curl);
241 fclose(public_key_file);
243 if (http_code / 100 != 2) {
244 ast_log(
LOG_ERROR,
"Failed to retrieve URL '%s': code %ld\n", public_cert_url, http_code);
245 remove(tmp_filename);
252 remove(tmp_filename);
256 if (
ast_asprintf(&filename,
"%s/%s.pem", path, serial) < 0) {
257 ast_log(
LOG_ERROR,
"Failed to allocate memory for new filename for temporary " 258 "file %s after CURL\n", tmp_filename);
260 remove(tmp_filename);
266 if (rename(tmp_filename, filename)) {
267 ast_log(
LOG_ERROR,
"Failed to rename temporary file %s to %s after CURL\n", tmp_filename, filename);
269 remove(tmp_filename);
struct stir_shaken_general * stir_shaken_general_get()
Retrieve the stir/shaken 'general' configuration object.
char * stir_shaken_get_serial_number_x509(const char *path)
Gets the serial number in hex form from the X509 certificate at path.
Asterisk main include file. File version handling, generic pbx functions.
void curl_cb_data_free(struct curl_cb_data *data)
Free a curl_cb_data struct.
#define MAX_HEADER_LENGTH
static size_t curl_header_callback(char *buffer, size_t size, size_t nitems, void *data)
Called when a CURL request completes.
#define ast_strdup(str)
A wrapper for strdup()
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
char * curl_public_key(const char *public_cert_url, const char *path, struct curl_cb_data *data)
CURL the public key from the provided URL to the specified path.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
unsigned int curl_timeout
static CURL * get_curl_instance(struct curl_cb_data *data)
Prepare a CURL instance to use.
char * curl_cb_data_get_expires(const struct curl_cb_data *data)
Get the expires field from a curl_cb_data struct.
char * curl_cb_data_get_cache_control(const struct curl_cb_data *data)
Get the cache_control field from a curl_cb_data struct.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
#define ast_calloc(num, len)
A wrapper for calloc()
Support for logging to various files, console and syslog Configuration in file logger.conf.
struct curl_cb_data * curl_cb_data_create(void)
Allocate memory for a curl_cb_data struct.
static int create_temp_file(const char *path, char **filename)
Create a temporary file located at path.
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
unsigned int ast_stir_shaken_curl_timeout(const struct stir_shaken_general *cfg)
Retrieve the 'curl_timeout' general configuration option value.