Debian Codegraph @

res/res_ari_asterisk.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012 - 2013, Digium, Inc.
5 *
6 * David M. Lee, II <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*
20 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 * !!!!! DO NOT EDIT !!!!!
22 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 * This file is generated by a mustache template. Please see the original
24 * template in rest-api-templates/res_ari_resource.c.mustache
25 */
26
27 /*! \file
28 *
29 * \brief Asterisk resources
30 *
31 * \author David M. Lee, II <[email protected]>
32 */
33
34 /*** MODULEINFO
35 <depend type="module">res_ari</depend>
36 <depend type="module">res_ari_model</depend>
37 <depend type="module">res_stasis</depend>
38 <support_level>core</support_level>
39 ***/
40
41 #include "asterisk.h"
42
43 #include "asterisk/app.h"
44 #include "asterisk/module.h"
45 #include "asterisk/stasis_app.h"
46 #include "ari/resource_asterisk.h"
47 #if defined(AST_DEVMODE)
48 #include "ari/ari_model_validators.h"
49 #endif
50
51 #define MAX_VALS 128
52
53 /*!
54 * \brief Parameter parsing callback for /asterisk/config/dynamic/{configClass}/{objectType}/{id}.
55 * \param ser TCP/TLS session object
56 * \param get_params GET parameters in the HTTP request.
57 * \param path_vars Path variables extracted from the request.
58 * \param headers HTTP headers.
59 * \param body
60 * \param[out] response Response to the HTTP request.
61 */
62 static void ast_ari_asterisk_get_object_cb(
63 struct ast_tcptls_session_instance *ser,
64 struct ast_variable *get_params, struct ast_variable *path_vars,
65 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
66 {
67 struct ast_ari_asterisk_get_object_args args = {};
68 struct ast_variable *i;
69 #if defined(AST_DEVMODE)
70 int is_valid;
71 int code;
72 #endif /* AST_DEVMODE */
73
74 for (i = path_vars; i; i = i->next) {
75 if (strcmp(i->name, "configClass") == 0) {
76 args.config_class = (i->value);
77 } else
78 if (strcmp(i->name, "objectType") == 0) {
79 args.object_type = (i->value);
80 } else
81 if (strcmp(i->name, "id") == 0) {
82 args.id = (i->value);
83 } else
84 {}
85 }
86 ast_ari_asterisk_get_object(headers, &args, response);
87 #if defined(AST_DEVMODE)
88 code = response->response_code;
89
90 switch (code) {
91 case 0: /* Implementation is still a stub, or the code wasn't set */
92 is_valid = response->message == NULL;
93 break;
94 case 500: /* Internal Server Error */
95 case 501: /* Not Implemented */
96 case 404: /* {configClass|objectType|id} not found */
97 is_valid = 1;
98 break;
99 default:
100 if (200 <= code && code <= 299) {
101 is_valid = ast_ari_validate_list(response->message,
102 ast_ari_validate_config_tuple_fn());
103 } else {
104 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/config/dynamic/{configClass}/{objectType}/{id}\n", code);
105 is_valid = 0;
106 }
107 }
108
109 if (!is_valid) {
110 ast_log(LOG_ERROR, "Response validation failed for /asterisk/config/dynamic/{configClass}/{objectType}/{id}\n");
111 ast_ari_response_error(response, 500,
112 "Internal Server Error", "Response validation failed");
113 }
114 #endif /* AST_DEVMODE */
115
116 fin: __attribute__((unused))
117 return;
118 }
119 int ast_ari_asterisk_update_object_parse_body(
120 struct ast_json *body,
121 struct ast_ari_asterisk_update_object_args *args)
122 {
123 /* Parse query parameters out of it */
124 return 0;
125 }
126
127 /*!
128 * \brief Parameter parsing callback for /asterisk/config/dynamic/{configClass}/{objectType}/{id}.
129 * \param ser TCP/TLS session object
130 * \param get_params GET parameters in the HTTP request.
131 * \param path_vars Path variables extracted from the request.
132 * \param headers HTTP headers.
133 * \param body
134 * \param[out] response Response to the HTTP request.
135 */
136 static void ast_ari_asterisk_update_object_cb(
137 struct ast_tcptls_session_instance *ser,
138 struct ast_variable *get_params, struct ast_variable *path_vars,
139 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
140 {
141 struct ast_ari_asterisk_update_object_args args = {};
142 struct ast_variable *i;
143 #if defined(AST_DEVMODE)
144 int is_valid;
145 int code;
146 #endif /* AST_DEVMODE */
147
148 for (i = path_vars; i; i = i->next) {
149 if (strcmp(i->name, "configClass") == 0) {
150 args.config_class = (i->value);
151 } else
152 if (strcmp(i->name, "objectType") == 0) {
153 args.object_type = (i->value);
154 } else
155 if (strcmp(i->name, "id") == 0) {
156 args.id = (i->value);
157 } else
158 {}
159 }
160 args.fields = body;
161 ast_ari_asterisk_update_object(headers, &args, response);
162 #if defined(AST_DEVMODE)
163 code = response->response_code;
164
165 switch (code) {
166 case 0: /* Implementation is still a stub, or the code wasn't set */
167 is_valid = response->message == NULL;
168 break;
169 case 500: /* Internal Server Error */
170 case 501: /* Not Implemented */
171 case 400: /* Bad request body */
172 case 403: /* Could not create or update object */
173 case 404: /* {configClass|objectType} not found */
174 is_valid = 1;
175 break;
176 default:
177 if (200 <= code && code <= 299) {
178 is_valid = ast_ari_validate_list(response->message,
179 ast_ari_validate_config_tuple_fn());
180 } else {
181 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/config/dynamic/{configClass}/{objectType}/{id}\n", code);
182 is_valid = 0;
183 }
184 }
185
186 if (!is_valid) {
187 ast_log(LOG_ERROR, "Response validation failed for /asterisk/config/dynamic/{configClass}/{objectType}/{id}\n");
188 ast_ari_response_error(response, 500,
189 "Internal Server Error", "Response validation failed");
190 }
191 #endif /* AST_DEVMODE */
192
193 fin: __attribute__((unused))
194 return;
195 }
196 /*!
197 * \brief Parameter parsing callback for /asterisk/config/dynamic/{configClass}/{objectType}/{id}.
198 * \param ser TCP/TLS session object
199 * \param get_params GET parameters in the HTTP request.
200 * \param path_vars Path variables extracted from the request.
201 * \param headers HTTP headers.
202 * \param body
203 * \param[out] response Response to the HTTP request.
204 */
205 static void ast_ari_asterisk_delete_object_cb(
206 struct ast_tcptls_session_instance *ser,
207 struct ast_variable *get_params, struct ast_variable *path_vars,
208 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
209 {
210 struct ast_ari_asterisk_delete_object_args args = {};
211 struct ast_variable *i;
212 #if defined(AST_DEVMODE)
213 int is_valid;
214 int code;
215 #endif /* AST_DEVMODE */
216
217 for (i = path_vars; i; i = i->next) {
218 if (strcmp(i->name, "configClass") == 0) {
219 args.config_class = (i->value);
220 } else
221 if (strcmp(i->name, "objectType") == 0) {
222 args.object_type = (i->value);
223 } else
224 if (strcmp(i->name, "id") == 0) {
225 args.id = (i->value);
226 } else
227 {}
228 }
229 ast_ari_asterisk_delete_object(headers, &args, response);
230 #if defined(AST_DEVMODE)
231 code = response->response_code;
232
233 switch (code) {
234 case 0: /* Implementation is still a stub, or the code wasn't set */
235 is_valid = response->message == NULL;
236 break;
237 case 500: /* Internal Server Error */
238 case 501: /* Not Implemented */
239 case 403: /* Could not delete object */
240 case 404: /* {configClass|objectType|id} not found */
241 is_valid = 1;
242 break;
243 default:
244 if (200 <= code && code <= 299) {
245 is_valid = ast_ari_validate_void(
246 response->message);
247 } else {
248 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/config/dynamic/{configClass}/{objectType}/{id}\n", code);
249 is_valid = 0;
250 }
251 }
252
253 if (!is_valid) {
254 ast_log(LOG_ERROR, "Response validation failed for /asterisk/config/dynamic/{configClass}/{objectType}/{id}\n");
255 ast_ari_response_error(response, 500,
256 "Internal Server Error", "Response validation failed");
257 }
258 #endif /* AST_DEVMODE */
259
260 fin: __attribute__((unused))
261 return;
262 }
263 int ast_ari_asterisk_get_info_parse_body(
264 struct ast_json *body,
265 struct ast_ari_asterisk_get_info_args *args)
266 {
267 struct ast_json *field;
268 /* Parse query parameters out of it */
269 field = ast_json_object_get(body, "only");
270 if (field) {
271 /* If they were silly enough to both pass in a query param and a
272 * JSON body, free up the query value.
273 */
274 ast_free(args->only);
275 if (ast_json_typeof(field) == AST_JSON_ARRAY) {
276 /* Multiple param passed as array */
277 size_t i;
278 args->only_count = ast_json_array_size(field);
279 args->only = ast_malloc(sizeof(*args->only) * args->only_count);
280
281 if (!args->only) {
282 return -1;
283 }
284
285 for (i = 0; i < args->only_count; ++i) {
286 args->only[i] = ast_json_string_get(ast_json_array_get(field, i));
287 }
288 } else {
289 /* Multiple param passed as single value */
290 args->only_count = 1;
291 args->only = ast_malloc(sizeof(*args->only) * args->only_count);
292 if (!args->only) {
293 return -1;
294 }
295 args->only[0] = ast_json_string_get(field);
296 }
297 }
298 return 0;
299 }
300
301 /*!
302 * \brief Parameter parsing callback for /asterisk/info.
303 * \param ser TCP/TLS session object
304 * \param get_params GET parameters in the HTTP request.
305 * \param path_vars Path variables extracted from the request.
306 * \param headers HTTP headers.
307 * \param body
308 * \param[out] response Response to the HTTP request.
309 */
310 static void ast_ari_asterisk_get_info_cb(
311 struct ast_tcptls_session_instance *ser,
312 struct ast_variable *get_params, struct ast_variable *path_vars,
313 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
314 {
315 struct ast_ari_asterisk_get_info_args args = {};
316 struct ast_variable *i;
317 #if defined(AST_DEVMODE)
318 int is_valid;
319 int code;
320 #endif /* AST_DEVMODE */
321
322 for (i = get_params; i; i = i->next) {
323 if (strcmp(i->name, "only") == 0) {
324 /* Parse comma separated list */
325 char *vals[MAX_VALS];
326 size_t j;
327
328 args.only_parse = ast_strdup(i->value);
329 if (!args.only_parse) {
330 ast_ari_response_alloc_failed(response);
331 goto fin;
332 }
333
334 if (strlen(args.only_parse) == 0) {
335 /* ast_app_separate_args can't handle "" */
336 args.only_count = 1;
337 vals[0] = args.only_parse;
338 } else {
339 args.only_count = ast_app_separate_args(
340 args.only_parse, ',', vals,
341 ARRAY_LEN(vals));
342 }
343
344 if (args.only_count == 0) {
345 ast_ari_response_alloc_failed(response);
346 goto fin;
347 }
348
349 if (args.only_count >= MAX_VALS) {
350 ast_ari_response_error(response, 400,
351 "Bad Request",
352 "Too many values for only");
353 goto fin;
354 }
355
356 args.only = ast_malloc(sizeof(*args.only) * args.only_count);
357 if (!args.only) {
358 ast_ari_response_alloc_failed(response);
359 goto fin;
360 }
361
362 for (j = 0; j < args.only_count; ++j) {
363 args.only[j] = (vals[j]);
364 }
365 } else
366 {}
367 }
368 if (ast_ari_asterisk_get_info_parse_body(body, &args)) {
369 ast_ari_response_alloc_failed(response);
370 goto fin;
371 }
372 ast_ari_asterisk_get_info(headers, &args, response);
373 #if defined(AST_DEVMODE)
374 code = response->response_code;
375
376 switch (code) {
377 case 0: /* Implementation is still a stub, or the code wasn't set */
378 is_valid = response->message == NULL;
379 break;
380 case 500: /* Internal Server Error */
381 case 501: /* Not Implemented */
382 is_valid = 1;
383 break;
384 default:
385 if (200 <= code && code <= 299) {
386 is_valid = ast_ari_validate_asterisk_info(
387 response->message);
388 } else {
389 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/info\n", code);
390 is_valid = 0;
391 }
392 }
393
394 if (!is_valid) {
395 ast_log(LOG_ERROR, "Response validation failed for /asterisk/info\n");
396 ast_ari_response_error(response, 500,
397 "Internal Server Error", "Response validation failed");
398 }
399 #endif /* AST_DEVMODE */
400
401 fin: __attribute__((unused))
402 ast_free(args.only_parse);
403 ast_free(args.only);
404 return;
405 }
406 /*!
407 * \brief Parameter parsing callback for /asterisk/ping.
408 * \param ser TCP/TLS session object
409 * \param get_params GET parameters in the HTTP request.
410 * \param path_vars Path variables extracted from the request.
411 * \param headers HTTP headers.
412 * \param body
413 * \param[out] response Response to the HTTP request.
414 */
415 static void ast_ari_asterisk_ping_cb(
416 struct ast_tcptls_session_instance *ser,
417 struct ast_variable *get_params, struct ast_variable *path_vars,
418 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
419 {
420 struct ast_ari_asterisk_ping_args args = {};
421 #if defined(AST_DEVMODE)
422 int is_valid;
423 int code;
424 #endif /* AST_DEVMODE */
425
426 ast_ari_asterisk_ping(headers, &args, response);
427 #if defined(AST_DEVMODE)
428 code = response->response_code;
429
430 switch (code) {
431 case 0: /* Implementation is still a stub, or the code wasn't set */
432 is_valid = response->message == NULL;
433 break;
434 case 500: /* Internal Server Error */
435 case 501: /* Not Implemented */
436 is_valid = 1;
437 break;
438 default:
439 if (200 <= code && code <= 299) {
440 is_valid = ast_ari_validate_asterisk_ping(
441 response->message);
442 } else {
443 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/ping\n", code);
444 is_valid = 0;
445 }
446 }
447
448 if (!is_valid) {
449 ast_log(LOG_ERROR, "Response validation failed for /asterisk/ping\n");
450 ast_ari_response_error(response, 500,
451 "Internal Server Error", "Response validation failed");
452 }
453 #endif /* AST_DEVMODE */
454
455 fin: __attribute__((unused))
456 return;
457 }
458 /*!
459 * \brief Parameter parsing callback for /asterisk/modules.
460 * \param ser TCP/TLS session object
461 * \param get_params GET parameters in the HTTP request.
462 * \param path_vars Path variables extracted from the request.
463 * \param headers HTTP headers.
464 * \param body
465 * \param[out] response Response to the HTTP request.
466 */
467 static void ast_ari_asterisk_list_modules_cb(
468 struct ast_tcptls_session_instance *ser,
469 struct ast_variable *get_params, struct ast_variable *path_vars,
470 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
471 {
472 struct ast_ari_asterisk_list_modules_args args = {};
473 #if defined(AST_DEVMODE)
474 int is_valid;
475 int code;
476 #endif /* AST_DEVMODE */
477
478 ast_ari_asterisk_list_modules(headers, &args, response);
479 #if defined(AST_DEVMODE)
480 code = response->response_code;
481
482 switch (code) {
483 case 0: /* Implementation is still a stub, or the code wasn't set */
484 is_valid = response->message == NULL;
485 break;
486 case 500: /* Internal Server Error */
487 case 501: /* Not Implemented */
488 is_valid = 1;
489 break;
490 default:
491 if (200 <= code && code <= 299) {
492 is_valid = ast_ari_validate_list(response->message,
493 ast_ari_validate_module_fn());
494 } else {
495 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/modules\n", code);
496 is_valid = 0;
497 }
498 }
499
500 if (!is_valid) {
501 ast_log(LOG_ERROR, "Response validation failed for /asterisk/modules\n");
502 ast_ari_response_error(response, 500,
503 "Internal Server Error", "Response validation failed");
504 }
505 #endif /* AST_DEVMODE */
506
507 fin: __attribute__((unused))
508 return;
509 }
510 /*!
511 * \brief Parameter parsing callback for /asterisk/modules/{moduleName}.
512 * \param ser TCP/TLS session object
513 * \param get_params GET parameters in the HTTP request.
514 * \param path_vars Path variables extracted from the request.
515 * \param headers HTTP headers.
516 * \param body
517 * \param[out] response Response to the HTTP request.
518 */
519 static void ast_ari_asterisk_get_module_cb(
520 struct ast_tcptls_session_instance *ser,
521 struct ast_variable *get_params, struct ast_variable *path_vars,
522 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
523 {
524 struct ast_ari_asterisk_get_module_args args = {};
525 struct ast_variable *i;
526 #if defined(AST_DEVMODE)
527 int is_valid;
528 int code;
529 #endif /* AST_DEVMODE */
530
531 for (i = path_vars; i; i = i->next) {
532 if (strcmp(i->name, "moduleName") == 0) {
533 args.module_name = (i->value);
534 } else
535 {}
536 }
537 ast_ari_asterisk_get_module(headers, &args, response);
538 #if defined(AST_DEVMODE)
539 code = response->response_code;
540
541 switch (code) {
542 case 0: /* Implementation is still a stub, or the code wasn't set */
543 is_valid = response->message == NULL;
544 break;
545 case 500: /* Internal Server Error */
546 case 501: /* Not Implemented */
547 case 404: /* Module could not be found in running modules. */
548 case 409: /* Module information could not be retrieved. */
549 is_valid = 1;
550 break;
551 default:
552 if (200 <= code && code <= 299) {
553 is_valid = ast_ari_validate_module(
554 response->message);
555 } else {
556 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/modules/{moduleName}\n", code);
557 is_valid = 0;
558 }
559 }
560
561 if (!is_valid) {
562 ast_log(LOG_ERROR, "Response validation failed for /asterisk/modules/{moduleName}\n");
563 ast_ari_response_error(response, 500,
564 "Internal Server Error", "Response validation failed");
565 }
566 #endif /* AST_DEVMODE */
567
568 fin: __attribute__((unused))
569 return;
570 }
571 /*!
572 * \brief Parameter parsing callback for /asterisk/modules/{moduleName}.
573 * \param ser TCP/TLS session object
574 * \param get_params GET parameters in the HTTP request.
575 * \param path_vars Path variables extracted from the request.
576 * \param headers HTTP headers.
577 * \param body
578 * \param[out] response Response to the HTTP request.
579 */
580 static void ast_ari_asterisk_load_module_cb(
581 struct ast_tcptls_session_instance *ser,
582 struct ast_variable *get_params, struct ast_variable *path_vars,
583 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
584 {
585 struct ast_ari_asterisk_load_module_args args = {};
586 struct ast_variable *i;
587 #if defined(AST_DEVMODE)
588 int is_valid;
589 int code;
590 #endif /* AST_DEVMODE */
591
592 for (i = path_vars; i; i = i->next) {
593 if (strcmp(i->name, "moduleName") == 0) {
594 args.module_name = (i->value);
595 } else
596 {}
597 }
598 ast_ari_asterisk_load_module(headers, &args, response);
599 #if defined(AST_DEVMODE)
600 code = response->response_code;
601
602 switch (code) {
603 case 0: /* Implementation is still a stub, or the code wasn't set */
604 is_valid = response->message == NULL;
605 break;
606 case 500: /* Internal Server Error */
607 case 501: /* Not Implemented */
608 case 409: /* Module could not be loaded. */
609 is_valid = 1;
610 break;
611 default:
612 if (200 <= code && code <= 299) {
613 is_valid = ast_ari_validate_void(
614 response->message);
615 } else {
616 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/modules/{moduleName}\n", code);
617 is_valid = 0;
618 }
619 }
620
621 if (!is_valid) {
622 ast_log(LOG_ERROR, "Response validation failed for /asterisk/modules/{moduleName}\n");
623 ast_ari_response_error(response, 500,
624 "Internal Server Error", "Response validation failed");
625 }
626 #endif /* AST_DEVMODE */
627
628 fin: __attribute__((unused))
629 return;
630 }
631 /*!
632 * \brief Parameter parsing callback for /asterisk/modules/{moduleName}.
633 * \param ser TCP/TLS session object
634 * \param get_params GET parameters in the HTTP request.
635 * \param path_vars Path variables extracted from the request.
636 * \param headers HTTP headers.
637 * \param body
638 * \param[out] response Response to the HTTP request.
639 */
640 static void ast_ari_asterisk_unload_module_cb(
641 struct ast_tcptls_session_instance *ser,
642 struct ast_variable *get_params, struct ast_variable *path_vars,
643 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
644 {
645 struct ast_ari_asterisk_unload_module_args args = {};
646 struct ast_variable *i;
647 #if defined(AST_DEVMODE)
648 int is_valid;
649 int code;
650 #endif /* AST_DEVMODE */
651
652 for (i = path_vars; i; i = i->next) {
653 if (strcmp(i->name, "moduleName") == 0) {
654 args.module_name = (i->value);
655 } else
656 {}
657 }
658 ast_ari_asterisk_unload_module(headers, &args, response);
659 #if defined(AST_DEVMODE)
660 code = response->response_code;
661
662 switch (code) {
663 case 0: /* Implementation is still a stub, or the code wasn't set */
664 is_valid = response->message == NULL;
665 break;
666 case 500: /* Internal Server Error */
667 case 501: /* Not Implemented */
668 case 404: /* Module not found in running modules. */
669 case 409: /* Module could not be unloaded. */
670 is_valid = 1;
671 break;
672 default:
673 if (200 <= code && code <= 299) {
674 is_valid = ast_ari_validate_void(
675 response->message);
676 } else {
677 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/modules/{moduleName}\n", code);
678 is_valid = 0;
679 }
680 }
681
682 if (!is_valid) {
683 ast_log(LOG_ERROR, "Response validation failed for /asterisk/modules/{moduleName}\n");
684 ast_ari_response_error(response, 500,
685 "Internal Server Error", "Response validation failed");
686 }
687 #endif /* AST_DEVMODE */
688
689 fin: __attribute__((unused))
690 return;
691 }
692 /*!
693 * \brief Parameter parsing callback for /asterisk/modules/{moduleName}.
694 * \param ser TCP/TLS session object
695 * \param get_params GET parameters in the HTTP request.
696 * \param path_vars Path variables extracted from the request.
697 * \param headers HTTP headers.
698 * \param body
699 * \param[out] response Response to the HTTP request.
700 */
701 static void ast_ari_asterisk_reload_module_cb(
702 struct ast_tcptls_session_instance *ser,
703 struct ast_variable *get_params, struct ast_variable *path_vars,
704 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
705 {
706 struct ast_ari_asterisk_reload_module_args args = {};
707 struct ast_variable *i;
708 #if defined(AST_DEVMODE)
709 int is_valid;
710 int code;
711 #endif /* AST_DEVMODE */
712
713 for (i = path_vars; i; i = i->next) {
714 if (strcmp(i->name, "moduleName") == 0) {
715 args.module_name = (i->value);
716 } else
717 {}
718 }
719 ast_ari_asterisk_reload_module(headers, &args, response);
720 #if defined(AST_DEVMODE)
721 code = response->response_code;
722
723 switch (code) {
724 case 0: /* Implementation is still a stub, or the code wasn't set */
725 is_valid = response->message == NULL;
726 break;
727 case 500: /* Internal Server Error */
728 case 501: /* Not Implemented */
729 case 404: /* Module not found in running modules. */
730 case 409: /* Module could not be reloaded. */
731 is_valid = 1;
732 break;
733 default:
734 if (200 <= code && code <= 299) {
735 is_valid = ast_ari_validate_void(
736 response->message);
737 } else {
738 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/modules/{moduleName}\n", code);
739 is_valid = 0;
740 }
741 }
742
743 if (!is_valid) {
744 ast_log(LOG_ERROR, "Response validation failed for /asterisk/modules/{moduleName}\n");
745 ast_ari_response_error(response, 500,
746 "Internal Server Error", "Response validation failed");
747 }
748 #endif /* AST_DEVMODE */
749
750 fin: __attribute__((unused))
751 return;
752 }
753 /*!
754 * \brief Parameter parsing callback for /asterisk/logging.
755 * \param ser TCP/TLS session object
756 * \param get_params GET parameters in the HTTP request.
757 * \param path_vars Path variables extracted from the request.
758 * \param headers HTTP headers.
759 * \param body
760 * \param[out] response Response to the HTTP request.
761 */
762 static void ast_ari_asterisk_list_log_channels_cb(
763 struct ast_tcptls_session_instance *ser,
764 struct ast_variable *get_params, struct ast_variable *path_vars,
765 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
766 {
767 struct ast_ari_asterisk_list_log_channels_args args = {};
768 #if defined(AST_DEVMODE)
769 int is_valid;
770 int code;
771 #endif /* AST_DEVMODE */
772
773 ast_ari_asterisk_list_log_channels(headers, &args, response);
774 #if defined(AST_DEVMODE)
775 code = response->response_code;
776
777 switch (code) {
778 case 0: /* Implementation is still a stub, or the code wasn't set */
779 is_valid = response->message == NULL;
780 break;
781 case 500: /* Internal Server Error */
782 case 501: /* Not Implemented */
783 is_valid = 1;
784 break;
785 default:
786 if (200 <= code && code <= 299) {
787 is_valid = ast_ari_validate_list(response->message,
788 ast_ari_validate_log_channel_fn());
789 } else {
790 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/logging\n", code);
791 is_valid = 0;
792 }
793 }
794
795 if (!is_valid) {
796 ast_log(LOG_ERROR, "Response validation failed for /asterisk/logging\n");
797 ast_ari_response_error(response, 500,
798 "Internal Server Error", "Response validation failed");
799 }
800 #endif /* AST_DEVMODE */
801
802 fin: __attribute__((unused))
803 return;
804 }
805 int ast_ari_asterisk_add_log_parse_body(
806 struct ast_json *body,
807 struct ast_ari_asterisk_add_log_args *args)
808 {
809 struct ast_json *field;
810 /* Parse query parameters out of it */
811 field = ast_json_object_get(body, "configuration");
812 if (field) {
813 args->configuration = ast_json_string_get(field);
814 }
815 return 0;
816 }
817
818 /*!
819 * \brief Parameter parsing callback for /asterisk/logging/{logChannelName}.
820 * \param ser TCP/TLS session object
821 * \param get_params GET parameters in the HTTP request.
822 * \param path_vars Path variables extracted from the request.
823 * \param headers HTTP headers.
824 * \param body
825 * \param[out] response Response to the HTTP request.
826 */
827 static void ast_ari_asterisk_add_log_cb(
828 struct ast_tcptls_session_instance *ser,
829 struct ast_variable *get_params, struct ast_variable *path_vars,
830 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
831 {
832 struct ast_ari_asterisk_add_log_args args = {};
833 struct ast_variable *i;
834 #if defined(AST_DEVMODE)
835 int is_valid;
836 int code;
837 #endif /* AST_DEVMODE */
838
839 for (i = get_params; i; i = i->next) {
840 if (strcmp(i->name, "configuration") == 0) {
841 args.configuration = (i->value);
842 } else
843 {}
844 }
845 for (i = path_vars; i; i = i->next) {
846 if (strcmp(i->name, "logChannelName") == 0) {
847 args.log_channel_name = (i->value);
848 } else
849 {}
850 }
851 if (ast_ari_asterisk_add_log_parse_body(body, &args)) {
852 ast_ari_response_alloc_failed(response);
853 goto fin;
854 }
855 ast_ari_asterisk_add_log(headers, &args, response);
856 #if defined(AST_DEVMODE)
857 code = response->response_code;
858
859 switch (code) {
860 case 0: /* Implementation is still a stub, or the code wasn't set */
861 is_valid = response->message == NULL;
862 break;
863 case 500: /* Internal Server Error */
864 case 501: /* Not Implemented */
865 case 400: /* Bad request body */
866 case 409: /* Log channel could not be created. */
867 is_valid = 1;
868 break;
869 default:
870 if (200 <= code && code <= 299) {
871 is_valid = ast_ari_validate_void(
872 response->message);
873 } else {
874 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/logging/{logChannelName}\n", code);
875 is_valid = 0;
876 }
877 }
878
879 if (!is_valid) {
880 ast_log(LOG_ERROR, "Response validation failed for /asterisk/logging/{logChannelName}\n");
881 ast_ari_response_error(response, 500,
882 "Internal Server Error", "Response validation failed");
883 }
884 #endif /* AST_DEVMODE */
885
886 fin: __attribute__((unused))
887 return;
888 }
889 /*!
890 * \brief Parameter parsing callback for /asterisk/logging/{logChannelName}.
891 * \param ser TCP/TLS session object
892 * \param get_params GET parameters in the HTTP request.
893 * \param path_vars Path variables extracted from the request.
894 * \param headers HTTP headers.
895 * \param body
896 * \param[out] response Response to the HTTP request.
897 */
898 static void ast_ari_asterisk_delete_log_cb(
899 struct ast_tcptls_session_instance *ser,
900 struct ast_variable *get_params, struct ast_variable *path_vars,
901 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
902 {
903 struct ast_ari_asterisk_delete_log_args args = {};
904 struct ast_variable *i;
905 #if defined(AST_DEVMODE)
906 int is_valid;
907 int code;
908 #endif /* AST_DEVMODE */
909
910 for (i = path_vars; i; i = i->next) {
911 if (strcmp(i->name, "logChannelName") == 0) {
912 args.log_channel_name = (i->value);
913 } else
914 {}
915 }
916 ast_ari_asterisk_delete_log(headers, &args, response);
917 #if defined(AST_DEVMODE)
918 code = response->response_code;
919
920 switch (code) {
921 case 0: /* Implementation is still a stub, or the code wasn't set */
922 is_valid = response->message == NULL;
923 break;
924 case 500: /* Internal Server Error */
925 case 501: /* Not Implemented */
926 case 404: /* Log channel does not exist. */
927 is_valid = 1;
928 break;
929 default:
930 if (200 <= code && code <= 299) {
931 is_valid = ast_ari_validate_void(
932 response->message);
933 } else {
934 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/logging/{logChannelName}\n", code);
935 is_valid = 0;
936 }
937 }
938
939 if (!is_valid) {
940 ast_log(LOG_ERROR, "Response validation failed for /asterisk/logging/{logChannelName}\n");
941 ast_ari_response_error(response, 500,
942 "Internal Server Error", "Response validation failed");
943 }
944 #endif /* AST_DEVMODE */
945
946 fin: __attribute__((unused))
947 return;
948 }
949 /*!
950 * \brief Parameter parsing callback for /asterisk/logging/{logChannelName}/rotate.
951 * \param ser TCP/TLS session object
952 * \param get_params GET parameters in the HTTP request.
953 * \param path_vars Path variables extracted from the request.
954 * \param headers HTTP headers.
955 * \param body
956 * \param[out] response Response to the HTTP request.
957 */
958 static void ast_ari_asterisk_rotate_log_cb(
959 struct ast_tcptls_session_instance *ser,
960 struct ast_variable *get_params, struct ast_variable *path_vars,
961 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
962 {
963 struct ast_ari_asterisk_rotate_log_args args = {};
964 struct ast_variable *i;
965 #if defined(AST_DEVMODE)
966 int is_valid;
967 int code;
968 #endif /* AST_DEVMODE */
969
970 for (i = path_vars; i; i = i->next) {
971 if (strcmp(i->name, "logChannelName") == 0) {
972 args.log_channel_name = (i->value);
973 } else
974 {}
975 }
976 ast_ari_asterisk_rotate_log(headers, &args, response);
977 #if defined(AST_DEVMODE)
978 code = response->response_code;
979
980 switch (code) {
981 case 0: /* Implementation is still a stub, or the code wasn't set */
982 is_valid = response->message == NULL;
983 break;
984 case 500: /* Internal Server Error */
985 case 501: /* Not Implemented */
986 case 404: /* Log channel does not exist. */
987 is_valid = 1;
988 break;
989 default:
990 if (200 <= code && code <= 299) {
991 is_valid = ast_ari_validate_void(
992 response->message);
993 } else {
994 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/logging/{logChannelName}/rotate\n", code);
995 is_valid = 0;
996 }
997 }
998
999 if (!is_valid) {
1000 ast_log(LOG_ERROR, "Response validation failed for /asterisk/logging/{logChannelName}/rotate\n");
1001 ast_ari_response_error(response, 500,
1002 "Internal Server Error", "Response validation failed");
1003 }
1004 #endif /* AST_DEVMODE */
1005
1006 fin: __attribute__((unused))
1007 return;
1008 }
1009 int ast_ari_asterisk_get_global_var_parse_body(
1010 struct ast_json *body,
1011 struct ast_ari_asterisk_get_global_var_args *args)
1012 {
1013 struct ast_json *field;
1014 /* Parse query parameters out of it */
1015 field = ast_json_object_get(body, "variable");
1016 if (field) {
1017 args->variable = ast_json_string_get(field);
1018 }
1019 return 0;
1020 }
1021
1022 /*!
1023 * \brief Parameter parsing callback for /asterisk/variable.
1024 * \param ser TCP/TLS session object
1025 * \param get_params GET parameters in the HTTP request.
1026 * \param path_vars Path variables extracted from the request.
1027 * \param headers HTTP headers.
1028 * \param body
1029 * \param[out] response Response to the HTTP request.
1030 */
1031 static void ast_ari_asterisk_get_global_var_cb(
1032 struct ast_tcptls_session_instance *ser,
1033 struct ast_variable *get_params, struct ast_variable *path_vars,
1034 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
1035 {
1036 struct ast_ari_asterisk_get_global_var_args args = {};
1037 struct ast_variable *i;
1038 #if defined(AST_DEVMODE)
1039 int is_valid;
1040 int code;
1041 #endif /* AST_DEVMODE */
1042
1043 for (i = get_params; i; i = i->next) {
1044 if (strcmp(i->name, "variable") == 0) {
1045 args.variable = (i->value);
1046 } else
1047 {}
1048 }
1049 if (ast_ari_asterisk_get_global_var_parse_body(body, &args)) {
1050 ast_ari_response_alloc_failed(response);
1051 goto fin;
1052 }
1053 ast_ari_asterisk_get_global_var(headers, &args, response);
1054 #if defined(AST_DEVMODE)
1055 code = response->response_code;
1056
1057 switch (code) {
1058 case 0: /* Implementation is still a stub, or the code wasn't set */
1059 is_valid = response->message == NULL;
1060 break;
1061 case 500: /* Internal Server Error */
1062 case 501: /* Not Implemented */
1063 case 400: /* Missing variable parameter. */
1064 is_valid = 1;
1065 break;
1066 default:
1067 if (200 <= code && code <= 299) {
1068 is_valid = ast_ari_validate_variable(
1069 response->message);
1070 } else {
1071 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/variable\n", code);
1072 is_valid = 0;
1073 }
1074 }
1075
1076 if (!is_valid) {
1077 ast_log(LOG_ERROR, "Response validation failed for /asterisk/variable\n");
1078 ast_ari_response_error(response, 500,
1079 "Internal Server Error", "Response validation failed");
1080 }
1081 #endif /* AST_DEVMODE */
1082
1083 fin: __attribute__((unused))
1084 return;
1085 }
1086 int ast_ari_asterisk_set_global_var_parse_body(
1087 struct ast_json *body,
1088 struct ast_ari_asterisk_set_global_var_args *args)
1089 {
1090 struct ast_json *field;
1091 /* Parse query parameters out of it */
1092 field = ast_json_object_get(body, "variable");
1093 if (field) {
1094 args->variable = ast_json_string_get(field);
1095 }
1096 field = ast_json_object_get(body, "value");
1097 if (field) {
1098 args->value = ast_json_string_get(field);
1099 }
1100 return 0;
1101 }
1102
1103 /*!
1104 * \brief Parameter parsing callback for /asterisk/variable.
1105 * \param ser TCP/TLS session object
1106 * \param get_params GET parameters in the HTTP request.
1107 * \param path_vars Path variables extracted from the request.
1108 * \param headers HTTP headers.
1109 * \param body
1110 * \param[out] response Response to the HTTP request.
1111 */
1112 static void ast_ari_asterisk_set_global_var_cb(
1113 struct ast_tcptls_session_instance *ser,
1114 struct ast_variable *get_params, struct ast_variable *path_vars,
1115 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
1116 {
1117 struct ast_ari_asterisk_set_global_var_args args = {};
1118 struct ast_variable *i;
1119 #if defined(AST_DEVMODE)
1120 int is_valid;
1121 int code;
1122 #endif /* AST_DEVMODE */
1123
1124 for (i = get_params; i; i = i->next) {
1125 if (strcmp(i->name, "variable") == 0) {
1126 args.variable = (i->value);
1127 } else
1128 if (strcmp(i->name, "value") == 0) {
1129 args.value = (i->value);
1130 } else
1131 {}
1132 }
1133 if (ast_ari_asterisk_set_global_var_parse_body(body, &args)) {
1134 ast_ari_response_alloc_failed(response);
1135 goto fin;
1136 }
1137 ast_ari_asterisk_set_global_var(headers, &args, response);
1138 #if defined(AST_DEVMODE)
1139 code = response->response_code;
1140
1141 switch (code) {
1142 case 0: /* Implementation is still a stub, or the code wasn't set */
1143 is_valid = response->message == NULL;
1144 break;
1145 case 500: /* Internal Server Error */
1146 case 501: /* Not Implemented */
1147 case 400: /* Missing variable parameter. */
1148 is_valid = 1;
1149 break;
1150 default:
1151 if (200 <= code && code <= 299) {
1152 is_valid = ast_ari_validate_void(
1153 response->message);
1154 } else {
1155 ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/variable\n", code);
1156 is_valid = 0;
1157 }
1158 }
1159
1160 if (!is_valid) {
1161 ast_log(LOG_ERROR, "Response validation failed for /asterisk/variable\n");
1162 ast_ari_response_error(response, 500,
1163 "Internal Server Error", "Response validation failed");
1164 }
1165 #endif /* AST_DEVMODE */
1166
1167 fin: __attribute__((unused))
1168 return;
1169 }
1170
1171 /*! \brief REST handler for /api-docs/asterisk.json */
1172 static struct stasis_rest_handlers asterisk_config_dynamic_configClass_objectType_id = {
1173 .path_segment = "id",
1174 .is_wildcard = 1,
1175 .callbacks = {
1176 [AST_HTTP_GET] = ast_ari_asterisk_get_object_cb,
1177 [AST_HTTP_PUT] = ast_ari_asterisk_update_object_cb,
1178 [AST_HTTP_DELETE] = ast_ari_asterisk_delete_object_cb,
1179 },
1180 .num_children = 0,
1181 .children = { }
1182 };
1183 /*! \brief REST handler for /api-docs/asterisk.json */
1184 static struct stasis_rest_handlers asterisk_config_dynamic_configClass_objectType = {
1185 .path_segment = "objectType",
1186 .is_wildcard = 1,
1187 .callbacks = {
1188 },
1189 .num_children = 1,
1190 .children = { &asterisk_config_dynamic_configClass_objectType_id, }
1191 };
1192 /*! \brief REST handler for /api-docs/asterisk.json */
1193 static struct stasis_rest_handlers asterisk_config_dynamic_configClass = {
1194 .path_segment = "configClass",
1195 .is_wildcard = 1,
1196 .callbacks = {
1197 },
1198 .num_children = 1,
1199 .children = { &asterisk_config_dynamic_configClass_objectType, }
1200 };
1201 /*! \brief REST handler for /api-docs/asterisk.json */
1202 static struct stasis_rest_handlers asterisk_config_dynamic = {
1203 .path_segment = "dynamic",
1204 .callbacks = {
1205 },
1206 .num_children = 1,
1207 .children = { &asterisk_config_dynamic_configClass, }
1208 };
1209 /*! \brief REST handler for /api-docs/asterisk.json */
1210 static struct stasis_rest_handlers asterisk_config = {
1211 .path_segment = "config",
1212 .callbacks = {
1213 },
1214 .num_children = 1,
1215 .children = { &asterisk_config_dynamic, }
1216 };
1217 /*! \brief REST handler for /api-docs/asterisk.json */
1218 static struct stasis_rest_handlers asterisk_info = {
1219 .path_segment = "info",
1220 .callbacks = {
1221 [AST_HTTP_GET] = ast_ari_asterisk_get_info_cb,
1222 },
1223 .num_children = 0,
1224 .children = { }
1225 };
1226 /*! \brief REST handler for /api-docs/asterisk.json */
1227 static struct stasis_rest_handlers asterisk_ping = {
1228 .path_segment = "ping",
1229 .callbacks = {
1230 [AST_HTTP_GET] = ast_ari_asterisk_ping_cb,
1231 },
1232 .num_children = 0,
1233 .children = { }
1234 };
1235 /*! \brief REST handler for /api-docs/asterisk.json */
1236 static struct stasis_rest_handlers asterisk_modules_moduleName = {
1237 .path_segment = "moduleName",
1238 .is_wildcard = 1,
1239 .callbacks = {
1240 [AST_HTTP_GET] = ast_ari_asterisk_get_module_cb,
1241 [AST_HTTP_POST] = ast_ari_asterisk_load_module_cb,
1242 [AST_HTTP_DELETE] = ast_ari_asterisk_unload_module_cb,
1243 [AST_HTTP_PUT] = ast_ari_asterisk_reload_module_cb,
1244 },
1245 .num_children = 0,
1246 .children = { }
1247 };
1248 /*! \brief REST handler for /api-docs/asterisk.json */
1249 static struct stasis_rest_handlers asterisk_modules = {
1250 .path_segment = "modules",
1251 .callbacks = {
1252 [AST_HTTP_GET] = ast_ari_asterisk_list_modules_cb,
1253 },
1254 .num_children = 1,
1255 .children = { &asterisk_modules_moduleName, }
1256 };
1257 /*! \brief REST handler for /api-docs/asterisk.json */
1258 static struct stasis_rest_handlers asterisk_logging_logChannelName_rotate = {
1259 .path_segment = "rotate",
1260 .callbacks = {
1261 [AST_HTTP_PUT] = ast_ari_asterisk_rotate_log_cb,
1262 },
1263 .num_children = 0,
1264 .children = { }
1265 };
1266 /*! \brief REST handler for /api-docs/asterisk.json */
1267 static struct stasis_rest_handlers asterisk_logging_logChannelName = {
1268 .path_segment = "logChannelName",
1269 .is_wildcard = 1,
1270 .callbacks = {
1271 [AST_HTTP_POST] = ast_ari_asterisk_add_log_cb,
1272 [AST_HTTP_DELETE] = ast_ari_asterisk_delete_log_cb,
1273 },
1274 .num_children = 1,
1275 .children = { &asterisk_logging_logChannelName_rotate, }
1276 };
1277 /*! \brief REST handler for /api-docs/asterisk.json */
1278 static struct stasis_rest_handlers asterisk_logging = {
1279 .path_segment = "logging",
1280 .callbacks = {
1281 [AST_HTTP_GET] = ast_ari_asterisk_list_log_channels_cb,
1282 },
1283 .num_children = 1,
1284 .children = { &asterisk_logging_logChannelName, }
1285 };
1286 /*! \brief REST handler for /api-docs/asterisk.json */
1287 static struct stasis_rest_handlers asterisk_variable = {
1288 .path_segment = "variable",
1289 .callbacks = {
1290 [AST_HTTP_GET] = ast_ari_asterisk_get_global_var_cb,
1291 [AST_HTTP_POST] = ast_ari_asterisk_set_global_var_cb,
1292 },
1293 .num_children = 0,
1294 .children = { }
1295 };
1296 /*! \brief REST handler for /api-docs/asterisk.json */
1297 static struct stasis_rest_handlers asterisk = {
1298 .path_segment = "asterisk",
1299 .callbacks = {
1300 },
1301 .num_children = 6,
1302 .children = { &asterisk_config,&asterisk_info,&asterisk_ping,&asterisk_modules,&asterisk_logging,&asterisk_variable, }
1303 };
1304
1305 static int unload_module(void)
1306 {
1307 ast_ari_remove_handler(&asterisk);
1308 return 0;
1309 }
1310
1311 static int load_module(void)
1312 {
1313 int res = 0;
1314
1315
1316 res |= ast_ari_add_handler(&asterisk);
1317 if (res) {
1318 unload_module();
1319 return AST_MODULE_LOAD_DECLINE;
1320 }
1321
1322 return AST_MODULE_LOAD_SUCCESS;
1323 }
1324
1325 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Asterisk resources",
1326 .support_level = AST_MODULE_SUPPORT_CORE,
1327 .load = load_module,
1328 .unload = unload_module,
1329 .requires = "res_ari,res_ari_model,res_stasis",
1330 );