Blame SOURCES/Add-hostname-canonicalization-helper-to-k5test.py.patch

a53771
From c76a01279bbbbcfd296d2ead8f6e2a5bee7e8443 Mon Sep 17 00:00:00 2001
a53771
From: Greg Hudson <ghudson@mit.edu>
a53771
Date: Fri, 15 Jan 2021 14:43:34 -0500
a53771
Subject: [PATCH] Add hostname canonicalization helper to k5test.py
a53771
a53771
To facilitate fallback tests, add a canonicalize_hostname() function
a53771
to k5test.py which works similarly to krb5_expand_hostname().  Use it
a53771
in t_gssapi.py for the recently-added acceptor name fallback test.
a53771
a53771
(cherry picked from commit 225fffe4e912772acea3a01d45bafb60bfb80948)
a53771
---
a53771
 src/tests/gssapi/t_gssapi.py | 11 +++--------
a53771
 src/util/k5test.py           | 22 ++++++++++++++++++++++
a53771
 2 files changed, 25 insertions(+), 8 deletions(-)
a53771
a53771
diff --git a/src/tests/gssapi/t_gssapi.py b/src/tests/gssapi/t_gssapi.py
a53771
index 1af6f31c2..e22cec427 100755
a53771
--- a/src/tests/gssapi/t_gssapi.py
a53771
+++ b/src/tests/gssapi/t_gssapi.py
a53771
@@ -8,7 +8,7 @@ for realm in multipass_realms():
a53771
     realm.run(['./t_iov', '-s', 'p:' + realm.host_princ])
a53771
     realm.run(['./t_pcontok', 'p:' + realm.host_princ])
a53771
 
a53771
-realm = K5Realm(krb5_conf={'libdefaults': {'rdns': 'false'}})
a53771
+realm = K5Realm()
a53771
 
a53771
 # Test gss_add_cred().
a53771
 realm.run(['./t_add_cred'])
a53771
@@ -62,13 +62,8 @@ realm.run(['./t_accname', 'p:host/-nomatch-',
a53771
           expected_msg=' not found in keytab')
a53771
 
a53771
 # If possible, test with an acceptor name requiring fallback to match
a53771
-# against a keytab entry.  Forward-canonicalize the hostname, relying
a53771
-# on the rdns=false realm setting.
a53771
-try:
a53771
-    ai = socket.getaddrinfo(hostname, None, 0, 0, 0, socket.AI_CANONNAME)
a53771
-    (family, socktype, proto, canonname, sockaddr) = ai[0]
a53771
-except socket.gaierror:
a53771
-    canonname = hostname
a53771
+# against a keytab entry.
a53771
+canonname = canonicalize_hostname(hostname)
a53771
 if canonname != hostname:
a53771
     os.rename(realm.keytab, realm.keytab + '.save')
a53771
     canonprinc = 'host/' + canonname
a53771
diff --git a/src/util/k5test.py b/src/util/k5test.py
a53771
index 789b0f4b9..251d11a9d 100644
a53771
--- a/src/util/k5test.py
a53771
+++ b/src/util/k5test.py
a53771
@@ -155,6 +155,10 @@ Scripts may use the following functions and variables:
a53771
 * password(name): Return a weakly random password based on name.  The
a53771
   password will be consistent across calls with the same name.
a53771
 
a53771
+* canonicalize_hostname(name, rdns=True): Return the DNS
a53771
+  canonicalization of name, optionally using reverse DNS.  On error,
a53771
+  return name converted to lowercase.
a53771
+
a53771
 * stop_daemon(proc): Stop a daemon process started with
a53771
   realm.start_server() or realm.start_in_inetd().  Only necessary if
a53771
   the port needs to be reused; daemon processes will be stopped
a53771
@@ -458,6 +462,24 @@ def password(name):
a53771
     return name + str(os.getpid())
a53771
 
a53771
 
a53771
+def canonicalize_hostname(name, rdns=True):
a53771
+    """Canonicalize name using DNS, optionally with reverse DNS."""
a53771
+    try:
a53771
+        ai = socket.getaddrinfo(name, None, 0, 0, 0, socket.AI_CANONNAME)
a53771
+    except socket.gaierror as e:
a53771
+        return name.lower()
a53771
+    (family, socktype, proto, canonname, sockaddr) = ai[0]
a53771
+
a53771
+    if not rdns:
a53771
+        return canonname.lower()
a53771
+
a53771
+    try:
a53771
+        rname = socket.getnameinfo(sockaddr, socket.NI_NAMEREQD)
a53771
+    except socket.gaierror:
a53771
+        return canonname.lower()
a53771
+    return rname[0].lower()
a53771
+
a53771
+
a53771
 # Exit handler which ensures processes are cleaned up and, on failure,
a53771
 # prints messages to help developers debug the problem.
a53771
 def _onexit():