Support extra user_attributes in accounts configuration (#1792)

This allows you to add for example

    |userdb_mail=mbox:~/mail:INBOX=~/inbox

 to the end of an account to have a different mailbox configuration.
This commit is contained in:
Ask Bjørn Hansen 2021-02-07 10:02:09 -08:00 committed by GitHub
parent df3ef4865f
commit 4a3735bced
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 8 deletions

View file

@ -37,6 +37,7 @@ clean:
generate-accounts: generate-accounts:
@ docker run --rm -e MAIL_USER=user1@localhost.localdomain -e MAIL_PASS=mypassword -t $(NAME) /bin/sh -c 'echo "$$MAIL_USER|$$(doveadm pw -s SHA512-CRYPT -u $$MAIL_USER -p $$MAIL_PASS)"' > test/config/postfix-accounts.cf @ docker run --rm -e MAIL_USER=user1@localhost.localdomain -e MAIL_PASS=mypassword -t $(NAME) /bin/sh -c 'echo "$$MAIL_USER|$$(doveadm pw -s SHA512-CRYPT -u $$MAIL_USER -p $$MAIL_PASS)"' > test/config/postfix-accounts.cf
@ docker run --rm -e MAIL_USER=user2@otherdomain.tld -e MAIL_PASS=mypassword -t $(NAME) /bin/sh -c 'echo "$$MAIL_USER|$$(doveadm pw -s SHA512-CRYPT -u $$MAIL_USER -p $$MAIL_PASS)"' >> test/config/postfix-accounts.cf @ docker run --rm -e MAIL_USER=user2@otherdomain.tld -e MAIL_PASS=mypassword -t $(NAME) /bin/sh -c 'echo "$$MAIL_USER|$$(doveadm pw -s SHA512-CRYPT -u $$MAIL_USER -p $$MAIL_PASS)"' >> test/config/postfix-accounts.cf
@ docker run --rm -e MAIL_USER=user3@localhost.localdomain -e MAIL_PASS=mypassword -t $(NAME) /bin/sh -c 'echo "$$MAIL_USER|$$(doveadm pw -s SHA512-CRYPT -u $$MAIL_USER -p $$MAIL_PASS)|userdb_mail=mbox:~/mail:INBOX=~/inbox"' >> test/config/postfix-accounts.cf
@ echo "# this is a test comment, please don't delete me :'(" >> test/config/postfix-accounts.cf @ echo "# this is a test comment, please don't delete me :'(" >> test/config/postfix-accounts.cf
@ echo " # this is also a test comment, :O" >> test/config/postfix-accounts.cf @ echo " # this is also a test comment, :O" >> test/config/postfix-accounts.cf

View file

@ -136,12 +136,11 @@ do
# creating users ; 'pass' is encrypted # creating users ; 'pass' is encrypted
# comments and empty lines are ignored # comments and empty lines are ignored
while IFS=$'|' read -r LOGIN PASS while IFS=$'|' read -r LOGIN PASS USER_ATTRIBUTES
do do
USER=$(echo "${LOGIN}" | cut -d @ -f1) USER=$(echo "${LOGIN}" | cut -d @ -f1)
DOMAIN=$(echo "${LOGIN}" | cut -d @ -f2) DOMAIN=$(echo "${LOGIN}" | cut -d @ -f2)
user_attributes=""
# test if user has a defined quota # test if user has a defined quota
if [[ -f /tmp/docker-mailserver/dovecot-quotas.cf ]] if [[ -f /tmp/docker-mailserver/dovecot-quotas.cf ]]
then then
@ -149,7 +148,7 @@ do
IFS=':' ; read -r -a USER_QUOTA < <(grep "${USER}@${DOMAIN}:" -i /tmp/docker-mailserver/dovecot-quotas.cf) IFS=':' ; read -r -a USER_QUOTA < <(grep "${USER}@${DOMAIN}:" -i /tmp/docker-mailserver/dovecot-quotas.cf)
unset IFS unset IFS
[[ ${#USER_QUOTA[@]} -eq 2 ]] && user_attributes="${user_attributes}userdb_quota_rule=*:bytes=${USER_QUOTA[1]}" [[ ${#USER_QUOTA[@]} -eq 2 ]] && USER_ATTRIBUTES="${USER_ATTRIBUTES} userdb_quota_rule=*:bytes=${USER_QUOTA[1]}"
fi fi
echo "${LOGIN} ${DOMAIN}/${USER}/" >>/etc/postfix/vmailbox echo "${LOGIN} ${DOMAIN}/${USER}/" >>/etc/postfix/vmailbox
@ -158,7 +157,7 @@ do
# user:password:uid:gid:(gecos):home:(shell):extra_fields # user:password:uid:gid:(gecos):home:(shell):extra_fields
# example : # example :
# ${LOGIN}:${PASS}:5000:5000::/var/mail/${DOMAIN}/${USER}::userdb_mail=maildir:/var/mail/${DOMAIN}/${USER} # ${LOGIN}:${PASS}:5000:5000::/var/mail/${DOMAIN}/${USER}::userdb_mail=maildir:/var/mail/${DOMAIN}/${USER}
echo "${LOGIN}:${PASS}:5000:5000::/var/mail/${DOMAIN}/${USER}::${user_attributes}" >>/etc/dovecot/userdb echo "${LOGIN}:${PASS}:5000:5000::/var/mail/${DOMAIN}/${USER}::${USER_ATTRIBUTES}" >>/etc/dovecot/userdb
mkdir -p "/var/mail/${DOMAIN}/${USER}" mkdir -p "/var/mail/${DOMAIN}/${USER}"
if [[ -e /tmp/docker-mailserver/${LOGIN}.dovecot.sieve ]] if [[ -e /tmp/docker-mailserver/${LOGIN}.dovecot.sieve ]]

View file

@ -711,13 +711,12 @@ function _setup_dovecot_local_user
# creating users ; 'pass' is encrypted # creating users ; 'pass' is encrypted
# comments and empty lines are ignored # comments and empty lines are ignored
while IFS=$'|' read -r LOGIN PASS while IFS=$'|' read -r LOGIN PASS USER_ATTRIBUTES
do do
# Setting variables for better readability # Setting variables for better readability
USER=$(echo "${LOGIN}" | cut -d @ -f1) USER=$(echo "${LOGIN}" | cut -d @ -f1)
DOMAIN=$(echo "${LOGIN}" | cut -d @ -f2) DOMAIN=$(echo "${LOGIN}" | cut -d @ -f2)
USER_ATTRIBUTES=""
# test if user has a defined quota # test if user has a defined quota
if [[ -f /tmp/docker-mailserver/dovecot-quotas.cf ]] if [[ -f /tmp/docker-mailserver/dovecot-quotas.cf ]]
then then
@ -725,7 +724,7 @@ function _setup_dovecot_local_user
IFS=':' ; read -r -a USER_QUOTA < <(grep "${USER}@${DOMAIN}:" -i /tmp/docker-mailserver/dovecot-quotas.cf) IFS=':' ; read -r -a USER_QUOTA < <(grep "${USER}@${DOMAIN}:" -i /tmp/docker-mailserver/dovecot-quotas.cf)
unset IFS unset IFS
[[ ${#USER_QUOTA[@]} -eq 2 ]] && USER_ATTRIBUTES="${USER_ATTRIBUTES}userdb_quota_rule=*:bytes=${USER_QUOTA[1]}" [[ ${#USER_QUOTA[@]} -eq 2 ]] && USER_ATTRIBUTES="${USER_ATTRIBUTES} userdb_quota_rule=*:bytes=${USER_QUOTA[1]}"
fi fi
# Let's go! # Let's go!

View file

@ -0,0 +1,12 @@
HELO mail.external.tld
MAIL FROM: user@external.tld
RCPT TO: user3@localhost.localdomain
DATA
From: Docker Mail Server <dockermailserver@external.tld>
To: Existing Local User <user1@localhost.localdomain>
Date: Sat, 22 May 2010 07:43:33 -0400
Subject: Test Message existing-user1.txt
This is a test mail.
.
QUIT

View file

@ -60,6 +60,7 @@ setup_file() {
docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-alias-recipient-delimiter.txt" docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-alias-recipient-delimiter.txt"
docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user1.txt" docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user1.txt"
docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user2.txt" docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user2.txt"
docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user3.txt"
docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-added.txt" docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-added.txt"
docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user-and-cc-local-alias.txt" docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user-and-cc-local-alias.txt"
docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-regexp-alias-external.txt" docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-regexp-alias-external.txt"
@ -248,6 +249,7 @@ teardown_file() {
1 <user1@localhost.localdomain>, orig_to=<root> 1 <user1@localhost.localdomain>, orig_to=<root>
1 <user1~test@localhost.localdomain> 1 <user1~test@localhost.localdomain>
2 <user2@otherdomain.tld> 2 <user2@otherdomain.tld>
1 <user3@localhost.localdomain>
EOF EOF
} }
@ -338,7 +340,8 @@ EOF
assert_success assert_success
assert_line --index 0 "user1@localhost.localdomain" assert_line --index 0 "user1@localhost.localdomain"
assert_line --index 1 "user2@otherdomain.tld" assert_line --index 1 "user2@otherdomain.tld"
assert_line --index 2 "added@localhost.localdomain" assert_line --index 2 "user3@localhost.localdomain"
assert_line --index 3 "added@localhost.localdomain"
} }
@test "checking accounts: user mail folder for user1" { @test "checking accounts: user mail folder for user1" {
@ -351,6 +354,11 @@ EOF
assert_success assert_success
} }
@test "checking accounts: user mail folder for user3" {
run docker exec mail /bin/bash -c "ls -d /var/mail/localhost.localdomain/user3/mail"
assert_success
}
@test "checking accounts: user mail folder for added user" { @test "checking accounts: user mail folder for added user" {
run docker exec mail /bin/bash -c "ls -d /var/mail/localhost.localdomain/added" run docker exec mail /bin/bash -c "ls -d /var/mail/localhost.localdomain/added"
assert_success assert_success