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
/**
 * rust-daemon
 * User module for access control use
 *
 * https://github.com/ryankurte/rust-daemon
 * Copyright 2018 Ryan Kurte
 */
use std::io::Error as IoError;
use std::io::ErrorKind as IoErrorKind;

use std::ffi::CString;

use users::{get_group_by_gid, get_user_by_uid};

use libc::{c_int, getgrouplist, gid_t, uid_t};

/// User object for asserting permissions
#[derive(Debug, PartialEq, Clone)]
pub struct User {
    pub id: uid_t,
    pub group_id: uid_t,
    pub name: String,
    pub groups: Vec<String>,
}

impl User {
    /// Create a user object from a given UID
    pub fn from_uid(uid: uid_t) -> Result<User, IoError> {
        let user = get_user_by_uid(uid).unwrap();
        let groups = get_groups(user.name(), user.primary_group_id() as gid_t).unwrap();

        Ok(User {
            id: uid,
            name: user.name().to_string(),
            group_id: user.primary_group_id(),
            groups: groups,
        })
    }
}

/// Fetch groups for a given username and primary group id
fn get_groups(username: &str, gid: gid_t) -> Result<Vec<String>, IoError> {
    unsafe {
        #[cfg(all(unix, target_os="macos"))]
        let mut groups: Vec<i32> = vec![0; 1024];
        #[cfg(all(unix, not(target_os="macos")))]
        let mut groups: Vec<gid_t> = vec![0; 1024];

        let name = CString::new(username).unwrap();
        let mut count = groups.len() as c_int;

        #[cfg(all(unix, target_os="macos"))]
        let res = getgrouplist(name.as_ptr(), gid as i32, groups.as_mut_ptr(), &mut count);
        #[cfg(all(unix, not(target_os="macos")))]
        let res = getgrouplist(name.as_ptr(), gid, groups.as_mut_ptr(), &mut count);
        if res < 0 {
            return Err(IoError::new(
                IoErrorKind::Other,
                format!("libc::getgrouplist error: {}", res),
            ));
        }

        let mut names: Vec<String> = Vec::new();
        for i in 0..count {
            let g = get_group_by_gid(groups[i as usize] as u32);
            match g {
                Some(g) => names.push(g.name().to_string()),
                None => (),
            }
        }

        Ok(names)
    }
}

#[cfg(test)]
mod tests {
    use user::User;

    use users::get_current_uid;

    #[test]
    fn test_get_groups() {
        let uid = get_current_uid();
        let user = User::from_uid(uid).unwrap();
        println!("Groups: {:?}", user.groups);
        assert!(user.groups.len() > 0);
    }
}