summaryrefslogtreecommitdiffstats
path: root/tools/iphonebackup.c
blob: 8e2916054a93e12e0bfc9d12add8a3f33a1b8581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * iphonebackup.c
 * Command line interface to use the device's backup and restore service
 *
 * Copyright (c) 2009 Martin Szulecki All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>

#include <libiphone/libiphone.h>
#include <libiphone/lockdown.h>
#include <libiphone/mobilebackup.h>

#define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup"

static int quit_flag = 0;

enum cmd_mode {
	CMD_BACKUP,
	CMD_RESTORE
};

/*
Backup Process Communication:
--------------------------------------------------------------------------------
* Check lockdown value for domain com.apple.mobile.backup key RequiresEncrypt and WillEncrypt
* Verify battery on AC enough battery remaining
* ValidatePair with device for TrustedHost ability
> DLMessageVersionExchange
< DLMessageVersionExchange - DLVersionsOk
> DLMessageDeviceReady
< DLMessageProcessMessage: BackupMessageTypeKey: BackupMessageBackupRequest
> DLMessageProcessMessage: BackupMessageTypeKey: BackupMessageBackupReplyOK
...
> DLSendFile
< DLMessageProcessMessage: BackupMessageTypeKey: BackupMessageBackupFileReceived
...
> DLMessageProcessMessage: BackupMessageTypeKey: BackupMessageBackupFinished


*/

/*
Restore Process Communication:
--------------------------------------------------------------------------------
* Verify battery on AC enough battery remaining
* ValidatePair with device for TrustedHost ability
> DLMessageVersionExchange
< DLMessageVersionExchange - DLVersionsOk
> DLMessageDeviceReady
< DLMessageProcessMessage - BackupMessageTypeKey: BackupMessageRestoreMigrate
...
DLSendFile
...
< DLMessageProcessMessage - BackupMessageTypeKey: BackupMessageRestoreReplyOK
*/

static plist_t mobilebackup_factory_metadata()
{
/*
Metadata key is:
<dict>
	<key>Path</key>
	<string>Library/SMS/sms.db</string>
	<key>Version</key>
	<string>3.0</string>
	<key>Greylist</key>
	<false/>
	<key>Domain</key>
	<string>HomeDomain</string>
</dict>
*/
/*
<dict>
	<key>Metadata</key>
	<data><!-- binary plist -->
	YnBsaXN0MDDUAQIDBAUGBwhUUGF0aFdWZXJzaW9uWEdyZXlsaXN0VkRvbWFp
	bl8QEkxpYnJhcnkvU01TL3Ntcy5kYlMzLjAIWkhvbWVEb21haW4IERYeJy5D
	R0gAAAAAAAABAQAAAAAAAAAJAAAAAAAAAAAAAAAAAAAAUw==
	</data>
	<key>StorageVersion</key>
	<string>1.0</string>
	<key>Version</key>
	<string>3.0</string>
	<key>AuthVersion</key>
	<string>1.0</string>
	<key>IsEncrypted</key>
	<false/>
</dict>
*/
}

/**
 * Generates a manifest data plist with all files and corresponding hashes
 */
static plist_t mobilebackup_factory_manifest_data_plist()
{
	plist_t manifest_data = plist_dict_new();
	/*
	File hash is:
	sha1(<Domain>-<Relative File Path>)
	*/
	/*
	Data hash is:
	sha1(<file>)
	*/

/*
<dict>
	<key>DeviceId</key>
	<string>7a7b570ee169f02c43d3893f0d661cf7a32e1cf5</string>
	<key>Version</key>
	<string>6.2</string>
	<key>Files</key>
	<dict>
		<key>3d0d7e5fb2ce288813306e4d4636395e047a3d28</key>
		<dict>
			<key>ModificationTime</key>
			<date>2009-12-29T02:12:17Z</date>
			<key>FileLength</key>
			<integer>131072</integer>
			<key>Domain</key>
			<string>HomeDomain</string>
			<key>DataHash</key>
			<data>
			MfpSk+qw+RAJqLNTJI81tntvrwc=
			</data>
			<key>Group ID</key>
			<integer>501</integer>
			<key>User ID</key>
			<integer>501</integer>
			<key>Mode</key>
			<integer>420</integer>
		</dict>
	</dict>
	<key>DeviceICCID</key>
	<string>89492060399209300736</string>
</dict>
*/

	return manifest_data;
}

/**
 * Generates a manifest plist with all needed information and hashes
 */
static plist_t mobilebackup_factory_manifest_plist()
{
	plist_t manifest_data = mobilebackup_factory_manifest_data_plist();
	plist_t manifest = plist_dict_new();

	/*
	AuthSignature Hash is:
	sha1(<manifest_data>)
	*/

/*
<dict>
		<key>BackupManifestKey</key>
		<dict>
			<key>AuthVersion</key>
			<string>2.0</string>
			<key>AuthSignature</key>
			<data>
			WBdfjcZWg/u/Bpn7aKDJC68UZF4=
			</data>
			<key>IsEncrypted</key>
			<integer>0</integer>
			<key>Data</key>
			<data><!-- binary plist -->
			...
			</data>
		</dict>
		<key>BackupComputerBasePathKey</key>
		<string>/</string>
		<key>BackupMessageTypeKey</key>
		<string>BackupMessageBackupRequest</string>
		<key>BackupProtocolVersion</key>
		<string>1.6</string>
</dict>
*/

	return manifest;
}

/**
 * signal handler function for cleaning up properly
 */
static void clean_exit(int sig)
{
	fprintf(stderr, "Exiting...\n");
	quit_flag++;
}

static void print_usage(int argc, char **argv)
{
	char *name = NULL;
	name = strrchr(argv[0], '/');
	printf("Usage: %s [OPTIONS] CMD [DIRECTORY]\n", (name ? name + 1: argv[0]));
	printf("Create or restore backup from the current or specified directory.\n\n");
	printf("commands:\n");
	printf("  backup\tSaves a device backup into DIRECTORY\n");
	printf("  restore\tRestores a device backup from DIRECTORY.\n\n");
	printf("options:\n");
	printf("  -d, --debug\t\tenable communication debugging\n");
	printf("  -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n");
	printf("  -h, --help\t\tprints usage information\n");
	printf("\n");
}

static mobilebackup_client_t mobilebackup = NULL;
static lockdownd_client_t client = NULL;
static iphone_device_t phone = NULL;

int main(int argc, char *argv[])
{
	iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
	int i;
	char uuid[41];
	uint16_t port = 0;
	uuid[0] = 0;
	int cmd = -1;
	char *backup_directory = NULL;

	/* we need to exit cleanly on running backups and restores or we cause havok */
	signal(SIGINT, clean_exit);
	signal(SIGQUIT, clean_exit);
	signal(SIGTERM, clean_exit);
	signal(SIGPIPE, SIG_IGN);

	/* parse cmdline args */
	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
			iphone_set_debug_level(1);
			continue;
		}
		else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) {
			i++;
			if (!argv[i] || (strlen(argv[i]) != 40)) {
				print_usage(argc, argv);
				return 0;
			}
			strcpy(uuid, argv[i]);
			continue;
		}
		else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
			print_usage(argc, argv);
			return 0;
		}
		else if (!strcmp(argv[i], "backup")) {
			cmd = CMD_BACKUP;
		}
		else if (!strcmp(argv[i], "restore")) {
			cmd = CMD_RESTORE;
		}
		else if (backup_directory == NULL) {
			backup_directory = argv[i];
		}
		else {
			print_usage(argc, argv);
			return 0;
		}
	}

	/* verify options */
	if (cmd == -1) {
		printf("No command specified.\n");
		print_usage(argc, argv);
		return -1;
	}

	if (backup_directory == NULL) {
		printf("No target backup directory specified.\n");
		print_usage(argc, argv);
		return -1;
	}

	printf("Backup directory is \"%s\"\n", backup_directory);

	if (uuid[0] != 0) {
		ret = iphone_device_new(&phone, uuid);
		if (ret != IPHONE_E_SUCCESS) {
			printf("No device found with uuid %s, is it plugged in?\n", uuid);
			return -1;
		}
	}
	else
	{
		ret = iphone_device_new(&phone, NULL);
		if (ret != IPHONE_E_SUCCESS) {
			printf("No device found, is it plugged in?\n");
			return -1;
		}
	}

	if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "iphonebackup")) {
		iphone_device_free(phone);
		return -1;
	}

	/* start syslog_relay service and retrieve port */
	ret = lockdownd_start_service(client, MOBILEBACKUP_SERVICE_NAME, &port);
	if ((ret == LOCKDOWN_E_SUCCESS) && port) {
		printf("Started \"%s\" service on port %d.\n", MOBILEBACKUP_SERVICE_NAME, port);
		mobilebackup_client_new(phone, port, &mobilebackup);

		/* TODO: Command implementations */
		switch(cmd) {
			case CMD_BACKUP:
			printf("TODO: Creating backup...\n");
/*
Create target directory:
MobileSync/Backup/<uuid>-YYYYMMDD-HHMMSS/
*/

/*
Create: Info.plist (Device infos, IC-Info.sidb, photos, app_ids, iTunesPrefs)
Create:Manifest.plist (backup manifest (backup state))
*/
			lockdownd_client_free(client);
/*
Receive:
...
<hash>.mddata (Raw filedata)
<hash>.mdinfo (backup file information)
...
Create: Status.plist (Info on how the backup process turned out)
*/

/*
- Check lockdown value for domain com.apple.mobile.backup key RequiresEncrypt and WillEncrypt
- Verify battery on AC enough battery remaining
- Request backup from device with manifest (BackupMessageBackupRequest)
- Receive and save DLSendFile files and metadata, ACK each
- Wait until received final backup finished message
*/
			break;
			case CMD_RESTORE:
			printf("TODO: Restoring backup...\n");
/*
- Verify battery on AC enough battery remaining
- Request restore from device (BackupMessageRestoreMigrate)
- Read mddata files and send to devices using DLSendFile
- Signal restore finished message to device
*/
			lockdownd_client_free(client);
			break;
			default:
			break;
		}
	} else {
		printf("ERROR: Could not start service %s.\n", MOBILEBACKUP_SERVICE_NAME);
		lockdownd_client_free(client);
	}

	if (mobilebackup)
		mobilebackup_client_free(mobilebackup);
	iphone_device_free(phone);

	return 0;
}