<p>Kevin Harwell <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/9720">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve
Kevin Harwell: Looks good to me, approved; Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_endpoint_identifier_ip.c: Added regex support to match_header<br><br>This patch adds regular expression support to make the identify section's<br>match_header option more useful when attempting to match complex headers<br>like the 'To' or 'From' headers. The 'From' header has variable<br>components such as the tag parameter that you cannot predict. To specify<br>a regular expression put slashes around the regular expression in place of<br>the header value.<br><br>[identify-alice]<br>type=identify<br>endpoint=alice<br>match_header=From: /<sip:alice@127\\.0\\.0\\.1>/<br><br>* Added regex support to match_header so you could match a 'To' header<br>among other complex headers.<br><br>Fixed reported crashes when trying to match special headers like 'Contact'.<br>The identify section's match_header method used code that assumed you were<br>matching a generic header. Any other type of header could cause a crash<br>if the header structure variant did not match the generic header enough.<br><br>* Made use code that will work for any header type instead of code<br>specific to generic headers.<br><br>Other fixes while in the area:<br><br>* Made check all headers of the requested name.<br>* Added some more sanity checks to the configured identify matching<br>options when applying the configuration.<br><br>ASTERISK-27548<br><br>Change-Id: I27dfd4ff5e2259b906640e3c330681b76b4ed1f1<br>---<br>M CHANGES<br>M res/res_pjsip_endpoint_identifier_ip.c<br>2 files changed, 125 insertions(+), 38 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/CHANGES b/CHANGES</span><br><span>index c1cd83f..38c87e6 100644</span><br><span>--- a/CHANGES</span><br><span>+++ b/CHANGES</span><br><span>@@ -143,6 +143,13 @@</span><br><span> when both 'SIP' and 'Q.850' Reason headers are received. This option allows</span><br><span> the 'Q.850' Reason header to be suppressed. The default value is 'no'.</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+res_pjsip_endpoint_identifier_ip</span><br><span style="color: hsl(120, 100%, 40%);">+------------------</span><br><span style="color: hsl(120, 100%, 40%);">+ * Added regex support to the identify section match_header option. You</span><br><span style="color: hsl(120, 100%, 40%);">+ specify a regex instead of an explicit string by surrounding the header</span><br><span style="color: hsl(120, 100%, 40%);">+ value with slashes:</span><br><span style="color: hsl(120, 100%, 40%);">+ match_header = SIPHeader: /regex/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> ------------------------------------------------------------------------------</span><br><span> --- Functionality changes from Asterisk 15.4.0 to Asterisk 15.5.0 ------------</span><br><span> ------------------------------------------------------------------------------</span><br><span>diff --git a/res/res_pjsip_endpoint_identifier_ip.c b/res/res_pjsip_endpoint_identifier_ip.c</span><br><span>index 2468786..30b2a2e 100644</span><br><span>--- a/res/res_pjsip_endpoint_identifier_ip.c</span><br><span>+++ b/res/res_pjsip_endpoint_identifier_ip.c</span><br><span>@@ -42,7 +42,7 @@</span><br><span> <description></span><br><span> <para>This module provides alternatives to matching inbound requests to</span><br><span> a configured endpoint. At least one of the matching mechanisms</span><br><span style="color: hsl(0, 100%, 40%);">- must be provided, or the object configuration will be invalid.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ must be provided, or the object configuration is invalid.</para></span><br><span> <para>The matching mechanisms are provided by the following</span><br><span> configuration options:</para></span><br><span> <enumlist></span><br><span>@@ -86,6 +86,13 @@</span><br><span> specified with a <literal>:</literal>, as in</span><br><span> <literal>match_header = SIPHeader: value</literal>.</span><br><span> </para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>The specified SIP header value can be a regular</span><br><span style="color: hsl(120, 100%, 40%);">+ expression if the value is of the form</span><br><span style="color: hsl(120, 100%, 40%);">+ /<replaceable>regex</replaceable>/.</span><br><span style="color: hsl(120, 100%, 40%);">+ </para></span><br><span style="color: hsl(120, 100%, 40%);">+ <note><para>Use of a regex is expensive so be sure you need</span><br><span style="color: hsl(120, 100%, 40%);">+ to use a regex to match your endpoint.</span><br><span style="color: hsl(120, 100%, 40%);">+ </para></note></span><br><span> </description></span><br><span> </configOption></span><br><span> <configOption name="type"></span><br><span>@@ -109,13 +116,21 @@</span><br><span> AST_STRING_FIELD(endpoint_name);</span><br><span> /*! If matching by header, the header/value to match against */</span><br><span> AST_STRING_FIELD(match_header);</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! SIP header name of the match_header string */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD(match_header_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! SIP header value of the match_header string */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD(match_header_value);</span><br><span> );</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Compiled match_header regular expression when is_regex is non-zero */</span><br><span style="color: hsl(120, 100%, 40%);">+ regex_t regex_buf;</span><br><span> /*! \brief Networks or addresses that should match this */</span><br><span> struct ast_ha *matches;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! \brief Perform SRV resolution of hostnames */</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int srv_lookups;</span><br><span> /*! \brief Hosts to be resolved when applying configuration */</span><br><span> struct ao2_container *hosts;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! \brief Perform SRV resolution of hostnames */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int srv_lookups;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Non-zero if match_header has a regular expression (i.e., regex_buf is valid) */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int is_regex:1;</span><br><span> };</span><br><span> </span><br><span> /*! \brief Destructor function for a matching object */</span><br><span>@@ -126,6 +141,9 @@</span><br><span> ast_string_field_free_memory(identify);</span><br><span> ast_free_ha(identify->matches);</span><br><span> ao2_cleanup(identify->hosts);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (identify->is_regex) {</span><br><span style="color: hsl(120, 100%, 40%);">+ regfree(&identify->regex_buf);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> </span><br><span> /*! \brief Allocator function for a matching object */</span><br><span>@@ -146,47 +164,66 @@</span><br><span> {</span><br><span> struct ip_identify_match *identify = obj;</span><br><span> struct pjsip_rx_data *rdata = arg;</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_generic_string_hdr *header;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_hdr *header;</span><br><span> pj_str_t pj_header_name;</span><br><span style="color: hsl(0, 100%, 40%);">- pj_str_t pj_header_value;</span><br><span style="color: hsl(0, 100%, 40%);">- char *c_header;</span><br><span style="color: hsl(0, 100%, 40%);">- char *c_value;</span><br><span style="color: hsl(120, 100%, 40%);">+ int header_present;</span><br><span> </span><br><span> if (ast_strlen_zero(identify->match_header)) {</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- c_header = ast_strdupa(identify->match_header);</span><br><span style="color: hsl(0, 100%, 40%);">- c_value = strchr(c_header, ':');</span><br><span style="color: hsl(0, 100%, 40%);">- if (!c_value) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* This should not be possible. The object cannot be created if so. */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_assert(0);</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- *c_value = '\0';</span><br><span style="color: hsl(0, 100%, 40%);">- c_value++;</span><br><span style="color: hsl(0, 100%, 40%);">- c_value = ast_strip(c_value);</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_header_name = pj_str((void *) identify->match_header_name);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- pj_header_name = pj_str(c_header);</span><br><span style="color: hsl(0, 100%, 40%);">- header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &pj_header_name, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!header) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(3, "Identify '%s': SIP message does not have header '%s'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check all headers of the given name for a match. */</span><br><span style="color: hsl(120, 100%, 40%);">+ header_present = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ for (header = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ (header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &pj_header_name, header));</span><br><span style="color: hsl(120, 100%, 40%);">+ header = header->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char *pos;</span><br><span style="color: hsl(120, 100%, 40%);">+ int len;</span><br><span style="color: hsl(120, 100%, 40%);">+ char buf[PATH_MAX];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ header_present = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Print header line to buf */</span><br><span style="color: hsl(120, 100%, 40%);">+ len = pjsip_hdr_print_on(header, buf, sizeof(buf) - 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (len < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Buffer not large enough or no header vptr! */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_assert(0);</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ buf[len] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove header name from pj_buf and trim blanks. */</span><br><span style="color: hsl(120, 100%, 40%);">+ pos = strchr(buf, ':');</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!pos) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* No header name? Bug in PJPROJECT if so. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_assert(0);</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ pos = ast_strip(pos + 1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Does header value match what we are looking for? */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (identify->is_regex) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!regexec(&identify->regex_buf, pos, 0, NULL, 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return CMP_MATCH;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(identify->match_header_value, pos)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return CMP_MATCH;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Identify '%s': SIP message has '%s' header but value '%s' does not match '%s'.\n",</span><br><span> ast_sorcery_object_get_id(identify),</span><br><span style="color: hsl(0, 100%, 40%);">- c_header);</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ identify->match_header_name,</span><br><span style="color: hsl(120, 100%, 40%);">+ pos,</span><br><span style="color: hsl(120, 100%, 40%);">+ identify->match_header_value);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- pj_header_value = pj_str(c_value);</span><br><span style="color: hsl(0, 100%, 40%);">- if (pj_strcmp(&pj_header_value, &header->hvalue)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(3, "Identify '%s': SIP message has header '%s' but value '%.*s' does not match '%s'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!header_present) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Identify '%s': SIP message does not have '%s' header.\n",</span><br><span> ast_sorcery_object_get_id(identify),</span><br><span style="color: hsl(0, 100%, 40%);">- c_header,</span><br><span style="color: hsl(0, 100%, 40%);">- (int) pj_strlen(&header->hvalue), pj_strbuf(&header->hvalue),</span><br><span style="color: hsl(0, 100%, 40%);">- c_value);</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ identify->match_header_name);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return CMP_MATCH;</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span> }</span><br><span> </span><br><span> /*! \brief Comparator function for matching an object by IP address */</span><br><span>@@ -404,14 +441,57 @@</span><br><span> ast_sorcery_object_get_id(identify));</span><br><span> return -1;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_strlen_zero(identify->match_header)</span><br><span style="color: hsl(0, 100%, 40%);">- && !strchr(identify->match_header, ':')) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_ERROR, "Identify '%s' missing ':' separator in match_header '%s'.\n",</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sorcery_object_get_id(identify), identify->match_header);</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(identify->match_header)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char *c_header;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *c_value;</span><br><span style="color: hsl(120, 100%, 40%);">+ int len;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Split the header name and value */</span><br><span style="color: hsl(120, 100%, 40%);">+ c_header = ast_strdupa(identify->match_header);</span><br><span style="color: hsl(120, 100%, 40%);">+ c_value = strchr(c_header, ':');</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!c_value) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Identify '%s' missing ':' separator in match_header '%s'.\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sorcery_object_get_id(identify), identify->match_header);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ *c_value = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ c_value = ast_strip(c_value + 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ c_header = ast_strip(c_header);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(c_header)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Identify '%s' has no SIP header to match in match_header '%s'.\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sorcery_object_get_id(identify), identify->match_header);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcmp(c_value, "//")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* An empty regex is the same as an empty literal string. */</span><br><span style="color: hsl(120, 100%, 40%);">+ c_value = "";</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_string_field_set(identify, match_header_name, c_header)</span><br><span style="color: hsl(120, 100%, 40%);">+ || ast_string_field_set(identify, match_header_value, c_value)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ len = strlen(c_value);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (2 < len && c_value[0] == '/' && c_value[len - 1] == '/') {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Make "/regex/" into "regex" */</span><br><span style="color: hsl(120, 100%, 40%);">+ c_value[len - 1] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ ++c_value;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (regcomp(&identify->regex_buf, c_value, REG_EXTENDED | REG_NOSUB)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Identify '%s' failed to compile match_header regex '%s'.\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sorcery_object_get_id(identify), c_value);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ identify->is_regex = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> </span><br><span> if (!identify->hosts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* No match addresses to resolve */</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/9720">change 9720</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/9720"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: I27dfd4ff5e2259b906640e3c330681b76b4ed1f1 </div>
<div style="display:none"> Gerrit-Change-Number: 9720 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Richard Mudgett <rmudgett@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>