2014-09-22 19:47:22 +00:00
|
|
|
use std::string::raw::from_buf;
|
2014-05-05 10:29:50 +00:00
|
|
|
use std::ptr::read;
|
2014-11-24 01:13:58 +00:00
|
|
|
use std::ptr;
|
2014-11-23 21:29:11 +00:00
|
|
|
use std::collections::HashMap;
|
2014-05-05 10:29:50 +00:00
|
|
|
|
|
|
|
mod c {
|
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
extern crate libc;
|
2014-06-26 22:26:27 +00:00
|
|
|
pub use self::libc::{
|
2014-05-05 10:29:50 +00:00
|
|
|
c_char,
|
|
|
|
c_int,
|
|
|
|
uid_t,
|
2014-06-26 22:26:27 +00:00
|
|
|
gid_t,
|
2014-05-05 10:29:50 +00:00
|
|
|
time_t
|
|
|
|
};
|
|
|
|
|
2014-09-22 19:12:07 +00:00
|
|
|
#[repr(C)]
|
2014-05-05 10:29:50 +00:00
|
|
|
pub struct c_passwd {
|
2014-06-29 21:28:52 +00:00
|
|
|
pub pw_name: *const c_char, // login name
|
|
|
|
pub pw_passwd: *const c_char,
|
2014-11-24 01:13:58 +00:00
|
|
|
pub pw_uid: c_int, // user ID
|
|
|
|
pub pw_gid: c_int, // group ID
|
2014-05-05 10:29:50 +00:00
|
|
|
pub pw_change: time_t,
|
2014-06-29 21:28:52 +00:00
|
|
|
pub pw_class: *const c_char,
|
|
|
|
pub pw_gecos: *const c_char, // full name
|
|
|
|
pub pw_dir: *const c_char, // login dir
|
|
|
|
pub pw_shell: *const c_char, // login shell
|
2014-11-24 01:13:58 +00:00
|
|
|
pub pw_expire: time_t, // password expiry time
|
2014-05-05 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 19:12:07 +00:00
|
|
|
#[repr(C)]
|
2014-11-24 01:13:58 +00:00
|
|
|
#[deriving(Show)]
|
2014-05-05 10:29:50 +00:00
|
|
|
pub struct c_group {
|
2014-11-24 01:13:58 +00:00
|
|
|
pub gr_name: *const c_char, // group name
|
|
|
|
pub gr_passwd: *const c_char, // password
|
|
|
|
pub gr_gid: gid_t, // group id
|
2014-06-29 21:28:52 +00:00
|
|
|
pub gr_mem: *const *const c_char, // names of users in the group
|
2014-05-05 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
2014-06-29 21:28:52 +00:00
|
|
|
pub fn getpwuid(uid: c_int) -> *const c_passwd;
|
|
|
|
pub fn getgrgid(gid: uid_t) -> *const c_group;
|
2014-05-27 18:05:15 +00:00
|
|
|
pub fn getuid() -> libc::c_int;
|
2014-05-05 10:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-22 14:41:20 +00:00
|
|
|
|
2014-06-21 16:50:37 +00:00
|
|
|
pub struct Unix {
|
2014-06-26 22:26:27 +00:00
|
|
|
user_names: HashMap<u32, Option<String>>, // mapping of user IDs to user names
|
|
|
|
group_names: HashMap<u32, Option<String>>, // mapping of groups IDs to group names
|
|
|
|
groups: HashMap<u32, bool>, // mapping of group IDs to whether the current user is a member
|
|
|
|
pub uid: u32, // current user's ID
|
|
|
|
pub username: String, // current user's name
|
2014-06-21 16:50:37 +00:00
|
|
|
}
|
2014-05-05 10:29:50 +00:00
|
|
|
|
2014-06-21 16:50:37 +00:00
|
|
|
impl Unix {
|
|
|
|
pub fn empty_cache() -> Unix {
|
2014-06-26 22:26:27 +00:00
|
|
|
let uid = unsafe { c::getuid() };
|
2014-07-21 21:05:04 +00:00
|
|
|
let infoptr = unsafe { c::getpwuid(uid as i32) };
|
2014-09-22 19:12:07 +00:00
|
|
|
let info = unsafe { infoptr.as_ref().unwrap() }; // the user has to have a name
|
2014-06-26 22:26:27 +00:00
|
|
|
|
2014-09-22 19:47:22 +00:00
|
|
|
let username = unsafe { from_buf(info.pw_name as *const u8) };
|
2014-06-26 22:26:27 +00:00
|
|
|
|
|
|
|
let mut user_names = HashMap::new();
|
|
|
|
user_names.insert(uid as u32, Some(username.clone()));
|
|
|
|
|
|
|
|
// Unix groups work like this: every group has a list of
|
|
|
|
// users, referred to by their names. But, every user also has
|
|
|
|
// a primary group, which isn't in this list. So handle this
|
|
|
|
// case immediately after we look up the user's details.
|
|
|
|
let mut groups = HashMap::new();
|
|
|
|
groups.insert(info.pw_gid as u32, true);
|
|
|
|
|
2014-06-21 16:50:37 +00:00
|
|
|
Unix {
|
2014-06-26 22:26:27 +00:00
|
|
|
user_names: user_names,
|
2014-06-21 16:50:37 +00:00
|
|
|
group_names: HashMap::new(),
|
2014-06-26 22:26:27 +00:00
|
|
|
uid: uid as u32,
|
|
|
|
username: username,
|
|
|
|
groups: groups,
|
2014-06-21 16:50:37 +00:00
|
|
|
}
|
2014-05-05 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 18:51:04 +00:00
|
|
|
pub fn get_user_name(&self, uid: u32) -> Option<String> {
|
2014-09-22 16:03:47 +00:00
|
|
|
self.user_names[uid].clone()
|
2014-06-27 18:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_group_name(&self, gid: u32) -> Option<String> {
|
2014-09-22 16:03:47 +00:00
|
|
|
self.group_names[gid].clone()
|
2014-06-27 18:51:04 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 22:26:27 +00:00
|
|
|
pub fn is_group_member(&self, gid: u32) -> bool {
|
2014-09-22 16:03:47 +00:00
|
|
|
self.groups[gid]
|
2014-06-26 22:26:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 18:51:04 +00:00
|
|
|
pub fn load_user(&mut self, uid: u32) {
|
|
|
|
let pw = unsafe { c::getpwuid(uid as i32) };
|
|
|
|
if pw.is_not_null() {
|
2014-09-22 19:47:22 +00:00
|
|
|
let username = unsafe { Some(from_buf(read(pw).pw_name as *const u8)) };
|
2014-06-27 18:51:04 +00:00
|
|
|
self.user_names.insert(uid, username);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.user_names.insert(uid, None);
|
|
|
|
}
|
2014-05-05 10:29:50 +00:00
|
|
|
}
|
2014-06-21 16:50:37 +00:00
|
|
|
|
2014-11-24 01:13:58 +00:00
|
|
|
fn group_membership(group: *const *const c::c_char, uname: &String) -> bool {
|
2014-06-26 22:26:27 +00:00
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
// The list of members is a pointer to a pointer of
|
|
|
|
// characters, terminated by a null pointer. So the first call
|
2014-09-22 19:12:07 +00:00
|
|
|
// to `as_ref` will always succeed, as that memory is
|
2014-06-26 22:26:27 +00:00
|
|
|
// guaranteed to be there (unless we go past the end of RAM).
|
|
|
|
// The second call will return None if it's a null pointer.
|
|
|
|
|
|
|
|
loop {
|
2014-11-24 01:13:58 +00:00
|
|
|
match unsafe { group.offset(i).as_ref() } {
|
|
|
|
Some(&username) => {
|
|
|
|
if username == ptr::null() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if unsafe { from_buf(username as *const u8) } == *uname {
|
2014-06-26 22:26:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-11-24 01:13:58 +00:00
|
|
|
else {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => return false,
|
2014-06-21 16:50:37 +00:00
|
|
|
}
|
2014-06-26 22:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 18:51:04 +00:00
|
|
|
pub fn load_group(&mut self, gid: u32) {
|
2014-09-22 19:12:07 +00:00
|
|
|
match unsafe { c::getgrgid(gid).as_ref() } {
|
2014-06-26 22:26:27 +00:00
|
|
|
None => {
|
2014-11-23 21:29:11 +00:00
|
|
|
self.group_names.insert(gid, None);
|
|
|
|
self.groups.insert(gid, false);
|
2014-06-27 18:51:04 +00:00
|
|
|
},
|
|
|
|
Some(r) => {
|
2014-09-22 19:47:22 +00:00
|
|
|
let group_name = unsafe { Some(from_buf(r.gr_name as *const u8)) };
|
2014-11-24 01:13:58 +00:00
|
|
|
if !self.groups.contains_key(&gid) {
|
|
|
|
self.groups.insert(gid, Unix::group_membership(r.gr_mem, &self.username));
|
|
|
|
}
|
2014-11-23 21:29:11 +00:00
|
|
|
self.group_names.insert(gid, group_name);
|
2014-06-21 16:50:37 +00:00
|
|
|
}
|
2014-06-26 22:26:27 +00:00
|
|
|
}
|
2014-06-27 18:51:04 +00:00
|
|
|
|
2014-05-05 10:29:50 +00:00
|
|
|
}
|
|
|
|
}
|