Debian Codegraph @

src/restore.c

raw
1 /*
2 * restore.c
3 * com.apple.mobile.restored service implementation.
4 *
5 * Copyright (c) 2010 Joshua Hill. 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 <errno.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <plist/plist.h>
29
30 #include "property_list_service.h"
31 #include "restore.h"
32 #include "idevice.h"
33 #include "common/debug.h"
34
35 #define RESULT_SUCCESS 0
36 #define RESULT_FAILURE 1
37
38 /**
39 * Internally used function for checking the result from restore's answer
40 * plist to a previously sent request.
41 *
42 * @param dict The plist to evaluate.
43 * @param query_match Name of the request to match.
44 *
45 * @return RESULT_SUCCESS when the result is 'Success',
46 * RESULT_FAILURE when the result is 'Failure',
47 * or a negative value if an error occurred during evaluation.
48 */
49 static int restored_check_result(plist_t dict)
50 {
51 int ret = -1;
52 plist_t result_node = plist_dict_get_item(dict, "Result");
53 if (!result_node) {
54 return ret;
55 }
56
57 plist_type result_type = plist_get_node_type(result_node);
58
59 if (result_type == PLIST_STRING) {
60
61 char *result_value = NULL;
62
63 plist_get_string_val(result_node, &result_value);
64
65 if (result_value) {
66 if (!strcmp(result_value, "Success")) {
67 ret = RESULT_SUCCESS;
68 } else if (!strcmp(result_value, "Failure")) {
69 ret = RESULT_FAILURE;
70 } else {
71 debug_info("ERROR: unknown result value '%s'", result_value);
72 }
73 }
74 if (result_value)
75 free(result_value);
76 }
77 return ret;
78 }
79
80 /**
81 * Adds a label key with the passed value to a plist dict node.
82 *
83 * @param plist The plist to add the key to
84 * @param label The value for the label key
85 *
86 */
87 static void plist_dict_add_label(plist_t plist, const char *label)
88 {
89 if (plist && label) {
90 if (plist_get_node_type(plist) == PLIST_DICT)
91 plist_dict_set_item(plist, "Label", plist_new_string(label));
92 }
93 }
94
95 static restored_error_t restored_error(property_list_service_error_t err)
96 {
97 switch (err) {
98 case PROPERTY_LIST_SERVICE_E_SUCCESS:
99 return RESTORE_E_SUCCESS;
100 case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
101 return RESTORE_E_INVALID_ARG;
102 case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
103 return RESTORE_E_PLIST_ERROR;
104 case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
105 return RESTORE_E_MUX_ERROR;
106 case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT:
107 return RESTORE_E_RECEIVE_TIMEOUT;
108 default:
109 break;
110 }
111 return RESTORE_E_UNKNOWN_ERROR;
112 }
113
114 restored_error_t restored_client_free(restored_client_t client)
115 {
116 if (!client)
117 return RESTORE_E_INVALID_ARG;
118
119 restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
120
121 if (client->parent) {
122 restored_goodbye(client);
123
124 ret = restored_error(property_list_service_client_free(client->parent));
125 }
126
127 if (client->udid) {
128 free(client->udid);
129 }
130 if (client->label) {
131 free(client->label);
132 }
133
134 if (client->info) {
135 plist_free(client->info);
136 }
137
138 free(client);
139 return ret;
140 }
141
142 void restored_client_set_label(restored_client_t client, const char *label)
143 {
144 if (client) {
145 if (client->label)
146 free(client->label);
147
148 client->label = (label != NULL) ? strdup(label): NULL;
149 }
150 }
151
152 restored_error_t restored_receive(restored_client_t client, plist_t *plist)
153 {
154 if (!client || !plist || (plist && *plist))
155 return RESTORE_E_INVALID_ARG;
156
157 return restored_error(property_list_service_receive_plist(client->parent, plist));
158 }
159
160 restored_error_t restored_send(restored_client_t client, plist_t plist)
161 {
162 if (!client || !plist)
163 return RESTORE_E_INVALID_ARG;
164
165 return restored_error(property_list_service_send_xml_plist(client->parent, plist));
166 }
167
168 restored_error_t restored_query_type(restored_client_t client, char **type, uint64_t *version)
169 {
170 if (!client)
171 return RESTORE_E_INVALID_ARG;
172
173 restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
174
175 plist_t dict = plist_new_dict();
176 plist_dict_add_label(dict, client->label);
177 plist_dict_set_item(dict,"Request", plist_new_string("QueryType"));
178
179 debug_info("called");
180 ret = restored_send(client, dict);
181
182 plist_free(dict);
183 dict = NULL;
184
185 ret = restored_receive(client, &dict);
186
187 if (RESTORE_E_SUCCESS != ret)
188 return ret;
189
190 ret = RESTORE_E_UNKNOWN_ERROR;
191 plist_t type_node = plist_dict_get_item(dict, "Type");
192 if (type_node && (plist_get_node_type(type_node) == PLIST_STRING)) {
193 char* typestr = NULL;
194
195 /* save our device information info */
196 client->info = dict;
197
198 plist_get_string_val(type_node, &typestr);
199 debug_info("success with type %s", typestr);
200 /* return the type if requested */
201 if (type) {
202 *type = typestr;
203 } else {
204 free(typestr);
205 }
206
207 /* fetch the restore protocol version */
208 if (version) {
209 plist_t version_node = plist_dict_get_item(dict, "RestoreProtocolVersion");
210 if (version_node && PLIST_UINT == plist_get_node_type(version_node)) {
211 plist_get_uint_val(version_node, version);
212 debug_info("restored protocol version %llu", *version);
213 } else {
214 return RESTORE_E_UNKNOWN_ERROR;
215 }
216 }
217 ret = RESTORE_E_SUCCESS;
218 } else {
219 debug_info("hmm. QueryType response does not contain a type?!");
220 debug_plist(dict);
221 plist_free(dict);
222 }
223
224 return ret;
225 }
226
227 restored_error_t restored_query_value(restored_client_t client, const char *key, plist_t *value)
228 {
229 if (!client || !key)
230 return RESTORE_E_INVALID_ARG;
231
232 plist_t dict = NULL;
233 restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
234
235 /* setup request plist */
236 dict = plist_new_dict();
237 plist_dict_add_label(dict, client->label);
238 if (key) {
239 plist_dict_set_item(dict,"QueryKey", plist_new_string(key));
240 }
241 plist_dict_set_item(dict,"Request", plist_new_string("QueryValue"));
242
243 /* send to device */
244 ret = restored_send(client, dict);
245
246 plist_free(dict);
247 dict = NULL;
248
249 if (ret != RESTORE_E_SUCCESS)
250 return ret;
251
252 /* Now get device's answer */
253 ret = restored_receive(client, &dict);
254 if (ret != RESTORE_E_SUCCESS)
255 return ret;
256
257 plist_t value_node = plist_dict_get_item(dict, key);
258 if (value_node) {
259 debug_info("has a value");
260 *value = plist_copy(value_node);
261 } else {
262 ret = RESTORE_E_PLIST_ERROR;
263 }
264
265 plist_free(dict);
266 return ret;
267 }
268
269 restored_error_t restored_get_value(restored_client_t client, const char *key, plist_t *value)
270 {
271 plist_t item;
272
273 if (!client || !value || (value && *value))
274 return RESTORE_E_INVALID_ARG;
275
276 if (!client->info)
277 return RESTORE_E_NOT_ENOUGH_DATA;
278
279 if (!key) {
280 *value = plist_copy(client->info);
281 return RESTORE_E_SUCCESS;
282 }
283
284 item = plist_dict_get_item(client->info, key);
285 if (!item) {
286 return RESTORE_E_PLIST_ERROR;
287 }
288
289 *value = plist_copy(item);
290
291 return RESTORE_E_SUCCESS;
292 }
293
294 restored_error_t restored_client_new(idevice_t device, restored_client_t *client, const char *label)
295 {
296 if (!client)
297 return RESTORE_E_INVALID_ARG;
298
299 restored_error_t ret = RESTORE_E_SUCCESS;
300 idevice_error_t idev_ret;
301
302 static struct lockdownd_service_descriptor service = {
303 .port = 0xf27e,
304 .ssl_enabled = 0
305 };
306
307 property_list_service_client_t plistclient = NULL;
308 ret = restored_error(property_list_service_client_new(device, (lockdownd_service_descriptor_t)&service, &plistclient));
309 if (ret != RESTORE_E_SUCCESS) {
310 debug_info("could not connect to restored (device %s)", device->udid);
311 return ret;
312 }
313
314 restored_client_t client_loc = (restored_client_t) malloc(sizeof(struct restored_client_private));
315 client_loc->parent = plistclient;
316 client_loc->udid = NULL;
317 client_loc->label = NULL;
318 client_loc->info = NULL;
319 if (label != NULL)
320 client_loc->label = strdup(label);
321
322 idev_ret = idevice_get_udid(device, &client_loc->udid);
323 if (IDEVICE_E_SUCCESS != idev_ret) {
324 debug_info("failed to get device udid.");
325 ret = RESTORE_E_UNKNOWN_ERROR;
326 }
327 debug_info("device udid: %s", client_loc->udid);
328
329 if (RESTORE_E_SUCCESS == ret) {
330 *client = client_loc;
331 } else {
332 restored_client_free(client_loc);
333 }
334
335 return ret;
336 }
337
338 restored_error_t restored_goodbye(restored_client_t client)
339 {
340 if (!client)
341 return RESTORE_E_INVALID_ARG;
342
343 restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
344
345 plist_t dict = plist_new_dict();
346 plist_dict_add_label(dict, client->label);
347 plist_dict_set_item(dict,"Request", plist_new_string("Goodbye"));
348
349 debug_info("called");
350
351 ret = restored_send(client, dict);
352 plist_free(dict);
353 dict = NULL;
354
355 ret = restored_receive(client, &dict);
356 if (!dict) {
357 debug_info("did not get goodbye response back");
358 return RESTORE_E_PLIST_ERROR;
359 }
360
361 if (restored_check_result(dict) == RESULT_SUCCESS) {
362 debug_info("success");
363 ret = RESTORE_E_SUCCESS;
364 }
365 plist_free(dict);
366 dict = NULL;
367 return ret;
368 }
369
370 restored_error_t restored_start_restore(restored_client_t client, plist_t options, uint64_t version)
371 {
372 if (!client)
373 return RESTORE_E_INVALID_ARG;
374
375 plist_t dict = NULL;
376 restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
377
378 dict = plist_new_dict();
379 plist_dict_add_label(dict, client->label);
380 plist_dict_set_item(dict,"Request", plist_new_string("StartRestore"));
381 if (options) {
382 plist_dict_set_item(dict, "RestoreOptions", plist_copy(options));
383 }
384 plist_dict_set_item(dict,"RestoreProtocolVersion", plist_new_uint(version));
385
386 /* send to device */
387 ret = restored_send(client, dict);
388 plist_free(dict);
389 dict = NULL;
390
391 return ret;
392 }
393
394 restored_error_t restored_reboot(restored_client_t client)
395 {
396 if (!client)
397 return RESTORE_E_INVALID_ARG;
398
399 plist_t dict = NULL;
400 restored_error_t ret = RESTORE_E_UNKNOWN_ERROR;
401
402 dict = plist_new_dict();
403 plist_dict_add_label(dict, client->label);
404 plist_dict_set_item(dict,"Request", plist_new_string("Reboot"));
405
406 /* send to device */
407 ret = restored_send(client, dict);
408 plist_free(dict);
409 dict = NULL;
410
411 if (RESTORE_E_SUCCESS != ret)
412 return ret;
413
414 ret = restored_receive(client, &dict);
415 if (RESTORE_E_SUCCESS != ret)
416 return ret;
417
418 if (!dict)
419 return RESTORE_E_PLIST_ERROR;
420
421 plist_free(dict);
422 dict = NULL;
423 return ret;
424 }