Skip to content

Commit e608ff4

Browse files
committed
PYTHON-2192 - Use krb5's canonicalization algorithm
1 parent a90f804 commit e608ff4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

.evergreen/run-enterprise-auth-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ if [ ${PLATFORM} != "Java" ]; then
1818
if [ "Windows_NT" = "$OS" ]; then
1919
echo "Setting GSSAPI_PASS"
2020
export GSSAPI_PASS=${SASL_PASS}
21+
export GSSAPI_CANONICALIZE="true"
2122
else
2223
# BUILD-3830
2324
touch ${PROJECT_DIRECTORY}/.evergreen/krb5.conf.empty

pymongo/auth.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,20 @@ def _auth_key(nonce, username, password):
375375
return _unicode(md5hash.hexdigest())
376376

377377

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+
378392
def _authenticate_gssapi(credentials, sock_info):
379393
"""Authenticate using GSSAPI.
380394
"""
@@ -390,7 +404,7 @@ def _authenticate_gssapi(credentials, sock_info):
390404
# the security context. See RFC 4752, Section 3.1, first paragraph.
391405
host = sock_info.address[0]
392406
if props.canonicalize_host_name:
393-
host = socket.getfqdn(host)
407+
host = _canonicalize_hostname(host)
394408
service = props.service_name + '@' + host
395409
if props.service_realm is not None:
396410
service = service + '@' + props.service_realm

0 commit comments

Comments
 (0)