#!/bin/sh # Direct artifact deployment in the test guest. Enrollment is a separate step. set -eu [ "$(id -u)" = 0 ] && [ "$(uname -s)" = FreeBSD ] && [ -x /usr/local/sbin/opnsense-version ] [ "$#" = 1 ] || { echo 'Usage: install-dev.sh /path/to/staged-artifacts' >&2; exit 2; } src=$1 for f in agent.pyz collect.pyz discovery_agent ca.pem; do [ -f "$src/$f" ]; done /usr/bin/openssl x509 -in "$src/ca.pem" -noout -checkend 0 >/dev/null pw groupshow discovery >/dev/null 2>&1 || pw groupadd discovery pw usershow discovery >/dev/null 2>&1 || pw useradd discovery -g discovery -d /var/db/discovery-agent -s /usr/sbin/nologin [ "$(id -gn discovery)" = discovery ] && [ "$(id -u discovery)" != 0 ] app=/usr/local/libexec/discovery-agent conf=/usr/local/etc/discovery-agent rc=/usr/local/etc/rc.d/discovery_agent install -d -o root -g wheel -m 0755 "$app" "$conf" install -d -o discovery -g discovery -m 0700 /var/db/discovery-agent install -d -o root -g discovery -m 2750 /var/db/discovery-observations if [ -e "$conf/ca.pem" ]; then cmp -s "$conf/ca.pem" "$src/ca.pem" || { echo 'Existing trust anchor differs' >&2; exit 1; } fi for part in collect agent; do install -o root -g wheel -m 0444 "$src/$part.pyz" "$app/$part.next" done if [ -x "$rc" ]; then "$rc" onestop for _ in 1 2 3 4 5; do [ -f /var/run/discovery-agent.pid ] || [ -f /var/run/discovery-collect.pid ] || [ -f /var/run/discovery-root-jobs.pid ] || break sleep 1 done if [ -f /var/run/discovery-agent.pid ] || [ -f /var/run/discovery-collect.pid ] || [ -f /var/run/discovery-root-jobs.pid ]; then echo 'Existing service still running' >&2; exit 1 fi fi if [ -f "$src/rmm-opnsense-jobs" ]; then install -o root -g wheel -m 0555 "$src/rmm-opnsense-jobs" "$app/jobs.next" if [ -f "$conf/root-policy.json" ] && ! /usr/local/bin/python3 -I -c \ 'import hashlib,json,sys;sys.exit(hashlib.file_digest(open(sys.argv[1],"rb"),"sha256").hexdigest()!=json.load(open(sys.argv[2]))["helper_binary_sha256"])' \ "$app/jobs.next" "$conf/root-policy.json"; then echo 'Native helper differs from the local policy pin' >&2; "$rc" start; exit 1 fi mv -f "$app/jobs.next" "$app/rmm-opnsense-jobs" fi for part in collect agent; do mv -f "$app/$part.next" "$app/$part.pyz"; done install -o root -g wheel -m 0444 "$src/ca.pem" "$conf/ca.pem" install -o root -g wheel -m 0555 "$src/discovery_agent" "$rc" sysrc -f /etc/rc.conf.local discovery_agent_enable=YES >/dev/null if [ -s /var/db/discovery-agent/identity.json ]; then "$rc" start else echo 'Ready for enrollment under the discovery service account.' fi