| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
#include "asterisk/module.h" |
| 21 |
#include "asterisk/cli.h" |
| 22 |
#define AST_API_MODULE |
| 23 |
#include "geoloc_private.h" |
| 24 |
|
| 25 |
static struct ast_sorcery *geoloc_sorcery; |
| 26 |
|
| 27 |
static const char *pidf_element_names[] = { |
| 28 |
"<none>", |
| 29 |
"device", |
| 30 |
"tuple", |
| 31 |
"person" |
| 32 |
}; |
| 33 |
|
| 34 |
static const char *format_names[] = { |
| 35 |
"<none>", |
| 36 |
"civicAddress", |
| 37 |
"GML", |
| 38 |
"URI", |
| 39 |
}; |
| 40 |
|
| 41 |
static const char * precedence_names[] = { |
| 42 |
"prefer_incoming", |
| 43 |
"prefer_config", |
| 44 |
"discard_incoming", |
| 45 |
"discard_config", |
| 46 |
}; |
| 47 |
|
| 48 |
CONFIG_ENUM(location, format) |
| 49 |
CONFIG_VAR_LIST(location, location_info) |
| 50 |
CONFIG_VAR_LIST(location, confidence) |
| 51 |
|
| 52 |
static void geoloc_location_destructor(void *obj) { |
| 53 |
struct ast_geoloc_location *location = obj; |
| 54 |
|
| 55 |
ast_string_field_free_memory(location); |
| 56 |
ast_variables_destroy(location->location_info); |
| 57 |
ast_variables_destroy(location->confidence); |
| 58 |
} |
| 59 |
|
| 60 |
static void *geoloc_location_alloc(const char *name) |
| 61 |
{ |
| 62 |
struct ast_geoloc_location *location = ast_sorcery_generic_alloc(sizeof(struct ast_geoloc_location), geoloc_location_destructor); |
| 63 |
if (location) { |
| 64 |
ast_string_field_init(location, 128); |
| 65 |
} |
| 66 |
|
| 67 |
return location; |
| 68 |
} |
| 69 |
|
| 70 |
CONFIG_ENUM(profile, pidf_element) |
| 71 |
CONFIG_ENUM(profile, precedence) |
| 72 |
CONFIG_VAR_LIST(profile, location_refinement) |
| 73 |
CONFIG_VAR_LIST(profile, location_variables) |
| 74 |
CONFIG_VAR_LIST(profile, usage_rules) |
| 75 |
|
| 76 |
CONFIG_ENUM_HANDLER(profile, format) |
| 77 |
CONFIG_ENUM_TO_STR(profile, format) |
| 78 |
CONFIG_VAR_LIST(profile, location_info) |
| 79 |
CONFIG_VAR_LIST(profile, confidence) |
| 80 |
|
| 81 |
static void geoloc_profile_destructor(void *obj) { |
| 82 |
struct ast_geoloc_profile *profile = obj; |
| 83 |
|
| 84 |
ast_string_field_free_memory(profile); |
| 85 |
ast_variables_destroy(profile->location_refinement); |
| 86 |
ast_variables_destroy(profile->location_variables); |
| 87 |
ast_variables_destroy(profile->usage_rules); |
| 88 |
ast_variables_destroy(profile->location_info); |
| 89 |
ast_variables_destroy(profile->confidence); |
| 90 |
} |
| 91 |
|
| 92 |
static void *geoloc_profile_alloc(const char *name) |
| 93 |
{ |
| 94 |
struct ast_geoloc_profile *profile = ast_sorcery_generic_alloc(sizeof(*profile), |
| 95 |
geoloc_profile_destructor); |
| 96 |
if (profile) { |
| 97 |
ast_string_field_init(profile, 128); |
| 98 |
} |
| 99 |
|
| 100 |
return profile; |
| 101 |
} |
| 102 |
|
| 103 |
static enum ast_geoloc_validate_result validate_location_info(const char *id, |
| 104 |
enum ast_geoloc_format format, struct ast_variable *location_info) |
| 105 |
{ |
| 106 |
enum ast_geoloc_validate_result result; |
| 107 |
char *failed; |
| 108 |
const char *uri; |
| 109 |
|
| 110 |
switch (format) { |
| 111 |
case AST_GEOLOC_FORMAT_NONE: |
| 112 |
case AST_GEOLOC_FORMAT_LAST: |
| 113 |
ast_log(LOG_ERROR, "Location '%s' must have a format\n", id); |
| 114 |
return -1; |
| 115 |
case AST_GEOLOC_FORMAT_CIVIC_ADDRESS: |
| 116 |
result = ast_geoloc_civicaddr_validate_varlist(location_info, &failed); |
| 117 |
if (result != AST_GEOLOC_VALIDATE_SUCCESS) { |
| 118 |
ast_log(LOG_ERROR, "Location '%s' has invalid item '%s' in the location\n", |
| 119 |
id, failed); |
| 120 |
ast_free(failed); |
| 121 |
return result; |
| 122 |
} |
| 123 |
break; |
| 124 |
case AST_GEOLOC_FORMAT_GML: |
| 125 |
result = ast_geoloc_gml_validate_varlist(location_info, &failed); |
| 126 |
if (result != AST_GEOLOC_VALIDATE_SUCCESS) { |
| 127 |
ast_log(LOG_ERROR, "Location '%s' failed: %s\n", id, failed); |
| 128 |
ast_free(failed); |
| 129 |
return result; |
| 130 |
} |
| 131 |
break; |
| 132 |
case AST_GEOLOC_FORMAT_URI: |
| 133 |
uri = ast_variable_find_in_list(location_info, "URI"); |
| 134 |
if (!uri) { |
| 135 |
struct ast_str *str = ast_variable_list_join(location_info, ",", "=", "\"", NULL); |
| 136 |
|
| 137 |
ast_log(LOG_ERROR, "Geolocation location '%s' format is set to '%s' but no 'URI' was found in location parameter '%s'\n", |
| 138 |
id, format_names[AST_GEOLOC_FORMAT_URI], ast_str_buffer(str)); |
| 139 |
ast_free(str); |
| 140 |
return AST_GEOLOC_VALIDATE_NOT_ENOUGH_VARNAMES; |
| 141 |
} |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
return AST_GEOLOC_VALIDATE_SUCCESS; |
| 146 |
} |
| 147 |
|
| 148 |
static int validate_location_source(const char *id, const char *location_source) |
| 149 |
{ |
| 150 |
if (!ast_strlen_zero(location_source)) { |
| 151 |
struct ast_sockaddr loc_source_addr; |
| 152 |
int rc = ast_sockaddr_parse(&loc_source_addr, location_source, PARSE_PORT_FORBID); |
| 153 |
if (rc == 1) { |
| 154 |
ast_log(LOG_ERROR, "Geolocation location '%s' location_source '%s' must be a FQDN." |
| 155 |
" RFC8787 expressly forbids IP addresses.\n", |
| 156 |
id, location_source); |
| 157 |
return -1; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
static int geoloc_location_apply_handler(const struct ast_sorcery *sorcery, void *obj) |
| 165 |
{ |
| 166 |
struct ast_geoloc_location *location = obj; |
| 167 |
const char *location_id = ast_sorcery_object_get_id(location); |
| 168 |
enum ast_geoloc_validate_result result; |
| 169 |
int rc = 0; |
| 170 |
|
| 171 |
if (!location->location_info) { |
| 172 |
ast_log(LOG_ERROR, "Location '%s' is missing required element 'location_info'", |
| 173 |
location_id); |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
result = validate_location_info(location_id, location->format, location->location_info); |
| 178 |
if (result != AST_GEOLOC_VALIDATE_SUCCESS) { |
| 179 |
return -1; |
| 180 |
} |
| 181 |
|
| 182 |
rc = validate_location_source(location_id, location->location_source); |
| 183 |
if (rc != 0) { |
| 184 |
return -1; |
| 185 |
} |
| 186 |
|
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static int geoloc_profile_apply_handler(const struct ast_sorcery *sorcery, void *obj) |
| 191 |
{ |
| 192 |
struct ast_geoloc_profile *profile = obj; |
| 193 |
struct ast_geoloc_location *location; |
| 194 |
const char *id = ast_sorcery_object_get_id(profile); |
| 195 |
enum ast_geoloc_validate_result result; |
| 196 |
enum ast_geoloc_format format = AST_GEOLOC_FORMAT_NONE; |
| 197 |
int rc = 0; |
| 198 |
|
| 199 |
if (!ast_strlen_zero(profile->location_reference)) { |
| 200 |
if (profile->location_info || |
| 201 |
profile->format != AST_GEOLOC_FORMAT_NONE) { |
| 202 |
ast_log(LOG_ERROR, "Profile '%s' can't have location_reference and location_info or format at the same time", |
| 203 |
id); |
| 204 |
return -1; |
| 205 |
} |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
if (profile->location_info) { |
| 210 |
result = validate_location_info(id, profile->format, profile->location_info); |
| 211 |
if (result != AST_GEOLOC_VALIDATE_SUCCESS) { |
| 212 |
return -1; |
| 213 |
} |
| 214 |
|
| 215 |
rc = validate_location_source(id, profile->location_source); |
| 216 |
if (rc != 0) { |
| 217 |
return -1; |
| 218 |
} |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
if (!ast_strlen_zero(profile->location_reference)) { |
| 224 |
location = ast_sorcery_retrieve_by_id(geoloc_sorcery, "location", profile->location_reference); |
| 225 |
if (!location) { |
| 226 |
ast_log(LOG_ERROR, "Profile '%s' has a location_reference '%s' that doesn't exist", |
| 227 |
id, profile->location_reference); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
format = location->format; |
| 231 |
ao2_ref(location, -1); |
| 232 |
} |
| 233 |
|
| 234 |
if (profile->location_refinement) { |
| 235 |
result = validate_location_info(id, format, profile->location_refinement); |
| 236 |
if (result != AST_GEOLOC_VALIDATE_SUCCESS) { |
| 237 |
return -1; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
struct ast_sorcery *geoloc_get_sorcery(void) |
| 245 |
{ |
| 246 |
ast_sorcery_ref(geoloc_sorcery); |
| 247 |
return geoloc_sorcery; |
| 248 |
} |
| 249 |
|
| 250 |
static char *geoloc_config_list_locations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 251 |
{ |
| 252 |
struct ao2_iterator iter; |
| 253 |
struct ao2_container *sorted_container; |
| 254 |
struct ao2_container *unsorted_container; |
| 255 |
struct ast_geoloc_location *loc; |
| 256 |
int using_regex = 0; |
| 257 |
char *result = CLI_SUCCESS; |
| 258 |
int ret = 0; |
| 259 |
int count = 0; |
| 260 |
|
| 261 |
switch (cmd) { |
| 262 |
case CLI_INIT: |
| 263 |
e->command = "geoloc list locations"; |
| 264 |
e->usage = "Usage: geoloc list locations [ like <pattern> ]\n" |
| 265 |
" List Geolocation Location Objects\n"; |
| 266 |
return NULL; |
| 267 |
case CLI_GENERATE: |
| 268 |
return NULL; |
| 269 |
} |
| 270 |
|
| 271 |
if (a->argc != 3 && a->argc != 5) { |
| 272 |
return CLI_SHOWUSAGE; |
| 273 |
} |
| 274 |
|
| 275 |
if (a->argc == 5) { |
| 276 |
if (strcasecmp(a->argv[3], "like")) { |
| 277 |
return CLI_SHOWUSAGE; |
| 278 |
} |
| 279 |
using_regex = 1; |
| 280 |
} |
| 281 |
|
| 282 |
sorted_container = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 283 |
ast_sorcery_object_id_sort, NULL); |
| 284 |
if (!sorted_container) { |
| 285 |
ast_cli(a->fd, "Geolocation Location Objects: Unable to allocate temporary container\n"); |
| 286 |
return CLI_FAILURE; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
if (using_regex) { |
| 291 |
unsorted_container = ast_sorcery_retrieve_by_regex(geoloc_sorcery, "location", a->argv[4]); |
| 292 |
} else { |
| 293 |
unsorted_container = ast_sorcery_retrieve_by_fields(geoloc_sorcery, "location", |
| 294 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 295 |
} |
| 296 |
|
| 297 |
ret = ao2_container_dup(sorted_container, unsorted_container, 0); |
| 298 |
ao2_ref(unsorted_container, -1); |
| 299 |
if (ret != 0) { |
| 300 |
ao2_ref(sorted_container, -1); |
| 301 |
ast_cli(a->fd, "Geolocation Location Objects: Unable to sort temporary container\n"); |
| 302 |
return CLI_FAILURE; |
| 303 |
} |
| 304 |
|
| 305 |
ast_cli(a->fd, "Geolocation Location Objects:\n\n"); |
| 306 |
|
| 307 |
ast_cli(a->fd, |
| 308 |
"<Object ID...................................> <Format.....> <Details.............>\n" |
| 309 |
"===================================================================================\n"); |
| 310 |
|
| 311 |
iter = ao2_iterator_init(sorted_container, AO2_ITERATOR_UNLINK); |
| 312 |
for (; (loc = ao2_iterator_next(&iter)); ao2_ref(loc, -1)) { |
| 313 |
struct ast_str *str; |
| 314 |
|
| 315 |
ao2_lock(loc); |
| 316 |
str = ast_variable_list_join(loc->location_info, ",", "=", "\"", NULL); |
| 317 |
if (!str) { |
| 318 |
ao2_unlock(loc); |
| 319 |
ao2_ref(loc, -1); |
| 320 |
ast_cli(a->fd, "Geolocation Location Objects: Unable to allocate temp string for '%s'\n", |
| 321 |
ast_sorcery_object_get_id(loc)); |
| 322 |
result = CLI_FAILURE; |
| 323 |
break; |
| 324 |
} |
| 325 |
|
| 326 |
ast_cli(a->fd, "%-46.46s %-13s %-s\n", |
| 327 |
ast_sorcery_object_get_id(loc), |
| 328 |
format_names[loc->format], |
| 329 |
ast_str_buffer(str)); |
| 330 |
ao2_unlock(loc); |
| 331 |
ast_free(str); |
| 332 |
count++; |
| 333 |
} |
| 334 |
ao2_iterator_destroy(&iter); |
| 335 |
ao2_ref(sorted_container, -1); |
| 336 |
ast_cli(a->fd, "\nTotal Location Objects: %d\n\n", count); |
| 337 |
|
| 338 |
return result; |
| 339 |
} |
| 340 |
|
| 341 |
static char *geoloc_config_list_profiles(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 342 |
{ |
| 343 |
struct ao2_iterator iter; |
| 344 |
struct ao2_container *sorted_container; |
| 345 |
struct ao2_container *unsorted_container; |
| 346 |
struct ast_geoloc_profile *profile; |
| 347 |
int using_regex = 0; |
| 348 |
char *result = CLI_SUCCESS; |
| 349 |
int ret = 0; |
| 350 |
int count = 0; |
| 351 |
|
| 352 |
switch (cmd) { |
| 353 |
case CLI_INIT: |
| 354 |
e->command = "geoloc list profiles"; |
| 355 |
e->usage = "Usage: geoloc list profiles [ like <pattern> ]\n" |
| 356 |
" List Geolocation Profile Objects\n"; |
| 357 |
return NULL; |
| 358 |
case CLI_GENERATE: |
| 359 |
return NULL; |
| 360 |
} |
| 361 |
|
| 362 |
if (a->argc != 3 && a->argc != 5) { |
| 363 |
return CLI_SHOWUSAGE; |
| 364 |
} |
| 365 |
|
| 366 |
if (a->argc == 5) { |
| 367 |
if (strcasecmp(a->argv[3], "like")) { |
| 368 |
return CLI_SHOWUSAGE; |
| 369 |
} |
| 370 |
using_regex = 1; |
| 371 |
} |
| 372 |
|
| 373 |
sorted_container = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 374 |
ast_sorcery_object_id_sort, NULL); |
| 375 |
if (!sorted_container) { |
| 376 |
ast_cli(a->fd, "Geolocation Profile Objects: Unable to allocate temporary container\n"); |
| 377 |
return CLI_FAILURE; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
if (using_regex) { |
| 382 |
unsorted_container = ast_sorcery_retrieve_by_regex(geoloc_sorcery, "profile", a->argv[4]); |
| 383 |
} else { |
| 384 |
unsorted_container = ast_sorcery_retrieve_by_fields(geoloc_sorcery, "profile", |
| 385 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 386 |
} |
| 387 |
|
| 388 |
ret = ao2_container_dup(sorted_container, unsorted_container, 0); |
| 389 |
ao2_ref(unsorted_container, -1); |
| 390 |
if (ret != 0) { |
| 391 |
ao2_ref(sorted_container, -1); |
| 392 |
ast_cli(a->fd, "Geolocation Profile Objects: Unable to sort temporary container\n"); |
| 393 |
return CLI_FAILURE; |
| 394 |
} |
| 395 |
|
| 396 |
ast_cli(a->fd, "Geolocation Profile Objects:\n\n"); |
| 397 |
|
| 398 |
ast_cli(a->fd, |
| 399 |
"<Object ID...................................> <Profile Action> <Location Reference> \n" |
| 400 |
"=====================================================================================\n"); |
| 401 |
|
| 402 |
iter = ao2_iterator_init(sorted_container, AO2_ITERATOR_UNLINK); |
| 403 |
for (; (profile = ao2_iterator_next(&iter)); ao2_ref(profile, -1)) { |
| 404 |
ao2_lock(profile); |
| 405 |
|
| 406 |
ast_cli(a->fd, "%-46.46s %-16s %-s\n", |
| 407 |
ast_sorcery_object_get_id(profile), |
| 408 |
precedence_names[profile->precedence], |
| 409 |
profile->location_reference); |
| 410 |
ao2_unlock(profile); |
| 411 |
count++; |
| 412 |
} |
| 413 |
ao2_iterator_destroy(&iter); |
| 414 |
ao2_ref(sorted_container, -1); |
| 415 |
ast_cli(a->fd, "\nTotal Profile Objects: %d\n\n", count); |
| 416 |
|
| 417 |
return result; |
| 418 |
} |
| 419 |
|
| 420 |
static char *geoloc_config_show_profiles(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 421 |
{ |
| 422 |
struct ao2_iterator iter; |
| 423 |
struct ao2_container *sorted_container; |
| 424 |
struct ao2_container *unsorted_container; |
| 425 |
struct ast_geoloc_profile *profile; |
| 426 |
int using_regex = 0; |
| 427 |
char *result = CLI_SUCCESS; |
| 428 |
int ret = 0; |
| 429 |
int count = 0; |
| 430 |
|
| 431 |
switch (cmd) { |
| 432 |
case CLI_INIT: |
| 433 |
e->command = "geoloc show profiles"; |
| 434 |
e->usage = "Usage: geoloc show profiles [ like <pattern> ]\n" |
| 435 |
" List Geolocation Profile Objects\n"; |
| 436 |
return NULL; |
| 437 |
case CLI_GENERATE: |
| 438 |
return NULL; |
| 439 |
} |
| 440 |
|
| 441 |
if (a->argc != 3 && a->argc != 5) { |
| 442 |
return CLI_SHOWUSAGE; |
| 443 |
} |
| 444 |
|
| 445 |
if (a->argc == 5) { |
| 446 |
if (strcasecmp(a->argv[3], "like")) { |
| 447 |
return CLI_SHOWUSAGE; |
| 448 |
} |
| 449 |
using_regex = 1; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
sorted_container = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 454 |
ast_sorcery_object_id_sort, NULL); |
| 455 |
if (!sorted_container) { |
| 456 |
ast_cli(a->fd, "Geolocation Profile Objects: Unable to allocate temporary container\n"); |
| 457 |
return CLI_FAILURE; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
if (using_regex) { |
| 462 |
unsorted_container = ast_sorcery_retrieve_by_regex(geoloc_sorcery, "profile", a->argv[4]); |
| 463 |
} else { |
| 464 |
unsorted_container = ast_sorcery_retrieve_by_fields(geoloc_sorcery, "profile", |
| 465 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
ret = ao2_container_dup(sorted_container, unsorted_container, 0); |
| 470 |
ao2_ref(unsorted_container, -1); |
| 471 |
if (ret != 0) { |
| 472 |
ao2_ref(sorted_container, -1); |
| 473 |
ast_cli(a->fd, "Geolocation Profile Objects: Unable to sort temporary container\n"); |
| 474 |
return CLI_FAILURE; |
| 475 |
} |
| 476 |
|
| 477 |
ast_cli(a->fd, "Geolocation Profile Objects:\n"); |
| 478 |
|
| 479 |
iter = ao2_iterator_init(sorted_container, AO2_ITERATOR_UNLINK); |
| 480 |
for (; (profile = ao2_iterator_next(&iter)); ) { |
| 481 |
struct ast_str *loc_str = NULL; |
| 482 |
struct ast_str *refinement_str = NULL; |
| 483 |
struct ast_str *variables_str = NULL; |
| 484 |
struct ast_str *resolved_str = NULL; |
| 485 |
struct ast_str *usage_rules_str = NULL; |
| 486 |
struct ast_str *confidence_str = NULL; |
| 487 |
struct ast_geoloc_eprofile *eprofile = ast_geoloc_eprofile_create_from_profile(profile); |
| 488 |
ao2_ref(profile, -1); |
| 489 |
|
| 490 |
loc_str = ast_variable_list_join(eprofile->location_info, ",", "=", "\"", NULL); |
| 491 |
resolved_str = ast_variable_list_join(eprofile->effective_location, ",", "=", "\"", NULL); |
| 492 |
|
| 493 |
refinement_str = ast_variable_list_join(eprofile->location_refinement, ",", "=", "\"", NULL); |
| 494 |
variables_str = ast_variable_list_join(eprofile->location_variables, ",", "=", "\"", NULL); |
| 495 |
usage_rules_str = ast_variable_list_join(eprofile->usage_rules, ",", "=", "\"", NULL); |
| 496 |
confidence_str = ast_variable_list_join(eprofile->confidence, ",", "=", "\"", NULL); |
| 497 |
|
| 498 |
ast_cli(a->fd,"\n" |
| 499 |
"id: %-s\n" |
| 500 |
"profile_precedence: %-s\n" |
| 501 |
"pidf_element: %-s\n" |
| 502 |
"pidf_element_id: %-s\n" |
| 503 |
"location_reference: %-s\n" |
| 504 |
"location_format: %-s\n" |
| 505 |
"location_info: %-s\n" |
| 506 |
"location_method: %-s\n" |
| 507 |
"location_source: %-s\n" |
| 508 |
"location_confidence: %-s\n" |
| 509 |
"location_refinement: %-s\n" |
| 510 |
"location_variables: %-s\n" |
| 511 |
"allow_routing_use: %-s\n" |
| 512 |
"suppress_empty_elements: %-s\n" |
| 513 |
"effective_location: %-s\n" |
| 514 |
"usage_rules: %-s\n" |
| 515 |
"notes: %-s\n" |
| 516 |
"device_id: %-s\n", |
| 517 |
eprofile->id, |
| 518 |
precedence_names[eprofile->precedence], |
| 519 |
pidf_element_names[eprofile->pidf_element], |
| 520 |
S_OR(eprofile->pidf_element_id, "<none>"), |
| 521 |
S_OR(eprofile->location_reference, "<none>"), |
| 522 |
format_names[eprofile->format], |
| 523 |
S_COR(loc_str, ast_str_buffer(loc_str), "<none>"), |
| 524 |
S_OR(eprofile->method, "<none>"), |
| 525 |
S_OR(eprofile->location_source, "<none>"), |
| 526 |
S_COR(confidence_str, ast_str_buffer(confidence_str), "<none>"), |
| 527 |
S_COR(refinement_str, ast_str_buffer(refinement_str), "<none>"), |
| 528 |
S_COR(variables_str, ast_str_buffer(variables_str), "<none>"), |
| 529 |
S_COR(eprofile->allow_routing_use, "yes", "no"), |
| 530 |
S_COR(eprofile->suppress_empty_ca_elements, "yes", "no"), |
| 531 |
S_COR(resolved_str, ast_str_buffer(resolved_str), "<none>"), |
| 532 |
S_COR(usage_rules_str, ast_str_buffer(usage_rules_str), "<none>"), |
| 533 |
S_OR(eprofile->notes, "<none>"), |
| 534 |
S_OR(eprofile->device_id, "<none>") |
| 535 |
); |
| 536 |
ao2_ref(eprofile, -1); |
| 537 |
|
| 538 |
ast_free(loc_str); |
| 539 |
ast_free(refinement_str); |
| 540 |
ast_free(variables_str); |
| 541 |
ast_free(resolved_str); |
| 542 |
ast_free(usage_rules_str); |
| 543 |
ast_free(confidence_str); |
| 544 |
count++; |
| 545 |
} |
| 546 |
ao2_iterator_destroy(&iter); |
| 547 |
ao2_ref(sorted_container, -1); |
| 548 |
ast_cli(a->fd, "\nTotal Profile Objects: %d\n\n", count); |
| 549 |
|
| 550 |
return result; |
| 551 |
} |
| 552 |
|
| 553 |
static char *geoloc_config_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 554 |
{ |
| 555 |
char *result = CLI_SUCCESS; |
| 556 |
|
| 557 |
switch (cmd) { |
| 558 |
case CLI_INIT: |
| 559 |
e->command = "geoloc reload"; |
| 560 |
e->usage = "Usage: geoloc reload\n" |
| 561 |
" Reload Geolocation Configuration\n"; |
| 562 |
return NULL; |
| 563 |
case CLI_GENERATE: |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
|
| 567 |
if (a->argc != 2) { |
| 568 |
return CLI_SHOWUSAGE; |
| 569 |
} |
| 570 |
|
| 571 |
geoloc_config_reload(); |
| 572 |
ast_cli(a->fd, "Geolocation Configuration reloaded.\n"); |
| 573 |
|
| 574 |
return result; |
| 575 |
} |
| 576 |
|
| 577 |
static struct ast_cli_entry geoloc_location_cli_commands[] = { |
| 578 |
AST_CLI_DEFINE(geoloc_config_list_locations, "List Geolocation Location Objects"), |
| 579 |
AST_CLI_DEFINE(geoloc_config_list_profiles, "List Geolocation Profile Objects"), |
| 580 |
AST_CLI_DEFINE(geoloc_config_show_profiles, "Show Geolocation Profile Objects"), |
| 581 |
AST_CLI_DEFINE(geoloc_config_cli_reload, "Reload Geolocation Configuration"), |
| 582 |
}; |
| 583 |
|
| 584 |
struct ast_geoloc_location * AST_OPTIONAL_API_NAME(ast_geoloc_get_location)(const char *id) |
| 585 |
{ |
| 586 |
if (ast_strlen_zero(id)) { |
| 587 |
return NULL; |
| 588 |
} |
| 589 |
|
| 590 |
return ast_sorcery_retrieve_by_id(geoloc_sorcery, "location", id); |
| 591 |
} |
| 592 |
|
| 593 |
struct ast_geoloc_profile * AST_OPTIONAL_API_NAME(ast_geoloc_get_profile)(const char *id) |
| 594 |
{ |
| 595 |
if (ast_strlen_zero(id)) { |
| 596 |
return NULL; |
| 597 |
} |
| 598 |
|
| 599 |
return ast_sorcery_retrieve_by_id(geoloc_sorcery, "profile", id); |
| 600 |
} |
| 601 |
|
| 602 |
int geoloc_config_reload(void) |
| 603 |
{ |
| 604 |
if (geoloc_sorcery) { |
| 605 |
ast_sorcery_reload(geoloc_sorcery); |
| 606 |
} |
| 607 |
return AST_MODULE_LOAD_SUCCESS; |
| 608 |
} |
| 609 |
|
| 610 |
int geoloc_config_unload(void) |
| 611 |
{ |
| 612 |
ast_cli_unregister_multiple(geoloc_location_cli_commands, ARRAY_LEN(geoloc_location_cli_commands)); |
| 613 |
|
| 614 |
ast_sorcery_object_unregister(geoloc_sorcery, "profile"); |
| 615 |
ast_sorcery_object_unregister(geoloc_sorcery, "location"); |
| 616 |
|
| 617 |
if (geoloc_sorcery) { |
| 618 |
ast_sorcery_unref(geoloc_sorcery); |
| 619 |
} |
| 620 |
geoloc_sorcery = NULL; |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
static int default_profile_create(const char *name) |
| 626 |
{ |
| 627 |
int rc = 0; |
| 628 |
struct ast_geoloc_profile *profile; |
| 629 |
char *id = ast_alloca(strlen(name) + 3 ); |
| 630 |
|
| 631 |
sprintf(id, "<%s>", name); |
| 632 |
profile = ast_sorcery_alloc(geoloc_sorcery, "profile", id); |
| 633 |
ast_assert_return(profile != NULL, 0); |
| 634 |
|
| 635 |
profile->precedence = ast_geoloc_precedence_str_to_enum(name); |
| 636 |
profile->pidf_element = AST_PIDF_ELEMENT_DEVICE; |
| 637 |
rc = ast_sorcery_create(geoloc_sorcery, profile); |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
ao2_ref(profile, -1); |
| 643 |
|
| 644 |
|
| 645 |
return rc == 0 ? 1 : 0; |
| 646 |
} |
| 647 |
|
| 648 |
static int geoloc_load_default_profiles(void) |
| 649 |
{ |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
ast_assert_return(default_profile_create("prefer_config"), -1); |
| 656 |
ast_assert_return(default_profile_create("discard_config"), -1); |
| 657 |
ast_assert_return(default_profile_create("prefer_incoming"), -1); |
| 658 |
ast_assert_return(default_profile_create("discard_incoming"), -1); |
| 659 |
|
| 660 |
return 0; |
| 661 |
} |
| 662 |
|
| 663 |
int geoloc_config_load(void) |
| 664 |
{ |
| 665 |
enum ast_sorcery_apply_result result; |
| 666 |
int rc = 0; |
| 667 |
|
| 668 |
if (!(geoloc_sorcery = ast_sorcery_open())) { |
| 669 |
ast_log(LOG_ERROR, "Failed to open geolocation sorcery\n"); |
| 670 |
return AST_MODULE_LOAD_DECLINE; |
| 671 |
} |
| 672 |
|
| 673 |
ast_sorcery_apply_config(geoloc_sorcery, "location"); |
| 674 |
result = ast_sorcery_apply_default(geoloc_sorcery, "location", "config", "geolocation.conf,criteria=type=location"); |
| 675 |
if (result != AST_SORCERY_APPLY_SUCCESS) { |
| 676 |
ast_log(LOG_ERROR, "Failed to apply defaults for geoloc location object with sorcery\n"); |
| 677 |
return AST_MODULE_LOAD_DECLINE; |
| 678 |
} |
| 679 |
|
| 680 |
rc = ast_sorcery_object_register(geoloc_sorcery, "location", geoloc_location_alloc, NULL, geoloc_location_apply_handler); |
| 681 |
if (rc != 0) { |
| 682 |
ast_log(LOG_ERROR, "Failed to register geoloc location object with sorcery\n"); |
| 683 |
return AST_MODULE_LOAD_DECLINE; |
| 684 |
} |
| 685 |
|
| 686 |
ast_sorcery_object_field_register(geoloc_sorcery, "location", "type", "", OPT_NOOP_T, 0, 0); |
| 687 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "location", "format", AST_GEOLOC_FORMAT_NONE, |
| 688 |
location_format_handler, location_format_to_str, NULL, 0, 0); |
| 689 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "location", "location_info", NULL, |
| 690 |
location_location_info_handler, location_location_info_to_str, location_location_info_dup, 0, 0); |
| 691 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "location", "confidence", NULL, |
| 692 |
location_confidence_handler, location_confidence_to_str, location_confidence_dup, 0, 0); |
| 693 |
ast_sorcery_object_field_register(geoloc_sorcery, "location", "location_source", "", OPT_STRINGFIELD_T, |
| 694 |
0, STRFLDSET(struct ast_geoloc_location, location_source)); |
| 695 |
ast_sorcery_object_field_register(geoloc_sorcery, "location", "method", "", OPT_STRINGFIELD_T, |
| 696 |
0, STRFLDSET(struct ast_geoloc_location, method)); |
| 697 |
|
| 698 |
|
| 699 |
ast_sorcery_apply_config(geoloc_sorcery, "profile"); |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
result = ast_sorcery_apply_wizard_mapping(geoloc_sorcery, "profile", "memory", NULL, 0); |
| 704 |
if (result == AST_SORCERY_APPLY_FAIL) { |
| 705 |
ast_log(LOG_ERROR, "Failed to add memory wizard mapping to geoloc profile object\n"); |
| 706 |
return AST_MODULE_LOAD_DECLINE; |
| 707 |
} |
| 708 |
|
| 709 |
result = ast_sorcery_apply_wizard_mapping(geoloc_sorcery, "profile", "config", |
| 710 |
"geolocation.conf,criteria=type=profile", 0); |
| 711 |
if (result == AST_SORCERY_APPLY_FAIL) { |
| 712 |
ast_log(LOG_ERROR, "Failed to add memory wizard mapping to geoloc profile object\n"); |
| 713 |
return AST_MODULE_LOAD_DECLINE; |
| 714 |
} |
| 715 |
rc = ast_sorcery_object_register(geoloc_sorcery, "profile", geoloc_profile_alloc, NULL, geoloc_profile_apply_handler); |
| 716 |
if (rc != 0) { |
| 717 |
ast_log(LOG_ERROR, "Failed to register geoloc profile object with sorcery\n"); |
| 718 |
return AST_MODULE_LOAD_DECLINE; |
| 719 |
} |
| 720 |
|
| 721 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "type", "", OPT_NOOP_T, 0, 0); |
| 722 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "profile", "pidf_element", |
| 723 |
pidf_element_names[AST_PIDF_ELEMENT_DEVICE], profile_pidf_element_handler, profile_pidf_element_to_str, NULL, 0, 0); |
| 724 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "pidf_element_id", "", OPT_STRINGFIELD_T, |
| 725 |
0, STRFLDSET(struct ast_geoloc_profile, pidf_element_id)); |
| 726 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "location_reference", "", OPT_STRINGFIELD_T, |
| 727 |
0, STRFLDSET(struct ast_geoloc_profile, location_reference)); |
| 728 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "profile", "profile_precedence", "discard_incoming", |
| 729 |
profile_precedence_handler, profile_precedence_to_str, NULL, 0, 0); |
| 730 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "profile", "usage_rules", NULL, |
| 731 |
profile_usage_rules_handler, profile_usage_rules_to_str, profile_usage_rules_dup, 0, 0); |
| 732 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "profile", "location_info_refinement", NULL, |
| 733 |
profile_location_refinement_handler, profile_location_refinement_to_str, profile_location_refinement_dup, 0, 0); |
| 734 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "profile", "location_variables", NULL, |
| 735 |
profile_location_variables_handler, profile_location_variables_to_str, profile_location_variables_dup, 0, 0); |
| 736 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "notes", "", OPT_STRINGFIELD_T, |
| 737 |
0, STRFLDSET(struct ast_geoloc_profile, notes)); |
| 738 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "allow_routing_use", |
| 739 |
"no", OPT_BOOL_T, 1, FLDSET(struct ast_geoloc_profile, allow_routing_use)); |
| 740 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "suppress_empty_ca_elements", |
| 741 |
"no", OPT_BOOL_T, 1, FLDSET(struct ast_geoloc_profile, suppress_empty_ca_elements)); |
| 742 |
|
| 743 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "profile", "format", AST_GEOLOC_FORMAT_NONE, |
| 744 |
profile_format_handler, profile_format_to_str, NULL, 0, 0); |
| 745 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "profile", "location_info", NULL, |
| 746 |
profile_location_info_handler, profile_location_info_to_str, profile_location_info_dup, 0, 0); |
| 747 |
ast_sorcery_object_field_register_custom(geoloc_sorcery, "profile", "confidence", NULL, |
| 748 |
profile_confidence_handler, profile_confidence_to_str, profile_confidence_dup, 0, 0); |
| 749 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "location_source", "", OPT_STRINGFIELD_T, |
| 750 |
0, STRFLDSET(struct ast_geoloc_profile, location_source)); |
| 751 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "method", "", OPT_STRINGFIELD_T, |
| 752 |
0, STRFLDSET(struct ast_geoloc_profile, method)); |
| 753 |
ast_sorcery_object_field_register(geoloc_sorcery, "profile", "device_id", "", OPT_STRINGFIELD_T, |
| 754 |
0, STRFLDSET(struct ast_geoloc_profile, device_id)); |
| 755 |
|
| 756 |
ast_sorcery_load(geoloc_sorcery); |
| 757 |
|
| 758 |
rc = geoloc_load_default_profiles(); |
| 759 |
if (rc != 0) { |
| 760 |
ast_log(LOG_ERROR, "Failed to load default geoloc profiles\n"); |
| 761 |
return AST_MODULE_LOAD_DECLINE; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
ast_cli_register_multiple(geoloc_location_cli_commands, ARRAY_LEN(geoloc_location_cli_commands)); |
| 766 |
|
| 767 |
return AST_MODULE_LOAD_SUCCESS; |
| 768 |
} |
| 769 |
|
| 770 |
int AST_OPTIONAL_API_NAME(ast_geoloc_is_loaded)(void) |
| 771 |
{ |
| 772 |
return 1; |
| 773 |
} |
| 774 |
|