forked from szepeviktor/debian-server-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
change-mailpassword.sh
executable file
·59 lines (50 loc) · 1.77 KB
/
change-mailpassword.sh
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
#
# Change password of a Courier account.
#
# VERSION :0.1.1
# DATE :2015-08-10
# AUTHOR :Viktor Szépe <viktor@szepe.net>
# LICENSE :The MIT License (MIT)
# URL :https://github.com/szepeviktor/debian-server-tools
# BASH-VERSION :4.2+
# DEPENDS :apt-get install courier-authdaemon apg
# DEPENDS :/usr/local/share/password2remember/password2remember_hu.txt
# LOCATION :/usr/local/sbin/change-mailpassword.sh
COURIER_AUTH_DBNAME="horde4"
COURIER_AUTH_DBTABLE="courier_horde"
WORDLIST_HU="/usr/local/share/password2remember/password2remember_hu.txt"
Error() {
echo "ERROR: $*"
exit "$1"
}
set -e
if ! [ "$(id --user)" == 0 ]; then
Error 100 "Only root is allowed to add mail accounts."
fi
USER_NAME="$1"
if [ -z "$USER_NAME" ]; then
Error 1 "No account given."
fi
DEFAULT_PWD="$(apg -n 1 -M NC)"
# xkcd-style password
if [ -f "$WORDLIST_HU" ]; then
DEFAULT_PWD="$(xkcdpass -d . -w "$WORDLIST_HU" -n 4)"
fi
# Ask for password
read -r -e -p "New password for ${USER_NAME}? " -i "$DEFAULT_PWD" PASS || Error 1 "Invalid password"
# MySQL authentication
if which mysql &> /dev/null \
&& grep -q "^authmodulelist=.*\bauthmysql\b" /etc/courier/authdaemonrc; then
mysql "$COURIER_AUTH_DBNAME" <<EOF || Error 2 "Failed to update password"
-- USE ${COURIER_AUTH_DBNAME};
UPDATE \`${COURIER_AUTH_DBTABLE}\` SET \`crypt\`=ENCRYPT('${PASS}') WHERE \`id\`='${USER_NAME}';
EOF
fi
# Userdb authentication
if which userdb userdbpw &> /dev/null \
&& [ -r /etc/courier/userdb ] \
&& grep -q "^authmodulelist=.*\bauthuserdb\b" /etc/courier/authdaemonrc; then
echo "$PASS" | userdbpw -md5 | userdb "$USER_NAME" set systempw || Error 3 "Failed to update password"
makeuserdb || Error 4 "Failed to make userdb"
fi