utils.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * utils Module
  3. *
  4. * Copyright (C) 2008 Juha Heinanen
  5. * Copyright (C) 2009 1&1 Internet AG
  6. * Copyright (C) 2013 Carsten Bock, ng-voice GmbH
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * -------
  26. * 2008-11-24: Introduced utils module and its first function: http_query.
  27. *
  28. */
  29. /*! \file
  30. * \brief SIP-router utils :: Module core
  31. * \ingroup utils
  32. */
  33. /*! \defgroup Utils SIP-router :: Various utilities
  34. *
  35. */
  36. #include <curl/curl.h>
  37. #include "../../mod_fix.h"
  38. #include "../../sr_module.h"
  39. #include "../../ut.h"
  40. #include "../../forward.h"
  41. #include "../../resolve.h"
  42. #include "../../locking.h"
  43. #include "../../script_cb.h"
  44. #include "../../mem/shm_mem.h"
  45. #include "../../lib/srdb1/db.h"
  46. #include "functions.h"
  47. #include "conf.h"
  48. #include "xcap_auth.h"
  49. MODULE_VERSION
  50. #define XCAP_TABLE_VERSION 4
  51. /* Module parameter variables */
  52. int http_query_timeout = 4;
  53. static int forward_active = 0;
  54. static int mp_max_id = 0;
  55. static char* mp_switch = "";
  56. static char* mp_filter = "";
  57. static char* mp_proxy = "";
  58. str xcap_table= str_init("xcap");
  59. str pres_db_url = {0, 0};
  60. /* lock for configuration access */
  61. static gen_lock_t *conf_lock = NULL;
  62. /* FIFO interface functions */
  63. static struct mi_root* forward_fifo_list(struct mi_root* cmd_tree, void *param);
  64. static struct mi_root* forward_fifo_switch(struct mi_root* cmd_tree, void* param);
  65. static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param);
  66. static struct mi_root* forward_fifo_proxy(struct mi_root* cmd_tree, void* param);
  67. /* Database connection */
  68. db1_con_t *pres_dbh = NULL;
  69. db_func_t pres_dbf;
  70. /* Module management function prototypes */
  71. static int mod_init(void);
  72. static int child_init(int);
  73. static void destroy(void);
  74. /* Fixup functions to be defined later */
  75. static int fixup_http_query_get(void** param, int param_no);
  76. static int fixup_free_http_query_get(void** param, int param_no);
  77. static int fixup_http_query_post(void** param, int param_no);
  78. static int fixup_free_http_query_post(void** param, int param_no);
  79. /* Wrappers for http_query to be defined later */
  80. static int w_http_query(struct sip_msg* _m, char* _url, char* _result);
  81. static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result);
  82. /* forward function */
  83. int utils_forward(struct sip_msg *msg, int id, int proto);
  84. /* Exported functions */
  85. static cmd_export_t cmds[] = {
  86. {"http_query", (cmd_function)w_http_query, 2, fixup_http_query_get,
  87. fixup_free_http_query_get,
  88. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  89. {"http_query", (cmd_function)w_http_query_post, 3, fixup_http_query_post,
  90. fixup_free_http_query_post,
  91. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  92. {"xcap_auth_status", (cmd_function)xcap_auth_status, 2, fixup_pvar_pvar,
  93. fixup_free_pvar_pvar, REQUEST_ROUTE},
  94. {0, 0, 0, 0, 0, 0}
  95. };
  96. /* Exported parameters */
  97. static param_export_t params[] = {
  98. {"pres_db_url", PARAM_STR, &pres_db_url},
  99. {"xcap_table", PARAM_STR, &xcap_table},
  100. {"http_query_timeout", INT_PARAM, &http_query_timeout},
  101. {"forward_active", INT_PARAM, &forward_active},
  102. {0, 0, 0}
  103. };
  104. static mi_export_t mi_cmds[] = {
  105. { "forward_list", forward_fifo_list, MI_NO_INPUT_FLAG, 0, 0 },
  106. { "forward_switch", forward_fifo_switch, 0, 0, 0 },
  107. { "forward_filter", forward_fifo_filter, 0, 0, 0 },
  108. { "forward_proxy", forward_fifo_proxy, 0, 0, 0 },
  109. { 0, 0, 0, 0, 0}
  110. };
  111. /* Module interface */
  112. struct module_exports exports = {
  113. "utils",
  114. DEFAULT_DLFLAGS, /* dlopen flags */
  115. cmds, /* Exported functions */
  116. params, /* Exported parameters */
  117. 0, /* exported statistics */
  118. mi_cmds, /* exported MI functions */
  119. 0, /* exported pseudo-variables */
  120. 0, /* extra processes */
  121. mod_init, /* module initialization function */
  122. 0, /* response function*/
  123. destroy, /* destroy function */
  124. child_init /* per-child init function */
  125. };
  126. static int init_shmlock(void)
  127. {
  128. conf_lock = lock_alloc();
  129. if (conf_lock == NULL) {
  130. LM_CRIT("cannot allocate memory for lock.\n");
  131. return -1;
  132. }
  133. if (lock_init(conf_lock) == 0) {
  134. LM_CRIT("cannot initialize lock.\n");
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. static int pre_script_filter(struct sip_msg *msg, unsigned int flags, void *unused)
  140. {
  141. /* use id 0 for pre script callback */
  142. utils_forward(msg, 0, PROTO_UDP);
  143. /* always return 1 so that routing skript is called for msg */
  144. return 1;
  145. }
  146. static void destroy_shmlock(void)
  147. {
  148. if (conf_lock) {
  149. lock_destroy(conf_lock);
  150. lock_dealloc((void *)conf_lock);
  151. conf_lock = NULL;
  152. }
  153. }
  154. static void pres_db_close(void) {
  155. if (pres_dbh) {
  156. pres_dbf.close(pres_dbh);
  157. pres_dbh = NULL;
  158. }
  159. }
  160. static int pres_db_init(void) {
  161. if (!pres_db_url.s || !pres_db_url.len) {
  162. LM_INFO("xcap_auth_status function is disabled\n");
  163. return 0;
  164. }
  165. if (db_bind_mod(&pres_db_url, &pres_dbf) < 0) {
  166. LM_ERR("can't bind database module\n");
  167. return -1;
  168. }
  169. if ((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) {
  170. LM_ERR("can't connect to database\n");
  171. return -1;
  172. }
  173. if (db_check_table_version(&pres_dbf, pres_dbh, &xcap_table,
  174. XCAP_TABLE_VERSION) < 0) {
  175. LM_ERR("during table version check\n");
  176. pres_db_close();
  177. return -1;
  178. }
  179. pres_db_close();
  180. return 0;
  181. }
  182. static int pres_db_open(void) {
  183. if (!pres_db_url.s || !pres_db_url.len) {
  184. return 0;
  185. }
  186. if (pres_dbh) {
  187. pres_dbf.close(pres_dbh);
  188. }
  189. if ((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) {
  190. LM_ERR("can't connect to database\n");
  191. return -1;
  192. }
  193. if (pres_dbf.use_table(pres_dbh, &xcap_table) < 0) {
  194. LM_ERR("in use_table: %.*s\n", xcap_table.len, xcap_table.s);
  195. return -1;
  196. }
  197. return 0;
  198. }
  199. /* Module initialization function */
  200. static int mod_init(void)
  201. {
  202. if(register_mi_mod(exports.name, mi_cmds)!=0)
  203. {
  204. LM_ERR("failed to register MI commands\n");
  205. return -1;
  206. }
  207. /* Initialize curl */
  208. if (curl_global_init(CURL_GLOBAL_ALL)) {
  209. LM_ERR("curl_global_init failed\n");
  210. return -1;
  211. }
  212. if (init_shmlock() != 0) {
  213. LM_CRIT("cannot initialize shmlock.\n");
  214. return -1;
  215. }
  216. if (conf_init(mp_max_id) < 0) {
  217. LM_CRIT("cannot initialize configuration.\n");
  218. return -1;
  219. }
  220. /* read module parameters and update configuration structure */
  221. if (conf_parse_proxy(mp_proxy) < 0) {
  222. LM_CRIT("cannot parse proxy module parameter.\n");
  223. return -1;
  224. }
  225. if (conf_parse_filter(mp_filter) < 0) {
  226. LM_CRIT("cannot parse filter module parameter.\n");
  227. return -1;
  228. }
  229. if (conf_parse_switch(mp_switch) < 0) {
  230. LM_CRIT("cannot parse switch module parameter.\n");
  231. return -1;
  232. }
  233. if (forward_active == 1) {
  234. /* register callback for id 0 */
  235. if (register_script_cb(pre_script_filter, PRE_SCRIPT_CB|ONREPLY_CB, 0) < 0) {
  236. LM_CRIT("cannot register script callback for requests.\n");
  237. return -1;
  238. }
  239. if (register_script_cb(pre_script_filter, PRE_SCRIPT_CB|ONREPLY_CB, 0) < 0) {
  240. LM_CRIT("cannot register script callback for replies.\n");
  241. return -1;
  242. }
  243. } else {
  244. LM_INFO("forward functionality disabled");
  245. }
  246. /* presence database */
  247. LM_DBG("pres_db_url=%s/%d/%p\n", ZSW(pres_db_url.s), pres_db_url.len,
  248. pres_db_url.s);
  249. if(pres_db_init() < 0) {
  250. return -1;
  251. }
  252. return 0;
  253. }
  254. /* Child initialization function */
  255. static int child_init(int rank)
  256. {
  257. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  258. return 0; /* do nothing for the main process */
  259. return pres_db_open();
  260. }
  261. static void destroy(void)
  262. {
  263. /* Cleanup curl */
  264. curl_global_cleanup();
  265. /* Cleanup forward */
  266. conf_destroy();
  267. destroy_shmlock();
  268. /* Close pres db */
  269. pres_db_close();
  270. }
  271. /* Fixup functions */
  272. /*
  273. * Fix http_query params: url (string that may contain pvars) and
  274. * result (writable pvar).
  275. */
  276. static int fixup_http_query_get(void** param, int param_no)
  277. {
  278. if (param_no == 1) {
  279. return fixup_spve_null(param, 1);
  280. }
  281. if (param_no == 2) {
  282. if (fixup_pvar_null(param, 1) != 0) {
  283. LM_ERR("failed to fixup result pvar\n");
  284. return -1;
  285. }
  286. if (((pv_spec_t *)(*param))->setf == NULL) {
  287. LM_ERR("result pvar is not writeble\n");
  288. return -1;
  289. }
  290. return 0;
  291. }
  292. LM_ERR("invalid parameter number <%d>\n", param_no);
  293. return -1;
  294. }
  295. /*
  296. * Free http_query params.
  297. */
  298. static int fixup_free_http_query_get(void** param, int param_no)
  299. {
  300. if (param_no == 1) {
  301. LM_WARN("free function has not been defined for spve\n");
  302. return 0;
  303. }
  304. if (param_no == 2) {
  305. return fixup_free_pvar_null(param, 1);
  306. }
  307. LM_ERR("invalid parameter number <%d>\n", param_no);
  308. return -1;
  309. }
  310. /*
  311. * Fix http_query params: url (string that may contain pvars) and
  312. * result (writable pvar).
  313. */
  314. static int fixup_http_query_post(void** param, int param_no)
  315. {
  316. if ((param_no == 1) || (param_no == 2)) {
  317. return fixup_spve_null(param, 1);
  318. }
  319. if (param_no == 3) {
  320. if (fixup_pvar_null(param, 1) != 0) {
  321. LM_ERR("failed to fixup result pvar\n");
  322. return -1;
  323. }
  324. if (((pv_spec_t *)(*param))->setf == NULL) {
  325. LM_ERR("result pvar is not writeble\n");
  326. return -1;
  327. }
  328. return 0;
  329. }
  330. LM_ERR("invalid parameter number <%d>\n", param_no);
  331. return -1;
  332. }
  333. /*
  334. * Free http_query params.
  335. */
  336. static int fixup_free_http_query_post(void** param, int param_no)
  337. {
  338. if ((param_no == 1) || (param_no == 2)) {
  339. LM_WARN("free function has not been defined for spve\n");
  340. return 0;
  341. }
  342. if (param_no == 3) {
  343. return fixup_free_pvar_null(param, 1);
  344. }
  345. LM_ERR("invalid parameter number <%d>\n", param_no);
  346. return -1;
  347. }
  348. /*
  349. * Wrapper for HTTP-Query (GET)
  350. */
  351. static int w_http_query(struct sip_msg* _m, char* _url, char* _result) {
  352. return http_query(_m, _url, _result, NULL);
  353. }
  354. /*
  355. * Wrapper for HTTP-Query (POST-Variant)
  356. */
  357. static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result) {
  358. return http_query(_m, _url, _result, _post);
  359. }
  360. /*!
  361. * \brief checks precondition, switch, filter and forwards msg if necessary
  362. * \param msg the message to be forwarded
  363. * \param id use configuration with this ID when checking switch, filter, proxy.
  364. * \param proto protocol to be used. Should be PROTO_UDP.
  365. * \return 0 on success, -1 otherwise
  366. */
  367. int utils_forward(struct sip_msg *msg, int id, int proto)
  368. {
  369. int ret = -1;
  370. struct dest_info dst;
  371. init_dest_info(&dst);
  372. dst.proto = proto;
  373. // critical section start:
  374. // avoids dirty reads when updating configuration.
  375. lock_get(conf_lock);
  376. struct proxy_l *proxy = conf_needs_forward(msg, id);
  377. if (proxy != NULL) {
  378. proxy2su(&dst.to, proxy);
  379. if (forward_request(msg, NULL, 0, &dst) < 0){
  380. LM_ERR("could not forward message\n");
  381. }
  382. ret = 0;
  383. }
  384. // critical section end
  385. lock_release(conf_lock);
  386. return ret;
  387. }
  388. /* FIFO functions */
  389. /*!
  390. * \brief fifo command for listing configuration
  391. * \return pointer to the mi_root on success, 0 otherwise
  392. */
  393. static struct mi_root* forward_fifo_list(struct mi_root* cmd_tree, void *param)
  394. {
  395. struct mi_node *node = NULL;
  396. struct mi_root * ret = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  397. if(ret == NULL)
  398. return 0;
  399. node = addf_mi_node_child( &ret->node, 0, 0, 0, "Printing forwarding information:");
  400. if(node == NULL)
  401. goto error;
  402. // critical section start:
  403. // avoids dirty reads when updating configuration.
  404. lock_get(conf_lock);
  405. conf_show(ret);
  406. // critical section end
  407. lock_release(conf_lock);
  408. return ret;
  409. error:
  410. free_mi_tree(ret);
  411. return 0;
  412. }
  413. /*!
  414. * \brief fifo command for configuring switch
  415. * \return pointer to the mi_root on success, 0 otherwise
  416. */
  417. static struct mi_root* forward_fifo_switch(struct mi_root* cmd_tree, void* param)
  418. {
  419. struct mi_node *node = NULL;
  420. int result;
  421. node = cmd_tree->node.kids;
  422. if (node==NULL || node->next!=NULL || node->value.s==NULL)
  423. return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  424. // critical section start:
  425. // avoids dirty reads when updating configuration.
  426. lock_get(conf_lock);
  427. result = conf_parse_switch(node->value.s);
  428. // critical section end
  429. lock_release(conf_lock);
  430. if (result < 0) {
  431. LM_ERR("cannot parse parameter\n");
  432. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  433. }
  434. return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  435. }
  436. /*!
  437. * \brief fifo command for configuring filter
  438. * \return pointer to the mi_root on success, 0 otherwise
  439. */
  440. static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param)
  441. {
  442. struct mi_node *node = NULL;
  443. int result;
  444. node = cmd_tree->node.kids;
  445. if (node==NULL || node->next!=NULL || node->value.s==NULL)
  446. return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  447. // critical section start:
  448. // avoids dirty reads when updating configuration.
  449. lock_get(conf_lock);
  450. result = conf_parse_filter(node->value.s);
  451. // critical section end
  452. lock_release(conf_lock);
  453. if (result < 0) {
  454. LM_ERR("cannot parse parameter\n");
  455. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  456. }
  457. return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  458. }
  459. /*!
  460. * \brief fifo command for configuring proxy
  461. * \return pointer to the mi_root on success, 0 otherwise
  462. */
  463. static struct mi_root* forward_fifo_proxy(struct mi_root* cmd_tree, void* param)
  464. {
  465. struct mi_node *node = NULL;
  466. int result;
  467. node = cmd_tree->node.kids;
  468. if (node==NULL || node->next!=NULL || node->value.s==NULL)
  469. return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
  470. // critical section start:
  471. // avoids dirty reads when updating configuration.
  472. lock_get(conf_lock);
  473. result = conf_parse_proxy(node->value.s);
  474. // critical section end
  475. lock_release(conf_lock);
  476. if (result < 0) {
  477. LM_ERR("cannot parse parameter\n");
  478. return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
  479. }
  480. return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  481. }