summaryrefslogtreecommitdiffstats
path: root/src/lockdown.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lockdown.c')
-rw-r--r--src/lockdown.c463
1 files changed, 240 insertions, 223 deletions
diff --git a/src/lockdown.c b/src/lockdown.c
index 1a434aa..bc430c9 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -19,10 +19,6 @@
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22#include "utils.h"
23#include "iphone.h"
24#include "lockdown.h"
25#include "userpref.h"
26#include <arpa/inet.h> 22#include <arpa/inet.h>
27#include <errno.h> 23#include <errno.h>
28#include <string.h> 24#include <string.h>
@@ -30,9 +26,13 @@
30#include <glib.h> 26#include <glib.h>
31#include <libtasn1.h> 27#include <libtasn1.h>
32#include <gnutls/x509.h> 28#include <gnutls/x509.h>
33
34#include <plist/plist.h> 29#include <plist/plist.h>
35 30
31#include "lockdown.h"
32#include "iphone.h"
33#include "utils.h"
34#include "userpref.h"
35
36#define RESULT_SUCCESS 0 36#define RESULT_SUCCESS 0
37#define RESULT_FAILURE 1 37#define RESULT_FAILURE 1
38 38
@@ -126,21 +126,23 @@ static int lockdown_check_result(plist_t dict, const char *query_match)
126 * the StopSession Request to the device. 126 * the StopSession Request to the device.
127 * 127 *
128 * @param control The lockdown client 128 * @param control The lockdown client
129 *
130 * @return an error code (LOCKDOWN_E_SUCCESS on success)
129 */ 131 */
130iphone_error_t lockdownd_stop_session(lockdownd_client_t client) 132lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char *session_id)
131{ 133{
132 if (!client) 134 if (!client)
133 return IPHONE_E_INVALID_ARG; 135 return LOCKDOWN_E_INVALID_ARG;
134 136
135 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 137 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
136 138
137 plist_t dict = plist_new_dict(); 139 plist_t dict = plist_new_dict();
138 plist_add_sub_key_el(dict, "Request"); 140 plist_add_sub_key_el(dict, "Request");
139 plist_add_sub_string_el(dict, "StopSession"); 141 plist_add_sub_string_el(dict, "StopSession");
140 plist_add_sub_key_el(dict, "SessionID"); 142 plist_add_sub_key_el(dict, "SessionID");
141 plist_add_sub_string_el(dict, client->session_id); 143 plist_add_sub_string_el(dict, session_id);
142 144
143 log_dbg_msg(DBGMASK_LOCKDOWND, "iphone_lckd_stop_session() called\n"); 145 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__);
144 146
145 ret = lockdownd_send(client, dict); 147 ret = lockdownd_send(client, dict);
146 148
@@ -150,14 +152,14 @@ iphone_error_t lockdownd_stop_session(lockdownd_client_t client)
150 ret = lockdownd_recv(client, &dict); 152 ret = lockdownd_recv(client, &dict);
151 153
152 if (!dict) { 154 if (!dict) {
153 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n"); 155 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: LOCKDOWN_E_PLIST_ERROR\n", __func__);
154 return IPHONE_E_PLIST_ERROR; 156 return LOCKDOWN_E_PLIST_ERROR;
155 } 157 }
156 158
157 ret = IPHONE_E_UNKNOWN_ERROR; 159 ret = LOCKDOWN_E_UNKNOWN_ERROR;
158 if (lockdown_check_result(dict, "StopSession") == RESULT_SUCCESS) { 160 if (lockdown_check_result(dict, "StopSession") == RESULT_SUCCESS) {
159 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): success\n"); 161 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__);
160 ret = IPHONE_E_SUCCESS; 162 ret = LOCKDOWN_E_SUCCESS;
161 } 163 }
162 plist_free(dict); 164 plist_free(dict);
163 dict = NULL; 165 dict = NULL;
@@ -171,19 +173,21 @@ iphone_error_t lockdownd_stop_session(lockdownd_client_t client)
171 * performing a close notify, which is done by "gnutls_bye". 173 * performing a close notify, which is done by "gnutls_bye".
172 * 174 *
173 * @param client The lockdown client 175 * @param client The lockdown client
176 *
177 * @return an error code (LOCKDOWN_E_SUCCESS on success)
174 */ 178 */
175static iphone_error_t lockdownd_stop_ssl_session(lockdownd_client_t client) 179static lockdownd_error_t lockdownd_stop_ssl_session(lockdownd_client_t client)
176{ 180{
177 if (!client) { 181 if (!client) {
178 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_ssl_session(): invalid argument!\n"); 182 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: invalid argument!\n", __func__);
179 return IPHONE_E_INVALID_ARG; 183 return LOCKDOWN_E_INVALID_ARG;
180 } 184 }
181 iphone_error_t ret = IPHONE_E_SUCCESS; 185 lockdownd_error_t ret = LOCKDOWN_E_SUCCESS;
182 186
183 if (client->in_SSL) { 187 if (client->in_SSL) {
184 log_dbg_msg(DBGMASK_LOCKDOWND, "Stopping SSL Session\n"); 188 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: stopping SSL session\n", __func__);
185 ret = lockdownd_stop_session(client); 189 ret = lockdownd_stop_session(client, client->session_id);
186 log_dbg_msg(DBGMASK_LOCKDOWND, "Sending SSL close notify\n"); 190 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending SSL close notify\n", __func__);
187 gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR); 191 gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR);
188 } 192 }
189 if (client->ssl_session) { 193 if (client->ssl_session) {
@@ -198,12 +202,14 @@ static iphone_error_t lockdownd_stop_ssl_session(lockdownd_client_t client)
198/** Closes the lockdownd client and does the necessary housekeeping. 202/** Closes the lockdownd client and does the necessary housekeeping.
199 * 203 *
200 * @param client The lockdown client 204 * @param client The lockdown client
205 *
206 * @return an error code (LOCKDOWN_E_SUCCESS on success)
201 */ 207 */
202iphone_error_t lockdownd_free_client(lockdownd_client_t client) 208lockdownd_error_t lockdownd_client_free(lockdownd_client_t client)
203{ 209{
204 if (!client) 210 if (!client)
205 return IPHONE_E_INVALID_ARG; 211 return LOCKDOWN_E_INVALID_ARG;
206 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 212 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
207 213
208 lockdownd_stop_ssl_session(client); 214 lockdownd_stop_ssl_session(client);
209 215
@@ -224,13 +230,13 @@ iphone_error_t lockdownd_free_client(lockdownd_client_t client)
224 * @param control The lockdownd client 230 * @param control The lockdownd client
225 * @param plist The plist to store the received data 231 * @param plist The plist to store the received data
226 * 232 *
227 * @return an error code (IPHONE_E_SUCCESS on success) 233 * @return an error code (LOCKDOWN_E_SUCCESS on success)
228 */ 234 */
229iphone_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist) 235lockdownd_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist)
230{ 236{
231 if (!client || !plist || (plist && *plist)) 237 if (!client || !plist || (plist && *plist))
232 return IPHONE_E_INVALID_ARG; 238 return LOCKDOWN_E_INVALID_ARG;
233 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 239 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
234 char *receive = NULL; 240 char *receive = NULL;
235 uint32_t datalen = 0, bytes = 0, received_bytes = 0; 241 uint32_t datalen = 0, bytes = 0, received_bytes = 0;
236 242
@@ -240,10 +246,10 @@ iphone_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist)
240 ssize_t res = gnutls_record_recv(*client->ssl_session, &datalen, sizeof(datalen)); 246 ssize_t res = gnutls_record_recv(*client->ssl_session, &datalen, sizeof(datalen));
241 if (res < 0) { 247 if (res < 0) {
242 log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_recv: Error occured: %s\n", gnutls_strerror(res)); 248 log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_recv: Error occured: %s\n", gnutls_strerror(res));
243 return IPHONE_E_SSL_ERROR; 249 return LOCKDOWN_E_SSL_ERROR;
244 } else { 250 } else {
245 bytes = res; 251 bytes = res;
246 ret = IPHONE_E_SUCCESS; 252 ret = LOCKDOWN_E_SUCCESS;
247 } 253 }
248 } 254 }
249 datalen = ntohl(datalen); 255 datalen = ntohl(datalen);
@@ -253,40 +259,40 @@ iphone_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist)
253 259
254 /* fill buffer and request more packets if needed */ 260 /* fill buffer and request more packets if needed */
255 if (!client->in_SSL) { 261 if (!client->in_SSL) {
256 while ((received_bytes < datalen) && (ret == IPHONE_E_SUCCESS)) { 262 while ((received_bytes < datalen) && (ret == LOCKDOWN_E_SUCCESS)) {
257 ret = usbmuxd_recv(client->sfd, receive + received_bytes, datalen - received_bytes, &bytes); 263 ret = usbmuxd_recv(client->sfd, receive + received_bytes, datalen - received_bytes, &bytes);
258 received_bytes += bytes; 264 received_bytes += bytes;
259 } 265 }
260 } else { 266 } else {
261 ssize_t res = 0; 267 ssize_t res = 0;
262 while ((received_bytes < datalen) && (ret == IPHONE_E_SUCCESS)) { 268 while ((received_bytes < datalen) && (ret == LOCKDOWN_E_SUCCESS)) {
263 res = gnutls_record_recv(*client->ssl_session, receive + received_bytes, datalen - received_bytes); 269 res = gnutls_record_recv(*client->ssl_session, receive + received_bytes, datalen - received_bytes);
264 if (res < 0) { 270 if (res < 0) {
265 log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_recv: Error occured: %s\n", gnutls_strerror(res)); 271 log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_recv: Error occured: %s\n", gnutls_strerror(res));
266 ret = IPHONE_E_SSL_ERROR; 272 ret = LOCKDOWN_E_SSL_ERROR;
267 } else { 273 } else {
268 received_bytes += res; 274 received_bytes += res;
269 ret = IPHONE_E_SUCCESS; 275 ret = LOCKDOWN_E_SUCCESS;
270 } 276 }
271 } 277 }
272 } 278 }
273 279
274 if (ret != IPHONE_E_SUCCESS) { 280 if (ret != LOCKDOWN_E_SUCCESS) {
275 free(receive); 281 free(receive);
276 return ret; 282 return ret;
277 } 283 }
278 284
279 if ((ssize_t)received_bytes <= 0) { 285 if ((ssize_t)received_bytes <= 0) {
280 free(receive); 286 free(receive);
281 return IPHONE_E_NOT_ENOUGH_DATA; 287 return LOCKDOWN_E_NOT_ENOUGH_DATA;
282 } 288 }
283 289
284 log_dbg_msg(DBGMASK_LOCKDOWND, "Recv msg :\nsize : %i\nbuffer :\n%s\n", received_bytes, receive); 290 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: received msg size: %i, buffer follows:\n%s", __func__, received_bytes, receive);
285 plist_from_xml(receive, received_bytes, plist); 291 plist_from_xml(receive, received_bytes, plist);
286 free(receive); 292 free(receive);
287 293
288 if (!*plist) 294 if (!*plist)
289 ret = IPHONE_E_PLIST_ERROR; 295 ret = LOCKDOWN_E_PLIST_ERROR;
290 296
291 return ret; 297 return ret;
292} 298}
@@ -299,27 +305,27 @@ iphone_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist)
299 * @param client The lockdownd client 305 * @param client The lockdownd client
300 * @param plist The plist to send 306 * @param plist The plist to send
301 * 307 *
302 * @return an error code (IPHONE_E_SUCCESS on success) 308 * @return an error code (LOCKDOWN_E_SUCCESS on success)
303 */ 309 */
304iphone_error_t lockdownd_send(lockdownd_client_t client, plist_t plist) 310lockdownd_error_t lockdownd_send(lockdownd_client_t client, plist_t plist)
305{ 311{
306 if (!client || !plist) 312 if (!client || !plist)
307 return IPHONE_E_INVALID_ARG; 313 return LOCKDOWN_E_INVALID_ARG;
308 char *real_query; 314 char *real_query;
309 int bytes; 315 int bytes;
310 char *XMLContent = NULL; 316 char *XMLContent = NULL;
311 uint32_t length = 0; 317 uint32_t length = 0;
312 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 318 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
313 319
314 plist_to_xml(plist, &XMLContent, &length); 320 plist_to_xml(plist, &XMLContent, &length);
315 log_dbg_msg(DBGMASK_LOCKDOWND, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent); 321 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending msg size %i, buffer follows:\n%s", __func__, length, XMLContent);
316 322
317 real_query = (char *) malloc(sizeof(char) * (length + 4)); 323 real_query = (char *) malloc(sizeof(char) * (length + 4));
318 length = htonl(length); 324 length = htonl(length);
319 memcpy(real_query, &length, sizeof(length)); 325 memcpy(real_query, &length, sizeof(length));
320 memcpy(real_query + 4, XMLContent, ntohl(length)); 326 memcpy(real_query + 4, XMLContent, ntohl(length));
321 free(XMLContent); 327 free(XMLContent);
322 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): made the query, sending it along\n"); 328 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: made the query, sending it along\n", __func__);
323 329
324 if (!client->in_SSL) 330 if (!client->in_SSL)
325 ret = usbmuxd_send(client->sfd, real_query, ntohl(length) + sizeof(length), (uint32_t*)&bytes); 331 ret = usbmuxd_send(client->sfd, real_query, ntohl(length) + sizeof(length), (uint32_t*)&bytes);
@@ -327,16 +333,16 @@ iphone_error_t lockdownd_send(lockdownd_client_t client, plist_t plist)
327 ssize_t res = gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); 333 ssize_t res = gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length));
328 if (res < 0) { 334 if (res < 0) {
329 log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_send: Error occured: %s\n", gnutls_strerror(res)); 335 log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_send: Error occured: %s\n", gnutls_strerror(res));
330 ret = IPHONE_E_SSL_ERROR; 336 ret = LOCKDOWN_E_SSL_ERROR;
331 } else { 337 } else {
332 bytes = res; 338 bytes = res;
333 ret = IPHONE_E_SUCCESS; 339 ret = LOCKDOWN_E_SUCCESS;
334 } 340 }
335 } 341 }
336 if (ret == IPHONE_E_SUCCESS) { 342 if (ret == LOCKDOWN_E_SUCCESS) {
337 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): sent it!\n"); 343 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sent it!\n", __func__);
338 } else { 344 } else {
339 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): sending failed!\n"); 345 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending failed!\n", __func__);
340 } 346 }
341 free(real_query); 347 free(real_query);
342 348
@@ -347,20 +353,20 @@ iphone_error_t lockdownd_send(lockdownd_client_t client, plist_t plist)
347 * 353 *
348 * @param client The lockdownd client 354 * @param client The lockdownd client
349 * 355 *
350 * @return an error code (IPHONE_E_SUCCESS on success) 356 * @return an error code (LOCKDOWN_E_SUCCESS on success)
351 */ 357 */
352iphone_error_t lockdownd_query_type(lockdownd_client_t client) 358lockdownd_error_t lockdownd_query_type(lockdownd_client_t client)
353{ 359{
354 if (!client) 360 if (!client)
355 return IPHONE_E_INVALID_ARG; 361 return LOCKDOWN_E_INVALID_ARG;
356 362
357 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 363 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
358 364
359 plist_t dict = plist_new_dict(); 365 plist_t dict = plist_new_dict();
360 plist_add_sub_key_el(dict, "Request"); 366 plist_add_sub_key_el(dict, "Request");
361 plist_add_sub_string_el(dict, "QueryType"); 367 plist_add_sub_string_el(dict, "QueryType");
362 368
363 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_query_type() called\n"); 369 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__);
364 ret = lockdownd_send(client, dict); 370 ret = lockdownd_send(client, dict);
365 371
366 plist_free(dict); 372 plist_free(dict);
@@ -368,13 +374,13 @@ iphone_error_t lockdownd_query_type(lockdownd_client_t client)
368 374
369 ret = lockdownd_recv(client, &dict); 375 ret = lockdownd_recv(client, &dict);
370 376
371 if (IPHONE_E_SUCCESS != ret) 377 if (LOCKDOWN_E_SUCCESS != ret)
372 return ret; 378 return ret;
373 379
374 ret = IPHONE_E_UNKNOWN_ERROR; 380 ret = LOCKDOWN_E_UNKNOWN_ERROR;
375 if (lockdown_check_result(dict, "QueryType") == RESULT_SUCCESS) { 381 if (lockdown_check_result(dict, "QueryType") == RESULT_SUCCESS) {
376 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_query_type(): success\n"); 382 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__);
377 ret = IPHONE_E_SUCCESS; 383 ret = LOCKDOWN_E_SUCCESS;
378 } 384 }
379 plist_free(dict); 385 plist_free(dict);
380 dict = NULL; 386 dict = NULL;
@@ -389,15 +395,15 @@ iphone_error_t lockdownd_query_type(lockdownd_client_t client)
389 * @param key the key name to request or NULL to query for all keys 395 * @param key the key name to request or NULL to query for all keys
390 * @param value a plist node representing the result value node 396 * @param value a plist node representing the result value node
391 * 397 *
392 * @return an error code (IPHONE_E_SUCCESS on success) 398 * @return an error code (LOCKDOWN_E_SUCCESS on success)
393 */ 399 */
394iphone_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain, const char *key, plist_t *value) 400lockdownd_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain, const char *key, plist_t *value)
395{ 401{
396 if (!client) 402 if (!client)
397 return IPHONE_E_INVALID_ARG; 403 return LOCKDOWN_E_INVALID_ARG;
398 404
399 plist_t dict = NULL; 405 plist_t dict = NULL;
400 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 406 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
401 407
402 /* setup request plist */ 408 /* setup request plist */
403 dict = plist_new_dict(); 409 dict = plist_new_dict();
@@ -418,19 +424,19 @@ iphone_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain
418 plist_free(dict); 424 plist_free(dict);
419 dict = NULL; 425 dict = NULL;
420 426
421 if (ret != IPHONE_E_SUCCESS) 427 if (ret != LOCKDOWN_E_SUCCESS)
422 return ret; 428 return ret;
423 429
424 /* Now get device's answer */ 430 /* Now get device's answer */
425 ret = lockdownd_recv(client, &dict); 431 ret = lockdownd_recv(client, &dict);
426 if (ret != IPHONE_E_SUCCESS) 432 if (ret != LOCKDOWN_E_SUCCESS)
427 return ret; 433 return ret;
428 434
429 if (lockdown_check_result(dict, "GetValue") == RESULT_SUCCESS) { 435 if (lockdown_check_result(dict, "GetValue") == RESULT_SUCCESS) {
430 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); 436 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__);
431 ret = IPHONE_E_SUCCESS; 437 ret = LOCKDOWN_E_SUCCESS;
432 } 438 }
433 if (ret != IPHONE_E_SUCCESS) { 439 if (ret != LOCKDOWN_E_SUCCESS) {
434 plist_free(dict); 440 plist_free(dict);
435 return ret; 441 return ret;
436 } 442 }
@@ -445,7 +451,7 @@ iphone_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain
445 plist_get_key_val(value_key_node, &result_key); 451 plist_get_key_val(value_key_node, &result_key);
446 452
447 if (!strcmp(result_key, "Value")) { 453 if (!strcmp(result_key, "Value")) {
448 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_get_value(): has a value\n"); 454 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: has a value\n", __func__);
449 *value = plist_copy(value_value_node); 455 *value = plist_copy(value_value_node);
450 } 456 }
451 free(result_key); 457 free(result_key);
@@ -462,15 +468,15 @@ iphone_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain
462 * @param key the key name to set the value or NULL to set a value dict plist 468 * @param key the key name to set the value or NULL to set a value dict plist
463 * @param value a plist node of any node type representing the value to set 469 * @param value a plist node of any node type representing the value to set
464 * 470 *
465 * @return an error code (IPHONE_E_SUCCESS on success) 471 * @return an error code (LOCKDOWN_E_SUCCESS on success)
466 */ 472 */
467iphone_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain, const char *key, plist_t value) 473lockdownd_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain, const char *key, plist_t value)
468{ 474{
469 if (!client || !value) 475 if (!client || !value)
470 return IPHONE_E_INVALID_ARG; 476 return LOCKDOWN_E_INVALID_ARG;
471 477
472 plist_t dict = NULL; 478 plist_t dict = NULL;
473 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 479 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
474 480
475 /* setup request plist */ 481 /* setup request plist */
476 dict = plist_new_dict(); 482 dict = plist_new_dict();
@@ -494,20 +500,20 @@ iphone_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain
494 plist_free(dict); 500 plist_free(dict);
495 dict = NULL; 501 dict = NULL;
496 502
497 if (ret != IPHONE_E_SUCCESS) 503 if (ret != LOCKDOWN_E_SUCCESS)
498 return ret; 504 return ret;
499 505
500 /* Now get device's answer */ 506 /* Now get device's answer */
501 ret = lockdownd_recv(client, &dict); 507 ret = lockdownd_recv(client, &dict);
502 if (ret != IPHONE_E_SUCCESS) 508 if (ret != LOCKDOWN_E_SUCCESS)
503 return ret; 509 return ret;
504 510
505 if (lockdown_check_result(dict, "SetValue") == RESULT_SUCCESS) { 511 if (lockdown_check_result(dict, "SetValue") == RESULT_SUCCESS) {
506 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); 512 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__);
507 ret = IPHONE_E_SUCCESS; 513 ret = LOCKDOWN_E_SUCCESS;
508 } 514 }
509 515
510 if (ret != IPHONE_E_SUCCESS) { 516 if (ret != LOCKDOWN_E_SUCCESS) {
511 plist_free(dict); 517 plist_free(dict);
512 return ret; 518 return ret;
513 } 519 }
@@ -524,15 +530,15 @@ iphone_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain
524 * @param domain the domain to query on or NULL for global domain 530 * @param domain the domain to query on or NULL for global domain
525 * @param key the key name to remove or NULL remove all keys for the current domain 531 * @param key the key name to remove or NULL remove all keys for the current domain
526 * 532 *
527 * @return an error code (IPHONE_E_SUCCESS on success) 533 * @return an error code (LOCKDOWN_E_SUCCESS on success)
528 */ 534 */
529iphone_error_t lockdownd_remove_value(lockdownd_client_t client, const char *domain, const char *key) 535lockdownd_error_t lockdownd_remove_value(lockdownd_client_t client, const char *domain, const char *key)
530{ 536{
531 if (!client) 537 if (!client)
532 return IPHONE_E_INVALID_ARG; 538 return LOCKDOWN_E_INVALID_ARG;
533 539
534 plist_t dict = NULL; 540 plist_t dict = NULL;
535 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 541 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
536 542
537 /* setup request plist */ 543 /* setup request plist */
538 dict = plist_new_dict(); 544 dict = plist_new_dict();
@@ -553,20 +559,20 @@ iphone_error_t lockdownd_remove_value(lockdownd_client_t client, const char *dom
553 plist_free(dict); 559 plist_free(dict);
554 dict = NULL; 560 dict = NULL;
555 561
556 if (ret != IPHONE_E_SUCCESS) 562 if (ret != LOCKDOWN_E_SUCCESS)
557 return ret; 563 return ret;
558 564
559 /* Now get device's answer */ 565 /* Now get device's answer */
560 ret = lockdownd_recv(client, &dict); 566 ret = lockdownd_recv(client, &dict);
561 if (ret != IPHONE_E_SUCCESS) 567 if (ret != LOCKDOWN_E_SUCCESS)
562 return ret; 568 return ret;
563 569
564 if (lockdown_check_result(dict, "RemoveValue") == RESULT_SUCCESS) { 570 if (lockdown_check_result(dict, "RemoveValue") == RESULT_SUCCESS) {
565 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); 571 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__);
566 ret = IPHONE_E_SUCCESS; 572 ret = LOCKDOWN_E_SUCCESS;
567 } 573 }
568 574
569 if (ret != IPHONE_E_SUCCESS) { 575 if (ret != LOCKDOWN_E_SUCCESS) {
570 plist_free(dict); 576 plist_free(dict);
571 return ret; 577 return ret;
572 } 578 }
@@ -577,18 +583,18 @@ iphone_error_t lockdownd_remove_value(lockdownd_client_t client, const char *dom
577 583
578/** Asks for the device's unique id. Part of the lockdownd handshake. 584/** Asks for the device's unique id. Part of the lockdownd handshake.
579 * 585 *
580 * @return an error code (IPHONE_E_SUCCESS on success) 586 * @return an error code (LOCKDOWN_E_SUCCESS on success)
581 */ 587 */
582iphone_error_t lockdownd_get_device_uid(lockdownd_client_t client, char **uid) 588lockdownd_error_t lockdownd_get_device_uuid(lockdownd_client_t client, char **uuid)
583{ 589{
584 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 590 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
585 plist_t value = NULL; 591 plist_t value = NULL;
586 592
587 ret = lockdownd_get_value(client, NULL, "UniqueDeviceID", &value); 593 ret = lockdownd_get_value(client, NULL, "UniqueDeviceID", &value);
588 if (ret != IPHONE_E_SUCCESS) { 594 if (ret != LOCKDOWN_E_SUCCESS) {
589 return ret; 595 return ret;
590 } 596 }
591 plist_get_string_val(value, uid); 597 plist_get_string_val(value, uuid);
592 598
593 plist_free(value); 599 plist_free(value);
594 value = NULL; 600 value = NULL;
@@ -597,17 +603,17 @@ iphone_error_t lockdownd_get_device_uid(lockdownd_client_t client, char **uid)
597 603
598/** Askes for the device's public key. Part of the lockdownd handshake. 604/** Askes for the device's public key. Part of the lockdownd handshake.
599 * 605 *
600 * @return an error code (IPHONE_E_SUCCESS on success) 606 * @return an error code (LOCKDOWN_E_SUCCESS on success)
601 */ 607 */
602iphone_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key) 608lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key)
603{ 609{
604 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 610 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
605 plist_t value = NULL; 611 plist_t value = NULL;
606 char *value_value = NULL; 612 char *value_value = NULL;
607 uint64_t size = 0; 613 uint64_t size = 0;
608 614
609 ret = lockdownd_get_value(client, NULL, "DevicePublicKey", &value); 615 ret = lockdownd_get_value(client, NULL, "DevicePublicKey", &value);
610 if (ret != IPHONE_E_SUCCESS) { 616 if (ret != LOCKDOWN_E_SUCCESS) {
611 return ret; 617 return ret;
612 } 618 }
613 plist_get_data_val(value, &value_value, &size); 619 plist_get_data_val(value, &value_value, &size);
@@ -625,15 +631,15 @@ iphone_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls
625 * @param client The pointer to the location of the new lockdownd_client 631 * @param client The pointer to the location of the new lockdownd_client
626 * 632 *
627 * 633 *
628 * @return an error code (IPHONE_E_SUCCESS on success) 634 * @return an error code (LOCKDOWN_E_SUCCESS on success)
629 */ 635 */
630iphone_error_t lockdownd_get_device_name(lockdownd_client_t client, char **device_name) 636lockdownd_error_t lockdownd_get_device_name(lockdownd_client_t client, char **device_name)
631{ 637{
632 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 638 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
633 plist_t value = NULL; 639 plist_t value = NULL;
634 640
635 ret = lockdownd_get_value(client, NULL, "DeviceName", &value); 641 ret = lockdownd_get_value(client, NULL, "DeviceName", &value);
636 if (ret != IPHONE_E_SUCCESS) { 642 if (ret != LOCKDOWN_E_SUCCESS) {
637 return ret; 643 return ret;
638 } 644 }
639 plist_get_string_val(value, device_name); 645 plist_get_string_val(value, device_name);
@@ -649,19 +655,19 @@ iphone_error_t lockdownd_get_device_name(lockdownd_client_t client, char **devic
649 * @param phone The iPhone to create a lockdownd client for 655 * @param phone The iPhone to create a lockdownd client for
650 * @param client The pointer to the location of the new lockdownd_client 656 * @param client The pointer to the location of the new lockdownd_client
651 * 657 *
652 * @return an error code (IPHONE_E_SUCCESS on success) 658 * @return an error code (LOCKDOWN_E_SUCCESS on success)
653 */ 659 */
654iphone_error_t lockdownd_new_client(iphone_device_t device, lockdownd_client_t *client) 660lockdownd_error_t lockdownd_client_new(iphone_device_t device, lockdownd_client_t *client)
655{ 661{
656 if (!device || !client || (client && *client)) 662 if (!device || !client || (client && *client))
657 return IPHONE_E_INVALID_ARG; 663 return LOCKDOWN_E_INVALID_ARG;
658 iphone_error_t ret = IPHONE_E_SUCCESS; 664 lockdownd_error_t ret = LOCKDOWN_E_SUCCESS;
659 char *host_id = NULL; 665 char *host_id = NULL;
660 666
661 int sfd = usbmuxd_connect(device->handle, 0xf27e); 667 int sfd = usbmuxd_connect(device->handle, 0xf27e);
662 if (sfd < 0) { 668 if (sfd < 0) {
663 log_debug_msg("%s: could not connect to lockdownd (device handle %d)\n", __func__, device->handle); 669 log_debug_msg("%s: could not connect to lockdownd (device handle %d)\n", __func__, device->handle);
664 return IPHONE_E_UNKNOWN_ERROR; 670 return LOCKDOWN_E_MUX_ERROR;
665 } 671 }
666 672
667 lockdownd_client_t client_loc = (lockdownd_client_t) malloc(sizeof(struct lockdownd_client_int)); 673 lockdownd_client_t client_loc = (lockdownd_client_t) malloc(sizeof(struct lockdownd_client_int));
@@ -669,36 +675,36 @@ iphone_error_t lockdownd_new_client(iphone_device_t device, lockdownd_client_t *
669 client_loc->ssl_session = (gnutls_session_t *) malloc(sizeof(gnutls_session_t)); 675 client_loc->ssl_session = (gnutls_session_t *) malloc(sizeof(gnutls_session_t));
670 client_loc->in_SSL = 0; 676 client_loc->in_SSL = 0;
671 677
672 if (IPHONE_E_SUCCESS != lockdownd_query_type(client_loc)) { 678 if (LOCKDOWN_E_SUCCESS != lockdownd_query_type(client_loc)) {
673 log_debug_msg("QueryType failed in the lockdownd client.\n"); 679 log_debug_msg("%s: QueryType failed in the lockdownd client.\n", __func__);
674 ret = IPHONE_E_NOT_ENOUGH_DATA; 680 ret = LOCKDOWN_E_NOT_ENOUGH_DATA;
675 } 681 }
676 682
677 char *uid = NULL; 683 char *uuid = NULL;
678 ret = lockdownd_get_device_uid(client_loc, &uid); 684 ret = iphone_device_get_uuid(device, &uuid);
679 if (IPHONE_E_SUCCESS != ret) { 685 if (LOCKDOWN_E_SUCCESS != ret) {
680 log_debug_msg("Device refused to send uid.\n"); 686 log_debug_msg("%s: failed to get device uuid.\n", __func__);
681 } 687 }
682 log_debug_msg("Device uid: %s\n", uid); 688 log_debug_msg("%s: device uuid: %s\n", __func__, uuid);
683 689
684 host_id = get_host_id(); 690 userpref_get_host_id(&host_id);
685 if (IPHONE_E_SUCCESS == ret && !host_id) { 691 if (LOCKDOWN_E_SUCCESS == ret && !host_id) {
686 ret = IPHONE_E_INVALID_CONF; 692 ret = LOCKDOWN_E_INVALID_CONF;
687 } 693 }
688 694
689 if (IPHONE_E_SUCCESS == ret && !is_device_known(uid)) 695 if (LOCKDOWN_E_SUCCESS == ret && !userpref_has_device_public_key(uuid))
690 ret = lockdownd_pair(client_loc, uid, host_id); 696 ret = lockdownd_pair(client_loc, uuid, host_id);
691 697
692 if (uid) { 698 if (uuid) {
693 free(uid); 699 free(uuid);
694 uid = NULL; 700 uuid = NULL;
695 } 701 }
696 702
697 if (IPHONE_E_SUCCESS == ret) { 703 if (LOCKDOWN_E_SUCCESS == ret) {
698 ret = lockdownd_start_ssl_session(client_loc, host_id); 704 ret = lockdownd_start_ssl_session(client_loc, host_id);
699 if (IPHONE_E_SUCCESS != ret) { 705 if (LOCKDOWN_E_SUCCESS != ret) {
700 ret = IPHONE_E_SSL_ERROR; 706 ret = LOCKDOWN_E_SSL_ERROR;
701 log_debug_msg("SSL Session opening failed.\n"); 707 log_debug_msg("%s: SSL Session opening failed.\n", __func__);
702 } 708 }
703 709
704 if (host_id) { 710 if (host_id) {
@@ -706,7 +712,7 @@ iphone_error_t lockdownd_new_client(iphone_device_t device, lockdownd_client_t *
706 host_id = NULL; 712 host_id = NULL;
707 } 713 }
708 714
709 if (IPHONE_E_SUCCESS == ret) 715 if (LOCKDOWN_E_SUCCESS == ret)
710 *client = client_loc; 716 *client = client_loc;
711 } 717 }
712 718
@@ -716,11 +722,11 @@ iphone_error_t lockdownd_new_client(iphone_device_t device, lockdownd_client_t *
716/** Generates the appropriate keys and pairs the device. It's part of the 722/** Generates the appropriate keys and pairs the device. It's part of the
717 * lockdownd handshake. 723 * lockdownd handshake.
718 * 724 *
719 * @return an error code (IPHONE_E_SUCCESS on success) 725 * @return an error code (LOCKDOWN_E_SUCCESS on success)
720 */ 726 */
721iphone_error_t lockdownd_pair(lockdownd_client_t client, char *uid, char *host_id) 727lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *host_id)
722{ 728{
723 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 729 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
724 plist_t dict = NULL; 730 plist_t dict = NULL;
725 plist_t dict_record = NULL; 731 plist_t dict_record = NULL;
726 732
@@ -730,14 +736,14 @@ iphone_error_t lockdownd_pair(lockdownd_client_t client, char *uid, char *host_i
730 gnutls_datum_t public_key = { NULL, 0 }; 736 gnutls_datum_t public_key = { NULL, 0 };
731 737
732 ret = lockdownd_get_device_public_key(client, &public_key); 738 ret = lockdownd_get_device_public_key(client, &public_key);
733 if (ret != IPHONE_E_SUCCESS) { 739 if (ret != LOCKDOWN_E_SUCCESS) {
734 log_debug_msg("Device refused to send public key.\n"); 740 log_debug_msg("%s: device refused to send public key.\n", __func__);
735 return ret; 741 return ret;
736 } 742 }
737 log_debug_msg("device public key :\n %s.\n", public_key.data); 743 log_debug_msg("%s: device public key follows:\n%s\n", __func__, public_key.data);
738 744
739 ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert); 745 ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert);
740 if (ret != IPHONE_E_SUCCESS) { 746 if (ret != LOCKDOWN_E_SUCCESS) {
741 free(public_key.data); 747 free(public_key.data);
742 return ret; 748 return ret;
743 } 749 }
@@ -763,29 +769,28 @@ iphone_error_t lockdownd_pair(lockdownd_client_t client, char *uid, char *host_i
763 plist_free(dict); 769 plist_free(dict);
764 dict = NULL; 770 dict = NULL;
765 771
766 if (ret != IPHONE_E_SUCCESS) 772 if (ret != LOCKDOWN_E_SUCCESS)
767 return ret; 773 return ret;
768 774
769 /* Now get iPhone's answer */ 775 /* Now get iPhone's answer */
770 ret = lockdownd_recv(client, &dict); 776 ret = lockdownd_recv(client, &dict);
771 777
772 if (ret != IPHONE_E_SUCCESS) 778 if (ret != LOCKDOWN_E_SUCCESS)
773 return ret; 779 return ret;
774 780
775 if (lockdown_check_result(dict, "Pair") == RESULT_SUCCESS) { 781 if (lockdown_check_result(dict, "Pair") == RESULT_SUCCESS) {
776 ret = IPHONE_E_SUCCESS; 782 ret = LOCKDOWN_E_SUCCESS;
777 } 783 }
778 plist_free(dict); 784 plist_free(dict);
779 dict = NULL; 785 dict = NULL;
780 786
781 /* store public key in config if pairing succeeded */ 787 /* store public key in config if pairing succeeded */
782 if (ret == IPHONE_E_SUCCESS) { 788 if (ret == LOCKDOWN_E_SUCCESS) {
783 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair: pair success\n"); 789 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair success\n", __func__);
784 store_device_public_key(uid, public_key); 790 userpref_set_device_public_key(uuid, public_key);
785 ret = IPHONE_E_SUCCESS;
786 } else { 791 } else {
787 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair: pair failure\n"); 792 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair failure\n", __func__);
788 ret = IPHONE_E_PAIRING_FAILED; 793 ret = LOCKDOWN_E_PAIRING_FAILED;
789 } 794 }
790 free(public_key.data); 795 free(public_key.data);
791 return ret; 796 return ret;
@@ -796,20 +801,20 @@ iphone_error_t lockdownd_pair(lockdownd_client_t client, char *uid, char *host_i
796 * 801 *
797 * @param client The lockdown client 802 * @param client The lockdown client
798 * 803 *
799 * @return an error code (IPHONE_E_SUCCESS on success) 804 * @return an error code (LOCKDOWN_E_SUCCESS on success)
800 */ 805 */
801iphone_error_t lockdownd_enter_recovery(lockdownd_client_t client) 806lockdownd_error_t lockdownd_enter_recovery(lockdownd_client_t client)
802{ 807{
803 if (!client) 808 if (!client)
804 return IPHONE_E_INVALID_ARG; 809 return LOCKDOWN_E_INVALID_ARG;
805 810
806 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 811 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
807 812
808 plist_t dict = plist_new_dict(); 813 plist_t dict = plist_new_dict();
809 plist_add_sub_key_el(dict, "Request"); 814 plist_add_sub_key_el(dict, "Request");
810 plist_add_sub_string_el(dict, "EnterRecovery"); 815 plist_add_sub_string_el(dict, "EnterRecovery");
811 816
812 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Telling device to enter recovery mode\n", __func__); 817 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: telling device to enter recovery mode\n", __func__);
813 818
814 ret = lockdownd_send(client, dict); 819 ret = lockdownd_send(client, dict);
815 plist_free(dict); 820 plist_free(dict);
@@ -819,7 +824,7 @@ iphone_error_t lockdownd_enter_recovery(lockdownd_client_t client)
819 824
820 if (lockdown_check_result(dict, "EnterRecovery") == RESULT_SUCCESS) { 825 if (lockdown_check_result(dict, "EnterRecovery") == RESULT_SUCCESS) {
821 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); 826 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__);
822 ret = IPHONE_E_SUCCESS; 827 ret = LOCKDOWN_E_SUCCESS;
823 } 828 }
824 plist_free(dict); 829 plist_free(dict);
825 dict = NULL; 830 dict = NULL;
@@ -832,35 +837,34 @@ iphone_error_t lockdownd_enter_recovery(lockdownd_client_t client)
832 * 837 *
833 * @param client The lockdown client 838 * @param client The lockdown client
834 * 839 *
835 * @return an error code (IPHONE_E_SUCCESS on success) 840 * @return an error code (LOCKDOWN_E_SUCCESS on success)
836 */ 841 */
837iphone_error_t lockdownd_goodbye(lockdownd_client_t client) 842lockdownd_error_t lockdownd_goodbye(lockdownd_client_t client)
838{ 843{
839 if (!client) 844 if (!client)
840 return IPHONE_E_INVALID_ARG; 845 return LOCKDOWN_E_INVALID_ARG;
841 846
842 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 847 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
843 848
844 plist_t dict = plist_new_dict(); 849 plist_t dict = plist_new_dict();
845 plist_add_sub_key_el(dict, "Request"); 850 plist_add_sub_key_el(dict, "Request");
846 plist_add_sub_string_el(dict, "Goodbye"); 851 plist_add_sub_string_el(dict, "Goodbye");
847 852
848 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_goodbye() called\n"); 853 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__);
849 854
850 ret = lockdownd_send(client, dict); 855 ret = lockdownd_send(client, dict);
851 plist_free(dict); 856 plist_free(dict);
852 dict = NULL; 857 dict = NULL;
853 858
854 ret = lockdownd_recv(client, &dict); 859 ret = lockdownd_recv(client, &dict);
855
856 if (!dict) { 860 if (!dict) {
857 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_goodbye(): IPHONE_E_PLIST_ERROR\n"); 861 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: did not get goodbye response back\n", __func__);
858 return IPHONE_E_PLIST_ERROR; 862 return LOCKDOWN_E_PLIST_ERROR;
859 } 863 }
860 864
861 if (lockdown_check_result(dict, "Goodbye") == RESULT_SUCCESS) { 865 if (lockdown_check_result(dict, "Goodbye") == RESULT_SUCCESS) {
862 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_goodbye(): success\n"); 866 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__);
863 ret = IPHONE_E_SUCCESS; 867 ret = LOCKDOWN_E_SUCCESS;
864 } 868 }
865 plist_free(dict); 869 plist_free(dict);
866 dict = NULL; 870 dict = NULL;
@@ -870,14 +874,15 @@ iphone_error_t lockdownd_goodbye(lockdownd_client_t client)
870/** Generates the device certificate from the public key as well as the host 874/** Generates the device certificate from the public key as well as the host
871 * and root certificates. 875 * and root certificates.
872 * 876 *
873 * @return an error code (IPHONE_E_SUCCESS on success) 877 * @return an error code (LOCKDOWN_E_SUCCESS on success)
874 */ 878 */
875iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert, 879lockdownd_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert,
876 gnutls_datum_t * ohost_cert, gnutls_datum_t * oroot_cert) 880 gnutls_datum_t * ohost_cert, gnutls_datum_t * oroot_cert)
877{ 881{
878 if (!public_key.data || !odevice_cert || !ohost_cert || !oroot_cert) 882 if (!public_key.data || !odevice_cert || !ohost_cert || !oroot_cert)
879 return IPHONE_E_INVALID_ARG; 883 return LOCKDOWN_E_INVALID_ARG;
880 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 884 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
885 userpref_error_t uret = USERPREF_E_UNKNOWN_ERROR;
881 886
882 gnutls_datum_t modulus = { NULL, 0 }; 887 gnutls_datum_t modulus = { NULL, 0 };
883 gnutls_datum_t exponent = { NULL, 0 }; 888 gnutls_datum_t exponent = { NULL, 0 };
@@ -905,7 +910,7 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t
905 ret1 = asn1_read_value(asn1_pub_key, "modulus", modulus.data, (int*)&modulus.size); 910 ret1 = asn1_read_value(asn1_pub_key, "modulus", modulus.data, (int*)&modulus.size);
906 ret2 = asn1_read_value(asn1_pub_key, "publicExponent", exponent.data, (int*)&exponent.size); 911 ret2 = asn1_read_value(asn1_pub_key, "publicExponent", exponent.data, (int*)&exponent.size);
907 if (ASN1_SUCCESS == ret1 && ASN1_SUCCESS == ret2) 912 if (ASN1_SUCCESS == ret1 && ASN1_SUCCESS == ret2)
908 ret = IPHONE_E_SUCCESS; 913 ret = LOCKDOWN_E_SUCCESS;
909 } 914 }
910 if (asn1_pub_key) 915 if (asn1_pub_key)
911 asn1_delete_structure(&asn1_pub_key); 916 asn1_delete_structure(&asn1_pub_key);
@@ -915,7 +920,7 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t
915 } 920 }
916 921
917 /* now generate certifcates */ 922 /* now generate certifcates */
918 if (IPHONE_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) { 923 if (LOCKDOWN_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) {
919 924
920 gnutls_global_init(); 925 gnutls_global_init();
921 gnutls_datum_t essentially_null = { (unsigned char*)strdup("abababababababab"), strlen("abababababababab") }; 926 gnutls_datum_t essentially_null = { (unsigned char*)strdup("abababababababab"), strlen("abababababababab") };
@@ -935,10 +940,9 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t
935 gnutls_x509_privkey_init(&root_privkey); 940 gnutls_x509_privkey_init(&root_privkey);
936 gnutls_x509_privkey_init(&host_privkey); 941 gnutls_x509_privkey_init(&host_privkey);
937 942
938 ret = get_keys_and_certs(root_privkey, root_cert, host_privkey, host_cert); 943 uret = userpref_get_keys_and_certs(root_privkey, root_cert, host_privkey, host_cert);
939
940 if (IPHONE_E_SUCCESS == ret) {
941 944
945 if (USERPREF_E_SUCCESS == uret) {
942 /* generate device certificate */ 946 /* generate device certificate */
943 gnutls_x509_crt_set_key(dev_cert, fake_privkey); 947 gnutls_x509_crt_set_key(dev_cert, fake_privkey);
944 gnutls_x509_crt_set_serial(dev_cert, "\x00", 1); 948 gnutls_x509_crt_set_serial(dev_cert, "\x00", 1);
@@ -948,7 +952,7 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t
948 gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); 952 gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
949 gnutls_x509_crt_sign(dev_cert, root_cert, root_privkey); 953 gnutls_x509_crt_sign(dev_cert, root_cert, root_privkey);
950 954
951 if (IPHONE_E_SUCCESS == ret) { 955 if (LOCKDOWN_E_SUCCESS == ret) {
952 /* if everything went well, export in PEM format */ 956 /* if everything went well, export in PEM format */
953 gnutls_datum_t dev_pem = { NULL, 0 }; 957 gnutls_datum_t dev_pem = { NULL, 0 };
954 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size); 958 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size);
@@ -958,7 +962,9 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t
958 gnutls_datum_t pem_root_cert = { NULL, 0 }; 962 gnutls_datum_t pem_root_cert = { NULL, 0 };
959 gnutls_datum_t pem_host_cert = { NULL, 0 }; 963 gnutls_datum_t pem_host_cert = { NULL, 0 };
960 964
961 if ( IPHONE_E_SUCCESS == get_certs_as_pem(&pem_root_cert, &pem_host_cert) ) { 965 uret = userpref_get_certs_as_pem(&pem_root_cert, &pem_host_cert);
966
967 if (USERPREF_E_SUCCESS == uret) {
962 /* copy buffer for output */ 968 /* copy buffer for output */
963 odevice_cert->data = malloc(dev_pem.size); 969 odevice_cert->data = malloc(dev_pem.size);
964 memcpy(odevice_cert->data, dev_pem.data, dev_pem.size); 970 memcpy(odevice_cert->data, dev_pem.data, dev_pem.size);
@@ -977,6 +983,19 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t
977 } 983 }
978 } 984 }
979 } 985 }
986
987 switch(uret) {
988 case USERPREF_E_INVALID_ARG:
989 ret = LOCKDOWN_E_INVALID_ARG;
990 break;
991 case USERPREF_E_INVALID_CONF:
992 ret = LOCKDOWN_E_INVALID_CONF;
993 break;
994 case USERPREF_E_SSL_ERROR:
995 ret = LOCKDOWN_E_SSL_ERROR;
996 default:
997 break;
998 }
980 } 999 }
981 } 1000 }
982 1001
@@ -993,14 +1012,14 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t
993 * @param client The lockdownd client 1012 * @param client The lockdownd client
994 * @param HostID The HostID used with this phone 1013 * @param HostID The HostID used with this phone
995 * 1014 *
996 * @return an error code (IPHONE_E_SUCCESS on success) 1015 * @return an error code (LOCKDOWN_E_SUCCESS on success)
997 */ 1016 */
998iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char *HostID) 1017lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char *HostID)
999{ 1018{
1000 plist_t dict = NULL; 1019 plist_t dict = NULL;
1001 uint32_t return_me = 0; 1020 uint32_t return_me = 0;
1002 1021
1003 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 1022 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
1004 client->session_id[0] = '\0'; 1023 client->session_id[0] = '\0';
1005 1024
1006 /* Setup DevicePublicKey request plist */ 1025 /* Setup DevicePublicKey request plist */
@@ -1014,13 +1033,13 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char
1014 plist_free(dict); 1033 plist_free(dict);
1015 dict = NULL; 1034 dict = NULL;
1016 1035
1017 if (ret != IPHONE_E_SUCCESS) 1036 if (ret != LOCKDOWN_E_SUCCESS)
1018 return ret; 1037 return ret;
1019 1038
1020 ret = lockdownd_recv(client, &dict); 1039 ret = lockdownd_recv(client, &dict);
1021 1040
1022 if (!dict) 1041 if (!dict)
1023 return IPHONE_E_PLIST_ERROR; 1042 return LOCKDOWN_E_PLIST_ERROR;
1024 1043
1025 if (lockdown_check_result(dict, "StartSession") == RESULT_FAILURE) { 1044 if (lockdown_check_result(dict, "StartSession") == RESULT_FAILURE) {
1026 plist_t error_node = plist_get_dict_el_from_key(dict, "Error"); 1045 plist_t error_node = plist_get_dict_el_from_key(dict, "Error");
@@ -1029,12 +1048,14 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char
1029 plist_get_string_val(error_node, &error); 1048 plist_get_string_val(error_node, &error);
1030 1049
1031 if (!strcmp(error, "InvalidHostID")) { 1050 if (!strcmp(error, "InvalidHostID")) {
1032 //hostid is unknown. Pair and try again 1051 /* hostid is unknown. Pair and try again */
1033 char *uid = NULL; 1052 char *uuid = NULL;
1034 char* host_id = get_host_id(); 1053 char *host_id = NULL;
1035 if (IPHONE_E_SUCCESS == lockdownd_get_device_uid(client, &uid) ) { 1054 userpref_get_host_id(&host_id);
1036 if (IPHONE_E_SUCCESS == lockdownd_pair(client, uid, host_id) ) { 1055
1037 //start session again 1056 if (LOCKDOWN_E_SUCCESS == lockdownd_get_device_uuid(client, &uuid) ) {
1057 if (LOCKDOWN_E_SUCCESS == lockdownd_pair(client, uuid, host_id) ) {
1058 /* start session again */
1038 plist_free(dict); 1059 plist_free(dict);
1039 dict = plist_new_dict(); 1060 dict = plist_new_dict();
1040 plist_add_sub_key_el(dict, "HostID"); 1061 plist_add_sub_key_el(dict, "HostID");
@@ -1049,20 +1070,20 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char
1049 ret = lockdownd_recv(client, &dict); 1070 ret = lockdownd_recv(client, &dict);
1050 } 1071 }
1051 } 1072 }
1052 free(uid); 1073 free(uuid);
1053 free(host_id); 1074 free(host_id);
1054 } 1075 }
1055 free(error); 1076 free(error);
1056 } 1077 }
1057 } 1078 }
1058 1079
1059 ret = IPHONE_E_SSL_ERROR; 1080 ret = LOCKDOWN_E_SSL_ERROR;
1060 if (lockdown_check_result(dict, "StartSession") == RESULT_SUCCESS) { 1081 if (lockdown_check_result(dict, "StartSession") == RESULT_SUCCESS) {
1061 // Set up GnuTLS... 1082 // Set up GnuTLS...
1062 //gnutls_anon_client_credentials_t anoncred; 1083 //gnutls_anon_client_credentials_t anoncred;
1063 gnutls_certificate_credentials_t xcred; 1084 gnutls_certificate_credentials_t xcred;
1064 1085
1065 log_dbg_msg(DBGMASK_LOCKDOWND, "We started the session OK, now trying GnuTLS\n"); 1086 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: started the session OK, now trying GnuTLS\n", __func__);
1066 errno = 0; 1087 errno = 0;
1067 gnutls_global_init(); 1088 gnutls_global_init();
1068 //gnutls_anon_allocate_client_credentials(&anoncred); 1089 //gnutls_anon_allocate_client_credentials(&anoncred);
@@ -1084,30 +1105,29 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char
1084 } 1105 }
1085 gnutls_credentials_set(*client->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. 1106 gnutls_credentials_set(*client->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me.
1086 1107
1087 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 1...\n"); 1108 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS step 1...\n", __func__);
1088 gnutls_transport_set_ptr(*client->ssl_session, (gnutls_transport_ptr_t) client); 1109 gnutls_transport_set_ptr(*client->ssl_session, (gnutls_transport_ptr_t) client);
1089 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 2...\n"); 1110 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS step 2...\n", __func__);
1090 gnutls_transport_set_push_function(*client->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); 1111 gnutls_transport_set_push_function(*client->ssl_session, (gnutls_push_func) & lockdownd_secuwrite);
1091 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 3...\n"); 1112 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS step 3...\n", __func__);
1092 gnutls_transport_set_pull_function(*client->ssl_session, (gnutls_pull_func) & lockdownd_securead); 1113 gnutls_transport_set_pull_function(*client->ssl_session, (gnutls_pull_func) & lockdownd_securead);
1093 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 4 -- now handshaking...\n"); 1114 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS step 4 -- now handshaking...\n", __func__);
1094
1095 if (errno) 1115 if (errno)
1096 log_dbg_msg(DBGMASK_LOCKDOWND, "WARN: errno says %s before handshake!\n", strerror(errno)); 1116 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: WARN: errno says %s before handshake!\n", __func__, strerror(errno));
1097 return_me = gnutls_handshake(*client->ssl_session); 1117 return_me = gnutls_handshake(*client->ssl_session);
1098 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS handshake done...\n"); 1118 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS handshake done...\n", __func__);
1099 1119
1100 if (return_me != GNUTLS_E_SUCCESS) { 1120 if (return_me != GNUTLS_E_SUCCESS) {
1101 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS reported something wrong.\n"); 1121 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS reported something wrong.\n", __func__);
1102 gnutls_perror(return_me); 1122 gnutls_perror(return_me);
1103 log_dbg_msg(DBGMASK_LOCKDOWND, "oh.. errno says %s\n", strerror(errno)); 1123 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: oh.. errno says %s\n", __func__, strerror(errno));
1104 return IPHONE_E_SSL_ERROR; 1124 return LOCKDOWN_E_SSL_ERROR;
1105 } else { 1125 } else {
1106 client->in_SSL = 1; 1126 client->in_SSL = 1;
1107 ret = IPHONE_E_SUCCESS; 1127 ret = LOCKDOWN_E_SUCCESS;
1108 } 1128 }
1109 } 1129 }
1110 //store session id 1130 /* store session id */
1111 plist_t session_node = plist_find_node_by_key(dict, "SessionID"); 1131 plist_t session_node = plist_find_node_by_key(dict, "SessionID");
1112 if (session_node) { 1132 if (session_node) {
1113 1133
@@ -1120,23 +1140,23 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char
1120 plist_get_string_val(session_node_val, &session_id); 1140 plist_get_string_val(session_node_val, &session_id);
1121 1141
1122 if (session_node_val_type == PLIST_STRING && session_id) { 1142 if (session_node_val_type == PLIST_STRING && session_id) {
1123 // we need to store the session ID for StopSession 1143 /* we need to store the session ID for StopSession */
1124 strcpy(client->session_id, session_id); 1144 strcpy(client->session_id, session_id);
1125 log_dbg_msg(DBGMASK_LOCKDOWND, "SessionID: %s\n", client->session_id); 1145 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: SessionID: %s\n", __func__, client->session_id);
1126 } 1146 }
1127 if (session_id) 1147 if (session_id)
1128 free(session_id); 1148 free(session_id);
1129 } 1149 }
1130 } else 1150 } else
1131 log_dbg_msg(DBGMASK_LOCKDOWND, "Failed to get SessionID!\n"); 1151 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Failed to get SessionID!\n", __func__);
1132 plist_free(dict); 1152 plist_free(dict);
1133 dict = NULL; 1153 dict = NULL;
1134 1154
1135 if (ret == IPHONE_E_SUCCESS) 1155 if (ret == LOCKDOWN_E_SUCCESS)
1136 return ret; 1156 return ret;
1137 1157
1138 log_dbg_msg(DBGMASK_LOCKDOWND, "Apparently failed negotiating with lockdownd.\n"); 1158 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Apparently failed negotiating with lockdownd.\n", __func__);
1139 return IPHONE_E_SSL_ERROR; 1159 return LOCKDOWN_E_SSL_ERROR;
1140} 1160}
1141 1161
1142/** gnutls callback for writing data to the iPhone. 1162/** gnutls callback for writing data to the iPhone.
@@ -1152,12 +1172,10 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size
1152 uint32_t bytes = 0; 1172 uint32_t bytes = 0;
1153 lockdownd_client_t client; 1173 lockdownd_client_t client;
1154 client = (lockdownd_client_t) transport; 1174 client = (lockdownd_client_t) transport;
1155 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_secuwrite() called\n"); 1175 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__);
1156 log_dbg_msg(DBGMASK_LOCKDOWND, "pre-send\nlength = %zi\n", length); 1176 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pre-send length = %zi\n", __func__, length);
1157 usbmuxd_send(client->sfd, buffer, length, &bytes); 1177 usbmuxd_send(client->sfd, buffer, length, &bytes);
1158 log_dbg_msg(DBGMASK_LOCKDOWND, "post-send\nsent %i bytes\n", bytes); 1178 log_dbg_msg(DBGMASK_LOCKDOWND, "%s: post-send sent %i bytes\n", __func__, bytes);
1159
1160 dump_debug_buffer("sslpacketwrite.out", buffer, length);
1161 return bytes; 1179 return bytes;
1162} 1180}
1163 1181
@@ -1179,19 +1197,17 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
1179 client = (lockdownd_client_t) transport; 1197 client = (lockdownd_client_t) transport;
1180 char *recv_buffer; 1198 char *recv_buffer;
1181 1199
1182 log_debug_msg("lockdownd_securead() called\nlength = %zi\n", length); 1200 log_debug_msg("%s: pre-read client wants %zi bytes\n", __func__, length);
1183
1184 log_debug_msg("pre-read\nclient wants %zi bytes\n", length);
1185 1201
1186 recv_buffer = (char *) malloc(sizeof(char) * this_len); 1202 recv_buffer = (char *) malloc(sizeof(char) * this_len);
1187 1203
1188 // repeat until we have the full data or an error occurs. 1204 // repeat until we have the full data or an error occurs.
1189 do { 1205 do {
1190 if ((res = usbmuxd_recv(client->sfd, recv_buffer, this_len, (uint32_t*)&bytes)) != IPHONE_E_SUCCESS) { 1206 if ((res = usbmuxd_recv(client->sfd, recv_buffer, this_len, (uint32_t*)&bytes)) != LOCKDOWN_E_SUCCESS) {
1191 log_debug_msg("%s: ERROR: usbmux_recv returned %d\n", __func__, res); 1207 log_debug_msg("%s: ERROR: usbmux_recv returned %d\n", __func__, res);
1192 return res; 1208 return res;
1193 } 1209 }
1194 log_debug_msg("post-read\nwe got %i bytes\n", bytes); 1210 log_debug_msg("%s: post-read we got %i bytes\n", __func__, bytes);
1195 1211
1196 // increase read count 1212 // increase read count
1197 tbytes += bytes; 1213 tbytes += bytes;
@@ -1205,7 +1221,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
1205 } 1221 }
1206 1222
1207 this_len = length - tbytes; 1223 this_len = length - tbytes;
1208 log_debug_msg("re-read\ntrying to read missing %i bytes\n", this_len); 1224 log_debug_msg("%s: re-read trying to read missing %i bytes\n", __func__, this_len);
1209 } while (tbytes < length); 1225 } while (tbytes < length);
1210 1226
1211 if (recv_buffer) { 1227 if (recv_buffer) {
@@ -1221,22 +1237,23 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
1221 * @param service The name of the service to start 1237 * @param service The name of the service to start
1222 * @param port The port number the service was started on 1238 * @param port The port number the service was started on
1223 1239
1224 * @return an error code (IPHONE_E_SUCCESS on success) 1240 * @return an error code (LOCKDOWN_E_SUCCESS on success)
1225 */ 1241 */
1226iphone_error_t lockdownd_start_service(lockdownd_client_t client, const char *service, int *port) 1242lockdownd_error_t lockdownd_start_service(lockdownd_client_t client, const char *service, int *port)
1227{ 1243{
1228 if (!client || !service || !port) 1244 if (!client || !service || !port)
1229 return IPHONE_E_INVALID_ARG; 1245 return LOCKDOWN_E_INVALID_ARG;
1230 1246
1231 char *host_id = get_host_id(); 1247 char *host_id = NULL;
1248 userpref_get_host_id(&host_id);
1232 if (!host_id) 1249 if (!host_id)
1233 return IPHONE_E_INVALID_CONF; 1250 return LOCKDOWN_E_INVALID_CONF;
1234 if (!client->in_SSL && !lockdownd_start_ssl_session(client, host_id)) 1251 if (!client->in_SSL && !lockdownd_start_ssl_session(client, host_id))
1235 return IPHONE_E_SSL_ERROR; 1252 return LOCKDOWN_E_SSL_ERROR;
1236 1253
1237 plist_t dict = NULL; 1254 plist_t dict = NULL;
1238 uint32_t port_loc = 0; 1255 uint32_t port_loc = 0;
1239 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 1256 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
1240 1257
1241 free(host_id); 1258 free(host_id);
1242 host_id = NULL; 1259 host_id = NULL;
@@ -1252,18 +1269,18 @@ iphone_error_t lockdownd_start_service(lockdownd_client_t client, const char *se
1252 plist_free(dict); 1269 plist_free(dict);
1253 dict = NULL; 1270 dict = NULL;
1254 1271
1255 if (IPHONE_E_SUCCESS != ret) 1272 if (LOCKDOWN_E_SUCCESS != ret)
1256 return ret; 1273 return ret;
1257 1274
1258 ret = lockdownd_recv(client, &dict); 1275 ret = lockdownd_recv(client, &dict);
1259 1276
1260 if (IPHONE_E_SUCCESS != ret) 1277 if (LOCKDOWN_E_SUCCESS != ret)
1261 return ret; 1278 return ret;
1262 1279
1263 if (!dict) 1280 if (!dict)
1264 return IPHONE_E_PLIST_ERROR; 1281 return LOCKDOWN_E_PLIST_ERROR;
1265 1282
1266 ret = IPHONE_E_UNKNOWN_ERROR; 1283 ret = LOCKDOWN_E_UNKNOWN_ERROR;
1267 if (lockdown_check_result(dict, "StartService") == RESULT_SUCCESS) { 1284 if (lockdown_check_result(dict, "StartService") == RESULT_SUCCESS) {
1268 plist_t port_key_node = plist_find_node_by_key(dict, "Port"); 1285 plist_t port_key_node = plist_find_node_by_key(dict, "Port");
1269 plist_t port_value_node = plist_get_next_sibling(port_key_node); 1286 plist_t port_value_node = plist_get_next_sibling(port_key_node);
@@ -1277,17 +1294,17 @@ iphone_error_t lockdownd_start_service(lockdownd_client_t client, const char *se
1277 plist_get_uint_val(port_value_node, &port_value); 1294 plist_get_uint_val(port_value_node, &port_value);
1278 if (port_key && !strcmp(port_key, "Port")) { 1295 if (port_key && !strcmp(port_key, "Port")) {
1279 port_loc = port_value; 1296 port_loc = port_value;
1280 ret = IPHONE_E_SUCCESS; 1297 ret = LOCKDOWN_E_SUCCESS;
1281 } 1298 }
1282 if (port_key) 1299 if (port_key)
1283 free(port_key); 1300 free(port_key);
1284 1301
1285 if (port && ret == IPHONE_E_SUCCESS) 1302 if (port && ret == LOCKDOWN_E_SUCCESS)
1286 *port = port_loc; 1303 *port = port_loc;
1287 } 1304 }
1288 } 1305 }
1289 else 1306 else
1290 ret = IPHONE_E_START_SERVICE_FAILED; 1307 ret = LOCKDOWN_E_START_SERVICE_FAILED;
1291 1308
1292 plist_free(dict); 1309 plist_free(dict);
1293 dict = NULL; 1310 dict = NULL;