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 {
|
pub trait DatabaseInterface: Clone + Send + Sync + 'static {
|
||||||
async fn get_user(&self, user: &UserRef) -> Result<Option<User>, BoxedError>;
|
fn get_user(&self, user: &UserRef) -> impl std::future::Future<Output = Result<Option<User>, BoxedError>> + Send;
|
||||||
async fn create_user(
|
async fn create_user(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: User,
|
user: User,
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,30 @@ use ldap3_proto::LdapMsg;
|
||||||
use ldap3_proto::proto::LdapOp;
|
use ldap3_proto::proto::LdapOp;
|
||||||
|
|
||||||
use crate::db::{Database, DatabaseInterface};
|
use crate::db::{Database, DatabaseInterface};
|
||||||
|
use crate::error::GlobalError;
|
||||||
use crate::ldap::{
|
use crate::ldap::{
|
||||||
LdapClientState, LdapStream, LdapStreamError, op_bind, op_ext, search_by_mail_filter,
|
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))]
|
#[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>) {
|
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 filter;
|
||||||
mod handler;
|
mod handler;
|
||||||
mod op;
|
mod op;
|
||||||
pub use handler::ldap_handler;
|
pub use handler::ldap_listen;
|
||||||
pub use op::bind::{BindDn, op_bind};
|
pub use op::bind::{BindDn, op_bind};
|
||||||
pub use op::ext::op_ext;
|
pub use op::ext::op_ext;
|
||||||
pub use op::search::search_by_mail_filter;
|
pub use op::search::search_by_mail_filter;
|
||||||
|
|
|
||||||
10
src/main.rs
10
src/main.rs
|
|
@ -10,7 +10,7 @@ mod stream;
|
||||||
use cli::CliArgs;
|
use cli::CliArgs;
|
||||||
use db::{Database, DatabaseInterface, MemoryDatabase, User};
|
use db::{Database, DatabaseInterface, MemoryDatabase, User};
|
||||||
use error::GlobalError;
|
use error::GlobalError;
|
||||||
use ldap::ldap_handler;
|
use ldap::ldap_listen;
|
||||||
use listener::ListenerPath;
|
use listener::ListenerPath;
|
||||||
|
|
||||||
async fn create_dummy_users<D: DatabaseInterface>(db: &mut Database<D>) {
|
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();
|
let mut db = MemoryDatabase::new();
|
||||||
create_dummy_users(&mut db).await;
|
create_dummy_users(&mut db).await;
|
||||||
|
|
||||||
// If the connection is None, it's because the client aborted early
|
let ldap_db = db.clone();
|
||||||
// so there's nothing to do about it.
|
tokio::spawn(ldap_listen(listener, ldap_db));
|
||||||
while let Some(stream) = listener.accept_ldap().await? {
|
|
||||||
let db = db.clone();
|
|
||||||
tokio::spawn(ldap_handler(stream, db));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue