blob: ea8a4a43c462190dbe55a2c6be54a1d5f72092ef [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001#!/usr/bin/env bash
2[[ -f mailcow.conf ]] && source mailcow.conf
3[[ -f ../mailcow.conf ]] && source ../mailcow.conf
4
5if [[ -z ${DBUSER} ]] || [[ -z ${DBPASS} ]] || [[ -z ${DBNAME} ]]; then
6 echo "Cannot find mailcow.conf, make sure this script is run from within the mailcow folder."
7 exit 1
8fi
9
10echo -n "Checking MySQL service... "
11if [[ -z $(docker ps -qf name=mysql-mailcow) ]]; then
12 echo "failed"
13 echo "MySQL (mysql-mailcow) is not up and running, exiting..."
14 exit 1
15fi
16
17echo "OK"
18read -r -p "Are you sure you want to reset the mailcow administrator account? [y/N] " response
19response=${response,,} # tolower
20if [[ "$response" =~ ^(yes|y)$ ]]; then
21 echo -e "\nWorking, please wait..."
Matthias Andreas Benkardd1f5b682023-11-18 13:18:30 +010022 random=$(</dev/urandom tr -dc _A-Z-a-z-0-9 2> /dev/null | head -c${1:-16})
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020023 password=$(docker exec -it $(docker ps -qf name=dovecot-mailcow) doveadm pw -s SSHA256 -p ${random} | tr -d '\r')
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010024 docker exec -it $(docker ps -qf name=mysql-mailcow) mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM admin WHERE username='admin';"
25 docker exec -it $(docker ps -qf name=mysql-mailcow) mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM domain_admins WHERE username='admin';"
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020026 docker exec -it $(docker ps -qf name=mysql-mailcow) mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "INSERT INTO admin (username, password, superadmin, active) VALUES ('admin', '${password}', 1, 1);"
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010027 docker exec -it $(docker ps -qf name=mysql-mailcow) mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM tfa WHERE username='admin';"
28 echo "
29Reset credentials:
30---
31Username: admin
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020032Password: ${random}
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010033TFA: none
34"
35else
36 echo "Operation canceled."
37fi