pgs_files/
shadow.rs

1//! Fuctions and Structs for dealing with /etc/shadow
2
3use 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/// An entry from /etc/shadow
11#[derive(Debug, PartialEq, PartialOrd)]
12pub struct ShadowEntry {
13    /// Login name
14    pub name: String,
15
16    /// Encrypted password
17    pub passwd: String,
18
19    /// Date of last change (measured in days since 1970-01-01 00:00:00 +0000 (UTC))
20    pub last_change: c_long,
21
22    /// Min number of days between changes
23    pub min: c_long,
24
25    /// Max number of days between changes
26    pub max: c_long,
27
28    /// Number of days before password expires to warn user to change it
29    pub warning: c_long,
30
31    /// Number of days after password expires until account is disabled
32    pub inactivity: c_long,
33
34    /// Date when account expires (measured
35    /// in days since 1970-01-01 00:00:00 +0000 (UTC))
36    pub expires: c_long,
37
38    /// Reserved
39    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
63/// Return a [`ShadowEntry`](struct.ShadowEntry.html)
64/// for a given `name` and `&Path`
65pub 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
70/// Return a [`ShadowEntry`](struct.ShadowEntry.html)
71/// for a given `name` from `/etc/shadow`
72pub fn get_entry_by_name(name: &str) -> Option<ShadowEntry> {
73    get_entry_by_name_from_path(&Path::new("/etc/shadow"), name)
74}
75
76
77/// Return a `Vec<`[`ShadowEntry`](struct.ShadowEntry.html)`>` containing all
78/// [`ShadowEntry`](struct.ShadowEntry.html)'s for a given `&Path`
79pub fn get_all_entries_from_path(path: &Path) -> Vec<ShadowEntry> {
80    Entries::new(path).collect()
81}
82
83
84/// Return a `Vec<`[`ShadowEntry`](struct.ShadowEntry.html)`>` containing all
85/// [`ShadowEntry`](struct.ShadowEntry.html)'s from `/etc/shadow`
86pub fn get_all_entries() -> Vec<ShadowEntry> {
87    get_all_entries_from_path(&Path::new("/etc/shadow"))
88}