1#!/bin/bash
2
3##########################################################################
4# SecPanel.dist - Key-Distribution
5# Shellscript for distributing public keys to remote hosts
6#
7# Version SecPanel 0.6.0
8# Author: Steffen Leich-Nienhaus <steffen.leich _at_ gmail.com>
9##########################################################################
10
11
12if [ -z $4 ]
13then
14cat <<EOF
15
16	SecPanel 0.6.0 - key distribution
17	Shellscript for distributing public keys to remote hosts
18	Usage: spdistkey.sh <host> <port> <user> <keyfile> <sshbin>
19
20EOF
21    exit 2
22fi
23
24HOST=$1
25PORT=$2
26USER=$3
27IDENTITY=$4
28SSHBIN=$5
29
30if [ ! -s $IDENTITY ]
31then
32    echo "Keyfile $IDENTITY does not exist or has size zero"
33    do_exit
34fi
35
36
37cat <<EOF
38
39    SecPanel - Distribution of public keys to remote hosts
40    ------------------------------------------------------
41
42    Connecting to $HOST:$PORT as $USER
43    with key $IDENTITY
44
45    First we try to check if the key is already on the target host.
46
47EOF
48
49$SSHBIN -l $USER -p $PORT $HOST "mkdir \$HOME/.ssh 2>/dev/null; grep '$(cat $IDENTITY)' \$HOME/.ssh/authorized_keys > /dev/null 2>&1"
50
51DISTRET=$?
52
53if [ $DISTRET = 0 ]
54then
55    echo
56    echo "    Your public key is already on the remote host"
57    echo
58elif [ $DISTRET = 255 ]
59then
60    echo
61    echo "There was an error connecting to the remote site"
62    echo -e "Parameters:\n\tHost:\t$HOST\n\tUser:\t$USER"
63    echo
64    echo "Canceling the key-transfer"
65    do_exit
66else
67    echo
68    echo "    The key could not be found on this host"
69    echo "    -> Transfering your public key to remote host"
70    echo
71    $SSHBIN -l $USER $HOST "cat >> \$HOME/.ssh/authorized_keys; chmod 600 \$HOME/.ssh/authorized_keys; chmod 700 \$HOME/.ssh" < $IDENTITY
72fi
73
74