| 1 |
use crate::bititer::BitIter; |
| 2 |
use crate::failx::*; |
| 3 |
use crate::flag::Flag; |
| 4 |
use crate::perm::Perm; |
| 5 |
use crate::qualifier::Qualifier; |
| 6 |
use crate::sys::*; |
| 7 |
use crate::util::util_common; |
| 8 |
|
| 9 |
use scopeguard::defer; |
| 10 |
use std::ffi::{c_void, CString}; |
| 11 |
use std::io; |
| 12 |
use std::os::unix::ffi::OsStrExt; |
| 13 |
use std::path::Path; |
| 14 |
use uuid::Uuid; |
| 15 |
|
| 16 |
pub use util_common::{xacl_create_entry, xacl_foreach, xacl_free, xacl_init, xacl_is_empty}; |
| 17 |
|
| 18 |
use util_common::*; |
| 19 |
|
| 20 |
|
| 21 |
fn path_exists(path: &Path, symlink_only: bool) -> bool { |
| 22 |
if symlink_only { |
| 23 |
path.symlink_metadata().is_ok() |
| 24 |
} else { |
| 25 |
path.exists() |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
pub fn xacl_get_file(path: &Path, symlink_acl: bool, default_acl: bool) -> io::Result<acl_t> { |
| 34 |
if default_acl { |
| 35 |
return fail_custom("macOS does not support default ACL"); |
| 36 |
} |
| 37 |
|
| 38 |
let c_path = CString::new(path.as_os_str().as_bytes())?; |
| 39 |
let acl = if symlink_acl { |
| 40 |
unsafe { acl_get_link_np(c_path.as_ptr(), acl_type_t_ACL_TYPE_EXTENDED) } |
| 41 |
} else { |
| 42 |
unsafe { acl_get_file(c_path.as_ptr(), acl_type_t_ACL_TYPE_EXTENDED) } |
| 43 |
}; |
| 44 |
|
| 45 |
if acl.is_null() { |
| 46 |
let func = if symlink_acl { |
| 47 |
"acl_get_link_np" |
| 48 |
} else { |
| 49 |
"acl_get_file" |
| 50 |
}; |
| 51 |
let err = log_err("null", func, &c_path); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if err.raw_os_error() == Some(sg::ENOENT) && path_exists(path, symlink_acl) { |
| 56 |
return xacl_init(1); |
| 57 |
} |
| 58 |
|
| 59 |
return Err(err); |
| 60 |
} |
| 61 |
|
| 62 |
Ok(acl) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
fn xacl_set_file_symlink_alt(c_path: &CString, acl: acl_t) -> io::Result<()> { |
| 67 |
let fd = unsafe { open(c_path.as_ptr(), sg::O_SYMLINK) }; |
| 68 |
if fd < 0 { |
| 69 |
return fail_err(fd, "open", c_path); |
| 70 |
} |
| 71 |
defer! { unsafe{ close(fd) }; } |
| 72 |
|
| 73 |
let ret = unsafe { acl_set_fd(fd, acl) }; |
| 74 |
if ret != 0 { |
| 75 |
return fail_err(ret, "acl_set_fd", fd); |
| 76 |
} |
| 77 |
|
| 78 |
Ok(()) |
| 79 |
} |
| 80 |
|
| 81 |
pub fn xacl_set_file( |
| 82 |
path: &Path, |
| 83 |
acl: acl_t, |
| 84 |
symlink_acl: bool, |
| 85 |
default_acl: bool, |
| 86 |
) -> io::Result<()> { |
| 87 |
if default_acl { |
| 88 |
return fail_custom("macOS does not support default ACL"); |
| 89 |
} |
| 90 |
|
| 91 |
let c_path = CString::new(path.as_os_str().as_bytes())?; |
| 92 |
let ret = if symlink_acl { |
| 93 |
unsafe { acl_set_link_np(c_path.as_ptr(), acl_type_t_ACL_TYPE_EXTENDED, acl) } |
| 94 |
} else { |
| 95 |
unsafe { acl_set_file(c_path.as_ptr(), acl_type_t_ACL_TYPE_EXTENDED, acl) } |
| 96 |
}; |
| 97 |
|
| 98 |
if ret != 0 { |
| 99 |
let err = log_err(ret, "acl_set_link_np", &c_path); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if err.raw_os_error() == Some(sg::ENOTSUP) && symlink_acl { |
| 104 |
return xacl_set_file_symlink_alt(&c_path, acl); |
| 105 |
} |
| 106 |
|
| 107 |
return Err(err); |
| 108 |
} |
| 109 |
|
| 110 |
Ok(()) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
fn xacl_get_qualifier(entry: acl_entry_t) -> io::Result<Qualifier> { |
| 117 |
let uuid_ptr = unsafe { acl_get_qualifier(entry).cast::<Uuid>() }; |
| 118 |
if uuid_ptr.is_null() { |
| 119 |
return fail_err("null", "acl_get_qualifier", ()); |
| 120 |
} |
| 121 |
defer! { xacl_free(uuid_ptr) } |
| 122 |
|
| 123 |
let guid = unsafe { *uuid_ptr }; |
| 124 |
Qualifier::from_guid(guid) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
fn xacl_get_tag_qualifier(_acl: acl_t, entry: acl_entry_t) -> io::Result<(bool, Qualifier)> { |
| 129 |
let tag = xacl_get_tag_type(entry)?; |
| 130 |
|
| 131 |
let result = match tag { |
| 132 |
sg::ACL_EXTENDED_ALLOW => (true, xacl_get_qualifier(entry)?), |
| 133 |
sg::ACL_EXTENDED_DENY => (false, xacl_get_qualifier(entry)?), |
| 134 |
_ => (false, Qualifier::Unknown(format!("@tag {tag}"))), |
| 135 |
}; |
| 136 |
|
| 137 |
Ok(result) |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
fn xacl_get_flags_np(obj: *mut c_void) -> io::Result<Flag> { |
| 142 |
assert!(!obj.is_null()); |
| 143 |
|
| 144 |
let mut flagset: acl_flagset_t = std::ptr::null_mut(); |
| 145 |
let ret = unsafe { acl_get_flagset_np(obj, &mut flagset) }; |
| 146 |
if ret != 0 { |
| 147 |
return fail_err(ret, "acl_get_flagset_np", ()); |
| 148 |
} |
| 149 |
|
| 150 |
assert!(!flagset.is_null()); |
| 151 |
|
| 152 |
let mut flags = Flag::empty(); |
| 153 |
for flag in BitIter(Flag::all()) { |
| 154 |
let res = unsafe { acl_get_flag_np(flagset, flag.bits()) }; |
| 155 |
debug_assert!((0..=1).contains(&res)); |
| 156 |
if res == 1 { |
| 157 |
flags |= flag; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
Ok(flags) |
| 162 |
} |
| 163 |
|
| 164 |
fn xacl_get_flags(_acl: acl_t, entry: acl_entry_t) -> io::Result<Flag> { |
| 165 |
xacl_get_flags_np(entry.cast::<c_void>()) |
| 166 |
} |
| 167 |
|
| 168 |
pub fn xacl_get_entry(acl: acl_t, entry: acl_entry_t) -> io::Result<(bool, Qualifier, Perm, Flag)> { |
| 169 |
let (allow, qualifier) = xacl_get_tag_qualifier(acl, entry)?; |
| 170 |
let perms = xacl_get_perm(entry)?; |
| 171 |
let flags = xacl_get_flags(acl, entry)?; |
| 172 |
|
| 173 |
Ok((allow, qualifier, perms, flags)) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
pub fn xacl_set_qualifier(entry: acl_entry_t, qualifier: &Qualifier) -> io::Result<()> { |
| 180 |
|
| 181 |
let mut bytes = qualifier.guid()?.into_bytes(); |
| 182 |
|
| 183 |
let ret = unsafe { acl_set_qualifier(entry, bytes.as_mut_ptr().cast::<c_void>()) }; |
| 184 |
if ret != 0 { |
| 185 |
return fail_err(ret, "acl_set_qualifier", ()); |
| 186 |
} |
| 187 |
|
| 188 |
Ok(()) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
fn xacl_set_tag_qualifier( |
| 193 |
entry: acl_entry_t, |
| 194 |
allow: bool, |
| 195 |
qualifier: &Qualifier, |
| 196 |
) -> io::Result<()> { |
| 197 |
let tag = if let Qualifier::Unknown(_) = qualifier { |
| 198 |
debug_assert!(!allow); |
| 199 |
sg::ACL_EXTENDED_DENY |
| 200 |
} else if allow { |
| 201 |
sg::ACL_EXTENDED_ALLOW |
| 202 |
} else { |
| 203 |
sg::ACL_EXTENDED_DENY |
| 204 |
}; |
| 205 |
|
| 206 |
xacl_set_tag_type(entry, tag)?; |
| 207 |
xacl_set_qualifier(entry, qualifier)?; |
| 208 |
|
| 209 |
Ok(()) |
| 210 |
} |
| 211 |
|
| 212 |
fn xacl_set_flags_np(obj: *mut c_void, flags: Flag) -> io::Result<()> { |
| 213 |
assert!(!obj.is_null()); |
| 214 |
|
| 215 |
let mut flagset: acl_flagset_t = std::ptr::null_mut(); |
| 216 |
let ret_get = unsafe { acl_get_flagset_np(obj, &mut flagset) }; |
| 217 |
if ret_get != 0 { |
| 218 |
return fail_err(ret_get, "acl_get_flagset_np", ()); |
| 219 |
} |
| 220 |
|
| 221 |
assert!(!flagset.is_null()); |
| 222 |
|
| 223 |
let ret_clear = unsafe { acl_clear_flags_np(flagset) }; |
| 224 |
if ret_clear != 0 { |
| 225 |
return fail_err(ret_clear, "acl_clear_flags_np", ()); |
| 226 |
} |
| 227 |
|
| 228 |
for flag in BitIter(flags) { |
| 229 |
let ret = unsafe { acl_add_flag_np(flagset, flag.bits()) }; |
| 230 |
debug_assert!(ret == 0); |
| 231 |
} |
| 232 |
|
| 233 |
Ok(()) |
| 234 |
} |
| 235 |
|
| 236 |
fn xacl_set_flags(entry: acl_entry_t, flags: Flag) -> io::Result<()> { |
| 237 |
xacl_set_flags_np(entry.cast::<c_void>(), flags) |
| 238 |
} |
| 239 |
|
| 240 |
pub fn xacl_add_entry( |
| 241 |
acl: &mut acl_t, |
| 242 |
allow: bool, |
| 243 |
qualifier: &Qualifier, |
| 244 |
perms: Perm, |
| 245 |
flags: Flag, |
| 246 |
) -> io::Result<acl_entry_t> { |
| 247 |
let entry = xacl_create_entry(acl)?; |
| 248 |
xacl_set_tag_qualifier(entry, allow, qualifier)?; |
| 249 |
xacl_set_perm(entry, perms)?; |
| 250 |
xacl_set_flags(entry, flags)?; |
| 251 |
|
| 252 |
Ok(entry) |
| 253 |
} |
| 254 |
|
| 255 |
pub const fn xacl_is_posix(_acl: acl_t) -> bool { |
| 256 |
false |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
#[cfg(test)] |
| 262 |
mod util_macos_test { |
| 263 |
use super::*; |
| 264 |
|
| 265 |
#[test] |
| 266 |
fn test_acl_init() { |
| 267 |
use std::convert::TryInto; |
| 268 |
let max_entries: usize = ACL_MAX_ENTRIES.try_into().unwrap(); |
| 269 |
|
| 270 |
let acl = xacl_init(max_entries).ok().unwrap(); |
| 271 |
assert!(!acl.is_null()); |
| 272 |
xacl_free(acl); |
| 273 |
|
| 274 |
|
| 275 |
let err = xacl_init(max_entries + 1).unwrap_err(); |
| 276 |
assert_eq!(err.to_string(), "Too many ACL entries"); |
| 277 |
} |
| 278 |
|
| 279 |
#[test] |
| 280 |
fn test_acl_too_big() { |
| 281 |
let mut acl = xacl_init(3).ok().unwrap(); |
| 282 |
assert!(!acl.is_null()); |
| 283 |
|
| 284 |
for _ in 0..ACL_MAX_ENTRIES { |
| 285 |
xacl_create_entry(&mut acl).unwrap(); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
let err = xacl_create_entry(&mut acl).unwrap_err(); |
| 290 |
assert_eq!(err.raw_os_error(), Some(sg::ENOMEM)); |
| 291 |
|
| 292 |
xacl_free(acl); |
| 293 |
} |
| 294 |
|
| 295 |
#[test] |
| 296 |
fn test_acl_api_misuse() { |
| 297 |
let mut acl = xacl_init(1).unwrap(); |
| 298 |
let entry = xacl_create_entry(&mut acl).unwrap(); |
| 299 |
|
| 300 |
|
| 301 |
let err = xacl_set_tag_type(entry, 0).unwrap_err(); |
| 302 |
assert_eq!(err.raw_os_error(), Some(sg::EINVAL)); |
| 303 |
|
| 304 |
|
| 305 |
let err = xacl_set_qualifier(entry, &Qualifier::Guid(Uuid::nil())).unwrap_err(); |
| 306 |
assert_eq!(err.raw_os_error(), Some(sg::EINVAL)); |
| 307 |
|
| 308 |
let entry2 = xacl_create_entry(&mut acl).unwrap(); |
| 309 |
xacl_set_tag_type(entry2, 1).unwrap(); |
| 310 |
|
| 311 |
xacl_free(acl); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn test_uninitialized_entry() { |
| 316 |
let mut acl = xacl_init(1).unwrap(); |
| 317 |
let entry_p = xacl_create_entry(&mut acl).unwrap(); |
| 318 |
|
| 319 |
let (allow, qualifier) = xacl_get_tag_qualifier(acl, entry_p).unwrap(); |
| 320 |
assert_eq!(qualifier.name().unwrap(), "@tag 0"); |
| 321 |
assert!(!allow); |
| 322 |
|
| 323 |
xacl_free(acl); |
| 324 |
} |
| 325 |
} |