Subversion Repositories ALCASAR

Rev

Rev 2467 | Rev 2488 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

#!/bin/bash
# $Id: alcasar-uninstall.sh 2470 2017-12-27 18:46:44Z richard $

# alcasar-uninstall.sh
# by Franck BOUIJOUX, Pascal LEVANT and Richard REY
# This script is distributed under the Gnu General Public License (GPL)

# Désisntallation ou mise à jour d'ALCASAR
# Uninstall or update ALCASAR

SED="/bin/sed -i"

init ()
{
        echo -en "(3) : "
        rm -f /root/ALCASAR* && echo -n "1, " # The files in /usr/local/ will be removed at the end (still usefull here)
        [ -e /boot/grub2/user.cfg ] && rm -f /boot/grub2/user.cfg && echo -n "2, "
        [ -e /root/grub.default ] && mv -f /root/grub.default /etc/grub.d/10_linux && echo -n "3"
}

ACC ()
{
        echo -en "(11) : "
        [ -d /var/www/html ] && rm -rf /var/www/html && echo -n "1, "
        [ -d /etc/freeradius-web ] && rm -rf /etc/freeradius-webl && echo -n "2, "
        [ -e /etc/php.ini.default ] && mv -f /etc/php.ini.default /etc/php.ini && echo -n "3, "
        [ -e /etc/httpd/conf/httpd.conf.default ] && mv /etc/httpd/conf/httpd.conf.default /etc/httpd/conf/httpd.conf && echo -n "4, "
        [ -e /etc/httpd/conf/modules.d/00_base.conf.default ] && mv /etc/httpd/conf/modules.d/00_base.conf.default /etc/httpd/conf/modules.d/00_base.conf && echo -n "5, "
        [ -e /etc/httpd/conf/conf.d/ssl.conf.default ] && mv /etc/httpd/conf/conf.d/ssl.conf.default /etc/httpd/conf/conf.d/ssl.conf && echo -n "6, "
        [ -e /etc/httpd/conf/conf.d/multilang-errordoc.conf.default ] && mv /etc/httpd/conf/conf.d/multilang-errordoc.conf.default /etc/httpd/conf/conf.d/multilang-errordoc.conf && echo -n "7, "
        [ -e /usr/share/httpd/error/include/top.html.default ] && mv /usr/share/httpd/error/include/top.html.default /usr/share/httpd/error/include/top.html && echo -n "8, "
        [ -e /usr/share/httpd/error/include/bottom.html.default ] && mv /usr/share/httpd/error/include/bottom.html.default /usr/share/httpd/error/include/top.html && echo -n "9, "
        [ -d /usr/local/etc/digest ] && rm -rf /usr/local/etc/digest && echo -n "10, "
        [ -e /etc/httpd/conf/webapps.d/alcasar.conf ] && rm -f /etc/httpd/conf/webapps.d/alcasar.conf && echo -n "11"
}

CA ()
{
        echo -en "(5) : "
        [ -e /etc/pki/CA/alcasar-ca.crt ] && rm -f /etc/pki/CA/alcasar-ca.crt && echo -n "1, "
        [ -e /etc/pki/CA/private/alcasar-ca.key ] && rm -f /etc/pki/CA/private/alcasar-ca.key && echo -n "2, "
        [ -e /etc/pki/tls/certs/alcasar.crt ] && rm -f /etc/pki/tls/certs/alcasar.crt && echo -n "3, "
        [ -e /etc/pki/tls/private/alcasar.key ] && rm -f /etc/pki/tls/private/alcasar.key && echo -n "4, "
        [ -e /etc/httpd/conf/vhosts-ssl.default ] && FIC_VIRTUAL_SSL=`find /etc/httpd/conf -type f -name *default_ssl_vhost.conf` && mv /etc/httpd/conf/vhosts-ssl.default $FIC_VIRTUAL_SSL && echo -n "5"
}

time_server ()
{
        echo -en "(2) : "
        [ -e /etc/ntp/step-tickers.default ] && mv /etc/ntp/step-tickers.default /etc/ntp/step-tickers && echo -n "1, "
        [ -e /etc/ntp.conf.default ] && mv /etc/ntp.conf.default /etc/ntp.conf && echo -n "2"
}

init_db ()
{
        echo -en "(2) : "
        [ -e /etc/my.cnf.default ] && mv -f /etc/my.cnf.default /etc/my.cnf && echo -n "1, "
        [ -e /lib/systemd/system/mysqld.service.default ] && mv -f /lib/systemd/system/mysqld.service.default /lib/systemd/system/mysqld.service && echo -n "2"
        /usr/bin/systemctl daemon-reload
        rm -rf /var/lib/mysql
}

freeradius ()
{
        echo -en "(15) : "
        [ -e /etc/raddb/empty-radiusd-db.sql ] && rm -f /etc/raddb/empty-radiusd-db.sql && echo -n "1, "
        [ -e /etc/raddb/radiusd.conf.default ] && mv /etc/raddb/radiusd.conf.default /etc/raddb/radiusd.conf && echo -n "2, "
        [ -e /etc/raddb/sites-enabled/alcasar ] && rm /etc/raddb/sites-enabled/alcasar && echo -n "3, "
        [ -e /etc/raddb/sites-available/alcasar ] && rm /etc/raddb/sites-available/alcasar && echo -n "4, "
        [ -e /etc/raddb/sites-available/alcasar-with-ldap ] && rm /etc/raddb/sites-available/alcasar-with-ldap && echo -n "5, "
        [ -e /etc/raddb/clients.conf.default ] && mv /etc/raddb/clients.conf.default /etc/raddb/clients.conf && echo -n "6, "
        [ -e /etc/raddb/mods-enabled/ldap ] && rm /etc/raddb/mods-enabled/ldap && echo -n "7, " #Add here other mods
        [ -e /etc/raddb/mods-enabled/sql ] && rm /etc/raddb/mods-enabled/sql && echo -n "7bis, " #Add here other mods
        [ -e /etc/raddb/mods-available/ldap-alcasar ] && rm /etc/raddb/mods-available/ldap-alcasar && echo -n "8, "
        [ -e /etc/raddb/mods-available/sql.default ] && mv /etc/raddb/mods-available/sql.default /etc/raddb/mods-available/sql && echo -n "9, "
        [ -e /etc/raddb/mods-config/sql/main/mysql/queries.conf.default ] && mv /etc/raddb/mods-config/sql/main/mysql/queries.conf.default /etc/raddb/mods-config/sql/main/mysql/queries.conf && echo -n "10, "
        [ -e /etc/raddb/mods-available/sqlcounter.default ] && mv /etc/raddb/mods-available/sqlcounter.default /etc/raddb/mods-available/sqlcounter && echo -n "11, "
        [ -e /etc/raddb/mods-config/sql/counter/mysql/dailycounter.conf.default ] && mv /etc/raddb/mods-config/sql/counter/mysql/dailycounter.conf.default /etc/raddb/mods-config/sql/counter/mysql/dailycounter.conf && echo -n "12, "
        [ -e /etc/raddb/mods-config/sql/counter/mysql/monthlycounter.conf.default ] && mv /etc/raddb/mods-config/sql/counter/mysql/monthlycounter.conf.default /etc/raddb/mods-config/sql/counter/mysql/monthlycounter.conf && echo -n "13, "
        [ -e /etc/raddb/mods-config/sql/counter/mysql/noresetcounter.conf.default ] && mv /etc/raddb/mods-config/sql/counter/mysql/noresetcounter.conf.default /etc/raddb/mods-config/sql/counter/mysql/noresetcounter.conf && echo -n "14, "
        [ -e /lib/systemd/system/radiusd.service.default ] && mv /lib/systemd/system/radiusd.service.default /lib/systemd/system/radiusd.service && echo -n "15"
}

chilli ()
{
        echo -en "(4) : "
        [ -e /etc/init.d/chilli.default ] && mv /etc/init.d/chilli.default /etc/init.d/chilli && echo -n "1, "
        [ -e /usr/libexec/chilli ] && rm /usr/libexec/chilli && echo -n "2, "
        [ -e /etc/chilli.conf.default ] && mv /etc/chilli.conf.default /etc/chilli.conf && echo -n "3, "
        [ -e /lib/systemd/system/chilli.service ] && rm /lib/systemd/system/chilli.service && echo -n "4"
}

dansguardian ()
{
        echo -en "(8) : "
        [ -d /var/dansguardian ] && rm -rf /var/dansguardian && echo -n "1, "
        [ -e /etc/dansguardian/dansguardian.conf.default ] && mv /etc/dansguardian/dansguardian.conf.default /etc/dansguardian/dansguardian.conf && echo -n "2, "
        [ -e /etc/dansguardian/lists/bannedphraselist.default ] && mv /etc/dansguardian/lists/bannedphraselist.default /etc/dansguardian/lists/bannedphraselist && echo -n "3, "
        [ -e /etc/dansguardian/dansguardianf1.conf.default ] && mv /etc/dansguardian/dansguardianf1.conf.default /etc/dansguardian/dansguardianf1.conf && echo -n "4, "
        [ -e /etc/dansguardian/lists/bannedextensionlist.default ] && mv /etc/dansguardian/lists/bannedextensionlist.default /etc/dansguardian/lists/bannedextensionlist && echo -n "5, "
        [ -e /etc/dansguardian/lists/bannedmimetypelist.default ] && mv /etc/dansguardian/lists/bannedmimetypelist.default /etc/dansguardian/lists/bannedmimetypelist && echo -n "6, "
        [ -e /etc/dansguardian/lists/exceptioniplist.default ] && mv /etc/dansguardian/lists/exceptioniplist.default /etc/dansguardian/lists/exceptioniplist && echo -n "7, "
        [ -e /etc/dansguardian/lists/bannedsitelist.default ] && mv /etc/dansguardian/lists/bannedsitelist.default /etc/dansguardian/lists/bannedsitelist && echo -n "8"
}

antivirus ()
{
        echo -en "(5) : "
        if [ -e /etc/init.d/havp ]
        then
                [ -e /etc/havp/havp.config.default ] && mv /etc/havp/havp.config.default /etc/havp/havp.config && echo -n "1, "
                userdel -r havp 2>/dev/null && echo -n "2, "
                [ -e /etc/init.d/havp.default ] && mv /etc/init.d/havp.default /etc/init.d/havp && echo -n "3, "
                [ -e /lib/systemd/system/havp.service.default ] && mv /lib/systemd/system/havp.service.default /lib/systemd/system/havp.service && echo -n "4, "
                [ -e /etc/freshclam.conf.default ] && mv /etc/freshclam.conf.default /etc/freshclam.conf && echo -n "5"
        else    echo -n "already uninstalled"
        fi
}

tinyproxy ()
{
        echo -en "(2) : "
        if [ -e /etc/init.d/tinyproxy ]
        then
                [ -e /etc/tinyproxy/tinyproxy.conf.default ] && mv /etc/tinyproxy/tinyproxy.conf.default /etc/tinyproxy/tinyproxy.conf && echo -n "1, "
                userdel -r tinyproxy 2>/dev/null && echo -n "2"
        else    echo -n "already uninstalled"
        fi
}

ulogd ()
{
        echo -en "(6) : "
        i=0
        for log_type in traceability ssh ext-access
        do
                i=`expr $i + 1`
                [ -e /etc/ulogd-$log_type.conf ] && rm -f /etc/ulogd-$log_type.conf && echo -n "$i, "
                i=`expr $i + 1`
                [ -e /lib/systemd/system/ulogd-$log_type.service ] && rm -f /lib/systemd/system/ulogd-$log_type.service && echo -n "$i, "
        done
}

nfsen ()
{
        echo -en "(1) : "
        [ -e /lib/systemd/system/nfsen.service ] && rm -f /lib/systemd/system/nfsen.service && echo -n "1"
}

vnstat ()
{
        echo -en "(1) : "
        [ -e /etc/vnstat.conf.default ] && mv /etc/vnstat.conf.default /etc/vnstat.conf && echo -n "1"
}

dnsmasq ()
{
        echo -en "(6 if update ; 8 if uninstall) : "
        i=0
        for list in blacklist whitelist blackhole
        do
                i=`expr $i + 1`
                [ -e /etc/dnsmasq-$list.conf ] && rm /etc/dnsmasq-$list.conf && echo -n "$i, "
                i=`expr $i + 1`
                [ -e /lib/systemd/system/dnsmasq-$list.service ] && rm /lib/systemd/system/dnsmasq-$list.service && echo -n "$i, "
        done
        if [ $mode == "full" ] # don't remove dnsmasq in update mode (DHCP + DNS !)
        then
                if [ -e /lib/systemd/system/dnsmasq.service ]
                then
                        [ -e /etc/dnsmasq.conf.default ] && mv /etc/dnsmasq.conf.default /etc/dnsmasq.conf && echo -n "7, "
                        [ -e /lib/systemd/system/dnsmasq.service.default ] && mv /lib/systemd/system/dnsmasq.service.default /lib/systemd/system/dnsmasq.service && echo -n "8"
                fi
        fi
}

cron ()
{
        echo -en "(11) : "
        i=1
        for cron in `ls /etc/cron.d/alcasar-* 2>/dev/null`
        do
                rm $cron && echo -n "$i, "
                i=`expr $i + 1`
        done
        [ -e /etc/crontab.default ] && mv /etc/crontab.default /etc/crontab && echo -n "10, "
        [ -e /etc/anacrontab.default ] && mv /etc/anacrontab.default /etc/anacrontab && echo -n "11"
}

fail2ban ()
{
        echo -en "(8) : "
        [ -e /etc/fail2ban/fail2ban.conf.default ] && mv /etc/fail2ban/fail2ban.conf.default /etc/fail2ban/fail2ban.conf && echo -n "1, "
        [ -e /etc/fail2ban/jail.conf.default ] && mv /etc/fail2ban/jail.conf.default /etc/fail2ban/jail.conf && echo -n "2, "
        [ -e /etc/fail2ban/action.d/iptables-allports.conf.default ] && mv /etc/fail2ban/action.d/iptables-allports.conf.default /etc/fail2ban/action.d/iptables-allports.conf && echo -n "3, "
        i=3
        for filter in `ls /etc/fail2ban/filter.d/alcasar_* 2>/dev/null`
        do
                i=`expr $i + 1`
                rm $filter && echo -n "$i, "
        done
        [ -e /lib/systemd/system/fail2ban.service.default ] && mv /lib/systemd/system/fail2ban.service.default /lib/systemd/system/fail2ban.service && echo -n "8"
}

gammu_smsd ()
{
        echo -en "(3) : "
        [ -e /etc/gammu_smsd_conf ] && rm -f /etc/gammu_smsd_conf && echo -n "1, "
        [ -e /lib/udev/rules.d/66-huawei.rules ] && rm -f /lib/udev/rules.d/66-huawei.rules && echo -n "2, "
        [ -e /var/log/gammu-smsd ] && rm -rf /var/log/gammu-smsd && echo -n "3"
}

msec ()
{
        echo -en "(2) : "
        if [ -e /etc/security/msec/security.conf ]
        then
                [ -e /etc/security/msec/security.conf.default ] && mv /etc/security/msec/security.conf.default /etc/security/msec/security.conf && echo -n "1, "
                [ -e /etc/security/msec/perm.local ] && rm -f /etc/security/msec/perm.local && echo -n "2"
        else    echo -n "uninstalled"
        fi
}

letsencrypt ()
{
        echo -en "(3) : "
        [ -e /usr/local/etc/letsencrypt ] && rm -rf /usr/local/etc/letsencrypt && echo -n "1, "
        [ -e /opt/acme.sh/acme.sh ] && /opt/acme.sh/acme.sh --uninstall --nocron 1>/dev/null && echo -n "2, "
        [ -e /opt/acme.sh ] && rm -rf /opt/acme.sh && echo -n "3"
}

post_install ()
{
echo -en "(7) : "
        [ -e /etc/mageia-release.default ] && mv -f /etc/mageia-release.default /etc/mageia-release && echo -n "1, "
        [ -e /etc/ssh/alcasar-banner-ssh ] && rm -f /etc/ssh/alcasar-banner-ssh && echo -n "2, "
        [ -e /etc/ssh/sshd_config.default ] && mv -f /etc/ssh/sshd_config.default /etc/ssh/sshd_config && echo -n "3, "
        [ -e /etc/bashrc.default ] && mv -f /etc/bashrc.default /etc/bashrc && echo -n "4, "
        [ -e /etc/sudoers.default ] && mv -f /etc/sudoers.default /etc/sudoers && echo -n "5, "
        [ -e /lib/systemd/system/alcasar-load_balancing.service ] && rm -f /lib/systemd/system/alcasar-load_balancing.service && echo -n "6, "
        [ -e /etc/default/grub.default ] && mv -f /etc/default/grub.default /etc/default/grub && echo -n "7"
}


usage="Usage: alcasar-uninstall.sh {-update or --update} | {-full or --full}"

nb_args=$#
args=$1
if [ $nb_args -eq 0 ]
then
        nb_args=1
        args="-h"
fi
case $args in
        -\? | -h* | --h*)
                echo "$usage"
                exit 0
                ;;
        --update | -update)
                mode="update"
                ;;
        --full | -full)
                mode="full"
                ;;
        *)
                echo "Argument inconnu :$1";
                echo "$usage"
                exit 1
                ;;
esac
clear
if [ $mode == "full" ]
then
        echo "----------------------------------------------------------------------------"
        echo "**                     Uninstall/Désinstallation d'ALCASAR               **"
        echo "----------------------------------------------------------------------------"
        services="alcasar-load_balancing vnstat havp freshclam ntpd httpd radiusd mysqld dnsmasq-blacklist dnsmasq-whitelist dnsmasq-blackhole tinyproxy nfsen fail2ban iptables ulogd-ext-access ulogd-ssh ulogd-traceability dansguardian dnsmasq sshd chilli"
        /usr/local/bin/alcasar-logout.sh all # logout everybody
else
        echo "--------------------------------------------------------------------------"
        echo "**                     update/mise à jour d'ALCASAR                      **"
        echo "--------------------------------------------------------------------------"
        # dnsmasq & sshd should stay on to allow remote update
        services="alcasar-load_balancing vnstat havp freshclam ntpd httpd radiusd mysqld dnsmasq-blacklist dnsmasq-whitelist dnsmasq-blackhole tinyproxy nfsen fail2ban ulogd-ext-access ulogd-ssh ulogd-traceability dansguardian chilli"
        /usr/local/bin/alcasar-bypass.sh -on # to allow remote update
fi
echo "Stopping service : "
/usr/local/bin/alcasar-sms.sh --stop
for i in $services
do
        if [ -e /lib/systemd/system/$i.service ]
        then
                /usr/bin/systemctl disable $i.service
                /usr/bin/systemctl stop $i.service 1>/dev/null
                sleep 1
        else
                echo "The service $i.service doesn't exist !"
        fi
done
echo "Check the service clearing"
for i in $services
do
        if [ `systemctl is-active $i.service` == "active" ]
        then
                echo "The service '$i' need to be killed"
                /usr/bin/systemctl stop $i.service
                killall $i
        fi
done
if [ $mode == "update" ] # reload sshd in case of remote update
then
        /usr/bin/systemctl reload sshd
fi
echo "Reset ALCASAR main functions : "
for func in init ACC CA time_server init_db freeradius chilli dansguardian antivirus tinyproxy ulogd nfsen vnstat dnsmasq cron fail2ban gammu_smsd msec letsencrypt post_install
do
        echo -en "\n- $func "
        $func
        sleep 1
        if [ $DEBUG_ALCASAR == "on" ]
        then
                echo -n " *** 'debug' : end of cleaning '$func' *** "
                read a
        fi
done
if [ $mode == "full" ]
then
        echo -en "\n- network(9) : "
        hostnamectl set-hostname localhost.localdomain
        chmod a-x /etc/sysconfig/network-scripts/default-*
        i=0
        for nic in `ls /etc/sysconfig/network-scripts/default-*|cut -d'-' -f4`
        do
                i=`expr $i + 1`
                /sbin/ifdown $nic
                [ -e /etc/sysconfig/network-scripts/default-ifcfg-$nic ] && mv -f /etc/sysconfig/network-scripts/default-ifcfg-$nic /etc/sysconfig/network-scripts/ifcfg-$nic && echo -n "$i ($nic), "
        done
        [ -e /etc/sysconfig/network.default ] && mv -f /etc/sysconfig/network.default /etc/sysconfig/network && echo -n "3, "
        [ -e /etc/hosts.default ] && mv -f /etc/hosts.default /etc/hosts && echo -n "4, "
        [ -e /etc/hosts.allow.default ] && mv -f /etc/hosts.allow.default /etc/hosts.allow && echo -n "5, "
        [ -e /etc/hosts.deny.default ] && mv -f /etc/hosts.deny.default /etc/hosts.deny && echo -n "6, "
        [ -e /etc/modprobe.preload.default ] && mv -f /etc/modprobe.preload.default /etc/modprobe.preload && echo -n "7, "
        [ -e /lib/systemd/system/iptables.service.default ] && mv -f /lib/systemd/system/iptables.service.default /lib/systemd/system/iptables.service && echo -n "8, "
        [ -e /usr/libexec/iptables.init.default ] && mv -f /usr/libexec/iptables.init.default /usr/libexec/iptables.init && echo -n "9"
        /usr/bin/systemctl restart network
        sleep 1
fi

# Cleaning (remove all "old" alcasar scripts)
echo -en "\n- End of cleaning ...\n"
for rm_fic in /usr/local/bin /usr/local/etc /usr/local/sbin
do
        rm -rf $rm_fic/alcasar*
done
/usr/bin/update-grub2

if [ $mode == "full" ]
then
        echo -n "Waiting for Network to be up again : "
        i=0
        while [ $i -lt 10 ] # We wait 10 seconds max
        do
                echo -n "."
                DNS1=`grep ^nameserver /etc/resolv.conf|awk -F" " '{print $2}'|head -n 1`
                if [ "$DNS1" != "" ] && [ "$DNS1" != "127.0.0.1" ]
                then
                        i=9
                        echo -n "ok"
                fi
                i=`expr $i + 1`
                sleep 1
        done
fi
echo