use std::path::Path;
use std::num::ParseIntError;
use libc::types::os::arch::c95::c_long;
use libc::types::os::arch::c95::c_ulong;
use entries::{Entries,Entry};
#[derive(Debug, PartialEq, PartialOrd)]
pub struct ShadowEntry {
pub name: String,
pub passwd: String,
pub last_change: c_long,
pub min: c_long,
pub max: c_long,
pub warning: c_long,
pub inactivity: c_long,
pub expires: c_long,
pub flag: c_ulong,
}
impl Entry for ShadowEntry {
fn from_line(line: &str) -> Result<ShadowEntry, ParseIntError> {
let parts: Vec<&str> = line.split(":").map(|part| part.trim()).collect();
Ok(ShadowEntry {
name: parts[0].to_string(),
passwd: parts[1].to_string(),
last_change: parts[2].parse().unwrap_or(-1),
min: parts[3].parse().unwrap_or(-1),
max: parts[4].parse().unwrap_or(-1),
warning: parts[5].parse().unwrap_or(-1),
inactivity: parts[6].parse().unwrap_or(-1),
expires: parts[7].parse().unwrap_or(-1),
flag: parts[8].parse().unwrap_or(0),
})
}
}
pub fn get_entry_by_name_from_path(path: &Path, name: &str) -> Option<ShadowEntry> {
Entries::<ShadowEntry>::new(path).find(|x| x.name == name)
}
pub fn get_entry_by_name(name: &str) -> Option<ShadowEntry> {
get_entry_by_name_from_path(&Path::new("/etc/shadow"), name)
}
pub fn get_all_entries_from_path(path: &Path) -> Vec<ShadowEntry> {
Entries::new(path).collect()
}
pub fn get_all_entries() -> Vec<ShadowEntry> {
get_all_entries_from_path(&Path::new("/etc/shadow"))
}