#!/bin/bash
# BEGIN_ICS_COPYRIGHT8 ****************************************
# Copyright (c) 2003 InfiniCon Systems, Inc.  All rights reserved.
#
# THIS SOFTWARE IS PROVIDED BY INFINICON SYSTEMS, INC. ("INFINICON") TO EACH
# PERSON OR COMPANY ("RECIPIENT") ON AN "AS IS" BASIS.  ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL INFINICON BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED OR ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
# Any agreements between InfiniCon and the Recipient shall apply to Recipient's
# use of the Software.
# END_ICS_COPYRIGHT8   ****************************************

# [ICS VERSION STRING: @(#) ./bin/setup_self_ssh 7_4_3_0_2 [01/26/18 09:02]
# setup password-less ssh on a single host so it can ssh to itself
# This is part of IntelIB-Basic for use by setup_ssh in FastFabric
# Authors:      Steve T. Pesini, Todd Rimmer

trap "exit 1" SIGHUP SIGTERM SIGINT

Usage()
{
	echo "Usage: setup_self_ssh [-U] [-i ipoib_hostname]" >&2
	echo "              or" >&2
	echo "       setup_self_ssh --help" >&2
	echo "   --help - produce full help text" >&2
	echo "   -U - only perform connect (to enter in local hosts knownhosts)" >&2
	echo "   -i ipoib_hostname - ipoib hostname for this host" >&2
	echo "         default is to skip setup for ipoib to self" >&2
	echo "example:">&2
	echo "   setup_self_ssh -i myhost-ib" >&2
	echo "   setup_self_ssh -U" >&2
	exit 2
}

if [ x"$1" = "x--help" ]
then
	Usage
fi

user=`id -u -n`
Uopt=n
ihost_ib=
while getopts Ui: param
do
	case $param in
	U)
		Uopt=y;;
	i)
		ihost_ib="$OPTARG";;
	?)
		Usage;;
	esac
done
shift $((OPTIND -1))
if [ $# -gt 0 ]
then
	Usage
fi

# connect to host via ssh
connect_to_host()
{
	# $1 = user
	# $2 = host

	# We use an alternate file to build up the new keys
	# this way parallel calls can let ssh itself handle file locking
	# then update_known_hosts can replace data in real known_hosts file
	ssh -o 'UserKnownHostsFile=~/.ssh/.known_hosts-ffnew' -o 'StrictHostKeyChecking=no' $1@$2 echo "$2: Connected"
}

# update known_hosts file with information from connect_to_host calls
update_known_hosts()
{
	if [ -e ~/.ssh/.known_hosts-ffnew ]
	then
		if [ -e ~/.ssh/known_hosts ]
		then
			(
			IFS=" ,	"
			while read name trash
			do
				# remove old entry from known hosts in case key changed
				if grep "^$name[, ]" < ~/.ssh/known_hosts > /dev/null 2>&1
				then
					grep -v "^$name[, ]" < ~/.ssh/known_hosts > ~/.ssh/.known_hosts-fftmp
					mv ~/.ssh/.known_hosts-fftmp ~/.ssh/known_hosts
				fi
			done < ~/.ssh/.known_hosts-ffnew
			)
		fi
		cat ~/.ssh/.known_hosts-ffnew >> ~/.ssh/known_hosts
		chmod go-w ~/.ssh/known_hosts

		rm -rf ~/.ssh/.known_hosts-ffnew ~/.ssh/.known_hosts-fftmp
	fi
}

# Generate SSH Key
cd ~
if [ ! -f .ssh/id_rsa.pub -o ! -f .ssh/id_rsa ]
then
	ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa 
fi

# configure ssh on the host(s)
rm -rf ~/.ssh/.known_hosts-ffnew ~/.ssh/.known_hosts-fftmp

#stty_settings=`stty -g`
ihost=`hostname | cut -f1 -d.`

# setup ssh to ourselves
if [ $Uopt = n ]
then
	echo "$ihost: Configuring localhost ssh..."
	rm -f ~/.ssh/.tmp_keys$$

	>> ~/.ssh/authorized_keys
	cat ~/.ssh/authorized_keys ~/.ssh/id_rsa.pub |sort -u > ~/.ssh/.tmp_keys$$
	mv ~/.ssh/.tmp_keys$$ ~/.ssh/authorized_keys

	>> ~/.ssh/authorized_keys2
	cat ~/.ssh/authorized_keys2 ~/.ssh/id_rsa.pub |sort -u > ~/.ssh/.tmp_keys$$
	mv ~/.ssh/.tmp_keys$$ ~/.ssh/authorized_keys2

	chmod go-w ~/.ssh/authorized_keys ~/.ssh/authorized_keys2
fi

echo "$ihost: Verifying localhost ssh..."
# make sure we can ssh ourselves so ipoibping test works
connect_to_host $user localhost
connect_to_host $user $ihost
# make sure we can ssh to ourselves over ipoib, so MPI can be run on master
if [ x"$ihost_ib" != "x" ]
then
	connect_to_host $user $ihost_ib
fi
echo "$ihost: Configured localhost ssh"

#stty $stty_settings
update_known_hosts
