Fix mailuser tools (#441)

* Add some checks for user name matching in mail user scripts
* Fix user matching problems in mail user scripts
** fix matching problems at several places:
    "delmailuser a@example.com" deletes also user "aa@example.com"
    "delmailuser a@sub.example.com" deletes also user "a@sub-example.com"
** similar problems when inserting
** refactor and clean up
This commit is contained in:
Wolfgang Ocker 2016-12-30 20:06:44 +01:00 committed by Thomas VIAL
parent de70a155f2
commit 461c88e6ae
5 changed files with 109 additions and 84 deletions

View file

@ -1,29 +1,32 @@
#! /bin/bash #! /bin/bash
DATABASE=/tmp/docker-mailserver/postfix-accounts.cf DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
function usage { USER="$1"
echo 'Usage: addmailuser <user@domain.tld> [password]' PASSWD="$2"
usage() {
echo "Usage: addmailuser <user@domain> [<password>]"
}
errex() {
echo "$@" 1>&2
exit 1 exit 1
} }
if [ ! -z "$1" ]; then escape() {
USER=$1 echo "${1//./\\.}"
if [ -e "$DATABASE" ] && [ ! -z "$(grep $USER -i $DATABASE)" ]; then }
echo "User already exists"
exit 1 [ -z "$USER" ] && { usage; errex "no username specified"; }
fi
if [ ! -z "$2" ]; then grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null &&
PASS="$2" errex "User \"$USER\" already exists"
else
read -s -p "Enter Password: " PASS if [ -z "$PASSWD" ]; then
if [ -z "$PASS" ]; then read -s -p "Enter Password: " PASSWD
echo "Password can't be empty" echo
exit 1 [ -z "$PASSWD" ] && errex "Password must not be empty"
fi
fi
ENTRY=$(echo "$USER|$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASS")")
echo "$ENTRY" >> $DATABASE
else
usage
fi fi
echo "$USER|$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASSWD")" >>$DATABASE

View file

@ -1,18 +1,24 @@
#! /bin/bash #! /bin/bash
DATABASE=/tmp/docker-mailserver/postfix-accounts.cf DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
function usage { USER="$1"
echo "Usage: delmailuser <user@domain.tld>"
usage() {
echo "Usage: delmailuser <user@domain>"
}
errex() {
echo "$@" 1>&2
exit 1 exit 1
} }
if [ ! -z "$1" ]; then escape() {
USER=$1 echo "${1//./\\.}"
if [ -f "$DATABASE" ]; then }
ENTRIES=$(grep "$USER" -vi $DATABASE)
echo "$ENTRIES" > $DATABASE [ -z "$USER" ] && { usage; errex "No user specifed"; }
fi [ -s "$DATABASE" ] || exit 0
else
usage # XXX $USER must not contain /s and other syntactic characters
fi sed -i "/^$(escape "$USER")|/d" $DATABASE

View file

@ -1,16 +1,13 @@
#! /bin/sh #! /bin/bash
DATABASE=/tmp/docker-mailserver/postfix-accounts.cf DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
if [ ! -f "$DATABASE" ]; then errex() {
echo "The configuration file 'postfix-accounts.cf' doesn't exist. Until now no email addresses have been added." echo "$@" 1>&2
exit 1 exit 1
fi }
if [ ! -s "$DATABASE" ]; then [ -f $DATABASE ] || errex "No postfix-accounts.cf file"
echo "No email addresses have been added." [ -s $DATABASE ] || errex "Empty postfix-accounts.cf - no users have been added"
exit 1
fi
cat "$DATABASE" | awk -F '|' '{print $1}'
awk -F '|' '{ print $1; }' $DATABASE

View file

@ -1,29 +1,27 @@
#! /bin/bash #! /bin/bash
DATABASE=/tmp/docker-mailserver/postfix-accounts.cf DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
function usage { USER="$1"
echo 'Usage: updatemailuser <user@domain.tld> [password]' PASSWD="$2"
usage() {
echo "Usage: updatemailuser <user@domain.tld> [password]"
}
errex() {
echo "$@" 1>&2
exit 1 exit 1
} }
if [ ! -z "$1" ]; then escape() {
USER=$1 echo "${1//./\\.}"
if [ -e "$DATABASE" ] && [ -z "$(grep $USER -i $DATABASE)" ]; then }
echo "User doesn't exist"
exit 1 [ -z "$USER" ] && { usage; errex "no username specified"; }
fi
if [ ! -z "$2" ]; then grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null ||
PASS="$2" errex "User \"$USER\" does not exist"
else
read -s -p "Enter Password: " PASS delmailuser "$USER"
if [ -z "$PASS" ]; then addmailuser "$USER" "$PASSWD"
echo "Password can't be empty"
exit 1
fi
fi
ENTRY=$(echo "$USER|$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASS")")
sed -i.bak "s%^$USER.*%$ENTRY%g" $DATABASE
else
usage
fi

View file

@ -653,27 +653,47 @@
@test "checking accounts: user3 should have been added to /tmp/docker-mailserver/postfix-accounts.cf" { @test "checking accounts: user3 should have been added to /tmp/docker-mailserver/postfix-accounts.cf" {
docker exec mail /bin/sh -c "addmailuser user3@domain.tld mypassword" docker exec mail /bin/sh -c "addmailuser user3@domain.tld mypassword"
run docker exec mail /bin/sh -c "grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf" run docker exec mail /bin/sh -c "grep '^user3@domain\.tld|' -i /tmp/docker-mailserver/postfix-accounts.cf"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ ! -z "$output" ] [ ! -z "$output" ]
} }
@test "checking accounts: user3 should have been removed from /tmp/docker-mailserver/postfix-accounts.cf" { @test "checking accounts: auser3 should have been added to /tmp/docker-mailserver/postfix-accounts.cf" {
docker exec mail /bin/sh -c "addmailuser auser3@domain.tld mypassword"
run docker exec mail /bin/sh -c "grep '^auser3@domain\.tld|' -i /tmp/docker-mailserver/postfix-accounts.cf"
[ "$status" -eq 0 ]
[ ! -z "$output" ]
}
@test "checking accounts: a.ser3 should have been added to /tmp/docker-mailserver/postfix-accounts.cf" {
docker exec mail /bin/sh -c "addmailuser a.ser3@domain.tld mypassword"
run docker exec mail /bin/sh -c "grep '^a\.ser3@domain\.tld|' -i /tmp/docker-mailserver/postfix-accounts.cf"
[ "$status" -eq 0 ]
[ ! -z "$output" ]
}
@test "checking accounts: user3 should have been removed from /tmp/docker-mailserver/postfix-accounts.cf but not auser3" {
docker exec mail /bin/sh -c "delmailuser user3@domain.tld" docker exec mail /bin/sh -c "delmailuser user3@domain.tld"
run docker exec mail /bin/sh -c "grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf" run docker exec mail /bin/sh -c "grep '^user3@domain\.tld' -i /tmp/docker-mailserver/postfix-accounts.cf"
[ "$status" -eq 1 ] [ "$status" -eq 1 ]
[ -z "$output" ] [ -z "$output" ]
run docker exec mail /bin/sh -c "grep '^auser3@domain\.tld' -i /tmp/docker-mailserver/postfix-accounts.cf"
[ "$status" -eq 0 ]
[ ! -z "$output" ]
} }
@test "checking user updating password for user in /tmp/docker-mailserver/postfix-accounts.cf" { @test "checking user updating password for user in /tmp/docker-mailserver/postfix-accounts.cf" {
docker exec mail /bin/sh -c "addmailuser user3@domain.tld mypassword" docker exec mail /bin/sh -c "addmailuser user4@domain.tld mypassword"
initialpass=$(run docker exec mail /bin/sh -c "grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf") initialpass=$(run docker exec mail /bin/sh -c "grep '^user4@domain\.tld' -i /tmp/docker-mailserver/postfix-accounts.cf")
sleep 2 sleep 2
docker exec mail /bin/sh -c "updatemailuser user3@domain.tld mynewpassword" docker exec mail /bin/sh -c "updatemailuser user4@domain.tld mynewpassword"
sleep 2 sleep 2
changepass=$(run docker exec mail /bin/sh -c "grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf") changepass=$(run docker exec mail /bin/sh -c "grep '^user4@domain\.tld' -i /tmp/docker-mailserver/postfix-accounts.cf")
if [ initialpass != changepass ]; then if [ initialpass != changepass ]; then
status="0" status="0"
@ -681,7 +701,7 @@
status="1" status="1"
fi fi
docker exec mail /bin/sh -c "delmailuser user3@domain.tld" docker exec mail /bin/sh -c "delmailuser auser3@domain.tld"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
} }
@ -705,6 +725,7 @@
run docker run --rm \ run docker run --rm \
-v "$(pwd)/test/config/without-accounts/":/tmp/docker-mailserver/ \ -v "$(pwd)/test/config/without-accounts/":/tmp/docker-mailserver/ \
`docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'addmailuser user3@domain.tld mypassword' `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'addmailuser user3@domain.tld mypassword'
[ "$status" -eq 0 ]
run docker run --rm \ run docker run --rm \
-v "$(pwd)/test/config/without-accounts/":/tmp/docker-mailserver/ \ -v "$(pwd)/test/config/without-accounts/":/tmp/docker-mailserver/ \
`docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf' `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf'