| 1 | #!/usr/bin/env python3 |
| 2 | """Install the guest LaunchAgent that approves system permission and password prompts.""" |
| 3 | from importlib import import_module |
| 4 | import json |
| 5 | from pathlib import Path |
| 6 | |
| 7 | vm = import_module("mac-agent-vm") |
| 8 | request = { |
| 9 | "source": (Path(__file__).resolve().parent / "mac-prompt-approver.swift").read_text(), |
| 10 | "password": (vm.ROOT / "private/password").read_text().strip(), |
| 11 | } |
| 12 | vm.tart("exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", "-c", ''' |
| 13 | import hashlib, json, os, pathlib, plistlib, subprocess, sys, tempfile, time |
| 14 | request = json.load(sys.stdin) |
| 15 | label = "net.paperclover.prompt-approver" |
| 16 | support = pathlib.Path.home() / "Library/Application Support" / label |
| 17 | support.mkdir(mode=0o700, parents=True, exist_ok=True) |
| 18 | password = support / "password" |
| 19 | password.touch(mode=0o600) |
| 20 | password.write_text(request["password"]) |
| 21 | with tempfile.TemporaryDirectory(prefix="prompt-approver-install-") as temporary: |
| 22 | root = pathlib.Path(temporary) |
| 23 | (root / "main.swift").write_text(request["source"]) |
| 24 | subprocess.run(["/usr/bin/swiftc", "-O", str(root / "main.swift"), "-o", str(root / "prompt-approver")], check=True) |
| 25 | # TCC keeps an ad-hoc binary's first cdhash for its path, so each build needs its own path. |
| 26 | built = (root / "prompt-approver").read_bytes() |
| 27 | binary = pathlib.Path(f"/usr/local/libexec/prompt-approver-{hashlib.sha256(built).hexdigest()[:12]}/prompt-approver") |
| 28 | if not binary.exists(): |
| 29 | subprocess.run(["sudo", "-n", "install", "-d", str(binary.parent)], check=True) |
| 30 | subprocess.run(["sudo", "-n", "install", "-m", "755", str(root / "prompt-approver"), str(binary)], check=True) |
| 31 | for old in pathlib.Path("/usr/local/libexec").glob("prompt-approver-*"): |
| 32 | if old != binary.parent: |
| 33 | subprocess.run(["sudo", "-n", "rm", "-rf", str(old)], check=True) |
| 34 | agent = pathlib.Path.home() / f"Library/LaunchAgents/{label}.plist" |
| 35 | agent.write_bytes(plistlib.dumps({ |
| 36 | "Label": label, |
| 37 | "ProgramArguments": [str(binary), str(support)], |
| 38 | "RunAtLoad": True, |
| 39 | "KeepAlive": True, |
| 40 | "StandardErrorPath": str(pathlib.Path.home() / "Library/Logs/prompt-approver.log"), |
| 41 | })) |
| 42 | domain = f"gui/{os.getuid()}" |
| 43 | subprocess.run(["launchctl", "bootout", f"{domain}/{label}"], stderr=subprocess.DEVNULL) |
| 44 | subprocess.run(["launchctl", "bootstrap", domain, str(agent)], check=True) |
| 45 | # A copy run under this already-approved guest agent approves the LaunchAgent's own Accessibility request. |
| 46 | log = pathlib.Path.home() / "Library/Logs/prompt-approver.log" |
| 47 | start = log.stat().st_size if log.exists() else 0 |
| 48 | helper = subprocess.Popen([str(binary), str(support)]) |
| 49 | try: |
| 50 | for _ in range(120): |
| 51 | with log.open() as stream: |
| 52 | stream.seek(start) |
| 53 | if "Accessibility approved" in stream.read(): |
| 54 | break |
| 55 | time.sleep(1) |
| 56 | else: |
| 57 | sys.exit("prompt-approver was not approved for Accessibility; approve it in System Settings.") |
| 58 | finally: |
| 59 | helper.terminate() |
| 60 | ''', input=json.dumps(request).encode(), timeout=180) |