1use std::path::Path;
4use std::num::ParseIntError;
5use libc::types::os::arch::c95::c_long;
6use libc::types::os::arch::c95::c_ulong;
7use entries::{Entries,Entry};
8
9
10#[derive(Debug, PartialEq, PartialOrd)]
12pub struct ShadowEntry {
13 pub name: String,
15
16 pub passwd: String,
18
19 pub last_change: c_long,
21
22 pub min: c_long,
24
25 pub max: c_long,
27
28 pub warning: c_long,
30
31 pub inactivity: c_long,
33
34 pub expires: c_long,
37
38 pub flag: c_ulong,
40}
41
42
43impl Entry for ShadowEntry {
44 fn from_line(line: &str) -> Result<ShadowEntry, ParseIntError> {
45
46 let parts: Vec<&str> = line.split(":").map(|part| part.trim()).collect();
47
48 Ok(ShadowEntry {
49 name: parts[0].to_string(),
50 passwd: parts[1].to_string(),
51 last_change: parts[2].parse().unwrap_or(-1),
52 min: parts[3].parse().unwrap_or(-1),
53 max: parts[4].parse().unwrap_or(-1),
54 warning: parts[5].parse().unwrap_or(-1),
55 inactivity: parts[6].parse().unwrap_or(-1),
56 expires: parts[7].parse().unwrap_or(-1),
57 flag: parts[8].parse().unwrap_or(0),
58 })
59 }
60}
61
62
63pub fn get_entry_by_name_from_path(path: &Path, name: &str) -> Option<ShadowEntry> {
66 Entries::<ShadowEntry>::new(path).find(|x| x.name == name)
67}
68
69
70pub fn get_entry_by_name(name: &str) -> Option<ShadowEntry> {
73 get_entry_by_name_from_path(&Path::new("/etc/shadow"), name)
74}
75
76
77pub fn get_all_entries_from_path(path: &Path) -> Vec<ShadowEntry> {
80 Entries::new(path).collect()
81}
82
83
84pub fn get_all_entries() -> Vec<ShadowEntry> {
87 get_all_entries_from_path(&Path::new("/etc/shadow"))
88}