| 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 |
#include "asterisk.h" |
| 43 |
|
| 44 |
#include <pjsip.h> |
| 45 |
|
| 46 |
#include "asterisk/res_pjsip.h" |
| 47 |
#include "asterisk/module.h" |
| 48 |
#include "asterisk/sorcery.h" |
| 49 |
#include "asterisk/phoneprov.h" |
| 50 |
#include "res_pjsip/include/res_pjsip_private.h" |
| 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 |
static struct ast_sorcery *sorcery; |
| 133 |
|
| 134 |
|
| 135 |
struct phoneprov { |
| 136 |
SORCERY_OBJECT(details); |
| 137 |
struct varshead *vars; |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
static void phoneprov_destroy(void *obj) |
| 142 |
{ |
| 143 |
struct phoneprov *pp = obj; |
| 144 |
char *mac = ast_var_find(pp->vars, "MAC"); |
| 145 |
|
| 146 |
if (mac) { |
| 147 |
ast_phoneprov_delete_extension(AST_MODULE, mac); |
| 148 |
} |
| 149 |
|
| 150 |
ast_var_list_destroy(pp->vars); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
static void *phoneprov_alloc(const char *name) |
| 155 |
{ |
| 156 |
struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy); |
| 157 |
|
| 158 |
if (!pp || !(pp->vars = ast_var_list_create())) { |
| 159 |
ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n", |
| 160 |
name); |
| 161 |
ao2_cleanup(pp); |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
return pp; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
static int assign_and_insert(const char *name, const char *value, struct varshead *vars) |
| 170 |
{ |
| 171 |
struct ast_var_t *var; |
| 172 |
|
| 173 |
if (ast_strlen_zero(name) || !vars) { |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
if (ast_strlen_zero(value)) { |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
var = ast_var_assign(name, value); |
| 183 |
if (!var) { |
| 184 |
ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n"); |
| 185 |
return -1; |
| 186 |
} |
| 187 |
AST_VAR_LIST_INSERT_TAIL(vars, var); |
| 188 |
|
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
static int aco_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 194 |
{ |
| 195 |
struct phoneprov *pp = obj; |
| 196 |
|
| 197 |
return assign_and_insert(var->name, var->value, pp->vars); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
static int fields_handler(const void *obj, struct ast_variable **fields) |
| 202 |
{ |
| 203 |
const struct phoneprov *pp = obj; |
| 204 |
struct ast_var_t *pvar; |
| 205 |
struct ast_variable *head = NULL; |
| 206 |
struct ast_variable *tail = NULL; |
| 207 |
struct ast_variable *var; |
| 208 |
|
| 209 |
AST_VAR_LIST_TRAVERSE(pp->vars, pvar) { |
| 210 |
var = ast_variable_new(pvar->name, pvar->value, ""); |
| 211 |
if (!var) { |
| 212 |
ast_variables_destroy(head); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
if (!head) { |
| 216 |
head = var; |
| 217 |
tail = var; |
| 218 |
continue; |
| 219 |
} |
| 220 |
tail->next = var; |
| 221 |
tail = var; |
| 222 |
} |
| 223 |
|
| 224 |
*fields = head; |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
static int load_endpoint(const char *id, const char *endpoint_name, struct varshead *vars, |
| 230 |
char *port_string) |
| 231 |
{ |
| 232 |
struct ast_sip_auth *auth; |
| 233 |
RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); |
| 234 |
RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup); |
| 235 |
const char *auth_name; |
| 236 |
|
| 237 |
*port_string = '\0'; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
endpoint = ast_sorcery_retrieve_by_id(sorcery, "endpoint", |
| 243 |
endpoint_name); |
| 244 |
if (!endpoint) { |
| 245 |
ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id, |
| 246 |
endpoint_name); |
| 247 |
return -1; |
| 248 |
} |
| 249 |
|
| 250 |
assign_and_insert("ENDPOINT_ID", endpoint_name, vars); |
| 251 |
assign_and_insert("TRANSPORT_ID", endpoint->transport, vars); |
| 252 |
|
| 253 |
if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) { |
| 254 |
assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_CALLERID), |
| 255 |
endpoint->id.self.number.str, vars); |
| 256 |
} |
| 257 |
|
| 258 |
if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) { |
| 259 |
assign_and_insert( |
| 260 |
ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_DISPLAY_NAME), |
| 261 |
endpoint->id.self.name.str, vars); |
| 262 |
} |
| 263 |
|
| 264 |
transport = ast_sorcery_retrieve_by_id(sorcery, "transport", |
| 265 |
endpoint->transport); |
| 266 |
if (!transport) { |
| 267 |
ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name, |
| 268 |
endpoint->transport); |
| 269 |
return -1; |
| 270 |
} |
| 271 |
snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host)); |
| 272 |
|
| 273 |
if (!AST_VECTOR_SIZE(&endpoint->inbound_auths)) { |
| 274 |
return 0; |
| 275 |
} |
| 276 |
auth_name = AST_VECTOR_GET(&endpoint->inbound_auths, 0); |
| 277 |
|
| 278 |
auth = ast_sorcery_retrieve_by_id(sorcery, "auth", auth_name); |
| 279 |
if (!auth) { |
| 280 |
ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name); |
| 281 |
return -1; |
| 282 |
} |
| 283 |
|
| 284 |
assign_and_insert("AUTH_ID", auth_name, vars); |
| 285 |
assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME), |
| 286 |
auth->auth_user, vars); |
| 287 |
assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SECRET), |
| 288 |
auth->auth_pass, vars); |
| 289 |
ao2_ref(auth, -1); |
| 290 |
|
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
static void users_apply_handler(struct phoneprov *pp) |
| 296 |
{ |
| 297 |
const char *id = ast_sorcery_object_get_id(pp); |
| 298 |
const char *endpoint_name; |
| 299 |
char port_string[6]; |
| 300 |
|
| 301 |
if (!ast_var_find(pp->vars, |
| 302 |
ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_MAC))) { |
| 303 |
ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
if (!ast_var_find(pp->vars, |
| 308 |
ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) { |
| 309 |
ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id); |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
endpoint_name = ast_var_find(pp->vars, "endpoint"); |
| 314 |
if (endpoint_name) { |
| 315 |
if (load_endpoint(id, endpoint_name, pp->vars, port_string)) { |
| 316 |
return; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
if (!ast_var_find(pp->vars, |
| 321 |
ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME))) { |
| 322 |
assign_and_insert( |
| 323 |
ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME), id, |
| 324 |
pp->vars); |
| 325 |
} |
| 326 |
|
| 327 |
if (!ast_var_find(pp->vars, |
| 328 |
ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL))) { |
| 329 |
assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL), |
| 330 |
id, pp->vars); |
| 331 |
} |
| 332 |
|
| 333 |
if (!ast_var_find(pp->vars, |
| 334 |
ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SERVER_PORT))) { |
| 335 |
assign_and_insert("SERVER_PORT", S_OR(port_string, "5060"), pp->vars); |
| 336 |
} |
| 337 |
|
| 338 |
if (!ast_var_find(pp->vars, |
| 339 |
ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) { |
| 340 |
ast_log(LOG_ERROR, "phoneprov %s didn't contain a PROFILE entry.\n", id); |
| 341 |
} |
| 342 |
|
| 343 |
ast_phoneprov_add_extension(AST_MODULE, pp->vars); |
| 344 |
|
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
static int load_users(void) |
| 350 |
{ |
| 351 |
struct ao2_container *users; |
| 352 |
struct ao2_iterator i; |
| 353 |
struct phoneprov *pp; |
| 354 |
|
| 355 |
ast_sorcery_reload_object(sorcery, "phoneprov"); |
| 356 |
|
| 357 |
users = ast_sorcery_retrieve_by_fields(sorcery, "phoneprov", |
| 358 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 359 |
if (!users) { |
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
i = ao2_iterator_init(users, 0); |
| 364 |
while ((pp = ao2_iterator_next(&i))) { |
| 365 |
users_apply_handler(pp); |
| 366 |
ao2_ref(pp, -1); |
| 367 |
} |
| 368 |
ao2_iterator_destroy(&i); |
| 369 |
ao2_ref(users, -1); |
| 370 |
|
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
static int load_module(void) |
| 375 |
{ |
| 376 |
sorcery = ast_sip_get_sorcery(); |
| 377 |
|
| 378 |
ast_sorcery_apply_config(sorcery, "res_pjsip_phoneprov_provider"); |
| 379 |
ast_sorcery_apply_default(sorcery, "phoneprov", "config", |
| 380 |
"pjsip.conf,criteria=type=phoneprov"); |
| 381 |
|
| 382 |
ast_sorcery_object_register(sorcery, "phoneprov", phoneprov_alloc, NULL, |
| 383 |
NULL); |
| 384 |
|
| 385 |
ast_sorcery_object_field_register(sorcery, "phoneprov", "type", "", OPT_NOOP_T, 0, |
| 386 |
0); |
| 387 |
ast_sorcery_object_fields_register(sorcery, "phoneprov", "^", aco_handler, |
| 388 |
fields_handler); |
| 389 |
|
| 390 |
ast_sorcery_load_object(sorcery, "phoneprov"); |
| 391 |
|
| 392 |
if (ast_phoneprov_provider_register(AST_MODULE, load_users)) { |
| 393 |
ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n"); |
| 394 |
return AST_MODULE_LOAD_DECLINE; |
| 395 |
} |
| 396 |
|
| 397 |
return AST_MODULE_LOAD_SUCCESS; |
| 398 |
} |
| 399 |
|
| 400 |
static int unload_module(void) |
| 401 |
{ |
| 402 |
ast_phoneprov_provider_unregister(AST_MODULE); |
| 403 |
|
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
static int reload_module(void) |
| 408 |
{ |
| 409 |
ast_phoneprov_provider_unregister(AST_MODULE); |
| 410 |
|
| 411 |
if (ast_phoneprov_provider_register(AST_MODULE, load_users)) { |
| 412 |
ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n"); |
| 413 |
return AST_MODULE_LOAD_DECLINE; |
| 414 |
} |
| 415 |
|
| 416 |
return 0; |
| 417 |
} |
| 418 |
|
| 419 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Phoneprov Provider", |
| 420 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 421 |
.load = load_module, |
| 422 |
.reload = reload_module, |
| 423 |
.unload = unload_module, |
| 424 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 425 |
.requires = "res_pjsip,res_phoneprov", |
| 426 |
); |