2020-05-29 15:33:40 +00:00
|
|
|
#!/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 <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2020-05-30 16:27:13 +00:00
|
|
|
state_should=$(cat "${__object}/parameter/state")
|
|
|
|
|
2020-05-31 09:57:54 +00:00
|
|
|
if test -f "${__object}/parameter/server"
|
2020-05-30 16:27:13 +00:00
|
|
|
then
|
2020-05-31 09:57:54 +00:00
|
|
|
server=$(cat "${__object}/parameter/server")
|
2020-05-30 16:27:13 +00:00
|
|
|
else
|
2020-05-31 09:57:54 +00:00
|
|
|
server=$__object_id
|
2020-05-30 16:27:13 +00:00
|
|
|
fi
|
2020-05-31 09:57:54 +00:00
|
|
|
login=$(cat "${__object}/parameter/login")
|
2020-05-30 16:27:13 +00:00
|
|
|
|
|
|
|
case $state_should
|
|
|
|
in
|
|
|
|
(present)
|
2020-05-31 13:01:40 +00:00
|
|
|
line_should=$(printf '%s|%s:%s\n' \
|
|
|
|
"${login}" "${server}" \
|
|
|
|
"$(cksum "${__object}/parameter/password" | cut -d' ' -f1)")
|
|
|
|
if grep -qxF "${line_should}" "${__object}/explorer/authusers"
|
2020-05-30 16:27:13 +00:00
|
|
|
then
|
|
|
|
# correct line already present -> nothing to do
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-05-31 13:01:40 +00:00
|
|
|
test -n "${login}" || { echo '--login must be non-empty' >&2; exit 1 }
|
|
|
|
|
2020-05-30 16:27:13 +00:00
|
|
|
mode=1
|
|
|
|
;;
|
|
|
|
(absent)
|
2020-05-31 13:01:40 +00:00
|
|
|
# no matching logins present -> nothing to do
|
|
|
|
test -s "${__object}/explorer/authusers" || exit 0
|
2020-05-30 16:27:13 +00:00
|
|
|
|
|
|
|
mode=0
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
printf 'Invalid --state: %s' "${state_should}" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-05-31 13:01:40 +00:00
|
|
|
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
|
|
|
|
|
2020-05-30 16:27:13 +00:00
|
|
|
cat <<EOF
|
2020-05-31 13:01:40 +00:00
|
|
|
export auth_conf='${auth_conf}'
|
|
|
|
export login='${login}'
|
|
|
|
export server='${server}'
|
|
|
|
mode=${mode}
|
2020-05-31 09:57:54 +00:00
|
|
|
EOF
|
2020-05-30 16:27:13 +00:00
|
|
|
|
2020-05-31 09:57:54 +00:00
|
|
|
cat <<'EOF'
|
2020-05-31 13:01:40 +00:00
|
|
|
test -f "${auth_conf}" || touch "${auth_conf}"
|
|
|
|
|
|
|
|
awk -F '\n' -v mode=$mode '
|
|
|
|
function getpw( line, path) {
|
|
|
|
path = (ENVIRON["__object"] "/parameter/password")
|
|
|
|
getline line < path
|
|
|
|
close(path)
|
|
|
|
return line
|
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
DP = "[: \t]" # copied from dma/conf.c
|
|
|
|
}
|
|
|
|
|
|
|
|
# skip comments and empty lines
|
|
|
|
/^#/ || /^$/ { print; next }
|
|
|
|
|
|
|
|
{
|
|
|
|
login = substr($0, 1, index($0, "|") - 1)
|
|
|
|
host = substr($0, length(login) + 2)
|
|
|
|
if (match(host, DP)) {
|
|
|
|
host = substr(host, 1, RSTART - 1)
|
|
|
|
endpos = length(login) + RSTART
|
|
|
|
} else endpos = length
|
|
|
|
}
|
|
|
|
|
|
|
|
host == ENVIRON["server"] {
|
|
|
|
if (mode) {
|
|
|
|
if (login == ENVIRON["login"] && !written) {
|
|
|
|
printf "%s%s\n", substr($0, 1, endpos+1), getpw()
|
|
|
|
written = 1
|
|
|
|
next
|
|
|
|
}
|
|
|
|
} else if (!ENVIRON["login"] || login == ENVIRON["login"]) next
|
|
|
|
}
|
|
|
|
|
|
|
|
{ print }
|
|
|
|
|
|
|
|
END {
|
|
|
|
if (mode && !written) {
|
|
|
|
printf "%s|%s:%s\n", ENVIRON["login"], ENVIRON["server"], getpw()
|
|
|
|
}
|
2020-05-30 16:27:13 +00:00
|
|
|
}
|
2020-05-31 13:01:40 +00:00
|
|
|
' <"${auth_conf}" >"${auth_conf}.tmp" \
|
|
|
|
&& mv "${auth_conf}.tmp" "${auth_conf}"
|
2020-05-30 16:27:13 +00:00
|
|
|
EOF
|