start-mailserver.sh split (#1820)

* splitting start-mailserver.sh

* refactoring part 2

* refactored setup-stack.sh
* stzarted adjusting target/bin/*.sh to use new usage format

* corrected lowercase-uppercase test error

* better handling of .bashrc variable export

* linting tests and fix for default assignements

* last stylistic changes and rebase
This commit is contained in:
Georg Lauterbach 2021-02-23 20:03:01 +01:00 committed by GitHub
parent 2262d354db
commit c881facbd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 2214 additions and 2278 deletions

View file

@ -6,7 +6,7 @@ DMS_DEBUG="${DMS_DEBUG:=0}"
function errex
{
echo "${@}" 1>&2
echo -e "Error :: ${*}\nAborting." >&2
exit 1
}
@ -149,13 +149,13 @@ function _populate_relayhost_map
sed -n '/^\s*[^#[:space:]]/ s/^[^@|]*@\([^|]\+\)|.*$/\1/p' /tmp/docker-mailserver/postfix-accounts.cf
[ -f /tmp/docker-mailserver/postfix-virtual.cf ] && sed -n '/^\s*[^#[:space:]]/ s/^\s*[^@[:space:]]*@\(\S\+\)\s.*/\1/p' /tmp/docker-mailserver/postfix-virtual.cf
} | while read -r domain
} | while read -r DOMAIN
do
# domain not already present *and* not ignored
if ! grep -q -e "^@${domain}\b" /etc/postfix/relayhost_map && ! grep -qs -e "^\s*@${domain}\s*$" /tmp/docker-mailserver/postfix-relaymap.cf
# DOMAIN not already present *and* not ignored
if ! grep -q -e "^@${DOMAIN}\b" /etc/postfix/relayhost_map && ! grep -qs -e "^\s*@${DOMAIN}\s*$" /tmp/docker-mailserver/postfix-relaymap.cf
then
_notify 'inf' "Adding relay mapping for ${domain}"
echo "@${domain} [${RELAY_HOST}]:${RELAY_PORT}" >> /etc/postfix/relayhost_map
_notify 'inf' "Adding relay mapping for ${DOMAIN}"
echo "@${DOMAIN} [${RELAY_HOST}]:${RELAY_PORT}" >> /etc/postfix/relayhost_map
fi
done
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,31 @@
#! /bin/bash
function check
{
_notify 'tasklog' 'Checking configuration'
for FUNC in "${FUNCS_CHECK[@]}"
do
${FUNC} || _defunc
done
}
function _check_hostname
{
_notify 'task' 'Checking that hostname/domainname is provided or overridden'
if [[ -n ${OVERRIDE_HOSTNAME} ]]
then
export HOSTNAME=${OVERRIDE_HOSTNAME}
export DOMAINNAME="${HOSTNAME#*.}"
fi
_notify 'inf' "Domain has been set to ${DOMAINNAME}"
_notify 'inf' "Hostname has been set to ${HOSTNAME}"
if ! grep -q -E '^(\S+[.]\S+)$' <<< "${HOSTNAME}"
then
_notify 'err' 'Setting hostname/domainname is required'
kill "$(< /var/run/supervisord.pid)"
return 1
fi
}

View file

@ -0,0 +1,154 @@
#! /bin/bash
function start_daemons
{
_notify 'tasklog' 'Starting daemons & mail server'
for FUNC in "${DAEMONS_START[@]}"
do
${FUNC} || _defunc
done
}
function _start_daemons_cron
{
_notify 'task' 'Starting cron'
supervisorctl start cron
}
function _start_daemons_rsyslog
{
_notify 'task' 'Starting rsyslog'
supervisorctl start rsyslog
}
function _start_daemons_saslauthd
{
_notify 'task' 'Starting saslauthd'
supervisorctl start "saslauthd_${SASLAUTHD_MECHANISMS}"
}
function _start_daemons_fail2ban
{
_notify 'task' 'Starting fail2ban'
touch /var/log/auth.log
# delete fail2ban.sock that probably was left here after container restart
if [[ -e /var/run/fail2ban/fail2ban.sock ]]
then
rm /var/run/fail2ban/fail2ban.sock
fi
supervisorctl start fail2ban
}
function _start_daemons_opendkim
{
_notify 'task' 'Starting opendkim'
supervisorctl start opendkim
}
function _start_daemons_opendmarc
{
_notify 'task' 'Starting opendmarc'
supervisorctl start opendmarc
}
function _start_daemons_postsrsd
{
_notify 'task' 'Starting postsrsd'
supervisorctl start postsrsd
}
function _start_daemons_postfix
{
_notify 'task' 'Starting postfix'
supervisorctl start postfix
}
function _start_daemons_dovecot
{
_notify 'task' 'Starting dovecot services'
if [[ ${ENABLE_POP3} -eq 1 ]]
then
_notify 'task' 'Starting pop3 services'
mv /etc/dovecot/protocols.d/pop3d.protocol.disab \
/etc/dovecot/protocols.d/pop3d.protocol
fi
if [[ -f /tmp/docker-mailserver/dovecot.cf ]]
then
cp /tmp/docker-mailserver/dovecot.cf /etc/dovecot/local.conf
fi
supervisorctl start dovecot
}
function _start_daemons_fetchmail
{
_notify 'task' 'Preparing fetchmail config'
/usr/local/bin/setup-fetchmail
if [[ ${FETCHMAIL_PARALLEL} -eq 1 ]]
then
mkdir /etc/fetchmailrc.d/
/usr/local/bin/fetchmailrc_split
local COUNTER=0
for RC in /etc/fetchmailrc.d/fetchmail-*.rc
do
COUNTER=$(( COUNTER + 1 ))
cat >"/etc/supervisor/conf.d/fetchmail-${COUNTER}.conf" << EOF
[program:fetchmail-${COUNTER}]
startsecs=0
autostart=false
autorestart=true
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
user=fetchmail
command=/usr/bin/fetchmail -f ${RC} -v --nodetach --daemon %(ENV_FETCHMAIL_POLL)s -i /var/lib/fetchmail/.fetchmail-UIDL-cache --pidfile /var/run/fetchmail/%(program_name)s.pid
EOF
chmod 700 "${RC}"
chown fetchmail:root "${RC}"
done
supervisorctl reread
supervisorctl update
COUNTER=0
for _ in /etc/fetchmailrc.d/fetchmail-*.rc
do
COUNTER=$(( COUNTER + 1 ))
_notify 'task' "Starting fetchmail instance ${COUNTER}"
supervisorctl start "fetchmail-${COUNTER}"
done
else
_notify 'task' 'Starting fetchmail'
supervisorctl start fetchmail
fi
}
function _start_daemons_clamav
{
_notify 'task' 'Starting clamav'
supervisorctl start clamav
}
function _start_daemons_postgrey
{
_notify 'task' 'Starting postgrey'
rm -f /var/run/postgrey/postgrey.pid
supervisorctl start postgrey
}
function _start_daemons_amavis
{
_notify 'task' 'Starting amavis'
supervisorctl start amavis
}
function _start_changedetector
{
_notify 'task' 'Starting changedetector'
supervisorctl start changedetector
}

View file

@ -0,0 +1,53 @@
#! /bin/bash
function fix
{
_notify 'tasklog' 'Post-configuration checks'
for FUNC in "${FUNCS_FIX[@]}"
do
${FUNC} || _defunc
done
_notify 'inf' 'Removing leftover PID files from a stop/start'
rm -rf /var/run/*.pid /var/run/*/*.pid
touch /dev/shm/supervisor.sock
}
function _fix_var_mail_permissions
{
_notify 'task' 'Checking /var/mail permissions'
# fix permissions, but skip this if 3 levels deep the user id is already set
if find /var/mail -maxdepth 3 -a \( \! -user 5000 -o \! -group 5000 \) | read -r
then
_notify 'inf' 'Fixing /var/mail permissions'
chown -R 5000:5000 /var/mail
else
_notify 'inf' 'Permissions in /var/mail look OK'
fi
}
function _fix_var_amavis_permissions
{
local AMAVIS_STATE_DIR='/var/mail-state/lib-amavis'
[[ ${ONE_DIR} -eq 0 ]] && AMAVIS_STATE_DIR="/var/lib/amavis"
[[ ! -e ${AMAVIS_STATE_DIR} ]] && return 0
_notify 'inf' 'Checking and fixing Amavis permissions'
chown -hR amavis:amavis "${AMAVIS_STATE_DIR}"
return 0
}
function _fix_cleanup_clamav
{
_notify 'task' 'Cleaning up disabled Clamav'
rm -f /etc/logrotate.d/clamav-*
rm -f /etc/cron.d/clamav-freshclam
}
function _fix_cleanup_spamassassin
{
_notify 'task' 'Cleaning up disabled spamassassin'
rm -f /etc/cron.daily/spamassassin
}

View file

@ -0,0 +1,64 @@
#! /bin/bash
function start_misc
{
_notify 'inf' 'Starting miscellaneous tasks'
for FUNC in "${FUNCS_MISC[@]}"
do
${FUNC} || _defunc
done
}
# consolidate all states into a single directory
# (/var/mail-state) to allow persistence using docker volumes
function _misc_save_states
{
local STATEDIR FILE FILES
STATEDIR='/var/mail-state'
if [[ ${ONE_DIR} -eq 1 ]] && [[ -d ${STATEDIR} ]]
then
_notify 'inf' "Consolidating all state onto ${STATEDIR}"
FILES=(
spool/postfix
lib/postfix
lib/amavis
lib/clamav
lib/spamassassin
lib/fail2ban
lib/postgrey
lib/dovecot
)
for FILE in "${FILES[@]}"
do
DEST="${STATEDIR}/${FILE//\//-}"
FILE="/var/${FILE}"
if [[ -d ${DEST} ]]
then
_notify 'inf' "Destination ${DEST} exists, linking ${FILE} to it"
rm -rf "${FILE}"
ln -s "${DEST}" "${FILE}"
elif [[ -d ${FILE} ]]
then
_notify 'inf' "Moving contents of ${FILE} to ${DEST}:" "$(ls "${FILE}")"
mv "${FILE}" "${DEST}"
ln -s "${DEST}" "${FILE}"
else
_notify 'inf' "Linking ${FILE} to ${DEST}"
mkdir -p "${DEST}"
ln -s "${DEST}" "${FILE}"
fi
done
_notify 'inf' 'Fixing /var/mail-state/* permissions'
chown -R clamav /var/mail-state/lib-clamav
chown -R postfix /var/mail-state/lib-postfix
chown -R postgrey /var/mail-state/lib-postgrey
chown -R debian-spamd /var/mail-state/lib-spamassassin
chown -R postfix /var/mail-state/spool-postfix
fi
}

File diff suppressed because it is too large Load diff