87 lines
2.4 KiB
Rust
87 lines
2.4 KiB
Rust
// #![deny(warnings)]
|
|
|
|
mod cli;
|
|
mod db;
|
|
mod error;
|
|
#[cfg(feature = "http")]
|
|
mod http;
|
|
mod ldap;
|
|
mod listener;
|
|
mod stream;
|
|
|
|
#[cfg(feature = "http")]
|
|
use crate::http::http_listen;
|
|
use cli::CliArgs;
|
|
use db::{Database, DatabaseInterface, MemoryDatabase, Role, User};
|
|
use error::GlobalError;
|
|
use ldap::ldap_handler;
|
|
use listener::ListenerPath;
|
|
|
|
async fn create_dummy_users<D: DatabaseInterface>(db: &mut Database<D>) {
|
|
db.create_user(User {
|
|
username: "admin".to_string(),
|
|
domain: None,
|
|
password: "adminadmin".to_string(),
|
|
mail: "TODO".to_string(),
|
|
role: Role::Admin,
|
|
})
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
for domain in &["a", "b", "c"] {
|
|
db.create_user(User {
|
|
username: domain.to_string(),
|
|
domain: Some(format!("{domain}.localhost")),
|
|
password: "adminadmin".to_string(),
|
|
mail: format!("{domain}@{domain}.localhost"),
|
|
role: Role::DomainAdmin(format!("{domain}.localhost")),
|
|
})
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
db.create_user(User {
|
|
username: domain.to_string(),
|
|
domain: Some(format!("user{domain}.localhost")),
|
|
password: "adminadmin".to_string(),
|
|
mail: format!("user{domain}@{domain}.localhost"),
|
|
role: Role::User,
|
|
})
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
}
|
|
}
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
async fn main() -> Result<(), GlobalError> {
|
|
let cli: CliArgs = argh::from_env();
|
|
|
|
tracing_subscriber::fmt::init();
|
|
// let subscriber = tracing_subscriber::FmtSubscriber::new();
|
|
// .with_file(true)
|
|
// .with_line_number(true)
|
|
// .finish()
|
|
// .init();
|
|
// tracing::subscriber::set_global_default(subscriber)?;
|
|
|
|
let ldap_listener = ListenerPath::new(&cli.listen_ldap)?.listener().await?;
|
|
|
|
let mut db = MemoryDatabase::new();
|
|
create_dummy_users(&mut db).await;
|
|
|
|
#[cfg(feature = "http")]
|
|
{
|
|
let http_listener = ListenerPath::new(&cli.listen_http)?.listener().await?;
|
|
let http_db = db.clone();
|
|
tokio::spawn(http_listen(http_listener, http_db));
|
|
}
|
|
|
|
// If the connection is None, it's because the client aborted early
|
|
// so there's nothing to do about it.
|
|
while let Some(stream) = ldap_listener.accept_ldap().await? {
|
|
let db = db.clone();
|
|
tokio::spawn(ldap_handler(stream, db));
|
|
}
|
|
|
|
Ok(())
|
|
}
|