2017-06-28 11:19:32 +00:00
|
|
|
#!/bin/sh -e
|
2011-03-19 22:26:41 +00:00
|
|
|
#
|
2012-11-07 16:52:05 +00:00
|
|
|
# 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
|
2022-04-10 21:52:53 +00:00
|
|
|
# 2013-2022 Steven Armstrong (steven-cdist armstrong.cc)
|
2011-03-19 22:26:41 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
destination="/$__object_id"
|
2013-09-12 12:02:33 +00:00
|
|
|
state_should="$(cat "$__object/parameter/state")"
|
|
|
|
type="$(cat "$__object/explorer/type")"
|
2012-04-03 22:08:23 +00:00
|
|
|
|
2018-10-01 20:13:24 +00:00
|
|
|
[ "$state_should" = "exists" ] && [ "$type" = "file" ] && exit 0 # nothing to do
|
2011-03-19 22:26:41 +00:00
|
|
|
|
2018-03-06 06:33:22 +00:00
|
|
|
if [ "$state_should" = "pre-exists" ]; then
|
|
|
|
if [ -f "$__object/parameter/source" ]; then
|
|
|
|
echo "--source cannot be used with --state pre-exists"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-02-26 15:40:53 +00:00
|
|
|
case $type in
|
|
|
|
file)
|
|
|
|
# nothing to do
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
none)
|
|
|
|
printf 'File "%s" does not exist\n' "$destination" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
directory|symlink)
|
|
|
|
printf 'File "%s" exists and is a %s, but should be a regular file\n' "$destination" "$type" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
printf 'File or directory "%s" is in an unknown state\n' "$destination" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
2018-03-06 06:33:22 +00:00
|
|
|
fi
|
|
|
|
|
2013-09-12 12:02:33 +00:00
|
|
|
upload_file=
|
|
|
|
create_file=
|
2018-10-01 20:13:24 +00:00
|
|
|
if [ "$state_should" = "present" ] || [ "$state_should" = "exists" ]; then
|
2013-09-12 12:02:33 +00:00
|
|
|
if [ ! -f "$__object/parameter/source" ]; then
|
2017-07-04 16:23:51 +00:00
|
|
|
remote_stat="$(cat "$__object/explorer/stat")"
|
|
|
|
if [ -z "$remote_stat" ]; then
|
2017-07-04 16:25:08 +00:00
|
|
|
create_file=1
|
|
|
|
echo create >> "$__messages_out"
|
2017-07-04 16:23:51 +00:00
|
|
|
fi
|
2013-09-12 12:02:33 +00:00
|
|
|
else
|
2011-07-18 11:34:39 +00:00
|
|
|
source="$(cat "$__object/parameter/source")"
|
2012-06-04 12:17:42 +00:00
|
|
|
if [ "$source" = "-" ]; then
|
|
|
|
source="$__object/stdin"
|
|
|
|
fi
|
2013-09-12 12:02:33 +00:00
|
|
|
if [ ! -f "$source" ]; then
|
2011-07-18 11:34:39 +00:00
|
|
|
echo "Source \"$source\" does not exist." >&2
|
|
|
|
exit 1
|
2013-09-12 12:02:33 +00:00
|
|
|
else
|
|
|
|
if [ "$type" != "file" ]; then
|
|
|
|
# destination is not a regular file, upload source to replace it
|
|
|
|
upload_file=1
|
|
|
|
else
|
|
|
|
local_cksum="$(cksum < "$source")"
|
|
|
|
remote_cksum="$(cat "$__object/explorer/cksum")"
|
|
|
|
if [ "$local_cksum" != "$remote_cksum" ]; then
|
|
|
|
# destination is a regular file, but not the right one
|
|
|
|
upload_file=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2018-10-01 20:13:24 +00:00
|
|
|
if [ "$create_file" ] || [ "$upload_file" ]; then
|
2013-11-26 15:30:03 +00:00
|
|
|
# tell gencode-remote that we created or uploaded a file and that it must
|
|
|
|
# set all attributes no matter what the explorer retreived
|
2013-09-12 12:02:33 +00:00
|
|
|
mkdir "$__object/files"
|
2013-11-26 15:30:03 +00:00
|
|
|
touch "$__object/files/set-attributes"
|
|
|
|
|
|
|
|
# upload file to temp location
|
2022-04-13 22:16:10 +00:00
|
|
|
upload_destination="$(mktemp -u "/__cdist${destination}.cdist.XXXXXXXXXX" | sed 's|^/__cdist||')"
|
2022-04-10 21:52:53 +00:00
|
|
|
# Yes, we are aware that this is a race condition.
|
|
|
|
# However:
|
|
|
|
# a) cdist usually writes to directories that are not user writable
|
|
|
|
# (probably > 99.9%)
|
|
|
|
# b) if they are user owned, the user / attacker always wins
|
|
|
|
# (probably < 0.1%)
|
|
|
|
# c) the only case which we could improve are tmp directories and we
|
|
|
|
# don't think managing tmp directories with cdist is a typical case
|
|
|
|
# ("the rest %)"
|
2022-04-13 22:27:28 +00:00
|
|
|
|
2022-04-10 21:52:53 +00:00
|
|
|
# Tell gencode-remote to where we uploaded the file so it can move
|
|
|
|
# it to its final destination.
|
|
|
|
echo "$upload_destination" > "$__object/files/upload-destination"
|
|
|
|
|
2013-09-12 12:02:33 +00:00
|
|
|
if [ "$upload_file" ]; then
|
2013-12-03 14:41:43 +00:00
|
|
|
echo upload >> "$__messages_out"
|
2016-11-02 12:55:25 +00:00
|
|
|
# IPv6 fix
|
2018-10-04 20:22:22 +00:00
|
|
|
if echo "${__target_host}" | grep -q -E '^[0-9a-fA-F:]+$'
|
2016-11-05 15:38:49 +00:00
|
|
|
then
|
|
|
|
my_target_host="[${__target_host}]"
|
|
|
|
else
|
|
|
|
my_target_host="${__target_host}"
|
|
|
|
fi
|
2013-11-26 15:30:03 +00:00
|
|
|
cat << DONE
|
2022-04-10 21:52:53 +00:00
|
|
|
$__remote_copy "$source" "${my_target_host}:${upload_destination}"
|
2013-11-26 15:30:03 +00:00
|
|
|
DONE
|
2011-03-19 22:26:41 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|