GIF89a; Mini Shell

Mini Shell

Direktori : /etc/rc.d/init.d/
Upload File :
Current File : //etc/rc.d/init.d/cpanel2onapp

#!/bin/bash
#           
# cpanel2onapp        Script to preconfigure cPanel
#   
# chkconfig: - 70 14
# description: Script to preconfigure cPanel
        
# Source function library.
. /etc/rc.d/init.d/functions
# Put me in /etc/rc.d/rc3.d to start just after MySQL service and before the cPanel's services


start()
{
    logger "$0 - START"

    # Generate a better resolv.conf
    NS1='8.8.8.8'
    NS2='8.8.4.4'
    resolvconf='/etc/resolv.conf'

    resolversnum=`cat $resolvconf | grep '^nameserver.*$' |wc -l 2>/dev/null`
    if [ $resolversnum -lt 2 ]; then
        logger "$0 - Configuring nameservers with $NS1 and or $NS2 ..."
        if grep $NS1 $resolvconf >/dev/null 2>&1; then
            logger "$0 - $NS1 found."
        else 
            echo "nameserver $NS1" >> $resolvconf
        fi

        if grep $NS2 $resolvconf >/dev/null 2>&1; then
            logger "$0 - $NS2 found."
        else 
            echo "nameserver $NS2" >> $resolvconf
        fi
    fi

    # Get the IP and hostname
    IP=
    HOSTNAME=
    IP=`ifconfig eth0 2>/dev/null | grep "inet " 2>/dev/null | awk '{print $2}' 2>/dev/null`
    HOSTNAME=`hostname 2>/dev/null`

    if [ "x${IP}" != "x" -a "x${HOSTNAME}" != "x" ]; then

        # Generate the cPanel www config
        logger "$0 - Create /etc/wwwacct.conf and configure it with ADDR: $IP; HOST: $HOSTNAME"
        cat << ! > /etc/wwwacct.conf
HOST $HOSTNAME
HOMEDIR /home
ETHDEV 
NS ns1.vm
NS2 ns2.vm
HOMEMATCH home
NSTTL 86400
NS4 
TTL 14400
ADDR $IP
DEFMOD x3
SCRIPTALIAS y
CONTACTPAGER 
MINUID 500
NS3 
CONTACTEMAIL 
LOGSTYLE combined
DEFWEBMAILTHEME x3
!

        # Set primary IP address
        logger "$0 - Create /var/cpanel/mainip"
        echo $IP > /var/cpanel/mainip

        # Replace old IP (set up during the cPanel install) with current primary one
        logger "$0 - Set the IP for 'VirtualHost' in all found httpd.conf*"
        find / -name httpd.conf\* -exec sed -i "s/<VirtualHost [0-9\.]\+:/<VirtualHost $IP:/g; s/RewriteCond %{HTTP_HOST} .\+$/RewriteCond %{HTTP_HOST} \!\^$HOSTNAME$/g; s/ServerName .\+$/ServerName $HOSTNAME/g; s/ServerAdmin 
.\+$/ServerAdmin root@$HOSTNAME/g" {} \;

        # Fix/create /etc/mail_reverse_dns with correct primary IP address and host name
        logger "$0 - Create /etc/mail_reverse_dns"
        echo "$IP: $HOSTNAME" > /etc/mail_reverse_dns

        # Fix/create /etc/localdomains with correct host name
        logger "$0 - Create /etc/localdomains"
        echo "$HOSTNAME" > /etc/localdomains

        # Fix /var/cpanel/userdata/nobody/main with correct host name
        logger "$0 - Fix /var/cpanel/userdata/nobody/main with correct host name"
        sed -i "s/^main_domain:[[:space:]]*.*\$/main_domain: $HOSTNAME/g" /var/cpanel/userdata/nobody/main

        # Fix /var/cpanel/conf/apache/main with correct host name
        logger "$0 - Fix /var/cpanel/conf/apache/main with correct host name"
        sed -i "s/\(\"*serveradmin\"*: root@\).\+$/\1$HOSTNAME/g; s/\(\"*servername\"*: \).\+$/\1$HOSTNAME/g;" /var/cpanel/conf/apache/main
    else
        logger "$0 - Undefined IP: '$IP' or hostname: '$HOSTNAME'"
    fi

    # Tidy up
    > /var/log/wtmp
    > /root/.bash_history

    # Reset mysql root user password
    if [ -e /root/.my.cnf ]; then
        passwd=
        passwd=`grep 'password=' /root/.my.cnf 2>/dev/null | sed 's/.*password="\(.\+\)".*/\1/g' 2>/dev/null`
        new_passwd=
        #new_passwd=`tr -dc 'a-zA-Z0-9!#$%^&()' < /dev/urandom 2>/dev/null | fold -w 10 2>/dev/null | sed 5q 2>/dev/null | head -1 2>/dev/null`
        new_passwd=`openssl rand -base64 10 2>/dev/null`

        if [ "x${passwd}" != "x" -a "x${new_passwd}" != "x"  ]; then
            logger "$0 - Changing MySQL root user password ..."
            sh -c "mysql mysql --password='${passwd}' -e \"UPDATE user SET Password = PASSWORD('${new_passwd}') WHERE User = 'root'; FLUSH PRIVILEGES;\""
            if [ $? -eq 0 ]; then
                logger "$0 - MySQL root user password has been changed."
                echo -e "[client]\npassword=\"${new_passwd}\"\nuser=root" > /root/.my.cnf
            fi
        fi
    fi

    # Don't run again
    systemctl disable cpanel2onapp

    logger "$0 - FINISHED"
}
stop()
{

    true;

}

# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        *)
                echo $"Usage: $0 {start|stop}"
                exit 1
esac

./BlackJoker Mini Shell 1.0