11"""
22Compatibility library for older versions of python and requests_kerberos
33"""
4+ import socket
45import sys
56
67import gssapi
@@ -23,7 +24,8 @@ class HTTPKerberosAuth(HTTPSPNEGOAuth):
2324 """Deprecated compat shim; see HTTPSPNEGOAuth instead."""
2425 def __init__ (self , mutual_authentication = DISABLED , service = "HTTP" ,
2526 delegate = False , force_preemptive = False , principal = None ,
26- hostname_override = None , sanitize_mutual_error_response = True ):
27+ hostname_override = None , sanitize_mutual_error_response = True ,
28+ dns_canonicalize_hostname = False , use_reverse_dns = False ):
2729 # put these here for later
2830 self .principal = principal
2931 self .service = service
@@ -36,12 +38,27 @@ def __init__(self, mutual_authentication=DISABLED, service="HTTP",
3638 delegate = delegate ,
3739 opportunistic_auth = force_preemptive ,
3840 creds = None ,
39- sanitize_mutual_error_response = sanitize_mutual_error_response )
41+ sanitize_mutual_error_response = sanitize_mutual_error_response ,
42+ dns_canonicalize_hostname = dns_canonicalize_hostname ,
43+ use_reverse_dns = use_reverse_dns )
4044
4145 def generate_request_header (self , response , host , is_preemptive = False ):
4246 # This method needs to be shimmed because `host` isn't exposed to
4347 # __init__() and we need to derive things from it. Also, __init__()
4448 # can't fail, in the strictest compatability sense.
49+ canonhost = host
50+ if self .dns_canonicalize_hostname :
51+ try :
52+ ai = socket .getaddrinfo (host , 0 , flags = socket .AI_CANONNAME )
53+ canonhost = ai [0 ][3 ]
54+
55+ if self .use_reverse_dns :
56+ ni = socket .getnameinfo (ai [0 ][4 ], socket .NI_NAMEREQD )
57+ canonhost = ni [0 ]
58+
59+ except socket .gaierror as e :
60+ if e .errno == socket .EAI_MEMORY :
61+ raise e
4562 try :
4663 if self .principal is not None :
4764 gss_stage = "acquiring credentials"
@@ -55,7 +72,7 @@ def generate_request_header(self, response, host, is_preemptive=False):
5572 # name-based HTTP hosting)
5673 if self .service is not None :
5774 gss_stage = "initiating context"
58- kerb_host = host
75+ kerb_host = canonhost
5976 if self .hostname_override :
6077 kerb_host = self .hostname_override
6178
0 commit comments