From 2b05e48cb4a90dfc94ff584124f08e431398bb1a Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Mon, 1 Sep 2008 15:04:31 -0700 Subject: Enforce a modified kr style. Use "make indent" from now on before committing. --- Makefile.am | 3 + src/AFC.c | 597 ++++++++++++++++++++++++++++++++------------------------- src/AFC.h | 1 - src/ifuse.c | 187 ++++++++++-------- src/initconf.c | 72 +++---- src/iphone.c | 109 ++++++----- src/iphone.h | 2 +- src/lockdown.c | 586 ++++++++++++++++++++++++++++++++----------------------- src/lockdown.h | 3 +- src/main.c | 78 ++++---- src/plist.c | 76 ++++---- src/plist.h | 10 +- src/usbmux.c | 188 ++++++++++-------- src/userpref.c | 115 ++++++----- src/userpref.h | 18 +- 15 files changed, 1167 insertions(+), 878 deletions(-) diff --git a/Makefile.am b/Makefile.am index 76541d7..82c4c95 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,3 +3,6 @@ SUBDIRS = src doc: doxygen doxygen.cfg + +indent: + indent -kr -ut -ts4 -l120 src/*.c src/*.h diff --git a/src/AFC.c b/src/AFC.c index b475a06..42ada68 100644 --- a/src/AFC.c +++ b/src/AFC.c @@ -33,10 +33,12 @@ extern int debug; * * @param client The AFC client connection to lock */ -static void afc_lock(iphone_afc_client_t client) { - if (debug) fprintf(stderr, "Locked\n"); +static void afc_lock(iphone_afc_client_t client) +{ + if (debug) + fprintf(stderr, "Locked\n"); while (client->lock) { - usleep(500); // they say it's obsolete, but whatever + usleep(500); // they say it's obsolete, but whatever } client->lock = 1; } @@ -45,9 +47,11 @@ static void afc_lock(iphone_afc_client_t client) { * * @param client The AFC */ -static void afc_unlock(iphone_afc_client_t client) { // just to be pretty - if (debug) fprintf(stderr, "Unlocked\n"); - client->lock = 0; +static void afc_unlock(iphone_afc_client_t client) +{ // just to be pretty + if (debug) + fprintf(stderr, "Unlocked\n"); + client->lock = 0; } /** Makes a connection to the AFC service on the phone. @@ -58,22 +62,23 @@ static void afc_unlock(iphone_afc_client_t client) { // just to be pretty * * @return A handle to the newly-connected client or NULL upon error. */ -iphone_error_t iphone_afc_new_client ( iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t *client ) { +iphone_error_t iphone_afc_new_client(iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t * client) +{ int ret = IPHONE_E_SUCCESS; - iphone_afc_client_t client_loc = (iphone_afc_client_t)malloc(sizeof(struct iphone_afc_client_int)); - - if (!device) return IPHONE_E_INVALID_ARG; - + iphone_afc_client_t client_loc = (iphone_afc_client_t) malloc(sizeof(struct iphone_afc_client_int)); + + if (!device) + return IPHONE_E_INVALID_ARG; + // Attempt connection client_loc->connection = NULL; - ret = iphone_mux_new_client(device, src_port, dst_port,&client_loc->connection); + ret = iphone_mux_new_client(device, src_port, dst_port, &client_loc->connection); if (IPHONE_E_SUCCESS != ret || !client_loc->connection) { free(client_loc); - return ret; + return ret; } - // Allocate a packet - client_loc->afc_packet = (AFCPacket*)malloc(sizeof(AFCPacket)); + client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); if (!client_loc->afc_packet) { iphone_mux_free_client(client_loc->connection); free(client_loc); @@ -100,10 +105,11 @@ iphone_error_t iphone_afc_new_client ( iphone_device_t device, int src_port, int * * @param client The client to disconnect. */ -iphone_error_t iphone_afc_free_client ( iphone_afc_client_t client ) { +iphone_error_t iphone_afc_free_client(iphone_afc_client_t client) +{ if (!client || !client->connection || !client->afc_packet) return IPHONE_E_INVALID_ARG; - + iphone_mux_free_client(client->connection); free(client->afc_packet); free(client); @@ -124,65 +130,78 @@ iphone_error_t iphone_afc_free_client ( iphone_afc_client_t client ) { * reason is that if you set them to different values, it indicates * you want to send the data as two packets. */ -static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int length) { +static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int length) +{ int bytes = 0, offset = 0; - char *buffer; + char *buffer; + + if (!client || !client->connection || !client->afc_packet) + return 0; + if (!data || !length) + length = 0; - if (!client || !client->connection || !client->afc_packet) return 0; - if (!data || !length) length = 0; - client->afc_packet->packet_num++; if (!client->afc_packet->entire_length) { client->afc_packet->entire_length = (length) ? sizeof(AFCPacket) + length + 1 : sizeof(AFCPacket); client->afc_packet->this_length = client->afc_packet->entire_length; } - if (!client->afc_packet->this_length){ + if (!client->afc_packet->this_length) { client->afc_packet->this_length = sizeof(AFCPacket); } - - // We want to send two segments; buffer+sizeof(AFCPacket) to this_length is the parameters + // We want to send two segments; buffer+sizeof(AFCPacket) to + // this_length is the parameters // And everything beyond that is the next packet. (for writing) if (client->afc_packet->this_length != client->afc_packet->entire_length) { - buffer = (char*)malloc(client->afc_packet->this_length); - memcpy(buffer, (char*)client->afc_packet, sizeof(AFCPacket)); + buffer = (char *) malloc(client->afc_packet->this_length); + memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); offset = client->afc_packet->this_length - sizeof(AFCPacket); - - if (debug) fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset); + + if (debug) + fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset); if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { - if (debug){ + if (debug) { fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed"); - fprintf(stderr, "to based on the packet.\n"); - fprintf(stderr, "length minus offset: %i\n", length-offset); - fprintf(stderr, "rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length); + fprintf(stderr, "to based on the packet.\n"); + fprintf(stderr, "length minus offset: %i\n", length - offset); + fprintf(stderr, "rest of packet: %i\n", + client->afc_packet->entire_length - client->afc_packet->this_length); } free(buffer); return -1; } - memcpy(buffer+sizeof(AFCPacket), data, offset); + memcpy(buffer + sizeof(AFCPacket), data, offset); iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); free(buffer); if (bytes <= 0) { return bytes; - } - + } + if (debug) { fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n"); - fprintf(stderr, "Length: %i\n", length-offset); + fprintf(stderr, "Length: %i\n", length - offset); fprintf(stderr, "Buffer: \n"); - fwrite(data+offset, 1, length-offset, stdout); + fwrite(data + offset, 1, length - offset, stdout); } - - iphone_mux_send(client->connection, data+offset, length-offset, &bytes); + + iphone_mux_send(client->connection, data + offset, length - offset, &bytes); return bytes; } else { - if (debug) fprintf(stderr, "dispatch_AFC_packet doin things the old way\n"); - char *buffer = (char*)malloc(sizeof(char) * client->afc_packet->this_length); - if (debug) fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); - memcpy(buffer, (char*)client->afc_packet, sizeof(AFCPacket)); - if (debug) fprintf(stderr, "dispatch_AFC_packet packet data follows\n"); - if (length > 0) { memcpy(buffer+sizeof(AFCPacket), data, length); buffer[sizeof(AFCPacket)+length] = '\0'; } - if (debug) fwrite(buffer, 1, client->afc_packet->this_length, stdout); - if (debug) fprintf(stderr, "\n"); + if (debug) + fprintf(stderr, "dispatch_AFC_packet doin things the old way\n"); + char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); + if (debug) + fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); + memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); + if (debug) + fprintf(stderr, "dispatch_AFC_packet packet data follows\n"); + if (length > 0) { + memcpy(buffer + sizeof(AFCPacket), data, length); + buffer[sizeof(AFCPacket) + length] = '\0'; + } + if (debug) + fwrite(buffer, 1, client->afc_packet->this_length, stdout); + if (debug) + fprintf(stderr, "\n"); iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); if (buffer) { @@ -205,13 +224,14 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int * AFC_ERROR operation) */ -static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) { +static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) +{ AFCPacket *r_packet; - char *buffer = (char*)malloc(sizeof(AFCPacket) * 4); + char *buffer = (char *) malloc(sizeof(AFCPacket) * 4); char *final_buffer = NULL; - int bytes = 0, recv_len = 0, current_count=0; + int bytes = 0, recv_len = 0, current_count = 0; int retval = 0; - + iphone_mux_recv(client->connection, buffer, sizeof(AFCPacket) * 4, &bytes); if (bytes <= 0) { free(buffer); @@ -219,102 +239,119 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) { *dump_here = NULL; return -1; } - - r_packet = (AFCPacket*)malloc(sizeof(AFCPacket)); + + r_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); memcpy(r_packet, buffer, sizeof(AFCPacket)); - - if (r_packet->entire_length == r_packet->this_length && r_packet->entire_length > sizeof(AFCPacket) && r_packet->operation != AFC_ERROR) { - *dump_here = (char*)malloc(sizeof(char) * (r_packet->entire_length-sizeof(AFCPacket))); - memcpy(*dump_here, buffer+sizeof(AFCPacket), r_packet->entire_length-sizeof(AFCPacket)); - retval = r_packet->entire_length - sizeof(AFCPacket); + + if (r_packet->entire_length == r_packet->this_length + && r_packet->entire_length > sizeof(AFCPacket) && r_packet->operation != AFC_ERROR) { + *dump_here = (char *) malloc(sizeof(char) * (r_packet->entire_length - sizeof(AFCPacket))); + memcpy(*dump_here, buffer + sizeof(AFCPacket), r_packet->entire_length - sizeof(AFCPacket)); + retval = r_packet->entire_length - sizeof(AFCPacket); free(buffer); free(r_packet); return retval; } - + uint32 param1 = buffer[sizeof(AFCPacket)]; free(buffer); if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { - if (debug) fprintf(stderr, "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", - r_packet->operation, client->afc_packet->operation, param1); + if (debug) + fprintf(stderr, + "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", + r_packet->operation, client->afc_packet->operation, param1); recv_len = r_packet->entire_length - r_packet->this_length; free(r_packet); - if (debug) fprintf(stderr, "recv_len=%d\n", recv_len); - if(param1 == 0) { - if (debug) fprintf(stderr, "... false alarm, but still\n"); + if (debug) + fprintf(stderr, "recv_len=%d\n", recv_len); + if (param1 == 0) { + if (debug) + fprintf(stderr, "... false alarm, but still\n"); *dump_here = NULL; return 0; + } else { + if (debug) + fprintf(stderr, "Errno %i\n", param1); } - else { if (debug) fprintf(stderr, "Errno %i\n", param1); } *dump_here = NULL; return -1; } else { - if (debug) fprintf(stderr, "Operation code %x\nFull length %i and this length %i\n", r_packet->operation, r_packet->entire_length, r_packet->this_length); + if (debug) + fprintf(stderr, + "Operation code %x\nFull length %i and this length %i\n", + r_packet->operation, r_packet->entire_length, r_packet->this_length); } recv_len = r_packet->entire_length - r_packet->this_length; free(r_packet); - if (!recv_len && r_packet->operation == AFC_SUCCESS_RESPONSE) - { + if (!recv_len && r_packet->operation == AFC_SUCCESS_RESPONSE) { *dump_here = NULL; return 0; } - // Keep collecting packets until we have received the entire file. - buffer = (char*)malloc(sizeof(char) * (recv_len < MAXIMUM_PACKET_SIZE) ? recv_len : MAXIMUM_PACKET_SIZE); - final_buffer = (char*)malloc(sizeof(char) * recv_len); - while(current_count < recv_len){ - iphone_mux_recv(client->connection, buffer, recv_len-current_count, &bytes); - if (debug) fprintf(stderr, "receive_AFC_data: still collecting packets\n"); - if (bytes < 0) - { - if(debug) fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes); + buffer = (char *) malloc(sizeof(char) * (recv_len < MAXIMUM_PACKET_SIZE) ? recv_len : MAXIMUM_PACKET_SIZE); + final_buffer = (char *) malloc(sizeof(char) * recv_len); + while (current_count < recv_len) { + iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes); + if (debug) + fprintf(stderr, "receive_AFC_data: still collecting packets\n"); + if (bytes < 0) { + if (debug) + fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes); break; } - if (bytes > recv_len-current_count) - { - if(debug) fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n"); + if (bytes > recv_len - current_count) { + if (debug) + fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n"); break; } if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { - if (debug) fprintf(stderr, "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", strstr(buffer, "CFA6LPAA") - buffer); - if (debug) fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes); + if (debug) + fprintf(stderr, + "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", + strstr(buffer, "CFA6LPAA") - buffer); + if (debug) + fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes); } - - memcpy(final_buffer+current_count, buffer, bytes); + + memcpy(final_buffer + current_count, buffer, bytes); current_count += bytes; } free(buffer); - + *dump_here = final_buffer; return current_count; } -static int count_nullspaces(char *string, int number) { +static int count_nullspaces(char *string, int number) +{ int i = 0, nulls = 0; - + for (i = 0; i < number; i++) { - if (string[i] == '\0') nulls++; + if (string[i] == '\0') + nulls++; } - + return nulls; } -static char **make_strings_list(char *tokens, int true_length) { +static char **make_strings_list(char *tokens, int true_length) +{ int nulls = 0, i = 0, j = 0; char **list = NULL; - - if (!tokens || !true_length) return NULL; - + + if (!tokens || !true_length) + return NULL; + nulls = count_nullspaces(tokens, true_length); - list = (char**)malloc(sizeof(char*) * (nulls + 1)); + list = (char **) malloc(sizeof(char *) * (nulls + 1)); for (i = 0; i < nulls; i++) { - list[i] = strdup(tokens+j); + list[i] = strdup(tokens + j); j += strlen(list[i]) + 1; } list[i] = NULL; - + return list; } @@ -326,15 +363,17 @@ static char **make_strings_list(char *tokens, int true_length) { * @return A char ** list of files in that directory, terminated by an empty * string for now or NULL if there was an error. */ -iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char *dir, char ***list) { +iphone_error_t iphone_afc_get_dir_list(iphone_afc_client_t client, const char *dir, char ***list) +{ int bytes = 0; char *data = NULL, **list_loc = NULL; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - - if (!client || !dir || !list || (list && *list)) return IPHONE_E_INVALID_ARG; + + if (!client || !dir || !list || (list && *list)) + return IPHONE_E_INVALID_ARG; afc_lock(client); - + // Send the command client->afc_packet->operation = AFC_LIST_DIR; client->afc_packet->entire_length = 0; @@ -344,22 +383,22 @@ iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } - // Receive the data bytes = receive_AFC_data(client, &data); if (bytes < 0 && !data) { afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; - } - + } // Parse the data list_loc = make_strings_list(data, bytes); - if (list_loc) ret = IPHONE_E_SUCCESS; - if (data) free(data); + if (list_loc) + ret = IPHONE_E_SUCCESS; + if (data) + free(data); afc_unlock(client); *list = list_loc; - + return ret; } @@ -370,14 +409,16 @@ iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char * @return A char ** list of parameters as given by AFC or NULL if there was an * error. */ -iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***infos) { +iphone_error_t iphone_afc_get_devinfo(iphone_afc_client_t client, char ***infos) +{ int bytes = 0; char *data = NULL, **list = NULL; - - if (!client || !infos) return IPHONE_E_INVALID_ARG; + + if (!client || !infos) + return IPHONE_E_INVALID_ARG; afc_lock(client); - + // Send the command client->afc_packet->operation = AFC_GET_DEVINFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; @@ -386,18 +427,17 @@ iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***info afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } - // Receive the data bytes = receive_AFC_data(client, &data); if (bytes < 0 && !data) { afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; - } - + } // Parse the data list = make_strings_list(data, bytes); - if (data) free(data); - + if (data) + free(data); + afc_unlock(client); *infos = list; return IPHONE_E_SUCCESS; @@ -411,14 +451,16 @@ iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***info * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char *path) { +iphone_error_t iphone_afc_delete_file(iphone_afc_client_t client, const char *path) +{ char *response = NULL; int bytes; - - if (!client || !path || !client->afc_packet || !client->connection) return IPHONE_E_INVALID_ARG; - + + if (!client || !path || !client->afc_packet || !client->connection) + return IPHONE_E_INVALID_ARG; + afc_lock(client); - + // Send command client->afc_packet->this_length = client->afc_packet->entire_length = 0; client->afc_packet->operation = AFC_DELETE; @@ -427,13 +469,13 @@ iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char * afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } - // Receive response bytes = receive_AFC_data(client, &response); - if (response) free(response); - + if (response) + free(response); + afc_unlock(client); - + if (bytes < 0) { return IPHONE_E_NOT_ENOUGH_DATA; } else { @@ -450,18 +492,20 @@ iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char * * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char *from, const char *to) { +iphone_error_t iphone_afc_rename_file(iphone_afc_client_t client, const char *from, const char *to) +{ char *response = NULL; - char *send = (char*)malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32))); + char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32))); int bytes = 0; - - if (!client || !from || !to || !client->afc_packet || !client->connection) return IPHONE_E_INVALID_ARG; - + + if (!client || !from || !to || !client->afc_packet || !client->connection) + return IPHONE_E_INVALID_ARG; + afc_lock(client); - + // Send command - memcpy(send, from, strlen(from)+1); - memcpy(send+strlen(from)+1, to, strlen(to)+1); + memcpy(send, from, strlen(from) + 1); + 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_RENAME; bytes = dispatch_AFC_packet(client, send, strlen(to) + strlen(from) + 2); @@ -470,13 +514,13 @@ iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char * afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } - // Receive response bytes = receive_AFC_data(client, &response); - if (response) free(response); + if (response) + free(response); afc_unlock(client); - + if (bytes < 0) { return IPHONE_E_NOT_ENOUGH_DATA; } else { @@ -493,14 +537,16 @@ iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char * * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) { +iphone_error_t iphone_afc_mkdir(iphone_afc_client_t client, const char *dir) +{ int bytes = 0; char *response = NULL; - - if (!client) return IPHONE_E_INVALID_ARG; - + + if (!client) + return IPHONE_E_INVALID_ARG; + afc_lock(client); - + // Send command client->afc_packet->operation = AFC_MAKE_DIR; client->afc_packet->this_length = client->afc_packet->entire_length = 0; @@ -509,13 +555,13 @@ iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) { afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } - // Receive response bytes = receive_AFC_data(client, &response); - if (response) free(response); + if (response) + free(response); afc_unlock(client); - + if (bytes < 0) { return IPHONE_E_NOT_ENOUGH_DATA; } else { @@ -531,18 +577,19 @@ iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) { * @return A pointer to an AFCFile struct containing the information received, * or NULL on failure. */ -iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path) { +iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path) +{ char *received, **list; iphone_afc_file_t my_file; int length, i = 0; - + afc_lock(client); - + // Send command client->afc_packet->operation = AFC_GET_INFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; dispatch_AFC_packet(client, path, strlen(path)); - + // Receive data length = receive_AFC_data(client, &received); if (received) { @@ -554,23 +601,23 @@ iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path } afc_unlock(client); - + // Parse the data if (list) { - my_file = (iphone_afc_file_t)malloc(sizeof(struct iphone_afc_file_int)); + my_file = (iphone_afc_file_t) malloc(sizeof(struct iphone_afc_file_int)); for (i = 0; list[i]; i++) { if (!strcmp(list[i], "st_size")) { - my_file->size = atoi(list[i+1]); + my_file->size = atoi(list[i + 1]); } - + if (!strcmp(list[i], "st_blocks")) { - my_file->blocks = atoi(list[i+1]); + my_file->blocks = atoi(list[i + 1]); } - + if (!strcmp(list[i], "st_ifmt")) { - if (!strcmp(list[i+1], "S_IFREG")) { + if (!strcmp(list[i + 1], "S_IFREG")) { my_file->type = S_IFREG; - } else if (!strcmp(list[i+1], "S_IFDIR")) { + } else if (!strcmp(list[i + 1], "S_IFDIR")) { my_file->type = S_IFDIR; } } @@ -591,24 +638,27 @@ iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path * @return A pointer to an AFCFile struct containing the information received, * or NULL on failure. */ -iphone_error_t iphone_afc_get_file_attr ( iphone_afc_client_t client, const char *filename, struct stat *stbuf ) { +iphone_error_t iphone_afc_get_file_attr(iphone_afc_client_t client, const char *filename, struct stat * stbuf) +{ iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - if (!client ||!client->connection || !client->afc_packet || !stbuf) return IPHONE_E_INVALID_ARG; + if (!client || !client->connection || !client->afc_packet || !stbuf) + return IPHONE_E_INVALID_ARG; memset(stbuf, 0, sizeof(struct stat)); iphone_afc_file_t file = afc_get_file_info(client, filename); - if (!file){ + if (!file) { ret = IPHONE_E_NO_SUCH_FILE; } else { stbuf->st_mode = file->type | (S_ISDIR(file->type) ? 0755 : 0644); stbuf->st_size = file->size; - stbuf->st_blksize = 2048; // FIXME: Is this the actual block size used on the iPhone? + stbuf->st_blksize = 2048; // FIXME: Is this the actual block + // size used on the iPhone? stbuf->st_blocks = file->blocks; stbuf->st_uid = getuid(); stbuf->st_gid = getgid(); - ret = iphone_afc_close_file(client,file); + ret = iphone_afc_close_file(client, file); } return ret; } @@ -626,32 +676,36 @@ iphone_error_t iphone_afc_get_file_attr ( iphone_afc_client_t client, const char * received by afc_get_file_info) as well as the handle to the file or * NULL in the case of failure. */ -iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *filename, iphone_afc_file_mode_t file_mode, iphone_afc_file_t *file ) { +iphone_error_t +iphone_afc_open_file(iphone_afc_client_t client, const char *filename, + iphone_afc_file_mode_t file_mode, iphone_afc_file_t * file) +{ iphone_afc_file_t file_loc = NULL; uint32 ag = 0; int bytes = 0, length = 0; - char *data = (char*)malloc(sizeof(char) * (8 + strlen(filename) + 1)); - - if (!client ||!client->connection || !client->afc_packet) return IPHONE_E_INVALID_ARG; - + char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); + + if (!client || !client->connection || !client->afc_packet) + return IPHONE_E_INVALID_ARG; + afc_lock(client); - + // Send command memcpy(data, &file_mode, 4); - memcpy(data+4, &ag, 4); - memcpy(data+8, filename, strlen(filename)); - data[8+strlen(filename)] = '\0'; + memcpy(data + 4, &ag, 4); + memcpy(data + 8, filename, strlen(filename)); + data[8 + strlen(filename)] = '\0'; client->afc_packet->operation = AFC_FILE_OPEN; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, data, 8+strlen(filename)); + bytes = dispatch_AFC_packet(client, data, 8 + strlen(filename)); free(data); - + if (bytes <= 0) { - if (debug) fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n"); + if (debug) + fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n"); afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } - // Receive the data length = receive_AFC_data(client, &data); if (length > 0 && data) { @@ -664,13 +718,14 @@ iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *fi *file = file_loc; return IPHONE_E_SUCCESS; } else { - if (debug) fprintf(stderr, "afc_open_file: Didn't get any further data\n"); + if (debug) + fprintf(stderr, "afc_open_file: Didn't get any further data\n"); afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } afc_unlock(client); - + return IPHONE_E_UNKNOWN_ERROR; } @@ -683,59 +738,70 @@ iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *fi * * @return The number of bytes read if successful. If there was an error -1. */ -iphone_error_t iphone_afc_read_file ( iphone_afc_client_t client, iphone_afc_file_t file, char *data, int length, uint32_t *bytes) { +iphone_error_t +iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *data, int length, uint32_t * bytes) +{ char *input = NULL; int current_count = 0, bytes_loc = 0; const int MAXIMUM_READ_SIZE = 1 << 16; - if (!client || !client->afc_packet || !client->connection || !file) return IPHONE_E_INVALID_ARG; - if (debug) fprintf(stderr, "afc_read_file called for length %i\n", length); + if (!client || !client->afc_packet || !client->connection || !file) + return IPHONE_E_INVALID_ARG; + if (debug) + fprintf(stderr, "afc_read_file called for length %i\n", length); afc_lock(client); - // Looping here to get around the maximum amount of data that recieve_AFC_data can handle - while (current_count < length){ - if (debug) fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length); - + // Looping here to get around the maximum amount of data that + // recieve_AFC_data can handle + while (current_count < length) { + if (debug) + fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length); + // Send the read command - AFCFilePacket *packet = (AFCFilePacket*)malloc(sizeof(AFCFilePacket)); + AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); packet->unknown1 = packet->unknown2 = 0; packet->filehandle = file->filehandle; packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; client->afc_packet->operation = AFC_READ; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes_loc = dispatch_AFC_packet(client, (char*)packet, sizeof(AFCFilePacket)); + bytes_loc = dispatch_AFC_packet(client, (char *) packet, sizeof(AFCFilePacket)); free(packet); - + if (bytes_loc <= 0) { afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } - // Receive the data bytes_loc = receive_AFC_data(client, &input); - if (debug) fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc); + if (debug) + fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc); if (bytes_loc < 0) { - if (input) free(input); + if (input) + free(input); afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } else if (bytes_loc == 0) { - if (input) free(input); + if (input) + free(input); afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; //FIXME check that's actually a success + return IPHONE_E_SUCCESS; // FIXME check that's actually a + // success } else { if (input) { - if (debug) fprintf(stderr, "afc_read_file: %d\n", bytes_loc); - memcpy(data+current_count, input, (bytes_loc > length) ? length : bytes_loc); + if (debug) + fprintf(stderr, "afc_read_file: %d\n", bytes_loc); + memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); free(input); input = NULL; current_count += (bytes_loc > length) ? length : bytes_loc; } } } - if (debug) fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count); - + if (debug) + fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count); + afc_unlock(client); *bytes = current_count; return IPHONE_E_SUCCESS; @@ -751,17 +817,22 @@ iphone_error_t iphone_afc_read_file ( iphone_afc_client_t client, iphone_afc_fil * @return The number of bytes written to the file, or a value less than 0 if * none were written... */ -iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_file_t file, const char *data, int length, uint32_t *bytes) { +iphone_error_t +iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file, + const char *data, int length, uint32_t * bytes) +{ char *acknowledgement = NULL; const int MAXIMUM_WRITE_SIZE = 1 << 16; uint32 zero = 0, bytes_loc = 0, segments = (length / MAXIMUM_WRITE_SIZE), current_count = 0, i = 0; char *out_buffer = NULL; - if (!client ||!client->afc_packet || !client->connection || !file || !bytes) return IPHONE_E_INVALID_ARG; - + if (!client || !client->afc_packet || !client->connection || !file || !bytes) + return IPHONE_E_INVALID_ARG; + afc_lock(client); - if (debug) fprintf(stderr, "afc_write_file: Write length: %i\n", length); + if (debug) + fprintf(stderr, "afc_write_file: Write length: %i\n", length); // Divide the file into segments. for (i = 0; i < segments; i++) { @@ -769,10 +840,10 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi client->afc_packet->this_length = sizeof(AFCPacket) + 8; client->afc_packet->entire_length = client->afc_packet->this_length + MAXIMUM_WRITE_SIZE; client->afc_packet->operation = AFC_WRITE; - out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); - memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); - memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); - memcpy(out_buffer+8, data+current_count, MAXIMUM_WRITE_SIZE); + out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); + memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32)); + memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32)); + memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); if (bytes_loc < 0) { afc_unlock(client); @@ -788,39 +859,42 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi return IPHONE_E_NOT_ENOUGH_DATA; } } - - // By this point, we should be at the end. i.e. the last segment that 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 + + // By this point, we should be at the end. i.e. the last segment that + // 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 == length) { afc_unlock(client); *bytes = current_count; return IPHONE_E_SUCCESS; } - + client->afc_packet->this_length = sizeof(AFCPacket) + 8; client->afc_packet->entire_length = client->afc_packet->this_length + (length - current_count); client->afc_packet->operation = AFC_WRITE; - out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); - memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); - memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); - memcpy(out_buffer+8, data+current_count, (length - current_count)); + out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); + memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32)); + memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32)); + memcpy(out_buffer + 8, data + current_count, (length - current_count)); bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8); free(out_buffer); out_buffer = NULL; - + current_count += bytes_loc; - + if (bytes_loc <= 0) { afc_unlock(client); *bytes = current_count; return IPHONE_E_SUCCESS; } - + zero = bytes_loc; bytes_loc = receive_AFC_data(client, &acknowledgement); afc_unlock(client); if (bytes_loc < 0) { - if (debug) fprintf(stderr, "afc_write_file: uh oh?\n"); + if (debug) + fprintf(stderr, "afc_write_file: uh oh?\n"); } *bytes = current_count; return IPHONE_E_SUCCESS; @@ -832,19 +906,22 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi * @param file A pointer to an AFCFile struct containing the file handle of the * file to close. */ -iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_file_t file) { - if (!client || !file) return IPHONE_E_INVALID_ARG; +iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file_t file) +{ + if (!client || !file) + return IPHONE_E_INVALID_ARG; char *buffer = malloc(sizeof(char) * 8); uint32 zero = 0; int bytes = 0; - + afc_lock(client); - - if (debug) fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle); - + + if (debug) + fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle); + // Send command memcpy(buffer, &file->filehandle, sizeof(uint32)); - memcpy(buffer+sizeof(uint32), &zero, sizeof(zero)); + memcpy(buffer + sizeof(uint32), &zero, sizeof(zero)); client->afc_packet->operation = AFC_FILE_CLOSE; client->afc_packet->entire_length = client->afc_packet->this_length = 0; bytes = dispatch_AFC_packet(client, buffer, sizeof(char) * 8); @@ -852,16 +929,17 @@ iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_fi buffer = NULL; // FIXME: Is this necesary? - //client->afc_packet->entire_length = client->afc_packet->this_length = 0; - - if (bytes <= 0) { - afc_unlock(client); + // client->afc_packet->entire_length = client->afc_packet->this_length + // = 0; + + if (bytes <= 0) { + afc_unlock(client); return IPHONE_E_UNKNOWN_ERROR; } - // Receive the response bytes = receive_AFC_data(client, &buffer); - if (buffer) free(buffer); + if (buffer) + free(buffer); free(file); afc_unlock(client); return IPHONE_E_SUCCESS; @@ -876,39 +954,41 @@ iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_fi * * @return IPHONE_E_SUCCESS on success, IPHONE_E_NOT_ENOUGH_DATA on failure. */ -iphone_error_t iphone_afc_seek_file ( iphone_afc_client_t client, iphone_afc_file_t file, int seekpos) { - char *buffer = (char*)malloc(sizeof(char) * 24); +iphone_error_t iphone_afc_seek_file(iphone_afc_client_t client, iphone_afc_file_t file, int seekpos) +{ + char *buffer = (char *) malloc(sizeof(char) * 24); uint32 seekto = 0, bytes = 0, zero = 0; - - if (seekpos < 0) seekpos = file->size - abs(seekpos); + + if (seekpos < 0) + seekpos = file->size - abs(seekpos); afc_lock(client); - + // Send the command seekto = seekpos; - memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle - memcpy(buffer+4, &zero, sizeof(uint32)); // pad - memcpy(buffer+8, &zero, sizeof(uint32)); // fromwhere - memcpy(buffer+12, &zero, sizeof(uint32)); // pad - memcpy(buffer+16, &seekto, sizeof(uint32)); // offset - memcpy(buffer+20, &zero, sizeof(uint32)); // pad + memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle + memcpy(buffer + 4, &zero, sizeof(uint32)); // pad + memcpy(buffer + 8, &zero, sizeof(uint32)); // fromwhere + memcpy(buffer + 12, &zero, sizeof(uint32)); // pad + memcpy(buffer + 16, &seekto, sizeof(uint32)); // offset + memcpy(buffer + 20, &zero, sizeof(uint32)); // pad client->afc_packet->operation = AFC_FILE_SEEK; client->afc_packet->this_length = client->afc_packet->entire_length = 0; bytes = dispatch_AFC_packet(client, buffer, 23); free(buffer); buffer = NULL; - - if (bytes <= 0) { + + if (bytes <= 0) { afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; - } - + } // Receive response bytes = receive_AFC_data(client, &buffer); - if (buffer) free(buffer); - + if (buffer) + free(buffer); + afc_unlock(client); - + if (bytes >= 0) { return IPHONE_E_SUCCESS; } else { @@ -927,17 +1007,18 @@ iphone_error_t iphone_afc_seek_file ( iphone_afc_client_t client, iphone_afc_fil * @note This function is more akin to ftruncate than truncate, and truncate * calls would have to open the file before calling this, sadly. */ -iphone_error_t iphone_afc_truncate_file ( iphone_afc_client_t client, iphone_afc_file_t file, uint32_t newsize) { - char *buffer = (char*)malloc(sizeof(char) * 16); +iphone_error_t iphone_afc_truncate_file(iphone_afc_client_t client, iphone_afc_file_t file, uint32_t newsize) +{ + char *buffer = (char *) malloc(sizeof(char) * 16); uint32 bytes = 0, zero = 0; - + afc_lock(client); - + // Send command - memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle - memcpy(buffer+4, &zero, sizeof(uint32)); // pad - memcpy(buffer+8, &newsize, sizeof(uint32)); // newsize - memcpy(buffer+12, &zero, 3); // pad + memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle + memcpy(buffer + 4, &zero, sizeof(uint32)); // pad + memcpy(buffer + 8, &newsize, sizeof(uint32)); // newsize + memcpy(buffer + 12, &zero, 3); // pad client->afc_packet->operation = AFC_FILE_TRUNCATE; client->afc_packet->this_length = client->afc_packet->entire_length = 0; bytes = dispatch_AFC_packet(client, buffer, 15); @@ -948,13 +1029,13 @@ iphone_error_t iphone_afc_truncate_file ( iphone_afc_client_t client, iphone_afc afc_unlock(client); return IPHONE_E_NOT_ENOUGH_DATA; } - // Receive response bytes = receive_AFC_data(client, &buffer); - if (buffer) free(buffer); - + if (buffer) + free(buffer); + afc_unlock(client); - + if (bytes >= 0) { return IPHONE_E_SUCCESS; } else { diff --git a/src/AFC.h b/src/AFC.h index 7271dfa..b52d693 100644 --- a/src/AFC.h +++ b/src/AFC.h @@ -71,4 +71,3 @@ enum { AFC_READ = 0x0000000f, AFC_WRITE = 0x00000010 }; - diff --git a/src/ifuse.c b/src/ifuse.c index c266879..ad34eb5 100644 --- a/src/ifuse.c +++ b/src/ifuse.c @@ -42,7 +42,8 @@ iphone_lckd_client_t control = NULL; int debug = 0; -static int ifuse_getattr(const char *path, struct stat *stbuf) { +static int ifuse_getattr(const char *path, struct stat *stbuf) +{ int res = 0; iphone_afc_client_t afc = fuse_get_context()->private_data; @@ -54,31 +55,32 @@ static int ifuse_getattr(const char *path, struct stat *stbuf) { return res; } -static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi) { +static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) +{ int i; char **dirs = NULL; iphone_afc_client_t afc = fuse_get_context()->private_data; iphone_afc_get_dir_list(afc, path, &dirs); - if(!dirs) + if (!dirs) return -ENOENT; for (i = 0; dirs[i]; i++) { filler(buf, dirs[i], NULL, 0); } - + free_dictionary(dirs); return 0; } -static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) { +static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) +{ // exactly the same as open but using a different mode iphone_afc_file_t file = NULL; iphone_afc_client_t afc = fuse_get_context()->private_data; - + iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_WRITE, &file); fh_index++; fi->fh = fh_index; @@ -86,11 +88,12 @@ static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi return 0; } -static int ifuse_open(const char *path, struct fuse_file_info *fi) { +static int ifuse_open(const char *path, struct fuse_file_info *fi) +{ iphone_afc_file_t file = NULL; iphone_afc_client_t afc = fuse_get_context()->private_data; uint32_t mode = 0; - + if ((fi->flags & 3) == O_RDWR || (fi->flags & 3) == O_WRONLY) { mode = IPHONE_AFC_FILE_READ; } else if ((fi->flags & 3) == O_RDONLY) { @@ -98,9 +101,9 @@ static int ifuse_open(const char *path, struct fuse_file_info *fi) { } else { mode = IPHONE_AFC_FILE_READ; } - + iphone_afc_open_file(afc, path, mode, &file); - + fh_index++; fi->fh = fh_index; g_hash_table_insert(file_handles, &fh_index, file); @@ -108,8 +111,8 @@ static int ifuse_open(const char *path, struct fuse_file_info *fi) { return 0; } -static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, - struct fuse_file_info *fi) { +static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) +{ int bytes = 0; iphone_afc_file_t file; iphone_afc_client_t afc = fuse_get_context()->private_data; @@ -118,7 +121,7 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, return 0; file = g_hash_table_lookup(file_handles, &(fi->fh)); - if (!file){ + if (!file) { return -ENOENT; } @@ -127,171 +130,195 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, return bytes; } -static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { +static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) +{ int bytes = 0; iphone_afc_file_t file = NULL; iphone_afc_client_t afc = fuse_get_context()->private_data; - - if (size == 0) return 0; - + + if (size == 0) + return 0; + file = g_hash_table_lookup(file_handles, &(fi->fh)); - if (!file) return -ENOENT; - + if (!file) + return -ENOENT; + if (IPHONE_E_SUCCESS == iphone_afc_seek_file(afc, file, offset)) iphone_afc_write_file(afc, file, buf, size, &bytes); return bytes; } -static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) { +static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) +{ return 0; } -static int ifuse_release(const char *path, struct fuse_file_info *fi){ +static int ifuse_release(const char *path, struct fuse_file_info *fi) +{ iphone_afc_file_t file = NULL; iphone_afc_client_t afc = fuse_get_context()->private_data; - + file = g_hash_table_lookup(file_handles, &(fi->fh)); - if (!file){ + if (!file) { return -ENOENT; } iphone_afc_close_file(afc, file); - + g_hash_table_remove(file_handles, &(fi->fh)); return 0; } -void *ifuse_init(struct fuse_conn_info *conn) { +void *ifuse_init(struct fuse_conn_info *conn) +{ int port = 0; iphone_afc_client_t afc = NULL; - + conn->async_read = 0; file_handles = g_hash_table_new(g_int_hash, g_int_equal); iphone_get_device(&phone); - if (!phone){ + if (!phone) { fprintf(stderr, "No iPhone found, is it connected?\n"); return NULL; } - + if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { iphone_free_device(phone); fprintf(stderr, "Something went wrong in the lockdownd client.\n"); return NULL; } - + if (IPHONE_E_SUCCESS == iphone_lckd_start_service(control, "com.apple.afc", &port) && !port) { iphone_lckd_free_client(control); iphone_free_device(phone); fprintf(stderr, "Something went wrong when starting AFC."); - return NULL; + return NULL; } iphone_afc_new_client(phone, 3432, port, &afc); - return afc; + return afc; } -void ifuse_cleanup(void *data) { - iphone_afc_client_t afc = (iphone_afc_client_t )data; +void ifuse_cleanup(void *data) +{ + iphone_afc_client_t afc = (iphone_afc_client_t) data; iphone_afc_free_client(afc); iphone_lckd_free_client(control); iphone_free_device(phone); } -int ifuse_flush(const char *path, struct fuse_file_info *fi) { +int ifuse_flush(const char *path, struct fuse_file_info *fi) +{ return 0; } -int ifuse_statfs(const char *path, struct statvfs *stats) { +int ifuse_statfs(const char *path, struct statvfs *stats) +{ iphone_afc_client_t afc = fuse_get_context()->private_data; char **info_raw = NULL; uint32_t totalspace = 0, freespace = 0, blocksize = 0, i = 0; - + iphone_afc_get_devinfo(afc, &info_raw); - if (!info_raw) return -ENOENT; - + if (!info_raw) + return -ENOENT; + for (i = 0; info_raw[i]; i++) { if (!strcmp(info_raw[i], "FSTotalBytes")) { - totalspace = atoi(info_raw[i+1]); + totalspace = atoi(info_raw[i + 1]); } else if (!strcmp(info_raw[i], "FSFreeBytes")) { - freespace = atoi(info_raw[i+1]); + freespace = atoi(info_raw[i + 1]); } else if (!strcmp(info_raw[i], "FSBlockSize")) { - blocksize = atoi(info_raw[i+1]); + blocksize = atoi(info_raw[i + 1]); } } free_dictionary(info_raw); - + // Now to fill the struct. stats->f_bsize = stats->f_frsize = blocksize; - stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize - stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess. - stats->f_namemax = 255; // blah - stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess + stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize + stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess. + stats->f_namemax = 255; // blah + stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess return 0; } -int ifuse_truncate(const char *path, off_t size) { +int ifuse_truncate(const char *path, off_t size) +{ int result = 0; iphone_afc_client_t afc = fuse_get_context()->private_data; iphone_afc_file_t tfile = NULL; iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_READ, &tfile); - if (!tfile) return -1; - + if (!tfile) + return -1; + result = iphone_afc_truncate_file(afc, tfile, size); iphone_afc_close_file(afc, tfile); return result; } -int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) { +int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) +{ iphone_afc_client_t afc = fuse_get_context()->private_data; iphone_afc_file_t file = g_hash_table_lookup(file_handles, &fi->fh); - if (!file) return -ENOENT; - + if (!file) + return -ENOENT; + return iphone_afc_truncate_file(afc, file, size); } -int ifuse_unlink(const char *path) { +int ifuse_unlink(const char *path) +{ iphone_afc_client_t afc = fuse_get_context()->private_data; - if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path)) return 0; - else return -1; + if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path)) + return 0; + else + return -1; } -int ifuse_rename(const char *from, const char *to) { +int ifuse_rename(const char *from, const char *to) +{ iphone_afc_client_t afc = fuse_get_context()->private_data; - if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to)) return 0; - else return -1; + if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to)) + return 0; + else + return -1; } -int ifuse_mkdir(const char *dir, mode_t ignored) { +int ifuse_mkdir(const char *dir, mode_t ignored) +{ iphone_afc_client_t afc = fuse_get_context()->private_data; - if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir)) return 0; - else return -1; + if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir)) + return 0; + else + return -1; } static struct fuse_operations ifuse_oper = { - .getattr = ifuse_getattr, - .statfs = ifuse_statfs, - .readdir = ifuse_readdir, - .mkdir = ifuse_mkdir, - .rmdir = ifuse_unlink, // AFC uses the same op for both. - .create = ifuse_create, - .open = ifuse_open, - .read = ifuse_read, - .write = ifuse_write, - .truncate = ifuse_truncate, - .ftruncate = ifuse_ftruncate, - .unlink = ifuse_unlink, - .rename = ifuse_rename, - .fsync = ifuse_fsync, - .release = ifuse_release, - .init = ifuse_init, - .destroy = ifuse_cleanup + .getattr = ifuse_getattr, + .statfs = ifuse_statfs, + .readdir = ifuse_readdir, + .mkdir = ifuse_mkdir, + .rmdir = ifuse_unlink, // AFC uses the same op for both. + .create = ifuse_create, + .open = ifuse_open, + .read = ifuse_read, + .write = ifuse_write, + .truncate = ifuse_truncate, + .ftruncate = ifuse_ftruncate, + .unlink = ifuse_unlink, + .rename = ifuse_rename, + .fsync = ifuse_fsync, + .release = ifuse_release, + .init = ifuse_init, + .destroy = ifuse_cleanup }; -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ return fuse_main(argc, argv, &ifuse_oper, NULL); } diff --git a/src/initconf.c b/src/initconf.c index b900f7f..412dd70 100644 --- a/src/initconf.c +++ b/src/initconf.c @@ -36,38 +36,43 @@ int debug = 1; * * @param key The pointer to the desired location of the new key. */ -void generate_key(gpointer key){ - gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t*)key), GNUTLS_PK_RSA, 2048, 0); +void generate_key(gpointer key) +{ + gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t *) key), GNUTLS_PK_RSA, 2048, 0); g_thread_exit(0); } + /** Simple function that generates a spinner until the mutex is released. */ -void progress_bar(gpointer mutex){ +void progress_bar(gpointer mutex) +{ const char *spinner = "|/-\\|/-\\"; int i = 0; - while (!g_static_mutex_trylock((GStaticMutex*)mutex)){ + while (!g_static_mutex_trylock((GStaticMutex *) mutex)) { usleep(500000); printf("Generating key... %c\r", spinner[i++]); fflush(stdout); - if (i > 8) i = 0; + if (i > 8) + i = 0; } printf("Generating key... done\n"); g_thread_exit(0); } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ GThread *progress_thread, *key_thread; GError *err; static GStaticMutex mutex = G_STATIC_MUTEX_INIT; - char* host_id = NULL; + char *host_id = NULL; gnutls_x509_privkey_t root_privkey; gnutls_x509_privkey_t host_privkey; gnutls_x509_crt_t root_cert; gnutls_x509_crt_t host_cert; // Create the thread - if (!g_thread_supported()){ + if (!g_thread_supported()) { g_thread_init(NULL); } gnutls_global_init(); @@ -88,28 +93,28 @@ int main(int argc, char *argv[]) { /* generate root key */ g_static_mutex_lock(&mutex); - if((key_thread = g_thread_create((GThreadFunc)generate_key, &root_privkey, TRUE, &err)) == NULL) { - printf("Thread create failed: %s!!\n", err->message ); - g_error_free(err) ; + if ((key_thread = g_thread_create((GThreadFunc) generate_key, &root_privkey, TRUE, &err)) == NULL) { + printf("Thread create failed: %s!!\n", err->message); + g_error_free(err); } - if((progress_thread = g_thread_create((GThreadFunc)progress_bar, &mutex, TRUE, &err)) == NULL) { - printf("Thread create failed: %s!!\n", err->message ); - g_error_free(err) ; + if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) { + printf("Thread create failed: %s!!\n", err->message); + g_error_free(err); } g_thread_join(key_thread); g_static_mutex_unlock(&mutex); g_thread_join(progress_thread); - + /* generate host key */ g_static_mutex_init(&mutex); g_static_mutex_lock(&mutex); - if((key_thread = g_thread_create((GThreadFunc)generate_key, &host_privkey, TRUE, &err)) == NULL) { - printf("Thread create failed: %s!!\n", err->message ); - g_error_free(err) ; + if ((key_thread = g_thread_create((GThreadFunc) generate_key, &host_privkey, TRUE, &err)) == NULL) { + printf("Thread create failed: %s!!\n", err->message); + g_error_free(err); } - if((progress_thread = g_thread_create((GThreadFunc)progress_bar, &mutex, TRUE, &err)) == NULL) { - printf("Thread create failed: %s!!\n", err->message ); - g_error_free(err) ; + if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) { + printf("Thread create failed: %s!!\n", err->message); + g_error_free(err); } g_thread_join(key_thread); g_static_mutex_unlock(&mutex); @@ -136,33 +141,33 @@ int main(int argc, char *argv[]) { /* export to PEM format */ - gnutls_datum_t root_key_pem = {NULL, 0}; - gnutls_datum_t host_key_pem = {NULL, 0}; + gnutls_datum_t root_key_pem = { NULL, 0 }; + gnutls_datum_t host_key_pem = { NULL, 0 }; - gnutls_x509_privkey_export (root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size); - gnutls_x509_privkey_export (host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size); + gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size); + gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size); root_key_pem.data = gnutls_malloc(root_key_pem.size); host_key_pem.data = gnutls_malloc(host_key_pem.size); - gnutls_x509_privkey_export (root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size); - gnutls_x509_privkey_export (host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size); + gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size); + gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size); - gnutls_datum_t root_cert_pem = {NULL, 0}; - gnutls_datum_t host_cert_pem = {NULL, 0}; + gnutls_datum_t root_cert_pem = { NULL, 0 }; + gnutls_datum_t host_cert_pem = { NULL, 0 }; - gnutls_x509_crt_export (root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size); - gnutls_x509_crt_export (host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size); + gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size); + gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size); root_cert_pem.data = gnutls_malloc(root_cert_pem.size); host_cert_pem.data = gnutls_malloc(host_cert_pem.size); printf("Generating root certificate..."); - gnutls_x509_crt_export (root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size); + gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size); printf("done\n"); printf("Generating host certificate..."); - gnutls_x509_crt_export (host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size); + gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size); printf("done\n"); @@ -176,4 +181,3 @@ int main(int argc, char *argv[]) { return 0; } - diff --git a/src/iphone.c b/src/iphone.c index 68963fe..2c4c541 100644 --- a/src/iphone.c +++ b/src/iphone.c @@ -27,43 +27,42 @@ #include #include -extern int debug; +extern int debug; /** Gets a handle to an iPhone * * @return A structure with data on the first iPhone it finds. (Or NULL, on * error) */ -iphone_error_t iphone_get_device ( iphone_device_t *device ){ +iphone_error_t iphone_get_device(iphone_device_t * device) +{ //check we can actually write in device if (!device || (device && *device)) return IPHONE_E_INVALID_ARG; struct usb_bus *bus, *busses; struct usb_device *dev; - iphone_device_t phone = (iphone_device_t)malloc(sizeof(struct iphone_device_int)); - + iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int)); + // Initialize the struct phone->device = NULL; phone->__device = NULL; - phone->buffer = NULL; - + phone->buffer = NULL; + // Initialize libusb usb_init(); usb_find_busses(); usb_find_devices(); busses = usb_get_busses(); - + // Set the device configuration - for (bus = busses; bus; bus = bus->next) { + for (bus = busses; bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { - if (dev->descriptor.idVendor == 0x05ac && + if (dev->descriptor.idVendor == 0x05ac && (dev->descriptor.idProduct == 0x1290 || - dev->descriptor.idProduct == 0x1291 || - dev->descriptor.idProduct == 0x1292 - ) - ) { + dev->descriptor.idProduct == 0x1291 || dev->descriptor.idProduct == 0x1292) + ) { phone->__device = dev; phone->device = usb_open(phone->__device); usb_set_configuration(phone->device, 3); @@ -71,20 +70,21 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){ break; } } - if (phone->__device && phone->device) break; + if (phone->__device && phone->device) + break; } - + // Check to see if we are connected if (!phone->device || !phone->__device) { iphone_free_device(phone); - if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n"); + if (debug) + fprintf(stderr, "get_iPhone(): iPhone not found\n"); return IPHONE_E_NO_DEVICE; } - // Send the version command to the phone int bytes = 0; usbmux_version_header *version = version_header(); - bytes = usb_bulk_write(phone->device, BULKOUT, (char*)version, sizeof(*version), 800); + bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800); if (bytes < 20 && debug) { fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); if (bytes < 0) { @@ -92,20 +92,20 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){ bytes, usb_strerror(), strerror(-bytes)); } } - // Read the phone's response - bytes = usb_bulk_read(phone->device, BULKIN, (char*)version, sizeof(*version), 800); - + bytes = usb_bulk_read(phone->device, BULKIN, (char *) version, sizeof(*version), 800); + // Check for bad response if (bytes < 20) { free(version); iphone_free_device(phone); - if (debug) fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); - if (debug && bytes < 0) fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", - bytes, usb_strerror(), strerror(-bytes)); + if (debug) + fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); + if (debug && bytes < 0) + fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", + bytes, usb_strerror(), strerror(-bytes)); return IPHONE_E_NOT_ENOUGH_DATA; } - // Check for correct version if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) { // We're all ready to roll. @@ -117,15 +117,17 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){ // Bad header iphone_free_device(phone); free(version); - if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); + if (debug) + fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); return IPHONE_E_BAD_HEADER; } // If it got to this point it's gotta be bad - if (debug) fprintf(stderr, "get_iPhone(): Unknown error.\n"); + if (debug) + fprintf(stderr, "get_iPhone(): Unknown error.\n"); iphone_free_device(phone); free(version); - return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad + return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad } /** Cleans up an iPhone structure, then frees the structure itself. @@ -134,8 +136,10 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){ * * @param phone A pointer to an iPhone structure. */ -iphone_error_t iphone_free_device ( iphone_device_t device ) { - if (!device) return IPHONE_E_INVALID_ARG; +iphone_error_t iphone_free_device(iphone_device_t device) +{ + if (!device) + return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; if (device->buffer) { @@ -150,7 +154,7 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) { free(device); return ret; } - + /** Sends data to the phone * This is a low-level (i.e. directly to phone) function. * @@ -159,22 +163,27 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) { * @param datalen The length of the data * @return The number of bytes sent, or -1 on error or something. */ -int send_to_phone(iphone_device_t phone, char *data, int datalen) { - if (!phone) return -1; +int send_to_phone(iphone_device_t phone, char *data, int datalen) +{ + if (!phone) + return -1; int bytes = 0; - - if (!phone) return -1; - if (debug) fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data); + + if (!phone) + return -1; + if (debug) + fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data); bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); if (bytes < datalen) { - if(debug && bytes < 0) - fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), strerror(-bytes)); + if (debug && bytes < 0) + fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), + strerror(-bytes)); return -1; } else { return bytes; } - + return -1; } @@ -186,18 +195,24 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) { * * @return How many bytes were read in, or -1 on error. */ -int recv_from_phone(iphone_device_t phone, char *data, int datalen) { - if (!phone) return -1; +int recv_from_phone(iphone_device_t phone, char *data, int datalen) +{ + if (!phone) + return -1; int bytes = 0; - - if (!phone) return -1; - if (debug) fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen); - + + if (!phone) + return -1; + if (debug) + fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen); + bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); if (bytes < 0) { - if(debug) fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes)); + if (debug) + fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), + strerror(-bytes)); return -1; } - + return bytes; } diff --git a/src/iphone.h b/src/iphone.h index 556a93a..222a1be 100644 --- a/src/iphone.h +++ b/src/iphone.h @@ -22,7 +22,7 @@ #ifndef IPHONE_H #define IPHONE_H -#ifndef USBMUX_H +#ifndef USBMUX_H #include "usbmux.h" #warning usbmux not included? #endif diff --git a/src/lockdown.c b/src/lockdown.c index e5420a3..80974d2 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -32,16 +32,17 @@ extern int debug; -const ASN1_ARRAY_TYPE pkcs1_asn1_tab[]={ - {"PKCS1",536872976,0}, - {0,1073741836,0}, - {"RSAPublicKey",536870917,0}, - {"modulus",1073741827,0}, - {"publicExponent",3,0}, - {0,0,0} +const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { + {"PKCS1", 536872976, 0}, + {0, 1073741836, 0}, + {"RSAPublicKey", 536870917, 0}, + {"modulus", 1073741827, 0}, + {"publicExponent", 3, 0}, + {0, 0, 0} }; -int get_rand(int min, int max) { +int get_rand(int min, int max) +{ int retval = (rand() % (max - min)) + min; return retval; } @@ -50,21 +51,22 @@ int get_rand(int min, int max) { * * @param A null terminated string containing a valid HostID. */ -char *lockdownd_generate_hostid() { - char *hostid = (char*)malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long +char *lockdownd_generate_hostid() +{ + char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long const char *chars = "ABCDEF0123456789"; srand(time(NULL)); int i = 0; - + for (i = 0; i < 36; i++) { if (i == 8 || i == 13 || i == 18 || i == 23) { hostid[i] = '-'; continue; } else { - hostid[i] = chars[get_rand(0,16)]; + hostid[i] = chars[get_rand(0, 16)]; } } - hostid[36] = '\0'; // make it a real string + hostid[36] = '\0'; // make it a real string return hostid; } @@ -74,16 +76,18 @@ char *lockdownd_generate_hostid() { * * @return The lockdownd client. */ -iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) { - if (!phone) return NULL; - iphone_lckd_client_t control = (iphone_lckd_client_t)malloc(sizeof(struct iphone_lckd_client_int)); +iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) +{ + if (!phone) + return NULL; + iphone_lckd_client_t control = (iphone_lckd_client_t) malloc(sizeof(struct iphone_lckd_client_int)); - if (IPHONE_E_SUCCESS != iphone_mux_new_client ( phone, 0x0a00, 0xf27e, &control->connection)) { + if (IPHONE_E_SUCCESS != iphone_mux_new_client(phone, 0x0a00, 0xf27e, &control->connection)) { free(control); return NULL; } - - control->ssl_session = (gnutls_session_t*)malloc(sizeof(gnutls_session_t)); + + control->ssl_session = (gnutls_session_t *) malloc(sizeof(gnutls_session_t)); control->in_SSL = 0; control->gtls_buffer_hack_len = 0; return control; @@ -94,15 +98,18 @@ iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) { * * @param control The lockdown client */ -iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client ) { - if (!client) return IPHONE_E_INVALID_ARG; +iphone_error_t iphone_lckd_free_client(iphone_lckd_client_t client) +{ + if (!client) + return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; if (client->connection) { ret = iphone_mux_free_client(client->connection); } - if (client->ssl_session) gnutls_deinit(*client->ssl_session); + if (client->ssl_session) + gnutls_deinit(*client->ssl_session); free(client->ssl_session); free(client); return ret; @@ -116,24 +123,30 @@ iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client ) { * * @return The number of bytes received */ -iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, char **dump_data, uint32_t *recv_bytes ) { - if (!client || !dump_data || !recv_bytes) return IPHONE_E_INVALID_ARG; +iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, uint32_t * recv_bytes) +{ + if (!client || !dump_data || !recv_bytes) + return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; char *receive; uint32 datalen = 0, bytes = 0; - - if (!client->in_SSL) ret = iphone_mux_recv(client->connection, (char *)&datalen, sizeof(datalen), &bytes); + + if (!client->in_SSL) + ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes); else { bytes = gnutls_record_recv(*client->ssl_session, &datalen, sizeof(datalen)); - if (bytes > 0) ret = IPHONE_E_SUCCESS; + if (bytes > 0) + ret = IPHONE_E_SUCCESS; } datalen = ntohl(datalen); - - receive = (char*)malloc(sizeof(char) * datalen); - if (!client->in_SSL) ret = iphone_mux_recv(client->connection, receive, datalen, &bytes); + + receive = (char *) malloc(sizeof(char) * datalen); + if (!client->in_SSL) + ret = iphone_mux_recv(client->connection, receive, datalen, &bytes); else { bytes = gnutls_record_recv(*client->ssl_session, receive, datalen); - if (bytes > 0) ret = IPHONE_E_SUCCESS; + if (bytes > 0) + ret = IPHONE_E_SUCCESS; } *dump_data = receive; *recv_bytes = bytes; @@ -151,30 +164,34 @@ iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, char **dump_data, * * @return The number of bytes sent */ -iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t *sent_bytes ) { - if (!client || !raw_data || length == 0 || !sent_bytes) return IPHONE_E_INVALID_ARG; +iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t * sent_bytes) +{ + if (!client || !raw_data || length == 0 || !sent_bytes) + return IPHONE_E_INVALID_ARG; char *real_query; int bytes; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - real_query = (char*)malloc(sizeof(char) * (length+4)); + real_query = (char *) malloc(sizeof(char) * (length + 4)); length = htonl(length); memcpy(real_query, &length, sizeof(length)); - memcpy(real_query+4, raw_data, ntohl(length)); + memcpy(real_query + 4, raw_data, ntohl(length)); if (debug) { printf("lockdownd_send(): made the query, sending it along\n"); FILE *packet = fopen("grpkt", "w"); - fwrite(real_query, 1, ntohl(length)+4, packet); + fwrite(real_query, 1, ntohl(length) + 4, packet); fclose(packet); packet = NULL; } - - if (!client->in_SSL) ret = iphone_mux_send(client->connection, real_query, ntohl(length)+sizeof(length), &bytes); + + if (!client->in_SSL) + ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); else { - gnutls_record_send(*client->ssl_session, real_query, ntohl(length)+sizeof(length)); + gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); ret = IPHONE_E_SUCCESS; } - if (debug) printf("lockdownd_send(): sent it!\n"); + if (debug) + printf("lockdownd_send(): sent it!\n"); free(real_query); *sent_bytes = bytes; return ret; @@ -188,46 +205,54 @@ iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, char *raw_data, u * * @return 1 on success and 0 on failure. */ -iphone_error_t lockdownd_hello(iphone_lckd_client_t control) { - if (!control) return IPHONE_E_INVALID_ARG; +iphone_error_t lockdownd_hello(iphone_lckd_client_t control) +{ + if (!control) + return IPHONE_E_INVALID_ARG; xmlDocPtr plist = new_plist(); xmlNode *dict, *key; char **dictionary; int bytes = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - - if (debug) printf("lockdownd_hello() called\n"); + + if (debug) + printf("lockdownd_hello() called\n"); dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); char *XML_content; uint32 length; - - xmlDocDumpMemory(plist, (xmlChar **)&XML_content, &length); + + xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); ret = iphone_lckd_send(control, XML_content, length, &bytes); - + xmlFree(XML_content); - xmlFreeDoc(plist); plist = NULL; + xmlFreeDoc(plist); + plist = NULL; ret = iphone_lckd_recv(control, &XML_content, &bytes); plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); - if (!plist) return IPHONE_E_PLIST_ERROR; + if (!plist) + return IPHONE_E_PLIST_ERROR; dict = xmlDocGetRootElement(plist); for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) break; + if (!xmlStrcmp(dict->name, "dict")) + break; } - if (!dict) return IPHONE_E_DICT_ERROR; + if (!dict) + return IPHONE_E_DICT_ERROR; dictionary = read_dict_element_strings(dict); xmlFreeDoc(plist); - free(XML_content); - - for (i = 0; dictionary[i]; i+=2) { - if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i+1], "Success")) { - if (debug) printf("lockdownd_hello(): success\n"); + free(XML_content); + + for (i = 0; dictionary[i]; i += 2) { + if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { + if (debug) + printf("lockdownd_hello(): success\n"); ret = IPHONE_E_SUCCESS; break; } } - + free_dictionary(dictionary); return ret; } @@ -242,7 +267,8 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) { */ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *req_key, char **value) { - if (!control || !req_key || !value || (value && *value)) return IPHONE_E_INVALID_ARG; + if (!control || !req_key || !value || (value && *value)) + return IPHONE_E_INVALID_ARG; xmlDocPtr plist = new_plist(); xmlNode *dict = NULL; xmlNode *key = NULL;; @@ -251,54 +277,61 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r char *XML_content = NULL; uint32 length = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - + /* Setup DevicePublicKey request plist */ dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); key = add_key_str_dict_element(plist, dict, "Key", req_key, 1); key = add_key_str_dict_element(plist, dict, "Request", "GetValue", 1); - xmlDocDumpMemory(plist, (xmlChar**)&XML_content, &length); + xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); /* send to iPhone */ ret = iphone_lckd_send(control, XML_content, length, &bytes); - + xmlFree(XML_content); - xmlFreeDoc(plist); plist = NULL; + xmlFreeDoc(plist); + plist = NULL; - if (ret != IPHONE_E_SUCCESS) return ret; + if (ret != IPHONE_E_SUCCESS) + return ret; /* Now get iPhone's answer */ ret = iphone_lckd_recv(control, &XML_content, &bytes); - if (ret != IPHONE_E_SUCCESS) return ret; + if (ret != IPHONE_E_SUCCESS) + return ret; plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); - if (!plist) return IPHONE_E_PLIST_ERROR; + if (!plist) + return IPHONE_E_PLIST_ERROR; dict = xmlDocGetRootElement(plist); for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) break; + if (!xmlStrcmp(dict->name, "dict")) + break; } - if (!dict) return IPHONE_E_DICT_ERROR; - + if (!dict) + return IPHONE_E_DICT_ERROR; + /* Parse xml to check success and to find public key */ dictionary = read_dict_element_strings(dict); xmlFreeDoc(plist); - free(XML_content); - + free(XML_content); + int success = 0; - for (i = 0; dictionary[i]; i+=2) { - if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i+1], "Success")) { + for (i = 0; dictionary[i]; i += 2) { + if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { success = 1; } if (!strcmp(dictionary[i], "Value")) { - *value = strdup(dictionary[i+1]); + *value = strdup(dictionary[i + 1]); } } - + if (dictionary) { free_dictionary(dictionary); dictionary = NULL; } - if (success) ret = IPHONE_E_SUCCESS; + if (success) + ret = IPHONE_E_SUCCESS; return ret; } @@ -331,15 +364,15 @@ iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, cha * * @return 1 on success and 0 on failure */ -iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_client_t *client ) +iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client_t * client) { - if (!device || !client || (client && *client) ) + if (!device || !client || (client && *client)) return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_SUCCESS; char *host_id = NULL; - iphone_lckd_client_t client_loc = new_lockdownd_client( device ); - if (IPHONE_E_SUCCESS != lockdownd_hello(client_loc)){ + iphone_lckd_client_t client_loc = new_lockdownd_client(device); + if (IPHONE_E_SUCCESS != lockdownd_hello(client_loc)) { fprintf(stderr, "Hello failed in the lockdownd client.\n"); ret = IPHONE_E_NOT_ENOUGH_DATA; } @@ -347,12 +380,12 @@ iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_clie char *uid = NULL; ret = lockdownd_get_device_uid(client_loc, &uid); - if(IPHONE_E_SUCCESS != ret){ + if (IPHONE_E_SUCCESS != ret) { fprintf(stderr, "Device refused to send uid.\n"); } host_id = get_host_id(); - if (IPHONE_E_SUCCESS == ret && !host_id){ + if (IPHONE_E_SUCCESS == ret && !host_id) { fprintf(stderr, "No HostID found, run libiphone-initconf.\n"); ret = IPHONE_E_INVALID_CONF; } @@ -366,7 +399,7 @@ iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_clie } ret = lockdownd_start_SSL_session(client_loc, host_id); - if (IPHONE_E_SUCCESS != ret ) { + if (IPHONE_E_SUCCESS != ret) { ret = IPHONE_E_SSL_ERROR; fprintf(stderr, "SSL Session opening failed.\n"); } @@ -399,19 +432,19 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch char *XML_content = NULL; uint32 length = 0; - char* device_cert_b64 = NULL; - char* host_cert_b64 = NULL; - char* root_cert_b64 = NULL; + char *device_cert_b64 = NULL; + char *host_cert_b64 = NULL; + char *root_cert_b64 = NULL; char *public_key_b64 = NULL; ret = lockdownd_get_device_public_key(control, &public_key_b64); - if(ret != IPHONE_E_SUCCESS){ + if (ret != IPHONE_E_SUCCESS) { fprintf(stderr, "Device refused to send public key.\n"); return ret; } ret = lockdownd_gen_pair_cert(public_key_b64, &device_cert_b64, &host_cert_b64, &root_cert_b64); - if(ret != IPHONE_E_SUCCESS){ + if (ret != IPHONE_E_SUCCESS) { free(public_key_b64); return ret; } @@ -426,51 +459,55 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch add_key_data_dict_element(plist, dictRecord, "RootCertificate", root_cert_b64, 2); add_key_str_dict_element(plist, dict, "Request", "Pair", 1); - xmlDocDumpMemory(plist, (xmlChar**)&XML_content, &length); + xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); - printf("XML Pairing request : %s\n",XML_content); + printf("XML Pairing request : %s\n", XML_content); /* send to iPhone */ ret = iphone_lckd_send(control, XML_content, length, &bytes); - + xmlFree(XML_content); - xmlFreeDoc(plist); plist = NULL; + xmlFreeDoc(plist); + plist = NULL; - if (ret != IPHONE_E_SUCCESS) return ret; + if (ret != IPHONE_E_SUCCESS) + return ret; /* Now get iPhone's answer */ ret = iphone_lckd_recv(control, &XML_content, &bytes); - if (ret != IPHONE_E_SUCCESS) return ret; + if (ret != IPHONE_E_SUCCESS) + return ret; if (debug) { printf("lockdown_pair_device: iPhone's response to our pair request:\n"); fwrite(XML_content, 1, bytes, stdout); printf("\n\n"); } - + plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); if (!plist) { free(public_key_b64); return IPHONE_E_PLIST_ERROR; - } + } dict = xmlDocGetRootElement(plist); for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) break; + if (!xmlStrcmp(dict->name, "dict")) + break; } if (!dict) { free(public_key_b64); return IPHONE_E_DICT_ERROR; } - + /* Parse xml to check success and to find public key */ dictionary = read_dict_element_strings(dict); xmlFreeDoc(plist); - free(XML_content); - + free(XML_content); + int success = 0; - for (i = 0; dictionary[i]; i+=2) { - if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i+1], "Success")) { + for (i = 0; dictionary[i]; i += 2) { + if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { success = 1; } } @@ -482,11 +519,13 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch /* store public key in config if pairing succeeded */ if (success) { - if (debug) printf("lockdownd_pair_device: pair success\n"); + if (debug) + printf("lockdownd_pair_device: pair success\n"); store_device_public_key(uid, public_key_b64); ret = IPHONE_E_SUCCESS; } else { - if (debug) printf("lockdownd_pair_device: pair failure\n"); + if (debug) + printf("lockdownd_pair_device: pair failure\n"); ret = IPHONE_E_PAIRING_FAILED; } free(public_key_b64); @@ -498,24 +537,26 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch * * @return IPHONE_E_SUCCESS on success. */ -iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, char **root_cert_b64) +iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, + char **root_cert_b64) { - if (!public_key_b64 || !device_cert_b64 || !host_cert_b64 || !root_cert_b64) return IPHONE_E_INVALID_ARG; + if (!public_key_b64 || !device_cert_b64 || !host_cert_b64 || !root_cert_b64) + return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - gnutls_datum_t modulus = {NULL, 0}; - gnutls_datum_t exponent = {NULL, 0}; + gnutls_datum_t modulus = { NULL, 0 }; + gnutls_datum_t exponent = { NULL, 0 }; /* first decode base64 public_key */ gnutls_datum_t pem_pub_key; gsize decoded_size; - pem_pub_key.data = g_base64_decode (public_key_b64, &decoded_size); + pem_pub_key.data = g_base64_decode(public_key_b64, &decoded_size); pem_pub_key.size = decoded_size; /* now decode the PEM encoded key */ gnutls_datum_t der_pub_key; - if( GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc ("RSA PUBLIC KEY", &pem_pub_key, &der_pub_key) ){ - + if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &pem_pub_key, &der_pub_key)) { + /* initalize asn.1 parser */ ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY; if (ASN1_SUCCESS == asn1_array2tree(pkcs1_asn1_tab, &pkcs1, NULL)) { @@ -526,14 +567,14 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ if (ASN1_SUCCESS == asn1_der_decoding(&asn1_pub_key, der_pub_key.data, der_pub_key.size, NULL)) { /* get size to read */ - int ret1 = asn1_read_value (asn1_pub_key, "modulus", NULL, &modulus.size); - int ret2 = asn1_read_value (asn1_pub_key, "publicExponent", NULL, &exponent.size); + int ret1 = asn1_read_value(asn1_pub_key, "modulus", NULL, &modulus.size); + int ret2 = asn1_read_value(asn1_pub_key, "publicExponent", NULL, &exponent.size); modulus.data = gnutls_malloc(modulus.size); exponent.data = gnutls_malloc(exponent.size); - ret1 = asn1_read_value (asn1_pub_key, "modulus", modulus.data, &modulus.size); - ret2 = asn1_read_value (asn1_pub_key, "publicExponent", exponent.data, &exponent.size); + ret1 = asn1_read_value(asn1_pub_key, "modulus", modulus.data, &modulus.size); + ret2 = asn1_read_value(asn1_pub_key, "publicExponent", exponent.data, &exponent.size); if (ASN1_SUCCESS == ret1 && ASN1_SUCCESS == ret2) ret = IPHONE_E_SUCCESS; } @@ -548,34 +589,36 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ if (IPHONE_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) { gnutls_global_init(); - gnutls_datum_t essentially_null = {strdup("abababababababab"), strlen("abababababababab")}; - + gnutls_datum_t essentially_null = { strdup("abababababababab"), strlen("abababababababab") }; + gnutls_x509_privkey_t fake_privkey, root_privkey; gnutls_x509_crt_t dev_cert, root_cert, host_cert; - + gnutls_x509_privkey_init(&fake_privkey); gnutls_x509_crt_init(&dev_cert); gnutls_x509_crt_init(&root_cert); gnutls_x509_crt_init(&host_cert); - if ( GNUTLS_E_SUCCESS == gnutls_x509_privkey_import_rsa_raw(fake_privkey, &modulus, &exponent, &essentially_null, &essentially_null, &essentially_null, &essentially_null) ) { - + if (GNUTLS_E_SUCCESS == + gnutls_x509_privkey_import_rsa_raw(fake_privkey, &modulus, &exponent, &essentially_null, &essentially_null, + &essentially_null, &essentially_null)) { + gnutls_x509_privkey_init(&root_privkey); - + /* get root cert */ - gnutls_datum_t pem_root_cert = {NULL, 0}; + gnutls_datum_t pem_root_cert = { NULL, 0 }; get_root_certificate(&pem_root_cert); if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(root_cert, &pem_root_cert, GNUTLS_X509_FMT_PEM)) ret = IPHONE_E_SSL_ERROR; /* get host cert */ - gnutls_datum_t pem_host_cert = {NULL, 0}; + gnutls_datum_t pem_host_cert = { NULL, 0 }; get_host_certificate(&pem_host_cert); if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(host_cert, &pem_host_cert, GNUTLS_X509_FMT_PEM)) ret = IPHONE_E_SSL_ERROR; /* get root private key */ - gnutls_datum_t pem_root_priv = {NULL, 0}; + gnutls_datum_t pem_root_priv = { NULL, 0 }; get_root_private_key(&pem_root_priv); if (GNUTLS_E_SUCCESS != gnutls_x509_privkey_import(root_privkey, &pem_root_priv, GNUTLS_X509_FMT_PEM)) ret = IPHONE_E_SSL_ERROR; @@ -591,7 +634,7 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ if (IPHONE_E_SUCCESS == ret) { /* if everything went well, export in PEM format */ - gnutls_datum_t dev_pem = {NULL, 0}; + gnutls_datum_t dev_pem = { NULL, 0 }; gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size); dev_pem.data = gnutls_malloc(dev_pem.size); gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); @@ -623,7 +666,8 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ * * @return 1 on success and 0 on failure */ -iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) { +iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) +{ xmlDocPtr plist = new_plist(); xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); xmlNode *key; @@ -631,46 +675,52 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c uint32 len = 0, bytes = 0, return_me = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; // end variables - + key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); if (!key) { - if (debug) printf("Couldn't add a key.\n"); + if (debug) + printf("Couldn't add a key.\n"); xmlFreeDoc(plist); return IPHONE_E_DICT_ERROR; } key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1); if (!key) { - if (debug) printf("Couldn't add a key.\n"); + if (debug) + printf("Couldn't add a key.\n"); xmlFreeDoc(plist); return IPHONE_E_DICT_ERROR; } - - xmlDocDumpMemory(plist, (xmlChar **)&what2send, &len); + + xmlDocDumpMemory(plist, (xmlChar **) & what2send, &len); ret = iphone_lckd_send(control, what2send, len, &bytes); - + xmlFree(what2send); xmlFreeDoc(plist); - if (ret != IPHONE_E_SUCCESS) return ret; - + if (ret != IPHONE_E_SUCCESS) + return ret; + if (bytes > 0) { ret = iphone_lckd_recv(control, &what2send, &len); plist = xmlReadMemory(what2send, len, NULL, NULL, 0); dict = xmlDocGetRootElement(plist); - if (!dict) return IPHONE_E_DICT_ERROR; + if (!dict) + return IPHONE_E_DICT_ERROR; for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) break; + if (!xmlStrcmp(dict->name, "dict")) + break; } dictionary = read_dict_element_strings(dict); xmlFreeDoc(plist); free(what2send); - for (i = 0; dictionary[i]; i+=2) { - if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i+1], "Success")) { + for (i = 0; dictionary[i]; i += 2) { + if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { // Set up GnuTLS... //gnutls_anon_client_credentials_t anoncred; gnutls_certificate_credentials_t xcred; - if (debug) printf("We started the session OK, now trying GnuTLS\n"); + if (debug) + printf("We started the session OK, now trying GnuTLS\n"); errno = 0; gnutls_global_init(); //gnutls_anon_allocate_client_credentials(&anoncred); @@ -678,7 +728,7 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); gnutls_init(control->ssl_session, GNUTLS_CLIENT); { - int protocol_priority[16] = {GNUTLS_SSL3, 0 }; + int protocol_priority[16] = { GNUTLS_SSL3, 0 }; int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; @@ -687,30 +737,38 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); gnutls_compression_set_priority(*control->ssl_session, comp_priority); gnutls_kx_set_priority(*control->ssl_session, kx_priority); - gnutls_protocol_set_priority( *control->ssl_session, protocol_priority); + gnutls_protocol_set_priority(*control->ssl_session, protocol_priority); gnutls_mac_set_priority(*control->ssl_session, mac_priority); } - gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. - - if (debug) printf("GnuTLS step 1...\n"); + gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. + + if (debug) + printf("GnuTLS step 1...\n"); gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); - if (debug) printf("GnuTLS step 2...\n"); - gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func)&lockdownd_secuwrite); - if (debug) printf("GnuTLS step 3...\n"); - gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func)&lockdownd_securead); - if (debug) printf("GnuTLS step 4 -- now handshaking...\n"); - - if (errno && debug) printf("WARN: errno says %s before handshake!\n", strerror(errno)); + if (debug) + printf("GnuTLS step 2...\n"); + gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); + if (debug) + printf("GnuTLS step 3...\n"); + gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); + if (debug) + printf("GnuTLS step 4 -- now handshaking...\n"); + + if (errno && debug) + printf("WARN: errno says %s before handshake!\n", strerror(errno)); return_me = gnutls_handshake(*control->ssl_session); - if (debug) printf("GnuTLS handshake done...\n"); - + if (debug) + printf("GnuTLS handshake done...\n"); + free_dictionary(dictionary); if (return_me != GNUTLS_E_SUCCESS) { - if (debug) printf("GnuTLS reported something wrong.\n"); + if (debug) + printf("GnuTLS reported something wrong.\n"); gnutls_perror(return_me); - if (debug) printf("oh.. errno says %s\n", strerror(errno)); + if (debug) + printf("oh.. errno says %s\n", strerror(errno)); return IPHONE_E_SSL_ERROR; } else { control->in_SSL = 1; @@ -718,19 +776,20 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c } } } - + if (debug) { printf("Apparently failed negotiating with lockdownd.\n"); printf("Responding dictionary: \n"); - for (i = 0; dictionary[i]; i+=2) { - printf("\t%s: %s\n", dictionary[i], dictionary[i+1]); + for (i = 0; dictionary[i]; i += 2) { + printf("\t%s: %s\n", dictionary[i], dictionary[i + 1]); } } - + free_dictionary(dictionary); return IPHONE_E_SSL_ERROR; - } else { - if (debug) printf("Didn't get enough bytes.\n"); + } else { + if (debug) + printf("Didn't get enough bytes.\n"); return IPHONE_E_NOT_ENOUGH_DATA; } } @@ -743,14 +802,18 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c * * @return The number of bytes sent */ -ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length) { +ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length) +{ int bytes = 0; iphone_lckd_client_t control; - control = (iphone_lckd_client_t)transport; - if (debug) printf("lockdownd_secuwrite() called\n"); - if (debug) printf("pre-send\nlength = %zi\n", length); + control = (iphone_lckd_client_t) transport; + if (debug) + printf("lockdownd_secuwrite() called\n"); + if (debug) + printf("pre-send\nlength = %zi\n", length); iphone_mux_send(control->connection, buffer, length, &bytes); - if (debug) printf("post-send\nsent %i bytes\n", bytes); + if (debug) + printf("post-send\nsent %i bytes\n", bytes); if (debug) { FILE *my_ssl_packet = fopen("sslpacketwrite.out", "w+"); fwrite(buffer, 1, length, my_ssl_packet); @@ -758,7 +821,7 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size printf("Wrote SSL packet to drive, too.\n"); fclose(my_ssl_packet); } - + return bytes; } @@ -770,68 +833,86 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size * * @return The number of bytes read */ -ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length) { +ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length) +{ int bytes = 0, pos_start_fill = 0; - char *hackhackhack = NULL; + char *hackhackhack = NULL; iphone_lckd_client_t control; - control = (iphone_lckd_client_t)transport; - if (debug) printf("lockdownd_securead() called\nlength = %zi\n", length); + control = (iphone_lckd_client_t) transport; + if (debug) + printf("lockdownd_securead() called\nlength = %zi\n", length); // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. if (control->gtls_buffer_hack_len > 0) { - if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got - length -= control->gtls_buffer_hack_len; // Subtract what we have from their requested length - pos_start_fill = control->gtls_buffer_hack_len; // set the pos to start filling at - memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially - free(control->gtls_buffer_hack); // free our memory, it's not chained anymore - control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore - if (debug) printf("Did a partial fill to help quench thirst for data\n"); - } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less... - control->gtls_buffer_hack_len -= length; // subtract what they're asking for - memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer - hackhackhack = (char*)malloc(sizeof(char) * control->gtls_buffer_hack_len); // strndup is NOT a good solution -- concatenates \0!!!! Anyway, make a new "hack" buffer. - memcpy(hackhackhack, control->gtls_buffer_hack+length, control->gtls_buffer_hack_len); // Move what's left into the new one - free(control->gtls_buffer_hack); // Free the old one - control->gtls_buffer_hack = hackhackhack; // And make it the new one. - hackhackhack = NULL; - if (debug) printf("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len); - return length; // hand it over. - } else { // length == hack length - memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs - free(control->gtls_buffer_hack); // free our "obligation" - control->gtls_buffer_hack_len = 0; // free our "obligation" - if (debug) printf("Satiated the thirst for data; now we have to eventually receive again.\n"); - return length; // hand it over + if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got + length -= control->gtls_buffer_hack_len; // Subtract what we have from their requested length + pos_start_fill = control->gtls_buffer_hack_len; // set the pos to start filling at + memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially + free(control->gtls_buffer_hack); // free our memory, it's not chained anymore + control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore + if (debug) + printf("Did a partial fill to help quench thirst for data\n"); + } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less... + control->gtls_buffer_hack_len -= length; // subtract what they're asking for + memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer + hackhackhack = (char *) malloc(sizeof(char) * control->gtls_buffer_hack_len); // strndup is NOT a good solution -- concatenates \0!!!! Anyway, make a new "hack" buffer. + memcpy(hackhackhack, control->gtls_buffer_hack + length, control->gtls_buffer_hack_len); // Move what's left into the new one + free(control->gtls_buffer_hack); // Free the old one + control->gtls_buffer_hack = hackhackhack; // And make it the new one. + hackhackhack = NULL; + if (debug) + printf("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len); + return length; // hand it over. + } else { // length == hack length + memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs + free(control->gtls_buffer_hack); // free our "obligation" + control->gtls_buffer_hack_len = 0; // free our "obligation" + if (debug) + printf("Satiated the thirst for data; now we have to eventually receive again.\n"); + return length; // hand it over } } // End buffering hack! - char *recv_buffer = (char*)malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens - - if (debug) printf("pre-read\nclient wants %zi bytes\n", length); + char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens + + if (debug) + printf("pre-read\nclient wants %zi bytes\n", length); iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes); - if (debug) printf("post-read\nwe got %i bytes\n", bytes); + if (debug) + printf("post-read\nwe got %i bytes\n", bytes); if (debug && bytes < 0) { printf("lockdownd_securead(): uh oh\n"); - printf("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", usb_strerror(), strerror(errno)); - return bytes + 28; // an errno + printf("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", + usb_strerror(), strerror(errno)); + return bytes + 28; // an errno } if (bytes >= length) { if (bytes > length) { - if (debug) printf("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); - if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet + if (debug) + printf + ("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); + if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution! - control->gtls_buffer_hack_len += bytes-length; - control->gtls_buffer_hack = (char*)malloc(sizeof(char) * control->gtls_buffer_hack_len); - memcpy(control->gtls_buffer_hack, recv_buffer+length, control->gtls_buffer_hack_len); - } else { // if there is. - control->gtls_buffer_hack = realloc(control->gtls_buffer_hack, control->gtls_buffer_hack_len + (bytes - length)); - memcpy(control->gtls_buffer_hack+control->gtls_buffer_hack_len, recv_buffer+length, bytes-length); + control->gtls_buffer_hack_len += bytes - length; + control->gtls_buffer_hack = (char *) malloc(sizeof(char) * control->gtls_buffer_hack_len); + memcpy(control->gtls_buffer_hack, recv_buffer + length, control->gtls_buffer_hack_len); + } else { // if there is. + control->gtls_buffer_hack = + realloc(control->gtls_buffer_hack, control->gtls_buffer_hack_len + (bytes - length)); + memcpy(control->gtls_buffer_hack + control->gtls_buffer_hack_len, recv_buffer + length, bytes - length); control->gtls_buffer_hack_len += bytes - length; } } - memcpy(buffer+pos_start_fill, recv_buffer, length); + memcpy(buffer + pos_start_fill, recv_buffer, length); free(recv_buffer); - if (bytes == length) { if (debug) printf("Returning how much we received.\n"); return bytes; } - else { if (debug) printf("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len); return length; } + if (bytes == length) { + if (debug) + printf("Returning how much we received.\n"); + return bytes; + } else { + if (debug) + printf("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len); + return length; + } } return bytes; } @@ -843,12 +924,16 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ * * @return The port number the service was started on or 0 on failure. */ -iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const char *service, int *port ) { - if (!client || !service || !port) return IPHONE_E_INVALID_ARG; +iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char *service, int *port) +{ + if (!client || !service || !port) + return IPHONE_E_INVALID_ARG; - char* host_id = get_host_id(); - if (!host_id) return IPHONE_E_INVALID_CONF; - if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) return IPHONE_E_SSL_ERROR; + char *host_id = get_host_id(); + if (!host_id) + return IPHONE_E_INVALID_CONF; + if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) + return IPHONE_E_SSL_ERROR; char *XML_query, **dictionary; uint32 length, i = 0, port_loc = 0, bytes = 0; @@ -862,63 +947,78 @@ iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const ch xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); xmlNode *key; key = add_key_str_dict_element(plist, dict, "Request", "StartService", 1); - if (!key) { xmlFreeDoc(plist); return IPHONE_E_UNKNOWN_ERROR; } + if (!key) { + xmlFreeDoc(plist); + return IPHONE_E_UNKNOWN_ERROR; + } key = add_key_str_dict_element(plist, dict, "Service", service, 1); - if (!key) { xmlFreeDoc(plist); return IPHONE_E_UNKNOWN_ERROR; } - - xmlDocDumpMemory(plist, (xmlChar **)&XML_query, &length); - + if (!key) { + xmlFreeDoc(plist); + return IPHONE_E_UNKNOWN_ERROR; + } + + xmlDocDumpMemory(plist, (xmlChar **) & XML_query, &length); + ret = iphone_lckd_send(client, XML_query, length, &bytes); free(XML_query); - if (IPHONE_E_SUCCESS != ret) return ret; - + if (IPHONE_E_SUCCESS != ret) + return ret; + ret = iphone_lckd_recv(client, &XML_query, &bytes); xmlFreeDoc(plist); - if (IPHONE_E_SUCCESS != ret) return ret; - - if (bytes <= 0) return IPHONE_E_NOT_ENOUGH_DATA; + if (IPHONE_E_SUCCESS != ret) + return ret; + + if (bytes <= 0) + return IPHONE_E_NOT_ENOUGH_DATA; else { plist = xmlReadMemory(XML_query, bytes, NULL, NULL, 0); - if (!plist) return IPHONE_E_UNKNOWN_ERROR; + if (!plist) + return IPHONE_E_UNKNOWN_ERROR; dict = xmlDocGetRootElement(plist); - if (!dict) return IPHONE_E_UNKNOWN_ERROR; + if (!dict) + return IPHONE_E_UNKNOWN_ERROR; for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) break; + if (!xmlStrcmp(dict->name, "dict")) + break; } - - if (!dict) return IPHONE_E_UNKNOWN_ERROR; + + if (!dict) + return IPHONE_E_UNKNOWN_ERROR; dictionary = read_dict_element_strings(dict); - - for (i = 0; dictionary[i]; i+=2) { - if (debug) printf("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i+1]); - + + for (i = 0; dictionary[i]; i += 2) { + if (debug) + printf("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]); + if (!xmlStrcmp(dictionary[i], "Port")) { - port_loc = atoi(dictionary[i+1]); - if (debug) printf("lockdownd_start_service() atoi'd port: %i\n", port); + port_loc = atoi(dictionary[i + 1]); + if (debug) + printf("lockdownd_start_service() atoi'd port: %i\n", port); } - + if (!xmlStrcmp(dictionary[i], "Result")) { - if (!xmlStrcmp(dictionary[i+1], "Success")) { + if (!xmlStrcmp(dictionary[i + 1], "Success")) { result = 1; } } } - + if (debug) { printf("lockdownd_start_service(): DATA RECEIVED:\n\n"); fwrite(XML_query, 1, bytes, stdout); printf("end data received by lockdownd_start_service()\n"); } - + free(XML_query); xmlFreeDoc(plist); free_dictionary(dictionary); if (port && result) { *port = port_loc; return IPHONE_E_SUCCESS; - } - else return IPHONE_E_UNKNOWN_ERROR; + } else + return IPHONE_E_UNKNOWN_ERROR; } - + return IPHONE_E_UNKNOWN_ERROR; } diff --git a/src/lockdown.h b/src/lockdown.h index 62c453f..41402c4 100644 --- a/src/lockdown.h +++ b/src/lockdown.h @@ -47,7 +47,8 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control); iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid); iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key); -iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, char **root_cert_b64); +iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, + char **root_cert_b64); iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *public_key, char *host_id); void lockdownd_close(iphone_lckd_client_t control); diff --git a/src/main.c b/src/main.c index dd6e51b..c38103d 100644 --- a/src/main.c +++ b/src/main.c @@ -34,23 +34,24 @@ int debug = 1; -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ int bytes = 0, port = 0, i = 0; iphone_lckd_client_t control = NULL; iphone_device_t phone = NULL; - - if (argc > 1 && !strcasecmp(argv[1], "--debug")){ + + if (argc > 1 && !strcasecmp(argv[1], "--debug")) { debug = 1; } else { debug = 0; } - + if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) { printf("No iPhone found, is it plugged in?\n"); return -1; - } + } - if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)){ + if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { iphone_free_device(phone); return -1; } @@ -62,34 +63,36 @@ int main(int argc, char *argv[]) { } iphone_lckd_start_service(control, "com.apple.afc", &port); - + if (port) { iphone_afc_client_t afc = NULL; iphone_afc_new_client(phone, 3432, port, &afc); if (afc) { char **dirs = NULL; iphone_afc_get_dir_list(afc, "/eafaedf", &dirs); - if (!dirs) iphone_afc_get_dir_list(afc, "/", &dirs); + if (!dirs) + iphone_afc_get_dir_list(afc, "/", &dirs); printf("Directory time.\n"); for (i = 0; dirs[i]; i++) { printf("/%s\n", dirs[i]); } - + g_strfreev(dirs); iphone_afc_get_devinfo(afc, &dirs); if (dirs) { - for (i = 0; dirs[i]; i+=2) { - printf("%s: %s\n", dirs[i], dirs[i+1]); + for (i = 0; dirs[i]; i += 2) { + printf("%s: %s\n", dirs[i], dirs[i + 1]); } } g_strfreev(dirs); - + iphone_afc_file_t my_file = NULL; struct stat stbuf; - iphone_afc_get_file_attr ( afc, "/iTunesOnTheGoPlaylist.plist", &stbuf ); - if (IPHONE_E_SUCCESS == iphone_afc_open_file(afc, "/iTunesOnTheGoPlaylist.plist", IPHONE_AFC_FILE_READ, &my_file) && my_file) { + iphone_afc_get_file_attr(afc, "/iTunesOnTheGoPlaylist.plist", &stbuf); + if (IPHONE_E_SUCCESS == + iphone_afc_open_file(afc, "/iTunesOnTheGoPlaylist.plist", IPHONE_AFC_FILE_READ, &my_file) && my_file) { printf("A file size: %i\n", stbuf.st_size); - char *file_data = (char*)malloc(sizeof(char) * stbuf.st_size); + char *file_data = (char *) malloc(sizeof(char) * stbuf.st_size); iphone_afc_read_file(afc, my_file, file_data, stbuf.st_size, &bytes); if (bytes >= 0) { printf("The file's data:\n"); @@ -98,49 +101,58 @@ int main(int argc, char *argv[]) { printf("\nClosing my file.\n"); iphone_afc_close_file(afc, my_file); free(file_data); - } else printf("couldn't open a file\n"); - + } else + printf("couldn't open a file\n"); + iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_WRITE, &my_file); if (my_file) { char *outdatafile = strdup("this is a bitchin text file\n"); iphone_afc_write_file(afc, my_file, outdatafile, strlen(outdatafile), &bytes); free(outdatafile); - if (bytes > 0) printf("Wrote a surprise. ;)\n"); - else printf("I wanted to write a surprise, but... :(\n"); + if (bytes > 0) + printf("Wrote a surprise. ;)\n"); + else + printf("I wanted to write a surprise, but... :(\n"); iphone_afc_close_file(afc, my_file); } printf("Deleting a file...\n"); bytes = iphone_afc_delete_file(afc, "/delme"); - if (bytes) printf("Success.\n"); - else printf("Failure. (expected unless you have a /delme file on your phone)\n"); - + if (bytes) + printf("Success.\n"); + else + printf("Failure. (expected unless you have a /delme file on your phone)\n"); + printf("Renaming a file...\n"); bytes = iphone_afc_rename_file(afc, "/renme", "/renme2"); - if (bytes > 0) printf("Success.\n"); - else printf("Failure. (expected unless you have a /renme file on your phone)\n"); - + if (bytes > 0) + printf("Success.\n"); + else + printf("Failure. (expected unless you have a /renme file on your phone)\n"); + printf("Seek & read\n"); iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_READ, &my_file); - if (IPHONE_E_SUCCESS != iphone_afc_seek_file(afc, my_file, 5)) printf("WARN: SEEK DID NOT WORK\n"); - char *threeletterword = (char*)malloc(sizeof(char) * 5); + if (IPHONE_E_SUCCESS != iphone_afc_seek_file(afc, my_file, 5)) + printf("WARN: SEEK DID NOT WORK\n"); + char *threeletterword = (char *) malloc(sizeof(char) * 5); iphone_afc_read_file(afc, my_file, threeletterword, 3, &bytes); threeletterword[3] = '\0'; - if (bytes > 0) printf("Result: %s\n", threeletterword); - else printf("Couldn't read!\n"); + if (bytes > 0) + printf("Result: %s\n", threeletterword); + else + printf("Couldn't read!\n"); free(threeletterword); iphone_afc_close_file(afc, my_file); - + } iphone_afc_free_client(afc); } else { printf("Start service failure.\n"); } - + printf("All done.\n"); iphone_lckd_free_client(control); iphone_free_device(phone); - + return 0; } - diff --git a/src/plist.c b/src/plist.c index 31490d0..c4d6bfa 100644 --- a/src/plist.c +++ b/src/plist.c @@ -40,12 +40,12 @@ const char *plist_base = "\n\ * * @return The formatted string. */ -char* format_string(const char* buf, int cols, int depth) +char *format_string(const char *buf, int cols, int depth) { int colw = depth + cols + 1; int len = strlen(buf); int nlines = len / cols + 1; - char* new_buf = (char*)malloc(nlines * colw + depth + 1); + char *new_buf = (char *) malloc(nlines * colw + depth + 1); int i = 0; int j = 0; @@ -53,18 +53,18 @@ char* format_string(const char* buf, int cols, int depth) assert(depth >= 0); // Inserts new lines and tabs at appropriate locations - for (i = 0; i < nlines; i++){ + for (i = 0; i < nlines; i++) { new_buf[i * colw] = '\n'; for (j = 0; j < depth; j++) new_buf[i * colw + 1 + j] = '\t'; memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols); } - new_buf[len+(1+depth)*nlines] = '\n'; + new_buf[len + (1 + depth) * nlines] = '\n'; // Inserts final row of indentation and termination character for (j = 0; j < depth; j++) - new_buf[len+(1+depth)*nlines + 1 + j] = '\t'; - new_buf[len+(1+depth)*nlines+depth+1] = '\0'; + new_buf[len + (1 + depth) * nlines + 1 + j] = '\t'; + new_buf[len + (1 + depth) * nlines + depth + 1] = '\0'; return new_buf; } @@ -73,12 +73,14 @@ char* format_string(const char* buf, int cols, int depth) * * @return The plist XML document. */ -xmlDocPtr new_plist() { +xmlDocPtr new_plist() +{ char *plist = strdup(plist_base); xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0); - if (!plist_xml) return NULL; - + if (!plist_xml) + return NULL; + free(plist); return plist_xml; @@ -88,8 +90,10 @@ xmlDocPtr new_plist() { * * @param plist The XML document to destroy. */ -void free_plist(xmlDocPtr plist) { - if (!plist) return; +void free_plist(xmlDocPtr plist) +{ + if (!plist) + return; xmlFreeDoc(plist); } @@ -109,14 +113,17 @@ void free_plist(xmlDocPtr plist) { * * @return The newly created node. */ -xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode *to_node, int depth) { +xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth) +{ int i = 0; xmlNode *child; - if (!plist) return NULL; + if (!plist) + return NULL; assert(depth >= 0); - if (!to_node) to_node = xmlDocGetRootElement(plist); - + if (!to_node) + to_node = xmlDocGetRootElement(plist); + for (i = 0; i < depth; i++) { xmlNodeAddContent(to_node, "\t"); } @@ -136,12 +143,13 @@ xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *conte * * @return The newly created key node. */ -xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { +xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) +{ xmlNode *keyPtr; keyPtr = add_child_to_plist(plist, "key", key, dict, depth); add_child_to_plist(plist, "string", value, dict, depth); - + return keyPtr; } @@ -155,9 +163,10 @@ xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *ke * * @return The newly created dict node. */ -xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { +xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) +{ xmlNode *child; - + add_child_to_plist(plist, "key", key, dict, depth); child = add_child_to_plist(plist, "dict", value, dict, depth); @@ -174,12 +183,13 @@ xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, cons * * @return The newly created key node. */ -xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) { +xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth) +{ xmlNode *keyPtr; keyPtr = add_child_to_plist(plist, "key", key, dict, depth); add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth); - + return keyPtr; } @@ -190,17 +200,18 @@ xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *k * @return An array where each even number is a key and the odd numbers are * values. If the odd number is \0, that's the end of the list. */ -char **read_dict_element_strings(xmlNode *dict) { +char **read_dict_element_strings(xmlNode * dict) +{ char **return_me = NULL, **old = NULL; int current_length = 0; int current_pos = 0; xmlNode *dict_walker; - + for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) { if (!xmlStrcmp(dict_walker->name, "key")) { current_length += 2; old = return_me; - return_me = realloc(return_me, sizeof(char*) * current_length); + return_me = realloc(return_me, sizeof(char *) * current_length); if (!return_me) { free(old); return NULL; @@ -209,25 +220,26 @@ char **read_dict_element_strings(xmlNode *dict) { return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next); } } - + old = return_me; - return_me = realloc(return_me, sizeof(char*) * (current_length+1)); + return_me = realloc(return_me, sizeof(char *) * (current_length + 1)); return_me[current_pos] = NULL; - + return return_me; } /** Destroys a dictionary as returned by read_dict_element_strings */ -void free_dictionary(char **dictionary) { +void free_dictionary(char **dictionary) +{ int i = 0; - - if (!dictionary) return; - + + if (!dictionary) + return; + for (i = 0; dictionary[i]; i++) { free(dictionary[i]); } free(dictionary); } - diff --git a/src/plist.h b/src/plist.h index ab79386..b27a0c5 100644 --- a/src/plist.h +++ b/src/plist.h @@ -25,14 +25,14 @@ #include #include -xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth); -xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth); -xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth); -xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode *to_node, int depth); +xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); +xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); +xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth); +xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth); void free_plist(xmlDocPtr plist); xmlDocPtr new_plist(); -char **read_dict_element_strings(xmlNode *dict); +char **read_dict_element_strings(xmlNode * dict); void free_dictionary(char **dictionary); #endif diff --git a/src/usbmux.c b/src/usbmux.c index 35f2ef3..cf1a2d9 100644 --- a/src/usbmux.c +++ b/src/usbmux.c @@ -39,8 +39,9 @@ static int clients = 0; * * @return A USBMux packet */ -usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) { - usbmux_tcp_header *conn = (usbmux_tcp_header*)malloc(sizeof(usbmux_tcp_header)); +usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) +{ + usbmux_tcp_header *conn = (usbmux_tcp_header *) malloc(sizeof(usbmux_tcp_header)); conn->type = htonl(6); conn->length = 28; conn->sport = htons(s_port); @@ -58,8 +59,9 @@ usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) { * * @return A USBMux header */ -usbmux_version_header *version_header() { - usbmux_version_header *version = (usbmux_version_header*)malloc(sizeof(usbmux_version_header)); +usbmux_version_header *version_header() +{ + usbmux_version_header *version = (usbmux_version_header *) malloc(sizeof(usbmux_version_header)); version->type = 0; version->length = htonl(20); version->major = htonl(1); @@ -76,11 +78,13 @@ usbmux_version_header *version_header() { * * @param connection The connection to delete from the tracking list. */ -void delete_connection(iphone_umux_client_t connection) { - iphone_umux_client_t *newlist = (iphone_umux_client_t*)malloc(sizeof(iphone_umux_client_t) * (clients - 1)); +void delete_connection(iphone_umux_client_t connection) +{ + iphone_umux_client_t *newlist = (iphone_umux_client_t *) malloc(sizeof(iphone_umux_client_t) * (clients - 1)); int i = 0, j = 0; for (i = 0; i < clients; i++) { - if (connlist[i] == connection) continue; + if (connlist[i] == connection) + continue; else { newlist[j] = connlist[i]; j++; @@ -89,8 +93,10 @@ void delete_connection(iphone_umux_client_t connection) { free(connlist); connlist = newlist; clients--; - if (connection->recv_buffer) free(connection->recv_buffer); - if (connection->header) free(connection->header); + if (connection->recv_buffer) + free(connection->recv_buffer); + if (connection->header) + free(connection->header); connection->r_len = 0; free(connection); } @@ -101,8 +107,10 @@ void delete_connection(iphone_umux_client_t connection) { * @param connection The connection to add to the global list of connections. */ -void add_connection(iphone_umux_client_t connection) { - iphone_umux_client_t* newlist = (iphone_umux_client_t*)realloc(connlist, sizeof(iphone_umux_client_t) * (clients+1)); +void add_connection(iphone_umux_client_t connection) +{ + iphone_umux_client_t *newlist = + (iphone_umux_client_t *) realloc(connlist, sizeof(iphone_umux_client_t) * (clients + 1)); newlist[clients] = connection; connlist = newlist; clients++; @@ -116,13 +124,15 @@ void add_connection(iphone_umux_client_t connection) { * @param client A mux TCP header for the connection which is used for tracking and data transfer. * @return IPHONE_E_SUCCESS on success, an error code otherwise. */ -iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port, uint16_t dst_port, iphone_umux_client_t *client ){ +iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port, uint16_t dst_port, + iphone_umux_client_t * client) +{ if (!device || !src_port || !dst_port) return IPHONE_E_INVALID_ARG; int bytes = 0; // Initialize connection stuff - iphone_umux_client_t new_connection = (iphone_umux_client_t)malloc(sizeof(struct iphone_umux_client_int)); + iphone_umux_client_t new_connection = (iphone_umux_client_t) malloc(sizeof(struct iphone_umux_client_int)); new_connection->header = new_mux_packet(src_port, dst_port); // blargg @@ -130,18 +140,19 @@ iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port new_connection->header->tcp_flags = 0x02; new_connection->header->length = htonl(new_connection->header->length); new_connection->header->length16 = htons(new_connection->header->length16); - - if (send_to_phone(device, (char*)new_connection->header, sizeof(usbmux_tcp_header)) >= 0) { + + if (send_to_phone(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)) >= 0) { usbmux_tcp_header *response; - response = (usbmux_tcp_header*)malloc(sizeof(usbmux_tcp_header)); - bytes = recv_from_phone(device, (char*)response, sizeof(*response)); + response = (usbmux_tcp_header *) malloc(sizeof(usbmux_tcp_header)); + bytes = recv_from_phone(device, (char *) response, sizeof(*response)); if (response->tcp_flags != 0x12) { free(response); return IPHONE_E_UNKNOWN_ERROR; } else { free(response); - if (debug) printf("mux_connect: connection success\n"); + if (debug) + printf("mux_connect: connection success\n"); new_connection->header->tcp_flags = 0x10; new_connection->header->scnt = 1; new_connection->header->ocnt = 1; @@ -156,7 +167,6 @@ iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port return IPHONE_E_NOT_ENOUGH_DATA; } } - // if we get to this point it's probably bad return IPHONE_E_UNKNOWN_ERROR; } @@ -168,22 +178,24 @@ iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port * * @return IPHONE_E_SUCCESS on success. */ -iphone_error_t iphone_mux_free_client ( iphone_umux_client_t client ) { - if (!client || !client->phone) return; - +iphone_error_t iphone_mux_free_client(iphone_umux_client_t client) +{ + if (!client || !client->phone) + return; + client->header->tcp_flags = 0x04; client->header->scnt = htonl(client->header->scnt); client->header->ocnt = htonl(client->header->ocnt); int bytes = 0; - - bytes = usb_bulk_write(client->phone->device, BULKOUT, (char*)client->header, sizeof(usbmux_tcp_header), 800); - if(debug && bytes < 0) + + bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800); + if (debug && bytes < 0) printf("iphone_muxèfree_client(): when writing, libusb gave me the error: %s\n", usb_strerror()); - bytes = usb_bulk_read(client->phone->device, BULKIN, (char*)client->header, sizeof(usbmux_tcp_header), 800); - if(debug && bytes < 0) + bytes = usb_bulk_read(client->phone->device, BULKIN, (char *) client->header, sizeof(usbmux_tcp_header), 800); + if (debug && bytes < 0) printf("get_iPhone(): when reading, libusb gave me the error: %s\n", usb_strerror()); - + delete_connection(client); return IPHONE_E_SUCCESS; @@ -201,30 +213,35 @@ iphone_error_t iphone_mux_free_client ( iphone_umux_client_t client ) { * @return IPHONE_E_SUCCESS on success. */ -iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t datalen, uint32_t *sent_bytes ) { - if (!client->phone || !client || !data || datalen == 0 || !sent_bytes) return IPHONE_E_INVALID_ARG; +iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, uint32_t datalen, uint32_t * sent_bytes) +{ + if (!client->phone || !client || !data || datalen == 0 || !sent_bytes) + return IPHONE_E_INVALID_ARG; // client->scnt and client->ocnt should already be in host notation... // we don't need to change them juuuust yet. *sent_bytes = 0; - if (debug) printf("mux_send(): client wants to send %i bytes\n", datalen); - char *buffer = (char*)malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding + if (debug) + printf("mux_send(): client wants to send %i bytes\n", datalen); + char *buffer = (char *) malloc(sizeof(usbmux_tcp_header) + datalen + 2); // allow 2 bytes of safety padding // Set the length and pre-emptively htonl/htons it client->header->length = htonl(sizeof(usbmux_tcp_header) + datalen); client->header->length16 = htons(sizeof(usbmux_tcp_header) + datalen); - + // Put scnt and ocnt into big-endian notation client->header->scnt = htonl(client->header->scnt); client->header->ocnt = htonl(client->header->ocnt); // Concatenation of stuff in the buffer. memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); - memcpy(buffer+sizeof(usbmux_tcp_header), data, datalen); - + memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); + // We have a buffer full of data, we should now send it to the phone. - if (debug) printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header)+datalen, buffer); + if (debug) + printf("actually sending %zi bytes of data at %p\n", sizeof(usbmux_tcp_header) + datalen, buffer); + - - *sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header)+datalen); - if (debug) printf("mux_send: sent %i bytes!\n", *sent_bytes); + *sent_bytes = send_to_phone(client->phone, buffer, sizeof(usbmux_tcp_header) + datalen); + if (debug) + printf("mux_send: sent %i bytes!\n", *sent_bytes); // Now that we've sent it off, we can clean up after our sloppy selves. if (debug) { FILE *packet = fopen("packet", "a+"); @@ -232,24 +249,25 @@ iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, fclose(packet); printf("\n"); } - - if (buffer) free(buffer); + + if (buffer) + free(buffer); // Re-calculate scnt and ocnt client->header->scnt = ntohl(client->header->scnt) + datalen; client->header->ocnt = ntohl(client->header->ocnt); - + // Revert lengths client->header->length = ntohl(client->header->length); client->header->length16 = ntohs(client->header->length16); - + // Now return the bytes. - if (*sent_bytes < sizeof(usbmux_tcp_header)+datalen) { + if (*sent_bytes < sizeof(usbmux_tcp_header) + datalen) { *sent_bytes = 0; return IPHONE_E_NOT_ENOUGH_DATA; } else { - *sent_bytes = *sent_bytes - 28; // actual length sent. :/ + *sent_bytes = *sent_bytes - 28; // actual length sent. :/ } - + return IPHONE_E_SUCCESS; } @@ -261,7 +279,8 @@ iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, * * @return How many bytes were read, or -1 if something bad happens. */ -iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t *recv_bytes ) { +iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t * recv_bytes) +{ if (!client || !data || datalen == 0 || !recv_bytes) return IPHONE_E_INVALID_ARG; @@ -269,18 +288,19 @@ iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32 * Order of operation: * 1.) Check if the client has a pre-received buffer. * 2.) If so, fill data with the buffer, as much as needed. - * a.) Return quickly if the buffer has enough - * b.) If the buffer is only part of the datalen, get the rest of datalen (and if we can't, just return) + * a.) Return quickly if the buffer has enough + * b.) If the buffer is only part of the datalen, get the rest of datalen (and if we can't, just return) * 3.) If not, receive directly from the phone. - * a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation. - * b.) If not, find the client the ports belong to and fill that client's buffer, then return mux_recv with the same args to try again. + * a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation. + * b.) If not, find the client the ports belong to and fill that client's buffer, then return mux_recv with the same args to try again. */ - if (debug) printf("mux_recv: datalen == %i\n", datalen); + if (debug) + printf("mux_recv: datalen == %i\n", datalen); int bytes = 0, i = 0, complex = 0, offset = 0; *recv_bytes = 0; char *buffer = NULL; usbmux_tcp_header *header = NULL; - + if (client->recv_buffer) { if (client->r_len >= datalen) { memcpy(data, client->recv_buffer, datalen); @@ -290,41 +310,42 @@ iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32 client->r_len = 0; client->recv_buffer = NULL; } else { - buffer = (char*)malloc(sizeof(char) * (client->r_len - datalen)); - memcpy(buffer, client->recv_buffer+datalen, (client->r_len - datalen)); + buffer = (char *) malloc(sizeof(char) * (client->r_len - datalen)); + memcpy(buffer, client->recv_buffer + datalen, (client->r_len - datalen)); client->r_len -= datalen; free(client->recv_buffer); client->recv_buffer = buffer; } - + // Since we were able to fill the data straight from our buffer, we can just return datalen. See 2a above. return datalen; } else { memcpy(data, client->recv_buffer, client->r_len); - free(client->recv_buffer); // don't need to deal with anymore, but... - offset = client->r_len; // see #2b, above + free(client->recv_buffer); // don't need to deal with anymore, but... + offset = client->r_len; // see #2b, above client->r_len = 0; } - } // End of what to do if we have a pre-buffer. See #1 and #2 above. - - buffer = (char*)malloc(sizeof(char) * 131072); // make sure we get enough ;) - + } // End of what to do if we have a pre-buffer. See #1 and #2 above. + + buffer = (char *) malloc(sizeof(char) * 131072); // make sure we get enough ;) + // See #3. bytes = recv_from_phone(client->phone, buffer, 131072); if (bytes < 28) { free(buffer); - if (debug) printf("mux_recv: Did not even get the header.\n"); + if (debug) + printf("mux_recv: Did not even get the header.\n"); return IPHONE_E_NOT_ENOUGH_DATA; } - - header = (usbmux_tcp_header*)buffer; + + header = (usbmux_tcp_header *) buffer; if (header->sport != client->header->dport || header->dport != client->header->sport) { // Ooooops -- we got someone else's packet. // We gotta stick it in their buffer. (Take that any old way you want ;) ) for (i = 0; i < clients; i++) { if (connlist[i]->header->sport == header->dport && connlist[i]->header->dport == header->sport) { // we have a winner. - char *nfb = (char*)malloc(sizeof(char) * (connlist[i]->r_len + (bytes - 28))); + char *nfb = (char *) malloc(sizeof(char) * (connlist[i]->r_len + (bytes - 28))); if (connlist[i]->recv_buffer && connlist[i]->r_len) { memcpy(nfb, connlist[i]->recv_buffer, connlist[i]->r_len); free(connlist[i]->recv_buffer); @@ -332,45 +353,44 @@ iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32 connlist[i]->r_len += bytes - 28; //connlist[i]->recv_buffer = (char*)realloc(connlist[i]->recv_buffer, sizeof(char) * client->r_len); // grow their buffer connlist[i]->recv_buffer = nfb; - nfb = NULL; // A cookie for you if you can guess what "nfb" means. + nfb = NULL; // A cookie for you if you can guess what "nfb" means. complex = connlist[i]->r_len - (bytes - 28); - memcpy(connlist[i]->recv_buffer+complex, buffer+28, bytes-28); // paste into their buffer - connlist[i]->header->ocnt += bytes-28; + memcpy(connlist[i]->recv_buffer + complex, buffer + 28, bytes - 28); // paste into their buffer + connlist[i]->header->ocnt += bytes - 28; } } // If it wasn't ours, it's been handled by this point... or forgotten. // Free our buffer and continue. free(buffer); buffer = NULL; - return iphone_mux_recv(client, data, datalen, recv_bytes); // recurse back in to try again + return iphone_mux_recv(client, data, datalen, recv_bytes); // recurse back in to try again } - // The packet was absolutely meant for us if it hits this point. // The pre-buffer has been taken care of, so, again, if we're at this point we have to read from the phone. - - if ((bytes-28) > datalen) { + + if ((bytes - 28) > datalen) { // Copy what we need into the data, buffer the rest because we can. - memcpy(data+offset, buffer+28, datalen); // data+offset: see #2b, above - complex = client->r_len + (bytes-28) - datalen; - client->recv_buffer = (char*)realloc(client->recv_buffer, (sizeof(char) * complex)); + memcpy(data + offset, buffer + 28, datalen); // data+offset: see #2b, above + complex = client->r_len + (bytes - 28) - datalen; + client->recv_buffer = (char *) realloc(client->recv_buffer, (sizeof(char) * complex)); client->r_len = complex; - complex = client->r_len - (bytes-28) - datalen; - memcpy(client->recv_buffer+complex, buffer+28+datalen, (bytes-28) - datalen); + complex = client->r_len - (bytes - 28) - datalen; + memcpy(client->recv_buffer + complex, buffer + 28 + datalen, (bytes - 28) - datalen); free(buffer); - client->header->ocnt += bytes-28; + client->header->ocnt += bytes - 28; *recv_bytes = datalen; return IPHONE_E_SUCCESS; } else { // Fill the data with what we have, and just return. - memcpy(data+offset, buffer+28, bytes-28); // data+offset: see #2b, above - client->header->ocnt += bytes-28; + memcpy(data + offset, buffer + 28, bytes - 28); // data+offset: see #2b, above + client->header->ocnt += bytes - 28; free(buffer); *recv_bytes = bytes - 28; return IPHONE_E_SUCCESS; } - + // If we get to this point, 'tis probably bad. - if (debug) printf("mux_recv: Heisenbug: bytes and datalen not matching up\n"); + if (debug) + printf("mux_recv: Heisenbug: bytes and datalen not matching up\n"); return IPHONE_E_UNKNOWN_ERROR; } - diff --git a/src/userpref.c b/src/userpref.c index 5b53775..57946f7 100644 --- a/src/userpref.c +++ b/src/userpref.c @@ -39,10 +39,11 @@ extern int debug; /** Creates a freedesktop compatible configuration directory for libiphone. */ -inline void create_config_dir() { - gchar* config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL); +inline void create_config_dir() +{ + gchar *config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL); - if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) )) + if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) g_mkdir_with_parents(config_dir, 0755); g_free(config_dir); @@ -55,26 +56,29 @@ inline void create_config_dir() { * * @return The string containing the HostID or NULL */ -char* get_host_id() { - char* host_id = NULL; - gchar* config_file; - GKeyFile* key_file; - gchar* loc_host_id; +char *get_host_id() +{ + char *host_id = NULL; + gchar *config_file; + GKeyFile *key_file; + gchar *loc_host_id; - config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); + config_file = + g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); /* now parse file to get the HostID */ key_file = g_key_file_new(); - if(g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { + if (g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL); if (loc_host_id) - host_id = strdup((char*)loc_host_id); + host_id = strdup((char *) loc_host_id); g_free(loc_host_id); } g_key_file_free(key_file); g_free(config_file); - if (debug) printf("get_host_id(): Using %s as HostID\n",host_id); + if (debug) + printf("get_host_id(): Using %s as HostID\n", host_id); return host_id; } @@ -85,16 +89,17 @@ char* get_host_id() { * @return 1 if the iPhone has been connected previously to this configuration * or 0 otherwise. */ -int is_device_known(char* uid) { +int is_device_known(char *uid) +{ int ret = 0; gchar *config_file; GKeyFile *key_file; gchar **devices_list, **pcur, *keyfilepath, *stored_key; GIOChannel *keyfile; - + /* first get config file */ - gchar* device_file = g_strconcat(uid, ".pem", NULL); - config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); + gchar *device_file = g_strconcat(uid, ".pem", NULL); + config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) ret = 1; g_free(config_file); @@ -110,7 +115,8 @@ int is_device_known(char* uid) { * @return 1 on success and 0 if no public key is given or if it has already * been marked as connected previously. */ -int store_device_public_key(char* uid, char* public_key) { +int store_device_public_key(char *uid, char *public_key) +{ if (NULL == public_key || is_device_known(uid)) return 0; @@ -119,14 +125,14 @@ int store_device_public_key(char* uid, char* public_key) { create_config_dir(); /* build file path */ - gchar* device_file = g_strconcat(uid, ".pem", NULL); - gchar* pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); + gchar *device_file = g_strconcat(uid, ".pem", NULL); + gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); /* decode public key for storing */ gsize decoded_size; - gchar* data = g_base64_decode (public_key, &decoded_size); + gchar *data = g_base64_decode(public_key, &decoded_size); /* store file */ - FILE* pFile = fopen(pem , "wb"); + FILE *pFile = fopen(pem, "wb"); fwrite(data, 1, decoded_size, pFile); fclose(pFile); g_free(pem); @@ -142,24 +148,25 @@ int store_device_public_key(char* uid, char* public_key) { * * @return 1 if the file contents where read successfully and 0 otherwise. */ -int read_file_in_confdir(char* file, gnutls_datum_t* data) { +int read_file_in_confdir(char *file, gnutls_datum_t * data) +{ gboolean success; gsize size; - char *content; + char *content; gchar *filepath; if (NULL == file || NULL == data) return 0; /* Read file */ - filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL); + filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL); success = g_file_get_contents(filepath, &content, &size, NULL); g_free(filepath); /* Add it to the gnutls_datnum_t structure */ data->data = content; data->size = size; - + return success; } @@ -169,7 +176,8 @@ int read_file_in_confdir(char* file, gnutls_datum_t* data) { * * @return 1 if the file was successfully read and 0 otherwise. */ -int get_root_private_key(gnutls_datum_t* root_privkey) { +int get_root_private_key(gnutls_datum_t * root_privkey) +{ return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey); } @@ -179,7 +187,8 @@ int get_root_private_key(gnutls_datum_t* root_privkey) { * * @return 1 if the file was successfully read and 0 otherwise. */ -int get_host_private_key(gnutls_datum_t* host_privkey) { +int get_host_private_key(gnutls_datum_t * host_privkey) +{ return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey); } @@ -189,7 +198,8 @@ int get_host_private_key(gnutls_datum_t* host_privkey) { * * @return 1 if the file was successfully read and 0 otherwise. */ -int get_root_certificate(gnutls_datum_t* root_cert) { +int get_root_certificate(gnutls_datum_t * root_cert) +{ return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert); } @@ -199,7 +209,8 @@ int get_root_certificate(gnutls_datum_t* root_cert) { * * @return 1 if the file was successfully read and 0 otherwise. */ -int get_host_certificate(gnutls_datum_t* host_cert) { +int get_host_certificate(gnutls_datum_t * host_cert) +{ return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert); } @@ -215,30 +226,34 @@ int get_host_certificate(gnutls_datum_t* host_cert) { * * @return 1 on success and 0 otherwise. */ -int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* host_key, gnutls_datum_t* root_cert, gnutls_datum_t* host_cert) { - FILE * pFile; - gchar* pem; - GKeyFile* key_file; +int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert, + gnutls_datum_t * host_cert) +{ + FILE *pFile; + gchar *pem; + GKeyFile *key_file; gsize length; gchar *buf, *config_file; - GIOChannel* file; + GIOChannel *file; if (!host_id || !root_key || !host_key || !root_cert || !host_cert) return 0; - /* Make sure config directory exists*/ + /* Make sure config directory exists */ create_config_dir(); /* Now parse file to get the HostID */ key_file = g_key_file_new(); /* Store in config file */ - if (debug) printf("init_config_file(): setting hostID to %s\n", host_id); + if (debug) + printf("init_config_file(): setting hostID to %s\n", host_id); g_key_file_set_value(key_file, "Global", "HostID", host_id); /* Write config file on disk */ - buf = g_key_file_to_data(key_file, &length,NULL); - config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); + buf = g_key_file_to_data(key_file, &length, NULL); + config_file = + g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); file = g_io_channel_new_file(config_file, "w", NULL); g_free(config_file); g_io_channel_write_chars(file, buf, length, NULL, NULL); @@ -248,27 +263,27 @@ int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* ho g_key_file_free(key_file); /* Now write keys and certificates to disk */ - pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL); - pFile = fopen(pem , "wb"); - fwrite(root_key->data, 1 , root_key->size , pFile ); + pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL); + pFile = fopen(pem, "wb"); + fwrite(root_key->data, 1, root_key->size, pFile); fclose(pFile); g_free(pem); - pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL); - pFile = fopen(pem , "wb"); - fwrite(host_key->data, 1 , host_key->size , pFile); + pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL); + pFile = fopen(pem, "wb"); + fwrite(host_key->data, 1, host_key->size, pFile); fclose(pFile); g_free(pem); - pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL); - pFile = fopen(pem , "wb"); - fwrite(root_cert->data, 1 , root_cert->size , pFile); + pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL); + pFile = fopen(pem, "wb"); + fwrite(root_cert->data, 1, root_cert->size, pFile); fclose(pFile); g_free(pem); - pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL); - pFile = fopen(pem , "wb"); - fwrite(host_cert->data, 1 , host_cert->size , pFile); + pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL); + pFile = fopen(pem, "wb"); + fwrite(host_cert->data, 1, host_cert->size, pFile); fclose(pFile); g_free(pem); diff --git a/src/userpref.h b/src/userpref.h index 553c5df..5171929 100644 --- a/src/userpref.h +++ b/src/userpref.h @@ -28,44 +28,44 @@ * * @return the HostID if exist in config file. Returns NULL otherwise. */ -char* get_host_id(); +char *get_host_id(); /** * Determine if we already paired this device. * * @return 1 if device is already paired. Returns 0 otherwise. */ -int is_device_known(char* uid); +int is_device_known(char *uid); /** * @return 1 if everything went well. Returns 0 otherwise. */ -int store_device_public_key(char* uid, char* public_key); +int store_device_public_key(char *uid, char *public_key); /** * @return 1 if everything went well. Returns 0 otherwise. */ -int get_root_private_key(gnutls_datum_t* root_privkey); +int get_root_private_key(gnutls_datum_t * root_privkey); /** * @return 1 if everything went well. Returns 0 otherwise. */ -int get_host_private_key(gnutls_datum_t* host_privkey); +int get_host_private_key(gnutls_datum_t * host_privkey); /** * @return 1 if everything went well. Returns 0 otherwise. */ -int get_root_certificate(gnutls_datum_t* root_cert); +int get_root_certificate(gnutls_datum_t * root_cert); /** * @return 1 if everything went well. Returns 0 otherwise. */ -int get_host_certificate(gnutls_datum_t* host_cert); +int get_host_certificate(gnutls_datum_t * host_cert); /** * Setup a brand new config file. * @return 1 if everything went well. Returns 0 otherwise. */ -int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* host_key, gnutls_datum_t* root_cert, gnutls_datum_t* host_cert); +int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert, + gnutls_datum_t * host_cert); #endif - -- cgit v1.1-32-gdbae