1use crate::Value;
2
3use crate::bridge;
4use crate::error::{Result, SecurityError};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[repr(transparent)]
8#[must_use]
9pub struct AuthorizationOptions(u32);
11
12impl AuthorizationOptions {
13 pub const DEFAULTS: Self = Self(0);
15 pub const INTERACTION_ALLOWED: Self = Self(1 << 0);
17 pub const EXTEND_RIGHTS: Self = Self(1 << 1);
19 pub const PARTIAL_RIGHTS: Self = Self(1 << 2);
21 pub const DESTROY_RIGHTS: Self = Self(1 << 3);
23 pub const PREAUTHORIZE: Self = Self(1 << 4);
25 pub const SKIP_INTERNAL_AUTH: Self = Self(1 << 9);
27 pub const NO_DATA: Self = Self(1 << 20);
29
30 pub const fn empty() -> Self {
31 Self(0)
32 }
33 pub const fn all() -> Self {
34 Self(0x0010_021f)
35 }
36 pub const fn bits(self) -> u32 {
37 self.0
38 }
39 pub const fn from_bits(bits: u32) -> Option<Self> {
40 if bits & !Self::all().0 == 0 {
41 Some(Self(bits))
42 } else {
43 None
44 }
45 }
46 pub const fn from_bits_retain(bits: u32) -> Self {
47 Self(bits)
48 }
49 pub const fn from_bits_truncate(bits: u32) -> Self {
50 Self(bits & Self::all().0)
51 }
52 pub const fn is_empty(self) -> bool {
53 self.0 == 0
54 }
55 pub const fn is_all(self) -> bool {
56 self.0 & Self::all().0 == Self::all().0
57 }
58 pub const fn contains(self, other: Self) -> bool {
59 self.0 & other.0 == other.0
60 }
61 pub const fn intersects(self, other: Self) -> bool {
62 self.0 & other.0 != 0
63 }
64 pub const fn union(self, other: Self) -> Self {
65 Self(self.0 | other.0)
66 }
67 pub const fn intersection(self, other: Self) -> Self {
68 Self(self.0 & other.0)
69 }
70 pub const fn difference(self, other: Self) -> Self {
71 Self(self.0 & !other.0)
72 }
73 pub const fn symmetric_difference(self, other: Self) -> Self {
74 Self(self.0 ^ other.0)
75 }
76 pub const fn complement(self) -> Self {
77 Self(!self.0 & Self::all().0)
78 }
79 pub fn insert(&mut self, other: Self) {
80 self.0 |= other.0;
81 }
82 pub fn remove(&mut self, other: Self) {
83 self.0 &= !other.0;
84 }
85 pub fn toggle(&mut self, other: Self) {
86 self.0 ^= other.0;
87 }
88 pub fn set(&mut self, other: Self, value: bool) {
89 if value {
90 self.0 |= other.0;
91 } else {
92 self.0 &= !other.0;
93 }
94 }
95}
96
97impl std::ops::BitOr for AuthorizationOptions {
98 type Output = Self;
99 fn bitor(self, rhs: Self) -> Self {
100 Self(self.0 | rhs.0)
101 }
102}
103impl std::ops::BitAnd for AuthorizationOptions {
104 type Output = Self;
105 fn bitand(self, rhs: Self) -> Self {
106 Self(self.0 & rhs.0)
107 }
108}
109impl std::ops::BitXor for AuthorizationOptions {
110 type Output = Self;
111 fn bitxor(self, rhs: Self) -> Self {
112 Self(self.0 ^ rhs.0)
113 }
114}
115impl std::ops::Sub for AuthorizationOptions {
116 type Output = Self;
117 fn sub(self, rhs: Self) -> Self {
118 Self(self.0 & !rhs.0)
119 }
120}
121impl std::ops::Not for AuthorizationOptions {
122 type Output = Self;
123 fn not(self) -> Self {
124 Self(!self.0 & Self::all().0)
125 }
126}
127impl std::ops::BitOrAssign for AuthorizationOptions {
128 fn bitor_assign(&mut self, rhs: Self) {
129 self.0 |= rhs.0;
130 }
131}
132impl std::ops::BitAndAssign for AuthorizationOptions {
133 fn bitand_assign(&mut self, rhs: Self) {
134 self.0 &= rhs.0;
135 }
136}
137impl std::ops::BitXorAssign for AuthorizationOptions {
138 fn bitxor_assign(&mut self, rhs: Self) {
139 self.0 ^= rhs.0;
140 }
141}
142impl std::ops::SubAssign for AuthorizationOptions {
143 fn sub_assign(&mut self, rhs: Self) {
144 self.0 &= !rhs.0;
145 }
146}
147
148#[derive(Debug)]
149pub struct Authorization {
151 handle: bridge::Handle,
152}
153
154impl Authorization {
155 #[cfg(feature = "async")]
156 pub(crate) fn as_ptr(&self) -> *mut std::ffi::c_void {
157 self.handle.as_ptr()
158 }
159
160 pub fn new() -> Result<Self> {
162 Self::with_options(AuthorizationOptions::DEFAULTS)
163 }
164
165 pub fn with_options(options: AuthorizationOptions) -> Result<Self> {
167 let mut status = 0;
168 let mut error = std::ptr::null_mut();
169 let raw = unsafe {
170 bridge::security_authorization_create(options.bits(), &raw mut status, &raw mut error)
171 };
172 bridge::required_handle("security_authorization_create", raw, status, error)
173 .map(|handle| Self { handle })
174 }
175
176 pub fn set_destroy_rights_on_drop(&mut self, destroy_rights: bool) {
177 unsafe {
178 bridge::security_authorization_set_destroy_rights(self.handle.as_ptr(), destroy_rights);
179 }
180 }
181
182 pub fn external_form(&self) -> Result<Vec<u8>> {
184 let mut status = 0;
185 let mut error = std::ptr::null_mut();
186 let raw = unsafe {
187 bridge::security_authorization_make_external_form(
188 self.handle.as_ptr(),
189 &raw mut status,
190 &raw mut error,
191 )
192 };
193 bridge::required_data(
194 "security_authorization_make_external_form",
195 raw,
196 status,
197 error,
198 )
199 }
200
201 pub fn from_external_form(external_form: &[u8]) -> Result<Self> {
203 let mut status = 0;
204 let mut error = std::ptr::null_mut();
205 let raw = unsafe {
206 bridge::security_authorization_create_from_external_form(
207 external_form.as_ptr().cast(),
208 bridge::len_to_isize(external_form.len())?,
209 &raw mut status,
210 &raw mut error,
211 )
212 };
213 bridge::required_handle(
214 "security_authorization_create_from_external_form",
215 raw,
216 status,
217 error,
218 )
219 .map(|handle| Self { handle })
220 }
221
222 pub fn copy_info(&self, tag: Option<&str>) -> Result<Value> {
224 let tag = tag.map(bridge::cstring).transpose()?;
225 let mut status = 0;
226 let mut error = std::ptr::null_mut();
227 let raw = unsafe {
228 bridge::security_authorization_copy_info(
229 self.handle.as_ptr(),
230 tag.as_ref()
231 .map_or(std::ptr::null(), bridge::BridgeString::as_ptr),
232 &raw mut status,
233 &raw mut error,
234 )
235 };
236 bridge::required_value("security_authorization_copy_info", raw, status, error)
237 }
238
239 pub fn copy_rights(&self, rights: &[&str], options: AuthorizationOptions) -> Result<Value> {
241 let rights_value = rights_value(rights)?;
242 let mut status = 0;
243 let mut error = std::ptr::null_mut();
244 let raw = unsafe {
245 bridge::security_authorization_copy_rights(
246 self.handle.as_ptr(),
247 rights_value.as_ptr(),
248 options.bits(),
249 &raw mut status,
250 &raw mut error,
251 )
252 };
253 bridge::required_value("security_authorization_copy_rights", raw, status, error)
254 }
255
256 pub fn copy_rights_async(
258 &self,
259 rights: &[&str],
260 options: AuthorizationOptions,
261 ) -> Result<Value> {
262 let rights_value = rights_value(rights)?;
263 let mut status = 0;
264 let mut error = std::ptr::null_mut();
265 let raw = unsafe {
266 bridge::security_authorization_copy_rights_async(
267 self.handle.as_ptr(),
268 rights_value.as_ptr(),
269 options.bits(),
270 &raw mut status,
271 &raw mut error,
272 )
273 };
274 bridge::required_value(
275 "security_authorization_copy_rights_async",
276 raw,
277 status,
278 error,
279 )
280 }
281}
282
283pub(crate) fn rights_value(rights: &[&str]) -> Result<bridge::InputValue> {
284 if rights.is_empty() {
285 return Err(SecurityError::InvalidArgument(
286 "at least one authorization right is required".to_owned(),
287 ));
288 }
289 if rights
290 .iter()
291 .any(|right| right.is_empty() || right.contains('\0'))
292 {
293 return Err(SecurityError::InvalidArgument(
294 "authorization right names must be non-empty and free of NUL bytes".to_owned(),
295 ));
296 }
297 bridge::InputValue::strings(rights)
298}
299
300#[cfg(test)]
301mod tests {
302 use super::*;
303
304 #[test]
305 fn rights_value_rejects_empty_and_nul_names() {
306 assert!(rights_value(&[]).is_err());
307 assert!(rights_value(&[""]).is_err());
308 assert!(rights_value(&["system.privilege.admin\0other"]).is_err());
309 let value = rights_value(&["system.preferences"]).unwrap();
310 assert_eq!(
311 unsafe { bridge::value_abi::decode(&*value.as_ptr(), 0) }.unwrap(),
312 Value::Array(vec![Value::String("system.preferences".to_owned())])
313 );
314 }
315
316 #[test]
317 fn defaults_are_empty() {
318 assert!(AuthorizationOptions::DEFAULTS.is_empty());
319 assert_eq!(AuthorizationOptions::DEFAULTS.bits(), 0);
320 }
321
322 #[test]
323 fn authorization_options_round_trip_through_bits() {
324 let options = AuthorizationOptions::INTERACTION_ALLOWED
325 | AuthorizationOptions::EXTEND_RIGHTS
326 | AuthorizationOptions::PREAUTHORIZE;
327
328 assert_eq!(
329 AuthorizationOptions::from_bits(options.bits()),
330 Some(options)
331 );
332 }
333
334 #[test]
335 fn authorization_options_keep_sparse_bits() {
336 let options = AuthorizationOptions::SKIP_INTERNAL_AUTH | AuthorizationOptions::NO_DATA;
337
338 assert!(options.contains(AuthorizationOptions::SKIP_INTERNAL_AUTH));
339 assert!(options.contains(AuthorizationOptions::NO_DATA));
340 assert_eq!(options.bits(), (1 << 9) | (1 << 20));
341 }
342}