diff options
Diffstat (limited to 'tools/idevicepair.c')
| -rw-r--r-- | tools/idevicepair.c | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/tools/idevicepair.c b/tools/idevicepair.c new file mode 100644 index 0000000..85aa903 --- /dev/null +++ b/tools/idevicepair.c | |||
| @@ -0,0 +1,224 @@ | |||
| 1 | /* | ||
| 2 | * idevicepair.c | ||
| 3 | * Simple utility to pair/unpair an iDevice | ||
| 4 | * | ||
| 5 | * Copyright (c) 2010 Nikias Bassen 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 | |||
| 22 | #include <stdio.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <stdlib.h> | ||
| 25 | #include <getopt.h> | ||
| 26 | #include "userpref.h" | ||
| 27 | |||
| 28 | #include <libimobiledevice/libimobiledevice.h> | ||
| 29 | #include <libimobiledevice/lockdown.h> | ||
| 30 | |||
| 31 | static char *uuid = NULL; | ||
| 32 | |||
| 33 | static void print_usage(int argc, char **argv) | ||
| 34 | { | ||
| 35 | char *name = NULL; | ||
| 36 | |||
| 37 | name = strrchr(argv[0], '/'); | ||
| 38 | printf("\n%s - Pair or unpair a connected iPhone/iPod Touch/iPad.\n\n", (name ? name + 1: argv[0])); | ||
| 39 | printf("Usage: %s [OPTIONS] COMMAND\n\n", (name ? name + 1: argv[0])); | ||
| 40 | printf(" Where COMMAND is one of:\n"); | ||
| 41 | printf(" pair pair device\n"); | ||
| 42 | printf(" validate validate if paired with device\n"); | ||
| 43 | printf(" unpair unpair device\n"); | ||
| 44 | printf(" list list currently paired devices\n\n"); | ||
| 45 | printf(" The following OPTIONS are accepted:\n"); | ||
| 46 | printf(" -d, --debug enable communication debugging\n"); | ||
| 47 | printf(" -u, --uuid UUID target specific device by its 40-digit device UUID\n"); | ||
| 48 | printf(" -h, --help prints usage information\n"); | ||
| 49 | printf("\n"); | ||
| 50 | } | ||
| 51 | |||
| 52 | static void parse_opts(int argc, char **argv) | ||
| 53 | { | ||
| 54 | static struct option longopts[] = { | ||
| 55 | {"help", 0, NULL, 'h'}, | ||
| 56 | {"uuid", 0, NULL, 'u'}, | ||
| 57 | {"debug", 0, NULL, 'd'}, | ||
| 58 | {NULL, 0, NULL, 0} | ||
| 59 | }; | ||
| 60 | int c; | ||
| 61 | |||
| 62 | while (1) { | ||
| 63 | c = getopt_long(argc, argv, "hu:d", longopts, (int*)0); | ||
| 64 | if (c == -1) { | ||
| 65 | break; | ||
| 66 | } | ||
| 67 | |||
| 68 | switch (c) { | ||
| 69 | case 'h': | ||
| 70 | print_usage(argc, argv); | ||
| 71 | exit(0); | ||
| 72 | case 'u': | ||
| 73 | if (strlen(optarg) != 40) { | ||
| 74 | printf("%s: invalid UUID specified (length != 40)\n", argv[0]); | ||
| 75 | print_usage(argc, argv); | ||
| 76 | exit(2); | ||
| 77 | } | ||
| 78 | uuid = strdup(optarg); | ||
| 79 | break; | ||
| 80 | case 'd': | ||
| 81 | idevice_set_debug_level(1); | ||
| 82 | break; | ||
| 83 | default: | ||
| 84 | print_usage(argc, argv); | ||
| 85 | exit(2); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | int main(int argc, char **argv) | ||
| 91 | { | ||
| 92 | lockdownd_client_t client = NULL; | ||
| 93 | idevice_t phone = NULL; | ||
| 94 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; | ||
| 95 | lockdownd_error_t lerr; | ||
| 96 | int result; | ||
| 97 | char *type = NULL; | ||
| 98 | char *cmd; | ||
| 99 | typedef enum { | ||
| 100 | OP_NONE = 0, OP_PAIR, OP_VALIDATE, OP_UNPAIR, OP_LIST | ||
| 101 | } op_t; | ||
| 102 | op_t op = OP_NONE; | ||
| 103 | |||
| 104 | parse_opts(argc, argv); | ||
| 105 | |||
| 106 | if ((argc - optind) < 1) { | ||
| 107 | printf("ERROR: You need to specify a COMMAND!\n"); | ||
| 108 | print_usage(argc, argv); | ||
| 109 | exit(2); | ||
| 110 | } | ||
| 111 | |||
| 112 | cmd = (argv+optind)[0]; | ||
| 113 | |||
| 114 | if (!strcmp(cmd, "pair")) { | ||
| 115 | op = OP_PAIR; | ||
| 116 | } else if (!strcmp(cmd, "validate")) { | ||
| 117 | op = OP_VALIDATE; | ||
| 118 | } else if (!strcmp(cmd, "unpair")) { | ||
| 119 | op = OP_UNPAIR; | ||
| 120 | } else if (!strcmp(cmd, "list")) { | ||
| 121 | op = OP_LIST; | ||
| 122 | } else { | ||
| 123 | printf("ERROR: Invalid command '%s' specified\n", cmd); | ||
| 124 | print_usage(argc, argv); | ||
| 125 | exit(2); | ||
| 126 | } | ||
| 127 | |||
| 128 | if (op == OP_LIST) { | ||
| 129 | unsigned int i; | ||
| 130 | char **uuids = NULL; | ||
| 131 | unsigned int count = 0; | ||
| 132 | userpref_get_paired_uuids(&uuids, &count); | ||
| 133 | for (i = 0; i < count; i++) { | ||
| 134 | printf("%s\n", uuids[i]); | ||
| 135 | } | ||
| 136 | if (uuids) { | ||
| 137 | g_strfreev(uuids); | ||
| 138 | } | ||
| 139 | if (uuid) { | ||
| 140 | free(uuid); | ||
| 141 | } | ||
| 142 | return 0; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (uuid) { | ||
| 146 | ret = idevice_new(&phone, uuid); | ||
| 147 | free(uuid); | ||
| 148 | uuid = NULL; | ||
| 149 | if (ret != IDEVICE_E_SUCCESS) { | ||
| 150 | printf("No device found with uuid %s, is it plugged in?\n", uuid); | ||
| 151 | return -1; | ||
| 152 | } | ||
| 153 | } else { | ||
| 154 | ret = idevice_new(&phone, NULL); | ||
| 155 | if (ret != IDEVICE_E_SUCCESS) { | ||
| 156 | printf("No device found, is it plugged in?\n"); | ||
| 157 | return -1; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | lerr = lockdownd_client_new(phone, &client, "idevicepair"); | ||
| 162 | if (lerr != LOCKDOWN_E_SUCCESS) { | ||
| 163 | idevice_free(phone); | ||
| 164 | printf("ERROR: lockdownd_client_new failed with error code %d\n", lerr); | ||
| 165 | return -1; | ||
| 166 | } | ||
| 167 | |||
| 168 | result = 0; | ||
| 169 | lerr = lockdownd_query_type(client, &type); | ||
| 170 | if (lerr != LOCKDOWN_E_SUCCESS) { | ||
| 171 | printf("QueryType failed, error code %d\n", lerr); | ||
| 172 | result = -1; | ||
| 173 | goto leave; | ||
| 174 | } else { | ||
| 175 | if (strcmp("com.apple.mobile.lockdown", type)) { | ||
| 176 | printf("WARNING: QueryType request returned '%s'\n", type); | ||
| 177 | } | ||
| 178 | if (type) { | ||
| 179 | free(type); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | ret = idevice_get_uuid(phone, &uuid); | ||
| 184 | if (ret != IDEVICE_E_SUCCESS) { | ||
| 185 | printf("Could not get device uuid, error code %d\n", ret); | ||
| 186 | result = -1; | ||
| 187 | goto leave; | ||
| 188 | } | ||
| 189 | |||
| 190 | if ((op == OP_PAIR) || (op == OP_VALIDATE)) { | ||
| 191 | /* TODO */ | ||
| 192 | lerr = lockdownd_pair(client, NULL); | ||
| 193 | if (op == OP_VALIDATE) { | ||
| 194 | ret = lockdownd_validate_pair(client, NULL); | ||
| 195 | } | ||
| 196 | if (lerr == LOCKDOWN_E_SUCCESS) { | ||
| 197 | printf("SUCCESS - device %s paired\n", uuid); | ||
| 198 | } else if (lerr == LOCKDOWN_E_PASSWORD_PROTECTED) { | ||
| 199 | printf("ERROR - Could not pair device because a passcode is set. Enter the passcode on the device and try again.\n"); | ||
| 200 | } else { | ||
| 201 | printf("ERROR - Pairing failed, error code %d\n", lerr); | ||
| 202 | } | ||
| 203 | } else if (op == OP_UNPAIR) { | ||
| 204 | lerr = lockdownd_unpair(client, NULL); | ||
| 205 | if (lerr == LOCKDOWN_E_SUCCESS) { | ||
| 206 | printf("SUCCESS - device %s unpaired\n", uuid); | ||
| 207 | } else { | ||
| 208 | if (lerr == LOCKDOWN_E_INVALID_HOST_ID) { | ||
| 209 | printf("ERROR - Unpair %s failed: device is not paired with this system\n", uuid); | ||
| 210 | } else { | ||
| 211 | printf("ERROR - Unpair %s failed: return code %d\n", uuid, lerr); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | leave: | ||
| 217 | lockdownd_client_free(client); | ||
| 218 | idevice_free(phone); | ||
| 219 | if (uuid) { | ||
| 220 | free(uuid); | ||
| 221 | } | ||
| 222 | return result; | ||
| 223 | } | ||
| 224 | |||
