summaryrefslogtreecommitdiff
path: root/res/ari/resource_asterisk.c
blob: 4c2948d2b824637cc1f564695071ee2f0e08561c (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* -*- C -*-
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012 - 2013, Digium, Inc.
 *
 * David M. Lee, II <dlee@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Implementation for ARI stubs.
 *
 * \author David M. Lee, II <dlee@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/ast_version.h"
#include "asterisk/buildinfo.h"
#include "asterisk/module.h"
#include "asterisk/paths.h"
#include "asterisk/pbx.h"
#include "resource_asterisk.h"

void ast_ari_asterisk_get_info(struct ast_variable *headers,
	struct ast_ari_asterisk_get_info_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	int show_all = args->only_count == 0;
	int show_build = show_all;
	int show_system = show_all;
	int show_config = show_all;
	int show_status = show_all;
	size_t i;
	int res = 0;

	for (i = 0; i < args->only_count; ++i) {
		if (strcasecmp("build", args->only[i]) == 0) {
			show_build = 1;
		} else if (strcasecmp("system", args->only[i]) == 0) {
			show_system = 1;
		} else if (strcasecmp("config", args->only[i]) == 0) {
			show_config = 1;
		} else if (strcasecmp("status", args->only[i]) == 0) {
			show_status = 1;
		} else {
			ast_log(LOG_WARNING, "Unrecognized info section '%s'\n",
				args->only[i]);
		}
	}

	json = ast_json_object_create();

	if (show_build) {
		res |= ast_json_object_set(json, "build",
			ast_json_pack(
				"{ s: s, s: s, s: s,"
				"  s: s, s: s, s: s }",

				"os", ast_build_os,
				"kernel", ast_build_kernel,
				"machine", ast_build_machine,

				"options", AST_BUILDOPTS,
				"date", ast_build_date,
				"user", ast_build_user));
	}

	if (show_system) {
		char eid_str[128];

		ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);

		res |= ast_json_object_set(json, "system",
			ast_json_pack("{ s: s, s: s }",
				"version", ast_get_version(),
				"entity_id", eid_str));
	}

	if (show_config) {
		struct ast_json *config = ast_json_pack(
			"{ s: s, s: s,"
			" s: { s: s, s: s } }",

			"name", ast_config_AST_SYSTEM_NAME,
			"default_language", ast_defaultlanguage,

			"setid",
			"user", ast_config_AST_RUN_USER,
			"group", ast_config_AST_RUN_GROUP);

		res |= ast_json_object_set(json, "config", config);

		if (ast_option_maxcalls) {
			res |= ast_json_object_set(config, "max_channels",
				ast_json_integer_create(ast_option_maxcalls));
		}

		if (ast_option_maxfiles) {
			res |= ast_json_object_set(config, "max_open_files",
				ast_json_integer_create(ast_option_maxfiles));
		}

		if (ast_option_maxload) {
			res |= ast_json_object_set(config, "max_load",
				ast_json_real_create(ast_option_maxload));
		}
	}

	if (show_status) {
		res |= ast_json_object_set(json, "status",
			ast_json_pack("{ s: o, s: o }",
				"startup_time",
				ast_json_timeval(ast_startuptime, NULL),
				"last_reload_time",
				ast_json_timeval(ast_lastreloadtime, NULL)));
	}

	if (res != 0) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	ast_ari_response_ok(response, ast_json_ref(json));
}

/*!
 * \brief Process module information and append to a json array
 * \param module Resource name
 * \param description Resource description
 * \param usecnt Resource use count
 * \param status Resource running status
 * \param like
 * \param support_level Resource support level
 * \param module_data_list Resource array
 *
 * \retval 0 if no resource exists
 * \retval 1 if resource exists
 */
static int process_module_list(const char *module, const char *description, int usecnt,
                               const char *status, const char *like,
                               enum ast_module_support_level support_level, void *module_data_list)
{
	struct ast_json *module_info;

	module_info = ast_json_pack("{s: s, s: s, s: i, s: s, s: s}",
                              "name", module,
                              "description", description,
                              "use_count", usecnt,
                              "status", status,
                              "support_level", ast_module_support_level_to_string(support_level));
	if (!module_info) {
		return 0;
	}
	ast_json_array_append(module_data_list, module_info);
	return 1;
}

void ast_ari_asterisk_list_modules(struct ast_variable *headers,
	struct ast_ari_asterisk_list_modules_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;

	json = ast_json_array_create();
	ast_update_module_list_data(&process_module_list, NULL, json);

	ast_ari_response_ok(response, json);
}

/*!
 * \brief Identify module by name and process resource information
 * \param module Resource name
 * \param description Resource description
 * \param usecnt Resource use count
 * \param status Resource running status
 * \param like
 * \param support_level Resource support level
 * \param data JSON body for resource
 * \param condition Name to match resource to
 *
 * \retval 0 if no resource exists
 * \retval 1 if resource exists
 */
static int identify_module(const char *module, const char *description, int usecnt,
                           const char *status, const char *like,
                           enum ast_module_support_level support_level, void *data,
                           const char *condition)
{
	int json_obj_set = 0;

	if (strcmp(condition, module) != 0) {
		return 0;
	}

	json_obj_set += ast_json_object_set(data, "name", ast_json_string_create(module));
	json_obj_set += ast_json_object_set(data, "description", ast_json_string_create(description));
	json_obj_set += ast_json_object_set(data, "use_count", ast_json_integer_create(usecnt));
	json_obj_set += ast_json_object_set(data, "status", ast_json_string_create(status));
	json_obj_set += ast_json_object_set(data, "support_level", ast_json_string_create(
	                                    ast_module_support_level_to_string(support_level)));

	if (json_obj_set != 0) {
		return 0;
	}

	return 1;
}

void ast_ari_asterisk_get_module(struct ast_variable *headers,
	struct ast_ari_asterisk_get_module_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;
	int module_retrieved = 0;

	ast_assert(response != NULL);

	if (!ast_module_check(args->module_name)) {
		ast_ari_response_error(
			response, 404, "Not Found",
			"Module could not be found in running modules");
		return;
	}

	json = ast_json_object_create();
	if (!json) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	module_retrieved = ast_update_module_list_condition(&identify_module, NULL, json,
	                                                    args->module_name);
	if (!module_retrieved) {
		ast_ari_response_error(
			response, 409, "Conflict",
			"Module information could not be retrieved");
		return;
	}

	ast_ari_response_ok(response, json);
}

void ast_ari_asterisk_load_module(struct ast_variable *headers,
	struct ast_ari_asterisk_load_module_args *args,
	struct ast_ari_response *response)
{
	enum ast_module_load_result load_result;

	ast_assert(response != NULL);

	if (ast_module_check(args->module_name)) {
		ast_ari_response_error(
			response, 409, "Conflict",
			"Module is already loaded");
		return;
	}

	load_result = ast_load_resource(args->module_name);

	if (load_result == AST_MODULE_LOAD_DECLINE) {
		ast_ari_response_error(
			response, 409, "Conflict",
			"Module load declined");
		return;
	} else if (load_result == AST_MODULE_LOAD_SKIP) {
		ast_ari_response_error(
			response, 409, "Conflict",
			"Module was skipped");
		return;
	} else if (load_result == AST_MODULE_LOAD_FAILURE) {
		ast_ari_response_error(
			response, 409, "Conflict",
			"Module could not be loaded properly");
		return;
	}

	ast_ari_response_no_content(response);
}

void ast_ari_asterisk_unload_module(struct ast_variable *headers,
	struct ast_ari_asterisk_unload_module_args *args,
	struct ast_ari_response *response)
{
	int unload_result;
	enum ast_module_unload_mode unload_mode = AST_FORCE_FIRM;

	ast_assert(response != NULL);

	if (!ast_module_check(args->module_name)) {
		ast_ari_response_error(
			response, 404, "Not Found",
			"Module not found in running modules");
		return;
	}

	unload_result = ast_unload_resource(args->module_name, unload_mode);

	if (unload_result != 0) {
		ast_ari_response_error(
			response, 409, "Conflict",
			"Module could not be unloaded");
		return;
	}

	ast_ari_response_no_content(response);
}

void ast_ari_asterisk_get_global_var(struct ast_variable *headers,
	struct ast_ari_asterisk_get_global_var_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	RAII_VAR(struct ast_str *, tmp, NULL, ast_free);

	const char *value;

	ast_assert(response != NULL);

	if (ast_strlen_zero(args->variable)) {
		ast_ari_response_error(
			response, 400, "Bad Request",
			"Variable name is required");
		return;
	}

	tmp = ast_str_create(32);
	if (!tmp) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	value = ast_str_retrieve_variable(&tmp, 0, NULL, NULL, args->variable);

	if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	ast_ari_response_ok(response, ast_json_ref(json));
}

void ast_ari_asterisk_set_global_var(struct ast_variable *headers,
	struct ast_ari_asterisk_set_global_var_args *args,
	struct ast_ari_response *response)
{
	ast_assert(response != NULL);

	if (ast_strlen_zero(args->variable)) {
		ast_ari_response_error(
			response, 400, "Bad Request",
			"Variable name is required");
		return;
	}

	pbx_builtin_setvar_helper(NULL, args->variable, args->value);

	ast_ari_response_no_content(response);
}