2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.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.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <support_level>core</support_level>
29 #include "asterisk/res_pjsip.h"
30 #include "asterisk/res_pjsip_cli.h"
31 #include "asterisk/module.h"
32 #include "asterisk/acl.h"
33 #include "asterisk/manager.h"
34 #include "res_pjsip/include/res_pjsip_private.h"
37 <configInfo name="res_pjsip_endpoint_identifier_ip" language="en_US">
38 <synopsis>Module that identifies endpoints via source IP address</synopsis>
39 <configFile name="pjsip.conf">
40 <configObject name="identify">
41 <synopsis>Identifies endpoints via source IP address</synopsis>
42 <configOption name="endpoint">
43 <synopsis>Name of Endpoint</synopsis>
45 <configOption name="match">
46 <synopsis>IP addresses or networks to match against</synopsis>
48 The value is a comma-delimited list of IP addresses. IP addresses may
49 have a subnet mask appended. The subnet mask may be written in either
50 CIDR or dot-decimal notation. Separate the IP address and subnet
51 mask with a slash ('/')
54 <configOption name="type">
55 <synopsis>Must be of type 'identify'.</synopsis>
62 /*! \brief Structure for an IP identification matching object */
63 struct ip_identify_match {
64 /*! \brief Sorcery object details */
65 SORCERY_OBJECT(details);
66 /*! \brief Stringfields */
67 AST_DECLARE_STRING_FIELDS(
68 /*! The name of the endpoint */
69 AST_STRING_FIELD(endpoint_name);
71 /*! \brief Networks or addresses that should match this */
72 struct ast_ha *matches;
75 /*! \brief Destructor function for a matching object */
76 static void ip_identify_destroy(void *obj)
78 struct ip_identify_match *identify = obj;
80 ast_string_field_free_memory(identify);
81 ast_free_ha(identify->matches);
84 /*! \brief Allocator function for a matching object */
85 static void *ip_identify_alloc(const char *name)
87 struct ip_identify_match *identify = ast_sorcery_generic_alloc(sizeof(*identify), ip_identify_destroy);
89 if (!identify || ast_string_field_init(identify, 256)) {
90 ao2_cleanup(identify);
97 /*! \brief Comparator function for a matching object */
98 static int ip_identify_match_check(void *obj, void *arg, int flags)
100 struct ip_identify_match *identify = obj;
101 struct ast_sockaddr *addr = arg;
104 sense = ast_apply_ha(identify->matches, addr);
105 if (sense != AST_SENSE_ALLOW) {
106 ast_debug(3, "Source address %s matches identify '%s'\n",
107 ast_sockaddr_stringify(addr),
108 ast_sorcery_object_get_id(identify));
109 return CMP_MATCH | CMP_STOP;
111 ast_debug(3, "Source address %s does not match identify '%s'\n",
112 ast_sockaddr_stringify(addr),
113 ast_sorcery_object_get_id(identify));
118 static struct ast_sip_endpoint *ip_identify(pjsip_rx_data *rdata)
120 struct ast_sockaddr addr = { { 0, } };
121 RAII_VAR(struct ao2_container *, candidates, NULL, ao2_cleanup);
122 RAII_VAR(struct ip_identify_match *, match, NULL, ao2_cleanup);
123 struct ast_sip_endpoint *endpoint;
125 /* If no possibilities exist return early to save some time */
126 if (!(candidates = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL)) ||
127 !ao2_container_count(candidates)) {
128 ast_debug(3, "No identify sections to match against\n");
132 ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID);
133 ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port);
135 if (!(match = ao2_callback(candidates, 0, ip_identify_match_check, &addr))) {
136 ast_debug(3, "'%s' did not match any identify section rules\n",
137 ast_sockaddr_stringify(&addr));
141 endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", match->endpoint_name);
143 ast_debug(3, "Retrieved endpoint %s\n", ast_sorcery_object_get_id(endpoint));
145 ast_log(LOG_WARNING, "Identify section '%s' points to endpoint '%s' but endpoint could not be looked up\n",
146 ast_sorcery_object_get_id(match), match->endpoint_name);
152 static struct ast_sip_endpoint_identifier ip_identifier = {
153 .identify_endpoint = ip_identify,
156 /*! \brief Custom handler for match field */
157 static int ip_identify_match_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
159 struct ip_identify_match *identify = obj;
160 int num_addrs = 0, error = 0, i;
161 struct ast_sockaddr *addrs;
163 num_addrs = ast_sockaddr_resolve(&addrs, var->value, PARSE_PORT_FORBID, AST_AF_UNSPEC);
165 ast_log(LOG_ERROR, "Address '%s' provided on ip endpoint identifier '%s' did not resolve to any address\n",
166 var->value, ast_sorcery_object_get_id(obj));
170 for (i = 0; i < num_addrs; ++i) {
171 /* We deny what we actually want to match because there is an implicit permit all rule for ACLs */
172 identify->matches = ast_append_ha("d", ast_sockaddr_stringify_addr(&addrs[i]), identify->matches, &error);
174 if (!identify->matches || error) {
175 ast_log(LOG_ERROR, "Failed to add address '%s' to ip endpoint identifier '%s'\n",
176 ast_sockaddr_stringify_addr(&addrs[i]), ast_sorcery_object_get_id(obj));
188 static int match_to_str(const void *obj, const intptr_t *args, char **buf)
190 RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
191 const struct ip_identify_match *identify = obj;
193 ast_ha_join(identify->matches, &str);
194 *buf = ast_strdup(ast_str_buffer(str));
198 static int match_to_var_list(const void *obj, struct ast_variable **fields)
200 char str[MAX_OBJECT_FIELD];
201 const struct ip_identify_match *identify = obj;
202 struct ast_variable *head = NULL;
203 struct ast_ha *ha = identify->matches;
205 for (; ha; ha = ha->next) {
206 const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
207 snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "",
208 addr, ast_sockaddr_stringify_addr(&ha->netmask));
210 ast_variable_list_append(&head, ast_variable_new("match", str, ""));
221 static int sip_identify_to_ami(const struct ip_identify_match *identify,
222 struct ast_str **buf)
224 return ast_sip_sorcery_object_to_ami(identify, buf);
227 static int find_identify_by_endpoint(void *obj, void *arg, int flags)
229 struct ip_identify_match *identify = obj;
230 const char *endpoint_name = arg;
232 return strcmp(identify->endpoint_name, endpoint_name) ? 0 : CMP_MATCH;
235 static int format_ami_endpoint_identify(const struct ast_sip_endpoint *endpoint,
236 struct ast_sip_ami *ami)
238 RAII_VAR(struct ao2_container *, identifies, NULL, ao2_cleanup);
239 RAII_VAR(struct ip_identify_match *, identify, NULL, ao2_cleanup);
240 RAII_VAR(struct ast_str *, buf, NULL, ast_free);
242 identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify",
243 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
248 identify = ao2_callback(identifies, 0, find_identify_by_endpoint,
249 (void *) ast_sorcery_object_get_id(endpoint));
254 if (!(buf = ast_sip_create_ami_event("IdentifyDetail", ami))) {
258 if (sip_identify_to_ami(identify, &buf)) {
262 ast_str_append(&buf, 0, "EndpointName: %s\r\n",
263 ast_sorcery_object_get_id(endpoint));
265 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
271 struct ast_sip_endpoint_formatter endpoint_identify_formatter = {
272 .format_ami = format_ami_endpoint_identify
275 static int cli_populate_container(void *obj, void *arg, int flags)
282 static int cli_iterator(void *container, ao2_callback_fn callback, void *args)
284 const struct ast_sip_endpoint *endpoint = container;
285 struct ao2_container *identifies;
287 struct ast_variable fields = {
289 .value = ast_sorcery_object_get_id(endpoint),
293 identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify",
294 AST_RETRIEVE_FLAG_MULTIPLE, &fields);
299 ao2_callback(identifies, OBJ_NODATA, callback, args);
304 static int cli_endpoint_gather_identifies(void *obj, void *arg, int flags)
306 struct ast_sip_endpoint *endpoint = obj;
307 struct ao2_container *container = arg;
309 cli_iterator(endpoint, cli_populate_container, container);
314 static struct ao2_container *cli_get_container(void)
316 RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup);
317 RAII_VAR(struct ao2_container *, s_parent_container, NULL, ao2_cleanup);
318 struct ao2_container *child_container;
320 parent_container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "endpoint",
321 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
322 if (!parent_container) {
326 s_parent_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
327 ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
328 if (!s_parent_container) {
332 if (ao2_container_dup(s_parent_container, parent_container, 0)) {
336 child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
337 ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
338 if (!child_container) {
342 ao2_callback(s_parent_container, OBJ_NODATA, cli_endpoint_gather_identifies, child_container);
344 return child_container;
347 static void *cli_retrieve_by_id(const char *id)
349 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "identify", id);
352 static int cli_print_header(void *obj, void *arg, int flags)
354 struct ast_sip_cli_context *context = arg;
355 int indent = CLI_INDENT_TO_SPACES(context->indent_level);
356 int filler = CLI_MAX_WIDTH - indent - 14;
358 ast_assert(context->output_buffer != NULL);
360 ast_str_append(&context->output_buffer, 0,
361 "%*s: <MatchList%*.*s>\n",
362 indent, "Identify", filler, filler, CLI_HEADER_FILLER);
367 static int cli_print_body(void *obj, void *arg, int flags)
369 RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
370 struct ip_identify_match *ident = obj;
371 struct ast_sip_cli_context *context = arg;
373 ast_assert(context->output_buffer != NULL);
375 ast_str_append(&context->output_buffer, 0, "%*s: ",
376 CLI_INDENT_TO_SPACES(context->indent_level), "Identify");
377 ast_ha_join_cidr(ident->matches, &str);
378 ast_str_append(&context->output_buffer, 0, "%s\n", ast_str_buffer(str));
383 static struct ast_sip_cli_formatter_entry *cli_formatter;
385 static int load_module(void)
387 ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_endpoint_identifier_ip");
388 ast_sorcery_apply_default(ast_sip_get_sorcery(), "identify", "config", "pjsip.conf,criteria=type=identify");
390 if (ast_sorcery_object_register(ast_sip_get_sorcery(), "identify", ip_identify_alloc, NULL, NULL)) {
391 return AST_MODULE_LOAD_DECLINE;
394 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "type", "", OPT_NOOP_T, 0, 0);
395 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "endpoint", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ip_identify_match, endpoint_name));
396 ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "identify", "match", "", ip_identify_match_handler, match_to_str, match_to_var_list, 0, 0);
397 ast_sorcery_reload_object(ast_sip_get_sorcery(), "identify");
399 ast_sip_register_endpoint_identifier(&ip_identifier);
400 ast_sip_register_endpoint_formatter(&endpoint_identify_formatter);
402 cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
403 if (!cli_formatter) {
404 ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
407 cli_formatter->name = "identify";
408 cli_formatter->print_header = cli_print_header;
409 cli_formatter->print_body = cli_print_body;
410 cli_formatter->get_container = cli_get_container;
411 cli_formatter->iterate = cli_iterator;
412 cli_formatter->get_id = ast_sorcery_object_get_id;
413 cli_formatter->retrieve_by_id = cli_retrieve_by_id;
415 ast_sip_register_cli_formatter(cli_formatter);
417 return AST_MODULE_LOAD_SUCCESS;
420 static int reload_module(void)
422 ast_sorcery_reload_object(ast_sip_get_sorcery(), "identify");
427 static int unload_module(void)
429 ast_sip_unregister_cli_formatter(cli_formatter);
430 ast_sip_unregister_endpoint_formatter(&endpoint_identify_formatter);
431 ast_sip_unregister_endpoint_identifier(&ip_identifier);
436 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP IP endpoint identifier",
437 .support_level = AST_MODULE_SUPPORT_CORE,
439 .reload = reload_module,
440 .unload = unload_module,
441 .load_pri = AST_MODPRI_APP_DEPEND,