summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am4
-rw-r--r--README21
-rwxr-xr-xautogen.sh2
-rw-r--r--configure.ac15
-rw-r--r--dev/Makefile.am12
-rw-r--r--dev/lckdclient.c1
-rw-r--r--dev/main.c8
-rwxr-xr-xdev/msync.py40
-rw-r--r--dev/msyncclient.c69
-rw-r--r--include/libiphone/libiphone.h27
-rw-r--r--m4/ac_pkg_swig.m4122
-rw-r--r--m4/ac_python_devel.m4265
-rw-r--r--m4/as-compiler-flag.m462
-rw-r--r--m4/swig_python.m465
-rw-r--r--src/AFC.c57
-rw-r--r--src/AFC.h10
-rw-r--r--src/Makefile.am14
-rw-r--r--src/MobileSync.c299
-rw-r--r--src/MobileSync.h39
-rw-r--r--src/NotificationProxy.c91
-rw-r--r--src/NotificationProxy.h1
-rw-r--r--src/initconf.c185
-rw-r--r--src/lockdown.c1007
-rw-r--r--src/lockdown.h21
-rw-r--r--src/plist.c245
-rw-r--r--src/plist.h38
-rw-r--r--src/usbmux.c3
-rw-r--r--src/usbmux.h20
-rw-r--r--src/userpref.c334
-rw-r--r--src/userpref.h50
-rw-r--r--src/utils.c25
-rw-r--r--src/utils.h4
-rw-r--r--swig/Makefile.am18
-rw-r--r--swig/__init__.py1
-rw-r--r--swig/iphone.i157
35 files changed, 2105 insertions, 1227 deletions
diff --git a/Makefile.am b/Makefile.am
index 2b9ce9a..cc6c06f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
1AUTOMAKE_OPTIONS = foreign 1AUTOMAKE_OPTIONS = foreign
2 2ACLOCAL_AMFLAGS = -I m4
3SUBDIRS = src include fdi udev $(DEV_SUB) 3SUBDIRS = src include fdi swig udev $(DEV_SUB)
4 4
5pkgconfigdir = $(libdir)/pkgconfig 5pkgconfigdir = $(libdir)/pkgconfig
6pkgconfig_DATA = libiphone-1.0.pc 6pkgconfig_DATA = libiphone-1.0.pc
diff --git a/README b/README
index e3c7053..699064d 100644
--- a/README
+++ b/README
@@ -7,10 +7,11 @@ For:
7 7
8You must have: 8You must have:
9 libgnutls-dev 9 libgnutls-dev
10 libgcrypt-dev
10 libusb-dev 11 libusb-dev
11 libfuse-dev (and the associated kernel modules) 12 libfuse-dev (and the associated kernel modules)
12 libglib2.0-dev 13 libglib2.0-dev
13 libxml2-dev 14 libplist-dev
14 make 15 make
15 autoheader 16 autoheader
16 automake 17 automake
@@ -30,24 +31,6 @@ On Ubuntu/Debian, you can do:
30 libgnutls-dev libusb-dev libfuse-dev libglib2.0-dev libxml2-dev \ 31 libgnutls-dev libusb-dev libfuse-dev libglib2.0-dev libxml2-dev \
31 libreadline5-dev 32 libreadline5-dev
32 33
33USAGE
34================================================================================
35
36Now comes the fun bit!
37
38== Generating keys ==
39
40IMPORTANT: Before using the library you must run "libiphone-initconf"
41as your own user (not root). It will generate keys and a host id for your
42system to be able to communicate with 'lockdownd' on the iPhone.
43
44It will probably take 5-10 minutes, but thankfully only needs to be
45run _once_. It MUST be run otherwise communication will not work:
46
47 libiphone-initconf
48
49The generated keys are saved in '~/.config/libiphone/' in your home directory.
50
51== Who/what/where? == 34== Who/what/where? ==
52 35
53wiki: 36wiki:
diff --git a/autogen.sh b/autogen.sh
index c17ea96..9aa7170 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -1,5 +1,5 @@
1#!/bin/sh 1#!/bin/sh
2aclocal 2aclocal -I m4
3libtoolize 3libtoolize
4autoheader 4autoheader
5automake --add-missing 5automake --add-missing
diff --git a/configure.ac b/configure.ac
index c871011..a26be74 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,24 +6,30 @@ AC_INIT(libiphone, 0.1.0, nospam@nowhere.com)
6AM_INIT_AUTOMAKE(libiphone, 0.1.0) 6AM_INIT_AUTOMAKE(libiphone, 0.1.0)
7AC_CONFIG_SRCDIR([src/]) 7AC_CONFIG_SRCDIR([src/])
8AC_CONFIG_HEADER([config.h]) 8AC_CONFIG_HEADER([config.h])
9AC_CONFIG_MACRO_DIR([m4])
9 10
10AC_PROG_LIBTOOL 11AC_PROG_LIBTOOL
11 12
13AM_PATH_PYTHON(2.3)
14AC_PROG_SWIG(1.3.21)
15SWIG_PYTHON
16
17
12# Checks for programs. 18# Checks for programs.
13AC_PROG_CC 19AC_PROG_CC
14AM_PROG_CC_C_O 20AM_PROG_CC_C_O
15 21
16# Checks for libraries. 22# Checks for libraries.
17PKG_CHECK_MODULES(libxml2, libxml-2.0 >= 2.6.30)
18PKG_CHECK_MODULES(libusb, libusb >= 0.1.12) 23PKG_CHECK_MODULES(libusb, libusb >= 0.1.12)
19PKG_CHECK_MODULES(libglib2, glib-2.0 >= 2.14.1) 24PKG_CHECK_MODULES(libglib2, glib-2.0 >= 2.14.1)
20PKG_CHECK_MODULES(libgthread2, gthread-2.0 >= 2.14.1) 25PKG_CHECK_MODULES(libgthread2, gthread-2.0 >= 2.14.1)
21PKG_CHECK_MODULES(libgnutls, gnutls >= 1.6.3 ) 26PKG_CHECK_MODULES(libgnutls, gnutls >= 1.6.3 )
22PKG_CHECK_MODULES(libtasn1, libtasn1 >= 1.1) 27PKG_CHECK_MODULES(libtasn1, libtasn1 >= 1.1)
28PKG_CHECK_MODULES(libplist, libplist >= 0.10)
23 29
24# Checks for header files. 30# Checks for header files.
25AC_HEADER_STDC 31AC_HEADER_STDC
26AC_CHECK_HEADERS([arpa/inet.h stdint.h stdlib.h string.h]) 32AC_CHECK_HEADERS([arpa/inet.h stdint.h stdlib.h string.h gcrypt.h])
27 33
28# Checks for typedefs, structures, and compiler characteristics. 34# Checks for typedefs, structures, and compiler characteristics.
29AC_C_CONST 35AC_C_CONST
@@ -59,6 +65,9 @@ if test "$no_debug_code" = true; then
59 AC_DEFINE(STRIP_DEBUG_CODE,1,[Strip debug reporting code]) 65 AC_DEFINE(STRIP_DEBUG_CODE,1,[Strip debug reporting code])
60fi 66fi
61 67
68AS_COMPILER_FLAGS(GLOBAL_CFLAGS, "-Wall -Wextra -Wmissing-declarations -Wredundant-decls -Wshadow -Wpointer-arith -Wwrite-strings -Wswitch-default -Wno-unused-parameter")
69AC_SUBST(GLOBAL_CFLAGS)
70
62# check for large file support 71# check for large file support
63AC_SYS_LARGEFILE 72AC_SYS_LARGEFILE
64LFS_CFLAGS='' 73LFS_CFLAGS=''
@@ -86,4 +95,4 @@ if test "$enable_largefile" != no; then
86fi 95fi
87AC_SUBST(LFS_CFLAGS) 96AC_SUBST(LFS_CFLAGS)
88 97
89AC_OUTPUT(Makefile src/Makefile include/Makefile fdi/Makefile udev/Makefile dev/Makefile libiphone-1.0.pc) 98AC_OUTPUT(Makefile src/Makefile include/Makefile fdi/Makefile dev/Makefile swig/Makefile udev/Makefile libiphone-1.0.pc)
diff --git a/dev/Makefile.am b/dev/Makefile.am
index 5f85ad7..f7d1109 100644
--- a/dev/Makefile.am
+++ b/dev/Makefile.am
@@ -1,9 +1,9 @@
1INCLUDES = -I$(top_srcdir)/include 1INCLUDES = -I$(top_srcdir)/include
2 2
3AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) -g $(LFS_CFLAGS) 3AM_CFLAGS = $(GLOBAL_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS)
4AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) 4AM_LDFLAGS = $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS)
5 5
6bin_PROGRAMS = iphoneclient lckd-client afccheck 6bin_PROGRAMS = iphoneclient lckd-client afccheck msyncclient
7 7
8iphoneclient_SOURCES = main.c 8iphoneclient_SOURCES = main.c
9iphoneclient_LDADD = ../src/libiphone.la 9iphoneclient_LDADD = ../src/libiphone.la
@@ -17,3 +17,9 @@ afccheck_SOURCES = afccheck.c
17afccheck_CFLAGS = $(AM_CFLAGS) 17afccheck_CFLAGS = $(AM_CFLAGS)
18afccheck_LDFLAGS = $(AM_LDFLAGS) 18afccheck_LDFLAGS = $(AM_LDFLAGS)
19afccheck_LDADD = ../src/libiphone.la 19afccheck_LDADD = ../src/libiphone.la
20
21msyncclient_SOURCES = msyncclient.c
22msyncclient_CFLAGS = $(AM_CFLAGS)
23msyncclient_LDFLAGS = $(AM_LDFLAGS)
24msyncclient_LDADD = ../src/libiphone.la
25
diff --git a/dev/lckdclient.c b/dev/lckdclient.c
index 96bc27d..c96f052 100644
--- a/dev/lckdclient.c
+++ b/dev/lckdclient.c
@@ -20,6 +20,7 @@
20 */ 20 */
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h>
23#include <string.h> 24#include <string.h>
24#include <glib.h> 25#include <glib.h>
25#include <readline/readline.h> 26#include <readline/readline.h>
diff --git a/dev/main.c b/dev/main.c
index c68427b..babcf67 100644
--- a/dev/main.c
+++ b/dev/main.c
@@ -24,10 +24,8 @@
24#include <errno.h> 24#include <errno.h>
25#include <usb.h> 25#include <usb.h>
26 26
27#include <libxml/parser.h>
28#include <libxml/tree.h>
29
30#include <libiphone/libiphone.h> 27#include <libiphone/libiphone.h>
28#include "../src/utils.h"
31 29
32void perform_syncWillStart(iphone_device_t phone, iphone_lckd_client_t control) 30void perform_syncWillStart(iphone_device_t phone, iphone_lckd_client_t control)
33{ 31{
@@ -77,8 +75,10 @@ int main(int argc, char *argv[])
77 75
78 if (argc > 1 && !strcasecmp(argv[1], "--debug")) { 76 if (argc > 1 && !strcasecmp(argv[1], "--debug")) {
79 iphone_set_debug(1); 77 iphone_set_debug(1);
78 iphone_set_debug_mask(DBGMASK_ALL);
80 } else { 79 } else {
81 iphone_set_debug(0); 80 iphone_set_debug(0);
81 iphone_set_debug_mask(DBGMASK_NONE);
82 } 82 }
83 83
84 if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) { 84 if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
@@ -136,7 +136,7 @@ int main(int argc, char *argv[])
136 iphone_afc_get_file_attr(afc, "/iTunesOnTheGoPlaylist.plist", &stbuf); 136 iphone_afc_get_file_attr(afc, "/iTunesOnTheGoPlaylist.plist", &stbuf);
137 if (IPHONE_E_SUCCESS == 137 if (IPHONE_E_SUCCESS ==
138 iphone_afc_open_file(afc, "/iTunesOnTheGoPlaylist.plist", IPHONE_AFC_FILE_READ, &my_file) && my_file) { 138 iphone_afc_open_file(afc, "/iTunesOnTheGoPlaylist.plist", IPHONE_AFC_FILE_READ, &my_file) && my_file) {
139 printf("A file size: %i\n", stbuf.st_size); 139 printf("A file size: %i\n", (int) stbuf.st_size);
140 char *file_data = (char *) malloc(sizeof(char) * stbuf.st_size); 140 char *file_data = (char *) malloc(sizeof(char) * stbuf.st_size);
141 iphone_afc_read_file(afc, my_file, file_data, stbuf.st_size, &bytes); 141 iphone_afc_read_file(afc, my_file, file_data, stbuf.st_size, &bytes);
142 if (bytes >= 0) { 142 if (bytes >= 0) {
diff --git a/dev/msync.py b/dev/msync.py
new file mode 100755
index 0000000..4170f87
--- /dev/null
+++ b/dev/msync.py
@@ -0,0 +1,40 @@
1#! /usr/bin/env python
2
3from libiphone.iPhone import *
4
5# get msync client
6def GetMobileSyncClient() :
7 phone = iPhone()
8 if not phone.InitDevice() :
9 print "Couldn't find device, is it connected ?\n"
10 return None
11 lckd = phone.GetLockdownClient()
12 if not lckd :
13 print "Failed to start lockdown service.\n"
14 return None
15 msync = lckd.GetMobileSyncClient()
16 if not msync :
17 print "Failed to start mobilesync service.\n"
18 return None
19 return msync
20
21
22msync = GetMobileSyncClient()
23
24if not msync :
25 exit(1)
26
27array = PListNode(PLIST_ARRAY)
28array.AddSubString("SDMessageSyncDataClassWithDevice")
29array.AddSubString("com.apple.Contacts");
30array.AddSubString("---");
31array.AddSubString("2009-01-13 22:25:58 +0100");
32array.AddSubUInt(106);
33array.AddSubString("___EmptyParameterString___");
34
35msync.Send(array)
36array = msync.Receive()
37print array.ToXml()
38
39
40
diff --git a/dev/msyncclient.c b/dev/msyncclient.c
new file mode 100644
index 0000000..804e1ed
--- /dev/null
+++ b/dev/msyncclient.c
@@ -0,0 +1,69 @@
1/*
2 * msyncclient.c
3 * Rudimentary interface to the MobileSync iPhone
4 *
5 * Copyright (c) 2009 Jonathan Beck 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 <errno.h>
25#include <usb.h>
26
27#include <libiphone/libiphone.h>
28
29
30int main(int argc, char *argv[])
31{
32 int bytes = 0, port = 0, i = 0;
33 iphone_lckd_client_t control = NULL;
34 iphone_device_t phone = NULL;
35
36 if (argc > 1 && !strcasecmp(argv[1], "--debug"))
37 iphone_set_debug_mask(DBGMASK_MOBILESYNC);
38
39
40 if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
41 printf("No iPhone found, is it plugged in?\n");
42 return -1;
43 }
44
45 if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
46 iphone_free_device(phone);
47 return -1;
48 }
49
50 iphone_lckd_start_service(control, "com.apple.mobilesync", &port);
51
52 if (port) {
53 iphone_msync_client_t msync = NULL;
54 iphone_msync_new_client(phone, 3432, port, &msync);
55 if (msync) {
56 iphone_msync_get_all_contacts(msync);
57 iphone_msync_free_client(msync);
58 }
59 } else {
60 printf("Start service failure.\n");
61 }
62
63 printf("All done.\n");
64
65 iphone_lckd_free_client(control);
66 iphone_free_device(phone);
67
68 return 0;
69}
diff --git a/include/libiphone/libiphone.h b/include/libiphone/libiphone.h
index 53be73f..0d5257a 100644
--- a/include/libiphone/libiphone.h
+++ b/include/libiphone/libiphone.h
@@ -29,6 +29,7 @@ extern "C" {
29#include <stdint.h> 29#include <stdint.h>
30#include <sys/types.h> 30#include <sys/types.h>
31#include <sys/stat.h> 31#include <sys/stat.h>
32#include <plist/plist.h>
32 33
33//general errors 34//general errors
34#define IPHONE_E_SUCCESS 0 35#define IPHONE_E_SUCCESS 0
@@ -77,11 +78,23 @@ typedef struct iphone_afc_client_int *iphone_afc_client_t;
77struct iphone_afc_file_int; 78struct iphone_afc_file_int;
78typedef struct iphone_afc_file_int *iphone_afc_file_t; 79typedef struct iphone_afc_file_int *iphone_afc_file_t;
79 80
81struct iphone_msync_client_int;
82typedef struct iphone_msync_client_int *iphone_msync_client_t;
83
80struct iphone_np_client_int; 84struct iphone_np_client_int;
81typedef struct iphone_np_client_int *iphone_np_client_t; 85typedef struct iphone_np_client_int *iphone_np_client_t;
82 86
83//device related functions 87//debug related functions
88#define DBGMASK_ALL 0xFFFF
89#define DBGMASK_NONE 0x0000
90#define DBGMASK_USBMUX (1 << 1)
91#define DBGMASK_LOCKDOWND (1 << 2)
92#define DBGMASK_MOBILESYNC (1 << 3)
93
94void iphone_set_debug_mask(uint16_t mask);
84void iphone_set_debug(int level); 95void iphone_set_debug(int level);
96
97//device related functions
85iphone_error_t iphone_get_device ( iphone_device_t *device ); 98iphone_error_t iphone_get_device ( iphone_device_t *device );
86iphone_error_t iphone_get_specific_device( unsigned int bus_n, int dev_n, iphone_device_t * device ); 99iphone_error_t iphone_get_specific_device( unsigned int bus_n, int dev_n, iphone_device_t * device );
87iphone_error_t iphone_free_device ( iphone_device_t device ); 100iphone_error_t iphone_free_device ( iphone_device_t device );
@@ -93,8 +106,8 @@ iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_clie
93iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client ); 106iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client );
94 107
95iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const char *service, int *port ); 108iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const char *service, int *port );
96iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, char **dump_data, uint32_t *recv_bytes ); 109iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, plist_t* plist);
97iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t *recv_bytes ); 110iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, plist_t plist);
98 111
99 112
100//usbmux related functions 113//usbmux related functions
@@ -126,6 +139,14 @@ iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir);
126iphone_error_t iphone_afc_truncate(iphone_afc_client_t client, const char *path, off_t newsize); 139iphone_error_t iphone_afc_truncate(iphone_afc_client_t client, const char *path, off_t newsize);
127 140
128 141
142
143iphone_error_t iphone_msync_new_client(iphone_device_t device, int src_port, int dst_port,
144 iphone_msync_client_t * client);
145iphone_error_t iphone_msync_free_client(iphone_msync_client_t client);
146
147iphone_error_t iphone_msync_recv(iphone_msync_client_t client, plist_t * plist);
148iphone_error_t iphone_msync_send(iphone_msync_client_t client, plist_t plist);
149
129#ifdef __cplusplus 150#ifdef __cplusplus
130} 151}
131#endif 152#endif
diff --git a/m4/ac_pkg_swig.m4 b/m4/ac_pkg_swig.m4
new file mode 100644
index 0000000..738f69d
--- /dev/null
+++ b/m4/ac_pkg_swig.m4
@@ -0,0 +1,122 @@
1# ===========================================================================
2# http://autoconf-archive.cryp.to/ac_pkg_swig.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7# AC_PROG_SWIG([major.minor.micro])
8#
9# DESCRIPTION
10#
11# This macro searches for a SWIG installation on your system. If found you
12# should call SWIG via $(SWIG). You can use the optional first argument to
13# check if the version of the available SWIG is greater than or equal to
14# the value of the argument. It should have the format: N[.N[.N]] (N is a
15# number between 0 and 999. Only the first N is mandatory.)
16#
17# If the version argument is given (e.g. 1.3.17), AC_PROG_SWIG checks that
18# the swig package is this version number or higher.
19#
20# In configure.in, use as:
21#
22# AC_PROG_SWIG(1.3.17)
23# SWIG_ENABLE_CXX
24# SWIG_MULTI_MODULE_SUPPORT
25# SWIG_PYTHON
26#
27# LAST MODIFICATION
28#
29# 2008-04-12
30#
31# COPYLEFT
32#
33# Copyright (c) 2008 Sebastian Huber <sebastian-huber@web.de>
34# Copyright (c) 2008 Alan W. Irwin <irwin@beluga.phys.uvic.ca>
35# Copyright (c) 2008 Rafael Laboissiere <rafael@laboissiere.net>
36# Copyright (c) 2008 Andrew Collier <colliera@ukzn.ac.za>
37#
38# This program is free software; you can redistribute it and/or modify it
39# under the terms of the GNU General Public License as published by the
40# Free Software Foundation; either version 2 of the License, or (at your
41# option) any later version.
42#
43# This program is distributed in the hope that it will be useful, but
44# WITHOUT ANY WARRANTY; without even the implied warranty of
45# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
46# Public License for more details.
47#
48# You should have received a copy of the GNU General Public License along
49# with this program. If not, see <http://www.gnu.org/licenses/>.
50#
51# As a special exception, the respective Autoconf Macro's copyright owner
52# gives unlimited permission to copy, distribute and modify the configure
53# scripts that are the output of Autoconf when processing the Macro. You
54# need not follow the terms of the GNU General Public License when using
55# or distributing such scripts, even though portions of the text of the
56# Macro appear in them. The GNU General Public License (GPL) does govern
57# all other use of the material that constitutes the Autoconf Macro.
58#
59# This special exception to the GPL applies to versions of the Autoconf
60# Macro released by the Autoconf Macro Archive. When you make and
61# distribute a modified version of the Autoconf Macro, you may extend this
62# special exception to the GPL to apply to your modified version as well.
63
64AC_DEFUN([AC_PROG_SWIG],[
65 AC_PATH_PROG([SWIG],[swig])
66 if test -z "$SWIG" ; then
67 AC_MSG_WARN([cannot find 'swig' program. You should look at http://www.swig.org])
68 SWIG='echo "Error: SWIG is not installed. You should look at http://www.swig.org" ; false'
69 elif test -n "$1" ; then
70 AC_MSG_CHECKING([for SWIG version])
71 [swig_version=`$SWIG -version 2>&1 | grep 'SWIG Version' | sed 's/.*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/g'`]
72 AC_MSG_RESULT([$swig_version])
73 if test -n "$swig_version" ; then
74 # Calculate the required version number components
75 [required=$1]
76 [required_major=`echo $required | sed 's/[^0-9].*//'`]
77 if test -z "$required_major" ; then
78 [required_major=0]
79 fi
80 [required=`echo $required | sed 's/[0-9]*[^0-9]//'`]
81 [required_minor=`echo $required | sed 's/[^0-9].*//'`]
82 if test -z "$required_minor" ; then
83 [required_minor=0]
84 fi
85 [required=`echo $required | sed 's/[0-9]*[^0-9]//'`]
86 [required_patch=`echo $required | sed 's/[^0-9].*//'`]
87 if test -z "$required_patch" ; then
88 [required_patch=0]
89 fi
90 # Calculate the available version number components
91 [available=$swig_version]
92 [available_major=`echo $available | sed 's/[^0-9].*//'`]
93 if test -z "$available_major" ; then
94 [available_major=0]
95 fi
96 [available=`echo $available | sed 's/[0-9]*[^0-9]//'`]
97 [available_minor=`echo $available | sed 's/[^0-9].*//'`]
98 if test -z "$available_minor" ; then
99 [available_minor=0]
100 fi
101 [available=`echo $available | sed 's/[0-9]*[^0-9]//'`]
102 [available_patch=`echo $available | sed 's/[^0-9].*//'`]
103 if test -z "$available_patch" ; then
104 [available_patch=0]
105 fi
106 if test $available_major -ne $required_major \
107 -o $available_minor -ne $required_minor \
108 -o $available_patch -lt $required_patch ; then
109 AC_MSG_WARN([SWIG version >= $1 is required. You have $swig_version. You should look at http://www.swig.org])
110 SWIG='echo "Error: SWIG version >= $1 is required. You have '"$swig_version"'. You should look at http://www.swig.org" ; false'
111 else
112 AC_MSG_NOTICE([SWIG executable is '$SWIG'])
113 SWIG_LIB=`$SWIG -swiglib`
114 AC_MSG_NOTICE([SWIG library directory is '$SWIG_LIB'])
115 fi
116 else
117 AC_MSG_WARN([cannot determine SWIG version])
118 SWIG='echo "Error: Cannot determine SWIG version. You should look at http://www.swig.org" ; false'
119 fi
120 fi
121 AC_SUBST([SWIG_LIB])
122])
diff --git a/m4/ac_python_devel.m4 b/m4/ac_python_devel.m4
new file mode 100644
index 0000000..7cec10f
--- /dev/null
+++ b/m4/ac_python_devel.m4
@@ -0,0 +1,265 @@
1# ===========================================================================
2# http://autoconf-archive.cryp.to/ac_python_devel.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7# AC_PYTHON_DEVEL([version])
8#
9# DESCRIPTION
10#
11# Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
12# in your configure.ac.
13#
14# This macro checks for Python and tries to get the include path to
15# 'Python.h'. It provides the $(PYTHON_CPPFLAGS) and $(PYTHON_LDFLAGS)
16# output variables. It also exports $(PYTHON_EXTRA_LIBS) and
17# $(PYTHON_EXTRA_LDFLAGS) for embedding Python in your code.
18#
19# You can search for some particular version of Python by passing a
20# parameter to this macro, for example ">= '2.3.1'", or "== '2.4'". Please
21# note that you *have* to pass also an operator along with the version to
22# match, and pay special attention to the single quotes surrounding the
23# version number. Don't use "PYTHON_VERSION" for this: that environment
24# variable is declared as precious and thus reserved for the end-user.
25#
26# This macro should work for all versions of Python >= 2.1.0. As an end
27# user, you can disable the check for the python version by setting the
28# PYTHON_NOVERSIONCHECK environment variable to something else than the
29# empty string.
30#
31# If you need to use this macro for an older Python version, please
32# contact the authors. We're always open for feedback.
33#
34# LAST MODIFICATION
35#
36# 2008-04-12
37#
38# COPYLEFT
39#
40# Copyright (c) 2008 Sebastian Huber <sebastian-huber@web.de>
41# Copyright (c) 2008 Alan W. Irwin <irwin@beluga.phys.uvic.ca>
42# Copyright (c) 2008 Rafael Laboissiere <rafael@laboissiere.net>
43# Copyright (c) 2008 Andrew Collier <colliera@ukzn.ac.za>
44# Copyright (c) 2008 Matteo Settenvini <matteo@member.fsf.org>
45# Copyright (c) 2008 Horst Knorr <hk_classes@knoda.org>
46#
47# This program is free software: you can redistribute it and/or modify it
48# under the terms of the GNU General Public License as published by the
49# Free Software Foundation, either version 3 of the License, or (at your
50# option) any later version.
51#
52# This program is distributed in the hope that it will be useful, but
53# WITHOUT ANY WARRANTY; without even the implied warranty of
54# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
55# Public License for more details.
56#
57# You should have received a copy of the GNU General Public License along
58# with this program. If not, see <http://www.gnu.org/licenses/>.
59#
60# As a special exception, the respective Autoconf Macro's copyright owner
61# gives unlimited permission to copy, distribute and modify the configure
62# scripts that are the output of Autoconf when processing the Macro. You
63# need not follow the terms of the GNU General Public License when using
64# or distributing such scripts, even though portions of the text of the
65# Macro appear in them. The GNU General Public License (GPL) does govern
66# all other use of the material that constitutes the Autoconf Macro.
67#
68# This special exception to the GPL applies to versions of the Autoconf
69# Macro released by the Autoconf Macro Archive. When you make and
70# distribute a modified version of the Autoconf Macro, you may extend this
71# special exception to the GPL to apply to your modified version as well.
72
73AC_DEFUN([AC_PYTHON_DEVEL],[
74 #
75 # Allow the use of a (user set) custom python version
76 #
77 AC_ARG_VAR([PYTHON_VERSION],[The installed Python
78 version to use, for example '2.3'. This string
79 will be appended to the Python interpreter
80 canonical name.])
81
82 AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
83 if test -z "$PYTHON"; then
84 AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
85 PYTHON_VERSION=""
86 fi
87
88 #
89 # Check for a version of Python >= 2.1.0
90 #
91 AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
92 ac_supports_python_ver=`$PYTHON -c "import sys, string; \
93 ver = string.split(sys.version)[[0]]; \
94 print ver >= '2.1.0'"`
95 if test "$ac_supports_python_ver" != "True"; then
96 if test -z "$PYTHON_NOVERSIONCHECK"; then
97 AC_MSG_RESULT([no])
98 AC_MSG_FAILURE([
99This version of the AC@&t@_PYTHON_DEVEL macro
100doesn't work properly with versions of Python before
1012.1.0. You may need to re-run configure, setting the
102variables PYTHON_CPPFLAGS, PYTHON_LDFLAGS, PYTHON_SITE_PKG,
103PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
104Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
105to something else than an empty string.
106])
107 else
108 AC_MSG_RESULT([skip at user request])
109 fi
110 else
111 AC_MSG_RESULT([yes])
112 fi
113
114 #
115 # if the macro parameter ``version'' is set, honour it
116 #
117 if test -n "$1"; then
118 AC_MSG_CHECKING([for a version of Python $1])
119 ac_supports_python_ver=`$PYTHON -c "import sys, string; \
120 ver = string.split(sys.version)[[0]]; \
121 print ver $1"`
122 if test "$ac_supports_python_ver" = "True"; then
123 AC_MSG_RESULT([yes])
124 else
125 AC_MSG_RESULT([no])
126 AC_MSG_ERROR([this package requires Python $1.
127If you have it installed, but it isn't the default Python
128interpreter in your system path, please pass the PYTHON_VERSION
129variable to configure. See ``configure --help'' for reference.
130])
131 PYTHON_VERSION=""
132 fi
133 fi
134
135 #
136 # Check if you have distutils, else fail
137 #
138 AC_MSG_CHECKING([for the distutils Python package])
139 ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
140 if test -z "$ac_distutils_result"; then
141 AC_MSG_RESULT([yes])
142 else
143 AC_MSG_RESULT([no])
144 AC_MSG_ERROR([cannot import Python module "distutils".
145Please check your Python installation. The error was:
146$ac_distutils_result])
147 PYTHON_VERSION=""
148 fi
149
150 #
151 # Check for Python include path
152 #
153 AC_MSG_CHECKING([for Python include path])
154 if test -z "$PYTHON_CPPFLAGS"; then
155 python_path=`$PYTHON -c "import distutils.sysconfig; \
156 print distutils.sysconfig.get_python_inc();"`
157 if test -n "${python_path}"; then
158 python_path="-I$python_path"
159 fi
160 PYTHON_CPPFLAGS=$python_path
161 fi
162 AC_MSG_RESULT([$PYTHON_CPPFLAGS])
163 AC_SUBST([PYTHON_CPPFLAGS])
164
165 #
166 # Check for Python library path
167 #
168 AC_MSG_CHECKING([for Python library path])
169 if test -z "$PYTHON_LDFLAGS"; then
170 # (makes two attempts to ensure we've got a version number
171 # from the interpreter)
172 py_version=`$PYTHON -c "from distutils.sysconfig import *; \
173 from string import join; \
174 print join(get_config_vars('VERSION'))"`
175 if test "$py_version" == "[None]"; then
176 if test -n "$PYTHON_VERSION"; then
177 py_version=$PYTHON_VERSION
178 else
179 py_version=`$PYTHON -c "import sys; \
180 print sys.version[[:3]]"`
181 fi
182 fi
183
184 PYTHON_LDFLAGS=`$PYTHON -c "from distutils.sysconfig import *; \
185 from string import join; \
186 print '-L' + get_python_lib(0,1), \
187 '-lpython';"`$py_version
188 fi
189 AC_MSG_RESULT([$PYTHON_LDFLAGS])
190 AC_SUBST([PYTHON_LDFLAGS])
191
192 #
193 # Check for site packages
194 #
195 AC_MSG_CHECKING([for Python site-packages path])
196 if test -z "$PYTHON_SITE_PKG"; then
197 PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
198 print distutils.sysconfig.get_python_lib(0,0);"`
199 fi
200 AC_MSG_RESULT([$PYTHON_SITE_PKG])
201 AC_SUBST([PYTHON_SITE_PKG])
202
203 #
204 # libraries which must be linked in when embedding
205 #
206 AC_MSG_CHECKING(python extra libraries)
207 if test -z "$PYTHON_EXTRA_LIBS"; then
208 PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
209 conf = distutils.sysconfig.get_config_var; \
210 print conf('LOCALMODLIBS'), conf('LIBS')"`
211 fi
212 AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
213 AC_SUBST(PYTHON_EXTRA_LIBS)
214
215 #
216 # linking flags needed when embedding
217 #
218 AC_MSG_CHECKING(python extra linking flags)
219 if test -z "$PYTHON_EXTRA_LDFLAGS"; then
220 PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
221 conf = distutils.sysconfig.get_config_var; \
222 print conf('LINKFORSHARED')"`
223 fi
224 AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
225 AC_SUBST(PYTHON_EXTRA_LDFLAGS)
226
227 #
228 # final check to see if everything compiles alright
229 #
230 AC_MSG_CHECKING([consistency of all components of python development environment])
231 AC_LANG_PUSH([C])
232 # save current global flags
233 LIBS="$ac_save_LIBS $PYTHON_LDFLAGS"
234 CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
235 AC_TRY_LINK([
236 #include <Python.h>
237 ],[
238 Py_Initialize();
239 ],[pythonexists=yes],[pythonexists=no])
240
241 AC_MSG_RESULT([$pythonexists])
242
243 if test ! "$pythonexists" = "yes"; then
244 AC_MSG_ERROR([
245 Could not link test program to Python. Maybe the main Python library has been
246 installed in some non-standard library path. If so, pass it to configure,
247 via the LDFLAGS environment variable.
248 Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
249 ============================================================================
250 ERROR!
251 You probably have to install the development version of the Python package
252 for your distribution. The exact name of this package varies among them.
253 ============================================================================
254 ])
255 PYTHON_VERSION=""
256 fi
257 AC_LANG_POP
258 # turn back to default flags
259 CPPFLAGS="$ac_save_CPPFLAGS"
260 LIBS="$ac_save_LIBS"
261
262 #
263 # all done!
264 #
265])
diff --git a/m4/as-compiler-flag.m4 b/m4/as-compiler-flag.m4
new file mode 100644
index 0000000..0f660cf
--- /dev/null
+++ b/m4/as-compiler-flag.m4
@@ -0,0 +1,62 @@
1dnl as-compiler-flag.m4 0.1.0
2
3dnl autostars m4 macro for detection of compiler flags
4
5dnl David Schleef <ds@schleef.org>
6
7dnl $Id: as-compiler-flag.m4,v 1.1 2005/12/15 23:35:19 ds Exp $
8
9dnl AS_COMPILER_FLAG(CFLAGS, ACTION-IF-ACCEPTED, [ACTION-IF-NOT-ACCEPTED])
10dnl Tries to compile with the given CFLAGS.
11dnl Runs ACTION-IF-ACCEPTED if the compiler can compile with the flags,
12dnl and ACTION-IF-NOT-ACCEPTED otherwise.
13
14AC_DEFUN([AS_COMPILER_FLAG],
15[
16 AC_MSG_CHECKING([to see if compiler understands $1])
17
18 save_CFLAGS="$CFLAGS"
19 CFLAGS="$CFLAGS $1"
20
21 AC_TRY_COMPILE([ ], [], [flag_ok=yes], [flag_ok=no])
22 CFLAGS="$save_CFLAGS"
23
24 if test "X$flag_ok" = Xyes ; then
25 m4_ifvaln([$2],[$2])
26 true
27 else
28 m4_ifvaln([$3],[$3])
29 true
30 fi
31 AC_MSG_RESULT([$flag_ok])
32])
33
34dnl AS_COMPILER_FLAGS(VAR, FLAGS)
35dnl Tries to compile with the given CFLAGS.
36
37AC_DEFUN([AS_COMPILER_FLAGS],
38[
39 list=$2
40 flags_supported=""
41 flags_unsupported=""
42 AC_MSG_CHECKING([for supported compiler flags])
43 for each in $list
44 do
45 save_CFLAGS="$CFLAGS"
46 CFLAGS="$CFLAGS $each"
47 AC_TRY_COMPILE([ ], [], [flag_ok=yes], [flag_ok=no])
48 CFLAGS="$save_CFLAGS"
49
50 if test "X$flag_ok" = Xyes ; then
51 flags_supported="$flags_supported $each"
52 else
53 flags_unsupported="$flags_unsupported $each"
54 fi
55 done
56 AC_MSG_RESULT([$flags_supported])
57 if test "X$flags_unsupported" != X ; then
58 AC_MSG_WARN([unsupported compiler flags: $flags_unsupported])
59 fi
60 $1="$$1 $flags_supported"
61])
62
diff --git a/m4/swig_python.m4 b/m4/swig_python.m4
new file mode 100644
index 0000000..2496976
--- /dev/null
+++ b/m4/swig_python.m4
@@ -0,0 +1,65 @@
1# ===========================================================================
2# http://autoconf-archive.cryp.to/swig_python.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7# SWIG_PYTHON([use-shadow-classes = {no, yes}])
8#
9# DESCRIPTION
10#
11# Checks for Python and provides the $(SWIG_PYTHON_CPPFLAGS), and
12# $(SWIG_PYTHON_OPT) output variables.
13#
14# $(SWIG_PYTHON_OPT) contains all necessary SWIG options to generate code
15# for Python. Shadow classes are enabled unless the value of the optional
16# first argument is exactly 'no'. If you need multi module support
17# (provided by the SWIG_MULTI_MODULE_SUPPORT macro) use
18# $(SWIG_PYTHON_LIBS) to link against the appropriate library. It contains
19# the SWIG Python runtime library that is needed by the type check system
20# for example.
21#
22# LAST MODIFICATION
23#
24# 2008-04-12
25#
26# COPYLEFT
27#
28# Copyright (c) 2008 Sebastian Huber <sebastian-huber@web.de>
29# Copyright (c) 2008 Alan W. Irwin <irwin@beluga.phys.uvic.ca>
30# Copyright (c) 2008 Rafael Laboissiere <rafael@laboissiere.net>
31# Copyright (c) 2008 Andrew Collier <colliera@ukzn.ac.za>
32#
33# This program is free software; you can redistribute it and/or modify it
34# under the terms of the GNU General Public License as published by the
35# Free Software Foundation; either version 2 of the License, or (at your
36# option) any later version.
37#
38# This program is distributed in the hope that it will be useful, but
39# WITHOUT ANY WARRANTY; without even the implied warranty of
40# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
41# Public License for more details.
42#
43# You should have received a copy of the GNU General Public License along
44# with this program. If not, see <http://www.gnu.org/licenses/>.
45#
46# As a special exception, the respective Autoconf Macro's copyright owner
47# gives unlimited permission to copy, distribute and modify the configure
48# scripts that are the output of Autoconf when processing the Macro. You
49# need not follow the terms of the GNU General Public License when using
50# or distributing such scripts, even though portions of the text of the
51# Macro appear in them. The GNU General Public License (GPL) does govern
52# all other use of the material that constitutes the Autoconf Macro.
53#
54# This special exception to the GPL applies to versions of the Autoconf
55# Macro released by the Autoconf Macro Archive. When you make and
56# distribute a modified version of the Autoconf Macro, you may extend this
57# special exception to the GPL to apply to your modified version as well.
58
59AC_DEFUN([SWIG_PYTHON],[
60 AC_REQUIRE([AC_PROG_SWIG])
61 AC_REQUIRE([AC_PYTHON_DEVEL])
62 test "x$1" != "xno" || swig_shadow=" -noproxy"
63 AC_SUBST([SWIG_PYTHON_OPT],[-python$swig_shadow])
64 AC_SUBST([SWIG_PYTHON_CPPFLAGS],[$PYTHON_CPPFLAGS])
65])
diff --git a/src/AFC.c b/src/AFC.c
index af07b56..dfe8af7 100644
--- a/src/AFC.c
+++ b/src/AFC.c
@@ -21,8 +21,7 @@
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include "AFC.h" 23#include "AFC.h"
24#include "plist.h" 24
25#include "utils.h"
26 25
27 26
28// This is the maximum size an AFC data packet can be 27// This is the maximum size an AFC data packet can be
@@ -246,7 +245,7 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here)
246 return retval; 245 return retval;
247 } 246 }
248 247
249 uint32 param1 = buffer[sizeof(AFCPacket)]; 248 uint32_t param1 = buffer[sizeof(AFCPacket)];
250 free(buffer); 249 free(buffer);
251 250
252 if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { 251 if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) {
@@ -475,7 +474,7 @@ iphone_error_t iphone_afc_delete_file(iphone_afc_client_t client, const char *pa
475iphone_error_t iphone_afc_rename_file(iphone_afc_client_t client, const char *from, const char *to) 474iphone_error_t iphone_afc_rename_file(iphone_afc_client_t client, const char *from, const char *to)
476{ 475{
477 char *response = NULL; 476 char *response = NULL;
478 char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32))); 477 char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t)));
479 int bytes = 0; 478 int bytes = 0;
480 479
481 if (!client || !from || !to || !client->afc_packet || !client->connection) 480 if (!client || !from || !to || !client->afc_packet || !client->connection)
@@ -661,7 +660,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename,
661 iphone_afc_file_mode_t file_mode, iphone_afc_file_t * file) 660 iphone_afc_file_mode_t file_mode, iphone_afc_file_t * file)
662{ 661{
663 iphone_afc_file_t file_loc = NULL; 662 iphone_afc_file_t file_loc = NULL;
664 uint32 ag = 0; 663 uint32_t ag = 0;
665 int bytes = 0, length = 0; 664 int bytes = 0, length = 0;
666 char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); 665 char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1));
667 666
@@ -796,8 +795,8 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file,
796{ 795{
797 char *acknowledgement = NULL; 796 char *acknowledgement = NULL;
798 const int MAXIMUM_WRITE_SIZE = 1 << 15; 797 const int MAXIMUM_WRITE_SIZE = 1 << 15;
799 uint32 zero = 0, current_count = 0, i = 0; 798 uint32_t zero = 0, current_count = 0, i = 0;
800 uint32 segments = (length / MAXIMUM_WRITE_SIZE); 799 uint32_t segments = (length / MAXIMUM_WRITE_SIZE);
801 int bytes_loc = 0; 800 int bytes_loc = 0;
802 char *out_buffer = NULL; 801 char *out_buffer = NULL;
803 802
@@ -815,8 +814,8 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file,
815 client->afc_packet->entire_length = client->afc_packet->this_length + MAXIMUM_WRITE_SIZE; 814 client->afc_packet->entire_length = client->afc_packet->this_length + MAXIMUM_WRITE_SIZE;
816 client->afc_packet->operation = AFC_WRITE; 815 client->afc_packet->operation = AFC_WRITE;
817 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); 816 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket));
818 memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32)); 817 memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32_t));
819 memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32)); 818 memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32_t));
820 memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); 819 memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE);
821 bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); 820 bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8);
822 if (bytes_loc < 0) { 821 if (bytes_loc < 0) {
@@ -848,8 +847,8 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file,
848 client->afc_packet->entire_length = client->afc_packet->this_length + (length - current_count); 847 client->afc_packet->entire_length = client->afc_packet->this_length + (length - current_count);
849 client->afc_packet->operation = AFC_WRITE; 848 client->afc_packet->operation = AFC_WRITE;
850 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); 849 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket));
851 memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32)); 850 memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32_t));
852 memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32)); 851 memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32_t));
853 memcpy(out_buffer + 8, data + current_count, (length - current_count)); 852 memcpy(out_buffer + 8, data + current_count, (length - current_count));
854 bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8); 853 bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8);
855 free(out_buffer); 854 free(out_buffer);
@@ -884,7 +883,7 @@ iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file
884 if (!client || !file) 883 if (!client || !file)
885 return IPHONE_E_INVALID_ARG; 884 return IPHONE_E_INVALID_ARG;
886 char *buffer = malloc(sizeof(char) * 8); 885 char *buffer = malloc(sizeof(char) * 8);
887 uint32 zero = 0; 886 uint32_t zero = 0;
888 int bytes = 0; 887 int bytes = 0;
889 888
890 afc_lock(client); 889 afc_lock(client);
@@ -892,8 +891,8 @@ iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file
892 log_debug_msg("afc_close_file: File handle %i\n", file->filehandle); 891 log_debug_msg("afc_close_file: File handle %i\n", file->filehandle);
893 892
894 // Send command 893 // Send command
895 memcpy(buffer, &file->filehandle, sizeof(uint32)); 894 memcpy(buffer, &file->filehandle, sizeof(uint32_t));
896 memcpy(buffer + sizeof(uint32), &zero, sizeof(zero)); 895 memcpy(buffer + sizeof(uint32_t), &zero, sizeof(zero));
897 client->afc_packet->operation = AFC_FILE_CLOSE; 896 client->afc_packet->operation = AFC_FILE_CLOSE;
898 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 897 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
899 bytes = dispatch_AFC_packet(client, buffer, sizeof(char) * 8); 898 bytes = dispatch_AFC_packet(client, buffer, sizeof(char) * 8);
@@ -939,7 +938,7 @@ iphone_error_t iphone_afc_lock_file(iphone_afc_client_t client, iphone_afc_file_
939 if (!client || !file) 938 if (!client || !file)
940 return IPHONE_E_INVALID_ARG; 939 return IPHONE_E_INVALID_ARG;
941 char *buffer = malloc(16); 940 char *buffer = malloc(16);
942 uint32 zero = 0; 941 uint32_t zero = 0;
943 int bytes = 0; 942 int bytes = 0;
944 uint64_t op = operation; 943 uint64_t op = operation;
945 944
@@ -948,8 +947,8 @@ iphone_error_t iphone_afc_lock_file(iphone_afc_client_t client, iphone_afc_file_
948 log_debug_msg("afc_lock_file: File handle %i\n", file->filehandle); 947 log_debug_msg("afc_lock_file: File handle %i\n", file->filehandle);
949 948
950 // Send command 949 // Send command
951 memcpy(buffer, &file->filehandle, sizeof(uint32)); 950 memcpy(buffer, &file->filehandle, sizeof(uint32_t));
952 memcpy(buffer + sizeof(uint32), &zero, sizeof(zero)); 951 memcpy(buffer + sizeof(uint32_t), &zero, sizeof(zero));
953 memcpy(buffer + 8, &op, 8); 952 memcpy(buffer + 8, &op, 8);
954 953
955 client->afc_packet->operation = AFC_FILE_LOCK; 954 client->afc_packet->operation = AFC_FILE_LOCK;
@@ -986,7 +985,7 @@ iphone_error_t iphone_afc_lock_file(iphone_afc_client_t client, iphone_afc_file_
986iphone_error_t iphone_afc_seek_file(iphone_afc_client_t client, iphone_afc_file_t file, int seekpos) 985iphone_error_t iphone_afc_seek_file(iphone_afc_client_t client, iphone_afc_file_t file, int seekpos)
987{ 986{
988 char *buffer = (char *) malloc(sizeof(char) * 24); 987 char *buffer = (char *) malloc(sizeof(char) * 24);
989 uint32 seekto = 0, zero = 0; 988 uint32_t seekto = 0, zero = 0;
990 int bytes = 0; 989 int bytes = 0;
991 990
992 if (seekpos < 0) 991 if (seekpos < 0)
@@ -996,12 +995,12 @@ iphone_error_t iphone_afc_seek_file(iphone_afc_client_t client, iphone_afc_file_
996 995
997 // Send the command 996 // Send the command
998 seekto = seekpos; 997 seekto = seekpos;
999 memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle 998 memcpy(buffer, &file->filehandle, sizeof(uint32_t)); // handle
1000 memcpy(buffer + 4, &zero, sizeof(uint32)); // pad 999 memcpy(buffer + 4, &zero, sizeof(uint32_t)); // pad
1001 memcpy(buffer + 8, &zero, sizeof(uint32)); // fromwhere 1000 memcpy(buffer + 8, &zero, sizeof(uint32_t)); // fromwhere
1002 memcpy(buffer + 12, &zero, sizeof(uint32)); // pad 1001 memcpy(buffer + 12, &zero, sizeof(uint32_t)); // pad
1003 memcpy(buffer + 16, &seekto, sizeof(uint32)); // offset 1002 memcpy(buffer + 16, &seekto, sizeof(uint32_t)); // offset
1004 memcpy(buffer + 20, &zero, sizeof(uint32)); // pad 1003 memcpy(buffer + 20, &zero, sizeof(uint32_t)); // pad
1005 client->afc_packet->operation = AFC_FILE_SEEK; 1004 client->afc_packet->operation = AFC_FILE_SEEK;
1006 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 1005 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
1007 bytes = dispatch_AFC_packet(client, buffer, 23); 1006 bytes = dispatch_AFC_packet(client, buffer, 23);
@@ -1041,14 +1040,14 @@ iphone_error_t iphone_afc_truncate_file(iphone_afc_client_t client, iphone_afc_f
1041{ 1040{
1042 char *buffer = (char *) malloc(sizeof(char) * 16); 1041 char *buffer = (char *) malloc(sizeof(char) * 16);
1043 int bytes = 0; 1042 int bytes = 0;
1044 uint32 zero = 0; 1043 uint32_t zero = 0;
1045 1044
1046 afc_lock(client); 1045 afc_lock(client);
1047 1046
1048 // Send command 1047 // Send command
1049 memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle 1048 memcpy(buffer, &file->filehandle, sizeof(uint32_t)); // handle
1050 memcpy(buffer + 4, &zero, sizeof(uint32)); // pad 1049 memcpy(buffer + 4, &zero, sizeof(uint32_t)); // pad
1051 memcpy(buffer + 8, &newsize, sizeof(uint32)); // newsize 1050 memcpy(buffer + 8, &newsize, sizeof(uint32_t)); // newsize
1052 memcpy(buffer + 12, &zero, 3); // pad 1051 memcpy(buffer + 12, &zero, 3); // pad
1053 client->afc_packet->operation = AFC_FILE_TRUNCATE; 1052 client->afc_packet->operation = AFC_FILE_TRUNCATE;
1054 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 1053 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
@@ -1121,7 +1120,7 @@ iphone_error_t iphone_afc_truncate(iphone_afc_client_t client, const char *path,
1121} 1120}
1122 1121
1123 1122
1124uint32 iphone_afc_get_file_handle(iphone_afc_file_t file) 1123uint32_t iphone_afc_get_file_handle(iphone_afc_file_t file)
1125{ 1124{
1126 return file->filehandle; 1125 return file->filehandle;
1127} 1126}
diff --git a/src/AFC.h b/src/AFC.h
index ae035c6..86a924e 100644
--- a/src/AFC.h
+++ b/src/AFC.h
@@ -29,12 +29,12 @@
29#include <glib.h> 29#include <glib.h>
30 30
31typedef struct { 31typedef struct {
32 uint32 header1, header2; 32 uint32_t header1, header2;
33 uint32 entire_length, unknown1, this_length, unknown2, packet_num, unknown3, operation, unknown4; 33 uint32_t entire_length, unknown1, this_length, unknown2, packet_num, unknown3, operation, unknown4;
34} AFCPacket; 34} AFCPacket;
35 35
36typedef struct { 36typedef struct {
37 uint32 filehandle, unknown1, size, unknown2; 37 uint32_t filehandle, unknown1, size, unknown2;
38} AFCFilePacket; 38} AFCFilePacket;
39 39
40typedef struct __AFCToken { 40typedef struct __AFCToken {
@@ -51,7 +51,7 @@ struct iphone_afc_client_int {
51}; 51};
52 52
53struct iphone_afc_file_int { 53struct iphone_afc_file_int {
54 uint32 filehandle, blocks, size, type; 54 uint32_t filehandle, blocks, size, type;
55}; 55};
56 56
57 57
@@ -87,4 +87,4 @@ enum {
87 AFC_MAKE_LINK = 0x0000001C // MakeLink 87 AFC_MAKE_LINK = 0x0000001C // MakeLink
88}; 88};
89 89
90uint32 iphone_afc_get_file_handle(iphone_afc_file_t file); 90uint32_t iphone_afc_get_file_handle(iphone_afc_file_t file);
diff --git a/src/Makefile.am b/src/Makefile.am
index 76b15ec..a10254c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,15 +1,7 @@
1INCLUDES = -I$(top_srcdir)/include 1INCLUDES = -I$(top_srcdir)/include
2 2
3AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) -g -Wall $(LFS_CFLAGS) 3AM_CFLAGS = $(GLOBAL_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(libplist_CFLAGS) $(LFS_CFLAGS)
4AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) 4AM_LDFLAGS = $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) $(libplist_LIBS)
5
6bin_PROGRAMS = libiphone-initconf
7
8
9libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c utils.c
10libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS)
11libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS)
12
13 5
14lib_LTLIBRARIES = libiphone.la 6lib_LTLIBRARIES = libiphone.la
15libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c NotificationProxy.c userpref.c utils.c 7libiphone_la_SOURCES = usbmux.c iphone.c lockdown.c AFC.c NotificationProxy.c userpref.c utils.c MobileSync.c
diff --git a/src/MobileSync.c b/src/MobileSync.c
new file mode 100644
index 0000000..b16a51b
--- /dev/null
+++ b/src/MobileSync.c
@@ -0,0 +1,299 @@
1/*
2 * MobileSync.c
3 * Contains functions for the built-in MobileSync client.
4 *
5 * Copyright (c) 2009 Jonathan Beck 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 "MobileSync.h"
23#include <plist/plist.h>
24#include <string.h>
25
26
27#define MSYNC_VERSION_INT1 100
28#define MSYNC_VERSION_INT2 100
29
30iphone_error_t iphone_msync_new_client(iphone_device_t device, int src_port, int dst_port,
31 iphone_msync_client_t * client)
32{
33 if (!device || src_port == 0 || dst_port == 0 || !client || *client)
34 return IPHONE_E_INVALID_ARG;
35
36 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
37
38 iphone_msync_client_t client_loc = (iphone_msync_client_t) malloc(sizeof(struct iphone_msync_client_int));
39
40 // Attempt connection
41 client_loc->connection = NULL;
42 ret = iphone_mux_new_client(device, src_port, dst_port, &client_loc->connection);
43 if (IPHONE_E_SUCCESS != ret || !client_loc->connection) {
44 free(client_loc);
45 return ret;
46 }
47 //perform handshake
48 plist_t array = NULL;
49
50 //first receive version
51 ret = iphone_msync_recv(client_loc, &array);
52
53 plist_t msg_node = plist_find_node_by_string(array, "DLMessageVersionExchange");
54 plist_t ver_1 = plist_get_next_sibling(msg_node);
55 plist_t ver_2 = plist_get_next_sibling(ver_1);
56
57 plist_type ver_1_type = plist_get_node_type(ver_1);
58 plist_type ver_2_type = plist_get_node_type(ver_2);
59
60 if (PLIST_UINT == ver_1_type && PLIST_UINT == ver_2_type) {
61
62 uint64_t ver_1_val = 0;
63 uint64_t ver_2_val = 0;
64
65 plist_get_uint_val(ver_1, &ver_1_val);
66 plist_get_uint_val(ver_2, &ver_2_val);
67
68 plist_free(array);
69 array = NULL;
70
71 if (ver_1_type == PLIST_UINT && ver_2_type == PLIST_UINT && ver_1_val == MSYNC_VERSION_INT1
72 && ver_2_val == MSYNC_VERSION_INT2) {
73
74 array = plist_new_array();
75 plist_add_sub_string_el(array, "DLMessageVersionExchange");
76 plist_add_sub_string_el(array, "DLVersionsOk");
77
78 ret = iphone_msync_send(client_loc, array);
79
80 plist_free(array);
81 array = NULL;
82
83 ret = iphone_msync_recv(client_loc, &array);
84 plist_t rep_node = plist_find_node_by_string(array, "DLMessageDeviceReady");
85
86 if (rep_node) {
87 ret = IPHONE_E_SUCCESS;
88 *client = client_loc;
89 }
90 plist_free(array);
91 array = NULL;
92
93 }
94 }
95
96 if (IPHONE_E_SUCCESS != ret)
97 iphone_msync_free_client(client_loc);
98
99 return ret;
100}
101
102static void iphone_msync_stop_session(iphone_msync_client_t client)
103{
104 if (!client)
105 return;
106
107 plist_t array = plist_new_array();
108 plist_add_sub_string_el(array, "DLMessageDisconnect");
109 plist_add_sub_string_el(array, "All done, thanks for the memories");
110
111 iphone_msync_send(client, array);
112 plist_free(array);
113 array = NULL;
114}
115
116iphone_error_t iphone_msync_free_client(iphone_msync_client_t client)
117{
118 if (!client)
119 return IPHONE_E_INVALID_ARG;
120
121 iphone_msync_stop_session(client);
122 return iphone_mux_free_client(client->connection);
123}
124
125/** Polls the iPhone for MobileSync data.
126 *
127 * @param client The MobileSync client
128 * @param dump_data The pointer to the location of the buffer in which to store
129 * the received data
130 * @param recv_byhtes The number of bytes received
131 *
132 * @return an error code
133 */
134iphone_error_t iphone_msync_recv(iphone_msync_client_t client, plist_t * plist)
135{
136 if (!client || !plist || (plist && *plist))
137 return IPHONE_E_INVALID_ARG;
138 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
139 char *receive;
140 uint32_t datalen = 0, bytes = 0;
141
142 ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes);
143 datalen = ntohl(datalen);
144
145 receive = (char *) malloc(sizeof(char) * datalen);
146 ret = iphone_mux_recv(client->connection, receive, datalen, &bytes);
147
148 plist_from_bin(receive, bytes, plist);
149
150 char *XMLContent = NULL;
151 uint32_t length = 0;
152 plist_to_xml(*plist, &XMLContent, &length);
153 log_dbg_msg(DBGMASK_MOBILESYNC, "Recv msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent);
154 free(XMLContent);
155
156 return ret;
157}
158
159/** Sends MobileSync data to the iPhone
160 *
161 * @note This function is low-level and should only be used if you need to send
162 * a new type of message.
163 *
164 * @param client The MobileSync client
165 * @param raw_data The null terminated string buffer to send
166 * @param length The length of data to send
167 * @param sent_bytes The number of bytes sent
168 *
169 * @return an error code
170 */
171iphone_error_t iphone_msync_send(iphone_msync_client_t client, plist_t plist)
172{
173 if (!client || !plist)
174 return IPHONE_E_INVALID_ARG;
175
176 char *XMLContent = NULL;
177 uint32_t length = 0;
178 plist_to_xml(plist, &XMLContent, &length);
179 log_dbg_msg(DBGMASK_MOBILESYNC, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent);
180 free(XMLContent);
181
182 char *content = NULL;
183 length = 0;
184
185 plist_to_bin(plist, &content, &length);
186
187 char *real_query;
188 int bytes;
189 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
190
191 real_query = (char *) malloc(sizeof(char) * (length + 4));
192 length = htonl(length);
193 memcpy(real_query, &length, sizeof(length));
194 memcpy(real_query + 4, content, ntohl(length));
195
196 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes);
197 free(real_query);
198 return ret;
199}
200
201iphone_error_t iphone_msync_get_all_contacts(iphone_msync_client_t client)
202{
203 if (!client)
204 return IPHONE_E_INVALID_ARG;
205
206 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
207 plist_t array = NULL;
208
209 array = plist_new_array();
210 plist_add_sub_string_el(array, "SDMessageSyncDataClassWithDevice");
211 plist_add_sub_string_el(array, "com.apple.Contacts");
212 plist_add_sub_string_el(array, "---");
213 plist_add_sub_string_el(array, "2009-01-09 18:03:58 +0100");
214 plist_add_sub_uint_el(array, 106);
215 plist_add_sub_string_el(array, "___EmptyParameterString___");
216
217 ret = iphone_msync_send(client, array);
218 plist_free(array);
219 array = NULL;
220
221 ret = iphone_msync_recv(client, &array);
222
223 plist_t rep_node = plist_find_node_by_string(array, "SDSyncTypeSlow");
224
225 if (!rep_node)
226 return ret;
227
228 plist_free(array);
229 array = NULL;
230
231 array = plist_new_array();
232 plist_add_sub_string_el(array, "SDMessageGetAllRecordsFromDevice");
233 plist_add_sub_string_el(array, "com.apple.Contacts");
234
235
236 ret = iphone_msync_send(client, array);
237 plist_free(array);
238 array = NULL;
239
240 ret = iphone_msync_recv(client, &array);
241
242 plist_t contact_node;
243 plist_t switch_node;
244
245 contact_node = plist_find_node_by_string(array, "com.apple.Contacts");
246 switch_node = plist_find_node_by_string(array, "SDMessageDeviceReadyToReceiveChanges");
247
248 while (NULL == switch_node) {
249
250 plist_free(array);
251 array = NULL;
252
253 array = plist_new_array();
254 plist_add_sub_string_el(array, "SDMessageAcknowledgeChangesFromDevice");
255 plist_add_sub_string_el(array, "com.apple.Contacts");
256
257 ret = iphone_msync_send(client, array);
258 plist_free(array);
259 array = NULL;
260
261 ret = iphone_msync_recv(client, &array);
262
263 contact_node = plist_find_node_by_string(array, "com.apple.Contacts");
264 switch_node = plist_find_node_by_string(array, "SDMessageDeviceReadyToReceiveChanges");
265 }
266
267 array = plist_new_array();
268 plist_add_sub_string_el(array, "DLMessagePing");
269 plist_add_sub_string_el(array, "Preparing to get changes for device");
270
271 ret = iphone_msync_send(client, array);
272 plist_free(array);
273 array = NULL;
274
275 array = plist_new_array();
276 plist_add_sub_string_el(array, "SDMessageProcessChanges");
277 plist_add_sub_string_el(array, "com.apple.Contacts");
278 plist_add_sub_node(array, plist_new_dict());
279 plist_add_sub_bool_el(array, 0);
280 plist_t dict = plist_new_dict();
281 plist_add_sub_node(array, dict);
282 plist_add_sub_key_el(dict, "SyncDeviceLinkEntityNamesKey");
283 plist_t array2 = plist_new_array();
284 plist_add_sub_string_el(array2, "com.apple.contacts.Contact");
285 plist_add_sub_string_el(array2, "com.apple.contacts.Group");
286 plist_add_sub_key_el(dict, "SyncDeviceLinkAllRecordsOfPulledEntityTypeSentKey");
287 plist_add_sub_bool_el(dict, 0);
288
289 ret = iphone_msync_send(client, array);
290 plist_free(array);
291 array = NULL;
292
293 ret = iphone_msync_recv(client, &array);
294 plist_free(array);
295 array = NULL;
296
297
298 return ret;
299}
diff --git a/src/MobileSync.h b/src/MobileSync.h
new file mode 100644
index 0000000..7655b59
--- /dev/null
+++ b/src/MobileSync.h
@@ -0,0 +1,39 @@
1/*
2 * MobileSync.h
3 * Definitions for the built-in MobileSync client
4 *
5 * Copyright (c) 2009 Jonathan Beck 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#ifndef MOBILESYNC_H
22#define MOBILESYNC_H
23
24#include "usbmux.h"
25#include "iphone.h"
26#include "utils.h"
27
28#include <plist/plist.h>
29
30
31
32struct iphone_msync_client_int {
33 iphone_umux_client_t connection;
34};
35
36
37iphone_error_t iphone_msync_get_all_contacts(iphone_msync_client_t client);
38
39#endif
diff --git a/src/NotificationProxy.c b/src/NotificationProxy.c
index defbc4e..bf837bf 100644
--- a/src/NotificationProxy.c
+++ b/src/NotificationProxy.c
@@ -21,8 +21,8 @@
21 21
22#include <string.h> 22#include <string.h>
23#include <stdio.h> 23#include <stdio.h>
24#include <plist/plist.h>
24#include "NotificationProxy.h" 25#include "NotificationProxy.h"
25#include "plist.h"
26#include "utils.h" 26#include "utils.h"
27 27
28/** Locks an NP client, done for thread safety stuff. 28/** Locks an NP client, done for thread safety stuff.
@@ -104,26 +104,26 @@ iphone_error_t iphone_np_free_client(iphone_np_client_t client)
104 */ 104 */
105iphone_error_t iphone_np_post_notification(iphone_np_client_t client, const char *notification) 105iphone_error_t iphone_np_post_notification(iphone_np_client_t client, const char *notification)
106{ 106{
107 xmlDocPtr plist; 107 char *XML_content = NULL;
108 xmlNode *dict, *key; 108 uint32_t length = 0;
109 char *XML_content; 109 int bytes = 0;
110 uint32_t length;
111 int bytes;
112 iphone_error_t ret; 110 iphone_error_t ret;
113 unsigned char sndbuf[4096]; 111 unsigned char sndbuf[4096];
114 int sndlen = 0; 112 int sndlen = 0;
115 int nlen; 113 int nlen = 0;
114 plist_t dict = NULL;
116 115
117 if (!client || !notification) { 116 if (!client || !notification) {
118 return IPHONE_E_INVALID_ARG; 117 return IPHONE_E_INVALID_ARG;
119 } 118 }
120 np_lock(client); 119 np_lock(client);
121 120
122 plist = new_plist(); 121 dict = plist_new_dict();
123 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 122 plist_add_sub_key_el(dict, "Command");
124 key = add_key_str_dict_element(plist, dict, "Command", "PostNotification", 1); 123 plist_add_sub_string_el(dict, "PostNotification");
125 key = add_key_str_dict_element(plist, dict, "Name", notification, 1); 124 plist_add_sub_key_el(dict, "Name");
126 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 125 plist_add_sub_string_el(dict, notification);
126 plist_to_xml(dict, &XML_content, &length);
127 127
128 nlen = htonl(length); 128 nlen = htonl(length);
129 129
@@ -132,13 +132,15 @@ iphone_error_t iphone_np_post_notification(iphone_np_client_t client, const char
132 memcpy(sndbuf + sndlen, XML_content, length); 132 memcpy(sndbuf + sndlen, XML_content, length);
133 sndlen += length; 133 sndlen += length;
134 134
135 xmlFree(XML_content); 135 plist_free(dict);
136 xmlFreeDoc(plist); 136 dict = NULL;
137 free(XML_content);
138 XML_content = NULL;
137 139
138 plist = new_plist(); 140 dict = plist_new_dict();
139 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 141 plist_add_sub_key_el(dict, "Command");
140 key = add_key_str_dict_element(plist, dict, "Command", "Shutdown", 1); 142 plist_add_sub_string_el(dict, "Shutdown");
141 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 143 plist_to_xml(dict, &XML_content, &length);
142 144
143 nlen = htonl(length); 145 nlen = htonl(length);
144 146
@@ -148,9 +150,10 @@ iphone_error_t iphone_np_post_notification(iphone_np_client_t client, const char
148 memcpy(sndbuf + sndlen, XML_content, length); 150 memcpy(sndbuf + sndlen, XML_content, length);
149 sndlen += length; 151 sndlen += length;
150 152
151 xmlFree(XML_content); 153 plist_free(dict);
152 xmlFreeDoc(plist); 154 dict = NULL;
153 plist = NULL; 155 free(XML_content);
156 XML_content = NULL;
154 157
155 log_debug_buffer(sndbuf, sndlen); 158 log_debug_buffer(sndbuf, sndlen);
156 159
@@ -181,17 +184,16 @@ iphone_error_t iphone_np_post_notification(iphone_np_client_t client, const char
181 */ 184 */
182iphone_error_t iphone_np_observe_notification(iphone_np_client_t client) 185iphone_error_t iphone_np_observe_notification(iphone_np_client_t client)
183{ 186{
184 xmlDocPtr plist; 187 plist_t dict = NULL;
185 xmlNode *dict, *key; 188 char *XML_content = NULL;
186 char *XML_content; 189 uint32_t length = 0;
187 uint32_t length; 190 int bytes = 0;
188 int bytes;
189 iphone_error_t ret; 191 iphone_error_t ret;
190 unsigned char sndbuf[4096]; 192 unsigned char sndbuf[4096];
191 int sndlen = 0; 193 int sndlen = 0;
192 int nlen; 194 int nlen = 0;
193 int i = 0; 195 int i = 0;
194 char *notifications[10] = { 196 const char *notifications[10] = {
195 "com.apple.itunes-client.syncCancelRequest", 197 "com.apple.itunes-client.syncCancelRequest",
196 "com.apple.itunes-client.syncSuspendRequest", 198 "com.apple.itunes-client.syncSuspendRequest",
197 "com.apple.itunes-client.syncResumeRequest", 199 "com.apple.itunes-client.syncResumeRequest",
@@ -212,11 +214,13 @@ iphone_error_t iphone_np_observe_notification(iphone_np_client_t client)
212 np_lock(client); 214 np_lock(client);
213 215
214 while (notifications[i]) { 216 while (notifications[i]) {
215 plist = new_plist(); 217
216 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 218 dict = plist_new_dict();
217 key = add_key_str_dict_element(plist, dict, "Command", "ObserveNotification", 1); 219 plist_add_sub_key_el(dict, "Command");
218 key = add_key_str_dict_element(plist, dict, "Name", notifications[i++], 1); 220 plist_add_sub_string_el(dict, "ObserveNotification");
219 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 221 plist_add_sub_key_el(dict, "Name");
222 plist_add_sub_string_el(dict, notifications[i++]);
223 plist_to_xml(dict, &XML_content, &length);
220 224
221 nlen = htonl(length); 225 nlen = htonl(length);
222 memcpy(sndbuf + sndlen, &nlen, 4); 226 memcpy(sndbuf + sndlen, &nlen, 4);
@@ -224,14 +228,16 @@ iphone_error_t iphone_np_observe_notification(iphone_np_client_t client)
224 memcpy(sndbuf + sndlen, XML_content, length); 228 memcpy(sndbuf + sndlen, XML_content, length);
225 sndlen += length; 229 sndlen += length;
226 230
227 xmlFree(XML_content); 231 plist_free(dict);
228 xmlFreeDoc(plist); 232 dict = NULL;
233 free(XML_content);
234 XML_content = NULL;
229 } 235 }
230 236
231 plist = new_plist(); 237 dict = plist_new_dict();
232 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 238 plist_add_sub_key_el(dict, "Command");
233 key = add_key_str_dict_element(plist, dict, "Command", "Shutdown", 1); 239 plist_add_sub_string_el(dict, "Shutdown");
234 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 240 plist_to_xml(dict, &XML_content, &length);
235 241
236 nlen = htonl(length); 242 nlen = htonl(length);
237 243
@@ -241,9 +247,10 @@ iphone_error_t iphone_np_observe_notification(iphone_np_client_t client)
241 memcpy(sndbuf + sndlen, XML_content, length); 247 memcpy(sndbuf + sndlen, XML_content, length);
242 sndlen += length; 248 sndlen += length;
243 249
244 xmlFree(XML_content); 250 plist_free(dict);
245 xmlFreeDoc(plist); 251 dict = NULL;
246 plist = NULL; 252 free(XML_content);
253 XML_content = NULL;
247 254
248 log_debug_buffer(sndbuf, sndlen); 255 log_debug_buffer(sndbuf, sndlen);
249 256
diff --git a/src/NotificationProxy.h b/src/NotificationProxy.h
index b2fa3d9..7b4b48d 100644
--- a/src/NotificationProxy.h
+++ b/src/NotificationProxy.h
@@ -18,6 +18,7 @@
18 * License along with this library; if not, write to the Free Software 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 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21#include "libiphone/libiphone.h"
21#include "usbmux.h" 22#include "usbmux.h"
22#include "iphone.h" 23#include "iphone.h"
23 24
diff --git a/src/initconf.c b/src/initconf.c
deleted file mode 100644
index 205c97a..0000000
--- a/src/initconf.c
+++ /dev/null
@@ -1,185 +0,0 @@
1/*
2 * userpref.c
3 * contains methods to access user specific certificates IDs and more.
4 *
5 * Copyright (c) 2008 Jonathan Beck 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 <stdlib.h>
24#include <string.h>
25#include <gnutls/gnutls.h>
26#include <gnutls/x509.h>
27#include <glib.h>
28
29#include "libiphone/libiphone.h"
30#include "userpref.h"
31#include "lockdown.h"
32#include "utils.h"
33
34/** Generates a 2048 byte key, split into a function so that it can be run in a
35 * thread.
36 *
37 * @param key The pointer to the desired location of the new key.
38 */
39static void generate_key(gpointer key)
40{
41 gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t *) key), GNUTLS_PK_RSA, 2048, 0);
42 g_thread_exit(0);
43}
44
45/** Simple function that generates a spinner until the mutex is released.
46 */
47static void progress_bar(gpointer mutex)
48{
49 const char *spinner = "|/-\\|/-\\";
50 int i = 0;
51
52 while (!g_static_mutex_trylock((GStaticMutex *) mutex)) {
53 usleep(500000);
54 printf("Generating key... %c\r", spinner[i++]);
55 fflush(stdout);
56 if (i > 8)
57 i = 0;
58 }
59 printf("Generating key... done\n");
60 g_thread_exit(0);
61}
62
63int main(int argc, char *argv[])
64{
65 GThread *progress_thread, *key_thread;
66 GError *err;
67 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
68 char *host_id = NULL;
69 gnutls_x509_privkey_t root_privkey;
70 gnutls_x509_privkey_t host_privkey;
71 gnutls_x509_crt_t root_cert;
72 gnutls_x509_crt_t host_cert;
73
74 iphone_set_debug(1);
75
76 // Create the thread
77 if (!g_thread_supported()) {
78 g_thread_init(NULL);
79 }
80 gnutls_global_init();
81
82 printf("This program generates keys required to connect with the iPhone\n");
83 printf("It only needs to be run ONCE.\n\n");
84 printf("Additionally it may take several minutes to run, please be patient.\n\n");
85
86
87 gnutls_x509_privkey_init(&root_privkey);
88 gnutls_x509_privkey_init(&host_privkey);
89
90 gnutls_x509_crt_init(&root_cert);
91 gnutls_x509_crt_init(&host_cert);
92
93 /* generate HostID */
94 host_id = lockdownd_generate_hostid();
95
96 /* generate root key */
97 g_static_mutex_lock(&mutex);
98 if ((key_thread = g_thread_create((GThreadFunc) generate_key, &root_privkey, TRUE, &err)) == NULL) {
99 printf("Thread create failed: %s!!\n", err->message);
100 g_error_free(err);
101 }
102 if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) {
103 printf("Thread create failed: %s!!\n", err->message);
104 g_error_free(err);
105 }
106 g_thread_join(key_thread);
107 g_static_mutex_unlock(&mutex);
108 g_thread_join(progress_thread);
109
110 /* generate host key */
111 g_static_mutex_init(&mutex);
112 g_static_mutex_lock(&mutex);
113 if ((key_thread = g_thread_create((GThreadFunc) generate_key, &host_privkey, TRUE, &err)) == NULL) {
114 printf("Thread create failed: %s!!\n", err->message);
115 g_error_free(err);
116 }
117 if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) {
118 printf("Thread create failed: %s!!\n", err->message);
119 g_error_free(err);
120 }
121 g_thread_join(key_thread);
122 g_static_mutex_unlock(&mutex);
123 g_thread_join(progress_thread);
124
125 /* generate certificates */
126 gnutls_x509_crt_set_key(root_cert, root_privkey);
127 gnutls_x509_crt_set_serial(root_cert, "\x00", 1);
128 gnutls_x509_crt_set_version(root_cert, 3);
129 gnutls_x509_crt_set_ca_status(root_cert, 1);
130 gnutls_x509_crt_set_activation_time(root_cert, time(NULL));
131 gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
132 gnutls_x509_crt_sign(root_cert, root_cert, root_privkey);
133
134
135 gnutls_x509_crt_set_key(host_cert, host_privkey);
136 gnutls_x509_crt_set_serial(host_cert, "\x00", 1);
137 gnutls_x509_crt_set_version(host_cert, 3);
138 gnutls_x509_crt_set_ca_status(host_cert, 0);
139 gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE);
140 gnutls_x509_crt_set_activation_time(host_cert, time(NULL));
141 gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
142 gnutls_x509_crt_sign(host_cert, root_cert, root_privkey);
143
144
145 /* export to PEM format */
146 gnutls_datum_t root_key_pem = { NULL, 0 };
147 gnutls_datum_t host_key_pem = { NULL, 0 };
148
149 gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size);
150 gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size);
151
152 root_key_pem.data = gnutls_malloc(root_key_pem.size);
153 host_key_pem.data = gnutls_malloc(host_key_pem.size);
154
155 gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size);
156 gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size);
157
158 gnutls_datum_t root_cert_pem = { NULL, 0 };
159 gnutls_datum_t host_cert_pem = { NULL, 0 };
160
161 gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size);
162 gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size);
163
164 root_cert_pem.data = gnutls_malloc(root_cert_pem.size);
165 host_cert_pem.data = gnutls_malloc(host_cert_pem.size);
166
167 printf("Generating root certificate...");
168 gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size);
169 printf("done\n");
170
171 printf("Generating host certificate...");
172 gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size);
173 printf("done\n");
174
175
176 /* store values in config file */
177 init_config_file(host_id, &root_key_pem, &host_key_pem, &root_cert_pem, &host_cert_pem);
178
179 gnutls_free(root_key_pem.data);
180 gnutls_free(host_key_pem.data);
181 gnutls_free(root_cert_pem.data);
182 gnutls_free(host_cert_pem.data);
183
184 return 0;
185}
diff --git a/src/lockdown.c b/src/lockdown.c
index 73a8bbc..e720b29 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -1,22 +1,22 @@
1/* 1/*
2 * lockdown.c 2 * lockdown.c
3 * libiphone built-in lockdownd client 3 * libiphone built-in lockdownd client
4 * 4 *
5 * Copyright (c) 2008 Zach C. All Rights Reserved. 5 * Copyright (c) 2008 Zach C. All Rights Reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version. 10 * version 2.1 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details. 15 * Lesser General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Lesser General Public 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 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 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22#include "usbmux.h" 22#include "usbmux.h"
@@ -31,6 +31,9 @@
31#include <libtasn1.h> 31#include <libtasn1.h>
32#include <gnutls/x509.h> 32#include <gnutls/x509.h>
33 33
34#include <plist/plist.h>
35
36
34const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { 37const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
35 {"PKCS1", 536872976, 0}, 38 {"PKCS1", 536872976, 0},
36 {0, 1073741836, 0}, 39 {0, 1073741836, 0},
@@ -40,35 +43,6 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
40 {0, 0, 0} 43 {0, 0, 0}
41}; 44};
42 45
43static int get_rand(int min, int max)
44{
45 int retval = (rand() % (max - min)) + min;
46 return retval;
47}
48
49/** Generates a valid HostID (which is actually a UUID).
50 *
51 * @param A null terminated string containing a valid HostID.
52 */
53char *lockdownd_generate_hostid(void)
54{
55 char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long
56 const char *chars = "ABCDEF0123456789";
57 srand(time(NULL));
58 int i = 0;
59
60 for (i = 0; i < 36; i++) {
61 if (i == 8 || i == 13 || i == 18 || i == 23) {
62 hostid[i] = '-';
63 continue;
64 } else {
65 hostid[i] = chars[get_rand(0, 16)];
66 }
67 }
68 hostid[36] = '\0'; // make it a real string
69 return hostid;
70}
71
72/** Creates a lockdownd client for the give iPhone. 46/** Creates a lockdownd client for the give iPhone.
73 * 47 *
74 * @param phone The iPhone to create a lockdownd client for 48 * @param phone The iPhone to create a lockdownd client for
@@ -93,69 +67,68 @@ iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone)
93 67
94/** 68/**
95 * Closes the lockdownd communication session, by sending 69 * Closes the lockdownd communication session, by sending
96 * the StopSession Request to the device. 70 * the StopSession Request to the device.
97 * 71 *
98 * @param control The lockdown client 72 * @param control The lockdown client
99 */ 73 */
100static void iphone_lckd_stop_session(iphone_lckd_client_t control) 74static void iphone_lckd_stop_session(iphone_lckd_client_t control)
101{ 75{
102 if (!control) 76 if (!control)
103 return; // IPHONE_E_INVALID_ARG; 77 return; //IPHONE_E_INVALID_ARG;
104 xmlDocPtr plist = new_plist(); 78
105 xmlNode *dict, *key;
106 char **dictionary;
107 int bytes = 0, i = 0;
108 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 79 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
109 80
110 log_debug_msg("lockdownd_stop_session() called\n"); 81 plist_t dict = plist_new_dict();
111 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 82 plist_add_sub_key_el(dict, "Request");
112 key = add_key_str_dict_element(plist, dict, "Request", "StopSession", 1); 83 plist_add_sub_string_el(dict, "StopSession");
113 key = add_key_str_dict_element(plist, dict, "SessionID", control->session_id, 1); 84 plist_add_sub_key_el(dict, "SessionID");
85 plist_add_sub_string_el(dict, control->session_id);
114 86
115 char *XML_content; 87 log_dbg_msg(DBGMASK_LOCKDOWND, "iphone_lckd_stop_session() called\n");
116 uint32 length;
117 88
118 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 89 ret = iphone_lckd_send(control, dict);
119 ret = iphone_lckd_send(control, XML_content, length, &bytes);
120 90
121 xmlFree(XML_content); 91 plist_free(dict);
122 xmlFreeDoc(plist); 92 dict = NULL;
123 plist = NULL; 93
124 ret = iphone_lckd_recv(control, &XML_content, &bytes); 94 ret = iphone_lckd_recv(control, &dict);
125 95
126 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
127 if (!plist) {
128 log_debug_msg("lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n");
129 return; //IPHONE_E_PLIST_ERROR;
130 }
131 dict = xmlDocGetRootElement(plist);
132 for (dict = dict->children; dict; dict = dict->next) {
133 if (!xmlStrcmp(dict->name, "dict"))
134 break;
135 }
136 if (!dict) { 96 if (!dict) {
137 log_debug_msg("lockdownd_stop_session(): IPHONE_E_DICT_ERROR\n"); 97 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n");
138 return; //IPHONE_E_DICT_ERROR; 98 return; // IPHONE_E_PLIST_ERROR;
139 } 99 }
140 dictionary = read_dict_element_strings(dict);
141 xmlFreeDoc(plist);
142 free(XML_content);
143 100
144 for (i = 0; dictionary[i]; i += 2) { 101 plist_t query_node = plist_find_node_by_string(dict, "StopSession");
145 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 102 plist_t result_node = plist_get_next_sibling(query_node);
146 log_debug_msg("lockdownd_stop_session(): success\n"); 103 plist_t value_node = plist_get_next_sibling(result_node);
104
105 plist_type result_type = plist_get_node_type(result_node);
106 plist_type value_type = plist_get_node_type(value_node);
107
108 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
109
110 char *result_value = NULL;
111 char *value_value = NULL;
112
113 plist_get_key_val(result_node, &result_value);
114 plist_get_string_val(value_node, &value_value);
115
116 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
117 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): success\n");
147 ret = IPHONE_E_SUCCESS; 118 ret = IPHONE_E_SUCCESS;
148 break;
149 } 119 }
120 free(result_value);
121 free(value_value);
150 } 122 }
123 plist_free(dict);
124 dict = NULL;
151 125
152 free_dictionary(dictionary); 126 return; // ret;
153 return; //ret;
154} 127}
155 128
156/** 129/**
157 * Shuts down the SSL session by first calling iphone_lckd_stop_session 130 * Shuts down the SSL session by first calling iphone_lckd_stop_session
158 * to cleanly close the lockdownd communication session, and then 131 * to cleanly close the lockdownd communication session, and then
159 * performing a close notify, which is done by "gnutls_bye". 132 * performing a close notify, which is done by "gnutls_bye".
160 * 133 *
161 * @param client The lockdown client 134 * @param client The lockdown client
@@ -163,14 +136,14 @@ static void iphone_lckd_stop_session(iphone_lckd_client_t control)
163static void iphone_lckd_stop_SSL_session(iphone_lckd_client_t client) 136static void iphone_lckd_stop_SSL_session(iphone_lckd_client_t client)
164{ 137{
165 if (!client) { 138 if (!client) {
166 log_debug_msg("lockdownd_stop_SSL_session(): invalid argument!\n"); 139 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_SSL_session(): invalid argument!\n");
167 return; 140 return;
168 } 141 }
169 142
170 if (client->in_SSL) { 143 if (client->in_SSL) {
171 log_debug_msg("Stopping SSL Session\n"); 144 log_dbg_msg(DBGMASK_LOCKDOWND, "Stopping SSL Session\n");
172 iphone_lckd_stop_session(client); 145 iphone_lckd_stop_session(client);
173 log_debug_msg("Sending SSL close notify\n"); 146 log_dbg_msg(DBGMASK_LOCKDOWND, "Sending SSL close notify\n");
174 gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR); 147 gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR);
175 } 148 }
176 if (client->ssl_session) { 149 if (client->ssl_session) {
@@ -215,13 +188,13 @@ iphone_error_t iphone_lckd_free_client(iphone_lckd_client_t client)
215 * 188 *
216 * @return The number of bytes received 189 * @return The number of bytes received
217 */ 190 */
218iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, uint32_t * recv_bytes) 191iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, plist_t * plist)
219{ 192{
220 if (!client || !dump_data || !recv_bytes) 193 if (!client || !plist || (plist && *plist))
221 return IPHONE_E_INVALID_ARG; 194 return IPHONE_E_INVALID_ARG;
222 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 195 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
223 char *receive; 196 char *receive;
224 uint32 datalen = 0, bytes = 0; 197 uint32_t datalen = 0, bytes = 0;
225 198
226 if (!client->in_SSL) 199 if (!client->in_SSL)
227 ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes); 200 ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes);
@@ -240,36 +213,52 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, u
240 if (bytes > 0) 213 if (bytes > 0)
241 ret = IPHONE_E_SUCCESS; 214 ret = IPHONE_E_SUCCESS;
242 } 215 }
243 *dump_data = receive; 216
244 *recv_bytes = bytes; 217 if (bytes <= 0) {
218 free(receive);
219 return IPHONE_E_NOT_ENOUGH_DATA;
220 }
221
222 log_dbg_msg(DBGMASK_LOCKDOWND, "Recv msg :\nsize : %i\nbuffer :\n%s\n", bytes, receive);
223 plist_from_xml(receive, bytes, plist);
224 free(receive);
225
226 if (!*plist)
227 ret = IPHONE_E_PLIST_ERROR;
228
245 return ret; 229 return ret;
246} 230}
247 231
248/** Sends lockdownd data to the iPhone 232/** Sends lockdownd data to the iPhone
249 * 233 *
250 * @note This function is low-level and should only be used if you need to send 234 * @note This function is low-level and should only be used if you need to send
251 * a new type of message. 235 * a new type of message.
252 * 236 *
253 * @param control The lockdownd client 237 * @param client The lockdownd client
254 * @param raw_data The null terminated string buffer to send 238 * @param plist The plist to send
255 * @param length The length of data to send
256 * 239 *
257 * @return The number of bytes sent 240 * @return an error code (IPHONE_E_SUCCESS on success)
258 */ 241 */
259iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t * sent_bytes) 242iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, plist_t plist)
260{ 243{
261 if (!client || !raw_data || length == 0 || !sent_bytes) 244 if (!client || !plist)
262 return IPHONE_E_INVALID_ARG; 245 return IPHONE_E_INVALID_ARG;
263 char *real_query; 246 char *real_query;
264 int bytes; 247 int bytes;
248 char *XMLContent = NULL;
249 uint32_t length = 0;
265 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 250 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
266 251
252 plist_to_xml(plist, &XMLContent, &length);
253 log_dbg_msg(DBGMASK_LOCKDOWND, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent);
254
255
267 real_query = (char *) malloc(sizeof(char) * (length + 4)); 256 real_query = (char *) malloc(sizeof(char) * (length + 4));
268 length = htonl(length); 257 length = htonl(length);
269 memcpy(real_query, &length, sizeof(length)); 258 memcpy(real_query, &length, sizeof(length));
270 memcpy(real_query + 4, raw_data, ntohl(length)); 259 memcpy(real_query + 4, XMLContent, ntohl(length));
271 log_debug_msg("lockdownd_send(): made the query, sending it along\n"); 260 free(XMLContent);
272 dump_debug_buffer("grpkt", real_query, ntohl(length) + 4); 261 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): made the query, sending it along\n");
273 262
274 if (!client->in_SSL) 263 if (!client->in_SSL)
275 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); 264 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes);
@@ -277,14 +266,14 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin
277 gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); 266 gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length));
278 ret = IPHONE_E_SUCCESS; 267 ret = IPHONE_E_SUCCESS;
279 } 268 }
280 log_debug_msg("lockdownd_send(): sent it!\n"); 269 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): sent it!\n");
281 free(real_query); 270 free(real_query);
282 *sent_bytes = bytes; 271
283 return ret; 272 return ret;
284} 273}
285 274
286/** Initiates the handshake for the lockdown session. Part of the lockdownd handshake. 275/** Initiates the handshake for the lockdown session. Part of the lockdownd handshake.
287 * 276 *
288 * @note You most likely want lockdownd_init unless you are doing something special. 277 * @note You most likely want lockdownd_init unless you are doing something special.
289 * 278 *
290 * @param control The lockdownd client 279 * @param control The lockdownd client
@@ -295,49 +284,50 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
295{ 284{
296 if (!control) 285 if (!control)
297 return IPHONE_E_INVALID_ARG; 286 return IPHONE_E_INVALID_ARG;
298 xmlDocPtr plist = new_plist(); 287
299 xmlNode *dict, *key;
300 char **dictionary;
301 int bytes = 0, i = 0;
302 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 288 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
303 289
304 log_debug_msg("lockdownd_hello() called\n"); 290 plist_t dict = plist_new_dict();
305 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 291 plist_add_sub_key_el(dict, "Request");
306 key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); 292 plist_add_sub_string_el(dict, "QueryType");
307 char *XML_content;
308 uint32 length;
309 293
310 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 294 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_hello() called\n");
311 ret = iphone_lckd_send(control, XML_content, length, &bytes); 295 ret = iphone_lckd_send(control, dict);
312 296
313 xmlFree(XML_content); 297 plist_free(dict);
314 xmlFreeDoc(plist); 298 dict = NULL;
315 plist = NULL;
316 ret = iphone_lckd_recv(control, &XML_content, &bytes);
317 299
318 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 300 ret = iphone_lckd_recv(control, &dict);
319 if (!plist) 301
320 return IPHONE_E_PLIST_ERROR; 302 if (IPHONE_E_SUCCESS != ret)
321 dict = xmlDocGetRootElement(plist); 303 return ret;
322 for (dict = dict->children; dict; dict = dict->next) { 304
323 if (!xmlStrcmp(dict->name, "dict")) 305 plist_t query_node = plist_find_node_by_string(dict, "QueryType");
324 break; 306 plist_t result_node = plist_get_next_sibling(query_node);
325 } 307 plist_t value_node = plist_get_next_sibling(result_node);
326 if (!dict) 308
327 return IPHONE_E_DICT_ERROR; 309 plist_type result_type = plist_get_node_type(result_node);
328 dictionary = read_dict_element_strings(dict); 310 plist_type value_type = plist_get_node_type(value_node);
329 xmlFreeDoc(plist); 311
330 free(XML_content); 312 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
331 313
332 for (i = 0; dictionary[i]; i += 2) { 314 char *result_value = NULL;
333 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 315 char *value_value = NULL;
334 log_debug_msg("lockdownd_hello(): success\n"); 316
317 plist_get_key_val(result_node, &result_value);
318 plist_get_string_val(value_node, &value_value);
319
320 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
321 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_hello(): success\n");
335 ret = IPHONE_E_SUCCESS; 322 ret = IPHONE_E_SUCCESS;
336 break;
337 } 323 }
324 free(result_value);
325 free(value_value);
338 } 326 }
339 327
340 free_dictionary(dictionary); 328 plist_free(dict);
329 dict = NULL;
330
341 return ret; 331 return ret;
342} 332}
343 333
@@ -350,73 +340,100 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
350 * @return IPHONE_E_SUCCESS on success. 340 * @return IPHONE_E_SUCCESS on success.
351 */ 341 */
352iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, const char *req_string, 342iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, const char *req_string,
353 char **value) 343 gnutls_datum_t * value)
354{ 344{
355 if (!control || !req_key || !value || (value && *value)) 345 if (!control || !req_key || !value || value->data)
356 return IPHONE_E_INVALID_ARG; 346 return IPHONE_E_INVALID_ARG;
357 xmlDocPtr plist = new_plist(); 347
358 xmlNode *dict = NULL; 348 plist_t dict = NULL;
359 xmlNode *key = NULL;;
360 char **dictionary = NULL;
361 int bytes = 0, i = 0;
362 char *XML_content = NULL;
363 uint32 length = 0;
364 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 349 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
365 350
366 /* Setup DevicePublicKey request plist */ 351 /* Setup DevicePublicKey request plist */
367 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 352 dict = plist_new_dict();
368 key = add_key_str_dict_element(plist, dict, req_key, req_string, 1); 353 plist_add_sub_key_el(dict, req_key);
369 key = add_key_str_dict_element(plist, dict, "Request", "GetValue", 1); 354 plist_add_sub_string_el(dict, req_string);
370 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 355 plist_add_sub_key_el(dict, "Request");
356 plist_add_sub_string_el(dict, "GetValue");
371 357
372 /* send to iPhone */ 358 /* send to iPhone */
373 ret = iphone_lckd_send(control, XML_content, length, &bytes); 359 ret = iphone_lckd_send(control, dict);
374 360
375 xmlFree(XML_content); 361 plist_free(dict);
376 xmlFreeDoc(plist); 362 dict = NULL;
377 plist = NULL;
378 363
379 if (ret != IPHONE_E_SUCCESS) 364 if (ret != IPHONE_E_SUCCESS)
380 return ret; 365 return ret;
381 366
382 /* Now get iPhone's answer */ 367 /* Now get iPhone's answer */
383 ret = iphone_lckd_recv(control, &XML_content, &bytes); 368 ret = iphone_lckd_recv(control, &dict);
384 369
385 if (ret != IPHONE_E_SUCCESS) 370 if (ret != IPHONE_E_SUCCESS)
386 return ret; 371 return ret;
387 372
388 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 373 plist_t query_node = plist_find_node_by_string(dict, "GetValue");
389 if (!plist) 374 plist_t result_key_node = plist_get_next_sibling(query_node);
390 return IPHONE_E_PLIST_ERROR; 375 plist_t result_value_node = plist_get_next_sibling(result_key_node);
391 dict = xmlDocGetRootElement(plist);
392 for (dict = dict->children; dict; dict = dict->next) {
393 if (!xmlStrcmp(dict->name, "dict"))
394 break;
395 }
396 if (!dict)
397 return IPHONE_E_DICT_ERROR;
398 376
399 /* Parse xml to check success and to find public key */ 377 plist_type result_key_type = plist_get_node_type(result_key_node);
400 dictionary = read_dict_element_strings(dict); 378 plist_type result_value_type = plist_get_node_type(result_value_node);
401 xmlFreeDoc(plist);
402 free(XML_content);
403 379
404 int success = 0; 380 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
405 for (i = 0; dictionary[i]; i += 2) { 381
406 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 382 char *result_key = NULL;
407 success = 1; 383 char *result_value = NULL;
408 } 384 ret = IPHONE_E_DICT_ERROR;
409 if (!strcmp(dictionary[i], "Value")) { 385
410 *value = strdup(dictionary[i + 1]); 386 plist_get_key_val(result_key_node, &result_key);
387 plist_get_string_val(result_value_node, &result_value);
388
389 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
390 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n");
391 ret = IPHONE_E_SUCCESS;
411 } 392 }
393 free(result_key);
394 free(result_value);
395 }
396 if (ret != IPHONE_E_SUCCESS) {
397 return ret;
412 } 398 }
413 399
414 if (dictionary) { 400 plist_t value_key_node = plist_find_node_by_key(dict, "Value");//plist_get_next_sibling(result_value_node);
415 free_dictionary(dictionary); 401 plist_t value_value_node = plist_get_next_sibling(value_key_node);
416 dictionary = NULL; 402
403 plist_type value_key_type = plist_get_node_type(value_key_node);
404
405 if (value_key_type == PLIST_KEY) {
406
407 char *result_key = NULL;
408 plist_get_key_val(value_key_node, &result_key);
409
410 if (!strcmp(result_key, "Value")) {
411 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n");
412
413 plist_type value_value_type = plist_get_node_type(value_value_node);
414 if (PLIST_STRING == value_value_type) {
415 char *value_value = NULL;
416 plist_get_string_val(value_value_node, &value_value);
417
418 value->data = value_value;
419 value->size = strlen(value_value);
420 ret = IPHONE_E_SUCCESS;
421 }
422
423 if (PLIST_DATA == value_value_type) {
424 char *value_value = NULL;
425 uint64_t size = 0;
426 plist_get_data_val(value_value_node, &value_value, &size);
427
428 value->data = value_value;
429 value->size = size;
430 ret = IPHONE_E_SUCCESS;
431 }
432 }
433 free(result_key);
417 } 434 }
418 if (success) 435
419 ret = IPHONE_E_SUCCESS; 436 plist_free(dict);
420 return ret; 437 return ret;
421} 438}
422 439
@@ -428,7 +445,10 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c
428 */ 445 */
429iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid) 446iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid)
430{ 447{
431 return lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", uid); 448 gnutls_datum_t temp = { NULL, 0 };
449 iphone_error_t ret = lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", &temp);
450 *uid = temp.data;
451 return ret;
432} 452}
433 453
434/** Askes for the device's public key. Part of the lockdownd handshake. 454/** Askes for the device's public key. Part of the lockdownd handshake.
@@ -437,7 +457,7 @@ iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid
437 * 457 *
438 * @return 1 on success and 0 on failure. 458 * @return 1 on success and 0 on failure.
439 */ 459 */
440iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key) 460iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, gnutls_datum_t * public_key)
441{ 461{
442 return lockdownd_generic_get_value(control, "Key", "DevicePublicKey", public_key); 462 return lockdownd_generic_get_value(control, "Key", "DevicePublicKey", public_key);
443} 463}
@@ -472,6 +492,7 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client
472 if (IPHONE_E_SUCCESS != ret) { 492 if (IPHONE_E_SUCCESS != ret) {
473 log_debug_msg("Device refused to send uid.\n"); 493 log_debug_msg("Device refused to send uid.\n");
474 } 494 }
495 log_debug_msg("Device uid: %s\n", uid);
475 496
476 host_id = get_host_id(); 497 host_id = get_host_id();
477 if (IPHONE_E_SUCCESS == ret && !host_id) { 498 if (IPHONE_E_SUCCESS == ret && !host_id) {
@@ -487,19 +508,22 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client
487 uid = NULL; 508 uid = NULL;
488 } 509 }
489 510
490 ret = lockdownd_start_SSL_session(client_loc, host_id); 511 if (IPHONE_E_SUCCESS == ret) {
491 if (IPHONE_E_SUCCESS != ret) { 512 ret = lockdownd_start_SSL_session(client_loc, host_id);
492 ret = IPHONE_E_SSL_ERROR; 513 if (IPHONE_E_SUCCESS != ret) {
493 log_debug_msg("SSL Session opening failed.\n"); 514 ret = IPHONE_E_SSL_ERROR;
494 } 515 log_debug_msg("SSL Session opening failed.\n");
516 }
517
518 if (host_id) {
519 free(host_id);
520 host_id = NULL;
521 }
495 522
496 if (host_id) { 523 if (IPHONE_E_SUCCESS == ret)
497 free(host_id); 524 *client = client_loc;
498 host_id = NULL;
499 } 525 }
500 526
501 if (IPHONE_E_SUCCESS == ret)
502 *client = client_loc;
503 return ret; 527 return ret;
504} 528}
505 529
@@ -513,107 +537,92 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client
513iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id) 537iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id)
514{ 538{
515 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 539 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
516 xmlDocPtr plist = new_plist(); 540 plist_t dict = NULL;
517 xmlNode *dict = NULL; 541 plist_t dict_record = NULL;
518 xmlNode *dictRecord = NULL; 542
519 char **dictionary = NULL; 543 gnutls_datum_t device_cert = { NULL, 0 };
520 int bytes = 0, i = 0; 544 gnutls_datum_t host_cert = { NULL, 0 };
521 char *XML_content = NULL; 545 gnutls_datum_t root_cert = { NULL, 0 };
522 uint32 length = 0; 546 gnutls_datum_t public_key = { NULL, 0 };
523 547
524 char *device_cert_b64 = NULL; 548 ret = lockdownd_get_device_public_key(control, &public_key);
525 char *host_cert_b64 = NULL;
526 char *root_cert_b64 = NULL;
527 char *public_key_b64 = NULL;
528
529 ret = lockdownd_get_device_public_key(control, &public_key_b64);
530 if (ret != IPHONE_E_SUCCESS) { 549 if (ret != IPHONE_E_SUCCESS) {
531 log_debug_msg("Device refused to send public key.\n"); 550 log_debug_msg("Device refused to send public key.\n");
532 return ret; 551 return ret;
533 } 552 }
553 log_debug_msg("device public key :\n %s.\n", public_key.data);
534 554
535 ret = lockdownd_gen_pair_cert(public_key_b64, &device_cert_b64, &host_cert_b64, &root_cert_b64); 555 ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert);
536 if (ret != IPHONE_E_SUCCESS) { 556 if (ret != IPHONE_E_SUCCESS) {
537 free(public_key_b64); 557 free(public_key.data);
538 return ret; 558 return ret;
539 } 559 }
540 560
541 /* Setup Pair request plist */ 561 /* Setup Pair request plist */
542 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 562 dict = plist_new_dict();
543 dictRecord = add_key_dict_node(plist, dict, "PairRecord", "\n", 1); 563 plist_add_sub_key_el(dict, "PairRecord");
544 //dictRecord = add_child_to_plist(plist, "dict", "\n", NULL, 1); 564 dict_record = plist_new_dict();
545 add_key_data_dict_element(plist, dictRecord, "DeviceCertificate", device_cert_b64, 2); 565 plist_add_sub_node(dict, dict_record);
546 add_key_data_dict_element(plist, dictRecord, "HostCertificate", host_cert_b64, 2); 566 plist_add_sub_key_el(dict_record, "DeviceCertificate");
547 add_key_str_dict_element(plist, dictRecord, "HostID", host_id, 2); 567 plist_add_sub_data_el(dict_record, (const char*)device_cert.data, device_cert.size);
548 add_key_data_dict_element(plist, dictRecord, "RootCertificate", root_cert_b64, 2); 568 plist_add_sub_key_el(dict_record, "HostCertificate");
549 add_key_str_dict_element(plist, dict, "Request", "Pair", 1); 569 plist_add_sub_data_el(dict_record, (const char*)host_cert.data, host_cert.size);
550 570 plist_add_sub_key_el(dict_record, "HostID");
551 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 571 plist_add_sub_string_el(dict_record, host_id);
552 572 plist_add_sub_key_el(dict_record, "RootCertificate");
553 printf("XML Pairing request : %s\n", XML_content); 573 plist_add_sub_data_el(dict_record, (const char*)root_cert.data, root_cert.size);
574 plist_add_sub_key_el(dict, "Request");
575 plist_add_sub_string_el(dict, "Pair");
554 576
555 /* send to iPhone */ 577 /* send to iPhone */
556 ret = iphone_lckd_send(control, XML_content, length, &bytes); 578 ret = iphone_lckd_send(control, dict);
557 579 plist_free(dict);
558 xmlFree(XML_content); 580 dict = NULL;
559 xmlFreeDoc(plist);
560 plist = NULL;
561 581
562 if (ret != IPHONE_E_SUCCESS) 582 if (ret != IPHONE_E_SUCCESS)
563 return ret; 583 return ret;
564 584
565 /* Now get iPhone's answer */ 585 /* Now get iPhone's answer */
566 ret = iphone_lckd_recv(control, &XML_content, &bytes); 586 ret = iphone_lckd_recv(control, &dict);
567 587
568 if (ret != IPHONE_E_SUCCESS) 588 if (ret != IPHONE_E_SUCCESS)
569 return ret; 589 return ret;
570 590
571 log_debug_msg("lockdown_pair_device: iPhone's response to our pair request:\n"); 591 plist_t query_node = plist_find_node_by_string(dict, "Pair");
572 log_debug_msg(XML_content); 592 plist_t result_key_node = plist_get_next_sibling(query_node);
573 log_debug_msg("\n\n"); 593 plist_t result_value_node = plist_get_next_sibling(result_key_node);
574 594
575 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 595 plist_type result_key_type = plist_get_node_type(result_key_node);
576 if (!plist) { 596 plist_type result_value_type = plist_get_node_type(result_value_node);
577 free(public_key_b64);
578 return IPHONE_E_PLIST_ERROR;
579 }
580 dict = xmlDocGetRootElement(plist);
581 for (dict = dict->children; dict; dict = dict->next) {
582 if (!xmlStrcmp(dict->name, "dict"))
583 break;
584 }
585 if (!dict) {
586 free(public_key_b64);
587 return IPHONE_E_DICT_ERROR;
588 }
589 597
590 /* Parse xml to check success and to find public key */ 598 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
591 dictionary = read_dict_element_strings(dict);
592 xmlFreeDoc(plist);
593 free(XML_content);
594 599
595 int success = 0; 600 char *result_key = NULL;
596 for (i = 0; dictionary[i]; i += 2) { 601 char *result_value = NULL;
597 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 602
598 success = 1; 603 plist_get_key_val(result_key_node, &result_key);
604 plist_get_string_val(result_value_node, &result_value);
605
606 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
607 ret = IPHONE_E_SUCCESS;
599 } 608 }
600 }
601 609
602 if (dictionary) { 610 free(result_key);
603 free_dictionary(dictionary); 611 free(result_value);
604 dictionary = NULL;
605 } 612 }
613 plist_free(dict);
614 dict = NULL;
606 615
607 /* store public key in config if pairing succeeded */ 616 /* store public key in config if pairing succeeded */
608 if (success) { 617 if (ret == IPHONE_E_SUCCESS) {
609 log_debug_msg("lockdownd_pair_device: pair success\n"); 618 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair_device: pair success\n");
610 store_device_public_key(uid, public_key_b64); 619 store_device_public_key(uid, public_key);
611 ret = IPHONE_E_SUCCESS; 620 ret = IPHONE_E_SUCCESS;
612 } else { 621 } else {
613 log_debug_msg("lockdownd_pair_device: pair failure\n"); 622 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair_device: pair failure\n");
614 ret = IPHONE_E_PAIRING_FAILED; 623 ret = IPHONE_E_PAIRING_FAILED;
615 } 624 }
616 free(public_key_b64); 625 free(public_key.data);
617 return ret; 626 return ret;
618} 627}
619 628
@@ -626,81 +635,71 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
626void lockdownd_close(iphone_lckd_client_t control) 635void lockdownd_close(iphone_lckd_client_t control)
627{ 636{
628 if (!control) 637 if (!control)
629 return; // IPHONE_E_INVALID_ARG; 638 return; //IPHONE_E_INVALID_ARG;
630 xmlDocPtr plist = new_plist(); 639
631 xmlNode *dict, *key;
632 char **dictionary;
633 int bytes = 0, i = 0;
634 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 640 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
635 641
636 log_debug_msg("lockdownd_close() called\n"); 642 plist_t dict = plist_new_dict();
637 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 643 plist_add_sub_key_el(dict, "Request");
638 key = add_key_str_dict_element(plist, dict, "Request", "Goodbye", 1); 644 plist_add_sub_string_el(dict, "Goodbye");
639 char *XML_content;
640 uint32 length;
641 645
642 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 646 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close() called\n");
643 ret = iphone_lckd_send(control, XML_content, length, &bytes);
644 647
645 xmlFree(XML_content); 648 ret = iphone_lckd_send(control, dict);
646 xmlFreeDoc(plist); 649 plist_free(dict);
647 plist = NULL; 650 dict = NULL;
648 ret = iphone_lckd_recv(control, &XML_content, &bytes); 651
652 ret = iphone_lckd_recv(control, &dict);
649 653
650 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
651 if (!plist) {
652 log_debug_msg("lockdownd_close(): IPHONE_E_PLIST_ERROR\n");
653 return; //IPHONE_E_PLIST_ERROR;
654 }
655 dict = xmlDocGetRootElement(plist);
656 for (dict = dict->children; dict; dict = dict->next) {
657 if (!xmlStrcmp(dict->name, "dict"))
658 break;
659 }
660 if (!dict) { 654 if (!dict) {
661 log_debug_msg("lockdownd_close(): IPHONE_E_DICT_ERROR\n"); 655 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close(): IPHONE_E_PLIST_ERROR\n");
662 return; //IPHONE_E_DICT_ERROR; 656 return; // IPHONE_E_PLIST_ERROR;
663 } 657 }
664 dictionary = read_dict_element_strings(dict);
665 xmlFreeDoc(plist);
666 free(XML_content);
667 658
668 for (i = 0; dictionary[i]; i += 2) { 659 plist_t query_node = plist_find_node_by_string(dict, "Goodbye");
669 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 660 plist_t result_node = plist_get_next_sibling(query_node);
670 log_debug_msg("lockdownd_close(): success\n"); 661 plist_t value_node = plist_get_next_sibling(result_node);
662
663 plist_type result_type = plist_get_node_type(result_node);
664 plist_type value_type = plist_get_node_type(value_node);
665
666 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
667 char *result_value = NULL;
668 char *value_value = NULL;
669
670 plist_get_key_val(result_node, &result_value);
671 plist_get_string_val(value_node, &value_value);
672
673 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
674 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close(): success\n");
671 ret = IPHONE_E_SUCCESS; 675 ret = IPHONE_E_SUCCESS;
672 break;
673 } 676 }
677 free(result_value);
678 free(value_value);
674 } 679 }
675 680 plist_free(dict);
676 free_dictionary(dictionary); 681 dict = NULL;
677 return; //ret; 682 return; // ret;
678} 683}
679 684
680/** Generates the device certificate from the public key as well as the host 685/** Generates the device certificate from the public key as well as the host
681 * and root certificates. 686 * and root certificates.
682 * 687 *
683 * @return IPHONE_E_SUCCESS on success. 688 * @return IPHONE_E_SUCCESS on success.
684 */ 689 */
685iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, 690iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert,
686 char **root_cert_b64) 691 gnutls_datum_t * ohost_cert, gnutls_datum_t * oroot_cert)
687{ 692{
688 if (!public_key_b64 || !device_cert_b64 || !host_cert_b64 || !root_cert_b64) 693 if (!public_key.data || !odevice_cert || !ohost_cert || !oroot_cert)
689 return IPHONE_E_INVALID_ARG; 694 return IPHONE_E_INVALID_ARG;
690 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 695 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
691 696
692 gnutls_datum_t modulus = { NULL, 0 }; 697 gnutls_datum_t modulus = { NULL, 0 };
693 gnutls_datum_t exponent = { NULL, 0 }; 698 gnutls_datum_t exponent = { NULL, 0 };
694 699
695 /* first decode base64 public_key */
696 gnutls_datum_t pem_pub_key;
697 gsize decoded_size;
698 pem_pub_key.data = g_base64_decode(public_key_b64, &decoded_size);
699 pem_pub_key.size = decoded_size;
700
701 /* now decode the PEM encoded key */ 700 /* now decode the PEM encoded key */
702 gnutls_datum_t der_pub_key; 701 gnutls_datum_t der_pub_key;
703 if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &pem_pub_key, &der_pub_key)) { 702 if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &public_key, &der_pub_key)) {
704 703
705 /* initalize asn.1 parser */ 704 /* initalize asn.1 parser */
706 ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY; 705 ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY;
@@ -736,7 +735,7 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
736 gnutls_global_init(); 735 gnutls_global_init();
737 gnutls_datum_t essentially_null = { strdup("abababababababab"), strlen("abababababababab") }; 736 gnutls_datum_t essentially_null = { strdup("abababababababab"), strlen("abababababababab") };
738 737
739 gnutls_x509_privkey_t fake_privkey, root_privkey; 738 gnutls_x509_privkey_t fake_privkey, root_privkey, host_privkey;
740 gnutls_x509_crt_t dev_cert, root_cert, host_cert; 739 gnutls_x509_crt_t dev_cert, root_cert, host_cert;
741 740
742 gnutls_x509_privkey_init(&fake_privkey); 741 gnutls_x509_privkey_init(&fake_privkey);
@@ -749,49 +748,50 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
749 &essentially_null, &essentially_null)) { 748 &essentially_null, &essentially_null)) {
750 749
751 gnutls_x509_privkey_init(&root_privkey); 750 gnutls_x509_privkey_init(&root_privkey);
751 gnutls_x509_privkey_init(&host_privkey);
752 752
753 /* get root cert */ 753 ret = get_keys_and_certs( root_privkey, root_cert, host_privkey, host_cert);
754 gnutls_datum_t pem_root_cert = { NULL, 0 };
755 get_root_certificate(&pem_root_cert);
756 if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(root_cert, &pem_root_cert, GNUTLS_X509_FMT_PEM))
757 ret = IPHONE_E_SSL_ERROR;
758
759 /* get host cert */
760 gnutls_datum_t pem_host_cert = { NULL, 0 };
761 get_host_certificate(&pem_host_cert);
762 if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(host_cert, &pem_host_cert, GNUTLS_X509_FMT_PEM))
763 ret = IPHONE_E_SSL_ERROR;
764
765 /* get root private key */
766 gnutls_datum_t pem_root_priv = { NULL, 0 };
767 get_root_private_key(&pem_root_priv);
768 if (GNUTLS_E_SUCCESS != gnutls_x509_privkey_import(root_privkey, &pem_root_priv, GNUTLS_X509_FMT_PEM))
769 ret = IPHONE_E_SSL_ERROR;
770
771 /* generate device certificate */
772 gnutls_x509_crt_set_key(dev_cert, fake_privkey);
773 gnutls_x509_crt_set_serial(dev_cert, "\x00", 1);
774 gnutls_x509_crt_set_version(dev_cert, 3);
775 gnutls_x509_crt_set_ca_status(dev_cert, 0);
776 gnutls_x509_crt_set_activation_time(dev_cert, time(NULL));
777 gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
778 gnutls_x509_crt_sign(dev_cert, root_cert, root_privkey);
779 754
780 if (IPHONE_E_SUCCESS == ret) { 755 if (IPHONE_E_SUCCESS == ret) {
781 /* if everything went well, export in PEM format */ 756
782 gnutls_datum_t dev_pem = { NULL, 0 }; 757 /* generate device certificate */
783 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size); 758 gnutls_x509_crt_set_key(dev_cert, fake_privkey);
784 dev_pem.data = gnutls_malloc(dev_pem.size); 759 gnutls_x509_crt_set_serial(dev_cert, "\x00", 1);
785 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); 760 gnutls_x509_crt_set_version(dev_cert, 3);
786 761 gnutls_x509_crt_set_ca_status(dev_cert, 0);
787 /* now encode certificates for output */ 762 gnutls_x509_crt_set_activation_time(dev_cert, time(NULL));
788 *device_cert_b64 = g_base64_encode(dev_pem.data, dev_pem.size); 763 gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
789 *host_cert_b64 = g_base64_encode(pem_host_cert.data, pem_host_cert.size); 764 gnutls_x509_crt_sign(dev_cert, root_cert, root_privkey);
790 *root_cert_b64 = g_base64_encode(pem_root_cert.data, pem_root_cert.size); 765
766 if (IPHONE_E_SUCCESS == ret) {
767 /* if everything went well, export in PEM format */
768 gnutls_datum_t dev_pem = { NULL, 0 };
769 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size);
770 dev_pem.data = gnutls_malloc(dev_pem.size);
771 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size);
772
773 gnutls_datum_t pem_root_cert = { NULL, 0 };
774 gnutls_datum_t pem_host_cert = { NULL, 0 };
775
776 if ( IPHONE_E_SUCCESS == get_certs_as_pem(&pem_root_cert, &pem_host_cert) ) {
777 /* copy buffer for output */
778 odevice_cert->data = malloc(dev_pem.size);
779 memcpy(odevice_cert->data, dev_pem.data, dev_pem.size);
780 odevice_cert->size = dev_pem.size;
781
782 ohost_cert->data = malloc(pem_host_cert.size);
783 memcpy(ohost_cert->data, pem_host_cert.data, pem_host_cert.size);
784 ohost_cert->size = pem_host_cert.size;
785
786 oroot_cert->data = malloc(pem_root_cert.size);
787 memcpy(oroot_cert->data, pem_root_cert.data, pem_root_cert.size);
788 oroot_cert->size = pem_root_cert.size;
789
790 g_free(pem_root_cert.data);
791 g_free(pem_host_cert.data);
792 }
793 }
791 } 794 }
792 gnutls_free(pem_root_priv.data);
793 gnutls_free(pem_root_cert.data);
794 gnutls_free(pem_host_cert.data);
795 } 795 }
796 } 796 }
797 797
@@ -799,7 +799,6 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
799 gnutls_free(exponent.data); 799 gnutls_free(exponent.data);
800 800
801 gnutls_free(der_pub_key.data); 801 gnutls_free(der_pub_key.data);
802 g_free(pem_pub_key.data);
803 802
804 return ret; 803 return ret;
805} 804}
@@ -813,129 +812,127 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
813 */ 812 */
814iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) 813iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID)
815{ 814{
816 xmlDocPtr plist = new_plist(); 815 plist_t dict = NULL;
817 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 816 uint32_t return_me = 0;
818 xmlNode *key;
819 char *what2send = NULL, **dictionary = NULL;
820 uint32 len = 0, bytes = 0, return_me = 0, i = 0;
821 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
822 // end variables
823 817
818 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
824 control->session_id[0] = '\0'; 819 control->session_id[0] = '\0';
825 820
826 key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); 821 /* Setup DevicePublicKey request plist */
827 if (!key) { 822 dict = plist_new_dict();
828 log_debug_msg("Couldn't add a key.\n"); 823 plist_add_sub_key_el(dict, "HostID");
829 xmlFreeDoc(plist); 824 plist_add_sub_string_el(dict, HostID);
830 return IPHONE_E_DICT_ERROR; 825 plist_add_sub_key_el(dict, "Request");
831 } 826 plist_add_sub_string_el(dict, "StartSession");
832 key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1);
833 if (!key) {
834 log_debug_msg("Couldn't add a key.\n");
835 xmlFreeDoc(plist);
836 return IPHONE_E_DICT_ERROR;
837 }
838
839 xmlDocDumpMemory(plist, (xmlChar **) & what2send, &len);
840 ret = iphone_lckd_send(control, what2send, len, &bytes);
841 827
842 xmlFree(what2send); 828 ret = iphone_lckd_send(control, dict);
843 xmlFreeDoc(plist); 829 plist_free(dict);
830 dict = NULL;
844 831
845 if (ret != IPHONE_E_SUCCESS) 832 if (ret != IPHONE_E_SUCCESS)
846 return ret; 833 return ret;
847 834
848 if (bytes > 0) { 835 ret = iphone_lckd_recv(control, &dict);
849 ret = iphone_lckd_recv(control, &what2send, &len); 836
850 plist = xmlReadMemory(what2send, len, NULL, NULL, 0); 837 if (!dict)
851 dict = xmlDocGetRootElement(plist); 838 return IPHONE_E_PLIST_ERROR;
852 if (!dict) 839
853 return IPHONE_E_DICT_ERROR; 840 plist_t query_node = plist_find_node_by_string(dict, "StartSession");
854 for (dict = dict->children; dict; dict = dict->next) { 841 plist_t result_key_node = plist_get_next_sibling(query_node);
855 if (!xmlStrcmp(dict->name, "dict")) 842 plist_t result_value_node = plist_get_next_sibling(result_key_node);
856 break; 843
857 } 844 plist_type result_key_type = plist_get_node_type(result_key_node);
858 dictionary = read_dict_element_strings(dict); 845 plist_type result_value_type = plist_get_node_type(result_value_node);
859 xmlFreeDoc(plist); 846
860 free(what2send); 847 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
848 char *result_key = NULL;
849 char *result_value = NULL;
850
851 plist_get_key_val(result_key_node, &result_key);
852 plist_get_string_val(result_value_node, &result_value);
853
861 ret = IPHONE_E_SSL_ERROR; 854 ret = IPHONE_E_SSL_ERROR;
862 for (i = 0; dictionary[i]; i += 2) { 855 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
863 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 856 // Set up GnuTLS...
864 // Set up GnuTLS... 857 //gnutls_anon_client_credentials_t anoncred;
865 //gnutls_anon_client_credentials_t anoncred; 858 gnutls_certificate_credentials_t xcred;
866 gnutls_certificate_credentials_t xcred; 859
867 860 log_dbg_msg(DBGMASK_LOCKDOWND, "We started the session OK, now trying GnuTLS\n");
868 log_debug_msg("We started the session OK, now trying GnuTLS\n"); 861 errno = 0;
869 errno = 0; 862 gnutls_global_init();
870 gnutls_global_init(); 863 //gnutls_anon_allocate_client_credentials(&anoncred);
871 //gnutls_anon_allocate_client_credentials(&anoncred); 864 gnutls_certificate_allocate_credentials(&xcred);
872 gnutls_certificate_allocate_credentials(&xcred); 865 gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM);
873 gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); 866 gnutls_init(control->ssl_session, GNUTLS_CLIENT);
874 gnutls_init(control->ssl_session, GNUTLS_CLIENT); 867 {
875 { 868 int protocol_priority[16] = { GNUTLS_SSL3, 0 };
876 int protocol_priority[16] = { GNUTLS_SSL3, 0 }; 869 int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 };
877 int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; 870 int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 };
878 int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; 871 int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 };
879 int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; 872 int comp_priority[16] = { GNUTLS_COMP_NULL, 0 };
880 int comp_priority[16] = { GNUTLS_COMP_NULL, 0 }; 873
881 874 gnutls_cipher_set_priority(*control->ssl_session, cipher_priority);
882 gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); 875 gnutls_compression_set_priority(*control->ssl_session, comp_priority);
883 gnutls_compression_set_priority(*control->ssl_session, comp_priority); 876 gnutls_kx_set_priority(*control->ssl_session, kx_priority);
884 gnutls_kx_set_priority(*control->ssl_session, kx_priority); 877 gnutls_protocol_set_priority(*control->ssl_session, protocol_priority);
885 gnutls_protocol_set_priority(*control->ssl_session, protocol_priority); 878 gnutls_mac_set_priority(*control->ssl_session, mac_priority);
886 gnutls_mac_set_priority(*control->ssl_session, mac_priority);
887 879
888 } 880 }
889 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. 881 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me.
890 882
891 log_debug_msg("GnuTLS step 1...\n"); 883 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 1...\n");
892 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); 884 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control);
893 log_debug_msg("GnuTLS step 2...\n"); 885 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 2...\n");
894 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); 886 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite);
895 log_debug_msg("GnuTLS step 3...\n"); 887 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 3...\n");
896 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); 888 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead);
897 log_debug_msg("GnuTLS step 4 -- now handshaking...\n"); 889 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 4 -- now handshaking...\n");
898 890
899 if (errno) 891 if (errno)
900 log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno)); 892 log_dbg_msg(DBGMASK_LOCKDOWND, "WARN: errno says %s before handshake!\n", strerror(errno));
901 return_me = gnutls_handshake(*control->ssl_session); 893 return_me = gnutls_handshake(*control->ssl_session);
902 log_debug_msg("GnuTLS handshake done...\n"); 894 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS handshake done...\n");
903 895
904 if (return_me != GNUTLS_E_SUCCESS) { 896 if (return_me != GNUTLS_E_SUCCESS) {
905 log_debug_msg("GnuTLS reported something wrong.\n"); 897 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS reported something wrong.\n");
906 gnutls_perror(return_me); 898 gnutls_perror(return_me);
907 log_debug_msg("oh.. errno says %s\n", strerror(errno)); 899 log_dbg_msg(DBGMASK_LOCKDOWND, "oh.. errno says %s\n", strerror(errno));
908 return IPHONE_E_SSL_ERROR; 900 return IPHONE_E_SSL_ERROR;
909 } else { 901 } else {
910 control->in_SSL = 1; 902 control->in_SSL = 1;
911 ret = IPHONE_E_SUCCESS; 903 ret = IPHONE_E_SUCCESS;
912 }
913 } else if (!strcmp(dictionary[i], "SessionID")) {
914 // we need to store the session ID for StopSession
915 strcpy(control->session_id, dictionary[i + 1]);
916 log_debug_msg("SessionID: %s\n", control->session_id);
917 free_dictionary(dictionary);
918 return ret;
919 } 904 }
920 } 905 }
921 if (ret == IPHONE_E_SUCCESS) { 906 }
922 log_debug_msg("Failed to get SessionID!\n"); 907 //store session id
923 return ret; 908 plist_t session_node = plist_find_node_by_key(dict, "SessionID");
924 } 909 if (session_node) {
910
911 plist_t session_node_val = plist_get_next_sibling(session_node);
912 plist_type session_node_val_type = plist_get_node_type(session_node_val);
913
914 if (session_node_val_type == PLIST_STRING) {
925 915
926 log_debug_msg("Apparently failed negotiating with lockdownd.\n"); 916 char *session_id = NULL;
927 log_debug_msg("Responding dictionary: \n"); 917 plist_get_string_val(session_node_val, &session_id);
928 for (i = 0; dictionary[i]; i += 2) { 918
929 log_debug_msg("\t%s: %s\n", dictionary[i], dictionary[i + 1]); 919 if (session_node_val_type == PLIST_STRING && session_id) {
920 // we need to store the session ID for StopSession
921 strcpy(control->session_id, session_id);
922 log_dbg_msg(DBGMASK_LOCKDOWND, "SessionID: %s\n", control->session_id);
923 }
924 free(session_id);
930 } 925 }
926 } else
927 log_dbg_msg(DBGMASK_LOCKDOWND, "Failed to get SessionID!\n");
928 plist_free(dict);
929 dict = NULL;
931 930
931 if (ret == IPHONE_E_SUCCESS)
932 return ret;
932 933
933 free_dictionary(dictionary); 934 log_dbg_msg(DBGMASK_LOCKDOWND, "Apparently failed negotiating with lockdownd.\n");
934 return IPHONE_E_SSL_ERROR; 935 return IPHONE_E_SSL_ERROR;
935 } else {
936 log_debug_msg("Didn't get enough bytes.\n");
937 return IPHONE_E_NOT_ENOUGH_DATA;
938 }
939} 936}
940 937
941/** gnutls callback for writing data to the iPhone. 938/** gnutls callback for writing data to the iPhone.
@@ -951,10 +948,10 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size
951 int bytes = 0; 948 int bytes = 0;
952 iphone_lckd_client_t control; 949 iphone_lckd_client_t control;
953 control = (iphone_lckd_client_t) transport; 950 control = (iphone_lckd_client_t) transport;
954 log_debug_msg("lockdownd_secuwrite() called\n"); 951 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_secuwrite() called\n");
955 log_debug_msg("pre-send\nlength = %zi\n", length); 952 log_dbg_msg(DBGMASK_LOCKDOWND, "pre-send\nlength = %zi\n", length);
956 iphone_mux_send(control->connection, buffer, length, &bytes); 953 iphone_mux_send(control->connection, buffer, length, &bytes);
957 log_debug_msg("post-send\nsent %i bytes\n", bytes); 954 log_dbg_msg(DBGMASK_LOCKDOWND, "post-send\nsent %i bytes\n", bytes);
958 955
959 dump_debug_buffer("sslpacketwrite.out", buffer, length); 956 dump_debug_buffer("sslpacketwrite.out", buffer, length);
960 return bytes; 957 return bytes;
@@ -1038,86 +1035,72 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
1038 if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) 1035 if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id))
1039 return IPHONE_E_SSL_ERROR; 1036 return IPHONE_E_SSL_ERROR;
1040 1037
1041 char *XML_query, **dictionary; 1038 plist_t dict = NULL;
1042 uint32 length, i = 0, port_loc = 0, bytes = 0; 1039 uint32_t port_loc = 0;
1043 uint8 result = 0;
1044 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 1040 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
1045 1041
1046 free(host_id); 1042 free(host_id);
1047 host_id = NULL; 1043 host_id = NULL;
1048 1044
1049 xmlDocPtr plist = new_plist(); 1045 dict = plist_new_dict();
1050 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 1046 plist_add_sub_key_el(dict, "Request");
1051 xmlNode *key; 1047 plist_add_sub_string_el(dict, "StartService");
1052 key = add_key_str_dict_element(plist, dict, "Request", "StartService", 1); 1048 plist_add_sub_key_el(dict, "Service");
1053 if (!key) { 1049 plist_add_sub_string_el(dict, service);
1054 xmlFreeDoc(plist);
1055 return IPHONE_E_UNKNOWN_ERROR;
1056 }
1057 key = add_key_str_dict_element(plist, dict, "Service", service, 1);
1058 if (!key) {
1059 xmlFreeDoc(plist);
1060 return IPHONE_E_UNKNOWN_ERROR;
1061 }
1062 1050
1063 xmlDocDumpMemory(plist, (xmlChar **) & XML_query, &length); 1051 /* send to iPhone */
1052 ret = iphone_lckd_send(client, dict);
1053 plist_free(dict);
1054 dict = NULL;
1064 1055
1065 ret = iphone_lckd_send(client, XML_query, length, &bytes);
1066 free(XML_query);
1067 if (IPHONE_E_SUCCESS != ret) 1056 if (IPHONE_E_SUCCESS != ret)
1068 return ret; 1057 return ret;
1069 1058
1070 ret = iphone_lckd_recv(client, &XML_query, &bytes); 1059 ret = iphone_lckd_recv(client, &dict);
1071 xmlFreeDoc(plist); 1060
1072 if (IPHONE_E_SUCCESS != ret) 1061 if (IPHONE_E_SUCCESS != ret)
1073 return ret; 1062 return ret;
1074 1063
1075 if (bytes <= 0) 1064 if (!dict)
1076 return IPHONE_E_NOT_ENOUGH_DATA; 1065 return IPHONE_E_PLIST_ERROR;
1077 else {
1078 plist = xmlReadMemory(XML_query, bytes, NULL, NULL, 0);
1079 if (!plist)
1080 return IPHONE_E_UNKNOWN_ERROR;
1081 dict = xmlDocGetRootElement(plist);
1082 if (!dict)
1083 return IPHONE_E_UNKNOWN_ERROR;
1084 for (dict = dict->children; dict; dict = dict->next) {
1085 if (!xmlStrcmp(dict->name, "dict"))
1086 break;
1087 }
1088 1066
1089 if (!dict) 1067 plist_t query_node = plist_find_node_by_string(dict, "StartService");
1090 return IPHONE_E_UNKNOWN_ERROR; 1068 plist_t result_key_node = plist_get_next_sibling(query_node);
1091 dictionary = read_dict_element_strings(dict); 1069 plist_t result_value_node = plist_get_next_sibling(result_key_node);
1092 1070
1093 for (i = 0; dictionary[i]; i += 2) { 1071 plist_t port_key_node = plist_find_node_by_key(dict, "Port");
1094 log_debug_msg("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]); 1072 plist_t port_value_node = plist_get_next_sibling(port_key_node);
1095 1073
1096 if (!xmlStrcmp(dictionary[i], "Port")) { 1074 plist_type result_key_type = plist_get_node_type(result_key_node);
1097 port_loc = atoi(dictionary[i + 1]); 1075 plist_type result_value_type = plist_get_node_type(result_value_node);
1098 log_debug_msg("lockdownd_start_service() atoi'd port: %i\n", port); 1076 plist_type port_key_type = plist_get_node_type(port_key_node);
1099 } 1077 plist_type port_value_type = plist_get_node_type(port_value_node);
1100 1078
1101 if (!xmlStrcmp(dictionary[i], "Result")) { 1079 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && port_key_type == PLIST_KEY
1102 if (!xmlStrcmp(dictionary[i + 1], "Success")) { 1080 && port_value_type == PLIST_UINT) {
1103 result = 1;
1104 }
1105 }
1106 }
1107 1081
1108 log_debug_msg("lockdownd_start_service(): DATA RECEIVED:\n\n"); 1082 char *result_key = NULL;
1109 log_debug_msg(XML_query); 1083 char *result_value = NULL;
1110 log_debug_msg("end data received by lockdownd_start_service()\n"); 1084 char *port_key = NULL;
1085 uint64_t port_value = 0;
1111 1086
1112 free(XML_query); 1087 plist_get_key_val(result_key_node, &result_key);
1113 xmlFreeDoc(plist); 1088 plist_get_string_val(result_value_node, &result_value);
1114 free_dictionary(dictionary); 1089 plist_get_key_val(port_key_node, &port_key);
1115 if (port && result) { 1090 plist_get_uint_val(port_value_node, &port_value);
1091
1092 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) {
1093 port_loc = port_value;
1094 ret = IPHONE_E_SUCCESS;
1095 }
1096
1097 if (port && ret == IPHONE_E_SUCCESS)
1116 *port = port_loc; 1098 *port = port_loc;
1117 return IPHONE_E_SUCCESS; 1099 else
1118 } else 1100 ret = IPHONE_E_UNKNOWN_ERROR;
1119 return IPHONE_E_UNKNOWN_ERROR;
1120 } 1101 }
1121 1102
1122 return IPHONE_E_UNKNOWN_ERROR; 1103 plist_free(dict);
1104 dict = NULL;
1105 return ret;
1123} 1106}
diff --git a/src/lockdown.h b/src/lockdown.h
index c30a182..7485006 100644
--- a/src/lockdown.h
+++ b/src/lockdown.h
@@ -8,22 +8,21 @@
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version. 10 * version 2.1 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details. 15 * Lesser General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Lesser General Public 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 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 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22#ifndef LOCKDOWND_H 22#ifndef LOCKDOWND_H
23#define LOCKDOWND_H 23#define LOCKDOWND_H
24 24
25#include "usbmux.h" 25#include "usbmux.h"
26#include "plist.h"
27 26
28#include <gnutls/gnutls.h> 27#include <gnutls/gnutls.h>
29#include <string.h> 28#include <string.h>
@@ -39,17 +38,17 @@ struct iphone_lckd_client_int {
39 char session_id[40]; 38 char session_id[40];
40}; 39};
41 40
42char *lockdownd_generate_hostid(void);
43
44iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone); 41iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone);
45iphone_error_t lockdownd_hello(iphone_lckd_client_t control); 42iphone_error_t lockdownd_hello(iphone_lckd_client_t control);
43
46iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, const char *req_string, 44iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, const char *req_string,
47 char **value); 45 gnutls_datum_t * value);
48iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key); 46
47iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, gnutls_datum_t * public_key);
49 48
50iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, 49iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * device_cert,
51 char **root_cert_b64); 50 gnutls_datum_t * host_cert, gnutls_datum_t * root_cert);
52iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *public_key, char *host_id); 51iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id);
53void lockdownd_close(iphone_lckd_client_t control); 52void lockdownd_close(iphone_lckd_client_t control);
54 53
55// SSL functions 54// SSL functions
diff --git a/src/plist.c b/src/plist.c
deleted file mode 100644
index b9d9e6a..0000000
--- a/src/plist.c
+++ /dev/null
@@ -1,245 +0,0 @@
1/*
2 * plist.c
3 * Builds plist XML structures.
4 *
5 * Copyright (c) 2008 Zach C. 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 <libxml/parser.h>
23#include <libxml/tree.h>
24#include <string.h>
25#include <assert.h>
26#include "plist.h"
27
28const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
29<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
30<plist version=\"1.0\">\n\
31</plist>\0";
32
33/** Formats a block of text to be a given indentation and width.
34 *
35 * The total width of the return string will be depth + cols.
36 *
37 * @param buf The string to format.
38 * @param cols The number of text columns for returned block of text.
39 * @param depth The number of tabs to indent the returned block of text.
40 *
41 * @return The formatted string.
42 */
43static char *format_string(const char *buf, int cols, int depth)
44{
45 int colw = depth + cols + 1;
46 int len = strlen(buf);
47 int nlines = len / cols + 1;
48 char *new_buf = (char *) malloc(nlines * colw + depth + 1);
49 int i = 0;
50 int j = 0;
51
52 assert(cols >= 0);
53 assert(depth >= 0);
54
55 // Inserts new lines and tabs at appropriate locations
56 for (i = 0; i < nlines; i++) {
57 new_buf[i * colw] = '\n';
58 for (j = 0; j < depth; j++)
59 new_buf[i * colw + 1 + j] = '\t';
60 memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols);
61 }
62 new_buf[len + (1 + depth) * nlines] = '\n';
63
64 // Inserts final row of indentation and termination character
65 for (j = 0; j < depth; j++)
66 new_buf[len + (1 + depth) * nlines + 1 + j] = '\t';
67 new_buf[len + (1 + depth) * nlines + depth + 1] = '\0';
68
69 return new_buf;
70}
71
72/** Creates a new plist XML document.
73 *
74 * @return The plist XML document.
75 */
76xmlDocPtr new_plist(void)
77{
78 char *plist = strdup(plist_base);
79 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0);
80
81 if (!plist_xml)
82 return NULL;
83
84 free(plist);
85
86 return plist_xml;
87}
88
89/** Destroys a previously created XML document.
90 *
91 * @param plist The XML document to destroy.
92 */
93void free_plist(xmlDocPtr plist)
94{
95 if (!plist)
96 return;
97
98 xmlFreeDoc(plist);
99}
100
101/** Adds a new node as a child to a given node.
102 *
103 * This is a lower level function so you probably want to use
104 * add_key_str_dict_element, add_key_dict_node or add_key_data_dict_element
105 * instead.
106 *
107 * @param plist The plist XML document to which the to_node belongs.
108 * @param name The name of the new node.
109 * @param content The string containing the text node of the new node.
110 * @param to_node The node to attach the child node to. If none is given, the
111 * root node of the given document is used.
112 * @param depth The number of tabs to indent the new node.
113 *
114 * @return The newly created node.
115 */
116xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth)
117{
118 int i = 0;
119 xmlNode *child;
120
121 if (!plist)
122 return NULL;
123 assert(depth >= 0);
124 if (!to_node)
125 to_node = xmlDocGetRootElement(plist);
126
127 for (i = 0; i < depth; i++) {
128 xmlNodeAddContent(to_node, "\t");
129 }
130 child = xmlNewChild(to_node, NULL, name, content);
131 xmlNodeAddContent(to_node, "\n");
132
133 return child;
134}
135
136/** Adds a string key-pair to a plist XML document.
137 *
138 * @param plist The plist XML document to add the new node to.
139 * @param dict The dictionary node within the plist XML document to add the new node to.
140 * @param key The string containing the key value.
141 * @param value The string containing the value.
142 * @param depth The number of tabs to indent the new node.
143 *
144 * @return The newly created key node.
145 */
146xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
147{
148 xmlNode *keyPtr;
149
150 keyPtr = add_child_to_plist(plist, "key", key, dict, depth);
151 add_child_to_plist(plist, "string", value, dict, depth);
152
153 return keyPtr;
154}
155
156/** Adds a new dictionary key-pair to a plist XML document.
157 *
158 * @param plist The plist XML document to add the new node to.
159 * @param dict The dictionary node within the plist XML document to add the new node to.
160 * @param key The string containing the key value.
161 * @param value The string containing the value.
162 * @param depth The number of tabs to indent the new node.
163 *
164 * @return The newly created dict node.
165 */
166xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
167{
168 xmlNode *child;
169
170 add_child_to_plist(plist, "key", key, dict, depth);
171 child = add_child_to_plist(plist, "dict", value, dict, depth);
172
173 return child;
174}
175
176/** Adds a new data dictionary key-pair to a plist XML document.
177 *
178 * @param plist The plist XML document to add the new node to.
179 * @param dict The dictionary node within the plist XML document to add the new node to.
180 * @param key The string containing the key value.
181 * @param value The string containing the value.
182 * @param depth The number of tabs to indent the new node.
183 *
184 * @return The newly created key node.
185 */
186xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
187{
188 xmlNode *keyPtr;
189
190 keyPtr = add_child_to_plist(plist, "key", key, dict, depth);
191 add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth);
192
193 return keyPtr;
194}
195
196/** Reads a set of keys and strings into an array from a plist XML document.
197 *
198 * @param dict The root XMLNode of a plist XML document to be read.
199 *
200 * @return An array where each even number is a key and the odd numbers are
201 * values. If the odd number is \0, that's the end of the list.
202 */
203char **read_dict_element_strings(xmlNode * dict)
204{
205 char **return_me = NULL, **old = NULL;
206 int current_length = 0;
207 int current_pos = 0;
208 xmlNode *dict_walker;
209
210 for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) {
211 if (!xmlStrcmp(dict_walker->name, "key")) {
212 current_length += 2;
213 old = return_me;
214 return_me = realloc(return_me, sizeof(char *) * current_length);
215 if (!return_me) {
216 free(old);
217 return NULL;
218 }
219 return_me[current_pos++] = xmlNodeGetContent(dict_walker);
220 return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next);
221 }
222 }
223
224 old = return_me;
225 return_me = realloc(return_me, sizeof(char *) * (current_length + 1));
226 return_me[current_pos] = NULL;
227
228 return return_me;
229}
230
231/** Destroys a dictionary as returned by read_dict_element_strings
232 */
233void free_dictionary(char **dictionary)
234{
235 int i = 0;
236
237 if (!dictionary)
238 return;
239
240 for (i = 0; dictionary[i]; i++) {
241 free(dictionary[i]);
242 }
243
244 free(dictionary);
245}
diff --git a/src/plist.h b/src/plist.h
deleted file mode 100644
index cd2028e..0000000
--- a/src/plist.h
+++ /dev/null
@@ -1,38 +0,0 @@
1/*
2 * plist.h
3 * contains structures and the like for plists
4 *
5 * Copyright (c) 2008 Zach C. 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#ifndef PLIST_H
23#define PLIST_H
24
25#include <libxml/parser.h>
26#include <libxml/tree.h>
27
28xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth);
29xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth);
30xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth);
31xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth);
32
33void free_plist(xmlDocPtr plist);
34xmlDocPtr new_plist(void);
35
36char **read_dict_element_strings(xmlNode * dict);
37void free_dictionary(char **dictionary);
38#endif
diff --git a/src/usbmux.c b/src/usbmux.c
index d8e6b44..22ce588 100644
--- a/src/usbmux.c
+++ b/src/usbmux.c
@@ -38,7 +38,7 @@ static int clients = 0;
38 * 38 *
39 * @return A USBMux packet 39 * @return A USBMux packet
40 */ 40 */
41usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) 41usbmux_tcp_header *new_mux_packet(uint16_t s_port, uint16_t d_port)
42{ 42{
43 usbmux_tcp_header *conn = (usbmux_tcp_header *) malloc(sizeof(usbmux_tcp_header)); 43 usbmux_tcp_header *conn = (usbmux_tcp_header *) malloc(sizeof(usbmux_tcp_header));
44 conn->type = htonl(6); 44 conn->type = htonl(6);
@@ -314,6 +314,7 @@ iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t
314 } else { 314 } else {
315 memcpy(data, client->recv_buffer, client->r_len); 315 memcpy(data, client->recv_buffer, client->r_len);
316 free(client->recv_buffer); // don't need to deal with anymore, but... 316 free(client->recv_buffer); // don't need to deal with anymore, but...
317 client->recv_buffer = NULL;
317 offset = client->r_len; // see #2b, above 318 offset = client->r_len; // see #2b, above
318 client->r_len = 0; 319 client->r_len = 0;
319 } 320 }
diff --git a/src/usbmux.h b/src/usbmux.h
index fd5fc78..bea83f7 100644
--- a/src/usbmux.h
+++ b/src/usbmux.h
@@ -22,6 +22,7 @@
22#include <sys/types.h> 22#include <sys/types.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <stdint.h> 24#include <stdint.h>
25#include "libiphone/libiphone.h"
25 26
26#ifndef USBMUX_H 27#ifndef USBMUX_H
27#define USBMUX_H 28#define USBMUX_H
@@ -30,17 +31,12 @@
30#include "iphone.h" 31#include "iphone.h"
31#endif 32#endif
32 33
33typedef uint16_t uint16;
34typedef uint32_t uint32;
35typedef uint8_t uint8;
36
37
38typedef struct { 34typedef struct {
39 uint32 type, length; 35 uint32_t type, length;
40 uint16 sport, dport; 36 uint16_t sport, dport;
41 uint32 scnt, ocnt; 37 uint32_t scnt, ocnt;
42 uint8 offset, tcp_flags; 38 uint8_t offset, tcp_flags;
43 uint16 window, nullnull, length16; 39 uint16_t window, nullnull, length16;
44} usbmux_tcp_header; 40} usbmux_tcp_header;
45 41
46struct iphone_umux_client_int { 42struct iphone_umux_client_int {
@@ -50,10 +46,10 @@ struct iphone_umux_client_int {
50 int r_len; 46 int r_len;
51}; 47};
52 48
53usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port); 49usbmux_tcp_header *new_mux_packet(uint16_t s_port, uint16_t d_port);
54 50
55typedef struct { 51typedef struct {
56 uint32 type, length, major, minor, allnull; 52 uint32_t type, length, major, minor, allnull;
57} usbmux_version_header; 53} usbmux_version_header;
58 54
59usbmux_version_header *version_header(void); 55usbmux_version_header *version_header(void);
diff --git a/src/userpref.c b/src/userpref.c
index 5f227b0..0e83133 100644
--- a/src/userpref.c
+++ b/src/userpref.c
@@ -8,25 +8,28 @@
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version. 10 * version 2.1 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details. 15 * Lesser General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Lesser General Public 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 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 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22#include <glib.h> 22#include <glib.h>
23#include <glib/gprintf.h> 23#include <glib/gprintf.h>
24#include <stdio.h> 24#include <stdio.h>
25#include <stdlib.h>
25#include <string.h> 26#include <string.h>
27#include <gnutls/gnutls.h>
28#include <gnutls/x509.h>
29#include <gcrypt.h>
30
26#include "userpref.h" 31#include "userpref.h"
27#include "utils.h" 32#include "utils.h"
28#include <string.h>
29#include <stdlib.h>
30 33
31#define LIBIPHONE_CONF_DIR "libiphone" 34#define LIBIPHONE_CONF_DIR "libiphone"
32#define LIBIPHONE_CONF_FILE "libiphonerc" 35#define LIBIPHONE_CONF_FILE "libiphonerc"
@@ -49,9 +52,75 @@ static void create_config_dir(void)
49 g_free(config_dir); 52 g_free(config_dir);
50} 53}
51 54
55static int get_rand(int min, int max)
56{
57 int retval = (rand() % (max - min)) + min;
58 return retval;
59}
60
61/** Generates a valid HostID (which is actually a UUID).
62 *
63 * @return A null terminated string containing a valid HostID.
64 */
65static char *lockdownd_generate_hostid()
66{
67 char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long
68 const char *chars = "ABCDEF0123456789";
69 srand(time(NULL));
70 int i = 0;
71
72 for (i = 0; i < 36; i++) {
73 if (i == 8 || i == 13 || i == 18 || i == 23) {
74 hostid[i] = '-';
75 continue;
76 } else {
77 hostid[i] = chars[get_rand(0, 16)];
78 }
79 }
80 hostid[36] = '\0'; // make it a real string
81 return hostid;
82}
83
84/** Store HostID in config file.
85 *
86 * @param host_id A null terminated string containing a valid HostID.
87 */
88static int write_host_id(char *host_id)
89{
90 GKeyFile *key_file;
91 gsize length;
92 gchar *buf, *config_file;
93 GIOChannel *file;
94
95 if (!host_id)
96 return 0;
97
98 /* Make sure config directory exists */
99 create_config_dir();
100
101 /* Now parse file to get the HostID */
102 key_file = g_key_file_new();
52 103
53/** Reads the HostID from a previously generated configuration file. 104 /* Store in config file */
54 * 105 log_debug_msg("init_config_file(): setting hostID to %s\n", host_id);
106 g_key_file_set_value(key_file, "Global", "HostID", host_id);
107
108 /* Write config file on disk */
109 buf = g_key_file_to_data(key_file, &length, NULL);
110 config_file =
111 g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
112 file = g_io_channel_new_file(config_file, "w", NULL);
113 g_free(config_file);
114 g_io_channel_write_chars(file, buf, length, NULL, NULL);
115 g_io_channel_shutdown(file, TRUE, NULL);
116 g_io_channel_unref(file);
117
118 g_key_file_free(key_file);
119 return 1;
120}
121
122/** Reads the HostID from a previously generated configuration file.
123 *
55 * @note It is the responsibility of the calling function to free the returned host_id 124 * @note It is the responsibility of the calling function to free the returned host_id
56 * 125 *
57 * @return The string containing the HostID or NULL 126 * @return The string containing the HostID or NULL
@@ -77,6 +146,12 @@ char *get_host_id(void)
77 g_key_file_free(key_file); 146 g_key_file_free(key_file);
78 g_free(config_file); 147 g_free(config_file);
79 148
149 if (!host_id) {
150 //no config, generate host_id
151 host_id = lockdownd_generate_hostid();
152 write_host_id(host_id);
153 }
154
80 log_debug_msg("get_host_id(): Using %s as HostID\n", host_id); 155 log_debug_msg("get_host_id(): Using %s as HostID\n", host_id);
81 return host_id; 156 return host_id;
82} 157}
@@ -111,10 +186,10 @@ int is_device_known(char *uid)
111 * @return 1 on success and 0 if no public key is given or if it has already 186 * @return 1 on success and 0 if no public key is given or if it has already
112 * been marked as connected previously. 187 * been marked as connected previously.
113 */ 188 */
114int store_device_public_key(char *uid, char *public_key) 189int store_device_public_key(char *uid, gnutls_datum_t public_key)
115{ 190{
116 191
117 if (NULL == public_key || is_device_known(uid)) 192 if (NULL == public_key.data || is_device_known(uid))
118 return 0; 193 return 0;
119 194
120 /* ensure config directory exists */ 195 /* ensure config directory exists */
@@ -124,15 +199,11 @@ int store_device_public_key(char *uid, char *public_key)
124 gchar *device_file = g_strconcat(uid, ".pem", NULL); 199 gchar *device_file = g_strconcat(uid, ".pem", NULL);
125 gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); 200 gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
126 201
127 /* decode public key for storing */
128 gsize decoded_size;
129 gchar *data = g_base64_decode(public_key, &decoded_size);
130 /* store file */ 202 /* store file */
131 FILE *pFile = fopen(pem, "wb"); 203 FILE *pFile = fopen(pem, "wb");
132 fwrite(data, 1, decoded_size, pFile); 204 fwrite(public_key.data, 1, public_key.size, pFile);
133 fclose(pFile); 205 fclose(pFile);
134 g_free(pem); 206 g_free(pem);
135 g_free(data);
136 g_free(device_file); 207 g_free(device_file);
137 return 1; 208 return 1;
138} 209}
@@ -160,56 +231,220 @@ static int read_file_in_confdir(const char *file, gnutls_datum_t * data)
160 g_free(filepath); 231 g_free(filepath);
161 232
162 /* Add it to the gnutls_datnum_t structure */ 233 /* Add it to the gnutls_datnum_t structure */
163 data->data = content; 234 data->data = (uint8_t*) content;
164 data->size = size; 235 data->size = size;
165 236
166 return success; 237 return success;
167} 238}
168 239
169/** Read the root private key 240
170 * 241/** Private function which generate private keys and certificates.
171 * @param root_privkey A pointer to the appropriate gnutls structure
172 * 242 *
173 * @return 1 if the file was successfully read and 0 otherwise. 243 * @return IPHONE_E_SUCCESS if keys were successfully generated.
174 */ 244 */
175int get_root_private_key(gnutls_datum_t * root_privkey) 245static iphone_error_t gen_keys_and_cert(void)
176{ 246{
177 return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey); 247 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
248 gnutls_x509_privkey_t root_privkey;
249 gnutls_x509_privkey_t host_privkey;
250 gnutls_x509_crt_t root_cert;
251 gnutls_x509_crt_t host_cert;
252
253 gnutls_global_deinit();
254 gnutls_global_init();
255
256 //use less secure random to speed up key generation
257 gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM);
258
259 gnutls_x509_privkey_init(&root_privkey);
260 gnutls_x509_privkey_init(&host_privkey);
261
262 gnutls_x509_crt_init(&root_cert);
263 gnutls_x509_crt_init(&host_cert);
264
265 /* generate root key */
266 gnutls_x509_privkey_generate(root_privkey, GNUTLS_PK_RSA, 2048, 0);
267 gnutls_x509_privkey_generate(host_privkey, GNUTLS_PK_RSA, 2048, 0);
268
269 /* generate certificates */
270 gnutls_x509_crt_set_key(root_cert, root_privkey);
271 gnutls_x509_crt_set_serial(root_cert, "\x00", 1);
272 gnutls_x509_crt_set_version(root_cert, 3);
273 gnutls_x509_crt_set_ca_status(root_cert, 1);
274 gnutls_x509_crt_set_activation_time(root_cert, time(NULL));
275 gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
276 gnutls_x509_crt_sign(root_cert, root_cert, root_privkey);
277
278
279 gnutls_x509_crt_set_key(host_cert, host_privkey);
280 gnutls_x509_crt_set_serial(host_cert, "\x00", 1);
281 gnutls_x509_crt_set_version(host_cert, 3);
282 gnutls_x509_crt_set_ca_status(host_cert, 0);
283 gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE);
284 gnutls_x509_crt_set_activation_time(host_cert, time(NULL));
285 gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
286 gnutls_x509_crt_sign(host_cert, root_cert, root_privkey);
287
288 /* export to PEM format */
289 gnutls_datum_t root_key_pem = { NULL, 0 };
290 gnutls_datum_t host_key_pem = { NULL, 0 };
291
292 gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size);
293 gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size);
294
295 root_key_pem.data = gnutls_malloc(root_key_pem.size);
296 host_key_pem.data = gnutls_malloc(host_key_pem.size);
297
298 gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size);
299 gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size);
300
301 gnutls_datum_t root_cert_pem = { NULL, 0 };
302 gnutls_datum_t host_cert_pem = { NULL, 0 };
303
304 gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size);
305 gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size);
306
307 root_cert_pem.data = gnutls_malloc(root_cert_pem.size);
308 host_cert_pem.data = gnutls_malloc(host_cert_pem.size);
309
310 gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size);
311 gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size);
312
313 if (NULL != root_cert_pem.data && 0 != root_cert_pem.size &&
314 NULL != host_cert_pem.data && 0 != host_cert_pem.size)
315 ret = IPHONE_E_SUCCESS;
316
317 /* store values in config file */
318 init_config_file( &root_key_pem, &host_key_pem, &root_cert_pem, &host_cert_pem);
319
320 gnutls_free(root_key_pem.data);
321 gnutls_free(host_key_pem.data);
322 gnutls_free(root_cert_pem.data);
323 gnutls_free(host_cert_pem.data);
324
325 //restore gnutls env
326 gnutls_global_deinit();
327 gnutls_global_init();
328
329 return ret;
178} 330}
179 331
180/** Read the host private key 332/** Private function which import the given key into a gnutls structure.
181 * 333 *
182 * @param host_privkey A pointer to the appropriate gnutls structure 334 * @param key_name The filename of the private key to import.
335 * @param key the gnutls key structure.
183 * 336 *
184 * @return 1 if the file was successfully read and 0 otherwise. 337 * @return IPHONE_E_SUCCESS if the key was successfully imported.
185 */ 338 */
186int get_host_private_key(gnutls_datum_t * host_privkey) 339static iphone_error_t import_key(const char* key_name, gnutls_x509_privkey_t key)
187{ 340{
188 return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey); 341 iphone_error_t ret = IPHONE_E_INVALID_CONF;
342 gnutls_datum_t pem_key = { NULL, 0 };
343
344 if ( read_file_in_confdir(key_name, &pem_key) ) {
345 if (GNUTLS_E_SUCCESS == gnutls_x509_privkey_import(key, &pem_key, GNUTLS_X509_FMT_PEM))
346 ret = IPHONE_E_SUCCESS;
347 else
348 ret = IPHONE_E_SSL_ERROR;
349 }
350 gnutls_free(pem_key.data);
351 return ret;
189} 352}
190 353
191/** Read the root certificate 354/** Private function which import the given certificate into a gnutls structure.
192 * 355 *
193 * @param root_privkey A pointer to the appropriate gnutls structure 356 * @param crt_name The filename of the certificate to import.
357 * @param cert the gnutls certificate structure.
194 * 358 *
195 * @return 1 if the file was successfully read and 0 otherwise. 359 * @return IPHONE_E_SUCCESS if the certificate was successfully imported.
196 */ 360 */
197int get_root_certificate(gnutls_datum_t * root_cert) 361static iphone_error_t import_crt(const char* crt_name, gnutls_x509_crt_t cert)
198{ 362{
199 return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert); 363 iphone_error_t ret = IPHONE_E_INVALID_CONF;
364 gnutls_datum_t pem_cert = { NULL, 0 };
365
366 if ( read_file_in_confdir(crt_name, &pem_cert) ) {
367 if (GNUTLS_E_SUCCESS == gnutls_x509_crt_import(cert, &pem_cert, GNUTLS_X509_FMT_PEM))
368 ret = IPHONE_E_SUCCESS;
369 else
370 ret = IPHONE_E_SSL_ERROR;
371 }
372 gnutls_free(pem_cert.data);
373 return ret;
200} 374}
201 375
202/** Read the host certificate 376/** Function to retrieve host keys and certificates.
377 * This function trigger key generation if they do not exists yet or are invalid.
203 * 378 *
204 * @param root_privkey A pointer to the appropriate gnutls structure 379 * @note This function can take few seconds to complete (typically 5 seconds)
205 * 380 *
206 * @return 1 if the file was successfully read and 0 otherwise. 381 * @param root_privkey The root private key.
382 * @param root_crt The root certificate.
383 * @param host_privkey The host private key.
384 * @param host_crt The host certificate.
385 *
386 * @return IPHONE_E_SUCCESS if the keys and certificates were successfully retrieved.
207 */ 387 */
208int get_host_certificate(gnutls_datum_t * host_cert) 388iphone_error_t get_keys_and_certs(gnutls_x509_privkey_t root_privkey, gnutls_x509_crt_t root_crt, gnutls_x509_privkey_t host_privkey, gnutls_x509_crt_t host_crt)
209{ 389{
210 return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert); 390 iphone_error_t ret = IPHONE_E_SUCCESS;
391
392 if (ret == IPHONE_E_SUCCESS)
393 ret = import_key(LIBIPHONE_ROOT_PRIVKEY, root_privkey);
394
395 if (ret == IPHONE_E_SUCCESS)
396 ret = import_key(LIBIPHONE_HOST_PRIVKEY, host_privkey);
397
398 if (ret == IPHONE_E_SUCCESS)
399 ret = import_crt(LIBIPHONE_ROOT_CERTIF, root_crt);
400
401 if (ret == IPHONE_E_SUCCESS)
402 ret = import_crt(LIBIPHONE_HOST_CERTIF, host_crt);
403
404
405 if (IPHONE_E_SUCCESS != ret) {
406 //we had problem reading or importing root cert
407 //try with a new ones.
408 ret = gen_keys_and_cert();
409
410 if (ret == IPHONE_E_SUCCESS)
411 ret = import_key(LIBIPHONE_ROOT_PRIVKEY, root_privkey);
412
413 if (ret == IPHONE_E_SUCCESS)
414 ret = import_key(LIBIPHONE_HOST_PRIVKEY, host_privkey);
415
416 if (ret == IPHONE_E_SUCCESS)
417 ret = import_crt(LIBIPHONE_ROOT_CERTIF, root_crt);
418
419 if (ret == IPHONE_E_SUCCESS)
420 ret = import_crt(LIBIPHONE_HOST_CERTIF, host_crt);
421 }
422
423 return ret;
211} 424}
212 425
426/** Function to retrieve certificates encoded in PEM format.
427 *
428 * @param pem_root_cert The root certificate.
429 * @param pem_host_cert The host certificate.
430 *
431 * @return IPHONE_E_SUCCESS if the certificates were successfully retrieved.
432 */
433iphone_error_t get_certs_as_pem(gnutls_datum_t *pem_root_cert, gnutls_datum_t *pem_host_cert)
434{
435 iphone_error_t ret = IPHONE_E_INVALID_CONF;
436
437 if ( !pem_root_cert || !pem_host_cert)
438 return IPHONE_E_INVALID_ARG;
439
440 if ( read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, pem_root_cert) && read_file_in_confdir(LIBIPHONE_HOST_CERTIF, pem_host_cert))
441 ret = IPHONE_E_SUCCESS;
442 else {
443 g_free(pem_root_cert->data);
444 g_free(pem_host_cert->data);
445 }
446 return ret;
447}
213/** Create and save a configuration file containing the given data. 448/** Create and save a configuration file containing the given data.
214 * 449 *
215 * @note: All fields must specified and be non-null 450 * @note: All fields must specified and be non-null
@@ -222,41 +457,18 @@ int get_host_certificate(gnutls_datum_t * host_cert)
222 * 457 *
223 * @return 1 on success and 0 otherwise. 458 * @return 1 on success and 0 otherwise.
224 */ 459 */
225int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert, 460int init_config_file( gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert,
226 gnutls_datum_t * host_cert) 461 gnutls_datum_t * host_cert)
227{ 462{
228 FILE *pFile; 463 FILE *pFile;
229 gchar *pem; 464 gchar *pem;
230 GKeyFile *key_file;
231 gsize length;
232 gchar *buf, *config_file;
233 GIOChannel *file;
234 465
235 if (!host_id || !root_key || !host_key || !root_cert || !host_cert) 466 if (!root_key || !host_key || !root_cert || !host_cert)
236 return 0; 467 return 0;
237 468
238 /* Make sure config directory exists */ 469 /* Make sure config directory exists */
239 create_config_dir(); 470 create_config_dir();
240 471
241 /* Now parse file to get the HostID */
242 key_file = g_key_file_new();
243
244 /* Store in config file */
245 log_debug_msg("init_config_file(): setting hostID to %s\n", host_id);
246 g_key_file_set_value(key_file, "Global", "HostID", host_id);
247
248 /* Write config file on disk */
249 buf = g_key_file_to_data(key_file, &length, NULL);
250 config_file =
251 g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
252 file = g_io_channel_new_file(config_file, "w", NULL);
253 g_free(config_file);
254 g_io_channel_write_chars(file, buf, length, NULL, NULL);
255 g_io_channel_shutdown(file, TRUE, NULL);
256 g_io_channel_unref(file);
257
258 g_key_file_free(key_file);
259
260 /* Now write keys and certificates to disk */ 472 /* Now write keys and certificates to disk */
261 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL); 473 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL);
262 pFile = fopen(pem, "wb"); 474 pFile = fopen(pem, "wb");
diff --git a/src/userpref.h b/src/userpref.h
index e7835d0..deced04 100644
--- a/src/userpref.h
+++ b/src/userpref.h
@@ -8,64 +8,34 @@
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version. 10 * version 2.1 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details. 15 * Lesser General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Lesser General Public 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 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 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22#ifndef USERPREF_H 22#ifndef USERPREF_H
23#define USERPREF_H 23#define USERPREF_H
24 24
25#include <gnutls/gnutls.h> 25#include <gnutls/gnutls.h>
26/** 26#include "libiphone/libiphone.h"
27 * Method to get user's HostID. Caller must free returned buffer.
28 *
29 * @return the HostID if exist in config file. Returns NULL otherwise.
30 */
31char *get_host_id(void);
32 27
33/**
34 * Determine if we already paired this device.
35 *
36 * @return 1 if device is already paired. Returns 0 otherwise.
37 */
38int is_device_known(char *uid);
39 28
40/** 29iphone_error_t get_keys_and_certs(gnutls_x509_privkey_t root_privkey, gnutls_x509_crt_t root_crt, gnutls_x509_privkey_t host_privkey, gnutls_x509_crt_t host_crt);
41 * @return 1 if everything went well. Returns 0 otherwise.
42 */
43int store_device_public_key(char *uid, char *public_key);
44 30
45/** 31iphone_error_t get_certs_as_pem(gnutls_datum_t *pem_root_cert, gnutls_datum_t *pem_host_cert);
46 * @return 1 if everything went well. Returns 0 otherwise.
47 */
48int get_root_private_key(gnutls_datum_t * root_privkey);
49 32
50/** 33char *get_host_id(void);
51 * @return 1 if everything went well. Returns 0 otherwise.
52 */
53int get_host_private_key(gnutls_datum_t * host_privkey);
54 34
55/** 35int is_device_known(char *uid);
56 * @return 1 if everything went well. Returns 0 otherwise.
57 */
58int get_root_certificate(gnutls_datum_t * root_cert);
59 36
60/** 37int store_device_public_key(char *uid, gnutls_datum_t public_key);
61 * @return 1 if everything went well. Returns 0 otherwise.
62 */
63int get_host_certificate(gnutls_datum_t * host_cert);
64 38
65/** 39int init_config_file( gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert,
66 * Setup a brand new config file.
67 * @return 1 if everything went well. Returns 0 otherwise.
68 */
69int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert,
70 gnutls_datum_t * host_cert); 40 gnutls_datum_t * host_cert);
71#endif 41#endif
diff --git a/src/utils.c b/src/utils.c
index fb98471..5b0872d 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -23,6 +23,7 @@
23#include "utils.h" 23#include "utils.h"
24 24
25int toto_debug = 0; 25int toto_debug = 0;
26uint16_t dbg_mask = 0;
26 27
27/** 28/**
28 * Sets the level of debugging. Currently the only acceptable values are 0 and 29 * Sets the level of debugging. Currently the only acceptable values are 0 and
@@ -36,6 +37,15 @@ void iphone_set_debug(int level)
36} 37}
37 38
38 39
40/**
41 * Set debug ids to display. Values can be OR-ed
42 *
43 * @param level Set to 0 for no debugging or 1 for debugging.
44 */
45void iphone_set_debug_mask(uint16_t mask)
46{
47 dbg_mask = mask;
48}
39 49
40void log_debug_msg(const char *format, ...) 50void log_debug_msg(const char *format, ...)
41{ 51{
@@ -53,6 +63,21 @@ void log_debug_msg(const char *format, ...)
53#endif 63#endif
54} 64}
55 65
66void log_dbg_msg(uint16_t id, const char *format, ...)
67{
68#ifndef STRIP_DEBUG_CODE
69 if (id & dbg_mask) {
70 va_list args;
71 /* run the real fprintf */
72 va_start(args, format);
73
74 vfprintf(stderr, format, args);
75
76 va_end(args);
77 }
78#endif
79}
80
56inline void log_debug_buffer(const char *data, const int length) 81inline void log_debug_buffer(const char *data, const int length)
57{ 82{
58#ifndef STRIP_DEBUG_CODE 83#ifndef STRIP_DEBUG_CODE
diff --git a/src/utils.h b/src/utils.h
index 489f610..1750b8e 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -24,7 +24,11 @@
24 24
25#include "libiphone/libiphone.h" 25#include "libiphone/libiphone.h"
26 26
27
28
27inline void log_debug_msg(const char *format, ...); 29inline void log_debug_msg(const char *format, ...);
30inline void log_dbg_msg(uint16_t id, const char *format, ...);
31
28inline void log_debug_buffer(const char *data, const int length); 32inline void log_debug_buffer(const char *data, const int length);
29inline void dump_debug_buffer(const char *file, const char *data, const int length); 33inline void dump_debug_buffer(const char *file, const char *data, const int length);
30#endif 34#endif
diff --git a/swig/Makefile.am b/swig/Makefile.am
new file mode 100644
index 0000000..e47356b
--- /dev/null
+++ b/swig/Makefile.am
@@ -0,0 +1,18 @@
1INCLUDES = -I$(top_srcdir)/include $(libplist_CFLAGS)
2
3BUILT_SOURCES = $(srcdir)/iphone_wrap.c
4SWIG_SOURCES = iphone.i
5
6swigincludedir =$(includedir)/libiphone/swig
7swiginclude_HEADERS = $(SWIG_SOURCES)
8
9pkgpython_PYTHON = iPhone.py __init__.py
10pkgpyexec_LTLIBRARIES = _iPhone.la
11_iPhone_la_SOURCES = $(srcdir)/iphone_wrap.c $(SWIG_SOURCES)
12_iPhone_la_CFLAGS = $(PYTHON_CPPFLAGS) -I$(top_srcdir)/src
13_iPhone_la_LDFLAGS = -module $(PYTHON_LDFLAGS)
14_iPhone_la_LIBADD = ../src/libiphone.la
15
16$(srcdir)/iphone_wrap.c : $(SWIG_SOURCES)
17 $(SWIG) $(SWIG_PYTHON_OPT) $(INCLUDES) -I$(top_srcdir)/src -o $@ $<
18
diff --git a/swig/__init__.py b/swig/__init__.py
new file mode 100644
index 0000000..8d1c8b6
--- /dev/null
+++ b/swig/__init__.py
@@ -0,0 +1 @@
diff --git a/swig/iphone.i b/swig/iphone.i
new file mode 100644
index 0000000..3ae0999
--- /dev/null
+++ b/swig/iphone.i
@@ -0,0 +1,157 @@
1 /* swig.i */
2 %module(package="libiphone") iPhone
3 %feature("autodoc", "1");
4 %{
5 /* Includes the header in the wrapper code */
6 #include <libiphone/libiphone.h>
7 #include <plist/plist.h>
8#include "../src/utils.h"
9 typedef struct {
10 iphone_device_t dev;
11 } iPhone;
12
13 typedef struct {
14 iPhone* dev;
15 iphone_lckd_client_t client;
16 } Lockdownd;
17
18 typedef struct {
19 Lockdownd* lckd;
20 iphone_msync_client_t client;
21 } MobileSync;
22
23//now declare funtions to handle creation and deletion of objects
24void my_delete_iPhone(iPhone* dev);
25Lockdownd* my_new_Lockdownd(iPhone* phone);
26void my_delete_Lockdownd(Lockdownd* lckd);
27MobileSync* my_new_MobileSync(Lockdownd* lckd);
28
29 %}
30/* Parse the header file to generate wrappers */
31%include "stdint.i"
32%include "plist/swig/plist.i"
33
34typedef struct {
35 iphone_device_t dev;
36} iPhone;
37
38typedef struct {
39 iPhone* dev;
40 iphone_lckd_client_t client;
41} Lockdownd;
42
43typedef struct {
44 Lockdownd* lckd;
45 iphone_msync_client_t client;
46} MobileSync;
47
48%inline %{
49//now define funtions to handle creation and deletion of objects
50
51
52void my_delete_iPhone(iPhone* dev) {
53 if (dev) {
54 iphone_free_device ( dev->dev );
55 free(dev);
56 }
57}
58
59Lockdownd* my_new_Lockdownd(iPhone* phone) {
60 if (!phone) return NULL;
61 Lockdownd* client = (Lockdownd*) malloc(sizeof(Lockdownd));
62 client->dev = phone;
63 client->client = NULL;
64 if (IPHONE_E_SUCCESS == iphone_lckd_new_client ( phone->dev , &(client->client))) {
65 return client;
66 }
67 else {
68 free(client);
69 return NULL;
70 }
71}
72
73void my_delete_Lockdownd(Lockdownd* lckd) {
74 if (lckd) {
75 my_delete_iPhone(lckd->dev);
76 iphone_lckd_free_client ( lckd->client );
77 free(lckd);
78 }
79}
80
81MobileSync* my_new_MobileSync(Lockdownd* lckd) {
82 if (!lckd || !lckd->dev) return NULL;
83 MobileSync* client = NULL;
84 int port = 0;
85 if (IPHONE_E_SUCCESS == iphone_lckd_start_service ( lckd->client, "com.apple.mobilesync", &port )) {
86 client = (MobileSync*) malloc(sizeof(MobileSync));
87 client->lckd = lckd;
88 client->client = NULL;
89 iphone_msync_new_client ( lckd->dev->dev, 3432, port, &(client->client));
90 }
91 return client;
92}
93
94%}
95
96
97%extend iPhone { // Attach these functions to struct iPhone
98 iPhone() {
99 iPhone* phone = (iPhone*) malloc(sizeof(iPhone));
100 phone->dev = NULL;
101 iphone_set_debug_mask(DBGMASK_LOCKDOWND | DBGMASK_MOBILESYNC);
102 return phone;
103 }
104
105 ~iPhone() {
106 my_delete_iPhone($self);
107 }
108
109 int init_device() {
110 if (IPHONE_E_SUCCESS == iphone_get_device ( &($self->dev)))
111 return 1;
112 return 0;
113 }
114
115 Lockdownd* get_lockdown_client() {
116 return my_new_Lockdownd($self);
117 }
118};
119
120
121%extend Lockdownd { // Attach these functions to struct Lockdownd
122 Lockdownd(iPhone* phone) {
123 return my_new_Lockdownd(phone);
124 }
125
126 ~Lockdownd() {
127 my_delete_Lockdownd($self);
128 }
129
130 MobileSync* get_mobile_sync_client() {
131 return my_new_MobileSync($self);
132 }
133};
134
135%extend MobileSync { // Attach these functions to struct MobileSync
136 MobileSync(Lockdownd* lckd) {
137 return my_new_MobileSync(lckd);
138 }
139
140 ~MobileSync() {
141 my_delete_Lockdownd($self->lckd);
142 iphone_msync_free_client ( $self->client );
143 free($self);
144 }
145
146 void send(PListNode* node) {
147 iphone_msync_send($self->client, node->node);
148 }
149
150 PListNode* receive() {
151 PListNode* node = (PListNode*)malloc(sizeof(PListNode));
152 node->node = NULL;
153 iphone_msync_recv($self->client, &(node->node));
154 return node;
155 }
156};
157