fix: do not add accounts that already exists to account files (#2419)

Co-authored-by: Casper <casperklein@users.noreply.github.com>
Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
This commit is contained in:
Georg Lauterbach 2022-02-21 13:31:03 +01:00 committed by GitHub
parent d858669dd4
commit 399284c092
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 14 deletions

View file

@ -1,14 +1,17 @@
#! /bin/bash #! /bin/bash
# Support for Postfix accounts managed via Dovecot # Support for Postfix accounts managed via Dovecot
# It looks like the DOMAIN in below logic is being stored in /etc/postfix/vhost, # 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? # 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). # Only an issue when $myhostname is an exact match (eg: bare domain FQDN).
DOVECOT_USERDB_FILE=/etc/dovecot/userdb
function _create_accounts function _create_accounts
{ {
: >/etc/postfix/vmailbox : >/etc/postfix/vmailbox
: >/etc/dovecot/userdb : >"${DOVECOT_USERDB_FILE}"
if [[ -f /tmp/docker-mailserver/postfix-accounts.cf ]] && [[ ${ENABLE_LDAP} -ne 1 ]] if [[ -f /tmp/docker-mailserver/postfix-accounts.cf ]] && [[ ${ENABLE_LDAP} -ne 1 ]]
then then
@ -22,8 +25,8 @@ function _create_accounts
# shellcheck disable=SC1003 # shellcheck disable=SC1003
sed -i -e '$a\' /tmp/docker-mailserver/postfix-accounts.cf sed -i -e '$a\' /tmp/docker-mailserver/postfix-accounts.cf
chown dovecot:dovecot /etc/dovecot/userdb chown dovecot:dovecot "${DOVECOT_USERDB_FILE}"
chmod 640 /etc/dovecot/userdb chmod 640 "${DOVECOT_USERDB_FILE}"
sed -i -e '/\!include auth-ldap\.conf\.ext/s/^/#/' /etc/dovecot/conf.d/10-auth.conf 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 sed -i -e '/\!include auth-passwdfile\.inc/s/^#//' /etc/dovecot/conf.d/10-auth.conf
@ -56,12 +59,25 @@ function _create_accounts
_notify 'inf' "Creating user '${USER}' for domain '${DOMAIN}' with attributes '${USER_ATTRIBUTES}'" _notify 'inf' "Creating user '${USER}' for domain '${DOMAIN}' with attributes '${USER_ATTRIBUTES}'"
fi fi
echo "${LOGIN} ${DOMAIN}/${USER}/" >> /etc/postfix/vmailbox local POSTFIX_VMAILBOX_LINE DOVECOT_USERDB_LINE
POSTFIX_VMAILBOX_LINE="${LOGIN} ${DOMAIN}/${USER}/"
if grep -qF "${POSTFIX_VMAILBOX_LINE}" /etc/postfix/vmailbox
then
_notify 'warn' "User '${USER}@${DOMAIN}' will not be added to '/etc/postfix/vmailbox' twice"
else
echo "${POSTFIX_VMAILBOX_LINE}" >>/etc/postfix/vmailbox
fi
# Dovecot's userdb has the following format # Dovecot's userdb has the following format
# user:password:uid:gid:(gecos):home:(shell):extra_fields # user:password:uid:gid:(gecos):home:(shell):extra_fields
echo \ DOVECOT_USERDB_LINE="${LOGIN}:${PASS}:5000:5000::/var/mail/${DOMAIN}/${USER}::${USER_ATTRIBUTES}"
"${LOGIN}:${PASS}:5000:5000::/var/mail/${DOMAIN}/${USER}::${USER_ATTRIBUTES}" \ if grep -qF "${DOVECOT_USERDB_LINE}" "${DOVECOT_USERDB_FILE}"
>>/etc/dovecot/userdb then
_notify 'warn' "Login '${LOGIN}' will not be added to '${DOVECOT_USERDB_FILE}' twice"
else
echo "${DOVECOT_USERDB_LINE}" >>"${DOVECOT_USERDB_FILE}"
fi
mkdir -p "/var/mail/${DOMAIN}/${USER}" mkdir -p "/var/mail/${DOMAIN}/${USER}"
@ -91,7 +107,7 @@ function _create_dovecot_alias_dummy_accounts
then then
# adding aliases to Dovecot's userdb # adding aliases to Dovecot's userdb
# ${REAL_FQUN} is a user's fully-qualified username # ${REAL_FQUN} is a user's fully-qualified username
local ALIAS REAL_FQUN local ALIAS REAL_FQUN DOVECOT_USERDB_LINE
while read -r ALIAS REAL_FQUN while read -r ALIAS REAL_FQUN
do do
# ignore comments # ignore comments
@ -138,9 +154,13 @@ function _create_dovecot_alias_dummy_accounts
fi fi
fi fi
echo \ DOVECOT_USERDB_LINE="${ALIAS}:${REAL_ACC[1]}:5000:5000::/var/mail/${REAL_DOMAINNAME}/${REAL_USERNAME}::${REAL_ACC[2]:-}"
"${ALIAS}:${REAL_ACC[1]}:5000:5000::/var/mail/${REAL_DOMAINNAME}/${REAL_USERNAME}::${REAL_ACC[2]:-}" \ if grep -qF "${DOVECOT_USERDB_LINE}" "${DOVECOT_USERDB_FILE}"
>> /etc/dovecot/userdb then
_notify 'warn' "Alias '${ALIAS}' will not be added to '${DOVECOT_USERDB_FILE}' twice"
else
echo "${DOVECOT_USERDB_LINE}" >>"${DOVECOT_USERDB_FILE}"
fi
done < /tmp/docker-mailserver/postfix-virtual.cf done < /tmp/docker-mailserver/postfix-virtual.cf
fi fi
} }

View file

@ -415,21 +415,21 @@ function _setup_ldap
then then
postconf -e "virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cf" postconf -e "virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cf"
else else
_notify 'war' "'/etc/postfix/ldap-users.cf' not found" _notify 'warn' "'/etc/postfix/ldap-users.cf' not found"
fi fi
if [[ -f /etc/postfix/ldap-domains.cf ]] if [[ -f /etc/postfix/ldap-domains.cf ]]
then then
postconf -e "virtual_mailbox_domains = /etc/postfix/vhost, ldap:/etc/postfix/ldap-domains.cf" postconf -e "virtual_mailbox_domains = /etc/postfix/vhost, ldap:/etc/postfix/ldap-domains.cf"
else else
_notify 'war' "'/etc/postfix/ldap-domains.cf' not found" _notify 'warn' "'/etc/postfix/ldap-domains.cf' not found"
fi fi
if [[ -f /etc/postfix/ldap-aliases.cf ]] && [[ -f /etc/postfix/ldap-groups.cf ]] if [[ -f /etc/postfix/ldap-aliases.cf ]] && [[ -f /etc/postfix/ldap-groups.cf ]]
then then
postconf -e "virtual_alias_maps = ldap:/etc/postfix/ldap-aliases.cf, ldap:/etc/postfix/ldap-groups.cf" postconf -e "virtual_alias_maps = ldap:/etc/postfix/ldap-aliases.cf, ldap:/etc/postfix/ldap-groups.cf"
else else
_notify 'war' "'/etc/postfix/ldap-aliases.cf' and / or '/etc/postfix/ldap-groups.cf' not found" _notify 'warn' "'/etc/postfix/ldap-aliases.cf' and / or '/etc/postfix/ldap-groups.cf' not found"
fi fi
# shellcheck disable=SC2016 # shellcheck disable=SC2016