unix.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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: *const c_char, // login name
  16. pub pw_passwd: *const 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: *const c_char,
  21. pub pw_gecos: *const c_char, // full name
  22. pub pw_dir: *const c_char, // login dir
  23. pub pw_shell: *const c_char, // login shell
  24. pub pw_expire: time_t // password expiry time
  25. }
  26. pub struct c_group {
  27. pub gr_name: *const c_char, // group name
  28. pub gr_passwd: *const c_char, // password
  29. pub gr_gid: gid_t, // group id
  30. pub gr_mem: *const *const c_char, // names of users in the group
  31. }
  32. extern {
  33. pub fn getpwuid(uid: c_int) -> *const c_passwd;
  34. pub fn getgrgid(gid: uid_t) -> *const 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 infoptr = unsafe { c::getpwuid(uid as i32) };
  49. let info = unsafe { infoptr.to_option().unwrap() }; // the user has to have a name
  50. let username = unsafe { from_c_str(info.pw_name) };
  51. let mut user_names = HashMap::new();
  52. user_names.insert(uid as u32, Some(username.clone()));
  53. // Unix groups work like this: every group has a list of
  54. // users, referred to by their names. But, every user also has
  55. // a primary group, which isn't in this list. So handle this
  56. // case immediately after we look up the user's details.
  57. let mut groups = HashMap::new();
  58. groups.insert(info.pw_gid as u32, true);
  59. Unix {
  60. user_names: user_names,
  61. group_names: HashMap::new(),
  62. uid: uid as u32,
  63. username: username,
  64. groups: groups,
  65. }
  66. }
  67. pub fn get_user_name(&self, uid: u32) -> Option<String> {
  68. self.user_names.get(&uid).clone()
  69. }
  70. pub fn get_group_name(&self, gid: u32) -> Option<String> {
  71. self.group_names.get(&gid).clone()
  72. }
  73. pub fn is_group_member(&self, gid: u32) -> bool {
  74. *self.groups.get(&gid)
  75. }
  76. pub fn load_user(&mut self, uid: u32) {
  77. let pw = unsafe { c::getpwuid(uid as i32) };
  78. if pw.is_not_null() {
  79. let username = unsafe { Some(from_c_str(read(pw).pw_name)) };
  80. self.user_names.insert(uid, username);
  81. }
  82. else {
  83. self.user_names.insert(uid, None);
  84. }
  85. }
  86. fn group_membership(group: *const *const i8, uname: &String) -> bool {
  87. let mut i = 0;
  88. // The list of members is a pointer to a pointer of
  89. // characters, terminated by a null pointer. So the first call
  90. // to `to_option` will always succeed, as that memory is
  91. // guaranteed to be there (unless we go past the end of RAM).
  92. // The second call will return None if it's a null pointer.
  93. loop {
  94. match unsafe { group.offset(i).to_option().unwrap().to_option() } {
  95. Some(username) => {
  96. if unsafe { from_c_str(username) } == *uname {
  97. return true;
  98. }
  99. }
  100. None => {
  101. return false;
  102. }
  103. }
  104. i += 1;
  105. }
  106. }
  107. pub fn load_group(&mut self, gid: u32) {
  108. match unsafe { c::getgrgid(gid).to_option() } {
  109. None => {
  110. self.group_names.find_or_insert(gid, None);
  111. self.groups.find_or_insert(gid, false);
  112. },
  113. Some(r) => {
  114. let group_name = unsafe { Some(from_c_str(r.gr_name)) };
  115. self.groups.find_or_insert(gid, Unix::group_membership(r.gr_mem, &self.username));
  116. self.group_names.find_or_insert(gid, group_name);
  117. }
  118. }
  119. }
  120. }