1
1
use cross_krb5:: { ClientCtx , InitiateFlags , K5Ctx , PendingClientCtx , Step } ;
2
- use hickory_resolver:: proto:: rr:: RData ;
3
2
4
3
use crate :: {
5
4
bson:: Bson ,
@@ -324,21 +323,24 @@ async fn canonicalize_hostname(
324
323
let resolver =
325
324
crate :: runtime:: AsyncResolver :: new ( resolver_config. map ( |c| c. inner . clone ( ) ) ) . await ?;
326
325
327
- match mode {
326
+ let hostname = match mode {
328
327
CanonicalizeHostName :: Forward => {
329
328
let lookup_records = resolver. cname_lookup ( hostname) . await ?;
330
329
331
- if let Some ( first_record) = lookup_records. records ( ) . first ( ) {
332
- if let Some ( RData :: CNAME ( cname) ) = first_record. data ( ) {
333
- Ok ( cname. to_lowercase ( ) . to_string ( ) )
334
- } else {
335
- Ok ( hostname. to_string ( ) )
336
- }
330
+ if !lookup_records. records ( ) . is_empty ( ) {
331
+ // As long as there is a record, we can return the original hostname.
332
+ // Although the spec says to return the canonical name, this is not
333
+ // done by any drivers in practice since the majority of them use
334
+ // libraries that do not follow CNAME chains. Also, we do not want to
335
+ // use the canonical name since it will likely differ from the input
336
+ // name, and the use of the input name is required for the service
337
+ // principal to be accepted by the GSSAPI auth flow.
338
+ hostname. to_lowercase ( ) . to_string ( )
337
339
} else {
338
- Err ( Error :: authentication_error (
340
+ return Err ( Error :: authentication_error (
339
341
GSSAPI_STR ,
340
342
& format ! ( "No addresses found for hostname: {hostname}" ) ,
341
- ) )
343
+ ) ) ;
342
344
}
343
345
}
344
346
CanonicalizeHostName :: ForwardAndReverse => {
@@ -350,20 +352,27 @@ async fn canonicalize_hostname(
350
352
match resolver. reverse_lookup ( first_address) . await {
351
353
Ok ( reverse_lookup) => {
352
354
if let Some ( name) = reverse_lookup. iter ( ) . next ( ) {
353
- Ok ( name. to_lowercase ( ) . to_string ( ) )
355
+ name. to_lowercase ( ) . to_string ( )
354
356
} else {
355
- Ok ( hostname. to_lowercase ( ) )
357
+ hostname. to_lowercase ( )
356
358
}
357
359
}
358
- Err ( _) => Ok ( hostname. to_lowercase ( ) ) ,
360
+ Err ( _) => hostname. to_lowercase ( ) ,
359
361
}
360
362
} else {
361
- Err ( Error :: authentication_error (
363
+ return Err ( Error :: authentication_error (
362
364
GSSAPI_STR ,
363
365
& format ! ( "No addresses found for hostname: {hostname}" ) ,
364
- ) )
366
+ ) ) ;
365
367
}
366
368
}
367
369
CanonicalizeHostName :: None => unreachable ! ( ) ,
368
- }
370
+ } ;
371
+
372
+ // Sometimes reverse lookup results in a trailing "." since that is the correct
373
+ // way to present a FQDN. However, GSSAPI rejects the trailing "." so we remove
374
+ // it here manually.
375
+ let hostname = hostname. trim_end_matches ( "." ) ;
376
+
377
+ Ok ( hostname. to_string ( ) )
369
378
}
0 commit comments