Skip to content
Snippets Groups Projects
freedombone-logging 10 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# Turn logging on or off

# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# it under the terms of the GNU Affero General Public License as published by
Bob Mottram's avatar
Bob Mottram committed
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Bob Mottram's avatar
Bob Mottram committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
PROJECT_NAME='freedombone'

Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAIN=${PROJECT_NAME}-logging
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

Bob Mottram's avatar
Bob Mottram committed
WEBSERVER_LOG_LEVEL='warn'

REMOVE_FILES_COMMAND='rm -rf'

source /usr/local/bin/${PROJECT_NAME}-vars

Bob Mottram's avatar
Bob Mottram committed
UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
for f in $UTILS_FILES
do
Bob Mottram's avatar
Bob Mottram committed
    source "$f"
Bob Mottram's avatar
Bob Mottram committed
APP_FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
Bob Mottram's avatar
Bob Mottram committed
for f in $APP_FILES
do
Bob Mottram's avatar
Bob Mottram committed
    source "$f"
Bob Mottram's avatar
Bob Mottram committed
done

APPS_AVAILABLE=()

function logging_get_app_names {
Bob Mottram's avatar
Bob Mottram committed
    FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
Bob Mottram's avatar
Bob Mottram committed

    for filename in $FILES
    do
        app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
Bob Mottram's avatar
Bob Mottram committed
        if grep -q "logging_on_" "${filename}"; then
            if grep -q "logging_off_" "${filename}"; then
Bob Mottram's avatar
Bob Mottram committed
                APPS_AVAILABLE+=("${app_name}")
            fi
        fi
    done
}

function turn_logging_on {
    logging_get_app_names

Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2068
    for a in ${APPS_AVAILABLE[@]}
Bob Mottram's avatar
Bob Mottram committed
    do
        echo $"Turning on logging for ${a}"
Bob Mottram's avatar
Bob Mottram committed
        "logging_on_${a}"
Bob Mottram's avatar
Bob Mottram committed
    done
}

function turn_logging_off {
    logging_get_app_names

Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2068
    for a in ${APPS_AVAILABLE[@]}
Bob Mottram's avatar
Bob Mottram committed
    do
        echo $"Turning off logging for ${a}"
Bob Mottram's avatar
Bob Mottram committed
        "logging_off_${a}"
Bob Mottram's avatar
Bob Mottram committed
function turn_on_rsys_logging {
Bob Mottram's avatar
Bob Mottram committed
    save_rsys_header

    { echo 'auth,authpriv.*         /var/log/auth.log';
      echo '*.*;auth,authpriv.none -/var/log/syslog';
      echo 'cron.*                  /var/log/cron.log';
      echo 'daemon.*               -/var/log/daemon.log';
      echo 'kern.*                 -/var/log/kern.log';
      echo 'lpr.*                  -/var/log/lpr.log';
      echo 'mail.*                 -/var/log/mail.log';
      echo 'user.*                 -/var/log/user.log';
      echo '';
      echo 'mail.info              -/var/log/mail.info';
      echo 'mail.warn              -/var/log/mail.warn';
      echo 'mail.err                /var/log/mail.err';
      echo '';
      echo "*.=debug;\\";
      echo "        auth,authpriv.none;\\";
      echo '        news.none;mail.none -/var/log/debug';
      echo "*.=info;*.=notice;*.=warn;\\";
      echo "        auth,authpriv.none;\\";
      echo "        cron,daemon.none;\\";
      echo '        mail,news.none      -/var/log/messages';
      echo '';
      echo '*.emerg                         :omusrmsg:*'; } >> /etc/rsyslog.conf
Bob Mottram's avatar
Bob Mottram committed
}

Bob Mottram's avatar
Bob Mottram committed
if [ ! "$1" ]; then
    exit 1
fi

if [[ "$1" == "on" || "$1" == "On" || "$1" == "ON" ]]; then
Bob Mottram's avatar
Bob Mottram committed
    turn_logging_on

    if [ -f /var/lib/turn/turnserver.conf ]; then
        sed -i 's|log-file=.*|log-file=/var/lib/turn/turnserver.log|g' /var/lib/turn/turnserver.conf
    fi

Bob Mottram's avatar
Bob Mottram committed
    if [ -d /etc/tor ]; then
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -d /var/log/tor ]; then
            mkdir /var/log/tor
            chown -R debian-tor:adm /var/log/tor
        fi
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -f /var/log/tor/notices.log ]; then
            touch /var/log/tor/notices.log
Bob Mottram's avatar
Bob Mottram committed
            chown debian-tor:adm /var/log/tor/notices.log
Bob Mottram's avatar
Bob Mottram committed
        fi
        echo 'Log notice file /var/log/tor/notices.log' > /etc/torrc.d/logging
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ -f "/etc/php/${PHP_VERSION}/fpm/php-fpm.conf" ]; then
        sed -i 's|error_log =.*|error_log = /var/log/php-fpm.log|g' "/etc/php/${PHP_VERSION}/fpm/php-fpm.conf"
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -d /etc/nginx ]; then
        if [ ! -d /var/log/nginx ]; then
            mkdir /var/log/nginx
        fi
        for filename in /etc/nginx/sites-available/* ; do
            filename_domain=$(echo "$filename" | awk -F '/' '{print $5}')
Bob Mottram's avatar
Bob Mottram committed
            sed -i "s|access_log.*|access_log /var/log/nginx/$filename_domain.access.log;|g" "$filename"
            sed -i "s|error_log.*|error_log /var/log/nginx/$filename_domain.err.log $WEBSERVER_LOG_LEVEL;|g" "$filename"
Bob Mottram's avatar
Bob Mottram committed
        done
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|access_log.*|access_log /var/log/nginx/access.log;|g' /etc/nginx/nginx.conf
        sed -i 's|error_log.*|error_log /var/log/nginx/error.log;|g' /etc/nginx/nginx.conf
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -f /etc/init.d/spamassassin ]; then
Bob Mottram's avatar
Bob Mottram committed
        sed -i "s|DOPTIONS=\"-s null -d --pidfile=\$PIDFILE\"|DOPTIONS=\"-d --pidfile=\$PIDFILE\"|g" /etc/init.d/spamassassin
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -d /etc/exim4 ]; then
        if [ ! -d /var/log/exim4 ]; then
            mkdir /var/log/exim4
        fi
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = +tls_peerdn|g' /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = +tls_peerdn|g' /etc/exim4/exim4.conf.template
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|log_selector =.*|log_selector = MAIN_LOG_SELECTOR|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
    fi
    if [ -f /etc/dovecot/dovecot.conf ]; then
        sed -i 's|log_path =.*|log_path = /var/log/dovecot.log|g' /etc/dovecot/dovecot.conf
        sed -i 's|info_log_path =.*|info_log_path = /var/log/dovecot-info.log|g' /etc/dovecot/dovecot.conf
        sed -i 's|debug_log_path =.*|debug_log_path = /var/log/dovecot-debug.log|g' /etc/dovecot/dovecot.conf
    fi
    if [ -d /etc/mysql ]; then
        if [ ! -d /var/log/mysql ]; then
            mkdir /var/log/mysql
        fi
Bob Mottram's avatar
Bob Mottram committed
        if [ -f /etc/mysql/my.cnf ]; then
            sed -i 's|log_error =.*|log_error = /var/log/mysql/error.log|g' /etc/mysql/my.cnf
        fi
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    turn_on_rsys_logging
    turn_on_postgresql_logging
Bob Mottram's avatar
Bob Mottram committed
else
Bob Mottram's avatar
Bob Mottram committed
    turn_logging_off
    if [ -f /var/lib/turn/turnserver.conf ]; then
        sed -i 's|log-file=.*|log-file=/dev/null|g' /var/lib/turn/turnserver.conf
    fi

    if [ -d /etc/torrc.d ]; then
        echo 'Log notice file /var/log/tor/notices.log' > /etc/torrc.d/logging
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -d /var/log/radicale ]; then
        $REMOVE_FILES_COMMAND /var/log/radicale/*
Bob Mottram's avatar
Bob Mottram committed
        rm -rf /var/log/radicale
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ -f "/etc/php/${PHP_VERSION}/fpm/php-fpm.conf" ]; then
        sed -i 's|error_log =.*|error_log = /dev/null|g' "/etc/php/${PHP_VERSION}/fpm/php-fpm.conf"
        $REMOVE_FILES_COMMAND /var/log/php-fpm.*
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -d /etc/nginx ]; then
        for filename in /etc/nginx/sites-available/* ; do
Bob Mottram's avatar
Bob Mottram committed
            sed -i 's|access_log.*|access_log /dev/null;|g' "$filename"
            sed -i 's|warn_log.*|warn_log /dev/null;|g' "$filename"
            sed -i 's|error_log.*|error_log /dev/null;|g' "$filename"
Bob Mottram's avatar
Bob Mottram committed
        done
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|access_log.*|access_log /dev/null;|g' /etc/nginx/nginx.conf
        sed -i 's|error_log.*|error_log /dev/null;|g' /etc/nginx/nginx.conf
        $REMOVE_FILES_COMMAND /var/log/nginx/*
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -f /etc/init.d/spamassassin ]; then
Bob Mottram's avatar
Bob Mottram committed
        sed -i "s|DOPTIONS=\"-d --pidfile=\$PIDFILE\"|DOPTIONS=\"-s null -d --pidfile=\$PIDFILE\"|g" /etc/init.d/spamassassin
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -d /etc/exim4 ]; then
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = -all|g' /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|MAIN_LOG_SELECTOR = .*|MAIN_LOG_SELECTOR = -all|g' /etc/exim4/exim4.conf.template
Bob Mottram's avatar
Bob Mottram committed
        sed -i 's|log_selector =.*|log_selector = -all|g' /etc/exim4/conf.d/main/90_exim4-config_log_selector
        $REMOVE_FILES_COMMAND /var/log/exim4/*
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -f /etc/dovecot/dovecot.conf ]; then
        sed -i 's|log_path =.*|log_path = /dev/null|g' /etc/dovecot/dovecot.conf
        sed -i 's|info_log_path =.*|info_log_path = /dev/null|g' /etc/dovecot/dovecot.conf
        sed -i 's|debug_log_path =.*|debug_log_path = /dev/null|g' /etc/dovecot/dovecot.conf
        $REMOVE_FILES_COMMAND /var/log/mail.*
        $REMOVE_FILES_COMMAND /var/log/dovecot*
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ -d /etc/mysql ]; then
        if [ -d /var/log/mysql ]; then
            $REMOVE_FILES_COMMAND /var/log/mysql/*
Bob Mottram's avatar
Bob Mottram committed
        fi
        if [ -f /var/log/mysql.err ]; then
            $REMOVE_FILES_COMMAND /var/log/mysql.err
Bob Mottram's avatar
Bob Mottram committed
        fi
        if [ -f /var/log/mysql.log ]; then
            $REMOVE_FILES_COMMAND /var/log/mysql.log
Bob Mottram's avatar
Bob Mottram committed
        fi
Bob Mottram's avatar
Bob Mottram committed
        if [ -f /etc/mysql/my.cnf ]; then
            sed -i 's|log_error =.*|log_error = /dev/null|g' /etc/mysql/my.cnf
        fi
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    turn_off_rsys_logging
    turn_off_postgresql_logging
Bob Mottram's avatar
Bob Mottram committed
fi

if [ -d /etc/exim4 ]; then
    update-exim4.conf.template -r
    update-exim4.conf
    dpkg-reconfigure --frontend noninteractive exim4-config
fi

if [[ "$2" == "--reboot"* || "$2" == "--restart"* ]]; then
    # if we are rebooting anyway then there is no need to
    # restart the daemons
Bob Mottram's avatar
Bob Mottram committed
    if [ -f /tmp/.upgrading ]; then
        rm /tmp/.upgrading
    fi
    exit 0
fi

if [ -d /etc/exim4 ]; then
    systemctl restart exim4
fi
Bob Mottram's avatar
Bob Mottram committed
systemctl restart syslog
Bob Mottram's avatar
Bob Mottram committed
if [ -d /etc/tor ]; then
    if [[ "$2" != "--onion" ]]; then
        systemctl restart tor
    fi
Bob Mottram's avatar
Bob Mottram committed
fi
Bob Mottram's avatar
Bob Mottram committed
if [ -f /etc/init.d/spamassassin ]; then
Bob Mottram's avatar
Bob Mottram committed
    systemctl restart spamassassin
Bob Mottram's avatar
Bob Mottram committed
fi
if [ -d /etc/ices2 ]; then
    systemctl restart icecast2
fi
Bob Mottram's avatar
Bob Mottram committed
if [ -d /etc/dovecot ]; then
    systemctl restart dovecot
fi
if [ -f /etc/mumble-server.ini ]; then
    systemctl restart mumble-server
fi
if [ -d /var/www/radicale ]; then
    systemctl restart radicale
Bob Mottram's avatar
Bob Mottram committed
fi
Bob Mottram's avatar
Bob Mottram committed
if [ -d /etc/matrix ]; then
    systemctl restart matrix
fi
if [ -d /etc/postgresql ]; then
    systemctl restart postgresql
fi
if [ -f /var/lib/turn/turnserver.conf ]; then
Bob Mottram's avatar
Bob Mottram committed
    systemctl restart turn
Bob Mottram's avatar
Bob Mottram committed

fuser -k 80/tcp
fuser -k 443/tcp
# shellcheck disable=SC2086
systemctl restart php${PHP_VERSION}-fpm
systemctl restart nginx

Bob Mottram's avatar
Bob Mottram committed
exit 0