#!/bin/bash

# Copyright (C) 2010 Oregon State University
#
# 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.
#
# Remove all generated keys so that each instance has unique keys for ssh

set -e

. common.sh

debug set -x

HOST_KEY="/etc/ssh/ssh_host_key"
RSA_KEY="/etc/ssh/ssh_host_rsa_key"
DSA_KEY="/etc/ssh/ssh_host_dsa_key"

if [ -e ${TARGET}/etc/debian_version ] ; then
    echo "Debian/Ubuntu: replace existing ssh keys"
    [ -f "${TARGET}/${RSA_KEY}" ] && rm -f ${TARGET}/${RSA_KEY}* && \
        ssh-keygen -t rsa -q -N '' -f ${TARGET}/${RSA_KEY}
    [ -f "${TARGET}/${DSA_KEY}" ] && rm -f ${TARGET}/${DSA_KEY}* && \
        ssh-keygen -t dsa -q -N '' -f ${TARGET}/${DSA_KEY}
    exit 0
fi

for key in $HOST_KEY $RSA_KEY $DSA_KEY ; do
    if [ -f "${TARGET}/${key}" ] ; then
        rm -f ${TARGET}/${key}*
    fi
done

exit 0
