| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
#include "asterisk.h" |
| 82 |
|
| 83 |
#include "ari/internal.h" |
| 84 |
#include "ari/ari_websockets.h" |
| 85 |
#include "asterisk/ari.h" |
| 86 |
#include "asterisk/astobj2.h" |
| 87 |
#include "asterisk/module.h" |
| 88 |
#include "asterisk/paths.h" |
| 89 |
#include "asterisk/stasis_app.h" |
| 90 |
|
| 91 |
#include <string.h> |
| 92 |
#include <sys/stat.h> |
| 93 |
#include <unistd.h> |
| 94 |
|
| 95 |
|
| 96 |
static int is_enabled(void) |
| 97 |
{ |
| 98 |
RAII_VAR(struct ari_conf_general *, general, ari_conf_get_general(), ao2_cleanup); |
| 99 |
return general && general->enabled; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
static ast_mutex_t root_handler_lock; |
| 104 |
|
| 105 |
|
| 106 |
static struct stasis_rest_handlers *root_handler; |
| 107 |
|
| 108 |
|
| 109 |
static struct ast_json *oom_json; |
| 110 |
|
| 111 |
|
| 112 |
static int ast_ari_callback(struct ast_tcptls_session_instance *ser, |
| 113 |
const struct ast_http_uri *urih, const char *uri, |
| 114 |
enum ast_http_method method, struct ast_variable *get_params, |
| 115 |
struct ast_variable *headers); |
| 116 |
|
| 117 |
static struct ast_http_uri http_uri = { |
| 118 |
.callback = ast_ari_callback, |
| 119 |
.description = "Asterisk RESTful API", |
| 120 |
.uri = "ari", |
| 121 |
.has_subtree = 1, |
| 122 |
.data = NULL, |
| 123 |
.key = __FILE__, |
| 124 |
.no_decode_uri = 1, |
| 125 |
}; |
| 126 |
|
| 127 |
struct ast_json *ast_ari_oom_json(void) |
| 128 |
{ |
| 129 |
return oom_json; |
| 130 |
} |
| 131 |
|
| 132 |
int ast_ari_add_handler(struct stasis_rest_handlers *handler) |
| 133 |
{ |
| 134 |
RAII_VAR(struct stasis_rest_handlers *, new_handler, NULL, ao2_cleanup); |
| 135 |
size_t old_size, new_size; |
| 136 |
|
| 137 |
SCOPED_MUTEX(lock, &root_handler_lock); |
| 138 |
|
| 139 |
old_size = sizeof(*new_handler) + root_handler->num_children * sizeof(handler); |
| 140 |
new_size = old_size + sizeof(handler); |
| 141 |
|
| 142 |
new_handler = ao2_alloc(new_size, NULL); |
| 143 |
if (!new_handler) { |
| 144 |
return -1; |
| 145 |
} |
| 146 |
memcpy(new_handler, root_handler, old_size); |
| 147 |
new_handler->children[new_handler->num_children++] = handler; |
| 148 |
|
| 149 |
ao2_cleanup(root_handler); |
| 150 |
ao2_ref(new_handler, +1); |
| 151 |
root_handler = new_handler; |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
int ast_ari_remove_handler(struct stasis_rest_handlers *handler) |
| 156 |
{ |
| 157 |
struct stasis_rest_handlers *new_handler; |
| 158 |
size_t size; |
| 159 |
size_t i; |
| 160 |
size_t j; |
| 161 |
|
| 162 |
ast_assert(root_handler != NULL); |
| 163 |
|
| 164 |
ast_mutex_lock(&root_handler_lock); |
| 165 |
size = sizeof(*new_handler) + root_handler->num_children * sizeof(handler); |
| 166 |
|
| 167 |
new_handler = ao2_alloc(size, NULL); |
| 168 |
if (!new_handler) { |
| 169 |
ast_mutex_unlock(&root_handler_lock); |
| 170 |
return -1; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
memcpy(new_handler, root_handler, sizeof(*new_handler)); |
| 175 |
for (i = 0, j = 0; i < root_handler->num_children; ++i) { |
| 176 |
if (root_handler->children[i] == handler) { |
| 177 |
continue; |
| 178 |
} |
| 179 |
new_handler->children[j++] = root_handler->children[i]; |
| 180 |
} |
| 181 |
new_handler->num_children = j; |
| 182 |
|
| 183 |
|
| 184 |
ao2_cleanup(root_handler); |
| 185 |
root_handler = new_handler; |
| 186 |
|
| 187 |
ast_mutex_unlock(&root_handler_lock); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
static struct stasis_rest_handlers *get_root_handler(void) |
| 192 |
{ |
| 193 |
SCOPED_MUTEX(lock, &root_handler_lock); |
| 194 |
ao2_ref(root_handler, +1); |
| 195 |
return root_handler; |
| 196 |
} |
| 197 |
|
| 198 |
static struct stasis_rest_handlers *root_handler_create(void) |
| 199 |
{ |
| 200 |
RAII_VAR(struct stasis_rest_handlers *, handler, NULL, ao2_cleanup); |
| 201 |
|
| 202 |
handler = ao2_alloc(sizeof(*handler), NULL); |
| 203 |
if (!handler) { |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
handler->path_segment = "ari"; |
| 207 |
|
| 208 |
ao2_ref(handler, +1); |
| 209 |
return handler; |
| 210 |
} |
| 211 |
|
| 212 |
void ast_ari_response_error(struct ast_ari_response *response, |
| 213 |
int response_code, |
| 214 |
const char *response_text, |
| 215 |
const char *message_fmt, ...) |
| 216 |
{ |
| 217 |
RAII_VAR(struct ast_json *, message, NULL, ast_json_unref); |
| 218 |
va_list ap; |
| 219 |
|
| 220 |
va_start(ap, message_fmt); |
| 221 |
message = ast_json_vstringf(message_fmt, ap); |
| 222 |
va_end(ap); |
| 223 |
response->message = ast_json_pack("{s: o}", |
| 224 |
"message", ast_json_ref(message)); |
| 225 |
response->response_code = response_code; |
| 226 |
response->response_text = response_text; |
| 227 |
} |
| 228 |
|
| 229 |
void ast_ari_response_ok(struct ast_ari_response *response, |
| 230 |
struct ast_json *message) |
| 231 |
{ |
| 232 |
response->message = message; |
| 233 |
response->response_code = 200; |
| 234 |
response->response_text = "OK"; |
| 235 |
} |
| 236 |
|
| 237 |
void ast_ari_response_no_content(struct ast_ari_response *response) |
| 238 |
{ |
| 239 |
response->message = ast_json_null(); |
| 240 |
response->response_code = 204; |
| 241 |
response->response_text = "No Content"; |
| 242 |
} |
| 243 |
|
| 244 |
void ast_ari_response_accepted(struct ast_ari_response *response) |
| 245 |
{ |
| 246 |
response->message = ast_json_null(); |
| 247 |
response->response_code = 202; |
| 248 |
response->response_text = "Accepted"; |
| 249 |
} |
| 250 |
|
| 251 |
void ast_ari_response_alloc_failed(struct ast_ari_response *response) |
| 252 |
{ |
| 253 |
response->message = ast_json_ref(oom_json); |
| 254 |
response->response_code = 500; |
| 255 |
response->response_text = "Internal Server Error"; |
| 256 |
} |
| 257 |
|
| 258 |
void ast_ari_response_created(struct ast_ari_response *response, |
| 259 |
const char *url, struct ast_json *message) |
| 260 |
{ |
| 261 |
RAII_VAR(struct stasis_rest_handlers *, root, get_root_handler(), ao2_cleanup); |
| 262 |
response->message = message; |
| 263 |
response->response_code = 201; |
| 264 |
response->response_text = "Created"; |
| 265 |
ast_str_append(&response->headers, 0, "Location: /%s%s\r\n", root->path_segment, url); |
| 266 |
} |
| 267 |
|
| 268 |
static void add_allow_header(struct stasis_rest_handlers *handler, |
| 269 |
struct ast_ari_response *response) |
| 270 |
{ |
| 271 |
enum ast_http_method m; |
| 272 |
ast_str_append(&response->headers, 0, |
| 273 |
"Allow: OPTIONS"); |
| 274 |
for (m = 0; m < AST_HTTP_MAX_METHOD; ++m) { |
| 275 |
if (handler->callbacks[m] != NULL) { |
| 276 |
ast_str_append(&response->headers, 0, |
| 277 |
",%s", ast_get_http_method(m)); |
| 278 |
} |
| 279 |
} |
| 280 |
ast_str_append(&response->headers, 0, "\r\n"); |
| 281 |
} |
| 282 |
|
| 283 |
static int origin_allowed(const char *origin) |
| 284 |
{ |
| 285 |
RAII_VAR(struct ari_conf_general *, general, ari_conf_get_general(), ao2_cleanup); |
| 286 |
|
| 287 |
char *allowed = ast_strdupa(general ? general->allowed_origins : ""); |
| 288 |
char *current; |
| 289 |
|
| 290 |
while ((current = strsep(&allowed, ","))) { |
| 291 |
if (!strcmp(current, "*")) { |
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
if (!strcmp(current, origin)) { |
| 296 |
return 1; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
#define ACR_METHOD "Access-Control-Request-Method" |
| 304 |
#define ACR_HEADERS "Access-Control-Request-Headers" |
| 305 |
#define ACA_METHODS "Access-Control-Allow-Methods" |
| 306 |
#define ACA_HEADERS "Access-Control-Allow-Headers" |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
static void handle_options(struct stasis_rest_handlers *handler, |
| 315 |
struct ast_variable *headers, |
| 316 |
struct ast_ari_response *response) |
| 317 |
{ |
| 318 |
struct ast_variable *header; |
| 319 |
char const *acr_method = NULL; |
| 320 |
char const *acr_headers = NULL; |
| 321 |
char const *origin = NULL; |
| 322 |
|
| 323 |
RAII_VAR(struct ast_str *, allow, NULL, ast_free); |
| 324 |
enum ast_http_method m; |
| 325 |
int allowed = 0; |
| 326 |
|
| 327 |
|
| 328 |
add_allow_header(handler, response); |
| 329 |
ast_ari_response_no_content(response); |
| 330 |
|
| 331 |
|
| 332 |
for (header = headers; header != NULL; header = header->next) { |
| 333 |
if (strcmp(ACR_METHOD, header->name) == 0) { |
| 334 |
acr_method = header->value; |
| 335 |
} else if (strcmp(ACR_HEADERS, header->name) == 0) { |
| 336 |
acr_headers = header->value; |
| 337 |
} else if (strcmp("Origin", header->name) == 0) { |
| 338 |
origin = header->value; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
if (origin == NULL) { |
| 346 |
return; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if (!origin_allowed(origin)) { |
| 360 |
ast_log(LOG_NOTICE, "Origin header '%s' does not match an allowed origin.\n", origin); |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
if (acr_method == NULL) { |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
if (acr_headers == NULL) { |
| 376 |
acr_headers = ""; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
allow = ast_str_create(20); |
| 384 |
|
| 385 |
if (!allow) { |
| 386 |
ast_ari_response_alloc_failed(response); |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
for (m = 0; m < AST_HTTP_MAX_METHOD; ++m) { |
| 392 |
if (handler->callbacks[m] != NULL) { |
| 393 |
char const *m_str = ast_get_http_method(m); |
| 394 |
if (strcmp(m_str, acr_method) == 0) { |
| 395 |
allowed = 1; |
| 396 |
} |
| 397 |
ast_str_append(&allow, 0, ",%s", m_str); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
if (!allowed) { |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
ast_str_append(&response->headers, 0, "%s: OPTIONS%s\r\n", |
| 429 |
ACA_METHODS, ast_str_buffer(allow)); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
if (!ast_strlen_zero(acr_headers)) { |
| 439 |
ast_str_append(&response->headers, 0, "%s: %s\r\n", |
| 440 |
ACA_HEADERS, acr_headers); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
static struct ast_http_auth *create_http_auth(const char *userid, const char *password) |
| 449 |
{ |
| 450 |
struct ast_http_auth *auth; |
| 451 |
size_t userid_len; |
| 452 |
size_t password_len; |
| 453 |
|
| 454 |
if (!userid || !password) { |
| 455 |
ast_log(LOG_ERROR, "Invalid userid/password\n"); |
| 456 |
return NULL; |
| 457 |
} |
| 458 |
|
| 459 |
userid_len = strlen(userid) + 1; |
| 460 |
password_len = strlen(password) + 1; |
| 461 |
|
| 462 |
|
| 463 |
auth = ao2_alloc_options(sizeof(*auth) + userid_len + password_len, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 464 |
if (!auth) { |
| 465 |
return NULL; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
auth->userid = (char *)(auth + 1); |
| 470 |
strcpy(auth->userid, userid); |
| 471 |
|
| 472 |
|
| 473 |
auth->password = auth->userid + userid_len; |
| 474 |
strcpy(auth->password, password); |
| 475 |
|
| 476 |
return auth; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
static struct ast_http_auth *parse_api_keys(struct ast_variable *get_params) |
| 487 |
{ |
| 488 |
struct ast_variable *v; |
| 489 |
|
| 490 |
for (v = get_params; v; v = v->next) { |
| 491 |
if (ast_strings_equal(v->name, "api_key")) { |
| 492 |
if (!ast_strlen_zero(v->value)) { |
| 493 |
char *password = ast_strdupa(v->value); |
| 494 |
char *username = strsep(&password, ":"); |
| 495 |
if (!ast_strlen_zero(password)) { |
| 496 |
return create_http_auth(username, password); |
| 497 |
} |
| 498 |
} |
| 499 |
break; |
| 500 |
} |
| 501 |
} |
| 502 |
return NULL; |
| 503 |
} |
| 504 |
|
| 505 |
#if AST_DEVMODE |
| 506 |
static void test_ari_user_destructor(void *obj) |
| 507 |
{ |
| 508 |
struct ari_conf_user *ari_user = obj; |
| 509 |
ast_string_field_free_memory(ari_user); |
| 510 |
} |
| 511 |
#endif |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
static struct ari_conf_user *authenticate_user(struct ast_variable *get_params, |
| 522 |
struct ast_variable *headers, enum ast_ari_invoke_source source) |
| 523 |
{ |
| 524 |
RAII_VAR(struct ast_http_auth *, http_auth, NULL, ao2_cleanup); |
| 525 |
struct ari_conf_user *ari_user = NULL; |
| 526 |
SCOPE_ENTER(3, "\n"); |
| 527 |
|
| 528 |
|
| 529 |
http_auth = ast_http_get_auth(headers); |
| 530 |
if (!http_auth) { |
| 531 |
|
| 532 |
http_auth = parse_api_keys(get_params); |
| 533 |
} |
| 534 |
if (!http_auth) { |
| 535 |
SCOPE_EXIT_RTN_VALUE(NULL, "No credentials found in HTTP headers or api_key\n"); |
| 536 |
} |
| 537 |
|
| 538 |
if (source != ARI_INVOKE_SOURCE_TEST) { |
| 539 |
ast_trace(-1, "Validating user %s\n", http_auth->userid); |
| 540 |
ari_user = ari_conf_validate_user(http_auth->userid, http_auth->password); |
| 541 |
if (!ari_user) { |
| 542 |
SCOPE_EXIT_RTN_VALUE(NULL, "User %s failed to validate\n", http_auth->userid); |
| 543 |
} |
| 544 |
SCOPE_EXIT_RTN_VALUE(ari_user, "User %s validated. Read only: %s\n", |
| 545 |
http_auth->userid, AST_YESNO(ari_user->read_only)); |
| 546 |
} |
| 547 |
#if AST_DEVMODE |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
else { |
| 554 |
int readonly = 0; |
| 555 |
|
| 556 |
if (ast_strings_equal(http_auth->userid, "aritest")) { |
| 557 |
if (!ast_strings_equal(http_auth->password, "aritestpw")) { |
| 558 |
SCOPE_EXIT_RTN_VALUE(NULL, "User %s failed to validate\n", http_auth->userid); |
| 559 |
} |
| 560 |
} else if (ast_strings_equal(http_auth->userid, "aritestro")) { |
| 561 |
if (!ast_strings_equal(http_auth->password, "aritestropw")) { |
| 562 |
SCOPE_EXIT_RTN_VALUE(NULL, "User %s failed to validate\n", http_auth->userid); |
| 563 |
} |
| 564 |
readonly = 1; |
| 565 |
} |
| 566 |
|
| 567 |
ari_user = ao2_alloc(sizeof(*ari_user), test_ari_user_destructor); |
| 568 |
if (!ari_user) { |
| 569 |
SCOPE_EXIT_RTN_VALUE(NULL, "alloc failed\n"); |
| 570 |
} |
| 571 |
if (ast_string_field_init(ari_user, 256) != 0) { |
| 572 |
SCOPE_EXIT_RTN_VALUE(NULL, "alloc failed\n"); |
| 573 |
} |
| 574 |
ast_string_field_set(ari_user, password, http_auth->password); |
| 575 |
ari_user->read_only = readonly; |
| 576 |
|
| 577 |
SCOPE_EXIT_RTN_VALUE(ari_user, "User %s validated. Read only: %s\n", |
| 578 |
http_auth->userid, AST_YESNO(ari_user->read_only)); |
| 579 |
} |
| 580 |
|
| 581 |
#else |
| 582 |
return NULL; |
| 583 |
#endif |
| 584 |
} |
| 585 |
|
| 586 |
static void remove_trailing_slash(const char *uri, |
| 587 |
struct ast_ari_response *response) |
| 588 |
{ |
| 589 |
char *slashless = ast_strdupa(uri); |
| 590 |
slashless[strlen(slashless) - 1] = '\0'; |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
ast_ari_response_error(response, 404, "Not Found", |
| 609 |
"ARI URLs do not end with a slash. Try /ari/%s", slashless); |
| 610 |
} |
| 611 |
|
| 612 |
enum ast_ari_invoke_result ast_ari_invoke(struct ast_tcptls_session_instance *ser, |
| 613 |
enum ast_ari_invoke_source source, const struct ast_http_uri *urih, |
| 614 |
const char *uri, enum ast_http_method method, |
| 615 |
struct ast_variable *get_params, struct ast_variable *headers, |
| 616 |
struct ast_json *body, struct ast_ari_response *response) |
| 617 |
{ |
| 618 |
RAII_VAR(struct stasis_rest_handlers *, root, NULL, ao2_cleanup); |
| 619 |
struct stasis_rest_handlers *handler = NULL; |
| 620 |
struct stasis_rest_handlers *wildcard_handler = NULL; |
| 621 |
RAII_VAR(struct ast_variable *, path_vars, NULL, ast_variables_destroy); |
| 622 |
RAII_VAR(struct ari_conf_user *, user, NULL, ao2_cleanup); |
| 623 |
RAII_VAR(struct ari_conf_general *, general, ari_conf_get_general(), ao2_cleanup); |
| 624 |
|
| 625 |
char *path = ast_strdupa(uri); |
| 626 |
char *path_segment = NULL; |
| 627 |
stasis_rest_callback callback; |
| 628 |
SCOPE_ENTER(3, "Request: %s %s, path:%s\n", ast_get_http_method(method), uri, path); |
| 629 |
|
| 630 |
|
| 631 |
if (!general) { |
| 632 |
if (ser && source == ARI_INVOKE_SOURCE_REST) { |
| 633 |
ast_http_request_close_on_completion(ser); |
| 634 |
} |
| 635 |
ast_ari_response_error(response, 500, "Server Error", "URI handler config missing"); |
| 636 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CLOSE, "Response: %d : %s\n", |
| 637 |
response->response_code, response->response_text); |
| 638 |
} |
| 639 |
|
| 640 |
user = authenticate_user(get_params, headers, source); |
| 641 |
if (!user) { |
| 642 |
ast_ari_response_error(response, 401, "Unauthorized", "Authentication required"); |
| 643 |
if (source == ARI_INVOKE_SOURCE_REST) { |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
ast_str_append(&response->headers, 0, |
| 658 |
"WWW-Authenticate: Basic realm=\"%s\"\r\n", |
| 659 |
general->auth_realm); |
| 660 |
} |
| 661 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 662 |
response->response_code, response->response_text); |
| 663 |
} |
| 664 |
|
| 665 |
if (source == ARI_INVOKE_SOURCE_REST && ser && user->acl && !ast_acl_list_is_empty(user->acl) && |
| 666 |
ast_apply_acl(user->acl, &ser->remote_address, "ARI User ACL") == AST_SENSE_DENY) { |
| 667 |
ast_ari_response_error(response, 403, "Forbidden", "Access denied by ACL"); |
| 668 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 669 |
response->response_code, response->response_text); |
| 670 |
} else if (!ast_fully_booted) { |
| 671 |
ast_ari_response_error(response, 503, "Service Unavailable", "Asterisk not booted"); |
| 672 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CLOSE, "Response: %d : %s\n", |
| 673 |
response->response_code, response->response_text); |
| 674 |
} else if (user->read_only && method != AST_HTTP_GET && method != AST_HTTP_OPTIONS) { |
| 675 |
ast_ari_response_error(response, 403, "Forbidden", "Write access denied"); |
| 676 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 677 |
response->response_code, response->response_text); |
| 678 |
} else if (ast_ends_with(uri, "/")) { |
| 679 |
remove_trailing_slash(uri, response); |
| 680 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 681 |
response->response_code, response->response_text); |
| 682 |
} else if (ast_begins_with(uri, "api-docs/")) { |
| 683 |
|
| 684 |
if (method != AST_HTTP_GET) { |
| 685 |
ast_ari_response_error(response, 405, "Method Not Allowed", "Unsupported method"); |
| 686 |
} else { |
| 687 |
if (urih) { |
| 688 |
|
| 689 |
ast_ari_get_docs(strchr(uri, '/') + 1, urih->prefix, headers, response); |
| 690 |
} else { |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
ast_ari_get_docs(strchr(uri, '/') + 1, http_uri.prefix, headers, response); |
| 697 |
} |
| 698 |
} |
| 699 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 700 |
response->response_code, response->response_text); |
| 701 |
} |
| 702 |
|
| 703 |
root = handler = get_root_handler(); |
| 704 |
ast_assert(root != NULL); |
| 705 |
|
| 706 |
while ((path_segment = strsep(&path, "/")) && (strlen(path_segment) > 0)) { |
| 707 |
struct stasis_rest_handlers *found_handler = NULL; |
| 708 |
int i; |
| 709 |
SCOPE_ENTER(4, "Finding handler for path segment %s\n", path_segment); |
| 710 |
|
| 711 |
ast_uri_decode(path_segment, ast_uri_http_legacy); |
| 712 |
|
| 713 |
for (i = 0; found_handler == NULL && i < handler->num_children; ++i) { |
| 714 |
struct stasis_rest_handlers *child = handler->children[i]; |
| 715 |
SCOPE_ENTER(5, "Checking handler path segment %s\n", child->path_segment); |
| 716 |
|
| 717 |
if (child->is_wildcard) { |
| 718 |
|
| 719 |
struct ast_variable *path_var = ast_variable_new(child->path_segment, path_segment, __FILE__); |
| 720 |
path_var->next = path_vars; |
| 721 |
path_vars = path_var; |
| 722 |
wildcard_handler = child; |
| 723 |
ast_trace(-1, " Checking %s %s: Matched wildcard.\n", handler->path_segment, child->path_segment); |
| 724 |
|
| 725 |
} else if (strcmp(child->path_segment, path_segment) == 0) { |
| 726 |
found_handler = child; |
| 727 |
ast_trace(-1, " Checking %s %s: Explicit match with %s\n", handler->path_segment, child->path_segment, path_segment); |
| 728 |
} else { |
| 729 |
ast_trace(-1, " Checking %s %s: Didn't match %s\n", handler->path_segment, child->path_segment, path_segment); |
| 730 |
} |
| 731 |
SCOPE_EXIT("Done checking %s\n", child->path_segment); |
| 732 |
} |
| 733 |
|
| 734 |
if (!found_handler && wildcard_handler) { |
| 735 |
ast_trace(-1, " No explicit handler found for %s. Using wildcard %s.\n", |
| 736 |
path_segment, wildcard_handler->path_segment); |
| 737 |
found_handler = wildcard_handler; |
| 738 |
wildcard_handler = NULL; |
| 739 |
} |
| 740 |
|
| 741 |
if (found_handler == NULL) { |
| 742 |
|
| 743 |
ast_ari_response_error( |
| 744 |
response, 404, "Not Found", |
| 745 |
"Resource not found"); |
| 746 |
SCOPE_EXIT_EXPR(break, "Handler not found for %s\n", path_segment); |
| 747 |
} else { |
| 748 |
handler = found_handler; |
| 749 |
} |
| 750 |
SCOPE_EXIT("Done checking %s\n", path_segment); |
| 751 |
} |
| 752 |
|
| 753 |
if (handler == NULL || response->response_code == 404) { |
| 754 |
|
| 755 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s %s\n", |
| 756 |
response->response_code, response->response_text, uri); |
| 757 |
} |
| 758 |
|
| 759 |
ast_assert(handler != NULL); |
| 760 |
if (method == AST_HTTP_OPTIONS) { |
| 761 |
handle_options(handler, headers, response); |
| 762 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Was options\n"); |
| 763 |
} |
| 764 |
|
| 765 |
if (method < 0 || method >= AST_HTTP_MAX_METHOD) { |
| 766 |
add_allow_header(handler, response); |
| 767 |
ast_ari_response_error( |
| 768 |
response, 405, "Method Not Allowed", |
| 769 |
"Invalid method"); |
| 770 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 771 |
response->response_code, response->response_text); |
| 772 |
} |
| 773 |
|
| 774 |
if (handler->is_websocket && method == AST_HTTP_GET) { |
| 775 |
if (source == ARI_INVOKE_SOURCE_WEBSOCKET) { |
| 776 |
ast_ari_response_error( |
| 777 |
response, 400, "Bad request", |
| 778 |
"Can't upgrade to websocket from a websocket"); |
| 779 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 780 |
response->response_code, response->response_text); |
| 781 |
} |
| 782 |
|
| 783 |
ast_trace(-1, "Handling websocket %s\n", uri); |
| 784 |
ari_handle_websocket(ser, uri, method, |
| 785 |
get_params, headers); |
| 786 |
|
| 787 |
|
| 788 |
response->no_response = 1; |
| 789 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Upgrade to websocket\n"); |
| 790 |
} |
| 791 |
|
| 792 |
callback = handler->callbacks[method]; |
| 793 |
if (callback == NULL) { |
| 794 |
add_allow_header(handler, response); |
| 795 |
ast_ari_response_error( |
| 796 |
response, 405, "Method Not Allowed", |
| 797 |
"Invalid method"); |
| 798 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 799 |
response->response_code, response->response_text); |
| 800 |
} |
| 801 |
|
| 802 |
ast_trace(-1, "Running callback: %s\n", uri); |
| 803 |
callback(ser, get_params, path_vars, headers, body, response); |
| 804 |
if (response->message == NULL && response->response_code == 0) { |
| 805 |
|
| 806 |
ast_log(LOG_ERROR, "ARI %s %s not implemented\n", |
| 807 |
ast_get_http_method(method), uri); |
| 808 |
ast_ari_response_error( |
| 809 |
response, 501, "Not Implemented", |
| 810 |
"Method not implemented"); |
| 811 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_ERROR_CONTINUE, "Response: %d : %s\n", |
| 812 |
response->response_code, response->response_text); |
| 813 |
} |
| 814 |
SCOPE_EXIT_RTN_VALUE(ARI_INVOKE_RESULT_SUCCESS, "Response: %d : %s\n", |
| 815 |
response->response_code, response->response_text); |
| 816 |
} |
| 817 |
|
| 818 |
void ast_ari_get_docs(const char *uri, const char *prefix, struct ast_variable *headers, |
| 819 |
struct ast_ari_response *response) |
| 820 |
{ |
| 821 |
RAII_VAR(struct ast_str *, absolute_path_builder, NULL, ast_free); |
| 822 |
RAII_VAR(char *, absolute_api_dirname, NULL, ast_std_free); |
| 823 |
RAII_VAR(char *, absolute_filename, NULL, ast_std_free); |
| 824 |
struct ast_json *obj = NULL; |
| 825 |
struct ast_variable *host = NULL; |
| 826 |
struct ast_json_error error = {}; |
| 827 |
struct stat file_stat; |
| 828 |
|
| 829 |
ast_debug(3, "%s(%s)\n", __func__, uri); |
| 830 |
|
| 831 |
absolute_path_builder = ast_str_create(80); |
| 832 |
if (absolute_path_builder == NULL) { |
| 833 |
ast_ari_response_alloc_failed(response); |
| 834 |
return; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
ast_str_append(&absolute_path_builder, 0, "%s", ast_config_AST_DATA_DIR); |
| 839 |
ast_str_append(&absolute_path_builder, 0, "/rest-api/"); |
| 840 |
absolute_api_dirname = realpath(ast_str_buffer(absolute_path_builder), NULL); |
| 841 |
if (absolute_api_dirname == NULL) { |
| 842 |
ast_log(LOG_ERROR, "Error determining real directory for rest-api\n"); |
| 843 |
ast_ari_response_error( |
| 844 |
response, 500, "Internal Server Error", |
| 845 |
"Cannot find rest-api directory"); |
| 846 |
return; |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
ast_str_append(&absolute_path_builder, 0, "%s", uri); |
| 851 |
absolute_filename = realpath(ast_str_buffer(absolute_path_builder), NULL); |
| 852 |
if (absolute_filename == NULL) { |
| 853 |
switch (errno) { |
| 854 |
case ENAMETOOLONG: |
| 855 |
case ENOENT: |
| 856 |
case ENOTDIR: |
| 857 |
ast_ari_response_error( |
| 858 |
response, 404, "Not Found", |
| 859 |
"Resource not found"); |
| 860 |
break; |
| 861 |
case EACCES: |
| 862 |
ast_ari_response_error( |
| 863 |
response, 403, "Forbidden", |
| 864 |
"Permission denied"); |
| 865 |
break; |
| 866 |
default: |
| 867 |
ast_log(LOG_ERROR, |
| 868 |
"Error determining real path for uri '%s': %s\n", |
| 869 |
uri, strerror(errno)); |
| 870 |
ast_ari_response_error( |
| 871 |
response, 500, "Internal Server Error", |
| 872 |
"Cannot find file"); |
| 873 |
break; |
| 874 |
} |
| 875 |
return; |
| 876 |
} |
| 877 |
|
| 878 |
if (!ast_begins_with(absolute_filename, absolute_api_dirname)) { |
| 879 |
|
| 880 |
ast_log(LOG_ERROR, |
| 881 |
"Invalid attempt to access '%s' (not in %s)\n", |
| 882 |
absolute_filename, absolute_api_dirname); |
| 883 |
ast_ari_response_error( |
| 884 |
response, 404, "Not Found", |
| 885 |
"Resource not found"); |
| 886 |
return; |
| 887 |
} |
| 888 |
|
| 889 |
if (stat(absolute_filename, &file_stat) == 0) { |
| 890 |
if (!(file_stat.st_mode & S_IFREG)) { |
| 891 |
|
| 892 |
ast_ari_response_error( |
| 893 |
response, 403, "Forbidden", |
| 894 |
"Invalid access"); |
| 895 |
return; |
| 896 |
} |
| 897 |
} else { |
| 898 |
|
| 899 |
ast_ari_response_error( |
| 900 |
response, 404, "Not Found", |
| 901 |
"Resource not found"); |
| 902 |
return; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
obj = ast_json_load_new_file(absolute_filename, &error); |
| 907 |
if (obj == NULL) { |
| 908 |
ast_log(LOG_ERROR, "Error parsing resource file: %s:%d(%d) %s\n", |
| 909 |
error.source, error.line, error.column, error.text); |
| 910 |
ast_ari_response_error( |
| 911 |
response, 500, "Internal Server Error", |
| 912 |
"Yikes! Cannot parse resource"); |
| 913 |
return; |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
if (ast_json_object_get(obj, "basePath") != NULL) { |
| 918 |
for (host = headers; host; host = host->next) { |
| 919 |
if (strcasecmp(host->name, "Host") == 0) { |
| 920 |
break; |
| 921 |
} |
| 922 |
} |
| 923 |
if (host != NULL) { |
| 924 |
if (prefix != NULL && strlen(prefix) > 0) { |
| 925 |
ast_json_object_set( |
| 926 |
obj, "basePath", |
| 927 |
ast_json_stringf("http://%s%s/ari", host->value,prefix)); |
| 928 |
} else { |
| 929 |
ast_json_object_set( |
| 930 |
obj, "basePath", |
| 931 |
ast_json_stringf("http://%s/ari", host->value)); |
| 932 |
} |
| 933 |
} else { |
| 934 |
|
| 935 |
ast_json_object_del(obj, "basePath"); |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
ast_ari_response_ok(response, obj); |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
static void process_cors_request(struct ast_variable *headers, |
| 948 |
struct ast_ari_response *response) |
| 949 |
{ |
| 950 |
char const *origin = NULL; |
| 951 |
struct ast_variable *header; |
| 952 |
|
| 953 |
|
| 954 |
for (header = headers; header != NULL; header = header->next) { |
| 955 |
if (strcmp("Origin", header->name) == 0) { |
| 956 |
origin = header->value; |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
if (origin == NULL) { |
| 964 |
return; |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
if (!origin_allowed(origin)) { |
| 975 |
ast_log(LOG_NOTICE, "Origin header '%s' does not match an allowed origin.\n", origin); |
| 976 |
return; |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
ast_str_append(&response->headers, 0, |
| 988 |
"Access-Control-Allow-Origin: %s\r\n", origin); |
| 989 |
ast_str_append(&response->headers, 0, |
| 990 |
"Access-Control-Allow-Credentials: true\r\n"); |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
} |
| 999 |
|
| 1000 |
enum ast_json_encoding_format ast_ari_json_format(void) |
| 1001 |
{ |
| 1002 |
RAII_VAR(struct ari_conf_general *, general, ari_conf_get_general(), ao2_cleanup); |
| 1003 |
return general ? general->format : AST_JSON_COMPACT; |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
static int ast_ari_callback(struct ast_tcptls_session_instance *ser, |
| 1021 |
const struct ast_http_uri *urih, |
| 1022 |
const char *uri, |
| 1023 |
enum ast_http_method method, |
| 1024 |
struct ast_variable *get_params, |
| 1025 |
struct ast_variable *headers) |
| 1026 |
{ |
| 1027 |
RAII_VAR(struct ast_str *, response_body, ast_str_create(256), ast_free); |
| 1028 |
struct ast_ari_response response = { .fd = -1, 0 }; |
| 1029 |
RAII_VAR(struct ast_variable *, post_vars, NULL, ast_variables_destroy); |
| 1030 |
struct ast_variable *var; |
| 1031 |
const char *app_name = NULL; |
| 1032 |
RAII_VAR(struct ast_json *, body, ast_json_null(), ast_json_unref); |
| 1033 |
int debug_app = 0; |
| 1034 |
enum ast_ari_invoke_result result; |
| 1035 |
SCOPE_ENTER(2, "%s: Request: %s %s\n", ast_sockaddr_stringify(&ser->remote_address), |
| 1036 |
ast_get_http_method(method), uri); |
| 1037 |
|
| 1038 |
if (!response_body) { |
| 1039 |
ast_http_request_close_on_completion(ser); |
| 1040 |
ast_http_error(ser, 500, "Server Error", "Out of memory"); |
| 1041 |
SCOPE_EXIT_RTN_VALUE(0, "Out of memory\n"); |
| 1042 |
} |
| 1043 |
|
| 1044 |
response.headers = ast_str_create(40); |
| 1045 |
if (!response.headers) { |
| 1046 |
ast_http_request_close_on_completion(ser); |
| 1047 |
ast_http_error(ser, 500, "Server Error", "Out of memory"); |
| 1048 |
SCOPE_EXIT_RTN_VALUE(0, "Out of memory\n"); |
| 1049 |
} |
| 1050 |
|
| 1051 |
process_cors_request(headers, &response); |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
post_vars = ast_http_get_post_vars(ser, headers); |
| 1058 |
if (!post_vars) { |
| 1059 |
ast_trace(-1, "No post_vars\n"); |
| 1060 |
switch (errno) { |
| 1061 |
case EFBIG: |
| 1062 |
ast_ari_response_error(&response, 413, |
| 1063 |
"Request Entity Too Large", |
| 1064 |
"Request body too large"); |
| 1065 |
goto request_failed; |
| 1066 |
case ENOMEM: |
| 1067 |
ast_http_request_close_on_completion(ser); |
| 1068 |
ast_ari_response_error(&response, 500, |
| 1069 |
"Internal Server Error", |
| 1070 |
"Out of memory"); |
| 1071 |
goto request_failed; |
| 1072 |
case EIO: |
| 1073 |
ast_ari_response_error(&response, 400, |
| 1074 |
"Bad Request", "Error parsing request body"); |
| 1075 |
goto request_failed; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
ast_trace(-1, "Checking body for vars\n"); |
| 1083 |
body = ast_http_get_json(ser, headers); |
| 1084 |
if (!body) { |
| 1085 |
switch (errno) { |
| 1086 |
case EFBIG: |
| 1087 |
ast_ari_response_error(&response, 413, "Request Entity Too Large", "Request body too large"); |
| 1088 |
goto request_failed; |
| 1089 |
case ENOMEM: |
| 1090 |
ast_ari_response_error(&response, 500, "Internal Server Error", "Error processing request"); |
| 1091 |
goto request_failed; |
| 1092 |
case EIO: |
| 1093 |
ast_ari_response_error(&response, 400, "Bad Request", "Error parsing request body"); |
| 1094 |
goto request_failed; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
if (get_params == NULL) { |
| 1099 |
ast_trace(-1, "No get_params, using post_vars if any\n"); |
| 1100 |
get_params = post_vars; |
| 1101 |
} else if (get_params && post_vars) { |
| 1102 |
|
| 1103 |
struct ast_variable *last_var = post_vars; |
| 1104 |
ast_trace(-1, "Has get_params and post_vars. Merging\n"); |
| 1105 |
while (last_var->next) { |
| 1106 |
last_var = last_var->next; |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
last_var->next = ast_variables_dup(get_params); |
| 1112 |
get_params = post_vars; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
app_name = ast_variable_find_in_list(get_params, "app"); |
| 1117 |
if (!app_name) { |
| 1118 |
struct ast_json *app = ast_json_object_get(body, "app"); |
| 1119 |
|
| 1120 |
app_name = (app ? ast_json_string_get(app) : NULL); |
| 1121 |
} |
| 1122 |
ast_trace(-1, "app_name: %s\n", app_name); |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
debug_app = stasis_app_get_debug_by_name(app_name); |
| 1128 |
if (debug_app) { |
| 1129 |
struct ast_str *buf = ast_str_create(512); |
| 1130 |
char *str = ast_json_dump_string_format(body, ast_ari_json_format()); |
| 1131 |
|
| 1132 |
if (!buf || (body && !str)) { |
| 1133 |
ast_http_request_close_on_completion(ser); |
| 1134 |
ast_ari_response_error(&response, 500, "Server Error", "Out of memory"); |
| 1135 |
ast_json_free(str); |
| 1136 |
ast_free(buf); |
| 1137 |
goto request_failed; |
| 1138 |
} |
| 1139 |
|
| 1140 |
ast_str_append(&buf, 0, "<--- ARI request received from: %s --->\n", |
| 1141 |
ast_sockaddr_stringify(&ser->remote_address)); |
| 1142 |
ast_str_append(&buf, 0, "%s %s\n", ast_get_http_method(method), uri); |
| 1143 |
for (var = headers; var; var = var->next) { |
| 1144 |
ast_str_append(&buf, 0, "%s: %s\n", var->name, var->value); |
| 1145 |
} |
| 1146 |
for (var = get_params; var; var = var->next) { |
| 1147 |
ast_str_append(&buf, 0, "%s: %s\n", var->name, var->value); |
| 1148 |
} |
| 1149 |
ast_verbose("%sbody:\n%s\n\n", ast_str_buffer(buf), S_OR(str, "")); |
| 1150 |
ast_json_free(str); |
| 1151 |
ast_free(buf); |
| 1152 |
} |
| 1153 |
|
| 1154 |
result = SCOPE_CALL_WITH_RESULT(-1, enum ast_ari_invoke_result, |
| 1155 |
ast_ari_invoke, ser, ARI_INVOKE_SOURCE_REST, |
| 1156 |
urih, uri, method, get_params, headers, body, &response); |
| 1157 |
if (result == ARI_INVOKE_RESULT_ERROR_CLOSE) { |
| 1158 |
ast_http_request_close_on_completion(ser); |
| 1159 |
} |
| 1160 |
|
| 1161 |
if (response.no_response) { |
| 1162 |
|
| 1163 |
|
| 1164 |
ast_free(response.headers); |
| 1165 |
SCOPE_EXIT_RTN_VALUE(0, "No response needed\n"); |
| 1166 |
} |
| 1167 |
|
| 1168 |
request_failed: |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
ast_assert(response.message != NULL); |
| 1174 |
ast_assert(response.response_code > 0); |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
if (response.message && !ast_json_is_null(response.message)) { |
| 1180 |
ast_str_append(&response.headers, 0, |
| 1181 |
"Content-type: application/json\r\n"); |
| 1182 |
if (ast_json_dump_str_format(response.message, &response_body, |
| 1183 |
ast_ari_json_format()) != 0) { |
| 1184 |
|
| 1185 |
response.response_code = 500; |
| 1186 |
response.response_text = "Internal Server Error"; |
| 1187 |
ast_str_set(&response_body, 0, "%s", ""); |
| 1188 |
ast_str_set(&response.headers, 0, "%s", ""); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
if (debug_app) { |
| 1193 |
ast_verbose("<--- Sending ARI response to %s --->\n%d %s\n%s%s\n\n", |
| 1194 |
ast_sockaddr_stringify(&ser->remote_address), response.response_code, |
| 1195 |
response.response_text, ast_str_buffer(response.headers), |
| 1196 |
ast_str_buffer(response_body)); |
| 1197 |
} |
| 1198 |
|
| 1199 |
ast_http_send(ser, method, response.response_code, |
| 1200 |
response.response_text, response.headers, response_body, |
| 1201 |
response.fd != -1 ? response.fd : 0, 0); |
| 1202 |
|
| 1203 |
response_body = NULL; |
| 1204 |
|
| 1205 |
ast_json_unref(response.message); |
| 1206 |
if (response.fd >= 0) { |
| 1207 |
close(response.fd); |
| 1208 |
} |
| 1209 |
SCOPE_EXIT_RTN_VALUE(0, "Done. response: %d : %s\n", response.response_code, |
| 1210 |
response.response_text); |
| 1211 |
} |
| 1212 |
|
| 1213 |
static int unload_module(void) |
| 1214 |
{ |
| 1215 |
ari_websocket_unload_module(); |
| 1216 |
|
| 1217 |
ari_cli_unregister(); |
| 1218 |
|
| 1219 |
if (is_enabled()) { |
| 1220 |
ast_debug(3, "Disabling ARI\n"); |
| 1221 |
ast_http_uri_unlink(&http_uri); |
| 1222 |
} |
| 1223 |
|
| 1224 |
ari_conf_destroy(); |
| 1225 |
|
| 1226 |
ao2_cleanup(root_handler); |
| 1227 |
root_handler = NULL; |
| 1228 |
ast_mutex_destroy(&root_handler_lock); |
| 1229 |
|
| 1230 |
ast_json_unref(oom_json); |
| 1231 |
oom_json = NULL; |
| 1232 |
|
| 1233 |
return 0; |
| 1234 |
} |
| 1235 |
|
| 1236 |
static int load_module(void) |
| 1237 |
{ |
| 1238 |
ast_mutex_init(&root_handler_lock); |
| 1239 |
|
| 1240 |
|
| 1241 |
if (!root_handler) { |
| 1242 |
root_handler = root_handler_create(); |
| 1243 |
} |
| 1244 |
if (!root_handler) { |
| 1245 |
return AST_MODULE_LOAD_DECLINE; |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
if (!oom_json) { |
| 1250 |
oom_json = ast_json_pack( |
| 1251 |
"{s: s}", "error", "Allocation failed"); |
| 1252 |
} |
| 1253 |
if (!oom_json) { |
| 1254 |
|
| 1255 |
unload_module(); |
| 1256 |
return AST_MODULE_LOAD_DECLINE; |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
if (ari_conf_load(ARI_CONF_INIT | ARI_CONF_LOAD_GENERAL | ARI_CONF_LOAD_USER) != 0) { |
| 1270 |
unload_module(); |
| 1271 |
return AST_MODULE_LOAD_DECLINE; |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (ari_websocket_load_module(is_enabled()) != AST_MODULE_LOAD_SUCCESS) { |
| 1275 |
unload_module(); |
| 1276 |
return AST_MODULE_LOAD_DECLINE; |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
ari_conf_load(ARI_CONF_LOAD_OWC); |
| 1284 |
|
| 1285 |
if (ari_cli_register() != 0) { |
| 1286 |
unload_module(); |
| 1287 |
return AST_MODULE_LOAD_DECLINE; |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (is_enabled()) { |
| 1291 |
ast_debug(3, "ARI enabled\n"); |
| 1292 |
ast_http_uri_link(&http_uri); |
| 1293 |
} else { |
| 1294 |
ast_debug(3, "ARI disabled\n"); |
| 1295 |
} |
| 1296 |
|
| 1297 |
return AST_MODULE_LOAD_SUCCESS; |
| 1298 |
} |
| 1299 |
|
| 1300 |
static int reload_module(void) |
| 1301 |
{ |
| 1302 |
char was_enabled = is_enabled(); |
| 1303 |
int is_now_enabled = 0; |
| 1304 |
|
| 1305 |
ari_conf_load(ARI_CONF_RELOAD | ARI_CONF_LOAD_ALL); |
| 1306 |
|
| 1307 |
is_now_enabled = is_enabled(); |
| 1308 |
|
| 1309 |
if (was_enabled && !is_now_enabled) { |
| 1310 |
ast_debug(3, "Disabling ARI\n"); |
| 1311 |
ast_http_uri_unlink(&http_uri); |
| 1312 |
} else if (!was_enabled && is_now_enabled) { |
| 1313 |
ast_debug(3, "Enabling ARI\n"); |
| 1314 |
ast_http_uri_link(&http_uri); |
| 1315 |
} |
| 1316 |
|
| 1317 |
return AST_MODULE_LOAD_SUCCESS; |
| 1318 |
} |
| 1319 |
|
| 1320 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Asterisk RESTful Interface", |
| 1321 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1322 |
.load = load_module, |
| 1323 |
.unload = unload_module, |
| 1324 |
.reload = reload_module, |
| 1325 |
.requires = "http,res_stasis,res_http_websocket,res_websocket_client", |
| 1326 |
.optional_modules = "res_ari_model", |
| 1327 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 1328 |
); |