#!/bin/sh
#
# Script to install openssh on a HP-UX machine
# author Stefano Turolla
# ESO - European Southern Observatory
# e-mail: sturolla@eso.org
#

#sub to delete old ssh installations
delete_old()
{
	echo "removing old installation ...."
	rm -f /usr/lib/libz.a
	rm -f /usr/bin/scp
	rm -f /usr/bin/slogin
	rm -f /usr/bin/sftp
	rm -f /usr/bin/ssh
	rm -f /usr/libexec/sftp-server
	rm -f /usr/bin/ssh-add
	rm -f /usr/bin/ssh-agent
	rm -f /usr/bin/ssh-keygen
	rm -f /usr/bin/ssh-keyscan
	rm -f /usr/sbin/sshd
	rm -f /usr/man/man1/scp.1
	rm -f /usr/man/man1/sftp.1
	rm -f /usr/man/man8/sftp-server.8
	rm -f /usr/man/man1/slogin.1
	rm -f /usr/man/man1/ssh-add.1
	rm -f /usr/man/man1/ssh-keygen.1
	rm -f /usr/man/man1/ssh-keyscan.1
	rm -f /usr/man/man8/sshd.8
	rm -f /usr/man/man1/ssh-agent.1
	rm -f /usr/man/man1/ssh.1

	find /sbin/init.d -name "*ssh*" -exec rm -rf {} \;
	find /sbin/rc2.d -name "*ssh*" -exec rm -rf {} \;

	echo "done..."
}

# subroutine to kill all sshd daemon running before installation
kill_ssh()
{
	if [ -x /sbin/init.d/sshd ]
	then 
       	 	/sbin/init.d/sshd stop
	fi

	# if there is a process called sshd with parent id=1 (init) then kill it
	id=`cat /var/run/sshd.pid`

       	echo "killing proces number $id"
     	kill -9  $id
}

# main

PATH=/usr/bin:/usr/bin:/bin:/usr/ucb
clear
machine=`hostname`
os=`/usr/bin/uname -s`
ver=`/usr/bin/uname -r | /usr/bin/awk -F'.' '{print $2 "." $3}'`
user=`/usr/bin/whoami`
current_dir=`/usr/bin/pwd`
tarfile="openssh-$os-$ver.tar"

if [ $user != "root" ] 
then
	echo "You must be root, sorry..."
	exit 1
fi

if [ $os != "HP-UX" ] 
then
	echo "this script install openssh for HP-UX 10.20 and 11.00 !"
	exit 1
fi

echo "Installing openssh version 3.4p1 on machine $machine and os = $os"


if [ ! -f "$tarfile" ] ;
then
	echo "the file $tarfile is missing in the current directory"
	exit 1
fi

# copying tar file in /tmp

echo "copying tar file in /tmp"
/usr/bin/cp $tarfile /tmp

# removing old version
echo "Removing old version....."

kill_ssh

delete_old

echo "extracting openssh distribution....."
cd /
/usr/bin/tar xvf /tmp/$tarfile
cd $current_dir
/usr/bin/rm -f /tmp/$tarfile

echo "configuring  openssh...."

if [ ! -f /etc/ssh/ssh_host_key -o ! -s /etc/ssh/ssh_host_key ]
then
        /usr/bin/ssh-keygen -b 1024 -f /etc/ssh/ssh_host_key -N '' >&2
fi
if [ ! -f /etc/ssh/ssh_host_dsa_key -o ! -s /etc/ssh/ssh_host_dsa_key ]
then
        /usr/bin/ssh-keygen -d -f /etc/ssh/ssh_host_dsa_key -N '' >&2
fi

if [ -f "/etc/ssh/ssh_host_key" ] ; 
then 
	  echo "/etc/ssh/ssh_host_key already exists, skipping." ; 
else 
	  /usr/bin//ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_key -N "" ; 
fi ; 

if [ -f /etc/ssh/ssh_host_dsa_key ] ; then 
	  echo "/etc/ssh/ssh_host_dsa_key already exists, skipping." ; 
else 
	  /usr/bin/ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N "" ; 
fi ; 

if [ -f /etc/ssh/ssh_host_rsa_key ] ; then 
	  echo "/etc/ssh/ssh_host_rsa_key already exists, skipping." ; 
else
	/usr/bin/ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" ; 
                fi ; 


/sbin/rc2.d/S999sshd start
echo done

