Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: remove conflicting Apache default sites and any
|
||||
# site config that would try to bind port 80 (nginx owns port 80).
|
||||
# Invoked via adpanel-priv: adpanel-priv job apache-cleanup-sites
|
||||
set -u
|
||||
|
||||
rm -f /etc/apache2/sites-enabled/000-default.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/sites-enabled/default-ssl.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/conf-enabled/phpmyadmin.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/conf-enabled/pgadmin4.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/conf-enabled/serve-cgi-bin.conf 2>/dev/null || true
|
||||
|
||||
grep -l ':80>' /etc/apache2/sites-enabled/*.conf 2>/dev/null | xargs -r rm -f
|
||||
grep -l 'Listen 80' /etc/apache2/sites-enabled/*.conf 2>/dev/null | xargs -r rm -f
|
||||
true
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: install PHP-FPM + phpMyAdmin.
|
||||
# Invoked via adpanel-priv: adpanel-priv job install-phpmyadmin
|
||||
set -u
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
apt-get update -qq
|
||||
|
||||
# Install PHP-FPM and required extensions first
|
||||
apt-get install -y --no-install-recommends \
|
||||
php-fpm php-mysql php-mbstring php-zip php-gd php-curl php-xml php-cli
|
||||
|
||||
# Install phpMyAdmin (skip Apache recommends)
|
||||
apt-get install -y --no-install-recommends phpmyadmin
|
||||
|
||||
# If Apache was pulled in anyway, stop and disable it - nginx handles traffic
|
||||
if systemctl is-active apache2 >/dev/null 2>&1; then
|
||||
systemctl stop apache2 2>/dev/null || true
|
||||
fi
|
||||
systemctl disable apache2 2>/dev/null || true
|
||||
|
||||
# Ensure PHP-FPM is running (prefer the newest installed version)
|
||||
PHP_FPM_SVC=$(systemctl list-units --type=service --state=running,exited --no-legend 2>/dev/null | grep -oP 'php[\d.]+-fpm\.service' | sort -rV | head -1)
|
||||
if [ -z "$PHP_FPM_SVC" ]; then
|
||||
PHP_FPM_SVC=$(systemctl list-unit-files --type=service --no-legend 2>/dev/null | grep -oP 'php[\d.]+-fpm\.service' | sort -rV | head -1)
|
||||
fi
|
||||
if [ -n "$PHP_FPM_SVC" ]; then
|
||||
systemctl enable "$PHP_FPM_SVC" 2>/dev/null || true
|
||||
systemctl restart "$PHP_FPM_SVC" 2>/dev/null || true
|
||||
fi
|
||||
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: fully disable and remove MongoDB.
|
||||
# Invoked via adpanel-priv: adpanel-priv job mongodb-disable [listen-port]
|
||||
#
|
||||
# Failure policy: every critical step (service stop, package purge, config and
|
||||
# data removal) emits an ADP_MONGO_DISABLE_FAIL marker on stderr and sets
|
||||
# FAILED=1. The script NEVER exits early — later cleanup still runs — and it
|
||||
# stays idempotent: already-clean states (stopped service, purged packages,
|
||||
# absent files) count as success, so a re-run after a partial failure attempts
|
||||
# every step again. The runner exits non-zero when FAILED=1 so the caller can
|
||||
# refuse to wipe the panel's MongoDB bookkeeping while cleanup is incomplete.
|
||||
set -u
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
FAILED=0
|
||||
fail() { echo "ADP_MONGO_DISABLE_FAIL: $*" >&2; FAILED=1; }
|
||||
|
||||
# --- stop the service (critical: a live mongod must not survive "disabled") ---
|
||||
if ! systemctl stop mongod 2>/dev/null; then
|
||||
# Tolerate an already-stopped or already-removed unit (idempotent re-run);
|
||||
# only a unit that is still active after the stop attempt is a failure.
|
||||
if systemctl is-active mongod >/dev/null 2>&1; then
|
||||
fail "could not stop the mongod service"
|
||||
fi
|
||||
fi
|
||||
systemctl disable mongod 2>/dev/null
|
||||
if systemctl is-enabled mongod >/dev/null 2>&1; then
|
||||
fail "could not disable the mongod service (boot start still enabled)"
|
||||
fi
|
||||
pkill -9 -x mongod 2>/dev/null
|
||||
sleep 1
|
||||
if pgrep -x mongod >/dev/null 2>&1; then
|
||||
fail "a mongod process is still running"
|
||||
fi
|
||||
|
||||
# --- purge packages (critical; only purge what is actually installed so
|
||||
# re-runs after a successful purge stay green) ---
|
||||
PURGE_PKGS=""
|
||||
for pkg in mongodb-org mongodb-org-server mongodb-org-mongos mongodb-org-shell mongodb-org-tools mongodb-mongosh; do
|
||||
if dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -q "installed"; then
|
||||
PURGE_PKGS="$PURGE_PKGS $pkg"
|
||||
fi
|
||||
done
|
||||
if [ -n "$PURGE_PKGS" ]; then
|
||||
apt-get purge -y $PURGE_PKGS 2>/dev/null || fail "apt-get purge failed for:$PURGE_PKGS"
|
||||
fi
|
||||
apt-get autoremove -y 2>/dev/null || true
|
||||
|
||||
# --- remove data, logs and config (critical) ---
|
||||
rm -rf /var/lib/mongodb /var/log/mongodb 2>/dev/null
|
||||
if [ -e /var/lib/mongodb ] || [ -e /var/log/mongodb ]; then
|
||||
fail "MongoDB data/log directories could not be removed"
|
||||
fi
|
||||
rm -f /etc/mongod.conf 2>/dev/null
|
||||
if [ -e /etc/mongod.conf ]; then
|
||||
fail "/etc/mongod.conf could not be removed"
|
||||
fi
|
||||
|
||||
# Best-effort leftovers only: stale sockets, apt sources, keyrings, indexes.
|
||||
rm -f /tmp/mongodb-*.sock 2>/dev/null || true
|
||||
rm -f /etc/apt/sources.list.d/mongodb*.list 2>/dev/null || true
|
||||
rm -f /usr/share/keyrings/mongodb*.gpg 2>/dev/null || true
|
||||
apt-get update 2>/dev/null || true
|
||||
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
|
||||
# Optional: remove the firewall rule for the former MongoDB port.
|
||||
PORT="${1:-}"
|
||||
if [ -n "$PORT" ] && [ "$PORT" = "${PORT//[!0-9]/}" ] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then
|
||||
ufw delete allow "${PORT}/tcp" 2>/dev/null || true
|
||||
iptables -D INPUT -p tcp --dport "$PORT" -j ACCEPT 2>/dev/null || true
|
||||
fi
|
||||
|
||||
if [ "$FAILED" -ne 0 ]; then
|
||||
echo "ADP_MONGO_DISABLE_FAIL: one or more critical cleanup steps failed" >&2
|
||||
exit 1
|
||||
fi
|
||||
exit 0
|
||||
@@ -0,0 +1,361 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: MySQL/MariaDB admin bootstrap.
|
||||
# Invoked via adpanel-priv: adpanel-priv job mysql-admin-bootstrap
|
||||
# env: ADPANEL_DB_ADMIN_PASSWORD (required) — the password for the dedicated
|
||||
# panel admin account, generated by the panel process and passed through
|
||||
# the sudoers env_keep allowlist (never argv, never a file the panel
|
||||
# writes as root).
|
||||
#
|
||||
# Why this job exists: the panel's database-management SQL (CREATE/ALTER/DROP
|
||||
# USER, GRANT, REVOKE, probe queries) used to run through whatever account the
|
||||
# admin-channel probe could authenticate as — most commonly MySQL/MariaDB
|
||||
# 'root'@'localhost' via the unix socket or the operator-reused MYSQL_PASSWORD.
|
||||
# That identity is (a) far too powerful for a network-facing panel process and
|
||||
# (b) demotable by the panel's own account-management flows: a legacy setup
|
||||
# that adopted 'root' as its tracked database administrator ran
|
||||
# `REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'root'@'localhost'` while staging,
|
||||
# after which every GRANT batch failed with ERROR 1044 and phpMyAdmin setup
|
||||
# became impossible.
|
||||
#
|
||||
# This job fixes both directions, once, as OS root:
|
||||
# 1. HEAL — if a root account exists but cannot administer (the panel-demoted
|
||||
# state), restore its packaging-default static privileges via the
|
||||
# skip-grant-tables mechanism. MySQL and MariaDB need different SQL here
|
||||
# (MySQL 8 rejects FLUSH/GRANT in skip-grant mode; MariaDB's mysql.user is
|
||||
# a read-only view), so both strategies are attempted.
|
||||
# 2. PROVISION — create the dedicated least-privilege panel admin account
|
||||
# ('adpanel_admin'@'localhost'). It holds exactly the privileges the
|
||||
# panel's account-management batches need: the panel DML/DDL set on *.*
|
||||
# WITH GRANT OPTION (so it can grant that same set to panel users on any
|
||||
# schema) plus CREATE USER (which also authorizes DROP USER and ALTER USER
|
||||
# password changes on MySQL 8 and MariaDB). It deliberately holds NO
|
||||
# SUPER, FILE, SHUTDOWN, RELOAD, mysql.* write access or dynamic
|
||||
# privileges — a compromised panel can no longer read the server's
|
||||
# password hashes with administrative authority, restart the server,
|
||||
# read files, or touch the privilege tables directly.
|
||||
# 3. CERTIFY — run a full canary AS the dedicated account covering every
|
||||
# statement class the panel issues (CREATE/ALTER/DROP USER, db-scoped
|
||||
# GRANT, REVOKE ALL+GRANT OPTION, mysql.user probe, SHOW DATABASES). The
|
||||
# job exits non-zero unless the canary passed, so the panel never switches
|
||||
# to a channel that cannot actually administer the server.
|
||||
#
|
||||
# The job is idempotent: re-running heals nothing (already capable), resets the
|
||||
# dedicated account's password to the freshly supplied one, and re-certifies.
|
||||
set -u
|
||||
|
||||
ADMIN_USER="adpanel_admin"
|
||||
ADMIN_HOST="localhost"
|
||||
ADMIN_PASSWORD="${ADPANEL_DB_ADMIN_PASSWORD:-}"
|
||||
CANARY_DB="adpanel_admin_canary"
|
||||
CANARY_USER="adpanel_admin_probe"
|
||||
|
||||
die() {
|
||||
echo "mysql-admin-bootstrap: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# The panel generates this password as crypto.randomBytes(...).toString('hex');
|
||||
# accepting only lowercase hex keeps it safely embeddable in single-quoted SQL.
|
||||
[ -n "$ADMIN_PASSWORD" ] || die "ADPANEL_DB_ADMIN_PASSWORD is not set"
|
||||
case "$ADMIN_PASSWORD" in
|
||||
*[!0-9a-f]*) die "ADPANEL_DB_ADMIN_PASSWORD must be lowercase hex" ;;
|
||||
esac
|
||||
[ "${#ADMIN_PASSWORD}" -ge 48 ] || die "ADPANEL_DB_ADMIN_PASSWORD is too short"
|
||||
[ "${#ADMIN_PASSWORD}" -le 128 ] || die "ADPANEL_DB_ADMIN_PASSWORD is too long"
|
||||
|
||||
# Serialize invocations: a manual run must never interleave its stop /
|
||||
# skip-grant / restart sequence with the panel's own cooldown-gated attempt.
|
||||
# Fail loudly rather than silently running unserialized.
|
||||
if command -v flock >/dev/null 2>&1; then
|
||||
mkdir -p /run/lock 2>/dev/null || true
|
||||
exec 9>/run/lock/adpanel-mysql-bootstrap.lock || die "cannot open /run/lock/adpanel-mysql-bootstrap.lock"
|
||||
flock -n 9 || die "another mysql-admin-bootstrap is already running"
|
||||
fi
|
||||
|
||||
# --- service / binary detection (same contract as mysql-skip-grant.sh) -------
|
||||
|
||||
SVC=""
|
||||
for s in mariadb mysql mysqld; do
|
||||
if systemctl list-unit-files "$s.service" >/dev/null 2>&1; then
|
||||
SVC="$s"
|
||||
break
|
||||
fi
|
||||
done
|
||||
[ -n "$SVC" ] || die "Could not detect MySQL/MariaDB service name"
|
||||
|
||||
SERVER_BIN=""
|
||||
for bin in mariadbd mysqld; do
|
||||
if command -v "$bin" >/dev/null 2>&1; then
|
||||
SERVER_BIN="$bin"
|
||||
break
|
||||
fi
|
||||
for p in /usr/sbin /usr/bin /usr/libexec; do
|
||||
if [ -x "$p/$bin" ]; then
|
||||
SERVER_BIN="$p/$bin"
|
||||
break 2
|
||||
fi
|
||||
done
|
||||
done
|
||||
[ -n "$SERVER_BIN" ] || die "Could not find mariadbd or mysqld binary"
|
||||
|
||||
CLIENT_BIN=""
|
||||
for bin in mariadb mysql; do
|
||||
if command -v "$bin" >/dev/null 2>&1; then
|
||||
CLIENT_BIN="$bin"
|
||||
break
|
||||
fi
|
||||
done
|
||||
[ -n "$CLIENT_BIN" ] || die "Could not find mariadb or mysql client"
|
||||
|
||||
# --- capability probe ---------------------------------------------------------
|
||||
# Authenticated AND authorized: the account's own row in mysql.user must hold
|
||||
# Create_user_priv and Grant_priv. SELECT 1 alone passes for a demoted root —
|
||||
# the exact failure mode this job exists to repair — so the probe must query
|
||||
# the grant columns. Both engines expose them (MariaDB 10.4+ through the
|
||||
# mysql.user compatibility view). CURRENT_USER() never contains '@' inside its
|
||||
# user part for any account the panel authenticates as.
|
||||
CAP_PROBE_SQL="SELECT COUNT(*) FROM mysql.user WHERE user=SUBSTRING_INDEX(CURRENT_USER(),'@',1) AND host=SUBSTRING_INDEX(CURRENT_USER(),'@',-1) AND Grant_priv='Y' AND Create_user_priv='Y'"
|
||||
|
||||
# Make sure the server is actually up before probing: the panel calls this job
|
||||
# after ensureMySqlRunning, but the job stays self-sufficient when the service
|
||||
# is down (a stop-point heal also leaves it stopped until the trailing start).
|
||||
if ! systemctl is-active "$SVC" >/dev/null 2>&1; then
|
||||
systemctl start "$SVC" >/dev/null 2>&1 || true
|
||||
for i in $(seq 1 30); do
|
||||
systemctl is-active "$SVC" >/dev/null 2>&1 && break
|
||||
sleep 0.5
|
||||
done
|
||||
fi
|
||||
|
||||
# Candidates are probed with the capability SQL and remembered as
|
||||
# (KIND, ARGS) so the winning method can be re-run for provisioning:
|
||||
# KIND=plain -> "$CLIENT_BIN" "${CAPABLE_ARGS[@]+"${CAPABLE_ARGS[@]}"}" ...
|
||||
# KIND=envpw -> MYSQL_PWD="$MYSQL_PASSWORD" "$CLIENT_BIN" "${CAPABLE_ARGS[@]+"${CAPABLE_ARGS[@]}"}" ...
|
||||
# (bash variable-prefix assignment, not env(1): the password must never
|
||||
# appear in a process argv, which /proc/<pid>/cmdline publishes.)
|
||||
AUTH_SEEN=0
|
||||
CAPABLE_KIND=""
|
||||
CAPABLE_ARGS=()
|
||||
|
||||
run_probe() {
|
||||
local kind="$1"
|
||||
shift
|
||||
local merged out
|
||||
if [ "$kind" = "envpw" ]; then
|
||||
merged="$(MYSQL_PWD="$MYSQL_PASSWORD" "$CLIENT_BIN" "$@" -N -e "$CAP_PROBE_SQL" 2>&1)"
|
||||
else
|
||||
merged="$("$CLIENT_BIN" "$@" -N -e "$CAP_PROBE_SQL" 2>&1)"
|
||||
fi
|
||||
# No (usable) count output with an error code: an ERROR 1045/1698 means the
|
||||
# channel could not even authenticate; ERROR 1044/1142 ("command denied")
|
||||
# means the identity IS authenticated but too demoted to read mysql.user —
|
||||
# exactly the state the heal exists to repair, so it counts as auth.
|
||||
case "$merged" in
|
||||
*"ERROR 1045"*|*"ERROR 1698"*) echo no ;;
|
||||
*"ERROR 1044"*|*"ERROR 1142"*|*"command denied"*) echo auth ;;
|
||||
*)
|
||||
out="$(printf '%s\n' "$merged" | tail -n 1 | tr -d '[:space:]')"
|
||||
case "$out" in
|
||||
1) echo capable ;;
|
||||
0) echo auth ;;
|
||||
*) echo no ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
check_method() {
|
||||
local kind="$1"
|
||||
shift
|
||||
local state
|
||||
state="$(run_probe "$kind" "$@")"
|
||||
if [ "$state" = "capable" ]; then
|
||||
CAPABLE_KIND="$kind"
|
||||
CAPABLE_ARGS=("$@")
|
||||
return 0
|
||||
fi
|
||||
if [ "$state" = "auth" ]; then
|
||||
AUTH_SEEN=1
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# Re-run the winning method's client with the caller's SQL on stdin/args.
|
||||
run_capable_client() {
|
||||
if [ "$CAPABLE_KIND" = "envpw" ]; then
|
||||
MYSQL_PWD="$MYSQL_PASSWORD" "$CLIENT_BIN" ${CAPABLE_ARGS[@]+"${CAPABLE_ARGS[@]}"} "$@"
|
||||
else
|
||||
"$CLIENT_BIN" ${CAPABLE_ARGS[@]+"${CAPABLE_ARGS[@]}"} "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
find_capable_method() {
|
||||
CAPABLE_KIND=""
|
||||
CAPABLE_ARGS=()
|
||||
AUTH_SEEN=0
|
||||
# Method order mirrors op_mysql_admin's catalog.
|
||||
if [ -n "${MYSQL_PASSWORD:-}" ]; then
|
||||
check_method envpw -u root && return 0
|
||||
check_method envpw -u root -h 127.0.0.1 && return 0
|
||||
fi
|
||||
check_method plain && return 0
|
||||
check_method plain -u root && return 0
|
||||
check_method plain -u root --skip-password && return 0
|
||||
if [ -f /etc/mysql/debian.cnf ]; then
|
||||
check_method plain --defaults-file=/etc/mysql/debian.cnf && return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# --- heal: restore a demoted root via skip-grant-tables -----------------------
|
||||
|
||||
heal_needed=0
|
||||
if find_capable_method; then
|
||||
echo "mysql-admin-bootstrap: capable admin channel found (kind=$CAPABLE_KIND args=${CAPABLE_ARGS[*]})"
|
||||
else
|
||||
heal_needed=1
|
||||
[ "$AUTH_SEEN" = "1" ] || die "No MySQL/MariaDB auth method works at all; not auto-healing a server the panel cannot even reach"
|
||||
echo "mysql-admin-bootstrap: admin identities authenticate but cannot administer; healing via skip-grant-tables"
|
||||
fi
|
||||
|
||||
if [ "$heal_needed" = "1" ]; then
|
||||
SOCK_DIR="/var/run/mysqld"
|
||||
SOCK_PATH="$SOCK_DIR/mysqld.sock"
|
||||
mkdir -p "$SOCK_DIR"
|
||||
chown mysql:mysql "$SOCK_DIR" 2>/dev/null || true
|
||||
chmod 755 "$SOCK_DIR"
|
||||
|
||||
systemctl stop "$SVC" 2>/dev/null || true
|
||||
pkill -9 -x mariadbd 2>/dev/null || true
|
||||
pkill -9 -x mysqld 2>/dev/null || true
|
||||
sleep 2
|
||||
rm -f "$SOCK_PATH" "$SOCK_PATH.lock" 2>/dev/null || true
|
||||
|
||||
"$SERVER_BIN" --skip-grant-tables --skip-networking --user=mysql --socket="$SOCK_PATH" &
|
||||
SAFE_PID=$!
|
||||
READY=0
|
||||
for i in $(seq 1 40); do
|
||||
if [ -S "$SOCK_PATH" ]; then
|
||||
if "$CLIENT_BIN" --socket="$SOCK_PATH" -u root -e "SELECT 1" >/dev/null 2>&1; then
|
||||
READY=1
|
||||
break
|
||||
fi
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
if [ "$READY" -ne 1 ]; then
|
||||
kill "$SAFE_PID" 2>/dev/null || true
|
||||
pkill -9 -x mariadbd 2>/dev/null || true
|
||||
pkill -9 -x mysqld 2>/dev/null || true
|
||||
systemctl start "$SVC" 2>/dev/null || true
|
||||
die "skip-grant-tables server did not become ready"
|
||||
fi
|
||||
|
||||
heal_applied=0
|
||||
# Strategy 1 (MariaDB): FLUSH PRIVILEGES loads the grant tables, then the
|
||||
# session (whose identity is root@localhost on the skip-grant socket) is
|
||||
# authorized and GRANT ALL ... WITH GRANT OPTION restores the packaging
|
||||
# default. MySQL 8 rejects the FLUSH with ER_SPECIFIC_ACCESS_DENIED (RELOAD)
|
||||
# — there the grant tables are updated directly instead.
|
||||
if [ "$heal_applied" = "0" ]; then
|
||||
if "$CLIENT_BIN" --socket="$SOCK_PATH" -u root <<'HEALSQL' >/dev/null 2>&1; then
|
||||
FLUSH PRIVILEGES;
|
||||
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
|
||||
HEALSQL
|
||||
heal_applied=1
|
||||
fi
|
||||
fi
|
||||
# Strategy 2 (MySQL 8.x): mysql.user is a base table; in skip-grant mode
|
||||
# direct DML on it is authorized (the earlier probe proved FLUSH is refused,
|
||||
# table DML is not). Every static _priv column is set to 'Y' — the packaging
|
||||
# default for root@localhost — with the column list read from
|
||||
# information_schema so a schema change across server versions cannot break
|
||||
# the UPDATE shape. Dynamic privileges (mysql.global_grants) are not touched.
|
||||
if [ "$heal_applied" = "0" ]; then
|
||||
PRIV_COLS="$("$CLIENT_BIN" --socket="$SOCK_PATH" -u root -N -e \
|
||||
"SELECT COLUMN_NAME FROM information_schema.columns WHERE table_schema='mysql' AND table_name='user' AND column_name LIKE '%_priv' ORDER BY ordinal_position" 2>/dev/null | tr '\n' ' ' | sed 's/ $//')"
|
||||
[ -n "$PRIV_COLS" ] || PRIV_COLS="Select_priv Insert_priv Update_priv Delete_priv Create_priv Drop_priv Reload_priv Shutdown_priv Process_priv File_priv Grant_priv References_priv Index_priv Alter_priv Show_db_priv Super_priv Create_tmp_table_priv Lock_tables_priv Execute_priv Repl_slave_priv Repl_client_priv Create_view_priv Show_view_priv Create_routine_priv Alter_routine_priv Create_user_priv Event_priv Trigger_priv Create_tablespace_priv"
|
||||
SET_LIST=""
|
||||
for col in $PRIV_COLS; do
|
||||
case "$col" in
|
||||
*[!A-Za-z0-9_]*) die "unexpected privilege column name: $col" ;;
|
||||
esac
|
||||
SET_LIST="${SET_LIST:+$SET_LIST, }${col}='Y'"
|
||||
done
|
||||
if "$CLIENT_BIN" --socket="$SOCK_PATH" -u root -e "UPDATE mysql.user SET $SET_LIST WHERE User='root' AND Host='localhost'" >/dev/null 2>&1; then
|
||||
heal_applied=1
|
||||
fi
|
||||
fi
|
||||
|
||||
kill "$SAFE_PID" 2>/dev/null || true
|
||||
pkill -9 -x mariadbd 2>/dev/null || true
|
||||
pkill -9 -x mysqld 2>/dev/null || true
|
||||
sleep 2
|
||||
systemctl start "$SVC" 2>/dev/null || true
|
||||
for i in $(seq 1 30); do
|
||||
if systemctl is-active "$SVC" >/dev/null 2>&1; then
|
||||
break
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
|
||||
if [ "$heal_applied" != "1" ]; then
|
||||
die "both heal strategies failed; root@localhost could not be restored"
|
||||
fi
|
||||
echo "mysql-admin-bootstrap: root@localhost privileges restored"
|
||||
|
||||
find_capable_method || die "root@localhost was healed but still cannot administer the server"
|
||||
fi
|
||||
|
||||
# --- provision the dedicated panel admin account ------------------------------
|
||||
# Batch aborts on the first error (plain client semantics), so a partial state
|
||||
# is always visible in the exit code. CREATE USER IF NOT EXISTS + ALTER USER
|
||||
# gives a deterministic final password whether or not a previous bootstrap (or
|
||||
# a degraded older panel) left the account behind; the account name is
|
||||
# panel-reserved (MYSQL_RESERVED_USERNAMES) so no other panel flow may ever
|
||||
# create, re-password or drop it.
|
||||
run_capable_client <<PROVISION
|
||||
CREATE USER IF NOT EXISTS '$ADMIN_USER'@'$ADMIN_HOST' IDENTIFIED BY '$ADMIN_PASSWORD';
|
||||
ALTER USER '$ADMIN_USER'@'$ADMIN_HOST' IDENTIFIED BY '$ADMIN_PASSWORD';
|
||||
REVOKE ALL PRIVILEGES, GRANT OPTION FROM '$ADMIN_USER'@'$ADMIN_HOST';
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON *.* TO '$ADMIN_USER'@'$ADMIN_HOST' WITH GRANT OPTION;
|
||||
GRANT CREATE USER ON *.* TO '$ADMIN_USER'@'$ADMIN_HOST' WITH GRANT OPTION;
|
||||
PROVISION
|
||||
[ $? -eq 0 ] || die "provisioning the dedicated admin account failed"
|
||||
|
||||
# --- certify the dedicated account with a full canary -------------------------
|
||||
# Every statement class the panel's database-management SQL relies on. The
|
||||
# canary database is created by the root channel; everything else runs AS the
|
||||
# dedicated account through the socket, exactly like the panel's own channel.
|
||||
|
||||
run_capable_client -e "CREATE DATABASE IF NOT EXISTS \`$CANARY_DB\`" 2>/dev/null || die "cannot create the canary database"
|
||||
|
||||
canary_cleanup() {
|
||||
# Separate invocations: either statement may be a no-op error (the probe
|
||||
# user is absent when the canary failed before creating it) and must not
|
||||
# mask the other.
|
||||
MYSQL_PWD="$ADMIN_PASSWORD" "$CLIENT_BIN" -u "$ADMIN_USER" \
|
||||
-e "REVOKE ALL PRIVILEGES, GRANT OPTION FROM '$CANARY_USER'@'localhost'" >/dev/null 2>&1 || true
|
||||
MYSQL_PWD="$ADMIN_PASSWORD" "$CLIENT_BIN" -u "$ADMIN_USER" \
|
||||
-e "DROP USER IF EXISTS '$CANARY_USER'@'localhost'" >/dev/null 2>&1 || true
|
||||
run_capable_client -e "DROP DATABASE IF EXISTS \`$CANARY_DB\`" >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
canary_rc=0
|
||||
MYSQL_PWD="$ADMIN_PASSWORD" "$CLIENT_BIN" -u "$ADMIN_USER" <<'CANARY' >/dev/null 2>&1 || canary_rc=$?
|
||||
CREATE USER IF NOT EXISTS 'adpanel_admin_probe'@'localhost' IDENTIFIED BY 'canary-probe-password';
|
||||
ALTER USER 'adpanel_admin_probe'@'localhost' IDENTIFIED BY 'canary-probe-password-2';
|
||||
GRANT SELECT ON `adpanel_admin_canary`.* TO 'adpanel_admin_probe'@'localhost';
|
||||
SELECT COUNT(*) FROM mysql.user WHERE user = 'adpanel_admin_probe';
|
||||
SHOW DATABASES;
|
||||
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'adpanel_admin_probe'@'localhost';
|
||||
DROP USER 'adpanel_admin_probe'@'localhost';
|
||||
CANARY
|
||||
if [ "$canary_rc" != "0" ]; then
|
||||
canary_cleanup
|
||||
die "canary failed: the dedicated account cannot administer the server"
|
||||
fi
|
||||
canary_cleanup
|
||||
|
||||
echo "mysql-admin-bootstrap: dedicated admin account '$ADMIN_USER'@'$ADMIN_HOST' provisioned and certified"
|
||||
exit 0
|
||||
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: MySQL/MariaDB admin reset via skip-grant-tables.
|
||||
# Invoked via adpanel-priv: adpanel-priv job mysql-skip-grant
|
||||
# stdin: SQL statements to execute as root once the server is in
|
||||
# skip-grant-tables mode.
|
||||
set -u
|
||||
|
||||
SQL="$(cat)"
|
||||
[ -n "$SQL" ] || { echo "mysql-skip-grant: no SQL on stdin" >&2; exit 2; }
|
||||
|
||||
SOCK_DIR="/var/run/mysqld"
|
||||
SOCK_PATH="$SOCK_DIR/mysqld.sock"
|
||||
|
||||
# Detect service name (mariadb, mysql or mysqld)
|
||||
SVC=""
|
||||
for s in mariadb mysql mysqld; do
|
||||
if systemctl list-unit-files "$s.service" >/dev/null 2>&1; then
|
||||
SVC="$s"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ -z "$SVC" ]; then
|
||||
echo "ERROR: Could not detect MySQL/MariaDB service name" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Detect the server binary (mariadbd or mysqld)
|
||||
SERVER_BIN=""
|
||||
for bin in mariadbd mysqld; do
|
||||
if command -v "$bin" >/dev/null 2>&1; then
|
||||
SERVER_BIN="$bin"
|
||||
break
|
||||
fi
|
||||
for p in /usr/sbin /usr/bin /usr/libexec; do
|
||||
if [ -x "$p/$bin" ]; then
|
||||
SERVER_BIN="$p/$bin"
|
||||
break 2
|
||||
fi
|
||||
done
|
||||
done
|
||||
if [ -z "$SERVER_BIN" ]; then
|
||||
echo "ERROR: Could not find mariadbd or mysqld binary" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
systemctl stop "$SVC" 2>/dev/null || true
|
||||
pkill -9 -x mariadbd 2>/dev/null || true
|
||||
pkill -9 -x mysqld 2>/dev/null || true
|
||||
sleep 2
|
||||
|
||||
mkdir -p "$SOCK_DIR"
|
||||
chown mysql:mysql "$SOCK_DIR" 2>/dev/null || true
|
||||
chmod 755 "$SOCK_DIR"
|
||||
rm -f "$SOCK_PATH" 2>/dev/null || true
|
||||
|
||||
"$SERVER_BIN" --skip-grant-tables --skip-networking --user=mysql --socket="$SOCK_PATH" &
|
||||
SAFE_PID=$!
|
||||
|
||||
READY=0
|
||||
for i in $(seq 1 40); do
|
||||
if [ -S "$SOCK_PATH" ]; then
|
||||
if mysql --socket="$SOCK_PATH" -u root -e "SELECT 1" >/dev/null 2>&1; then
|
||||
READY=1
|
||||
break
|
||||
fi
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
|
||||
if [ "$READY" -ne 1 ]; then
|
||||
echo "ERROR: skip-grant-tables server did not become ready" >&2
|
||||
kill "$SAFE_PID" 2>/dev/null || true
|
||||
pkill -9 -x mariadbd 2>/dev/null || true
|
||||
pkill -9 -x mysqld 2>/dev/null || true
|
||||
systemctl start "$SVC" 2>/dev/null || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
{ echo "FLUSH PRIVILEGES;"; printf '%s\n' "$SQL"; } | mysql --socket="$SOCK_PATH" -u root
|
||||
SQL_EXIT=$?
|
||||
|
||||
kill "$SAFE_PID" 2>/dev/null || true
|
||||
pkill -9 -x mariadbd 2>/dev/null || true
|
||||
pkill -9 -x mysqld 2>/dev/null || true
|
||||
sleep 2
|
||||
|
||||
systemctl start "$SVC" 2>/dev/null || true
|
||||
for i in $(seq 1 30); do
|
||||
if systemctl is-active "$SVC" >/dev/null 2>&1; then
|
||||
break
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
|
||||
exit "$SQL_EXIT"
|
||||
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: fully disable and remove pgAdmin4 + PostgreSQL.
|
||||
# Invoked via adpanel-priv: adpanel-priv job pgadmin-disable [listen-port]
|
||||
set -u
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
systemctl stop postgresql 2>/dev/null || true
|
||||
systemctl disable postgresql 2>/dev/null || true
|
||||
pkill -9 -x postgres 2>/dev/null || true
|
||||
|
||||
# Remove the ADPanel-managed remote-bind pg_hba.conf rules installed by
|
||||
# pg-add-hba (marker-delimited block) plus any legacy untagged allow lines
|
||||
# from older panel versions. Only those lines are removed; PostgreSQL
|
||||
# packages and data are deliberately NOT purged here — the panel's database
|
||||
# management feature (type=postgresql) can reuse the same instance and a
|
||||
# purge would destroy user databases.
|
||||
# pg_hba.conf lives at /etc/postgresql/<version>/<cluster>/pg_hba.conf on
|
||||
# Debian — the same recursive layout pg-add-hba writes to — so locate the
|
||||
# files the same way instead of a single-level glob.
|
||||
while IFS= read -r hba; do
|
||||
[ -f "$hba" ] || continue
|
||||
sed -i \
|
||||
-e '/^# BEGIN ADPanel-managed remote-bind rules (added by adpanel-priv pg-add-hba)$/,/^# END ADPanel-managed remote-bind rules$/d' \
|
||||
-e '/^host all all 0\.0\.0\.0\/0 scram-sha-256$/d' \
|
||||
-e '/^host all all ::0\/0 scram-sha-256$/d' \
|
||||
"$hba" 2>/dev/null || true
|
||||
done < <(find /etc/postgresql -name pg_hba.conf 2>/dev/null)
|
||||
# Only if PostgreSQL is somehow still active (it was just stopped above),
|
||||
# reload so the removed rules take effect; otherwise leave the on-disk
|
||||
# config clean for the next start.
|
||||
if systemctl is-active --quiet postgresql 2>/dev/null; then
|
||||
systemctl reload postgresql 2>/dev/null || true
|
||||
fi
|
||||
|
||||
systemctl stop apache2 2>/dev/null || true
|
||||
systemctl disable apache2 2>/dev/null || true
|
||||
pkill -9 -x apache2 2>/dev/null || true
|
||||
|
||||
rm -f /etc/apache2/sites-enabled/pgadmin4.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/sites-available/pgadmin4.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/conf-enabled/pgadmin4.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/conf-available/pgadmin4.conf 2>/dev/null || true
|
||||
|
||||
# Remove Listen 5050 from ports.conf (clean slate for reinstall); if no
|
||||
# listener remains, leave a placeholder comment (nginx owns port 80).
|
||||
sed -i '/Listen 5050/d' /etc/apache2/ports.conf 2>/dev/null || true
|
||||
if ! grep -q '^Listen' /etc/apache2/ports.conf 2>/dev/null; then
|
||||
echo '# Apache ports - no active listeners (nginx handles web traffic)' > /etc/apache2/ports.conf
|
||||
fi
|
||||
|
||||
# Placeholder keeps nginx include lines valid without serving pgAdmin.
|
||||
echo '# pgAdmin4 disabled - placeholder to prevent nginx include errors' > /etc/nginx/snippets/pgadmin4.conf 2>/dev/null || true
|
||||
for conf in /etc/nginx/sites-available/*.conf /etc/nginx/sites-enabled/*.conf /etc/nginx/conf.d/*.conf; do
|
||||
if [ -f "$conf" ]; then
|
||||
sed -i '/include.*pgadmin4.conf/d' "$conf" 2>/dev/null || true
|
||||
fi
|
||||
done
|
||||
nginx -t 2>/dev/null && systemctl reload nginx 2>/dev/null || true
|
||||
|
||||
apt-get remove --purge -y pgadmin4-web pgadmin4-desktop pgadmin4 2>/dev/null || true
|
||||
apt-get autoremove -y 2>/dev/null || true
|
||||
|
||||
rm -rf /var/lib/pgadmin /var/log/pgadmin /etc/pgadmin /usr/pgadmin4 2>/dev/null || true
|
||||
|
||||
rm -f /etc/apt/sources.list.d/pgadmin4.list 2>/dev/null || true
|
||||
rm -f /usr/share/keyrings/packages-pgadmin-org.gpg 2>/dev/null || true
|
||||
apt-get update -qq 2>/dev/null || true
|
||||
|
||||
# Optional: remove the firewall rule for the former PostgreSQL port.
|
||||
PORT="${1:-}"
|
||||
if [ -n "$PORT" ] && [ "$PORT" = "${PORT//[!0-9]/}" ] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then
|
||||
ufw delete allow "${PORT}/tcp" 2>/dev/null || true
|
||||
iptables -D INPUT -p tcp --dport "$PORT" -j ACCEPT 2>/dev/null || true
|
||||
fi
|
||||
|
||||
true
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: prepare the pgAdmin Apache backend (localhost:5050).
|
||||
# Invoked via adpanel-priv: adpanel-priv job pgadmin-prepare
|
||||
# stdin: base64-encoded Apache VirtualHost configuration for pgAdmin4.
|
||||
set -u
|
||||
|
||||
CONF_B64="$(cat)"
|
||||
[ -n "$CONF_B64" ] || { echo "pgadmin-prepare: missing config payload" >&2; exit 2; }
|
||||
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
systemctl start postgresql 2>/dev/null || true
|
||||
|
||||
mkdir -p /etc/apache2/sites-available /etc/apache2/sites-enabled
|
||||
printf %s "$CONF_B64" | base64 -d > /etc/apache2/sites-available/pgadmin4.conf
|
||||
chmod 0644 /etc/apache2/sites-available/pgadmin4.conf
|
||||
ln -sf /etc/apache2/sites-available/pgadmin4.conf /etc/apache2/sites-enabled/pgadmin4.conf
|
||||
|
||||
rm -f /etc/apache2/sites-enabled/000-default.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/sites-enabled/default-ssl.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/conf-enabled/phpmyadmin.conf 2>/dev/null || true
|
||||
rm -f /etc/apache2/conf-enabled/pgadmin4.conf 2>/dev/null || true
|
||||
|
||||
cat > /etc/apache2/ports.conf <<'EOF'
|
||||
# Apache ports configuration - managed by ADPanel
|
||||
Listen 127.0.0.1:5050
|
||||
EOF
|
||||
|
||||
a2enmod wsgi >/dev/null 2>&1 || true
|
||||
a2enmod headers >/dev/null 2>&1 || true
|
||||
apache2ctl -t >/dev/null 2>&1 || true
|
||||
systemctl restart apache2 2>/dev/null || systemctl start apache2 2>/dev/null || true
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: re-run pgAdmin setup-web.sh to (re)create the
|
||||
# initial admin account, then restart Apache.
|
||||
# Invoked via adpanel-priv: adpanel-priv job pgadmin-setup-recover
|
||||
# Environment: PGADMIN_SETUP_EMAIL, PGADMIN_SETUP_PASSWORD
|
||||
set -u
|
||||
|
||||
SETUP_SCRIPT=""
|
||||
if [ -x /usr/pgadmin4/bin/setup-web.sh ]; then
|
||||
SETUP_SCRIPT="/usr/pgadmin4/bin/setup-web.sh"
|
||||
elif [ -x /usr/share/pgadmin4/bin/setup-web.sh ]; then
|
||||
SETUP_SCRIPT="/usr/share/pgadmin4/bin/setup-web.sh"
|
||||
fi
|
||||
|
||||
if [ -n "$SETUP_SCRIPT" ]; then
|
||||
: "${PGADMIN_SETUP_EMAIL:?PGADMIN_SETUP_EMAIL not set}"
|
||||
: "${PGADMIN_SETUP_PASSWORD:?PGADMIN_SETUP_PASSWORD not set}"
|
||||
"$SETUP_SCRIPT" --yes >/dev/null 2>&1 || true
|
||||
rm -f /etc/apache2/conf-enabled/pgadmin4.conf 2>/dev/null || true
|
||||
systemctl restart apache2 2>/dev/null || systemctl start apache2 2>/dev/null || true
|
||||
fi
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
# ADPanel privileged job: prepare the phpMyAdmin backend (PHP-FPM + nginx).
|
||||
# Invoked via adpanel-priv: adpanel-priv job phpmyadmin-prepare
|
||||
set -u
|
||||
|
||||
PHP_FPM_SVC=$(systemctl list-unit-files --type=service --no-legend | awk '{print $1}' | grep -E '^php[0-9.]+-fpm\.service$' | sort -rV | head -1)
|
||||
if [ -n "$PHP_FPM_SVC" ]; then
|
||||
systemctl enable "$PHP_FPM_SVC" 2>/dev/null || true
|
||||
systemctl restart "$PHP_FPM_SVC" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
systemctl start nginx 2>/dev/null || true
|
||||
nginx -t >/dev/null 2>&1 && systemctl reload nginx 2>/dev/null || systemctl restart nginx 2>/dev/null || true
|
||||
Reference in new issue
Block a user