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"
exit 1
usage() {
echo "Usage: addmailuser <user@domain> [<password>]"
} }
if [ ! -z "$1" ]; then errex() {
USER=$1 echo "$@" 1>&2
if [ -e "$DATABASE" ] && [ ! -z "$(grep $USER -i $DATABASE)" ]; then exit 1
echo "User already exists" }
exit 1
fi escape() {
if [ ! -z "$2" ]; then echo "${1//./\\.}"
PASS="$2" }
else
read -s -p "Enter Password: " PASS [ -z "$USER" ] && { usage; errex "no username specified"; }
if [ -z "$PASS" ]; then
echo "Password can't be empty" grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null &&
exit 1 errex "User \"$USER\" already exists"
fi
fi if [ -z "$PASSWD" ]; then
ENTRY=$(echo "$USER|$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASS")") read -s -p "Enter Password: " PASSWD
echo "$ENTRY" >> $DATABASE echo
else [ -z "$PASSWD" ] && errex "Password must not be empty"
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>"
exit 1 usage() {
echo "Usage: delmailuser <user@domain>"
} }
if [ ! -z "$1" ]; then errex() {
USER=$1 echo "$@" 1>&2
if [ -f "$DATABASE" ]; then exit 1
ENTRIES=$(grep "$USER" -vi $DATABASE) }
echo "$ENTRIES" > $DATABASE
fi escape() {
else echo "${1//./\\.}"
usage }
fi
[ -z "$USER" ] && { usage; errex "No user specifed"; }
[ -s "$DATABASE" ] || exit 0
# XXX $USER must not contain /s and other syntactic characters
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"
exit 1
usage() {
echo "Usage: updatemailuser <user@domain.tld> [password]"
} }
if [ ! -z "$1" ]; then errex() {
USER=$1 echo "$@" 1>&2
if [ -e "$DATABASE" ] && [ -z "$(grep $USER -i $DATABASE)" ]; then exit 1
echo "User doesn't exist" }
exit 1
fi escape() {
if [ ! -z "$2" ]; then echo "${1//./\\.}"
PASS="$2" }
else
read -s -p "Enter Password: " PASS [ -z "$USER" ] && { usage; errex "no username specified"; }
if [ -z "$PASS" ]; then
echo "Password can't be empty" grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null ||
exit 1 errex "User \"$USER\" does not exist"
fi
fi delmailuser "$USER"
ENTRY=$(echo "$USER|$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASS")") addmailuser "$USER" "$PASSWD"
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'