More compgen -W instead of embedding user input in sed/awk/grep fixes.

This commit is contained in:
Ville Skyttä 2009-11-01 21:01:18 +02:00
parent 3725168d8a
commit ac644578e0
4 changed files with 42 additions and 38 deletions

View File

@ -33,7 +33,8 @@ bash-completion (2.x)
$plusdirs have been dropped too. 3rd party completions should switch $plusdirs have been dropped too. 3rd party completions should switch
to using the complete/compgen features directly, and BASH_VERSINFO to using the complete/compgen features directly, and BASH_VERSINFO
for bash version checks. for bash version checks.
* Fix sed error in qdbus completions containing slashes (Debian: 552631). * Protect various completions from unusual user input by not embedding the
input in external command arguments (Debian: 552631).
* Add /sbin to $PATH when invoking ifconfig and iwconfig. * Add /sbin to $PATH when invoking ifconfig and iwconfig.
* Combine dcop and qdbus completions into the latter. * Combine dcop and qdbus completions into the latter.

View File

@ -480,23 +480,23 @@ _configured_interfaces()
{ {
if [ -f /etc/debian_version ]; then if [ -f /etc/debian_version ]; then
# Debian system # Debian system
COMPREPLY=( $( sed -ne 's|^iface \([^ ]\+\).*$|\1|p' \ COMPREPLY=( $( compgen -W "$( sed -ne 's|^iface \([^ ]\+\).*$|\1|p' \
/etc/network/interfaces ) ) /etc/network/interfaces )" -- "$cur" ) )
elif [ -f /etc/SuSE-release ]; then elif [ -f /etc/SuSE-release ]; then
# SuSE system # SuSE system
COMPREPLY=( $( command ls \ COMPREPLY=( $( compgen -W "$( command ls \
/etc/sysconfig/network/ifcfg-* | \ /etc/sysconfig/network/ifcfg-* | \
sed -ne 's|.*ifcfg-\('"$cur"'.*\)|\1|p' ) ) sed -ne 's|.*ifcfg-\(.*\)|\1|p' )" -- "$cur" ) )
elif [ -f /etc/pld-release ]; then elif [ -f /etc/pld-release ]; then
# PLD Linux # PLD Linux
COMPREPLY=( $( command ls -B \ COMPREPLY=( $( compgen -W "$( command ls -B \
/etc/sysconfig/interfaces | \ /etc/sysconfig/interfaces | \
sed -ne 's|.*ifcfg-\('"$cur"'.*\)|\1|p' ) ) sed -ne 's|.*ifcfg-\(.*\)|\1|p' )" -- "$cur" ) )
else else
# Assume Red Hat # Assume Red Hat
COMPREPLY=( $( command ls \ COMPREPLY=( $( compgen -W "$( command ls \
/etc/sysconfig/network-scripts/ifcfg-* | \ /etc/sysconfig/network-scripts/ifcfg-* | \
sed -ne 's|.*ifcfg-\('"$cur"'.*\)|\1|p' ) ) sed -ne 's|.*ifcfg-\(.*\)|\1|p' )" -- "$cur" ) )
fi fi
} }
@ -524,7 +524,8 @@ _available_interfaces()
fi fi
COMPREPLY=( $( eval PATH="$PATH:/sbin" $cmd 2>/dev/null | \ COMPREPLY=( $( eval PATH="$PATH:/sbin" $cmd 2>/dev/null | \
sed -ne 's|^\('"$cur"'[^[:space:][:punct:]]\{1,\}\).*$|\1|p') ) awk '/^[^[:space:]]/ { print $1 }' ) )
COMPREPLY=( $( compgen -W '${COMPREPLY[@]/%[[:punct:]]/}' -- "$cur" ) )
} }
# This function expands tildes in pathnames # This function expands tildes in pathnames
@ -618,14 +619,13 @@ _uids()
_gids() _gids()
{ {
if type getent &>/dev/null; then if type getent &>/dev/null; then
COMPREPLY=( $( getent group | \ COMPREPLY=( $( compgen -W '$( getent group | cut -d: -f3 )' \
awk -F: '{if ($3 ~ /^'"$cur"'/) print $3}' ) ) -- "$cur" ) )
elif type perl &>/dev/null; then elif type perl &>/dev/null; then
COMPREPLY=( $( compgen -W '$( perl -e '"'"'while (($gid) = (getgrent)[2]) { print $gid . "\n" }'"'"' )' -- "$cur" ) ) COMPREPLY=( $( compgen -W '$( perl -e '"'"'while (($gid) = (getgrent)[2]) { print $gid . "\n" }'"'"' )' -- "$cur" ) )
else else
# make do with /etc/group # make do with /etc/group
COMPREPLY=( $( awk 'BEGIN {FS=":"} {if ($3 ~ /^'"$cur"'/) print $3}'\ COMPREPLY=( $( compgen -W '$( cut -d: -f3 /etc/group )' -- "$cur" ) )
/etc/group ) )
fi fi
} }
@ -651,8 +651,8 @@ _modules()
{ {
local modpath local modpath
modpath=/lib/modules/$1 modpath=/lib/modules/$1
COMPREPLY=( $( command ls -R $modpath | \ COMPREPLY=( $( compgen -W "$( command ls -R $modpath | \
sed -ne 's/^\('"$cur"'.*\)\.k\?o\(\|.gz\)$/\1/p') ) sed -ne 's/^\(.*\)\.k\?o\(\|.gz\)$/\1/p' )" -- "$cur" ) )
} }
# This function completes on installed modules # This function completes on installed modules
@ -660,7 +660,7 @@ _modules()
_installed_modules() _installed_modules()
{ {
COMPREPLY=( $( compgen -W "$( PATH="$PATH:/sbin" lsmod | \ COMPREPLY=( $( compgen -W "$( PATH="$PATH:/sbin" lsmod | \
awk '{if (NR != 1) print $1}' )" -- $1 ) ) awk '{if (NR != 1) print $1}' )" -- "$1" ) )
} }
# This function completes on user:group format # This function completes on user:group format
@ -899,15 +899,17 @@ _mount()
for i in {,/usr}/{,s}bin/showmount; do [ -x $i ] && sm=$i && break; done for i in {,/usr}/{,s}bin/showmount; do [ -x $i ] && sm=$i && break; done
if [ -n "$sm" ] && [[ "$cur" == *:* ]]; then if [ -n "$sm" ] && [[ "$cur" == *:* ]]; then
COMPREPLY=( $( $sm -e ${cur%%:*} | sed 1d | \ COMPREPLY=( $( compgen -W "$( $sm -e ${cur%%:*} | sed 1d | \
grep ^${cur#*:} | awk '{print $1}' ) ) awk '{print $1}' )" -- "$cur" ) )
elif [[ "$cur" == //* ]]; then elif [[ "$cur" == //* ]]; then
host=${cur#//} host=${cur#//}
host=${host%%/*} host=${host%%/*}
if [ -n "$host" ]; then if [ -n "$host" ]; then
COMPREPLY=( $( compgen -W "$( echo $( smbclient -d 0 -NL $host 2>/dev/null| COMPREPLY=( $( compgen -P "//$host" -W \
"$( smbclient -d 0 -NL $host 2>/dev/null |
sed -ne '/^['"$'\t '"']*Sharename/,/^$/p' | sed -ne '/^['"$'\t '"']*Sharename/,/^$/p' |
sed -ne '3,$s|^[^A-Za-z]*\([^'"$'\t '"']*\).*$|//'$host'/\1|p' ) )" -- "$cur" ) ) sed -ne '3,$s|^[^A-Za-z]*\([^'"$'\t '"']*\).*$|/\1|p' )" \
-- "${cur#//$host}" ) )
fi fi
elif [ -r /etc/vfstab ]; then elif [ -r /etc/vfstab ]; then
# Solaris # Solaris
@ -976,9 +978,8 @@ _insmod()
if [ $COMP_CWORD -gt 1 ] && if [ $COMP_CWORD -gt 1 ] &&
[[ "${COMP_WORDS[COMP_CWORD-1]}" != -* ]]; then [[ "${COMP_WORDS[COMP_CWORD-1]}" != -* ]]; then
# do module parameter completion # do module parameter completion
COMPREPLY=( $( /sbin/modinfo -p ${COMP_WORDS[1]} 2>/dev/null | \ COMPREPLY=( $( compgen -W "$( /sbin/modinfo -p ${COMP_WORDS[1]} | \
awk '{if ($1 ~ /^parm:/ && $2 ~ /^'"$cur"'/) { print $2 } \ cut -d: -f1 )" -- "$cur" ) )
else if ($1 !~ /:/ && $1 ~ /^'"$cur"'/) { print $1 }}' ) )
else else
_modules $(uname -r) _modules $(uname -r)
fi fi

14
contrib/configure vendored
View File

@ -23,13 +23,15 @@ _configure()
[[ "$cur" != -* ]] && return 0 [[ "$cur" != -* ]] && return 0
if [ -n "$COMP_CONFIGURE_HINTS" ]; then if [ -n "$COMP_CONFIGURE_HINTS" ]; then
COMPREPLY=( $( $1 --help 2>&1 | awk '/^ --[A-Za-z]/ { print $1; \ COMPREPLY=( $( compgen -W "$( $1 --help 2>&1 | \
if ($2 ~ /--[A-Za-z]/) print $2 }' | sed -e 's/[[,].*//g' | \ awk '/^ --[A-Za-z]/ { print $1; \
grep ^$cur ) ) if ($2 ~ /--[A-Za-z]/) print $2 }' | sed -e 's/[[,].*//g' )" \
-- "$cur" ) )
else else
COMPREPLY=( $( $1 --help 2>&1 | awk '/^ --[A-Za-z]/ { print $1; \ COMPREPLY=( $( compgen -W "$( $1 --help 2>&1 | \
if ($2 ~ /--[A-Za-z]/) print $2 }' | sed -e 's/[[,=].*//g' | \ awk '/^ --[A-Za-z]/ { print $1; \
grep ^$cur ) ) if ($2 ~ /--[A-Za-z]/) print $2 }' | sed -e 's/[[,=].*//g' )" \
-- "$cur" ) )
fi fi
} }
complete -F _configure -o filenames configure complete -F _configure -o filenames configure

View File

@ -27,13 +27,13 @@ _getent()
return 0 return 0
;; ;;
protocols|networks|ahosts|ahostsv4|ahostsv6|rpc) protocols|networks|ahosts|ahostsv4|ahostsv6|rpc)
COMPREPLY=( $( getent "$prev" | \ COMPREPLY=( $( compgen -W "$( getent "$prev" | \
sed -ne 's|^\('"$cur"'[^[:space:]]*\).*|\1|p' ) ) awk '{ print $1 }' )" -- "$cur" ) )
return 0 return 0
;; ;;
aliases|shadow) aliases|shadow)
COMPREPLY=( $( getent "$prev" | \ COMPREPLY=( $( compgen -W "$( getent "$prev" | cut -d: -f1 )" \
sed -ne 's|^\('"$cur"'[^:]*\).*|\1|p' ) ) -- "$cur" ) )
return 0 return 0
;; ;;
esac esac