[type/__uci] Refactor to do proper quoting of UCI commands

This commit is contained in:
Dennis Camera 2020-10-23 11:45:37 +02:00
parent d453d964e1
commit f782a5a370

View file

@ -18,15 +18,37 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>. # along with cdist. If not, see <http://www.gnu.org/licenses/>.
# #
in_list() { printf '%s\n' "$@" | { grep -qxF "$(read -r NDL; echo "${NDL}")"; } } in_list() {
printf '%s\n' "$@" | { grep -qxF "$(read -r ndl; echo "${ndl}")"; }
}
quote() {
for _arg
do
shift
if test -n "$(printf %s "${_arg}" | tr -d -c '\t\n \042-\047\050-\052\073-\077\133\\`|~' | tr -c '' '.')"
then
# needs quoting
set -- "$@" "$(printf "'%s'" "$(printf %s "${_arg}" | sed -e "s/'/'\\\\''/g")")"
else
set -- "$@" "${_arg}"
fi
done
unset _arg
# NOTE: Use printf because POSIX echo interprets escape sequences
printf '%s' "$*"
}
uci_cmd() { uci_cmd() {
printf 'printf "%s\n"' "$1" # Usage: uci_cmd [UCI ARGUMENTS]...
shift # Will output a shell command to be executed locally to add the given
printf " '%s'" "$@" # command to $tmpfile.
printf " >>'%s'\n" "${tmpfile}" printf "printf '%%s\\\\n' %s" "$(quote "$(quote "$@")")"
printf ' >>%s\n' "$(quote "${tmpfile}")"
} }
config=${__object_id:?} config=${__object_id:?}
state_is=$(cat "${__object:?}/explorer/state") state_is=$(cat "${__object:?}/explorer/state")
@ -57,19 +79,19 @@ in
if test "${state_is}" != 'absent' if test "${state_is}" != 'absent'
then then
printf "uci delete '%s'\n" "${config}" uci_cmd delete "${config}"
fi fi
while read -r value while read -r value
do do
uci_cmd "add_list '%s'='%s'" "${config}" "${value}" uci_cmd add_list "${config}"="${value}"
done <"${__object:?}/parameter/value" done <"${__object:?}/parameter/value"
else else
# "should" is a scalar # "should" is a scalar
printf 'set %s\n' "${config}" >>"${__messages_out:?}" printf 'set %s\n' "${config}" >>"${__messages_out:?}"
value=$(cat "${__object:?}/parameter/value") value=$(cat "${__object:?}/parameter/value")
uci_cmd "set '%s'='%s'" "${config}" "${value}" uci_cmd set "${config}"="${value}"
fi fi
;; ;;
(absent) (absent)
@ -79,7 +101,7 @@ in
fi fi
printf 'delete %s\n' "${config}" >>"${__messages_out:?}" printf 'delete %s\n' "${config}" >>"${__messages_out:?}"
uci_cmd "delete '%s'" "${config}" uci_cmd delete "${config}"
;; ;;
(*) (*)
printf 'Invalid --state: %s\n' "${state_should}" >&2 printf 'Invalid --state: %s\n' "${state_should}" >&2