Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderKeynes committed Aug 26, 2024
1 parent 9eb64dc commit 4be803c
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ldap3::{LdapConnAsync, LdapConnSettings};
use crate::errors::{ClientIdentifier, Error};
use crate::pool::BanReason;
/// Handle clients by pretending to be a PostgreSQL server.
use bytes::{Buf, BufMut, BytesMut};
use ldap3::{LdapConnAsync, LdapConnSettings};
use log::{debug, error, info, trace, warn};
use once_cell::sync::Lazy;
use std::collections::{HashMap, VecDeque};
Expand Down Expand Up @@ -414,14 +414,18 @@ pub async fn startup_tls(
}
}


// Pass in username and password to authenticate against LDAP
async fn authenticate_ldap(username: &str, password: &str, ldapurl: &str, ldapsuffix: &str) -> bool {
async fn authenticate_ldap(
username: &str,
password: &str,
ldapurl: &str,
ldapsuffix: &str,
) -> bool {
// Connection to the LDAP Server
let ldap_conn_settings = LdapConnSettings::new();
let (conn, mut ldap) =
LdapConnAsync::with_settings(
ldap_conn_settings, ldapurl).await.unwrap();
let (conn, mut ldap) = LdapConnAsync::with_settings(ldap_conn_settings, ldapurl)
.await
.unwrap();
ldap3::drive!(conn);

// Takes the username provided and converts it into an email for validation
Expand All @@ -430,7 +434,11 @@ async fn authenticate_ldap(username: &str, password: &str, ldapurl: &str, ldapsu

// Attempts a simple bind using the passed in values of username and Password
println!("{:?}", password);
let result = ldap.simple_bind(email.as_str(), &password).await.unwrap().success();
let result = ldap
.simple_bind(email.as_str(), &password)
.await
.unwrap()
.success();
ldap.unbind().await.unwrap();

// If the authentication is successful return true, else return false.
Expand All @@ -440,7 +448,6 @@ async fn authenticate_ldap(username: &str, password: &str, ldapurl: &str, ldapsu
}
}


impl<S, T> Client<S, T>
where
S: tokio::io::AsyncRead + std::marker::Unpin,
Expand Down Expand Up @@ -575,8 +582,7 @@ where

return Err(error);
}
}
else if let "ldap" = config.general.admin_auth_type.as_str() {
} else if let "ldap" = config.general.admin_auth_type.as_str() {
clear_text_challenge(&mut write).await?;
let code = match read.read_u8().await {
Ok(p) => p,
Expand Down Expand Up @@ -624,14 +630,15 @@ where
&str_password,
&config.general.admin_auth_ldapurl.unwrap(),
&config.general.admin_auth_ldapsuffix.unwrap(),
).await;
)
.await;
if unsuccessful_auth {
wrong_password(&mut write, username).await?;
wrong_password(&mut write, username).await?;

return Err(Error::ClientGeneralError(
"Invalid password".into(),
client_identifier,
));
return Err(Error::ClientGeneralError(
"Invalid password".into(),
client_identifier,
));
}
}
(false, generate_server_parameters_for_admin())
Expand Down Expand Up @@ -790,9 +797,7 @@ where
));
}
}
}

else if let "ldap" = pool.settings.user.auth_type.as_str() {
} else if let "ldap" = pool.settings.user.auth_type.as_str() {
clear_text_challenge(&mut write).await?;
let code = match read.read_u8().await {
Ok(p) => p,
Expand Down Expand Up @@ -840,16 +845,16 @@ where
&str_password,
&pool.settings.user.auth_ldapurl.clone().unwrap(),
&pool.settings.user.auth_ldapsuffix.clone().unwrap(),
).await;
)
.await;
if unsuccessful_auth {
wrong_password(&mut write, username).await?;
wrong_password(&mut write, username).await?;

return Err(Error::ClientGeneralError(
"Invalid password".into(),
client_identifier,
));
return Err(Error::ClientGeneralError(
"Invalid password".into(),
client_identifier,
));
}

}
let transaction_mode = pool.settings.pool_mode == PoolMode::Transaction;
prepared_statements_enabled =
Expand Down

0 comments on commit 4be803c

Please sign in to comment.