summaryrefslogtreecommitdiff
path: root/res/res_pjsip_phoneprov_provider.c
blob: 7e082c4b550383abbcbee7f5c8afc4e12f50cfbf (plain)
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
43
44
45
46
47
48
49
50
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2014, Fairview 5 Engineering, LLC
 *
 * George Joseph <george.joseph@fairview5.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief PJSIP Phoneprov Configuration Provider
 *
 * \author George Joseph <george.joseph@fairview5.com>
  */

/*! \li \ref res_pjsip_phoneprov_provider.c uses the configuration file \ref pjsip.conf
 * \addtogroup configuration_file Configuration Files
 */

/*!
 * \page pjsip.conf pjsip.conf
 * \verbinclude pjsip.conf.sample
 */

/*** MODULEINFO
	<depend>pjproject</depend>
	<depend>res_pjsip</depend>
	<depend>res_phoneprov</depend>
	<support_level>extended</support_level>
 ***/

#include "asterisk.h"

#include <pjsip.h>

#include "asterisk/res_pjsip.h"
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
#include "asterisk/phoneprov.h"
#include "res_pjsip/include/res_pjsip_private.h"

/*** DOCUMENTATION
	<configInfo name="res_pjsip_phoneprov_provider" language="en_US">
		<synopsis>Module that integrates res_pjsip with res_phoneprov.</synopsis>
		<description><para>
			<emphasis>PJSIP Phoneprov Provider</emphasis>
			</para>
			<para>This module creates the integration between <literal>res_pjsip</literal> and
			<literal>res_phoneprov</literal>.
			</para>
			<para>Each user to be integrated requires a <literal>phoneprov</literal>
			section defined in <filename>pjsip.conf</filename>.  Each section identifies
			the endpoint associated with the user and any other name/value pairs to be passed
			on to res_phoneprov's template substitution.  Only <literal>MAC</literal> and
			<literal>PROFILE</literal> variables are required.  Any other variables
			supplied will be passed through.</para>
			<para> </para>
			<para>Example:</para>
			<para>[1000]</para>
			<para>type = phoneprovr</para>
			<para>endpoint = ep1000</para>
			<para>MAC = deadbeef4dad</para>
			<para>PROFILE = grandstream2</para>
			<para>LINEKEYS = 2</para>
			<para>LINE = 1</para>
			<para>OTHERVAR = othervalue</para>
			<para> </para>
			<para>The following variables are automatically defined if an endpoint
			is defined for the user:</para>
			<enumlist>
				<enum name="USERNAME"><para>Source: The user_name defined in the first auth reference
				in the endpoint.</para></enum>
				<enum name="SECRET"><para>Source: The user_pass defined in the first auth reference
				in the endpoint.</para></enum>
				<enum name="CALLERID"><para>Source: The number part of the callerid defined in
				the endpoint.</para></enum>
				<enum name="DISPLAY_NAME"><para>Source: The name part of the callerid defined in
				the endpoint.</para></enum>
				<enum name="LABEL"><para>Source: The id of the phoneprov section.</para></enum>
			</enumlist>
			<para> </para>
			<para>In addition to the standard variables, the following are also automatically defined:</para>
			<enumlist>
				<enum name="ENDPOINT_ID"><para>Source: The id of the endpoint.</para></enum>
				<enum name="TRANSPORT_ID"><para>Source: The id of the transport used by the endpoint.</para></enum>
				<enum name="AUTH_ID"><para>Source: The id of the auth used by the endpoint.</para></enum>
			</enumlist>
			<para> </para>
			<para>All other template substitution variables must be explicitly defined in the
			phoneprov_default or phoneprov sections.</para>
		</description>

		<configFile name="pjsip.conf">
			<configObject name="phoneprov">
				<synopsis>Provides variables for each user.</synopsis>
				<configOption name="type">
					<synopsis>Must be of type 'phoneprov'.</synopsis>
				</configOption>
				<configOption name="endpoint">
					<synopsis>The endpoint from which variables will be retrieved.</synopsis>
				</configOption>
				<configOption name="MAC">
					<synopsis>The mac address for this user. (required)</synopsis>
				</configOption>
				<configOption name="PROFILE">
					<synopsis>The phoneprov profile to use for this user. (required)</synopsis>
				</configOption>
				<configOption name="*">
					<synopsis>Other name/value pairs to be passed through for use in templates.</synopsis>
				</configOption>
			</configObject>
		</configFile>
	</configInfo>
 ***/

static struct ast_sorcery *sorcery;

/*! \brief Structure for a phoneprov object */
struct phoneprov {
	SORCERY_OBJECT(details);
	struct varshead *vars;
};

/*! \brief Destructor function for phoneprov */
static void phoneprov_destroy(void *obj)
{
	struct phoneprov *pp = obj;
	char *mac = ast_var_find(pp->vars, "MAC");

	if (mac) {
		ast_phoneprov_delete_extension(AST_MODULE, mac);
	}

	ast_var_list_destroy(pp->vars);
}

/*! \brief Allocator for phoneprov */
static void *phoneprov_alloc(const char *name)
{
	struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy);

	if (!pp || !(pp->vars = ast_var_list_create())) {
		ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n",
			name);
		ao2_cleanup(pp);
		return NULL;
	}

	return pp;
}

/*! \brief Helper that creates an ast_var_t and inserts it into the list */
static int assign_and_insert(const char *name, const char *value, struct varshead *vars)
{
	struct ast_var_t *var;

	if (ast_strlen_zero(name) || !vars) {
		return -1;
	}

	/* Just ignore if the value is NULL or empty */
	if (ast_strlen_zero(value)) {
		return 0;
	}

	var = ast_var_assign(name, value);
	if (!var) {
		ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n");
		return -1;
	}
	AST_VAR_LIST_INSERT_TAIL(vars, var);

	return 0;
}

/*! \brief Adds a config name/value pair to the phoneprov object */
static int aco_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
{
	struct phoneprov *pp = obj;

	return assign_and_insert(var->name, var->value, pp->vars);
}

/*! \brief Converts the phoneprov varlist to an ast_variable list */
static int fields_handler(const void *obj, struct ast_variable **fields)
{
	const struct phoneprov *pp = obj;
	struct ast_var_t *pvar;
	struct ast_variable *head = NULL;
	struct ast_variable *tail = NULL;
	struct ast_variable *var;

	AST_VAR_LIST_TRAVERSE(pp->vars, pvar) {
		var = ast_variable_new(pvar->name, pvar->value, "");
		if (!var) {
			ast_variables_destroy(head);
			return -1;
		}
		if (!head) {
			head = var;
			tail = var;
			continue;
		}
		tail->next = var;
		tail = var;
	}

	*fields = head;

	return 0;
}

static int load_endpoint(const char *id, const char *endpoint_name, struct varshead *vars,
	char *port_string)
{
	struct ast_sip_auth *auth;
	RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
	RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
	const char *auth_name;

	*port_string = '\0';

	/* We need to use res_pjsip's sorcery instance instead of our own to
	 * get endpoint and auth.
	 */
	endpoint = ast_sorcery_retrieve_by_id(sorcery, "endpoint",
		endpoint_name);
	if (!endpoint) {
		ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id,
			endpoint_name);
		return -1;
	}

	assign_and_insert("ENDPOINT_ID", endpoint_name, vars);
	assign_and_insert("TRANSPORT_ID", endpoint->transport, vars);

	if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) {
		assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_CALLERID),
			endpoint->id.self.number.str, vars);
	}

	if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) {
		assign_and_insert(
			ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_DISPLAY_NAME),
			endpoint->id.self.name.str, vars);
	}

	transport = ast_sorcery_retrieve_by_id(sorcery, "transport",
		endpoint->transport);
	if (!transport) {
		ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name,
			endpoint->transport);
		return -1;
	}
	snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host));

	if (!AST_VECTOR_SIZE(&endpoint->inbound_auths)) {
		return 0;
	}
	auth_name = AST_VECTOR_GET(&endpoint->inbound_auths, 0);

	auth = ast_sorcery_retrieve_by_id(sorcery, "auth", auth_name);
	if (!auth) {
		ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name);
		return -1;
	}

	assign_and_insert("AUTH_ID", auth_name, vars);
	assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME),
		auth->auth_user, vars);
	assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SECRET),
		auth->auth_pass, vars);
	ao2_ref(auth, -1);

	return 0;
}

/*! \brief Callback that validates the phoneprov object */
static void users_apply_handler(struct phoneprov *pp)
{
	const char *id = ast_sorcery_object_get_id(pp);
	const char *endpoint_name;
	char port_string[6];

	if (!ast_var_find(pp->vars,
		ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_MAC))) {
		ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id);
		return;
	}

	if (!ast_var_find(pp->vars,
		ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
		ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id);
		return;
	}

	endpoint_name = ast_var_find(pp->vars, "endpoint");
	if (endpoint_name) {
		if (load_endpoint(id, endpoint_name, pp->vars, port_string)) {
			return;
		}
	}

	if (!ast_var_find(pp->vars,
		ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME))) {
		assign_and_insert(
			ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME), id,
			pp->vars);
	}

	if (!ast_var_find(pp->vars,
		ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL))) {
		assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL),
			id, pp->vars);
	}

	if (!ast_var_find(pp->vars,
		ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SERVER_PORT))) {
		assign_and_insert("SERVER_PORT", S_OR(port_string, "5060"), pp->vars);
	}

	if (!ast_var_find(pp->vars,
		ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
		ast_log(LOG_ERROR, "phoneprov %s didn't contain a PROFILE entry.\n", id);
	}

	ast_phoneprov_add_extension(AST_MODULE, pp->vars);

	return;
}

/*! \brief Callback that loads the users from phoneprov sections */
static int load_users(void)
{
	struct ao2_container *users;
	struct ao2_iterator i;
	struct phoneprov *pp;

	ast_sorcery_reload_object(sorcery, "phoneprov");

	users = ast_sorcery_retrieve_by_fields(sorcery, "phoneprov",
		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
	if (!users) {
		return 0;
	}

	i = ao2_iterator_init(users, 0);
	while ((pp = ao2_iterator_next(&i))) {
		users_apply_handler(pp);
		ao2_ref(pp, -1);
	}
	ao2_iterator_destroy(&i);
	ao2_ref(users, -1);

	return 0;
}

static int load_module(void)
{
	CHECK_PJSIP_MODULE_LOADED();

	sorcery = ast_sip_get_sorcery();

	ast_sorcery_apply_config(sorcery, "res_pjsip_phoneprov_provider");
	ast_sorcery_apply_default(sorcery, "phoneprov", "config",
		"pjsip.conf,criteria=type=phoneprov");

	ast_sorcery_object_register(sorcery, "phoneprov", phoneprov_alloc, NULL,
		NULL);

	ast_sorcery_object_field_register(sorcery, "phoneprov", "type", "", OPT_NOOP_T, 0,
		0);
	ast_sorcery_object_fields_register(sorcery, "phoneprov", "^", aco_handler,
		fields_handler);

	ast_sorcery_load_object(sorcery, "phoneprov");

	if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
		ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
		return AST_MODULE_LOAD_DECLINE;
	}

	return AST_MODULE_LOAD_SUCCESS;
}

static int unload_module(void)
{
	ast_phoneprov_provider_unregister(AST_MODULE);

	return 0;
}

static int reload_module(void)
{
	ast_phoneprov_provider_unregister(AST_MODULE);

	if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
		ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
		return AST_MODULE_LOAD_DECLINE;
	}

	return 0;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Phoneprov Provider",
	.support_level = AST_MODULE_SUPPORT_EXTENDED,
	.load = load_module,
	.reload = reload_module,
	.unload = unload_module,
	.load_pri = AST_MODPRI_APP_DEPEND,
);