41 #include <curl/curl.h> 207 #define CURLVERSION_ATLEAST(a,b,c) \ 208 ((LIBCURL_VERSION_MAJOR > (a)) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR > (b))) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR == (b)) && (LIBCURL_VERSION_PATCH >= (c)))) 210 #define CURLOPT_SPECIAL_HASHCOMPAT ((CURLoption) -500) 212 #define CURLOPT_SPECIAL_FAILURE_CODE 999 259 if (!strcasecmp(name,
"header")) {
260 *key = CURLOPT_HEADER;
262 }
else if (!strcasecmp(name,
"httpheader")) {
263 *key = CURLOPT_HTTPHEADER;
265 }
else if (!strcasecmp(name,
"proxy")) {
266 *key = CURLOPT_PROXY;
268 }
else if (!strcasecmp(name,
"proxyport")) {
269 *key = CURLOPT_PROXYPORT;
271 }
else if (!strcasecmp(name,
"proxytype")) {
272 *key = CURLOPT_PROXYTYPE;
274 }
else if (!strcasecmp(name,
"dnstimeout")) {
275 *key = CURLOPT_DNS_CACHE_TIMEOUT;
277 }
else if (!strcasecmp(name,
"userpwd")) {
278 *key = CURLOPT_USERPWD;
280 }
else if (!strcasecmp(name,
"proxyuserpwd")) {
281 *key = CURLOPT_PROXYUSERPWD;
283 }
else if (!strcasecmp(name,
"followlocation")) {
284 *key = CURLOPT_FOLLOWLOCATION;
286 }
else if (!strcasecmp(name,
"maxredirs")) {
287 *key = CURLOPT_MAXREDIRS;
289 }
else if (!strcasecmp(name,
"referer")) {
290 *key = CURLOPT_REFERER;
292 }
else if (!strcasecmp(name,
"useragent")) {
293 *key = CURLOPT_USERAGENT;
295 }
else if (!strcasecmp(name,
"cookie")) {
296 *key = CURLOPT_COOKIE;
298 }
else if (!strcasecmp(name,
"ftptimeout")) {
299 *key = CURLOPT_FTP_RESPONSE_TIMEOUT;
301 }
else if (!strcasecmp(name,
"httptimeout")) {
302 #if CURLVERSION_ATLEAST(7,16,2) 303 *key = CURLOPT_TIMEOUT_MS;
306 *key = CURLOPT_TIMEOUT;
309 }
else if (!strcasecmp(name,
"conntimeout")) {
310 #if CURLVERSION_ATLEAST(7,16,2) 311 *key = CURLOPT_CONNECTTIMEOUT_MS;
314 *key = CURLOPT_CONNECTTIMEOUT;
317 }
else if (!strcasecmp(name,
"ftptext")) {
318 *key = CURLOPT_TRANSFERTEXT;
320 }
else if (!strcasecmp(name,
"ssl_verifypeer")) {
321 *key = CURLOPT_SSL_VERIFYPEER;
323 }
else if (!strcasecmp(name,
"hashcompat")) {
326 }
else if (!strcasecmp(name,
"failurecodes")) {
347 ast_log(
LOG_ERROR,
"Unable to allocate new datastore. Cannot set any CURL options\n");
352 ast_log(
LOG_ERROR,
"Unable to allocate list head. Cannot set any CURL options\n");
371 new->value = (
void *)((
long)
ast_true(value));
374 long tmp = atol(value);
376 new->value = (
void *)tmp;
379 long tmp = atof(value) * 1000.0;
381 new->value = (
void *)tmp;
384 if ((
new =
ast_calloc(1,
sizeof(*
new) + strlen(value) + 1))) {
385 new->value = (
char *)
new +
sizeof(*
new);
386 strcpy(new->value, value);
389 if (key == CURLOPT_PROXYTYPE) {
391 #if CURLVERSION_ATLEAST(7,10,0) 397 #if CURLVERSION_ATLEAST(7,15,2) 398 }
else if (!strcasecmp(value,
"socks4")) {
399 ptype = CURLPROXY_SOCKS4;
401 #if CURLVERSION_ATLEAST(7,18,0) 402 }
else if (!strcasecmp(value,
"socks4a")) {
403 ptype = CURLPROXY_SOCKS4A;
405 #if CURLVERSION_ATLEAST(7,18,0) 406 }
else if (!strcasecmp(value,
"socks5")) {
407 ptype = CURLPROXY_SOCKS5;
409 #if CURLVERSION_ATLEAST(7,18,0) 410 }
else if (!strncasecmp(value,
"socks5", 6)) {
411 ptype = CURLPROXY_SOCKS5_HOSTNAME;
416 new->value = (
void *)ptype;
442 if (new->key != CURLOPT_HTTPHEADER) {
444 if (cur->
key == new->key) {
454 ast_debug(1,
"Inserting entry %p with key %d and value %p\n",
new, new->key, new->value);
476 list[0] = store->
data;
480 for (i = 0; i < 2; i++) {
486 if (cur->
key == key) {
489 snprintf(buf, len,
"%ld", (
long) cur->
value);
494 if ((
long) cur->
value % 1000 == 0) {
496 snprintf(buf, len,
"%ld", (
long)cur->
value / 1000);
502 snprintf(buf, len,
"%.3f", (
double) ((
long) cur->
value) / 1000.0);
508 ast_debug(1,
"Found entry %p, with key %d and value %p\n", cur, cur->
key, cur->
value);
514 }
else if (key == CURLOPT_PROXYTYPE) {
515 const char *strval =
"unknown";
517 #if CURLVERSION_ATLEAST(7,15,2) 518 }
else if ((
long)cur->
value == CURLPROXY_SOCKS4) {
521 #if CURLVERSION_ATLEAST(7,18,0) 522 }
else if ((
long)cur->
value == CURLPROXY_SOCKS4A) {
525 }
else if ((
long)cur->
value == CURLPROXY_SOCKS5) {
527 #if CURLVERSION_ATLEAST(7,18,0) 528 }
else if ((
long)cur->
value == CURLPROXY_SOCKS5_HOSTNAME) {
529 strval =
"socks5hostname";
531 #if CURLVERSION_ATLEAST(7,10,0) 532 }
else if ((
long)cur->
value == CURLPROXY_HTTP) {
542 const char *strval =
"unknown";
590 register int realsize = 0;
594 realsize = size * nmemb;
597 realsize = fwrite(ptr, size, nmemb, cb_data->
out_file);
609 if (!(*curl = curl_easy_init()))
612 curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
613 curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
624 curl_easy_cleanup(*curl);
650 if (strpbrk(url,
"\r\n")) {
669 struct ast_vector_int hasfailurecode = {
NULL };
670 char *failurecodestrings,*found;
673 struct curl_slist *headers =
NULL;
677 char curl_errbuf[CURL_ERROR_SIZE + 1];
689 ast_log(
LOG_ERROR,
"URL '%s' is vulnerable to HTTP injection attacks. Aborting CURL() call.\n", args->
url);
701 hashcompat = (long) cur->
value;
702 }
else if (cur->
key == CURLOPT_HTTPHEADER) {
703 headers = curl_slist_append(headers, (
char*) cur->
value);
705 failurecodestrings = (
char*) cur->
value;
706 while( (found =
strsep(&failurecodestrings,
",")) !=
NULL) {
710 curl_easy_setopt(*curl, cur->
key, cur->
value);
724 hashcompat = (long) cur->
value;
725 }
else if (cur->
key == CURLOPT_HTTPHEADER) {
726 headers = curl_slist_append(headers, (
char*) cur->
value);
728 failurecodestrings = (
char*) cur->
value;
729 while( (found =
strsep(&failurecodestrings,
",")) !=
NULL) {
733 curl_easy_setopt(*curl, cur->
key, cur->
value);
739 curl_easy_setopt(*curl, CURLOPT_URL, args->
url);
740 curl_easy_setopt(*curl, CURLOPT_FILE, (
void *) &args->
cb_data);
743 curl_easy_setopt(*curl, CURLOPT_POST, 1);
744 curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, args->
postdata);
750 curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headers);
753 curl_errbuf[0] = curl_errbuf[CURL_ERROR_SIZE] =
'\0';
754 curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, curl_errbuf);
756 if (curl_easy_perform(*curl) != 0) {
764 curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, (
char*)
NULL);
765 curl_easy_getinfo (*curl, CURLINFO_RESPONSE_CODE, &http_code);
783 curl_slist_free_all(headers);
786 curl_easy_setopt(*curl, CURLOPT_POST, 0);
799 while (fields && values && (piece =
strsep(&remainder,
"&"))) {
901 .synopsis =
"Set options for use with the CURL() function",
902 .syntax =
"CURLOPT(<option>)",
904 " cookie - Send cookie with request [none]\n" 905 " conntimeout - Number of seconds to wait for connection\n" 906 " dnstimeout - Number of seconds to wait for DNS response\n" 907 " followlocation - Follow HTTP 3xx redirects (boolean)\n" 908 " ftptext - For FTP, force a text transfer (boolean)\n" 909 " ftptimeout - For FTP, the server response timeout\n" 910 " header - Retrieve header information (boolean)\n" 911 " httpheader - Add new custom http header (string)\n" 912 " httptimeout - Number of seconds to wait for HTTP response\n" 913 " maxredirs - Maximum number of redirects to follow\n" 914 " proxy - Hostname or IP to use as a proxy\n" 915 " proxytype - http, socks4, or socks5\n" 916 " proxyport - port number of the proxy\n" 917 " proxyuserpwd - A <user>:<pass> to use for authentication\n" 918 " referer - Referer URL to use for the request\n" 919 " useragent - UserAgent string to use\n" 920 " userpwd - A <user>:<pass> to use for authentication\n" 921 " ssl_verifypeer - Whether to verify the peer certificate (boolean)\n" 922 " hashcompat - Result data will be compatible for use with HASH()\n" 923 " - if value is \"legacy\", will translate '+' to ' '\n" 924 " failurecodes - A comma separated list of HTTP response codes to be treated as errors\n" 931 #ifdef TEST_FRAMEWORK 934 const char *bad_urls [] = {
935 "http://example.com\r\nDELETE http://example.com/everything",
936 "http://example.com\rDELETE http://example.com/everything",
937 "http://example.com\nDELETE http://example.com/everything",
938 "\r\nhttp://example.com",
939 "\rhttp://example.com",
940 "\nhttp://example.com",
941 "http://example.com\r\n",
942 "http://example.com\r",
943 "http://example.com\n",
945 const char *good_urls [] = {
946 "http://example.com",
947 "http://example.com/%5Cr%5Cn",
954 info->name =
"vulnerable_url";
955 info->category =
"/funcs/func_curl/";
956 info->summary =
"cURL vulnerable URL test";
958 "Ensure that any combination of '\\r' or '\\n' in a URL invalidates the URL";
963 for (i = 0; i <
ARRAY_LEN(bad_urls); ++i) {
970 for (i = 0; i <
ARRAY_LEN(good_urls); ++i) {
1010 .requires =
"res_curl",
void ast_uri_decode(char *s, struct ast_flags spec)
Decode URI, URN, URL (overwrite string)
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
#define ast_channel_lock(chan)
Main Channel structure associated with a channel.
static int acf_curlopt_write(struct ast_channel *chan, const char *cmd, char *name, const char *value)
#define AST_LIST_LOCK(head)
Locks a list.
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define AST_LIST_HEAD(name, type)
Defines a structure to be used to hold a list of specified type.
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
static struct ast_custom_function acf_curlopt
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Callback data passed to WriteMemoryCallback.
static struct ast_threadstorage curl_instance
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
static int parse_curlopt_key(const char *name, CURLoption *key, enum optiontype *ot)
#define AST_TEST_REGISTER(cb)
Structure for a data store type.
static int acf_curlopt_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Structure for a data store object.
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
Definitions to aid in the use of thread local storage.
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
struct global_curl_info global_curl_info
static const char *const global_useragent
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
#define ast_strlen_zero(foo)
#define AST_LIST_HEAD_DESTROY(head)
Destroys a list head structure.
#define CURLOPT_SPECIAL_FAILURE_CODE
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
static int url_is_vulnerable(const char *url)
Check for potential HTTP injection risk.
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
General Asterisk PBX channel definitions.
#define ast_test_status_update(a, b, c...)
char * ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Append a non-NULL terminated substring to the end of a dynamic string.
Data structure associated with a custom dialplan function.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
struct curl_settings::@205 list
#define ast_strdupa(s)
duplicate a string in memory from the stack
FILE * out_file
If a file is being retrieved, the file to write to.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
struct curl_write_callback_data cb_data
AST_TEST_DEFINE(vulnerable_url)
Core PBX routines and definitions.
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
static int curl_instance_init(void *data)
static int acf_curl_write(struct ast_channel *chan, const char *cmd, char *name, const char *value)
ssize_t len
The max size of str.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
const struct ast_flags ast_uri_http_legacy
static int acf_curl_helper(struct ast_channel *chan, struct curl_args *args)
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
static struct ast_custom_function acf_curl
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
#define AST_TEST_UNREGISTER(cb)
static int load_module(void)
static struct ast_threadstorage thread_escapebuf
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int unload_module(void)
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
#define ast_channel_unlock(chan)
#define AST_LIST_HEAD_INIT(head)
Initializes a list head structure.
static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, struct ast_str **buf, ssize_t len)
#define ast_calloc(num, len)
A wrapper for calloc()
Structure used to handle boolean flags.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static int acf_curlopt_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, ssize_t len)
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
#define AST_THREADSTORAGE_CUSTOM(a, b, c)
Define a thread storage variable, with custom initialization and cleanup.
char * ast_str_set_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Set a dynamic string to a non-NULL terminated substring, with escaping of commas. ...
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
char * strsep(char **str, const char *delims)
static void curlds_free(void *data)
Standard Command Line Interface.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
const char * ast_channel_name(const struct ast_channel *chan)
const struct ast_flags ast_uri_http
#define ast_datastore_alloc(info, uid)
static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
struct ast_str * str
If a string is being built, the string buffer.
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
void ast_str_trim_blanks(struct ast_str *buf)
Trims trailing whitespace characters from an ast_str string.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define CURLOPT_SPECIAL_HASHCOMPAT
static const struct ast_datastore_info curl_info
#define ast_custom_function_register(acf)
Register a custom function.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
#define AST_APP_ARG(name)
Define an application argument.
static void curl_instance_cleanup(void *data)