#!/bin/sh 
#
# ssh-pubkeymgr - A user public key manager for Secure Shell
#
# Author: Anne Carasik <anne@ssh.com> 
#
# Copyright (C) 2000 SSH Communications Security Corp, Helsinki, 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.

# 18 August 2000 - removed downloading hostkeys because you get them anyway
# during the first connection :)

## Set the default keypair to id_dsa_1024_a
keypair="id_dsa_1024_a"

while [ -n "$1" ]
do
	case $1 in
		-k)	keypair="$2" 
			echo $keypair 
			echo "Running ssh-pubkeymgr.."
			shift 2
			;;
		-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 
			;;
		*)	echo " "
			echo "Usage: ssh-pubkeymgr [-k keypair]"
			echo " "
			echo "Type man ssh-pubkeymgr for more information."
			exit 
	esac
done

## Check for compatibility for the $LOGNAME instead of $USER

if [ -z "$USER" ]; then
	if [ -n "$LOGNAME" ]; then
		USER=$LOGNAME
	else
		USER=`whoami`
	fi
fi

## Set the hostname

if [ -z "$HOSTNAME" ]; then
	HOSTNAME=`hostname -s`
	echo "Setting host to $HOSTNAME"
fi

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

## Check for the user's DSA keypair

if [ -s "$HOME/.ssh2/$keypair" -a "$HOME/.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 $HOME/.ssh2/identification
if [ -s "$HOME/.ssh2/identification" ] ; then
	echo "You already have an identity file.. Skipping.."
else
	echo "Creating your identity file.."
	echo IdKey $keypair > $HOME/.ssh2/identification
fi

## Check for $HOME/.ssh2/authorization
if [ -s "$HOME/.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 "$HOME/.ssh2/authorization"
fi


## Check for $HOME/.ssh2/$USER-$HOSTNAME.pub  

if [ -s "$HOME/.ssh2/$USER-$HOSTNAME.pub" ] ; then
	echo "You already have your local host public key.. Skipping.."
else
	echo "Creating your local host public key.."
	cp "$HOME/.ssh2/$keypair.pub" $HOME/.ssh2/$USER-$HOSTNAME.pub
	echo "Adding your local host in case you don't want to go anywhere ;)"
	echo Key $USER-$HOSTNAME.pub >> $HOME/.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. "
			echo " "
			echo "Add which user?"
				read user
			echo "Add which host?"
				read host
				echo Key $user-$host.pub >> $HOME/.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.." 
			break 
	esac	
done

echo
echo "All the new files are in your $HOME/.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

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