func_curl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2006, Tilghman Lesher
  5. *
  6. * Tilghman Lesher <curl-20050919@the-tilghman.com>
  7. * and Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
  8. *
  9. * app_curl.c is distributed with no restrictions on usage or
  10. * redistribution.
  11. *
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. */
  19. /*! \file
  20. *
  21. * \brief Curl - Load a URL
  22. *
  23. * \author Tilghman Lesher <curl-20050919@the-tilghman.com>
  24. *
  25. * \note Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
  26. *
  27. * \extref Depends on the CURL library - http://curl.haxx.se/
  28. *
  29. * \ingroup functions
  30. */
  31. /*** MODULEINFO
  32. <depend>curl</depend>
  33. <support_level>core</support_level>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include <curl/curl.h>
  38. #include "asterisk/lock.h"
  39. #include "asterisk/file.h"
  40. #include "asterisk/channel.h"
  41. #include "asterisk/pbx.h"
  42. #include "asterisk/cli.h"
  43. #include "asterisk/module.h"
  44. #include "asterisk/app.h"
  45. #include "asterisk/utils.h"
  46. #include "asterisk/threadstorage.h"
  47. /*** DOCUMENTATION
  48. <function name="CURL" language="en_US">
  49. <synopsis>
  50. Retrieve content from a remote web or ftp server
  51. </synopsis>
  52. <syntax>
  53. <parameter name="url" required="true" />
  54. <parameter name="post-data">
  55. <para>If specified, an <literal>HTTP POST</literal> will be
  56. performed with the content of
  57. <replaceable>post-data</replaceable>, instead of an
  58. <literal>HTTP GET</literal> (default).</para>
  59. </parameter>
  60. </syntax>
  61. <description />
  62. <see-also>
  63. <ref type="function">CURLOPT</ref>
  64. </see-also>
  65. </function>
  66. <function name="CURLOPT" language="en_US">
  67. <synopsis>
  68. Sets various options for future invocations of CURL.
  69. </synopsis>
  70. <syntax>
  71. <parameter name="key" required="yes">
  72. <enumlist>
  73. <enum name="cookie">
  74. <para>A cookie to send with the request. Multiple
  75. cookies are supported.</para>
  76. </enum>
  77. <enum name="conntimeout">
  78. <para>Number of seconds to wait for a connection to succeed</para>
  79. </enum>
  80. <enum name="dnstimeout">
  81. <para>Number of seconds to wait for DNS to be resolved</para>
  82. </enum>
  83. <enum name="ftptext">
  84. <para>For FTP URIs, force a text transfer (boolean)</para>
  85. </enum>
  86. <enum name="ftptimeout">
  87. <para>For FTP URIs, number of seconds to wait for a
  88. server response</para>
  89. </enum>
  90. <enum name="header">
  91. <para>Include header information in the result
  92. (boolean)</para>
  93. </enum>
  94. <enum name="httptimeout">
  95. <para>For HTTP(S) URIs, number of seconds to wait for a
  96. server response</para>
  97. </enum>
  98. <enum name="maxredirs">
  99. <para>Maximum number of redirects to follow</para>
  100. </enum>
  101. <enum name="proxy">
  102. <para>Hostname or IP address to use as a proxy server</para>
  103. </enum>
  104. <enum name="proxytype">
  105. <para>Type of <literal>proxy</literal></para>
  106. <enumlist>
  107. <enum name="http" />
  108. <enum name="socks4" />
  109. <enum name="socks5" />
  110. </enumlist>
  111. </enum>
  112. <enum name="proxyport">
  113. <para>Port number of the <literal>proxy</literal></para>
  114. </enum>
  115. <enum name="proxyuserpwd">
  116. <para>A <replaceable>username</replaceable><literal>:</literal><replaceable>password</replaceable>
  117. combination to use for authenticating requests through a
  118. <literal>proxy</literal></para>
  119. </enum>
  120. <enum name="referer">
  121. <para>Referer URL to use for the request</para>
  122. </enum>
  123. <enum name="useragent">
  124. <para>UserAgent string to use for the request</para>
  125. </enum>
  126. <enum name="userpwd">
  127. <para>A <replaceable>username</replaceable><literal>:</literal><replaceable>password</replaceable>
  128. to use for authentication when the server response to
  129. an initial request indicates a 401 status code.</para>
  130. </enum>
  131. <enum name="ssl_verifypeer">
  132. <para>Whether to verify the server certificate against
  133. a list of known root certificate authorities (boolean).</para>
  134. </enum>
  135. <enum name="hashcompat">
  136. <para>Assuming the responses will be in <literal>key1=value1&amp;key2=value2</literal>
  137. format, reformat the response such that it can be used
  138. by the <literal>HASH</literal> function.</para>
  139. <enumlist>
  140. <enum name="yes" />
  141. <enum name="no" />
  142. <enum name="legacy">
  143. <para>Also translate <literal>+</literal> to the
  144. space character, in violation of current RFC
  145. standards.</para>
  146. </enum>
  147. </enumlist>
  148. </enum>
  149. </enumlist>
  150. </parameter>
  151. </syntax>
  152. <description>
  153. <para>Options may be set globally or per channel. Per-channel
  154. settings will override global settings.</para>
  155. </description>
  156. <see-also>
  157. <ref type="function">CURL</ref>
  158. <ref type="function">HASH</ref>
  159. </see-also>
  160. </function>
  161. ***/
  162. #define CURLVERSION_ATLEAST(a,b,c) \
  163. ((LIBCURL_VERSION_MAJOR > (a)) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR > (b))) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR == (b)) && (LIBCURL_VERSION_PATCH >= (c))))
  164. #define CURLOPT_SPECIAL_HASHCOMPAT -500
  165. static void curlds_free(void *data);
  166. static const struct ast_datastore_info curl_info = {
  167. .type = "CURL",
  168. .destroy = curlds_free,
  169. };
  170. struct curl_settings {
  171. AST_LIST_ENTRY(curl_settings) list;
  172. CURLoption key;
  173. void *value;
  174. };
  175. AST_LIST_HEAD_STATIC(global_curl_info, curl_settings);
  176. static void curlds_free(void *data)
  177. {
  178. AST_LIST_HEAD(global_curl_info, curl_settings) *list = data;
  179. struct curl_settings *setting;
  180. if (!list) {
  181. return;
  182. }
  183. while ((setting = AST_LIST_REMOVE_HEAD(list, list))) {
  184. free(setting);
  185. }
  186. AST_LIST_HEAD_DESTROY(list);
  187. }
  188. enum optiontype {
  189. OT_BOOLEAN,
  190. OT_INTEGER,
  191. OT_INTEGER_MS,
  192. OT_STRING,
  193. OT_ENUM,
  194. };
  195. enum hashcompat {
  196. HASHCOMPAT_NO = 0,
  197. HASHCOMPAT_YES,
  198. HASHCOMPAT_LEGACY,
  199. };
  200. static int parse_curlopt_key(const char *name, CURLoption *key, enum optiontype *ot)
  201. {
  202. if (!strcasecmp(name, "header")) {
  203. *key = CURLOPT_HEADER;
  204. *ot = OT_BOOLEAN;
  205. } else if (!strcasecmp(name, "proxy")) {
  206. *key = CURLOPT_PROXY;
  207. *ot = OT_STRING;
  208. } else if (!strcasecmp(name, "proxyport")) {
  209. *key = CURLOPT_PROXYPORT;
  210. *ot = OT_INTEGER;
  211. } else if (!strcasecmp(name, "proxytype")) {
  212. *key = CURLOPT_PROXYTYPE;
  213. *ot = OT_ENUM;
  214. } else if (!strcasecmp(name, "dnstimeout")) {
  215. *key = CURLOPT_DNS_CACHE_TIMEOUT;
  216. *ot = OT_INTEGER;
  217. } else if (!strcasecmp(name, "userpwd")) {
  218. *key = CURLOPT_USERPWD;
  219. *ot = OT_STRING;
  220. } else if (!strcasecmp(name, "proxyuserpwd")) {
  221. *key = CURLOPT_PROXYUSERPWD;
  222. *ot = OT_STRING;
  223. } else if (!strcasecmp(name, "maxredirs")) {
  224. *key = CURLOPT_MAXREDIRS;
  225. *ot = OT_INTEGER;
  226. } else if (!strcasecmp(name, "referer")) {
  227. *key = CURLOPT_REFERER;
  228. *ot = OT_STRING;
  229. } else if (!strcasecmp(name, "useragent")) {
  230. *key = CURLOPT_USERAGENT;
  231. *ot = OT_STRING;
  232. } else if (!strcasecmp(name, "cookie")) {
  233. *key = CURLOPT_COOKIE;
  234. *ot = OT_STRING;
  235. } else if (!strcasecmp(name, "ftptimeout")) {
  236. *key = CURLOPT_FTP_RESPONSE_TIMEOUT;
  237. *ot = OT_INTEGER;
  238. } else if (!strcasecmp(name, "httptimeout")) {
  239. #if CURLVERSION_ATLEAST(7,16,2)
  240. *key = CURLOPT_TIMEOUT_MS;
  241. *ot = OT_INTEGER_MS;
  242. #else
  243. *key = CURLOPT_TIMEOUT;
  244. *ot = OT_INTEGER;
  245. #endif
  246. } else if (!strcasecmp(name, "conntimeout")) {
  247. #if CURLVERSION_ATLEAST(7,16,2)
  248. *key = CURLOPT_CONNECTTIMEOUT_MS;
  249. *ot = OT_INTEGER_MS;
  250. #else
  251. *key = CURLOPT_CONNECTTIMEOUT;
  252. *ot = OT_INTEGER;
  253. #endif
  254. } else if (!strcasecmp(name, "ftptext")) {
  255. *key = CURLOPT_TRANSFERTEXT;
  256. *ot = OT_BOOLEAN;
  257. } else if (!strcasecmp(name, "ssl_verifypeer")) {
  258. *key = CURLOPT_SSL_VERIFYPEER;
  259. *ot = OT_BOOLEAN;
  260. } else if (!strcasecmp(name, "hashcompat")) {
  261. *key = CURLOPT_SPECIAL_HASHCOMPAT;
  262. *ot = OT_ENUM;
  263. } else {
  264. return -1;
  265. }
  266. return 0;
  267. }
  268. static int acf_curlopt_write(struct ast_channel *chan, const char *cmd, char *name, const char *value)
  269. {
  270. struct ast_datastore *store;
  271. struct global_curl_info *list;
  272. struct curl_settings *cur, *new = NULL;
  273. CURLoption key;
  274. enum optiontype ot;
  275. if (chan) {
  276. if (!(store = ast_channel_datastore_find(chan, &curl_info, NULL))) {
  277. /* Create a new datastore */
  278. if (!(store = ast_datastore_alloc(&curl_info, NULL))) {
  279. ast_log(LOG_ERROR, "Unable to allocate new datastore. Cannot set any CURL options\n");
  280. return -1;
  281. }
  282. if (!(list = ast_calloc(1, sizeof(*list)))) {
  283. ast_log(LOG_ERROR, "Unable to allocate list head. Cannot set any CURL options\n");
  284. ast_datastore_free(store);
  285. return -1;
  286. }
  287. store->data = list;
  288. AST_LIST_HEAD_INIT(list);
  289. ast_channel_datastore_add(chan, store);
  290. } else {
  291. list = store->data;
  292. }
  293. } else {
  294. /* Populate the global structure */
  295. list = &global_curl_info;
  296. }
  297. if (!parse_curlopt_key(name, &key, &ot)) {
  298. if (ot == OT_BOOLEAN) {
  299. if ((new = ast_calloc(1, sizeof(*new)))) {
  300. new->value = (void *)((long) ast_true(value));
  301. }
  302. } else if (ot == OT_INTEGER) {
  303. long tmp = atol(value);
  304. if ((new = ast_calloc(1, sizeof(*new)))) {
  305. new->value = (void *)tmp;
  306. }
  307. } else if (ot == OT_INTEGER_MS) {
  308. long tmp = atof(value) * 1000.0;
  309. if ((new = ast_calloc(1, sizeof(*new)))) {
  310. new->value = (void *)tmp;
  311. }
  312. } else if (ot == OT_STRING) {
  313. if ((new = ast_calloc(1, sizeof(*new) + strlen(value) + 1))) {
  314. new->value = (char *)new + sizeof(*new);
  315. strcpy(new->value, value);
  316. }
  317. } else if (ot == OT_ENUM) {
  318. if (key == CURLOPT_PROXYTYPE) {
  319. long ptype =
  320. #if CURLVERSION_ATLEAST(7,10,0)
  321. CURLPROXY_HTTP;
  322. #else
  323. CURLPROXY_SOCKS5;
  324. #endif
  325. if (0) {
  326. #if CURLVERSION_ATLEAST(7,15,2)
  327. } else if (!strcasecmp(value, "socks4")) {
  328. ptype = CURLPROXY_SOCKS4;
  329. #endif
  330. #if CURLVERSION_ATLEAST(7,18,0)
  331. } else if (!strcasecmp(value, "socks4a")) {
  332. ptype = CURLPROXY_SOCKS4A;
  333. #endif
  334. #if CURLVERSION_ATLEAST(7,18,0)
  335. } else if (!strcasecmp(value, "socks5")) {
  336. ptype = CURLPROXY_SOCKS5;
  337. #endif
  338. #if CURLVERSION_ATLEAST(7,18,0)
  339. } else if (!strncasecmp(value, "socks5", 6)) {
  340. ptype = CURLPROXY_SOCKS5_HOSTNAME;
  341. #endif
  342. }
  343. if ((new = ast_calloc(1, sizeof(*new)))) {
  344. new->value = (void *)ptype;
  345. }
  346. } else if (key == CURLOPT_SPECIAL_HASHCOMPAT) {
  347. if ((new = ast_calloc(1, sizeof(*new)))) {
  348. new->value = (void *) (long) (!strcasecmp(value, "legacy") ? HASHCOMPAT_LEGACY : ast_true(value) ? HASHCOMPAT_YES : HASHCOMPAT_NO);
  349. }
  350. } else {
  351. /* Highly unlikely */
  352. goto yuck;
  353. }
  354. }
  355. /* Memory allocation error */
  356. if (!new) {
  357. return -1;
  358. }
  359. new->key = key;
  360. } else {
  361. yuck:
  362. ast_log(LOG_ERROR, "Unrecognized option: %s\n", name);
  363. return -1;
  364. }
  365. /* Remove any existing entry */
  366. AST_LIST_LOCK(list);
  367. AST_LIST_TRAVERSE_SAFE_BEGIN(list, cur, list) {
  368. if (cur->key == new->key) {
  369. AST_LIST_REMOVE_CURRENT(list);
  370. free(cur);
  371. break;
  372. }
  373. }
  374. AST_LIST_TRAVERSE_SAFE_END
  375. /* Insert new entry */
  376. ast_debug(1, "Inserting entry %p with key %d and value %p\n", new, new->key, new->value);
  377. AST_LIST_INSERT_TAIL(list, new, list);
  378. AST_LIST_UNLOCK(list);
  379. return 0;
  380. }
  381. static int acf_curlopt_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, ssize_t len)
  382. {
  383. struct ast_datastore *store;
  384. struct global_curl_info *list[2] = { &global_curl_info, NULL };
  385. struct curl_settings *cur = NULL;
  386. CURLoption key;
  387. enum optiontype ot;
  388. int i;
  389. if (parse_curlopt_key(data, &key, &ot)) {
  390. ast_log(LOG_ERROR, "Unrecognized option: '%s'\n", data);
  391. return -1;
  392. }
  393. if (chan && (store = ast_channel_datastore_find(chan, &curl_info, NULL))) {
  394. list[0] = store->data;
  395. list[1] = &global_curl_info;
  396. }
  397. for (i = 0; i < 2; i++) {
  398. if (!list[i]) {
  399. break;
  400. }
  401. AST_LIST_LOCK(list[i]);
  402. AST_LIST_TRAVERSE(list[i], cur, list) {
  403. if (cur->key == key) {
  404. if (ot == OT_BOOLEAN || ot == OT_INTEGER) {
  405. if (buf) {
  406. snprintf(buf, len, "%ld", (long) cur->value);
  407. } else {
  408. ast_str_set(bufstr, len, "%ld", (long) cur->value);
  409. }
  410. } else if (ot == OT_INTEGER_MS) {
  411. if ((long) cur->value % 1000 == 0) {
  412. if (buf) {
  413. snprintf(buf, len, "%ld", (long)cur->value / 1000);
  414. } else {
  415. ast_str_set(bufstr, len, "%ld", (long) cur->value / 1000);
  416. }
  417. } else {
  418. if (buf) {
  419. snprintf(buf, len, "%.3f", (double) ((long) cur->value) / 1000.0);
  420. } else {
  421. ast_str_set(bufstr, len, "%.3f", (double) ((long) cur->value) / 1000.0);
  422. }
  423. }
  424. } else if (ot == OT_STRING) {
  425. ast_debug(1, "Found entry %p, with key %d and value %p\n", cur, cur->key, cur->value);
  426. if (buf) {
  427. ast_copy_string(buf, cur->value, len);
  428. } else {
  429. ast_str_set(bufstr, 0, "%s", (char *) cur->value);
  430. }
  431. } else if (key == CURLOPT_PROXYTYPE) {
  432. const char *strval = "unknown";
  433. if (0) {
  434. #if CURLVERSION_ATLEAST(7,15,2)
  435. } else if ((long)cur->value == CURLPROXY_SOCKS4) {
  436. strval = "socks4";
  437. #endif
  438. #if CURLVERSION_ATLEAST(7,18,0)
  439. } else if ((long)cur->value == CURLPROXY_SOCKS4A) {
  440. strval = "socks4a";
  441. #endif
  442. } else if ((long)cur->value == CURLPROXY_SOCKS5) {
  443. strval = "socks5";
  444. #if CURLVERSION_ATLEAST(7,18,0)
  445. } else if ((long)cur->value == CURLPROXY_SOCKS5_HOSTNAME) {
  446. strval = "socks5hostname";
  447. #endif
  448. #if CURLVERSION_ATLEAST(7,10,0)
  449. } else if ((long)cur->value == CURLPROXY_HTTP) {
  450. strval = "http";
  451. #endif
  452. }
  453. if (buf) {
  454. ast_copy_string(buf, strval, len);
  455. } else {
  456. ast_str_set(bufstr, 0, "%s", strval);
  457. }
  458. } else if (key == CURLOPT_SPECIAL_HASHCOMPAT) {
  459. const char *strval = "unknown";
  460. if ((long) cur->value == HASHCOMPAT_LEGACY) {
  461. strval = "legacy";
  462. } else if ((long) cur->value == HASHCOMPAT_YES) {
  463. strval = "yes";
  464. } else if ((long) cur->value == HASHCOMPAT_NO) {
  465. strval = "no";
  466. }
  467. if (buf) {
  468. ast_copy_string(buf, strval, len);
  469. } else {
  470. ast_str_set(bufstr, 0, "%s", strval);
  471. }
  472. }
  473. break;
  474. }
  475. }
  476. AST_LIST_UNLOCK(list[i]);
  477. if (cur) {
  478. break;
  479. }
  480. }
  481. return cur ? 0 : -1;
  482. }
  483. static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  484. {
  485. return acf_curlopt_helper(chan, cmd, data, buf, NULL, len);
  486. }
  487. static int acf_curlopt_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  488. {
  489. return acf_curlopt_helper(chan, cmd, data, NULL, buf, len);
  490. }
  491. static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
  492. {
  493. register int realsize = size * nmemb;
  494. struct ast_str **pstr = (struct ast_str **)data;
  495. ast_debug(3, "Called with data=%p, str=%p, realsize=%d, len=%zu, used=%zu\n", data, *pstr, realsize, ast_str_size(*pstr), ast_str_strlen(*pstr));
  496. ast_str_append_substr(pstr, 0, ptr, realsize);
  497. ast_debug(3, "Now, len=%zu, used=%zu\n", ast_str_size(*pstr), ast_str_strlen(*pstr));
  498. return realsize;
  499. }
  500. static const char * const global_useragent = "asterisk-libcurl-agent/1.0";
  501. static int curl_instance_init(void *data)
  502. {
  503. CURL **curl = data;
  504. if (!(*curl = curl_easy_init()))
  505. return -1;
  506. curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
  507. curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
  508. curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  509. curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
  510. return 0;
  511. }
  512. static void curl_instance_cleanup(void *data)
  513. {
  514. CURL **curl = data;
  515. curl_easy_cleanup(*curl);
  516. ast_free(data);
  517. }
  518. AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
  519. AST_THREADSTORAGE(thread_escapebuf);
  520. static int acf_curl_helper(struct ast_channel *chan, const char *cmd, char *info, char *buf, struct ast_str **input_str, ssize_t len)
  521. {
  522. struct ast_str *escapebuf = ast_str_thread_get(&thread_escapebuf, 16);
  523. struct ast_str *str = ast_str_create(16);
  524. int ret = -1;
  525. AST_DECLARE_APP_ARGS(args,
  526. AST_APP_ARG(url);
  527. AST_APP_ARG(postdata);
  528. );
  529. CURL **curl;
  530. struct curl_settings *cur;
  531. struct ast_datastore *store = NULL;
  532. int hashcompat = 0;
  533. AST_LIST_HEAD(global_curl_info, curl_settings) *list = NULL;
  534. char curl_errbuf[CURL_ERROR_SIZE + 1]; /* add one to be safe */
  535. if (buf) {
  536. *buf = '\0';
  537. }
  538. if (!str) {
  539. return -1;
  540. }
  541. if (!escapebuf) {
  542. ast_free(str);
  543. return -1;
  544. }
  545. if (ast_strlen_zero(info)) {
  546. ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
  547. ast_free(str);
  548. return -1;
  549. }
  550. AST_STANDARD_APP_ARGS(args, info);
  551. if (chan) {
  552. ast_autoservice_start(chan);
  553. }
  554. if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl)))) {
  555. ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
  556. ast_free(str);
  557. return -1;
  558. }
  559. AST_LIST_LOCK(&global_curl_info);
  560. AST_LIST_TRAVERSE(&global_curl_info, cur, list) {
  561. if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) {
  562. hashcompat = (long) cur->value;
  563. } else {
  564. curl_easy_setopt(*curl, cur->key, cur->value);
  565. }
  566. }
  567. if (chan && (store = ast_channel_datastore_find(chan, &curl_info, NULL))) {
  568. list = store->data;
  569. AST_LIST_LOCK(list);
  570. AST_LIST_TRAVERSE(list, cur, list) {
  571. if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) {
  572. hashcompat = (long) cur->value;
  573. } else {
  574. curl_easy_setopt(*curl, cur->key, cur->value);
  575. }
  576. }
  577. }
  578. curl_easy_setopt(*curl, CURLOPT_URL, args.url);
  579. curl_easy_setopt(*curl, CURLOPT_FILE, (void *) &str);
  580. if (args.postdata) {
  581. curl_easy_setopt(*curl, CURLOPT_POST, 1);
  582. curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, args.postdata);
  583. }
  584. /* Temporarily assign a buffer for curl to write errors to. */
  585. curl_errbuf[0] = curl_errbuf[CURL_ERROR_SIZE] = '\0';
  586. curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, curl_errbuf);
  587. if (curl_easy_perform(*curl) != 0) {
  588. ast_log(LOG_WARNING, "%s ('%s')\n", curl_errbuf, args.url);
  589. }
  590. /* Reset buffer to NULL so curl doesn't try to write to it when the
  591. * buffer is deallocated. Documentation is vague about allowing NULL
  592. * here, but the source allows it. See: "typecheck: allow NULL to unset
  593. * CURLOPT_ERRORBUFFER" (62bcf005f4678a93158358265ba905bace33b834). */
  594. curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, (char*)NULL);
  595. if (store) {
  596. AST_LIST_UNLOCK(list);
  597. }
  598. AST_LIST_UNLOCK(&global_curl_info);
  599. if (args.postdata) {
  600. curl_easy_setopt(*curl, CURLOPT_POST, 0);
  601. }
  602. if (ast_str_strlen(str)) {
  603. ast_str_trim_blanks(str);
  604. ast_debug(3, "str='%s'\n", ast_str_buffer(str));
  605. if (hashcompat) {
  606. char *remainder = ast_str_buffer(str);
  607. char *piece;
  608. struct ast_str *fields = ast_str_create(ast_str_strlen(str) / 2);
  609. struct ast_str *values = ast_str_create(ast_str_strlen(str) / 2);
  610. int rowcount = 0;
  611. while (fields && values && (piece = strsep(&remainder, "&"))) {
  612. char *name = strsep(&piece, "=");
  613. struct ast_flags mode = (hashcompat == HASHCOMPAT_LEGACY ? ast_uri_http_legacy : ast_uri_http);
  614. if (piece) {
  615. ast_uri_decode(piece, mode);
  616. }
  617. ast_uri_decode(name, mode);
  618. ast_str_append(&fields, 0, "%s%s", rowcount ? "," : "", ast_str_set_escapecommas(&escapebuf, 0, name, INT_MAX));
  619. ast_str_append(&values, 0, "%s%s", rowcount ? "," : "", ast_str_set_escapecommas(&escapebuf, 0, S_OR(piece, ""), INT_MAX));
  620. rowcount++;
  621. }
  622. pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
  623. if (buf) {
  624. ast_copy_string(buf, ast_str_buffer(values), len);
  625. } else {
  626. ast_str_set(input_str, len, "%s", ast_str_buffer(values));
  627. }
  628. ast_free(fields);
  629. ast_free(values);
  630. } else {
  631. if (buf) {
  632. ast_copy_string(buf, ast_str_buffer(str), len);
  633. } else {
  634. ast_str_set(input_str, len, "%s", ast_str_buffer(str));
  635. }
  636. }
  637. ret = 0;
  638. }
  639. ast_free(str);
  640. if (chan)
  641. ast_autoservice_stop(chan);
  642. return ret;
  643. }
  644. static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, char *buf, size_t len)
  645. {
  646. return acf_curl_helper(chan, cmd, info, buf, NULL, len);
  647. }
  648. static int acf_curl2_exec(struct ast_channel *chan, const char *cmd, char *info, struct ast_str **buf, ssize_t len)
  649. {
  650. return acf_curl_helper(chan, cmd, info, NULL, buf, len);
  651. }
  652. static struct ast_custom_function acf_curl = {
  653. .name = "CURL",
  654. .synopsis = "Retrieves the contents of a URL",
  655. .syntax = "CURL(url[,post-data])",
  656. .desc =
  657. " url - URL to retrieve\n"
  658. " post-data - Optional data to send as a POST (GET is default action)\n",
  659. .read = acf_curl_exec,
  660. .read2 = acf_curl2_exec,
  661. };
  662. static struct ast_custom_function acf_curlopt = {
  663. .name = "CURLOPT",
  664. .synopsis = "Set options for use with the CURL() function",
  665. .syntax = "CURLOPT(<option>)",
  666. .desc =
  667. " cookie - Send cookie with request [none]\n"
  668. " conntimeout - Number of seconds to wait for connection\n"
  669. " dnstimeout - Number of seconds to wait for DNS response\n"
  670. " ftptext - For FTP, force a text transfer (boolean)\n"
  671. " ftptimeout - For FTP, the server response timeout\n"
  672. " header - Retrieve header information (boolean)\n"
  673. " httptimeout - Number of seconds to wait for HTTP response\n"
  674. " maxredirs - Maximum number of redirects to follow\n"
  675. " proxy - Hostname or IP to use as a proxy\n"
  676. " proxytype - http, socks4, or socks5\n"
  677. " proxyport - port number of the proxy\n"
  678. " proxyuserpwd - A <user>:<pass> to use for authentication\n"
  679. " referer - Referer URL to use for the request\n"
  680. " useragent - UserAgent string to use\n"
  681. " userpwd - A <user>:<pass> to use for authentication\n"
  682. " ssl_verifypeer - Whether to verify the peer certificate (boolean)\n"
  683. " hashcompat - Result data will be compatible for use with HASH()\n"
  684. " - if value is \"legacy\", will translate '+' to ' '\n"
  685. "",
  686. .read = acf_curlopt_read,
  687. .read2 = acf_curlopt_read2,
  688. .write = acf_curlopt_write,
  689. };
  690. static int unload_module(void)
  691. {
  692. int res;
  693. res = ast_custom_function_unregister(&acf_curl);
  694. res |= ast_custom_function_unregister(&acf_curlopt);
  695. return res;
  696. }
  697. static int load_module(void)
  698. {
  699. int res;
  700. if (!ast_module_check("res_curl.so")) {
  701. if (ast_load_resource("res_curl.so") != AST_MODULE_LOAD_SUCCESS) {
  702. ast_log(LOG_ERROR, "Cannot load res_curl, so func_curl cannot be loaded\n");
  703. return AST_MODULE_LOAD_DECLINE;
  704. }
  705. }
  706. res = ast_custom_function_register(&acf_curl);
  707. res |= ast_custom_function_register(&acf_curlopt);
  708. return res;
  709. }
  710. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Load external URL",
  711. .support_level = AST_MODULE_SUPPORT_CORE,
  712. .load = load_module,
  713. .unload = unload_module,
  714. .load_pri = AST_MODPRI_REALTIME_DEPEND2,
  715. );