Compare commits

..

No commits in common. "6389bb988d04d7a88a29f92964b8c885dad976f9" and "8dd2c53aa9460e121be86f8116afa2b8bf5167f9" have entirely different histories.

5 changed files with 50 additions and 94 deletions

View File

@ -24,13 +24,6 @@ pub struct 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) {
use std::io::BufRead;
let mut cursor = std::io::Cursor::new(content);

View File

@ -40,13 +40,6 @@ impl Shadow {
pub fn get_password(&self) -> &str {
&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 {

View File

@ -85,7 +85,6 @@ impl LockedFileGuard {
self.file
.write_all(&new_content.into_bytes())
.expect("Failed to write all users.");
let _ = self.file.write("\n".as_bytes());
Ok(())
}

View File

@ -1,37 +0,0 @@
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
pub struct SourceHash {
hashvalue: String,
}
impl SourceHash {
pub fn new(src: &str) -> Self {
Self {
hashvalue: src.to_owned(),
}
}
pub fn has_changed(&self, new: &str) -> bool {
trace!(
"Old and new lengths: {}, {}",
self.hashvalue.len(),
new.len()
);
!self.hashvalue.eq(new)
}
}
pub struct Hashes {
pub passwd: SourceHash,
pub shadow: SourceHash,
pub group: SourceHash,
}
impl Hashes {
pub fn new(passwd: &str, shadow: &str, group: &str) -> Self {
Self {
passwd: SourceHash::new(passwd),
shadow: SourceHash::new(shadow),
group: SourceHash::new(group),
}
}
}

View File

@ -8,7 +8,6 @@
#![allow(clippy::non_ascii_literal)]
pub mod files;
pub mod hashes;
use crate::api::GroupRead;
use crate::api::UserRead;
@ -20,7 +19,7 @@ use std::io::{BufReader, Read};
pub struct UserDBLocal {
source_files: files::Files,
source_hashes: hashes::Hashes, // to detect changes
source_hashes: Hashes, // to detect changes
pub users: HashMap<String, crate::User>,
pub groups: Vec<crate::Group>,
}
@ -45,7 +44,7 @@ impl UserDBLocal {
},
users,
groups,
source_hashes: hashes::Hashes::new(&passwd_content, &shadow_content, &group_content),
source_hashes: Hashes::new(&passwd_content, &shadow_content, &group_content),
};
res
}
@ -72,7 +71,7 @@ impl UserDBLocal {
source_files: files,
users,
groups: string_to(&my_group_lines),
source_hashes: hashes::Hashes::new(&my_passwd_lines, &my_shadow_lines, &my_group_lines),
source_hashes: Hashes::new(&my_passwd_lines, &my_shadow_lines, &my_group_lines),
})
}
}
@ -98,60 +97,33 @@ impl UserDBWrite for UserDBLocal {
}
} else {
let opened = self.source_files.lock_all_get();
let (mut locked_p, mut locked_s, locked_g) = opened.expect("failed to lock files!");
let (mut locked_p, locked_s, locked_g) = opened.expect("failed to lock files!");
// read the files to strings
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 src = &self.source_hashes;
if src.passwd.has_changed(&p) | src.shadow.has_changed(&s) {
if self.source_hashes.passwd.has_changed(&p) {
error!("The source files have changed. Deleting the user could corrupt the userdatabase. Aborting!");
} else {
// create the new content of passwd
let modified_p = user.remove_in(&p);
let modified = user.remove_in(&p);
// write the new content to the file.
let ncont = locked_p.replace_contents(modified_p);
let ncont = locked_p.replace_contents(modified);
match ncont {
Ok(_) => {
let shad = user.get_shadow();
match shad {
Some(shadow) => {
let modified_s = shadow.remove_in(&s);
let ncont = locked_s.replace_contents(modified_s);
match ncont {
Ok(_) => (),
Err(e) => {
return Err(format!(
"Error during write to the database. \
Please doublecheck as the shadowdatabase could be corrupted: {}",
e,
)
.into());
}
}
}
None => (),
}
// Remove the user from the memory database(HashMap)
let res = self.users.remove(username);
return Ok(
res.expect("Failed to remove the user from the internal HashMap")
);
return Ok(res.unwrap());
}
Err(e) => {
return Err(format!(
"Error during write to the database. \
Please doublecheck as the userdatabase could be corrupted: {}",
e,
)
Err(_) => {
return Err("Error during write to the database. \
Please doublecheck as the userdatabase could be corrupted: {}"
.into());
}
}
}
Err(format!("The userdatabase has been changed {}", username).into())
Err(format!("The user has been changed {}", username).into())
}
}
}
@ -282,6 +254,42 @@ impl UserDBValidation for UserDBLocal {
}
}
pub struct SourceHash {
hashvalue: String,
}
impl SourceHash {
pub fn new(src: &str) -> Self {
Self {
hashvalue: src.to_owned(),
}
}
pub fn has_changed(&self, new: &str) -> bool {
trace!(
"Old and new lengths: {}, {}",
self.hashvalue.len(),
new.len()
);
!self.hashvalue.eq(new)
}
}
pub struct Hashes {
pub passwd: SourceHash,
pub shadow: SourceHash,
pub group: SourceHash,
}
impl Hashes {
pub fn new(passwd: &str, shadow: &str, group: &str) -> Self {
Self {
passwd: SourceHash::new(passwd),
shadow: SourceHash::new(shadow),
group: SourceHash::new(group),
}
}
}
/// Parse a file to a string
fn file_to_string(file: &File) -> Result<String, crate::UserLibError> {
let mut reader = BufReader::new(file);