2022-10-17 08:40:09 +00:00
|
|
|
#!/bin/bash
|
2021-02-23 19:03:01 +00:00
|
|
|
|
2023-02-26 10:42:14 +00:00
|
|
|
declare -a FUNCS_SETUP
|
|
|
|
|
|
|
|
function _register_setup_function
|
|
|
|
{
|
|
|
|
FUNCS_SETUP+=("${1}")
|
|
|
|
_log 'trace' "${1}() registered"
|
|
|
|
}
|
|
|
|
|
2022-05-15 21:37:21 +00:00
|
|
|
function _setup
|
2021-02-23 19:03:01 +00:00
|
|
|
{
|
2023-02-27 22:37:35 +00:00
|
|
|
# requires `shopt -s globstar` because of `**` which in
|
|
|
|
# turn is required as we're decending through directories
|
|
|
|
for FILE in /usr/local/bin/setup.d/**/*.sh
|
2023-02-26 10:42:14 +00:00
|
|
|
do
|
|
|
|
# shellcheck source=/dev/null
|
|
|
|
source "${FILE}"
|
2023-02-27 22:37:35 +00:00
|
|
|
done
|
2023-02-26 10:42:14 +00:00
|
|
|
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'info' 'Configuring mail server'
|
2021-02-23 19:03:01 +00:00
|
|
|
for FUNC in "${FUNCS_SETUP[@]}"
|
|
|
|
do
|
|
|
|
${FUNC}
|
|
|
|
done
|
2022-06-04 23:59:54 +00:00
|
|
|
|
|
|
|
# All startup modifications to configs should have taken place before calling this:
|
|
|
|
_prepare_for_change_detection
|
2021-02-23 19:03:01 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 09:25:23 +00:00
|
|
|
function _early_supervisor_setup
|
2021-02-23 19:03:01 +00:00
|
|
|
{
|
2022-08-22 06:31:32 +00:00
|
|
|
SUPERVISOR_LOGLEVEL="${SUPERVISOR_LOGLEVEL:-warn}"
|
|
|
|
|
2021-02-23 19:03:01 +00:00
|
|
|
if ! grep -q "loglevel = ${SUPERVISOR_LOGLEVEL}" /etc/supervisor/supervisord.conf
|
|
|
|
then
|
|
|
|
case "${SUPERVISOR_LOGLEVEL}" in
|
2022-03-17 10:24:30 +00:00
|
|
|
( 'critical' | 'error' | 'info' | 'debug' )
|
2021-02-23 19:03:01 +00:00
|
|
|
sed -i -E \
|
2021-02-25 09:57:20 +00:00
|
|
|
"s|(loglevel).*|\1 = ${SUPERVISOR_LOGLEVEL}|g" \
|
2021-02-23 19:03:01 +00:00
|
|
|
/etc/supervisor/supervisord.conf
|
2021-02-25 09:57:20 +00:00
|
|
|
|
|
|
|
supervisorctl reload
|
2021-12-29 16:36:29 +00:00
|
|
|
exit
|
2021-02-25 09:57:20 +00:00
|
|
|
;;
|
|
|
|
|
2022-08-22 06:31:32 +00:00
|
|
|
( 'warn' ) ;;
|
2021-02-23 19:03:01 +00:00
|
|
|
|
2022-03-17 10:24:30 +00:00
|
|
|
( * )
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'warn' \
|
2021-02-25 09:57:20 +00:00
|
|
|
"SUPERVISOR_LOGLEVEL '${SUPERVISOR_LOGLEVEL}' unknown. Using default 'warn'"
|
2021-02-23 19:03:01 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
fi
|
2021-02-25 09:57:20 +00:00
|
|
|
|
|
|
|
return 0
|
2021-02-23 19:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# File/folder permissions are fine when using docker volumes, but may be wrong
|
|
|
|
# when file system folders are mounted into the container.
|
|
|
|
# Set the expected values and create missing folders/files just in case.
|
|
|
|
function _setup_file_permissions
|
|
|
|
{
|
2022-03-26 09:17:08 +00:00
|
|
|
_log 'debug' 'Setting file and directory permissions'
|
2021-02-23 19:03:01 +00:00
|
|
|
|
|
|
|
mkdir -p /var/log/supervisor
|
|
|
|
|
|
|
|
mkdir -p /var/log/mail
|
|
|
|
chown syslog:root /var/log/mail
|
|
|
|
|
|
|
|
touch /var/log/mail/clamav.log
|
|
|
|
chown clamav:adm /var/log/mail/clamav.log
|
|
|
|
chmod 640 /var/log/mail/clamav.log
|
|
|
|
|
|
|
|
touch /var/log/mail/freshclam.log
|
|
|
|
chown clamav:adm /var/log/mail/freshclam.log
|
|
|
|
chmod 640 /var/log/mail/freshclam.log
|
|
|
|
}
|
|
|
|
|
2022-04-06 14:48:41 +00:00
|
|
|
function _setup_timezone
|
|
|
|
{
|
2022-08-22 06:31:32 +00:00
|
|
|
[[ -n ${TZ} ]] || return 0
|
2022-04-06 14:48:41 +00:00
|
|
|
_log 'debug' "Setting timezone to '${TZ}'"
|
|
|
|
|
|
|
|
local ZONEINFO_FILE="/usr/share/zoneinfo/${TZ}"
|
|
|
|
|
|
|
|
if [[ ! -e ${ZONEINFO_FILE} ]]
|
|
|
|
then
|
|
|
|
_log 'warn' "Cannot find timezone '${TZ}'"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ln -fs "${ZONEINFO_FILE}" /etc/localtime \
|
|
|
|
&& dpkg-reconfigure -f noninteractive tzdata &>/dev/null
|
|
|
|
then
|
|
|
|
_log 'trace' "Set time zone to '${TZ}'"
|
|
|
|
else
|
|
|
|
_log 'warn' "Setting timezone to '${TZ}' failed"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2023-02-28 09:25:23 +00:00
|
|
|
|
2023-02-27 19:21:45 +00:00
|
|
|
function _setup_apply_fixes_after_configuration
|
|
|
|
{
|
|
|
|
_log 'trace' 'Removing leftover PID files from a stop/start'
|
|
|
|
find /var/run/ -not -name 'supervisord.pid' -name '*.pid' -delete
|
|
|
|
touch /dev/shm/supervisor.sock
|
2023-02-28 09:25:23 +00:00
|
|
|
|
2023-02-27 19:21:45 +00:00
|
|
|
_log 'debug' 'Checking /var/mail permissions'
|
|
|
|
_chown_var_mail_if_necessary || _shutdown 'Failed to fix /var/mail permissions'
|
|
|
|
}
|
2023-02-28 09:25:23 +00:00
|
|
|
|
|
|
|
function _run_user_patches
|
|
|
|
{
|
|
|
|
local USER_PATCHES='/tmp/docker-mailserver/user-patches.sh'
|
|
|
|
|
|
|
|
if [[ -f ${USER_PATCHES} ]]
|
|
|
|
then
|
|
|
|
_log 'debug' 'Applying user patches'
|
|
|
|
/bin/bash "${USER_PATCHES}"
|
|
|
|
else
|
|
|
|
_log 'trace' "No optional '${USER_PATCHES}' provided"
|
|
|
|
fi
|
|
|
|
}
|