-
Notifications
You must be signed in to change notification settings - Fork 257
/
ideviceinstaller.c
1593 lines (1406 loc) · 41.6 KB
/
ideviceinstaller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ideviceinstaller - Manage apps on iOS devices.
*
* Copyright (C) 2010-2023 Nikias Bassen <nikias@gmx.li>
* Copyright (C) 2010-2015 Martin Szulecki <m.szulecki@libimobiledevice.org>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more profile.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#define _GNU_SOURCE 1
#define __USE_GNU 1
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include <libgen.h>
#include <inttypes.h>
#include <limits.h>
#include <sys/stat.h>
#include <dirent.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef WIN32
#include <signal.h>
#endif
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#include <libimobiledevice/installation_proxy.h>
#include <libimobiledevice/notification_proxy.h>
#include <libimobiledevice/afc.h>
#include <plist/plist.h>
#include <zip.h>
#ifdef WIN32
#include <windows.h>
#define wait_ms(x) Sleep(x)
#else
#define wait_ms(x) { struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = x * 1000000; nanosleep(&ts, NULL); }
#endif
#ifndef HAVE_VASPRINTF
static int vasprintf(char **PTR, const char *TEMPLATE, va_list AP)
{
int res;
char buf[16];
res = vsnprintf(buf, 16, TEMPLATE, AP);
if (res > 0) {
*PTR = (char*)malloc(res+1);
res = vsnprintf(*PTR, res+1, TEMPLATE, AP);
}
return res;
}
#endif
#ifndef HAVE_ASPRINTF
static int asprintf(char **PTR, const char *TEMPLATE, ...)
{
int res;
va_list AP;
va_start(AP, TEMPLATE);
res = vasprintf(PTR, TEMPLATE, AP);
va_end(AP);
return res;
}
#endif
#define ITUNES_METADATA_PLIST_FILENAME "iTunesMetadata.plist"
const char PKG_PATH[] = "PublicStaging";
const char APPARCH_PATH[] = "ApplicationArchives";
char *udid = NULL;
char *cmdarg = NULL;
enum cmd_mode {
CMD_NONE = 0,
CMD_LIST_APPS,
CMD_INSTALL,
CMD_UNINSTALL,
CMD_UPGRADE,
CMD_LIST_ARCHIVES,
CMD_ARCHIVE,
CMD_RESTORE,
CMD_REMOVE_ARCHIVE
};
int cmd = CMD_NONE;
char *last_status = NULL;
int wait_for_command_complete = 0;
int use_network = 0;
int use_notifier = 0;
int notification_expected = 0;
int is_device_connected = 0;
int command_completed = 0;
int ignore_events = 0;
int err_occurred = 0;
int notified = 0;
plist_t bundle_ids = NULL;
plist_t return_attrs = NULL;
#define FORMAT_XML 1
#define FORMAT_JSON 2
int output_format = 0;
int opt_list_user = 0;
int opt_list_system = 0;
char *copy_path = NULL;
int remove_after_copy = 0;
int skip_uninstall = 1;
int app_only = 0;
int docs_only = 0;
static void print_apps_header()
{
if (!return_attrs) {
return;
}
uint32_t i = 0;
for (i = 0; i < plist_array_get_size(return_attrs); i++) {
plist_t node = plist_array_get_item(return_attrs, i);
if (i > 0) {
printf(", ");
}
printf("%s", plist_get_string_ptr(node, NULL));
}
printf("\n");
}
static void print_apps(plist_t apps)
{
if (!return_attrs) {
return;
}
uint32_t i = 0;
for (i = 0; i < plist_array_get_size(apps); i++) {
plist_t app = plist_array_get_item(apps, i);
uint32_t j = 0;
for (j = 0; j < plist_array_get_size(return_attrs); j++) {
plist_t node = plist_array_get_item(return_attrs, j);
if (j > 0) {
printf(", ");
}
const char* key = plist_get_string_ptr(node, NULL);
node = plist_dict_get_item(app, key);
if (node) {
if (!strcmp(key, "CFBundleIdentifier")) {
printf("%s", plist_get_string_ptr(node, NULL));
} else {
uint64_t uval = 0;
switch (plist_get_node_type(node)) {
case PLIST_STRING:
printf("\"%s\"", plist_get_string_ptr(node, NULL));
break;
case PLIST_INT:
plist_get_uint_val(node, &uval);
printf("%" PRIu64, uval);
break;
case PLIST_BOOLEAN:
printf("%s", plist_bool_val_is_true(node) ? "true" : "false");
break;
case PLIST_ARRAY:
printf("(array)");
break;
case PLIST_DICT:
printf("(dict)");
break;
default:
break;
}
}
}
}
printf("\n");
}
}
static void notifier(const char *notification, void *unused)
{
notified = 1;
}
static void status_cb(plist_t command, plist_t status, void *unused)
{
if (command && status) {
char* command_name = NULL;
instproxy_command_get_name(command, &command_name);
/* get status */
char *status_name = NULL;
instproxy_status_get_name(status, &status_name);
if (status_name) {
if (!strcmp(status_name, "Complete")) {
command_completed = 1;
}
}
/* get error if any */
char* error_name = NULL;
char* error_description = NULL;
uint64_t error_code = 0;
instproxy_status_get_error(status, &error_name, &error_description, &error_code);
/* output/handling */
if (!error_name) {
if (!strcmp(command_name, "Browse")) {
uint64_t total = 0;
uint64_t current_index = 0;
uint64_t current_amount = 0;
plist_t current_list = NULL;
instproxy_status_get_current_list(status, &total, ¤t_index, ¤t_amount, ¤t_list);
if (current_list) {
print_apps(current_list);
plist_free(current_list);
}
} else if (status_name) {
/* get progress if any */
int percent = -1;
instproxy_status_get_percent_complete(status, &percent);
if (last_status && (strcmp(last_status, status_name))) {
printf("\n");
}
if (percent >= 0) {
printf("\r%s: %s (%d%%)", command_name, status_name, percent);
} else {
printf("\r%s: %s", command_name, status_name);
}
if (command_completed) {
printf("\n");
}
}
} else {
/* report error to the user */
if (error_description)
fprintf(stderr, "ERROR: %s failed. Got error \"%s\" with code 0x%08"PRIx64": %s\n", command_name, error_name, error_code, error_description ? error_description: "N/A");
else
fprintf(stderr, "ERROR: %s failed. Got error \"%s\".\n", command_name, error_name);
err_occurred = 1;
}
/* clean up */
free(error_name);
free(error_description);
free(last_status);
last_status = status_name;
free(command_name);
command_name = NULL;
} else {
fprintf(stderr, "ERROR: %s was called with invalid arguments!\n", __func__);
}
}
static int zip_get_contents(struct zip *zf, const char *filename, int locate_flags, char **buffer, uint32_t *len)
{
struct zip_stat zs;
struct zip_file *zfile;
int zindex = zip_name_locate(zf, filename, locate_flags);
*buffer = NULL;
*len = 0;
if (zindex < 0) {
return -1;
}
zip_stat_init(&zs);
if (zip_stat_index(zf, zindex, 0, &zs) != 0) {
fprintf(stderr, "ERROR: zip_stat_index '%s' failed!\n", filename);
return -2;
}
if (zs.size > 10485760) {
fprintf(stderr, "ERROR: file '%s' is too large!\n", filename);
return -3;
}
zfile = zip_fopen_index(zf, zindex, 0);
if (!zfile) {
fprintf(stderr, "ERROR: zip_fopen '%s' failed!\n", filename);
return -4;
}
*buffer = malloc(zs.size);
if (zs.size > LLONG_MAX || zip_fread(zfile, *buffer, zs.size) != (zip_int64_t)zs.size) {
fprintf(stderr, "ERROR: zip_fread %" PRIu64 " bytes from '%s'\n", (uint64_t)zs.size, filename);
free(*buffer);
*buffer = NULL;
zip_fclose(zfile);
return -5;
}
*len = zs.size;
zip_fclose(zfile);
return 0;
}
static int zip_get_app_directory(struct zip* zf, char** path)
{
zip_int64_t i = 0;
zip_int64_t c = (zip_int64_t)zip_get_num_entries(zf, 0);
int len = 0;
const char* name = NULL;
/* look through all filenames in the archive */
do {
/* get filename at current index */
name = zip_get_name(zf, i++, 0);
if (name != NULL) {
/* check if we have a "Payload/.../" name */
len = strlen(name);
if (!strncmp(name, "Payload/", 8) && (len > 8)) {
/* skip hidden files */
if (name[8] == '.')
continue;
/* locate the second directory delimiter */
const char* p = name + 8;
do {
if (*p == '/') {
break;
}
} while(p++ != NULL);
/* try next entry if not found */
if (p == NULL)
continue;
len = p - name + 1;
if (path != NULL) {
free(*path);
*path = NULL;
}
/* allocate and copy filename */
*path = (char*)malloc(len + 1);
strncpy(*path, name, len);
/* add terminating null character */
char* t = *path + len;
*t = '\0';
break;
}
}
} while(i < c);
if (*path == NULL) {
return -1;
}
return 0;
}
static void idevice_event_callback(const idevice_event_t* event, void* userdata)
{
if (ignore_events) {
return;
}
if (event->event == IDEVICE_DEVICE_REMOVE) {
if (!strcmp(udid, event->udid)) {
fprintf(stderr, "ideviceinstaller: Device removed\n");
is_device_connected = 0;
}
}
}
static void idevice_wait_for_command_to_complete()
{
is_device_connected = 1;
ignore_events = 0;
/* subscribe to make sure to exit on device removal */
idevice_event_subscribe(idevice_event_callback, NULL);
/* wait for command to complete */
while (wait_for_command_complete && !command_completed && !err_occurred
&& is_device_connected) {
wait_ms(50);
}
/* wait some time if a notification is expected */
while (use_notifier && notification_expected && !notified && !err_occurred && is_device_connected) {
wait_ms(50);
}
ignore_events = 1;
idevice_event_unsubscribe();
}
static void print_usage(int argc, char **argv, int is_error)
{
char *name = strrchr(argv[0], '/');
fprintf((is_error) ? stderr : stdout, "Usage: %s OPTIONS\n", (name ? name + 1 : argv[0]));
fprintf((is_error) ? stderr : stdout,
"\n"
"Manage apps on iOS devices.\n"
"\n"
"COMMANDS:\n"
" list List installed apps. Options:\n"
" --user List user apps only (this is the default)\n"
" --system List system apps only\n"
" --all List all types of apps\n"
" --xml Print output as XML Property List\n"
" -a, --attribute ATTR Specify attribute to return - see man page\n"
" (can be passed multiple times)\n"
" -b, --bundle-identifier BUNDLEID Only query given bundle identifier\n"
" (can be passed multiple times)\n"
" install PATH Install app from package file specified by PATH.\n"
" PATH can also be a .ipcc file for carrier bundles.\n"
" uninstall BUNDLEID Uninstall app specified by BUNDLEID.\n"
" upgrade PATH Upgrade app from package file specified by PATH.\n"
"\n"
"LEGACY COMMANDS (non-functional with iOS 7 or later):\n"
" archive BUNDLEID Archive app specified by BUNDLEID. Options:\n"
" --uninstall Uninstall the package after making an archive\n"
" --app-only Archive application data only\n"
" --docs-only Archive documents (user data) only\n"
" --copy=PATH Copy the app archive to directory PATH when done\n"
" --remove Only valid when copy=PATH is used: remove after copy\n"
" restore BUNDLEID Restore archived app specified by BUNDLEID\n"
" list-archives List archived apps. Options:\n"
" --xml Print output as XML Property List\n"
" remove-archive BUNDLEID Remove app archive specified by BUNDLEID\n"
"\n"
"OPTIONS:\n"
" -u, --udid UDID Target specific device by UDID\n"
" -n, --network Connect to network device\n"
" -w, --notify-wait Wait for app installed/uninstalled notification\n"
" before reporting success of operation\n"
" -h, --help Print usage information\n"
" -d, --debug Enable communication debugging\n"
" -v, --version Print version information\n"
"\n"
"Homepage: <" PACKAGE_URL ">\n"
"Bug Reports: <" PACKAGE_BUGREPORT ">\n"
);
}
enum numerical_opts {
LIST_USER = 1,
LIST_SYSTEM,
LIST_ALL,
ARCHIVE_UNINSTALL,
ARCHIVE_APP_ONLY,
ARCHIVE_DOCS_ONLY,
ARCHIVE_COPY_PATH,
ARCHIVE_COPY_REMOVE,
OUTPUT_XML,
OUTPUT_JSON
};
static void parse_opts(int argc, char **argv)
{
static struct option longopts[] = {
{ "help", no_argument, NULL, 'h' },
{ "udid", required_argument, NULL, 'u' },
{ "network", no_argument, NULL, 'n' },
{ "notify-wait", no_argument, NULL, 'w' },
{ "debug", no_argument, NULL, 'd' },
{ "version", no_argument, NULL, 'v' },
{ "bundle-identifier", required_argument, NULL, 'b' },
{ "attribute", required_argument, NULL, 'a' },
{ "user", no_argument, NULL, LIST_USER },
{ "system", no_argument, NULL, LIST_SYSTEM },
{ "all", no_argument, NULL, LIST_ALL },
{ "xml", no_argument, NULL, OUTPUT_XML },
{ "json", no_argument, NULL, OUTPUT_JSON },
{ "uninstall", no_argument, NULL, ARCHIVE_UNINSTALL },
{ "app-only", no_argument, NULL, ARCHIVE_APP_ONLY },
{ "docs-only", no_argument, NULL, ARCHIVE_DOCS_ONLY },
{ "copy", required_argument, NULL, ARCHIVE_COPY_PATH },
{ "remove", no_argument, NULL, ARCHIVE_COPY_REMOVE },
{ NULL, 0, NULL, 0 }
};
int c;
while (1) {
c = getopt_long(argc, argv, "hu:nwdvb:a:", longopts, (int*)0);
if (c == -1) {
break;
}
switch (c) {
case 'h':
print_usage(argc, argv, 0);
exit(0);
case 'u':
if (!*optarg) {
printf("ERROR: UDID must not be empty!\n");
print_usage(argc, argv, 1);
exit(2);
}
udid = strdup(optarg);
break;
case 'n':
use_network = 1;
break;
case 'a':
if (!*optarg) {
printf("ERROR: attribute must not be empty!\n");
print_usage(argc, argv, 1);
exit(2);
}
if (return_attrs == NULL) {
return_attrs = plist_new_array();
}
plist_array_append_item(return_attrs, plist_new_string(optarg));
break;
case 'b':
if (!*optarg) {
printf("ERROR: bundle identifier must not be empty!\n");
print_usage(argc, argv, 1);
exit(2);
}
if (bundle_ids == NULL) {
bundle_ids = plist_new_array();
}
plist_array_append_item(bundle_ids, plist_new_string(optarg));
break;
case 'w':
use_notifier = 1;
break;
case 'd':
idevice_set_debug_level(1);
break;
case 'v':
printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
exit(0);
case LIST_USER:
opt_list_user = 1;
break;
case LIST_SYSTEM:
opt_list_system = 1;
break;
case LIST_ALL:
opt_list_user = 1;
opt_list_system = 1;
break;
case OUTPUT_XML:
output_format = FORMAT_XML;
break;
case OUTPUT_JSON:
output_format = FORMAT_JSON;
break;
case ARCHIVE_UNINSTALL:
skip_uninstall = 0;
break;
case ARCHIVE_APP_ONLY:
app_only = 1;
docs_only = 0;
break;
case ARCHIVE_DOCS_ONLY:
docs_only = 1;
app_only = 0;
break;
case ARCHIVE_COPY_PATH:
copy_path = strdup(optarg);
break;
case ARCHIVE_COPY_REMOVE:
remove_after_copy = 1;
break;
default:
print_usage(argc, argv, 1);
exit(2);
}
}
argv += optind;
argc -= optind;
if (argc == 0) {
fprintf(stderr, "ERROR: Missing command.\n\n");
print_usage(argc+optind, argv-optind, 1);
exit(2);
}
char *cmdstr = argv[0];
if (!strcmp(cmdstr, "list")) {
cmd = CMD_LIST_APPS;
} else if (!strcmp(cmdstr, "install")) {
cmd = CMD_INSTALL;
} else if (!strcmp(cmdstr, "upgrade")) {
cmd = CMD_UPGRADE;
} else if (!strcmp(cmdstr, "uninstall") || !strcmp(cmdstr, "remove")) {
cmd = CMD_UNINSTALL;
} else if (!strcmp(cmdstr, "archives") || !strcmp(cmdstr, "list-archives")) {
cmd = CMD_LIST_ARCHIVES;
} else if (!strcmp(cmdstr, "archive")) {
cmd = CMD_ARCHIVE;
} else if (!strcmp(cmdstr, "restore")) {
cmd = CMD_RESTORE;
} else if (!strcmp(cmdstr, "remove-archive")) {
cmd = CMD_REMOVE_ARCHIVE;
}
switch (cmd) {
case CMD_LIST_APPS:
case CMD_LIST_ARCHIVES:
break;
case CMD_INSTALL:
case CMD_UPGRADE:
if (argc < 2) {
fprintf(stderr, "ERROR: Missing filename for '%s' command.\n\n", cmdstr);
print_usage(argc+optind, argv-optind, 1);
exit(2);
}
cmdarg = argv[1];
break;
case CMD_UNINSTALL:
case CMD_ARCHIVE:
case CMD_RESTORE:
case CMD_REMOVE_ARCHIVE:
if (argc < 2) {
fprintf(stderr, "ERROR: Missing bundle ID for '%s' command.\n\n", cmdstr);
print_usage(argc+optind, argv-optind, 1);
exit(2);
}
cmdarg = argv[1];
break;
default:
fprintf(stderr, "ERROR: Invalid command '%s'.\n\n", cmdstr);
print_usage(argc+optind, argv-optind, 1);
exit(2);
}
}
static int afc_upload_file(afc_client_t afc, const char* filename, const char* dstfn)
{
FILE *f = NULL;
uint64_t af = 0;
char buf[1048576];
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "fopen: %s: %s\n", filename, strerror(errno));
return -1;
}
if ((afc_file_open(afc, dstfn, AFC_FOPEN_WRONLY, &af) != AFC_E_SUCCESS) || !af) {
fclose(f);
fprintf(stderr, "afc_file_open on '%s' failed!\n", dstfn);
return -1;
}
size_t amount = 0;
do {
amount = fread(buf, 1, sizeof(buf), f);
if (amount > 0) {
uint32_t written, total = 0;
while (total < amount) {
written = 0;
afc_error_t aerr = afc_file_write(afc, af, buf, amount, &written);
if (aerr != AFC_E_SUCCESS) {
fprintf(stderr, "AFC Write error: %d\n", aerr);
break;
}
total += written;
}
if (total != amount) {
fprintf(stderr, "Error: wrote only %u of %u\n", total, (uint32_t)amount);
afc_file_close(afc, af);
fclose(f);
return -1;
}
}
} while (amount > 0);
afc_file_close(afc, af);
fclose(f);
return 0;
}
static void afc_upload_dir(afc_client_t afc, const char* path, const char* afcpath)
{
afc_make_directory(afc, afcpath);
DIR *dir = opendir(path);
if (dir) {
struct dirent* ep;
while ((ep = readdir(dir))) {
if ((strcmp(ep->d_name, ".") == 0) || (strcmp(ep->d_name, "..") == 0)) {
continue;
}
char *fpath = (char*)malloc(strlen(path)+1+strlen(ep->d_name)+1);
char *apath = (char*)malloc(strlen(afcpath)+1+strlen(ep->d_name)+1);
struct stat st;
strcpy(fpath, path);
strcat(fpath, "/");
strcat(fpath, ep->d_name);
strcpy(apath, afcpath);
strcat(apath, "/");
strcat(apath, ep->d_name);
#ifdef HAVE_LSTAT
if ((lstat(fpath, &st) == 0) && S_ISLNK(st.st_mode)) {
char *target = (char *)malloc(st.st_size+1);
if (readlink(fpath, target, st.st_size+1) < 0) {
fprintf(stderr, "ERROR: readlink: %s (%d)\n", strerror(errno), errno);
} else {
target[st.st_size] = '\0';
afc_make_link(afc, AFC_SYMLINK, target, fpath);
}
free(target);
} else
#endif
if ((stat(fpath, &st) == 0) && S_ISDIR(st.st_mode)) {
afc_upload_dir(afc, fpath, apath);
} else {
afc_upload_file(afc, fpath, apath);
}
free(fpath);
free(apath);
}
closedir(dir);
}
}
int main(int argc, char **argv)
{
idevice_t device = NULL;
lockdownd_client_t client = NULL;
instproxy_client_t ipc = NULL;
instproxy_error_t err;
np_client_t np = NULL;
afc_client_t afc = NULL;
lockdownd_service_descriptor_t service = NULL;
int res = EXIT_FAILURE;
char *bundleidentifier = NULL;
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
parse_opts(argc, argv);
argc -= optind;
argv += optind;
if (IDEVICE_E_SUCCESS != idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX)) {
if (udid) {
fprintf(stderr, "No device found with udid %s.\n", udid);
} else {
fprintf(stderr, "No device found.\n");
}
return EXIT_FAILURE;
}
if (!udid) {
idevice_get_udid(device, &udid);
}
lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &client, "ideviceinstaller");
if (lerr != LOCKDOWN_E_SUCCESS) {
fprintf(stderr, "Could not connect to lockdownd: %s. Exiting.\n", lockdownd_strerror(lerr));
goto leave_cleanup;
}
if (use_notifier) {
lerr =lockdownd_start_service(client, "com.apple.mobile.notification_proxy", &service);
if (lerr != LOCKDOWN_E_SUCCESS) {
fprintf(stderr, "Could not start com.apple.mobile.notification_proxy: %s\n", lockdownd_strerror(lerr));
goto leave_cleanup;
}
np_error_t nperr = np_client_new(device, service, &np);
if (service) {
lockdownd_service_descriptor_free(service);
}
service = NULL;
if (nperr != NP_E_SUCCESS) {
fprintf(stderr, "Could not connect to notification_proxy!\n");
goto leave_cleanup;
}
np_set_notify_callback(np, notifier, NULL);
const char *noties[3] = { NP_APP_INSTALLED, NP_APP_UNINSTALLED, NULL };
np_observe_notifications(np, noties);
}
run_again:
if (service) {
lockdownd_service_descriptor_free(service);
}
service = NULL;
lerr = lockdownd_start_service(client, "com.apple.mobile.installation_proxy", &service);
if (lerr != LOCKDOWN_E_SUCCESS) {
fprintf(stderr, "Could not start com.apple.mobile.installation_proxy: %s\n", lockdownd_strerror(lerr));
goto leave_cleanup;
}
err = instproxy_client_new(device, service, &ipc);
if (service) {
lockdownd_service_descriptor_free(service);
}
service = NULL;
if (err != INSTPROXY_E_SUCCESS) {
fprintf(stderr, "Could not connect to installation_proxy!\n");
goto leave_cleanup;
}
setbuf(stdout, NULL);
free(last_status);
last_status = NULL;
notification_expected = 0;
if (cmd == CMD_LIST_APPS) {
plist_t client_opts = instproxy_client_options_new();
instproxy_client_options_add(client_opts, "ApplicationType", "User", NULL);
plist_t apps = NULL;
if (opt_list_system && opt_list_user) {
plist_dict_remove_item(client_opts, "ApplicationType");
} else if (opt_list_system) {
instproxy_client_options_add(client_opts, "ApplicationType", "System", NULL);
} else if (opt_list_user) {
instproxy_client_options_add(client_opts, "ApplicationType", "User", NULL);
}
if (bundle_ids) {
plist_dict_set_item(client_opts, "BundleIDs", plist_copy(bundle_ids));
}
if (!output_format && !return_attrs) {
return_attrs = plist_new_array();
plist_array_append_item(return_attrs, plist_new_string("CFBundleIdentifier"));
plist_array_append_item(return_attrs, plist_new_string("CFBundleShortVersionString"));
plist_array_append_item(return_attrs, plist_new_string("CFBundleDisplayName"));
}
if (return_attrs) {
instproxy_client_options_add(client_opts, "ReturnAttributes", return_attrs, NULL);
}
if (output_format) {
err = instproxy_browse(ipc, client_opts, &apps);
if (!apps || (plist_get_node_type(apps) != PLIST_ARRAY)) {
fprintf(stderr, "ERROR: instproxy_browse returnd an invalid plist!\n");
goto leave_cleanup;
}
char *buf = NULL;
uint32_t len = 0;
if (output_format == FORMAT_XML) {
plist_err_t perr = plist_to_xml(apps, &buf, &len);
if (perr != PLIST_ERR_SUCCESS) {
fprintf(stderr, "ERROR: Failed to convert data to XML format (%d).\n", perr);
}
} else if (output_format == FORMAT_JSON) {
/* for JSON, we need to convert some stuff since it doesn't support PLIST_DATA nodes */
plist_array_iter aiter = NULL;
plist_array_new_iter(apps, &aiter);
plist_t entry = NULL;
do {
plist_array_next_item(apps, aiter, &entry);
if (!entry) break;
plist_t items = plist_dict_get_item(entry, "UIApplicationShortcutItems");
plist_array_iter inner = NULL;
plist_array_new_iter(items, &inner);
plist_t item = NULL;
do {
plist_array_next_item(items, inner, &item);
if (!item) break;
plist_t userinfo = plist_dict_get_item(item, "UIApplicationShortcutItemUserInfo");
if (userinfo) {
plist_t data_node = plist_dict_get_item(userinfo, "data");
if (data_node) {
char *strbuf = NULL;
uint32_t buflen = 0;
plist_write_to_string(data_node, &strbuf, &buflen, PLIST_FORMAT_LIMD, PLIST_OPT_NO_NEWLINE);
plist_set_string_val(data_node, strbuf);
free(strbuf);
}
}
} while (item);
free(inner);
} while (entry);
free(aiter);
plist_err_t perr = plist_to_json(apps, &buf, &len, 1);
if (perr != PLIST_ERR_SUCCESS) {
fprintf(stderr, "ERROR: Failed to convert data to JSON format (%d).\n", perr);
}
}
if (buf) {
puts(buf);
free(buf);
}
plist_free(apps);
goto leave_cleanup;
}
print_apps_header();
err = instproxy_browse_with_callback(ipc, client_opts, status_cb, NULL);
if (err == INSTPROXY_E_RECEIVE_TIMEOUT) {
fprintf(stderr, "NOTE: timeout waiting for device to browse apps, trying again...\n");
}
instproxy_client_options_free(client_opts);
if (err != INSTPROXY_E_SUCCESS) {
fprintf(stderr, "ERROR: instproxy_browse returned %d\n", err);
goto leave_cleanup;
}
wait_for_command_complete = 1;
notification_expected = 0;
} else if (cmd == CMD_INSTALL || cmd == CMD_UPGRADE) {
plist_t sinf = NULL;
plist_t meta = NULL;
char *pkgname = NULL;
struct stat fst;
uint64_t af = 0;
char buf[8192];
lockdownd_service_descriptor_free(service);
service = NULL;
lerr = lockdownd_start_service(client, "com.apple.afc", &service);
if (lerr != LOCKDOWN_E_SUCCESS) {
fprintf(stderr, "Could not start com.apple.afc: %s\n", lockdownd_strerror(lerr));
goto leave_cleanup;
}
lockdownd_client_free(client);
client = NULL;
if (afc_client_new(device, service, &afc) != AFC_E_SUCCESS) {
fprintf(stderr, "Could not connect to AFC!\n");
goto leave_cleanup;
}
if (stat(cmdarg, &fst) != 0) {
fprintf(stderr, "ERROR: stat: %s: %s\n", cmdarg, strerror(errno));
goto leave_cleanup;
}
char **strs = NULL;
if (afc_get_file_info(afc, PKG_PATH, &strs) != AFC_E_SUCCESS) {
if (afc_make_directory(afc, PKG_PATH) != AFC_E_SUCCESS) {
fprintf(stderr, "WARNING: Could not create directory '%s' on device!\n", PKG_PATH);
}
}
if (strs) {
int i = 0;
while (strs[i]) {
free(strs[i]);
i++;
}
free(strs);
}
plist_t client_opts = instproxy_client_options_new();
/* open install package */
int errp = 0;
struct zip *zf = NULL;
if ((strlen(cmdarg) > 5) && (strcmp(&cmdarg[strlen(cmdarg)-5], ".ipcc") == 0)) {
zf = zip_open(cmdarg, 0, &errp);
if (!zf) {
fprintf(stderr, "ERROR: zip_open: %s: %d\n", cmdarg, errp);