#!/usr/bin/env python3
"""starchaser-firewall-keep: answer the firewall's Keep countdown for a change you just made, without a second
password. Root-owned in /usr/local/lib/starchaser/; polkit action org.starchaser.firewall-keep, allowed for the
person at the computer (an active local session in the wheel group) by
/etc/polkit-1/rules.d/50-starchaser-firewall-keep.rules. Everything else Starchaser does as root asks for the
password every time (the org.starchaser.admin.* actions).

It does exactly these and nothing else, through starchaser-admin beside it:

    starchaser-firewall-keep firewall-confirm [id]   Keep: the change waiting for Keep stays
    starchaser-firewall-keep firewall-extend <id>    Need more time: add 60 seconds to that waiting change
    starchaser-firewall-keep firewall-revert [id]    Go back now: the firewall from before that change comes back
                                                (id: the waiting change the button was showing; a newer one that
                                                took its place is left alone)
    starchaser-firewall-keep firewall-mark-network <network uuid> public
                                                No to the one-time "Is this a network you trust?" question:
                                                that network stays Public, and is recorded so it isn't asked again

Keep, Need more time and Go back only act on a change that is waiting for Keep (one applied with the password moments ago); with
nothing waiting they change nothing. Marking a network Public only marks one nobody has marked yet, which never lets
anything in (an unmarked network already counts as Public unless someone chose otherwise), or moves one an older
setup counted as home by itself (unchanged since) back to Public, the No to the question asked once more there,
and not when a rule works differently on Public networks in a way that could let more in; Yes (Trusted) lets things in, so it goes through
starchaser-admin and asks for the password.
"""
import os
import re
import sys

ALLOWED = ("firewall-confirm", "firewall-revert", "firewall-extend")
UUID = re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
PENDING_ID = re.compile(r"[0-9a-f]{32}")

if __name__ == "__main__":
    args = sys.argv[1:]
    if args in (["-h"], ["--help"]):
        print(__doc__.strip())
        sys.exit(0)
    ok = (len(args) in (1, 2) and args[0] in ALLOWED and (args[0] != "firewall-extend" or len(args) == 2) and (len(args) == 1 or PENDING_ID.fullmatch(args[1]))) or \
        (len(args) == 3 and args[0] == "firewall-mark-network" and UUID.fullmatch(args[1]) and args[2] == "public")
    if not ok:
        # a known answer with an id that can't be a change: say so, rather than listing the answers again
        if args and args[0] in ALLOWED:
            print('{"ok": false, "errors": "that isn\'t a change waiting for Keep: expected the change id from the '
                  'countdown (32 letters and numbers)."}')
        elif args and args[0] == "firewall-mark-network":
            print('{"ok": false, "errors": "firewall-mark-network takes a network id and the word public."}')
        else:
            print('{"ok": false, "errors": "starchaser-firewall-keep only answers Keep, Need more time or Go back, or keeps a network Public."}')
        sys.exit(2)
    admin = os.path.join(os.path.dirname(os.path.realpath(__file__)), "starchaser-admin")
    # -I: the helper's own folder and the standard library only, nothing from the caller's environment
    os.execv(sys.executable, [sys.executable, "-I", admin, *args])
