[type/__postgres_database] Improve quoting

This commit is contained in:
Dennis Camera 2021-04-15 16:34:17 +02:00
parent 6ac8cbf98f
commit 58b279a8d0
1 changed files with 55 additions and 40 deletions

View File

@ -1,6 +1,7 @@
#!/bin/sh -e #!/bin/sh -e
# #
# 2011 Steven Armstrong (steven-cdist at armstrong.cc) # 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2021 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -18,49 +19,63 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>. # along with cdist. If not, see <http://www.gnu.org/licenses/>.
# #
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' "${_arg}" | sed -e "s/'/'\\\\''/g")'"
else
set -- "$@" "${_arg}"
fi
done
unset _arg
# NOTE: Use printf because POSIX echo interprets escape sequences
printf '%s' "$*"
}
name="$__object_id"
state_should="$(cat "$__object/parameter/state")"
state_is="$(cat "$__object/explorer/state")"
postgres_user=$(cat "${__object:?}/explorer/postgres_user") postgres_user=$(cat "${__object:?}/explorer/postgres_user")
if [ "$state_should" != "$state_is" ]; then dbname=${__object_id:?}
case "$state_should" in state_should=$(cat "${__object:?}/parameter/state")
present) state_is=$(cat "${__object:?}/explorer/state")
owner=""
if [ -f "$__object/parameter/owner" ]; then if test "${state_should}" = "$state_is"
owner="-O \"$(cat "$__object/parameter/owner")\"" then
exit 0
fi fi
template="" case ${state_should}
if [ -f "$__object/parameter/template" ]; then in
template="--template \"$(cat "$__object/parameter/template")\"" (present)
fi set --
encoding="" while read -r param_name opt
if [ -f "$__object/parameter/encoding" ]; then do
encoding="--encoding \"$(cat "$__object/parameter/encoding")\"" if test -f "${__object:?}/parameter/${param_name}"
then
set -- "$@" "${opt}" "$(cat "${__object:?}/parameter/${param_name}")"
fi fi
done <<-'EOF'
owner -O
template --template
encoding --encoding
lc_collate --lc-collate
lc_ctype --lc-ctype
EOF
lc_collate="" set -- "$@" "${dbname}"
if [ -f "$__object/parameter/lc-collate" ]; then
lc_collate="--lc-collate \"$(cat "$__object/parameter/lc-collate")\""
fi
lc_ctype="" cat <<-EOF
if [ -f "$__object/parameter/lc-ctype" ]; then su - $(quote "${postgres_user}") -c $(quote "$(quote createdb "$@")")
lc_ctype="--lc-ctype \"$(cat "$__object/parameter/lc-ctype")\""
fi
cat << EOF
su - '$postgres_user' -c "createdb $owner \"$name\" $template $encoding $lc_collate $lc_ctype"
EOF EOF
;; ;;
absent) (absent)
cat << EOF cat <<-EOF
su - '$postgres_user' -c "dropdb \"$name\"" su - $(quote "${postgres_user}") -c $(quote "$(quote dropdb "${dbname}")")
EOF EOF
;; ;;
esac esac
fi