diff options
| author | 2010-01-24 02:45:22 +0100 | |
|---|---|---|
| committer | 2010-01-24 02:45:22 +0100 | |
| commit | 79fcf91cc86574257539fb45b0b6d1a1c64852c5 (patch) | |
| tree | d6d3ff60a92aef65d460fc0048914fc7aabc58a9 /tools/iphonebackup.c | |
| parent | b42576baef5775d46a477219de572f68024b1aac (diff) | |
| download | libimobiledevice-79fcf91cc86574257539fb45b0b6d1a1c64852c5.tar.gz libimobiledevice-79fcf91cc86574257539fb45b0b6d1a1c64852c5.tar.bz2 | |
Add service code and a backup tool for mobilebackup support
Diffstat (limited to 'tools/iphonebackup.c')
| -rw-r--r-- | tools/iphonebackup.c | 380 |
1 files changed, 380 insertions, 0 deletions
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 | |||
