### BEGIN TASK INFO
# Provides: DeleteSSHKeys
-# RunBefore: UmountImage
+# RunBefore: EnforcePersonality
# RunAfter: MountImage
# Short-Description: Remove ssh keys and in some cases recreate them
### END TASK INFO
. "@commondir@/common.sh"
trap task_cleanup EXIT
-report_start_task
+report_task_start
# Check if the task should be prevented from running.
check_if_excluded
log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing."
fi
-if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" != "linux" ]; then
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
exit 0
fi
-distro=$(get_base_distro "$SNF_IMAGE_TARGET")
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
+ distro=$(get_base_distro "$SNF_IMAGE_TARGET")
+fi
HOST_KEY="/etc/ssh/ssh_host_key"
RSA_KEY="/etc/ssh/ssh_host_rsa_key"
target="$SNF_IMAGE_TARGET"
+if [ "x$distro" = "xdebian" ]; then
+ mount -o bind /proc "$target/proc"
+ add_cleanup umount "$target/proc"
+ mount -o bind /dev "$target/dev"
+ add_cleanup umount "$target/dev"
+fi
+
#Remove the default keys
for pair in "$HOST_KEY@rsa1" "$RSA_KEY@rsa" "$DSA_KEY@dsa" "$ECDSA_KEY@ecdsa"; do
key=$(echo $pair | cut -d@ -f1)
fi
# Remove non-default keys...
-grep ^HostKey "$config" || true | while read key_line; do
+{ grep ^HostKey "$config" || true; } | while read key_line; do
key=$(echo $key_line | cut -d" " -f2)
if [ "$key" = $HOST_KEY -o "$key" = $RSA_KEY -o \
"$key" = $DSA_KEY -o "$key" = $ECDSA_KEY ]; then
- continue;
+ continue
fi
if [ "x$distro" = "xdebian" ]; then
# Most distros recreate missing keys...debian complains
type=""
if [ -e "$target/$key" ]; then
- if grep -e "-----BEGIN DSA PRIVATE KEY-----" "$target/$key"; then
+ if grep -e "-----BEGIN DSA PRIVATE KEY-----" "$target/$key" > /dev/null; then
type=dsa
- elif grep -e "-----BEGIN EC PRIVATE KEY-----" "$target/$key"; then
+ elif grep -e "-----BEGIN EC PRIVATE KEY-----" "$target/$key" > /dev/null; then
type=ecdsa
- elif grep -e "-----BEGIN RSA PRIVATE KEY-----" "$target/$key"; then
+ elif grep -e "-----BEGIN RSA PRIVATE KEY-----" "$target/$key" > /dev/null; then
type=rsa
- elif grep -e "SSH PRIVATE KEY FILE FORMAT" "$target/$key"; then
+ elif grep -e "SSH PRIVATE KEY FILE FORMAT" "$target/$key" > /dev/null; then
type=rsa1
fi
else # do some guessing...