Protect user db with flock

This commit is contained in:
Erik Wramner 2019-08-01 19:39:25 +02:00
parent d6861881ab
commit 81e9c7dcff
5 changed files with 68 additions and 38 deletions

View file

@ -22,13 +22,20 @@ escape() {
[ -z "$USER" ] && { usage; errex "no username specified"; } [ -z "$USER" ] && { usage; errex "no username specified"; }
expr index "$USER" "@" >/dev/null || { usage; errex "username must include the domain"; } expr index "$USER" "@" >/dev/null || { usage; errex "username must include the domain"; }
grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null && # Protect config file with lock to avoid race conditions
errex "User \"$USER\" already exists" touch $DATABASE
(
flock -e 200
if [ -z "$PASSWD" ]; then grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null &&
read -s -p "Enter Password: " PASSWD errex "User \"$USER\" already exists"
echo
[ -z "$PASSWD" ] && errex "Password must not be empty" if [ -z "$PASSWD" ]; then
fi read -s -p "Enter Password: " PASSWD
HASH="$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASSWD")" echo
echo "$USER|$HASH" >> $DATABASE [ -z "$PASSWD" ] && errex "Password must not be empty"
fi
HASH="$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASSWD")"
echo "$USER|$HASH" >> $DATABASE
) 200<$DATABASE

View file

@ -33,25 +33,31 @@ shift $((OPTIND-1))
[ -z "$@" ] && { usage; errex "No user specifed"; } [ -z "$@" ] && { usage; errex "No user specifed"; }
[ -s "$DATABASE" ] || exit 0 [ -s "$DATABASE" ] || exit 0
for USER in "$@"; do # Protect config file with lock to avoid race conditions
#very simple plausibility check (
[[ "$USER" != *"@"*"."* ]] && errex "No valid address: $USER" flock -e 200
MAILARR=(${USER//@/ })
# XXX $USER must not contain /s and other syntactic characters for USER in "$@"; do
USER=$(escape "$USER") # very simple plausibility check
sed -i "/^"$USER"|/d" $DATABASE [[ "$USER" != *"@"*"."* ]] && errex "No valid address: $USER"
[ $? != 0 ] && errex "$USER couldn't be deleted in $DATABASE. $?" MAILARR=(${USER//@/ })
# Delete all aliases where the user is the only recipient( " $USER$" ) # XXX $USER must not contain /s and other syntactic characters
# Delete user only for all aliases that deliver to multiple recipients ( ",$USER" "$USER," ) USER=$(escape "$USER")
sed -i -e "/ "$USER"$/d" \ sed -i "/^"$USER"|/d" $DATABASE
-e "s/,"$USER"//g" \ [ $? != 0 ] && errex "$USER couldn't be deleted in $DATABASE. $?"
-e "s/"$USER",//g" $ALIAS_DATABASE # Delete all aliases where the user is the only recipient( " $USER$" )
[ $? = 0 ] && echo "$USER and potential aliases deleted." || errex "Aliases for $USER couldn't be deleted in $ALIAS_DATABASE. $?" # Delete user only for all aliases that deliver to multiple recipients ( ",$USER" "$USER," )
if [ "$MAILDEL" != "y" ]; then sed -i -e "/ "$USER"$/d" \
read -p "Do you want to delete the maildir as well(all mails will be removed)?(y/n) " MAILDEL -e "s/,"$USER"//g" \
echo -e "s/"$USER",//g" $ALIAS_DATABASE
fi [ $? = 0 ] && echo "$USER and potential aliases deleted." || errex "Aliases for $USER couldn't be deleted in $ALIAS_DATABASE. $?"
[ "$MAILDEL" != "y" ] && errex "Leaving the maildir untouched. If you want to delete it at a later point use \"sudo docker exec mail rm -R /var/mail/${MAILARR[1]}/${MAILARR[0]}\"" if [ "$MAILDEL" != "y" ]; then
rm -r -f /var/mail/${MAILARR[1]}/${MAILARR[0]} read -p "Do you want to delete the maildir as well(all mails will be removed)?(y/n) " MAILDEL
[ $? = 0 ] && echo "Maildir deleted." || errex "Maildir couldn't be deleted: $?" echo
done fi
[ "$MAILDEL" != "y" ] && errex "Leaving the maildir untouched. If you want to delete it at a later point use \"sudo docker exec mail rm -R /var/mail/${MAILARR[1]}/${MAILARR[0]}\""
rm -r -f /var/mail/${MAILARR[1]}/${MAILARR[0]}
[ $? = 0 ] && echo "Maildir deleted." || errex "Maildir couldn't be deleted: $?"
done
) 200<$DATABASE

View file

@ -10,4 +10,9 @@ errex() {
[ -f $DATABASE ] || errex "No postfix-accounts.cf file" [ -f $DATABASE ] || errex "No postfix-accounts.cf file"
[ -s $DATABASE ] || errex "Empty postfix-accounts.cf - no users have been added" [ -s $DATABASE ] || errex "Empty postfix-accounts.cf - no users have been added"
awk -F '|' '{ print $1; }' $DATABASE # Lock database even though we are only reading
(
flock -e 200
awk -F '|' '{ print $1; }' $DATABASE
) 200<$DATABASE

View file

@ -27,7 +27,13 @@ if [ -z "$PASSWD" ]; then
[ -z "$PASSWD" ] && errex "Password must not be empty" [ -z "$PASSWD" ] && errex "Password must not be empty"
fi fi
HASH="$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASSWD")" HASH="$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASSWD")"
grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null ||
errex "User \"$USER\" does not exist"
sed -i "s ^"$USER"|.* "$USER"|"$HASH" " $DATABASE # Protect config file with lock to avoid race conditions
(
flock -e 200
grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null ||
errex "User \"$USER\" does not exist"
sed -i "s ^"$USER"|.* "$USER"|"$HASH" " $DATABASE
) 200<$DATABASE

View file

@ -37,8 +37,7 @@ for file in postfix-accounts.cf postfix-virtual.cf postfix-aliases.cf; do
done done
# Wait to make sure server is up before we start # Wait to make sure server is up before we start
# Plus the files have just been generated, no hurry to process changes sleep 10
sleep 20
# Run forever # Run forever
while true; do while true; do
@ -46,7 +45,7 @@ while true; do
# recreate logdate # recreate logdate
log_date=$(date +"%Y-%m-%d %H:%M:%S ") log_date=$(date +"%Y-%m-%d %H:%M:%S ")
# Get chksum and check it. # Get chksum and check it, no need to lock config yet
chksum=$(sha512sum -c --ignore-missing $CHKSUM_FILE) chksum=$(sha512sum -c --ignore-missing $CHKSUM_FILE)
if [[ $chksum == *"FAIL"* ]]; then if [[ $chksum == *"FAIL"* ]]; then
@ -57,6 +56,11 @@ if [[ $chksum == *"FAIL"* ]]; then
# Also note that changes are performed in place and are not atomic # Also note that changes are performed in place and are not atomic
# We should fix that and write to temporary files, stop, swap and start # We should fix that and write to temporary files, stop, swap and start
# Lock configuration while working
# Not fixing indentation yet to reduce diff (fix later in separate commit)
(
flock -e 200
#regen postix aliases. #regen postix aliases.
echo "root: ${PM_ADDRESS}" > /etc/aliases echo "root: ${PM_ADDRESS}" > /etc/aliases
if [ -f /tmp/docker-mailserver/postfix-aliases.cf ]; then if [ -f /tmp/docker-mailserver/postfix-aliases.cf ]; then
@ -195,6 +199,8 @@ if [[ $chksum == *"FAIL"* ]]; then
echo "${log_date} Update checksum" echo "${log_date} Update checksum"
sha512sum ${cf_files[@]/#/--tag } >$CHKSUM_FILE sha512sum ${cf_files[@]/#/--tag } >$CHKSUM_FILE
) 200<postfix-accounts.cf # end lock
fi fi
sleep 1 sleep 1