[Home]

Summary:ASTERISK-29239: pbx: Using a = in a parameter for Set results in incorrect parsing
Reporter:Leandro Dardini (ldardini)Labels:patch
Date Opened:2021-01-09 18:00:48.000-0600Date Closed:
Priority:MinorRegression?
Status:Open/NewComponents:PBX/General
Versions:16.15.0 Frequency of
Occurrence
Constant
Related
Issues:
Environment:CentOS 6.x, MySQL 5.7, Asterisk 16.15.0Attachments:( 0) full.14
( 1) pbx_variables.diff
Description:If I have a func_odbc function writing in a table and one of the parameters contains a = sign, the command is not executed and no error is reported.

Let's take a very simple func_odbc entry like:

{noformat}
[QUERY_BUG]
dsn=asteriskcdrdb1,asteriskcdrdb2
synopsis=Test a query bug
writesql=insert into cdr(clid,src,dst) values ('${ARG1}','${ARG2}','${VAL1}')
{noformat}

And its AEL dialplan usage:

{noformat}
       9999 => {
            Set(ODBC_QUERY_BUG("one","twoequalthree")="four");
            Set(ODBC_QUERY_BUG("one","two=three")="four");
            Hangup();
       }
{noformat}

Only the first ODBC_QUERY_BUG command is executed while the second is skipped without any error reported.

This is the relevant DEBUG. Full DEBUG in attach

{noformat}
[2021-01-10 00:50:36] DEBUG[13330][C-00001320] pbx.c: Launching 'Set'
[2021-01-10 00:50:36] VERBOSE[13330][C-00001320] pbx.c: Executing [9999@authenticated:1] Set("PJSIP/108-DEVEL-00000017", "ODBC_QUERY_BUG("one","twoequalthree")="four"") in new stack
[2021-01-10 00:50:36] DEBUG[13330][C-00001320] pbx_variables.c: Evaluating 'ARG1' (from 'ARG1}','${ARG2}','${VAL1}')' len 4)
[2021-01-10 00:50:36] DEBUG[13330][C-00001320] pbx_variables.c: Result of 'ARG1' is 'one'
[2021-01-10 00:50:36] DEBUG[13330][C-00001320] pbx_variables.c: Evaluating 'ARG2' (from 'ARG2}','${VAL1}')' len 4)
[2021-01-10 00:50:36] DEBUG[13330][C-00001320] pbx_variables.c: Result of 'ARG2' is 'twoequalthree'
[2021-01-10 00:50:36] DEBUG[13330][C-00001320] pbx_variables.c: Evaluating 'VAL1' (from 'VAL1}')' len 4)
[2021-01-10 00:50:36] DEBUG[13330][C-00001320] pbx_variables.c: Result of 'VAL1' is 'four'
[2021-01-10 00:50:37] DEBUG[13330][C-00001320] pbx.c: Launching 'Set'
[2021-01-10 00:50:37] VERBOSE[13330][C-00001320] pbx.c: Executing [9999@authenticated:2] Set("PJSIP/108-DEVEL-00000017", "ODBC_QUERY_BUG("one","two=three")="four"") in new stack
[2021-01-10 00:50:37] DEBUG[13330][C-00001320] pbx.c: Launching 'Hangup'
[2021-01-10 00:50:37] VERBOSE[13330][C-00001320] pbx.c: Executing [9999@authenticated:3] Hangup("PJSIP/108-DEVEL-00000017", "") in new stack
{noformat}
Comments:By: Asterisk Team (asteriskteam) 2021-01-09 18:00:50.122-0600

Thanks for creating a report! The issue has entered the triage process. That means the issue will wait in this status until a Bug Marshal has an opportunity to review the issue. Once the issue has been reviewed you will receive comments regarding the next steps towards resolution. Please note that log messages and other files should not be sent to the Sangoma Asterisk Team unless explicitly asked for. All files should be placed on this issue in a sanitized fashion as needed.

A good first step is for you to review the [Asterisk Issue Guidelines|https://wiki.asterisk.org/wiki/display/AST/Asterisk+Issue+Guidelines] if you haven't already. The guidelines detail what is expected from an Asterisk issue report.

Then, if you are submitting a patch, please review the [Patch Contribution Process|https://wiki.asterisk.org/wiki/display/AST/Patch+Contribution+Process].

Please note that once your issue enters an open state it has been accepted. As Asterisk is an open source project there is no guarantee or timeframe on when your issue will be looked into. If you need expedient resolution you will need to find and pay a suitable developer. Asking for an update on your issue will not yield any progress on it and will not result in a response. All updates are posted to the issue when they occur.

Please note that by submitting data, code, or documentation to Sangoma through JIRA, you accept the Terms of Use present at [https://www.asterisk.org/terms-of-use/|https://www.asterisk.org/terms-of-use/].

By: Leandro Dardini (ldardini) 2021-01-09 18:01:29.758-0600

Asterisk log with debug at 5

By: Leandro Dardini (ldardini) 2021-01-09 18:02:57.783-0600

Tested with old plain extensions.conf and same result. Only the first query is executed

{noformat}
exten =>8888,1,Set(ODBC_QUERY_BUG("one","twoequalthree")="four")
exten =>8888,2,Set(ODBC_QUERY_BUG("one","two=three")="four")
exten =>8888,3,Hangup()
{noformat}

By: Joshua C. Colp (jcolp) 2021-01-09 18:19:00.661-0600

What happens if you try escaping the = by placing \ in front of it?

By: Leandro Dardini (ldardini) 2021-01-10 03:19:45.687-0600

Good idea, but unfortunately the result was the same. Tried with one and several escapes. Tried also using single quote

{noformat}
       9999 => {
            Set(ODBC_QUERY_BUG("one","twoequalthree")="four");
            Set(ODBC_QUERY_BUG("one","two=three")="four");
            Set(ODBC_QUERY_BUG("one","two\=three")="four");
            Set(ODBC_QUERY_BUG("one","two\\=three")="four");
            Set(ODBC_QUERY_BUG("one","two\\\=three")="four");
            Set(ODBC_QUERY_BUG("one","two\\\\=three")="four");
            Set(ODBC_QUERY_BUG('one','two=three')='four');
            Hangup();
       }
{noformat}

{noformat}
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx.c: Launching 'Set'
[2021-01-10 10:05:50] VERBOSE[9509][C-00001494] pbx.c: Executing [9999@authenticated:1] Set("PJSIP/108-DEVEL-0000001a", "ODBC_QUERY_BUG("one","twoequalthree")="four"") in new stack
2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx_variables.c: Evaluating 'ARG1' (from 'ARG1}','${ARG2}','${VAL1}')' len 4)
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx_variables.c: Result of 'ARG1' is 'one'
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx_variables.c: Evaluating 'ARG2' (from 'ARG2}','${VAL1}')' len 4)
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx_variables.c: Result of 'ARG2' is 'twoequalthree'
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx_variables.c: Evaluating 'VAL1' (from 'VAL1}')' len 4)
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx_variables.c: Result of 'VAL1' is 'four'
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx.c: Launching 'Set'
[2021-01-10 10:05:50] VERBOSE[9509][C-00001494] pbx.c: Executing [9999@authenticated:2] Set("PJSIP/108-DEVEL-0000001a", "ODBC_QUERY_BUG("one","two=three")="four"") in new stack
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx.c: Launching 'Set'
[2021-01-10 10:05:50] VERBOSE[9509][C-00001494] pbx.c: Executing [9999@authenticated:3] Set("PJSIP/108-DEVEL-0000001a", "ODBC_QUERY_BUG("one","two\=three")="four"") in new stack
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx.c: Launching 'Set'
[2021-01-10 10:05:50] VERBOSE[9509][C-00001494] pbx.c: Executing [9999@authenticated:4] Set("PJSIP/108-DEVEL-0000001a", "ODBC_QUERY_BUG("one","two\\=three")="four"") in new stack
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx.c: Launching 'Set'
[2021-01-10 10:05:50] VERBOSE[9509][C-00001494] pbx.c: Executing [9999@authenticated:5] Set("PJSIP/108-DEVEL-0000001a", "ODBC_QUERY_BUG("one","two\\\=three")="four"") in new stack
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx.c: Launching 'Set'
[2021-01-10 10:05:50] VERBOSE[9509][C-00001494] pbx.c: Executing [9999@authenticated:6] Set("PJSIP/108-DEVEL-0000001a", "ODBC_QUERY_BUG("one","two\\\\=three")="four"") in new stack
[2021-01-10 10:05:50] DEBUG[9509][C-00001494] pbx.c: Launching 'Set'
[2021-01-10 10:05:50] VERBOSE[9509][C-00001494] pbx.c: Executing [9999@authenticated:7] Set("PJSIP/108-DEVEL-0000001a", "ODBC_QUERY_BUG('one','two=three')='four'") in new stack
{noformat}

We can see the parser making the mistake in picking up the first = even in the generation of AMI event

{noformat}
[2021-01-10 10:05:50] DEBUG[9508] manager.c: Examining AMI event:
Event: VarSet
Privilege: dialplan,all
SystemName: srv02
Channel: PJSIP/108-DEVEL-0000001a
ChannelState: 4
ChannelStateDesc: Ring
CallerIDNum: 108
CallerIDName: Ruben P. Terrace
ConnectedLineNum: <unknown>
ConnectedLineName: <unknown>
Language: en
AccountCode: DEVEL
Context: authenticated
Exten: 9999
Priority: 2
Uniqueid: srv02-1610269550.1539
Linkedid: srv02-1610269550.1539
Variable: ODBC_QUERY_BUG("one","two
Value: three\")=\"four\"
{noformat}



By: Leandro Dardini (ldardini) 2021-01-10 05:24:49.303-0600

Ok, found the issue and attached a possible fix. The problem was in main/pbx_variables.c where there was no parsing of the Set command, but just a brute "strsep" looking for the first "=". I have replaced that command with a more complex scan based on the code I have found in other parts of the Asterisk source code. I am not really good at writing C, but I can read it and appreciate the really well-written code. File .diff in attach for the brave, here it is for the inpatients

{noformat}
--- /usr/local/src/asterisk-16.15.1/main/pbx_variables.orig 2021-01-10 12:02:51.220040938 +0100
+++ /usr/local/src/asterisk-16.15.1/main/pbx_variables.c 2021-01-10 12:18:51.309314729 +0100
@@ -1126,7 +1126,8 @@

int pbx_builtin_setvar(struct ast_channel *chan, const char *data)
{
- char *name, *value, *mydata;
+ char *name, *value, *mydata, *scan;
+ int paren = 0, quote = 0, bracket = 0;

if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "Set requires one variable name/value pair.\n");
@@ -1134,8 +1135,36 @@
}

mydata = ast_strdupa(data);
- name = strsep(&mydata, "=");
- value = mydata;
+
+ scan=mydata;
+ name=NULL;
+ value=NULL;
+        
+ for (; *scan; scan++) {
+          if (*scan == '(') {
+            paren++;
+          } else if (*scan == ')') {
+            if (paren) {
+              paren--;
+            }
+          } else if (*scan == '[') {
+            bracket++;
+          } else if (*scan == ']') {
+            if (bracket) {
+              bracket--;
+            }
+          } else if (*scan == '"') {
+            quote = quote ? 0 : 1;
+          } else if (*scan == '\\') {
+            scan++;
+          } else if ((*scan == '=') && !paren && !quote && !bracket) {
+            *scan++ = '\0';
+            value=scan;
+            name=mydata;
+            break;
+          }
+        }
+
if (!value) {
ast_log(LOG_WARNING, "Set requires an '=' to be a valid assignment.\n");
return 0;

{noformat}


By: Leandro Dardini (ldardini) 2021-01-10 05:28:07.591-0600

Copied a snip from ast_app_separate_args from main/app.c

By: Joshua C. Colp (jcolp) 2021-01-10 14:41:21.136-0600

Do you plan on putting this change up for review using the instructions in the initial comment, or are you just planning to only attach the patch?

By: Leandro Dardini (ldardini) 2021-01-10 16:39:57.410-0600

I think I was able to submit the patch for review using Gerrit. Being my first review submit, I hope to have done all in the right way.

By: Benjamin Keith Ford (bford) 2021-01-11 09:33:08.688-0600

I'll assign the issue to you and whoever looks at your patch on Gerrit will help you out if there are any questions!