#!/usr/bin/env bash # reset-setup-token.sh — regenerate the first-run boot token. # # Root-only. Refuses to run if the users table already has entries (setup # is a one-time event — after that, use the app's own password-recovery # flow to get back in, or wipe + reinstall if you've truly lost everything). # # On success, writes a fresh token to /etc/customsso-manager/first-run.token # (mode 660, root:apache) and prints the setup URL. set -euo pipefail if [[ $EUID -ne 0 ]]; then echo "error: must be run as root (writing to /etc/customsso-manager/)" >&2 exit 1 fi TOKEN_FILE=/etc/customsso-manager/first-run.token DB_CREDS=/etc/customsso-manager/db.creds if [[ ! -r "$DB_CREDS" ]]; then echo "error: cannot read $DB_CREDS — is customsso-manager actually installed on this host?" >&2 exit 2 fi # shellcheck disable=SC1090 . "$DB_CREDS" USER_COUNT=$(mysql -N -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "SELECT COUNT(*) FROM users") if [[ "$USER_COUNT" -gt 0 ]]; then cat >&2 < "$TOKEN_FILE" chown root:apache "$TOKEN_FILE" chmod 660 "$TOKEN_FILE" # Best-effort URL — from Apache vhost's ServerName if we can find it, else # fall back to the machine's hostname. HOST=$(awk '/^\s*ServerName/ {print $2; exit}' /etc/httpd/conf.d/customsso-manager.conf 2>/dev/null || true) [[ -z "$HOST" ]] && HOST=$(hostname) cat <