Remove the boot and shutdown process in helper VM
[snf-image] / snf-image-helper / tasks / 40DeleteSSHKeys.in
index d87ff9d..9b80630 100644 (file)
@@ -1,8 +1,25 @@
 #! /bin/bash
 
+# Copyright (C) 2011 GRNET S.A. 
+#
+# This program 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 2 of the License, or
+# (at your option) any later version.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
 ### 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
 set -e
 . "@commondir@/common.sh"
 
+trap task_cleanup EXIT
+report_task_start
+
+# Check if the task should be prevented from running.
+check_if_excluded
+
 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing."
 fi
 
-target="$SNF_IMAGE_TARGET"
-
-if [ "$SNF_IMAGE_TYPE" != "extdump" ]; then
-    cleanup
-    trap - EXIT
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" != "linux" ]; then
     exit 0
 fi
 
@@ -29,6 +48,7 @@ RSA_KEY="/etc/ssh/ssh_host_rsa_key"
 DSA_KEY="/etc/ssh/ssh_host_dsa_key"
 ECDSA_KEY="/etc/ssh/ssh_host_ecdsa_key"
 
+target="$SNF_IMAGE_TARGET"
 
 #Remove the default keys
 for pair in "$HOST_KEY@rsa1" "$RSA_KEY@rsa" "$DSA_KEY@dsa" "$ECDSA_KEY@ecdsa"; do
@@ -44,13 +64,15 @@ for pair in "$HOST_KEY@rsa1" "$RSA_KEY@rsa" "$DSA_KEY@dsa" "$ECDSA_KEY@ecdsa"; d
     fi
 done
 
-config="$target/etc/ssh/sshd_config" 
+config="$target/etc/ssh/sshd_config"
 if [ ! -e "$config" ]; then
-    log_error "Config file: \`$config' is missing."
+    warn "Config file: \`$config' is missing."
+    warn "Can't check for non-default keys."
+    exit 0
 fi
 
 # Remove non-default keys...
-grep ^HostKey "$config" | 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
@@ -72,7 +94,10 @@ grep ^HostKey "$config" | while read key_line; do
             fi
         else # do some guessing...
             for i in rsa dsa ecdsa; do
-                echo "$key" | grep _${i}_ && { type="$i"; break; }
+                if echo "$key" | grep _${i}_ > /dev/null; then
+                    type="$i";
+                    break;
+                fi
             done
         fi
         if [ -z "$type" ]; then
@@ -89,9 +114,6 @@ grep ^HostKey "$config" | while read key_line; do
     fi
 done
 
-cleanup
-trap - EXIT
-
 exit 0
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :