"""Bounded local inventory projection. Runs independently of the network agent.""" import datetime import json import os from pathlib import Path import re import resource import socket import subprocess import tempfile import time import xml.etree.ElementTree as ET CACHE = Path('/var/db/discovery-observations') def run(*args): # Appliance commands may produce much more than one snapshot. Cap the # producer before reading its output, including on a misconfigured host. def limits(): resource.setrlimit(resource.RLIMIT_FSIZE, (2**21, 2**21)) with tempfile.TemporaryFile() as out: subprocess.run(args, stdout=out, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL, check=True, timeout=10, preexec_fn=limits) out.seek(0) return out.read(2**21).decode('utf8', errors='replace').strip() def sysctl(name): return run('/sbin/sysctl', '-n', name) def rows(provider): entries = [] partial = False try: for text in provider(): if len(entries) == 128: partial = True break text = ' '.join(text.split()) if len(text.encode()) > 512: partial = True text = text.encode()[:509].decode(errors='ignore') + '…' if text: entries.append(text) except (OSError, ValueError, KeyError, TypeError, ET.ParseError, subprocess.SubprocessError): return dict(status='partial' if entries else 'unavailable', entries=entries) return dict(status='partial' if partial else 'ok', entries=entries) def configured(config_path='/conf/config.xml'): # Project only these model paths and display fields. Never serialize XML. with open(config_path, 'rb') as f: data = f.read(8 * 1024 * 1024 + 1) if len(data) > 8 * 1024 * 1024: raise ValueError('config size') root = ET.fromstring(data) for title, path, field in [ ('WireGuard-Instanz', 'wireguard/server/servers/server', 'name'), ('WireGuard-Peer', 'wireguard/client/clients/client', 'name'), ('IPsec', 'Swanctl/Connections/Connection', 'description'), ('OpenVPN', 'OpenVPN/Instances/Instance', 'description'), ]: for item in root.findall('OPNsense/' + path): enabled = {'0': 'deaktiviert', '1': 'aktiviert'}.get(item.findtext('enabled'), 'Status unbekannt') yield f"{title} · {item.findtext(field) or item.get('uuid') or '–'} · {enabled}" def wireguard(): for line in run('/usr/bin/wg', 'show', 'all', 'latest-handshakes').splitlines(): interface, public_key, epoch = line.split() stamp = datetime.datetime.fromtimestamp(int(epoch), datetime.timezone.utc).isoformat() if int(epoch) else 'nie' yield f'{interface} · {public_key} · {stamp}' def openvpn(): data = json.loads(run('/usr/local/opnsense/scripts/openvpn/ovpn_status.py', '--options', 'client,server')) for role in ('server', 'client'): for instance, status in data.get(role, {}).items(): yield f"{role} {instance} · {status.get('status', 'unbekannt')}" for peer in status.get('client_list', []): yield f"{instance} · {peer.get('common_name', '–')} · {peer.get('real_address', '–')}" def ipsec(): # Only IKE summary lines, not CHILD_SA details or authentication settings. for line in run('/usr/local/sbin/swanctl', '--list-sas').splitlines(): if re.match(r'^\S.*: #\d+, ', line): yield line def interfaces(): item = None for line in run('/sbin/ifconfig','-f','inet:cidr,inet6:cidr','-a').splitlines(): if line and not line[0].isspace(): if item: yield item item = dict(name=line.split(':',1)[0],mac=None,addresses=[]) elif item: fields = line.split() if len(fields)>1 and fields[0]=='ether': item['mac'] = fields[1] elif len(fields)>1 and fields[0] in ('inet','inet6'): item['addresses'].append(fields[1]) if item: yield item def packages(): for line in run('/usr/sbin/pkg','query','%n\t%v').splitlines(): name,version = line.split('\t',1) yield dict(name=name,version=version) def inventory_rows(fn, limit): entries = [] try: for item in fn(): if len(entries) >= limit: return dict(status='partial',entries=entries) entries.append(item) return dict(status='ok',entries=entries) except (OSError,ValueError,subprocess.SubprocessError): return dict(status='partial' if entries else 'unavailable',entries=entries) def snapshot(): disk = os.statvfs('/') boot = int(re.search(r'sec = (\d+)',sysctl('kern.boottime'))[1]) hardware = {'device_type':'physical' if sysctl('kern.vm_guest')=='none' else 'virtual', 'booted_at':datetime.datetime.fromtimestamp(boot,datetime.timezone.utc).isoformat()} for key,name in [('smbios.system.maker','manufacturer'),('smbios.system.serial','bios_serial_number')]: try: value = run('/bin/kenv',key) if value and value.lower() not in ('none','unknown','not specified','default string'): hardware[name]=value[:128] except (OSError,subprocess.SubprocessError): pass return dict(os=run('/usr/local/sbin/opnsense-version'), hostname=socket.gethostname(), architecture=os.uname().machine, root_total_bytes=disk.f_blocks*disk.f_frsize, root_available_bytes=max(0,disk.f_bavail)*disk.f_frsize, cpu_model=sysctl('hw.model'), logical_processors=int(sysctl('hw.ncpu')), memory_total_bytes=int(sysctl('hw.physmem')), firewall=rows(lambda: run('/sbin/pfctl', '-sr').splitlines()), vpn_configured=rows(configured), wireguard=rows(wireguard), openvpn=rows(openvpn), ipsec=rows(ipsec), hardware=hardware, interfaces=inventory_rows(interfaces,128), software=inventory_rows(packages,1024), users=rows(lambda:sorted(set(run('/usr/bin/users').split())))) if __name__ == '__main__': while True: try: with tempfile.NamedTemporaryFile(dir=CACHE, delete=False) as f: f.write(json.dumps(snapshot(), ensure_ascii=False).encode()) os.fchmod(f.fileno(), 0o640) os.replace(f.name, CACHE/'inventory.json') except Exception as exc: print('OPNsense collection:', type(exc).__name__, flush=True) if 'f' in locals(): Path(f.name).unlink(missing_ok=True) time.sleep(60)