| 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 |
#include "asterisk.h" |
| 40 |
|
| 41 |
#include <curl/curl.h> |
| 42 |
|
| 43 |
#include "asterisk/lock.h" |
| 44 |
#include "asterisk/file.h" |
| 45 |
#include "asterisk/channel.h" |
| 46 |
#include "asterisk/pbx.h" |
| 47 |
#include "asterisk/cli.h" |
| 48 |
#include "asterisk/module.h" |
| 49 |
#include "asterisk/app.h" |
| 50 |
#include "asterisk/utils.h" |
| 51 |
#include "asterisk/threadstorage.h" |
| 52 |
#include "asterisk/test.h" |
| 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 |
#define CURLVERSION_ATLEAST(a,b,c) \ |
| 263 |
((LIBCURL_VERSION_MAJOR > (a)) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR > (b))) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR == (b)) && (LIBCURL_VERSION_PATCH >= (c)))) |
| 264 |
|
| 265 |
#define CURLOPT_SPECIAL_HASHCOMPAT ((CURLoption) -500) |
| 266 |
|
| 267 |
#define CURLOPT_SPECIAL_FAILURE_CODE 999 |
| 268 |
|
| 269 |
static void curlds_free(void *data); |
| 270 |
|
| 271 |
static const struct ast_datastore_info curl_info = { |
| 272 |
.type = "CURL", |
| 273 |
.destroy = curlds_free, |
| 274 |
}; |
| 275 |
|
| 276 |
struct curl_settings { |
| 277 |
AST_LIST_ENTRY(curl_settings) list; |
| 278 |
CURLoption key; |
| 279 |
void *value; |
| 280 |
}; |
| 281 |
|
| 282 |
AST_LIST_HEAD_STATIC(global_curl_info, curl_settings); |
| 283 |
|
| 284 |
static void curlds_free(void *data) |
| 285 |
{ |
| 286 |
AST_LIST_HEAD(global_curl_info, curl_settings) *list = data; |
| 287 |
struct curl_settings *setting; |
| 288 |
if (!list) { |
| 289 |
return; |
| 290 |
} |
| 291 |
while ((setting = AST_LIST_REMOVE_HEAD(list, list))) { |
| 292 |
ast_free(setting); |
| 293 |
} |
| 294 |
AST_LIST_HEAD_DESTROY(list); |
| 295 |
ast_free(list); |
| 296 |
} |
| 297 |
|
| 298 |
enum optiontype { |
| 299 |
OT_BOOLEAN, |
| 300 |
OT_INTEGER, |
| 301 |
OT_INTEGER_MS, |
| 302 |
OT_STRING, |
| 303 |
OT_ENUM, |
| 304 |
}; |
| 305 |
|
| 306 |
enum hashcompat { |
| 307 |
HASHCOMPAT_NO = 0, |
| 308 |
HASHCOMPAT_YES, |
| 309 |
HASHCOMPAT_LEGACY, |
| 310 |
}; |
| 311 |
|
| 312 |
static int parse_curlopt_key(const char *name, CURLoption *key, enum optiontype *ot) |
| 313 |
{ |
| 314 |
if (!strcasecmp(name, "header")) { |
| 315 |
*key = CURLOPT_HEADER; |
| 316 |
*ot = OT_BOOLEAN; |
| 317 |
} else if (!strcasecmp(name, "httpheader")) { |
| 318 |
*key = CURLOPT_HTTPHEADER; |
| 319 |
*ot = OT_STRING; |
| 320 |
} else if (!strcasecmp(name, "httpauth")) { |
| 321 |
*key = CURLOPT_HTTPAUTH; |
| 322 |
*ot = OT_ENUM; |
| 323 |
} else if (!strcasecmp(name, "proxy")) { |
| 324 |
*key = CURLOPT_PROXY; |
| 325 |
*ot = OT_STRING; |
| 326 |
} else if (!strcasecmp(name, "proxyport")) { |
| 327 |
*key = CURLOPT_PROXYPORT; |
| 328 |
*ot = OT_INTEGER; |
| 329 |
} else if (!strcasecmp(name, "proxytype")) { |
| 330 |
*key = CURLOPT_PROXYTYPE; |
| 331 |
*ot = OT_ENUM; |
| 332 |
} else if (!strcasecmp(name, "dnstimeout")) { |
| 333 |
*key = CURLOPT_DNS_CACHE_TIMEOUT; |
| 334 |
*ot = OT_INTEGER; |
| 335 |
} else if (!strcasecmp(name, "userpwd")) { |
| 336 |
*key = CURLOPT_USERPWD; |
| 337 |
*ot = OT_STRING; |
| 338 |
} else if (!strcasecmp(name, "proxyuserpwd")) { |
| 339 |
*key = CURLOPT_PROXYUSERPWD; |
| 340 |
*ot = OT_STRING; |
| 341 |
} else if (!strcasecmp(name, "followlocation")) { |
| 342 |
*key = CURLOPT_FOLLOWLOCATION; |
| 343 |
*ot = OT_BOOLEAN; |
| 344 |
} else if (!strcasecmp(name, "maxredirs")) { |
| 345 |
*key = CURLOPT_MAXREDIRS; |
| 346 |
*ot = OT_INTEGER; |
| 347 |
} else if (!strcasecmp(name, "referer")) { |
| 348 |
*key = CURLOPT_REFERER; |
| 349 |
*ot = OT_STRING; |
| 350 |
} else if (!strcasecmp(name, "useragent")) { |
| 351 |
*key = CURLOPT_USERAGENT; |
| 352 |
*ot = OT_STRING; |
| 353 |
} else if (!strcasecmp(name, "cookie")) { |
| 354 |
*key = CURLOPT_COOKIE; |
| 355 |
*ot = OT_STRING; |
| 356 |
} else if (!strcasecmp(name, "ftptimeout")) { |
| 357 |
*key = CURLOPT_FTP_RESPONSE_TIMEOUT; |
| 358 |
*ot = OT_INTEGER; |
| 359 |
} else if (!strcasecmp(name, "httptimeout")) { |
| 360 |
#if CURLVERSION_ATLEAST(7,16,2) |
| 361 |
*key = CURLOPT_TIMEOUT_MS; |
| 362 |
*ot = OT_INTEGER_MS; |
| 363 |
#else |
| 364 |
*key = CURLOPT_TIMEOUT; |
| 365 |
*ot = OT_INTEGER; |
| 366 |
#endif |
| 367 |
} else if (!strcasecmp(name, "conntimeout")) { |
| 368 |
#if CURLVERSION_ATLEAST(7,16,2) |
| 369 |
*key = CURLOPT_CONNECTTIMEOUT_MS; |
| 370 |
*ot = OT_INTEGER_MS; |
| 371 |
#else |
| 372 |
*key = CURLOPT_CONNECTTIMEOUT; |
| 373 |
*ot = OT_INTEGER; |
| 374 |
#endif |
| 375 |
} else if (!strcasecmp(name, "ftptext")) { |
| 376 |
*key = CURLOPT_TRANSFERTEXT; |
| 377 |
*ot = OT_BOOLEAN; |
| 378 |
} else if (!strcasecmp(name, "ssl_verifypeer")) { |
| 379 |
*key = CURLOPT_SSL_VERIFYPEER; |
| 380 |
*ot = OT_BOOLEAN; |
| 381 |
} else if (!strcasecmp(name, "ssl_verifyhost")) { |
| 382 |
*key = CURLOPT_SSL_VERIFYHOST; |
| 383 |
*ot = OT_INTEGER; |
| 384 |
} else if (!strcasecmp(name, "ssl_cainfo")) { |
| 385 |
*key = CURLOPT_CAINFO; |
| 386 |
*ot = OT_STRING; |
| 387 |
} else if (!strcasecmp(name, "ssl_capath")) { |
| 388 |
*key = CURLOPT_CAPATH; |
| 389 |
*ot = OT_STRING; |
| 390 |
} else if (!strcasecmp(name, "ssl_cert")) { |
| 391 |
*key = CURLOPT_SSLCERT; |
| 392 |
*ot = OT_STRING; |
| 393 |
} else if (!strcasecmp(name, "ssl_certtype")) { |
| 394 |
*key = CURLOPT_SSLCERTTYPE; |
| 395 |
*ot = OT_STRING; |
| 396 |
} else if (!strcasecmp(name, "ssl_key")) { |
| 397 |
*key = CURLOPT_SSLKEY; |
| 398 |
*ot = OT_STRING; |
| 399 |
} else if (!strcasecmp(name, "ssl_keytype")) { |
| 400 |
*key = CURLOPT_SSLKEYTYPE; |
| 401 |
*ot = OT_STRING; |
| 402 |
} else if (!strcasecmp(name, "ssl_keypasswd")) { |
| 403 |
*key = CURLOPT_KEYPASSWD; |
| 404 |
*ot = OT_STRING; |
| 405 |
} else if (!strcasecmp(name, "hashcompat")) { |
| 406 |
*key = CURLOPT_SPECIAL_HASHCOMPAT; |
| 407 |
*ot = OT_ENUM; |
| 408 |
} else if (!strcasecmp(name, "failurecodes")) { |
| 409 |
*key = CURLOPT_SPECIAL_FAILURE_CODE; |
| 410 |
*ot = OT_STRING; |
| 411 |
} else { |
| 412 |
return -1; |
| 413 |
} |
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
static int acf_curlopt_write(struct ast_channel *chan, const char *cmd, char *name, const char *value) |
| 418 |
{ |
| 419 |
struct ast_datastore *store; |
| 420 |
struct global_curl_info *list; |
| 421 |
struct curl_settings *cur, *new = NULL; |
| 422 |
CURLoption key; |
| 423 |
enum optiontype ot; |
| 424 |
|
| 425 |
if (chan) { |
| 426 |
ast_channel_lock(chan); |
| 427 |
if (!(store = ast_channel_datastore_find(chan, &curl_info, NULL))) { |
| 428 |
|
| 429 |
if (!(store = ast_datastore_alloc(&curl_info, NULL))) { |
| 430 |
ast_log(LOG_ERROR, "Unable to allocate new datastore. Cannot set any CURL options\n"); |
| 431 |
ast_channel_unlock(chan); |
| 432 |
return -1; |
| 433 |
} |
| 434 |
|
| 435 |
if (!(list = ast_calloc(1, sizeof(*list)))) { |
| 436 |
ast_log(LOG_ERROR, "Unable to allocate list head. Cannot set any CURL options\n"); |
| 437 |
ast_datastore_free(store); |
| 438 |
ast_channel_unlock(chan); |
| 439 |
return -1; |
| 440 |
} |
| 441 |
|
| 442 |
store->data = list; |
| 443 |
AST_LIST_HEAD_INIT(list); |
| 444 |
ast_channel_datastore_add(chan, store); |
| 445 |
} else { |
| 446 |
list = store->data; |
| 447 |
} |
| 448 |
ast_channel_unlock(chan); |
| 449 |
} else { |
| 450 |
|
| 451 |
list = &global_curl_info; |
| 452 |
} |
| 453 |
|
| 454 |
if (!parse_curlopt_key(name, &key, &ot)) { |
| 455 |
if (ot == OT_BOOLEAN) { |
| 456 |
if ((new = ast_calloc(1, sizeof(*new)))) { |
| 457 |
new->value = (void *)((long) ast_true(value)); |
| 458 |
} |
| 459 |
} else if (ot == OT_INTEGER) { |
| 460 |
long tmp = atol(value); |
| 461 |
if ((new = ast_calloc(1, sizeof(*new)))) { |
| 462 |
new->value = (void *)tmp; |
| 463 |
} |
| 464 |
} else if (ot == OT_INTEGER_MS) { |
| 465 |
long tmp = atof(value) * 1000.0; |
| 466 |
if ((new = ast_calloc(1, sizeof(*new)))) { |
| 467 |
new->value = (void *)tmp; |
| 468 |
} |
| 469 |
} else if (ot == OT_STRING) { |
| 470 |
if ((new = ast_calloc(1, sizeof(*new) + strlen(value) + 1))) { |
| 471 |
new->value = (char *)new + sizeof(*new); |
| 472 |
strcpy(new->value, value); |
| 473 |
} |
| 474 |
} else if (ot == OT_ENUM) { |
| 475 |
if (key == CURLOPT_PROXYTYPE) { |
| 476 |
long ptype = |
| 477 |
#if CURLVERSION_ATLEAST(7,10,0) |
| 478 |
CURLPROXY_HTTP; |
| 479 |
#else |
| 480 |
CURLPROXY_SOCKS5; |
| 481 |
#endif |
| 482 |
if (0) { |
| 483 |
#if CURLVERSION_ATLEAST(7,15,2) |
| 484 |
} else if (!strcasecmp(value, "socks4")) { |
| 485 |
ptype = CURLPROXY_SOCKS4; |
| 486 |
#endif |
| 487 |
#if CURLVERSION_ATLEAST(7,18,0) |
| 488 |
} else if (!strcasecmp(value, "socks4a")) { |
| 489 |
ptype = CURLPROXY_SOCKS4A; |
| 490 |
#endif |
| 491 |
#if CURLVERSION_ATLEAST(7,18,0) |
| 492 |
} else if (!strcasecmp(value, "socks5")) { |
| 493 |
ptype = CURLPROXY_SOCKS5; |
| 494 |
#endif |
| 495 |
#if CURLVERSION_ATLEAST(7,18,0) |
| 496 |
} else if (!strncasecmp(value, "socks5", 6)) { |
| 497 |
ptype = CURLPROXY_SOCKS5_HOSTNAME; |
| 498 |
#endif |
| 499 |
} |
| 500 |
|
| 501 |
if ((new = ast_calloc(1, sizeof(*new)))) { |
| 502 |
new->value = (void *)ptype; |
| 503 |
} |
| 504 |
} else if (key == CURLOPT_HTTPAUTH) { |
| 505 |
long authtype = 0; |
| 506 |
char *authmethod, *authstr = ast_strdupa(value); |
| 507 |
while ((authmethod = strsep(&authstr, ","))) { |
| 508 |
if (!strcasecmp(authmethod, "basic")) { |
| 509 |
authtype |= CURLAUTH_BASIC; |
| 510 |
} else if (!strcasecmp(authmethod, "digest")) { |
| 511 |
authtype |= CURLAUTH_DIGEST; |
| 512 |
} else { |
| 513 |
ast_log(LOG_WARNING, "Auth method '%s' invalid or not supported\n", authmethod); |
| 514 |
return -1; |
| 515 |
} |
| 516 |
} |
| 517 |
if (!authmethod) { |
| 518 |
ast_log(LOG_WARNING, "Auth method '%s' invalid or not supported\n", value); |
| 519 |
} |
| 520 |
if ((new = ast_calloc(1, sizeof(*new)))) { |
| 521 |
new->value = (void *)authtype; |
| 522 |
} |
| 523 |
} else if (key == CURLOPT_SPECIAL_HASHCOMPAT) { |
| 524 |
if ((new = ast_calloc(1, sizeof(*new)))) { |
| 525 |
new->value = (void *) (long) (!strcasecmp(value, "legacy") ? HASHCOMPAT_LEGACY : ast_true(value) ? HASHCOMPAT_YES : HASHCOMPAT_NO); |
| 526 |
} |
| 527 |
} else { |
| 528 |
|
| 529 |
goto yuck; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
if (!new) { |
| 535 |
return -1; |
| 536 |
} |
| 537 |
|
| 538 |
new->key = key; |
| 539 |
} else { |
| 540 |
yuck: |
| 541 |
ast_log(LOG_ERROR, "Unrecognized option: %s\n", name); |
| 542 |
return -1; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
AST_LIST_LOCK(list); |
| 547 |
if (new->key != CURLOPT_HTTPHEADER) { |
| 548 |
AST_LIST_TRAVERSE_SAFE_BEGIN(list, cur, list) { |
| 549 |
if (cur->key == new->key) { |
| 550 |
AST_LIST_REMOVE_CURRENT(list); |
| 551 |
ast_free(cur); |
| 552 |
break; |
| 553 |
} |
| 554 |
} |
| 555 |
AST_LIST_TRAVERSE_SAFE_END |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
ast_debug(1, "Inserting entry %p with key %d and value %p\n", new, new->key, new->value); |
| 560 |
AST_LIST_INSERT_TAIL(list, new, list); |
| 561 |
AST_LIST_UNLOCK(list); |
| 562 |
|
| 563 |
return 0; |
| 564 |
} |
| 565 |
|
| 566 |
static int acf_curlopt_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, ssize_t len) |
| 567 |
{ |
| 568 |
struct ast_datastore *store; |
| 569 |
struct global_curl_info *list[2] = { &global_curl_info, NULL }; |
| 570 |
struct curl_settings *cur = NULL; |
| 571 |
CURLoption key; |
| 572 |
enum optiontype ot; |
| 573 |
int i; |
| 574 |
|
| 575 |
if (parse_curlopt_key(data, &key, &ot)) { |
| 576 |
ast_log(LOG_ERROR, "Unrecognized option: '%s'\n", data); |
| 577 |
return -1; |
| 578 |
} |
| 579 |
|
| 580 |
if (chan) { |
| 581 |
|
| 582 |
|
| 583 |
ast_channel_lock(chan); |
| 584 |
store = ast_channel_datastore_find(chan, &curl_info, NULL); |
| 585 |
ast_channel_unlock(chan); |
| 586 |
|
| 587 |
if (store) { |
| 588 |
list[0] = store->data; |
| 589 |
list[1] = &global_curl_info; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
for (i = 0; i < 2; i++) { |
| 594 |
if (!list[i]) { |
| 595 |
break; |
| 596 |
} |
| 597 |
AST_LIST_LOCK(list[i]); |
| 598 |
AST_LIST_TRAVERSE(list[i], cur, list) { |
| 599 |
if (cur->key == key) { |
| 600 |
if (ot == OT_BOOLEAN || ot == OT_INTEGER) { |
| 601 |
if (buf) { |
| 602 |
snprintf(buf, len, "%ld", (long) cur->value); |
| 603 |
} else { |
| 604 |
ast_str_set(bufstr, len, "%ld", (long) cur->value); |
| 605 |
} |
| 606 |
} else if (ot == OT_INTEGER_MS) { |
| 607 |
if ((long) cur->value % 1000 == 0) { |
| 608 |
if (buf) { |
| 609 |
snprintf(buf, len, "%ld", (long)cur->value / 1000); |
| 610 |
} else { |
| 611 |
ast_str_set(bufstr, len, "%ld", (long) cur->value / 1000); |
| 612 |
} |
| 613 |
} else { |
| 614 |
if (buf) { |
| 615 |
snprintf(buf, len, "%.3f", (double) ((long) cur->value) / 1000.0); |
| 616 |
} else { |
| 617 |
ast_str_set(bufstr, len, "%.3f", (double) ((long) cur->value) / 1000.0); |
| 618 |
} |
| 619 |
} |
| 620 |
} else if (ot == OT_STRING) { |
| 621 |
ast_debug(1, "Found entry %p, with key %d and value %p\n", cur, cur->key, cur->value); |
| 622 |
if (buf) { |
| 623 |
ast_copy_string(buf, cur->value, len); |
| 624 |
} else { |
| 625 |
ast_str_set(bufstr, 0, "%s", (char *) cur->value); |
| 626 |
} |
| 627 |
} else if (key == CURLOPT_PROXYTYPE) { |
| 628 |
const char *strval = "unknown"; |
| 629 |
if (0) { |
| 630 |
#if CURLVERSION_ATLEAST(7,15,2) |
| 631 |
} else if ((long)cur->value == CURLPROXY_SOCKS4) { |
| 632 |
strval = "socks4"; |
| 633 |
#endif |
| 634 |
#if CURLVERSION_ATLEAST(7,18,0) |
| 635 |
} else if ((long)cur->value == CURLPROXY_SOCKS4A) { |
| 636 |
strval = "socks4a"; |
| 637 |
#endif |
| 638 |
} else if ((long)cur->value == CURLPROXY_SOCKS5) { |
| 639 |
strval = "socks5"; |
| 640 |
#if CURLVERSION_ATLEAST(7,18,0) |
| 641 |
} else if ((long)cur->value == CURLPROXY_SOCKS5_HOSTNAME) { |
| 642 |
strval = "socks5hostname"; |
| 643 |
#endif |
| 644 |
#if CURLVERSION_ATLEAST(7,10,0) |
| 645 |
} else if ((long)cur->value == CURLPROXY_HTTP) { |
| 646 |
strval = "http"; |
| 647 |
#endif |
| 648 |
} |
| 649 |
if (buf) { |
| 650 |
ast_copy_string(buf, strval, len); |
| 651 |
} else { |
| 652 |
ast_str_set(bufstr, 0, "%s", strval); |
| 653 |
} |
| 654 |
} else if (key == CURLOPT_SPECIAL_HASHCOMPAT) { |
| 655 |
const char *strval = "unknown"; |
| 656 |
if ((long) cur->value == HASHCOMPAT_LEGACY) { |
| 657 |
strval = "legacy"; |
| 658 |
} else if ((long) cur->value == HASHCOMPAT_YES) { |
| 659 |
strval = "yes"; |
| 660 |
} else if ((long) cur->value == HASHCOMPAT_NO) { |
| 661 |
strval = "no"; |
| 662 |
} |
| 663 |
if (buf) { |
| 664 |
ast_copy_string(buf, strval, len); |
| 665 |
} else { |
| 666 |
ast_str_set(bufstr, 0, "%s", strval); |
| 667 |
} |
| 668 |
} |
| 669 |
break; |
| 670 |
} |
| 671 |
} |
| 672 |
AST_LIST_UNLOCK(list[i]); |
| 673 |
if (cur) { |
| 674 |
break; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
return cur ? 0 : -1; |
| 679 |
} |
| 680 |
|
| 681 |
static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 682 |
{ |
| 683 |
return acf_curlopt_helper(chan, cmd, data, buf, NULL, len); |
| 684 |
} |
| 685 |
|
| 686 |
static int acf_curlopt_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len) |
| 687 |
{ |
| 688 |
return acf_curlopt_helper(chan, cmd, data, NULL, buf, len); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
struct curl_write_callback_data { |
| 693 |
|
| 694 |
struct ast_str *str; |
| 695 |
|
| 696 |
ssize_t len; |
| 697 |
|
| 698 |
FILE *out_file; |
| 699 |
}; |
| 700 |
|
| 701 |
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) |
| 702 |
{ |
| 703 |
register int realsize = 0; |
| 704 |
struct curl_write_callback_data *cb_data = data; |
| 705 |
|
| 706 |
if (cb_data->str) { |
| 707 |
realsize = size * nmemb; |
| 708 |
ast_str_append_substr(&cb_data->str, 0, ptr, realsize); |
| 709 |
} else if (cb_data->out_file) { |
| 710 |
realsize = fwrite(ptr, size, nmemb, cb_data->out_file); |
| 711 |
} |
| 712 |
|
| 713 |
return realsize; |
| 714 |
} |
| 715 |
|
| 716 |
static int curl_instance_init(void *data) |
| 717 |
{ |
| 718 |
CURL **curl = data; |
| 719 |
|
| 720 |
if (!(*curl = curl_easy_init())) |
| 721 |
return -1; |
| 722 |
|
| 723 |
curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1L); |
| 724 |
curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180L); |
| 725 |
curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); |
| 726 |
curl_easy_setopt(*curl, CURLOPT_USERAGENT, AST_CURL_USER_AGENT); |
| 727 |
|
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
static void curl_instance_cleanup(void *data) |
| 732 |
{ |
| 733 |
CURL **curl = data; |
| 734 |
|
| 735 |
curl_easy_cleanup(*curl); |
| 736 |
|
| 737 |
ast_free(data); |
| 738 |
} |
| 739 |
|
| 740 |
AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup); |
| 741 |
AST_THREADSTORAGE(thread_escapebuf); |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
static int url_is_vulnerable(const char *url) |
| 760 |
{ |
| 761 |
if (strpbrk(url, "\r\n")) { |
| 762 |
return 1; |
| 763 |
} |
| 764 |
|
| 765 |
return 0; |
| 766 |
} |
| 767 |
|
| 768 |
struct curl_args { |
| 769 |
const char *url; |
| 770 |
const char *postdata; |
| 771 |
struct curl_write_callback_data cb_data; |
| 772 |
}; |
| 773 |
|
| 774 |
static int acf_curl_helper(struct ast_channel *chan, struct curl_args *args) |
| 775 |
{ |
| 776 |
struct ast_str *escapebuf = ast_str_thread_get(&thread_escapebuf, 16); |
| 777 |
int ret = 0; |
| 778 |
long http_code = 0; |
| 779 |
size_t i; |
| 780 |
struct ast_vector_int hasfailurecode = { NULL }; |
| 781 |
char *failurecodestrings,*found; |
| 782 |
CURL **curl; |
| 783 |
struct curl_settings *cur; |
| 784 |
struct curl_slist *headers = NULL; |
| 785 |
struct ast_datastore *store = NULL; |
| 786 |
int hashcompat = 0; |
| 787 |
AST_LIST_HEAD(global_curl_info, curl_settings) *list = NULL; |
| 788 |
char curl_errbuf[CURL_ERROR_SIZE + 1]; |
| 789 |
|
| 790 |
if (!escapebuf) { |
| 791 |
return -1; |
| 792 |
} |
| 793 |
|
| 794 |
if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl)))) { |
| 795 |
ast_log(LOG_ERROR, "Cannot allocate curl structure\n"); |
| 796 |
return -1; |
| 797 |
} |
| 798 |
|
| 799 |
if (url_is_vulnerable(args->url)) { |
| 800 |
ast_log(LOG_ERROR, "URL '%s' is vulnerable to HTTP injection attacks. Aborting CURL() call.\n", args->url); |
| 801 |
return -1; |
| 802 |
} |
| 803 |
|
| 804 |
if (chan) { |
| 805 |
ast_autoservice_start(chan); |
| 806 |
} |
| 807 |
|
| 808 |
AST_VECTOR_INIT(&hasfailurecode, 0); |
| 809 |
AST_LIST_LOCK(&global_curl_info); |
| 810 |
AST_LIST_TRAVERSE(&global_curl_info, cur, list) { |
| 811 |
if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) { |
| 812 |
hashcompat = (long) cur->value; |
| 813 |
} else if (cur->key == CURLOPT_HTTPHEADER) { |
| 814 |
headers = curl_slist_append(headers, (char*) cur->value); |
| 815 |
} else if (cur->key == CURLOPT_SPECIAL_FAILURE_CODE) { |
| 816 |
failurecodestrings = (char*) cur->value; |
| 817 |
while( (found = strsep(&failurecodestrings, ",")) != NULL) { |
| 818 |
AST_VECTOR_APPEND(&hasfailurecode, atoi(found)); |
| 819 |
} |
| 820 |
} else { |
| 821 |
curl_easy_setopt(*curl, cur->key, cur->value); |
| 822 |
} |
| 823 |
} |
| 824 |
AST_LIST_UNLOCK(&global_curl_info); |
| 825 |
|
| 826 |
if (chan) { |
| 827 |
ast_channel_lock(chan); |
| 828 |
store = ast_channel_datastore_find(chan, &curl_info, NULL); |
| 829 |
ast_channel_unlock(chan); |
| 830 |
if (store) { |
| 831 |
list = store->data; |
| 832 |
AST_LIST_LOCK(list); |
| 833 |
AST_LIST_TRAVERSE(list, cur, list) { |
| 834 |
if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) { |
| 835 |
hashcompat = (long) cur->value; |
| 836 |
} else if (cur->key == CURLOPT_HTTPHEADER) { |
| 837 |
headers = curl_slist_append(headers, (char*) cur->value); |
| 838 |
} else if (cur->key == CURLOPT_SPECIAL_FAILURE_CODE) { |
| 839 |
failurecodestrings = (char*) cur->value; |
| 840 |
while( (found = strsep(&failurecodestrings, ",")) != NULL) { |
| 841 |
AST_VECTOR_APPEND(&hasfailurecode, atoi(found)); |
| 842 |
} |
| 843 |
} else { |
| 844 |
curl_easy_setopt(*curl, cur->key, cur->value); |
| 845 |
} |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
curl_easy_setopt(*curl, CURLOPT_URL, args->url); |
| 851 |
curl_easy_setopt(*curl, CURLOPT_FILE, (void *) &args->cb_data); |
| 852 |
|
| 853 |
if (args->postdata) { |
| 854 |
curl_easy_setopt(*curl, CURLOPT_POST, 1); |
| 855 |
curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, args->postdata); |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headers); |
| 862 |
|
| 863 |
|
| 864 |
curl_errbuf[0] = curl_errbuf[CURL_ERROR_SIZE] = '\0'; |
| 865 |
curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, curl_errbuf); |
| 866 |
|
| 867 |
if (curl_easy_perform(*curl) != 0) { |
| 868 |
ast_log(LOG_WARNING, "%s ('%s')\n", curl_errbuf, args->url); |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, (char*)NULL); |
| 876 |
curl_easy_getinfo (*curl, CURLINFO_RESPONSE_CODE, &http_code); |
| 877 |
|
| 878 |
for (i = 0; i < AST_VECTOR_SIZE(&hasfailurecode); ++i) { |
| 879 |
if (http_code == AST_VECTOR_GET(&hasfailurecode,i)){ |
| 880 |
ast_log(LOG_NOTICE, "%s%sCURL '%s' returned response code (%ld).\n", |
| 881 |
chan ? ast_channel_name(chan) : "", |
| 882 |
chan ? ast_channel_name(chan) : ": ", |
| 883 |
args->url, |
| 884 |
http_code); |
| 885 |
ret=-1; |
| 886 |
break; |
| 887 |
} |
| 888 |
} |
| 889 |
AST_VECTOR_FREE(&hasfailurecode); |
| 890 |
|
| 891 |
if (store) { |
| 892 |
AST_LIST_UNLOCK(list); |
| 893 |
} |
| 894 |
curl_slist_free_all(headers); |
| 895 |
|
| 896 |
if (args->postdata) { |
| 897 |
curl_easy_setopt(*curl, CURLOPT_POST, 0); |
| 898 |
} |
| 899 |
|
| 900 |
if (args->cb_data.str && ast_str_strlen(args->cb_data.str)) { |
| 901 |
ast_str_trim_blanks(args->cb_data.str); |
| 902 |
|
| 903 |
ast_debug(3, "CURL returned str='%s'\n", ast_str_buffer(args->cb_data.str)); |
| 904 |
if (hashcompat) { |
| 905 |
char *remainder = ast_str_buffer(args->cb_data.str); |
| 906 |
char *piece; |
| 907 |
struct ast_str *fields = ast_str_create(ast_str_strlen(args->cb_data.str) / 2); |
| 908 |
struct ast_str *values = ast_str_create(ast_str_strlen(args->cb_data.str) / 2); |
| 909 |
int rowcount = 0; |
| 910 |
while (fields && values && (piece = strsep(&remainder, "&"))) { |
| 911 |
char *name = strsep(&piece, "="); |
| 912 |
struct ast_flags mode = (hashcompat == HASHCOMPAT_LEGACY ? ast_uri_http_legacy : ast_uri_http); |
| 913 |
if (piece) { |
| 914 |
ast_uri_decode(piece, mode); |
| 915 |
} |
| 916 |
ast_uri_decode(name, mode); |
| 917 |
ast_str_append(&fields, 0, "%s%s", rowcount ? "," : "", ast_str_set_escapecommas(&escapebuf, 0, name, INT_MAX)); |
| 918 |
ast_str_append(&values, 0, "%s%s", rowcount ? "," : "", ast_str_set_escapecommas(&escapebuf, 0, S_OR(piece, ""), INT_MAX)); |
| 919 |
rowcount++; |
| 920 |
} |
| 921 |
pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields)); |
| 922 |
ast_str_set(&args->cb_data.str, 0, "%s", ast_str_buffer(values)); |
| 923 |
ast_free(fields); |
| 924 |
ast_free(values); |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
if (chan) { |
| 929 |
ast_autoservice_stop(chan); |
| 930 |
} |
| 931 |
|
| 932 |
return ret; |
| 933 |
} |
| 934 |
|
| 935 |
static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, struct ast_str **buf, ssize_t len) |
| 936 |
{ |
| 937 |
struct curl_args curl_params = { 0, }; |
| 938 |
int res; |
| 939 |
|
| 940 |
AST_DECLARE_APP_ARGS(args, |
| 941 |
AST_APP_ARG(url); |
| 942 |
AST_APP_ARG(postdata); |
| 943 |
); |
| 944 |
|
| 945 |
AST_STANDARD_APP_ARGS(args, info); |
| 946 |
|
| 947 |
if (ast_strlen_zero(info)) { |
| 948 |
ast_log(LOG_WARNING, "CURL requires an argument (URL)\n"); |
| 949 |
return -1; |
| 950 |
} |
| 951 |
|
| 952 |
curl_params.url = args.url; |
| 953 |
curl_params.postdata = args.postdata; |
| 954 |
curl_params.cb_data.str = ast_str_create(16); |
| 955 |
if (!curl_params.cb_data.str) { |
| 956 |
return -1; |
| 957 |
} |
| 958 |
|
| 959 |
res = acf_curl_helper(chan, &curl_params); |
| 960 |
ast_str_set(buf, len, "%s", ast_str_buffer(curl_params.cb_data.str)); |
| 961 |
ast_free(curl_params.cb_data.str); |
| 962 |
|
| 963 |
return res; |
| 964 |
} |
| 965 |
|
| 966 |
static int acf_curl_write(struct ast_channel *chan, const char *cmd, char *name, const char *value) |
| 967 |
{ |
| 968 |
struct curl_args curl_params = { 0, }; |
| 969 |
int res; |
| 970 |
char *args_value = ast_strdupa(value); |
| 971 |
AST_DECLARE_APP_ARGS(args, |
| 972 |
AST_APP_ARG(file_path); |
| 973 |
); |
| 974 |
|
| 975 |
AST_STANDARD_APP_ARGS(args, args_value); |
| 976 |
|
| 977 |
if (ast_strlen_zero(name)) { |
| 978 |
ast_log(LOG_WARNING, "CURL requires an argument (URL)\n"); |
| 979 |
return -1; |
| 980 |
} |
| 981 |
|
| 982 |
if (ast_strlen_zero(args.file_path)) { |
| 983 |
ast_log(LOG_WARNING, "CURL requires a file to write\n"); |
| 984 |
return -1; |
| 985 |
} |
| 986 |
|
| 987 |
curl_params.url = name; |
| 988 |
curl_params.cb_data.out_file = fopen(args.file_path, "w"); |
| 989 |
if (!curl_params.cb_data.out_file) { |
| 990 |
ast_log(LOG_WARNING, "Failed to open file %s: %s (%d)\n", |
| 991 |
args.file_path, |
| 992 |
strerror(errno), |
| 993 |
errno); |
| 994 |
return -1; |
| 995 |
} |
| 996 |
|
| 997 |
res = acf_curl_helper(chan, &curl_params); |
| 998 |
|
| 999 |
fclose(curl_params.cb_data.out_file); |
| 1000 |
|
| 1001 |
return res; |
| 1002 |
} |
| 1003 |
|
| 1004 |
static struct ast_custom_function acf_curl = { |
| 1005 |
.name = "CURL", |
| 1006 |
.read2 = acf_curl_exec, |
| 1007 |
.write = acf_curl_write, |
| 1008 |
}; |
| 1009 |
|
| 1010 |
static struct ast_custom_function acf_curlopt = { |
| 1011 |
.name = "CURLOPT", |
| 1012 |
.read = acf_curlopt_read, |
| 1013 |
.read2 = acf_curlopt_read2, |
| 1014 |
.write = acf_curlopt_write, |
| 1015 |
}; |
| 1016 |
|
| 1017 |
#ifdef TEST_FRAMEWORK |
| 1018 |
AST_TEST_DEFINE(vulnerable_url) |
| 1019 |
{ |
| 1020 |
const char *bad_urls [] = { |
| 1021 |
"http://example.com\r\nDELETE http://example.com/everything", |
| 1022 |
"http://example.com\rDELETE http://example.com/everything", |
| 1023 |
"http://example.com\nDELETE http://example.com/everything", |
| 1024 |
"\r\nhttp://example.com", |
| 1025 |
"\rhttp://example.com", |
| 1026 |
"\nhttp://example.com", |
| 1027 |
"http://example.com\r\n", |
| 1028 |
"http://example.com\r", |
| 1029 |
"http://example.com\n", |
| 1030 |
}; |
| 1031 |
const char *good_urls [] = { |
| 1032 |
"http://example.com", |
| 1033 |
"http://example.com/%5Cr%5Cn", |
| 1034 |
}; |
| 1035 |
int i; |
| 1036 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 1037 |
|
| 1038 |
switch (cmd) { |
| 1039 |
case TEST_INIT: |
| 1040 |
info->name = "vulnerable_url"; |
| 1041 |
info->category = "/funcs/func_curl/"; |
| 1042 |
info->summary = "cURL vulnerable URL test"; |
| 1043 |
info->description = |
| 1044 |
"Ensure that any combination of '\\r' or '\\n' in a URL invalidates the URL"; |
| 1045 |
case TEST_EXECUTE: |
| 1046 |
break; |
| 1047 |
} |
| 1048 |
|
| 1049 |
for (i = 0; i < ARRAY_LEN(bad_urls); ++i) { |
| 1050 |
if (!url_is_vulnerable(bad_urls[i])) { |
| 1051 |
ast_test_status_update(test, "String '%s' detected as valid when it should be invalid\n", bad_urls[i]); |
| 1052 |
res = AST_TEST_FAIL; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
for (i = 0; i < ARRAY_LEN(good_urls); ++i) { |
| 1057 |
if (url_is_vulnerable(good_urls[i])) { |
| 1058 |
ast_test_status_update(test, "String '%s' detected as invalid when it should be valid\n", good_urls[i]); |
| 1059 |
res = AST_TEST_FAIL; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
return res; |
| 1064 |
} |
| 1065 |
#endif |
| 1066 |
|
| 1067 |
static int unload_module(void) |
| 1068 |
{ |
| 1069 |
int res; |
| 1070 |
|
| 1071 |
res = ast_custom_function_unregister(&acf_curl); |
| 1072 |
res |= ast_custom_function_unregister(&acf_curlopt); |
| 1073 |
|
| 1074 |
AST_TEST_UNREGISTER(vulnerable_url); |
| 1075 |
|
| 1076 |
return res; |
| 1077 |
} |
| 1078 |
|
| 1079 |
static int load_module(void) |
| 1080 |
{ |
| 1081 |
int res; |
| 1082 |
|
| 1083 |
res = ast_custom_function_register_escalating(&acf_curl, AST_CFE_WRITE); |
| 1084 |
res |= ast_custom_function_register(&acf_curlopt); |
| 1085 |
|
| 1086 |
AST_TEST_REGISTER(vulnerable_url); |
| 1087 |
|
| 1088 |
return res; |
| 1089 |
} |
| 1090 |
|
| 1091 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Load external URL", |
| 1092 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1093 |
.load = load_module, |
| 1094 |
.unload = unload_module, |
| 1095 |
.load_pri = AST_MODPRI_REALTIME_DEPEND2, |
| 1096 |
.requires = "res_curl", |
| 1097 |
); |