summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am4
-rwxr-xr-xautogen.sh2
-rw-r--r--configure.ac14
-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.am8
-rw-r--r--src/MobileSync.c302
-rw-r--r--src/MobileSync.h39
-rw-r--r--src/NotificationProxy.c93
-rw-r--r--src/NotificationProxy.h1
-rw-r--r--src/initconf.c30
-rw-r--r--src/lockdown.c921
-rw-r--r--src/lockdown.h17
-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.c10
-rw-r--r--src/userpref.h2
-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.i156
34 files changed, 1808 insertions, 883 deletions
diff --git a/Makefile.am b/Makefile.am
index f103377..982f42b 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 $(DEV_SUB) 3SUBDIRS = src include fdi swig $(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/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 7c31e2a..a91d9f9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,20 +6,26 @@ 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 gnutls <= 2.5.0 ) 26PKG_CHECK_MODULES(libgnutls, gnutls >= 1.6.3 gnutls <= 2.5.0 )
22PKG_CHECK_MODULES(libtasn1, libtasn1 >= 1.1) 27PKG_CHECK_MODULES(libtasn1, libtasn1 >= 1.1)
28PKG_CHECK_MODULES(libplist, libplist >= 0.1.0)
23 29
24# Checks for header files. 30# Checks for header files.
25AC_HEADER_STDC 31AC_HEADER_STDC
@@ -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,5 @@ 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 dev/Makefile libiphone-1.0.pc) 98AC_OUTPUT(Makefile src/Makefile include/Makefile fdi/Makefile dev/Makefile swig/Makefile libiphone-1.0.pc)
99
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 7d9c223..0035e2f 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_free_device ( iphone_device_t device ); 99iphone_error_t iphone_free_device ( iphone_device_t device );
87 100
@@ -92,8 +105,8 @@ iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_clie
92iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client ); 105iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client );
93 106
94iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const char *service, int *port ); 107iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const char *service, int *port );
95iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, char **dump_data, uint32_t *recv_bytes ); 108iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, plist_t* plist);
96iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t *recv_bytes ); 109iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, plist_t plist);
97 110
98 111
99//usbmux related functions 112//usbmux related functions
@@ -125,6 +138,14 @@ iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir);
125iphone_error_t iphone_afc_truncate(iphone_afc_client_t client, const char *path, off_t newsize); 138iphone_error_t iphone_afc_truncate(iphone_afc_client_t client, const char *path, off_t newsize);
126 139
127 140
141
142iphone_error_t iphone_msync_new_client(iphone_device_t device, int src_port, int dst_port,
143 iphone_msync_client_t * client);
144iphone_error_t iphone_msync_free_client(iphone_msync_client_t client);
145
146iphone_error_t iphone_msync_recv(iphone_msync_client_t client, plist_t * plist);
147iphone_error_t iphone_msync_send(iphone_msync_client_t client, plist_t plist);
148
128#ifdef __cplusplus 149#ifdef __cplusplus
129} 150}
130#endif 151#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 62ec659..90b406a 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,5 +87,5 @@ 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);
91 90
91uint32_t iphone_afc_get_file_handle(iphone_afc_file_t file);
diff --git a/src/Makefile.am b/src/Makefile.am
index 76b15ec..71667ae 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,15 +1,15 @@
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 5
6bin_PROGRAMS = libiphone-initconf 6bin_PROGRAMS = libiphone-initconf
7 7
8 8
9libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c utils.c 9libiphone_initconf_SOURCES = initconf.c userpref.c utils.c
10libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS) 10libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS)
11libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS) 11libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS)
12 12
13 13
14lib_LTLIBRARIES = libiphone.la 14lib_LTLIBRARIES = libiphone.la
15libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c NotificationProxy.c userpref.c utils.c 15libiphone_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..839ed2b
--- /dev/null
+++ b/src/MobileSync.c
@@ -0,0 +1,302 @@
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 =
54 plist_find_node_by_string(array, "DLMessageVersionExchange");
55 plist_t ver_1 = plist_get_next_sibling(msg_node);
56 plist_t ver_2 = plist_get_next_sibling(ver_1);
57
58 plist_type ver_1_type = plist_get_node_type(ver_1);
59 plist_type ver_2_type = plist_get_node_type(ver_2);
60
61 if (PLIST_UINT == ver_1_type && PLIST_UINT == ver_2_type) {
62
63 uint64_t ver_1_val = 0;
64 uint64_t ver_2_val = 0;
65
66 plist_get_uint_val(ver_1, &ver_1_val);
67 plist_get_uint_val(ver_2, &ver_2_val);
68
69 plist_free(array);
70 array = NULL;
71
72 if (ver_1_type == PLIST_UINT && ver_2_type == PLIST_UINT && ver_1_val == MSYNC_VERSION_INT1
73 && ver_2_val == MSYNC_VERSION_INT2) {
74
75 array = plist_new_array();
76 plist_add_sub_string_el(array, "DLMessageVersionExchange");
77 plist_add_sub_string_el(array, "DLVersionsOk");
78
79 ret = iphone_msync_send(client_loc, array);
80
81 plist_free(array);
82 array = NULL;
83
84 ret = iphone_msync_recv(client_loc, &array);
85 plist_t rep_node =
86 plist_find_node_by_string(array, "DLMessageDeviceReady");
87
88 if (rep_node) {
89 ret = IPHONE_E_SUCCESS;
90 *client = client_loc;
91 }
92 plist_free(array);
93 array = NULL;
94
95 }
96 }
97
98 if (IPHONE_E_SUCCESS != ret)
99 iphone_msync_free_client(client_loc);
100
101 return ret;
102}
103
104static void iphone_msync_stop_session(iphone_msync_client_t client)
105{
106 if (!client)
107 return;
108
109 plist_t array = plist_new_array();
110 plist_add_sub_string_el(array, "DLMessageDisconnect");
111 plist_add_sub_string_el(array, "All done, thanks for the memories");
112
113 iphone_msync_send(client, array);
114 plist_free(array);
115 array = NULL;
116}
117
118iphone_error_t iphone_msync_free_client(iphone_msync_client_t client)
119{
120 if (!client)
121 return IPHONE_E_INVALID_ARG;
122
123 iphone_msync_stop_session(client);
124 return iphone_mux_free_client(client->connection);
125}
126
127/** Polls the iPhone for MobileSync data.
128 *
129 * @param client The MobileSync client
130 * @param dump_data The pointer to the location of the buffer in which to store
131 * the received data
132 * @param recv_byhtes The number of bytes received
133 *
134 * @return an error code
135 */
136iphone_error_t iphone_msync_recv(iphone_msync_client_t client, plist_t * plist)
137{
138 if (!client || !plist || (plist && *plist))
139 return IPHONE_E_INVALID_ARG;
140 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
141 char *receive;
142 uint32_t datalen = 0, bytes = 0;
143
144 ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes);
145 datalen = ntohl(datalen);
146
147 receive = (char *) malloc(sizeof(char) * datalen);
148 ret = iphone_mux_recv(client->connection, receive, datalen, &bytes);
149
150 plist_from_bin(receive, bytes, plist);
151
152 char *XMLContent = NULL;
153 uint32_t length = 0;
154 plist_to_xml(*plist, &XMLContent, &length);
155 log_dbg_msg(DBGMASK_MOBILESYNC, "Recv msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent);
156 free(XMLContent);
157
158 return ret;
159}
160
161/** Sends MobileSync data to the iPhone
162 *
163 * @note This function is low-level and should only be used if you need to send
164 * a new type of message.
165 *
166 * @param client The MobileSync client
167 * @param raw_data The null terminated string buffer to send
168 * @param length The length of data to send
169 * @param sent_bytes The number of bytes sent
170 *
171 * @return an error code
172 */
173iphone_error_t iphone_msync_send(iphone_msync_client_t client, plist_t plist)
174{
175 if (!client || !plist)
176 return IPHONE_E_INVALID_ARG;
177
178 char *XMLContent = NULL;
179 uint32_t length = 0;
180 plist_to_xml(plist, &XMLContent, &length);
181 log_dbg_msg(DBGMASK_MOBILESYNC, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent);
182 free(XMLContent);
183
184 char *content = NULL;
185 length = 0;
186
187 plist_to_bin(plist, &content, &length);
188
189 char *real_query;
190 int bytes;
191 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
192
193 real_query = (char *) malloc(sizeof(char) * (length + 4));
194 length = htonl(length);
195 memcpy(real_query, &length, sizeof(length));
196 memcpy(real_query + 4, content, ntohl(length));
197
198 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes);
199 free(real_query);
200 return ret;
201}
202
203iphone_error_t iphone_msync_get_all_contacts(iphone_msync_client_t client)
204{
205 if (!client)
206 return IPHONE_E_INVALID_ARG;
207
208 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
209 plist_t array = NULL;
210
211 array = plist_new_array();
212 plist_add_sub_string_el(array, "SDMessageSyncDataClassWithDevice");
213 plist_add_sub_string_el(array, "com.apple.Contacts");
214 plist_add_sub_string_el(array, "---");
215 plist_add_sub_string_el(array, "2009-01-09 18:03:58 +0100");
216 plist_add_sub_uint_el(array, 106);
217 plist_add_sub_string_el(array, "___EmptyParameterString___");
218
219 ret = iphone_msync_send(client, array);
220 plist_free(array);
221 array = NULL;
222
223 ret = iphone_msync_recv(client, &array);
224
225 plist_t rep_node = plist_find_node_by_string(array, "SDSyncTypeSlow");
226
227 if (!rep_node)
228 return ret;
229
230 plist_free(array);
231 array = NULL;
232
233 array = plist_new_array();
234 plist_add_sub_string_el(array, "SDMessageGetAllRecordsFromDevice");
235 plist_add_sub_string_el(array, "com.apple.Contacts");
236
237
238 ret = iphone_msync_send(client, array);
239 plist_free(array);
240 array = NULL;
241
242 ret = iphone_msync_recv(client, &array);
243
244 plist_t contact_node;
245 plist_t switch_node;
246
247 contact_node = plist_find_node_by_string(array, "com.apple.Contacts");
248 switch_node =
249 plist_find_node_by_string(array, "SDMessageDeviceReadyToReceiveChanges");
250
251 while (NULL == switch_node) {
252
253 plist_free(array);
254 array = NULL;
255
256 array = plist_new_array();
257 plist_add_sub_string_el(array, "SDMessageAcknowledgeChangesFromDevice");
258 plist_add_sub_string_el(array, "com.apple.Contacts");
259
260 ret = iphone_msync_send(client, array);
261 plist_free(array);
262 array = NULL;
263
264 ret = iphone_msync_recv(client, &array);
265
266 contact_node = plist_find_node_by_string(array, "com.apple.Contacts");
267 switch_node = plist_find_node_by_string(array, "SDMessageDeviceReadyToReceiveChanges");
268 }
269
270 array = plist_new_array();
271 plist_add_sub_string_el(array, "DLMessagePing");
272 plist_add_sub_string_el(array, "Preparing to get changes for device");
273
274 ret = iphone_msync_send(client, array);
275 plist_free(array);
276 array = NULL;
277
278 array = plist_new_array();
279 plist_add_sub_string_el(array, "SDMessageProcessChanges");
280 plist_add_sub_string_el(array, "com.apple.Contacts");
281 plist_add_sub_node(array, plist_new_dict());
282 plist_add_sub_bool_el(array, 0);
283 plist_t dict = plist_new_dict();
284 plist_add_sub_node(array, dict);
285 plist_add_sub_key_el(dict, "SyncDeviceLinkEntityNamesKey");
286 plist_t array2 = plist_new_array();
287 plist_add_sub_string_el(array2, "com.apple.contacts.Contact");
288 plist_add_sub_string_el(array2, "com.apple.contacts.Group");
289 plist_add_sub_key_el(dict, "SyncDeviceLinkAllRecordsOfPulledEntityTypeSentKey");
290 plist_add_sub_bool_el(dict, 0);
291
292 ret = iphone_msync_send(client, array);
293 plist_free(array);
294 array = NULL;
295
296 ret = iphone_msync_recv(client, &array);
297 plist_free(array);
298 array = NULL;
299
300
301 return ret;
302}
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 b0c10ea..eec7857 100644
--- a/src/NotificationProxy.c
+++ b/src/NotificationProxy.c
@@ -20,8 +20,8 @@
20 */ 20 */
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <plist/plist.h>
23#include "NotificationProxy.h" 24#include "NotificationProxy.h"
24#include "plist.h"
25#include "utils.h" 25#include "utils.h"
26 26
27/** Locks an NP client, done for thread safety stuff. 27/** Locks an NP client, done for thread safety stuff.
@@ -103,26 +103,26 @@ iphone_error_t iphone_np_free_client ( iphone_np_client_t client )
103 */ 103 */
104iphone_error_t iphone_np_post_notification( iphone_np_client_t client, const char *notification ) 104iphone_error_t iphone_np_post_notification( iphone_np_client_t client, const char *notification )
105{ 105{
106 xmlDocPtr plist; 106 char *XML_content = NULL;
107 xmlNode *dict, *key; 107 uint32_t length = 0;
108 char *XML_content; 108 int bytes = 0;
109 uint32_t length;
110 int bytes;
111 iphone_error_t ret; 109 iphone_error_t ret;
112 unsigned char sndbuf[4096]; 110 unsigned char sndbuf[4096];
113 int sndlen = 0; 111 int sndlen = 0;
114 int nlen; 112 int nlen = 0;
113 plist_t dict = NULL;
115 114
116 if (!client || !notification) { 115 if (!client || !notification) {
117 return IPHONE_E_INVALID_ARG; 116 return IPHONE_E_INVALID_ARG;
118 } 117 }
119 np_lock(client); 118 np_lock(client);
120 119
121 plist = new_plist(); 120 dict = plist_new_dict();
122 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 121 plist_add_sub_key_el(dict, "Command");
123 key = add_key_str_dict_element(plist, dict, "Command", "PostNotification", 1); 122 plist_add_sub_string_el(dict, "PostNotification");
124 key = add_key_str_dict_element(plist, dict, "Name", notification, 1); 123 plist_add_sub_key_el(dict, "Name");
125 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 124 plist_add_sub_string_el(dict, notification);
125 plist_to_xml(dict, &XML_content, &length);
126 126
127 nlen = htonl(length); 127 nlen = htonl(length);
128 128
@@ -130,14 +130,16 @@ iphone_error_t iphone_np_post_notification( iphone_np_client_t client, const cha
130 sndlen += 4; 130 sndlen += 4;
131 memcpy(sndbuf+sndlen, XML_content, length); 131 memcpy(sndbuf+sndlen, XML_content, length);
132 sndlen += length; 132 sndlen += length;
133
134 xmlFree(XML_content);
135 xmlFreeDoc(plist);
136 133
137 plist = new_plist(); 134 plist_free(dict);
138 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 135 dict = NULL;
139 key = add_key_str_dict_element(plist, dict, "Command", "Shutdown", 1); 136 free(XML_content);
140 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 137 XML_content = NULL;
138
139 dict = plist_new_dict();
140 plist_add_sub_key_el(dict, "Command");
141 plist_add_sub_string_el(dict, "Shutdown");
142 plist_to_xml(dict, &XML_content, &length);
141 143
142 nlen = htonl(length); 144 nlen = htonl(length);
143 145
@@ -147,9 +149,10 @@ iphone_error_t iphone_np_post_notification( iphone_np_client_t client, const cha
147 memcpy(sndbuf+sndlen, XML_content, length); 149 memcpy(sndbuf+sndlen, XML_content, length);
148 sndlen+=length; 150 sndlen+=length;
149 151
150 xmlFree(XML_content); 152 plist_free(dict);
151 xmlFreeDoc(plist); 153 dict = NULL;
152 plist = NULL; 154 free(XML_content);
155 XML_content = NULL;
153 156
154 log_debug_buffer(sndbuf, sndlen); 157 log_debug_buffer(sndbuf, sndlen);
155 158
@@ -180,17 +183,16 @@ iphone_error_t iphone_np_post_notification( iphone_np_client_t client, const cha
180 */ 183 */
181iphone_error_t iphone_np_observe_notification( iphone_np_client_t client ) 184iphone_error_t iphone_np_observe_notification( iphone_np_client_t client )
182{ 185{
183 xmlDocPtr plist; 186 plist_t dict = NULL;
184 xmlNode *dict, *key; 187 char *XML_content = NULL;
185 char *XML_content; 188 uint32_t length = 0;
186 uint32_t length; 189 int bytes = 0;
187 int bytes;
188 iphone_error_t ret; 190 iphone_error_t ret;
189 unsigned char sndbuf[4096]; 191 unsigned char sndbuf[4096];
190 int sndlen = 0; 192 int sndlen = 0;
191 int nlen; 193 int nlen = 0;
192 int i=0; 194 int i=0;
193 char *notifications[10] = { 195 const char *notifications[10] = {
194 "com.apple.itunes-client.syncCancelRequest", 196 "com.apple.itunes-client.syncCancelRequest",
195 "com.apple.itunes-client.syncSuspendRequest", 197 "com.apple.itunes-client.syncSuspendRequest",
196 "com.apple.itunes-client.syncResumeRequest", 198 "com.apple.itunes-client.syncResumeRequest",
@@ -210,11 +212,13 @@ iphone_error_t iphone_np_observe_notification( iphone_np_client_t client )
210 np_lock(client); 212 np_lock(client);
211 213
212 while (notifications[i]) { 214 while (notifications[i]) {
213 plist = new_plist(); 215
214 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 216 dict = plist_new_dict();
215 key = add_key_str_dict_element(plist, dict, "Command", "ObserveNotification", 1); 217 plist_add_sub_key_el(dict, "Command");
216 key = add_key_str_dict_element(plist, dict, "Name", notifications[i++], 1); 218 plist_add_sub_string_el(dict, "ObserveNotification");
217 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 219 plist_add_sub_key_el(dict, "Name");
220 plist_add_sub_string_el(dict, notifications[i++]);
221 plist_to_xml(dict, &XML_content, &length);
218 222
219 nlen = htonl(length); 223 nlen = htonl(length);
220 memcpy(sndbuf+sndlen, &nlen, 4); 224 memcpy(sndbuf+sndlen, &nlen, 4);
@@ -222,14 +226,16 @@ iphone_error_t iphone_np_observe_notification( iphone_np_client_t client )
222 memcpy(sndbuf+sndlen, XML_content, length); 226 memcpy(sndbuf+sndlen, XML_content, length);
223 sndlen += length; 227 sndlen += length;
224 228
225 xmlFree(XML_content); 229 plist_free(dict);
226 xmlFreeDoc(plist); 230 dict = NULL;
231 free(XML_content);
232 XML_content = NULL;
227 } 233 }
228 234
229 plist = new_plist(); 235 dict = plist_new_dict();
230 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 236 plist_add_sub_key_el(dict, "Command");
231 key = add_key_str_dict_element(plist, dict, "Command", "Shutdown", 1); 237 plist_add_sub_string_el(dict, "Shutdown");
232 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 238 plist_to_xml(dict, &XML_content, &length);
233 239
234 nlen = htonl(length); 240 nlen = htonl(length);
235 241
@@ -239,9 +245,10 @@ iphone_error_t iphone_np_observe_notification( iphone_np_client_t client )
239 memcpy(sndbuf+sndlen, XML_content, length); 245 memcpy(sndbuf+sndlen, XML_content, length);
240 sndlen+=length; 246 sndlen+=length;
241 247
242 xmlFree(XML_content); 248 plist_free(dict);
243 xmlFreeDoc(plist); 249 dict = NULL;
244 plist = NULL; 250 free(XML_content);
251 XML_content = NULL;
245 252
246 log_debug_buffer(sndbuf, sndlen); 253 log_debug_buffer(sndbuf, sndlen);
247 254
diff --git a/src/NotificationProxy.h b/src/NotificationProxy.h
index 7b47346..57ad751 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
index 205c97a..538f344 100644
--- a/src/initconf.c
+++ b/src/initconf.c
@@ -28,7 +28,6 @@
28 28
29#include "libiphone/libiphone.h" 29#include "libiphone/libiphone.h"
30#include "userpref.h" 30#include "userpref.h"
31#include "lockdown.h"
32#include "utils.h" 31#include "utils.h"
33 32
34/** Generates a 2048 byte key, split into a function so that it can be run in a 33/** Generates a 2048 byte key, split into a function so that it can be run in a
@@ -60,6 +59,35 @@ static void progress_bar(gpointer mutex)
60 g_thread_exit(0); 59 g_thread_exit(0);
61} 60}
62 61
62int get_rand(int min, int max)
63{
64 int retval = (rand() % (max - min)) + min;
65 return retval;
66}
67
68/** Generates a valid HostID (which is actually a UUID).
69 *
70 * @param A null terminated string containing a valid HostID.
71 */
72char *lockdownd_generate_hostid()
73{
74 char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long
75 const char *chars = "ABCDEF0123456789";
76 srand(time(NULL));
77 int i = 0;
78
79 for (i = 0; i < 36; i++) {
80 if (i == 8 || i == 13 || i == 18 || i == 23) {
81 hostid[i] = '-';
82 continue;
83 } else {
84 hostid[i] = chars[get_rand(0, 16)];
85 }
86 }
87 hostid[36] = '\0'; // make it a real string
88 return hostid;
89}
90
63int main(int argc, char *argv[]) 91int main(int argc, char *argv[])
64{ 92{
65 GThread *progress_thread, *key_thread; 93 GThread *progress_thread, *key_thread;
diff --git a/src/lockdown.c b/src/lockdown.c
index 2d85a03..2f48dfd 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -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
@@ -101,57 +75,56 @@ iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone)
101static void iphone_lckd_stop_session(iphone_lckd_client_t control) 75static void iphone_lckd_stop_session(iphone_lckd_client_t control)
102{ 76{
103 if (!control) 77 if (!control)
104 return; // IPHONE_E_INVALID_ARG; 78 return; //IPHONE_E_INVALID_ARG;
105 xmlDocPtr plist = new_plist(); 79
106 xmlNode *dict, *key;
107 char **dictionary;
108 int bytes = 0, i = 0;
109 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 80 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
110 81
111 log_debug_msg("lockdownd_stop_session() called\n"); 82 plist_t dict = plist_new_dict();
112 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 83 plist_add_sub_key_el(dict, "Request");
113 key = add_key_str_dict_element(plist, dict, "Request", "StopSession", 1); 84 plist_add_sub_string_el(dict, "StopSession");
114 key = add_key_str_dict_element(plist, dict, "SessionID", control->session_id, 1); 85 plist_add_sub_key_el(dict, "SessionID");
86 plist_add_sub_string_el(dict, control->session_id);
115 87
116 char *XML_content; 88 log_dbg_msg(DBGMASK_LOCKDOWND, "iphone_lckd_stop_session() called\n");
117 uint32 length;
118 89
119 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 90 ret = iphone_lckd_send(control, dict);
120 ret = iphone_lckd_send(control, XML_content, length, &bytes);
121 91
122 xmlFree(XML_content); 92 plist_free(dict);
123 xmlFreeDoc(plist); 93 dict = NULL;
124 plist = NULL; 94
125 ret = iphone_lckd_recv(control, &XML_content, &bytes); 95 ret = iphone_lckd_recv(control, &dict);
126 96
127 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
128 if (!plist) {
129 log_debug_msg("lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n");
130 return; //IPHONE_E_PLIST_ERROR;
131 }
132 dict = xmlDocGetRootElement(plist);
133 for (dict = dict->children; dict; dict = dict->next) {
134 if (!xmlStrcmp(dict->name, "dict"))
135 break;
136 }
137 if (!dict) { 97 if (!dict) {
138 log_debug_msg("lockdownd_stop_session(): IPHONE_E_DICT_ERROR\n"); 98 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n");
139 return; //IPHONE_E_DICT_ERROR; 99 return; // IPHONE_E_PLIST_ERROR;
140 } 100 }
141 dictionary = read_dict_element_strings(dict);
142 xmlFreeDoc(plist);
143 free(XML_content);
144 101
145 for (i = 0; dictionary[i]; i += 2) { 102 plist_t query_node = plist_find_node_by_string(dict, "StopSession");
146 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 103 plist_t result_node = plist_get_next_sibling(query_node);
147 log_debug_msg("lockdownd_stop_session(): success\n"); 104 plist_t value_node = plist_get_next_sibling(result_node);
105
106 plist_type result_type = plist_get_node_type(result_node);
107 plist_type value_type = plist_get_node_type(value_node);
108
109 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
110
111 char *result_value = NULL;
112 char *value_value = NULL;
113
114 plist_get_key_val(result_node, &result_value);
115 plist_get_string_val(value_node, &value_value);
116
117 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
118 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): success\n");
148 ret = IPHONE_E_SUCCESS; 119 ret = IPHONE_E_SUCCESS;
149 break;
150 } 120 }
121 free(result_value);
122 free(value_value);
151 } 123 }
124 plist_free(dict);
125 dict = NULL;
152 126
153 free_dictionary(dictionary); 127 return; // ret;
154 return; //ret;
155} 128}
156 129
157/** 130/**
@@ -164,14 +137,14 @@ static void iphone_lckd_stop_session(iphone_lckd_client_t control)
164static void iphone_lckd_stop_SSL_session(iphone_lckd_client_t client) 137static void iphone_lckd_stop_SSL_session(iphone_lckd_client_t client)
165{ 138{
166 if (!client) { 139 if (!client) {
167 log_debug_msg("lockdownd_stop_SSL_session(): invalid argument!\n"); 140 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_SSL_session(): invalid argument!\n");
168 return; 141 return;
169 } 142 }
170 143
171 if (client->in_SSL) { 144 if (client->in_SSL) {
172 log_debug_msg("Stopping SSL Session\n"); 145 log_dbg_msg(DBGMASK_LOCKDOWND, "Stopping SSL Session\n");
173 iphone_lckd_stop_session(client); 146 iphone_lckd_stop_session(client);
174 log_debug_msg("Sending SSL close notify\n"); 147 log_dbg_msg(DBGMASK_LOCKDOWND, "Sending SSL close notify\n");
175 gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR); 148 gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR);
176 } 149 }
177 if (client->ssl_session) { 150 if (client->ssl_session) {
@@ -217,13 +190,13 @@ iphone_error_t iphone_lckd_free_client(iphone_lckd_client_t client)
217 * 190 *
218 * @return The number of bytes received 191 * @return The number of bytes received
219 */ 192 */
220iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, uint32_t * recv_bytes) 193iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, plist_t * plist)
221{ 194{
222 if (!client || !dump_data || !recv_bytes) 195 if (!client || !plist || (plist && *plist))
223 return IPHONE_E_INVALID_ARG; 196 return IPHONE_E_INVALID_ARG;
224 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 197 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
225 char *receive; 198 char *receive;
226 uint32 datalen = 0, bytes = 0; 199 uint32_t datalen = 0, bytes = 0;
227 200
228 if (!client->in_SSL) 201 if (!client->in_SSL)
229 ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes); 202 ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes);
@@ -242,8 +215,18 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, u
242 if (bytes > 0) 215 if (bytes > 0)
243 ret = IPHONE_E_SUCCESS; 216 ret = IPHONE_E_SUCCESS;
244 } 217 }
245 *dump_data = receive; 218
246 *recv_bytes = bytes; 219 if (bytes <= 0) {
220 free(receive);
221 return IPHONE_E_NOT_ENOUGH_DATA;
222 }
223
224 plist_from_xml(receive, bytes, plist);
225 free(receive);
226
227 if (!*plist)
228 ret = IPHONE_E_PLIST_ERROR;
229
247 return ret; 230 return ret;
248} 231}
249 232
@@ -252,26 +235,31 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, u
252 * @note This function is low-level and should only be used if you need to send 235 * @note This function is low-level and should only be used if you need to send
253 * a new type of message. 236 * a new type of message.
254 * 237 *
255 * @param control The lockdownd client 238 * @param client The lockdownd client
256 * @param raw_data The null terminated string buffer to send 239 * @param plist The plist to send
257 * @param length The length of data to send
258 * 240 *
259 * @return The number of bytes sent 241 * @return an error code (IPHONE_E_SUCCESS on success)
260 */ 242 */
261iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t * sent_bytes) 243iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, plist_t plist)
262{ 244{
263 if (!client || !raw_data || length == 0 || !sent_bytes) 245 if (!client || !plist)
264 return IPHONE_E_INVALID_ARG; 246 return IPHONE_E_INVALID_ARG;
265 char *real_query; 247 char *real_query;
266 int bytes; 248 int bytes;
249 char *XMLContent = NULL;
250 uint32_t length = 0;
267 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 251 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
268 252
253 plist_to_xml(plist, &XMLContent, &length);
254 log_dbg_msg(DBGMASK_LOCKDOWND, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent);
255
256
269 real_query = (char *) malloc(sizeof(char) * (length + 4)); 257 real_query = (char *) malloc(sizeof(char) * (length + 4));
270 length = htonl(length); 258 length = htonl(length);
271 memcpy(real_query, &length, sizeof(length)); 259 memcpy(real_query, &length, sizeof(length));
272 memcpy(real_query + 4, raw_data, ntohl(length)); 260 memcpy(real_query + 4, XMLContent, ntohl(length));
273 log_debug_msg("lockdownd_send(): made the query, sending it along\n"); 261 free(XMLContent);
274 dump_debug_buffer("grpkt", real_query, ntohl(length) + 4); 262 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): made the query, sending it along\n");
275 263
276 if (!client->in_SSL) 264 if (!client->in_SSL)
277 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); 265 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes);
@@ -279,9 +267,9 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin
279 gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); 267 gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length));
280 ret = IPHONE_E_SUCCESS; 268 ret = IPHONE_E_SUCCESS;
281 } 269 }
282 log_debug_msg("lockdownd_send(): sent it!\n"); 270 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): sent it!\n");
283 free(real_query); 271 free(real_query);
284 *sent_bytes = bytes; 272
285 return ret; 273 return ret;
286} 274}
287 275
@@ -297,49 +285,50 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
297{ 285{
298 if (!control) 286 if (!control)
299 return IPHONE_E_INVALID_ARG; 287 return IPHONE_E_INVALID_ARG;
300 xmlDocPtr plist = new_plist(); 288
301 xmlNode *dict, *key;
302 char **dictionary;
303 int bytes = 0, i = 0;
304 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 289 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
305 290
306 log_debug_msg("lockdownd_hello() called\n"); 291 plist_t dict = plist_new_dict();
307 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 292 plist_add_sub_key_el(dict, "Request");
308 key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); 293 plist_add_sub_string_el(dict, "QueryType");
309 char *XML_content;
310 uint32 length;
311 294
312 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 295 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_hello() called\n");
313 ret = iphone_lckd_send(control, XML_content, length, &bytes); 296 ret = iphone_lckd_send(control, dict);
314 297
315 xmlFree(XML_content); 298 plist_free(dict);
316 xmlFreeDoc(plist); 299 dict = NULL;
317 plist = NULL;
318 ret = iphone_lckd_recv(control, &XML_content, &bytes);
319 300
320 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 301 ret = iphone_lckd_recv(control, &dict);
321 if (!plist) 302
322 return IPHONE_E_PLIST_ERROR; 303 if (IPHONE_E_SUCCESS != ret)
323 dict = xmlDocGetRootElement(plist); 304 return ret;
324 for (dict = dict->children; dict; dict = dict->next) { 305
325 if (!xmlStrcmp(dict->name, "dict")) 306 plist_t query_node = plist_find_node_by_string(dict, "QueryType");
326 break; 307 plist_t result_node = plist_get_next_sibling(query_node);
327 } 308 plist_t value_node = plist_get_next_sibling(result_node);
328 if (!dict) 309
329 return IPHONE_E_DICT_ERROR; 310 plist_type result_type = plist_get_node_type(result_node);
330 dictionary = read_dict_element_strings(dict); 311 plist_type value_type = plist_get_node_type(value_node);
331 xmlFreeDoc(plist); 312
332 free(XML_content); 313 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
333 314
334 for (i = 0; dictionary[i]; i += 2) { 315 char *result_value = NULL;
335 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 316 char *value_value = NULL;
336 log_debug_msg("lockdownd_hello(): success\n"); 317
318 plist_get_key_val(result_node, &result_value);
319 plist_get_string_val(value_node, &value_value);
320
321 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
322 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_hello(): success\n");
337 ret = IPHONE_E_SUCCESS; 323 ret = IPHONE_E_SUCCESS;
338 break;
339 } 324 }
325 free(result_value);
326 free(value_value);
340 } 327 }
341 328
342 free_dictionary(dictionary); 329 plist_free(dict);
330 dict = NULL;
331
343 return ret; 332 return ret;
344} 333}
345 334
@@ -351,74 +340,91 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
351 * 340 *
352 * @return IPHONE_E_SUCCESS on success. 341 * @return IPHONE_E_SUCCESS on success.
353 */ 342 */
354iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, const char *req_string, 343iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, char *req_string,
355 char **value) 344 gnutls_datum_t * value)
356{ 345{
357 if (!control || !req_key || !value || (value && *value)) 346 if (!control || !req_key || !value || value->data)
358 return IPHONE_E_INVALID_ARG; 347 return IPHONE_E_INVALID_ARG;
359 xmlDocPtr plist = new_plist(); 348
360 xmlNode *dict = NULL; 349 plist_t dict = NULL;
361 xmlNode *key = NULL;;
362 char **dictionary = NULL;
363 int bytes = 0, i = 0;
364 char *XML_content = NULL;
365 uint32 length = 0;
366 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 350 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
367 351
368 /* Setup DevicePublicKey request plist */ 352 /* Setup DevicePublicKey request plist */
369 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 353 dict = plist_new_dict();
370 key = add_key_str_dict_element(plist, dict, req_key, req_string, 1); 354 plist_add_sub_key_el(dict, req_key);
371 key = add_key_str_dict_element(plist, dict, "Request", "GetValue", 1); 355 plist_add_sub_string_el(dict, req_string);
372 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 356 plist_add_sub_key_el(dict, "Request");
357 plist_add_sub_string_el(dict, "GetValue");
373 358
374 /* send to iPhone */ 359 /* send to iPhone */
375 ret = iphone_lckd_send(control, XML_content, length, &bytes); 360 ret = iphone_lckd_send(control, dict);
376 361
377 xmlFree(XML_content); 362 plist_free(dict);
378 xmlFreeDoc(plist); 363 dict = NULL;
379 plist = NULL;
380 364
381 if (ret != IPHONE_E_SUCCESS) 365 if (ret != IPHONE_E_SUCCESS)
382 return ret; 366 return ret;
383 367
384 /* Now get iPhone's answer */ 368 /* Now get iPhone's answer */
385 ret = iphone_lckd_recv(control, &XML_content, &bytes); 369 ret = iphone_lckd_recv(control, &dict);
386 370
387 if (ret != IPHONE_E_SUCCESS) 371 if (ret != IPHONE_E_SUCCESS)
388 return ret; 372 return ret;
389 373
390 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 374 plist_t query_node = plist_find_node_by_string(dict, "GetValue");
391 if (!plist) 375 plist_t result_key_node = plist_get_next_sibling(query_node);
392 return IPHONE_E_PLIST_ERROR; 376 plist_t result_value_node = plist_get_next_sibling(result_key_node);
393 dict = xmlDocGetRootElement(plist);
394 for (dict = dict->children; dict; dict = dict->next) {
395 if (!xmlStrcmp(dict->name, "dict"))
396 break;
397 }
398 if (!dict)
399 return IPHONE_E_DICT_ERROR;
400 377
401 /* Parse xml to check success and to find public key */ 378 plist_type result_key_type = plist_get_node_type(result_key_node);
402 dictionary = read_dict_element_strings(dict); 379 plist_type result_value_type = plist_get_node_type(result_value_node);
403 xmlFreeDoc(plist);
404 free(XML_content);
405 380
406 int success = 0; 381 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
407 for (i = 0; dictionary[i]; i += 2) { 382
408 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 383 char *result_key = NULL;
409 success = 1; 384 char *result_value = NULL;
410 } 385 ret = IPHONE_E_DICT_ERROR;
411 if (!strcmp(dictionary[i], "Value")) { 386
412 *value = strdup(dictionary[i + 1]); 387 plist_get_key_val(result_key_node, &result_key);
388 plist_get_string_val(result_value_node, &result_value);
389
390 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
391 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n");
392 ret = IPHONE_E_SUCCESS;
413 } 393 }
394 free(result_key);
395 free(result_value);
396 }
397 if (ret != IPHONE_E_SUCCESS) {
398 return ret;
414 } 399 }
415 400
416 if (dictionary) { 401 plist_t value_key_node = plist_get_next_sibling(result_key_node);
417 free_dictionary(dictionary); 402 plist_t value_value_node = plist_get_next_sibling(value_key_node);
418 dictionary = NULL; 403
404 plist_type value_key_type = plist_get_node_type(value_key_node);
405
406 if (value_key_type == PLIST_KEY) {
407
408 char *result_key = NULL;
409 plist_get_key_val(value_key_node, &result_key);
410
411 if (!strcmp(result_key, "Value")) {
412 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n");
413
414 plist_type value_value_type = plist_get_node_type(value_value_node);
415 if (PLIST_STRING == value_value_type) {
416 char *value_value = NULL;
417 plist_get_string_val(value_value_node, &value_value);
418
419 value->data = value_value;
420 value->size = strlen(value_value);
421 ret = IPHONE_E_SUCCESS;
422 }
423 }
424 free(result_key);
419 } 425 }
420 if (success) 426
421 ret = IPHONE_E_SUCCESS; 427 plist_free(dict);
422 return ret; 428 return ret;
423} 429}
424 430
@@ -430,7 +436,9 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c
430 */ 436 */
431iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid) 437iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid)
432{ 438{
433 return lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", uid); 439 gnutls_datum_t temp = { NULL, 0 };
440 return lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", &temp);
441 *uid = temp.data;
434} 442}
435 443
436/** Askes for the device's public key. Part of the lockdownd handshake. 444/** Askes for the device's public key. Part of the lockdownd handshake.
@@ -439,7 +447,7 @@ iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid
439 * 447 *
440 * @return 1 on success and 0 on failure. 448 * @return 1 on success and 0 on failure.
441 */ 449 */
442iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key) 450iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, gnutls_datum_t * public_key)
443{ 451{
444 return lockdownd_generic_get_value(control, "Key", "DevicePublicKey", public_key); 452 return lockdownd_generic_get_value(control, "Key", "DevicePublicKey", public_key);
445} 453}
@@ -511,107 +519,91 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client
511iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id) 519iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id)
512{ 520{
513 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 521 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
514 xmlDocPtr plist = new_plist(); 522 plist_t dict = NULL;
515 xmlNode *dict = NULL; 523 plist_t dict_record = NULL;
516 xmlNode *dictRecord = NULL; 524
517 char **dictionary = NULL; 525 gnutls_datum_t device_cert = { NULL, 0 };
518 int bytes = 0, i = 0; 526 gnutls_datum_t host_cert = { NULL, 0 };
519 char *XML_content = NULL; 527 gnutls_datum_t root_cert = { NULL, 0 };
520 uint32 length = 0; 528 gnutls_datum_t public_key = { NULL, 0 };
521 529
522 char *device_cert_b64 = NULL; 530 ret = lockdownd_get_device_public_key(control, &public_key);
523 char *host_cert_b64 = NULL;
524 char *root_cert_b64 = NULL;
525 char *public_key_b64 = NULL;
526
527 ret = lockdownd_get_device_public_key(control, &public_key_b64);
528 if (ret != IPHONE_E_SUCCESS) { 531 if (ret != IPHONE_E_SUCCESS) {
529 log_debug_msg("Device refused to send public key.\n"); 532 log_debug_msg("Device refused to send public key.\n");
530 return ret; 533 return ret;
531 } 534 }
532 535
533 ret = lockdownd_gen_pair_cert(public_key_b64, &device_cert_b64, &host_cert_b64, &root_cert_b64); 536 ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert);
534 if (ret != IPHONE_E_SUCCESS) { 537 if (ret != IPHONE_E_SUCCESS) {
535 free(public_key_b64); 538 free(public_key.data);
536 return ret; 539 return ret;
537 } 540 }
538 541
539 /* Setup Pair request plist */ 542 /* Setup Pair request plist */
540 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 543 dict = plist_new_dict();
541 dictRecord = add_key_dict_node(plist, dict, "PairRecord", "\n", 1); 544 plist_add_sub_key_el(dict, "PairRecord");
542 //dictRecord = add_child_to_plist(plist, "dict", "\n", NULL, 1); 545 dict_record = plist_new_dict();
543 add_key_data_dict_element(plist, dictRecord, "DeviceCertificate", device_cert_b64, 2); 546 plist_add_sub_node(dict, dict_record);
544 add_key_data_dict_element(plist, dictRecord, "HostCertificate", host_cert_b64, 2); 547 plist_add_sub_key_el(dict_record, "DeviceCertificate");
545 add_key_str_dict_element(plist, dictRecord, "HostID", host_id, 2); 548 plist_add_sub_data_el(dict_record, device_cert.data, device_cert.size);
546 add_key_data_dict_element(plist, dictRecord, "RootCertificate", root_cert_b64, 2); 549 plist_add_sub_key_el(dict_record, "HostCertificate");
547 add_key_str_dict_element(plist, dict, "Request", "Pair", 1); 550 plist_add_sub_data_el(dict_record, host_cert.data, host_cert.size);
548 551 plist_add_sub_key_el(dict_record, "HostID");
549 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 552 plist_add_sub_string_el(dict_record, host_id);
550 553 plist_add_sub_key_el(dict_record, "RootCertificate");
551 printf("XML Pairing request : %s\n", XML_content); 554 plist_add_sub_data_el(dict_record, root_cert.data, root_cert.size);
555 plist_add_sub_key_el(dict_record, "Request");
556 plist_add_sub_string_el(dict_record, "Pair");
552 557
553 /* send to iPhone */ 558 /* send to iPhone */
554 ret = iphone_lckd_send(control, XML_content, length, &bytes); 559 ret = iphone_lckd_send(control, dict);
555 560 plist_free(dict);
556 xmlFree(XML_content); 561 dict = NULL;
557 xmlFreeDoc(plist);
558 plist = NULL;
559 562
560 if (ret != IPHONE_E_SUCCESS) 563 if (ret != IPHONE_E_SUCCESS)
561 return ret; 564 return ret;
562 565
563 /* Now get iPhone's answer */ 566 /* Now get iPhone's answer */
564 ret = iphone_lckd_recv(control, &XML_content, &bytes); 567 ret = iphone_lckd_recv(control, &dict);
565 568
566 if (ret != IPHONE_E_SUCCESS) 569 if (ret != IPHONE_E_SUCCESS)
567 return ret; 570 return ret;
568 571
569 log_debug_msg("lockdown_pair_device: iPhone's response to our pair request:\n"); 572 plist_t query_node = plist_find_node_by_string(dict, "Pair");
570 log_debug_msg(XML_content); 573 plist_t result_key_node = plist_get_next_sibling(query_node);
571 log_debug_msg("\n\n"); 574 plist_t result_value_node = plist_get_next_sibling(result_key_node);
572 575
573 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 576 plist_type result_key_type = plist_get_node_type(result_key_node);
574 if (!plist) { 577 plist_type result_value_type = plist_get_node_type(result_value_node);
575 free(public_key_b64); 578
576 return IPHONE_E_PLIST_ERROR; 579 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
577 } 580
578 dict = xmlDocGetRootElement(plist); 581 char *result_key = NULL;
579 for (dict = dict->children; dict; dict = dict->next) { 582 char *result_value = NULL;
580 if (!xmlStrcmp(dict->name, "dict"))
581 break;
582 }
583 if (!dict) {
584 free(public_key_b64);
585 return IPHONE_E_DICT_ERROR;
586 }
587 583
588 /* Parse xml to check success and to find public key */ 584 plist_get_key_val(result_key_node, &result_key);
589 dictionary = read_dict_element_strings(dict); 585 plist_get_string_val(result_value_node, &result_value);
590 xmlFreeDoc(plist);
591 free(XML_content);
592 586
593 int success = 0; 587 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
594 for (i = 0; dictionary[i]; i += 2) { 588 ret = IPHONE_E_SUCCESS;
595 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) {
596 success = 1;
597 } 589 }
598 }
599 590
600 if (dictionary) { 591 free(result_key);
601 free_dictionary(dictionary); 592 free(result_value);
602 dictionary = NULL;
603 } 593 }
594 plist_free(dict);
595 dict = NULL;
604 596
605 /* store public key in config if pairing succeeded */ 597 /* store public key in config if pairing succeeded */
606 if (success) { 598 if (ret == IPHONE_E_SUCCESS) {
607 log_debug_msg("lockdownd_pair_device: pair success\n"); 599 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair_device: pair success\n");
608 store_device_public_key(uid, public_key_b64); 600 store_device_public_key(uid, public_key);
609 ret = IPHONE_E_SUCCESS; 601 ret = IPHONE_E_SUCCESS;
610 } else { 602 } else {
611 log_debug_msg("lockdownd_pair_device: pair failure\n"); 603 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair_device: pair failure\n");
612 ret = IPHONE_E_PAIRING_FAILED; 604 ret = IPHONE_E_PAIRING_FAILED;
613 } 605 }
614 free(public_key_b64); 606 free(public_key.data);
615 return ret; 607 return ret;
616} 608}
617 609
@@ -624,55 +616,51 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
624void lockdownd_close(iphone_lckd_client_t control) 616void lockdownd_close(iphone_lckd_client_t control)
625{ 617{
626 if (!control) 618 if (!control)
627 return; // IPHONE_E_INVALID_ARG; 619 return; //IPHONE_E_INVALID_ARG;
628 xmlDocPtr plist = new_plist(); 620
629 xmlNode *dict, *key;
630 char **dictionary;
631 int bytes = 0, i = 0;
632 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 621 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
633 622
634 log_debug_msg("lockdownd_close() called\n"); 623 plist_t dict = plist_new_dict();
635 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 624 plist_add_sub_key_el(dict, "Request");
636 key = add_key_str_dict_element(plist, dict, "Request", "Goodbye", 1); 625 plist_add_sub_string_el(dict, "Goodbye");
637 char *XML_content;
638 uint32 length;
639 626
640 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 627 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close() called\n");
641 ret = iphone_lckd_send(control, XML_content, length, &bytes);
642 628
643 xmlFree(XML_content); 629 ret = iphone_lckd_send(control, dict);
644 xmlFreeDoc(plist); 630 plist_free(dict);
645 plist = NULL; 631 dict = NULL;
646 ret = iphone_lckd_recv(control, &XML_content, &bytes); 632
633 ret = iphone_lckd_recv(control, &dict);
647 634
648 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
649 if (!plist) {
650 log_debug_msg("lockdownd_close(): IPHONE_E_PLIST_ERROR\n");
651 return; //IPHONE_E_PLIST_ERROR;
652 }
653 dict = xmlDocGetRootElement(plist);
654 for (dict = dict->children; dict; dict = dict->next) {
655 if (!xmlStrcmp(dict->name, "dict"))
656 break;
657 }
658 if (!dict) { 635 if (!dict) {
659 log_debug_msg("lockdownd_close(): IPHONE_E_DICT_ERROR\n"); 636 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close(): IPHONE_E_PLIST_ERROR\n");
660 return; //IPHONE_E_DICT_ERROR; 637 return; // IPHONE_E_PLIST_ERROR;
661 } 638 }
662 dictionary = read_dict_element_strings(dict);
663 xmlFreeDoc(plist);
664 free(XML_content);
665 639
666 for (i = 0; dictionary[i]; i += 2) { 640 plist_t query_node = plist_find_node_by_string(dict, "Goodbye");
667 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 641 plist_t result_node = plist_get_next_sibling(query_node);
668 log_debug_msg("lockdownd_close(): success\n"); 642 plist_t value_node = plist_get_next_sibling(result_node);
643
644 plist_type result_type = plist_get_node_type(result_node);
645 plist_type value_type = plist_get_node_type(value_node);
646
647 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
648 char *result_value = NULL;
649 char *value_value = NULL;
650
651 plist_get_key_val(result_node, &result_value);
652 plist_get_string_val(value_node, &value_value);
653
654 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
655 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close(): success\n");
669 ret = IPHONE_E_SUCCESS; 656 ret = IPHONE_E_SUCCESS;
670 break;
671 } 657 }
658 free(result_value);
659 free(value_value);
672 } 660 }
673 661 plist_free(dict);
674 free_dictionary(dictionary); 662 dict = NULL;
675 return; //ret; 663 return; // ret;
676} 664}
677 665
678/** Generates the device certificate from the public key as well as the host 666/** Generates the device certificate from the public key as well as the host
@@ -680,25 +668,19 @@ void lockdownd_close(iphone_lckd_client_t control)
680 * 668 *
681 * @return IPHONE_E_SUCCESS on success. 669 * @return IPHONE_E_SUCCESS on success.
682 */ 670 */
683iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, 671iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert,
684 char **root_cert_b64) 672 gnutls_datum_t * ohost_cert, gnutls_datum_t * oroot_cert)
685{ 673{
686 if (!public_key_b64 || !device_cert_b64 || !host_cert_b64 || !root_cert_b64) 674 if (!public_key.data || !odevice_cert || !ohost_cert || !oroot_cert)
687 return IPHONE_E_INVALID_ARG; 675 return IPHONE_E_INVALID_ARG;
688 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 676 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
689 677
690 gnutls_datum_t modulus = { NULL, 0 }; 678 gnutls_datum_t modulus = { NULL, 0 };
691 gnutls_datum_t exponent = { NULL, 0 }; 679 gnutls_datum_t exponent = { NULL, 0 };
692 680
693 /* first decode base64 public_key */
694 gnutls_datum_t pem_pub_key;
695 gsize decoded_size;
696 pem_pub_key.data = g_base64_decode(public_key_b64, &decoded_size);
697 pem_pub_key.size = decoded_size;
698
699 /* now decode the PEM encoded key */ 681 /* now decode the PEM encoded key */
700 gnutls_datum_t der_pub_key; 682 gnutls_datum_t der_pub_key;
701 if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &pem_pub_key, &der_pub_key)) { 683 if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &public_key, &der_pub_key)) {
702 684
703 /* initalize asn.1 parser */ 685 /* initalize asn.1 parser */
704 ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY; 686 ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY;
@@ -782,10 +764,18 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
782 dev_pem.data = gnutls_malloc(dev_pem.size); 764 dev_pem.data = gnutls_malloc(dev_pem.size);
783 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); 765 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size);
784 766
785 /* now encode certificates for output */ 767 /* copy buffer for output */
786 *device_cert_b64 = g_base64_encode(dev_pem.data, dev_pem.size); 768 odevice_cert->data = malloc(dev_pem.size);
787 *host_cert_b64 = g_base64_encode(pem_host_cert.data, pem_host_cert.size); 769 memcpy(odevice_cert->data, dev_pem.data, dev_pem.size);
788 *root_cert_b64 = g_base64_encode(pem_root_cert.data, pem_root_cert.size); 770 odevice_cert->size = dev_pem.size;
771
772 ohost_cert->data = malloc(pem_host_cert.size);
773 memcpy(ohost_cert->data, pem_host_cert.data, pem_host_cert.size);
774 ohost_cert->size = pem_host_cert.size;
775
776 oroot_cert->data = malloc(pem_root_cert.size);
777 memcpy(oroot_cert->data, pem_root_cert.data, pem_root_cert.size);
778 oroot_cert->size = pem_root_cert.size;
789 } 779 }
790 gnutls_free(pem_root_priv.data); 780 gnutls_free(pem_root_priv.data);
791 gnutls_free(pem_root_cert.data); 781 gnutls_free(pem_root_cert.data);
@@ -797,7 +787,6 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
797 gnutls_free(exponent.data); 787 gnutls_free(exponent.data);
798 788
799 gnutls_free(der_pub_key.data); 789 gnutls_free(der_pub_key.data);
800 g_free(pem_pub_key.data);
801 790
802 return ret; 791 return ret;
803} 792}
@@ -811,129 +800,127 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
811 */ 800 */
812iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) 801iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID)
813{ 802{
814 xmlDocPtr plist = new_plist(); 803 plist_t dict = NULL;
815 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 804 uint32_t return_me = 0;
816 xmlNode *key;
817 char *what2send = NULL, **dictionary = NULL;
818 uint32 len = 0, bytes = 0, return_me = 0, i = 0;
819 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
820 // end variables
821 805
806 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
822 control->session_id[0] = '\0'; 807 control->session_id[0] = '\0';
823 808
824 key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); 809 /* Setup DevicePublicKey request plist */
825 if (!key) { 810 dict = plist_new_dict();
826 log_debug_msg("Couldn't add a key.\n"); 811 plist_add_sub_key_el(dict, "HostID");
827 xmlFreeDoc(plist); 812 plist_add_sub_string_el(dict, HostID);
828 return IPHONE_E_DICT_ERROR; 813 plist_add_sub_key_el(dict, "Request");
829 } 814 plist_add_sub_string_el(dict, "StartSession");
830 key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1);
831 if (!key) {
832 log_debug_msg("Couldn't add a key.\n");
833 xmlFreeDoc(plist);
834 return IPHONE_E_DICT_ERROR;
835 }
836
837 xmlDocDumpMemory(plist, (xmlChar **) & what2send, &len);
838 ret = iphone_lckd_send(control, what2send, len, &bytes);
839 815
840 xmlFree(what2send); 816 ret = iphone_lckd_send(control, dict);
841 xmlFreeDoc(plist); 817 plist_free(dict);
818 dict = NULL;
842 819
843 if (ret != IPHONE_E_SUCCESS) 820 if (ret != IPHONE_E_SUCCESS)
844 return ret; 821 return ret;
845 822
846 if (bytes > 0) { 823 ret = iphone_lckd_recv(control, &dict);
847 ret = iphone_lckd_recv(control, &what2send, &len); 824
848 plist = xmlReadMemory(what2send, len, NULL, NULL, 0); 825 if (!dict)
849 dict = xmlDocGetRootElement(plist); 826 return IPHONE_E_PLIST_ERROR;
850 if (!dict) 827
851 return IPHONE_E_DICT_ERROR; 828 plist_t query_node = plist_find_node_by_string(dict, "StartSession");
852 for (dict = dict->children; dict; dict = dict->next) { 829 plist_t result_key_node = plist_get_next_sibling(query_node);
853 if (!xmlStrcmp(dict->name, "dict")) 830 plist_t result_value_node = plist_get_next_sibling(result_key_node);
854 break; 831
855 } 832 plist_type result_key_type = plist_get_node_type(result_key_node);
856 dictionary = read_dict_element_strings(dict); 833 plist_type result_value_type = plist_get_node_type(result_value_node);
857 xmlFreeDoc(plist); 834
858 free(what2send); 835 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
836 char *result_key = NULL;
837 char *result_value = NULL;
838
839 plist_get_key_val(result_key_node, &result_key);
840 plist_get_string_val(result_value_node, &result_value);
841
859 ret = IPHONE_E_SSL_ERROR; 842 ret = IPHONE_E_SSL_ERROR;
860 for (i = 0; dictionary[i]; i += 2) { 843 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
861 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 844 // Set up GnuTLS...
862 // Set up GnuTLS... 845 //gnutls_anon_client_credentials_t anoncred;
863 //gnutls_anon_client_credentials_t anoncred; 846 gnutls_certificate_credentials_t xcred;
864 gnutls_certificate_credentials_t xcred; 847
865 848 log_dbg_msg(DBGMASK_LOCKDOWND, "We started the session OK, now trying GnuTLS\n");
866 log_debug_msg("We started the session OK, now trying GnuTLS\n"); 849 errno = 0;
867 errno = 0; 850 gnutls_global_init();
868 gnutls_global_init(); 851 //gnutls_anon_allocate_client_credentials(&anoncred);
869 //gnutls_anon_allocate_client_credentials(&anoncred); 852 gnutls_certificate_allocate_credentials(&xcred);
870 gnutls_certificate_allocate_credentials(&xcred); 853 gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM);
871 gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); 854 gnutls_init(control->ssl_session, GNUTLS_CLIENT);
872 gnutls_init(control->ssl_session, GNUTLS_CLIENT); 855 {
873 { 856 int protocol_priority[16] = { GNUTLS_SSL3, 0 };
874 int protocol_priority[16] = { GNUTLS_SSL3, 0 }; 857 int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 };
875 int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; 858 int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 };
876 int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; 859 int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 };
877 int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; 860 int comp_priority[16] = { GNUTLS_COMP_NULL, 0 };
878 int comp_priority[16] = { GNUTLS_COMP_NULL, 0 }; 861
879 862 gnutls_cipher_set_priority(*control->ssl_session, cipher_priority);
880 gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); 863 gnutls_compression_set_priority(*control->ssl_session, comp_priority);
881 gnutls_compression_set_priority(*control->ssl_session, comp_priority); 864 gnutls_kx_set_priority(*control->ssl_session, kx_priority);
882 gnutls_kx_set_priority(*control->ssl_session, kx_priority); 865 gnutls_protocol_set_priority(*control->ssl_session, protocol_priority);
883 gnutls_protocol_set_priority(*control->ssl_session, protocol_priority); 866 gnutls_mac_set_priority(*control->ssl_session, mac_priority);
884 gnutls_mac_set_priority(*control->ssl_session, mac_priority); 867
885 868 }
886 } 869 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me.
887 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. 870
888 871 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 1...\n");
889 log_debug_msg("GnuTLS step 1...\n"); 872 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control);
890 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); 873 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 2...\n");
891 log_debug_msg("GnuTLS step 2...\n"); 874 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite);
892 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); 875 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 3...\n");
893 log_debug_msg("GnuTLS step 3...\n"); 876 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead);
894 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); 877 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 4 -- now handshaking...\n");
895 log_debug_msg("GnuTLS step 4 -- now handshaking...\n"); 878
896 879 if (errno)
897 if (errno) 880 log_dbg_msg(DBGMASK_LOCKDOWND, "WARN: errno says %s before handshake!\n", strerror(errno));
898 log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno)); 881 return_me = gnutls_handshake(*control->ssl_session);
899 return_me = gnutls_handshake(*control->ssl_session); 882 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS handshake done...\n");
900 log_debug_msg("GnuTLS handshake done...\n"); 883
901 884 if (return_me != GNUTLS_E_SUCCESS) {
902 if (return_me != GNUTLS_E_SUCCESS) { 885 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS reported something wrong.\n");
903 log_debug_msg("GnuTLS reported something wrong.\n"); 886 gnutls_perror(return_me);
904 gnutls_perror(return_me); 887 log_dbg_msg(DBGMASK_LOCKDOWND, "oh.. errno says %s\n", strerror(errno));
905 log_debug_msg("oh.. errno says %s\n", strerror(errno)); 888 return IPHONE_E_SSL_ERROR;
906 return IPHONE_E_SSL_ERROR; 889 } else {
907 } else { 890 control->in_SSL = 1;
908 control->in_SSL = 1; 891 ret = IPHONE_E_SUCCESS;
909 ret = IPHONE_E_SUCCESS;
910 }
911 } else if (!strcmp(dictionary[i], "SessionID")) {
912 // we need to store the session ID for StopSession
913 strcpy(control->session_id, dictionary[i + 1]);
914 log_debug_msg("SessionID: %s\n", control->session_id);
915 free_dictionary(dictionary);
916 return ret;
917 } 892 }
918 } 893 }
919 if (ret == IPHONE_E_SUCCESS) { 894 }
920 log_debug_msg("Failed to get SessionID!\n"); 895 //store session id
921 return ret; 896 plist_t session_node = plist_find_node_by_key(dict, "SessionID");
922 } 897 if (session_node) {
898
899 plist_t session_node_val = plist_get_next_sibling(session_node);
900 plist_type session_node_val_type = plist_get_node_type(session_node_val);
923 901
924 log_debug_msg("Apparently failed negotiating with lockdownd.\n"); 902 if (session_node_val_type == PLIST_STRING) {
925 log_debug_msg("Responding dictionary: \n"); 903
926 for (i = 0; dictionary[i]; i += 2) { 904 char *session_id = NULL;
927 log_debug_msg("\t%s: %s\n", dictionary[i], dictionary[i + 1]); 905 plist_get_string_val(session_node_val, &session_id);
906
907 if (session_node_val_type == PLIST_STRING && session_id) {
908 // we need to store the session ID for StopSession
909 strcpy(control->session_id, session_id);
910 log_dbg_msg(DBGMASK_LOCKDOWND, "SessionID: %s\n", control->session_id);
911 }
912 free(session_id);
928 } 913 }
914 } else
915 log_dbg_msg(DBGMASK_LOCKDOWND, "Failed to get SessionID!\n");
916 plist_free(dict);
917 dict = NULL;
929 918
919 if (ret == IPHONE_E_SUCCESS)
920 return ret;
930 921
931 free_dictionary(dictionary); 922 log_dbg_msg(DBGMASK_LOCKDOWND, "Apparently failed negotiating with lockdownd.\n");
932 return IPHONE_E_SSL_ERROR; 923 return IPHONE_E_SSL_ERROR;
933 } else {
934 log_debug_msg("Didn't get enough bytes.\n");
935 return IPHONE_E_NOT_ENOUGH_DATA;
936 }
937} 924}
938 925
939/** gnutls callback for writing data to the iPhone. 926/** gnutls callback for writing data to the iPhone.
@@ -949,10 +936,10 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size
949 int bytes = 0; 936 int bytes = 0;
950 iphone_lckd_client_t control; 937 iphone_lckd_client_t control;
951 control = (iphone_lckd_client_t) transport; 938 control = (iphone_lckd_client_t) transport;
952 log_debug_msg("lockdownd_secuwrite() called\n"); 939 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_secuwrite() called\n");
953 log_debug_msg("pre-send\nlength = %zi\n", length); 940 log_dbg_msg(DBGMASK_LOCKDOWND, "pre-send\nlength = %zi\n", length);
954 iphone_mux_send(control->connection, buffer, length, &bytes); 941 iphone_mux_send(control->connection, buffer, length, &bytes);
955 log_debug_msg("post-send\nsent %i bytes\n", bytes); 942 log_dbg_msg(DBGMASK_LOCKDOWND, "post-send\nsent %i bytes\n", bytes);
956 943
957 dump_debug_buffer("sslpacketwrite.out", buffer, length); 944 dump_debug_buffer("sslpacketwrite.out", buffer, length);
958 return bytes; 945 return bytes;
@@ -972,7 +959,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
972 char *hackhackhack = NULL; 959 char *hackhackhack = NULL;
973 iphone_lckd_client_t control; 960 iphone_lckd_client_t control;
974 control = (iphone_lckd_client_t) transport; 961 control = (iphone_lckd_client_t) transport;
975 log_debug_msg("lockdownd_securead() called\nlength = %zi\n", length); 962 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_securead() called\nlength = %zi\n", length);
976 // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. 963 // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more.
977 if (control->gtls_buffer_hack_len > 0) { 964 if (control->gtls_buffer_hack_len > 0) {
978 if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got 965 if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got
@@ -981,7 +968,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
981 memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially 968 memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially
982 free(control->gtls_buffer_hack); // free our memory, it's not chained anymore 969 free(control->gtls_buffer_hack); // free our memory, it's not chained anymore
983 control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore 970 control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore
984 log_debug_msg("Did a partial fill to help quench thirst for data\n"); 971 log_dbg_msg(DBGMASK_LOCKDOWND, "Did a partial fill to help quench thirst for data\n");
985 } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less... 972 } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less...
986 control->gtls_buffer_hack_len -= length; // subtract what they're asking for 973 control->gtls_buffer_hack_len -= length; // subtract what they're asking for
987 memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer 974 memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer
@@ -990,33 +977,34 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
990 free(control->gtls_buffer_hack); // Free the old one 977 free(control->gtls_buffer_hack); // Free the old one
991 control->gtls_buffer_hack = hackhackhack; // And make it the new one. 978 control->gtls_buffer_hack = hackhackhack; // And make it the new one.
992 hackhackhack = NULL; 979 hackhackhack = NULL;
993 log_debug_msg("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len); 980 log_dbg_msg(DBGMASK_LOCKDOWND, "Quenched the thirst for data; new hack length is %i\n",
981 control->gtls_buffer_hack_len);
994 return length; // hand it over. 982 return length; // hand it over.
995 } else { // length == hack length 983 } else { // length == hack length
996 memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs 984 memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs
997 free(control->gtls_buffer_hack); // free our "obligation" 985 free(control->gtls_buffer_hack); // free our "obligation"
998 control->gtls_buffer_hack_len = 0; // free our "obligation" 986 control->gtls_buffer_hack_len = 0; // free our "obligation"
999 log_debug_msg("Satiated the thirst for data; now we have to eventually receive again.\n"); 987 log_dbg_msg(DBGMASK_LOCKDOWND, "Satiated the thirst for data; now we have to eventually receive again.\n");
1000 return length; // hand it over 988 return length; // hand it over
1001 } 989 }
1002 } 990 }
1003 // End buffering hack! 991 // End buffering hack!
1004 char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens 992 char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens
1005 993
1006 log_debug_msg("pre-read\nclient wants %zi bytes\n", length); 994 log_dbg_msg(DBGMASK_LOCKDOWND, "pre-read\nclient wants %zi bytes\n", length);
1007 iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes); 995 iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes);
1008 log_debug_msg("post-read\nwe got %i bytes\n", bytes); 996 log_dbg_msg(DBGMASK_LOCKDOWND, "post-read\nwe got %i bytes\n", bytes);
1009 if (bytes < 0) { 997 if (bytes < 0) {
1010 log_debug_msg("lockdownd_securead(): uh oh\n"); 998 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_securead(): uh oh\n");
1011 log_debug_msg 999 log_dbg_msg(DBGMASK_LOCKDOWND,
1012 ("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", 1000 "I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n",
1013 usb_strerror(), strerror(errno)); 1001 usb_strerror(), strerror(errno));
1014 return bytes + 28; // an errno 1002 return bytes + 28; // an errno
1015 } 1003 }
1016 if (bytes >= length) { 1004 if (bytes >= length) {
1017 if (bytes > length) { 1005 if (bytes > length) {
1018 log_debug_msg 1006 log_dbg_msg(DBGMASK_LOCKDOWND,
1019 ("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); 1007 "lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n");
1020 if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet 1008 if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet
1021 //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution! 1009 //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution!
1022 control->gtls_buffer_hack_len += bytes - length; 1010 control->gtls_buffer_hack_len += bytes - length;
@@ -1032,10 +1020,11 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
1032 memcpy(buffer + pos_start_fill, recv_buffer, length); 1020 memcpy(buffer + pos_start_fill, recv_buffer, length);
1033 free(recv_buffer); 1021 free(recv_buffer);
1034 if (bytes == length) { 1022 if (bytes == length) {
1035 log_debug_msg("Returning how much we received.\n"); 1023 log_dbg_msg(DBGMASK_LOCKDOWND, "Returning how much we received.\n");
1036 return bytes; 1024 return bytes;
1037 } else { 1025 } else {
1038 log_debug_msg("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len); 1026 log_dbg_msg(DBGMASK_LOCKDOWND, "Returning what they want to hear.\nHack length: %i\n",
1027 control->gtls_buffer_hack_len);
1039 return length; 1028 return length;
1040 } 1029 }
1041 } 1030 }
@@ -1060,86 +1049,72 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
1060 if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) 1049 if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id))
1061 return IPHONE_E_SSL_ERROR; 1050 return IPHONE_E_SSL_ERROR;
1062 1051
1063 char *XML_query, **dictionary; 1052 plist_t dict = NULL;
1064 uint32 length, i = 0, port_loc = 0, bytes = 0; 1053 uint32_t port_loc = 0;
1065 uint8 result = 0;
1066 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 1054 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
1067 1055
1068 free(host_id); 1056 free(host_id);
1069 host_id = NULL; 1057 host_id = NULL;
1070 1058
1071 xmlDocPtr plist = new_plist(); 1059 dict = plist_new_dict();
1072 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 1060 plist_add_sub_key_el(dict, "Request");
1073 xmlNode *key; 1061 plist_add_sub_string_el(dict, "StartService");
1074 key = add_key_str_dict_element(plist, dict, "Request", "StartService", 1); 1062 plist_add_sub_key_el(dict, "Service");
1075 if (!key) { 1063 plist_add_sub_string_el(dict, service);
1076 xmlFreeDoc(plist);
1077 return IPHONE_E_UNKNOWN_ERROR;
1078 }
1079 key = add_key_str_dict_element(plist, dict, "Service", service, 1);
1080 if (!key) {
1081 xmlFreeDoc(plist);
1082 return IPHONE_E_UNKNOWN_ERROR;
1083 }
1084 1064
1085 xmlDocDumpMemory(plist, (xmlChar **) & XML_query, &length); 1065 /* send to iPhone */
1066 ret = iphone_lckd_send(client, dict);
1067 plist_free(dict);
1068 dict = NULL;
1086 1069
1087 ret = iphone_lckd_send(client, XML_query, length, &bytes);
1088 free(XML_query);
1089 if (IPHONE_E_SUCCESS != ret) 1070 if (IPHONE_E_SUCCESS != ret)
1090 return ret; 1071 return ret;
1091 1072
1092 ret = iphone_lckd_recv(client, &XML_query, &bytes); 1073 ret = iphone_lckd_recv(client, &dict);
1093 xmlFreeDoc(plist); 1074
1094 if (IPHONE_E_SUCCESS != ret) 1075 if (IPHONE_E_SUCCESS != ret)
1095 return ret; 1076 return ret;
1096 1077
1097 if (bytes <= 0) 1078 if (!dict)
1098 return IPHONE_E_NOT_ENOUGH_DATA; 1079 return IPHONE_E_PLIST_ERROR;
1099 else {
1100 plist = xmlReadMemory(XML_query, bytes, NULL, NULL, 0);
1101 if (!plist)
1102 return IPHONE_E_UNKNOWN_ERROR;
1103 dict = xmlDocGetRootElement(plist);
1104 if (!dict)
1105 return IPHONE_E_UNKNOWN_ERROR;
1106 for (dict = dict->children; dict; dict = dict->next) {
1107 if (!xmlStrcmp(dict->name, "dict"))
1108 break;
1109 }
1110 1080
1111 if (!dict) 1081 plist_t query_node = plist_find_node_by_string(dict, "StartService");
1112 return IPHONE_E_UNKNOWN_ERROR; 1082 plist_t result_key_node = plist_get_next_sibling(query_node);
1113 dictionary = read_dict_element_strings(dict); 1083 plist_t result_value_node = plist_get_next_sibling(result_key_node);
1114 1084
1115 for (i = 0; dictionary[i]; i += 2) { 1085 plist_t port_key_node = plist_find_node_by_key(dict, "Port");
1116 log_debug_msg("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]); 1086 plist_t port_value_node = plist_get_next_sibling(port_key_node);
1117 1087
1118 if (!xmlStrcmp(dictionary[i], "Port")) { 1088 plist_type result_key_type = plist_get_node_type(result_key_node);
1119 port_loc = atoi(dictionary[i + 1]); 1089 plist_type result_value_type = plist_get_node_type(result_value_node);
1120 log_debug_msg("lockdownd_start_service() atoi'd port: %i\n", port); 1090 plist_type port_key_type = plist_get_node_type(port_key_node);
1121 } 1091 plist_type port_value_type = plist_get_node_type(port_value_node);
1122 1092
1123 if (!xmlStrcmp(dictionary[i], "Result")) { 1093 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && port_key_type == PLIST_KEY
1124 if (!xmlStrcmp(dictionary[i + 1], "Success")) { 1094 && port_value_type == PLIST_UINT) {
1125 result = 1; 1095
1126 } 1096 char *result_key = NULL;
1127 } 1097 char *result_value = NULL;
1128 } 1098 char *port_key = NULL;
1099 uint64_t port_value = 0;
1129 1100
1130 log_debug_msg("lockdownd_start_service(): DATA RECEIVED:\n\n"); 1101 plist_get_key_val(result_key_node, &result_key);
1131 log_debug_msg(XML_query); 1102 plist_get_string_val(result_value_node, &result_value);
1132 log_debug_msg("end data received by lockdownd_start_service()\n"); 1103 plist_get_key_val(port_key_node, &port_key);
1104 plist_get_uint_val(port_value_node, &port_value);
1133 1105
1134 free(XML_query); 1106 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) {
1135 xmlFreeDoc(plist); 1107 port_loc = port_value;
1136 free_dictionary(dictionary); 1108 ret = IPHONE_E_SUCCESS;
1137 if (port && result) { 1109 }
1110
1111 if (port && ret == IPHONE_E_SUCCESS)
1138 *port = port_loc; 1112 *port = port_loc;
1139 return IPHONE_E_SUCCESS; 1113 else
1140 } else 1114 ret = IPHONE_E_UNKNOWN_ERROR;
1141 return IPHONE_E_UNKNOWN_ERROR;
1142 } 1115 }
1143 1116
1144 return IPHONE_E_UNKNOWN_ERROR; 1117 plist_free(dict);
1118 dict = NULL;
1119 return ret;
1145} 1120}
diff --git a/src/lockdown.h b/src/lockdown.h
index 91bcc77..2f2a4b9 100644
--- a/src/lockdown.h
+++ b/src/lockdown.h
@@ -23,7 +23,6 @@
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>
@@ -41,17 +40,17 @@ struct iphone_lckd_client_int {
41 char session_id[40]; 40 char session_id[40];
42}; 41};
43 42
44char *lockdownd_generate_hostid(void);
45
46iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone); 43iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone);
47iphone_error_t lockdownd_hello(iphone_lckd_client_t control); 44iphone_error_t lockdownd_hello(iphone_lckd_client_t control);
48iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, const char *req_string,
49 char **value);
50iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key);
51 45
52iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, 46iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, char *req_string,
53 char **root_cert_b64); 47 gnutls_datum_t * value);
54iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *public_key, char *host_id); 48
49iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, gnutls_datum_t * public_key);
50
51iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * device_cert,
52 gnutls_datum_t * host_cert, gnutls_datum_t * root_cert);
53iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id);
55void lockdownd_close(iphone_lckd_client_t control); 54void lockdownd_close(iphone_lckd_client_t control);
56 55
57// SSL functions 56// 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..3e5eb06 100644
--- a/src/userpref.c
+++ b/src/userpref.c
@@ -111,10 +111,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 111 * @return 1 on success and 0 if no public key is given or if it has already
112 * been marked as connected previously. 112 * been marked as connected previously.
113 */ 113 */
114int store_device_public_key(char *uid, char *public_key) 114int store_device_public_key(char *uid, gnutls_datum_t public_key)
115{ 115{
116 116
117 if (NULL == public_key || is_device_known(uid)) 117 if (NULL == public_key.data || is_device_known(uid))
118 return 0; 118 return 0;
119 119
120 /* ensure config directory exists */ 120 /* ensure config directory exists */
@@ -124,15 +124,11 @@ int store_device_public_key(char *uid, char *public_key)
124 gchar *device_file = g_strconcat(uid, ".pem", NULL); 124 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); 125 gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
126 126
127 /* decode public key for storing */
128 gsize decoded_size;
129 gchar *data = g_base64_decode(public_key, &decoded_size);
130 /* store file */ 127 /* store file */
131 FILE *pFile = fopen(pem, "wb"); 128 FILE *pFile = fopen(pem, "wb");
132 fwrite(data, 1, decoded_size, pFile); 129 fwrite(public_key.data, 1, public_key.size, pFile);
133 fclose(pFile); 130 fclose(pFile);
134 g_free(pem); 131 g_free(pem);
135 g_free(data);
136 g_free(device_file); 132 g_free(device_file);
137 return 1; 133 return 1;
138} 134}
diff --git a/src/userpref.h b/src/userpref.h
index e7835d0..7e606eb 100644
--- a/src/userpref.h
+++ b/src/userpref.h
@@ -40,7 +40,7 @@ int is_device_known(char *uid);
40/** 40/**
41 * @return 1 if everything went well. Returns 0 otherwise. 41 * @return 1 if everything went well. Returns 0 otherwise.
42 */ 42 */
43int store_device_public_key(char *uid, char *public_key); 43int store_device_public_key(char *uid, gnutls_datum_t public_key);
44 44
45/** 45/**
46 * @return 1 if everything went well. Returns 0 otherwise. 46 * @return 1 if everything went well. Returns 0 otherwise.
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..e970e89
--- /dev/null
+++ b/swig/iphone.i
@@ -0,0 +1,156 @@
1 /* swig.i */
2 %module(package="libiphone") iPhone
3 %{
4 /* Includes the header in the wrapper code */
5 #include <libiphone/libiphone.h>
6 #include <plist/plist.h>
7#include "../src/utils.h"
8 typedef struct {
9 iphone_device_t dev;
10 } iPhone;
11
12 typedef struct {
13 iPhone* dev;
14 iphone_lckd_client_t client;
15 } Lockdownd;
16
17 typedef struct {
18 Lockdownd* lckd;
19 iphone_msync_client_t client;
20 } MobileSync;
21
22//now declare funtions to handle creation and deletion of objects
23void my_delete_iPhone(iPhone* dev);
24Lockdownd* my_new_Lockdownd(iPhone* phone);
25void my_delete_Lockdownd(Lockdownd* lckd);
26MobileSync* my_new_MobileSync(Lockdownd* lckd);
27
28 %}
29/* Parse the header file to generate wrappers */
30%include "stdint.i"
31%include "plist/swig/plist.i"
32
33typedef struct {
34 iphone_device_t dev;
35} iPhone;
36
37typedef struct {
38 iPhone* dev;
39 iphone_lckd_client_t client;
40} Lockdownd;
41
42typedef struct {
43 Lockdownd* lckd;
44 iphone_msync_client_t client;
45} MobileSync;
46
47%inline %{
48//now define funtions to handle creation and deletion of objects
49
50
51void my_delete_iPhone(iPhone* dev) {
52 if (dev) {
53 iphone_free_device ( dev->dev );
54 free(dev);
55 }
56}
57
58Lockdownd* my_new_Lockdownd(iPhone* phone) {
59 if (!phone) return NULL;
60 Lockdownd* client = (Lockdownd*) malloc(sizeof(Lockdownd));
61 client->dev = phone;
62 client->client = NULL;
63 if (IPHONE_E_SUCCESS == iphone_lckd_new_client ( phone->dev , &(client->client))) {
64 return client;
65 }
66 else {
67 free(client);
68 return NULL;
69 }
70}
71
72void my_delete_Lockdownd(Lockdownd* lckd) {
73 if (lckd) {
74 my_delete_iPhone(lckd->dev);
75 iphone_lckd_free_client ( lckd->client );
76 free(lckd);
77 }
78}
79
80MobileSync* my_new_MobileSync(Lockdownd* lckd) {
81 if (!lckd || !lckd->dev) return NULL;
82 MobileSync* client = NULL;
83 int port = 0;
84 if (IPHONE_E_SUCCESS == iphone_lckd_start_service ( lckd->client, "com.apple.mobilesync", &port )) {
85 client = (MobileSync*) malloc(sizeof(MobileSync));
86 client->lckd = lckd;
87 client->client = NULL;
88 iphone_msync_new_client ( lckd->dev->dev, 3432, port, &(client->client));
89 }
90 return client;
91}
92
93%}
94
95
96%extend iPhone { // Attach these functions to struct iPhone
97 iPhone() {
98 iPhone* phone = (iPhone*) malloc(sizeof(iPhone));
99 phone->dev = NULL;
100 iphone_set_debug_mask(DBGMASK_LOCKDOWND | DBGMASK_MOBILESYNC);
101 return phone;
102 }
103
104 ~iPhone() {
105 my_delete_iPhone($self);
106 }
107
108 int InitDevice() {
109 if (IPHONE_E_SUCCESS == iphone_get_device ( &($self->dev)))
110 return 1;
111 return 0;
112 }
113
114 Lockdownd* GetLockdownClient() {
115 return my_new_Lockdownd($self);
116 }
117};
118
119
120%extend Lockdownd { // Attach these functions to struct Lockdownd
121 Lockdownd(iPhone* phone) {
122 return my_new_Lockdownd(phone);
123 }
124
125 ~Lockdownd() {
126 my_delete_Lockdownd($self);
127 }
128
129 MobileSync* GetMobileSyncClient() {
130 return my_new_MobileSync($self);
131 }
132};
133
134%extend MobileSync { // Attach these functions to struct MobileSync
135 MobileSync(Lockdownd* lckd) {
136 return my_new_MobileSync(lckd);
137 }
138
139 ~MobileSync() {
140 my_delete_Lockdownd($self->lckd);
141 iphone_msync_free_client ( $self->client );
142 free($self);
143 }
144
145 void Send(PListNode* node) {
146 iphone_msync_send($self->client, node->node);
147 }
148
149 PListNode* Receive() {
150 PListNode* node = (PListNode*)malloc(sizeof(PListNode));
151 node->node = NULL;
152 iphone_msync_recv($self->client, &(node->node));
153 return node;
154 }
155};
156