diff options
Diffstat (limited to 'src/persist.rs')
-rw-r--r-- | src/persist.rs | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/src/persist.rs b/src/persist.rs index 667b12a..fe4ce69 100644 --- a/src/persist.rs +++ b/src/persist.rs @@ -1,13 +1,22 @@ use std::time::SystemTime; use serde_json::Value; - use crate::secure; + const PERSIST_TIME: u64 = 60 * 3; const PERSIST_PATH: &str = "/var/run/crab"; + +/// Returns true or false if a user is currently persisted from +/// a prior authentication. If the persist file had been tampered +/// with, or not trusted, it will be ignored, and return false. +/// #### Arguments +/// * `user` - The user to check if is persisted +/// #### Returns +/// * `true` - If the user is persisted +/// * `false` - If the user is not persisted, or if the persist file is not trusted pub fn get_persist(user: &str) -> bool { - let json = match get_terminal_config() { + let json = match get_persist_config() { Some(data) => data, None => return false }; @@ -18,17 +27,21 @@ pub fn get_persist(user: &str) -> bool { return now() - timestamp < PERSIST_TIME && timestamp - 1 < now(); } + +/// Updates the current sessions persist file +/// #### Arguments +/// * `user` - The user to set persisted pub fn set_persist(user: &str) { - let mut json = match get_terminal_config() { + let mut json = match get_persist_config() { Some(data) => data, None => serde_json::from_str("{}").unwrap() }; json[user] = Value::from(now()); - let id = match get_terminal_process() { + let session = match get_current_session() { Some(data) => data, None => return }; - match secure::write_file(PERSIST_PATH, &format!("{}", id), &json.to_string()) { + match secure::write_file(PERSIST_PATH, &format!("{}", session), &json.to_string()) { Ok(_) => {}, Err(_) => { eprintln!("crab: An Internal Has Error") @@ -36,7 +49,12 @@ pub fn set_persist(user: &str) { }; } -fn get_terminal_process() -> Option<i32> { + +/// Gets the current session that crab is running in +/// #### Returns +/// * `None` - If crab failed to get the current session +/// * `Some(i32)` - If the session is retrieved, returns the i32/pid_t session id +fn get_current_session() -> Option<i32> { let pid: i32 = match std::process::id().try_into() { Ok(data) => data, Err(_) => return None @@ -48,12 +66,17 @@ fn get_terminal_process() -> Option<i32> { Some(pid_stat.session) } -fn get_terminal_config() -> Option<Value> { - let id = match get_terminal_process() { + +/// Gets the current persist file for the current session +/// #### Returns +/// * `None` - If the persist file is untrusted or doesnt exist +/// * `Some(Value)` - If the persist file is retrieved, returns the serde_json Value of the file +fn get_persist_config() -> Option<Value> { + let session = match get_current_session() { Some(data) => data, None => return None }; - let data = match secure::read_file(PERSIST_PATH, &format!("{}", id)) { + let data = match secure::read_file(PERSIST_PATH, &format!("{}", session)) { Some(data) => data, None => "{}".to_string() }; @@ -64,6 +87,8 @@ fn get_terminal_config() -> Option<Value> { Some(json) } + +// Gets the current time in seconds since the Unix Epoch fn now() -> u64 { return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); } |