Skip to content

Commit b358d71

Browse files
committedApr 16, 2024·
Fix for #192: Automatically add domains to provided usernames for SASL
So, according to the documentation, usernames must always include a domain for SASL. In other words. User cannot be `johhny` but `[email protected]`. Further info can be found on this ticket: #192 This commit will automatically append domain if one is not provided in `SMTPD_SASL_USERS`.
1 parent bda13b3 commit b358d71

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed
 

‎README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,14 @@ To change the log format, set the (unsurprisingly named) variable `LOG_FORMAT=js
209209
- `XOAUTH2_INITIAL_ACCESS_TOKEN` = Initial OAuth2 access token.
210210
- `XOAUTH2_INITIAL_REFRESH_TOKEN` = Initial OAuth2 refresh token.
211211
- `XOAUTH2_TOKEN_ENDPOINT` = Token endpoint provided four your XOAUTH App , GMail use : https://accounts.google.com/o/oauth2/token
212-
- `SMTPD_SASL_USERS` = Users allow to send mail (ex: user1:pass1,user2:pass2,...)
212+
- `SMTPD_SASL_USERS` = Users allow to send mail (ex: user1:pass1,user2:pass2,...). *Warning:* Users need to be specified with a domain, as explained
213+
on ticket [[#192](https://github.com/bokysan/docker-postfix/issues/192)]. This image will automatically add a domain if one is not provided and will
214+
issue a notice when that happens.
213215
- `MASQUERADED_DOMAINS` = domains where you want to masquerade internal hosts
214216
- `SMTP_HEADER_CHECKS`= Set to `1` to enable header checks of to a location of the file for header checks
215217
- `POSTFIX_myhostname` = Set the name of this postfix server
216218
- `POSTFIX_mynetworks` = Allow sending mails only from specific networks ( default `127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16` )
217-
- `POSTFIX_message_size_limit` = The maximum size of the messsage, in bytes, by default it's unlimited
219+
- `POSTFIX_message_size_limit` = The maximum size of the message, in bytes, by default it's unlimited
218220
- `POSTFIX_<any_postfix_setting>` = provide any additional postfix setting
219221

220222
#### `RELAYHOST`, `RELAYHOST_USERNAME` and `RELAYHOST_PASSWORD`

‎scripts/common-run.sh

+29-5
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ postfix_setup_xoauth2_post_setup() {
424424
}
425425

426426
postfix_setup_smtpd_sasl_auth() {
427+
local first_bad_user bad_users mydomain message
428+
local _user _pwd
427429
if [ ! -z "$SMTPD_SASL_USERS" ]; then
428430
info "Enable smtpd sasl auth."
429431
do_postconf -e "smtpd_sasl_auth_enable=yes"
@@ -435,19 +437,41 @@ pwcheck_method: auxprop
435437
auxprop_plugin: sasldb
436438
mech_list: PLAIN LOGIN CRAM-MD5 DIGEST-MD5 NTLM
437439
EOF
438-
[ ! -d /etc/sasl2 ] && mkdir /etc/sasl2
439-
ln -s /etc/postfix/sasl/smtpd.conf /etc/sasl2/
440+
[[ ! -d /etc/sasl2 ]] && mkdir /etc/sasl2
441+
ln -s -f /etc/postfix/sasl/smtpd.conf /etc/sasl2/
440442

443+
bad_users=""
444+
mydomain="$(postconf -h mydomain)"
441445
# sasldb2
442446
echo $SMTPD_SASL_USERS | tr , \\n > /tmp/passwd
443447
while IFS=':' read -r _user _pwd; do
444-
echo $_pwd | saslpasswd2 -p -c $_user
448+
# Fix for issue https://github.com/bokysan/docker-postfix/issues/192
449+
if [[ "$_user" = *@* ]]; then
450+
echo $_pwd | saslpasswd2 -p -c $_user
451+
else
452+
if [[ -z "$bad_users" ]]; then
453+
bad_users="${emphasis}${_user}${reset}"
454+
first_bad_user="${_user}"
455+
else
456+
bad_users="${bad_users},${emphasis}${_user}${reset}"
457+
fi
458+
echo $_pwd | saslpasswd2 -p -c -u $mydomain $_user
459+
fi
445460
done < /tmp/passwd
446461

447462
rm -f /tmp/passwd
448463

449-
[ -f /etc/sasldb2 ] && chown postfix:postfix /etc/sasldb2
450-
[ -f /etc/sasl2/sasldb2 ] && chown postfix:postfix /etc/sasl2/sasldb2
464+
[[ -f /etc/sasldb2 ]] && chown postfix:postfix /etc/sasldb2
465+
[[ -f /etc/sasl2/sasldb2 ]] && chown postfix:postfix /etc/sasl2/sasldb2
466+
467+
if [[ -n "$bad_users" ]]; then
468+
notice "$(printf '%s' \
469+
"Some SASL users (${bad_users}) were specified without the domain. Container domain (${emphasis}${mydomain}${reset}) was automatically applied. " \
470+
"If this was an intended behavour, you can safely ignore this message. To prevent the message in the future, specify your usernames with domain " \
471+
"name, e.g. ${emphasis}${first_bad_user}@${mydomain}:<pass>${reset}. For more info, see https://github.com/bokysan/docker-postfix/issues/192"
472+
)"
473+
fi
474+
451475
debug 'Sasldb configured'
452476
fi
453477
}

‎unit-tests/000_test-multi-comment.bats

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
load /code/scripts/common.sh
44
load /code/scripts/common-run.sh
55

6-
#
6+
77
postconf daemon_directory=/usr/libexec/postfix
88

99
if [[ ! -f /etc/postfix/main.test-multi-comment ]]; then
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bats
2+
3+
load /code/scripts/common.sh
4+
load /code/scripts/common-run.sh
5+
6+
@test "check if SMTPD_SASL_USERS works with and without domain" {
7+
local db_file
8+
local SMTPD_SASL_USERS="hello:world,foo@example.com:bar"
9+
do_postconf -e 'mydomain=example.org'
10+
postfix_setup_smtpd_sasl_auth
11+
12+
postfix check
13+
14+
[[ -f /etc/postfix/sasl/smtpd.conf ]]
15+
[[ -f /etc/sasl2/smtpd.conf ]]
16+
[[ -f /etc/sasldb2 ]] || [[ -f /etc/sasl2/sasldb2 ]]
17+
18+
sasldblistusers2 | grep -qE "^hello@example.org:"
19+
sasldblistusers2 | grep -qE "^foo@example.com:"
20+
21+
}
22+

0 commit comments

Comments
 (0)
Please sign in to comment.