Skip to content

Commit

Permalink
Fix Windows tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MEhrn00 committed Feb 16, 2024
1 parent d7995e5 commit d6aa6cf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Payload_Type/thanatos/agent/ffiwrappers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ version.workspace = true
bitflags.workspace = true
generic-array = "1"
hex-literal.workspace = true
libc.workspace = true
windows.workspace = true

[target.'cfg(target_os = "linux")'.dependencies]
libc.workspace = true
10 changes: 10 additions & 0 deletions Payload_Type/thanatos/agent/ffiwrappers/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub enum FfiError {
OsError(i32),
NoNullTerminator,
InteriorNull,

#[cfg(target_os = "linux")]
GaiError(EaiError),
NonNullPointer,
CanonNameNotFound,
Expand All @@ -13,10 +15,16 @@ impl FfiError {
pub fn os_error() -> Self {
Self::OsError(libc_errno())
}

#[cfg(target_os = "windows")]
pub fn from_windows_error(e: windows::core::Error) -> Self {
Self::OsError(e.code().0)
}
}

#[derive(Debug)]
#[repr(i32)]
#[cfg(target_os = "linux")]
pub enum EaiError {
Other(i32),
System(i32),
Expand All @@ -31,6 +39,7 @@ pub enum EaiError {
Overflow = libc::EAI_OVERFLOW,
}

#[cfg(target_os = "linux")]
impl EaiError {
pub fn from_code(code: i32) -> EaiError {
match code {
Expand All @@ -50,6 +59,7 @@ impl EaiError {
}

/// Returns the libc `errno` value
#[cfg(target_os = "linux")]
fn libc_errno() -> i32 {
// SAFETY: `__errno_location` is a pointer to libc's errno value. This pointer
// is guaranteed to be aligned and non-NULL
Expand Down
8 changes: 4 additions & 4 deletions Payload_Type/thanatos/agent/ffiwrappers/src/windows/domain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use errors::ThanatosError;
use crate::errors::FfiError;

use windows::{
core::{Error as WinError, PSTR},
Expand All @@ -9,7 +9,7 @@ use windows::{
};

/// Get the domain name of the system
pub fn domain() -> Result<String, ThanatosError> {
pub fn domain() -> Result<String, FfiError> {
let mut domainname_length = 0u32;

// Get the length of the computer's domain name.
Expand All @@ -28,7 +28,7 @@ pub fn domain() -> Result<String, ThanatosError> {
Err(e) if e.code() == WinError::from(ERROR_MORE_DATA).code() => (),

// Check if any other error was returned
Err(e) => return Err(ThanatosError::from_windows(e)),
Err(e) => return Err(FfiError::from_windows_error(e)),

// This function should never return successfully since the length is 0
_ => unreachable!(),
Expand All @@ -53,7 +53,7 @@ pub fn domain() -> Result<String, ThanatosError> {
&mut domainname_length,
)
}
.map_err(ThanatosError::from_windows)?;
.map_err(FfiError::from_windows_error)?;

// Cast the domain name length.
// The domain name length value now contains the length of the system's domain name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use errors::ThanatosError;
use crate::errors::FfiError;

use windows::{
core::{Error as WinError, PSTR},
Expand All @@ -8,7 +8,7 @@ use windows::{
},
};

pub fn hostname() -> Result<String, ThanatosError> {
pub fn hostname() -> Result<String, FfiError> {
let mut hostname_length = 0u32;

// Get the length of the computer's hostname.
Expand All @@ -27,7 +27,7 @@ pub fn hostname() -> Result<String, ThanatosError> {
Err(e) if e.code() == WinError::from(ERROR_MORE_DATA).code() => (),

// Check if any other error was returned
Err(e) => return Err(ThanatosError::from_windows(e)),
Err(e) => return Err(FfiError::from_windows_error(e)),

// This function should never return successfully since the length is 0
_ => unreachable!(),
Expand All @@ -52,7 +52,7 @@ pub fn hostname() -> Result<String, ThanatosError> {
&mut hostname_length,
)
}
.map_err(ThanatosError::from_windows)?;
.map_err(FfiError::from_windows_error)?;

// Cast the hostname length.
// The hostname length value now contains the length of the system's hostname
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use errors::ThanatosError;
use crate::errors::FfiError;

use windows::{
core::{Error as WinError, PSTR},
Win32::{Foundation::ERROR_INSUFFICIENT_BUFFER, System::WindowsProgramming::GetUserNameA},
};

pub fn username() -> Result<String, ThanatosError> {
pub fn username() -> Result<String, FfiError> {
let mut username_length = 0u32;

// Get the length of the current user's username
Expand All @@ -18,7 +18,7 @@ pub fn username() -> Result<String, ThanatosError> {
Err(e) if e.code() == WinError::from(ERROR_INSUFFICIENT_BUFFER).code() => (),

// Check if any other error was returned
Err(e) => return Err(ThanatosError::from_windows(e)),
Err(e) => return Err(FfiError::from_windows_error(e)),

// This function should never return successfully since the length is 0
_ => unreachable!(),
Expand All @@ -37,7 +37,7 @@ pub fn username() -> Result<String, ThanatosError> {
// length of the username was found above. The username length must match the
// length of the allocated buffer! An error needs to be checked in case the function fails
unsafe { GetUserNameA(PSTR(username_buffer.as_mut_ptr()), &mut username_length) }
.map_err(ThanatosError::from_windows)?;
.map_err(FfiError::from_windows_error)?;

// Cast the username length.
// The username length value now contains the length of the current user's username
Expand Down

0 comments on commit d6aa6cf

Please sign in to comment.