diff options
Diffstat (limited to 'tools/iphonebackup.c')
| -rw-r--r-- | tools/iphonebackup.c | 830 |
1 files changed, 830 insertions, 0 deletions
diff --git a/tools/iphonebackup.c b/tools/iphonebackup.c new file mode 100644 index 0000000..f0cfa7a --- /dev/null +++ b/tools/iphonebackup.c | |||
| @@ -0,0 +1,830 @@ | |||
| 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 | #include <glib.h> | ||
| 28 | |||
| 29 | #include <libiphone/libiphone.h> | ||
| 30 | #include <libiphone/lockdown.h> | ||
| 31 | #include <libiphone/mobilebackup.h> | ||
| 32 | |||
| 33 | #define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup" | ||
| 34 | |||
| 35 | static mobilebackup_client_t mobilebackup = NULL; | ||
| 36 | static lockdownd_client_t client = NULL; | ||
| 37 | static iphone_device_t phone = NULL; | ||
| 38 | |||
| 39 | static int quit_flag = 0; | ||
| 40 | |||
| 41 | enum cmd_mode { | ||
| 42 | CMD_BACKUP, | ||
| 43 | CMD_RESTORE, | ||
| 44 | CMD_LEAVE | ||
| 45 | }; | ||
| 46 | |||
| 47 | enum plist_format_t { | ||
| 48 | PLIST_FORMAT_XML, | ||
| 49 | PLIST_FORMAT_BINARY | ||
| 50 | }; | ||
| 51 | |||
| 52 | enum device_link_file_status_t { | ||
| 53 | DEVICE_LINK_FILE_STATUS_NONE = 0, | ||
| 54 | DEVICE_LINK_FILE_STATUS_HUNK, | ||
| 55 | DEVICE_LINK_FILE_STATUS_LAST_HUNK | ||
| 56 | }; | ||
| 57 | |||
| 58 | static plist_t mobilebackup_factory_info_plist_new() | ||
| 59 | { | ||
| 60 | /* gather data from lockdown */ | ||
| 61 | GTimeVal tv = {0, 0}; | ||
| 62 | plist_t value_node = NULL; | ||
| 63 | plist_t root_node = NULL; | ||
| 64 | char *uuid = NULL; | ||
| 65 | char *uuid_uppercase = NULL; | ||
| 66 | |||
| 67 | plist_t ret = plist_new_dict(); | ||
| 68 | |||
| 69 | /* get basic device information in one go */ | ||
| 70 | lockdownd_get_value(client, NULL, NULL, &root_node); | ||
| 71 | |||
| 72 | /* set fields we understand */ | ||
| 73 | value_node = plist_dict_get_item(root_node, "BuildVersion"); | ||
| 74 | plist_dict_insert_item(ret, "Build Version", plist_copy(value_node)); | ||
| 75 | |||
| 76 | value_node = plist_dict_get_item(root_node, "DeviceName"); | ||
| 77 | plist_dict_insert_item(ret, "Device Name", plist_copy(value_node)); | ||
| 78 | plist_dict_insert_item(ret, "Display Name", plist_copy(value_node)); | ||
| 79 | |||
| 80 | /* FIXME: How is the GUID generated? */ | ||
| 81 | plist_dict_insert_item(ret, "GUID", plist_new_string("---")); | ||
| 82 | |||
| 83 | value_node = plist_dict_get_item(root_node, "InternationalMobileEquipmentIdentity"); | ||
| 84 | if (value_node) | ||
| 85 | plist_dict_insert_item(ret, "IMEI", plist_copy(value_node)); | ||
| 86 | |||
| 87 | g_get_current_time(&tv); | ||
| 88 | plist_dict_insert_item(ret, "Last Backup Date", plist_new_date(tv.tv_sec, tv.tv_usec)); | ||
| 89 | |||
| 90 | value_node = plist_dict_get_item(root_node, "ProductType"); | ||
| 91 | plist_dict_insert_item(ret, "Product Type", plist_copy(value_node)); | ||
| 92 | |||
| 93 | value_node = plist_dict_get_item(root_node, "ProductVersion"); | ||
| 94 | plist_dict_insert_item(ret, "Product Version", plist_copy(value_node)); | ||
| 95 | |||
| 96 | value_node = plist_dict_get_item(root_node, "SerialNumber"); | ||
| 97 | plist_dict_insert_item(ret, "Serial Number", plist_copy(value_node)); | ||
| 98 | |||
| 99 | value_node = plist_dict_get_item(root_node, "UniqueDeviceID"); | ||
| 100 | iphone_device_get_uuid(phone, &uuid); | ||
| 101 | plist_dict_insert_item(ret, "Target Identifier", plist_new_string(uuid)); | ||
| 102 | |||
| 103 | /* uppercase */ | ||
| 104 | uuid_uppercase = g_ascii_strup(uuid, -1); | ||
| 105 | plist_dict_insert_item(ret, "Unique Identifier", plist_new_string(uuid_uppercase)); | ||
| 106 | free(uuid_uppercase); | ||
| 107 | free(uuid); | ||
| 108 | |||
| 109 | /* FIXME: Embed files as <data> nodes */ | ||
| 110 | plist_t files = plist_new_dict(); | ||
| 111 | plist_dict_insert_item(ret, "iTunes Files", files); | ||
| 112 | plist_dict_insert_item(ret, "iTunes Version", plist_new_string("9.0.2")); | ||
| 113 | |||
| 114 | plist_free(root_node); | ||
| 115 | |||
| 116 | return ret; | ||
| 117 | } | ||
| 118 | |||
| 119 | static void mobilebackup_info_update_last_backup_date(plist_t info_plist) | ||
| 120 | { | ||
| 121 | GTimeVal tv = {0, 0}; | ||
| 122 | plist_t node = NULL; | ||
| 123 | |||
| 124 | if (!info_plist) | ||
| 125 | return; | ||
| 126 | |||
| 127 | g_get_current_time(&tv); | ||
| 128 | node = plist_dict_get_item(info_plist, "Last Backup Date"); | ||
| 129 | plist_set_date_val(node, tv.tv_sec, tv.tv_usec); | ||
| 130 | |||
| 131 | node = NULL; | ||
| 132 | } | ||
| 133 | |||
| 134 | static void buffer_read_from_filename(const char *filename, char **buffer, uint32_t *length) | ||
| 135 | { | ||
| 136 | FILE *f; | ||
| 137 | uint64_t size; | ||
| 138 | |||
| 139 | f = fopen(filename, "rb"); | ||
| 140 | |||
| 141 | fseek(f, 0, SEEK_END); | ||
| 142 | size = ftell(f); | ||
| 143 | rewind(f); | ||
| 144 | |||
| 145 | *buffer = (char*)malloc(sizeof(char)*size); | ||
| 146 | fread(*buffer, sizeof(char), size, f); | ||
| 147 | fclose(f); | ||
| 148 | |||
| 149 | *length = size; | ||
| 150 | } | ||
| 151 | |||
| 152 | static void buffer_write_to_filename(const char *filename, const char *buffer, uint32_t length) | ||
| 153 | { | ||
| 154 | FILE *f; | ||
| 155 | |||
| 156 | f = fopen(filename, "ab"); | ||
| 157 | fwrite(buffer, sizeof(char), length, f); | ||
| 158 | fclose(f); | ||
| 159 | } | ||
| 160 | |||
| 161 | static int plist_read_from_filename(plist_t *plist, const char *filename) | ||
| 162 | { | ||
| 163 | char *buffer = NULL; | ||
| 164 | uint32_t length; | ||
| 165 | |||
| 166 | if (!filename) | ||
| 167 | return 0; | ||
| 168 | |||
| 169 | buffer_read_from_filename(filename, &buffer, &length); | ||
| 170 | |||
| 171 | if (!buffer) { | ||
| 172 | return 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | if (memcmp(buffer, "bplist00", 8) == 0) { | ||
| 176 | plist_from_bin(buffer, length, plist); | ||
| 177 | } else { | ||
| 178 | plist_from_xml(buffer, length, plist); | ||
| 179 | } | ||
| 180 | |||
| 181 | free(buffer); | ||
| 182 | |||
| 183 | return 1; | ||
| 184 | } | ||
| 185 | |||
| 186 | static int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) | ||
| 187 | { | ||
| 188 | char *buffer = NULL; | ||
| 189 | uint32_t length; | ||
| 190 | |||
| 191 | if (!plist || !filename) | ||
| 192 | return 0; | ||
| 193 | |||
| 194 | if (format == PLIST_FORMAT_XML) | ||
| 195 | plist_to_xml(plist, &buffer, &length); | ||
| 196 | else if (format == PLIST_FORMAT_BINARY) | ||
| 197 | plist_to_bin(plist, &buffer, &length); | ||
| 198 | else | ||
| 199 | return 0; | ||
| 200 | |||
| 201 | buffer_write_to_filename(filename, buffer, length); | ||
| 202 | |||
| 203 | free(buffer); | ||
| 204 | |||
| 205 | return 1; | ||
| 206 | } | ||
| 207 | |||
| 208 | static int plist_strcmp(plist_t node, const char *str) | ||
| 209 | { | ||
| 210 | char *buffer = NULL; | ||
| 211 | int ret = 0; | ||
| 212 | |||
| 213 | if (plist_get_node_type(node) != PLIST_STRING) | ||
| 214 | return ret; | ||
| 215 | |||
| 216 | plist_get_string_val(node, &buffer); | ||
| 217 | ret = strcmp(buffer, str); | ||
| 218 | free(buffer); | ||
| 219 | |||
| 220 | return ret; | ||
| 221 | } | ||
| 222 | |||
| 223 | static plist_t device_link_message_factory_process_message_new(plist_t content) | ||
| 224 | { | ||
| 225 | plist_t ret = plist_new_array(); | ||
| 226 | plist_array_append_item(ret, plist_new_string("DLMessageProcessMessage")); | ||
| 227 | plist_array_append_item(ret, content); | ||
| 228 | return ret; | ||
| 229 | } | ||
| 230 | |||
| 231 | static void mobilebackup_cancel_backup_with_error(const char *reason) | ||
| 232 | { | ||
| 233 | plist_t node = plist_new_dict(); | ||
| 234 | plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("BackupMessageError")); | ||
| 235 | plist_dict_insert_item(node, "BackupErrorReasonKey", plist_new_string(reason)); | ||
| 236 | |||
| 237 | plist_t message = device_link_message_factory_process_message_new(node); | ||
| 238 | |||
| 239 | mobilebackup_send(mobilebackup, message); | ||
| 240 | |||
| 241 | plist_free(message); | ||
| 242 | message = NULL; | ||
| 243 | } | ||
| 244 | |||
| 245 | static plist_t mobilebackup_factory_backup_file_received_new() | ||
| 246 | { | ||
| 247 | plist_t node = plist_new_dict(); | ||
| 248 | plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("kBackupMessageBackupFileReceived")); | ||
| 249 | return device_link_message_factory_process_message_new(node); | ||
| 250 | } | ||
| 251 | |||
| 252 | static gchar *mobilebackup_build_path(const char *backup_directory, const char *name, const char *extension) | ||
| 253 | { | ||
| 254 | gchar *filename = g_strconcat(name, extension, NULL); | ||
| 255 | gchar *path = g_build_path(G_DIR_SEPARATOR_S, backup_directory, filename, NULL); | ||
| 256 | g_free(filename); | ||
| 257 | return path; | ||
| 258 | } | ||
| 259 | |||
| 260 | static void mobilebackup_write_status(const char *path, int status) | ||
| 261 | { | ||
| 262 | struct stat st; | ||
| 263 | plist_t status_plist = plist_new_dict(); | ||
| 264 | plist_dict_insert_item(status_plist, "Backup Success", plist_new_bool(status)); | ||
| 265 | gchar *file_path = mobilebackup_build_path(path, "Status", ".plist"); | ||
| 266 | |||
| 267 | if (stat(file_path, &st) == 0) | ||
| 268 | remove(file_path); | ||
| 269 | |||
| 270 | plist_write_to_filename(status_plist, file_path, PLIST_FORMAT_XML); | ||
| 271 | |||
| 272 | plist_free(status_plist); | ||
| 273 | status_plist = NULL; | ||
| 274 | |||
| 275 | g_free(file_path); | ||
| 276 | } | ||
| 277 | |||
| 278 | static int mobilebackup_info_is_current_device(plist_t info) | ||
| 279 | { | ||
| 280 | plist_t value_node = NULL; | ||
| 281 | plist_t node = NULL; | ||
| 282 | plist_t root_node = NULL; | ||
| 283 | int ret = 0; | ||
| 284 | |||
| 285 | if (!info) | ||
| 286 | return ret; | ||
| 287 | |||
| 288 | if (plist_get_node_type(info) != PLIST_DICT) | ||
| 289 | return ret; | ||
| 290 | |||
| 291 | /* get basic device information in one go */ | ||
| 292 | lockdownd_get_value(client, NULL, NULL, &root_node); | ||
| 293 | |||
| 294 | /* verify UUID */ | ||
| 295 | value_node = plist_dict_get_item(root_node, "UniqueDeviceID"); | ||
| 296 | node = plist_dict_get_item(info, "Target Identifier"); | ||
| 297 | |||
| 298 | if(plist_compare_node_value(value_node, node)) | ||
| 299 | ret = 1; | ||
| 300 | |||
| 301 | /* verify SerialNumber */ | ||
| 302 | if (ret == 1) { | ||
| 303 | value_node = plist_dict_get_item(root_node, "SerialNumber"); | ||
| 304 | node = plist_dict_get_item(info, "Serial Number"); | ||
| 305 | |||
| 306 | if(plist_compare_node_value(value_node, node)) | ||
| 307 | ret = 1; | ||
| 308 | else | ||
| 309 | ret = 0; | ||
| 310 | } | ||
| 311 | |||
| 312 | plist_free(root_node); | ||
| 313 | root_node = NULL; | ||
| 314 | |||
| 315 | value_node = NULL; | ||
| 316 | node = NULL; | ||
| 317 | |||
| 318 | return ret; | ||
| 319 | } | ||
| 320 | |||
| 321 | static int mobilebackup_delete_backup_file_by_hash(const char *backup_directory, const char *hash) | ||
| 322 | { | ||
| 323 | int ret = 0; | ||
| 324 | gchar *path = mobilebackup_build_path(backup_directory, hash, ".mddata"); | ||
| 325 | printf("Removing \"%s\"... ", path); | ||
| 326 | if (!remove( path )) | ||
| 327 | ret = 1; | ||
| 328 | else | ||
| 329 | ret = 0; | ||
| 330 | |||
| 331 | g_free(path); | ||
| 332 | |||
| 333 | if (!ret) | ||
| 334 | return ret; | ||
| 335 | |||
| 336 | path = mobilebackup_build_path(backup_directory, hash, ".mdinfo"); | ||
| 337 | printf("Removing \"%s\"... ", path); | ||
| 338 | if (!remove( path )) | ||
| 339 | ret = 1; | ||
| 340 | else | ||
| 341 | ret = 0; | ||
| 342 | |||
| 343 | g_free(path); | ||
| 344 | |||
| 345 | return ret; | ||
| 346 | } | ||
| 347 | |||
| 348 | /** | ||
| 349 | * signal handler function for cleaning up properly | ||
| 350 | */ | ||
| 351 | static void clean_exit(int sig) | ||
| 352 | { | ||
| 353 | fprintf(stderr, "Exiting...\n"); | ||
| 354 | quit_flag++; | ||
| 355 | } | ||
| 356 | |||
| 357 | static void print_usage(int argc, char **argv) | ||
| 358 | { | ||
| 359 | char *name = NULL; | ||
| 360 | name = strrchr(argv[0], '/'); | ||
| 361 | printf("Usage: %s [OPTIONS] CMD [DIRECTORY]\n", (name ? name + 1: argv[0])); | ||
| 362 | printf("Create or restore backup from the current or specified directory.\n\n"); | ||
| 363 | printf("commands:\n"); | ||
| 364 | printf(" backup\tSaves a device backup into DIRECTORY\n"); | ||
| 365 | printf(" restore\tRestores a device backup from DIRECTORY.\n\n"); | ||
| 366 | printf("options:\n"); | ||
| 367 | printf(" -d, --debug\t\tenable communication debugging\n"); | ||
| 368 | printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); | ||
| 369 | printf(" -h, --help\t\tprints usage information\n"); | ||
| 370 | printf("\n"); | ||
| 371 | } | ||
| 372 | |||
| 373 | int main(int argc, char *argv[]) | ||
| 374 | { | ||
| 375 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | ||
| 376 | int i; | ||
| 377 | char uuid[41]; | ||
| 378 | uint16_t port = 0; | ||
| 379 | uuid[0] = 0; | ||
| 380 | int cmd = -1; | ||
| 381 | int is_full_backup = 0; | ||
| 382 | char *backup_directory = NULL; | ||
| 383 | struct stat st; | ||
| 384 | plist_t node = NULL; | ||
| 385 | plist_t node_tmp = NULL; | ||
| 386 | plist_t manifest_plist = NULL; | ||
| 387 | plist_t info_plist = NULL; | ||
| 388 | char *buffer = NULL; | ||
| 389 | uint64_t length = 0; | ||
| 390 | uint64_t backup_total_size = 0; | ||
| 391 | enum device_link_file_status_t file_status; | ||
| 392 | uint64_t c = 0; | ||
| 393 | |||
| 394 | /* we need to exit cleanly on running backups and restores or we cause havok */ | ||
| 395 | signal(SIGINT, clean_exit); | ||
| 396 | signal(SIGQUIT, clean_exit); | ||
| 397 | signal(SIGTERM, clean_exit); | ||
| 398 | signal(SIGPIPE, SIG_IGN); | ||
| 399 | |||
| 400 | /* parse cmdline args */ | ||
| 401 | for (i = 1; i < argc; i++) { | ||
| 402 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { | ||
| 403 | iphone_set_debug_level(1); | ||
| 404 | continue; | ||
| 405 | } | ||
| 406 | else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) { | ||
| 407 | i++; | ||
| 408 | if (!argv[i] || (strlen(argv[i]) != 40)) { | ||
| 409 | print_usage(argc, argv); | ||
| 410 | return 0; | ||
| 411 | } | ||
| 412 | strcpy(uuid, argv[i]); | ||
| 413 | continue; | ||
| 414 | } | ||
| 415 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { | ||
| 416 | print_usage(argc, argv); | ||
| 417 | return 0; | ||
| 418 | } | ||
| 419 | else if (!strcmp(argv[i], "backup")) { | ||
| 420 | cmd = CMD_BACKUP; | ||
| 421 | } | ||
| 422 | else if (!strcmp(argv[i], "restore")) { | ||
| 423 | cmd = CMD_RESTORE; | ||
| 424 | } | ||
| 425 | else if (backup_directory == NULL) { | ||
| 426 | backup_directory = argv[i]; | ||
| 427 | } | ||
| 428 | else { | ||
| 429 | print_usage(argc, argv); | ||
| 430 | return 0; | ||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | /* verify options */ | ||
| 435 | if (cmd == -1) { | ||
| 436 | printf("No command specified.\n"); | ||
| 437 | print_usage(argc, argv); | ||
| 438 | return -1; | ||
| 439 | } | ||
| 440 | |||
| 441 | if (backup_directory == NULL) { | ||
| 442 | printf("No target backup directory specified.\n"); | ||
| 443 | print_usage(argc, argv); | ||
| 444 | return -1; | ||
| 445 | } | ||
| 446 | |||
| 447 | /* verify if passed backup directory exists */ | ||
| 448 | if (stat(backup_directory, &st) != 0) { | ||
| 449 | printf("ERROR: Backup directory \"%s\" does not exist!\n", backup_directory); | ||
| 450 | return -1; | ||
| 451 | } | ||
| 452 | |||
| 453 | /* restore directory must contain an Info.plist */ | ||
| 454 | char *info_path = mobilebackup_build_path(backup_directory, "Info", ".plist"); | ||
| 455 | if (cmd == CMD_RESTORE) { | ||
| 456 | if (stat(info_path, &st) != 0) { | ||
| 457 | g_free(info_path); | ||
| 458 | printf("ERROR: Backup directory \"%s\" is invalid. No Info.plist found.\n", backup_directory); | ||
| 459 | return -1; | ||
| 460 | } | ||
| 461 | } | ||
| 462 | |||
| 463 | printf("Backup directory is \"%s\"\n", backup_directory); | ||
| 464 | |||
| 465 | if (uuid[0] != 0) { | ||
| 466 | ret = iphone_device_new(&phone, uuid); | ||
| 467 | if (ret != IPHONE_E_SUCCESS) { | ||
| 468 | printf("No device found with uuid %s, is it plugged in?\n", uuid); | ||
| 469 | return -1; | ||
| 470 | } | ||
| 471 | } | ||
| 472 | else | ||
| 473 | { | ||
| 474 | ret = iphone_device_new(&phone, NULL); | ||
| 475 | if (ret != IPHONE_E_SUCCESS) { | ||
| 476 | printf("No device found, is it plugged in?\n"); | ||
| 477 | return -1; | ||
| 478 | } | ||
| 479 | } | ||
| 480 | |||
| 481 | if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "iphonebackup")) { | ||
| 482 | iphone_device_free(phone); | ||
| 483 | return -1; | ||
| 484 | } | ||
| 485 | |||
| 486 | /* start syslog_relay service and retrieve port */ | ||
| 487 | ret = lockdownd_start_service(client, MOBILEBACKUP_SERVICE_NAME, &port); | ||
| 488 | if ((ret == LOCKDOWN_E_SUCCESS) && port) { | ||
| 489 | printf("Started \"%s\" service on port %d.\n", MOBILEBACKUP_SERVICE_NAME, port); | ||
| 490 | mobilebackup_client_new(phone, port, &mobilebackup); | ||
| 491 | |||
| 492 | if (quit_flag > 0) { | ||
| 493 | printf("Aborting backup. Cancelled by user.\n"); | ||
| 494 | cmd = CMD_LEAVE; | ||
| 495 | } | ||
| 496 | |||
| 497 | switch(cmd) { | ||
| 498 | case CMD_BACKUP: | ||
| 499 | printf("Starting backup...\n"); | ||
| 500 | /* TODO: check domain com.apple.mobile.backup key RequiresEncrypt and WillEncrypt with lockdown */ | ||
| 501 | /* TODO: verify battery on AC enough battery remaining */ | ||
| 502 | |||
| 503 | /* Info.plist (Device infos, IC-Info.sidb, photos, app_ids, iTunesPrefs) */ | ||
| 504 | |||
| 505 | /* read existing Info.plist or create new one */ | ||
| 506 | if (stat(info_path, &st) == 0) { | ||
| 507 | printf("Reading Info.plist from existing backup.\n"); | ||
| 508 | plist_read_from_filename(&info_plist, info_path); | ||
| 509 | |||
| 510 | if(!is_full_backup) { | ||
| 511 | /* update the last backup time within Info.plist */ | ||
| 512 | mobilebackup_info_update_last_backup_date(info_plist); | ||
| 513 | remove(info_path); | ||
| 514 | plist_write_to_filename(info_plist, info_path, PLIST_FORMAT_XML); | ||
| 515 | } | ||
| 516 | } else { | ||
| 517 | printf("Creating Info.plist for new backup.\n"); | ||
| 518 | info_plist = mobilebackup_factory_info_plist_new(); | ||
| 519 | plist_write_to_filename(info_plist, info_path, PLIST_FORMAT_XML); | ||
| 520 | is_full_backup = 1; | ||
| 521 | } | ||
| 522 | |||
| 523 | g_free(info_path); | ||
| 524 | |||
| 525 | /* Manifest.plist (backup manifest (backup state)) */ | ||
| 526 | char *manifest_path = mobilebackup_build_path(backup_directory, "Manifest", ".plist"); | ||
| 527 | /* read the last Manifest.plist if the current backup is for this device */ | ||
| 528 | if (!is_full_backup && mobilebackup_info_is_current_device(info_plist)) { | ||
| 529 | printf("Reading existing Manifest.\n"); | ||
| 530 | plist_read_from_filename(&manifest_plist, manifest_path); | ||
| 531 | } | ||
| 532 | |||
| 533 | plist_free(info_plist); | ||
| 534 | info_plist = NULL; | ||
| 535 | |||
| 536 | /* close down the lockdown connection as it is no longer needed */ | ||
| 537 | if (client) { | ||
| 538 | lockdownd_client_free(client); | ||
| 539 | client = NULL; | ||
| 540 | } | ||
| 541 | |||
| 542 | /* create Status.plist with failed status for now */ | ||
| 543 | mobilebackup_write_status(backup_directory, 0); | ||
| 544 | |||
| 545 | /* request backup from device with manifest from last backup */ | ||
| 546 | printf("Requesting backup from device...\n"); | ||
| 547 | |||
| 548 | node = plist_new_dict(); | ||
| 549 | |||
| 550 | if (manifest_plist) | ||
| 551 | plist_dict_insert_item(node, "BackupManifestKey", manifest_plist); | ||
| 552 | |||
| 553 | plist_dict_insert_item(node, "BackupComputerBasePathKey", plist_new_string("/")); | ||
| 554 | plist_dict_insert_item(node, "BackupMessageTypeKey", plist_new_string("BackupMessageBackupRequest")); | ||
| 555 | plist_dict_insert_item(node, "BackupProtocolVersion", plist_new_string("1.6")); | ||
| 556 | |||
| 557 | plist_t message = device_link_message_factory_process_message_new(node); | ||
| 558 | mobilebackup_send(mobilebackup, message); | ||
| 559 | plist_free(message); | ||
| 560 | message = NULL; | ||
| 561 | |||
| 562 | /* get response */ | ||
| 563 | int backup_ok = 0; | ||
| 564 | mobilebackup_receive(mobilebackup, &message); | ||
| 565 | |||
| 566 | node = plist_array_get_item(message, 0); | ||
| 567 | if (!plist_strcmp(node, "DLMessageProcessMessage")) { | ||
| 568 | node = plist_array_get_item(message, 1); | ||
| 569 | node = plist_dict_get_item(node, "BackupMessageTypeKey"); | ||
| 570 | if (node && !plist_strcmp(node, "BackupMessageBackupReplyOK")) { | ||
| 571 | printf("Device accepts manifest and will send backup data now...\n"); | ||
| 572 | backup_ok = 1; | ||
| 573 | printf("Acknowledging...\n"); | ||
| 574 | if (is_full_backup) | ||
| 575 | printf("Full backup mode.\n"); | ||
| 576 | else | ||
| 577 | printf("Incremental backup mode.\n"); | ||
| 578 | printf("Please wait. Device prepares backup data...\n"); | ||
| 579 | /* send it back for ACK */ | ||
| 580 | mobilebackup_send(mobilebackup, message); | ||
| 581 | } | ||
| 582 | } else { | ||
| 583 | printf("ERROR: Unhandled message received!\n"); | ||
| 584 | } | ||
| 585 | plist_free(message); | ||
| 586 | message = NULL; | ||
| 587 | |||
| 588 | if (!backup_ok) { | ||
| 589 | printf("ERROR: Device rejected to start the backup process.\n"); | ||
| 590 | break; | ||
| 591 | } | ||
| 592 | |||
| 593 | /* reset backup status */ | ||
| 594 | backup_ok = 0; | ||
| 595 | |||
| 596 | /* receive and save DLSendFile files and metadata, ACK each */ | ||
| 597 | int file_index = 0; | ||
| 598 | int hunk_index = 0; | ||
| 599 | uint64_t backup_real_size = 0; | ||
| 600 | char *file_path = NULL; | ||
| 601 | char *file_ext = NULL; | ||
| 602 | char *filename_mdinfo = NULL; | ||
| 603 | char *filename_mddata = NULL; | ||
| 604 | char *filename_source = NULL; | ||
| 605 | char *format_size = NULL; | ||
| 606 | gboolean is_manifest = FALSE; | ||
| 607 | uint8_t b = 0; | ||
| 608 | |||
| 609 | /* process series of DLSendFile messages */ | ||
| 610 | do { | ||
| 611 | mobilebackup_receive(mobilebackup, &message); | ||
| 612 | node = plist_array_get_item(message, 0); | ||
| 613 | |||
| 614 | /* get out if we don't get a DLSendFile */ | ||
| 615 | if (plist_strcmp(node, "DLSendFile")) | ||
| 616 | break; | ||
| 617 | |||
| 618 | node_tmp = plist_array_get_item(message, 2); | ||
| 619 | |||
| 620 | /* first message hunk contains total backup size */ | ||
| 621 | if (hunk_index == 0) { | ||
| 622 | node = plist_dict_get_item(node_tmp, "BackupTotalSizeKey"); | ||
| 623 | if (node) { | ||
| 624 | plist_get_uint_val(node, &backup_total_size); | ||
| 625 | format_size = g_format_size_for_display(backup_total_size); | ||
| 626 | printf("Backup data requires %s on the disk.\n", format_size); | ||
| 627 | g_free(format_size); | ||
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | /* check DLFileStatusKey (codes: 1 = Hunk, 2 = Last Hunk) */ | ||
| 632 | node = plist_dict_get_item(node_tmp, "DLFileStatusKey"); | ||
| 633 | plist_get_uint_val(node, &c); | ||
| 634 | file_status = c; | ||
| 635 | |||
| 636 | /* get source filename */ | ||
| 637 | node = plist_dict_get_item(node_tmp, "BackupManifestKey"); | ||
| 638 | b = 0; | ||
| 639 | if (node) { | ||
| 640 | plist_get_bool_val(node, &b); | ||
| 641 | } | ||
| 642 | is_manifest = (b == 1) ? TRUE: FALSE; | ||
| 643 | |||
| 644 | /* check if we completed a file */ | ||
| 645 | if ((file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) && (!is_manifest)) { | ||
| 646 | /* get source filename */ | ||
| 647 | node = plist_dict_get_item(node_tmp, "DLFileSource"); | ||
| 648 | plist_get_string_val(node, &filename_source); | ||
| 649 | |||
| 650 | /* increase received size */ | ||
| 651 | node = plist_dict_get_item(node_tmp, "DLFileAttributesKey"); | ||
| 652 | node = plist_dict_get_item(node, "FileSize"); | ||
| 653 | plist_get_uint_val(node, &length); | ||
| 654 | backup_real_size += length; | ||
| 655 | |||
| 656 | format_size = g_format_size_for_display(backup_real_size); | ||
| 657 | printf("(%s", format_size); | ||
| 658 | g_free(format_size); | ||
| 659 | |||
| 660 | format_size = g_format_size_for_display(backup_total_size); | ||
| 661 | printf("/%s): ", format_size); | ||
| 662 | g_free(format_size); | ||
| 663 | |||
| 664 | printf("Received file %s... ", filename_source); | ||
| 665 | |||
| 666 | if (filename_source) | ||
| 667 | free(filename_source); | ||
| 668 | |||
| 669 | /* save <hash>.mdinfo */ | ||
| 670 | node = plist_dict_get_item(node_tmp, "BackupFileInfo"); | ||
| 671 | if (node) { | ||
| 672 | node = plist_dict_get_item(node_tmp, "DLFileDest"); | ||
| 673 | plist_get_string_val(node, &file_path); | ||
| 674 | |||
| 675 | filename_mdinfo = mobilebackup_build_path(backup_directory, file_path, ".mdinfo"); | ||
| 676 | |||
| 677 | /* remove any existing file */ | ||
| 678 | if (stat(filename_mdinfo, &st) != 0) | ||
| 679 | remove(filename_mdinfo); | ||
| 680 | |||
| 681 | node = plist_dict_get_item(node_tmp, "BackupFileInfo"); | ||
| 682 | plist_write_to_filename(node, filename_mdinfo, PLIST_FORMAT_BINARY); | ||
| 683 | |||
| 684 | g_free(filename_mdinfo); | ||
| 685 | } | ||
| 686 | |||
| 687 | file_index++; | ||
| 688 | } | ||
| 689 | |||
| 690 | /* save <hash>.mddata */ | ||
| 691 | node = plist_dict_get_item(node_tmp, "BackupFileInfo"); | ||
| 692 | if (node_tmp && file_path) { | ||
| 693 | node = plist_dict_get_item(node_tmp, "DLFileDest"); | ||
| 694 | plist_get_string_val(node, &file_path); | ||
| 695 | |||
| 696 | filename_mddata = mobilebackup_build_path(backup_directory, file_path, is_manifest ? NULL: ".mddata"); | ||
| 697 | |||
| 698 | /* if this is the first hunk, remove any existing file */ | ||
| 699 | if (stat(filename_mddata, &st) != 0) | ||
| 700 | remove(filename_mddata); | ||
| 701 | |||
| 702 | /* get file data hunk */ | ||
| 703 | node_tmp = plist_array_get_item(message, 1); | ||
| 704 | plist_get_data_val(node_tmp, &buffer, &length); | ||
| 705 | |||
| 706 | buffer_write_to_filename(filename_mddata, buffer, length); | ||
| 707 | |||
| 708 | /* activate currently sent manifest */ | ||
| 709 | if ((file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) && (is_manifest)) { | ||
| 710 | rename(filename_mddata, manifest_path); | ||
| 711 | } | ||
| 712 | |||
| 713 | free(buffer); | ||
| 714 | buffer = NULL; | ||
| 715 | |||
| 716 | g_free(filename_mddata); | ||
| 717 | } | ||
| 718 | |||
| 719 | hunk_index++; | ||
| 720 | |||
| 721 | if (file_ext) | ||
| 722 | free(file_ext); | ||
| 723 | |||
| 724 | if (message) | ||
| 725 | plist_free(message); | ||
| 726 | message = NULL; | ||
| 727 | |||
| 728 | if (file_status == DEVICE_LINK_FILE_STATUS_LAST_HUNK) { | ||
| 729 | if (!is_manifest) | ||
| 730 | printf("DONE\n"); | ||
| 731 | |||
| 732 | /* acknowlegdge that we received the file */ | ||
| 733 | message = mobilebackup_factory_backup_file_received_new(); | ||
| 734 | mobilebackup_send(mobilebackup, message); | ||
| 735 | plist_free(message); | ||
| 736 | message = NULL; | ||
| 737 | } | ||
| 738 | |||
| 739 | if (quit_flag > 0) { | ||
| 740 | /* need to cancel the backup here */ | ||
| 741 | mobilebackup_cancel_backup_with_error("Cancelling DLSendFile"); | ||
| 742 | break; | ||
| 743 | } | ||
| 744 | } while (1); | ||
| 745 | |||
| 746 | printf("Received %d files from device.\n", file_index); | ||
| 747 | |||
| 748 | if (!plist_strcmp(node, "DLMessageProcessMessage")) { | ||
| 749 | node_tmp = plist_array_get_item(message, 1); | ||
| 750 | node = plist_dict_get_item(node_tmp, "BackupMessageTypeKey"); | ||
| 751 | /* check if we received the final "backup finished" message */ | ||
| 752 | if (node && !plist_strcmp(node, "BackupMessageBackupFinished")) { | ||
| 753 | /* backup finished */ | ||
| 754 | |||
| 755 | /* process BackupFilesToDeleteKey */ | ||
| 756 | node = plist_dict_get_item(node_tmp, "BackupFilesToDeleteKey"); | ||
| 757 | if (node) { | ||
| 758 | length = plist_array_get_size(node); | ||
| 759 | i = 0; | ||
| 760 | while ((node_tmp = plist_array_get_item(node, i++)) != NULL) { | ||
| 761 | plist_get_string_val(node_tmp, &file_path); | ||
| 762 | |||
| 763 | if (mobilebackup_delete_backup_file_by_hash(backup_directory, file_path)) { | ||
| 764 | printf("DONE\n"); | ||
| 765 | } else | ||
| 766 | printf("FAILED\n"); | ||
| 767 | } | ||
| 768 | } | ||
| 769 | |||
| 770 | /* save last valid Manifest.plist */ | ||
| 771 | node_tmp = plist_array_get_item(message, 1); | ||
| 772 | manifest_plist = plist_dict_get_item(node_tmp, "BackupManifestKey"); | ||
| 773 | if (manifest_plist) { | ||
| 774 | remove(manifest_path); | ||
| 775 | printf("Storing Manifest.plist...\n"); | ||
| 776 | plist_write_to_filename(manifest_plist, manifest_path, PLIST_FORMAT_XML); | ||
| 777 | } | ||
| 778 | |||
| 779 | backup_ok = 1; | ||
| 780 | } | ||
| 781 | } | ||
| 782 | |||
| 783 | if (backup_ok) { | ||
| 784 | /* Status.plist (Info on how the backup process turned out) */ | ||
| 785 | printf("Backup Successful.\n"); | ||
| 786 | mobilebackup_write_status(backup_directory, 1); | ||
| 787 | } else { | ||
| 788 | printf("Backup Failed.\n"); | ||
| 789 | } | ||
| 790 | |||
| 791 | if (manifest_path) | ||
| 792 | g_free(manifest_path); | ||
| 793 | |||
| 794 | if (node) | ||
| 795 | plist_free(node); | ||
| 796 | |||
| 797 | break; | ||
| 798 | case CMD_RESTORE: | ||
| 799 | printf("Restoring backup is NOT IMPLEMENTED.\n"); | ||
| 800 | /* verify battery on AC enough battery remaining */ | ||
| 801 | /* request restore from device with manifest (BackupMessageRestoreMigrate) */ | ||
| 802 | /* read mddata/mdinfo files and send to devices using DLSendFile */ | ||
| 803 | /* signal restore finished message to device */ | ||
| 804 | /* close down lockdown connection as it is no longer needed */ | ||
| 805 | lockdownd_client_free(client); | ||
| 806 | client = NULL; | ||
| 807 | break; | ||
| 808 | case CMD_LEAVE: | ||
| 809 | default: | ||
| 810 | break; | ||
| 811 | } | ||
| 812 | } else { | ||
| 813 | printf("ERROR: Could not start service %s.\n", MOBILEBACKUP_SERVICE_NAME); | ||
| 814 | lockdownd_client_free(client); | ||
| 815 | client = NULL; | ||
| 816 | } | ||
| 817 | |||
| 818 | if (client) { | ||
| 819 | lockdownd_client_free(client); | ||
| 820 | client = NULL; | ||
| 821 | } | ||
| 822 | |||
| 823 | if (mobilebackup) | ||
| 824 | mobilebackup_client_free(mobilebackup); | ||
| 825 | |||
| 826 | iphone_device_free(phone); | ||
| 827 | |||
| 828 | return 0; | ||
| 829 | } | ||
| 830 | |||
