| 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 |
#include "asterisk.h" |
| 26 |
|
| 27 |
#include <pjsip.h> |
| 28 |
|
| 29 |
#include "asterisk/res_pjsip.h" |
| 30 |
#include "asterisk/res_pjsip_cli.h" |
| 31 |
#include "asterisk/module.h" |
| 32 |
#include "asterisk/acl.h" |
| 33 |
#include "asterisk/manager.h" |
| 34 |
#include "res_pjsip/include/res_pjsip_private.h" |
| 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 |
#define HOSTS_BUCKETS 53 |
| 163 |
|
| 164 |
|
| 165 |
struct ip_identify_match { |
| 166 |
|
| 167 |
SORCERY_OBJECT(details); |
| 168 |
|
| 169 |
AST_DECLARE_STRING_FIELDS( |
| 170 |
|
| 171 |
AST_STRING_FIELD(endpoint_name); |
| 172 |
|
| 173 |
AST_STRING_FIELD(match_request_uri); |
| 174 |
|
| 175 |
AST_STRING_FIELD(match_header); |
| 176 |
|
| 177 |
AST_STRING_FIELD(match_header_name); |
| 178 |
|
| 179 |
AST_STRING_FIELD(match_header_value); |
| 180 |
); |
| 181 |
|
| 182 |
regex_t regex_header_buf; |
| 183 |
|
| 184 |
regex_t regex_request_uri_buf; |
| 185 |
|
| 186 |
struct ast_ha *matches; |
| 187 |
|
| 188 |
struct ao2_container *hosts; |
| 189 |
|
| 190 |
unsigned int srv_lookups; |
| 191 |
|
| 192 |
unsigned int is_header_regex:1; |
| 193 |
|
| 194 |
unsigned int is_request_uri_regex:1; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
static void ip_identify_destroy(void *obj) |
| 199 |
{ |
| 200 |
struct ip_identify_match *identify = obj; |
| 201 |
|
| 202 |
ast_string_field_free_memory(identify); |
| 203 |
ast_free_ha(identify->matches); |
| 204 |
ao2_cleanup(identify->hosts); |
| 205 |
if (identify->is_header_regex) { |
| 206 |
regfree(&identify->regex_header_buf); |
| 207 |
} |
| 208 |
if (identify->is_request_uri_regex) { |
| 209 |
regfree(&identify->regex_request_uri_buf); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static void *ip_identify_alloc(const char *name) |
| 215 |
{ |
| 216 |
struct ip_identify_match *identify = ast_sorcery_generic_alloc(sizeof(*identify), ip_identify_destroy); |
| 217 |
|
| 218 |
if (!identify || ast_string_field_init(identify, 256)) { |
| 219 |
ao2_cleanup(identify); |
| 220 |
return NULL; |
| 221 |
} |
| 222 |
|
| 223 |
return identify; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
static int header_identify_match_check(void *obj, void *arg, int flags) |
| 228 |
{ |
| 229 |
struct ip_identify_match *identify = obj; |
| 230 |
struct pjsip_rx_data *rdata = arg; |
| 231 |
pjsip_hdr *header; |
| 232 |
pj_str_t pj_header_name; |
| 233 |
int header_present; |
| 234 |
|
| 235 |
if (ast_strlen_zero(identify->match_header)) { |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
pj_header_name = pj_str((void *) identify->match_header_name); |
| 240 |
|
| 241 |
|
| 242 |
header_present = 0; |
| 243 |
for (header = NULL; |
| 244 |
(header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &pj_header_name, header)); |
| 245 |
header = header->next) { |
| 246 |
char *pos; |
| 247 |
int len; |
| 248 |
char buf[PATH_MAX]; |
| 249 |
|
| 250 |
header_present = 1; |
| 251 |
|
| 252 |
|
| 253 |
len = pjsip_hdr_print_on(header, buf, sizeof(buf) - 1); |
| 254 |
if (len < 0) { |
| 255 |
|
| 256 |
ast_assert(0); |
| 257 |
continue; |
| 258 |
} |
| 259 |
buf[len] = '\0'; |
| 260 |
|
| 261 |
|
| 262 |
pos = strchr(buf, ':'); |
| 263 |
if (!pos) { |
| 264 |
|
| 265 |
ast_assert(0); |
| 266 |
continue; |
| 267 |
} |
| 268 |
pos = ast_strip(pos + 1); |
| 269 |
|
| 270 |
|
| 271 |
if (identify->is_header_regex) { |
| 272 |
if (!regexec(&identify->regex_header_buf, pos, 0, NULL, 0)) { |
| 273 |
return CMP_MATCH; |
| 274 |
} |
| 275 |
} else if (!strcmp(identify->match_header_value, pos)) { |
| 276 |
return CMP_MATCH; |
| 277 |
} |
| 278 |
|
| 279 |
ast_debug(3, "Identify '%s': SIP message has '%s' header but value '%s' does not match '%s'.\n", |
| 280 |
ast_sorcery_object_get_id(identify), |
| 281 |
identify->match_header_name, |
| 282 |
pos, |
| 283 |
identify->match_header_value); |
| 284 |
} |
| 285 |
if (!header_present) { |
| 286 |
ast_debug(3, "Identify '%s': SIP message does not have '%s' header.\n", |
| 287 |
ast_sorcery_object_get_id(identify), |
| 288 |
identify->match_header_name); |
| 289 |
} |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
static int request_identify_match_check(void *obj, void *arg, int flags) |
| 295 |
{ |
| 296 |
struct ip_identify_match *identify = obj; |
| 297 |
struct pjsip_rx_data *rdata = arg; |
| 298 |
int len; |
| 299 |
char buf[PJSIP_MAX_URL_SIZE]; |
| 300 |
|
| 301 |
if (ast_strlen_zero(identify->match_request_uri)) { |
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, rdata->msg_info.msg->line.req.uri, buf, sizeof(buf) - 1); |
| 307 |
if (len < 0) { |
| 308 |
|
| 309 |
ast_assert(0); |
| 310 |
} else { |
| 311 |
|
| 312 |
buf[len] = '\0'; |
| 313 |
|
| 314 |
if (identify->is_request_uri_regex) { |
| 315 |
if (!regexec(&identify->regex_request_uri_buf, buf, 0, NULL, 0)) { |
| 316 |
return CMP_MATCH; |
| 317 |
} |
| 318 |
} else if (!strcmp(identify->match_request_uri, buf)) { |
| 319 |
return CMP_MATCH; |
| 320 |
} |
| 321 |
ast_debug(3, "Identify '%s': request URI not match '%s' (value='%s').\n", |
| 322 |
ast_sorcery_object_get_id(identify), identify->match_request_uri, buf); |
| 323 |
} |
| 324 |
|
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
static int ip_identify_match_check(void *obj, void *arg, int flags) |
| 330 |
{ |
| 331 |
struct ip_identify_match *identify = obj; |
| 332 |
struct ast_sockaddr *addr = arg; |
| 333 |
int sense; |
| 334 |
|
| 335 |
sense = ast_apply_ha(identify->matches, addr); |
| 336 |
if (sense != AST_SENSE_ALLOW) { |
| 337 |
ast_debug(3, "Source address %s matches identify '%s'\n", |
| 338 |
ast_sockaddr_stringify(addr), |
| 339 |
ast_sorcery_object_get_id(identify)); |
| 340 |
return CMP_MATCH; |
| 341 |
} else { |
| 342 |
ast_debug(3, "Source address %s does not match identify '%s'\n", |
| 343 |
ast_sockaddr_stringify(addr), |
| 344 |
ast_sorcery_object_get_id(identify)); |
| 345 |
return 0; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
static struct ast_sip_endpoint *common_identify(ao2_callback_fn *identify_match_cb, void *arg) |
| 350 |
{ |
| 351 |
RAII_VAR(struct ao2_container *, candidates, NULL, ao2_cleanup); |
| 352 |
struct ip_identify_match *match; |
| 353 |
struct ast_sip_endpoint *endpoint; |
| 354 |
|
| 355 |
|
| 356 |
candidates = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", |
| 357 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 358 |
if (!candidates || !ao2_container_count(candidates)) { |
| 359 |
ast_debug(3, "No identify sections to match against\n"); |
| 360 |
return NULL; |
| 361 |
} |
| 362 |
|
| 363 |
match = ao2_callback(candidates, 0, identify_match_cb, arg); |
| 364 |
if (!match) { |
| 365 |
return NULL; |
| 366 |
} |
| 367 |
|
| 368 |
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", |
| 369 |
match->endpoint_name); |
| 370 |
if (endpoint) { |
| 371 |
ast_debug(3, "Identify '%s' SIP message matched to endpoint %s\n", |
| 372 |
ast_sorcery_object_get_id(match), match->endpoint_name); |
| 373 |
} else { |
| 374 |
ast_log(LOG_WARNING, "Identify '%s' points to endpoint '%s' but endpoint could not be found\n", |
| 375 |
ast_sorcery_object_get_id(match), match->endpoint_name); |
| 376 |
} |
| 377 |
|
| 378 |
ao2_ref(match, -1); |
| 379 |
return endpoint; |
| 380 |
} |
| 381 |
|
| 382 |
static struct ast_sip_endpoint *ip_identify(pjsip_rx_data *rdata) |
| 383 |
{ |
| 384 |
struct ast_sockaddr addr = { { 0, } }; |
| 385 |
|
| 386 |
ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID); |
| 387 |
ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port); |
| 388 |
|
| 389 |
return common_identify(ip_identify_match_check, &addr); |
| 390 |
} |
| 391 |
|
| 392 |
static struct ast_sip_endpoint_identifier ip_identifier = { |
| 393 |
.identify_endpoint = ip_identify, |
| 394 |
}; |
| 395 |
|
| 396 |
static struct ast_sip_endpoint *header_identify(pjsip_rx_data *rdata) |
| 397 |
{ |
| 398 |
return common_identify(header_identify_match_check, rdata); |
| 399 |
} |
| 400 |
|
| 401 |
static struct ast_sip_endpoint *request_identify(pjsip_rx_data *rdata) |
| 402 |
{ |
| 403 |
return common_identify(request_identify_match_check, rdata); |
| 404 |
} |
| 405 |
|
| 406 |
static struct ast_sip_endpoint_identifier header_identifier = { |
| 407 |
.identify_endpoint = header_identify, |
| 408 |
}; |
| 409 |
|
| 410 |
static struct ast_sip_endpoint_identifier request_identifier = { |
| 411 |
.identify_endpoint = request_identify, |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
static int ip_identify_match_host_lookup(struct ip_identify_match *identify, const char *host) |
| 416 |
{ |
| 417 |
struct ast_sockaddr *addrs; |
| 418 |
int num_addrs = 0, error = 0, i; |
| 419 |
int results = 0; |
| 420 |
|
| 421 |
num_addrs = ast_sockaddr_resolve(&addrs, host, 0, AST_AF_UNSPEC); |
| 422 |
if (!num_addrs) { |
| 423 |
return -1; |
| 424 |
} |
| 425 |
|
| 426 |
for (i = 0; i < num_addrs; ++i) { |
| 427 |
|
| 428 |
if (identify->matches && (ast_apply_ha(identify->matches, &addrs[i]) != AST_SENSE_ALLOW)) { |
| 429 |
continue; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
identify->matches = ast_append_ha_with_port("d", ast_sockaddr_stringify(&addrs[i]), identify->matches, &error); |
| 434 |
|
| 435 |
if (!identify->matches || error) { |
| 436 |
results = -1; |
| 437 |
break; |
| 438 |
} |
| 439 |
|
| 440 |
results += 1; |
| 441 |
} |
| 442 |
|
| 443 |
ast_free(addrs); |
| 444 |
|
| 445 |
return results; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
static int ip_identify_match_srv_lookup(struct ip_identify_match *identify, const char *prefix, const char *host, int results) |
| 450 |
{ |
| 451 |
char service[NI_MAXHOST]; |
| 452 |
struct srv_context *context = NULL; |
| 453 |
int srv_ret; |
| 454 |
const char *srvhost; |
| 455 |
unsigned short srvport; |
| 456 |
|
| 457 |
snprintf(service, sizeof(service), "%s.%s", prefix, host); |
| 458 |
|
| 459 |
while (!(srv_ret = ast_srv_lookup(&context, service, &srvhost, &srvport))) { |
| 460 |
int hosts; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
hosts = ip_identify_match_host_lookup(identify, srvhost); |
| 466 |
if (hosts == -1) { |
| 467 |
results = -1; |
| 468 |
break; |
| 469 |
} else { |
| 470 |
results += hosts; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
ast_srv_cleanup(&context); |
| 475 |
|
| 476 |
return results; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
static int ip_identify_match_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 481 |
{ |
| 482 |
struct ip_identify_match *identify = obj; |
| 483 |
char *input_string = ast_strdupa(var->value); |
| 484 |
char *current_string; |
| 485 |
|
| 486 |
if (ast_strlen_zero(var->value)) { |
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
while ((current_string = ast_strip(strsep(&input_string, ",")))) { |
| 491 |
char *mask; |
| 492 |
struct ast_sockaddr address; |
| 493 |
int error = 0; |
| 494 |
|
| 495 |
if (ast_strlen_zero(current_string)) { |
| 496 |
continue; |
| 497 |
} |
| 498 |
|
| 499 |
mask = strrchr(current_string, '/'); |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
if (mask || ast_sockaddr_parse(&address, current_string, 0)) { |
| 504 |
identify->matches = ast_append_ha_with_port("d", current_string, identify->matches, &error); |
| 505 |
|
| 506 |
if (!identify->matches || error) { |
| 507 |
ast_log(LOG_ERROR, "Failed to add address '%s' to ip endpoint identifier '%s'\n", |
| 508 |
current_string, ast_sorcery_object_get_id(obj)); |
| 509 |
return -1; |
| 510 |
} |
| 511 |
|
| 512 |
continue; |
| 513 |
} |
| 514 |
|
| 515 |
if (!identify->hosts) { |
| 516 |
identify->hosts = ast_str_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, HOSTS_BUCKETS); |
| 517 |
if (!identify->hosts) { |
| 518 |
ast_log(LOG_ERROR, "Failed to create container to store hosts on ip endpoint identifier '%s'\n", |
| 519 |
ast_sorcery_object_get_id(obj)); |
| 520 |
return -1; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
error = ast_str_container_add(identify->hosts, current_string); |
| 525 |
if (error) { |
| 526 |
ast_log(LOG_ERROR, "Failed to store host '%s' for resolution on ip endpoint identifier '%s'\n", |
| 527 |
current_string, ast_sorcery_object_get_id(obj)); |
| 528 |
return -1; |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
return 0; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
static int ip_identify_apply(const struct ast_sorcery *sorcery, void *obj) |
| 537 |
{ |
| 538 |
struct ip_identify_match *identify = obj; |
| 539 |
char *current_string; |
| 540 |
struct ao2_iterator i; |
| 541 |
|
| 542 |
|
| 543 |
if (ast_strlen_zero(identify->endpoint_name)) { |
| 544 |
ast_log(LOG_ERROR, "Identify '%s' missing required endpoint name.\n", |
| 545 |
ast_sorcery_object_get_id(identify)); |
| 546 |
return -1; |
| 547 |
} |
| 548 |
if (ast_strlen_zero(identify->match_header) |
| 549 |
&& ast_strlen_zero(identify->match_request_uri) |
| 550 |
|
| 551 |
&& !identify->matches |
| 552 |
|
| 553 |
&& (!identify->hosts || !ao2_container_count(identify->hosts))) { |
| 554 |
ast_log(LOG_ERROR, "Identify '%s' is not configured to match anything.\n", |
| 555 |
ast_sorcery_object_get_id(identify)); |
| 556 |
return -1; |
| 557 |
} |
| 558 |
|
| 559 |
if (!ast_strlen_zero(identify->match_header)) { |
| 560 |
char *c_header; |
| 561 |
char *c_value; |
| 562 |
int len; |
| 563 |
|
| 564 |
|
| 565 |
c_header = ast_strdupa(identify->match_header); |
| 566 |
c_value = strchr(c_header, ':'); |
| 567 |
if (!c_value) { |
| 568 |
ast_log(LOG_ERROR, "Identify '%s' missing ':' separator in match_header '%s'.\n", |
| 569 |
ast_sorcery_object_get_id(identify), identify->match_header); |
| 570 |
return -1; |
| 571 |
} |
| 572 |
*c_value = '\0'; |
| 573 |
c_value = ast_strip(c_value + 1); |
| 574 |
c_header = ast_strip(c_header); |
| 575 |
|
| 576 |
if (ast_strlen_zero(c_header)) { |
| 577 |
ast_log(LOG_ERROR, "Identify '%s' has no SIP header to match in match_header '%s'.\n", |
| 578 |
ast_sorcery_object_get_id(identify), identify->match_header); |
| 579 |
return -1; |
| 580 |
} |
| 581 |
|
| 582 |
if (!strcmp(c_value, "//")) { |
| 583 |
|
| 584 |
c_value = ""; |
| 585 |
} |
| 586 |
|
| 587 |
if (ast_string_field_set(identify, match_header_name, c_header) |
| 588 |
|| ast_string_field_set(identify, match_header_value, c_value)) { |
| 589 |
return -1; |
| 590 |
} |
| 591 |
|
| 592 |
len = strlen(c_value); |
| 593 |
if (2 < len && c_value[0] == '/' && c_value[len - 1] == '/') { |
| 594 |
|
| 595 |
c_value[len - 1] = '\0'; |
| 596 |
++c_value; |
| 597 |
|
| 598 |
if (regcomp(&identify->regex_header_buf, c_value, REG_EXTENDED | REG_NOSUB)) { |
| 599 |
ast_log(LOG_ERROR, "Identify '%s' failed to compile match_request_uri regex '%s'.\n", |
| 600 |
ast_sorcery_object_get_id(identify), c_value); |
| 601 |
return -1; |
| 602 |
} |
| 603 |
identify->is_header_regex = 1; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
if (!ast_strlen_zero(identify->match_request_uri)) { |
| 608 |
char *c_string; |
| 609 |
int len; |
| 610 |
|
| 611 |
len = strlen(identify->match_request_uri); |
| 612 |
c_string = ast_strdupa(identify->match_request_uri); |
| 613 |
|
| 614 |
if (!strcmp(c_string, "//")) { |
| 615 |
|
| 616 |
c_string = ""; |
| 617 |
} |
| 618 |
|
| 619 |
if (2 < len && c_string[0] == '/' && c_string[len - 1] == '/') { |
| 620 |
|
| 621 |
c_string[len - 1] = '\0'; |
| 622 |
++c_string; |
| 623 |
|
| 624 |
if (regcomp(&identify->regex_request_uri_buf, c_string, REG_EXTENDED | REG_NOSUB)) { |
| 625 |
ast_log(LOG_ERROR, "Identify '%s' failed to compile match_header regex '%s'.\n", |
| 626 |
ast_sorcery_object_get_id(identify), c_string); |
| 627 |
return -1; |
| 628 |
} |
| 629 |
identify->is_request_uri_regex = 1; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
if (!identify->hosts) { |
| 634 |
|
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
ast_sorcery_object_set_has_dynamic_contents(obj); |
| 640 |
|
| 641 |
|
| 642 |
i = ao2_iterator_init(identify->hosts, 0); |
| 643 |
while ((current_string = ao2_iterator_next(&i))) { |
| 644 |
int results = 0; |
| 645 |
char *colon = strrchr(current_string, ':'); |
| 646 |
|
| 647 |
|
| 648 |
if (!colon) { |
| 649 |
|
| 650 |
if (identify->srv_lookups) { |
| 651 |
results = ip_identify_match_srv_lookup(identify, "_sip._udp", current_string, |
| 652 |
results); |
| 653 |
if (results != -1) { |
| 654 |
results = ip_identify_match_srv_lookup(identify, "_sip._tcp", |
| 655 |
current_string, results); |
| 656 |
} |
| 657 |
if (results != -1) { |
| 658 |
results = ip_identify_match_srv_lookup(identify, "_sips._tcp", |
| 659 |
current_string, results); |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
if (!results) { |
| 666 |
results = ip_identify_match_host_lookup(identify, current_string); |
| 667 |
} |
| 668 |
|
| 669 |
if (results == 0) { |
| 670 |
ast_log(LOG_WARNING, "Identify '%s' provided address '%s' did not resolve to any address\n", |
| 671 |
ast_sorcery_object_get_id(identify), current_string); |
| 672 |
} else if (results == -1) { |
| 673 |
ast_log(LOG_ERROR, "Identify '%s' failed when adding resolution results of '%s'\n", |
| 674 |
ast_sorcery_object_get_id(identify), current_string); |
| 675 |
ao2_ref(current_string, -1); |
| 676 |
ao2_iterator_destroy(&i); |
| 677 |
return -1; |
| 678 |
} |
| 679 |
|
| 680 |
ao2_ref(current_string, -1); |
| 681 |
} |
| 682 |
ao2_iterator_destroy(&i); |
| 683 |
|
| 684 |
ao2_ref(identify->hosts, -1); |
| 685 |
identify->hosts = NULL; |
| 686 |
|
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
static int match_to_str(const void *obj, const intptr_t *args, char **buf) |
| 691 |
{ |
| 692 |
RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free); |
| 693 |
const struct ip_identify_match *identify = obj; |
| 694 |
|
| 695 |
ast_ha_join(identify->matches, &str); |
| 696 |
*buf = ast_strdup(ast_str_buffer(str)); |
| 697 |
return 0; |
| 698 |
} |
| 699 |
|
| 700 |
static void match_to_var_list_append(struct ast_variable **head, struct ast_ha *ha) |
| 701 |
{ |
| 702 |
char str[MAX_OBJECT_FIELD]; |
| 703 |
const char *addr; |
| 704 |
|
| 705 |
if (ast_sockaddr_port(&ha->addr)) { |
| 706 |
addr = ast_strdupa(ast_sockaddr_stringify(&ha->addr)); |
| 707 |
} else { |
| 708 |
addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr)); |
| 709 |
} |
| 710 |
|
| 711 |
snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "", |
| 712 |
addr, ast_sockaddr_stringify_addr(&ha->netmask)); |
| 713 |
|
| 714 |
ast_variable_list_append(head, ast_variable_new("match", str, "")); |
| 715 |
} |
| 716 |
|
| 717 |
static int match_to_var_list(const void *obj, struct ast_variable **fields) |
| 718 |
{ |
| 719 |
const struct ip_identify_match *identify = obj; |
| 720 |
struct ast_variable *head = NULL; |
| 721 |
struct ast_ha *ha = identify->matches; |
| 722 |
|
| 723 |
for (; ha; ha = ha->next) { |
| 724 |
match_to_var_list_append(&head, ha); |
| 725 |
} |
| 726 |
|
| 727 |
if (head) { |
| 728 |
*fields = head; |
| 729 |
} |
| 730 |
|
| 731 |
return 0; |
| 732 |
} |
| 733 |
|
| 734 |
static int sip_identify_to_ami(const struct ip_identify_match *identify, |
| 735 |
struct ast_str **buf) |
| 736 |
{ |
| 737 |
return ast_sip_sorcery_object_to_ami(identify, buf); |
| 738 |
} |
| 739 |
|
| 740 |
static int send_identify_ami_event(void *obj, void *arg, void *data, int flags) |
| 741 |
{ |
| 742 |
struct ip_identify_match *identify = obj; |
| 743 |
const char *endpoint_name = arg; |
| 744 |
struct ast_sip_ami *ami = data; |
| 745 |
struct ast_str *buf; |
| 746 |
|
| 747 |
|
| 748 |
buf = ast_sip_create_ami_event("IdentifyDetail", ami); |
| 749 |
if (!buf) { |
| 750 |
return CMP_STOP; |
| 751 |
} |
| 752 |
if (sip_identify_to_ami(identify, &buf)) { |
| 753 |
ast_free(buf); |
| 754 |
return CMP_STOP; |
| 755 |
} |
| 756 |
ast_str_append(&buf, 0, "EndpointName: %s\r\n", endpoint_name); |
| 757 |
|
| 758 |
|
| 759 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 760 |
++ami->count; |
| 761 |
|
| 762 |
ast_free(buf); |
| 763 |
return 0; |
| 764 |
} |
| 765 |
|
| 766 |
static int format_ami_endpoint_identify(const struct ast_sip_endpoint *endpoint, |
| 767 |
struct ast_sip_ami *ami) |
| 768 |
{ |
| 769 |
struct ao2_container *identifies; |
| 770 |
struct ast_variable fields = { |
| 771 |
.name = "endpoint", |
| 772 |
.value = ast_sorcery_object_get_id(endpoint), |
| 773 |
}; |
| 774 |
|
| 775 |
identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", |
| 776 |
AST_RETRIEVE_FLAG_MULTIPLE, &fields); |
| 777 |
if (!identifies) { |
| 778 |
return -1; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
ao2_callback_data(identifies, OBJ_MULTIPLE | OBJ_NODATA, |
| 783 |
send_identify_ami_event, |
| 784 |
(void *) ast_sorcery_object_get_id(endpoint), |
| 785 |
ami); |
| 786 |
|
| 787 |
ao2_ref(identifies, -1); |
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
struct ast_sip_endpoint_formatter endpoint_identify_formatter = { |
| 792 |
.format_ami = format_ami_endpoint_identify |
| 793 |
}; |
| 794 |
|
| 795 |
static int cli_iterator(void *container, ao2_callback_fn callback, void *args) |
| 796 |
{ |
| 797 |
const struct ast_sip_endpoint *endpoint = container; |
| 798 |
struct ao2_container *identifies; |
| 799 |
|
| 800 |
struct ast_variable fields = { |
| 801 |
.name = "endpoint", |
| 802 |
.value = ast_sorcery_object_get_id(endpoint), |
| 803 |
.next = NULL, |
| 804 |
}; |
| 805 |
|
| 806 |
identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", |
| 807 |
AST_RETRIEVE_FLAG_MULTIPLE, &fields); |
| 808 |
if (!identifies) { |
| 809 |
return -1; |
| 810 |
} |
| 811 |
|
| 812 |
ao2_callback(identifies, OBJ_NODATA, callback, args); |
| 813 |
ao2_cleanup(identifies); |
| 814 |
|
| 815 |
return 0; |
| 816 |
} |
| 817 |
|
| 818 |
static struct ao2_container *cli_get_container(const char *regex) |
| 819 |
{ |
| 820 |
RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); |
| 821 |
struct ao2_container *s_container; |
| 822 |
|
| 823 |
container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "identify", regex); |
| 824 |
if (!container) { |
| 825 |
return NULL; |
| 826 |
} |
| 827 |
|
| 828 |
s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 829 |
ast_sorcery_object_id_sort, ast_sorcery_object_id_compare); |
| 830 |
if (!s_container) { |
| 831 |
return NULL; |
| 832 |
} |
| 833 |
|
| 834 |
if (ao2_container_dup(s_container, container, 0)) { |
| 835 |
ao2_ref(s_container, -1); |
| 836 |
return NULL; |
| 837 |
} |
| 838 |
|
| 839 |
return s_container; |
| 840 |
} |
| 841 |
|
| 842 |
static void *cli_retrieve_by_id(const char *id) |
| 843 |
{ |
| 844 |
return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "identify", id); |
| 845 |
} |
| 846 |
|
| 847 |
static int cli_print_header(void *obj, void *arg, int flags) |
| 848 |
{ |
| 849 |
struct ast_sip_cli_context *context = arg; |
| 850 |
int indent = CLI_INDENT_TO_SPACES(context->indent_level); |
| 851 |
int filler = CLI_MAX_WIDTH - indent - 22; |
| 852 |
|
| 853 |
ast_assert(context->output_buffer != NULL); |
| 854 |
|
| 855 |
ast_str_append(&context->output_buffer, 0, |
| 856 |
"%*s: <Identify/Endpoint%*.*s>\n", |
| 857 |
indent, "Identify", filler, filler, CLI_HEADER_FILLER); |
| 858 |
|
| 859 |
if (context->recurse) { |
| 860 |
context->indent_level++; |
| 861 |
indent = CLI_INDENT_TO_SPACES(context->indent_level); |
| 862 |
filler = CLI_LAST_TABSTOP - indent - 24; |
| 863 |
|
| 864 |
ast_str_append(&context->output_buffer, 0, |
| 865 |
"%*s: <criteria%*.*s>\n", |
| 866 |
indent, "Match", filler, filler, CLI_HEADER_FILLER); |
| 867 |
|
| 868 |
context->indent_level--; |
| 869 |
} |
| 870 |
|
| 871 |
return 0; |
| 872 |
} |
| 873 |
|
| 874 |
static int cli_print_body(void *obj, void *arg, int flags) |
| 875 |
{ |
| 876 |
RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free); |
| 877 |
struct ip_identify_match *ident = obj; |
| 878 |
struct ast_sip_cli_context *context = arg; |
| 879 |
struct ast_ha *match; |
| 880 |
int indent; |
| 881 |
|
| 882 |
ast_assert(context->output_buffer != NULL); |
| 883 |
|
| 884 |
ast_str_append(&context->output_buffer, 0, "%*s: %s/%s\n", |
| 885 |
CLI_INDENT_TO_SPACES(context->indent_level), "Identify", |
| 886 |
ast_sorcery_object_get_id(ident), ident->endpoint_name); |
| 887 |
|
| 888 |
if (context->recurse) { |
| 889 |
context->indent_level++; |
| 890 |
indent = CLI_INDENT_TO_SPACES(context->indent_level); |
| 891 |
|
| 892 |
for (match = ident->matches; match; match = match->next) { |
| 893 |
const char *addr; |
| 894 |
|
| 895 |
if (ast_sockaddr_port(&match->addr)) { |
| 896 |
addr = ast_sockaddr_stringify(&match->addr); |
| 897 |
} else { |
| 898 |
addr = ast_sockaddr_stringify_addr(&match->addr); |
| 899 |
} |
| 900 |
|
| 901 |
ast_str_append(&context->output_buffer, 0, "%*s: %s%s/%d\n", |
| 902 |
indent, |
| 903 |
"Match", |
| 904 |
match->sense == AST_SENSE_ALLOW ? "!" : "", |
| 905 |
addr, ast_sockaddr_cidr_bits(&match->netmask)); |
| 906 |
} |
| 907 |
|
| 908 |
if (!ast_strlen_zero(ident->match_header)) { |
| 909 |
ast_str_append(&context->output_buffer, 0, "%*s: %s\n", |
| 910 |
indent, |
| 911 |
"Header", |
| 912 |
ident->match_header); |
| 913 |
} |
| 914 |
|
| 915 |
if (!ast_strlen_zero(ident->match_request_uri)) { |
| 916 |
ast_str_append(&context->output_buffer, 0, "%*s: %s\n", |
| 917 |
indent, |
| 918 |
"RequestURI", |
| 919 |
ident->match_request_uri); |
| 920 |
} |
| 921 |
|
| 922 |
context->indent_level--; |
| 923 |
|
| 924 |
if (context->indent_level == 0) { |
| 925 |
ast_str_append(&context->output_buffer, 0, "\n"); |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
if (context->show_details |
| 930 |
|| (context->show_details_only_level_0 && context->indent_level == 0)) { |
| 931 |
ast_str_append(&context->output_buffer, 0, "\n"); |
| 932 |
ast_sip_cli_print_sorcery_objectset(ident, context, 0); |
| 933 |
} |
| 934 |
|
| 935 |
return 0; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
static char *my_cli_traverse_objects(struct ast_cli_entry *e, int cmd, |
| 944 |
struct ast_cli_args *a) |
| 945 |
{ |
| 946 |
return ast_sip_cli_traverse_objects(e, cmd, a); |
| 947 |
} |
| 948 |
|
| 949 |
static struct ast_cli_entry cli_identify[] = { |
| 950 |
AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Identifies", |
| 951 |
.command = "pjsip list identifies", |
| 952 |
.usage = "Usage: pjsip list identifies [ like <pattern> ]\n" |
| 953 |
" List the configured PJSIP Identifies\n" |
| 954 |
" Optional regular expression pattern is used to filter the list.\n"), |
| 955 |
AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Identifies", |
| 956 |
.command = "pjsip show identifies", |
| 957 |
.usage = "Usage: pjsip show identifies [ like <pattern> ]\n" |
| 958 |
" Show the configured PJSIP Identifies\n" |
| 959 |
" Optional regular expression pattern is used to filter the list.\n"), |
| 960 |
AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Identify", |
| 961 |
.command = "pjsip show identify", |
| 962 |
.usage = "Usage: pjsip show identify <id>\n" |
| 963 |
" Show the configured PJSIP Identify\n"), |
| 964 |
}; |
| 965 |
|
| 966 |
static struct ast_sip_cli_formatter_entry *cli_formatter; |
| 967 |
|
| 968 |
static int load_module(void) |
| 969 |
{ |
| 970 |
ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_endpoint_identifier_ip"); |
| 971 |
ast_sorcery_apply_default(ast_sip_get_sorcery(), "identify", "config", "pjsip.conf,criteria=type=identify"); |
| 972 |
|
| 973 |
if (ast_sorcery_object_register(ast_sip_get_sorcery(), "identify", ip_identify_alloc, NULL, ip_identify_apply)) { |
| 974 |
return AST_MODULE_LOAD_DECLINE; |
| 975 |
} |
| 976 |
|
| 977 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "type", "", OPT_NOOP_T, 0, 0); |
| 978 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "endpoint", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ip_identify_match, endpoint_name)); |
| 979 |
ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "identify", "match", "", ip_identify_match_handler, match_to_str, match_to_var_list, 0, 0); |
| 980 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "match_header", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ip_identify_match, match_header)); |
| 981 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "match_request_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ip_identify_match, match_request_uri)); |
| 982 |
ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "srv_lookups", "yes", OPT_BOOL_T, 1, FLDSET(struct ip_identify_match, srv_lookups)); |
| 983 |
ast_sorcery_load_object(ast_sip_get_sorcery(), "identify"); |
| 984 |
|
| 985 |
ast_sip_register_endpoint_identifier_with_name(&ip_identifier, "ip"); |
| 986 |
ast_sip_register_endpoint_identifier_with_name(&header_identifier, "header"); |
| 987 |
ast_sip_register_endpoint_identifier_with_name(&request_identifier, "request_uri"); |
| 988 |
ast_sip_register_endpoint_formatter(&endpoint_identify_formatter); |
| 989 |
|
| 990 |
cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL); |
| 991 |
if (!cli_formatter) { |
| 992 |
ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n"); |
| 993 |
return AST_MODULE_LOAD_DECLINE; |
| 994 |
} |
| 995 |
cli_formatter->name = "identify"; |
| 996 |
cli_formatter->print_header = cli_print_header; |
| 997 |
cli_formatter->print_body = cli_print_body; |
| 998 |
cli_formatter->get_container = cli_get_container; |
| 999 |
cli_formatter->iterate = cli_iterator; |
| 1000 |
cli_formatter->get_id = ast_sorcery_object_get_id; |
| 1001 |
cli_formatter->retrieve_by_id = cli_retrieve_by_id; |
| 1002 |
|
| 1003 |
ast_sip_register_cli_formatter(cli_formatter); |
| 1004 |
ast_cli_register_multiple(cli_identify, ARRAY_LEN(cli_identify)); |
| 1005 |
|
| 1006 |
return AST_MODULE_LOAD_SUCCESS; |
| 1007 |
} |
| 1008 |
|
| 1009 |
static int reload_module(void) |
| 1010 |
{ |
| 1011 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "identify"); |
| 1012 |
|
| 1013 |
return 0; |
| 1014 |
} |
| 1015 |
|
| 1016 |
static int unload_module(void) |
| 1017 |
{ |
| 1018 |
ast_cli_unregister_multiple(cli_identify, ARRAY_LEN(cli_identify)); |
| 1019 |
ast_sip_unregister_cli_formatter(cli_formatter); |
| 1020 |
ast_sip_unregister_endpoint_formatter(&endpoint_identify_formatter); |
| 1021 |
ast_sip_unregister_endpoint_identifier(&header_identifier); |
| 1022 |
ast_sip_unregister_endpoint_identifier(&request_identifier); |
| 1023 |
ast_sip_unregister_endpoint_identifier(&ip_identifier); |
| 1024 |
|
| 1025 |
return 0; |
| 1026 |
} |
| 1027 |
|
| 1028 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP IP endpoint identifier", |
| 1029 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1030 |
.load = load_module, |
| 1031 |
.reload = reload_module, |
| 1032 |
.unload = unload_module, |
| 1033 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND - 4, |
| 1034 |
.requires = "res_pjsip", |
| 1035 |
); |