diff --git a/src/service.rs b/src/service.rs index 3fe426d..36d7c9f 100644 --- a/src/service.rs +++ b/src/service.rs @@ -359,6 +359,45 @@ impl ServiceFailureActions { } } +/// Enum describing the service launch protection options. +/// +/// See +/// for more information. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[repr(u32)] +pub enum ServiceLaunchProtected { + /// No launch protection. The service can be modified or replaced without restriction. + None = Services::SERVICE_LAUNCH_PROTECTED_NONE, + + /// Launch protection for Windows components. + Windows = Services::SERVICE_LAUNCH_PROTECTED_WINDOWS, + + /// A lighter version of Windows launch protection. + WindowsLight = Services::SERVICE_LAUNCH_PROTECTED_WINDOWS_LIGHT, + + /// Launch protection used by antimalware (ELAM) services. + AntimalwareLight = Services::SERVICE_LAUNCH_PROTECTED_ANTIMALWARE_LIGHT, +} +impl TryFrom for ServiceLaunchProtected { + type Error = Error; + + fn try_from(value: u32) -> Result { + match value { + Services::SERVICE_LAUNCH_PROTECTED_NONE => Ok(ServiceLaunchProtected::None), + Services::SERVICE_LAUNCH_PROTECTED_WINDOWS => Ok(ServiceLaunchProtected::Windows), + Services::SERVICE_LAUNCH_PROTECTED_WINDOWS_LIGHT => { + Ok(ServiceLaunchProtected::WindowsLight) + } + Services::SERVICE_LAUNCH_PROTECTED_ANTIMALWARE_LIGHT => { + Ok(ServiceLaunchProtected::AntimalwareLight) + } + _ => Err(Error::ParseValue( + "Invalid launch protection value", + ParseRawError::InvalidInteger(value), + )), + } + } +} /// A struct that describes the service. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ServiceInfo { @@ -1802,6 +1841,36 @@ impl Service { } } + /// Set service launch protection. + /// This is a security feature that allows the service to run in a more secure environment. + /// there is no example because you need EV Certification and ELAM Driver to test it. + /// Please refer to the official documentation for more information: + /// + pub fn set_launch_protected(&self, protection: ServiceLaunchProtected) -> crate::Result<()> { + let mut launch_protected = + unsafe { mem::zeroed::() }; + launch_protected.dwLaunchProtected = protection as u32; + unsafe { + self.change_config2( + Services::SERVICE_CONFIG_LAUNCH_PROTECTED, + &mut launch_protected, + ) + .map_err(Error::Winapi) + } + } + + /// Get service launch protection. + /// This is a security feature that allows the service to run in a more secure environment. + pub fn get_launch_protected(&self) -> crate::Result { + let mut data = [0u8; std::mem::size_of::()]; + unsafe { + let raw_data: Services::SERVICE_LAUNCH_PROTECTED_INFO = self + .query_config2(Services::SERVICE_CONFIG_LAUNCH_PROTECTED, &mut data) + .map_err(Error::Winapi)?; + raw_data.dwLaunchProtected.try_into() + } + } + /// Set service description. /// /// Required permission: [`ServiceAccess::CHANGE_CONFIG`].