Skip to content

Commit

Permalink
a final pass on location.rs to match the new error handling for decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouaix committed Dec 12, 2024
1 parent dbb2cda commit bdb31c7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 122 deletions.
23 changes: 8 additions & 15 deletions src/decoders/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use crate::{
},
location::{
client_authorization_status, client_manager_state_tracker_state, daemon_status_type,
io_message, location_manager_state_tracker_state, sqlite, subharvester_identifier,
io_message, location_manager_state_tracker_state, sqlite_location,
subharvester_identifier,
},
network::{ipv_four, ipv_six, sockaddr},
opendirectory::{errors, member_details, member_id_type, sid_details},
Expand Down Expand Up @@ -73,25 +74,17 @@ pub(crate) fn check_objects(
} else if format_string.contains("odtypes:nt_sid_t") {
Ok(sid_details(&message_values[index].message_strings))
} else if format_string.contains("location:CLClientAuthorizationStatus") {
Ok(client_authorization_status(
&message_values[index].message_strings,
))
client_authorization_status(&message_values[index].message_strings)
} else if format_string.contains("location:CLDaemonStatus_Type::Reachability") {
Ok(daemon_status_type(&message_values[index].message_strings))
daemon_status_type(&message_values[index].message_strings)
} else if format_string.contains("location:CLSubHarvesterIdentifier") {
Ok(subharvester_identifier(
&message_values[index].message_strings,
))
subharvester_identifier(&message_values[index].message_strings)
} else if format_string.contains("location:SqliteResult") {
Ok(sqlite(&message_values[index].message_strings))
sqlite_location(&message_values[index].message_strings).map(ToString::to_string)
} else if format_string.contains("location:_CLClientManagerStateTrackerState") {
Ok(client_manager_state_tracker_state(
&message_values[index].message_strings,
))
client_manager_state_tracker_state(&message_values[index].message_strings)
} else if format_string.contains("location:_CLLocationManagerStateTrackerState") {
Ok(location_manager_state_tracker_state(
&message_values[index].message_strings,
))
location_manager_state_tracker_state(&message_values[index].message_strings)
} else if format_string.contains("network:in6_addr") {
Ok(ipv_six(&message_values[index].message_strings))
} else if format_string.contains("network:in_addr") {
Expand Down
191 changes: 84 additions & 107 deletions src/decoders/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

use crate::util::decode_standard;

use super::bool::{lowercase_bool, lowercase_int_bool};
use log::{error, warn};
use super::{
bool::{lowercase_bool, lowercase_int_bool},
DecoderError,
};
use log::warn;
use nom::{
bytes::complete::take,
number::complete::{le_f64, le_i32, le_i64, le_u32, le_u8},
sequence::tuple,
IResult,
};

#[derive(Debug, Default)]
Expand Down Expand Up @@ -48,42 +52,45 @@ struct LocationTrackerState {
}

/// Convert Core Location Client Autherization Status code to string
pub(crate) fn client_authorization_status(status: &str) -> String {
pub(crate) fn client_authorization_status(status: &str) -> Result<String, DecoderError<'_>> {
let message = match status {
"0" => "Not Determined",
"1" => "Restricted",
"2" => "Denied",
"3" => "Authorized Always",
"4" => "Authorized When In Use",
_ => {
warn!(
"Unknown Core Location client authorization status: {}",
status
);
status
return Err(DecoderError::Parse {
input: status.as_bytes(),
parser_name: "client authorization status",
message: "Unknown Core Location client authorization status",
});
}
};
message.to_string()
Ok(message.to_string())
}

/// Convert Core Location Daemon Status type to string
pub(crate) fn daemon_status_type(status: &str) -> String {
pub(crate) fn daemon_status_type(status: &str) -> Result<String, DecoderError<'_>> {
// Found in dyldcache liblog
let message = match status {
"0" => "Reachability Unavailable",
"1" => "Reachability Small",
"2" => "Reachability Large",
"56" => "Reachability Unachievable",
_ => {
warn!("Unknown Core Location daemon status type: {}", status);
status
return Err(DecoderError::Parse {
input: status.as_bytes(),
parser_name: "daemon status type",
message: "Unknown Core Location daemon status type",
});
}
};
message.to_string()
Ok(message.to_string())
}

/// Convert Core Location Subhaverester id to string
pub(crate) fn subharvester_identifier(status: &str) -> String {
pub(crate) fn subharvester_identifier(status: &str) -> Result<String, DecoderError<'_>> {
// Found in dyldcache liblog
let message = match status {
"1" => "Wifi",
Expand All @@ -100,46 +107,36 @@ pub(crate) fn subharvester_identifier(status: &str) -> String {
"12" => "Ionosphere",
"13" => "Unknown",
_ => {
warn!(
"Unknown Core Location subhaverster identifier type: {}",
status
);
status
return Err(DecoderError::Parse {
input: status.as_bytes(),
parser_name: "subharvester identifier",
message: "Unknown Core Location subhaverster identifier type",
});
}
};
message.to_string()
Ok(message.to_string())
}

/// Convert Core Location SQLITE code to string
pub(crate) fn sqlite(status: &str) -> String {
let decoded_data_result = decode_standard(status);
let decoded_data = match decoded_data_result {
Ok(result) => result,
Err(err) => {
error!(
"[macos-unifiedlogs] Failed to base64 decodesqlite {}, error: {:?}",
status, err
);
return String::from("Failed to base64 decode sqlite details");
}
};

let message_result = get_sqlite_data(&decoded_data);
match message_result {
Ok((_, result)) => result,
Err(err) => {
error!(
"[macos-unifiedlogs] Failed to get sqlite {}, error code, error: {:?}",
status, err
);
String::from("Failed to get sqlite error")
}
}
pub(crate) fn sqlite_location(input: &str) -> Result<&'static str, DecoderError<'_>> {
let decoded_data = decode_standard(input).map_err(|_| DecoderError::Parse {
input: input.as_bytes(),
parser_name: "sqlite location",
message: "Failed to base64 decode sqlite details",
})?;

let (_, result) = get_sqlite_data(&decoded_data).map_err(|_| DecoderError::Parse {
input: input.as_bytes(),
parser_name: "sqlite location",
message: "Failed to get sqlite error",
})?;

Ok(result)
}

/// Get the SQLITE error message
fn get_sqlite_data(data: &[u8]) -> nom::IResult<&[u8], String> {
let (empty, sqlite_code) = le_u32(data)?;
fn get_sqlite_data(input: &[u8]) -> IResult<&[u8], &'static str> {
let (input, sqlite_code) = le_u32(input)?;

// Found at https://www.sqlite.org/rescode.html
let message = match sqlite_code {
Expand Down Expand Up @@ -184,35 +181,28 @@ fn get_sqlite_data(data: &[u8]) -> nom::IResult<&[u8], String> {
}
};

Ok((empty, message.to_string()))
Ok((input, message))
}

/// Parse the manager tracker state data
pub(crate) fn client_manager_state_tracker_state(status: &str) -> String {
let decoded_data_result = decode_standard(status);
let decoded_data = match decoded_data_result {
Ok(result) => result,
Err(err) => {
error!("[macos-unifiedlogs] Failed to base64 decode client manager tracker state {}, error: {:?}", status, err);
return String::from("Failed to base64 decode client manager tracker state");
}
};

let message_result = get_state_tracker_data(&decoded_data);
match message_result {
Ok((_, result)) => result,
Err(err) => {
error!(
"[macos-unifiedlogs] Failed to get client tracker data {}, error: {:?}",
status, err
);
String::from("Failed to get client tracker data")
}
}
pub(crate) fn client_manager_state_tracker_state(input: &str) -> Result<String, DecoderError<'_>> {
let decoded_data = decode_standard(input).map_err(|_| DecoderError::Parse {
input: input.as_bytes(),
parser_name: "client manager state tracker state",
message: "Failed to base64 decode client manager tracker state",
})?;

let (_, result) = get_state_tracker_data(&decoded_data).map_err(|_| DecoderError::Parse {
input: input.as_bytes(),
parser_name: "client manager state tracker state",
message: "Failed to get client tracker data",
})?;

Ok(result)
}

/// Get the tracker data
pub(crate) fn get_state_tracker_data(input: &[u8]) -> nom::IResult<&[u8], String> {
pub(crate) fn get_state_tracker_data(input: &[u8]) -> IResult<&[u8], String> {
let (input, (location_enabled, location_restricted)) = tuple((le_u32, le_u32))(input)?;
Ok((
input,
Expand All @@ -225,27 +215,23 @@ pub(crate) fn get_state_tracker_data(input: &[u8]) -> nom::IResult<&[u8], String
}

/// Parse location tracker state data
pub(crate) fn location_manager_state_tracker_state(status: &str) -> String {
let decoded_data_result = decode_standard(status);
let decoded_data = match decoded_data_result {
Ok(result) => result,
Err(err) => {
error!("[macos-unifiedlogs] Failed to base64 decode location manager tracker state {}, error: {:?}", status, err);
return String::from("Failed to base64 decode logon manager trackder data");
}
};

let message_result = get_location_tracker_state(&decoded_data);
match message_result {
Ok((_, result)) => result,
Err(err) => {
error!(
"[macos-unifiedlogs] Failed to get location tracker data {}, error: {:?}",
status, err
);
String::from("Failed to get logon manager trackder data")
}
}
pub(crate) fn location_manager_state_tracker_state(
input: &str,
) -> Result<String, DecoderError<'_>> {
let decoded_data = decode_standard(input).map_err(|_| DecoderError::Parse {
input: input.as_bytes(),
parser_name: "location manager state tracker state",
message: "Failed to base64 decode logon manager trackder data",
})?;

let (_, result) =
get_location_tracker_state(&decoded_data).map_err(|_| DecoderError::Parse {
input: input.as_bytes(),
parser_name: "location manager state tracker state",
message: "Failed to get logon manager tracker data",
})?;

Ok(result)
}

/// Get the location state data
Expand Down Expand Up @@ -546,46 +532,37 @@ pub(crate) fn get_daemon_status_tracker(input: &[u8]) -> nom::IResult<&[u8], Str

#[cfg(test)]
mod tests {
use crate::{
decoders::location::{
client_manager_state_tracker_state, daemon_status_type, get_daemon_status_tracker,
get_location_tracker_state, get_sqlite_data, get_state_tracker_data, io_message,
location_manager_state_tracker_state, location_tracker_object, sqlite,
subharvester_identifier, LocationTrackerState,
},
util::decode_standard,
};

use super::client_authorization_status;
use super::*;
use crate::util::decode_standard;

#[test]
fn test_client_authorization_status() {
let test_data = "0";
let result = client_authorization_status(test_data);
let result = client_authorization_status(test_data).unwrap();

assert_eq!(result, "Not Determined")
}

#[test]
fn test_daemon_status_type() {
let test_data = "2";
let result = daemon_status_type(test_data);
let result = daemon_status_type(test_data).unwrap();

assert_eq!(result, "Reachability Large")
}

#[test]
fn test_subharvester_identifier() {
let test_data = "2";
let result = subharvester_identifier(test_data);
let result = subharvester_identifier(test_data).unwrap();

assert_eq!(result, "Tracks")
}

#[test]
fn test_sqlite() {
let test_data = "AAAAAA==";
let result = sqlite(test_data);
let result = sqlite_location(test_data).unwrap();

assert_eq!(result, "SQLITE OK")
}
Expand All @@ -603,7 +580,7 @@ mod tests {
#[test]
fn test_client_manager_state_tracker_state() {
let test_data = "AQAAAAAAAAA=";
let result = client_manager_state_tracker_state(test_data);
let result = client_manager_state_tracker_state(test_data).unwrap();

assert_eq!(
result,
Expand Down Expand Up @@ -639,7 +616,7 @@ mod tests {
#[test]
fn test_location_manager_state_tracker_state() {
let test_data = "AAAAAAAA8L8AAAAAAABZQAAAAAAAAAAAAAAAAAAA8D8BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAQAAAAAAAAAA";
let result = location_manager_state_tracker_state(test_data);
let result = location_manager_state_tracker_state(test_data).unwrap();

assert_eq!(
result,
Expand Down

0 comments on commit bdb31c7

Please sign in to comment.