| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2013, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Mark Michelson <mmichelson@digium.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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<depend>pjproject</depend> | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 	<depend>res_pjsip</depend> | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <pjsip.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | #include "asterisk/res_pjsip.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | #include "asterisk/res_pjsip_cli.h"
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/acl.h"
 | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | #include "asterisk/manager.h"
 | 
					
						
							|  |  |  | #include "res_pjsip/include/res_pjsip_private.h"
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | /*** DOCUMENTATION
 | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 	<configInfo name="res_pjsip_endpoint_identifier_ip" language="en_US"> | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 		<synopsis>Module that identifies endpoints</synopsis> | 
					
						
							| 
									
										
										
										
											2013-07-30 22:41:05 +00:00
										 |  |  | 		<configFile name="pjsip.conf"> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 			<configObject name="identify"> | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 				<synopsis>Identifies endpoints via some criteria.</synopsis> | 
					
						
							|  |  |  | 				<description> | 
					
						
							|  |  |  | 					<para>This module provides alternatives to matching inbound requests to | 
					
						
							|  |  |  | 					a configured endpoint. At least one of the matching mechanisms | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 					must be provided, or the object configuration is invalid.</para> | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 					<para>The matching mechanisms are provided by the following | 
					
						
							|  |  |  | 					configuration options:</para> | 
					
						
							|  |  |  | 					<enumlist> | 
					
						
							|  |  |  | 						<enum name="match"><para>Match by source IP address.</para></enum> | 
					
						
							|  |  |  | 						<enum name="match_header"><para>Match by SIP header.</para></enum> | 
					
						
							|  |  |  | 					</enumlist> | 
					
						
							| 
									
										
										
										
											2018-01-03 17:26:42 -06:00
										 |  |  | 					<note><para>If multiple matching criteria are provided then an inbound | 
					
						
							|  |  |  | 					request will be matched to the endpoint if it matches | 
					
						
							|  |  |  | 					<emphasis>any</emphasis> of the criteria.</para></note> | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 				</description> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 				<configOption name="endpoint"> | 
					
						
							| 
									
										
										
										
											2018-01-03 17:26:42 -06:00
										 |  |  | 					<synopsis>Name of endpoint identified</synopsis> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="match"> | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 					<synopsis>IP addresses or networks to match against.</synopsis> | 
					
						
							| 
									
										
										
										
											2018-01-03 17:26:42 -06:00
										 |  |  | 					<description> | 
					
						
							|  |  |  | 						<para>The value is a comma-delimited list of IP addresses or | 
					
						
							| 
									
										
										
										
											2020-01-10 16:13:49 -05:00
										 |  |  | 						hostnames.</para> | 
					
						
							|  |  |  | 						<para>IP addresses may have a subnet mask appended. The subnet | 
					
						
							|  |  |  | 						mask may be written in either CIDR or dotted-decimal | 
					
						
							|  |  |  | 						notation. Separate the IP address and subnet mask with a slash | 
					
						
							| 
									
										
										
										
											2019-12-24 10:16:23 -05:00
										 |  |  | 						('/'). A source port can also be specified by adding a colon (':') | 
					
						
							|  |  |  | 						after the address but before the subnet mask, e.g. | 
					
						
							| 
									
										
										
										
											2020-01-10 16:13:49 -05:00
										 |  |  | 						3.2.1.0:5061/24. To specify a source port for an IPv6 address, the | 
					
						
							|  |  |  | 						address itself must be enclosed in square brackets | 
					
						
							|  |  |  | 						('[2001:db8:0::1]:5060')</para> | 
					
						
							|  |  |  | 						<para>When a hostname is used, the behavior depends on whether | 
					
						
							|  |  |  | 						<replaceable>srv_lookups</replaceable> is enabled and/or a source | 
					
						
							|  |  |  | 						port is provided. If <replaceable>srv_lookups</replaceable> is | 
					
						
							|  |  |  | 						enabled and a source port is not provided, Asterisk will perform | 
					
						
							|  |  |  | 						an SRV lookup on the provided hostname, adding all of the A and | 
					
						
							|  |  |  | 						AAAA records that are resolved.</para> | 
					
						
							|  |  |  | 						<para>If the SRV lookup fails, | 
					
						
							|  |  |  | 						<replaceable>srv_lookups</replaceable> is disabled, or a source | 
					
						
							|  |  |  | 						port is specified when the hostname is configured, Asterisk will | 
					
						
							|  |  |  | 						resolve the hostname and add all A and AAAA records that are | 
					
						
							|  |  |  | 						resolved.</para> | 
					
						
							| 
									
										
										
										
											2018-01-03 17:26:42 -06:00
										 |  |  | 					</description> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 				</configOption> | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | 				<configOption name="srv_lookups" default="yes"> | 
					
						
							|  |  |  | 					<synopsis>Perform SRV lookups for provided hostnames.</synopsis> | 
					
						
							| 
									
										
										
										
											2018-01-03 17:26:42 -06:00
										 |  |  | 					<description> | 
					
						
							|  |  |  | 						<para>When enabled, <replaceable>srv_lookups</replaceable> will | 
					
						
							|  |  |  | 						perform SRV lookups for _sip._udp, _sip._tcp, and _sips._tcp of | 
					
						
							|  |  |  | 						the given hostnames to determine additional addresses that traffic | 
					
						
							|  |  |  | 						may originate from. | 
					
						
							|  |  |  | 						</para> | 
					
						
							|  |  |  | 					</description> | 
					
						
							| 
									
										
										
										
											2017-03-08 12:39:20 -06:00
										 |  |  | 				</configOption> | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 				<configOption name="match_header"> | 
					
						
							|  |  |  | 					<synopsis>Header/value pair to match against.</synopsis> | 
					
						
							| 
									
										
										
										
											2018-01-03 17:26:42 -06:00
										 |  |  | 					<description> | 
					
						
							|  |  |  | 						<para>A SIP header whose value is used to match against.  SIP | 
					
						
							|  |  |  | 						requests containing the header, along with the specified value, | 
					
						
							|  |  |  | 						will be mapped to the specified endpoint.  The header must be | 
					
						
							|  |  |  | 						specified with a <literal>:</literal>, as in | 
					
						
							|  |  |  | 						<literal>match_header = SIPHeader: value</literal>. | 
					
						
							|  |  |  | 						</para> | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 						<para>The specified SIP header value can be a regular | 
					
						
							|  |  |  | 						expression if the value is of the form | 
					
						
							|  |  |  | 						/<replaceable>regex</replaceable>/. | 
					
						
							|  |  |  | 						</para> | 
					
						
							|  |  |  | 						<note><para>Use of a regex is expensive so be sure you need | 
					
						
							|  |  |  | 						to use a regex to match your endpoint. | 
					
						
							|  |  |  | 						</para></note> | 
					
						
							| 
									
										
										
										
											2018-01-03 17:26:42 -06:00
										 |  |  | 					</description> | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 				</configOption> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 				<configOption name="type"> | 
					
						
							|  |  |  | 					<synopsis>Must be of type 'identify'.</synopsis> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							|  |  |  | 			</configObject> | 
					
						
							|  |  |  | 		</configFile> | 
					
						
							|  |  |  | 	</configInfo> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | /*! \brief The number of buckets for storing hosts for resolution */ | 
					
						
							|  |  |  | #define HOSTS_BUCKETS 53
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*! \brief Structure for an IP identification matching object */ | 
					
						
							|  |  |  | struct ip_identify_match { | 
					
						
							|  |  |  | 	/*! \brief Sorcery object details */ | 
					
						
							|  |  |  | 	SORCERY_OBJECT(details); | 
					
						
							|  |  |  | 	/*! \brief Stringfields */ | 
					
						
							|  |  |  | 	AST_DECLARE_STRING_FIELDS( | 
					
						
							|  |  |  | 		/*! The name of the endpoint */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(endpoint_name); | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 		/*! If matching by header, the header/value to match against */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(match_header); | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 		/*! SIP header name of the match_header string */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(match_header_name); | 
					
						
							|  |  |  | 		/*! SIP header value of the match_header string */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(match_header_value); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 	/*! Compiled match_header regular expression when is_regex is non-zero */ | 
					
						
							|  |  |  | 	regex_t regex_buf; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	/*! \brief Networks or addresses that should match this */ | 
					
						
							|  |  |  | 	struct ast_ha *matches; | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 	/*! \brief Hosts to be resolved when applying configuration */ | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | 	struct ao2_container *hosts; | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 	/*! \brief Perform SRV resolution of hostnames */ | 
					
						
							|  |  |  | 	unsigned int srv_lookups; | 
					
						
							|  |  |  | 	/*! Non-zero if match_header has a regular expression (i.e., regex_buf is valid) */ | 
					
						
							|  |  |  | 	unsigned int is_regex:1; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Destructor function for a matching object */ | 
					
						
							|  |  |  | static void ip_identify_destroy(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ip_identify_match *identify = obj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_string_field_free_memory(identify); | 
					
						
							|  |  |  | 	ast_free_ha(identify->matches); | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | 	ao2_cleanup(identify->hosts); | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 	if (identify->is_regex) { | 
					
						
							|  |  |  | 		regfree(&identify->regex_buf); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Allocator function for a matching object */ | 
					
						
							|  |  |  | static void *ip_identify_alloc(const char *name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-23 18:59:36 +00:00
										 |  |  | 	struct ip_identify_match *identify = ast_sorcery_generic_alloc(sizeof(*identify), ip_identify_destroy); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!identify || ast_string_field_init(identify, 256)) { | 
					
						
							|  |  |  | 		ao2_cleanup(identify); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return identify; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | /*! \brief Comparator function for matching an object by header */ | 
					
						
							|  |  |  | static int header_identify_match_check(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ip_identify_match *identify = obj; | 
					
						
							|  |  |  | 	struct pjsip_rx_data *rdata = arg; | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 	pjsip_hdr *header; | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 	pj_str_t pj_header_name; | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 	int header_present; | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-15 13:44:43 +00:00
										 |  |  | 	if (ast_strlen_zero(identify->match_header)) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 	pj_header_name = pj_str((void *) identify->match_header_name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Check all headers of the given name for a match. */ | 
					
						
							|  |  |  | 	header_present = 0; | 
					
						
							|  |  |  | 	for (header = NULL; | 
					
						
							|  |  |  | 		(header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &pj_header_name, header)); | 
					
						
							|  |  |  | 		header = header->next) { | 
					
						
							|  |  |  | 		char *pos; | 
					
						
							|  |  |  | 		int len; | 
					
						
							|  |  |  | 		char buf[PATH_MAX]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		header_present = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Print header line to buf */ | 
					
						
							|  |  |  | 		len = pjsip_hdr_print_on(header, buf, sizeof(buf) - 1); | 
					
						
							|  |  |  | 		if (len < 0) { | 
					
						
							|  |  |  | 			/* Buffer not large enough or no header vptr! */ | 
					
						
							|  |  |  | 			ast_assert(0); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		buf[len] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Remove header name from pj_buf and trim blanks. */ | 
					
						
							|  |  |  | 		pos = strchr(buf, ':'); | 
					
						
							|  |  |  | 		if (!pos) { | 
					
						
							|  |  |  | 			/* No header name?  Bug in PJPROJECT if so. */ | 
					
						
							|  |  |  | 			ast_assert(0); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		pos = ast_strip(pos + 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Does header value match what we are looking for? */ | 
					
						
							|  |  |  | 		if (identify->is_regex) { | 
					
						
							|  |  |  | 			if (!regexec(&identify->regex_buf, pos, 0, NULL, 0)) { | 
					
						
							|  |  |  | 				return CMP_MATCH; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else if (!strcmp(identify->match_header_value, pos)) { | 
					
						
							|  |  |  | 			return CMP_MATCH; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ast_debug(3, "Identify '%s': SIP message has '%s' header but value '%s' does not match '%s'.\n", | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 			ast_sorcery_object_get_id(identify), | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 			identify->match_header_name, | 
					
						
							|  |  |  | 			pos, | 
					
						
							|  |  |  | 			identify->match_header_value); | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 	if (!header_present) { | 
					
						
							|  |  |  | 		ast_debug(3, "Identify '%s': SIP message does not have '%s' header.\n", | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 			ast_sorcery_object_get_id(identify), | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 			identify->match_header_name); | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Comparator function for matching an object by IP address */ | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | static int ip_identify_match_check(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ip_identify_match *identify = obj; | 
					
						
							|  |  |  | 	struct ast_sockaddr *addr = arg; | 
					
						
							| 
									
										
										
										
											2013-09-09 22:00:44 +00:00
										 |  |  | 	int sense; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sense = ast_apply_ha(identify->matches, addr); | 
					
						
							|  |  |  | 	if (sense != AST_SENSE_ALLOW) { | 
					
						
							|  |  |  | 		ast_debug(3, "Source address %s matches identify '%s'\n", | 
					
						
							|  |  |  | 				ast_sockaddr_stringify(addr), | 
					
						
							|  |  |  | 				ast_sorcery_object_get_id(identify)); | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 		return CMP_MATCH; | 
					
						
							| 
									
										
										
										
											2013-09-09 22:00:44 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_debug(3, "Source address %s does not match identify '%s'\n", | 
					
						
							|  |  |  | 				ast_sockaddr_stringify(addr), | 
					
						
							|  |  |  | 				ast_sorcery_object_get_id(identify)); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | static struct ast_sip_endpoint *common_identify(ao2_callback_fn *identify_match_cb, void *arg) | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct ao2_container *, candidates, NULL, ao2_cleanup); | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 	struct ip_identify_match *match; | 
					
						
							| 
									
										
										
										
											2013-08-20 15:32:20 +00:00
										 |  |  | 	struct ast_sip_endpoint *endpoint; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* If no possibilities exist return early to save some time */ | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 	candidates = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", | 
					
						
							|  |  |  | 		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); | 
					
						
							|  |  |  | 	if (!candidates || !ao2_container_count(candidates)) { | 
					
						
							| 
									
										
										
										
											2013-09-09 22:00:44 +00:00
										 |  |  | 		ast_debug(3, "No identify sections to match against\n"); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 	match = ao2_callback(candidates, 0, identify_match_cb, arg); | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 	if (!match) { | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 	endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", | 
					
						
							|  |  |  | 		match->endpoint_name); | 
					
						
							| 
									
										
										
										
											2013-08-20 15:32:20 +00:00
										 |  |  | 	if (endpoint) { | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 		ast_debug(3, "Identify '%s' SIP message matched to endpoint %s\n", | 
					
						
							|  |  |  | 			ast_sorcery_object_get_id(match), match->endpoint_name); | 
					
						
							| 
									
										
										
										
											2013-09-09 22:00:44 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 		ast_log(LOG_WARNING, "Identify '%s' points to endpoint '%s' but endpoint could not be found\n", | 
					
						
							|  |  |  | 			ast_sorcery_object_get_id(match), match->endpoint_name); | 
					
						
							| 
									
										
										
										
											2013-08-20 15:32:20 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 	ao2_ref(match, -1); | 
					
						
							| 
									
										
										
										
											2013-08-20 15:32:20 +00:00
										 |  |  | 	return endpoint; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | static struct ast_sip_endpoint *ip_identify(pjsip_rx_data *rdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_sockaddr addr = { { 0, } }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID); | 
					
						
							|  |  |  | 	ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return common_identify(ip_identify_match_check, &addr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | static struct ast_sip_endpoint_identifier ip_identifier = { | 
					
						
							|  |  |  | 	.identify_endpoint = ip_identify, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | static struct ast_sip_endpoint *header_identify(pjsip_rx_data *rdata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return common_identify(header_identify_match_check, rdata); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_sip_endpoint_identifier header_identifier = { | 
					
						
							|  |  |  | 	.identify_endpoint = header_identify, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | /*! \brief Helper function which performs a host lookup and adds result to identify match */ | 
					
						
							|  |  |  | static int ip_identify_match_host_lookup(struct ip_identify_match *identify, const char *host) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_sockaddr *addrs; | 
					
						
							|  |  |  | 	int num_addrs = 0, error = 0, i; | 
					
						
							|  |  |  | 	int results = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 10:16:23 -05:00
										 |  |  | 	num_addrs = ast_sockaddr_resolve(&addrs, host, 0, AST_AF_UNSPEC); | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | 	if (!num_addrs) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < num_addrs; ++i) { | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 		/* Check if the address is already in the list, if so don't add it again */ | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | 		if (identify->matches && (ast_apply_ha(identify->matches, &addrs[i]) != AST_SENSE_ALLOW)) { | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* We deny what we actually want to match because there is an implicit permit all rule for ACLs */ | 
					
						
							| 
									
										
										
										
											2019-12-24 10:16:23 -05:00
										 |  |  | 		identify->matches = ast_append_ha_with_port("d", ast_sockaddr_stringify(&addrs[i]), identify->matches, &error); | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!identify->matches || error) { | 
					
						
							|  |  |  | 			results = -1; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		results += 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_free(addrs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return results; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Helper function which performs an SRV lookup and then resolves the hostname */ | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | static int ip_identify_match_srv_lookup(struct ip_identify_match *identify, const char *prefix, const char *host, int results) | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char service[NI_MAXHOST]; | 
					
						
							|  |  |  | 	struct srv_context *context = NULL; | 
					
						
							|  |  |  | 	int srv_ret; | 
					
						
							|  |  |  | 	const char *srvhost; | 
					
						
							|  |  |  | 	unsigned short srvport; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	snprintf(service, sizeof(service), "%s.%s", prefix, host); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (!(srv_ret = ast_srv_lookup(&context, service, &srvhost, &srvport))) { | 
					
						
							|  |  |  | 		int hosts; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* In the case of the SRV lookup we don't care if it fails, we will output a log message
 | 
					
						
							|  |  |  | 		 * when we fallback to a normal lookup. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		hosts = ip_identify_match_host_lookup(identify, srvhost); | 
					
						
							|  |  |  | 		if (hosts == -1) { | 
					
						
							|  |  |  | 			results = -1; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			results += hosts; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_srv_cleanup(&context); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return results; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*! \brief Custom handler for match field */ | 
					
						
							|  |  |  | static int ip_identify_match_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ip_identify_match *identify = obj; | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:03 +00:00
										 |  |  | 	char *input_string = ast_strdupa(var->value); | 
					
						
							|  |  |  | 	char *current_string; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-01 12:28:05 +00:00
										 |  |  | 	if (ast_strlen_zero(var->value)) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												res_pjsip:  Strip spaces from items parsed from comma-separated lists
Configurations like "aors = a, b, c" were either ignoring everything after "a"
or trying to look up " b".  Same for mailboxes,  ciphers, contacts and a few
others.
To fix, all the strsep(©, ",") calls have been wrapped in ast_strip.  To
facilitate this, ast_strip, ast_skip_blanks and ast_skip_nonblanks were
updated to handle null pointers.
In some cases, an ast_strlen_zero() test was added to skip consecutive commas.
There was also an attempt to ast_free an ast_strdupa'd string in
ast_sip_for_each_aor which was causing a SEGV.  I removed it.
Although this issue was reported for realtime, the issue was in the res_pjsip
modules so all config mechanisms were affected.
ASTERISK-25829 #close
Reported-by: Mateusz Kowalski
Change-Id: I0b22a2cf22a7c1c50d4ecacbfa540155bec0e7a2
											
										 
											2016-03-06 13:38:41 -07:00
										 |  |  | 	while ((current_string = ast_strip(strsep(&input_string, ",")))) { | 
					
						
							| 
									
										
										
										
											2019-12-24 10:16:23 -05:00
										 |  |  | 		char *mask; | 
					
						
							|  |  |  | 		struct ast_sockaddr address; | 
					
						
							| 
									
										
										
										
											2017-01-24 21:39:39 +00:00
										 |  |  | 		int error = 0; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												res_pjsip:  Strip spaces from items parsed from comma-separated lists
Configurations like "aors = a, b, c" were either ignoring everything after "a"
or trying to look up " b".  Same for mailboxes,  ciphers, contacts and a few
others.
To fix, all the strsep(©, ",") calls have been wrapped in ast_strip.  To
facilitate this, ast_strip, ast_skip_blanks and ast_skip_nonblanks were
updated to handle null pointers.
In some cases, an ast_strlen_zero() test was added to skip consecutive commas.
There was also an attempt to ast_free an ast_strdupa'd string in
ast_sip_for_each_aor which was causing a SEGV.  I removed it.
Although this issue was reported for realtime, the issue was in the res_pjsip
modules so all config mechanisms were affected.
ASTERISK-25829 #close
Reported-by: Mateusz Kowalski
Change-Id: I0b22a2cf22a7c1c50d4ecacbfa540155bec0e7a2
											
										 
											2016-03-06 13:38:41 -07:00
										 |  |  | 		if (ast_strlen_zero(current_string)) { | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 10:16:23 -05:00
										 |  |  | 		mask = strrchr(current_string, '/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* If it looks like a netmask is present, or we can immediately parse as an IP,
 | 
					
						
							|  |  |  | 		 * hand things off to the ACL */ | 
					
						
							|  |  |  | 		if (mask || ast_sockaddr_parse(&address, current_string, 0)) { | 
					
						
							|  |  |  | 			identify->matches = ast_append_ha_with_port("d", current_string, identify->matches, &error); | 
					
						
							| 
									
										
										
										
											2013-12-31 22:21:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:03 +00:00
										 |  |  | 			if (!identify->matches || error) { | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "Failed to add address '%s' to ip endpoint identifier '%s'\n", | 
					
						
							|  |  |  | 					current_string, ast_sorcery_object_get_id(obj)); | 
					
						
							|  |  |  | 				return -1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2013-12-31 22:21:07 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | 		if (!identify->hosts) { | 
					
						
							|  |  |  | 			identify->hosts = ast_str_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, HOSTS_BUCKETS); | 
					
						
							|  |  |  | 			if (!identify->hosts) { | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "Failed to create container to store hosts on ip endpoint identifier '%s'\n", | 
					
						
							|  |  |  | 					ast_sorcery_object_get_id(obj)); | 
					
						
							|  |  |  | 				return -1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		error = ast_str_container_add(identify->hosts, current_string); | 
					
						
							|  |  |  | 		if (error) { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "Failed to store host '%s' for resolution on ip endpoint identifier '%s'\n", | 
					
						
							|  |  |  | 				current_string, ast_sorcery_object_get_id(obj)); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Apply handler for identify type */ | 
					
						
							|  |  |  | static int ip_identify_apply(const struct ast_sorcery *sorcery, void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ip_identify_match *identify = obj; | 
					
						
							|  |  |  | 	char *current_string; | 
					
						
							|  |  |  | 	struct ao2_iterator i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 	/* Validate the identify object configuration */ | 
					
						
							|  |  |  | 	if (ast_strlen_zero(identify->endpoint_name)) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Identify '%s' missing required endpoint name.\n", | 
					
						
							|  |  |  | 			ast_sorcery_object_get_id(identify)); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (ast_strlen_zero(identify->match_header) /* No header to match */ | 
					
						
							|  |  |  | 		/* and no static IP addresses with a mask */ | 
					
						
							|  |  |  | 		&& !identify->matches | 
					
						
							|  |  |  | 		/* and no addresses to resolve */ | 
					
						
							|  |  |  | 		&& (!identify->hosts || !ao2_container_count(identify->hosts))) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Identify '%s' is not configured to match anything.\n", | 
					
						
							|  |  |  | 			ast_sorcery_object_get_id(identify)); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!ast_strlen_zero(identify->match_header)) { | 
					
						
							|  |  |  | 		char *c_header; | 
					
						
							|  |  |  | 		char *c_value; | 
					
						
							|  |  |  | 		int len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Split the header name and value */ | 
					
						
							|  |  |  | 		c_header = ast_strdupa(identify->match_header); | 
					
						
							|  |  |  | 		c_value = strchr(c_header, ':'); | 
					
						
							|  |  |  | 		if (!c_value) { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "Identify '%s' missing ':' separator in match_header '%s'.\n", | 
					
						
							|  |  |  | 				ast_sorcery_object_get_id(identify), identify->match_header); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		*c_value = '\0'; | 
					
						
							|  |  |  | 		c_value = ast_strip(c_value + 1); | 
					
						
							|  |  |  | 		c_header = ast_strip(c_header); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (ast_strlen_zero(c_header)) { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "Identify '%s' has no SIP header to match in match_header '%s'.\n", | 
					
						
							|  |  |  | 				ast_sorcery_object_get_id(identify), identify->match_header); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!strcmp(c_value, "//")) { | 
					
						
							|  |  |  | 			/* An empty regex is the same as an empty literal string. */ | 
					
						
							|  |  |  | 			c_value = ""; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (ast_string_field_set(identify, match_header_name, c_header) | 
					
						
							|  |  |  | 			|| ast_string_field_set(identify, match_header_value, c_value)) { | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		len = strlen(c_value); | 
					
						
							|  |  |  | 		if (2 < len && c_value[0] == '/' && c_value[len - 1] == '/') { | 
					
						
							|  |  |  | 			/* Make "/regex/" into "regex" */ | 
					
						
							|  |  |  | 			c_value[len - 1] = '\0'; | 
					
						
							|  |  |  | 			++c_value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (regcomp(&identify->regex_buf, c_value, REG_EXTENDED | REG_NOSUB)) { | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "Identify '%s' failed to compile match_header regex '%s'.\n", | 
					
						
							|  |  |  | 					ast_sorcery_object_get_id(identify), c_value); | 
					
						
							|  |  |  | 				return -1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			identify->is_regex = 1; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | 	if (!identify->hosts) { | 
					
						
							| 
									
										
										
										
											2018-07-24 13:44:41 -05:00
										 |  |  | 		/* No match addresses to resolve */ | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-01 17:35:20 -04:00
										 |  |  | 	/* Hosts can produce dynamic content, so mark the identify as such */ | 
					
						
							|  |  |  | 	ast_sorcery_object_set_has_dynamic_contents(obj); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 	/* Resolve the match addresses now */ | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | 	i = ao2_iterator_init(identify->hosts, 0); | 
					
						
							|  |  |  | 	while ((current_string = ao2_iterator_next(&i))) { | 
					
						
							|  |  |  | 		int results = 0; | 
					
						
							| 
									
										
										
										
											2019-12-24 10:16:23 -05:00
										 |  |  | 		char *colon = strrchr(current_string, ':'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* We skip SRV lookup if a colon is present, assuming a port was specified */ | 
					
						
							|  |  |  | 		if (!colon) { | 
					
						
							|  |  |  | 			/* No port, and we know this is not an IP address, so perform SRV resolution on it */ | 
					
						
							|  |  |  | 			if (identify->srv_lookups) { | 
					
						
							|  |  |  | 				results = ip_identify_match_srv_lookup(identify, "_sip._udp", current_string, | 
					
						
							|  |  |  | 					results); | 
					
						
							|  |  |  | 				if (results != -1) { | 
					
						
							|  |  |  | 					results = ip_identify_match_srv_lookup(identify, "_sip._tcp", | 
					
						
							|  |  |  | 						current_string, results); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (results != -1) { | 
					
						
							|  |  |  | 					results = ip_identify_match_srv_lookup(identify, "_sips._tcp", | 
					
						
							|  |  |  | 						current_string, results); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:03 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 		/* If SRV fails fall back to a normal lookup on the host itself */ | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | 		if (!results) { | 
					
						
							|  |  |  | 			results = ip_identify_match_host_lookup(identify, current_string); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | 		if (results == 0) { | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 			ast_log(LOG_WARNING, "Identify '%s' provided address '%s' did not resolve to any address\n", | 
					
						
							|  |  |  | 				ast_sorcery_object_get_id(identify), current_string); | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | 		} else if (results == -1) { | 
					
						
							| 
									
										
										
										
											2018-01-04 12:23:15 -06:00
										 |  |  | 			ast_log(LOG_ERROR, "Identify '%s' failed when adding resolution results of '%s'\n", | 
					
						
							|  |  |  | 				ast_sorcery_object_get_id(identify), current_string); | 
					
						
							| 
									
										
										
										
											2017-01-26 13:57:59 +00:00
										 |  |  | 			ao2_ref(current_string, -1); | 
					
						
							|  |  |  | 			ao2_iterator_destroy(&i); | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:03 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-26 13:57:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		ao2_ref(current_string, -1); | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:03 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-26 13:57:59 +00:00
										 |  |  | 	ao2_iterator_destroy(&i); | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | 	ao2_ref(identify->hosts, -1); | 
					
						
							|  |  |  | 	identify->hosts = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 17:22:03 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-06 22:39:54 +00:00
										 |  |  | static int match_to_str(const void *obj, const intptr_t *args, char **buf) | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free); | 
					
						
							|  |  |  | 	const struct ip_identify_match *identify = obj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_ha_join(identify->matches, &str); | 
					
						
							|  |  |  | 	*buf = ast_strdup(ast_str_buffer(str)); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 12:36:17 -05:00
										 |  |  | static void match_to_var_list_append(struct ast_variable **head, struct ast_ha *ha) | 
					
						
							| 
									
										
										
										
											2014-03-06 22:39:54 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char str[MAX_OBJECT_FIELD]; | 
					
						
							| 
									
										
										
										
											2019-12-24 10:16:23 -05:00
										 |  |  | 	const char *addr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_sockaddr_port(&ha->addr)) { | 
					
						
							|  |  |  | 		addr = ast_strdupa(ast_sockaddr_stringify(&ha->addr)); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 12:36:17 -05:00
										 |  |  | 	snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "", | 
					
						
							|  |  |  | 			 addr, ast_sockaddr_stringify_addr(&ha->netmask)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_variable_list_append(head, ast_variable_new("match", str, "")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int match_to_var_list(const void *obj, struct ast_variable **fields) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-06 22:39:54 +00:00
										 |  |  | 	const struct ip_identify_match *identify = obj; | 
					
						
							|  |  |  | 	struct ast_variable *head = NULL; | 
					
						
							|  |  |  | 	struct ast_ha *ha = identify->matches; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (; ha; ha = ha->next) { | 
					
						
							| 
									
										
										
										
											2019-10-23 12:36:17 -05:00
										 |  |  | 		match_to_var_list_append(&head, ha); | 
					
						
							| 
									
										
										
										
											2014-03-06 22:39:54 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (head) { | 
					
						
							|  |  |  | 		*fields = head; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | static int sip_identify_to_ami(const struct ip_identify_match *identify, | 
					
						
							|  |  |  | 			       struct ast_str **buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ast_sip_sorcery_object_to_ami(identify, buf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 17:04:39 -06:00
										 |  |  | static int send_identify_ami_event(void *obj, void *arg, void *data, int flags) | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct ip_identify_match *identify = obj; | 
					
						
							|  |  |  | 	const char *endpoint_name = arg; | 
					
						
							| 
									
										
										
										
											2018-01-04 17:04:39 -06:00
										 |  |  | 	struct ast_sip_ami *ami = data; | 
					
						
							|  |  |  | 	struct ast_str *buf; | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 17:04:39 -06:00
										 |  |  | 	/* Build AMI event */ | 
					
						
							|  |  |  | 	buf = ast_sip_create_ami_event("IdentifyDetail", ami); | 
					
						
							|  |  |  | 	if (!buf) { | 
					
						
							|  |  |  | 		return CMP_STOP; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (sip_identify_to_ami(identify, &buf)) { | 
					
						
							|  |  |  | 		ast_free(buf); | 
					
						
							|  |  |  | 		return CMP_STOP; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ast_str_append(&buf, 0, "EndpointName: %s\r\n", endpoint_name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Send AMI event */ | 
					
						
							|  |  |  | 	astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); | 
					
						
							|  |  |  | 	++ami->count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_free(buf); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int format_ami_endpoint_identify(const struct ast_sip_endpoint *endpoint, | 
					
						
							|  |  |  | 					struct ast_sip_ami *ami) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-04 17:04:39 -06:00
										 |  |  | 	struct ao2_container *identifies; | 
					
						
							|  |  |  | 	struct ast_variable fields = { | 
					
						
							|  |  |  | 		.name = "endpoint", | 
					
						
							|  |  |  | 		.value = ast_sorcery_object_get_id(endpoint), | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 	identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", | 
					
						
							| 
									
										
										
										
											2018-01-04 17:04:39 -06:00
										 |  |  | 		AST_RETRIEVE_FLAG_MULTIPLE, &fields); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 	if (!identifies) { | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 17:04:39 -06:00
										 |  |  | 	/* Build and send any found identify object's AMI IdentifyDetail event. */ | 
					
						
							|  |  |  | 	ao2_callback_data(identifies, OBJ_MULTIPLE | OBJ_NODATA, | 
					
						
							|  |  |  | 		send_identify_ami_event, | 
					
						
							|  |  |  | 		(void *) ast_sorcery_object_get_id(endpoint), | 
					
						
							|  |  |  | 		ami); | 
					
						
							| 
									
										
										
										
											2014-09-18 15:14:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 17:04:39 -06:00
										 |  |  | 	ao2_ref(identifies, -1); | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ast_sip_endpoint_formatter endpoint_identify_formatter = { | 
					
						
							|  |  |  | 	.format_ami = format_ami_endpoint_identify | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | static int cli_iterator(void *container, ao2_callback_fn callback, void *args) | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	const struct ast_sip_endpoint *endpoint = container; | 
					
						
							|  |  |  | 	struct ao2_container *identifies; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct ast_variable fields = { | 
					
						
							|  |  |  | 		.name = "endpoint", | 
					
						
							|  |  |  | 		.value = ast_sorcery_object_get_id(endpoint), | 
					
						
							|  |  |  | 		.next = NULL, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	identifies = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "identify", | 
					
						
							|  |  |  | 		AST_RETRIEVE_FLAG_MULTIPLE, &fields); | 
					
						
							|  |  |  | 	if (!identifies) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ao2_callback(identifies, OBJ_NODATA, callback, args); | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 	ao2_cleanup(identifies); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | static struct ao2_container *cli_get_container(const char *regex) | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | 	RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); | 
					
						
							|  |  |  | 	struct ao2_container *s_container; | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | 	container =  ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "identify", regex); | 
					
						
							|  |  |  | 	if (!container) { | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | 	s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 		ast_sorcery_object_id_sort, ast_sorcery_object_id_compare); | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | 	if (!s_container) { | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | 	if (ao2_container_dup(s_container, container, 0)) { | 
					
						
							|  |  |  | 		ao2_ref(s_container, -1); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | 	return s_container; | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | static void *cli_retrieve_by_id(const char *id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "identify", id); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | static int cli_print_header(void *obj, void *arg, int flags) | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct ast_sip_cli_context *context = arg; | 
					
						
							|  |  |  | 	int indent = CLI_INDENT_TO_SPACES(context->indent_level); | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 	int filler = CLI_MAX_WIDTH - indent - 22; | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 	ast_assert(context->output_buffer != NULL); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ast_str_append(&context->output_buffer, 0, | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 		"%*s:  <Identify/Endpoint%*.*s>\n", | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 		indent, "Identify", filler, filler, CLI_HEADER_FILLER); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 	if (context->recurse) { | 
					
						
							|  |  |  | 		context->indent_level++; | 
					
						
							|  |  |  | 		indent = CLI_INDENT_TO_SPACES(context->indent_level); | 
					
						
							|  |  |  | 		filler = CLI_LAST_TABSTOP - indent - 24; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ast_str_append(&context->output_buffer, 0, | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 			"%*s:  <criteria%*.*s>\n", | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 			indent, "Match", filler, filler, CLI_HEADER_FILLER); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		context->indent_level--; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | static int cli_print_body(void *obj, void *arg, int flags) | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free); | 
					
						
							|  |  |  | 	struct ip_identify_match *ident = obj; | 
					
						
							|  |  |  | 	struct ast_sip_cli_context *context = arg; | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 	struct ast_ha *match; | 
					
						
							|  |  |  | 	int indent; | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 	ast_assert(context->output_buffer != NULL); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 	ast_str_append(&context->output_buffer, 0, "%*s:  %s/%s\n", | 
					
						
							|  |  |  | 		CLI_INDENT_TO_SPACES(context->indent_level), "Identify", | 
					
						
							|  |  |  | 		ast_sorcery_object_get_id(ident), ident->endpoint_name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (context->recurse) { | 
					
						
							|  |  |  | 		context->indent_level++; | 
					
						
							|  |  |  | 		indent = CLI_INDENT_TO_SPACES(context->indent_level); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (match = ident->matches; match; match = match->next) { | 
					
						
							| 
									
										
										
										
											2019-12-24 10:16:23 -05:00
										 |  |  | 			const char *addr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (ast_sockaddr_port(&match->addr)) { | 
					
						
							|  |  |  | 				addr = ast_sockaddr_stringify(&match->addr); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				addr = ast_sockaddr_stringify_addr(&match->addr); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			ast_str_append(&context->output_buffer, 0, "%*s: %s%s/%d\n", | 
					
						
							|  |  |  | 				indent, | 
					
						
							|  |  |  | 				"Match", | 
					
						
							|  |  |  | 				match->sense == AST_SENSE_ALLOW ? "!" : "", | 
					
						
							|  |  |  | 				addr, ast_sockaddr_cidr_bits(&match->netmask)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 		if (!ast_strlen_zero(ident->match_header)) { | 
					
						
							|  |  |  | 			ast_str_append(&context->output_buffer, 0, "%*s: %s\n", | 
					
						
							|  |  |  | 				indent, | 
					
						
							|  |  |  | 				"Match", | 
					
						
							|  |  |  | 				ident->match_header); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 		context->indent_level--; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (context->indent_level == 0) { | 
					
						
							|  |  |  | 			ast_str_append(&context->output_buffer, 0, "\n"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (context->show_details | 
					
						
							|  |  |  | 		|| (context->show_details_only_level_0 && context->indent_level == 0)) { | 
					
						
							|  |  |  | 		ast_str_append(&context->output_buffer, 0, "\n"); | 
					
						
							|  |  |  | 		ast_sip_cli_print_sorcery_objectset(ident, context, 0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * A function pointer to callback needs to be within the | 
					
						
							|  |  |  |  * module in order to avoid problems with an undefined | 
					
						
							|  |  |  |  * symbol when the module is loaded. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static char *my_cli_traverse_objects(struct ast_cli_entry *e, int cmd, | 
					
						
							|  |  |  | 	struct ast_cli_args *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ast_sip_cli_traverse_objects(e, cmd, a); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_cli_entry cli_identify[] = { | 
					
						
							|  |  |  | AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Identifies", | 
					
						
							|  |  |  | 	.command = "pjsip list identifies", | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | 	.usage = "Usage: pjsip list identifies [ like <pattern> ]\n" | 
					
						
							|  |  |  | 	"       List the configured PJSIP Identifies\n" | 
					
						
							|  |  |  | 	"       Optional regular expression pattern is used to filter the list.\n"), | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Identifies", | 
					
						
							|  |  |  | 	.command = "pjsip show identifies", | 
					
						
							| 
									
										
										
										
											2015-10-20 15:02:30 -06:00
										 |  |  | 	.usage = "Usage: pjsip show identifies [ like <pattern> ]\n" | 
					
						
							|  |  |  | 	"       Show the configured PJSIP Identifies\n" | 
					
						
							|  |  |  | 	"       Optional regular expression pattern is used to filter the list.\n"), | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Identify", | 
					
						
							|  |  |  | 	.command = "pjsip show identify", | 
					
						
							|  |  |  | 	.usage = "Usage: pjsip show identify <id>\n" | 
					
						
							|  |  |  | 	"       Show the configured PJSIP Identify\n"), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | static struct ast_sip_cli_formatter_entry *cli_formatter; | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | static int load_module(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-19 17:27:57 +00:00
										 |  |  | 	ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_endpoint_identifier_ip"); | 
					
						
							| 
									
										
										
										
											2013-07-30 22:41:05 +00:00
										 |  |  | 	ast_sorcery_apply_default(ast_sip_get_sorcery(), "identify", "config", "pjsip.conf,criteria=type=identify"); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:08:37 +00:00
										 |  |  | 	if (ast_sorcery_object_register(ast_sip_get_sorcery(), "identify", ip_identify_alloc, NULL, ip_identify_apply)) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "type", "", OPT_NOOP_T, 0, 0); | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "endpoint", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ip_identify_match, endpoint_name)); | 
					
						
							| 
									
										
										
										
											2014-03-06 22:39:54 +00:00
										 |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2017-03-14 07:50:07 -05:00
										 |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "match_header", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ip_identify_match, match_header)); | 
					
						
							| 
									
										
										
										
											2017-01-05 12:11:43 +00:00
										 |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "identify", "srv_lookups", "yes", OPT_BOOL_T, 1, FLDSET(struct ip_identify_match, srv_lookups)); | 
					
						
							| 
									
										
										
										
											2015-12-05 12:01:55 -04:00
										 |  |  | 	ast_sorcery_load_object(ast_sip_get_sorcery(), "identify"); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 18:35:07 +00:00
										 |  |  | 	ast_sip_register_endpoint_identifier_with_name(&ip_identifier, "ip"); | 
					
						
							| 
									
										
										
										
											2017-12-22 19:50:34 -06:00
										 |  |  | 	ast_sip_register_endpoint_identifier_with_name(&header_identifier, "header"); | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 	ast_sip_register_endpoint_formatter(&endpoint_identify_formatter); | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL); | 
					
						
							|  |  |  | 	if (!cli_formatter) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n"); | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	cli_formatter->name = "identify"; | 
					
						
							|  |  |  | 	cli_formatter->print_header = cli_print_header; | 
					
						
							|  |  |  | 	cli_formatter->print_body = cli_print_body; | 
					
						
							|  |  |  | 	cli_formatter->get_container = cli_get_container; | 
					
						
							|  |  |  | 	cli_formatter->iterate = cli_iterator; | 
					
						
							|  |  |  | 	cli_formatter->get_id = ast_sorcery_object_get_id; | 
					
						
							|  |  |  | 	cli_formatter->retrieve_by_id = cli_retrieve_by_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_sip_register_cli_formatter(cli_formatter); | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 	ast_cli_register_multiple(cli_identify, ARRAY_LEN(cli_identify)); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int reload_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ast_sorcery_reload_object(ast_sip_get_sorcery(), "identify"); | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int unload_module(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-12-02 00:31:49 +00:00
										 |  |  | 	ast_cli_unregister_multiple(cli_identify, ARRAY_LEN(cli_identify)); | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 	ast_sip_unregister_cli_formatter(cli_formatter); | 
					
						
							| 
									
										
										
										
											2013-12-20 21:32:13 +00:00
										 |  |  | 	ast_sip_unregister_endpoint_formatter(&endpoint_identify_formatter); | 
					
						
							| 
									
										
										
										
											2018-05-17 08:58:43 +02:00
										 |  |  | 	ast_sip_unregister_endpoint_identifier(&header_identifier); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	ast_sip_unregister_endpoint_identifier(&ip_identifier); | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP IP endpoint identifier", | 
					
						
							| 
									
										
										
										
											2015-05-05 20:49:04 -04:00
										 |  |  | 	.support_level = AST_MODULE_SUPPORT_CORE, | 
					
						
							|  |  |  | 	.load = load_module, | 
					
						
							|  |  |  | 	.reload = reload_module, | 
					
						
							|  |  |  | 	.unload = unload_module, | 
					
						
							| 
									
										
										
										
											2016-05-06 12:54:17 -04:00
										 |  |  | 	.load_pri = AST_MODPRI_CHANNEL_DEPEND - 4, | 
					
						
							| 
									
										
										
										
											2017-11-19 17:30:49 -05:00
										 |  |  | 	.requires = "res_pjsip", | 
					
						
							| 
									
										
										
										
											2015-05-05 20:49:04 -04:00
										 |  |  | ); |