forked from ungleich-public/cdist
152 lines
4.1 KiB
Bash
Executable File
152 lines
4.1 KiB
Bash
Executable File
#!/bin/sh -e
|
|
#
|
|
# 2012-2014 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
# 2014 Nico Schottelius (nico-cdist at schottelius.org)
|
|
# 2021 Darko Poljak (darko.poljak at gmail.com)
|
|
#
|
|
# 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/>.
|
|
#
|
|
|
|
state="$(cat "$__object/parameter/state" 2>/dev/null)"
|
|
file="$(cat "$__object/explorer/file")"
|
|
keys_file="$__object/explorer/keys"
|
|
|
|
_type_and_key() {
|
|
echo "$1" | tr ' ' '\n' | awk '/^(ssh|ecdsa)-[^ ]+/ { printf $1" "; getline; printf $1 }'
|
|
}
|
|
|
|
_gen_key_entry() {
|
|
_the_key="$1"
|
|
# generate the key entry as it should be
|
|
if [ -f "$__object/parameter/option" ]; then
|
|
# comma seperated list of options
|
|
options="$(tr '\n' ',' < "$__object/parameter/option")"
|
|
printf '%s ' "${options%*,}"
|
|
fi
|
|
if [ -f "$__object/parameter/comment" ]; then
|
|
# extract the keytype and base64 encoded key ignoring any options and comment
|
|
printf '%s ' "$(echo "${_the_key}" | tr ' ' '\n' | awk '/^(ssh|ecdsa)-[^ ]+/ { printf $1" "; getline; printf $1 }')"
|
|
# override the comment with the one explicitly given
|
|
printf '%s' "$(cat "$__object/parameter/comment")"
|
|
else
|
|
printf '%s' "${_the_key}"
|
|
fi
|
|
printf '\n'
|
|
}
|
|
|
|
|
|
cat << DONE
|
|
new_keys=\$(mktemp ${file}.cdist.XXXXXXXXXX)
|
|
patterns=\$(mktemp ${file}.cdist.XXXXXXXXXX)
|
|
|
|
tmpfile=\$(mktemp ${file}.cdist.XXXXXXXXXX)
|
|
|
|
# preserve ownership and permissions of existing file
|
|
if [ -f "${file}" ]
|
|
then
|
|
cp -p "${file}" "\${tmpfile}"
|
|
fi
|
|
DONE
|
|
|
|
while read -r key; do
|
|
# validate key
|
|
validated_key="$(echo "${key}" | tr ' ' '\n' | awk '/^(ssh|ecdsa)-[^ ]+/ { printf $1" "; getline; printf $1 }')"
|
|
if [ -z "${validated_key}" ]
|
|
then
|
|
echo "Key is invalid: \"${key}\"" >&2
|
|
exit 1
|
|
fi
|
|
|
|
type_and_key="$(_type_and_key "${key}")"
|
|
|
|
# remove conflicting entries
|
|
cat << DONE
|
|
echo '${type_and_key}\\([ \\\\n].*\\)*\$' >> "\${patterns}"
|
|
DONE
|
|
|
|
entry="$(_gen_key_entry "${key}")"
|
|
|
|
case "${state}" in
|
|
present)
|
|
# escape single quotes
|
|
_line_sanitised=$(echo "${entry}" | sed -e "s/'/'\"'\"'/g")
|
|
cat << DONE
|
|
printf "%s\\n" "${_line_sanitised}" >> "\${new_keys}"
|
|
DONE
|
|
echo "added to ${file} (${entry})" >> "$__messages_out"
|
|
;;
|
|
absent)
|
|
cat << DONE
|
|
echo "${entry}" >> "\${patterns}"
|
|
DONE
|
|
echo "removed from ${file} (${entry})" >> "$__messages_out"
|
|
;;
|
|
esac
|
|
done < "$__object/parameter/key"
|
|
|
|
set --
|
|
cat << DONE
|
|
if [ -s "\${patterns}" ] && [ -f "${file}" ]
|
|
then
|
|
grep -v -f "\${patterns}" "${file}" > "\${tmpfile}" || true
|
|
fi
|
|
if [ -s "\${new_keys}" ]
|
|
then
|
|
cat "\${new_keys}" >> "\${tmpfile}"
|
|
fi
|
|
|
|
rm -f "\${patterns}"
|
|
rm -f "\${new_keys}"
|
|
DONE
|
|
|
|
if [ -f "$__object/parameter/remove-unknown" ] && [ -s "${keys_file}" ]
|
|
then
|
|
while read -r key
|
|
do
|
|
type_and_key="$( _type_and_key "${key}" )"
|
|
|
|
if grep -Fq "${type_and_key}" "$__object/parameter/key"
|
|
then
|
|
continue
|
|
fi
|
|
|
|
# build grep patterns
|
|
cat << DONE
|
|
echo "${key}" >> "\${patterns}"
|
|
DONE
|
|
done < "${keys_file}"
|
|
fi
|
|
|
|
cat << DONE
|
|
if [ -s "\${patterns}" ] && [ -f "${file}" ]
|
|
then
|
|
newfile=\$(mktemp ${file}.cdist.XXXXXXXXXX)
|
|
# preserve ownership and permissions of existing file
|
|
if [ -f "${file}" ]; then
|
|
cp -p "${file}" "\${newfile}"
|
|
fi
|
|
|
|
grep -v -F -x -f "\${patterns}" "\${tmpfile}" > "\${newfile}" || true
|
|
mv -f "\${newfile}" "${file}"
|
|
rm -f "\${tmpfile}"
|
|
else
|
|
mv -f "\${tmpfile}" "${file}"
|
|
fi
|
|
|
|
rm -f "\${patterns}"
|
|
rm -f "\${new_keys}"
|
|
DONE
|