Debian Codegraph @

src/installation_proxy.c

raw
1 /*
2 * installation_proxy.c
3 * com.apple.mobile.installation_proxy service implementation.
4 *
5 * Copyright (c) 2010-2015 Martin Szulecki All Rights Reserved.
6 * Copyright (c) 2010-2013 Nikias Bassen, All Rights Reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <string.h>
27 #include <stdlib.h>
28 #include <inttypes.h>
29
30 #ifndef _MSC_VER
31 #include <unistd.h>
32 #endif
33
34 #include <plist/plist.h>
35
36 #include "installation_proxy.h"
37 #include "property_list_service.h"
38 #include "common/debug.h"
39
40 typedef enum {
41 INSTPROXY_COMMAND_TYPE_ASYNC,
42 INSTPROXY_COMMAND_TYPE_SYNC
43 } instproxy_command_type_t;
44
45 struct instproxy_status_data {
46 instproxy_client_t client;
47 plist_t command;
48 instproxy_status_cb_t cbfunc;
49 void *user_data;
50 };
51
52 /**
53 * Converts an error string identifier to a instproxy_error_t value.
54 * Used internally to get correct error codes from a response.
55 *
56 * @param name The error name to convert.
57 * @param error_detail Pointer to store error detail text if available. The
58 * caller is reponsible for freeing the allocated buffer after use. If NULL
59 * is passed no error detail will be returned.
60 *
61 * @return A matching instproxy_error_t error code or
62 * INSTPROXY_E_UNKNOWN_ERROR otherwise.
63 */
64 static instproxy_error_t instproxy_strtoerr(const char* name)
65 {
66 instproxy_error_t err = INSTPROXY_E_UNKNOWN_ERROR;
67
68 if (strcmp(name, "AlreadyArchived") == 0) {
69 err = INSTPROXY_E_ALREADY_ARCHIVED;
70 } else if (strcmp(name, "APIInternalError") == 0) {
71 err = INSTPROXY_E_API_INTERNAL_ERROR;
72 } else if (strcmp(name, "ApplicationAlreadyInstalled") == 0) {
73 err = INSTPROXY_E_APPLICATION_ALREADY_INSTALLED;
74 } else if (strcmp(name, "ApplicationMoveFailed") == 0) {
75 err = INSTPROXY_E_APPLICATION_MOVE_FAILED;
76 } else if (strcmp(name, "ApplicationSINFCaptureFailed") == 0) {
77 err = INSTPROXY_E_APPLICATION_SINF_CAPTURE_FAILED;
78 } else if (strcmp(name, "ApplicationSandboxFailed") == 0) {
79 err = INSTPROXY_E_APPLICATION_SANDBOX_FAILED;
80 } else if (strcmp(name, "ApplicationVerificationFailed") == 0) {
81 err = INSTPROXY_E_APPLICATION_VERIFICATION_FAILED;
82 } else if (strcmp(name, "ArchiveDestructionFailed") == 0) {
83 err = INSTPROXY_E_ARCHIVE_DESTRUCTION_FAILED;
84 } else if (strcmp(name, "BundleVerificationFailed") == 0) {
85 err = INSTPROXY_E_BUNDLE_VERIFICATION_FAILED;
86 } else if (strcmp(name, "CarrierBundleCopyFailed") == 0) {
87 err = INSTPROXY_E_CARRIER_BUNDLE_COPY_FAILED;
88 } else if (strcmp(name, "CarrierBundleDirectoryCreationFailed") == 0) {
89 err = INSTPROXY_E_CARRIER_BUNDLE_DIRECTORY_CREATION_FAILED;
90 } else if (strcmp(name, "CarrierBundleMissingSupportedSIMs") == 0) {
91 err = INSTPROXY_E_CARRIER_BUNDLE_MISSING_SUPPORTED_SIMS;
92 } else if (strcmp(name, "CommCenterNotificationFailed") == 0) {
93 err = INSTPROXY_E_COMM_CENTER_NOTIFICATION_FAILED;
94 } else if (strcmp(name, "ContainerCreationFailed") == 0) {
95 err = INSTPROXY_E_CONTAINER_CREATION_FAILED;
96 } else if (strcmp(name, "ContainerP0wnFailed") == 0) {
97 err = INSTPROXY_E_CONTAINER_P0WN_FAILED;
98 } else if (strcmp(name, "ContainerRemovalFailed") == 0) {
99 err = INSTPROXY_E_CONTAINER_REMOVAL_FAILED;
100 } else if (strcmp(name, "EmbeddedProfileInstallFailed") == 0) {
101 err = INSTPROXY_E_EMBEDDED_PROFILE_INSTALL_FAILED;
102 } else if (strcmp(name, "ExecutableTwiddleFailed") == 0) {
103 err = INSTPROXY_E_EXECUTABLE_TWIDDLE_FAILED;
104 } else if (strcmp(name, "ExistenceCheckFailed") == 0) {
105 err = INSTPROXY_E_EXISTENCE_CHECK_FAILED;
106 } else if (strcmp(name, "InstallMapUpdateFailed") == 0) {
107 err = INSTPROXY_E_INSTALL_MAP_UPDATE_FAILED;
108 } else if (strcmp(name, "ManifestCaptureFailed") == 0) {
109 err = INSTPROXY_E_MANIFEST_CAPTURE_FAILED;
110 } else if (strcmp(name, "MapGenerationFailed") == 0) {
111 err = INSTPROXY_E_MAP_GENERATION_FAILED;
112 } else if (strcmp(name, "MissingBundleExecutable") == 0) {
113 err = INSTPROXY_E_MISSING_BUNDLE_EXECUTABLE;
114 } else if (strcmp(name, "MissingBundleIdentifier") == 0) {
115 err = INSTPROXY_E_MISSING_BUNDLE_IDENTIFIER;
116 } else if (strcmp(name, "MissingBundlePath") == 0) {
117 err = INSTPROXY_E_MISSING_BUNDLE_PATH;
118 } else if (strcmp(name, "MissingContainer") == 0) {
119 err = INSTPROXY_E_MISSING_CONTAINER;
120 } else if (strcmp(name, "NotificationFailed") == 0) {
121 err = INSTPROXY_E_NOTIFICATION_FAILED;
122 } else if (strcmp(name, "PackageExtractionFailed") == 0) {
123 err = INSTPROXY_E_PACKAGE_EXTRACTION_FAILED;
124 } else if (strcmp(name, "PackageInspectionFailed") == 0) {
125 err = INSTPROXY_E_PACKAGE_INSPECTION_FAILED;
126 } else if (strcmp(name, "PackageMoveFailed") == 0) {
127 err = INSTPROXY_E_PACKAGE_MOVE_FAILED;
128 } else if (strcmp(name, "PathConversionFailed") == 0) {
129 err = INSTPROXY_E_PATH_CONVERSION_FAILED;
130 } else if (strcmp(name, "RestoreContainerFailed") == 0) {
131 err = INSTPROXY_E_RESTORE_CONTAINER_FAILED;
132 } else if (strcmp(name, "SeatbeltProfileRemovalFailed") == 0) {
133 err = INSTPROXY_E_SEATBELT_PROFILE_REMOVAL_FAILED;
134 } else if (strcmp(name, "StageCreationFailed") == 0) {
135 err = INSTPROXY_E_STAGE_CREATION_FAILED;
136 } else if (strcmp(name, "SymlinkFailed") == 0) {
137 err = INSTPROXY_E_SYMLINK_FAILED;
138 } else if (strcmp(name, "UnknownCommand") == 0) {
139 err = INSTPROXY_E_UNKNOWN_COMMAND;
140 } else if (strcmp(name, "iTunesArtworkCaptureFailed") == 0) {
141 err = INSTPROXY_E_ITUNES_ARTWORK_CAPTURE_FAILED;
142 } else if (strcmp(name, "iTunesMetadataCaptureFailed") == 0) {
143 err = INSTPROXY_E_ITUNES_METADATA_CAPTURE_FAILED;
144 } else if (strcmp(name, "DeviceOSVersionTooLow") == 0) {
145 err = INSTPROXY_E_DEVICE_OS_VERSION_TOO_LOW;
146 } else if (strcmp(name, "DeviceFamilyNotSupported") == 0) {
147 err = INSTPROXY_E_DEVICE_FAMILY_NOT_SUPPORTED;
148 } else if (strcmp(name, "PackagePatchFailed") == 0) {
149 err = INSTPROXY_E_PACKAGE_PATCH_FAILED;
150 } else if (strcmp(name, "IncorrectArchitecture") == 0) {
151 err = INSTPROXY_E_INCORRECT_ARCHITECTURE;
152 } else if (strcmp(name, "PluginCopyFailed") == 0) {
153 err = INSTPROXY_E_PLUGIN_COPY_FAILED;
154 } else if (strcmp(name, "BreadcrumbFailed") == 0) {
155 err = INSTPROXY_E_BREADCRUMB_FAILED;
156 } else if (strcmp(name, "BreadcrumbUnlockFailed") == 0) {
157 err = INSTPROXY_E_BREADCRUMB_UNLOCK_FAILED;
158 } else if (strcmp(name, "GeoJSONCaptureFailed") == 0) {
159 err = INSTPROXY_E_GEOJSON_CAPTURE_FAILED;
160 } else if (strcmp(name, "NewsstandArtworkCaptureFailed") == 0) {
161 err = INSTPROXY_E_NEWSSTAND_ARTWORK_CAPTURE_FAILED;
162 } else if (strcmp(name, "MissingCommand") == 0) {
163 err = INSTPROXY_E_MISSING_COMMAND;
164 } else if (strcmp(name, "NotEntitled") == 0) {
165 err = INSTPROXY_E_NOT_ENTITLED;
166 } else if (strcmp(name, "MissingPackagePath") == 0) {
167 err = INSTPROXY_E_MISSING_PACKAGE_PATH;
168 } else if (strcmp(name, "MissingContainerPath") == 0) {
169 err = INSTPROXY_E_MISSING_CONTAINER_PATH;
170 } else if (strcmp(name, "MissingApplicationIdentifier") == 0) {
171 err = INSTPROXY_E_MISSING_APPLICATION_IDENTIFIER;
172 } else if (strcmp(name, "MissingAttributeValue") == 0) {
173 err = INSTPROXY_E_MISSING_ATTRIBUTE_VALUE;
174 } else if (strcmp(name, "LookupFailed") == 0) {
175 err = INSTPROXY_E_LOOKUP_FAILED;
176 } else if (strcmp(name, "DictCreationFailed") == 0) {
177 err = INSTPROXY_E_DICT_CREATION_FAILED;
178 } else if (strcmp(name, "InstallProhibited") == 0) {
179 err = INSTPROXY_E_INSTALL_PROHIBITED;
180 } else if (strcmp(name, "UninstallProhibited") == 0) {
181 err = INSTPROXY_E_UNINSTALL_PROHIBITED;
182 } else if (strcmp(name, "MissingBundleVersion") == 0) {
183 err = INSTPROXY_E_MISSING_BUNDLE_VERSION;
184 }
185
186 return err;
187 }
188
189 /**
190 * Locks an installation_proxy client, used for thread safety.
191 *
192 * @param client The installation_proxy client to lock
193 */
194 static void instproxy_lock(instproxy_client_t client)
195 {
196 debug_info("Locked");
197 mutex_lock(&client->mutex);
198 }
199
200 /**
201 * Unlocks an installation_proxy client, used for thread safety.
202 *
203 * @param client The installation_proxy client to lock
204 */
205 static void instproxy_unlock(instproxy_client_t client)
206 {
207 debug_info("Unlocked");
208 mutex_unlock(&client->mutex);
209 }
210
211 /**
212 * Converts a property_list_service_error_t value to an instproxy_error_t value.
213 * Used internally to get correct error codes.
214 *
215 * @param err A property_list_service_error_t error code
216 *
217 * @return A matching instproxy_error_t error code,
218 * INSTPROXY_E_UNKNOWN_ERROR otherwise.
219 */
220 static instproxy_error_t instproxy_error(property_list_service_error_t err)
221 {
222 switch (err) {
223 case PROPERTY_LIST_SERVICE_E_SUCCESS:
224 return INSTPROXY_E_SUCCESS;
225 case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
226 return INSTPROXY_E_INVALID_ARG;
227 case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
228 return INSTPROXY_E_PLIST_ERROR;
229 case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
230 return INSTPROXY_E_CONN_FAILED;
231 case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT:
232 return INSTPROXY_E_RECEIVE_TIMEOUT;
233 default:
234 break;
235 }
236 return INSTPROXY_E_UNKNOWN_ERROR;
237 }
238
239 instproxy_error_t instproxy_client_new(idevice_t device, lockdownd_service_descriptor_t service, instproxy_client_t *client)
240 {
241 property_list_service_client_t plistclient = NULL;
242 instproxy_error_t err = instproxy_error(property_list_service_client_new(device, service, &plistclient));
243 if (err != INSTPROXY_E_SUCCESS) {
244 return err;
245 }
246
247 instproxy_client_t client_loc = (instproxy_client_t) malloc(sizeof(struct instproxy_client_private));
248 client_loc->parent = plistclient;
249 mutex_init(&client_loc->mutex);
250 client_loc->receive_status_thread = THREAD_T_NULL;
251
252 *client = client_loc;
253 return INSTPROXY_E_SUCCESS;
254 }
255
256 instproxy_error_t instproxy_client_start_service(idevice_t device, instproxy_client_t * client, const char* label)
257 {
258 int32_t err = INSTPROXY_E_UNKNOWN_ERROR;
259 service_client_factory_start_service(device, INSTPROXY_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(instproxy_client_new), &err);
260 return err;
261 }
262
263 instproxy_error_t instproxy_client_free(instproxy_client_t client)
264 {
265 if (!client)
266 return INSTPROXY_E_INVALID_ARG;
267
268 property_list_service_client_t parent = client->parent;
269 client->parent = NULL;
270 if (client->receive_status_thread) {
271 debug_info("joining receive_status_thread");
272 thread_join(client->receive_status_thread);
273 thread_free(client->receive_status_thread);
274 client->receive_status_thread = THREAD_T_NULL;
275 }
276 property_list_service_client_free(parent);
277 mutex_destroy(&client->mutex);
278 free(client);
279
280 return INSTPROXY_E_SUCCESS;
281 }
282
283 /**
284 * Sends a command to the device.
285 * Only used internally.
286 *
287 * @param client The connected installation_proxy client.
288 * @param command The command to execute. Required.
289 *
290 * @return INSTPROXY_E_SUCCESS on success or an INSTPROXY_E_* error value if
291 * an error occurred.
292 */
293 static instproxy_error_t instproxy_send_command(instproxy_client_t client, plist_t command)
294 {
295 if (!client || !command)
296 return INSTPROXY_E_INVALID_ARG;
297
298 instproxy_error_t res = instproxy_error(property_list_service_send_xml_plist(client->parent, command));
299
300 if (res != INSTPROXY_E_SUCCESS) {
301 debug_info("could not send command plist, error %d", res);
302 return res;
303 }
304
305 return res;
306 }
307
308 /**
309 * Internally used function that will synchronously receive messages from
310 * the specified installation_proxy until it completes or an error occurs.
311 *
312 * If status_cb is not NULL, the callback function will be called each time
313 * a status update or error message is received.
314 *
315 * @param client The connected installation proxy client
316 * @param status_cb Pointer to a callback function or NULL
317 * @param command Operation specificiation in plist. Will be passed to the
318 * status_cb callback.
319 * @param user_data Callback data passed to status_cb.
320 */
321 static instproxy_error_t instproxy_receive_status_loop(instproxy_client_t client, plist_t command, instproxy_status_cb_t status_cb, void *user_data)
322 {
323 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
324 int complete = 0;
325 plist_t node = NULL;
326 char* command_name = NULL;
327 char* status_name = NULL;
328 char* error_name = NULL;
329 char* error_description = NULL;
330 uint64_t error_code = 0;
331 #ifndef STRIP_DEBUG_CODE
332 int percent_complete = 0;
333 #endif
334
335 instproxy_command_get_name(command, &command_name);
336
337 do {
338 /* receive status response */
339 instproxy_lock(client);
340 res = instproxy_error(property_list_service_receive_plist_with_timeout(client->parent, &node, 1000));
341 instproxy_unlock(client);
342
343 /* break out if we have a communication problem */
344 if (res != INSTPROXY_E_SUCCESS && res != INSTPROXY_E_RECEIVE_TIMEOUT) {
345 debug_info("could not receive plist, error %d", res);
346 break;
347 }
348
349 /* parse status response */
350 if (node) {
351 /* check status for possible error to allow reporting it and aborting it gracefully */
352 res = instproxy_status_get_error(node, &error_name, &error_description, &error_code);
353 if (res != INSTPROXY_E_SUCCESS) {
354 debug_info("command: %s, error %d, code 0x%08"PRIx64", name: %s, description: \"%s\"", command_name, res, error_code, error_name, error_description ? error_description: "N/A");
355 complete = 1;
356 }
357
358 if (error_name) {
359 free(error_name);
360 error_name = NULL;
361 }
362
363 if (error_description) {
364 free(error_description);
365 error_description = NULL;
366 }
367
368 /* check status from response */
369 instproxy_status_get_name(node, &status_name);
370 if (!status_name) {
371 debug_info("ignoring message without Status key:");
372 debug_plist(node);
373 } else {
374 if (!strcmp(status_name, "Complete")) {
375 complete = 1;
376 } else {
377 res = INSTPROXY_E_OP_IN_PROGRESS;
378 }
379 #ifndef STRIP_DEBUG_CODE
380 percent_complete = -1;
381 instproxy_status_get_percent_complete(node, &percent_complete);
382 if (percent_complete >= 0) {
383 debug_info("command: %s, status: %s, percent (%d%%)", command_name, status_name, percent_complete);
384 } else {
385 debug_info("command: %s, status: %s", command_name, status_name);
386 }
387 #endif
388 free(status_name);
389 status_name = NULL;
390 }
391
392 /* invoke status callback function */
393 if (status_cb) {
394 status_cb(command, node, user_data);
395 }
396
397 plist_free(node);
398 node = NULL;
399 }
400 } while (!complete && client->parent);
401
402 if (command_name)
403 free(command_name);
404
405 return res;
406 }
407
408 /**
409 * Internally used "receive status" thread function that will call the specified
410 * callback function when status update messages (or error messages) are
411 * received.
412 *
413 * @param arg Pointer to an allocated struct instproxy_status_data that holds
414 * the required data about the connected client and the callback function.
415 *
416 * @return Always NULL.
417 */
418 static void* instproxy_receive_status_loop_thread(void* arg)
419 {
420 struct instproxy_status_data *data = (struct instproxy_status_data*)arg;
421
422 /* run until the command is complete or an error occurs */
423 (void)instproxy_receive_status_loop(data->client, data->command, data->cbfunc, data->user_data);
424
425 /* cleanup */
426 instproxy_lock(data->client);
427
428 debug_info("done, cleaning up.");
429
430 if (data->command) {
431 plist_free(data->command);
432 }
433
434 if (data->client->receive_status_thread) {
435 thread_free(data->client->receive_status_thread);
436 data->client->receive_status_thread = THREAD_T_NULL;
437 }
438
439 instproxy_unlock(data->client);
440 free(data);
441
442 return NULL;
443 }
444
445 /**
446 * Internally used helper function that creates a "receive status" thread which
447 * will call the passed callback function when a status is received.
448 *
449 * If async is 0 no thread will be created and the command will run
450 * synchronously until it completes or an error occurs.
451 *
452 * @param client The connected installation proxy client
453 * @param command Operation name. Will be passed to the callback function
454 * in async mode or shown in debug messages in sync mode.
455 * @param async A boolean indicating if receive loop should be run
456 * asynchronously or block.
457 * @param status_cb Pointer to a callback function or NULL.
458 * @param user_data Callback data passed to status_cb.
459 *
460 * @return INSTPROXY_E_SUCCESS when the thread was created (async mode), or
461 * when the command completed successfully (sync).
462 * An INSTPROXY_E_* error value is returned if an error occurred.
463 */
464 static instproxy_error_t instproxy_receive_status_loop_with_callback(instproxy_client_t client, plist_t command, instproxy_command_type_t async, instproxy_status_cb_t status_cb, void *user_data)
465 {
466 if (!client || !client->parent || !command) {
467 return INSTPROXY_E_INVALID_ARG;
468 }
469
470 if (client->receive_status_thread) {
471 return INSTPROXY_E_OP_IN_PROGRESS;
472 }
473
474 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
475 if (async == INSTPROXY_COMMAND_TYPE_ASYNC) {
476 /* async mode */
477 struct instproxy_status_data *data = (struct instproxy_status_data*)malloc(sizeof(struct instproxy_status_data));
478 if (data) {
479 data->client = client;
480 data->command = plist_copy(command);
481 data->cbfunc = status_cb;
482 data->user_data = user_data;
483
484 if (thread_new(&client->receive_status_thread, instproxy_receive_status_loop_thread, data) == 0) {
485 res = INSTPROXY_E_SUCCESS;
486 }
487 }
488 } else {
489 /* sync mode as a fallback */
490 res = instproxy_receive_status_loop(client, command, status_cb, user_data);
491 }
492
493 return res;
494 }
495
496 /**
497 * Internal core function to send a command and process the response.
498 *
499 * @param client The connected installation_proxy client
500 * @param command The command specification dictionary.
501 * @param async A boolean indicating whether the receive loop should be run
502 * asynchronously or block until completing the command.
503 * @param status_cb Callback function to call if a command status is received.
504 * @param user_data Callback data passed to status_cb.
505 *
506 * @return INSTPROXY_E_SUCCESS on success or an INSTPROXY_E_* error value if
507 * an error occurred.
508 */
509 static instproxy_error_t instproxy_perform_command(instproxy_client_t client, plist_t command, instproxy_command_type_t async, instproxy_status_cb_t status_cb, void *user_data)
510 {
511 if (!client || !client->parent || !command) {
512 return INSTPROXY_E_INVALID_ARG;
513 }
514
515 if (client->receive_status_thread) {
516 return INSTPROXY_E_OP_IN_PROGRESS;
517 }
518
519 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
520
521 /* send command */
522 instproxy_lock(client);
523 res = instproxy_send_command(client, command);
524 instproxy_unlock(client);
525
526 /* loop until status or error is received */
527 res = instproxy_receive_status_loop_with_callback(client, command, async, status_cb, user_data);
528
529 return res;
530 }
531
532 instproxy_error_t instproxy_browse_with_callback(instproxy_client_t client, plist_t client_options, instproxy_status_cb_t status_cb, void *user_data)
533 {
534 if (!client || !client->parent || !status_cb)
535 return INSTPROXY_E_INVALID_ARG;
536
537 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
538
539 plist_t command = plist_new_dict();
540 plist_dict_set_item(command, "Command", plist_new_string("Browse"));
541 if (client_options)
542 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
543
544 res = instproxy_perform_command(client, command, INSTPROXY_COMMAND_TYPE_ASYNC, status_cb, (void*)user_data);
545
546 plist_free(command);
547
548 return res;
549 }
550
551 static void instproxy_append_current_list_to_result_cb(plist_t command, plist_t status, void *user_data)
552 {
553 plist_t *result_array = (plist_t*)user_data;
554 uint64_t current_amount = 0;
555 plist_t current_list = NULL;
556 uint64_t i;
557
558 instproxy_status_get_current_list(status, NULL, NULL, &current_amount, &current_list);
559
560 debug_info("current_amount: %d", current_amount);
561
562 if (current_amount > 0) {
563 for (i = 0; current_list && (i < current_amount); i++) {
564 plist_t item = plist_array_get_item(current_list, i);
565 plist_array_append_item(*result_array, plist_copy(item));
566 }
567 }
568
569 if (current_list)
570 plist_free(current_list);
571 }
572
573 instproxy_error_t instproxy_browse(instproxy_client_t client, plist_t client_options, plist_t *result)
574 {
575 if (!client || !client->parent || !result)
576 return INSTPROXY_E_INVALID_ARG;
577
578 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
579
580 plist_t result_array = plist_new_array();
581
582 plist_t command = plist_new_dict();
583 plist_dict_set_item(command, "Command", plist_new_string("Browse"));
584 if (client_options)
585 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
586
587 res = instproxy_perform_command(client, command, INSTPROXY_COMMAND_TYPE_SYNC, instproxy_append_current_list_to_result_cb, (void*)&result_array);
588
589 if (res == INSTPROXY_E_SUCCESS) {
590 *result = result_array;
591 } else {
592 plist_free(result_array);
593 }
594
595 plist_free(command);
596
597 return res;
598 }
599
600 static void instproxy_copy_lookup_result_cb(plist_t command, plist_t status, void *user_data)
601 {
602 plist_t* result = (plist_t*)user_data;
603
604 plist_t node = plist_dict_get_item(status, "LookupResult");
605 if (node) {
606 *result = plist_copy(node);
607 }
608 }
609
610 instproxy_error_t instproxy_lookup(instproxy_client_t client, const char** appids, plist_t client_options, plist_t *result)
611 {
612 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
613 int i = 0;
614 plist_t lookup_result = NULL;
615 plist_t command = NULL;
616 plist_t appid_array = NULL;
617 plist_t node = NULL;
618
619 if (!client || !client->parent || !result)
620 return INSTPROXY_E_INVALID_ARG;
621
622 command = plist_new_dict();
623 plist_dict_set_item(command, "Command", plist_new_string("Lookup"));
624 if (client_options) {
625 node = plist_copy(client_options);
626 } else if (appids) {
627 node = plist_new_dict();
628 }
629
630 /* add bundle identifiers to client options */
631 if (appids) {
632 appid_array = plist_new_array();
633 while (appids[i]) {
634 plist_array_append_item(appid_array, plist_new_string(appids[i]));
635 i++;
636 }
637 plist_dict_set_item(node, "BundleIDs", appid_array);
638 }
639
640 if (node) {
641 plist_dict_set_item(command, "ClientOptions", node);
642 }
643
644 res = instproxy_perform_command(client, command, INSTPROXY_COMMAND_TYPE_SYNC, instproxy_copy_lookup_result_cb, (void*)&lookup_result);
645
646 if (res == INSTPROXY_E_SUCCESS) {
647 *result = lookup_result;
648 } else {
649 plist_free(lookup_result);
650 }
651
652 plist_free(command);
653
654 return res;
655 }
656
657 instproxy_error_t instproxy_install(instproxy_client_t client, const char *pkg_path, plist_t client_options, instproxy_status_cb_t status_cb, void *user_data)
658 {
659 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
660
661 plist_t command = plist_new_dict();
662 plist_dict_set_item(command, "Command", plist_new_string("Install"));
663 if (client_options)
664 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
665 plist_dict_set_item(command, "PackagePath", plist_new_string(pkg_path));
666
667 res = instproxy_perform_command(client, command, status_cb == NULL ? INSTPROXY_COMMAND_TYPE_SYNC : INSTPROXY_COMMAND_TYPE_ASYNC, status_cb, user_data);
668
669 plist_free(command);
670
671 return res;
672 }
673
674 instproxy_error_t instproxy_upgrade(instproxy_client_t client, const char *pkg_path, plist_t client_options, instproxy_status_cb_t status_cb, void *user_data)
675 {
676 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
677
678 plist_t command = plist_new_dict();
679 plist_dict_set_item(command, "Command", plist_new_string("Upgrade"));
680 if (client_options)
681 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
682 plist_dict_set_item(command, "PackagePath", plist_new_string(pkg_path));
683
684 res = instproxy_perform_command(client, command, status_cb == NULL ? INSTPROXY_COMMAND_TYPE_SYNC : INSTPROXY_COMMAND_TYPE_ASYNC, status_cb, user_data);
685
686 plist_free(command);
687
688 return res;
689 }
690
691 instproxy_error_t instproxy_uninstall(instproxy_client_t client, const char *appid, plist_t client_options, instproxy_status_cb_t status_cb, void *user_data)
692 {
693 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
694
695 plist_t command = plist_new_dict();
696 plist_dict_set_item(command, "Command", plist_new_string("Uninstall"));
697 if (client_options)
698 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
699 plist_dict_set_item(command, "ApplicationIdentifier", plist_new_string(appid));
700
701 res = instproxy_perform_command(client, command, status_cb == NULL ? INSTPROXY_COMMAND_TYPE_SYNC : INSTPROXY_COMMAND_TYPE_ASYNC, status_cb, user_data);
702
703 plist_free(command);
704
705 return res;
706 }
707
708 instproxy_error_t instproxy_lookup_archives(instproxy_client_t client, plist_t client_options, plist_t *result)
709 {
710 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
711
712 plist_t command = plist_new_dict();
713 plist_dict_set_item(command, "Command", plist_new_string("LookupArchives"));
714 if (client_options)
715 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
716
717 res = instproxy_perform_command(client, command, INSTPROXY_COMMAND_TYPE_SYNC, instproxy_copy_lookup_result_cb, (void*)result);
718
719 plist_free(command);
720
721 return res;
722 }
723
724 instproxy_error_t instproxy_archive(instproxy_client_t client, const char *appid, plist_t client_options, instproxy_status_cb_t status_cb, void *user_data)
725 {
726 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
727
728 plist_t command = plist_new_dict();
729 plist_dict_set_item(command, "Command", plist_new_string("Archive"));
730 if (client_options)
731 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
732 plist_dict_set_item(command, "ApplicationIdentifier", plist_new_string(appid));
733
734 res = instproxy_perform_command(client, command, status_cb == NULL ? INSTPROXY_COMMAND_TYPE_SYNC : INSTPROXY_COMMAND_TYPE_ASYNC, status_cb, user_data);
735
736 plist_free(command);
737
738 return res;
739 }
740
741 instproxy_error_t instproxy_restore(instproxy_client_t client, const char *appid, plist_t client_options, instproxy_status_cb_t status_cb, void *user_data)
742 {
743 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
744
745 plist_t command = plist_new_dict();
746 plist_dict_set_item(command, "Command", plist_new_string("Restore"));
747 if (client_options)
748 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
749 plist_dict_set_item(command, "ApplicationIdentifier", plist_new_string(appid));
750
751 res = instproxy_perform_command(client, command, status_cb == NULL ? INSTPROXY_COMMAND_TYPE_SYNC : INSTPROXY_COMMAND_TYPE_ASYNC, status_cb, user_data);
752
753 plist_free(command);
754
755 return res;
756 }
757
758 instproxy_error_t instproxy_remove_archive(instproxy_client_t client, const char *appid, plist_t client_options, instproxy_status_cb_t status_cb, void *user_data)
759 {
760 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
761
762 plist_t command = plist_new_dict();
763 plist_dict_set_item(command, "Command", plist_new_string("RemoveArchive"));
764 if (client_options)
765 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
766 plist_dict_set_item(command, "ApplicationIdentifier", plist_new_string(appid));
767
768 res = instproxy_perform_command(client, command, status_cb == NULL ? INSTPROXY_COMMAND_TYPE_SYNC : INSTPROXY_COMMAND_TYPE_ASYNC, status_cb, user_data);
769
770 plist_free(command);
771
772 return res;
773 }
774
775 instproxy_error_t instproxy_check_capabilities_match(instproxy_client_t client, const char** capabilities, plist_t client_options, plist_t *result)
776 {
777 if (!client || !capabilities || !result)
778 return INSTPROXY_E_INVALID_ARG;
779
780 plist_t lookup_result = NULL;
781
782 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
783
784 plist_t command = plist_new_dict();
785 plist_dict_set_item(command, "Command", plist_new_string("CheckCapabilitiesMatch"));
786 if (client_options)
787 plist_dict_set_item(command, "ClientOptions", plist_copy(client_options));
788
789 if (capabilities) {
790 int i = 0;
791 plist_t capabilities_array = plist_new_array();
792 while (capabilities[i]) {
793 plist_array_append_item(capabilities_array, plist_new_string(capabilities[i]));
794 i++;
795 }
796 plist_dict_set_item(command, "Capabilities", capabilities_array);
797 }
798
799 res = instproxy_perform_command(client, command, INSTPROXY_COMMAND_TYPE_SYNC, instproxy_copy_lookup_result_cb, (void*)&lookup_result);
800
801 if (res == INSTPROXY_E_SUCCESS) {
802 *result = lookup_result;
803 } else {
804 plist_free(lookup_result);
805 }
806
807 plist_free(command);
808
809 return res;
810 }
811
812 instproxy_error_t instproxy_status_get_error(plist_t status, char **name, char** description, uint64_t* code)
813 {
814 instproxy_error_t res = INSTPROXY_E_UNKNOWN_ERROR;
815
816 if (!status || !name)
817 return INSTPROXY_E_INVALID_ARG;
818
819 plist_t node = plist_dict_get_item(status, "Error");
820 if (node) {
821 plist_get_string_val(node, name);
822 } else {
823 /* no error here */
824 res = INSTPROXY_E_SUCCESS;
825 }
826
827 if (code != NULL) {
828 *code = 0;
829 node = plist_dict_get_item(status, "ErrorDetail");
830 if (node) {
831 plist_get_uint_val(node, code);
832 *code &= 0xffffffff;
833 }
834 }
835
836 if (description != NULL) {
837 node = plist_dict_get_item(status, "ErrorDescription");
838 if (node) {
839 plist_get_string_val(node, description);
840 }
841 }
842
843 if (*name) {
844 res = instproxy_strtoerr(*name);
845 }
846
847 return res;
848 }
849
850 void instproxy_status_get_name(plist_t status, char **name)
851 {
852 if (name) {
853 plist_t node = plist_dict_get_item(status, "Status");
854 if (node) {
855 plist_get_string_val(node, name);
856 } else {
857 *name = NULL;
858 }
859 }
860 }
861
862 void instproxy_status_get_percent_complete(plist_t status, int *percent)
863 {
864 uint64_t val = 0;
865 if (percent) {
866 plist_t node = plist_dict_get_item(status, "PercentComplete");
867 if (node) {
868 plist_get_uint_val(node, &val);
869 *percent = val;
870 }
871 }
872 }
873
874 void instproxy_status_get_current_list(plist_t status, uint64_t* total, uint64_t* current_index, uint64_t* current_amount, plist_t* list)
875 {
876 plist_t node = NULL;
877
878 if (status && plist_get_node_type(status) == PLIST_DICT) {
879 /* command specific logic: parse browsed list */
880 if (list != NULL) {
881 node = plist_dict_get_item(status, "CurrentList");
882 if (node) {
883 *current_amount = plist_array_get_size(node);
884 *list = plist_copy(node);
885 }
886 }
887
888 if (total != NULL) {
889 node = plist_dict_get_item(status, "Total");
890 if (node) {
891 plist_get_uint_val(node, total);
892 }
893 }
894
895 if (current_amount != NULL) {
896 node = plist_dict_get_item(status, "CurrentAmount");
897 if (node) {
898 plist_get_uint_val(node, current_amount);
899 }
900 }
901
902 if (current_index != NULL) {
903 node = plist_dict_get_item(status, "CurrentIndex");
904 if (node) {
905 plist_get_uint_val(node, current_index);
906 }
907 }
908 }
909 }
910
911 void instproxy_command_get_name(plist_t command, char** name)
912 {
913 if (name) {
914 plist_t node = plist_dict_get_item(command, "Command");
915 if (node) {
916 plist_get_string_val(node, name);
917 } else {
918 *name = NULL;
919 }
920 }
921 }
922
923 plist_t instproxy_client_options_new(void)
924 {
925 return plist_new_dict();
926 }
927
928 void instproxy_client_options_add(plist_t client_options, ...)
929 {
930 if (!client_options)
931 return;
932
933 va_list args;
934 va_start(args, client_options);
935 char *arg = va_arg(args, char*);
936 while (arg) {
937 char *key = strdup(arg);
938 if (!strcmp(key, "SkipUninstall")) {
939 int intval = va_arg(args, int);
940 plist_dict_set_item(client_options, key, plist_new_bool(intval));
941 } else if (!strcmp(key, "ApplicationSINF") || !strcmp(key, "iTunesMetadata") || !strcmp(key, "ReturnAttributes") || !strcmp(key, "BundleIDs")) {
942 plist_t plistval = va_arg(args, plist_t);
943 if (!plistval) {
944 free(key);
945 break;
946 }
947 plist_dict_set_item(client_options, key, plist_copy(plistval));
948 } else {
949 char *strval = va_arg(args, char*);
950 if (!strval) {
951 free(key);
952 break;
953 }
954 plist_dict_set_item(client_options, key, plist_new_string(strval));
955 }
956 free(key);
957 arg = va_arg(args, char*);
958 }
959 va_end(args);
960 }
961
962 void instproxy_client_options_set_return_attributes(plist_t client_options, ...)
963 {
964 if (!client_options)
965 return;
966
967 plist_t return_attributes = plist_new_array();
968
969 va_list args;
970 va_start(args, client_options);
971 char *arg = va_arg(args, char*);
972 while (arg) {
973 char *attribute = strdup(arg);
974 plist_array_append_item(return_attributes, plist_new_string(attribute));
975 free(attribute);
976 arg = va_arg(args, char*);
977 }
978 va_end(args);
979
980 plist_dict_set_item(client_options, "ReturnAttributes", return_attributes);
981 }
982
983 void instproxy_client_options_free(plist_t client_options)
984 {
985 if (client_options) {
986 plist_free(client_options);
987 }
988 }
989
990 instproxy_error_t instproxy_client_get_path_for_bundle_identifier(instproxy_client_t client, const char* bundle_id, char** path)
991 {
992 if (!client || !client->parent || !bundle_id)
993 return INSTPROXY_E_INVALID_ARG;
994
995 plist_t apps = NULL;
996
997 // create client options for any application types
998 plist_t client_opts = instproxy_client_options_new();
999 instproxy_client_options_add(client_opts, "ApplicationType", "Any", NULL);
1000
1001 // only return attributes we need
1002 instproxy_client_options_set_return_attributes(client_opts, "CFBundleIdentifier", "CFBundleExecutable", "Path", NULL);
1003
1004 // only query for specific appid
1005 const char* appids[] = {bundle_id, NULL};
1006
1007 // query device for list of apps
1008 instproxy_error_t ierr = instproxy_lookup(client, appids, client_opts, &apps);
1009
1010 instproxy_client_options_free(client_opts);
1011
1012 if (ierr != INSTPROXY_E_SUCCESS) {
1013 return ierr;
1014 }
1015
1016 plist_t app_found = plist_access_path(apps, 1, bundle_id);
1017 if (!app_found) {
1018 if (apps)
1019 plist_free(apps);
1020 *path = NULL;
1021 return INSTPROXY_E_OP_FAILED;
1022 }
1023
1024 char* path_str = NULL;
1025 plist_t path_p = plist_dict_get_item(app_found, "Path");
1026 if (path_p) {
1027 plist_get_string_val(path_p, &path_str);
1028 }
1029
1030 char* exec_str = NULL;
1031 plist_t exec_p = plist_dict_get_item(app_found, "CFBundleExecutable");
1032 if (exec_p) {
1033 plist_get_string_val(exec_p, &exec_str);
1034 }
1035
1036 if (!path_str) {
1037 debug_info("app path not found");
1038 return INSTPROXY_E_OP_FAILED;
1039 }
1040
1041 if (!exec_str) {
1042 debug_info("bundle executable not found");
1043 return INSTPROXY_E_OP_FAILED;
1044 }
1045
1046 plist_free(apps);
1047
1048 char* ret = (char*)malloc(strlen(path_str) + 1 + strlen(exec_str) + 1);
1049 strcpy(ret, path_str);
1050 strcat(ret, "/");
1051 strcat(ret, exec_str);
1052
1053 *path = ret;
1054
1055 if (path_str) {
1056 free(path_str);
1057 }
1058
1059 if (exec_str) {
1060 free(exec_str);
1061 }
1062
1063 return INSTPROXY_E_SUCCESS;
1064 }