Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5683174746 | |||
| 1626fb0624 |
5 changed files with 28 additions and 11 deletions
|
|
@ -14,8 +14,8 @@ impl<D: DatabaseInterface> DatabaseInterface for Database<D> {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait DatabaseInterface {
|
||||
async fn get_user(&self, user: &UserRef) -> Result<Option<User>, BoxedError>;
|
||||
pub trait DatabaseInterface: Clone + Send + Sync + 'static {
|
||||
fn get_user(&self, user: &UserRef) -> impl std::future::Future<Output = Result<Option<User>, BoxedError>> + Send;
|
||||
async fn create_user(
|
||||
&mut self,
|
||||
user: User,
|
||||
|
|
|
|||
|
|
@ -2,9 +2,30 @@ use ldap3_proto::LdapMsg;
|
|||
use ldap3_proto::proto::LdapOp;
|
||||
|
||||
use crate::db::{Database, DatabaseInterface};
|
||||
use crate::error::GlobalError;
|
||||
use crate::ldap::{
|
||||
LdapClientState, LdapStream, LdapStreamError, op_bind, op_ext, search_by_mail_filter,
|
||||
};
|
||||
use crate::listener::Listener;
|
||||
|
||||
pub async fn ldap_listen<D: DatabaseInterface>(listener: Listener, db: Database<D>) {
|
||||
// If the connection is None, it's because the client aborted early
|
||||
// so there's nothing to do about it.
|
||||
loop {
|
||||
match listener.accept_ldap().await {
|
||||
Ok(Some(stream)) => {
|
||||
let db = db.clone();
|
||||
tokio::spawn(ldap_handler(stream, db));
|
||||
}
|
||||
Ok(None) => {
|
||||
panic!("LDAP listener closed");
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("Failed to listen on LDAP listener");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(name = "ldap", skip(stream, db), fields(session = %stream.session))]
|
||||
pub async fn ldap_handler<D: DatabaseInterface>(mut stream: LdapStream, mut db: Database<D>) {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ pub use dn::{Dn, MalformedDn};
|
|||
mod filter;
|
||||
mod handler;
|
||||
mod op;
|
||||
pub use handler::ldap_handler;
|
||||
pub use handler::ldap_listen;
|
||||
pub use op::bind::{BindDn, op_bind};
|
||||
pub use op::ext::op_ext;
|
||||
pub use op::search::search_by_mail_filter;
|
||||
|
|
|
|||
|
|
@ -153,7 +153,7 @@ fn search_entry_from_user(user: &User, req_attrs: &[String]) -> LdapSearchResult
|
|||
for attr in req_attrs {
|
||||
if let Some(attr_value) = match attr.as_str() {
|
||||
"uid" => Some(user.username.clone()),
|
||||
"cn"|"mail" => Some(user.mail.clone()),
|
||||
"cn" | "mail" => Some(user.mail.clone()),
|
||||
_ => {
|
||||
tracing::warn!("Ignoring unknown attr in search query: {attr}");
|
||||
None
|
||||
|
|
|
|||
10
src/main.rs
10
src/main.rs
|
|
@ -10,7 +10,7 @@ mod stream;
|
|||
use cli::CliArgs;
|
||||
use db::{Database, DatabaseInterface, MemoryDatabase, User};
|
||||
use error::GlobalError;
|
||||
use ldap::ldap_handler;
|
||||
use ldap::ldap_listen;
|
||||
use listener::ListenerPath;
|
||||
|
||||
async fn create_dummy_users<D: DatabaseInterface>(db: &mut Database<D>) {
|
||||
|
|
@ -44,12 +44,8 @@ async fn main() -> Result<(), GlobalError> {
|
|||
let mut db = MemoryDatabase::new();
|
||||
create_dummy_users(&mut db).await;
|
||||
|
||||
// If the connection is None, it's because the client aborted early
|
||||
// so there's nothing to do about it.
|
||||
while let Some(stream) = listener.accept_ldap().await? {
|
||||
let db = db.clone();
|
||||
tokio::spawn(ldap_handler(stream, db));
|
||||
}
|
||||
let ldap_db = db.clone();
|
||||
tokio::spawn(ldap_listen(listener, ldap_db));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue