#!/bin/sh /etc/rc.common # ============================================================================= # netpulse-agent — agente nativo NetPulse para OpenWrt (SPEC-AGENTE-PILOTO §2) # # procd con respawn; logs del agente (stderr) van a syslog (logread -e # netpulse-agent). La config vive en /etc/netpulse-agent.env (chmod 600) — # procd reinicia el servicio si ese fichero cambia (procd_set_param file). # # Variante /tmp (equipos con flash justa): si el binario no está en # /usr/sbin se usa /tmp/netpulse-agent (RAM; hay que reinstalar tras reboot — # ver install-agent.sh --tmp). # ============================================================================= START=99 STOP=10 USE_PROCD=1 ENV_FILE=/etc/netpulse-agent.env BIN=/usr/sbin/netpulse-agent [ -x "$BIN" ] || BIN=/tmp/netpulse-agent # Self-heal: un sysupgrade NO conserva los ficheros sueltos (solo conffiles # de paquetes, /etc/sysupgrade.conf y keep.d), por eso el instalador añade # binario, init y env a /etc/sysupgrade.conf (#1476). Si aun así falta el # binario al arrancar se descarga del server con el env (mismo criterio que el # reinstall del server; #851 retiró el watchdog cron, esto cubre el boot). selfheal_binary() { [ -x "$BIN" ] && return 0 [ -f "$ENV_FILE" ] || return 1 . "$ENV_FILE" 2>/dev/null [ -n "${NETPULSE_SERVER:-}" ] && [ -n "${NETPULSE_TOKEN:-}" ] && [ -n "${NETPULSE_SLUG:-}" ] || return 1 case "${NETPULSE_SERVER%%:*}" in https|HTTPS) ;; *) [ "${NETPULSE_ALLOW_INSECURE_COMMANDS:-}" = "1" ] || { logger -t netpulse-agent "self-heal: HTTP without NETPULSE_ALLOW_INSECURE_COMMANDS=1, binary not downloaded" return 1 } ;; esac case "$(uname -m)" in aarch64|arm64) local ARCH=arm64 ;; armv7l|armv7|armhf|arm) local ARCH=arm ;; x86_64|amd64) local ARCH=amd64 ;; mips) case "$(head -c 6 /bin/sh | tail -c 1 | tr '\001\002' '12')" in 1) local ARCH=mipsle ;; 2) local ARCH=mips ;; *) return 1 ;; esac ;; *) return 1 ;; esac local url tmp hdr digest sum url="${NETPULSE_SERVER%/}/api/agents/${NETPULSE_SLUG}/binary?arch=${ARCH}" logger -t netpulse-agent "self-heal: binary missing, downloading from $NETPULSE_SERVER" tmp=/tmp/netpulse-agent.$$ hdr=/tmp/netpulse-agent.hdr.$$ if command -v curl >/dev/null 2>&1; then curl -fsSL -m 120 -D "$hdr" -H "Authorization: Bearer $NETPULSE_TOKEN" -o "$tmp" "$url" || { rm -f "$tmp" "$hdr"; return 1; } else wget -S -T 60 -O "$tmp" --header="Authorization: Bearer $NETPULSE_TOKEN" "$url" 2>"$hdr" || { rm -f "$tmp" "$hdr"; return 1; } fi digest=$(grep -i '^[[:space:]]*x-checksum-sha256:' "$hdr" 2>/dev/null | tail -n 1 | tr -d '\r' | awk '{print $2}') rm -f "$hdr" case "$digest" in ""|*[!0123456789abcdefABCDEF]*) logger -t netpulse-agent "self-heal: no valid X-Checksum-Sha256, binary discarded" rm -f "$tmp" return 1 ;; esac [ "${#digest}" -eq 64 ] || { logger -t netpulse-agent "self-heal: digest with invalid length, binary discarded"; rm -f "$tmp"; return 1; } sum=$(sha256sum "$tmp" 2>/dev/null | awk '{print $1}') if [ -z "$sum" ] || [ "$(printf '%s' "$sum" | tr 'A-F' 'a-f')" != "$(printf '%s' "$digest" | tr 'A-F' 'a-f')" ]; then logger -t netpulse-agent "self-heal: SHA-256 mismatch, binary discarded" rm -f "$tmp" return 1 fi chmod 0755 "$tmp" && mv "$tmp" /usr/sbin/netpulse-agent || { rm -f "$tmp"; return 1; } logger -t netpulse-agent "self-heal: binary restored" BIN=/usr/sbin/netpulse-agent return 0 } # (#879) self-heal: tras un sysupgrade /etc sobrevive pero el binario del # watchdog no: si quedó la entrada de cron del antiguo watchdog, retirarla # (cron loguearía un comando inexistente cada 2 min). cleanup_stale_watchdog_cron() { [ -f /usr/sbin/netpulse-watchdog ] && return 0 crontab -l 2>/dev/null | grep -q netpulse-watchdog || return 0 ( crontab -l 2>/dev/null | grep -v netpulse-watchdog ) | crontab - 2>/dev/null || true logger -t netpulse-agent "orphan watchdog cron entry removed (#879)" } start_service() { cleanup_stale_watchdog_cron selfheal_binary || logger -t netpulse-agent "self-heal: could not restore the binary" procd_open_instance netpulse-agent procd_set_param command "$BIN" # respawn: si muere, procd lo levanta (5 s entre intentos, 5 intentos/hora) procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}" procd_set_param stdout 1 procd_set_param stderr 1 procd_set_param file /etc/netpulse-agent.env procd_close_instance }