#!/bin/sh 
#
# Pubkeymgr - A user public key manager for Secure Shell
#
# Author: Anne Carasik <anne@ssh.com> 
#
# Copyright (C) 2000 SSH Communications Security Oy, Espoo, Finland
# All rights reserved.
#
# It's too much of a pain to create the public key files like identification
# and authorization. This quick little script runs ssh-keygen2, then creates
# the identification and authorization files. Then it runs scp to the remote 
# system to copy the public keys there.


## Set the command line option to keypair

while [ -n $1 ]
do
	case $1 in
		-k )	keypair=$2 
			echo $keypair 
			echo "Running ssh-pubkeymgr.."
			break ;;
		-h )	echo " "
			echo "SSH Secure Shell user public key manager"
			echo "Usage: ssh-pubkeymgr [-k keypair]"
			echo " "
			echo "Type man ssh-pubkeymgr for more information."
			exit ;;
		* )	keypair="id_dsa_1024_a"
			echo $keypair 
			echo "Running ssh-pubkeymgr.."
			break ;;
	esac
done


echo " "
echo "Checking for existing user public keys.."

## Check for the user's DSA keypair

if [ -s ~/.ssh2/$keypair -a ~/.ssh2/$keypair.pub ] ; then
	echo "You have public and private keys.. Skipping ssh-keygen2.."
else
	echo "Couldn't find your DSA keypair.. I'll generate you a new set.."
	echo "Running ssh-keygen2... don't forget to give it a passphrase!"
	ssh-keygen2
fi

## Check for ~/.ssh2/identification
if [ -s ~/.ssh2/identification ] ; then
	echo "You already have an identity file.. Skipping.."
else
	echo "Creating your identity file.."
	touch ~/.ssh2/identification
	echo IdKey $keypair > ~/.ssh2/identification
fi

## Check for ~/.ssh2/authorization
if [ -s ~/.ssh2/authorization ] ; then
	echo "You already have an authorization file.. Skipping.."
else
	echo "Creating your authorization file.."
	echo
	echo "Note: You'll need to edit this appropriately."
	touch ~/.ssh2/authorization
fi

## Check for ~/.ssh2/$USER-$HOSTNAME.pub  
if [ -s ~/.ssh2/$USER-$HOSTNAME.pub ] ; then
	echo "You already have your local host public key.. Skipping.."
else
	echo "Creating your local host public key.."
	cp ~/.ssh2/$keypair.pub ~/.ssh2/$USER-$HOSTNAME.pub
	echo "Adding your local host in case you don't want to go anywhere ;)"
	echo Key $USER-$HOSTNAME.pub >> ~/.ssh2/authorization
fi

## Ask the user for the hostname of which remote hosts to add.

echo -n "Do you want to add any hosts to your authorization file? (Default: yes)"
while read addhosts
do
	case "$addhosts" in
		"" | [yY] | [yY][eE][sS])
       	        	echo " "
                	echo "Type in their hostname, press return after"
                	echo "each one. Press return on a blank line to "
			echo "finish."
			echo " "
			echo "Add which user?"
				read user
			echo "Add which host?"
				read host
				echo Key $user-$host.pub >> ~/.ssh2/authorization
			echo "You added "$user" at "$host" as a trusted login."
			echo "Press return to continue or Ctrl-D to exit."
			;;		
		[nN] | [nN][oO])
			echo "Skipping editing the authorization file.." ;;
	esac	
done

echo
echo "All the new files are in your ~/.ssh2 directory."
echo " "
echo
echo "All the new files are in your ~/.ssh2 directory."
echo " "

echo -n "Do you want to upload " $USER"@"$HOSTNAME" key to a remote host? (Default: yes)"
while read uploadhost
do
	case "$uploadhost" in
 		"" | [yY] | [yY][eE][sS])
			echo "Upload to which host?"
				read host
			echo "Which user account?"
				read user
			echo "Where is the " $user"'s home directory? "
			echo "(e.g. /home/anne, /u/ahc, etc.)"
				read homedir
			# Run scp2 to copy the file
			echo "Now running scp2 to connect to "$host".."
			echo "Most likely you'll have to type a password :)"
 			scp2 $HOME/.ssh2/$USER-$HOSTNAME.pub $user@$host:$homedir/.ssh2/
			echo " " 
			echo "Press return to upload to more hosts or Ctrl-D to exit." ;;		
 		[nN] | [nN][oO])
 			echo "Skipping local user public key uploads.." 
			break ;;
	esac	
done

## Download the desired host keys
echo -n "Do you want to add any host keys to your list? (Default: yes)"
while read downloadhostkey
do
	case "$downloadhostkey" in
 		"" | [yY] | [yY][eE][sS])
			echo "Download from which host?"
				read host2
			echo "Which user account are you using?"
				read user2
			# Run scp2 to copy the file
			echo "Now running scp2 to connect to "$host2".."
			echo "Most likely you'll have to type a password :)"
 			scp2 $user2@$host2:/etc/ssh2/hostkey.pub ~/.ssh2/hostkeys/key_22_$host2.pub
			echo " " 
			echo "Press return to download to more hosts or Ctrl-D to exit." ;;		
 		[nN] | [nN][oO])
 			echo "Skipping host public key downloads.." 
			break ;;
	esac	
done

echo " "
echo "Don't forget to run ssh-pubkeymgr on any remote hosts you sent"
echo "your public key to."
echo " "
echo "Done."
