mailcow-reset-admin.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. [[ -f mailcow.conf ]] && source mailcow.conf
  3. [[ -f ../mailcow.conf ]] && source ../mailcow.conf
  4. if [[ -z ${DBUSER} ]] || [[ -z ${DBPASS} ]] || [[ -z ${DBNAME} ]]; then
  5. echo "Cannot find mailcow.conf, make sure this script is run from within the mailcow folder."
  6. exit 1
  7. fi
  8. echo -n "Checking MySQL service... "
  9. if [[ -z $(docker ps -qf name=mysql-mailcow) ]]; then
  10. echo "failed"
  11. echo "MySQL (mysql-mailcow) is not up and running, exiting..."
  12. exit 1
  13. fi
  14. echo "OK"
  15. read -r -p "Are you sure you want to reset the mailcow administrator account? [y/N] " response
  16. response=${response,,} # tolower
  17. if [[ "$response" =~ ^(yes|y)$ ]]; then
  18. echo -e "\nWorking, please wait..."
  19. random=$(</dev/urandom tr -dc _A-Z-a-z-0-9 2> /dev/null | head -c${1:-16})
  20. password=$(docker exec -it $(docker ps -qf name=dovecot-mailcow) doveadm pw -s SSHA256 -p ${random} | tr -d '\r')
  21. docker exec -it $(docker ps -qf name=mysql-mailcow) mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM admin WHERE username='admin';"
  22. docker exec -it $(docker ps -qf name=mysql-mailcow) mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM domain_admins WHERE username='admin';"
  23. 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);"
  24. docker exec -it $(docker ps -qf name=mysql-mailcow) mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "DELETE FROM tfa WHERE username='admin';"
  25. echo "
  26. Reset credentials:
  27. ---
  28. Username: admin
  29. Password: ${random}
  30. TFA: none
  31. "
  32. else
  33. echo "Operation canceled."
  34. fi