unix.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. use std::string::raw::from_buf;
  2. use std::ptr::read;
  3. use std::collections::HashMap;
  4. mod c {
  5. #![allow(non_camel_case_types)]
  6. extern crate libc;
  7. pub use self::libc::{
  8. c_char,
  9. c_int,
  10. uid_t,
  11. gid_t,
  12. time_t
  13. };
  14. #[repr(C)]
  15. pub struct c_passwd {
  16. pub pw_name: *const c_char, // login name
  17. pub pw_passwd: *const c_char,
  18. pub pw_uid: c_int, // user ID
  19. pub pw_gid: c_int, // group ID
  20. pub pw_change: time_t,
  21. pub pw_class: *const c_char,
  22. pub pw_gecos: *const c_char, // full name
  23. pub pw_dir: *const c_char, // login dir
  24. pub pw_shell: *const c_char, // login shell
  25. pub pw_expire: time_t // password expiry time
  26. }
  27. #[repr(C)]
  28. pub struct c_group {
  29. pub gr_name: *const c_char, // group name
  30. pub gr_passwd: *const c_char, // password
  31. pub gr_gid: gid_t, // group id
  32. pub gr_mem: *const *const c_char, // names of users in the group
  33. }
  34. extern {
  35. pub fn getpwuid(uid: c_int) -> *const c_passwd;
  36. pub fn getgrgid(gid: uid_t) -> *const c_group;
  37. pub fn getuid() -> libc::c_int;
  38. }
  39. }
  40. pub struct Unix {
  41. user_names: HashMap<u32, Option<String>>, // mapping of user IDs to user names
  42. group_names: HashMap<u32, Option<String>>, // mapping of groups IDs to group names
  43. groups: HashMap<u32, bool>, // mapping of group IDs to whether the current user is a member
  44. pub uid: u32, // current user's ID
  45. pub username: String, // current user's name
  46. }
  47. impl Unix {
  48. pub fn empty_cache() -> Unix {
  49. let uid = unsafe { c::getuid() };
  50. let infoptr = unsafe { c::getpwuid(uid as i32) };
  51. let info = unsafe { infoptr.as_ref().unwrap() }; // the user has to have a name
  52. let username = unsafe { from_buf(info.pw_name as *const u8) };
  53. let mut user_names = HashMap::new();
  54. user_names.insert(uid as u32, Some(username.clone()));
  55. // Unix groups work like this: every group has a list of
  56. // users, referred to by their names. But, every user also has
  57. // a primary group, which isn't in this list. So handle this
  58. // case immediately after we look up the user's details.
  59. let mut groups = HashMap::new();
  60. groups.insert(info.pw_gid as u32, true);
  61. Unix {
  62. user_names: user_names,
  63. group_names: HashMap::new(),
  64. uid: uid as u32,
  65. username: username,
  66. groups: groups,
  67. }
  68. }
  69. pub fn get_user_name(&self, uid: u32) -> Option<String> {
  70. self.user_names[uid].clone()
  71. }
  72. pub fn get_group_name(&self, gid: u32) -> Option<String> {
  73. self.group_names[gid].clone()
  74. }
  75. pub fn is_group_member(&self, gid: u32) -> bool {
  76. self.groups[gid]
  77. }
  78. pub fn load_user(&mut self, uid: u32) {
  79. let pw = unsafe { c::getpwuid(uid as i32) };
  80. if pw.is_not_null() {
  81. let username = unsafe { Some(from_buf(read(pw).pw_name as *const u8)) };
  82. self.user_names.insert(uid, username);
  83. }
  84. else {
  85. self.user_names.insert(uid, None);
  86. }
  87. }
  88. fn group_membership(group: *const *const i8, uname: &String) -> bool {
  89. let mut i = 0;
  90. // The list of members is a pointer to a pointer of
  91. // characters, terminated by a null pointer. So the first call
  92. // to `as_ref` will always succeed, as that memory is
  93. // guaranteed to be there (unless we go past the end of RAM).
  94. // The second call will return None if it's a null pointer.
  95. loop {
  96. match unsafe { group.offset(i).as_ref().unwrap().as_ref() } {
  97. Some(username) => {
  98. if unsafe { from_buf(*username as *const u8) } == *uname {
  99. return true;
  100. }
  101. }
  102. None => {
  103. return false;
  104. }
  105. }
  106. i += 1;
  107. }
  108. }
  109. pub fn load_group(&mut self, gid: u32) {
  110. match unsafe { c::getgrgid(gid).as_ref() } {
  111. None => {
  112. self.group_names.insert(gid, None);
  113. self.groups.insert(gid, false);
  114. },
  115. Some(r) => {
  116. let group_name = unsafe { Some(from_buf(r.gr_name as *const u8)) };
  117. self.groups.insert(gid, Unix::group_membership(r.gr_mem, &self.username));
  118. self.group_names.insert(gid, group_name);
  119. }
  120. }
  121. }
  122. }