#!/bin/sh
#
# /usr/local/bin/mksshauth
# Copyright 2000-2001 Juri Jensen <juri@xenux.dk>
# Copyright 2002 Juri Jensen <juri@xenux.dk> & Jonas Smedegaard <dr@jones.dk>
#
# $Id: mksshauth,v 1.5 2003-03-24 12:08:47 jonas Exp $
#
# Setup local and remote SSH for non-interactive authorization
#

set -e

prg=`basename $0`

if [ $# \< 1 ]; then
	echo "ERROR: Wrong arguments! (Use '$prg --help' for help)"
	exit 1
fi
keytype=$1
shift

case "$keytype" in
    --help|-h)
	echo "$prg: Setup local and remote SSH for non-interactive authorization."
	echo
	echo "Usage: $prg keytype [user@]host [options]"
	echo
	echo "Options:"
	echo "    keytype: SSH key type, either rsa1, rsa, dsa, 1 or 2."
	echo "             rsa1 is for SSHv1, rsa and dsa is for SSHv2. 1 and 2 are"
	echo "             aliases for rsa1 and dsa. dsa (or 2) is recommended."
	echo "    user:    User id on remote host. Default is same as local user."
	echo "    host:    Hostname of remote host."
	echo "    options: Options to prepend the authorization key (read 'man sshd')."
	exit 0
	;;
    1|rsa1)
	keytype=rsa1
	id_file=identity.pub
	auth_file=authorized_keys
	;;
    2|dsa)
	keytype=dsa
	id_file=id_dsa.pub
	auth_file=authorized_keys2
	;;
    rsa)
	keytype=rsa
	id_file=id_rsa.pub
	auth_file=authorized_keys2
	;;
    *)
	echo "ERROR: Wrong keytype! (Use '$prg --help' for help)"
	exit 1
	;;
esac

host=$1
shift
options=$@

if [ -n "$options" ]; then
	options="$options "
fi

[ -f ~/.ssh/$id_file ] || ssh-keygen -t $keytype
ssh $host "mkdir -p ~/.ssh && echo '$options'`cat ~/.ssh/$id_file` >> ~/.ssh/$auth_file"