diff options
| -rw-r--r-- | include/Makefile.am | 3 | ||||
| -rw-r--r-- | include/libiphone/mobilebackup.h | 55 | ||||
| -rw-r--r-- | src/Makefile.am | 3 | ||||
| -rw-r--r-- | src/mobilebackup.c | 131 | ||||
| -rw-r--r-- | src/mobilebackup.h | 31 | ||||
| -rw-r--r-- | tools/Makefile.am | 8 | ||||
| -rw-r--r-- | tools/iphonebackup.c | 380 |
7 files changed, 608 insertions, 3 deletions
diff --git a/include/Makefile.am b/include/Makefile.am index 54dae0e..a4246cf 100644 --- a/include/Makefile.am +++ b/include/Makefile.am | |||
| @@ -4,4 +4,5 @@ nobase_include_HEADERS = libiphone/libiphone.h \ | |||
| 4 | libiphone/notification_proxy.h \ | 4 | libiphone/notification_proxy.h \ |
| 5 | libiphone/installation_proxy.h \ | 5 | libiphone/installation_proxy.h \ |
| 6 | libiphone/sbservices.h \ | 6 | libiphone/sbservices.h \ |
| 7 | libiphone/mobilesync.h | 7 | libiphone/mobilesync.h \ |
| 8 | libiphone/mobilebackup.h | ||
diff --git a/include/libiphone/mobilebackup.h b/include/libiphone/mobilebackup.h new file mode 100644 index 0000000..8db6758 --- /dev/null +++ b/include/libiphone/mobilebackup.h | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | /** | ||
| 2 | * @file libiphone/mobilebackup.h | ||
| 3 | * @brief MobileBackup Implementation | ||
| 4 | * \internal | ||
| 5 | * | ||
| 6 | * Copyright (c) 2009 Martin Szulecki All Rights Reserved. | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this library; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifndef IMOBILEBACKUP_H | ||
| 24 | #define IMOBILEBACKUP_H | ||
| 25 | |||
| 26 | #ifdef __cplusplus | ||
| 27 | extern "C" { | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #include <libiphone/libiphone.h> | ||
| 31 | |||
| 32 | /* Error Codes */ | ||
| 33 | #define MOBILEBACKUP_E_SUCCESS 0 | ||
| 34 | #define MOBILEBACKUP_E_INVALID_ARG -1 | ||
| 35 | #define MOBILEBACKUP_E_PLIST_ERROR -2 | ||
| 36 | #define MOBILEBACKUP_E_MUX_ERROR -3 | ||
| 37 | #define MOBILEBACKUP_E_BAD_VERSION -4 | ||
| 38 | |||
| 39 | #define MOBILEBACKUP_E_UNKNOWN_ERROR -256 | ||
| 40 | |||
| 41 | typedef int16_t mobilebackup_error_t; | ||
| 42 | |||
| 43 | struct mobilebackup_client_int; | ||
| 44 | typedef struct mobilebackup_client_int *mobilebackup_client_t; | ||
| 45 | |||
| 46 | mobilebackup_error_t mobilebackup_client_new(iphone_device_t device, uint16_t port, mobilebackup_client_t * client); | ||
| 47 | mobilebackup_error_t mobilebackup_client_free(mobilebackup_client_t client); | ||
| 48 | mobilebackup_error_t mobilebackup_receive(mobilebackup_client_t client, plist_t *plist); | ||
| 49 | mobilebackup_error_t mobilebackup_send(mobilebackup_client_t client, plist_t plist); | ||
| 50 | |||
| 51 | #ifdef __cplusplus | ||
| 52 | } | ||
| 53 | #endif | ||
| 54 | |||
| 55 | #endif | ||
diff --git a/src/Makefile.am b/src/Makefile.am index 2e92382..6c29020 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
| @@ -14,4 +14,5 @@ libiphone_la_SOURCES = iphone.c iphone.h \ | |||
| 14 | notification_proxy.c notification_proxy.h\ | 14 | notification_proxy.c notification_proxy.h\ |
| 15 | installation_proxy.c installation_proxy.h\ | 15 | installation_proxy.c installation_proxy.h\ |
| 16 | sbservices.c sbservices.h\ | 16 | sbservices.c sbservices.h\ |
| 17 | mobilesync.c mobilesync.h | 17 | mobilesync.c mobilesync.h\ |
| 18 | mobilebackup.c mobilebackup.h | ||
diff --git a/src/mobilebackup.c b/src/mobilebackup.c new file mode 100644 index 0000000..5b81c7f --- /dev/null +++ b/src/mobilebackup.c | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | /* | ||
| 2 | * mobilebackup.c | ||
| 3 | * Contains functions for the built-in MobileBackup client. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2009 Martin Szulecki All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <plist/plist.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <stdlib.h> | ||
| 25 | #include <arpa/inet.h> | ||
| 26 | |||
| 27 | #include "mobilebackup.h" | ||
| 28 | #include "device_link_service.h" | ||
| 29 | #include "debug.h" | ||
| 30 | |||
| 31 | #define MBACKUP_VERSION_INT1 100 | ||
| 32 | #define MBACKUP_VERSION_INT2 0 | ||
| 33 | |||
| 34 | /** | ||
| 35 | * Convert an device_link_service_error_t value to an mobilebackup_error_t value. | ||
| 36 | * Used internally to get correct error codes when using device_link_service stuff. | ||
| 37 | * | ||
| 38 | * @param err An device_link_service_error_t error code | ||
| 39 | * | ||
| 40 | * @return A matching mobilebackup_error_t error code, | ||
| 41 | * MOBILEBACKUP_E_UNKNOWN_ERROR otherwise. | ||
| 42 | */ | ||
| 43 | static mobilebackup_error_t mobilebackup_error(device_link_service_error_t err) | ||
| 44 | { | ||
| 45 | switch (err) { | ||
| 46 | case DEVICE_LINK_SERVICE_E_SUCCESS: | ||
| 47 | return MOBILEBACKUP_E_SUCCESS; | ||
| 48 | case DEVICE_LINK_SERVICE_E_INVALID_ARG: | ||
| 49 | return MOBILEBACKUP_E_INVALID_ARG; | ||
| 50 | case DEVICE_LINK_SERVICE_E_PLIST_ERROR: | ||
| 51 | return MOBILEBACKUP_E_PLIST_ERROR; | ||
| 52 | case DEVICE_LINK_SERVICE_E_MUX_ERROR: | ||
| 53 | return MOBILEBACKUP_E_MUX_ERROR; | ||
| 54 | case DEVICE_LINK_SERVICE_E_BAD_VERSION: | ||
| 55 | return MOBILEBACKUP_E_BAD_VERSION; | ||
| 56 | default: | ||
| 57 | break; | ||
| 58 | } | ||
| 59 | return MOBILEBACKUP_E_UNKNOWN_ERROR; | ||
| 60 | } | ||
| 61 | |||
| 62 | mobilebackup_error_t mobilebackup_client_new(iphone_device_t device, uint16_t port, | ||
| 63 | mobilebackup_client_t * client) | ||
| 64 | { | ||
| 65 | if (!device || port == 0 || !client || *client) | ||
| 66 | return MOBILEBACKUP_E_INVALID_ARG; | ||
| 67 | |||
| 68 | device_link_service_client_t dlclient = NULL; | ||
| 69 | mobilebackup_error_t ret = mobilebackup_error(device_link_service_client_new(device, port, &dlclient)); | ||
| 70 | if (ret != MOBILEBACKUP_E_SUCCESS) { | ||
| 71 | return ret; | ||
| 72 | } | ||
| 73 | |||
| 74 | mobilebackup_client_t client_loc = (mobilebackup_client_t) malloc(sizeof(struct mobilebackup_client_int)); | ||
| 75 | client_loc->parent = dlclient; | ||
| 76 | |||
| 77 | /* perform handshake */ | ||
| 78 | ret = mobilebackup_error(device_link_service_version_exchange(dlclient, MBACKUP_VERSION_INT1, MBACKUP_VERSION_INT2)); | ||
| 79 | if (ret != MOBILEBACKUP_E_SUCCESS) { | ||
| 80 | debug_info("version exchange failed, error %d", ret); | ||
| 81 | mobilebackup_client_free(client_loc); | ||
| 82 | return ret; | ||
| 83 | } | ||
| 84 | |||
| 85 | *client = client_loc; | ||
| 86 | |||
| 87 | return ret; | ||
| 88 | } | ||
| 89 | |||
| 90 | mobilebackup_error_t mobilebackup_client_free(mobilebackup_client_t client) | ||
| 91 | { | ||
| 92 | if (!client) | ||
| 93 | return MOBILEBACKUP_E_INVALID_ARG; | ||
| 94 | device_link_service_disconnect(client->parent); | ||
| 95 | mobilebackup_error_t err = mobilebackup_error(device_link_service_client_free(client->parent)); | ||
| 96 | free(client); | ||
| 97 | return err; | ||
| 98 | } | ||
| 99 | |||
| 100 | /** Polls the iPhone for MobileBackup data. | ||
| 101 | * | ||
| 102 | * @param client The MobileBackup client | ||
| 103 | * @param plist A pointer to the location where the plist should be stored | ||
| 104 | * | ||
| 105 | * @return an error code | ||
| 106 | */ | ||
| 107 | mobilebackup_error_t mobilebackup_receive(mobilebackup_client_t client, plist_t * plist) | ||
| 108 | { | ||
| 109 | if (!client) | ||
| 110 | return MOBILEBACKUP_E_INVALID_ARG; | ||
| 111 | mobilebackup_error_t ret = mobilebackup_error(device_link_service_receive(client->parent, plist)); | ||
| 112 | return ret; | ||
| 113 | } | ||
| 114 | |||
| 115 | /** Sends MobileBackup data to the iPhone | ||
| 116 | * | ||
| 117 | * @note This function is low-level and should only be used if you need to send | ||
| 118 | * a new type of message. | ||
| 119 | * | ||
| 120 | * @param client The MobileBackup client | ||
| 121 | * @param plist The location of the plist to send | ||
| 122 | * | ||
| 123 | * @return an error code | ||
| 124 | */ | ||
| 125 | mobilebackup_error_t mobilebackup_send(mobilebackup_client_t client, plist_t plist) | ||
| 126 | { | ||
| 127 | if (!client || !plist) | ||
| 128 | return MOBILEBACKUP_E_INVALID_ARG; | ||
| 129 | return mobilebackup_error(device_link_service_send(client->parent, plist)); | ||
| 130 | } | ||
| 131 | |||
diff --git a/src/mobilebackup.h b/src/mobilebackup.h new file mode 100644 index 0000000..04ebc45 --- /dev/null +++ b/src/mobilebackup.h | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | /* | ||
| 2 | * mobilebackup.h | ||
| 3 | * Definitions for the mobilebackup service | ||
| 4 | * | ||
| 5 | * Copyright (c) 2009 Martin Szulecki All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | #ifndef MOBILEBACKUP_H | ||
| 22 | #define MOBILEBACKUP_H | ||
| 23 | |||
| 24 | #include "libiphone/mobilebackup.h" | ||
| 25 | #include "device_link_service.h" | ||
| 26 | |||
| 27 | struct mobilebackup_client_int { | ||
| 28 | device_link_service_client_t parent; | ||
| 29 | }; | ||
| 30 | |||
| 31 | #endif | ||
diff --git a/tools/Makefile.am b/tools/Makefile.am index 7f1be1c..d19ef0c 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am | |||
| @@ -3,7 +3,7 @@ INCLUDES = -I$(top_srcdir)/include | |||
| 3 | AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS) | 3 | AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS) |
| 4 | AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) | 4 | AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) |
| 5 | 5 | ||
| 6 | bin_PROGRAMS = iphone_id iphoneinfo iphonesyslog | 6 | bin_PROGRAMS = iphone_id iphoneinfo iphonesyslog iphonebackup |
| 7 | 7 | ||
| 8 | iphoneinfo_SOURCES = iphoneinfo.c | 8 | iphoneinfo_SOURCES = iphoneinfo.c |
| 9 | iphoneinfo_CFLAGS = $(AM_CFLAGS) | 9 | iphoneinfo_CFLAGS = $(AM_CFLAGS) |
| @@ -19,3 +19,9 @@ iphone_id_SOURCES = iphone_id.c | |||
| 19 | iphone_id_CFLAGS = $(AM_CFLAGS) | 19 | iphone_id_CFLAGS = $(AM_CFLAGS) |
| 20 | iphone_id_LDFLAGS = $(AM_LDFLAGS) | 20 | iphone_id_LDFLAGS = $(AM_LDFLAGS) |
| 21 | iphone_id_LDADD = ../src/libiphone.la | 21 | iphone_id_LDADD = ../src/libiphone.la |
| 22 | |||
| 23 | iphonebackup_SOURCES = iphonebackup.c | ||
| 24 | iphonebackup_CFLAGS = $(AM_CFLAGS) | ||
| 25 | iphonebackup_LDFLAGS = $(AM_LDFLAGS) | ||
| 26 | iphonebackup_LDADD = ../src/libiphone.la | ||
| 27 | |||
diff --git a/tools/iphonebackup.c b/tools/iphonebackup.c new file mode 100644 index 0000000..8e29160 --- /dev/null +++ b/tools/iphonebackup.c | |||
| @@ -0,0 +1,380 @@ | |||
| 1 | /* | ||
| 2 | * iphonebackup.c | ||
| 3 | * Command line interface to use the device's backup and restore service | ||
| 4 | * | ||
| 5 | * Copyright (c) 2009 Martin Szulecki All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <stdio.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <errno.h> | ||
| 25 | #include <stdlib.h> | ||
| 26 | #include <signal.h> | ||
| 27 | |||
| 28 | #include <libiphone/libiphone.h> | ||
| 29 | #include <libiphone/lockdown.h> | ||
| 30 | #include <libiphone/mobilebackup.h> | ||
| 31 | |||
| 32 | #define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup" | ||
| 33 | |||
| 34 | static int quit_flag = 0; | ||
| 35 | |||
| 36 | enum cmd_mode { | ||
| 37 | CMD_BACKUP, | ||
| 38 | CMD_RESTORE | ||
| 39 | }; | ||
| 40 | |||
| 41 | /* | ||
| 42 | Backup Process Communication: | ||
| 43 | -------------------------------------------------------------------------------- | ||
| 44 | * Check lockdown value for domain com.apple.mobile.backup key RequiresEncrypt and WillEncrypt | ||
| 45 | * Verify battery on AC enough battery remaining | ||
| 46 | * ValidatePair with device for TrustedHost ability | ||
| 47 | > DLMessageVersionExchange | ||
| 48 | < DLMessageVersionExchange - DLVersionsOk | ||
| 49 | > DLMessageDeviceReady | ||
| 50 | < DLMessageProcessMessage: BackupMessageTypeKey: BackupMessageBackupRequest | ||
| 51 | > DLMessageProcessMessage: BackupMessageTypeKey: BackupMessageBackupReplyOK | ||
| 52 | ... | ||
| 53 | > DLSendFile | ||
| 54 | < DLMessageProcessMessage: BackupMessageTypeKey: BackupMessageBackupFileReceived | ||
| 55 | ... | ||
| 56 | > DLMessageProcessMessage: BackupMessageTypeKey: BackupMessageBackupFinished | ||
| 57 | |||
| 58 | |||
| 59 | */ | ||
| 60 | |||
| 61 | /* | ||
| 62 | Restore Process Communication: | ||
| 63 | -------------------------------------------------------------------------------- | ||
| 64 | * Verify battery on AC enough battery remaining | ||
| 65 | * ValidatePair with device for TrustedHost ability | ||
| 66 | > DLMessageVersionExchange | ||
| 67 | < DLMessageVersionExchange - DLVersionsOk | ||
| 68 | > DLMessageDeviceReady | ||
| 69 | < DLMessageProcessMessage - BackupMessageTypeKey: BackupMessageRestoreMigrate | ||
| 70 | ... | ||
| 71 | DLSendFile | ||
| 72 | ... | ||
| 73 | < DLMessageProcessMessage - BackupMessageTypeKey: BackupMessageRestoreReplyOK | ||
| 74 | */ | ||
| 75 | |||
| 76 | static plist_t mobilebackup_factory_metadata() | ||
| 77 | { | ||
| 78 | /* | ||
| 79 | Metadata key is: | ||
| 80 | <dict> | ||
| 81 | <key>Path</key> | ||
| 82 | <string>Library/SMS/sms.db</string> | ||
| 83 | <key>Version</key> | ||
| 84 | <string>3.0</string> | ||
| 85 | <key>Greylist</key> | ||
| 86 | <false/> | ||
| 87 | <key>Domain</key> | ||
| 88 | <string>HomeDomain</string> | ||
| 89 | </dict> | ||
| 90 | */ | ||
| 91 | /* | ||
| 92 | <dict> | ||
| 93 | <key>Metadata</key> | ||
| 94 | <data><!-- binary plist --> | ||
| 95 | YnBsaXN0MDDUAQIDBAUGBwhUUGF0aFdWZXJzaW9uWEdyZXlsaXN0VkRvbWFp | ||
| 96 | bl8QEkxpYnJhcnkvU01TL3Ntcy5kYlMzLjAIWkhvbWVEb21haW4IERYeJy5D | ||
| 97 | R0gAAAAAAAABAQAAAAAAAAAJAAAAAAAAAAAAAAAAAAAAUw== | ||
| 98 | </data> | ||
| 99 | <key>StorageVersion</key> | ||
| 100 | <string>1.0</string> | ||
| 101 | <key>Version</key> | ||
| 102 | <string>3.0</string> | ||
| 103 | <key>AuthVersion</key> | ||
| 104 | <string>1.0</string> | ||
| 105 | <key>IsEncrypted</key> | ||
| 106 | <false/> | ||
| 107 | </dict> | ||
| 108 | */ | ||
| 109 | } | ||
| 110 | |||
| 111 | /** | ||
| 112 | * Generates a manifest data plist with all files and corresponding hashes | ||
| 113 | */ | ||
| 114 | static plist_t mobilebackup_factory_manifest_data_plist() | ||
| 115 | { | ||
| 116 | plist_t manifest_data = plist_dict_new(); | ||
| 117 | /* | ||
| 118 | File hash is: | ||
| 119 | sha1(<Domain>-<Relative File Path>) | ||
| 120 | */ | ||
| 121 | /* | ||
| 122 | Data hash is: | ||
| 123 | sha1(<file>) | ||
| 124 | */ | ||
| 125 | |||
| 126 | /* | ||
| 127 | <dict> | ||
| 128 | <key>DeviceId</key> | ||
| 129 | <string>7a7b570ee169f02c43d3893f0d661cf7a32e1cf5</string> | ||
| 130 | <key>Version</key> | ||
| 131 | <string>6.2</string> | ||
| 132 | <key>Files</key> | ||
| 133 | <dict> | ||
| 134 | <key>3d0d7e5fb2ce288813306e4d4636395e047a3d28</key> | ||
| 135 | <dict> | ||
| 136 | <key>ModificationTime</key> | ||
| 137 | <date>2009-12-29T02:12:17Z</date> | ||
| 138 | <key>FileLength</key> | ||
| 139 | <integer>131072</integer> | ||
| 140 | <key>Domain</key> | ||
| 141 | <string>HomeDomain</string> | ||
| 142 | <key>DataHash</key> | ||
| 143 | <data> | ||
| 144 | MfpSk+qw+RAJqLNTJI81tntvrwc= | ||
| 145 | </data> | ||
| 146 | <key>Group ID</key> | ||
| 147 | <integer>501</integer> | ||
| 148 | <key>User ID</key> | ||
| 149 | <integer>501</integer> | ||
| 150 | <key>Mode</key> | ||
| 151 | <integer>420</integer> | ||
| 152 | </dict> | ||
| 153 | </dict> | ||
| 154 | <key>DeviceICCID</key> | ||
| 155 | <string>89492060399209300736</string> | ||
| 156 | </dict> | ||
| 157 | */ | ||
| 158 | |||
| 159 | return manifest_data; | ||
| 160 | } | ||
| 161 | |||
| 162 | /** | ||
| 163 | * Generates a manifest plist with all needed information and hashes | ||
| 164 | */ | ||
| 165 | static plist_t mobilebackup_factory_manifest_plist() | ||
| 166 | { | ||
| 167 | plist_t manifest_data = mobilebackup_factory_manifest_data_plist(); | ||
| 168 | plist_t manifest = plist_dict_new(); | ||
| 169 | |||
| 170 | /* | ||
| 171 | AuthSignature Hash is: | ||
| 172 | sha1(<manifest_data>) | ||
| 173 | */ | ||
| 174 | |||
| 175 | /* | ||
| 176 | <dict> | ||
| 177 | <key>BackupManifestKey</key> | ||
| 178 | <dict> | ||
| 179 | <key>AuthVersion</key> | ||
| 180 | <string>2.0</string> | ||
| 181 | <key>AuthSignature</key> | ||
| 182 | <data> | ||
| 183 | WBdfjcZWg/u/Bpn7aKDJC68UZF4= | ||
| 184 | </data> | ||
| 185 | <key>IsEncrypted</key> | ||
| 186 | <integer>0</integer> | ||
| 187 | <key>Data</key> | ||
| 188 | <data><!-- binary plist --> | ||
| 189 | ... | ||
| 190 | </data> | ||
| 191 | </dict> | ||
| 192 | <key>BackupComputerBasePathKey</key> | ||
| 193 | <string>/</string> | ||
| 194 | <key>BackupMessageTypeKey</key> | ||
| 195 | <string>BackupMessageBackupRequest</string> | ||
| 196 | <key>BackupProtocolVersion</key> | ||
| 197 | <string>1.6</string> | ||
| 198 | </dict> | ||
| 199 | */ | ||
| 200 | |||
| 201 | return manifest; | ||
| 202 | } | ||
| 203 | |||
| 204 | /** | ||
| 205 | * signal handler function for cleaning up properly | ||
| 206 | */ | ||
| 207 | static void clean_exit(int sig) | ||
| 208 | { | ||
| 209 | fprintf(stderr, "Exiting...\n"); | ||
| 210 | quit_flag++; | ||
| 211 | } | ||
| 212 | |||
| 213 | static void print_usage(int argc, char **argv) | ||
| 214 | { | ||
| 215 | char *name = NULL; | ||
| 216 | name = strrchr(argv[0], '/'); | ||
| 217 | printf("Usage: %s [OPTIONS] CMD [DIRECTORY]\n", (name ? name + 1: argv[0])); | ||
| 218 | printf("Create or restore backup from the current or specified directory.\n\n"); | ||
| 219 | printf("commands:\n"); | ||
| 220 | printf(" backup\tSaves a device backup into DIRECTORY\n"); | ||
| 221 | printf(" restore\tRestores a device backup from DIRECTORY.\n\n"); | ||
| 222 | printf("options:\n"); | ||
| 223 | printf(" -d, --debug\t\tenable communication debugging\n"); | ||
| 224 | printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); | ||
| 225 | printf(" -h, --help\t\tprints usage information\n"); | ||
| 226 | printf("\n"); | ||
| 227 | } | ||
| 228 | |||
| 229 | static mobilebackup_client_t mobilebackup = NULL; | ||
| 230 | static lockdownd_client_t client = NULL; | ||
| 231 | static iphone_device_t phone = NULL; | ||
| 232 | |||
| 233 | int main(int argc, char *argv[]) | ||
| 234 | { | ||
| 235 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | ||
| 236 | int i; | ||
| 237 | char uuid[41]; | ||
| 238 | uint16_t port = 0; | ||
| 239 | uuid[0] = 0; | ||
| 240 | int cmd = -1; | ||
| 241 | char *backup_directory = NULL; | ||
| 242 | |||
| 243 | /* we need to exit cleanly on running backups and restores or we cause havok */ | ||
| 244 | signal(SIGINT, clean_exit); | ||
| 245 | signal(SIGQUIT, clean_exit); | ||
| 246 | signal(SIGTERM, clean_exit); | ||
| 247 | signal(SIGPIPE, SIG_IGN); | ||
| 248 | |||
| 249 | /* parse cmdline args */ | ||
| 250 | for (i = 1; i < argc; i++) { | ||
| 251 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { | ||
| 252 | iphone_set_debug_level(1); | ||
| 253 | continue; | ||
| 254 | } | ||
| 255 | else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) { | ||
| 256 | i++; | ||
| 257 | if (!argv[i] || (strlen(argv[i]) != 40)) { | ||
| 258 | print_usage(argc, argv); | ||
| 259 | return 0; | ||
| 260 | } | ||
| 261 | strcpy(uuid, argv[i]); | ||
| 262 | continue; | ||
| 263 | } | ||
| 264 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { | ||
| 265 | print_usage(argc, argv); | ||
| 266 | return 0; | ||
| 267 | } | ||
| 268 | else if (!strcmp(argv[i], "backup")) { | ||
| 269 | cmd = CMD_BACKUP; | ||
| 270 | } | ||
| 271 | else if (!strcmp(argv[i], "restore")) { | ||
| 272 | cmd = CMD_RESTORE; | ||
| 273 | } | ||
| 274 | else if (backup_directory == NULL) { | ||
| 275 | backup_directory = argv[i]; | ||
| 276 | } | ||
| 277 | else { | ||
| 278 | print_usage(argc, argv); | ||
| 279 | return 0; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | /* verify options */ | ||
| 284 | if (cmd == -1) { | ||
| 285 | printf("No command specified.\n"); | ||
| 286 | print_usage(argc, argv); | ||
| 287 | return -1; | ||
| 288 | } | ||
| 289 | |||
| 290 | if (backup_directory == NULL) { | ||
| 291 | printf("No target backup directory specified.\n"); | ||
| 292 | print_usage(argc, argv); | ||
| 293 | return -1; | ||
| 294 | } | ||
| 295 | |||
| 296 | printf("Backup directory is \"%s\"\n", backup_directory); | ||
| 297 | |||
| 298 | if (uuid[0] != 0) { | ||
| 299 | ret = iphone_device_new(&phone, uuid); | ||
| 300 | if (ret != IPHONE_E_SUCCESS) { | ||
| 301 | printf("No device found with uuid %s, is it plugged in?\n", uuid); | ||
| 302 | return -1; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | else | ||
| 306 | { | ||
| 307 | ret = iphone_device_new(&phone, NULL); | ||
| 308 | if (ret != IPHONE_E_SUCCESS) { | ||
| 309 | printf("No device found, is it plugged in?\n"); | ||
| 310 | return -1; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "iphonebackup")) { | ||
| 315 | iphone_device_free(phone); | ||
| 316 | return -1; | ||
| 317 | } | ||
| 318 | |||
| 319 | /* start syslog_relay service and retrieve port */ | ||
| 320 | ret = lockdownd_start_service(client, MOBILEBACKUP_SERVICE_NAME, &port); | ||
| 321 | if ((ret == LOCKDOWN_E_SUCCESS) && port) { | ||
| 322 | printf("Started \"%s\" service on port %d.\n", MOBILEBACKUP_SERVICE_NAME, port); | ||
| 323 | mobilebackup_client_new(phone, port, &mobilebackup); | ||
| 324 | |||
| 325 | /* TODO: Command implementations */ | ||
| 326 | switch(cmd) { | ||
| 327 | case CMD_BACKUP: | ||
| 328 | printf("TODO: Creating backup...\n"); | ||
| 329 | /* | ||
| 330 | Create target directory: | ||
| 331 | MobileSync/Backup/<uuid>-YYYYMMDD-HHMMSS/ | ||
| 332 | */ | ||
| 333 | |||
| 334 | /* | ||
| 335 | Create: Info.plist (Device infos, IC-Info.sidb, photos, app_ids, iTunesPrefs) | ||
| 336 | Create:Manifest.plist (backup manifest (backup state)) | ||
| 337 | */ | ||
| 338 | lockdownd_client_free(client); | ||
| 339 | /* | ||
| 340 | Receive: | ||
| 341 | ... | ||
| 342 | <hash>.mddata (Raw filedata) | ||
| 343 | <hash>.mdinfo (backup file information) | ||
| 344 | ... | ||
| 345 | Create: Status.plist (Info on how the backup process turned out) | ||
| 346 | */ | ||
| 347 | |||
| 348 | /* | ||
| 349 | - Check lockdown value for domain com.apple.mobile.backup key RequiresEncrypt and WillEncrypt | ||
| 350 | - Verify battery on AC enough battery remaining | ||
| 351 | - Request backup from device with manifest (BackupMessageBackupRequest) | ||
| 352 | - Receive and save DLSendFile files and metadata, ACK each | ||
| 353 | - Wait until received final backup finished message | ||
| 354 | */ | ||
| 355 | break; | ||
| 356 | case CMD_RESTORE: | ||
| 357 | printf("TODO: Restoring backup...\n"); | ||
| 358 | /* | ||
| 359 | - Verify battery on AC enough battery remaining | ||
| 360 | - Request restore from device (BackupMessageRestoreMigrate) | ||
| 361 | - Read mddata files and send to devices using DLSendFile | ||
| 362 | - Signal restore finished message to device | ||
| 363 | */ | ||
| 364 | lockdownd_client_free(client); | ||
| 365 | break; | ||
| 366 | default: | ||
| 367 | break; | ||
| 368 | } | ||
| 369 | } else { | ||
| 370 | printf("ERROR: Could not start service %s.\n", MOBILEBACKUP_SERVICE_NAME); | ||
| 371 | lockdownd_client_free(client); | ||
| 372 | } | ||
| 373 | |||
| 374 | if (mobilebackup) | ||
| 375 | mobilebackup_client_free(mobilebackup); | ||
| 376 | iphone_device_free(phone); | ||
| 377 | |||
| 378 | return 0; | ||
| 379 | } | ||
| 380 | |||
