Debian Codegraph @

src/lockdown.c

raw
1 /*
2 * lockdown.c
3 * com.apple.mobile.lockdownd service implementation.
4 *
5 * Copyright (c) 2009-2015 Martin Szulecki All Rights Reserved.
6 * Copyright (c) 2014-2015 Nikias Bassen All Rights Reserved.
7 * Copyright (c) 2010 Bryan Forbes All Rights Reserved.
8 * Copyright (c) 2008 Zach C. All Rights Reserved.
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30 #include <stdlib.h>
31 #define _GNU_SOURCE 1
32 #define __USE_GNU 1
33 #include <stdio.h>
34 #include <ctype.h>
35
36 #ifndef _MSC_VER
37 #include <unistd.h>
38 #endif
39
40 #include <plist/plist.h>
41 #include <libimobiledevice-glue/utils.h>
42
43 #include "property_list_service.h"
44 #include "lockdown.h"
45 #include "idevice.h"
46 #include "common/debug.h"
47 #include "common/userpref.h"
48 #include "asprintf.h"
49
50 #ifdef _WIN32
51 #include <windows.h>
52 #define sleep(x) Sleep(x*1000)
53 #endif
54
55 struct st_lockdownd_error_str_map {
56 const char *lockdown_errstr;
57 const char *errstr;
58 lockdownd_error_t errcode;
59 };
60
61 static struct st_lockdownd_error_str_map lockdownd_error_str_map[] = {
62 { "InvalidResponse", "Invalid response", LOCKDOWN_E_INVALID_RESPONSE },
63 { "MissingKey", "Missing key", LOCKDOWN_E_MISSING_KEY },
64 { "MissingValue", "Missing value", LOCKDOWN_E_MISSING_VALUE },
65 { "GetProhibited", "Get value prohibited", LOCKDOWN_E_GET_PROHIBITED },
66 { "SetProhibited", "Set value prohibited", LOCKDOWN_E_SET_PROHIBITED },
67 { "RemoveProhibited", "Remove value prohibited", LOCKDOWN_E_REMOVE_PROHIBITED },
68 { "ImmutableValue", "Immutable value", LOCKDOWN_E_IMMUTABLE_VALUE },
69 { "PasswordProtected", "Password protected", LOCKDOWN_E_PASSWORD_PROTECTED },
70 { "UserDeniedPairing", "User denied pairing", LOCKDOWN_E_USER_DENIED_PAIRING },
71 { "PairingDialogResponsePending", "Pairing dialog response pending", LOCKDOWN_E_PAIRING_DIALOG_RESPONSE_PENDING },
72 { "MissingHostID", "Missing HostID", LOCKDOWN_E_MISSING_HOST_ID },
73 { "InvalidHostID", "Invalid HostID", LOCKDOWN_E_INVALID_HOST_ID },
74 { "SessionActive", "Session active", LOCKDOWN_E_SESSION_ACTIVE },
75 { "SessionInactive", "Session inactive", LOCKDOWN_E_SESSION_INACTIVE },
76 { "MissingSessionID", "Missing session ID", LOCKDOWN_E_MISSING_SESSION_ID },
77 { "InvalidSessionID", "Invalid session ID", LOCKDOWN_E_INVALID_SESSION_ID },
78 { "MissingService", "Missing service", LOCKDOWN_E_MISSING_SERVICE },
79 { "InvalidService", "Invalid service", LOCKDOWN_E_INVALID_SERVICE },
80 { "ServiceLimit", "Service limit reached", LOCKDOWN_E_SERVICE_LIMIT },
81 { "MissingPairRecord", "Missing pair record", LOCKDOWN_E_MISSING_PAIR_RECORD },
82 { "SavePairRecordFailed", "Saving pair record failed", LOCKDOWN_E_SAVE_PAIR_RECORD_FAILED },
83 { "InvalidPairRecord", "Invalid pair record", LOCKDOWN_E_INVALID_PAIR_RECORD },
84 { "InvalidActivationRecord", "Invalid activation record", LOCKDOWN_E_INVALID_ACTIVATION_RECORD },
85 { "MissingActivationRecord", "Missing activation record", LOCKDOWN_E_MISSING_ACTIVATION_RECORD },
86 { "ServiceProhibited", "Service prohibited", LOCKDOWN_E_SERVICE_PROHIBITED },
87 { "EscrowLocked", "Escrow lockded", LOCKDOWN_E_ESCROW_LOCKED },
88 { "PairingProhibitedOverThisConnection", "Pairing prohibited over this connection", LOCKDOWN_E_PAIRING_PROHIBITED_OVER_THIS_CONNECTION },
89 { "FMiPProtected", "Find My iPhone/iPod/iPad protected", LOCKDOWN_E_FMIP_PROTECTED },
90 { "MCProtected", "MC protected" , LOCKDOWN_E_MC_PROTECTED },
91 { "MCChallengeRequired", "MC challenge required", LOCKDOWN_E_MC_CHALLENGE_REQUIRED },
92 { NULL, NULL, 0 }
93 };
94
95 /**
96 * Convert an error string identifier to a lockdownd_error_t value.
97 * Used internally to get correct error codes from a response.
98 *
99 * @param name The error name to convert.
100 *
101 * @return A matching lockdownd_error_t error code,
102 * LOCKDOWN_E_UNKNOWN_ERROR otherwise.
103 */
104 static lockdownd_error_t lockdownd_strtoerr(const char* name)
105 {
106 lockdownd_error_t err = LOCKDOWN_E_UNKNOWN_ERROR;
107 int i = 0;
108 while (lockdownd_error_str_map[i].lockdown_errstr) {
109 if (strcmp(lockdownd_error_str_map[i].lockdown_errstr, name) == 0) {
110 return lockdownd_error_str_map[i].errcode;
111 }
112 i++;
113 }
114 return err;
115 }
116
117 /**
118 * Convert a property_list_service_error_t value to a lockdownd_error_t
119 * value. Used internally to get correct error codes.
120 *
121 * @param err A property_list_service_error_t error code
122 *
123 * @return A matching lockdownd_error_t error code,
124 * LOCKDOWND_E_UNKNOWN_ERROR otherwise.
125 */
126 static lockdownd_error_t lockdownd_error(property_list_service_error_t err)
127 {
128 switch (err) {
129 case PROPERTY_LIST_SERVICE_E_SUCCESS:
130 return LOCKDOWN_E_SUCCESS;
131 case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
132 return LOCKDOWN_E_INVALID_ARG;
133 case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
134 return LOCKDOWN_E_PLIST_ERROR;
135 case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
136 return LOCKDOWN_E_MUX_ERROR;
137 case PROPERTY_LIST_SERVICE_E_SSL_ERROR:
138 return LOCKDOWN_E_SSL_ERROR;
139 case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT:
140 return LOCKDOWN_E_RECEIVE_TIMEOUT;
141 default:
142 break;
143 }
144 return LOCKDOWN_E_UNKNOWN_ERROR;
145 }
146
147 /**
148 * Internally used function for checking the result from lockdown's answer
149 * plist to a previously sent request.
150 *
151 * @param dict The plist to evaluate.
152 * @param query_match Name of the request to match or NULL if no match is
153 * required.
154 *
155 * @return LOCKDOWN_E_SUCCESS when the result is 'Success',
156 * LOCKDOWN_E_UNKNOWN_ERROR when the result is 'Failure',
157 * or a specific error code if derieved from the result.
158 */
159 lockdownd_error_t lockdown_check_result(plist_t dict, const char *query_match)
160 {
161 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
162
163 plist_t query_node = plist_dict_get_item(dict, "Request");
164 if (!query_node) {
165 return ret;
166 }
167
168 if (plist_get_node_type(query_node) != PLIST_STRING) {
169 return ret;
170 }
171
172 const char *query_value = plist_get_string_ptr(query_node, NULL);
173 if (!query_value) {
174 return ret;
175 }
176
177 if (query_match && (strcmp(query_value, query_match) != 0)) {
178 return ret;
179 }
180
181 /* Check for 'Error' in reply */
182 plist_t err_node = plist_dict_get_item(dict, "Error");
183 if (err_node) {
184 if (plist_get_node_type(err_node) == PLIST_STRING) {
185 const char *err_value = plist_get_string_ptr(err_node, NULL);
186 if (err_value) {
187 debug_info("ERROR: %s", err_value);
188 ret = lockdownd_strtoerr(err_value);
189 } else {
190 debug_info("ERROR: unknown error occurred");
191 }
192 }
193 return ret;
194 }
195
196 plist_t result_node = plist_dict_get_item(dict, "Result");
197 if (!result_node) {
198 /* With iOS 5+ 'Result' is not present anymore.
199 If there is no 'Error', we can just assume success. */
200 return LOCKDOWN_E_SUCCESS;
201 }
202 if (plist_get_node_type(result_node) == PLIST_STRING) {
203 const char *result_value = plist_get_string_ptr(result_node, NULL);
204 if (result_value) {
205 if (!strcmp(result_value, "Success")) {
206 ret = LOCKDOWN_E_SUCCESS;
207 } else if (!strcmp(result_value, "Failure")) {
208 ret = LOCKDOWN_E_UNKNOWN_ERROR;
209 } else {
210 debug_info("ERROR: unknown result value '%s'", result_value);
211 }
212 }
213 }
214
215 return ret;
216 }
217
218 /**
219 * Adds a label key with the passed value to a plist dict node.
220 *
221 * @param plist The plist to add the key to
222 * @param label The value for the label key
223 *
224 */
225 static void plist_dict_add_label(plist_t plist, const char *label)
226 {
227 if (plist && label) {
228 if (plist_get_node_type(plist) == PLIST_DICT)
229 plist_dict_set_item(plist, "Label", plist_new_string(label));
230 }
231 }
232
233 lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char *session_id)
234 {
235 if (!client)
236 return LOCKDOWN_E_INVALID_ARG;
237
238 if (!session_id) {
239 debug_info("no session_id given, cannot stop session");
240 return LOCKDOWN_E_INVALID_ARG;
241 }
242
243 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
244
245 plist_t dict = plist_new_dict();
246 plist_dict_add_label(dict, client->label);
247 plist_dict_set_item(dict,"Request", plist_new_string("StopSession"));
248 plist_dict_set_item(dict,"SessionID", plist_new_string(session_id));
249
250 debug_info("stopping session %s", session_id);
251
252 ret = lockdownd_send(client, dict);
253
254 plist_free(dict);
255 dict = NULL;
256
257 ret = lockdownd_receive(client, &dict);
258
259 if (!dict) {
260 debug_info("LOCKDOWN_E_PLIST_ERROR");
261 return LOCKDOWN_E_PLIST_ERROR;
262 }
263
264 ret = lockdown_check_result(dict, "StopSession");
265 if (ret == LOCKDOWN_E_SUCCESS) {
266 debug_info("success");
267 }
268
269 plist_free(dict);
270 dict = NULL;
271
272 if (client->session_id) {
273 free(client->session_id);
274 client->session_id = NULL;
275 }
276
277 if (client->ssl_enabled) {
278 property_list_service_disable_ssl(client->parent);
279 client->ssl_enabled = 0;
280 }
281
282 return ret;
283 }
284
285 static lockdownd_error_t lockdownd_client_free_simple(lockdownd_client_t client)
286 {
287 if (!client)
288 return LOCKDOWN_E_INVALID_ARG;
289
290 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
291
292 if (client->parent) {
293 if (property_list_service_client_free(client->parent) == PROPERTY_LIST_SERVICE_E_SUCCESS) {
294 ret = LOCKDOWN_E_SUCCESS;
295 }
296 }
297
298 if (client->session_id) {
299 free(client->session_id);
300 client->session_id = NULL;
301 }
302 if (client->label) {
303 free(client->label);
304 }
305 if (client->cu_key) {
306 free(client->cu_key);
307 client->cu_key = NULL;
308 }
309
310 free(client);
311 client = NULL;
312
313 return ret;
314 }
315
316 lockdownd_error_t lockdownd_client_free(lockdownd_client_t client)
317 {
318 if (!client)
319 return LOCKDOWN_E_INVALID_ARG;
320
321 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
322
323 if (client->session_id) {
324 lockdownd_stop_session(client, client->session_id);
325 }
326
327 ret = lockdownd_client_free_simple(client);
328
329 return ret;
330 }
331
332 void lockdownd_client_set_label(lockdownd_client_t client, const char *label)
333 {
334 if (client) {
335 if (client->label)
336 free(client->label);
337
338 client->label = (label != NULL) ? strdup(label): NULL;
339 }
340 }
341
342 lockdownd_error_t lockdownd_receive(lockdownd_client_t client, plist_t *plist)
343 {
344 if (!client || !plist || (plist && *plist))
345 return LOCKDOWN_E_INVALID_ARG;
346
347 return lockdownd_error(property_list_service_receive_plist(client->parent, plist));
348 }
349
350 lockdownd_error_t lockdownd_send(lockdownd_client_t client, plist_t plist)
351 {
352 if (!client || !plist)
353 return LOCKDOWN_E_INVALID_ARG;
354
355 return lockdownd_error(property_list_service_send_xml_plist(client->parent, plist));
356 }
357
358 lockdownd_error_t lockdownd_query_type(lockdownd_client_t client, char **type)
359 {
360 if (!client)
361 return LOCKDOWN_E_INVALID_ARG;
362
363 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
364
365 plist_t dict = plist_new_dict();
366 plist_dict_add_label(dict, client->label);
367 plist_dict_set_item(dict,"Request", plist_new_string("QueryType"));
368
369 debug_info("called");
370 ret = lockdownd_send(client, dict);
371
372 plist_free(dict);
373 dict = NULL;
374
375 ret = lockdownd_receive(client, &dict);
376
377 if (LOCKDOWN_E_SUCCESS != ret)
378 return ret;
379
380 ret = LOCKDOWN_E_UNKNOWN_ERROR;
381 plist_t type_node = plist_dict_get_item(dict, "Type");
382 if (type_node && (plist_get_node_type(type_node) == PLIST_STRING)) {
383 char* typestr = NULL;
384 plist_get_string_val(type_node, &typestr);
385 debug_info("success with type %s", typestr);
386 /* return the type if requested */
387 if (type != NULL) {
388 *type = typestr;
389 } else {
390 free(typestr);
391 }
392 ret = LOCKDOWN_E_SUCCESS;
393 } else {
394 debug_info("hmm. QueryType response does not contain a type?!");
395 debug_plist(dict);
396 }
397 plist_free(dict);
398 dict = NULL;
399
400 return ret;
401 }
402
403 lockdownd_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain, const char *key, plist_t *value)
404 {
405 if (!client)
406 return LOCKDOWN_E_INVALID_ARG;
407
408 plist_t dict = NULL;
409 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
410
411 /* setup request plist */
412 dict = plist_new_dict();
413 plist_dict_add_label(dict, client->label);
414 if (domain) {
415 plist_dict_set_item(dict,"Domain", plist_new_string(domain));
416 }
417 if (key) {
418 plist_dict_set_item(dict,"Key", plist_new_string(key));
419 }
420 plist_dict_set_item(dict,"Request", plist_new_string("GetValue"));
421
422 /* send to device */
423 ret = lockdownd_send(client, dict);
424
425 plist_free(dict);
426 dict = NULL;
427
428 if (ret != LOCKDOWN_E_SUCCESS)
429 return ret;
430
431 /* Now get device's answer */
432 ret = lockdownd_receive(client, &dict);
433 if (ret != LOCKDOWN_E_SUCCESS)
434 return ret;
435
436 ret = lockdown_check_result(dict, "GetValue");
437 if (ret == LOCKDOWN_E_SUCCESS) {
438 debug_info("success");
439 }
440
441 if (ret != LOCKDOWN_E_SUCCESS) {
442 plist_free(dict);
443 return ret;
444 }
445
446 plist_t value_node = plist_dict_get_item(dict, "Value");
447
448 if (value_node) {
449 debug_info("has a value");
450 *value = plist_copy(value_node);
451 }
452
453 plist_free(dict);
454 return ret;
455 }
456
457 lockdownd_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain, const char *key, plist_t value)
458 {
459 if (!client || !value)
460 return LOCKDOWN_E_INVALID_ARG;
461
462 plist_t dict = NULL;
463 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
464
465 /* setup request plist */
466 dict = plist_new_dict();
467 plist_dict_add_label(dict, client->label);
468 if (domain) {
469 plist_dict_set_item(dict,"Domain", plist_new_string(domain));
470 }
471 if (key) {
472 plist_dict_set_item(dict,"Key", plist_new_string(key));
473 }
474 plist_dict_set_item(dict,"Request", plist_new_string("SetValue"));
475 plist_dict_set_item(dict,"Value", value);
476
477 /* send to device */
478 ret = lockdownd_send(client, dict);
479
480 plist_free(dict);
481 dict = NULL;
482
483 if (ret != LOCKDOWN_E_SUCCESS)
484 return ret;
485
486 /* Now get device's answer */
487 ret = lockdownd_receive(client, &dict);
488 if (ret != LOCKDOWN_E_SUCCESS)
489 return ret;
490
491 ret = lockdown_check_result(dict, "SetValue");
492 if (ret == LOCKDOWN_E_SUCCESS) {
493 debug_info("success");
494 }
495
496 if (ret != LOCKDOWN_E_SUCCESS) {
497 plist_free(dict);
498 return ret;
499 }
500
501 plist_free(dict);
502 return ret;
503 }
504
505 lockdownd_error_t lockdownd_remove_value(lockdownd_client_t client, const char *domain, const char *key)
506 {
507 if (!client)
508 return LOCKDOWN_E_INVALID_ARG;
509
510 plist_t dict = NULL;
511 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
512
513 /* setup request plist */
514 dict = plist_new_dict();
515 plist_dict_add_label(dict, client->label);
516 if (domain) {
517 plist_dict_set_item(dict,"Domain", plist_new_string(domain));
518 }
519 if (key) {
520 plist_dict_set_item(dict,"Key", plist_new_string(key));
521 }
522 plist_dict_set_item(dict,"Request", plist_new_string("RemoveValue"));
523
524 /* send to device */
525 ret = lockdownd_send(client, dict);
526
527 plist_free(dict);
528 dict = NULL;
529
530 if (ret != LOCKDOWN_E_SUCCESS)
531 return ret;
532
533 /* Now get device's answer */
534 ret = lockdownd_receive(client, &dict);
535 if (ret != LOCKDOWN_E_SUCCESS)
536 return ret;
537
538 ret = lockdown_check_result(dict, "RemoveValue");
539 if (ret == LOCKDOWN_E_SUCCESS) {
540 debug_info("success");
541 }
542
543 if (ret != LOCKDOWN_E_SUCCESS) {
544 plist_free(dict);
545 return ret;
546 }
547
548 plist_free(dict);
549 return ret;
550 }
551
552 lockdownd_error_t lockdownd_get_device_udid(lockdownd_client_t client, char **udid)
553 {
554 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
555 plist_t value = NULL;
556
557 ret = lockdownd_get_value(client, NULL, "UniqueDeviceID", &value);
558 if (ret != LOCKDOWN_E_SUCCESS) {
559 return ret;
560 }
561 plist_get_string_val(value, udid);
562
563 plist_free(value);
564 value = NULL;
565 return ret;
566 }
567
568 /**
569 * Retrieves the public key of the device from lockdownd.
570 *
571 * @param client An initialized lockdownd client.
572 * @param public_key Holds the public key of the device. The caller is
573 * responsible for freeing the memory.
574 *
575 * @return LOCKDOWN_E_SUCCESS on success
576 */
577 static lockdownd_error_t lockdownd_get_device_public_key_as_key_data(lockdownd_client_t client, key_data_t *public_key)
578 {
579 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
580 plist_t value = NULL;
581 char *value_value = NULL;
582 uint64_t size = 0;
583
584 ret = lockdownd_get_value(client, NULL, "DevicePublicKey", &value);
585 if (ret != LOCKDOWN_E_SUCCESS) {
586 return ret;
587 }
588 plist_get_data_val(value, &value_value, &size);
589 public_key->data = (unsigned char*)value_value;
590 public_key->size = size;
591
592 plist_free(value);
593 value = NULL;
594
595 return ret;
596 }
597
598 lockdownd_error_t lockdownd_get_device_name(lockdownd_client_t client, char **device_name)
599 {
600 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
601 plist_t value = NULL;
602
603 ret = lockdownd_get_value(client, NULL, "DeviceName", &value);
604 if (ret != LOCKDOWN_E_SUCCESS) {
605 return ret;
606 }
607 plist_get_string_val(value, device_name);
608
609 plist_free(value);
610 value = NULL;
611
612 return ret;
613 }
614
615 lockdownd_error_t lockdownd_client_new(idevice_t device, lockdownd_client_t *client, const char *label)
616 {
617 if (!device || !client)
618 return LOCKDOWN_E_INVALID_ARG;
619
620 static struct lockdownd_service_descriptor service = {
621 .port = 0xf27e,
622 .ssl_enabled = 0
623 };
624 char *type = NULL;
625
626 property_list_service_client_t plistclient = NULL;
627 if (property_list_service_client_new(device, (lockdownd_service_descriptor_t)&service, &plistclient) != PROPERTY_LIST_SERVICE_E_SUCCESS) {
628 debug_info("could not connect to lockdownd (device %s)", device->udid);
629 return LOCKDOWN_E_MUX_ERROR;
630 }
631
632 lockdownd_client_t client_loc = (lockdownd_client_t) malloc(sizeof(struct lockdownd_client_private));
633 client_loc->parent = plistclient;
634 client_loc->ssl_enabled = 0;
635 client_loc->session_id = NULL;
636 client_loc->device = device;
637 client_loc->cu_key = NULL;
638 client_loc->cu_key_len = 0;
639
640 if (device->udid) {
641 debug_info("device udid: %s", device->udid);
642 }
643
644 client_loc->label = label ? strdup(label) : NULL;
645
646 int is_lockdownd = 0;
647 if (lockdownd_query_type(client_loc, &type) != LOCKDOWN_E_SUCCESS) {
648 debug_info("QueryType failed in the lockdownd client.");
649 } else if (!strcmp("com.apple.mobile.lockdown", type)) {
650 is_lockdownd = 1;
651 } else {
652 debug_info("QueryType request returned \"%s\"", type);
653 }
654 free(type);
655
656 *client = client_loc;
657
658 if (is_lockdownd && device->version == 0) {
659 plist_t p_version = NULL;
660 if (lockdownd_get_value(client_loc, NULL, "ProductVersion", &p_version) == LOCKDOWN_E_SUCCESS) {
661 int vers[3] = {0, 0, 0};
662 char *s_version = NULL;
663 plist_get_string_val(p_version, &s_version);
664 if (s_version && sscanf(s_version, "%d.%d.%d", &vers[0], &vers[1], &vers[2]) >= 2) {
665 device->version = IDEVICE_DEVICE_VERSION(vers[0], vers[1], vers[2]);
666 }
667 free(s_version);
668 }
669 plist_free(p_version);
670 }
671 if (is_lockdownd && device->device_class == 0) {
672 plist_t p_device_class = NULL;
673 if (lockdownd_get_value(client_loc, NULL, "DeviceClass", &p_device_class) == LOCKDOWN_E_SUCCESS) {
674 char* s_device_class = NULL;
675 plist_get_string_val(p_device_class, &s_device_class);
676 if (s_device_class != NULL) {
677 if (!strcmp(s_device_class, "iPhone")) {
678 device->device_class = DEVICE_CLASS_IPHONE;
679 } else if (!strcmp(s_device_class, "iPad")) {
680 device->device_class = DEVICE_CLASS_IPAD;
681 } else if (!strcmp(s_device_class, "iPod")) {
682 device->device_class = DEVICE_CLASS_IPOD;
683 } else if (!strcmp(s_device_class, "Watch")) {
684 device->device_class = DEVICE_CLASS_WATCH;
685 } else if (!strcmp(s_device_class, "AppleTV")) {
686 device->device_class = DEVICE_CLASS_APPLETV;
687 } else {
688 device->device_class = DEVICE_CLASS_UNKNOWN;
689 }
690 free(s_device_class);
691 }
692 }
693 plist_free(p_device_class);
694 }
695
696 return LOCKDOWN_E_SUCCESS;
697 }
698
699 lockdownd_error_t lockdownd_client_new_with_handshake(idevice_t device, lockdownd_client_t *client, const char *label)
700 {
701 if (!client)
702 return LOCKDOWN_E_INVALID_ARG;
703
704 lockdownd_error_t ret = LOCKDOWN_E_SUCCESS;
705 lockdownd_client_t client_loc = NULL;
706 plist_t pair_record = NULL;
707 char *host_id = NULL;
708
709 ret = lockdownd_client_new(device, &client_loc, label);
710 if (LOCKDOWN_E_SUCCESS != ret) {
711 debug_info("failed to create lockdownd client.");
712 return ret;
713 }
714
715 /* perform handshake */
716 userpref_error_t uerr = userpref_read_pair_record(client_loc->device->udid, &pair_record);
717 if (uerr == USERPREF_E_READ_ERROR) {
718 debug_info("ERROR: Failed to retrieve pair record for %s", client_loc->device->udid);
719 lockdownd_client_free(client_loc);
720 return LOCKDOWN_E_RECEIVE_TIMEOUT;
721 }
722 if (pair_record) {
723 pair_record_get_host_id(pair_record, &host_id);
724 }
725 if (LOCKDOWN_E_SUCCESS == ret && pair_record && !host_id) {
726 ret = LOCKDOWN_E_INVALID_CONF;
727 }
728
729 if (LOCKDOWN_E_SUCCESS == ret && !pair_record) {
730 /* attempt pairing */
731 free(host_id);
732 host_id = NULL;
733 ret = lockdownd_pair(client_loc, NULL);
734 }
735
736 plist_free(pair_record);
737 pair_record = NULL;
738
739 if (device->version < IDEVICE_DEVICE_VERSION(7,0,0) && device->device_class != DEVICE_CLASS_WATCH) {
740 /* for older devices, we need to validate pairing to receive trusted host status */
741 ret = lockdownd_validate_pair(client_loc, NULL);
742
743 /* if not paired yet, let's do it now */
744 if (LOCKDOWN_E_INVALID_HOST_ID == ret) {
745 free(host_id);
746 host_id = NULL;
747 ret = lockdownd_pair(client_loc, NULL);
748 if (LOCKDOWN_E_SUCCESS == ret) {
749 ret = lockdownd_validate_pair(client_loc, NULL);
750 }
751 }
752 }
753
754 if (LOCKDOWN_E_SUCCESS == ret) {
755 if (!host_id) {
756 uerr = userpref_read_pair_record(client_loc->device->udid, &pair_record);
757 if (uerr == USERPREF_E_READ_ERROR) {
758 debug_info("ERROR: Failed to retrieve pair record for %s", client_loc->device->udid);
759 lockdownd_client_free(client_loc);
760 return LOCKDOWN_E_RECEIVE_TIMEOUT;
761 } else if (uerr == USERPREF_E_NOENT) {
762 debug_info("ERROR: No pair record for %s", client_loc->device->udid);
763 lockdownd_client_free(client_loc);
764 return LOCKDOWN_E_INVALID_CONF;
765 } else if (uerr != USERPREF_E_SUCCESS) {
766 debug_info("ERROR: Failed to retrieve or parse pair record for %s", client_loc->device->udid);
767 lockdownd_client_free(client_loc);
768 return LOCKDOWN_E_INVALID_CONF;
769 }
770 if (pair_record) {
771 pair_record_get_host_id(pair_record, &host_id);
772 plist_free(pair_record);
773 }
774 }
775
776 ret = lockdownd_start_session(client_loc, host_id, NULL, NULL);
777 if (LOCKDOWN_E_SUCCESS != ret) {
778 debug_info("Session opening failed.");
779 }
780
781 }
782
783 if (LOCKDOWN_E_SUCCESS == ret) {
784 *client = client_loc;
785 } else {
786 lockdownd_client_free(client_loc);
787 }
788 free(host_id);
789 return ret;
790 }
791
792 /**
793 * Returns a new plist from the supplied lockdownd pair record. The caller is
794 * responsible for freeing the plist.
795 *
796 * @param pair_record The pair record to create a plist from.
797 *
798 * @return A pair record plist from the device, NULL if pair_record is not set
799 */
800 static plist_t lockdownd_pair_record_to_plist(lockdownd_pair_record_t pair_record)
801 {
802 if (!pair_record)
803 return NULL;
804
805 /* setup request plist */
806 plist_t dict = plist_new_dict();
807 plist_dict_set_item(dict, "DeviceCertificate", plist_new_data(pair_record->device_certificate, strlen(pair_record->device_certificate)));
808 plist_dict_set_item(dict, "HostCertificate", plist_new_data(pair_record->host_certificate, strlen(pair_record->host_certificate)));
809 plist_dict_set_item(dict, "HostID", plist_new_string(pair_record->host_id));
810 plist_dict_set_item(dict, "RootCertificate", plist_new_data(pair_record->root_certificate, strlen(pair_record->root_certificate)));
811 plist_dict_set_item(dict, "SystemBUID", plist_new_string(pair_record->system_buid));
812
813 return dict;
814 }
815
816 /**
817 * Generates a pair record plist with required certificates for a specific
818 * device. If a pairing exists, it is loaded from the computer instead of being
819 * generated.
820 *
821 * @param pair_record_plist Holds the pair record.
822 *
823 * @return LOCKDOWN_E_SUCCESS on success
824 */
825 static lockdownd_error_t pair_record_generate(lockdownd_client_t client, plist_t *pair_record)
826 {
827 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
828
829 key_data_t public_key = { NULL, 0 };
830 char* host_id = NULL;
831 char* system_buid = NULL;
832
833 /* retrieve device public key */
834 ret = lockdownd_get_device_public_key_as_key_data(client, &public_key);
835 if (ret != LOCKDOWN_E_SUCCESS) {
836 debug_info("device refused to send public key.");
837 goto leave;
838 }
839 debug_info("device public key follows:\n%.*s", public_key.size, public_key.data);
840
841 *pair_record = plist_new_dict();
842
843 /* generate keys and certificates into pair record */
844 userpref_error_t uret = USERPREF_E_SUCCESS;
845 uret = pair_record_generate_keys_and_certs(*pair_record, public_key, client->device->version);
846 switch(uret) {
847 case USERPREF_E_INVALID_ARG:
848 ret = LOCKDOWN_E_INVALID_ARG;
849 break;
850 case USERPREF_E_INVALID_CONF:
851 ret = LOCKDOWN_E_INVALID_CONF;
852 break;
853 case USERPREF_E_SSL_ERROR:
854 ret = LOCKDOWN_E_SSL_ERROR;
855 break;
856 default:
857 break;
858 }
859
860 /* set SystemBUID */
861 userpref_read_system_buid(&system_buid);
862 if (system_buid) {
863 plist_dict_set_item(*pair_record, USERPREF_SYSTEM_BUID_KEY, plist_new_string(system_buid));
864 }
865
866 /* set HostID */
867 host_id = generate_uuid();
868 pair_record_set_host_id(*pair_record, host_id);
869
870 leave:
871 if (host_id)
872 free(host_id);
873 if (system_buid)
874 free(system_buid);
875 if (public_key.data)
876 free(public_key.data);
877
878 return ret;
879 }
880
881 /**
882 * Function used internally by lockdownd_pair() and lockdownd_validate_pair()
883 *
884 * @param client The lockdown client
885 * @param pair_record The pair record to use for pairing. If NULL is passed, then
886 * the pair records from the current machine are used. New records will be
887 * generated automatically when pairing is done for the first time.
888 * @param verb This is either "Pair", "ValidatePair" or "Unpair".
889 * @param options The pairing options to pass.
890 * @param response If non-NULL a pointer to lockdownd's response dictionary is returned.
891 *
892 * @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL,
893 * LOCKDOWN_E_PLIST_ERROR if the pair_record certificates are wrong,
894 * LOCKDOWN_E_PAIRING_FAILED if the pairing failed,
895 * LOCKDOWN_E_PASSWORD_PROTECTED if the device is password protected,
896 * LOCKDOWN_E_INVALID_HOST_ID if the device does not know the caller's host id
897 */
898 static lockdownd_error_t lockdownd_do_pair(lockdownd_client_t client, lockdownd_pair_record_t pair_record, const char *verb, plist_t options, plist_t *result)
899 {
900 if (!client)
901 return LOCKDOWN_E_INVALID_ARG;
902
903 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
904 plist_t dict = NULL;
905 plist_t pair_record_plist = NULL;
906 plist_t wifi_node = NULL;
907 int pairing_mode = 0; /* 0 = libimobiledevice, 1 = external */
908
909 if (pair_record && pair_record->system_buid && pair_record->host_id) {
910 /* valid pair_record passed? */
911 if (!pair_record->device_certificate || !pair_record->host_certificate || !pair_record->root_certificate) {
912 return LOCKDOWN_E_PLIST_ERROR;
913 }
914
915 /* use passed pair_record */
916 pair_record_plist = lockdownd_pair_record_to_plist(pair_record);
917
918 pairing_mode = 1;
919 } else {
920 /* generate a new pair record if pairing */
921 if (!strcmp("Pair", verb)) {
922 ret = pair_record_generate(client, &pair_record_plist);
923
924 if (ret != LOCKDOWN_E_SUCCESS) {
925 if (pair_record_plist)
926 plist_free(pair_record_plist);
927 return ret;
928 }
929
930 /* get wifi mac now, if we get it later we fail on iOS 7 which causes a reconnect */
931 lockdownd_get_value(client, NULL, "WiFiAddress", &wifi_node);
932 } else {
933 /* use existing pair record */
934 userpref_error_t uerr = userpref_read_pair_record(client->device->udid, &pair_record_plist);
935 if (uerr == USERPREF_E_READ_ERROR) {
936 debug_info("ERROR: Failed to retrieve pair record for %s", client->device->udid);
937 return LOCKDOWN_E_RECEIVE_TIMEOUT;
938 } else if (uerr == USERPREF_E_NOENT) {
939 debug_info("ERROR: No pair record for %s", client->device->udid);
940 return LOCKDOWN_E_INVALID_CONF;
941 } else if (uerr != USERPREF_E_SUCCESS) {
942 debug_info("ERROR: Failed to retrieve or parse pair record for %s", client->device->udid);
943 return LOCKDOWN_E_INVALID_CONF;
944 }
945 }
946 }
947
948 plist_t request_pair_record = plist_copy(pair_record_plist);
949
950 /* remove stuff that is private */
951 plist_dict_remove_item(request_pair_record, USERPREF_ROOT_PRIVATE_KEY_KEY);
952 plist_dict_remove_item(request_pair_record, USERPREF_HOST_PRIVATE_KEY_KEY);
953
954 /* setup pair request plist */
955 dict = plist_new_dict();
956 plist_dict_add_label(dict, client->label);
957 plist_dict_set_item(dict, "PairRecord", request_pair_record);
958 plist_dict_set_item(dict, "Request", plist_new_string(verb));
959 plist_dict_set_item(dict, "ProtocolVersion", plist_new_string(LOCKDOWN_PROTOCOL_VERSION));
960
961 if (options) {
962 plist_dict_set_item(dict, "PairingOptions", plist_copy(options));
963 }
964
965 /* send to device */
966 ret = lockdownd_send(client, dict);
967 plist_free(dict);
968 dict = NULL;
969
970 if (ret != LOCKDOWN_E_SUCCESS) {
971 plist_free(pair_record_plist);
972 if (wifi_node)
973 plist_free(wifi_node);
974 return ret;
975 }
976
977 /* Now get device's answer */
978 ret = lockdownd_receive(client, &dict);
979
980 if (ret != LOCKDOWN_E_SUCCESS) {
981 plist_free(pair_record_plist);
982 if (wifi_node)
983 plist_free(wifi_node);
984 return ret;
985 }
986
987 if (strcmp(verb, "Unpair") == 0) {
988 /* workaround for Unpair giving back ValidatePair,
989 * seems to be a bug in the device's fw */
990 if (lockdown_check_result(dict, NULL) != LOCKDOWN_E_SUCCESS) {
991 ret = LOCKDOWN_E_PAIRING_FAILED;
992 }
993 } else {
994 if (lockdown_check_result(dict, verb) != LOCKDOWN_E_SUCCESS) {
995 ret = LOCKDOWN_E_PAIRING_FAILED;
996 }
997 }
998
999 /* if pairing succeeded */
1000 if (ret == LOCKDOWN_E_SUCCESS) {
1001 debug_info("%s success", verb);
1002 if (!pairing_mode) {
1003 debug_info("internal pairing mode");
1004 if (!strcmp("Unpair", verb)) {
1005 /* remove public key from config */
1006 userpref_delete_pair_record(client->device->udid);
1007 } else {
1008 if (!strcmp("Pair", verb)) {
1009 /* add returned escrow bag if available */
1010 plist_t extra_node = plist_dict_get_item(dict, USERPREF_ESCROW_BAG_KEY);
1011 if (extra_node && plist_get_node_type(extra_node) == PLIST_DATA) {
1012 debug_info("Saving EscrowBag from response in pair record");
1013 plist_dict_set_item(pair_record_plist, USERPREF_ESCROW_BAG_KEY, plist_copy(extra_node));
1014 }
1015
1016 /* save previously retrieved wifi mac address in pair record */
1017 if (wifi_node) {
1018 debug_info("Saving WiFiAddress from device in pair record");
1019 plist_dict_set_item(pair_record_plist, USERPREF_WIFI_MAC_ADDRESS_KEY, plist_copy(wifi_node));
1020 plist_free(wifi_node);
1021 wifi_node = NULL;
1022 }
1023
1024 userpref_save_pair_record(client->device->udid, client->device->mux_id, pair_record_plist);
1025 }
1026 }
1027 } else {
1028 debug_info("external pairing mode");
1029 }
1030 } else {
1031 debug_info("%s failure", verb);
1032 plist_t error_node = NULL;
1033 /* verify error condition */
1034 error_node = plist_dict_get_item(dict, "Error");
1035 if (error_node) {
1036 char *value = NULL;
1037 plist_get_string_val(error_node, &value);
1038 if (value) {
1039 /* the first pairing fails if the device is password protected */
1040 ret = lockdownd_strtoerr(value);
1041 free(value);
1042 }
1043 }
1044 }
1045
1046 if (pair_record_plist) {
1047 plist_free(pair_record_plist);
1048 pair_record_plist = NULL;
1049 }
1050
1051 if (wifi_node) {
1052 plist_free(wifi_node);
1053 wifi_node = NULL;
1054 }
1055
1056 if (result) {
1057 *result = dict;
1058 } else {
1059 plist_free(dict);
1060 dict = NULL;
1061 }
1062
1063 return ret;
1064 }
1065
1066 lockdownd_error_t lockdownd_pair(lockdownd_client_t client, lockdownd_pair_record_t pair_record)
1067 {
1068
1069 plist_t options = plist_new_dict();
1070 plist_dict_set_item(options, "ExtendedPairingErrors", plist_new_bool(1));
1071
1072 lockdownd_error_t ret = lockdownd_do_pair(client, pair_record, "Pair", options, NULL);
1073
1074 plist_free(options);
1075
1076 return ret;
1077 }
1078
1079 lockdownd_error_t lockdownd_pair_with_options(lockdownd_client_t client, lockdownd_pair_record_t pair_record, plist_t options, plist_t *response)
1080 {
1081 return lockdownd_do_pair(client, pair_record, "Pair", options, response);
1082 }
1083
1084 lockdownd_error_t lockdownd_validate_pair(lockdownd_client_t client, lockdownd_pair_record_t pair_record)
1085 {
1086 return lockdownd_do_pair(client, pair_record, "ValidatePair", NULL, NULL);
1087 }
1088
1089 lockdownd_error_t lockdownd_unpair(lockdownd_client_t client, lockdownd_pair_record_t pair_record)
1090 {
1091 return lockdownd_do_pair(client, pair_record, "Unpair", NULL, NULL);
1092 }
1093
1094 lockdownd_error_t lockdownd_enter_recovery(lockdownd_client_t client)
1095 {
1096 if (!client)
1097 return LOCKDOWN_E_INVALID_ARG;
1098
1099 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
1100
1101 plist_t dict = plist_new_dict();
1102 plist_dict_add_label(dict, client->label);
1103 plist_dict_set_item(dict,"Request", plist_new_string("EnterRecovery"));
1104
1105 debug_info("telling device to enter recovery mode");
1106
1107 ret = lockdownd_send(client, dict);
1108 plist_free(dict);
1109 dict = NULL;
1110
1111 ret = lockdownd_receive(client, &dict);
1112
1113 ret = lockdown_check_result(dict, "EnterRecovery");
1114 if (ret == LOCKDOWN_E_SUCCESS) {
1115 debug_info("success");
1116 }
1117
1118 plist_free(dict);
1119 dict = NULL;
1120
1121 return ret;
1122 }
1123
1124 lockdownd_error_t lockdownd_goodbye(lockdownd_client_t client)
1125 {
1126 if (!client)
1127 return LOCKDOWN_E_INVALID_ARG;
1128
1129 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
1130
1131 plist_t dict = plist_new_dict();
1132 plist_dict_add_label(dict, client->label);
1133 plist_dict_set_item(dict,"Request", plist_new_string("Goodbye"));
1134
1135 debug_info("called");
1136
1137 ret = lockdownd_send(client, dict);
1138 plist_free(dict);
1139 dict = NULL;
1140
1141 ret = lockdownd_receive(client, &dict);
1142 if (!dict) {
1143 debug_info("did not get goodbye response back");
1144 return LOCKDOWN_E_PLIST_ERROR;
1145 }
1146
1147 ret = lockdown_check_result(dict, "Goodbye");
1148 if (ret == LOCKDOWN_E_SUCCESS) {
1149 debug_info("success");
1150 }
1151
1152 plist_free(dict);
1153 dict = NULL;
1154
1155 return ret;
1156 }
1157
1158 lockdownd_error_t lockdownd_start_session(lockdownd_client_t client, const char *host_id, char **session_id, int *ssl_enabled)
1159 {
1160 lockdownd_error_t ret = LOCKDOWN_E_SUCCESS;
1161 plist_t dict = NULL;
1162
1163 if (!client || !host_id)
1164 ret = LOCKDOWN_E_INVALID_ARG;
1165
1166 /* if we have a running session, stop current one first */
1167 if (client->session_id) {
1168 lockdownd_stop_session(client, client->session_id);
1169 }
1170
1171 /* setup request plist */
1172 dict = plist_new_dict();
1173 plist_dict_add_label(dict, client->label);
1174 plist_dict_set_item(dict,"Request", plist_new_string("StartSession"));
1175
1176 /* add host id */
1177 if (host_id) {
1178 plist_dict_set_item(dict, "HostID", plist_new_string(host_id));
1179 }
1180
1181 /* add system buid */
1182 char *system_buid = NULL;
1183 userpref_read_system_buid(&system_buid);
1184 if (system_buid) {
1185 plist_dict_set_item(dict, "SystemBUID", plist_new_string(system_buid));
1186 if (system_buid) {
1187 free(system_buid);
1188 system_buid = NULL;
1189 }
1190 }
1191
1192 ret = lockdownd_send(client, dict);
1193 plist_free(dict);
1194 dict = NULL;
1195
1196 if (ret != LOCKDOWN_E_SUCCESS)
1197 return ret;
1198
1199 ret = lockdownd_receive(client, &dict);
1200
1201 if (!dict)
1202 return LOCKDOWN_E_PLIST_ERROR;
1203
1204 ret = lockdown_check_result(dict, "StartSession");
1205 if (ret == LOCKDOWN_E_SUCCESS) {
1206 uint8_t use_ssl = 0;
1207
1208 plist_t enable_ssl = plist_dict_get_item(dict, "EnableSessionSSL");
1209 if (enable_ssl && (plist_get_node_type(enable_ssl) == PLIST_BOOLEAN)) {
1210 plist_get_bool_val(enable_ssl, &use_ssl);
1211 }
1212 debug_info("Session startup OK");
1213
1214 if (ssl_enabled != NULL)
1215 *ssl_enabled = use_ssl;
1216
1217 /* store session id, we need it for StopSession */
1218 plist_t session_node = plist_dict_get_item(dict, "SessionID");
1219 if (session_node && (plist_get_node_type(session_node) == PLIST_STRING)) {
1220 plist_get_string_val(session_node, &client->session_id);
1221 }
1222
1223 if (client->session_id) {
1224 debug_info("SessionID: %s", client->session_id);
1225 if (session_id != NULL)
1226 *session_id = strdup(client->session_id);
1227 } else {
1228 debug_info("Failed to get SessionID!");
1229 }
1230
1231 debug_info("Enable SSL Session: %s", (use_ssl ? "true" : "false"));
1232
1233 if (use_ssl) {
1234 ret = lockdownd_error(property_list_service_enable_ssl(client->parent));
1235 client->ssl_enabled = (ret == LOCKDOWN_E_SUCCESS ? 1 : 0);
1236 } else {
1237 ret = LOCKDOWN_E_SUCCESS;
1238 client->ssl_enabled = 0;
1239 }
1240 }
1241
1242 plist_free(dict);
1243 dict = NULL;
1244
1245 return ret;
1246 }
1247
1248 /**
1249 * Internal function used by lockdownd_do_start_service to create the
1250 * StartService request's plist.
1251 *
1252 * @param client The lockdownd client
1253 * @param identifier The identifier of the service to start
1254 * @param send_escrow_bag Should we send the device's escrow bag with the request
1255 * @param request The request's plist on success, NULL on failure
1256 *
1257 * @return LOCKDOWN_E_SUCCESS on success, LOCKDOWN_E_INVALID_CONF on failure
1258 * to read the escrow bag from the device's record (when used).
1259 */
1260 static lockdownd_error_t lockdownd_build_start_service_request(lockdownd_client_t client, const char *identifier, int send_escrow_bag, plist_t *request)
1261 {
1262 plist_t dict = plist_new_dict();
1263
1264 /* create the basic request params */
1265 plist_dict_add_label(dict, client->label);
1266 plist_dict_set_item(dict, "Request", plist_new_string("StartService"));
1267 plist_dict_set_item(dict, "Service", plist_new_string(identifier));
1268
1269 /* if needed - get the escrow bag for the device and send it with the request */
1270 if (send_escrow_bag) {
1271 /* get the pairing record */
1272 plist_t pair_record = NULL;
1273 userpref_error_t uerr = userpref_read_pair_record(client->device->udid, &pair_record);
1274 if (uerr == USERPREF_E_READ_ERROR) {
1275 debug_info("ERROR: Failed to retrieve pair record for %s", client->device->udid);
1276 plist_free(dict);
1277 return LOCKDOWN_E_RECEIVE_TIMEOUT;
1278 } else if (uerr == USERPREF_E_NOENT) {
1279 debug_info("ERROR: No pair record for %s", client->device->udid);
1280 plist_free(dict);
1281 return LOCKDOWN_E_INVALID_CONF;
1282 } else if (uerr != USERPREF_E_SUCCESS) {
1283 debug_info("ERROR: Failed to retrieve or parse pair record for %s", client->device->udid);
1284 plist_free(dict);
1285 return LOCKDOWN_E_INVALID_CONF;
1286 }
1287
1288 /* try to read the escrow bag from the record */
1289 plist_t escrow_bag = plist_dict_get_item(pair_record, USERPREF_ESCROW_BAG_KEY);
1290 if (!escrow_bag || (PLIST_DATA != plist_get_node_type(escrow_bag))) {
1291 debug_info("ERROR: Failed to retrieve the escrow bag from the device's record");
1292 plist_free(dict);
1293 plist_free(pair_record);
1294 return LOCKDOWN_E_INVALID_CONF;
1295 }
1296
1297 debug_info("Adding escrow bag to StartService for %s", identifier);
1298 plist_dict_set_item(dict, USERPREF_ESCROW_BAG_KEY, plist_copy(escrow_bag));
1299 plist_free(pair_record);
1300 }
1301
1302 *request = dict;
1303 return LOCKDOWN_E_SUCCESS;
1304 }
1305
1306 /**
1307 * Function used internally by lockdownd_start_service and lockdownd_start_service_with_escrow_bag.
1308 *
1309 * @param client The lockdownd client
1310 * @param identifier The identifier of the service to start
1311 * @param send_escrow_bag Should we send the device's escrow bag with the request
1312 * @param descriptor The service descriptor on success or NULL on failure
1313 *
1314 * @return LOCKDOWN_E_SUCCESS on success, LOCKDOWN_E_INVALID_ARG if a parameter
1315 * is NULL, LOCKDOWN_E_INVALID_SERVICE if the requested service is not known
1316 * by the device, LOCKDOWN_E_START_SERVICE_FAILED if the service could not because
1317 * started by the device, LOCKDOWN_E_INVALID_CONF if the host id or escrow bag (when
1318 * used) are missing from the device record.
1319 */
1320 static lockdownd_error_t lockdownd_do_start_service(lockdownd_client_t client, const char *identifier, int send_escrow_bag, lockdownd_service_descriptor_t *service)
1321 {
1322 if (!client || !identifier || !service)
1323 return LOCKDOWN_E_INVALID_ARG;
1324
1325 if (*service) {
1326 // reset fields if service descriptor is reused
1327 (*service)->port = 0;
1328 (*service)->ssl_enabled = 0;
1329 }
1330
1331 plist_t dict = NULL;
1332 uint16_t port_loc = 0;
1333 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
1334
1335 /* create StartService request */
1336 ret = lockdownd_build_start_service_request(client, identifier, send_escrow_bag, &dict);
1337 if (LOCKDOWN_E_SUCCESS != ret)
1338 return ret;
1339
1340 /* send to device */
1341 ret = lockdownd_send(client, dict);
1342 plist_free(dict);
1343 dict = NULL;
1344
1345 if (LOCKDOWN_E_SUCCESS != ret)
1346 return ret;
1347
1348 ret = lockdownd_receive(client, &dict);
1349
1350 if (LOCKDOWN_E_SUCCESS != ret)
1351 return ret;
1352
1353 if (!dict)
1354 return LOCKDOWN_E_PLIST_ERROR;
1355
1356 ret = lockdown_check_result(dict, "StartService");
1357 if (ret == LOCKDOWN_E_SUCCESS) {
1358 if (*service == NULL)
1359 *service = (lockdownd_service_descriptor_t)malloc(sizeof(struct lockdownd_service_descriptor));
1360 (*service)->port = 0;
1361 (*service)->ssl_enabled = 0;
1362 (*service)->identifier = strdup(identifier);
1363
1364 /* read service port number */
1365 plist_t node = plist_dict_get_item(dict, "Port");
1366 if (node && (plist_get_node_type(node) == PLIST_UINT)) {
1367 uint64_t port_value = 0;
1368 plist_get_uint_val(node, &port_value);
1369
1370 if (port_value) {
1371 port_loc = port_value;
1372 ret = LOCKDOWN_E_SUCCESS;
1373 }
1374 if (port_loc && ret == LOCKDOWN_E_SUCCESS) {
1375 (*service)->port = port_loc;
1376 }
1377 }
1378
1379 /* check if the service requires SSL */
1380 node = plist_dict_get_item(dict, "EnableServiceSSL");
1381 if (node && (plist_get_node_type(node) == PLIST_BOOLEAN)) {
1382 uint8_t b = 0;
1383 plist_get_bool_val(node, &b);
1384 (*service)->ssl_enabled = b;
1385 }
1386 } else {
1387 plist_t error_node = plist_dict_get_item(dict, "Error");
1388 if (error_node && PLIST_STRING == plist_get_node_type(error_node)) {
1389 char *error = NULL;
1390 plist_get_string_val(error_node, &error);
1391 ret = lockdownd_strtoerr(error);
1392 free(error);
1393 }
1394 }
1395
1396 plist_free(dict);
1397 dict = NULL;
1398
1399 return ret;
1400 }
1401
1402 lockdownd_error_t lockdownd_start_service(lockdownd_client_t client, const char *identifier, lockdownd_service_descriptor_t *service)
1403 {
1404 return lockdownd_do_start_service(client, identifier, 0, service);
1405 }
1406
1407 lockdownd_error_t lockdownd_start_service_with_escrow_bag(lockdownd_client_t client, const char *identifier, lockdownd_service_descriptor_t *service)
1408 {
1409 return lockdownd_do_start_service(client, identifier, 1, service);
1410 }
1411
1412 lockdownd_error_t lockdownd_activate(lockdownd_client_t client, plist_t activation_record)
1413 {
1414 if (!client)
1415 return LOCKDOWN_E_INVALID_ARG;
1416
1417 if (!client->session_id)
1418 return LOCKDOWN_E_NO_RUNNING_SESSION;
1419
1420 if (!activation_record)
1421 return LOCKDOWN_E_INVALID_ARG;
1422
1423 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
1424
1425 plist_t dict = plist_new_dict();
1426 plist_dict_add_label(dict, client->label);
1427 plist_dict_set_item(dict,"Request", plist_new_string("Activate"));
1428 plist_dict_set_item(dict,"ActivationRecord", plist_copy(activation_record));
1429
1430 ret = lockdownd_send(client, dict);
1431 plist_free(dict);
1432 dict = NULL;
1433
1434 ret = lockdownd_receive(client, &dict);
1435 if (!dict) {
1436 debug_info("LOCKDOWN_E_PLIST_ERROR");
1437 return LOCKDOWN_E_PLIST_ERROR;
1438 }
1439
1440 ret = lockdown_check_result(dict, "Activate");
1441 if (ret == LOCKDOWN_E_SUCCESS) {
1442 debug_info("success");
1443 }
1444
1445 plist_free(dict);
1446 dict = NULL;
1447
1448 return ret;
1449 }
1450
1451 lockdownd_error_t lockdownd_deactivate(lockdownd_client_t client)
1452 {
1453 if (!client)
1454 return LOCKDOWN_E_INVALID_ARG;
1455
1456 if (!client->session_id)
1457 return LOCKDOWN_E_NO_RUNNING_SESSION;
1458
1459 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
1460
1461 plist_t dict = plist_new_dict();
1462 plist_dict_add_label(dict, client->label);
1463 plist_dict_set_item(dict,"Request", plist_new_string("Deactivate"));
1464
1465 ret = lockdownd_send(client, dict);
1466 plist_free(dict);
1467 dict = NULL;
1468
1469 ret = lockdownd_receive(client, &dict);
1470 if (!dict) {
1471 debug_info("LOCKDOWN_E_PLIST_ERROR");
1472 return LOCKDOWN_E_PLIST_ERROR;
1473 }
1474
1475 ret = lockdown_check_result(dict, "Deactivate");
1476 if (ret == LOCKDOWN_E_SUCCESS) {
1477 debug_info("success");
1478 }
1479
1480 plist_free(dict);
1481 dict = NULL;
1482
1483 return ret;
1484 }
1485
1486 static void str_remove_spaces(char *source)
1487 {
1488 char *dest = source;
1489 while (*source != 0) {
1490 if (!isspace(*source)) {
1491 *dest++ = *source; /* copy */
1492 }
1493 source++;
1494 }
1495 *dest = 0;
1496 }
1497
1498 lockdownd_error_t lockdownd_get_sync_data_classes(lockdownd_client_t client, char ***classes, int *count)
1499 {
1500 if (!client)
1501 return LOCKDOWN_E_INVALID_ARG;
1502
1503 if (!client->session_id)
1504 return LOCKDOWN_E_NO_RUNNING_SESSION;
1505
1506 plist_t dict = NULL;
1507 lockdownd_error_t err = LOCKDOWN_E_UNKNOWN_ERROR;
1508
1509 plist_t value = NULL;
1510
1511 char **newlist = NULL;
1512 char *val = NULL;
1513
1514 *classes = NULL;
1515 *count = 0;
1516
1517 err = lockdownd_get_value(client, "com.apple.mobile.iTunes", "SyncDataClasses", &dict);
1518 if (err != LOCKDOWN_E_SUCCESS) {
1519 if (dict) {
1520 plist_free(dict);
1521 }
1522 return err;
1523 }
1524
1525 if (plist_get_node_type(dict) != PLIST_ARRAY) {
1526 plist_free(dict);
1527 return LOCKDOWN_E_PLIST_ERROR;
1528 }
1529
1530 while((value = plist_array_get_item(dict, *count)) != NULL) {
1531 plist_get_string_val(value, &val);
1532 newlist = realloc(*classes, sizeof(char*) * (*count+1));
1533 str_remove_spaces(val);
1534 if (asprintf(&newlist[*count], "com.apple.%s", val) < 0) {
1535 debug_info("ERROR: asprintf failed");
1536 }
1537 free(val);
1538 val = NULL;
1539 *classes = newlist;
1540 *count = *count+1;
1541 }
1542
1543 newlist = realloc(*classes, sizeof(char*) * (*count+1));
1544 newlist[*count] = NULL;
1545 *classes = newlist;
1546
1547 if (dict) {
1548 plist_free(dict);
1549 }
1550 return LOCKDOWN_E_SUCCESS;
1551 }
1552
1553 lockdownd_error_t lockdownd_data_classes_free(char **classes)
1554 {
1555 if (classes) {
1556 int i = 0;
1557 while (classes[i++]) {
1558 free(classes[i]);
1559 }
1560 free(classes);
1561 }
1562 return LOCKDOWN_E_SUCCESS;
1563 }
1564
1565 lockdownd_error_t lockdownd_service_descriptor_free(lockdownd_service_descriptor_t service)
1566 {
1567 if (service) {
1568 free(service->identifier);
1569 free(service);
1570 }
1571
1572 return LOCKDOWN_E_SUCCESS;
1573 }
1574
1575 const char* lockdownd_strerror(lockdownd_error_t err)
1576 {
1577 switch (err) {
1578 case LOCKDOWN_E_SUCCESS:
1579 return "Success";
1580 case LOCKDOWN_E_INVALID_ARG:
1581 return "Invalid argument";
1582 case LOCKDOWN_E_INVALID_CONF:
1583 return "Invalid configuration";
1584 case LOCKDOWN_E_PLIST_ERROR:
1585 return "PropertyList error";
1586 case LOCKDOWN_E_PAIRING_FAILED:
1587 return "Pairing failed";
1588 case LOCKDOWN_E_SSL_ERROR:
1589 return "SSL error";
1590 case LOCKDOWN_E_DICT_ERROR:
1591 return "Invalid dictionary";
1592 case LOCKDOWN_E_RECEIVE_TIMEOUT:
1593 return "Receive timeout";
1594 case LOCKDOWN_E_MUX_ERROR:
1595 return "Mux error";
1596 case LOCKDOWN_E_NO_RUNNING_SESSION:
1597 return "No running session";
1598 case LOCKDOWN_E_UNKNOWN_ERROR:
1599 return "Unknown Error";
1600 default: {
1601 int i = 0;
1602 while (lockdownd_error_str_map[i].lockdown_errstr) {
1603 if (lockdownd_error_str_map[i].errcode == err) {
1604 return lockdownd_error_str_map[i].errstr;
1605 }
1606 i++;
1607 }
1608 } break;
1609 }
1610 return "Unknown Error";
1611 }