@@ -416,20 +416,20 @@ pub async fn startup_tls(
416
416
417
417
418
418
// Pass in username and password to authenticate against LDAP
419
- async fn authenticate_ldap ( username : & str , password : & str ) -> bool {
419
+ async fn authenticate_ldap ( username : & str , password : & str , ldapurl : & str , ldapsuffix : & str ) -> bool {
420
420
// Connection to the LDAP Server
421
421
let ldap_conn_settings = LdapConnSettings :: new ( ) ;
422
422
let ( conn, mut ldap) =
423
423
LdapConnAsync :: with_settings (
424
- ldap_conn_settings, "ldap://127.0.0.1:3893" ) . await . unwrap ( ) ;
424
+ ldap_conn_settings, ldapurl ) . await . unwrap ( ) ;
425
425
ldap3:: drive!( conn) ;
426
426
427
427
// Takes the username provided and converts it into an email for validation
428
428
// This is required because LDAP uses either the Distinguished name or Email in order to bind. Username alone will not work :/
429
- let email = format ! ( "{}@example.com " , username) ;
429
+ let email = format ! ( "{}{} " , username, ldapsuffix ) ;
430
430
431
431
// Attempts a simple bind using the passed in values of username and Password
432
- println ! ( "{}" , password) ;
432
+ println ! ( "{:? }" , password) ;
433
433
let result = ldap. simple_bind ( email. as_str ( ) , & password) . await . unwrap ( ) . success ( ) ;
434
434
ldap. unbind ( ) . await . unwrap ( ) ;
435
435
@@ -617,15 +617,14 @@ where
617
617
) )
618
618
}
619
619
} ;
620
- let str_password = std:: str:: from_utf8 ( & password_response) . unwrap ( ) ;
620
+ let str_password = String :: from_utf8 ( password_response) . unwrap ( ) ;
621
+ let str_password = str_password. trim_matches ( char:: from ( 0 ) ) ;
621
622
let unsuccessful_auth = !authenticate_ldap (
622
623
& config. general . admin_username ,
623
- str_password,
624
- // "dogood",
625
- // config.general.admin_auth_ldapurl.expect("ldapurl not set").as_str(),
626
- // config.general.admin_auth_ldapsuffix.expect("ldapsuffix not set").as_str(),
624
+ & str_password,
625
+ & config. general . admin_auth_ldapurl . unwrap ( ) ,
626
+ & config. general . admin_auth_ldapsuffix . unwrap ( ) ,
627
627
) . await ;
628
- println ! ( "{}" , unsuccessful_auth) ;
629
628
if unsuccessful_auth {
630
629
wrong_password ( & mut write, username) . await ?;
631
630
@@ -794,6 +793,62 @@ where
794
793
}
795
794
796
795
else if let "ldap" = pool. settings . user . auth_type . as_str ( ) {
796
+ clear_text_challenge ( & mut write) . await ?;
797
+ let code = match read. read_u8 ( ) . await {
798
+ Ok ( p) => p,
799
+ Err ( _) => {
800
+ return Err ( Error :: ClientSocketError (
801
+ "password code" . into ( ) ,
802
+ client_identifier,
803
+ ) )
804
+ }
805
+ } ;
806
+
807
+ // PasswordMessage
808
+ if code as char != 'p' {
809
+ return Err ( Error :: ProtocolSyncError ( format ! (
810
+ "Expected p, got {}" ,
811
+ code as char
812
+ ) ) ) ;
813
+ }
814
+
815
+ let len = match read. read_i32 ( ) . await {
816
+ Ok ( len) => len,
817
+ Err ( _) => {
818
+ return Err ( Error :: ClientSocketError (
819
+ "password message length" . into ( ) ,
820
+ client_identifier,
821
+ ) )
822
+ }
823
+ } ;
824
+
825
+ let mut password_response = vec ! [ 0u8 ; ( len - 4 ) as usize ] ;
826
+
827
+ match read. read_exact ( & mut password_response) . await {
828
+ Ok ( _) => ( ) ,
829
+ Err ( _) => {
830
+ return Err ( Error :: ClientSocketError (
831
+ "password message" . into ( ) ,
832
+ client_identifier,
833
+ ) )
834
+ }
835
+ } ;
836
+ let str_password = String :: from_utf8 ( password_response) . unwrap ( ) ;
837
+ let str_password = str_password. trim_matches ( char:: from ( 0 ) ) ;
838
+ let unsuccessful_auth = !authenticate_ldap (
839
+ & pool. settings . user . username . as_str ( ) ,
840
+ & str_password,
841
+ & pool. settings . user . auth_ldapurl . clone ( ) . unwrap ( ) ,
842
+ & pool. settings . user . auth_ldapsuffix . clone ( ) . unwrap ( ) ,
843
+ ) . await ;
844
+ if unsuccessful_auth {
845
+ wrong_password ( & mut write, username) . await ?;
846
+
847
+ return Err ( Error :: ClientGeneralError (
848
+ "Invalid password" . into ( ) ,
849
+ client_identifier,
850
+ ) ) ;
851
+ }
797
852
798
853
}
799
854
let transaction_mode = pool. settings . pool_mode == PoolMode :: Transaction ;
0 commit comments