lockdownd bindings

This commit is contained in:
Jackson Coxson
2025-03-28 12:12:06 -06:00
parent 6c86212d9b
commit 2fde404f88
3 changed files with 460 additions and 0 deletions

149
ffi/examples/lockdownd.c Normal file
View File

@@ -0,0 +1,149 @@
// Jackson Coxson
#include "idevice.h"
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Initialize logger
idevice_init_logger(Debug, Disabled, NULL);
// Create the socket address (replace with your device's IP)
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(LOCKDOWN_PORT);
inet_pton(AF_INET, "10.7.0.2", &addr.sin_addr);
// Read pairing file (replace with your pairing file path)
IdevicePairingFile *pairing_file = NULL;
IdeviceErrorCode err =
idevice_pairing_file_read("pairing_file.plist", &pairing_file);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to read pairing file: %d\n", err);
return 1;
}
// Create TCP provider
TcpProviderHandle *provider = NULL;
err = idevice_tcp_provider_new((struct sockaddr *)&addr, pairing_file,
"LockdowndTest", &provider);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to create TCP provider: %d\n", err);
idevice_pairing_file_free(pairing_file);
return 1;
}
// Connect to lockdownd
LockdowndClientHandle *client = NULL;
err = lockdownd_connect_tcp(provider, &client);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to connect to lockdownd: %d\n", err);
tcp_provider_free(provider);
return 1;
}
// Read pairing file (replace with your pairing file path)
IdevicePairingFile *pairing_file_2 = NULL;
err = idevice_pairing_file_read("pairing_file.plist", &pairing_file_2);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to read pairing file: %d\n", err);
return 1;
}
// Start session
err = lockdownd_start_session(client, pairing_file_2);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to start session: %d\n", err);
lockdownd_client_free(client);
tcp_provider_free(provider);
return 1;
}
// Get device name
plist_t name_plist = NULL;
err = lockdownd_get_value(client, "DeviceName", &name_plist);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to get device name: %d\n", err);
} else {
char *name = NULL;
plist_get_string_val(name_plist, &name);
printf("Device name: %s\n", name);
free(name);
plist_free(name_plist);
}
// Get product version
plist_t version_plist = NULL;
err = lockdownd_get_value(client, "ProductVersion", &version_plist);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to get product version: %d\n", err);
} else {
char *version = NULL;
plist_get_string_val(version_plist, &version);
printf("iOS version: %s\n", version);
free(version);
plist_free(version_plist);
}
// Get all values
plist_t all_values = NULL;
err = lockdownd_get_all_values(client, &all_values);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to get all values: %d\n", err);
} else {
printf("\nAll device values:\n");
// Iterate through dictionary (simplified example)
plist_dict_iter it = NULL;
plist_dict_new_iter(all_values, &it);
if (it) {
char *key = NULL;
plist_t val = NULL;
do {
plist_dict_next_item(all_values, it, &key, &val);
if (key) {
printf("- %s: ", key);
// Print value based on type (simplified)
if (plist_get_node_type(val) == PLIST_STRING) {
char *str_val = NULL;
plist_get_string_val(val, &str_val);
printf("%s", str_val);
free(str_val);
} else if (plist_get_node_type(val) == PLIST_BOOLEAN) {
uint8_t bool_val = 0;
plist_get_bool_val(val, &bool_val);
printf("%s", bool_val ? "true" : "false");
} else if (plist_get_node_type(val) == PLIST_UINT) {
uint64_t int_val = 0;
plist_get_uint_val(val, &int_val);
printf("%llu", int_val);
}
printf("\n");
free(key);
}
} while (key);
free(it);
}
plist_free(all_values);
}
// Test starting a service (heartbeat in this example)
uint16_t port = 0;
bool ssl = false;
err = lockdownd_start_service(client, "com.apple.mobile.heartbeat", &port,
&ssl);
if (err != IdeviceSuccess) {
fprintf(stderr, "Failed to start heartbeat service: %d\n", err);
} else {
printf("\nStarted heartbeat service on port %d (SSL: %s)\n", port,
ssl ? "true" : "false");
}
// Cleanup
lockdownd_client_free(client);
tcp_provider_free(provider);
return 0;
}

View File

@@ -6,6 +6,7 @@ pub mod debug_proxy;
mod errors; mod errors;
pub mod heartbeat; pub mod heartbeat;
pub mod installation_proxy; pub mod installation_proxy;
pub mod lockdownd;
pub mod logging; pub mod logging;
pub mod mounter; pub mod mounter;
mod pairing_file; mod pairing_file;

310
ffi/src/lockdownd.rs Normal file
View File

@@ -0,0 +1,310 @@
// Jackson Coxson
use std::ffi::c_void;
use idevice::{IdeviceError, IdeviceService, lockdownd::LockdowndClient};
use crate::{
IdeviceErrorCode, IdeviceHandle, IdevicePairingFile, RUNTIME,
provider::{TcpProviderHandle, UsbmuxdProviderHandle},
};
pub struct LockdowndClientHandle(pub LockdowndClient);
/// Connects to lockdownd service using TCP provider
///
/// # Arguments
/// * [`provider`] - A TcpProvider
/// * [`client`] - On success, will be set to point to a newly allocated LockdowndClient handle
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `provider` must be a valid pointer to a handle allocated by this library
/// `client` must be a valid, non-null pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lockdownd_connect_tcp(
provider: *mut TcpProviderHandle,
client: *mut *mut LockdowndClientHandle,
) -> IdeviceErrorCode {
if provider.is_null() || client.is_null() {
log::error!("Null pointer provided");
return IdeviceErrorCode::InvalidArg;
}
let res: Result<LockdowndClient, IdeviceError> = RUNTIME.block_on(async move {
let provider_box = unsafe { Box::from_raw(provider) };
let provider_ref = &provider_box.0;
let result = LockdowndClient::connect(provider_ref).await;
std::mem::forget(provider_box);
result
});
match res {
Ok(r) => {
let boxed = Box::new(LockdowndClientHandle(r));
unsafe { *client = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => {
let _ = unsafe { Box::from_raw(provider) };
e.into()
}
}
}
/// Connects to lockdownd service using Usbmuxd provider
///
/// # Arguments
/// * [`provider`] - A UsbmuxdProvider
/// * [`client`] - On success, will be set to point to a newly allocated LockdowndClient handle
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `provider` must be a valid pointer to a handle allocated by this library
/// `client` must be a valid, non-null pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lockdownd_connect_usbmuxd(
provider: *mut UsbmuxdProviderHandle,
client: *mut *mut LockdowndClientHandle,
) -> IdeviceErrorCode {
if provider.is_null() || client.is_null() {
log::error!("Null pointer provided");
return IdeviceErrorCode::InvalidArg;
}
let res: Result<LockdowndClient, IdeviceError> = RUNTIME.block_on(async move {
let provider_box = unsafe { Box::from_raw(provider) };
let provider_ref = &provider_box.0;
let result = LockdowndClient::connect(provider_ref).await;
std::mem::forget(provider_box);
result
});
match res {
Ok(r) => {
let boxed = Box::new(LockdowndClientHandle(r));
unsafe { *client = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Creates a new LockdowndClient from an existing Idevice connection
///
/// # Arguments
/// * [`socket`] - An IdeviceSocket handle
/// * [`client`] - On success, will be set to point to a newly allocated LockdowndClient handle
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `socket` must be a valid pointer to a handle allocated by this library
/// `client` must be a valid, non-null pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lockdownd_new(
socket: *mut IdeviceHandle,
client: *mut *mut LockdowndClientHandle,
) -> IdeviceErrorCode {
if socket.is_null() || client.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let socket = unsafe { Box::from_raw(socket) }.0;
let r = LockdowndClient::new(socket);
let boxed = Box::new(LockdowndClientHandle(r));
unsafe { *client = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
/// Starts a session with lockdownd
///
/// # Arguments
/// * `client` - A valid LockdowndClient handle
/// * `pairing_file` - An IdevicePairingFile alocated by this library
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
/// `pairing_file` must be a valid plist_t containing a pairing file
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lockdownd_start_session(
client: *mut LockdowndClientHandle,
pairing_file: *mut IdevicePairingFile,
) -> IdeviceErrorCode {
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
let mut client_box = unsafe { Box::from_raw(client) };
let pairing_file = unsafe { Box::from_raw(pairing_file) };
let client_ref = &mut client_box.0;
let res = client_ref.start_session(&pairing_file.0).await;
std::mem::forget(client_box);
std::mem::forget(pairing_file);
res
});
match res {
Ok(_) => IdeviceErrorCode::IdeviceSuccess,
Err(e) => e.into(),
}
}
/// Starts a service through lockdownd
///
/// # Arguments
/// * `client` - A valid LockdowndClient handle
/// * `identifier` - The service identifier to start (null-terminated string)
/// * `port` - Pointer to store the returned port number
/// * `ssl` - Pointer to store whether SSL should be enabled
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
/// `identifier` must be a valid null-terminated string
/// `port` and `ssl` must be valid pointers
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lockdownd_start_service(
client: *mut LockdowndClientHandle,
identifier: *const libc::c_char,
port: *mut u16,
ssl: *mut bool,
) -> IdeviceErrorCode {
if identifier.is_null() || port.is_null() || ssl.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let identifier = unsafe { std::ffi::CStr::from_ptr(identifier) }
.to_string_lossy()
.into_owned();
let res: Result<(u16, bool), IdeviceError> = RUNTIME.block_on(async move {
let mut client_box = unsafe { Box::from_raw(client) };
let client_ref = &mut client_box.0;
let res = client_ref.start_service(identifier).await;
std::mem::forget(client_box);
res
});
match res {
Ok((p, s)) => {
unsafe {
*port = p;
*ssl = s;
}
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Gets a value from lockdownd
///
/// # Arguments
/// * `client` - A valid LockdowndClient handle
/// * `value` - The value to get (null-terminated string)
/// * `out_plist` - Pointer to store the returned plist value
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
/// `value` must be a valid null-terminated string
/// `out_plist` must be a valid pointer to store the plist
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lockdownd_get_value(
client: *mut LockdowndClientHandle,
value: *const libc::c_char,
out_plist: *mut *mut c_void,
) -> IdeviceErrorCode {
if value.is_null() || out_plist.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let value = unsafe { std::ffi::CStr::from_ptr(value) }
.to_string_lossy()
.into_owned();
let res: Result<plist::Value, IdeviceError> = RUNTIME.block_on(async move {
let mut client_box = unsafe { Box::from_raw(client) };
let client_ref = &mut client_box.0;
let res = client_ref.get_value(value).await;
std::mem::forget(client_box);
res
});
match res {
Ok(value) => {
unsafe {
*out_plist = crate::util::plist_to_libplist(&value);
}
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Gets all values from lockdownd
///
/// # Arguments
/// * `client` - A valid LockdowndClient handle
/// * `out_plist` - Pointer to store the returned plist dictionary
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
/// `out_plist` must be a valid pointer to store the plist
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lockdownd_get_all_values(
client: *mut LockdowndClientHandle,
out_plist: *mut *mut c_void,
) -> IdeviceErrorCode {
if out_plist.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let res: Result<plist::Dictionary, IdeviceError> = RUNTIME.block_on(async move {
let mut client_box = unsafe { Box::from_raw(client) };
let client_ref = &mut client_box.0;
let res = client_ref.get_all_values().await;
std::mem::forget(client_box);
res
});
match res {
Ok(dict) => {
unsafe {
*out_plist = crate::util::plist_to_libplist(&plist::Value::Dictionary(dict));
}
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Frees a LockdowndClient handle
///
/// # Arguments
/// * [`handle`] - The handle to free
///
/// # Safety
/// `handle` must be a valid pointer to the handle that was allocated by this library,
/// or NULL (in which case this function does nothing)
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lockdownd_client_free(handle: *mut LockdowndClientHandle) {
if !handle.is_null() {
log::debug!("Freeing lockdownd_client");
let _ = unsafe { Box::from_raw(handle) };
}
}