Skip to content

Commit

Permalink
Fix quoting for omprog, improg, mmexternal
Browse files Browse the repository at this point in the history
This changes the current behaviour from honouring the double quotes in
any part of the argument but leaving them in place and passing to the
executed binary to requiring quotes exactly at the beginning and at the
end of a multi-word argument, and not including them in the actual call.

Testcases added to ensure the expected results.

Fixes #4249.
  • Loading branch information
paulfertser committed May 26, 2023
1 parent cd3b9b5 commit 7a63ac8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
25 changes: 19 additions & 6 deletions runtime/srutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,12 +836,25 @@ split_binary_parameters(uchar **const szBinary, char ***const __restrict__ aPara
iCnt = iStr = 0;
c = es_getBufAddr(estrParams); /* Reset to beginning */
while(iCnt < es_strlen(estrParams) ) {
if ( c[iCnt] == ' ' && !bInQuotes ) {
estrTmp = es_newStrFromSubStr( estrParams, iStr, iCnt-iStr);
} else if ( iCnt+1 >= es_strlen(estrParams) ) {
estrTmp = es_newStrFromSubStr( estrParams, iStr, iCnt-iStr+1);
} else if (c[iCnt] == '"') {
bInQuotes = !bInQuotes;
if (c[iCnt] == '"' && iCnt == iStr && !bInQuotes) {
bInQuotes = TRUE;
iStr++;
} else {
int bEOL = iCnt+1 == es_strlen(estrParams);
int bSpace = c[iCnt] == ' ';
int bQuoteEnd = bInQuotes && ((bSpace && c[iCnt-1] == '"') ||
(c[iCnt] == '"' && bEOL));
if (bEOL || bQuoteEnd || (bSpace && !bInQuotes)) {
int iSubCnt = iCnt - iStr;
if (bEOL)
iSubCnt++;
if (bQuoteEnd)
iSubCnt--;
estrTmp = es_newStrFromSubStr(estrParams, iStr, iSubCnt);
}

if (bQuoteEnd)
bInQuotes = FALSE;
}

if ( estrTmp != NULL ) {
Expand Down
12 changes: 10 additions & 2 deletions tests/omprog-defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ template(name="outfmt" type="string" string="%msg%\n")
:msg, contains, "msgnum:" {
action(
type="omprog"
binary=`echo $srcdir/testsuites/omprog-defaults-bin.sh p1 p2 p3`
binary="'$srcdir'/testsuites/omprog-defaults-bin.sh \"p1 with spaces\"'\
' p2 \"\" --p4=\"middle quote\" \"--p6=\"proper middle quote\"\" \"p7 is last\""
template="outfmt"
name="omprog_action"
)
Expand All @@ -37,7 +38,14 @@ injectmsg 0 10
shutdown_when_empty
wait_shutdown

export EXPECTED="Starting with parameters: p1 p2 p3
export EXPECTED="Starting with parameters: p1 with spaces p2 --p4=\"middle quote\" --p6=\"proper middle quote\" p7 is last
Next parameter is \"p1 with spaces\"
Next parameter is \"p2\"
Next parameter is \"\"
Next parameter is \"--p4=\"middle\"
Next parameter is \"quote\"\"
Next parameter is \"--p6=\"proper middle quote\"\"
Next parameter is \"p7 is last\"
Received msgnum:00000000:
Received msgnum:00000001:
Received msgnum:00000002:
Expand Down
3 changes: 3 additions & 0 deletions tests/omprog-if-error.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ cat $RSYSLOG_DYNNAME.othermsg
content_check 'must be terminated with \n' $RSYSLOG_DYNNAME.othermsg

export EXPECTED="Starting with parameters: p1 p2 p3
Next parameter is \"p1\"
Next parameter is \"p2\"
Next parameter is \"p3\"
Received msgnum:00000000:
Received msgnum:00000001:
Received msgnum:00000002:
Expand Down
4 changes: 4 additions & 0 deletions tests/testsuites/omprog-defaults-bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
outfile=$RSYSLOG_OUT_LOG

echo "Starting with parameters: $@" >> $outfile
while [ $# -gt 0 ]; do
echo Next parameter is \""$1"\"
shift
done >> $outfile

read log_line
while [[ -n "$log_line" ]]; do
Expand Down

0 comments on commit 7a63ac8

Please sign in to comment.