122 lines
2.9 KiB
Bash
Executable File
122 lines
2.9 KiB
Bash
Executable File
#!/bin/sh -e
|
|
#
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
|
|
#
|
|
# This file is part of cdist.
|
|
#
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# cdist is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
quote() {
|
|
if test $# -gt 0
|
|
then
|
|
printf '%s' "$*"
|
|
else
|
|
cat -
|
|
fi | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
|
|
}
|
|
|
|
case $(cat "${__global:?}/explorer/os")
|
|
in
|
|
(netbsd)
|
|
postgres_user='pgsql'
|
|
;;
|
|
(openbsd)
|
|
postgres_user='_postgresql'
|
|
;;
|
|
(*)
|
|
postgres_user='postgres'
|
|
;;
|
|
esac
|
|
|
|
|
|
rolename=${__object_id:?}
|
|
state_is=$(cat "${__object:?}/explorer/state")
|
|
state_should=$(cat "${__object:?}/parameter/state")
|
|
|
|
if test "${state_is}" = "${state_should}"
|
|
then
|
|
exit 0
|
|
fi
|
|
|
|
case ${state_should}
|
|
in
|
|
(present)
|
|
if test -s "${__object:?}/parameter/password"
|
|
then
|
|
quoted_password=$(
|
|
delim='$$'
|
|
# NOTE: Strip away trailing $ because with it the check breaks
|
|
# if the password ends with $ + random value.
|
|
while grep -q -F "${delim%$}" "${__object:?}/parameter/password"
|
|
do
|
|
delim="\$$(LC_ALL=C tr -cd '[:alpha:]' </dev/urandom | dd bs=1 count=4 2>/dev/null)$"
|
|
done
|
|
|
|
raw_passwd=$(cat "${__object:?}/parameter/password"; printf .)
|
|
# shellcheck disable=SC2016
|
|
printf '%s%s%s' "${delim}" "${raw_passwd%?.}" "${delim}"
|
|
)
|
|
fi
|
|
|
|
booleans=
|
|
for boolean in login createdb createrole superuser
|
|
do
|
|
booleans="${booleans}${booleans:+ }$(
|
|
if test -f "${__object:?}/parameter/${boolean}"
|
|
then
|
|
echo "${boolean}"
|
|
else
|
|
echo "no${boolean}"
|
|
fi \
|
|
| tr '[:lower:]' '[:upper:]')"
|
|
done
|
|
|
|
case ${state_is}
|
|
in
|
|
(absent)
|
|
query=$(printf 'CREATE ROLE "%s" WITH %s PASSWORD %s;' \
|
|
"${rolename}" "${booleans}" "${quoted_password:-NULL}")
|
|
;;
|
|
(different*)
|
|
query="ALTER ROLE \"${rolename}\" WITH"
|
|
|
|
if expr "${state_is}" : 'different.*properties' >/dev/null
|
|
then
|
|
query="${query} ${booleans}"
|
|
fi
|
|
if expr "${state_is}" : 'different.*password' >/dev/null
|
|
then
|
|
query="${query} PASSWORD ${quoted_password:-NULL}"
|
|
fi
|
|
|
|
query="${query};"
|
|
;;
|
|
(*)
|
|
printf 'Invalid state reported by state explorer: %s\n' "${state_is}" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
psql_cmd=$(printf 'psql postgres -wc %s' "$(quote "${query}")" | quote)
|
|
printf "su -l '%s' -c %s\\n" "${postgres_user}" "${psql_cmd}"
|
|
;;
|
|
(absent)
|
|
printf "su -l '%s' -c 'dropuser '\\\\'%s\\\\'\\n" \
|
|
"${postgres_user}" \
|
|
"$(quote "${rolename}")"
|
|
;;
|
|
esac
|