From f004b6a83dbe4b2ba96b0d88bf0f87918425579b Mon Sep 17 00:00:00 2001 From: Julian Hille Date: Tue, 15 Aug 2017 02:14:24 +0200 Subject: [PATCH 1/5] Add dovecot mysql --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index aeaac6f8..0064c5ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,7 @@ RUN apt-get update -q --fix-missing && \ dovecot-imapd \ dovecot-ldap \ dovecot-lmtpd \ + dovecot-mysql \ dovecot-managesieved \ dovecot-pop3d \ dovecot-sieve \ From 590197022badd95a5a5816b87bd826d9064804c4 Mon Sep 17 00:00:00 2001 From: Julian Hille Date: Wed, 16 Aug 2017 02:25:36 +0200 Subject: [PATCH 2/5] Add ENABLE_MYSQL env variable --- target/start-mailserver.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index f17d5c5d..18795210 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -14,6 +14,7 @@ DEFAULT_VARS["ENABLE_FAIL2BAN"]="${ENABLE_FAIL2BAN:="0"}" DEFAULT_VARS["ENABLE_MANAGESIEVE"]="${ENABLE_MANAGESIEVE:="0"}" DEFAULT_VARS["ENABLE_FETCHMAIL"]="${ENABLE_FETCHMAIL:="0"}" DEFAULT_VARS["ENABLE_LDAP"]="${ENABLE_LDAP:="0"}" +DEFAULT_VARS["ENABLE_MYSQL"]="${ENABLE_MYSQL:="0"}" DEFAULT_VARS["ENABLE_POSTGREY"]="${ENABLE_POSTGREY:="0"}" DEFAULT_VARS["POSTGREY_DELAY"]="${POSTGREY_DELAY:="300"}" DEFAULT_VARS["POSTGREY_MAX_AGE"]="${POSTGREY_MAX_AGE:="35"}" @@ -463,7 +464,7 @@ function _setup_dovecot_local_user() { notify 'task' 'Setting up Dovecot Local User' echo -n > /etc/postfix/vmailbox echo -n > /etc/dovecot/userdb - if [ -f /tmp/docker-mailserver/postfix-accounts.cf -a "$ENABLE_LDAP" != 1 ]; then + if [[ -f /tmp/docker-mailserver/postfix-accounts.cf ]] && [[ ${ENABLE_LDAP} != 1 ]] && [[ ${ENABLE_MYSQL} != 1 ]]; then notify 'inf' "Checking file line endings" sed -i 's/\r//g' /tmp/docker-mailserver/postfix-accounts.cf notify 'inf' "Regenerating postfix user list" @@ -512,8 +513,8 @@ function _setup_dovecot_local_user() { fi if [[ ! $(grep '@' /tmp/docker-mailserver/postfix-accounts.cf | grep '|') ]]; then - if [ $ENABLE_LDAP -eq 0 ]; then - notify 'fatal' "Unless using LDAP, you need at least 1 email account to start the server." + if [ $ENABLE_LDAP -eq 0 -a $ENABLE_MYSQL -eq 0 ]; then + notify 'fatal' "Unless using LDAP or MySQL, you need at least 1 email account to start the server." defunc fi fi From b8095c24c190ad24d9c4c9c89359e21bd615d30b Mon Sep 17 00:00:00 2001 From: Julian Hille Date: Wed, 16 Aug 2017 02:31:53 +0200 Subject: [PATCH 3/5] ADD new env variable to readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index be25c308..a75299a3 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,10 @@ Otherwise, `iptables` won't be able to ban IPs. - **0** => `fetchmail` disabled - 1 => `fetchmail` enabled +##### ENABLE_MYSQL + - **empty** => MYSQL authentification is disabled + - 1 => MYSQL authentification is enabled + ##### ENABLE_LDAP - **empty** => LDAP authentification is disabled From f1d5209b35570398bf1d0bc236dd16e4bad438cd Mon Sep 17 00:00:00 2001 From: Julian Hille Date: Thu, 17 Aug 2017 00:01:28 +0200 Subject: [PATCH 4/5] More mysql implementation --- Dockerfile | 1 + target/start-mailserver.sh | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/Dockerfile b/Dockerfile index 0064c5ef..b7e3e704 100644 --- a/Dockerfile +++ b/Dockerfile @@ -57,6 +57,7 @@ RUN apt-get update -q --fix-missing && \ pax \ p7zip-full \ postfix-ldap \ + postifx-mysql \ postfix-policyd-spf-python \ pyzor \ rar \ diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index 18795210..6ea08c95 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -89,6 +89,10 @@ function register_functions() { _register_setup_function "_setup_ldap" fi + if [ "$ENABLE_MYSQL" = 1 ];then + _register_setup_function "_setup_mysql" + fi + if [ "$ENABLE_SASLAUTHD" = 1 ];then _register_setup_function "_setup_saslauthd" fi @@ -360,6 +364,10 @@ function _check_hostname() { function _check_environment_variables() { notify "task" "Check that there are no conflicts with env variables [$FUNCNAME]" + if [[ ${ENABLE_LDAP} = 1 ]] && [[ ${ENABLE_MYSQL} = 1 ]]; then + notify 'fatal' "Mysql and LDAP must not be enabled at the same time." + defunc + fi return 0 } ########################################################################## @@ -565,8 +573,11 @@ function _setup_ldap() { notify 'inf' "Enabling dovecot LDAP authentification" sed -i -e '/\!include auth-ldap\.conf\.ext/s/^#//' /etc/dovecot/conf.d/10-auth.conf + sed -i -e '/\!include auth-sql\.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 + + notify 'inf' "Configuring LDAP" [ -f /etc/postfix/ldap-users.cf ] && \ postconf -e "virtual_mailbox_maps = ldap:/etc/postfix/ldap-users.cf" || \ @@ -579,6 +590,50 @@ function _setup_ldap() { return 0 } +function _setup_mysql() { + notify 'task' 'Setting up MySQL' + + notify 'inf' "Configuring postfix MySQL" + + declare -A _postfix_mysql_mapping + + _postfix_mysql_mapping["POSTFIX_MYSQL_HOSTS"]="${POSTFIX_MYSQL_HOSTS:="${MYSQL_HOST}"}" + _postfix_mysql_mapping["POSTFIX_MYSQL_DBNAME"]="${POSTFIX_MYSQL_DBNAME:="${MYSQL_DB}"}" + _postfix_mysql_mapping["POSTFIX_MYSQL_USER"]="${POSTFIX_MYSQL_USER:="${MYSQL_USER}"}" + _postfix_mysql_mapping["POSTFIX_MYSQL_PASSWORD"]="${POSTFIX_MYSQL_PASSWORD:="${MYSQL_PASSWORD}"}" + for var in ${!_dovecot_mysql_mapping[@]}; do + export $var=${_dovecot_mysql_mapping[$var]} + done + configomat.sh "POSTFIX_MYSQL_" "/etc/postfix/mysql.cf" + + notify 'inf' "Configuring dovecot MySQL" + declare -A _dovecot_mysql_mapping + + _dovecot_mysql_mapping["DOVECOT_MYSQL_CONNECT"]="${DOVECOT_MYSQL_CONNECT:="host=${MYSQL_HOST} dbname=${MYSQL_DB} user=${MYSQL_USER} password=${MYSQL_PASSWORD}"}" + _dovecot_mysql_mapping["DOVECOT_MYSQL_DEFAULT_PASS_SCHEME"]="${DOVECOT_MYSQL_DEFAULT_PASS_SCHEME:="${MYSQL_PASS_SCHEME}"}" + + for var in ${!_dovecot_mysql_mapping[@]}; do + export $var=${_dovecot_mysql_mapping[$var]} + done + + configomat.sh "DOVECOT_MYSQL_" "/etc/dovecot/dovecot-mysql.conf.ext" + + # Add domainname to vhost. + echo $DOMAINNAME >> /tmp/vhost.tmp + + notify 'inf' "Enabling dovecot mysql authentification" + sed -i -e '/\!include auth-sql\.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-ldap\.inc/s/^/#/' /etc/dovecot/conf.d/10-auth.conf + + notify 'inf' "Configuring MySQL" + [ -f /etc/postfix/mysql.cf ] && \ + postconf -e "virtual_mailbox_maps = mysql:/etc/postfix/mysql.cf" || \ + notify 'inf' "==> Warning: /etc/postfix/ldap-user.cf not found" + + return 0 +} + function _setup_postgrey() { notify 'inf' "Configuring postgrey" sed -i -e 's/bl.spamcop.net$/bl.spamcop.net, check_policy_service inet:127.0.0.1:10023/' /etc/postfix/main.cf From 74cbe1ffed1b207e9584e97382321e2db257fd35 Mon Sep 17 00:00:00 2001 From: Julian Hille Date: Thu, 17 Aug 2017 00:17:46 +0200 Subject: [PATCH 5/5] Add missing files and fix typo in package. --- Dockerfile | 5 +++-- target/dovecot/dovecot-mysql.conf.ext | 8 ++++++++ target/postfix/mysql.cf | 5 +++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 target/dovecot/dovecot-mysql.conf.ext create mode 100644 target/postfix/mysql.cf diff --git a/Dockerfile b/Dockerfile index b7e3e704..9477e6af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -57,7 +57,7 @@ RUN apt-get update -q --fix-missing && \ pax \ p7zip-full \ postfix-ldap \ - postifx-mysql \ + postfix-mysql \ postfix-policyd-spf-python \ pyzor \ rar \ @@ -115,7 +115,8 @@ RUN sed -i -e 's/include_try \/usr\/share\/dovecot\/protocols\.d/include_try \/e # Configures LDAP COPY target/dovecot/dovecot-ldap.conf.ext /etc/dovecot -COPY target/postfix/ldap-users.cf target/postfix/ldap-groups.cf target/postfix/ldap-aliases.cf /etc/postfix/ +COPY target/dovecot/dovecot-mysql.conf.ext /etc/dovecot +COPY target/postfix/mysql.cf target/postfix/ldap-users.cf target/postfix/ldap-groups.cf target/postfix/ldap-aliases.cf /etc/postfix/ # Enables Spamassassin CRON updates RUN sed -i -r 's/^(CRON)=0/\1=1/g' /etc/default/spamassassin diff --git a/target/dovecot/dovecot-mysql.conf.ext b/target/dovecot/dovecot-mysql.conf.ext new file mode 100644 index 00000000..b4f49c1d --- /dev/null +++ b/target/dovecot/dovecot-mysql.conf.ext @@ -0,0 +1,8 @@ +driver = mysql +connect = "host=127.0.0.1 dbname=vmail user=vmail password=vmaildbpass" +default_pass_scheme = SHA512-CRYPT + +password_query = SELECT username AS user, domain, password FROM accounts WHERE username = '%n' AND domain = '%d' and enabled = true; + +user_query = SELECT concat('*:storage=', quota, 'M') AS quota_rule FROM accounts WHERE username = '%n' AND domain = '%d' AND sendonly = false; +iterate_query = SELECT username, domain FROM accounts where sendonly = false; \ No newline at end of file diff --git a/target/postfix/mysql.cf b/target/postfix/mysql.cf new file mode 100644 index 00000000..b6eae2a2 --- /dev/null +++ b/target/postfix/mysql.cf @@ -0,0 +1,5 @@ +hosts = +user = +password = +dbname = +query = SELECT 1 FROM transport WHERE domain= '%s \ No newline at end of file