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 40 41 42 43 44 45 46 47 48 49 50 51 52 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
/*
* utils Module
*
* Copyright (C) 2008 Juha Heinanen
* Copyright (C) 2009 1&1 Internet AG
* Copyright (C) 2013 Carsten Bock, ng-voice GmbH
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* -------
* 2008-11-24: Introduced utils module and its first function: http_query.
*
*/
/*! \file
* \brief SIP-router utils :: Module core
* \ingroup utils
*/
/*! \defgroup Utils SIP-router :: Various utilities
*
*/
#include <curl/curl.h>
#include "../../mod_fix.h"
#include "../../sr_module.h"
#include "../../ut.h"
#include "../../forward.h"
#include "../../resolve.h"
#include "../../locking.h"
#include "../../script_cb.h"
#include "../../mem/shm_mem.h"
#include "../../lib/srdb1/db.h"
#include "functions.h"
#include "conf.h"
#include "xcap_auth.h"
MODULE_VERSION
#define XCAP_TABLE_VERSION 4
/* Module parameter variables */
int http_query_timeout = 4;
static int forward_active = 0;
static int mp_max_id = 0;
static char* mp_switch = "";
static char* mp_filter = "";
static char* mp_proxy = "";
str xcap_table= str_init("xcap");
str pres_db_url = {0, 0};
/* lock for configuration access */
static gen_lock_t *conf_lock = NULL;
/* FIFO interface functions */
static struct mi_root* forward_fifo_list(struct mi_root* cmd_tree, void *param);
static struct mi_root* forward_fifo_switch(struct mi_root* cmd_tree, void* param);
static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param);
static struct mi_root* forward_fifo_proxy(struct mi_root* cmd_tree, void* param);
/* Database connection */
db1_con_t *pres_dbh = NULL;
db_func_t pres_dbf;
/* Module management function prototypes */
static int mod_init(void);
static int child_init(int);
static void destroy(void);
/* Fixup functions to be defined later */
static int fixup_http_query_get(void** param, int param_no);
static int fixup_free_http_query_get(void** param, int param_no);
static int fixup_http_query_post(void** param, int param_no);
static int fixup_free_http_query_post(void** param, int param_no);
/* Wrappers for http_query to be defined later */
static int w_http_query(struct sip_msg* _m, char* _url, char* _result);
static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result);
/* forward function */
int utils_forward(struct sip_msg *msg, int id, int proto);
/* Exported functions */
static cmd_export_t cmds[] = {
{"http_query", (cmd_function)w_http_query, 2, fixup_http_query_get,
fixup_free_http_query_get,
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
{"http_query", (cmd_function)w_http_query_post, 3, fixup_http_query_post,
fixup_free_http_query_post,
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
{"xcap_auth_status", (cmd_function)xcap_auth_status, 2, fixup_pvar_pvar,
fixup_free_pvar_pvar, REQUEST_ROUTE},
{0, 0, 0, 0, 0, 0}
};
/* Exported parameters */
static param_export_t params[] = {
{"pres_db_url", PARAM_STR, &pres_db_url},
{"xcap_table", PARAM_STR, &xcap_table},
{"http_query_timeout", INT_PARAM, &http_query_timeout},
{"forward_active", INT_PARAM, &forward_active},
{0, 0, 0}
};
static mi_export_t mi_cmds[] = {
{ "forward_list", forward_fifo_list, MI_NO_INPUT_FLAG, 0, 0 },
{ "forward_switch", forward_fifo_switch, 0, 0, 0 },
{ "forward_filter", forward_fifo_filter, 0, 0, 0 },
{ "forward_proxy", forward_fifo_proxy, 0, 0, 0 },
{ 0, 0, 0, 0, 0}
};
/* Module interface */
struct module_exports exports = {
"utils",
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* Exported functions */
params, /* Exported parameters */
0, /* exported statistics */
mi_cmds, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* extra processes */
mod_init, /* module initialization function */
0, /* response function*/
destroy, /* destroy function */
child_init /* per-child init function */
};
static int init_shmlock(void)
{
conf_lock = lock_alloc();
if (conf_lock == NULL) {
LM_CRIT("cannot allocate memory for lock.\n");
return -1;
}
if (lock_init(conf_lock) == 0) {
LM_CRIT("cannot initialize lock.\n");
return -1;
}
return 0;
}
static int pre_script_filter(struct sip_msg *msg, unsigned int flags, void *unused)
{
/* use id 0 for pre script callback */
utils_forward(msg, 0, PROTO_UDP);
/* always return 1 so that routing skript is called for msg */
return 1;
}
static void destroy_shmlock(void)
{
if (conf_lock) {
lock_destroy(conf_lock);
lock_dealloc((void *)conf_lock);
conf_lock = NULL;
}
}
static void pres_db_close(void) {
if (pres_dbh) {
pres_dbf.close(pres_dbh);
pres_dbh = NULL;
}
}
static int pres_db_init(void) {
if (!pres_db_url.s || !pres_db_url.len) {
LM_INFO("xcap_auth_status function is disabled\n");
return 0;
}
if (db_bind_mod(&pres_db_url, &pres_dbf) < 0) {
LM_ERR("can't bind database module\n");
return -1;
}
if ((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) {
LM_ERR("can't connect to database\n");
return -1;
}
if (db_check_table_version(&pres_dbf, pres_dbh, &xcap_table,
XCAP_TABLE_VERSION) < 0) {
LM_ERR("during table version check\n");
pres_db_close();
return -1;
}
pres_db_close();
return 0;
}
static int pres_db_open(void) {
if (!pres_db_url.s || !pres_db_url.len) {
return 0;
}
if (pres_dbh) {
pres_dbf.close(pres_dbh);
}
if ((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) {
LM_ERR("can't connect to database\n");
return -1;
}
if (pres_dbf.use_table(pres_dbh, &xcap_table) < 0) {
LM_ERR("in use_table: %.*s\n", xcap_table.len, xcap_table.s);
return -1;
}
return 0;
}
/* Module initialization function */
static int mod_init(void)
{
if(register_mi_mod(exports.name, mi_cmds)!=0)
{
LM_ERR("failed to register MI commands\n");
return -1;
}
/* Initialize curl */
if (curl_global_init(CURL_GLOBAL_ALL)) {
LM_ERR("curl_global_init failed\n");
return -1;
}
if (init_shmlock() != 0) {
LM_CRIT("cannot initialize shmlock.\n");
return -1;
}
if (conf_init(mp_max_id) < 0) {
LM_CRIT("cannot initialize configuration.\n");
return -1;
}
/* read module parameters and update configuration structure */
if (conf_parse_proxy(mp_proxy) < 0) {
LM_CRIT("cannot parse proxy module parameter.\n");
return -1;
}
if (conf_parse_filter(mp_filter) < 0) {
LM_CRIT("cannot parse filter module parameter.\n");
return -1;
}
if (conf_parse_switch(mp_switch) < 0) {
LM_CRIT("cannot parse switch module parameter.\n");
return -1;
}
if (forward_active == 1) {
/* register callback for id 0 */
if (register_script_cb(pre_script_filter, PRE_SCRIPT_CB|ONREPLY_CB, 0) < 0) {
LM_CRIT("cannot register script callback for requests.\n");
return -1;
}
if (register_script_cb(pre_script_filter, PRE_SCRIPT_CB|ONREPLY_CB, 0) < 0) {
LM_CRIT("cannot register script callback for replies.\n");
return -1;
}
} else {
LM_INFO("forward functionality disabled");
}
/* presence database */
LM_DBG("pres_db_url=%s/%d/%p\n", ZSW(pres_db_url.s), pres_db_url.len,
pres_db_url.s);
if(pres_db_init() < 0) {
return -1;
}
return 0;
}
/* Child initialization function */
static int child_init(int rank)
{
if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
return 0; /* do nothing for the main process */
return pres_db_open();
}
static void destroy(void)
{
/* Cleanup curl */
curl_global_cleanup();
/* Cleanup forward */
conf_destroy();
destroy_shmlock();
/* Close pres db */
pres_db_close();
}
/* Fixup functions */
/*
* Fix http_query params: url (string that may contain pvars) and
* result (writable pvar).
*/
static int fixup_http_query_get(void** param, int param_no)
{
if (param_no == 1) {
return fixup_spve_null(param, 1);
}
if (param_no == 2) {
if (fixup_pvar_null(param, 1) != 0) {
LM_ERR("failed to fixup result pvar\n");
return -1;
}
if (((pv_spec_t *)(*param))->setf == NULL) {
LM_ERR("result pvar is not writeble\n");
return -1;
}
return 0;
}
LM_ERR("invalid parameter number <%d>\n", param_no);
return -1;
}
/*
* Free http_query params.
*/
static int fixup_free_http_query_get(void** param, int param_no)
{
if (param_no == 1) {
LM_WARN("free function has not been defined for spve\n");
return 0;
}
if (param_no == 2) {
return fixup_free_pvar_null(param, 1);
}
LM_ERR("invalid parameter number <%d>\n", param_no);
return -1;
}
/*
* Fix http_query params: url (string that may contain pvars) and
* result (writable pvar).
*/
static int fixup_http_query_post(void** param, int param_no)
{
if ((param_no == 1) || (param_no == 2)) {
return fixup_spve_null(param, 1);
}
if (param_no == 3) {
if (fixup_pvar_null(param, 1) != 0) {
LM_ERR("failed to fixup result pvar\n");
return -1;
}
if (((pv_spec_t *)(*param))->setf == NULL) {
LM_ERR("result pvar is not writeble\n");
return -1;
}
return 0;
}
LM_ERR("invalid parameter number <%d>\n", param_no);
return -1;
}
/*
* Free http_query params.
*/
static int fixup_free_http_query_post(void** param, int param_no)
{
if ((param_no == 1) || (param_no == 2)) {
LM_WARN("free function has not been defined for spve\n");
return 0;
}
if (param_no == 3) {
return fixup_free_pvar_null(param, 1);
}
LM_ERR("invalid parameter number <%d>\n", param_no);
return -1;
}
/*
* Wrapper for HTTP-Query (GET)
*/
static int w_http_query(struct sip_msg* _m, char* _url, char* _result) {
return http_query(_m, _url, _result, NULL);
}
/*
* Wrapper for HTTP-Query (POST-Variant)
*/
static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result) {
return http_query(_m, _url, _result, _post);
}
/*!
* \brief checks precondition, switch, filter and forwards msg if necessary
* \param msg the message to be forwarded
* \param id use configuration with this ID when checking switch, filter, proxy.
* \param proto protocol to be used. Should be PROTO_UDP.
* \return 0 on success, -1 otherwise
*/
int utils_forward(struct sip_msg *msg, int id, int proto)
{
int ret = -1;
struct dest_info dst;
init_dest_info(&dst);
dst.proto = proto;
// critical section start:
// avoids dirty reads when updating configuration.
lock_get(conf_lock);
struct proxy_l *proxy = conf_needs_forward(msg, id);
if (proxy != NULL) {
proxy2su(&dst.to, proxy);
if (forward_request(msg, NULL, 0, &dst) < 0){
LM_ERR("could not forward message\n");
}
ret = 0;
}
// critical section end
lock_release(conf_lock);
return ret;
}
/* FIFO functions */
/*!
* \brief fifo command for listing configuration
* \return pointer to the mi_root on success, 0 otherwise
*/
static struct mi_root* forward_fifo_list(struct mi_root* cmd_tree, void *param)
{
struct mi_node *node = NULL;
struct mi_root * ret = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
if(ret == NULL)
return 0;
node = addf_mi_node_child( &ret->node, 0, 0, 0, "Printing forwarding information:");
if(node == NULL)
goto error;
// critical section start:
// avoids dirty reads when updating configuration.
lock_get(conf_lock);
conf_show(ret);
// critical section end
lock_release(conf_lock);
return ret;
error:
free_mi_tree(ret);
return 0;
}
/*!
* \brief fifo command for configuring switch
* \return pointer to the mi_root on success, 0 otherwise
*/
static struct mi_root* forward_fifo_switch(struct mi_root* cmd_tree, void* param)
{
struct mi_node *node = NULL;
int result;
node = cmd_tree->node.kids;
if (node==NULL || node->next!=NULL || node->value.s==NULL)
return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
// critical section start:
// avoids dirty reads when updating configuration.
lock_get(conf_lock);
result = conf_parse_switch(node->value.s);
// critical section end
lock_release(conf_lock);
if (result < 0) {
LM_ERR("cannot parse parameter\n");
return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
}
return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
}
/*!
* \brief fifo command for configuring filter
* \return pointer to the mi_root on success, 0 otherwise
*/
static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param)
{
struct mi_node *node = NULL;
int result;
node = cmd_tree->node.kids;
if (node==NULL || node->next!=NULL || node->value.s==NULL)
return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
// critical section start:
// avoids dirty reads when updating configuration.
lock_get(conf_lock);
result = conf_parse_filter(node->value.s);
// critical section end
lock_release(conf_lock);
if (result < 0) {
LM_ERR("cannot parse parameter\n");
return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
}
return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
}
/*!
* \brief fifo command for configuring proxy
* \return pointer to the mi_root on success, 0 otherwise
*/
static struct mi_root* forward_fifo_proxy(struct mi_root* cmd_tree, void* param)
{
struct mi_node *node = NULL;
int result;
node = cmd_tree->node.kids;
if (node==NULL || node->next!=NULL || node->value.s==NULL)
return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
// critical section start:
// avoids dirty reads when updating configuration.
lock_get(conf_lock);
result = conf_parse_proxy(node->value.s);
// critical section end
lock_release(conf_lock);
if (result < 0) {
LM_ERR("cannot parse parameter\n");
return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
}
return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
}
|