2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2014, Fairview 5 Engineering, LLC
6 * George Joseph <george.joseph@fairview5.com>
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.
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.
21 * \brief PJSIP Phoneprov Configuration Provider
23 * \author George Joseph <george.joseph@fairview5.com>
26 /*! \li \ref res_pjsip_phoneprov_provider.c uses the configuration file \ref pjsip.conf
27 * \addtogroup configuration_file Configuration Files
31 * \page pjsip.conf pjsip.conf
32 * \verbinclude pjsip.conf.sample
36 <depend>pjproject</depend>
37 <depend>res_pjsip</depend>
38 <depend>res_phoneprov</depend>
39 <support_level>extended</support_level>
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"
53 <configInfo name="res_pjsip_phoneprov_provider" language="en_US">
54 <synopsis>Module that integrates res_pjsip with res_phoneprov.</synopsis>
56 <emphasis>PJSIP Phoneprov Provider</emphasis>
58 <para>This module creates the integration between <literal>res_pjsip</literal> and
59 <literal>res_phoneprov</literal>.
61 <para>Each user to be integrated requires a <literal>phoneprov</literal>
62 section defined in <filename>pjsip.conf</filename>. Each section identifies
63 the endpoint associated with the user and any other name/value pairs to be passed
64 on to res_phoneprov's template substitution. Only <literal>MAC</literal> and
65 <literal>PROFILE</literal> variables are required. Any other variables
66 supplied will be passed through.</para>
70 <para>type = phoneprovr</para>
71 <para>endpoint = ep1000</para>
72 <para>MAC = deadbeef4dad</para>
73 <para>PROFILE = grandstream2</para>
74 <para>LINEKEYS = 2</para>
76 <para>OTHERVAR = othervalue</para>
78 <para>The following variables are automatically defined if an endpoint
79 is defined for the user:</para>
81 <enum name="USERNAME"><para>Source: The user_name defined in the first auth reference
82 in the endpoint.</para></enum>
83 <enum name="SECRET"><para>Source: The user_pass defined in the first auth reference
84 in the endpoint.</para></enum>
85 <enum name="CALLERID"><para>Source: The number part of the callerid defined in
86 the endpoint.</para></enum>
87 <enum name="DISPLAY_NAME"><para>Source: The name part of the callerid defined in
88 the endpoint.</para></enum>
89 <enum name="LABEL"><para>Source: The id of the phoneprov section.</para></enum>
92 <para>In addition to the standard variables, the following are also automatically defined:</para>
94 <enum name="ENDPOINT_ID"><para>Source: The id of the endpoint.</para></enum>
95 <enum name="TRANSPORT_ID"><para>Source: The id of the transport used by the endpoint.</para></enum>
96 <enum name="AUTH_ID"><para>Source: The id of the auth used by the endpoint.</para></enum>
99 <para>All other template substitution variables must be explicitly defined in the
100 phoneprov_default or phoneprov sections.</para>
103 <configFile name="pjsip.conf">
104 <configObject name="phoneprov">
105 <synopsis>Provides variables for each user.</synopsis>
106 <configOption name="type">
107 <synopsis>Must be of type 'phoneprov'.</synopsis>
109 <configOption name="endpoint">
110 <synopsis>The endpoint from which variables will be retrieved.</synopsis>
112 <configOption name="MAC">
113 <synopsis>The mac address for this user. (required)</synopsis>
115 <configOption name="PROFILE">
116 <synopsis>The phoneprov profile to use for this user. (required)</synopsis>
118 <configOption name="*">
119 <synopsis>Other name/value pairs to be passed through for use in templates.</synopsis>
126 static struct ast_sorcery *sorcery;
128 /*! \brief Structure for a phoneprov object */
130 SORCERY_OBJECT(details);
131 struct varshead *vars;
134 /*! \brief Destructor function for phoneprov */
135 static void phoneprov_destroy(void *obj)
137 struct phoneprov *pp = obj;
138 char *mac = ast_var_find(pp->vars, "MAC");
141 ast_phoneprov_delete_extension(AST_MODULE, mac);
144 ast_var_list_destroy(pp->vars);
147 /*! \brief Allocator for phoneprov */
148 static void *phoneprov_alloc(const char *name)
150 struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy);
152 if (!pp || !(pp->vars = ast_var_list_create())) {
153 ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n",
162 /*! \brief Helper that creates an ast_var_t and inserts it into the list */
163 static int assign_and_insert(const char *name, const char *value, struct varshead *vars)
165 struct ast_var_t *var;
167 if (ast_strlen_zero(name) || !vars) {
171 /* Just ignore if the value is NULL or empty */
172 if (ast_strlen_zero(value)) {
176 var = ast_var_assign(name, value);
178 ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n");
181 AST_VAR_LIST_INSERT_TAIL(vars, var);
186 /*! \brief Adds a config name/value pair to the phoneprov object */
187 static int aco_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
189 struct phoneprov *pp = obj;
191 return assign_and_insert(var->name, var->value, pp->vars);
194 /*! \brief Converts the phoneprov varlist to an ast_variable list */
195 static int fields_handler(const void *obj, struct ast_variable **fields)
197 const struct phoneprov *pp = obj;
198 struct ast_var_t *pvar;
199 struct ast_variable *head = NULL;
200 struct ast_variable *tail = NULL;
201 struct ast_variable *var;
203 AST_VAR_LIST_TRAVERSE(pp->vars, pvar) {
204 var = ast_variable_new(pvar->name, pvar->value, "");
206 ast_variables_destroy(head);
223 static int load_endpoint(const char *id, const char *endpoint_name, struct varshead *vars,
226 struct ast_sip_auth *auth;
227 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
228 RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
229 const char *auth_name;
233 /* We need to use res_pjsip's sorcery instance instead of our own to
234 * get endpoint and auth.
236 endpoint = ast_sorcery_retrieve_by_id(sorcery, "endpoint",
239 ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id,
244 assign_and_insert("ENDPOINT_ID", endpoint_name, vars);
245 assign_and_insert("TRANSPORT_ID", endpoint->transport, vars);
247 if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) {
248 assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_CALLERID),
249 endpoint->id.self.number.str, vars);
252 if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) {
254 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_DISPLAY_NAME),
255 endpoint->id.self.name.str, vars);
258 transport = ast_sorcery_retrieve_by_id(sorcery, "transport",
259 endpoint->transport);
261 ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name,
262 endpoint->transport);
265 snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host));
267 if (!AST_VECTOR_SIZE(&endpoint->inbound_auths)) {
270 auth_name = AST_VECTOR_GET(&endpoint->inbound_auths, 0);
272 auth = ast_sorcery_retrieve_by_id(sorcery, "auth", auth_name);
274 ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name);
278 assign_and_insert("AUTH_ID", auth_name, vars);
279 assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME),
280 auth->auth_user, vars);
281 assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SECRET),
282 auth->auth_pass, vars);
288 /*! \brief Callback that validates the phoneprov object */
289 static int users_apply_handler(void *obj, void *arg, int flags)
291 struct phoneprov *pp = obj;
292 const char *id = ast_sorcery_object_get_id(pp);
293 const char *endpoint_name;
296 if (!ast_var_find(pp->vars,
297 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_MAC))) {
298 ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id);
302 if (!ast_var_find(pp->vars,
303 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
304 ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id);
308 endpoint_name = ast_var_find(pp->vars, "endpoint");
310 if (load_endpoint(id, endpoint_name, pp->vars, port_string)) {
315 if (!ast_var_find(pp->vars,
316 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME))) {
318 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME), id,
322 if (!ast_var_find(pp->vars,
323 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL))) {
324 assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL),
328 if (!ast_var_find(pp->vars,
329 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SERVER_PORT))) {
330 assign_and_insert("SERVER_PORT", S_OR(port_string, "5060"), pp->vars);
333 if (!ast_var_find(pp->vars,
334 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
335 ast_log(LOG_ERROR, "phoneprov %s didn't contain a PROFILE entry.\n", id);
338 ast_phoneprov_add_extension(AST_MODULE, pp->vars);
343 /*! \brief Callback that loads the users from phoneprov sections */
344 static int load_users(void)
346 struct ao2_container *users;
348 ast_sorcery_reload_object(sorcery, "phoneprov");
350 users = ast_sorcery_retrieve_by_fields(sorcery, "phoneprov",
351 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
356 ao2_callback(users, OBJ_MULTIPLE | OBJ_NODATA, users_apply_handler, sorcery);
363 static int load_module(void)
365 CHECK_PJSIP_MODULE_LOADED();
367 sorcery = ast_sip_get_sorcery();
369 ast_sorcery_apply_config(sorcery, "res_pjsip_phoneprov_provider");
370 ast_sorcery_apply_default(sorcery, "phoneprov", "config",
371 "pjsip.conf,criteria=type=phoneprov");
373 ast_sorcery_object_register(sorcery, "phoneprov", phoneprov_alloc, NULL,
376 ast_sorcery_object_field_register(sorcery, "phoneprov", "type", "", OPT_NOOP_T, 0,
378 ast_sorcery_object_fields_register(sorcery, "phoneprov", "^", aco_handler,
381 ast_sorcery_load_object(sorcery, "phoneprov");
383 if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
384 ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
385 return AST_MODULE_LOAD_DECLINE;
388 return AST_MODULE_LOAD_SUCCESS;
391 static int unload_module(void)
393 ast_phoneprov_provider_unregister(AST_MODULE);
398 static int reload_module(void)
400 ast_phoneprov_provider_unregister(AST_MODULE);
402 if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
403 ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
404 return AST_MODULE_LOAD_DECLINE;
410 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Phoneprov Provider",
412 .reload = reload_module,
413 .unload = unload_module,
414 .load_pri = AST_MODPRI_APP_DEPEND,