Merge "app_voicemail: Fix module unload leak."
[asterisk/asterisk.git] / res / ari / resource_asterisk.c
1 /* -*- C -*-
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
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 /*! \file
20  *
21  * \brief Implementation for ARI stubs.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/ast_version.h"
33 #include "asterisk/buildinfo.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/module.h"
36 #include "asterisk/paths.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/sorcery.h"
39 #include "resource_asterisk.h"
40
41 static void return_sorcery_object(struct ast_sorcery *sorcery, void *sorcery_obj,
42         struct ast_ari_response *response)
43 {
44         RAII_VAR(struct ast_json *, return_set, NULL, ast_json_unref);
45         struct ast_variable *change_set;
46         struct ast_variable *it_change_set;
47
48         return_set = ast_json_array_create();
49         if (!return_set) {
50                 ast_ari_response_alloc_failed(response);
51                 return;
52         }
53
54         /* Note that we can't use the sorcery JSON change set directly,
55          * as it will hand us back an Object (with fields), and we need
56          * a more generic representation of whatever the API call asked
57          * for, i.e., a list of tuples.
58          */
59         change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
60         if (!change_set) {
61                 ast_ari_response_alloc_failed(response);
62                 return;
63         }
64
65         for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
66                 struct ast_json *tuple;
67
68                 tuple = ast_json_pack("{s: s, s: s}",
69                         "attribute", it_change_set->name,
70                         "value", it_change_set->value);
71                 if (!tuple) {
72                         ast_variables_destroy(change_set);
73                         ast_ari_response_alloc_failed(response);
74                         return;
75                 }
76
77                 if (ast_json_array_append(return_set, tuple)) {
78                         ast_json_unref(tuple);
79                         ast_variables_destroy(change_set);
80                         ast_ari_response_alloc_failed(response);
81                         return;
82                 }
83         }
84         ast_variables_destroy(change_set);
85
86         ast_ari_response_ok(response, ast_json_ref(return_set));
87 }
88
89 void ast_ari_asterisk_get_object(struct ast_variable *headers,
90         struct ast_ari_asterisk_get_object_args *args,
91         struct ast_ari_response *response)
92 {
93         RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
94         RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
95         RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
96
97
98         sorcery = ast_sorcery_retrieve_by_module_name(args->config_class);
99         if (!sorcery) {
100                 ast_ari_response_error(
101                         response, 404, "Not Found",
102                         "configClass '%s' not found",
103                         args->config_class);
104                 return;
105         }
106
107         object_type = ast_sorcery_get_object_type(sorcery, args->object_type);
108         if (!object_type) {
109                 ast_ari_response_error(
110                         response, 404, "Not Found",
111                         "objectType '%s' not found",
112                         args->object_type);
113                 return;
114         }
115
116         sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args->object_type, args->id);
117         if (!sorcery_obj) {
118                 ast_ari_response_error(
119                         response, 404, "Not Found",
120                         "Object with id '%s' not found",
121                         args->id);
122                 return;
123         }
124
125         return_sorcery_object(sorcery, sorcery_obj, response);
126 }
127
128 void ast_ari_asterisk_update_object(struct ast_variable *headers, struct ast_ari_asterisk_update_object_args *args, struct ast_ari_response *response)
129 {
130         RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
131         RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
132         RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
133         struct ast_json *fields;
134         struct ast_variable *update_set = NULL;
135         int created = 0;
136
137         sorcery = ast_sorcery_retrieve_by_module_name(args->config_class);
138         if (!sorcery) {
139                 ast_ari_response_error(
140                         response, 404, "Not Found",
141                         "configClass '%s' not found",
142                         args->config_class);
143                 return;
144         }
145
146         object_type = ast_sorcery_get_object_type(sorcery, args->object_type);
147         if (!object_type) {
148                 ast_ari_response_error(
149                         response, 404, "Not Found",
150                         "objectType '%s' not found",
151                         args->object_type);
152                 return;
153         }
154
155         sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args->object_type, args->id);
156         if (!sorcery_obj) {
157                 ast_debug(5, "Sorcery object '%s' does not exist; creating it\n", args->id);
158                 sorcery_obj = ast_sorcery_alloc(sorcery, args->object_type, args->id);
159                 if (!sorcery_obj) {
160                         ast_ari_response_alloc_failed(response);
161                         return;
162                 }
163
164                 created = 1;
165         } else {
166                 void *copy;
167
168                 copy = ast_sorcery_copy(sorcery, sorcery_obj);
169                 if (!copy) {
170                         ast_ari_response_alloc_failed(response);
171                         return;
172                 }
173
174                 ao2_ref(sorcery_obj, -1);
175                 sorcery_obj = copy;
176         }
177
178         fields = ast_json_object_get(args->fields, "fields");
179         if (!fields && !created) {
180                 /* Whoops. We need data. */
181                 ast_ari_response_error(
182                         response, 400, "Bad request",
183                         "Fields must be provided to update object '%s'",
184                         args->id);
185                 return;
186         } else if (fields) {
187                 size_t i;
188
189                 for (i = 0; i < ast_json_array_size(fields); i++) {
190                         struct ast_variable *new_var;
191                         struct ast_json *json_value = ast_json_array_get(fields, i);
192
193                         if (!json_value) {
194                                 continue;
195                         }
196
197                         new_var = ast_variable_new(
198                                 ast_json_string_get(ast_json_object_get(json_value, "attribute")),
199                                 ast_json_string_get(ast_json_object_get(json_value, "value")),
200                                 "");
201                         if (!new_var) {
202                                 ast_variables_destroy(update_set);
203                                 ast_ari_response_alloc_failed(response);
204                                 return;
205                         }
206                         ast_variable_list_append(&update_set, new_var);
207                 }
208         }
209
210         /* APPLY! Note that a NULL update_set is fine (and necessary), as it
211          * will force validation on a newly created object.
212          */
213         if (ast_sorcery_objectset_apply(sorcery, sorcery_obj, update_set)) {
214                 ast_variables_destroy(update_set);
215                 ast_ari_response_error(
216                         response, 400, "Bad request",
217                         "%s of object '%s' failed field value validation",
218                         created ? "Creation" : "Update",
219                         args->id);
220                 return;
221         }
222
223         ast_variables_destroy(update_set);
224
225         if (created) {
226                 if (ast_sorcery_create(sorcery, sorcery_obj)) {
227                         ast_ari_response_error(
228                                 response, 403, "Forbidden",
229                                 "Cannot create sorcery objects of type '%s'",
230                                 args->object_type);
231                         return;
232                 }
233         } else {
234                 if (ast_sorcery_update(sorcery, sorcery_obj)) {
235                         ast_ari_response_error(
236                                 response, 403, "Forbidden",
237                                 "Cannot update sorcery objects of type '%s'",
238                                 args->object_type);
239                         return;
240                 }
241         }
242
243         return_sorcery_object(sorcery, sorcery_obj, response);
244 }
245
246
247 void ast_ari_asterisk_delete_object(struct ast_variable *headers,
248         struct ast_ari_asterisk_delete_object_args *args,
249         struct ast_ari_response *response)
250 {
251         RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
252         RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
253         RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
254
255         sorcery = ast_sorcery_retrieve_by_module_name(args->config_class);
256         if (!sorcery) {
257                 ast_ari_response_error(
258                         response, 404, "Not Found",
259                         "configClass '%s' not found",
260                         args->config_class);
261                 return;
262         }
263
264         object_type = ast_sorcery_get_object_type(sorcery, args->object_type);
265         if (!object_type) {
266                 ast_ari_response_error(
267                         response, 404, "Not Found",
268                         "objectType '%s' not found",
269                         args->object_type);
270                 return;
271         }
272
273         sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args->object_type, args->id);
274         if (!sorcery_obj) {
275                 ast_ari_response_error(
276                         response, 404, "Not Found",
277                         "Object with id '%s' not found",
278                         args->id);
279                 return;
280         }
281
282         if (ast_sorcery_delete(sorcery, sorcery_obj)) {
283                 ast_ari_response_error(
284                         response, 403, "Forbidden",
285                         "Could not delete object with id '%s'",
286                         args->id);
287                 return;
288         }
289
290         ast_ari_response_no_content(response);
291 }
292
293
294 void ast_ari_asterisk_get_info(struct ast_variable *headers,
295         struct ast_ari_asterisk_get_info_args *args,
296         struct ast_ari_response *response)
297 {
298         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
299         int show_all = args->only_count == 0;
300         int show_build = show_all;
301         int show_system = show_all;
302         int show_config = show_all;
303         int show_status = show_all;
304         size_t i;
305         int res = 0;
306
307         for (i = 0; i < args->only_count; ++i) {
308                 if (strcasecmp("build", args->only[i]) == 0) {
309                         show_build = 1;
310                 } else if (strcasecmp("system", args->only[i]) == 0) {
311                         show_system = 1;
312                 } else if (strcasecmp("config", args->only[i]) == 0) {
313                         show_config = 1;
314                 } else if (strcasecmp("status", args->only[i]) == 0) {
315                         show_status = 1;
316                 } else {
317                         ast_log(LOG_WARNING, "Unrecognized info section '%s'\n",
318                                 args->only[i]);
319                 }
320         }
321
322         json = ast_json_object_create();
323
324         if (show_build) {
325                 res |= ast_json_object_set(json, "build",
326                         ast_json_pack(
327                                 "{ s: s, s: s, s: s,"
328                                 "  s: s, s: s, s: s }",
329
330                                 "os", ast_build_os,
331                                 "kernel", ast_build_kernel,
332                                 "machine", ast_build_machine,
333
334                                 "options", AST_BUILDOPTS,
335                                 "date", ast_build_date,
336                                 "user", ast_build_user));
337         }
338
339         if (show_system) {
340                 char eid_str[128];
341
342                 ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
343
344                 res |= ast_json_object_set(json, "system",
345                         ast_json_pack("{ s: s, s: s }",
346                                 "version", ast_get_version(),
347                                 "entity_id", eid_str));
348         }
349
350         if (show_config) {
351                 struct ast_json *config = ast_json_pack(
352                         "{ s: s, s: s,"
353                         " s: { s: s, s: s } }",
354
355                         "name", ast_config_AST_SYSTEM_NAME,
356                         "default_language", ast_defaultlanguage,
357
358                         "setid",
359                         "user", ast_config_AST_RUN_USER,
360                         "group", ast_config_AST_RUN_GROUP);
361
362                 res |= ast_json_object_set(json, "config", config);
363
364                 if (ast_option_maxcalls) {
365                         res |= ast_json_object_set(config, "max_channels",
366                                 ast_json_integer_create(ast_option_maxcalls));
367                 }
368
369                 if (ast_option_maxfiles) {
370                         res |= ast_json_object_set(config, "max_open_files",
371                                 ast_json_integer_create(ast_option_maxfiles));
372                 }
373
374                 if (ast_option_maxload) {
375                         res |= ast_json_object_set(config, "max_load",
376                                 ast_json_real_create(ast_option_maxload));
377                 }
378         }
379
380         if (show_status) {
381                 res |= ast_json_object_set(json, "status",
382                         ast_json_pack("{ s: o, s: o }",
383                                 "startup_time",
384                                 ast_json_timeval(ast_startuptime, NULL),
385                                 "last_reload_time",
386                                 ast_json_timeval(ast_lastreloadtime, NULL)));
387         }
388
389         if (res != 0) {
390                 ast_ari_response_alloc_failed(response);
391                 return;
392         }
393
394         ast_ari_response_ok(response, ast_json_ref(json));
395 }
396
397 /*!
398  * \brief Process module information and append to a json array
399  * \param module Resource name
400  * \param description Resource description
401  * \param usecnt Resource use count
402  * \param status Resource running status
403  * \param like
404  * \param support_level Resource support level
405  * \param module_data_list Resource array
406  *
407  * \retval 0 if no resource exists
408  * \retval 1 if resource exists
409  */
410 static int process_module_list(const char *module, const char *description, int usecnt,
411                                const char *status, const char *like,
412                                enum ast_module_support_level support_level, void *module_data_list)
413 {
414         struct ast_json *module_info;
415
416         module_info = ast_json_pack("{s: s, s: s, s: i, s: s, s: s}",
417                               "name", module,
418                               "description", description,
419                               "use_count", usecnt,
420                               "status", status,
421                               "support_level", ast_module_support_level_to_string(support_level));
422         if (!module_info) {
423                 return 0;
424         }
425         ast_json_array_append(module_data_list, module_info);
426         return 1;
427 }
428
429 void ast_ari_asterisk_list_modules(struct ast_variable *headers,
430         struct ast_ari_asterisk_list_modules_args *args,
431         struct ast_ari_response *response)
432 {
433         struct ast_json *json;
434
435         json = ast_json_array_create();
436         if (!json) {
437                 ast_ari_response_alloc_failed(response);
438                 return;
439         }
440         ast_update_module_list_data(&process_module_list, NULL, json);
441
442         ast_ari_response_ok(response, json);
443 }
444
445 /*!
446  * \brief Identify module by name and process resource information
447  * \param module Resource name
448  * \param description Resource description
449  * \param usecnt Resource use count
450  * \param status Resource running status
451  * \param like
452  * \param support_level Resource support level
453  * \param data JSON body for resource
454  * \param condition Name to match resource to
455  *
456  * \retval 0 if no resource exists
457  * \retval 1 if resource exists
458  */
459 static int identify_module(const char *module, const char *description, int usecnt,
460                            const char *status, const char *like,
461                            enum ast_module_support_level support_level, void *data,
462                            const char *condition)
463 {
464         int json_obj_set = 0;
465
466         if (strcmp(condition, module) != 0) {
467                 return 0;
468         }
469
470         json_obj_set += ast_json_object_set(data, "name", ast_json_string_create(module));
471         json_obj_set += ast_json_object_set(data, "description", ast_json_string_create(description));
472         json_obj_set += ast_json_object_set(data, "use_count", ast_json_integer_create(usecnt));
473         json_obj_set += ast_json_object_set(data, "status", ast_json_string_create(status));
474         json_obj_set += ast_json_object_set(data, "support_level", ast_json_string_create(
475                                             ast_module_support_level_to_string(support_level)));
476
477         if (json_obj_set != 0) {
478                 return 0;
479         }
480
481         return 1;
482 }
483
484 void ast_ari_asterisk_get_module(struct ast_variable *headers,
485         struct ast_ari_asterisk_get_module_args *args,
486         struct ast_ari_response *response)
487 {
488         struct ast_json *json;
489         int module_retrieved = 0;
490
491         ast_assert(response != NULL);
492
493         if (!ast_module_check(args->module_name)) {
494                 ast_ari_response_error(
495                         response, 404, "Not Found",
496                         "Module could not be found in running modules");
497                 return;
498         }
499
500         json = ast_json_object_create();
501         if (!json) {
502                 ast_ari_response_alloc_failed(response);
503                 return;
504         }
505
506         module_retrieved = ast_update_module_list_condition(&identify_module, NULL, json,
507                                                             args->module_name);
508         if (!module_retrieved) {
509                 ast_ari_response_error(
510                         response, 409, "Conflict",
511                         "Module information could not be retrieved");
512                 ast_json_unref(json);
513                 return;
514         }
515
516         ast_ari_response_ok(response, json);
517 }
518
519 void ast_ari_asterisk_load_module(struct ast_variable *headers,
520         struct ast_ari_asterisk_load_module_args *args,
521         struct ast_ari_response *response)
522 {
523         enum ast_module_load_result load_result;
524
525         ast_assert(response != NULL);
526
527         if (ast_module_check(args->module_name)) {
528                 ast_ari_response_error(
529                         response, 409, "Conflict",
530                         "Module is already loaded");
531                 return;
532         }
533
534         load_result = ast_load_resource(args->module_name);
535
536         if (load_result == AST_MODULE_LOAD_DECLINE) {
537                 ast_ari_response_error(
538                         response, 409, "Conflict",
539                         "Module load declined");
540                 return;
541         } else if (load_result == AST_MODULE_LOAD_SKIP) {
542                 ast_ari_response_error(
543                         response, 409, "Conflict",
544                         "Module was skipped");
545                 return;
546         } else if (load_result == AST_MODULE_LOAD_FAILURE) {
547                 ast_ari_response_error(
548                         response, 409, "Conflict",
549                         "Module could not be loaded properly");
550                 return;
551         }
552
553         ast_ari_response_no_content(response);
554 }
555
556 void ast_ari_asterisk_unload_module(struct ast_variable *headers,
557         struct ast_ari_asterisk_unload_module_args *args,
558         struct ast_ari_response *response)
559 {
560         int unload_result;
561         enum ast_module_unload_mode unload_mode = AST_FORCE_SOFT;
562
563         ast_assert(response != NULL);
564
565         if (!ast_module_check(args->module_name)) {
566                 ast_ari_response_error(
567                         response, 404, "Not Found",
568                         "Module not found in running modules");
569                 return;
570         }
571
572         unload_result = ast_unload_resource(args->module_name, unload_mode);
573
574         if (unload_result != 0) {
575                 ast_ari_response_error(
576                         response, 409, "Conflict",
577                         "Module could not be unloaded");
578                 return;
579         }
580
581         ast_ari_response_no_content(response);
582 }
583
584 void ast_ari_asterisk_reload_module(struct ast_variable *headers,
585         struct ast_ari_asterisk_reload_module_args *args,
586         struct ast_ari_response *response)
587 {
588         enum ast_module_reload_result reload_result;
589
590         ast_assert(response != NULL);
591
592         if (!ast_module_check(args->module_name)) {
593                 ast_ari_response_error(
594                         response, 404, "Not Found",
595                         "Module not found in running modules");
596                 return;
597         }
598
599         reload_result = ast_module_reload(args->module_name);
600
601         if (reload_result == AST_MODULE_RELOAD_NOT_FOUND) {
602                 ast_ari_response_error(
603                         response, 404, "Not Found",
604                         "Module could not be found");
605                 return;
606         } else if (reload_result == AST_MODULE_RELOAD_ERROR) {
607                 ast_ari_response_error(
608                         response, 409, "Conflict",
609                         "An unknown error occurred while reloading the module");
610                 return;
611         } else if (reload_result == AST_MODULE_RELOAD_IN_PROGRESS) {
612                 ast_ari_response_error(
613                         response, 409, "Conflict",
614                         "Another reload is currently in progress");
615                 return;
616         } else if (reload_result == AST_MODULE_RELOAD_UNINITIALIZED) {
617                 ast_ari_response_error(
618                         response, 409, "Conflict",
619                         "Module has not been initialized");
620                 return;
621         } else if (reload_result == AST_MODULE_RELOAD_NOT_IMPLEMENTED) {
622                 ast_ari_response_error(
623                         response, 409, "Conflict",
624                         "Module does not support reloading");
625                 return;
626         } else if (reload_result == AST_MODULE_RELOAD_QUEUED) {
627                 ast_ari_response_accepted(response);
628                 return;
629         }
630
631         ast_ari_response_no_content(response);
632 }
633
634 void ast_ari_asterisk_ping(struct ast_variable *headers,
635         struct ast_ari_asterisk_ping_args *args,
636         struct ast_ari_response *response)
637 {
638         struct ast_json *json;
639         char eid[20];
640
641         ast_assert(response != NULL);
642
643         json = ast_json_pack("{s: s, s: o, s: s}",
644                         "ping", "pong",
645                         "timestamp", ast_json_timeval(ast_tvnow(), NULL),
646                         "asterisk_id", ast_eid_to_str(eid, sizeof(eid), &ast_eid_default)
647                         );
648
649         ast_ari_response_ok(response, json);
650 }
651
652 /*!
653  * \brief Process logger information and append to a json array
654  * \param channel Resource logger channel name path
655  * \param type Resource log type
656  * \param status Resource log status
657  * \param configuration Resource logger levels
658  * \param log_data_list Resource array
659  *
660  * \retval -1 if no resource exists
661  * \retval 0 if resource exists
662  */
663 static int process_log_list(const char *channel, const char *type,
664         const char *status, const char *configuration, void *log_data_list)
665 {
666         struct ast_json *logger_info;
667
668         logger_info = ast_json_pack("{s: s, s: s, s: s, s: s}",
669                 "channel", channel, "type", type, "status", status, "configuration",
670                 configuration);
671
672         if (!logger_info) {
673                 return AST_LOGGER_FAILURE;
674         }
675
676         ast_json_array_append(log_data_list, logger_info);
677         return AST_LOGGER_SUCCESS;
678 }
679
680 void ast_ari_asterisk_list_log_channels(struct ast_variable *headers,
681         struct ast_ari_asterisk_list_log_channels_args *args,
682         struct ast_ari_response *response)
683 {
684         struct ast_json *json;
685         int res;
686
687         json = ast_json_array_create();
688         res = ast_logger_get_channels(&process_log_list, json);
689
690         if (res == AST_LOGGER_FAILURE) {
691                 ast_ari_response_error(response, 500, "Internal Server Error",
692                         "Response body is not valid");
693                 ast_json_unref(json);
694                 return;
695         } else if (res == AST_LOGGER_ALLOC_ERROR) {
696                 ast_ari_response_error(response, 500, "Internal Server Error",
697                         "Allocation Failed");
698                 ast_json_unref(json);
699                 return;
700         }
701
702         ast_ari_response_ok(response, json);
703 }
704
705 void ast_ari_asterisk_add_log(struct ast_variable *headers,
706         struct ast_ari_asterisk_add_log_args *args,
707         struct ast_ari_response *response)
708 {
709         int res;
710
711         ast_assert(response != NULL);
712
713         res = ast_logger_create_channel(args->log_channel_name, args->configuration);
714
715         if (res == AST_LOGGER_DECLINE) {
716                 ast_ari_response_error(response, 400, "Bad Request",
717                         "Configuration levels are required");
718                 return;
719         } else if (res == AST_LOGGER_FAILURE) {
720                 ast_ari_response_error(response, 409, "Conflict",
721                         "Log channel already exists");
722                 return;
723         } else if (res == AST_LOGGER_ALLOC_ERROR) {
724                 ast_ari_response_error(response, 500, "Internal Server Error",
725                         "Allocation failed");
726                 return;
727         }
728
729         ast_ari_response_no_content(response);
730 }
731
732 void ast_ari_asterisk_rotate_log(struct ast_variable *headers,
733         struct ast_ari_asterisk_rotate_log_args *args,
734         struct ast_ari_response *response)
735 {
736         int res;
737
738         ast_assert(response != NULL);
739
740         res = ast_logger_rotate_channel(args->log_channel_name);
741
742         if (res == AST_LOGGER_FAILURE) {
743                 ast_ari_response_error(
744                         response, 404, "Not Found",
745                         "Log channel does not exist");
746                 return;
747         } else if (res == AST_LOGGER_ALLOC_ERROR) {
748                 ast_ari_response_error(
749                         response, 500, "Internal Server Error",
750                         "Allocation failed");
751                 return;
752         }
753
754         ast_ari_response_no_content(response);
755 }
756
757 void ast_ari_asterisk_delete_log(struct ast_variable *headers,
758         struct ast_ari_asterisk_delete_log_args *args,
759         struct ast_ari_response *response)
760 {
761         int res;
762
763         ast_assert(response != NULL);
764
765         res = ast_logger_remove_channel(args->log_channel_name);
766
767         if (res == AST_LOGGER_FAILURE) {
768                 ast_ari_response_error(response, 404, "Not Found",
769                         "Log channel does not exist");
770                 return;
771         } else if (res == AST_LOGGER_ALLOC_ERROR) {
772                 ast_ari_response_error(response, 500, "Internal Server Error",
773                         "Allocation failed");
774                 return;
775         }
776
777         ast_ari_response_no_content(response);
778 }
779
780 void ast_ari_asterisk_get_global_var(struct ast_variable *headers,
781         struct ast_ari_asterisk_get_global_var_args *args,
782         struct ast_ari_response *response)
783 {
784         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
785         RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
786
787         const char *value;
788
789         ast_assert(response != NULL);
790
791         if (ast_strlen_zero(args->variable)) {
792                 ast_ari_response_error(
793                         response, 400, "Bad Request",
794                         "Variable name is required");
795                 return;
796         }
797
798         tmp = ast_str_create(32);
799         if (!tmp) {
800                 ast_ari_response_alloc_failed(response);
801                 return;
802         }
803
804         value = ast_str_retrieve_variable(&tmp, 0, NULL, NULL, args->variable);
805
806         if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
807                 ast_ari_response_alloc_failed(response);
808                 return;
809         }
810
811         ast_ari_response_ok(response, ast_json_ref(json));
812 }
813
814 void ast_ari_asterisk_set_global_var(struct ast_variable *headers,
815         struct ast_ari_asterisk_set_global_var_args *args,
816         struct ast_ari_response *response)
817 {
818         ast_assert(response != NULL);
819
820         if (ast_strlen_zero(args->variable)) {
821                 ast_ari_response_error(
822                         response, 400, "Bad Request",
823                         "Variable name is required");
824                 return;
825         }
826
827         pbx_builtin_setvar_helper(NULL, args->variable, args->value);
828
829         ast_ari_response_no_content(response);
830 }