From 96101a1231a4ddfeb40fd738a24e108a3a904048 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Thu, 28 Jan 2010 22:18:41 +0100 Subject: Global renames due to project rename to libimobiledevice --- tools/Makefile.am | 34 +- tools/idevice_id.c | 107 ++++++ tools/idevicebackup.c | 953 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/ideviceinfo.c | 336 ++++++++++++++++++ tools/idevicesyslog.c | 165 +++++++++ tools/iphone_id.c | 107 ------ tools/iphonebackup.c | 953 -------------------------------------------------- tools/iphoneinfo.c | 336 ------------------ tools/iphonesyslog.c | 165 --------- 9 files changed, 1578 insertions(+), 1578 deletions(-) create mode 100644 tools/idevice_id.c create mode 100644 tools/idevicebackup.c create mode 100644 tools/ideviceinfo.c create mode 100644 tools/idevicesyslog.c delete mode 100644 tools/iphone_id.c delete mode 100644 tools/iphonebackup.c delete mode 100644 tools/iphoneinfo.c delete mode 100644 tools/iphonesyslog.c (limited to 'tools') diff --git a/tools/Makefile.am b/tools/Makefile.am index d19ef0c..9b6a6e8 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -3,25 +3,25 @@ INCLUDES = -I$(top_srcdir)/include AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS) AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) -bin_PROGRAMS = iphone_id iphoneinfo iphonesyslog iphonebackup +bin_PROGRAMS = idevice_id ideviceinfo idevicesyslog idevicebackup -iphoneinfo_SOURCES = iphoneinfo.c -iphoneinfo_CFLAGS = $(AM_CFLAGS) -iphoneinfo_LDFLAGS = $(AM_LDFLAGS) -iphoneinfo_LDADD = ../src/libiphone.la +ideviceinfo_SOURCES = ideviceinfo.c +ideviceinfo_CFLAGS = $(AM_CFLAGS) +ideviceinfo_LDFLAGS = $(AM_LDFLAGS) +ideviceinfo_LDADD = ../src/libimobiledevice.la -iphonesyslog_SOURCES = iphonesyslog.c -iphonesyslog_CFLAGS = $(AM_CFLAGS) -iphonesyslog_LDFLAGS = $(AM_LDFLAGS) -iphonesyslog_LDADD = ../src/libiphone.la +idevicesyslog_SOURCES = idevicesyslog.c +idevicesyslog_CFLAGS = $(AM_CFLAGS) +idevicesyslog_LDFLAGS = $(AM_LDFLAGS) +idevicesyslog_LDADD = ../src/libimobiledevice.la -iphone_id_SOURCES = iphone_id.c -iphone_id_CFLAGS = $(AM_CFLAGS) -iphone_id_LDFLAGS = $(AM_LDFLAGS) -iphone_id_LDADD = ../src/libiphone.la +idevice_id_SOURCES = idevice_id.c +idevice_id_CFLAGS = $(AM_CFLAGS) +idevice_id_LDFLAGS = $(AM_LDFLAGS) +idevice_id_LDADD = ../src/libimobiledevice.la -iphonebackup_SOURCES = iphonebackup.c -iphonebackup_CFLAGS = $(AM_CFLAGS) -iphonebackup_LDFLAGS = $(AM_LDFLAGS) -iphonebackup_LDADD = ../src/libiphone.la +idevicebackup_SOURCES = idevicebackup.c +idevicebackup_CFLAGS = $(AM_CFLAGS) +idevicebackup_LDFLAGS = $(AM_LDFLAGS) +idevicebackup_LDADD = ../src/libimobiledevice.la diff --git a/tools/idevice_id.c b/tools/idevice_id.c new file mode 100644 index 0000000..1facb60 --- /dev/null +++ b/tools/idevice_id.c @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include + +#define MODE_NONE 0 +#define MODE_SHOW_ID 1 +#define MODE_LIST_DEVICES 2 + +static void print_usage(int argc, char **argv) +{ + char *name = NULL; + + name = strrchr(argv[0], '/'); + printf("Usage: %s [OPTIONS] [UUID]\n", (name ? name + 1: argv[0])); + printf("Prints device name or a list of attached iPhone/iPod Touch devices.\n\n"); + printf(" The UUID is a 40-digit hexadecimal number of the device\n"); + printf(" for which the name should be retrieved.\n\n"); + printf(" -l, --list\t\tlist UUID of all attached devices\n"); + printf(" -d, --debug\t\tenable communication debugging\n"); + printf(" -h, --help\t\tprints usage information\n"); + printf("\n"); +} + +int main(int argc, char **argv) +{ + idevice_t phone = NULL; + lockdownd_client_t client = NULL; + char **dev_list = NULL; + char *devname = NULL; + int ret = 0; + int i; + int mode = MODE_SHOW_ID; + char uuid[41]; + uuid[0] = 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], "-l") || !strcmp(argv[i], "--list")) { + mode = MODE_LIST_DEVICES; + continue; + } + else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_usage(argc, argv); + return 0; + } + } + + /* check if uuid was passed */ + if (mode == MODE_SHOW_ID) { + i--; + if (!argv[i] || (strlen(argv[i]) != 40)) { + print_usage(argc, argv); + return 0; + } + strcpy(uuid, argv[i]); + } + + switch (mode) { + case MODE_SHOW_ID: + idevice_new(&phone, uuid); + if (!phone) { + fprintf(stderr, "ERROR: No device with UUID=%s attached.\n", uuid); + return -2; + } + + if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client, "idevice_id")) { + idevice_free(phone); + fprintf(stderr, "ERROR: Connecting to device failed!\n"); + return -2; + } + + if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &devname)) || !devname) { + fprintf(stderr, "ERROR: Could not get device name!\n"); + ret = -2; + } + + lockdownd_client_free(client); + idevice_free(phone); + + if (ret == 0) { + printf("%s\n", devname); + } + + if (devname) { + free(devname); + } + + return ret; + case MODE_LIST_DEVICES: + default: + if (idevice_get_device_list(&dev_list, &i) < 0) { + fprintf(stderr, "ERROR: Unable to retrieve device list!\n"); + return -1; + } + for (i = 0; dev_list[i] != NULL; i++) { + printf("%s\n", dev_list[i]); + } + idevice_device_list_free(dev_list); + return 0; + } +} diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c new file mode 100644 index 0000000..d3b3ccc --- /dev/null +++ b/tools/idevicebackup.c @@ -0,0 +1,953 @@ +/* + * idevicebackup.c + * Command line interface to use the device's backup and restore service + * + * Copyright (c) 2009-2010 Martin Szulecki All Rights Reserved. + * 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 +#include +#include +#include +#include + +#define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup" +#define NP_SERVICE_NAME "com.apple.mobile.notification_proxy" + +static mobilebackup_client_t mobilebackup = NULL; +static lockdownd_client_t client = NULL; +static idevice_t phone = NULL; + +static int quit_flag = 0; + +enum cmd_mode { + CMD_BACKUP, + CMD_RESTORE, + CMD_LEAVE +}; + +enum plist_format_t { + PLIST_FORMAT_XML, + PLIST_FORMAT_BINARY +}; + +enum device_link_file_status_t { + DEVICE_LINK_FILE_STATUS_NONE = 0, + DEVICE_LINK_FILE_STATUS_HUNK, + DEVICE_LINK_FILE_STATUS_LAST_HUNK +}; + +static void notify_cb(const char *notification) +{ + if (!strcmp(notification, NP_SYNC_CANCEL_REQUEST)) { + printf("User has aborted on-device\n"); + quit_flag++; + } else { + printf("unhandled notification '%s' (TODO: implement)\n", notification); + } +} + +static plist_t mobilebackup_factory_info_plist_new() +{ + /* gather data from lockdown */ + GTimeVal tv = {0, 0}; + plist_t value_node = NULL; + plist_t root_node = NULL; + char *uuid = NULL; + char *uuid_uppercase = NULL; + + plist_t ret = plist_new_dict(); + + /* get basic device information in one go */ + lockdownd_get_value(client, NULL, NULL, &root_node); + + /* set fields we understand */ + value_node = plist_dict_get_item(root_node, "BuildVersion"); + plist_dict_insert_item(ret, "Build Version", plist_copy(value_node)); + + value_node = plist_dict_get_item(root_node, "DeviceName"); + plist_dict_insert_item(ret, "Device Name", plist_copy(value_node)); + plist_dict_insert_item(ret, "Display Name", plist_copy(value_node)); + + /* FIXME: How is the GUID generated? */ + plist_dict_insert_item(ret, "GUID", plist_new_string("---")); + + value_node = plist_dict_get_item(root_node, "InternationalMobileEquipmentIdentity"); + if (value_node) + plist_dict_insert_item(ret, "IMEI", plist_copy(value_node)); + + g_get_current_time(&tv); + plist_dict_insert_item(ret, "Last Backup Date", plist_new_date(tv.tv_sec, tv.tv_usec)); + + value_node = plist_dict_get_item(root_node, "ProductType"); + plist_dict_insert_item(ret, "Product Type", plist_copy(value_node)); + + value_node = plist_dict_get_item(root_node, "ProductVersion"); + plist_dict_insert_item(ret, "Product Version", plist_copy(value_node)); + + value_node = plist_dict_get_item(root_node, "SerialNumber"); + plist_dict_insert_item(ret, "Serial Number", plist_copy(value_node)); + + value_node = plist_dict_get_item(root_node, "UniqueDeviceID"); + idevice_get_uuid(phone, &uuid); + plist_dict_insert_item(ret, "Target Identifier", plist_new_string(uuid)); + + /* uppercase */ + uuid_uppercase = g_ascii_strup(uuid, -1); + plist_dict_insert_item(ret, "Unique Identifier", plist_new_string(uuid_uppercase)); + free(uuid_uppercase); + free(uuid); + + /* FIXME: Embed files as nodes */ + plist_t files = plist_new_dict(); + plist_dict_insert_item(ret, "iTunes Files", files); + plist_dict_insert_item(ret, "iTunes Version", plist_new_string("9.0.2")); + + plist_free(root_node); + + return ret; +} + +static void mobilebackup_info_update_last_backup_date(plist_t info_plist) +{ + GTimeVal tv = {0, 0}; + plist_t node = NULL; + + if (!info_plist) + return; + + g_get_current_time(&tv); + node = plist_dict_get_item(info_plist, "Last Backup Date"); + plist_set_date_val(node, tv.tv_sec, tv.tv_usec); + + node = NULL; +} + +static void buffer_read_from_filename(const char *filename, char **buffer, uint32_t *length) +{ + FILE *f; + uint64_t size; + + f = fopen(filename, "rb"); + + fseek(f, 0, SEEK_END); + size = ftell(f); + rewind(f); + + *buffer = (char*)malloc(sizeof(char)*size); + fread(*buffer, sizeof(char), size, f); + fclose(f); + + *length = size; +} + +static void buffer_write_to_filename(const char *filename, const char *buffer, uint32_t length) +{ + FILE *f; + + f = fopen(filename, "ab"); + fwrite(buffer, sizeof(char), length, f); + fclose(f); +} + +static int plist_read_from_filename(plist_t *plist, const char *filename) +{ + char *buffer = NULL; + uint32_t length; + + if (!filename) + return 0; + + buffer_read_from_filename(filename, &buffer, &length); + + if (!buffer) { + return 0; + } + + if (memcmp(buffer, "bplist00", 8) == 0) { + plist_from_bin(buffer, length, plist); + } else { + plist_from_xml(buffer, length, plist); + } + + free(buffer); + + return 1; +} + +static int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) +{ + char *buffer = NULL; + uint32_t length; + + if (!plist || !filename) + return 0; + + if (format == PLIST_FORMAT_XML) + plist_to_xml(plist, &buffer, &length); + else if (format == PLIST_FORMAT_BINARY) + plist_to_bin(plist, &buffer, &length); + else + return 0; + + buffer_write_to_filename(filename, buffer, length); + + free(buffer); + + return 1; +} + +static int plist_strcmp(plist_t node, const char *str) +{ + char *buffer = NULL; + int ret = 0; + + if (plist_get_node_type(node) != PLIST_STRING) + return ret; + + plist_get_string_val(node, &buffer); + ret = strcmp(buffer, str); + free(buffer); + + return ret; +} + +static plist_t device_link_message_factory_process_message_new(plist_t content) +{ + plist_t ret = plist_new_array(); + plist_array_append_item(ret, plist_new_string("DLMessageProcessMessage")); + plist_array_append_item(ret, content); + return ret; +} + +static void mobilebackup_cancel_backup_with_error(const char *reason) +{ + plist_t node = plist_new_dict(); + plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("BackupMessageError")); + plist_dict_insert_item(node, "BackupErrorReasonKey", plist_new_string(reason)); + + plist_t message = device_link_message_factory_process_message_new(node); + + mobilebackup_send(mobilebackup, message); + + plist_free(message); + message = NULL; +} + +static plist_t mobilebackup_factory_backup_file_received_new() +{ + plist_t node = plist_new_dict(); + plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("kBackupMessageBackupFileReceived")); + return device_link_message_factory_process_message_new(node); +} + +static gchar *mobilebackup_build_path(const char *backup_directory, const char *name, const char *extension) +{ + gchar *filename = g_strconcat(name, extension, NULL); + gchar *path = g_build_path(G_DIR_SEPARATOR_S, backup_directory, filename, NULL); + g_free(filename); + return path; +} + +static void mobilebackup_write_status(const char *path, int status) +{ + struct stat st; + plist_t status_plist = plist_new_dict(); + plist_dict_insert_item(status_plist, "Backup Success", plist_new_bool(status)); + gchar *file_path = mobilebackup_build_path(path, "Status", ".plist"); + + if (stat(file_path, &st) == 0) + remove(file_path); + + plist_write_to_filename(status_plist, file_path, PLIST_FORMAT_XML); + + plist_free(status_plist); + status_plist = NULL; + + g_free(file_path); +} + +static int mobilebackup_info_is_current_device(plist_t info) +{ + plist_t value_node = NULL; + plist_t node = NULL; + plist_t root_node = NULL; + int ret = 0; + + if (!info) + return ret; + + if (plist_get_node_type(info) != PLIST_DICT) + return ret; + + /* get basic device information in one go */ + lockdownd_get_value(client, NULL, NULL, &root_node); + + /* verify UUID */ + value_node = plist_dict_get_item(root_node, "UniqueDeviceID"); + node = plist_dict_get_item(info, "Target Identifier"); + + if(plist_compare_node_value(value_node, node)) + ret = 1; + else { + printf("Info.plist: UniqueDeviceID does not match.\n"); + } + + /* verify SerialNumber */ + if (ret == 1) { + value_node = plist_dict_get_item(root_node, "SerialNumber"); + node = plist_dict_get_item(info, "Serial Number"); + + if(plist_compare_node_value(value_node, node)) + ret = 1; + else { + printf("Info.plist: SerialNumber does not match.\n"); + ret = 0; + } + } + + /* verify ProductVersion to prevent using backup with different OS version */ + if (ret == 1) { + value_node = plist_dict_get_item(root_node, "ProductVersion"); + node = plist_dict_get_item(info, "Product Version"); + + if(plist_compare_node_value(value_node, node)) + ret = 1; + else { + printf("Info.plist: ProductVersion does not match.\n"); + ret = 0; + } + } + + plist_free(root_node); + root_node = NULL; + + value_node = NULL; + node = NULL; + + return ret; +} + +static int mobilebackup_delete_backup_file_by_hash(const char *backup_directory, const char *hash) +{ + int ret = 0; + gchar *path = mobilebackup_build_path(backup_directory, hash, ".mddata"); + printf("Removing \"%s\"... ", path); + if (!remove( path )) + ret = 1; + else + ret = 0; + + g_free(path); + + if (!ret) + return ret; + + path = mobilebackup_build_path(backup_directory, hash, ".mdinfo"); + printf("Removing \"%s\"... ", path); + if (!remove( path )) + ret = 1; + else + ret = 0; + + g_free(path); + + return ret; +} + +static void do_post_notification(const char *notification) +{ + uint16_t nport = 0; + np_client_t np; + + if (!client) { + if (lockdownd_client_new_with_handshake(phone, &client, "idevicebackup") != LOCKDOWN_E_SUCCESS) { + return; + } + } + + lockdownd_start_service(client, NP_SERVICE_NAME, &nport); + if (nport) { + np_client_new(phone, nport, &np); + if (np) { + np_post_notification(np, notification); + np_client_free(np); + } + } else { + printf("Could not start %s\n", NP_SERVICE_NAME); + } +} + +/** + * signal handler function for cleaning up properly + */ +static void clean_exit(int sig) +{ + fprintf(stderr, "Exiting...\n"); + quit_flag++; +} + +static void print_usage(int argc, char **argv) +{ + char *name = NULL; + name = strrchr(argv[0], '/'); + printf("Usage: %s [OPTIONS] CMD [DIRECTORY]\n", (name ? name + 1: argv[0])); + printf("Create or restore backup from the current or specified directory.\n\n"); + printf("commands:\n"); + printf(" backup\tSaves a device backup into DIRECTORY\n"); + printf(" restore\tRestores a device backup from DIRECTORY.\n\n"); + printf("options:\n"); + printf(" -d, --debug\t\tenable communication debugging\n"); + printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); + printf(" -h, --help\t\tprints usage information\n"); + printf("\n"); +} + +int main(int argc, char *argv[]) +{ + idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; + int i; + char uuid[41]; + uint16_t port = 0; + uuid[0] = 0; + int cmd = -1; + int is_full_backup = 0; + char *backup_directory = NULL; + struct stat st; + plist_t node = NULL; + plist_t node_tmp = NULL; + plist_t manifest_plist = NULL; + plist_t info_plist = NULL; + char *buffer = NULL; + uint64_t length = 0; + uint64_t backup_total_size = 0; + enum device_link_file_status_t file_status; + uint64_t c = 0; + + /* we need to exit cleanly on running backups and restores or we cause havok */ + signal(SIGINT, clean_exit); + signal(SIGQUIT, clean_exit); + signal(SIGTERM, clean_exit); + signal(SIGPIPE, SIG_IGN); + + /* 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; + } + strcpy(uuid, argv[i]); + continue; + } + else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_usage(argc, argv); + return 0; + } + else if (!strcmp(argv[i], "backup")) { + cmd = CMD_BACKUP; + } + else if (!strcmp(argv[i], "restore")) { + cmd = CMD_RESTORE; + } + else if (backup_directory == NULL) { + backup_directory = argv[i]; + } + else { + print_usage(argc, argv); + return 0; + } + } + + /* verify options */ + if (cmd == -1) { + printf("No command specified.\n"); + print_usage(argc, argv); + return -1; + } + + if (backup_directory == NULL) { + printf("No target backup directory specified.\n"); + print_usage(argc, argv); + return -1; + } + + /* verify if passed backup directory exists */ + if (stat(backup_directory, &st) != 0) { + printf("ERROR: Backup directory \"%s\" does not exist!\n", backup_directory); + return -1; + } + + /* restore directory must contain an Info.plist */ + char *info_path = mobilebackup_build_path(backup_directory, "Info", ".plist"); + if (cmd == CMD_RESTORE) { + if (stat(info_path, &st) != 0) { + g_free(info_path); + printf("ERROR: Backup directory \"%s\" is invalid. No Info.plist found.\n", backup_directory); + return -1; + } + } + + printf("Backup directory is \"%s\"\n", backup_directory); + + if (uuid[0] != 0) { + ret = idevice_new(&phone, uuid); + if (ret != IDEVICE_E_SUCCESS) { + printf("No device found with uuid %s, is it plugged in?\n", uuid); + return -1; + } + } + else + { + ret = idevice_new(&phone, NULL); + if (ret != IDEVICE_E_SUCCESS) { + printf("No device found, is it plugged in?\n"); + return -1; + } + } + + if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "idevicebackup")) { + idevice_free(phone); + return -1; + } + + /* start notification_proxy */ + np_client_t np = NULL; + ret = lockdownd_start_service(client, NP_SERVICE_NAME, &port); + if ((ret == LOCKDOWN_E_SUCCESS) && port) { + np_client_new(phone, port, &np); + np_set_notify_callback(np, notify_cb); + const char *noties[5] = { + NP_SYNC_CANCEL_REQUEST, + NP_SYNC_SUSPEND_REQUEST, + NP_SYNC_RESUME_REQUEST, + NP_BACKUP_DOMAIN_CHANGED, + NULL + }; + np_observe_notifications(np, noties); + } else { + printf("ERROR: Could not start service %s.\n", NP_SERVICE_NAME); + } + + /* start AFC, we need this for the lock file */ + afc_client_t afc = NULL; + port = 0; + ret = lockdownd_start_service(client, "com.apple.afc", &port); + if ((ret == LOCKDOWN_E_SUCCESS) && port) { + afc_client_new(phone, port, &afc); + } + + /* start syslog_relay service and retrieve port */ + port = 0; + ret = lockdownd_start_service(client, MOBILEBACKUP_SERVICE_NAME, &port); + if ((ret == LOCKDOWN_E_SUCCESS) && port) { + printf("Started \"%s\" service on port %d.\n", MOBILEBACKUP_SERVICE_NAME, port); + mobilebackup_client_new(phone, port, &mobilebackup); + + /* check abort conditions */ + if (quit_flag > 0) { + printf("Aborting backup. Cancelled by user.\n"); + cmd = CMD_LEAVE; + } + + /* verify existing Info.plist */ + if (stat(info_path, &st) == 0) { + printf("Reading Info.plist from backup.\n"); + plist_read_from_filename(&info_plist, info_path); + + if (cmd == CMD_BACKUP) { + if (mobilebackup_info_is_current_device(info_plist)) { + /* update the last backup time within Info.plist */ + mobilebackup_info_update_last_backup_date(info_plist); + remove(info_path); + plist_write_to_filename(info_plist, info_path, PLIST_FORMAT_XML); + } else { + printf("Aborting backup. Backup is not compatible with the current device.\n"); + cmd = CMD_LEAVE; + } + } + } else { + is_full_backup = 1; + } + + do_post_notification(NP_SYNC_WILL_START); + uint64_t lockfile = 0; + afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile); + if (lockfile) { + do_post_notification(NP_SYNC_LOCK_REQUEST); + if (afc_file_lock(afc, lockfile, AFC_LOCK_EX) == AFC_E_SUCCESS) { + do_post_notification(NP_SYNC_DID_START); + } else { + afc_file_close(afc, lockfile); + lockfile = 0; + } + } + + switch(cmd) { + case CMD_BACKUP: + printf("Starting backup...\n"); + /* TODO: check domain com.apple.mobile.backup key RequiresEncrypt and WillEncrypt with lockdown */ + /* TODO: verify battery on AC enough battery remaining */ + + /* Info.plist (Device infos, IC-Info.sidb, photos, app_ids, iTunesPrefs) */ + /* create new Info.plist on new backups */ + if (is_full_backup) { + printf("Creating Info.plist for new backup.\n"); + info_plist = mobilebackup_factory_info_plist_new(); + plist_write_to_filename(info_plist, info_path, PLIST_FORMAT_XML); + } + + g_free(info_path); + + /* Manifest.plist (backup manifest (backup state)) */ + char *manifest_path = mobilebackup_build_path(backup_directory, "Manifest", ".plist"); + + /* read the last Manifest.plist */ + if (!is_full_backup) { + printf("Reading existing Manifest.\n"); + plist_read_from_filename(&manifest_plist, manifest_path); + } + + plist_free(info_plist); + info_plist = NULL; + + /* close down the lockdown connection as it is no longer needed */ + if (client) { + lockdownd_client_free(client); + client = NULL; + } + + /* create Status.plist with failed status for now */ + mobilebackup_write_status(backup_directory, 0); + + /* request backup from device with manifest from last backup */ + printf("Requesting backup from device...\n"); + + node = plist_new_dict(); + + if (manifest_plist) + plist_dict_insert_item(node, "BackupManifestKey", manifest_plist); + + plist_dict_insert_item(node, "BackupComputerBasePathKey", plist_new_string("/")); + plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("BackupMessageBackupRequest")); + plist_dict_insert_item(node, "BackupProtocolVersion", plist_new_string("1.6")); + + plist_t message = device_link_message_factory_process_message_new(node); + mobilebackup_send(mobilebackup, message); + plist_free(message); + message = NULL; + node = NULL; + + /* get response */ + int backup_ok = 0; + mobilebackup_receive(mobilebackup, &message); + + node = plist_array_get_item(message, 0); + if (!plist_strcmp(node, "DLMessageProcessMessage")) { + node = plist_array_get_item(message, 1); + node = plist_dict_get_item(node, "BackupMessageTypeKey"); + if (node && !plist_strcmp(node, "BackupMessageBackupReplyOK")) { + printf("Device accepts manifest and will send backup data now...\n"); + backup_ok = 1; + printf("Acknowledging...\n"); + if (is_full_backup) + printf("Full backup mode.\n"); + else + printf("Incremental backup mode.\n"); + printf("Please wait. Device prepares backup data...\n"); + /* send it back for ACK */ + mobilebackup_send(mobilebackup, message); + } + } else { + printf("ERROR: Unhandled message received!\n"); + } + plist_free(message); + message = NULL; + + if (!backup_ok) { + printf("ERROR: Device rejected to start the backup process.\n"); + break; + } + + /* reset backup status */ + backup_ok = 0; + + /* receive and save DLSendFile files and metadata, ACK each */ + int file_index = 0; + int hunk_index = 0; + uint64_t backup_real_size = 0; + char *file_path = NULL; + char *file_ext = NULL; + char *filename_mdinfo = NULL; + char *filename_mddata = NULL; + char *filename_source = NULL; + char *format_size = NULL; + gboolean is_manifest = FALSE; + uint8_t b = 0; + + /* process series of DLSendFile messages */ + do { + mobilebackup_receive(mobilebackup, &message); + node = plist_array_get_item(message, 0); + + /* get out if we don't get a DLSendFile */ + if (plist_strcmp(node, "DLSendFile")) + break; + + node_tmp = plist_array_get_item(message, 2); + + /* first message hunk contains total backup size */ + if (hunk_index == 0) { + node = plist_dict_get_item(node_tmp, "BackupTotalSizeKey"); + if (node) { + plist_get_uint_val(node, &backup_total_size); + format_size = g_format_size_for_display(backup_total_size); + printf("Backup data requires %s on the disk.\n", format_size); + g_free(format_size); + } + } + + /* check DLFileStatusKey (codes: 1 = Hunk, 2 = Last Hunk) */ + node = plist_dict_get_item(node_tmp, "DLFileStatusKey"); + plist_get_uint_val(node, &c); + file_status = c; + + /* get source filename */ + node = plist_dict_get_item(node_tmp, "BackupManifestKey"); + b = 0; + if (node) { + plist_get_bool_val(node, &b); + } + is_manifest = (b == 1) ? TRUE: FALSE; + + /* check if we completed a file */ + if ((file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) && (!is_manifest)) { + /* get source filename */ + node = plist_dict_get_item(node_tmp, "DLFileSource"); + plist_get_string_val(node, &filename_source); + + /* increase received size */ + node = plist_dict_get_item(node_tmp, "DLFileAttributesKey"); + node = plist_dict_get_item(node, "FileSize"); + plist_get_uint_val(node, &length); + backup_real_size += length; + + format_size = g_format_size_for_display(backup_real_size); + printf("(%s", format_size); + g_free(format_size); + + format_size = g_format_size_for_display(backup_total_size); + printf("/%s): ", format_size); + g_free(format_size); + + printf("Received file %s... ", filename_source); + + if (filename_source) + free(filename_source); + + /* save .mdinfo */ + node = plist_dict_get_item(node_tmp, "BackupFileInfo"); + if (node) { + node = plist_dict_get_item(node_tmp, "DLFileDest"); + plist_get_string_val(node, &file_path); + + filename_mdinfo = mobilebackup_build_path(backup_directory, file_path, ".mdinfo"); + + /* remove any existing file */ + if (stat(filename_mdinfo, &st) != 0) + remove(filename_mdinfo); + + node = plist_dict_get_item(node_tmp, "BackupFileInfo"); + plist_write_to_filename(node, filename_mdinfo, PLIST_FORMAT_BINARY); + + g_free(filename_mdinfo); + } + + file_index++; + } + + /* save .mddata */ + node = plist_dict_get_item(node_tmp, "BackupFileInfo"); + if (node_tmp && file_path) { + node = plist_dict_get_item(node_tmp, "DLFileDest"); + plist_get_string_val(node, &file_path); + + filename_mddata = mobilebackup_build_path(backup_directory, file_path, is_manifest ? NULL: ".mddata"); + + /* if this is the first hunk, remove any existing file */ + if (stat(filename_mddata, &st) != 0) + remove(filename_mddata); + + /* get file data hunk */ + node_tmp = plist_array_get_item(message, 1); + plist_get_data_val(node_tmp, &buffer, &length); + + buffer_write_to_filename(filename_mddata, buffer, length); + + /* activate currently sent manifest */ + if ((file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) && (is_manifest)) { + rename(filename_mddata, manifest_path); + } + + free(buffer); + buffer = NULL; + + g_free(filename_mddata); + } + + hunk_index++; + + if (file_ext) + free(file_ext); + + if (message) + plist_free(message); + message = NULL; + + if (file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) { + if (!is_manifest) + printf("DONE\n"); + + /* acknowlegdge that we received the file */ + message = mobilebackup_factory_backup_file_received_new(); + mobilebackup_send(mobilebackup, message); + plist_free(message); + message = NULL; + } + + if (quit_flag > 0) { + /* need to cancel the backup here */ + mobilebackup_cancel_backup_with_error("Cancelling DLSendFile"); + + /* remove any atomic Manifest.plist.tmp */ + if (manifest_path) + g_free(manifest_path); + + manifest_path = mobilebackup_build_path(backup_directory, "Manifest", ".plist.tmp"); + if (stat(manifest_path, &st) == 0) + remove(manifest_path); + break; + } + } while (1); + + printf("Received %d files from device.\n", file_index); + + if (!quit_flag && !plist_strcmp(node, "DLMessageProcessMessage")) { + node_tmp = plist_array_get_item(message, 1); + node = plist_dict_get_item(node_tmp, "BackupMessageTypeKey"); + /* check if we received the final "backup finished" message */ + if (node && !plist_strcmp(node, "BackupMessageBackupFinished")) { + /* backup finished */ + + /* process BackupFilesToDeleteKey */ + node = plist_dict_get_item(node_tmp, "BackupFilesToDeleteKey"); + if (node) { + length = plist_array_get_size(node); + i = 0; + while ((node_tmp = plist_array_get_item(node, i++)) != NULL) { + plist_get_string_val(node_tmp, &file_path); + + if (mobilebackup_delete_backup_file_by_hash(backup_directory, file_path)) { + printf("DONE\n"); + } else + printf("FAILED\n"); + } + } + + /* save last valid Manifest.plist */ + node_tmp = plist_array_get_item(message, 1); + manifest_plist = plist_dict_get_item(node_tmp, "BackupManifestKey"); + if (manifest_plist) { + remove(manifest_path); + printf("Storing Manifest.plist...\n"); + plist_write_to_filename(manifest_plist, manifest_path, PLIST_FORMAT_XML); + } + + backup_ok = 1; + } + } + + if (backup_ok) { + /* Status.plist (Info on how the backup process turned out) */ + printf("Backup Successful.\n"); + mobilebackup_write_status(backup_directory, 1); + } else { + printf("Backup Failed.\n"); + } + + if (manifest_path) + g_free(manifest_path); + + break; + case CMD_RESTORE: + printf("Restoring backup is NOT IMPLEMENTED.\n"); + /* verify battery on AC enough battery remaining */ + /* request restore from device with manifest (BackupMessageRestoreMigrate) */ + /* read mddata/mdinfo files and send to devices using DLSendFile */ + /* signal restore finished message to device */ + /* close down lockdown connection as it is no longer needed */ + lockdownd_client_free(client); + client = NULL; + break; + case CMD_LEAVE: + default: + break; + } + if (lockfile) { + afc_file_lock(afc, lockfile, AFC_LOCK_UN); + afc_file_close(afc, lockfile); + lockfile = 0; + do_post_notification(NP_SYNC_DID_FINISH); + } + } else { + printf("ERROR: Could not start service %s.\n", MOBILEBACKUP_SERVICE_NAME); + lockdownd_client_free(client); + client = NULL; + } + + if (client) { + lockdownd_client_free(client); + client = NULL; + } + + if (afc) + afc_client_free(afc); + + if (np) + np_client_free(np); + + if (mobilebackup) + mobilebackup_client_free(mobilebackup); + + idevice_free(phone); + + return 0; +} + diff --git a/tools/ideviceinfo.c b/tools/ideviceinfo.c new file mode 100644 index 0000000..9183d92 --- /dev/null +++ b/tools/ideviceinfo.c @@ -0,0 +1,336 @@ +/* + * ideviceinfo.c + * Simple utility to show information about an attached device + * + * Copyright (c) 2009 Martin Szulecki 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 + +#define FORMAT_KEY_VALUE 1 +#define FORMAT_XML 2 + +static const char *domains[] = { + "com.apple.disk_usage", + "com.apple.mobile.battery", +/* FIXME: For some reason lockdownd segfaults on this, works sometimes though + "com.apple.mobile.debug",. */ + "com.apple.xcode.developerdomain", + "com.apple.international", + "com.apple.mobile.data_sync", + "com.apple.mobile.tethered_sync", + "com.apple.mobile.mobile_application_usage", + "com.apple.mobile.backup", + "com.apple.mobile.nikita", + "com.apple.mobile.restriction", + "com.apple.mobile.user_preferences", + "com.apple.mobile.sync_data_class", + "com.apple.mobile.software_behavior", + "com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands", + "com.apple.mobile.iTunes.accessories", + "com.apple.fairplay", + "com.apple.iTunes", + "com.apple.mobile.iTunes.store", + "com.apple.mobile.iTunes", + NULL +}; + +static int indent_level = 0; + +static int is_domain_known(char *domain) +{ + int i = 0; + while (domains[i] != NULL) { + if (strstr(domain, domains[i++])) { + return 1; + } + } + return 0; +} + +static void plist_node_to_string(plist_t node); + +static void plist_array_to_string(plist_t node) +{ + /* iterate over items */ + int i, count; + plist_t subnode = NULL; + + count = plist_array_get_size(node); + + for (i = 0; i < count; i++) { + subnode = plist_array_get_item(node, i); + printf("%*s", indent_level, ""); + printf("%d: ", i); + plist_node_to_string(subnode); + } +} + +static void plist_dict_to_string(plist_t node) +{ + /* iterate over key/value pairs */ + plist_dict_iter it = NULL; + + char* key = NULL; + plist_t subnode = NULL; + plist_dict_new_iter(node, &it); + plist_dict_next_item(node, it, &key, &subnode); + while (subnode) + { + printf("%*s", indent_level, ""); + printf("%s", key); + if (plist_get_node_type(subnode) == PLIST_ARRAY) + printf("[%d]: ", plist_array_get_size(subnode)); + else + printf(": "); + free(key); + key = NULL; + plist_node_to_string(subnode); + plist_dict_next_item(node, it, &key, &subnode); + } + free(it); +} + +static void plist_node_to_string(plist_t node) +{ + char *s = NULL; + char *data = NULL; + double d; + uint8_t b; + uint64_t u = 0; + GTimeVal tv = { 0, 0 }; + + plist_type t; + + if (!node) + return; + + t = plist_get_node_type(node); + + switch (t) { + case PLIST_BOOLEAN: + plist_get_bool_val(node, &b); + printf("%s\n", (b ? "true" : "false")); + break; + + case PLIST_UINT: + plist_get_uint_val(node, &u); + printf("%llu\n", (long long)u); + break; + + case PLIST_REAL: + plist_get_real_val(node, &d); + printf("%f\n", d); + break; + + case PLIST_STRING: + plist_get_string_val(node, &s); + printf("%s\n", s); + free(s); + break; + + case PLIST_KEY: + plist_get_key_val(node, &s); + printf("%s: ", s); + free(s); + break; + + case PLIST_DATA: + plist_get_data_val(node, &data, &u); + s = g_base64_encode((guchar *)data, u); + free(data); + printf("%s\n", s); + g_free(s); + break; + + case PLIST_DATE: + plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec); + s = g_time_val_to_iso8601(&tv); + printf("%s\n", s); + free(s); + break; + + case PLIST_ARRAY: + printf("\n"); + indent_level++; + plist_array_to_string(node); + indent_level--; + break; + + case PLIST_DICT: + printf("\n"); + indent_level++; + plist_dict_to_string(node); + indent_level--; + break; + + default: + break; + } +} + +static void print_usage(int argc, char **argv) +{ + int i = 0; + char *name = NULL; + + name = strrchr(argv[0], '/'); + printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); + printf("Show information about a connected iPhone/iPod Touch.\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(" -q, --domain NAME\tset domain of query to NAME. Default: None\n"); + printf(" -k, --key NAME\tonly query key specified by NAME. Default: All keys.\n"); + printf(" -x, --xml\t\toutput information as xml plist instead of key/value pairs\n"); + printf(" -h, --help\t\tprints usage information\n"); + printf("\n"); + printf(" Known domains are:\n\n"); + while (domains[i] != NULL) { + printf(" %s\n", domains[i++]); + } + printf("\n"); +} + +int main(int argc, char *argv[]) +{ + lockdownd_client_t client = NULL; + idevice_t phone = NULL; + idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; + int i; + int format = FORMAT_KEY_VALUE; + char uuid[41]; + char *domain = NULL; + char *key = NULL; + char *xml_doc = NULL; + uint32_t xml_length; + plist_t node = NULL; + plist_type node_type; + uuid[0] = 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; + } + strcpy(uuid, argv[i]); + continue; + } + else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--domain")) { + i++; + if (!argv[i] || (strlen(argv[i]) < 4)) { + print_usage(argc, argv); + return 0; + } + if (!is_domain_known(argv[i])) { + fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", argv[i]); + } + domain = strdup(argv[i]); + continue; + } + else if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--key")) { + i++; + if (!argv[i] || (strlen(argv[i]) <= 1)) { + print_usage(argc, argv); + return 0; + } + key = strdup(argv[i]); + continue; + } + else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--xml")) { + format = FORMAT_XML; + continue; + } + else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_usage(argc, argv); + return 0; + } + else { + print_usage(argc, argv); + return 0; + } + } + + if (uuid[0] != 0) { + ret = idevice_new(&phone, uuid); + if (ret != IDEVICE_E_SUCCESS) { + printf("No device found with uuid %s, is it plugged in?\n", uuid); + return -1; + } + } + else + { + ret = idevice_new(&phone, NULL); + if (ret != IDEVICE_E_SUCCESS) { + printf("No device found, is it plugged in?\n"); + return -1; + } + } + + if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "ideviceinfo")) { + idevice_free(phone); + return -1; + } + + /* run query and output information */ + if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS) { + if (node) { + switch (format) { + case FORMAT_XML: + plist_to_xml(node, &xml_doc, &xml_length); + printf("%s", xml_doc); + free(xml_doc); + break; + case FORMAT_KEY_VALUE: + node_type = plist_get_node_type(node); + if (node_type == PLIST_DICT) { + plist_dict_to_string(node); + } else if (node_type == PLIST_ARRAY) { + plist_array_to_string(node); + break; + } + default: + if (key != NULL) + plist_node_to_string(node); + break; + } + plist_free(node); + node = NULL; + } + } + + if (domain != NULL) + free(domain); + lockdownd_client_free(client); + idevice_free(phone); + + return 0; +} + diff --git a/tools/idevicesyslog.c b/tools/idevicesyslog.c new file mode 100644 index 0000000..32b9711 --- /dev/null +++ b/tools/idevicesyslog.c @@ -0,0 +1,165 @@ +/* + * idevicesyslog.c + * Relay the syslog of a device to stdout + * + * Copyright (c) 2009 Martin Szulecki 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 +#include + +static int quit_flag = 0; + +void print_usage(int argc, char **argv); + +/** + * signal handler function for cleaning up properly + */ +static void clean_exit(int sig) +{ + fprintf(stderr, "Exiting...\n"); + quit_flag++; +} + +int main(int argc, char *argv[]) +{ + lockdownd_client_t client = NULL; + idevice_t phone = NULL; + idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; + int i; + char uuid[41]; + uint16_t port = 0; + uuid[0] = 0; + + signal(SIGINT, clean_exit); + signal(SIGQUIT, clean_exit); + signal(SIGTERM, clean_exit); + signal(SIGPIPE, SIG_IGN); + + /* 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; + } + strcpy(uuid, argv[i]); + continue; + } + else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_usage(argc, argv); + return 0; + } + else { + print_usage(argc, argv); + return 0; + } + } + + if (uuid[0] != 0) { + ret = idevice_new(&phone, uuid); + if (ret != IDEVICE_E_SUCCESS) { + printf("No device found with uuid %s, is it plugged in?\n", uuid); + return -1; + } + } + else + { + ret = idevice_new(&phone, NULL); + if (ret != IDEVICE_E_SUCCESS) { + printf("No device found, is it plugged in?\n"); + return -1; + } + } + + if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "idevicesyslog")) { + idevice_free(phone); + return -1; + } + + /* start syslog_relay service and retrieve port */ + ret = lockdownd_start_service(client, "com.apple.syslog_relay", &port); + if ((ret == LOCKDOWN_E_SUCCESS) && port) { + lockdownd_client_free(client); + + /* connect to socket relay messages */ + idevice_connection_t conn = NULL; + if ((idevice_connect(phone, port, &conn) != IDEVICE_E_SUCCESS) || !conn) { + printf("ERROR: Could not open usbmux connection.\n"); + } else { + while (!quit_flag) { + char *receive = NULL; + uint32_t datalen = 0, bytes = 0, recv_bytes = 0; + + ret = idevice_connection_receive(conn, (char *) &datalen, sizeof(datalen), &bytes); + datalen = ntohl(datalen); + + if (datalen == 0) + continue; + + recv_bytes += bytes; + receive = (char *) malloc(sizeof(char) * datalen); + + while (!quit_flag && (recv_bytes <= datalen)) { + ret = idevice_connection_receive(conn, receive, datalen, &bytes); + + if (bytes == 0) + break; + + recv_bytes += bytes; + + fwrite(receive, sizeof(char), bytes, stdout); + } + + free(receive); + } + } + idevice_disconnect(conn); + } else { + printf("ERROR: Could not start service com.apple.syslog_relay.\n"); + } + + idevice_free(phone); + + return 0; +} + +void print_usage(int argc, char **argv) +{ + char *name = NULL; + + name = strrchr(argv[0], '/'); + printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); + printf("Relay syslog of a connected iPhone/iPod Touch.\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(" -h, --help\t\tprints usage information\n"); + printf("\n"); +} + diff --git a/tools/iphone_id.c b/tools/iphone_id.c deleted file mode 100644 index 4a2c3af..0000000 --- a/tools/iphone_id.c +++ /dev/null @@ -1,107 +0,0 @@ -#include -#include -#include -#include -#include - -#define MODE_NONE 0 -#define MODE_SHOW_ID 1 -#define MODE_LIST_DEVICES 2 - -static void print_usage(int argc, char **argv) -{ - char *name = NULL; - - name = strrchr(argv[0], '/'); - printf("Usage: %s [OPTIONS] [UUID]\n", (name ? name + 1: argv[0])); - printf("Prints device name or a list of attached iPhone/iPod Touch devices.\n\n"); - printf(" The UUID is a 40-digit hexadecimal number of the device\n"); - printf(" for which the name should be retrieved.\n\n"); - printf(" -l, --list\t\tlist UUID of all attached devices\n"); - printf(" -d, --debug\t\tenable communication debugging\n"); - printf(" -h, --help\t\tprints usage information\n"); - printf("\n"); -} - -int main(int argc, char **argv) -{ - iphone_device_t phone = NULL; - lockdownd_client_t client = NULL; - char **dev_list = NULL; - char *devname = NULL; - int ret = 0; - int i; - int mode = MODE_SHOW_ID; - char uuid[41]; - uuid[0] = 0; - - /* parse cmdline args */ - for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { - iphone_set_debug_level(1); - continue; - } - else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list")) { - mode = MODE_LIST_DEVICES; - continue; - } - else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - print_usage(argc, argv); - return 0; - } - } - - /* check if uuid was passed */ - if (mode == MODE_SHOW_ID) { - i--; - if (!argv[i] || (strlen(argv[i]) != 40)) { - print_usage(argc, argv); - return 0; - } - strcpy(uuid, argv[i]); - } - - switch (mode) { - case MODE_SHOW_ID: - iphone_device_new(&phone, uuid); - if (!phone) { - fprintf(stderr, "ERROR: No device with UUID=%s attached.\n", uuid); - return -2; - } - - if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client, "iphone_id")) { - iphone_device_free(phone); - fprintf(stderr, "ERROR: Connecting to device failed!\n"); - return -2; - } - - if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &devname)) || !devname) { - fprintf(stderr, "ERROR: Could not get device name!\n"); - ret = -2; - } - - lockdownd_client_free(client); - iphone_device_free(phone); - - if (ret == 0) { - printf("%s\n", devname); - } - - if (devname) { - free(devname); - } - - return ret; - case MODE_LIST_DEVICES: - default: - if (iphone_get_device_list(&dev_list, &i) < 0) { - fprintf(stderr, "ERROR: Unable to retrieve device list!\n"); - return -1; - } - for (i = 0; dev_list[i] != NULL; i++) { - printf("%s\n", dev_list[i]); - } - iphone_device_list_free(dev_list); - return 0; - } -} diff --git a/tools/iphonebackup.c b/tools/iphonebackup.c deleted file mode 100644 index bc61347..0000000 --- a/tools/iphonebackup.c +++ /dev/null @@ -1,953 +0,0 @@ -/* - * iphonebackup.c - * Command line interface to use the device's backup and restore service - * - * Copyright (c) 2009-2010 Martin Szulecki All Rights Reserved. - * 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 -#include -#include -#include -#include - -#define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup" -#define NP_SERVICE_NAME "com.apple.mobile.notification_proxy" - -static mobilebackup_client_t mobilebackup = NULL; -static lockdownd_client_t client = NULL; -static iphone_device_t phone = NULL; - -static int quit_flag = 0; - -enum cmd_mode { - CMD_BACKUP, - CMD_RESTORE, - CMD_LEAVE -}; - -enum plist_format_t { - PLIST_FORMAT_XML, - PLIST_FORMAT_BINARY -}; - -enum device_link_file_status_t { - DEVICE_LINK_FILE_STATUS_NONE = 0, - DEVICE_LINK_FILE_STATUS_HUNK, - DEVICE_LINK_FILE_STATUS_LAST_HUNK -}; - -static void notify_cb(const char *notification) -{ - if (!strcmp(notification, NP_SYNC_CANCEL_REQUEST)) { - printf("User has aborted on-device\n"); - quit_flag++; - } else { - printf("unhandled notification '%s' (TODO: implement)\n", notification); - } -} - -static plist_t mobilebackup_factory_info_plist_new() -{ - /* gather data from lockdown */ - GTimeVal tv = {0, 0}; - plist_t value_node = NULL; - plist_t root_node = NULL; - char *uuid = NULL; - char *uuid_uppercase = NULL; - - plist_t ret = plist_new_dict(); - - /* get basic device information in one go */ - lockdownd_get_value(client, NULL, NULL, &root_node); - - /* set fields we understand */ - value_node = plist_dict_get_item(root_node, "BuildVersion"); - plist_dict_insert_item(ret, "Build Version", plist_copy(value_node)); - - value_node = plist_dict_get_item(root_node, "DeviceName"); - plist_dict_insert_item(ret, "Device Name", plist_copy(value_node)); - plist_dict_insert_item(ret, "Display Name", plist_copy(value_node)); - - /* FIXME: How is the GUID generated? */ - plist_dict_insert_item(ret, "GUID", plist_new_string("---")); - - value_node = plist_dict_get_item(root_node, "InternationalMobileEquipmentIdentity"); - if (value_node) - plist_dict_insert_item(ret, "IMEI", plist_copy(value_node)); - - g_get_current_time(&tv); - plist_dict_insert_item(ret, "Last Backup Date", plist_new_date(tv.tv_sec, tv.tv_usec)); - - value_node = plist_dict_get_item(root_node, "ProductType"); - plist_dict_insert_item(ret, "Product Type", plist_copy(value_node)); - - value_node = plist_dict_get_item(root_node, "ProductVersion"); - plist_dict_insert_item(ret, "Product Version", plist_copy(value_node)); - - value_node = plist_dict_get_item(root_node, "SerialNumber"); - plist_dict_insert_item(ret, "Serial Number", plist_copy(value_node)); - - value_node = plist_dict_get_item(root_node, "UniqueDeviceID"); - iphone_device_get_uuid(phone, &uuid); - plist_dict_insert_item(ret, "Target Identifier", plist_new_string(uuid)); - - /* uppercase */ - uuid_uppercase = g_ascii_strup(uuid, -1); - plist_dict_insert_item(ret, "Unique Identifier", plist_new_string(uuid_uppercase)); - free(uuid_uppercase); - free(uuid); - - /* FIXME: Embed files as nodes */ - plist_t files = plist_new_dict(); - plist_dict_insert_item(ret, "iTunes Files", files); - plist_dict_insert_item(ret, "iTunes Version", plist_new_string("9.0.2")); - - plist_free(root_node); - - return ret; -} - -static void mobilebackup_info_update_last_backup_date(plist_t info_plist) -{ - GTimeVal tv = {0, 0}; - plist_t node = NULL; - - if (!info_plist) - return; - - g_get_current_time(&tv); - node = plist_dict_get_item(info_plist, "Last Backup Date"); - plist_set_date_val(node, tv.tv_sec, tv.tv_usec); - - node = NULL; -} - -static void buffer_read_from_filename(const char *filename, char **buffer, uint32_t *length) -{ - FILE *f; - uint64_t size; - - f = fopen(filename, "rb"); - - fseek(f, 0, SEEK_END); - size = ftell(f); - rewind(f); - - *buffer = (char*)malloc(sizeof(char)*size); - fread(*buffer, sizeof(char), size, f); - fclose(f); - - *length = size; -} - -static void buffer_write_to_filename(const char *filename, const char *buffer, uint32_t length) -{ - FILE *f; - - f = fopen(filename, "ab"); - fwrite(buffer, sizeof(char), length, f); - fclose(f); -} - -static int plist_read_from_filename(plist_t *plist, const char *filename) -{ - char *buffer = NULL; - uint32_t length; - - if (!filename) - return 0; - - buffer_read_from_filename(filename, &buffer, &length); - - if (!buffer) { - return 0; - } - - if (memcmp(buffer, "bplist00", 8) == 0) { - plist_from_bin(buffer, length, plist); - } else { - plist_from_xml(buffer, length, plist); - } - - free(buffer); - - return 1; -} - -static int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) -{ - char *buffer = NULL; - uint32_t length; - - if (!plist || !filename) - return 0; - - if (format == PLIST_FORMAT_XML) - plist_to_xml(plist, &buffer, &length); - else if (format == PLIST_FORMAT_BINARY) - plist_to_bin(plist, &buffer, &length); - else - return 0; - - buffer_write_to_filename(filename, buffer, length); - - free(buffer); - - return 1; -} - -static int plist_strcmp(plist_t node, const char *str) -{ - char *buffer = NULL; - int ret = 0; - - if (plist_get_node_type(node) != PLIST_STRING) - return ret; - - plist_get_string_val(node, &buffer); - ret = strcmp(buffer, str); - free(buffer); - - return ret; -} - -static plist_t device_link_message_factory_process_message_new(plist_t content) -{ - plist_t ret = plist_new_array(); - plist_array_append_item(ret, plist_new_string("DLMessageProcessMessage")); - plist_array_append_item(ret, content); - return ret; -} - -static void mobilebackup_cancel_backup_with_error(const char *reason) -{ - plist_t node = plist_new_dict(); - plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("BackupMessageError")); - plist_dict_insert_item(node, "BackupErrorReasonKey", plist_new_string(reason)); - - plist_t message = device_link_message_factory_process_message_new(node); - - mobilebackup_send(mobilebackup, message); - - plist_free(message); - message = NULL; -} - -static plist_t mobilebackup_factory_backup_file_received_new() -{ - plist_t node = plist_new_dict(); - plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("kBackupMessageBackupFileReceived")); - return device_link_message_factory_process_message_new(node); -} - -static gchar *mobilebackup_build_path(const char *backup_directory, const char *name, const char *extension) -{ - gchar *filename = g_strconcat(name, extension, NULL); - gchar *path = g_build_path(G_DIR_SEPARATOR_S, backup_directory, filename, NULL); - g_free(filename); - return path; -} - -static void mobilebackup_write_status(const char *path, int status) -{ - struct stat st; - plist_t status_plist = plist_new_dict(); - plist_dict_insert_item(status_plist, "Backup Success", plist_new_bool(status)); - gchar *file_path = mobilebackup_build_path(path, "Status", ".plist"); - - if (stat(file_path, &st) == 0) - remove(file_path); - - plist_write_to_filename(status_plist, file_path, PLIST_FORMAT_XML); - - plist_free(status_plist); - status_plist = NULL; - - g_free(file_path); -} - -static int mobilebackup_info_is_current_device(plist_t info) -{ - plist_t value_node = NULL; - plist_t node = NULL; - plist_t root_node = NULL; - int ret = 0; - - if (!info) - return ret; - - if (plist_get_node_type(info) != PLIST_DICT) - return ret; - - /* get basic device information in one go */ - lockdownd_get_value(client, NULL, NULL, &root_node); - - /* verify UUID */ - value_node = plist_dict_get_item(root_node, "UniqueDeviceID"); - node = plist_dict_get_item(info, "Target Identifier"); - - if(plist_compare_node_value(value_node, node)) - ret = 1; - else { - printf("Info.plist: UniqueDeviceID does not match.\n"); - } - - /* verify SerialNumber */ - if (ret == 1) { - value_node = plist_dict_get_item(root_node, "SerialNumber"); - node = plist_dict_get_item(info, "Serial Number"); - - if(plist_compare_node_value(value_node, node)) - ret = 1; - else { - printf("Info.plist: SerialNumber does not match.\n"); - ret = 0; - } - } - - /* verify ProductVersion to prevent using backup with different OS version */ - if (ret == 1) { - value_node = plist_dict_get_item(root_node, "ProductVersion"); - node = plist_dict_get_item(info, "Product Version"); - - if(plist_compare_node_value(value_node, node)) - ret = 1; - else { - printf("Info.plist: ProductVersion does not match.\n"); - ret = 0; - } - } - - plist_free(root_node); - root_node = NULL; - - value_node = NULL; - node = NULL; - - return ret; -} - -static int mobilebackup_delete_backup_file_by_hash(const char *backup_directory, const char *hash) -{ - int ret = 0; - gchar *path = mobilebackup_build_path(backup_directory, hash, ".mddata"); - printf("Removing \"%s\"... ", path); - if (!remove( path )) - ret = 1; - else - ret = 0; - - g_free(path); - - if (!ret) - return ret; - - path = mobilebackup_build_path(backup_directory, hash, ".mdinfo"); - printf("Removing \"%s\"... ", path); - if (!remove( path )) - ret = 1; - else - ret = 0; - - g_free(path); - - return ret; -} - -static void do_post_notification(const char *notification) -{ - uint16_t nport = 0; - np_client_t np; - - if (!client) { - if (lockdownd_client_new_with_handshake(phone, &client, "iphonebackup") != LOCKDOWN_E_SUCCESS) { - return; - } - } - - lockdownd_start_service(client, NP_SERVICE_NAME, &nport); - if (nport) { - np_client_new(phone, nport, &np); - if (np) { - np_post_notification(np, notification); - np_client_free(np); - } - } else { - printf("Could not start %s\n", NP_SERVICE_NAME); - } -} - -/** - * signal handler function for cleaning up properly - */ -static void clean_exit(int sig) -{ - fprintf(stderr, "Exiting...\n"); - quit_flag++; -} - -static void print_usage(int argc, char **argv) -{ - char *name = NULL; - name = strrchr(argv[0], '/'); - printf("Usage: %s [OPTIONS] CMD [DIRECTORY]\n", (name ? name + 1: argv[0])); - printf("Create or restore backup from the current or specified directory.\n\n"); - printf("commands:\n"); - printf(" backup\tSaves a device backup into DIRECTORY\n"); - printf(" restore\tRestores a device backup from DIRECTORY.\n\n"); - printf("options:\n"); - printf(" -d, --debug\t\tenable communication debugging\n"); - printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); - printf(" -h, --help\t\tprints usage information\n"); - printf("\n"); -} - -int main(int argc, char *argv[]) -{ - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - int i; - char uuid[41]; - uint16_t port = 0; - uuid[0] = 0; - int cmd = -1; - int is_full_backup = 0; - char *backup_directory = NULL; - struct stat st; - plist_t node = NULL; - plist_t node_tmp = NULL; - plist_t manifest_plist = NULL; - plist_t info_plist = NULL; - char *buffer = NULL; - uint64_t length = 0; - uint64_t backup_total_size = 0; - enum device_link_file_status_t file_status; - uint64_t c = 0; - - /* we need to exit cleanly on running backups and restores or we cause havok */ - signal(SIGINT, clean_exit); - signal(SIGQUIT, clean_exit); - signal(SIGTERM, clean_exit); - signal(SIGPIPE, SIG_IGN); - - /* parse cmdline args */ - for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { - iphone_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; - } - strcpy(uuid, argv[i]); - continue; - } - else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - print_usage(argc, argv); - return 0; - } - else if (!strcmp(argv[i], "backup")) { - cmd = CMD_BACKUP; - } - else if (!strcmp(argv[i], "restore")) { - cmd = CMD_RESTORE; - } - else if (backup_directory == NULL) { - backup_directory = argv[i]; - } - else { - print_usage(argc, argv); - return 0; - } - } - - /* verify options */ - if (cmd == -1) { - printf("No command specified.\n"); - print_usage(argc, argv); - return -1; - } - - if (backup_directory == NULL) { - printf("No target backup directory specified.\n"); - print_usage(argc, argv); - return -1; - } - - /* verify if passed backup directory exists */ - if (stat(backup_directory, &st) != 0) { - printf("ERROR: Backup directory \"%s\" does not exist!\n", backup_directory); - return -1; - } - - /* restore directory must contain an Info.plist */ - char *info_path = mobilebackup_build_path(backup_directory, "Info", ".plist"); - if (cmd == CMD_RESTORE) { - if (stat(info_path, &st) != 0) { - g_free(info_path); - printf("ERROR: Backup directory \"%s\" is invalid. No Info.plist found.\n", backup_directory); - return -1; - } - } - - printf("Backup directory is \"%s\"\n", backup_directory); - - if (uuid[0] != 0) { - ret = iphone_device_new(&phone, uuid); - if (ret != IPHONE_E_SUCCESS) { - printf("No device found with uuid %s, is it plugged in?\n", uuid); - return -1; - } - } - else - { - ret = iphone_device_new(&phone, NULL); - if (ret != IPHONE_E_SUCCESS) { - printf("No device found, is it plugged in?\n"); - return -1; - } - } - - if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "iphonebackup")) { - iphone_device_free(phone); - return -1; - } - - /* start notification_proxy */ - np_client_t np = NULL; - ret = lockdownd_start_service(client, NP_SERVICE_NAME, &port); - if ((ret == LOCKDOWN_E_SUCCESS) && port) { - np_client_new(phone, port, &np); - np_set_notify_callback(np, notify_cb); - const char *noties[5] = { - NP_SYNC_CANCEL_REQUEST, - NP_SYNC_SUSPEND_REQUEST, - NP_SYNC_RESUME_REQUEST, - NP_BACKUP_DOMAIN_CHANGED, - NULL - }; - np_observe_notifications(np, noties); - } else { - printf("ERROR: Could not start service %s.\n", NP_SERVICE_NAME); - } - - /* start AFC, we need this for the lock file */ - afc_client_t afc = NULL; - port = 0; - ret = lockdownd_start_service(client, "com.apple.afc", &port); - if ((ret == LOCKDOWN_E_SUCCESS) && port) { - afc_client_new(phone, port, &afc); - } - - /* start syslog_relay service and retrieve port */ - port = 0; - ret = lockdownd_start_service(client, MOBILEBACKUP_SERVICE_NAME, &port); - if ((ret == LOCKDOWN_E_SUCCESS) && port) { - printf("Started \"%s\" service on port %d.\n", MOBILEBACKUP_SERVICE_NAME, port); - mobilebackup_client_new(phone, port, &mobilebackup); - - /* check abort conditions */ - if (quit_flag > 0) { - printf("Aborting backup. Cancelled by user.\n"); - cmd = CMD_LEAVE; - } - - /* verify existing Info.plist */ - if (stat(info_path, &st) == 0) { - printf("Reading Info.plist from backup.\n"); - plist_read_from_filename(&info_plist, info_path); - - if (cmd == CMD_BACKUP) { - if (mobilebackup_info_is_current_device(info_plist)) { - /* update the last backup time within Info.plist */ - mobilebackup_info_update_last_backup_date(info_plist); - remove(info_path); - plist_write_to_filename(info_plist, info_path, PLIST_FORMAT_XML); - } else { - printf("Aborting backup. Backup is not compatible with the current device.\n"); - cmd = CMD_LEAVE; - } - } - } else { - is_full_backup = 1; - } - - do_post_notification(NP_SYNC_WILL_START); - uint64_t lockfile = 0; - afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile); - if (lockfile) { - do_post_notification(NP_SYNC_LOCK_REQUEST); - if (afc_file_lock(afc, lockfile, AFC_LOCK_EX) == AFC_E_SUCCESS) { - do_post_notification(NP_SYNC_DID_START); - } else { - afc_file_close(afc, lockfile); - lockfile = 0; - } - } - - switch(cmd) { - case CMD_BACKUP: - printf("Starting backup...\n"); - /* TODO: check domain com.apple.mobile.backup key RequiresEncrypt and WillEncrypt with lockdown */ - /* TODO: verify battery on AC enough battery remaining */ - - /* Info.plist (Device infos, IC-Info.sidb, photos, app_ids, iTunesPrefs) */ - /* create new Info.plist on new backups */ - if (is_full_backup) { - printf("Creating Info.plist for new backup.\n"); - info_plist = mobilebackup_factory_info_plist_new(); - plist_write_to_filename(info_plist, info_path, PLIST_FORMAT_XML); - } - - g_free(info_path); - - /* Manifest.plist (backup manifest (backup state)) */ - char *manifest_path = mobilebackup_build_path(backup_directory, "Manifest", ".plist"); - - /* read the last Manifest.plist */ - if (!is_full_backup) { - printf("Reading existing Manifest.\n"); - plist_read_from_filename(&manifest_plist, manifest_path); - } - - plist_free(info_plist); - info_plist = NULL; - - /* close down the lockdown connection as it is no longer needed */ - if (client) { - lockdownd_client_free(client); - client = NULL; - } - - /* create Status.plist with failed status for now */ - mobilebackup_write_status(backup_directory, 0); - - /* request backup from device with manifest from last backup */ - printf("Requesting backup from device...\n"); - - node = plist_new_dict(); - - if (manifest_plist) - plist_dict_insert_item(node, "BackupManifestKey", manifest_plist); - - plist_dict_insert_item(node, "BackupComputerBasePathKey", plist_new_string("/")); - plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("BackupMessageBackupRequest")); - plist_dict_insert_item(node, "BackupProtocolVersion", plist_new_string("1.6")); - - plist_t message = device_link_message_factory_process_message_new(node); - mobilebackup_send(mobilebackup, message); - plist_free(message); - message = NULL; - node = NULL; - - /* get response */ - int backup_ok = 0; - mobilebackup_receive(mobilebackup, &message); - - node = plist_array_get_item(message, 0); - if (!plist_strcmp(node, "DLMessageProcessMessage")) { - node = plist_array_get_item(message, 1); - node = plist_dict_get_item(node, "BackupMessageTypeKey"); - if (node && !plist_strcmp(node, "BackupMessageBackupReplyOK")) { - printf("Device accepts manifest and will send backup data now...\n"); - backup_ok = 1; - printf("Acknowledging...\n"); - if (is_full_backup) - printf("Full backup mode.\n"); - else - printf("Incremental backup mode.\n"); - printf("Please wait. Device prepares backup data...\n"); - /* send it back for ACK */ - mobilebackup_send(mobilebackup, message); - } - } else { - printf("ERROR: Unhandled message received!\n"); - } - plist_free(message); - message = NULL; - - if (!backup_ok) { - printf("ERROR: Device rejected to start the backup process.\n"); - break; - } - - /* reset backup status */ - backup_ok = 0; - - /* receive and save DLSendFile files and metadata, ACK each */ - int file_index = 0; - int hunk_index = 0; - uint64_t backup_real_size = 0; - char *file_path = NULL; - char *file_ext = NULL; - char *filename_mdinfo = NULL; - char *filename_mddata = NULL; - char *filename_source = NULL; - char *format_size = NULL; - gboolean is_manifest = FALSE; - uint8_t b = 0; - - /* process series of DLSendFile messages */ - do { - mobilebackup_receive(mobilebackup, &message); - node = plist_array_get_item(message, 0); - - /* get out if we don't get a DLSendFile */ - if (plist_strcmp(node, "DLSendFile")) - break; - - node_tmp = plist_array_get_item(message, 2); - - /* first message hunk contains total backup size */ - if (hunk_index == 0) { - node = plist_dict_get_item(node_tmp, "BackupTotalSizeKey"); - if (node) { - plist_get_uint_val(node, &backup_total_size); - format_size = g_format_size_for_display(backup_total_size); - printf("Backup data requires %s on the disk.\n", format_size); - g_free(format_size); - } - } - - /* check DLFileStatusKey (codes: 1 = Hunk, 2 = Last Hunk) */ - node = plist_dict_get_item(node_tmp, "DLFileStatusKey"); - plist_get_uint_val(node, &c); - file_status = c; - - /* get source filename */ - node = plist_dict_get_item(node_tmp, "BackupManifestKey"); - b = 0; - if (node) { - plist_get_bool_val(node, &b); - } - is_manifest = (b == 1) ? TRUE: FALSE; - - /* check if we completed a file */ - if ((file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) && (!is_manifest)) { - /* get source filename */ - node = plist_dict_get_item(node_tmp, "DLFileSource"); - plist_get_string_val(node, &filename_source); - - /* increase received size */ - node = plist_dict_get_item(node_tmp, "DLFileAttributesKey"); - node = plist_dict_get_item(node, "FileSize"); - plist_get_uint_val(node, &length); - backup_real_size += length; - - format_size = g_format_size_for_display(backup_real_size); - printf("(%s", format_size); - g_free(format_size); - - format_size = g_format_size_for_display(backup_total_size); - printf("/%s): ", format_size); - g_free(format_size); - - printf("Received file %s... ", filename_source); - - if (filename_source) - free(filename_source); - - /* save .mdinfo */ - node = plist_dict_get_item(node_tmp, "BackupFileInfo"); - if (node) { - node = plist_dict_get_item(node_tmp, "DLFileDest"); - plist_get_string_val(node, &file_path); - - filename_mdinfo = mobilebackup_build_path(backup_directory, file_path, ".mdinfo"); - - /* remove any existing file */ - if (stat(filename_mdinfo, &st) != 0) - remove(filename_mdinfo); - - node = plist_dict_get_item(node_tmp, "BackupFileInfo"); - plist_write_to_filename(node, filename_mdinfo, PLIST_FORMAT_BINARY); - - g_free(filename_mdinfo); - } - - file_index++; - } - - /* save .mddata */ - node = plist_dict_get_item(node_tmp, "BackupFileInfo"); - if (node_tmp && file_path) { - node = plist_dict_get_item(node_tmp, "DLFileDest"); - plist_get_string_val(node, &file_path); - - filename_mddata = mobilebackup_build_path(backup_directory, file_path, is_manifest ? NULL: ".mddata"); - - /* if this is the first hunk, remove any existing file */ - if (stat(filename_mddata, &st) != 0) - remove(filename_mddata); - - /* get file data hunk */ - node_tmp = plist_array_get_item(message, 1); - plist_get_data_val(node_tmp, &buffer, &length); - - buffer_write_to_filename(filename_mddata, buffer, length); - - /* activate currently sent manifest */ - if ((file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) && (is_manifest)) { - rename(filename_mddata, manifest_path); - } - - free(buffer); - buffer = NULL; - - g_free(filename_mddata); - } - - hunk_index++; - - if (file_ext) - free(file_ext); - - if (message) - plist_free(message); - message = NULL; - - if (file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) { - if (!is_manifest) - printf("DONE\n"); - - /* acknowlegdge that we received the file */ - message = mobilebackup_factory_backup_file_received_new(); - mobilebackup_send(mobilebackup, message); - plist_free(message); - message = NULL; - } - - if (quit_flag > 0) { - /* need to cancel the backup here */ - mobilebackup_cancel_backup_with_error("Cancelling DLSendFile"); - - /* remove any atomic Manifest.plist.tmp */ - if (manifest_path) - g_free(manifest_path); - - manifest_path = mobilebackup_build_path(backup_directory, "Manifest", ".plist.tmp"); - if (stat(manifest_path, &st) == 0) - remove(manifest_path); - break; - } - } while (1); - - printf("Received %d files from device.\n", file_index); - - if (!quit_flag && !plist_strcmp(node, "DLMessageProcessMessage")) { - node_tmp = plist_array_get_item(message, 1); - node = plist_dict_get_item(node_tmp, "BackupMessageTypeKey"); - /* check if we received the final "backup finished" message */ - if (node && !plist_strcmp(node, "BackupMessageBackupFinished")) { - /* backup finished */ - - /* process BackupFilesToDeleteKey */ - node = plist_dict_get_item(node_tmp, "BackupFilesToDeleteKey"); - if (node) { - length = plist_array_get_size(node); - i = 0; - while ((node_tmp = plist_array_get_item(node, i++)) != NULL) { - plist_get_string_val(node_tmp, &file_path); - - if (mobilebackup_delete_backup_file_by_hash(backup_directory, file_path)) { - printf("DONE\n"); - } else - printf("FAILED\n"); - } - } - - /* save last valid Manifest.plist */ - node_tmp = plist_array_get_item(message, 1); - manifest_plist = plist_dict_get_item(node_tmp, "BackupManifestKey"); - if (manifest_plist) { - remove(manifest_path); - printf("Storing Manifest.plist...\n"); - plist_write_to_filename(manifest_plist, manifest_path, PLIST_FORMAT_XML); - } - - backup_ok = 1; - } - } - - if (backup_ok) { - /* Status.plist (Info on how the backup process turned out) */ - printf("Backup Successful.\n"); - mobilebackup_write_status(backup_directory, 1); - } else { - printf("Backup Failed.\n"); - } - - if (manifest_path) - g_free(manifest_path); - - break; - case CMD_RESTORE: - printf("Restoring backup is NOT IMPLEMENTED.\n"); - /* verify battery on AC enough battery remaining */ - /* request restore from device with manifest (BackupMessageRestoreMigrate) */ - /* read mddata/mdinfo files and send to devices using DLSendFile */ - /* signal restore finished message to device */ - /* close down lockdown connection as it is no longer needed */ - lockdownd_client_free(client); - client = NULL; - break; - case CMD_LEAVE: - default: - break; - } - if (lockfile) { - afc_file_lock(afc, lockfile, AFC_LOCK_UN); - afc_file_close(afc, lockfile); - lockfile = 0; - do_post_notification(NP_SYNC_DID_FINISH); - } - } else { - printf("ERROR: Could not start service %s.\n", MOBILEBACKUP_SERVICE_NAME); - lockdownd_client_free(client); - client = NULL; - } - - if (client) { - lockdownd_client_free(client); - client = NULL; - } - - if (afc) - afc_client_free(afc); - - if (np) - np_client_free(np); - - if (mobilebackup) - mobilebackup_client_free(mobilebackup); - - iphone_device_free(phone); - - return 0; -} - diff --git a/tools/iphoneinfo.c b/tools/iphoneinfo.c deleted file mode 100644 index 5ee92f5..0000000 --- a/tools/iphoneinfo.c +++ /dev/null @@ -1,336 +0,0 @@ -/* - * iphoneinfo.c - * Simple utility to show information about an attached device - * - * Copyright (c) 2009 Martin Szulecki 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 - -#define FORMAT_KEY_VALUE 1 -#define FORMAT_XML 2 - -static const char *domains[] = { - "com.apple.disk_usage", - "com.apple.mobile.battery", -/* FIXME: For some reason lockdownd segfaults on this, works sometimes though - "com.apple.mobile.debug",. */ - "com.apple.xcode.developerdomain", - "com.apple.international", - "com.apple.mobile.data_sync", - "com.apple.mobile.tethered_sync", - "com.apple.mobile.mobile_application_usage", - "com.apple.mobile.backup", - "com.apple.mobile.nikita", - "com.apple.mobile.restriction", - "com.apple.mobile.user_preferences", - "com.apple.mobile.sync_data_class", - "com.apple.mobile.software_behavior", - "com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands", - "com.apple.mobile.iTunes.accessories", - "com.apple.fairplay", - "com.apple.iTunes", - "com.apple.mobile.iTunes.store", - "com.apple.mobile.iTunes", - NULL -}; - -static int indent_level = 0; - -static int is_domain_known(char *domain) -{ - int i = 0; - while (domains[i] != NULL) { - if (strstr(domain, domains[i++])) { - return 1; - } - } - return 0; -} - -static void plist_node_to_string(plist_t node); - -static void plist_array_to_string(plist_t node) -{ - /* iterate over items */ - int i, count; - plist_t subnode = NULL; - - count = plist_array_get_size(node); - - for (i = 0; i < count; i++) { - subnode = plist_array_get_item(node, i); - printf("%*s", indent_level, ""); - printf("%d: ", i); - plist_node_to_string(subnode); - } -} - -static void plist_dict_to_string(plist_t node) -{ - /* iterate over key/value pairs */ - plist_dict_iter it = NULL; - - char* key = NULL; - plist_t subnode = NULL; - plist_dict_new_iter(node, &it); - plist_dict_next_item(node, it, &key, &subnode); - while (subnode) - { - printf("%*s", indent_level, ""); - printf("%s", key); - if (plist_get_node_type(subnode) == PLIST_ARRAY) - printf("[%d]: ", plist_array_get_size(subnode)); - else - printf(": "); - free(key); - key = NULL; - plist_node_to_string(subnode); - plist_dict_next_item(node, it, &key, &subnode); - } - free(it); -} - -static void plist_node_to_string(plist_t node) -{ - char *s = NULL; - char *data = NULL; - double d; - uint8_t b; - uint64_t u = 0; - GTimeVal tv = { 0, 0 }; - - plist_type t; - - if (!node) - return; - - t = plist_get_node_type(node); - - switch (t) { - case PLIST_BOOLEAN: - plist_get_bool_val(node, &b); - printf("%s\n", (b ? "true" : "false")); - break; - - case PLIST_UINT: - plist_get_uint_val(node, &u); - printf("%llu\n", (long long)u); - break; - - case PLIST_REAL: - plist_get_real_val(node, &d); - printf("%f\n", d); - break; - - case PLIST_STRING: - plist_get_string_val(node, &s); - printf("%s\n", s); - free(s); - break; - - case PLIST_KEY: - plist_get_key_val(node, &s); - printf("%s: ", s); - free(s); - break; - - case PLIST_DATA: - plist_get_data_val(node, &data, &u); - s = g_base64_encode((guchar *)data, u); - free(data); - printf("%s\n", s); - g_free(s); - break; - - case PLIST_DATE: - plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec); - s = g_time_val_to_iso8601(&tv); - printf("%s\n", s); - free(s); - break; - - case PLIST_ARRAY: - printf("\n"); - indent_level++; - plist_array_to_string(node); - indent_level--; - break; - - case PLIST_DICT: - printf("\n"); - indent_level++; - plist_dict_to_string(node); - indent_level--; - break; - - default: - break; - } -} - -static void print_usage(int argc, char **argv) -{ - int i = 0; - char *name = NULL; - - name = strrchr(argv[0], '/'); - printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); - printf("Show information about the first connected iPhone/iPod Touch.\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(" -q, --domain NAME\tset domain of query to NAME. Default: None\n"); - printf(" -k, --key NAME\tonly query key specified by NAME. Default: All keys.\n"); - printf(" -x, --xml\t\toutput information as xml plist instead of key/value pairs\n"); - printf(" -h, --help\t\tprints usage information\n"); - printf("\n"); - printf(" Known domains are:\n\n"); - while (domains[i] != NULL) { - printf(" %s\n", domains[i++]); - } - printf("\n"); -} - -int main(int argc, char *argv[]) -{ - lockdownd_client_t client = NULL; - iphone_device_t phone = NULL; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - int i; - int format = FORMAT_KEY_VALUE; - char uuid[41]; - char *domain = NULL; - char *key = NULL; - char *xml_doc = NULL; - uint32_t xml_length; - plist_t node = NULL; - plist_type node_type; - uuid[0] = 0; - - /* parse cmdline args */ - for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { - iphone_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; - } - strcpy(uuid, argv[i]); - continue; - } - else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--domain")) { - i++; - if (!argv[i] || (strlen(argv[i]) < 4)) { - print_usage(argc, argv); - return 0; - } - if (!is_domain_known(argv[i])) { - fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", argv[i]); - } - domain = strdup(argv[i]); - continue; - } - else if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--key")) { - i++; - if (!argv[i] || (strlen(argv[i]) <= 1)) { - print_usage(argc, argv); - return 0; - } - key = strdup(argv[i]); - continue; - } - else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--xml")) { - format = FORMAT_XML; - continue; - } - else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - print_usage(argc, argv); - return 0; - } - else { - print_usage(argc, argv); - return 0; - } - } - - if (uuid[0] != 0) { - ret = iphone_device_new(&phone, uuid); - if (ret != IPHONE_E_SUCCESS) { - printf("No device found with uuid %s, is it plugged in?\n", uuid); - return -1; - } - } - else - { - ret = iphone_device_new(&phone, NULL); - if (ret != IPHONE_E_SUCCESS) { - printf("No device found, is it plugged in?\n"); - return -1; - } - } - - if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "iphoneinfo")) { - iphone_device_free(phone); - return -1; - } - - /* run query and output information */ - if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS) { - if (node) { - switch (format) { - case FORMAT_XML: - plist_to_xml(node, &xml_doc, &xml_length); - printf("%s", xml_doc); - free(xml_doc); - break; - case FORMAT_KEY_VALUE: - node_type = plist_get_node_type(node); - if (node_type == PLIST_DICT) { - plist_dict_to_string(node); - } else if (node_type == PLIST_ARRAY) { - plist_array_to_string(node); - break; - } - default: - if (key != NULL) - plist_node_to_string(node); - break; - } - plist_free(node); - node = NULL; - } - } - - if (domain != NULL) - free(domain); - lockdownd_client_free(client); - iphone_device_free(phone); - - return 0; -} - diff --git a/tools/iphonesyslog.c b/tools/iphonesyslog.c deleted file mode 100644 index 41e490f..0000000 --- a/tools/iphonesyslog.c +++ /dev/null @@ -1,165 +0,0 @@ -/* - * syslog_relay.c - * Relay the syslog of a device to stdout - * - * Copyright (c) 2009 Martin Szulecki 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 -#include - -static int quit_flag = 0; - -void print_usage(int argc, char **argv); - -/** - * signal handler function for cleaning up properly - */ -static void clean_exit(int sig) -{ - fprintf(stderr, "Exiting...\n"); - quit_flag++; -} - -int main(int argc, char *argv[]) -{ - lockdownd_client_t client = NULL; - iphone_device_t phone = NULL; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - int i; - char uuid[41]; - uint16_t port = 0; - uuid[0] = 0; - - signal(SIGINT, clean_exit); - signal(SIGQUIT, clean_exit); - signal(SIGTERM, clean_exit); - signal(SIGPIPE, SIG_IGN); - - /* parse cmdline args */ - for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { - iphone_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; - } - strcpy(uuid, argv[i]); - continue; - } - else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - print_usage(argc, argv); - return 0; - } - else { - print_usage(argc, argv); - return 0; - } - } - - if (uuid[0] != 0) { - ret = iphone_device_new(&phone, uuid); - if (ret != IPHONE_E_SUCCESS) { - printf("No device found with uuid %s, is it plugged in?\n", uuid); - return -1; - } - } - else - { - ret = iphone_device_new(&phone, NULL); - if (ret != IPHONE_E_SUCCESS) { - printf("No device found, is it plugged in?\n"); - return -1; - } - } - - if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "iphonesyslog")) { - iphone_device_free(phone); - return -1; - } - - /* start syslog_relay service and retrieve port */ - ret = lockdownd_start_service(client, "com.apple.syslog_relay", &port); - if ((ret == LOCKDOWN_E_SUCCESS) && port) { - lockdownd_client_free(client); - - /* connect to socket relay messages */ - iphone_connection_t conn = NULL; - if ((iphone_device_connect(phone, port, &conn) != IPHONE_E_SUCCESS) || !conn) { - printf("ERROR: Could not open usbmux connection.\n"); - } else { - while (!quit_flag) { - char *receive = NULL; - uint32_t datalen = 0, bytes = 0, recv_bytes = 0; - - ret = iphone_connection_receive(conn, (char *) &datalen, sizeof(datalen), &bytes); - datalen = ntohl(datalen); - - if (datalen == 0) - continue; - - recv_bytes += bytes; - receive = (char *) malloc(sizeof(char) * datalen); - - while (!quit_flag && (recv_bytes <= datalen)) { - ret = iphone_connection_receive(conn, receive, datalen, &bytes); - - if (bytes == 0) - break; - - recv_bytes += bytes; - - fwrite(receive, sizeof(char), bytes, stdout); - } - - free(receive); - } - } - iphone_device_disconnect(conn); - } else { - printf("ERROR: Could not start service com.apple.syslog_relay.\n"); - } - - iphone_device_free(phone); - - return 0; -} - -void print_usage(int argc, char **argv) -{ - char *name = NULL; - - name = strrchr(argv[0], '/'); - printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); - printf("Relay syslog of a connected iPhone/iPod Touch.\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(" -h, --help\t\tprints usage information\n"); - printf("\n"); -} - -- cgit v1.1-32-gdbae