| 1 |
#[cfg(test)] |
| 2 |
mod tests; |
| 3 |
|
| 4 |
use core::ffi::CStr; |
| 5 |
use core::marker::PhantomData; |
| 6 |
use core::{iter, ptr}; |
| 7 |
use std::io; |
| 8 |
use std::os::raw::{c_int, c_uint}; |
| 9 |
use std::path::Path; |
| 10 |
|
| 11 |
use crate::errors::{Error, Result}; |
| 12 |
use crate::label::Labeler; |
| 13 |
use crate::utils::{ |
| 14 |
OptionalNativeFunctions, c_str_ptr_to_path, c_str_ptr_to_str, os_str_to_c_string, |
| 15 |
ret_val_to_result, |
| 16 |
}; |
| 17 |
|
| 18 |
bitflags! { |
| 19 |
/// Flags controlling relabeling operations. |
| 20 |
pub struct RestoreFlags: c_uint { |
| 21 |
/// Force the checking of labels even if the stored SHA1 digest matches |
| 22 |
/// the specfile entries SHA1 digest. |
| 23 |
/// |
| 24 |
/// The specfile entries digest will be written to the `security.sehash` |
| 25 |
/// extended attribute once relabeling has been completed successfully |
| 26 |
/// provided the [`NO_CHANGE`] flag has not been set. |
| 27 |
/// |
| 28 |
/// [`NO_CHANGE`]: Self::NO_CHANGE |
| 29 |
#[allow(clippy::as_conversions)] |
| 30 |
const IGNORE_DIGEST = selinux_sys::SELINUX_RESTORECON_IGNORE_DIGEST as c_uint; |
| 31 |
|
| 32 |
/// Don't change any file labels (passive check) or update the digest in |
| 33 |
/// the `security.sehash` extended attribute. |
| 34 |
#[allow(clippy::as_conversions)] |
| 35 |
const NO_CHANGE = selinux_sys::SELINUX_RESTORECON_NOCHANGE as c_uint; |
| 36 |
|
| 37 |
/// If set, reset the files label to match the default spec file context. |
| 38 |
/// If not set only reset the files "type" component of the context |
| 39 |
/// to match the default spec file context. |
| 40 |
#[allow(clippy::as_conversions)] |
| 41 |
const SET_SPEC_FILE_CTX = |
| 42 |
selinux_sys::SELINUX_RESTORECON_SET_SPECFILE_CTX as c_uint; |
| 43 |
|
| 44 |
/// Change file and directory labels recursively (descend directories) |
| 45 |
/// and if successful write an SHA1 digest of the spec file entries |
| 46 |
/// to an extended attribute. |
| 47 |
#[allow(clippy::as_conversions)] |
| 48 |
const RECURSE = selinux_sys::SELINUX_RESTORECON_RECURSE as c_uint; |
| 49 |
|
| 50 |
/// Log file label changes. |
| 51 |
/// |
| 52 |
/// Note that if [`VERBOSE`] and [`PROGRESS`] flags are set, |
| 53 |
/// then [`PROGRESS`] will take precedence. |
| 54 |
/// |
| 55 |
/// [`VERBOSE`]: Self::VERBOSE |
| 56 |
/// [`PROGRESS`]: Self::PROGRESS |
| 57 |
#[allow(clippy::as_conversions)] |
| 58 |
const VERBOSE = selinux_sys::SELINUX_RESTORECON_VERBOSE as c_uint; |
| 59 |
|
| 60 |
/// Show progress by outputting the number of files in 1k blocks |
| 61 |
/// processed to stdout. |
| 62 |
/// |
| 63 |
/// If the [`MASS_RELABEL`] flag is also set then the approximate |
| 64 |
/// percentage complete will be shown. |
| 65 |
/// |
| 66 |
/// [`MASS_RELABEL`]: Self::MASS_RELABEL |
| 67 |
#[allow(clippy::as_conversions)] |
| 68 |
const PROGRESS = selinux_sys::SELINUX_RESTORECON_PROGRESS as c_uint; |
| 69 |
|
| 70 |
/// Convert passed-in path name to the canonical path name using |
| 71 |
/// `realpath()`. |
| 72 |
#[allow(clippy::as_conversions)] |
| 73 |
const REAL_PATH = selinux_sys::SELINUX_RESTORECON_REALPATH as c_uint; |
| 74 |
|
| 75 |
/// Prevent descending into directories that have a different device |
| 76 |
/// number than the path name entry from which the descent began. |
| 77 |
#[allow(clippy::as_conversions)] |
| 78 |
const XDEV = selinux_sys::SELINUX_RESTORECON_XDEV as c_uint; |
| 79 |
|
| 80 |
/// Attempt to add an association between an inode and a specification. |
| 81 |
/// If there is already an association for the inode and it conflicts |
| 82 |
/// with the specification, then use the last matching specification. |
| 83 |
#[allow(clippy::as_conversions)] |
| 84 |
const ADD_ASSOC = selinux_sys::SELINUX_RESTORECON_ADD_ASSOC as c_uint; |
| 85 |
|
| 86 |
/// Abort on errors during the file tree walk. |
| 87 |
#[allow(clippy::as_conversions)] |
| 88 |
const ABORT_ON_ERROR = selinux_sys::SELINUX_RESTORECON_ABORT_ON_ERROR as c_uint; |
| 89 |
|
| 90 |
/// Log any label changes to `syslog()`. |
| 91 |
#[allow(clippy::as_conversions)] |
| 92 |
const SYS_LOG_CHANGES = selinux_sys::SELINUX_RESTORECON_SYSLOG_CHANGES as c_uint; |
| 93 |
|
| 94 |
/// Log what spec file context matched each file. |
| 95 |
#[allow(clippy::as_conversions)] |
| 96 |
const LOG_MATCHES = selinux_sys::SELINUX_RESTORECON_LOG_MATCHES as c_uint; |
| 97 |
|
| 98 |
/// Ignore files that do not exist. |
| 99 |
#[allow(clippy::as_conversions)] |
| 100 |
const IGNORE_NO_ENTRY = selinux_sys::SELINUX_RESTORECON_IGNORE_NOENTRY as c_uint; |
| 101 |
|
| 102 |
/// Do not read `/proc/mounts` to obtain a list of non-seclabel mounts |
| 103 |
/// to be excluded from relabeling checks. |
| 104 |
/// |
| 105 |
/// Setting [`IGNORE_MOUNTS`] is useful where there is a non-seclabel fs |
| 106 |
/// mounted with a seclabel fs mounted on a directory below this. |
| 107 |
/// |
| 108 |
/// [`IGNORE_MOUNTS`]: Self::IGNORE_MOUNTS |
| 109 |
#[allow(clippy::as_conversions)] |
| 110 |
const IGNORE_MOUNTS = selinux_sys::SELINUX_RESTORECON_IGNORE_MOUNTS as c_uint; |
| 111 |
|
| 112 |
/// Generally set when relabeling the entire OS, that will then show |
| 113 |
/// the approximate percentage complete. |
| 114 |
/// |
| 115 |
/// The [`PROGRESS`] flag must also be set. |
| 116 |
/// |
| 117 |
/// [`PROGRESS`]: Self::PROGRESS |
| 118 |
#[allow(clippy::as_conversions)] |
| 119 |
const MASS_RELABEL = selinux_sys::SELINUX_RESTORECON_MASS_RELABEL as c_uint; |
| 120 |
|
| 121 |
// The rest of the constants were defined after version 2.8, so selinux_sys might not |
| 122 |
// export them. We therefore define them manually. |
| 123 |
|
| 124 |
/// Do not check or update any extended attribute security.sehash entries. |
| 125 |
/// |
| 126 |
/// This flag is supported only by `libselinux` version `3.0` or later. |
| 127 |
const SKIP_DIGEST = 0x8000; |
| 128 |
|
| 129 |
/// Treat conflicting specifications, such as where two hardlinks for |
| 130 |
/// the same inode have different contexts, as errors. |
| 131 |
/// |
| 132 |
/// This flag is supported only by `libselinux` version `3.1` or later. |
| 133 |
const CONFLICT_ERROR = 0x1_0000; |
| 134 |
|
| 135 |
/// Count, but otherwise ignore, errors during the file tree walk. |
| 136 |
/// |
| 137 |
/// This flag requires `libselinux` version `3.4` or later. |
| 138 |
const COUNT_ERRORS = 0x2_0000; |
| 139 |
|
| 140 |
/// In addition to the type component also change the user and |
| 141 |
/// role component of security contexts. |
| 142 |
/// |
| 143 |
/// This flag requires `libselinux` version `3.9` or later. |
| 144 |
const SET_USER_ROLE = 0x4_0000; |
| 145 |
|
| 146 |
/// Count the number of relabeled files (or would be relabeled if |
| 147 |
/// the [`NO_CHANGE`] flag was not set). |
| 148 |
/// |
| 149 |
/// This flag requires `libselinux` version `3.10` or later. |
| 150 |
/// |
| 151 |
/// [`NO_CHANGE`]: Self::NO_CHANGE |
| 152 |
const COUNT_RELABELED = 0x8_0000; |
| 153 |
|
| 154 |
/// Do not relabel non-directories that have more than one hard link. |
| 155 |
/// |
| 156 |
/// This flag requires `libselinux` version `3.11` or later. |
| 157 |
const SKIP_MULTILINK = 0x10_0000; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
bitflags! { |
| 162 |
/// Flags of [`ContextRestore::manage_security_sehash_xattr_entries`]. |
| 163 |
pub struct XAttrFlags: c_uint { |
| 164 |
/// Recursively descend directories. |
| 165 |
#[allow(clippy::as_conversions)] |
| 166 |
const RECURSE = selinux_sys::SELINUX_RESTORECON_XATTR_RECURSE as c_uint; |
| 167 |
|
| 168 |
/// Delete non-matching digests from each directory in path name. |
| 169 |
#[allow(clippy::as_conversions)] |
| 170 |
const DELETE_NON_MATCH_DIGESTS = selinux_sys::SELINUX_RESTORECON_XATTR_DELETE_NONMATCH_DIGESTS as c_uint; |
| 171 |
|
| 172 |
/// Delete all digests from each directory in path name. |
| 173 |
#[allow(clippy::as_conversions)] |
| 174 |
const DELETE_ALL_DIGESTS = selinux_sys::SELINUX_RESTORECON_XATTR_DELETE_ALL_DIGESTS as c_uint; |
| 175 |
|
| 176 |
/// Don't read `/proc/mounts` to obtain a list of non-seclabel mounts |
| 177 |
/// to be excluded from the search. |
| 178 |
/// |
| 179 |
/// Setting [`IGNORE_MOUNTS`] is useful where there is a non-seclabel fs |
| 180 |
/// mounted with a seclabel fs mounted on a directory below this. |
| 181 |
/// |
| 182 |
/// [`IGNORE_MOUNTS`]: Self::IGNORE_MOUNTS |
| 183 |
#[allow(clippy::as_conversions)] |
| 184 |
const IGNORE_MOUNTS = selinux_sys::SELINUX_RESTORECON_XATTR_IGNORE_MOUNTS as c_uint; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/// Restore file(s) default SELinux security contexts. |
| 189 |
#[derive(Debug, Default)] |
| 190 |
pub struct ContextRestore<'labeler, T: crate::label::back_end::BackEnd> { |
| 191 |
labeler: Option<&'labeler mut Labeler<T>>, |
| 192 |
} |
| 193 |
|
| 194 |
impl<'labeler, T> ContextRestore<'labeler, T> |
| 195 |
where |
| 196 |
T: crate::label::back_end::BackEnd, |
| 197 |
{ |
| 198 |
/// Set a labeling handle for relabeling. |
| 199 |
/// |
| 200 |
/// See: `selinux_restorecon_set_sehandle()`. |
| 201 |
#[doc(alias = "selinux_restorecon_set_sehandle")] |
| 202 |
pub fn with_labeler(labeler: &'labeler mut Labeler<T>) -> Self { |
| 203 |
Self { |
| 204 |
labeler: Some(labeler), |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/// Get the labeling handle to be used for relabeling. |
| 209 |
#[must_use] |
| 210 |
pub fn labeler(&self) -> Option<&&'labeler mut Labeler<T>> { |
| 211 |
self.labeler.as_ref() |
| 212 |
} |
| 213 |
|
| 214 |
/// Set an alternate root path for relabeling. |
| 215 |
/// |
| 216 |
/// See: `selinux_restorecon_set_alt_rootpath()`. |
| 217 |
#[doc(alias = "selinux_restorecon_set_alt_rootpath")] |
| 218 |
pub fn set_alternative_root_path(&mut self, path: impl AsRef<Path>) -> Result<()> { |
| 219 |
let c_path = os_str_to_c_string(path.as_ref().as_os_str())?; |
| 220 |
let r = unsafe { selinux_sys::selinux_restorecon_set_alt_rootpath(c_path.as_ptr()) }; |
| 221 |
ret_val_to_result("selinux_restorecon_set_alt_rootpath()", r) |
| 222 |
} |
| 223 |
|
| 224 |
/// Add to the list of directories to be excluded from relabeling. |
| 225 |
/// |
| 226 |
/// See: `selinux_restorecon_set_exclude_list()`. |
| 227 |
#[doc(alias = "selinux_restorecon_set_exclude_list")] |
| 228 |
pub fn add_exclude_list<P>( |
| 229 |
&mut self, |
| 230 |
exclusion_patterns: impl IntoIterator<Item = P>, |
| 231 |
) -> Result<()> |
| 232 |
where |
| 233 |
P: AsRef<Path>, |
| 234 |
{ |
| 235 |
let c_list_storage = exclusion_patterns |
| 236 |
.into_iter() |
| 237 |
.map(|path| os_str_to_c_string(path.as_ref().as_os_str())) |
| 238 |
.collect::<Result<Vec<_>>>()?; |
| 239 |
|
| 240 |
if !c_list_storage.is_empty() { |
| 241 |
let mut c_ptr_list: Vec<_> = c_list_storage |
| 242 |
.iter() |
| 243 |
.map(AsRef::as_ref) |
| 244 |
.map(CStr::as_ptr) |
| 245 |
.chain(iter::once(ptr::null())) |
| 246 |
.collect(); |
| 247 |
|
| 248 |
unsafe { selinux_sys::selinux_restorecon_set_exclude_list(c_ptr_list.as_mut_ptr()) }; |
| 249 |
} |
| 250 |
Ok(()) |
| 251 |
} |
| 252 |
|
| 253 |
/// Restore file(s) default SELinux security contexts. |
| 254 |
/// |
| 255 |
/// If `threads_count` is zero, then: |
| 256 |
/// - If `selinux_restorecon_parallel()` is supported by `libselinux` (version 3.4 or later), |
| 257 |
/// then this operation will use as many threads as the number of online processor |
| 258 |
/// cores present. |
| 259 |
/// - Otherwise, this operation will run in one thread. |
| 260 |
/// |
| 261 |
/// If this method succeeds, then it returns some statistics about the result of the operation. |
| 262 |
/// The information returned depends on the `libselinux` version, and on the specified `flags`. |
| 263 |
/// See the fields of [`FileSystemEntryContextRestoreResult`], in order to find out |
| 264 |
/// which [`RestoreFlags`] enable which statistics. |
| 265 |
/// |
| 266 |
/// See: `selinux_restorecon()`, `selinux_restorecon_parallel()`, |
| 267 |
/// `selinux_restorecon_get_skipped_errors()`, `selinux_restorecon_get_relabeled_files()`. |
| 268 |
#[doc(alias = "selinux_restorecon")] |
| 269 |
#[doc(alias = "selinux_restorecon_parallel")] |
| 270 |
#[allow(clippy::useless_conversion)] |
| 271 |
pub fn restore_context_of_file_system_entry( |
| 272 |
self, |
| 273 |
path: impl AsRef<Path>, |
| 274 |
threads_count: usize, |
| 275 |
flags: RestoreFlags, |
| 276 |
) -> Result<FileSystemEntryContextRestoreResult> { |
| 277 |
if let Some(labeler) = self.labeler.map(Labeler::as_mut_ptr) { |
| 278 |
unsafe { selinux_sys::selinux_restorecon_set_sehandle(labeler) }; |
| 279 |
} |
| 280 |
|
| 281 |
let c_path = os_str_to_c_string(path.as_ref().as_os_str())?; |
| 282 |
match threads_count { |
| 283 |
0 => { |
| 284 |
// Call `selinux_restorecon_parallel()` if possible. |
| 285 |
Error::clear_errno(); |
| 286 |
let r = unsafe { |
| 287 |
(OptionalNativeFunctions::get().selinux_restorecon_parallel)( |
| 288 |
c_path.as_ptr(), |
| 289 |
flags.bits(), |
| 290 |
threads_count, |
| 291 |
) |
| 292 |
}; |
| 293 |
|
| 294 |
if r == -1_i32 { |
| 295 |
if io::Error::last_os_error().raw_os_error() != Some(libc::ENOSYS) { |
| 296 |
return Err(Error::last_io_error("selinux_restorecon_parallel()")); |
| 297 |
} |
| 298 |
|
| 299 |
// `selinux_restorecon_parallel()` is unsupported. |
| 300 |
// Call `selinux_restorecon()` instead. |
| 301 |
let r = |
| 302 |
unsafe { selinux_sys::selinux_restorecon(c_path.as_ptr(), flags.bits()) }; |
| 303 |
ret_val_to_result("selinux_restorecon()", r)?; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
1 => { |
| 308 |
let r = unsafe { selinux_sys::selinux_restorecon(c_path.as_ptr(), flags.bits()) }; |
| 309 |
ret_val_to_result("selinux_restorecon()", r)?; |
| 310 |
} |
| 311 |
|
| 312 |
_ => { |
| 313 |
let r = unsafe { |
| 314 |
(OptionalNativeFunctions::get().selinux_restorecon_parallel)( |
| 315 |
c_path.as_ptr(), |
| 316 |
flags.bits(), |
| 317 |
threads_count, |
| 318 |
) |
| 319 |
}; |
| 320 |
ret_val_to_result("selinux_restorecon_parallel()", r)?; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
let skipped_errors = flags.contains(RestoreFlags::COUNT_ERRORS).then(|| { |
| 325 |
let r = |
| 326 |
unsafe { (OptionalNativeFunctions::get().selinux_restorecon_get_skipped_errors)() }; |
| 327 |
u64::from(r) |
| 328 |
}); |
| 329 |
|
| 330 |
let relabeled_files = if flags.contains(RestoreFlags::COUNT_RELABELED) { |
| 331 |
Error::clear_errno(); |
| 332 |
|
| 333 |
let r = unsafe { |
| 334 |
(OptionalNativeFunctions::get().selinux_restorecon_get_relabeled_files)() |
| 335 |
}; |
| 336 |
|
| 337 |
(r != 0 || io::Error::last_os_error().raw_os_error() != Some(libc::ENOSYS)) |
| 338 |
.then_some(u64::from(r)) |
| 339 |
} else { |
| 340 |
None |
| 341 |
}; |
| 342 |
|
| 343 |
Ok(FileSystemEntryContextRestoreResult { |
| 344 |
relabeled_files, |
| 345 |
skipped_errors, |
| 346 |
}) |
| 347 |
} |
| 348 |
|
| 349 |
/// Manage default `security.sehash` extended attribute entries added by |
| 350 |
/// `selinux_restorecon()`, `setfiles()` or `restorecon()`. |
| 351 |
/// |
| 352 |
/// See: `selinux_restorecon_xattr()`. |
| 353 |
#[doc(alias = "selinux_restorecon_xattr")] |
| 354 |
pub fn manage_security_sehash_xattr_entries( |
| 355 |
dir_path: impl AsRef<Path>, |
| 356 |
flags: XAttrFlags, |
| 357 |
) -> Result<DirectoryXAttributesIter> { |
| 358 |
let mut xattr_list_ptr: *mut *mut selinux_sys::dir_xattr = ptr::null_mut(); |
| 359 |
let c_dir_path = os_str_to_c_string(dir_path.as_ref().as_os_str())?; |
| 360 |
let r: c_int = unsafe { |
| 361 |
selinux_sys::selinux_restorecon_xattr( |
| 362 |
c_dir_path.as_ptr(), |
| 363 |
flags.bits(), |
| 364 |
&raw mut xattr_list_ptr, |
| 365 |
) |
| 366 |
}; |
| 367 |
|
| 368 |
if r == -1_i32 { |
| 369 |
return Err(Error::last_io_error("selinux_restorecon_xattr()")); |
| 370 |
} |
| 371 |
|
| 372 |
let Some(mut xattr_list_ptr) = ptr::NonNull::new(xattr_list_ptr) else { |
| 373 |
return Ok(DirectoryXAttributesIter(None)); // Empty list. |
| 374 |
}; |
| 375 |
|
| 376 |
let xattr_list = unsafe { *xattr_list_ptr.as_ref() }; |
| 377 |
|
| 378 |
// Detach the linked list from libselinux, because the returned DirectoryXAttributesIter |
| 379 |
// owns it from now on. |
| 380 |
unsafe { *xattr_list_ptr.as_mut() = ptr::null_mut() }; |
| 381 |
|
| 382 |
Ok(DirectoryXAttributesIter(ptr::NonNull::new(xattr_list))) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
/// Statistics of a successful [`ContextRestore::restore_context_of_file_system_entry`] operation. |
| 387 |
/// |
| 388 |
/// The information contained here depends on the `libselinux` version, |
| 389 |
/// and on the specified `flags`. |
| 390 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 391 |
#[non_exhaustive] |
| 392 |
pub struct FileSystemEntryContextRestoreResult { |
| 393 |
/// Number of files that were successfully relabeled. |
| 394 |
/// |
| 395 |
/// If `flags` included [`RestoreFlags::NO_CHANGE`], then this is the number of files |
| 396 |
/// that would be relabeled. |
| 397 |
/// |
| 398 |
/// This information will only be available when supported, and when `flags` included |
| 399 |
/// [`RestoreFlags::COUNT_RELABELED`]. |
| 400 |
pub relabeled_files: Option<u64>, |
| 401 |
|
| 402 |
/// Number of errors ignored during the file tree walk. |
| 403 |
/// |
| 404 |
/// This information will only be available when supported, and when `flags` included |
| 405 |
/// [`RestoreFlags::COUNT_ERRORS`]. |
| 406 |
pub skipped_errors: Option<u64>, |
| 407 |
} |
| 408 |
|
| 409 |
/// Status of a [`DirectoryXAttributes`]. |
| 410 |
#[derive(Debug)] |
| 411 |
#[non_exhaustive] |
| 412 |
pub enum DirectoryDigestResult { |
| 413 |
/// Match. |
| 414 |
Match { |
| 415 |
/// Matching digest deleted from the directory. |
| 416 |
deleted: bool, |
| 417 |
}, |
| 418 |
/// No match. |
| 419 |
NoMatch { |
| 420 |
/// Non-matching digest deleted from the directory. |
| 421 |
deleted: bool, |
| 422 |
}, |
| 423 |
/// Error. |
| 424 |
Error, |
| 425 |
/// Unknown status. |
| 426 |
Unknown(c_uint), |
| 427 |
} |
| 428 |
|
| 429 |
/// Result of [`ContextRestore::manage_security_sehash_xattr_entries`]. |
| 430 |
#[derive(Debug)] |
| 431 |
pub struct DirectoryXAttributes { |
| 432 |
pointer: ptr::NonNull<selinux_sys::dir_xattr>, |
| 433 |
_phantom_data: PhantomData<selinux_sys::dir_xattr>, |
| 434 |
} |
| 435 |
|
| 436 |
impl DirectoryXAttributes { |
| 437 |
/// Return the managed raw pointer to [`selinux_sys::dir_xattr`]. |
| 438 |
#[must_use] |
| 439 |
pub fn as_ptr(&self) -> *const selinux_sys::dir_xattr { |
| 440 |
self.pointer.as_ptr() |
| 441 |
} |
| 442 |
|
| 443 |
/// Return the managed raw pointer to [`selinux_sys::dir_xattr`]. |
| 444 |
#[must_use] |
| 445 |
pub fn as_mut_ptr(&mut self) -> *mut selinux_sys::dir_xattr { |
| 446 |
self.pointer.as_ptr() |
| 447 |
} |
| 448 |
|
| 449 |
/// Directory path. |
| 450 |
#[must_use] |
| 451 |
pub fn directory_path(&self) -> &Path { |
| 452 |
c_str_ptr_to_path(unsafe { self.pointer.as_ref().directory }) |
| 453 |
} |
| 454 |
|
| 455 |
/// A hex encoded string that can be printed. |
| 456 |
pub fn digest(&self) -> Result<&str> { |
| 457 |
c_str_ptr_to_str(unsafe { self.pointer.as_ref().digest }) |
| 458 |
} |
| 459 |
|
| 460 |
/// Status of this entry. |
| 461 |
#[must_use] |
| 462 |
pub fn digest_result(&self) -> DirectoryDigestResult { |
| 463 |
match unsafe { self.pointer.as_ref().result } { |
| 464 |
selinux_sys::digest_result::MATCH => DirectoryDigestResult::Match { deleted: false }, |
| 465 |
|
| 466 |
selinux_sys::digest_result::NOMATCH => { |
| 467 |
DirectoryDigestResult::NoMatch { deleted: false } |
| 468 |
} |
| 469 |
|
| 470 |
selinux_sys::digest_result::DELETED_MATCH => { |
| 471 |
DirectoryDigestResult::Match { deleted: true } |
| 472 |
} |
| 473 |
|
| 474 |
selinux_sys::digest_result::DELETED_NOMATCH => { |
| 475 |
DirectoryDigestResult::NoMatch { deleted: true } |
| 476 |
} |
| 477 |
|
| 478 |
selinux_sys::digest_result::ERROR => DirectoryDigestResult::Error, |
| 479 |
|
| 480 |
value => DirectoryDigestResult::Unknown(value), |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
impl Drop for DirectoryXAttributes { |
| 486 |
fn drop(&mut self) { |
| 487 |
unsafe { |
| 488 |
libc::free(self.pointer.as_ref().directory.cast()); |
| 489 |
libc::free(self.pointer.as_ref().digest.cast()); |
| 490 |
libc::free(self.pointer.as_ptr().cast()); |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
/// Iterator producing [`DirectoryXAttributes`] elements. |
| 496 |
#[derive(Debug)] |
| 497 |
pub struct DirectoryXAttributesIter(Option<ptr::NonNull<selinux_sys::dir_xattr>>); |
| 498 |
|
| 499 |
impl Drop for DirectoryXAttributesIter { |
| 500 |
fn drop(&mut self) { |
| 501 |
self.for_each(drop); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
impl Iterator for DirectoryXAttributesIter { |
| 506 |
type Item = DirectoryXAttributes; |
| 507 |
|
| 508 |
fn next(&mut self) -> Option<DirectoryXAttributes> { |
| 509 |
self.0.map(|pointer| { |
| 510 |
// `pointer` is the head of the linked list that we currently own. |
| 511 |
// Detach the head and return it inside a `DirectoryXAttributes` which owns it. |
| 512 |
// We still own the remaining tail of the linked list. |
| 513 |
|
| 514 |
self.0 = ptr::NonNull::new(unsafe { pointer.as_ref() }.next); |
| 515 |
|
| 516 |
DirectoryXAttributes { |
| 517 |
pointer, |
| 518 |
_phantom_data: PhantomData, |
| 519 |
} |
| 520 |
}) |
| 521 |
} |
| 522 |
} |