res_pjsip_phoneprov_provider: Provides pjsip integration with res_phoneprov
[asterisk/asterisk.git] / res / res_pjsip_phoneprov_provider.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Fairview 5 Engineering, LLC
5  *
6  * George Joseph <george.joseph@fairview5.com>
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 /*! \file
20  *
21  * \brief PJSIP Phoneprov Configuration Provider
22  *
23  * \author George Joseph <george.joseph@fairview5.com>
24   */
25
26 /*! \li \ref res_pjsip_phoneprov_provider.c uses the configuration file \ref pjsip.conf
27  * \addtogroup configuration_file Configuration Files
28  */
29
30 /*!
31  * \page pjsip.conf pjsip.conf
32  * \verbinclude pjsip.conf.sample
33  */
34
35 /*** MODULEINFO
36         <depend>res_pjsip</depend>
37         <depend>res_phoneprov</depend>
38         <support_level>extended</support_level>
39  ***/
40
41 #include "asterisk.h"
42
43 #include <pjsip.h>
44
45 #include "asterisk/res_pjsip.h"
46 #include "asterisk/module.h"
47 #include "asterisk/sorcery.h"
48 #include "asterisk/phoneprov.h"
49 #include "res_pjsip/include/res_pjsip_private.h"
50
51 /*** DOCUMENTATION
52         <configInfo name="res_pjsip_phoneprov_provider" language="en_US">
53                 <synopsis>Module that integrates res_pjsip with res_phoneprov.</synopsis>
54                 <description><para>
55                         <emphasis>PJSIP Phoneprov Provider</emphasis>
56                         </para>
57                         <para>This module creates the integration between <literal>res_pjsip</literal> and
58                         <literal>res_phoneprov</literal>.
59                         </para>
60                         <para>Each user to be integrated requires a <literal>phoneprov</literal>
61                         section defined in <filename>pjsip.conf</filename>.  Each section identifies
62                         the endpoint associated with the user and any other name/value pairs to be passed
63                         on to res_phoneprov's template substitution.  Only <literal>MAC</literal> and
64                         <literal>PROFILE</literal> variables are required.  Any other variables
65                         supplied will be passed through.</para>
66                         <para> </para>
67                         <para>Example:</para>
68                         <para>[1000]</para>
69                         <para>type = phoneprovr</para>
70                         <para>endpoint = ep1000</para>
71                         <para>MAC = deadbeef4dad</para>
72                         <para>PROFILE = grandstream2</para>
73                         <para>LINEKEYS = 2</para>
74                         <para>LINE = 1</para>
75                         <para>OTHERVAR = othervalue</para>
76                         <para> </para>
77                         <para>The following variables are automatically defined if an endpoint
78                         is defined for the user:</para>
79                         <enumlist>
80                                 <enum name="USERNAME"><para>Source: The user_name defined in the first auth reference
81                                 in the endpoint.</para></enum>
82                                 <enum name="SECRET"><para>Source: The user_pass defined in the first auth reference
83                                 in the endpoint.</para></enum>
84                                 <enum name="CALLERID"><para>Source: The number part of the callerid defined in
85                                 the endpoint.</para></enum>
86                                 <enum name="DISPLAY_NAME"><para>Source: The name part of the callerid defined in
87                                 the endpoint.</para></enum>
88                                 <enum name="LABEL"><para>Source: The id of the phoneprov section.</para></enum>
89                         </enumlist>
90                         <para> </para>
91                         <para>In addition to the standard variables, the following are also automatically defined:</para>
92                         <enumlist>
93                                 <enum name="ENDPOINT_ID"><para>Source: The id of the endpoint.</para></enum>
94                                 <enum name="TRANSPORT_ID"><para>Source: The id of the transport used by the endpoint.</para></enum>
95                                 <enum name="AUTH_ID"><para>Source: The id of the auth used by the endpoint.</para></enum>
96                         </enumlist>
97                         <para> </para>
98                         <para>All other template substitution variables must be explicitly defined in the
99                         phoneprov_default or phoneprov sections.</para>
100                 </description>
101
102                 <configFile name="pjsip.conf">
103                         <configObject name="phoneprov">
104                                 <synopsis>Provides variables for each user.</synopsis>
105                                 <configOption name="type">
106                                         <synopsis>Must be of type 'phoneprov'.</synopsis>
107                                 </configOption>
108                                 <configOption name="endpoint">
109                                         <synopsis>The endpoint from which variables will be retrieved.</synopsis>
110                                 </configOption>
111                                 <configOption name="MAC">
112                                         <synopsis>The mac address for this user. (required)</synopsis>
113                                 </configOption>
114                                 <configOption name="PROFILE">
115                                         <synopsis>The phoneprov profile to use for this user. (required)</synopsis>
116                                 </configOption>
117                                 <configOption name="*">
118                                         <synopsis>Other name/value pairs to be passed through for use in templates.</synopsis>
119                                 </configOption>
120                         </configObject>
121                 </configFile>
122         </configInfo>
123  ***/
124
125 static struct ast_sorcery *sorcery;
126
127 /*! \brief Structure for a phoneprov object */
128 struct phoneprov {
129         SORCERY_OBJECT(details);
130         struct varshead *vars;
131 };
132
133 /*! \brief Destructor function for phoneprov */
134 static void phoneprov_destroy(void *obj)
135 {
136         struct phoneprov *pp = obj;
137
138         ast_var_list_destroy(pp->vars);
139 }
140
141 /*! \brief Allocator for phoneprov */
142 static void *phoneprov_alloc(const char *name)
143 {
144         struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy);
145
146         if (!pp || !(pp->vars = ast_var_list_create())) {
147                 ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n",
148                         name);
149                 ao2_cleanup(pp);
150                 return NULL;
151         }
152
153         return pp;
154 }
155
156 /*! \brief Helper that creates an ast_var_t and inserts it into the list */
157 static int assign_and_insert(const char *name, const char *value, struct varshead *vars)
158 {
159         struct ast_var_t *var;
160
161         if (ast_strlen_zero(name) || !vars) {
162                 return -1;
163         }
164
165         /* Just ignore if the value is NULL or empty */
166         if (ast_strlen_zero(value)) {
167                 return 0;
168         }
169
170         var = ast_var_assign(name, value);
171         if (!var) {
172                 ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n");
173                 return -1;
174         }
175         AST_VAR_LIST_INSERT_TAIL(vars, var);
176
177         return 0;
178 }
179
180 /*! \brief Adds a config name/value pair to the phoneprov object */
181 static int aco_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
182 {
183         struct phoneprov *pp = obj;
184
185         return assign_and_insert(var->name, var->value, pp->vars);
186 }
187
188 /*! \brief Converts the phoneprov varlist to an ast_variable list */
189 static int fields_handler(const void *obj, struct ast_variable **fields)
190 {
191         const struct phoneprov *pp = obj;
192         struct ast_var_t *pvar;
193         struct ast_variable *head = NULL;
194         struct ast_variable *tail = NULL;
195         struct ast_variable *var;
196
197         AST_VAR_LIST_TRAVERSE(pp->vars, pvar) {
198                 var = ast_variable_new(pvar->name, pvar->value, NULL);
199                 if (!var) {
200                         ast_variables_destroy(head);
201                         return -1;
202                 }
203                 if (!head) {
204                         head = var;
205                         tail = var;
206                         continue;
207                 }
208                 tail->next = var;
209                 tail = var;
210         }
211
212         *fields = head;
213
214         return 0;
215 }
216
217 static int load_endpoint(const char *id, const char *endpoint_name, struct varshead *vars,
218         char *port_string)
219 {
220         struct ast_sip_auth *auth;
221         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
222         RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
223         const char *auth_name;
224
225         *port_string = '\0';
226
227         /* We need to use res_pjsip's sorcery instance instead of our own to
228          * get endpoint and auth.
229          */
230         endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
231                 endpoint_name);
232         if (!endpoint) {
233                 ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id,
234                         endpoint_name);
235                 return -1;
236         }
237
238         assign_and_insert("ENDPOINT_ID", endpoint_name, vars);
239         assign_and_insert("TRANSPORT_ID", endpoint->transport, vars);
240
241         if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) {
242                 assign_and_insert(ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_CALLERID],
243                         endpoint->id.self.number.str, vars);
244         }
245
246         if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) {
247                 assign_and_insert(
248                         ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_DISPLAY_NAME],
249                         endpoint->id.self.name.str, vars);
250         }
251
252         transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
253                 endpoint->transport);
254         if (!transport) {
255                 ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name,
256                         endpoint->transport);
257                 return -1;
258         }
259         snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host));
260
261         if (!endpoint->inbound_auths.num) {
262                 return 0;
263         }
264         auth_name = endpoint->inbound_auths.names[0];
265
266         auth = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "auth", auth_name);
267         if (!auth) {
268                 ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name);
269                 return -1;
270         }
271
272         assign_and_insert("AUTH_ID", auth_name, vars);
273         assign_and_insert(ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_USERNAME],
274                 auth->auth_user, vars);
275         assign_and_insert(ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_SECRET],
276                 auth->auth_pass, vars);
277         ao2_ref(auth, -1);
278
279         return 0;
280 }
281
282 /*! \brief Callback that loads the users from phoneprov sections */
283 static int load_users(void)
284 {
285         struct phoneprov *pp;
286         struct ao2_container *c;
287         struct ao2_iterator i;
288         int user_count = 0;
289         char port_string[6];
290
291         c = ast_sorcery_retrieve_by_regex(sorcery, "phoneprov", "");
292         if (!c) {
293                 ast_log(LOG_ERROR, "Retrieve by regex failed to allocate a container.\n");
294                 return -1;
295         }
296         if (ao2_container_count(c) == 0) {
297                 ast_log(LOG_ERROR, "Unable to find any phoneprov users.\n");
298                 ao2_cleanup(c);
299                 return -1;
300         }
301
302         i = ao2_iterator_init(c, 0);
303         while ((pp = ao2_iterator_next(&i))) {
304                 const char *endpoint_name;
305                 const char *id = ast_sorcery_object_get_id(pp);
306
307                 endpoint_name = ast_var_find(pp->vars, "endpoint");
308                 if (endpoint_name) {
309                         if (load_endpoint(id, endpoint_name, pp->vars, port_string)) {
310                                 goto cleanup;
311                         }
312                 }
313
314                 if (!ast_var_find(pp->vars,
315                         ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_USERNAME])) {
316                         assign_and_insert(
317                                 ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_USERNAME], id,
318                                 pp->vars);
319                 }
320
321                 if (!ast_var_find(pp->vars,
322                         ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_LABEL])) {
323                         assign_and_insert(ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_LABEL],
324                                 id, pp->vars);
325                 }
326
327                 if (!ast_var_find(pp->vars,
328                         ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_SERVER_PORT])) {
329                         assign_and_insert("SERVER_PORT", S_OR(port_string, "5060"), pp->vars);
330                 }
331
332                 if (!ast_var_find(pp->vars,
333                         ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_PROFILE])) {
334                         ast_log(LOG_ERROR, "phoneprov %s didn't contain a PROFILE entry.\n", id);
335                 } else if (!ast_phoneprov_add_extension(AST_MODULE, pp->vars)) {
336                         user_count++;
337                 }
338                 ao2_ref(pp, -1);
339         }
340
341 cleanup:
342         ao2_iterator_destroy(&i);
343         ao2_cleanup(pp);
344         ao2_cleanup(c);
345
346         return user_count > 0 ? 0 : -1;
347 }
348
349 /*! \brief Callback that validates the phoneprov object */
350 static int users_apply_handler(const struct ast_sorcery *sorcery, void *obj)
351 {
352         struct phoneprov *pp = obj;
353         const char *id = ast_sorcery_object_get_id(pp);
354
355         if (!ast_var_find(pp->vars,
356                 ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_MAC])) {
357                 ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id);
358                 return -1;
359         }
360
361         if (!ast_var_find(pp->vars,
362                 ast_phoneprov_std_variable_lookup[AST_PHONEPROV_STD_PROFILE])) {
363                 ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id);
364                 return -1;
365         }
366
367         return 0;
368 }
369
370 static int load_module(void)
371 {
372         if (!(sorcery = ast_sorcery_open())) {
373                 ast_log(LOG_ERROR, "Unable to open a sorcery instance.\n");
374                 return AST_MODULE_LOAD_DECLINE;
375         }
376
377         if (ast_sorcery_apply_default(sorcery, "phoneprov", "config",
378                 "pjsip.conf,criteria=type=phoneprov")) {
379                 ast_log(LOG_ERROR, "Unable to register object phoneprov.\n");
380                 ast_sorcery_unref(sorcery);
381                 return AST_MODULE_LOAD_DECLINE;
382         }
383
384         ast_sorcery_object_register(sorcery, "phoneprov", phoneprov_alloc, NULL,
385                 users_apply_handler);
386         ast_sorcery_object_field_register(sorcery, "phoneprov", "type", "", OPT_NOOP_T, 0, 0);
387         ast_sorcery_object_fields_register(sorcery, "phoneprov", "^", aco_handler,
388                 fields_handler);
389         ast_sorcery_reload_object(sorcery, "phoneprov");
390
391         if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
392                 ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
393                 ast_sorcery_unref(sorcery);
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         ast_sorcery_unref(sorcery);
404
405         return 0;
406 }
407
408 static int reload_module(void)
409 {
410         unload_module();
411         load_module();
412
413         return 0;
414 }
415
416 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Phoneprov Provider",
417                 .load = load_module,
418                 .reload = reload_module,
419                 .unload = unload_module,
420                 .load_pri = AST_MODPRI_APP_DEPEND,
421                 );