| 1 |
#[cfg(test)] |
| 2 |
mod tests; |
| 3 |
|
| 4 |
use std::os::raw::{c_char, c_int, c_void}; |
| 5 |
|
| 6 |
/// Call back for SELinux operations. |
| 7 |
pub trait CallBack { |
| 8 |
/// Prototype of call back function. |
| 9 |
type CallBackType; |
| 10 |
|
| 11 |
/// Get the current call back function, if one has been set. |
| 12 |
/// |
| 13 |
/// See: `selinux_get_callback()`. |
| 14 |
#[doc(alias = "selinux_get_callback")] |
| 15 |
fn get_call_back() -> Option<Self::CallBackType>; |
| 16 |
|
| 17 |
/// Set or clear the call back function. |
| 18 |
/// |
| 19 |
/// See: `selinux_set_callback()`. |
| 20 |
#[doc(alias = "selinux_set_callback")] |
| 21 |
fn set_call_back(call_back: Option<Self::CallBackType>); |
| 22 |
} |
| 23 |
|
| 24 |
/// Call back used for logging. |
| 25 |
#[derive(Debug, Default)] |
| 26 |
#[non_exhaustive] |
| 27 |
pub struct Log; |
| 28 |
|
| 29 |
impl CallBack for Log { |
| 30 |
type CallBackType = unsafe extern "C" fn(c_int, *const c_char, ...) -> c_int; |
| 31 |
|
| 32 |
fn get_call_back() -> Option<Self::CallBackType> { |
| 33 |
unsafe { selinux_sys::selinux_get_callback(selinux_sys::SELINUX_CB_LOG).func_log } |
| 34 |
} |
| 35 |
|
| 36 |
fn set_call_back(func_log: Option<Self::CallBackType>) { |
| 37 |
use selinux_sys::{SELINUX_CB_LOG, selinux_callback, selinux_set_callback}; |
| 38 |
unsafe { selinux_set_callback(SELINUX_CB_LOG, selinux_callback { func_log }) } |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/// Call back used for supplemental auditing in AVC messages. |
| 43 |
#[derive(Debug, Default)] |
| 44 |
#[non_exhaustive] |
| 45 |
pub struct Audit; |
| 46 |
|
| 47 |
impl CallBack for Audit { |
| 48 |
type CallBackType = unsafe extern "C" fn( |
| 49 |
*mut c_void, |
| 50 |
selinux_sys::security_class_t, |
| 51 |
*mut c_char, |
| 52 |
usize, |
| 53 |
) -> c_int; |
| 54 |
|
| 55 |
fn get_call_back() -> Option<Self::CallBackType> { |
| 56 |
unsafe { selinux_sys::selinux_get_callback(selinux_sys::SELINUX_CB_AUDIT).func_audit } |
| 57 |
} |
| 58 |
|
| 59 |
fn set_call_back(func_audit: Option<Self::CallBackType>) { |
| 60 |
use selinux_sys::{SELINUX_CB_AUDIT, selinux_callback, selinux_set_callback}; |
| 61 |
unsafe { selinux_set_callback(SELINUX_CB_AUDIT, selinux_callback { func_audit }) } |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/// Call back used for context validation. |
| 66 |
#[derive(Debug, Default)] |
| 67 |
#[non_exhaustive] |
| 68 |
pub struct ContextValidation; |
| 69 |
|
| 70 |
impl CallBack for ContextValidation { |
| 71 |
type CallBackType = unsafe extern "C" fn(*mut *mut c_char) -> c_int; |
| 72 |
|
| 73 |
fn get_call_back() -> Option<Self::CallBackType> { |
| 74 |
unsafe { selinux_sys::selinux_get_callback(selinux_sys::SELINUX_CB_VALIDATE).func_validate } |
| 75 |
} |
| 76 |
|
| 77 |
fn set_call_back(func_validate: Option<Self::CallBackType>) { |
| 78 |
use selinux_sys::{SELINUX_CB_VALIDATE, selinux_callback, selinux_set_callback}; |
| 79 |
unsafe { selinux_set_callback(SELINUX_CB_VALIDATE, selinux_callback { func_validate }) } |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/// Call back invoked when the system enforcing state changes. |
| 84 |
#[derive(Debug, Default)] |
| 85 |
#[non_exhaustive] |
| 86 |
pub struct EnforcingChange; |
| 87 |
|
| 88 |
impl CallBack for EnforcingChange { |
| 89 |
type CallBackType = unsafe extern "C" fn(c_int) -> c_int; |
| 90 |
|
| 91 |
fn get_call_back() -> Option<Self::CallBackType> { |
| 92 |
use selinux_sys::{SELINUX_CB_SETENFORCE, selinux_get_callback}; |
| 93 |
unsafe { selinux_get_callback(SELINUX_CB_SETENFORCE).func_setenforce } |
| 94 |
} |
| 95 |
|
| 96 |
fn set_call_back(func_setenforce: Option<Self::CallBackType>) { |
| 97 |
use selinux_sys::{SELINUX_CB_SETENFORCE, selinux_callback, selinux_set_callback}; |
| 98 |
unsafe { selinux_set_callback(SELINUX_CB_SETENFORCE, selinux_callback { func_setenforce }) } |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/// Call back invoked when the system security policy is reloaded. |
| 103 |
#[derive(Debug, Default)] |
| 104 |
#[non_exhaustive] |
| 105 |
pub struct SecurityPolicyReload; |
| 106 |
|
| 107 |
impl CallBack for SecurityPolicyReload { |
| 108 |
type CallBackType = unsafe extern "C" fn(c_int) -> c_int; |
| 109 |
|
| 110 |
fn get_call_back() -> Option<Self::CallBackType> { |
| 111 |
use selinux_sys::{SELINUX_CB_POLICYLOAD, selinux_get_callback}; |
| 112 |
unsafe { selinux_get_callback(SELINUX_CB_POLICYLOAD).func_policyload } |
| 113 |
} |
| 114 |
|
| 115 |
fn set_call_back(func_policyload: Option<Self::CallBackType>) { |
| 116 |
use selinux_sys::{SELINUX_CB_POLICYLOAD, selinux_callback, selinux_set_callback}; |
| 117 |
unsafe { selinux_set_callback(SELINUX_CB_POLICYLOAD, selinux_callback { func_policyload }) } |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/// Log type argument indicating the type of message. |
| 122 |
pub mod log_type { |
| 123 |
use std::os::raw::c_int; |
| 124 |
|
| 125 |
/// Error log entry. |
| 126 |
pub use selinux_sys::SELINUX_ERROR as ERROR; |
| 127 |
|
| 128 |
/// Warning log entry. |
| 129 |
pub use selinux_sys::SELINUX_WARNING as WARNING; |
| 130 |
|
| 131 |
/// Informational log entry. |
| 132 |
pub use selinux_sys::SELINUX_INFO as INFO; |
| 133 |
|
| 134 |
/// AVC log entry. |
| 135 |
pub use selinux_sys::SELINUX_AVC as AVC; |
| 136 |
|
| 137 |
// The rest of the constants were defined after version 2.8, so selinux_sys might not |
| 138 |
// export them. We therefore define them manually. |
| 139 |
|
| 140 |
/// Policy loaded. |
| 141 |
pub static POLICY_LOAD: c_int = 4_i32; |
| 142 |
|
| 143 |
/// SELinux enforcing mode changed. |
| 144 |
pub static SET_ENFORCE: c_int = 5_i32; |
| 145 |
} |