#!/bin/sh -e
#
# 2020 Dennis Camera (dennis.camera@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 .
#
state_should=$(cat "${__object}/parameter/state")
if test -f "${__object}/parameter/server"
then
server=$(cat "${__object}/parameter/server")
else
server=$__object_id
fi
login=$(cat "${__object}/parameter/login")
case $state_should
in
(present)
line_should=$(printf '%s|%s:%s\n' \
"${login}" "${server}" \
"$(cksum "${__object}/parameter/password" | cut -d' ' -f1)")
if grep -qxF "${line_should}" "${__object}/explorer/authusers"
then
# correct line already present -> nothing to do
exit 0
fi
test -n "${login}" || { echo '--login must be non-empty' >&2; exit 1; }
mode=1
if test -s "${__object}/explorer/authusers"
then
printf 'set authuser %s on %s\n' "${login}" "${server}" >>"${__messages_out}"
else
printf 'add authuser %s on %s\n' "${login}" "${server}" >>"${__messages_out}"
fi
;;
(absent)
# no matching logins present -> nothing to do
test -s "${__object}/explorer/authusers" || exit 0
mode=0
printf 'delete authuser %s on %s\n' "${login}" "${server}" >>"${__messages_out}"
;;
(*)
printf 'Invalid --state: %s' "${state_should}" >&2
exit 1
;;
esac
auth_conf=$(cat "${__object}/explorer/auth_conf")
if test -z "${auth_conf}"
then
echo 'Cannot determine path of dma auth.conf' >&2
exit 1
fi
cat <"${auth_conf}.tmp" \
&& mv "${auth_conf}.tmp" "${auth_conf}"
EOF