1
1
"""
2
2
Compatibility library for older versions of python and requests_kerberos
3
3
"""
4
+ import socket
4
5
import sys
5
6
6
7
import gssapi
@@ -23,7 +24,8 @@ class HTTPKerberosAuth(HTTPSPNEGOAuth):
23
24
"""Deprecated compat shim; see HTTPSPNEGOAuth instead."""
24
25
def __init__ (self , mutual_authentication = DISABLED , service = "HTTP" ,
25
26
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 ):
27
29
# put these here for later
28
30
self .principal = principal
29
31
self .service = service
@@ -36,12 +38,27 @@ def __init__(self, mutual_authentication=DISABLED, service="HTTP",
36
38
delegate = delegate ,
37
39
opportunistic_auth = force_preemptive ,
38
40
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 )
40
44
41
45
def generate_request_header (self , response , host , is_preemptive = False ):
42
46
# This method needs to be shimmed because `host` isn't exposed to
43
47
# __init__() and we need to derive things from it. Also, __init__()
44
48
# 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
45
62
try :
46
63
if self .principal is not None :
47
64
gss_stage = "acquiring credentials"
@@ -55,7 +72,7 @@ def generate_request_header(self, response, host, is_preemptive=False):
55
72
# name-based HTTP hosting)
56
73
if self .service is not None :
57
74
gss_stage = "initiating context"
58
- kerb_host = host
75
+ kerb_host = canonhost
59
76
if self .hostname_override :
60
77
kerb_host = self .hostname_override
61
78
0 commit comments