Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker/add-user.sh: Don't crash on updating when there is a single user #43

Closed
wants to merge 2 commits into from

Conversation

erjoalgo
Copy link
Contributor

When a single dovecot user exists and their password is updated,
the grep -v command intended to remove the user's old password
will not match any lines and fail, causing the entire script to fail.

@albertito albertito self-assigned this Oct 29, 2023
@albertito
Copy link
Owner

Hi! Thank you for sending this!

While using sed -i to remove the line seems reasonable in principle, I am not sure if I understand the bug this is fixing.

I am worried that what makes the change work for you could be the removal of the : at the end of the expression, which can be a problem because the wrong email could be removed (e.g. if you have a user abc and an abcde).

So I think I need to understand the problem better.

Can you send the contents of the users file the current script is failing on? How did you generate it?

Thank you!

@erjoalgo erjoalgo force-pushed the fix-dovecot-sole-user-bug branch from b556f70 to 9157305 Compare October 29, 2023 13:14
@erjoalgo
Copy link
Contributor Author

Here is an example repro script:

#!/bin/bash -x

set -e

# prelude setting up /data/dovecot/users
sudo mkdir -p /data/dovecot/
sudo chown -R $(whoami) /data
cat <<EOF > /data/dovecot/users
[email protected]:{CRYPT}$2y$05$gTTsE0TKRLke8d8H/qIKhfyQf0HCdZ17mh975LdHfTMZbPhmVQ/56::::
EOF
[email protected]

# code from add-users.sh
mkdir -p /data/dovecot
touch /data/dovecot/users
if grep -q "^${EMAIL}:" /data/dovecot/users; then
	cp /data/dovecot/users /data/dovecot/users.old
        # the next command will fail
	grep -v "^${EMAIL}:" /data/dovecot/users.old \
		> /data/dovecot/users
fi

echo "success" # this line never runs

Basically the grep -v command will fail because after removing the email in question, there are no lines left.

The removal of the : was not intended, and I've added it back and updated the PR.

@albertito
Copy link
Owner

albertito commented Oct 29, 2023

Thank you for the details!

Here is an example repro script:

cat <<EOF > /data/dovecot/users
[email protected]:{CRYPT}$2y$05$gTTsE0TKRLke8d8H/qIKhfyQf0HCdZ17mh975LdHfTMZbPhmVQ/56::::
EOF

[...]

if grep -q "^${EMAIL}:" /data/dovecot/users; then
	cp /data/dovecot/users /data/dovecot/users.old
        # the next command will fail
	grep -v "^${EMAIL}:" /data/dovecot/users.old \
		> /data/dovecot/users
fi

Basically the grep -v command will fail because after removing the email in question, there are no lines left.

Oooohhhh I see, the problem is the exit value of grep -v:

$ echo abcd:efg > xxx
$ grep -v '^abcd:' xxx; echo $?
1
$ grep -v '^pppp:' xxx; echo $?
abcd:efg
0

Thank you, now I get where the problem is. sed -i seems like a more practical way to implement this.

I'll have one comment inline on the patch, but this seems fine to me, thank you!

The removal of the : was not intended, and I've added it back and updated the PR.

Great, thanks! Glad to rule that out too.

grep -v "^${EMAIL}:" /data/dovecot/users.old \
> /data/dovecot/users
fi
sed -i "/^${EMAIL}:/d" /data/dovecot/users
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use sed --in-place=.old instead of sed -i, so it leaves the previous file around just in case there are issues?

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -7,15 +7,14 @@

set -e

read -r -p "Email (full user@domain format): " EMAIL
test -n "${EMAIL:-}" || read -r -p "Email (full user@domain format): " EMAIL
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace this with:

if test -z "${EMAIL}"; then
        read -r -p "Email (full user@domain format): " EMAIL
fi

It should be the same effect, and I am okay with the change, I just find this form more readable in this case.

Is that okay with you? What do you think?

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@erjoalgo erjoalgo force-pushed the fix-dovecot-sole-user-bug branch from 9157305 to a937900 Compare October 29, 2023 15:07
When a single dovecot user exists and their password is updated,
the `grep -v` command intended to remove the user's old password
will not match any lines and fail, causing the entire script to fail.
@erjoalgo erjoalgo force-pushed the fix-dovecot-sole-user-bug branch from a937900 to f89bb2b Compare October 29, 2023 15:11
albertito pushed a commit that referenced this pull request Oct 29, 2023
…bles

This patch extends docker/add-user.sh to support getting the email and
password from environment variables.

That way, docker/add-user.sh can be used in scripts.

#43

Amended-by: Alberto Bertogli <[email protected]>
  Minor edits to the commit message.
albertito pushed a commit that referenced this pull request Oct 29, 2023
When a single dovecot user exists and their password is being updated via
docker/add-user.sh, the `grep -v` command intended to remove the user's
old password will not match any lines and exit with error code 1, causing
the entire script to fail.

This patch fixes it by replacing the if-grep logic with a simpler sed
invocation.

#43

Amended-by: Alberto Bertogli <[email protected]>
  Minor edits to the commit message.
@albertito albertito changed the title Fix dovecot sole user bug docker/add-user.sh: Don't crash on updating when there is a single user Oct 29, 2023
@albertito albertito added bug fixed in next The "next" branch contains a fix labels Oct 29, 2023
@albertito
Copy link
Owner

Thanks a lot for the explanation and the updated patches!

I've merged them to next with minor edits to the commit messages, please take a look and tell me if there's something you prefer me to change. They are a0f0930 and 0ce84a3.

Thanks again!

@erjoalgo
Copy link
Contributor Author

Looks great to me. Thanks for the quick response.

@albertito albertito closed this Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fixed in next The "next" branch contains a fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants