| 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 |
#include "asterisk.h" |
| 41 |
|
| 42 |
#include <regex.h> |
| 43 |
#include <pjsip.h> |
| 44 |
|
| 45 |
#include "asterisk/astobj2.h" |
| 46 |
#include "asterisk/cli.h" |
| 47 |
#include "asterisk/res_pjsip.h" |
| 48 |
#include "asterisk/module.h" |
| 49 |
#include "asterisk/pbx.h" |
| 50 |
#include "asterisk/sorcery.h" |
| 51 |
#include "asterisk/vector.h" |
| 52 |
#include "asterisk/stasis.h" |
| 53 |
#include "asterisk/acl.h" |
| 54 |
#include "asterisk/security_events.h" |
| 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 |
#define MAX_ID_SUFFIX 20 |
| 278 |
|
| 279 |
#define BASE_REGISTRAR "res_pjsip_config_wizard" |
| 280 |
|
| 281 |
|
| 282 |
AST_VECTOR(string_vector, char *); |
| 283 |
|
| 284 |
|
| 285 |
struct object_type_wizard { |
| 286 |
struct ast_sorcery *sorcery; |
| 287 |
struct ast_sorcery_wizard *wizard; |
| 288 |
void *wizard_data; |
| 289 |
struct ast_config *last_config; |
| 290 |
char object_type[]; |
| 291 |
}; |
| 292 |
static AST_VECTOR_RW(object_type_wizards, struct object_type_wizard *) object_type_wizards; |
| 293 |
|
| 294 |
|
| 295 |
#define NOT_EQUALS(a, b) (a != b) |
| 296 |
#define OTW_DELETE_CB(otw) ({ \ |
| 297 |
ast_config_destroy(otw->last_config); \ |
| 298 |
ast_free(otw); \ |
| 299 |
}) |
| 300 |
|
| 301 |
const static char *object_types[] = {"phoneprov", "registration", "identify", "endpoint", "aor", "auth", NULL}; |
| 302 |
|
| 303 |
static int acl_change_detected = 0; |
| 304 |
static struct stasis_subscription *acl_change_sub; |
| 305 |
|
| 306 |
|
| 307 |
static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 308 |
{ |
| 309 |
if (stasis_message_type(message) != ast_named_acl_change_type()) { |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
ast_debug(3, "PJSIP Wizard: Named ACL change detected via Stasis. Triggering reload.\n"); |
| 314 |
acl_change_detected = 1; |
| 315 |
ast_sorcery_reload(ast_sip_get_sorcery()); |
| 316 |
acl_change_detected = 0; |
| 317 |
} |
| 318 |
|
| 319 |
static int is_one_of(const char *needle, const char *haystack[]) |
| 320 |
{ |
| 321 |
int i; |
| 322 |
for (i = 0; haystack[i]; i++) { |
| 323 |
if (!strcmp(needle, haystack[i])) { |
| 324 |
return 1; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
static struct object_type_wizard *find_wizard(const char *object_type) |
| 333 |
{ |
| 334 |
int idx; |
| 335 |
|
| 336 |
AST_VECTOR_RW_RDLOCK(&object_type_wizards); |
| 337 |
for(idx = 0; idx < AST_VECTOR_SIZE(&object_type_wizards); idx++) { |
| 338 |
struct object_type_wizard *otw = AST_VECTOR_GET(&object_type_wizards, idx); |
| 339 |
if (!strcmp(otw->object_type, object_type)) { |
| 340 |
AST_VECTOR_RW_UNLOCK(&object_type_wizards); |
| 341 |
return otw; |
| 342 |
} |
| 343 |
} |
| 344 |
AST_VECTOR_RW_UNLOCK(&object_type_wizards); |
| 345 |
|
| 346 |
return NULL; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
static void *create_object(const struct ast_sorcery *sorcery, |
| 351 |
const char *id, const char *type, struct ast_variable *vars) |
| 352 |
{ |
| 353 |
struct ast_sorcery_object *obj = ast_sorcery_alloc(sorcery, type, id); |
| 354 |
|
| 355 |
if (!obj) { |
| 356 |
ast_log(LOG_ERROR, "Unable to allocate an object of type '%s' with id '%s'.\n", type, id); |
| 357 |
return NULL; |
| 358 |
} |
| 359 |
|
| 360 |
if (ast_sorcery_objectset_apply(sorcery, obj, vars)) { |
| 361 |
ast_log(LOG_ERROR, "Unable to apply object type '%s' with id '%s'. Check preceeding errors.\n", type, id); |
| 362 |
ao2_ref(obj, -1); |
| 363 |
return NULL; |
| 364 |
} |
| 365 |
|
| 366 |
return obj; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
static int is_variable_true(struct ast_variable *vars, const char *name) |
| 371 |
{ |
| 372 |
return ast_true(ast_variable_find_last_in_list(vars, name)); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
static int variable_list_append(struct ast_variable **existing, const char *name, const char *value) |
| 377 |
{ |
| 378 |
struct ast_variable *new = ast_variable_new(name, value, ""); |
| 379 |
|
| 380 |
if (!new) { |
| 381 |
ast_log(LOG_ERROR, "Unable to allocate memory for new variable '%s'.\n", name); |
| 382 |
return -1; |
| 383 |
} |
| 384 |
|
| 385 |
ast_variable_list_append(existing, new); |
| 386 |
|
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
#define variable_list_append_return(existing, name, value) ({ \ |
| 393 |
struct ast_variable *new = ast_variable_new(name, value, ""); \ |
| 394 |
if (!new) { \ |
| 395 |
ast_log(LOG_ERROR, "Unable to allocate memory for new variable '%s'.\n", name); \ |
| 396 |
return -1; \ |
| 397 |
} \ |
| 398 |
ast_variable_list_append(existing, new); \ |
| 399 |
}) |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
static struct ast_variable *get_object_variables(struct ast_variable *vars, char *prefix) |
| 406 |
{ |
| 407 |
struct ast_variable *return_vars = NULL; |
| 408 |
struct ast_variable *v = vars; |
| 409 |
int plen = strlen(prefix); |
| 410 |
|
| 411 |
for(; v; v = v->next) { |
| 412 |
if (ast_begins_with(v->name, prefix) && strlen(v->name) > plen) { |
| 413 |
if (variable_list_append(&return_vars, v->name + plen, v->value)) { |
| 414 |
ast_variables_destroy(return_vars); |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
return return_vars; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
static int delete_extens(const char *context, const char *exten) |
| 425 |
{ |
| 426 |
struct pbx_find_info find_info = { .stacklen = 0 }; |
| 427 |
|
| 428 |
if (pbx_find_extension(NULL, NULL, &find_info, context, exten, PRIORITY_HINT, NULL, NULL, E_MATCH)) { |
| 429 |
ast_context_remove_extension(context, exten, PRIORITY_HINT, BASE_REGISTRAR); |
| 430 |
} |
| 431 |
|
| 432 |
if (pbx_find_extension(NULL, NULL, &find_info, context, exten, 1, NULL, NULL, E_MATCH)) { |
| 433 |
ast_context_remove_extension(context, exten, 1, BASE_REGISTRAR); |
| 434 |
} |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
static int add_extension(struct ast_context *context, const char *exten, |
| 440 |
int priority, const char *application) |
| 441 |
{ |
| 442 |
struct pbx_find_info find_info = { .stacklen = 0 }; |
| 443 |
struct ast_exten *existing_exten; |
| 444 |
char *data = NULL; |
| 445 |
char *app = NULL; |
| 446 |
void *free_ptr = NULL; |
| 447 |
const char *paren; |
| 448 |
const char *context_name; |
| 449 |
|
| 450 |
if (!context || ast_strlen_zero(exten) || ast_strlen_zero(application)) { |
| 451 |
return -1; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
paren = strchr(application, '('); |
| 461 |
if (!paren) { |
| 462 |
app = (char *)application; |
| 463 |
} else { |
| 464 |
app = ast_strdupa(application); |
| 465 |
app[paren - application] = '\0'; |
| 466 |
data = ast_strdup(paren + 1); |
| 467 |
if (!data) { |
| 468 |
return -1; |
| 469 |
} |
| 470 |
data[strlen(data) - 1] = '\0'; |
| 471 |
free_ptr = ast_free_ptr; |
| 472 |
if (ast_strlen_zero(app) || ast_strlen_zero(data)) { |
| 473 |
ast_free(data); |
| 474 |
return -1; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
context_name = ast_get_context_name(context); |
| 480 |
if ((existing_exten = pbx_find_extension(NULL, NULL, &find_info, context_name, exten, |
| 481 |
priority, NULL, NULL, E_MATCH))) { |
| 482 |
const char *existing_app = ast_get_extension_app(existing_exten); |
| 483 |
const char *existing_data = ast_get_extension_app_data(existing_exten); |
| 484 |
if (!strcmp(existing_app, app) |
| 485 |
&& !strcmp(existing_data ? existing_data : "", data ? data : "")) { |
| 486 |
ast_free(data); |
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
ast_context_remove_extension2(context, exten, priority, BASE_REGISTRAR, 1); |
| 491 |
} |
| 492 |
|
| 493 |
if (ast_add_extension2_nolock(context, 0, exten, priority, NULL, NULL, |
| 494 |
app, data, free_ptr, BASE_REGISTRAR, NULL, 0)) { |
| 495 |
return -1; |
| 496 |
} |
| 497 |
|
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
static int add_hints(const char *context, const char *exten, const char *application, const char *id) |
| 502 |
{ |
| 503 |
struct ast_context *hint_context; |
| 504 |
char *hint_device; |
| 505 |
|
| 506 |
hint_device = ast_alloca(strlen("PJSIP/") + strlen(id) + 1); |
| 507 |
sprintf(hint_device, "PJSIP/%s", id); |
| 508 |
|
| 509 |
|
| 510 |
if (ast_wrlock_contexts()) { |
| 511 |
ast_log(LOG_ERROR, "Failed to lock the contexts list.\n"); |
| 512 |
return -1; |
| 513 |
} |
| 514 |
|
| 515 |
if (!(hint_context = ast_context_find_or_create(NULL, NULL, context, BASE_REGISTRAR))) { |
| 516 |
ast_log(LOG_ERROR, "Unable to find or create hint context '%s'\n", context); |
| 517 |
if (ast_unlock_contexts()) { |
| 518 |
ast_assert(0); |
| 519 |
} |
| 520 |
return -1; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
if (ast_wrlock_context(hint_context)) { |
| 525 |
ast_unlock_contexts(); |
| 526 |
ast_log(LOG_ERROR, "failed to obtain write lock on context\n"); |
| 527 |
return -1; |
| 528 |
} |
| 529 |
ast_unlock_contexts(); |
| 530 |
|
| 531 |
if (add_extension(hint_context, exten, PRIORITY_HINT, hint_device)) { |
| 532 |
ast_log(LOG_ERROR, "Failed to add hint '%s@%s' to the PBX.\n", |
| 533 |
exten, context); |
| 534 |
} |
| 535 |
|
| 536 |
if (!ast_strlen_zero(application)) { |
| 537 |
if (add_extension(hint_context, exten, 1, application)) { |
| 538 |
ast_log(LOG_ERROR, "Failed to add hint '%s@%s' to the PBX.\n", |
| 539 |
exten, context); |
| 540 |
} |
| 541 |
} else { |
| 542 |
ast_context_remove_extension2(hint_context, exten, 1, BASE_REGISTRAR, 1); |
| 543 |
} |
| 544 |
|
| 545 |
ast_unlock_context(hint_context); |
| 546 |
|
| 547 |
return 0; |
| 548 |
} |
| 549 |
|
| 550 |
static int handle_auth(const struct ast_sorcery *sorcery, struct object_type_wizard *otw, |
| 551 |
struct ast_category *wiz, char *direction) |
| 552 |
{ |
| 553 |
struct ast_variable *wizvars = ast_category_first(wiz); |
| 554 |
struct ast_sorcery_object *obj = NULL; |
| 555 |
const char *id = ast_category_get_name(wiz); |
| 556 |
char new_id[strlen(id) + MAX_ID_SUFFIX]; |
| 557 |
char prefix[strlen(direction) + strlen("_auth/") + 1]; |
| 558 |
char *test_variable = NULL; |
| 559 |
RAII_VAR(struct ast_variable *, vars, NULL, ast_variables_destroy); |
| 560 |
|
| 561 |
snprintf(prefix, sizeof(prefix), "%s_auth/", direction); |
| 562 |
vars = get_object_variables(wizvars, prefix); |
| 563 |
|
| 564 |
if (!strcmp(direction, "outbound")) { |
| 565 |
snprintf(new_id, sizeof(new_id), "%s-oauth", id); |
| 566 |
test_variable = "sends_auth"; |
| 567 |
} else { |
| 568 |
snprintf(new_id, sizeof(new_id), "%s-iauth", id); |
| 569 |
test_variable = "accepts_auth"; |
| 570 |
} |
| 571 |
|
| 572 |
if (is_variable_true(wizvars, test_variable)) { |
| 573 |
if (!ast_variable_find_last_in_list(vars, "username")) { |
| 574 |
ast_log(LOG_ERROR, |
| 575 |
"Wizard '%s' must have '%s_auth/username' if it %s.\n", id, direction, test_variable); |
| 576 |
return -1; |
| 577 |
} |
| 578 |
} else { |
| 579 |
|
| 580 |
obj = otw->wizard->retrieve_id(sorcery, otw->wizard_data, "auth", new_id); |
| 581 |
if (obj) { |
| 582 |
otw->wizard->delete(sorcery, otw->wizard_data, obj); |
| 583 |
ao2_ref(obj, -1); |
| 584 |
} |
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
variable_list_append_return(&vars, "@pjsip_wizard", id); |
| 589 |
|
| 590 |
|
| 591 |
if (!ast_variable_find_last_in_list(vars, "auth_type")) { |
| 592 |
variable_list_append_return(&vars, "auth_type", "userpass"); |
| 593 |
} |
| 594 |
|
| 595 |
obj = create_object(sorcery, new_id, "auth", vars); |
| 596 |
if (!obj) { |
| 597 |
return -1; |
| 598 |
} |
| 599 |
|
| 600 |
if (otw->wizard->update(sorcery, otw->wizard_data, obj)) { |
| 601 |
otw->wizard->create(sorcery, otw->wizard_data, obj); |
| 602 |
} |
| 603 |
ao2_ref(obj, -1); |
| 604 |
|
| 605 |
return 0; |
| 606 |
} |
| 607 |
|
| 608 |
static int handle_auths(const struct ast_sorcery *sorcery, struct object_type_wizard *otw, |
| 609 |
struct ast_category *wiz) |
| 610 |
{ |
| 611 |
int rc; |
| 612 |
|
| 613 |
if ((rc = handle_auth(sorcery, otw, wiz, "outbound"))) { |
| 614 |
return rc; |
| 615 |
} |
| 616 |
|
| 617 |
return handle_auth(sorcery, otw, wiz, "inbound"); |
| 618 |
} |
| 619 |
|
| 620 |
static int handle_aor(const struct ast_sorcery *sorcery, struct object_type_wizard *otw, |
| 621 |
struct ast_category *wiz, struct string_vector *remote_hosts_vector) |
| 622 |
{ |
| 623 |
struct ast_variable *wizvars = ast_category_first(wiz); |
| 624 |
struct ast_sorcery_object *obj = NULL; |
| 625 |
const char *id = ast_category_get_name(wiz); |
| 626 |
const char *contact_pattern; |
| 627 |
const char *outbound_proxy = ast_variable_find_last_in_list(wizvars, "outbound_proxy"); |
| 628 |
int host_count = AST_VECTOR_SIZE(remote_hosts_vector); |
| 629 |
RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "aor/"), ast_variables_destroy); |
| 630 |
|
| 631 |
variable_list_append(&vars, "@pjsip_wizard", id); |
| 632 |
|
| 633 |
if (!ast_strlen_zero(outbound_proxy)) { |
| 634 |
variable_list_append_return(&vars, "outbound_proxy", outbound_proxy); |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
if (!ast_variable_find_last_in_list(vars, "contact")) { |
| 639 |
if (!(contact_pattern = ast_variable_find_last_in_list(wizvars, "contact_pattern"))) { |
| 640 |
contact_pattern = "sip:${REMOTE_HOST}"; |
| 641 |
} |
| 642 |
|
| 643 |
if (host_count > 0 && !ast_strlen_zero(contact_pattern)) { |
| 644 |
int host_counter; |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
for (host_counter = 0; host_counter < host_count; host_counter++) { |
| 651 |
RAII_VAR(struct ast_str *, new_str, ast_str_create(64), ast_free); |
| 652 |
RAII_VAR(struct varshead *, subst_vars, ast_var_list_create(), ast_var_list_destroy); |
| 653 |
struct ast_var_t *var = ast_var_assign("REMOTE_HOST", |
| 654 |
AST_VECTOR_GET(remote_hosts_vector, host_counter)); |
| 655 |
|
| 656 |
AST_VAR_LIST_INSERT_TAIL(subst_vars, var); |
| 657 |
ast_str_substitute_variables_varshead(&new_str, 0, subst_vars, |
| 658 |
contact_pattern); |
| 659 |
|
| 660 |
variable_list_append_return(&vars, "contact", ast_str_buffer(new_str)); |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
obj = create_object(sorcery, id, "aor", vars); |
| 666 |
if (!obj) { |
| 667 |
return -1; |
| 668 |
} |
| 669 |
|
| 670 |
if (otw->wizard->update(sorcery, otw->wizard_data, obj)) { |
| 671 |
otw->wizard->create(sorcery, otw->wizard_data, obj); |
| 672 |
} |
| 673 |
ao2_ref(obj, -1); |
| 674 |
|
| 675 |
return 0; |
| 676 |
} |
| 677 |
|
| 678 |
static int handle_endpoint(const struct ast_sorcery *sorcery, struct object_type_wizard *otw, |
| 679 |
struct ast_category *wiz) |
| 680 |
{ |
| 681 |
struct ast_variable *wizvars = ast_category_first(wiz); |
| 682 |
struct ast_sorcery_object *obj = NULL; |
| 683 |
const char *id = ast_category_get_name(wiz); |
| 684 |
const char *outbound_proxy = ast_variable_find_last_in_list(wizvars, "outbound_proxy"); |
| 685 |
const char *transport = ast_variable_find_last_in_list(wizvars, "transport"); |
| 686 |
const char *hint_context = hint_context = ast_variable_find_last_in_list(wizvars, "hint_context"); |
| 687 |
const char *hint_exten = ast_variable_find_last_in_list(wizvars, "hint_exten"); |
| 688 |
const char *hint_application= ast_variable_find_last_in_list(wizvars, "hint_application"); |
| 689 |
char new_id[strlen(id) + MAX_ID_SUFFIX]; |
| 690 |
RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "endpoint/"), ast_variables_destroy); |
| 691 |
|
| 692 |
variable_list_append_return(&vars, "@pjsip_wizard", id); |
| 693 |
variable_list_append_return(&vars, "aors", id); |
| 694 |
|
| 695 |
if (!ast_strlen_zero(outbound_proxy)) { |
| 696 |
variable_list_append_return(&vars, "outbound_proxy", outbound_proxy); |
| 697 |
} |
| 698 |
|
| 699 |
if (ast_strlen_zero(hint_context)) { |
| 700 |
hint_context = ast_variable_find_last_in_list(vars, "context"); |
| 701 |
} |
| 702 |
|
| 703 |
if (ast_strlen_zero(hint_context)) { |
| 704 |
hint_context = "default"; |
| 705 |
} |
| 706 |
|
| 707 |
if (!ast_strlen_zero(hint_exten)) { |
| 708 |
|
| 709 |
variable_list_append_return(&vars, "@hint_context", hint_context); |
| 710 |
variable_list_append_return(&vars, "@hint_exten", hint_exten); |
| 711 |
} |
| 712 |
|
| 713 |
if (!ast_strlen_zero(transport)) { |
| 714 |
variable_list_append_return(&vars, "transport", transport); |
| 715 |
} |
| 716 |
|
| 717 |
if (is_variable_true(wizvars, "sends_auth")) { |
| 718 |
snprintf(new_id, sizeof(new_id), "%s-oauth", id); |
| 719 |
variable_list_append_return(&vars, "outbound_auth", new_id); |
| 720 |
} |
| 721 |
|
| 722 |
if (is_variable_true(wizvars, "accepts_auth")) { |
| 723 |
snprintf(new_id, sizeof(new_id), "%s-iauth", id); |
| 724 |
variable_list_append_return(&vars, "auth", new_id); |
| 725 |
} |
| 726 |
|
| 727 |
obj = create_object(sorcery, id, "endpoint", vars); |
| 728 |
if (!obj) { |
| 729 |
return -1; |
| 730 |
} |
| 731 |
|
| 732 |
if (otw->wizard->update(sorcery, otw->wizard_data, obj)) { |
| 733 |
otw->wizard->create(sorcery, otw->wizard_data, obj); |
| 734 |
} |
| 735 |
ao2_ref(obj, -1); |
| 736 |
|
| 737 |
if (!ast_strlen_zero(hint_exten)) { |
| 738 |
if (is_variable_true(wizvars, "has_hint")) { |
| 739 |
add_hints(hint_context, hint_exten, hint_application, id); |
| 740 |
} else { |
| 741 |
delete_extens(hint_context, hint_exten); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
static int handle_identify(const struct ast_sorcery *sorcery, struct object_type_wizard *otw, |
| 749 |
struct ast_category *wiz, struct string_vector *remote_hosts_vector) |
| 750 |
{ |
| 751 |
struct ast_variable *wizvars = ast_category_first(wiz); |
| 752 |
struct ast_sorcery_object *obj = NULL; |
| 753 |
const char *id = ast_category_get_name(wiz); |
| 754 |
char new_id[strlen(id) + MAX_ID_SUFFIX]; |
| 755 |
int host_count = AST_VECTOR_SIZE(remote_hosts_vector); |
| 756 |
int host_counter; |
| 757 |
RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "identify/"), ast_variables_destroy); |
| 758 |
|
| 759 |
snprintf(new_id, sizeof(new_id), "%s-identify", id); |
| 760 |
|
| 761 |
|
| 762 |
if (is_variable_true(wizvars, "accepts_registrations") |
| 763 |
|| is_variable_true(wizvars, "sends_line_with_registrations")) { |
| 764 |
|
| 765 |
obj = otw->wizard->retrieve_id(sorcery, otw->wizard_data, "identify", new_id); |
| 766 |
if (obj) { |
| 767 |
otw->wizard->delete(sorcery, otw->wizard_data, obj); |
| 768 |
ao2_ref(obj, -1); |
| 769 |
} |
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
if (!host_count) { |
| 774 |
ast_log(LOG_ERROR, |
| 775 |
"Wizard '%s' must have 'remote_hosts' if it doesn't accept registrations.\n", id); |
| 776 |
return -1; |
| 777 |
} |
| 778 |
|
| 779 |
variable_list_append_return(&vars, "endpoint", id); |
| 780 |
variable_list_append_return(&vars, "@pjsip_wizard", id); |
| 781 |
|
| 782 |
if (!ast_variable_find_last_in_list(vars, "match")) { |
| 783 |
for (host_counter = 0; host_counter < host_count; host_counter++) { |
| 784 |
variable_list_append_return(&vars, "match", |
| 785 |
AST_VECTOR_GET(remote_hosts_vector, host_counter)); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
obj = create_object(sorcery, new_id, "identify", vars); |
| 790 |
if (!obj) { |
| 791 |
return -1; |
| 792 |
} |
| 793 |
|
| 794 |
if (otw->wizard->update(sorcery, otw->wizard_data, obj)) { |
| 795 |
otw->wizard->create(sorcery, otw->wizard_data, obj); |
| 796 |
} |
| 797 |
ao2_ref(obj, -1); |
| 798 |
|
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
static int handle_phoneprov(const struct ast_sorcery *sorcery, struct object_type_wizard *otw, |
| 803 |
struct ast_category *wiz) |
| 804 |
{ |
| 805 |
struct ast_variable *wizvars = ast_category_first(wiz); |
| 806 |
struct ast_sorcery_object *obj = NULL; |
| 807 |
const char *id = ast_category_get_name(wiz); |
| 808 |
char new_id[strlen(id) + MAX_ID_SUFFIX]; |
| 809 |
RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "phoneprov/"), ast_variables_destroy); |
| 810 |
|
| 811 |
snprintf(new_id, sizeof(new_id), "%s-phoneprov", id); |
| 812 |
|
| 813 |
if (!is_variable_true(wizvars, "has_phoneprov")) { |
| 814 |
obj = otw->wizard->retrieve_id(sorcery, otw->wizard_data, "phoneprov", new_id); |
| 815 |
if (obj) { |
| 816 |
otw->wizard->delete(sorcery, otw->wizard_data, obj); |
| 817 |
ao2_ref(obj, -1); |
| 818 |
} |
| 819 |
return 0; |
| 820 |
} |
| 821 |
|
| 822 |
if (!ast_variable_find_last_in_list(wizvars, "phoneprov/MAC")) { |
| 823 |
ast_log(LOG_ERROR, |
| 824 |
"Wizard '%s' must have 'phoneprov/MAC' if it has_phoneprov.\n", id); |
| 825 |
return -1; |
| 826 |
} |
| 827 |
|
| 828 |
variable_list_append_return(&vars, "endpoint", id); |
| 829 |
variable_list_append_return(&vars, "@pjsip_wizard", id); |
| 830 |
|
| 831 |
obj = create_object(sorcery, new_id, "phoneprov", vars); |
| 832 |
if (!obj) { |
| 833 |
return -1; |
| 834 |
} |
| 835 |
|
| 836 |
if (otw->wizard->update(sorcery, otw->wizard_data, obj)) { |
| 837 |
otw->wizard->create(sorcery, otw->wizard_data, obj); |
| 838 |
} |
| 839 |
ao2_ref(obj, -1); |
| 840 |
|
| 841 |
return 0; |
| 842 |
} |
| 843 |
|
| 844 |
static int delete_existing_cb(void *obj, void *arg, int flags) |
| 845 |
{ |
| 846 |
struct object_type_wizard *otw = arg; |
| 847 |
|
| 848 |
if (!strcmp(otw->object_type, "endpoint")) { |
| 849 |
const char *context = ast_sorcery_object_get_extended(obj, "hint_context"); |
| 850 |
const char *exten = ast_sorcery_object_get_extended(obj, "hint_exten"); |
| 851 |
if (!ast_strlen_zero(context) && !ast_strlen_zero(exten)) { |
| 852 |
delete_extens(context, exten); |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
otw->wizard->delete(otw->sorcery, otw->wizard_data, obj); |
| 857 |
|
| 858 |
return CMP_MATCH; |
| 859 |
} |
| 860 |
|
| 861 |
static int handle_registrations(const struct ast_sorcery *sorcery, struct object_type_wizard *otw, |
| 862 |
struct ast_category *wiz, struct string_vector *remote_hosts_vector) |
| 863 |
{ |
| 864 |
struct ast_variable *search; |
| 865 |
struct ast_variable *wizvars = ast_category_first(wiz); |
| 866 |
const char *id = ast_category_get_name(wiz); |
| 867 |
const char *server_uri_pattern; |
| 868 |
const char *client_uri_pattern; |
| 869 |
const char *outbound_proxy = ast_variable_find_last_in_list(wizvars, "outbound_proxy"); |
| 870 |
const char *transport = ast_variable_find_last_in_list(wizvars, "transport"); |
| 871 |
const char *username; |
| 872 |
char new_id[strlen(id) + MAX_ID_SUFFIX]; |
| 873 |
int host_count = AST_VECTOR_SIZE(remote_hosts_vector); |
| 874 |
int host_counter; |
| 875 |
RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "registration/"), ast_variables_destroy); |
| 876 |
RAII_VAR(struct ao2_container *, existing, |
| 877 |
ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL), ao2_cleanup); |
| 878 |
|
| 879 |
if (!existing) { |
| 880 |
return -1; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
search = ast_variable_new("@pjsip_wizard", id, ""); |
| 885 |
if (!search) { |
| 886 |
return -1; |
| 887 |
} |
| 888 |
|
| 889 |
if (!ast_strlen_zero(outbound_proxy)) { |
| 890 |
variable_list_append_return(&vars, "outbound_proxy", outbound_proxy); |
| 891 |
} |
| 892 |
|
| 893 |
otw->wizard->retrieve_multiple(sorcery, otw->wizard_data, "registration", existing, search); |
| 894 |
ast_variables_destroy(search); |
| 895 |
|
| 896 |
|
| 897 |
if (!is_variable_true(wizvars, "sends_registrations")) { |
| 898 |
if (ao2_container_count(existing) > 0) { |
| 899 |
ao2_callback(existing, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, delete_existing_cb, otw); |
| 900 |
} |
| 901 |
return 0; |
| 902 |
} |
| 903 |
|
| 904 |
if (!host_count) { |
| 905 |
ast_log(LOG_ERROR, "Wizard '%s' must have 'remote_hosts' if it sends registrations.\n", id); |
| 906 |
return -1; |
| 907 |
} |
| 908 |
|
| 909 |
variable_list_append_return(&vars, "@pjsip_wizard", id); |
| 910 |
|
| 911 |
if (!(server_uri_pattern = ast_variable_find_last_in_list(wizvars, "server_uri_pattern"))) { |
| 912 |
server_uri_pattern = "sip:${REMOTE_HOST}"; |
| 913 |
} |
| 914 |
|
| 915 |
if (!(client_uri_pattern = ast_variable_find_last_in_list(wizvars, "client_uri_pattern"))) { |
| 916 |
client_uri_pattern = "sip:${USERNAME}@${REMOTE_HOST}"; |
| 917 |
} |
| 918 |
|
| 919 |
if (is_variable_true(wizvars, "sends_auth")) { |
| 920 |
if (!(username = ast_variable_find_last_in_list(wizvars, "outbound_auth/username"))) { |
| 921 |
ast_log(LOG_ERROR, "Wizard '%s' must have 'outbound_auth/username' if it sends" |
| 922 |
" authentication.\n", id); |
| 923 |
return -1; |
| 924 |
} |
| 925 |
} else { |
| 926 |
username = id; |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
for (host_counter = 0; host_counter < host_count; host_counter++) { |
| 934 |
struct ast_var_t *rh = ast_var_assign("REMOTE_HOST", |
| 935 |
AST_VECTOR_GET(remote_hosts_vector, host_counter)); |
| 936 |
struct ast_var_t *un = ast_var_assign("USERNAME", username); |
| 937 |
struct ast_sorcery_object *obj; |
| 938 |
RAII_VAR(struct ast_str *, uri, ast_str_create(64), ast_free); |
| 939 |
RAII_VAR(struct varshead *, subst_vars, ast_var_list_create(), ast_var_list_destroy); |
| 940 |
RAII_VAR(struct ast_variable *, registration_vars, vars ? ast_variables_dup(vars) : NULL, ast_variables_destroy); |
| 941 |
|
| 942 |
AST_VAR_LIST_INSERT_TAIL(subst_vars, rh); |
| 943 |
AST_VAR_LIST_INSERT_TAIL(subst_vars, un); |
| 944 |
|
| 945 |
if (!ast_strlen_zero(server_uri_pattern)) { |
| 946 |
ast_str_substitute_variables_varshead(&uri, 0, subst_vars, |
| 947 |
server_uri_pattern); |
| 948 |
variable_list_append_return(®istration_vars, "server_uri", ast_str_buffer(uri)); |
| 949 |
} |
| 950 |
|
| 951 |
if (!ast_strlen_zero(client_uri_pattern)) { |
| 952 |
ast_str_reset(uri); |
| 953 |
ast_str_substitute_variables_varshead(&uri, 0, subst_vars, |
| 954 |
client_uri_pattern); |
| 955 |
variable_list_append_return(®istration_vars, "client_uri", ast_str_buffer(uri)); |
| 956 |
} |
| 957 |
|
| 958 |
if (is_variable_true(wizvars, "sends_auth")) { |
| 959 |
snprintf(new_id, sizeof(new_id), "%s-oauth", id); |
| 960 |
variable_list_append_return(®istration_vars, "outbound_auth", new_id); |
| 961 |
} |
| 962 |
|
| 963 |
if (!ast_strlen_zero(transport)) { |
| 964 |
variable_list_append_return(®istration_vars, "transport", transport); |
| 965 |
} |
| 966 |
|
| 967 |
if (is_variable_true(wizvars, "sends_line_with_registrations")) { |
| 968 |
variable_list_append_return(®istration_vars, "line", "yes"); |
| 969 |
variable_list_append_return(®istration_vars, "endpoint", id); |
| 970 |
} |
| 971 |
|
| 972 |
snprintf(new_id, sizeof(new_id), "%s-reg-%d", id, host_counter); |
| 973 |
|
| 974 |
obj = create_object(sorcery, new_id, "registration", registration_vars); |
| 975 |
if (!obj) { |
| 976 |
return -1; |
| 977 |
} |
| 978 |
|
| 979 |
if (otw->wizard->update(sorcery, otw->wizard_data, obj)) { |
| 980 |
otw->wizard->create(sorcery, otw->wizard_data, obj); |
| 981 |
} |
| 982 |
ao2_ref(obj, -1); |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
ao2_callback(existing, OBJ_NODATA | OBJ_UNLINK | OBJ_SEARCH_KEY, ast_sorcery_object_id_compare, new_id); |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
if (ao2_container_count(existing) > 0) { |
| 992 |
ao2_callback(existing, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, delete_existing_cb, otw); |
| 993 |
} |
| 994 |
|
| 995 |
return 0; |
| 996 |
} |
| 997 |
|
| 998 |
static int wizard_apply_handler(const struct ast_sorcery *sorcery, struct object_type_wizard *otw, |
| 999 |
struct ast_category *wiz) |
| 1000 |
{ |
| 1001 |
struct ast_variable *wizvars = ast_category_first(wiz); |
| 1002 |
struct string_vector remote_hosts_vector; |
| 1003 |
const char *remote_hosts; |
| 1004 |
int rc = -1; |
| 1005 |
|
| 1006 |
AST_VECTOR_INIT(&remote_hosts_vector, 16); |
| 1007 |
remote_hosts = ast_variable_find_last_in_list(wizvars, "remote_hosts"); |
| 1008 |
|
| 1009 |
if (!ast_strlen_zero(remote_hosts)) { |
| 1010 |
char *host; |
| 1011 |
char *hosts = ast_strdupa(remote_hosts); |
| 1012 |
|
| 1013 |
while ((host = ast_strsep(&hosts, ',', AST_STRSEP_TRIM))) { |
| 1014 |
host = ast_strdup(host); |
| 1015 |
if (host && AST_VECTOR_APPEND(&remote_hosts_vector, host)) { |
| 1016 |
ast_free(host); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
ast_debug(4, "%s handler starting.\n", otw->object_type); |
| 1022 |
|
| 1023 |
if (!strcmp(otw->object_type, "auth")) { |
| 1024 |
rc = handle_auths(sorcery, otw, wiz); |
| 1025 |
} else if (!strcmp(otw->object_type, "aor")) { |
| 1026 |
rc = handle_aor(sorcery, otw, wiz, &remote_hosts_vector); |
| 1027 |
} else if (!strcmp(otw->object_type, "endpoint")) { |
| 1028 |
rc = handle_endpoint(sorcery, otw, wiz); |
| 1029 |
} else if (!strcmp(otw->object_type, "identify")) { |
| 1030 |
rc = handle_identify(sorcery, otw, wiz, &remote_hosts_vector); |
| 1031 |
} else if (!strcmp(otw->object_type, "phoneprov")) { |
| 1032 |
rc = handle_phoneprov(sorcery, otw, wiz); |
| 1033 |
} else if (!strcmp(otw->object_type, "registration")) { |
| 1034 |
rc = handle_registrations(sorcery, otw, wiz, &remote_hosts_vector); |
| 1035 |
} |
| 1036 |
|
| 1037 |
AST_VECTOR_REMOVE_ALL_CMP_UNORDERED(&remote_hosts_vector, NULL, NOT_EQUALS, ast_free); |
| 1038 |
AST_VECTOR_FREE(&remote_hosts_vector); |
| 1039 |
|
| 1040 |
ast_debug(4, "%s handler complete. rc: %d\n", otw->object_type, rc); |
| 1041 |
|
| 1042 |
return rc; |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
static void instance_created_observer(const char *name, struct ast_sorcery *sorcery); |
| 1049 |
static void instance_destroying_observer(const char *name, struct ast_sorcery *sorcery); |
| 1050 |
static void object_type_loaded_observer(const char *name, |
| 1051 |
const struct ast_sorcery *sorcery, const char *object_type, int reloaded); |
| 1052 |
static void object_type_registered_observer(const char *name, |
| 1053 |
struct ast_sorcery *sorcery, const char *object_type); |
| 1054 |
|
| 1055 |
const static struct ast_sorcery_global_observer global_observer = { |
| 1056 |
.instance_created = instance_created_observer, |
| 1057 |
.instance_destroying = instance_destroying_observer, |
| 1058 |
}; |
| 1059 |
|
| 1060 |
struct ast_sorcery_instance_observer observer = { |
| 1061 |
.object_type_registered = object_type_registered_observer, |
| 1062 |
.object_type_loaded = object_type_loaded_observer, |
| 1063 |
}; |
| 1064 |
|
| 1065 |
|
| 1066 |
static void object_type_loaded_observer(const char *name, |
| 1067 |
const struct ast_sorcery *sorcery, const char *object_type, int reloaded) |
| 1068 |
{ |
| 1069 |
struct ast_category *category = NULL; |
| 1070 |
struct object_type_wizard *otw = NULL; |
| 1071 |
char *filename = "pjsip_wizard.conf"; |
| 1072 |
struct ast_flags flags = { 0 }; |
| 1073 |
struct ast_config *cfg; |
| 1074 |
|
| 1075 |
if (!strstr("auth aor endpoint identify registration phoneprov", object_type)) { |
| 1076 |
|
| 1077 |
return; |
| 1078 |
} |
| 1079 |
|
| 1080 |
otw = find_wizard(object_type); |
| 1081 |
if (!otw) { |
| 1082 |
ast_log(LOG_ERROR, "There was no wizard for object type '%s'\n", object_type); |
| 1083 |
return; |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
if (reloaded && otw->last_config && !acl_change_detected) { |
| 1089 |
flags.flags = CONFIG_FLAG_FILEUNCHANGED; |
| 1090 |
} |
| 1091 |
|
| 1092 |
cfg = ast_config_load2(filename, object_type, flags); |
| 1093 |
|
| 1094 |
if (!cfg) { |
| 1095 |
ast_log(LOG_ERROR, "Unable to load config file '%s'\n", filename); |
| 1096 |
return; |
| 1097 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 1098 |
ast_debug(2, "Config file '%s' was unchanged for '%s'.\n", filename, object_type); |
| 1099 |
return; |
| 1100 |
} else if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 1101 |
ast_log(LOG_ERROR, "Contents of config file '%s' are invalid and cannot be parsed\n", filename); |
| 1102 |
return; |
| 1103 |
} |
| 1104 |
|
| 1105 |
while ((category = ast_category_browse_filtered(cfg, NULL, category, "type=^wizard$"))) { |
| 1106 |
const char *id = ast_category_get_name(category); |
| 1107 |
struct ast_category *last_cat = NULL; |
| 1108 |
int changes = 0; |
| 1109 |
|
| 1110 |
if (otw->last_config) { |
| 1111 |
last_cat = ast_category_get(otw->last_config, id, "type=^wizard$"); |
| 1112 |
changes = !ast_variable_lists_match(ast_category_first(category), ast_category_first(last_cat), 1); |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
if (!changes && reloaded && acl_change_detected) { |
| 1117 |
ast_debug(3, "Forcing update of wizard '%s' due to global ACL change.\n", id); |
| 1118 |
changes = 1; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (last_cat) { |
| 1122 |
ast_category_delete(otw->last_config, last_cat); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (!last_cat || changes) { |
| 1127 |
ast_debug(3, "%s: %s(s) for wizard '%s'\n", reloaded ? "Reload" : "Load", object_type, id); |
| 1128 |
if (wizard_apply_handler(sorcery, otw, category)) { |
| 1129 |
ast_log(LOG_ERROR, "Unable to create objects for wizard '%s'\n", id); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (!otw->last_config) { |
| 1135 |
otw->last_config = cfg; |
| 1136 |
return; |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
category = NULL; |
| 1143 |
while ((category = ast_category_browse_filtered(otw->last_config, NULL, category, "type=^wizard$"))) { |
| 1144 |
const char *id = ast_category_get_name(category); |
| 1145 |
struct ast_variable *search; |
| 1146 |
RAII_VAR(struct ao2_container *, existing, |
| 1147 |
ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL), ao2_cleanup); |
| 1148 |
|
| 1149 |
if (!existing) { |
| 1150 |
ast_log(LOG_ERROR, "Unable to allocate temporary container.\n"); |
| 1151 |
break; |
| 1152 |
} |
| 1153 |
|
| 1154 |
search = ast_variable_new("@pjsip_wizard", id, ""); |
| 1155 |
if (!search) { |
| 1156 |
ast_log(LOG_ERROR, "Unable to allocate memory for vaiable '@pjsip_wizard'.\n"); |
| 1157 |
break; |
| 1158 |
} |
| 1159 |
otw->wizard->retrieve_multiple(sorcery, otw->wizard_data, object_type, existing, search); |
| 1160 |
ast_variables_destroy(search); |
| 1161 |
|
| 1162 |
if (ao2_container_count(existing) > 0) { |
| 1163 |
ast_debug(3, "Delete on %s: %d %s(s) for wizard: %s\n", |
| 1164 |
reloaded ? "Reload" : "Load", ao2_container_count(existing), object_type, id); |
| 1165 |
ao2_callback(existing, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, |
| 1166 |
delete_existing_cb, otw); |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
ast_config_destroy(otw->last_config); |
| 1171 |
otw->last_config = cfg; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
static void object_type_registered_observer(const char *name, |
| 1176 |
struct ast_sorcery *sorcery, const char *object_type) |
| 1177 |
{ |
| 1178 |
struct ast_sorcery_wizard *wizard; |
| 1179 |
void *wizard_data; |
| 1180 |
struct object_type_wizard *otw; |
| 1181 |
|
| 1182 |
if (is_one_of(object_type, object_types)) { |
| 1183 |
if (ast_sorcery_object_type_apply_wizard(sorcery, object_type, |
| 1184 |
"memory", "pjsip_wizard", AST_SORCERY_WIZARD_APPLY_READONLY | AST_SORCERY_WIZARD_APPLY_ALLOW_DUPLICATE, |
| 1185 |
&wizard, &wizard_data) != AST_SORCERY_APPLY_SUCCESS) { |
| 1186 |
ast_log(LOG_ERROR, "Unable to apply sangoma wizard to object type '%s'\n", object_type); |
| 1187 |
return; |
| 1188 |
} |
| 1189 |
|
| 1190 |
otw = ast_malloc(sizeof(*otw) + strlen(object_type) + 1); |
| 1191 |
if (!otw) { |
| 1192 |
return; |
| 1193 |
} |
| 1194 |
|
| 1195 |
otw->sorcery = sorcery; |
| 1196 |
otw->wizard = wizard; |
| 1197 |
otw->wizard_data = wizard_data; |
| 1198 |
otw->last_config = NULL; |
| 1199 |
strcpy(otw->object_type, object_type); |
| 1200 |
AST_VECTOR_RW_WRLOCK(&object_type_wizards); |
| 1201 |
if (AST_VECTOR_APPEND(&object_type_wizards, otw)) { |
| 1202 |
ast_free(otw); |
| 1203 |
} else { |
| 1204 |
ast_debug(1, "Wizard mapped for object_type '%s'\n", object_type); |
| 1205 |
} |
| 1206 |
AST_VECTOR_RW_UNLOCK(&object_type_wizards); |
| 1207 |
|
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
static void instance_created_observer(const char *name, struct ast_sorcery *sorcery) |
| 1216 |
{ |
| 1217 |
if (strcmp(name, "res_pjsip")) { |
| 1218 |
return; |
| 1219 |
} |
| 1220 |
ast_module_ref(ast_module_info->self); |
| 1221 |
ast_sorcery_instance_observer_add(sorcery, &observer); |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
static void instance_destroying_observer(const char *name, struct ast_sorcery *sorcery) |
| 1228 |
{ |
| 1229 |
if (strcmp(name, "res_pjsip")) { |
| 1230 |
return; |
| 1231 |
} |
| 1232 |
|
| 1233 |
ast_sorcery_instance_observer_remove(sorcery, &observer); |
| 1234 |
ast_module_unref(ast_module_info->self); |
| 1235 |
} |
| 1236 |
|
| 1237 |
static char *handle_export_primitives(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1238 |
{ |
| 1239 |
struct ast_sorcery *sorcery; |
| 1240 |
int idx; |
| 1241 |
FILE *f = NULL; |
| 1242 |
const char *fn = NULL; |
| 1243 |
|
| 1244 |
switch (cmd) { |
| 1245 |
case CLI_INIT: |
| 1246 |
e->command = "pjsip export config_wizard primitives [to]"; |
| 1247 |
e->usage = |
| 1248 |
"Usage: pjsip export config_wizard primitives [ to <filename ]\n" |
| 1249 |
" Export the config_wizard objects as pjsip primitives to\n" |
| 1250 |
" the console or to <filename>\n"; |
| 1251 |
return NULL; |
| 1252 |
case CLI_GENERATE: |
| 1253 |
return NULL; |
| 1254 |
} |
| 1255 |
|
| 1256 |
if (a->argc > 5) { |
| 1257 |
char date[256]=""; |
| 1258 |
time_t t; |
| 1259 |
fn = a->argv[5]; |
| 1260 |
|
| 1261 |
time(&t); |
| 1262 |
ast_copy_string(date, ctime(&t), sizeof(date)); |
| 1263 |
f = fopen(fn, "w"); |
| 1264 |
if (!f) { |
| 1265 |
ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno)); |
| 1266 |
return CLI_FAILURE; |
| 1267 |
} |
| 1268 |
|
| 1269 |
fprintf(f, ";!\n"); |
| 1270 |
fprintf(f, ";! Automatically generated configuration file\n"); |
| 1271 |
fprintf(f, ";! Filename: %s\n", fn); |
| 1272 |
fprintf(f, ";! Generator: %s\n", "'pjsip export config_wizard primitives'"); |
| 1273 |
fprintf(f, ";! Creation Date: %s", date); |
| 1274 |
fprintf(f, ";!\n"); |
| 1275 |
} |
| 1276 |
|
| 1277 |
sorcery = ast_sip_get_sorcery(); |
| 1278 |
|
| 1279 |
AST_VECTOR_RW_RDLOCK(&object_type_wizards); |
| 1280 |
for(idx = 0; idx < AST_VECTOR_SIZE(&object_type_wizards); idx++) { |
| 1281 |
struct object_type_wizard *otw = AST_VECTOR_GET(&object_type_wizards, idx); |
| 1282 |
struct ao2_container *container; |
| 1283 |
struct ao2_iterator i; |
| 1284 |
void *o; |
| 1285 |
|
| 1286 |
container = ast_sorcery_retrieve_by_fields(sorcery, otw->object_type, AST_RETRIEVE_FLAG_MULTIPLE, NULL); |
| 1287 |
if (!container) { |
| 1288 |
continue; |
| 1289 |
} |
| 1290 |
|
| 1291 |
i = ao2_iterator_init(container, 0); |
| 1292 |
while ((o = ao2_iterator_next(&i))) { |
| 1293 |
struct ast_variable *vars; |
| 1294 |
struct ast_variable *v; |
| 1295 |
|
| 1296 |
vars = ast_sorcery_objectset_create(sorcery, o); |
| 1297 |
if (vars && ast_variable_find_in_list(vars, "@pjsip_wizard")) { |
| 1298 |
if (f) { |
| 1299 |
fprintf(f, "\n[%s]\ntype = %s\n", ast_sorcery_object_get_id(o), otw->object_type); |
| 1300 |
} else { |
| 1301 |
ast_cli(a->fd, "\n[%s]\ntype = %s\n", ast_sorcery_object_get_id(o), otw->object_type); |
| 1302 |
} |
| 1303 |
for (v = vars; v; v = v->next) { |
| 1304 |
if (!ast_strlen_zero(v->value)) { |
| 1305 |
if (f) { |
| 1306 |
fprintf(f, "%s = %s\n", v->name, v->value); |
| 1307 |
} else { |
| 1308 |
ast_cli(a->fd, "%s = %s\n", v->name, v->value); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
} |
| 1312 |
} |
| 1313 |
ast_variables_destroy(vars); |
| 1314 |
ao2_ref(o, -1); |
| 1315 |
} |
| 1316 |
ao2_iterator_destroy(&i); |
| 1317 |
ao2_cleanup(container); |
| 1318 |
} |
| 1319 |
AST_VECTOR_RW_UNLOCK(&object_type_wizards); |
| 1320 |
|
| 1321 |
if (f) { |
| 1322 |
fclose(f); |
| 1323 |
ast_cli(a->fd, "Wrote configuration to %s\n", fn); |
| 1324 |
} |
| 1325 |
|
| 1326 |
|
| 1327 |
return CLI_SUCCESS; |
| 1328 |
} |
| 1329 |
|
| 1330 |
static struct ast_cli_entry config_wizard_cli[] = { |
| 1331 |
AST_CLI_DEFINE(handle_export_primitives, "Export config wizard primitives"), |
| 1332 |
}; |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
static int reload_configuration_task(void *obj) |
| 1339 |
{ |
| 1340 |
struct ast_sorcery *sip_sorcery = ast_sip_get_sorcery(); |
| 1341 |
if (sip_sorcery) { |
| 1342 |
ast_sorcery_reload(sip_sorcery); |
| 1343 |
} |
| 1344 |
return 0; |
| 1345 |
} |
| 1346 |
|
| 1347 |
static int reload_module(void) |
| 1348 |
{ |
| 1349 |
if (ast_sip_push_task_wait_servant(NULL, reload_configuration_task, NULL)) { |
| 1350 |
ast_log(LOG_WARNING, "Failed to reload PJSIP\n"); |
| 1351 |
return -1; |
| 1352 |
} |
| 1353 |
|
| 1354 |
return 0; |
| 1355 |
} |
| 1356 |
|
| 1357 |
static int load_module(void) |
| 1358 |
{ |
| 1359 |
AST_VECTOR_RW_INIT(&object_type_wizards, 12); |
| 1360 |
ast_sorcery_global_observer_add(&global_observer); |
| 1361 |
ast_cli_register_multiple(config_wizard_cli, ARRAY_LEN(config_wizard_cli)); |
| 1362 |
|
| 1363 |
acl_change_sub = stasis_subscribe(ast_security_topic(), acl_change_stasis_cb, NULL); |
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
if (ast_sip_get_sorcery()) { |
| 1373 |
int i; |
| 1374 |
|
| 1375 |
ast_module_ref(ast_module_info->self); |
| 1376 |
ast_sorcery_instance_observer_add(ast_sip_get_sorcery(), &observer); |
| 1377 |
|
| 1378 |
|
| 1379 |
for (i = 0; i < ARRAY_LEN(object_types); ++i) { |
| 1380 |
if (!object_types[i]) { |
| 1381 |
break; |
| 1382 |
} else if (!ast_sorcery_get_object_type(ast_sip_get_sorcery(), object_types[i])) { |
| 1383 |
continue; |
| 1384 |
} |
| 1385 |
|
| 1386 |
object_type_registered_observer("res_pjsip", ast_sip_get_sorcery(), |
| 1387 |
object_types[i]); |
| 1388 |
} |
| 1389 |
|
| 1390 |
ast_sorcery_reload(ast_sip_get_sorcery()); |
| 1391 |
} |
| 1392 |
|
| 1393 |
return AST_MODULE_LOAD_SUCCESS; |
| 1394 |
} |
| 1395 |
|
| 1396 |
static int unload_module(void) |
| 1397 |
{ |
| 1398 |
if (acl_change_sub) { |
| 1399 |
acl_change_sub = stasis_unsubscribe_and_join(acl_change_sub); |
| 1400 |
} |
| 1401 |
|
| 1402 |
ast_cli_unregister_multiple(config_wizard_cli, ARRAY_LEN(config_wizard_cli)); |
| 1403 |
ast_sorcery_global_observer_remove(&global_observer); |
| 1404 |
AST_VECTOR_REMOVE_ALL_CMP_UNORDERED(&object_type_wizards, NULL, NOT_EQUALS, OTW_DELETE_CB); |
| 1405 |
AST_VECTOR_RW_FREE(&object_type_wizards); |
| 1406 |
|
| 1407 |
return 0; |
| 1408 |
} |
| 1409 |
|
| 1410 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP Config Wizard", |
| 1411 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1412 |
.load = load_module, |
| 1413 |
.reload = reload_module, |
| 1414 |
.unload = unload_module, |
| 1415 |
.load_pri = AST_MODPRI_REALTIME_DRIVER, |
| 1416 |
); |