[tools/bgpscanner] Handle arguments quoting properly

This commit is contained in:
Lorenzo Cogotti 2021-08-10 20:01:01 +02:00
parent c23a29d2da
commit f9e7ab9972
1 changed files with 25 additions and 17 deletions

View File

@ -106,7 +106,7 @@ chkint() {
} }
isip4() { isip4() {
for i in 1 2 3 4; do for i in 1 2 3 4; do
case $(echo "$1" | cut -d. -f"$i") in case $(printf %s\\n "$1" | cut -d. -f"$i") in
[0-9]|[0-9][0-9]|[0-1][0-9][0-9]|2[0-4][0-9]|25[0-5]) ;; [0-9]|[0-9][0-9]|[0-1][0-9][0-9]|2[0-4][0-9]|25[0-5]) ;;
*) return 1;; *) return 1;;
esac esac
@ -114,7 +114,7 @@ isip4() {
} }
isip6() { isip6() {
test "$( \ test "$( \
echo "$1" | \ printf %s\\n "$1" | \
grep -Ec '^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$' \ grep -Ec '^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$' \
)" -eq 1 )" -eq 1
} }
@ -165,22 +165,22 @@ templatef() {
[ -n "$2" ] && "$2" "$UNQ" [ -n "$2" ] && "$2" "$UNQ"
[ $? -ne 0 ] && exit 1 [ $? -ne 0 ] && exit 1
echo "$UNQ" printf %s\\n "$UNQ"
done > "$TPL" || die "'$1': File conversion failed" done > "$TPL" || die "'$1': File conversion failed"
echo "$TPL" echo "$TPL"
} }
append() { quote () { printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"; }
echo "${1:+${1}${3:+ ${3}} }${2}"
} append() { printf %s\\n "${1:+${1}${3:+ ${3}} }${2}"; }
TERM="" TERM=""
EXPR="" EXPR=""
appendtermargs() { appendtermargs() {
_T=$1; shift _T=$1; shift
TERM="$_T ( $* )" TERM="$_T \( $* \)"
} }
appendtermfiles() { appendtermfiles() {
@ -188,7 +188,7 @@ appendtermfiles() {
for i in "$@"; do _L=$(append "$_L" "$_T $i" "-or"); done for i in "$@"; do _L=$(append "$_L" "$_T $i" "-or"); done
[ -n "$TERM" ] && TERM="( $TERM -or $_L )" || TERM="$_L" [ -n "$TERM" ] && TERM="\( $TERM -or $_L \)" || TERM="$_L"
} }
pushterm() { pushterm() {
@ -201,12 +201,12 @@ exprcompile() {
pushterm pushterm
for i in $ATTRS; do TERM=$(append "$TERM" "-attr $i" "-or"); done for i in $ATTRS; do TERM=$(append "$TERM" "-attr $i" "-or"); done
[ "$(echo "$ATTRS" | wc -w)" -gt 1 ] && TERM="( $TERM )" [ "$(echo "$ATTRS" | wc -w)" -gt 1 ] && TERM="\( $TERM \)"
pushterm pushterm
[ -n "$NCOMMS" ] && TERM="-not -communities ( $NCOMMS )" [ -n "$NCOMMS" ] && TERM="-not -communities \( $NCOMMS \)"
[ -n "$COMMS" ] && TERM=$(append "$TERM" "-all-communities ( $COMMS )" "-or") [ -n "$COMMS" ] && TERM=$(append "$TERM" "-all-communities \( $COMMS \)" "-or")
[ -n "$NCOMMS" ] && [ -n "$COMMS" ] && TERM="( $TERM )" [ -n "$NCOMMS" ] && [ -n "$COMMS" ] && TERM="\( $TERM \)"
pushterm pushterm
[ -n "$EXACT" ] && appendtermargs -exact $EXACT [ -n "$EXACT" ] && appendtermargs -exact $EXACT
@ -229,10 +229,15 @@ exprcompile() {
[ -n "$REXPS" ] && TERM="-aspath \"$REXPS\"" [ -n "$REXPS" ] && TERM="-aspath \"$REXPS\""
[ -n "$NREXPS" ] && TERM=$(append "$TERM" "-not -aspath \"$NREXPS\"" "-or") [ -n "$NREXPS" ] && TERM=$(append "$TERM" "-not -aspath \"$NREXPS\"" "-or")
[ -n "$REXPS" ] && [ -n "$NREXPS" ] && TERM="( $TERM )" [ -n "$REXPS" ] && [ -n "$NREXPS" ] && TERM="\( $TERM \)"
pushterm pushterm
} }
execute() {
eval "set -- $OPTS -- "$@" $EXPR"
"$CMD" "$TGT" "$@"
}
GOTEXACT=""; GOTRELATED=""; GOTSUBNETS=""; GOTSUPNETS="" GOTEXACT=""; GOTRELATED=""; GOTSUBNETS=""; GOTSUPNETS=""
while getopts "a:A:cde:E:fi:I:lLo:m:M:p:P:r:R:s:S:t:T:u:U:" o; do while getopts "a:A:cde:E:fi:I:lLo:m:M:p:P:r:R:s:S:t:T:u:U:" o; do
case $o in case $o in
@ -273,10 +278,10 @@ while getopts "a:A:cde:E:fi:I:lLo:m:M:p:P:r:R:s:S:t:T:u:U:" o; do
o) OUTREDIR="$OPTARG" o) OUTREDIR="$OPTARG"
;; ;;
p) chkrexp "$OPTARG" p) chkrexp "$OPTARG"
REXPS=$(append "$REXPS" "$(echo "$OPTARG" | tr "?" ".")" "|") REXPS=$(append "$REXPS" "$(printf %s\\n "$OPTARG" | tr "?" ".")" "|")
;; ;;
P) chkrexp "$OPTARG" P) chkrexp "$OPTARG"
NREXPS=$(append "$NREXPS" "$(echo "$OPTARG" | tr "?" "." )" "|") NREXPS=$(append "$NREXPS" "$(printf %s\\n "$OPTARG" | tr "?" "." )" "|")
;; ;;
r) chkprefix "$OPTARG" r) chkprefix "$OPTARG"
GOTRELATED=y; RELATED=$(append "$RELATED" "$OPTARG") GOTRELATED=y; RELATED=$(append "$RELATED" "$OPTARG")
@ -321,6 +326,9 @@ exprcompile
[ -n "$OUTREDIR" ] && OPTS=$(append "$OPTS" "-o $OUTREDIR") [ -n "$OUTREDIR" ] && OPTS=$(append "$OPTS" "-o $OUTREDIR")
CMD="exec" CMD="exec"
[ -n "$PRETEND" ] && [ "$PRETEND" = 1 ] || [ "$PRETEND" = y ] || [ "$PRETEND" = yes ] && CMD="echo" if [ -n "$PRETEND" ] && [ "$PRETEND" = 1 ] || [ "$PRETEND" = y ] || [ "$PRETEND" = yes ]; then
CMD="echo"
EXPR=$(quote "$EXPR")
fi
"$CMD" "$TGT" $OPTS -- "$@" $EXPR execute "$@"