Blame SOURCES/ansible-freeipa-1.9.2-paclient-Fix-allow_repair-with-removed-krb5.conf-an_RHBZ#2189235.patch

e80d55
From bfeefaf454e3e705e509ed13b2e650ddfd487fa2 Mon Sep 17 00:00:00 2001
e80d55
From: Thomas Woerner <twoerner@redhat.com>
e80d55
Date: Wed, 8 Feb 2023 13:38:12 +0100
e80d55
Subject: [PATCH] ipaclient: Fix allow_repair with removed krb5.conf and DNS
e80d55
 lookup
e80d55
e80d55
The test in ipaclient_test_keytab is at first trying to use an existing
e80d55
krb5.conf to test if the host keytab can be used. With working DNS lookup
e80d55
an absent krb5.conf is not reported as an error as DNS lookup is
e80d55
silently used instead.
e80d55
e80d55
A temporary krb5.conf is now used in this test that forces to deactivate
e80d55
DNS lookups and also to load /etc/krb5.conf. A missing krb5.conf is now
e80d55
detected properly as the kinit call fails now properly. Thanks to Julien
e80d55
Rische for this proposal.
e80d55
e80d55
ipaclient_test_keytab is now properly returning the state of usable or
e80d55
not usable krb5.conf in krb5_conf_ok. This fixes the handling of this
e80d55
case later on in the role.
e80d55
---
e80d55
 .../library/ipaclient_test_keytab.py          | 27 +++++++++++++++++--
e80d55
 1 file changed, 25 insertions(+), 2 deletions(-)
e80d55
e80d55
diff --git a/roles/ipaclient/library/ipaclient_test_keytab.py b/roles/ipaclient/library/ipaclient_test_keytab.py
e80d55
index a86b237..3f1c69d 100644
e80d55
--- a/roles/ipaclient/library/ipaclient_test_keytab.py
e80d55
+++ b/roles/ipaclient/library/ipaclient_test_keytab.py
e80d55
@@ -159,11 +159,29 @@ def main():
e80d55
     ca_crt_exists = os.path.exists(paths.IPA_CA_CRT)
e80d55
     env = {'PATH': SECURE_PATH, 'KRB5CCNAME': paths.IPA_DNS_CCACHE}
e80d55
 
e80d55
-    # First try: Validate krb5 keytab with system krb5 configuraiton
e80d55
+    # First try: Validate with temporary test krb5.conf that forces
e80d55
+    # 1) no DNS lookups and
e80d55
+    # 2) to load /etc/krb5.conf:
e80d55
+    #
e80d55
+    # [libdefaults]
e80d55
+    # dns_lookup_realm = false
e80d55
+    # dns_lookup_kdc = false
e80d55
+    # include /etc/krb5.conf
e80d55
+    #
e80d55
     try:
e80d55
+        (krb_fd, krb_name) = tempfile.mkstemp()
e80d55
+        os.close(krb_fd)
e80d55
+        content = "\n".join([
e80d55
+            "[libdefaults]",
e80d55
+            "dns_lookup_realm = false",
e80d55
+            "dns_lookup_kdc = false",
e80d55
+            "include /etc/krb5.conf"
e80d55
+        ])
e80d55
+        with open(krb_name, "w") as outf:
e80d55
+            outf.write(content)
e80d55
         kinit_keytab(host_principal, paths.KRB5_KEYTAB,
e80d55
                      paths.IPA_DNS_CCACHE,
e80d55
-                     config=paths.KRB5_CONF,
e80d55
+                     config=krb_name,
e80d55
                      attempts=kinit_attempts)
e80d55
         krb5_keytab_ok = True
e80d55
         krb5_conf_ok = True
e80d55
@@ -177,6 +195,11 @@ def main():
e80d55
             pass
e80d55
     except GSSError:
e80d55
         pass
e80d55
+    finally:
e80d55
+        try:
e80d55
+            os.remove(krb_name)
e80d55
+        except OSError:
e80d55
+            module.fail_json(msg="Could not remove %s" % krb_name)
e80d55
 
e80d55
     # Second try: Validate krb5 keytab with temporary krb5
e80d55
     # configuration
e80d55
-- 
e80d55
2.39.2
e80d55