From 12a44afce569c2cdf31018c259745e09b13bffc3 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 17 Nov 2009 00:17:22 +0100 Subject: added endian safety to AFC This should make libiphone compatible with big endian machines. [#85 state:resolved] Signed-off-by: Matt Colyer --- src/AFC.c | 34 +++++++++++++++++++--------------- src/AFC.h | 12 ++++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/AFC.c b/src/AFC.c index fe6fc00..2b83c44 100644 --- a/src/AFC.c +++ b/src/AFC.c @@ -161,6 +161,7 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l return -1; } + AFCPacket_to_LE(client->afc_packet); iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); if (sent == 0) { return bytes; @@ -190,6 +191,7 @@ 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"); + AFCPacket_to_LE(client->afc_packet); iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); if (sent == 0) { return bytes; @@ -230,6 +232,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * /* first, read the AFC header */ iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); + AFCPacket_from_LE(&header); if (*bytes <= 0) { log_debug_msg("%s: Just didn't get enough.\n", __func__); *dump_here = NULL; @@ -688,7 +691,7 @@ 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; + uint64_t file_mode_loc = GUINT64_TO_LE(file_mode); int bytes = 0; char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -702,8 +705,7 @@ 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; @@ -765,7 +767,7 @@ 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)); @@ -822,7 +824,7 @@ afc_file_write(afc_client_t client, uint64_t handle, { char *acknowledgement = NULL; const int MAXIMUM_WRITE_SIZE = 1 << 15; - uint32_t zero = 0, current_count = 0, i = 0; + uint32_t current_count = 0, i = 0; uint32_t segments = (length / MAXIMUM_WRITE_SIZE); int bytes_loc = 0; char *out_buffer = NULL; @@ -890,7 +892,6 @@ afc_file_write(afc_client_t client, uint64_t handle, return AFC_E_SUCCESS; } - zero = bytes_loc; ret = afc_receive_data(client, &acknowledgement, &bytes_loc); afc_unlock(client); if (ret != AFC_E_SUCCESS) { @@ -958,7 +959,7 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op { char *buffer = malloc(16); int bytes = 0; - uint64_t op = operation; + uint64_t op = GUINT64_TO_LE(operation); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1006,7 +1007,8 @@ 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; + int64_t offset_loc = (int64_t)GUINT64_TO_LE(offset); + uint64_t whence_loc = GUINT64_TO_LE(whence); int bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -1017,9 +1019,8 @@ 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); @@ -1077,6 +1078,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); @@ -1101,6 +1103,7 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new { char *buffer = (char *) malloc(sizeof(char) * 16); int bytes = 0; + uint64_t newsize_loc = GUINT64_TO_LE(newsize); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1110,7 +1113,7 @@ 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); @@ -1145,7 +1148,7 @@ afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); int bytes = 0; - uint64_t size_requested = newsize; + uint64_t size_requested = GUINT64_TO_LE(newsize); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || !client->connection) @@ -1189,7 +1192,7 @@ 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; + uint64_t type = GUINT64_TO_LE(linktype); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !target || !linkname || !client->afc_packet || !client->connection) @@ -1237,6 +1240,7 @@ 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; + 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,7 +1249,7 @@ 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; diff --git a/src/AFC.h b/src/AFC.h index 6b4a0aa..5db865f 100644 --- a/src/AFC.h +++ b/src/AFC.h @@ -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; -- cgit v1.1-32-gdbae From 7adc81dddd764b0ef76fefd73852b835d3211b9b Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sat, 28 Nov 2009 04:33:24 +0100 Subject: uint32_t type fixes for afc_file_read/afc_file_write This patch also adapts all corresponding internal functions. The buffer lengths are now consistently handled as uint32_t. --- include/libiphone/afc.h | 4 +- src/AFC.c | 230 +++++++++++++++++++++++++----------------------- 2 files changed, 124 insertions(+), 110 deletions(-) diff --git a/include/libiphone/afc.h b/include/libiphone/afc.h index 1c714c9..94eb02e 100644 --- a/include/libiphone/afc.h +++ b/include/libiphone/afc.h @@ -95,8 +95,8 @@ 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); diff --git a/src/AFC.c b/src/AFC.c index 2b83c44..15d746e 100644 --- a/src/AFC.c +++ b/src/AFC.c @@ -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,21 +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); @@ -182,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); @@ -191,36 +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; @@ -228,16 +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); + iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), bytes_recv); AFCPacket_from_LE(&header); - if (*bytes <= 0) { + 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; @@ -265,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 { @@ -285,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); @@ -303,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); @@ -348,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); @@ -356,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') @@ -372,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) @@ -401,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; @@ -414,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; } @@ -445,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; @@ -457,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; } @@ -526,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) @@ -537,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; } @@ -569,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) @@ -582,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; } @@ -609,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; @@ -621,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; } @@ -650,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) @@ -661,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); @@ -692,7 +706,7 @@ afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle) { uint64_t file_mode_loc = GUINT64_TO_LE(file_mode); - int bytes = 0; + uint32_t bytes = 0; char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -710,10 +724,10 @@ afc_file_open(afc_client_t client, const char *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; @@ -742,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); @@ -770,10 +785,10 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint 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; } @@ -788,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 { @@ -804,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; } @@ -814,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; + 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); @@ -846,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; } @@ -868,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; } @@ -880,15 +894,15 @@ 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; } @@ -899,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; } @@ -911,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)) @@ -925,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; } @@ -958,7 +972,7 @@ 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; + uint32_t bytes = 0; uint64_t op = GUINT64_TO_LE(operation); afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -975,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; @@ -1009,7 +1023,7 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, char *buffer = (char *) malloc(sizeof(char) * 24); int64_t offset_loc = (int64_t)GUINT64_TO_LE(offset); uint64_t whence_loc = GUINT64_TO_LE(whence); - int bytes = 0; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1023,11 +1037,11 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, 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; } @@ -1052,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)) @@ -1064,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; } @@ -1102,7 +1116,7 @@ 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; @@ -1116,11 +1130,11 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new 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; } @@ -1147,7 +1161,7 @@ afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); - int bytes = 0; + uint32_t bytes = 0; uint64_t size_requested = GUINT64_TO_LE(newsize); afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -1161,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; } @@ -1191,7 +1205,7 @@ 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; + uint32_t bytes = 0; uint64_t type = GUINT64_TO_LE(linktype); afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -1210,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; } @@ -1239,7 +1253,7 @@ 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; @@ -1253,9 +1267,9 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt 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; } -- cgit v1.1-32-gdbae From fe239d46809996def06502d55f8b34b46e475e53 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sat, 28 Nov 2009 04:50:01 +0100 Subject: afc_truncate: use uint64_t instead of off_t This makes afc_truncate look more like afc_file_truncate which is also using uint64_t for the file size. [#82 state:resolved] Signed-off-by: Matt Colyer --- include/libiphone/afc.h | 2 +- src/AFC.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/libiphone/afc.h b/include/libiphone/afc.h index 94eb02e..7eff678 100644 --- a/include/libiphone/afc.h +++ b/include/libiphone/afc.h @@ -103,7 +103,7 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new 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/src/AFC.c b/src/AFC.c index 15d746e..956c8fc 100644 --- a/src/AFC.c +++ b/src/AFC.c @@ -1157,7 +1157,7 @@ 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)); -- cgit v1.1-32-gdbae From 15b9f3451e20ccb4f9c6cdadc0dd75bc72246360 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sun, 29 Nov 2009 16:04:18 +0100 Subject: Evaluate EnableSessionSSL key on session startup This is required if the device does not send the EnableSessionSSL:true key-value pair in the answer to the StartSession request. [#92 state:resolved] Signed-off-by: Matt Colyer --- src/lockdown.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lockdown.c b/src/lockdown.c index d147f75..b182706 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -971,7 +971,8 @@ lockdownd_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datu return ret; } -/** Starts SSL communication with lockdownd after the iPhone has been paired. +/** Starts communication with lockdownd after the iPhone has been paired, + * and if the device requires it, switches to SSL mode. * * @param client The lockdownd client * @param HostID The HostID used with this phone @@ -1038,9 +1039,24 @@ lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const c } ret = LOCKDOWN_E_SSL_ERROR; + + int session_ok = 0; + uint8_t UseSSL = 0; + if (lockdown_check_result(dict, "StartSession") == RESULT_SUCCESS) { + plist_t enable_ssl = plist_dict_get_item(dict, "EnableSessionSSL"); + if (enable_ssl && (plist_get_node_type(enable_ssl) == PLIST_BOOLEAN)) { + plist_get_bool_val(enable_ssl, &UseSSL); + } + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Session startup OK\n", __func__); + session_ok = 1; + } + if (session_ok && !UseSSL) { + client->in_SSL = 0; + ret = LOCKDOWN_E_SUCCESS; + } else if (session_ok) { // Set up GnuTLS... - log_dbg_msg(DBGMASK_LOCKDOWND, "%s: started the session OK, now trying GnuTLS\n", __func__); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Switching to SSL mode\n", __func__); errno = 0; gnutls_global_init(); //gnutls_anon_allocate_client_credentials(&anoncred); -- cgit v1.1-32-gdbae From 3bb7d9596e9b852b2e13185386cc0b71c952e84d Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sun, 6 Dec 2009 10:36:14 +0100 Subject: Updated autofoo stuff; swig is now optional and can be disabled. Use --without-swig to prevent building the swig extensions even if swig is installed. [#93 state:resolved] Signed-off-by: Matt Colyer --- configure.ac | 47 ++++++++++++++++++++++++++++++++++++++++++++++- m4/ac_pkg_swig.m4 | 8 ++++---- m4/ax_swig_enable_cxx.m4 | 4 +++- swig/Makefile.am | 3 +++ 4 files changed, 56 insertions(+), 6 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/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 ]) 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 + -- cgit v1.1-32-gdbae From 213025d04ae8788be393b63e245f2805386f7f8a Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 7 Dec 2009 18:07:56 +0100 Subject: fix signature of lockdownd_stop_session This removes the session_id parameter from lockdownd_stop_session because the session_id is stored in the lockdownd_client_int structure anyway. --- include/libiphone/lockdown.h | 2 +- src/lockdown.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/libiphone/lockdown.h b/include/libiphone/lockdown.h index af58190..daa5800 100644 --- a/include/libiphone/lockdown.h +++ b/include/libiphone/lockdown.h @@ -60,7 +60,7 @@ 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); diff --git a/src/lockdown.c b/src/lockdown.c index b182706..afca410 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -115,7 +115,7 @@ 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; @@ -124,7 +124,7 @@ lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char * 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__); @@ -170,7 +170,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); } -- cgit v1.1-32-gdbae From ea45a41a7987ab1d05e2160ce831d2fcff695077 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 7 Dec 2009 18:13:21 +0100 Subject: better handling of session_id This will change session_id out of the lockdownd_client_int struct to a pointer instead of using a buffer of fixed size. The session_id is allocated anyway by libplist when reading it from the plist received from the device, so why don't just use it? [#94 state:resolved] Signed-off-by: Matt Colyer --- src/lockdown.c | 47 ++++++++++++++++++++++++++--------------------- src/lockdown.h | 2 +- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/lockdown.c b/src/lockdown.c index afca410..6bf4c84 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -120,13 +120,18 @@ 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(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) plist_free(dict); dict = NULL; + free(client->session_id); + client->session_id = NULL; + return ret; } @@ -209,6 +217,10 @@ lockdownd_error_t lockdownd_client_free(lockdownd_client_t client) } } + if (client->session_id) { + free(client->session_id); + } + free(client); return ret; } @@ -642,6 +654,7 @@ 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; if (LOCKDOWN_E_SUCCESS != lockdownd_query_type(client_loc)) { log_debug_msg("%s: QueryType failed in the lockdownd client.\n", __func__); @@ -985,7 +998,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(); @@ -1100,27 +1116,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..49b467f 100644 --- a/src/lockdown.h +++ b/src/lockdown.h @@ -32,7 +32,7 @@ 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; }; lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key); -- cgit v1.1-32-gdbae From 62c23c1ccafedab1bf8bed2fb0e511462b8ab4cc Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 7 Dec 2009 16:37:47 +0100 Subject: fix lockdownd_pair returning success on error This fixes a bug where lockdown_check_result() might return -1 and lockdownd_pair() still returns success. Thanks to dborca for spotting this. --- src/lockdown.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lockdown.c b/src/lockdown.c index 6bf4c84..02b0024 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -755,8 +755,8 @@ 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, "Pair") != RESULT_SUCCESS) { + ret = LOCKDOWN_E_PAIRING_FAILED; } plist_free(dict); dict = NULL; @@ -767,7 +767,6 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho userpref_set_device_public_key(uuid, public_key); } else { log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair failure\n", __func__); - ret = LOCKDOWN_E_PAIRING_FAILED; } free(public_key.data); return ret; -- cgit v1.1-32-gdbae From b9ecd70c30ac1fd7024cadfcda9c7be1d1f7f44f Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 7 Dec 2009 18:58:55 +0100 Subject: cache device uuid in client struct When accessing/storing key info with userprefs, a device uuid is required that makes it possible to distinguish between different devices. On execution of lockdownd_client_new, the uuid is queried via lockdown and now stored in the client struct for later reuse. This patch also removes the uuid parameter from lockdownd_pair(). --- include/libiphone/lockdown.h | 2 +- src/lockdown.c | 51 ++++++++++++++++++++++---------------------- src/lockdown.h | 1 + 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/include/libiphone/lockdown.h b/include/libiphone/lockdown.h index daa5800..e6b75da 100644 --- a/include/libiphone/lockdown.h +++ b/include/libiphone/lockdown.h @@ -63,7 +63,7 @@ lockdownd_error_t lockdownd_start_service(lockdownd_client_t client, const char 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_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/src/lockdown.c b/src/lockdown.c index 02b0024..b6289ed 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -220,6 +220,9 @@ 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; @@ -655,31 +658,26 @@ lockdownd_error_t lockdownd_client_new(iphone_device_t device, lockdownd_client_ 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); @@ -703,9 +701,14 @@ lockdownd_error_t lockdownd_client_new(iphone_device_t device, lockdownd_client_ /** Generates the appropriate keys and pairs 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_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 ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t dict = NULL; @@ -764,7 +767,7 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho /* 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); + userpref_set_device_public_key(client->uuid, public_key); } else { log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair failure\n", __func__); } @@ -1027,26 +1030,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); diff --git a/src/lockdown.h b/src/lockdown.h index 49b467f..931623a 100644 --- a/src/lockdown.h +++ b/src/lockdown.h @@ -33,6 +33,7 @@ struct lockdownd_client_int { gnutls_certificate_credentials_t ssl_certificate; int in_SSL; char *session_id; + char *uuid; }; lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key); -- cgit v1.1-32-gdbae From 6ae6880ce5cf00977dfdb204855a7308d7bf42c9 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 7 Dec 2009 19:16:48 +0100 Subject: Allow passing NULL as HostID to lockdownd_pair() When NULL is given as HostID, lockdownd_pair() will use the HostID available from userprefs. --- src/lockdown.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lockdown.c b/src/lockdown.c index b6289ed..d717c01 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -719,6 +719,8 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id) 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__); @@ -732,6 +734,10 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id) 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(); @@ -739,7 +745,7 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id) 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")); @@ -749,6 +755,10 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id) plist_free(dict); dict = NULL; + if (!host_id) { + free(host_id_loc); + } + if (ret != LOCKDOWN_E_SUCCESS) return ret; -- cgit v1.1-32-gdbae From 318cc4f7b336109819c7b4c6a1a9f2e8d37d9bed Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 7 Dec 2009 19:27:54 +0100 Subject: New function lockdownd_validate_pair() This function allows the current host (or the host specified by the given HostID to become the trusted host of the device. [#89 state:resolved] Signed-off-by: Matt Colyer --- include/libiphone/lockdown.h | 1 + src/lockdown.c | 47 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/libiphone/lockdown.h b/include/libiphone/lockdown.h index e6b75da..31ffeab 100644 --- a/include/libiphone/lockdown.h +++ b/include/libiphone/lockdown.h @@ -64,6 +64,7 @@ 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 *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/src/lockdown.c b/src/lockdown.c index d717c01..fb5f8f5 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -698,17 +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 *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; @@ -748,7 +748,7 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *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); @@ -768,7 +768,7 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id) if (ret != LOCKDOWN_E_SUCCESS) return ret; - if (lockdown_check_result(dict, "Pair") != RESULT_SUCCESS) { + if (lockdown_check_result(dict, verb) != RESULT_SUCCESS) { ret = LOCKDOWN_E_PAIRING_FAILED; } plist_free(dict); @@ -776,15 +776,48 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id) /* store public key in config if pairing succeeded */ if (ret == LOCKDOWN_E_SUCCESS) { - log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair success\n", __func__); + 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__); + 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. * -- cgit v1.1-32-gdbae