diff options
Diffstat (limited to 'src/mobilebackup2.c')
| -rw-r--r-- | src/mobilebackup2.c | 386 |
1 files changed, 386 insertions, 0 deletions
diff --git a/src/mobilebackup2.c b/src/mobilebackup2.c new file mode 100644 index 0000000..a8d673f --- /dev/null +++ b/src/mobilebackup2.c | |||
| @@ -0,0 +1,386 @@ | |||
| 1 | /* | ||
| 2 | * mobilebackup2.c | ||
| 3 | * Contains functions for the built-in MobileBackup2 client (iOS4+ only) | ||
| 4 | * | ||
| 5 | * Copyright (c) 2010-2019 Nikias Bassen, 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 | #ifdef HAVE_CONFIG_H | ||
| 23 | #include <config.h> | ||
| 24 | #endif | ||
| 25 | #include <plist/plist.h> | ||
| 26 | #include <string.h> | ||
| 27 | #include <stdlib.h> | ||
| 28 | |||
| 29 | #include "mobilebackup2.h" | ||
| 30 | #include "device_link_service.h" | ||
| 31 | #include "common/debug.h" | ||
| 32 | |||
| 33 | #define MBACKUP2_VERSION_INT1 400 | ||
| 34 | #define MBACKUP2_VERSION_INT2 0 | ||
| 35 | |||
| 36 | #define IS_FLAG_SET(x, y) (((x) & (y)) == (y)) | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Convert an device_link_service_error_t value to an mobilebackup2_error_t value. | ||
| 40 | * Used internally to get correct error codes from the underlying | ||
| 41 | * device_link_service. | ||
| 42 | * | ||
| 43 | * @param err An device_link_service_error_t error code | ||
| 44 | * | ||
| 45 | * @return A matching mobilebackup2_error_t error code, | ||
| 46 | * MOBILEBACKUP2_E_UNKNOWN_ERROR otherwise. | ||
| 47 | */ | ||
| 48 | static mobilebackup2_error_t mobilebackup2_error(device_link_service_error_t err) | ||
| 49 | { | ||
| 50 | switch (err) { | ||
| 51 | case DEVICE_LINK_SERVICE_E_SUCCESS: | ||
| 52 | return MOBILEBACKUP2_E_SUCCESS; | ||
| 53 | case DEVICE_LINK_SERVICE_E_INVALID_ARG: | ||
| 54 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 55 | case DEVICE_LINK_SERVICE_E_PLIST_ERROR: | ||
| 56 | return MOBILEBACKUP2_E_PLIST_ERROR; | ||
| 57 | case DEVICE_LINK_SERVICE_E_MUX_ERROR: | ||
| 58 | return MOBILEBACKUP2_E_MUX_ERROR; | ||
| 59 | case DEVICE_LINK_SERVICE_E_SSL_ERROR: | ||
| 60 | return MOBILEBACKUP2_E_SSL_ERROR; | ||
| 61 | case DEVICE_LINK_SERVICE_E_RECEIVE_TIMEOUT: | ||
| 62 | return MOBILEBACKUP2_E_RECEIVE_TIMEOUT; | ||
| 63 | case DEVICE_LINK_SERVICE_E_BAD_VERSION: | ||
| 64 | return MOBILEBACKUP2_E_BAD_VERSION; | ||
| 65 | default: | ||
| 66 | break; | ||
| 67 | } | ||
| 68 | return MOBILEBACKUP2_E_UNKNOWN_ERROR; | ||
| 69 | } | ||
| 70 | |||
| 71 | mobilebackup2_error_t mobilebackup2_client_new(idevice_t device, lockdownd_service_descriptor_t service, | ||
| 72 | mobilebackup2_client_t * client) | ||
| 73 | { | ||
| 74 | if (!device || !service || service->port == 0 || !client || *client) | ||
| 75 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 76 | |||
| 77 | device_link_service_client_t dlclient = NULL; | ||
| 78 | mobilebackup2_error_t ret = mobilebackup2_error(device_link_service_client_new(device, service, &dlclient)); | ||
| 79 | if (ret != MOBILEBACKUP2_E_SUCCESS) { | ||
| 80 | return ret; | ||
| 81 | } | ||
| 82 | |||
| 83 | mobilebackup2_client_t client_loc = (mobilebackup2_client_t) malloc(sizeof(struct mobilebackup2_client_private)); | ||
| 84 | client_loc->parent = dlclient; | ||
| 85 | |||
| 86 | /* perform handshake */ | ||
| 87 | ret = mobilebackup2_error(device_link_service_version_exchange(dlclient, MBACKUP2_VERSION_INT1, MBACKUP2_VERSION_INT2)); | ||
| 88 | if (ret != MOBILEBACKUP2_E_SUCCESS) { | ||
| 89 | debug_info("version exchange failed, error %d", ret); | ||
| 90 | mobilebackup2_client_free(client_loc); | ||
| 91 | return ret; | ||
| 92 | } | ||
| 93 | |||
| 94 | *client = client_loc; | ||
| 95 | |||
| 96 | return ret; | ||
| 97 | } | ||
| 98 | |||
| 99 | mobilebackup2_error_t mobilebackup2_client_start_service(idevice_t device, mobilebackup2_client_t * client, const char* label) | ||
| 100 | { | ||
| 101 | mobilebackup2_error_t err = MOBILEBACKUP2_E_UNKNOWN_ERROR; | ||
| 102 | service_client_factory_start_service(device, MOBILEBACKUP2_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(mobilebackup2_client_new), &err); | ||
| 103 | return err; | ||
| 104 | } | ||
| 105 | |||
| 106 | mobilebackup2_error_t mobilebackup2_client_free(mobilebackup2_client_t client) | ||
| 107 | { | ||
| 108 | if (!client) | ||
| 109 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 110 | mobilebackup2_error_t err = MOBILEBACKUP2_E_SUCCESS; | ||
| 111 | if (client->parent) { | ||
| 112 | device_link_service_disconnect(client->parent, NULL); | ||
| 113 | err = mobilebackup2_error(device_link_service_client_free(client->parent)); | ||
| 114 | } | ||
| 115 | free(client); | ||
| 116 | return err; | ||
| 117 | } | ||
| 118 | |||
| 119 | mobilebackup2_error_t mobilebackup2_send_message(mobilebackup2_client_t client, const char *message, plist_t options) | ||
| 120 | { | ||
| 121 | if (!client || !client->parent || (!message && !options)) | ||
| 122 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 123 | |||
| 124 | if (options && (plist_get_node_type(options) != PLIST_DICT)) { | ||
| 125 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 126 | } | ||
| 127 | |||
| 128 | mobilebackup2_error_t err; | ||
| 129 | |||
| 130 | if (message) { | ||
| 131 | plist_t dict = NULL; | ||
| 132 | if (options) { | ||
| 133 | dict = plist_copy(options); | ||
| 134 | } else { | ||
| 135 | dict = plist_new_dict(); | ||
| 136 | } | ||
| 137 | plist_dict_set_item(dict, "MessageName", plist_new_string(message)); | ||
| 138 | |||
| 139 | /* send it as DLMessageProcessMessage */ | ||
| 140 | err = mobilebackup2_error(device_link_service_send_process_message(client->parent, dict)); | ||
| 141 | plist_free(dict); | ||
| 142 | } else { | ||
| 143 | err = mobilebackup2_error(device_link_service_send_process_message(client->parent, options)); | ||
| 144 | } | ||
| 145 | if (err != MOBILEBACKUP2_E_SUCCESS) { | ||
| 146 | debug_info("ERROR: Could not send message '%s' (%d)!", message, err); | ||
| 147 | } | ||
| 148 | return err; | ||
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * Receives a plist from the device and checks if the value for the | ||
| 153 | * MessageName key matches the value passed in the message parameter. | ||
| 154 | * | ||
| 155 | * @param client The connected MobileBackup client to use. | ||
| 156 | * @param message The expected message to check. | ||
| 157 | * @param result Pointer to a plist_t that will be set to the received plist | ||
| 158 | * for further processing. The caller has to free it using plist_free(). | ||
| 159 | * Note that it will be set to NULL if the operation itself fails due to | ||
| 160 | * a communication or plist error. | ||
| 161 | * If this parameter is NULL, it will be ignored. | ||
| 162 | * | ||
| 163 | * @return MOBILEBACKUP2_E_SUCCESS on success, MOBILEBACKUP2_E_INVALID_ARG if | ||
| 164 | * client or message is invalid, MOBILEBACKUP2_E_REPLY_NOT_OK if the | ||
| 165 | * expected message could not be received, MOBILEBACKUP2_E_PLIST_ERROR if | ||
| 166 | * the received message is not a valid backup message plist (i.e. the | ||
| 167 | * MessageName key is not present), or MOBILEBACKUP2_E_MUX_ERROR | ||
| 168 | * if a communication error occurs. | ||
| 169 | */ | ||
| 170 | static mobilebackup2_error_t internal_mobilebackup2_receive_message(mobilebackup2_client_t client, const char *message, plist_t *result) | ||
| 171 | { | ||
| 172 | if (!client || !client->parent || !message) | ||
| 173 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 174 | |||
| 175 | if (result) | ||
| 176 | *result = NULL; | ||
| 177 | mobilebackup2_error_t err; | ||
| 178 | |||
| 179 | plist_t dict = NULL; | ||
| 180 | |||
| 181 | /* receive DLMessageProcessMessage */ | ||
| 182 | err = mobilebackup2_error(device_link_service_receive_process_message(client->parent, &dict)); | ||
| 183 | if (err != MOBILEBACKUP2_E_SUCCESS) { | ||
| 184 | goto leave; | ||
| 185 | } | ||
| 186 | |||
| 187 | plist_t node = plist_dict_get_item(dict, "MessageName"); | ||
| 188 | if (!node) { | ||
| 189 | debug_info("ERROR: MessageName key not found in plist!"); | ||
| 190 | err = MOBILEBACKUP2_E_PLIST_ERROR; | ||
| 191 | goto leave; | ||
| 192 | } | ||
| 193 | |||
| 194 | char *str = NULL; | ||
| 195 | plist_get_string_val(node, &str); | ||
| 196 | if (str && (strcmp(str, message) == 0)) { | ||
| 197 | err = MOBILEBACKUP2_E_SUCCESS; | ||
| 198 | } else { | ||
| 199 | debug_info("ERROR: MessageName value does not match '%s'!", message); | ||
| 200 | err = MOBILEBACKUP2_E_REPLY_NOT_OK; | ||
| 201 | } | ||
| 202 | if (str) | ||
| 203 | free(str); | ||
| 204 | |||
| 205 | if (result) { | ||
| 206 | *result = dict; | ||
| 207 | dict = NULL; | ||
| 208 | } | ||
| 209 | leave: | ||
| 210 | if (dict) { | ||
| 211 | plist_free(dict); | ||
| 212 | } | ||
| 213 | |||
| 214 | return err; | ||
| 215 | } | ||
| 216 | |||
| 217 | mobilebackup2_error_t mobilebackup2_receive_message(mobilebackup2_client_t client, plist_t *msg_plist, char **dlmessage) | ||
| 218 | { | ||
| 219 | return mobilebackup2_error(device_link_service_receive_message(client->parent, msg_plist, dlmessage)); | ||
| 220 | } | ||
| 221 | |||
| 222 | mobilebackup2_error_t mobilebackup2_send_raw(mobilebackup2_client_t client, const char *data, uint32_t length, uint32_t *bytes) | ||
| 223 | { | ||
| 224 | if (!client || !client->parent || !data || (length == 0) || !bytes) | ||
| 225 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 226 | |||
| 227 | *bytes = 0; | ||
| 228 | |||
| 229 | service_client_t raw = client->parent->parent->parent; | ||
| 230 | |||
| 231 | int bytes_loc = 0; | ||
| 232 | uint32_t sent = 0; | ||
| 233 | do { | ||
| 234 | bytes_loc = 0; | ||
| 235 | service_send(raw, data+sent, length-sent, (uint32_t*)&bytes_loc); | ||
| 236 | if (bytes_loc <= 0) | ||
| 237 | break; | ||
| 238 | sent += bytes_loc; | ||
| 239 | } while (sent < length); | ||
| 240 | if (sent > 0) { | ||
| 241 | *bytes = sent; | ||
| 242 | return MOBILEBACKUP2_E_SUCCESS; | ||
| 243 | } | ||
| 244 | return MOBILEBACKUP2_E_MUX_ERROR; | ||
| 245 | } | ||
| 246 | |||
| 247 | mobilebackup2_error_t mobilebackup2_receive_raw(mobilebackup2_client_t client, char *data, uint32_t length, uint32_t *bytes) | ||
| 248 | { | ||
| 249 | if (!client || !client->parent || !data || (length == 0) || !bytes) | ||
| 250 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 251 | |||
| 252 | service_client_t raw = client->parent->parent->parent; | ||
| 253 | |||
| 254 | *bytes = 0; | ||
| 255 | |||
| 256 | int bytes_loc = 0; | ||
| 257 | uint32_t received = 0; | ||
| 258 | do { | ||
| 259 | bytes_loc = 0; | ||
| 260 | service_receive(raw, data+received, length-received, (uint32_t*)&bytes_loc); | ||
| 261 | if (bytes_loc <= 0) break; | ||
| 262 | received += bytes_loc; | ||
| 263 | } while (received < length); | ||
| 264 | if (received > 0) { | ||
| 265 | *bytes = received; | ||
| 266 | return MOBILEBACKUP2_E_SUCCESS; | ||
| 267 | } | ||
| 268 | if (received == 0) { | ||
| 269 | return MOBILEBACKUP2_E_SUCCESS; | ||
| 270 | } | ||
| 271 | return MOBILEBACKUP2_E_MUX_ERROR; | ||
| 272 | } | ||
| 273 | |||
| 274 | mobilebackup2_error_t mobilebackup2_version_exchange(mobilebackup2_client_t client, double local_versions[], char count, double *remote_version) | ||
| 275 | { | ||
| 276 | int i; | ||
| 277 | |||
| 278 | if (!client || !client->parent) | ||
| 279 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 280 | |||
| 281 | plist_t dict = plist_new_dict(); | ||
| 282 | plist_t array = plist_new_array(); | ||
| 283 | for (i = 0; i < count; i++) { | ||
| 284 | plist_array_append_item(array, plist_new_real(local_versions[i])); | ||
| 285 | } | ||
| 286 | plist_dict_set_item(dict, "SupportedProtocolVersions", array); | ||
| 287 | |||
| 288 | mobilebackup2_error_t err = mobilebackup2_send_message(client, "Hello", dict); | ||
| 289 | plist_free(dict); | ||
| 290 | |||
| 291 | if (err != MOBILEBACKUP2_E_SUCCESS) | ||
| 292 | goto leave; | ||
| 293 | |||
| 294 | dict = NULL; | ||
| 295 | err = internal_mobilebackup2_receive_message(client, "Response", &dict); | ||
| 296 | if (err != MOBILEBACKUP2_E_SUCCESS) | ||
| 297 | goto leave; | ||
| 298 | |||
| 299 | /* check if we received an error */ | ||
| 300 | plist_t node = plist_dict_get_item(dict, "ErrorCode"); | ||
| 301 | if (!node || (plist_get_node_type(node) != PLIST_UINT)) { | ||
| 302 | err = MOBILEBACKUP2_E_PLIST_ERROR; | ||
| 303 | goto leave; | ||
| 304 | } | ||
| 305 | |||
| 306 | uint64_t val = 0; | ||
| 307 | plist_get_uint_val(node, &val); | ||
| 308 | if (val != 0) { | ||
| 309 | if (val == 1) { | ||
| 310 | err = MOBILEBACKUP2_E_NO_COMMON_VERSION; | ||
| 311 | } else { | ||
| 312 | err = MOBILEBACKUP2_E_REPLY_NOT_OK; | ||
| 313 | } | ||
| 314 | goto leave; | ||
| 315 | } | ||
| 316 | |||
| 317 | /* retrieve the protocol version of the device */ | ||
| 318 | node = plist_dict_get_item(dict, "ProtocolVersion"); | ||
| 319 | if (!node || (plist_get_node_type(node) != PLIST_REAL)) { | ||
| 320 | err = MOBILEBACKUP2_E_PLIST_ERROR; | ||
| 321 | goto leave; | ||
| 322 | } | ||
| 323 | |||
| 324 | *remote_version = 0.0; | ||
| 325 | plist_get_real_val(node, remote_version); | ||
| 326 | leave: | ||
| 327 | if (dict) | ||
| 328 | plist_free(dict); | ||
| 329 | return err; | ||
| 330 | } | ||
| 331 | |||
| 332 | mobilebackup2_error_t mobilebackup2_send_request(mobilebackup2_client_t client, const char *request, const char *target_identifier, const char *source_identifier, plist_t options) | ||
| 333 | { | ||
| 334 | if (!client || !client->parent || !request || !target_identifier) | ||
| 335 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 336 | |||
| 337 | plist_t dict = plist_new_dict(); | ||
| 338 | plist_dict_set_item(dict, "TargetIdentifier", plist_new_string(target_identifier)); | ||
| 339 | if (source_identifier) { | ||
| 340 | plist_dict_set_item(dict, "SourceIdentifier", plist_new_string(source_identifier)); | ||
| 341 | } | ||
| 342 | if (options) { | ||
| 343 | plist_dict_set_item(dict, "Options", plist_copy(options)); | ||
| 344 | } | ||
| 345 | if (!strcmp(request, "Unback") && options) { | ||
| 346 | plist_t node = plist_dict_get_item(options, "Password"); | ||
| 347 | if (node) { | ||
| 348 | plist_dict_set_item(dict, "Password", plist_copy(node)); | ||
| 349 | } | ||
| 350 | } | ||
| 351 | if (!strcmp(request, "EnableCloudBackup") && options) { | ||
| 352 | plist_t node = plist_dict_get_item(options, "CloudBackupState"); | ||
| 353 | if (node) { | ||
| 354 | plist_dict_set_item(dict, "CloudBackupState", plist_copy(node)); | ||
| 355 | } | ||
| 356 | } | ||
| 357 | mobilebackup2_error_t err = mobilebackup2_send_message(client, request, dict); | ||
| 358 | plist_free(dict); | ||
| 359 | |||
| 360 | return err; | ||
| 361 | } | ||
| 362 | |||
| 363 | mobilebackup2_error_t mobilebackup2_send_status_response(mobilebackup2_client_t client, int status_code, const char *status1, plist_t status2) | ||
| 364 | { | ||
| 365 | if (!client || !client->parent) | ||
| 366 | return MOBILEBACKUP2_E_INVALID_ARG; | ||
| 367 | |||
| 368 | plist_t array = plist_new_array(); | ||
| 369 | plist_array_append_item(array, plist_new_string("DLMessageStatusResponse")); | ||
| 370 | plist_array_append_item(array, plist_new_uint(status_code)); | ||
| 371 | if (status1) { | ||
| 372 | plist_array_append_item(array, plist_new_string(status1)); | ||
| 373 | } else { | ||
| 374 | plist_array_append_item(array, plist_new_string("___EmptyParameterString___")); | ||
| 375 | } | ||
| 376 | if (status2) { | ||
| 377 | plist_array_append_item(array, plist_copy(status2)); | ||
| 378 | } else { | ||
| 379 | plist_array_append_item(array, plist_new_string("___EmptyParameterString___")); | ||
| 380 | } | ||
| 381 | |||
| 382 | mobilebackup2_error_t err = mobilebackup2_error(device_link_service_send(client->parent, array)); | ||
| 383 | plist_free(array); | ||
| 384 | |||
| 385 | return err; | ||
| 386 | } | ||
