1 /*
2  * ideviceinfo.c
3  * Simple utility to show information about an attached device
4  *
5  * Copyright (c) 2010-2019 Nikias Bassen, All Rights Reserved.
6  * Copyright (c) 2009 Martin Szulecki 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 "ideviceinfo"
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <getopt.h>
34 #ifndef WIN32
35 #include <signal.h>
36 #endif
37 
38 #include <libimobiledevice/libimobiledevice.h>
39 #include <libimobiledevice/lockdown.h>
40 #include "common/utils.h"
41 
42 #define FORMAT_KEY_VALUE 1
43 #define FORMAT_XML 2
44 
45 static const char *domains[] = {
46 	"com.apple.disk_usage",
47 	"com.apple.disk_usage.factory",
48 	"com.apple.mobile.battery",
49 /* FIXME: For some reason lockdownd segfaults on this, works sometimes though
50 	"com.apple.mobile.debug",. */
51 	"com.apple.iqagent",
52 	"com.apple.purplebuddy",
53 	"com.apple.PurpleBuddy",
54 	"com.apple.mobile.chaperone",
55 	"com.apple.mobile.third_party_termination",
56 	"com.apple.mobile.lockdownd",
57 	"com.apple.mobile.lockdown_cache",
58 	"com.apple.xcode.developerdomain",
59 	"com.apple.international",
60 	"com.apple.mobile.data_sync",
61 	"com.apple.mobile.tethered_sync",
62 	"com.apple.mobile.mobile_application_usage",
63 	"com.apple.mobile.backup",
64 	"com.apple.mobile.nikita",
65 	"com.apple.mobile.restriction",
66 	"com.apple.mobile.user_preferences",
67 	"com.apple.mobile.sync_data_class",
68 	"com.apple.mobile.software_behavior",
69 	"com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands",
70 	"com.apple.mobile.iTunes.accessories",
71 	"com.apple.mobile.internal", /**< iOS 4.0+ */
72 	"com.apple.mobile.wireless_lockdown", /**< iOS 4.0+ */
73 	"com.apple.fairplay",
74 	"com.apple.iTunes",
75 	"com.apple.mobile.iTunes.store",
76 	"com.apple.mobile.iTunes",
77 	NULL
78 };
79 
is_domain_known(const char * domain)80 static int is_domain_known(const char *domain)
81 {
82 	int i = 0;
83 	while (domains[i] != NULL) {
84 		if (strstr(domain, domains[i++])) {
85 			return 1;
86 		}
87 	}
88 	return 0;
89 }
90 
print_usage(int argc,char ** argv,int is_error)91 static void print_usage(int argc, char **argv, int is_error)
92 {
93 	int i = 0;
94 	char *name = NULL;
95 	name = strrchr(argv[0], '/');
96 	fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
97 	fprintf(is_error ? stderr : stdout,
98 		"\n" \
99 		"Show information about a connected device.\n" \
100 		"\n" \
101 		"OPTIONS:\n" \
102 		"  -u, --udid UDID    target specific device by UDID\n" \
103 		"  -n, --network      connect to network device\n" \
104 		"  -s, --simple       use a simple connection to avoid auto-pairing with the device\n" \
105 		"  -q, --domain NAME  set domain of query to NAME. Default: None\n" \
106 		"  -k, --key NAME     only query key specified by NAME. Default: All keys.\n" \
107 		"  -x, --xml          output information as xml plist instead of key/value pairs\n" \
108 		"  -h, --help         prints usage information\n" \
109 		"  -d, --debug        enable communication debugging\n" \
110 		"  -v, --version      prints version information\n" \
111 		"\n"
112 	);
113 	fprintf(is_error ? stderr : stdout, "Known domains are:\n\n");
114 	while (domains[i] != NULL) {
115 		fprintf(is_error ? stderr : stdout, "  %s\n", domains[i++]);
116 	}
117 	fprintf(is_error ? stderr : stdout,
118 		"\n" \
119 		"Homepage:    <" PACKAGE_URL ">\n"
120 		"Bug Reports: <" PACKAGE_BUGREPORT ">\n"
121 	);
122 }
123 
main(int argc,char * argv[])124 int main(int argc, char *argv[])
125 {
126 	lockdownd_client_t client = NULL;
127 	lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
128 	idevice_t device = NULL;
129 	idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
130 	int simple = 0;
131 	int format = FORMAT_KEY_VALUE;
132 	const char* udid = NULL;
133 	int use_network = 0;
134 	const char *domain = NULL;
135 	const char *key = NULL;
136 	char *xml_doc = NULL;
137 	uint32_t xml_length;
138 	plist_t node = NULL;
139 
140 	int c = 0;
141 	const struct option longopts[] = {
142 		{ "debug", no_argument, NULL, 'd' },
143 		{ "help", no_argument, NULL, 'h' },
144 		{ "udid", required_argument, NULL, 'u' },
145 		{ "network", no_argument, NULL, 'n' },
146 		{ "domain", required_argument, NULL, 'q' },
147 		{ "key", required_argument, NULL, 'k' },
148 		{ "simple", no_argument, NULL, 's' },
149 		{ "xml", no_argument, NULL, 'x' },
150 		{ "version", no_argument, NULL, 'v' },
151 		{ NULL, 0, NULL, 0}
152 	};
153 
154 #ifndef WIN32
155 	signal(SIGPIPE, SIG_IGN);
156 #endif
157 
158 	while ((c = getopt_long(argc, argv, "dhu:nq:k:sxv", longopts, NULL)) != -1) {
159 		switch (c) {
160 		case 'd':
161 			idevice_set_debug_level(1);
162 			break;
163 		case 'u':
164 			if (!*optarg) {
165 				fprintf(stderr, "ERROR: UDID must not be empty!\n");
166 				print_usage(argc, argv, 1);
167 				return 2;
168 			}
169 			udid = optarg;
170 			break;
171 		case 'n':
172 			use_network = 1;
173 			break;
174 		case 'q':
175 			if (!*optarg) {
176 				fprintf(stderr, "ERROR: 'domain' must not be empty!\n");
177 				print_usage(argc, argv, 1);
178 				return 2;
179 			}
180 			domain = optarg;
181 			break;
182 		case 'k':
183 			if (!*optarg) {
184 				fprintf(stderr, "ERROR: 'key' must not be empty!\n");
185 				print_usage(argc, argv, 1);
186 				return 2;
187 			}
188 			key = optarg;
189 			break;
190 		case 'x':
191 			format = FORMAT_XML;
192 			break;
193 		case 's':
194 			simple = 1;
195 			break;
196 		case 'h':
197 			print_usage(argc, argv, 0);
198 			return 0;
199 		case 'v':
200 			printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
201 			return 0;
202 		default:
203 			print_usage(argc, argv, 1);
204 			return 2;
205 		}
206 	}
207 
208 	argc -= optind;
209 	argv += optind;
210 
211 	ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX);
212 	if (ret != IDEVICE_E_SUCCESS) {
213 		if (udid) {
214 			printf("ERROR: Device %s not found!\n", udid);
215 		} else {
216 			printf("ERROR: No device found!\n");
217 		}
218 		return -1;
219 	}
220 
221 	if (LOCKDOWN_E_SUCCESS != (ldret = simple ?
222 			lockdownd_client_new(device, &client, TOOL_NAME):
223 			lockdownd_client_new_with_handshake(device, &client, TOOL_NAME))) {
224 		fprintf(stderr, "ERROR: Could not connect to lockdownd: %s (%d)\n", lockdownd_strerror(ldret), ldret);
225 		idevice_free(device);
226 		return -1;
227 	}
228 
229 	if (domain && !is_domain_known(domain)) {
230 		fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", domain);
231 	}
232 
233 	/* run query and output information */
234 	if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS) {
235 		if (node) {
236 			switch (format) {
237 			case FORMAT_XML:
238 				plist_to_xml(node, &xml_doc, &xml_length);
239 				printf("%s", xml_doc);
240 				free(xml_doc);
241 				break;
242 			case FORMAT_KEY_VALUE:
243 				plist_print_to_stream(node, stdout);
244 				break;
245 			default:
246 				if (key != NULL)
247 					plist_print_to_stream(node, stdout);
248 			break;
249 			}
250 			plist_free(node);
251 			node = NULL;
252 		}
253 	}
254 
255 	lockdownd_client_free(client);
256 	idevice_free(device);
257 
258 	return 0;
259 }
260 
261