| 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 |
#undef AST_MODULE |
| 33 |
#define AST_MODULE "acl" |
| 34 |
|
| 35 |
#include "asterisk.h" |
| 36 |
|
| 37 |
#include "asterisk/config.h" |
| 38 |
#include "asterisk/config_options.h" |
| 39 |
#include "asterisk/utils.h" |
| 40 |
#include "asterisk/module.h" |
| 41 |
#include "asterisk/cli.h" |
| 42 |
#include "asterisk/acl.h" |
| 43 |
#include "asterisk/astobj2.h" |
| 44 |
#include "asterisk/paths.h" |
| 45 |
#include "asterisk/stasis.h" |
| 46 |
#include "asterisk/json.h" |
| 47 |
#include "asterisk/security_events.h" |
| 48 |
|
| 49 |
#define NACL_CONFIG "acl.conf" |
| 50 |
#define ACL_FAMILY "acls" |
| 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 |
struct named_acl_config { |
| 83 |
struct ao2_container *named_acl_list; |
| 84 |
}; |
| 85 |
|
| 86 |
static AO2_GLOBAL_OBJ_STATIC(globals); |
| 87 |
|
| 88 |
|
| 89 |
static void *named_acl_config_alloc(void); |
| 90 |
static void *named_acl_alloc(const char *cat); |
| 91 |
static void *named_acl_find(struct ao2_container *container, const char *cat); |
| 92 |
|
| 93 |
|
| 94 |
static struct aco_type named_acl_type = { |
| 95 |
.type = ACO_ITEM, |
| 96 |
.name = "named_acl", |
| 97 |
.category_match = ACO_BLACKLIST_EXACT, |
| 98 |
.category = "general", |
| 99 |
.item_alloc = named_acl_alloc, |
| 100 |
.item_find = named_acl_find, |
| 101 |
.item_offset = offsetof(struct named_acl_config, named_acl_list), |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
struct aco_type *named_acl_types[] = ACO_TYPES(&named_acl_type); |
| 106 |
|
| 107 |
struct aco_file named_acl_conf = { |
| 108 |
.filename = "acl.conf", |
| 109 |
.types = ACO_TYPES(&named_acl_type), |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
CONFIG_INFO_CORE("named_acl", cfg_info, globals, named_acl_config_alloc, |
| 114 |
.files = ACO_FILES(&named_acl_conf), |
| 115 |
); |
| 116 |
|
| 117 |
struct named_acl { |
| 118 |
struct ast_ha *ha; |
| 119 |
char name[ACL_NAME_LENGTH]; |
| 120 |
}; |
| 121 |
|
| 122 |
AO2_STRING_FIELD_HASH_FN(named_acl, name) |
| 123 |
AO2_STRING_FIELD_CMP_FN(named_acl, name) |
| 124 |
|
| 125 |
|
| 126 |
static void named_acl_config_destructor(void *obj) |
| 127 |
{ |
| 128 |
struct named_acl_config *cfg = obj; |
| 129 |
ao2_cleanup(cfg->named_acl_list); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
static void *named_acl_config_alloc(void) |
| 136 |
{ |
| 137 |
struct named_acl_config *cfg; |
| 138 |
|
| 139 |
if (!(cfg = ao2_alloc(sizeof(*cfg), named_acl_config_destructor))) { |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
cfg->named_acl_list = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 37, |
| 144 |
named_acl_hash_fn, NULL, named_acl_cmp_fn); |
| 145 |
if (!cfg->named_acl_list) { |
| 146 |
goto error; |
| 147 |
} |
| 148 |
|
| 149 |
return cfg; |
| 150 |
|
| 151 |
error: |
| 152 |
ao2_ref(cfg, -1); |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
static void destroy_named_acl(void *obj) |
| 158 |
{ |
| 159 |
struct named_acl *named_acl = obj; |
| 160 |
ast_free_ha(named_acl->ha); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
static void *named_acl_alloc(const char *cat) |
| 171 |
{ |
| 172 |
struct named_acl *named_acl; |
| 173 |
|
| 174 |
named_acl = ao2_alloc(sizeof(*named_acl), destroy_named_acl); |
| 175 |
if (!named_acl) { |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
|
| 179 |
ast_copy_string(named_acl->name, cat, sizeof(named_acl->name)); |
| 180 |
|
| 181 |
return named_acl; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
static void *named_acl_find(struct ao2_container *container, const char *cat) |
| 192 |
{ |
| 193 |
struct named_acl tmp; |
| 194 |
ast_copy_string(tmp.name, cat, sizeof(tmp.name)); |
| 195 |
return ao2_find(container, &tmp, OBJ_POINTER); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
static int acl_order_comparator(struct ast_category *p, struct ast_category *q) |
| 211 |
{ |
| 212 |
int p_value = 0, q_value = 0; |
| 213 |
struct ast_variable *p_var = ast_category_first(p); |
| 214 |
struct ast_variable *q_var = ast_category_first(q); |
| 215 |
|
| 216 |
while (p_var) { |
| 217 |
if (!strcasecmp(p_var->name, "rule_order")) { |
| 218 |
p_value = atoi(p_var->value); |
| 219 |
break; |
| 220 |
} |
| 221 |
p_var = p_var->next; |
| 222 |
} |
| 223 |
|
| 224 |
while (q_var) { |
| 225 |
if (!strcasecmp(q_var->name, "rule_order")) { |
| 226 |
q_value = atoi(q_var->value); |
| 227 |
break; |
| 228 |
} |
| 229 |
q_var = q_var->next; |
| 230 |
} |
| 231 |
|
| 232 |
if (p_value < q_value) { |
| 233 |
return -1; |
| 234 |
} else if (q_value < p_value) { |
| 235 |
return 1; |
| 236 |
} |
| 237 |
|
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
static struct named_acl *named_acl_find_realtime(const char *name) |
| 250 |
{ |
| 251 |
struct ast_config *cfg; |
| 252 |
char *item = NULL; |
| 253 |
const char *systemname = NULL; |
| 254 |
struct ast_ha *built_ha = NULL; |
| 255 |
struct named_acl *acl; |
| 256 |
|
| 257 |
|
| 258 |
systemname = ast_config_AST_SYSTEM_NAME; |
| 259 |
|
| 260 |
if (ast_strlen_zero(systemname)) { |
| 261 |
cfg = ast_load_realtime_multientry(ACL_FAMILY, "name", name, SENTINEL); |
| 262 |
} else { |
| 263 |
cfg = ast_load_realtime_multientry(ACL_FAMILY, "name", name, "systemname", systemname, SENTINEL); |
| 264 |
} |
| 265 |
|
| 266 |
if (!cfg) { |
| 267 |
return NULL; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
ast_config_sort_categories(cfg, 0, acl_order_comparator); |
| 272 |
|
| 273 |
while ((item = ast_category_browse(cfg, item))) { |
| 274 |
int append_ha_error = 0; |
| 275 |
const char *order = ast_variable_retrieve(cfg, item, "rule_order"); |
| 276 |
const char *sense = ast_variable_retrieve(cfg, item, "sense"); |
| 277 |
const char *rule = ast_variable_retrieve(cfg, item, "rule"); |
| 278 |
|
| 279 |
built_ha = ast_append_ha(sense, rule, built_ha, &append_ha_error); |
| 280 |
if (append_ha_error) { |
| 281 |
|
| 282 |
ast_log(LOG_ERROR, "Rejecting realtime ACL due to bad ACL definition '%s': %s - %s - %s\n", name, order, sense, rule); |
| 283 |
ast_free_ha(built_ha); |
| 284 |
return NULL; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
ast_config_destroy(cfg); |
| 289 |
|
| 290 |
acl = named_acl_alloc(name); |
| 291 |
if (!acl) { |
| 292 |
ast_log(LOG_ERROR, "allocation error\n"); |
| 293 |
ast_free_ha(built_ha); |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
|
| 297 |
acl->ha = built_ha; |
| 298 |
|
| 299 |
return acl; |
| 300 |
} |
| 301 |
|
| 302 |
struct ast_ha *ast_named_acl_find(const char *name, int *is_realtime, int *is_undefined) |
| 303 |
{ |
| 304 |
struct ast_ha *ha = NULL; |
| 305 |
|
| 306 |
RAII_VAR(struct named_acl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup); |
| 307 |
RAII_VAR(struct named_acl *, named_acl, NULL, ao2_cleanup); |
| 308 |
|
| 309 |
if (is_realtime) { |
| 310 |
*is_realtime = 0; |
| 311 |
} |
| 312 |
|
| 313 |
if (is_undefined) { |
| 314 |
*is_undefined = 0; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
if ((!cfg) || (!(cfg->named_acl_list))) { |
| 319 |
ast_log(LOG_ERROR, "Attempted to find named ACL '%s', but the ACL configuration isn't available.\n", name); |
| 320 |
return NULL; |
| 321 |
} |
| 322 |
|
| 323 |
named_acl = named_acl_find(cfg->named_acl_list, name); |
| 324 |
|
| 325 |
|
| 326 |
if (!named_acl) { |
| 327 |
RAII_VAR(struct named_acl *, realtime_acl, NULL, ao2_cleanup); |
| 328 |
|
| 329 |
|
| 330 |
if ((realtime_acl = named_acl_find_realtime(name))) { |
| 331 |
if (is_realtime) { |
| 332 |
*is_realtime = 1; |
| 333 |
} |
| 334 |
ha = ast_duplicate_ha_list(realtime_acl->ha); |
| 335 |
return ha; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
if (ast_realtime_is_mapping_defined(ACL_FAMILY) && !ast_check_realtime(ACL_FAMILY)) { |
| 340 |
ast_log(LOG_WARNING, "ACL '%s' does not exist. The ACL will be marked as undefined and will automatically fail if applied.\n" |
| 341 |
"This ACL may exist in the configured realtime backend, but that backend hasn't been registered yet. " |
| 342 |
"Fix this establishing preload for the backend in 'modules.conf'.\n", name); |
| 343 |
} else { |
| 344 |
ast_log(LOG_WARNING, "ACL '%s' does not exist. The ACL will be marked as undefined and will automatically fail if applied.\n", name); |
| 345 |
} |
| 346 |
|
| 347 |
if (is_undefined) { |
| 348 |
*is_undefined = 1; |
| 349 |
} |
| 350 |
|
| 351 |
return NULL; |
| 352 |
} |
| 353 |
|
| 354 |
ha = ast_duplicate_ha_list(named_acl->ha); |
| 355 |
|
| 356 |
if (!ha) { |
| 357 |
ast_log(LOG_NOTICE, "ACL '%s' contains no rules. It is valid, but it will accept addresses unconditionally.\n", name); |
| 358 |
} |
| 359 |
|
| 360 |
return ha; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
STASIS_MESSAGE_TYPE_DEFN(ast_named_acl_change_type); |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
static int publish_acl_change(const char *name) |
| 381 |
{ |
| 382 |
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); |
| 383 |
RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup); |
| 384 |
RAII_VAR(struct ast_json *, json_object, ast_json_object_create(), ast_json_unref); |
| 385 |
|
| 386 |
if (!json_object || !ast_named_acl_change_type()) { |
| 387 |
goto publish_failure; |
| 388 |
} |
| 389 |
|
| 390 |
if (ast_json_object_set(json_object, "name", ast_json_string_create(name))) { |
| 391 |
goto publish_failure; |
| 392 |
} |
| 393 |
|
| 394 |
if (!(json_payload = ast_json_payload_create(json_object))) { |
| 395 |
goto publish_failure; |
| 396 |
} |
| 397 |
|
| 398 |
msg = stasis_message_create(ast_named_acl_change_type(), json_payload); |
| 399 |
|
| 400 |
if (!msg) { |
| 401 |
goto publish_failure; |
| 402 |
} |
| 403 |
|
| 404 |
stasis_publish(ast_security_topic(), msg); |
| 405 |
|
| 406 |
return 0; |
| 407 |
|
| 408 |
publish_failure: |
| 409 |
ast_log(LOG_ERROR, "Failed to issue ACL change message for %s.\n", |
| 410 |
ast_strlen_zero(name) ? "all named ACLs" : name); |
| 411 |
return -1; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
static void cli_display_named_acl(int fd, const char *name) |
| 422 |
{ |
| 423 |
int is_realtime = 0; |
| 424 |
|
| 425 |
RAII_VAR(struct named_acl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup); |
| 426 |
RAII_VAR(struct named_acl *, named_acl, NULL, ao2_cleanup); |
| 427 |
|
| 428 |
|
| 429 |
if ((!cfg) || (!cfg->named_acl_list)) { |
| 430 |
ast_log(LOG_ERROR, "Attempted to show named ACL '%s', but the acl configuration isn't available.\n", name); |
| 431 |
return; |
| 432 |
} |
| 433 |
|
| 434 |
named_acl = named_acl_find(cfg->named_acl_list, name); |
| 435 |
|
| 436 |
|
| 437 |
if (!named_acl) { |
| 438 |
if (!(named_acl = named_acl_find_realtime(name))) { |
| 439 |
ast_cli(fd, "\nCould not find ACL named '%s'\n", name); |
| 440 |
return; |
| 441 |
} |
| 442 |
|
| 443 |
is_realtime = 1; |
| 444 |
} |
| 445 |
|
| 446 |
ast_cli(fd, "\nACL: %s%s\n---------------------------------------------\n", name, is_realtime ? " (realtime)" : ""); |
| 447 |
ast_ha_output(fd, named_acl->ha, NULL); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
static void cli_display_named_acl_list(int fd) |
| 457 |
{ |
| 458 |
struct ao2_iterator i; |
| 459 |
void *o; |
| 460 |
RAII_VAR(struct named_acl_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup); |
| 461 |
|
| 462 |
ast_cli(fd, "\nacl\n---\n"); |
| 463 |
|
| 464 |
if (!cfg || !cfg->named_acl_list) { |
| 465 |
ast_cli(fd, "ACL configuration isn't available.\n"); |
| 466 |
return; |
| 467 |
} |
| 468 |
i = ao2_iterator_init(cfg->named_acl_list, 0); |
| 469 |
|
| 470 |
while ((o = ao2_iterator_next(&i))) { |
| 471 |
struct named_acl *named_acl = o; |
| 472 |
ast_cli(fd, "%s\n", named_acl->name); |
| 473 |
ao2_ref(o, -1); |
| 474 |
} |
| 475 |
|
| 476 |
ao2_iterator_destroy(&i); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
static char *handle_show_named_acl_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 481 |
{ |
| 482 |
struct named_acl_config *cfg; |
| 483 |
int length; |
| 484 |
struct ao2_iterator i; |
| 485 |
struct named_acl *named_acl; |
| 486 |
|
| 487 |
switch (cmd) { |
| 488 |
case CLI_INIT: |
| 489 |
e->command = "acl show"; |
| 490 |
e->usage = |
| 491 |
"Usage: acl show [name]\n" |
| 492 |
" Shows a list of named ACLs or lists all entries in a given named ACL.\n"; |
| 493 |
return NULL; |
| 494 |
case CLI_GENERATE: |
| 495 |
if (a->pos != 2) { |
| 496 |
return NULL; |
| 497 |
} |
| 498 |
|
| 499 |
cfg = ao2_global_obj_ref(globals); |
| 500 |
if (!cfg) { |
| 501 |
return NULL; |
| 502 |
} |
| 503 |
length = strlen(a->word); |
| 504 |
i = ao2_iterator_init(cfg->named_acl_list, 0); |
| 505 |
while ((named_acl = ao2_iterator_next(&i))) { |
| 506 |
if (!strncasecmp(a->word, named_acl->name, length)) { |
| 507 |
if (ast_cli_completion_add(ast_strdup(named_acl->name))) { |
| 508 |
ao2_ref(named_acl, -1); |
| 509 |
break; |
| 510 |
} |
| 511 |
} |
| 512 |
ao2_ref(named_acl, -1); |
| 513 |
} |
| 514 |
ao2_iterator_destroy(&i); |
| 515 |
ao2_ref(cfg, -1); |
| 516 |
|
| 517 |
return NULL; |
| 518 |
} |
| 519 |
|
| 520 |
if (a->argc == 2) { |
| 521 |
cli_display_named_acl_list(a->fd); |
| 522 |
return CLI_SUCCESS; |
| 523 |
} |
| 524 |
|
| 525 |
if (a->argc == 3) { |
| 526 |
cli_display_named_acl(a->fd, a->argv[2]); |
| 527 |
return CLI_SUCCESS; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
return CLI_SHOWUSAGE; |
| 532 |
} |
| 533 |
|
| 534 |
static struct ast_cli_entry cli_named_acl[] = { |
| 535 |
AST_CLI_DEFINE(handle_show_named_acl_cmd, "Show a named ACL or list all named ACLs"), |
| 536 |
}; |
| 537 |
|
| 538 |
static int reload_module(void) |
| 539 |
{ |
| 540 |
enum aco_process_status status; |
| 541 |
|
| 542 |
status = aco_process_config(&cfg_info, 1); |
| 543 |
|
| 544 |
if (status == ACO_PROCESS_ERROR) { |
| 545 |
ast_log(LOG_WARNING, "Could not reload ACL config\n"); |
| 546 |
return 0; |
| 547 |
} |
| 548 |
|
| 549 |
if (status == ACO_PROCESS_UNCHANGED) { |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
publish_acl_change(""); |
| 558 |
|
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
static int unload_module(void) |
| 563 |
{ |
| 564 |
ast_cli_unregister_multiple(cli_named_acl, ARRAY_LEN(cli_named_acl)); |
| 565 |
|
| 566 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_named_acl_change_type); |
| 567 |
aco_info_destroy(&cfg_info); |
| 568 |
ao2_global_obj_release(globals); |
| 569 |
|
| 570 |
return 0; |
| 571 |
} |
| 572 |
|
| 573 |
static int load_module(void) |
| 574 |
{ |
| 575 |
if (aco_info_init(&cfg_info)) { |
| 576 |
return AST_MODULE_LOAD_FAILURE; |
| 577 |
} |
| 578 |
|
| 579 |
STASIS_MESSAGE_TYPE_INIT(ast_named_acl_change_type); |
| 580 |
|
| 581 |
|
| 582 |
aco_option_register(&cfg_info, "permit", ACO_EXACT, named_acl_types, NULL, OPT_ACL_T, 1, FLDSET(struct named_acl, ha)); |
| 583 |
aco_option_register(&cfg_info, "deny", ACO_EXACT, named_acl_types, NULL, OPT_ACL_T, 0, FLDSET(struct named_acl, ha)); |
| 584 |
|
| 585 |
aco_process_config(&cfg_info, 0); |
| 586 |
|
| 587 |
ast_cli_register_multiple(cli_named_acl, ARRAY_LEN(cli_named_acl)); |
| 588 |
|
| 589 |
return AST_MODULE_LOAD_SUCCESS; |
| 590 |
} |
| 591 |
|
| 592 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Named ACL system", |
| 593 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 594 |
.load = load_module, |
| 595 |
.unload = unload_module, |
| 596 |
.reload = reload_module, |
| 597 |
.load_pri = AST_MODPRI_CORE, |
| 598 |
.requires = "extconfig", |
| 599 |
); |