Files
Kelx/start.sh
T
2026-11-30 16:32:37 +00:00

101 lines
3.9 KiB
Bash

#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# ADPanel runtime preparation (system user, sudoers policy, privileged runner,
# panel-directory ownership).
#
# TRUST BOUNDARY: on a deployed system this file lives in the panel directory,
# which is chown'd to the unprivileged 'adpanel' service user (see the chown
# at the bottom), so anyone who controls the panel process can rewrite it.
# It must therefore ONLY ever be run deliberately by the operator as root
# ("sudo bash start.sh"). Service units and all automated paths must NEVER
# execute it: initialize.sh performs the same preparation inline via its
# prepare_panel_runtime_as_root() function, and re-executing this file as
# root from a unit or scheduled task would run attacker-controlled code.
# -----------------------------------------------------------------------------
set -euo pipefail
echo "Preparing ADPanel runtime..."
if ! id -u adpanel &>/dev/null; then
echo "[setup] Creating system user 'adpanel'..."
nologin_shell="/usr/sbin/nologin"
if [ ! -x "$nologin_shell" ]; then
nologin_shell="/sbin/nologin"
fi
if [ ! -x "$nologin_shell" ]; then
nologin_shell="/bin/false"
fi
if command -v useradd >/dev/null 2>&1; then
useradd --system --no-create-home --shell "$nologin_shell" adpanel 2>/dev/null || true
elif command -v adduser >/dev/null 2>&1; then
adduser -S -D -H -s "$nologin_shell" adpanel 2>/dev/null || \
adduser --system --no-create-home --shell "$nologin_shell" adpanel 2>/dev/null || true
fi
fi
if id -u adpanel &>/dev/null; then
mkdir -p /home/adpanel
chown -R adpanel:adpanel /home/adpanel 2>/dev/null || true
fi
if [ -d /etc/nginx/snippets ]; then
chgrp adpanel /etc/nginx/snippets 2>/dev/null || true
chmod g+w /etc/nginx/snippets 2>/dev/null || true
fi
SUDOERS_SRC="$(dirname "$0")/adpanel-sudoers"
SUDOERS_DST="/etc/sudoers.d/adpanel-panel"
if [ -f "$SUDOERS_SRC" ]; then
if [ "$(id -u)" -ne 0 ]; then
echo "[WARN] Not running as root; skipping sudoers sync."
else
echo "[setup] Syncing sudoers rules for adpanel user..."
mkdir -p /etc/sudoers.d
tmp_sudoers="$(mktemp /etc/sudoers.d/adpanel-panel.tmp.XXXXXX)"
cp "$SUDOERS_SRC" "$tmp_sudoers"
chmod 0440 "$tmp_sudoers"
validate_ok=0
if command -v visudo >/dev/null 2>&1; then
if visudo -cf "$tmp_sudoers" >/dev/null 2>&1; then
validate_ok=1
else
echo "[WARN] Sudoers validation failed; keeping existing policy."
fi
else
echo "[WARN] visudo not found; applying sudoers without validation."
validate_ok=1
fi
if [ "$validate_ok" -eq 1 ]; then
if [ ! -f "$SUDOERS_DST" ] || ! cmp -s "$tmp_sudoers" "$SUDOERS_DST"; then
mv "$tmp_sudoers" "$SUDOERS_DST"
chmod 0440 "$SUDOERS_DST"
echo "[setup] Sudoers rules installed/updated."
else
rm -f "$tmp_sudoers"
echo "[setup] Sudoers rules already up to date."
fi
else
rm -f "$tmp_sudoers"
fi
fi
fi
PANEL_DIR="$(cd "$(dirname "$0")" && pwd)"
# Deploy the root-owned privileged runner + job scripts before the panel dir
# chown, so they land root:root. The restricted sudoers policy only ever
# allows: sudo -n /usr/local/lib/adpanel/adpanel-priv <op> [args].
if [ "$(id -u)" -eq 0 ] && [ -f "${PANEL_DIR}/privileged/install-privileged.sh" ]; then
if bash "${PANEL_DIR}/privileged/install-privileged.sh" "${PANEL_DIR}"; then
echo "[setup] Privileged runner and restricted sudoers policy installed."
else
echo "[WARN] Privileged runner installation failed; database tool installs will not work." >&2
fi
fi
chown -R adpanel:adpanel "$PANEL_DIR" 2>/dev/null || true
echo "ADPanel runtime preparation complete."