summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-08-19 23:29:43 +0200
committerGravatar Jonathan Beck2008-08-31 19:27:20 +0200
commit21d5d4ac4b4bd419e0d8752147464984497c98ec (patch)
tree7edcb52dff793f5728653cf42d3b1854f3e2c181
parent89050631439f71ad652e68b59020f8801e100e45 (diff)
downloadlibimobiledevice-21d5d4ac4b4bd419e0d8752147464984497c98ec.tar.gz
libimobiledevice-21d5d4ac4b4bd419e0d8752147464984497c98ec.tar.bz2
migrate lockdown.c
-rw-r--r--src/lockdown.c111
-rw-r--r--src/lockdown.h23
2 files changed, 69 insertions, 65 deletions
diff --git a/src/lockdown.c b/src/lockdown.c
index 7cee12e..bc70d53 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -74,9 +74,9 @@ char *lockdownd_generate_hostid() {
74 * 74 *
75 * @return The lockdownd client. 75 * @return The lockdownd client.
76 */ 76 */
77lockdownd_client *new_lockdownd_client(iPhone *phone) { 77iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) {
78 if (!phone) return NULL; 78 if (!phone) return NULL;
79 lockdownd_client *control = (lockdownd_client*)malloc(sizeof(lockdownd_client)); 79 iphone_lckd_client_t control = (iphone_lckd_client_t)malloc(sizeof(struct iphone_lckd_client_int));
80 control->connection = mux_connect(phone, 0x0a00, 0xf27e); 80 control->connection = mux_connect(phone, 0x0a00, 0xf27e);
81 if (!control->connection) { 81 if (!control->connection) {
82 free(control); 82 free(control);
@@ -94,15 +94,15 @@ lockdownd_client *new_lockdownd_client(iPhone *phone) {
94 * 94 *
95 * @param control The lockdown client 95 * @param control The lockdown client
96 */ 96 */
97void lockdownd_close(lockdownd_client *control) { 97void iphone_lckd_free_client( iphone_lckd_client_t client ) {
98 if (!control) return; 98 if (!client) return;
99 if (control->connection) { 99 if (client->connection) {
100 mux_close_connection(control->connection); 100 mux_close_connection(client->connection);
101 } 101 }
102 102
103 if (control->ssl_session) gnutls_deinit(*control->ssl_session); 103 if (client->ssl_session) gnutls_deinit(*control->ssl_session);
104 free(control->ssl_session); 104 free(client->ssl_session);
105 free(control); 105 free(client);
106} 106}
107 107
108/** Polls the iPhone for lockdownd data. 108/** Polls the iPhone for lockdownd data.
@@ -113,18 +113,18 @@ void lockdownd_close(lockdownd_client *control) {
113 * 113 *
114 * @return The number of bytes received 114 * @return The number of bytes received
115 */ 115 */
116int lockdownd_recv(lockdownd_client *control, char **dump_data) { 116int iphone_lckd_recv ( iphone_lckd_client_t client, char **dump_data ) {
117 if (!control) return 0; 117 if (!client) return 0;
118 char *receive; 118 char *receive;
119 uint32 datalen = 0, bytes = 0; 119 uint32 datalen = 0, bytes = 0;
120 120
121 if (!control->in_SSL) bytes = mux_recv(control->connection, (char *)&datalen, sizeof(datalen)); 121 if (!client->in_SSL) bytes = mux_recv(client->connection, (char *)&datalen, sizeof(datalen));
122 else bytes = gnutls_record_recv(*control->ssl_session, &datalen, sizeof(datalen)); 122 else bytes = gnutls_record_recv(*client->ssl_session, &datalen, sizeof(datalen));
123 datalen = ntohl(datalen); 123 datalen = ntohl(datalen);
124 124
125 receive = (char*)malloc(sizeof(char) * datalen); 125 receive = (char*)malloc(sizeof(char) * datalen);
126 if (!control->in_SSL) bytes = mux_recv(control->connection, receive, datalen); 126 if (!client->in_SSL) bytes = mux_recv(client->connection, receive, datalen);
127 else bytes = gnutls_record_recv(*control->ssl_session, receive, datalen); 127 else bytes = gnutls_record_recv(*client->ssl_session, receive, datalen);
128 *dump_data = receive; 128 *dump_data = receive;
129 return bytes; 129 return bytes;
130} 130}
@@ -140,8 +140,8 @@ int lockdownd_recv(lockdownd_client *control, char **dump_data) {
140 * 140 *
141 * @return The number of bytes sent 141 * @return The number of bytes sent
142 */ 142 */
143int lockdownd_send(lockdownd_client *control, char *raw_data, uint32 length) { 143int iphone_lckd_send ( iphone_lckd_client_t client, char *raw_data, uint32_t length ) {
144 if (!control) return 0; 144 if (!client) return 0;
145 char *real_query; 145 char *real_query;
146 int bytes; 146 int bytes;
147 147
@@ -157,8 +157,8 @@ int lockdownd_send(lockdownd_client *control, char *raw_data, uint32 length) {
157 packet = NULL; 157 packet = NULL;
158 } 158 }
159 159
160 if (!control->in_SSL) bytes = mux_send(control->connection, real_query, ntohl(length)+sizeof(length)); 160 if (!client->in_SSL) bytes = mux_send(client->connection, real_query, ntohl(length)+sizeof(length));
161 else gnutls_record_send(*control->ssl_session, real_query, ntohl(length)+sizeof(length)); 161 else gnutls_record_send(*client->ssl_session, real_query, ntohl(length)+sizeof(length));
162 if (debug) printf("lockdownd_send(): sent it!\n"); 162 if (debug) printf("lockdownd_send(): sent it!\n");
163 free(real_query); 163 free(real_query);
164 return bytes; 164 return bytes;
@@ -172,7 +172,7 @@ int lockdownd_send(lockdownd_client *control, char *raw_data, uint32 length) {
172 * 172 *
173 * @return 1 on success and 0 on failure. 173 * @return 1 on success and 0 on failure.
174 */ 174 */
175int lockdownd_hello(lockdownd_client *control) { 175int lockdownd_hello(iphone_lckd_client_t control) {
176 if (!control) return 0; 176 if (!control) return 0;
177 xmlDocPtr plist = new_plist(); 177 xmlDocPtr plist = new_plist();
178 xmlNode *dict, *key; 178 xmlNode *dict, *key;
@@ -223,7 +223,7 @@ int lockdownd_hello(lockdownd_client *control) {
223 * 223 *
224 * @return 1 on success and 0 on failure. 224 * @return 1 on success and 0 on failure.
225 */ 225 */
226int lockdownd_generic_get_value(lockdownd_client *control, char *req_key, char **value) 226int lockdownd_generic_get_value(iphone_lckd_client_t control, char *req_key, char **value)
227{ 227{
228 xmlDocPtr plist = new_plist(); 228 xmlDocPtr plist = new_plist();
229 xmlNode *dict = NULL; 229 xmlNode *dict = NULL;
@@ -284,7 +284,7 @@ int lockdownd_generic_get_value(lockdownd_client *control, char *req_key, char *
284 * 284 *
285 * @return 1 on success and 0 on failure. 285 * @return 1 on success and 0 on failure.
286 */ 286 */
287int lockdownd_get_device_uid(lockdownd_client *control, char **uid) 287int lockdownd_get_device_uid(lockdownd_client_t control, char **uid)
288{ 288{
289 return lockdownd_generic_get_value(control, "UniqueDeviceID", uid); 289 return lockdownd_generic_get_value(control, "UniqueDeviceID", uid);
290} 290}
@@ -295,7 +295,7 @@ int lockdownd_get_device_uid(lockdownd_client *control, char **uid)
295 * 295 *
296 * @return 1 on success and 0 on failure. 296 * @return 1 on success and 0 on failure.
297 */ 297 */
298int lockdownd_get_device_public_key(lockdownd_client *control, char **public_key) 298int lockdownd_get_device_public_key(lockdownd_client_t control, char **public_key)
299{ 299{
300 return lockdownd_generic_get_value(control, "DevicePublicKey", public_key); 300 return lockdownd_generic_get_value(control, "DevicePublicKey", public_key);
301} 301}
@@ -307,42 +307,45 @@ int lockdownd_get_device_public_key(lockdownd_client *control, char **public_key
307 * 307 *
308 * @return 1 on success and 0 on failure 308 * @return 1 on success and 0 on failure
309 */ 309 */
310int lockdownd_init(iPhone_t phone, lockdownd_client_t *control) 310int iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_client_t *client )
311{ 311{
312 int ret = 0; 312 if (!device || !client || (client && *client) )
313 return IPHONE_E_INVALID_ARG;
314 int ret = IPHONE_E_SUCCESS;
313 char *host_id = NULL; 315 char *host_id = NULL;
314 316
315 if (!phone) 317 iphone_lckd_client_t client_loc = new_lockdownd_client( device );
316 return 0; 318 if (!lockdownd_hello(client_loc)){
317
318 lockdownd_client *control_loc = new_lockdownd_client( (iPhone*)phone );
319 if (!lockdownd_hello(control_loc)){
320 fprintf(stderr, "Hello failed in the lockdownd client.\n"); 319 fprintf(stderr, "Hello failed in the lockdownd client.\n");
320 ret = IPHONE_E_NOT_ENOUGH_DATA;
321 } 321 }
322 322
323
323 char *uid = NULL; 324 char *uid = NULL;
324 if(!lockdownd_get_device_uid(control_loc, &uid)){ 325 if(IPHONE_E_SUCCESS == ret && !lockdownd_get_device_uid(control_loc, &uid)){
325 fprintf(stderr, "Device refused to send public key.\n"); 326 fprintf(stderr, "Device refused to send uid.\n");
327 ret = IPHONE_E_NOT_ENOUGH_DATA;
326 } 328 }
327 329
328 host_id = get_host_id(); 330 host_id = get_host_id();
331 if (IPHONE_E_SUCCESS == ret && !host_id){
332 fprintf(stderr, "No HostID found, run libiphone-initconf.\n");
333 ret = IPHONE_E_INVALID_CONF;
334 }
329 335
330 if (!is_device_known(uid)) 336 if (IPHONE_E_SUCCESS == ret && !is_device_known(uid))
331 ret = lockdownd_pair_device(*control, uid, host_id); 337 ret = lockdownd_pair_device(*control, uid, host_id);
332 else
333 ret = 1;
334 338
335 if (uid) { 339 if (uid) {
336 free(uid); 340 free(uid);
337 uid = NULL; 341 uid = NULL;
338 } 342 }
339
340 343
341 if (ret && host_id && lockdownd_start_SSL_session(control_loc, host_id)) { 344 if (IPHONE_E_SUCCESS == ret && !lockdownd_start_SSL_session(client_loc, host_id)) {
342 ret = 1; 345 ret = IPHONE_E_SUCCESS;
343 } else { 346 } else {
344 ret = 0; 347 ret = IPHONE_E_SSL_ERROR;
345 fprintf(stderr, "lockdownd_init: SSL Session opening failed, has libiphone-initconf been run?\n"); 348 fprintf(stderr, "SSL Session opening failed.\n");
346 } 349 }
347 350
348 if (host_id) { 351 if (host_id) {
@@ -350,8 +353,8 @@ int lockdownd_init(iPhone_t phone, lockdownd_client_t *control)
350 host_id = NULL; 353 host_id = NULL;
351 } 354 }
352 355
353 *control = (lockdownd_client_t)control_loc; 356 if (IPHONE_E_SUCCESS == ret)
354 357 *client = client_loc;
355 return ret; 358 return ret;
356} 359}
357 360
@@ -362,7 +365,7 @@ int lockdownd_init(iPhone_t phone, lockdownd_client_t *control)
362 * 365 *
363 * @return 1 on success and 0 on failure 366 * @return 1 on success and 0 on failure
364 */ 367 */
365int lockdownd_pair_device(lockdownd_client *control, char *uid, char *host_id) 368int lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id)
366{ 369{
367 int ret = 0; 370 int ret = 0;
368 xmlDocPtr plist = new_plist(); 371 xmlDocPtr plist = new_plist();
@@ -596,7 +599,7 @@ int lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char *
596 * 599 *
597 * @return 1 on success and 0 on failure 600 * @return 1 on success and 0 on failure
598 */ 601 */
599int lockdownd_start_SSL_session(lockdownd_client *control, const char *HostID) { 602int lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) {
600 xmlDocPtr plist = new_plist(); 603 xmlDocPtr plist = new_plist();
601 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 604 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0);
602 xmlNode *key; 605 xmlNode *key;
@@ -714,8 +717,8 @@ int lockdownd_start_SSL_session(lockdownd_client *control, const char *HostID) {
714 */ 717 */
715ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length) { 718ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length) {
716 int bytes = 0; 719 int bytes = 0;
717 lockdownd_client *control; 720 iphone_lckd_client_t control;
718 control = (lockdownd_client*)transport; 721 control = (iphone_lckd_client_t)transport;
719 if (debug) printf("lockdownd_secuwrite() called\n"); 722 if (debug) printf("lockdownd_secuwrite() called\n");
720 if (debug) printf("pre-send\nlength = %zi\n", length); 723 if (debug) printf("pre-send\nlength = %zi\n", length);
721 bytes = mux_send(control->connection, buffer, length); 724 bytes = mux_send(control->connection, buffer, length);
@@ -742,8 +745,8 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size
742ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length) { 745ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length) {
743 int bytes = 0, pos_start_fill = 0; 746 int bytes = 0, pos_start_fill = 0;
744 char *hackhackhack = NULL; 747 char *hackhackhack = NULL;
745 lockdownd_client *control; 748 iphone_lckd_client_t control;
746 control = (lockdownd_client*)transport; 749 control = (iphone_lckd_client_t)transport;
747 if (debug) printf("lockdownd_securead() called\nlength = %zi\n", length); 750 if (debug) printf("lockdownd_securead() called\nlength = %zi\n", length);
748 // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. 751 // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more.
749 if (control->gtls_buffer_hack_len > 0) { 752 if (control->gtls_buffer_hack_len > 0) {
@@ -812,11 +815,11 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
812 * 815 *
813 * @return The port number the service was started on or 0 on failure. 816 * @return The port number the service was started on or 0 on failure.
814 */ 817 */
815int lockdownd_start_service(lockdownd_client *control, const char *service) { 818int iphone_lckd_start_service ( iphone_lckd_client_t client, const char *service ) {
816 if (!control) return 0; 819 if (!client) return 0;
817 820
818 char* host_id = get_host_id(); 821 char* host_id = get_host_id();
819 if (host_id && !control->in_SSL && !lockdownd_start_SSL_session(control, host_id)) return 0; 822 if (host_id && !client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) return 0;
820 823
821 char *XML_query, **dictionary; 824 char *XML_query, **dictionary;
822 uint32 length, i = 0, port = 0; 825 uint32 length, i = 0, port = 0;
@@ -835,10 +838,10 @@ int lockdownd_start_service(lockdownd_client *control, const char *service) {
835 838
836 xmlDocDumpMemory(plist, (xmlChar **)&XML_query, &length); 839 xmlDocDumpMemory(plist, (xmlChar **)&XML_query, &length);
837 840
838 lockdownd_send(control, XML_query, length); 841 lockdownd_send(client, XML_query, length);
839 free(XML_query); 842 free(XML_query);
840 843
841 length = lockdownd_recv(control, &XML_query); 844 length = lockdownd_recv(client, &XML_query);
842 845
843 xmlFreeDoc(plist); 846 xmlFreeDoc(plist);
844 847
diff --git a/src/lockdown.h b/src/lockdown.h
index 9893e8e..1c83ab3 100644
--- a/src/lockdown.h
+++ b/src/lockdown.h
@@ -32,29 +32,30 @@
32 32
33 33
34 34
35typedef struct lockdownd_client_s { 35struct iphone_lckd_client_int {
36 usbmux_connection *connection; 36 usbmux_connection *connection;
37 gnutls_session_t *ssl_session; 37 gnutls_session_t *ssl_session;
38 int in_SSL; 38 int in_SSL;
39 char *gtls_buffer_hack; 39 char *gtls_buffer_hack;
40 int gtls_buffer_hack_len; 40 int gtls_buffer_hack_len;
41} lockdownd_client; 41};
42 42
43char *lockdownd_generate_hostid(); 43char *lockdownd_generate_hostid();
44 44
45lockdownd_client *new_lockdownd_client(iPhone *phone); 45iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone);
46int lockdownd_hello(lockdownd_client *control); 46int lockdownd_hello(iphone_lckd_client_t control);
47int lockdownd_get_device_uid(lockdownd_client *control, char **uid); 47int lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid);
48int lockdownd_get_device_public_key(lockdownd_client *control, char **public_key); 48int lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key);
49
49int lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, char **root_cert_b64); 50int lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, char **root_cert_b64);
50int lockdownd_pair_device(lockdownd_client *control, char *uid, char *host_id); 51int lockdownd_pair_device(iphone_lckd_client_t control, char *public_key, char *host_id);
51int lockdownd_recv(lockdownd_client *control, char **dump_data); 52int lockdownd_recv(iphone_lckd_client_t control, char **dump_data);
52int lockdownd_send(lockdownd_client *control, char *raw_data, uint32 length); 53int lockdownd_send(iphone_lckd_client_t control, char *raw_data, uint32 length);
53void lockdownd_close(lockdownd_client *control); 54void lockdownd_close(iphone_lckd_client_t control);
54 55
55// SSL functions 56// SSL functions
56 57
57int lockdownd_start_SSL_session(lockdownd_client *control, const char *HostID); 58int lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID);
58ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length); 59ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length);
59ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length); 60ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length);
60 61