diff options
-rw-r--r-- | configure.ac | 47 | ||||
-rw-r--r-- | include/libiphone/afc.h | 6 | ||||
-rw-r--r-- | include/libiphone/lockdown.h | 5 | ||||
-rw-r--r-- | m4/ac_pkg_swig.m4 | 8 | ||||
-rw-r--r-- | m4/ax_swig_enable_cxx.m4 | 4 | ||||
-rw-r--r-- | src/AFC.c | 266 | ||||
-rw-r--r-- | src/AFC.h | 12 | ||||
-rw-r--r-- | src/lockdown.c | 164 | ||||
-rw-r--r-- | src/lockdown.h | 3 | ||||
-rw-r--r-- | swig/Makefile.am | 3 |
10 files changed, 323 insertions, 195 deletions
diff --git a/configure.ac b/configure.ac index 7c59171..32a55f7 100644 --- a/configure.ac +++ b/configure.ac @@ -46,6 +46,24 @@ AC_FUNC_MALLOC AC_FUNC_REALLOC AC_CHECK_FUNCS([strcasecmp strdup strerror strndup]) +python_bindings=yes +AC_ARG_WITH([swig], + [AS_HELP_STRING([--without-swig], + [build Python bindings using swig (default is yes)])], + [build_swig=false], + [build_swig=true]) +if test "$build_swig" = "true" -a "$SWIG" != "false" ; then + SWIG_SUB=swig +else + SWIG_SUB= + python_bindings=no +fi + +AM_CONDITIONAL([HAVE_SWIG],[test "x$SWIG_SUB" = "xswig"]) + +AC_SUBST([DEV_SUB]) + + AC_ARG_ENABLE([dev-tools], [AS_HELP_STRING([--enable-dev-tools], [build development helper tools (default is no)])], @@ -57,8 +75,10 @@ if test "$build_dev_tools" = true; then [], [AC_MSG_ERROR([Please install readline development headers])] ) + building_dev_tools=yes else DEV_SUB= + building_dev_tools=no fi AM_CONDITIONAL([ENABLE_DEVTOOLS],[test "x$DEV_SUB" = "xdev"]) @@ -71,7 +91,10 @@ AC_ARG_ENABLE([debug-code], [no_debug_code=false], [no_debug_code=true]) if test "$no_debug_code" = true; then + building_debug_code=no AC_DEFINE(STRIP_DEBUG_CODE,1,[Strip debug reporting code]) +else + building_debug_code=yes fi AS_COMPILER_FLAGS(GLOBAL_CFLAGS, "-Wall -Wextra -Wmissing-declarations -Wredundant-decls -Wshadow -Wpointer-arith -Wwrite-strings -Wswitch-default -Wno-unused-parameter") @@ -106,4 +129,26 @@ AC_SUBST(LFS_CFLAGS) m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])]) -AC_OUTPUT(Makefile src/Makefile include/Makefile fdi/Makefile dev/Makefile tools/Makefile swig/Makefile libiphone-1.0.pc) +AC_OUTPUT([ +Makefile +src/Makefile +include/Makefile +fdi/Makefile +dev/Makefile +tools/Makefile +swig/Makefile +libiphone-1.0.pc +]) + +echo " +Configuration for $PACKAGE $VERSION: +------------------------------------------- + + Install prefix: .........: $prefix + Debug code ..............: $building_debug_code + Dev tools ...............: $building_dev_tools + Python bindings .........: $python_bindings + + Now type 'make' to build $PACKAGE $VERSION, + and then 'make install' for installation. +" diff --git a/include/libiphone/afc.h b/include/libiphone/afc.h index 1c714c9..7eff678 100644 --- a/include/libiphone/afc.h +++ b/include/libiphone/afc.h @@ -95,15 +95,15 @@ afc_error_t afc_get_file_info(afc_client_t client, const char *filename, char ** afc_error_t afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle); afc_error_t afc_file_close(afc_client_t client, uint64_t handle); afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation); -afc_error_t afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint32_t *bytes); -afc_error_t afc_file_write(afc_client_t client, uint64_t handle, const char *data, int length, uint32_t *bytes); +afc_error_t afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read); +afc_error_t afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t length, uint32_t *bytes_written); afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence); afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position); afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize); afc_error_t afc_remove_path(afc_client_t client, const char *path); afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *to); afc_error_t afc_make_directory(afc_client_t client, const char *dir); -afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize); +afc_error_t afc_truncate(afc_client_t client, const char *path, uint64_t newsize); afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname); afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mtime); diff --git a/include/libiphone/lockdown.h b/include/libiphone/lockdown.h index af58190..31ffeab 100644 --- a/include/libiphone/lockdown.h +++ b/include/libiphone/lockdown.h @@ -60,10 +60,11 @@ lockdownd_error_t lockdownd_get_value(lockdownd_client_t client, const char *dom lockdownd_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain, const char *key, plist_t value); lockdownd_error_t lockdownd_remove_value(lockdownd_client_t client, const char *domain, const char *key); lockdownd_error_t lockdownd_start_service(lockdownd_client_t client, const char *service, int *port); -lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char *session_id); +lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client); lockdownd_error_t lockdownd_send(lockdownd_client_t client, plist_t plist); lockdownd_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist); -lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *host_id); +lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id); +lockdownd_error_t lockdownd_validate_pair(lockdownd_client_t client, char *host_id); lockdownd_error_t lockdownd_get_device_uuid(lockdownd_client_t control, char **uuid); lockdownd_error_t lockdownd_get_device_name(lockdownd_client_t client, char **device_name); lockdownd_error_t lockdownd_enter_recovery(lockdownd_client_t client); diff --git a/m4/ac_pkg_swig.m4 b/m4/ac_pkg_swig.m4 index 738f69d..97244bc 100644 --- a/m4/ac_pkg_swig.m4 +++ b/m4/ac_pkg_swig.m4 @@ -64,8 +64,8 @@ AC_DEFUN([AC_PROG_SWIG],[ AC_PATH_PROG([SWIG],[swig]) if test -z "$SWIG" ; then - AC_MSG_WARN([cannot find 'swig' program. You should look at http://www.swig.org]) - SWIG='echo "Error: SWIG is not installed. You should look at http://www.swig.org" ; false' + AC_MSG_WARN([cannot find 'swig' program. You should look at http://www.swig.org] or install your distribution specific swig package.) + SWIG=false elif test -n "$1" ; then AC_MSG_CHECKING([for SWIG version]) [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'`] @@ -107,7 +107,7 @@ AC_DEFUN([AC_PROG_SWIG],[ -o $available_minor -ne $required_minor \ -o $available_patch -lt $required_patch ; then AC_MSG_WARN([SWIG version >= $1 is required. You have $swig_version. You should look at http://www.swig.org]) - SWIG='echo "Error: SWIG version >= $1 is required. You have '"$swig_version"'. You should look at http://www.swig.org" ; false' + SWIG=false else AC_MSG_NOTICE([SWIG executable is '$SWIG']) SWIG_LIB=`$SWIG -swiglib` @@ -115,7 +115,7 @@ AC_DEFUN([AC_PROG_SWIG],[ fi else AC_MSG_WARN([cannot determine SWIG version]) - SWIG='echo "Error: Cannot determine SWIG version. You should look at http://www.swig.org" ; false' + SWIG=false fi fi AC_SUBST([SWIG_LIB]) diff --git a/m4/ax_swig_enable_cxx.m4 b/m4/ax_swig_enable_cxx.m4 index 722caeb..c1eca8c 100644 --- a/m4/ax_swig_enable_cxx.m4 +++ b/m4/ax_swig_enable_cxx.m4 @@ -47,5 +47,7 @@ AU_ALIAS([SWIG_ENABLE_CXX], [AX_SWIG_ENABLE_CXX]) AC_DEFUN([AX_SWIG_ENABLE_CXX],[ AC_REQUIRE([AC_PROG_SWIG]) AC_REQUIRE([AC_PROG_CXX]) - SWIG="$SWIG -c++" + if test "$SWIG" != "false"; then + SWIG="$SWIG -c++" + fi ]) @@ -119,21 +119,24 @@ afc_error_t afc_client_free(afc_client_t client) * @param client The client to send data through. * @param data The data to send. * @param length The length to send. - * - * @return The number of bytes actually sent, or -1 on error. + * @param bytes_sent The number of bytes actually sent. + * + * @return AFC_E_SUCCESS on success, or an AFC_E_* error value on error. * * @warning set client->afc_packet->this_length and * client->afc_packet->entire_length to 0 before calling this. The * reason is that if you set them to different values, it indicates * you want to send the data as two packets. */ -static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t length) +static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, uint32_t length, uint32_t *bytes_sent) { - int bytes = 0, offset = 0; + uint32_t offset = 0; uint32_t sent = 0; if (!client || !client->connection || !client->afc_packet) - return 0; + return AFC_E_INVALID_ARGUMENT; + + *bytes_sent = 0; if (!data || !length) length = 0; @@ -158,20 +161,26 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l log_debug_msg("to based on the packet.\n"); log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset); log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length); - return -1; + return AFC_E_INTERNAL_ERROR; } + /* send AFC packet header */ + AFCPacket_to_LE(client->afc_packet); + sent = 0; iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); if (sent == 0) { - return bytes; + /* FIXME: should this be handled as success?! */ + return AFC_E_SUCCESS; } - bytes += sent; + *bytes_sent += sent; + /* send AFC packet data */ + sent = 0; iphone_device_send(client->connection, data, offset, &sent); if (sent == 0) { - return bytes; + return AFC_E_SUCCESS; } - bytes += sent; + *bytes_sent += sent; log_debug_msg("%s: sent the first now go with the second\n", __func__); log_debug_msg("%s: Length: %i\n", __func__, length - offset); @@ -181,8 +190,8 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l sent = 0; iphone_device_send(client->connection, data + offset, length - offset, &sent); - bytes = sent; - return bytes; + *bytes_sent = sent; + return AFC_E_SUCCESS; } else { log_debug_msg("%s: doin things the old way\n", __func__); log_debug_msg("%s: packet length = %i\n", __func__, client->afc_packet->this_length); @@ -190,35 +199,38 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket)); log_debug_msg("\n"); + /* send AFC packet header */ + AFCPacket_to_LE(client->afc_packet); + sent = 0; iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); if (sent == 0) { - return bytes; + return AFC_E_SUCCESS; } - bytes += sent; + *bytes_sent += sent; + /* send AFC packet data (if there's data to send) */ if (length > 0) { log_debug_msg("%s: packet data follows\n", __func__); log_debug_buffer(data, length); log_debug_msg("\n"); iphone_device_send(client->connection, data, length, &sent); - bytes += sent; + *bytes_sent += sent; } - return bytes; + return AFC_E_SUCCESS; } - return -1; + return AFC_E_INTERNAL_ERROR; } /** Receives data through an AFC client and sets a variable to the received data. * * @param client The client to receive data on. * @param dump_here The char* to point to the newly-received data. + * @param bytes_recv How much data was received. * - * @return How much data was received, 0 on successful receive with no errors, - * -1 if there was an error involved with receiving or if the packet - * received raised a non-trivial error condition (i.e. non-zero with - * AFC_ERROR operation) + * @return AFC_E_SUCCESS when data has been received, or an AFC_E_* error value + * when an error occured. */ -static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *bytes) +static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint32_t *bytes_recv) { AFCPacket header; uint32_t entire_len = 0; @@ -226,15 +238,16 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * uint32_t current_count = 0; uint64_t param1 = -1; - *bytes = 0; + *bytes_recv = 0; /* first, read the AFC header */ - iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); - if (*bytes <= 0) { + iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), bytes_recv); + AFCPacket_from_LE(&header); + if (*bytes_recv == 0) { log_debug_msg("%s: Just didn't get enough.\n", __func__); *dump_here = NULL; return AFC_E_MUX_ERROR; - } else if ((uint32_t)*bytes < sizeof(AFCPacket)) { + } else if (*bytes_recv < sizeof(AFCPacket)) { log_debug_msg("%s: Did not even get the AFCPacket header\n", __func__); *dump_here = NULL; return AFC_E_MUX_ERROR; @@ -262,7 +275,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * && header.entire_length == sizeof(AFCPacket)) { log_debug_msg("%s: Empty AFCPacket received!\n", __func__); *dump_here = NULL; - *bytes = 0; + *bytes_recv = 0; if (header.operation == AFC_OP_DATA) { return AFC_E_SUCCESS; } else { @@ -282,13 +295,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * *dump_here = (char*)malloc(entire_len); if (this_len > 0) { - iphone_device_recv(client->connection, *dump_here, this_len, (uint32_t*)bytes); - if (*bytes <= 0) { + iphone_device_recv(client->connection, *dump_here, this_len, bytes_recv); + if (*bytes_recv <= 0) { free(*dump_here); *dump_here = NULL; log_debug_msg("%s: Did not get packet contents!\n", __func__); return AFC_E_NOT_ENOUGH_DATA; - } else if ((uint32_t)*bytes < this_len) { + } else if (*bytes_recv < this_len) { free(*dump_here); *dump_here = NULL; log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); @@ -300,12 +313,12 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * if (entire_len > this_len) { while (current_count < entire_len) { - iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)bytes); - if (*bytes <= 0) { - log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes); + iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, bytes_recv); + if (*bytes_recv <= 0) { + log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes_recv); break; } - current_count += *bytes; + current_count += *bytes_recv; } if (current_count < entire_len) { log_debug_msg("%s: WARNING: could not receive full packet (read %s, size %d)\n", __func__, current_count, entire_len); @@ -345,7 +358,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * /* unknown operation code received */ free(*dump_here); *dump_here = NULL; - *bytes = 0; + *bytes_recv = 0; log_debug_msg("%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, header.operation, param1); fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, (long long)header.operation, (long long)param1); @@ -353,13 +366,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * return AFC_E_OP_NOT_SUPPORTED; } - *bytes = current_count; + *bytes_recv = current_count; return AFC_E_SUCCESS; } -static int count_nullspaces(char *string, int number) +static uint32_t count_nullspaces(char *string, uint32_t number) { - int i = 0, nulls = 0; + uint32_t i = 0, nulls = 0; for (i = 0; i < number; i++) { if (string[i] == '\0') @@ -369,9 +382,9 @@ static int count_nullspaces(char *string, int number) return nulls; } -static char **make_strings_list(char *tokens, int true_length) +static char **make_strings_list(char *tokens, uint32_t true_length) { - int nulls = 0, i = 0, j = 0; + uint32_t nulls = 0, i = 0, j = 0; char **list = NULL; if (!tokens || !true_length) @@ -398,7 +411,7 @@ static char **make_strings_list(char *tokens, int true_length) */ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list) { - int bytes = 0; + uint32_t bytes = 0; char *data = NULL, **list_loc = NULL; afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -411,8 +424,8 @@ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***lis client->afc_packet->operation = AFC_OP_READ_DIR; client->afc_packet->entire_length = 0; client->afc_packet->this_length = 0; - bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); - if (bytes <= 0) { + ret = afc_dispatch_packet(client, dir, strlen(dir)+1, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -442,7 +455,7 @@ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***lis */ afc_error_t afc_get_device_info(afc_client_t client, char ***infos) { - int bytes = 0; + uint32_t bytes = 0; char *data = NULL, **list = NULL; afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -454,8 +467,8 @@ afc_error_t afc_get_device_info(afc_client_t client, char ***infos) // Send the command client->afc_packet->operation = AFC_OP_GET_DEVINFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = afc_dispatch_packet(client, NULL, 0); - if (bytes < 0) { + ret = afc_dispatch_packet(client, NULL, 0, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -523,7 +536,7 @@ afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char * afc_error_t afc_remove_path(afc_client_t client, const char *path) { char *response = NULL; - int bytes; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || !client->connection) @@ -534,8 +547,8 @@ afc_error_t afc_remove_path(afc_client_t client, const char *path) // Send command client->afc_packet->this_length = client->afc_packet->entire_length = 0; client->afc_packet->operation = AFC_OP_REMOVE_PATH; - bytes = afc_dispatch_packet(client, path, strlen(path)+1); - if (bytes <= 0) { + ret = afc_dispatch_packet(client, path, strlen(path)+1, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -566,7 +579,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t))); - int bytes = 0; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !from || !to || !client->afc_packet || !client->connection) @@ -579,9 +592,9 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t memcpy(send + strlen(from) + 1, to, strlen(to) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; client->afc_packet->operation = AFC_OP_RENAME_PATH; - bytes = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1); + ret = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1, &bytes); free(send); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -606,7 +619,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t */ afc_error_t afc_make_directory(afc_client_t client, const char *dir) { - int bytes = 0; + uint32_t bytes = 0; char *response = NULL; afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -618,8 +631,8 @@ afc_error_t afc_make_directory(afc_client_t client, const char *dir) // Send command client->afc_packet->operation = AFC_OP_MAKE_DIR; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); - if (bytes <= 0) { + ret = afc_dispatch_packet(client, dir, strlen(dir)+1, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -647,7 +660,7 @@ afc_error_t afc_make_directory(afc_client_t client, const char *dir) afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) { char *received = NULL; - int bytes; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !infolist) @@ -658,7 +671,11 @@ afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***inf // Send command client->afc_packet->operation = AFC_OP_GET_FILE_INFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - afc_dispatch_packet(client, path, strlen(path)+1); + ret = afc_dispatch_packet(client, path, strlen(path)+1, &bytes); + if (ret != AFC_E_SUCCESS) { + afc_unlock(client); + return AFC_E_NOT_ENOUGH_DATA; + } // Receive data ret = afc_receive_data(client, &received, &bytes); @@ -688,8 +705,8 @@ iphone_error_t afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle) { - uint32_t ag = 0; - int bytes = 0; + uint64_t file_mode_loc = GUINT64_TO_LE(file_mode); + uint32_t bytes = 0; char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -702,16 +719,15 @@ afc_file_open(afc_client_t client, const char *filename, afc_lock(client); // Send command - memcpy(data, &file_mode, 4); - memcpy(data + 4, &ag, 4); + memcpy(data, &file_mode_loc, 8); memcpy(data + 8, filename, strlen(filename)); data[8 + strlen(filename)] = '\0'; client->afc_packet->operation = AFC_OP_FILE_OPEN; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1); + ret = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1, &bytes); free(data); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { log_debug_msg("%s: Didn't receive a response to the command\n", __func__); afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; @@ -740,18 +756,19 @@ afc_file_open(afc_client_t client, const char *filename, * @param handle File handle of a previously opened file * @param data The pointer to the memory region to store the read data * @param length The number of bytes to read + * @param bytes_read The number of bytes actually read. * - * @return The number of bytes read if successful. If there was an error -1. + * @return AFC_E_SUCCESS on success or an AFC_E_* error value on error. */ iphone_error_t -afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint32_t * bytes) +afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read) { char *input = NULL; - int current_count = 0, bytes_loc = 0; - const int MAXIMUM_READ_SIZE = 1 << 16; + uint32_t current_count = 0, bytes_loc = 0; + const uint32_t MAXIMUM_READ_SIZE = 1 << 16; afc_error_t ret = AFC_E_SUCCESS; - if (!client || !client->afc_packet || !client->connection || handle == 0 || (length < 0)) + if (!client || !client->afc_packet || !client->connection || handle == 0) return AFC_E_INVALID_ARGUMENT; log_debug_msg("%s: called for length %i\n", __func__, length); @@ -765,13 +782,13 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint // Send the read command AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); packet->filehandle = handle; - packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; + packet->size = GUINT64_TO_LE(((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE); client->afc_packet->operation = AFC_OP_READ; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes_loc = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket)); + ret = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket), &bytes_loc); free(packet); - if (bytes_loc <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -786,7 +803,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint if (input) free(input); afc_unlock(client); - *bytes = current_count; + *bytes_read = current_count; /* FIXME: check that's actually a success */ return ret; } else { @@ -802,7 +819,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint log_debug_msg("%s: returning current_count as %i\n", __func__, current_count); afc_unlock(client); - *bytes = current_count; + *bytes_read = current_count; return ret; } @@ -812,23 +829,22 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint * @param handle File handle of previously opened file. * @param data The data to write to the file. * @param length How much data to write. + * @param bytes_written The number of bytes actually written to the file. * - * @return The number of bytes written to the file, or a value less than 0 if - * none were written... + * @return AFC_E_SUCCESS on success, or an AFC_E_* error value on error. */ iphone_error_t -afc_file_write(afc_client_t client, uint64_t handle, - const char *data, int length, uint32_t * bytes) +afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t length, uint32_t *bytes_written) { char *acknowledgement = NULL; - const int MAXIMUM_WRITE_SIZE = 1 << 15; - uint32_t zero = 0, current_count = 0, i = 0; + const uint32_t MAXIMUM_WRITE_SIZE = 1 << 15; + uint32_t current_count = 0, i = 0; uint32_t segments = (length / MAXIMUM_WRITE_SIZE); - int bytes_loc = 0; + uint32_t bytes_loc = 0; char *out_buffer = NULL; afc_error_t ret = AFC_E_SUCCESS; - if (!client || !client->afc_packet || !client->connection || !bytes || (handle == 0) || (length < 0)) + if (!client || !client->afc_packet || !client->connection || !bytes_written || (handle == 0)) return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -844,8 +860,8 @@ afc_file_write(afc_client_t client, uint64_t handle, out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); memcpy(out_buffer, (char *)&handle, sizeof(uint64_t)); memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); - bytes_loc = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); - if (bytes_loc < 0) { + ret = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8, &bytes_loc); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -866,9 +882,9 @@ afc_file_write(afc_client_t client, uint64_t handle, // didn't get sent in the for loop // this length is fine because it's always sizeof(AFCPacket) + 8, but // to be sure we do it again - if (current_count == (uint32_t)length) { + if (current_count == length) { afc_unlock(client); - *bytes = current_count; + *bytes_written = current_count; return ret; } @@ -878,19 +894,18 @@ afc_file_write(afc_client_t client, uint64_t handle, out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); memcpy(out_buffer, (char *) &handle, sizeof(uint64_t)); memcpy(out_buffer + 8, data + current_count, (length - current_count)); - bytes_loc = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8); + ret = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8, &bytes_loc); free(out_buffer); out_buffer = NULL; current_count += bytes_loc; - if (bytes_loc <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); - *bytes = current_count; + *bytes_written = current_count; return AFC_E_SUCCESS; } - zero = bytes_loc; ret = afc_receive_data(client, &acknowledgement, &bytes_loc); afc_unlock(client); if (ret != AFC_E_SUCCESS) { @@ -898,7 +913,7 @@ afc_file_write(afc_client_t client, uint64_t handle, } else { free(acknowledgement); } - *bytes = current_count; + *bytes_written = current_count; return ret; } @@ -910,7 +925,7 @@ afc_file_write(afc_client_t client, uint64_t handle, afc_error_t afc_file_close(afc_client_t client, uint64_t handle) { char *buffer = malloc(sizeof(char) * 8); - int bytes = 0; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -924,11 +939,11 @@ afc_error_t afc_file_close(afc_client_t client, uint64_t handle) memcpy(buffer, &handle, sizeof(uint64_t)); client->afc_packet->operation = AFC_OP_FILE_CLOSE; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = afc_dispatch_packet(client, buffer, 8); + ret = afc_dispatch_packet(client, buffer, 8, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_UNKNOWN_ERROR; } @@ -957,8 +972,8 @@ afc_error_t afc_file_close(afc_client_t client, uint64_t handle) afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) { char *buffer = malloc(16); - int bytes = 0; - uint64_t op = operation; + uint32_t bytes = 0; + uint64_t op = GUINT64_TO_LE(operation); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -974,11 +989,11 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op client->afc_packet->operation = AFC_OP_FILE_LOCK; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = afc_dispatch_packet(client, buffer, 16); + ret = afc_dispatch_packet(client, buffer, 16, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); log_debug_msg("%s: could not send lock command\n", __func__); return AFC_E_UNKNOWN_ERROR; @@ -1006,8 +1021,9 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) { char *buffer = (char *) malloc(sizeof(char) * 24); - uint32_t zero = 0; - int bytes = 0; + int64_t offset_loc = (int64_t)GUINT64_TO_LE(offset); + uint64_t whence_loc = GUINT64_TO_LE(whence); + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1017,16 +1033,15 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, // Send the command memcpy(buffer, &handle, sizeof(uint64_t)); // handle - memcpy(buffer + 8, &whence, sizeof(int32_t)); // fromwhere - memcpy(buffer + 12, &zero, sizeof(uint32_t)); // pad - memcpy(buffer + 16, &offset, sizeof(uint64_t)); // offset + memcpy(buffer + 8, &whence_loc, sizeof(uint64_t)); // fromwhere + memcpy(buffer + 16, &offset_loc, sizeof(uint64_t)); // offset client->afc_packet->operation = AFC_OP_FILE_SEEK; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = afc_dispatch_packet(client, buffer, 24); + ret = afc_dispatch_packet(client, buffer, 24, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1051,7 +1066,7 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) { char *buffer = (char *) malloc(sizeof(char) * 8); - int bytes = 0; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1063,11 +1078,11 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi memcpy(buffer, &handle, sizeof(uint64_t)); // handle client->afc_packet->operation = AFC_OP_FILE_TELL; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = afc_dispatch_packet(client, buffer, 8); + ret = afc_dispatch_packet(client, buffer, 8, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1077,6 +1092,7 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi if (bytes > 0 && buffer) { /* Get the position */ memcpy(position, buffer, sizeof(uint64_t)); + *position = GUINT64_FROM_LE(*position); } if (buffer) free(buffer); @@ -1100,7 +1116,8 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) { char *buffer = (char *) malloc(sizeof(char) * 16); - int bytes = 0; + uint32_t bytes = 0; + uint64_t newsize_loc = GUINT64_TO_LE(newsize); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1110,14 +1127,14 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new // Send command memcpy(buffer, &handle, sizeof(uint64_t)); // handle - memcpy(buffer + 8, &newsize, sizeof(uint64_t)); // newsize + memcpy(buffer + 8, &newsize_loc, sizeof(uint64_t)); // newsize client->afc_packet->operation = AFC_OP_FILE_SET_SIZE; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = afc_dispatch_packet(client, buffer, 16); + ret = afc_dispatch_packet(client, buffer, 16, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1140,12 +1157,12 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) +afc_error_t afc_truncate(afc_client_t client, const char *path, uint64_t newsize) { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); - int bytes = 0; - uint64_t size_requested = newsize; + uint32_t bytes = 0; + uint64_t size_requested = GUINT64_TO_LE(newsize); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || !client->connection) @@ -1158,9 +1175,9 @@ afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) memcpy(send + 8, path, strlen(path) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; client->afc_packet->operation = AFC_OP_TRUNCATE; - bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); + ret = afc_dispatch_packet(client, send, 8 + strlen(path) + 1, &bytes); free(send); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1188,8 +1205,8 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); - int bytes = 0; - uint64_t type = linktype; + uint32_t bytes = 0; + uint64_t type = GUINT64_TO_LE(linktype); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !target || !linkname || !client->afc_packet || !client->connection) @@ -1207,9 +1224,9 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c memcpy(send + 8 + strlen(target) + 1, linkname, strlen(linkname) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; client->afc_packet->operation = AFC_OP_MAKE_LINK; - bytes = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1); + ret = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1, &bytes); free(send); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1236,7 +1253,8 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); - int bytes = 0; + uint32_t bytes = 0; + uint64_t mtime_loc = GUINT64_TO_LE(mtime); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || !client->connection) @@ -1245,13 +1263,13 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt afc_lock(client); // Send command - memcpy(send, &mtime, 8); + memcpy(send, &mtime_loc, 8); memcpy(send + 8, path, strlen(path) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; client->afc_packet->operation = AFC_OP_SET_FILE_TIME; - bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); + ret = afc_dispatch_packet(client, send, 8 + strlen(path) + 1, &bytes); free(send); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -36,6 +36,18 @@ typedef struct { uint64_t entire_length, this_length, packet_num, operation; } AFCPacket; +#define AFCPacket_to_LE(x) \ + (x)->entire_length = GUINT64_TO_LE((x)->entire_length); \ + (x)->this_length = GUINT64_TO_LE((x)->this_length); \ + (x)->packet_num = GUINT64_TO_LE((x)->packet_num); \ + (x)->operation = GUINT64_TO_LE((x)->operation); + +#define AFCPacket_from_LE(x) \ + (x)->entire_length = GUINT64_FROM_LE((x)->entire_length); \ + (x)->this_length = GUINT64_FROM_LE((x)->this_length); \ + (x)->packet_num = GUINT64_FROM_LE((x)->packet_num); \ + (x)->operation = GUINT64_FROM_LE((x)->operation); + typedef struct { uint64_t filehandle, size; } AFCFilePacket; diff --git a/src/lockdown.c b/src/lockdown.c index b182706..fb5f8f5 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -115,18 +115,23 @@ static int lockdown_check_result(plist_t dict, const char *query_match) * * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char *session_id) +lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client) { if (!client) return LOCKDOWN_E_INVALID_ARG; + if (!client->session_id) { + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: no session_id given, cannot stop session\n", __func__); + return LOCKDOWN_E_INVALID_ARG; + } + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t dict = plist_new_dict(); plist_dict_insert_item(dict,"Request", plist_new_string("StopSession")); - plist_dict_insert_item(dict,"SessionID", plist_new_string(session_id)); + plist_dict_insert_item(dict,"SessionID", plist_new_string(client->session_id)); - log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: stopping session %s\n", __func__, client->session_id); ret = lockdownd_send(client, dict); @@ -148,6 +153,9 @@ lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char * plist_free(dict); dict = NULL; + free(client->session_id); + client->session_id = NULL; + return ret; } @@ -170,7 +178,7 @@ static lockdownd_error_t lockdownd_stop_ssl_session(lockdownd_client_t client) if (client->in_SSL) { log_dbg_msg(DBGMASK_LOCKDOWND, "%s: stopping SSL session\n", __func__); - ret = lockdownd_stop_session(client, client->session_id); + ret = lockdownd_stop_session(client); log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending SSL close notify\n", __func__); gnutls_bye(client->ssl_session, GNUTLS_SHUT_RDWR); } @@ -209,6 +217,13 @@ lockdownd_error_t lockdownd_client_free(lockdownd_client_t client) } } + if (client->session_id) { + free(client->session_id); + } + if (client->uuid) { + free(client->uuid); + } + free(client); return ret; } @@ -642,31 +657,27 @@ lockdownd_error_t lockdownd_client_new(iphone_device_t device, lockdownd_client_ client_loc->ssl_session = NULL; client_loc->ssl_certificate = NULL; client_loc->in_SSL = 0; + client_loc->session_id = NULL; + client_loc->uuid = NULL; if (LOCKDOWN_E_SUCCESS != lockdownd_query_type(client_loc)) { log_debug_msg("%s: QueryType failed in the lockdownd client.\n", __func__); ret = LOCKDOWN_E_NOT_ENOUGH_DATA; } - char *uuid = NULL; - ret = iphone_device_get_uuid(device, &uuid); + ret = iphone_device_get_uuid(device, &client_loc->uuid); if (LOCKDOWN_E_SUCCESS != ret) { log_debug_msg("%s: failed to get device uuid.\n", __func__); } - log_debug_msg("%s: device uuid: %s\n", __func__, uuid); + log_debug_msg("%s: device uuid: %s\n", __func__, client_loc->uuid); userpref_get_host_id(&host_id); if (LOCKDOWN_E_SUCCESS == ret && !host_id) { ret = LOCKDOWN_E_INVALID_CONF; } - if (LOCKDOWN_E_SUCCESS == ret && !userpref_has_device_public_key(uuid)) - ret = lockdownd_pair(client_loc, uuid, host_id); - - if (uuid) { - free(uuid); - uuid = NULL; - } + if (LOCKDOWN_E_SUCCESS == ret && !userpref_has_device_public_key(client_loc->uuid)) + ret = lockdownd_pair(client_loc, host_id); if (LOCKDOWN_E_SUCCESS == ret) { ret = lockdownd_start_ssl_session(client_loc, host_id); @@ -687,12 +698,17 @@ lockdownd_error_t lockdownd_client_new(iphone_device_t device, lockdownd_client_ return ret; } -/** Generates the appropriate keys and pairs the device. It's part of the - * lockdownd handshake. +/** Function used internally by lockdownd_pair() and lockdownd_validate_pair() + * + * @param client The lockdown client to pair with. + * @param host_id The HostID to use for pairing. If NULL is passed, then + * the HostID of the current machine is used. A new HostID will be + * generated automatically when pairing is done for the first time. + * @param verb This is either "Pair" or "ValidatePair". * * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *host_id) +static lockdownd_error_t lockdownd_do_pair(lockdownd_client_t client, char *host_id, const char *verb) { lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t dict = NULL; @@ -703,6 +719,8 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho gnutls_datum_t root_cert = { NULL, 0 }; gnutls_datum_t public_key = { NULL, 0 }; + char *host_id_loc = host_id; + ret = lockdownd_get_device_public_key(client, &public_key); if (ret != LOCKDOWN_E_SUCCESS) { log_debug_msg("%s: device refused to send public key.\n", __func__); @@ -716,6 +734,10 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho return ret; } + if (!host_id) { + userpref_get_host_id(&host_id_loc); + } + /* Setup Pair request plist */ dict = plist_new_dict(); dict_record = plist_new_dict(); @@ -723,16 +745,20 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho plist_dict_insert_item(dict_record, "DeviceCertificate", plist_new_data((const char*)device_cert.data, device_cert.size)); plist_dict_insert_item(dict_record, "HostCertificate", plist_new_data((const char*)host_cert.data, host_cert.size)); - plist_dict_insert_item(dict_record, "HostID", plist_new_string(host_id)); + plist_dict_insert_item(dict_record, "HostID", plist_new_string(host_id_loc)); plist_dict_insert_item(dict_record, "RootCertificate", plist_new_data((const char*)root_cert.data, root_cert.size)); - plist_dict_insert_item(dict, "Request", plist_new_string("Pair")); + plist_dict_insert_item(dict, "Request", plist_new_string(verb)); /* send to iPhone */ ret = lockdownd_send(client, dict); plist_free(dict); dict = NULL; + if (!host_id) { + free(host_id_loc); + } + if (ret != LOCKDOWN_E_SUCCESS) return ret; @@ -742,24 +768,56 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho if (ret != LOCKDOWN_E_SUCCESS) return ret; - if (lockdown_check_result(dict, "Pair") == RESULT_SUCCESS) { - ret = LOCKDOWN_E_SUCCESS; + if (lockdown_check_result(dict, verb) != RESULT_SUCCESS) { + ret = LOCKDOWN_E_PAIRING_FAILED; } plist_free(dict); dict = NULL; /* store public key in config if pairing succeeded */ if (ret == LOCKDOWN_E_SUCCESS) { - log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair success\n", __func__); - userpref_set_device_public_key(uuid, public_key); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: %s success\n", __func__, verb); + userpref_set_device_public_key(client->uuid, public_key); } else { - log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair failure\n", __func__); - ret = LOCKDOWN_E_PAIRING_FAILED; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: %s failure\n", __func__, verb); } free(public_key.data); return ret; } +/** + * Pairs the device with the given HostID. + * It's part of the lockdownd handshake. + * + * @param client The lockdown client to pair with. + * @param host_id The HostID to use for pairing. If NULL is passed, then + * the HostID of the current machine is used. A new HostID will be + * generated automatically when pairing is done for the first time. + * + * @return an error code (LOCKDOWN_E_SUCCESS on success) + */ +lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id) +{ + return lockdownd_do_pair(client, host_id, "Pair"); +} + +/** + * Pairs the device with the given HostID. The difference to lockdownd_pair() + * is that the specified host will become trusted host of the device. + * It's part of the lockdownd handshake. + * + * @param client The lockdown client to pair with. + * @param host_id The HostID to use for pairing. If NULL is passed, then + * the HostID of the current machine is used. A new HostID will be + * generated automatically when pairing is done for the first time. + * + * @return an error code (LOCKDOWN_E_SUCCESS on success) + */ +lockdownd_error_t lockdownd_validate_pair(lockdownd_client_t client, char *host_id) +{ + return lockdownd_do_pair(client, host_id, "ValidatePair"); +} + /** * Tells the device to immediately enter recovery mode. * @@ -985,7 +1043,10 @@ lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const c uint32_t return_me = 0; lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; - client->session_id[0] = '\0'; + if (client->session_id) { + free(client->session_id); + client->session_id = NULL; + } /* Setup DevicePublicKey request plist */ dict = plist_new_dict(); @@ -1012,26 +1073,22 @@ lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const c if (!strcmp(error, "InvalidHostID")) { /* hostid is unknown. Pair and try again */ - char *uuid = NULL; char *host_id = NULL; userpref_get_host_id(&host_id); - if (LOCKDOWN_E_SUCCESS == lockdownd_get_device_uuid(client, &uuid) ) { - if (LOCKDOWN_E_SUCCESS == lockdownd_pair(client, uuid, host_id) ) { - /* start session again */ - plist_free(dict); - dict = plist_new_dict(); - plist_dict_insert_item(dict,"HostID", plist_new_string(HostID)); - plist_dict_insert_item(dict,"Request", plist_new_string("StartSession")); + if (LOCKDOWN_E_SUCCESS == lockdownd_pair(client, host_id) ) { + /* start session again */ + plist_free(dict); + dict = plist_new_dict(); + plist_dict_insert_item(dict,"HostID", plist_new_string(HostID)); + plist_dict_insert_item(dict,"Request", plist_new_string("StartSession")); - ret = lockdownd_send(client, dict); - plist_free(dict); - dict = NULL; + ret = lockdownd_send(client, dict); + plist_free(dict); + dict = NULL; - ret = lockdownd_recv(client, &dict); - } + ret = lockdownd_recv(client, &dict); } - free(uuid); free(host_id); } free(error); @@ -1100,27 +1157,16 @@ lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const c ret = LOCKDOWN_E_SUCCESS; } } - /* store session id */ + /* store session id, we need it for StopSession */ plist_t session_node = plist_dict_get_item(dict, "SessionID"); - if (session_node) { - - plist_type session_node_type = plist_get_node_type(session_node); - - if (session_node_type == PLIST_STRING) { - - char *session_id = NULL; - plist_get_string_val(session_node, &session_id); - - if (session_node_type == PLIST_STRING && session_id) { - /* we need to store the session ID for StopSession */ - strcpy(client->session_id, session_id); - log_dbg_msg(DBGMASK_LOCKDOWND, "%s: SessionID: %s\n", __func__, client->session_id); - } - if (session_id) - free(session_id); - } - } else + if (session_node && (plist_get_node_type(session_node) == PLIST_STRING)) { + plist_get_string_val(session_node, &client->session_id); + } + if (client->session_id) { + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: SessionID: %s\n", __func__, client->session_id); + } else { log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Failed to get SessionID!\n", __func__); + } plist_free(dict); dict = NULL; diff --git a/src/lockdown.h b/src/lockdown.h index 9312867..931623a 100644 --- a/src/lockdown.h +++ b/src/lockdown.h @@ -32,7 +32,8 @@ struct lockdownd_client_int { gnutls_session_t ssl_session; gnutls_certificate_credentials_t ssl_certificate; int in_SSL; - char session_id[40]; + char *session_id; + char *uuid; }; lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key); diff --git a/swig/Makefile.am b/swig/Makefile.am index a38534d..ef87733 100644 --- a/swig/Makefile.am +++ b/swig/Makefile.am @@ -1,5 +1,6 @@ INCLUDES = -I$(top_srcdir)/include $(libplist_CFLAGS) $(SWIG_PYTHON_CPPFLAGS) -I$(oldincludedir) +if HAVE_SWIG BUILT_SOURCES = iphone_wrap.cxx SWIG_SOURCES = iphone.i @@ -29,3 +30,5 @@ _iphone_la_LIBADD = $(top_builddir)/src/libiphone.la $(libplistmm_LIBS) iphone_wrap.cxx : $(SWIG_SOURCES) $(SWIG) $(SWIG_PYTHON_OPT) $(INCLUDES) -I$(top_srcdir)/src -o $@ $< +endif # HAVE_SWIG + |