| 1 |
/* |
| 2 |
* mobilebackup.c |
| 3 |
* Contains functions for the built-in MobileBackup client. |
| 4 |
* |
| 5 |
* Copyright (c) 2010-2019 Nikias Bassen, All Rights Reserved. |
| 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 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
#include <plist/plist.h> |
| 27 |
#include <string.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <stdio.h> |
| 30 |
|
| 31 |
#include "mobilebackup.h" |
| 32 |
#include "device_link_service.h" |
| 33 |
#include "common/debug.h" |
| 34 |
|
| 35 |
#define MBACKUP_VERSION_INT1 100 |
| 36 |
#define MBACKUP_VERSION_INT2 0 |
| 37 |
|
| 38 |
#define IS_FLAG_SET(x, y) (((x) & (y)) == (y)) |
| 39 |
|
| 40 |
/** |
| 41 |
* Convert an device_link_service_error_t value to an mobilebackup_error_t value. |
| 42 |
* Used internally to get correct error codes when using device_link_service stuff. |
| 43 |
* |
| 44 |
* @param err An device_link_service_error_t error code |
| 45 |
* |
| 46 |
* @return A matching mobilebackup_error_t error code, |
| 47 |
* MOBILEBACKUP_E_UNKNOWN_ERROR otherwise. |
| 48 |
*/ |
| 49 |
static mobilebackup_error_t mobilebackup_error(device_link_service_error_t err) |
| 50 |
{ |
| 51 |
switch (err) { |
| 52 |
case DEVICE_LINK_SERVICE_E_SUCCESS: |
| 53 |
return MOBILEBACKUP_E_SUCCESS; |
| 54 |
case DEVICE_LINK_SERVICE_E_INVALID_ARG: |
| 55 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 56 |
case DEVICE_LINK_SERVICE_E_PLIST_ERROR: |
| 57 |
return MOBILEBACKUP_E_PLIST_ERROR; |
| 58 |
case DEVICE_LINK_SERVICE_E_MUX_ERROR: |
| 59 |
return MOBILEBACKUP_E_MUX_ERROR; |
| 60 |
case DEVICE_LINK_SERVICE_E_SSL_ERROR: |
| 61 |
return MOBILEBACKUP_E_SSL_ERROR; |
| 62 |
case DEVICE_LINK_SERVICE_E_RECEIVE_TIMEOUT: |
| 63 |
return MOBILEBACKUP_E_RECEIVE_TIMEOUT; |
| 64 |
case DEVICE_LINK_SERVICE_E_BAD_VERSION: |
| 65 |
return MOBILEBACKUP_E_BAD_VERSION; |
| 66 |
default: |
| 67 |
break; |
| 68 |
} |
| 69 |
return MOBILEBACKUP_E_UNKNOWN_ERROR; |
| 70 |
} |
| 71 |
|
| 72 |
mobilebackup_error_t mobilebackup_client_new(idevice_t device, lockdownd_service_descriptor_t service, mobilebackup_client_t * client) |
| 73 |
{ |
| 74 |
if (!device || !service || service->port == 0 || !client || *client) |
| 75 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 76 |
|
| 77 |
device_link_service_client_t dlclient = NULL; |
| 78 |
mobilebackup_error_t ret = mobilebackup_error(device_link_service_client_new(device, service, &dlclient)); |
| 79 |
if (ret != MOBILEBACKUP_E_SUCCESS) { |
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
mobilebackup_client_t client_loc = (mobilebackup_client_t) malloc(sizeof(struct mobilebackup_client_private)); |
| 84 |
client_loc->parent = dlclient; |
| 85 |
|
| 86 |
/* perform handshake */ |
| 87 |
ret = mobilebackup_error(device_link_service_version_exchange(dlclient, MBACKUP_VERSION_INT1, MBACKUP_VERSION_INT2)); |
| 88 |
if (ret != MOBILEBACKUP_E_SUCCESS) { |
| 89 |
debug_info("version exchange failed, error %d", ret); |
| 90 |
mobilebackup_client_free(client_loc); |
| 91 |
return ret; |
| 92 |
} |
| 93 |
|
| 94 |
*client = client_loc; |
| 95 |
|
| 96 |
return ret; |
| 97 |
} |
| 98 |
|
| 99 |
mobilebackup_error_t mobilebackup_client_start_service(idevice_t device, mobilebackup_client_t * client, const char* label) |
| 100 |
{ |
| 101 |
mobilebackup_error_t err = MOBILEBACKUP_E_UNKNOWN_ERROR; |
| 102 |
service_client_factory_start_service(device, MOBILEBACKUP_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(mobilebackup_client_new), &err); |
| 103 |
return err; |
| 104 |
} |
| 105 |
|
| 106 |
mobilebackup_error_t mobilebackup_client_free(mobilebackup_client_t client) |
| 107 |
{ |
| 108 |
if (!client) |
| 109 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 110 |
mobilebackup_error_t err = MOBILEBACKUP_E_SUCCESS; |
| 111 |
if (client->parent) { |
| 112 |
device_link_service_disconnect(client->parent, NULL); |
| 113 |
err = mobilebackup_error(device_link_service_client_free(client->parent)); |
| 114 |
} |
| 115 |
free(client); |
| 116 |
return err; |
| 117 |
} |
| 118 |
|
| 119 |
mobilebackup_error_t mobilebackup_receive(mobilebackup_client_t client, plist_t * plist) |
| 120 |
{ |
| 121 |
if (!client) |
| 122 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 123 |
mobilebackup_error_t ret = mobilebackup_error(device_link_service_receive(client->parent, plist)); |
| 124 |
return ret; |
| 125 |
} |
| 126 |
|
| 127 |
mobilebackup_error_t mobilebackup_send(mobilebackup_client_t client, plist_t plist) |
| 128 |
{ |
| 129 |
if (!client || !plist) |
| 130 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 131 |
return mobilebackup_error(device_link_service_send(client->parent, plist)); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Sends a backup message plist. |
| 136 |
* |
| 137 |
* @param client The connected MobileBackup client to use. |
| 138 |
* @param message The message to send. This will be inserted into the request |
| 139 |
* plist as value for BackupMessageTypeKey. If this parameter is NULL, |
| 140 |
* the plist passed in the options parameter will be sent directly. |
| 141 |
* @param options Additional options as PLIST_DICT to add to the request. |
| 142 |
* The BackupMessageTypeKey with the value passed in the message parameter |
| 143 |
* will be inserted into this plist before sending it. This parameter |
| 144 |
* can be NULL if message is not NULL. |
| 145 |
*/ |
| 146 |
static mobilebackup_error_t mobilebackup_send_message(mobilebackup_client_t client, const char *message, plist_t options) |
| 147 |
{ |
| 148 |
if (!client || !client->parent || (!message && !options)) |
| 149 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 150 |
|
| 151 |
if (options && (plist_get_node_type(options) != PLIST_DICT)) { |
| 152 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 153 |
} |
| 154 |
|
| 155 |
mobilebackup_error_t err; |
| 156 |
|
| 157 |
if (message) { |
| 158 |
plist_t dict = NULL; |
| 159 |
if (options) { |
| 160 |
dict = plist_copy(options); |
| 161 |
} else { |
| 162 |
dict = plist_new_dict(); |
| 163 |
} |
| 164 |
plist_dict_set_item(dict, "BackupMessageTypeKey", plist_new_string(message)); |
| 165 |
|
| 166 |
/* send it as DLMessageProcessMessage */ |
| 167 |
err = mobilebackup_error(device_link_service_send_process_message(client->parent, dict)); |
| 168 |
plist_free(dict); |
| 169 |
} else { |
| 170 |
err = mobilebackup_error(device_link_service_send_process_message(client->parent, options)); |
| 171 |
} |
| 172 |
if (err != MOBILEBACKUP_E_SUCCESS) { |
| 173 |
debug_info("ERROR: Could not send message '%s' (%d)!", message, err); |
| 174 |
} |
| 175 |
return err; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Receives a plist from the device and checks if the value for the |
| 180 |
* BackupMessageTypeKey matches the value passed in the message parameter. |
| 181 |
* |
| 182 |
* @param client The connected MobileBackup client to use. |
| 183 |
* @param message The expected message to check. |
| 184 |
* @param result Pointer to a plist_t that will be set to the received plist |
| 185 |
* for further processing. The caller has to free it using plist_free(). |
| 186 |
* Note that it will be set to NULL if the operation itself fails due to |
| 187 |
* a communication or plist error. |
| 188 |
* If this parameter is NULL, it will be ignored. |
| 189 |
* |
| 190 |
* @return MOBILEBACKUP_E_SUCCESS on success, MOBILEBACKUP_E_INVALID_ARG if |
| 191 |
* client or message is invalid, MOBILEBACKUP_E_REPLY_NOT_OK if the |
| 192 |
* expected message could not be received, MOBILEBACKUP_E_PLIST_ERROR if |
| 193 |
* the received message is not a valid backup message plist (i.e. the |
| 194 |
* BackupMessageTypeKey is not present), or MOBILEBACKUP_E_MUX_ERROR |
| 195 |
* if a communication error occurs. |
| 196 |
*/ |
| 197 |
static mobilebackup_error_t mobilebackup_receive_message(mobilebackup_client_t client, const char *message, plist_t *result) |
| 198 |
{ |
| 199 |
if (!client || !client->parent || !message) |
| 200 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 201 |
|
| 202 |
if (result) |
| 203 |
*result = NULL; |
| 204 |
mobilebackup_error_t err; |
| 205 |
|
| 206 |
plist_t dict = NULL; |
| 207 |
|
| 208 |
/* receive DLMessageProcessMessage */ |
| 209 |
err = mobilebackup_error(device_link_service_receive_process_message(client->parent, &dict)); |
| 210 |
if (err != MOBILEBACKUP_E_SUCCESS) { |
| 211 |
goto leave; |
| 212 |
} |
| 213 |
|
| 214 |
plist_t node = plist_dict_get_item(dict, "BackupMessageTypeKey"); |
| 215 |
if (!node) { |
| 216 |
debug_info("ERROR: BackupMessageTypeKey not found in plist!"); |
| 217 |
err = MOBILEBACKUP_E_PLIST_ERROR; |
| 218 |
goto leave; |
| 219 |
} |
| 220 |
|
| 221 |
char *str = NULL; |
| 222 |
plist_get_string_val(node, &str); |
| 223 |
if (str && (strcmp(str, message) == 0)) { |
| 224 |
err = MOBILEBACKUP_E_SUCCESS; |
| 225 |
} else { |
| 226 |
debug_info("ERROR: BackupMessageTypeKey value does not match '%s'!", message); |
| 227 |
err = MOBILEBACKUP_E_REPLY_NOT_OK; |
| 228 |
} |
| 229 |
if (str) |
| 230 |
free(str); |
| 231 |
|
| 232 |
if (result) { |
| 233 |
*result = dict; |
| 234 |
dict = NULL; |
| 235 |
} |
| 236 |
leave: |
| 237 |
if (dict) { |
| 238 |
plist_free(dict); |
| 239 |
} |
| 240 |
|
| 241 |
return err; |
| 242 |
} |
| 243 |
|
| 244 |
mobilebackup_error_t mobilebackup_request_backup(mobilebackup_client_t client, plist_t backup_manifest, const char *base_path, const char *proto_version) |
| 245 |
{ |
| 246 |
if (!client || !client->parent || !base_path || !proto_version) |
| 247 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 248 |
|
| 249 |
if (backup_manifest && (plist_get_node_type(backup_manifest) != PLIST_DICT)) |
| 250 |
return MOBILEBACKUP_E_PLIST_ERROR; |
| 251 |
|
| 252 |
mobilebackup_error_t err; |
| 253 |
|
| 254 |
/* construct request plist */ |
| 255 |
plist_t dict = plist_new_dict(); |
| 256 |
if (backup_manifest) |
| 257 |
plist_dict_set_item(dict, "BackupManifestKey", plist_copy(backup_manifest)); |
| 258 |
plist_dict_set_item(dict, "BackupComputerBasePathKey", plist_new_string(base_path)); |
| 259 |
plist_dict_set_item(dict, "BackupMessageTypeKey", plist_new_string("BackupMessageBackupRequest")); |
| 260 |
plist_dict_set_item(dict, "BackupProtocolVersion", plist_new_string(proto_version)); |
| 261 |
|
| 262 |
/* send request */ |
| 263 |
err = mobilebackup_send_message(client, NULL, dict); |
| 264 |
plist_free(dict); |
| 265 |
dict = NULL; |
| 266 |
if (err != MOBILEBACKUP_E_SUCCESS) { |
| 267 |
debug_info("ERROR: Could not send backup request message (%d)!", err); |
| 268 |
goto leave; |
| 269 |
} |
| 270 |
|
| 271 |
/* now receive and hopefully get a BackupMessageBackupReplyOK */ |
| 272 |
err = mobilebackup_receive_message(client, "BackupMessageBackupReplyOK", &dict); |
| 273 |
if (err != MOBILEBACKUP_E_SUCCESS) { |
| 274 |
debug_info("ERROR: Could not receive BackupReplyOK message (%d)!", err); |
| 275 |
goto leave; |
| 276 |
} |
| 277 |
|
| 278 |
plist_t node = plist_dict_get_item(dict, "BackupProtocolVersion"); |
| 279 |
if (node) { |
| 280 |
char *str = NULL; |
| 281 |
plist_get_string_val(node, &str); |
| 282 |
if (str) { |
| 283 |
int maj = 0; |
| 284 |
int min = 0; |
| 285 |
sscanf(str, "%u.%u", &maj, &min); |
| 286 |
uint32_t this_ver = ((maj & 0xFF) << 8) | (min & 0xFF); |
| 287 |
maj = 0; |
| 288 |
min = 0; |
| 289 |
sscanf(proto_version, "%u.%u", &maj, &min); |
| 290 |
uint32_t proto_ver = ((maj & 0xFF) << 8) | (min & 0xFF); |
| 291 |
if (this_ver > proto_ver) { |
| 292 |
err = MOBILEBACKUP_E_BAD_VERSION; |
| 293 |
} |
| 294 |
free(str); |
| 295 |
} |
| 296 |
} |
| 297 |
if (err != MOBILEBACKUP_E_SUCCESS) |
| 298 |
goto leave; |
| 299 |
|
| 300 |
/* BackupMessageBackupReplyOK received, send it back */ |
| 301 |
err = mobilebackup_send_message(client, NULL, dict); |
| 302 |
if (err != MOBILEBACKUP_E_SUCCESS) { |
| 303 |
debug_info("ERROR: Could not send BackupReplyOK ACK (%d)", err); |
| 304 |
} |
| 305 |
|
| 306 |
leave: |
| 307 |
if (dict) |
| 308 |
plist_free(dict); |
| 309 |
return err; |
| 310 |
} |
| 311 |
|
| 312 |
mobilebackup_error_t mobilebackup_send_backup_file_received(mobilebackup_client_t client) |
| 313 |
{ |
| 314 |
return mobilebackup_send_message(client, "kBackupMessageBackupFileReceived", NULL); |
| 315 |
} |
| 316 |
|
| 317 |
mobilebackup_error_t mobilebackup_request_restore(mobilebackup_client_t client, plist_t backup_manifest, mobilebackup_flags_t flags, const char *proto_version) |
| 318 |
{ |
| 319 |
if (!client || !client->parent || !backup_manifest || !proto_version) |
| 320 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 321 |
|
| 322 |
if (backup_manifest && (plist_get_node_type(backup_manifest) != PLIST_DICT)) |
| 323 |
return MOBILEBACKUP_E_PLIST_ERROR; |
| 324 |
|
| 325 |
mobilebackup_error_t err; |
| 326 |
|
| 327 |
/* construct request plist */ |
| 328 |
plist_t dict = plist_new_dict(); |
| 329 |
plist_dict_set_item(dict, "BackupManifestKey", plist_copy(backup_manifest)); |
| 330 |
plist_dict_set_item(dict, "BackupMessageTypeKey", plist_new_string("kBackupMessageRestoreRequest")); |
| 331 |
plist_dict_set_item(dict, "BackupProtocolVersion", plist_new_string(proto_version)); |
| 332 |
/* add flags */ |
| 333 |
plist_dict_set_item(dict, "BackupNotifySpringBoard", plist_new_bool(IS_FLAG_SET(flags, MB_RESTORE_NOTIFY_SPRINGBOARD))); |
| 334 |
plist_dict_set_item(dict, "BackupPreserveSettings", plist_new_bool(IS_FLAG_SET(flags, MB_RESTORE_PRESERVE_SETTINGS))); |
| 335 |
plist_dict_set_item(dict, "BackupPreserveCameraRoll", plist_new_bool(IS_FLAG_SET(flags, MB_RESTORE_PRESERVE_CAMERA_ROLL))); |
| 336 |
|
| 337 |
/* send request */ |
| 338 |
err = mobilebackup_send_message(client, NULL, dict); |
| 339 |
plist_free(dict); |
| 340 |
dict = NULL; |
| 341 |
if (err != MOBILEBACKUP_E_SUCCESS) { |
| 342 |
debug_info("ERROR: Could not send restore request message (%d)!", err); |
| 343 |
goto leave; |
| 344 |
} |
| 345 |
|
| 346 |
/* now receive and hopefully get a BackupMessageRestoreReplyOK */ |
| 347 |
err = mobilebackup_receive_message(client, "BackupMessageRestoreReplyOK", &dict); |
| 348 |
if (err != MOBILEBACKUP_E_SUCCESS) { |
| 349 |
debug_info("ERROR: Could not receive RestoreReplyOK message (%d)!", err); |
| 350 |
goto leave; |
| 351 |
} |
| 352 |
|
| 353 |
plist_t node = plist_dict_get_item(dict, "BackupProtocolVersion"); |
| 354 |
if (node) { |
| 355 |
char *str = NULL; |
| 356 |
plist_get_string_val(node, &str); |
| 357 |
if (str) { |
| 358 |
int maj = 0; |
| 359 |
int min = 0; |
| 360 |
sscanf(str, "%u.%u", &maj, &min); |
| 361 |
uint32_t this_ver = ((maj & 0xFF) << 8) | (min & 0xFF); |
| 362 |
maj = 0; |
| 363 |
min = 0; |
| 364 |
sscanf(proto_version, "%u.%u", &maj, &min); |
| 365 |
uint32_t proto_ver = ((maj & 0xFF) << 8) | (min & 0xFF); |
| 366 |
if (this_ver > proto_ver) { |
| 367 |
err = MOBILEBACKUP_E_BAD_VERSION; |
| 368 |
} |
| 369 |
free(str); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
leave: |
| 374 |
if (dict) |
| 375 |
plist_free(dict); |
| 376 |
return err; |
| 377 |
} |
| 378 |
|
| 379 |
mobilebackup_error_t mobilebackup_receive_restore_file_received(mobilebackup_client_t client, plist_t *result) |
| 380 |
{ |
| 381 |
return mobilebackup_receive_message(client, "BackupMessageRestoreFileReceived", result); |
| 382 |
} |
| 383 |
|
| 384 |
mobilebackup_error_t mobilebackup_receive_restore_application_received(mobilebackup_client_t client, plist_t *result) |
| 385 |
{ |
| 386 |
return mobilebackup_receive_message(client, "BackupMessageRestoreApplicationReceived", result); |
| 387 |
} |
| 388 |
|
| 389 |
mobilebackup_error_t mobilebackup_send_restore_complete(mobilebackup_client_t client) |
| 390 |
{ |
| 391 |
mobilebackup_error_t err = mobilebackup_send_message(client, "BackupMessageRestoreComplete", NULL); |
| 392 |
if (err != MOBILEBACKUP_E_SUCCESS) { |
| 393 |
return err; |
| 394 |
} |
| 395 |
plist_t dlmsg = NULL; |
| 396 |
err = mobilebackup_receive(client, &dlmsg); |
| 397 |
if ((err != MOBILEBACKUP_E_SUCCESS) || !dlmsg || (plist_get_node_type(dlmsg) != PLIST_ARRAY) || (plist_array_get_size(dlmsg) != 2)) { |
| 398 |
if (dlmsg) { |
| 399 |
debug_info("ERROR: Did not receive DLMessageDisconnect:"); |
| 400 |
debug_plist(dlmsg); |
| 401 |
plist_free(dlmsg); |
| 402 |
} |
| 403 |
if (err == MOBILEBACKUP_E_SUCCESS) { |
| 404 |
err = MOBILEBACKUP_E_PLIST_ERROR; |
| 405 |
} |
| 406 |
return err; |
| 407 |
} |
| 408 |
plist_t node = plist_array_get_item (dlmsg, 0); |
| 409 |
char *msg = NULL; |
| 410 |
if (node && (plist_get_node_type(node) == PLIST_STRING)) { |
| 411 |
plist_get_string_val(node, &msg); |
| 412 |
} |
| 413 |
|
| 414 |
if (msg && !strcmp(msg, "DLMessageDisconnect")) { |
| 415 |
err = MOBILEBACKUP_E_SUCCESS; |
| 416 |
/* we need to do this here, otherwise mobilebackup_client_free |
| 417 |
will fail */ |
| 418 |
device_link_service_client_free(client->parent); |
| 419 |
client->parent = NULL; |
| 420 |
} else { |
| 421 |
debug_info("ERROR: Malformed plist received:"); |
| 422 |
debug_plist(dlmsg); |
| 423 |
err = MOBILEBACKUP_E_PLIST_ERROR; |
| 424 |
} |
| 425 |
|
| 426 |
plist_free(dlmsg); |
| 427 |
|
| 428 |
if (msg) |
| 429 |
free(msg); |
| 430 |
|
| 431 |
return err; |
| 432 |
} |
| 433 |
|
| 434 |
mobilebackup_error_t mobilebackup_send_error(mobilebackup_client_t client, const char *reason) |
| 435 |
{ |
| 436 |
if (!client || !client->parent || !reason) |
| 437 |
return MOBILEBACKUP_E_INVALID_ARG; |
| 438 |
|
| 439 |
mobilebackup_error_t err; |
| 440 |
|
| 441 |
/* construct error plist */ |
| 442 |
plist_t dict = plist_new_dict(); |
| 443 |
plist_dict_set_item(dict, "BackupErrorReasonKey", plist_new_string(reason)); |
| 444 |
|
| 445 |
err = mobilebackup_send_message(client, "BackupMessageError", dict); |
| 446 |
plist_free(dict); |
| 447 |
|
| 448 |
return err; |
| 449 |
} |