3
* com.apple.mobile.restored service implementation.
5
* Copyright (c) 2010 Joshua Hill. All Rights Reserved.
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.
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.
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
22
#include <arpa/inet.h>
27
#include <plist/plist.h>
29
#include "property_list_service.h"
34
#define RESULT_SUCCESS 0
35
#define RESULT_FAILURE 1
38
* Internally used function for checking the result from restore's answer
39
* plist to a previously sent request.
41
* @param dict The plist to evaluate.
42
* @param query_match Name of the request to match.
44
* @return RESULT_SUCCESS when the result is 'Success',
45
* RESULT_FAILURE when the result is 'Failure',
46
* or a negative value if an error occured during evaluation.
48
static int restored_check_result(plist_t dict)
51
plist_t result_node = plist_dict_get_item(dict, "Result");
56
plist_type result_type = plist_get_node_type(result_node);
58
if (result_type == PLIST_STRING) {
60
char *result_value = NULL;
62
plist_get_string_val(result_node, &result_value);
65
if (!strcmp(result_value, "Success")) {
67
} else if (!strcmp(result_value, "Failure")) {
70
debug_info("ERROR: unknown result value '%s'", result_value);
80
* Adds a label key with the passed value to a plist dict node.
82
* @param plist The plist to add the key to
83
* @param label The value for the label key
86
static void plist_dict_add_label(plist_t plist, const char *label)
89
if (plist_get_node_type(plist) == PLIST_DICT)
90
plist_dict_insert_item(plist, "Label", plist_new_string(label));
95
* Closes the restored client session if one is running and frees up the
96
* restored_client struct.
98
* @param client The restore client
100
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
102
restored_error_t restored_client_free(restored_client_t client)
105
return RESTORE_E_INVALID_ARG;
107
restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
109
if (client->parent) {
110
restored_goodbye(client);
112
if (property_list_service_client_free(client->parent) == PROPERTY_LIST_SERVICE_E_SUCCESS) {
113
ret = RESTORE_E_SUCCESS;
125
plist_free(client->info);
133
* Sets the label to send for requests to restored.
135
* @param client The restore client
136
* @param label The label to set or NULL to disable sending a label
139
void restored_client_set_label(restored_client_t client, const char *label)
145
client->label = (label != NULL) ? strdup(label): NULL;
150
* Receives a plist from restored.
152
* @param client The restored client
153
* @param plist The plist to store the received data
155
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG when client or
158
restored_error_t restored_receive(restored_client_t client, plist_t *plist)
160
if (!client || !plist || (plist && *plist))
161
return RESTORE_E_INVALID_ARG;
163
restored_error_t ret = RESTORE_E_SUCCESS;
164
property_list_service_error_t err;
166
err = property_list_service_receive_plist(client->parent, plist);
167
if (err != PROPERTY_LIST_SERVICE_E_SUCCESS) {
168
ret = RESTORE_E_UNKNOWN_ERROR;
172
ret = RESTORE_E_PLIST_ERROR;
178
* Sends a plist to restored.
180
* @note This function is low-level and should only be used if you need to send
181
* a new type of message.
183
* @param client The restored client
184
* @param plist The plist to send
186
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG when client or
189
restored_error_t restored_send(restored_client_t client, plist_t plist)
191
if (!client || !plist)
192
return RESTORE_E_INVALID_ARG;
194
restored_error_t ret = RESTORE_E_SUCCESS;
197
err = property_list_service_send_xml_plist(client->parent, plist);
198
if (err != PROPERTY_LIST_SERVICE_E_SUCCESS) {
199
ret = RESTORE_E_UNKNOWN_ERROR;
205
* Query the type of the service daemon. Depending on whether the device is
206
* queried in normal mode or restore mode, different types will be returned.
208
* @param client The restored client
209
* @param type The type returned by the service daemon. Pass NULL to ignore.
210
* @param version The restore protocol version. Pass NULL to ignore.
212
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
214
restored_error_t restored_query_type(restored_client_t client, char **type, uint64_t *version)
217
return RESTORE_E_INVALID_ARG;
219
restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
221
plist_t dict = plist_new_dict();
222
plist_dict_add_label(dict, client->label);
223
plist_dict_insert_item(dict,"Request", plist_new_string("QueryType"));
225
debug_info("called");
226
ret = restored_send(client, dict);
231
ret = restored_receive(client, &dict);
233
if (RESTORE_E_SUCCESS != ret)
236
ret = RESTORE_E_UNKNOWN_ERROR;
237
if (restored_check_result(dict) == RESULT_SUCCESS) {
238
/* save our device information info */
241
/* return the type if requested */
243
plist_t type_node = plist_dict_get_item(dict, "Type");
244
if (type_node && PLIST_STRING == plist_get_node_type(type_node)) {
245
plist_get_string_val(type_node, type);
246
debug_info("success with type %s", *type);
247
ret = RESTORE_E_SUCCESS;
249
return RESTORE_E_UNKNOWN_ERROR;
253
/* fetch the restore protocol version */
255
plist_t version_node = plist_dict_get_item(dict, "RestoreProtocolVersion");
256
if (version_node && PLIST_UINT == plist_get_node_type(version_node)) {
257
plist_get_uint_val(version_node, version);
258
debug_info("restored protocol version %llu", *version);
259
ret = RESTORE_E_SUCCESS;
261
return RESTORE_E_UNKNOWN_ERROR;
264
ret = RESTORE_E_SUCCESS;
271
* Retrieves a value from information plist specified by a key.
273
* @param client An initialized restored client.
274
* @param key The key name to request or NULL to query for all keys
275
* @param value A plist node representing the result value node
277
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL, RESTORE_E_PLIST_ERROR if value for key can't be found
279
restored_error_t restored_get_value(restored_client_t client, const char *key, plist_t *value)
281
if (!client || !value || (value && *value))
282
return RESTORE_E_INVALID_ARG;
285
return RESTORE_E_NOT_ENOUGH_DATA;
287
restored_error_t ret = RESTORE_E_SUCCESS;
291
*value = plist_copy(client->info);
292
return RESTORE_E_SUCCESS;
294
item = plist_dict_get_item(client->info, key);
298
*value = plist_copy(item);
300
ret = RESTORE_E_PLIST_ERROR;
307
* Creates a new restored client for the device.
309
* @param device The device to create a restored client for
310
* @param client The pointer to the location of the new restored_client
311
* @param label The label to use for communication. Usually the program name.
313
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
315
restored_error_t restored_client_new(idevice_t device, restored_client_t *client, const char *label)
318
return RESTORE_E_INVALID_ARG;
320
restored_error_t ret = RESTORE_E_SUCCESS;
322
property_list_service_client_t plistclient = NULL;
323
if (property_list_service_client_new(device, 0xf27e, &plistclient) != PROPERTY_LIST_SERVICE_E_SUCCESS) {
324
debug_info("could not connect to restored (device %s)", device->uuid);
325
return RESTORE_E_MUX_ERROR;
328
restored_client_t client_loc = (restored_client_t) malloc(sizeof(struct restored_client_private));
329
client_loc->parent = plistclient;
330
client_loc->uuid = NULL;
331
client_loc->label = NULL;
333
client_loc->label = strdup(label);
335
ret = idevice_get_uuid(device, &client_loc->uuid);
336
if (RESTORE_E_SUCCESS != ret) {
337
debug_info("failed to get device uuid.");
339
debug_info("device uuid: %s", client_loc->uuid);
341
if (RESTORE_E_SUCCESS == ret) {
342
*client = client_loc;
344
restored_client_free(client_loc);
351
* Sends the Goodbye request to restored signaling the end of communication.
353
* @param client The restore client
355
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL,
356
* RESTORE_E_PLIST_ERROR if the device did not acknowledge the request
358
restored_error_t restored_goodbye(restored_client_t client)
361
return RESTORE_E_INVALID_ARG;
363
restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
365
plist_t dict = plist_new_dict();
366
plist_dict_add_label(dict, client->label);
367
plist_dict_insert_item(dict,"Request", plist_new_string("Goodbye"));
369
debug_info("called");
371
ret = restored_send(client, dict);
375
ret = restored_receive(client, &dict);
377
debug_info("did not get goodbye response back");
378
return RESTORE_E_PLIST_ERROR;
381
if (restored_check_result(dict) == RESULT_SUCCESS) {
382
debug_info("success");
383
ret = RESTORE_E_SUCCESS;
391
* Requests to start a restore and retrieve it's port on success.
393
* @param client The restored client
395
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG if a parameter
396
* is NULL, RESTORE_E_START_RESTORE_FAILED if the request fails
398
restored_error_t restored_start_restore(restored_client_t client)
401
return RESTORE_E_INVALID_ARG;
404
restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
406
dict = plist_new_dict();
407
plist_dict_add_label(dict, client->label);
408
plist_dict_insert_item(dict,"Request", plist_new_string("StartRestore"));
409
plist_dict_insert_item(dict,"RestoreProtocolVersion", plist_new_uint(2));
412
ret = restored_send(client, dict);
420
* Requests device to reboot.
422
* @param client The restored client
424
* @return RESTORE_E_SUCCESS on success, NP_E_INVALID_ARG if a parameter
427
restored_error_t restored_reboot(restored_client_t client)
430
return RESTORE_E_INVALID_ARG;
433
restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
435
dict = plist_new_dict();
436
plist_dict_add_label(dict, client->label);
437
plist_dict_insert_item(dict,"Request", plist_new_string("Reboot"));
440
ret = restored_send(client, dict);
444
if (RESTORE_E_SUCCESS != ret)
447
ret = restored_receive(client, &dict);
448
if (RESTORE_E_SUCCESS != ret)
452
return RESTORE_E_PLIST_ERROR;