move persist data to /var/run/crab

This commit is contained in:
Tyler Murphy 2022-11-09 15:47:04 -05:00
parent a751327e28
commit dcd28fd14a
2 changed files with 30 additions and 22 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target
Cargo.lock
test.sh

View file

@ -16,7 +16,7 @@ pub fn get_persist(user: &str) -> bool {
Some(data) => data,
None => return false
};
return now() - timestamp < PERSIST_TIME;
return now() - timestamp < PERSIST_TIME && timestamp < now();
}
pub fn set_persist(user: &str) {
@ -49,26 +49,12 @@ fn get_terminal_process() -> Option<i32> {
Some(stat.session)
}
fn is_file_root_only(id: &i32) -> bool {
let metadata = match std::fs::metadata(path(&id)) {
Ok(data) => data,
Err(e) => {
if let Some(err) = e.raw_os_error() {
return err == 2;
}
return true
}
};
let perms = metadata.permissions();
return perms.mode() == 33200 && metadata.st_uid() == 0 && metadata.st_gid() == 0;
}
fn get_terminal_config() -> Option<Value> {
let id = match get_terminal_process() {
Some(data) => data,
None => return None
};
if !is_file_root_only(&id) {
if !is_file_root(&path(&id)) {
return None;
}
let data = match std::fs::read_to_string(path(&id)) {
@ -83,20 +69,41 @@ fn get_terminal_config() -> Option<Value> {
}
fn write_terminal_config(id: &i32, data: &str) -> Result<(), Box<dyn std::error::Error>> {
std::fs::create_dir_all("/var/run/crab")?;
make_file_root("/var/run/crab")?;
std::fs::write(path(&id), "")?;
unistd::chown(std::path::Path::new(&path(&id)), Some(unistd::Uid::from(0)), Some(unistd::Gid::from(0)))?;
let metadata = std::fs::metadata(path(&id))?;
let mut perms = metadata.permissions();
perms.set_mode(0o660);
fs::set_permissions(path(&id), perms)?;
make_file_root(&path(&id))?;
std::fs::write(path(&id), data)?;
Ok(())
}
fn make_file_root(path: &str) -> Result<(), Box<dyn std::error::Error>> {
unistd::chown(std::path::Path::new(path), Some(unistd::Uid::from(0)), Some(unistd::Gid::from(0)))?;
let metadata = std::fs::metadata(path)?;
let mut perms = metadata.permissions();
perms.set_mode(0o100600);
fs::set_permissions(path, perms)?;
Ok(())
}
fn is_file_root(path: &str) -> bool {
let metadata = match std::fs::metadata(path) {
Ok(data) => data,
Err(e) => {
if let Some(err) = e.raw_os_error() {
return err == 2;
}
return true
}
};
let perms = metadata.permissions();
return perms.mode() == 0o100600 && metadata.st_uid() == 0 && metadata.st_gid() == 0;
}
fn now() -> u64 {
return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
}
fn path(id: &i32) -> String {
return format!("/tmp/crab-{}", id);
return format!("/var/run/crab/{}", id);
}