#! /bin/sh
#
usage="\
Usage: $0 -c <client> [-u <user>] [-p <port>] [-t <type>] [--test] |-h]

establish ssh-key(s) so that user $USER on server `hostname`
have access as <user> on remote client <client>.

Attention! You need <user> acccess on <client>! That means either public/private
key connection is always established or you need to know the password of <user>
at <client>

	-c <client>	- client machine where we want to have access
	-u <user>	- user-name at what we want connect to the client
				Default = root
	-p <port>	- specify the port to use for ssh connection.
				Default is 22
	-t <type>	- specifies the type of key to create.
				Possible values are rsa1, rsa or dsa
				Default = rsa
				(Details see man ssh-keygen)
	--test		- the success of the key exchange will be tested
	-h		- this help

Example:
- for backuppc requirements (you have to be logged in as user backuppc!)
 $0 -c <name of the client which should backed up> --test
"
# based on http://backuppc.sourceforge.net/faq/ssh.html#how_do_i_setup_openssh

typeset -i len
clUser=root
type="rsa"

while test $# -gt 0; do
	case "$1" in
	-p | --port )
		shift; scpport="-P $1"; sshport="-p $1"; shift;;
	-c | --client )
		shift; client=$1; shift;;
	-u | --user )
		shift; clUser=$1; shift;;
	-t | --type ) shift;
		case "$1" in
		rsa1 | RSA1 )
			type="rsa1";;
		dsa | DSA )
			type="dsa";;
		rsa | RSA )
			type="rsa";;
		* )
			echo "unknown protocol type $1"
			echo "$usage"
			exit 1;;
		esac
		shift;;
	--test)
		test="true"; shift;;
	* | -h | --help)
		echo "$usage"
		exit 0
		;;
	esac
done

if [ -z $client ]
then
	echo "$usage"
	exit 0
fi

if [ ! -e $HOME/.ssh/id_$type.pub ]
then
	echo "generate our private and public key"
	ssh-keygen -q -t $type -f $HOME/.ssh/id_$type -N "" -C "$USER@`hostname`"
fi

echo "copy our public key to $clUser@$client"
scp -p $scpport $HOME/.ssh/id_$type.pub $clUser@$client:\$HOME
status=$?
if [ $status == 0 ]
then
	echo "install our public key on $clUser@$client"
	ssh $sshport $clUser@$client "mkdir \$HOME/.ssh > /dev/null 2>&1; cat \$HOME/id_$type.pub >> \$HOME/.ssh/authorized_keys; rm -f \$HOME/id_$type.pub; chmod -R go-rwx \$HOME/.ssh"
	status=$?

	if [ $status == 0 ]
	then
		echo "generate private and public key on $clUser@$client, if neccessary"
		ssh $sshport $clUser@$client "if [ ! -e \$HOME/.ssh/id_$type.pub ]; then ssh-keygen -q -t $type -f \$HOME/.ssh/id_$type -N \"\" -C \"\$USER@\`hostname\`\"; fi"
		status=$?
		if [ $status == 0 ]
		then
			echo "get public key from $clUser@$client"
			scp -p $scpport $clUser@$client:\$HOME/.ssh/id_$type.pub $HOME
			status=$?
			if [ $status == 0 ]
			then
				echo "install $clUser@$client public key on $USER@`hostname`"
				cat $HOME/id_$type.pub >> $HOME/.ssh/known_hosts
				rm -f $HOME/id_$type.pub
				chmod -R go-rwx $HOME/.ssh
				if [ ! -z $test ]
				then
					echo "Successfull Key exchange will be tested"
					echo "You should see the string \"$clUser\""
					ssh $sshport -l $clUser $client whoami
					status=$?
				fi
			fi
		fi
	fi
fi
if [ $status != 0 ]
then
	echo "ERROR: something fails with status $status"
fi
exit $status
