2023-07-17 00:38:02 +01:00
|
|
|
use chrono::{Datelike, Utc};
|
2023-07-29 22:19:15 +01:00
|
|
|
use dotenvy::dotenv;
|
2023-07-30 01:19:19 +01:00
|
|
|
use ldap3::{LdapConn, Mod};
|
|
|
|
use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, Config};
|
|
|
|
use sqlx::{Pool, Sqlite};
|
|
|
|
use std::{collections::HashSet, env, error::Error};
|
2023-06-18 17:19:59 +01:00
|
|
|
|
|
|
|
#[async_std::main]
|
|
|
|
async fn main() -> tide::Result<()> {
|
|
|
|
let config = get_config();
|
|
|
|
|
2023-06-18 19:10:46 +01:00
|
|
|
update_users(&config).await?;
|
2023-06-18 17:19:59 +01:00
|
|
|
update_admin(&config).await?;
|
2023-06-18 18:37:44 +01:00
|
|
|
update_committee(&config).await?;
|
2023-06-18 18:34:27 +01:00
|
|
|
|
2023-06-18 17:19:59 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-18 19:10:46 +01:00
|
|
|
async fn update_users(config: &Config) -> tide::Result<()> {
|
2023-07-17 00:14:48 +01:00
|
|
|
let mut users_tmp = HashSet::new();
|
|
|
|
// default user to ensure group is never empty
|
|
|
|
users_tmp.insert(String::from("compsoc"));
|
2023-06-18 19:10:46 +01:00
|
|
|
|
|
|
|
if let Ok(x) = env::var("USERS_LIFETIME") {
|
|
|
|
for user in x.split(',').collect::<Vec<&str>>() {
|
2023-07-17 00:14:48 +01:00
|
|
|
users_tmp.insert(user.to_string());
|
2023-06-18 19:10:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
pull in data from wolves (csv or api (hopefully api)
|
|
|
|
pull entire ldap data
|
|
|
|
|
|
|
|
for every valid user in wolves match to ldap
|
|
|
|
add to users
|
|
|
|
*/
|
2023-07-17 00:14:48 +01:00
|
|
|
// pull from wolves csv
|
|
|
|
for user in from_csv(config).await.unwrap_or_default() {
|
|
|
|
users_tmp.insert(user);
|
|
|
|
}
|
2023-06-18 19:10:46 +01:00
|
|
|
|
|
|
|
// sorting makes it easier/faster
|
|
|
|
if let Ok(x) = env::var("USERS_BANNED") {
|
|
|
|
for user in x.split(',').collect::<Vec<&str>>() {
|
2023-07-17 00:14:48 +01:00
|
|
|
users_tmp.remove(user);
|
2023-06-18 19:10:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// easier to work with Strings above but easier to work with &str below
|
|
|
|
let users: Vec<&str> = users_tmp.iter().map(|s| &**s).collect();
|
|
|
|
|
|
|
|
update_group(config, "skynet-users", &users, true).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-06-18 18:34:27 +01:00
|
|
|
|
|
|
|
fn uid_to_dn(uid: &str) -> String {
|
2023-06-18 17:19:59 +01:00
|
|
|
format!("uid={},ou=users,dc=skynet,dc=ie", uid)
|
|
|
|
}
|
|
|
|
|
2023-06-18 18:34:27 +01:00
|
|
|
async fn update_admin(config: &Config) -> tide::Result<()> {
|
2023-06-18 18:29:49 +01:00
|
|
|
dotenv().ok();
|
2023-06-18 18:34:27 +01:00
|
|
|
|
2023-06-18 18:29:49 +01:00
|
|
|
// read from teh env
|
|
|
|
if let Ok(x) = env::var("USERS_ADMIN") {
|
|
|
|
let users = x.split(',').collect::<Vec<&str>>();
|
2023-06-18 18:34:27 +01:00
|
|
|
|
|
|
|
update_group(config, "skynet-admins", &users, true).await?;
|
2023-06-18 18:29:49 +01:00
|
|
|
// admins automatically get added as users
|
2023-06-18 18:34:27 +01:00
|
|
|
update_group(config, "skynet-users", &users, false).await?;
|
2023-06-18 18:29:49 +01:00
|
|
|
}
|
2023-06-18 17:33:39 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-18 18:37:44 +01:00
|
|
|
async fn update_committee(config: &Config) -> tide::Result<()> {
|
|
|
|
dotenv().ok();
|
|
|
|
|
|
|
|
// read from teh env
|
|
|
|
if let Ok(x) = env::var("USERS_COMMITTEE") {
|
|
|
|
let users = x.split(',').collect::<Vec<&str>>();
|
|
|
|
|
|
|
|
update_group(config, "skynet-committee", &users, true).await?;
|
|
|
|
// admins automatically get added as users
|
|
|
|
update_group(config, "skynet-users", &users, false).await?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-18 18:34:27 +01:00
|
|
|
async fn update_group(config: &Config, group: &str, users: &[&str], replace: bool) -> tide::Result<()> {
|
2023-06-18 20:47:36 +01:00
|
|
|
if users.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-06-18 17:19:59 +01:00
|
|
|
let mut ldap = LdapConn::new(&config.ldap_host)?;
|
|
|
|
|
|
|
|
// use the admin account
|
|
|
|
ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw)?.success()?;
|
2023-06-18 17:33:39 +01:00
|
|
|
|
2023-06-18 18:32:52 +01:00
|
|
|
let dn = format!("cn={},ou=groups,dc=skynet,dc=ie", group);
|
2023-06-18 18:34:27 +01:00
|
|
|
let members = users.iter().map(|uid| uid_to_dn(uid)).collect();
|
2023-06-18 17:45:33 +01:00
|
|
|
let mods = if replace {
|
2023-06-18 18:32:52 +01:00
|
|
|
vec![Mod::Replace("member".to_string(), members)]
|
2023-06-18 17:45:33 +01:00
|
|
|
} else {
|
2023-06-18 18:32:52 +01:00
|
|
|
vec![Mod::Add("member".to_string(), members)]
|
2023-06-18 17:45:33 +01:00
|
|
|
};
|
2023-06-18 18:34:27 +01:00
|
|
|
|
2023-06-18 18:32:52 +01:00
|
|
|
if let Err(x) = ldap.modify(&dn, mods) {
|
2023-06-18 17:45:33 +01:00
|
|
|
println!("{:?}", x);
|
|
|
|
}
|
2023-06-18 17:33:39 +01:00
|
|
|
|
2023-06-18 18:32:52 +01:00
|
|
|
let dn_linux = format!("cn={}-linux,ou=groups,dc=skynet,dc=ie", group);
|
2023-06-18 18:34:27 +01:00
|
|
|
let members_linux = users.iter().map(|uid| uid.to_string()).collect();
|
2023-06-18 17:45:33 +01:00
|
|
|
let mods = if replace {
|
2023-06-18 18:32:52 +01:00
|
|
|
vec![Mod::Replace("memberUid".to_string(), members_linux)]
|
2023-06-18 17:45:33 +01:00
|
|
|
} else {
|
2023-06-18 18:32:52 +01:00
|
|
|
vec![Mod::Add("memberUid".to_string(), members_linux)]
|
2023-06-18 17:45:33 +01:00
|
|
|
};
|
2023-06-18 18:34:27 +01:00
|
|
|
if let Err(x) = ldap.modify(&dn_linux, mods) {
|
2023-06-18 17:45:33 +01:00
|
|
|
println!("{:?}", x);
|
|
|
|
};
|
2023-06-18 17:19:59 +01:00
|
|
|
|
2023-06-18 18:32:52 +01:00
|
|
|
// tidy up
|
2023-06-18 17:19:59 +01:00
|
|
|
ldap.unbind()?;
|
2023-06-18 17:33:39 +01:00
|
|
|
|
2023-06-18 17:19:59 +01:00
|
|
|
Ok(())
|
2023-06-18 18:34:27 +01:00
|
|
|
}
|
2023-07-16 23:29:52 +01:00
|
|
|
|
2023-07-17 00:14:48 +01:00
|
|
|
async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
|
2023-07-30 01:19:19 +01:00
|
|
|
let db = db_init(config).await.unwrap();
|
|
|
|
|
2023-07-16 23:29:52 +01:00
|
|
|
let mut uids = HashSet::new();
|
|
|
|
|
2023-07-17 01:24:52 +01:00
|
|
|
let records = read_csv(config)?;
|
2023-07-17 00:14:48 +01:00
|
|
|
|
2023-07-17 00:38:02 +01:00
|
|
|
let now = Utc::now();
|
|
|
|
let today = format!("{}-{:02}-{:02}", now.year(), now.month(), now.day());
|
|
|
|
|
2023-07-16 23:29:52 +01:00
|
|
|
for record in records {
|
2023-07-17 00:38:02 +01:00
|
|
|
// only import users if it is actually active.
|
|
|
|
if record.expiry < today {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-30 01:19:19 +01:00
|
|
|
if let Some(uid) = account_mail_get_uid(&db, &record.email).await {
|
|
|
|
uids.insert(uid);
|
|
|
|
} else if let Some(uid) = account_id_get_uid(&db, &record.id_student).await {
|
|
|
|
uids.insert(uid);
|
2023-07-16 23:29:52 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-17 00:14:48 +01:00
|
|
|
|
2023-07-16 23:29:52 +01:00
|
|
|
Ok(uids)
|
|
|
|
}
|
2023-07-30 01:19:19 +01:00
|
|
|
|
|
|
|
async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> {
|
|
|
|
match sqlx::query_as::<_, Accounts>(
|
|
|
|
r#"
|
|
|
|
SELECT user
|
|
|
|
FROM accounts
|
|
|
|
WHERE mail == ?
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(mail)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(res) => {
|
|
|
|
if res.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(res[0].user.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn account_id_get_uid(db: &Pool<Sqlite>, id: &str) -> Option<String> {
|
|
|
|
match sqlx::query_as::<_, Accounts>(
|
|
|
|
r#"
|
|
|
|
SELECT student_id
|
|
|
|
FROM accounts
|
|
|
|
WHERE mail == ?
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(id)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(res) => {
|
|
|
|
if res.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(res[0].student_id.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|