1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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"))
}