[Asterisk-code-review] res pjsip endpoint identifier ip.c: Fix crash on non-generic... (asterisk[13])
Richard Mudgett
asteriskteam at digium.com
Thu Jul 26 13:46:00 CDT 2018
Richard Mudgett has uploaded this change for review. ( https://gerrit.asterisk.org/9718
Change subject: res_pjsip_endpoint_identifier_ip.c: Fix crash on non-generic headers.
......................................................................
res_pjsip_endpoint_identifier_ip.c: Fix crash on non-generic headers.
The identify section's match_header method used code that assumed you were
matching a generic header. Any other type of header could cause a crash
if the header structure variant did not match the generic header enough.
* Made use code that will work for any header type instead of generic
headers.
* Made check all headers of the requested name.
* Added regex support to match_header so you could match a To: header
among other complex headers.
ASTERISK-27548
Change-Id: I27dfd4ff5e2259b906640e3c330681b76b4ed1f1
---
M CHANGES
M res/res_pjsip_endpoint_identifier_ip.c
2 files changed, 86 insertions(+), 19 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/18/9718/1
diff --git a/CHANGES b/CHANGES
index 8116882..a5a34cb 100644
--- a/CHANGES
+++ b/CHANGES
@@ -19,6 +19,13 @@
when both 'SIP' and 'Q.850' Reason headers are received. This option allows
the 'Q.850' Reason header to be suppressed. The default value is 'no'.
+res_pjsip_endpoint_identifier_ip
+------------------
+ * Added regex support to the identify section match_header option. You
+ specify a regex instead of an explicit string by surounding the header
+ value with slashes:
+ match_header = SIPHeader: /regex/
+
------------------------------------------------------------------------------
--- Functionality changes from Asterisk 13.21.0 to Asterisk 13.22.0 ----------
------------------------------------------------------------------------------
diff --git a/res/res_pjsip_endpoint_identifier_ip.c b/res/res_pjsip_endpoint_identifier_ip.c
index e737b6f..8a56e84 100644
--- a/res/res_pjsip_endpoint_identifier_ip.c
+++ b/res/res_pjsip_endpoint_identifier_ip.c
@@ -42,7 +42,7 @@
<description>
<para>This module provides alternatives to matching inbound requests to
a configured endpoint. At least one of the matching mechanisms
- must be provided, or the object configuration will be invalid.</para>
+ must be provided, or the object configuration is invalid.</para>
<para>The matching mechanisms are provided by the following
configuration options:</para>
<enumlist>
@@ -86,6 +86,13 @@
specified with a <literal>:</literal>, as in
<literal>match_header = SIPHeader: value</literal>.
</para>
+ <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>
</description>
</configOption>
<configOption name="type">
@@ -146,11 +153,14 @@
{
struct ip_identify_match *identify = obj;
struct pjsip_rx_data *rdata = arg;
- pjsip_generic_string_hdr *header;
- pj_str_t pj_header_name;
- pj_str_t pj_header_value;
+ pjsip_hdr *header;
char *c_header;
char *c_value;
+ pj_str_t pj_header_name;
+ int len;
+ int header_present;
+ int is_regex;
+ regex_t regex_buf;
if (ast_strlen_zero(identify->match_header)) {
return 0;
@@ -166,27 +176,77 @@
*c_value = '\0';
c_value++;
c_value = ast_strip(c_value);
+ len = strlen(c_value);
+ is_regex = 2 < len && c_value[0] == '/' && c_value[len - 1] == '/';
+ if (is_regex) {
+ /* Make "/regex/" into "regex" */
+ c_value[len - 1] = '\0';
+ ++c_value;
+
+ if (regcomp(®ex_buf, c_value, REG_EXTENDED | REG_NOSUB)) {
+ ast_log(LOG_ERROR, "Identify '%s': Could not compile match_header regex: /%s/\n",
+ ast_sorcery_object_get_id(identify), c_value);
+ return 0;
+ }
+ }
pj_header_name = pj_str(c_header);
- header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &pj_header_name, NULL);
- if (!header) {
+
+ /* 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;
+ 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;
+ pos = ast_strip(pos);
+
+ /* Does header value match what we are looking for? */
+ if (is_regex) {
+ if (!regexec(®ex_buf, pos, 0, NULL, 0)) {
+ regfree(®ex_buf);
+ return CMP_MATCH;
+ }
+ } else if (!strcmp(c_value, pos)) {
+ return CMP_MATCH;
+ }
+
+ ast_debug(3, "Identify '%s': SIP message has header '%s' but value '%s' does not match%s '%s'\n",
+ ast_sorcery_object_get_id(identify),
+ c_header,
+ pos,
+ is_regex ? " regex" : "",
+ c_value);
+ }
+ if (is_regex) {
+ regfree(®ex_buf);
+ }
+ if (!header_present) {
ast_debug(3, "Identify '%s': SIP message does not have header '%s'\n",
ast_sorcery_object_get_id(identify),
c_header);
- return 0;
}
-
- pj_header_value = pj_str(c_value);
- if (pj_strcmp(&pj_header_value, &header->hvalue)) {
- ast_debug(3, "Identify '%s': SIP message has header '%s' but value '%.*s' does not match '%s'\n",
- ast_sorcery_object_get_id(identify),
- c_header,
- (int) pj_strlen(&header->hvalue), pj_strbuf(&header->hvalue),
- c_value);
- return 0;
- }
-
- return CMP_MATCH;
+ return 0;
}
/*! \brief Comparator function for matching an object by IP address */
--
To view, visit https://gerrit.asterisk.org/9718
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: newchange
Gerrit-Change-Id: I27dfd4ff5e2259b906640e3c330681b76b4ed1f1
Gerrit-Change-Number: 9718
Gerrit-PatchSet: 1
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180726/cd739501/attachment-0001.html>
More information about the asterisk-code-review
mailing list