#!/bin/bash

if command -v gcc >/dev/null 2>&1; then
    work_path=$(dirname $(readlink -f $0))
    echo "zlib编译安装..."
    cd $work_path
    zlib_tar="zlib-1.2.13.tar.gz"
    tar zxvf $zlib_tar
    zlib="/root/update_latest/zlib-1.2.13"
    cd $zlib
    ./configure --prefix=/usr
    make
    # rpm -e --nodeps zlib
    make install
    echo '/usr/lib' >> /etc/ld.so.conf
    ldconfig
    echo "zlib安装完成..."
    echo "openssl编译安装..."
    cd $work_path
    openssl_tar="openssl-1.1.1t.tar.gz"
    tar zxvf $openssl_tar
    openssl="/root/update_latest/openssl-1.1.1t"
    cd $openssl
    cp -r /etc/pki/ca-trust/extracted/openssl /etc/pki/ca-trust/extracted/openssl.old  
    cp -r /usr/bin/openssl /usr/bin/openssl.old
    cp -r /usr/lib64/openssl /usr/lib64/openssl.old
    cp /usr/lib64/libcrypto.so.10 /usr/lib64/libcrypto.so.10.old
    cp /usr/lib64/libssl.so.10 /usr/lib64/libssl.so.10.old
    echo "openssl备份完成..."
    ./config --prefix=/usr -d shared
    make
    # rpm -qa |grep openssl|xargs -i rpm -e --nodeps {}
    make install
    echo "openssl安装完成..."
    echo "openssh编译安装..."
    cd $work_path
    openssh_tar="openssh-9.3p1.tar.gz"
    tar zxvf $openssh_tar
    openssh="/root/update_latest/openssh-9.3p1"
    cd $openssh
    config=`ls /etc/ssh |grep sshd_config |awk NR==1`
    if [ "$config" = "sshd_config" ]; then
    cp -a /etc/ssh/sshd_config /etc/sshd_config.bak
    echo "sshd_config文件备份完成 /etc/sshd_config.bak"
    else
    echo "没有找到文件,直接安装..."
    fi
    install  -v -m700 -d /var/lib/sshd &&
    chown    -v root:sys /var/lib/sshd &&
    groupadd -g 50 sshd        &&
    useradd  -c 'sshd PrivSep' \
             -d /var/lib/sshd  \
             -g sshd           \
             -s /bin/false     \
             -u 50 sshd
    ./configure --prefix=/usr                     \
                --sysconfdir=/etc/ssh             \
                --with-md5-passwords              \
                --without-openssl-header-check    \
                --with-privsep-path=/var/lib/sshd &&
    make
    # rpm -qa |grep openssh|xargs -i rpm -e --nodeps {}
    make install &&
    install -v -m755    contrib/ssh-copy-id /usr/bin     &&
    install -v -m644    contrib/ssh-copy-id.1 \
                        /usr/share/man/man1              &&
    install -v -m755 -d /usr/share/doc/openssh-9.3p1     &&
    install -v -m644    INSTALL LICENCE OVERVIEW README* \
                        /usr/share/doc/openssh-9.3p1
    echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
    echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
    echo 'X11Forwarding yes' >> /etc/ssh/sshd_config
    echo 'KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group1-sha1,curve25519-sha256@libssh.org' >> /etc/ssh/sshd_config
    chmod 600 /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key
    cp -n contrib/redhat/sshd.init /etc/init.d/sshd
    echo "openssh安装完成..."
    chmod +x /etc/init.d/sshd
    chkconfig  --add  sshd
    chkconfig  sshd  on
    chkconfig  --list  sshd
    #service sshd start
    #systemctl restart sshd
    service sshd restart
    #systemctl enable sshd
    ssh -V
    uname -a
    netstat -tulnp | grep ssh
else
    echo "GCC is not installed."
fi
