2021-11-20 20:33:49 +00:00
|
|
|
#! /bin/bash
|
2022-02-21 12:31:03 +00:00
|
|
|
|
2021-11-20 20:33:49 +00:00
|
|
|
# Support for Postfix accounts managed via Dovecot
|
|
|
|
|
|
|
|
# It looks like the DOMAIN in below logic is being stored in /etc/postfix/vhost,
|
|
|
|
# even if it's a value used for Postfix `main.cf:mydestination`, which apparently isn't good?
|
|
|
|
# Only an issue when $myhostname is an exact match (eg: bare domain FQDN).
|
|
|
|
|
2022-02-21 12:31:03 +00:00
|
|
|
DOVECOT_USERDB_FILE=/etc/dovecot/userdb
|
2022-05-07 22:28:32 +00:00
|
|
|
DOVECOT_MASTERDB_FILE=/etc/dovecot/masterdb
|
2022-02-21 12:31:03 +00:00
|
|
|
|
2021-11-20 20:33:49 +00:00
|
|
|
function _create_accounts
|
|
|
|
{
|
|
|
|
: >/etc/postfix/vmailbox
|
2022-02-21 12:31:03 +00:00
|
|
|
: >"${DOVECOT_USERDB_FILE}"
|
2021-11-20 20:33:49 +00:00
|
|
|
|
2022-05-07 22:28:32 +00:00
|
|
|
[[ ${ENABLE_LDAP} -eq 1 ]] && return 0
|
|
|
|
|
2022-06-06 13:07:30 +00:00
|
|
|
local DATABASE_ACCOUNTS='/tmp/docker-mailserver/postfix-accounts.cf'
|
2022-05-07 22:28:32 +00:00
|
|
|
_create_masters
|
2022-06-06 13:07:30 +00:00
|
|
|
if [[ -f ${DATABASE_ACCOUNTS} ]]
|
2021-11-20 20:33:49 +00:00
|
|
|
then
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'trace' "Checking file line endings"
|
2022-06-06 13:07:30 +00:00
|
|
|
sed -i 's|\r||g' "${DATABASE_ACCOUNTS}"
|
2021-11-20 20:33:49 +00:00
|
|
|
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'trace' "Regenerating postfix user list"
|
2022-06-06 13:07:30 +00:00
|
|
|
echo "# WARNING: this file is auto-generated. Modify ${DATABASE_ACCOUNTS} to edit the user list." > /etc/postfix/vmailbox
|
2021-11-20 20:33:49 +00:00
|
|
|
|
2022-06-06 13:07:30 +00:00
|
|
|
# checking that ${DATABASE_ACCOUNTS} ends with a newline
|
2021-11-20 20:33:49 +00:00
|
|
|
# shellcheck disable=SC1003
|
2022-06-06 13:07:30 +00:00
|
|
|
sed -i -e '$a\' "${DATABASE_ACCOUNTS}"
|
2021-11-20 20:33:49 +00:00
|
|
|
|
2022-02-21 12:31:03 +00:00
|
|
|
chown dovecot:dovecot "${DOVECOT_USERDB_FILE}"
|
|
|
|
chmod 640 "${DOVECOT_USERDB_FILE}"
|
2021-11-20 20:33:49 +00:00
|
|
|
|
|
|
|
sed -i -e '/\!include auth-ldap\.conf\.ext/s/^/#/' /etc/dovecot/conf.d/10-auth.conf
|
|
|
|
sed -i -e '/\!include auth-passwdfile\.inc/s/^#//' /etc/dovecot/conf.d/10-auth.conf
|
|
|
|
|
|
|
|
# creating users ; 'pass' is encrypted
|
|
|
|
# comments and empty lines are ignored
|
|
|
|
local LOGIN PASS USER_ATTRIBUTES
|
|
|
|
while IFS=$'|' read -r LOGIN PASS USER_ATTRIBUTES
|
|
|
|
do
|
|
|
|
# Setting variables for better readability
|
|
|
|
USER=$(echo "${LOGIN}" | cut -d @ -f1)
|
|
|
|
DOMAIN=$(echo "${LOGIN}" | cut -d @ -f2)
|
|
|
|
|
|
|
|
# test if user has a defined quota
|
|
|
|
if [[ -f /tmp/docker-mailserver/dovecot-quotas.cf ]]
|
|
|
|
then
|
|
|
|
declare -a USER_QUOTA
|
|
|
|
IFS=':' read -r -a USER_QUOTA < <(grep "${USER}@${DOMAIN}:" -i /tmp/docker-mailserver/dovecot-quotas.cf)
|
|
|
|
|
|
|
|
if [[ ${#USER_QUOTA[@]} -eq 2 ]]
|
|
|
|
then
|
|
|
|
USER_ATTRIBUTES="${USER_ATTRIBUTES:+${USER_ATTRIBUTES} }userdb_quota_rule=*:bytes=${USER_QUOTA[1]}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z ${USER_ATTRIBUTES} ]]
|
|
|
|
then
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'debug' "Creating user '${USER}' for domain '${DOMAIN}'"
|
2021-11-20 20:33:49 +00:00
|
|
|
else
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'debug' "Creating user '${USER}' for domain '${DOMAIN}' with attributes '${USER_ATTRIBUTES}'"
|
2021-11-20 20:33:49 +00:00
|
|
|
fi
|
|
|
|
|
2022-02-21 12:31:03 +00:00
|
|
|
local POSTFIX_VMAILBOX_LINE DOVECOT_USERDB_LINE
|
|
|
|
|
|
|
|
POSTFIX_VMAILBOX_LINE="${LOGIN} ${DOMAIN}/${USER}/"
|
|
|
|
if grep -qF "${POSTFIX_VMAILBOX_LINE}" /etc/postfix/vmailbox
|
|
|
|
then
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'warn' "User '${USER}@${DOMAIN}' will not be added to '/etc/postfix/vmailbox' twice"
|
2022-02-21 12:31:03 +00:00
|
|
|
else
|
|
|
|
echo "${POSTFIX_VMAILBOX_LINE}" >>/etc/postfix/vmailbox
|
|
|
|
fi
|
|
|
|
|
2021-11-20 20:33:49 +00:00
|
|
|
# Dovecot's userdb has the following format
|
|
|
|
# user:password:uid:gid:(gecos):home:(shell):extra_fields
|
2022-02-21 12:31:03 +00:00
|
|
|
DOVECOT_USERDB_LINE="${LOGIN}:${PASS}:5000:5000::/var/mail/${DOMAIN}/${USER}::${USER_ATTRIBUTES}"
|
|
|
|
if grep -qF "${DOVECOT_USERDB_LINE}" "${DOVECOT_USERDB_FILE}"
|
|
|
|
then
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'warn' "Login '${LOGIN}' will not be added to '${DOVECOT_USERDB_FILE}' twice"
|
2022-02-21 12:31:03 +00:00
|
|
|
else
|
|
|
|
echo "${DOVECOT_USERDB_LINE}" >>"${DOVECOT_USERDB_FILE}"
|
|
|
|
fi
|
2021-11-20 20:33:49 +00:00
|
|
|
|
|
|
|
mkdir -p "/var/mail/${DOMAIN}/${USER}"
|
|
|
|
|
|
|
|
# copy user provided sieve file, if present
|
|
|
|
if [[ -e "/tmp/docker-mailserver/${LOGIN}.dovecot.sieve" ]]
|
|
|
|
then
|
|
|
|
cp "/tmp/docker-mailserver/${LOGIN}.dovecot.sieve" "/var/mail/${DOMAIN}/${USER}/.dovecot.sieve"
|
|
|
|
fi
|
2022-06-06 13:07:30 +00:00
|
|
|
done < <(_get_valid_lines_from_file "${DATABASE_ACCOUNTS}")
|
2021-11-20 20:33:49 +00:00
|
|
|
|
|
|
|
_create_dovecot_alias_dummy_accounts
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Required when using Dovecot Quotas to avoid blacklisting risk from backscatter
|
|
|
|
# Note: This is a workaround only suitable for basic aliases that map to single real addresses,
|
|
|
|
# not multiple addresses (real accounts or additional aliases), those will not work with Postfix
|
|
|
|
# `quota-status` policy service and remain at risk of backscatter.
|
|
|
|
#
|
|
|
|
# see https://github.com/docker-mailserver/docker-mailserver/pull/2248#issuecomment-953313852
|
|
|
|
# for more details on this method
|
|
|
|
function _create_dovecot_alias_dummy_accounts
|
|
|
|
{
|
2022-06-06 13:07:30 +00:00
|
|
|
local DATABASE_VIRTUAL='/tmp/docker-mailserver/postfix-virtual.cf'
|
|
|
|
|
|
|
|
if [[ -f ${DATABASE_VIRTUAL} ]] && [[ ${ENABLE_QUOTAS} -eq 1 ]]
|
2021-11-20 20:33:49 +00:00
|
|
|
then
|
|
|
|
# adding aliases to Dovecot's userdb
|
|
|
|
# ${REAL_FQUN} is a user's fully-qualified username
|
2022-02-21 12:31:03 +00:00
|
|
|
local ALIAS REAL_FQUN DOVECOT_USERDB_LINE
|
2021-11-20 20:33:49 +00:00
|
|
|
while read -r ALIAS REAL_FQUN
|
|
|
|
do
|
|
|
|
# alias is assumed to not be a proper e-mail
|
|
|
|
# these aliases do not need to be added to Dovecot's userdb
|
|
|
|
[[ ! ${ALIAS} == *@* ]] && continue
|
|
|
|
|
|
|
|
# clear possibly already filled arrays
|
|
|
|
# do not remove the following line of code
|
|
|
|
unset REAL_ACC USER_QUOTA
|
|
|
|
declare -a REAL_ACC USER_QUOTA
|
|
|
|
|
|
|
|
local REAL_USERNAME REAL_DOMAINNAME
|
|
|
|
REAL_USERNAME=$(cut -d '@' -f 1 <<< "${REAL_FQUN}")
|
|
|
|
REAL_DOMAINNAME=$(cut -d '@' -f 2 <<< "${REAL_FQUN}")
|
|
|
|
|
2022-06-06 13:07:30 +00:00
|
|
|
if ! grep -q "${REAL_FQUN}" "${DATABASE_ACCOUNTS}"
|
2021-11-20 20:33:49 +00:00
|
|
|
then
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'debug' "Alias '${ALIAS}' is non-local (or mapped to a non-existing account) and will not be added to Dovecot's userdb"
|
2021-11-20 20:33:49 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'debug' "Adding alias '${ALIAS}' for user '${REAL_FQUN}' to Dovecot's userdb"
|
2021-11-20 20:33:49 +00:00
|
|
|
|
|
|
|
# ${REAL_ACC[0]} => real account name (e-mail address) == ${REAL_FQUN}
|
|
|
|
# ${REAL_ACC[1]} => password hash
|
|
|
|
# ${REAL_ACC[2]} => optional user attributes
|
2022-06-06 13:07:30 +00:00
|
|
|
IFS='|' read -r -a REAL_ACC < <(grep "${REAL_FQUN}" "${DATABASE_ACCOUNTS}")
|
2021-11-20 20:33:49 +00:00
|
|
|
|
|
|
|
if [[ -z ${REAL_ACC[1]} ]]
|
|
|
|
then
|
|
|
|
dms_panic__misconfigured 'postfix-accounts.cf' 'alias configuration'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# test if user has a defined quota
|
|
|
|
if [[ -f /tmp/docker-mailserver/dovecot-quotas.cf ]]
|
|
|
|
then
|
|
|
|
IFS=':' read -r -a USER_QUOTA < <(grep "${REAL_FQUN}:" -i /tmp/docker-mailserver/dovecot-quotas.cf)
|
|
|
|
if [[ ${#USER_QUOTA[@]} -eq 2 ]]
|
|
|
|
then
|
|
|
|
REAL_ACC[2]="${REAL_ACC[2]:+${REAL_ACC[2]} }userdb_quota_rule=*:bytes=${USER_QUOTA[1]}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-02-21 12:31:03 +00:00
|
|
|
DOVECOT_USERDB_LINE="${ALIAS}:${REAL_ACC[1]}:5000:5000::/var/mail/${REAL_DOMAINNAME}/${REAL_USERNAME}::${REAL_ACC[2]:-}"
|
|
|
|
if grep -qF "${DOVECOT_USERDB_LINE}" "${DOVECOT_USERDB_FILE}"
|
|
|
|
then
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'warn' "Alias '${ALIAS}' will not be added to '${DOVECOT_USERDB_FILE}' twice"
|
2022-02-21 12:31:03 +00:00
|
|
|
else
|
|
|
|
echo "${DOVECOT_USERDB_LINE}" >>"${DOVECOT_USERDB_FILE}"
|
|
|
|
fi
|
2022-06-06 13:07:30 +00:00
|
|
|
done < <(_get_valid_lines_from_file "${DATABASE_VIRTUAL}")
|
2021-11-20 20:33:49 +00:00
|
|
|
fi
|
|
|
|
}
|
2022-05-07 22:28:32 +00:00
|
|
|
|
|
|
|
# Support Dovecot master user: https://doc.dovecot.org/configuration_manual/authentication/master_users/
|
|
|
|
# Supporting LDAP users requires `auth_bind = yes` in `dovecot-ldap.conf.ext`, see docker-mailserver/docker-mailserver/pull/2535 for details
|
|
|
|
function _create_masters
|
|
|
|
{
|
|
|
|
: >"${DOVECOT_MASTERDB_FILE}"
|
|
|
|
|
2022-06-06 13:07:30 +00:00
|
|
|
local DATABASE_DOVECOT_MASTERS='/tmp/docker-mailserver/dovecot-masters.cf'
|
|
|
|
if [[ -f ${DATABASE_DOVECOT_MASTERS} ]]
|
2022-05-07 22:28:32 +00:00
|
|
|
then
|
|
|
|
_log 'trace' "Checking file line endings"
|
2022-06-06 13:07:30 +00:00
|
|
|
sed -i 's|\r||g' "${DATABASE_DOVECOT_MASTERS}"
|
2022-05-07 22:28:32 +00:00
|
|
|
|
|
|
|
_log 'trace' "Regenerating dovecot masters list"
|
|
|
|
|
2022-06-06 13:07:30 +00:00
|
|
|
# checking that ${DATABASE_DOVECOT_MASTERS} ends with a newline
|
2022-05-07 22:28:32 +00:00
|
|
|
# shellcheck disable=SC1003
|
2022-06-06 13:07:30 +00:00
|
|
|
sed -i -e '$a\' "${DATABASE_DOVECOT_MASTERS}"
|
2022-05-07 22:28:32 +00:00
|
|
|
|
|
|
|
chown dovecot:dovecot "${DOVECOT_MASTERDB_FILE}"
|
|
|
|
chmod 640 "${DOVECOT_MASTERDB_FILE}"
|
|
|
|
|
|
|
|
sed -i -e '/\!include auth-master\.inc/s/^#//' /etc/dovecot/conf.d/10-auth.conf
|
|
|
|
|
|
|
|
# creating users ; 'pass' is encrypted
|
|
|
|
# comments and empty lines are ignored
|
|
|
|
local LOGIN PASS
|
|
|
|
while IFS=$'|' read -r LOGIN PASS
|
|
|
|
do
|
|
|
|
_log 'debug' "Creating master user '${LOGIN}'"
|
|
|
|
|
|
|
|
local DOVECOT_MASTERDB_LINE
|
|
|
|
|
|
|
|
# Dovecot's masterdb has the following format
|
|
|
|
# user:password
|
|
|
|
DOVECOT_MASTERDB_LINE="${LOGIN}:${PASS}"
|
|
|
|
if grep -qF "${DOVECOT_MASTERDB_LINE}" "${DOVECOT_MASTERDB_FILE}"
|
|
|
|
then
|
|
|
|
_log 'warn' "Login '${LOGIN}' will not be added to '${DOVECOT_MASTERDB_FILE}' twice"
|
|
|
|
else
|
|
|
|
echo "${DOVECOT_MASTERDB_LINE}" >>"${DOVECOT_MASTERDB_FILE}"
|
|
|
|
fi
|
2022-06-06 13:07:30 +00:00
|
|
|
done < <(_get_valid_lines_from_file "${DATABASE_DOVECOT_MASTERS}")
|
2022-05-07 22:28:32 +00:00
|
|
|
fi
|
|
|
|
}
|