Debian Codegraph @

src/system/interface.rs

raw symbols SCIP
1 use std::{ffi::CStr, fmt::Display, num::ParseIntError, str::FromStr};
2
3 /// Represents a group ID in the system.
4 ///
5 /// `GroupId` is transparent because the memory mapping should stay the same as the underlying
6 /// type, so we can safely cast as a pointer.
7 /// See the implementation in `system::mod::set_target_user`.
8 #[repr(transparent)]
9 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10 pub struct GroupId(libc::gid_t);
11
12 /// Represents a user ID in the system.
13 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14 pub struct UserId(libc::uid_t);
15
16 /// Represents a process ID in the system.
17 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
18 pub struct ProcessId(libc::pid_t);
19
20 /// Represents a device ID in the system.
21 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22 pub struct DeviceId(libc::dev_t);
23
24 impl GroupId {
25 pub fn new(id: libc::gid_t) -> Self {
26 Self(id)
27 }
28
29 pub fn inner(&self) -> libc::gid_t {
30 self.0
31 }
32 }
33
34 impl UserId {
35 pub fn new(id: libc::uid_t) -> Self {
36 Self(id)
37 }
38
39 pub fn inner(&self) -> libc::uid_t {
40 self.0
41 }
42
43 pub const ROOT: Self = Self(0);
44 }
45
46 impl ProcessId {
47 pub fn new(id: libc::pid_t) -> Self {
48 Self(id)
49 }
50
51 pub fn inner(&self) -> libc::pid_t {
52 self.0
53 }
54
55 pub fn is_valid(&self) -> bool {
56 self.0 != 0
57 }
58 }
59
60 impl DeviceId {
61 pub fn new(id: libc::dev_t) -> Self {
62 Self(id)
63 }
64
65 pub fn inner(&self) -> libc::dev_t {
66 self.0
67 }
68 }
69
70 impl Display for GroupId {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 write!(f, "{}", self.0)
73 }
74 }
75
76 impl Display for UserId {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 write!(f, "{}", self.0)
79 }
80 }
81
82 impl Display for ProcessId {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 write!(f, "{}", self.0)
85 }
86 }
87
88 impl Display for DeviceId {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 write!(f, "{}", self.0)
91 }
92 }
93
94 impl FromStr for GroupId {
95 type Err = ParseIntError;
96
97 fn from_str(s: &str) -> Result<Self, Self::Err> {
98 s.parse::<libc::gid_t>().map(GroupId::new)
99 }
100 }
101
102 impl FromStr for UserId {
103 type Err = ParseIntError;
104
105 fn from_str(s: &str) -> Result<Self, Self::Err> {
106 s.parse::<libc::uid_t>().map(UserId::new)
107 }
108 }
109
110 /// This trait/module is here to not make this crate independent (at the present time) in the idiosyncrasies of user representation details
111 /// (which we may decide over time), as well as to make explicit what functionality a user-representation must have; this
112 /// interface is not set in stone and "easy" to change.
113 pub trait UnixUser {
114 fn has_name(&self, _name: &str) -> bool;
115 fn has_uid(&self, _uid: UserId) -> bool;
116 fn is_root(&self) -> bool;
117 fn in_group_by_name(&self, _name: &CStr) -> bool;
118 fn in_group_by_gid(&self, _gid: GroupId) -> bool;
119
120 type Group: UnixGroup;
121 fn group(&self) -> Self::Group;
122 }
123
124 pub trait UnixGroup {
125 fn as_gid(&self) -> GroupId;
126 fn try_as_name(&self) -> Option<&str>;
127 }
128
129 impl UnixUser for super::User {
130 fn has_name(&self, name: &str) -> bool {
131 self.name == name
132 }
133 fn has_uid(&self, uid: UserId) -> bool {
134 self.uid == uid
135 }
136 fn is_root(&self) -> bool {
137 self.has_uid(UserId::ROOT)
138 }
139 fn in_group_by_name(&self, name_c: &CStr) -> bool {
140 if let Ok(Some(group)) = super::Group::from_name(name_c) {
141 self.in_group_by_gid(group.gid)
142 } else {
143 false
144 }
145 }
146 fn in_group_by_gid(&self, gid: GroupId) -> bool {
147 self.groups.contains(&gid)
148 }
149 type Group = super::Group;
150 fn group(&self) -> super::Group {
151 Self::Group {
152 gid: self.gid,
153 name: None,
154 }
155 }
156 }
157
158 impl UnixGroup for super::Group {
159 fn as_gid(&self) -> GroupId {
160 self.gid
161 }
162 fn try_as_name(&self) -> Option<&str> {
163 self.name.as_deref()
164 }
165 }
166
167 #[cfg(test)]
168 mod test {
169 use super::*;
170 use crate::system::{Group, ROOT_GROUP_NAME, User};
171 use std::ffi::CString;
172
173 fn test_user(user: impl UnixUser, name_c: &CStr) {
174 let name = name_c.to_str().unwrap();
175 assert!(user.has_name(name));
176 if user.has_name("root") {
177 assert!(user.in_group_by_name(CString::new(ROOT_GROUP_NAME).unwrap().as_c_str()));
178 assert!(user.is_root());
179 } else {
180 assert!(user.in_group_by_name(name_c));
181 assert!(!user.is_root());
182 }
183 assert_eq!(user.is_root(), name == "root");
184 }
185
186 fn test_group(group: impl UnixGroup, name: &str) {
187 assert_eq!(name == ROOT_GROUP_NAME, group.as_gid() == GroupId::new(0));
188 assert_eq!(group.try_as_name(), Some(name));
189 }
190
191 #[test]
192 fn test_unix_user() {
193 let user = |name| User::from_name(name).unwrap().unwrap();
194 test_user(user(c"root"), c"root");
195 test_user(user(c"daemon"), c"daemon");
196 }
197
198 #[test]
199 fn test_unix_group() {
200 let group = |name| Group::from_name(name).unwrap().unwrap();
201 let root_group_cstr = CString::new(ROOT_GROUP_NAME).unwrap();
202 test_group(group(root_group_cstr.as_c_str()), ROOT_GROUP_NAME);
203 test_group(group(c"daemon"), "daemon");
204 }
205 }