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
|
/*
* ideviceinfo.c
* Simple utility to show information about an attached device
*
* Copyright (c) 2009 Martin Szulecki All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <inttypes.h>
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#define FORMAT_KEY_VALUE 1
#define FORMAT_XML 2
static const char *domains[] = {
"com.apple.disk_usage",
"com.apple.disk_usage.factory",
"com.apple.mobile.battery",
/* FIXME: For some reason lockdownd segfaults on this, works sometimes though
"com.apple.mobile.debug",. */
"com.apple.iqagent",
"com.apple.purplebuddy",
"com.apple.PurpleBuddy",
"com.apple.mobile.chaperone",
"com.apple.mobile.third_party_termination",
"com.apple.mobile.lockdownd",
"com.apple.mobile.lockdown_cache",
"com.apple.xcode.developerdomain",
"com.apple.international",
"com.apple.mobile.data_sync",
"com.apple.mobile.tethered_sync",
"com.apple.mobile.mobile_application_usage",
"com.apple.mobile.backup",
"com.apple.mobile.nikita",
"com.apple.mobile.restriction",
"com.apple.mobile.user_preferences",
"com.apple.mobile.sync_data_class",
"com.apple.mobile.software_behavior",
"com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands",
"com.apple.mobile.iTunes.accessories",
"com.apple.mobile.internal", /**< iOS 4.0+ */
"com.apple.mobile.wireless_lockdown", /**< iOS 4.0+ */
"com.apple.fairplay",
"com.apple.iTunes",
"com.apple.mobile.iTunes.store",
"com.apple.mobile.iTunes",
NULL
};
static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char base64_pad = '=';
static char *base64encode(const unsigned char *buf, size_t size)
{
if (!buf || !(size > 0)) return NULL;
int outlen = (size / 3) * 4;
char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0'
size_t n = 0;
size_t m = 0;
unsigned char input[3];
unsigned int output[4];
while (n < size) {
input[0] = buf[n];
input[1] = (n+1 < size) ? buf[n+1] : 0;
input[2] = (n+2 < size) ? buf[n+2] : 0;
output[0] = input[0] >> 2;
output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
output[3] = input[2] & 63;
outbuf[m++] = base64_str[(int)output[0]];
outbuf[m++] = base64_str[(int)output[1]];
outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad;
outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad;
n+=3;
}
outbuf[m] = 0; // 0-termination!
return outbuf;
}
static int indent_level = 0;
static int is_domain_known(char *domain)
{
int i = 0;
while (domains[i] != NULL) {
if (strstr(domain, domains[i++])) {
return 1;
}
}
return 0;
}
static void plist_node_to_string(plist_t node);
static void plist_array_to_string(plist_t node)
{
/* iterate over items */
int i, count;
plist_t subnode = NULL;
count = plist_array_get_size(node);
for (i = 0; i < count; i++) {
subnode = plist_array_get_item(node, i);
printf("%*s", indent_level, "");
printf("%d: ", i);
plist_node_to_string(subnode);
}
}
static void plist_dict_to_string(plist_t node)
{
/* iterate over key/value pairs */
plist_dict_iter it = NULL;
char* key = NULL;
plist_t subnode = NULL;
plist_dict_new_iter(node, &it);
plist_dict_next_item(node, it, &key, &subnode);
while (subnode)
{
printf("%*s", indent_level, "");
printf("%s", key);
if (plist_get_node_type(subnode) == PLIST_ARRAY)
printf("[%d]: ", plist_array_get_size(subnode));
else
printf(": ");
free(key);
key = NULL;
plist_node_to_string(subnode);
plist_dict_next_item(node, it, &key, &subnode);
}
free(it);
}
static void plist_node_to_string(plist_t node)
{
char *s = NULL;
char *data = NULL;
double d;
uint8_t b;
uint64_t u = 0;
struct timeval tv = { 0, 0 };
plist_type t;
if (!node)
return;
t = plist_get_node_type(node);
switch (t) {
case PLIST_BOOLEAN:
plist_get_bool_val(node, &b);
printf("%s\n", (b ? "true" : "false"));
break;
case PLIST_UINT:
plist_get_uint_val(node, &u);
printf("%"PRIu64"\n", (long long)u);
break;
case PLIST_REAL:
plist_get_real_val(node, &d);
printf("%f\n", d);
break;
case PLIST_STRING:
plist_get_string_val(node, &s);
printf("%s\n", s);
free(s);
break;
case PLIST_KEY:
plist_get_key_val(node, &s);
printf("%s: ", s);
free(s);
break;
case PLIST_DATA:
plist_get_data_val(node, &data, &u);
if (u > 0) {
s = base64encode((unsigned char*)data, u);
free(data);
if (s) {
printf("%s\n", s);
free(s);
} else {
printf("\n");
}
} else {
printf("\n");
}
break;
case PLIST_DATE:
plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec);
{
time_t ti = (time_t)tv.tv_sec;
struct tm *btime = localtime(&ti);
if (btime) {
s = (char*)malloc(24);
memset(s, 0, 24);
if (strftime(s, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) {
free (s);
s = NULL;
}
}
}
if (s) {
printf("%s\n", s);
free(s);
} else {
printf("\n");
}
break;
case PLIST_ARRAY:
printf("\n");
indent_level++;
plist_array_to_string(node);
indent_level--;
break;
case PLIST_DICT:
printf("\n");
indent_level++;
plist_dict_to_string(node);
indent_level--;
break;
default:
break;
}
}
static void print_usage(int argc, char **argv)
{
int i = 0;
char *name = NULL;
name = strrchr(argv[0], '/');
printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
printf("Show information about a connected device.\n\n");
printf(" -d, --debug\t\tenable communication debugging\n");
printf(" -s, --simple\t\tuse a simple connection to avoid auto-pairing with the device\n");
printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
printf(" -q, --domain NAME\tset domain of query to NAME. Default: None\n");
printf(" -k, --key NAME\tonly query key specified by NAME. Default: All keys.\n");
printf(" -x, --xml\t\toutput information as xml plist instead of key/value pairs\n");
printf(" -h, --help\t\tprints usage information\n");
printf("\n");
printf(" Known domains are:\n\n");
while (domains[i] != NULL) {
printf(" %s\n", domains[i++]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
lockdownd_client_t client = NULL;
idevice_t device = NULL;
idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
int i;
int simple = 0;
int format = FORMAT_KEY_VALUE;
const char* udid = NULL;
char *domain = NULL;
char *key = NULL;
char *xml_doc = NULL;
uint32_t xml_length;
plist_t node = NULL;
plist_type node_type;
/* parse cmdline args */
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
idevice_set_debug_level(1);
continue;
}
else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
i++;
if (!argv[i] || (strlen(argv[i]) != 40)) {
print_usage(argc, argv);
return 0;
}
udid = argv[i];
continue;
}
else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--domain")) {
i++;
if (!argv[i] || (strlen(argv[i]) < 4)) {
print_usage(argc, argv);
return 0;
}
if (!is_domain_known(argv[i])) {
fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", argv[i]);
}
domain = strdup(argv[i]);
continue;
}
else if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--key")) {
i++;
if (!argv[i] || (strlen(argv[i]) <= 1)) {
print_usage(argc, argv);
return 0;
}
key = strdup(argv[i]);
continue;
}
else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--xml")) {
format = FORMAT_XML;
continue;
}
else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--simple")) {
simple = 1;
continue;
}
else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
print_usage(argc, argv);
return 0;
}
else {
print_usage(argc, argv);
return 0;
}
}
ret = idevice_new(&device, udid);
if (ret != IDEVICE_E_SUCCESS) {
if (udid) {
printf("No device found with udid %s, is it plugged in?\n", udid);
} else {
printf("No device found, is it plugged in?\n");
}
return -1;
}
if (LOCKDOWN_E_SUCCESS != (simple ?
lockdownd_client_new(device, &client, "ideviceinfo"):
lockdownd_client_new_with_handshake(device, &client, "ideviceinfo"))) {
idevice_free(device);
return -1;
}
/* run query and output information */
if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS) {
if (node) {
switch (format) {
case FORMAT_XML:
plist_to_xml(node, &xml_doc, &xml_length);
printf("%s", xml_doc);
free(xml_doc);
break;
case FORMAT_KEY_VALUE:
node_type = plist_get_node_type(node);
if (node_type == PLIST_DICT) {
plist_dict_to_string(node);
break;
} else if (node_type == PLIST_ARRAY) {
plist_array_to_string(node);
break;
}
default:
if (key != NULL)
plist_node_to_string(node);
break;
}
plist_free(node);
node = NULL;
}
}
if (domain != NULL)
free(domain);
lockdownd_client_free(client);
idevice_free(device);
return 0;
}
|