Debian Codegraph @

tools/ideviceprovision.c

raw
1 /*
2 * ideviceprovision.c
3 * Simple utility to install, get, or remove provisioning profiles
4 * to/from idevices
5 *
6 * Copyright (c) 2012-2016 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
27 #define TOOL_NAME "ideviceprovision"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <getopt.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35 #ifndef _WIN32
36 #include <signal.h>
37 #endif
38
39 #ifdef _WIN32
40 #include <winsock2.h>
41 #include <windows.h>
42 #else
43 #include <arpa/inet.h>
44 #endif
45
46 #include <libimobiledevice/libimobiledevice.h>
47 #include <libimobiledevice/lockdown.h>
48 #include <libimobiledevice/misagent.h>
49 #include <plist/plist.h>
50
51 static void print_usage(int argc, char **argv, int is_error)
52 {
53 char *name = strrchr(argv[0], '/');
54 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0]));
55 fprintf(is_error ? stderr : stdout,
56 "\n"
57 "Manage provisioning profiles on a device.\n"
58 "\n"
59 "Where COMMAND is one of:\n"
60 " install FILE Installs the provisioning profile specified by FILE.\n"
61 " A valid .mobileprovision file is expected.\n"
62 " list Get a list of all provisioning profiles on the device.\n"
63 " copy PATH Retrieves all provisioning profiles from the device and\n"
64 " stores them into the existing directory specified by PATH.\n"
65 " The files will be stored as UUID.mobileprovision\n"
66 " copy UUID PATH Retrieves the provisioning profile identified by UUID\n"
67 " from the device and stores it into the existing directory\n"
68 " specified by PATH. The file will be stored as UUID.mobileprovision.\n"
69 " remove UUID Removes the provisioning profile identified by UUID.\n"
70 " remove-all Removes all installed provisioning profiles.\n"
71 " dump FILE Prints detailed information about the provisioning profile\n"
72 " specified by FILE.\n"
73 "\n"
74 "The following OPTIONS are accepted:\n"
75 " -u, --udid UDID target specific device by UDID\n"
76 " -n, --network connect to network device\n"
77 " -x, --xml print XML output when using the 'dump' command\n"
78 " -d, --debug enable communication debugging\n"
79 " -h, --help prints usage information\n"
80 " -v, --version prints version information\n"
81 "\n"
82 "Homepage: <" PACKAGE_URL ">\n"
83 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
84 );
85 }
86
87 enum {
88 OP_INSTALL,
89 OP_LIST,
90 OP_COPY,
91 OP_REMOVE,
92 OP_DUMP,
93 NUM_OPS
94 };
95
96 #define ASN1_SEQUENCE 0x30
97 #define ASN1_CONTAINER 0xA0
98 #define ASN1_OBJECT_IDENTIFIER 0x06
99 #define ASN1_OCTET_STRING 0x04
100
101 static void asn1_next_item(unsigned char** p)
102 {
103 char bsize = *(*p+1);
104 if (bsize & 0x80) {
105 *p += 2 + (bsize & 0xF);
106 } else {
107 *p += 3;
108 }
109 }
110
111 static size_t asn1_item_get_size(const unsigned char* p)
112 {
113 size_t res = 0;
114 char bsize = *(p+1);
115 if (bsize & 0x80) {
116 uint16_t ws = 0;
117 uint32_t ds = 0;
118 switch (bsize & 0xF) {
119 case 2:
120 ws = *(uint16_t*)(p+2);
121 res = ntohs(ws);
122 break;
123 case 3:
124 ds = *(uint32_t*)(p+2);
125 res = ntohl(ds) >> 8;
126 break;
127 case 4:
128 ds = *(uint32_t*)(p+2);
129 res = ntohl(ds);
130 break;
131 default:
132 fprintf(stderr, "ERROR: Invalid or unimplemented byte size %d\n", bsize & 0xF);
133 break;
134 }
135 } else {
136 res = (int)bsize;
137 }
138 return res;
139 }
140
141 static void asn1_skip_item(unsigned char** p)
142 {
143 size_t sz = asn1_item_get_size(*p);
144 *p += 2;
145 *p += sz;
146 }
147
148 static plist_t profile_get_embedded_plist(plist_t profile)
149 {
150 if (plist_get_node_type(profile) != PLIST_DATA) {
151 fprintf(stderr, "%s: unexpected plist node type for profile (PLIST_DATA expected)\n", __func__);
152 return NULL;
153 }
154 char* bbuf = NULL;
155 uint64_t blen = 0;
156 plist_get_data_val(profile, &bbuf, &blen);
157 if (!bbuf) {
158 fprintf(stderr, "%s: could not get data value from plist node\n", __func__);
159 return NULL;
160 }
161
162 unsigned char* pp = (unsigned char*)bbuf;
163
164 if (*pp != ASN1_SEQUENCE) {
165 free(bbuf);
166 fprintf(stderr, "%s: unexpected profile data (0)\n", __func__);
167 return NULL;
168 }
169 size_t slen = asn1_item_get_size(pp);
170 char bsize = *(pp+1);
171 if (bsize & 0x80) {
172 slen += 2 + (bsize & 0xF);
173 } else {
174 slen += 3;
175 }
176 if (slen != blen) {
177 free(bbuf);
178 fprintf(stderr, "%s: unexpected profile data (1)\n", __func__);
179 return NULL;
180 }
181 asn1_next_item(&pp);
182
183 if (*pp != ASN1_OBJECT_IDENTIFIER) {
184 free(bbuf);
185 fprintf(stderr, "%s: unexpected profile data (2)\n", __func__);
186 return NULL;
187 }
188 asn1_skip_item(&pp);
189
190 if (*pp != ASN1_CONTAINER) {
191 free(bbuf);
192 fprintf(stderr, "%s: unexpected profile data (3)\n", __func__);
193 return NULL;
194 }
195 asn1_next_item(&pp);
196
197 if (*pp != ASN1_SEQUENCE) {
198 free(bbuf);
199 fprintf(stderr, "%s: unexpected profile data (4)\n", __func__);
200 return NULL;
201 }
202 asn1_next_item(&pp);
203
204 int k = 0;
205 // go to the 3rd element (skip 2)
206 while (k < 2) {
207 asn1_skip_item(&pp);
208 k++;
209 }
210 if (*pp != ASN1_SEQUENCE) {
211 free(bbuf);
212 fprintf(stderr, "%s: unexpected profile data (5)\n", __func__);
213 return NULL;
214 }
215 asn1_next_item(&pp);
216
217 if (*pp != ASN1_OBJECT_IDENTIFIER) {
218 free(bbuf);
219 fprintf(stderr, "%s: unexpected profile data (6)\n", __func__);
220 return NULL;
221 }
222 asn1_skip_item(&pp);
223
224 if (*pp != ASN1_CONTAINER) {
225 free(bbuf);
226 fprintf(stderr, "%s: unexpected profile data (7)\n", __func__);
227 return NULL;
228 }
229 asn1_next_item(&pp);
230
231 if (*pp != ASN1_OCTET_STRING) {
232 free(bbuf);
233 fprintf(stderr, "%s: unexpected profile data (8)\n", __func__);
234 return NULL;
235 }
236 slen = asn1_item_get_size(pp);
237 asn1_next_item(&pp);
238
239 plist_t pl = NULL;
240 plist_from_xml((char*)pp, slen, &pl);
241 free(bbuf);
242
243 return pl;
244 }
245
246 static int profile_read_from_file(const char* path, unsigned char **profile_data, unsigned int *profile_size)
247 {
248 FILE* f = fopen(path, "rb");
249 if (!f) {
250 fprintf(stderr, "Could not open file '%s'\n", path);
251 return -1;
252 }
253 fseek(f, 0, SEEK_END);
254 long int size = ftell(f);
255 fseek(f, 0, SEEK_SET);
256
257 if (size >= 0x1000000) {
258 fprintf(stderr, "The file '%s' is too large for processing.\n", path);
259 fclose(f);
260 return -1;
261 }
262
263 unsigned char* buf = malloc(size);
264 if (!buf) {
265 fprintf(stderr, "Could not allocate memory...\n");
266 fclose(f);
267 return -1;
268 }
269
270 long int cur = 0;
271 while (cur < size) {
272 ssize_t r = fread(buf+cur, 1, 512, f);
273 if (r <= 0) {
274 break;
275 }
276 cur += r;
277 }
278 fclose(f);
279
280 if (cur != size) {
281 free(buf);
282 fprintf(stderr, "Could not read in file '%s' (size %ld read %ld)\n", path, size, cur);
283 return -1;
284 }
285
286 *profile_data = buf;
287 *profile_size = (unsigned int)size;
288
289 return 0;
290 }
291
292 int main(int argc, char *argv[])
293 {
294 lockdownd_client_t client = NULL;
295 lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
296 lockdownd_service_descriptor_t service = NULL;
297 idevice_t device = NULL;
298 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
299 int res = 0;
300 int i;
301 int op = -1;
302 int output_xml = 0;
303 const char* udid = NULL;
304 const char* param = NULL;
305 const char* param2 = NULL;
306 int use_network = 0;
307 int c = 0;
308 const struct option longopts[] = {
309 { "debug", no_argument, NULL, 'd' },
310 { "help", no_argument, NULL, 'h' },
311 { "udid", required_argument, NULL, 'u' },
312 { "network", no_argument, NULL, 'n' },
313 { "version", no_argument, NULL, 'v' },
314 { "xml", no_argument, NULL, 'x' },
315 { NULL, 0, NULL, 0}
316 };
317
318 #ifndef _WIN32
319 signal(SIGPIPE, SIG_IGN);
320 #endif
321 /* parse cmdline args */
322 while ((c = getopt_long(argc, argv, "dhu:nvx", longopts, NULL)) != -1) {
323 switch (c) {
324 case 'd':
325 idevice_set_debug_level(1);
326 break;
327 case 'u':
328 if (!*optarg) {
329 fprintf(stderr, "ERROR: UDID argument must not be empty!\n");
330 print_usage(argc, argv, 1);
331 return 2;
332 }
333 udid = optarg;
334 break;
335 case 'n':
336 use_network = 1;
337 break;
338 case 'h':
339 print_usage(argc, argv, 0);
340 return 0;
341 case 'v':
342 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
343 return 0;
344 case 'x':
345 output_xml = 1;
346 break;
347 default:
348 print_usage(argc, argv, 1);
349 return 2;
350 }
351 }
352 argc -= optind;
353 argv += optind;
354
355 if (!argv[0]) {
356 fprintf(stderr, "ERROR: Missing command.\n");
357 print_usage(argc+optind, argv-optind, 1);
358 return 2;
359 }
360
361 i = 0;
362 if (!strcmp(argv[i], "install")) {
363 op = OP_INSTALL;
364 i++;
365 if (!argv[i] || !*argv[i]) {
366 fprintf(stderr, "Missing argument for 'install' command.\n");
367 print_usage(argc+optind, argv-optind, 1);
368 return 2;
369 }
370 param = argv[i];
371 }
372 else if (!strcmp(argv[i], "list")) {
373 op = OP_LIST;
374 }
375 else if (!strcmp(argv[i], "copy")) {
376 op = OP_COPY;
377 i++;
378 if (!argv[i] || !*argv[i]) {
379 fprintf(stderr, "Missing argument for 'copy' command.\n");
380 print_usage(argc+optind, argv-optind, 1);
381 return 2;
382 }
383 param = argv[i];
384 i++;
385 if (argv[i] && (strlen(argv[i]) > 0)) {
386 param2 = argv[i];
387 }
388 }
389 else if (!strcmp(argv[i], "remove")) {
390 op = OP_REMOVE;
391 i++;
392 if (!argv[i] || !*argv[i]) {
393 fprintf(stderr, "Missing argument for 'remove' command.\n");
394 print_usage(argc+optind, argv-optind, 1);
395 return 2;
396 }
397 param = argv[i];
398 }
399 else if (!strcmp(argv[i], "remove-all")) {
400 op = OP_REMOVE;
401 }
402 else if (!strcmp(argv[i], "dump")) {
403 op = OP_DUMP;
404 i++;
405 if (!argv[i] || !*argv[i]) {
406 fprintf(stderr, "Missing argument for 'remove' command.\n");
407 print_usage(argc+optind, argv-optind, 1);
408 return 2;
409 }
410 param = argv[i];
411 }
412 if ((op == -1) || (op >= NUM_OPS)) {
413 fprintf(stderr, "ERROR: Unsupported command '%s'\n", argv[i]);
414 print_usage(argc+optind, argv-optind, 1);
415 return 2;
416 }
417
418 if (op == OP_DUMP) {
419 unsigned char* profile_data = NULL;
420 unsigned int profile_size = 0;
421 if (profile_read_from_file(param, &profile_data, &profile_size) != 0) {
422 return -1;
423 }
424 plist_t pdata = plist_new_data((char*)profile_data, profile_size);
425 plist_t pl = profile_get_embedded_plist(pdata);
426 plist_free(pdata);
427 free(profile_data);
428
429 if (pl) {
430 if (output_xml) {
431 char* xml = NULL;
432 uint32_t xlen = 0;
433 plist_to_xml(pl, &xml, &xlen);
434 if (xml) {
435 printf("%s\n", xml);
436 free(xml);
437 }
438 } else {
439 if (pl && (plist_get_node_type(pl) == PLIST_DICT)) {
440 plist_write_to_stream(pl, stdout, PLIST_FORMAT_LIMD, 0);
441 } else {
442 fprintf(stderr, "ERROR: unexpected node type in profile plist (not PLIST_DICT)\n");
443 res = -1;
444 }
445 }
446 } else {
447 fprintf(stderr, "ERROR: could not extract embedded plist from profile!\n");
448 }
449 plist_free(pl);
450
451 return res;
452 }
453
454 if (op == OP_COPY) {
455 struct stat st;
456 const char *checkdir = (param2) ? param2 : param;
457 if ((stat(checkdir, &st) < 0) || !S_ISDIR(st.st_mode)) {
458 fprintf(stderr, "ERROR: %s does not exist or is not a directory!\n", checkdir);
459 return -1;
460 }
461 }
462
463 ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX);
464 if (ret != IDEVICE_E_SUCCESS) {
465 if (udid) {
466 printf("No device found with udid %s.\n", udid);
467 } else {
468 printf("No device found.\n");
469 }
470 return -1;
471 }
472
473 if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &client, TOOL_NAME))) {
474 fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", ldret);
475 idevice_free(device);
476 return -1;
477 }
478
479 unsigned int device_version = idevice_get_device_version(device);
480
481 lockdownd_error_t lerr = lockdownd_start_service(client, MISAGENT_SERVICE_NAME, &service);
482 if (lerr != LOCKDOWN_E_SUCCESS) {
483 fprintf(stderr, "Could not start service %s: %s\n", MISAGENT_SERVICE_NAME, lockdownd_strerror(lerr));
484 lockdownd_client_free(client);
485 idevice_free(device);
486 return -1;
487 }
488 lockdownd_client_free(client);
489 client = NULL;
490
491 misagent_client_t mis = NULL;
492 if (misagent_client_new(device, service, &mis) != MISAGENT_E_SUCCESS) {
493 fprintf(stderr, "Could not connect to %s on device\n", MISAGENT_SERVICE_NAME);
494 if (service)
495 lockdownd_service_descriptor_free(service);
496 lockdownd_client_free(client);
497 idevice_free(device);
498 return -1;
499 }
500
501 if (service)
502 lockdownd_service_descriptor_free(service);
503
504 switch (op) {
505 case OP_INSTALL:
506 {
507 unsigned char* profile_data = NULL;
508 unsigned int profile_size = 0;
509 if (profile_read_from_file(param, &profile_data, &profile_size) != 0) {
510 break;
511 }
512
513 uint64_t psize = profile_size;
514 plist_t pdata = plist_new_data((const char*)profile_data, psize);
515 free(profile_data);
516
517 if (misagent_install(mis, pdata) == MISAGENT_E_SUCCESS) {
518 printf("Profile '%s' installed successfully.\n", param);
519 } else {
520 int sc = misagent_get_status_code(mis);
521 fprintf(stderr, "Could not install profile '%s', status code: 0x%x\n", param, sc);
522 }
523 }
524 break;
525 case OP_LIST:
526 case OP_COPY:
527 {
528 plist_t profiles = NULL;
529 misagent_error_t merr;
530 if (device_version < IDEVICE_DEVICE_VERSION(9,3,0)) {
531 merr = misagent_copy(mis, &profiles);
532 } else {
533 merr = misagent_copy_all(mis, &profiles);
534 }
535 if (merr == MISAGENT_E_SUCCESS) {
536 int found_match = 0;
537 uint32_t num_profiles = plist_array_get_size(profiles);
538 if (op == OP_LIST || !param2) {
539 printf("Device has %d provisioning %s installed:\n", num_profiles, (num_profiles == 1) ? "profile" : "profiles");
540 }
541 uint32_t j;
542 for (j = 0; !found_match && j < num_profiles; j++) {
543 char* p_name = NULL;
544 char* p_uuid = NULL;
545 plist_t profile = plist_array_get_item(profiles, j);
546 plist_t pl = profile_get_embedded_plist(profile);
547 if (pl && (plist_get_node_type(pl) == PLIST_DICT)) {
548 plist_t node;
549 node = plist_dict_get_item(pl, "Name");
550 if (node && (plist_get_node_type(node) == PLIST_STRING)) {
551 plist_get_string_val(node, &p_name);
552 }
553 node = plist_dict_get_item(pl, "UUID");
554 if (node && (plist_get_node_type(node) == PLIST_STRING)) {
555 plist_get_string_val(node, &p_uuid);
556 }
557 }
558 if (param2) {
559 if (p_uuid && !strcmp(p_uuid, param)) {
560 found_match = 1;
561 } else {
562 free(p_uuid);
563 free(p_name);
564 continue;
565 }
566 }
567 printf("%s - %s\n", (p_uuid) ? p_uuid : "(unknown id)", (p_name) ? p_name : "(no name)");
568 if (op == OP_COPY) {
569 char pfname[512];
570 if (p_uuid) {
571 sprintf(pfname, "%s/%s.mobileprovision", (param2) ? param2 : param, p_uuid);
572 } else {
573 sprintf(pfname, "%s/profile%d.mobileprovision", (param2) ? param2 : param, j);
574 }
575 FILE* f = fopen(pfname, "wb");
576 if (f) {
577 char* dt = NULL;
578 uint64_t ds = 0;
579 plist_get_data_val(profile, &dt, &ds);
580 fwrite(dt, 1, ds, f);
581 fclose(f);
582 printf(" => %s\n", pfname);
583 } else {
584 fprintf(stderr, "Could not open '%s' for writing: %s\n", pfname, strerror(errno));
585 }
586 }
587 free(p_uuid);
588 free(p_name);
589 }
590 if (param2 && !found_match) {
591 fprintf(stderr, "Profile '%s' was not found on the device.\n", param);
592 res = -1;
593 }
594 } else {
595 int sc = misagent_get_status_code(mis);
596 fprintf(stderr, "Could not get installed profiles from device, status code: 0x%x\n", sc);
597 res = -1;
598 }
599 plist_free(profiles);
600 }
601 break;
602 case OP_REMOVE:
603 if (param) {
604 /* remove specified provisioning profile */
605 if (misagent_remove(mis, param) == MISAGENT_E_SUCCESS) {
606 printf("Profile '%s' removed.\n", param);
607 } else {
608 int sc = misagent_get_status_code(mis);
609 fprintf(stderr, "Could not remove profile '%s', status code 0x%x\n", param, sc);
610 }
611 } else {
612 /* remove all provisioning profiles */
613 plist_t profiles = NULL;
614 misagent_error_t merr;
615 if (device_version < IDEVICE_DEVICE_VERSION(9,3,0)) {
616 merr = misagent_copy(mis, &profiles);
617 } else {
618 merr = misagent_copy_all(mis, &profiles);
619 }
620 if (merr == MISAGENT_E_SUCCESS) {
621 uint32_t j;
622 uint32_t num_removed = 0;
623 for (j = 0; j < plist_array_get_size(profiles); j++) {
624 char* p_name = NULL;
625 char* p_uuid = NULL;
626 plist_t profile = plist_array_get_item(profiles, j);
627 plist_t pl = profile_get_embedded_plist(profile);
628 if (pl && (plist_get_node_type(pl) == PLIST_DICT)) {
629 plist_t node;
630 node = plist_dict_get_item(pl, "Name");
631 if (node && (plist_get_node_type(node) == PLIST_STRING)) {
632 plist_get_string_val(node, &p_name);
633 }
634 node = plist_dict_get_item(pl, "UUID");
635 if (node && (plist_get_node_type(node) == PLIST_STRING)) {
636 plist_get_string_val(node, &p_uuid);
637 }
638 }
639 if (p_uuid) {
640 if (misagent_remove(mis, p_uuid) == MISAGENT_E_SUCCESS) {
641 printf("OK profile removed: %s - %s\n", p_uuid, (p_name) ? p_name : "(no name)");
642 num_removed++;
643 } else {
644 int sc = misagent_get_status_code(mis);
645 printf("FAIL profile not removed: %s - %s (status code 0x%x)\n", p_uuid, (p_name) ? p_name : "(no name)", sc);
646 }
647 }
648 free(p_name);
649 free(p_uuid);
650 }
651 printf("%d profiles removed.\n", num_removed);
652 } else {
653 int sc = misagent_get_status_code(mis);
654 fprintf(stderr, "Could not get installed profiles from device, status code: 0x%x\n", sc);
655 res = -1;
656 }
657 plist_free(profiles);
658 }
659 break;
660 default:
661 break;
662 }
663
664 misagent_client_free(mis);
665
666 idevice_free(device);
667
668 return res;
669 }
670