delete the shadow entry along with the passwd one

This commit is contained in:
Dietrich 2020-10-30 19:09:56 +01:00
parent 4207e1dc0f
commit bacbfae4d1
3 changed files with 33 additions and 11 deletions

View File

@ -24,6 +24,13 @@ pub struct User {
} }
impl User { impl User {
pub fn get_shadow(&self) -> Option<&crate::Shadow> {
match self.password {
crate::Password::Encrypted(_) => None,
crate::Password::Shadow(ref s) => Some(s),
crate::Password::Disabled => None,
}
}
/*fn get_nth_line(content: &str, n: u32) -> (String, u64) { /*fn get_nth_line(content: &str, n: u32) -> (String, u64) {
use std::io::BufRead; use std::io::BufRead;
let mut cursor = std::io::Cursor::new(content); let mut cursor = std::io::Cursor::new(content);

View File

@ -40,6 +40,13 @@ impl Shadow {
pub fn get_password(&self) -> &str { pub fn get_password(&self) -> &str {
&self.password.password &self.password.password
} }
pub fn remove_in(&self, content: &str) -> String {
content
.split(&self.source)
.map(|x| x.trim())
.collect::<Vec<&str>>()
.join("\n")
}
} }
impl Display for Shadow { impl Display for Shadow {

View File

@ -98,33 +98,41 @@ impl UserDBWrite for UserDBLocal {
} }
} else { } else {
let opened = self.source_files.lock_all_get(); let opened = self.source_files.lock_all_get();
let (mut locked_p, locked_s, locked_g) = opened.expect("failed to lock files!"); let (mut locked_p, mut locked_s, locked_g) = opened.expect("failed to lock files!");
// read the files to strings // read the files to strings
let p = file_to_string(&locked_p.file)?; let p = file_to_string(&locked_p.file)?;
let _s = file_to_string(&locked_s.file)?; let s = file_to_string(&locked_s.file)?;
let _g = file_to_string(&locked_g.file)?; let _g = file_to_string(&locked_g.file)?;
{ {
if self.source_hashes.passwd.has_changed(&p) { let src = &self.source_hashes;
if src.passwd.has_changed(&p) | src.shadow.has_changed(&s) {
error!("The source files have changed. Deleting the user could corrupt the userdatabase. Aborting!"); error!("The source files have changed. Deleting the user could corrupt the userdatabase. Aborting!");
} else { } else {
// create the new content of passwd // create the new content of passwd
let modified = user.remove_in(&p); let modified_p = user.remove_in(&p);
// write the new content to the file. // write the new content to the file.
let ncont = locked_p.replace_contents(modified); let ncont = locked_p.replace_contents(modified_p);
match ncont { match ncont {
Ok(_) => { Ok(_) => {
// Remove the user from the memory database(HashMap)
let res = self.users.remove(username); let res = self.users.remove(username);
return Ok(res.unwrap()); return Ok(
res.expect("Failed to remove the user from the internal HashMap")
);
} }
Err(_) => { Err(e) => {
return Err("Error during write to the database. \ return Err(format!(
Please doublecheck as the userdatabase could be corrupted: {}" "Error during write to the database. \
.into()); Please doublecheck as the userdatabase could be corrupted: {}",
e,
)
.into());
} }
} }
} }
Err(format!("The user has been changed {}", username).into()) Err(format!("The userdatabase has been changed {}", username).into())
} }
} }
} }