]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
enum.c: Add support for regular expression flag in NAPTR record
authorSean Bright <sean.bright@gmail.com>
Fri, 6 Mar 2020 20:59:37 +0000 (15:59 -0500)
committerSean Bright <sean.bright@gmail.com>
Fri, 6 Mar 2020 21:06:31 +0000 (15:06 -0600)
A regular expression in a NAPTR response record can have a trailing
'i' flag to indicate that the expression should be evaluated in a
case-insensitive way. We were not checking for that flag which caused
the record parsing to fail on otherwise valid input.

Although this change will initially go into Asterisk 13, 16, and 17,
it is my intention to replace the majority of this code in 16 and up -
including this fix - by changing enum.c to consume the new DNS API
which duplicates most of this logic already. Asterisk 13 doesn't have
the DNS API, so this fix will be as good as it gets.

ASTERISK-26711 #close
Reported by: Vitold

Change-Id: I33943a5b3e7539c6dca3a5079982ee15a08186f0

main/enum.c

index 344ae404d56414ff1febcb3bf4216e99d878f360..f4177f98118719517864b61462f518b78f9cbaff 100644 (file)
@@ -414,6 +414,7 @@ static int parse_naptr(unsigned char *dst, int dstsize, char *tech, int techsize
        int size, matchindex; /* size is the size of the backreference sub. */
        size_t d_len = sizeof(tempdst) - 1;
        regex_t preg;
+       int re_flags = REG_EXTENDED | REG_NEWLINE;
        regmatch_t pmatch[max_bt];
 
        tech_return[0] = '\0';
@@ -495,13 +496,23 @@ static int parse_naptr(unsigned char *dst, int dstsize, char *tech, int techsize
         * and uses that character to find the index of the second delimiter */
        delim = regexp[0];
        delim2 = strchr(regexp + 1, delim);
-       if ((delim2 == NULL) || (regexp[regexp_len - 1] != delim)) {  /* is the second delimiter found, and is the end of the regexp a delimiter */
+       if ((delim2 == NULL)
+               || ((regexp[regexp_len - 1] != 'i' || regexp[regexp_len - 2] != delim)
+                       && regexp[regexp_len - 1] != delim)) {
                ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
                return -1;
        } else if (strchr((delim2 + 1), delim) == NULL) { /* if the second delimiter is found, make sure there is a third instance.  this could be the end one instead of the middle */
                ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
                return -1;
        }
+
+       /* Make the regex case-insensitive if the 'i' flag is present. This assumes you
+        * aren't using 'i' as a delimiter which, altough dubious, does not appear to be
+        * explicitly non-compliant */
+       if (regexp[regexp_len - 1] == 'i') {
+               re_flags |= REG_ICASE;
+       }
+
        pattern = regexp + 1;   /* pattern is the regex without the begining and ending delimiter */
        *delim2 = 0;    /* zero out the middle delimiter */
        subst   = delim2 + 1; /* dst substring is everything after the second delimiter. */
@@ -511,7 +522,7 @@ static int parse_naptr(unsigned char *dst, int dstsize, char *tech, int techsize
  * now do the regex wizardry.
  */
 
-       if (regcomp(&preg, pattern, REG_EXTENDED | REG_NEWLINE)) {
+       if (regcomp(&preg, pattern, re_flags)) {
                ast_log(LOG_WARNING, "NAPTR Regex compilation error (regex = \"%s\").\n", regexp);
                return -1;
        }