#!/bin/sh
#
# RMM Agent Plugin Installer fuer OPNsense
#
# Usage (automatischer Download):
# fetch -o /tmp/install.sh "https://git.cynfo.net/christian/rmm/raw/branch/main/opnsense-plugin/install.sh"
# sh /tmp/install.sh
#
# Oder mit lokalen Binaries:
# scp install.sh rmm-agent rmm-updater-freebsd root@opnsense:/tmp/
# ssh root@opnsense '/bin/sh /tmp/install.sh'
#
set -e
BASEDIR="/tmp"
BINARY="${BASEDIR}/rmm-agent"
UPDATER_BINARY="${BASEDIR}/rmm-updater"
BACKEND_URL="${RMM_BACKEND:-https://dsbmueller.spdns.org:8443}"
API_KEY="${RMM_API_KEY:-01532e5a7c9e70bf2757df77a2f5b9b9}"
echo "========================================"
echo " RMM Agent Plugin Installer"
echo "========================================"
# Binaries herunterladen wenn nicht lokal vorhanden
if [ ! -f "${BINARY}" ]; then
echo "[*] Binaries nicht lokal gefunden — lade vom Backend..."
/usr/local/bin/fetch --no-verify-peer -o "${BASEDIR}/rmm-bundle.zip" \
"${BACKEND_URL}/api/v1/firmware/download?platform=freebsd&api_key=${API_KEY}"
if [ ! -f "${BASEDIR}/rmm-bundle.zip" ]; then
echo "FEHLER: Download fehlgeschlagen."
echo "Pruefe Backend-Erreichbarkeit: ${BACKEND_URL}"
exit 1
fi
cd "${BASEDIR}"
unzip -o rmm-bundle.zip 2>/dev/null || true
# ZIP enthaelt rmm-agent und rmm-updater-freebsd
if [ -f "${BASEDIR}/rmm-updater-freebsd" ] && [ ! -f "${UPDATER_BINARY}" ]; then
mv "${BASEDIR}/rmm-updater-freebsd" "${UPDATER_BINARY}"
fi
rm -f rmm-bundle.zip
echo " Download OK"
if [ ! -f "${BINARY}" ]; then
echo "FEHLER: ${BINARY} nach Download nicht vorhanden."
echo "Firmware-ZIP muss 'rmm-agent' enthalten."
exit 1
fi
fi
# Alten Agent stoppen falls vorhanden
echo "[1/8] Alte Dienste stoppen..."
/usr/local/etc/rc.d/rmm_updater stop 2>/dev/null || true
/usr/local/etc/rc.d/rmm_agent stop 2>/dev/null || true
pkill -9 -f rmm-agent 2>/dev/null || true
pkill -9 -f rmm-updater 2>/dev/null || true
# Binaries installieren
echo "[2/8] Binaries installieren..."
mkdir -p /usr/local/rmm
cp "${BINARY}" /usr/local/rmm/rmm-agent
chmod +x /usr/local/rmm/rmm-agent
if [ -f "${UPDATER_BINARY}" ]; then
cp "${UPDATER_BINARY}" /usr/local/rmm/rmm-updater
chmod +x /usr/local/rmm/rmm-updater
echo " Updater installiert"
else
echo " Updater-Binary nicht gefunden — uebersprungen"
fi
# rc.d Script
echo "[3/8] rc.d Service installieren..."
cat > /usr/local/etc/rc.d/rmm_agent << 'RCEOF'
#!/bin/sh
#
# PROVIDE: rmm_agent
# REQUIRE: NETWORKING
# KEYWORD: shutdown
. /etc/rc.subr
name="rmm_agent"
rcvar="rmm_agent_enable"
load_rc_config ${name}
: ${rmm_agent_enable:="NO"}
: ${rmm_agent_config:="/usr/local/rmm/config.yaml"}
: ${rmm_agent_logfile:="/var/log/rmm-agent.log"}
: ${rmm_agent_insecure:="YES"}
pidfile="/var/run/${name}.pid"
command="/usr/local/rmm/rmm-agent"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"
rmm_agent_start()
{
if [ ! -f "${command}" ]; then
echo "rmm-agent binary not found: ${command}"
return 1
fi
if [ ! -f "${rmm_agent_config}" ]; then
echo "Config not found: ${rmm_agent_config}"
return 1
fi
echo "Starting ${name}."
INSECURE_FLAG=""
if checkyesno rmm_agent_insecure; then
INSECURE_FLAG="--insecure"
fi
/usr/sbin/daemon -f -p ${pidfile} -o ${rmm_agent_logfile} \
${command} --config ${rmm_agent_config} ${INSECURE_FLAG}
}
rmm_agent_stop()
{
if [ -f "${pidfile}" ]; then
echo "Stopping ${name}."
kill $(cat ${pidfile}) 2>/dev/null
rm -f ${pidfile}
else
echo "${name} is not running."
fi
}
rmm_agent_status()
{
if [ -f "${pidfile}" ] && kill -0 $(cat ${pidfile}) 2>/dev/null; then
echo "${name} is running as pid $(cat ${pidfile})."
else
echo "${name} is not running."
return 1
fi
}
run_rc_command "$1"
RCEOF
chmod +x /usr/local/etc/rc.d/rmm_agent
# Updater rc.d Service
cat > /usr/local/etc/rc.d/rmm_updater << 'RCEOF2'
#!/bin/sh
#
# PROVIDE: rmm_updater
# REQUIRE: NETWORKING rmm_agent
# KEYWORD: shutdown
. /etc/rc.subr
name="rmm_updater"
rcvar="${name}_enable"
command="/usr/local/rmm/rmm-updater"
pidfile="/var/run/${name}.pid"
logfile="/var/log/rmm-updater.log"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"
rmm_updater_start()
{
if [ ! -f "${command}" ]; then
echo "rmm-updater binary not found: ${command}"
return 1
fi
if [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>/dev/null; then
echo "${name} already running"
return 0
fi
echo "Starting ${name}."
/usr/sbin/daemon -f -p "$pidfile" -o "$logfile" "$command"
}
rmm_updater_stop()
{
if [ -f "${pidfile}" ]; then
echo "Stopping ${name}."
kill $(cat ${pidfile}) 2>/dev/null
rm -f ${pidfile}
else
echo "${name} is not running."
fi
}
rmm_updater_status()
{
if [ -f "${pidfile}" ] && kill -0 $(cat ${pidfile}) 2>/dev/null; then
echo "${name} is running as pid $(cat ${pidfile})."
else
echo "${name} is not running."
return 1
fi
}
load_rc_config $name
: ${rmm_updater_enable:="NO"}
run_rc_command "$1"
RCEOF2
chmod +x /usr/local/etc/rc.d/rmm_updater
# MVC Plugin installieren
echo "[4/8] OPNsense MVC Plugin installieren..."
# Model
mkdir -p /usr/local/opnsense/mvc/app/models/OPNsense/RMMAgent/Menu
cat > /usr/local/opnsense/mvc/app/models/OPNsense/RMMAgent/RMMAgent.xml << 'XMLEOF'