#!/usr/bin/env python3

import sys
import os
import socket

NAGIOS_OK = 0
NAGIOS_ERROR = 2

PAKITI_RESULT = "pakiti_results"

# Get CVE from script name
script_name = os.path.basename(__file__)
cve = script_name.replace("check_", "")

# Check pakiti results - if CVE not found, skip mitigation check
if os.path.isfile(PAKITI_RESULT):
    with open(PAKITI_RESULT, "r") as f:
        content = f.read()
        if cve not in content:
            print(f"No {cve} vulnerability found, skipping the mitigation check")
            sys.exit(NAGIOS_OK)

# Check if algif_aead module is blocked (mitigation applied)
# If the module is blocked, we should get FileNotFoundError or OSError
try:
    s = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
    s.bind(("aead", "gcm(aes)"))
    s.close()
    print(f"No known mitigation of {cve} detected")
    sys.exit(NAGIOS_ERROR)
except FileNotFoundError:
    print("OK: AEAD algorithm not available (module blocked)")
    sys.exit(NAGIOS_OK)
except OSError:
    print("OK: AEAD algorithm not available (module blocked)")
    sys.exit(NAGIOS_OK)
