diff --git a/.gitignore b/.gitignore index 67c759c..ceff09f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /target -/.idea \ No newline at end of file +/.idea + +.env \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..67e9187 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,44 @@ +use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; +use sqlx::{Error, Pool, Sqlite}; + +use std::str::FromStr; +use std::time::{SystemTime, UNIX_EPOCH}; +use tide::prelude::*; + +pub async fn db_init(database: &str) -> Result, Error> { + let pool = SqlitePoolOptions::new() + .max_connections(5) + .connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true)) + .await?; + + /* + // https://store.steampowered.com/api/appdetails?appids=1258740 + sqlx::query( + "CREATE TABLE IF NOT EXISTS store_details ( + id integer primary key, + name text not null, + item_type text not null, + last_timestamp integer not null + )", + ) + .execute(&pool) + .await?; + + */ + + // set up indexes? + /* + sqlx::query("CREATE INDEX IF NOT EXISTS index_estimate ON bus_results (valid_estimate)") + .execute(&pool) + .await?; + */ + Ok(pool) +} + +pub fn get_now() -> i64 { + if let Ok(x) = SystemTime::now().duration_since(UNIX_EPOCH) { + x.as_secs() as i64 + } else { + 0 + } +} diff --git a/src/main.rs b/src/main.rs index a9aacfa..8409759 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,73 @@ use ldap3::{LdapConn, Scope, SearchEntry, Mod}; use base64::{Engine as _, engine::general_purpose}; use crypto::{sha2::Sha512, digest::Digest}; +// for teh webserver +use sqlx::{Pool, Sqlite}; +use std::env; +use dotenv::dotenv; +use tide::prelude::*; +use tide::{Request, Response}; +use skynet_ldap_server::db_init; + + +#[derive(Clone)] +struct State { + db: Pool, + config: Config, +} + +#[async_std::main] +async fn main() -> tide::Result<()> { + let config = get_config(); + let db = db_init(&config.database).await?; + + let host_port = config.host_port.clone(); + + tide::log::start(); + + let state = State { + db, + config, + }; + + let mut app = tide::with_state(state); + + //app.at("/steam_ost/:username").get(results_get); + + app.listen(host_port).await?; + Ok(()) +} + + +#[derive(Debug, Clone)] +struct Config { + ldap_host: String, + database: String, + host_port: String, +} +fn get_config() -> Config { + dotenv().ok(); + + // reasonable defaults + let mut config = Config { + ldap_host: "".to_string(), + database: "database.db".to_string(), + host_port: "127.0.0.1:8087".to_string(), + }; + + if let Ok(x) = env::var("LDAP_HOST") { + config.key = x.trim().to_string(); + } + if let Ok(x) = env::var("DATABASE") { + config.database = x.trim().to_string(); + } + if let Ok(x) = env::var("HOST_PORT") { + config.host_port = x.trim().to_string(); + } + + config +} + //https://stackoverflow.com/a/44532957 pub fn hex_to_base64(hex: &str) -> String { // Make vector of bytes from octets @@ -21,20 +88,20 @@ pub fn hex_to_base64(hex: &str) -> String { } -fn main() -> Result<()> { +async fn post_update_ldap(mut req: Request) -> tide::Result { let mut ldap = LdapConn::new("ldaps://sso.skynet.ie")?; - + let user = "silver"; let pass = ""; let field = "sshPublicKey"; let value = "em232323232323"; - + let dn = format!("uid={},ou=users,dc=skynet,dc=ie", user); ldap.simple_bind(&dn, pass)?.success()?; // always assume insecure let mut secure = false; - + // get the users current password hash let (rs, _res) = ldap.search(&dn,Scope::Base,"(objectClass=*)",vec!["userPassword"])?.success()?; if !rs.is_empty() { @@ -56,17 +123,14 @@ fn main() -> Result<()> { // get it as hex string let hex = hasher.result_str(); - + // convert it to b64 pass_tmp = format!("{{SHA512}}{}", hex_to_base64(&hex)); pw_hashset.insert(pass_tmp.as_str()); mods.push(Mod::Replace("userPassword", pw_hashset)); }; - - let res = ldap.modify(&dn, mods)?.success()?; - - println!("{:?}", res); - - Ok(ldap.unbind()?) + ldap.unbind()?; + + Ok(format!("Hello, {}! I've put in an order for {} shoes", "name", "legs").into()) }