#!/bin/bash

sshver=9.8p1
maindir=`pwd`

##主机名，ip信息
HOST=`hostname`
IP=`ifconfig -a | grep -v 127.0.0.1 | grep inet | grep -v inet6 | awk '{print $2}' | head -1`
echo "------------------hosname: ${HOST}  IP: ${IP}------------------"
echo ''

##检查原sshd文件
sshd_check_old=`/usr/sbin/sshd -t`
if [ -n "${sshd_check_old}" ];then
  echo "$sshd_check"
  echo "error: sshd check failed"
  exit 10
else
  echo "success: sshd check"
fi
sleep 2
echo ''

##备份文件
file_backup(){
echo "####backup ssh file####"
sleep 2
if [ ! -d "backup" ];then
  mkdir -p backup/{ssh_bin_bak,log,sshd_bin_bak}
fi

cp -pr /etc/ssh/ backup/ssh_bak
cp -pr ~/.ssh/ backup/root_ssh_bak
cp -p /var/log/audit/audit.log backup/log/
cp -p /usr/bin/ssh* backup/ssh_bin_bak/
cp -p /usr/sbin/sshd backup/sshd_bin_bak/
}
file_backup

echo ''

##安装openssl
sslver='1.1.1b'
ssldir="/usr/local/openssl${sslver}"
openssl_install(){
echo "####install openssl####"
sleep 2

if [ ! -e "openssl-${sslver}.tar.gz" ];then
  echo "error: openssl${sslver} not exist"
  exit 10
fi

if [ -d "openssl-${sslver}" ];then
  rm -rf openssl-${sslver}
fi

tar xvf openssl-${sslver}.tar.gz
if [ $? -eq 0 ];then
  echo "success: openssl-${sslver}.tar.gz package uncompress"
else
  echo "error: openssl-${sslver}.tar.gz package uncompress failed"
  exit 10
fi
sleep 2
echo ''

cd openssl-${sslver}
./config \
--prefix="${ssldir}"
if [ $? -eq 0 ];then
  echo "success: openssl${sslver} config"
else
  echo "error: openssl${sslver} config"
  exit 10
fi
sleep 2

echo ''

make
if [ $? -eq 0 ];then
  echo "success: openssl${sslver} make"
else
  echo "error: openssl${sslver} make failed"
  exit 10
fi
sleep 2
echo ''

make install
if [ $? -eq 0 ];then
  echo "success: openssl${sslver} install"
else
  echo "error: openssl${sslver} failed"
  exit 10
fi
sleep 2

#ln -s ${ssldir}/lib/lib* /usr/lib
#ln -s ${ssldir}/lib/lib* /usr/lib64

echo ''
}
openssl_install

##安装zlib
zlibver=1.2.11
zlibdir="/usr/local/zlib${zlibver}"
zlib_install(){
cd ${maindir}

if [ ! -e "zlib-${zlibver}.tar.gz" ];then
  echo "error: zlib-${zlibver} package not exist"
  exit 10
fi

if [ -d "zlib-${zlibver}" ];then
  rm -rf zlib-${zlibver}
fi


tar xvf zlib-${zlibver}.tar.gz
if [ $? -eq 0 ];then
  echo "success: zlib-${zlibver} package uncompress"
else
  echo "error: zlib-${zlibver} package uncompress failed"
  exit 10
fi
sleep 2
echo ''

cd zlib-${zlibver}
./configure --prefix="${zlibdir}"
if [ $? -eq 0 ];then
  echo "success: zlib-${zlibver} configure"
else
  echo "error: zlib-${zlibver} configure failed"
  exit 10
fi
sleep 2
echo ''

make 
if [ $? -eq 0 ];then
  echo "success: zlib-${zlibver} make"
else
  echo "error: zlib-${zlibver} make failed"
  exit 10
fi
sleep 2
echo ''

make install
if [ $? -eq 0 ];then
  echo "success: zlib-${zlibver} install"
else
  echo "error: zlib-${zlibver} install failed"
  exit 10
fi
sleep 2
echo ''
}
zlib_install


##openssh安装
#sshver='9.8p1'
sshdir=/usr
openssh_install(){
echo "####install openssh####"
sleep 2

##设置openssl环境变量
export LD_LIBRARY_PATH="/usr/local/openssl${sslver}/lib":$LD_LIBRARY_PATH
grep "export LD_LIBRARY_PATH=/usr/local/openssl${sslver}/lib:\$LD_LIBRARY_PATH" /etc/profile &>/dev/null
if [ $? -ne 0 ];then
  echo "export LD_LIBRARY_PATH=/usr/local/openssl${sslver}/lib:\$LD_LIBRARY_PATH" >> /etc/profile
fi

source /etc/profile

ln -s /usr/local/openssl1.1.1b/lib/libcrypto.so.1.1 /usr/lib
ln -s /usr/local/openssl1.1.1b/lib/libcrypto.so.1.1 /usr/lib64

cd ${maindir}

if [ ! -e "openssh-${sshver}.tar.gz" ];then
  echo "error: openssh${sshver} not exist"
  exit 10
fi

if [ -d "openssh-${sshver}" ];then
  rm -rf openssh-${sshver}
fi


tar xvf openssh-${sshver}.tar.gz
if [ $? -eq 0 ];then
  echo "success: openssh-${sshver}.tar.gz package uncompress"
else
  echo "error: openssh-${sshver}.tar.gz package uncompress failed"
  exit 10
fi 
sleep 2

cd openssh-${sshver}

sed -i 's/^GSSAPIAuthentication/#GSSAPIAuthentication/g' /etc/ssh/sshd_config
sed -i 's/^GSSAPICleanupCredentials/#GSSAPICleanupCredentials/g' /etc/ssh/sshd_config
sed -i 's/^UsePAM/#UsePAM/g' /etc/ssh/sshd_config

./configure \
--prefix="${sshdir}" \
--sysconfdir=/etc/ssh \
--with-ssl-dir=${ssldir} \
--with-zlib=${zlibdir}
if [ $? -eq 0 ];then
  echo "success: openssh${sshver} config"
else
  echo "error: openssh${sshver} config"
  exit 10
fi
sleep 2

make
if [ $? -eq 0 ];then
  echo "success: openssh${sshver} make"
else
  echo "error: openssh${sshver} make failed"
  exit 10
fi
sleep 2
echo ''

##新版sshd文件检查
sshd_check_new=`sshd -t`
if [ -n "${sshd_check_new}" ];then
  echo "$sshd_check_new"
  echo "error: new version sshd check failed"
  exit 10
else
  echo "success: new version sshd check"
fi
sleep 2
echo ''

make install
if [ $? -eq 0 ];then
  echo "success: openssh${sshver} install"
else
  echo "error: openssh${sshver} install failed"
  exit 10
fi
sleep 2

cp contrib/ssh-copy-id /usr/bin
chmod +x /usr/bin/ssh-copy-id

echo ''
}
openssh_install

echo ''

##版本检测
ver_check=`ssh -V 2>&1 | awk '{print $1}' | sed 's/,//g' | awk -F'_' '{print $2}'`
if [ "${ver_check}" != "${sshver}" ];then
  echo "error: openssh version error: ${ver_check}"
  exit 10
fi
sleep 2

##重启sshd
service sshd restart
if [ $? -eq 0 ];then
  echo "success: sshd restart"
else
  echo "error: sshd restart failed"
  exit 10
fi

sleep 2

echo "####openssh update success: ${ver_check}####"

echo ''



