| 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 <limits.h> |
| 26 |
|
| 27 |
#include "asterisk.h" |
| 28 |
|
| 29 |
#include "asterisk/sorcery.h" |
| 30 |
#include "asterisk/config_options.h" |
| 31 |
#include "asterisk/http_websocket.h" |
| 32 |
#include "asterisk/websocket_client.h" |
| 33 |
#include "asterisk/app.h" |
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/vector.h" |
| 36 |
#include "internal.h" |
| 37 |
|
| 38 |
static struct ast_sorcery *sorcery; |
| 39 |
|
| 40 |
struct outbound_websocket_state { |
| 41 |
enum ari_conf_owc_fields invalid_fields; |
| 42 |
char id[0]; |
| 43 |
}; |
| 44 |
|
| 45 |
#define OWC_STATES_BUCKETS 13 |
| 46 |
struct ao2_container *owc_states = NULL; |
| 47 |
|
| 48 |
static void outbound_websocket_dtor(void *obj) |
| 49 |
{ |
| 50 |
struct ari_conf_outbound_websocket *owc = obj; |
| 51 |
|
| 52 |
ast_debug(3, "%s: Disposing of outbound websocket config\n", |
| 53 |
ast_sorcery_object_get_id(owc)); |
| 54 |
ast_string_field_free_memory(owc); |
| 55 |
ao2_cleanup(owc->websocket_client); |
| 56 |
owc->websocket_client = NULL; |
| 57 |
} |
| 58 |
|
| 59 |
static void *outbound_websocket_alloc(const char *id) |
| 60 |
{ |
| 61 |
struct ari_conf_outbound_websocket *owc = NULL; |
| 62 |
|
| 63 |
owc = ast_sorcery_generic_alloc(sizeof(*owc), outbound_websocket_dtor); |
| 64 |
if (!owc) { |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
if (ast_string_field_init(owc, 1024) != 0) { |
| 69 |
ao2_cleanup(owc); |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
ast_debug(2, "%s: Allocated outbound websocket config\n", id); |
| 74 |
return owc; |
| 75 |
} |
| 76 |
|
| 77 |
static int outbound_websocket_websocket_client_id_from_str(const struct aco_option *opt, |
| 78 |
struct ast_variable *var, void *obj) |
| 79 |
{ |
| 80 |
struct ari_conf_outbound_websocket *owc = obj; |
| 81 |
|
| 82 |
if (ast_strlen_zero(var->value)) { |
| 83 |
ast_log(LOG_ERROR, "%s: Outbound websocket missing websocket client id\n", |
| 84 |
ast_sorcery_object_get_id(owc)); |
| 85 |
return -1; |
| 86 |
} |
| 87 |
owc->websocket_client = ast_websocket_client_retrieve_by_id(var->value); |
| 88 |
if (!owc->websocket_client) { |
| 89 |
ast_log(LOG_ERROR, "%s: Outbound websocket invalid websocket client id '%s'\n", |
| 90 |
ast_sorcery_object_get_id(owc), var->value); |
| 91 |
return -1; |
| 92 |
} |
| 93 |
if (ast_string_field_set(owc, websocket_client_id, var->value) != 0) { |
| 94 |
return -1; |
| 95 |
} |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
static int outbound_websocket_websocket_client_id_to_str(const void *obj, const intptr_t *args, char **buf) |
| 100 |
{ |
| 101 |
const struct ari_conf_outbound_websocket *owc = obj; |
| 102 |
if (!owc->websocket_client) { |
| 103 |
return -1; |
| 104 |
} |
| 105 |
*buf = ast_strdup(owc->websocket_client_id); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
static int outbound_websocket_apply(const struct ast_sorcery *sorcery, void *obj) |
| 115 |
{ |
| 116 |
struct ari_conf_outbound_websocket *owc = obj; |
| 117 |
const char *id = ast_sorcery_object_get_id(owc); |
| 118 |
int res = 0; |
| 119 |
|
| 120 |
ast_debug(3, "%s: Initializing outbound websocket\n", id); |
| 121 |
|
| 122 |
if (!owc->websocket_client) { |
| 123 |
ast_log(LOG_WARNING, "%s: Outbound websocket missing websocket_client_id\n", id); |
| 124 |
res = -1; |
| 125 |
} |
| 126 |
|
| 127 |
if (ast_strlen_zero(owc->apps)) { |
| 128 |
ast_log(LOG_WARNING, "%s: Outbound websocket missing apps\n", id); |
| 129 |
res = -1; |
| 130 |
} else { |
| 131 |
char *apps = ast_strdupa(owc->apps); |
| 132 |
char *app; |
| 133 |
while ((app = ast_strsep(&apps, ',', AST_STRSEP_STRIP))) { |
| 134 |
|
| 135 |
if (ast_strlen_zero(app)) { |
| 136 |
ast_log(LOG_WARNING, "%s: Outbound websocket has empty app\n", id); |
| 137 |
res = -1; |
| 138 |
} |
| 139 |
if (strlen(app) > ARI_MAX_APP_NAME_LEN) { |
| 140 |
ast_log(LOG_WARNING, "%s: Outbound websocket app '%s' > %d characters\n", |
| 141 |
id, app, (int)ARI_MAX_APP_NAME_LEN); |
| 142 |
res = -1; |
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (ast_strlen_zero(owc->local_ari_user)) { |
| 149 |
ast_log(LOG_WARNING, "%s: Outbound websocket missing local_ari_user\n", id); |
| 150 |
res = -1; |
| 151 |
} |
| 152 |
|
| 153 |
if (res != 0) { |
| 154 |
ast_log(LOG_WARNING, "%s: Outbound websocket configuration failed\n", id); |
| 155 |
} else { |
| 156 |
ast_debug(3, "%s: Outbound websocket configuration succeeded\n", id); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
return res; |
| 161 |
} |
| 162 |
|
| 163 |
enum ari_conf_owc_fields ari_conf_owc_get_invalid_fields(const char *id) |
| 164 |
{ |
| 165 |
RAII_VAR(struct outbound_websocket_state *, state, NULL, ao2_cleanup); |
| 166 |
|
| 167 |
state = ao2_find(owc_states, id, OBJ_SEARCH_KEY); |
| 168 |
return state ? state->invalid_fields : ARI_OWC_FIELD_NONE; |
| 169 |
} |
| 170 |
|
| 171 |
static int outbound_websocket_validate_cb(void *obj, void *args, int flags) |
| 172 |
{ |
| 173 |
struct ari_conf_outbound_websocket *owc = obj; |
| 174 |
struct ari_conf_outbound_websocket *other_owc = NULL; |
| 175 |
RAII_VAR(struct ao2_container *, owcs, NULL, ao2_cleanup); |
| 176 |
struct ao2_iterator it; |
| 177 |
const char *id = ast_sorcery_object_get_id(owc); |
| 178 |
struct ast_vector_string apps = { 0, }; |
| 179 |
struct ari_conf_user *user = NULL; |
| 180 |
struct outbound_websocket_state *state = NULL; |
| 181 |
int res = 0; |
| 182 |
|
| 183 |
ast_debug(2, "%s: Validating outbound websocket\n", id); |
| 184 |
|
| 185 |
owcs = ari_conf_get_owcs(); |
| 186 |
if (!owcs || ao2_container_count(owcs) == 0) { |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
if (AST_VECTOR_INIT(&apps, 5) != 0) { |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
res = ast_vector_string_split(&apps, owc->apps, ",", 0, NULL); |
| 195 |
if (res != 0) { |
| 196 |
ast_log(LOG_WARNING, "%s: Outbound websocket apps '%s' failed to split\n", |
| 197 |
id, owc->apps); |
| 198 |
AST_VECTOR_RESET(&apps, ast_free_ptr); |
| 199 |
AST_VECTOR_FREE(&apps); |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
state = ao2_find(owc_states, id, OBJ_SEARCH_KEY); |
| 204 |
if (!state) { |
| 205 |
state = ao2_alloc(sizeof(*state) + strlen(id) + 1, NULL); |
| 206 |
if (!state) { |
| 207 |
ast_log(LOG_WARNING, "%s: Outbound websocket state allocation failed\n", id); |
| 208 |
AST_VECTOR_RESET(&apps, ast_free_ptr); |
| 209 |
AST_VECTOR_FREE(&apps); |
| 210 |
return 0; |
| 211 |
} |
| 212 |
strcpy(state->id, id); |
| 213 |
ast_debug(3, "%s: Created new outbound websocket state\n", id); |
| 214 |
} else { |
| 215 |
ast_debug(3, "%s: Outbound websocket state already exists\n", id); |
| 216 |
} |
| 217 |
state->invalid_fields = ARI_OWC_FIELD_NONE; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
it = ao2_iterator_init(owcs, 0); |
| 224 |
while ((other_owc = ao2_iterator_next(&it))) { |
| 225 |
const char *other_id = ast_sorcery_object_get_id(other_owc); |
| 226 |
if (!ast_strings_equal(other_id, id)) { |
| 227 |
int i = 0; |
| 228 |
for (i = 0; i < AST_VECTOR_SIZE(&apps); i++) { |
| 229 |
const char *app = AST_VECTOR_GET(&apps, i); |
| 230 |
if (ast_in_delimited_string(app, other_owc->apps, ',')) { |
| 231 |
ast_log(LOG_WARNING, |
| 232 |
"%s: Outbound websocket '%s' is also trying to register app '%s'\n", |
| 233 |
id, other_id, app); |
| 234 |
state->invalid_fields |= ARI_OWC_FIELD_APPS; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
ao2_cleanup(other_owc); |
| 239 |
if (owc->invalid) { |
| 240 |
break; |
| 241 |
} |
| 242 |
} |
| 243 |
ao2_iterator_destroy(&it); |
| 244 |
AST_VECTOR_RESET(&apps, ast_free_ptr); |
| 245 |
AST_VECTOR_FREE(&apps); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
user = ast_sorcery_retrieve_by_id(sorcery, "user", owc->local_ari_user); |
| 252 |
if (!user) { |
| 253 |
ast_log(LOG_WARNING, "%s: Outbound websocket ARI user '%s' not found\n", |
| 254 |
id, owc->local_ari_user); |
| 255 |
state->invalid_fields |= ARI_OWC_FIELD_LOCAL_ARI_USER; |
| 256 |
} else { |
| 257 |
if (user->password_format != ARI_PASSWORD_FORMAT_PLAIN) { |
| 258 |
ast_log(LOG_WARNING, "%s: Outbound websocket ARI user '%s' password MUST be plain text\n", |
| 259 |
id, owc->local_ari_user); |
| 260 |
state->invalid_fields |= ARI_OWC_FIELD_LOCAL_ARI_USER; |
| 261 |
} |
| 262 |
if (ast_string_field_set(owc, local_ari_password, user->password) != 0) { |
| 263 |
state->invalid_fields |= ARI_OWC_FIELD_LOCAL_ARI_USER; |
| 264 |
} |
| 265 |
} |
| 266 |
ao2_cleanup(user); |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
ao2_link(owc_states, state); |
| 273 |
ao2_cleanup(state); |
| 274 |
|
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
static int outbound_websocket_state_cleanup(void *obj, void *arg, int flags) |
| 279 |
{ |
| 280 |
struct outbound_websocket_state *state = obj; |
| 281 |
struct ari_conf_outbound_websocket *owc = ari_conf_get_owc(state->id); |
| 282 |
int res = 0; |
| 283 |
|
| 284 |
if (!owc) { |
| 285 |
ast_debug(3, "%s: Cleaning up orphaned outbound websocket state\n", state->id); |
| 286 |
res = CMP_MATCH; |
| 287 |
} |
| 288 |
ao2_cleanup(owc); |
| 289 |
|
| 290 |
return res; |
| 291 |
} |
| 292 |
|
| 293 |
static void outbound_websockets_validate(const char *name) |
| 294 |
{ |
| 295 |
RAII_VAR(struct ao2_container *, owcs, ari_conf_get_owcs(), ao2_cleanup); |
| 296 |
|
| 297 |
ao2_callback(owcs, OBJ_NODATA, outbound_websocket_validate_cb, NULL); |
| 298 |
|
| 299 |
ao2_callback(owc_states, OBJ_NODATA | OBJ_UNLINK, |
| 300 |
outbound_websocket_state_cleanup, NULL); |
| 301 |
} |
| 302 |
|
| 303 |
struct ao2_container *ari_conf_get_owcs(void) |
| 304 |
{ |
| 305 |
if (!sorcery) { |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
return ast_sorcery_retrieve_by_fields(sorcery, "outbound_websocket", |
| 310 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 311 |
} |
| 312 |
|
| 313 |
struct ari_conf_outbound_websocket *ari_conf_get_owc(const char *id) |
| 314 |
{ |
| 315 |
if (!sorcery) { |
| 316 |
return NULL; |
| 317 |
} |
| 318 |
|
| 319 |
return ast_sorcery_retrieve_by_id(sorcery, "outbound_websocket", id); |
| 320 |
} |
| 321 |
|
| 322 |
struct ari_conf_outbound_websocket *ari_conf_get_owc_for_app( |
| 323 |
const char *app_name, unsigned int ws_type) |
| 324 |
{ |
| 325 |
struct ari_conf_outbound_websocket *owc = NULL; |
| 326 |
struct ao2_container *owcs = NULL; |
| 327 |
struct ao2_iterator i; |
| 328 |
|
| 329 |
if (ast_strlen_zero(app_name)) { |
| 330 |
return NULL; |
| 331 |
} |
| 332 |
|
| 333 |
ast_debug(3, "Checking outbound websockets for app '%s'\n", app_name); |
| 334 |
|
| 335 |
owcs = ari_conf_get_owcs(); |
| 336 |
if (!owcs || ao2_container_count(owcs) == 0) { |
| 337 |
ast_debug(3, "No outbound websockets found\n"); |
| 338 |
return NULL; |
| 339 |
} |
| 340 |
|
| 341 |
i = ao2_iterator_init(owcs, 0); |
| 342 |
while ((owc = ao2_iterator_next(&i))) { |
| 343 |
const char *id = ast_sorcery_object_get_id(owc); |
| 344 |
|
| 345 |
ast_debug(3, "%s: Checking outbound websocket apps '%s' for app '%s'\n", |
| 346 |
id, owc->apps, app_name); |
| 347 |
if (owc->websocket_client->connection_type & ws_type |
| 348 |
&& ast_in_delimited_string(app_name, owc->apps, ',')) { |
| 349 |
ast_debug(3, "%s: Found correct websocket type for apps '%s' for app '%s'\n", |
| 350 |
id, owc->apps, app_name); |
| 351 |
break; |
| 352 |
} |
| 353 |
ao2_cleanup(owc); |
| 354 |
} |
| 355 |
ao2_iterator_destroy(&i); |
| 356 |
ao2_cleanup(owcs); |
| 357 |
if (!owc) { |
| 358 |
ast_debug(3, "No outbound websocket found for app '%s'\n", app_name); |
| 359 |
} |
| 360 |
|
| 361 |
return owc; |
| 362 |
} |
| 363 |
|
| 364 |
const char *ari_websocket_type_to_str(enum ast_websocket_type type) |
| 365 |
{ |
| 366 |
switch (type) { |
| 367 |
case AST_WS_TYPE_CLIENT_PERSISTENT: |
| 368 |
return "persistent"; |
| 369 |
case AST_WS_TYPE_CLIENT_PER_CALL: |
| 370 |
return "per_call"; |
| 371 |
case AST_WS_TYPE_CLIENT_PER_CALL_CONFIG: |
| 372 |
return "per_call_config"; |
| 373 |
case AST_WS_TYPE_INBOUND: |
| 374 |
return "inbound"; |
| 375 |
case AST_WS_TYPE_ANY: |
| 376 |
return "any"; |
| 377 |
default: |
| 378 |
return "unknown"; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
enum ari_conf_owc_fields ari_conf_owc_detect_changes( |
| 383 |
struct ari_conf_outbound_websocket *old_owc, |
| 384 |
struct ari_conf_outbound_websocket *new_owc) |
| 385 |
{ |
| 386 |
enum ari_conf_owc_fields changed = ARI_OWC_FIELD_NONE; |
| 387 |
const char *new_id = ast_sorcery_object_get_id(new_owc); |
| 388 |
RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy); |
| 389 |
struct ast_variable *v = NULL; |
| 390 |
int res = 0; |
| 391 |
int changes_found = 0; |
| 392 |
|
| 393 |
ast_debug(2, "%s: Detecting changes\n", new_id); |
| 394 |
|
| 395 |
res = ast_sorcery_diff(sorcery, old_owc, new_owc, &changes); |
| 396 |
if (res != 0) { |
| 397 |
ast_log(LOG_WARNING, "%s: Failed to create changeset\n", new_id); |
| 398 |
return ARI_OWC_FIELD_NONE; |
| 399 |
} |
| 400 |
|
| 401 |
for (v = changes; v; v = v->next) { |
| 402 |
changes_found = 1; |
| 403 |
ast_debug(2, "%s: %s changed to %s\n", new_id, v->name, v->value); |
| 404 |
if (ast_strings_equal(v->name, "apps")) { |
| 405 |
changed |= ARI_OWC_FIELD_APPS; |
| 406 |
} else if (ast_strings_equal(v->name, "subscribe_all")) { |
| 407 |
changed |= ARI_OWC_FIELD_SUBSCRIBE_ALL; |
| 408 |
} else if (ast_strings_equal(v->name, "local_ari_user")) { |
| 409 |
changed |= ARI_OWC_FIELD_LOCAL_ARI_USER; |
| 410 |
} else if (ast_strings_equal(v->name, "local_ari_password")) { |
| 411 |
changed |= ARI_OWC_FIELD_LOCAL_ARI_PASSWORD; |
| 412 |
} else { |
| 413 |
ast_debug(2, "%s: Unknown change %s\n", new_id, v->name); |
| 414 |
} |
| 415 |
} |
| 416 |
if (!changes_found) { |
| 417 |
ast_debug(2, "%s: No changes found %p %p\n", new_id, |
| 418 |
old_owc->websocket_client,new_owc->websocket_client); |
| 419 |
} |
| 420 |
|
| 421 |
changed |= ast_websocket_client_get_field_diff( |
| 422 |
old_owc->websocket_client, new_owc->websocket_client); |
| 423 |
|
| 424 |
return changed; |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
static void general_dtor(void *obj) |
| 430 |
{ |
| 431 |
struct ari_conf_general *cfg = obj; |
| 432 |
|
| 433 |
ast_string_field_free_memory(cfg); |
| 434 |
} |
| 435 |
|
| 436 |
static void *general_alloc(const char *name) |
| 437 |
{ |
| 438 |
struct ari_conf_general *general = ast_sorcery_generic_alloc( |
| 439 |
sizeof(*general), general_dtor); |
| 440 |
|
| 441 |
if (!general) { |
| 442 |
return NULL; |
| 443 |
} |
| 444 |
|
| 445 |
if (ast_string_field_init(general, 64) != 0) { |
| 446 |
return NULL; |
| 447 |
} |
| 448 |
|
| 449 |
return general; |
| 450 |
} |
| 451 |
|
| 452 |
#define MAX_VARS 128 |
| 453 |
|
| 454 |
static int general_apply(const struct ast_sorcery *sorcery, void *obj) |
| 455 |
{ |
| 456 |
struct ari_conf_general *general = obj; |
| 457 |
char *parse = NULL; |
| 458 |
AST_DECLARE_APP_ARGS(args, |
| 459 |
AST_APP_ARG(vars)[MAX_VARS]; |
| 460 |
); |
| 461 |
|
| 462 |
ast_debug(2, "Initializing general config\n"); |
| 463 |
|
| 464 |
parse = ast_strdupa(general->channelvars); |
| 465 |
AST_STANDARD_APP_ARGS(args, parse); |
| 466 |
|
| 467 |
ast_channel_set_ari_vars(args.argc, args.vars); |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
static int general_pretty_from_str(const struct aco_option *opt, |
| 473 |
struct ast_variable *var, void *obj) |
| 474 |
{ |
| 475 |
struct ari_conf_general *general = obj; |
| 476 |
|
| 477 |
general->format = ast_true(var->value) ? AST_JSON_PRETTY : AST_JSON_COMPACT; |
| 478 |
|
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
struct ari_conf_general* ari_conf_get_general(void) |
| 483 |
{ |
| 484 |
if (!sorcery) { |
| 485 |
return NULL; |
| 486 |
} |
| 487 |
|
| 488 |
return ast_sorcery_retrieve_by_id(sorcery, "general", "general"); |
| 489 |
} |
| 490 |
|
| 491 |
static int general_pretty_to_str(const void *obj, const intptr_t *args, char **buf) |
| 492 |
{ |
| 493 |
const struct ari_conf_general *general = obj; |
| 494 |
|
| 495 |
if (general->format == AST_JSON_PRETTY) { |
| 496 |
*buf = ast_strdup("yes"); |
| 497 |
} else { |
| 498 |
*buf = ast_strdup("no"); |
| 499 |
} |
| 500 |
return 0; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
static void user_dtor(void *obj) |
| 505 |
{ |
| 506 |
struct ari_conf_user *user = obj; |
| 507 |
ast_string_field_free_memory(user); |
| 508 |
user->acl = ast_free_acl_list(user->acl); |
| 509 |
ast_debug(3, "%s: Disposing of user\n", ast_sorcery_object_get_id(user)); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
static void *user_alloc(const char *cat) |
| 514 |
{ |
| 515 |
struct ari_conf_user *user = ast_sorcery_generic_alloc( |
| 516 |
sizeof(*user), user_dtor); |
| 517 |
|
| 518 |
if (!user) { |
| 519 |
return NULL; |
| 520 |
} |
| 521 |
|
| 522 |
if (ast_string_field_init(user, 64) != 0) { |
| 523 |
ao2_cleanup(user); |
| 524 |
user = NULL; |
| 525 |
} |
| 526 |
|
| 527 |
return user; |
| 528 |
} |
| 529 |
|
| 530 |
static int user_apply(const struct ast_sorcery *sorcery, void *obj) |
| 531 |
{ |
| 532 |
struct ari_conf_user *user = obj; |
| 533 |
const char *id = ast_sorcery_object_get_id(user); |
| 534 |
|
| 535 |
ast_debug(2, "%s: Initializing user\n", id); |
| 536 |
|
| 537 |
if (ast_strlen_zero(user->password)) { |
| 538 |
ast_log(LOG_WARNING, "%s: User missing password\n", id); |
| 539 |
return -1; |
| 540 |
} |
| 541 |
|
| 542 |
return 0; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
static int user_password_format_from_str(const struct aco_option *opt, |
| 547 |
struct ast_variable *var, void *obj) |
| 548 |
{ |
| 549 |
struct ari_conf_user *user = obj; |
| 550 |
|
| 551 |
if (strcasecmp(var->value, "plain") == 0) { |
| 552 |
user->password_format = ARI_PASSWORD_FORMAT_PLAIN; |
| 553 |
} else if (strcasecmp(var->value, "crypt") == 0) { |
| 554 |
user->password_format = ARI_PASSWORD_FORMAT_CRYPT; |
| 555 |
} else { |
| 556 |
return -1; |
| 557 |
} |
| 558 |
|
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
static int user_acl_handler(const struct aco_option *opt, |
| 564 |
struct ast_variable *var, void *obj) |
| 565 |
{ |
| 566 |
struct ari_conf_user *user = obj; |
| 567 |
int error = 0; |
| 568 |
int ignore; |
| 569 |
|
| 570 |
ast_append_acl(var->name, var->value, &user->acl, &error, &ignore); |
| 571 |
if (error) { |
| 572 |
ast_log(LOG_ERROR, "Bad ACL '%s' at line '%d' of ari.conf\n", |
| 573 |
var->value, var->lineno); |
| 574 |
} |
| 575 |
|
| 576 |
return error; |
| 577 |
} |
| 578 |
|
| 579 |
static int user_password_format_to_str(const void *obj, const intptr_t *args, char **buf) |
| 580 |
{ |
| 581 |
const struct ari_conf_user *user = obj; |
| 582 |
|
| 583 |
if (user->password_format == ARI_PASSWORD_FORMAT_CRYPT) { |
| 584 |
*buf = ast_strdup("crypt"); |
| 585 |
} else { |
| 586 |
*buf = ast_strdup("plain"); |
| 587 |
} |
| 588 |
return 0; |
| 589 |
} |
| 590 |
|
| 591 |
struct ao2_container *ari_conf_get_users(void) |
| 592 |
{ |
| 593 |
if (!sorcery) { |
| 594 |
return NULL; |
| 595 |
} |
| 596 |
|
| 597 |
return ast_sorcery_retrieve_by_fields(sorcery, "user", |
| 598 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 599 |
} |
| 600 |
|
| 601 |
struct ari_conf_user *ari_conf_get_user(const char *username) |
| 602 |
{ |
| 603 |
if (!sorcery) { |
| 604 |
return NULL; |
| 605 |
} |
| 606 |
|
| 607 |
return ast_sorcery_retrieve_by_id(sorcery, "user", username); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
struct ari_conf_user *ari_conf_validate_user(const char *username, |
| 615 |
const char *password) |
| 616 |
{ |
| 617 |
struct ari_conf_user *user = NULL; |
| 618 |
int is_valid = 0; |
| 619 |
|
| 620 |
if (ast_strlen_zero(username) || ast_strlen_zero(password)) { |
| 621 |
return NULL; |
| 622 |
} |
| 623 |
|
| 624 |
user = ast_sorcery_retrieve_by_id(sorcery, "user", username); |
| 625 |
if (!user) { |
| 626 |
return NULL; |
| 627 |
} |
| 628 |
|
| 629 |
switch (user->password_format) { |
| 630 |
case ARI_PASSWORD_FORMAT_PLAIN: |
| 631 |
is_valid = strcmp(password, user->password) == 0; |
| 632 |
break; |
| 633 |
case ARI_PASSWORD_FORMAT_CRYPT: |
| 634 |
is_valid = ast_crypt_validate(password, user->password); |
| 635 |
break; |
| 636 |
} |
| 637 |
|
| 638 |
if (!is_valid) { |
| 639 |
ao2_cleanup(user); |
| 640 |
user = NULL; |
| 641 |
} |
| 642 |
|
| 643 |
return user; |
| 644 |
} |
| 645 |
|
| 646 |
int ari_sorcery_observer_add(const char *object_type, |
| 647 |
const struct ast_sorcery_observer *callbacks) |
| 648 |
{ |
| 649 |
if (!sorcery) { |
| 650 |
return -1; |
| 651 |
} |
| 652 |
return ast_sorcery_observer_add(sorcery, object_type, callbacks); |
| 653 |
} |
| 654 |
|
| 655 |
int ari_sorcery_observer_remove(const char *object_type, |
| 656 |
const struct ast_sorcery_observer *callbacks) |
| 657 |
{ |
| 658 |
if (!sorcery) { |
| 659 |
return -1; |
| 660 |
} |
| 661 |
ast_sorcery_observer_remove(sorcery, object_type, callbacks); |
| 662 |
return 0; |
| 663 |
} |
| 664 |
|
| 665 |
static struct ast_sorcery_observer observer_callbacks = { |
| 666 |
.loaded = outbound_websockets_validate, |
| 667 |
}; |
| 668 |
|
| 669 |
static void ws_client_load(const char *name) |
| 670 |
{ |
| 671 |
ast_sorcery_force_reload_object(sorcery, "outbound_websocket"); |
| 672 |
} |
| 673 |
|
| 674 |
static struct ast_sorcery_observer ws_client_observer_callbacks = { |
| 675 |
.loaded = ws_client_load, |
| 676 |
}; |
| 677 |
|
| 678 |
AO2_STRING_FIELD_HASH_FN(outbound_websocket_state, id) |
| 679 |
AO2_STRING_FIELD_CMP_FN(outbound_websocket_state, id) |
| 680 |
|
| 681 |
static int ari_conf_init(void) |
| 682 |
{ |
| 683 |
int res = 0; |
| 684 |
ast_debug(2, "Initializing ARI configuration\n"); |
| 685 |
|
| 686 |
owc_states = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, |
| 687 |
AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, OWC_STATES_BUCKETS, |
| 688 |
outbound_websocket_state_hash_fn, NULL, |
| 689 |
outbound_websocket_state_cmp_fn); |
| 690 |
if (!owc_states) { |
| 691 |
ast_log(LOG_ERROR, "Failed to allocate outbound websocket states\n"); |
| 692 |
return -1; |
| 693 |
} |
| 694 |
|
| 695 |
if (!(sorcery = ast_sorcery_open())) { |
| 696 |
ast_log(LOG_ERROR, "Failed to open sorcery\n"); |
| 697 |
return -1; |
| 698 |
} |
| 699 |
|
| 700 |
ast_sorcery_apply_default(sorcery, "general", "config", |
| 701 |
"ari.conf,criteria=type=general,single_object=yes,explicit_name=general"); |
| 702 |
ast_sorcery_apply_default(sorcery, "user", "config", |
| 703 |
"ari.conf,criteria=type=user"); |
| 704 |
ast_sorcery_apply_default(sorcery, "outbound_websocket", "config", |
| 705 |
"ari.conf,criteria=type=outbound_websocket"); |
| 706 |
|
| 707 |
if (ast_sorcery_object_register(sorcery, "general", general_alloc, NULL, general_apply)) { |
| 708 |
ast_log(LOG_ERROR, "Failed to register ARI general object with sorcery\n"); |
| 709 |
ast_sorcery_unref(sorcery); |
| 710 |
sorcery = NULL; |
| 711 |
return -1; |
| 712 |
} |
| 713 |
|
| 714 |
if (ast_sorcery_object_register(sorcery, "user", user_alloc, NULL, user_apply)) { |
| 715 |
ast_log(LOG_ERROR, "Failed to register ARI user object with sorcery\n"); |
| 716 |
ast_sorcery_unref(sorcery); |
| 717 |
sorcery = NULL; |
| 718 |
return -1; |
| 719 |
} |
| 720 |
|
| 721 |
if (ast_sorcery_object_register(sorcery, "outbound_websocket", outbound_websocket_alloc, |
| 722 |
NULL, outbound_websocket_apply)) { |
| 723 |
ast_log(LOG_ERROR, "Failed to register ARI outbound_websocket object with sorcery\n"); |
| 724 |
ast_sorcery_unref(sorcery); |
| 725 |
sorcery = NULL; |
| 726 |
return -1; |
| 727 |
} |
| 728 |
|
| 729 |
if (ast_sorcery_observer_add(sorcery, "outbound_websocket", &observer_callbacks)) { |
| 730 |
ast_log(LOG_ERROR, "Failed to register ARI outbound_websocket observer with sorcery\n"); |
| 731 |
ast_sorcery_unref(sorcery); |
| 732 |
sorcery = NULL; |
| 733 |
return -1; |
| 734 |
} |
| 735 |
|
| 736 |
ast_sorcery_object_field_register_nodoc(sorcery, "general", "type", "", OPT_NOOP_T, 0, 0); |
| 737 |
ast_sorcery_register_sf(general, ari_conf_general, auth_realm, auth_realm, "Asterisk REST Interface"); |
| 738 |
ast_sorcery_register_sf(general, ari_conf_general, allowed_origins, allowed_origins, ""); |
| 739 |
ast_sorcery_register_sf(general, ari_conf_general, channelvars, channelvars, ""); |
| 740 |
ast_sorcery_register_bool(general, ari_conf_general, enabled, enabled, "yes"); |
| 741 |
ast_sorcery_register_cust(general, pretty, "no"); |
| 742 |
ast_sorcery_register_int(general, ari_conf_general, websocket_write_timeout, write_timeout, |
| 743 |
AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT); |
| 744 |
|
| 745 |
|
| 746 |
ast_sorcery_object_field_register(sorcery, "user", "type", "", OPT_NOOP_T, 0, 0); |
| 747 |
ast_sorcery_register_sf(user, ari_conf_user, password, password, ""); |
| 748 |
ast_sorcery_register_bool(user, ari_conf_user, read_only, read_only, "no"); |
| 749 |
ast_sorcery_register_cust(user, password_format, "plain"); |
| 750 |
ast_sorcery_object_field_register_custom(sorcery, "user", "permit", "", user_acl_handler, NULL, NULL, 0, 0); |
| 751 |
ast_sorcery_object_field_register_custom(sorcery, "user", "deny", "", user_acl_handler, NULL, NULL, 0, 0); |
| 752 |
ast_sorcery_object_field_register_custom(sorcery, "user", "acl", "", user_acl_handler, NULL, NULL, 0, 0); |
| 753 |
|
| 754 |
ast_sorcery_object_field_register(sorcery, "outbound_websocket", "type", "", OPT_NOOP_T, 0, 0); |
| 755 |
ast_sorcery_register_cust(outbound_websocket, websocket_client_id, ""); |
| 756 |
ast_sorcery_register_sf(outbound_websocket, ari_conf_outbound_websocket, apps, apps, ""); |
| 757 |
ast_sorcery_register_sf(outbound_websocket, ari_conf_outbound_websocket, local_ari_user, local_ari_user, ""); |
| 758 |
ast_sorcery_register_bool(outbound_websocket, ari_conf_outbound_websocket, subscribe_all, subscribe_all, "no"); |
| 759 |
|
| 760 |
res = ast_websocket_client_observer_add(&ws_client_observer_callbacks); |
| 761 |
if (res < 0) { |
| 762 |
ast_log(LOG_WARNING, "Failed to register websocket client observer\n"); |
| 763 |
ast_sorcery_unref(sorcery); |
| 764 |
sorcery = NULL; |
| 765 |
return -1; |
| 766 |
} |
| 767 |
|
| 768 |
return 0; |
| 769 |
} |
| 770 |
|
| 771 |
int ari_conf_load(enum ari_conf_load_flags flags) |
| 772 |
{ |
| 773 |
void (*loader)(const struct ast_sorcery *sorcery, const char *type); |
| 774 |
const char *msg_prefix; |
| 775 |
|
| 776 |
if (flags & ARI_CONF_RELOAD) { |
| 777 |
loader = ast_sorcery_reload_object; |
| 778 |
msg_prefix= "Reloading"; |
| 779 |
ast_websocket_client_reload(); |
| 780 |
} else { |
| 781 |
loader = ast_sorcery_load_object; |
| 782 |
msg_prefix= "Loading"; |
| 783 |
} |
| 784 |
|
| 785 |
if (flags & ARI_CONF_INIT) { |
| 786 |
if (ari_conf_init() != 0) { |
| 787 |
ast_log(LOG_ERROR, "Failed to initialize ARI configuration\n"); |
| 788 |
return -1; |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
if (!sorcery) { |
| 793 |
ast_log(LOG_ERROR, "ARI configuration not initialized\n"); |
| 794 |
return -1; |
| 795 |
} |
| 796 |
|
| 797 |
if (flags & ARI_CONF_LOAD_GENERAL) { |
| 798 |
ast_debug(2, "%s ARI '%s' configuration\n", msg_prefix, "general"); |
| 799 |
loader(sorcery, "general"); |
| 800 |
} |
| 801 |
|
| 802 |
if (flags & ARI_CONF_LOAD_USER) { |
| 803 |
ast_debug(2, "%s ARI '%s' configuration\n", msg_prefix, "user"); |
| 804 |
loader(sorcery, "user"); |
| 805 |
} |
| 806 |
|
| 807 |
if (flags & ARI_CONF_LOAD_OWC) { |
| 808 |
ast_debug(2, "%s ARI '%s' configuration\n", msg_prefix, "outbound_websocket"); |
| 809 |
loader(sorcery, "outbound_websocket"); |
| 810 |
} |
| 811 |
|
| 812 |
return 0; |
| 813 |
} |
| 814 |
|
| 815 |
void ari_conf_destroy(void) |
| 816 |
{ |
| 817 |
ast_websocket_client_observer_remove(&ws_client_observer_callbacks); |
| 818 |
|
| 819 |
ast_sorcery_unref(sorcery); |
| 820 |
sorcery = NULL; |
| 821 |
ao2_cleanup(owc_states); |
| 822 |
} |