-
Notifications
You must be signed in to change notification settings - Fork 15
/
afcclient.c
622 lines (497 loc) · 17.2 KB
/
afcclient.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
/*
* afcclient
* Date: Nov 2013
* Eric Monti - esmonti at gmail dot com
*
* A simple CLI interface to AFC via libimobiledevice.
*
* Build with 'make'
*
* run "afcclient -h" for usage.
*
*/
#ifdef __linux
#include <limits.h>
#endif
#ifdef __APPLE__
#include <sys/syslimits.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include "libidev.h"
#define CHUNKSZ 8192
#pragma mark - AFC Implementation Utility Functions
char *progname;
void usage(FILE *outf);
bool is_dir(char *path)
{
struct stat s;
return (stat(path, &s) == 0 && s.st_mode & S_IFDIR);
}
int dump_afc_device_info(afc_client_t afc)
{
int ret=EXIT_FAILURE;
char **infos=NULL;
afc_error_t err=afc_get_device_info(afc, &infos);
if (err == AFC_E_SUCCESS && infos) {
int i;
printf("AFC Device Info: -");
for (i=0; infos[i]; i++)
printf("%c%s", ((i%2)? ':' : ' '), infos[i]);
printf("\n");
ret = EXIT_SUCCESS;
} else {
fprintf(stderr, "Error: afc get device info failed: %s\n", idev_afc_strerror(err));
}
if (infos)
idevice_device_list_free(infos);
return ret;
}
int dump_afc_file_info(afc_client_t afc, const char *path)
{
int i, ret=EXIT_FAILURE;
char **infolist=NULL;
afc_error_t err = afc_get_file_info(afc, path, &infolist);
if (err == AFC_E_SUCCESS && infolist) {
for(i=0; infolist[i]; i++)
printf("%c%s", ((i%2)? '=' : ' '), infolist[i]);
printf("\t%s\n", path);
ret=EXIT_SUCCESS;
} else {
fprintf(stderr, "Error: info error for path: %s - %s\n", path, idev_afc_strerror(err));
}
if (infolist)
idevice_device_list_free(infolist);
return ret;
}
int dump_afc_list_path(afc_client_t afc, const char *path)
{
int ret=EXIT_FAILURE;
char **list=NULL;
if (idev_verbose)
fprintf(stderr, "[debug] reading afc directory contents at \"%s\"\n", path);
afc_error_t err = afc_read_directory(afc, path, &list);
//if (err == AFC_E_SUCCESS && list) {
if (list) {
int i;
printf("AFC Device Listing path=\"%s\":\n", path);
for (i=0; list[i]; i++) {
char tpath[PATH_MAX], *lpath;
if (!strcmp(path, "")) {
lpath=list[i];
} else {
if (path[strlen(path)-1]=='/') {
snprintf(tpath, PATH_MAX-1, "%s%s", path, list[i]);
} else {
snprintf(tpath, PATH_MAX-1, "%s/%s", path, list[i]);
}
lpath = tpath;
}
dump_afc_file_info(afc, lpath);
}
ret=EXIT_SUCCESS;
} else if (err == AFC_E_READ_ERROR) { // fall-back to doing a file info request, incase its a file
if (idev_verbose)
fprintf(stderr, "[debug] directory read error -- falling back to file info at %s\n", path);
ret = dump_afc_file_info(afc, path);
} else {
fprintf(stderr, "Error: afc list \"%s\" failed: %s\n", path, idev_afc_strerror(err));
}
if (list)
free(list);
return ret;
}
int dump_afc_path(afc_client_t afc, const char *path, FILE *outf)
{
int ret=EXIT_FAILURE;
uint64_t handle=0;
if (idev_verbose)
fprintf(stderr, "[debug] creating afc file connection to %s\n", path);
afc_error_t err = afc_file_open(afc, path, AFC_FOPEN_RDONLY, &handle);
if (err == AFC_E_SUCCESS) {
char buf[CHUNKSZ];
uint32_t bytes_read=0;
while((err=afc_file_read(afc, handle, buf, CHUNKSZ, &bytes_read)) == AFC_E_SUCCESS && bytes_read > 0) {
fwrite(buf, 1, bytes_read, outf);
}
if (err)
fprintf(stderr, "Error: Encountered error while reading %s: %s\n", path, idev_afc_strerror(err));
else
ret=EXIT_SUCCESS;
afc_file_close(afc, handle);
} else {
fprintf(stderr, "Error: afc open file %s failed: %s\n", path, idev_afc_strerror(err));
}
return ret;
}
int get_afc_path(afc_client_t afc, const char *src, const char *dst)
{
int ret=EXIT_FAILURE;
if (idev_verbose)
fprintf(stderr, "[debug] Downloading %s to %s - creating afc file connection\n", src, dst);
uint64_t handle=0;
afc_error_t err = afc_file_open(afc, src, AFC_FOPEN_RDONLY, &handle);
if (err == AFC_E_SUCCESS) {
char buf[CHUNKSZ];
uint32_t bytes_read=0;
size_t totbytes=0;
FILE *outf = fopen(dst, "w");
if (outf) {
while((err=afc_file_read(afc, handle, buf, CHUNKSZ, &bytes_read)) == AFC_E_SUCCESS && bytes_read > 0) {
totbytes += fwrite(buf, 1, bytes_read, outf);
}
fclose(outf);
if (err) {
fprintf(stderr, "Error: Encountered error while reading %s: %s\n", src, idev_afc_strerror(err));
fprintf(stderr, "Warning! - %lu bytes read - incomplete data in %s may have resulted.\n", totbytes, dst);
} else {
printf("Saved %lu bytes to %s\n", totbytes, dst);
ret=EXIT_SUCCESS;
}
} else {
fprintf(stderr, "Error opening local file for writing: %s - %s\n", dst, strerror(errno));
}
afc_file_close(afc, handle);
} else {
fprintf(stderr, "Error: afc open file %s failed: %s\n", src, idev_afc_strerror(err));
}
return ret;
}
int put_afc_path(afc_client_t afc, const char *src, const char *dst)
{
int ret=EXIT_FAILURE;
uint64_t handle=0;
FILE *inf = fopen(src, "r");
if (inf) {
if (idev_verbose)
fprintf(stderr, "[debug] Uploading %s to %s - creating afc file connection\n", src, dst);
afc_error_t err = afc_file_open(afc, dst, AFC_FOPEN_WRONLY, &handle);
if (err == AFC_E_SUCCESS) {
char buf[CHUNKSZ];
uint32_t bytes_read=0;
size_t totbytes=0;
while(err==AFC_E_SUCCESS && (bytes_read=fread(buf, 1, CHUNKSZ, inf)) > 0) {
uint32_t bytes_written=0;
err=afc_file_write(afc, handle, buf, bytes_read, &bytes_written);
totbytes += bytes_written;
}
if (err) {
fprintf(stderr, "Error: Encountered error while writing %s: %s\n", src, idev_afc_strerror(err));
fprintf(stderr, "Warning! - %lu bytes read - incomplete data in %s may have resulted.\n", totbytes, dst);
} else {
printf("Uploaded %lu bytes to %s\n", totbytes, dst);
ret=EXIT_SUCCESS;
}
afc_file_close(afc, handle);
} else {
fprintf(stderr, "Error: afc open file %s failed: %s\n", src, idev_afc_strerror(err));
}
fclose(inf);
} else {
fprintf(stderr, "Error opening local file for reading: %s - %s\n", dst, strerror(errno));
}
return ret;
}
#pragma mark - Command handlers
int do_info(afc_client_t afc, int argc, char **argv)
{
int j, i, ret = EXIT_SUCCESS;
if (argc > 1) {
for (i=1; i<argc ; i++) {
ret |= dump_afc_file_info(afc, argv[i]);
}
} else {
fprintf(stderr, "Error: you must specify at least one path.\n");
ret = EXIT_FAILURE;
}
return ret;
}
int do_list(afc_client_t afc, int argc, char **argv)
{
int i, ret = EXIT_SUCCESS;
if (argc > 1) {
for (i=1; i<argc ; i++) {
ret |= dump_afc_list_path(afc, argv[i]);
}
} else {
ret = dump_afc_list_path(afc, "");
}
return ret;
}
int do_mkdir(afc_client_t afc, int argc, char **argv)
{
int i, ret=EXIT_SUCCESS;
if (argc > 1) {
for (i=1; i<argc ; i++) {
afc_error_t err = afc_make_directory(afc, argv[i]);
if (err == AFC_E_SUCCESS) {
printf("Created directory: %s\n", argv[i]);
} else {
fprintf(stderr, "Error: mkdir error: %s\n", idev_afc_strerror(err));
ret = EXIT_FAILURE;
}
}
} else {
fprintf(stderr, "Error: you must specify at least one directory path.\n");
ret = EXIT_FAILURE;
}
return ret;
}
int do_rm(afc_client_t afc, int argc, char **argv)
{
int i, ret=EXIT_SUCCESS;
if (argc > 1) {
for (i=1; i<argc ; i++) {
afc_error_t err = afc_remove_path(afc, argv[i]);
if (err == AFC_E_SUCCESS) {
printf("Removed: %s\n", argv[i]);
} else {
fprintf(stderr, "Error: mkdir error: %s\n", idev_afc_strerror(err));
ret = EXIT_FAILURE;
}
}
} else {
fprintf(stderr, "Error: you must specify at least one path to remove.\n");
ret = EXIT_FAILURE;
}
return ret;
}
int do_rename(afc_client_t afc, int argc, char **argv)
{
int ret = EXIT_FAILURE;
if (argc == 3) {
afc_error_t err = afc_rename_path(afc, argv[1], argv[2]);
if (err == AFC_E_SUCCESS) {
printf("Renamed %s to %s\n", argv[1], argv[2]);
ret = EXIT_SUCCESS;
} else {
fprintf(stderr, "Error: rename %s to %s - %s\n", argv[1], argv[2], idev_afc_strerror(err));
}
} else {
fprintf(stderr, "Error: invalid number of arguments for rename.\n");
}
return ret;
}
int do_link(afc_client_t afc, int argc, char **argv)
{
int ret=EXIT_FAILURE;
if (argc == 3) {
afc_error_t err = afc_make_link(afc, AFC_HARDLINK, argv[1], argv[2]);
if (err == AFC_E_SUCCESS) {
printf("Created hard-link %s -> %s\n", argv[2], argv[1]);
ret = EXIT_SUCCESS;
} else {
fprintf(stderr, "Error: link %s -> %s - %s\n", argv[2], argv[1], idev_afc_strerror(err));
}
} else {
fprintf(stderr, "Error: invalid number of arguments for link command.\n");
}
return ret;
}
int do_symlink(afc_client_t afc, int argc, char **argv)
{
int ret=EXIT_FAILURE;
if (argc == 3) {
afc_error_t err = afc_make_link(afc, AFC_SYMLINK, argv[1], argv[2]);
if (err == AFC_E_SUCCESS) {
printf("Created symbolic-link %s -> %s\n", argv[2], argv[1]);
ret = EXIT_SUCCESS;
} else {
fprintf(stderr, "Error: link %s -> %s - %s\n", argv[2], argv[1], idev_afc_strerror(err));
}
} else {
fprintf(stderr, "Error: invalid number of arguments for link command.\n");
}
return ret;
}
int do_cat(afc_client_t afc, int argc, char **argv)
{
int ret=EXIT_FAILURE;
if (argc == 2) {
ret = dump_afc_path(afc, argv[1], stdout);
} else {
fprintf(stderr, "Error: invalid number of arguments for cat command.\n");
}
return ret;
}
int do_get(afc_client_t afc, int argc, char **argv)
{
int ret=EXIT_FAILURE;
if (argc == 2) {
ret = get_afc_path(afc, argv[1], basename(argv[1]));
} else if (argc == 3) {
char *dst = argv[2];
char dpath[PATH_MAX];
if (is_dir(dst)) {
snprintf(dpath, PATH_MAX-1, "%s/%s", dst, basename(argv[1]));
dst = dpath;
}
ret = get_afc_path(afc, argv[1], dpath);
} else {
fprintf(stderr, "Error: invalid number of arguments for get command.\n");
}
return ret;
}
int do_put(afc_client_t afc, int argc, char **argv)
{
int ret=EXIT_FAILURE;
if (argc == 2) {
ret = put_afc_path(afc, argv[1], basename(argv[1]));
} else if (argc == 3) {
ret = put_afc_path(afc, argv[1], argv[2]);
} else {
fprintf(stderr, "Error: invalid number of arguments for put command.\n");
}
return ret;
}
int cmd_main(afc_client_t afc, int argc, char **argv)
{
int ret=0;
char *cmd = argv[0];
if (!strcmp(cmd, "devinfo") || !strcmp(cmd, "deviceinfo")) {
if (argc == 1) {
ret = dump_afc_device_info(afc);
} else {
fprintf(stderr, "Error: unexpected extra arguments for devinfo\n");
usage(stderr);
ret=EXIT_FAILURE;
}
}
else if (!strcmp(cmd, "info")) {
ret = do_info(afc, argc, argv);
}
else if (!strcmp(cmd, "ls") || !strcmp(cmd, "list")) {
ret = do_list(afc, argc, argv);
}
else if (!strcmp(cmd, "mkdir")) {
ret = do_mkdir(afc, argc, argv);
}
else if (!strcmp(cmd, "rm") || !strcmp(cmd, "remove")) {
ret = do_rm(afc, argc, argv);
}
else if (!strcmp(cmd, "rename")) {
ret = do_rename(afc, argc, argv);
}
else if (!strcmp(cmd, "link") || !strcmp(cmd, "hardlink")) {
ret = do_link(afc, argc, argv);
}
else if (!strcmp(cmd, "symlink")) {
ret = do_symlink(afc, argc, argv);
}
else if (!strcmp(cmd, "cat")) {
ret = do_cat(afc, argc, argv);
}
else if (!strcmp(cmd, "get")) {
ret = do_get(afc, argc, argv);
}
else if (!strcmp(cmd, "put")) {
ret = do_put(afc, argc, argv);
}
else {
fprintf(stderr, "Error: unknown command: %s\n", cmd);
usage(stderr);
ret = EXIT_FAILURE;
}
return ret;
}
#define OPTION_FLAGS "rs:c:d:u:vh"
void usage(FILE *outf)
{
fprintf(outf,
"Usage: %s [%s] command cmdargs...\n\n"
" Options:\n"
" -r, --root Use the afc2 server if jailbroken (ignored with -c/-d)\n"
" -s, --service=NAME> Use the specified lockdown service (ignored with -c/-d)\n"
" -c, --container=<APP-ID> Access dir for app-id (may not work on newer iOS vers)\n"
" -d, --documents=<APP-ID> Access doc dir for app-id (prefix paths with Documents/)\n"
" -u, --uuid=<UDID> Specify the device udid\n"
" -v, --verbose Enable verbose debug messages\n"
" -h, --help Display this help message\n\n"
" Where \"command\" and \"cmdargs...\" are as folows:\n"
" devinfo dump device info from AFC server\n"
" list <dir> [dir2...] list remote directory contents\n"
" info <path> [path2...] dump remote file information\n"
" mkdir <path> [path2...] create directory at path\n"
" rm <path> [path2...] remove directory at path\n"
" rename <from> <to> rename path 'from' to path 'to'\n"
" link <target> <link> create a hard-link from 'link' to 'target'\n"
" symlink <target> <link> create a symbolic-link from 'link' to 'target'\n"
" cat <path> cat contents of <path> to stdout\n"
" get <path> [localpath] download a file (default: current dir)\n"
" put <localpath> [path] upload a file (default: remote top-level dir)\n"
, progname, OPTION_FLAGS);
}
static struct option longopts[] = {
{ "root", no_argument, NULL, 'r' },
{ "service", required_argument, NULL, 's' },
{ "container", required_argument, NULL, 'c' },
{ "documents", required_argument, NULL, 'd' },
{ "appid", required_argument, NULL, 'a' },
{ "udid", required_argument, NULL, 'u' },
{ "verbose", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
int main(int argc, char **argv)
{
progname = basename(argv[0]);
char *appid=NULL, *udid=NULL, *svcname=NULL, *appdir=NULL;
svcname = AFC_SERVICE_NAME;
int flag;
while ((flag = getopt_long(argc, argv, OPTION_FLAGS, longopts, NULL)) != -1) {
switch(flag) {
case 'r':
svcname = AFC2_SERVICE_NAME;
break;
case 's':
svcname = optarg;
break;
case 'c':
appid = optarg;
appdir = APPDIR_CONTAINER;
break;
case 'd':
appid = optarg;
appdir = APPDIR_DOCUMENTS;
break;
case 'u':
if ((strlen(optarg) != 40)) {
fprintf(stderr, "Error: invalid udid (wrong length): %s\n", optarg);
return EXIT_FAILURE;
}
udid = optarg;
break;
case 'v':
idevice_set_debug_level(1);
idev_verbose=true;
break;
case 'h':
usage(stdout);
return EXIT_SUCCESS;
default:
usage(stderr);
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (argc < 1) {
fprintf(stderr, "Missing command argument\n");
usage(stderr);
return EXIT_FAILURE;
}
if (appid) {
return idev_afc_app_client(progname, udid, appid, appdir, ^int(afc_client_t afc) {
return cmd_main(afc, argc, argv);
});
} else {
return idev_afc_client_ex(progname, udid, svcname, ^int(idevice_t idev, lockdownd_client_t client, lockdownd_service_descriptor_t ldsvc, afc_client_t afc) {
return cmd_main(afc, argc, argv);
});
}
}