From f2bfe4d771ecdc56eadb46020348a915598b47ba Mon Sep 17 00:00:00 2001 From: tr4m0ryp Date: Sun, 5 Apr 2026 01:58:15 +0200 Subject: [PATCH] Task 006: Bypass registry and AFC utils (carried from research_icloud0) Ported bypass_module_t plugin interface and AFC file operation wrappers from research_icloud0/src/ with updated include paths for new layout. All function signatures preserved exactly. Files created: - include/bypass/bypass.h, src/bypass/bypass.c - include/bypass/afc_utils.h, src/bypass/afc_utils.c Co-Authored-By: Claude Opus 4.6 (1M context) --- include/bypass/afc_utils.h | 62 +++++++++++++ include/bypass/bypass.h | 63 +++++++++++++ src/bypass/afc_utils.c | 182 +++++++++++++++++++++++++++++++++++++ src/bypass/bypass.c | 79 ++++++++++++++++ 4 files changed, 386 insertions(+) create mode 100644 include/bypass/afc_utils.h create mode 100644 include/bypass/bypass.h create mode 100644 src/bypass/afc_utils.c create mode 100644 src/bypass/bypass.c diff --git a/include/bypass/afc_utils.h b/include/bypass/afc_utils.h new file mode 100644 index 0000000..169e4a6 --- /dev/null +++ b/include/bypass/afc_utils.h @@ -0,0 +1,62 @@ +/* + * afc_utils.h -- AFC file operations wrapper. + * + * Provides convenience functions over the libimobiledevice AFC API for + * reading, writing, listing, and checking files on the device filesystem. + * AFC root maps to /var/mobile/Media/ on the device. + */ + +#ifndef AFC_UTILS_H +#define AFC_UTILS_H + +#include +#include +#include +#include "device/device.h" + +/* + * afc_connect -- Start the AFC service and return a client handle. + * Caller must call afc_disconnect() when done. + * Returns 0 on success, -1 on error. + */ +int afc_connect(device_info_t *dev, afc_client_t *client); + +/* + * afc_disconnect -- Free the AFC client handle. + */ +void afc_disconnect(afc_client_t client); + +/* + * afc_write_file -- Write data to a file on the device. + * Creates or overwrites the file at the given AFC path. + * Returns bytes written on success, -1 on error. + */ +int afc_write_file(afc_client_t client, const char *path, + const char *data, uint32_t len); + +/* + * afc_read_file -- Read a file from the device into buf. + * Reads up to max_len bytes. Returns bytes read on success, -1 on error. + */ +int afc_read_file(afc_client_t client, const char *path, + char *buf, uint32_t max_len); + +/* + * afc_list_dir -- Print directory listing to stdout. + * Returns number of entries on success, -1 on error. + */ +int afc_list_dir(afc_client_t client, const char *path); + +/* + * afc_file_exists -- Check if a file or directory exists. + * Returns 1 if exists, 0 if not, -1 on error. + */ +int afc_file_exists(afc_client_t client, const char *path); + +/* + * afc_remove_file -- Remove a file at the given path. + * Returns 0 on success, -1 on error. + */ +int afc_remove_file(afc_client_t client, const char *path); + +#endif /* AFC_UTILS_H */ diff --git a/include/bypass/bypass.h b/include/bypass/bypass.h new file mode 100644 index 0000000..2453d35 --- /dev/null +++ b/include/bypass/bypass.h @@ -0,0 +1,63 @@ +/* + * bypass.h -- Bypass module interface. + * + * Defines a function-pointer-based plugin architecture for bypass strategies. + * Each module implements probe() to check compatibility and execute() to run. + * Modules are registered at startup and selected based on device properties. + */ + +#ifndef BYPASS_H +#define BYPASS_H + +#include "device/device.h" + +#define BYPASS_MAX_MODULES 16 +#define BYPASS_NAME_LEN 64 +#define BYPASS_DESC_LEN 256 + +typedef struct bypass_module { + char name[BYPASS_NAME_LEN]; + char description[BYPASS_DESC_LEN]; + + /* + * probe -- Check if this module can handle the given device. + * Returns 1 if compatible, 0 if not, -1 on error. + */ + int (*probe)(device_info_t *dev); + + /* + * execute -- Run the bypass on the given device. + * Returns 0 on success, non-zero on failure. + */ + int (*execute)(device_info_t *dev); +} bypass_module_t; + +/* + * bypass_register -- Register a bypass module. + * Returns 0 on success, -1 if the registry is full. + */ +int bypass_register(const bypass_module_t *mod); + +/* + * bypass_select -- Find the first registered module whose probe() returns 1. + * Returns a pointer to the module, or NULL if none match. + */ +const bypass_module_t *bypass_select(device_info_t *dev); + +/* + * bypass_execute -- Run the given module's execute() function. + * Returns the module's return code, or -1 if mod is NULL. + */ +int bypass_execute(const bypass_module_t *mod, device_info_t *dev); + +/* + * bypass_list -- Print all registered modules to stdout. + */ +void bypass_list(void); + +/* + * bypass_count -- Return the number of registered modules. + */ +int bypass_count(void); + +#endif /* BYPASS_H */ diff --git a/src/bypass/afc_utils.c b/src/bypass/afc_utils.c new file mode 100644 index 0000000..a28d4a5 --- /dev/null +++ b/src/bypass/afc_utils.c @@ -0,0 +1,182 @@ +/* + * afc_utils.c -- AFC file operations wrapper. + * + * Convenience layer over the libimobiledevice AFC API. AFC root on the + * device maps to /var/mobile/Media/. + */ + +#include +#include +#include +#include "bypass/afc_utils.h" +#include "util/log.h" + +int afc_connect(device_info_t *dev, afc_client_t *client) +{ + lockdownd_service_descriptor_t svc = NULL; + lockdownd_error_t lerr; + afc_error_t aerr; + + if (!dev || !dev->lockdown || !client) { + log_error("[afc] Invalid arguments to afc_connect"); + return -1; + } + + lerr = lockdownd_start_service(dev->lockdown, AFC_SERVICE_NAME, &svc); + if (lerr != LOCKDOWN_E_SUCCESS || !svc) { + log_error("[afc] Could not start AFC service: %s", + lockdownd_strerror(lerr)); + return -1; + } + + aerr = afc_client_new(dev->handle, svc, client); + lockdownd_service_descriptor_free(svc); + + if (aerr != AFC_E_SUCCESS) { + log_error("[afc] afc_client_new failed: %s", afc_strerror(aerr)); + return -1; + } + + return 0; +} + +void afc_disconnect(afc_client_t client) +{ + if (client) { + afc_client_free(client); + } +} + +int afc_write_file(afc_client_t client, const char *path, + const char *data, uint32_t len) +{ + uint64_t handle = 0; + uint32_t written = 0; + afc_error_t err; + + if (!client || !path || !data) { + log_error("[afc] Invalid arguments to afc_write_file"); + return -1; + } + + err = afc_file_open(client, path, AFC_FOPEN_WR, &handle); + if (err != AFC_E_SUCCESS) { + log_error("[afc] Cannot open '%s' for writing: %s", + path, afc_strerror(err)); + return -1; + } + + err = afc_file_write(client, handle, data, len, &written); + afc_file_close(client, handle); + + if (err != AFC_E_SUCCESS) { + log_error("[afc] Write to '%s' failed: %s", path, afc_strerror(err)); + return -1; + } + + return (int)written; +} + +int afc_read_file(afc_client_t client, const char *path, + char *buf, uint32_t max_len) +{ + uint64_t handle = 0; + uint32_t total_read = 0; + afc_error_t err; + + if (!client || !path || !buf || max_len == 0) { + log_error("[afc] Invalid arguments to afc_read_file"); + return -1; + } + + err = afc_file_open(client, path, AFC_FOPEN_RDONLY, &handle); + if (err != AFC_E_SUCCESS) { + log_error("[afc] Cannot open '%s' for reading: %s", + path, afc_strerror(err)); + return -1; + } + + /* Read in chunks until EOF or buffer full */ + while (total_read < max_len) { + uint32_t bytes_read = 0; + uint32_t chunk = max_len - total_read; + if (chunk > 65536) chunk = 65536; + + err = afc_file_read(client, handle, buf + total_read, + chunk, &bytes_read); + if (err != AFC_E_SUCCESS || bytes_read == 0) + break; + total_read += bytes_read; + } + + afc_file_close(client, handle); + return (int)total_read; +} + +int afc_list_dir(afc_client_t client, const char *path) +{ + char **entries = NULL; + afc_error_t err; + int count = 0; + + if (!client || !path) { + log_error("[afc] Invalid arguments to afc_list_dir"); + return -1; + } + + err = afc_read_directory(client, path, &entries); + if (err != AFC_E_SUCCESS) { + log_error("[afc] Cannot list '%s': %s", path, afc_strerror(err)); + return -1; + } + + log_info("[afc] Directory listing of '%s':", path); + for (int i = 0; entries[i] != NULL; i++) { + /* Skip . and .. */ + if (strcmp(entries[i], ".") == 0 || strcmp(entries[i], "..") == 0) + continue; + log_info(" %s", entries[i]); + count++; + } + log_info("[afc] %d entries", count); + + afc_dictionary_free(entries); + return count; +} + +int afc_file_exists(afc_client_t client, const char *path) +{ + char **info = NULL; + afc_error_t err; + + if (!client || !path) return -1; + + err = afc_get_file_info(client, path, &info); + if (err == AFC_E_SUCCESS && info) { + afc_dictionary_free(info); + return 1; + } + + if (err == AFC_E_OBJECT_NOT_FOUND) + return 0; + + return -1; +} + +int afc_remove_file(afc_client_t client, const char *path) +{ + afc_error_t err; + + if (!client || !path) { + log_error("[afc] Invalid arguments to afc_remove_file"); + return -1; + } + + err = afc_remove_path(client, path); + if (err != AFC_E_SUCCESS) { + log_error("[afc] Cannot remove '%s': %s", path, afc_strerror(err)); + return -1; + } + + return 0; +} diff --git a/src/bypass/bypass.c b/src/bypass/bypass.c new file mode 100644 index 0000000..ff27fbd --- /dev/null +++ b/src/bypass/bypass.c @@ -0,0 +1,79 @@ +/* + * bypass.c -- Bypass module registry implementation. + * + * Static array of registered modules. Selection iterates the list and + * returns the first module whose probe() returns 1. + */ + +#include +#include "bypass/bypass.h" +#include "util/log.h" + +static bypass_module_t g_modules[BYPASS_MAX_MODULES]; +static int g_module_count = 0; + +int bypass_register(const bypass_module_t *mod) +{ + if (!mod || !mod->probe || !mod->execute) { + log_error("[bypass] Invalid module (missing callbacks)"); + return -1; + } + + if (g_module_count >= BYPASS_MAX_MODULES) { + log_error("[bypass] Module registry full (%d max)", BYPASS_MAX_MODULES); + return -1; + } + + memcpy(&g_modules[g_module_count], mod, sizeof(bypass_module_t)); + g_module_count++; + + log_info("[bypass] Registered module: %s", mod->name); + return 0; +} + +const bypass_module_t *bypass_select(device_info_t *dev) +{ + if (!dev) return NULL; + + for (int i = 0; i < g_module_count; i++) { + int result = g_modules[i].probe(dev); + if (result == 1) { + log_info("[bypass] Selected module: %s", g_modules[i].name); + return &g_modules[i]; + } + if (result < 0) { + log_warn("[bypass] probe() failed for '%s'", g_modules[i].name); + } + } + + return NULL; +} + +int bypass_execute(const bypass_module_t *mod, device_info_t *dev) +{ + if (!mod) { + log_error("[bypass] No module selected"); + return -1; + } + if (!dev) { + log_error("[bypass] No device"); + return -1; + } + + log_info("[bypass] Executing module: %s", mod->name); + return mod->execute(dev); +} + +void bypass_list(void) +{ + log_info("[bypass] %d registered module(s):", g_module_count); + for (int i = 0; i < g_module_count; i++) { + log_info(" [%d] %s -- %s", i, g_modules[i].name, + g_modules[i].description); + } +} + +int bypass_count(void) +{ + return g_module_count; +}