unix.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 get_user_name(&self, uid: u32) -> Option<String> {
  67. self.user_names.get(&uid).clone()
  68. }
  69. pub fn get_group_name(&self, gid: u32) -> Option<String> {
  70. self.group_names.get(&gid).clone()
  71. }
  72. pub fn is_group_member(&self, gid: u32) -> bool {
  73. *self.groups.get(&gid)
  74. }
  75. pub fn load_user(&mut self, uid: u32) {
  76. let pw = unsafe { c::getpwuid(uid as i32) };
  77. if pw.is_not_null() {
  78. let username = unsafe { Some(from_c_str(read(pw).pw_name)) };
  79. self.user_names.insert(uid, username);
  80. }
  81. else {
  82. self.user_names.insert(uid, None);
  83. }
  84. }
  85. fn group_membership(group: **i8, uname: &String) -> bool {
  86. let mut i = 0;
  87. // The list of members is a pointer to a pointer of
  88. // characters, terminated by a null pointer. So the first call
  89. // to `to_option` will always succeed, as that memory is
  90. // guaranteed to be there (unless we go past the end of RAM).
  91. // The second call will return None if it's a null pointer.
  92. loop {
  93. match unsafe { group.offset(i).to_option().unwrap().to_option() } {
  94. Some(username) => {
  95. if unsafe { from_c_str(username) } == *uname {
  96. return true;
  97. }
  98. }
  99. None => {
  100. return false;
  101. }
  102. }
  103. i += 1;
  104. }
  105. }
  106. pub fn load_group(&mut self, gid: u32) {
  107. match unsafe { c::getgrgid(gid).to_option() } {
  108. None => {
  109. self.group_names.find_or_insert(gid, None);
  110. self.groups.find_or_insert(gid, false);
  111. },
  112. Some(r) => {
  113. let group_name = unsafe { Some(from_c_str(r.gr_name)) };
  114. self.groups.find_or_insert(gid, Unix::group_membership(r.gr_mem, &self.username));
  115. self.group_names.find_or_insert(gid, group_name);
  116. }
  117. }
  118. }
  119. }