Debian Codegraph @

res/res_geolocation/geoloc_dialplan.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2022, Sangoma Technologies Corporation
5 *
6 * George Joseph <[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 #include "asterisk.h"
20 #include "asterisk/config.h"
21 #include "asterisk/cli.h"
22 #include "asterisk/module.h"
23 #include "asterisk/channel.h"
24 #include "asterisk/pbx.h"
25 #include "asterisk/strings.h"
26 #include "asterisk/utils.h"
27 #include "asterisk/app.h"
28 #include "geoloc_private.h"
29
30 static void varlist_to_str(struct ast_variable *list, struct ast_str** buf, size_t len)
31 {
32 struct ast_variable *var = list;
33
34 for (; var; var = var->next) {
35 ast_str_append(buf, len, "%s=\"%s\"%s", var->name, var->value, var->next ? "," : "");
36 }
37 }
38
39 #define RESOLVE_FOR_READ(_param) \
40 ({ \
41 if (ast_test_flag(&opts, OPT_GEOLOC_RESOLVE)) { \
42 struct ast_variable *resolved = geoloc_eprofile_resolve_varlist( \
43 eprofile->_param, eprofile->location_variables, chan); \
44 if (!resolved) { \
45 ast_log(LOG_ERROR, "%s: Unable to resolve " #_param "\n", chan_name); \
46 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3"); \
47 return 0; \
48 } \
49 varlist_to_str(resolved, buf, len); \
50 ast_variables_destroy(resolved); \
51 } else { \
52 varlist_to_str(eprofile->_param, buf, len); \
53 } \
54 })
55
56 #define RESOLVE_STRINGFIELD_FOR_READ(_param) \
57 ({ \
58 if (ast_test_flag(&opts, OPT_GEOLOC_RESOLVE)) { \
59 char *resolved = geoloc_eprofile_resolve_string( \
60 eprofile->_param, eprofile->location_variables, chan); \
61 if (!resolved) { \
62 ast_log(LOG_ERROR, "%s: Unable to resolve " #_param "\n", chan_name); \
63 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3"); \
64 return 0; \
65 } \
66 ast_str_append(buf, len, "%s", resolved); \
67 ast_free(resolved); \
68 } else { \
69 ast_str_append(buf, len, "%s", eprofile->_param); \
70 } \
71 })
72
73 enum my_app_option_flags {
74 OPT_GEOLOC_RESOLVE = (1 << 0),
75 OPT_GEOLOC_APPEND = (1 << 1),
76 };
77
78 AST_APP_OPTIONS(action_options, {
79 AST_APP_OPTION('r', OPT_GEOLOC_RESOLVE),
80 AST_APP_OPTION('a', OPT_GEOLOC_APPEND),
81 });
82
83
84 static int geoloc_profile_read(struct ast_channel *chan,
85 const char *cmd, char *data, struct ast_str **buf, ssize_t len)
86 {
87 char *parsed_data = ast_strdupa(data);
88 const char *chan_name = ast_channel_name(chan);
89 struct ast_datastore *ds;
90 struct ast_geoloc_eprofile *orig_eprofile = NULL;
91 struct ast_geoloc_eprofile *eprofile = NULL;
92 struct ast_flags opts = { 0, };
93
94 AST_DECLARE_APP_ARGS(args,
95 AST_APP_ARG(field);
96 AST_APP_ARG(options);
97 );
98
99 /* Check for zero arguments */
100 if (ast_strlen_zero(parsed_data)) {
101 ast_log(LOG_ERROR, "%s: Cannot call without arguments\n", chan_name);
102 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-1");
103 return 0;
104 }
105
106 AST_STANDARD_APP_ARGS(args, parsed_data);
107
108 if (ast_strlen_zero(args.field)) {
109 ast_log(LOG_ERROR, "%s: Cannot call without a field to query\n", chan_name);
110 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-1");
111 return 0;
112 }
113
114 if (!ast_strlen_zero(args.options)) {
115 if (ast_app_parse_options(action_options, &opts, NULL, args.options)) {
116 ast_log(LOG_ERROR, "%s: Invalid options: %s\n", chan_name, args.options);
117 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-1");
118 return 0;
119 }
120 }
121
122 ds = ast_geoloc_datastore_find(chan);
123 if (!ds) {
124 ast_log(LOG_NOTICE, "%s: There is no geoloc profile on this channel\n", chan_name);
125 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-2");
126 return 0;
127 }
128
129 orig_eprofile = ast_geoloc_datastore_get_eprofile(ds, 0);
130 if (!orig_eprofile) {
131 ast_log(LOG_NOTICE, "%s: There is no geoloc profile on this channel\n", chan_name);
132 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-2");
133 return 0;
134 }
135
136 eprofile = ast_geoloc_eprofile_dup(orig_eprofile);
137 ao2_ref(orig_eprofile, -1);
138 if (!eprofile) {
139 ast_log(LOG_ERROR, "%s: Unable to duplicate eprofile\n", chan_name);
140 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-2");
141 return 0;
142 }
143
144 if (!eprofile->effective_location) {
145 ast_geoloc_eprofile_refresh_location(eprofile);
146 }
147
148 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "0");
149 if (ast_strings_equal(args.field, "inheritable")) {
150 ast_str_append(buf, len, "%s", ds->inheritance ? "true" : "false");
151 } else if (ast_strings_equal(args.field, "id")) {
152 ast_str_append(buf, len, "%s", eprofile->id);
153 } else if (ast_strings_equal(args.field, "location_reference")) {
154 ast_str_append(buf, len, "%s", eprofile->location_reference);
155 } else if (ast_strings_equal(args.field, "method")) {
156 ast_str_append(buf, len, "%s", eprofile->method);
157 } else if (ast_strings_equal(args.field, "allow_routing_use")) {
158 ast_str_append(buf, len, "%s", eprofile->allow_routing_use ? "yes" : "no");
159 } else if (ast_strings_equal(args.field, "suppress_empty_ca_elements")) {
160 ast_str_append(buf, len, "%s", eprofile->suppress_empty_ca_elements ? "yes" : "no");
161 } else if (ast_strings_equal(args.field, "profile_precedence")) {
162 ast_str_append(buf, len, "%s", ast_geoloc_precedence_to_name(eprofile->precedence));
163 } else if (ast_strings_equal(args.field, "format")) {
164 ast_str_append(buf, len, "%s", ast_geoloc_format_to_name(eprofile->format));
165 } else if (ast_strings_equal(args.field, "pidf_element")) {
166 ast_str_append(buf, len, "%s", ast_geoloc_pidf_element_to_name(eprofile->pidf_element));
167 } else if (ast_strings_equal(args.field, "pidf_element_id")) {
168 RESOLVE_STRINGFIELD_FOR_READ(pidf_element_id);
169 } else if (ast_strings_equal(args.field, "location_source")) {
170 ast_str_append(buf, len, "%s", eprofile->location_source);
171 } else if (ast_strings_equal(args.field, "notes")) {
172 ast_str_append(buf, len, "%s", eprofile->notes);
173 } else if (ast_strings_equal(args.field, "location_info")) {
174 RESOLVE_FOR_READ(location_info);
175 } else if (ast_strings_equal(args.field, "location_info_refinement")) {
176 RESOLVE_FOR_READ(location_refinement);
177 } else if (ast_strings_equal(args.field, "location_variables")) {
178 RESOLVE_FOR_READ(location_variables);
179 } else if (ast_strings_equal(args.field, "effective_location")) {
180 RESOLVE_FOR_READ(effective_location);
181 } else if (ast_strings_equal(args.field, "usage_rules")) {
182 RESOLVE_FOR_READ(usage_rules);
183 } else if (ast_strings_equal(args.field, "confidence")) {
184 varlist_to_str(eprofile->confidence, buf, len);
185 } else if (ast_strings_equal(args.field, "device_id")) {
186 RESOLVE_STRINGFIELD_FOR_READ(device_id);
187 } else {
188 ast_log(LOG_ERROR, "%s: Field '%s' is not valid\n", chan_name, args.field);
189 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3");
190 }
191
192 ao2_ref(eprofile, -1);
193 return 0;
194 }
195
196 #define VAR_LIST_REPLACE(_old, _new) \
197 ast_variables_destroy(_old); \
198 _old = _new;
199
200 #define TEST_ENUM_VALUE(_chan_name, _ep, _field, _value) \
201 ({ \
202 enum ast_geoloc_ ## _field v; \
203 v = ast_geoloc_ ## _field ## _str_to_enum(_value); \
204 if (v == AST_GEOLOC_INVALID_VALUE) { \
205 ast_log(LOG_ERROR, "%s: %s '%s' is invalid\n", _chan_name, #_field, value); \
206 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3"); \
207 return 0; \
208 } \
209 _ep->_field = v; \
210 })
211
212 #define TEST_VARLIST(_chan_name, _ep, _field, _value) \
213 ({ \
214 struct ast_variable *_list; \
215 _list = ast_variable_list_from_quoted_string(_value, ",", "=", "\"" ); \
216 if (!_list) { \
217 ast_log(LOG_ERROR, "%s: %s '%s' is malformed or contains invalid values", _chan_name, #_field, _value); \
218 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3"); \
219 return 0; \
220 } \
221 if (ast_test_flag(&opts, OPT_GEOLOC_APPEND)) { \
222 ast_variable_list_append(&_ep->_field, _list); \
223 } else {\
224 VAR_LIST_REPLACE(_ep->_field, _list); \
225 } \
226 })
227
228
229 #define RESOLVE_FOR_WRITE(_param) \
230 ({ \
231 if (ast_test_flag(&opts, OPT_GEOLOC_RESOLVE)) { \
232 struct ast_variable *resolved = geoloc_eprofile_resolve_varlist( \
233 eprofile->_param, eprofile->location_variables, chan); \
234 if (!resolved) { \
235 ast_log(LOG_ERROR, "%s: Unable to resolve " #_param " %p %p\n", chan_name, eprofile->_param, eprofile->location_variables); \
236 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3"); \
237 return 0; \
238 } \
239 VAR_LIST_REPLACE(eprofile->_param, resolved); \
240 } \
241 })
242
243 #define RESOLVE_STRINGFIELD_FOR_WRITE(_param, _value) \
244 ({ \
245 if (ast_test_flag(&opts, OPT_GEOLOC_RESOLVE)) { \
246 char *resolved = geoloc_eprofile_resolve_string( \
247 _value, eprofile->location_variables, chan); \
248 if (!resolved) { \
249 ast_log(LOG_ERROR, "%s: Unable to resolve " #_param " %p %p\n", chan_name, eprofile->_param, eprofile->location_variables); \
250 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3"); \
251 return 0; \
252 } \
253 ast_string_field_set(eprofile, _param, resolved); \
254 ast_free(resolved); \
255 } else { \
256 ast_string_field_set(eprofile, _param, _value); \
257 } \
258 })
259
260 static int geoloc_profile_write(struct ast_channel *chan, const char *cmd, char *data,
261 const char *value)
262 {
263 char *parsed_data = ast_strdupa(data);
264 const char *chan_name = ast_channel_name(chan);
265 struct ast_datastore *ds; /* Reminder: datastores aren't ao2 objects */
266 RAII_VAR(struct ast_geoloc_eprofile *, eprofile, NULL, ao2_cleanup);
267 struct ast_flags opts = { 0, };
268
269 AST_DECLARE_APP_ARGS(args,
270 AST_APP_ARG(field);
271 AST_APP_ARG(options);
272 );
273
274 /* Check for zero arguments */
275 if (ast_strlen_zero(parsed_data)) {
276 ast_log(LOG_ERROR, "%s: Cannot call without arguments\n", chan_name);
277 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-1");
278 return 0;
279 }
280
281 AST_STANDARD_APP_ARGS(args, parsed_data);
282
283 if (ast_strlen_zero(args.field)) {
284 ast_log(LOG_ERROR, "%s: Cannot call without a field to set\n", chan_name);
285 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-1");
286 return 0;
287 }
288
289 if (!ast_strlen_zero(args.options)) {
290 if (ast_app_parse_options(action_options, &opts, NULL, args.options)) {
291 ast_log(LOG_ERROR, "%s: Invalid options: %s\n", chan_name, args.options);
292 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-1");
293 return 0;
294 }
295 }
296
297 ast_debug(1, "%s: name: %s value: %s options: %s append: %s resolve: %s\n", chan_name,
298 args.field, value, args.options, ast_test_flag(&opts, OPT_GEOLOC_APPEND) ? "yes" : "no",
299 ast_test_flag(&opts, OPT_GEOLOC_RESOLVE) ? "yes" : "no");
300
301 ds = ast_geoloc_datastore_find(chan);
302 if (!ds) {
303 ds = ast_geoloc_datastore_create(ast_channel_name(chan));
304 if (!ds) {
305 ast_log(LOG_WARNING, "%s: Unable to create geolocation datastore\n", chan_name);
306 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-2");
307 return 0;
308 }
309 ast_channel_datastore_add(chan, ds);
310 }
311
312 eprofile = ast_geoloc_datastore_get_eprofile(ds, 0);
313 if (!eprofile) {
314 int rc;
315 eprofile = ast_geoloc_eprofile_alloc(chan_name);
316 if (!eprofile) {
317 ast_log(LOG_ERROR, "%s: Could not allocate eprofile\n", chan_name);
318 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-2");
319 return 0;
320 }
321 rc = ast_geoloc_datastore_add_eprofile(ds, eprofile);
322 if (rc <= 0) {
323 ast_log(LOG_ERROR, "%s: Could not add eprofile to datastore\n", chan_name);
324 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-2");
325 return 0;
326 }
327 }
328
329 if (ast_strings_equal(args.field, "inheritable")) {
330 ast_geoloc_datastore_set_inheritance(ds, ast_true(value));
331 } else if (ast_strings_equal(args.field, "id")) {
332 ast_string_field_set(eprofile, id, value);
333 } else if (ast_strings_equal(args.field, "location_reference")) {
334 struct ast_geoloc_location *loc = ast_geoloc_get_location(value);
335 ao2_cleanup(loc);
336 if (!loc) {
337 ast_log(LOG_ERROR, "%s: Location reference '%s' doesn't exist\n", chan_name, value);
338 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3");
339 return 0;
340 }
341 ast_string_field_set(eprofile, location_reference, value);
342 } else if (ast_strings_equal(args.field, "method")) {
343 ast_string_field_set(eprofile, method, value);
344 } else if (ast_strings_equal(args.field, "allow_routing_use")) {
345 eprofile->allow_routing_use = ast_true(value);
346 } else if (ast_strings_equal(args.field, "suppress_empty_ca_elements")) {
347 eprofile->suppress_empty_ca_elements = ast_true(value);
348 } else if (ast_strings_equal(args.field, "profile_precedence")) {
349 TEST_ENUM_VALUE(chan_name, eprofile, precedence, value);
350 } else if (ast_strings_equal(args.field, "format")) {
351 TEST_ENUM_VALUE(chan_name, eprofile, format, value);
352 } else if (ast_strings_equal(args.field, "pidf_element")) {
353 TEST_ENUM_VALUE(chan_name, eprofile, pidf_element, value);
354 } else if (ast_strings_equal(args.field, "pidf_element_id")) {
355 RESOLVE_STRINGFIELD_FOR_WRITE(pidf_element_id, value);
356 } else if (ast_strings_equal(args.field, "location_source")) {
357 ast_string_field_set(eprofile, location_source, value);
358 } else if (ast_strings_equal(args.field, "notes")) {
359 ast_string_field_set(eprofile, notes, value);
360 } else if (ast_strings_equal(args.field, "location_info")) {
361 TEST_VARLIST(chan_name, eprofile, location_info, value);
362 RESOLVE_FOR_WRITE(location_info);
363 } else if (ast_strings_equal(args.field, "location_info_refinement")) {
364 TEST_VARLIST(chan_name, eprofile, location_refinement, value);
365 RESOLVE_FOR_WRITE(location_refinement);
366 } else if (ast_strings_equal(args.field, "location_variables")) {
367 TEST_VARLIST(chan_name, eprofile, location_variables, value);
368 RESOLVE_FOR_WRITE(location_variables);
369 } else if (ast_strings_equal(args.field, "effective_location")) {
370 TEST_VARLIST(chan_name, eprofile, effective_location, value);
371 RESOLVE_FOR_WRITE(effective_location);
372 } else if (ast_strings_equal(args.field, "usage_rules")) {
373 TEST_VARLIST(chan_name, eprofile, usage_rules, value);
374 RESOLVE_FOR_WRITE(usage_rules);
375 } else if (ast_strings_equal(args.field, "confidence")) {
376 TEST_VARLIST(chan_name, eprofile, confidence, value);
377 } else if (ast_strings_equal(args.field, "device_id")) {
378 RESOLVE_STRINGFIELD_FOR_WRITE(pidf_element_id, value);
379 } else {
380 ast_log(LOG_ERROR, "%s: Field '%s' is not valid\n", chan_name, args.field);
381 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "-3");
382 return 0;
383 }
384
385 ast_geoloc_eprofile_refresh_location(eprofile);
386
387 pbx_builtin_setvar_helper(chan, "GEOLOCPROFILESTATUS", "0");
388
389 return 0;
390 }
391
392 static struct ast_custom_function geoloc_function = {
393 .name = "GEOLOC_PROFILE",
394 .read2 = geoloc_profile_read,
395 .write = geoloc_profile_write,
396 };
397
398 int geoloc_dialplan_unload(void)
399 {
400 ast_custom_function_unregister(&geoloc_function);
401
402 return AST_MODULE_LOAD_SUCCESS;
403 }
404
405 int geoloc_dialplan_load(void)
406 {
407 int res = 0;
408
409 res = ast_custom_function_register(&geoloc_function);
410
411 return res == 0 ? AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
412 }
413
414 int geoloc_dialplan_reload(void)
415 {
416 return AST_MODULE_LOAD_SUCCESS;
417 }
418