Debian Codegraph @

tools/idevicepair.c

raw
1 /*
2 * idevicepair.c
3 * Manage pairings with devices and this host
4 *
5 * Copyright (c) 2010-2021 Nikias Bassen, All Rights Reserved.
6 * Copyright (c) 2014 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 "idevicepair"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <getopt.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #ifdef _WIN32
36 #include <windows.h>
37 #include <conio.h>
38 #else
39 #include <termios.h>
40 #include <signal.h>
41 #endif
42
43 #include "common/userpref.h"
44
45 #include <libimobiledevice/libimobiledevice.h>
46 #include <libimobiledevice/lockdown.h>
47 #include <plist/plist.h>
48
49 static char *udid = NULL;
50
51 #ifdef HAVE_WIRELESS_PAIRING
52
53 #ifdef _WIN32
54 #define BS_CC '\b'
55 #define my_getch getch
56 #else
57 #define BS_CC 0x7f
58 static int my_getch(void)
59 {
60 struct termios oldt, newt;
61 int ch;
62 tcgetattr(STDIN_FILENO, &oldt);
63 newt = oldt;
64 newt.c_lflag &= ~(ICANON | ECHO);
65 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
66 ch = getchar();
67 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
68 return ch;
69 }
70 #endif
71
72 static int get_hidden_input(char *buf, int maxlen)
73 {
74 int pwlen = 0;
75 int c;
76
77 while ((c = my_getch())) {
78 if ((c == '\r') || (c == '\n')) {
79 break;
80 } else if (isprint(c)) {
81 if (pwlen < maxlen-1)
82 buf[pwlen++] = c;
83 fputc('*', stderr);
84 } else if (c == BS_CC) {
85 if (pwlen > 0) {
86 fputs("\b \b", stderr);
87 pwlen--;
88 }
89 }
90 }
91 buf[pwlen] = 0;
92 return pwlen;
93 }
94
95 static void pairing_cb(lockdownd_cu_pairing_cb_type_t cb_type, void *user_data, void* data_ptr, unsigned int* data_size)
96 {
97 if (cb_type == LOCKDOWN_CU_PAIRING_PIN_REQUESTED) {
98 printf("Enter PIN: ");
99 fflush(stdout);
100
101 *data_size = get_hidden_input((char*)data_ptr, *data_size);
102
103 printf("\n");
104 } else if (cb_type == LOCKDOWN_CU_PAIRING_DEVICE_INFO) {
105 printf("Device info:\n");
106 plist_write_to_stream((plist_t)data_ptr, stdout, PLIST_FORMAT_LIMD, PLIST_OPT_INDENT | PLIST_OPT_INDENT_BY(2));
107 } else if (cb_type == LOCKDOWN_CU_PAIRING_ERROR) {
108 printf("ERROR: %s\n", (data_ptr) ? (char*)data_ptr : "(unknown)");
109 }
110 }
111
112 #endif /* HAVE_WIRELESS_PAIRING */
113
114 static void print_error_message(lockdownd_error_t err)
115 {
116 switch (err) {
117 case LOCKDOWN_E_PASSWORD_PROTECTED:
118 printf("ERROR: Could not validate with device %s because a passcode is set. Please enter the passcode on the device and retry.\n", udid);
119 break;
120 case LOCKDOWN_E_INVALID_CONF:
121 case LOCKDOWN_E_INVALID_HOST_ID:
122 printf("ERROR: Device %s is not paired with this host\n", udid);
123 break;
124 case LOCKDOWN_E_PAIRING_DIALOG_RESPONSE_PENDING:
125 printf("ERROR: Please accept the trust dialog on the screen of device %s, then attempt to pair again.\n", udid);
126 break;
127 case LOCKDOWN_E_USER_DENIED_PAIRING:
128 printf("ERROR: Device %s said that the user denied the trust dialog.\n", udid);
129 break;
130 case LOCKDOWN_E_PAIRING_FAILED:
131 printf("ERROR: Pairing with device %s failed.\n", udid);
132 break;
133 case LOCKDOWN_E_GET_PROHIBITED:
134 case LOCKDOWN_E_PAIRING_PROHIBITED_OVER_THIS_CONNECTION:
135 printf("ERROR: Pairing is not possible over this connection.\n");
136 #ifdef HAVE_WIRELESS_PAIRING
137 printf("To perform a wireless pairing use the -w command line switch. See usage or man page for details.\n");
138 #endif
139 break;
140 default:
141 printf("ERROR: Device %s returned unhandled error code %d\n", udid, err);
142 break;
143 }
144 }
145
146 static void print_usage(int argc, char **argv, int is_error)
147 {
148 char *name = strrchr(argv[0], '/');
149 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0]));
150 fprintf(is_error ? stderr : stdout,
151 "\n"
152 "Manage host pairings with devices and usbmuxd.\n"
153 "\n"
154 "Where COMMAND is one of:\n"
155 " systembuid print the system buid of the usbmuxd host\n"
156 " hostid print the host id for target device\n"
157 " pair pair device with this host\n"
158 " validate validate if device is paired with this host\n"
159 " unpair unpair device with this host\n"
160 " list list devices paired with this host\n"
161 "\n"
162 "The following OPTIONS are accepted:\n"
163 " -u, --udid UDID target specific device by UDID\n"
164 );
165 #ifdef HAVE_WIRELESS_PAIRING
166 fprintf(is_error ? stderr : stdout,
167 " -w, --wireless perform wireless pairing (see NOTE)\n"
168 " -n, --network connect to network device (see NOTE)\n"
169 );
170 #endif
171 fprintf(is_error ? stderr : stdout,
172 " -d, --debug enable communication debugging\n"
173 " -h, --help prints usage information\n"
174 " -v, --version prints version information\n"
175 );
176 #ifdef HAVE_WIRELESS_PAIRING
177 fprintf(is_error ? stderr : stdout,
178 "\n"
179 "NOTE: Pairing over network (wireless pairing) is only supported by Apple TV\n"
180 "devices. To perform a wireless pairing, you need to use the -w command line\n"
181 "switch. Make sure to put the device into pairing mode first by opening\n"
182 "Settings > Remotes and Devices > Remote App and Devices.\n"
183 );
184 #endif
185 fprintf(is_error ? stderr : stdout,
186 "\n"
187 "Homepage: <" PACKAGE_URL ">\n"
188 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
189 );
190 }
191
192 int main(int argc, char **argv)
193 {
194 int c = 0;
195 static struct option longopts[] = {
196 { "help", no_argument, NULL, 'h' },
197 { "udid", required_argument, NULL, 'u' },
198 #ifdef HAVE_WIRELESS_PAIRING
199 { "wireless", no_argument, NULL, 'w' },
200 { "network", no_argument, NULL, 'n' },
201 { "hostinfo", required_argument, NULL, 1 },
202 #endif
203 { "debug", no_argument, NULL, 'd' },
204 { "version", no_argument, NULL, 'v' },
205 { NULL, 0, NULL, 0}
206 };
207 #ifdef HAVE_WIRELESS_PAIRING
208 #define SHORT_OPTIONS "hu:wndv"
209 #else
210 #define SHORT_OPTIONS "hu:dv"
211 #endif
212 lockdownd_client_t client = NULL;
213 idevice_t device = NULL;
214 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
215 lockdownd_error_t lerr;
216 int result;
217
218 char *type = NULL;
219 int use_network = 0;
220 int wireless_pairing = 0;
221 #ifdef HAVE_WIRELESS_PAIRING
222 plist_t host_info_plist = NULL;
223 #endif
224 char *cmd;
225 typedef enum {
226 OP_NONE = 0, OP_PAIR, OP_VALIDATE, OP_UNPAIR, OP_LIST, OP_HOSTID, OP_SYSTEMBUID
227 } op_t;
228 op_t op = OP_NONE;
229
230 while ((c = getopt_long(argc, argv, SHORT_OPTIONS, longopts, NULL)) != -1) {
231 switch (c) {
232 case 'h':
233 print_usage(argc, argv, 0);
234 exit(EXIT_SUCCESS);
235 case 'u':
236 if (!*optarg) {
237 fprintf(stderr, "ERROR: UDID must not be empty!\n");
238 print_usage(argc, argv, 1);
239 result = EXIT_FAILURE;
240 goto leave;
241 }
242 free(udid);
243 udid = strdup(optarg);
244 break;
245 #ifdef HAVE_WIRELESS_PAIRING
246 case 'w':
247 wireless_pairing = 1;
248 break;
249 case 'n':
250 use_network = 1;
251 break;
252 case 1:
253 if (!*optarg) {
254 fprintf(stderr, "ERROR: --hostinfo argument must not be empty!\n");
255 result = EXIT_FAILURE;
256 goto leave;
257 }
258 if (*optarg == '@') {
259 plist_read_from_file(optarg+1, &host_info_plist, NULL);
260 if (!host_info_plist) {
261 fprintf(stderr, "ERROR: Could not read from file '%s'\n", optarg+1);
262 result = EXIT_FAILURE;
263 goto leave;
264 }
265 }
266 #ifdef HAVE_PLIST_JSON
267 else if (*optarg == '{') {
268 if (plist_from_json(optarg, strlen(optarg), &host_info_plist) != PLIST_ERR_SUCCESS) {
269 fprintf(stderr, "ERROR: --hostinfo argument not valid. Make sure it is a JSON dictionary.\n");
270 result = EXIT_FAILURE;
271 goto leave;
272 }
273 }
274 #endif
275 else {
276 fprintf(stderr, "ERROR: --hostinfo argument not valid. To specify a path prefix with '@'\n");
277 result = EXIT_FAILURE;
278 goto leave;
279 }
280 break;
281 #endif
282 case 'd':
283 idevice_set_debug_level(1);
284 break;
285 case 'v':
286 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
287 result = EXIT_SUCCESS;
288 goto leave;
289 default:
290 print_usage(argc, argv, 1);
291 result = EXIT_FAILURE;
292 goto leave;
293 }
294 }
295
296 #ifndef _WIN32
297 signal(SIGPIPE, SIG_IGN);
298 #endif
299
300 if ((argc - optind) < 1) {
301 fprintf(stderr, "ERROR: You need to specify a COMMAND!\n");
302 print_usage(argc, argv, 1);
303 result = EXIT_FAILURE;
304 goto leave;
305 }
306
307 if (wireless_pairing && use_network) {
308 fprintf(stderr, "ERROR: You cannot use -w and -n together.\n");
309 print_usage(argc, argv, 1);
310 result = EXIT_FAILURE;
311 goto leave;
312 }
313
314 cmd = (argv+optind)[0];
315
316 if (!strcmp(cmd, "pair")) {
317 op = OP_PAIR;
318 } else if (!strcmp(cmd, "validate")) {
319 op = OP_VALIDATE;
320 } else if (!strcmp(cmd, "unpair")) {
321 op = OP_UNPAIR;
322 } else if (!strcmp(cmd, "list")) {
323 op = OP_LIST;
324 } else if (!strcmp(cmd, "hostid")) {
325 op = OP_HOSTID;
326 } else if (!strcmp(cmd, "systembuid")) {
327 op = OP_SYSTEMBUID;
328 } else {
329 fprintf(stderr, "ERROR: Invalid command '%s' specified\n", cmd);
330 print_usage(argc, argv, 1);
331 result = EXIT_FAILURE;
332 goto leave;
333 }
334
335 if (wireless_pairing) {
336 if (op == OP_VALIDATE || op == OP_UNPAIR) {
337 fprintf(stderr, "ERROR: Command '%s' is not supported with -w\n", cmd);
338 print_usage(argc, argv, 1);
339 result = EXIT_FAILURE;
340 goto leave;
341 }
342 use_network = 1;
343 }
344
345 if (op == OP_SYSTEMBUID) {
346 char *systembuid = NULL;
347 userpref_read_system_buid(&systembuid);
348
349 printf("%s\n", systembuid);
350
351 free(systembuid);
352
353 result = EXIT_SUCCESS;
354 goto leave;
355 }
356
357 if (op == OP_LIST) {
358 unsigned int i;
359 char **udids = NULL;
360 unsigned int count = 0;
361 userpref_get_paired_udids(&udids, &count);
362 for (i = 0; i < count; i++) {
363 printf("%s\n", udids[i]);
364 free(udids[i]);
365 }
366 free(udids);
367 result = EXIT_SUCCESS;
368 goto leave;
369 }
370
371 ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX);
372 if (ret != IDEVICE_E_SUCCESS) {
373 if (udid) {
374 printf("No device found with udid %s.\n", udid);
375 } else {
376 printf("No device found.\n");
377 }
378 result = EXIT_FAILURE;
379 goto leave;
380 }
381 if (!udid) {
382 ret = idevice_get_udid(device, &udid);
383 if (ret != IDEVICE_E_SUCCESS) {
384 printf("ERROR: Could not get device udid, error code %d\n", ret);
385 result = EXIT_FAILURE;
386 goto leave;
387 }
388 }
389
390 if (op == OP_HOSTID) {
391 plist_t pair_record = NULL;
392 char *hostid = NULL;
393
394 userpref_read_pair_record(udid, &pair_record);
395 pair_record_get_host_id(pair_record, &hostid);
396
397 printf("%s\n", hostid);
398
399 free(hostid);
400 plist_free(pair_record);
401
402 result = EXIT_SUCCESS;
403 goto leave;
404 }
405
406 lerr = lockdownd_client_new(device, &client, TOOL_NAME);
407 if (lerr != LOCKDOWN_E_SUCCESS) {
408 printf("ERROR: Could not connect to lockdownd, error code %d\n", lerr);
409 result = EXIT_FAILURE;
410 goto leave;
411 }
412
413 result = EXIT_SUCCESS;
414
415 lerr = lockdownd_query_type(client, &type);
416 if (lerr != LOCKDOWN_E_SUCCESS) {
417 printf("QueryType failed, error code %d\n", lerr);
418 result = EXIT_FAILURE;
419 goto leave;
420 } else {
421 if (strcmp("com.apple.mobile.lockdown", type) != 0) {
422 printf("WARNING: QueryType request returned '%s'\n", type);
423 }
424 free(type);
425 }
426
427 switch(op) {
428 default:
429 case OP_PAIR:
430 #ifdef HAVE_WIRELESS_PAIRING
431 if (wireless_pairing) {
432 lerr = lockdownd_cu_pairing_create(client, pairing_cb, NULL, host_info_plist, NULL);
433 if (lerr == LOCKDOWN_E_SUCCESS) {
434 lerr = lockdownd_pair_cu(client);
435 }
436 } else
437 #endif
438 {
439 lerr = lockdownd_pair(client, NULL);
440 }
441 if (lerr == LOCKDOWN_E_SUCCESS) {
442 printf("SUCCESS: Paired with device %s\n", udid);
443 } else {
444 result = EXIT_FAILURE;
445 print_error_message(lerr);
446 }
447 break;
448
449 case OP_VALIDATE:
450 lockdownd_client_free(client);
451 client = NULL;
452 lerr = lockdownd_client_new_with_handshake(device, &client, TOOL_NAME);
453 if (lerr == LOCKDOWN_E_SUCCESS) {
454 printf("SUCCESS: Validated pairing with device %s\n", udid);
455 } else {
456 result = EXIT_FAILURE;
457 print_error_message(lerr);
458 }
459 break;
460
461 case OP_UNPAIR:
462 lerr = lockdownd_unpair(client, NULL);
463 if (lerr == LOCKDOWN_E_SUCCESS) {
464 printf("SUCCESS: Unpaired with device %s\n", udid);
465 } else {
466 result = EXIT_FAILURE;
467 print_error_message(lerr);
468 }
469 break;
470 }
471
472 leave:
473 lockdownd_client_free(client);
474 idevice_free(device);
475 free(udid);
476
477 return result;
478 }
479