#!/usr/bin/env python3 """ Password Reset Script for Mailcow Alias Manager Usage: python3 reset_password.py [new_password] """ import sys import sqlite3 import bcrypt from pathlib import Path # Database location project_dir = Path(__file__).parent filepath = project_dir.joinpath('data') database = filepath.joinpath('malias2.db') def reset_password(new_password): """Reset the admin password""" if not database.exists(): print(f"❌ Error: Database not found at {database}") print(" Make sure the application has been run at least once to create the database.") return False try: # Hash the new password password_hash = bcrypt.hashpw(new_password.encode('utf-8'), bcrypt.gensalt()) # Update database conn = sqlite3.connect(database) cursor = conn.cursor() # Check if auth table exists cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='auth'") if not cursor.fetchone(): print("❌ Error: Auth table not found in database") conn.close() return False # Update password cursor.execute('UPDATE auth SET password_hash = ? WHERE id = 0', (password_hash.decode('utf-8'),)) # Verify update if cursor.rowcount == 0: print("❌ Error: No password record found. Creating new one...") cursor.execute('INSERT INTO auth VALUES (?, ?)', (0, password_hash.decode('utf-8'))) conn.commit() conn.close() print("=" * 60) print(" ✅ Password Reset Successful!") print("=" * 60) print(f" New password: {new_password}") print(f" Database: {database}") print("=" * 60) print() print("You can now login with the new password.") print() return True except Exception as e: print(f"❌ Error resetting password: {e}") return False def main(): print() print("=" * 60) print(" Mailcow Alias Manager - Password Reset") print("=" * 60) print() # Get new password if len(sys.argv) > 1: new_password = sys.argv[1] else: print("Enter new password (or press Ctrl+C to cancel):") new_password = input("New password: ").strip() if not new_password: print("❌ Error: Password cannot be empty") return 1 if len(new_password) < 6: print("⚠️ Warning: Password is less than 6 characters") response = input("Continue anyway? (y/n): ").strip().lower() if response != 'y': print("Password reset cancelled.") return 0 print() if reset_password(new_password): return 0 else: return 1 if __name__ == '__main__': try: sys.exit(main()) except KeyboardInterrupt: print("\n\nPassword reset cancelled.") sys.exit(0)