unix.rs 4.9 KB

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