sorcery: Make is_object_field_registered handle field names that are regexes.
[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>pjproject</depend>
37         <depend>res_pjsip</depend>
38         <depend>res_phoneprov</depend>
39         <support_level>extended</support_level>
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 /*** DOCUMENTATION
53         <configInfo name="res_pjsip_phoneprov_provider" language="en_US">
54                 <synopsis>Module that integrates res_pjsip with res_phoneprov.</synopsis>
55                 <description><para>
56                         <emphasis>PJSIP Phoneprov Provider</emphasis>
57                         </para>
58                         <para>This module creates the integration between <literal>res_pjsip</literal> and
59                         <literal>res_phoneprov</literal>.
60                         </para>
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>
67                         <para> </para>
68                         <para>Example:</para>
69                         <para>[1000]</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>
75                         <para>LINE = 1</para>
76                         <para>OTHERVAR = othervalue</para>
77                         <para> </para>
78                         <para>The following variables are automatically defined if an endpoint
79                         is defined for the user:</para>
80                         <enumlist>
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>
90                         </enumlist>
91                         <para> </para>
92                         <para>In addition to the standard variables, the following are also automatically defined:</para>
93                         <enumlist>
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>
97                         </enumlist>
98                         <para> </para>
99                         <para>All other template substitution variables must be explicitly defined in the
100                         phoneprov_default or phoneprov sections.</para>
101                 </description>
102
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>
108                                 </configOption>
109                                 <configOption name="endpoint">
110                                         <synopsis>The endpoint from which variables will be retrieved.</synopsis>
111                                 </configOption>
112                                 <configOption name="MAC">
113                                         <synopsis>The mac address for this user. (required)</synopsis>
114                                 </configOption>
115                                 <configOption name="PROFILE">
116                                         <synopsis>The phoneprov profile to use for this user. (required)</synopsis>
117                                 </configOption>
118                                 <configOption name="*">
119                                         <synopsis>Other name/value pairs to be passed through for use in templates.</synopsis>
120                                 </configOption>
121                         </configObject>
122                 </configFile>
123         </configInfo>
124  ***/
125
126 static struct ast_sorcery *sorcery;
127
128 /*! \brief Structure for a phoneprov object */
129 struct phoneprov {
130         SORCERY_OBJECT(details);
131         struct varshead *vars;
132 };
133
134 /*! \brief Destructor function for phoneprov */
135 static void phoneprov_destroy(void *obj)
136 {
137         struct phoneprov *pp = obj;
138
139         ast_var_list_destroy(pp->vars);
140 }
141
142 /*! \brief Allocator for phoneprov */
143 static void *phoneprov_alloc(const char *name)
144 {
145         struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy);
146
147         if (!pp || !(pp->vars = ast_var_list_create())) {
148                 ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n",
149                         name);
150                 ao2_cleanup(pp);
151                 return NULL;
152         }
153
154         return pp;
155 }
156
157 /*! \brief Helper that creates an ast_var_t and inserts it into the list */
158 static int assign_and_insert(const char *name, const char *value, struct varshead *vars)
159 {
160         struct ast_var_t *var;
161
162         if (ast_strlen_zero(name) || !vars) {
163                 return -1;
164         }
165
166         /* Just ignore if the value is NULL or empty */
167         if (ast_strlen_zero(value)) {
168                 return 0;
169         }
170
171         var = ast_var_assign(name, value);
172         if (!var) {
173                 ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n");
174                 return -1;
175         }
176         AST_VAR_LIST_INSERT_TAIL(vars, var);
177
178         return 0;
179 }
180
181 /*! \brief Adds a config name/value pair to the phoneprov object */
182 static int aco_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
183 {
184         struct phoneprov *pp = obj;
185
186         return assign_and_insert(var->name, var->value, pp->vars);
187 }
188
189 /*! \brief Converts the phoneprov varlist to an ast_variable list */
190 static int fields_handler(const void *obj, struct ast_variable **fields)
191 {
192         const struct phoneprov *pp = obj;
193         struct ast_var_t *pvar;
194         struct ast_variable *head = NULL;
195         struct ast_variable *tail = NULL;
196         struct ast_variable *var;
197
198         AST_VAR_LIST_TRAVERSE(pp->vars, pvar) {
199                 var = ast_variable_new(pvar->name, pvar->value, NULL);
200                 if (!var) {
201                         ast_variables_destroy(head);
202                         return -1;
203                 }
204                 if (!head) {
205                         head = var;
206                         tail = var;
207                         continue;
208                 }
209                 tail->next = var;
210                 tail = var;
211         }
212
213         *fields = head;
214
215         return 0;
216 }
217
218 static int load_endpoint(const char *id, const char *endpoint_name, struct varshead *vars,
219         char *port_string)
220 {
221         struct ast_sip_auth *auth;
222         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
223         RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
224         const char *auth_name;
225
226         *port_string = '\0';
227
228         /* We need to use res_pjsip's sorcery instance instead of our own to
229          * get endpoint and auth.
230          */
231         endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
232                 endpoint_name);
233         if (!endpoint) {
234                 ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id,
235                         endpoint_name);
236                 return -1;
237         }
238
239         assign_and_insert("ENDPOINT_ID", endpoint_name, vars);
240         assign_and_insert("TRANSPORT_ID", endpoint->transport, vars);
241
242         if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) {
243                 assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_CALLERID),
244                         endpoint->id.self.number.str, vars);
245         }
246
247         if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) {
248                 assign_and_insert(
249                         ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_DISPLAY_NAME),
250                         endpoint->id.self.name.str, vars);
251         }
252
253         transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
254                 endpoint->transport);
255         if (!transport) {
256                 ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name,
257                         endpoint->transport);
258                 return -1;
259         }
260         snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host));
261
262         if (!AST_VECTOR_SIZE(&endpoint->inbound_auths)) {
263                 return 0;
264         }
265         auth_name = AST_VECTOR_GET(&endpoint->inbound_auths, 0);
266
267         auth = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "auth", auth_name);
268         if (!auth) {
269                 ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name);
270                 return -1;
271         }
272
273         assign_and_insert("AUTH_ID", auth_name, vars);
274         assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME),
275                 auth->auth_user, vars);
276         assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SECRET),
277                 auth->auth_pass, vars);
278         ao2_ref(auth, -1);
279
280         return 0;
281 }
282
283 /*! \brief Callback that loads the users from phoneprov sections */
284 static int load_users(void)
285 {
286         struct phoneprov *pp;
287         struct ao2_container *c;
288         struct ao2_iterator i;
289         int user_count = 0;
290         char port_string[6];
291
292         c = ast_sorcery_retrieve_by_fields(sorcery, "phoneprov", AST_RETRIEVE_FLAG_MULTIPLE, NULL);
293         if (!c) {
294                 ast_log(LOG_ERROR, "Retrieve by regex failed to allocate a container.\n");
295                 return -1;
296         }
297         if (ao2_container_count(c) == 0) {
298                 ast_log(LOG_ERROR, "Unable to find any phoneprov users.\n");
299                 ao2_cleanup(c);
300                 return -1;
301         }
302
303         i = ao2_iterator_init(c, 0);
304         while ((pp = ao2_iterator_next(&i))) {
305                 const char *endpoint_name;
306                 const char *id = ast_sorcery_object_get_id(pp);
307
308                 endpoint_name = ast_var_find(pp->vars, "endpoint");
309                 if (endpoint_name) {
310                         if (load_endpoint(id, endpoint_name, pp->vars, port_string)) {
311                                 goto cleanup;
312                         }
313                 }
314
315                 if (!ast_var_find(pp->vars,
316                         ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME))) {
317                         assign_and_insert(
318                                 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME), id,
319                                 pp->vars);
320                 }
321
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),
325                                 id, pp->vars);
326                 }
327
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);
331                 }
332
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);
336                 } else if (!ast_phoneprov_add_extension(AST_MODULE, pp->vars)) {
337                         user_count++;
338                 }
339                 ao2_ref(pp, -1);
340         }
341
342 cleanup:
343         ao2_iterator_destroy(&i);
344         ao2_cleanup(pp);
345         ao2_cleanup(c);
346
347         return user_count > 0 ? 0 : -1;
348 }
349
350 /*! \brief Callback that validates the phoneprov object */
351 static int users_apply_handler(const struct ast_sorcery *sorcery, void *obj)
352 {
353         struct phoneprov *pp = obj;
354         const char *id = ast_sorcery_object_get_id(pp);
355
356         if (!ast_var_find(pp->vars,
357                 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_MAC))) {
358                 ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id);
359                 return -1;
360         }
361
362         if (!ast_var_find(pp->vars,
363                 ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
364                 ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id);
365                 return -1;
366         }
367
368         return 0;
369 }
370
371 static int load_module(void)
372 {
373         CHECK_PJSIP_MODULE_LOADED();
374
375         if (!(sorcery = ast_sorcery_open())) {
376                 ast_log(LOG_ERROR, "Unable to open a sorcery instance.\n");
377                 return AST_MODULE_LOAD_DECLINE;
378         }
379
380         ast_sorcery_apply_default(sorcery, "phoneprov", "config", "pjsip.conf,criteria=type=phoneprov");
381
382         ast_sorcery_object_register(sorcery, "phoneprov", phoneprov_alloc, NULL,
383                 users_apply_handler);
384         ast_sorcery_object_field_register(sorcery, "phoneprov", "type", "", OPT_NOOP_T, 0, 0);
385         ast_sorcery_object_fields_register(sorcery, "phoneprov", "^", aco_handler,
386                 fields_handler);
387         ast_sorcery_reload_object(sorcery, "phoneprov");
388
389         if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
390                 ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
391                 ast_sorcery_unref(sorcery);
392                 return AST_MODULE_LOAD_DECLINE;
393         }
394
395         return AST_MODULE_LOAD_SUCCESS;
396 }
397
398 static int unload_module(void)
399 {
400         ast_phoneprov_provider_unregister(AST_MODULE);
401         ast_sorcery_unref(sorcery);
402
403         return 0;
404 }
405
406 static int reload_module(void)
407 {
408         unload_module();
409         load_module();
410
411         return 0;
412 }
413
414 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Phoneprov Provider",
415                 .load = load_module,
416                 .reload = reload_module,
417                 .unload = unload_module,
418                 .load_pri = AST_MODPRI_APP_DEPEND,
419                 );