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-06-09 18:53:01 +00:00
|
|
|
state_is=$(cat "${__object}/explorer/state")
|
2020-05-30 16:27:13 +00:00
|
|
|
state_should=$(cat "${__object}/parameter/state")
|
|
|
|
|
2020-09-28 08:59:13 +00:00
|
|
|
server=$__object_id
|
2020-05-31 09:57:54 +00:00
|
|
|
login=$(cat "${__object}/parameter/login")
|
2020-05-30 16:27:13 +00:00
|
|
|
|
2020-09-28 08:59:13 +00:00
|
|
|
|
|
|
|
auth_conf=$(cat "${__object}/explorer/auth_conf")
|
|
|
|
test -n "${auth_conf}" || {
|
|
|
|
echo 'Cannot determine path of dma auth.conf' >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2020-06-09 18:53:01 +00:00
|
|
|
if test "${state_is}" = "${state_should}"
|
|
|
|
then
|
|
|
|
# state is as it should
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-05-30 16:27:13 +00:00
|
|
|
case $state_should
|
|
|
|
in
|
|
|
|
(present)
|
2020-06-01 17:20:05 +00:00
|
|
|
test -n "${login}" || { echo '--login must be non-empty' >&2; exit 1; }
|
2020-05-31 13:01:40 +00:00
|
|
|
|
2020-05-30 16:27:13 +00:00
|
|
|
mode=1
|
2020-05-31 13:18:11 +00:00
|
|
|
|
2020-06-09 18:53:01 +00:00
|
|
|
if test "${state_is}" = 'absent'
|
2020-05-31 13:18:11 +00:00
|
|
|
then
|
|
|
|
printf 'add authuser %s on %s\n' "${login}" "${server}" >>"${__messages_out}"
|
2020-06-09 18:53:01 +00:00
|
|
|
else
|
|
|
|
printf 'set authuser %s on %s\n' "${login}" "${server}" >>"${__messages_out}"
|
2020-05-31 13:18:11 +00:00
|
|
|
fi
|
|
|
|
;;
|
2020-05-30 16:27:13 +00:00
|
|
|
(absent)
|
|
|
|
mode=0
|
2020-05-31 13:18:11 +00:00
|
|
|
|
|
|
|
printf 'delete authuser %s on %s\n' "${login}" "${server}" >>"${__messages_out}"
|
2020-05-30 16:27:13 +00:00
|
|
|
;;
|
|
|
|
(*)
|
2020-06-11 13:38:34 +00:00
|
|
|
printf 'Invalid --state: %s.\n' "${state_should}" >&2
|
|
|
|
printf 'Acceptable values are: present, absent.\n' >&2
|
2020-05-30 16:27:13 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-05-31 13:01:40 +00:00
|
|
|
|
2020-05-30 16:27:13 +00:00
|
|
|
cat <<EOF
|
2020-06-09 18:53:01 +00:00
|
|
|
auth_conf='${auth_conf}'
|
2020-05-31 13:01:40 +00:00
|
|
|
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 '
|
2020-06-09 18:53:01 +00:00
|
|
|
function getvalue(path) {
|
2020-06-11 13:21:35 +00:00
|
|
|
# Reads the first line of the file located at path and returns it.
|
2020-06-09 18:53:01 +00:00
|
|
|
getline < path
|
2020-05-31 13:01:40 +00:00
|
|
|
close(path)
|
2020-06-09 18:53:01 +00:00
|
|
|
return $0
|
|
|
|
}
|
|
|
|
|
|
|
|
function print_should() {
|
|
|
|
printf "%s|%s:%s\n", login_param, host_param, passwd_param
|
2020-05-31 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
DP = "[: \t]" # copied from dma/conf.c
|
2020-06-09 18:53:01 +00:00
|
|
|
|
|
|
|
parameter_dir = ENVIRON["__object"] "/parameter/"
|
|
|
|
|
2020-09-28 08:59:13 +00:00
|
|
|
host_param = ENVIRON["__object_id"]
|
2020-06-09 18:53:01 +00:00
|
|
|
login_param = getvalue(parameter_dir "login")
|
|
|
|
passwd_param = getvalue(parameter_dir "password")
|
2020-05-31 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# skip comments and empty lines
|
2020-06-11 13:09:51 +00:00
|
|
|
/^#/ || /^$/ {
|
|
|
|
print
|
|
|
|
next
|
|
|
|
}
|
2020-05-31 13:01:40 +00:00
|
|
|
|
|
|
|
{
|
2020-06-09 18:53:01 +00:00
|
|
|
# parse line
|
|
|
|
|
2020-05-31 13:01:40 +00:00
|
|
|
login = substr($0, 1, index($0, "|") - 1)
|
2020-06-09 18:53:01 +00:00
|
|
|
if (!login) { login = $0 } # if no "|" found
|
|
|
|
|
2020-05-31 13:01:40 +00:00
|
|
|
host = substr($0, length(login) + 2)
|
2020-06-09 18:53:01 +00:00
|
|
|
|
2020-05-31 13:01:40 +00:00
|
|
|
if (match(host, DP)) {
|
2020-06-11 13:09:51 +00:00
|
|
|
passwd = substr(host, RSTART + 1)
|
2020-05-31 13:01:40 +00:00
|
|
|
host = substr(host, 1, RSTART - 1)
|
2020-06-09 18:53:01 +00:00
|
|
|
} else {
|
|
|
|
passwd = ""
|
|
|
|
}
|
2020-05-31 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 18:53:01 +00:00
|
|
|
host == host_param {
|
2020-05-31 13:01:40 +00:00
|
|
|
if (mode) {
|
2020-06-09 18:53:01 +00:00
|
|
|
# state_should == present
|
|
|
|
if (login == login_param && !written) {
|
2020-06-11 13:21:35 +00:00
|
|
|
# replace line if host and login match (but only if no line has
|
|
|
|
# been written already -> no duplicates)
|
2020-06-09 18:53:01 +00:00
|
|
|
print_should()
|
2020-05-31 13:01:40 +00:00
|
|
|
written = 1
|
|
|
|
}
|
2020-06-11 13:21:35 +00:00
|
|
|
next
|
2020-06-09 18:53:01 +00:00
|
|
|
} else {
|
|
|
|
# state_should == absent
|
|
|
|
if (!login_param || login == login_param) {
|
|
|
|
# empty --login -> drop all lines for this host
|
|
|
|
next
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 13:01:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 18:53:01 +00:00
|
|
|
# leave other lines alone
|
2020-06-11 13:21:35 +00:00
|
|
|
{
|
|
|
|
print
|
|
|
|
}
|
2020-05-31 13:01:40 +00:00
|
|
|
|
|
|
|
END {
|
|
|
|
if (mode && !written) {
|
2020-06-09 18:53:01 +00:00
|
|
|
# append line if no match to replace was found
|
|
|
|
print_should()
|
2020-05-31 13:01:40 +00:00
|
|
|
}
|
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
|