| 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 <plist/plist.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 |
"com.apple.fmip", |
| 78 |
"com.apple.Accessibility", |
| 79 |
NULL |
| 80 |
}; |
| 81 |
|
| 82 |
static int is_domain_known(const char *domain) |
| 83 |
{ |
| 84 |
int i = 0; |
| 85 |
while (domains[i] != NULL) { |
| 86 |
if (strstr(domain, domains[i++])) { |
| 87 |
return 1; |
| 88 |
} |
| 89 |
} |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
static void print_usage(int argc, char **argv, int is_error) |
| 94 |
{ |
| 95 |
int i = 0; |
| 96 |
char *name = strrchr(argv[0], '/'); |
| 97 |
fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); |
| 98 |
fprintf(is_error ? stderr : stdout, |
| 99 |
"\n" |
| 100 |
"Show information about a connected device.\n" |
| 101 |
"\n" |
| 102 |
"OPTIONS:\n" |
| 103 |
" -u, --udid UDID target specific device by UDID\n" |
| 104 |
" -n, --network connect to network device\n" |
| 105 |
" -s, --simple use simple connection to avoid auto-pairing with device\n" |
| 106 |
" -q, --domain NAME set domain of query to NAME. Default: None\n" \ |
| 107 |
" -k, --key NAME only query key specified by NAME. Default: All keys.\n" \ |
| 108 |
" -x, --xml output information in XML property list format\n" \ |
| 109 |
" -h, --help prints usage information\n" \ |
| 110 |
" -d, --debug enable communication debugging\n" \ |
| 111 |
" -v, --version prints version information\n" \ |
| 112 |
"\n" |
| 113 |
); |
| 114 |
fprintf(is_error ? stderr : stdout, "Known domains are:\n\n"); |
| 115 |
while (domains[i] != NULL) { |
| 116 |
fprintf(is_error ? stderr : stdout, " %s\n", domains[i++]); |
| 117 |
} |
| 118 |
fprintf(is_error ? stderr : stdout, |
| 119 |
"\n" \ |
| 120 |
"Homepage: <" PACKAGE_URL ">\n" |
| 121 |
"Bug Reports: <" PACKAGE_BUGREPORT ">\n" |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
int main(int argc, char *argv[]) |
| 126 |
{ |
| 127 |
lockdownd_client_t client = NULL; |
| 128 |
lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; |
| 129 |
idevice_t device = NULL; |
| 130 |
idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; |
| 131 |
int simple = 0; |
| 132 |
int format = FORMAT_KEY_VALUE; |
| 133 |
const char* udid = NULL; |
| 134 |
int use_network = 0; |
| 135 |
const char *domain = NULL; |
| 136 |
const char *key = NULL; |
| 137 |
char *xml_doc = NULL; |
| 138 |
uint32_t xml_length; |
| 139 |
plist_t node = NULL; |
| 140 |
|
| 141 |
int c = 0; |
| 142 |
const struct option longopts[] = { |
| 143 |
{ "debug", no_argument, NULL, 'd' }, |
| 144 |
{ "help", no_argument, NULL, 'h' }, |
| 145 |
{ "udid", required_argument, NULL, 'u' }, |
| 146 |
{ "network", no_argument, NULL, 'n' }, |
| 147 |
{ "domain", required_argument, NULL, 'q' }, |
| 148 |
{ "key", required_argument, NULL, 'k' }, |
| 149 |
{ "simple", no_argument, NULL, 's' }, |
| 150 |
{ "xml", no_argument, NULL, 'x' }, |
| 151 |
{ "version", no_argument, NULL, 'v' }, |
| 152 |
{ NULL, 0, NULL, 0} |
| 153 |
}; |
| 154 |
|
| 155 |
#ifndef _WIN32 |
| 156 |
signal(SIGPIPE, SIG_IGN); |
| 157 |
#endif |
| 158 |
|
| 159 |
while ((c = getopt_long(argc, argv, "dhu:nq:k:sxv", longopts, NULL)) != -1) { |
| 160 |
switch (c) { |
| 161 |
case 'd': |
| 162 |
idevice_set_debug_level(1); |
| 163 |
break; |
| 164 |
case 'u': |
| 165 |
if (!*optarg) { |
| 166 |
fprintf(stderr, "ERROR: UDID must not be empty!\n"); |
| 167 |
print_usage(argc, argv, 1); |
| 168 |
return 2; |
| 169 |
} |
| 170 |
udid = optarg; |
| 171 |
break; |
| 172 |
case 'n': |
| 173 |
use_network = 1; |
| 174 |
break; |
| 175 |
case 'q': |
| 176 |
if (!*optarg) { |
| 177 |
fprintf(stderr, "ERROR: 'domain' must not be empty!\n"); |
| 178 |
print_usage(argc, argv, 1); |
| 179 |
return 2; |
| 180 |
} |
| 181 |
domain = optarg; |
| 182 |
break; |
| 183 |
case 'k': |
| 184 |
if (!*optarg) { |
| 185 |
fprintf(stderr, "ERROR: 'key' must not be empty!\n"); |
| 186 |
print_usage(argc, argv, 1); |
| 187 |
return 2; |
| 188 |
} |
| 189 |
key = optarg; |
| 190 |
break; |
| 191 |
case 'x': |
| 192 |
format = FORMAT_XML; |
| 193 |
break; |
| 194 |
case 's': |
| 195 |
simple = 1; |
| 196 |
break; |
| 197 |
case 'h': |
| 198 |
print_usage(argc, argv, 0); |
| 199 |
return 0; |
| 200 |
case 'v': |
| 201 |
printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); |
| 202 |
return 0; |
| 203 |
default: |
| 204 |
print_usage(argc, argv, 1); |
| 205 |
return 2; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
argc -= optind; |
| 210 |
argv += optind; |
| 211 |
|
| 212 |
ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); |
| 213 |
if (ret != IDEVICE_E_SUCCESS) { |
| 214 |
if (udid) { |
| 215 |
fprintf(stderr, "ERROR: Device %s not found!\n", udid); |
| 216 |
} else { |
| 217 |
fprintf(stderr, "ERROR: No device found!\n"); |
| 218 |
} |
| 219 |
return -1; |
| 220 |
} |
| 221 |
|
| 222 |
if (LOCKDOWN_E_SUCCESS != (ldret = simple ? |
| 223 |
lockdownd_client_new(device, &client, TOOL_NAME): |
| 224 |
lockdownd_client_new_with_handshake(device, &client, TOOL_NAME))) { |
| 225 |
fprintf(stderr, "ERROR: Could not connect to lockdownd: %s (%d)\n", lockdownd_strerror(ldret), ldret); |
| 226 |
idevice_free(device); |
| 227 |
return -1; |
| 228 |
} |
| 229 |
|
| 230 |
if (domain && !is_domain_known(domain)) { |
| 231 |
fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", domain); |
| 232 |
} |
| 233 |
|
| 234 |
/* run query and output information */ |
| 235 |
if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS) { |
| 236 |
if (node) { |
| 237 |
switch (format) { |
| 238 |
case FORMAT_XML: |
| 239 |
plist_to_xml(node, &xml_doc, &xml_length); |
| 240 |
printf("%s", xml_doc); |
| 241 |
free(xml_doc); |
| 242 |
break; |
| 243 |
case FORMAT_KEY_VALUE: |
| 244 |
plist_write_to_stream(node, stdout, PLIST_FORMAT_LIMD, 0); |
| 245 |
break; |
| 246 |
default: |
| 247 |
if (key != NULL) |
| 248 |
plist_write_to_stream(node, stdout, PLIST_FORMAT_LIMD, 0); |
| 249 |
break; |
| 250 |
} |
| 251 |
plist_free(node); |
| 252 |
node = NULL; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
lockdownd_client_free(client); |
| 257 |
idevice_free(device); |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |
| 261 |
|