Debian Codegraph @

res/res_pjsip_phoneprov_provider.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Fairview 5 Engineering, LLC
5 *
6 * George Joseph <[email protected]>
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 <[email protected]>
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 <since>
106 <version>12.7.0</version>
107 </since>
108 <synopsis>Provides variables for each user.</synopsis>
109 <configOption name="type">
110 <since>
111 <version>13.2.0</version>
112 </since>
113 <synopsis>Must be of type 'phoneprov'.</synopsis>
114 </configOption>
115 <configOption name="endpoint">
116 <synopsis>The endpoint from which variables will be retrieved.</synopsis>
117 </configOption>
118 <configOption name="MAC">
119 <synopsis>The mac address for this user. (required)</synopsis>
120 </configOption>
121 <configOption name="PROFILE">
122 <synopsis>The phoneprov profile to use for this user. (required)</synopsis>
123 </configOption>
124 <configOption name="*">
125 <synopsis>Other name/value pairs to be passed through for use in templates.</synopsis>
126 </configOption>
127 </configObject>
128 </configFile>
129 </configInfo>
130 ***/
131
132 static struct ast_sorcery *sorcery;
133
134 /*! \brief Structure for a phoneprov object */
135 struct phoneprov {
136 SORCERY_OBJECT(details);
137 struct varshead *vars;
138 };
139
140 /*! \brief Destructor function for phoneprov */
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 /*! \brief Allocator for phoneprov */
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 /*! \brief Helper that creates an ast_var_t and inserts it into the list */
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 /* Just ignore if the value is NULL or empty */
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 /*! \brief Adds a config name/value pair to the phoneprov object */
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 /*! \brief Converts the phoneprov varlist to an ast_variable list */
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 /* We need to use res_pjsip's sorcery instance instead of our own to
240 * get endpoint and auth.
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 /*! \brief Callback that validates the phoneprov object */
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 /*! \brief Callback that loads the users from phoneprov sections */
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 );