Files
chasquid/docker/add-user.sh
Ernesto Alfonso a0f09308ed docker/add-user.sh: Don't crash on updating when there is a single user
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.

https://github.com/albertito/chasquid/pull/43

Amended-by: Alberto Bertogli <albertito@blitiri.com.ar>
  Minor edits to the commit message.
2023-10-29 22:28:08 +00:00

44 lines
961 B
Bash
Executable File

#!/bin/bash
#
# Creates a user. If it exists, updates the password.
#
# Note this is not robust, it's only for convenience on extremely simple
# setups.
set -e
if test -z "${EMAIL:-}"; then
read -r -p "Email (full user@domain format): " EMAIL
fi
if ! echo "${EMAIL}" | grep -q @; then
echo "Error: email should have '@'."
exit 1
fi
if test -z "${PASSWORD:-}"; then
read -r -p "Password: " -s PASSWORD
echo
fi
DOMAIN=$(echo echo "${EMAIL}" | cut -d '@' -f 2)
# If the domain doesn't exist in chasquid's config, create it.
mkdir -p "/data/chasquid/domains/${DOMAIN}/"
# Encrypt password.
ENCPASS=$(doveadm pw -u "${EMAIL}" -p "${PASSWORD}")
# Edit dovecot users: remove user if it exits.
mkdir -p /data/dovecot
touch /data/dovecot/users
sed --in-place=.old "/^${EMAIL}:/d" /data/dovecot/users
# Edit dovecot users: add user.
echo "${EMAIL}:${ENCPASS}::::" >> /data/dovecot/users
echo "${EMAIL} added to /data/dovecot/users"