Fix a bug in DeleteSSHKeys task.
[snf-image] / snf-image-helper / tasks / 40DeleteSSHKeys.in
index 2707b8c..ba171a2 100644 (file)
@@ -1,5 +1,22 @@
 #! /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
@@ -10,6 +27,9 @@
 set -e
 . "@commondir@/common.sh"
 
+# 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
@@ -43,13 +63,13 @@ done
 
 config="$target/etc/ssh/sshd_config"
 if [ ! -e "$config" ]; then
-    echo "Warning: Config file: \`$config' is missing."
-    echo "Warning: Can't check for non-default keys."
+    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
@@ -71,7 +91,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