unix.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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(&mut self, gid: u32) -> bool {
  67. // Groups that no longer exist have no members. If this method
  68. // is called before a group with the given gid has been read,
  69. // it doesn't exist, so insert false.
  70. *self.groups.find_or_insert(gid, false)
  71. }
  72. pub fn get_user_name(&mut self, uid: u32) -> Option<String> {
  73. self.user_names.find_or_insert_with(uid, |&u| {
  74. let pw = unsafe { c::getpwuid(u as i32) };
  75. if pw.is_not_null() {
  76. return unsafe { Some(from_c_str(read(pw).pw_name)) };
  77. }
  78. else {
  79. return None;
  80. }
  81. }).clone()
  82. }
  83. fn group_membership(group: **i8, uname: &String) -> bool {
  84. let mut i = 0;
  85. // The list of members is a pointer to a pointer of
  86. // characters, terminated by a null pointer. So the first call
  87. // to `to_option` will always succeed, as that memory is
  88. // guaranteed to be there (unless we go past the end of RAM).
  89. // The second call will return None if it's a null pointer.
  90. loop {
  91. match unsafe { group.offset(i).to_option().unwrap().to_option() } {
  92. Some(username) => {
  93. if unsafe { from_c_str(username) } == *uname {
  94. return true;
  95. }
  96. }
  97. None => {
  98. return false;
  99. }
  100. }
  101. i += 1;
  102. }
  103. }
  104. pub fn get_group_name(&mut self, gid: u32) -> Option<String> {
  105. match self.group_names.find_copy(&gid) {
  106. Some(name) => name,
  107. None => {
  108. match unsafe { c::getgrgid(gid).to_option() } {
  109. None => {
  110. self.group_names.insert(gid, None);
  111. return None;
  112. },
  113. Some(r) => {
  114. let group_name = unsafe { Some(from_c_str(r.gr_name)) };
  115. self.group_names.insert(gid, group_name.clone());
  116. // Calculate whether we are a member of the
  117. // group. Now's as good a time as any as we've
  118. // just retrieved the group details.
  119. if !self.groups.contains_key(&gid) {
  120. self.groups.insert(gid, Unix::group_membership(r.gr_mem, &self.username));
  121. }
  122. return group_name;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }