/libimobiledevice-tools/ideviceinfo/ideviceinfo.cpp

https://github.com/outbred/libimobiledevice-win32 · C++ · 413 lines · 346 code · 41 blank · 26 comment · 71 complexity · e6b0ecff68f7945d518734776b26b4a9 MD5 · raw file

  1. /*
  2. * ideviceinfo.c
  3. * Simple utility to show information about an attached device
  4. *
  5. * Copyright (c) 2009 Martin Szulecki All Rights Reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #ifdef WIN32
  26. # include <time.h>
  27. #else
  28. # include <sys/time.h>
  29. #endif
  30. #include <libimobiledevice/libimobiledevice.h>
  31. #include <libimobiledevice/lockdown.h>
  32. #include <plist/plist.h>
  33. struct timeval_t {
  34. long tv_sec; /* seconds */
  35. long tv_usec; /* and microseconds */
  36. };
  37. typedef timeval_t timeval;
  38. #define FORMAT_KEY_VALUE 1
  39. #define FORMAT_XML 2
  40. static const char *domains[] = {
  41. "com.apple.disk_usage",
  42. "com.apple.disk_usage.factory",
  43. "com.apple.mobile.battery",
  44. /* FIXME: For some reason lockdownd segfaults on this, works sometimes though
  45. "com.apple.mobile.debug",. */
  46. "com.apple.iqagent",
  47. "com.apple.purplebuddy",
  48. "com.apple.PurpleBuddy",
  49. "com.apple.mobile.chaperone",
  50. "com.apple.mobile.third_party_termination",
  51. "com.apple.mobile.lockdownd",
  52. "com.apple.mobile.lockdown_cache",
  53. "com.apple.xcode.developerdomain",
  54. "com.apple.international",
  55. "com.apple.mobile.data_sync",
  56. "com.apple.mobile.tethered_sync",
  57. "com.apple.mobile.mobile_application_usage",
  58. "com.apple.mobile.backup",
  59. "com.apple.mobile.nikita",
  60. "com.apple.mobile.restriction",
  61. "com.apple.mobile.user_preferences",
  62. "com.apple.mobile.sync_data_class",
  63. "com.apple.mobile.software_behavior",
  64. "com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands",
  65. "com.apple.mobile.iTunes.accessories",
  66. "com.apple.mobile.internal", /**< iOS 4.0+ */
  67. "com.apple.mobile.wireless_lockdown", /**< iOS 4.0+ */
  68. "com.apple.fairplay",
  69. "com.apple.iTunes",
  70. "com.apple.mobile.iTunes.store",
  71. "com.apple.mobile.iTunes",
  72. NULL
  73. };
  74. static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  75. static const char base64_pad = '=';
  76. static char *base64encode(const unsigned char *buf, size_t size)
  77. {
  78. if (!buf || !(size > 0)) return NULL;
  79. int outlen = (size / 3) * 4;
  80. char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0'
  81. size_t n = 0;
  82. size_t m = 0;
  83. unsigned char input[3];
  84. unsigned int output[4];
  85. while (n < size) {
  86. input[0] = buf[n];
  87. input[1] = (n+1 < size) ? buf[n+1] : 0;
  88. input[2] = (n+2 < size) ? buf[n+2] : 0;
  89. output[0] = input[0] >> 2;
  90. output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
  91. output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
  92. output[3] = input[2] & 63;
  93. outbuf[m++] = base64_str[(int)output[0]];
  94. outbuf[m++] = base64_str[(int)output[1]];
  95. outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad;
  96. outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad;
  97. n+=3;
  98. }
  99. outbuf[m] = 0; // 0-termination!
  100. return outbuf;
  101. }
  102. static int indent_level = 0;
  103. static int is_domain_known(char *domain)
  104. {
  105. int i = 0;
  106. while (domains[i] != NULL) {
  107. if (strstr(domain, domains[i++])) {
  108. return 1;
  109. }
  110. }
  111. return 0;
  112. }
  113. static void plist_node_to_string(plist_t node);
  114. static void plist_array_to_string(plist_t node)
  115. {
  116. /* iterate over items */
  117. int i, count;
  118. plist_t subnode = NULL;
  119. count = plist_array_get_size(node);
  120. for (i = 0; i < count; i++) {
  121. subnode = plist_array_get_item(node, i);
  122. printf("%*s", indent_level, "");
  123. printf("%d: ", i);
  124. plist_node_to_string(subnode);
  125. }
  126. }
  127. static void plist_dict_to_string(plist_t node)
  128. {
  129. /* iterate over key/value pairs */
  130. plist_dict_iter it = NULL;
  131. char* key = NULL;
  132. plist_t subnode = NULL;
  133. plist_dict_new_iter(node, &it);
  134. plist_dict_next_item(node, it, &key, &subnode);
  135. while (subnode)
  136. {
  137. printf("%*s", indent_level, "");
  138. printf("%s", key);
  139. if (plist_get_node_type(subnode) == PLIST_ARRAY)
  140. printf("[%d]: ", plist_array_get_size(subnode));
  141. else
  142. printf(": ");
  143. free(key);
  144. key = NULL;
  145. plist_node_to_string(subnode);
  146. plist_dict_next_item(node, it, &key, &subnode);
  147. }
  148. free(it);
  149. }
  150. static void plist_node_to_string(plist_t node)
  151. {
  152. char *s = NULL;
  153. char *data = NULL;
  154. double d;
  155. uint8_t b;
  156. uint64_t u = 0;
  157. timeval_t tv = { 0, 0 };
  158. plist_type t;
  159. if (!node)
  160. return;
  161. t = plist_get_node_type(node);
  162. switch (t) {
  163. case PLIST_BOOLEAN:
  164. plist_get_bool_val(node, &b);
  165. printf("%s\n", (b ? "true" : "false"));
  166. break;
  167. case PLIST_UINT:
  168. plist_get_uint_val(node, &u);
  169. printf("%llu\n", (long long)u);
  170. break;
  171. case PLIST_REAL:
  172. plist_get_real_val(node, &d);
  173. printf("%f\n", d);
  174. break;
  175. case PLIST_STRING:
  176. plist_get_string_val(node, &s);
  177. printf("%s\n", s);
  178. free(s);
  179. break;
  180. case PLIST_KEY:
  181. plist_get_key_val(node, &s);
  182. printf("%s: ", s);
  183. free(s);
  184. break;
  185. case PLIST_DATA:
  186. plist_get_data_val(node, &data, &u);
  187. if (u > 0) {
  188. s = base64encode((unsigned char*)data, u);
  189. free(data);
  190. if (s) {
  191. printf("%s\n", s);
  192. free(s);
  193. } else {
  194. printf("\n");
  195. }
  196. } else {
  197. printf("\n");
  198. }
  199. break;
  200. case PLIST_DATE:
  201. plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec);
  202. {
  203. time_t ti = (time_t)tv.tv_sec;
  204. struct tm *btime = localtime(&ti);
  205. if (btime) {
  206. s = (char*)malloc(24);
  207. memset(s, 0, 24);
  208. if (strftime(s, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) {
  209. free (s);
  210. s = NULL;
  211. }
  212. }
  213. }
  214. if (s) {
  215. printf("%s\n", s);
  216. free(s);
  217. } else {
  218. printf("\n");
  219. }
  220. break;
  221. case PLIST_ARRAY:
  222. printf("\n");
  223. indent_level++;
  224. plist_array_to_string(node);
  225. indent_level--;
  226. break;
  227. case PLIST_DICT:
  228. printf("\n");
  229. indent_level++;
  230. plist_dict_to_string(node);
  231. indent_level--;
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. static void print_usage(int argc, char **argv)
  238. {
  239. int i = 0;
  240. char *name = NULL;
  241. name = strrchr(argv[0], '/');
  242. printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
  243. printf("Show information about a connected device.\n\n");
  244. printf(" -d, --debug\t\tenable communication debugging\n");
  245. printf(" -s, --simple\t\tuse a simple connection to avoid auto-pairing with the device\n");
  246. printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
  247. printf(" -q, --domain NAME\tset domain of query to NAME. Default: None\n");
  248. printf(" -k, --key NAME\tonly query key specified by NAME. Default: All keys.\n");
  249. printf(" -x, --xml\t\toutput information as xml plist instead of key/value pairs\n");
  250. printf(" -h, --help\t\tprints usage information\n");
  251. printf("\n");
  252. printf(" Known domains are:\n\n");
  253. while (domains[i] != NULL) {
  254. printf(" %s\n", domains[i++]);
  255. }
  256. printf("\n");
  257. }
  258. int main(int argc, char *argv[])
  259. {
  260. lockdownd_client_t client = NULL;
  261. idevice_t device = NULL;
  262. idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
  263. int i;
  264. int simple = 0;
  265. int format = FORMAT_KEY_VALUE;
  266. const char* udid = NULL;
  267. char *domain = NULL;
  268. char *key = NULL;
  269. char *xml_doc = NULL;
  270. uint32_t xml_length;
  271. plist_t node = NULL;
  272. plist_type node_type;
  273. /* parse cmdline args */
  274. for (i = 1; i < argc; i++) {
  275. if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
  276. idevice_set_debug_level(1);
  277. continue;
  278. }
  279. else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
  280. i++;
  281. if (!argv[i] || (strlen(argv[i]) != 40)) {
  282. print_usage(argc, argv);
  283. return 0;
  284. }
  285. udid = argv[i];
  286. continue;
  287. }
  288. else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--domain")) {
  289. i++;
  290. if (!argv[i] || (strlen(argv[i]) < 4)) {
  291. print_usage(argc, argv);
  292. return 0;
  293. }
  294. if (!is_domain_known(argv[i])) {
  295. fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", argv[i]);
  296. }
  297. domain = strdup(argv[i]);
  298. continue;
  299. }
  300. else if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--key")) {
  301. i++;
  302. if (!argv[i] || (strlen(argv[i]) <= 1)) {
  303. print_usage(argc, argv);
  304. return 0;
  305. }
  306. key = strdup(argv[i]);
  307. continue;
  308. }
  309. else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--xml")) {
  310. format = FORMAT_XML;
  311. continue;
  312. }
  313. else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--simple")) {
  314. simple = 1;
  315. continue;
  316. }
  317. else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  318. print_usage(argc, argv);
  319. return 0;
  320. }
  321. else {
  322. print_usage(argc, argv);
  323. return 0;
  324. }
  325. }
  326. ret = idevice_new(&device, udid);
  327. if (ret != IDEVICE_E_SUCCESS) {
  328. if (udid) {
  329. printf("No device found with udid %s, is it plugged in?\n", udid);
  330. } else {
  331. printf("No device found, is it plugged in?\n");
  332. }
  333. return -1;
  334. }
  335. if (LOCKDOWN_E_SUCCESS != (simple ?
  336. lockdownd_client_new(device, &client, "ideviceinfo"):
  337. lockdownd_client_new_with_handshake(device, &client, "ideviceinfo"))) {
  338. idevice_free(device);
  339. return -1;
  340. }
  341. /* run query and output information */
  342. if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS) {
  343. if (node) {
  344. switch (format) {
  345. case FORMAT_XML:
  346. plist_to_xml(node, &xml_doc, &xml_length);
  347. printf("%s", xml_doc);
  348. free(xml_doc);
  349. break;
  350. case FORMAT_KEY_VALUE:
  351. node_type = plist_get_node_type(node);
  352. if (node_type == PLIST_DICT) {
  353. plist_dict_to_string(node);
  354. } else if (node_type == PLIST_ARRAY) {
  355. plist_array_to_string(node);
  356. break;
  357. }
  358. default:
  359. if (key != NULL)
  360. plist_node_to_string(node);
  361. break;
  362. }
  363. plist_free(node);
  364. node = NULL;
  365. }
  366. }
  367. if (domain != NULL)
  368. free(domain);
  369. lockdownd_client_free(client);
  370. idevice_free(device);
  371. return 0;
  372. }