#!/bin/bash

## System Information
os="`cat /etc/system-release`"
osvercheck="`uname -r | cut -b 1`"
if [ $osvercheck -eq 5 ] ; then
        osver=9
elif [ $osvercheck -eq 4 ] ; then
        osver=8
elif [ $osvercheck -eq 3 ] ; then
        osver=7
elif [ $osvercheck -eq 2 ] ; then
        osver=6
else
        echo " Server OS kernel version is not certificated! "
        exit
fi

## date setting
DATE="`date +%Y_%m_%d`"


SSHVER=openssh-9.9p2
OPENSSH=$SSHVER.tar.gz
OSP=/usr/local

if [ -d /usr/local/openssh ]; then
	LINK=0  ### openssh link will not create
	if [ -L /usr/local/openssh ]; then
		LINK=1
	fi
else
	LINK=1  ### openssh link will be create
fi

## Check os version && Install openssh
        if [ $osver -eq 6 ]; then
                OLDVER=`ssh -V 2>&1 | cut -d , -f 1 | cut -d _ -f 2` &&
                OPENSSL=`grep -r ssl /etc/ld.so.conf* | grep -v "#" | awk -F: '{print $2}' | cut -d / -f 1-4` &&
                RDIR=/usr/local/src/wellc-src &&
                cd $RDIR &&
                if [ -d $RDIR/$SSHVER ]; then mv $RDIR/$SSHVER $RDIR/$SSHVER\_$DATE; fi &&
                tar zxvf $OPENSSH &&
                cd $SSHVER &&
                if [ -d $OSP/$SSHVER ]; then mv $OSP/$SSHVER $OSP/$SSHVER\_$DATE; fi &&
		if [ -z $OPENSSL ]; then
			./configure --prefix=$OSP/$SSHVER --with-pam
		else
			./configure --prefix=$OSP/$SSHVER --with-pam --with-ssl-dir=$OPENSSL
		fi &&
                make -j $(nproc) &&
                make install &&
                cp -rp /etc/ssh/ $RDIR/ssh-backup-$OLDVER &&
                cp /etc/pam.d/sshd $RDIR/pam-sshd-backup-$OLDVER &&
                cp /etc/init.d/sshd $RDIR/init-sshd-backup-$OLDVER &&
                if [ `rpm -qa | grep openssh-server | wc -l` -eq 1 ]; then
                        rpm -e --nodeps openssh openssh-clients openssh-server
                fi &&
                if [ $LINK -eq 1 ]; then
                        ln -Tfs $OSP/$SSHVER $OSP/openssh &&
                        ln -Tfs $OSP/openssh/bin/ssh /usr/bin/ssh &&
                        ln -Tfs $OSP/openssh/bin/scp /usr/bin/scp &&
                        ln -Tfs $OSP/openssh/bin/sftp /usr/bin/sftp &&
                        ln -Tfs $OSP/openssh/sbin/sshd /usr/sbin/sshd &&
                        ln -Tfs $OSP/openssh/bin/ssh-add /usr/bin/ssh-add &&
                        ln -Tfs $OSP/openssh/bin/ssh-agent /usr/bin/ssh-agent &&
                        ln -Tfs $OSP/openssh/bin/ssh-keygen /usr/bin/ssh-keygen &&
                        ln -Tfs $OSP/openssh/bin/ssh-keyscan /usr/bin/ssh-keyscan &&
                        rm -rf /etc/ssh &&
                        ln -Tfs $OSP/openssh/etc /etc/ssh
                elif [ $LINK -eq 0 ]; then
                        ln -Tfs $OSP/$SSHVER/bin/ssh /usr/bin/ssh &&
                        ln -Tfs $OSP/$SSHVER/bin/scp /usr/bin/scp &&
                        ln -Tfs $OSP/$SSHVER/bin/sftp /usr/bin/sftp &&
                        ln -Tfs $OSP/$SSHVER/sbin/sshd /usr/sbin/sshd &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-add /usr/bin/ssh-add &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-agent /usr/bin/ssh-agent &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-keygen /usr/bin/ssh-keygen &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-keyscan /usr/bin/ssh-keyscan &&
                        rm -rf /etc/ssh &&
                        ln -Tfs $OSP/$SSHVER/etc /etc/ssh
                fi &&
                cp $RDIR/pam-sshd-backup-$OLDVER /etc/pam.d/sshd &&
                c\p -fp $RDIR/ssh-backup-$OLDVER/* /etc/ssh/ &&
                cp -rp $RDIR/init-sshd-backup-$OLDVER /etc/init.d/sshd &&
                sed -i -e s/^GSSAPIAuthentication/\#GSSAPIAuthentication/g /etc/ssh/sshd_config &&
                sed -i -e s/^GSSAPICleanupCredentials/\#GSSAPICleanupCredentials/g /etc/ssh/sshd_config &&
                sed -i -e s/Subsystem/\#Subsystem/g /etc/ssh/sshd_config &&
                sed -i -e "`grep -n ^#Subsystem /etc/ssh/sshd_config | awk -F: '{print $1}'` i\Subsystem       sftp    $OSP/$SSHVER/libexec/sftp-server" /etc/ssh/sshd_config &&
                chown -R root.root $RDIR &&
                unset RDIR &&
                service sshd restart &&
                chkconfig sshd on &&
                service sshd status
        elif [ $osver -ge 7 ] && [ $osver -le 9 ] ; then
                OLDVER=`ssh -V 2>&1 | cut -d , -f 1 | cut -d _ -f 2` &&
                OPENSSL=`grep -r ssl /etc/ld.so.conf* | grep -v "#" | awk -F: '{print $2}' | cut -d / -f 1-4` &&
                RDIR=/usr/local/src/wellc-src &&
                cd $RDIR &&
                if [ -d $RDIR/$SSHVER ]; then mv $RDIR/$SSHVER $RDIR/$SSHVER\_$DATE; fi &&
                tar zxvf $OPENSSH &&
                cd $SSHVER &&
                if [ -d $OSP/$SSHVER ]; then mv $OSP/$SSHVER $OSP/$SSHVER\_$DATE; fi &&
		if [ -z $OPENSSL ]; then
			./configure --prefix=$OSP/$SSHVER --with-pam
		else
			./configure --prefix=$OSP/$SSHVER --with-pam --with-ssl-dir=$OPENSSL
		fi &&
                make -j $(nproc) &&
                make install &&
                cp -rp /etc/ssh/ $RDIR/ssh-backup-$OLDVER &&
                cp /etc/pam.d/sshd $RDIR/pam-sshd-backup-$OLDVER &&
                cp /usr/bin/ssh-copy-id $OSP/$SSHVER/bin/ssh-copy-id &&
                if [ `rpm -qa | grep openssh-server | wc -l` -eq 1 ]; then
                        rpm -e --nodeps openssh openssh-clients openssh-server
                fi &&
                if [ $LINK -eq 1 ]; then
                        ln -Tfs $OSP/$SSHVER $OSP/openssh &&
                        ln -Tfs $OSP/openssh/bin/ssh /usr/bin/ssh &&
                        ln -Tfs $OSP/openssh/bin/scp /usr/bin/scp &&
                        ln -Tfs $OSP/openssh/bin/sftp /usr/bin/sftp &&
                        ln -Tfs $OSP/openssh/sbin/sshd /usr/sbin/sshd &&
                        ln -Tfs $OSP/openssh/bin/ssh-add /usr/bin/ssh-add &&
                        ln -Tfs $OSP/openssh/bin/ssh-agent /usr/bin/ssh-agent &&
                        ln -Tfs $OSP/openssh/bin/ssh-keygen /usr/bin/ssh-keygen &&
                        ln -Tfs $OSP/openssh/bin/ssh-keyscan /usr/bin/ssh-keyscan &&
                        ln -Tfs $OSP/openssh/bin/ssh-copy-id /usr/bin/ssh-copy-id &&
                        rm -rf /etc/ssh &&
                        ln -Tfs $OSP/openssh/etc /etc/ssh
                elif [ $LINK -eq 0 ]; then
                        ln -Tfs $OSP/$SSHVER/bin/ssh /usr/bin/ssh &&
                        ln -Tfs $OSP/$SSHVER/bin/scp /usr/bin/scp &&
                        ln -Tfs $OSP/$SSHVER/bin/sftp /usr/bin/sftp &&
                        ln -Tfs $OSP/$SSHVER/sbin/sshd /usr/sbin/sshd &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-add /usr/bin/ssh-add &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-agent /usr/bin/ssh-agent &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-keygen /usr/bin/ssh-keygen &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-keyscan /usr/bin/ssh-keyscan &&
                        ln -Tfs $OSP/$SSHVER/bin/ssh-copy-id /usr/bin/ssh-copy-id &&
                        rm -rf /etc/ssh &&
                        ln -Tfs $OSP/$SSHVER/etc /etc/ssh
                fi &&
                cp $RDIR/pam-sshd-backup-$OLDVER /etc/pam.d/sshd &&
                c\p -fp $RDIR/ssh-backup-$OLDVER/sshd_config /etc/ssh/sshd_config &&
                echo "[Unit]
Description= SSH Server

[Service]
ExecStart=$OSP/$SSHVER/sbin/sshd -D \$OPTIONS
ExecReload=/bin/kill -HUP \$MAINPID
KillMode=process
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target" > /usr/lib/systemd/system/sshd.service &&
                chmod 644 /usr/lib/systemd/system/sshd.service &&
                sed -i -e s/^GSSAPIAuthentication/\#GSSAPIAuthentication/g /etc/ssh/sshd_config &&
                sed -i -e s/^GSSAPICleanupCredentials/\#GSSAPICleanupCredentials/g /etc/ssh/sshd_config &&
                sed -i -e s/Subsystem/\#Subsystem/g /etc/ssh/sshd_config &&
                sed -i -e "`grep -n ^#Subsystem /etc/ssh/sshd_config | awk -F: '{print $1}'` i\Subsystem       sftp    $OSP/$SSHVER/libexec/sftp-server" /etc/ssh/sshd_config &&
                chown -R root.root $RDIR &&
                unset RDIR &&
                systemctl daemon-reload &&
                systemctl restart sshd &&
                systemctl enable sshd &&
                systemctl status sshd

fi
