File: add-user.sh

package info (click to toggle)
chasquid 1.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,636 kB
  • sloc: sh: 1,262; python: 325; makefile: 78
file content (43 lines) | stat: -rwxr-xr-x 961 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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"