linux_keyutils/
permissions.rs1use bitflags::bitflags;
4
5#[derive(Debug, Copy, Clone)]
18pub struct KeyPermissions(u32);
19
20#[derive(Debug, Copy, Clone)]
33pub struct KeyPermissionsBuilder(KeyPermissions);
34
35bitflags! {
36 #[repr(transparent)]
38 pub struct Permission: u8 {
39 const VIEW = 0x1;
41 const READ = 0x2;
43 const WRITE = 0x4;
45 const SEARCH = 0x8;
47 const LINK = 0x10;
49 const SETATTR = 0x20;
51 const ALL = 0x3f;
53 }
54}
55
56impl Default for KeyPermissions {
57 fn default() -> Self {
58 Self::new()
59 }
60}
61
62impl KeyPermissions {
63 pub fn new() -> Self {
65 Self(0)
66 }
67
68 pub fn from_u32(raw: u32) -> Self {
70 Self(raw)
71 }
72
73 pub fn bits(&self) -> u32 {
75 self.0
76 }
77
78 pub fn set_posessor_perms(&mut self, perm: Permission) {
80 self.0 &= !(0xFF << 24);
81 self.0 += (perm.bits() as u32) << 24;
82 }
83
84 pub fn set_user_perms(&mut self, perm: Permission) {
86 self.0 &= !(0xFF << 16);
87 self.0 += (perm.bits() as u32) << 16;
88 }
89
90 pub fn set_group_perms(&mut self, perm: Permission) {
92 self.0 &= !(0xFF << 8);
93 self.0 += (perm.bits() as u32) << 8;
94 }
95
96 pub fn set_world_perms(&mut self, perm: Permission) {
98 self.0 &= !0xFF;
99 self.0 += perm.bits() as u32;
100 }
101}
102
103impl KeyPermissionsBuilder {
104 pub fn builder() -> Self {
106 Self(KeyPermissions::default())
107 }
108
109 pub fn posessor(mut self, perm: Permission) -> Self {
111 self.0.set_posessor_perms(perm);
112 self
113 }
114
115 pub fn user(mut self, perm: Permission) -> Self {
117 self.0.set_user_perms(perm);
118 self
119 }
120
121 pub fn group(mut self, perm: Permission) -> Self {
123 self.0.set_group_perms(perm);
124 self
125 }
126
127 pub fn world(mut self, perm: Permission) -> Self {
129 self.0.set_world_perms(perm);
130 self
131 }
132
133 pub fn build(self) -> KeyPermissions {
135 self.0
136 }
137}
138
139#[test]
140fn test_posessor_perms() {
141 let mut perm = KeyPermissions::default();
142
143 perm.set_posessor_perms(Permission::ALL);
145 assert_eq!(perm.0, 0x3f000000);
146
147 perm.set_posessor_perms(Permission::SEARCH);
149 assert_eq!(perm.0, 0x08000000);
150
151 perm.set_posessor_perms(Permission::SEARCH | Permission::VIEW);
153 assert_eq!(perm.0, 0x09000000);
154
155 perm.set_posessor_perms(
157 Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
158 );
159 assert_eq!(perm.0, 0x27000000);
160}
161
162#[test]
163fn test_user_perms() {
164 let mut perm = KeyPermissions::default();
165
166 perm.set_user_perms(Permission::ALL);
168 assert_eq!(perm.0, 0x003f0000);
169
170 perm.set_user_perms(Permission::SEARCH);
172 assert_eq!(perm.0, 0x00080000);
173
174 perm.set_user_perms(Permission::SEARCH | Permission::VIEW);
176 assert_eq!(perm.0, 0x00090000);
177
178 perm.set_user_perms(
180 Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
181 );
182 assert_eq!(perm.0, 0x00270000);
183}
184
185#[test]
186fn test_group_perms() {
187 let mut perm = KeyPermissions::default();
188
189 perm.set_group_perms(Permission::ALL);
191 assert_eq!(perm.0, 0x00003f00);
192
193 perm.set_group_perms(Permission::SEARCH);
195 assert_eq!(perm.0, 0x00000800);
196
197 perm.set_group_perms(Permission::SEARCH | Permission::VIEW);
199 assert_eq!(perm.0, 0x00000900);
200
201 perm.set_group_perms(
203 Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
204 );
205 assert_eq!(perm.0, 0x00002700);
206}
207
208#[test]
209fn test_world_perms() {
210 let mut perm = KeyPermissions::default();
211
212 perm.set_world_perms(Permission::ALL);
214 assert_eq!(perm.0, 0x0000003f);
215
216 perm.set_world_perms(Permission::SEARCH);
218 assert_eq!(perm.0, 0x00000008);
219
220 perm.set_world_perms(Permission::SEARCH | Permission::VIEW);
222 assert_eq!(perm.0, 0x00000009);
223
224 perm.set_world_perms(
226 Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
227 );
228 assert_eq!(perm.0, 0x00000027);
229}
230
231#[test]
232fn test_combined_perms() {
233 let mut perm = KeyPermissions::default();
234
235 perm.set_posessor_perms(Permission::ALL);
237 assert_eq!(perm.0, 0x3f000000);
238
239 perm.set_user_perms(Permission::VIEW | Permission::READ | Permission::WRITE);
241 assert_eq!(perm.0, 0x3f070000);
242
243 perm.set_group_perms(Permission::SEARCH | Permission::VIEW);
245 assert_eq!(perm.0, 0x3f070900);
246
247 perm.set_world_perms(
249 Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
250 );
251 assert_eq!(perm.0, 0x3f070927);
252}