summaryrefslogtreecommitdiff
path: root/src/persist.rs
diff options
context:
space:
mode:
authortylermurphy534 <tylermurphy534@gmail.com>2022-11-19 17:37:56 +0000
committertylermurphy534 <tylermurphy534@gmail.com>2022-11-19 17:37:56 +0000
commitce6af95b74106d65c3d12f745d215732076562b9 (patch)
tree6f6c2ef19b93c80b9cc4567a4a94ce3621ff8eb4 /src/persist.rs
parentupdate aur depends (diff)
parentfix install file part 2 (diff)
downloadcrab-ce6af95b74106d65c3d12f745d215732076562b9.tar.gz
crab-ce6af95b74106d65c3d12f745d215732076562b9.tar.bz2
crab-ce6af95b74106d65c3d12f745d215732076562b9.zip
Merge pull request '0.6.0' (#1) from dev into main
Reviewed-on: https://g.tylerm.dev/tylermurphy534/crab/pulls/1
Diffstat (limited to 'src/persist.rs')
-rw-r--r--src/persist.rs147
1 files changed, 97 insertions, 50 deletions
diff --git a/src/persist.rs b/src/persist.rs
index 6a813dc..2bca386 100644
--- a/src/persist.rs
+++ b/src/persist.rs
@@ -1,69 +1,116 @@
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() {
- Some(data) => data,
- None => return false
- };
- let timestamp = match json[user].as_u64() {
- Some(data) => data,
- None => return false
- };
- return now() - timestamp < PERSIST_TIME && timestamp - 1 < now();
+ let json = match get_persist_config() {
+ Some(data) => data,
+ None => return false
+ };
+ let timestamp = match json[user].as_u64() {
+ Some(data) => data,
+ None => return false
+ };
+ return now() - timestamp < PERSIST_TIME && timestamp - 1 < now();
}
+
+/// Updates a user in 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() {
- Some(data) => data,
- None => return
- };
- json[user] = Value::from(now());
- let id = match get_terminal_process() {
- Some(data) => data,
- None => return
- };
- match secure::write_file(PERSIST_PATH, &format!("{}", id), &json.to_string()) {
- Ok(_) => {},
- Err(e) => {
- eprintln!("Internal Error: {}", e)
- }
- };
+ let mut json = match get_persist_config() {
+ Some(data) => data,
+ None => return
+ };
+ json[user] = Value::from(now());
+ let session = match get_current_session() {
+ Some(data) => data,
+ None => return
+ };
+ match secure::write_file(PERSIST_PATH, &format!("{}", session), &json.to_string()) {
+ Ok(_) => {},
+ Err(_) => {
+ eprintln!("crab: An internal error has occured");
+ }
+ };
+}
+
+
+/// Removes a user from the current sessions persist file
+/// #### Arguments
+/// * `user` - The user to set non-persisted
+pub fn remove_persist(user: &str) {
+ let mut json = match get_persist_config() {
+ Some(data) => data,
+ None => return
+ };
+ json[user] = Value::from(0);
+ let session = match get_current_session() {
+ Some(data) => data,
+ None => return
+ };
+ match secure::write_file(PERSIST_PATH, &format!("{}", session), &json.to_string()) {
+ Ok(_) => {},
+ Err(_) => {
+ eprintln!("crab: An internal error has occured");
+ }
+ };
}
-fn get_terminal_process() -> Option<i32> {
- let id: i32 = match std::process::id().try_into() {
- Ok(data) => data,
- Err(_) => return None
- };
- let stat = match procinfo::pid::stat(id) {
- Ok(data) => data,
- Err(_) => return None
- };
- Some(stat.session)
+
+/// 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
+ };
+ let pid_stat = match procinfo::pid::stat(pid) {
+ Ok(data) => data,
+ Err(_) => return None
+ };
+ Some(pid_stat.session)
}
-fn get_terminal_config() -> Option<Value> {
- let id = match get_terminal_process() {
- Some(data) => data,
- None => return None
- };
- let data = match secure::read_file(PERSIST_PATH, &format!("{}", id)) {
- Some(data) => data,
- None => "{}".to_string()
- };
- let json: Value = match serde_json::from_str(&data) {
- Ok(data) => data,
- Err(_) => return None
- };
- Some(json)
+
+/// 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!("{}", session)) {
+ Some(data) => data,
+ None => "{}".to_string()
+ };
+ let json: Value = match serde_json::from_str(&data) {
+ Ok(data) => data,
+ Err(_) => return None
+ };
+ 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();
+ return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
}