summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/Makefile.am3
-rw-r--r--include/libiphone/mobilebackup.h55
-rw-r--r--src/Makefile.am3
-rw-r--r--src/device_link_service.c20
-rw-r--r--src/device_link_service.h1
-rw-r--r--src/mobilebackup.c131
-rw-r--r--src/mobilebackup.h31
-rw-r--r--src/mobilesync.c18
-rw-r--r--tools/Makefile.am8
-rw-r--r--tools/iphonebackup.c830
10 files changed, 1079 insertions, 21 deletions
diff --git a/include/Makefile.am b/include/Makefile.am
index f871d28..aced258 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -5,4 +5,5 @@ nobase_include_HEADERS = libiphone/libiphone.h \
5 libiphone/notification_proxy.h \ 5 libiphone/notification_proxy.h \
6 libiphone/installation_proxy.h \ 6 libiphone/installation_proxy.h \
7 libiphone/sbservices.h \ 7 libiphone/sbservices.h \
8 libiphone/mobilesync.h 8 libiphone/mobilesync.h \
9 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
27extern "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
41typedef int16_t mobilebackup_error_t;
42
43struct mobilebackup_client_int;
44typedef struct mobilebackup_client_int *mobilebackup_client_t;
45
46mobilebackup_error_t mobilebackup_client_new(iphone_device_t device, uint16_t port, mobilebackup_client_t * client);
47mobilebackup_error_t mobilebackup_client_free(mobilebackup_client_t client);
48mobilebackup_error_t mobilebackup_receive(mobilebackup_client_t client, plist_t *plist);
49mobilebackup_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 f68f579..93cfbaf 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,4 +15,5 @@ libiphone_la_SOURCES = iphone.c iphone.h \
15 notification_proxy.c notification_proxy.h\ 15 notification_proxy.c notification_proxy.h\
16 installation_proxy.c installation_proxy.h\ 16 installation_proxy.c installation_proxy.h\
17 sbservices.c sbservices.h\ 17 sbservices.c sbservices.h\
18 mobilesync.c mobilesync.h 18 mobilesync.c mobilesync.h\
19 mobilebackup.c mobilebackup.h
diff --git a/src/device_link_service.c b/src/device_link_service.c
index b7d9ee8..9998fd0 100644
--- a/src/device_link_service.c
+++ b/src/device_link_service.c
@@ -253,6 +253,26 @@ device_link_service_error_t device_link_service_disconnect(device_link_service_c
253 return err; 253 return err;
254} 254}
255 255
256device_link_service_error_t device_link_service_process_message(device_link_service_client_t client, plist_t message)
257{
258 if (!client || !message)
259 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
260
261 if (plist_get_node_type(message) != PLIST_DICT)
262 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
263
264 plist_t array = plist_new_array();
265 plist_array_append_item(array, plist_new_string("DLMessageProcessMessage"));
266 plist_array_append_item(array, message);
267
268 device_link_service_error_t err = DEVICE_LINK_SERVICE_E_SUCCESS;
269 if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) {
270 err = DEVICE_LINK_SERVICE_E_MUX_ERROR;
271 }
272 plist_free(array);
273 return err;
274}
275
256/** 276/**
257 * Generic device link service send function. 277 * Generic device link service send function.
258 * 278 *
diff --git a/src/device_link_service.h b/src/device_link_service.h
index e14d897..8345d57 100644
--- a/src/device_link_service.h
+++ b/src/device_link_service.h
@@ -44,6 +44,7 @@ typedef int16_t device_link_service_error_t;
44device_link_service_error_t device_link_service_client_new(iphone_device_t device, uint16_t port, device_link_service_client_t *client); 44device_link_service_error_t device_link_service_client_new(iphone_device_t device, uint16_t port, device_link_service_client_t *client);
45device_link_service_error_t device_link_service_client_free(device_link_service_client_t client); 45device_link_service_error_t device_link_service_client_free(device_link_service_client_t client);
46device_link_service_error_t device_link_service_version_exchange(device_link_service_client_t client, uint64_t version_major, uint64_t version_minor); 46device_link_service_error_t device_link_service_version_exchange(device_link_service_client_t client, uint64_t version_major, uint64_t version_minor);
47device_link_service_error_t device_link_service_process_message(device_link_service_client_t client, plist_t message);
47device_link_service_error_t device_link_service_disconnect(device_link_service_client_t client); 48device_link_service_error_t device_link_service_disconnect(device_link_service_client_t client);
48device_link_service_error_t device_link_service_send(device_link_service_client_t client, plist_t plist); 49device_link_service_error_t device_link_service_send(device_link_service_client_t client, plist_t plist);
49device_link_service_error_t device_link_service_receive(device_link_service_client_t client, plist_t *plist); 50device_link_service_error_t device_link_service_receive(device_link_service_client_t client, plist_t *plist);
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 */
43static 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
62mobilebackup_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
90mobilebackup_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 */
107mobilebackup_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 */
125mobilebackup_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
27struct mobilebackup_client_int {
28 device_link_service_client_t parent;
29};
30
31#endif
diff --git a/src/mobilesync.c b/src/mobilesync.c
index 3492673..15614b5 100644
--- a/src/mobilesync.c
+++ b/src/mobilesync.c
@@ -109,16 +109,6 @@ mobilesync_error_t mobilesync_receive(mobilesync_client_t client, plist_t * plis
109 if (!client) 109 if (!client)
110 return MOBILESYNC_E_INVALID_ARG; 110 return MOBILESYNC_E_INVALID_ARG;
111 mobilesync_error_t ret = mobilesync_error(device_link_service_receive(client->parent, plist)); 111 mobilesync_error_t ret = mobilesync_error(device_link_service_receive(client->parent, plist));
112#ifndef STRIP_DEBUG_CODE
113 if (ret != MOBILESYNC_E_SUCCESS) {
114 return ret;
115 }
116 char *XMLContent = NULL;
117 uint32_t length = 0;
118 plist_to_xml(*plist, &XMLContent, &length);
119 debug_info("plist size: %i\nbuffer :\n%s", length, XMLContent);
120 free(XMLContent);
121#endif
122 return ret; 112 return ret;
123} 113}
124 114
@@ -136,13 +126,5 @@ mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist)
136{ 126{
137 if (!client || !plist) 127 if (!client || !plist)
138 return MOBILESYNC_E_INVALID_ARG; 128 return MOBILESYNC_E_INVALID_ARG;
139
140#ifndef STRIP_DEBUG_CODE
141 char *XMLContent = NULL;
142 uint32_t length = 0;
143 plist_to_xml(plist, &XMLContent, &length);
144 debug_info("plist size: %i\nbuffer :\n%s", length, XMLContent);
145 free(XMLContent);
146#endif
147 return mobilesync_error(device_link_service_send(client->parent, plist)); 129 return mobilesync_error(device_link_service_send(client->parent, plist));
148} 130}
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
3AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS) 3AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS)
4AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) 4AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS)
5 5
6bin_PROGRAMS = iphone_id iphoneinfo iphonesyslog 6bin_PROGRAMS = iphone_id iphoneinfo iphonesyslog iphonebackup
7 7
8iphoneinfo_SOURCES = iphoneinfo.c 8iphoneinfo_SOURCES = iphoneinfo.c
9iphoneinfo_CFLAGS = $(AM_CFLAGS) 9iphoneinfo_CFLAGS = $(AM_CFLAGS)
@@ -19,3 +19,9 @@ iphone_id_SOURCES = iphone_id.c
19iphone_id_CFLAGS = $(AM_CFLAGS) 19iphone_id_CFLAGS = $(AM_CFLAGS)
20iphone_id_LDFLAGS = $(AM_LDFLAGS) 20iphone_id_LDFLAGS = $(AM_LDFLAGS)
21iphone_id_LDADD = ../src/libiphone.la 21iphone_id_LDADD = ../src/libiphone.la
22
23iphonebackup_SOURCES = iphonebackup.c
24iphonebackup_CFLAGS = $(AM_CFLAGS)
25iphonebackup_LDFLAGS = $(AM_LDFLAGS)
26iphonebackup_LDADD = ../src/libiphone.la
27
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
35static mobilebackup_client_t mobilebackup = NULL;
36static lockdownd_client_t client = NULL;
37static iphone_device_t phone = NULL;
38
39static int quit_flag = 0;
40
41enum cmd_mode {
42 CMD_BACKUP,
43 CMD_RESTORE,
44 CMD_LEAVE
45};
46
47enum plist_format_t {
48 PLIST_FORMAT_XML,
49 PLIST_FORMAT_BINARY
50};
51
52enum 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
58static 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
119static 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
134static 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
152static 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
161static 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
186static 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
208static 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
223static 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
231static 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
245static 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
252static 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
260static 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
278static 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
321static 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 */
351static void clean_exit(int sig)
352{
353 fprintf(stderr, "Exiting...\n");
354 quit_flag++;
355}
356
357static 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
373int 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