#!/usr/bin/env bash set -uo pipefail if [[ -z "${SEA_ADMIN_EMAIL:-}" || -z "${SEA_ADMIN_PASSWORD:-}" ]]; then echo "SEA_ADMIN_EMAIL and SEA_ADMIN_PASSWORD are required." >&2 exit 1 fi set +e /www/server/panel/pyenv/bin/python3 - <<'PY' import json import os import re import stat import subprocess import tempfile from pathlib import Path from urllib.parse import unquote email = os.environ['SEA_ADMIN_EMAIL'].strip().lower() password = os.environ['SEA_ADMIN_PASSWORD'] if not re.fullmatch(r'[^\s@]+@[^\s@]+\.[^\s@]+', email): raise SystemExit('administrator email is invalid') if len(password) < 14 or len(password) > 128: raise SystemExit('administrator password length is invalid') config_path = Path('/var/lib/sea-vhost/config.json') if not config_path.is_file() or config_path.is_symlink(): raise SystemExit('Sea vHost configuration is missing or unsafe') config = json.loads(config_path.read_text(encoding='utf-8')) dsn = config.get('database_dsn', '') match = re.fullmatch(r'([^:]+):([^@]+)@tcp\(([^:()]+):(\d+)\)/([^?]+)(?:\?.*)?', dsn) if not match: raise SystemExit('Sea vHost database DSN has an unexpected format') db_user, db_password, db_host, db_port, db_name = (unquote(value) for value in match.groups()) php_binary = next((path for path in ('/www/server/php/84/bin/php', '/www/server/php/83/bin/php', '/usr/bin/php') if Path(path).is_file()), None) mysql_binary = next((path for path in ('/www/server/mysql/bin/mysql', '/usr/bin/mysql', '/usr/bin/mariadb') if Path(path).is_file()), None) if not php_binary or not mysql_binary: raise SystemExit('PHP or MySQL client is unavailable') hash_environment = os.environ.copy() hash_environment['SEA_RESET_PASSWORD'] = password password_hash = subprocess.check_output( [php_binary, '-r', 'echo password_hash(getenv("SEA_RESET_PASSWORD"), PASSWORD_BCRYPT);'], env=hash_environment, text=True, ).strip() if not re.fullmatch(r'\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}', password_hash): raise SystemExit('PHP returned an invalid bcrypt hash') escaped_email = email.replace("'", "''") sql = ( f"UPDATE users SET password_hash='{password_hash}',status='active' " f"WHERE role='admin' AND LOWER(email)='{escaped_email}';" f"DELETE s FROM sessions s JOIN users u ON u.id=s.user_id " f"WHERE u.role='admin' AND LOWER(u.email)='{escaped_email}';" "SELECT COUNT(*) FROM users WHERE role='admin' AND status='active' " f"AND LOWER(email)='{escaped_email}';" ) mysql_environment = os.environ.copy() mysql_environment['MYSQL_PWD'] = db_password result = subprocess.check_output( [mysql_binary, '--batch', '--skip-column-names', '--host', db_host, '--port', db_port, '--user', db_user, db_name, '--execute', sql], env=mysql_environment, text=True, stderr=subprocess.STDOUT, ).strip().splitlines() if not result or result[-1].strip() != '1': raise SystemExit('administrator row was not updated exactly as expected') credentials_path = Path('/etc/sea-vhost/admin-credentials') handle, temporary_name = tempfile.mkstemp(prefix='.admin-credentials-', dir=str(credentials_path.parent), text=True) try: with os.fdopen(handle, 'w', encoding='utf-8') as output: output.write(f'login={email}\npassword={password}\n') output.flush() os.fsync(output.fileno()) os.chmod(temporary_name, stat.S_IRUSR | stat.S_IWUSR) os.chown(temporary_name, 0, 0) os.replace(temporary_name, credentials_path) finally: try: os.unlink(temporary_name) except FileNotFoundError: pass print('Sea vHost production administrator password reset: success') PY reset_status=$? if (( reset_status != 0 )); then printf 'Sea vHost production administrator password reset: failed (status %s)\n' "${reset_status}" >&2 fi set +u