From 139306caebdcc9e4383795227ca3662d484e04d5 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Fri, 5 Nov 2010 20:39:07 +0100 Subject: Add new house_arrest service including small test tool --- dev/Makefile.am | 7 +- dev/housearresttest.c | 214 +++++++++++++++++++++++++++ include/Makefile.am | 1 + include/libimobiledevice/house_arrest.h | 64 ++++++++ src/Makefile.am | 1 + src/house_arrest.c | 250 ++++++++++++++++++++++++++++++++ src/house_arrest.h | 39 +++++ 7 files changed, 575 insertions(+), 1 deletion(-) create mode 100644 dev/housearresttest.c create mode 100644 include/libimobiledevice/house_arrest.h create mode 100644 src/house_arrest.c create mode 100644 src/house_arrest.h diff --git a/dev/Makefile.am b/dev/Makefile.am index 0790c80..6da20da 100644 --- a/dev/Makefile.am +++ b/dev/Makefile.am @@ -4,7 +4,7 @@ AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_C AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) if ENABLE_DEVTOOLS -noinst_PROGRAMS = ideviceclient lckd-client afccheck msyncclient filerelaytest +noinst_PROGRAMS = ideviceclient lckd-client afccheck msyncclient filerelaytest housearresttest ideviceclient_SOURCES = ideviceclient.c ideviceclient_LDADD = ../src/libimobiledevice.la @@ -29,6 +29,11 @@ filerelaytest_CFLAGS = $(AM_CFLAGS) filerelaytest_LDFLAGS = $(AM_LDFLAGS) filerelaytest_LDADD = ../src/libimobiledevice.la +housearresttest_SOURCES = housearresttest.c +housearresttest_CFLAGS = $(AM_CFLAGS) +housearresttest_LDFLAGS = $(AM_LDFLAGS) +housearresttest_LDADD = ../src/libimobiledevice.la + endif # ENABLE_DEVTOOLS EXTRA_DIST = ideviceclient.c lckdclient.c afccheck.c msyncclient.c diff --git a/dev/housearresttest.c b/dev/housearresttest.c new file mode 100644 index 0000000..7282a8c --- /dev/null +++ b/dev/housearresttest.c @@ -0,0 +1,214 @@ +/* + * housearresttest.c + * Simple Test program showing the usage of the house_arrest interface. + * + * Copyright (c) 2010 Nikias Bassen All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include +#include + +#include +#include +#include +#include + +static void print_usage(int argc, char **argv) +{ + char *name = NULL; + + name = strrchr(argv[0], '/'); + printf("Usage: %s [OPTIONS] APPID\n", (name ? name + 1: argv[0])); + printf("Test the house_arrest service.\n\n"); + printf(" -d, --debug\t\tenable communication debugging\n"); + printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); + printf(" -t, --test\t\ttest creating, writing, and deleting a file\n"); + printf(" -h, --help\t\tprints usage information\n"); + printf("\n"); +} + +int main(int argc, char **argv) +{ + idevice_t dev = NULL; + lockdownd_client_t client = NULL; + house_arrest_client_t hac = NULL; + house_arrest_error_t res; + int i; + char *uuid = NULL; + const char *appid = NULL; + int test_file_io = 0; + + /* parse cmdline args */ + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { + idevice_set_debug_level(1); + continue; + } + else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) { + i++; + if (!argv[i] || (strlen(argv[i]) != 40)) { + print_usage(argc, argv); + return 0; + } + uuid = strdup(argv[i]); + continue; + } + else if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--test")) { + test_file_io = 1; + continue; + } + else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_usage(argc, argv); + return 0; + } + else { + appid = argv[i]; + break; + } + } + + if (!appid) { + print_usage(argc, argv); + return 0; + } + + if (idevice_new(&dev, uuid) != IDEVICE_E_SUCCESS) { + printf("no device connected?!\n"); + goto leave_cleanup; + } + + if (lockdownd_client_new_with_handshake(dev, &client, NULL) != LOCKDOWN_E_SUCCESS) { + printf("could not connect to lockdownd!\n"); + goto leave_cleanup; + } + + uint16_t port = 0; + if (lockdownd_start_service(client, "com.apple.mobile.house_arrest", &port) != LOCKDOWN_E_SUCCESS) { + printf("could not start house_arrest service!\n"); + goto leave_cleanup; + } + + if (client) { + lockdownd_client_free(client); + client = NULL; + } + + if (house_arrest_client_new(dev, port, &hac) != HOUSE_ARREST_E_SUCCESS) { + printf("could not connect to house_arrest service!\n"); + goto leave_cleanup; + } + + res = house_arrest_send_command(hac, "VendDocuments", appid); + if (res != HOUSE_ARREST_E_SUCCESS) { + printf("error %d when trying to get VendDocuments\n", res); + goto leave_cleanup; + } + + plist_t dict = NULL; + if (house_arrest_get_result(hac, &dict) != HOUSE_ARREST_E_SUCCESS) { + if (house_arrest_get_result(hac, &dict) != HOUSE_ARREST_E_SUCCESS) { + printf("hmmm....\n"); + goto leave_cleanup; + } + } + + plist_t node = plist_dict_get_item(dict, "Error"); + if (node) { + char *str = NULL; + plist_get_string_val(node, &str); + printf("Error: %s\n", str); + if (str) free(str); + plist_free(dict); + dict = NULL; + goto leave_cleanup; + } + node = plist_dict_get_item(dict, "Status"); + if (node) { + char *str = NULL; + plist_get_string_val(node, &str); + if (str && (strcmp(str, "Complete") != 0)) { + printf("Warning: Status is not 'Complete' but '%s'\n", str); + } + if (str) free(str); + plist_free(dict); + dict = NULL; + } + if (dict) { + plist_free(dict); + } + + afc_client_t afc = NULL; + afc_error_t ae = afc_client_new_from_house_arrest_client(hac, &afc); + if (ae != AFC_E_SUCCESS) { + printf("afc error %d\n", ae); + } + if (ae == AFC_E_SUCCESS) { + char **list = NULL; + afc_read_directory(afc, "/", &list); + printf("Directory contents:\n"); + if (list) { + while (list[0]) { + if (strcmp(list[0], ".") && strcmp(list[0], "..")) { + puts(list[0]); + } + list++; + } + } + + if (test_file_io) { + uint64_t tf = 0; + printf("\n==== Performing file tests ====\n"); + printf("Opening file 'foobar' for writing: "); + if (afc_file_open(afc, "/foobar", AFC_FOPEN_RW, &tf) == AFC_E_SUCCESS) { + uint32_t wb = 0; + printf("OK\n"); + + printf("Writing to file: "); + if (afc_file_write(afc, tf, "test\r\n", 6, &wb) != AFC_E_SUCCESS) { + printf("ERROR\n"); + } else { + printf("OK\n"); + } + afc_file_close(afc, tf); + printf("Deleting file 'foobar': "); + if (afc_remove_path(afc, "/foobar") == AFC_E_SUCCESS) { + printf("OK\n"); + } else { + printf("ERROR\n"); + } + } else { + printf("ERROR\n"); + } + } + afc_client_free(afc); + } else { + printf("failed to connect to afc service, error %d\n", ae); + } + +leave_cleanup: + if (hac) { + house_arrest_client_free(hac); + } + if (client) { + lockdownd_client_free(client); + } + if (dev) { + idevice_free(dev); + } + + return 0; +} diff --git a/include/Makefile.am b/include/Makefile.am index 12f3188..44794c6 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -9,4 +9,5 @@ nobase_include_HEADERS = libimobiledevice/libimobiledevice.h \ libimobiledevice/screenshotr.h \ libimobiledevice/mobilesync.h \ libimobiledevice/mobilebackup.h \ + libimobiledevice/house_arrest.h \ libimobiledevice/restore.h diff --git a/include/libimobiledevice/house_arrest.h b/include/libimobiledevice/house_arrest.h new file mode 100644 index 0000000..04290f1 --- /dev/null +++ b/include/libimobiledevice/house_arrest.h @@ -0,0 +1,64 @@ +/** + * @file libimobiledevice/house_arrest.h + * @brief Access AppStore application folders and their contents. + * \internal + * + * Copyright (c) 2010 Nikias Bassen, All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef HOUSE_ARREST_H +#define HOUSE_ARREST_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +/** @name Error Codes */ +/*@{*/ +#define HOUSE_ARREST_E_SUCCESS 0 +#define HOUSE_ARREST_E_INVALID_ARG -1 +#define HOUSE_ARREST_E_PLIST_ERROR -2 +#define HOUSE_ARREST_E_CONN_FAILED -3 +#define HOUSE_ARREST_E_INVALID_MODE -4 + +#define HOUSE_ARREST_E_UNKNOWN_ERROR -256 +/*@}*/ + +/** Represents an error code. */ +typedef int16_t house_arrest_error_t; + +typedef struct house_arrest_client_private house_arrest_client_private; +typedef house_arrest_client_private *house_arrest_client_t; /**< The client handle. */ + +/* Interface */ +house_arrest_error_t house_arrest_client_new(idevice_t device, uint16_t port, house_arrest_client_t *client); +house_arrest_error_t house_arrest_client_free(house_arrest_client_t client); + +house_arrest_error_t house_arrest_send_request(house_arrest_client_t client, plist_t dict); +house_arrest_error_t house_arrest_send_command(house_arrest_client_t client, const char *command, const char *appid); +house_arrest_error_t house_arrest_get_result(house_arrest_client_t client, plist_t *dict); + +afc_error_t afc_client_new_from_house_arrest_client(house_arrest_client_t client, afc_client_t *afc_client); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/Makefile.am b/src/Makefile.am index 338daf2..70dc895 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,4 +20,5 @@ libimobiledevice_la_SOURCES = idevice.c idevice.h \ screenshotr.c screenshotr.h\ mobilesync.c mobilesync.h\ mobilebackup.c mobilebackup.h\ + house_arrest.c house_arrest.h\ restore.c restore.h diff --git a/src/house_arrest.c b/src/house_arrest.c new file mode 100644 index 0000000..5baa76e --- /dev/null +++ b/src/house_arrest.c @@ -0,0 +1,250 @@ +/* + * house_arrest.c + * com.apple.mobile.house_arrest service implementation. + * + * Copyright (c) 2010 Nikias Bassen, All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include + +#include "house_arrest.h" +#include "property_list_service.h" +#include "afc.h" +#include "debug.h" + +/** + * Convert a property_list_service_error_t value to a house_arrest_error_t + * value. Used internally to get correct error codes. + * + * @param err A property_list_service_error_t error code + * + * @return A matching house_arrest_error_t error code, + * HOUSE_ARREST_E_UNKNOWN_ERROR otherwise. + */ +static house_arrest_error_t house_arrest_error(property_list_service_error_t err) +{ + switch (err) { + case PROPERTY_LIST_SERVICE_E_SUCCESS: + return HOUSE_ARREST_E_SUCCESS; + case PROPERTY_LIST_SERVICE_E_INVALID_ARG: + return HOUSE_ARREST_E_INVALID_ARG; + case PROPERTY_LIST_SERVICE_E_PLIST_ERROR: + return HOUSE_ARREST_E_PLIST_ERROR; + case PROPERTY_LIST_SERVICE_E_MUX_ERROR: + return HOUSE_ARREST_E_CONN_FAILED; + default: + break; + } + return HOUSE_ARREST_E_UNKNOWN_ERROR; +} + +/** + * Connects to the house_arrest service on the specified device. + * + * @param device The device to connect to. + * @param port Destination port (usually given by lockdownd_start_service). + * @param client Pointer that will point to a newly allocated + * housearrest_client_t upon successful return. + * + * @return HOUSE_ARREST_E_SUCCESS on success, HOUSE_ARREST_E_INVALID_ARG when + * client is NULL, or an HOUSE_ARREST_E_* error code otherwise. + */ +house_arrest_error_t house_arrest_client_new(idevice_t device, uint16_t port, house_arrest_client_t *client) +{ + if (!device) + return HOUSE_ARREST_E_INVALID_ARG; + + property_list_service_client_t plistclient = NULL; + house_arrest_error_t err = house_arrest_error(property_list_service_client_new(device, port, &plistclient)); + if (err != HOUSE_ARREST_E_SUCCESS) { + return err; + } + + house_arrest_client_t client_loc = (house_arrest_client_t) malloc(sizeof(struct house_arrest_client_private)); + client_loc->parent = plistclient; + client_loc->mode = HOUSE_ARREST_CLIENT_MODE_NORMAL; + + *client = client_loc; + return HOUSE_ARREST_E_SUCCESS; +} + +/** + * Disconnects an house_arrest client from the device and frees up the + * house_arrest client data. + * + * @note After using afc_client_new_from_house_arrest_client(), make sure + * you call afc_client_free() before calling this function to ensure + * a proper cleanup. Do not call this function if you still need to + * perform AFC operations since it will close the connection. + * + * @param client The house_arrest client to disconnect and free. + * + * @return HOUSE_ARREST_E_SUCCESS on success, HOUSE_ARREST_E_INVALID_ARG when + * client is NULL, or an HOUSE_ARREST_E_* error code otherwise. + */ +house_arrest_error_t house_arrest_client_free(house_arrest_client_t client) +{ + if (!client) + return HOUSE_ARREST_E_INVALID_ARG; + + house_arrest_error_t err = HOUSE_ARREST_E_SUCCESS; + if (client->parent && client->parent->connection) { + house_arrest_error(property_list_service_client_free(client->parent)); + } + client->parent = NULL; + free(client); + + return err; +} + +/** + * Sends a generic request to the connected house_arrest service. + * + * @param client The house_arrest client to use. + * @param dict The request to send as a plist of type PLIST_DICT. + * + * @note If this function returns HOUSE_ARREST_E_SUCCESS it does not mean + * that the request was successful. To check for success or failure you + * need to call house_arrest_get_result(). + * @see house_arrest_get_result + * + * @return HOUSE_ARREST_E_SUCCESS if the request was successfully sent, + * HOUSE_ARREST_E_INVALID_ARG if client or dict is invalid, + * HOUSE_ARREST_E_PLIST_ERROR if dict is not a plist of type PLIST_DICT, + * HOUSE_ARREST_E_INVALID_MODE if the client is not in the correct mode, + * or HOUSE_ARREST_E_CONN_FAILED if a connection error occured. + */ +house_arrest_error_t house_arrest_send_request(house_arrest_client_t client, plist_t dict) +{ + if (!client || !client->parent || !dict) + return HOUSE_ARREST_E_INVALID_ARG; + if (plist_get_node_type(dict) != PLIST_DICT) + return HOUSE_ARREST_E_PLIST_ERROR; + if (client->mode != HOUSE_ARREST_CLIENT_MODE_NORMAL) + return HOUSE_ARREST_E_INVALID_MODE; + + house_arrest_error_t res = house_arrest_error(property_list_service_send_xml_plist(client->parent, dict)); + if (res != HOUSE_ARREST_E_SUCCESS) { + debug_info("could not send plist, error %d", res); + } + return res; +} + +/** + * Send a command to the connected house_arrest service. + * Calls house_arrest_send_request() internally. + * + * @param client The house_arrest client to use. + * @param command The command to send. Currently, only VendContainer and + * VendDocuments are known. + * @param appid The application identifier to pass along with the . + * + * @note If this function returns HOUSE_ARREST_E_SUCCESS it does not mean + * that the command was successful. To check for success or failure you + * need to call house_arrest_get_result(). + * @see house_arrest_get_result + * + * @return HOUSE_ARREST_E_SUCCESS if the command was successfully sent, + * HOUSE_ARREST_E_INVALID_ARG if client, command, or appid is invalid, + * HOUSE_ARREST_E_INVALID_MODE if the client is not in the correct mode, + * or HOUSE_ARREST_E_CONN_FAILED if a connection error occured. + */ +house_arrest_error_t house_arrest_send_command(house_arrest_client_t client, const char *command, const char *appid) +{ + if (!client || !client->parent || !command || !appid) + return HOUSE_ARREST_E_INVALID_ARG; + if (client->mode != HOUSE_ARREST_CLIENT_MODE_NORMAL) + return HOUSE_ARREST_E_INVALID_MODE; + + house_arrest_error_t res = HOUSE_ARREST_E_UNKNOWN_ERROR; + + plist_t dict = plist_new_dict(); + plist_dict_insert_item(dict, "Command", plist_new_string(command)); + plist_dict_insert_item(dict, "Identifier", plist_new_string(appid)); + + res = house_arrest_send_request(client, dict); + + plist_free(dict); + + return res; +} + +/** + * Retrieves the result of a previously sent house_arrest_request_* request. + * + * @param client The house_arrest client to use + * @param dict Pointer that will be set to a plist containing the result to + * the last performed operation. It holds a key 'Status' with the value + * 'Complete' on success or a key 'Error' with an error description as + * value. The caller is responsible for freeing the returned plist. + * + * @return HOUSE_ARREST_E_SUCCESS if a result plist was retrieved, + * HOUSE_ARREST_E_INVALID_ARG if client is invalid, + * HOUSE_ARREST_E_INVALID_MODE if the client is not in the correct mode, + * or HOUSE_ARREST_E_CONN_FAILED if a connection error occured. + */ +house_arrest_error_t house_arrest_get_result(house_arrest_client_t client, plist_t *dict) +{ + if (!client || !client->parent) + return HOUSE_ARREST_E_INVALID_ARG; + if (client->mode != HOUSE_ARREST_CLIENT_MODE_NORMAL) + return HOUSE_ARREST_E_INVALID_MODE; + + house_arrest_error_t res = house_arrest_error(property_list_service_receive_plist(client->parent, dict)); + if (res != HOUSE_ARREST_E_SUCCESS) { + debug_info("could not get result, error %d", res); + if (*dict) { + plist_free(*dict); + *dict = NULL; + } + } + return res; +} + +/** + * Creates an AFC client using the given house_arrest client's connection + * allowing file access to a specific application directory requested by + * functions like house_arrest_request_vendor_documents(). + * + * @param client The house_arrest client to use. + * @param afc_client Pointer that will be set to a newly allocated afc_client_t + * upon successful return. + * + * @note After calling this function the house_arrest client will go in an + * AFC mode that will only allow calling house_arrest_client_free(). + * Only call house_arrest_client_free() if all AFC operations have + * completed since it will close the connection. + * + * @return AFC_E_SUCCESS if the afc client was successfully created, + * AFC_E_INVALID_ARG if client is invalid or was already used to create + * an afc client, or an AFC_E_* error code returned by + * afc_client_new_from_connection(). + */ +afc_error_t afc_client_new_from_house_arrest_client(house_arrest_client_t client, afc_client_t *afc_client) +{ + if (!client || !client->parent || (client->mode == HOUSE_ARREST_CLIENT_MODE_AFC)) { + return AFC_E_INVALID_ARG; + } + afc_error_t err = afc_client_new_from_connection(client->parent->connection, afc_client); + if (err == AFC_E_SUCCESS) { + client->mode = HOUSE_ARREST_CLIENT_MODE_AFC; + } + return err; +} diff --git a/src/house_arrest.h b/src/house_arrest.h new file mode 100644 index 0000000..6d13a88 --- /dev/null +++ b/src/house_arrest.h @@ -0,0 +1,39 @@ +/* + * house_arrest.h + * com.apple.mobile.house_arrest service header file. + * + * Copyright (c) 2010 Nikias Bassen, All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef IHOUSE_ARREST_H +#define IHOUSE_ARREST_H + +#include + +#include "libimobiledevice/house_arrest.h" +#include "property_list_service.h" + +enum house_arrest_client_mode { + HOUSE_ARREST_CLIENT_MODE_NORMAL = 0, + HOUSE_ARREST_CLIENT_MODE_AFC, +}; + +struct house_arrest_client_private { + property_list_service_client_t parent; + enum house_arrest_client_mode mode; +}; + +#endif -- cgit v1.1-32-gdbae