diff --git a/Dockerfile b/Dockerfile index aeaac6f8..9477e6af 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 \ @@ -56,6 +57,7 @@ RUN apt-get update -q --fix-missing && \ pax \ p7zip-full \ postfix-ldap \ + postfix-mysql \ postfix-policyd-spf-python \ pyzor \ rar \ @@ -113,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/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 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 diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index f17d5c5d..6ea08c95 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"}" @@ -88,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 @@ -359,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 } ########################################################################## @@ -463,7 +472,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 +521,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 @@ -564,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" || \ @@ -578,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