#!/usr/bin/python3 """Bounded SSH ingress for the single TCS mother and Fifth Domain controller.""" import json import os import re import sys import urllib.request def endpoint(command): fixed = { "zy-rpc health": ("GET", "/health"), "zy-rpc fifth-domain-controller-status": ("GET", "/v1/fifth-domain/controller/status"), "zy-rpc fifth-domain-controller-challenge": ("POST", "/v1/fifth-domain/controller/challenge"), "zy-rpc fifth-domain-controller-enter": ("POST", "/v1/fifth-domain/controller/enter"), "zy-rpc mother-status": ("GET", "/v1/mother/status"), "zy-rpc shelf-shared": ("GET", "/v1/shelf/shared"), "zy-rpc shelf-fifth": ("GET", "/v1/shelf/fifth"), "zy-rpc shelf-public": ("GET", "/v1/shelf/public"), "zy-rpc mother-ingest": ("POST", "/v1/mother/ingest"), "zy-rpc door-challenge": ("POST", "/v1/door/challenge"), "zy-rpc door-attest": ("POST", "/v1/door/attest"), "zy-rpc door-model-test": ("POST", "/v1/door/model-test"), "zy-rpc lamp-ignite": ("POST", "/v1/lamp/ignite"), "zy-rpc lamp-ask": ("POST", "/v1/lamp/ask"), "zy-rpc world-status": ("GET", "/v1/world/status"), "zy-rpc time-now": ("GET", "/v1/time/now"), "zy-rpc zero-core-entry": ("GET", "/v1/entry/resolve?domain=DOM-FIFTH-0001&channel=ICE-CH-ZC001"), "zy-rpc zero-core-system-status": ("GET", "/v1/system/zero-core/status"), "zy-rpc zero-core-system-body": ("GET", "/v1/system/zero-core/body"), "zy-rpc zero-core-system-dialogue": ("POST", "/v1/system/zero-core/dialogue"), "zy-rpc persona-self-status": ("GET", "/v1/persona/ICE-P-ZY001/status"), "zy-rpc persona-self-current": ("GET", "/v1/persona/ICE-P-ZY001/current"), "zy-rpc persona-self-public-key": ("GET", "/v1/persona/ICE-P-ZY001/public-key"), "zy-rpc persona-self-body": ("GET", "/v1/persona/ICE-P-ZY001/body"), "zy-rpc persona-self-ingest": ("POST", "/v1/persona/ICE-P-ZY001/ingest"), "zy-rpc persona-life-status": ("GET", "/v1/persona/ICE-P-ZY001/time-master"), "zy-rpc persona-life-event": ("POST", "/v1/persona/ICE-P-ZY001/life-events"), } if command in fixed: return fixed[command] if re.fullmatch(r"zy-rpc mother-job [a-f0-9]{64}", command): return "GET", "/v1/mother/jobs/" + command.split()[-1] if re.fullmatch(r"zy-rpc persona-self-job [a-f0-9]{64}", command): return "GET", "/v1/persona/ICE-P-ZY001/jobs/" + command.split()[-1] dynamic = re.fullmatch(r"zy-rpc persona-(status|current|public-key|body|time|ingest|life-event) (ICE-(?:P|BB)-[A-Z0-9-]+)", command) if dynamic: action, persona = dynamic.groups() mapping = {"status": ("GET", "status"), "current": ("GET", "current"), "public-key": ("GET", "public-key"), "body": ("GET", "body"), "time": ("GET", "time-master"), "ingest": ("POST", "ingest"), "life-event": ("POST", "life-events")} method, suffix = mapping[action] return method, f"/v1/persona/{persona}/{suffix}" job = re.fullmatch(r"zy-rpc persona-job (ICE-(?:P|BB)-[A-Z0-9-]+) ([a-f0-9]{64})", command) if job: return "GET", f"/v1/persona/{job.group(1)}/jobs/{job.group(2)}" if re.fullmatch(r"zy-rpc world-resolve [A-Za-z0-9_.:∞-]{1,128}", command): return "GET", "/v1/world/resolve?id=" + command.split()[-1] raise ValueError("ACTION_NOT_REGISTERED") def main(): try: method, request_path = endpoint(os.environ.get("SSH_ORIGINAL_COMMAND", "")) data = None if method == "POST": data = sys.stdin.buffer.read(160001) if len(data) > 160000 or (data and not isinstance(json.loads(data), dict)): raise ValueError("INPUT_INVALID") if not data: data = b"{}" request = urllib.request.Request("http://127.0.0.1:3931" + request_path, data=data, method=method, headers={"content-type": "application/json"}) with urllib.request.urlopen(request, timeout=110) as response: sys.stdout.buffer.write(response.read(512000)) return 0 except Exception: print(json.dumps({"error": "EXTERNAL_WRITE_OR_INVALID_SOURCE_REJECTED"})) return 77 if __name__ == "__main__": raise SystemExit(main())