@@ -375,6 +375,20 @@ def _auth_key(nonce, username, password):
375
375
return _unicode (md5hash .hexdigest ())
376
376
377
377
378
+ def _canonicalize_hostname (hostname ):
379
+ """Canonicalize hostname following MIT-krb5 behavior."""
380
+ # https://github.com/krb5/krb5/blob/d406afa363554097ac48646a29249c04f498c88e/src/util/k5test.py#L505-L520
381
+ af , socktype , proto , canonname , sockaddr = socket .getaddrinfo (
382
+ hostname , None , 0 , 0 , socket .IPPROTO_TCP , socket .AI_CANONNAME )[0 ]
383
+
384
+ try :
385
+ name = socket .getnameinfo (sockaddr , socket .NI_NAMEREQD )
386
+ except socket .gaierror :
387
+ return canonname .lower ()
388
+
389
+ return name [0 ].lower ()
390
+
391
+
378
392
def _authenticate_gssapi (credentials , sock_info ):
379
393
"""Authenticate using GSSAPI.
380
394
"""
@@ -390,7 +404,7 @@ def _authenticate_gssapi(credentials, sock_info):
390
404
# the security context. See RFC 4752, Section 3.1, first paragraph.
391
405
host = sock_info .address [0 ]
392
406
if props .canonicalize_host_name :
393
- host = socket . getfqdn (host )
407
+ host = _canonicalize_hostname (host )
394
408
service = props .service_name + '@' + host
395
409
if props .service_realm is not None :
396
410
service = service + '@' + props .service_realm
0 commit comments