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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! This library parses `/etc/group` for information about groups on a system
//!
//! Groups are represented in `/etc/group` in the form of:
//!
//! `group_name:password:GID:user_list`

use std::fs;
use std::io::{BufReader,BufRead};

/// Structure used to wrap parsed information from groups database
#[derive(Debug,PartialEq,Clone)]
pub struct Group {
    /// Group's name
    pub name: String,
    /// Group's GID
    pub gid: u32,
    /// List of users for group
    pub user_list: Option<Vec<String>>,
}


// Parses a line from `/etc/group`
// Ignores the 'password' in `group_name:password:GID:user_list`
fn parse_line(line: String) -> Option<Group> {
    let mut split: Vec<&str> =  line.split(':').collect();
    if split.is_empty() { return None; };
    // Remove 'x' (password)
    split.remove(1);
    let mut users = Vec::new();
    if !split[2].is_empty() {
        let owned_users: Vec<&str> = split[2].split(',').collect();
        for user in owned_users {
            users.push(user.to_string());
        }
    }
    let group = Group {
        name: String::from(split[0]),
        gid: split[1].parse::<u32>().unwrap(),
        user_list:
            if !users.is_empty() { Some(users) }
            else { None }
    };
    return Some(group);
}

// Reads the `/etc/group` file and calls `parse_line` per each line
fn read_group() -> Vec<Group> {
    let mut groups = Vec::new();
    let file = match fs::File::open("/etc/group") {
        Ok(s) => { s },
        Err(e) => { panic!(e.to_string()) }
    };
    let file_buffer = BufReader::new(&file);
    for line in file_buffer.lines() {
        match line {
            Ok(l) => {
                groups.push(parse_line(l).unwrap());
            },
            Err(e) => { panic!(e.to_string()) }
        }

    }
    return groups;
}

/// Gets a group by its GID
///
/// #Example
///
/// ```
/// use groups;
///
/// let group = groups::get_group_by_gid(1).unwrap();
/// assert_eq!(group.gid, 1);
pub fn get_group_by_gid(gid: u32) -> Option<Group> {
    for group in read_group() {
        if group.gid == gid {
            return Some(group);
        }
    }
    return None;
}

/// Gets a group by its name
///
/// #Example
///
/// ```
/// use groups;
///
/// let group = groups::get_group_by_name("bin").unwrap();
/// assert_eq!(group.name, "bin".to_string());
pub fn get_group_by_name(name: &str) -> Option<Group> {
    for group in read_group() {
        if group.name == name {
            return Some(group);
        }
    }
    return None;
}

/// Returns all groups within `/etc/group`
pub fn get_groups() -> Vec<Group> {
    return read_group();
}

/// Searches all groups for any user given or their GID
///
/// #Example
///
/// ```
/// use groups;
///
/// let mut user = String::new();
/// for (key, value) in std::env::vars() {
///     if key == "USER" { user = value }
/// }
/// let list = groups::get_group_list(&user, None);
pub fn get_group_list(user_name: &str, gid: Option<u32>) -> Option<Vec<Group>> {
    let mut list = Vec::new();
    for group in read_group().iter() {
        if gid.is_some() {
            if group.gid == gid.unwrap() && group.name != user_name {
                list.push(group.clone());
            }
        }
        if group.user_list.is_some() {
            let users = group.user_list.clone().unwrap();
            for user in users {
                if user == user_name { list.push(group.clone()) }
            }
        }
    }
    if list.is_empty() { return None; }
    else { return Some(list); }
}

#[test]
fn test_parse_line() {
    let group = parse_line("hell:x:666:trump".to_string()).unwrap();

    assert_eq!(group.name, "hell".to_string());
    assert_eq!(group.gid, 666);
    assert_eq!(group.user_list, Some(vec!["trump".to_string()]));
}

#[test]
fn test_read_group() {
    let groups = read_group();
    for group in groups {
        println!("{:?}", group);
    }
}

#[test]
fn test_get_group_by_name() {
    match get_group_by_name("bin") {
        Some(group) => {
            assert_eq!(group.name, "bin".to_string());
        },
        None => { }
    };
}

#[test]
fn test_get_group_by_gid() {
    match get_group_by_gid(1) {
        Some(group) => {
            assert_eq!(group.gid, 1);
        },
        None => { }
    };
}

#[test]
fn test_get_group_list() {
    let mut user = String::new();
    for (key, value) in std::env::vars() {
        if key == "USER" { user = value }
    }
    match get_group_list(&user, None) {
        Some(list) => {
            for item in list { println!("{:?}", item); }
        },
        None => { }
    };
}