1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/*
* ideviceactivation.c
* A command line tool to handle the activation process
*
* Copyright (c) 2011-2015 Mirell Development, All Rights Reserved.
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <plist/plist.h>
#include <libimobiledevice/lockdown.h>
#include <libimobiledevice/libimobiledevice.h>
#include <libideviceactivation.h>
static void print_usage(int argc, char **argv)
{
char *name = NULL;
name = strrchr(argv[0], '/');
printf("Usage: %s COMMAND [OPTIONS]\n", (name ? name + 1: argv[0]));
printf("Activate or deactivate a device.\n\n");
printf("Where COMMAND is one of:\n");
printf(" activate\t\tattempt to activate the device\n");
printf(" deactivate\t\tdeactivate the device\n");
printf("\nThe following OPTIONS are accepted:\n");
printf(" -d, --debug\t\tenable communication debugging\n");
printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
printf(" -s, --service URL\tuse activation webservice at URL instead of default\n");
printf(" -v, --version\t\tprint version information and exit\n");
printf(" -h, --help\t\tprints usage information\n");
printf("\n");
printf("Homepage: <http://libimobiledevice.org>\n");
}
int main(int argc, char *argv[])
{
idevice_t device = NULL;
idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
lockdownd_client_t lockdown = NULL;
idevice_activation_request_t request = NULL;
idevice_activation_response_t response = NULL;
const char* response_title = NULL;
const char* response_description = NULL;
char* field_key = NULL;
char* field_label = NULL;
char input[1024];
plist_t fields = NULL;
plist_dict_iter iter = NULL;
plist_t record = NULL;
char *udid = NULL;
char *signing_service_url = NULL;
int i;
int result = EXIT_FAILURE;
typedef enum {
OP_NONE = 0, OP_ACTIVATE, OP_DEACTIVATE
} op_t;
op_t op = OP_NONE;
/* parse cmdline args */
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
idevice_set_debug_level(1);
idevice_activation_set_debug_level(1);
continue;
}
else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
i++;
if (!argv[i] || (strlen(argv[i]) != 40)) {
print_usage(argc, argv);
return EXIT_FAILURE;
}
udid = argv[i];
continue;
}
else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--service")) {
i++;
if (!argv[i]) {
print_usage(argc, argv);
return EXIT_FAILURE;
}
signing_service_url = argv[i];
continue;
}
else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
print_usage(argc, argv);
return EXIT_SUCCESS;
}
else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
printf("ideviceactivation %s\n", PACKAGE_VERSION);
return EXIT_SUCCESS;
}
else if (!strcmp(argv[i], "activate")) {
op = OP_ACTIVATE;
continue;
}
else if (!strcmp(argv[i], "deactivate")) {
op = OP_DEACTIVATE;
continue;
}
else {
print_usage(argc, argv);
return EXIT_SUCCESS;
}
}
if (op == OP_NONE) {
print_usage(argc, argv);
return EXIT_FAILURE;
}
if (udid) {
ret = idevice_new(&device, udid);
if (ret != IDEVICE_E_SUCCESS) {
printf("No device found with UDID %s, is it plugged in?\n", udid);
return EXIT_FAILURE;
}
}
else
{
ret = idevice_new(&device, NULL);
if (ret != IDEVICE_E_SUCCESS) {
printf("No device found, is it plugged in?\n");
return EXIT_FAILURE;
}
}
if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lockdown, "ideviceactivation")) {
result = EXIT_FAILURE;
goto cleanup;
}
switch (op) {
case OP_DEACTIVATE:
// deactivate device using lockdown
if (LOCKDOWN_E_SUCCESS != lockdownd_deactivate(lockdown)) {
fprintf(stderr, "Failed to deactivate device.\n");
result = EXIT_FAILURE;
goto cleanup;
}
result = EXIT_SUCCESS;
printf("Successfully deactivated device.\n");
break;
case OP_ACTIVATE:
default:
// create activation request
if (idevice_activation_request_new_from_lockdownd(
IDEVICE_ACTIVATION_CLIENT_MOBILE_ACTIVATION, lockdown, &request) != IDEVICE_ACTIVATION_E_SUCCESS) {
fprintf(stderr, "Failed to create activation request.\n");
result = EXIT_FAILURE;
goto cleanup;
}
if (request && signing_service_url) {
idevice_activation_request_set_url(request, signing_service_url);
}
while(1) {
if (idevice_activation_send_request(request, &response) != IDEVICE_ACTIVATION_E_SUCCESS) {
fprintf(stderr, "Failed to send request or retrieve response.\n");
// Here response might have some content that could't be correctly interpreted (parsed)
// by the library. Printing out the content could help to identify the cause of the error.
result = EXIT_FAILURE;
goto cleanup;
}
if (idevice_activation_response_is_activation_acknowledged(response)) {
printf("Activation server reports that device is already activated.\n");
result = EXIT_SUCCESS;
goto cleanup;
}
if (idevice_activation_response_has_errors(response)) {
fprintf(stderr, "Activation server repors errors.\n");
idevice_activation_response_get_title(response, &response_title);
if (response_title) {
fprintf(stderr, "\t%s\n", response_title);
}
idevice_activation_response_get_description(response, &response_description);
if (response_description) {
fprintf(stderr, "\t%s\n", response_description);
}
result = EXIT_FAILURE;
goto cleanup;
}
idevice_activation_response_get_activation_record(response, &record);
if (record) {
// activate device using lockdown
if (LOCKDOWN_E_SUCCESS != lockdownd_activate(lockdown, record)) {
fprintf(stderr, "Failed to activate device with record.\n");
result = EXIT_FAILURE;
goto cleanup;
}
// set ActivationStateAcknowledged if we succeeded
if (LOCKDOWN_E_SUCCESS != lockdownd_set_value(lockdown, NULL, "ActivationStateAcknowledged", plist_new_bool(1))) {
fprintf(stderr, "Failed to set ActivationStateAcknowledged on device.\n");
result = EXIT_FAILURE;
goto cleanup;
}
break;
} else {
idevice_activation_response_get_title(response, &response_title);
if (response_title) {
fprintf(stderr, "Server reports:\n%s\n", response_title);
}
idevice_activation_response_get_description(response, &response_description);
if (response_description) {
fprintf(stderr, "Server reports:\n%s\n", response_description);
}
idevice_activation_response_get_fields(response, &fields);
if (!fields || plist_dict_get_size(fields) == 0) {
// we have no activation record, no reported erros, no acknowledgment and no fields to send
fprintf(stderr, "Unknown error.\n");
result = EXIT_FAILURE;
goto cleanup;
}
plist_dict_new_iter(fields, &iter);
if (!iter) {
fprintf(stderr, "Unknown error.\n");
result = EXIT_FAILURE;
goto cleanup;
}
idevice_activation_request_free(request);
request = NULL;
if (idevice_activation_request_new(
IDEVICE_ACTIVATION_CLIENT_MOBILE_ACTIVATION, &request) != IDEVICE_ACTIVATION_E_SUCCESS) {
fprintf(stderr, "Could not create new request.\n");
result = EXIT_FAILURE;
goto cleanup;
}
idevice_activation_request_set_fields_from_response(request, response);
do {
field_key = NULL;
plist_dict_next_item(fields, iter, &field_key, NULL);
if (field_key) {
if (idevice_activation_response_field_requires_input(response, field_key)) {
idevice_activation_response_get_label(response, field_key, &field_label);
printf("input %s: ", field_label ? field_label : field_key);
fflush(stdin);
scanf("%1023s", input);
idevice_activation_request_set_field(request, field_key, input);
if (field_label) {
free(field_label);
field_label = NULL;
}
}
}
} while(field_key);
free(iter);
iter = NULL;
idevice_activation_response_free(response);
response = NULL;
}
}
result = EXIT_SUCCESS;
printf("Successfully activated device.\n");
break;
}
cleanup:
if (request)
idevice_activation_request_free(request);
if (response)
idevice_activation_response_free(response);
if (fields)
plist_free(fields);
if (field_label)
free(field_label);
if (iter)
free(iter);
if (record)
plist_free(record);
if (lockdown)
lockdownd_client_free(lockdown);
if (device)
idevice_free(device);
return result;
}
|